tcu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * JZ47xx SoCs TCU clocks driver
  4. * Copyright (C) 2019 Paul Cercueil <[email protected]>
  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 long ingenic_tcu_round_rate(struct clk_hw *hw, unsigned long req_rate,
  141. unsigned long *parent_rate)
  142. {
  143. unsigned long rate = *parent_rate;
  144. u8 prescale;
  145. if (req_rate > rate)
  146. return rate;
  147. prescale = ingenic_tcu_get_prescale(rate, req_rate);
  148. return rate >> (prescale * 2);
  149. }
  150. static int ingenic_tcu_set_rate(struct clk_hw *hw, unsigned long req_rate,
  151. unsigned long parent_rate)
  152. {
  153. struct ingenic_tcu_clk *tcu_clk = to_tcu_clk(hw);
  154. const struct ingenic_tcu_clk_info *info = tcu_clk->info;
  155. u8 prescale = ingenic_tcu_get_prescale(parent_rate, req_rate);
  156. bool was_enabled;
  157. int ret;
  158. was_enabled = ingenic_tcu_enable_regs(hw);
  159. ret = regmap_update_bits(tcu_clk->tcu->map, info->tcsr_reg,
  160. TCU_TCSR_PRESCALE_MASK,
  161. prescale << TCU_TCSR_PRESCALE_LSB);
  162. WARN_ONCE(ret < 0, "Unable to update TCSR %d", tcu_clk->idx);
  163. if (!was_enabled)
  164. ingenic_tcu_disable_regs(hw);
  165. return 0;
  166. }
  167. static const struct clk_ops ingenic_tcu_clk_ops = {
  168. .get_parent = ingenic_tcu_get_parent,
  169. .set_parent = ingenic_tcu_set_parent,
  170. .recalc_rate = ingenic_tcu_recalc_rate,
  171. .round_rate = ingenic_tcu_round_rate,
  172. .set_rate = ingenic_tcu_set_rate,
  173. .enable = ingenic_tcu_enable,
  174. .disable = ingenic_tcu_disable,
  175. .is_enabled = ingenic_tcu_is_enabled,
  176. };
  177. static const char * const ingenic_tcu_timer_parents[] = {
  178. [TCU_PARENT_PCLK] = "pclk",
  179. [TCU_PARENT_RTC] = "rtc",
  180. [TCU_PARENT_EXT] = "ext",
  181. };
  182. #define DEF_TIMER(_name, _gate_bit, _tcsr) \
  183. { \
  184. .init_data = { \
  185. .name = _name, \
  186. .parent_names = ingenic_tcu_timer_parents, \
  187. .num_parents = ARRAY_SIZE(ingenic_tcu_timer_parents),\
  188. .ops = &ingenic_tcu_clk_ops, \
  189. .flags = CLK_SET_RATE_UNGATE, \
  190. }, \
  191. .gate_bit = _gate_bit, \
  192. .tcsr_reg = _tcsr, \
  193. }
  194. static const struct ingenic_tcu_clk_info ingenic_tcu_clk_info[] = {
  195. [TCU_CLK_TIMER0] = DEF_TIMER("timer0", 0, TCU_REG_TCSRc(0)),
  196. [TCU_CLK_TIMER1] = DEF_TIMER("timer1", 1, TCU_REG_TCSRc(1)),
  197. [TCU_CLK_TIMER2] = DEF_TIMER("timer2", 2, TCU_REG_TCSRc(2)),
  198. [TCU_CLK_TIMER3] = DEF_TIMER("timer3", 3, TCU_REG_TCSRc(3)),
  199. [TCU_CLK_TIMER4] = DEF_TIMER("timer4", 4, TCU_REG_TCSRc(4)),
  200. [TCU_CLK_TIMER5] = DEF_TIMER("timer5", 5, TCU_REG_TCSRc(5)),
  201. [TCU_CLK_TIMER6] = DEF_TIMER("timer6", 6, TCU_REG_TCSRc(6)),
  202. [TCU_CLK_TIMER7] = DEF_TIMER("timer7", 7, TCU_REG_TCSRc(7)),
  203. };
  204. static const struct ingenic_tcu_clk_info ingenic_tcu_watchdog_clk_info =
  205. DEF_TIMER("wdt", 16, TCU_REG_WDT_TCSR);
  206. static const struct ingenic_tcu_clk_info ingenic_tcu_ost_clk_info =
  207. DEF_TIMER("ost", 15, TCU_REG_OST_TCSR);
  208. #undef DEF_TIMER
  209. static int __init ingenic_tcu_register_clock(struct ingenic_tcu *tcu,
  210. unsigned int idx, enum tcu_clk_parent parent,
  211. const struct ingenic_tcu_clk_info *info,
  212. struct clk_hw_onecell_data *clocks)
  213. {
  214. struct ingenic_tcu_clk *tcu_clk;
  215. int err;
  216. tcu_clk = kzalloc(sizeof(*tcu_clk), GFP_KERNEL);
  217. if (!tcu_clk)
  218. return -ENOMEM;
  219. tcu_clk->hw.init = &info->init_data;
  220. tcu_clk->idx = idx;
  221. tcu_clk->info = info;
  222. tcu_clk->tcu = tcu;
  223. /* Reset channel and clock divider, set default parent */
  224. ingenic_tcu_enable_regs(&tcu_clk->hw);
  225. regmap_update_bits(tcu->map, info->tcsr_reg, 0xffff, BIT(parent));
  226. ingenic_tcu_disable_regs(&tcu_clk->hw);
  227. err = clk_hw_register(NULL, &tcu_clk->hw);
  228. if (err) {
  229. kfree(tcu_clk);
  230. return err;
  231. }
  232. clocks->hws[idx] = &tcu_clk->hw;
  233. return 0;
  234. }
  235. static const struct ingenic_soc_info jz4740_soc_info = {
  236. .num_channels = 8,
  237. .has_ost = false,
  238. .has_tcu_clk = true,
  239. };
  240. static const struct ingenic_soc_info jz4725b_soc_info = {
  241. .num_channels = 6,
  242. .has_ost = true,
  243. .has_tcu_clk = true,
  244. };
  245. static const struct ingenic_soc_info jz4770_soc_info = {
  246. .num_channels = 8,
  247. .has_ost = true,
  248. .has_tcu_clk = false,
  249. };
  250. static const struct ingenic_soc_info x1000_soc_info = {
  251. .num_channels = 8,
  252. .has_ost = false, /* X1000 has OST, but it not belong TCU */
  253. .has_tcu_clk = true,
  254. .allow_missing_tcu_clk = true,
  255. };
  256. static const struct of_device_id __maybe_unused ingenic_tcu_of_match[] __initconst = {
  257. { .compatible = "ingenic,jz4740-tcu", .data = &jz4740_soc_info, },
  258. { .compatible = "ingenic,jz4725b-tcu", .data = &jz4725b_soc_info, },
  259. { .compatible = "ingenic,jz4760-tcu", .data = &jz4770_soc_info, },
  260. { .compatible = "ingenic,jz4770-tcu", .data = &jz4770_soc_info, },
  261. { .compatible = "ingenic,x1000-tcu", .data = &x1000_soc_info, },
  262. { /* sentinel */ }
  263. };
  264. static int __init ingenic_tcu_probe(struct device_node *np)
  265. {
  266. const struct of_device_id *id = of_match_node(ingenic_tcu_of_match, np);
  267. struct ingenic_tcu *tcu;
  268. struct regmap *map;
  269. unsigned int i;
  270. int ret;
  271. map = device_node_to_regmap(np);
  272. if (IS_ERR(map))
  273. return PTR_ERR(map);
  274. tcu = kzalloc(sizeof(*tcu), GFP_KERNEL);
  275. if (!tcu)
  276. return -ENOMEM;
  277. tcu->map = map;
  278. tcu->soc_info = id->data;
  279. if (tcu->soc_info->has_tcu_clk) {
  280. tcu->clk = of_clk_get_by_name(np, "tcu");
  281. if (IS_ERR(tcu->clk)) {
  282. ret = PTR_ERR(tcu->clk);
  283. /*
  284. * Old device trees for some SoCs did not include the
  285. * TCU clock because this driver (incorrectly) didn't
  286. * use it. In this case we complain loudly and attempt
  287. * to continue without the clock, which might work if
  288. * booting with workarounds like "clk_ignore_unused".
  289. */
  290. if (tcu->soc_info->allow_missing_tcu_clk && ret == -EINVAL) {
  291. pr_warn("TCU clock missing from device tree, please update your device tree\n");
  292. tcu->clk = NULL;
  293. } else {
  294. pr_crit("Cannot get TCU clock from device tree\n");
  295. goto err_free_tcu;
  296. }
  297. } else {
  298. ret = clk_prepare_enable(tcu->clk);
  299. if (ret) {
  300. pr_crit("Unable to enable TCU clock\n");
  301. goto err_put_clk;
  302. }
  303. }
  304. }
  305. tcu->clocks = kzalloc(struct_size(tcu->clocks, hws, TCU_CLK_COUNT),
  306. GFP_KERNEL);
  307. if (!tcu->clocks) {
  308. ret = -ENOMEM;
  309. goto err_clk_disable;
  310. }
  311. tcu->clocks->num = TCU_CLK_COUNT;
  312. for (i = 0; i < tcu->soc_info->num_channels; i++) {
  313. ret = ingenic_tcu_register_clock(tcu, i, TCU_PARENT_EXT,
  314. &ingenic_tcu_clk_info[i],
  315. tcu->clocks);
  316. if (ret) {
  317. pr_crit("cannot register clock %d\n", i);
  318. goto err_unregister_timer_clocks;
  319. }
  320. }
  321. /*
  322. * We set EXT as the default parent clock for all the TCU clocks
  323. * except for the watchdog one, where we set the RTC clock as the
  324. * parent. Since the EXT and PCLK are much faster than the RTC clock,
  325. * the watchdog would kick after a maximum time of 5s, and we might
  326. * want a slower kicking time.
  327. */
  328. ret = ingenic_tcu_register_clock(tcu, TCU_CLK_WDT, TCU_PARENT_RTC,
  329. &ingenic_tcu_watchdog_clk_info,
  330. tcu->clocks);
  331. if (ret) {
  332. pr_crit("cannot register watchdog clock\n");
  333. goto err_unregister_timer_clocks;
  334. }
  335. if (tcu->soc_info->has_ost) {
  336. ret = ingenic_tcu_register_clock(tcu, TCU_CLK_OST,
  337. TCU_PARENT_EXT,
  338. &ingenic_tcu_ost_clk_info,
  339. tcu->clocks);
  340. if (ret) {
  341. pr_crit("cannot register ost clock\n");
  342. goto err_unregister_watchdog_clock;
  343. }
  344. }
  345. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, tcu->clocks);
  346. if (ret) {
  347. pr_crit("cannot add OF clock provider\n");
  348. goto err_unregister_ost_clock;
  349. }
  350. ingenic_tcu = tcu;
  351. return 0;
  352. err_unregister_ost_clock:
  353. if (tcu->soc_info->has_ost)
  354. clk_hw_unregister(tcu->clocks->hws[i + 1]);
  355. err_unregister_watchdog_clock:
  356. clk_hw_unregister(tcu->clocks->hws[i]);
  357. err_unregister_timer_clocks:
  358. for (i = 0; i < tcu->clocks->num; i++)
  359. if (tcu->clocks->hws[i])
  360. clk_hw_unregister(tcu->clocks->hws[i]);
  361. kfree(tcu->clocks);
  362. err_clk_disable:
  363. if (tcu->clk)
  364. clk_disable_unprepare(tcu->clk);
  365. err_put_clk:
  366. if (tcu->clk)
  367. clk_put(tcu->clk);
  368. err_free_tcu:
  369. kfree(tcu);
  370. return ret;
  371. }
  372. static int __maybe_unused tcu_pm_suspend(void)
  373. {
  374. struct ingenic_tcu *tcu = ingenic_tcu;
  375. if (tcu->clk)
  376. clk_disable(tcu->clk);
  377. return 0;
  378. }
  379. static void __maybe_unused tcu_pm_resume(void)
  380. {
  381. struct ingenic_tcu *tcu = ingenic_tcu;
  382. if (tcu->clk)
  383. clk_enable(tcu->clk);
  384. }
  385. static struct syscore_ops __maybe_unused tcu_pm_ops = {
  386. .suspend = tcu_pm_suspend,
  387. .resume = tcu_pm_resume,
  388. };
  389. static void __init ingenic_tcu_init(struct device_node *np)
  390. {
  391. int ret = ingenic_tcu_probe(np);
  392. if (ret)
  393. pr_crit("Failed to initialize TCU clocks: %d\n", ret);
  394. if (IS_ENABLED(CONFIG_PM_SLEEP))
  395. register_syscore_ops(&tcu_pm_ops);
  396. }
  397. CLK_OF_DECLARE_DRIVER(jz4740_cgu, "ingenic,jz4740-tcu", ingenic_tcu_init);
  398. CLK_OF_DECLARE_DRIVER(jz4725b_cgu, "ingenic,jz4725b-tcu", ingenic_tcu_init);
  399. CLK_OF_DECLARE_DRIVER(jz4760_cgu, "ingenic,jz4760-tcu", ingenic_tcu_init);
  400. CLK_OF_DECLARE_DRIVER(jz4770_cgu, "ingenic,jz4770-tcu", ingenic_tcu_init);
  401. CLK_OF_DECLARE_DRIVER(x1000_cgu, "ingenic,x1000-tcu", ingenic_tcu_init);