m41t11.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. 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 CONFIG_SYS_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 CONFIG_SYS_M41T11_BASE_YEAR
  71. #define CONFIG_SYS_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. int rtc_get (struct rtc_time *tmp)
  81. {
  82. int rel = 0;
  83. uchar data[RTC_REG_CNT];
  84. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
  85. if( data[RTC_SEC_ADDR] & 0x80 ){
  86. printf( "m41t11 RTC Clock stopped!!!\n" );
  87. rel = -1;
  88. }
  89. tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
  90. tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
  91. tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
  92. tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
  93. tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
  94. #ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
  95. tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
  96. + bcd2bin(data[RTC_YEARS_ADDR])
  97. + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
  98. #else
  99. {
  100. unsigned char cent;
  101. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  102. if( !(data[RTC_HOUR_ADDR] & 0x80) ){
  103. printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
  104. rel = -1;
  105. }
  106. if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
  107. /*century flip store off new year*/
  108. cent += 1;
  109. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  110. }
  111. tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
  112. }
  113. #endif
  114. tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
  115. tmp->tm_yday = 0;
  116. tmp->tm_isdst= 0;
  117. debug ( "Get 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. return rel;
  121. }
  122. int rtc_set (struct rtc_time *tmp)
  123. {
  124. uchar data[RTC_REG_CNT];
  125. debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  126. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  127. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  128. data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
  129. data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
  130. data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
  131. data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
  132. data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
  133. data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
  134. data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
  135. data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
  136. #ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
  137. if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
  138. (tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
  139. printf( "m41t11 RTC setting year out of range!!need recompile\n" );
  140. }
  141. data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
  142. #else
  143. {
  144. unsigned char cent;
  145. cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
  146. data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
  147. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
  148. }
  149. #endif
  150. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
  151. return 0;
  152. }
  153. void rtc_reset (void)
  154. {
  155. unsigned char val;
  156. /* clear all control & status registers */
  157. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
  158. val = val & 0x7F;/*make sure we are running*/
  159. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
  160. i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
  161. val = val & 0x3F;/*turn off freq test keep calibration*/
  162. i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
  163. }
  164. int rtc_store(int addr, unsigned char* data, int size)
  165. {
  166. /*don't let things wrap onto the time on a write*/
  167. if( (addr+size) >= M41T11_STORAGE_SZ )
  168. return 1;
  169. return i2c_write( CONFIG_SYS_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
  170. }
  171. int rtc_recall(int addr, unsigned char* data, int size)
  172. {
  173. return i2c_read( CONFIG_SYS_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
  174. }
  175. #endif