Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / arch / x86 / platform / intel-mid / intel_mid_vrtc.c
blob4762cff7facd0293c63317144118195b453de2f9
1 /*
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
9 * of the License.
11 * Note:
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
15 * processor.
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>
26 #include <asm/intel-mid.h>
27 #include <asm/intel_mid_vrtc.h>
28 #include <asm/time.h>
29 #include <asm/fixmap.h>
31 static unsigned char __iomem *vrtc_virt_base;
33 unsigned char vrtc_cmos_read(unsigned char reg)
35 unsigned char retval;
37 /* vRTC's registers range from 0x0 to 0xD */
38 if (reg > 0xd || !vrtc_virt_base)
39 return 0xff;
41 lock_cmos_prefix(reg);
42 retval = __raw_readb(vrtc_virt_base + (reg << 2));
43 lock_cmos_suffix(reg);
44 return retval;
46 EXPORT_SYMBOL_GPL(vrtc_cmos_read);
48 void vrtc_cmos_write(unsigned char val, unsigned char reg)
50 if (reg > 0xd || !vrtc_virt_base)
51 return;
53 lock_cmos_prefix(reg);
54 __raw_writeb(val, vrtc_virt_base + (reg << 2));
55 lock_cmos_suffix(reg);
57 EXPORT_SYMBOL_GPL(vrtc_cmos_write);
59 void vrtc_get_time(struct timespec *now)
61 u8 sec, min, hour, mday, mon;
62 unsigned long flags;
63 u32 year;
65 spin_lock_irqsave(&rtc_lock, flags);
67 while ((vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP))
68 cpu_relax();
70 sec = vrtc_cmos_read(RTC_SECONDS);
71 min = vrtc_cmos_read(RTC_MINUTES);
72 hour = vrtc_cmos_read(RTC_HOURS);
73 mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
74 mon = vrtc_cmos_read(RTC_MONTH);
75 year = vrtc_cmos_read(RTC_YEAR);
77 spin_unlock_irqrestore(&rtc_lock, flags);
79 /* vRTC YEAR reg contains the offset to 1972 */
80 year += 1972;
82 pr_info("vRTC: sec: %d min: %d hour: %d day: %d "
83 "mon: %d year: %d\n", sec, min, hour, mday, mon, year);
85 now->tv_sec = mktime(year, mon, mday, hour, min, sec);
86 now->tv_nsec = 0;
89 int vrtc_set_mmss(const struct timespec *now)
91 unsigned long flags;
92 struct rtc_time tm;
93 int year;
94 int retval = 0;
96 rtc_time_to_tm(now->tv_sec, &tm);
97 if (!rtc_valid_tm(&tm) && tm.tm_year >= 72) {
99 * tm.year is the number of years since 1900, and the
100 * vrtc need the years since 1972.
102 year = tm.tm_year - 72;
103 spin_lock_irqsave(&rtc_lock, flags);
104 vrtc_cmos_write(year, RTC_YEAR);
105 vrtc_cmos_write(tm.tm_mon, RTC_MONTH);
106 vrtc_cmos_write(tm.tm_mday, RTC_DAY_OF_MONTH);
107 vrtc_cmos_write(tm.tm_hour, RTC_HOURS);
108 vrtc_cmos_write(tm.tm_min, RTC_MINUTES);
109 vrtc_cmos_write(tm.tm_sec, RTC_SECONDS);
110 spin_unlock_irqrestore(&rtc_lock, flags);
111 } else {
112 pr_err("%s: Invalid vRTC value: write of %lx to vRTC failed\n",
113 __FUNCTION__, now->tv_sec);
114 retval = -EINVAL;
116 return retval;
119 void __init intel_mid_rtc_init(void)
121 unsigned long vrtc_paddr;
123 sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
125 vrtc_paddr = sfi_mrtc_array[0].phys_addr;
126 if (!sfi_mrtc_num || !vrtc_paddr)
127 return;
129 vrtc_virt_base = (void __iomem *)set_fixmap_offset_nocache(FIX_LNW_VRTC,
130 vrtc_paddr);
131 x86_platform.get_wallclock = vrtc_get_time;
132 x86_platform.set_wallclock = vrtc_set_mmss;
136 * The Moorestown platform has a memory mapped virtual RTC device that emulates
137 * the programming interface of the RTC.
140 static struct resource vrtc_resources[] = {
141 [0] = {
142 .flags = IORESOURCE_MEM,
144 [1] = {
145 .flags = IORESOURCE_IRQ,
149 static struct platform_device vrtc_device = {
150 .name = "rtc_mrst",
151 .id = -1,
152 .resource = vrtc_resources,
153 .num_resources = ARRAY_SIZE(vrtc_resources),
156 /* Register the RTC device if appropriate */
157 static int __init intel_mid_device_create(void)
159 /* No Moorestown, no device */
160 if (!intel_mid_identify_cpu())
161 return -ENODEV;
162 /* No timer, no device */
163 if (!sfi_mrtc_num)
164 return -ENODEV;
166 /* iomem resource */
167 vrtc_resources[0].start = sfi_mrtc_array[0].phys_addr;
168 vrtc_resources[0].end = sfi_mrtc_array[0].phys_addr +
169 MRST_VRTC_MAP_SZ;
170 /* irq resource */
171 vrtc_resources[1].start = sfi_mrtc_array[0].irq;
172 vrtc_resources[1].end = sfi_mrtc_array[0].irq;
174 return platform_device_register(&vrtc_device);
177 module_init(intel_mid_device_create);