m41t11.c 5.5 KB

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