xref: /rk3399_rockchip-uboot/drivers/rtc/m41t62.c (revision 326ea986ac150acdc7656d57fca647db80b50158)
188aff62dSStefan Roese /*
288aff62dSStefan Roese  * (C) Copyright 2008
388aff62dSStefan Roese  * Stefan Roese, DENX Software Engineering, sr@denx.de.
488aff62dSStefan Roese  *
588aff62dSStefan Roese  * based on a the Linux rtc-m41t80.c driver which is:
688aff62dSStefan Roese  *   Alexander Bigga <ab@mycable.de>, 2006 (c) mycable GmbH
788aff62dSStefan Roese  *
8*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
988aff62dSStefan Roese  */
1088aff62dSStefan Roese 
1188aff62dSStefan Roese /*
1288aff62dSStefan Roese  * Date & Time support for STMicroelectronics M41T62
1388aff62dSStefan Roese  */
1488aff62dSStefan Roese 
1588aff62dSStefan Roese /* #define	DEBUG	*/
1688aff62dSStefan Roese 
1788aff62dSStefan Roese #include <common.h>
1888aff62dSStefan Roese #include <command.h>
1988aff62dSStefan Roese #include <rtc.h>
2088aff62dSStefan Roese #include <i2c.h>
2188aff62dSStefan Roese 
2288aff62dSStefan Roese #if defined(CONFIG_CMD_DATE)
2388aff62dSStefan Roese 
2488aff62dSStefan Roese #define M41T62_REG_SSEC	0
2588aff62dSStefan Roese #define M41T62_REG_SEC	1
2688aff62dSStefan Roese #define M41T62_REG_MIN	2
2788aff62dSStefan Roese #define M41T62_REG_HOUR	3
2888aff62dSStefan Roese #define M41T62_REG_WDAY	4
2988aff62dSStefan Roese #define M41T62_REG_DAY	5
3088aff62dSStefan Roese #define M41T62_REG_MON	6
3188aff62dSStefan Roese #define M41T62_REG_YEAR	7
3288aff62dSStefan Roese #define M41T62_REG_ALARM_MON	0xa
3388aff62dSStefan Roese #define M41T62_REG_ALARM_DAY	0xb
3488aff62dSStefan Roese #define M41T62_REG_ALARM_HOUR	0xc
3588aff62dSStefan Roese #define M41T62_REG_ALARM_MIN	0xd
3688aff62dSStefan Roese #define M41T62_REG_ALARM_SEC	0xe
3788aff62dSStefan Roese #define M41T62_REG_FLAGS	0xf
3888aff62dSStefan Roese 
3988aff62dSStefan Roese #define M41T62_DATETIME_REG_SIZE	(M41T62_REG_YEAR + 1)
4088aff62dSStefan Roese #define M41T62_ALARM_REG_SIZE	\
4188aff62dSStefan Roese 	(M41T62_REG_ALARM_SEC + 1 - M41T62_REG_ALARM_MON)
4288aff62dSStefan Roese 
4388aff62dSStefan Roese #define M41T62_SEC_ST		(1 << 7)	/* ST: Stop Bit */
4488aff62dSStefan Roese #define M41T62_ALMON_AFE	(1 << 7)	/* AFE: AF Enable Bit */
4588aff62dSStefan Roese #define M41T62_ALMON_SQWE	(1 << 6)	/* SQWE: SQW Enable Bit */
4688aff62dSStefan Roese #define M41T62_ALHOUR_HT	(1 << 6)	/* HT: Halt Update Bit */
4788aff62dSStefan Roese #define M41T62_FLAGS_AF		(1 << 6)	/* AF: Alarm Flag Bit */
4888aff62dSStefan Roese #define M41T62_FLAGS_BATT_LOW	(1 << 4)	/* BL: Battery Low Bit */
4988aff62dSStefan Roese 
5088aff62dSStefan Roese #define M41T62_FEATURE_HT	(1 << 0)
5188aff62dSStefan Roese #define M41T62_FEATURE_BL	(1 << 1)
5288aff62dSStefan Roese 
53038f3c54SStefan Roese #define M41T80_ALHOUR_HT	(1 << 6)	/* HT: Halt Update Bit */
54038f3c54SStefan Roese 
rtc_get(struct rtc_time * tm)55b73a19e1SYuri Tikhonov int rtc_get(struct rtc_time *tm)
5688aff62dSStefan Roese {
5788aff62dSStefan Roese 	u8 buf[M41T62_DATETIME_REG_SIZE];
5888aff62dSStefan Roese 
596d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE);
6088aff62dSStefan Roese 
6188aff62dSStefan Roese 	debug("%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
6288aff62dSStefan Roese 	      "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
6388aff62dSStefan Roese 	      __FUNCTION__,
6488aff62dSStefan Roese 	      buf[0], buf[1], buf[2], buf[3],
6588aff62dSStefan Roese 	      buf[4], buf[5], buf[6], buf[7]);
6688aff62dSStefan Roese 
67e84aba13SAlbin Tonnerre 	tm->tm_sec = bcd2bin(buf[M41T62_REG_SEC] & 0x7f);
68e84aba13SAlbin Tonnerre 	tm->tm_min = bcd2bin(buf[M41T62_REG_MIN] & 0x7f);
69e84aba13SAlbin Tonnerre 	tm->tm_hour = bcd2bin(buf[M41T62_REG_HOUR] & 0x3f);
70e84aba13SAlbin Tonnerre 	tm->tm_mday = bcd2bin(buf[M41T62_REG_DAY] & 0x3f);
7188aff62dSStefan Roese 	tm->tm_wday = buf[M41T62_REG_WDAY] & 0x07;
72e84aba13SAlbin Tonnerre 	tm->tm_mon = bcd2bin(buf[M41T62_REG_MON] & 0x1f);
7388aff62dSStefan Roese 
7488aff62dSStefan Roese 	/* assume 20YY not 19YY, and ignore the Century Bit */
7588aff62dSStefan Roese 	/* U-Boot needs to add 1900 here */
76e84aba13SAlbin Tonnerre 	tm->tm_year = bcd2bin(buf[M41T62_REG_YEAR]) + 100 + 1900;
7788aff62dSStefan Roese 
7888aff62dSStefan Roese 	debug("%s: tm is secs=%d, mins=%d, hours=%d, "
7988aff62dSStefan Roese 	      "mday=%d, mon=%d, year=%d, wday=%d\n",
8088aff62dSStefan Roese 	      __FUNCTION__,
8188aff62dSStefan Roese 	      tm->tm_sec, tm->tm_min, tm->tm_hour,
8288aff62dSStefan Roese 	      tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
83b73a19e1SYuri Tikhonov 
84b73a19e1SYuri Tikhonov 	return 0;
8588aff62dSStefan Roese }
8688aff62dSStefan Roese 
rtc_set(struct rtc_time * tm)87d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set(struct rtc_time *tm)
8888aff62dSStefan Roese {
8988aff62dSStefan Roese 	u8 buf[M41T62_DATETIME_REG_SIZE];
9088aff62dSStefan Roese 
9188aff62dSStefan Roese 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
9288aff62dSStefan Roese 	      tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
9388aff62dSStefan Roese 	      tm->tm_hour, tm->tm_min, tm->tm_sec);
9488aff62dSStefan Roese 
956d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE);
9688aff62dSStefan Roese 
9788aff62dSStefan Roese 	/* Merge time-data and register flags into buf[0..7] */
9888aff62dSStefan Roese 	buf[M41T62_REG_SSEC] = 0;
9988aff62dSStefan Roese 	buf[M41T62_REG_SEC] =
100e84aba13SAlbin Tonnerre 		bin2bcd(tm->tm_sec) | (buf[M41T62_REG_SEC] & ~0x7f);
10188aff62dSStefan Roese 	buf[M41T62_REG_MIN] =
102e84aba13SAlbin Tonnerre 		bin2bcd(tm->tm_min) | (buf[M41T62_REG_MIN] & ~0x7f);
10388aff62dSStefan Roese 	buf[M41T62_REG_HOUR] =
104e84aba13SAlbin Tonnerre 		bin2bcd(tm->tm_hour) | (buf[M41T62_REG_HOUR] & ~0x3f) ;
10588aff62dSStefan Roese 	buf[M41T62_REG_WDAY] =
10688aff62dSStefan Roese 		(tm->tm_wday & 0x07) | (buf[M41T62_REG_WDAY] & ~0x07);
10788aff62dSStefan Roese 	buf[M41T62_REG_DAY] =
108e84aba13SAlbin Tonnerre 		bin2bcd(tm->tm_mday) | (buf[M41T62_REG_DAY] & ~0x3f);
10988aff62dSStefan Roese 	buf[M41T62_REG_MON] =
110e84aba13SAlbin Tonnerre 		bin2bcd(tm->tm_mon) | (buf[M41T62_REG_MON] & ~0x1f);
11188aff62dSStefan Roese 	/* assume 20YY not 19YY */
112e84aba13SAlbin Tonnerre 	buf[M41T62_REG_YEAR] = bin2bcd(tm->tm_year % 100);
11388aff62dSStefan Roese 
1146d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE)) {
11588aff62dSStefan Roese 		printf("I2C write failed in %s()\n", __func__);
116d1e23194SJean-Christophe PLAGNIOL-VILLARD 		return -1;
117d1e23194SJean-Christophe PLAGNIOL-VILLARD 	}
118d1e23194SJean-Christophe PLAGNIOL-VILLARD 
119d1e23194SJean-Christophe PLAGNIOL-VILLARD 	return 0;
12088aff62dSStefan Roese }
12188aff62dSStefan Roese 
rtc_reset(void)12288aff62dSStefan Roese void rtc_reset(void)
12388aff62dSStefan Roese {
124038f3c54SStefan Roese 	u8 val;
125038f3c54SStefan Roese 
12688aff62dSStefan Roese 	/*
127038f3c54SStefan Roese 	 * M41T82: Make sure HT (Halt Update) bit is cleared.
128038f3c54SStefan Roese 	 * This bit is 0 in M41T62 so its save to clear it always.
12988aff62dSStefan Roese 	 */
130038f3c54SStefan Roese 	i2c_read(CONFIG_SYS_I2C_RTC_ADDR, M41T62_REG_ALARM_HOUR, 1, &val, 1);
131038f3c54SStefan Roese 	val &= ~M41T80_ALHOUR_HT;
132038f3c54SStefan Roese 	i2c_write(CONFIG_SYS_I2C_RTC_ADDR, M41T62_REG_ALARM_HOUR, 1, &val, 1);
13388aff62dSStefan Roese }
13488aff62dSStefan Roese 
13588aff62dSStefan Roese #endif
136