meson6_timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Amlogic Meson6 SoCs timer handling.
  3. *
  4. * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
  5. *
  6. * Based on code from Amlogic, Inc
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqreturn.h>
  17. #include <linux/sched_clock.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #define CED_ID 0
  22. #define CSD_ID 4
  23. #define TIMER_ISA_MUX 0
  24. #define TIMER_ISA_VAL(t) (((t) + 1) << 2)
  25. #define TIMER_INPUT_BIT(t) (2 * (t))
  26. #define TIMER_ENABLE_BIT(t) (16 + (t))
  27. #define TIMER_PERIODIC_BIT(t) (12 + (t))
  28. #define TIMER_CED_INPUT_MASK (3UL << TIMER_INPUT_BIT(CED_ID))
  29. #define TIMER_CSD_INPUT_MASK (7UL << TIMER_INPUT_BIT(CSD_ID))
  30. #define TIMER_CED_UNIT_1US 0
  31. #define TIMER_CSD_UNIT_1US 1
  32. static void __iomem *timer_base;
  33. static u64 notrace meson6_timer_sched_read(void)
  34. {
  35. return (u64)readl(timer_base + TIMER_ISA_VAL(CSD_ID));
  36. }
  37. static void meson6_clkevt_time_stop(unsigned char timer)
  38. {
  39. u32 val = readl(timer_base + TIMER_ISA_MUX);
  40. writel(val & ~TIMER_ENABLE_BIT(timer), timer_base + TIMER_ISA_MUX);
  41. }
  42. static void meson6_clkevt_time_setup(unsigned char timer, unsigned long delay)
  43. {
  44. writel(delay, timer_base + TIMER_ISA_VAL(timer));
  45. }
  46. static void meson6_clkevt_time_start(unsigned char timer, bool periodic)
  47. {
  48. u32 val = readl(timer_base + TIMER_ISA_MUX);
  49. if (periodic)
  50. val |= TIMER_PERIODIC_BIT(timer);
  51. else
  52. val &= ~TIMER_PERIODIC_BIT(timer);
  53. writel(val | TIMER_ENABLE_BIT(timer), timer_base + TIMER_ISA_MUX);
  54. }
  55. static int meson6_shutdown(struct clock_event_device *evt)
  56. {
  57. meson6_clkevt_time_stop(CED_ID);
  58. return 0;
  59. }
  60. static int meson6_set_oneshot(struct clock_event_device *evt)
  61. {
  62. meson6_clkevt_time_stop(CED_ID);
  63. meson6_clkevt_time_start(CED_ID, false);
  64. return 0;
  65. }
  66. static int meson6_set_periodic(struct clock_event_device *evt)
  67. {
  68. meson6_clkevt_time_stop(CED_ID);
  69. meson6_clkevt_time_setup(CED_ID, USEC_PER_SEC / HZ - 1);
  70. meson6_clkevt_time_start(CED_ID, true);
  71. return 0;
  72. }
  73. static int meson6_clkevt_next_event(unsigned long evt,
  74. struct clock_event_device *unused)
  75. {
  76. meson6_clkevt_time_stop(CED_ID);
  77. meson6_clkevt_time_setup(CED_ID, evt);
  78. meson6_clkevt_time_start(CED_ID, false);
  79. return 0;
  80. }
  81. static struct clock_event_device meson6_clockevent = {
  82. .name = "meson6_tick",
  83. .rating = 400,
  84. .features = CLOCK_EVT_FEAT_PERIODIC |
  85. CLOCK_EVT_FEAT_ONESHOT,
  86. .set_state_shutdown = meson6_shutdown,
  87. .set_state_periodic = meson6_set_periodic,
  88. .set_state_oneshot = meson6_set_oneshot,
  89. .tick_resume = meson6_shutdown,
  90. .set_next_event = meson6_clkevt_next_event,
  91. };
  92. static irqreturn_t meson6_timer_interrupt(int irq, void *dev_id)
  93. {
  94. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  95. evt->event_handler(evt);
  96. return IRQ_HANDLED;
  97. }
  98. static struct irqaction meson6_timer_irq = {
  99. .name = "meson6_timer",
  100. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  101. .handler = meson6_timer_interrupt,
  102. .dev_id = &meson6_clockevent,
  103. };
  104. static int __init meson6_timer_init(struct device_node *node)
  105. {
  106. u32 val;
  107. int ret, irq;
  108. timer_base = of_io_request_and_map(node, 0, "meson6-timer");
  109. if (IS_ERR(timer_base)) {
  110. pr_err("Can't map registers\n");
  111. return -ENXIO;
  112. }
  113. irq = irq_of_parse_and_map(node, 0);
  114. if (irq <= 0) {
  115. pr_err("Can't parse IRQ\n");
  116. return -EINVAL;
  117. }
  118. /* Set 1us for timer E */
  119. val = readl(timer_base + TIMER_ISA_MUX);
  120. val &= ~TIMER_CSD_INPUT_MASK;
  121. val |= TIMER_CSD_UNIT_1US << TIMER_INPUT_BIT(CSD_ID);
  122. writel(val, timer_base + TIMER_ISA_MUX);
  123. sched_clock_register(meson6_timer_sched_read, 32, USEC_PER_SEC);
  124. clocksource_mmio_init(timer_base + TIMER_ISA_VAL(CSD_ID), node->name,
  125. 1000 * 1000, 300, 32, clocksource_mmio_readl_up);
  126. /* Timer A base 1us */
  127. val &= ~TIMER_CED_INPUT_MASK;
  128. val |= TIMER_CED_UNIT_1US << TIMER_INPUT_BIT(CED_ID);
  129. writel(val, timer_base + TIMER_ISA_MUX);
  130. /* Stop the timer A */
  131. meson6_clkevt_time_stop(CED_ID);
  132. ret = setup_irq(irq, &meson6_timer_irq);
  133. if (ret) {
  134. pr_warn("failed to setup irq %d\n", irq);
  135. return ret;
  136. }
  137. meson6_clockevent.cpumask = cpu_possible_mask;
  138. meson6_clockevent.irq = irq;
  139. clockevents_config_and_register(&meson6_clockevent, USEC_PER_SEC,
  140. 1, 0xfffe);
  141. return 0;
  142. }
  143. TIMER_OF_DECLARE(meson6, "amlogic,meson6-timer",
  144. meson6_timer_init);