smp_twd.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/cpufreq.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/smp.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/irq.h>
  22. #include <linux/io.h>
  23. #include <asm/smp_twd.h>
  24. #include <asm/localtimer.h>
  25. #include <asm/hardware/gic.h>
  26. /* set up by the platform code */
  27. void __iomem *twd_base;
  28. static struct clk *twd_clk;
  29. static unsigned long twd_timer_rate;
  30. static struct clock_event_device __percpu **twd_evt;
  31. static void twd_set_mode(enum clock_event_mode mode,
  32. struct clock_event_device *clk)
  33. {
  34. unsigned long ctrl;
  35. switch (mode) {
  36. case CLOCK_EVT_MODE_PERIODIC:
  37. /* timer load already set up */
  38. ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
  39. | TWD_TIMER_CONTROL_PERIODIC;
  40. __raw_writel(twd_timer_rate / HZ, twd_base + TWD_TIMER_LOAD);
  41. break;
  42. case CLOCK_EVT_MODE_ONESHOT:
  43. /* period set, and timer enabled in 'next_event' hook */
  44. ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
  45. break;
  46. case CLOCK_EVT_MODE_UNUSED:
  47. case CLOCK_EVT_MODE_SHUTDOWN:
  48. default:
  49. ctrl = 0;
  50. }
  51. __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
  52. }
  53. static int twd_set_next_event(unsigned long evt,
  54. struct clock_event_device *unused)
  55. {
  56. unsigned long ctrl = __raw_readl(twd_base + TWD_TIMER_CONTROL);
  57. ctrl |= TWD_TIMER_CONTROL_ENABLE;
  58. __raw_writel(evt, twd_base + TWD_TIMER_COUNTER);
  59. __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
  60. return 0;
  61. }
  62. /*
  63. * local_timer_ack: checks for a local timer interrupt.
  64. *
  65. * If a local timer interrupt has occurred, acknowledge and return 1.
  66. * Otherwise, return 0.
  67. */
  68. int twd_timer_ack(void)
  69. {
  70. if (__raw_readl(twd_base + TWD_TIMER_INTSTAT)) {
  71. __raw_writel(1, twd_base + TWD_TIMER_INTSTAT);
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. static void twd_timer_stop(struct clock_event_device *clk)
  77. {
  78. twd_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  79. disable_percpu_irq(clk->irq);
  80. }
  81. /* Temporary hack to be removed when all TWD users are converted to
  82. the new registration interface */
  83. void local_timer_stop(struct clock_event_device *clk)
  84. __attribute__ ((alias ("twd_timer_stop")));
  85. #ifdef CONFIG_CPU_FREQ
  86. /*
  87. * Updates clockevent frequency when the cpu frequency changes.
  88. * Called on the cpu that is changing frequency with interrupts disabled.
  89. */
  90. static void twd_update_frequency(void *data)
  91. {
  92. twd_timer_rate = clk_get_rate(twd_clk);
  93. clockevents_update_freq(*__this_cpu_ptr(twd_evt), twd_timer_rate);
  94. }
  95. static int twd_cpufreq_transition(struct notifier_block *nb,
  96. unsigned long state, void *data)
  97. {
  98. struct cpufreq_freqs *freqs = data;
  99. /*
  100. * The twd clock events must be reprogrammed to account for the new
  101. * frequency. The timer is local to a cpu, so cross-call to the
  102. * changing cpu.
  103. */
  104. if (state == CPUFREQ_POSTCHANGE || state == CPUFREQ_RESUMECHANGE)
  105. smp_call_function_single(freqs->cpu, twd_update_frequency,
  106. NULL, 1);
  107. return NOTIFY_OK;
  108. }
  109. static struct notifier_block twd_cpufreq_nb = {
  110. .notifier_call = twd_cpufreq_transition,
  111. };
  112. static int twd_cpufreq_init(void)
  113. {
  114. if (!IS_ERR(twd_clk))
  115. return cpufreq_register_notifier(&twd_cpufreq_nb,
  116. CPUFREQ_TRANSITION_NOTIFIER);
  117. return 0;
  118. }
  119. core_initcall(twd_cpufreq_init);
  120. #endif
  121. static void __cpuinit twd_calibrate_rate(void)
  122. {
  123. unsigned long count;
  124. u64 waitjiffies;
  125. /*
  126. * If this is the first time round, we need to work out how fast
  127. * the timer ticks
  128. */
  129. if (twd_timer_rate == 0) {
  130. printk(KERN_INFO "Calibrating local timer... ");
  131. /* Wait for a tick to start */
  132. waitjiffies = get_jiffies_64() + 1;
  133. while (get_jiffies_64() < waitjiffies)
  134. udelay(10);
  135. /* OK, now the tick has started, let's get the timer going */
  136. waitjiffies += 5;
  137. /* enable, no interrupt or reload */
  138. __raw_writel(0x1, twd_base + TWD_TIMER_CONTROL);
  139. /* maximum value */
  140. __raw_writel(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
  141. while (get_jiffies_64() < waitjiffies)
  142. udelay(10);
  143. count = __raw_readl(twd_base + TWD_TIMER_COUNTER);
  144. twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  145. printk("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
  146. (twd_timer_rate / 10000) % 100);
  147. }
  148. }
  149. static irqreturn_t twd_handler(int irq, void *dev_id)
  150. {
  151. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  152. if (twd_timer_ack()) {
  153. evt->event_handler(evt);
  154. return IRQ_HANDLED;
  155. }
  156. return IRQ_NONE;
  157. }
  158. static struct clk *twd_get_clock(void)
  159. {
  160. struct clk *clk;
  161. int err;
  162. clk = clk_get_sys("smp_twd", NULL);
  163. if (IS_ERR(clk)) {
  164. pr_err("smp_twd: clock not found: %d\n", (int)PTR_ERR(clk));
  165. return clk;
  166. }
  167. err = clk_prepare(clk);
  168. if (err) {
  169. pr_err("smp_twd: clock failed to prepare: %d\n", err);
  170. clk_put(clk);
  171. return ERR_PTR(err);
  172. }
  173. err = clk_enable(clk);
  174. if (err) {
  175. pr_err("smp_twd: clock failed to enable: %d\n", err);
  176. clk_unprepare(clk);
  177. clk_put(clk);
  178. return ERR_PTR(err);
  179. }
  180. return clk;
  181. }
  182. /*
  183. * Setup the local clock events for a CPU.
  184. */
  185. void __cpuinit twd_timer_setup(struct clock_event_device *clk)
  186. {
  187. struct clock_event_device **this_cpu_clk;
  188. if (!twd_evt) {
  189. int err;
  190. twd_evt = alloc_percpu(struct clock_event_device *);
  191. if (!twd_evt) {
  192. pr_err("twd: can't allocate memory\n");
  193. return;
  194. }
  195. err = request_percpu_irq(clk->irq, twd_handler,
  196. "twd", twd_evt);
  197. if (err) {
  198. pr_err("twd: can't register interrupt %d (%d)\n",
  199. clk->irq, err);
  200. return;
  201. }
  202. }
  203. if (!twd_clk)
  204. twd_clk = twd_get_clock();
  205. if (!IS_ERR_OR_NULL(twd_clk))
  206. twd_timer_rate = clk_get_rate(twd_clk);
  207. else
  208. twd_calibrate_rate();
  209. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  210. clk->name = "local_timer";
  211. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  212. CLOCK_EVT_FEAT_C3STOP;
  213. clk->rating = 350;
  214. clk->set_mode = twd_set_mode;
  215. clk->set_next_event = twd_set_next_event;
  216. this_cpu_clk = __this_cpu_ptr(twd_evt);
  217. *this_cpu_clk = clk;
  218. clockevents_config_and_register(clk, twd_timer_rate,
  219. 0xf, 0xffffffff);
  220. enable_percpu_irq(clk->irq, 0);
  221. }