ingenic-timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ingenic SoCs TCU IRQ driver
  4. * Copyright (C) 2019 Paul Cercueil <[email protected]>
  5. * Copyright (C) 2020 周琰杰 (Zhou Yanjie) <[email protected]>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/clockchips.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mfd/ingenic-tcu.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/overflow.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/sched_clock.h>
  22. #include <dt-bindings/clock/ingenic,tcu.h>
  23. static DEFINE_PER_CPU(call_single_data_t, ingenic_cevt_csd);
  24. struct ingenic_soc_info {
  25. unsigned int num_channels;
  26. };
  27. struct ingenic_tcu_timer {
  28. unsigned int cpu;
  29. unsigned int channel;
  30. struct clock_event_device cevt;
  31. struct clk *clk;
  32. char name[8];
  33. };
  34. struct ingenic_tcu {
  35. struct regmap *map;
  36. struct device_node *np;
  37. struct clk *cs_clk;
  38. unsigned int cs_channel;
  39. struct clocksource cs;
  40. unsigned long pwm_channels_mask;
  41. struct ingenic_tcu_timer timers[];
  42. };
  43. static struct ingenic_tcu *ingenic_tcu;
  44. static u64 notrace ingenic_tcu_timer_read(void)
  45. {
  46. struct ingenic_tcu *tcu = ingenic_tcu;
  47. unsigned int count;
  48. regmap_read(tcu->map, TCU_REG_TCNTc(tcu->cs_channel), &count);
  49. return count;
  50. }
  51. static u64 notrace ingenic_tcu_timer_cs_read(struct clocksource *cs)
  52. {
  53. return ingenic_tcu_timer_read();
  54. }
  55. static inline struct ingenic_tcu *
  56. to_ingenic_tcu(struct ingenic_tcu_timer *timer)
  57. {
  58. return container_of(timer, struct ingenic_tcu, timers[timer->cpu]);
  59. }
  60. static inline struct ingenic_tcu_timer *
  61. to_ingenic_tcu_timer(struct clock_event_device *evt)
  62. {
  63. return container_of(evt, struct ingenic_tcu_timer, cevt);
  64. }
  65. static int ingenic_tcu_cevt_set_state_shutdown(struct clock_event_device *evt)
  66. {
  67. struct ingenic_tcu_timer *timer = to_ingenic_tcu_timer(evt);
  68. struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
  69. regmap_write(tcu->map, TCU_REG_TECR, BIT(timer->channel));
  70. return 0;
  71. }
  72. static int ingenic_tcu_cevt_set_next(unsigned long next,
  73. struct clock_event_device *evt)
  74. {
  75. struct ingenic_tcu_timer *timer = to_ingenic_tcu_timer(evt);
  76. struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
  77. if (next > 0xffff)
  78. return -EINVAL;
  79. regmap_write(tcu->map, TCU_REG_TDFRc(timer->channel), next);
  80. regmap_write(tcu->map, TCU_REG_TCNTc(timer->channel), 0);
  81. regmap_write(tcu->map, TCU_REG_TESR, BIT(timer->channel));
  82. return 0;
  83. }
  84. static void ingenic_per_cpu_event_handler(void *info)
  85. {
  86. struct clock_event_device *cevt = (struct clock_event_device *) info;
  87. cevt->event_handler(cevt);
  88. }
  89. static irqreturn_t ingenic_tcu_cevt_cb(int irq, void *dev_id)
  90. {
  91. struct ingenic_tcu_timer *timer = dev_id;
  92. struct ingenic_tcu *tcu = to_ingenic_tcu(timer);
  93. call_single_data_t *csd;
  94. regmap_write(tcu->map, TCU_REG_TECR, BIT(timer->channel));
  95. if (timer->cevt.event_handler) {
  96. csd = &per_cpu(ingenic_cevt_csd, timer->cpu);
  97. csd->info = (void *) &timer->cevt;
  98. csd->func = ingenic_per_cpu_event_handler;
  99. smp_call_function_single_async(timer->cpu, csd);
  100. }
  101. return IRQ_HANDLED;
  102. }
  103. static struct clk *ingenic_tcu_get_clock(struct device_node *np, int id)
  104. {
  105. struct of_phandle_args args;
  106. args.np = np;
  107. args.args_count = 1;
  108. args.args[0] = id;
  109. return of_clk_get_from_provider(&args);
  110. }
  111. static int ingenic_tcu_setup_cevt(unsigned int cpu)
  112. {
  113. struct ingenic_tcu *tcu = ingenic_tcu;
  114. struct ingenic_tcu_timer *timer = &tcu->timers[cpu];
  115. unsigned int timer_virq;
  116. struct irq_domain *domain;
  117. unsigned long rate;
  118. int err;
  119. timer->clk = ingenic_tcu_get_clock(tcu->np, timer->channel);
  120. if (IS_ERR(timer->clk))
  121. return PTR_ERR(timer->clk);
  122. err = clk_prepare_enable(timer->clk);
  123. if (err)
  124. goto err_clk_put;
  125. rate = clk_get_rate(timer->clk);
  126. if (!rate) {
  127. err = -EINVAL;
  128. goto err_clk_disable;
  129. }
  130. domain = irq_find_host(tcu->np);
  131. if (!domain) {
  132. err = -ENODEV;
  133. goto err_clk_disable;
  134. }
  135. timer_virq = irq_create_mapping(domain, timer->channel);
  136. if (!timer_virq) {
  137. err = -EINVAL;
  138. goto err_clk_disable;
  139. }
  140. snprintf(timer->name, sizeof(timer->name), "TCU%u", timer->channel);
  141. err = request_irq(timer_virq, ingenic_tcu_cevt_cb, IRQF_TIMER,
  142. timer->name, timer);
  143. if (err)
  144. goto err_irq_dispose_mapping;
  145. timer->cpu = smp_processor_id();
  146. timer->cevt.cpumask = cpumask_of(smp_processor_id());
  147. timer->cevt.features = CLOCK_EVT_FEAT_ONESHOT;
  148. timer->cevt.name = timer->name;
  149. timer->cevt.rating = 200;
  150. timer->cevt.set_state_shutdown = ingenic_tcu_cevt_set_state_shutdown;
  151. timer->cevt.set_next_event = ingenic_tcu_cevt_set_next;
  152. clockevents_config_and_register(&timer->cevt, rate, 10, 0xffff);
  153. return 0;
  154. err_irq_dispose_mapping:
  155. irq_dispose_mapping(timer_virq);
  156. err_clk_disable:
  157. clk_disable_unprepare(timer->clk);
  158. err_clk_put:
  159. clk_put(timer->clk);
  160. return err;
  161. }
  162. static int __init ingenic_tcu_clocksource_init(struct device_node *np,
  163. struct ingenic_tcu *tcu)
  164. {
  165. unsigned int channel = tcu->cs_channel;
  166. struct clocksource *cs = &tcu->cs;
  167. unsigned long rate;
  168. int err;
  169. tcu->cs_clk = ingenic_tcu_get_clock(np, channel);
  170. if (IS_ERR(tcu->cs_clk))
  171. return PTR_ERR(tcu->cs_clk);
  172. err = clk_prepare_enable(tcu->cs_clk);
  173. if (err)
  174. goto err_clk_put;
  175. rate = clk_get_rate(tcu->cs_clk);
  176. if (!rate) {
  177. err = -EINVAL;
  178. goto err_clk_disable;
  179. }
  180. /* Reset channel */
  181. regmap_update_bits(tcu->map, TCU_REG_TCSRc(channel),
  182. 0xffff & ~TCU_TCSR_RESERVED_BITS, 0);
  183. /* Reset counter */
  184. regmap_write(tcu->map, TCU_REG_TDFRc(channel), 0xffff);
  185. regmap_write(tcu->map, TCU_REG_TCNTc(channel), 0);
  186. /* Enable channel */
  187. regmap_write(tcu->map, TCU_REG_TESR, BIT(channel));
  188. cs->name = "ingenic-timer";
  189. cs->rating = 200;
  190. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  191. cs->mask = CLOCKSOURCE_MASK(16);
  192. cs->read = ingenic_tcu_timer_cs_read;
  193. err = clocksource_register_hz(cs, rate);
  194. if (err)
  195. goto err_clk_disable;
  196. return 0;
  197. err_clk_disable:
  198. clk_disable_unprepare(tcu->cs_clk);
  199. err_clk_put:
  200. clk_put(tcu->cs_clk);
  201. return err;
  202. }
  203. static const struct ingenic_soc_info jz4740_soc_info = {
  204. .num_channels = 8,
  205. };
  206. static const struct ingenic_soc_info jz4725b_soc_info = {
  207. .num_channels = 6,
  208. };
  209. static const struct of_device_id ingenic_tcu_of_match[] = {
  210. { .compatible = "ingenic,jz4740-tcu", .data = &jz4740_soc_info, },
  211. { .compatible = "ingenic,jz4725b-tcu", .data = &jz4725b_soc_info, },
  212. { .compatible = "ingenic,jz4760-tcu", .data = &jz4740_soc_info, },
  213. { .compatible = "ingenic,jz4770-tcu", .data = &jz4740_soc_info, },
  214. { .compatible = "ingenic,x1000-tcu", .data = &jz4740_soc_info, },
  215. { /* sentinel */ }
  216. };
  217. static int __init ingenic_tcu_init(struct device_node *np)
  218. {
  219. const struct of_device_id *id = of_match_node(ingenic_tcu_of_match, np);
  220. const struct ingenic_soc_info *soc_info = id->data;
  221. struct ingenic_tcu_timer *timer;
  222. struct ingenic_tcu *tcu;
  223. struct regmap *map;
  224. unsigned int cpu;
  225. int ret, last_bit = -1;
  226. long rate;
  227. of_node_clear_flag(np, OF_POPULATED);
  228. map = device_node_to_regmap(np);
  229. if (IS_ERR(map))
  230. return PTR_ERR(map);
  231. tcu = kzalloc(struct_size(tcu, timers, num_possible_cpus()),
  232. GFP_KERNEL);
  233. if (!tcu)
  234. return -ENOMEM;
  235. /*
  236. * Enable all TCU channels for PWM use by default except channels 0/1,
  237. * and channel 2 if target CPU is JZ4780/X2000 and SMP is selected.
  238. */
  239. tcu->pwm_channels_mask = GENMASK(soc_info->num_channels - 1,
  240. num_possible_cpus() + 1);
  241. of_property_read_u32(np, "ingenic,pwm-channels-mask",
  242. (u32 *)&tcu->pwm_channels_mask);
  243. /* Verify that we have at least num_possible_cpus() + 1 free channels */
  244. if (hweight8(tcu->pwm_channels_mask) >
  245. soc_info->num_channels - num_possible_cpus() + 1) {
  246. pr_crit("%s: Invalid PWM channel mask: 0x%02lx\n", __func__,
  247. tcu->pwm_channels_mask);
  248. ret = -EINVAL;
  249. goto err_free_ingenic_tcu;
  250. }
  251. tcu->map = map;
  252. tcu->np = np;
  253. ingenic_tcu = tcu;
  254. for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
  255. timer = &tcu->timers[cpu];
  256. timer->cpu = cpu;
  257. timer->channel = find_next_zero_bit(&tcu->pwm_channels_mask,
  258. soc_info->num_channels,
  259. last_bit + 1);
  260. last_bit = timer->channel;
  261. }
  262. tcu->cs_channel = find_next_zero_bit(&tcu->pwm_channels_mask,
  263. soc_info->num_channels,
  264. last_bit + 1);
  265. ret = ingenic_tcu_clocksource_init(np, tcu);
  266. if (ret) {
  267. pr_crit("%s: Unable to init clocksource: %d\n", __func__, ret);
  268. goto err_free_ingenic_tcu;
  269. }
  270. /* Setup clock events on each CPU core */
  271. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "Ingenic XBurst: online",
  272. ingenic_tcu_setup_cevt, NULL);
  273. if (ret < 0) {
  274. pr_crit("%s: Unable to start CPU timers: %d\n", __func__, ret);
  275. goto err_tcu_clocksource_cleanup;
  276. }
  277. /* Register the sched_clock at the end as there's no way to undo it */
  278. rate = clk_get_rate(tcu->cs_clk);
  279. sched_clock_register(ingenic_tcu_timer_read, 16, rate);
  280. return 0;
  281. err_tcu_clocksource_cleanup:
  282. clocksource_unregister(&tcu->cs);
  283. clk_disable_unprepare(tcu->cs_clk);
  284. clk_put(tcu->cs_clk);
  285. err_free_ingenic_tcu:
  286. kfree(tcu);
  287. return ret;
  288. }
  289. TIMER_OF_DECLARE(jz4740_tcu_intc, "ingenic,jz4740-tcu", ingenic_tcu_init);
  290. TIMER_OF_DECLARE(jz4725b_tcu_intc, "ingenic,jz4725b-tcu", ingenic_tcu_init);
  291. TIMER_OF_DECLARE(jz4760_tcu_intc, "ingenic,jz4760-tcu", ingenic_tcu_init);
  292. TIMER_OF_DECLARE(jz4770_tcu_intc, "ingenic,jz4770-tcu", ingenic_tcu_init);
  293. TIMER_OF_DECLARE(x1000_tcu_intc, "ingenic,x1000-tcu", ingenic_tcu_init);
  294. static int __init ingenic_tcu_probe(struct platform_device *pdev)
  295. {
  296. platform_set_drvdata(pdev, ingenic_tcu);
  297. return 0;
  298. }
  299. static int __maybe_unused ingenic_tcu_suspend(struct device *dev)
  300. {
  301. struct ingenic_tcu *tcu = dev_get_drvdata(dev);
  302. unsigned int cpu;
  303. clk_disable(tcu->cs_clk);
  304. for (cpu = 0; cpu < num_online_cpus(); cpu++)
  305. clk_disable(tcu->timers[cpu].clk);
  306. return 0;
  307. }
  308. static int __maybe_unused ingenic_tcu_resume(struct device *dev)
  309. {
  310. struct ingenic_tcu *tcu = dev_get_drvdata(dev);
  311. unsigned int cpu;
  312. int ret;
  313. for (cpu = 0; cpu < num_online_cpus(); cpu++) {
  314. ret = clk_enable(tcu->timers[cpu].clk);
  315. if (ret)
  316. goto err_timer_clk_disable;
  317. }
  318. ret = clk_enable(tcu->cs_clk);
  319. if (ret)
  320. goto err_timer_clk_disable;
  321. return 0;
  322. err_timer_clk_disable:
  323. for (; cpu > 0; cpu--)
  324. clk_disable(tcu->timers[cpu - 1].clk);
  325. return ret;
  326. }
  327. static const struct dev_pm_ops __maybe_unused ingenic_tcu_pm_ops = {
  328. /* _noirq: We want the TCU clocks to be gated last / ungated first */
  329. .suspend_noirq = ingenic_tcu_suspend,
  330. .resume_noirq = ingenic_tcu_resume,
  331. };
  332. static struct platform_driver ingenic_tcu_driver = {
  333. .driver = {
  334. .name = "ingenic-tcu-timer",
  335. #ifdef CONFIG_PM_SLEEP
  336. .pm = &ingenic_tcu_pm_ops,
  337. #endif
  338. .of_match_table = ingenic_tcu_of_match,
  339. },
  340. };
  341. builtin_platform_driver_probe(ingenic_tcu_driver, ingenic_tcu_probe);