john stultz | 8d016ef | 2006-06-26 00:25:09 -0700 | [diff] [blame] | 1 | /* |
Dave Jones | 835c34a | 2007-10-12 21:10:53 -0400 | [diff] [blame] | 2 | * 8253/PIT functions |
john stultz | 8d016ef | 2006-06-26 00:25:09 -0700 | [diff] [blame] | 3 | * |
| 4 | */ |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 5 | #include <linux/clockchips.h> |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 6 | #include <linux/interrupt.h> |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 7 | #include <linux/spinlock.h> |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 8 | #include <linux/jiffies.h> |
| 9 | #include <linux/module.h> |
Arnd Bergmann | 08604bd | 2009-06-16 15:31:12 -0700 | [diff] [blame] | 10 | #include <linux/timex.h> |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 11 | #include <linux/delay.h> |
Ralf Baechle | 334955e | 2011-06-01 19:04:57 +0100 | [diff] [blame] | 12 | #include <linux/i8253.h> |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/io.h> |
john stultz | 8d016ef | 2006-06-26 00:25:09 -0700 | [diff] [blame] | 15 | |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 16 | #include <asm/hpet.h> |
Ralf Baechle | 16f871b | 2011-06-01 19:05:06 +0100 | [diff] [blame^] | 17 | #include <asm/time.h> |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 18 | #include <asm/smp.h> |
john stultz | 8d016ef | 2006-06-26 00:25:09 -0700 | [diff] [blame] | 19 | |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 20 | /* |
| 21 | * HPET replaces the PIT, when enabled. So we need to know, which of |
| 22 | * the two timers is used |
| 23 | */ |
| 24 | struct clock_event_device *global_clock_event; |
| 25 | |
| 26 | /* |
| 27 | * Initialize the PIT timer. |
| 28 | * |
| 29 | * This is also called after resume to bring the PIT into operation again. |
| 30 | */ |
| 31 | static void init_pit_timer(enum clock_event_mode mode, |
| 32 | struct clock_event_device *evt) |
john stultz | 8d016ef | 2006-06-26 00:25:09 -0700 | [diff] [blame] | 33 | { |
Thomas Gleixner | ced918e | 2010-02-17 16:47:10 +0000 | [diff] [blame] | 34 | raw_spin_lock(&i8253_lock); |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 35 | |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 36 | switch (mode) { |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 37 | case CLOCK_EVT_MODE_PERIODIC: |
| 38 | /* binary, mode 2, LSB/MSB, ch 0 */ |
Alan Cox | 466eed2 | 2008-01-30 13:33:14 +0100 | [diff] [blame] | 39 | outb_pit(0x34, PIT_MODE); |
| 40 | outb_pit(LATCH & 0xff , PIT_CH0); /* LSB */ |
| 41 | outb_pit(LATCH >> 8 , PIT_CH0); /* MSB */ |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 42 | break; |
| 43 | |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 44 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 45 | case CLOCK_EVT_MODE_UNUSED: |
Thomas Gleixner | 7671988 | 2007-07-21 04:37:38 -0700 | [diff] [blame] | 46 | if (evt->mode == CLOCK_EVT_MODE_PERIODIC || |
| 47 | evt->mode == CLOCK_EVT_MODE_ONESHOT) { |
Alan Cox | 466eed2 | 2008-01-30 13:33:14 +0100 | [diff] [blame] | 48 | outb_pit(0x30, PIT_MODE); |
| 49 | outb_pit(0, PIT_CH0); |
| 50 | outb_pit(0, PIT_CH0); |
Thomas Gleixner | 7671988 | 2007-07-21 04:37:38 -0700 | [diff] [blame] | 51 | } |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 52 | break; |
| 53 | |
Thomas Gleixner | 6b3964c | 2007-03-22 22:46:18 +0100 | [diff] [blame] | 54 | case CLOCK_EVT_MODE_ONESHOT: |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 55 | /* One shot setup */ |
Alan Cox | 466eed2 | 2008-01-30 13:33:14 +0100 | [diff] [blame] | 56 | outb_pit(0x38, PIT_MODE); |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 57 | break; |
| 58 | |
| 59 | case CLOCK_EVT_MODE_RESUME: |
| 60 | /* Nothing to do here */ |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 61 | break; |
| 62 | } |
Thomas Gleixner | ced918e | 2010-02-17 16:47:10 +0000 | [diff] [blame] | 63 | raw_spin_unlock(&i8253_lock); |
john stultz | 8d016ef | 2006-06-26 00:25:09 -0700 | [diff] [blame] | 64 | } |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 67 | * Program the next event in oneshot mode |
| 68 | * |
| 69 | * Delta is given in PIT ticks |
| 70 | */ |
| 71 | static int pit_next_event(unsigned long delta, struct clock_event_device *evt) |
| 72 | { |
Thomas Gleixner | ced918e | 2010-02-17 16:47:10 +0000 | [diff] [blame] | 73 | raw_spin_lock(&i8253_lock); |
Alan Cox | 466eed2 | 2008-01-30 13:33:14 +0100 | [diff] [blame] | 74 | outb_pit(delta & 0xff , PIT_CH0); /* LSB */ |
| 75 | outb_pit(delta >> 8 , PIT_CH0); /* MSB */ |
Thomas Gleixner | ced918e | 2010-02-17 16:47:10 +0000 | [diff] [blame] | 76 | raw_spin_unlock(&i8253_lock); |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * On UP the PIT can serve all of the possible timer functions. On SMP systems |
| 83 | * it can be solely used for the global tick. |
| 84 | * |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 85 | * The profiling and update capabilities are switched off once the local apic is |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 86 | * registered. This mechanism replaces the previous #ifdef LOCAL_APIC - |
| 87 | * !using_apic_timer decisions in do_timer_interrupt_hook() |
| 88 | */ |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 89 | static struct clock_event_device pit_ce = { |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 90 | .name = "pit", |
| 91 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 92 | .set_mode = init_pit_timer, |
| 93 | .set_next_event = pit_next_event, |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 94 | .irq = 0, |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Initialize the conversion factor and the min/max deltas of the clock event |
| 99 | * structure and register the clock event source with the framework. |
| 100 | */ |
| 101 | void __init setup_pit_timer(void) |
| 102 | { |
| 103 | /* |
| 104 | * Start pit with the boot cpu mask and make it global after the |
| 105 | * IO_APIC has been initialized. |
| 106 | */ |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 107 | pit_ce.cpumask = cpumask_of(smp_processor_id()); |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 108 | |
Thomas Gleixner | 61ee9a4 | 2011-05-18 21:33:42 +0000 | [diff] [blame] | 109 | clockevents_config_and_register(&pit_ce, CLOCK_TICK_RATE, 0xF, 0x7FFF); |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 110 | global_clock_event = &pit_ce; |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Thomas Gleixner | f5e0e93 | 2007-10-12 23:04:06 +0200 | [diff] [blame] | 113 | #ifndef CONFIG_X86_64 |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 114 | static int __init init_pit_clocksource(void) |
| 115 | { |
Thomas Gleixner | 316da3b | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 116 | /* |
| 117 | * Several reasons not to register PIT as a clocksource: |
| 118 | * |
| 119 | * - On SMP PIT does not scale due to i8253_lock |
| 120 | * - when HPET is enabled |
| 121 | * - when local APIC timer is active (PIT is switched off) |
| 122 | */ |
| 123 | if (num_possible_cpus() > 1 || is_hpet_enabled() || |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 124 | pit_ce.mode != CLOCK_EVT_MODE_PERIODIC) |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | |
Russell King | 8249145 | 2011-05-08 18:55:19 +0100 | [diff] [blame] | 127 | return clocksource_i8253_init(); |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 128 | } |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 129 | arch_initcall(init_pit_clocksource); |
Jaswinder Singh Rajput | c8344bc | 2009-03-21 16:56:10 +0530 | [diff] [blame] | 130 | #endif /* !CONFIG_X86_64 */ |