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