timer-sp804.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/clocksource/timer-sp.c
  4. *
  5. * Copyright (C) 1999 - 2003 ARM Limited
  6. * Copyright (C) 2000 Deep Blue Solutions Ltd
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/clk.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_clk.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/sched_clock.h>
  21. #ifdef CONFIG_ARM
  22. #include <linux/delay.h>
  23. #endif
  24. #include "timer-sp.h"
  25. /* Hisilicon 64-bit timer(a variant of ARM SP804) */
  26. #define HISI_TIMER_1_BASE 0x00
  27. #define HISI_TIMER_2_BASE 0x40
  28. #define HISI_TIMER_LOAD 0x00
  29. #define HISI_TIMER_LOAD_H 0x04
  30. #define HISI_TIMER_VALUE 0x08
  31. #define HISI_TIMER_VALUE_H 0x0c
  32. #define HISI_TIMER_CTRL 0x10
  33. #define HISI_TIMER_INTCLR 0x14
  34. #define HISI_TIMER_RIS 0x18
  35. #define HISI_TIMER_MIS 0x1c
  36. #define HISI_TIMER_BGLOAD 0x20
  37. #define HISI_TIMER_BGLOAD_H 0x24
  38. static struct sp804_timer arm_sp804_timer __initdata = {
  39. .load = TIMER_LOAD,
  40. .value = TIMER_VALUE,
  41. .ctrl = TIMER_CTRL,
  42. .intclr = TIMER_INTCLR,
  43. .timer_base = {TIMER_1_BASE, TIMER_2_BASE},
  44. .width = 32,
  45. };
  46. static struct sp804_timer hisi_sp804_timer __initdata = {
  47. .load = HISI_TIMER_LOAD,
  48. .load_h = HISI_TIMER_LOAD_H,
  49. .value = HISI_TIMER_VALUE,
  50. .value_h = HISI_TIMER_VALUE_H,
  51. .ctrl = HISI_TIMER_CTRL,
  52. .intclr = HISI_TIMER_INTCLR,
  53. .timer_base = {HISI_TIMER_1_BASE, HISI_TIMER_2_BASE},
  54. .width = 64,
  55. };
  56. static struct sp804_clkevt sp804_clkevt[NR_TIMERS];
  57. static long __init sp804_get_clock_rate(struct clk *clk, const char *name)
  58. {
  59. int err;
  60. if (!clk)
  61. clk = clk_get_sys("sp804", name);
  62. if (IS_ERR(clk)) {
  63. pr_err("%s clock not found: %ld\n", name, PTR_ERR(clk));
  64. return PTR_ERR(clk);
  65. }
  66. err = clk_prepare_enable(clk);
  67. if (err) {
  68. pr_err("clock failed to enable: %d\n", err);
  69. clk_put(clk);
  70. return err;
  71. }
  72. return clk_get_rate(clk);
  73. }
  74. static struct sp804_clkevt * __init sp804_clkevt_get(void __iomem *base)
  75. {
  76. int i;
  77. for (i = 0; i < NR_TIMERS; i++) {
  78. if (sp804_clkevt[i].base == base)
  79. return &sp804_clkevt[i];
  80. }
  81. /* It's impossible to reach here */
  82. WARN_ON(1);
  83. return NULL;
  84. }
  85. static struct sp804_clkevt *sched_clkevt;
  86. static u64 notrace sp804_read(void)
  87. {
  88. return ~readl_relaxed(sched_clkevt->value);
  89. }
  90. /* Register delay timer backed by the hardware counter */
  91. #ifdef CONFIG_ARM
  92. static struct delay_timer delay;
  93. static struct sp804_clkevt *delay_clkevt;
  94. static unsigned long sp804_read_delay_timer_read(void)
  95. {
  96. return ~readl_relaxed(delay_clkevt->value);
  97. }
  98. static void sp804_register_delay_timer(struct sp804_clkevt *clk, int freq)
  99. {
  100. delay_clkevt = clk;
  101. delay.freq = freq;
  102. delay.read_current_timer = sp804_read_delay_timer_read;
  103. register_current_timer_delay(&delay);
  104. }
  105. #else
  106. static inline void sp804_register_delay_timer(struct sp804_clkevt *clk, int freq) {}
  107. #endif
  108. static int __init sp804_clocksource_and_sched_clock_init(void __iomem *base,
  109. const char *name,
  110. struct clk *clk,
  111. int use_sched_clock)
  112. {
  113. long rate;
  114. struct sp804_clkevt *clkevt;
  115. rate = sp804_get_clock_rate(clk, name);
  116. if (rate < 0)
  117. return -EINVAL;
  118. clkevt = sp804_clkevt_get(base);
  119. writel(0, clkevt->ctrl);
  120. writel(0xffffffff, clkevt->load);
  121. writel(0xffffffff, clkevt->value);
  122. if (clkevt->width == 64) {
  123. writel(0xffffffff, clkevt->load_h);
  124. writel(0xffffffff, clkevt->value_h);
  125. }
  126. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  127. clkevt->ctrl);
  128. clocksource_mmio_init(clkevt->value, name,
  129. rate, 200, 32, clocksource_mmio_readl_down);
  130. sp804_register_delay_timer(clkevt, rate);
  131. if (use_sched_clock) {
  132. sched_clkevt = clkevt;
  133. sched_clock_register(sp804_read, 32, rate);
  134. }
  135. return 0;
  136. }
  137. static struct sp804_clkevt *common_clkevt;
  138. /*
  139. * IRQ handler for the timer
  140. */
  141. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  142. {
  143. struct clock_event_device *evt = dev_id;
  144. /* clear the interrupt */
  145. writel(1, common_clkevt->intclr);
  146. evt->event_handler(evt);
  147. return IRQ_HANDLED;
  148. }
  149. static inline void evt_timer_shutdown(struct clock_event_device *evt)
  150. {
  151. writel(0, common_clkevt->ctrl);
  152. }
  153. static int sp804_shutdown(struct clock_event_device *evt)
  154. {
  155. evt_timer_shutdown(evt);
  156. return 0;
  157. }
  158. static int sp804_set_periodic(struct clock_event_device *evt)
  159. {
  160. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  161. TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  162. evt_timer_shutdown(evt);
  163. writel(common_clkevt->reload, common_clkevt->load);
  164. writel(ctrl, common_clkevt->ctrl);
  165. return 0;
  166. }
  167. static int sp804_set_next_event(unsigned long next,
  168. struct clock_event_device *evt)
  169. {
  170. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  171. TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
  172. writel(next, common_clkevt->load);
  173. writel(ctrl, common_clkevt->ctrl);
  174. return 0;
  175. }
  176. static struct clock_event_device sp804_clockevent = {
  177. .features = CLOCK_EVT_FEAT_PERIODIC |
  178. CLOCK_EVT_FEAT_ONESHOT |
  179. CLOCK_EVT_FEAT_DYNIRQ,
  180. .set_state_shutdown = sp804_shutdown,
  181. .set_state_periodic = sp804_set_periodic,
  182. .set_state_oneshot = sp804_shutdown,
  183. .tick_resume = sp804_shutdown,
  184. .set_next_event = sp804_set_next_event,
  185. .rating = 300,
  186. };
  187. static int __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
  188. struct clk *clk, const char *name)
  189. {
  190. struct clock_event_device *evt = &sp804_clockevent;
  191. long rate;
  192. rate = sp804_get_clock_rate(clk, name);
  193. if (rate < 0)
  194. return -EINVAL;
  195. common_clkevt = sp804_clkevt_get(base);
  196. common_clkevt->reload = DIV_ROUND_CLOSEST(rate, HZ);
  197. evt->name = name;
  198. evt->irq = irq;
  199. evt->cpumask = cpu_possible_mask;
  200. writel(0, common_clkevt->ctrl);
  201. if (request_irq(irq, sp804_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
  202. "timer", &sp804_clockevent))
  203. pr_err("request_irq() failed\n");
  204. clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
  205. return 0;
  206. }
  207. static void __init sp804_clkevt_init(struct sp804_timer *timer, void __iomem *base)
  208. {
  209. int i;
  210. for (i = 0; i < NR_TIMERS; i++) {
  211. void __iomem *timer_base;
  212. struct sp804_clkevt *clkevt;
  213. timer_base = base + timer->timer_base[i];
  214. clkevt = &sp804_clkevt[i];
  215. clkevt->base = timer_base;
  216. clkevt->load = timer_base + timer->load;
  217. clkevt->load_h = timer_base + timer->load_h;
  218. clkevt->value = timer_base + timer->value;
  219. clkevt->value_h = timer_base + timer->value_h;
  220. clkevt->ctrl = timer_base + timer->ctrl;
  221. clkevt->intclr = timer_base + timer->intclr;
  222. clkevt->width = timer->width;
  223. }
  224. }
  225. static int __init sp804_of_init(struct device_node *np, struct sp804_timer *timer)
  226. {
  227. static bool initialized = false;
  228. void __iomem *base;
  229. void __iomem *timer1_base;
  230. void __iomem *timer2_base;
  231. int irq, ret = -EINVAL;
  232. u32 irq_num = 0;
  233. struct clk *clk1, *clk2;
  234. const char *name = of_get_property(np, "compatible", NULL);
  235. if (initialized) {
  236. pr_debug("%pOF: skipping further SP804 timer device\n", np);
  237. return 0;
  238. }
  239. base = of_iomap(np, 0);
  240. if (!base)
  241. return -ENXIO;
  242. timer1_base = base + timer->timer_base[0];
  243. timer2_base = base + timer->timer_base[1];
  244. /* Ensure timers are disabled */
  245. writel(0, timer1_base + timer->ctrl);
  246. writel(0, timer2_base + timer->ctrl);
  247. clk1 = of_clk_get(np, 0);
  248. if (IS_ERR(clk1))
  249. clk1 = NULL;
  250. /* Get the 2nd clock if the timer has 3 timer clocks */
  251. if (of_clk_get_parent_count(np) == 3) {
  252. clk2 = of_clk_get(np, 1);
  253. if (IS_ERR(clk2)) {
  254. pr_err("%pOFn clock not found: %d\n", np,
  255. (int)PTR_ERR(clk2));
  256. clk2 = NULL;
  257. }
  258. } else
  259. clk2 = clk1;
  260. irq = irq_of_parse_and_map(np, 0);
  261. if (irq <= 0)
  262. goto err;
  263. sp804_clkevt_init(timer, base);
  264. of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
  265. if (irq_num == 2) {
  266. ret = sp804_clockevents_init(timer2_base, irq, clk2, name);
  267. if (ret)
  268. goto err;
  269. ret = sp804_clocksource_and_sched_clock_init(timer1_base,
  270. name, clk1, 1);
  271. if (ret)
  272. goto err;
  273. } else {
  274. ret = sp804_clockevents_init(timer1_base, irq, clk1, name);
  275. if (ret)
  276. goto err;
  277. ret = sp804_clocksource_and_sched_clock_init(timer2_base,
  278. name, clk2, 1);
  279. if (ret)
  280. goto err;
  281. }
  282. initialized = true;
  283. return 0;
  284. err:
  285. iounmap(base);
  286. return ret;
  287. }
  288. static int __init arm_sp804_of_init(struct device_node *np)
  289. {
  290. return sp804_of_init(np, &arm_sp804_timer);
  291. }
  292. TIMER_OF_DECLARE(sp804, "arm,sp804", arm_sp804_of_init);
  293. static int __init hisi_sp804_of_init(struct device_node *np)
  294. {
  295. return sp804_of_init(np, &hisi_sp804_timer);
  296. }
  297. TIMER_OF_DECLARE(hisi_sp804, "hisilicon,sp804", hisi_sp804_of_init);
  298. static int __init integrator_cp_of_init(struct device_node *np)
  299. {
  300. static int init_count = 0;
  301. void __iomem *base;
  302. int irq, ret = -EINVAL;
  303. const char *name = of_get_property(np, "compatible", NULL);
  304. struct clk *clk;
  305. base = of_iomap(np, 0);
  306. if (!base) {
  307. pr_err("Failed to iomap\n");
  308. return -ENXIO;
  309. }
  310. clk = of_clk_get(np, 0);
  311. if (IS_ERR(clk)) {
  312. pr_err("Failed to get clock\n");
  313. return PTR_ERR(clk);
  314. }
  315. /* Ensure timer is disabled */
  316. writel(0, base + arm_sp804_timer.ctrl);
  317. if (init_count == 2 || !of_device_is_available(np))
  318. goto err;
  319. sp804_clkevt_init(&arm_sp804_timer, base);
  320. if (!init_count) {
  321. ret = sp804_clocksource_and_sched_clock_init(base,
  322. name, clk, 0);
  323. if (ret)
  324. goto err;
  325. } else {
  326. irq = irq_of_parse_and_map(np, 0);
  327. if (irq <= 0)
  328. goto err;
  329. ret = sp804_clockevents_init(base, irq, clk, name);
  330. if (ret)
  331. goto err;
  332. }
  333. init_count++;
  334. return 0;
  335. err:
  336. iounmap(base);
  337. return ret;
  338. }
  339. TIMER_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);