2 * rtc-mrst.c: Driver for Moorestown virtual RTC
4 * (C) Copyright 2009 Intel Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
14 * VRTC is emulated by system controller firmware, the real HW
15 * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
16 * in a memory mapped IO space that is visible to the host IA
19 * This driver is based upon drivers/rtc/rtc-cmos.c
24 * * vRTC only supports binary mode and 24H mode
25 * * vRTC only support PIE and AIE, no UIE, and its PIE only happens
26 * at 23:59:59pm everyday, no support for adjustable frequency
27 * * Alarm function is also limited to hr/min/sec.
30 #include <linux/mod_devicetable.h>
31 #include <linux/platform_device.h>
32 #include <linux/interrupt.h>
33 #include <linux/spinlock.h>
34 #include <linux/kernel.h>
35 #include <linux/mc146818rtc.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/sfi.h>
40 #include <asm/intel_scu_ipc.h>
41 #include <asm/intel-mid.h>
42 #include <asm/intel_mid_vrtc.h>
45 struct rtc_device *rtc;
53 static const char driver_name[] = "rtc_mrst";
55 #define RTC_IRQMASK (RTC_PF | RTC_AF)
57 static inline int is_intr(u8 rtc_intr)
59 if (!(rtc_intr & RTC_IRQF))
61 return rtc_intr & RTC_IRQMASK;
64 static inline unsigned char vrtc_is_updating(void)
69 spin_lock_irqsave(&rtc_lock, flags);
70 uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP);
71 spin_unlock_irqrestore(&rtc_lock, flags);
76 * rtc_time's year contains the increment over 1900, but vRTC's YEAR
77 * register can't be programmed to value larger than 0x64, so vRTC
78 * driver chose to use 1972 (1970 is UNIX time start point) as the base,
79 * and does the translation at read/write time.
81 * Why not just use 1970 as the offset? it's because using 1972 will
82 * make it consistent in leap year setting for both vrtc and low-level
83 * physical rtc devices. Then why not use 1960 as the offset? If we use
84 * 1960, for a device's first use, its YEAR register is 0 and the system
85 * year will be parsed as 1960 which is not a valid UNIX time and will
86 * cause many applications to fail mysteriously.
88 static int mrst_read_time(struct device *dev, struct rtc_time *time)
92 if (vrtc_is_updating())
95 spin_lock_irqsave(&rtc_lock, flags);
96 time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
97 time->tm_min = vrtc_cmos_read(RTC_MINUTES);
98 time->tm_hour = vrtc_cmos_read(RTC_HOURS);
99 time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
100 time->tm_mon = vrtc_cmos_read(RTC_MONTH);
101 time->tm_year = vrtc_cmos_read(RTC_YEAR);
102 spin_unlock_irqrestore(&rtc_lock, flags);
104 /* Adjust for the 1972/1900 */
110 static int mrst_set_time(struct device *dev, struct rtc_time *time)
114 unsigned char mon, day, hrs, min, sec;
118 mon = time->tm_mon + 1; /* tm_mon starts at zero */
124 if (yrs < 72 || yrs > 172)
128 spin_lock_irqsave(&rtc_lock, flags);
130 vrtc_cmos_write(yrs, RTC_YEAR);
131 vrtc_cmos_write(mon, RTC_MONTH);
132 vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
133 vrtc_cmos_write(hrs, RTC_HOURS);
134 vrtc_cmos_write(min, RTC_MINUTES);
135 vrtc_cmos_write(sec, RTC_SECONDS);
137 spin_unlock_irqrestore(&rtc_lock, flags);
139 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
143 static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
145 struct mrst_rtc *mrst = dev_get_drvdata(dev);
146 unsigned char rtc_control;
151 /* vRTC only supports binary mode */
152 spin_lock_irq(&rtc_lock);
153 t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
154 t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
155 t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
157 rtc_control = vrtc_cmos_read(RTC_CONTROL);
158 spin_unlock_irq(&rtc_lock);
160 t->enabled = !!(rtc_control & RTC_AIE);
166 static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
168 unsigned char rtc_intr;
171 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
172 * allegedly some older rtcs need that to handle irqs properly
174 rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
175 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
176 if (is_intr(rtc_intr))
177 rtc_update_irq(mrst->rtc, 1, rtc_intr);
180 static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
182 unsigned char rtc_control;
185 * Flush any pending IRQ status, notably for update irqs,
186 * before we enable new IRQs
188 rtc_control = vrtc_cmos_read(RTC_CONTROL);
189 mrst_checkintr(mrst, rtc_control);
192 vrtc_cmos_write(rtc_control, RTC_CONTROL);
194 mrst_checkintr(mrst, rtc_control);
197 static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
199 unsigned char rtc_control;
201 rtc_control = vrtc_cmos_read(RTC_CONTROL);
202 rtc_control &= ~mask;
203 vrtc_cmos_write(rtc_control, RTC_CONTROL);
204 mrst_checkintr(mrst, rtc_control);
207 static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
209 struct mrst_rtc *mrst = dev_get_drvdata(dev);
210 unsigned char hrs, min, sec;
216 hrs = t->time.tm_hour;
217 min = t->time.tm_min;
218 sec = t->time.tm_sec;
220 spin_lock_irq(&rtc_lock);
221 /* Next rtc irq must not be from previous alarm setting */
222 mrst_irq_disable(mrst, RTC_AIE);
225 vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
226 vrtc_cmos_write(min, RTC_MINUTES_ALARM);
227 vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
229 spin_unlock_irq(&rtc_lock);
231 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
235 spin_lock_irq(&rtc_lock);
237 mrst_irq_enable(mrst, RTC_AIE);
239 spin_unlock_irq(&rtc_lock);
244 /* Currently, the vRTC doesn't support UIE ON/OFF */
245 static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
247 struct mrst_rtc *mrst = dev_get_drvdata(dev);
250 spin_lock_irqsave(&rtc_lock, flags);
252 mrst_irq_enable(mrst, RTC_AIE);
254 mrst_irq_disable(mrst, RTC_AIE);
255 spin_unlock_irqrestore(&rtc_lock, flags);
260 #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
262 static int mrst_procfs(struct device *dev, struct seq_file *seq)
264 unsigned char rtc_control;
266 spin_lock_irq(&rtc_lock);
267 rtc_control = vrtc_cmos_read(RTC_CONTROL);
268 spin_unlock_irq(&rtc_lock);
271 "periodic_IRQ\t: %s\n"
274 "periodic_freq\t: daily (not adjustable)\n",
275 (rtc_control & RTC_PIE) ? "on" : "off",
276 (rtc_control & RTC_AIE) ? "on" : "off");
282 #define mrst_procfs NULL
285 static const struct rtc_class_ops mrst_rtc_ops = {
286 .read_time = mrst_read_time,
287 .set_time = mrst_set_time,
288 .read_alarm = mrst_read_alarm,
289 .set_alarm = mrst_set_alarm,
291 .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
294 static struct mrst_rtc mrst_rtc;
297 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
298 * Reg B, so no need for this driver to clear it
300 static irqreturn_t mrst_rtc_irq(int irq, void *p)
304 spin_lock(&rtc_lock);
305 /* This read will clear all IRQ flags inside Reg C */
306 irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
307 spin_unlock(&rtc_lock);
309 irqstat &= RTC_IRQMASK | RTC_IRQF;
310 if (is_intr(irqstat)) {
311 rtc_update_irq(p, 1, irqstat);
317 static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
321 unsigned char rtc_control;
323 /* There can be only one ... */
330 iomem = devm_request_mem_region(dev, iomem->start, resource_size(iomem),
333 dev_dbg(dev, "i/o mem already in use.\n");
337 mrst_rtc.irq = rtc_irq;
339 dev_set_drvdata(dev, &mrst_rtc);
341 mrst_rtc.rtc = devm_rtc_allocate_device(dev);
342 if (IS_ERR(mrst_rtc.rtc))
343 return PTR_ERR(mrst_rtc.rtc);
345 mrst_rtc.rtc->ops = &mrst_rtc_ops;
347 rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
349 spin_lock_irq(&rtc_lock);
350 mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
351 rtc_control = vrtc_cmos_read(RTC_CONTROL);
352 spin_unlock_irq(&rtc_lock);
354 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
355 dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
358 retval = devm_request_irq(dev, rtc_irq, mrst_rtc_irq,
359 0, dev_name(&mrst_rtc.rtc->dev),
362 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
368 retval = rtc_register_device(mrst_rtc.rtc);
372 dev_dbg(dev, "initialised\n");
377 dev_err(dev, "rtc-mrst: unable to initialise\n");
381 static void rtc_mrst_do_shutdown(void)
383 spin_lock_irq(&rtc_lock);
384 mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
385 spin_unlock_irq(&rtc_lock);
388 static void rtc_mrst_do_remove(struct device *dev)
390 struct mrst_rtc *mrst = dev_get_drvdata(dev);
392 rtc_mrst_do_shutdown();
398 #ifdef CONFIG_PM_SLEEP
399 static int mrst_suspend(struct device *dev)
401 struct mrst_rtc *mrst = dev_get_drvdata(dev);
404 /* Only the alarm might be a wakeup event source */
405 spin_lock_irq(&rtc_lock);
406 mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
407 if (tmp & (RTC_PIE | RTC_AIE)) {
410 if (device_may_wakeup(dev))
411 mask = RTC_IRQMASK & ~RTC_AIE;
415 vrtc_cmos_write(tmp, RTC_CONTROL);
417 mrst_checkintr(mrst, tmp);
419 spin_unlock_irq(&rtc_lock);
422 mrst->enabled_wake = 1;
423 enable_irq_wake(mrst->irq);
426 dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
427 (tmp & RTC_AIE) ? ", alarm may wake" : "",
434 * We want RTC alarms to wake us from the deep power saving state
436 static inline int mrst_poweroff(struct device *dev)
438 return mrst_suspend(dev);
441 static int mrst_resume(struct device *dev)
443 struct mrst_rtc *mrst = dev_get_drvdata(dev);
444 unsigned char tmp = mrst->suspend_ctrl;
446 /* Re-enable any irqs previously active */
447 if (tmp & RTC_IRQMASK) {
450 if (mrst->enabled_wake) {
451 disable_irq_wake(mrst->irq);
452 mrst->enabled_wake = 0;
455 spin_lock_irq(&rtc_lock);
457 vrtc_cmos_write(tmp, RTC_CONTROL);
459 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
460 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
464 rtc_update_irq(mrst->rtc, 1, mask);
466 } while (mask & RTC_AIE);
467 spin_unlock_irq(&rtc_lock);
470 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
475 static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
476 #define MRST_PM_OPS (&mrst_pm_ops)
479 #define MRST_PM_OPS NULL
481 static inline int mrst_poweroff(struct device *dev)
488 static int vrtc_mrst_platform_probe(struct platform_device *pdev)
490 return vrtc_mrst_do_probe(&pdev->dev,
491 platform_get_resource(pdev, IORESOURCE_MEM, 0),
492 platform_get_irq(pdev, 0));
495 static int vrtc_mrst_platform_remove(struct platform_device *pdev)
497 rtc_mrst_do_remove(&pdev->dev);
501 static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
503 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
506 rtc_mrst_do_shutdown();
509 MODULE_ALIAS("platform:vrtc_mrst");
511 static struct platform_driver vrtc_mrst_platform_driver = {
512 .probe = vrtc_mrst_platform_probe,
513 .remove = vrtc_mrst_platform_remove,
514 .shutdown = vrtc_mrst_platform_shutdown,
521 module_platform_driver(vrtc_mrst_platform_driver);
523 MODULE_AUTHOR("Jacob Pan; Feng Tang");
524 MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
525 MODULE_LICENSE("GPL");