1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <common.h>
15#include <command.h>
16#include <log.h>
17#include <rtc.h>
18#include <i2c.h>
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#define RTC_SEC_ADDR 0x0
40#define RTC_MIN_ADDR 0x1
41#define RTC_HOUR_ADDR 0x2
42#define RTC_DAY_ADDR 0x3
43#define RTC_DATE_ADDR 0x4
44#define RTC_MONTH_ADDR 0x5
45#define RTC_YEARS_ADDR 0x6
46
47#define RTC_REG_CNT 7
48
49#define RTC_CONTROL_ADDR 0x7
50
51
52#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
53
54#define REG_CNT (RTC_REG_CNT+1)
55
56
57
58
59
60#ifndef CONFIG_SYS_M41T11_BASE_YEAR
61#define CONFIG_SYS_M41T11_BASE_YEAR 2000
62#endif
63
64#else
65
66#define M41T11_YEAR_DATA 0x8
67#define M41T11_YEAR_SIZE 1
68#define REG_CNT (RTC_REG_CNT+1+M41T11_YEAR_SIZE)
69#endif
70
71#define M41T11_STORAGE_SZ (64-REG_CNT)
72
73int rtc_get (struct rtc_time *tmp)
74{
75 int rel = 0;
76 uchar data[RTC_REG_CNT];
77
78 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
79
80 if( data[RTC_SEC_ADDR] & 0x80 ){
81 printf( "m41t11 RTC Clock stopped!!!\n" );
82 rel = -1;
83 }
84 tmp->tm_sec = bcd2bin (data[RTC_SEC_ADDR] & 0x7F);
85 tmp->tm_min = bcd2bin (data[RTC_MIN_ADDR] & 0x7F);
86 tmp->tm_hour = bcd2bin (data[RTC_HOUR_ADDR] & 0x3F);
87 tmp->tm_mday = bcd2bin (data[RTC_DATE_ADDR] & 0x3F);
88 tmp->tm_mon = bcd2bin (data[RTC_MONTH_ADDR]& 0x1F);
89#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
90 tmp->tm_year = CONFIG_SYS_M41T11_BASE_YEAR
91 + bcd2bin(data[RTC_YEARS_ADDR])
92 + ((data[RTC_HOUR_ADDR]&0x40) ? 100 : 0);
93#else
94 {
95 unsigned char cent;
96 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
97 if( !(data[RTC_HOUR_ADDR] & 0x80) ){
98 printf( "m41t11 RTC: cann't keep track of years without CEB set\n" );
99 rel = -1;
100 }
101 if( (cent & 0x1) != ((data[RTC_HOUR_ADDR]&0x40)>>7) ){
102
103 cent += 1;
104 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
105 }
106 tmp->tm_year =((int)cent*100)+bcd2bin(data[RTC_YEARS_ADDR]);
107 }
108#endif
109 tmp->tm_wday = bcd2bin (data[RTC_DAY_ADDR] & 0x07);
110 tmp->tm_yday = 0;
111 tmp->tm_isdst= 0;
112
113 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
114 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
115 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
116
117 return rel;
118}
119
120int rtc_set (struct rtc_time *tmp)
121{
122 uchar data[RTC_REG_CNT];
123
124 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
125 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
126 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
127
128 data[RTC_SEC_ADDR] = bin2bcd(tmp->tm_sec) & 0x7F;
129 data[RTC_MIN_ADDR] = bin2bcd(tmp->tm_min);
130 data[RTC_HOUR_ADDR] = bin2bcd(tmp->tm_hour) & 0x3F;
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
135 data[RTC_HOUR_ADDR] |= 0x80;
136
137 data[RTC_YEARS_ADDR] = bin2bcd(tmp->tm_year%100);
138#ifndef CONFIG_SYS_M41T11_EXT_CENTURY_DATA
139 if( ((tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 200) ||
140 (tmp->tm_year < CONFIG_SYS_M41T11_BASE_YEAR) ){
141 printf( "m41t11 RTC setting year out of range!!need recompile\n" );
142 }
143 data[RTC_HOUR_ADDR] |= (tmp->tm_year - CONFIG_SYS_M41T11_BASE_YEAR) > 100 ? 0x40 : 0;
144#else
145 {
146 unsigned char cent;
147 cent = tmp->tm_year ? tmp->tm_year / 100 : 0;
148 data[RTC_HOUR_ADDR] |= (cent & 0x1) ? 0x40 : 0;
149 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T11_YEAR_DATA, 1, ¢, M41T11_YEAR_SIZE);
150 }
151#endif
152 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, data, RTC_REG_CNT);
153
154 return 0;
155}
156
157void rtc_reset (void)
158{
159 unsigned char val;
160
161 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, 1);
162 val = val & 0x7F;
163 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC_ADDR, 1, &val, RTC_REG_CNT);
164
165 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
166 val = val & 0x3F;
167 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CONTROL_ADDR, 1, &val, 1);
168}
169