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