timer-sp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * linux/arch/arm/plat-versatile/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. #include <plat/timer-sp.h>
  28. /*
  29. * These timers are currently always setup to be clocked at 1MHz.
  30. */
  31. #define TIMER_FREQ_KHZ (1000)
  32. #define TIMER_RELOAD (TIMER_FREQ_KHZ * 1000 / HZ)
  33. static void __iomem *clksrc_base;
  34. static cycle_t sp804_read(struct clocksource *cs)
  35. {
  36. return ~readl(clksrc_base + TIMER_VALUE);
  37. }
  38. static struct clocksource clocksource_sp804 = {
  39. .name = "timer3",
  40. .rating = 200,
  41. .read = sp804_read,
  42. .mask = CLOCKSOURCE_MASK(32),
  43. .shift = 20,
  44. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  45. };
  46. void __init sp804_clocksource_init(void __iomem *base)
  47. {
  48. struct clocksource *cs = &clocksource_sp804;
  49. clksrc_base = base;
  50. /* setup timer 0 as free-running clocksource */
  51. writel(0, clksrc_base + TIMER_CTRL);
  52. writel(0xffffffff, clksrc_base + TIMER_LOAD);
  53. writel(0xffffffff, clksrc_base + TIMER_VALUE);
  54. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  55. clksrc_base + TIMER_CTRL);
  56. cs->mult = clocksource_khz2mult(TIMER_FREQ_KHZ, cs->shift);
  57. clocksource_register(cs);
  58. }
  59. static void __iomem *clkevt_base;
  60. /*
  61. * IRQ handler for the timer
  62. */
  63. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  64. {
  65. struct clock_event_device *evt = dev_id;
  66. /* clear the interrupt */
  67. writel(1, clkevt_base + TIMER_INTCLR);
  68. evt->event_handler(evt);
  69. return IRQ_HANDLED;
  70. }
  71. static void sp804_set_mode(enum clock_event_mode mode,
  72. struct clock_event_device *evt)
  73. {
  74. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  75. writel(ctrl, clkevt_base + TIMER_CTRL);
  76. switch (mode) {
  77. case CLOCK_EVT_MODE_PERIODIC:
  78. writel(TIMER_RELOAD, clkevt_base + TIMER_LOAD);
  79. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  80. break;
  81. case CLOCK_EVT_MODE_ONESHOT:
  82. /* period set, and timer enabled in 'next_event' hook */
  83. ctrl |= TIMER_CTRL_ONESHOT;
  84. break;
  85. case CLOCK_EVT_MODE_UNUSED:
  86. case CLOCK_EVT_MODE_SHUTDOWN:
  87. default:
  88. break;
  89. }
  90. writel(ctrl, clkevt_base + TIMER_CTRL);
  91. }
  92. static int sp804_set_next_event(unsigned long next,
  93. struct clock_event_device *evt)
  94. {
  95. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  96. writel(next, clkevt_base + TIMER_LOAD);
  97. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  98. return 0;
  99. }
  100. static struct clock_event_device sp804_clockevent = {
  101. .name = "timer0",
  102. .shift = 32,
  103. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  104. .set_mode = sp804_set_mode,
  105. .set_next_event = sp804_set_next_event,
  106. .rating = 300,
  107. .cpumask = cpu_all_mask,
  108. };
  109. static struct irqaction sp804_timer_irq = {
  110. .name = "timer",
  111. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  112. .handler = sp804_timer_interrupt,
  113. .dev_id = &sp804_clockevent,
  114. };
  115. void __init sp804_clockevents_init(void __iomem *base, unsigned int timer_irq)
  116. {
  117. struct clock_event_device *evt = &sp804_clockevent;
  118. clkevt_base = base;
  119. evt->irq = timer_irq;
  120. evt->mult = div_sc(TIMER_FREQ_KHZ, NSEC_PER_MSEC, evt->shift);
  121. evt->max_delta_ns = clockevent_delta2ns(0xffffffff, evt);
  122. evt->min_delta_ns = clockevent_delta2ns(0xf, evt);
  123. setup_irq(timer_irq, &sp804_timer_irq);
  124. clockevents_register_device(evt);
  125. }