psc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Clock driver for TI Davinci PSC controllers
  4. *
  5. * Copyright (C) 2017 David Lechner <david@lechnology.com>
  6. *
  7. * Based on: drivers/clk/keystone/gate.c
  8. * Copyright (C) 2013 Texas Instruments.
  9. * Murali Karicheri <m-karicheri2@ti.com>
  10. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  11. *
  12. * And: arch/arm/mach-davinci/psc.c
  13. * Copyright (C) 2006 Texas Instruments.
  14. */
  15. #include <linux/clk-provider.h>
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/err.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_clock.h>
  24. #include <linux/pm_domain.h>
  25. #include <linux/regmap.h>
  26. #include <linux/reset-controller.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include "psc.h"
  30. /* PSC register offsets */
  31. #define EPCPR 0x070
  32. #define PTCMD 0x120
  33. #define PTSTAT 0x128
  34. #define PDSTAT(n) (0x200 + 4 * (n))
  35. #define PDCTL(n) (0x300 + 4 * (n))
  36. #define MDSTAT(n) (0x800 + 4 * (n))
  37. #define MDCTL(n) (0xa00 + 4 * (n))
  38. /* PSC module states */
  39. enum davinci_lpsc_state {
  40. LPSC_STATE_SWRSTDISABLE = 0,
  41. LPSC_STATE_SYNCRST = 1,
  42. LPSC_STATE_DISABLE = 2,
  43. LPSC_STATE_ENABLE = 3,
  44. };
  45. #define MDSTAT_STATE_MASK GENMASK(5, 0)
  46. #define MDSTAT_MCKOUT BIT(12)
  47. #define PDSTAT_STATE_MASK GENMASK(4, 0)
  48. #define MDCTL_FORCE BIT(31)
  49. #define MDCTL_LRESET BIT(8)
  50. #define PDCTL_EPCGOOD BIT(8)
  51. #define PDCTL_NEXT BIT(0)
  52. struct davinci_psc_data {
  53. struct clk_onecell_data clk_data;
  54. struct genpd_onecell_data pm_data;
  55. struct reset_controller_dev rcdev;
  56. };
  57. /**
  58. * struct davinci_lpsc_clk - LPSC clock structure
  59. * @dev: the device that provides this LPSC
  60. * @hw: clk_hw for the LPSC
  61. * @pm_domain: power domain for the LPSC
  62. * @genpd_clk: clock reference owned by @pm_domain
  63. * @regmap: PSC MMIO region
  64. * @md: Module domain (LPSC module id)
  65. * @pd: Power domain
  66. * @flags: LPSC_* quirk flags
  67. */
  68. struct davinci_lpsc_clk {
  69. struct device *dev;
  70. struct clk_hw hw;
  71. struct generic_pm_domain pm_domain;
  72. struct clk *genpd_clk;
  73. struct regmap *regmap;
  74. u32 md;
  75. u32 pd;
  76. u32 flags;
  77. };
  78. #define to_davinci_psc_data(x) container_of(x, struct davinci_psc_data, x)
  79. #define to_davinci_lpsc_clk(x) container_of(x, struct davinci_lpsc_clk, x)
  80. /**
  81. * best_dev_name - get the "best" device name.
  82. * @dev: the device
  83. *
  84. * Returns the device tree compatible name if the device has a DT node,
  85. * otherwise return the device name. This is mainly needed because clkdev
  86. * lookups are limited to 20 chars for dev_id and when using device tree,
  87. * dev_name(dev) is much longer than that.
  88. */
  89. static inline const char *best_dev_name(struct device *dev)
  90. {
  91. const char *compatible;
  92. if (!of_property_read_string(dev->of_node, "compatible", &compatible))
  93. return compatible;
  94. return dev_name(dev);
  95. }
  96. static void davinci_lpsc_config(struct davinci_lpsc_clk *lpsc,
  97. enum davinci_lpsc_state next_state)
  98. {
  99. u32 epcpr, pdstat, mdstat, ptstat;
  100. regmap_write_bits(lpsc->regmap, MDCTL(lpsc->md), MDSTAT_STATE_MASK,
  101. next_state);
  102. if (lpsc->flags & LPSC_FORCE)
  103. regmap_write_bits(lpsc->regmap, MDCTL(lpsc->md), MDCTL_FORCE,
  104. MDCTL_FORCE);
  105. regmap_read(lpsc->regmap, PDSTAT(lpsc->pd), &pdstat);
  106. if ((pdstat & PDSTAT_STATE_MASK) == 0) {
  107. regmap_write_bits(lpsc->regmap, PDCTL(lpsc->pd), PDCTL_NEXT,
  108. PDCTL_NEXT);
  109. regmap_write(lpsc->regmap, PTCMD, BIT(lpsc->pd));
  110. regmap_read_poll_timeout(lpsc->regmap, EPCPR, epcpr,
  111. epcpr & BIT(lpsc->pd), 0, 0);
  112. regmap_write_bits(lpsc->regmap, PDCTL(lpsc->pd), PDCTL_EPCGOOD,
  113. PDCTL_EPCGOOD);
  114. } else {
  115. regmap_write(lpsc->regmap, PTCMD, BIT(lpsc->pd));
  116. }
  117. regmap_read_poll_timeout(lpsc->regmap, PTSTAT, ptstat,
  118. !(ptstat & BIT(lpsc->pd)), 0, 0);
  119. regmap_read_poll_timeout(lpsc->regmap, MDSTAT(lpsc->md), mdstat,
  120. (mdstat & MDSTAT_STATE_MASK) == next_state,
  121. 0, 0);
  122. }
  123. static int davinci_lpsc_clk_enable(struct clk_hw *hw)
  124. {
  125. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(hw);
  126. davinci_lpsc_config(lpsc, LPSC_STATE_ENABLE);
  127. return 0;
  128. }
  129. static void davinci_lpsc_clk_disable(struct clk_hw *hw)
  130. {
  131. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(hw);
  132. davinci_lpsc_config(lpsc, LPSC_STATE_DISABLE);
  133. }
  134. static int davinci_lpsc_clk_is_enabled(struct clk_hw *hw)
  135. {
  136. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(hw);
  137. u32 mdstat;
  138. regmap_read(lpsc->regmap, MDSTAT(lpsc->md), &mdstat);
  139. return (mdstat & MDSTAT_MCKOUT) ? 1 : 0;
  140. }
  141. static const struct clk_ops davinci_lpsc_clk_ops = {
  142. .enable = davinci_lpsc_clk_enable,
  143. .disable = davinci_lpsc_clk_disable,
  144. .is_enabled = davinci_lpsc_clk_is_enabled,
  145. };
  146. static int davinci_psc_genpd_attach_dev(struct generic_pm_domain *pm_domain,
  147. struct device *dev)
  148. {
  149. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(pm_domain);
  150. struct clk *clk;
  151. int ret;
  152. /*
  153. * pm_clk_remove_clk() will call clk_put(), so we have to use clk_get()
  154. * to get the clock instead of using lpsc->hw.clk directly.
  155. */
  156. clk = clk_get_sys(best_dev_name(lpsc->dev), clk_hw_get_name(&lpsc->hw));
  157. if (IS_ERR(clk))
  158. return (PTR_ERR(clk));
  159. ret = pm_clk_create(dev);
  160. if (ret < 0)
  161. goto fail_clk_put;
  162. ret = pm_clk_add_clk(dev, clk);
  163. if (ret < 0)
  164. goto fail_pm_clk_destroy;
  165. lpsc->genpd_clk = clk;
  166. return 0;
  167. fail_pm_clk_destroy:
  168. pm_clk_destroy(dev);
  169. fail_clk_put:
  170. clk_put(clk);
  171. return ret;
  172. }
  173. static void davinci_psc_genpd_detach_dev(struct generic_pm_domain *pm_domain,
  174. struct device *dev)
  175. {
  176. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(pm_domain);
  177. pm_clk_remove_clk(dev, lpsc->genpd_clk);
  178. pm_clk_destroy(dev);
  179. lpsc->genpd_clk = NULL;
  180. }
  181. /**
  182. * davinci_lpsc_clk_register - register LPSC clock
  183. * @name: name of this clock
  184. * @parent_name: name of clock's parent
  185. * @regmap: PSC MMIO region
  186. * @md: local PSC number
  187. * @pd: power domain
  188. * @flags: LPSC_* flags
  189. */
  190. static struct davinci_lpsc_clk *
  191. davinci_lpsc_clk_register(struct device *dev, const char *name,
  192. const char *parent_name, struct regmap *regmap,
  193. u32 md, u32 pd, u32 flags)
  194. {
  195. struct clk_init_data init;
  196. struct davinci_lpsc_clk *lpsc;
  197. int ret;
  198. bool is_on;
  199. lpsc = devm_kzalloc(dev, sizeof(*lpsc), GFP_KERNEL);
  200. if (!lpsc)
  201. return ERR_PTR(-ENOMEM);
  202. init.name = name;
  203. init.ops = &davinci_lpsc_clk_ops;
  204. init.parent_names = (parent_name ? &parent_name : NULL);
  205. init.num_parents = (parent_name ? 1 : 0);
  206. init.flags = 0;
  207. if (flags & LPSC_ALWAYS_ENABLED)
  208. init.flags |= CLK_IS_CRITICAL;
  209. if (flags & LPSC_SET_RATE_PARENT)
  210. init.flags |= CLK_SET_RATE_PARENT;
  211. lpsc->dev = dev;
  212. lpsc->regmap = regmap;
  213. lpsc->hw.init = &init;
  214. lpsc->md = md;
  215. lpsc->pd = pd;
  216. lpsc->flags = flags;
  217. ret = devm_clk_hw_register(dev, &lpsc->hw);
  218. if (ret < 0)
  219. return ERR_PTR(ret);
  220. /* genpd attach needs a way to look up this clock */
  221. ret = clk_hw_register_clkdev(&lpsc->hw, name, best_dev_name(dev));
  222. lpsc->pm_domain.name = devm_kasprintf(dev, GFP_KERNEL, "%s: %s",
  223. best_dev_name(dev), name);
  224. lpsc->pm_domain.attach_dev = davinci_psc_genpd_attach_dev;
  225. lpsc->pm_domain.detach_dev = davinci_psc_genpd_detach_dev;
  226. lpsc->pm_domain.flags = GENPD_FLAG_PM_CLK;
  227. is_on = davinci_lpsc_clk_is_enabled(&lpsc->hw);
  228. pm_genpd_init(&lpsc->pm_domain, NULL, is_on);
  229. return lpsc;
  230. }
  231. static int davinci_lpsc_clk_reset(struct clk *clk, bool reset)
  232. {
  233. struct clk_hw *hw = __clk_get_hw(clk);
  234. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(hw);
  235. u32 mdctl;
  236. if (IS_ERR_OR_NULL(lpsc))
  237. return -EINVAL;
  238. mdctl = reset ? 0 : MDCTL_LRESET;
  239. regmap_write_bits(lpsc->regmap, MDCTL(lpsc->md), MDCTL_LRESET, mdctl);
  240. return 0;
  241. }
  242. /*
  243. * REVISIT: These exported functions can be removed after a non-DT lookup is
  244. * added to the reset controller framework and the davinci-rproc driver is
  245. * updated to use the generic reset controller framework.
  246. */
  247. int davinci_clk_reset_assert(struct clk *clk)
  248. {
  249. return davinci_lpsc_clk_reset(clk, true);
  250. }
  251. EXPORT_SYMBOL(davinci_clk_reset_assert);
  252. int davinci_clk_reset_deassert(struct clk *clk)
  253. {
  254. return davinci_lpsc_clk_reset(clk, false);
  255. }
  256. EXPORT_SYMBOL(davinci_clk_reset_deassert);
  257. static int davinci_psc_reset_assert(struct reset_controller_dev *rcdev,
  258. unsigned long id)
  259. {
  260. struct davinci_psc_data *psc = to_davinci_psc_data(rcdev);
  261. struct clk *clk = psc->clk_data.clks[id];
  262. return davinci_lpsc_clk_reset(clk, true);
  263. }
  264. static int davinci_psc_reset_deassert(struct reset_controller_dev *rcdev,
  265. unsigned long id)
  266. {
  267. struct davinci_psc_data *psc = to_davinci_psc_data(rcdev);
  268. struct clk *clk = psc->clk_data.clks[id];
  269. return davinci_lpsc_clk_reset(clk, false);
  270. }
  271. static const struct reset_control_ops davinci_psc_reset_ops = {
  272. .assert = davinci_psc_reset_assert,
  273. .deassert = davinci_psc_reset_deassert,
  274. };
  275. static int davinci_psc_reset_of_xlate(struct reset_controller_dev *rcdev,
  276. const struct of_phandle_args *reset_spec)
  277. {
  278. struct of_phandle_args clkspec = *reset_spec; /* discard const qualifier */
  279. struct clk *clk;
  280. struct clk_hw *hw;
  281. struct davinci_lpsc_clk *lpsc;
  282. /* the clock node is the same as the reset node */
  283. clk = of_clk_get_from_provider(&clkspec);
  284. if (IS_ERR(clk))
  285. return PTR_ERR(clk);
  286. hw = __clk_get_hw(clk);
  287. lpsc = to_davinci_lpsc_clk(hw);
  288. clk_put(clk);
  289. /* not all modules support local reset */
  290. if (!(lpsc->flags & LPSC_LOCAL_RESET))
  291. return -EINVAL;
  292. return lpsc->md;
  293. }
  294. static const struct regmap_config davinci_psc_regmap_config = {
  295. .reg_bits = 32,
  296. .reg_stride = 4,
  297. .val_bits = 32,
  298. };
  299. static struct davinci_psc_data *
  300. __davinci_psc_register_clocks(struct device *dev,
  301. const struct davinci_lpsc_clk_info *info,
  302. int num_clks,
  303. void __iomem *base)
  304. {
  305. struct davinci_psc_data *psc;
  306. struct clk **clks;
  307. struct generic_pm_domain **pm_domains;
  308. struct regmap *regmap;
  309. int i, ret;
  310. psc = devm_kzalloc(dev, sizeof(*psc), GFP_KERNEL);
  311. if (!psc)
  312. return ERR_PTR(-ENOMEM);
  313. clks = devm_kmalloc_array(dev, num_clks, sizeof(*clks), GFP_KERNEL);
  314. if (!clks)
  315. return ERR_PTR(-ENOMEM);
  316. psc->clk_data.clks = clks;
  317. psc->clk_data.clk_num = num_clks;
  318. /*
  319. * init array with error so that of_clk_src_onecell_get() doesn't
  320. * return NULL for gaps in the sparse array
  321. */
  322. for (i = 0; i < num_clks; i++)
  323. clks[i] = ERR_PTR(-ENOENT);
  324. pm_domains = devm_kcalloc(dev, num_clks, sizeof(*pm_domains), GFP_KERNEL);
  325. if (!pm_domains)
  326. return ERR_PTR(-ENOMEM);
  327. psc->pm_data.domains = pm_domains;
  328. psc->pm_data.num_domains = num_clks;
  329. regmap = devm_regmap_init_mmio(dev, base, &davinci_psc_regmap_config);
  330. if (IS_ERR(regmap))
  331. return ERR_CAST(regmap);
  332. for (; info->name; info++) {
  333. struct davinci_lpsc_clk *lpsc;
  334. lpsc = davinci_lpsc_clk_register(dev, info->name, info->parent,
  335. regmap, info->md, info->pd,
  336. info->flags);
  337. if (IS_ERR(lpsc)) {
  338. dev_warn(dev, "Failed to register %s (%ld)\n",
  339. info->name, PTR_ERR(lpsc));
  340. continue;
  341. }
  342. clks[info->md] = lpsc->hw.clk;
  343. pm_domains[info->md] = &lpsc->pm_domain;
  344. }
  345. psc->rcdev.ops = &davinci_psc_reset_ops;
  346. psc->rcdev.owner = THIS_MODULE;
  347. psc->rcdev.dev = dev;
  348. psc->rcdev.of_node = dev->of_node;
  349. psc->rcdev.of_reset_n_cells = 1;
  350. psc->rcdev.of_xlate = davinci_psc_reset_of_xlate;
  351. psc->rcdev.nr_resets = num_clks;
  352. ret = devm_reset_controller_register(dev, &psc->rcdev);
  353. if (ret < 0)
  354. dev_warn(dev, "Failed to register reset controller (%d)\n", ret);
  355. return psc;
  356. }
  357. int davinci_psc_register_clocks(struct device *dev,
  358. const struct davinci_lpsc_clk_info *info,
  359. u8 num_clks,
  360. void __iomem *base)
  361. {
  362. struct davinci_psc_data *psc;
  363. psc = __davinci_psc_register_clocks(dev, info, num_clks, base);
  364. if (IS_ERR(psc))
  365. return PTR_ERR(psc);
  366. for (; info->name; info++) {
  367. const struct davinci_lpsc_clkdev_info *cdevs = info->cdevs;
  368. struct clk *clk = psc->clk_data.clks[info->md];
  369. if (!cdevs || IS_ERR_OR_NULL(clk))
  370. continue;
  371. for (; cdevs->con_id || cdevs->dev_id; cdevs++)
  372. clk_register_clkdev(clk, cdevs->con_id, cdevs->dev_id);
  373. }
  374. return 0;
  375. }
  376. int of_davinci_psc_clk_init(struct device *dev,
  377. const struct davinci_lpsc_clk_info *info,
  378. u8 num_clks,
  379. void __iomem *base)
  380. {
  381. struct device_node *node = dev->of_node;
  382. struct davinci_psc_data *psc;
  383. psc = __davinci_psc_register_clocks(dev, info, num_clks, base);
  384. if (IS_ERR(psc))
  385. return PTR_ERR(psc);
  386. of_genpd_add_provider_onecell(node, &psc->pm_data);
  387. of_clk_add_provider(node, of_clk_src_onecell_get, &psc->clk_data);
  388. return 0;
  389. }
  390. static const struct of_device_id davinci_psc_of_match[] = {
  391. { .compatible = "ti,da850-psc0", .data = &of_da850_psc0_init_data },
  392. { .compatible = "ti,da850-psc1", .data = &of_da850_psc1_init_data },
  393. { }
  394. };
  395. static const struct platform_device_id davinci_psc_id_table[] = {
  396. { .name = "da830-psc0", .driver_data = (kernel_ulong_t)&da830_psc0_init_data },
  397. { .name = "da830-psc1", .driver_data = (kernel_ulong_t)&da830_psc1_init_data },
  398. { .name = "da850-psc0", .driver_data = (kernel_ulong_t)&da850_psc0_init_data },
  399. { .name = "da850-psc1", .driver_data = (kernel_ulong_t)&da850_psc1_init_data },
  400. { .name = "dm355-psc", .driver_data = (kernel_ulong_t)&dm355_psc_init_data },
  401. { .name = "dm365-psc", .driver_data = (kernel_ulong_t)&dm365_psc_init_data },
  402. { .name = "dm644x-psc", .driver_data = (kernel_ulong_t)&dm644x_psc_init_data },
  403. { .name = "dm646x-psc", .driver_data = (kernel_ulong_t)&dm646x_psc_init_data },
  404. { }
  405. };
  406. static int davinci_psc_probe(struct platform_device *pdev)
  407. {
  408. struct device *dev = &pdev->dev;
  409. const struct of_device_id *of_id;
  410. const struct davinci_psc_init_data *init_data = NULL;
  411. struct resource *res;
  412. void __iomem *base;
  413. int ret;
  414. of_id = of_match_device(davinci_psc_of_match, dev);
  415. if (of_id)
  416. init_data = of_id->data;
  417. else if (pdev->id_entry)
  418. init_data = (void *)pdev->id_entry->driver_data;
  419. if (!init_data) {
  420. dev_err(dev, "unable to find driver init data\n");
  421. return -EINVAL;
  422. }
  423. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  424. base = devm_ioremap_resource(dev, res);
  425. if (IS_ERR(base))
  426. return PTR_ERR(base);
  427. ret = devm_clk_bulk_get(dev, init_data->num_parent_clks,
  428. init_data->parent_clks);
  429. if (ret < 0)
  430. return ret;
  431. return init_data->psc_init(dev, base);
  432. }
  433. static struct platform_driver davinci_psc_driver = {
  434. .probe = davinci_psc_probe,
  435. .driver = {
  436. .name = "davinci-psc-clk",
  437. .of_match_table = davinci_psc_of_match,
  438. },
  439. .id_table = davinci_psc_id_table,
  440. };
  441. static int __init davinci_psc_driver_init(void)
  442. {
  443. return platform_driver_register(&davinci_psc_driver);
  444. }
  445. /* has to be postcore_initcall because davinci_gpio depend on PSC clocks */
  446. postcore_initcall(davinci_psc_driver_init);