isa-timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * linux/arch/arm/mach-footbridge/isa-timer.c
  3. *
  4. * Copyright (C) 1998 Russell King.
  5. * Copyright (C) 1998 Phil Blundell
  6. */
  7. #include <linux/clockchips.h>
  8. #include <linux/clocksource.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/io.h>
  13. #include <linux/timex.h>
  14. #include <asm/irq.h>
  15. #include <asm/mach/time.h>
  16. #include "common.h"
  17. #define PIT_MODE 0x43
  18. #define PIT_CH0 0x40
  19. #define PIT_LATCH ((PIT_TICK_RATE + HZ / 2) / HZ)
  20. static cycle_t pit_read(struct clocksource *cs)
  21. {
  22. unsigned long flags;
  23. static int old_count;
  24. static u32 old_jifs;
  25. int count;
  26. u32 jifs;
  27. raw_local_irq_save(flags);
  28. jifs = jiffies;
  29. outb_p(0x00, PIT_MODE); /* latch the count */
  30. count = inb_p(PIT_CH0); /* read the latched count */
  31. count |= inb_p(PIT_CH0) << 8;
  32. if (count > old_count && jifs == old_jifs)
  33. count = old_count;
  34. old_count = count;
  35. old_jifs = jifs;
  36. raw_local_irq_restore(flags);
  37. count = (PIT_LATCH - 1) - count;
  38. return (cycle_t)(jifs * PIT_LATCH) + count;
  39. }
  40. static struct clocksource pit_cs = {
  41. .name = "pit",
  42. .rating = 110,
  43. .read = pit_read,
  44. .mask = CLOCKSOURCE_MASK(32),
  45. };
  46. static void pit_set_mode(enum clock_event_mode mode,
  47. struct clock_event_device *evt)
  48. {
  49. unsigned long flags;
  50. raw_local_irq_save(flags);
  51. switch (mode) {
  52. case CLOCK_EVT_MODE_PERIODIC:
  53. outb_p(0x34, PIT_MODE);
  54. outb_p(PIT_LATCH & 0xff, PIT_CH0);
  55. outb_p(PIT_LATCH >> 8, PIT_CH0);
  56. break;
  57. case CLOCK_EVT_MODE_SHUTDOWN:
  58. case CLOCK_EVT_MODE_UNUSED:
  59. outb_p(0x30, PIT_MODE);
  60. outb_p(0, PIT_CH0);
  61. outb_p(0, PIT_CH0);
  62. break;
  63. case CLOCK_EVT_MODE_ONESHOT:
  64. case CLOCK_EVT_MODE_RESUME:
  65. break;
  66. }
  67. local_irq_restore(flags);
  68. }
  69. static int pit_set_next_event(unsigned long delta,
  70. struct clock_event_device *evt)
  71. {
  72. return 0;
  73. }
  74. static struct clock_event_device pit_ce = {
  75. .name = "pit",
  76. .features = CLOCK_EVT_FEAT_PERIODIC,
  77. .set_mode = pit_set_mode,
  78. .set_next_event = pit_set_next_event,
  79. .shift = 32,
  80. };
  81. static irqreturn_t pit_timer_interrupt(int irq, void *dev_id)
  82. {
  83. struct clock_event_device *ce = dev_id;
  84. ce->event_handler(ce);
  85. return IRQ_HANDLED;
  86. }
  87. static struct irqaction pit_timer_irq = {
  88. .name = "pit",
  89. .handler = pit_timer_interrupt,
  90. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  91. .dev_id = &pit_ce,
  92. };
  93. static void __init isa_timer_init(void)
  94. {
  95. pit_ce.cpumask = cpumask_of(smp_processor_id());
  96. pit_ce.mult = div_sc(PIT_TICK_RATE, NSEC_PER_SEC, pit_ce.shift);
  97. pit_ce.max_delta_ns = clockevent_delta2ns(0x7fff, &pit_ce);
  98. pit_ce.min_delta_ns = clockevent_delta2ns(0x000f, &pit_ce);
  99. clocksource_register_hz(&pit_cs, PIT_TICK_RATE);
  100. setup_irq(pit_ce.irq, &pit_timer_irq);
  101. clockevents_register_device(&pit_ce);
  102. }
  103. struct sys_timer isa_timer = {
  104. .init = isa_timer_init,
  105. };