m41t11.c 4.8 KB

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