timer-sp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * linux/arch/arm/common/timer-sp.c
  3. *
  4. * Copyright (C) 1999 - 2003 ARM Limited
  5. * Copyright (C) 2000 Deep Blue Solutions Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/clocksource.h>
  22. #include <linux/clockchips.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <asm/hardware/arm_timer.h>
  27. /*
  28. * These timers are currently always setup to be clocked at 1MHz.
  29. */
  30. #define TIMER_FREQ_KHZ (1000)
  31. #define TIMER_RELOAD (TIMER_FREQ_KHZ * 1000 / HZ)
  32. static void __iomem *clksrc_base;
  33. static cycle_t sp804_read(struct clocksource *cs)
  34. {
  35. return ~readl(clksrc_base + TIMER_VALUE);
  36. }
  37. static struct clocksource clocksource_sp804 = {
  38. .name = "timer3",
  39. .rating = 200,
  40. .read = sp804_read,
  41. .mask = CLOCKSOURCE_MASK(32),
  42. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  43. };
  44. void __init sp804_clocksource_init(void __iomem *base)
  45. {
  46. struct clocksource *cs = &clocksource_sp804;
  47. clksrc_base = base;
  48. /* setup timer 0 as free-running clocksource */
  49. writel(0, clksrc_base + TIMER_CTRL);
  50. writel(0xffffffff, clksrc_base + TIMER_LOAD);
  51. writel(0xffffffff, clksrc_base + TIMER_VALUE);
  52. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  53. clksrc_base + TIMER_CTRL);
  54. clocksource_register_khz(cs, TIMER_FREQ_KHZ);
  55. }
  56. static void __iomem *clkevt_base;
  57. /*
  58. * IRQ handler for the timer
  59. */
  60. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  61. {
  62. struct clock_event_device *evt = dev_id;
  63. /* clear the interrupt */
  64. writel(1, clkevt_base + TIMER_INTCLR);
  65. evt->event_handler(evt);
  66. return IRQ_HANDLED;
  67. }
  68. static void sp804_set_mode(enum clock_event_mode mode,
  69. struct clock_event_device *evt)
  70. {
  71. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  72. writel(ctrl, clkevt_base + TIMER_CTRL);
  73. switch (mode) {
  74. case CLOCK_EVT_MODE_PERIODIC:
  75. writel(TIMER_RELOAD, clkevt_base + TIMER_LOAD);
  76. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  77. break;
  78. case CLOCK_EVT_MODE_ONESHOT:
  79. /* period set, and timer enabled in 'next_event' hook */
  80. ctrl |= TIMER_CTRL_ONESHOT;
  81. break;
  82. case CLOCK_EVT_MODE_UNUSED:
  83. case CLOCK_EVT_MODE_SHUTDOWN:
  84. default:
  85. break;
  86. }
  87. writel(ctrl, clkevt_base + TIMER_CTRL);
  88. }
  89. static int sp804_set_next_event(unsigned long next,
  90. struct clock_event_device *evt)
  91. {
  92. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  93. writel(next, clkevt_base + TIMER_LOAD);
  94. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  95. return 0;
  96. }
  97. static struct clock_event_device sp804_clockevent = {
  98. .name = "timer0",
  99. .shift = 32,
  100. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  101. .set_mode = sp804_set_mode,
  102. .set_next_event = sp804_set_next_event,
  103. .rating = 300,
  104. .cpumask = cpu_all_mask,
  105. };
  106. static struct irqaction sp804_timer_irq = {
  107. .name = "timer",
  108. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  109. .handler = sp804_timer_interrupt,
  110. .dev_id = &sp804_clockevent,
  111. };
  112. void __init sp804_clockevents_init(void __iomem *base, unsigned int timer_irq)
  113. {
  114. struct clock_event_device *evt = &sp804_clockevent;
  115. clkevt_base = base;
  116. evt->irq = timer_irq;
  117. evt->mult = div_sc(TIMER_FREQ_KHZ, NSEC_PER_MSEC, evt->shift);
  118. evt->max_delta_ns = clockevent_delta2ns(0xffffffff, evt);
  119. evt->min_delta_ns = clockevent_delta2ns(0xf, evt);
  120. setup_irq(timer_irq, &sp804_timer_irq);
  121. clockevents_register_device(evt);
  122. }