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>
23 #include <asm/sections.h>
26 #include <asm/pgtable.h>
27 #include <asm/machdep.h>
33 #define DBG(x...) printk(x)
38 static int maple_rtc_addr;
40 static int maple_clock_read(int addr)
42 outb_p(addr, maple_rtc_addr);
43 return inb_p(maple_rtc_addr+1);
46 static void maple_clock_write(unsigned long val, int addr)
48 outb_p(addr, maple_rtc_addr);
49 outb_p(val, maple_rtc_addr+1);
52 void maple_get_rtc_time(struct rtc_time *tm)
55 tm->tm_sec = maple_clock_read(RTC_SECONDS);
56 tm->tm_min = maple_clock_read(RTC_MINUTES);
57 tm->tm_hour = maple_clock_read(RTC_HOURS);
58 tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
59 tm->tm_mon = maple_clock_read(RTC_MONTH);
60 tm->tm_year = maple_clock_read(RTC_YEAR);
61 } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
63 if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
65 tm->tm_sec = bcd2bin(tm->tm_sec);
66 tm->tm_min = bcd2bin(tm->tm_min);
67 tm->tm_hour = bcd2bin(tm->tm_hour);
68 tm->tm_mday = bcd2bin(tm->tm_mday);
69 tm->tm_mon = bcd2bin(tm->tm_mon);
70 tm->tm_year = bcd2bin(tm->tm_year);
72 if ((tm->tm_year + 1900) < 1970)
78 int maple_set_rtc_time(struct rtc_time *tm)
80 unsigned char save_control, save_freq_select;
81 int sec, min, hour, mon, mday, year;
85 save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
87 maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
89 save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
91 maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
100 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
103 hour = bin2bcd(hour);
105 mday = bin2bcd(mday);
106 year = bin2bcd(year);
108 maple_clock_write(sec, RTC_SECONDS);
109 maple_clock_write(min, RTC_MINUTES);
110 maple_clock_write(hour, RTC_HOURS);
111 maple_clock_write(mon, RTC_MONTH);
112 maple_clock_write(mday, RTC_DAY_OF_MONTH);
113 maple_clock_write(year, RTC_YEAR);
115 /* The following flags have to be released exactly in this order,
116 * otherwise the DS12887 (popular MC146818A clone with integrated
117 * battery and quartz) will not reset the oscillator and will not
118 * update precisely 500 ms later. You won't find this mentioned in
119 * the Dallas Semiconductor data sheets, but who believes data
120 * sheets anyway ... -- Markus Kuhn
122 maple_clock_write(save_control, RTC_CONTROL);
123 maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
125 spin_unlock(&rtc_lock);
130 static struct resource rtc_iores = {
132 .flags = IORESOURCE_IO | IORESOURCE_BUSY,
135 time64_t __init maple_get_boot_time(void)
138 struct device_node *rtcs;
140 rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
143 if (of_address_to_resource(rtcs, 0, &r)) {
144 printk(KERN_EMERG "Maple: Unable to translate RTC"
148 if (!(r.flags & IORESOURCE_IO)) {
149 printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
152 maple_rtc_addr = r.start;
153 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);