1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/alpha/kernel/rtc.c
5 * Copyright (C) 1991, 1992, 1995, 1999, 2000 Linus Torvalds
7 * This file contains date handling.
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>
23 * Support for the RTC device.
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.
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.
34 static unsigned long rtc_epoch;
37 specifiy_epoch(char *str)
39 unsigned long epoch = simple_strtoul(str, NULL, 0);
41 printk("Ignoring invalid user specified epoch %lu\n", epoch);
46 __setup("epoch=", specifiy_epoch);
51 int epoch, year, ctrl;
54 /* The epoch was specified on the command-line. */
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)
64 /* PC-like is standard; used for year >= 70 */
68 } else if (year >= 20 && year < 48) {
71 } else if (year >= 48 && year < 70) {
72 /* Digital UNIX epoch */
77 printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year);
81 alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
83 int ret = mc146818_get_time(tm);
86 dev_err_ratelimited(dev, "unable to read current time\n");
90 /* Adjust for non-default epochs. It's easier to depend on the
91 generic __get_rtc_time and adjust the epoch here than create
92 a copy of __get_rtc_time with the edits we need. */
93 if (rtc_epoch != 1900) {
94 int year = tm->tm_year;
95 /* Undo the century adjustment made in __get_rtc_time. */
98 year += rtc_epoch - 1900;
99 /* Redo the century adjustment with the epoch in place. */
109 alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
113 if (rtc_epoch != 1900) {
115 xtm.tm_year -= rtc_epoch - 1900;
119 return mc146818_set_time(tm);
123 alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
127 return put_user(rtc_epoch, (unsigned long __user *)arg);
138 static const struct rtc_class_ops alpha_rtc_ops = {
139 .read_time = alpha_rtc_read_time,
140 .set_time = alpha_rtc_set_time,
141 .ioctl = alpha_rtc_ioctl,
145 * Similarly, except do the actual CMOS access on the boot cpu only.
146 * This requires marshalling the data across an interprocessor call.
149 #if defined(CONFIG_SMP) && \
150 (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
151 # define HAVE_REMOTE_RTC 1
159 do_remote_read(void *data)
161 union remote_data *x = data;
162 x->retval = alpha_rtc_read_time(NULL, x->tm);
166 remote_read_time(struct device *dev, struct rtc_time *tm)
169 if (smp_processor_id() != boot_cpuid) {
171 smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
174 return alpha_rtc_read_time(NULL, tm);
178 do_remote_set(void *data)
180 union remote_data *x = data;
181 x->retval = alpha_rtc_set_time(NULL, x->tm);
185 remote_set_time(struct device *dev, struct rtc_time *tm)
188 if (smp_processor_id() != boot_cpuid) {
190 smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
193 return alpha_rtc_set_time(NULL, tm);
196 static const struct rtc_class_ops remote_rtc_ops = {
197 .read_time = remote_read_time,
198 .set_time = remote_set_time,
199 .ioctl = alpha_rtc_ioctl,
206 struct platform_device *pdev;
207 struct rtc_device *rtc;
211 pdev = platform_device_register_simple("rtc-alpha", -1, NULL, 0);
212 rtc = devm_rtc_allocate_device(&pdev->dev);
216 platform_set_drvdata(pdev, rtc);
217 rtc->ops = &alpha_rtc_ops;
219 #ifdef HAVE_REMOTE_RTC
220 if (alpha_mv.rtc_boot_cpu_only)
221 rtc->ops = &remote_rtc_ops;
224 return devm_rtc_register_device(rtc);
226 device_initcall(alpha_rtc_init);