xref: /rk3399_rockchip-uboot/drivers/rtc/m41t62.c (revision b73a19e1609d0f705cbab8014ca17aefe89e4c76)
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  *
888aff62dSStefan Roese  * This program is free software; you can redistribute it and/or
988aff62dSStefan Roese  * modify it under the terms of the GNU General Public License as
1088aff62dSStefan Roese  * published by the Free Software Foundation; either version 2 of
1188aff62dSStefan Roese  * the License, or (at your option) any later version.
1288aff62dSStefan Roese  *
1388aff62dSStefan Roese  * This program is distributed in the hope that it will be useful,
1488aff62dSStefan Roese  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1588aff62dSStefan Roese  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1688aff62dSStefan Roese  * GNU General Public License for more details.
1788aff62dSStefan Roese  *
1888aff62dSStefan Roese  * You should have received a copy of the GNU General Public License
1988aff62dSStefan Roese  * along with this program; if not, write to the Free Software
2088aff62dSStefan Roese  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2188aff62dSStefan Roese  * MA 02111-1307 USA
2288aff62dSStefan Roese  */
2388aff62dSStefan Roese 
2488aff62dSStefan Roese /*
2588aff62dSStefan Roese  * Date & Time support for STMicroelectronics M41T62
2688aff62dSStefan Roese  */
2788aff62dSStefan Roese 
2888aff62dSStefan Roese /* #define	DEBUG	*/
2988aff62dSStefan Roese 
3088aff62dSStefan Roese #include <common.h>
3188aff62dSStefan Roese #include <command.h>
3288aff62dSStefan Roese #include <rtc.h>
3388aff62dSStefan Roese #include <i2c.h>
3488aff62dSStefan Roese #include <bcd.h>
3588aff62dSStefan Roese 
3688aff62dSStefan Roese #if defined(CONFIG_CMD_DATE)
3788aff62dSStefan Roese 
3888aff62dSStefan Roese #define M41T62_REG_SSEC	0
3988aff62dSStefan Roese #define M41T62_REG_SEC	1
4088aff62dSStefan Roese #define M41T62_REG_MIN	2
4188aff62dSStefan Roese #define M41T62_REG_HOUR	3
4288aff62dSStefan Roese #define M41T62_REG_WDAY	4
4388aff62dSStefan Roese #define M41T62_REG_DAY	5
4488aff62dSStefan Roese #define M41T62_REG_MON	6
4588aff62dSStefan Roese #define M41T62_REG_YEAR	7
4688aff62dSStefan Roese #define M41T62_REG_ALARM_MON	0xa
4788aff62dSStefan Roese #define M41T62_REG_ALARM_DAY	0xb
4888aff62dSStefan Roese #define M41T62_REG_ALARM_HOUR	0xc
4988aff62dSStefan Roese #define M41T62_REG_ALARM_MIN	0xd
5088aff62dSStefan Roese #define M41T62_REG_ALARM_SEC	0xe
5188aff62dSStefan Roese #define M41T62_REG_FLAGS	0xf
5288aff62dSStefan Roese 
5388aff62dSStefan Roese #define M41T62_DATETIME_REG_SIZE	(M41T62_REG_YEAR + 1)
5488aff62dSStefan Roese #define M41T62_ALARM_REG_SIZE	\
5588aff62dSStefan Roese 	(M41T62_REG_ALARM_SEC + 1 - M41T62_REG_ALARM_MON)
5688aff62dSStefan Roese 
5788aff62dSStefan Roese #define M41T62_SEC_ST		(1 << 7)	/* ST: Stop Bit */
5888aff62dSStefan Roese #define M41T62_ALMON_AFE	(1 << 7)	/* AFE: AF Enable Bit */
5988aff62dSStefan Roese #define M41T62_ALMON_SQWE	(1 << 6)	/* SQWE: SQW Enable Bit */
6088aff62dSStefan Roese #define M41T62_ALHOUR_HT	(1 << 6)	/* HT: Halt Update Bit */
6188aff62dSStefan Roese #define M41T62_FLAGS_AF		(1 << 6)	/* AF: Alarm Flag Bit */
6288aff62dSStefan Roese #define M41T62_FLAGS_BATT_LOW	(1 << 4)	/* BL: Battery Low Bit */
6388aff62dSStefan Roese 
6488aff62dSStefan Roese #define M41T62_FEATURE_HT	(1 << 0)
6588aff62dSStefan Roese #define M41T62_FEATURE_BL	(1 << 1)
6688aff62dSStefan Roese 
67*b73a19e1SYuri Tikhonov int rtc_get(struct rtc_time *tm)
6888aff62dSStefan Roese {
6988aff62dSStefan Roese 	u8 buf[M41T62_DATETIME_REG_SIZE];
7088aff62dSStefan Roese 
7188aff62dSStefan Roese 	i2c_read(CFG_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE);
7288aff62dSStefan Roese 
7388aff62dSStefan Roese 	debug("%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
7488aff62dSStefan Roese 	      "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
7588aff62dSStefan Roese 	      __FUNCTION__,
7688aff62dSStefan Roese 	      buf[0], buf[1], buf[2], buf[3],
7788aff62dSStefan Roese 	      buf[4], buf[5], buf[6], buf[7]);
7888aff62dSStefan Roese 
7988aff62dSStefan Roese 	tm->tm_sec = BCD2BIN(buf[M41T62_REG_SEC] & 0x7f);
8088aff62dSStefan Roese 	tm->tm_min = BCD2BIN(buf[M41T62_REG_MIN] & 0x7f);
8188aff62dSStefan Roese 	tm->tm_hour = BCD2BIN(buf[M41T62_REG_HOUR] & 0x3f);
8288aff62dSStefan Roese 	tm->tm_mday = BCD2BIN(buf[M41T62_REG_DAY] & 0x3f);
8388aff62dSStefan Roese 	tm->tm_wday = buf[M41T62_REG_WDAY] & 0x07;
8488aff62dSStefan Roese 	tm->tm_mon = BCD2BIN(buf[M41T62_REG_MON] & 0x1f) - 1;
8588aff62dSStefan Roese 
8688aff62dSStefan Roese 	/* assume 20YY not 19YY, and ignore the Century Bit */
8788aff62dSStefan Roese 	/* U-Boot needs to add 1900 here */
8888aff62dSStefan Roese 	tm->tm_year = BCD2BIN(buf[M41T62_REG_YEAR]) + 100 + 1900;
8988aff62dSStefan Roese 
9088aff62dSStefan Roese 	debug("%s: tm is secs=%d, mins=%d, hours=%d, "
9188aff62dSStefan Roese 	      "mday=%d, mon=%d, year=%d, wday=%d\n",
9288aff62dSStefan Roese 	      __FUNCTION__,
9388aff62dSStefan Roese 	      tm->tm_sec, tm->tm_min, tm->tm_hour,
9488aff62dSStefan Roese 	      tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
95*b73a19e1SYuri Tikhonov 
96*b73a19e1SYuri Tikhonov 	return 0;
9788aff62dSStefan Roese }
9888aff62dSStefan Roese 
9988aff62dSStefan Roese void rtc_set(struct rtc_time *tm)
10088aff62dSStefan Roese {
10188aff62dSStefan Roese 	u8 buf[M41T62_DATETIME_REG_SIZE];
10288aff62dSStefan Roese 
10388aff62dSStefan Roese 	debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
10488aff62dSStefan Roese 	      tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
10588aff62dSStefan Roese 	      tm->tm_hour, tm->tm_min, tm->tm_sec);
10688aff62dSStefan Roese 
10788aff62dSStefan Roese 	i2c_read(CFG_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE);
10888aff62dSStefan Roese 
10988aff62dSStefan Roese 	/* Merge time-data and register flags into buf[0..7] */
11088aff62dSStefan Roese 	buf[M41T62_REG_SSEC] = 0;
11188aff62dSStefan Roese 	buf[M41T62_REG_SEC] =
11288aff62dSStefan Roese 		BIN2BCD(tm->tm_sec) | (buf[M41T62_REG_SEC] & ~0x7f);
11388aff62dSStefan Roese 	buf[M41T62_REG_MIN] =
11488aff62dSStefan Roese 		BIN2BCD(tm->tm_min) | (buf[M41T62_REG_MIN] & ~0x7f);
11588aff62dSStefan Roese 	buf[M41T62_REG_HOUR] =
11688aff62dSStefan Roese 		BIN2BCD(tm->tm_hour) | (buf[M41T62_REG_HOUR] & ~0x3f) ;
11788aff62dSStefan Roese 	buf[M41T62_REG_WDAY] =
11888aff62dSStefan Roese 		(tm->tm_wday & 0x07) | (buf[M41T62_REG_WDAY] & ~0x07);
11988aff62dSStefan Roese 	buf[M41T62_REG_DAY] =
12088aff62dSStefan Roese 		BIN2BCD(tm->tm_mday) | (buf[M41T62_REG_DAY] & ~0x3f);
12188aff62dSStefan Roese 	buf[M41T62_REG_MON] =
12288aff62dSStefan Roese 		BIN2BCD(tm->tm_mon + 1) | (buf[M41T62_REG_MON] & ~0x1f);
12388aff62dSStefan Roese 	/* assume 20YY not 19YY */
12488aff62dSStefan Roese 	buf[M41T62_REG_YEAR] = BIN2BCD(tm->tm_year % 100);
12588aff62dSStefan Roese 
12688aff62dSStefan Roese 	if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, buf, M41T62_DATETIME_REG_SIZE))
12788aff62dSStefan Roese 		printf("I2C write failed in %s()\n", __func__);
12888aff62dSStefan Roese }
12988aff62dSStefan Roese 
13088aff62dSStefan Roese void rtc_reset(void)
13188aff62dSStefan Roese {
13288aff62dSStefan Roese 	/*
13388aff62dSStefan Roese 	 * Nothing to do
13488aff62dSStefan Roese 	 */
13588aff62dSStefan Roese }
13688aff62dSStefan Roese 
13788aff62dSStefan Roese #endif
138