arc_timer.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1, Each can be
  10. * programmed to go from @count to @limit and optionally interrupt.
  11. * We've designated TIMER0 for clockevents and TIMER1 for clocksource
  12. *
  13. * ARCv2 based HS38 cores have RTC (in-core) and GFRC (inside ARConnect/MCIP)
  14. * which are suitable for UP and SMP based clocksources respectively
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/clocksource.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/cpu.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <soc/arc/timers.h>
  25. #include <soc/arc/mcip.h>
  26. static unsigned long arc_timer_freq;
  27. static int noinline arc_get_timer_clk(struct device_node *node)
  28. {
  29. struct clk *clk;
  30. int ret;
  31. clk = of_clk_get(node, 0);
  32. if (IS_ERR(clk)) {
  33. pr_err("timer missing clk");
  34. return PTR_ERR(clk);
  35. }
  36. ret = clk_prepare_enable(clk);
  37. if (ret) {
  38. pr_err("Couldn't enable parent clk\n");
  39. return ret;
  40. }
  41. arc_timer_freq = clk_get_rate(clk);
  42. return 0;
  43. }
  44. /********** Clock Source Device *********/
  45. #ifdef CONFIG_ARC_TIMERS_64BIT
  46. static cycle_t arc_read_gfrc(struct clocksource *cs)
  47. {
  48. unsigned long flags;
  49. u32 l, h;
  50. local_irq_save(flags);
  51. __mcip_cmd(CMD_GFRC_READ_LO, 0);
  52. l = read_aux_reg(ARC_REG_MCIP_READBACK);
  53. __mcip_cmd(CMD_GFRC_READ_HI, 0);
  54. h = read_aux_reg(ARC_REG_MCIP_READBACK);
  55. local_irq_restore(flags);
  56. return (((cycle_t)h) << 32) | l;
  57. }
  58. static struct clocksource arc_counter_gfrc = {
  59. .name = "ARConnect GFRC",
  60. .rating = 400,
  61. .read = arc_read_gfrc,
  62. .mask = CLOCKSOURCE_MASK(64),
  63. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  64. };
  65. static int __init arc_cs_setup_gfrc(struct device_node *node)
  66. {
  67. struct mcip_bcr mp;
  68. int ret;
  69. READ_BCR(ARC_REG_MCIP_BCR, mp);
  70. if (!mp.gfrc) {
  71. pr_warn("Global-64-bit-Ctr clocksource not detected");
  72. return -ENXIO;
  73. }
  74. ret = arc_get_timer_clk(node);
  75. if (ret)
  76. return ret;
  77. return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
  78. }
  79. CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
  80. #define AUX_RTC_CTRL 0x103
  81. #define AUX_RTC_LOW 0x104
  82. #define AUX_RTC_HIGH 0x105
  83. static cycle_t arc_read_rtc(struct clocksource *cs)
  84. {
  85. unsigned long status;
  86. u32 l, h;
  87. /*
  88. * hardware has an internal state machine which tracks readout of
  89. * low/high and updates the CTRL.status if
  90. * - interrupt/exception taken between the two reads
  91. * - high increments after low has been read
  92. */
  93. do {
  94. l = read_aux_reg(AUX_RTC_LOW);
  95. h = read_aux_reg(AUX_RTC_HIGH);
  96. status = read_aux_reg(AUX_RTC_CTRL);
  97. } while (!(status & _BITUL(31)));
  98. return (((cycle_t)h) << 32) | l;
  99. }
  100. static struct clocksource arc_counter_rtc = {
  101. .name = "ARCv2 RTC",
  102. .rating = 350,
  103. .read = arc_read_rtc,
  104. .mask = CLOCKSOURCE_MASK(64),
  105. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  106. };
  107. static int __init arc_cs_setup_rtc(struct device_node *node)
  108. {
  109. struct bcr_timer timer;
  110. int ret;
  111. READ_BCR(ARC_REG_TIMERS_BCR, timer);
  112. if (!timer.rtc) {
  113. pr_warn("Local-64-bit-Ctr clocksource not detected");
  114. return -ENXIO;
  115. }
  116. /* Local to CPU hence not usable in SMP */
  117. if (IS_ENABLED(CONFIG_SMP)) {
  118. pr_warn("Local-64-bit-Ctr not usable in SMP");
  119. return -EINVAL;
  120. }
  121. ret = arc_get_timer_clk(node);
  122. if (ret)
  123. return ret;
  124. write_aux_reg(AUX_RTC_CTRL, 1);
  125. return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
  126. }
  127. CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
  128. #endif
  129. /*
  130. * 32bit TIMER1 to keep counting monotonically and wraparound
  131. */
  132. static cycle_t arc_read_timer1(struct clocksource *cs)
  133. {
  134. return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
  135. }
  136. static struct clocksource arc_counter_timer1 = {
  137. .name = "ARC Timer1",
  138. .rating = 300,
  139. .read = arc_read_timer1,
  140. .mask = CLOCKSOURCE_MASK(32),
  141. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  142. };
  143. static int __init arc_cs_setup_timer1(struct device_node *node)
  144. {
  145. int ret;
  146. /* Local to CPU hence not usable in SMP */
  147. if (IS_ENABLED(CONFIG_SMP))
  148. return -EINVAL;
  149. ret = arc_get_timer_clk(node);
  150. if (ret)
  151. return ret;
  152. write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
  153. write_aux_reg(ARC_REG_TIMER1_CNT, 0);
  154. write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
  155. return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
  156. }
  157. /********** Clock Event Device *********/
  158. static int arc_timer_irq;
  159. /*
  160. * Arm the timer to interrupt after @cycles
  161. * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
  162. */
  163. static void arc_timer_event_setup(unsigned int cycles)
  164. {
  165. write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
  166. write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
  167. write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
  168. }
  169. static int arc_clkevent_set_next_event(unsigned long delta,
  170. struct clock_event_device *dev)
  171. {
  172. arc_timer_event_setup(delta);
  173. return 0;
  174. }
  175. static int arc_clkevent_set_periodic(struct clock_event_device *dev)
  176. {
  177. /*
  178. * At X Hz, 1 sec = 1000ms -> X cycles;
  179. * 10ms -> X / 100 cycles
  180. */
  181. arc_timer_event_setup(arc_timer_freq / HZ);
  182. return 0;
  183. }
  184. static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
  185. .name = "ARC Timer0",
  186. .features = CLOCK_EVT_FEAT_ONESHOT |
  187. CLOCK_EVT_FEAT_PERIODIC,
  188. .rating = 300,
  189. .set_next_event = arc_clkevent_set_next_event,
  190. .set_state_periodic = arc_clkevent_set_periodic,
  191. };
  192. static irqreturn_t timer_irq_handler(int irq, void *dev_id)
  193. {
  194. /*
  195. * Note that generic IRQ core could have passed @evt for @dev_id if
  196. * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
  197. */
  198. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  199. int irq_reenable = clockevent_state_periodic(evt);
  200. /*
  201. * Any write to CTRL reg ACks the interrupt, we rewrite the
  202. * Count when [N]ot [H]alted bit.
  203. * And re-arm it if perioid by [I]nterrupt [E]nable bit
  204. */
  205. write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
  206. evt->event_handler(evt);
  207. return IRQ_HANDLED;
  208. }
  209. static int arc_timer_starting_cpu(unsigned int cpu)
  210. {
  211. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  212. evt->cpumask = cpumask_of(smp_processor_id());
  213. clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
  214. enable_percpu_irq(arc_timer_irq, 0);
  215. return 0;
  216. }
  217. static int arc_timer_dying_cpu(unsigned int cpu)
  218. {
  219. disable_percpu_irq(arc_timer_irq);
  220. return 0;
  221. }
  222. /*
  223. * clockevent setup for boot CPU
  224. */
  225. static int __init arc_clockevent_setup(struct device_node *node)
  226. {
  227. struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
  228. int ret;
  229. arc_timer_irq = irq_of_parse_and_map(node, 0);
  230. if (arc_timer_irq <= 0) {
  231. pr_err("clockevent: missing irq");
  232. return -EINVAL;
  233. }
  234. ret = arc_get_timer_clk(node);
  235. if (ret) {
  236. pr_err("clockevent: missing clk");
  237. return ret;
  238. }
  239. /* Needs apriori irq_set_percpu_devid() done in intc map function */
  240. ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
  241. "Timer0 (per-cpu-tick)", evt);
  242. if (ret) {
  243. pr_err("clockevent: unable to request irq\n");
  244. return ret;
  245. }
  246. ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
  247. "AP_ARC_TIMER_STARTING",
  248. arc_timer_starting_cpu,
  249. arc_timer_dying_cpu);
  250. if (ret) {
  251. pr_err("Failed to setup hotplug state");
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. static int __init arc_of_timer_init(struct device_node *np)
  257. {
  258. static int init_count = 0;
  259. int ret;
  260. if (!init_count) {
  261. init_count = 1;
  262. ret = arc_clockevent_setup(np);
  263. } else {
  264. ret = arc_cs_setup_timer1(np);
  265. }
  266. return ret;
  267. }
  268. CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);