Merge branch 'tip/perf/urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/roste...
[deliverable/linux.git] / arch / x86 / platform / mrst / vrtc.c
1 /*
2 * vrtc.c: Driver for virtual RTC device on Intel MID platform
3 *
4 * (C) Copyright 2009 Intel Corporation
5 *
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.
10 *
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.
16 *
17 * This driver is based on RTC CMOS driver.
18 */
19
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sfi.h>
23 #include <linux/platform_device.h>
24
25 #include <asm/mrst.h>
26 #include <asm/mrst-vrtc.h>
27 #include <asm/time.h>
28 #include <asm/fixmap.h>
29
30 static unsigned char __iomem *vrtc_virt_base;
31
32 unsigned char vrtc_cmos_read(unsigned char reg)
33 {
34 unsigned char retval;
35
36 /* vRTC's registers range from 0x0 to 0xD */
37 if (reg > 0xd || !vrtc_virt_base)
38 return 0xff;
39
40 lock_cmos_prefix(reg);
41 retval = __raw_readb(vrtc_virt_base + (reg << 2));
42 lock_cmos_suffix(reg);
43 return retval;
44 }
45 EXPORT_SYMBOL_GPL(vrtc_cmos_read);
46
47 void vrtc_cmos_write(unsigned char val, unsigned char reg)
48 {
49 if (reg > 0xd || !vrtc_virt_base)
50 return;
51
52 lock_cmos_prefix(reg);
53 __raw_writeb(val, vrtc_virt_base + (reg << 2));
54 lock_cmos_suffix(reg);
55 }
56 EXPORT_SYMBOL_GPL(vrtc_cmos_write);
57
58 unsigned long vrtc_get_time(void)
59 {
60 u8 sec, min, hour, mday, mon;
61 unsigned long flags;
62 u32 year;
63
64 spin_lock_irqsave(&rtc_lock, flags);
65
66 while ((vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP))
67 cpu_relax();
68
69 sec = vrtc_cmos_read(RTC_SECONDS);
70 min = vrtc_cmos_read(RTC_MINUTES);
71 hour = vrtc_cmos_read(RTC_HOURS);
72 mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
73 mon = vrtc_cmos_read(RTC_MONTH);
74 year = vrtc_cmos_read(RTC_YEAR);
75
76 spin_unlock_irqrestore(&rtc_lock, flags);
77
78 /* vRTC YEAR reg contains the offset to 1960 */
79 year += 1960;
80
81 printk(KERN_INFO "vRTC: sec: %d min: %d hour: %d day: %d "
82 "mon: %d year: %d\n", sec, min, hour, mday, mon, year);
83
84 return mktime(year, mon, mday, hour, min, sec);
85 }
86
87 /* Only care about the minutes and seconds */
88 int vrtc_set_mmss(unsigned long nowtime)
89 {
90 int real_sec, real_min;
91 unsigned long flags;
92 int vrtc_min;
93
94 spin_lock_irqsave(&rtc_lock, flags);
95 vrtc_min = vrtc_cmos_read(RTC_MINUTES);
96
97 real_sec = nowtime % 60;
98 real_min = nowtime / 60;
99 if (((abs(real_min - vrtc_min) + 15)/30) & 1)
100 real_min += 30;
101 real_min %= 60;
102
103 vrtc_cmos_write(real_sec, RTC_SECONDS);
104 vrtc_cmos_write(real_min, RTC_MINUTES);
105 spin_unlock_irqrestore(&rtc_lock, flags);
106
107 return 0;
108 }
109
110 void __init mrst_rtc_init(void)
111 {
112 unsigned long vrtc_paddr;
113
114 sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
115
116 vrtc_paddr = sfi_mrtc_array[0].phys_addr;
117 if (!sfi_mrtc_num || !vrtc_paddr)
118 return;
119
120 vrtc_virt_base = (void __iomem *)set_fixmap_offset_nocache(FIX_LNW_VRTC,
121 vrtc_paddr);
122 x86_platform.get_wallclock = vrtc_get_time;
123 x86_platform.set_wallclock = vrtc_set_mmss;
124 }
125
126 /*
127 * The Moorestown platform has a memory mapped virtual RTC device that emulates
128 * the programming interface of the RTC.
129 */
130
131 static struct resource vrtc_resources[] = {
132 [0] = {
133 .flags = IORESOURCE_MEM,
134 },
135 [1] = {
136 .flags = IORESOURCE_IRQ,
137 }
138 };
139
140 static struct platform_device vrtc_device = {
141 .name = "rtc_mrst",
142 .id = -1,
143 .resource = vrtc_resources,
144 .num_resources = ARRAY_SIZE(vrtc_resources),
145 };
146
147 /* Register the RTC device if appropriate */
148 static int __init mrst_device_create(void)
149 {
150 /* No Moorestown, no device */
151 if (!mrst_identify_cpu())
152 return -ENODEV;
153 /* No timer, no device */
154 if (!sfi_mrtc_num)
155 return -ENODEV;
156
157 /* iomem resource */
158 vrtc_resources[0].start = sfi_mrtc_array[0].phys_addr;
159 vrtc_resources[0].end = sfi_mrtc_array[0].phys_addr +
160 MRST_VRTC_MAP_SZ;
161 /* irq resource */
162 vrtc_resources[1].start = sfi_mrtc_array[0].irq;
163 vrtc_resources[1].end = sfi_mrtc_array[0].irq;
164
165 return platform_device_register(&vrtc_device);
166 }
167
168 module_init(mrst_device_create);
This page took 0.035853 seconds and 6 git commands to generate.