1 // SPDX-License-Identifier: GPL-2.0-only
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)
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
15 * This driver is based upon drivers/rtc/rtc-cmos.c
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>
41 struct rtc_device
*rtc
;
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
))
57 return rtc_intr
& RTC_IRQMASK
;
60 static inline unsigned char vrtc_is_updating(void)
65 spin_lock_irqsave(&rtc_lock
, flags
);
66 uip
= (vrtc_cmos_read(RTC_FREQ_SELECT
) & RTC_UIP
);
67 spin_unlock_irqrestore(&rtc_lock
, flags
);
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
)
88 if (vrtc_is_updating())
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 */
106 static int mrst_set_time(struct device
*dev
, struct rtc_time
*time
)
110 unsigned char mon
, day
, hrs
, min
, sec
;
114 mon
= time
->tm_mon
+ 1; /* tm_mon starts at zero */
120 if (yrs
< 72 || yrs
> 172)
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
);
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
;
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
);
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
);
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
;
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
);
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
);
231 spin_lock_irq(&rtc_lock
);
233 mrst_irq_enable(mrst
, RTC_AIE
);
235 spin_unlock_irq(&rtc_lock
);
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
);
246 spin_lock_irqsave(&rtc_lock
, flags
);
248 mrst_irq_enable(mrst
, RTC_AIE
);
250 mrst_irq_disable(mrst
, RTC_AIE
);
251 spin_unlock_irqrestore(&rtc_lock
, flags
);
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
);
267 "periodic_IRQ\t: %s\n"
270 "periodic_freq\t: daily (not adjustable)\n",
271 (rtc_control
& RTC_PIE
) ? "on" : "off",
272 (rtc_control
& RTC_AIE
) ? "on" : "off");
278 #define mrst_procfs NULL
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
,
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
)
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
);
313 static int vrtc_mrst_do_probe(struct device
*dev
, struct resource
*iomem
,
317 unsigned char rtc_control
;
319 /* There can be only one ... */
326 iomem
= devm_request_mem_region(dev
, iomem
->start
, resource_size(iomem
),
329 dev_dbg(dev
, "i/o mem already in use.\n");
333 mrst_rtc
.irq
= rtc_irq
;
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");
354 retval
= devm_request_irq(dev
, rtc_irq
, mrst_rtc_irq
,
355 0, dev_name(&mrst_rtc
.rtc
->dev
),
358 dev_dbg(dev
, "IRQ %d is already in use, err %d\n",
364 retval
= rtc_register_device(mrst_rtc
.rtc
);
368 dev_dbg(dev
, "initialised\n");
373 dev_err(dev
, "rtc-mrst: unable to initialise\n");
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();
394 #ifdef CONFIG_PM_SLEEP
395 static int mrst_suspend(struct device
*dev
)
397 struct mrst_rtc
*mrst
= dev_get_drvdata(dev
);
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
)) {
406 if (device_may_wakeup(dev
))
407 mask
= RTC_IRQMASK
& ~RTC_AIE
;
411 vrtc_cmos_write(tmp
, RTC_CONTROL
);
413 mrst_checkintr(mrst
, tmp
);
415 spin_unlock_irq(&rtc_lock
);
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" : "",
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
) {
446 if (mrst
->enabled_wake
) {
447 disable_irq_wake(mrst
->irq
);
448 mrst
->enabled_wake
= 0;
451 spin_lock_irq(&rtc_lock
);
453 vrtc_cmos_write(tmp
, RTC_CONTROL
);
455 mask
= vrtc_cmos_read(RTC_INTR_FLAGS
);
456 mask
&= (tmp
& RTC_IRQMASK
) | RTC_IRQF
;
460 rtc_update_irq(mrst
->rtc
, 1, mask
);
462 } while (mask
& RTC_AIE
);
463 spin_unlock_irq(&rtc_lock
);
466 dev_dbg(&mrst_rtc
.rtc
->dev
, "resume, ctrl %02x\n", tmp
);
471 static SIMPLE_DEV_PM_OPS(mrst_pm_ops
, mrst_suspend
, mrst_resume
);
472 #define MRST_PM_OPS (&mrst_pm_ops)
475 #define MRST_PM_OPS NULL
477 static inline int mrst_poweroff(struct device
*dev
)
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
);
497 static void vrtc_mrst_platform_shutdown(struct platform_device
*pdev
)
499 if (system_state
== SYSTEM_POWER_OFF
&& !mrst_poweroff(&pdev
->dev
))
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
,
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");