]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/i386/kernel/hpet.c
i386: DMI_MATCH patch in reboot.c for SFF Dell OptiPlex 745 - fixes hang on reboot
[mirror_ubuntu-artful-kernel.git] / arch / i386 / kernel / hpet.c
CommitLineData
5d0cf410 1#include <linux/clocksource.h>
e9e2cdb4 2#include <linux/clockchips.h>
5d0cf410
JS
3#include <linux/errno.h>
4#include <linux/hpet.h>
5#include <linux/init.h>
399afa4f
ML
6#include <linux/sysdev.h>
7#include <linux/pm.h>
0655d7c3 8#include <linux/delay.h>
5d0cf410
JS
9
10#include <asm/hpet.h>
11#include <asm/io.h>
12
e9e2cdb4
TG
13extern struct clock_event_device *global_clock_event;
14
7f9f303a 15#define HPET_MASK CLOCKSOURCE_MASK(32)
5d0cf410
JS
16#define HPET_SHIFT 22
17
18/* FSEC = 10^-15 NSEC = 10^-9 */
19#define FSEC_PER_NSEC 1000000
20
e9e2cdb4
TG
21/*
22 * HPET address is set in acpi/boot.c, when an ACPI entry exists
23 */
24unsigned long hpet_address;
25static void __iomem * hpet_virt_address;
26
27static inline unsigned long hpet_readl(unsigned long a)
28{
29 return readl(hpet_virt_address + a);
30}
31
32static inline void hpet_writel(unsigned long d, unsigned long a)
33{
34 writel(d, hpet_virt_address + a);
35}
36
37/*
38 * HPET command line enable / disable
39 */
40static int boot_hpet_disable;
41
42static int __init hpet_setup(char* str)
43{
44 if (str) {
45 if (!strncmp("disable", str, 7))
46 boot_hpet_disable = 1;
47 }
48 return 1;
49}
50__setup("hpet=", hpet_setup);
51
52static inline int is_hpet_capable(void)
53{
54 return (!boot_hpet_disable && hpet_address);
55}
56
57/*
58 * HPET timer interrupt enable / disable
59 */
60static int hpet_legacy_int_enabled;
61
62/**
63 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
64 */
65int is_hpet_enabled(void)
66{
67 return is_hpet_capable() && hpet_legacy_int_enabled;
68}
69
70/*
71 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
72 * timer 0 and timer 1 in case of RTC emulation.
73 */
74#ifdef CONFIG_HPET
75static void hpet_reserve_platform_timers(unsigned long id)
76{
77 struct hpet __iomem *hpet = hpet_virt_address;
78 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
79 unsigned int nrtimers, i;
80 struct hpet_data hd;
81
82 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
83
84 memset(&hd, 0, sizeof (hd));
85 hd.hd_phys_address = hpet_address;
86 hd.hd_address = hpet_virt_address;
87 hd.hd_nirqs = nrtimers;
88 hd.hd_flags = HPET_DATA_PLATFORM;
89 hpet_reserve_timer(&hd, 0);
90
91#ifdef CONFIG_HPET_EMULATE_RTC
92 hpet_reserve_timer(&hd, 1);
93#endif
94
95 hd.hd_irq[0] = HPET_LEGACY_8254;
96 hd.hd_irq[1] = HPET_LEGACY_RTC;
97
98 for (i = 2; i < nrtimers; timer++, i++)
99 hd.hd_irq[i] = (timer->hpet_config & Tn_INT_ROUTE_CNF_MASK) >>
100 Tn_INT_ROUTE_CNF_SHIFT;
101
102 hpet_alloc(&hd);
103
104}
105#else
106static void hpet_reserve_platform_timers(unsigned long id) { }
107#endif
108
109/*
110 * Common hpet info
111 */
112static unsigned long hpet_period;
113
114static void hpet_set_mode(enum clock_event_mode mode,
115 struct clock_event_device *evt);
116static int hpet_next_event(unsigned long delta,
117 struct clock_event_device *evt);
118
119/*
120 * The hpet clock event device
121 */
122static struct clock_event_device hpet_clockevent = {
123 .name = "hpet",
124 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
125 .set_mode = hpet_set_mode,
126 .set_next_event = hpet_next_event,
127 .shift = 32,
128 .irq = 0,
129};
130
131static void hpet_start_counter(void)
132{
133 unsigned long cfg = hpet_readl(HPET_CFG);
134
135 cfg &= ~HPET_CFG_ENABLE;
136 hpet_writel(cfg, HPET_CFG);
137 hpet_writel(0, HPET_COUNTER);
138 hpet_writel(0, HPET_COUNTER + 4);
139 cfg |= HPET_CFG_ENABLE;
140 hpet_writel(cfg, HPET_CFG);
141}
142
143static void hpet_enable_int(void)
144{
145 unsigned long cfg = hpet_readl(HPET_CFG);
146
147 cfg |= HPET_CFG_LEGACY;
148 hpet_writel(cfg, HPET_CFG);
149 hpet_legacy_int_enabled = 1;
150}
151
152static void hpet_set_mode(enum clock_event_mode mode,
153 struct clock_event_device *evt)
154{
155 unsigned long cfg, cmp, now;
156 uint64_t delta;
157
158 switch(mode) {
159 case CLOCK_EVT_MODE_PERIODIC:
160 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
161 delta >>= hpet_clockevent.shift;
162 now = hpet_readl(HPET_COUNTER);
163 cmp = now + (unsigned long) delta;
164 cfg = hpet_readl(HPET_T0_CFG);
165 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
166 HPET_TN_SETVAL | HPET_TN_32BIT;
167 hpet_writel(cfg, HPET_T0_CFG);
168 /*
169 * The first write after writing TN_SETVAL to the
170 * config register sets the counter value, the second
171 * write sets the period.
172 */
173 hpet_writel(cmp, HPET_T0_CMP);
174 udelay(1);
175 hpet_writel((unsigned long) delta, HPET_T0_CMP);
176 break;
177
178 case CLOCK_EVT_MODE_ONESHOT:
179 cfg = hpet_readl(HPET_T0_CFG);
180 cfg &= ~HPET_TN_PERIODIC;
181 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
182 hpet_writel(cfg, HPET_T0_CFG);
183 break;
184
185 case CLOCK_EVT_MODE_UNUSED:
186 case CLOCK_EVT_MODE_SHUTDOWN:
187 cfg = hpet_readl(HPET_T0_CFG);
188 cfg &= ~HPET_TN_ENABLE;
189 hpet_writel(cfg, HPET_T0_CFG);
190 break;
18de5bc4
TG
191
192 case CLOCK_EVT_MODE_RESUME:
193 hpet_enable_int();
194 break;
e9e2cdb4
TG
195 }
196}
197
198static int hpet_next_event(unsigned long delta,
199 struct clock_event_device *evt)
200{
201 unsigned long cnt;
202
203 cnt = hpet_readl(HPET_COUNTER);
204 cnt += delta;
205 hpet_writel(cnt, HPET_T0_CMP);
206
c7f6d15f 207 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
e9e2cdb4
TG
208}
209
6bb74df4
JS
210/*
211 * Clock source related code
212 */
213static cycle_t read_hpet(void)
214{
215 return (cycle_t)hpet_readl(HPET_COUNTER);
216}
217
218static struct clocksource clocksource_hpet = {
219 .name = "hpet",
220 .rating = 250,
221 .read = read_hpet,
222 .mask = HPET_MASK,
223 .shift = HPET_SHIFT,
224 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
18de5bc4 225 .resume = hpet_start_counter,
6bb74df4
JS
226};
227
e9e2cdb4
TG
228/*
229 * Try to setup the HPET timer
230 */
231int __init hpet_enable(void)
232{
233 unsigned long id;
234 uint64_t hpet_freq;
6bb74df4 235 u64 tmp;
e9e2cdb4
TG
236
237 if (!is_hpet_capable())
238 return 0;
239
240 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
241
242 /*
243 * Read the period and check for a sane value:
244 */
245 hpet_period = hpet_readl(HPET_PERIOD);
246 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
247 goto out_nohpet;
248
249 /*
250 * The period is a femto seconds value. We need to calculate the
251 * scaled math multiplication factor for nanosecond to hpet tick
252 * conversion.
253 */
254 hpet_freq = 1000000000000000ULL;
255 do_div(hpet_freq, hpet_period);
256 hpet_clockevent.mult = div_sc((unsigned long) hpet_freq,
257 NSEC_PER_SEC, 32);
258 /* Calculate the min / max delta */
259 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
260 &hpet_clockevent);
261 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
262 &hpet_clockevent);
263
264 /*
265 * Read the HPET ID register to retrieve the IRQ routing
266 * information and the number of channels
267 */
268 id = hpet_readl(HPET_ID);
269
270#ifdef CONFIG_HPET_EMULATE_RTC
271 /*
272 * The legacy routing mode needs at least two channels, tick timer
273 * and the rtc emulation channel.
274 */
275 if (!(id & HPET_ID_NUMBER))
276 goto out_nohpet;
277#endif
278
279 /* Start the counter */
280 hpet_start_counter();
281
6bb74df4
JS
282 /* Initialize and register HPET clocksource
283 *
284 * hpet period is in femto seconds per cycle
285 * so we need to convert this to ns/cyc units
286 * aproximated by mult/2^shift
287 *
288 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
289 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
290 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
291 * (fsec/cyc << shift)/1000000 = mult
292 * (hpet_period << shift)/FSEC_PER_NSEC = mult
293 */
294 tmp = (u64)hpet_period << HPET_SHIFT;
295 do_div(tmp, FSEC_PER_NSEC);
296 clocksource_hpet.mult = (u32)tmp;
297
298 clocksource_register(&clocksource_hpet);
299
e9e2cdb4
TG
300 if (id & HPET_ID_LEGSUP) {
301 hpet_enable_int();
302 hpet_reserve_platform_timers(id);
303 /*
304 * Start hpet with the boot cpu mask and make it
305 * global after the IO_APIC has been initialized.
306 */
307 hpet_clockevent.cpumask =cpumask_of_cpu(0);
308 clockevents_register_device(&hpet_clockevent);
309 global_clock_event = &hpet_clockevent;
310 return 1;
311 }
312 return 0;
5d0cf410 313
e9e2cdb4
TG
314out_nohpet:
315 iounmap(hpet_virt_address);
316 hpet_virt_address = NULL;
399afa4f 317 boot_hpet_disable = 1;
e9e2cdb4
TG
318 return 0;
319}
320
e9e2cdb4
TG
321
322#ifdef CONFIG_HPET_EMULATE_RTC
323
324/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
325 * is enabled, we support RTC interrupt functionality in software.
326 * RTC has 3 kinds of interrupts:
327 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
328 * is updated
329 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
330 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
331 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
332 * (1) and (2) above are implemented using polling at a frequency of
333 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
334 * overhead. (DEFAULT_RTC_INT_FREQ)
335 * For (3), we use interrupts at 64Hz or user specified periodic
336 * frequency, whichever is higher.
337 */
338#include <linux/mc146818rtc.h>
339#include <linux/rtc.h>
340
341#define DEFAULT_RTC_INT_FREQ 64
342#define DEFAULT_RTC_SHIFT 6
343#define RTC_NUM_INTS 1
344
345static unsigned long hpet_rtc_flags;
346static unsigned long hpet_prev_update_sec;
347static struct rtc_time hpet_alarm_time;
348static unsigned long hpet_pie_count;
349static unsigned long hpet_t1_cmp;
350static unsigned long hpet_default_delta;
351static unsigned long hpet_pie_delta;
352static unsigned long hpet_pie_limit;
353
354/*
355 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
356 * is not supported by all HPET implementations for timer 1.
357 *
358 * hpet_rtc_timer_init() is called when the rtc is initialized.
359 */
360int hpet_rtc_timer_init(void)
361{
362 unsigned long cfg, cnt, delta, flags;
363
364 if (!is_hpet_enabled())
365 return 0;
366
367 if (!hpet_default_delta) {
368 uint64_t clc;
369
370 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
371 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
372 hpet_default_delta = (unsigned long) clc;
373 }
374
375 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
376 delta = hpet_default_delta;
377 else
378 delta = hpet_pie_delta;
379
380 local_irq_save(flags);
381
382 cnt = delta + hpet_readl(HPET_COUNTER);
383 hpet_writel(cnt, HPET_T1_CMP);
384 hpet_t1_cmp = cnt;
385
386 cfg = hpet_readl(HPET_T1_CFG);
387 cfg &= ~HPET_TN_PERIODIC;
388 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
389 hpet_writel(cfg, HPET_T1_CFG);
390
391 local_irq_restore(flags);
392
393 return 1;
394}
395
396/*
397 * The functions below are called from rtc driver.
398 * Return 0 if HPET is not being used.
399 * Otherwise do the necessary changes and return 1.
400 */
401int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
402{
403 if (!is_hpet_enabled())
404 return 0;
405
406 hpet_rtc_flags &= ~bit_mask;
407 return 1;
408}
409
410int hpet_set_rtc_irq_bit(unsigned long bit_mask)
411{
412 unsigned long oldbits = hpet_rtc_flags;
413
414 if (!is_hpet_enabled())
415 return 0;
416
417 hpet_rtc_flags |= bit_mask;
418
419 if (!oldbits)
420 hpet_rtc_timer_init();
421
422 return 1;
423}
424
425int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
426 unsigned char sec)
427{
428 if (!is_hpet_enabled())
429 return 0;
430
431 hpet_alarm_time.tm_hour = hrs;
432 hpet_alarm_time.tm_min = min;
433 hpet_alarm_time.tm_sec = sec;
434
435 return 1;
436}
437
438int hpet_set_periodic_freq(unsigned long freq)
439{
440 uint64_t clc;
441
442 if (!is_hpet_enabled())
443 return 0;
444
445 if (freq <= DEFAULT_RTC_INT_FREQ)
446 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
447 else {
448 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
449 do_div(clc, freq);
450 clc >>= hpet_clockevent.shift;
451 hpet_pie_delta = (unsigned long) clc;
452 }
453 return 1;
454}
455
456int hpet_rtc_dropped_irq(void)
457{
458 return is_hpet_enabled();
459}
460
461static void hpet_rtc_timer_reinit(void)
462{
463 unsigned long cfg, delta;
464 int lost_ints = -1;
465
466 if (unlikely(!hpet_rtc_flags)) {
467 cfg = hpet_readl(HPET_T1_CFG);
468 cfg &= ~HPET_TN_ENABLE;
469 hpet_writel(cfg, HPET_T1_CFG);
470 return;
471 }
472
473 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
474 delta = hpet_default_delta;
475 else
476 delta = hpet_pie_delta;
477
478 /*
479 * Increment the comparator value until we are ahead of the
480 * current count.
481 */
482 do {
483 hpet_t1_cmp += delta;
484 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
485 lost_ints++;
486 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
487
488 if (lost_ints) {
489 if (hpet_rtc_flags & RTC_PIE)
490 hpet_pie_count += lost_ints;
491 if (printk_ratelimit())
492 printk(KERN_WARNING "rtc: lost %d interrupts\n",
493 lost_ints);
494 }
495}
496
497irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
498{
499 struct rtc_time curr_time;
500 unsigned long rtc_int_flag = 0;
501
502 hpet_rtc_timer_reinit();
503
504 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
505 rtc_get_rtc_time(&curr_time);
506
507 if (hpet_rtc_flags & RTC_UIE &&
508 curr_time.tm_sec != hpet_prev_update_sec) {
509 rtc_int_flag = RTC_UF;
510 hpet_prev_update_sec = curr_time.tm_sec;
511 }
512
513 if (hpet_rtc_flags & RTC_PIE &&
514 ++hpet_pie_count >= hpet_pie_limit) {
515 rtc_int_flag |= RTC_PF;
516 hpet_pie_count = 0;
517 }
518
519 if (hpet_rtc_flags & RTC_PIE &&
520 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
521 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
522 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
523 rtc_int_flag |= RTC_AF;
524
525 if (rtc_int_flag) {
526 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
527 rtc_interrupt(rtc_int_flag, dev_id);
528 }
529 return IRQ_HANDLED;
530}
531#endif