timer-ep93xx.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cirrus Logic EP93xx timer driver.
  4. * Copyright (C) 2021 Nikita Shubin <nikita.shubin@maquefel.me>
  5. *
  6. * Based on a rewrite of arch/arm/mach-ep93xx/timer.c:
  7. */
  8. #include <linux/clockchips.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/io-64-nonatomic-lo-hi.h>
  14. #include <linux/irq.h>
  15. #include <linux/kernel.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/sched_clock.h>
  19. #include <asm/mach/time.h>
  20. /*************************************************************************
  21. * Timer handling for EP93xx
  22. *************************************************************************
  23. * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and
  24. * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
  25. * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz,
  26. * is free-running, and can't generate interrupts.
  27. *
  28. * The 508 kHz timers are ideal for use for the timer interrupt, as the
  29. * most common values of HZ divide 508 kHz nicely. We pick the 32 bit
  30. * timer (timer 3) to get as long sleep intervals as possible when using
  31. * CONFIG_NO_HZ.
  32. *
  33. * The higher clock rate of timer 4 makes it a better choice than the
  34. * other timers for use as clock source and for sched_clock(), providing
  35. * a stable 40 bit time base.
  36. *************************************************************************
  37. */
  38. #define EP93XX_TIMER1_LOAD 0x00
  39. #define EP93XX_TIMER1_VALUE 0x04
  40. #define EP93XX_TIMER1_CONTROL 0x08
  41. #define EP93XX_TIMER123_CONTROL_ENABLE BIT(7)
  42. #define EP93XX_TIMER123_CONTROL_MODE BIT(6)
  43. #define EP93XX_TIMER123_CONTROL_CLKSEL BIT(3)
  44. #define EP93XX_TIMER1_CLEAR 0x0c
  45. #define EP93XX_TIMER2_LOAD 0x20
  46. #define EP93XX_TIMER2_VALUE 0x24
  47. #define EP93XX_TIMER2_CONTROL 0x28
  48. #define EP93XX_TIMER2_CLEAR 0x2c
  49. /*
  50. * This read-only register contains the low word of the time stamp debug timer
  51. * ( Timer4). When this register is read, the high byte of the Timer4 counter is
  52. * saved in the Timer4ValueHigh register.
  53. */
  54. #define EP93XX_TIMER4_VALUE_LOW 0x60
  55. #define EP93XX_TIMER4_VALUE_HIGH 0x64
  56. #define EP93XX_TIMER4_VALUE_HIGH_ENABLE BIT(8)
  57. #define EP93XX_TIMER3_LOAD 0x80
  58. #define EP93XX_TIMER3_VALUE 0x84
  59. #define EP93XX_TIMER3_CONTROL 0x88
  60. #define EP93XX_TIMER3_CLEAR 0x8c
  61. #define EP93XX_TIMER123_RATE 508469
  62. #define EP93XX_TIMER4_RATE 983040
  63. struct ep93xx_tcu {
  64. void __iomem *base;
  65. };
  66. static struct ep93xx_tcu *ep93xx_tcu;
  67. static u64 ep93xx_clocksource_read(struct clocksource *c)
  68. {
  69. struct ep93xx_tcu *tcu = ep93xx_tcu;
  70. return lo_hi_readq(tcu->base + EP93XX_TIMER4_VALUE_LOW) & GENMASK_ULL(39, 0);
  71. }
  72. static u64 notrace ep93xx_read_sched_clock(void)
  73. {
  74. return ep93xx_clocksource_read(NULL);
  75. }
  76. static int ep93xx_clkevt_set_next_event(unsigned long next,
  77. struct clock_event_device *evt)
  78. {
  79. struct ep93xx_tcu *tcu = ep93xx_tcu;
  80. /* Default mode: periodic, off, 508 kHz */
  81. u32 tmode = EP93XX_TIMER123_CONTROL_MODE |
  82. EP93XX_TIMER123_CONTROL_CLKSEL;
  83. /* Clear timer */
  84. writel(tmode, tcu->base + EP93XX_TIMER3_CONTROL);
  85. /* Set next event */
  86. writel(next, tcu->base + EP93XX_TIMER3_LOAD);
  87. writel(tmode | EP93XX_TIMER123_CONTROL_ENABLE,
  88. tcu->base + EP93XX_TIMER3_CONTROL);
  89. return 0;
  90. }
  91. static int ep93xx_clkevt_shutdown(struct clock_event_device *evt)
  92. {
  93. struct ep93xx_tcu *tcu = ep93xx_tcu;
  94. /* Disable timer */
  95. writel(0, tcu->base + EP93XX_TIMER3_CONTROL);
  96. return 0;
  97. }
  98. static struct clock_event_device ep93xx_clockevent = {
  99. .name = "timer1",
  100. .features = CLOCK_EVT_FEAT_ONESHOT,
  101. .set_state_shutdown = ep93xx_clkevt_shutdown,
  102. .set_state_oneshot = ep93xx_clkevt_shutdown,
  103. .tick_resume = ep93xx_clkevt_shutdown,
  104. .set_next_event = ep93xx_clkevt_set_next_event,
  105. .rating = 300,
  106. };
  107. static irqreturn_t ep93xx_timer_interrupt(int irq, void *dev_id)
  108. {
  109. struct ep93xx_tcu *tcu = ep93xx_tcu;
  110. struct clock_event_device *evt = dev_id;
  111. /* Writing any value clears the timer interrupt */
  112. writel(1, tcu->base + EP93XX_TIMER3_CLEAR);
  113. evt->event_handler(evt);
  114. return IRQ_HANDLED;
  115. }
  116. static int __init ep93xx_timer_of_init(struct device_node *np)
  117. {
  118. int irq;
  119. unsigned long flags = IRQF_TIMER | IRQF_IRQPOLL;
  120. struct ep93xx_tcu *tcu;
  121. int ret;
  122. tcu = kzalloc(sizeof(*tcu), GFP_KERNEL);
  123. if (!tcu)
  124. return -ENOMEM;
  125. tcu->base = of_iomap(np, 0);
  126. if (!tcu->base) {
  127. pr_err("Can't remap registers\n");
  128. ret = -ENXIO;
  129. goto out_free;
  130. }
  131. ep93xx_tcu = tcu;
  132. irq = irq_of_parse_and_map(np, 0);
  133. if (!irq) {
  134. ret = -EINVAL;
  135. pr_err("EP93XX Timer Can't parse IRQ %d", irq);
  136. goto out_free;
  137. }
  138. /* Enable and register clocksource and sched_clock on timer 4 */
  139. writel(EP93XX_TIMER4_VALUE_HIGH_ENABLE,
  140. tcu->base + EP93XX_TIMER4_VALUE_HIGH);
  141. clocksource_mmio_init(NULL, "timer4",
  142. EP93XX_TIMER4_RATE, 200, 40,
  143. ep93xx_clocksource_read);
  144. sched_clock_register(ep93xx_read_sched_clock, 40,
  145. EP93XX_TIMER4_RATE);
  146. /* Set up clockevent on timer 3 */
  147. if (request_irq(irq, ep93xx_timer_interrupt, flags, "ep93xx timer",
  148. &ep93xx_clockevent))
  149. pr_err("Failed to request irq %d (ep93xx timer)\n", irq);
  150. clockevents_config_and_register(&ep93xx_clockevent,
  151. EP93XX_TIMER123_RATE,
  152. 1,
  153. UINT_MAX);
  154. return 0;
  155. out_free:
  156. kfree(tcu);
  157. return ret;
  158. }
  159. TIMER_OF_DECLARE(ep93xx_timer, "cirrus,ep9301-timer", ep93xx_timer_of_init);