1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
4  *                     IBM Corp.
5  */
6 
7 #undef DEBUG
8 
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>
14 #include <linux/mm.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 
23 #include <asm/sections.h>
24 #include <asm/prom.h>
25 #include <asm/io.h>
26 #include <asm/pgtable.h>
27 #include <asm/machdep.h>
28 #include <asm/time.h>
29 
30 #include "maple.h"
31 
32 #ifdef DEBUG
33 #define DBG(x...) printk(x)
34 #else
35 #define DBG(x...)
36 #endif
37 
38 static int maple_rtc_addr;
39 
40 static int maple_clock_read(int addr)
41 {
42 	outb_p(addr, maple_rtc_addr);
43 	return inb_p(maple_rtc_addr+1);
44 }
45 
46 static void maple_clock_write(unsigned long val, int addr)
47 {
48 	outb_p(addr, maple_rtc_addr);
49 	outb_p(val, maple_rtc_addr+1);
50 }
51 
52 void maple_get_rtc_time(struct rtc_time *tm)
53 {
54 	do {
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));
62 
63 	if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
64 	    || RTC_ALWAYS_BCD) {
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);
71 	  }
72 	if ((tm->tm_year + 1900) < 1970)
73 		tm->tm_year += 100;
74 
75 	tm->tm_wday = -1;
76 }
77 
78 int maple_set_rtc_time(struct rtc_time *tm)
79 {
80 	unsigned char save_control, save_freq_select;
81 	int sec, min, hour, mon, mday, year;
82 
83 	spin_lock(&rtc_lock);
84 
85 	save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
86 
87 	maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
88 
89 	save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
90 
91 	maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
92 
93 	sec = tm->tm_sec;
94 	min = tm->tm_min;
95 	hour = tm->tm_hour;
96 	mon = tm->tm_mon;
97 	mday = tm->tm_mday;
98 	year = tm->tm_year;
99 
100 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
101 		sec = bin2bcd(sec);
102 		min = bin2bcd(min);
103 		hour = bin2bcd(hour);
104 		mon = bin2bcd(mon);
105 		mday = bin2bcd(mday);
106 		year = bin2bcd(year);
107 	}
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);
114 
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
121 	 */
122 	maple_clock_write(save_control, RTC_CONTROL);
123 	maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
124 
125 	spin_unlock(&rtc_lock);
126 
127 	return 0;
128 }
129 
130 static struct resource rtc_iores = {
131 	.name = "rtc",
132 	.flags = IORESOURCE_IO | IORESOURCE_BUSY,
133 };
134 
135 time64_t __init maple_get_boot_time(void)
136 {
137 	struct rtc_time tm;
138 	struct device_node *rtcs;
139 
140 	rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
141 	if (rtcs) {
142 		struct resource r;
143 		if (of_address_to_resource(rtcs, 0, &r)) {
144 			printk(KERN_EMERG "Maple: Unable to translate RTC"
145 			       " address\n");
146 			goto bail;
147 		}
148 		if (!(r.flags & IORESOURCE_IO)) {
149 			printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
150 			goto bail;
151 		}
152 		maple_rtc_addr = r.start;
153 		printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
154 		       maple_rtc_addr);
155 	}
156  bail:
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);
161 	}
162 
163 	rtc_iores.start = maple_rtc_addr;
164 	rtc_iores.end = maple_rtc_addr + 7;
165 	request_resource(&ioport_resource, &rtc_iores);
166 
167 	maple_get_rtc_time(&tm);
168 	return rtc_tm_to_time64(&tm);
169 }
170 
171