arm_global_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/clocksource/arm_global_timer.c
  4. *
  5. * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
  6. * Author: Stuart Menefy <[email protected]>
  7. * Author: Srinivas Kandagatla <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/cpu.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_address.h>
  21. #include <linux/sched_clock.h>
  22. #include <asm/cputype.h>
  23. #define GT_COUNTER0 0x00
  24. #define GT_COUNTER1 0x04
  25. #define GT_CONTROL 0x08
  26. #define GT_CONTROL_TIMER_ENABLE BIT(0) /* this bit is NOT banked */
  27. #define GT_CONTROL_COMP_ENABLE BIT(1) /* banked */
  28. #define GT_CONTROL_IRQ_ENABLE BIT(2) /* banked */
  29. #define GT_CONTROL_AUTO_INC BIT(3) /* banked */
  30. #define GT_CONTROL_PRESCALER_SHIFT 8
  31. #define GT_CONTROL_PRESCALER_MAX 0xF
  32. #define GT_CONTROL_PRESCALER_MASK (GT_CONTROL_PRESCALER_MAX << \
  33. GT_CONTROL_PRESCALER_SHIFT)
  34. #define GT_INT_STATUS 0x0c
  35. #define GT_INT_STATUS_EVENT_FLAG BIT(0)
  36. #define GT_COMP0 0x10
  37. #define GT_COMP1 0x14
  38. #define GT_AUTO_INC 0x18
  39. #define MAX_F_ERR 50
  40. /*
  41. * We are expecting to be clocked by the ARM peripheral clock.
  42. *
  43. * Note: it is assumed we are using a prescaler value of zero, so this is
  44. * the units for all operations.
  45. */
  46. static void __iomem *gt_base;
  47. static struct notifier_block gt_clk_rate_change_nb;
  48. static u32 gt_psv_new, gt_psv_bck, gt_target_rate;
  49. static int gt_ppi;
  50. static struct clock_event_device __percpu *gt_evt;
  51. /*
  52. * To get the value from the Global Timer Counter register proceed as follows:
  53. * 1. Read the upper 32-bit timer counter register
  54. * 2. Read the lower 32-bit timer counter register
  55. * 3. Read the upper 32-bit timer counter register again. If the value is
  56. * different to the 32-bit upper value read previously, go back to step 2.
  57. * Otherwise the 64-bit timer counter value is correct.
  58. */
  59. static u64 notrace _gt_counter_read(void)
  60. {
  61. u64 counter;
  62. u32 lower;
  63. u32 upper, old_upper;
  64. upper = readl_relaxed(gt_base + GT_COUNTER1);
  65. do {
  66. old_upper = upper;
  67. lower = readl_relaxed(gt_base + GT_COUNTER0);
  68. upper = readl_relaxed(gt_base + GT_COUNTER1);
  69. } while (upper != old_upper);
  70. counter = upper;
  71. counter <<= 32;
  72. counter |= lower;
  73. return counter;
  74. }
  75. static u64 gt_counter_read(void)
  76. {
  77. return _gt_counter_read();
  78. }
  79. /**
  80. * To ensure that updates to comparator value register do not set the
  81. * Interrupt Status Register proceed as follows:
  82. * 1. Clear the Comp Enable bit in the Timer Control Register.
  83. * 2. Write the lower 32-bit Comparator Value Register.
  84. * 3. Write the upper 32-bit Comparator Value Register.
  85. * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.
  86. */
  87. static void gt_compare_set(unsigned long delta, int periodic)
  88. {
  89. u64 counter = gt_counter_read();
  90. unsigned long ctrl;
  91. counter += delta;
  92. ctrl = readl(gt_base + GT_CONTROL);
  93. ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
  94. GT_CONTROL_AUTO_INC);
  95. ctrl |= GT_CONTROL_TIMER_ENABLE;
  96. writel_relaxed(ctrl, gt_base + GT_CONTROL);
  97. writel_relaxed(lower_32_bits(counter), gt_base + GT_COMP0);
  98. writel_relaxed(upper_32_bits(counter), gt_base + GT_COMP1);
  99. if (periodic) {
  100. writel_relaxed(delta, gt_base + GT_AUTO_INC);
  101. ctrl |= GT_CONTROL_AUTO_INC;
  102. }
  103. ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;
  104. writel_relaxed(ctrl, gt_base + GT_CONTROL);
  105. }
  106. static int gt_clockevent_shutdown(struct clock_event_device *evt)
  107. {
  108. unsigned long ctrl;
  109. ctrl = readl(gt_base + GT_CONTROL);
  110. ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
  111. GT_CONTROL_AUTO_INC);
  112. writel(ctrl, gt_base + GT_CONTROL);
  113. return 0;
  114. }
  115. static int gt_clockevent_set_periodic(struct clock_event_device *evt)
  116. {
  117. gt_compare_set(DIV_ROUND_CLOSEST(gt_target_rate, HZ), 1);
  118. return 0;
  119. }
  120. static int gt_clockevent_set_next_event(unsigned long evt,
  121. struct clock_event_device *unused)
  122. {
  123. gt_compare_set(evt, 0);
  124. return 0;
  125. }
  126. static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
  127. {
  128. struct clock_event_device *evt = dev_id;
  129. if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
  130. GT_INT_STATUS_EVENT_FLAG))
  131. return IRQ_NONE;
  132. /**
  133. * ERRATA 740657( Global Timer can send 2 interrupts for
  134. * the same event in single-shot mode)
  135. * Workaround:
  136. * Either disable single-shot mode.
  137. * Or
  138. * Modify the Interrupt Handler to avoid the
  139. * offending sequence. This is achieved by clearing
  140. * the Global Timer flag _after_ having incremented
  141. * the Comparator register value to a higher value.
  142. */
  143. if (clockevent_state_oneshot(evt))
  144. gt_compare_set(ULONG_MAX, 0);
  145. writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
  146. evt->event_handler(evt);
  147. return IRQ_HANDLED;
  148. }
  149. static int gt_starting_cpu(unsigned int cpu)
  150. {
  151. struct clock_event_device *clk = this_cpu_ptr(gt_evt);
  152. clk->name = "arm_global_timer";
  153. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  154. CLOCK_EVT_FEAT_PERCPU;
  155. clk->set_state_shutdown = gt_clockevent_shutdown;
  156. clk->set_state_periodic = gt_clockevent_set_periodic;
  157. clk->set_state_oneshot = gt_clockevent_shutdown;
  158. clk->set_state_oneshot_stopped = gt_clockevent_shutdown;
  159. clk->set_next_event = gt_clockevent_set_next_event;
  160. clk->cpumask = cpumask_of(cpu);
  161. clk->rating = 300;
  162. clk->irq = gt_ppi;
  163. clockevents_config_and_register(clk, gt_target_rate,
  164. 1, 0xffffffff);
  165. enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
  166. return 0;
  167. }
  168. static int gt_dying_cpu(unsigned int cpu)
  169. {
  170. struct clock_event_device *clk = this_cpu_ptr(gt_evt);
  171. gt_clockevent_shutdown(clk);
  172. disable_percpu_irq(clk->irq);
  173. return 0;
  174. }
  175. static u64 gt_clocksource_read(struct clocksource *cs)
  176. {
  177. return gt_counter_read();
  178. }
  179. static void gt_resume(struct clocksource *cs)
  180. {
  181. unsigned long ctrl;
  182. ctrl = readl(gt_base + GT_CONTROL);
  183. if (!(ctrl & GT_CONTROL_TIMER_ENABLE))
  184. /* re-enable timer on resume */
  185. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  186. }
  187. static struct clocksource gt_clocksource = {
  188. .name = "arm_global_timer",
  189. .rating = 300,
  190. .read = gt_clocksource_read,
  191. .mask = CLOCKSOURCE_MASK(64),
  192. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  193. .resume = gt_resume,
  194. };
  195. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  196. static u64 notrace gt_sched_clock_read(void)
  197. {
  198. return _gt_counter_read();
  199. }
  200. #endif
  201. static unsigned long gt_read_long(void)
  202. {
  203. return readl_relaxed(gt_base + GT_COUNTER0);
  204. }
  205. static struct delay_timer gt_delay_timer = {
  206. .read_current_timer = gt_read_long,
  207. };
  208. static void gt_write_presc(u32 psv)
  209. {
  210. u32 reg;
  211. reg = readl(gt_base + GT_CONTROL);
  212. reg &= ~GT_CONTROL_PRESCALER_MASK;
  213. reg |= psv << GT_CONTROL_PRESCALER_SHIFT;
  214. writel(reg, gt_base + GT_CONTROL);
  215. }
  216. static u32 gt_read_presc(void)
  217. {
  218. u32 reg;
  219. reg = readl(gt_base + GT_CONTROL);
  220. reg &= GT_CONTROL_PRESCALER_MASK;
  221. return reg >> GT_CONTROL_PRESCALER_SHIFT;
  222. }
  223. static void __init gt_delay_timer_init(void)
  224. {
  225. gt_delay_timer.freq = gt_target_rate;
  226. register_current_timer_delay(&gt_delay_timer);
  227. }
  228. static int __init gt_clocksource_init(void)
  229. {
  230. writel(0, gt_base + GT_CONTROL);
  231. writel(0, gt_base + GT_COUNTER0);
  232. writel(0, gt_base + GT_COUNTER1);
  233. /* set prescaler and enable timer on all the cores */
  234. writel(((CONFIG_ARM_GT_INITIAL_PRESCALER_VAL - 1) <<
  235. GT_CONTROL_PRESCALER_SHIFT)
  236. | GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  237. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  238. sched_clock_register(gt_sched_clock_read, 64, gt_target_rate);
  239. #endif
  240. return clocksource_register_hz(&gt_clocksource, gt_target_rate);
  241. }
  242. static int gt_clk_rate_change_cb(struct notifier_block *nb,
  243. unsigned long event, void *data)
  244. {
  245. struct clk_notifier_data *ndata = data;
  246. switch (event) {
  247. case PRE_RATE_CHANGE:
  248. {
  249. int psv;
  250. psv = DIV_ROUND_CLOSEST(ndata->new_rate,
  251. gt_target_rate);
  252. if (abs(gt_target_rate - (ndata->new_rate / psv)) > MAX_F_ERR)
  253. return NOTIFY_BAD;
  254. psv--;
  255. /* prescaler within legal range? */
  256. if (psv < 0 || psv > GT_CONTROL_PRESCALER_MAX)
  257. return NOTIFY_BAD;
  258. /*
  259. * store timer clock ctrl register so we can restore it in case
  260. * of an abort.
  261. */
  262. gt_psv_bck = gt_read_presc();
  263. gt_psv_new = psv;
  264. /* scale down: adjust divider in post-change notification */
  265. if (ndata->new_rate < ndata->old_rate)
  266. return NOTIFY_DONE;
  267. /* scale up: adjust divider now - before frequency change */
  268. gt_write_presc(psv);
  269. break;
  270. }
  271. case POST_RATE_CHANGE:
  272. /* scale up: pre-change notification did the adjustment */
  273. if (ndata->new_rate > ndata->old_rate)
  274. return NOTIFY_OK;
  275. /* scale down: adjust divider now - after frequency change */
  276. gt_write_presc(gt_psv_new);
  277. break;
  278. case ABORT_RATE_CHANGE:
  279. /* we have to undo the adjustment in case we scale up */
  280. if (ndata->new_rate < ndata->old_rate)
  281. return NOTIFY_OK;
  282. /* restore original register value */
  283. gt_write_presc(gt_psv_bck);
  284. break;
  285. default:
  286. return NOTIFY_DONE;
  287. }
  288. return NOTIFY_DONE;
  289. }
  290. static int __init global_timer_of_register(struct device_node *np)
  291. {
  292. struct clk *gt_clk;
  293. static unsigned long gt_clk_rate;
  294. int err = 0;
  295. /*
  296. * In A9 r2p0 the comparators for each processor with the global timer
  297. * fire when the timer value is greater than or equal to. In previous
  298. * revisions the comparators fired when the timer value was equal to.
  299. */
  300. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
  301. && (read_cpuid_id() & 0xf0000f) < 0x200000) {
  302. pr_warn("global-timer: non support for this cpu version.\n");
  303. return -ENOSYS;
  304. }
  305. gt_ppi = irq_of_parse_and_map(np, 0);
  306. if (!gt_ppi) {
  307. pr_warn("global-timer: unable to parse irq\n");
  308. return -EINVAL;
  309. }
  310. gt_base = of_iomap(np, 0);
  311. if (!gt_base) {
  312. pr_warn("global-timer: invalid base address\n");
  313. return -ENXIO;
  314. }
  315. gt_clk = of_clk_get(np, 0);
  316. if (!IS_ERR(gt_clk)) {
  317. err = clk_prepare_enable(gt_clk);
  318. if (err)
  319. goto out_unmap;
  320. } else {
  321. pr_warn("global-timer: clk not found\n");
  322. err = -EINVAL;
  323. goto out_unmap;
  324. }
  325. gt_clk_rate = clk_get_rate(gt_clk);
  326. gt_target_rate = gt_clk_rate / CONFIG_ARM_GT_INITIAL_PRESCALER_VAL;
  327. gt_clk_rate_change_nb.notifier_call =
  328. gt_clk_rate_change_cb;
  329. err = clk_notifier_register(gt_clk, &gt_clk_rate_change_nb);
  330. if (err) {
  331. pr_warn("Unable to register clock notifier\n");
  332. goto out_clk;
  333. }
  334. gt_evt = alloc_percpu(struct clock_event_device);
  335. if (!gt_evt) {
  336. pr_warn("global-timer: can't allocate memory\n");
  337. err = -ENOMEM;
  338. goto out_clk_nb;
  339. }
  340. err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
  341. "gt", gt_evt);
  342. if (err) {
  343. pr_warn("global-timer: can't register interrupt %d (%d)\n",
  344. gt_ppi, err);
  345. goto out_free;
  346. }
  347. /* Register and immediately configure the timer on the boot CPU */
  348. err = gt_clocksource_init();
  349. if (err)
  350. goto out_irq;
  351. err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,
  352. "clockevents/arm/global_timer:starting",
  353. gt_starting_cpu, gt_dying_cpu);
  354. if (err)
  355. goto out_irq;
  356. gt_delay_timer_init();
  357. return 0;
  358. out_irq:
  359. free_percpu_irq(gt_ppi, gt_evt);
  360. out_free:
  361. free_percpu(gt_evt);
  362. out_clk_nb:
  363. clk_notifier_unregister(gt_clk, &gt_clk_rate_change_nb);
  364. out_clk:
  365. clk_disable_unprepare(gt_clk);
  366. out_unmap:
  367. iounmap(gt_base);
  368. WARN(err, "ARM Global timer register failed (%d)\n", err);
  369. return err;
  370. }
  371. /* Only tested on r2p2 and r3p0 */
  372. TIMER_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
  373. global_timer_of_register);