blob: bcd0cf63eb166df6520e5e0c5beef3b6abaac75b [file] [log] [blame]
Feng Tang0146f262010-11-10 17:29:17 +00001/*
2 * rtc-mrst.c: Driver for Moorestown virtual RTC
3 *
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Jacob Pan (jacob.jun.pan@intel.com)
6 * Feng Tang (feng.tang@intel.com)
7 *
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.
12 *
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.
18 *
19 * This driver is based upon drivers/rtc/rtc-cmos.c
20 */
21
22/*
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.
28 */
29
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>
38
39#include <asm-generic/rtc.h>
40#include <asm/intel_scu_ipc.h>
41#include <asm/mrst.h>
42#include <asm/mrst-vrtc.h>
43
44struct mrst_rtc {
45 struct rtc_device *rtc;
46 struct device *dev;
47 int irq;
48 struct resource *iomem;
49
50 u8 enabled_wake;
51 u8 suspend_ctrl;
52};
53
54static const char driver_name[] = "rtc_mrst";
55
56#define RTC_IRQMASK (RTC_PF | RTC_AF)
57
58static inline int is_intr(u8 rtc_intr)
59{
60 if (!(rtc_intr & RTC_IRQF))
61 return 0;
62 return rtc_intr & RTC_IRQMASK;
63}
64
65/*
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,
Feng Tangd3e18842010-11-17 12:11:24 +000069 * and does the translation at read/write time.
70 *
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.
Feng Tang0146f262010-11-10 17:29:17 +000074 */
75static int mrst_read_time(struct device *dev, struct rtc_time *time)
76{
77 unsigned long flags;
78
79 if (rtc_is_updating())
80 mdelay(20);
81
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);
90
91 /* Adjust for the 1960/1900 */
92 time->tm_year += 60;
93 time->tm_mon--;
94 return RTC_24H;
95}
96
97static int mrst_set_time(struct device *dev, struct rtc_time *time)
98{
99 int ret;
100 unsigned long flags;
101 unsigned char mon, day, hrs, min, sec;
102 unsigned int yrs;
103
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;
110
111 if (yrs < 70 || yrs > 138)
112 return -EINVAL;
113 yrs -= 60;
114
115 spin_lock_irqsave(&rtc_lock, flags);
116
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);
123
124 spin_unlock_irqrestore(&rtc_lock, flags);
125
126 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
127 return ret;
128}
129
130static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
131{
132 struct mrst_rtc *mrst = dev_get_drvdata(dev);
133 unsigned char rtc_control;
134
135 if (mrst->irq <= 0)
136 return -EIO;
137
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.
141 */
142 t->time.tm_mday = -1;
143 t->time.tm_mon = -1;
144 t->time.tm_year = -1;
145
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);
151
152 rtc_control = vrtc_cmos_read(RTC_CONTROL);
153 spin_unlock_irq(&rtc_lock);
154
155 t->enabled = !!(rtc_control & RTC_AIE);
156 t->pending = 0;
157
158 return 0;
159}
160
161static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
162{
163 unsigned char rtc_intr;
164
165 /*
166 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
167 * allegedly some older rtcs need that to handle irqs properly
168 */
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);
173}
174
175static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
176{
177 unsigned char rtc_control;
178
179 /*
180 * Flush any pending IRQ status, notably for update irqs,
181 * before we enable new IRQs
182 */
183 rtc_control = vrtc_cmos_read(RTC_CONTROL);
184 mrst_checkintr(mrst, rtc_control);
185
186 rtc_control |= mask;
187 vrtc_cmos_write(rtc_control, RTC_CONTROL);
188
189 mrst_checkintr(mrst, rtc_control);
190}
191
192static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
193{
194 unsigned char rtc_control;
195
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);
200}
201
202static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
203{
204 struct mrst_rtc *mrst = dev_get_drvdata(dev);
205 unsigned char hrs, min, sec;
206 int ret = 0;
207
208 if (!mrst->irq)
209 return -EIO;
210
211 hrs = t->time.tm_hour;
212 min = t->time.tm_min;
213 sec = t->time.tm_sec;
214
215 spin_lock_irq(&rtc_lock);
216 /* Next rtc irq must not be from previous alarm setting */
217 mrst_irq_disable(mrst, RTC_AIE);
218
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);
223
224 spin_unlock_irq(&rtc_lock);
225
226 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
227 if (ret)
228 return ret;
229
230 spin_lock_irq(&rtc_lock);
231 if (t->enabled)
232 mrst_irq_enable(mrst, RTC_AIE);
233
234 spin_unlock_irq(&rtc_lock);
235
236 return 0;
237}
238
239static int mrst_irq_set_state(struct device *dev, int enabled)
240{
241 struct mrst_rtc *mrst = dev_get_drvdata(dev);
242 unsigned long flags;
243
244 if (!mrst->irq)
245 return -ENXIO;
246
247 spin_lock_irqsave(&rtc_lock, flags);
248
249 if (enabled)
250 mrst_irq_enable(mrst, RTC_PIE);
251 else
252 mrst_irq_disable(mrst, RTC_PIE);
253
254 spin_unlock_irqrestore(&rtc_lock, flags);
255 return 0;
256}
257
258#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
259
260/* Currently, the vRTC doesn't support UIE ON/OFF */
261static int
262mrst_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
263{
264 struct mrst_rtc *mrst = dev_get_drvdata(dev);
265 unsigned long flags;
266
267 switch (cmd) {
268 case RTC_AIE_OFF:
269 case RTC_AIE_ON:
270 if (!mrst->irq)
271 return -EINVAL;
272 break;
273 default:
274 /* PIE ON/OFF is handled by mrst_irq_set_state() */
275 return -ENOIOCTLCMD;
276 }
277
278 spin_lock_irqsave(&rtc_lock, flags);
279 switch (cmd) {
280 case RTC_AIE_OFF: /* alarm off */
281 mrst_irq_disable(mrst, RTC_AIE);
282 break;
283 case RTC_AIE_ON: /* alarm on */
284 mrst_irq_enable(mrst, RTC_AIE);
285 break;
286 }
287 spin_unlock_irqrestore(&rtc_lock, flags);
288 return 0;
289}
290
291#else
292#define mrst_rtc_ioctl NULL
293#endif
294
295#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
296
297static int mrst_procfs(struct device *dev, struct seq_file *seq)
298{
299 unsigned char rtc_control, valid;
300
301 spin_lock_irq(&rtc_lock);
302 rtc_control = vrtc_cmos_read(RTC_CONTROL);
303 valid = vrtc_cmos_read(RTC_VALID);
304 spin_unlock_irq(&rtc_lock);
305
306 return seq_printf(seq,
307 "periodic_IRQ\t: %s\n"
308 "alarm\t\t: %s\n"
309 "BCD\t\t: no\n"
310 "periodic_freq\t: daily (not adjustable)\n",
311 (rtc_control & RTC_PIE) ? "on" : "off",
312 (rtc_control & RTC_AIE) ? "on" : "off");
313}
314
315#else
316#define mrst_procfs NULL
317#endif
318
319static const struct rtc_class_ops mrst_rtc_ops = {
320 .ioctl = mrst_rtc_ioctl,
321 .read_time = mrst_read_time,
322 .set_time = mrst_set_time,
323 .read_alarm = mrst_read_alarm,
324 .set_alarm = mrst_set_alarm,
325 .proc = mrst_procfs,
326 .irq_set_state = mrst_irq_set_state,
327};
328
329static struct mrst_rtc mrst_rtc;
330
331/*
332 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
333 * Reg B, so no need for this driver to clear it
334 */
335static irqreturn_t mrst_rtc_irq(int irq, void *p)
336{
337 u8 irqstat;
338
339 spin_lock(&rtc_lock);
340 /* This read will clear all IRQ flags inside Reg C */
341 irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
342 spin_unlock(&rtc_lock);
343
344 irqstat &= RTC_IRQMASK | RTC_IRQF;
345 if (is_intr(irqstat)) {
346 rtc_update_irq(p, 1, irqstat);
347 return IRQ_HANDLED;
348 }
349 return IRQ_NONE;
350}
351
352static int __init
353vrtc_mrst_do_probe(struct device *dev, struct resource *iomem, int rtc_irq)
354{
355 int retval = 0;
356 unsigned char rtc_control;
357
358 /* There can be only one ... */
359 if (mrst_rtc.dev)
360 return -EBUSY;
361
362 if (!iomem)
363 return -ENODEV;
364
365 iomem = request_mem_region(iomem->start,
366 iomem->end + 1 - iomem->start,
367 driver_name);
368 if (!iomem) {
369 dev_dbg(dev, "i/o mem already in use.\n");
370 return -EBUSY;
371 }
372
373 mrst_rtc.irq = rtc_irq;
374 mrst_rtc.iomem = iomem;
375
376 mrst_rtc.rtc = rtc_device_register(driver_name, dev,
377 &mrst_rtc_ops, THIS_MODULE);
378 if (IS_ERR(mrst_rtc.rtc)) {
379 retval = PTR_ERR(mrst_rtc.rtc);
380 goto cleanup0;
381 }
382
383 mrst_rtc.dev = dev;
384 dev_set_drvdata(dev, &mrst_rtc);
385 rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
386
387 spin_lock_irq(&rtc_lock);
388 mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
389 rtc_control = vrtc_cmos_read(RTC_CONTROL);
390 spin_unlock_irq(&rtc_lock);
391
392 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
393 dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
394
395 if (rtc_irq) {
396 retval = request_irq(rtc_irq, mrst_rtc_irq,
397 IRQF_DISABLED, dev_name(&mrst_rtc.rtc->dev),
398 mrst_rtc.rtc);
399 if (retval < 0) {
400 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
401 rtc_irq, retval);
402 goto cleanup1;
403 }
404 }
405 dev_dbg(dev, "initialised\n");
406 return 0;
407
408cleanup1:
409 mrst_rtc.dev = NULL;
410 rtc_device_unregister(mrst_rtc.rtc);
411cleanup0:
412 release_region(iomem->start, iomem->end + 1 - iomem->start);
413 dev_err(dev, "rtc-mrst: unable to initialise\n");
414 return retval;
415}
416
417static void rtc_mrst_do_shutdown(void)
418{
419 spin_lock_irq(&rtc_lock);
420 mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
421 spin_unlock_irq(&rtc_lock);
422}
423
424static void __exit rtc_mrst_do_remove(struct device *dev)
425{
426 struct mrst_rtc *mrst = dev_get_drvdata(dev);
427 struct resource *iomem;
428
429 rtc_mrst_do_shutdown();
430
431 if (mrst->irq)
432 free_irq(mrst->irq, mrst->rtc);
433
434 rtc_device_unregister(mrst->rtc);
435 mrst->rtc = NULL;
436
437 iomem = mrst->iomem;
438 release_region(iomem->start, iomem->end + 1 - iomem->start);
439 mrst->iomem = NULL;
440
441 mrst->dev = NULL;
442 dev_set_drvdata(dev, NULL);
443}
444
445#ifdef CONFIG_PM
446static int mrst_suspend(struct device *dev, pm_message_t mesg)
447{
448 struct mrst_rtc *mrst = dev_get_drvdata(dev);
449 unsigned char tmp;
450
451 /* Only the alarm might be a wakeup event source */
452 spin_lock_irq(&rtc_lock);
453 mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
454 if (tmp & (RTC_PIE | RTC_AIE)) {
455 unsigned char mask;
456
457 if (device_may_wakeup(dev))
458 mask = RTC_IRQMASK & ~RTC_AIE;
459 else
460 mask = RTC_IRQMASK;
461 tmp &= ~mask;
462 vrtc_cmos_write(tmp, RTC_CONTROL);
463
464 mrst_checkintr(mrst, tmp);
465 }
466 spin_unlock_irq(&rtc_lock);
467
468 if (tmp & RTC_AIE) {
469 mrst->enabled_wake = 1;
470 enable_irq_wake(mrst->irq);
471 }
472
473 dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
474 (tmp & RTC_AIE) ? ", alarm may wake" : "",
475 tmp);
476
477 return 0;
478}
479
480/*
481 * We want RTC alarms to wake us from the deep power saving state
482 */
483static inline int mrst_poweroff(struct device *dev)
484{
485 return mrst_suspend(dev, PMSG_HIBERNATE);
486}
487
488static int mrst_resume(struct device *dev)
489{
490 struct mrst_rtc *mrst = dev_get_drvdata(dev);
491 unsigned char tmp = mrst->suspend_ctrl;
492
493 /* Re-enable any irqs previously active */
494 if (tmp & RTC_IRQMASK) {
495 unsigned char mask;
496
497 if (mrst->enabled_wake) {
498 disable_irq_wake(mrst->irq);
499 mrst->enabled_wake = 0;
500 }
501
502 spin_lock_irq(&rtc_lock);
503 do {
504 vrtc_cmos_write(tmp, RTC_CONTROL);
505
506 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
507 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
508 if (!is_intr(mask))
509 break;
510
511 rtc_update_irq(mrst->rtc, 1, mask);
512 tmp &= ~RTC_AIE;
513 } while (mask & RTC_AIE);
514 spin_unlock_irq(&rtc_lock);
515 }
516
517 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
518
519 return 0;
520}
521
522#else
523#define mrst_suspend NULL
524#define mrst_resume NULL
525
526static inline int mrst_poweroff(struct device *dev)
527{
528 return -ENOSYS;
529}
530
531#endif
532
533static int __init vrtc_mrst_platform_probe(struct platform_device *pdev)
534{
535 return vrtc_mrst_do_probe(&pdev->dev,
536 platform_get_resource(pdev, IORESOURCE_MEM, 0),
537 platform_get_irq(pdev, 0));
538}
539
540static int __exit vrtc_mrst_platform_remove(struct platform_device *pdev)
541{
542 rtc_mrst_do_remove(&pdev->dev);
543 return 0;
544}
545
546static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
547{
548 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
549 return;
550
551 rtc_mrst_do_shutdown();
552}
553
554MODULE_ALIAS("platform:vrtc_mrst");
555
556static struct platform_driver vrtc_mrst_platform_driver = {
557 .probe = vrtc_mrst_platform_probe,
558 .remove = __exit_p(vrtc_mrst_platform_remove),
559 .shutdown = vrtc_mrst_platform_shutdown,
560 .driver = {
561 .name = (char *) driver_name,
562 .suspend = mrst_suspend,
563 .resume = mrst_resume,
564 }
565};
566
567static int __init vrtc_mrst_init(void)
568{
569 return platform_driver_register(&vrtc_mrst_platform_driver);
570}
571
572static void __exit vrtc_mrst_exit(void)
573{
574 platform_driver_unregister(&vrtc_mrst_platform_driver);
575}
576
577module_init(vrtc_mrst_init);
578module_exit(vrtc_mrst_exit);
579
580MODULE_AUTHOR("Jacob Pan; Feng Tang");
581MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
582MODULE_LICENSE("GPL");