arm_arch_timer_mmio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARM Generic Memory Mapped Timer support
  4. *
  5. * Split from drivers/clocksource/arm_arch_timer.c
  6. *
  7. * Copyright (C) 2011 ARM Ltd.
  8. * All Rights Reserved
  9. */
  10. #define pr_fmt(fmt) "arch_timer_mmio: " fmt
  11. #include <linux/clockchips.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io-64-nonatomic-lo-hi.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <clocksource/arm_arch_timer.h>
  18. #define CNTTIDR 0x08
  19. #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
  20. #define CNTACR(n) (0x40 + ((n) * 4))
  21. #define CNTACR_RPCT BIT(0)
  22. #define CNTACR_RVCT BIT(1)
  23. #define CNTACR_RFRQ BIT(2)
  24. #define CNTACR_RVOFF BIT(3)
  25. #define CNTACR_RWVT BIT(4)
  26. #define CNTACR_RWPT BIT(5)
  27. #define CNTPCT_LO 0x00
  28. #define CNTVCT_LO 0x08
  29. #define CNTFRQ 0x10
  30. #define CNTP_CVAL_LO 0x20
  31. #define CNTP_CTL 0x2c
  32. #define CNTV_CVAL_LO 0x30
  33. #define CNTV_CTL 0x3c
  34. enum arch_timer_access {
  35. PHYS_ACCESS,
  36. VIRT_ACCESS,
  37. };
  38. struct arch_timer {
  39. struct clock_event_device evt;
  40. struct clocksource cs;
  41. struct arch_timer_mem *gt_block;
  42. void __iomem *base;
  43. enum arch_timer_access access;
  44. u32 rate;
  45. };
  46. #define evt_to_arch_timer(e) container_of(e, struct arch_timer, evt)
  47. #define cs_to_arch_timer(c) container_of(c, struct arch_timer, cs)
  48. static void arch_timer_mmio_write(struct arch_timer *timer,
  49. enum arch_timer_reg reg, u64 val)
  50. {
  51. switch (timer->access) {
  52. case PHYS_ACCESS:
  53. switch (reg) {
  54. case ARCH_TIMER_REG_CTRL:
  55. writel_relaxed((u32)val, timer->base + CNTP_CTL);
  56. return;
  57. case ARCH_TIMER_REG_CVAL:
  58. /*
  59. * Not guaranteed to be atomic, so the timer
  60. * must be disabled at this point.
  61. */
  62. writeq_relaxed(val, timer->base + CNTP_CVAL_LO);
  63. return;
  64. }
  65. break;
  66. case VIRT_ACCESS:
  67. switch (reg) {
  68. case ARCH_TIMER_REG_CTRL:
  69. writel_relaxed((u32)val, timer->base + CNTV_CTL);
  70. return;
  71. case ARCH_TIMER_REG_CVAL:
  72. /* Same restriction as above */
  73. writeq_relaxed(val, timer->base + CNTV_CVAL_LO);
  74. return;
  75. }
  76. break;
  77. }
  78. /* Should never be here */
  79. WARN_ON_ONCE(1);
  80. }
  81. static u32 arch_timer_mmio_read(struct arch_timer *timer, enum arch_timer_reg reg)
  82. {
  83. switch (timer->access) {
  84. case PHYS_ACCESS:
  85. switch (reg) {
  86. case ARCH_TIMER_REG_CTRL:
  87. return readl_relaxed(timer->base + CNTP_CTL);
  88. default:
  89. break;
  90. }
  91. break;
  92. case VIRT_ACCESS:
  93. switch (reg) {
  94. case ARCH_TIMER_REG_CTRL:
  95. return readl_relaxed(timer->base + CNTV_CTL);
  96. default:
  97. break;
  98. }
  99. break;
  100. }
  101. /* Should never be here */
  102. WARN_ON_ONCE(1);
  103. return 0;
  104. }
  105. static noinstr u64 arch_counter_mmio_get_cnt(struct arch_timer *t)
  106. {
  107. int offset_lo = t->access == VIRT_ACCESS ? CNTVCT_LO : CNTPCT_LO;
  108. u32 cnt_lo, cnt_hi, tmp_hi;
  109. do {
  110. cnt_hi = __le32_to_cpu((__le32 __force)__raw_readl(t->base + offset_lo + 4));
  111. cnt_lo = __le32_to_cpu((__le32 __force)__raw_readl(t->base + offset_lo));
  112. tmp_hi = __le32_to_cpu((__le32 __force)__raw_readl(t->base + offset_lo + 4));
  113. } while (cnt_hi != tmp_hi);
  114. return ((u64) cnt_hi << 32) | cnt_lo;
  115. }
  116. static u64 arch_mmio_counter_read(struct clocksource *cs)
  117. {
  118. struct arch_timer *at = cs_to_arch_timer(cs);
  119. return arch_counter_mmio_get_cnt(at);
  120. }
  121. static int arch_timer_mmio_shutdown(struct clock_event_device *clk)
  122. {
  123. struct arch_timer *at = evt_to_arch_timer(clk);
  124. unsigned long ctrl;
  125. ctrl = arch_timer_mmio_read(at, ARCH_TIMER_REG_CTRL);
  126. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  127. arch_timer_mmio_write(at, ARCH_TIMER_REG_CTRL, ctrl);
  128. return 0;
  129. }
  130. static int arch_timer_mmio_set_next_event(unsigned long evt,
  131. struct clock_event_device *clk)
  132. {
  133. struct arch_timer *timer = evt_to_arch_timer(clk);
  134. unsigned long ctrl;
  135. u64 cnt;
  136. ctrl = arch_timer_mmio_read(timer, ARCH_TIMER_REG_CTRL);
  137. /* Timer must be disabled before programming CVAL */
  138. if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
  139. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  140. arch_timer_mmio_write(timer, ARCH_TIMER_REG_CTRL, ctrl);
  141. }
  142. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  143. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  144. cnt = arch_counter_mmio_get_cnt(timer);
  145. arch_timer_mmio_write(timer, ARCH_TIMER_REG_CVAL, evt + cnt);
  146. arch_timer_mmio_write(timer, ARCH_TIMER_REG_CTRL, ctrl);
  147. return 0;
  148. }
  149. static irqreturn_t arch_timer_mmio_handler(int irq, void *dev_id)
  150. {
  151. struct clock_event_device *evt = dev_id;
  152. struct arch_timer *at = evt_to_arch_timer(evt);
  153. unsigned long ctrl;
  154. ctrl = arch_timer_mmio_read(at, ARCH_TIMER_REG_CTRL);
  155. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  156. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  157. arch_timer_mmio_write(at, ARCH_TIMER_REG_CTRL, ctrl);
  158. evt->event_handler(evt);
  159. return IRQ_HANDLED;
  160. }
  161. return IRQ_NONE;
  162. }
  163. static struct arch_timer_mem_frame *find_best_frame(struct platform_device *pdev)
  164. {
  165. struct arch_timer_mem_frame *frame, *best_frame = NULL;
  166. struct arch_timer *at = platform_get_drvdata(pdev);
  167. void __iomem *cntctlbase;
  168. u32 cnttidr;
  169. cntctlbase = ioremap(at->gt_block->cntctlbase, at->gt_block->size);
  170. if (!cntctlbase) {
  171. dev_err(&pdev->dev, "Can't map CNTCTLBase @ %pa\n",
  172. &at->gt_block->cntctlbase);
  173. return NULL;
  174. }
  175. cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
  176. /*
  177. * Try to find a virtual capable frame. Otherwise fall back to a
  178. * physical capable frame.
  179. */
  180. for (int i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  181. u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
  182. CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
  183. frame = &at->gt_block->frame[i];
  184. if (!frame->valid)
  185. continue;
  186. /* Try enabling everything, and see what sticks */
  187. writel_relaxed(cntacr, cntctlbase + CNTACR(i));
  188. cntacr = readl_relaxed(cntctlbase + CNTACR(i));
  189. /* Pick a suitable frame for which we have an IRQ */
  190. if ((cnttidr & CNTTIDR_VIRT(i)) &&
  191. !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT)) &&
  192. frame->virt_irq) {
  193. best_frame = frame;
  194. at->access = VIRT_ACCESS;
  195. break;
  196. }
  197. if ((~cntacr & (CNTACR_RWPT | CNTACR_RPCT)) ||
  198. !frame->phys_irq)
  199. continue;
  200. at->access = PHYS_ACCESS;
  201. best_frame = frame;
  202. }
  203. iounmap(cntctlbase);
  204. return best_frame;
  205. }
  206. static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
  207. {
  208. at->evt = (struct clock_event_device) {
  209. .features = (CLOCK_EVT_FEAT_ONESHOT |
  210. CLOCK_EVT_FEAT_DYNIRQ),
  211. .name = "arch_mem_timer",
  212. .rating = 400,
  213. .cpumask = cpu_possible_mask,
  214. .irq = irq,
  215. .set_next_event = arch_timer_mmio_set_next_event,
  216. .set_state_oneshot_stopped = arch_timer_mmio_shutdown,
  217. .set_state_shutdown = arch_timer_mmio_shutdown,
  218. };
  219. at->evt.set_state_shutdown(&at->evt);
  220. clockevents_config_and_register(&at->evt, at->rate, 0xf,
  221. (unsigned long)CLOCKSOURCE_MASK(56));
  222. enable_irq(at->evt.irq);
  223. at->cs = (struct clocksource) {
  224. .name = "arch_mmio_counter",
  225. .rating = 300,
  226. .read = arch_mmio_counter_read,
  227. .mask = CLOCKSOURCE_MASK(56),
  228. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  229. };
  230. clocksource_register_hz(&at->cs, at->rate);
  231. }
  232. static int arch_timer_mmio_frame_register(struct platform_device *pdev,
  233. struct arch_timer_mem_frame *frame)
  234. {
  235. struct arch_timer *at = platform_get_drvdata(pdev);
  236. struct device_node *np = pdev->dev.of_node;
  237. int ret, irq;
  238. u32 rate;
  239. if (!devm_request_mem_region(&pdev->dev, frame->cntbase, frame->size,
  240. "arch_mem_timer"))
  241. return -EBUSY;
  242. at->base = devm_ioremap(&pdev->dev, frame->cntbase, frame->size);
  243. if (!at->base) {
  244. dev_err(&pdev->dev, "Can't map frame's registers\n");
  245. return -ENXIO;
  246. }
  247. /*
  248. * Allow "clock-frequency" to override the probed rate. If neither
  249. * lead to something useful, use the CPU timer frequency as the
  250. * fallback. The nice thing about that last point is that we woudn't
  251. * made it here if we didn't have a valid frequency.
  252. */
  253. rate = readl_relaxed(at->base + CNTFRQ);
  254. if (!np || of_property_read_u32(np, "clock-frequency", &at->rate))
  255. at->rate = rate;
  256. if (!at->rate)
  257. at->rate = arch_timer_get_rate();
  258. irq = at->access == VIRT_ACCESS ? frame->virt_irq : frame->phys_irq;
  259. ret = devm_request_irq(&pdev->dev, irq, arch_timer_mmio_handler,
  260. IRQF_TIMER | IRQF_NO_AUTOEN, "arch_mem_timer",
  261. &at->evt);
  262. if (ret) {
  263. dev_err(&pdev->dev, "Failed to request mem timer irq\n");
  264. return ret;
  265. }
  266. /* Afer this point, we're not allowed to fail anymore */
  267. arch_timer_mmio_setup(at, irq);
  268. return 0;
  269. }
  270. static int of_populate_gt_block(struct platform_device *pdev,
  271. struct arch_timer *at)
  272. {
  273. struct resource res;
  274. if (of_address_to_resource(pdev->dev.of_node, 0, &res))
  275. return -EINVAL;
  276. at->gt_block->cntctlbase = res.start;
  277. at->gt_block->size = resource_size(&res);
  278. for_each_available_child_of_node_scoped(pdev->dev.of_node, frame_node) {
  279. struct arch_timer_mem_frame *frame;
  280. u32 n;
  281. if (of_property_read_u32(frame_node, "frame-number", &n)) {
  282. dev_err(&pdev->dev, FW_BUG "Missing frame-number\n");
  283. return -EINVAL;
  284. }
  285. if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
  286. dev_err(&pdev->dev,
  287. FW_BUG "Wrong frame-number, only 0-%u are permitted\n",
  288. ARCH_TIMER_MEM_MAX_FRAMES - 1);
  289. return -EINVAL;
  290. }
  291. frame = &at->gt_block->frame[n];
  292. if (frame->valid) {
  293. dev_err(&pdev->dev, FW_BUG "Duplicated frame-number\n");
  294. return -EINVAL;
  295. }
  296. if (of_address_to_resource(frame_node, 0, &res))
  297. return -EINVAL;
  298. frame->cntbase = res.start;
  299. frame->size = resource_size(&res);
  300. frame->phys_irq = irq_of_parse_and_map(frame_node, 0);
  301. frame->virt_irq = irq_of_parse_and_map(frame_node, 1);
  302. frame->valid = true;
  303. }
  304. return 0;
  305. }
  306. static int arch_timer_mmio_probe(struct platform_device *pdev)
  307. {
  308. struct arch_timer_mem_frame *frame;
  309. struct arch_timer *at;
  310. struct device_node *np;
  311. int ret;
  312. np = pdev->dev.of_node;
  313. at = devm_kmalloc(&pdev->dev, sizeof(*at), GFP_KERNEL | __GFP_ZERO);
  314. if (!at)
  315. return -ENOMEM;
  316. if (np) {
  317. at->gt_block = devm_kmalloc(&pdev->dev, sizeof(*at->gt_block),
  318. GFP_KERNEL | __GFP_ZERO);
  319. if (!at->gt_block)
  320. return -ENOMEM;
  321. ret = of_populate_gt_block(pdev, at);
  322. if (ret)
  323. return ret;
  324. } else {
  325. at->gt_block = dev_get_platdata(&pdev->dev);
  326. }
  327. platform_set_drvdata(pdev, at);
  328. frame = find_best_frame(pdev);
  329. if (!frame) {
  330. dev_err(&pdev->dev,
  331. "Unable to find a suitable frame in timer @ %pa\n",
  332. &at->gt_block->cntctlbase);
  333. return -EINVAL;
  334. }
  335. ret = arch_timer_mmio_frame_register(pdev, frame);
  336. if (!ret)
  337. dev_info(&pdev->dev,
  338. "mmio timer running at %lu.%02luMHz (%s)\n",
  339. (unsigned long)at->rate / 1000000,
  340. (unsigned long)(at->rate / 10000) % 100,
  341. at->access == VIRT_ACCESS ? "virt" : "phys");
  342. return ret;
  343. }
  344. static const struct of_device_id arch_timer_mmio_of_table[] = {
  345. { .compatible = "arm,armv7-timer-mem", },
  346. {}
  347. };
  348. static struct platform_driver arch_timer_mmio_drv = {
  349. .driver = {
  350. .name = "arch-timer-mmio",
  351. .of_match_table = arch_timer_mmio_of_table,
  352. .suppress_bind_attrs = true,
  353. },
  354. .probe = arch_timer_mmio_probe,
  355. };
  356. builtin_platform_driver(arch_timer_mmio_drv);
  357. static struct platform_driver arch_timer_mmio_acpi_drv = {
  358. .driver = {
  359. .name = "gtdt-arm-mmio-timer",
  360. .suppress_bind_attrs = true,
  361. },
  362. .probe = arch_timer_mmio_probe,
  363. };
  364. builtin_platform_driver(arch_timer_mmio_acpi_drv);