blob: dabf3222b8461114908a84a2a562e811ea2976de [file] [log] [blame]
wdenk1df49e22002-09-17 21:37:55 +00001/*
2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Date & Time support for the MK48T59 RTC
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
wdenk1df49e22002-09-17 21:37:55 +000036#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
wdenk452cfd62002-11-19 11:04:11 +000066#elif defined(CONFIG_AMIGAONEG3SE)
67
68#include "../board/MAI/AmigaOneG3SE/via686.h"
69#include "../board/MAI/AmigaOneG3SE/memio.h"
70
71
72static uchar rtc_read (short reg)
73{
74 out_byte(CMOS_ADDR, (uint8)reg);
75 return in_byte(CMOS_DATA);
76}
77
78static void rtc_write (short reg, uchar val)
79{
80 out_byte(CMOS_ADDR, (uint8)reg);
81 out_byte(CMOS_DATA, (uint8)val);
82}
83
wdenke97d3d92004-02-23 22:22:28 +000084#elif defined(CONFIG_EVAL5200)
85
86static uchar rtc_read (short reg)
87{
88 return in8(RTC(reg));
89}
90
91static void rtc_write (short reg, uchar val)
92{
93 out8(RTC(reg),val);
94}
95
wdenk1df49e22002-09-17 21:37:55 +000096#else
97# error Board specific rtc access functions should be supplied
98#endif
99
100static unsigned bcd2bin (uchar n)
101{
102 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
103}
104
105static unsigned char bin2bcd (unsigned int n)
106{
107 return (((n / 10) << 4) | (n % 10));
108}
109
110/* ------------------------------------------------------------------------- */
111
112void *nvram_read(void *dest, const short src, size_t count)
113{
114 uchar *d = (uchar *) dest;
115 short s = src;
116
117 while (count--)
118 *d++ = rtc_read(s++);
119
120 return dest;
121}
122
123void nvram_write(short dest, const void *src, size_t count)
124{
125 short d = dest;
126 uchar *s = (uchar *) src;
127
128 while (count--)
129 rtc_write(d++, *s++);
130}
131
Jon Loeligerf7e91252007-07-09 18:10:50 -0500132#if defined(CONFIG_CMD_DATE)
wdenk1df49e22002-09-17 21:37:55 +0000133
134/* ------------------------------------------------------------------------- */
135
Yuri Tikhonov9bacd942008-03-20 17:56:04 +0300136int rtc_get (struct rtc_time *tmp)
wdenk1df49e22002-09-17 21:37:55 +0000137{
138 uchar save_ctrl_a;
139 uchar sec, min, hour, mday, wday, mon, year;
140
141 /* Simple: freeze the clock, read it and allow updates again */
142 save_ctrl_a = rtc_read(RTC_CONTROLA);
143
144 /* Set the register to read the value. */
145 save_ctrl_a |= RTC_CA_READ;
146 rtc_write(RTC_CONTROLA, save_ctrl_a);
147
148 sec = rtc_read (RTC_SECONDS);
149 min = rtc_read (RTC_MINUTES);
150 hour = rtc_read (RTC_HOURS);
151 mday = rtc_read (RTC_DAY_OF_MONTH);
152 wday = rtc_read (RTC_DAY_OF_WEEK);
153 mon = rtc_read (RTC_MONTH);
154 year = rtc_read (RTC_YEAR);
155
156 /* re-enable update */
157 save_ctrl_a &= ~RTC_CA_READ;
158 rtc_write(RTC_CONTROLA, save_ctrl_a);
159
160#ifdef RTC_DEBUG
161 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
162 "hr: %02x min: %02x sec: %02x\n",
163 year, mon, mday, wday,
164 hour, min, sec );
165#endif
166 tmp->tm_sec = bcd2bin (sec & 0x7F);
167 tmp->tm_min = bcd2bin (min & 0x7F);
168 tmp->tm_hour = bcd2bin (hour & 0x3F);
169 tmp->tm_mday = bcd2bin (mday & 0x3F);
170 tmp->tm_mon = bcd2bin (mon & 0x1F);
171 tmp->tm_year = bcd2bin (year);
172 tmp->tm_wday = bcd2bin (wday & 0x07);
173 if(tmp->tm_year<70)
174 tmp->tm_year+=2000;
175 else
176 tmp->tm_year+=1900;
177 tmp->tm_yday = 0;
178 tmp->tm_isdst= 0;
179#ifdef RTC_DEBUG
180 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
181 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
182 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
183#endif
Yuri Tikhonov9bacd942008-03-20 17:56:04 +0300184
185 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000186}
187
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200188int rtc_set (struct rtc_time *tmp)
wdenk1df49e22002-09-17 21:37:55 +0000189{
190 uchar save_ctrl_a;
191
192#ifdef RTC_DEBUG
193 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
194 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
195 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
196#endif
197 save_ctrl_a = rtc_read(RTC_CONTROLA);
198
199 save_ctrl_a |= RTC_CA_WRITE;
200 rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
201
202 rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
203 rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
204
205 rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
206 rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
207 rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
208 rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
209 rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
210
211 save_ctrl_a &= ~RTC_CA_WRITE;
212 rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200213
214 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000215}
216
217void rtc_reset (void)
218{
219 uchar control_b;
220
221 /*
222 * Start oscillator here.
223 */
224 control_b = rtc_read(RTC_CONTROLB);
225
226 control_b &= ~RTC_CB_STOP;
227 rtc_write(RTC_CONTROLB, control_b);
228}
229
Wolfgang Denk489cf6d2008-09-08 21:28:14 +0200230void rtc_set_watchdog(short multi, short res)
wdenk1df49e22002-09-17 21:37:55 +0000231{
232 uchar wd_value;
233
234 wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
235 rtc_write(RTC_WATCHDOG, wd_value);
236}
237
Jon Loeligerf7e91252007-07-09 18:10:50 -0500238#endif