s3c24x0_rtc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
  5. */
  6. /*
  7. * Date & Time support for the built-in Samsung S3C24X0 RTC
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #if (defined(CONFIG_CMD_DATE))
  12. #include <asm/arch/s3c24x0_cpu.h>
  13. #include <rtc.h>
  14. #include <asm/io.h>
  15. #include <linux/compiler.h>
  16. typedef enum {
  17. RTC_ENABLE,
  18. RTC_DISABLE
  19. } RTC_ACCESS;
  20. static inline void SetRTC_Access(RTC_ACCESS a)
  21. {
  22. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  23. switch (a) {
  24. case RTC_ENABLE:
  25. writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
  26. break;
  27. case RTC_DISABLE:
  28. writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
  29. break;
  30. }
  31. }
  32. /* ------------------------------------------------------------------------- */
  33. int rtc_get(struct rtc_time *tmp)
  34. {
  35. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  36. uchar sec, min, hour, mday, wday, mon, year;
  37. __maybe_unused uchar a_sec, a_min, a_hour, a_date,
  38. a_mon, a_year, a_armed;
  39. /* enable access to RTC registers */
  40. SetRTC_Access(RTC_ENABLE);
  41. /* read RTC registers */
  42. do {
  43. sec = readb(&rtc->bcdsec);
  44. min = readb(&rtc->bcdmin);
  45. hour = readb(&rtc->bcdhour);
  46. mday = readb(&rtc->bcddate);
  47. wday = readb(&rtc->bcdday);
  48. mon = readb(&rtc->bcdmon);
  49. year = readb(&rtc->bcdyear);
  50. } while (sec != readb(&rtc->bcdsec));
  51. /* read ALARM registers */
  52. a_sec = readb(&rtc->almsec);
  53. a_min = readb(&rtc->almmin);
  54. a_hour = readb(&rtc->almhour);
  55. a_date = readb(&rtc->almdate);
  56. a_mon = readb(&rtc->almmon);
  57. a_year = readb(&rtc->almyear);
  58. a_armed = readb(&rtc->rtcalm);
  59. /* disable access to RTC registers */
  60. SetRTC_Access(RTC_DISABLE);
  61. #ifdef RTC_DEBUG
  62. printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  63. "hr: %02x min: %02x sec: %02x\n",
  64. year, mon, mday, wday, hour, min, sec);
  65. printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
  66. "%02x min: %02x sec: %02x\n",
  67. a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
  68. #endif
  69. tmp->tm_sec = bcd2bin(sec & 0x7F);
  70. tmp->tm_min = bcd2bin(min & 0x7F);
  71. tmp->tm_hour = bcd2bin(hour & 0x3F);
  72. tmp->tm_mday = bcd2bin(mday & 0x3F);
  73. tmp->tm_mon = bcd2bin(mon & 0x1F);
  74. tmp->tm_year = bcd2bin(year);
  75. tmp->tm_wday = bcd2bin(wday & 0x07);
  76. if (tmp->tm_year < 70)
  77. tmp->tm_year += 2000;
  78. else
  79. tmp->tm_year += 1900;
  80. tmp->tm_yday = 0;
  81. tmp->tm_isdst = 0;
  82. #ifdef RTC_DEBUG
  83. printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  84. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  85. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  86. #endif
  87. return 0;
  88. }
  89. int rtc_set(struct rtc_time *tmp)
  90. {
  91. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  92. uchar sec, min, hour, mday, wday, mon, year;
  93. #ifdef RTC_DEBUG
  94. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  95. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  96. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  97. #endif
  98. year = bin2bcd(tmp->tm_year % 100);
  99. mon = bin2bcd(tmp->tm_mon);
  100. wday = bin2bcd(tmp->tm_wday);
  101. mday = bin2bcd(tmp->tm_mday);
  102. hour = bin2bcd(tmp->tm_hour);
  103. min = bin2bcd(tmp->tm_min);
  104. sec = bin2bcd(tmp->tm_sec);
  105. /* enable access to RTC registers */
  106. SetRTC_Access(RTC_ENABLE);
  107. /* write RTC registers */
  108. writeb(sec, &rtc->bcdsec);
  109. writeb(min, &rtc->bcdmin);
  110. writeb(hour, &rtc->bcdhour);
  111. writeb(mday, &rtc->bcddate);
  112. writeb(wday, &rtc->bcdday);
  113. writeb(mon, &rtc->bcdmon);
  114. writeb(year, &rtc->bcdyear);
  115. /* disable access to RTC registers */
  116. SetRTC_Access(RTC_DISABLE);
  117. return 0;
  118. }
  119. void rtc_reset(void)
  120. {
  121. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  122. writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
  123. writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
  124. }
  125. #endif