arch_timer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * linux/arch/arm/kernel/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/delay.h>
  14. #include <linux/timex.h>
  15. #include <linux/device.h>
  16. #include <linux/smp.h>
  17. #include <linux/cpu.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_address.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/export.h>
  27. #include <linux/slab.h>
  28. #include <linux/sched_clock.h>
  29. #include <asm/cputype.h>
  30. #include <asm/delay.h>
  31. #include <asm/localtimer.h>
  32. #include <asm/arch_timer.h>
  33. #include <asm/hardware/gic.h>
  34. #include <asm/system_info.h>
  35. static unsigned long arch_timer_rate;
  36. static int arch_timer_spi;
  37. static int arch_timer_ppi;
  38. static int arch_timer_ppi2;
  39. static struct clock_event_device __percpu **arch_timer_evt;
  40. static void __iomem *timer_base;
  41. static struct delay_timer arch_delay_timer;
  42. /*
  43. * Architected system timer support.
  44. */
  45. #define ARCH_TIMER_CTRL_ENABLE (1 << 0)
  46. #define ARCH_TIMER_CTRL_IT_MASK (1 << 1)
  47. #define ARCH_TIMER_CTRL_IT_STAT (1 << 2)
  48. #define ARCH_TIMER_REG_CTRL 0
  49. #define ARCH_TIMER_REG_FREQ 1
  50. #define ARCH_TIMER_REG_TVAL 2
  51. /* Iomapped Register Offsets */
  52. #define QTIMER_CNTP_LOW_REG 0x000
  53. #define QTIMER_CNTP_HIGH_REG 0x004
  54. #define QTIMER_CNTV_LOW_REG 0x008
  55. #define QTIMER_CNTV_HIGH_REG 0x00C
  56. #define QTIMER_CTRL_REG 0x02C
  57. #define QTIMER_FREQ_REG 0x010
  58. #define QTIMER_CNTP_TVAL_REG 0x028
  59. #define QTIMER_CNTV_TVAL_REG 0x038
  60. static inline void timer_reg_write_mem(int reg, u32 val)
  61. {
  62. switch (reg) {
  63. case ARCH_TIMER_REG_CTRL:
  64. __raw_writel(val, timer_base + QTIMER_CTRL_REG);
  65. break;
  66. case ARCH_TIMER_REG_TVAL:
  67. __raw_writel(val, timer_base + QTIMER_CNTP_TVAL_REG);
  68. break;
  69. }
  70. }
  71. static inline void timer_reg_write_cp15(int reg, u32 val)
  72. {
  73. switch (reg) {
  74. case ARCH_TIMER_REG_CTRL:
  75. asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
  76. break;
  77. case ARCH_TIMER_REG_TVAL:
  78. asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val));
  79. break;
  80. }
  81. isb();
  82. }
  83. static inline void arch_timer_reg_write(int cp15, int reg, u32 val)
  84. {
  85. if (cp15)
  86. timer_reg_write_cp15(reg, val);
  87. else
  88. timer_reg_write_mem(reg, val);
  89. }
  90. static inline u32 timer_reg_read_mem(int reg)
  91. {
  92. u32 val;
  93. switch (reg) {
  94. case ARCH_TIMER_REG_CTRL:
  95. val = __raw_readl(timer_base + QTIMER_CTRL_REG);
  96. break;
  97. case ARCH_TIMER_REG_FREQ:
  98. val = __raw_readl(timer_base + QTIMER_FREQ_REG);
  99. break;
  100. case ARCH_TIMER_REG_TVAL:
  101. val = __raw_readl(timer_base + QTIMER_CNTP_TVAL_REG);
  102. break;
  103. default:
  104. BUG();
  105. }
  106. return val;
  107. }
  108. static inline u32 timer_reg_read_cp15(int reg)
  109. {
  110. u32 val;
  111. switch (reg) {
  112. case ARCH_TIMER_REG_CTRL:
  113. asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
  114. break;
  115. case ARCH_TIMER_REG_FREQ:
  116. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
  117. break;
  118. case ARCH_TIMER_REG_TVAL:
  119. asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val));
  120. break;
  121. default:
  122. BUG();
  123. }
  124. return val;
  125. }
  126. static inline u32 arch_timer_reg_read(int cp15, int reg)
  127. {
  128. if (cp15)
  129. return timer_reg_read_cp15(reg);
  130. else
  131. return timer_reg_read_mem(reg);
  132. }
  133. static inline irqreturn_t arch_timer_handler(int cp15,
  134. struct clock_event_device *evt)
  135. {
  136. unsigned long ctrl;
  137. ctrl = arch_timer_reg_read(cp15, ARCH_TIMER_REG_CTRL);
  138. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  139. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  140. arch_timer_reg_write(cp15, ARCH_TIMER_REG_CTRL, ctrl);
  141. evt->event_handler(evt);
  142. return IRQ_HANDLED;
  143. }
  144. return IRQ_NONE;
  145. }
  146. static irqreturn_t arch_timer_handler_cp15(int irq, void *dev_id)
  147. {
  148. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  149. return arch_timer_handler(1, evt);
  150. }
  151. static irqreturn_t arch_timer_handler_mem(int irq, void *dev_id)
  152. {
  153. return arch_timer_handler(0, dev_id);
  154. }
  155. static inline void arch_timer_set_mode(int cp15, enum clock_event_mode mode,
  156. struct clock_event_device *clk)
  157. {
  158. unsigned long ctrl;
  159. switch (mode) {
  160. case CLOCK_EVT_MODE_UNUSED:
  161. case CLOCK_EVT_MODE_SHUTDOWN:
  162. ctrl = arch_timer_reg_read(cp15, ARCH_TIMER_REG_CTRL);
  163. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  164. arch_timer_reg_write(cp15, ARCH_TIMER_REG_CTRL, ctrl);
  165. break;
  166. case CLOCK_EVT_MODE_ONESHOT:
  167. ctrl = arch_timer_reg_read(cp15, ARCH_TIMER_REG_CTRL);
  168. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  169. arch_timer_reg_write(cp15, ARCH_TIMER_REG_CTRL, ctrl);
  170. default:
  171. break;
  172. }
  173. }
  174. static void arch_timer_set_mode_cp15(enum clock_event_mode mode,
  175. struct clock_event_device *clk)
  176. {
  177. arch_timer_set_mode(1, mode, clk);
  178. }
  179. static void arch_timer_set_mode_mem(enum clock_event_mode mode,
  180. struct clock_event_device *clk)
  181. {
  182. arch_timer_set_mode(0, mode, clk);
  183. }
  184. static int arch_timer_set_next_event(int cp15, unsigned long evt,
  185. struct clock_event_device *unused)
  186. {
  187. unsigned long ctrl;
  188. ctrl = arch_timer_reg_read(cp15, ARCH_TIMER_REG_CTRL);
  189. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  190. arch_timer_reg_write(cp15, ARCH_TIMER_REG_CTRL, ctrl);
  191. arch_timer_reg_write(cp15, ARCH_TIMER_REG_TVAL, evt);
  192. return 0;
  193. }
  194. static int arch_timer_set_next_event_cp15(unsigned long evt,
  195. struct clock_event_device *unused)
  196. {
  197. return arch_timer_set_next_event(1, evt, unused);
  198. }
  199. static int arch_timer_set_next_event_mem(unsigned long evt,
  200. struct clock_event_device *unused)
  201. {
  202. return arch_timer_set_next_event(0, evt, unused);
  203. }
  204. static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
  205. {
  206. /* setup clock event only once for CPU 0 */
  207. if (!smp_processor_id() && clk->irq == arch_timer_ppi)
  208. return 0;
  209. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
  210. clk->name = "arch_sys_timer";
  211. clk->rating = 450;
  212. clk->set_mode = arch_timer_set_mode_cp15;
  213. clk->set_next_event = arch_timer_set_next_event_cp15;
  214. clk->irq = arch_timer_ppi;
  215. /* Be safe... */
  216. clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, clk);
  217. clockevents_config_and_register(clk, arch_timer_rate,
  218. 0xf, 0x7fffffff);
  219. *__this_cpu_ptr(arch_timer_evt) = clk;
  220. enable_percpu_irq(clk->irq, 0);
  221. if (arch_timer_ppi2)
  222. enable_percpu_irq(arch_timer_ppi2, 0);
  223. arch_counter_set_user_access();
  224. return 0;
  225. }
  226. /* Is the optional system timer available? */
  227. static int local_timer_is_architected(void)
  228. {
  229. return (cpu_architecture() >= CPU_ARCH_ARMv7) &&
  230. ((read_cpuid_ext(CPUID_EXT_PFR1) >> 16) & 0xf) == 1;
  231. }
  232. static int arch_timer_available(void)
  233. {
  234. unsigned long freq;
  235. if (arch_timer_rate == 0) {
  236. arch_timer_reg_write(1, ARCH_TIMER_REG_CTRL, 0);
  237. freq = arch_timer_reg_read(1, ARCH_TIMER_REG_FREQ);
  238. /* Check the timer frequency. */
  239. if (freq == 0) {
  240. pr_warn("Architected timer frequency not available\n");
  241. return -EINVAL;
  242. }
  243. arch_timer_rate = freq;
  244. pr_info("Architected local timer running at %lu.%02luMHz.\n",
  245. freq / 1000000, (freq / 10000) % 100);
  246. }
  247. return 0;
  248. }
  249. static inline cycle_t notrace counter_get_cntpct_mem(void)
  250. {
  251. u32 cvall, cvalh, thigh;
  252. do {
  253. cvalh = __raw_readl(timer_base + QTIMER_CNTP_HIGH_REG);
  254. cvall = __raw_readl(timer_base + QTIMER_CNTP_LOW_REG);
  255. thigh = __raw_readl(timer_base + QTIMER_CNTP_HIGH_REG);
  256. } while (cvalh != thigh);
  257. return ((cycle_t) cvalh << 32) | cvall;
  258. }
  259. static inline cycle_t notrace counter_get_cntpct_cp15(void)
  260. {
  261. u32 cvall, cvalh;
  262. asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (cvall), "=r" (cvalh));
  263. return ((cycle_t) cvalh << 32) | cvall;
  264. }
  265. static inline cycle_t notrace counter_get_cntvct_mem(void)
  266. {
  267. u32 cvall, cvalh, thigh;
  268. do {
  269. cvalh = __raw_readl(timer_base + QTIMER_CNTV_HIGH_REG);
  270. cvall = __raw_readl(timer_base + QTIMER_CNTV_LOW_REG);
  271. thigh = __raw_readl(timer_base + QTIMER_CNTV_HIGH_REG);
  272. } while (cvalh != thigh);
  273. return ((cycle_t) cvalh << 32) | cvall;
  274. }
  275. static inline cycle_t notrace counter_get_cntvct_cp15(void)
  276. {
  277. u32 cvall, cvalh;
  278. asm volatile("mrrc p15, 1, %0, %1, c14" : "=r" (cvall), "=r" (cvalh));
  279. return ((cycle_t) cvalh << 32) | cvall;
  280. }
  281. static cycle_t (*get_cntpct_func)(void) = counter_get_cntpct_cp15;
  282. static cycle_t (*get_cntvct_func)(void) = counter_get_cntvct_cp15;
  283. cycle_t arch_counter_get_cntpct(void)
  284. {
  285. return get_cntpct_func();
  286. }
  287. EXPORT_SYMBOL(arch_counter_get_cntpct);
  288. static cycle_t arch_counter_read(struct clocksource *cs)
  289. {
  290. return arch_counter_get_cntpct();
  291. }
  292. static unsigned long arch_timer_read_current_timer(void)
  293. {
  294. return arch_counter_get_cntpct();
  295. }
  296. static struct clocksource clocksource_counter = {
  297. .name = "arch_sys_counter",
  298. .rating = 400,
  299. .read = arch_counter_read,
  300. .mask = CLOCKSOURCE_MASK(56),
  301. .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
  302. };
  303. static u32 arch_counter_get_cntvct32(void)
  304. {
  305. cycle_t cntvct;
  306. cntvct = get_cntvct_func();
  307. /*
  308. * The sched_clock infrastructure only knows about counters
  309. * with at most 32bits. Forget about the upper 24 bits for the
  310. * time being...
  311. */
  312. return (u32)(cntvct & (u32)~0);
  313. }
  314. static u64 notrace arch_timer_update_sched_clock(void)
  315. {
  316. return arch_counter_get_cntvct32();
  317. }
  318. static void __cpuinit arch_timer_stop(struct clock_event_device *clk)
  319. {
  320. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  321. clk->irq, smp_processor_id());
  322. disable_percpu_irq(clk->irq);
  323. if (arch_timer_ppi2)
  324. disable_percpu_irq(arch_timer_ppi2);
  325. clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  326. }
  327. static struct local_timer_ops arch_timer_ops __cpuinitdata = {
  328. .setup = arch_timer_setup,
  329. .stop = arch_timer_stop,
  330. };
  331. static struct clock_event_device arch_timer_global_evt;
  332. static void __init arch_timer_counter_init(void)
  333. {
  334. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  335. sched_clock_register(arch_timer_update_sched_clock, 32, arch_timer_rate);
  336. /* Use the architected timer for the delay loop. */
  337. arch_delay_timer.read_current_timer = &arch_timer_read_current_timer;
  338. arch_delay_timer.freq = arch_timer_rate;
  339. register_current_timer_delay(&arch_delay_timer);
  340. }
  341. #ifdef CONFIG_CPU_PM
  342. static unsigned int saved_cntkctl;
  343. static int arch_timer_cpu_pm_notify(struct notifier_block *self,
  344. unsigned long action, void *hcpu)
  345. {
  346. if (action == CPU_PM_ENTER)
  347. saved_cntkctl = arch_timer_get_cntkctl();
  348. else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
  349. arch_timer_set_cntkctl(saved_cntkctl);
  350. return NOTIFY_OK;
  351. }
  352. static struct notifier_block arch_timer_cpu_pm_notifier = {
  353. .notifier_call = arch_timer_cpu_pm_notify,
  354. };
  355. static int __init arch_timer_cpu_pm_init(void)
  356. {
  357. return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
  358. }
  359. #else
  360. static int __init arch_timer_cpu_pm_init(void)
  361. {
  362. return 0;
  363. }
  364. #endif
  365. static int __init arch_timer_common_register(void)
  366. {
  367. int err;
  368. if (!local_timer_is_architected())
  369. return -ENXIO;
  370. err = arch_timer_available();
  371. if (err)
  372. return err;
  373. arch_timer_evt = alloc_percpu(struct clock_event_device *);
  374. if (!arch_timer_evt)
  375. return -ENOMEM;
  376. err = request_percpu_irq(arch_timer_ppi, arch_timer_handler_cp15,
  377. "arch_timer", arch_timer_evt);
  378. if (err) {
  379. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  380. arch_timer_ppi, err);
  381. goto out_free;
  382. }
  383. if (arch_timer_ppi2) {
  384. err = request_percpu_irq(arch_timer_ppi2,
  385. arch_timer_handler_cp15,
  386. "arch_timer", arch_timer_evt);
  387. if (err) {
  388. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  389. arch_timer_ppi2, err);
  390. arch_timer_ppi2 = 0;
  391. goto out_free_irq;
  392. }
  393. }
  394. err = arch_timer_cpu_pm_init();
  395. if (err)
  396. goto out_free_irq;
  397. err = local_timer_register(&arch_timer_ops);
  398. if (err) {
  399. /*
  400. * We couldn't register as a local timer (could be
  401. * because we're on a UP platform, or because some
  402. * other local timer is already present...). Try as a
  403. * global timer instead.
  404. */
  405. arch_timer_global_evt.cpumask = cpumask_of(0);
  406. err = arch_timer_setup(&arch_timer_global_evt);
  407. }
  408. if (err)
  409. goto out_unreg_notify;
  410. return 0;
  411. out_unreg_notify:
  412. cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier);
  413. out_free_irq:
  414. free_percpu_irq(arch_timer_ppi, arch_timer_evt);
  415. if (arch_timer_ppi2)
  416. free_percpu_irq(arch_timer_ppi2, arch_timer_evt);
  417. out_free:
  418. free_percpu(arch_timer_evt);
  419. return err;
  420. }
  421. static int __init arch_timer_mem_register(void)
  422. {
  423. int err;
  424. struct clock_event_device *clk;
  425. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  426. if (!clk)
  427. return -ENOMEM;
  428. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ;
  429. clk->name = "arch_mem_timer";
  430. clk->rating = 400;
  431. clk->set_mode = arch_timer_set_mode_mem;
  432. clk->set_next_event = arch_timer_set_next_event_mem;
  433. clk->irq = arch_timer_spi;
  434. clk->cpumask = cpu_all_mask;
  435. clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, clk);
  436. clockevents_config_and_register(clk, arch_timer_rate,
  437. 0xf, 0x7fffffff);
  438. err = request_irq(arch_timer_spi, arch_timer_handler_mem,
  439. IRQF_TIMER, "arch_timer", clk);
  440. return err;
  441. }
  442. int __init arch_timer_register(struct arch_timer *at)
  443. {
  444. if (at->res[0].start <= 0 || !(at->res[0].flags & IORESOURCE_IRQ))
  445. return -EINVAL;
  446. arch_timer_ppi = at->res[0].start;
  447. if (at->res[1].start > 0 && (at->res[1].flags & IORESOURCE_IRQ))
  448. arch_timer_ppi2 = at->res[1].start;
  449. if (at->res[2].start > 0 && at->res[2].end > 0 &&
  450. (at->res[2].flags & IORESOURCE_MEM))
  451. timer_base = ioremap(at->res[2].start,
  452. resource_size(&at->res[2]));
  453. if (!timer_base) {
  454. pr_err("arch_timer: cant map timer base\n");
  455. return -ENOMEM;
  456. }
  457. return arch_timer_common_register();
  458. }
  459. #ifdef CONFIG_OF
  460. static const struct of_device_id arch_timer_of_match[] __initconst = {
  461. { .compatible = "arm,armv7-timer", },
  462. {},
  463. };
  464. static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
  465. { .compatible = "arm,armv7-timer-mem", },
  466. {},
  467. };
  468. int __init arch_timer_of_register(void)
  469. {
  470. struct device_node *np, *frame;
  471. u32 freq;
  472. int ret;
  473. int has_cp15 = false, has_mem = false;
  474. np = of_find_matching_node(NULL, arch_timer_of_match);
  475. if (np) {
  476. has_cp15 = true;
  477. /*
  478. * Try to determine the frequency from the device tree
  479. */
  480. if (!of_property_read_u32(np, "clock-frequency", &freq))
  481. arch_timer_rate = freq;
  482. ret = irq_of_parse_and_map(np, 0);
  483. if (ret <= 0) {
  484. pr_err("arch_timer: interrupt not specified in timer node\n");
  485. return -ENODEV;
  486. }
  487. arch_timer_ppi = ret;
  488. ret = irq_of_parse_and_map(np, 1);
  489. if (ret > 0)
  490. arch_timer_ppi2 = ret;
  491. ret = arch_timer_common_register();
  492. if (ret)
  493. return ret;
  494. }
  495. np = of_find_matching_node(NULL, arch_timer_mem_of_match);
  496. if (np) {
  497. has_mem = true;
  498. if (!has_cp15) {
  499. get_cntpct_func = counter_get_cntpct_mem;
  500. get_cntvct_func = counter_get_cntvct_mem;
  501. }
  502. /*
  503. * Try to determine the frequency from the device tree
  504. */
  505. if (!of_property_read_u32(np, "clock-frequency", &freq))
  506. arch_timer_rate = freq;
  507. frame = of_get_next_child(np, NULL);
  508. if (!frame) {
  509. pr_err("arch_timer: no child frame\n");
  510. return -EINVAL;
  511. }
  512. timer_base = of_iomap(frame, 0);
  513. if (!timer_base) {
  514. pr_err("arch_timer: cant map timer base\n");
  515. return -ENOMEM;
  516. }
  517. arch_timer_spi = irq_of_parse_and_map(frame, 0);
  518. if (!arch_timer_spi) {
  519. pr_err("arch_timer: no physical timer irq\n");
  520. return -EINVAL;
  521. }
  522. ret = arch_timer_mem_register();
  523. if (ret)
  524. return ret;
  525. }
  526. if (!has_cp15 && !has_mem) {
  527. pr_err("arch_timer: can't find DT node\n");
  528. return -ENODEV;
  529. }
  530. arch_timer_counter_init();
  531. return 0;
  532. }
  533. #endif