s3c24x0_rtc.c 3.7 KB

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