timer-digicolor.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Conexant Digicolor timer driver
  3. *
  4. * Author: Baruch Siach <baruch@tkos.co.il>
  5. *
  6. * Copyright (C) 2014 Paradox Innovation Ltd.
  7. *
  8. * Based on:
  9. * Allwinner SoCs hstimer driver
  10. *
  11. * Copyright (C) 2013 Maxime Ripard
  12. *
  13. * Maxime Ripard <maxime.ripard@free-electrons.com>
  14. *
  15. * This file is licensed under the terms of the GNU General Public
  16. * License version 2. This program is licensed "as is" without any
  17. * warranty of any kind, whether express or implied.
  18. */
  19. /*
  20. * Conexant Digicolor SoCs have 8 configurable timers, named from "Timer A" to
  21. * "Timer H". Timer A is the only one with watchdog support, so it is dedicated
  22. * to the watchdog driver. This driver uses Timer B for sched_clock(), and
  23. * Timer C for clockevents.
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/clk.h>
  27. #include <linux/clockchips.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/irq.h>
  30. #include <linux/irqreturn.h>
  31. #include <linux/sched_clock.h>
  32. #include <linux/of.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. enum {
  36. TIMER_A,
  37. TIMER_B,
  38. TIMER_C,
  39. TIMER_D,
  40. TIMER_E,
  41. TIMER_F,
  42. TIMER_G,
  43. TIMER_H,
  44. };
  45. #define CONTROL(t) ((t)*8)
  46. #define COUNT(t) ((t)*8 + 4)
  47. #define CONTROL_DISABLE 0
  48. #define CONTROL_ENABLE BIT(0)
  49. #define CONTROL_MODE(m) ((m) << 4)
  50. #define CONTROL_MODE_ONESHOT CONTROL_MODE(1)
  51. #define CONTROL_MODE_PERIODIC CONTROL_MODE(2)
  52. struct digicolor_timer {
  53. struct clock_event_device ce;
  54. void __iomem *base;
  55. u32 ticks_per_jiffy;
  56. int timer_id; /* one of TIMER_* */
  57. };
  58. struct digicolor_timer *dc_timer(struct clock_event_device *ce)
  59. {
  60. return container_of(ce, struct digicolor_timer, ce);
  61. }
  62. static inline void dc_timer_disable(struct clock_event_device *ce)
  63. {
  64. struct digicolor_timer *dt = dc_timer(ce);
  65. writeb(CONTROL_DISABLE, dt->base + CONTROL(dt->timer_id));
  66. }
  67. static inline void dc_timer_enable(struct clock_event_device *ce, u32 mode)
  68. {
  69. struct digicolor_timer *dt = dc_timer(ce);
  70. writeb(CONTROL_ENABLE | mode, dt->base + CONTROL(dt->timer_id));
  71. }
  72. static inline void dc_timer_set_count(struct clock_event_device *ce,
  73. unsigned long count)
  74. {
  75. struct digicolor_timer *dt = dc_timer(ce);
  76. writel(count, dt->base + COUNT(dt->timer_id));
  77. }
  78. static int digicolor_clkevt_shutdown(struct clock_event_device *ce)
  79. {
  80. dc_timer_disable(ce);
  81. return 0;
  82. }
  83. static int digicolor_clkevt_set_oneshot(struct clock_event_device *ce)
  84. {
  85. dc_timer_disable(ce);
  86. dc_timer_enable(ce, CONTROL_MODE_ONESHOT);
  87. return 0;
  88. }
  89. static int digicolor_clkevt_set_periodic(struct clock_event_device *ce)
  90. {
  91. struct digicolor_timer *dt = dc_timer(ce);
  92. dc_timer_disable(ce);
  93. dc_timer_set_count(ce, dt->ticks_per_jiffy);
  94. dc_timer_enable(ce, CONTROL_MODE_PERIODIC);
  95. return 0;
  96. }
  97. static int digicolor_clkevt_next_event(unsigned long evt,
  98. struct clock_event_device *ce)
  99. {
  100. dc_timer_disable(ce);
  101. dc_timer_set_count(ce, evt);
  102. dc_timer_enable(ce, CONTROL_MODE_ONESHOT);
  103. return 0;
  104. }
  105. static struct digicolor_timer dc_timer_dev = {
  106. .ce = {
  107. .name = "digicolor_tick",
  108. .rating = 340,
  109. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  110. .set_state_shutdown = digicolor_clkevt_shutdown,
  111. .set_state_periodic = digicolor_clkevt_set_periodic,
  112. .set_state_oneshot = digicolor_clkevt_set_oneshot,
  113. .tick_resume = digicolor_clkevt_shutdown,
  114. .set_next_event = digicolor_clkevt_next_event,
  115. },
  116. .timer_id = TIMER_C,
  117. };
  118. static irqreturn_t digicolor_timer_interrupt(int irq, void *dev_id)
  119. {
  120. struct clock_event_device *evt = dev_id;
  121. evt->event_handler(evt);
  122. return IRQ_HANDLED;
  123. }
  124. static u64 notrace digicolor_timer_sched_read(void)
  125. {
  126. return ~readl(dc_timer_dev.base + COUNT(TIMER_B));
  127. }
  128. static void __init digicolor_timer_init(struct device_node *node)
  129. {
  130. unsigned long rate;
  131. struct clk *clk;
  132. int ret, irq;
  133. /*
  134. * timer registers are shared with the watchdog timer;
  135. * don't map exclusively
  136. */
  137. dc_timer_dev.base = of_iomap(node, 0);
  138. if (!dc_timer_dev.base) {
  139. pr_err("Can't map registers");
  140. return;
  141. }
  142. irq = irq_of_parse_and_map(node, dc_timer_dev.timer_id);
  143. if (irq <= 0) {
  144. pr_err("Can't parse IRQ");
  145. return;
  146. }
  147. clk = of_clk_get(node, 0);
  148. if (IS_ERR(clk)) {
  149. pr_err("Can't get timer clock");
  150. return;
  151. }
  152. clk_prepare_enable(clk);
  153. rate = clk_get_rate(clk);
  154. dc_timer_dev.ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  155. writeb(CONTROL_DISABLE, dc_timer_dev.base + CONTROL(TIMER_B));
  156. writel(UINT_MAX, dc_timer_dev.base + COUNT(TIMER_B));
  157. writeb(CONTROL_ENABLE, dc_timer_dev.base + CONTROL(TIMER_B));
  158. sched_clock_register(digicolor_timer_sched_read, 32, rate);
  159. clocksource_mmio_init(dc_timer_dev.base + COUNT(TIMER_B), node->name,
  160. rate, 340, 32, clocksource_mmio_readl_down);
  161. ret = request_irq(irq, digicolor_timer_interrupt,
  162. IRQF_TIMER | IRQF_IRQPOLL, "digicolor_timerC",
  163. &dc_timer_dev.ce);
  164. if (ret)
  165. pr_warn("request of timer irq %d failed (%d)\n", irq, ret);
  166. dc_timer_dev.ce.cpumask = cpu_possible_mask;
  167. dc_timer_dev.ce.irq = irq;
  168. clockevents_config_and_register(&dc_timer_dev.ce, rate, 0, 0xffffffff);
  169. }
  170. CLOCKSOURCE_OF_DECLARE(conexant_digicolor, "cnxt,cx92755-timer",
  171. digicolor_timer_init);