arm_global_timer.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_clockevents_init(struct clock_event_device *clk)
  144. {
  145. int cpu = smp_processor_id();
  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 void gt_clockevents_stop(struct clock_event_device *clk)
  163. {
  164. gt_clockevent_shutdown(clk);
  165. disable_percpu_irq(clk->irq);
  166. }
  167. static cycle_t gt_clocksource_read(struct clocksource *cs)
  168. {
  169. return gt_counter_read();
  170. }
  171. static void gt_resume(struct clocksource *cs)
  172. {
  173. unsigned long ctrl;
  174. ctrl = readl(gt_base + GT_CONTROL);
  175. if (!(ctrl & GT_CONTROL_TIMER_ENABLE))
  176. /* re-enable timer on resume */
  177. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  178. }
  179. static struct clocksource gt_clocksource = {
  180. .name = "arm_global_timer",
  181. .rating = 300,
  182. .read = gt_clocksource_read,
  183. .mask = CLOCKSOURCE_MASK(64),
  184. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  185. .resume = gt_resume,
  186. };
  187. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  188. static u64 notrace gt_sched_clock_read(void)
  189. {
  190. return _gt_counter_read();
  191. }
  192. #endif
  193. static unsigned long gt_read_long(void)
  194. {
  195. return readl_relaxed(gt_base + GT_COUNTER0);
  196. }
  197. static struct delay_timer gt_delay_timer = {
  198. .read_current_timer = gt_read_long,
  199. };
  200. static void __init gt_delay_timer_init(void)
  201. {
  202. gt_delay_timer.freq = gt_clk_rate;
  203. register_current_timer_delay(&gt_delay_timer);
  204. }
  205. static int __init gt_clocksource_init(void)
  206. {
  207. writel(0, gt_base + GT_CONTROL);
  208. writel(0, gt_base + GT_COUNTER0);
  209. writel(0, gt_base + GT_COUNTER1);
  210. /* enables timer on all the cores */
  211. writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
  212. #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
  213. sched_clock_register(gt_sched_clock_read, 64, gt_clk_rate);
  214. #endif
  215. return clocksource_register_hz(&gt_clocksource, gt_clk_rate);
  216. }
  217. static int gt_cpu_notify(struct notifier_block *self, unsigned long action,
  218. void *hcpu)
  219. {
  220. switch (action & ~CPU_TASKS_FROZEN) {
  221. case CPU_STARTING:
  222. gt_clockevents_init(this_cpu_ptr(gt_evt));
  223. break;
  224. case CPU_DYING:
  225. gt_clockevents_stop(this_cpu_ptr(gt_evt));
  226. break;
  227. }
  228. return NOTIFY_OK;
  229. }
  230. static struct notifier_block gt_cpu_nb = {
  231. .notifier_call = gt_cpu_notify,
  232. };
  233. static int __init global_timer_of_register(struct device_node *np)
  234. {
  235. struct clk *gt_clk;
  236. int err = 0;
  237. /*
  238. * In A9 r2p0 the comparators for each processor with the global timer
  239. * fire when the timer value is greater than or equal to. In previous
  240. * revisions the comparators fired when the timer value was equal to.
  241. */
  242. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
  243. && (read_cpuid_id() & 0xf0000f) < 0x200000) {
  244. pr_warn("global-timer: non support for this cpu version.\n");
  245. return -ENOSYS;
  246. }
  247. gt_ppi = irq_of_parse_and_map(np, 0);
  248. if (!gt_ppi) {
  249. pr_warn("global-timer: unable to parse irq\n");
  250. return -EINVAL;
  251. }
  252. gt_base = of_iomap(np, 0);
  253. if (!gt_base) {
  254. pr_warn("global-timer: invalid base address\n");
  255. return -ENXIO;
  256. }
  257. gt_clk = of_clk_get(np, 0);
  258. if (!IS_ERR(gt_clk)) {
  259. err = clk_prepare_enable(gt_clk);
  260. if (err)
  261. goto out_unmap;
  262. } else {
  263. pr_warn("global-timer: clk not found\n");
  264. err = -EINVAL;
  265. goto out_unmap;
  266. }
  267. gt_clk_rate = clk_get_rate(gt_clk);
  268. gt_evt = alloc_percpu(struct clock_event_device);
  269. if (!gt_evt) {
  270. pr_warn("global-timer: can't allocate memory\n");
  271. err = -ENOMEM;
  272. goto out_clk;
  273. }
  274. err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
  275. "gt", gt_evt);
  276. if (err) {
  277. pr_warn("global-timer: can't register interrupt %d (%d)\n",
  278. gt_ppi, err);
  279. goto out_free;
  280. }
  281. err = register_cpu_notifier(&gt_cpu_nb);
  282. if (err) {
  283. pr_warn("global-timer: unable to register cpu notifier.\n");
  284. goto out_irq;
  285. }
  286. /* Immediately configure the timer on the boot CPU */
  287. err = gt_clocksource_init();
  288. if (err)
  289. goto out_irq;
  290. err = gt_clockevents_init(this_cpu_ptr(gt_evt));
  291. if (err)
  292. goto out_irq;
  293. gt_delay_timer_init();
  294. return 0;
  295. out_irq:
  296. free_percpu_irq(gt_ppi, gt_evt);
  297. out_free:
  298. free_percpu(gt_evt);
  299. out_clk:
  300. clk_disable_unprepare(gt_clk);
  301. out_unmap:
  302. iounmap(gt_base);
  303. WARN(err, "ARM Global timer register failed (%d)\n", err);
  304. return err;
  305. }
  306. /* Only tested on r2p2 and r3p0 */
  307. CLOCKSOURCE_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
  308. global_timer_of_register);