1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/param.h>
13 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/time.h>
17 #include <linux/adb.h>
18 #include <linux/pmu.h>
19 #include <linux/interrupt.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/of_address.h>
24 #include <asm/sections.h>
26 #include <asm/machdep.h>
32 #define DBG(x...) printk(x)
37 static int maple_rtc_addr;
39 static int maple_clock_read(int addr)
41 outb_p(addr, maple_rtc_addr);
42 return inb_p(maple_rtc_addr+1);
45 static void maple_clock_write(unsigned long val, int addr)
47 outb_p(addr, maple_rtc_addr);
48 outb_p(val, maple_rtc_addr+1);
51 void maple_get_rtc_time(struct rtc_time *tm)
54 tm->tm_sec = maple_clock_read(RTC_SECONDS);
55 tm->tm_min = maple_clock_read(RTC_MINUTES);
56 tm->tm_hour = maple_clock_read(RTC_HOURS);
57 tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
58 tm->tm_mon = maple_clock_read(RTC_MONTH);
59 tm->tm_year = maple_clock_read(RTC_YEAR);
60 } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
62 if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
64 tm->tm_sec = bcd2bin(tm->tm_sec);
65 tm->tm_min = bcd2bin(tm->tm_min);
66 tm->tm_hour = bcd2bin(tm->tm_hour);
67 tm->tm_mday = bcd2bin(tm->tm_mday);
68 tm->tm_mon = bcd2bin(tm->tm_mon);
69 tm->tm_year = bcd2bin(tm->tm_year);
71 if ((tm->tm_year + 1900) < 1970)
77 int maple_set_rtc_time(struct rtc_time *tm)
79 unsigned char save_control, save_freq_select;
80 int sec, min, hour, mon, mday, year;
84 save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
86 maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
88 save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
90 maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
99 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
102 hour = bin2bcd(hour);
104 mday = bin2bcd(mday);
105 year = bin2bcd(year);
107 maple_clock_write(sec, RTC_SECONDS);
108 maple_clock_write(min, RTC_MINUTES);
109 maple_clock_write(hour, RTC_HOURS);
110 maple_clock_write(mon, RTC_MONTH);
111 maple_clock_write(mday, RTC_DAY_OF_MONTH);
112 maple_clock_write(year, RTC_YEAR);
114 /* The following flags have to be released exactly in this order,
115 * otherwise the DS12887 (popular MC146818A clone with integrated
116 * battery and quartz) will not reset the oscillator and will not
117 * update precisely 500 ms later. You won't find this mentioned in
118 * the Dallas Semiconductor data sheets, but who believes data
119 * sheets anyway ... -- Markus Kuhn
121 maple_clock_write(save_control, RTC_CONTROL);
122 maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
124 spin_unlock(&rtc_lock);
129 static struct resource rtc_iores = {
131 .flags = IORESOURCE_IO | IORESOURCE_BUSY,
134 time64_t __init maple_get_boot_time(void)
137 struct device_node *rtcs;
139 rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
142 if (of_address_to_resource(rtcs, 0, &r)) {
143 printk(KERN_EMERG "Maple: Unable to translate RTC"
147 if (!(r.flags & IORESOURCE_IO)) {
148 printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
151 maple_rtc_addr = r.start;
152 printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
157 if (maple_rtc_addr == 0) {
158 maple_rtc_addr = RTC_PORT(0); /* legacy address */
159 printk(KERN_INFO "Maple: No device node for RTC, assuming "
160 "legacy address (0x%x)\n", maple_rtc_addr);
163 rtc_iores.start = maple_rtc_addr;
164 rtc_iores.end = maple_rtc_addr + 7;
165 request_resource(&ioport_resource, &rtc_iores);
167 maple_get_rtc_time(&tm);
168 return rtc_tm_to_time64(&tm);