1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20c698dcaSJean-Christophe PLAGNIOL-VILLARD /*
30c698dcaSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
40c698dcaSJean-Christophe PLAGNIOL-VILLARD * Andreas Heppel <aheppel@sysgo.de>
50c698dcaSJean-Christophe PLAGNIOL-VILLARD */
60c698dcaSJean-Christophe PLAGNIOL-VILLARD
70c698dcaSJean-Christophe PLAGNIOL-VILLARD /*
80c698dcaSJean-Christophe PLAGNIOL-VILLARD * Date & Time support for the MK48T59 RTC
90c698dcaSJean-Christophe PLAGNIOL-VILLARD */
100c698dcaSJean-Christophe PLAGNIOL-VILLARD
110c698dcaSJean-Christophe PLAGNIOL-VILLARD #undef RTC_DEBUG
120c698dcaSJean-Christophe PLAGNIOL-VILLARD
130c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
140c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
150c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <config.h>
160c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <rtc.h>
170c698dcaSJean-Christophe PLAGNIOL-VILLARD #include <mk48t59.h>
180c698dcaSJean-Christophe PLAGNIOL-VILLARD
190c698dcaSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_BAB7xx)
200c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_read(short reg)210c698dcaSJean-Christophe PLAGNIOL-VILLARD static uchar rtc_read (short reg)
220c698dcaSJean-Christophe PLAGNIOL-VILLARD {
230c698dcaSJean-Christophe PLAGNIOL-VILLARD out8(RTC_PORT_ADDR0, reg & 0xFF);
240c698dcaSJean-Christophe PLAGNIOL-VILLARD out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
250c698dcaSJean-Christophe PLAGNIOL-VILLARD return in8(RTC_PORT_DATA);
260c698dcaSJean-Christophe PLAGNIOL-VILLARD }
270c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_write(short reg,uchar val)280c698dcaSJean-Christophe PLAGNIOL-VILLARD static void rtc_write (short reg, uchar val)
290c698dcaSJean-Christophe PLAGNIOL-VILLARD {
300c698dcaSJean-Christophe PLAGNIOL-VILLARD out8(RTC_PORT_ADDR0, reg & 0xFF);
310c698dcaSJean-Christophe PLAGNIOL-VILLARD out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
320c698dcaSJean-Christophe PLAGNIOL-VILLARD out8(RTC_PORT_DATA, val);
330c698dcaSJean-Christophe PLAGNIOL-VILLARD }
340c698dcaSJean-Christophe PLAGNIOL-VILLARD
350c698dcaSJean-Christophe PLAGNIOL-VILLARD #elif defined(CONFIG_EVAL5200)
360c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_read(short reg)370c698dcaSJean-Christophe PLAGNIOL-VILLARD static uchar rtc_read (short reg)
380c698dcaSJean-Christophe PLAGNIOL-VILLARD {
390c698dcaSJean-Christophe PLAGNIOL-VILLARD return in8(RTC(reg));
400c698dcaSJean-Christophe PLAGNIOL-VILLARD }
410c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_write(short reg,uchar val)420c698dcaSJean-Christophe PLAGNIOL-VILLARD static void rtc_write (short reg, uchar val)
430c698dcaSJean-Christophe PLAGNIOL-VILLARD {
440c698dcaSJean-Christophe PLAGNIOL-VILLARD out8(RTC(reg),val);
450c698dcaSJean-Christophe PLAGNIOL-VILLARD }
460c698dcaSJean-Christophe PLAGNIOL-VILLARD
470c698dcaSJean-Christophe PLAGNIOL-VILLARD #else
480c698dcaSJean-Christophe PLAGNIOL-VILLARD # error Board specific rtc access functions should be supplied
490c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif
500c698dcaSJean-Christophe PLAGNIOL-VILLARD
510c698dcaSJean-Christophe PLAGNIOL-VILLARD /* ------------------------------------------------------------------------- */
520c698dcaSJean-Christophe PLAGNIOL-VILLARD
nvram_read(void * dest,const short src,size_t count)530c698dcaSJean-Christophe PLAGNIOL-VILLARD void *nvram_read(void *dest, const short src, size_t count)
540c698dcaSJean-Christophe PLAGNIOL-VILLARD {
550c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar *d = (uchar *) dest;
560c698dcaSJean-Christophe PLAGNIOL-VILLARD short s = src;
570c698dcaSJean-Christophe PLAGNIOL-VILLARD
580c698dcaSJean-Christophe PLAGNIOL-VILLARD while (count--)
590c698dcaSJean-Christophe PLAGNIOL-VILLARD *d++ = rtc_read(s++);
600c698dcaSJean-Christophe PLAGNIOL-VILLARD
610c698dcaSJean-Christophe PLAGNIOL-VILLARD return dest;
620c698dcaSJean-Christophe PLAGNIOL-VILLARD }
630c698dcaSJean-Christophe PLAGNIOL-VILLARD
nvram_write(short dest,const void * src,size_t count)640c698dcaSJean-Christophe PLAGNIOL-VILLARD void nvram_write(short dest, const void *src, size_t count)
650c698dcaSJean-Christophe PLAGNIOL-VILLARD {
660c698dcaSJean-Christophe PLAGNIOL-VILLARD short d = dest;
670c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar *s = (uchar *) src;
680c698dcaSJean-Christophe PLAGNIOL-VILLARD
690c698dcaSJean-Christophe PLAGNIOL-VILLARD while (count--)
700c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(d++, *s++);
710c698dcaSJean-Christophe PLAGNIOL-VILLARD }
720c698dcaSJean-Christophe PLAGNIOL-VILLARD
730c698dcaSJean-Christophe PLAGNIOL-VILLARD /* ------------------------------------------------------------------------- */
740c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_get(struct rtc_time * tmp)75b73a19e1SYuri Tikhonov int rtc_get (struct rtc_time *tmp)
760c698dcaSJean-Christophe PLAGNIOL-VILLARD {
770c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar save_ctrl_a;
780c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar sec, min, hour, mday, wday, mon, year;
790c698dcaSJean-Christophe PLAGNIOL-VILLARD
800c698dcaSJean-Christophe PLAGNIOL-VILLARD /* Simple: freeze the clock, read it and allow updates again */
810c698dcaSJean-Christophe PLAGNIOL-VILLARD save_ctrl_a = rtc_read(RTC_CONTROLA);
820c698dcaSJean-Christophe PLAGNIOL-VILLARD
830c698dcaSJean-Christophe PLAGNIOL-VILLARD /* Set the register to read the value. */
840c698dcaSJean-Christophe PLAGNIOL-VILLARD save_ctrl_a |= RTC_CA_READ;
850c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(RTC_CONTROLA, save_ctrl_a);
860c698dcaSJean-Christophe PLAGNIOL-VILLARD
870c698dcaSJean-Christophe PLAGNIOL-VILLARD sec = rtc_read (RTC_SECONDS);
880c698dcaSJean-Christophe PLAGNIOL-VILLARD min = rtc_read (RTC_MINUTES);
890c698dcaSJean-Christophe PLAGNIOL-VILLARD hour = rtc_read (RTC_HOURS);
900c698dcaSJean-Christophe PLAGNIOL-VILLARD mday = rtc_read (RTC_DAY_OF_MONTH);
910c698dcaSJean-Christophe PLAGNIOL-VILLARD wday = rtc_read (RTC_DAY_OF_WEEK);
920c698dcaSJean-Christophe PLAGNIOL-VILLARD mon = rtc_read (RTC_MONTH);
930c698dcaSJean-Christophe PLAGNIOL-VILLARD year = rtc_read (RTC_YEAR);
940c698dcaSJean-Christophe PLAGNIOL-VILLARD
950c698dcaSJean-Christophe PLAGNIOL-VILLARD /* re-enable update */
960c698dcaSJean-Christophe PLAGNIOL-VILLARD save_ctrl_a &= ~RTC_CA_READ;
970c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(RTC_CONTROLA, save_ctrl_a);
980c698dcaSJean-Christophe PLAGNIOL-VILLARD
990c698dcaSJean-Christophe PLAGNIOL-VILLARD #ifdef RTC_DEBUG
1000c698dcaSJean-Christophe PLAGNIOL-VILLARD printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
1010c698dcaSJean-Christophe PLAGNIOL-VILLARD "hr: %02x min: %02x sec: %02x\n",
1020c698dcaSJean-Christophe PLAGNIOL-VILLARD year, mon, mday, wday,
1030c698dcaSJean-Christophe PLAGNIOL-VILLARD hour, min, sec );
1040c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif
1050c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_sec = bcd2bin (sec & 0x7F);
1060c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_min = bcd2bin (min & 0x7F);
1070c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour = bcd2bin (hour & 0x3F);
1080c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mday = bcd2bin (mday & 0x3F);
1090c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_mon = bcd2bin (mon & 0x1F);
1100c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year = bcd2bin (year);
1110c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_wday = bcd2bin (wday & 0x07);
1120c698dcaSJean-Christophe PLAGNIOL-VILLARD if(tmp->tm_year<70)
1130c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year+=2000;
1140c698dcaSJean-Christophe PLAGNIOL-VILLARD else
1150c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year+=1900;
1160c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_yday = 0;
1170c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_isdst= 0;
1180c698dcaSJean-Christophe PLAGNIOL-VILLARD #ifdef RTC_DEBUG
1190c698dcaSJean-Christophe PLAGNIOL-VILLARD printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
1200c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
1210c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
1220c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif
123b73a19e1SYuri Tikhonov
124b73a19e1SYuri Tikhonov return 0;
1250c698dcaSJean-Christophe PLAGNIOL-VILLARD }
1260c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_set(struct rtc_time * tmp)127d1e23194SJean-Christophe PLAGNIOL-VILLARD int rtc_set (struct rtc_time *tmp)
1280c698dcaSJean-Christophe PLAGNIOL-VILLARD {
1290c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar save_ctrl_a;
1300c698dcaSJean-Christophe PLAGNIOL-VILLARD
1310c698dcaSJean-Christophe PLAGNIOL-VILLARD #ifdef RTC_DEBUG
1320c698dcaSJean-Christophe PLAGNIOL-VILLARD printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
1330c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
1340c698dcaSJean-Christophe PLAGNIOL-VILLARD tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
1350c698dcaSJean-Christophe PLAGNIOL-VILLARD #endif
1360c698dcaSJean-Christophe PLAGNIOL-VILLARD save_ctrl_a = rtc_read(RTC_CONTROLA);
1370c698dcaSJean-Christophe PLAGNIOL-VILLARD
1380c698dcaSJean-Christophe PLAGNIOL-VILLARD save_ctrl_a |= RTC_CA_WRITE;
1390c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
1400c698dcaSJean-Christophe PLAGNIOL-VILLARD
1410c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
1420c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
1430c698dcaSJean-Christophe PLAGNIOL-VILLARD
1440c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
1450c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
1460c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
1470c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
1480c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
1490c698dcaSJean-Christophe PLAGNIOL-VILLARD
1500c698dcaSJean-Christophe PLAGNIOL-VILLARD save_ctrl_a &= ~RTC_CA_WRITE;
1510c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
152d1e23194SJean-Christophe PLAGNIOL-VILLARD
153d1e23194SJean-Christophe PLAGNIOL-VILLARD return 0;
1540c698dcaSJean-Christophe PLAGNIOL-VILLARD }
1550c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_reset(void)1560c698dcaSJean-Christophe PLAGNIOL-VILLARD void rtc_reset (void)
1570c698dcaSJean-Christophe PLAGNIOL-VILLARD {
1580c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar control_b;
1590c698dcaSJean-Christophe PLAGNIOL-VILLARD
1600c698dcaSJean-Christophe PLAGNIOL-VILLARD /*
1610c698dcaSJean-Christophe PLAGNIOL-VILLARD * Start oscillator here.
1620c698dcaSJean-Christophe PLAGNIOL-VILLARD */
1630c698dcaSJean-Christophe PLAGNIOL-VILLARD control_b = rtc_read(RTC_CONTROLB);
1640c698dcaSJean-Christophe PLAGNIOL-VILLARD
1650c698dcaSJean-Christophe PLAGNIOL-VILLARD control_b &= ~RTC_CB_STOP;
1660c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(RTC_CONTROLB, control_b);
1670c698dcaSJean-Christophe PLAGNIOL-VILLARD }
1680c698dcaSJean-Christophe PLAGNIOL-VILLARD
rtc_set_watchdog(short multi,short res)1692c5e3cc4SWolfgang Denk void rtc_set_watchdog(short multi, short res)
1700c698dcaSJean-Christophe PLAGNIOL-VILLARD {
1710c698dcaSJean-Christophe PLAGNIOL-VILLARD uchar wd_value;
1720c698dcaSJean-Christophe PLAGNIOL-VILLARD
1730c698dcaSJean-Christophe PLAGNIOL-VILLARD wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
1740c698dcaSJean-Christophe PLAGNIOL-VILLARD rtc_write(RTC_WATCHDOG, wd_value);
1750c698dcaSJean-Christophe PLAGNIOL-VILLARD }
176