1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20c698dcaSJean-Christophe PLAGNIOL-VILLARD /*
30c698dcaSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2004
40c698dcaSJean-Christophe PLAGNIOL-VILLARD * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
50c698dcaSJean-Christophe PLAGNIOL-VILLARD */
60c698dcaSJean-Christophe PLAGNIOL-VILLARD
70c698dcaSJean-Christophe PLAGNIOL-VILLARD /*
80c698dcaSJean-Christophe PLAGNIOL-VILLARD * Date & Time support for MAXIM MAX6900 RTC
90c698dcaSJean-Christophe PLAGNIOL-VILLARD */
100c698dcaSJean-Christophe PLAGNIOL-VILLARD
110c698dcaSJean-Christophe PLAGNIOL-VILLARD /* #define DEBUG */
120c698dcaSJean-Christophe PLAGNIOL-VILLARD
130c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
140c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
150c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <rtc.h>
160c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h>
170c698dcaSJean-Christophe PLAGNIOL-VILLARD
186d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_SYS_I2C_RTC_ADDR
196d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_I2C_RTC_ADDR 0x50
200c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif
210c698dcaSJean-Christophe PLAGNIOL-VILLARD
220c698dcaSJean-Christophe PLAGNIOL-VILLARD /* ------------------------------------------------------------------------- */
230c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_read(uchar reg)240c698dcaSJean-Christophe PLAGNIOL-VILLARD static uchar rtc_read (uchar reg)
250c698dcaSJean-Christophe PLAGNIOL-VILLARD {
266d0f6bcfSJean-Christophe PLAGNIOL-VILLARD return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
270c698dcaSJean-Christophe PLAGNIOL-VILLARD }
280c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_write(uchar reg,uchar val)290c698dcaSJean-Christophe PLAGNIOL-VILLARD static void rtc_write (uchar reg, uchar val)
300c698dcaSJean-Christophe PLAGNIOL-VILLARD {
316d0f6bcfSJean-Christophe PLAGNIOL-VILLARD i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
320c698dcaSJean-Christophe PLAGNIOL-VILLARD udelay(2500);
330c698dcaSJean-Christophe PLAGNIOL-VILLARD }
340c698dcaSJean-Christophe PLAGNIOL-VILLARD
350c698dcaSJean-Christophe PLAGNIOL-VILLARD /* ------------------------------------------------------------------------- */
360c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_get(struct rtc_time * tmp)37b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *tmp)
380c698dcaSJean-Christophe PLAGNIOL-VILLARD {
390c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar sec, min, hour, mday, wday, mon, cent, year;
400c698dcaSJean-Christophe PLAGNIOL-VILLARD int retry = 1;
410c698dcaSJean-Christophe PLAGNIOL-VILLARD
420c698dcaSJean-Christophe PLAGNIOL-VILLARD do {
430c698dcaSJean-Christophe PLAGNIOL-VILLARD sec = rtc_read (0x80);
440c698dcaSJean-Christophe PLAGNIOL-VILLARD min = rtc_read (0x82);
450c698dcaSJean-Christophe PLAGNIOL-VILLARD hour = rtc_read (0x84);
460c698dcaSJean-Christophe PLAGNIOL-VILLARD mday = rtc_read (0x86);
470c698dcaSJean-Christophe PLAGNIOL-VILLARD mon = rtc_read (0x88);
480c698dcaSJean-Christophe PLAGNIOL-VILLARD wday = rtc_read (0x8a);
490c698dcaSJean-Christophe PLAGNIOL-VILLARD year = rtc_read (0x8c);
500c698dcaSJean-Christophe PLAGNIOL-VILLARD cent = rtc_read (0x92);
510c698dcaSJean-Christophe PLAGNIOL-VILLARD /*
520c698dcaSJean-Christophe PLAGNIOL-VILLARD * Check for seconds rollover
530c698dcaSJean-Christophe PLAGNIOL-VILLARD */
540c698dcaSJean-Christophe PLAGNIOL-VILLARD if ((sec != 59) || (rtc_read(0x80) == sec)){
550c698dcaSJean-Christophe PLAGNIOL-VILLARD retry = 0;
560c698dcaSJean-Christophe PLAGNIOL-VILLARD }
570c698dcaSJean-Christophe PLAGNIOL-VILLARD } while (retry);
580c698dcaSJean-Christophe PLAGNIOL-VILLARD
590c698dcaSJean-Christophe PLAGNIOL-VILLARD debug ( "Get RTC year: %02x mon: %02x cent: %02x mday: %02x wday: %02x "
600c698dcaSJean-Christophe PLAGNIOL-VILLARD "hr: %02x min: %02x sec: %02x\n",
610c698dcaSJean-Christophe PLAGNIOL-VILLARD year, mon, cent, mday, wday,
620c698dcaSJean-Christophe PLAGNIOL-VILLARD hour, min, sec );
630c698dcaSJean-Christophe PLAGNIOL-VILLARD
640c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_sec = bcd2bin (sec & 0x7F);
650c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_min = bcd2bin (min & 0x7F);
660c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour = bcd2bin (hour & 0x3F);
670c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mday = bcd2bin (mday & 0x3F);
680c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mon = bcd2bin (mon & 0x1F);
690c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year = bcd2bin (year) + bcd2bin(cent) * 100;
700c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_wday = bcd2bin (wday & 0x07);
710c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_yday = 0;
720c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_isdst= 0;
730c698dcaSJean-Christophe PLAGNIOL-VILLARD
740c698dcaSJean-Christophe PLAGNIOL-VILLARD debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
750c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
760c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
77b73a19e1SYuri Tikhonov
78b73a19e1SYuri Tikhonov return 0;
790c698dcaSJean-Christophe PLAGNIOL-VILLARD }
800c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_set(struct rtc_time * tmp)81d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *tmp)
820c698dcaSJean-Christophe PLAGNIOL-VILLARD {
830c698dcaSJean-Christophe PLAGNIOL-VILLARD
840c698dcaSJean-Christophe PLAGNIOL-VILLARD debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
850c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
860c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
870c698dcaSJean-Christophe PLAGNIOL-VILLARD
880c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x9E, 0x00);
890c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x80, 0); /* Clear seconds to ensure no rollover */
900c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x92, bin2bcd(tmp->tm_year / 100));
910c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x8c, bin2bcd(tmp->tm_year % 100));
920c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x8a, bin2bcd(tmp->tm_wday));
930c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x88, bin2bcd(tmp->tm_mon));
940c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x86, bin2bcd(tmp->tm_mday));
950c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x84, bin2bcd(tmp->tm_hour));
960c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x82, bin2bcd(tmp->tm_min ));
970c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (0x80, bin2bcd(tmp->tm_sec ));
98d1e23194SJean-Christophe PLAGNIOL-VILLARD
99d1e23194SJean-Christophe PLAGNIOL-VILLARD return 0;
1000c698dcaSJean-Christophe PLAGNIOL-VILLARD }
1010c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_reset(void)1020c698dcaSJean-Christophe PLAGNIOL-VILLARD void rtc_reset (void)
1030c698dcaSJean-Christophe PLAGNIOL-VILLARD {
1040c698dcaSJean-Christophe PLAGNIOL-VILLARD }
105