smp_twd.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/smp_twd.c
  4. *
  5. * Copyright (C) 2002 ARM Ltd.
  6. * All Rights Reserved
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/clk.h>
  11. #include <linux/cpu.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/smp.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_address.h>
  22. #include <asm/smp_twd.h>
  23. /* set up by the platform code */
  24. static void __iomem *twd_base;
  25. static struct clk *twd_clk;
  26. static unsigned long twd_timer_rate;
  27. static DEFINE_PER_CPU(bool, percpu_setup_called);
  28. static struct clock_event_device __percpu *twd_evt;
  29. static unsigned int twd_features =
  30. CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  31. static int twd_ppi;
  32. static int twd_shutdown(struct clock_event_device *clk)
  33. {
  34. writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
  35. return 0;
  36. }
  37. static int twd_set_oneshot(struct clock_event_device *clk)
  38. {
  39. /* period set, and timer enabled in 'next_event' hook */
  40. writel_relaxed(TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT,
  41. twd_base + TWD_TIMER_CONTROL);
  42. return 0;
  43. }
  44. static int twd_set_periodic(struct clock_event_device *clk)
  45. {
  46. unsigned long ctrl = TWD_TIMER_CONTROL_ENABLE |
  47. TWD_TIMER_CONTROL_IT_ENABLE |
  48. TWD_TIMER_CONTROL_PERIODIC;
  49. writel_relaxed(DIV_ROUND_CLOSEST(twd_timer_rate, HZ),
  50. twd_base + TWD_TIMER_LOAD);
  51. writel_relaxed(ctrl, twd_base + TWD_TIMER_CONTROL);
  52. return 0;
  53. }
  54. static int twd_set_next_event(unsigned long evt,
  55. struct clock_event_device *unused)
  56. {
  57. unsigned long ctrl = readl_relaxed(twd_base + TWD_TIMER_CONTROL);
  58. ctrl |= TWD_TIMER_CONTROL_ENABLE;
  59. writel_relaxed(evt, twd_base + TWD_TIMER_COUNTER);
  60. writel_relaxed(ctrl, twd_base + TWD_TIMER_CONTROL);
  61. return 0;
  62. }
  63. /*
  64. * local_timer_ack: checks for a local timer interrupt.
  65. *
  66. * If a local timer interrupt has occurred, acknowledge and return 1.
  67. * Otherwise, return 0.
  68. */
  69. static int twd_timer_ack(void)
  70. {
  71. if (readl_relaxed(twd_base + TWD_TIMER_INTSTAT)) {
  72. writel_relaxed(1, twd_base + TWD_TIMER_INTSTAT);
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. static void twd_timer_stop(void)
  78. {
  79. struct clock_event_device *clk = raw_cpu_ptr(twd_evt);
  80. twd_shutdown(clk);
  81. disable_percpu_irq(clk->irq);
  82. }
  83. /*
  84. * Updates clockevent frequency when the cpu frequency changes.
  85. * Called on the cpu that is changing frequency with interrupts disabled.
  86. */
  87. static void twd_update_frequency(void *new_rate)
  88. {
  89. twd_timer_rate = *((unsigned long *) new_rate);
  90. clockevents_update_freq(raw_cpu_ptr(twd_evt), twd_timer_rate);
  91. }
  92. static int twd_rate_change(struct notifier_block *nb,
  93. unsigned long flags, void *data)
  94. {
  95. struct clk_notifier_data *cnd = data;
  96. /*
  97. * The twd clock events must be reprogrammed to account for the new
  98. * frequency. The timer is local to a cpu, so cross-call to the
  99. * changing cpu.
  100. */
  101. if (flags == POST_RATE_CHANGE)
  102. on_each_cpu(twd_update_frequency,
  103. (void *)&cnd->new_rate, 1);
  104. return NOTIFY_OK;
  105. }
  106. static struct notifier_block twd_clk_nb = {
  107. .notifier_call = twd_rate_change,
  108. };
  109. static int twd_clk_init(void)
  110. {
  111. if (twd_evt && raw_cpu_ptr(twd_evt) && !IS_ERR(twd_clk))
  112. return clk_notifier_register(twd_clk, &twd_clk_nb);
  113. return 0;
  114. }
  115. core_initcall(twd_clk_init);
  116. static void twd_calibrate_rate(void)
  117. {
  118. unsigned long count;
  119. u64 waitjiffies;
  120. /*
  121. * If this is the first time round, we need to work out how fast
  122. * the timer ticks
  123. */
  124. if (twd_timer_rate == 0) {
  125. pr_info("Calibrating local timer... ");
  126. /* Wait for a tick to start */
  127. waitjiffies = get_jiffies_64() + 1;
  128. while (get_jiffies_64() < waitjiffies)
  129. udelay(10);
  130. /* OK, now the tick has started, let's get the timer going */
  131. waitjiffies += 5;
  132. /* enable, no interrupt or reload */
  133. writel_relaxed(0x1, twd_base + TWD_TIMER_CONTROL);
  134. /* maximum value */
  135. writel_relaxed(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
  136. while (get_jiffies_64() < waitjiffies)
  137. udelay(10);
  138. count = readl_relaxed(twd_base + TWD_TIMER_COUNTER);
  139. twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
  140. pr_cont("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
  141. (twd_timer_rate / 10000) % 100);
  142. }
  143. }
  144. static irqreturn_t twd_handler(int irq, void *dev_id)
  145. {
  146. struct clock_event_device *evt = dev_id;
  147. if (twd_timer_ack()) {
  148. evt->event_handler(evt);
  149. return IRQ_HANDLED;
  150. }
  151. return IRQ_NONE;
  152. }
  153. static void twd_get_clock(struct device_node *np)
  154. {
  155. int err;
  156. if (np)
  157. twd_clk = of_clk_get(np, 0);
  158. else
  159. twd_clk = clk_get_sys("smp_twd", NULL);
  160. if (IS_ERR(twd_clk)) {
  161. pr_err("smp_twd: clock not found %d\n", (int) PTR_ERR(twd_clk));
  162. return;
  163. }
  164. err = clk_prepare_enable(twd_clk);
  165. if (err) {
  166. pr_err("smp_twd: clock failed to prepare+enable: %d\n", err);
  167. clk_put(twd_clk);
  168. return;
  169. }
  170. twd_timer_rate = clk_get_rate(twd_clk);
  171. }
  172. /*
  173. * Setup the local clock events for a CPU.
  174. */
  175. static void twd_timer_setup(void)
  176. {
  177. struct clock_event_device *clk = raw_cpu_ptr(twd_evt);
  178. int cpu = smp_processor_id();
  179. /*
  180. * If the basic setup for this CPU has been done before don't
  181. * bother with the below.
  182. */
  183. if (per_cpu(percpu_setup_called, cpu)) {
  184. writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
  185. clockevents_register_device(clk);
  186. enable_percpu_irq(clk->irq, 0);
  187. return;
  188. }
  189. per_cpu(percpu_setup_called, cpu) = true;
  190. twd_calibrate_rate();
  191. /*
  192. * The following is done once per CPU the first time .setup() is
  193. * called.
  194. */
  195. writel_relaxed(0, twd_base + TWD_TIMER_CONTROL);
  196. clk->name = "local_timer";
  197. clk->features = twd_features;
  198. clk->rating = 350;
  199. clk->set_state_shutdown = twd_shutdown;
  200. clk->set_state_periodic = twd_set_periodic;
  201. clk->set_state_oneshot = twd_set_oneshot;
  202. clk->tick_resume = twd_shutdown;
  203. clk->set_next_event = twd_set_next_event;
  204. clk->irq = twd_ppi;
  205. clk->cpumask = cpumask_of(cpu);
  206. clockevents_config_and_register(clk, twd_timer_rate,
  207. 0xf, 0xffffffff);
  208. enable_percpu_irq(clk->irq, 0);
  209. }
  210. static int twd_timer_starting_cpu(unsigned int cpu)
  211. {
  212. twd_timer_setup();
  213. return 0;
  214. }
  215. static int twd_timer_dying_cpu(unsigned int cpu)
  216. {
  217. twd_timer_stop();
  218. return 0;
  219. }
  220. static int __init twd_local_timer_common_register(struct device_node *np)
  221. {
  222. int err;
  223. twd_evt = alloc_percpu(struct clock_event_device);
  224. if (!twd_evt) {
  225. err = -ENOMEM;
  226. goto out_free;
  227. }
  228. err = request_percpu_irq(twd_ppi, twd_handler, "twd", twd_evt);
  229. if (err) {
  230. pr_err("twd: can't register interrupt %d (%d)\n", twd_ppi, err);
  231. goto out_free;
  232. }
  233. cpuhp_setup_state_nocalls(CPUHP_AP_ARM_TWD_STARTING,
  234. "arm/timer/twd:starting",
  235. twd_timer_starting_cpu, twd_timer_dying_cpu);
  236. twd_get_clock(np);
  237. if (!of_property_read_bool(np, "always-on"))
  238. twd_features |= CLOCK_EVT_FEAT_C3STOP;
  239. /*
  240. * Immediately configure the timer on the boot CPU, unless we need
  241. * jiffies to be incrementing to calibrate the rate in which case
  242. * setup the timer in late_time_init.
  243. */
  244. if (twd_timer_rate)
  245. twd_timer_setup();
  246. else
  247. late_time_init = twd_timer_setup;
  248. return 0;
  249. out_free:
  250. iounmap(twd_base);
  251. twd_base = NULL;
  252. free_percpu(twd_evt);
  253. return err;
  254. }
  255. static int __init twd_local_timer_of_register(struct device_node *np)
  256. {
  257. int err;
  258. twd_ppi = irq_of_parse_and_map(np, 0);
  259. if (!twd_ppi) {
  260. err = -EINVAL;
  261. goto out;
  262. }
  263. twd_base = of_iomap(np, 0);
  264. if (!twd_base) {
  265. err = -ENOMEM;
  266. goto out;
  267. }
  268. err = twd_local_timer_common_register(np);
  269. out:
  270. WARN(err, "twd_local_timer_of_register failed (%d)\n", err);
  271. return err;
  272. }
  273. TIMER_OF_DECLARE(arm_twd_a9, "arm,cortex-a9-twd-timer", twd_local_timer_of_register);
  274. TIMER_OF_DECLARE(arm_twd_a5, "arm,cortex-a5-twd-timer", twd_local_timer_of_register);
  275. TIMER_OF_DECLARE(arm_twd_11mp, "arm,arm11mp-twd-timer", twd_local_timer_of_register);