ingenic-timer.c 10 KB

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