meson6_timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 void meson6_clkevt_mode(enum clock_event_mode mode,
  56. struct clock_event_device *clk)
  57. {
  58. switch (mode) {
  59. case CLOCK_EVT_MODE_PERIODIC:
  60. meson6_clkevt_time_stop(CED_ID);
  61. meson6_clkevt_time_setup(CED_ID, USEC_PER_SEC/HZ - 1);
  62. meson6_clkevt_time_start(CED_ID, true);
  63. break;
  64. case CLOCK_EVT_MODE_ONESHOT:
  65. meson6_clkevt_time_stop(CED_ID);
  66. meson6_clkevt_time_start(CED_ID, false);
  67. break;
  68. case CLOCK_EVT_MODE_UNUSED:
  69. case CLOCK_EVT_MODE_SHUTDOWN:
  70. default:
  71. meson6_clkevt_time_stop(CED_ID);
  72. break;
  73. }
  74. }
  75. static int meson6_clkevt_next_event(unsigned long evt,
  76. struct clock_event_device *unused)
  77. {
  78. meson6_clkevt_time_stop(CED_ID);
  79. meson6_clkevt_time_setup(CED_ID, evt);
  80. meson6_clkevt_time_start(CED_ID, false);
  81. return 0;
  82. }
  83. static struct clock_event_device meson6_clockevent = {
  84. .name = "meson6_tick",
  85. .rating = 400,
  86. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  87. .set_mode = meson6_clkevt_mode,
  88. .set_next_event = meson6_clkevt_next_event,
  89. };
  90. static irqreturn_t meson6_timer_interrupt(int irq, void *dev_id)
  91. {
  92. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  93. evt->event_handler(evt);
  94. return IRQ_HANDLED;
  95. }
  96. static struct irqaction meson6_timer_irq = {
  97. .name = "meson6_timer",
  98. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  99. .handler = meson6_timer_interrupt,
  100. .dev_id = &meson6_clockevent,
  101. };
  102. static void __init meson6_timer_init(struct device_node *node)
  103. {
  104. u32 val;
  105. int ret, irq;
  106. timer_base = of_io_request_and_map(node, 0, "meson6-timer");
  107. if (IS_ERR(timer_base))
  108. panic("Can't map registers");
  109. irq = irq_of_parse_and_map(node, 0);
  110. if (irq <= 0)
  111. panic("Can't parse IRQ");
  112. /* Set 1us for timer E */
  113. val = readl(timer_base + TIMER_ISA_MUX);
  114. val &= ~TIMER_CSD_INPUT_MASK;
  115. val |= TIMER_CSD_UNIT_1US << TIMER_INPUT_BIT(CSD_ID);
  116. writel(val, timer_base + TIMER_ISA_MUX);
  117. sched_clock_register(meson6_timer_sched_read, 32, USEC_PER_SEC);
  118. clocksource_mmio_init(timer_base + TIMER_ISA_VAL(CSD_ID), node->name,
  119. 1000 * 1000, 300, 32, clocksource_mmio_readl_up);
  120. /* Timer A base 1us */
  121. val &= ~TIMER_CED_INPUT_MASK;
  122. val |= TIMER_CED_UNIT_1US << TIMER_INPUT_BIT(CED_ID);
  123. writel(val, timer_base + TIMER_ISA_MUX);
  124. /* Stop the timer A */
  125. meson6_clkevt_time_stop(CED_ID);
  126. ret = setup_irq(irq, &meson6_timer_irq);
  127. if (ret)
  128. pr_warn("failed to setup irq %d\n", irq);
  129. meson6_clockevent.cpumask = cpu_possible_mask;
  130. meson6_clockevent.irq = irq;
  131. clockevents_config_and_register(&meson6_clockevent, USEC_PER_SEC,
  132. 1, 0xfffe);
  133. }
  134. CLOCKSOURCE_OF_DECLARE(meson6, "amlogic,meson6-timer",
  135. meson6_timer_init);