mvrtc.c 2.8 KB

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