psc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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/clk/davinci.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/err.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/property.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 or NULL
  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. * @dev: the clocks's device or NULL
  184. * @name: name of this clock
  185. * @parent_name: name of clock's parent
  186. * @regmap: PSC MMIO region
  187. * @md: local PSC number
  188. * @pd: power domain
  189. * @flags: LPSC_* flags
  190. */
  191. static struct davinci_lpsc_clk *
  192. davinci_lpsc_clk_register(struct device *dev, const char *name,
  193. const char *parent_name, struct regmap *regmap,
  194. u32 md, u32 pd, u32 flags)
  195. {
  196. struct clk_init_data init;
  197. struct davinci_lpsc_clk *lpsc;
  198. int ret;
  199. bool is_on;
  200. lpsc = kzalloc(sizeof(*lpsc), GFP_KERNEL);
  201. if (!lpsc)
  202. return ERR_PTR(-ENOMEM);
  203. init.name = name;
  204. init.ops = &davinci_lpsc_clk_ops;
  205. init.parent_names = (parent_name ? &parent_name : NULL);
  206. init.num_parents = (parent_name ? 1 : 0);
  207. init.flags = 0;
  208. if (flags & LPSC_ALWAYS_ENABLED)
  209. init.flags |= CLK_IS_CRITICAL;
  210. if (flags & LPSC_SET_RATE_PARENT)
  211. init.flags |= CLK_SET_RATE_PARENT;
  212. lpsc->dev = dev;
  213. lpsc->regmap = regmap;
  214. lpsc->hw.init = &init;
  215. lpsc->md = md;
  216. lpsc->pd = pd;
  217. lpsc->flags = flags;
  218. ret = clk_hw_register(dev, &lpsc->hw);
  219. if (ret < 0) {
  220. kfree(lpsc);
  221. return ERR_PTR(ret);
  222. }
  223. /* for now, genpd is only registered when using device-tree */
  224. if (!dev || !dev->of_node)
  225. return lpsc;
  226. /* genpd attach needs a way to look up this clock */
  227. ret = clk_hw_register_clkdev(&lpsc->hw, name, best_dev_name(dev));
  228. lpsc->pm_domain.name = devm_kasprintf(dev, GFP_KERNEL, "%s: %s",
  229. best_dev_name(dev), name);
  230. lpsc->pm_domain.attach_dev = davinci_psc_genpd_attach_dev;
  231. lpsc->pm_domain.detach_dev = davinci_psc_genpd_detach_dev;
  232. lpsc->pm_domain.flags = GENPD_FLAG_PM_CLK;
  233. is_on = davinci_lpsc_clk_is_enabled(&lpsc->hw);
  234. pm_genpd_init(&lpsc->pm_domain, NULL, is_on);
  235. return lpsc;
  236. }
  237. static int davinci_lpsc_clk_reset(struct clk *clk, bool reset)
  238. {
  239. struct clk_hw *hw = __clk_get_hw(clk);
  240. struct davinci_lpsc_clk *lpsc = to_davinci_lpsc_clk(hw);
  241. u32 mdctl;
  242. if (IS_ERR_OR_NULL(lpsc))
  243. return -EINVAL;
  244. mdctl = reset ? 0 : MDCTL_LRESET;
  245. regmap_write_bits(lpsc->regmap, MDCTL(lpsc->md), MDCTL_LRESET, mdctl);
  246. return 0;
  247. }
  248. static int davinci_psc_reset_assert(struct reset_controller_dev *rcdev,
  249. unsigned long id)
  250. {
  251. struct davinci_psc_data *psc = to_davinci_psc_data(rcdev);
  252. struct clk *clk = psc->clk_data.clks[id];
  253. return davinci_lpsc_clk_reset(clk, true);
  254. }
  255. static int davinci_psc_reset_deassert(struct reset_controller_dev *rcdev,
  256. unsigned long id)
  257. {
  258. struct davinci_psc_data *psc = to_davinci_psc_data(rcdev);
  259. struct clk *clk = psc->clk_data.clks[id];
  260. return davinci_lpsc_clk_reset(clk, false);
  261. }
  262. static const struct reset_control_ops davinci_psc_reset_ops = {
  263. .assert = davinci_psc_reset_assert,
  264. .deassert = davinci_psc_reset_deassert,
  265. };
  266. static int davinci_psc_reset_of_xlate(struct reset_controller_dev *rcdev,
  267. const struct of_phandle_args *reset_spec)
  268. {
  269. struct of_phandle_args clkspec = *reset_spec; /* discard const qualifier */
  270. struct clk *clk;
  271. struct clk_hw *hw;
  272. struct davinci_lpsc_clk *lpsc;
  273. /* the clock node is the same as the reset node */
  274. clk = of_clk_get_from_provider(&clkspec);
  275. if (IS_ERR(clk))
  276. return PTR_ERR(clk);
  277. hw = __clk_get_hw(clk);
  278. lpsc = to_davinci_lpsc_clk(hw);
  279. clk_put(clk);
  280. /* not all modules support local reset */
  281. if (!(lpsc->flags & LPSC_LOCAL_RESET))
  282. return -EINVAL;
  283. return lpsc->md;
  284. }
  285. static const struct regmap_config davinci_psc_regmap_config = {
  286. .reg_bits = 32,
  287. .reg_stride = 4,
  288. .val_bits = 32,
  289. };
  290. static struct davinci_psc_data *
  291. __davinci_psc_register_clocks(struct device *dev,
  292. const struct davinci_lpsc_clk_info *info,
  293. int num_clks,
  294. void __iomem *base)
  295. {
  296. struct davinci_psc_data *psc;
  297. struct clk **clks;
  298. struct generic_pm_domain **pm_domains;
  299. struct regmap *regmap;
  300. int i, ret;
  301. psc = kzalloc(sizeof(*psc), GFP_KERNEL);
  302. if (!psc)
  303. return ERR_PTR(-ENOMEM);
  304. clks = kmalloc_array(num_clks, sizeof(*clks), GFP_KERNEL);
  305. if (!clks) {
  306. ret = -ENOMEM;
  307. goto err_free_psc;
  308. }
  309. psc->clk_data.clks = clks;
  310. psc->clk_data.clk_num = num_clks;
  311. /*
  312. * init array with error so that of_clk_src_onecell_get() doesn't
  313. * return NULL for gaps in the sparse array
  314. */
  315. for (i = 0; i < num_clks; i++)
  316. clks[i] = ERR_PTR(-ENOENT);
  317. pm_domains = kcalloc(num_clks, sizeof(*pm_domains), GFP_KERNEL);
  318. if (!pm_domains) {
  319. ret = -ENOMEM;
  320. goto err_free_clks;
  321. }
  322. psc->pm_data.domains = pm_domains;
  323. psc->pm_data.num_domains = num_clks;
  324. regmap = regmap_init_mmio(dev, base, &davinci_psc_regmap_config);
  325. if (IS_ERR(regmap)) {
  326. ret = PTR_ERR(regmap);
  327. goto err_free_pm_domains;
  328. }
  329. for (; info->name; info++) {
  330. struct davinci_lpsc_clk *lpsc;
  331. lpsc = davinci_lpsc_clk_register(dev, info->name, info->parent,
  332. regmap, info->md, info->pd,
  333. info->flags);
  334. if (IS_ERR(lpsc)) {
  335. dev_warn(dev, "Failed to register %s (%ld)\n",
  336. info->name, PTR_ERR(lpsc));
  337. continue;
  338. }
  339. clks[info->md] = lpsc->hw.clk;
  340. pm_domains[info->md] = &lpsc->pm_domain;
  341. }
  342. /*
  343. * for now, a reset controller is only registered when there is a device
  344. * to associate it with.
  345. */
  346. if (!dev)
  347. return psc;
  348. psc->rcdev.ops = &davinci_psc_reset_ops;
  349. psc->rcdev.owner = THIS_MODULE;
  350. psc->rcdev.dev = dev;
  351. psc->rcdev.of_node = dev->of_node;
  352. psc->rcdev.of_reset_n_cells = 1;
  353. psc->rcdev.of_xlate = davinci_psc_reset_of_xlate;
  354. psc->rcdev.nr_resets = num_clks;
  355. ret = devm_reset_controller_register(dev, &psc->rcdev);
  356. if (ret < 0)
  357. dev_warn(dev, "Failed to register reset controller (%d)\n", ret);
  358. return psc;
  359. err_free_pm_domains:
  360. kfree(pm_domains);
  361. err_free_clks:
  362. kfree(clks);
  363. err_free_psc:
  364. kfree(psc);
  365. return ERR_PTR(ret);
  366. }
  367. int davinci_psc_register_clocks(struct device *dev,
  368. const struct davinci_lpsc_clk_info *info,
  369. u8 num_clks,
  370. void __iomem *base)
  371. {
  372. struct davinci_psc_data *psc;
  373. psc = __davinci_psc_register_clocks(dev, info, num_clks, base);
  374. if (IS_ERR(psc))
  375. return PTR_ERR(psc);
  376. for (; info->name; info++) {
  377. const struct davinci_lpsc_clkdev_info *cdevs = info->cdevs;
  378. struct clk *clk = psc->clk_data.clks[info->md];
  379. if (!cdevs || IS_ERR_OR_NULL(clk))
  380. continue;
  381. for (; cdevs->con_id || cdevs->dev_id; cdevs++)
  382. clk_register_clkdev(clk, cdevs->con_id, cdevs->dev_id);
  383. }
  384. return 0;
  385. }
  386. int of_davinci_psc_clk_init(struct device *dev,
  387. const struct davinci_lpsc_clk_info *info,
  388. u8 num_clks,
  389. void __iomem *base)
  390. {
  391. struct device_node *node = dev->of_node;
  392. struct davinci_psc_data *psc;
  393. psc = __davinci_psc_register_clocks(dev, info, num_clks, base);
  394. if (IS_ERR(psc))
  395. return PTR_ERR(psc);
  396. of_genpd_add_provider_onecell(node, &psc->pm_data);
  397. of_clk_add_provider(node, of_clk_src_onecell_get, &psc->clk_data);
  398. return 0;
  399. }
  400. static const struct of_device_id davinci_psc_of_match[] = {
  401. #ifdef CONFIG_ARCH_DAVINCI_DA850
  402. { .compatible = "ti,da850-psc0", .data = &of_da850_psc0_init_data },
  403. { .compatible = "ti,da850-psc1", .data = &of_da850_psc1_init_data },
  404. #endif
  405. { }
  406. };
  407. static const struct platform_device_id davinci_psc_id_table[] = {
  408. #ifdef CONFIG_ARCH_DAVINCI_DA830
  409. { .name = "da830-psc0", .driver_data = (kernel_ulong_t)&da830_psc0_init_data },
  410. { .name = "da830-psc1", .driver_data = (kernel_ulong_t)&da830_psc1_init_data },
  411. #endif
  412. #ifdef CONFIG_ARCH_DAVINCI_DA850
  413. { .name = "da850-psc0", .driver_data = (kernel_ulong_t)&da850_psc0_init_data },
  414. { .name = "da850-psc1", .driver_data = (kernel_ulong_t)&da850_psc1_init_data },
  415. #endif
  416. { }
  417. };
  418. static int davinci_psc_probe(struct platform_device *pdev)
  419. {
  420. struct device *dev = &pdev->dev;
  421. const struct davinci_psc_init_data *init_data = NULL;
  422. void __iomem *base;
  423. int ret;
  424. init_data = device_get_match_data(dev);
  425. if (!init_data && pdev->id_entry)
  426. init_data = (void *)pdev->id_entry->driver_data;
  427. if (!init_data) {
  428. dev_err(dev, "unable to find driver init data\n");
  429. return -EINVAL;
  430. }
  431. base = devm_platform_ioremap_resource(pdev, 0);
  432. if (IS_ERR(base))
  433. return PTR_ERR(base);
  434. ret = devm_clk_bulk_get(dev, init_data->num_parent_clks,
  435. init_data->parent_clks);
  436. if (ret < 0)
  437. return ret;
  438. return init_data->psc_init(dev, base);
  439. }
  440. static struct platform_driver davinci_psc_driver = {
  441. .probe = davinci_psc_probe,
  442. .driver = {
  443. .name = "davinci-psc-clk",
  444. .of_match_table = davinci_psc_of_match,
  445. },
  446. .id_table = davinci_psc_id_table,
  447. };
  448. static int __init davinci_psc_driver_init(void)
  449. {
  450. return platform_driver_register(&davinci_psc_driver);
  451. }
  452. /* has to be postcore_initcall because davinci_gpio depend on PSC clocks */
  453. postcore_initcall(davinci_psc_driver_init);