2 * apb_timer.c: Driver for Langwell APB timers
4 * (C) Copyright 2009 Intel Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
13 * Langwell is the south complex of Intel Moorestown MID platform. There are
14 * eight external timers in total that can be used by the operating system.
15 * The timer information, such as frequency and addresses, is provided to the
17 * Timer interrupts are routed via FW/HW emulated IOAPIC independently via
18 * individual redirection table entries (RTE).
19 * Unlike HPET, there is no master counter, therefore one of the timers are
20 * used as clocksource. The overall allocation looks like:
21 * - timer 0 - NR_CPUs for per cpu timer
22 * - one timer for clocksource
23 * - one timer for watchdog driver.
24 * It is also worth notice that APB timer does not support true one-shot mode,
25 * free-running mode will be used here to emulate one-shot mode.
26 * APB timer can also be used as broadcast timer along with per cpu local APIC
27 * timer, but by default APB timer has higher rating than local APIC timers.
30 #include <linux/clocksource.h>
31 #include <linux/clockchips.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/sysdev.h>
37 #include <linux/pci.h>
38 #include <linux/sfi.h>
39 #include <linux/interrupt.h>
40 #include <linux/cpu.h>
41 #include <linux/irq.h>
43 #include <asm/fixmap.h>
44 #include <asm/apb_timer.h>
46 #define APBT_MASK CLOCKSOURCE_MASK(32)
48 #define APBT_CLOCKEVENT_RATING 150
49 #define APBT_CLOCKSOURCE_RATING 250
50 #define APBT_MIN_DELTA_USEC 200
52 #define EVT_TO_APBT_DEV(evt) container_of(evt, struct apbt_dev, evt)
53 #define APBT_CLOCKEVENT0_NUM (0)
54 #define APBT_CLOCKEVENT1_NUM (1)
55 #define APBT_CLOCKSOURCE_NUM (2)
57 static unsigned long apbt_address;
58 static int apb_timer_block_enabled;
59 static void __iomem *apbt_virt_address;
60 static int phy_cs_timer_id;
63 * Common DW APB timer info
65 static uint64_t apbt_freq;
67 static void apbt_set_mode(enum clock_event_mode mode,
68 struct clock_event_device *evt);
69 static int apbt_next_event(unsigned long delta,
70 struct clock_event_device *evt);
71 static cycle_t apbt_read_clocksource(struct clocksource *cs);
72 static void apbt_restart_clocksource(void);
75 struct clock_event_device evt;
85 int disable_apbt_percpu __cpuinitdata;
87 static DEFINE_PER_CPU(struct apbt_dev, cpu_apbt_dev);
90 static unsigned int apbt_num_timers_used;
91 static struct apbt_dev *apbt_devs;
94 static inline unsigned long apbt_readl_reg(unsigned long a)
96 return readl(apbt_virt_address + a);
99 static inline void apbt_writel_reg(unsigned long d, unsigned long a)
101 writel(d, apbt_virt_address + a);
104 static inline unsigned long apbt_readl(int n, unsigned long a)
106 return readl(apbt_virt_address + a + n * APBTMRS_REG_SIZE);
109 static inline void apbt_writel(int n, unsigned long d, unsigned long a)
111 writel(d, apbt_virt_address + a + n * APBTMRS_REG_SIZE);
114 static inline void apbt_set_mapping(void)
116 struct sfi_timer_table_entry *mtmr;
118 if (apbt_virt_address) {
119 pr_debug("APBT base already mapped\n");
122 mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
124 printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
125 APBT_CLOCKEVENT0_NUM);
128 apbt_address = (unsigned long)mtmr->phys_addr;
130 printk(KERN_WARNING "No timer base from SFI, use default\n");
131 apbt_address = APBT_DEFAULT_BASE;
133 apbt_virt_address = ioremap_nocache(apbt_address, APBT_MMAP_SIZE);
134 if (apbt_virt_address) {
135 pr_debug("Mapped APBT physical addr %p at virtual addr %p\n",\
136 (void *)apbt_address, (void *)apbt_virt_address);
138 pr_debug("Failed mapping APBT phy address at %p\n",\
139 (void *)apbt_address);
142 apbt_freq = mtmr->freq_hz / USEC_PER_SEC;
145 /* Now figure out the physical timer id for clocksource device */
146 mtmr = sfi_get_mtmr(APBT_CLOCKSOURCE_NUM);
150 /* Now figure out the physical timer id */
151 phy_cs_timer_id = (unsigned int)(mtmr->phys_addr & 0xff)
153 pr_debug("Use timer %d for clocksource\n", phy_cs_timer_id);
157 panic("Failed to setup APB system timer\n");
161 static inline void apbt_clear_mapping(void)
163 iounmap(apbt_virt_address);
164 apbt_virt_address = NULL;
168 * APBT timer interrupt enable / disable
170 static inline int is_apbt_capable(void)
172 return apbt_virt_address ? 1 : 0;
175 static struct clocksource clocksource_apbt = {
177 .rating = APBT_CLOCKSOURCE_RATING,
178 .read = apbt_read_clocksource,
181 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
182 .resume = apbt_restart_clocksource,
185 /* boot APB clock event device */
186 static struct clock_event_device apbt_clockevent = {
188 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
189 .set_mode = apbt_set_mode,
190 .set_next_event = apbt_next_event,
193 .rating = APBT_CLOCKEVENT_RATING,
197 * if user does not want to use per CPU apb timer, just give it a lower rating
198 * than local apic timer and skip the late per cpu timer init.
200 static inline int __init setup_x86_mrst_timer(char *arg)
205 if (strcmp("apbt_only", arg) == 0)
206 disable_apbt_percpu = 0;
207 else if (strcmp("lapic_and_apbt", arg) == 0)
208 disable_apbt_percpu = 1;
210 pr_warning("X86 MRST timer option %s not recognised"
211 " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
217 __setup("x86_mrst_timer=", setup_x86_mrst_timer);
220 * start count down from 0xffff_ffff. this is done by toggling the enable bit
221 * then load initial load count to ~0.
223 static void apbt_start_counter(int n)
225 unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
227 ctrl &= ~APBTMR_CONTROL_ENABLE;
228 apbt_writel(n, ctrl, APBTMR_N_CONTROL);
229 apbt_writel(n, ~0, APBTMR_N_LOAD_COUNT);
230 /* enable, mask interrupt */
231 ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
232 ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
233 apbt_writel(n, ctrl, APBTMR_N_CONTROL);
234 /* read it once to get cached counter value initialized */
235 apbt_read_clocksource(&clocksource_apbt);
238 static irqreturn_t apbt_interrupt_handler(int irq, void *data)
240 struct apbt_dev *dev = (struct apbt_dev *)data;
241 struct clock_event_device *aevt = &dev->evt;
243 if (!aevt->event_handler) {
244 printk(KERN_INFO "Spurious APBT timer interrupt on %d\n",
248 aevt->event_handler(aevt);
252 static void apbt_restart_clocksource(void)
254 apbt_start_counter(phy_cs_timer_id);
257 /* Setup IRQ routing via IOAPIC */
259 static void apbt_setup_irq(struct apbt_dev *adev)
261 struct irq_chip *chip;
262 struct irq_desc *desc;
264 /* timer0 irq has been setup early */
267 desc = irq_to_desc(adev->irq);
268 chip = get_irq_chip(adev->irq);
269 disable_irq(adev->irq);
270 desc->status |= IRQ_MOVE_PCNTXT;
271 irq_set_affinity(adev->irq, cpumask_of(adev->cpu));
272 /* APB timer irqs are set up as mp_irqs, timer is edge triggerred */
273 set_irq_chip_and_handler_name(adev->irq, chip, handle_edge_irq, "edge");
274 enable_irq(adev->irq);
275 if (system_state == SYSTEM_BOOTING)
276 if (request_irq(adev->irq, apbt_interrupt_handler,
277 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
279 printk(KERN_ERR "Failed request IRQ for APBT%d\n",
285 static void apbt_enable_int(int n)
287 unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
288 /* clear pending intr */
289 apbt_readl(n, APBTMR_N_EOI);
290 ctrl &= ~APBTMR_CONTROL_INT;
291 apbt_writel(n, ctrl, APBTMR_N_CONTROL);
294 static void apbt_disable_int(int n)
296 unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
298 ctrl |= APBTMR_CONTROL_INT;
299 apbt_writel(n, ctrl, APBTMR_N_CONTROL);
303 static int __init apbt_clockevent_register(void)
305 struct sfi_timer_table_entry *mtmr;
306 struct apbt_dev *adev = &__get_cpu_var(cpu_apbt_dev);
308 mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
310 printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
311 APBT_CLOCKEVENT0_NUM);
316 * We need to calculate the scaled math multiplication factor for
317 * nanosecond to apbt tick conversion.
318 * mult = (nsec/cycle)*2^APBT_SHIFT
320 apbt_clockevent.mult = div_sc((unsigned long) mtmr->freq_hz
321 , NSEC_PER_SEC, APBT_SHIFT);
323 /* Calculate the min / max delta */
324 apbt_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
326 apbt_clockevent.min_delta_ns = clockevent_delta2ns(
327 APBT_MIN_DELTA_USEC*apbt_freq,
330 * Start apbt with the boot cpu mask and make it
331 * global if not used for per cpu timer.
333 apbt_clockevent.cpumask = cpumask_of(smp_processor_id());
334 adev->num = smp_processor_id();
335 memcpy(&adev->evt, &apbt_clockevent, sizeof(struct clock_event_device));
337 if (disable_apbt_percpu) {
338 apbt_clockevent.rating = APBT_CLOCKEVENT_RATING - 100;
339 global_clock_event = &adev->evt;
340 printk(KERN_DEBUG "%s clockevent registered as global\n",
341 global_clock_event->name);
344 if (request_irq(apbt_clockevent.irq, apbt_interrupt_handler,
345 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
346 apbt_clockevent.name, adev)) {
347 printk(KERN_ERR "Failed request IRQ for APBT%d\n",
348 apbt_clockevent.irq);
351 clockevents_register_device(&adev->evt);
352 /* Start APBT 0 interrupts */
353 apbt_enable_int(APBT_CLOCKEVENT0_NUM);
360 /* Should be called with per cpu */
361 void apbt_setup_secondary_clock(void)
363 struct apbt_dev *adev;
364 struct clock_event_device *aevt;
367 /* Don't register boot CPU clockevent */
368 cpu = smp_processor_id();
369 if (cpu == boot_cpu_id)
372 * We need to calculate the scaled math multiplication factor for
373 * nanosecond to apbt tick conversion.
374 * mult = (nsec/cycle)*2^APBT_SHIFT
376 printk(KERN_INFO "Init per CPU clockevent %d\n", cpu);
377 adev = &per_cpu(cpu_apbt_dev, cpu);
380 memcpy(aevt, &apbt_clockevent, sizeof(*aevt));
381 aevt->cpumask = cpumask_of(cpu);
382 aevt->name = adev->name;
383 aevt->mode = CLOCK_EVT_MODE_UNUSED;
385 printk(KERN_INFO "Registering CPU %d clockevent device %s, mask %08x\n",
386 cpu, aevt->name, *(u32 *)aevt->cpumask);
388 apbt_setup_irq(adev);
390 clockevents_register_device(aevt);
392 apbt_enable_int(cpu);
398 * this notify handler process CPU hotplug events. in case of S0i3, nonboot
399 * cpus are disabled/enabled frequently, for performance reasons, we keep the
400 * per cpu timer irq registered so that we do need to do free_irq/request_irq.
402 * TODO: it might be more reliable to directly disable percpu clockevent device
403 * without the notifier chain. currently, cpu 0 may get interrupts from other
404 * cpu timers during the offline process due to the ordering of notification.
405 * the extra interrupt is harmless.
407 static int apbt_cpuhp_notify(struct notifier_block *n,
408 unsigned long action, void *hcpu)
410 unsigned long cpu = (unsigned long)hcpu;
411 struct apbt_dev *adev = &per_cpu(cpu_apbt_dev, cpu);
413 switch (action & 0xf) {
415 apbt_disable_int(cpu);
416 if (system_state == SYSTEM_RUNNING)
417 pr_debug("skipping APBT CPU %lu offline\n", cpu);
419 pr_debug("APBT clockevent for cpu %lu offline\n", cpu);
420 free_irq(adev->irq, adev);
424 pr_debug(KERN_INFO "APBT notified %lu, no action\n", action);
429 static __init int apbt_late_init(void)
431 if (disable_apbt_percpu)
433 /* This notifier should be called after workqueue is ready */
434 hotcpu_notifier(apbt_cpuhp_notify, -20);
437 fs_initcall(apbt_late_init);
440 void apbt_setup_secondary_clock(void) {}
442 #endif /* CONFIG_SMP */
444 static void apbt_set_mode(enum clock_event_mode mode,
445 struct clock_event_device *evt)
450 struct apbt_dev *adev = EVT_TO_APBT_DEV(evt);
452 timer_num = adev->num;
453 pr_debug("%s CPU %d timer %d mode=%d\n",
454 __func__, first_cpu(*evt->cpumask), timer_num, mode);
457 case CLOCK_EVT_MODE_PERIODIC:
458 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * apbt_clockevent.mult;
459 delta >>= apbt_clockevent.shift;
460 ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
461 ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
462 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
464 * DW APB p. 46, have to disable timer before load counter,
465 * may cause sync problem.
467 ctrl &= ~APBTMR_CONTROL_ENABLE;
468 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
470 pr_debug("Setting clock period %d for HZ %d\n", (int)delta, HZ);
471 apbt_writel(timer_num, delta, APBTMR_N_LOAD_COUNT);
472 ctrl |= APBTMR_CONTROL_ENABLE;
473 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
475 /* APB timer does not have one-shot mode, use free running mode */
476 case CLOCK_EVT_MODE_ONESHOT:
477 ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
479 * set free running mode, this mode will let timer reload max
480 * timeout which will give time (3min on 25MHz clock) to rearm
481 * the next event, therefore emulate the one-shot mode.
483 ctrl &= ~APBTMR_CONTROL_ENABLE;
484 ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
486 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
487 /* write again to set free running mode */
488 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
491 * DW APB p. 46, load counter with all 1s before starting free
494 apbt_writel(timer_num, ~0, APBTMR_N_LOAD_COUNT);
495 ctrl &= ~APBTMR_CONTROL_INT;
496 ctrl |= APBTMR_CONTROL_ENABLE;
497 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
500 case CLOCK_EVT_MODE_UNUSED:
501 case CLOCK_EVT_MODE_SHUTDOWN:
502 apbt_disable_int(timer_num);
503 ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
504 ctrl &= ~APBTMR_CONTROL_ENABLE;
505 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
508 case CLOCK_EVT_MODE_RESUME:
509 apbt_enable_int(timer_num);
514 static int apbt_next_event(unsigned long delta,
515 struct clock_event_device *evt)
520 struct apbt_dev *adev = EVT_TO_APBT_DEV(evt);
522 timer_num = adev->num;
524 ctrl = apbt_readl(timer_num, APBTMR_N_CONTROL);
525 ctrl &= ~APBTMR_CONTROL_ENABLE;
526 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
527 /* write new count */
528 apbt_writel(timer_num, delta, APBTMR_N_LOAD_COUNT);
529 ctrl |= APBTMR_CONTROL_ENABLE;
530 apbt_writel(timer_num, ctrl, APBTMR_N_CONTROL);
535 * APB timer clock is not in sync with pclk on Langwell, which translates to
536 * unreliable read value caused by sampling error. the error does not add up
537 * overtime and only happens when sampling a 0 as a 1 by mistake. so the time
538 * would go backwards. the following code is trying to prevent time traveling
539 * backwards. little bit paranoid.
541 static cycle_t apbt_read_clocksource(struct clocksource *cs)
543 unsigned long t0, t1, t2;
544 static unsigned long last_read;
547 t1 = apbt_readl(phy_cs_timer_id,
548 APBTMR_N_CURRENT_VALUE);
549 t2 = apbt_readl(phy_cs_timer_id,
550 APBTMR_N_CURRENT_VALUE);
551 if (unlikely(t1 < t2)) {
552 pr_debug("APBT: read current count error %lx:%lx:%lx\n",
557 * check against cached last read, makes sure time does not go back.
558 * it could be a normal rollover but we will do tripple check anyway
560 if (unlikely(t2 > last_read)) {
561 /* check if we have a normal rollover */
562 unsigned long raw_intr_status =
563 apbt_readl_reg(APBTMRS_RAW_INT_STATUS);
565 * cs timer interrupt is masked but raw intr bit is set if
566 * rollover occurs. then we read EOI reg to clear it.
568 if (raw_intr_status & (1 << phy_cs_timer_id)) {
569 apbt_readl(phy_cs_timer_id, APBTMR_N_EOI);
572 pr_debug("APB CS going back %lx:%lx:%lx ",
573 t2, last_read, t2 - last_read);
575 pr_debug(KERN_INFO "tripple check enforced\n");
576 t0 = apbt_readl(phy_cs_timer_id,
577 APBTMR_N_CURRENT_VALUE);
579 t1 = apbt_readl(phy_cs_timer_id,
580 APBTMR_N_CURRENT_VALUE);
582 t2 = apbt_readl(phy_cs_timer_id,
583 APBTMR_N_CURRENT_VALUE);
584 if ((t2 > t1) || (t1 > t0)) {
585 printk(KERN_ERR "Error: APB CS tripple check failed\n");
594 static int apbt_clocksource_register(void)
599 /* Start the counter, use timer 2 as source, timer 0/1 for event */
600 apbt_start_counter(phy_cs_timer_id);
602 /* Verify whether apbt counter works */
603 t1 = apbt_read_clocksource(&clocksource_apbt);
607 * We don't know the TSC frequency yet, but waiting for
608 * 200000 TSC cycles is safe:
615 } while ((now - start) < 200000UL);
617 /* APBT is the only always on clocksource, it has to work! */
618 if (t1 == apbt_read_clocksource(&clocksource_apbt))
619 panic("APBT counter not counting. APBT disabled\n");
622 * initialize and register APBT clocksource
623 * convert that to ns/clock cycle
624 * mult = (ns/c) * 2^APBT_SHIFT
626 clocksource_apbt.mult = div_sc(MSEC_PER_SEC,
627 (unsigned long) apbt_freq, APBT_SHIFT);
628 clocksource_register(&clocksource_apbt);
634 * Early setup the APBT timer, only use timer 0 for booting then switch to
635 * per CPU timer if possible.
636 * returns 1 if per cpu apbt is setup
637 * returns 0 if no per cpu apbt is chosen
638 * panic if set up failed, this is the only platform timer on Moorestown.
640 void __init apbt_time_init(void)
644 struct sfi_timer_table_entry *p_mtmr;
645 unsigned int percpu_timer;
646 struct apbt_dev *adev;
649 if (apb_timer_block_enabled)
652 if (apbt_virt_address) {
653 pr_debug("Found APBT version 0x%lx\n",\
654 apbt_readl_reg(APBTMRS_COMP_VERSION));
658 * Read the frequency and check for a sane value, for ESL model
659 * we extend the possible clock range to allow time scaling.
662 if (apbt_freq < APBT_MIN_FREQ || apbt_freq > APBT_MAX_FREQ) {
663 pr_debug("APBT has invalid freq 0x%llx\n", apbt_freq);
666 if (apbt_clocksource_register()) {
667 pr_debug("APBT has failed to register clocksource\n");
670 if (!apbt_clockevent_register())
671 apb_timer_block_enabled = 1;
673 pr_debug("APBT has failed to register clockevent\n");
677 /* kernel cmdline disable apb timer, so we will use lapic timers */
678 if (disable_apbt_percpu) {
679 printk(KERN_INFO "apbt: disabled per cpu timer\n");
682 pr_debug("%s: %d CPUs online\n", __func__, num_online_cpus());
683 if (num_possible_cpus() <= sfi_mtimer_num) {
685 apbt_num_timers_used = num_possible_cpus();
688 apbt_num_timers_used = 1;
689 adev = &per_cpu(cpu_apbt_dev, 0);
690 adev->flags &= ~APBT_DEV_USED;
692 pr_debug("%s: %d APB timers used\n", __func__, apbt_num_timers_used);
694 /* here we set up per CPU timer data structure */
695 apbt_devs = kzalloc(sizeof(struct apbt_dev) * apbt_num_timers_used,
698 printk(KERN_ERR "Failed to allocate APB timer devices\n");
701 for (i = 0; i < apbt_num_timers_used; i++) {
702 adev = &per_cpu(cpu_apbt_dev, i);
705 p_mtmr = sfi_get_mtmr(i);
707 adev->tick = p_mtmr->freq_hz;
708 adev->irq = p_mtmr->irq;
710 printk(KERN_ERR "Failed to get timer for cpu %d\n", i);
712 sprintf(adev->name, "apbt%d", i);
719 apbt_clear_mapping();
720 apb_timer_block_enabled = 0;
721 panic("failed to enable APB timer\n");
724 static inline void apbt_disable(int n)
726 if (is_apbt_capable()) {
727 unsigned long ctrl = apbt_readl(n, APBTMR_N_CONTROL);
728 ctrl &= ~APBTMR_CONTROL_ENABLE;
729 apbt_writel(n, ctrl, APBTMR_N_CONTROL);
733 /* called before apb_timer_enable, use early map */
734 unsigned long apbt_quick_calibrate()
739 unsigned long khz = 0;
743 apbt_start_counter(phy_cs_timer_id);
745 /* check if the timer can count down, otherwise return */
746 old = apbt_read_clocksource(&clocksource_apbt);
749 if (old != apbt_read_clocksource(&clocksource_apbt))
756 loop = (apbt_freq * 1000) << 4;
758 /* restart the timer to ensure it won't get to 0 in the calibration */
759 apbt_start_counter(phy_cs_timer_id);
761 old = apbt_read_clocksource(&clocksource_apbt);
764 t1 = __native_read_tsc();
767 new = apbt_read_clocksource(&clocksource_apbt);
770 t2 = __native_read_tsc();
773 if (unlikely(loop >> shift == 0)) {
775 "APBT TSC calibration failed, not enough resolution\n");
778 scale = (int)div_u64((t2 - t1), loop >> shift);
779 khz = (scale * apbt_freq * 1000) >> shift;
780 printk(KERN_INFO "TSC freq calculated by APB timer is %lu khz\n", khz);