smp_twd.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_address.h>
  25. #include <asm/smp_twd.h>
  26. #include <asm/localtimer.h>
  27. #include <asm/hardware/gic.h>
  28. /* set up by the platform code */
  29. static void __iomem *twd_base;
  30. static struct clk *twd_clk;
  31. static unsigned long twd_timer_rate;
  32. static struct clock_event_device __percpu **twd_evt;
  33. static int twd_ppi;
  34. static void twd_set_mode(enum clock_event_mode mode,
  35. struct clock_event_device *clk)
  36. {
  37. unsigned long ctrl;
  38. switch (mode) {
  39. case CLOCK_EVT_MODE_PERIODIC:
  40. /* timer load already set up */
  41. ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
  42. | TWD_TIMER_CONTROL_PERIODIC;
  43. __raw_writel(twd_timer_rate / HZ, twd_base + TWD_TIMER_LOAD);
  44. break;
  45. case CLOCK_EVT_MODE_ONESHOT:
  46. /* period set, and timer enabled in 'next_event' hook */
  47. ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
  48. break;
  49. case CLOCK_EVT_MODE_UNUSED:
  50. case CLOCK_EVT_MODE_SHUTDOWN:
  51. default:
  52. ctrl = 0;
  53. }
  54. __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
  55. }
  56. static int twd_set_next_event(unsigned long evt,
  57. struct clock_event_device *unused)
  58. {
  59. unsigned long ctrl = __raw_readl(twd_base + TWD_TIMER_CONTROL);
  60. ctrl |= TWD_TIMER_CONTROL_ENABLE;
  61. __raw_writel(evt, twd_base + TWD_TIMER_COUNTER);
  62. __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
  63. return 0;
  64. }
  65. /*
  66. * local_timer_ack: checks for a local timer interrupt.
  67. *
  68. * If a local timer interrupt has occurred, acknowledge and return 1.
  69. * Otherwise, return 0.
  70. */
  71. static int twd_timer_ack(void)
  72. {
  73. if (__raw_readl(twd_base + TWD_TIMER_INTSTAT)) {
  74. __raw_writel(1, twd_base + TWD_TIMER_INTSTAT);
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static void twd_timer_stop(struct clock_event_device *clk)
  80. {
  81. twd_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  82. disable_percpu_irq(clk->irq);
  83. }
  84. #ifdef CONFIG_CPU_FREQ
  85. /*
  86. * Updates clockevent frequency when the cpu frequency changes.
  87. * Called on the cpu that is changing frequency with interrupts disabled.
  88. */
  89. static void twd_update_frequency(void *data)
  90. {
  91. twd_timer_rate = clk_get_rate(twd_clk);
  92. clockevents_update_freq(*__this_cpu_ptr(twd_evt), twd_timer_rate);
  93. }
  94. static int twd_cpufreq_transition(struct notifier_block *nb,
  95. unsigned long state, void *data)
  96. {
  97. struct cpufreq_freqs *freqs = data;
  98. /*
  99. * The twd clock events must be reprogrammed to account for the new
  100. * frequency. The timer is local to a cpu, so cross-call to the
  101. * changing cpu.
  102. *
  103. * Only wait for it to finish, if the cpu is active to avoid
  104. * deadlock when cpu1 is spinning on while(!cpu_active(cpu1)) during
  105. * booting of that cpu.
  106. */
  107. if (state == CPUFREQ_POSTCHANGE || state == CPUFREQ_RESUMECHANGE)
  108. smp_call_function_single(freqs->cpu, twd_update_frequency,
  109. NULL, cpu_active(freqs->cpu));
  110. return NOTIFY_OK;
  111. }
  112. static struct notifier_block twd_cpufreq_nb = {
  113. .notifier_call = twd_cpufreq_transition,
  114. };
  115. static int twd_cpufreq_init(void)
  116. {
  117. if (twd_evt && *__this_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
  118. return cpufreq_register_notifier(&twd_cpufreq_nb,
  119. CPUFREQ_TRANSITION_NOTIFIER);
  120. return 0;
  121. }
  122. core_initcall(twd_cpufreq_init);
  123. #endif
  124. static void __cpuinit twd_calibrate_rate(void)
  125. {
  126. unsigned long count;
  127. u64 waitjiffies;
  128. /*
  129. * If this is the first time round, we need to work out how fast
  130. * the timer ticks
  131. */
  132. if (twd_timer_rate == 0) {
  133. printk(KERN_INFO "Calibrating local timer... ");
  134. /* Wait for a tick to start */
  135. waitjiffies = get_jiffies_64() + 1;
  136. while (get_jiffies_64() < waitjiffies)
  137. udelay(10);
  138. /* OK, now the tick has started, let's get the timer going */
  139. waitjiffies += 5;
  140. /* enable, no interrupt or reload */
  141. __raw_writel(0x1, twd_base + TWD_TIMER_CONTROL);
  142. /* maximum value */
  143. __raw_writel(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
  144. while (get_jiffies_64() < waitjiffies)
  145. udelay(10);
  146. count = __raw_readl(twd_base + TWD_TIMER_COUNTER);
  147. twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  148. printk("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
  149. (twd_timer_rate / 10000) % 100);
  150. }
  151. }
  152. static irqreturn_t twd_handler(int irq, void *dev_id)
  153. {
  154. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  155. if (twd_timer_ack()) {
  156. evt->event_handler(evt);
  157. return IRQ_HANDLED;
  158. }
  159. return IRQ_NONE;
  160. }
  161. static struct clk *twd_get_clock(void)
  162. {
  163. struct clk *clk;
  164. int err;
  165. clk = clk_get_sys("smp_twd", NULL);
  166. if (IS_ERR(clk)) {
  167. pr_err("smp_twd: clock not found: %d\n", (int)PTR_ERR(clk));
  168. return clk;
  169. }
  170. err = clk_prepare(clk);
  171. if (err) {
  172. pr_err("smp_twd: clock failed to prepare: %d\n", err);
  173. clk_put(clk);
  174. return ERR_PTR(err);
  175. }
  176. err = clk_enable(clk);
  177. if (err) {
  178. pr_err("smp_twd: clock failed to enable: %d\n", err);
  179. clk_unprepare(clk);
  180. clk_put(clk);
  181. return ERR_PTR(err);
  182. }
  183. return clk;
  184. }
  185. /*
  186. * Setup the local clock events for a CPU.
  187. */
  188. static int __cpuinit twd_timer_setup(struct clock_event_device *clk)
  189. {
  190. struct clock_event_device **this_cpu_clk;
  191. if (!twd_clk)
  192. twd_clk = twd_get_clock();
  193. if (!IS_ERR_OR_NULL(twd_clk))
  194. twd_timer_rate = clk_get_rate(twd_clk);
  195. else
  196. twd_calibrate_rate();
  197. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  198. clk->name = "local_timer";
  199. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  200. CLOCK_EVT_FEAT_C3STOP;
  201. clk->rating = 350;
  202. clk->set_mode = twd_set_mode;
  203. clk->set_next_event = twd_set_next_event;
  204. clk->irq = twd_ppi;
  205. this_cpu_clk = __this_cpu_ptr(twd_evt);
  206. *this_cpu_clk = clk;
  207. clockevents_config_and_register(clk, twd_timer_rate,
  208. 0xf, 0xffffffff);
  209. enable_percpu_irq(clk->irq, 0);
  210. return 0;
  211. }
  212. static struct local_timer_ops twd_lt_ops __cpuinitdata = {
  213. .setup = twd_timer_setup,
  214. .stop = twd_timer_stop,
  215. };
  216. static int __init twd_local_timer_common_register(void)
  217. {
  218. int err;
  219. twd_evt = alloc_percpu(struct clock_event_device *);
  220. if (!twd_evt) {
  221. err = -ENOMEM;
  222. goto out_free;
  223. }
  224. err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
  225. if (err) {
  226. pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
  227. goto out_free;
  228. }
  229. err = local_timer_register(&twd_lt_ops);
  230. if (err)
  231. goto out_irq;
  232. return 0;
  233. out_irq:
  234. free_percpu_irq(twd_ppi, twd_evt);
  235. out_free:
  236. iounmap(twd_base);
  237. twd_base = NULL;
  238. free_percpu(twd_evt);
  239. return err;
  240. }
  241. int __init twd_local_timer_register(struct twd_local_timer *tlt)
  242. {
  243. if (twd_base || twd_evt)
  244. return -EBUSY;
  245. twd_ppi = tlt->res[1].start;
  246. twd_base = ioremap(tlt->res[0].start, resource_size(&tlt->res[0]));
  247. if (!twd_base)
  248. return -ENOMEM;
  249. return twd_local_timer_common_register();
  250. }
  251. #ifdef CONFIG_OF
  252. const static struct of_device_id twd_of_match[] __initconst = {
  253. { .compatible = "arm,cortex-a9-twd-timer", },
  254. { .compatible = "arm,cortex-a5-twd-timer", },
  255. { .compatible = "arm,arm11mp-twd-timer", },
  256. { },
  257. };
  258. void __init twd_local_timer_of_register(void)
  259. {
  260. struct device_node *np;
  261. int err;
  262. np = of_find_matching_node(NULL, twd_of_match);
  263. if (!np) {
  264. err = -ENODEV;
  265. goto out;
  266. }
  267. twd_ppi = irq_of_parse_and_map(np, 0);
  268. if (!twd_ppi) {
  269. err = -EINVAL;
  270. goto out;
  271. }
  272. twd_base = of_iomap(np, 0);
  273. if (!twd_base) {
  274. err = -ENOMEM;
  275. goto out;
  276. }
  277. err = twd_local_timer_common_register();
  278. out:
  279. WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
  280. }
  281. #endif