arm_global_timer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 void gt_clockevent_set_mode(enum clock_event_mode mode,
  96. struct clock_event_device *clk)
  97. {
  98. unsigned long ctrl;
  99. switch (mode) {
  100. case CLOCK_EVT_MODE_PERIODIC:
  101. gt_compare_set(DIV_ROUND_CLOSEST(gt_clk_rate, HZ), 1);
  102. break;
  103. case CLOCK_EVT_MODE_ONESHOT:
  104. case CLOCK_EVT_MODE_UNUSED:
  105. case CLOCK_EVT_MODE_SHUTDOWN:
  106. ctrl = readl(gt_base + GT_CONTROL);
  107. ctrl &= ~(GT_CONTROL_COMP_ENABLE |
  108. GT_CONTROL_IRQ_ENABLE | GT_CONTROL_AUTO_INC);
  109. writel(ctrl, gt_base + GT_CONTROL);
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. static int gt_clockevent_set_next_event(unsigned long evt,
  116. struct clock_event_device *unused)
  117. {
  118. gt_compare_set(evt, 0);
  119. return 0;
  120. }
  121. static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
  122. {
  123. struct clock_event_device *evt = dev_id;
  124. if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
  125. GT_INT_STATUS_EVENT_FLAG))
  126. return IRQ_NONE;
  127. /**
  128. * ERRATA 740657( Global Timer can send 2 interrupts for
  129. * the same event in single-shot mode)
  130. * Workaround:
  131. * Either disable single-shot mode.
  132. * Or
  133. * Modify the Interrupt Handler to avoid the
  134. * offending sequence. This is achieved by clearing
  135. * the Global Timer flag _after_ having incremented
  136. * the Comparator register value to a higher value.
  137. */
  138. if (evt->mode == CLOCK_EVT_MODE_ONESHOT)
  139. gt_compare_set(ULONG_MAX, 0);
  140. writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
  141. evt->event_handler(evt);
  142. return IRQ_HANDLED;
  143. }
  144. static int gt_clockevents_init(struct clock_event_device *clk)
  145. {
  146. int cpu = smp_processor_id();
  147. clk->name = "arm_global_timer";
  148. clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  149. clk->set_mode = gt_clockevent_set_mode;
  150. clk->set_next_event = gt_clockevent_set_next_event;
  151. clk->cpumask = cpumask_of(cpu);
  152. clk->rating = 300;
  153. clk->irq = gt_ppi;
  154. clockevents_config_and_register(clk, gt_clk_rate,
  155. 1, 0xffffffff);
  156. enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
  157. return 0;
  158. }
  159. static void gt_clockevents_stop(struct clock_event_device *clk)
  160. {
  161. gt_clockevent_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  162. disable_percpu_irq(clk->irq);
  163. }
  164. static cycle_t gt_clocksource_read(struct clocksource *cs)
  165. {
  166. return gt_counter_read();
  167. }
  168. static struct clocksource gt_clocksource = {
  169. .name = "arm_global_timer",
  170. .rating = 300,
  171. .read = gt_clocksource_read,
  172. .mask = CLOCKSOURCE_MASK(64),
  173. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  174. };
  175. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  176. static u32 notrace gt_sched_clock_read(void)
  177. {
  178. return gt_counter_read();
  179. }
  180. #endif
  181. static void __init gt_clocksource_init(void)
  182. {
  183. writel(0, gt_base + GT_CONTROL);
  184. writel(0, gt_base + GT_COUNTER0);
  185. writel(0, gt_base + GT_COUNTER1);
  186. /* enables timer on all the cores */
  187. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  188. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  189. setup_sched_clock(gt_sched_clock_read, 32, gt_clk_rate);
  190. #endif
  191. clocksource_register_hz(&gt_clocksource, gt_clk_rate);
  192. }
  193. static int gt_cpu_notify(struct notifier_block *self, unsigned long action,
  194. void *hcpu)
  195. {
  196. switch (action & ~CPU_TASKS_FROZEN) {
  197. case CPU_STARTING:
  198. gt_clockevents_init(this_cpu_ptr(gt_evt));
  199. break;
  200. case CPU_DYING:
  201. gt_clockevents_stop(this_cpu_ptr(gt_evt));
  202. break;
  203. }
  204. return NOTIFY_OK;
  205. }
  206. static struct notifier_block gt_cpu_nb = {
  207. .notifier_call = gt_cpu_notify,
  208. };
  209. static void __init global_timer_of_register(struct device_node *np)
  210. {
  211. struct clk *gt_clk;
  212. int err = 0;
  213. /*
  214. * In r2p0 the comparators for each processor with the global timer
  215. * fire when the timer value is greater than or equal to. In previous
  216. * revisions the comparators fired when the timer value was equal to.
  217. */
  218. if ((read_cpuid_id() & 0xf0000f) < 0x200000) {
  219. pr_warn("global-timer: non support for this cpu version.\n");
  220. return;
  221. }
  222. gt_ppi = irq_of_parse_and_map(np, 0);
  223. if (!gt_ppi) {
  224. pr_warn("global-timer: unable to parse irq\n");
  225. return;
  226. }
  227. gt_base = of_iomap(np, 0);
  228. if (!gt_base) {
  229. pr_warn("global-timer: invalid base address\n");
  230. return;
  231. }
  232. gt_clk = of_clk_get(np, 0);
  233. if (!IS_ERR(gt_clk)) {
  234. err = clk_prepare_enable(gt_clk);
  235. if (err)
  236. goto out_unmap;
  237. } else {
  238. pr_warn("global-timer: clk not found\n");
  239. err = -EINVAL;
  240. goto out_unmap;
  241. }
  242. gt_clk_rate = clk_get_rate(gt_clk);
  243. gt_evt = alloc_percpu(struct clock_event_device);
  244. if (!gt_evt) {
  245. pr_warn("global-timer: can't allocate memory\n");
  246. err = -ENOMEM;
  247. goto out_clk;
  248. }
  249. err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
  250. "gt", gt_evt);
  251. if (err) {
  252. pr_warn("global-timer: can't register interrupt %d (%d)\n",
  253. gt_ppi, err);
  254. goto out_free;
  255. }
  256. err = register_cpu_notifier(&gt_cpu_nb);
  257. if (err) {
  258. pr_warn("global-timer: unable to register cpu notifier.\n");
  259. goto out_irq;
  260. }
  261. /* Immediately configure the timer on the boot CPU */
  262. gt_clocksource_init();
  263. gt_clockevents_init(this_cpu_ptr(gt_evt));
  264. return;
  265. out_irq:
  266. free_percpu_irq(gt_ppi, gt_evt);
  267. out_free:
  268. free_percpu(gt_evt);
  269. out_clk:
  270. clk_disable_unprepare(gt_clk);
  271. out_unmap:
  272. iounmap(gt_base);
  273. WARN(err, "ARM Global timer register failed (%d)\n", err);
  274. }
  275. /* Only tested on r2p2 and r3p0 */
  276. CLOCKSOURCE_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
  277. global_timer_of_register);