2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 * Date & Time support for the MK48T59 RTC
36 #if defined(CONFIG_RTC_MK48T59)
38 #if defined(CONFIG_BAB7xx)
40 static uchar rtc_read (short reg)
42 out8(RTC_PORT_ADDR0, reg & 0xFF);
43 out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
44 return in8(RTC_PORT_DATA);
47 static void rtc_write (short reg, uchar val)
49 out8(RTC_PORT_ADDR0, reg & 0xFF);
50 out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
51 out8(RTC_PORT_DATA, val);
54 #elif defined(CONFIG_PCIPPC2)
56 #include "../board/pcippc2/pcippc2.h"
58 static uchar rtc_read (short reg)
63 static void rtc_write (short reg, uchar val)
68 #elif defined(CONFIG_AMIGAONEG3SE)
70 #include "../board/MAI/AmigaOneG3SE/via686.h"
71 #include "../board/MAI/AmigaOneG3SE/memio.h"
74 static uchar rtc_read (short reg)
76 out_byte(CMOS_ADDR, (uint8)reg);
77 return in_byte(CMOS_DATA);
80 static void rtc_write (short reg, uchar val)
82 out_byte(CMOS_ADDR, (uint8)reg);
83 out_byte(CMOS_DATA, (uint8)val);
87 # error Board specific rtc access functions should be supplied
90 static unsigned bcd2bin (uchar n)
92 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
95 static unsigned char bin2bcd (unsigned int n)
97 return (((n / 10) << 4) | (n % 10));
100 /* ------------------------------------------------------------------------- */
102 void *nvram_read(void *dest, const short src, size_t count)
104 uchar *d = (uchar *) dest;
108 *d++ = rtc_read(s++);
113 void nvram_write(short dest, const void *src, size_t count)
116 uchar *s = (uchar *) src;
119 rtc_write(d++, *s++);
122 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
124 /* ------------------------------------------------------------------------- */
126 void rtc_get (struct rtc_time *tmp)
129 uchar sec, min, hour, mday, wday, mon, year;
131 /* Simple: freeze the clock, read it and allow updates again */
132 save_ctrl_a = rtc_read(RTC_CONTROLA);
134 /* Set the register to read the value. */
135 save_ctrl_a |= RTC_CA_READ;
136 rtc_write(RTC_CONTROLA, save_ctrl_a);
138 sec = rtc_read (RTC_SECONDS);
139 min = rtc_read (RTC_MINUTES);
140 hour = rtc_read (RTC_HOURS);
141 mday = rtc_read (RTC_DAY_OF_MONTH);
142 wday = rtc_read (RTC_DAY_OF_WEEK);
143 mon = rtc_read (RTC_MONTH);
144 year = rtc_read (RTC_YEAR);
146 /* re-enable update */
147 save_ctrl_a &= ~RTC_CA_READ;
148 rtc_write(RTC_CONTROLA, save_ctrl_a);
151 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
152 "hr: %02x min: %02x sec: %02x\n",
153 year, mon, mday, wday,
156 tmp->tm_sec = bcd2bin (sec & 0x7F);
157 tmp->tm_min = bcd2bin (min & 0x7F);
158 tmp->tm_hour = bcd2bin (hour & 0x3F);
159 tmp->tm_mday = bcd2bin (mday & 0x3F);
160 tmp->tm_mon = bcd2bin (mon & 0x1F);
161 tmp->tm_year = bcd2bin (year);
162 tmp->tm_wday = bcd2bin (wday & 0x07);
170 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
171 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
172 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
176 void rtc_set (struct rtc_time *tmp)
181 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
182 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
183 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
185 save_ctrl_a = rtc_read(RTC_CONTROLA);
187 save_ctrl_a |= RTC_CA_WRITE;
188 rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
190 rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
191 rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
193 rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
194 rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
195 rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
196 rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
197 rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
199 save_ctrl_a &= ~RTC_CA_WRITE;
200 rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
203 void rtc_reset (void)
208 * Start oscillator here.
210 control_b = rtc_read(RTC_CONTROLB);
212 control_b &= ~RTC_CB_STOP;
213 rtc_write(RTC_CONTROLB, control_b);
216 void rtc_set_watchdog(short multi, short res)
220 wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
221 rtc_write(RTC_WATCHDOG, wd_value);
224 #endif /* (CONFIG_COMMANDS & CFG_CMD_DATE) */
225 #endif /* CONFIG_RTC_MK48T59 */