ds164x.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. static uchar rtc_read(unsigned int addr );
  19. static void rtc_write(unsigned int addr, uchar val);
  20. #define RTC_EPOCH 2000 /* century */
  21. /*
  22. * DS164x registers layout
  23. */
  24. #define RTC_BASE ( CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE )
  25. #define RTC_YEAR ( RTC_BASE + 0x07 )
  26. #define RTC_MONTH ( RTC_BASE + 0x06 )
  27. #define RTC_DAY_OF_MONTH ( RTC_BASE + 0x05 )
  28. #define RTC_DAY_OF_WEEK ( RTC_BASE + 0x04 )
  29. #define RTC_HOURS ( RTC_BASE + 0x03 )
  30. #define RTC_MINUTES ( RTC_BASE + 0x02 )
  31. #define RTC_SECONDS ( RTC_BASE + 0x01 )
  32. #define RTC_CONTROL ( RTC_BASE + 0x00 )
  33. #define RTC_CONTROLA RTC_CONTROL /* W=bit6, R=bit5 */
  34. #define RTC_CA_WRITE 0x80
  35. #define RTC_CA_READ 0x40
  36. #define RTC_CONTROLB RTC_SECONDS /* OSC=bit7 */
  37. #define RTC_CB_OSC_DISABLE 0x80
  38. #define RTC_CONTROLC RTC_DAY_OF_WEEK /* FT=bit6 */
  39. #define RTC_CC_FREQ_TEST 0x40
  40. /* ------------------------------------------------------------------------- */
  41. int rtc_get( struct rtc_time *tmp )
  42. {
  43. uchar sec, min, hour;
  44. uchar mday, wday, mon, year;
  45. uchar reg_a;
  46. reg_a = rtc_read( RTC_CONTROLA );
  47. /* lock clock registers for read */
  48. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_READ ));
  49. sec = rtc_read( RTC_SECONDS );
  50. min = rtc_read( RTC_MINUTES );
  51. hour = rtc_read( RTC_HOURS );
  52. mday = rtc_read( RTC_DAY_OF_MONTH );
  53. wday = rtc_read( RTC_DAY_OF_WEEK );
  54. mon = rtc_read( RTC_MONTH );
  55. year = rtc_read( RTC_YEAR );
  56. /* unlock clock registers after read */
  57. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_READ ));
  58. #ifdef RTC_DEBUG
  59. printf( "Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  60. "hr: %02x min: %02x sec: %02x\n",
  61. year, mon, mday, wday,
  62. hour, min, sec );
  63. #endif
  64. tmp->tm_sec = bcd2bin( sec & 0x7F );
  65. tmp->tm_min = bcd2bin( min & 0x7F );
  66. tmp->tm_hour = bcd2bin( hour & 0x3F );
  67. tmp->tm_mday = bcd2bin( mday & 0x3F );
  68. tmp->tm_mon = bcd2bin( mon & 0x1F );
  69. tmp->tm_wday = bcd2bin( wday & 0x07 );
  70. /* glue year in century (2000) */
  71. tmp->tm_year = bcd2bin( year ) + RTC_EPOCH;
  72. tmp->tm_yday = 0;
  73. tmp->tm_isdst= 0;
  74. #ifdef RTC_DEBUG
  75. printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  76. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  77. tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
  78. #endif
  79. return 0;
  80. }
  81. int rtc_set( struct rtc_time *tmp )
  82. {
  83. uchar reg_a;
  84. #ifdef RTC_DEBUG
  85. printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  86. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  87. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  88. #endif
  89. /* lock clock registers for write */
  90. reg_a = rtc_read( RTC_CONTROLA );
  91. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
  92. rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
  93. rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
  94. rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
  95. rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
  96. rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
  97. rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
  98. /* break year in century */
  99. rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
  100. /* unlock clock registers after read */
  101. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
  102. return 0;
  103. }
  104. void rtc_reset (void)
  105. {
  106. uchar reg_a, reg_b;
  107. reg_a = rtc_read( RTC_CONTROLA );
  108. reg_b = rtc_read( RTC_CONTROLB );
  109. if ( reg_b & RTC_CB_OSC_DISABLE )
  110. {
  111. printf( "real-time-clock was stopped. Now starting...\n" );
  112. reg_a |= RTC_CA_WRITE;
  113. reg_b &= ~RTC_CB_OSC_DISABLE;
  114. rtc_write( RTC_CONTROLA, reg_a );
  115. rtc_write( RTC_CONTROLB, reg_b );
  116. }
  117. /* make sure read/write clock register bits are cleared */
  118. reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
  119. rtc_write( RTC_CONTROLA, reg_a );
  120. }
  121. /* ------------------------------------------------------------------------- */
  122. static uchar rtc_read( unsigned int addr )
  123. {
  124. uchar val = *(volatile unsigned char*)(addr);
  125. #ifdef RTC_DEBUG
  126. printf( "rtc_read: %x:%x\n", addr, val );
  127. #endif
  128. return( val );
  129. }
  130. static void rtc_write( unsigned int addr, uchar val )
  131. {
  132. #ifdef RTC_DEBUG
  133. printf( "rtc_write: %x:%x\n", addr, val );
  134. #endif
  135. *(volatile unsigned char*)(addr) = val;
  136. }