ds1556.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * (C) Copyright 2002
  3. * ARIO Data Networks, Inc. dchiu@ariodata.com
  4. *
  5. * modified for DS1556:
  6. * Frank Panno <fpanno@delphintech.com>, Delphin Technology AG
  7. *
  8. * Based on MontaVista DS1743 code and U-Boot mc146818 code
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. /*
  13. * Date & Time support for the DS1556 RTC
  14. */
  15. /*#define RTC_DEBUG */
  16. #include <common.h>
  17. #include <command.h>
  18. #include <rtc.h>
  19. #if defined(CONFIG_CMD_DATE)
  20. static uchar rtc_read( unsigned int addr );
  21. static void rtc_write( unsigned int addr, uchar val);
  22. #define RTC_BASE ( CONFIG_SYS_NVRAM_SIZE + CONFIG_SYS_NVRAM_BASE_ADDR )
  23. #define RTC_YEAR ( RTC_BASE + 0xf )
  24. #define RTC_MONTH ( RTC_BASE + 0xe )
  25. #define RTC_DAY_OF_MONTH ( RTC_BASE + 0xd )
  26. #define RTC_DAY_OF_WEEK ( RTC_BASE + 0xc )
  27. #define RTC_HOURS ( RTC_BASE + 0xb )
  28. #define RTC_MINUTES ( RTC_BASE + 0xa )
  29. #define RTC_SECONDS ( RTC_BASE + 0x9 )
  30. #define RTC_CENTURY ( RTC_BASE + 0x8 )
  31. #define RTC_CONTROLA RTC_CENTURY
  32. #define RTC_CONTROLB RTC_SECONDS
  33. #define RTC_CONTROLC RTC_BASE
  34. #define RTC_CA_WRITE 0x80
  35. #define RTC_CA_READ 0x40
  36. #define RTC_CB_OSC_DISABLE 0x80
  37. #define RTC_CC_BATTERY_FLAG 0x10
  38. #define RTC_CC_FREQ_TEST 0x40
  39. /* ------------------------------------------------------------------------- */
  40. int rtc_get( struct rtc_time *tmp )
  41. {
  42. uchar sec, min, hour;
  43. uchar mday, wday, mon, year;
  44. int century;
  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. century = rtc_read( RTC_CENTURY );
  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/cent: %02x mon: %02x mday: %02x wday: %02x "
  61. "hr: %02x min: %02x sec: %02x\n",
  62. year, century, 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 from century and year in century */
  72. tmp->tm_year = bcd2bin( year ) +
  73. ( bcd2bin( century & 0x3F ) * 100 );
  74. tmp->tm_yday = 0;
  75. tmp->tm_isdst= 0;
  76. #ifdef RTC_DEBUG
  77. printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  78. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  79. tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
  80. #endif
  81. return 0;
  82. }
  83. int rtc_set( struct rtc_time *tmp )
  84. {
  85. uchar reg_a;
  86. #ifdef RTC_DEBUG
  87. printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  88. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  89. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  90. #endif
  91. /* lock clock registers for write */
  92. reg_a = rtc_read( RTC_CONTROLA );
  93. rtc_write( RTC_CONTROLA, ( reg_a | RTC_CA_WRITE ));
  94. rtc_write( RTC_MONTH, bin2bcd( tmp->tm_mon ));
  95. rtc_write( RTC_DAY_OF_WEEK, bin2bcd( tmp->tm_wday ));
  96. rtc_write( RTC_DAY_OF_MONTH, bin2bcd( tmp->tm_mday ));
  97. rtc_write( RTC_HOURS, bin2bcd( tmp->tm_hour ));
  98. rtc_write( RTC_MINUTES, bin2bcd( tmp->tm_min ));
  99. rtc_write( RTC_SECONDS, bin2bcd( tmp->tm_sec ));
  100. /* break year up into century and year in century */
  101. rtc_write( RTC_YEAR, bin2bcd( tmp->tm_year % 100 ));
  102. rtc_write( RTC_CENTURY, bin2bcd( tmp->tm_year / 100 ));
  103. /* unlock clock registers after read */
  104. rtc_write( RTC_CONTROLA, ( reg_a & ~RTC_CA_WRITE ));
  105. return 0;
  106. }
  107. void rtc_reset (void)
  108. {
  109. uchar reg_a, reg_b, reg_c;
  110. reg_a = rtc_read( RTC_CONTROLA );
  111. reg_b = rtc_read( RTC_CONTROLB );
  112. if ( reg_b & RTC_CB_OSC_DISABLE )
  113. {
  114. printf( "real-time-clock was stopped. Now starting...\n" );
  115. reg_a |= RTC_CA_WRITE;
  116. reg_b &= ~RTC_CB_OSC_DISABLE;
  117. rtc_write( RTC_CONTROLA, reg_a );
  118. rtc_write( RTC_CONTROLB, reg_b );
  119. }
  120. /* make sure read/write clock register bits are cleared */
  121. reg_a &= ~( RTC_CA_WRITE | RTC_CA_READ );
  122. rtc_write( RTC_CONTROLA, reg_a );
  123. reg_c = rtc_read( RTC_CONTROLC );
  124. if (( reg_c & RTC_CC_BATTERY_FLAG ) == 0 )
  125. printf( "RTC battery low. Clock setting may not be reliable.\n" );
  126. }
  127. /* ------------------------------------------------------------------------- */
  128. static uchar rtc_read( unsigned int addr )
  129. {
  130. uchar val = *(volatile unsigned char*)(addr);
  131. #ifdef RTC_DEBUG
  132. printf( "rtc_read: %x:%x\n", addr, val );
  133. #endif
  134. return( val );
  135. }
  136. static void rtc_write( unsigned int addr, uchar val )
  137. {
  138. #ifdef RTC_DEBUG
  139. printf( "rtc_write: %x:%x\n", addr, val );
  140. #endif
  141. *(volatile unsigned char*)(addr) = val;
  142. }
  143. #endif