smp_twd.c 8.3 KB

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