2 * linux/arch/alpha/kernel/rtc.c
4 * Copyright (C) 1991, 1992, 1995, 1999, 2000 Linus Torvalds
6 * This file contains date handling.
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/param.h>
12 #include <linux/string.h>
13 #include <linux/mc146818rtc.h>
14 #include <linux/bcd.h>
15 #include <linux/rtc.h>
16 #include <linux/platform_device.h>
22 * Support for the RTC device.
24 * We don't want to use the rtc-cmos driver, because we don't want to support
25 * alarms, as that would be indistinguishable from timer interrupts.
27 * Further, generic code is really, really tied to a 1900 epoch. This is
28 * true in __get_rtc_time as well as the users of struct rtc_time e.g.
29 * rtc_tm_to_time. Thankfully all of the other epochs in use are later
30 * than 1900, and so it's easy to adjust.
33 static unsigned long rtc_epoch;
36 specifiy_epoch(char *str)
38 unsigned long epoch = simple_strtoul(str, NULL, 0);
40 printk("Ignoring invalid user specified epoch %lu\n", epoch);
45 __setup("epoch=", specifiy_epoch);
50 int epoch, year, ctrl;
53 /* The epoch was specified on the command-line. */
57 /* Detect the epoch in use on this computer. */
58 ctrl = CMOS_READ(RTC_CONTROL);
59 year = CMOS_READ(RTC_YEAR);
60 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
63 /* PC-like is standard; used for year >= 70 */
67 } else if (year >= 20 && year < 48) {
70 } else if (year >= 48 && year < 70) {
71 /* Digital UNIX epoch */
76 printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year);
80 alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
82 mc146818_get_time(tm);
84 /* Adjust for non-default epochs. It's easier to depend on the
85 generic __get_rtc_time and adjust the epoch here than create
86 a copy of __get_rtc_time with the edits we need. */
87 if (rtc_epoch != 1900) {
88 int year = tm->tm_year;
89 /* Undo the century adjustment made in __get_rtc_time. */
92 year += rtc_epoch - 1900;
93 /* Redo the century adjustment with the epoch in place. */
99 return rtc_valid_tm(tm);
103 alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
107 if (rtc_epoch != 1900) {
109 xtm.tm_year -= rtc_epoch - 1900;
113 return mc146818_set_time(tm);
117 alpha_rtc_set_mmss(struct device *dev, time64_t nowtime)
120 int real_seconds, real_minutes, cmos_minutes;
121 unsigned char save_control, save_freq_select;
123 /* Note: This code only updates minutes and seconds. Comments
124 indicate this was to avoid messing with unknown time zones,
125 and with the epoch nonsense described above. In order for
126 this to work, the existing clock cannot be off by more than
129 ??? This choice is may be out of date. The x86 port does
130 not have problems with timezones, and the epoch processing has
131 now been fixed in alpha_set_rtc_time.
133 In either case, one can always force a full rtc update with
134 the userland hwclock program, so surely 15 minute accuracy
135 is no real burden. */
137 /* In order to set the CMOS clock precisely, we have to be called
138 500 ms after the second nowtime has started, because when
139 nowtime is written into the registers of the CMOS clock, it will
140 jump to the next second precisely 500 ms later. Check the Motorola
141 MC146818A or Dallas DS12887 data sheet for details. */
143 /* irq are locally disabled here */
144 spin_lock(&rtc_lock);
145 /* Tell the clock it's being set */
146 save_control = CMOS_READ(RTC_CONTROL);
147 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
149 /* Stop and reset prescaler */
150 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
151 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
153 cmos_minutes = CMOS_READ(RTC_MINUTES);
154 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
155 cmos_minutes = bcd2bin(cmos_minutes);
157 real_seconds = nowtime % 60;
158 real_minutes = nowtime / 60;
159 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) {
160 /* correct for half hour time zone */
165 if (abs(real_minutes - cmos_minutes) < 30) {
166 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
167 real_seconds = bin2bcd(real_seconds);
168 real_minutes = bin2bcd(real_minutes);
170 CMOS_WRITE(real_seconds,RTC_SECONDS);
171 CMOS_WRITE(real_minutes,RTC_MINUTES);
173 printk_once(KERN_NOTICE
174 "set_rtc_mmss: can't update from %d to %d\n",
175 cmos_minutes, real_minutes);
179 /* The following flags have to be released exactly in this order,
180 * otherwise the DS12887 (popular MC146818A clone with integrated
181 * battery and quartz) will not reset the oscillator and will not
182 * update precisely 500 ms later. You won't find this mentioned in
183 * the Dallas Semiconductor data sheets, but who believes data
184 * sheets anyway ... -- Markus Kuhn
186 CMOS_WRITE(save_control, RTC_CONTROL);
187 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
188 spin_unlock(&rtc_lock);
194 alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
198 return put_user(rtc_epoch, (unsigned long __user *)arg);
209 static const struct rtc_class_ops alpha_rtc_ops = {
210 .read_time = alpha_rtc_read_time,
211 .set_time = alpha_rtc_set_time,
212 .set_mmss64 = alpha_rtc_set_mmss,
213 .ioctl = alpha_rtc_ioctl,
217 * Similarly, except do the actual CMOS access on the boot cpu only.
218 * This requires marshalling the data across an interprocessor call.
221 #if defined(CONFIG_SMP) && \
222 (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
223 # define HAVE_REMOTE_RTC 1
232 do_remote_read(void *data)
234 union remote_data *x = data;
235 x->retval = alpha_rtc_read_time(NULL, x->tm);
239 remote_read_time(struct device *dev, struct rtc_time *tm)
242 if (smp_processor_id() != boot_cpuid) {
244 smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
247 return alpha_rtc_read_time(NULL, tm);
251 do_remote_set(void *data)
253 union remote_data *x = data;
254 x->retval = alpha_rtc_set_time(NULL, x->tm);
258 remote_set_time(struct device *dev, struct rtc_time *tm)
261 if (smp_processor_id() != boot_cpuid) {
263 smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
266 return alpha_rtc_set_time(NULL, tm);
270 do_remote_mmss(void *data)
272 union remote_data *x = data;
273 x->retval = alpha_rtc_set_mmss(NULL, x->now);
277 remote_set_mmss(struct device *dev, time64_t now)
280 if (smp_processor_id() != boot_cpuid) {
282 smp_call_function_single(boot_cpuid, do_remote_mmss, &x, 1);
285 return alpha_rtc_set_mmss(NULL, now);
288 static const struct rtc_class_ops remote_rtc_ops = {
289 .read_time = remote_read_time,
290 .set_time = remote_set_time,
291 .set_mmss64 = remote_set_mmss,
292 .ioctl = alpha_rtc_ioctl,
299 const struct rtc_class_ops *ops;
300 struct platform_device *pdev;
301 struct rtc_device *rtc;
306 ops = &alpha_rtc_ops;
308 #ifdef HAVE_REMOTE_RTC
309 if (alpha_mv.rtc_boot_cpu_only)
310 ops = &remote_rtc_ops;
313 pdev = platform_device_register_simple(name, -1, NULL, 0);
314 rtc = devm_rtc_device_register(&pdev->dev, name, ops, THIS_MODULE);
318 platform_set_drvdata(pdev, rtc);
321 device_initcall(alpha_rtc_init);