timer-econet-en751221.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Timer present on EcoNet EN75xx MIPS based SoCs.
  4. *
  5. * Copyright (C) 2025 by Caleb James DeLisle <cjd@cjdns.fr>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/cpumask.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/sched_clock.h>
  12. #include <linux/of.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_address.h>
  15. #include <linux/cpuhotplug.h>
  16. #include <linux/clk.h>
  17. #define ECONET_BITS 32
  18. #define ECONET_MIN_DELTA 0x00001000
  19. #define ECONET_MAX_DELTA GENMASK(ECONET_BITS - 2, 0)
  20. /* 34Kc hardware has 1 block and 1004Kc has 2. */
  21. #define ECONET_NUM_BLOCKS DIV_ROUND_UP(NR_CPUS, 2)
  22. static struct {
  23. void __iomem *membase[ECONET_NUM_BLOCKS];
  24. u32 freq_hz;
  25. } econet_timer __ro_after_init;
  26. static DEFINE_PER_CPU(struct clock_event_device, econet_timer_pcpu);
  27. /* Each memory block has 2 timers, the order of registers is:
  28. * CTL, CMR0, CNT0, CMR1, CNT1
  29. */
  30. static inline void __iomem *reg_ctl(u32 timer_n)
  31. {
  32. return econet_timer.membase[timer_n >> 1];
  33. }
  34. static inline void __iomem *reg_compare(u32 timer_n)
  35. {
  36. return econet_timer.membase[timer_n >> 1] + (timer_n & 1) * 0x08 + 0x04;
  37. }
  38. static inline void __iomem *reg_count(u32 timer_n)
  39. {
  40. return econet_timer.membase[timer_n >> 1] + (timer_n & 1) * 0x08 + 0x08;
  41. }
  42. static inline u32 ctl_bit_enabled(u32 timer_n)
  43. {
  44. return 1U << (timer_n & 1);
  45. }
  46. static inline u32 ctl_bit_pending(u32 timer_n)
  47. {
  48. return 1U << ((timer_n & 1) + 16);
  49. }
  50. static bool cevt_is_pending(int cpu_id)
  51. {
  52. return ioread32(reg_ctl(cpu_id)) & ctl_bit_pending(cpu_id);
  53. }
  54. static irqreturn_t cevt_interrupt(int irq, void *dev_id)
  55. {
  56. struct clock_event_device *dev = this_cpu_ptr(&econet_timer_pcpu);
  57. int cpu = cpumask_first(dev->cpumask);
  58. /* Each VPE has its own events,
  59. * so this will only happen on spurious interrupt.
  60. */
  61. if (!cevt_is_pending(cpu))
  62. return IRQ_NONE;
  63. iowrite32(ioread32(reg_count(cpu)), reg_compare(cpu));
  64. dev->event_handler(dev);
  65. return IRQ_HANDLED;
  66. }
  67. static int cevt_set_next_event(ulong delta, struct clock_event_device *dev)
  68. {
  69. u32 next;
  70. int cpu;
  71. cpu = cpumask_first(dev->cpumask);
  72. next = ioread32(reg_count(cpu)) + delta;
  73. iowrite32(next, reg_compare(cpu));
  74. if ((s32)(next - ioread32(reg_count(cpu))) < ECONET_MIN_DELTA / 2)
  75. return -ETIME;
  76. return 0;
  77. }
  78. static int cevt_init_cpu(uint cpu)
  79. {
  80. struct clock_event_device *cd = &per_cpu(econet_timer_pcpu, cpu);
  81. u32 reg;
  82. pr_debug("%s: Setting up clockevent for CPU %d\n", cd->name, cpu);
  83. reg = ioread32(reg_ctl(cpu)) | ctl_bit_enabled(cpu);
  84. iowrite32(reg, reg_ctl(cpu));
  85. enable_percpu_irq(cd->irq, IRQ_TYPE_NONE);
  86. /* Do this last because it synchronously configures the timer */
  87. clockevents_config_and_register(cd, econet_timer.freq_hz,
  88. ECONET_MIN_DELTA, ECONET_MAX_DELTA);
  89. return 0;
  90. }
  91. static u64 notrace sched_clock_read(void)
  92. {
  93. /* Always read from clock zero no matter the CPU */
  94. return (u64)ioread32(reg_count(0));
  95. }
  96. /* Init */
  97. static void __init cevt_dev_init(uint cpu)
  98. {
  99. iowrite32(0, reg_count(cpu));
  100. iowrite32(U32_MAX, reg_compare(cpu));
  101. }
  102. static int __init cevt_init(struct device_node *np)
  103. {
  104. int i, irq, ret;
  105. irq = irq_of_parse_and_map(np, 0);
  106. if (irq <= 0) {
  107. pr_err("%pOFn: irq_of_parse_and_map failed", np);
  108. return -EINVAL;
  109. }
  110. ret = request_percpu_irq(irq, cevt_interrupt, np->name, &econet_timer_pcpu);
  111. if (ret < 0) {
  112. pr_err("%pOFn: IRQ %d setup failed (%d)\n", np, irq, ret);
  113. goto err_unmap_irq;
  114. }
  115. for_each_possible_cpu(i) {
  116. struct clock_event_device *cd = &per_cpu(econet_timer_pcpu, i);
  117. cd->rating = 310;
  118. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  119. CLOCK_EVT_FEAT_C3STOP |
  120. CLOCK_EVT_FEAT_PERCPU;
  121. cd->set_next_event = cevt_set_next_event;
  122. cd->irq = irq;
  123. cd->cpumask = cpumask_of(i);
  124. cd->name = np->name;
  125. cevt_dev_init(i);
  126. }
  127. cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  128. "clockevents/econet/timer:starting",
  129. cevt_init_cpu, NULL);
  130. return 0;
  131. err_unmap_irq:
  132. irq_dispose_mapping(irq);
  133. return ret;
  134. }
  135. static int __init timer_init(struct device_node *np)
  136. {
  137. int num_blocks = DIV_ROUND_UP(num_possible_cpus(), 2);
  138. struct clk *clk;
  139. int ret;
  140. clk = of_clk_get(np, 0);
  141. if (IS_ERR(clk)) {
  142. pr_err("%pOFn: Failed to get CPU clock from DT %ld\n", np, PTR_ERR(clk));
  143. return PTR_ERR(clk);
  144. }
  145. econet_timer.freq_hz = clk_get_rate(clk);
  146. for (int i = 0; i < num_blocks; i++) {
  147. econet_timer.membase[i] = of_iomap(np, i);
  148. if (!econet_timer.membase[i]) {
  149. pr_err("%pOFn: failed to map register [%d]\n", np, i);
  150. return -ENXIO;
  151. }
  152. }
  153. /* For clocksource purposes always read clock zero, whatever the CPU */
  154. ret = clocksource_mmio_init(reg_count(0), np->name,
  155. econet_timer.freq_hz, 301, ECONET_BITS,
  156. clocksource_mmio_readl_up);
  157. if (ret) {
  158. pr_err("%pOFn: clocksource_mmio_init failed: %d", np, ret);
  159. return ret;
  160. }
  161. ret = cevt_init(np);
  162. if (ret < 0)
  163. return ret;
  164. sched_clock_register(sched_clock_read, ECONET_BITS,
  165. econet_timer.freq_hz);
  166. pr_info("%pOFn: using %u.%03u MHz high precision timer\n", np,
  167. econet_timer.freq_hz / 1000000,
  168. (econet_timer.freq_hz / 1000) % 1000);
  169. return 0;
  170. }
  171. TIMER_OF_DECLARE(econet_timer_hpt, "econet,en751221-timer", timer_init);