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
;
65 static inline unsigned char vrtc_is_updating(void)
70 spin_lock_irqsave(&rtc_lock
, flags
);
71 uip
= (vrtc_cmos_read(RTC_FREQ_SELECT
) & RTC_UIP
);
72 spin_unlock_irqrestore(&rtc_lock
, flags
);
77 * rtc_time's year contains the increment over 1900, but vRTC's YEAR
78 * register can't be programmed to value larger than 0x64, so vRTC
79 * driver chose to use 1960 (1970 is UNIX time start point) as the base,
80 * and does the translation at read/write time.
82 * Why not just use 1970 as the offset? it's because using 1960 will
83 * make it consistent in leap year setting for both vrtc and low-level
84 * physical rtc devices.
86 static int mrst_read_time(struct device
*dev
, struct rtc_time
*time
)
90 if (vrtc_is_updating())
93 spin_lock_irqsave(&rtc_lock
, flags
);
94 time
->tm_sec
= vrtc_cmos_read(RTC_SECONDS
);
95 time
->tm_min
= vrtc_cmos_read(RTC_MINUTES
);
96 time
->tm_hour
= vrtc_cmos_read(RTC_HOURS
);
97 time
->tm_mday
= vrtc_cmos_read(RTC_DAY_OF_MONTH
);
98 time
->tm_mon
= vrtc_cmos_read(RTC_MONTH
);
99 time
->tm_year
= vrtc_cmos_read(RTC_YEAR
);
100 spin_unlock_irqrestore(&rtc_lock
, flags
);
102 /* Adjust for the 1960/1900 */
108 static int mrst_set_time(struct device
*dev
, struct rtc_time
*time
)
112 unsigned char mon
, day
, hrs
, min
, sec
;
116 mon
= time
->tm_mon
+ 1; /* tm_mon starts at zero */
122 if (yrs
< 70 || yrs
> 138)
126 spin_lock_irqsave(&rtc_lock
, flags
);
128 vrtc_cmos_write(yrs
, RTC_YEAR
);
129 vrtc_cmos_write(mon
, RTC_MONTH
);
130 vrtc_cmos_write(day
, RTC_DAY_OF_MONTH
);
131 vrtc_cmos_write(hrs
, RTC_HOURS
);
132 vrtc_cmos_write(min
, RTC_MINUTES
);
133 vrtc_cmos_write(sec
, RTC_SECONDS
);
135 spin_unlock_irqrestore(&rtc_lock
, flags
);
137 ret
= intel_scu_ipc_simple_command(IPCMSG_VRTC
, IPC_CMD_VRTC_SETTIME
);
141 static int mrst_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
143 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
144 unsigned char rtc_control
;
149 /* Basic alarms only support hour, minute, and seconds fields.
150 * Some also support day and month, for alarms up to a year in
153 t
->time
.tm_mday
= -1;
155 t
->time
.tm_year
= -1;
157 /* vRTC only supports binary mode */
158 spin_lock_irq(&rtc_lock
);
159 t
->time
.tm_sec
= vrtc_cmos_read(RTC_SECONDS_ALARM
);
160 t
->time
.tm_min
= vrtc_cmos_read(RTC_MINUTES_ALARM
);
161 t
->time
.tm_hour
= vrtc_cmos_read(RTC_HOURS_ALARM
);
163 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
164 spin_unlock_irq(&rtc_lock
);
166 t
->enabled
= !!(rtc_control
& RTC_AIE
);
172 static void mrst_checkintr(struct mrst_rtc
*mrst
, unsigned char rtc_control
)
174 unsigned char rtc_intr
;
177 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
178 * allegedly some older rtcs need that to handle irqs properly
180 rtc_intr
= vrtc_cmos_read(RTC_INTR_FLAGS
);
181 rtc_intr
&= (rtc_control
& RTC_IRQMASK
) | RTC_IRQF
;
182 if (is_intr(rtc_intr
))
183 rtc_update_irq(mrst
->rtc
, 1, rtc_intr
);
186 static void mrst_irq_enable(struct mrst_rtc
*mrst
, unsigned char mask
)
188 unsigned char rtc_control
;
191 * Flush any pending IRQ status, notably for update irqs,
192 * before we enable new IRQs
194 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
195 mrst_checkintr(mrst
, rtc_control
);
198 vrtc_cmos_write(rtc_control
, RTC_CONTROL
);
200 mrst_checkintr(mrst
, rtc_control
);
203 static void mrst_irq_disable(struct mrst_rtc
*mrst
, unsigned char mask
)
205 unsigned char rtc_control
;
207 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
208 rtc_control
&= ~mask
;
209 vrtc_cmos_write(rtc_control
, RTC_CONTROL
);
210 mrst_checkintr(mrst
, rtc_control
);
213 static int mrst_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
215 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
216 unsigned char hrs
, min
, sec
;
222 hrs
= t
->time
.tm_hour
;
223 min
= t
->time
.tm_min
;
224 sec
= t
->time
.tm_sec
;
226 spin_lock_irq(&rtc_lock
);
227 /* Next rtc irq must not be from previous alarm setting */
228 mrst_irq_disable(mrst
, RTC_AIE
);
231 vrtc_cmos_write(hrs
, RTC_HOURS_ALARM
);
232 vrtc_cmos_write(min
, RTC_MINUTES_ALARM
);
233 vrtc_cmos_write(sec
, RTC_SECONDS_ALARM
);
235 spin_unlock_irq(&rtc_lock
);
237 ret
= intel_scu_ipc_simple_command(IPCMSG_VRTC
, IPC_CMD_VRTC_SETALARM
);
241 spin_lock_irq(&rtc_lock
);
243 mrst_irq_enable(mrst
, RTC_AIE
);
245 spin_unlock_irq(&rtc_lock
);
250 /* Currently, the vRTC doesn't support UIE ON/OFF */
251 static int mrst_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
253 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
256 spin_lock_irqsave(&rtc_lock
, flags
);
258 mrst_irq_enable(mrst
, RTC_AIE
);
260 mrst_irq_disable(mrst
, RTC_AIE
);
261 spin_unlock_irqrestore(&rtc_lock
, flags
);
266 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
268 static int mrst_procfs(struct device
*dev
, struct seq_file
*seq
)
270 unsigned char rtc_control
, valid
;
272 spin_lock_irq(&rtc_lock
);
273 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
274 valid
= vrtc_cmos_read(RTC_VALID
);
275 spin_unlock_irq(&rtc_lock
);
277 return seq_printf(seq
,
278 "periodic_IRQ\t: %s\n"
281 "periodic_freq\t: daily (not adjustable)\n",
282 (rtc_control
& RTC_PIE
) ? "on" : "off",
283 (rtc_control
& RTC_AIE
) ? "on" : "off");
287 #define mrst_procfs NULL
290 static const struct rtc_class_ops mrst_rtc_ops
= {
291 .read_time
= mrst_read_time
,
292 .set_time
= mrst_set_time
,
293 .read_alarm
= mrst_read_alarm
,
294 .set_alarm
= mrst_set_alarm
,
296 .alarm_irq_enable
= mrst_rtc_alarm_irq_enable
,
299 static struct mrst_rtc mrst_rtc
;
302 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
303 * Reg B, so no need for this driver to clear it
305 static irqreturn_t
mrst_rtc_irq(int irq
, void *p
)
309 spin_lock(&rtc_lock
);
310 /* This read will clear all IRQ flags inside Reg C */
311 irqstat
= vrtc_cmos_read(RTC_INTR_FLAGS
);
312 spin_unlock(&rtc_lock
);
314 irqstat
&= RTC_IRQMASK
| RTC_IRQF
;
315 if (is_intr(irqstat
)) {
316 rtc_update_irq(p
, 1, irqstat
);
323 vrtc_mrst_do_probe(struct device
*dev
, struct resource
*iomem
, int rtc_irq
)
326 unsigned char rtc_control
;
328 /* There can be only one ... */
335 iomem
= request_mem_region(iomem
->start
,
336 iomem
->end
+ 1 - iomem
->start
,
339 dev_dbg(dev
, "i/o mem already in use.\n");
343 mrst_rtc
.irq
= rtc_irq
;
344 mrst_rtc
.iomem
= iomem
;
346 dev_set_drvdata(dev
, &mrst_rtc
);
348 mrst_rtc
.rtc
= rtc_device_register(driver_name
, dev
,
349 &mrst_rtc_ops
, THIS_MODULE
);
350 if (IS_ERR(mrst_rtc
.rtc
)) {
351 retval
= PTR_ERR(mrst_rtc
.rtc
);
355 rename_region(iomem
, dev_name(&mrst_rtc
.rtc
->dev
));
357 spin_lock_irq(&rtc_lock
);
358 mrst_irq_disable(&mrst_rtc
, RTC_PIE
| RTC_AIE
);
359 rtc_control
= vrtc_cmos_read(RTC_CONTROL
);
360 spin_unlock_irq(&rtc_lock
);
362 if (!(rtc_control
& RTC_24H
) || (rtc_control
& (RTC_DM_BINARY
)))
363 dev_dbg(dev
, "TODO: support more than 24-hr BCD mode\n");
366 retval
= request_irq(rtc_irq
, mrst_rtc_irq
,
367 IRQF_DISABLED
, dev_name(&mrst_rtc
.rtc
->dev
),
370 dev_dbg(dev
, "IRQ %d is already in use, err %d\n",
375 dev_dbg(dev
, "initialised\n");
379 rtc_device_unregister(mrst_rtc
.rtc
);
381 dev_set_drvdata(dev
, NULL
);
383 release_mem_region(iomem
->start
, resource_size(iomem
));
384 dev_err(dev
, "rtc-mrst: unable to initialise\n");
388 static void rtc_mrst_do_shutdown(void)
390 spin_lock_irq(&rtc_lock
);
391 mrst_irq_disable(&mrst_rtc
, RTC_IRQMASK
);
392 spin_unlock_irq(&rtc_lock
);
395 static void __devexit
rtc_mrst_do_remove(struct device
*dev
)
397 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
398 struct resource
*iomem
;
400 rtc_mrst_do_shutdown();
403 free_irq(mrst
->irq
, mrst
->rtc
);
405 rtc_device_unregister(mrst
->rtc
);
409 release_mem_region(iomem
->start
, resource_size(iomem
));
413 dev_set_drvdata(dev
, NULL
);
417 static int mrst_suspend(struct device
*dev
, pm_message_t mesg
)
419 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
422 /* Only the alarm might be a wakeup event source */
423 spin_lock_irq(&rtc_lock
);
424 mrst
->suspend_ctrl
= tmp
= vrtc_cmos_read(RTC_CONTROL
);
425 if (tmp
& (RTC_PIE
| RTC_AIE
)) {
428 if (device_may_wakeup(dev
))
429 mask
= RTC_IRQMASK
& ~RTC_AIE
;
433 vrtc_cmos_write(tmp
, RTC_CONTROL
);
435 mrst_checkintr(mrst
, tmp
);
437 spin_unlock_irq(&rtc_lock
);
440 mrst
->enabled_wake
= 1;
441 enable_irq_wake(mrst
->irq
);
444 dev_dbg(&mrst_rtc
.rtc
->dev
, "suspend%s, ctrl %02x\n",
445 (tmp
& RTC_AIE
) ? ", alarm may wake" : "",
452 * We want RTC alarms to wake us from the deep power saving state
454 static inline int mrst_poweroff(struct device
*dev
)
456 return mrst_suspend(dev
, PMSG_HIBERNATE
);
459 static int mrst_resume(struct device
*dev
)
461 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
462 unsigned char tmp
= mrst
->suspend_ctrl
;
464 /* Re-enable any irqs previously active */
465 if (tmp
& RTC_IRQMASK
) {
468 if (mrst
->enabled_wake
) {
469 disable_irq_wake(mrst
->irq
);
470 mrst
->enabled_wake
= 0;
473 spin_lock_irq(&rtc_lock
);
475 vrtc_cmos_write(tmp
, RTC_CONTROL
);
477 mask
= vrtc_cmos_read(RTC_INTR_FLAGS
);
478 mask
&= (tmp
& RTC_IRQMASK
) | RTC_IRQF
;
482 rtc_update_irq(mrst
->rtc
, 1, mask
);
484 } while (mask
& RTC_AIE
);
485 spin_unlock_irq(&rtc_lock
);
488 dev_dbg(&mrst_rtc
.rtc
->dev
, "resume, ctrl %02x\n", tmp
);
494 #define mrst_suspend NULL
495 #define mrst_resume NULL
497 static inline int mrst_poweroff(struct device
*dev
)
504 static int __devinit
vrtc_mrst_platform_probe(struct platform_device
*pdev
)
506 return vrtc_mrst_do_probe(&pdev
->dev
,
507 platform_get_resource(pdev
, IORESOURCE_MEM
, 0),
508 platform_get_irq(pdev
, 0));
511 static int __devexit
vrtc_mrst_platform_remove(struct platform_device
*pdev
)
513 rtc_mrst_do_remove(&pdev
->dev
);
517 static void vrtc_mrst_platform_shutdown(struct platform_device
*pdev
)
519 if (system_state
== SYSTEM_POWER_OFF
&& !mrst_poweroff(&pdev
->dev
))
522 rtc_mrst_do_shutdown();
525 MODULE_ALIAS("platform:vrtc_mrst");
527 static struct platform_driver vrtc_mrst_platform_driver
= {
528 .probe
= vrtc_mrst_platform_probe
,
529 .remove
= __devexit_p(vrtc_mrst_platform_remove
),
530 .shutdown
= vrtc_mrst_platform_shutdown
,
532 .name
= (char *) driver_name
,
533 .suspend
= mrst_suspend
,
534 .resume
= mrst_resume
,
538 static int __init
vrtc_mrst_init(void)
540 return platform_driver_register(&vrtc_mrst_platform_driver
);
543 static void __exit
vrtc_mrst_exit(void)
545 platform_driver_unregister(&vrtc_mrst_platform_driver
);
548 module_init(vrtc_mrst_init
);
549 module_exit(vrtc_mrst_exit
);
551 MODULE_AUTHOR("Jacob Pan; Feng Tang");
552 MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
553 MODULE_LICENSE("GPL");