blob: e2436d140175a109907e55eacafeb1906105748d [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>
Kuppuswamy Sathyanarayanan05454c22013-10-17 15:35:27 -070041#include <asm/intel-mid.h>
42#include <asm/intel_mid_vrtc.h>
Feng Tang0146f262010-11-10 17:29:17 +000043
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
Feng Tang168202c2011-02-15 00:13:32 +080065static inline unsigned char vrtc_is_updating(void)
66{
67 unsigned char uip;
68 unsigned long flags;
69
70 spin_lock_irqsave(&rtc_lock, flags);
71 uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP);
72 spin_unlock_irqrestore(&rtc_lock, flags);
73 return uip;
74}
75
Feng Tang0146f262010-11-10 17:29:17 +000076/*
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
Feng Tang57e63192011-11-10 13:23:39 +000079 * driver chose to use 1972 (1970 is UNIX time start point) as the base,
Feng Tangd3e18842010-11-17 12:11:24 +000080 * and does the translation at read/write time.
81 *
Feng Tang57e63192011-11-10 13:23:39 +000082 * Why not just use 1970 as the offset? it's because using 1972 will
Feng Tangd3e18842010-11-17 12:11:24 +000083 * make it consistent in leap year setting for both vrtc and low-level
Feng Tang57e63192011-11-10 13:23:39 +000084 * physical rtc devices. Then why not use 1960 as the offset? If we use
85 * 1960, for a device's first use, its YEAR register is 0 and the system
86 * year will be parsed as 1960 which is not a valid UNIX time and will
87 * cause many applications to fail mysteriously.
Feng Tang0146f262010-11-10 17:29:17 +000088 */
89static int mrst_read_time(struct device *dev, struct rtc_time *time)
90{
91 unsigned long flags;
92
Feng Tang168202c2011-02-15 00:13:32 +080093 if (vrtc_is_updating())
Feng Tang0146f262010-11-10 17:29:17 +000094 mdelay(20);
95
96 spin_lock_irqsave(&rtc_lock, flags);
97 time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
98 time->tm_min = vrtc_cmos_read(RTC_MINUTES);
99 time->tm_hour = vrtc_cmos_read(RTC_HOURS);
100 time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
101 time->tm_mon = vrtc_cmos_read(RTC_MONTH);
102 time->tm_year = vrtc_cmos_read(RTC_YEAR);
103 spin_unlock_irqrestore(&rtc_lock, flags);
104
Feng Tang57e63192011-11-10 13:23:39 +0000105 /* Adjust for the 1972/1900 */
106 time->tm_year += 72;
Feng Tang0146f262010-11-10 17:29:17 +0000107 time->tm_mon--;
Feng Tang57e63192011-11-10 13:23:39 +0000108 return rtc_valid_tm(time);
Feng Tang0146f262010-11-10 17:29:17 +0000109}
110
111static int mrst_set_time(struct device *dev, struct rtc_time *time)
112{
113 int ret;
114 unsigned long flags;
115 unsigned char mon, day, hrs, min, sec;
116 unsigned int yrs;
117
118 yrs = time->tm_year;
119 mon = time->tm_mon + 1; /* tm_mon starts at zero */
120 day = time->tm_mday;
121 hrs = time->tm_hour;
122 min = time->tm_min;
123 sec = time->tm_sec;
124
Feng Tang57e63192011-11-10 13:23:39 +0000125 if (yrs < 72 || yrs > 138)
Feng Tang0146f262010-11-10 17:29:17 +0000126 return -EINVAL;
Feng Tang57e63192011-11-10 13:23:39 +0000127 yrs -= 72;
Feng Tang0146f262010-11-10 17:29:17 +0000128
129 spin_lock_irqsave(&rtc_lock, flags);
130
131 vrtc_cmos_write(yrs, RTC_YEAR);
132 vrtc_cmos_write(mon, RTC_MONTH);
133 vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
134 vrtc_cmos_write(hrs, RTC_HOURS);
135 vrtc_cmos_write(min, RTC_MINUTES);
136 vrtc_cmos_write(sec, RTC_SECONDS);
137
138 spin_unlock_irqrestore(&rtc_lock, flags);
139
140 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
141 return ret;
142}
143
144static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
145{
146 struct mrst_rtc *mrst = dev_get_drvdata(dev);
147 unsigned char rtc_control;
148
149 if (mrst->irq <= 0)
150 return -EIO;
151
152 /* Basic alarms only support hour, minute, and seconds fields.
153 * Some also support day and month, for alarms up to a year in
154 * the future.
155 */
156 t->time.tm_mday = -1;
157 t->time.tm_mon = -1;
158 t->time.tm_year = -1;
159
160 /* vRTC only supports binary mode */
161 spin_lock_irq(&rtc_lock);
162 t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
163 t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
164 t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
165
166 rtc_control = vrtc_cmos_read(RTC_CONTROL);
167 spin_unlock_irq(&rtc_lock);
168
169 t->enabled = !!(rtc_control & RTC_AIE);
170 t->pending = 0;
171
172 return 0;
173}
174
175static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
176{
177 unsigned char rtc_intr;
178
179 /*
180 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
181 * allegedly some older rtcs need that to handle irqs properly
182 */
183 rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
184 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
185 if (is_intr(rtc_intr))
186 rtc_update_irq(mrst->rtc, 1, rtc_intr);
187}
188
189static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
190{
191 unsigned char rtc_control;
192
193 /*
194 * Flush any pending IRQ status, notably for update irqs,
195 * before we enable new IRQs
196 */
197 rtc_control = vrtc_cmos_read(RTC_CONTROL);
198 mrst_checkintr(mrst, rtc_control);
199
200 rtc_control |= mask;
201 vrtc_cmos_write(rtc_control, RTC_CONTROL);
202
203 mrst_checkintr(mrst, rtc_control);
204}
205
206static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
207{
208 unsigned char rtc_control;
209
210 rtc_control = vrtc_cmos_read(RTC_CONTROL);
211 rtc_control &= ~mask;
212 vrtc_cmos_write(rtc_control, RTC_CONTROL);
213 mrst_checkintr(mrst, rtc_control);
214}
215
216static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
217{
218 struct mrst_rtc *mrst = dev_get_drvdata(dev);
219 unsigned char hrs, min, sec;
220 int ret = 0;
221
222 if (!mrst->irq)
223 return -EIO;
224
225 hrs = t->time.tm_hour;
226 min = t->time.tm_min;
227 sec = t->time.tm_sec;
228
229 spin_lock_irq(&rtc_lock);
230 /* Next rtc irq must not be from previous alarm setting */
231 mrst_irq_disable(mrst, RTC_AIE);
232
233 /* Update alarm */
234 vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
235 vrtc_cmos_write(min, RTC_MINUTES_ALARM);
236 vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
237
238 spin_unlock_irq(&rtc_lock);
239
240 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
241 if (ret)
242 return ret;
243
244 spin_lock_irq(&rtc_lock);
245 if (t->enabled)
246 mrst_irq_enable(mrst, RTC_AIE);
247
248 spin_unlock_irq(&rtc_lock);
249
250 return 0;
251}
252
Feng Tang0146f262010-11-10 17:29:17 +0000253/* Currently, the vRTC doesn't support UIE ON/OFF */
John Stultz16380c12011-02-02 17:02:41 -0800254static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Feng Tang0146f262010-11-10 17:29:17 +0000255{
256 struct mrst_rtc *mrst = dev_get_drvdata(dev);
257 unsigned long flags;
258
Feng Tang0146f262010-11-10 17:29:17 +0000259 spin_lock_irqsave(&rtc_lock, flags);
John Stultz16380c12011-02-02 17:02:41 -0800260 if (enabled)
Feng Tang0146f262010-11-10 17:29:17 +0000261 mrst_irq_enable(mrst, RTC_AIE);
John Stultz16380c12011-02-02 17:02:41 -0800262 else
263 mrst_irq_disable(mrst, RTC_AIE);
Feng Tang0146f262010-11-10 17:29:17 +0000264 spin_unlock_irqrestore(&rtc_lock, flags);
265 return 0;
266}
267
Feng Tang0146f262010-11-10 17:29:17 +0000268
269#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
270
271static int mrst_procfs(struct device *dev, struct seq_file *seq)
272{
273 unsigned char rtc_control, valid;
274
275 spin_lock_irq(&rtc_lock);
276 rtc_control = vrtc_cmos_read(RTC_CONTROL);
277 valid = vrtc_cmos_read(RTC_VALID);
278 spin_unlock_irq(&rtc_lock);
279
280 return seq_printf(seq,
281 "periodic_IRQ\t: %s\n"
282 "alarm\t\t: %s\n"
283 "BCD\t\t: no\n"
284 "periodic_freq\t: daily (not adjustable)\n",
285 (rtc_control & RTC_PIE) ? "on" : "off",
286 (rtc_control & RTC_AIE) ? "on" : "off");
287}
288
289#else
290#define mrst_procfs NULL
291#endif
292
293static const struct rtc_class_ops mrst_rtc_ops = {
Feng Tang0146f262010-11-10 17:29:17 +0000294 .read_time = mrst_read_time,
295 .set_time = mrst_set_time,
296 .read_alarm = mrst_read_alarm,
297 .set_alarm = mrst_set_alarm,
298 .proc = mrst_procfs,
John Stultz16380c12011-02-02 17:02:41 -0800299 .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
Feng Tang0146f262010-11-10 17:29:17 +0000300};
301
302static struct mrst_rtc mrst_rtc;
303
304/*
305 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
306 * Reg B, so no need for this driver to clear it
307 */
308static irqreturn_t mrst_rtc_irq(int irq, void *p)
309{
310 u8 irqstat;
311
312 spin_lock(&rtc_lock);
313 /* This read will clear all IRQ flags inside Reg C */
314 irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
315 spin_unlock(&rtc_lock);
316
317 irqstat &= RTC_IRQMASK | RTC_IRQF;
318 if (is_intr(irqstat)) {
319 rtc_update_irq(p, 1, irqstat);
320 return IRQ_HANDLED;
321 }
322 return IRQ_NONE;
323}
324
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800325static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
326 int rtc_irq)
Feng Tang0146f262010-11-10 17:29:17 +0000327{
328 int retval = 0;
329 unsigned char rtc_control;
330
331 /* There can be only one ... */
332 if (mrst_rtc.dev)
333 return -EBUSY;
334
335 if (!iomem)
336 return -ENODEV;
337
Joe Perches28f65c112011-06-09 09:13:32 -0700338 iomem = request_mem_region(iomem->start, resource_size(iomem),
339 driver_name);
Feng Tang0146f262010-11-10 17:29:17 +0000340 if (!iomem) {
341 dev_dbg(dev, "i/o mem already in use.\n");
342 return -EBUSY;
343 }
344
345 mrst_rtc.irq = rtc_irq;
346 mrst_rtc.iomem = iomem;
Feng Tangde97a212011-04-07 09:39:44 +0800347 mrst_rtc.dev = dev;
348 dev_set_drvdata(dev, &mrst_rtc);
Feng Tang0146f262010-11-10 17:29:17 +0000349
350 mrst_rtc.rtc = rtc_device_register(driver_name, dev,
351 &mrst_rtc_ops, THIS_MODULE);
352 if (IS_ERR(mrst_rtc.rtc)) {
353 retval = PTR_ERR(mrst_rtc.rtc);
354 goto cleanup0;
355 }
356
Feng Tang0146f262010-11-10 17:29:17 +0000357 rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
358
359 spin_lock_irq(&rtc_lock);
360 mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
361 rtc_control = vrtc_cmos_read(RTC_CONTROL);
362 spin_unlock_irq(&rtc_lock);
363
364 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
365 dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
366
367 if (rtc_irq) {
368 retval = request_irq(rtc_irq, mrst_rtc_irq,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700369 0, dev_name(&mrst_rtc.rtc->dev),
Feng Tang0146f262010-11-10 17:29:17 +0000370 mrst_rtc.rtc);
371 if (retval < 0) {
372 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
373 rtc_irq, retval);
374 goto cleanup1;
375 }
376 }
377 dev_dbg(dev, "initialised\n");
378 return 0;
379
380cleanup1:
Feng Tang0146f262010-11-10 17:29:17 +0000381 rtc_device_unregister(mrst_rtc.rtc);
382cleanup0:
Feng Tangde97a212011-04-07 09:39:44 +0800383 mrst_rtc.dev = NULL;
Julia Lawallc258f9a2011-05-26 16:25:08 -0700384 release_mem_region(iomem->start, resource_size(iomem));
Feng Tang0146f262010-11-10 17:29:17 +0000385 dev_err(dev, "rtc-mrst: unable to initialise\n");
386 return retval;
387}
388
389static void rtc_mrst_do_shutdown(void)
390{
391 spin_lock_irq(&rtc_lock);
392 mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
393 spin_unlock_irq(&rtc_lock);
394}
395
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800396static void rtc_mrst_do_remove(struct device *dev)
Feng Tang0146f262010-11-10 17:29:17 +0000397{
398 struct mrst_rtc *mrst = dev_get_drvdata(dev);
399 struct resource *iomem;
400
401 rtc_mrst_do_shutdown();
402
403 if (mrst->irq)
404 free_irq(mrst->irq, mrst->rtc);
405
406 rtc_device_unregister(mrst->rtc);
407 mrst->rtc = NULL;
408
409 iomem = mrst->iomem;
Julia Lawallc258f9a2011-05-26 16:25:08 -0700410 release_mem_region(iomem->start, resource_size(iomem));
Feng Tang0146f262010-11-10 17:29:17 +0000411 mrst->iomem = NULL;
412
413 mrst->dev = NULL;
Feng Tang0146f262010-11-10 17:29:17 +0000414}
415
416#ifdef CONFIG_PM
417static int mrst_suspend(struct device *dev, pm_message_t mesg)
418{
419 struct mrst_rtc *mrst = dev_get_drvdata(dev);
420 unsigned char tmp;
421
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)) {
426 unsigned char mask;
427
428 if (device_may_wakeup(dev))
429 mask = RTC_IRQMASK & ~RTC_AIE;
430 else
431 mask = RTC_IRQMASK;
432 tmp &= ~mask;
433 vrtc_cmos_write(tmp, RTC_CONTROL);
434
435 mrst_checkintr(mrst, tmp);
436 }
437 spin_unlock_irq(&rtc_lock);
438
439 if (tmp & RTC_AIE) {
440 mrst->enabled_wake = 1;
441 enable_irq_wake(mrst->irq);
442 }
443
444 dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
445 (tmp & RTC_AIE) ? ", alarm may wake" : "",
446 tmp);
447
448 return 0;
449}
450
451/*
452 * We want RTC alarms to wake us from the deep power saving state
453 */
454static inline int mrst_poweroff(struct device *dev)
455{
456 return mrst_suspend(dev, PMSG_HIBERNATE);
457}
458
459static int mrst_resume(struct device *dev)
460{
461 struct mrst_rtc *mrst = dev_get_drvdata(dev);
462 unsigned char tmp = mrst->suspend_ctrl;
463
464 /* Re-enable any irqs previously active */
465 if (tmp & RTC_IRQMASK) {
466 unsigned char mask;
467
468 if (mrst->enabled_wake) {
469 disable_irq_wake(mrst->irq);
470 mrst->enabled_wake = 0;
471 }
472
473 spin_lock_irq(&rtc_lock);
474 do {
475 vrtc_cmos_write(tmp, RTC_CONTROL);
476
477 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
478 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
479 if (!is_intr(mask))
480 break;
481
482 rtc_update_irq(mrst->rtc, 1, mask);
483 tmp &= ~RTC_AIE;
484 } while (mask & RTC_AIE);
485 spin_unlock_irq(&rtc_lock);
486 }
487
488 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
489
490 return 0;
491}
492
493#else
494#define mrst_suspend NULL
495#define mrst_resume NULL
496
497static inline int mrst_poweroff(struct device *dev)
498{
499 return -ENOSYS;
500}
501
502#endif
503
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800504static int vrtc_mrst_platform_probe(struct platform_device *pdev)
Feng Tang0146f262010-11-10 17:29:17 +0000505{
506 return vrtc_mrst_do_probe(&pdev->dev,
507 platform_get_resource(pdev, IORESOURCE_MEM, 0),
508 platform_get_irq(pdev, 0));
509}
510
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800511static int vrtc_mrst_platform_remove(struct platform_device *pdev)
Feng Tang0146f262010-11-10 17:29:17 +0000512{
513 rtc_mrst_do_remove(&pdev->dev);
514 return 0;
515}
516
517static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
518{
519 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
520 return;
521
522 rtc_mrst_do_shutdown();
523}
524
525MODULE_ALIAS("platform:vrtc_mrst");
526
527static struct platform_driver vrtc_mrst_platform_driver = {
528 .probe = vrtc_mrst_platform_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800529 .remove = vrtc_mrst_platform_remove,
Feng Tang0146f262010-11-10 17:29:17 +0000530 .shutdown = vrtc_mrst_platform_shutdown,
531 .driver = {
532 .name = (char *) driver_name,
533 .suspend = mrst_suspend,
534 .resume = mrst_resume,
535 }
536};
537
Axel Lin0c4eae62012-01-10 15:10:48 -0800538module_platform_driver(vrtc_mrst_platform_driver);
Feng Tang0146f262010-11-10 17:29:17 +0000539
540MODULE_AUTHOR("Jacob Pan; Feng Tang");
541MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
542MODULE_LICENSE("GPL");