smp_twd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * linux/arch/arm/kernel/smp_twd.c
  3. *
  4. * Copyright (C) 2002 ARM Ltd.
  5. * All Rights Reserved
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/smp.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/clockchips.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <asm/smp_twd.h>
  23. #include <asm/localtimer.h>
  24. #include <asm/hardware/gic.h>
  25. /* set up by the platform code */
  26. void __iomem *twd_base;
  27. static struct clk *twd_clk;
  28. static unsigned long twd_timer_rate;
  29. static struct clock_event_device __percpu **twd_evt;
  30. static void twd_set_mode(enum clock_event_mode mode,
  31. struct clock_event_device *clk)
  32. {
  33. unsigned long ctrl;
  34. switch (mode) {
  35. case CLOCK_EVT_MODE_PERIODIC:
  36. /* timer load already set up */
  37. ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
  38. | TWD_TIMER_CONTROL_PERIODIC;
  39. __raw_writel(twd_timer_rate / HZ, twd_base + TWD_TIMER_LOAD);
  40. break;
  41. case CLOCK_EVT_MODE_ONESHOT:
  42. /* period set, and timer enabled in 'next_event' hook */
  43. ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
  44. break;
  45. case CLOCK_EVT_MODE_UNUSED:
  46. case CLOCK_EVT_MODE_SHUTDOWN:
  47. default:
  48. ctrl = 0;
  49. }
  50. __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
  51. }
  52. static int twd_set_next_event(unsigned long evt,
  53. struct clock_event_device *unused)
  54. {
  55. unsigned long ctrl = __raw_readl(twd_base + TWD_TIMER_CONTROL);
  56. ctrl |= TWD_TIMER_CONTROL_ENABLE;
  57. __raw_writel(evt, twd_base + TWD_TIMER_COUNTER);
  58. __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
  59. return 0;
  60. }
  61. /*
  62. * local_timer_ack: checks for a local timer interrupt.
  63. *
  64. * If a local timer interrupt has occurred, acknowledge and return 1.
  65. * Otherwise, return 0.
  66. */
  67. int twd_timer_ack(void)
  68. {
  69. if (__raw_readl(twd_base + TWD_TIMER_INTSTAT)) {
  70. __raw_writel(1, twd_base + TWD_TIMER_INTSTAT);
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. void twd_timer_stop(struct clock_event_device *clk)
  76. {
  77. twd_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  78. disable_percpu_irq(clk->irq);
  79. }
  80. static void __cpuinit twd_calibrate_rate(void)
  81. {
  82. unsigned long count;
  83. u64 waitjiffies;
  84. /*
  85. * If this is the first time round, we need to work out how fast
  86. * the timer ticks
  87. */
  88. if (twd_timer_rate == 0) {
  89. printk(KERN_INFO "Calibrating local timer... ");
  90. /* Wait for a tick to start */
  91. waitjiffies = get_jiffies_64() + 1;
  92. while (get_jiffies_64() < waitjiffies)
  93. udelay(10);
  94. /* OK, now the tick has started, let's get the timer going */
  95. waitjiffies += 5;
  96. /* enable, no interrupt or reload */
  97. __raw_writel(0x1, twd_base + TWD_TIMER_CONTROL);
  98. /* maximum value */
  99. __raw_writel(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
  100. while (get_jiffies_64() < waitjiffies)
  101. udelay(10);
  102. count = __raw_readl(twd_base + TWD_TIMER_COUNTER);
  103. twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  104. printk("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
  105. (twd_timer_rate / 10000) % 100);
  106. }
  107. }
  108. static irqreturn_t twd_handler(int irq, void *dev_id)
  109. {
  110. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  111. if (twd_timer_ack()) {
  112. evt->event_handler(evt);
  113. return IRQ_HANDLED;
  114. }
  115. return IRQ_NONE;
  116. }
  117. static struct clk *twd_get_clock(void)
  118. {
  119. struct clk *clk;
  120. int err;
  121. clk = clk_get_sys("smp_twd", NULL);
  122. if (IS_ERR(clk)) {
  123. pr_err("smp_twd: clock not found: %d\n", (int)PTR_ERR(clk));
  124. return clk;
  125. }
  126. err = clk_prepare(clk);
  127. if (err) {
  128. pr_err("smp_twd: clock failed to prepare: %d\n", err);
  129. clk_put(clk);
  130. return ERR_PTR(err);
  131. }
  132. err = clk_enable(clk);
  133. if (err) {
  134. pr_err("smp_twd: clock failed to enable: %d\n", err);
  135. clk_unprepare(clk);
  136. clk_put(clk);
  137. return ERR_PTR(err);
  138. }
  139. return clk;
  140. }
  141. /*
  142. * Setup the local clock events for a CPU.
  143. */
  144. void __cpuinit twd_timer_setup(struct clock_event_device *clk)
  145. {
  146. struct clock_event_device **this_cpu_clk;
  147. if (!twd_evt) {
  148. int err;
  149. twd_evt = alloc_percpu(struct clock_event_device *);
  150. if (!twd_evt) {
  151. pr_err("twd: can't allocate memory\n");
  152. return;
  153. }
  154. err = request_percpu_irq(clk->irq, twd_handler,
  155. "twd", twd_evt);
  156. if (err) {
  157. pr_err("twd: can't register interrupt %d (%d)\n",
  158. clk->irq, err);
  159. return;
  160. }
  161. }
  162. if (!twd_clk)
  163. twd_clk = twd_get_clock();
  164. if (!IS_ERR_OR_NULL(twd_clk))
  165. twd_timer_rate = clk_get_rate(twd_clk);
  166. else
  167. twd_calibrate_rate();
  168. clk->name = "local_timer";
  169. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  170. CLOCK_EVT_FEAT_C3STOP;
  171. clk->rating = 350;
  172. clk->set_mode = twd_set_mode;
  173. clk->set_next_event = twd_set_next_event;
  174. this_cpu_clk = __this_cpu_ptr(twd_evt);
  175. *this_cpu_clk = clk;
  176. clockevents_config_and_register(clk, twd_timer_rate,
  177. 0xf, 0xffffffff);
  178. enable_percpu_irq(clk->irq, 0);
  179. }