1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
4*4882a593Smuzhiyun * IBM Corp.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #undef DEBUG
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/errno.h>
10*4882a593Smuzhiyun #include <linux/sched.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/param.h>
13*4882a593Smuzhiyun #include <linux/string.h>
14*4882a593Smuzhiyun #include <linux/mm.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/time.h>
17*4882a593Smuzhiyun #include <linux/adb.h>
18*4882a593Smuzhiyun #include <linux/pmu.h>
19*4882a593Smuzhiyun #include <linux/interrupt.h>
20*4882a593Smuzhiyun #include <linux/mc146818rtc.h>
21*4882a593Smuzhiyun #include <linux/bcd.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <asm/sections.h>
24*4882a593Smuzhiyun #include <asm/prom.h>
25*4882a593Smuzhiyun #include <asm/io.h>
26*4882a593Smuzhiyun #include <asm/machdep.h>
27*4882a593Smuzhiyun #include <asm/time.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "maple.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifdef DEBUG
32*4882a593Smuzhiyun #define DBG(x...) printk(x)
33*4882a593Smuzhiyun #else
34*4882a593Smuzhiyun #define DBG(x...)
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun static int maple_rtc_addr;
38*4882a593Smuzhiyun
maple_clock_read(int addr)39*4882a593Smuzhiyun static int maple_clock_read(int addr)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun outb_p(addr, maple_rtc_addr);
42*4882a593Smuzhiyun return inb_p(maple_rtc_addr+1);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
maple_clock_write(unsigned long val,int addr)45*4882a593Smuzhiyun static void maple_clock_write(unsigned long val, int addr)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun outb_p(addr, maple_rtc_addr);
48*4882a593Smuzhiyun outb_p(val, maple_rtc_addr+1);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
maple_get_rtc_time(struct rtc_time * tm)51*4882a593Smuzhiyun void maple_get_rtc_time(struct rtc_time *tm)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun do {
54*4882a593Smuzhiyun tm->tm_sec = maple_clock_read(RTC_SECONDS);
55*4882a593Smuzhiyun tm->tm_min = maple_clock_read(RTC_MINUTES);
56*4882a593Smuzhiyun tm->tm_hour = maple_clock_read(RTC_HOURS);
57*4882a593Smuzhiyun tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
58*4882a593Smuzhiyun tm->tm_mon = maple_clock_read(RTC_MONTH);
59*4882a593Smuzhiyun tm->tm_year = maple_clock_read(RTC_YEAR);
60*4882a593Smuzhiyun } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
63*4882a593Smuzhiyun || RTC_ALWAYS_BCD) {
64*4882a593Smuzhiyun tm->tm_sec = bcd2bin(tm->tm_sec);
65*4882a593Smuzhiyun tm->tm_min = bcd2bin(tm->tm_min);
66*4882a593Smuzhiyun tm->tm_hour = bcd2bin(tm->tm_hour);
67*4882a593Smuzhiyun tm->tm_mday = bcd2bin(tm->tm_mday);
68*4882a593Smuzhiyun tm->tm_mon = bcd2bin(tm->tm_mon);
69*4882a593Smuzhiyun tm->tm_year = bcd2bin(tm->tm_year);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun if ((tm->tm_year + 1900) < 1970)
72*4882a593Smuzhiyun tm->tm_year += 100;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun tm->tm_wday = -1;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
maple_set_rtc_time(struct rtc_time * tm)77*4882a593Smuzhiyun int maple_set_rtc_time(struct rtc_time *tm)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun unsigned char save_control, save_freq_select;
80*4882a593Smuzhiyun int sec, min, hour, mon, mday, year;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun spin_lock(&rtc_lock);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun sec = tm->tm_sec;
93*4882a593Smuzhiyun min = tm->tm_min;
94*4882a593Smuzhiyun hour = tm->tm_hour;
95*4882a593Smuzhiyun mon = tm->tm_mon;
96*4882a593Smuzhiyun mday = tm->tm_mday;
97*4882a593Smuzhiyun year = tm->tm_year;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
100*4882a593Smuzhiyun sec = bin2bcd(sec);
101*4882a593Smuzhiyun min = bin2bcd(min);
102*4882a593Smuzhiyun hour = bin2bcd(hour);
103*4882a593Smuzhiyun mon = bin2bcd(mon);
104*4882a593Smuzhiyun mday = bin2bcd(mday);
105*4882a593Smuzhiyun year = bin2bcd(year);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun maple_clock_write(sec, RTC_SECONDS);
108*4882a593Smuzhiyun maple_clock_write(min, RTC_MINUTES);
109*4882a593Smuzhiyun maple_clock_write(hour, RTC_HOURS);
110*4882a593Smuzhiyun maple_clock_write(mon, RTC_MONTH);
111*4882a593Smuzhiyun maple_clock_write(mday, RTC_DAY_OF_MONTH);
112*4882a593Smuzhiyun maple_clock_write(year, RTC_YEAR);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* The following flags have to be released exactly in this order,
115*4882a593Smuzhiyun * otherwise the DS12887 (popular MC146818A clone with integrated
116*4882a593Smuzhiyun * battery and quartz) will not reset the oscillator and will not
117*4882a593Smuzhiyun * update precisely 500 ms later. You won't find this mentioned in
118*4882a593Smuzhiyun * the Dallas Semiconductor data sheets, but who believes data
119*4882a593Smuzhiyun * sheets anyway ... -- Markus Kuhn
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun maple_clock_write(save_control, RTC_CONTROL);
122*4882a593Smuzhiyun maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun spin_unlock(&rtc_lock);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun return 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun static struct resource rtc_iores = {
130*4882a593Smuzhiyun .name = "rtc",
131*4882a593Smuzhiyun .flags = IORESOURCE_IO | IORESOURCE_BUSY,
132*4882a593Smuzhiyun };
133*4882a593Smuzhiyun
maple_get_boot_time(void)134*4882a593Smuzhiyun time64_t __init maple_get_boot_time(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun struct rtc_time tm;
137*4882a593Smuzhiyun struct device_node *rtcs;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
140*4882a593Smuzhiyun if (rtcs) {
141*4882a593Smuzhiyun struct resource r;
142*4882a593Smuzhiyun if (of_address_to_resource(rtcs, 0, &r)) {
143*4882a593Smuzhiyun printk(KERN_EMERG "Maple: Unable to translate RTC"
144*4882a593Smuzhiyun " address\n");
145*4882a593Smuzhiyun goto bail;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun if (!(r.flags & IORESOURCE_IO)) {
148*4882a593Smuzhiyun printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
149*4882a593Smuzhiyun goto bail;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun maple_rtc_addr = r.start;
152*4882a593Smuzhiyun printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
153*4882a593Smuzhiyun maple_rtc_addr);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun bail:
156*4882a593Smuzhiyun if (maple_rtc_addr == 0) {
157*4882a593Smuzhiyun maple_rtc_addr = RTC_PORT(0); /* legacy address */
158*4882a593Smuzhiyun printk(KERN_INFO "Maple: No device node for RTC, assuming "
159*4882a593Smuzhiyun "legacy address (0x%x)\n", maple_rtc_addr);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun rtc_iores.start = maple_rtc_addr;
163*4882a593Smuzhiyun rtc_iores.end = maple_rtc_addr + 7;
164*4882a593Smuzhiyun request_resource(&ioport_resource, &rtc_iores);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun maple_get_rtc_time(&tm);
167*4882a593Smuzhiyun return rtc_tm_to_time64(&tm);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170