m41t11.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 CFG_I2C_RTC_ADDR 0x68
  34. #if 0
  35. #define CFG_M41T11_EXT_CENTURY_DATA
  36. #else
  37. #define CFG_M41T11_BASE_YEAR 2000
  38. #endif
  39. */
  40. #if defined(CONFIG_RTC_M41T11) && defined(CFG_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
  41. static unsigned bcd2bin (uchar n)
  42. {
  43. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  44. }
  45. static unsigned char bin2bcd (unsigned int n)
  46. {
  47. return (((n / 10) << 4) | (n % 10));
  48. }
  49. /* ------------------------------------------------------------------------- */
  50. /*
  51. these are simple defines for the chip local to here so they aren't too
  52. verbose
  53. DAY/DATE aren't nice but that is how they are on the data sheet
  54. */
  55. #define RTC_SEC_ADDR 0x0
  56. #define RTC_MIN_ADDR 0x1
  57. #define RTC_HOUR_ADDR 0x2
  58. #define RTC_DAY_ADDR 0x3
  59. #define RTC_DATE_ADDR 0x4
  60. #define RTC_MONTH_ADDR 0x5
  61. #define RTC_YEARS_ADDR 0x6
  62. #define RTC_REG_CNT 7
  63. #define RTC_CONTROL_ADDR 0x7
  64. #ifndef CFG_M41T11_EXT_CENTURY_DATA
  65. #define REG_CNT (RTC_REG_CNT+1)
  66. /*
  67. you only get 00-99 for the year we will asume you
  68. want from the year 2000 if you don't set the config
  69. */
  70. #ifndef CFG_M41T11_BASE_YEAR
  71. #define CFG_M41T11_BASE_YEAR 2000
  72. #endif
  73. #else
  74. /* we will store extra year info in byte 9*/
  75. #define M41T11_YEAR_DATA 0x8
  76. #define M41T11_YEAR_SIZE 1
  77. #define REG_CNT (RTC_REG_CNT+1+M41T11_YEAR_SIZE)
  78. #endif
  79. #define M41T11_STORAGE_SZ (64-REG_CNT)
  80. void rtc_get (struct rtc_time *tmp)
  81. {
  82. uchar data[RTC_REG_CNT];
  83. i2c_read(CFG_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
  84. if( data[RTC_SEC_ADDR] & 0x80 ){
  85. printf( "m41t11 RTC Clock stopped!!!\n" );
  86. }
  87. tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
  88. tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
  89. tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
  90. tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
  91. tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
  92. #ifndef CFG_M41T11_EXT_CENTURY_DATA
  93. tmp->tm_year = CFG_M41T11_BASE_YEAR
  94. + bcd2bin(data[RTC_YEARS_ADDR])
  95. + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
  96. #else
  97. {
  98. unsigned char cent;
  99. i2c_read(CFG_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  100. if( !(data[RTC_HOUR_ADDR] & 0x80) ){
  101. printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
  102. }
  103. if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
  104. /*century flip store off new year*/
  105. cent += 1;
  106. i2c_write(CFG_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  107. }
  108. tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
  109. }
  110. #endif
  111. tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
  112. tmp->tm_yday = 0;
  113. tmp->tm_isdst= 0;
  114. debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  115. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  116. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  117. }
  118. void rtc_set (struct rtc_time *tmp)
  119. {
  120. uchar data[RTC_REG_CNT];
  121. debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  122. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  123. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  124. data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
  125. data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
  126. data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
  127. data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
  128. data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
  129. data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
  130. data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
  131. data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
  132. #ifndef CFG_M41T11_EXT_CENTURY_DATA
  133. if( ((tmp->tm_year - CFG_M41T11_BASE_YEAR) > 200) ||
  134. (tmp->tm_year < CFG_M41T11_BASE_YEAR) ){
  135. printf( "m41t11 RTC setting year out of range!!need recompile\n" );
  136. }
  137. data[RTC_HOUR_ADDR] |= (tmp->tm_year - CFG_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
  138. #else
  139. {
  140. unsigned char cent;
  141. cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
  142. data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
  143. i2c_write(CFG_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  144. }
  145. #endif
  146. i2c_write(CFG_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
  147. }
  148. void rtc_reset (void)
  149. {
  150. unsigned char val;
  151. /* clear all control & status registers */
  152. i2c_read(CFG_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
  153. val = val & 0x7F;/*make sure we are running*/
  154. i2c_write(CFG_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
  155. i2c_read(CFG_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
  156. val = val & 0x3F;/*turn off freq test keep calibration*/
  157. i2c_write(CFG_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
  158. }
  159. int rtc_store(int addr, unsigned char* data, int size)
  160. {
  161. /*don't let things wrap onto the time on a write*/
  162. if( (addr+size) >= M41T11_STORAGE_SZ )
  163. return 1;
  164. return i2c_write( CFG_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
  165. }
  166. int rtc_recall(int addr, unsigned char* data, int size)
  167. {
  168. return i2c_read( CFG_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
  169. }
  170. #endif