mvrtc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2011
  3. * Jason Cooper <u-boot@lakedaemon.net>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Date & Time support for Marvell Integrated RTC
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <rtc.h>
  13. #include <asm/io.h>
  14. #include "mvrtc.h"
  15. /* This RTC does not support century, so we assume 20 */
  16. #define CENTURY 20
  17. int rtc_get(struct rtc_time *t)
  18. {
  19. u32 time;
  20. u32 date;
  21. struct mvrtc_registers *mvrtc_regs;
  22. mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
  23. /* read the time register */
  24. time = readl(&mvrtc_regs->time);
  25. /* read the date register */
  26. date = readl(&mvrtc_regs->date);
  27. /* test for 12 hour clock (can't tell if it's am/pm) */
  28. if (time & MVRTC_HRFMT_MSK) {
  29. printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
  30. return -1;
  31. }
  32. /* time */
  33. t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
  34. t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK);
  35. t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
  36. t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK);
  37. t->tm_wday--;
  38. /* date */
  39. t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
  40. t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK);
  41. t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
  42. t->tm_year += CENTURY * 100;
  43. /* not supported in this RTC */
  44. t->tm_yday = 0;
  45. t->tm_isdst = 0;
  46. return 0;
  47. }
  48. int rtc_set(struct rtc_time *t)
  49. {
  50. u32 time = 0; /* sets hour format bit to zero, 24hr format. */
  51. u32 date = 0;
  52. struct mvrtc_registers *mvrtc_regs;
  53. mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
  54. /* check that this code isn't 80+ years old ;-) */
  55. if ((t->tm_year / 100) != CENTURY)
  56. printf("Warning: Only century %d supported.\n", CENTURY);
  57. /* time */
  58. time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT;
  59. time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT;
  60. time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
  61. time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT;
  62. /* date */
  63. date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
  64. date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT;
  65. date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
  66. /* write the time register */
  67. writel(time, &mvrtc_regs->time);
  68. /* write the date register */
  69. writel(date, &mvrtc_regs->date);
  70. return 0;
  71. }
  72. void rtc_reset(void)
  73. {
  74. u32 time;
  75. u32 sec;
  76. struct mvrtc_registers *mvrtc_regs;
  77. mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
  78. /* no init routine for this RTC needed, just check that it's working */
  79. time = readl(&mvrtc_regs->time);
  80. sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
  81. udelay(1000000);
  82. time = readl(&mvrtc_regs->time);
  83. if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
  84. printf("Error: RTC did not increment.\n");
  85. }