smp_twd.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/cpu.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_plat.h>
  26. #include <asm/smp_twd.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 int twd_shutdown(struct clock_event_device *clk)
  35. {
  36. writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
  37. return 0;
  38. }
  39. static int twd_set_oneshot(struct clock_event_device *clk)
  40. {
  41. /* period set, and timer enabled in 'next_event' hook */
  42. writel_relaxed(TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT,
  43. twd_base + TWD_TIMER_CONTROL);
  44. return 0;
  45. }
  46. static int twd_set_periodic(struct clock_event_device *clk)
  47. {
  48. unsigned long ctrl = TWD_TIMER_CONTROL_ENABLE |
  49. TWD_TIMER_CONTROL_IT_ENABLE |
  50. TWD_TIMER_CONTROL_PERIODIC;
  51. writel_relaxed(DIV_ROUND_CLOSEST(twd_timer_rate, HZ),
  52. twd_base + TWD_TIMER_LOAD);
  53. writel_relaxed(ctrl, twd_base + TWD_TIMER_CONTROL);
  54. return 0;
  55. }
  56. static int twd_set_next_event(unsigned long evt,
  57. struct clock_event_device *unused)
  58. {
  59. unsigned long ctrl = readl_relaxed(twd_base + TWD_TIMER_CONTROL);
  60. ctrl |= TWD_TIMER_CONTROL_ENABLE;
  61. writel_relaxed(evt, twd_base + TWD_TIMER_COUNTER);
  62. writel_relaxed(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 (readl_relaxed(twd_base + TWD_TIMER_INTSTAT)) {
  74. writel_relaxed(1, twd_base + TWD_TIMER_INTSTAT);
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static void twd_timer_stop(void)
  80. {
  81. struct clock_event_device *clk = raw_cpu_ptr(twd_evt);
  82. twd_shutdown(clk);
  83. disable_percpu_irq(clk->irq);
  84. }
  85. #ifdef CONFIG_COMMON_CLK
  86. /*
  87. * Updates clockevent frequency when the cpu frequency changes.
  88. * Called on the cpu that is changing frequency with interrupts disabled.
  89. */
  90. static void twd_update_frequency(void *new_rate)
  91. {
  92. twd_timer_rate = *((unsigned long *) new_rate);
  93. clockevents_update_freq(raw_cpu_ptr(twd_evt), twd_timer_rate);
  94. }
  95. static int twd_rate_change(struct notifier_block *nb,
  96. unsigned long flags, void *data)
  97. {
  98. struct clk_notifier_data *cnd = data;
  99. /*
  100. * The twd clock events must be reprogrammed to account for the new
  101. * frequency. The timer is local to a cpu, so cross-call to the
  102. * changing cpu.
  103. */
  104. if (flags == POST_RATE_CHANGE)
  105. on_each_cpu(twd_update_frequency,
  106. (void *)&cnd->new_rate, 1);
  107. return NOTIFY_OK;
  108. }
  109. static struct notifier_block twd_clk_nb = {
  110. .notifier_call = twd_rate_change,
  111. };
  112. static int twd_clk_init(void)
  113. {
  114. if (twd_evt && raw_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
  115. return clk_notifier_register(twd_clk, &twd_clk_nb);
  116. return 0;
  117. }
  118. core_initcall(twd_clk_init);
  119. #elif defined (CONFIG_CPU_FREQ)
  120. #include <linux/cpufreq.h>
  121. /*
  122. * Updates clockevent frequency when the cpu frequency changes.
  123. * Called on the cpu that is changing frequency with interrupts disabled.
  124. */
  125. static void twd_update_frequency(void *data)
  126. {
  127. twd_timer_rate = clk_get_rate(twd_clk);
  128. clockevents_update_freq(raw_cpu_ptr(twd_evt), twd_timer_rate);
  129. }
  130. static int twd_cpufreq_transition(struct notifier_block *nb,
  131. unsigned long state, void *data)
  132. {
  133. struct cpufreq_freqs *freqs = data;
  134. /*
  135. * The twd clock events must be reprogrammed to account for the new
  136. * frequency. The timer is local to a cpu, so cross-call to the
  137. * changing cpu.
  138. */
  139. if (state == CPUFREQ_POSTCHANGE)
  140. smp_call_function_single(freqs->cpu, twd_update_frequency,
  141. NULL, 1);
  142. return NOTIFY_OK;
  143. }
  144. static struct notifier_block twd_cpufreq_nb = {
  145. .notifier_call = twd_cpufreq_transition,
  146. };
  147. static int twd_cpufreq_init(void)
  148. {
  149. if (twd_evt && raw_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
  150. return cpufreq_register_notifier(&twd_cpufreq_nb,
  151. CPUFREQ_TRANSITION_NOTIFIER);
  152. return 0;
  153. }
  154. core_initcall(twd_cpufreq_init);
  155. #endif
  156. static void twd_calibrate_rate(void)
  157. {
  158. unsigned long count;
  159. u64 waitjiffies;
  160. /*
  161. * If this is the first time round, we need to work out how fast
  162. * the timer ticks
  163. */
  164. if (twd_timer_rate == 0) {
  165. pr_info("Calibrating local timer... ");
  166. /* Wait for a tick to start */
  167. waitjiffies = get_jiffies_64() + 1;
  168. while (get_jiffies_64() < waitjiffies)
  169. udelay(10);
  170. /* OK, now the tick has started, let's get the timer going */
  171. waitjiffies += 5;
  172. /* enable, no interrupt or reload */
  173. writel_relaxed(0x1, twd_base + TWD_TIMER_CONTROL);
  174. /* maximum value */
  175. writel_relaxed(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
  176. while (get_jiffies_64() < waitjiffies)
  177. udelay(10);
  178. count = readl_relaxed(twd_base + TWD_TIMER_COUNTER);
  179. twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  180. pr_cont("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
  181. (twd_timer_rate / 10000) % 100);
  182. }
  183. }
  184. static irqreturn_t twd_handler(int irq, void *dev_id)
  185. {
  186. struct clock_event_device *evt = dev_id;
  187. if (twd_timer_ack()) {
  188. evt->event_handler(evt);
  189. return IRQ_HANDLED;
  190. }
  191. return IRQ_NONE;
  192. }
  193. static void twd_get_clock(struct device_node *np)
  194. {
  195. int err;
  196. if (np)
  197. twd_clk = of_clk_get(np, 0);
  198. else
  199. twd_clk = clk_get_sys("smp_twd", NULL);
  200. if (IS_ERR(twd_clk)) {
  201. pr_err("smp_twd: clock not found %d\n", (int) PTR_ERR(twd_clk));
  202. return;
  203. }
  204. err = clk_prepare_enable(twd_clk);
  205. if (err) {
  206. pr_err("smp_twd: clock failed to prepare+enable: %d\n", err);
  207. clk_put(twd_clk);
  208. return;
  209. }
  210. twd_timer_rate = clk_get_rate(twd_clk);
  211. }
  212. /*
  213. * Setup the local clock events for a CPU.
  214. */
  215. static void twd_timer_setup(void)
  216. {
  217. struct clock_event_device *clk = raw_cpu_ptr(twd_evt);
  218. int cpu = smp_processor_id();
  219. /*
  220. * If the basic setup for this CPU has been done before don't
  221. * bother with the below.
  222. */
  223. if (per_cpu(percpu_setup_called, cpu)) {
  224. writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
  225. clockevents_register_device(clk);
  226. enable_percpu_irq(clk->irq, 0);
  227. return;
  228. }
  229. per_cpu(percpu_setup_called, cpu) = true;
  230. twd_calibrate_rate();
  231. /*
  232. * The following is done once per CPU the first time .setup() is
  233. * called.
  234. */
  235. writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
  236. clk->name = "local_timer";
  237. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  238. CLOCK_EVT_FEAT_C3STOP;
  239. clk->rating = 350;
  240. clk->set_state_shutdown = twd_shutdown;
  241. clk->set_state_periodic = twd_set_periodic;
  242. clk->set_state_oneshot = twd_set_oneshot;
  243. clk->tick_resume = twd_shutdown;
  244. clk->set_next_event = twd_set_next_event;
  245. clk->irq = twd_ppi;
  246. clk->cpumask = cpumask_of(cpu);
  247. clockevents_config_and_register(clk, twd_timer_rate,
  248. 0xf, 0xffffffff);
  249. enable_percpu_irq(clk->irq, 0);
  250. }
  251. static int twd_timer_cpu_notify(struct notifier_block *self,
  252. unsigned long action, void *hcpu)
  253. {
  254. switch (action & ~CPU_TASKS_FROZEN) {
  255. case CPU_STARTING:
  256. twd_timer_setup();
  257. break;
  258. case CPU_DYING:
  259. twd_timer_stop();
  260. break;
  261. }
  262. return NOTIFY_OK;
  263. }
  264. static struct notifier_block twd_timer_cpu_nb = {
  265. .notifier_call = twd_timer_cpu_notify,
  266. };
  267. static int __init twd_local_timer_common_register(struct device_node *np)
  268. {
  269. int err;
  270. twd_evt = alloc_percpu(struct clock_event_device);
  271. if (!twd_evt) {
  272. err = -ENOMEM;
  273. goto out_free;
  274. }
  275. err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
  276. if (err) {
  277. pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
  278. goto out_free;
  279. }
  280. err = register_cpu_notifier(&twd_timer_cpu_nb);
  281. if (err)
  282. goto out_irq;
  283. twd_get_clock(np);
  284. /*
  285. * Immediately configure the timer on the boot CPU, unless we need
  286. * jiffies to be incrementing to calibrate the rate in which case
  287. * setup the timer in late_time_init.
  288. */
  289. if (twd_timer_rate)
  290. twd_timer_setup();
  291. else
  292. late_time_init = twd_timer_setup;
  293. return 0;
  294. out_irq:
  295. free_percpu_irq(twd_ppi, twd_evt);
  296. out_free:
  297. iounmap(twd_base);
  298. twd_base = NULL;
  299. free_percpu(twd_evt);
  300. return err;
  301. }
  302. int __init twd_local_timer_register(struct twd_local_timer *tlt)
  303. {
  304. if (twd_base || twd_evt)
  305. return -EBUSY;
  306. twd_ppi = tlt->res[1].start;
  307. twd_base = ioremap(tlt->res[0].start, resource_size(&tlt->res[0]));
  308. if (!twd_base)
  309. return -ENOMEM;
  310. return twd_local_timer_common_register(NULL);
  311. }
  312. #ifdef CONFIG_OF
  313. static void __init twd_local_timer_of_register(struct device_node *np)
  314. {
  315. int err;
  316. if (!is_smp() || !setup_max_cpus)
  317. return;
  318. twd_ppi = irq_of_parse_and_map(np, 0);
  319. if (!twd_ppi) {
  320. err = -EINVAL;
  321. goto out;
  322. }
  323. twd_base = of_iomap(np, 0);
  324. if (!twd_base) {
  325. err = -ENOMEM;
  326. goto out;
  327. }
  328. err = twd_local_timer_common_register(np);
  329. out:
  330. WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
  331. }
  332. CLOCKSOURCE_OF_DECLARE(arm_twd_a9, "arm,cortex-a9-twd-timer", twd_local_timer_of_register);
  333. CLOCKSOURCE_OF_DECLARE(arm_twd_a5, "arm,cortex-a5-twd-timer", twd_local_timer_of_register);
  334. CLOCKSOURCE_OF_DECLARE(arm_twd_11mp, "arm,arm11mp-twd-timer", twd_local_timer_of_register);
  335. #endif