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