m41t11.c 4.8 KB

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