moxart_timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * MOXA ART SoCs timer handling.
  3. *
  4. * Copyright (C) 2013 Jonas Jensen
  5. *
  6. * Jonas Jensen <jonas.jensen@gmail.com>
  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/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/io.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/bitops.h>
  23. #define TIMER1_BASE 0x00
  24. #define TIMER2_BASE 0x10
  25. #define TIMER3_BASE 0x20
  26. #define REG_COUNT 0x0 /* writable */
  27. #define REG_LOAD 0x4
  28. #define REG_MATCH1 0x8
  29. #define REG_MATCH2 0xC
  30. #define TIMER_CR 0x30
  31. #define TIMER_INTR_STATE 0x34
  32. #define TIMER_INTR_MASK 0x38
  33. /*
  34. * TIMER_CR flags:
  35. *
  36. * TIMEREG_CR_*_CLOCK 0: PCLK, 1: EXT1CLK
  37. * TIMEREG_CR_*_INT overflow interrupt enable bit
  38. */
  39. #define TIMEREG_CR_1_ENABLE BIT(0)
  40. #define TIMEREG_CR_1_CLOCK BIT(1)
  41. #define TIMEREG_CR_1_INT BIT(2)
  42. #define TIMEREG_CR_2_ENABLE BIT(3)
  43. #define TIMEREG_CR_2_CLOCK BIT(4)
  44. #define TIMEREG_CR_2_INT BIT(5)
  45. #define TIMEREG_CR_3_ENABLE BIT(6)
  46. #define TIMEREG_CR_3_CLOCK BIT(7)
  47. #define TIMEREG_CR_3_INT BIT(8)
  48. #define TIMEREG_CR_COUNT_UP BIT(9)
  49. #define TIMER1_ENABLE (TIMEREG_CR_2_ENABLE | TIMEREG_CR_1_ENABLE)
  50. #define TIMER1_DISABLE (TIMEREG_CR_2_ENABLE)
  51. static void __iomem *base;
  52. static unsigned int clock_count_per_tick;
  53. static int moxart_shutdown(struct clock_event_device *evt)
  54. {
  55. writel(TIMER1_DISABLE, base + TIMER_CR);
  56. return 0;
  57. }
  58. static int moxart_set_oneshot(struct clock_event_device *evt)
  59. {
  60. writel(TIMER1_DISABLE, base + TIMER_CR);
  61. writel(~0, base + TIMER1_BASE + REG_LOAD);
  62. return 0;
  63. }
  64. static int moxart_set_periodic(struct clock_event_device *evt)
  65. {
  66. writel(clock_count_per_tick, base + TIMER1_BASE + REG_LOAD);
  67. writel(TIMER1_ENABLE, base + TIMER_CR);
  68. return 0;
  69. }
  70. static int moxart_clkevt_next_event(unsigned long cycles,
  71. struct clock_event_device *unused)
  72. {
  73. u32 u;
  74. writel(TIMER1_DISABLE, base + TIMER_CR);
  75. u = readl(base + TIMER1_BASE + REG_COUNT) - cycles;
  76. writel(u, base + TIMER1_BASE + REG_MATCH1);
  77. writel(TIMER1_ENABLE, base + TIMER_CR);
  78. return 0;
  79. }
  80. static struct clock_event_device moxart_clockevent = {
  81. .name = "moxart_timer",
  82. .rating = 200,
  83. .features = CLOCK_EVT_FEAT_PERIODIC |
  84. CLOCK_EVT_FEAT_ONESHOT,
  85. .set_state_shutdown = moxart_shutdown,
  86. .set_state_periodic = moxart_set_periodic,
  87. .set_state_oneshot = moxart_set_oneshot,
  88. .tick_resume = moxart_set_oneshot,
  89. .set_next_event = moxart_clkevt_next_event,
  90. };
  91. static irqreturn_t moxart_timer_interrupt(int irq, void *dev_id)
  92. {
  93. struct clock_event_device *evt = dev_id;
  94. evt->event_handler(evt);
  95. return IRQ_HANDLED;
  96. }
  97. static struct irqaction moxart_timer_irq = {
  98. .name = "moxart-timer",
  99. .flags = IRQF_TIMER,
  100. .handler = moxart_timer_interrupt,
  101. .dev_id = &moxart_clockevent,
  102. };
  103. static void __init moxart_timer_init(struct device_node *node)
  104. {
  105. int ret, irq;
  106. unsigned long pclk;
  107. struct clk *clk;
  108. base = of_iomap(node, 0);
  109. if (!base)
  110. panic("%s: of_iomap failed\n", node->full_name);
  111. irq = irq_of_parse_and_map(node, 0);
  112. if (irq <= 0)
  113. panic("%s: irq_of_parse_and_map failed\n", node->full_name);
  114. ret = setup_irq(irq, &moxart_timer_irq);
  115. if (ret)
  116. panic("%s: setup_irq failed\n", node->full_name);
  117. clk = of_clk_get(node, 0);
  118. if (IS_ERR(clk))
  119. panic("%s: of_clk_get failed\n", node->full_name);
  120. pclk = clk_get_rate(clk);
  121. if (clocksource_mmio_init(base + TIMER2_BASE + REG_COUNT,
  122. "moxart_timer", pclk, 200, 32,
  123. clocksource_mmio_readl_down))
  124. panic("%s: clocksource_mmio_init failed\n", node->full_name);
  125. clock_count_per_tick = DIV_ROUND_CLOSEST(pclk, HZ);
  126. writel(~0, base + TIMER2_BASE + REG_LOAD);
  127. writel(TIMEREG_CR_2_ENABLE, base + TIMER_CR);
  128. moxart_clockevent.cpumask = cpumask_of(0);
  129. moxart_clockevent.irq = irq;
  130. /*
  131. * documentation is not publicly available:
  132. * min_delta / max_delta obtained by trial-and-error,
  133. * max_delta 0xfffffffe should be ok because count
  134. * register size is u32
  135. */
  136. clockevents_config_and_register(&moxart_clockevent, pclk,
  137. 0x4, 0xfffffffe);
  138. }
  139. CLOCKSOURCE_OF_DECLARE(moxart, "moxa,moxart-timer", moxart_timer_init);