s3c24x0_rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_RTC_S3C24X0) && (CONFIG_COMMANDS & CFG_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. /*#define DEBUG*/
  36. typedef enum {
  37. RTC_ENABLE,
  38. RTC_DISABLE
  39. } RTC_ACCESS;
  40. static inline void SetRTC_Access(RTC_ACCESS a)
  41. {
  42. S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
  43. switch (a) {
  44. case RTC_ENABLE:
  45. rtc->RTCCON |= 0x01; break;
  46. case RTC_DISABLE:
  47. rtc->RTCCON &= ~0x01; break;
  48. }
  49. }
  50. static unsigned bcd2bin (uchar n)
  51. {
  52. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  53. }
  54. static unsigned char bin2bcd (unsigned int n)
  55. {
  56. return (((n / 10) << 4) | (n % 10));
  57. }
  58. /* ------------------------------------------------------------------------- */
  59. void rtc_get (struct rtc_time *tmp)
  60. {
  61. S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
  62. uchar sec, min, hour, mday, wday, mon, year;
  63. uchar a_sec,a_min, a_hour, a_date, a_mon, a_year, a_armed;
  64. /* enable access to RTC registers */
  65. SetRTC_Access(RTC_ENABLE);
  66. /* read RTC registers */
  67. sec = rtc->BCDSEC;
  68. min = rtc->BCDMIN;
  69. hour = rtc->BCDHOUR;
  70. mday = rtc->BCDDATE;
  71. wday = rtc->BCDDAY;
  72. mon = rtc->BCDMON;
  73. year = rtc->BCDYEAR;
  74. /* read ALARM registers */
  75. a_sec = rtc->ALMSEC;
  76. a_min = rtc->ALMMIN;
  77. a_hour = rtc->ALMHOUR;
  78. a_date = rtc->ALMDATE;
  79. a_mon = rtc->ALMMON;
  80. a_year = rtc->ALMYEAR;
  81. a_armed = rtc->RTCALM;
  82. /* disable access to RTC registers */
  83. SetRTC_Access(RTC_DISABLE);
  84. #ifdef RTC_DEBUG
  85. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  86. "hr: %02x min: %02x sec: %02x\n",
  87. year, mon, mday, wday,
  88. hour, min, sec);
  89. printf ( "Alarms: %02x: year: %02x month: %02x date: %02x hour: %02x min: %02x sec: %02x\n",
  90. a_armed,
  91. a_year, a_mon, a_date,
  92. a_hour, a_min, a_sec);
  93. #endif
  94. tmp->tm_sec = bcd2bin(sec & 0x7F);
  95. tmp->tm_min = bcd2bin(min & 0x7F);
  96. tmp->tm_hour = bcd2bin(hour & 0x3F);
  97. tmp->tm_mday = bcd2bin(mday & 0x3F);
  98. tmp->tm_mon = bcd2bin(mon & 0x1F);
  99. tmp->tm_year = bcd2bin(year);
  100. tmp->tm_wday = bcd2bin(wday & 0x07);
  101. if(tmp->tm_year<70)
  102. tmp->tm_year+=2000;
  103. else
  104. tmp->tm_year+=1900;
  105. tmp->tm_yday = 0;
  106. tmp->tm_isdst= 0;
  107. #ifdef RTC_DEBUG
  108. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  109. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  110. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  111. #endif
  112. }
  113. void rtc_set (struct rtc_time *tmp)
  114. {
  115. S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
  116. uchar sec, min, hour, mday, wday, mon, year;
  117. #ifdef RTC_DEBUG
  118. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  119. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  120. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  121. #endif
  122. year = bin2bcd(tmp->tm_year % 100);
  123. mon = bin2bcd(tmp->tm_mon);
  124. wday = bin2bcd(tmp->tm_wday);
  125. mday = bin2bcd(tmp->tm_mday);
  126. hour = bin2bcd(tmp->tm_hour);
  127. min = bin2bcd(tmp->tm_min);
  128. sec = bin2bcd(tmp->tm_sec);
  129. /* enable access to RTC registers */
  130. SetRTC_Access(RTC_ENABLE);
  131. /* write RTC registers */
  132. rtc->BCDSEC = sec;
  133. rtc->BCDMIN = min;
  134. rtc->BCDHOUR = hour;
  135. rtc->BCDDATE = mday;
  136. rtc->BCDDAY = wday;
  137. rtc->BCDMON = mon;
  138. rtc->BCDYEAR = year;
  139. /* disable access to RTC registers */
  140. SetRTC_Access(RTC_DISABLE);
  141. }
  142. void rtc_reset (void)
  143. {
  144. S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
  145. rtc->RTCCON = (rtc->RTCCON & ~0x06) | 0x08;
  146. rtc->RTCCON &= ~0x08;
  147. }
  148. /* ------------------------------------------------------------------------- */
  149. #endif /* CONFIG_RTC_S3C24X0 && CFG_CMD_DATE */