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 MC146818 (PIXX4) RTC
34 #if defined(CONFIG_CMD_DATE)
36 static uchar rtc_read (uchar reg);
37 static void rtc_write (uchar reg, uchar val);
39 #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70
40 #define RTC_SECONDS 0x00
41 #define RTC_SECONDS_ALARM 0x01
42 #define RTC_MINUTES 0x02
43 #define RTC_MINUTES_ALARM 0x03
44 #define RTC_HOURS 0x04
45 #define RTC_HOURS_ALARM 0x05
46 #define RTC_DAY_OF_WEEK 0x06
47 #define RTC_DATE_OF_MONTH 0x07
48 #define RTC_MONTH 0x08
50 #define RTC_CONFIG_A 0x0A
51 #define RTC_CONFIG_B 0x0B
52 #define RTC_CONFIG_C 0x0C
53 #define RTC_CONFIG_D 0x0D
56 /* ------------------------------------------------------------------------- */
58 int rtc_get (struct rtc_time *tmp)
60 uchar sec, min, hour, mday, wday, mon, year;
61 /* here check if rtc can be accessed */
62 while((rtc_read(RTC_CONFIG_A)&0x80)==0x80);
63 sec = rtc_read (RTC_SECONDS);
64 min = rtc_read (RTC_MINUTES);
65 hour = rtc_read (RTC_HOURS);
66 mday = rtc_read (RTC_DATE_OF_MONTH);
67 wday = rtc_read (RTC_DAY_OF_WEEK);
68 mon = rtc_read (RTC_MONTH);
69 year = rtc_read (RTC_YEAR);
71 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
72 "hr: %02x min: %02x sec: %02x\n",
73 year, mon, mday, wday,
75 printf ( "Alarms: month: %02x hour: %02x min: %02x sec: %02x\n",
76 rtc_read (RTC_CONFIG_D) & 0x3F,
77 rtc_read (RTC_HOURS_ALARM),
78 rtc_read (RTC_MINUTES_ALARM),
79 rtc_read (RTC_SECONDS_ALARM) );
81 tmp->tm_sec = bcd2bin (sec & 0x7F);
82 tmp->tm_min = bcd2bin (min & 0x7F);
83 tmp->tm_hour = bcd2bin (hour & 0x3F);
84 tmp->tm_mday = bcd2bin (mday & 0x3F);
85 tmp->tm_mon = bcd2bin (mon & 0x1F);
86 tmp->tm_year = bcd2bin (year);
87 tmp->tm_wday = bcd2bin (wday & 0x07);
95 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
96 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
97 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
103 int rtc_set (struct rtc_time *tmp)
106 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
107 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
108 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
110 rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
112 rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
113 rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
114 rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
115 rtc_write (RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
116 rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
117 rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
118 rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
119 rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
124 void rtc_reset (void)
126 rtc_write(RTC_CONFIG_B,0x82); /* disables the RTC to update the regs */
127 rtc_write(RTC_CONFIG_A,0x20); /* Normal OP */
128 rtc_write(RTC_CONFIG_B,0x00);
129 rtc_write(RTC_CONFIG_B,0x00);
130 rtc_write(RTC_CONFIG_B,0x02); /* enables the RTC to update the regs */
133 /* ------------------------------------------------------------------------- */
135 #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
137 * use direct memory access
139 static uchar rtc_read (uchar reg)
141 return(in8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg));
144 static void rtc_write (uchar reg, uchar val)
146 out8(CONFIG_SYS_RTC_REG_BASE_ADDR+reg, val);
149 static uchar rtc_read (uchar reg)
151 out8(RTC_PORT_MC146818,reg);
152 return(in8(RTC_PORT_MC146818+1));
155 static void rtc_write (uchar reg, uchar val)
157 out8(RTC_PORT_MC146818,reg);
158 out8(RTC_PORT_MC146818+1,val);