arm_arch_timer.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * linux/drivers/clocksource/arm_arch_timer.c
  3. *
  4. * Copyright (C) 2011 ARM Ltd.
  5. * All Rights Reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/smp.h>
  15. #include <linux/cpu.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/io.h>
  20. #include <asm/arch_timer.h>
  21. #include <clocksource/arm_arch_timer.h>
  22. static u32 arch_timer_rate;
  23. enum ppi_nr {
  24. PHYS_SECURE_PPI,
  25. PHYS_NONSECURE_PPI,
  26. VIRT_PPI,
  27. HYP_PPI,
  28. MAX_TIMER_PPI
  29. };
  30. static int arch_timer_ppi[MAX_TIMER_PPI];
  31. static struct clock_event_device __percpu *arch_timer_evt;
  32. static bool arch_timer_use_virtual = true;
  33. /*
  34. * Architected system timer support.
  35. */
  36. static inline irqreturn_t timer_handler(const int access,
  37. struct clock_event_device *evt)
  38. {
  39. unsigned long ctrl;
  40. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  41. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  42. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  43. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  44. evt->event_handler(evt);
  45. return IRQ_HANDLED;
  46. }
  47. return IRQ_NONE;
  48. }
  49. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  50. {
  51. struct clock_event_device *evt = dev_id;
  52. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  53. }
  54. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  55. {
  56. struct clock_event_device *evt = dev_id;
  57. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  58. }
  59. static inline void timer_set_mode(const int access, int mode)
  60. {
  61. unsigned long ctrl;
  62. switch (mode) {
  63. case CLOCK_EVT_MODE_UNUSED:
  64. case CLOCK_EVT_MODE_SHUTDOWN:
  65. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  66. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  67. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. static void arch_timer_set_mode_virt(enum clock_event_mode mode,
  74. struct clock_event_device *clk)
  75. {
  76. timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode);
  77. }
  78. static void arch_timer_set_mode_phys(enum clock_event_mode mode,
  79. struct clock_event_device *clk)
  80. {
  81. timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode);
  82. }
  83. static inline void set_next_event(const int access, unsigned long evt)
  84. {
  85. unsigned long ctrl;
  86. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  87. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  88. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  89. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt);
  90. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  91. }
  92. static int arch_timer_set_next_event_virt(unsigned long evt,
  93. struct clock_event_device *unused)
  94. {
  95. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt);
  96. return 0;
  97. }
  98. static int arch_timer_set_next_event_phys(unsigned long evt,
  99. struct clock_event_device *unused)
  100. {
  101. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt);
  102. return 0;
  103. }
  104. static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
  105. {
  106. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
  107. clk->name = "arch_sys_timer";
  108. clk->rating = 450;
  109. if (arch_timer_use_virtual) {
  110. clk->irq = arch_timer_ppi[VIRT_PPI];
  111. clk->set_mode = arch_timer_set_mode_virt;
  112. clk->set_next_event = arch_timer_set_next_event_virt;
  113. } else {
  114. clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
  115. clk->set_mode = arch_timer_set_mode_phys;
  116. clk->set_next_event = arch_timer_set_next_event_phys;
  117. }
  118. clk->cpumask = cpumask_of(smp_processor_id());
  119. clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, NULL);
  120. clockevents_config_and_register(clk, arch_timer_rate,
  121. 0xf, 0x7fffffff);
  122. if (arch_timer_use_virtual)
  123. enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
  124. else {
  125. enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
  126. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  127. enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
  128. }
  129. arch_counter_set_user_access();
  130. return 0;
  131. }
  132. static int arch_timer_available(void)
  133. {
  134. u32 freq;
  135. if (arch_timer_rate == 0) {
  136. freq = arch_timer_get_cntfrq();
  137. /* Check the timer frequency. */
  138. if (freq == 0) {
  139. pr_warn("Architected timer frequency not available\n");
  140. return -EINVAL;
  141. }
  142. arch_timer_rate = freq;
  143. }
  144. pr_info_once("Architected local timer running at %lu.%02luMHz (%s).\n",
  145. (unsigned long)arch_timer_rate / 1000000,
  146. (unsigned long)(arch_timer_rate / 10000) % 100,
  147. arch_timer_use_virtual ? "virt" : "phys");
  148. return 0;
  149. }
  150. u32 arch_timer_get_rate(void)
  151. {
  152. return arch_timer_rate;
  153. }
  154. /*
  155. * Some external users of arch_timer_read_counter (e.g. sched_clock) may try to
  156. * call it before it has been initialised. Rather than incur a performance
  157. * penalty checking for initialisation, provide a default implementation that
  158. * won't lead to time appearing to jump backwards.
  159. */
  160. static u64 arch_timer_read_zero(void)
  161. {
  162. return 0;
  163. }
  164. u64 (*arch_timer_read_counter)(void) = arch_timer_read_zero;
  165. static cycle_t arch_counter_read(struct clocksource *cs)
  166. {
  167. return arch_timer_read_counter();
  168. }
  169. static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
  170. {
  171. return arch_timer_read_counter();
  172. }
  173. static struct clocksource clocksource_counter = {
  174. .name = "arch_sys_counter",
  175. .rating = 400,
  176. .read = arch_counter_read,
  177. .mask = CLOCKSOURCE_MASK(56),
  178. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  179. };
  180. static struct cyclecounter cyclecounter = {
  181. .read = arch_counter_read_cc,
  182. .mask = CLOCKSOURCE_MASK(56),
  183. };
  184. static struct timecounter timecounter;
  185. struct timecounter *arch_timer_get_timecounter(void)
  186. {
  187. return &timecounter;
  188. }
  189. static void __cpuinit arch_timer_stop(struct clock_event_device *clk)
  190. {
  191. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  192. clk->irq, smp_processor_id());
  193. if (arch_timer_use_virtual)
  194. disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
  195. else {
  196. disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
  197. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  198. disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  199. }
  200. clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  201. }
  202. static int __cpuinit arch_timer_cpu_notify(struct notifier_block *self,
  203. unsigned long action, void *hcpu)
  204. {
  205. struct clock_event_device *evt = this_cpu_ptr(arch_timer_evt);
  206. switch (action & ~CPU_TASKS_FROZEN) {
  207. case CPU_STARTING:
  208. arch_timer_setup(evt);
  209. break;
  210. case CPU_DYING:
  211. arch_timer_stop(evt);
  212. break;
  213. }
  214. return NOTIFY_OK;
  215. }
  216. static struct notifier_block arch_timer_cpu_nb __cpuinitdata = {
  217. .notifier_call = arch_timer_cpu_notify,
  218. };
  219. static int __init arch_timer_register(void)
  220. {
  221. int err;
  222. int ppi;
  223. err = arch_timer_available();
  224. if (err)
  225. goto out;
  226. arch_timer_evt = alloc_percpu(struct clock_event_device);
  227. if (!arch_timer_evt) {
  228. err = -ENOMEM;
  229. goto out;
  230. }
  231. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  232. cyclecounter.mult = clocksource_counter.mult;
  233. cyclecounter.shift = clocksource_counter.shift;
  234. timecounter_init(&timecounter, &cyclecounter,
  235. arch_counter_get_cntpct());
  236. if (arch_timer_use_virtual) {
  237. ppi = arch_timer_ppi[VIRT_PPI];
  238. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  239. "arch_timer", arch_timer_evt);
  240. } else {
  241. ppi = arch_timer_ppi[PHYS_SECURE_PPI];
  242. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  243. "arch_timer", arch_timer_evt);
  244. if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  245. ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
  246. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  247. "arch_timer", arch_timer_evt);
  248. if (err)
  249. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  250. arch_timer_evt);
  251. }
  252. }
  253. if (err) {
  254. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  255. ppi, err);
  256. goto out_free;
  257. }
  258. err = register_cpu_notifier(&arch_timer_cpu_nb);
  259. if (err)
  260. goto out_free_irq;
  261. /* Immediately configure the timer on the boot CPU */
  262. arch_timer_setup(this_cpu_ptr(arch_timer_evt));
  263. return 0;
  264. out_free_irq:
  265. if (arch_timer_use_virtual)
  266. free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
  267. else {
  268. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  269. arch_timer_evt);
  270. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  271. free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
  272. arch_timer_evt);
  273. }
  274. out_free:
  275. free_percpu(arch_timer_evt);
  276. out:
  277. return err;
  278. }
  279. static const struct of_device_id arch_timer_of_match[] __initconst = {
  280. { .compatible = "arm,armv7-timer", },
  281. {},
  282. };
  283. int __init arch_timer_init(void)
  284. {
  285. struct device_node *np;
  286. u32 freq;
  287. int i;
  288. np = of_find_matching_node(NULL, arch_timer_of_match);
  289. if (!np) {
  290. pr_err("arch_timer: can't find DT node\n");
  291. return -ENODEV;
  292. }
  293. /* Try to determine the frequency from the device tree or CNTFRQ */
  294. if (!of_property_read_u32(np, "clock-frequency", &freq))
  295. arch_timer_rate = freq;
  296. for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
  297. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  298. of_node_put(np);
  299. /*
  300. * If no interrupt provided for virtual timer, we'll have to
  301. * stick to the physical timer. It'd better be accessible...
  302. */
  303. if (!arch_timer_ppi[VIRT_PPI]) {
  304. arch_timer_use_virtual = false;
  305. if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
  306. !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  307. pr_warn("arch_timer: No interrupt available, giving up\n");
  308. return -EINVAL;
  309. }
  310. }
  311. if (arch_timer_use_virtual)
  312. arch_timer_read_counter = arch_counter_get_cntvct;
  313. else
  314. arch_timer_read_counter = arch_counter_get_cntpct;
  315. return arch_timer_register();
  316. }