timer-sp804.c 8.7 KB

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