arm_global_timer.c 12 KB

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