ds174x.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * ARIO Data Networks, Inc. dchiu@ariodata.com
  5. *
  6. * Based on MontaVista DS1743 code and U-Boot mc146818 code
  7. */
  8. /*
  9. * Date & Time support for the DS174x RTC
  10. */
  11. /*#define DEBUG*/
  12. #include <common.h>
  13. #include <command.h>
  14. #include <rtc.h>
  15. static uchar rtc_read( unsigned int addr );
  16. static void rtc_write( unsigned int addr, uchar val);
  17. #define RTC_BASE ( CONFIG_SYS_NVRAM_SIZE + CONFIG_SYS_NVRAM_BASE_ADDR )
  18. #define RTC_YEAR ( RTC_BASE + 7 )
  19. #define RTC_MONTH ( RTC_BASE + 6 )
  20. #define RTC_DAY_OF_MONTH ( RTC_BASE + 5 )
  21. #define RTC_DAY_OF_WEEK ( RTC_BASE + 4 )
  22. #define RTC_HOURS ( RTC_BASE + 3 )
  23. #define RTC_MINUTES ( RTC_BASE + 2 )
  24. #define RTC_SECONDS ( RTC_BASE + 1 )
  25. #define RTC_CENTURY ( RTC_BASE + 0 )
  26. #define RTC_CONTROLA RTC_CENTURY
  27. #define RTC_CONTROLB RTC_SECONDS
  28. #define RTC_CONTROLC RTC_DAY_OF_WEEK
  29. #define RTC_CA_WRITE 0x80
  30. #define RTC_CA_READ 0x40
  31. #define RTC_CB_OSC_DISABLE 0x80
  32. #define RTC_CC_BATTERY_FLAG 0x80
  33. #define RTC_CC_FREQ_TEST 0x40
  34. /* ------------------------------------------------------------------------- */
  35. int rtc_get( struct rtc_time *tmp )
  36. {
  37. uchar sec, min, hour;
  38. uchar mday, wday, mon, year;
  39. int century;
  40. uchar reg_a;
  41. reg_a = rtc_read( RTC_CONTROLA );
  42. /* lock clock registers for read */
  43. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
  44. sec = rtc_read( RTC_SECONDS );
  45. min = rtc_read( RTC_MINUTES );
  46. hour = rtc_read( RTC_HOURS );
  47. mday = rtc_read( RTC_DAY_OF_MONTH );
  48. wday = rtc_read( RTC_DAY_OF_WEEK );
  49. mon = rtc_read( RTC_MONTH );
  50. year = rtc_read( RTC_YEAR );
  51. century = rtc_read( RTC_CENTURY );
  52. /* unlock clock registers after read */
  53. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
  54. #ifdef RTC_DEBUG
  55. printf( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  56. "hr: %02x min: %02x sec: %02x\n",
  57. year, mon_cent, mday, wday,
  58. hour, min, sec );
  59. #endif
  60. tmp->tm_sec = bcd2bin( sec & 0x7F );
  61. tmp->tm_min = bcd2bin( min & 0x7F );
  62. tmp->tm_hour = bcd2bin( hour & 0x3F );
  63. tmp->tm_mday = bcd2bin( mday & 0x3F );
  64. tmp->tm_mon = bcd2bin( mon & 0x1F );
  65. tmp->tm_wday = bcd2bin( wday & 0x07 );
  66. /* glue year from century and year in century */
  67. tmp->tm_year = bcd2bin( year ) +
  68. ( bcd2bin( century & 0x3F ) * 100 );
  69. tmp->tm_yday = 0;
  70. tmp->tm_isdst= 0;
  71. #ifdef RTC_DEBUG
  72. printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  73. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  74. tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
  75. #endif
  76. return 0;
  77. }
  78. int rtc_set( struct rtc_time *tmp )
  79. {
  80. uchar reg_a;
  81. #ifdef RTC_DEBUG
  82. printf( "Set 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. /* lock clock registers for write */
  87. reg_a = rtc_read( RTC_CONTROLA );
  88. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
  89. rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
  90. rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
  91. rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
  92. rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
  93. rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
  94. rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
  95. /* break year up into century and year in century */
  96. rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
  97. rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 ));
  98. /* unlock clock registers after read */
  99. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
  100. return 0;
  101. }
  102. void rtc_reset (void)
  103. {
  104. uchar reg_a, reg_b, reg_c;
  105. reg_a = rtc_read( RTC_CONTROLA );
  106. reg_b = rtc_read( RTC_CONTROLB );
  107. if ( reg_b & RTC_CB_OSC_DISABLE )
  108. {
  109. printf( "real-time-clock was stopped. Now starting...\n" );
  110. reg_a |= RTC_CA_WRITE;
  111. reg_b &= ~RTC_CB_OSC_DISABLE;
  112. rtc_write( RTC_CONTROLA, reg_a );
  113. rtc_write( RTC_CONTROLB, reg_b );
  114. }
  115. /* make sure read/write clock register bits are cleared */
  116. reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
  117. rtc_write( RTC_CONTROLA, reg_a );
  118. reg_c = rtc_read( RTC_CONTROLC );
  119. if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 )
  120. printf( "RTC battery low. Clock setting may not be reliable.\n" );
  121. }
  122. /* ------------------------------------------------------------------------- */
  123. static uchar rtc_read( unsigned int addr )
  124. {
  125. uchar val = in8( addr );
  126. #ifdef RTC_DEBUG
  127. printf( "rtc_read: %x:%x\n", addr, val );
  128. #endif
  129. return( val );
  130. }
  131. static void rtc_write( unsigned int addr, uchar val )
  132. {
  133. #ifdef RTC_DEBUG
  134. printf( "rtc_write: %x:%x\n", addr, val );
  135. #endif
  136. out8( addr, val );
  137. }