blob: 5783e6de2079dc692cd3e017617b067b2045dee2 [file] [log] [blame]
john stultz8d016ef2006-06-26 00:25:09 -07001/*
Dave Jones835c34a2007-10-12 21:10:53 -04002 * 8253/PIT functions
john stultz8d016ef2006-06-26 00:25:09 -07003 *
4 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08005#include <linux/clockchips.h>
Thomas Gleixner18de5bc2007-07-21 04:37:34 -07006#include <linux/interrupt.h>
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +05307#include <linux/spinlock.h>
Thomas Gleixner18de5bc2007-07-21 04:37:34 -07008#include <linux/jiffies.h>
9#include <linux/module.h>
Arnd Bergmann08604bd2009-06-16 15:31:12 -070010#include <linux/timex.h>
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +053011#include <linux/delay.h>
Ralf Baechle334955e2011-06-01 19:04:57 +010012#include <linux/i8253.h>
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +053013#include <linux/init.h>
14#include <linux/io.h>
john stultz8d016ef2006-06-26 00:25:09 -070015
Thomas Gleixner4713e22c2008-01-30 13:30:02 +010016#include <asm/hpet.h>
Ralf Baechle16f871b2011-06-01 19:05:06 +010017#include <asm/time.h>
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +053018#include <asm/smp.h>
john stultz8d016ef2006-06-26 00:25:09 -070019
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080020/*
21 * HPET replaces the PIT, when enabled. So we need to know, which of
22 * the two timers is used
23 */
24struct 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 */
31static void init_pit_timer(enum clock_event_mode mode,
32 struct clock_event_device *evt)
john stultz8d016ef2006-06-26 00:25:09 -070033{
Thomas Gleixnerced918e2010-02-17 16:47:10 +000034 raw_spin_lock(&i8253_lock);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080035
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +053036 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080037 case CLOCK_EVT_MODE_PERIODIC:
38 /* binary, mode 2, LSB/MSB, ch 0 */
Alan Cox466eed22008-01-30 13:33:14 +010039 outb_pit(0x34, PIT_MODE);
40 outb_pit(LATCH & 0xff , PIT_CH0); /* LSB */
41 outb_pit(LATCH >> 8 , PIT_CH0); /* MSB */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080042 break;
43
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080044 case CLOCK_EVT_MODE_SHUTDOWN:
45 case CLOCK_EVT_MODE_UNUSED:
Thomas Gleixner76719882007-07-21 04:37:38 -070046 if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
47 evt->mode == CLOCK_EVT_MODE_ONESHOT) {
Alan Cox466eed22008-01-30 13:33:14 +010048 outb_pit(0x30, PIT_MODE);
49 outb_pit(0, PIT_CH0);
50 outb_pit(0, PIT_CH0);
Thomas Gleixner76719882007-07-21 04:37:38 -070051 }
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070052 break;
53
Thomas Gleixner6b3964c2007-03-22 22:46:18 +010054 case CLOCK_EVT_MODE_ONESHOT:
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080055 /* One shot setup */
Alan Cox466eed22008-01-30 13:33:14 +010056 outb_pit(0x38, PIT_MODE);
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070057 break;
58
59 case CLOCK_EVT_MODE_RESUME:
60 /* Nothing to do here */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080061 break;
62 }
Thomas Gleixnerced918e2010-02-17 16:47:10 +000063 raw_spin_unlock(&i8253_lock);
john stultz8d016ef2006-06-26 00:25:09 -070064}
john stultz5d0cf412006-06-26 00:25:12 -070065
66/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080067 * Program the next event in oneshot mode
68 *
69 * Delta is given in PIT ticks
70 */
71static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
72{
Thomas Gleixnerced918e2010-02-17 16:47:10 +000073 raw_spin_lock(&i8253_lock);
Alan Cox466eed22008-01-30 13:33:14 +010074 outb_pit(delta & 0xff , PIT_CH0); /* LSB */
75 outb_pit(delta >> 8 , PIT_CH0); /* MSB */
Thomas Gleixnerced918e2010-02-17 16:47:10 +000076 raw_spin_unlock(&i8253_lock);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080077
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 Arlott27b46d72007-10-20 01:13:56 +020085 * The profiling and update capabilities are switched off once the local apic is
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080086 * registered. This mechanism replaces the previous #ifdef LOCAL_APIC -
87 * !using_apic_timer decisions in do_timer_interrupt_hook()
88 */
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +053089static struct clock_event_device pit_ce = {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080090 .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 Gleixnere9e2cdb2007-02-16 01:28:04 -080094 .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 */
101void __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 Rajputc8344bc2009-03-21 16:56:10 +0530107 pit_ce.cpumask = cpumask_of(smp_processor_id());
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +0530108
Thomas Gleixner61ee9a42011-05-18 21:33:42 +0000109 clockevents_config_and_register(&pit_ce, CLOCK_TICK_RATE, 0xF, 0x7FFF);
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +0530110 global_clock_event = &pit_ce;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800111}
112
Thomas Gleixnerf5e0e932007-10-12 23:04:06 +0200113#ifndef CONFIG_X86_64
john stultz5d0cf412006-06-26 00:25:12 -0700114static int __init init_pit_clocksource(void)
115{
Thomas Gleixner316da3b2008-01-30 13:30:02 +0100116 /*
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 Rajputc8344bc2009-03-21 16:56:10 +0530124 pit_ce.mode != CLOCK_EVT_MODE_PERIODIC)
john stultz5d0cf412006-06-26 00:25:12 -0700125 return 0;
126
Russell King82491452011-05-08 18:55:19 +0100127 return clocksource_i8253_init();
john stultz5d0cf412006-06-26 00:25:12 -0700128}
john stultz6bb74df2007-03-05 00:30:50 -0800129arch_initcall(init_pit_clocksource);
Jaswinder Singh Rajputc8344bc2009-03-21 16:56:10 +0530130#endif /* !CONFIG_X86_64 */