18809a271SStefan Roese /*
28809a271SStefan Roese * (C) Copyright 2007
38809a271SStefan Roese * Stefan Roese, DENX Software Engineering, sr@denx.de.
48809a271SStefan Roese *
58809a271SStefan Roese * based on a the Linux rtc-x1207.c driver which is:
68809a271SStefan Roese * Copyright 2004 Karen Spearel
78809a271SStefan Roese * Copyright 2005 Alessandro Zummo
88809a271SStefan Roese *
98809a271SStefan Roese * Information and datasheet:
108809a271SStefan Roese * http://www.intersil.com/cda/deviceinfo/0,1477,X1205,00.html
118809a271SStefan Roese *
12*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
138809a271SStefan Roese */
148809a271SStefan Roese
158809a271SStefan Roese /*
168809a271SStefan Roese * Date & Time support for Xicor/Intersil X1205 RTC
178809a271SStefan Roese */
188809a271SStefan Roese
198809a271SStefan Roese /* #define DEBUG */
208809a271SStefan Roese
218809a271SStefan Roese #include <common.h>
228809a271SStefan Roese #include <command.h>
238809a271SStefan Roese #include <rtc.h>
248809a271SStefan Roese #include <i2c.h>
258809a271SStefan Roese
26871c18ddSMichal Simek #if defined(CONFIG_CMD_DATE)
278809a271SStefan Roese
288809a271SStefan Roese #define CCR_SEC 0
298809a271SStefan Roese #define CCR_MIN 1
308809a271SStefan Roese #define CCR_HOUR 2
318809a271SStefan Roese #define CCR_MDAY 3
328809a271SStefan Roese #define CCR_MONTH 4
338809a271SStefan Roese #define CCR_YEAR 5
348809a271SStefan Roese #define CCR_WDAY 6
358809a271SStefan Roese #define CCR_Y2K 7
368809a271SStefan Roese
378809a271SStefan Roese #define X1205_REG_SR 0x3F /* status register */
388809a271SStefan Roese #define X1205_REG_Y2K 0x37
398809a271SStefan Roese #define X1205_REG_DW 0x36
408809a271SStefan Roese #define X1205_REG_YR 0x35
418809a271SStefan Roese #define X1205_REG_MO 0x34
428809a271SStefan Roese #define X1205_REG_DT 0x33
438809a271SStefan Roese #define X1205_REG_HR 0x32
448809a271SStefan Roese #define X1205_REG_MN 0x31
458809a271SStefan Roese #define X1205_REG_SC 0x30
468809a271SStefan Roese #define X1205_REG_DTR 0x13
478809a271SStefan Roese #define X1205_REG_ATR 0x12
488809a271SStefan Roese #define X1205_REG_INT 0x11
498809a271SStefan Roese #define X1205_REG_0 0x10
508809a271SStefan Roese #define X1205_REG_Y2K1 0x0F
518809a271SStefan Roese #define X1205_REG_DWA1 0x0E
528809a271SStefan Roese #define X1205_REG_YRA1 0x0D
538809a271SStefan Roese #define X1205_REG_MOA1 0x0C
548809a271SStefan Roese #define X1205_REG_DTA1 0x0B
558809a271SStefan Roese #define X1205_REG_HRA1 0x0A
568809a271SStefan Roese #define X1205_REG_MNA1 0x09
578809a271SStefan Roese #define X1205_REG_SCA1 0x08
588809a271SStefan Roese #define X1205_REG_Y2K0 0x07
598809a271SStefan Roese #define X1205_REG_DWA0 0x06
608809a271SStefan Roese #define X1205_REG_YRA0 0x05
618809a271SStefan Roese #define X1205_REG_MOA0 0x04
628809a271SStefan Roese #define X1205_REG_DTA0 0x03
638809a271SStefan Roese #define X1205_REG_HRA0 0x02
648809a271SStefan Roese #define X1205_REG_MNA0 0x01
658809a271SStefan Roese #define X1205_REG_SCA0 0x00
668809a271SStefan Roese
678809a271SStefan Roese #define X1205_CCR_BASE 0x30 /* Base address of CCR */
688809a271SStefan Roese #define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */
698809a271SStefan Roese
708809a271SStefan Roese #define X1205_SR_RTCF 0x01 /* Clock failure */
718809a271SStefan Roese #define X1205_SR_WEL 0x02 /* Write Enable Latch */
728809a271SStefan Roese #define X1205_SR_RWEL 0x04 /* Register Write Enable */
738809a271SStefan Roese
748809a271SStefan Roese #define X1205_DTR_DTR0 0x01
758809a271SStefan Roese #define X1205_DTR_DTR1 0x02
768809a271SStefan Roese #define X1205_DTR_DTR2 0x04
778809a271SStefan Roese
788809a271SStefan Roese #define X1205_HR_MIL 0x80 /* Set in ccr.hour for 24 hr mode */
798809a271SStefan Roese
rtc_write(int reg,u8 val)808809a271SStefan Roese static void rtc_write(int reg, u8 val)
818809a271SStefan Roese {
826d0f6bcfSJean-Christophe PLAGNIOL-VILLARD i2c_write(CONFIG_SYS_I2C_RTC_ADDR, reg, 2, &val, 1);
838809a271SStefan Roese }
848809a271SStefan Roese
858809a271SStefan Roese /*
868809a271SStefan Roese * In the routines that deal directly with the x1205 hardware, we use
878809a271SStefan Roese * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
888809a271SStefan Roese * Epoch is initialized as 2000. Time is set to UTC.
898809a271SStefan Roese */
rtc_get(struct rtc_time * tm)90b73a19e1SYuri Tikhonov int rtc_get(struct rtc_time *tm)
918809a271SStefan Roese {
928809a271SStefan Roese u8 buf[8];
938809a271SStefan Roese
946d0f6bcfSJean-Christophe PLAGNIOL-VILLARD i2c_read(CONFIG_SYS_I2C_RTC_ADDR, X1205_CCR_BASE, 2, buf, 8);
958809a271SStefan Roese
968809a271SStefan Roese debug("%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
978809a271SStefan Roese "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
988809a271SStefan Roese __FUNCTION__,
998809a271SStefan Roese buf[0], buf[1], buf[2], buf[3],
1008809a271SStefan Roese buf[4], buf[5], buf[6], buf[7]);
1018809a271SStefan Roese
102e84aba13SAlbin Tonnerre tm->tm_sec = bcd2bin(buf[CCR_SEC]);
103e84aba13SAlbin Tonnerre tm->tm_min = bcd2bin(buf[CCR_MIN]);
104e84aba13SAlbin Tonnerre tm->tm_hour = bcd2bin(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
105e84aba13SAlbin Tonnerre tm->tm_mday = bcd2bin(buf[CCR_MDAY]);
106e84aba13SAlbin Tonnerre tm->tm_mon = bcd2bin(buf[CCR_MONTH]); /* mon is 0-11 */
107e84aba13SAlbin Tonnerre tm->tm_year = bcd2bin(buf[CCR_YEAR])
108e84aba13SAlbin Tonnerre + (bcd2bin(buf[CCR_Y2K]) * 100);
1098809a271SStefan Roese tm->tm_wday = buf[CCR_WDAY];
1108809a271SStefan Roese
1118809a271SStefan Roese debug("%s: tm is secs=%d, mins=%d, hours=%d, "
1128809a271SStefan Roese "mday=%d, mon=%d, year=%d, wday=%d\n",
1138809a271SStefan Roese __FUNCTION__,
1148809a271SStefan Roese tm->tm_sec, tm->tm_min, tm->tm_hour,
1158809a271SStefan Roese tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
116b73a19e1SYuri Tikhonov
117b73a19e1SYuri Tikhonov return 0;
1188809a271SStefan Roese }
1198809a271SStefan Roese
rtc_set(struct rtc_time * tm)120d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set(struct rtc_time *tm)
1218809a271SStefan Roese {
1228809a271SStefan Roese int i;
1238809a271SStefan Roese u8 buf[8];
1248809a271SStefan Roese
1258809a271SStefan Roese debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
1268809a271SStefan Roese tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
1278809a271SStefan Roese tm->tm_hour, tm->tm_min, tm->tm_sec);
1288809a271SStefan Roese
129e84aba13SAlbin Tonnerre buf[CCR_SEC] = bin2bcd(tm->tm_sec);
130e84aba13SAlbin Tonnerre buf[CCR_MIN] = bin2bcd(tm->tm_min);
1318809a271SStefan Roese
1328809a271SStefan Roese /* set hour and 24hr bit */
133e84aba13SAlbin Tonnerre buf[CCR_HOUR] = bin2bcd(tm->tm_hour) | X1205_HR_MIL;
1348809a271SStefan Roese
135e84aba13SAlbin Tonnerre buf[CCR_MDAY] = bin2bcd(tm->tm_mday);
1368809a271SStefan Roese
1378809a271SStefan Roese /* month, 1 - 12 */
138e84aba13SAlbin Tonnerre buf[CCR_MONTH] = bin2bcd(tm->tm_mon);
1398809a271SStefan Roese
1408809a271SStefan Roese /* year, since the rtc epoch*/
141e84aba13SAlbin Tonnerre buf[CCR_YEAR] = bin2bcd(tm->tm_year % 100);
1428809a271SStefan Roese buf[CCR_WDAY] = tm->tm_wday & 0x07;
143e84aba13SAlbin Tonnerre buf[CCR_Y2K] = bin2bcd(tm->tm_year / 100);
1448809a271SStefan Roese
1458809a271SStefan Roese /* this sequence is required to unlock the chip */
1468809a271SStefan Roese rtc_write(X1205_REG_SR, X1205_SR_WEL);
1478809a271SStefan Roese rtc_write(X1205_REG_SR, X1205_SR_WEL | X1205_SR_RWEL);
1488809a271SStefan Roese
1498809a271SStefan Roese /* write register's data */
1508809a271SStefan Roese for (i = 0; i < 8; i++)
1518809a271SStefan Roese rtc_write(X1205_CCR_BASE + i, buf[i]);
1528809a271SStefan Roese
1538809a271SStefan Roese rtc_write(X1205_REG_SR, 0);
154d1e23194SJean-Christophe PLAGNIOL-VILLARD
155d1e23194SJean-Christophe PLAGNIOL-VILLARD return 0;
1568809a271SStefan Roese }
1578809a271SStefan Roese
rtc_reset(void)1588809a271SStefan Roese void rtc_reset(void)
1598809a271SStefan Roese {
1608809a271SStefan Roese /*
1618809a271SStefan Roese * Nothing to do
1628809a271SStefan Roese */
1638809a271SStefan Roese }
1648809a271SStefan Roese
1658809a271SStefan Roese #endif
166