Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / arch / alpha / kernel / rtc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/arch/alpha/kernel/rtc.c
4  *
5  *  Copyright (C) 1991, 1992, 1995, 1999, 2000  Linus Torvalds
6  *
7  * This file contains date handling.
8  */
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/param.h>
13 #include <linux/string.h>
14 #include <linux/mc146818rtc.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/platform_device.h>
18
19 #include "proto.h"
20
21
22 /*
23  * Support for the RTC device.
24  *
25  * We don't want to use the rtc-cmos driver, because we don't want to support
26  * alarms, as that would be indistinguishable from timer interrupts.
27  *
28  * Further, generic code is really, really tied to a 1900 epoch.  This is
29  * true in __get_rtc_time as well as the users of struct rtc_time e.g.
30  * rtc_tm_to_time.  Thankfully all of the other epochs in use are later
31  * than 1900, and so it's easy to adjust.
32  */
33
34 static unsigned long rtc_epoch;
35
36 static int __init
37 specifiy_epoch(char *str)
38 {
39         unsigned long epoch = simple_strtoul(str, NULL, 0);
40         if (epoch < 1900)
41                 printk("Ignoring invalid user specified epoch %lu\n", epoch);
42         else
43                 rtc_epoch = epoch;
44         return 1;
45 }
46 __setup("epoch=", specifiy_epoch);
47
48 static void __init
49 init_rtc_epoch(void)
50 {
51         int epoch, year, ctrl;
52
53         if (rtc_epoch != 0) {
54                 /* The epoch was specified on the command-line.  */
55                 return;
56         }
57
58         /* Detect the epoch in use on this computer.  */
59         ctrl = CMOS_READ(RTC_CONTROL);
60         year = CMOS_READ(RTC_YEAR);
61         if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
62                 year = bcd2bin(year);
63
64         /* PC-like is standard; used for year >= 70 */
65         epoch = 1900;
66         if (year < 20) {
67                 epoch = 2000;
68         } else if (year >= 20 && year < 48) {
69                 /* NT epoch */
70                 epoch = 1980;
71         } else if (year >= 48 && year < 70) {
72                 /* Digital UNIX epoch */
73                 epoch = 1952;
74         }
75         rtc_epoch = epoch;
76
77         printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year);
78 }
79
80 static int
81 alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
82 {
83         mc146818_get_time(tm);
84
85         /* Adjust for non-default epochs.  It's easier to depend on the
86            generic __get_rtc_time and adjust the epoch here than create
87            a copy of __get_rtc_time with the edits we need.  */
88         if (rtc_epoch != 1900) {
89                 int year = tm->tm_year;
90                 /* Undo the century adjustment made in __get_rtc_time.  */
91                 if (year >= 100)
92                         year -= 100;
93                 year += rtc_epoch - 1900;
94                 /* Redo the century adjustment with the epoch in place.  */
95                 if (year <= 69)
96                         year += 100;
97                 tm->tm_year = year;
98         }
99
100         return rtc_valid_tm(tm);
101 }
102
103 static int
104 alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
105 {
106         struct rtc_time xtm;
107
108         if (rtc_epoch != 1900) {
109                 xtm = *tm;
110                 xtm.tm_year -= rtc_epoch - 1900;
111                 tm = &xtm;
112         }
113
114         return mc146818_set_time(tm);
115 }
116
117 static int
118 alpha_rtc_set_mmss(struct device *dev, time64_t nowtime)
119 {
120         int retval = 0;
121         int real_seconds, real_minutes, cmos_minutes;
122         unsigned char save_control, save_freq_select;
123
124         /* Note: This code only updates minutes and seconds.  Comments
125            indicate this was to avoid messing with unknown time zones,
126            and with the epoch nonsense described above.  In order for
127            this to work, the existing clock cannot be off by more than
128            15 minutes.
129
130            ??? This choice is may be out of date.  The x86 port does
131            not have problems with timezones, and the epoch processing has
132            now been fixed in alpha_set_rtc_time.
133
134            In either case, one can always force a full rtc update with
135            the userland hwclock program, so surely 15 minute accuracy
136            is no real burden.  */
137
138         /* In order to set the CMOS clock precisely, we have to be called
139            500 ms after the second nowtime has started, because when
140            nowtime is written into the registers of the CMOS clock, it will
141            jump to the next second precisely 500 ms later. Check the Motorola
142            MC146818A or Dallas DS12887 data sheet for details.  */
143
144         /* irq are locally disabled here */
145         spin_lock(&rtc_lock);
146         /* Tell the clock it's being set */
147         save_control = CMOS_READ(RTC_CONTROL);
148         CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
149
150         /* Stop and reset prescaler */
151         save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
152         CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
153
154         cmos_minutes = CMOS_READ(RTC_MINUTES);
155         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
156                 cmos_minutes = bcd2bin(cmos_minutes);
157
158         real_seconds = nowtime % 60;
159         real_minutes = nowtime / 60;
160         if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) {
161                 /* correct for half hour time zone */
162                 real_minutes += 30;
163         }
164         real_minutes %= 60;
165
166         if (abs(real_minutes - cmos_minutes) < 30) {
167                 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
168                         real_seconds = bin2bcd(real_seconds);
169                         real_minutes = bin2bcd(real_minutes);
170                 }
171                 CMOS_WRITE(real_seconds,RTC_SECONDS);
172                 CMOS_WRITE(real_minutes,RTC_MINUTES);
173         } else {
174                 printk_once(KERN_NOTICE
175                             "set_rtc_mmss: can't update from %d to %d\n",
176                             cmos_minutes, real_minutes);
177                 retval = -1;
178         }
179
180         /* The following flags have to be released exactly in this order,
181          * otherwise the DS12887 (popular MC146818A clone with integrated
182          * battery and quartz) will not reset the oscillator and will not
183          * update precisely 500 ms later. You won't find this mentioned in
184          * the Dallas Semiconductor data sheets, but who believes data
185          * sheets anyway ...                           -- Markus Kuhn
186          */
187         CMOS_WRITE(save_control, RTC_CONTROL);
188         CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
189         spin_unlock(&rtc_lock);
190
191         return retval;
192 }
193
194 static int
195 alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
196 {
197         switch (cmd) {
198         case RTC_EPOCH_READ:
199                 return put_user(rtc_epoch, (unsigned long __user *)arg);
200         case RTC_EPOCH_SET:
201                 if (arg < 1900)
202                         return -EINVAL;
203                 rtc_epoch = arg;
204                 return 0;
205         default:
206                 return -ENOIOCTLCMD;
207         }
208 }
209
210 static const struct rtc_class_ops alpha_rtc_ops = {
211         .read_time = alpha_rtc_read_time,
212         .set_time = alpha_rtc_set_time,
213         .set_mmss64 = alpha_rtc_set_mmss,
214         .ioctl = alpha_rtc_ioctl,
215 };
216
217 /*
218  * Similarly, except do the actual CMOS access on the boot cpu only.
219  * This requires marshalling the data across an interprocessor call.
220  */
221
222 #if defined(CONFIG_SMP) && \
223     (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
224 # define HAVE_REMOTE_RTC 1
225
226 union remote_data {
227         struct rtc_time *tm;
228         unsigned long now;
229         long retval;
230 };
231
232 static void
233 do_remote_read(void *data)
234 {
235         union remote_data *x = data;
236         x->retval = alpha_rtc_read_time(NULL, x->tm);
237 }
238
239 static int
240 remote_read_time(struct device *dev, struct rtc_time *tm)
241 {
242         union remote_data x;
243         if (smp_processor_id() != boot_cpuid) {
244                 x.tm = tm;
245                 smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
246                 return x.retval;
247         }
248         return alpha_rtc_read_time(NULL, tm);
249 }
250
251 static void
252 do_remote_set(void *data)
253 {
254         union remote_data *x = data;
255         x->retval = alpha_rtc_set_time(NULL, x->tm);
256 }
257
258 static int
259 remote_set_time(struct device *dev, struct rtc_time *tm)
260 {
261         union remote_data x;
262         if (smp_processor_id() != boot_cpuid) {
263                 x.tm = tm;
264                 smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
265                 return x.retval;
266         }
267         return alpha_rtc_set_time(NULL, tm);
268 }
269
270 static void
271 do_remote_mmss(void *data)
272 {
273         union remote_data *x = data;
274         x->retval = alpha_rtc_set_mmss(NULL, x->now);
275 }
276
277 static int
278 remote_set_mmss(struct device *dev, time64_t now)
279 {
280         union remote_data x;
281         if (smp_processor_id() != boot_cpuid) {
282                 x.now = now;
283                 smp_call_function_single(boot_cpuid, do_remote_mmss, &x, 1);
284                 return x.retval;
285         }
286         return alpha_rtc_set_mmss(NULL, now);
287 }
288
289 static const struct rtc_class_ops remote_rtc_ops = {
290         .read_time = remote_read_time,
291         .set_time = remote_set_time,
292         .set_mmss64 = remote_set_mmss,
293         .ioctl = alpha_rtc_ioctl,
294 };
295 #endif
296
297 static int __init
298 alpha_rtc_init(void)
299 {
300         const struct rtc_class_ops *ops;
301         struct platform_device *pdev;
302         struct rtc_device *rtc;
303         const char *name;
304
305         init_rtc_epoch();
306         name = "rtc-alpha";
307         ops = &alpha_rtc_ops;
308
309 #ifdef HAVE_REMOTE_RTC
310         if (alpha_mv.rtc_boot_cpu_only)
311                 ops = &remote_rtc_ops;
312 #endif
313
314         pdev = platform_device_register_simple(name, -1, NULL, 0);
315         rtc = devm_rtc_device_register(&pdev->dev, name, ops, THIS_MODULE);
316         if (IS_ERR(rtc))
317                 return PTR_ERR(rtc);
318
319         platform_set_drvdata(pdev, rtc);
320         return 0;
321 }
322 device_initcall(alpha_rtc_init);