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>
24 * Support for the RTC device.
26 * We don't want to use the rtc-cmos driver, because we don't want to support
27 * alarms, as that would be indistinguishable from timer interrupts.
29 * Further, generic code is really, really tied to a 1900 epoch. This is
30 * true in __get_rtc_time as well as the users of struct rtc_time e.g.
31 * rtc_tm_to_time. Thankfully all of the other epochs in use are later
32 * than 1900, and so it's easy to adjust.
35 static unsigned long rtc_epoch;
38 specifiy_epoch(char *str)
40 unsigned long epoch = simple_strtoul(str, NULL, 0);
42 printk("Ignoring invalid user specified epoch %lu\n", epoch);
47 __setup("epoch=", specifiy_epoch);
52 int epoch, year, ctrl;
55 /* The epoch was specified on the command-line. */
59 /* Detect the epoch in use on this computer. */
60 ctrl = CMOS_READ(RTC_CONTROL);
61 year = CMOS_READ(RTC_YEAR);
62 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
65 /* PC-like is standard; used for year >= 70 */
69 } else if (year >= 20 && year < 48) {
72 } else if (year >= 48 && year < 70) {
73 /* Digital UNIX epoch */
78 printk(KERN_INFO "Using epoch %d for rtc year %d\n", epoch, year);
82 alpha_rtc_read_time(struct device *dev, struct rtc_time *tm)
86 /* Adjust for non-default epochs. It's easier to depend on the
87 generic __get_rtc_time and adjust the epoch here than create
88 a copy of __get_rtc_time with the edits we need. */
89 if (rtc_epoch != 1900) {
90 int year = tm->tm_year;
91 /* Undo the century adjustment made in __get_rtc_time. */
94 year += rtc_epoch - 1900;
95 /* Redo the century adjustment with the epoch in place. */
101 return rtc_valid_tm(tm);
105 alpha_rtc_set_time(struct device *dev, struct rtc_time *tm)
109 if (rtc_epoch != 1900) {
111 xtm.tm_year -= rtc_epoch - 1900;
115 return __set_rtc_time(tm);
119 alpha_rtc_set_mmss(struct device *dev, unsigned long nowtime)
122 int real_seconds, real_minutes, cmos_minutes;
123 unsigned char save_control, save_freq_select;
125 /* Note: This code only updates minutes and seconds. Comments
126 indicate this was to avoid messing with unknown time zones,
127 and with the epoch nonsense described above. In order for
128 this to work, the existing clock cannot be off by more than
131 ??? This choice is may be out of date. The x86 port does
132 not have problems with timezones, and the epoch processing has
133 now been fixed in alpha_set_rtc_time.
135 In either case, one can always force a full rtc update with
136 the userland hwclock program, so surely 15 minute accuracy
137 is no real burden. */
139 /* In order to set the CMOS clock precisely, we have to be called
140 500 ms after the second nowtime has started, because when
141 nowtime is written into the registers of the CMOS clock, it will
142 jump to the next second precisely 500 ms later. Check the Motorola
143 MC146818A or Dallas DS12887 data sheet for details. */
145 /* irq are locally disabled here */
146 spin_lock(&rtc_lock);
147 /* Tell the clock it's being set */
148 save_control = CMOS_READ(RTC_CONTROL);
149 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
151 /* Stop and reset prescaler */
152 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
153 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
155 cmos_minutes = CMOS_READ(RTC_MINUTES);
156 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
157 cmos_minutes = bcd2bin(cmos_minutes);
159 real_seconds = nowtime % 60;
160 real_minutes = nowtime / 60;
161 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1) {
162 /* correct for half hour time zone */
167 if (abs(real_minutes - cmos_minutes) < 30) {
168 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
169 real_seconds = bin2bcd(real_seconds);
170 real_minutes = bin2bcd(real_minutes);
172 CMOS_WRITE(real_seconds,RTC_SECONDS);
173 CMOS_WRITE(real_minutes,RTC_MINUTES);
175 printk_once(KERN_NOTICE
176 "set_rtc_mmss: can't update from %d to %d\n",
177 cmos_minutes, real_minutes);
181 /* The following flags have to be released exactly in this order,
182 * otherwise the DS12887 (popular MC146818A clone with integrated
183 * battery and quartz) will not reset the oscillator and will not
184 * update precisely 500 ms later. You won't find this mentioned in
185 * the Dallas Semiconductor data sheets, but who believes data
186 * sheets anyway ... -- Markus Kuhn
188 CMOS_WRITE(save_control, RTC_CONTROL);
189 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
190 spin_unlock(&rtc_lock);
196 alpha_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
200 return put_user(rtc_epoch, (unsigned long __user *)arg);
211 static const struct rtc_class_ops alpha_rtc_ops = {
212 .read_time = alpha_rtc_read_time,
213 .set_time = alpha_rtc_set_time,
214 .set_mmss = alpha_rtc_set_mmss,
215 .ioctl = alpha_rtc_ioctl,
219 * Similarly, except do the actual CMOS access on the boot cpu only.
220 * This requires marshalling the data across an interprocessor call.
223 #if defined(CONFIG_SMP) && \
224 (defined(CONFIG_ALPHA_GENERIC) || defined(CONFIG_ALPHA_MARVEL))
225 # define HAVE_REMOTE_RTC 1
234 do_remote_read(void *data)
236 union remote_data *x = data;
237 x->retval = alpha_rtc_read_time(NULL, x->tm);
241 remote_read_time(struct device *dev, struct rtc_time *tm)
244 if (smp_processor_id() != boot_cpuid) {
246 smp_call_function_single(boot_cpuid, do_remote_read, &x, 1);
249 return alpha_rtc_read_time(NULL, tm);
253 do_remote_set(void *data)
255 union remote_data *x = data;
256 x->retval = alpha_rtc_set_time(NULL, x->tm);
260 remote_set_time(struct device *dev, struct rtc_time *tm)
263 if (smp_processor_id() != boot_cpuid) {
265 smp_call_function_single(boot_cpuid, do_remote_set, &x, 1);
268 return alpha_rtc_set_time(NULL, tm);
272 do_remote_mmss(void *data)
274 union remote_data *x = data;
275 x->retval = alpha_rtc_set_mmss(NULL, x->now);
279 remote_set_mmss(struct device *dev, unsigned long now)
282 if (smp_processor_id() != boot_cpuid) {
284 smp_call_function_single(boot_cpuid, do_remote_mmss, &x, 1);
287 return alpha_rtc_set_mmss(NULL, now);
290 static const struct rtc_class_ops remote_rtc_ops = {
291 .read_time = remote_read_time,
292 .set_time = remote_set_time,
293 .set_mmss = remote_set_mmss,
294 .ioctl = alpha_rtc_ioctl,
301 const struct rtc_class_ops *ops;
302 struct platform_device *pdev;
303 struct rtc_device *rtc;
308 ops = &alpha_rtc_ops;
310 #ifdef HAVE_REMOTE_RTC
311 if (alpha_mv.rtc_boot_cpu_only)
312 ops = &remote_rtc_ops;
315 pdev = platform_device_register_simple(name, -1, NULL, 0);
316 rtc = devm_rtc_device_register(&pdev->dev, name, ops, THIS_MODULE);
320 platform_set_drvdata(pdev, rtc);
323 device_initcall(alpha_rtc_init);