blob: 8c90a7040767571605dad911468a930dbfd0fa70 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk1df49e22002-09-17 21:37:55 +00002/*
3 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Andreas Heppel <aheppel@sysgo.de>
wdenk1df49e22002-09-17 21:37:55 +00005 */
6
7/*
8 * Date & Time support for the MK48T59 RTC
9 */
10
11#undef RTC_DEBUG
12
13#include <common.h>
14#include <command.h>
15#include <config.h>
16#include <rtc.h>
17#include <mk48t59.h>
18
wdenk1df49e22002-09-17 21:37:55 +000019#if defined(CONFIG_BAB7xx)
20
21static uchar rtc_read (short reg)
22{
23 out8(RTC_PORT_ADDR0, reg & 0xFF);
24 out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
25 return in8(RTC_PORT_DATA);
26}
27
28static void rtc_write (short reg, uchar val)
29{
30 out8(RTC_PORT_ADDR0, reg & 0xFF);
31 out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
32 out8(RTC_PORT_DATA, val);
33}
34
wdenke97d3d92004-02-23 22:22:28 +000035#elif defined(CONFIG_EVAL5200)
36
37static uchar rtc_read (short reg)
38{
39 return in8(RTC(reg));
40}
41
42static void rtc_write (short reg, uchar val)
43{
44 out8(RTC(reg),val);
45}
46
wdenk1df49e22002-09-17 21:37:55 +000047#else
48# error Board specific rtc access functions should be supplied
49#endif
50
wdenk1df49e22002-09-17 21:37:55 +000051/* ------------------------------------------------------------------------- */
52
53void *nvram_read(void *dest, const short src, size_t count)
54{
55 uchar *d = (uchar *) dest;
56 short s = src;
57
58 while (count--)
59 *d++ = rtc_read(s++);
60
61 return dest;
62}
63
64void nvram_write(short dest, const void *src, size_t count)
65{
66 short d = dest;
67 uchar *s = (uchar *) src;
68
69 while (count--)
70 rtc_write(d++, *s++);
71}
72
wdenk1df49e22002-09-17 21:37:55 +000073/* ------------------------------------------------------------------------- */
74
Yuri Tikhonov9bacd942008-03-20 17:56:04 +030075int rtc_get (struct rtc_time *tmp)
wdenk1df49e22002-09-17 21:37:55 +000076{
77 uchar save_ctrl_a;
78 uchar sec, min, hour, mday, wday, mon, year;
79
80 /* Simple: freeze the clock, read it and allow updates again */
81 save_ctrl_a = rtc_read(RTC_CONTROLA);
82
83 /* Set the register to read the value. */
84 save_ctrl_a |= RTC_CA_READ;
85 rtc_write(RTC_CONTROLA, save_ctrl_a);
86
87 sec = rtc_read (RTC_SECONDS);
88 min = rtc_read (RTC_MINUTES);
89 hour = rtc_read (RTC_HOURS);
90 mday = rtc_read (RTC_DAY_OF_MONTH);
91 wday = rtc_read (RTC_DAY_OF_WEEK);
92 mon = rtc_read (RTC_MONTH);
93 year = rtc_read (RTC_YEAR);
94
95 /* re-enable update */
96 save_ctrl_a &= ~RTC_CA_READ;
97 rtc_write(RTC_CONTROLA, save_ctrl_a);
98
99#ifdef RTC_DEBUG
100 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
101 "hr: %02x min: %02x sec: %02x\n",
102 year, mon, mday, wday,
103 hour, min, sec );
104#endif
105 tmp->tm_sec = bcd2bin (sec & 0x7F);
106 tmp->tm_min = bcd2bin (min & 0x7F);
107 tmp->tm_hour = bcd2bin (hour & 0x3F);
108 tmp->tm_mday = bcd2bin (mday & 0x3F);
109 tmp->tm_mon = bcd2bin (mon & 0x1F);
110 tmp->tm_year = bcd2bin (year);
111 tmp->tm_wday = bcd2bin (wday & 0x07);
112 if(tmp->tm_year<70)
113 tmp->tm_year+=2000;
114 else
115 tmp->tm_year+=1900;
116 tmp->tm_yday = 0;
117 tmp->tm_isdst= 0;
118#ifdef RTC_DEBUG
119 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
120 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
121 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
122#endif
Yuri Tikhonov9bacd942008-03-20 17:56:04 +0300123
124 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000125}
126
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200127int rtc_set (struct rtc_time *tmp)
wdenk1df49e22002-09-17 21:37:55 +0000128{
129 uchar save_ctrl_a;
130
131#ifdef RTC_DEBUG
132 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
133 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
134 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
135#endif
136 save_ctrl_a = rtc_read(RTC_CONTROLA);
137
138 save_ctrl_a |= RTC_CA_WRITE;
139 rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
140
141 rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
142 rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
143
144 rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
145 rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
146 rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
147 rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
148 rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
149
150 save_ctrl_a &= ~RTC_CA_WRITE;
151 rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200152
153 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000154}
155
156void rtc_reset (void)
157{
158 uchar control_b;
159
160 /*
161 * Start oscillator here.
162 */
163 control_b = rtc_read(RTC_CONTROLB);
164
165 control_b &= ~RTC_CB_STOP;
166 rtc_write(RTC_CONTROLB, control_b);
167}
168
Wolfgang Denk489cf6d2008-09-08 21:28:14 +0200169void rtc_set_watchdog(short multi, short res)
wdenk1df49e22002-09-17 21:37:55 +0000170{
171 uchar wd_value;
172
173 wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
174 rtc_write(RTC_WATCHDOG, wd_value);
175}