2 * 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/init.h>
22 #include <linux/sfi.h>
23 #include <linux/platform_device.h>
26 #include <asm/mrst-vrtc.h>
28 #include <asm/fixmap.h>
30 static unsigned char __iomem
*vrtc_virt_base
;
32 unsigned char vrtc_cmos_read(unsigned char reg
)
36 /* vRTC's registers range from 0x0 to 0xD */
37 if (reg
> 0xd || !vrtc_virt_base
)
40 lock_cmos_prefix(reg
);
41 retval
= __raw_readb(vrtc_virt_base
+ (reg
<< 2));
42 lock_cmos_suffix(reg
);
45 EXPORT_SYMBOL_GPL(vrtc_cmos_read
);
47 void vrtc_cmos_write(unsigned char val
, unsigned char reg
)
49 if (reg
> 0xd || !vrtc_virt_base
)
52 lock_cmos_prefix(reg
);
53 __raw_writeb(val
, vrtc_virt_base
+ (reg
<< 2));
54 lock_cmos_suffix(reg
);
56 EXPORT_SYMBOL_GPL(vrtc_cmos_write
);
58 unsigned long vrtc_get_time(void)
60 u8 sec
, min
, hour
, mday
, mon
;
63 while ((vrtc_cmos_read(RTC_FREQ_SELECT
) & RTC_UIP
))
66 sec
= vrtc_cmos_read(RTC_SECONDS
);
67 min
= vrtc_cmos_read(RTC_MINUTES
);
68 hour
= vrtc_cmos_read(RTC_HOURS
);
69 mday
= vrtc_cmos_read(RTC_DAY_OF_MONTH
);
70 mon
= vrtc_cmos_read(RTC_MONTH
);
71 year
= vrtc_cmos_read(RTC_YEAR
);
73 /* vRTC YEAR reg contains the offset to 1960 */
76 printk(KERN_INFO
"vRTC: sec: %d min: %d hour: %d day: %d "
77 "mon: %d year: %d\n", sec
, min
, hour
, mday
, mon
, year
);
79 return mktime(year
, mon
, mday
, hour
, min
, sec
);
82 /* Only care about the minutes and seconds */
83 int vrtc_set_mmss(unsigned long nowtime
)
85 int real_sec
, real_min
;
88 vrtc_min
= vrtc_cmos_read(RTC_MINUTES
);
90 real_sec
= nowtime
% 60;
91 real_min
= nowtime
/ 60;
92 if (((abs(real_min
- vrtc_min
) + 15)/30) & 1)
96 vrtc_cmos_write(real_sec
, RTC_SECONDS
);
97 vrtc_cmos_write(real_min
, RTC_MINUTES
);
101 void __init
mrst_rtc_init(void)
103 unsigned long vrtc_paddr
;
105 sfi_table_parse(SFI_SIG_MRTC
, NULL
, NULL
, sfi_parse_mrtc
);
107 vrtc_paddr
= sfi_mrtc_array
[0].phys_addr
;
108 if (!sfi_mrtc_num
|| !vrtc_paddr
)
111 vrtc_virt_base
= (void __iomem
*)set_fixmap_offset_nocache(FIX_LNW_VRTC
,
113 x86_platform
.get_wallclock
= vrtc_get_time
;
114 x86_platform
.set_wallclock
= vrtc_set_mmss
;
118 * The Moorestown platform has a memory mapped virtual RTC device that emulates
119 * the programming interface of the RTC.
122 static struct resource vrtc_resources
[] = {
124 .flags
= IORESOURCE_MEM
,
127 .flags
= IORESOURCE_IRQ
,
131 static struct platform_device vrtc_device
= {
134 .resource
= vrtc_resources
,
135 .num_resources
= ARRAY_SIZE(vrtc_resources
),
138 /* Register the RTC device if appropriate */
139 static int __init
mrst_device_create(void)
141 /* No Moorestown, no device */
142 if (!mrst_identify_cpu())
144 /* No timer, no device */
149 vrtc_resources
[0].start
= sfi_mrtc_array
[0].phys_addr
;
150 vrtc_resources
[0].end
= sfi_mrtc_array
[0].phys_addr
+
153 vrtc_resources
[1].start
= sfi_mrtc_array
[0].irq
;
154 vrtc_resources
[1].end
= sfi_mrtc_array
[0].irq
;
156 return platform_device_register(&vrtc_device
);
159 module_init(mrst_device_create
);