smp_twd.c 9.2 KB

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