ds164x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * (C) Copyright 2002
  3. * ARIO Data Networks, Inc. dchiu@ariodata.com
  4. *
  5. * modified for DS164x:
  6. * The LEOX team <team@leox.org>, http://www.leox.org
  7. *
  8. * Based on MontaVista DS1743 code and U-Boot mc146818 code
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. /*
  29. * Date & Time support for the DS164x RTC
  30. */
  31. /* #define RTC_DEBUG */
  32. #include <common.h>
  33. #include <command.h>
  34. #include <rtc.h>
  35. #if defined(CONFIG_CMD_DATE)
  36. static uchar rtc_read(unsigned int addr );
  37. static void rtc_write(unsigned int addr, uchar val);
  38. #define RTC_EPOCH 2000 /* century */
  39. /*
  40. * DS164x registers layout
  41. */
  42. #define RTC_BASE ( CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE )
  43. #define RTC_YEAR ( RTC_BASE + 0x07 )
  44. #define RTC_MONTH ( RTC_BASE + 0x06 )
  45. #define RTC_DAY_OF_MONTH ( RTC_BASE + 0x05 )
  46. #define RTC_DAY_OF_WEEK ( RTC_BASE + 0x04 )
  47. #define RTC_HOURS ( RTC_BASE + 0x03 )
  48. #define RTC_MINUTES ( RTC_BASE + 0x02 )
  49. #define RTC_SECONDS ( RTC_BASE + 0x01 )
  50. #define RTC_CONTROL ( RTC_BASE + 0x00 )
  51. #define RTC_CONTROLA RTC_CONTROL /* W=bit6, R=bit5 */
  52. #define RTC_CA_WRITE 0x80
  53. #define RTC_CA_READ 0x40
  54. #define RTC_CONTROLB RTC_SECONDS /* OSC=bit7 */
  55. #define RTC_CB_OSC_DISABLE 0x80
  56. #define RTC_CONTROLC RTC_DAY_OF_WEEK /* FT=bit6 */
  57. #define RTC_CC_FREQ_TEST 0x40
  58. /* ------------------------------------------------------------------------- */
  59. int rtc_get( struct rtc_time *tmp )
  60. {
  61. uchar sec, min, hour;
  62. uchar mday, wday, mon, year;
  63. uchar reg_a;
  64. reg_a = rtc_read( RTC_CONTROLA );
  65. /* lock clock registers for read */
  66. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
  67. sec = rtc_read( RTC_SECONDS );
  68. min = rtc_read( RTC_MINUTES );
  69. hour = rtc_read( RTC_HOURS );
  70. mday = rtc_read( RTC_DAY_OF_MONTH );
  71. wday = rtc_read( RTC_DAY_OF_WEEK );
  72. mon = rtc_read( RTC_MONTH );
  73. year = rtc_read( RTC_YEAR );
  74. /* unlock clock registers after read */
  75. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
  76. #ifdef RTC_DEBUG
  77. printf( "Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  78. "hr: %02x min: %02x sec: %02x\n",
  79. year, mon, mday, wday,
  80. hour, min, sec );
  81. #endif
  82. tmp->tm_sec = bcd2bin( sec & 0x7F );
  83. tmp->tm_min = bcd2bin( min & 0x7F );
  84. tmp->tm_hour = bcd2bin( hour & 0x3F );
  85. tmp->tm_mday = bcd2bin( mday & 0x3F );
  86. tmp->tm_mon = bcd2bin( mon & 0x1F );
  87. tmp->tm_wday = bcd2bin( wday & 0x07 );
  88. /* glue year in century (2000) */
  89. tmp->tm_year = bcd2bin( year ) + RTC_EPOCH;
  90. tmp->tm_yday = 0;
  91. tmp->tm_isdst= 0;
  92. #ifdef RTC_DEBUG
  93. printf( "Get 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. return 0;
  98. }
  99. int rtc_set( struct rtc_time *tmp )
  100. {
  101. uchar reg_a;
  102. #ifdef RTC_DEBUG
  103. printf( "Set 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. /* lock clock registers for write */
  108. reg_a = rtc_read( RTC_CONTROLA );
  109. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
  110. rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
  111. rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
  112. rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
  113. rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
  114. rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
  115. rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
  116. /* break year in century */
  117. rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
  118. /* unlock clock registers after read */
  119. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
  120. return 0;
  121. }
  122. void rtc_reset (void)
  123. {
  124. uchar reg_a, reg_b;
  125. reg_a = rtc_read( RTC_CONTROLA );
  126. reg_b = rtc_read( RTC_CONTROLB );
  127. if ( reg_b & RTC_CB_OSC_DISABLE )
  128. {
  129. printf( "real-time-clock was stopped. Now starting...\n" );
  130. reg_a |= RTC_CA_WRITE;
  131. reg_b &= ~RTC_CB_OSC_DISABLE;
  132. rtc_write( RTC_CONTROLA, reg_a );
  133. rtc_write( RTC_CONTROLB, reg_b );
  134. }
  135. /* make sure read/write clock register bits are cleared */
  136. reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
  137. rtc_write( RTC_CONTROLA, reg_a );
  138. }
  139. /* ------------------------------------------------------------------------- */
  140. static uchar rtc_read( unsigned int addr )
  141. {
  142. uchar val = *(volatile unsigned char*)(addr);
  143. #ifdef RTC_DEBUG
  144. printf( "rtc_read: %x:%x\n", addr, val );
  145. #endif
  146. return( val );
  147. }
  148. static void rtc_write( unsigned int addr, uchar val )
  149. {
  150. #ifdef RTC_DEBUG
  151. printf( "rtc_write: %x:%x\n", addr, val );
  152. #endif
  153. *(volatile unsigned char*)(addr) = val;
  154. }
  155. #endif