xref: /OK3568_Linux_fs/u-boot/drivers/rtc/m41t60.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2007
3*4882a593Smuzhiyun  * Larry Johnson, lrj@acm.org
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * based on rtc/m41t11.c which is ...
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * (C) Copyright 2002
8*4882a593Smuzhiyun  * Andrew May, Viasat Inc, amay@viasat.com
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * STMicroelectronics M41T60 serial access real-time clock
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* #define DEBUG 1 */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <common.h>
20*4882a593Smuzhiyun #include <command.h>
21*4882a593Smuzhiyun #include <rtc.h>
22*4882a593Smuzhiyun #include <i2c.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Convert between century and "century bits" (CB1 and CB0).  These routines
28*4882a593Smuzhiyun  * assume years are in the range 1900 - 2299.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
year2cb(unsigned const year)31*4882a593Smuzhiyun static unsigned char year2cb(unsigned const year)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	if (year < 1900 || year >= 2300)
34*4882a593Smuzhiyun 		printf("M41T60 RTC: year %d out of range\n", year);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	return (year / 100) & 0x3;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
cb2year(unsigned const cb)39*4882a593Smuzhiyun static unsigned cb2year(unsigned const cb)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	return 1900 + 100 * ((cb + 1) & 0x3);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * These are simple defines for the chip local to here so they aren't too
46*4882a593Smuzhiyun  * verbose.  DAY/DATE aren't nice but that is how they are on the data sheet.
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun #define RTC_SEC		0x0
49*4882a593Smuzhiyun #define RTC_MIN		0x1
50*4882a593Smuzhiyun #define RTC_HOUR	0x2
51*4882a593Smuzhiyun #define RTC_DAY		0x3
52*4882a593Smuzhiyun #define RTC_DATE	0x4
53*4882a593Smuzhiyun #define RTC_MONTH	0x5
54*4882a593Smuzhiyun #define RTC_YEAR	0x6
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define RTC_REG_CNT	7
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define RTC_CTRL	0x7
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #if defined(DEBUG)
rtc_dump(char const * const label)61*4882a593Smuzhiyun static void rtc_dump(char const *const label)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	uchar data[8];
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
66*4882a593Smuzhiyun 		printf("I2C read failed in rtc_dump()\n");
67*4882a593Smuzhiyun 		return;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 	printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
70*4882a593Smuzhiyun 	       label, data[0], data[1], data[2], data[3],
71*4882a593Smuzhiyun 	       data[4], data[5], data[6], data[7]);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun #else
74*4882a593Smuzhiyun #define rtc_dump(label)
75*4882a593Smuzhiyun #endif
76*4882a593Smuzhiyun 
rtc_validate(void)77*4882a593Smuzhiyun static uchar *rtc_validate(void)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	/*
80*4882a593Smuzhiyun 	 * This routine uses the OUT bit and the validity of the time values to
81*4882a593Smuzhiyun 	 * determine whether there has been an initial power-up since the last
82*4882a593Smuzhiyun 	 * time the routine was run.  It assumes that the OUT bit is not being
83*4882a593Smuzhiyun 	 * used for any other purpose.
84*4882a593Smuzhiyun 	 */
85*4882a593Smuzhiyun 	static const uchar daysInMonth[0x13] = {
86*4882a593Smuzhiyun 		0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
87*4882a593Smuzhiyun 		0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88*4882a593Smuzhiyun 		0x31, 0x30, 0x31
89*4882a593Smuzhiyun 	};
90*4882a593Smuzhiyun 	static uchar data[8];
91*4882a593Smuzhiyun 	uchar min, date, month, years;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	rtc_dump("begin validate");
94*4882a593Smuzhiyun 	if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
95*4882a593Smuzhiyun 		printf("I2C read failed in rtc_validate()\n");
96*4882a593Smuzhiyun 		return 0;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 	/*
99*4882a593Smuzhiyun 	 * If the OUT bit is "1", there has been a loss of power, so stop the
100*4882a593Smuzhiyun 	 * oscillator so it can be "kick-started" as per data sheet.
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	if (0x00 != (data[RTC_CTRL] & 0x80)) {
103*4882a593Smuzhiyun 		printf("M41T60 RTC clock lost power.\n");
104*4882a593Smuzhiyun 		data[RTC_SEC] = 0x80;
105*4882a593Smuzhiyun 		if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
106*4882a593Smuzhiyun 			printf("I2C write failed in rtc_validate()\n");
107*4882a593Smuzhiyun 			return 0;
108*4882a593Smuzhiyun 		}
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 	/*
111*4882a593Smuzhiyun 	 * If the oscillator is stopped or the date is invalid, then reset the
112*4882a593Smuzhiyun 	 * OUT bit to "0", reset the date registers, and start the oscillator.
113*4882a593Smuzhiyun 	 */
114*4882a593Smuzhiyun 	min = data[RTC_MIN] & 0x7F;
115*4882a593Smuzhiyun 	date = data[RTC_DATE];
116*4882a593Smuzhiyun 	month = data[RTC_MONTH] & 0x3F;
117*4882a593Smuzhiyun 	years = data[RTC_YEAR];
118*4882a593Smuzhiyun 	if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
119*4882a593Smuzhiyun 	    0x59 < min || 0x09 < (min & 0x0F) ||
120*4882a593Smuzhiyun 	    0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
121*4882a593Smuzhiyun 	    0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
122*4882a593Smuzhiyun 	    0x12 < month ||
123*4882a593Smuzhiyun 	    0x99 < years || 0x09 < (years & 0x0F) ||
124*4882a593Smuzhiyun 	    daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
125*4882a593Smuzhiyun 	    (0x29 == date && 0x02 == month &&
126*4882a593Smuzhiyun 	     ((0x00 != (years & 0x03)) ||
127*4882a593Smuzhiyun 	      (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
128*4882a593Smuzhiyun 		printf("Resetting M41T60 RTC clock.\n");
129*4882a593Smuzhiyun 		/*
130*4882a593Smuzhiyun 		 * Set to 00:00:00 1900-01-01 (Monday)
131*4882a593Smuzhiyun 		 */
132*4882a593Smuzhiyun 		data[RTC_SEC] = 0x00;
133*4882a593Smuzhiyun 		data[RTC_MIN] &= 0x80;	/* preserve OFIE bit */
134*4882a593Smuzhiyun 		data[RTC_HOUR] = 0x00;
135*4882a593Smuzhiyun 		data[RTC_DAY] = 0x02;
136*4882a593Smuzhiyun 		data[RTC_DATE] = 0x01;
137*4882a593Smuzhiyun 		data[RTC_MONTH] = 0xC1;
138*4882a593Smuzhiyun 		data[RTC_YEAR] = 0x00;
139*4882a593Smuzhiyun 		data[RTC_CTRL] &= 0x7F;	/* reset OUT bit */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
142*4882a593Smuzhiyun 			printf("I2C write failed in rtc_validate()\n");
143*4882a593Smuzhiyun 			return 0;
144*4882a593Smuzhiyun 		}
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 	return data;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
rtc_get(struct rtc_time * tmp)149*4882a593Smuzhiyun int rtc_get(struct rtc_time *tmp)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	uchar const *const data = rtc_validate();
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	if (!data)
154*4882a593Smuzhiyun 		return -1;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
157*4882a593Smuzhiyun 	tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
158*4882a593Smuzhiyun 	tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
159*4882a593Smuzhiyun 	tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
160*4882a593Smuzhiyun 	tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
161*4882a593Smuzhiyun 	tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
162*4882a593Smuzhiyun 	tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
163*4882a593Smuzhiyun 	tmp->tm_yday = 0;
164*4882a593Smuzhiyun 	tmp->tm_isdst = 0;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	debug("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
167*4882a593Smuzhiyun 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
168*4882a593Smuzhiyun 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
rtc_set(struct rtc_time * tmp)173*4882a593Smuzhiyun int rtc_set(struct rtc_time *tmp)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	uchar *const data = rtc_validate();
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	if (!data)
178*4882a593Smuzhiyun 		return -1;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
181*4882a593Smuzhiyun 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
182*4882a593Smuzhiyun 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
185*4882a593Smuzhiyun 	data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
186*4882a593Smuzhiyun 	data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
187*4882a593Smuzhiyun 	data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
188*4882a593Smuzhiyun 	data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
189*4882a593Smuzhiyun 	data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
190*4882a593Smuzhiyun 	data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
191*4882a593Smuzhiyun 	data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
192*4882a593Smuzhiyun 	if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
193*4882a593Smuzhiyun 		printf("I2C write failed in rtc_set()\n");
194*4882a593Smuzhiyun 		return -1;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
rtc_reset(void)200*4882a593Smuzhiyun void rtc_reset(void)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	uchar *const data = rtc_validate();
203*4882a593Smuzhiyun 	char const *const s = env_get("rtccal");
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if (!data)
206*4882a593Smuzhiyun 		return;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	rtc_dump("begin reset");
209*4882a593Smuzhiyun 	/*
210*4882a593Smuzhiyun 	 * If environmental variable "rtccal" is present, it must be a hex value
211*4882a593Smuzhiyun 	 * between 0x00 and 0x3F, inclusive.  The five least-significan bits
212*4882a593Smuzhiyun 	 * represent the calibration magnitude, and the sixth bit the sign bit.
213*4882a593Smuzhiyun 	 * If these do not match the contents of the hardware register, that
214*4882a593Smuzhiyun 	 * register is updated.  The value 0x00 imples no correction.  Consult
215*4882a593Smuzhiyun 	 * the M41T60 documentation for further details.
216*4882a593Smuzhiyun 	 */
217*4882a593Smuzhiyun 	if (s) {
218*4882a593Smuzhiyun 		unsigned long const l = simple_strtoul(s, 0, 16);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		if (l <= 0x3F) {
221*4882a593Smuzhiyun 			if ((data[RTC_CTRL] & 0x3F) != l) {
222*4882a593Smuzhiyun 				printf("Setting RTC calibration to 0x%02lX\n",
223*4882a593Smuzhiyun 				       l);
224*4882a593Smuzhiyun 				data[RTC_CTRL] &= 0xC0;
225*4882a593Smuzhiyun 				data[RTC_CTRL] |= (uchar) l;
226*4882a593Smuzhiyun 			}
227*4882a593Smuzhiyun 		} else
228*4882a593Smuzhiyun 			printf("environment parameter \"rtccal\" not valid: "
229*4882a593Smuzhiyun 			       "ignoring\n");
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 	/*
232*4882a593Smuzhiyun 	 * Turn off frequency test.
233*4882a593Smuzhiyun 	 */
234*4882a593Smuzhiyun 	data[RTC_CTRL] &= 0xBF;
235*4882a593Smuzhiyun 	if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
236*4882a593Smuzhiyun 		printf("I2C write failed in rtc_reset()\n");
237*4882a593Smuzhiyun 		return;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 	rtc_dump("end reset");
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun #endif /* CONFIG_RTC_M41T60 && CONFIG_SYS_I2C_RTC_ADDR && CONFIG_CMD_DATE */
242