arm_global_timer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * drivers/clocksource/arm_global_timer.c
  3. *
  4. * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
  5. * Author: Stuart Menefy <stuart.menefy@st.com>
  6. * Author: Srinivas Kandagatla <srinivas.kandagatla@st.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/clocksource.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/cpu.h>
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_address.h>
  24. #include <linux/sched_clock.h>
  25. #include <asm/cputype.h>
  26. #define GT_COUNTER0 0x00
  27. #define GT_COUNTER1 0x04
  28. #define GT_CONTROL 0x08
  29. #define GT_CONTROL_TIMER_ENABLE BIT(0) /* this bit is NOT banked */
  30. #define GT_CONTROL_COMP_ENABLE BIT(1) /* banked */
  31. #define GT_CONTROL_IRQ_ENABLE BIT(2) /* banked */
  32. #define GT_CONTROL_AUTO_INC BIT(3) /* banked */
  33. #define GT_INT_STATUS 0x0c
  34. #define GT_INT_STATUS_EVENT_FLAG BIT(0)
  35. #define GT_COMP0 0x10
  36. #define GT_COMP1 0x14
  37. #define GT_AUTO_INC 0x18
  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 unsigned long gt_clk_rate;
  46. static int gt_ppi;
  47. static struct clock_event_device __percpu *gt_evt;
  48. /*
  49. * To get the value from the Global Timer Counter register proceed as follows:
  50. * 1. Read the upper 32-bit timer counter register
  51. * 2. Read the lower 32-bit timer counter register
  52. * 3. Read the upper 32-bit timer counter register again. If the value is
  53. * different to the 32-bit upper value read previously, go back to step 2.
  54. * Otherwise the 64-bit timer counter value is correct.
  55. */
  56. static u64 notrace _gt_counter_read(void)
  57. {
  58. u64 counter;
  59. u32 lower;
  60. u32 upper, old_upper;
  61. upper = readl_relaxed(gt_base + GT_COUNTER1);
  62. do {
  63. old_upper = upper;
  64. lower = readl_relaxed(gt_base + GT_COUNTER0);
  65. upper = readl_relaxed(gt_base + GT_COUNTER1);
  66. } while (upper != old_upper);
  67. counter = upper;
  68. counter <<= 32;
  69. counter |= lower;
  70. return counter;
  71. }
  72. static u64 gt_counter_read(void)
  73. {
  74. return _gt_counter_read();
  75. }
  76. /**
  77. * To ensure that updates to comparator value register do not set the
  78. * Interrupt Status Register proceed as follows:
  79. * 1. Clear the Comp Enable bit in the Timer Control Register.
  80. * 2. Write the lower 32-bit Comparator Value Register.
  81. * 3. Write the upper 32-bit Comparator Value Register.
  82. * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.
  83. */
  84. static void gt_compare_set(unsigned long delta, int periodic)
  85. {
  86. u64 counter = gt_counter_read();
  87. unsigned long ctrl;
  88. counter += delta;
  89. ctrl = GT_CONTROL_TIMER_ENABLE;
  90. writel_relaxed(ctrl, gt_base + GT_CONTROL);
  91. writel_relaxed(lower_32_bits(counter), gt_base + GT_COMP0);
  92. writel_relaxed(upper_32_bits(counter), gt_base + GT_COMP1);
  93. if (periodic) {
  94. writel_relaxed(delta, gt_base + GT_AUTO_INC);
  95. ctrl |= GT_CONTROL_AUTO_INC;
  96. }
  97. ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;
  98. writel_relaxed(ctrl, gt_base + GT_CONTROL);
  99. }
  100. static int gt_clockevent_shutdown(struct clock_event_device *evt)
  101. {
  102. unsigned long ctrl;
  103. ctrl = readl(gt_base + GT_CONTROL);
  104. ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
  105. GT_CONTROL_AUTO_INC);
  106. writel(ctrl, gt_base + GT_CONTROL);
  107. return 0;
  108. }
  109. static int gt_clockevent_set_periodic(struct clock_event_device *evt)
  110. {
  111. gt_compare_set(DIV_ROUND_CLOSEST(gt_clk_rate, HZ), 1);
  112. return 0;
  113. }
  114. static int gt_clockevent_set_next_event(unsigned long evt,
  115. struct clock_event_device *unused)
  116. {
  117. gt_compare_set(evt, 0);
  118. return 0;
  119. }
  120. static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
  121. {
  122. struct clock_event_device *evt = dev_id;
  123. if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
  124. GT_INT_STATUS_EVENT_FLAG))
  125. return IRQ_NONE;
  126. /**
  127. * ERRATA 740657( Global Timer can send 2 interrupts for
  128. * the same event in single-shot mode)
  129. * Workaround:
  130. * Either disable single-shot mode.
  131. * Or
  132. * Modify the Interrupt Handler to avoid the
  133. * offending sequence. This is achieved by clearing
  134. * the Global Timer flag _after_ having incremented
  135. * the Comparator register value to a higher value.
  136. */
  137. if (clockevent_state_oneshot(evt))
  138. gt_compare_set(ULONG_MAX, 0);
  139. writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
  140. evt->event_handler(evt);
  141. return IRQ_HANDLED;
  142. }
  143. static int gt_starting_cpu(unsigned int cpu)
  144. {
  145. struct clock_event_device *clk = this_cpu_ptr(gt_evt);
  146. clk->name = "arm_global_timer";
  147. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  148. CLOCK_EVT_FEAT_PERCPU;
  149. clk->set_state_shutdown = gt_clockevent_shutdown;
  150. clk->set_state_periodic = gt_clockevent_set_periodic;
  151. clk->set_state_oneshot = gt_clockevent_shutdown;
  152. clk->set_state_oneshot_stopped = gt_clockevent_shutdown;
  153. clk->set_next_event = gt_clockevent_set_next_event;
  154. clk->cpumask = cpumask_of(cpu);
  155. clk->rating = 300;
  156. clk->irq = gt_ppi;
  157. clockevents_config_and_register(clk, gt_clk_rate,
  158. 1, 0xffffffff);
  159. enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
  160. return 0;
  161. }
  162. static int gt_dying_cpu(unsigned int cpu)
  163. {
  164. struct clock_event_device *clk = this_cpu_ptr(gt_evt);
  165. gt_clockevent_shutdown(clk);
  166. disable_percpu_irq(clk->irq);
  167. return 0;
  168. }
  169. static u64 gt_clocksource_read(struct clocksource *cs)
  170. {
  171. return gt_counter_read();
  172. }
  173. static void gt_resume(struct clocksource *cs)
  174. {
  175. unsigned long ctrl;
  176. ctrl = readl(gt_base + GT_CONTROL);
  177. if (!(ctrl & GT_CONTROL_TIMER_ENABLE))
  178. /* re-enable timer on resume */
  179. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  180. }
  181. static struct clocksource gt_clocksource = {
  182. .name = "arm_global_timer",
  183. .rating = 300,
  184. .read = gt_clocksource_read,
  185. .mask = CLOCKSOURCE_MASK(64),
  186. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  187. .resume = gt_resume,
  188. };
  189. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  190. static u64 notrace gt_sched_clock_read(void)
  191. {
  192. return _gt_counter_read();
  193. }
  194. #endif
  195. static unsigned long gt_read_long(void)
  196. {
  197. return readl_relaxed(gt_base + GT_COUNTER0);
  198. }
  199. static struct delay_timer gt_delay_timer = {
  200. .read_current_timer = gt_read_long,
  201. };
  202. static void __init gt_delay_timer_init(void)
  203. {
  204. gt_delay_timer.freq = gt_clk_rate;
  205. register_current_timer_delay(&gt_delay_timer);
  206. }
  207. static int __init gt_clocksource_init(void)
  208. {
  209. writel(0, gt_base + GT_CONTROL);
  210. writel(0, gt_base + GT_COUNTER0);
  211. writel(0, gt_base + GT_COUNTER1);
  212. /* enables timer on all the cores */
  213. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  214. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  215. sched_clock_register(gt_sched_clock_read, 64, gt_clk_rate);
  216. #endif
  217. return clocksource_register_hz(&gt_clocksource, gt_clk_rate);
  218. }
  219. static int __init global_timer_of_register(struct device_node *np)
  220. {
  221. struct clk *gt_clk;
  222. int err = 0;
  223. /*
  224. * In A9 r2p0 the comparators for each processor with the global timer
  225. * fire when the timer value is greater than or equal to. In previous
  226. * revisions the comparators fired when the timer value was equal to.
  227. */
  228. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
  229. && (read_cpuid_id() & 0xf0000f) < 0x200000) {
  230. pr_warn("global-timer: non support for this cpu version.\n");
  231. return -ENOSYS;
  232. }
  233. gt_ppi = irq_of_parse_and_map(np, 0);
  234. if (!gt_ppi) {
  235. pr_warn("global-timer: unable to parse irq\n");
  236. return -EINVAL;
  237. }
  238. gt_base = of_iomap(np, 0);
  239. if (!gt_base) {
  240. pr_warn("global-timer: invalid base address\n");
  241. return -ENXIO;
  242. }
  243. gt_clk = of_clk_get(np, 0);
  244. if (!IS_ERR(gt_clk)) {
  245. err = clk_prepare_enable(gt_clk);
  246. if (err)
  247. goto out_unmap;
  248. } else {
  249. pr_warn("global-timer: clk not found\n");
  250. err = -EINVAL;
  251. goto out_unmap;
  252. }
  253. gt_clk_rate = clk_get_rate(gt_clk);
  254. gt_evt = alloc_percpu(struct clock_event_device);
  255. if (!gt_evt) {
  256. pr_warn("global-timer: can't allocate memory\n");
  257. err = -ENOMEM;
  258. goto out_clk;
  259. }
  260. err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
  261. "gt", gt_evt);
  262. if (err) {
  263. pr_warn("global-timer: can't register interrupt %d (%d)\n",
  264. gt_ppi, err);
  265. goto out_free;
  266. }
  267. /* Register and immediately configure the timer on the boot CPU */
  268. err = gt_clocksource_init();
  269. if (err)
  270. goto out_irq;
  271. err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,
  272. "clockevents/arm/global_timer:starting",
  273. gt_starting_cpu, gt_dying_cpu);
  274. if (err)
  275. goto out_irq;
  276. gt_delay_timer_init();
  277. return 0;
  278. out_irq:
  279. free_percpu_irq(gt_ppi, gt_evt);
  280. out_free:
  281. free_percpu(gt_evt);
  282. out_clk:
  283. clk_disable_unprepare(gt_clk);
  284. out_unmap:
  285. iounmap(gt_base);
  286. WARN(err, "ARM Global timer register failed (%d)\n", err);
  287. return err;
  288. }
  289. /* Only tested on r2p2 and r3p0 */
  290. TIMER_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
  291. global_timer_of_register);