timer-gx6605s.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/init.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/sched_clock.h>
  6. #include "timer-of.h"
  7. #define CLKSRC_OFFSET 0x40
  8. #define TIMER_STATUS 0x00
  9. #define TIMER_VALUE 0x04
  10. #define TIMER_CONTRL 0x10
  11. #define TIMER_CONFIG 0x20
  12. #define TIMER_DIV 0x24
  13. #define TIMER_INI 0x28
  14. #define GX6605S_STATUS_CLR BIT(0)
  15. #define GX6605S_CONTRL_RST BIT(0)
  16. #define GX6605S_CONTRL_START BIT(1)
  17. #define GX6605S_CONFIG_EN BIT(0)
  18. #define GX6605S_CONFIG_IRQ_EN BIT(1)
  19. static irqreturn_t gx6605s_timer_interrupt(int irq, void *dev)
  20. {
  21. struct clock_event_device *ce = dev;
  22. void __iomem *base = timer_of_base(to_timer_of(ce));
  23. writel_relaxed(GX6605S_STATUS_CLR, base + TIMER_STATUS);
  24. ce->event_handler(ce);
  25. return IRQ_HANDLED;
  26. }
  27. static int gx6605s_timer_set_oneshot(struct clock_event_device *ce)
  28. {
  29. void __iomem *base = timer_of_base(to_timer_of(ce));
  30. /* reset and stop counter */
  31. writel_relaxed(GX6605S_CONTRL_RST, base + TIMER_CONTRL);
  32. /* enable with irq and start */
  33. writel_relaxed(GX6605S_CONFIG_EN | GX6605S_CONFIG_IRQ_EN,
  34. base + TIMER_CONFIG);
  35. return 0;
  36. }
  37. static int gx6605s_timer_set_next_event(unsigned long delta,
  38. struct clock_event_device *ce)
  39. {
  40. void __iomem *base = timer_of_base(to_timer_of(ce));
  41. /* use reset to pause timer */
  42. writel_relaxed(GX6605S_CONTRL_RST, base + TIMER_CONTRL);
  43. /* config next timeout value */
  44. writel_relaxed(ULONG_MAX - delta, base + TIMER_INI);
  45. writel_relaxed(GX6605S_CONTRL_START, base + TIMER_CONTRL);
  46. return 0;
  47. }
  48. static int gx6605s_timer_shutdown(struct clock_event_device *ce)
  49. {
  50. void __iomem *base = timer_of_base(to_timer_of(ce));
  51. writel_relaxed(0, base + TIMER_CONTRL);
  52. writel_relaxed(0, base + TIMER_CONFIG);
  53. return 0;
  54. }
  55. static struct timer_of to = {
  56. .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
  57. .clkevt = {
  58. .rating = 300,
  59. .features = CLOCK_EVT_FEAT_DYNIRQ |
  60. CLOCK_EVT_FEAT_ONESHOT,
  61. .set_state_shutdown = gx6605s_timer_shutdown,
  62. .set_state_oneshot = gx6605s_timer_set_oneshot,
  63. .set_next_event = gx6605s_timer_set_next_event,
  64. .cpumask = cpu_possible_mask,
  65. },
  66. .of_irq = {
  67. .handler = gx6605s_timer_interrupt,
  68. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  69. },
  70. };
  71. static u64 notrace gx6605s_sched_clock_read(void)
  72. {
  73. void __iomem *base;
  74. base = timer_of_base(&to) + CLKSRC_OFFSET;
  75. return (u64)readl_relaxed(base + TIMER_VALUE);
  76. }
  77. static void gx6605s_clkevt_init(void __iomem *base)
  78. {
  79. writel_relaxed(0, base + TIMER_DIV);
  80. writel_relaxed(0, base + TIMER_CONFIG);
  81. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), 2,
  82. ULONG_MAX);
  83. }
  84. static int gx6605s_clksrc_init(void __iomem *base)
  85. {
  86. writel_relaxed(0, base + TIMER_DIV);
  87. writel_relaxed(0, base + TIMER_INI);
  88. writel_relaxed(GX6605S_CONTRL_RST, base + TIMER_CONTRL);
  89. writel_relaxed(GX6605S_CONFIG_EN, base + TIMER_CONFIG);
  90. writel_relaxed(GX6605S_CONTRL_START, base + TIMER_CONTRL);
  91. sched_clock_register(gx6605s_sched_clock_read, 32, timer_of_rate(&to));
  92. return clocksource_mmio_init(base + TIMER_VALUE, "gx6605s",
  93. timer_of_rate(&to), 200, 32, clocksource_mmio_readl_up);
  94. }
  95. static int __init gx6605s_timer_init(struct device_node *np)
  96. {
  97. int ret;
  98. /*
  99. * The timer driver is for nationalchip gx6605s SOC and there are two
  100. * same timer in gx6605s. We use one for clkevt and another for clksrc.
  101. *
  102. * The timer is mmio map to access, so we need give mmio address in dts.
  103. *
  104. * It provides a 32bit countup timer and interrupt will be caused by
  105. * count-overflow.
  106. * So we need set-next-event by ULONG_MAX - delta in TIMER_INI reg.
  107. *
  108. * The counter at 0x0 offset is clock event.
  109. * The counter at 0x40 offset is clock source.
  110. * They are the same in hardware, just different used by driver.
  111. */
  112. ret = timer_of_init(np, &to);
  113. if (ret)
  114. return ret;
  115. gx6605s_clkevt_init(timer_of_base(&to));
  116. return gx6605s_clksrc_init(timer_of_base(&to) + CLKSRC_OFFSET);
  117. }
  118. TIMER_OF_DECLARE(csky_gx6605s_timer, "csky,gx6605s-timer", gx6605s_timer_init);