xref: /rk3399_rockchip-uboot/drivers/rtc/m41t60.c (revision d1e2319414ea5218ba801163e4530ecf2dfcbf36)
112618278SLarry Johnson /*
212618278SLarry Johnson  * (C) Copyright 2007
312618278SLarry Johnson  * Larry Johnson, lrj@acm.org
412618278SLarry Johnson  *
512618278SLarry Johnson  * based on rtc/m41t11.c which is ...
612618278SLarry Johnson  *
712618278SLarry Johnson  * (C) Copyright 2002
812618278SLarry Johnson  * Andrew May, Viasat Inc, amay@viasat.com
912618278SLarry Johnson  *
1012618278SLarry Johnson  * This program is free software; you can redistribute it and/or
1112618278SLarry Johnson  * modify it under the terms of the GNU General Public License as
1212618278SLarry Johnson  * published by the Free Software Foundation; either version 2 of
1312618278SLarry Johnson  * the License, or (at your option) any later version.
1412618278SLarry Johnson  *
1512618278SLarry Johnson  * This program is distributed in the hope that it will be useful,
1612618278SLarry Johnson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1712618278SLarry Johnson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1812618278SLarry Johnson  * GNU General Public License for more details.
1912618278SLarry Johnson  *
2012618278SLarry Johnson  * You should have received a copy of the GNU General Public License
2112618278SLarry Johnson  * along with this program; if not, write to the Free Software
2212618278SLarry Johnson  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2312618278SLarry Johnson  * MA 02111-1307 USA
2412618278SLarry Johnson  */
2512618278SLarry Johnson 
2612618278SLarry Johnson /*
2712618278SLarry Johnson  * STMicroelectronics M41T60 serial access real-time clock
2812618278SLarry Johnson  */
2912618278SLarry Johnson 
3012618278SLarry Johnson /* #define DEBUG 1 */
3112618278SLarry Johnson 
3212618278SLarry Johnson #include <common.h>
3312618278SLarry Johnson #include <command.h>
3412618278SLarry Johnson #include <rtc.h>
3512618278SLarry Johnson #include <i2c.h>
3612618278SLarry Johnson 
37871c18ddSMichal Simek #if defined(CFG_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
3812618278SLarry Johnson 
3912618278SLarry Johnson static unsigned bcd2bin(uchar n)
4012618278SLarry Johnson {
4112618278SLarry Johnson 	return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
4212618278SLarry Johnson }
4312618278SLarry Johnson 
4412618278SLarry Johnson static unsigned char bin2bcd(unsigned int n)
4512618278SLarry Johnson {
4612618278SLarry Johnson 	return (((n / 10) << 4) | (n % 10));
4712618278SLarry Johnson }
4812618278SLarry Johnson 
4912618278SLarry Johnson /*
5012618278SLarry Johnson  * Convert between century and "century bits" (CB1 and CB0).  These routines
5112618278SLarry Johnson  * assume years are in the range 1900 - 2299.
5212618278SLarry Johnson  */
5312618278SLarry Johnson 
5412618278SLarry Johnson static unsigned char year2cb(unsigned const year)
5512618278SLarry Johnson {
5612618278SLarry Johnson 	if (year < 1900 || year >= 2300)
5712618278SLarry Johnson 		printf("M41T60 RTC: year %d out of range\n", year);
5812618278SLarry Johnson 
5912618278SLarry Johnson 	return (year / 100) & 0x3;
6012618278SLarry Johnson }
6112618278SLarry Johnson 
6212618278SLarry Johnson static unsigned cb2year(unsigned const cb)
6312618278SLarry Johnson {
6412618278SLarry Johnson 	return 1900 + 100 * ((cb + 1) & 0x3);
6512618278SLarry Johnson }
6612618278SLarry Johnson 
6712618278SLarry Johnson /*
6812618278SLarry Johnson  * These are simple defines for the chip local to here so they aren't too
6912618278SLarry Johnson  * verbose.  DAY/DATE aren't nice but that is how they are on the data sheet.
7012618278SLarry Johnson  */
7112618278SLarry Johnson #define RTC_SEC		0x0
7212618278SLarry Johnson #define RTC_MIN		0x1
7312618278SLarry Johnson #define RTC_HOUR	0x2
7412618278SLarry Johnson #define RTC_DAY		0x3
7512618278SLarry Johnson #define RTC_DATE	0x4
7612618278SLarry Johnson #define RTC_MONTH	0x5
7712618278SLarry Johnson #define RTC_YEAR	0x6
7812618278SLarry Johnson 
7912618278SLarry Johnson #define RTC_REG_CNT	7
8012618278SLarry Johnson 
8112618278SLarry Johnson #define RTC_CTRL	0x7
8212618278SLarry Johnson 
8312618278SLarry Johnson #if defined(DEBUG)
8412618278SLarry Johnson static void rtc_dump(char const *const label)
8512618278SLarry Johnson {
8612618278SLarry Johnson 	uchar data[8];
8712618278SLarry Johnson 
8812618278SLarry Johnson 	if (i2c_read(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
8912618278SLarry Johnson 		printf("I2C read failed in rtc_dump()\n");
9012618278SLarry Johnson 		return;
9112618278SLarry Johnson 	}
9212618278SLarry Johnson 	printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
9312618278SLarry Johnson 	       label, data[0], data[1], data[2], data[3],
9412618278SLarry Johnson 	       data[4], data[5], data[6], data[7]);
9512618278SLarry Johnson }
9612618278SLarry Johnson #else
9712618278SLarry Johnson #define rtc_dump(label)
9812618278SLarry Johnson #endif
9912618278SLarry Johnson 
10012618278SLarry Johnson static uchar *rtc_validate(void)
10112618278SLarry Johnson {
10212618278SLarry Johnson 	/*
10312618278SLarry Johnson 	 * This routine uses the OUT bit and the validity of the time values to
10412618278SLarry Johnson 	 * determine whether there has been an initial power-up since the last
10512618278SLarry Johnson 	 * time the routine was run.  It assumes that the OUT bit is not being
10612618278SLarry Johnson 	 * used for any other purpose.
10712618278SLarry Johnson 	 */
10812618278SLarry Johnson 	static const uchar daysInMonth[0x13] = {
10912618278SLarry Johnson 		0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
11012618278SLarry Johnson 		0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11112618278SLarry Johnson 		0x31, 0x30, 0x31
11212618278SLarry Johnson 	};
11312618278SLarry Johnson 	static uchar data[8];
11412618278SLarry Johnson 	uchar min, date, month, years;
11512618278SLarry Johnson 
11612618278SLarry Johnson 	rtc_dump("begin validate");
11712618278SLarry Johnson 	if (i2c_read(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
11812618278SLarry Johnson 		printf("I2C read failed in rtc_validate()\n");
11912618278SLarry Johnson 		return 0;
12012618278SLarry Johnson 	}
12112618278SLarry Johnson 	/*
12212618278SLarry Johnson 	 * If the OUT bit is "1", there has been a loss of power, so stop the
12312618278SLarry Johnson 	 * oscillator so it can be "kick-started" as per data sheet.
12412618278SLarry Johnson 	 */
12512618278SLarry Johnson 	if (0x00 != (data[RTC_CTRL] & 0x80)) {
12612618278SLarry Johnson 		printf("M41T60 RTC clock lost power.\n");
12712618278SLarry Johnson 		data[RTC_SEC] = 0x80;
12812618278SLarry Johnson 		if (i2c_write(CFG_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
12912618278SLarry Johnson 			printf("I2C write failed in rtc_validate()\n");
13012618278SLarry Johnson 			return 0;
13112618278SLarry Johnson 		}
13212618278SLarry Johnson 	}
13312618278SLarry Johnson 	/*
13412618278SLarry Johnson 	 * If the oscillator is stopped or the date is invalid, then reset the
13512618278SLarry Johnson 	 * OUT bit to "0", reset the date registers, and start the oscillator.
13612618278SLarry Johnson 	 */
13712618278SLarry Johnson 	min = data[RTC_MIN] & 0x7F;
13812618278SLarry Johnson 	date = data[RTC_DATE];
13912618278SLarry Johnson 	month = data[RTC_MONTH] & 0x3F;
14012618278SLarry Johnson 	years = data[RTC_YEAR];
14112618278SLarry Johnson 	if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
14212618278SLarry Johnson 	    0x59 < min || 0x09 < (min & 0x0F) ||
14312618278SLarry Johnson 	    0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
14412618278SLarry Johnson 	    0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
14512618278SLarry Johnson 	    0x12 < month ||
14612618278SLarry Johnson 	    0x99 < years || 0x09 < (years & 0x0F) ||
14712618278SLarry Johnson 	    daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
14812618278SLarry Johnson 	    (0x29 == date && 0x02 == month &&
14912618278SLarry Johnson 	     ((0x00 != (years & 0x03)) ||
15012618278SLarry Johnson 	      (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
15112618278SLarry Johnson 		printf("Resetting M41T60 RTC clock.\n");
15212618278SLarry Johnson 		/*
15312618278SLarry Johnson 		 * Set to 00:00:00 1900-01-01 (Monday)
15412618278SLarry Johnson 		 */
15512618278SLarry Johnson 		data[RTC_SEC] = 0x00;
15612618278SLarry Johnson 		data[RTC_MIN] &= 0x80;	/* preserve OFIE bit */
15712618278SLarry Johnson 		data[RTC_HOUR] = 0x00;
15812618278SLarry Johnson 		data[RTC_DAY] = 0x02;
15912618278SLarry Johnson 		data[RTC_DATE] = 0x01;
16012618278SLarry Johnson 		data[RTC_MONTH] = 0xC1;
16112618278SLarry Johnson 		data[RTC_YEAR] = 0x00;
16212618278SLarry Johnson 		data[RTC_CTRL] &= 0x7F;	/* reset OUT bit */
16312618278SLarry Johnson 
16412618278SLarry Johnson 		if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
16512618278SLarry Johnson 			printf("I2C write failed in rtc_validate()\n");
16612618278SLarry Johnson 			return 0;
16712618278SLarry Johnson 		}
16812618278SLarry Johnson 	}
16912618278SLarry Johnson 	return data;
17012618278SLarry Johnson }
17112618278SLarry Johnson 
172b73a19e1SYuri Tikhonov int rtc_get(struct rtc_time *tmp)
17312618278SLarry Johnson {
17412618278SLarry Johnson 	uchar const *const data = rtc_validate();
17512618278SLarry Johnson 
17612618278SLarry Johnson 	if (!data)
177b73a19e1SYuri Tikhonov 		return -1;
17812618278SLarry Johnson 
17912618278SLarry Johnson 	tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
18012618278SLarry Johnson 	tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
18112618278SLarry Johnson 	tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
18212618278SLarry Johnson 	tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
18312618278SLarry Johnson 	tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
18412618278SLarry Johnson 	tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
18512618278SLarry Johnson 	tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
18612618278SLarry Johnson 	tmp->tm_yday = 0;
18712618278SLarry Johnson 	tmp->tm_isdst = 0;
18812618278SLarry Johnson 
18912618278SLarry Johnson 	debug("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
19012618278SLarry Johnson 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
19112618278SLarry Johnson 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
192b73a19e1SYuri Tikhonov 
193b73a19e1SYuri Tikhonov 	return 0;
19412618278SLarry Johnson }
19512618278SLarry Johnson 
196*d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set(struct rtc_time *tmp)
19712618278SLarry Johnson {
19812618278SLarry Johnson 	uchar *const data = rtc_validate();
19912618278SLarry Johnson 
20012618278SLarry Johnson 	if (!data)
201*d1e23194SJean-Christophe PLAGNIOL-VILLARD 		return -1;
20212618278SLarry Johnson 
20312618278SLarry Johnson 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
20412618278SLarry Johnson 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
20512618278SLarry Johnson 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
20612618278SLarry Johnson 
20712618278SLarry Johnson 	data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
20812618278SLarry Johnson 	data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
20912618278SLarry Johnson 	data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
21012618278SLarry Johnson 	data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
21112618278SLarry Johnson 	data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
21212618278SLarry Johnson 	data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
21312618278SLarry Johnson 	data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
21412618278SLarry Johnson 	data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
21512618278SLarry Johnson 	if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
21612618278SLarry Johnson 		printf("I2C write failed in rtc_set()\n");
217*d1e23194SJean-Christophe PLAGNIOL-VILLARD 		return -1;
21812618278SLarry Johnson 	}
219*d1e23194SJean-Christophe PLAGNIOL-VILLARD 
220*d1e23194SJean-Christophe PLAGNIOL-VILLARD 	return 0;
22112618278SLarry Johnson }
22212618278SLarry Johnson 
22312618278SLarry Johnson void rtc_reset(void)
22412618278SLarry Johnson {
22512618278SLarry Johnson 	uchar *const data = rtc_validate();
22612618278SLarry Johnson 	char const *const s = getenv("rtccal");
22712618278SLarry Johnson 
22812618278SLarry Johnson 	if (!data)
22912618278SLarry Johnson 		return;
23012618278SLarry Johnson 
23112618278SLarry Johnson 	rtc_dump("begin reset");
23212618278SLarry Johnson 	/*
23312618278SLarry Johnson 	 * If environmental variable "rtccal" is present, it must be a hex value
23412618278SLarry Johnson 	 * between 0x00 and 0x3F, inclusive.  The five least-significan bits
23512618278SLarry Johnson 	 * represent the calibration magnitude, and the sixth bit the sign bit.
23612618278SLarry Johnson 	 * If these do not match the contents of the hardware register, that
23712618278SLarry Johnson 	 * register is updated.  The value 0x00 imples no correction.  Consult
23812618278SLarry Johnson 	 * the M41T60 documentation for further details.
23912618278SLarry Johnson 	 */
24012618278SLarry Johnson 	if (s) {
24112618278SLarry Johnson 		unsigned long const l = simple_strtoul(s, 0, 16);
24212618278SLarry Johnson 
24312618278SLarry Johnson 		if (l <= 0x3F) {
24412618278SLarry Johnson 			if ((data[RTC_CTRL] & 0x3F) != l) {
24510943c9aSStefan Roese 				printf("Setting RTC calibration to 0x%02lX\n",
24612618278SLarry Johnson 				       l);
24712618278SLarry Johnson 				data[RTC_CTRL] &= 0xC0;
24812618278SLarry Johnson 				data[RTC_CTRL] |= (uchar) l;
24912618278SLarry Johnson 			}
25012618278SLarry Johnson 		} else
25112618278SLarry Johnson 			printf("environment parameter \"rtccal\" not valid: "
25212618278SLarry Johnson 			       "ignoring\n");
25312618278SLarry Johnson 	}
25412618278SLarry Johnson 	/*
25512618278SLarry Johnson 	 * Turn off frequency test.
25612618278SLarry Johnson 	 */
25712618278SLarry Johnson 	data[RTC_CTRL] &= 0xBF;
25812618278SLarry Johnson 	if (i2c_write(CFG_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
25912618278SLarry Johnson 		printf("I2C write failed in rtc_reset()\n");
26012618278SLarry Johnson 		return;
26112618278SLarry Johnson 	}
26212618278SLarry Johnson 	rtc_dump("end reset");
26312618278SLarry Johnson }
26412618278SLarry Johnson #endif /* CONFIG_RTC_M41T60 && CFG_I2C_RTC_ADDR && CONFIG_CMD_DATE */
265