arm_global_timer.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/err.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_address.h>
  23. #include <linux/sched_clock.h>
  24. #include <asm/cputype.h>
  25. #define GT_COUNTER0 0x00
  26. #define GT_COUNTER1 0x04
  27. #define GT_CONTROL 0x08
  28. #define GT_CONTROL_TIMER_ENABLE BIT(0) /* this bit is NOT banked */
  29. #define GT_CONTROL_COMP_ENABLE BIT(1) /* banked */
  30. #define GT_CONTROL_IRQ_ENABLE BIT(2) /* banked */
  31. #define GT_CONTROL_AUTO_INC BIT(3) /* banked */
  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. /*
  38. * We are expecting to be clocked by the ARM peripheral clock.
  39. *
  40. * Note: it is assumed we are using a prescaler value of zero, so this is
  41. * the units for all operations.
  42. */
  43. static void __iomem *gt_base;
  44. static unsigned long gt_clk_rate;
  45. static int gt_ppi;
  46. static struct clock_event_device __percpu *gt_evt;
  47. /*
  48. * To get the value from the Global Timer Counter register proceed as follows:
  49. * 1. Read the upper 32-bit timer counter register
  50. * 2. Read the lower 32-bit timer counter register
  51. * 3. Read the upper 32-bit timer counter register again. If the value is
  52. * different to the 32-bit upper value read previously, go back to step 2.
  53. * Otherwise the 64-bit timer counter value is correct.
  54. */
  55. static u64 gt_counter_read(void)
  56. {
  57. u64 counter;
  58. u32 lower;
  59. u32 upper, old_upper;
  60. upper = readl_relaxed(gt_base + GT_COUNTER1);
  61. do {
  62. old_upper = upper;
  63. lower = readl_relaxed(gt_base + GT_COUNTER0);
  64. upper = readl_relaxed(gt_base + GT_COUNTER1);
  65. } while (upper != old_upper);
  66. counter = upper;
  67. counter <<= 32;
  68. counter |= lower;
  69. return counter;
  70. }
  71. /**
  72. * To ensure that updates to comparator value register do not set the
  73. * Interrupt Status Register proceed as follows:
  74. * 1. Clear the Comp Enable bit in the Timer Control Register.
  75. * 2. Write the lower 32-bit Comparator Value Register.
  76. * 3. Write the upper 32-bit Comparator Value Register.
  77. * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.
  78. */
  79. static void gt_compare_set(unsigned long delta, int periodic)
  80. {
  81. u64 counter = gt_counter_read();
  82. unsigned long ctrl;
  83. counter += delta;
  84. ctrl = GT_CONTROL_TIMER_ENABLE;
  85. writel(ctrl, gt_base + GT_CONTROL);
  86. writel(lower_32_bits(counter), gt_base + GT_COMP0);
  87. writel(upper_32_bits(counter), gt_base + GT_COMP1);
  88. if (periodic) {
  89. writel(delta, gt_base + GT_AUTO_INC);
  90. ctrl |= GT_CONTROL_AUTO_INC;
  91. }
  92. ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;
  93. writel(ctrl, gt_base + GT_CONTROL);
  94. }
  95. static int gt_clockevent_shutdown(struct clock_event_device *evt)
  96. {
  97. unsigned long ctrl;
  98. ctrl = readl(gt_base + GT_CONTROL);
  99. ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
  100. GT_CONTROL_AUTO_INC);
  101. writel(ctrl, gt_base + GT_CONTROL);
  102. return 0;
  103. }
  104. static int gt_clockevent_set_periodic(struct clock_event_device *evt)
  105. {
  106. gt_compare_set(DIV_ROUND_CLOSEST(gt_clk_rate, HZ), 1);
  107. return 0;
  108. }
  109. static int gt_clockevent_set_next_event(unsigned long evt,
  110. struct clock_event_device *unused)
  111. {
  112. gt_compare_set(evt, 0);
  113. return 0;
  114. }
  115. static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
  116. {
  117. struct clock_event_device *evt = dev_id;
  118. if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
  119. GT_INT_STATUS_EVENT_FLAG))
  120. return IRQ_NONE;
  121. /**
  122. * ERRATA 740657( Global Timer can send 2 interrupts for
  123. * the same event in single-shot mode)
  124. * Workaround:
  125. * Either disable single-shot mode.
  126. * Or
  127. * Modify the Interrupt Handler to avoid the
  128. * offending sequence. This is achieved by clearing
  129. * the Global Timer flag _after_ having incremented
  130. * the Comparator register value to a higher value.
  131. */
  132. if (clockevent_state_oneshot(evt))
  133. gt_compare_set(ULONG_MAX, 0);
  134. writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
  135. evt->event_handler(evt);
  136. return IRQ_HANDLED;
  137. }
  138. static int gt_clockevents_init(struct clock_event_device *clk)
  139. {
  140. int cpu = smp_processor_id();
  141. clk->name = "arm_global_timer";
  142. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  143. CLOCK_EVT_FEAT_PERCPU;
  144. clk->set_state_shutdown = gt_clockevent_shutdown;
  145. clk->set_state_periodic = gt_clockevent_set_periodic;
  146. clk->set_state_oneshot = gt_clockevent_shutdown;
  147. clk->set_next_event = gt_clockevent_set_next_event;
  148. clk->cpumask = cpumask_of(cpu);
  149. clk->rating = 300;
  150. clk->irq = gt_ppi;
  151. clockevents_config_and_register(clk, gt_clk_rate,
  152. 1, 0xffffffff);
  153. enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
  154. return 0;
  155. }
  156. static void gt_clockevents_stop(struct clock_event_device *clk)
  157. {
  158. gt_clockevent_shutdown(clk);
  159. disable_percpu_irq(clk->irq);
  160. }
  161. static cycle_t gt_clocksource_read(struct clocksource *cs)
  162. {
  163. return gt_counter_read();
  164. }
  165. static struct clocksource gt_clocksource = {
  166. .name = "arm_global_timer",
  167. .rating = 300,
  168. .read = gt_clocksource_read,
  169. .mask = CLOCKSOURCE_MASK(64),
  170. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  171. };
  172. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  173. static u64 notrace gt_sched_clock_read(void)
  174. {
  175. return gt_counter_read();
  176. }
  177. #endif
  178. static void __init gt_clocksource_init(void)
  179. {
  180. writel(0, gt_base + GT_CONTROL);
  181. writel(0, gt_base + GT_COUNTER0);
  182. writel(0, gt_base + GT_COUNTER1);
  183. /* enables timer on all the cores */
  184. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  185. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  186. sched_clock_register(gt_sched_clock_read, 64, gt_clk_rate);
  187. #endif
  188. clocksource_register_hz(&gt_clocksource, gt_clk_rate);
  189. }
  190. static int gt_cpu_notify(struct notifier_block *self, unsigned long action,
  191. void *hcpu)
  192. {
  193. switch (action & ~CPU_TASKS_FROZEN) {
  194. case CPU_STARTING:
  195. gt_clockevents_init(this_cpu_ptr(gt_evt));
  196. break;
  197. case CPU_DYING:
  198. gt_clockevents_stop(this_cpu_ptr(gt_evt));
  199. break;
  200. }
  201. return NOTIFY_OK;
  202. }
  203. static struct notifier_block gt_cpu_nb = {
  204. .notifier_call = gt_cpu_notify,
  205. };
  206. static void __init global_timer_of_register(struct device_node *np)
  207. {
  208. struct clk *gt_clk;
  209. int err = 0;
  210. /*
  211. * In A9 r2p0 the comparators for each processor with the global timer
  212. * fire when the timer value is greater than or equal to. In previous
  213. * revisions the comparators fired when the timer value was equal to.
  214. */
  215. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
  216. && (read_cpuid_id() & 0xf0000f) < 0x200000) {
  217. pr_warn("global-timer: non support for this cpu version.\n");
  218. return;
  219. }
  220. gt_ppi = irq_of_parse_and_map(np, 0);
  221. if (!gt_ppi) {
  222. pr_warn("global-timer: unable to parse irq\n");
  223. return;
  224. }
  225. gt_base = of_iomap(np, 0);
  226. if (!gt_base) {
  227. pr_warn("global-timer: invalid base address\n");
  228. return;
  229. }
  230. gt_clk = of_clk_get(np, 0);
  231. if (!IS_ERR(gt_clk)) {
  232. err = clk_prepare_enable(gt_clk);
  233. if (err)
  234. goto out_unmap;
  235. } else {
  236. pr_warn("global-timer: clk not found\n");
  237. err = -EINVAL;
  238. goto out_unmap;
  239. }
  240. gt_clk_rate = clk_get_rate(gt_clk);
  241. gt_evt = alloc_percpu(struct clock_event_device);
  242. if (!gt_evt) {
  243. pr_warn("global-timer: can't allocate memory\n");
  244. err = -ENOMEM;
  245. goto out_clk;
  246. }
  247. err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
  248. "gt", gt_evt);
  249. if (err) {
  250. pr_warn("global-timer: can't register interrupt %d (%d)\n",
  251. gt_ppi, err);
  252. goto out_free;
  253. }
  254. err = register_cpu_notifier(&gt_cpu_nb);
  255. if (err) {
  256. pr_warn("global-timer: unable to register cpu notifier.\n");
  257. goto out_irq;
  258. }
  259. /* Immediately configure the timer on the boot CPU */
  260. gt_clocksource_init();
  261. gt_clockevents_init(this_cpu_ptr(gt_evt));
  262. return;
  263. out_irq:
  264. free_percpu_irq(gt_ppi, gt_evt);
  265. out_free:
  266. free_percpu(gt_evt);
  267. out_clk:
  268. clk_disable_unprepare(gt_clk);
  269. out_unmap:
  270. iounmap(gt_base);
  271. WARN(err, "ARM Global timer register failed (%d)\n", err);
  272. }
  273. /* Only tested on r2p2 and r3p0 */
  274. CLOCKSOURCE_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
  275. global_timer_of_register);