tcu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * JZ47xx SoCs TCU clocks driver
  4. * Copyright (C) 2019 Paul Cercueil <paul@crapouillou.net>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/clockchips.h>
  9. #include <linux/mfd/ingenic-tcu.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/regmap.h>
  12. #include <linux/slab.h>
  13. #include <linux/syscore_ops.h>
  14. #include <dt-bindings/clock/ingenic,tcu.h>
  15. /* 8 channels max + watchdog + OST */
  16. #define TCU_CLK_COUNT 10
  17. #undef pr_fmt
  18. #define pr_fmt(fmt) "ingenic-tcu-clk: " fmt
  19. enum tcu_clk_parent {
  20. TCU_PARENT_PCLK,
  21. TCU_PARENT_RTC,
  22. TCU_PARENT_EXT,
  23. };
  24. struct ingenic_soc_info {
  25. unsigned int num_channels;
  26. bool has_ost;
  27. bool has_tcu_clk;
  28. bool allow_missing_tcu_clk;
  29. };
  30. struct ingenic_tcu_clk_info {
  31. struct clk_init_data init_data;
  32. u8 gate_bit;
  33. u8 tcsr_reg;
  34. };
  35. struct ingenic_tcu_clk {
  36. struct clk_hw hw;
  37. unsigned int idx;
  38. struct ingenic_tcu *tcu;
  39. const struct ingenic_tcu_clk_info *info;
  40. };
  41. struct ingenic_tcu {
  42. const struct ingenic_soc_info *soc_info;
  43. struct regmap *map;
  44. struct clk *clk;
  45. struct clk_hw_onecell_data *clocks;
  46. };
  47. static struct ingenic_tcu *ingenic_tcu;
  48. static inline struct ingenic_tcu_clk *to_tcu_clk(struct clk_hw *hw)
  49. {
  50. return container_of(hw, struct ingenic_tcu_clk, hw);
  51. }
  52. static int ingenic_tcu_enable(struct clk_hw *hw)
  53. {
  54. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  55. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  56. struct ingenic_tcu *tcu = tcu_clk->tcu;
  57. regmap_write(tcu->map, TCU_REG_TSCR, BIT(info->gate_bit));
  58. return 0;
  59. }
  60. static void ingenic_tcu_disable(struct clk_hw *hw)
  61. {
  62. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  63. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  64. struct ingenic_tcu *tcu = tcu_clk->tcu;
  65. regmap_write(tcu->map, TCU_REG_TSSR, BIT(info->gate_bit));
  66. }
  67. static int ingenic_tcu_is_enabled(struct clk_hw *hw)
  68. {
  69. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  70. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  71. unsigned int value;
  72. regmap_read(tcu_clk->tcu->map, TCU_REG_TSR, &value);
  73. return !(value & BIT(info->gate_bit));
  74. }
  75. static bool ingenic_tcu_enable_regs(struct clk_hw *hw)
  76. {
  77. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  78. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  79. struct ingenic_tcu *tcu = tcu_clk->tcu;
  80. bool enabled = false;
  81. /*
  82. * According to the programming manual, a timer channel's registers can
  83. * only be accessed when the channel's stop bit is clear.
  84. */
  85. enabled = !!ingenic_tcu_is_enabled(hw);
  86. regmap_write(tcu->map, TCU_REG_TSCR, BIT(info->gate_bit));
  87. return enabled;
  88. }
  89. static void ingenic_tcu_disable_regs(struct clk_hw *hw)
  90. {
  91. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  92. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  93. struct ingenic_tcu *tcu = tcu_clk->tcu;
  94. regmap_write(tcu->map, TCU_REG_TSSR, BIT(info->gate_bit));
  95. }
  96. static u8 ingenic_tcu_get_parent(struct clk_hw *hw)
  97. {
  98. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  99. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  100. unsigned int val = 0;
  101. int ret;
  102. ret = regmap_read(tcu_clk->tcu->map, info->tcsr_reg, &val);
  103. WARN_ONCE(ret < 0, "Unable to read TCSR %d", tcu_clk->idx);
  104. return ffs(val & TCU_TCSR_PARENT_CLOCK_MASK) - 1;
  105. }
  106. static int ingenic_tcu_set_parent(struct clk_hw *hw, u8 idx)
  107. {
  108. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  109. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  110. bool was_enabled;
  111. int ret;
  112. was_enabled = ingenic_tcu_enable_regs(hw);
  113. ret = regmap_update_bits(tcu_clk->tcu->map, info->tcsr_reg,
  114. TCU_TCSR_PARENT_CLOCK_MASK, BIT(idx));
  115. WARN_ONCE(ret < 0, "Unable to update TCSR %d", tcu_clk->idx);
  116. if (!was_enabled)
  117. ingenic_tcu_disable_regs(hw);
  118. return 0;
  119. }
  120. static unsigned long ingenic_tcu_recalc_rate(struct clk_hw *hw,
  121. unsigned long parent_rate)
  122. {
  123. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  124. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  125. unsigned int prescale;
  126. int ret;
  127. ret = regmap_read(tcu_clk->tcu->map, info->tcsr_reg, &prescale);
  128. WARN_ONCE(ret < 0, "Unable to read TCSR %d", tcu_clk->idx);
  129. prescale = (prescale & TCU_TCSR_PRESCALE_MASK) >> TCU_TCSR_PRESCALE_LSB;
  130. return parent_rate >> (prescale * 2);
  131. }
  132. static u8 ingenic_tcu_get_prescale(unsigned long rate, unsigned long req_rate)
  133. {
  134. u8 prescale;
  135. for (prescale = 0; prescale < 5; prescale++)
  136. if ((rate >> (prescale * 2)) <= req_rate)
  137. return prescale;
  138. return 5; /* /1024 divider */
  139. }
  140. static int ingenic_tcu_determine_rate(struct clk_hw *hw,
  141. struct clk_rate_request *req)
  142. {
  143. unsigned long rate = req->best_parent_rate;
  144. u8 prescale;
  145. if (req->rate > rate) {
  146. req->rate = rate;
  147. return 0;
  148. }
  149. prescale = ingenic_tcu_get_prescale(rate, req->rate);
  150. req->rate = rate >> (prescale * 2);
  151. return 0;
  152. }
  153. static int ingenic_tcu_set_rate(struct clk_hw *hw, unsigned long req_rate,
  154. unsigned long parent_rate)
  155. {
  156. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  157. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  158. u8 prescale = ingenic_tcu_get_prescale(parent_rate, req_rate);
  159. bool was_enabled;
  160. int ret;
  161. was_enabled = ingenic_tcu_enable_regs(hw);
  162. ret = regmap_update_bits(tcu_clk->tcu->map, info->tcsr_reg,
  163. TCU_TCSR_PRESCALE_MASK,
  164. prescale << TCU_TCSR_PRESCALE_LSB);
  165. WARN_ONCE(ret < 0, "Unable to update TCSR %d", tcu_clk->idx);
  166. if (!was_enabled)
  167. ingenic_tcu_disable_regs(hw);
  168. return 0;
  169. }
  170. static const struct clk_ops ingenic_tcu_clk_ops = {
  171. .get_parent = ingenic_tcu_get_parent,
  172. .set_parent = ingenic_tcu_set_parent,
  173. .recalc_rate = ingenic_tcu_recalc_rate,
  174. .determine_rate = ingenic_tcu_determine_rate,
  175. .set_rate = ingenic_tcu_set_rate,
  176. .enable = ingenic_tcu_enable,
  177. .disable = ingenic_tcu_disable,
  178. .is_enabled = ingenic_tcu_is_enabled,
  179. };
  180. static const char * const ingenic_tcu_timer_parents[] = {
  181. [TCU_PARENT_PCLK] = "pclk",
  182. [TCU_PARENT_RTC] = "rtc",
  183. [TCU_PARENT_EXT] = "ext",
  184. };
  185. #define DEF_TIMER(_name, _gate_bit, _tcsr) \
  186. { \
  187. .init_data = { \
  188. .name = _name, \
  189. .parent_names = ingenic_tcu_timer_parents, \
  190. .num_parents = ARRAY_SIZE(ingenic_tcu_timer_parents),\
  191. .ops = &ingenic_tcu_clk_ops, \
  192. .flags = CLK_SET_RATE_UNGATE, \
  193. }, \
  194. .gate_bit = _gate_bit, \
  195. .tcsr_reg = _tcsr, \
  196. }
  197. static const struct ingenic_tcu_clk_info ingenic_tcu_clk_info[] = {
  198. [TCU_CLK_TIMER0] = DEF_TIMER("timer0", 0, TCU_REG_TCSRc(0)),
  199. [TCU_CLK_TIMER1] = DEF_TIMER("timer1", 1, TCU_REG_TCSRc(1)),
  200. [TCU_CLK_TIMER2] = DEF_TIMER("timer2", 2, TCU_REG_TCSRc(2)),
  201. [TCU_CLK_TIMER3] = DEF_TIMER("timer3", 3, TCU_REG_TCSRc(3)),
  202. [TCU_CLK_TIMER4] = DEF_TIMER("timer4", 4, TCU_REG_TCSRc(4)),
  203. [TCU_CLK_TIMER5] = DEF_TIMER("timer5", 5, TCU_REG_TCSRc(5)),
  204. [TCU_CLK_TIMER6] = DEF_TIMER("timer6", 6, TCU_REG_TCSRc(6)),
  205. [TCU_CLK_TIMER7] = DEF_TIMER("timer7", 7, TCU_REG_TCSRc(7)),
  206. };
  207. static const struct ingenic_tcu_clk_info ingenic_tcu_watchdog_clk_info =
  208. DEF_TIMER("wdt", 16, TCU_REG_WDT_TCSR);
  209. static const struct ingenic_tcu_clk_info ingenic_tcu_ost_clk_info =
  210. DEF_TIMER("ost", 15, TCU_REG_OST_TCSR);
  211. #undef DEF_TIMER
  212. static int __init ingenic_tcu_register_clock(struct ingenic_tcu *tcu,
  213. unsigned int idx, enum tcu_clk_parent parent,
  214. const struct ingenic_tcu_clk_info *info,
  215. struct clk_hw_onecell_data *clocks)
  216. {
  217. struct ingenic_tcu_clk *tcu_clk;
  218. int err;
  219. tcu_clk = kzalloc(sizeof(*tcu_clk), GFP_KERNEL);
  220. if (!tcu_clk)
  221. return -ENOMEM;
  222. tcu_clk->hw.init = &info->init_data;
  223. tcu_clk->idx = idx;
  224. tcu_clk->info = info;
  225. tcu_clk->tcu = tcu;
  226. /* Reset channel and clock divider, set default parent */
  227. ingenic_tcu_enable_regs(&tcu_clk->hw);
  228. regmap_update_bits(tcu->map, info->tcsr_reg, 0xffff, BIT(parent));
  229. ingenic_tcu_disable_regs(&tcu_clk->hw);
  230. err = clk_hw_register(NULL, &tcu_clk->hw);
  231. if (err) {
  232. kfree(tcu_clk);
  233. return err;
  234. }
  235. clocks->hws[idx] = &tcu_clk->hw;
  236. return 0;
  237. }
  238. static const struct ingenic_soc_info jz4740_soc_info = {
  239. .num_channels = 8,
  240. .has_ost = false,
  241. .has_tcu_clk = true,
  242. };
  243. static const struct ingenic_soc_info jz4725b_soc_info = {
  244. .num_channels = 6,
  245. .has_ost = true,
  246. .has_tcu_clk = true,
  247. };
  248. static const struct ingenic_soc_info jz4770_soc_info = {
  249. .num_channels = 8,
  250. .has_ost = true,
  251. .has_tcu_clk = false,
  252. };
  253. static const struct ingenic_soc_info x1000_soc_info = {
  254. .num_channels = 8,
  255. .has_ost = false, /* X1000 has OST, but it not belong TCU */
  256. .has_tcu_clk = true,
  257. .allow_missing_tcu_clk = true,
  258. };
  259. static const struct of_device_id __maybe_unused ingenic_tcu_of_match[] __initconst = {
  260. { .compatible = "ingenic,jz4740-tcu", .data = &jz4740_soc_info, },
  261. { .compatible = "ingenic,jz4725b-tcu", .data = &jz4725b_soc_info, },
  262. { .compatible = "ingenic,jz4760-tcu", .data = &jz4770_soc_info, },
  263. { .compatible = "ingenic,jz4770-tcu", .data = &jz4770_soc_info, },
  264. { .compatible = "ingenic,x1000-tcu", .data = &x1000_soc_info, },
  265. { /* sentinel */ }
  266. };
  267. static int __init ingenic_tcu_probe(struct device_node *np)
  268. {
  269. const struct of_device_id *id = of_match_node(ingenic_tcu_of_match, np);
  270. struct ingenic_tcu *tcu;
  271. struct regmap *map;
  272. unsigned int i;
  273. int ret;
  274. map = device_node_to_regmap(np);
  275. if (IS_ERR(map))
  276. return PTR_ERR(map);
  277. tcu = kzalloc(sizeof(*tcu), GFP_KERNEL);
  278. if (!tcu)
  279. return -ENOMEM;
  280. tcu->map = map;
  281. tcu->soc_info = id->data;
  282. if (tcu->soc_info->has_tcu_clk) {
  283. tcu->clk = of_clk_get_by_name(np, "tcu");
  284. if (IS_ERR(tcu->clk)) {
  285. ret = PTR_ERR(tcu->clk);
  286. /*
  287. * Old device trees for some SoCs did not include the
  288. * TCU clock because this driver (incorrectly) didn't
  289. * use it. In this case we complain loudly and attempt
  290. * to continue without the clock, which might work if
  291. * booting with workarounds like "clk_ignore_unused".
  292. */
  293. if (tcu->soc_info->allow_missing_tcu_clk && ret == -EINVAL) {
  294. pr_warn("TCU clock missing from device tree, please update your device tree\n");
  295. tcu->clk = NULL;
  296. } else {
  297. pr_crit("Cannot get TCU clock from device tree\n");
  298. goto err_free_tcu;
  299. }
  300. } else {
  301. ret = clk_prepare_enable(tcu->clk);
  302. if (ret) {
  303. pr_crit("Unable to enable TCU clock\n");
  304. goto err_put_clk;
  305. }
  306. }
  307. }
  308. tcu->clocks = kzalloc(struct_size(tcu->clocks, hws, TCU_CLK_COUNT),
  309. GFP_KERNEL);
  310. if (!tcu->clocks) {
  311. ret = -ENOMEM;
  312. goto err_clk_disable;
  313. }
  314. tcu->clocks->num = TCU_CLK_COUNT;
  315. for (i = 0; i < tcu->soc_info->num_channels; i++) {
  316. ret = ingenic_tcu_register_clock(tcu, i, TCU_PARENT_EXT,
  317. &ingenic_tcu_clk_info[i],
  318. tcu->clocks);
  319. if (ret) {
  320. pr_crit("cannot register clock %d\n", i);
  321. goto err_unregister_timer_clocks;
  322. }
  323. }
  324. /*
  325. * We set EXT as the default parent clock for all the TCU clocks
  326. * except for the watchdog one, where we set the RTC clock as the
  327. * parent. Since the EXT and PCLK are much faster than the RTC clock,
  328. * the watchdog would kick after a maximum time of 5s, and we might
  329. * want a slower kicking time.
  330. */
  331. ret = ingenic_tcu_register_clock(tcu, TCU_CLK_WDT, TCU_PARENT_RTC,
  332. &ingenic_tcu_watchdog_clk_info,
  333. tcu->clocks);
  334. if (ret) {
  335. pr_crit("cannot register watchdog clock\n");
  336. goto err_unregister_timer_clocks;
  337. }
  338. if (tcu->soc_info->has_ost) {
  339. ret = ingenic_tcu_register_clock(tcu, TCU_CLK_OST,
  340. TCU_PARENT_EXT,
  341. &ingenic_tcu_ost_clk_info,
  342. tcu->clocks);
  343. if (ret) {
  344. pr_crit("cannot register ost clock\n");
  345. goto err_unregister_watchdog_clock;
  346. }
  347. }
  348. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, tcu->clocks);
  349. if (ret) {
  350. pr_crit("cannot add OF clock provider\n");
  351. goto err_unregister_ost_clock;
  352. }
  353. ingenic_tcu = tcu;
  354. return 0;
  355. err_unregister_ost_clock:
  356. if (tcu->soc_info->has_ost)
  357. clk_hw_unregister(tcu->clocks->hws[i + 1]);
  358. err_unregister_watchdog_clock:
  359. clk_hw_unregister(tcu->clocks->hws[i]);
  360. err_unregister_timer_clocks:
  361. for (i = 0; i < tcu->clocks->num; i++)
  362. if (tcu->clocks->hws[i])
  363. clk_hw_unregister(tcu->clocks->hws[i]);
  364. kfree(tcu->clocks);
  365. err_clk_disable:
  366. if (tcu->clk)
  367. clk_disable_unprepare(tcu->clk);
  368. err_put_clk:
  369. if (tcu->clk)
  370. clk_put(tcu->clk);
  371. err_free_tcu:
  372. kfree(tcu);
  373. return ret;
  374. }
  375. static int __maybe_unused tcu_pm_suspend(void)
  376. {
  377. struct ingenic_tcu *tcu = ingenic_tcu;
  378. if (tcu->clk)
  379. clk_disable(tcu->clk);
  380. return 0;
  381. }
  382. static void __maybe_unused tcu_pm_resume(void)
  383. {
  384. struct ingenic_tcu *tcu = ingenic_tcu;
  385. if (tcu->clk)
  386. clk_enable(tcu->clk);
  387. }
  388. static struct syscore_ops __maybe_unused tcu_pm_ops = {
  389. .suspend = tcu_pm_suspend,
  390. .resume = tcu_pm_resume,
  391. };
  392. static void __init ingenic_tcu_init(struct device_node *np)
  393. {
  394. int ret = ingenic_tcu_probe(np);
  395. if (ret)
  396. pr_crit("Failed to initialize TCU clocks: %d\n", ret);
  397. if (IS_ENABLED(CONFIG_PM_SLEEP))
  398. register_syscore_ops(&tcu_pm_ops);
  399. }
  400. CLK_OF_DECLARE_DRIVER(jz4740_cgu, "ingenic,jz4740-tcu", ingenic_tcu_init);
  401. CLK_OF_DECLARE_DRIVER(jz4725b_cgu, "ingenic,jz4725b-tcu", ingenic_tcu_init);
  402. CLK_OF_DECLARE_DRIVER(jz4760_cgu, "ingenic,jz4760-tcu", ingenic_tcu_init);
  403. CLK_OF_DECLARE_DRIVER(jz4770_cgu, "ingenic,jz4770-tcu", ingenic_tcu_init);
  404. CLK_OF_DECLARE_DRIVER(x1000_cgu, "ingenic,x1000-tcu", ingenic_tcu_init);