ds174x.c 5.2 KB

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