2 * rtc-mrst.c: Driver for Moorestown virtual RTC
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Jacob Pan (jacob.jun.pan@intel.com)
6 * Feng Tang (feng.tang@intel.com)
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/module.h>
36 #include <linux/init.h>
37 #include <linux/sfi.h>
39 #include <asm-generic/rtc.h>
40 #include <asm/intel_scu_ipc.h>
42 #include <asm/mrst-vrtc.h>
45 struct rtc_device
*rtc
;
48 struct resource
*iomem
;
54 static const char driver_name
[] = "rtc_mrst";
56 #define RTC_IRQMASK (RTC_PF | RTC_AF)
58 static inline int is_intr(u8 rtc_intr
)
60 if (!(rtc_intr
& RTC_IRQF
))
62 return rtc_intr
& RTC_IRQMASK
;
66 * rtc_time's year contains the increment over 1900, but vRTC's YEAR
67 * register can't be programmed to value larger than 0x64, so vRTC
68 * driver chose to use 1960 (1970 is UNIX time start point) as the base,
69 * and does the translation at read/write time.
71 * Why not just use 1970 as the offset? it's because using 1960 will
72 * make it consistent in leap year setting for both vrtc and low-level
73 * physical rtc devices.
75 static int mrst_read_time(struct device
*dev
, struct rtc_time
*time
)
79 if (rtc_is_updating())
82 spin_lock_irqsave(&rtc_lock
, flags
);
83 time
->tm_sec
= vrtc_cmos_read(RTC_SECONDS
);
84 time
->tm_min
= vrtc_cmos_read(RTC_MINUTES
);
85 time
->tm_hour
= vrtc_cmos_read(RTC_HOURS
);
86 time
->tm_mday
= vrtc_cmos_read(RTC_DAY_OF_MONTH
);
87 time
->tm_mon
= vrtc_cmos_read(RTC_MONTH
);
88 time
->tm_year
= vrtc_cmos_read(RTC_YEAR
);
89 spin_unlock_irqrestore(&rtc_lock
, flags
);
91 /* Adjust for the 1960/1900 */
97 static int mrst_set_time(struct device
*dev
, struct rtc_time
*time
)
101 unsigned char mon
, day
, hrs
, min
, sec
;
105 mon
= time
->tm_mon
+ 1; /* tm_mon starts at zero */
111 if (yrs
< 70 || yrs
> 138)
115 spin_lock_irqsave(&rtc_lock
, flags
);
117 vrtc_cmos_write(yrs
, RTC_YEAR
);
118 vrtc_cmos_write(mon
, RTC_MONTH
);
119 vrtc_cmos_write(day
, RTC_DAY_OF_MONTH
);
120 vrtc_cmos_write(hrs
, RTC_HOURS
);
121 vrtc_cmos_write(min
, RTC_MINUTES
);
122 vrtc_cmos_write(sec
, RTC_SECONDS
);
124 spin_unlock_irqrestore(&rtc_lock
, flags
);
126 ret
= intel_scu_ipc_simple_command(IPCMSG_VRTC
, IPC_CMD_VRTC_SETTIME
);
130 static int mrst_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
132 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
133 unsigned char rtc_control
;
138 /* Basic alarms only support hour, minute, and seconds fields.
139 * Some also support day and month, for alarms up to a year in
142 t
->time
.tm_mday
= -1;
144 t
->time
.tm_year
= -1;
146 /* vRTC only supports binary mode */
147 spin_lock_irq(&rtc_lock
);
148 t
->time
.tm_sec
= vrtc_cmos_read(RTC_SECONDS_ALARM
);
149 t
->time
.tm_min
= vrtc_cmos_read(RTC_MINUTES_ALARM
);
150 t
->time
.tm_hour
= vrtc_cmos_read(RTC_HOURS_ALARM
);
152 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
153 spin_unlock_irq(&rtc_lock
);
155 t
->enabled
= !!(rtc_control
& RTC_AIE
);
161 static void mrst_checkintr(struct mrst_rtc
*mrst
, unsigned char rtc_control
)
163 unsigned char rtc_intr
;
166 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
167 * allegedly some older rtcs need that to handle irqs properly
169 rtc_intr
= vrtc_cmos_read(RTC_INTR_FLAGS
);
170 rtc_intr
&= (rtc_control
& RTC_IRQMASK
) | RTC_IRQF
;
171 if (is_intr(rtc_intr
))
172 rtc_update_irq(mrst
->rtc
, 1, rtc_intr
);
175 static void mrst_irq_enable(struct mrst_rtc
*mrst
, unsigned char mask
)
177 unsigned char rtc_control
;
180 * Flush any pending IRQ status, notably for update irqs,
181 * before we enable new IRQs
183 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
184 mrst_checkintr(mrst
, rtc_control
);
187 vrtc_cmos_write(rtc_control
, RTC_CONTROL
);
189 mrst_checkintr(mrst
, rtc_control
);
192 static void mrst_irq_disable(struct mrst_rtc
*mrst
, unsigned char mask
)
194 unsigned char rtc_control
;
196 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
197 rtc_control
&= ~mask
;
198 vrtc_cmos_write(rtc_control
, RTC_CONTROL
);
199 mrst_checkintr(mrst
, rtc_control
);
202 static int mrst_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
204 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
205 unsigned char hrs
, min
, sec
;
211 hrs
= t
->time
.tm_hour
;
212 min
= t
->time
.tm_min
;
213 sec
= t
->time
.tm_sec
;
215 spin_lock_irq(&rtc_lock
);
216 /* Next rtc irq must not be from previous alarm setting */
217 mrst_irq_disable(mrst
, RTC_AIE
);
220 vrtc_cmos_write(hrs
, RTC_HOURS_ALARM
);
221 vrtc_cmos_write(min
, RTC_MINUTES_ALARM
);
222 vrtc_cmos_write(sec
, RTC_SECONDS_ALARM
);
224 spin_unlock_irq(&rtc_lock
);
226 ret
= intel_scu_ipc_simple_command(IPCMSG_VRTC
, IPC_CMD_VRTC_SETALARM
);
230 spin_lock_irq(&rtc_lock
);
232 mrst_irq_enable(mrst
, RTC_AIE
);
234 spin_unlock_irq(&rtc_lock
);
239 static int mrst_irq_set_state(struct device
*dev
, int enabled
)
241 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
247 spin_lock_irqsave(&rtc_lock
, flags
);
250 mrst_irq_enable(mrst
, RTC_PIE
);
252 mrst_irq_disable(mrst
, RTC_PIE
);
254 spin_unlock_irqrestore(&rtc_lock
, flags
);
258 /* Currently, the vRTC doesn't support UIE ON/OFF */
259 static int mrst_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
261 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
264 spin_lock_irqsave(&rtc_lock
, flags
);
266 mrst_irq_enable(mrst
, RTC_AIE
);
268 mrst_irq_disable(mrst
, RTC_AIE
);
269 spin_unlock_irqrestore(&rtc_lock
, flags
);
274 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
276 static int mrst_procfs(struct device
*dev
, struct seq_file
*seq
)
278 unsigned char rtc_control
, valid
;
280 spin_lock_irq(&rtc_lock
);
281 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
282 valid
= vrtc_cmos_read(RTC_VALID
);
283 spin_unlock_irq(&rtc_lock
);
285 return seq_printf(seq
,
286 "periodic_IRQ\t: %s\n"
289 "periodic_freq\t: daily (not adjustable)\n",
290 (rtc_control
& RTC_PIE
) ? "on" : "off",
291 (rtc_control
& RTC_AIE
) ? "on" : "off");
295 #define mrst_procfs NULL
298 static const struct rtc_class_ops mrst_rtc_ops
= {
299 .read_time
= mrst_read_time
,
300 .set_time
= mrst_set_time
,
301 .read_alarm
= mrst_read_alarm
,
302 .set_alarm
= mrst_set_alarm
,
304 .irq_set_state
= mrst_irq_set_state
,
305 .alarm_irq_enable
= mrst_rtc_alarm_irq_enable
,
308 static struct mrst_rtc mrst_rtc
;
311 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
312 * Reg B, so no need for this driver to clear it
314 static irqreturn_t
mrst_rtc_irq(int irq
, void *p
)
318 spin_lock(&rtc_lock
);
319 /* This read will clear all IRQ flags inside Reg C */
320 irqstat
= vrtc_cmos_read(RTC_INTR_FLAGS
);
321 spin_unlock(&rtc_lock
);
323 irqstat
&= RTC_IRQMASK
| RTC_IRQF
;
324 if (is_intr(irqstat
)) {
325 rtc_update_irq(p
, 1, irqstat
);
332 vrtc_mrst_do_probe(struct device
*dev
, struct resource
*iomem
, int rtc_irq
)
335 unsigned char rtc_control
;
337 /* There can be only one ... */
344 iomem
= request_mem_region(iomem
->start
,
345 iomem
->end
+ 1 - iomem
->start
,
348 dev_dbg(dev
, "i/o mem already in use.\n");
352 mrst_rtc
.irq
= rtc_irq
;
353 mrst_rtc
.iomem
= iomem
;
355 mrst_rtc
.rtc
= rtc_device_register(driver_name
, dev
,
356 &mrst_rtc_ops
, THIS_MODULE
);
357 if (IS_ERR(mrst_rtc
.rtc
)) {
358 retval
= PTR_ERR(mrst_rtc
.rtc
);
363 dev_set_drvdata(dev
, &mrst_rtc
);
364 rename_region(iomem
, dev_name(&mrst_rtc
.rtc
->dev
));
366 spin_lock_irq(&rtc_lock
);
367 mrst_irq_disable(&mrst_rtc
, RTC_PIE
| RTC_AIE
);
368 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
369 spin_unlock_irq(&rtc_lock
);
371 if (!(rtc_control
& RTC_24H
) || (rtc_control
& (RTC_DM_BINARY
)))
372 dev_dbg(dev
, "TODO: support more than 24-hr BCD mode\n");
375 retval
= request_irq(rtc_irq
, mrst_rtc_irq
,
376 IRQF_DISABLED
, dev_name(&mrst_rtc
.rtc
->dev
),
379 dev_dbg(dev
, "IRQ %d is already in use, err %d\n",
384 dev_dbg(dev
, "initialised\n");
389 rtc_device_unregister(mrst_rtc
.rtc
);
391 release_region(iomem
->start
, iomem
->end
+ 1 - iomem
->start
);
392 dev_err(dev
, "rtc-mrst: unable to initialise\n");
396 static void rtc_mrst_do_shutdown(void)
398 spin_lock_irq(&rtc_lock
);
399 mrst_irq_disable(&mrst_rtc
, RTC_IRQMASK
);
400 spin_unlock_irq(&rtc_lock
);
403 static void __exit
rtc_mrst_do_remove(struct device
*dev
)
405 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
406 struct resource
*iomem
;
408 rtc_mrst_do_shutdown();
411 free_irq(mrst
->irq
, mrst
->rtc
);
413 rtc_device_unregister(mrst
->rtc
);
417 release_region(iomem
->start
, iomem
->end
+ 1 - iomem
->start
);
421 dev_set_drvdata(dev
, NULL
);
425 static int mrst_suspend(struct device
*dev
, pm_message_t mesg
)
427 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
430 /* Only the alarm might be a wakeup event source */
431 spin_lock_irq(&rtc_lock
);
432 mrst
->suspend_ctrl
= tmp
= vrtc_cmos_read(RTC_CONTROL
);
433 if (tmp
& (RTC_PIE
| RTC_AIE
)) {
436 if (device_may_wakeup(dev
))
437 mask
= RTC_IRQMASK
& ~RTC_AIE
;
441 vrtc_cmos_write(tmp
, RTC_CONTROL
);
443 mrst_checkintr(mrst
, tmp
);
445 spin_unlock_irq(&rtc_lock
);
448 mrst
->enabled_wake
= 1;
449 enable_irq_wake(mrst
->irq
);
452 dev_dbg(&mrst_rtc
.rtc
->dev
, "suspend%s, ctrl %02x\n",
453 (tmp
& RTC_AIE
) ? ", alarm may wake" : "",
460 * We want RTC alarms to wake us from the deep power saving state
462 static inline int mrst_poweroff(struct device
*dev
)
464 return mrst_suspend(dev
, PMSG_HIBERNATE
);
467 static int mrst_resume(struct device
*dev
)
469 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
470 unsigned char tmp
= mrst
->suspend_ctrl
;
472 /* Re-enable any irqs previously active */
473 if (tmp
& RTC_IRQMASK
) {
476 if (mrst
->enabled_wake
) {
477 disable_irq_wake(mrst
->irq
);
478 mrst
->enabled_wake
= 0;
481 spin_lock_irq(&rtc_lock
);
483 vrtc_cmos_write(tmp
, RTC_CONTROL
);
485 mask
= vrtc_cmos_read(RTC_INTR_FLAGS
);
486 mask
&= (tmp
& RTC_IRQMASK
) | RTC_IRQF
;
490 rtc_update_irq(mrst
->rtc
, 1, mask
);
492 } while (mask
& RTC_AIE
);
493 spin_unlock_irq(&rtc_lock
);
496 dev_dbg(&mrst_rtc
.rtc
->dev
, "resume, ctrl %02x\n", tmp
);
502 #define mrst_suspend NULL
503 #define mrst_resume NULL
505 static inline int mrst_poweroff(struct device
*dev
)
512 static int __init
vrtc_mrst_platform_probe(struct platform_device
*pdev
)
514 return vrtc_mrst_do_probe(&pdev
->dev
,
515 platform_get_resource(pdev
, IORESOURCE_MEM
, 0),
516 platform_get_irq(pdev
, 0));
519 static int __exit
vrtc_mrst_platform_remove(struct platform_device
*pdev
)
521 rtc_mrst_do_remove(&pdev
->dev
);
525 static void vrtc_mrst_platform_shutdown(struct platform_device
*pdev
)
527 if (system_state
== SYSTEM_POWER_OFF
&& !mrst_poweroff(&pdev
->dev
))
530 rtc_mrst_do_shutdown();
533 MODULE_ALIAS("platform:vrtc_mrst");
535 static struct platform_driver vrtc_mrst_platform_driver
= {
536 .probe
= vrtc_mrst_platform_probe
,
537 .remove
= __exit_p(vrtc_mrst_platform_remove
),
538 .shutdown
= vrtc_mrst_platform_shutdown
,
540 .name
= (char *) driver_name
,
541 .suspend
= mrst_suspend
,
542 .resume
= mrst_resume
,
546 static int __init
vrtc_mrst_init(void)
548 return platform_driver_register(&vrtc_mrst_platform_driver
);
551 static void __exit
vrtc_mrst_exit(void)
553 platform_driver_unregister(&vrtc_mrst_platform_driver
);
556 module_init(vrtc_mrst_init
);
557 module_exit(vrtc_mrst_exit
);
559 MODULE_AUTHOR("Jacob Pan; Feng Tang");
560 MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
561 MODULE_LICENSE("GPL");