m41t11.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * (C) Copyright 2002
  3. * Andrew May, Viasat Inc, amay@viasat.com
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * M41T11 Serial Access Timekeeper(R) SRAM
  9. * can you believe a trademark on that?
  10. */
  11. /* #define DEBUG 1 */
  12. #include <common.h>
  13. #include <command.h>
  14. #include <rtc.h>
  15. #include <i2c.h>
  16. /*
  17. I Don't have an example config file but this
  18. is what should be done.
  19. #define CONFIG_RTC_M41T11 1
  20. #define CONFIG_SYS_I2C_RTC_ADDR 0x68
  21. #if 0
  22. #define CONFIG_SYS_M41T11_EXT_CENTURY_DATA
  23. #else
  24. #define CONFIG_SYS_M41T11_BASE_YEAR 2000
  25. #endif
  26. */
  27. #if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
  28. /* ------------------------------------------------------------------------- */
  29. /*
  30. these are simple defines for the chip local to here so they aren't too
  31. verbose
  32. DAY/DATE aren't nice but that is how they are on the data sheet
  33. */
  34. #define RTC_SEC_ADDR 0x0
  35. #define RTC_MIN_ADDR 0x1
  36. #define RTC_HOUR_ADDR 0x2
  37. #define RTC_DAY_ADDR 0x3
  38. #define RTC_DATE_ADDR 0x4
  39. #define RTC_MONTH_ADDR 0x5
  40. #define RTC_YEARS_ADDR 0x6
  41. #define RTC_REG_CNT 7
  42. #define RTC_CONTROL_ADDR 0x7
  43. #ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
  44. #define REG_CNT (RTC_REG_CNT+1)
  45. /*
  46. you only get 00-99 for the year we will asume you
  47. want from the year 2000 if you don't set the config
  48. */
  49. #ifndef CONFIG_SYS_M41T11_BASE_YEAR
  50. #define CONFIG_SYS_M41T11_BASE_YEAR 2000
  51. #endif
  52. #else
  53. /* we will store extra year info in byte 9*/
  54. #define M41T11_YEAR_DATA 0x8
  55. #define M41T11_YEAR_SIZE 1
  56. #define REG_CNT (RTC_REG_CNT+1+M41T11_YEAR_SIZE)
  57. #endif
  58. #define M41T11_STORAGE_SZ (64-REG_CNT)
  59. int rtc_get (struct rtc_time *tmp)
  60. {
  61. int rel = 0;
  62. uchar data[RTC_REG_CNT];
  63. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
  64. if( data[RTC_SEC_ADDR] & 0x80 ){
  65. printf( "m41t11 RTC Clock stopped!!!\n" );
  66. rel = -1;
  67. }
  68. tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
  69. tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
  70. tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
  71. tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
  72. tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
  73. #ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
  74. tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
  75. + bcd2bin(data[RTC_YEARS_ADDR])
  76. + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
  77. #else
  78. {
  79. unsigned char cent;
  80. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  81. if( !(data[RTC_HOUR_ADDR] & 0x80) ){
  82. printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
  83. rel = -1;
  84. }
  85. if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
  86. /*century flip store off new year*/
  87. cent += 1;
  88. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  89. }
  90. tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
  91. }
  92. #endif
  93. tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
  94. tmp->tm_yday = 0;
  95. tmp->tm_isdst= 0;
  96. debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  97. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  98. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  99. return rel;
  100. }
  101. int rtc_set (struct rtc_time *tmp)
  102. {
  103. uchar data[RTC_REG_CNT];
  104. debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  105. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  106. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  107. data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
  108. data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
  109. data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
  110. data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
  111. data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
  112. data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
  113. data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
  114. data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
  115. #ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
  116. if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
  117. (tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
  118. printf( "m41t11 RTC setting year out of range!!need recompile\n" );
  119. }
  120. data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
  121. #else
  122. {
  123. unsigned char cent;
  124. cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
  125. data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
  126. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  127. }
  128. #endif
  129. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
  130. return 0;
  131. }
  132. void rtc_reset (void)
  133. {
  134. unsigned char val;
  135. /* clear all control & status registers */
  136. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
  137. val = val & 0x7F;/*make sure we are running*/
  138. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
  139. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
  140. val = val & 0x3F;/*turn off freq test keep calibration*/
  141. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
  142. }
  143. #endif