timer-ixp4xx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IXP4 timer driver
  4. * Copyright (C) 2019 Linus Walleij <[email protected]>
  5. *
  6. * Based on arch/arm/mach-ixp4xx/common.c
  7. * Copyright 2002 (C) Intel Corporation
  8. * Copyright 2003-2004 (C) MontaVista, Software, Inc.
  9. * Copyright (C) Deepak Saxena <[email protected]>
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/clocksource.h>
  15. #include <linux/sched_clock.h>
  16. #include <linux/slab.h>
  17. #include <linux/bitops.h>
  18. #include <linux/delay.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/platform_device.h>
  22. /*
  23. * Constants to make it easy to access Timer Control/Status registers
  24. */
  25. #define IXP4XX_OSTS_OFFSET 0x00 /* Continuous Timestamp */
  26. #define IXP4XX_OST1_OFFSET 0x04 /* Timer 1 Timestamp */
  27. #define IXP4XX_OSRT1_OFFSET 0x08 /* Timer 1 Reload */
  28. #define IXP4XX_OST2_OFFSET 0x0C /* Timer 2 Timestamp */
  29. #define IXP4XX_OSRT2_OFFSET 0x10 /* Timer 2 Reload */
  30. #define IXP4XX_OSST_OFFSET 0x20 /* Timer Status */
  31. /*
  32. * Timer register values and bit definitions
  33. */
  34. #define IXP4XX_OST_ENABLE 0x00000001
  35. #define IXP4XX_OST_ONE_SHOT 0x00000002
  36. /* Low order bits of reload value ignored */
  37. #define IXP4XX_OST_RELOAD_MASK 0x00000003
  38. #define IXP4XX_OST_DISABLED 0x00000000
  39. #define IXP4XX_OSST_TIMER_1_PEND 0x00000001
  40. #define IXP4XX_OSST_TIMER_2_PEND 0x00000002
  41. #define IXP4XX_OSST_TIMER_TS_PEND 0x00000004
  42. /* Remaining registers are for the watchdog and defined in the watchdog driver */
  43. struct ixp4xx_timer {
  44. void __iomem *base;
  45. u32 latch;
  46. struct clock_event_device clkevt;
  47. #ifdef CONFIG_ARM
  48. struct delay_timer delay_timer;
  49. #endif
  50. };
  51. /*
  52. * A local singleton used by sched_clock and delay timer reads, which are
  53. * fast and stateless
  54. */
  55. static struct ixp4xx_timer *local_ixp4xx_timer;
  56. static inline struct ixp4xx_timer *
  57. to_ixp4xx_timer(struct clock_event_device *evt)
  58. {
  59. return container_of(evt, struct ixp4xx_timer, clkevt);
  60. }
  61. static unsigned long ixp4xx_read_timer(void)
  62. {
  63. return __raw_readl(local_ixp4xx_timer->base + IXP4XX_OSTS_OFFSET);
  64. }
  65. static u64 notrace ixp4xx_read_sched_clock(void)
  66. {
  67. return ixp4xx_read_timer();
  68. }
  69. static u64 ixp4xx_clocksource_read(struct clocksource *c)
  70. {
  71. return ixp4xx_read_timer();
  72. }
  73. static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
  74. {
  75. struct ixp4xx_timer *tmr = dev_id;
  76. struct clock_event_device *evt = &tmr->clkevt;
  77. /* Clear Pending Interrupt */
  78. __raw_writel(IXP4XX_OSST_TIMER_1_PEND,
  79. tmr->base + IXP4XX_OSST_OFFSET);
  80. evt->event_handler(evt);
  81. return IRQ_HANDLED;
  82. }
  83. static int ixp4xx_set_next_event(unsigned long cycles,
  84. struct clock_event_device *evt)
  85. {
  86. struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
  87. u32 val;
  88. val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
  89. /* Keep enable/oneshot bits */
  90. val &= IXP4XX_OST_RELOAD_MASK;
  91. __raw_writel((cycles & ~IXP4XX_OST_RELOAD_MASK) | val,
  92. tmr->base + IXP4XX_OSRT1_OFFSET);
  93. return 0;
  94. }
  95. static int ixp4xx_shutdown(struct clock_event_device *evt)
  96. {
  97. struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
  98. u32 val;
  99. val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
  100. val &= ~IXP4XX_OST_ENABLE;
  101. __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
  102. return 0;
  103. }
  104. static int ixp4xx_set_oneshot(struct clock_event_device *evt)
  105. {
  106. struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
  107. __raw_writel(IXP4XX_OST_ENABLE | IXP4XX_OST_ONE_SHOT,
  108. tmr->base + IXP4XX_OSRT1_OFFSET);
  109. return 0;
  110. }
  111. static int ixp4xx_set_periodic(struct clock_event_device *evt)
  112. {
  113. struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
  114. u32 val;
  115. val = tmr->latch & ~IXP4XX_OST_RELOAD_MASK;
  116. val |= IXP4XX_OST_ENABLE;
  117. __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
  118. return 0;
  119. }
  120. static int ixp4xx_resume(struct clock_event_device *evt)
  121. {
  122. struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
  123. u32 val;
  124. val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
  125. val |= IXP4XX_OST_ENABLE;
  126. __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
  127. return 0;
  128. }
  129. /*
  130. * IXP4xx timer tick
  131. * We use OS timer1 on the CPU for the timer tick and the timestamp
  132. * counter as a source of real clock ticks to account for missed jiffies.
  133. */
  134. static __init int ixp4xx_timer_register(void __iomem *base,
  135. int timer_irq,
  136. unsigned int timer_freq)
  137. {
  138. struct ixp4xx_timer *tmr;
  139. int ret;
  140. tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
  141. if (!tmr)
  142. return -ENOMEM;
  143. tmr->base = base;
  144. /*
  145. * The timer register doesn't allow to specify the two least
  146. * significant bits of the timeout value and assumes them being zero.
  147. * So make sure the latch is the best value with the two least
  148. * significant bits unset.
  149. */
  150. tmr->latch = DIV_ROUND_CLOSEST(timer_freq,
  151. (IXP4XX_OST_RELOAD_MASK + 1) * HZ)
  152. * (IXP4XX_OST_RELOAD_MASK + 1);
  153. local_ixp4xx_timer = tmr;
  154. /* Reset/disable counter */
  155. __raw_writel(0, tmr->base + IXP4XX_OSRT1_OFFSET);
  156. /* Clear any pending interrupt on timer 1 */
  157. __raw_writel(IXP4XX_OSST_TIMER_1_PEND,
  158. tmr->base + IXP4XX_OSST_OFFSET);
  159. /* Reset time-stamp counter */
  160. __raw_writel(0, tmr->base + IXP4XX_OSTS_OFFSET);
  161. clocksource_mmio_init(NULL, "OSTS", timer_freq, 200, 32,
  162. ixp4xx_clocksource_read);
  163. tmr->clkevt.name = "ixp4xx timer1";
  164. tmr->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  165. tmr->clkevt.rating = 200;
  166. tmr->clkevt.set_state_shutdown = ixp4xx_shutdown;
  167. tmr->clkevt.set_state_periodic = ixp4xx_set_periodic;
  168. tmr->clkevt.set_state_oneshot = ixp4xx_set_oneshot;
  169. tmr->clkevt.tick_resume = ixp4xx_resume;
  170. tmr->clkevt.set_next_event = ixp4xx_set_next_event;
  171. tmr->clkevt.cpumask = cpumask_of(0);
  172. tmr->clkevt.irq = timer_irq;
  173. ret = request_irq(timer_irq, ixp4xx_timer_interrupt,
  174. IRQF_TIMER, "IXP4XX-TIMER1", tmr);
  175. if (ret) {
  176. pr_crit("no timer IRQ\n");
  177. return -ENODEV;
  178. }
  179. clockevents_config_and_register(&tmr->clkevt, timer_freq,
  180. 0xf, 0xfffffffe);
  181. sched_clock_register(ixp4xx_read_sched_clock, 32, timer_freq);
  182. #ifdef CONFIG_ARM
  183. /* Also use this timer for delays */
  184. tmr->delay_timer.read_current_timer = ixp4xx_read_timer;
  185. tmr->delay_timer.freq = timer_freq;
  186. register_current_timer_delay(&tmr->delay_timer);
  187. #endif
  188. return 0;
  189. }
  190. static struct platform_device ixp4xx_watchdog_device = {
  191. .name = "ixp4xx-watchdog",
  192. .id = -1,
  193. };
  194. /*
  195. * This probe gets called after the timer is already up and running. The main
  196. * function on this platform is to spawn the watchdog device as a child.
  197. */
  198. static int ixp4xx_timer_probe(struct platform_device *pdev)
  199. {
  200. struct device *dev = &pdev->dev;
  201. /* Pass the base address as platform data and nothing else */
  202. ixp4xx_watchdog_device.dev.platform_data = local_ixp4xx_timer->base;
  203. ixp4xx_watchdog_device.dev.parent = dev;
  204. return platform_device_register(&ixp4xx_watchdog_device);
  205. }
  206. static const struct of_device_id ixp4xx_timer_dt_id[] = {
  207. { .compatible = "intel,ixp4xx-timer", },
  208. { /* sentinel */ },
  209. };
  210. static struct platform_driver ixp4xx_timer_driver = {
  211. .probe = ixp4xx_timer_probe,
  212. .driver = {
  213. .name = "ixp4xx-timer",
  214. .of_match_table = ixp4xx_timer_dt_id,
  215. .suppress_bind_attrs = true,
  216. },
  217. };
  218. builtin_platform_driver(ixp4xx_timer_driver);
  219. static __init int ixp4xx_of_timer_init(struct device_node *np)
  220. {
  221. void __iomem *base;
  222. int irq;
  223. int ret;
  224. base = of_iomap(np, 0);
  225. if (!base) {
  226. pr_crit("IXP4xx: can't remap timer\n");
  227. return -ENODEV;
  228. }
  229. irq = irq_of_parse_and_map(np, 0);
  230. if (irq <= 0) {
  231. pr_err("Can't parse IRQ\n");
  232. ret = -EINVAL;
  233. goto out_unmap;
  234. }
  235. /* TODO: get some fixed clocks into the device tree */
  236. ret = ixp4xx_timer_register(base, irq, 66666000);
  237. if (ret)
  238. goto out_unmap;
  239. return 0;
  240. out_unmap:
  241. iounmap(base);
  242. return ret;
  243. }
  244. TIMER_OF_DECLARE(ixp4xx, "intel,ixp4xx-timer", ixp4xx_of_timer_init);