xref: /rk3399_rockchip-uboot/drivers/rtc/m41t60.c (revision 00caae6d47645e68d6e5277aceb69592b49381a6)
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  *
101a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
1112618278SLarry Johnson  */
1212618278SLarry Johnson 
1312618278SLarry Johnson /*
1412618278SLarry Johnson  * STMicroelectronics M41T60 serial access real-time clock
1512618278SLarry Johnson  */
1612618278SLarry Johnson 
1712618278SLarry Johnson /* #define DEBUG 1 */
1812618278SLarry Johnson 
1912618278SLarry Johnson #include <common.h>
2012618278SLarry Johnson #include <command.h>
2112618278SLarry Johnson #include <rtc.h>
2212618278SLarry Johnson #include <i2c.h>
2312618278SLarry Johnson 
246d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
2512618278SLarry Johnson 
2612618278SLarry Johnson /*
2712618278SLarry Johnson  * Convert between century and "century bits" (CB1 and CB0).  These routines
2812618278SLarry Johnson  * assume years are in the range 1900 - 2299.
2912618278SLarry Johnson  */
3012618278SLarry Johnson 
year2cb(unsigned const year)3112618278SLarry Johnson static unsigned char year2cb(unsigned const year)
3212618278SLarry Johnson {
3312618278SLarry Johnson 	if (year < 1900 || year >= 2300)
3412618278SLarry Johnson 		printf("M41T60 RTC: year %d out of range\n", year);
3512618278SLarry Johnson 
3612618278SLarry Johnson 	return (year / 100) & 0x3;
3712618278SLarry Johnson }
3812618278SLarry Johnson 
cb2year(unsigned const cb)3912618278SLarry Johnson static unsigned cb2year(unsigned const cb)
4012618278SLarry Johnson {
4112618278SLarry Johnson 	return 1900 + 100 * ((cb + 1) & 0x3);
4212618278SLarry Johnson }
4312618278SLarry Johnson 
4412618278SLarry Johnson /*
4512618278SLarry Johnson  * These are simple defines for the chip local to here so they aren't too
4612618278SLarry Johnson  * verbose.  DAY/DATE aren't nice but that is how they are on the data sheet.
4712618278SLarry Johnson  */
4812618278SLarry Johnson #define RTC_SEC		0x0
4912618278SLarry Johnson #define RTC_MIN		0x1
5012618278SLarry Johnson #define RTC_HOUR	0x2
5112618278SLarry Johnson #define RTC_DAY		0x3
5212618278SLarry Johnson #define RTC_DATE	0x4
5312618278SLarry Johnson #define RTC_MONTH	0x5
5412618278SLarry Johnson #define RTC_YEAR	0x6
5512618278SLarry Johnson 
5612618278SLarry Johnson #define RTC_REG_CNT	7
5712618278SLarry Johnson 
5812618278SLarry Johnson #define RTC_CTRL	0x7
5912618278SLarry Johnson 
6012618278SLarry Johnson #if defined(DEBUG)
rtc_dump(char const * const label)6112618278SLarry Johnson static void rtc_dump(char const *const label)
6212618278SLarry Johnson {
6312618278SLarry Johnson 	uchar data[8];
6412618278SLarry Johnson 
656d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
6612618278SLarry Johnson 		printf("I2C read failed in rtc_dump()\n");
6712618278SLarry Johnson 		return;
6812618278SLarry Johnson 	}
6912618278SLarry Johnson 	printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
7012618278SLarry Johnson 	       label, data[0], data[1], data[2], data[3],
7112618278SLarry Johnson 	       data[4], data[5], data[6], data[7]);
7212618278SLarry Johnson }
7312618278SLarry Johnson #else
7412618278SLarry Johnson #define rtc_dump(label)
7512618278SLarry Johnson #endif
7612618278SLarry Johnson 
rtc_validate(void)7712618278SLarry Johnson static uchar *rtc_validate(void)
7812618278SLarry Johnson {
7912618278SLarry Johnson 	/*
8012618278SLarry Johnson 	 * This routine uses the OUT bit and the validity of the time values to
8112618278SLarry Johnson 	 * determine whether there has been an initial power-up since the last
8212618278SLarry Johnson 	 * time the routine was run.  It assumes that the OUT bit is not being
8312618278SLarry Johnson 	 * used for any other purpose.
8412618278SLarry Johnson 	 */
8512618278SLarry Johnson 	static const uchar daysInMonth[0x13] = {
8612618278SLarry Johnson 		0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
8712618278SLarry Johnson 		0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8812618278SLarry Johnson 		0x31, 0x30, 0x31
8912618278SLarry Johnson 	};
9012618278SLarry Johnson 	static uchar data[8];
9112618278SLarry Johnson 	uchar min, date, month, years;
9212618278SLarry Johnson 
9312618278SLarry Johnson 	rtc_dump("begin validate");
946d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
9512618278SLarry Johnson 		printf("I2C read failed in rtc_validate()\n");
9612618278SLarry Johnson 		return 0;
9712618278SLarry Johnson 	}
9812618278SLarry Johnson 	/*
9912618278SLarry Johnson 	 * If the OUT bit is "1", there has been a loss of power, so stop the
10012618278SLarry Johnson 	 * oscillator so it can be "kick-started" as per data sheet.
10112618278SLarry Johnson 	 */
10212618278SLarry Johnson 	if (0x00 != (data[RTC_CTRL] & 0x80)) {
10312618278SLarry Johnson 		printf("M41T60 RTC clock lost power.\n");
10412618278SLarry Johnson 		data[RTC_SEC] = 0x80;
1056d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
10612618278SLarry Johnson 			printf("I2C write failed in rtc_validate()\n");
10712618278SLarry Johnson 			return 0;
10812618278SLarry Johnson 		}
10912618278SLarry Johnson 	}
11012618278SLarry Johnson 	/*
11112618278SLarry Johnson 	 * If the oscillator is stopped or the date is invalid, then reset the
11212618278SLarry Johnson 	 * OUT bit to "0", reset the date registers, and start the oscillator.
11312618278SLarry Johnson 	 */
11412618278SLarry Johnson 	min = data[RTC_MIN] & 0x7F;
11512618278SLarry Johnson 	date = data[RTC_DATE];
11612618278SLarry Johnson 	month = data[RTC_MONTH] & 0x3F;
11712618278SLarry Johnson 	years = data[RTC_YEAR];
11812618278SLarry Johnson 	if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
11912618278SLarry Johnson 	    0x59 < min || 0x09 < (min & 0x0F) ||
12012618278SLarry Johnson 	    0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
12112618278SLarry Johnson 	    0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
12212618278SLarry Johnson 	    0x12 < month ||
12312618278SLarry Johnson 	    0x99 < years || 0x09 < (years & 0x0F) ||
12412618278SLarry Johnson 	    daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
12512618278SLarry Johnson 	    (0x29 == date && 0x02 == month &&
12612618278SLarry Johnson 	     ((0x00 != (years & 0x03)) ||
12712618278SLarry Johnson 	      (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
12812618278SLarry Johnson 		printf("Resetting M41T60 RTC clock.\n");
12912618278SLarry Johnson 		/*
13012618278SLarry Johnson 		 * Set to 00:00:00 1900-01-01 (Monday)
13112618278SLarry Johnson 		 */
13212618278SLarry Johnson 		data[RTC_SEC] = 0x00;
13312618278SLarry Johnson 		data[RTC_MIN] &= 0x80;	/* preserve OFIE bit */
13412618278SLarry Johnson 		data[RTC_HOUR] = 0x00;
13512618278SLarry Johnson 		data[RTC_DAY] = 0x02;
13612618278SLarry Johnson 		data[RTC_DATE] = 0x01;
13712618278SLarry Johnson 		data[RTC_MONTH] = 0xC1;
13812618278SLarry Johnson 		data[RTC_YEAR] = 0x00;
13912618278SLarry Johnson 		data[RTC_CTRL] &= 0x7F;	/* reset OUT bit */
14012618278SLarry Johnson 
1416d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
14212618278SLarry Johnson 			printf("I2C write failed in rtc_validate()\n");
14312618278SLarry Johnson 			return 0;
14412618278SLarry Johnson 		}
14512618278SLarry Johnson 	}
14612618278SLarry Johnson 	return data;
14712618278SLarry Johnson }
14812618278SLarry Johnson 
rtc_get(struct rtc_time * tmp)149b73a19e1SYuri Tikhonov int rtc_get(struct rtc_time *tmp)
15012618278SLarry Johnson {
15112618278SLarry Johnson 	uchar const *const data = rtc_validate();
15212618278SLarry Johnson 
15312618278SLarry Johnson 	if (!data)
154b73a19e1SYuri Tikhonov 		return -1;
15512618278SLarry Johnson 
15612618278SLarry Johnson 	tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
15712618278SLarry Johnson 	tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
15812618278SLarry Johnson 	tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
15912618278SLarry Johnson 	tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
16012618278SLarry Johnson 	tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
16112618278SLarry Johnson 	tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
16212618278SLarry Johnson 	tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
16312618278SLarry Johnson 	tmp->tm_yday = 0;
16412618278SLarry Johnson 	tmp->tm_isdst = 0;
16512618278SLarry Johnson 
16612618278SLarry Johnson 	debug("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
16712618278SLarry Johnson 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
16812618278SLarry Johnson 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
169b73a19e1SYuri Tikhonov 
170b73a19e1SYuri Tikhonov 	return 0;
17112618278SLarry Johnson }
17212618278SLarry Johnson 
rtc_set(struct rtc_time * tmp)173d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set(struct rtc_time *tmp)
17412618278SLarry Johnson {
17512618278SLarry Johnson 	uchar *const data = rtc_validate();
17612618278SLarry Johnson 
17712618278SLarry Johnson 	if (!data)
178d1e23194SJean-Christophe PLAGNIOL-VILLARD 		return -1;
17912618278SLarry Johnson 
18012618278SLarry Johnson 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
18112618278SLarry Johnson 	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
18212618278SLarry Johnson 	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18312618278SLarry Johnson 
18412618278SLarry Johnson 	data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
18512618278SLarry Johnson 	data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
18612618278SLarry Johnson 	data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
18712618278SLarry Johnson 	data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
18812618278SLarry Johnson 	data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
18912618278SLarry Johnson 	data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
19012618278SLarry Johnson 	data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
19112618278SLarry Johnson 	data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
1926d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
19312618278SLarry Johnson 		printf("I2C write failed in rtc_set()\n");
194d1e23194SJean-Christophe PLAGNIOL-VILLARD 		return -1;
19512618278SLarry Johnson 	}
196d1e23194SJean-Christophe PLAGNIOL-VILLARD 
197d1e23194SJean-Christophe PLAGNIOL-VILLARD 	return 0;
19812618278SLarry Johnson }
19912618278SLarry Johnson 
rtc_reset(void)20012618278SLarry Johnson void rtc_reset(void)
20112618278SLarry Johnson {
20212618278SLarry Johnson 	uchar *const data = rtc_validate();
203*00caae6dSSimon Glass 	char const *const s = env_get("rtccal");
20412618278SLarry Johnson 
20512618278SLarry Johnson 	if (!data)
20612618278SLarry Johnson 		return;
20712618278SLarry Johnson 
20812618278SLarry Johnson 	rtc_dump("begin reset");
20912618278SLarry Johnson 	/*
21012618278SLarry Johnson 	 * If environmental variable "rtccal" is present, it must be a hex value
21112618278SLarry Johnson 	 * between 0x00 and 0x3F, inclusive.  The five least-significan bits
21212618278SLarry Johnson 	 * represent the calibration magnitude, and the sixth bit the sign bit.
21312618278SLarry Johnson 	 * If these do not match the contents of the hardware register, that
21412618278SLarry Johnson 	 * register is updated.  The value 0x00 imples no correction.  Consult
21512618278SLarry Johnson 	 * the M41T60 documentation for further details.
21612618278SLarry Johnson 	 */
21712618278SLarry Johnson 	if (s) {
21812618278SLarry Johnson 		unsigned long const l = simple_strtoul(s, 0, 16);
21912618278SLarry Johnson 
22012618278SLarry Johnson 		if (l <= 0x3F) {
22112618278SLarry Johnson 			if ((data[RTC_CTRL] & 0x3F) != l) {
22210943c9aSStefan Roese 				printf("Setting RTC calibration to 0x%02lX\n",
22312618278SLarry Johnson 				       l);
22412618278SLarry Johnson 				data[RTC_CTRL] &= 0xC0;
22512618278SLarry Johnson 				data[RTC_CTRL] |= (uchar) l;
22612618278SLarry Johnson 			}
22712618278SLarry Johnson 		} else
22812618278SLarry Johnson 			printf("environment parameter \"rtccal\" not valid: "
22912618278SLarry Johnson 			       "ignoring\n");
23012618278SLarry Johnson 	}
23112618278SLarry Johnson 	/*
23212618278SLarry Johnson 	 * Turn off frequency test.
23312618278SLarry Johnson 	 */
23412618278SLarry Johnson 	data[RTC_CTRL] &= 0xBF;
2356d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
23612618278SLarry Johnson 		printf("I2C write failed in rtc_reset()\n");
23712618278SLarry Johnson 		return;
23812618278SLarry Johnson 	}
23912618278SLarry Johnson 	rtc_dump("end reset");
24012618278SLarry Johnson }
2416d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #endif /* CONFIG_RTC_M41T60 && CONFIG_SYS_I2C_RTC_ADDR && CONFIG_CMD_DATE */
242