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