1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#undef RTC_DEBUG
29
30#include <common.h>
31#include <command.h>
32#include <config.h>
33#include <rtc.h>
34#include <mk48t59.h>
35
36#if defined(CONFIG_BAB7xx)
37
38static uchar rtc_read (short reg)
39{
40 out8(RTC_PORT_ADDR0, reg & 0xFF);
41 out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
42 return in8(RTC_PORT_DATA);
43}
44
45static void rtc_write (short reg, uchar val)
46{
47 out8(RTC_PORT_ADDR0, reg & 0xFF);
48 out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
49 out8(RTC_PORT_DATA, val);
50}
51
52#elif defined(CONFIG_PCIPPC2)
53
54#include "../board/pcippc2/pcippc2.h"
55
56static uchar rtc_read (short reg)
57{
58 return in8(RTC(reg));
59}
60
61static void rtc_write (short reg, uchar val)
62{
63 out8(RTC(reg),val);
64}
65
66#elif defined(CONFIG_EVAL5200)
67
68static uchar rtc_read (short reg)
69{
70 return in8(RTC(reg));
71}
72
73static void rtc_write (short reg, uchar val)
74{
75 out8(RTC(reg),val);
76}
77
78#else
79# error Board specific rtc access functions should be supplied
80#endif
81
82
83
84void *nvram_read(void *dest, const short src, size_t count)
85{
86 uchar *d = (uchar *) dest;
87 short s = src;
88
89 while (count--)
90 *d++ = rtc_read(s++);
91
92 return dest;
93}
94
95void nvram_write(short dest, const void *src, size_t count)
96{
97 short d = dest;
98 uchar *s = (uchar *) src;
99
100 while (count--)
101 rtc_write(d++, *s++);
102}
103
104#if defined(CONFIG_CMD_DATE)
105
106
107
108int rtc_get (struct rtc_time *tmp)
109{
110 uchar save_ctrl_a;
111 uchar sec, min, hour, mday, wday, mon, year;
112
113
114 save_ctrl_a = rtc_read(RTC_CONTROLA);
115
116
117 save_ctrl_a |= RTC_CA_READ;
118 rtc_write(RTC_CONTROLA, save_ctrl_a);
119
120 sec = rtc_read (RTC_SECONDS);
121 min = rtc_read (RTC_MINUTES);
122 hour = rtc_read (RTC_HOURS);
123 mday = rtc_read (RTC_DAY_OF_MONTH);
124 wday = rtc_read (RTC_DAY_OF_WEEK);
125 mon = rtc_read (RTC_MONTH);
126 year = rtc_read (RTC_YEAR);
127
128
129 save_ctrl_a &= ~RTC_CA_READ;
130 rtc_write(RTC_CONTROLA, save_ctrl_a);
131
132#ifdef RTC_DEBUG
133 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
134 "hr: %02x min: %02x sec: %02x\n",
135 year, mon, mday, wday,
136 hour, min, sec );
137#endif
138 tmp->tm_sec = bcd2bin (sec & 0x7F);
139 tmp->tm_min = bcd2bin (min & 0x7F);
140 tmp->tm_hour = bcd2bin (hour & 0x3F);
141 tmp->tm_mday = bcd2bin (mday & 0x3F);
142 tmp->tm_mon = bcd2bin (mon & 0x1F);
143 tmp->tm_year = bcd2bin (year);
144 tmp->tm_wday = bcd2bin (wday & 0x07);
145 if(tmp->tm_year<70)
146 tmp->tm_year+=2000;
147 else
148 tmp->tm_year+=1900;
149 tmp->tm_yday = 0;
150 tmp->tm_isdst= 0;
151#ifdef RTC_DEBUG
152 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
153 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
154 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
155#endif
156
157 return 0;
158}
159
160int rtc_set (struct rtc_time *tmp)
161{
162 uchar save_ctrl_a;
163
164#ifdef RTC_DEBUG
165 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
166 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
167 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
168#endif
169 save_ctrl_a = rtc_read(RTC_CONTROLA);
170
171 save_ctrl_a |= RTC_CA_WRITE;
172 rtc_write(RTC_CONTROLA, save_ctrl_a);
173
174 rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
175 rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
176
177 rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
178 rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
179 rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
180 rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
181 rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
182
183 save_ctrl_a &= ~RTC_CA_WRITE;
184 rtc_write(RTC_CONTROLA, save_ctrl_a);
185
186 return 0;
187}
188
189void rtc_reset (void)
190{
191 uchar control_b;
192
193
194
195
196 control_b = rtc_read(RTC_CONTROLB);
197
198 control_b &= ~RTC_CB_STOP;
199 rtc_write(RTC_CONTROLB, control_b);
200}
201
202void rtc_set_watchdog(short multi, short res)
203{
204 uchar wd_value;
205
206 wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
207 rtc_write(RTC_WATCHDOG, wd_value);
208}
209
210#endif
211