1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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#include <linux/mc146818rtc.h>
26
27#include <asm/intel-mid.h>
28#include <asm/intel_mid_vrtc.h>
29#include <asm/time.h>
30#include <asm/fixmap.h>
31
32static unsigned char __iomem *vrtc_virt_base;
33
34unsigned char vrtc_cmos_read(unsigned char reg)
35{
36 unsigned char retval;
37
38
39 if (reg > 0xd || !vrtc_virt_base)
40 return 0xff;
41
42 lock_cmos_prefix(reg);
43 retval = __raw_readb(vrtc_virt_base + (reg << 2));
44 lock_cmos_suffix(reg);
45 return retval;
46}
47EXPORT_SYMBOL_GPL(vrtc_cmos_read);
48
49void vrtc_cmos_write(unsigned char val, unsigned char reg)
50{
51 if (reg > 0xd || !vrtc_virt_base)
52 return;
53
54 lock_cmos_prefix(reg);
55 __raw_writeb(val, vrtc_virt_base + (reg << 2));
56 lock_cmos_suffix(reg);
57}
58EXPORT_SYMBOL_GPL(vrtc_cmos_write);
59
60void vrtc_get_time(struct timespec64 *now)
61{
62 u8 sec, min, hour, mday, mon;
63 unsigned long flags;
64 u32 year;
65
66 spin_lock_irqsave(&rtc_lock, flags);
67
68 while ((vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP))
69 cpu_relax();
70
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);
77
78 spin_unlock_irqrestore(&rtc_lock, flags);
79
80
81 year += 1972;
82
83 pr_info("vRTC: sec: %d min: %d hour: %d day: %d "
84 "mon: %d year: %d\n", sec, min, hour, mday, mon, year);
85
86 now->tv_sec = mktime64(year, mon, mday, hour, min, sec);
87 now->tv_nsec = 0;
88}
89
90int vrtc_set_mmss(const struct timespec64 *now)
91{
92 unsigned long flags;
93 struct rtc_time tm;
94 int year;
95 int retval = 0;
96
97 rtc_time64_to_tm(now->tv_sec, &tm);
98 if (!rtc_valid_tm(&tm) && tm.tm_year >= 72) {
99
100
101
102
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);
112 } else {
113 pr_err("%s: Invalid vRTC value: write of %llx to vRTC failed\n",
114 __func__, (s64)now->tv_sec);
115 retval = -EINVAL;
116 }
117 return retval;
118}
119
120void __init intel_mid_rtc_init(void)
121{
122 unsigned long vrtc_paddr;
123
124 sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
125
126 vrtc_paddr = sfi_mrtc_array[0].phys_addr;
127 if (!sfi_mrtc_num || !vrtc_paddr)
128 return;
129
130 vrtc_virt_base = (void __iomem *)set_fixmap_offset_nocache(FIX_LNW_VRTC,
131 vrtc_paddr);
132 x86_platform.get_wallclock = vrtc_get_time;
133 x86_platform.set_wallclock = vrtc_set_mmss;
134}
135
136
137
138
139
140
141static struct resource vrtc_resources[] = {
142 [0] = {
143 .flags = IORESOURCE_MEM,
144 },
145 [1] = {
146 .flags = IORESOURCE_IRQ,
147 }
148};
149
150static struct platform_device vrtc_device = {
151 .name = "rtc_mrst",
152 .id = -1,
153 .resource = vrtc_resources,
154 .num_resources = ARRAY_SIZE(vrtc_resources),
155};
156
157
158static int __init intel_mid_device_create(void)
159{
160
161 if (!intel_mid_identify_cpu())
162 return -ENODEV;
163
164 if (!sfi_mrtc_num)
165 return -ENODEV;
166
167
168 vrtc_resources[0].start = sfi_mrtc_array[0].phys_addr;
169 vrtc_resources[0].end = sfi_mrtc_array[0].phys_addr +
170 MRST_VRTC_MAP_SZ;
171
172 vrtc_resources[1].start = sfi_mrtc_array[0].irq;
173 vrtc_resources[1].end = sfi_mrtc_array[0].irq;
174
175 return platform_device_register(&vrtc_device);
176}
177device_initcall(intel_mid_device_create);
178