s3c24x0_rtc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * (C) Copyright 2003
  3. * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Date & Time support for the built-in Samsung S3C24X0 RTC
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #if (defined(CONFIG_CMD_DATE))
  29. #if defined(CONFIG_S3C2400)
  30. #include <s3c2400.h>
  31. #elif defined(CONFIG_S3C2410)
  32. #include <s3c2410.h>
  33. #endif
  34. #include <rtc.h>
  35. #include <asm/io.h>
  36. /*#define DEBUG*/
  37. typedef enum {
  38. RTC_ENABLE,
  39. RTC_DISABLE
  40. } RTC_ACCESS;
  41. static inline void SetRTC_Access(RTC_ACCESS a)
  42. {
  43. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  44. switch (a) {
  45. case RTC_ENABLE:
  46. writeb(readb(&rtc->RTCCON) | 0x01, &rtc->RTCCON);
  47. break;
  48. case RTC_DISABLE:
  49. writeb(readb(&rtc->RTCCON) & ~0x01, &rtc->RTCCON);
  50. break;
  51. }
  52. }
  53. /* ------------------------------------------------------------------------- */
  54. int rtc_get(struct rtc_time *tmp)
  55. {
  56. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  57. uchar sec, min, hour, mday, wday, mon, year;
  58. uchar a_sec, a_min, a_hour, a_date, a_mon, a_year, a_armed;
  59. /* enable access to RTC registers */
  60. SetRTC_Access(RTC_ENABLE);
  61. /* read RTC registers */
  62. do {
  63. sec = readb(&rtc->BCDSEC);
  64. min = readb(&rtc->BCDMIN);
  65. hour = readb(&rtc->BCDHOUR);
  66. mday = readb(&rtc->BCDDATE);
  67. wday = readb(&rtc->BCDDAY);
  68. mon = readb(&rtc->BCDMON);
  69. year = readb(&rtc->BCDYEAR);
  70. } while (sec != readb(&rtc->BCDSEC));
  71. /* read ALARM registers */
  72. a_sec = readb(&rtc->ALMSEC);
  73. a_min = readb(&rtc->ALMMIN);
  74. a_hour = readb(&rtc->ALMHOUR);
  75. a_date = readb(&rtc->ALMDATE);
  76. a_mon = readb(&rtc->ALMMON);
  77. a_year = readb(&rtc->ALMYEAR);
  78. a_armed = readb(&rtc->RTCALM);
  79. /* disable access to RTC registers */
  80. SetRTC_Access(RTC_DISABLE);
  81. #ifdef RTC_DEBUG
  82. printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  83. "hr: %02x min: %02x sec: %02x\n",
  84. year, mon, mday, wday, hour, min, sec);
  85. printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
  86. "%02x min: %02x sec: %02x\n",
  87. a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
  88. #endif
  89. tmp->tm_sec = bcd2bin(sec & 0x7F);
  90. tmp->tm_min = bcd2bin(min & 0x7F);
  91. tmp->tm_hour = bcd2bin(hour & 0x3F);
  92. tmp->tm_mday = bcd2bin(mday & 0x3F);
  93. tmp->tm_mon = bcd2bin(mon & 0x1F);
  94. tmp->tm_year = bcd2bin(year);
  95. tmp->tm_wday = bcd2bin(wday & 0x07);
  96. if (tmp->tm_year < 70)
  97. tmp->tm_year += 2000;
  98. else
  99. tmp->tm_year += 1900;
  100. tmp->tm_yday = 0;
  101. tmp->tm_isdst = 0;
  102. #ifdef RTC_DEBUG
  103. printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  104. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  105. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  106. #endif
  107. return 0;
  108. }
  109. int rtc_set(struct rtc_time *tmp)
  110. {
  111. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  112. uchar sec, min, hour, mday, wday, mon, year;
  113. #ifdef RTC_DEBUG
  114. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  115. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  116. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  117. #endif
  118. year = bin2bcd(tmp->tm_year % 100);
  119. mon = bin2bcd(tmp->tm_mon);
  120. wday = bin2bcd(tmp->tm_wday);
  121. mday = bin2bcd(tmp->tm_mday);
  122. hour = bin2bcd(tmp->tm_hour);
  123. min = bin2bcd(tmp->tm_min);
  124. sec = bin2bcd(tmp->tm_sec);
  125. /* enable access to RTC registers */
  126. SetRTC_Access(RTC_ENABLE);
  127. /* write RTC registers */
  128. writeb(sec, &rtc->BCDSEC);
  129. writeb(min, &rtc->BCDMIN);
  130. writeb(hour, &rtc->BCDHOUR);
  131. writeb(mday, &rtc->BCDDATE);
  132. writeb(wday, &rtc->BCDDAY);
  133. writeb(mon, &rtc->BCDMON);
  134. writeb(year, &rtc->BCDYEAR);
  135. /* disable access to RTC registers */
  136. SetRTC_Access(RTC_DISABLE);
  137. return 0;
  138. }
  139. void rtc_reset(void)
  140. {
  141. struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
  142. writeb((readb(&rtc->RTCCON) & ~0x06) | 0x08, &rtc->RTCCON);
  143. writeb(readb(&rtc->RTCCON) & ~(0x08 | 0x01), &rtc->RTCCON);
  144. }
  145. #endif