timer-arkmicro.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 2018-2019 Arkmicro, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/sched_clock.h>
  28. #include <linux/errno.h>
  29. #define TIMER0_BASE 0x00
  30. #define TIMER1_BASE 0x04
  31. #define TIMER2_BASE 0x08
  32. #if defined(CONFIG_TIMER_ARK1668)
  33. #define TIMER_TCTL 0x00
  34. #define TIMER_TPRS 0x10
  35. #define TIMER_TMOD 0x20
  36. #define TIMER_TCNT 0x30
  37. #define TIMER_TSTA 0x40
  38. #define TIMER_INT_MAINTAIN 0
  39. #define TIMER_CNT_VAL 0xffffff
  40. #define TIMER_CNT_BITS 24
  41. #elif defined(CONFIG_TIMER_ARKN141)
  42. #define TIMER_TCTL 0x00
  43. #define TIMER_TPRS 0x0C
  44. #define TIMER_TMOD 0x18
  45. #define TIMER_TCNT 0x24
  46. #define TIMER_INT_MAINTAIN (1 << 3)
  47. #define TIMER_INT_STATUS (1 << 4)
  48. #define TIMER_CNT_VAL 0xffffffff
  49. #define TIMER_CNT_BITS 32
  50. #define USE_SIBLING_TIMER
  51. #endif
  52. #define TIMER_CTRL_IE (1 << 2)
  53. #define TIMER_CTRL_PERIODIC (1 << 1)
  54. #define TIMER_CTRL_ENABLE (1 << 0)
  55. static void __iomem *sched_clock_base;
  56. static u64 notrace ark_read(void)
  57. {
  58. return ~(readl_relaxed(sched_clock_base + TIMER_TCNT) | ((u64)((1 << (32 - TIMER_CNT_BITS)) - 1) << TIMER_CNT_BITS));
  59. }
  60. void __init ark_timer_disable(void __iomem *base)
  61. {
  62. writel(0, base + TIMER_TCTL);
  63. }
  64. int __init ark_clocksource_and_sched_clock_init(void __iomem *base,
  65. const char *name,
  66. struct clk *clk,
  67. int use_sched_clock)
  68. {
  69. long rate;
  70. rate = clk_get_rate(clk);
  71. if (rate == 0)
  72. return -EINVAL;
  73. /* setup timer 1 as free-running clocksource */
  74. writel(0, base + TIMER_TCTL);
  75. writel(TIMER_CNT_VAL, base + TIMER_TMOD);
  76. writel(TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  77. base + TIMER_TCTL);
  78. clocksource_mmio_init(base + TIMER_TCNT, name,
  79. rate, 300, TIMER_CNT_BITS, clocksource_mmio_readl_down);
  80. if (use_sched_clock) {
  81. sched_clock_base = base;
  82. sched_clock_register(ark_read, TIMER_CNT_BITS, rate);
  83. }
  84. return 0;
  85. }
  86. static void __iomem *clkevt_base;
  87. static unsigned long clkevt_reload;
  88. #ifdef USE_SIBLING_TIMER
  89. static unsigned long usec_reload;
  90. #endif
  91. /*
  92. * IRQ handler for the timer
  93. */
  94. static irqreturn_t ark_timer_interrupt(int irq, void *dev_id)
  95. {
  96. struct clock_event_device *evt = dev_id;
  97. u32 reg;
  98. /* clear the interrupt */
  99. #if defined(CONFIG_TIMER_ARK1668)
  100. reg = readl(clkevt_base + TIMER_TSTA);
  101. reg &= ~1;
  102. writel(reg, clkevt_base + TIMER_TSTA);
  103. #elif defined(CONFIG_TIMER_ARKN141)
  104. reg = readl(clkevt_base + TIMER_TCTL);
  105. reg &= ~TIMER_INT_STATUS;
  106. writel(reg, clkevt_base + TIMER_TCTL);
  107. #endif
  108. #ifdef USE_SIBLING_TIMER
  109. writel(0, clkevt_base + TIMER2_BASE + TIMER_TCTL);
  110. #endif
  111. evt->event_handler(evt);
  112. return IRQ_HANDLED;
  113. }
  114. static inline void timer_shutdown(struct clock_event_device *evt)
  115. {
  116. #if defined(CONFIG_TIMER_ARK1668)
  117. writel(0, clkevt_base + TIMER_TCTL);
  118. #elif defined(CONFIG_TIMER_ARKN141)
  119. /* not clear interrupt */
  120. writel(1 << TIMER_INT_STATUS, clkevt_base + TIMER_TCTL);
  121. #endif
  122. }
  123. static int ark_shutdown(struct clock_event_device *evt)
  124. {
  125. timer_shutdown(evt);
  126. return 0;
  127. }
  128. static int ark_set_periodic(struct clock_event_device *evt)
  129. {
  130. unsigned long ctrl = TIMER_INT_MAINTAIN | TIMER_CTRL_IE | TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  131. timer_shutdown(evt);
  132. #if defined(CONFIG_TIMER_ARK1668)
  133. writel(1 << 1, clkevt_base + TIMER_TSTA);
  134. #endif
  135. writel(clkevt_reload, clkevt_base + TIMER_TMOD);
  136. writel(ctrl, clkevt_base + TIMER_TCTL);
  137. return 0;
  138. }
  139. static int ark_set_next_event(unsigned long next,
  140. struct clock_event_device *evt)
  141. {
  142. unsigned long ctrl = TIMER_INT_MAINTAIN | TIMER_CTRL_IE | TIMER_CTRL_ENABLE;
  143. timer_shutdown(evt);
  144. #if defined(CONFIG_TIMER_ARK1668)
  145. writel(1 << 1, clkevt_base + TIMER_TSTA);
  146. #endif
  147. writel(next, clkevt_base + TIMER_TMOD);
  148. writel(ctrl, clkevt_base + TIMER_TCTL);
  149. #ifdef USE_SIBLING_TIMER
  150. writel(0, clkevt_base + TIMER2_BASE + TIMER_TCTL);
  151. writel(next + usec_reload, clkevt_base + TIMER2_BASE + TIMER_TMOD);
  152. writel(ctrl, clkevt_base + TIMER2_BASE + TIMER_TCTL);
  153. #endif
  154. return 0;
  155. }
  156. static struct clock_event_device ark_clockevent = {
  157. .features = CLOCK_EVT_FEAT_PERIODIC |
  158. CLOCK_EVT_FEAT_ONESHOT,
  159. .set_state_shutdown = ark_shutdown,
  160. .set_state_periodic = ark_set_periodic,
  161. .set_state_oneshot = ark_shutdown,
  162. .tick_resume = ark_shutdown,
  163. .set_next_event = ark_set_next_event,
  164. .rating = 300,
  165. };
  166. static struct irqaction ark_timer_irq = {
  167. .name = "timer",
  168. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  169. .handler = ark_timer_interrupt,
  170. .dev_id = &ark_clockevent,
  171. };
  172. int __init ark_clockevents_init(void __iomem *base, unsigned int irq, struct clk *clk, const char *name)
  173. {
  174. struct clock_event_device *evt = &ark_clockevent;
  175. long rate;
  176. rate = clk_get_rate(clk);
  177. if (rate == 0)
  178. return -EINVAL;
  179. clkevt_base = base;
  180. clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
  181. #ifdef USE_SIBLING_TIMER
  182. usec_reload = DIV_ROUND_CLOSEST(rate, 1000000);
  183. #endif
  184. evt->name = name;
  185. evt->irq = irq;
  186. evt->cpumask = cpu_possible_mask;
  187. writel(0, base + TIMER_TCTL);
  188. setup_irq(irq, &ark_timer_irq);
  189. clockevents_config_and_register(evt, rate, 0xf, TIMER_CNT_VAL);
  190. return 0;
  191. }
  192. #ifdef USE_SIBLING_TIMER
  193. static irqreturn_t ark_sibling_timer_interrupt(int irq, void *dev_id)
  194. {
  195. u32 ctrl, mod, cnt;
  196. writel(0, clkevt_base + TIMER2_BASE + TIMER_TCTL);
  197. //restart clockevent timer
  198. ctrl = readl(clkevt_base + TIMER_TCTL);
  199. mod = readl(clkevt_base + TIMER_TMOD);
  200. cnt = readl(clkevt_base + TIMER_TCNT);
  201. if (ctrl == (TIMER_INT_MAINTAIN | TIMER_CTRL_IE | TIMER_CTRL_ENABLE) &&
  202. mod != 0 && cnt == 0) {
  203. writel(0, clkevt_base + TIMER_TCTL);
  204. writel(10, clkevt_base + TIMER_TMOD);
  205. writel(ctrl, clkevt_base + TIMER_TCTL);
  206. }
  207. return IRQ_HANDLED;
  208. }
  209. static struct irqaction ark_sibling_timer_irq = {
  210. .name = "sibling-timer",
  211. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  212. .handler = ark_sibling_timer_interrupt,
  213. };
  214. #endif
  215. static int __init ark_timer_init(struct device_node *np)
  216. {
  217. static bool initialized = false;
  218. void __iomem *base;
  219. int irq, ret = -EINVAL;
  220. struct clk *clk;
  221. const char *name = of_get_property(np, "compatible", NULL);
  222. base = of_iomap(np, 0);
  223. if (!base)
  224. return -ENXIO;
  225. /* Ensure timers are disabled */
  226. writel(0, base + TIMER_TCTL);
  227. writel(0, base + TIMER1_BASE + TIMER_TCTL);
  228. //reset clk prescale
  229. writel(0, base + TIMER_TPRS);
  230. writel(0, base + TIMER1_BASE + TIMER_TPRS);
  231. if (initialized || !of_device_is_available(np)) {
  232. ret = -EINVAL;
  233. goto err;
  234. }
  235. clk = of_clk_get(np, 0);
  236. if (IS_ERR(clk))
  237. goto err;
  238. irq = irq_of_parse_and_map(np, 0);
  239. if (irq <= 0)
  240. goto err;
  241. ret = ark_clockevents_init(base, irq, clk , name);
  242. if (ret)
  243. goto err;
  244. ret = ark_clocksource_and_sched_clock_init(base + TIMER1_BASE,
  245. name, clk, 1);
  246. if (ret)
  247. goto err;
  248. #ifdef USE_SIBLING_TIMER
  249. /* trigger a timer to detect clockevent timer error */
  250. writel(0, base + TIMER2_BASE + TIMER_TCTL);
  251. writel(0, base + TIMER2_BASE + TIMER_TPRS);
  252. irq = irq_of_parse_and_map(np, 1);
  253. setup_irq(irq, &ark_sibling_timer_irq);
  254. #endif
  255. initialized = true;
  256. return 0;
  257. err:
  258. iounmap(base);
  259. return ret;
  260. }
  261. TIMER_OF_DECLARE(arkmicro_timer, "arkmicro,ark-timer", ark_timer_init);
  262. MODULE_AUTHOR("Sim");
  263. MODULE_DESCRIPTION("Arkmicro timer driver");
  264. MODULE_LICENSE("GPL v2");