1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29#include <command.h>
30#include <rtc.h>
31#include <i2c.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
47
48static 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
62
63
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 CONFIG_SYS_M41T11_EXT_CENTURY_DATA
79
80#define REG_CNT (RTC_REG_CNT+1)
81
82
83
84
85
86#ifndef CONFIG_SYS_M41T11_BASE_YEAR
87#define CONFIG_SYS_M41T11_BASE_YEAR 2000
88#endif
89
90#else
91
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
99int rtc_get (struct rtc_time *tmp)
100{
101 int rel = 0;
102 uchar data[RTC_REG_CNT];
103
104 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
105
106 if( data[RTC_SEC_ADDR] & 0x80 ){
107 printf( "m41t11 RTC Clock stopped!!!\n" );
108 rel = -1;
109 }
110 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);
115#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
116 tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
117 + bcd2bin(data[RTC_YEARS_ADDR])
118 + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
119#else
120 {
121 unsigned char cent;
122 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
123 if( !(data[RTC_HOUR_ADDR] & 0x80) ){
124 printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
125 rel = -1;
126 }
127 if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
128
129 cent += 1;
130 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
131 }
132 tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
133 }
134#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);
142
143 return rel;
144}
145
146int rtc_set (struct rtc_time *tmp)
147{
148 uchar data[RTC_REG_CNT];
149
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;
155 data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
156 data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;
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
161 data[RTC_HOUR_ADDR] |= 0x80;
162
163 data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);
164#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) ){
167 printf( "m41t11 RTC setting year out of range!!need recompile\n" );
168 }
169 data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
170#else
171 {
172 unsigned char cent;
173 cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
174 data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
175 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
176 }
177#endif
178 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
179
180 return 0;
181}
182
183void rtc_reset (void)
184{
185 unsigned char val;
186
187 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
188 val = val & 0x7F;
189 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
190
191 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
192 val = val & 0x3F;
193 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
194}
195
196int rtc_store(int addr, unsigned char* data, int size)
197{
198
199 if( (addr+size) >= M41T11_STORAGE_SZ )
200 return 1;
201 return i2c_write( CONFIG_SYS_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
202}
203
204int rtc_recall(int addr, unsigned char* data, int size)
205{
206 return i2c_read( CONFIG_SYS_I2C_RTC_ADDR, REG_CNT+addr, 1, data, size );
207}
208
209#endif
210