2 * intel_mid_vrtc.c: Driver for virtual RTC device on Intel MID platform
4 * (C) Copyright 2009 Intel Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
12 * VRTC is emulated by system controller firmware, the real HW
13 * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
14 * in a memory mapped IO space that is visible to the host IA
17 * This driver is based on RTC CMOS driver.
20 #include <linux/kernel.h>
21 #include <linux/export.h>
22 #include <linux/init.h>
23 #include <linux/sfi.h>
24 #include <linux/platform_device.h>
25 #include <linux/mc146818rtc.h>
27 #include <asm/intel-mid.h>
28 #include <asm/intel_mid_vrtc.h>
30 #include <asm/fixmap.h>
32 static unsigned char __iomem
*vrtc_virt_base
;
34 unsigned char vrtc_cmos_read(unsigned char reg
)
38 /* vRTC's registers range from 0x0 to 0xD */
39 if (reg
> 0xd || !vrtc_virt_base
)
42 lock_cmos_prefix(reg
);
43 retval
= __raw_readb(vrtc_virt_base
+ (reg
<< 2));
44 lock_cmos_suffix(reg
);
47 EXPORT_SYMBOL_GPL(vrtc_cmos_read
);
49 void vrtc_cmos_write(unsigned char val
, unsigned char reg
)
51 if (reg
> 0xd || !vrtc_virt_base
)
54 lock_cmos_prefix(reg
);
55 __raw_writeb(val
, vrtc_virt_base
+ (reg
<< 2));
56 lock_cmos_suffix(reg
);
58 EXPORT_SYMBOL_GPL(vrtc_cmos_write
);
60 void vrtc_get_time(struct timespec64
*now
)
62 u8 sec
, min
, hour
, mday
, mon
;
66 spin_lock_irqsave(&rtc_lock
, flags
);
68 while ((vrtc_cmos_read(RTC_FREQ_SELECT
) & RTC_UIP
))
71 sec
= vrtc_cmos_read(RTC_SECONDS
);
72 min
= vrtc_cmos_read(RTC_MINUTES
);
73 hour
= vrtc_cmos_read(RTC_HOURS
);
74 mday
= vrtc_cmos_read(RTC_DAY_OF_MONTH
);
75 mon
= vrtc_cmos_read(RTC_MONTH
);
76 year
= vrtc_cmos_read(RTC_YEAR
);
78 spin_unlock_irqrestore(&rtc_lock
, flags
);
80 /* vRTC YEAR reg contains the offset to 1972 */
83 pr_info("vRTC: sec: %d min: %d hour: %d day: %d "
84 "mon: %d year: %d\n", sec
, min
, hour
, mday
, mon
, year
);
86 now
->tv_sec
= mktime64(year
, mon
, mday
, hour
, min
, sec
);
90 int vrtc_set_mmss(const struct timespec64
*now
)
97 rtc_time64_to_tm(now
->tv_sec
, &tm
);
98 if (!rtc_valid_tm(&tm
) && tm
.tm_year
>= 72) {
100 * tm.year is the number of years since 1900, and the
101 * vrtc need the years since 1972.
103 year
= tm
.tm_year
- 72;
104 spin_lock_irqsave(&rtc_lock
, flags
);
105 vrtc_cmos_write(year
, RTC_YEAR
);
106 vrtc_cmos_write(tm
.tm_mon
, RTC_MONTH
);
107 vrtc_cmos_write(tm
.tm_mday
, RTC_DAY_OF_MONTH
);
108 vrtc_cmos_write(tm
.tm_hour
, RTC_HOURS
);
109 vrtc_cmos_write(tm
.tm_min
, RTC_MINUTES
);
110 vrtc_cmos_write(tm
.tm_sec
, RTC_SECONDS
);
111 spin_unlock_irqrestore(&rtc_lock
, flags
);
113 pr_err("%s: Invalid vRTC value: write of %llx to vRTC failed\n",
114 __func__
, (s64
)now
->tv_sec
);
120 void __init
intel_mid_rtc_init(void)
122 unsigned long vrtc_paddr
;
124 sfi_table_parse(SFI_SIG_MRTC
, NULL
, NULL
, sfi_parse_mrtc
);
126 vrtc_paddr
= sfi_mrtc_array
[0].phys_addr
;
127 if (!sfi_mrtc_num
|| !vrtc_paddr
)
130 vrtc_virt_base
= (void __iomem
*)set_fixmap_offset_nocache(FIX_LNW_VRTC
,
132 x86_platform
.get_wallclock
= vrtc_get_time
;
133 x86_platform
.set_wallclock
= vrtc_set_mmss
;
137 * The Moorestown platform has a memory mapped virtual RTC device that emulates
138 * the programming interface of the RTC.
141 static struct resource vrtc_resources
[] = {
143 .flags
= IORESOURCE_MEM
,
146 .flags
= IORESOURCE_IRQ
,
150 static struct platform_device vrtc_device
= {
153 .resource
= vrtc_resources
,
154 .num_resources
= ARRAY_SIZE(vrtc_resources
),
157 /* Register the RTC device if appropriate */
158 static int __init
intel_mid_device_create(void)
160 /* No Moorestown, no device */
161 if (!intel_mid_identify_cpu())
163 /* No timer, no device */
168 vrtc_resources
[0].start
= sfi_mrtc_array
[0].phys_addr
;
169 vrtc_resources
[0].end
= sfi_mrtc_array
[0].phys_addr
+
172 vrtc_resources
[1].start
= sfi_mrtc_array
[0].irq
;
173 vrtc_resources
[1].end
= sfi_mrtc_array
[0].irq
;
175 return platform_device_register(&vrtc_device
);
177 device_initcall(intel_mid_device_create
);