timer-digicolor.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 void digicolor_clkevt_mode(enum clock_event_mode mode,
  79. struct clock_event_device *ce)
  80. {
  81. struct digicolor_timer *dt = dc_timer(ce);
  82. switch (mode) {
  83. case CLOCK_EVT_MODE_PERIODIC:
  84. dc_timer_disable(ce);
  85. dc_timer_set_count(ce, dt->ticks_per_jiffy);
  86. dc_timer_enable(ce, CONTROL_MODE_PERIODIC);
  87. break;
  88. case CLOCK_EVT_MODE_ONESHOT:
  89. dc_timer_disable(ce);
  90. dc_timer_enable(ce, CONTROL_MODE_ONESHOT);
  91. break;
  92. case CLOCK_EVT_MODE_UNUSED:
  93. case CLOCK_EVT_MODE_SHUTDOWN:
  94. default:
  95. dc_timer_disable(ce);
  96. break;
  97. }
  98. }
  99. static int digicolor_clkevt_next_event(unsigned long evt,
  100. struct clock_event_device *ce)
  101. {
  102. dc_timer_disable(ce);
  103. dc_timer_set_count(ce, evt);
  104. dc_timer_enable(ce, CONTROL_MODE_ONESHOT);
  105. return 0;
  106. }
  107. static struct digicolor_timer dc_timer_dev = {
  108. .ce = {
  109. .name = "digicolor_tick",
  110. .rating = 340,
  111. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  112. .set_mode = digicolor_clkevt_mode,
  113. .set_next_event = digicolor_clkevt_next_event,
  114. },
  115. .timer_id = TIMER_C,
  116. };
  117. static irqreturn_t digicolor_timer_interrupt(int irq, void *dev_id)
  118. {
  119. struct clock_event_device *evt = dev_id;
  120. evt->event_handler(evt);
  121. return IRQ_HANDLED;
  122. }
  123. static u64 digicolor_timer_sched_read(void)
  124. {
  125. return ~readl(dc_timer_dev.base + COUNT(TIMER_B));
  126. }
  127. static void __init digicolor_timer_init(struct device_node *node)
  128. {
  129. unsigned long rate;
  130. struct clk *clk;
  131. int ret, irq;
  132. /*
  133. * timer registers are shared with the watchdog timer;
  134. * don't map exclusively
  135. */
  136. dc_timer_dev.base = of_iomap(node, 0);
  137. if (!dc_timer_dev.base) {
  138. pr_err("Can't map registers");
  139. return;
  140. }
  141. irq = irq_of_parse_and_map(node, dc_timer_dev.timer_id);
  142. if (irq <= 0) {
  143. pr_err("Can't parse IRQ");
  144. return;
  145. }
  146. clk = of_clk_get(node, 0);
  147. if (IS_ERR(clk)) {
  148. pr_err("Can't get timer clock");
  149. return;
  150. }
  151. clk_prepare_enable(clk);
  152. rate = clk_get_rate(clk);
  153. dc_timer_dev.ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  154. writeb(CONTROL_DISABLE, dc_timer_dev.base + CONTROL(TIMER_B));
  155. writel(UINT_MAX, dc_timer_dev.base + COUNT(TIMER_B));
  156. writeb(CONTROL_ENABLE, dc_timer_dev.base + CONTROL(TIMER_B));
  157. sched_clock_register(digicolor_timer_sched_read, 32, rate);
  158. clocksource_mmio_init(dc_timer_dev.base + COUNT(TIMER_B), node->name,
  159. rate, 340, 32, clocksource_mmio_readl_down);
  160. ret = request_irq(irq, digicolor_timer_interrupt,
  161. IRQF_TIMER | IRQF_IRQPOLL, "digicolor_timerC",
  162. &dc_timer_dev.ce);
  163. if (ret)
  164. pr_warn("request of timer irq %d failed (%d)\n", irq, ret);
  165. dc_timer_dev.ce.cpumask = cpu_possible_mask;
  166. dc_timer_dev.ce.irq = irq;
  167. clockevents_config_and_register(&dc_timer_dev.ce, rate, 0, 0xffffffff);
  168. }
  169. CLOCKSOURCE_OF_DECLARE(conexant_digicolor, "cnxt,cx92755-timer",
  170. digicolor_timer_init);