smp_twd.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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(clk);
  201. if (err) {
  202. pr_err("smp_twd: clock failed to prepare: %d\n", err);
  203. clk_put(clk);
  204. return ERR_PTR(err);
  205. }
  206. err = clk_enable(clk);
  207. if (err) {
  208. pr_err("smp_twd: clock failed to enable: %d\n", err);
  209. clk_unprepare(clk);
  210. clk_put(clk);
  211. return ERR_PTR(err);
  212. }
  213. return clk;
  214. }
  215. /*
  216. * Setup the local clock events for a CPU.
  217. */
  218. static int __cpuinit twd_timer_setup(struct clock_event_device *clk)
  219. {
  220. struct clock_event_device **this_cpu_clk;
  221. if (!twd_clk)
  222. twd_clk = twd_get_clock();
  223. if (!IS_ERR_OR_NULL(twd_clk))
  224. twd_timer_rate = clk_get_rate(twd_clk);
  225. else
  226. twd_calibrate_rate();
  227. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  228. clk->name = "local_timer";
  229. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  230. CLOCK_EVT_FEAT_C3STOP;
  231. clk->rating = 350;
  232. clk->set_mode = twd_set_mode;
  233. clk->set_next_event = twd_set_next_event;
  234. clk->irq = twd_ppi;
  235. this_cpu_clk = __this_cpu_ptr(twd_evt);
  236. *this_cpu_clk = clk;
  237. clockevents_config_and_register(clk, twd_timer_rate,
  238. 0xf, 0xffffffff);
  239. enable_percpu_irq(clk->irq, 0);
  240. return 0;
  241. }
  242. static struct local_timer_ops twd_lt_ops __cpuinitdata = {
  243. .setup = twd_timer_setup,
  244. .stop = twd_timer_stop,
  245. };
  246. static int __init twd_local_timer_common_register(void)
  247. {
  248. int err;
  249. twd_evt = alloc_percpu(struct clock_event_device *);
  250. if (!twd_evt) {
  251. err = -ENOMEM;
  252. goto out_free;
  253. }
  254. err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
  255. if (err) {
  256. pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
  257. goto out_free;
  258. }
  259. err = local_timer_register(&twd_lt_ops);
  260. if (err)
  261. goto out_irq;
  262. return 0;
  263. out_irq:
  264. free_percpu_irq(twd_ppi, twd_evt);
  265. out_free:
  266. iounmap(twd_base);
  267. twd_base = NULL;
  268. free_percpu(twd_evt);
  269. return err;
  270. }
  271. int __init twd_local_timer_register(struct twd_local_timer *tlt)
  272. {
  273. if (twd_base || twd_evt)
  274. return -EBUSY;
  275. twd_ppi = tlt->res[1].start;
  276. twd_base = ioremap(tlt->res[0].start, resource_size(&tlt->res[0]));
  277. if (!twd_base)
  278. return -ENOMEM;
  279. return twd_local_timer_common_register();
  280. }
  281. #ifdef CONFIG_OF
  282. const static struct of_device_id twd_of_match[] __initconst = {
  283. { .compatible = "arm,cortex-a9-twd-timer", },
  284. { .compatible = "arm,cortex-a5-twd-timer", },
  285. { .compatible = "arm,arm11mp-twd-timer", },
  286. { },
  287. };
  288. void __init twd_local_timer_of_register(void)
  289. {
  290. struct device_node *np;
  291. int err;
  292. np = of_find_matching_node(NULL, twd_of_match);
  293. if (!np) {
  294. err = -ENODEV;
  295. goto out;
  296. }
  297. twd_ppi = irq_of_parse_and_map(np, 0);
  298. if (!twd_ppi) {
  299. err = -EINVAL;
  300. goto out;
  301. }
  302. twd_base = of_iomap(np, 0);
  303. if (!twd_base) {
  304. err = -ENOMEM;
  305. goto out;
  306. }
  307. err = twd_local_timer_common_register();
  308. out:
  309. WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
  310. }
  311. #endif