blob: 3a77c1b63879f22057980ebcf9825a2ef8585cce [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
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
wdenke6325152005-03-17 16:43:10 +000026/* #define DEBUG 1 */
27
wdenkaffae2b2002-08-17 09:36:01 +000028#include <common.h>
29#include <command.h>
30#include <rtc.h>
31#include <i2c.h>
32
33/*
34 I Don't have an example config file but this
35 is what should be done.
36
37#define CONFIG_RTC_M41T11 1
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#define CONFIG_SYS_I2C_RTC_ADDR 0x68
wdenkaffae2b2002-08-17 09:36:01 +000039#if 0
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020040#define CONFIG_SYS_M41T11_EXT_CENTURY_DATA
wdenkaffae2b2002-08-17 09:36:01 +000041#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020042#define CONFIG_SYS_M41T11_BASE_YEAR 2000
wdenkaffae2b2002-08-17 09:36:01 +000043#endif
44*/
45
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020046#if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
wdenkaffae2b2002-08-17 09:36:01 +000047
wdenkaffae2b2002-08-17 09:36:01 +000048static unsigned bcd2bin (uchar n)
49{
50 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
51}
52
53static 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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
wdenkaffae2b2002-08-17 09:36:01 +000079
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*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086#ifndef CONFIG_SYS_M41T11_BASE_YEAR
87#define CONFIG_SYS_M41T11_BASE_YEAR 2000
wdenkaffae2b2002-08-17 09:36:01 +000088#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
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +030099int rtc_get (struct rtc_time *tmp)
wdenkaffae2b2002-08-17 09:36:01 +0000100{
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300101 int rel = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000102 uchar data[RTC_REG_CNT];
wdenkaffae2b2002-08-17 09:36:01 +0000103
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200104 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
wdenkaffae2b2002-08-17 09:36:01 +0000105
wdenk8bde7f72003-06-27 21:31:46 +0000106 if( data[RTC_SEC_ADDR] & 0x80 ){
107 printf( "m41t11 RTC Clock stopped!!!\n" );
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300108 rel = -1;
wdenk8bde7f72003-06-27 21:31:46 +0000109 }
wdenkaffae2b2002-08-17 09:36:01 +0000110 tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
111 tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
112 tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
113 tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
114 tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200115#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
116 tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
wdenk8bde7f72003-06-27 21:31:46 +0000117 + bcd2bin(data[RTC_YEARS_ADDR])
118 + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
wdenkaffae2b2002-08-17 09:36:01 +0000119#else
wdenk8bde7f72003-06-27 21:31:46 +0000120 {
121 unsigned char cent;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000123 if( !(data[RTC_HOUR_ADDR] & 0x80) ){
124 printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300125 rel = -1;
wdenk8bde7f72003-06-27 21:31:46 +0000126 }
127 if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
128 /*century flip store off new year*/
129 cent += 1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200130 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000131 }
132 tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
133 }
wdenkaffae2b2002-08-17 09:36:01 +0000134#endif
135 tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
136 tmp->tm_yday = 0;
137 tmp->tm_isdst= 0;
138
139 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
140 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
141 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300142
143 return rel;
wdenkaffae2b2002-08-17 09:36:01 +0000144}
145
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200146int rtc_set (struct rtc_time *tmp)
wdenkaffae2b2002-08-17 09:36:01 +0000147{
wdenk8bde7f72003-06-27 21:31:46 +0000148 uchar data[RTC_REG_CNT];
wdenkaffae2b2002-08-17 09:36:01 +0000149
150 debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
151 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
152 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
153
154 data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;/*just in case*/
155 data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
156 data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;/*handle cent stuff later*/
157 data[RTC_DATE_ADDR] = bin2bcd(tmp->tm_mday) & 0x3F;
158 data[RTC_MONTH_ADDR] = bin2bcd(tmp->tm_mon);
159 data[RTC_DAY_ADDR] = bin2bcd(tmp->tm_wday) & 0x07;
160
wdenk8bde7f72003-06-27 21:31:46 +0000161 data[RTC_HOUR_ADDR] |= 0x80;/*we will always use CEB*/
wdenkaffae2b2002-08-17 09:36:01 +0000162
wdenk8bde7f72003-06-27 21:31:46 +0000163 data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);/*same thing either way*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200164#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
165 if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
166 (tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
wdenk8bde7f72003-06-27 21:31:46 +0000167 printf( "m41t11 RTC setting year out of range!!need recompile\n" );
168 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200169 data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
wdenkaffae2b2002-08-17 09:36:01 +0000170#else
wdenk8bde7f72003-06-27 21:31:46 +0000171 {
172 unsigned char cent;
173 cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
174 data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200175 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, &cent, M41T11_YEAR_SIZE);
wdenk8bde7f72003-06-27 21:31:46 +0000176 }
wdenkaffae2b2002-08-17 09:36:01 +0000177#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200178 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200179
180 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000181}
182
183void rtc_reset (void)
184{
wdenk8bde7f72003-06-27 21:31:46 +0000185 unsigned char val;
wdenkaffae2b2002-08-17 09:36:01 +0000186 /* clear all control & status registers */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200187 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
wdenk8bde7f72003-06-27 21:31:46 +0000188 val = val & 0x7F;/*make sure we are running*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200189 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
wdenkaffae2b2002-08-17 09:36:01 +0000190
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200191 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
wdenk8bde7f72003-06-27 21:31:46 +0000192 val = val & 0x3F;/*turn off freq test keep calibration*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200193 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
wdenkaffae2b2002-08-17 09:36:01 +0000194}
195
196int rtc_store(int addr, unsigned char* data, int size)
197{
wdenk8bde7f72003-06-27 21:31:46 +0000198 /*don't let things wrap onto the time on a write*/
199 if( (addr+size) >= M41T11_STORAGE_SZ )
200 return 1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200201 return i2c_write( CONFIG_SYS_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
wdenkaffae2b2002-08-17 09:36:01 +0000202}
203
204int rtc_recall(int addr, unsigned char* data, int size)
205{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200206 return i2c_read( CONFIG_SYS_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
wdenkaffae2b2002-08-17 09:36:01 +0000207}
208
Jon Loeliger068b60a2007-07-10 10:27:39 -0500209#endif