jcore-pit.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * J-Core SoC PIT/clocksource driver
  4. *
  5. * Copyright (C) 2015-2016 Smart Energy Instruments, Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/sched_clock.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpuhotplug.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #define PIT_IRQ_SHIFT 12
  18. #define PIT_PRIO_SHIFT 20
  19. #define PIT_ENABLE_SHIFT 26
  20. #define PIT_PRIO_MASK 0xf
  21. #define REG_PITEN 0x00
  22. #define REG_THROT 0x10
  23. #define REG_COUNT 0x14
  24. #define REG_BUSPD 0x18
  25. #define REG_SECHI 0x20
  26. #define REG_SECLO 0x24
  27. #define REG_NSEC 0x28
  28. struct jcore_pit {
  29. struct clock_event_device ced;
  30. void __iomem *base;
  31. unsigned long periodic_delta;
  32. u32 enable_val;
  33. };
  34. static void __iomem *jcore_pit_base;
  35. static struct jcore_pit __percpu *jcore_pit_percpu;
  36. static notrace u64 jcore_sched_clock_read(void)
  37. {
  38. u32 seclo, nsec, seclo0;
  39. __iomem void *base = jcore_pit_base;
  40. seclo = readl(base + REG_SECLO);
  41. do {
  42. seclo0 = seclo;
  43. nsec = readl(base + REG_NSEC);
  44. seclo = readl(base + REG_SECLO);
  45. } while (seclo0 != seclo);
  46. return seclo * NSEC_PER_SEC + nsec;
  47. }
  48. static u64 jcore_clocksource_read(struct clocksource *cs)
  49. {
  50. return jcore_sched_clock_read();
  51. }
  52. static int jcore_pit_disable(struct jcore_pit *pit)
  53. {
  54. writel(0, pit->base + REG_PITEN);
  55. return 0;
  56. }
  57. static int jcore_pit_set(unsigned long delta, struct jcore_pit *pit)
  58. {
  59. jcore_pit_disable(pit);
  60. writel(delta, pit->base + REG_THROT);
  61. writel(pit->enable_val, pit->base + REG_PITEN);
  62. return 0;
  63. }
  64. static int jcore_pit_set_state_shutdown(struct clock_event_device *ced)
  65. {
  66. struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
  67. return jcore_pit_disable(pit);
  68. }
  69. static int jcore_pit_set_state_oneshot(struct clock_event_device *ced)
  70. {
  71. struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
  72. return jcore_pit_disable(pit);
  73. }
  74. static int jcore_pit_set_state_periodic(struct clock_event_device *ced)
  75. {
  76. struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
  77. return jcore_pit_set(pit->periodic_delta, pit);
  78. }
  79. static int jcore_pit_set_next_event(unsigned long delta,
  80. struct clock_event_device *ced)
  81. {
  82. struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
  83. return jcore_pit_set(delta, pit);
  84. }
  85. static int jcore_pit_local_init(unsigned cpu)
  86. {
  87. struct jcore_pit *pit = this_cpu_ptr(jcore_pit_percpu);
  88. unsigned buspd, freq;
  89. pr_info("Local J-Core PIT init on cpu %u\n", cpu);
  90. buspd = readl(pit->base + REG_BUSPD);
  91. freq = DIV_ROUND_CLOSEST(NSEC_PER_SEC, buspd);
  92. pit->periodic_delta = DIV_ROUND_CLOSEST(NSEC_PER_SEC, HZ * buspd);
  93. clockevents_config_and_register(&pit->ced, freq, 1, ULONG_MAX);
  94. return 0;
  95. }
  96. static irqreturn_t jcore_timer_interrupt(int irq, void *dev_id)
  97. {
  98. struct jcore_pit *pit = this_cpu_ptr(dev_id);
  99. if (clockevent_state_oneshot(&pit->ced))
  100. jcore_pit_disable(pit);
  101. pit->ced.event_handler(&pit->ced);
  102. return IRQ_HANDLED;
  103. }
  104. static int __init jcore_pit_init(struct device_node *node)
  105. {
  106. int err;
  107. unsigned pit_irq, cpu;
  108. unsigned long hwirq;
  109. u32 irqprio, enable_val;
  110. jcore_pit_base = of_iomap(node, 0);
  111. if (!jcore_pit_base) {
  112. pr_err("Error: Cannot map base address for J-Core PIT\n");
  113. return -ENXIO;
  114. }
  115. pit_irq = irq_of_parse_and_map(node, 0);
  116. if (!pit_irq) {
  117. pr_err("Error: J-Core PIT has no IRQ\n");
  118. return -ENXIO;
  119. }
  120. pr_info("Initializing J-Core PIT at %p IRQ %d\n",
  121. jcore_pit_base, pit_irq);
  122. err = clocksource_mmio_init(jcore_pit_base, "jcore_pit_cs",
  123. NSEC_PER_SEC, 400, 32,
  124. jcore_clocksource_read);
  125. if (err) {
  126. pr_err("Error registering clocksource device: %d\n", err);
  127. return err;
  128. }
  129. sched_clock_register(jcore_sched_clock_read, 32, NSEC_PER_SEC);
  130. jcore_pit_percpu = alloc_percpu(struct jcore_pit);
  131. if (!jcore_pit_percpu) {
  132. pr_err("Failed to allocate memory for clock event device\n");
  133. return -ENOMEM;
  134. }
  135. err = request_irq(pit_irq, jcore_timer_interrupt,
  136. IRQF_TIMER | IRQF_PERCPU,
  137. "jcore_pit", jcore_pit_percpu);
  138. if (err) {
  139. pr_err("pit irq request failed: %d\n", err);
  140. free_percpu(jcore_pit_percpu);
  141. return err;
  142. }
  143. /*
  144. * The J-Core PIT is not hard-wired to a particular IRQ, but
  145. * integrated with the interrupt controller such that the IRQ it
  146. * generates is programmable, as follows:
  147. *
  148. * The bit layout of the PIT enable register is:
  149. *
  150. * .....e..ppppiiiiiiii............
  151. *
  152. * where the .'s indicate unrelated/unused bits, e is enable,
  153. * p is priority, and i is hard irq number.
  154. *
  155. * For the PIT included in AIC1 (obsolete but still in use),
  156. * any hard irq (trap number) can be programmed via the 8
  157. * iiiiiiii bits, and a priority (0-15) is programmable
  158. * separately in the pppp bits.
  159. *
  160. * For the PIT included in AIC2 (current), the programming
  161. * interface is equivalent modulo interrupt mapping. This is
  162. * why a different compatible tag was not used. However only
  163. * traps 64-127 (the ones actually intended to be used for
  164. * interrupts, rather than syscalls/exceptions/etc.) can be
  165. * programmed (the high 2 bits of i are ignored) and the
  166. * priority pppp is <<2'd and or'd onto the irq number. This
  167. * choice seems to have been made on the hardware engineering
  168. * side under an assumption that preserving old AIC1 priority
  169. * mappings was important. Future models will likely ignore
  170. * the pppp field.
  171. */
  172. hwirq = irq_get_irq_data(pit_irq)->hwirq;
  173. irqprio = (hwirq >> 2) & PIT_PRIO_MASK;
  174. enable_val = (1U << PIT_ENABLE_SHIFT)
  175. | (hwirq << PIT_IRQ_SHIFT)
  176. | (irqprio << PIT_PRIO_SHIFT);
  177. for_each_present_cpu(cpu) {
  178. struct jcore_pit *pit = per_cpu_ptr(jcore_pit_percpu, cpu);
  179. pit->base = of_iomap(node, cpu);
  180. if (!pit->base) {
  181. pr_err("Unable to map PIT for cpu %u\n", cpu);
  182. continue;
  183. }
  184. pit->ced.name = "jcore_pit";
  185. pit->ced.features = CLOCK_EVT_FEAT_PERIODIC
  186. | CLOCK_EVT_FEAT_ONESHOT
  187. | CLOCK_EVT_FEAT_PERCPU;
  188. pit->ced.cpumask = cpumask_of(cpu);
  189. pit->ced.rating = 400;
  190. pit->ced.irq = pit_irq;
  191. pit->ced.set_state_shutdown = jcore_pit_set_state_shutdown;
  192. pit->ced.set_state_periodic = jcore_pit_set_state_periodic;
  193. pit->ced.set_state_oneshot = jcore_pit_set_state_oneshot;
  194. pit->ced.set_next_event = jcore_pit_set_next_event;
  195. pit->enable_val = enable_val;
  196. }
  197. cpuhp_setup_state(CPUHP_AP_JCORE_TIMER_STARTING,
  198. "clockevents/jcore:starting",
  199. jcore_pit_local_init, NULL);
  200. return 0;
  201. }
  202. TIMER_OF_DECLARE(jcore_pit, "jcore,pit", jcore_pit_init);