smp_twd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_plat.h>
  25. #include <asm/smp_twd.h>
  26. #include <asm/localtimer.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 DEFINE_PER_CPU(bool, percpu_setup_called);
  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. ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
  41. | TWD_TIMER_CONTROL_PERIODIC;
  42. __raw_writel(DIV_ROUND_CLOSEST(twd_timer_rate, HZ),
  43. 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_COMMON_CLK
  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 *new_rate)
  90. {
  91. twd_timer_rate = *((unsigned long *) new_rate);
  92. clockevents_update_freq(*__this_cpu_ptr(twd_evt), twd_timer_rate);
  93. }
  94. static int twd_rate_change(struct notifier_block *nb,
  95. unsigned long flags, void *data)
  96. {
  97. struct clk_notifier_data *cnd = 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. if (flags == POST_RATE_CHANGE)
  104. smp_call_function(twd_update_frequency,
  105. (void *)&cnd->new_rate, 1);
  106. return NOTIFY_OK;
  107. }
  108. static struct notifier_block twd_clk_nb = {
  109. .notifier_call = twd_rate_change,
  110. };
  111. static int twd_clk_init(void)
  112. {
  113. if (twd_evt && *__this_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
  114. return clk_notifier_register(twd_clk, &twd_clk_nb);
  115. return 0;
  116. }
  117. core_initcall(twd_clk_init);
  118. #elif defined (CONFIG_CPU_FREQ)
  119. #include <linux/cpufreq.h>
  120. /*
  121. * Updates clockevent frequency when the cpu frequency changes.
  122. * Called on the cpu that is changing frequency with interrupts disabled.
  123. */
  124. static void twd_update_frequency(void *data)
  125. {
  126. twd_timer_rate = clk_get_rate(twd_clk);
  127. clockevents_update_freq(*__this_cpu_ptr(twd_evt), twd_timer_rate);
  128. }
  129. static int twd_cpufreq_transition(struct notifier_block *nb,
  130. unsigned long state, void *data)
  131. {
  132. struct cpufreq_freqs *freqs = data;
  133. /*
  134. * The twd clock events must be reprogrammed to account for the new
  135. * frequency. The timer is local to a cpu, so cross-call to the
  136. * changing cpu.
  137. */
  138. if (state == CPUFREQ_POSTCHANGE || state == CPUFREQ_RESUMECHANGE)
  139. smp_call_function_single(freqs->cpu, twd_update_frequency,
  140. NULL, 1);
  141. return NOTIFY_OK;
  142. }
  143. static struct notifier_block twd_cpufreq_nb = {
  144. .notifier_call = twd_cpufreq_transition,
  145. };
  146. static int twd_cpufreq_init(void)
  147. {
  148. if (twd_evt && *__this_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
  149. return cpufreq_register_notifier(&twd_cpufreq_nb,
  150. CPUFREQ_TRANSITION_NOTIFIER);
  151. return 0;
  152. }
  153. core_initcall(twd_cpufreq_init);
  154. #endif
  155. static void __cpuinit twd_calibrate_rate(void)
  156. {
  157. unsigned long count;
  158. u64 waitjiffies;
  159. /*
  160. * If this is the first time round, we need to work out how fast
  161. * the timer ticks
  162. */
  163. if (twd_timer_rate == 0) {
  164. printk(KERN_INFO "Calibrating local timer... ");
  165. /* Wait for a tick to start */
  166. waitjiffies = get_jiffies_64() + 1;
  167. while (get_jiffies_64() < waitjiffies)
  168. udelay(10);
  169. /* OK, now the tick has started, let's get the timer going */
  170. waitjiffies += 5;
  171. /* enable, no interrupt or reload */
  172. __raw_writel(0x1, twd_base + TWD_TIMER_CONTROL);
  173. /* maximum value */
  174. __raw_writel(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
  175. while (get_jiffies_64() < waitjiffies)
  176. udelay(10);
  177. count = __raw_readl(twd_base + TWD_TIMER_COUNTER);
  178. twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  179. printk("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
  180. (twd_timer_rate / 10000) % 100);
  181. }
  182. }
  183. static irqreturn_t twd_handler(int irq, void *dev_id)
  184. {
  185. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  186. if (twd_timer_ack()) {
  187. evt->event_handler(evt);
  188. return IRQ_HANDLED;
  189. }
  190. return IRQ_NONE;
  191. }
  192. static void twd_get_clock(struct device_node *np)
  193. {
  194. int err;
  195. if (np)
  196. twd_clk = of_clk_get(np, 0);
  197. else
  198. twd_clk = clk_get_sys("smp_twd", NULL);
  199. if (IS_ERR(twd_clk)) {
  200. pr_err("smp_twd: clock not found %d\n", (int) PTR_ERR(twd_clk));
  201. return;
  202. }
  203. err = clk_prepare_enable(twd_clk);
  204. if (err) {
  205. pr_err("smp_twd: clock failed to prepare+enable: %d\n", err);
  206. clk_put(twd_clk);
  207. return;
  208. }
  209. twd_timer_rate = clk_get_rate(twd_clk);
  210. }
  211. /*
  212. * Setup the local clock events for a CPU.
  213. */
  214. static int __cpuinit twd_timer_setup(struct clock_event_device *clk)
  215. {
  216. struct clock_event_device **this_cpu_clk;
  217. int cpu = smp_processor_id();
  218. /*
  219. * If the basic setup for this CPU has been done before don't
  220. * bother with the below.
  221. */
  222. if (per_cpu(percpu_setup_called, cpu)) {
  223. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  224. clockevents_register_device(*__this_cpu_ptr(twd_evt));
  225. enable_percpu_irq(clk->irq, 0);
  226. return 0;
  227. }
  228. per_cpu(percpu_setup_called, cpu) = true;
  229. twd_calibrate_rate();
  230. /*
  231. * The following is done once per CPU the first time .setup() is
  232. * called.
  233. */
  234. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  235. clk->name = "local_timer";
  236. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  237. CLOCK_EVT_FEAT_C3STOP;
  238. clk->rating = 350;
  239. clk->set_mode = twd_set_mode;
  240. clk->set_next_event = twd_set_next_event;
  241. clk->irq = twd_ppi;
  242. this_cpu_clk = __this_cpu_ptr(twd_evt);
  243. *this_cpu_clk = clk;
  244. clockevents_config_and_register(clk, twd_timer_rate,
  245. 0xf, 0xffffffff);
  246. enable_percpu_irq(clk->irq, 0);
  247. return 0;
  248. }
  249. static struct local_timer_ops twd_lt_ops __cpuinitdata = {
  250. .setup = twd_timer_setup,
  251. .stop = twd_timer_stop,
  252. };
  253. static int __init twd_local_timer_common_register(struct device_node *np)
  254. {
  255. int err;
  256. twd_evt = alloc_percpu(struct clock_event_device *);
  257. if (!twd_evt) {
  258. err = -ENOMEM;
  259. goto out_free;
  260. }
  261. err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
  262. if (err) {
  263. pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
  264. goto out_free;
  265. }
  266. err = local_timer_register(&twd_lt_ops);
  267. if (err)
  268. goto out_irq;
  269. twd_get_clock(np);
  270. return 0;
  271. out_irq:
  272. free_percpu_irq(twd_ppi, twd_evt);
  273. out_free:
  274. iounmap(twd_base);
  275. twd_base = NULL;
  276. free_percpu(twd_evt);
  277. return err;
  278. }
  279. int __init twd_local_timer_register(struct twd_local_timer *tlt)
  280. {
  281. if (twd_base || twd_evt)
  282. return -EBUSY;
  283. twd_ppi = tlt->res[1].start;
  284. twd_base = ioremap(tlt->res[0].start, resource_size(&tlt->res[0]));
  285. if (!twd_base)
  286. return -ENOMEM;
  287. return twd_local_timer_common_register(NULL);
  288. }
  289. #ifdef CONFIG_OF
  290. const static struct of_device_id twd_of_match[] __initconst = {
  291. { .compatible = "arm,cortex-a9-twd-timer", },
  292. { .compatible = "arm,cortex-a5-twd-timer", },
  293. { .compatible = "arm,arm11mp-twd-timer", },
  294. { },
  295. };
  296. void __init twd_local_timer_of_register(void)
  297. {
  298. struct device_node *np;
  299. int err;
  300. if (!is_smp() || !setup_max_cpus)
  301. return;
  302. np = of_find_matching_node(NULL, twd_of_match);
  303. if (!np)
  304. return;
  305. twd_ppi = irq_of_parse_and_map(np, 0);
  306. if (!twd_ppi) {
  307. err = -EINVAL;
  308. goto out;
  309. }
  310. twd_base = of_iomap(np, 0);
  311. if (!twd_base) {
  312. err = -ENOMEM;
  313. goto out;
  314. }
  315. err = twd_local_timer_common_register(np);
  316. out:
  317. WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
  318. }
  319. #endif