smp_twd.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /* set up by the platform code */
  27. static void __iomem *twd_base;
  28. static struct clk *twd_clk;
  29. static unsigned long twd_timer_rate;
  30. static DEFINE_PER_CPU(bool, percpu_setup_called);
  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. ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
  40. | TWD_TIMER_CONTROL_PERIODIC;
  41. __raw_writel(DIV_ROUND_CLOSEST(twd_timer_rate, HZ),
  42. 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 void twd_get_clock(struct device_node *np)
  192. {
  193. int err;
  194. if (np)
  195. twd_clk = of_clk_get(np, 0);
  196. else
  197. twd_clk = clk_get_sys("smp_twd", NULL);
  198. if (IS_ERR(twd_clk)) {
  199. pr_err("smp_twd: clock not found %d\n", (int) PTR_ERR(twd_clk));
  200. return;
  201. }
  202. err = clk_prepare_enable(twd_clk);
  203. if (err) {
  204. pr_err("smp_twd: clock failed to prepare+enable: %d\n", err);
  205. clk_put(twd_clk);
  206. return;
  207. }
  208. twd_timer_rate = clk_get_rate(twd_clk);
  209. }
  210. /*
  211. * Setup the local clock events for a CPU.
  212. */
  213. static int __cpuinit twd_timer_setup(struct clock_event_device *clk)
  214. {
  215. struct clock_event_device **this_cpu_clk;
  216. int cpu = smp_processor_id();
  217. /*
  218. * If the basic setup for this CPU has been done before don't
  219. * bother with the below.
  220. */
  221. if (per_cpu(percpu_setup_called, cpu)) {
  222. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  223. clockevents_register_device(*__this_cpu_ptr(twd_evt));
  224. enable_percpu_irq(clk->irq, 0);
  225. return 0;
  226. }
  227. per_cpu(percpu_setup_called, cpu) = true;
  228. twd_calibrate_rate();
  229. /*
  230. * The following is done once per CPU the first time .setup() is
  231. * called.
  232. */
  233. __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
  234. clk->name = "local_timer";
  235. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  236. CLOCK_EVT_FEAT_C3STOP;
  237. clk->rating = 350;
  238. clk->set_mode = twd_set_mode;
  239. clk->set_next_event = twd_set_next_event;
  240. clk->irq = twd_ppi;
  241. this_cpu_clk = __this_cpu_ptr(twd_evt);
  242. *this_cpu_clk = clk;
  243. clockevents_config_and_register(clk, twd_timer_rate,
  244. 0xf, 0xffffffff);
  245. enable_percpu_irq(clk->irq, 0);
  246. return 0;
  247. }
  248. static struct local_timer_ops twd_lt_ops __cpuinitdata = {
  249. .setup = twd_timer_setup,
  250. .stop = twd_timer_stop,
  251. };
  252. static int __init twd_local_timer_common_register(struct device_node *np)
  253. {
  254. int err;
  255. twd_evt = alloc_percpu(struct clock_event_device *);
  256. if (!twd_evt) {
  257. err = -ENOMEM;
  258. goto out_free;
  259. }
  260. err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
  261. if (err) {
  262. pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
  263. goto out_free;
  264. }
  265. err = local_timer_register(&twd_lt_ops);
  266. if (err)
  267. goto out_irq;
  268. twd_get_clock(np);
  269. return 0;
  270. out_irq:
  271. free_percpu_irq(twd_ppi, twd_evt);
  272. out_free:
  273. iounmap(twd_base);
  274. twd_base = NULL;
  275. free_percpu(twd_evt);
  276. return err;
  277. }
  278. int __init twd_local_timer_register(struct twd_local_timer *tlt)
  279. {
  280. if (twd_base || twd_evt)
  281. return -EBUSY;
  282. twd_ppi = tlt->res[1].start;
  283. twd_base = ioremap(tlt->res[0].start, resource_size(&tlt->res[0]));
  284. if (!twd_base)
  285. return -ENOMEM;
  286. return twd_local_timer_common_register(NULL);
  287. }
  288. #ifdef CONFIG_OF
  289. const static struct of_device_id twd_of_match[] __initconst = {
  290. { .compatible = "arm,cortex-a9-twd-timer", },
  291. { .compatible = "arm,cortex-a5-twd-timer", },
  292. { .compatible = "arm,arm11mp-twd-timer", },
  293. { },
  294. };
  295. void __init twd_local_timer_of_register(void)
  296. {
  297. struct device_node *np;
  298. int err;
  299. np = of_find_matching_node(NULL, twd_of_match);
  300. if (!np)
  301. return;
  302. twd_ppi = irq_of_parse_and_map(np, 0);
  303. if (!twd_ppi) {
  304. err = -EINVAL;
  305. goto out;
  306. }
  307. twd_base = of_iomap(np, 0);
  308. if (!twd_base) {
  309. err = -ENOMEM;
  310. goto out;
  311. }
  312. err = twd_local_timer_common_register(np);
  313. out:
  314. WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
  315. }
  316. #endif