1 // SPDX-License-Identifier: GPL-2.0+
4 * Denis Peter MPL AG Switzerland. d.peter@mpl.ch
8 * Date & Time support for the MC146818 (PIXX4) RTC
16 #if defined(CONFIG_X86) || defined(CONFIG_MALTA)
19 #define out8(p, v) outb(v, p)
22 /* Set this to 1 to clear the CMOS RAM */
25 #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70
26 #define RTC_SECONDS 0x00
27 #define RTC_SECONDS_ALARM 0x01
28 #define RTC_MINUTES 0x02
29 #define RTC_MINUTES_ALARM 0x03
30 #define RTC_HOURS 0x04
31 #define RTC_HOURS_ALARM 0x05
32 #define RTC_DAY_OF_WEEK 0x06
33 #define RTC_DATE_OF_MONTH 0x07
34 #define RTC_MONTH 0x08
36 #define RTC_CONFIG_A 0x0a
37 #define RTC_CONFIG_B 0x0b
38 #define RTC_CONFIG_C 0x0c
39 #define RTC_CONFIG_D 0x0d
40 #define RTC_REG_SIZE 0x80
42 #define RTC_CONFIG_A_REF_CLCK_32KHZ (1 << 5)
43 #define RTC_CONFIG_A_RATE_1024HZ 6
45 #define RTC_CONFIG_B_24H (1 << 1)
47 #define RTC_CONFIG_D_VALID_RAM_AND_TIME 0x80
49 static int mc146818_read8(int reg)
51 #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
52 return in8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg);
60 out8(RTC_PORT_MC146818 + ofs, reg);
62 return in8(RTC_PORT_MC146818 + ofs + 1);
66 static void mc146818_write8(int reg, uchar val)
68 #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
69 out8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg, val);
77 out8(RTC_PORT_MC146818 + ofs, reg);
78 out8(RTC_PORT_MC146818 + ofs + 1, val);
82 static int mc146818_get(struct rtc_time *tmp)
84 uchar sec, min, hour, mday, wday __attribute__((unused)),mon, year;
86 /* here check if rtc can be accessed */
87 while ((mc146818_read8(RTC_CONFIG_A) & 0x80) == 0x80)
90 sec = mc146818_read8(RTC_SECONDS);
91 min = mc146818_read8(RTC_MINUTES);
92 hour = mc146818_read8(RTC_HOURS);
93 mday = mc146818_read8(RTC_DATE_OF_MONTH);
94 wday = mc146818_read8(RTC_DAY_OF_WEEK);
95 mon = mc146818_read8(RTC_MONTH);
96 year = mc146818_read8(RTC_YEAR);
98 printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x hr: %02x min: %02x sec: %02x\n",
99 year, mon, mday, wday, hour, min, sec);
100 printf("Alarms: mday: %02x hour: %02x min: %02x sec: %02x\n",
101 mc146818_read8(RTC_CONFIG_D) & 0x3f,
102 mc146818_read8(RTC_HOURS_ALARM),
103 mc146818_read8(RTC_MINUTES_ALARM),
104 mc146818_read8(RTC_SECONDS_ALARM));
106 tmp->tm_sec = bcd2bin(sec & 0x7f);
107 tmp->tm_min = bcd2bin(min & 0x7f);
108 tmp->tm_hour = bcd2bin(hour & 0x3f);
109 tmp->tm_mday = bcd2bin(mday & 0x3f);
110 tmp->tm_mon = bcd2bin(mon & 0x1f);
111 tmp->tm_year = bcd2bin(year);
113 if (tmp->tm_year < 70)
114 tmp->tm_year += 2000;
116 tmp->tm_year += 1900;
121 * The mc146818 only updates wday if it is non-zero, sunday is 1
122 * saturday is 7. So let's use our library routine.
124 rtc_calc_weekday(tmp);
126 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
127 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
128 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
134 static int mc146818_set(struct rtc_time *tmp)
137 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
138 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
139 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
141 /* Disable the RTC to update the regs */
142 mc146818_write8(RTC_CONFIG_B, 0x82);
144 mc146818_write8(RTC_YEAR, bin2bcd(tmp->tm_year % 100));
145 mc146818_write8(RTC_MONTH, bin2bcd(tmp->tm_mon));
146 /* Sunday = 1, Saturday = 7 */
147 mc146818_write8(RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday + 1));
148 mc146818_write8(RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
149 mc146818_write8(RTC_HOURS, bin2bcd(tmp->tm_hour));
150 mc146818_write8(RTC_MINUTES, bin2bcd(tmp->tm_min));
151 mc146818_write8(RTC_SECONDS, bin2bcd(tmp->tm_sec));
153 /* Enable the RTC to update the regs */
154 mc146818_write8(RTC_CONFIG_B, 0x02);
159 static void mc146818_reset(void)
161 /* Disable the RTC to update the regs */
162 mc146818_write8(RTC_CONFIG_B, 0x82);
165 mc146818_write8(RTC_CONFIG_A, 0x20);
166 mc146818_write8(RTC_CONFIG_B, 0x00);
167 mc146818_write8(RTC_CONFIG_B, 0x00);
169 /* Enable the RTC to update the regs */
170 mc146818_write8(RTC_CONFIG_B, 0x02);
173 static void mc146818_init(void)
178 rtc_write8(RTC_SECONDS_ALARM, 0);
179 rtc_write8(RTC_MINUTES_ALARM, 0);
180 rtc_write8(RTC_HOURS_ALARM, 0);
181 for (i = RTC_CONFIG_A; i < RTC_REG_SIZE; i++)
183 printf("RTC: zeroing CMOS RAM\n");
186 /* Setup the real time clock */
187 mc146818_write8(RTC_CONFIG_B, RTC_CONFIG_B_24H);
188 /* Setup the frequency it operates at */
189 mc146818_write8(RTC_CONFIG_A, RTC_CONFIG_A_REF_CLCK_32KHZ |
190 RTC_CONFIG_A_RATE_1024HZ);
191 /* Ensure all reserved bits are 0 in register D */
192 mc146818_write8(RTC_CONFIG_D, RTC_CONFIG_D_VALID_RAM_AND_TIME);
194 /* Clear any pending interrupts */
195 mc146818_read8(RTC_CONFIG_C);
200 static int rtc_mc146818_get(struct udevice *dev, struct rtc_time *time)
202 return mc146818_get(time);
205 static int rtc_mc146818_set(struct udevice *dev, const struct rtc_time *time)
207 return mc146818_set((struct rtc_time *)time);
210 static int rtc_mc146818_reset(struct udevice *dev)
217 static int rtc_mc146818_read8(struct udevice *dev, unsigned int reg)
219 return mc146818_read8(reg);
222 static int rtc_mc146818_write8(struct udevice *dev, unsigned int reg, int val)
224 mc146818_write8(reg, val);
229 static int rtc_mc146818_probe(struct udevice *dev)
236 static const struct rtc_ops rtc_mc146818_ops = {
237 .get = rtc_mc146818_get,
238 .set = rtc_mc146818_set,
239 .reset = rtc_mc146818_reset,
240 .read8 = rtc_mc146818_read8,
241 .write8 = rtc_mc146818_write8,
244 static const struct udevice_id rtc_mc146818_ids[] = {
245 { .compatible = "motorola,mc146818" },
249 U_BOOT_DRIVER(rtc_mc146818) = {
250 .name = "rtc_mc146818",
252 .of_match = rtc_mc146818_ids,
253 .probe = rtc_mc146818_probe,
254 .ops = &rtc_mc146818_ops,
257 #else /* !CONFIG_DM_RTC */
259 int rtc_get(struct rtc_time *tmp)
261 return mc146818_get(tmp);
264 int rtc_set(struct rtc_time *tmp)
266 return mc146818_set(tmp);
274 int rtc_read8(int reg)
276 return mc146818_read8(reg);
279 void rtc_write8(int reg, uchar val)
281 mc146818_write8(reg, val);
284 u32 rtc_read32(int reg)
289 for (i = 0; i < sizeof(value); i++)
290 value |= rtc_read8(reg + i) << (i << 3);
295 void rtc_write32(int reg, u32 value)
299 for (i = 0; i < sizeof(value); i++)
300 rtc_write8(reg + i, (value >> (i << 3)) & 0xff);
308 #endif /* CONFIG_DM_RTC */