eCryptfs: Allow 2 scatterlist entries for encrypted filenames
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-mrst.c
blob1db62db8469db64611da09b47e1cd5654309cd00
1 /*
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
11 * of the License.
13 * Note:
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
17 * processor.
19 * This driver is based upon drivers/rtc/rtc-cmos.c
23 * Note:
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>
41 #include <asm/mrst.h>
42 #include <asm/mrst-vrtc.h>
44 struct mrst_rtc {
45 struct rtc_device *rtc;
46 struct device *dev;
47 int irq;
48 struct resource *iomem;
50 u8 enabled_wake;
51 u8 suspend_ctrl;
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))
61 return 0;
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)
77 unsigned long flags;
79 if (rtc_is_updating())
80 mdelay(20);
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 */
92 time->tm_year += 60;
93 time->tm_mon--;
94 return RTC_24H;
97 static int mrst_set_time(struct device *dev, struct rtc_time *time)
99 int ret;
100 unsigned long flags;
101 unsigned char mon, day, hrs, min, sec;
102 unsigned int yrs;
104 yrs = time->tm_year;
105 mon = time->tm_mon + 1; /* tm_mon starts at zero */
106 day = time->tm_mday;
107 hrs = time->tm_hour;
108 min = time->tm_min;
109 sec = time->tm_sec;
111 if (yrs < 70 || yrs > 138)
112 return -EINVAL;
113 yrs -= 60;
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);
127 return ret;
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;
135 if (mrst->irq <= 0)
136 return -EIO;
138 /* Basic alarms only support hour, minute, and seconds fields.
139 * Some also support day and month, for alarms up to a year in
140 * the future.
142 t->time.tm_mday = -1;
143 t->time.tm_mon = -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);
156 t->pending = 0;
158 return 0;
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);
186 rtc_control |= mask;
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;
206 int ret = 0;
208 if (!mrst->irq)
209 return -EIO;
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);
219 /* Update alarm */
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);
227 if (ret)
228 return ret;
230 spin_lock_irq(&rtc_lock);
231 if (t->enabled)
232 mrst_irq_enable(mrst, RTC_AIE);
234 spin_unlock_irq(&rtc_lock);
236 return 0;
239 static int mrst_irq_set_state(struct device *dev, int enabled)
241 struct mrst_rtc *mrst = dev_get_drvdata(dev);
242 unsigned long flags;
244 if (!mrst->irq)
245 return -ENXIO;
247 spin_lock_irqsave(&rtc_lock, flags);
249 if (enabled)
250 mrst_irq_enable(mrst, RTC_PIE);
251 else
252 mrst_irq_disable(mrst, RTC_PIE);
254 spin_unlock_irqrestore(&rtc_lock, flags);
255 return 0;
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);
262 unsigned long flags;
264 spin_lock_irqsave(&rtc_lock, flags);
265 if (enabled)
266 mrst_irq_enable(mrst, RTC_AIE);
267 else
268 mrst_irq_disable(mrst, RTC_AIE);
269 spin_unlock_irqrestore(&rtc_lock, flags);
270 return 0;
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"
287 "alarm\t\t: %s\n"
288 "BCD\t\t: no\n"
289 "periodic_freq\t: daily (not adjustable)\n",
290 (rtc_control & RTC_PIE) ? "on" : "off",
291 (rtc_control & RTC_AIE) ? "on" : "off");
294 #else
295 #define mrst_procfs NULL
296 #endif
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,
303 .proc = mrst_procfs,
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)
316 u8 irqstat;
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);
326 return IRQ_HANDLED;
328 return IRQ_NONE;
331 static int __init
332 vrtc_mrst_do_probe(struct device *dev, struct resource *iomem, int rtc_irq)
334 int retval = 0;
335 unsigned char rtc_control;
337 /* There can be only one ... */
338 if (mrst_rtc.dev)
339 return -EBUSY;
341 if (!iomem)
342 return -ENODEV;
344 iomem = request_mem_region(iomem->start,
345 iomem->end + 1 - iomem->start,
346 driver_name);
347 if (!iomem) {
348 dev_dbg(dev, "i/o mem already in use.\n");
349 return -EBUSY;
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);
359 goto cleanup0;
362 mrst_rtc.dev = dev;
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");
374 if (rtc_irq) {
375 retval = request_irq(rtc_irq, mrst_rtc_irq,
376 IRQF_DISABLED, dev_name(&mrst_rtc.rtc->dev),
377 mrst_rtc.rtc);
378 if (retval < 0) {
379 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
380 rtc_irq, retval);
381 goto cleanup1;
384 dev_dbg(dev, "initialised\n");
385 return 0;
387 cleanup1:
388 mrst_rtc.dev = NULL;
389 rtc_device_unregister(mrst_rtc.rtc);
390 cleanup0:
391 release_region(iomem->start, iomem->end + 1 - iomem->start);
392 dev_err(dev, "rtc-mrst: unable to initialise\n");
393 return retval;
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();
410 if (mrst->irq)
411 free_irq(mrst->irq, mrst->rtc);
413 rtc_device_unregister(mrst->rtc);
414 mrst->rtc = NULL;
416 iomem = mrst->iomem;
417 release_region(iomem->start, iomem->end + 1 - iomem->start);
418 mrst->iomem = NULL;
420 mrst->dev = NULL;
421 dev_set_drvdata(dev, NULL);
424 #ifdef CONFIG_PM
425 static int mrst_suspend(struct device *dev, pm_message_t mesg)
427 struct mrst_rtc *mrst = dev_get_drvdata(dev);
428 unsigned char tmp;
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)) {
434 unsigned char mask;
436 if (device_may_wakeup(dev))
437 mask = RTC_IRQMASK & ~RTC_AIE;
438 else
439 mask = RTC_IRQMASK;
440 tmp &= ~mask;
441 vrtc_cmos_write(tmp, RTC_CONTROL);
443 mrst_checkintr(mrst, tmp);
445 spin_unlock_irq(&rtc_lock);
447 if (tmp & RTC_AIE) {
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" : "",
454 tmp);
456 return 0;
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) {
474 unsigned char mask;
476 if (mrst->enabled_wake) {
477 disable_irq_wake(mrst->irq);
478 mrst->enabled_wake = 0;
481 spin_lock_irq(&rtc_lock);
482 do {
483 vrtc_cmos_write(tmp, RTC_CONTROL);
485 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
486 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
487 if (!is_intr(mask))
488 break;
490 rtc_update_irq(mrst->rtc, 1, mask);
491 tmp &= ~RTC_AIE;
492 } while (mask & RTC_AIE);
493 spin_unlock_irq(&rtc_lock);
496 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
498 return 0;
501 #else
502 #define mrst_suspend NULL
503 #define mrst_resume NULL
505 static inline int mrst_poweroff(struct device *dev)
507 return -ENOSYS;
510 #endif
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);
522 return 0;
525 static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
527 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
528 return;
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,
539 .driver = {
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");