1
2
3
4#include <linux/platform_device.h>
5#include <linux/mc146818rtc.h>
6#include <linux/acpi.h>
7#include <linux/bcd.h>
8#include <linux/pnp.h>
9#include <linux/of.h>
10
11#include <asm/vsyscall.h>
12#include <asm/x86_init.h>
13#include <asm/time.h>
14
15#ifdef CONFIG_X86_32
16
17
18
19
20
21volatile unsigned long cmos_lock;
22EXPORT_SYMBOL(cmos_lock);
23#endif
24
25
26#define CMOS_YEARS_OFFS 2000
27
28DEFINE_SPINLOCK(rtc_lock);
29EXPORT_SYMBOL(rtc_lock);
30
31
32
33
34
35
36
37
38
39
40
41int mach_set_rtc_mmss(unsigned long nowtime)
42{
43 int real_seconds, real_minutes, cmos_minutes;
44 unsigned char save_control, save_freq_select;
45 int retval = 0;
46
47
48 save_control = CMOS_READ(RTC_CONTROL);
49 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
50
51
52 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
53 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
54
55 cmos_minutes = CMOS_READ(RTC_MINUTES);
56 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
57 cmos_minutes = bcd2bin(cmos_minutes);
58
59
60
61
62
63
64
65 real_seconds = nowtime % 60;
66 real_minutes = nowtime / 60;
67
68 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
69 real_minutes += 30;
70 real_minutes %= 60;
71
72 if (abs(real_minutes - cmos_minutes) < 30) {
73 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
74 real_seconds = bin2bcd(real_seconds);
75 real_minutes = bin2bcd(real_minutes);
76 }
77 CMOS_WRITE(real_seconds, RTC_SECONDS);
78 CMOS_WRITE(real_minutes, RTC_MINUTES);
79 } else {
80 printk_once(KERN_NOTICE
81 "set_rtc_mmss: can't update from %d to %d\n",
82 cmos_minutes, real_minutes);
83 retval = -1;
84 }
85
86
87
88
89
90
91
92
93 CMOS_WRITE(save_control, RTC_CONTROL);
94 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
95
96 return retval;
97}
98
99unsigned long mach_get_cmos_time(void)
100{
101 unsigned int status, year, mon, day, hour, min, sec, century = 0;
102
103
104
105
106
107
108
109 while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
110 cpu_relax();
111
112 sec = CMOS_READ(RTC_SECONDS);
113 min = CMOS_READ(RTC_MINUTES);
114 hour = CMOS_READ(RTC_HOURS);
115 day = CMOS_READ(RTC_DAY_OF_MONTH);
116 mon = CMOS_READ(RTC_MONTH);
117 year = CMOS_READ(RTC_YEAR);
118
119#ifdef CONFIG_ACPI
120 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
121 acpi_gbl_FADT.century)
122 century = CMOS_READ(acpi_gbl_FADT.century);
123#endif
124
125 status = CMOS_READ(RTC_CONTROL);
126 WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
127
128 if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
129 sec = bcd2bin(sec);
130 min = bcd2bin(min);
131 hour = bcd2bin(hour);
132 day = bcd2bin(day);
133 mon = bcd2bin(mon);
134 year = bcd2bin(year);
135 }
136
137 if (century) {
138 century = bcd2bin(century);
139 year += century * 100;
140 printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
141 } else
142 year += CMOS_YEARS_OFFS;
143
144 return mktime(year, mon, day, hour, min, sec);
145}
146
147
148unsigned char rtc_cmos_read(unsigned char addr)
149{
150 unsigned char val;
151
152 lock_cmos_prefix(addr);
153 outb(addr, RTC_PORT(0));
154 val = inb(RTC_PORT(1));
155 lock_cmos_suffix(addr);
156
157 return val;
158}
159EXPORT_SYMBOL(rtc_cmos_read);
160
161void rtc_cmos_write(unsigned char val, unsigned char addr)
162{
163 lock_cmos_prefix(addr);
164 outb(addr, RTC_PORT(0));
165 outb(val, RTC_PORT(1));
166 lock_cmos_suffix(addr);
167}
168EXPORT_SYMBOL(rtc_cmos_write);
169
170int update_persistent_clock(struct timespec now)
171{
172 unsigned long flags;
173 int retval;
174
175 spin_lock_irqsave(&rtc_lock, flags);
176 retval = x86_platform.set_wallclock(now.tv_sec);
177 spin_unlock_irqrestore(&rtc_lock, flags);
178
179 return retval;
180}
181
182
183void read_persistent_clock(struct timespec *ts)
184{
185 unsigned long retval, flags;
186
187 spin_lock_irqsave(&rtc_lock, flags);
188 retval = x86_platform.get_wallclock();
189 spin_unlock_irqrestore(&rtc_lock, flags);
190
191 ts->tv_sec = retval;
192 ts->tv_nsec = 0;
193}
194
195unsigned long long native_read_tsc(void)
196{
197 return __native_read_tsc();
198}
199EXPORT_SYMBOL(native_read_tsc);
200
201
202static struct resource rtc_resources[] = {
203 [0] = {
204 .start = RTC_PORT(0),
205 .end = RTC_PORT(1),
206 .flags = IORESOURCE_IO,
207 },
208 [1] = {
209 .start = RTC_IRQ,
210 .end = RTC_IRQ,
211 .flags = IORESOURCE_IRQ,
212 }
213};
214
215static struct platform_device rtc_device = {
216 .name = "rtc_cmos",
217 .id = -1,
218 .resource = rtc_resources,
219 .num_resources = ARRAY_SIZE(rtc_resources),
220};
221
222static __init int add_rtc_cmos(void)
223{
224#ifdef CONFIG_PNP
225 static const char *ids[] __initconst =
226 { "PNP0b00", "PNP0b01", "PNP0b02", };
227 struct pnp_dev *dev;
228 struct pnp_id *id;
229 int i;
230
231 pnp_for_each_dev(dev) {
232 for (id = dev->id; id; id = id->next) {
233 for (i = 0; i < ARRAY_SIZE(ids); i++) {
234 if (compare_pnp_id(id, ids[i]) != 0)
235 return 0;
236 }
237 }
238 }
239#endif
240 if (of_have_populated_dt())
241 return 0;
242
243 platform_device_register(&rtc_device);
244 dev_info(&rtc_device.dev,
245 "registered platform RTC device (no PNP device found)\n");
246
247 return 0;
248}
249device_initcall(add_rtc_cmos);
250