Merge tag '5.9-rc-smb3-fixes-part2' of git://git.samba.org/sfrench/cifs-2.6
[linux/fpc-iii.git] / drivers / rtc / rtc-mrst.c
blob17bf5394e1e5161c1eebcfdbc273c4227601d33d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * rtc-mrst.c: Driver for Moorestown virtual RTC
5 * (C) Copyright 2009 Intel Corporation
6 * Author: Jacob Pan (jacob.jun.pan@intel.com)
7 * Feng Tang (feng.tang@intel.com)
9 * Note:
10 * VRTC is emulated by system controller firmware, the real HW
11 * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
12 * in a memory mapped IO space that is visible to the host IA
13 * processor.
15 * This driver is based upon drivers/rtc/rtc-cmos.c
19 * Note:
20 * * vRTC only supports binary mode and 24H mode
21 * * vRTC only support PIE and AIE, no UIE, and its PIE only happens
22 * at 23:59:59pm everyday, no support for adjustable frequency
23 * * Alarm function is also limited to hr/min/sec.
26 #include <linux/mod_devicetable.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
29 #include <linux/spinlock.h>
30 #include <linux/kernel.h>
31 #include <linux/mc146818rtc.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/sfi.h>
36 #include <asm/intel_scu_ipc.h>
37 #include <asm/intel-mid.h>
38 #include <asm/intel_mid_vrtc.h>
40 struct mrst_rtc {
41 struct rtc_device *rtc;
42 struct device *dev;
43 int irq;
45 u8 enabled_wake;
46 u8 suspend_ctrl;
49 static const char driver_name[] = "rtc_mrst";
51 #define RTC_IRQMASK (RTC_PF | RTC_AF)
53 static inline int is_intr(u8 rtc_intr)
55 if (!(rtc_intr & RTC_IRQF))
56 return 0;
57 return rtc_intr & RTC_IRQMASK;
60 static inline unsigned char vrtc_is_updating(void)
62 unsigned char uip;
63 unsigned long flags;
65 spin_lock_irqsave(&rtc_lock, flags);
66 uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP);
67 spin_unlock_irqrestore(&rtc_lock, flags);
68 return uip;
72 * rtc_time's year contains the increment over 1900, but vRTC's YEAR
73 * register can't be programmed to value larger than 0x64, so vRTC
74 * driver chose to use 1972 (1970 is UNIX time start point) as the base,
75 * and does the translation at read/write time.
77 * Why not just use 1970 as the offset? it's because using 1972 will
78 * make it consistent in leap year setting for both vrtc and low-level
79 * physical rtc devices. Then why not use 1960 as the offset? If we use
80 * 1960, for a device's first use, its YEAR register is 0 and the system
81 * year will be parsed as 1960 which is not a valid UNIX time and will
82 * cause many applications to fail mysteriously.
84 static int mrst_read_time(struct device *dev, struct rtc_time *time)
86 unsigned long flags;
88 if (vrtc_is_updating())
89 msleep(20);
91 spin_lock_irqsave(&rtc_lock, flags);
92 time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
93 time->tm_min = vrtc_cmos_read(RTC_MINUTES);
94 time->tm_hour = vrtc_cmos_read(RTC_HOURS);
95 time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
96 time->tm_mon = vrtc_cmos_read(RTC_MONTH);
97 time->tm_year = vrtc_cmos_read(RTC_YEAR);
98 spin_unlock_irqrestore(&rtc_lock, flags);
100 /* Adjust for the 1972/1900 */
101 time->tm_year += 72;
102 time->tm_mon--;
103 return 0;
106 static int mrst_set_time(struct device *dev, struct rtc_time *time)
108 int ret;
109 unsigned long flags;
110 unsigned char mon, day, hrs, min, sec;
111 unsigned int yrs;
113 yrs = time->tm_year;
114 mon = time->tm_mon + 1; /* tm_mon starts at zero */
115 day = time->tm_mday;
116 hrs = time->tm_hour;
117 min = time->tm_min;
118 sec = time->tm_sec;
120 if (yrs < 72 || yrs > 172)
121 return -EINVAL;
122 yrs -= 72;
124 spin_lock_irqsave(&rtc_lock, flags);
126 vrtc_cmos_write(yrs, RTC_YEAR);
127 vrtc_cmos_write(mon, RTC_MONTH);
128 vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
129 vrtc_cmos_write(hrs, RTC_HOURS);
130 vrtc_cmos_write(min, RTC_MINUTES);
131 vrtc_cmos_write(sec, RTC_SECONDS);
133 spin_unlock_irqrestore(&rtc_lock, flags);
135 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
136 return ret;
139 static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
141 struct mrst_rtc *mrst = dev_get_drvdata(dev);
142 unsigned char rtc_control;
144 if (mrst->irq <= 0)
145 return -EIO;
147 /* vRTC only supports binary mode */
148 spin_lock_irq(&rtc_lock);
149 t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
150 t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
151 t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
153 rtc_control = vrtc_cmos_read(RTC_CONTROL);
154 spin_unlock_irq(&rtc_lock);
156 t->enabled = !!(rtc_control & RTC_AIE);
157 t->pending = 0;
159 return 0;
162 static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
164 unsigned char rtc_intr;
167 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
168 * allegedly some older rtcs need that to handle irqs properly
170 rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
171 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
172 if (is_intr(rtc_intr))
173 rtc_update_irq(mrst->rtc, 1, rtc_intr);
176 static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
178 unsigned char rtc_control;
181 * Flush any pending IRQ status, notably for update irqs,
182 * before we enable new IRQs
184 rtc_control = vrtc_cmos_read(RTC_CONTROL);
185 mrst_checkintr(mrst, rtc_control);
187 rtc_control |= mask;
188 vrtc_cmos_write(rtc_control, RTC_CONTROL);
190 mrst_checkintr(mrst, rtc_control);
193 static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
195 unsigned char rtc_control;
197 rtc_control = vrtc_cmos_read(RTC_CONTROL);
198 rtc_control &= ~mask;
199 vrtc_cmos_write(rtc_control, RTC_CONTROL);
200 mrst_checkintr(mrst, rtc_control);
203 static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
205 struct mrst_rtc *mrst = dev_get_drvdata(dev);
206 unsigned char hrs, min, sec;
207 int ret = 0;
209 if (!mrst->irq)
210 return -EIO;
212 hrs = t->time.tm_hour;
213 min = t->time.tm_min;
214 sec = t->time.tm_sec;
216 spin_lock_irq(&rtc_lock);
217 /* Next rtc irq must not be from previous alarm setting */
218 mrst_irq_disable(mrst, RTC_AIE);
220 /* Update alarm */
221 vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
222 vrtc_cmos_write(min, RTC_MINUTES_ALARM);
223 vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
225 spin_unlock_irq(&rtc_lock);
227 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
228 if (ret)
229 return ret;
231 spin_lock_irq(&rtc_lock);
232 if (t->enabled)
233 mrst_irq_enable(mrst, RTC_AIE);
235 spin_unlock_irq(&rtc_lock);
237 return 0;
240 /* Currently, the vRTC doesn't support UIE ON/OFF */
241 static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
243 struct mrst_rtc *mrst = dev_get_drvdata(dev);
244 unsigned long flags;
246 spin_lock_irqsave(&rtc_lock, flags);
247 if (enabled)
248 mrst_irq_enable(mrst, RTC_AIE);
249 else
250 mrst_irq_disable(mrst, RTC_AIE);
251 spin_unlock_irqrestore(&rtc_lock, flags);
252 return 0;
256 #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
258 static int mrst_procfs(struct device *dev, struct seq_file *seq)
260 unsigned char rtc_control;
262 spin_lock_irq(&rtc_lock);
263 rtc_control = vrtc_cmos_read(RTC_CONTROL);
264 spin_unlock_irq(&rtc_lock);
266 seq_printf(seq,
267 "periodic_IRQ\t: %s\n"
268 "alarm\t\t: %s\n"
269 "BCD\t\t: no\n"
270 "periodic_freq\t: daily (not adjustable)\n",
271 (rtc_control & RTC_PIE) ? "on" : "off",
272 (rtc_control & RTC_AIE) ? "on" : "off");
274 return 0;
277 #else
278 #define mrst_procfs NULL
279 #endif
281 static const struct rtc_class_ops mrst_rtc_ops = {
282 .read_time = mrst_read_time,
283 .set_time = mrst_set_time,
284 .read_alarm = mrst_read_alarm,
285 .set_alarm = mrst_set_alarm,
286 .proc = mrst_procfs,
287 .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
290 static struct mrst_rtc mrst_rtc;
293 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
294 * Reg B, so no need for this driver to clear it
296 static irqreturn_t mrst_rtc_irq(int irq, void *p)
298 u8 irqstat;
300 spin_lock(&rtc_lock);
301 /* This read will clear all IRQ flags inside Reg C */
302 irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
303 spin_unlock(&rtc_lock);
305 irqstat &= RTC_IRQMASK | RTC_IRQF;
306 if (is_intr(irqstat)) {
307 rtc_update_irq(p, 1, irqstat);
308 return IRQ_HANDLED;
310 return IRQ_NONE;
313 static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
314 int rtc_irq)
316 int retval = 0;
317 unsigned char rtc_control;
319 /* There can be only one ... */
320 if (mrst_rtc.dev)
321 return -EBUSY;
323 if (!iomem)
324 return -ENODEV;
326 iomem = devm_request_mem_region(dev, iomem->start, resource_size(iomem),
327 driver_name);
328 if (!iomem) {
329 dev_dbg(dev, "i/o mem already in use.\n");
330 return -EBUSY;
333 mrst_rtc.irq = rtc_irq;
334 mrst_rtc.dev = dev;
335 dev_set_drvdata(dev, &mrst_rtc);
337 mrst_rtc.rtc = devm_rtc_allocate_device(dev);
338 if (IS_ERR(mrst_rtc.rtc))
339 return PTR_ERR(mrst_rtc.rtc);
341 mrst_rtc.rtc->ops = &mrst_rtc_ops;
343 rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
345 spin_lock_irq(&rtc_lock);
346 mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
347 rtc_control = vrtc_cmos_read(RTC_CONTROL);
348 spin_unlock_irq(&rtc_lock);
350 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
351 dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
353 if (rtc_irq) {
354 retval = devm_request_irq(dev, rtc_irq, mrst_rtc_irq,
355 0, dev_name(&mrst_rtc.rtc->dev),
356 mrst_rtc.rtc);
357 if (retval < 0) {
358 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
359 rtc_irq, retval);
360 goto cleanup0;
364 retval = rtc_register_device(mrst_rtc.rtc);
365 if (retval)
366 goto cleanup0;
368 dev_dbg(dev, "initialised\n");
369 return 0;
371 cleanup0:
372 mrst_rtc.dev = NULL;
373 dev_err(dev, "rtc-mrst: unable to initialise\n");
374 return retval;
377 static void rtc_mrst_do_shutdown(void)
379 spin_lock_irq(&rtc_lock);
380 mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
381 spin_unlock_irq(&rtc_lock);
384 static void rtc_mrst_do_remove(struct device *dev)
386 struct mrst_rtc *mrst = dev_get_drvdata(dev);
388 rtc_mrst_do_shutdown();
390 mrst->rtc = NULL;
391 mrst->dev = NULL;
394 #ifdef CONFIG_PM_SLEEP
395 static int mrst_suspend(struct device *dev)
397 struct mrst_rtc *mrst = dev_get_drvdata(dev);
398 unsigned char tmp;
400 /* Only the alarm might be a wakeup event source */
401 spin_lock_irq(&rtc_lock);
402 mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
403 if (tmp & (RTC_PIE | RTC_AIE)) {
404 unsigned char mask;
406 if (device_may_wakeup(dev))
407 mask = RTC_IRQMASK & ~RTC_AIE;
408 else
409 mask = RTC_IRQMASK;
410 tmp &= ~mask;
411 vrtc_cmos_write(tmp, RTC_CONTROL);
413 mrst_checkintr(mrst, tmp);
415 spin_unlock_irq(&rtc_lock);
417 if (tmp & RTC_AIE) {
418 mrst->enabled_wake = 1;
419 enable_irq_wake(mrst->irq);
422 dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
423 (tmp & RTC_AIE) ? ", alarm may wake" : "",
424 tmp);
426 return 0;
430 * We want RTC alarms to wake us from the deep power saving state
432 static inline int mrst_poweroff(struct device *dev)
434 return mrst_suspend(dev);
437 static int mrst_resume(struct device *dev)
439 struct mrst_rtc *mrst = dev_get_drvdata(dev);
440 unsigned char tmp = mrst->suspend_ctrl;
442 /* Re-enable any irqs previously active */
443 if (tmp & RTC_IRQMASK) {
444 unsigned char mask;
446 if (mrst->enabled_wake) {
447 disable_irq_wake(mrst->irq);
448 mrst->enabled_wake = 0;
451 spin_lock_irq(&rtc_lock);
452 do {
453 vrtc_cmos_write(tmp, RTC_CONTROL);
455 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
456 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
457 if (!is_intr(mask))
458 break;
460 rtc_update_irq(mrst->rtc, 1, mask);
461 tmp &= ~RTC_AIE;
462 } while (mask & RTC_AIE);
463 spin_unlock_irq(&rtc_lock);
466 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
468 return 0;
471 static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
472 #define MRST_PM_OPS (&mrst_pm_ops)
474 #else
475 #define MRST_PM_OPS NULL
477 static inline int mrst_poweroff(struct device *dev)
479 return -ENOSYS;
482 #endif
484 static int vrtc_mrst_platform_probe(struct platform_device *pdev)
486 return vrtc_mrst_do_probe(&pdev->dev,
487 platform_get_resource(pdev, IORESOURCE_MEM, 0),
488 platform_get_irq(pdev, 0));
491 static int vrtc_mrst_platform_remove(struct platform_device *pdev)
493 rtc_mrst_do_remove(&pdev->dev);
494 return 0;
497 static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
499 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
500 return;
502 rtc_mrst_do_shutdown();
505 MODULE_ALIAS("platform:vrtc_mrst");
507 static struct platform_driver vrtc_mrst_platform_driver = {
508 .probe = vrtc_mrst_platform_probe,
509 .remove = vrtc_mrst_platform_remove,
510 .shutdown = vrtc_mrst_platform_shutdown,
511 .driver = {
512 .name = driver_name,
513 .pm = MRST_PM_OPS,
517 module_platform_driver(vrtc_mrst_platform_driver);
519 MODULE_AUTHOR("Jacob Pan; Feng Tang");
520 MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
521 MODULE_LICENSE("GPL");