timer-sp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/clk.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/io.h>
  28. #include <asm/hardware/arm_timer.h>
  29. static long __init sp804_get_clock_rate(const char *name)
  30. {
  31. struct clk *clk;
  32. long rate;
  33. int err;
  34. clk = clk_get_sys("sp804", name);
  35. if (IS_ERR(clk)) {
  36. pr_err("sp804: %s clock not found: %d\n", name,
  37. (int)PTR_ERR(clk));
  38. return PTR_ERR(clk);
  39. }
  40. err = clk_enable(clk);
  41. if (err) {
  42. pr_err("sp804: %s clock failed to enable: %d\n", name, err);
  43. clk_put(clk);
  44. return err;
  45. }
  46. rate = clk_get_rate(clk);
  47. if (rate < 0) {
  48. pr_err("sp804: %s clock failed to get rate: %ld\n", name, rate);
  49. clk_disable(clk);
  50. clk_put(clk);
  51. }
  52. return rate;
  53. }
  54. void __init sp804_clocksource_init(void __iomem *base, const char *name)
  55. {
  56. long rate = sp804_get_clock_rate(name);
  57. if (rate < 0)
  58. return;
  59. /* setup timer 0 as free-running clocksource */
  60. writel(0, base + TIMER_CTRL);
  61. writel(0xffffffff, base + TIMER_LOAD);
  62. writel(0xffffffff, base + TIMER_VALUE);
  63. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  64. base + TIMER_CTRL);
  65. clocksource_mmio_init(base + TIMER_VALUE, name,
  66. rate, 200, 32, clocksource_mmio_readl_down);
  67. }
  68. static void __iomem *clkevt_base;
  69. static unsigned long clkevt_reload;
  70. /*
  71. * IRQ handler for the timer
  72. */
  73. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  74. {
  75. struct clock_event_device *evt = dev_id;
  76. /* clear the interrupt */
  77. writel(1, clkevt_base + TIMER_INTCLR);
  78. evt->event_handler(evt);
  79. return IRQ_HANDLED;
  80. }
  81. static void sp804_set_mode(enum clock_event_mode mode,
  82. struct clock_event_device *evt)
  83. {
  84. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  85. writel(ctrl, clkevt_base + TIMER_CTRL);
  86. switch (mode) {
  87. case CLOCK_EVT_MODE_PERIODIC:
  88. writel(clkevt_reload, clkevt_base + TIMER_LOAD);
  89. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  90. break;
  91. case CLOCK_EVT_MODE_ONESHOT:
  92. /* period set, and timer enabled in 'next_event' hook */
  93. ctrl |= TIMER_CTRL_ONESHOT;
  94. break;
  95. case CLOCK_EVT_MODE_UNUSED:
  96. case CLOCK_EVT_MODE_SHUTDOWN:
  97. default:
  98. break;
  99. }
  100. writel(ctrl, clkevt_base + TIMER_CTRL);
  101. }
  102. static int sp804_set_next_event(unsigned long next,
  103. struct clock_event_device *evt)
  104. {
  105. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  106. writel(next, clkevt_base + TIMER_LOAD);
  107. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  108. return 0;
  109. }
  110. static struct clock_event_device sp804_clockevent = {
  111. .shift = 32,
  112. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  113. .set_mode = sp804_set_mode,
  114. .set_next_event = sp804_set_next_event,
  115. .rating = 300,
  116. .cpumask = cpu_all_mask,
  117. };
  118. static struct irqaction sp804_timer_irq = {
  119. .name = "timer",
  120. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  121. .handler = sp804_timer_interrupt,
  122. .dev_id = &sp804_clockevent,
  123. };
  124. void __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
  125. const char *name)
  126. {
  127. struct clock_event_device *evt = &sp804_clockevent;
  128. long rate = sp804_get_clock_rate(name);
  129. if (rate < 0)
  130. return;
  131. clkevt_base = base;
  132. clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
  133. evt->name = name;
  134. evt->irq = irq;
  135. evt->mult = div_sc(rate, NSEC_PER_SEC, evt->shift);
  136. evt->max_delta_ns = clockevent_delta2ns(0xffffffff, evt);
  137. evt->min_delta_ns = clockevent_delta2ns(0xf, evt);
  138. setup_irq(irq, &sp804_timer_irq);
  139. clockevents_register_device(evt);
  140. }