s3c24x0_rtc.c 3.6 KB

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