ds164x.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * ARIO Data Networks, Inc. dchiu@ariodata.com
  5. *
  6. * modified for DS164x:
  7. * The LEOX team <team@leox.org>, http://www.leox.org
  8. *
  9. * Based on MontaVista DS1743 code and U-Boot mc146818 code
  10. */
  11. /*
  12. * Date & Time support for the DS164x RTC
  13. */
  14. /* #define RTC_DEBUG */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <rtc.h>
  18. #if defined(CONFIG_CMD_DATE)
  19. static uchar rtc_read(unsigned int addr );
  20. static void rtc_write(unsigned int addr, uchar val);
  21. #define RTC_EPOCH 2000 /* century */
  22. /*
  23. * DS164x registers layout
  24. */
  25. #define RTC_BASE ( CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE )
  26. #define RTC_YEAR ( RTC_BASE + 0x07 )
  27. #define RTC_MONTH ( RTC_BASE + 0x06 )
  28. #define RTC_DAY_OF_MONTH ( RTC_BASE + 0x05 )
  29. #define RTC_DAY_OF_WEEK ( RTC_BASE + 0x04 )
  30. #define RTC_HOURS ( RTC_BASE + 0x03 )
  31. #define RTC_MINUTES ( RTC_BASE + 0x02 )
  32. #define RTC_SECONDS ( RTC_BASE + 0x01 )
  33. #define RTC_CONTROL ( RTC_BASE + 0x00 )
  34. #define RTC_CONTROLA RTC_CONTROL /* W=bit6, R=bit5 */
  35. #define RTC_CA_WRITE 0x80
  36. #define RTC_CA_READ 0x40
  37. #define RTC_CONTROLB RTC_SECONDS /* OSC=bit7 */
  38. #define RTC_CB_OSC_DISABLE 0x80
  39. #define RTC_CONTROLC RTC_DAY_OF_WEEK /* FT=bit6 */
  40. #define RTC_CC_FREQ_TEST 0x40
  41. /* ------------------------------------------------------------------------- */
  42. int rtc_get( struct rtc_time *tmp )
  43. {
  44. uchar sec, min, hour;
  45. uchar mday, wday, mon, year;
  46. uchar reg_a;
  47. reg_a = rtc_read( RTC_CONTROLA );
  48. /* lock clock registers for read */
  49. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
  50. sec = rtc_read( RTC_SECONDS );
  51. min = rtc_read( RTC_MINUTES );
  52. hour = rtc_read( RTC_HOURS );
  53. mday = rtc_read( RTC_DAY_OF_MONTH );
  54. wday = rtc_read( RTC_DAY_OF_WEEK );
  55. mon = rtc_read( RTC_MONTH );
  56. year = rtc_read( RTC_YEAR );
  57. /* unlock clock registers after read */
  58. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
  59. #ifdef RTC_DEBUG
  60. printf( "Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  61. "hr: %02x min: %02x sec: %02x\n",
  62. year, mon, mday, wday,
  63. hour, min, sec );
  64. #endif
  65. tmp->tm_sec = bcd2bin( sec & 0x7F );
  66. tmp->tm_min = bcd2bin( min & 0x7F );
  67. tmp->tm_hour = bcd2bin( hour & 0x3F );
  68. tmp->tm_mday = bcd2bin( mday & 0x3F );
  69. tmp->tm_mon = bcd2bin( mon & 0x1F );
  70. tmp->tm_wday = bcd2bin( wday & 0x07 );
  71. /* glue year in century (2000) */
  72. tmp->tm_year = bcd2bin( year ) + RTC_EPOCH;
  73. tmp->tm_yday = 0;
  74. tmp->tm_isdst= 0;
  75. #ifdef RTC_DEBUG
  76. printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  77. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  78. tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
  79. #endif
  80. return 0;
  81. }
  82. int rtc_set( struct rtc_time *tmp )
  83. {
  84. uchar reg_a;
  85. #ifdef RTC_DEBUG
  86. printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  87. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  88. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  89. #endif
  90. /* lock clock registers for write */
  91. reg_a = rtc_read( RTC_CONTROLA );
  92. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
  93. rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
  94. rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
  95. rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
  96. rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
  97. rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
  98. rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
  99. /* break year in century */
  100. rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
  101. /* unlock clock registers after read */
  102. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
  103. return 0;
  104. }
  105. void rtc_reset (void)
  106. {
  107. uchar reg_a, reg_b;
  108. reg_a = rtc_read( RTC_CONTROLA );
  109. reg_b = rtc_read( RTC_CONTROLB );
  110. if ( reg_b & RTC_CB_OSC_DISABLE )
  111. {
  112. printf( "real-time-clock was stopped. Now starting...\n" );
  113. reg_a |= RTC_CA_WRITE;
  114. reg_b &= ~RTC_CB_OSC_DISABLE;
  115. rtc_write( RTC_CONTROLA, reg_a );
  116. rtc_write( RTC_CONTROLB, reg_b );
  117. }
  118. /* make sure read/write clock register bits are cleared */
  119. reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
  120. rtc_write( RTC_CONTROLA, reg_a );
  121. }
  122. /* ------------------------------------------------------------------------- */
  123. static uchar rtc_read( unsigned int addr )
  124. {
  125. uchar val = *(volatile unsigned char*)(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. *(volatile unsigned char*)(addr) = val;
  137. }
  138. #endif