renesas-cpg-mssr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * Renesas Clock Pulse Generator / Module Standby and Software Reset
  3. *
  4. * Copyright (C) 2015 Glider bvba
  5. *
  6. * Based on clk-mstp.c, clk-rcar-gen2.c, and clk-rcar-gen3.c
  7. *
  8. * Copyright (C) 2013 Ideas On Board SPRL
  9. * Copyright (C) 2015 Renesas Electronics Corp.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/clk/renesas.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/module.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_clock.h>
  26. #include <linux/pm_domain.h>
  27. #include <linux/slab.h>
  28. #include <dt-bindings/clock/renesas-cpg-mssr.h>
  29. #include "renesas-cpg-mssr.h"
  30. #include "clk-div6.h"
  31. #ifdef DEBUG
  32. #define WARN_DEBUG(x) WARN_ON(x)
  33. #else
  34. #define WARN_DEBUG(x) do { } while (0)
  35. #endif
  36. /*
  37. * Module Standby and Software Reset register offets.
  38. *
  39. * If the registers exist, these are valid for SH-Mobile, R-Mobile,
  40. * R-Car Gen 2, and R-Car Gen 3.
  41. * These are NOT valid for R-Car Gen1 and RZ/A1!
  42. */
  43. /*
  44. * Module Stop Status Register offsets
  45. */
  46. static const u16 mstpsr[] = {
  47. 0x030, 0x038, 0x040, 0x048, 0x04C, 0x03C, 0x1C0, 0x1C4,
  48. 0x9A0, 0x9A4, 0x9A8, 0x9AC,
  49. };
  50. #define MSTPSR(i) mstpsr[i]
  51. /*
  52. * System Module Stop Control Register offsets
  53. */
  54. static const u16 smstpcr[] = {
  55. 0x130, 0x134, 0x138, 0x13C, 0x140, 0x144, 0x148, 0x14C,
  56. 0x990, 0x994, 0x998, 0x99C,
  57. };
  58. #define SMSTPCR(i) smstpcr[i]
  59. /*
  60. * Software Reset Register offsets
  61. */
  62. static const u16 srcr[] = {
  63. 0x0A0, 0x0A8, 0x0B0, 0x0B8, 0x0BC, 0x0C4, 0x1C8, 0x1CC,
  64. 0x920, 0x924, 0x928, 0x92C,
  65. };
  66. #define SRCR(i) srcr[i]
  67. /* Realtime Module Stop Control Register offsets */
  68. #define RMSTPCR(i) (smstpcr[i] - 0x20)
  69. /* Modem Module Stop Control Register offsets (r8a73a4) */
  70. #define MMSTPCR(i) (smstpcr[i] + 0x20)
  71. /* Software Reset Clearing Register offsets */
  72. #define SRSTCLR(i) (0x940 + (i) * 4)
  73. /**
  74. * Clock Pulse Generator / Module Standby and Software Reset Private Data
  75. *
  76. * @dev: CPG/MSSR device
  77. * @base: CPG/MSSR register block base address
  78. * @mstp_lock: protects writes to SMSTPCR
  79. * @clks: Array containing all Core and Module Clocks
  80. * @num_core_clks: Number of Core Clocks in clks[]
  81. * @num_mod_clks: Number of Module Clocks in clks[]
  82. * @last_dt_core_clk: ID of the last Core Clock exported to DT
  83. */
  84. struct cpg_mssr_priv {
  85. struct device *dev;
  86. void __iomem *base;
  87. spinlock_t mstp_lock;
  88. struct clk **clks;
  89. unsigned int num_core_clks;
  90. unsigned int num_mod_clks;
  91. unsigned int last_dt_core_clk;
  92. };
  93. /**
  94. * struct mstp_clock - MSTP gating clock
  95. * @hw: handle between common and hardware-specific interfaces
  96. * @index: MSTP clock number
  97. * @priv: CPG/MSSR private data
  98. */
  99. struct mstp_clock {
  100. struct clk_hw hw;
  101. u32 index;
  102. struct cpg_mssr_priv *priv;
  103. };
  104. #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
  105. static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable)
  106. {
  107. struct mstp_clock *clock = to_mstp_clock(hw);
  108. struct cpg_mssr_priv *priv = clock->priv;
  109. unsigned int reg = clock->index / 32;
  110. unsigned int bit = clock->index % 32;
  111. struct device *dev = priv->dev;
  112. u32 bitmask = BIT(bit);
  113. unsigned long flags;
  114. unsigned int i;
  115. u32 value;
  116. dev_dbg(dev, "MSTP %u%02u/%pC %s\n", reg, bit, hw->clk,
  117. enable ? "ON" : "OFF");
  118. spin_lock_irqsave(&priv->mstp_lock, flags);
  119. value = readl(priv->base + SMSTPCR(reg));
  120. if (enable)
  121. value &= ~bitmask;
  122. else
  123. value |= bitmask;
  124. writel(value, priv->base + SMSTPCR(reg));
  125. spin_unlock_irqrestore(&priv->mstp_lock, flags);
  126. if (!enable)
  127. return 0;
  128. for (i = 1000; i > 0; --i) {
  129. if (!(readl(priv->base + MSTPSR(reg)) & bitmask))
  130. break;
  131. cpu_relax();
  132. }
  133. if (!i) {
  134. dev_err(dev, "Failed to enable SMSTP %p[%d]\n",
  135. priv->base + SMSTPCR(reg), bit);
  136. return -ETIMEDOUT;
  137. }
  138. return 0;
  139. }
  140. static int cpg_mstp_clock_enable(struct clk_hw *hw)
  141. {
  142. return cpg_mstp_clock_endisable(hw, true);
  143. }
  144. static void cpg_mstp_clock_disable(struct clk_hw *hw)
  145. {
  146. cpg_mstp_clock_endisable(hw, false);
  147. }
  148. static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
  149. {
  150. struct mstp_clock *clock = to_mstp_clock(hw);
  151. struct cpg_mssr_priv *priv = clock->priv;
  152. u32 value;
  153. value = readl(priv->base + MSTPSR(clock->index / 32));
  154. return !(value & BIT(clock->index % 32));
  155. }
  156. static const struct clk_ops cpg_mstp_clock_ops = {
  157. .enable = cpg_mstp_clock_enable,
  158. .disable = cpg_mstp_clock_disable,
  159. .is_enabled = cpg_mstp_clock_is_enabled,
  160. };
  161. static
  162. struct clk *cpg_mssr_clk_src_twocell_get(struct of_phandle_args *clkspec,
  163. void *data)
  164. {
  165. unsigned int clkidx = clkspec->args[1];
  166. struct cpg_mssr_priv *priv = data;
  167. struct device *dev = priv->dev;
  168. unsigned int idx;
  169. const char *type;
  170. struct clk *clk;
  171. switch (clkspec->args[0]) {
  172. case CPG_CORE:
  173. type = "core";
  174. if (clkidx > priv->last_dt_core_clk) {
  175. dev_err(dev, "Invalid %s clock index %u\n", type,
  176. clkidx);
  177. return ERR_PTR(-EINVAL);
  178. }
  179. clk = priv->clks[clkidx];
  180. break;
  181. case CPG_MOD:
  182. type = "module";
  183. idx = MOD_CLK_PACK(clkidx);
  184. if (clkidx % 100 > 31 || idx >= priv->num_mod_clks) {
  185. dev_err(dev, "Invalid %s clock index %u\n", type,
  186. clkidx);
  187. return ERR_PTR(-EINVAL);
  188. }
  189. clk = priv->clks[priv->num_core_clks + idx];
  190. break;
  191. default:
  192. dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]);
  193. return ERR_PTR(-EINVAL);
  194. }
  195. if (IS_ERR(clk))
  196. dev_err(dev, "Cannot get %s clock %u: %ld", type, clkidx,
  197. PTR_ERR(clk));
  198. else
  199. dev_dbg(dev, "clock (%u, %u) is %pC at %pCr Hz\n",
  200. clkspec->args[0], clkspec->args[1], clk, clk);
  201. return clk;
  202. }
  203. static void __init cpg_mssr_register_core_clk(const struct cpg_core_clk *core,
  204. const struct cpg_mssr_info *info,
  205. struct cpg_mssr_priv *priv)
  206. {
  207. struct clk *clk = NULL, *parent;
  208. struct device *dev = priv->dev;
  209. unsigned int id = core->id, div = core->div;
  210. const char *parent_name;
  211. WARN_DEBUG(id >= priv->num_core_clks);
  212. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  213. switch (core->type) {
  214. case CLK_TYPE_IN:
  215. clk = of_clk_get_by_name(priv->dev->of_node, core->name);
  216. break;
  217. case CLK_TYPE_FF:
  218. case CLK_TYPE_DIV6P1:
  219. case CLK_TYPE_DIV6_RO:
  220. WARN_DEBUG(core->parent >= priv->num_core_clks);
  221. parent = priv->clks[core->parent];
  222. if (IS_ERR(parent)) {
  223. clk = parent;
  224. goto fail;
  225. }
  226. parent_name = __clk_get_name(parent);
  227. if (core->type == CLK_TYPE_DIV6_RO)
  228. /* Multiply with the DIV6 register value */
  229. div *= (readl(priv->base + core->offset) & 0x3f) + 1;
  230. if (core->type == CLK_TYPE_DIV6P1) {
  231. clk = cpg_div6_register(core->name, 1, &parent_name,
  232. priv->base + core->offset);
  233. } else {
  234. clk = clk_register_fixed_factor(NULL, core->name,
  235. parent_name, 0,
  236. core->mult, div);
  237. }
  238. break;
  239. default:
  240. if (info->cpg_clk_register)
  241. clk = info->cpg_clk_register(dev, core, info,
  242. priv->clks, priv->base);
  243. else
  244. dev_err(dev, "%s has unsupported core clock type %u\n",
  245. core->name, core->type);
  246. break;
  247. }
  248. if (IS_ERR_OR_NULL(clk))
  249. goto fail;
  250. dev_dbg(dev, "Core clock %pC at %pCr Hz\n", clk, clk);
  251. priv->clks[id] = clk;
  252. return;
  253. fail:
  254. dev_err(dev, "Failed to register %s clock %s: %ld\n", "core",
  255. core->name, PTR_ERR(clk));
  256. }
  257. static void __init cpg_mssr_register_mod_clk(const struct mssr_mod_clk *mod,
  258. const struct cpg_mssr_info *info,
  259. struct cpg_mssr_priv *priv)
  260. {
  261. struct mstp_clock *clock = NULL;
  262. struct device *dev = priv->dev;
  263. unsigned int id = mod->id;
  264. struct clk_init_data init;
  265. struct clk *parent, *clk;
  266. const char *parent_name;
  267. unsigned int i;
  268. WARN_DEBUG(id < priv->num_core_clks);
  269. WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks);
  270. WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks);
  271. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  272. parent = priv->clks[mod->parent];
  273. if (IS_ERR(parent)) {
  274. clk = parent;
  275. goto fail;
  276. }
  277. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  278. if (!clock) {
  279. clk = ERR_PTR(-ENOMEM);
  280. goto fail;
  281. }
  282. init.name = mod->name;
  283. init.ops = &cpg_mstp_clock_ops;
  284. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  285. for (i = 0; i < info->num_crit_mod_clks; i++)
  286. if (id == info->crit_mod_clks[i]) {
  287. #ifdef CLK_ENABLE_HAND_OFF
  288. dev_dbg(dev, "MSTP %s setting CLK_ENABLE_HAND_OFF\n",
  289. mod->name);
  290. init.flags |= CLK_ENABLE_HAND_OFF;
  291. break;
  292. #else
  293. dev_dbg(dev, "Ignoring MSTP %s to prevent disabling\n",
  294. mod->name);
  295. kfree(clock);
  296. return;
  297. #endif
  298. }
  299. parent_name = __clk_get_name(parent);
  300. init.parent_names = &parent_name;
  301. init.num_parents = 1;
  302. clock->index = id - priv->num_core_clks;
  303. clock->priv = priv;
  304. clock->hw.init = &init;
  305. clk = clk_register(NULL, &clock->hw);
  306. if (IS_ERR(clk))
  307. goto fail;
  308. dev_dbg(dev, "Module clock %pC at %pCr Hz\n", clk, clk);
  309. priv->clks[id] = clk;
  310. return;
  311. fail:
  312. dev_err(dev, "Failed to register %s clock %s: %ld\n", "module",
  313. mod->name, PTR_ERR(clk));
  314. kfree(clock);
  315. }
  316. struct cpg_mssr_clk_domain {
  317. struct generic_pm_domain genpd;
  318. struct device_node *np;
  319. unsigned int num_core_pm_clks;
  320. unsigned int core_pm_clks[0];
  321. };
  322. static struct cpg_mssr_clk_domain *cpg_mssr_clk_domain;
  323. static bool cpg_mssr_is_pm_clk(const struct of_phandle_args *clkspec,
  324. struct cpg_mssr_clk_domain *pd)
  325. {
  326. unsigned int i;
  327. if (clkspec->np != pd->np || clkspec->args_count != 2)
  328. return false;
  329. switch (clkspec->args[0]) {
  330. case CPG_CORE:
  331. for (i = 0; i < pd->num_core_pm_clks; i++)
  332. if (clkspec->args[1] == pd->core_pm_clks[i])
  333. return true;
  334. return false;
  335. case CPG_MOD:
  336. return true;
  337. default:
  338. return false;
  339. }
  340. }
  341. int cpg_mssr_attach_dev(struct generic_pm_domain *unused, struct device *dev)
  342. {
  343. struct cpg_mssr_clk_domain *pd = cpg_mssr_clk_domain;
  344. struct device_node *np = dev->of_node;
  345. struct of_phandle_args clkspec;
  346. struct clk *clk;
  347. int i = 0;
  348. int error;
  349. if (!pd) {
  350. dev_dbg(dev, "CPG/MSSR clock domain not yet available\n");
  351. return -EPROBE_DEFER;
  352. }
  353. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  354. &clkspec)) {
  355. if (cpg_mssr_is_pm_clk(&clkspec, pd))
  356. goto found;
  357. of_node_put(clkspec.np);
  358. i++;
  359. }
  360. return 0;
  361. found:
  362. clk = of_clk_get_from_provider(&clkspec);
  363. of_node_put(clkspec.np);
  364. if (IS_ERR(clk))
  365. return PTR_ERR(clk);
  366. error = pm_clk_create(dev);
  367. if (error) {
  368. dev_err(dev, "pm_clk_create failed %d\n", error);
  369. goto fail_put;
  370. }
  371. error = pm_clk_add_clk(dev, clk);
  372. if (error) {
  373. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  374. goto fail_destroy;
  375. }
  376. return 0;
  377. fail_destroy:
  378. pm_clk_destroy(dev);
  379. fail_put:
  380. clk_put(clk);
  381. return error;
  382. }
  383. void cpg_mssr_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  384. {
  385. if (!list_empty(&dev->power.subsys_data->clock_list))
  386. pm_clk_destroy(dev);
  387. }
  388. static int __init cpg_mssr_add_clk_domain(struct device *dev,
  389. const unsigned int *core_pm_clks,
  390. unsigned int num_core_pm_clks)
  391. {
  392. struct device_node *np = dev->of_node;
  393. struct generic_pm_domain *genpd;
  394. struct cpg_mssr_clk_domain *pd;
  395. size_t pm_size = num_core_pm_clks * sizeof(core_pm_clks[0]);
  396. pd = devm_kzalloc(dev, sizeof(*pd) + pm_size, GFP_KERNEL);
  397. if (!pd)
  398. return -ENOMEM;
  399. pd->np = np;
  400. pd->num_core_pm_clks = num_core_pm_clks;
  401. memcpy(pd->core_pm_clks, core_pm_clks, pm_size);
  402. genpd = &pd->genpd;
  403. genpd->name = np->name;
  404. genpd->flags = GENPD_FLAG_PM_CLK;
  405. genpd->attach_dev = cpg_mssr_attach_dev;
  406. genpd->detach_dev = cpg_mssr_detach_dev;
  407. pm_genpd_init(genpd, &pm_domain_always_on_gov, false);
  408. cpg_mssr_clk_domain = pd;
  409. of_genpd_add_provider_simple(np, genpd);
  410. return 0;
  411. }
  412. static const struct of_device_id cpg_mssr_match[] = {
  413. #ifdef CONFIG_ARCH_R8A7743
  414. {
  415. .compatible = "renesas,r8a7743-cpg-mssr",
  416. .data = &r8a7743_cpg_mssr_info,
  417. },
  418. #endif
  419. #ifdef CONFIG_ARCH_R8A7745
  420. {
  421. .compatible = "renesas,r8a7745-cpg-mssr",
  422. .data = &r8a7745_cpg_mssr_info,
  423. },
  424. #endif
  425. #ifdef CONFIG_ARCH_R8A7795
  426. {
  427. .compatible = "renesas,r8a7795-cpg-mssr",
  428. .data = &r8a7795_cpg_mssr_info,
  429. },
  430. #endif
  431. #ifdef CONFIG_ARCH_R8A7796
  432. {
  433. .compatible = "renesas,r8a7796-cpg-mssr",
  434. .data = &r8a7796_cpg_mssr_info,
  435. },
  436. #endif
  437. { /* sentinel */ }
  438. };
  439. static void cpg_mssr_del_clk_provider(void *data)
  440. {
  441. of_clk_del_provider(data);
  442. }
  443. static int __init cpg_mssr_probe(struct platform_device *pdev)
  444. {
  445. struct device *dev = &pdev->dev;
  446. struct device_node *np = dev->of_node;
  447. const struct cpg_mssr_info *info;
  448. struct cpg_mssr_priv *priv;
  449. unsigned int nclks, i;
  450. struct resource *res;
  451. struct clk **clks;
  452. int error;
  453. info = of_match_node(cpg_mssr_match, np)->data;
  454. if (info->init) {
  455. error = info->init(dev);
  456. if (error)
  457. return error;
  458. }
  459. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  460. if (!priv)
  461. return -ENOMEM;
  462. priv->dev = dev;
  463. spin_lock_init(&priv->mstp_lock);
  464. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  465. priv->base = devm_ioremap_resource(dev, res);
  466. if (IS_ERR(priv->base))
  467. return PTR_ERR(priv->base);
  468. nclks = info->num_total_core_clks + info->num_hw_mod_clks;
  469. clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL);
  470. if (!clks)
  471. return -ENOMEM;
  472. priv->clks = clks;
  473. priv->num_core_clks = info->num_total_core_clks;
  474. priv->num_mod_clks = info->num_hw_mod_clks;
  475. priv->last_dt_core_clk = info->last_dt_core_clk;
  476. for (i = 0; i < nclks; i++)
  477. clks[i] = ERR_PTR(-ENOENT);
  478. for (i = 0; i < info->num_core_clks; i++)
  479. cpg_mssr_register_core_clk(&info->core_clks[i], info, priv);
  480. for (i = 0; i < info->num_mod_clks; i++)
  481. cpg_mssr_register_mod_clk(&info->mod_clks[i], info, priv);
  482. error = of_clk_add_provider(np, cpg_mssr_clk_src_twocell_get, priv);
  483. if (error)
  484. return error;
  485. error = devm_add_action_or_reset(dev,
  486. cpg_mssr_del_clk_provider,
  487. np);
  488. if (error)
  489. return error;
  490. error = cpg_mssr_add_clk_domain(dev, info->core_pm_clks,
  491. info->num_core_pm_clks);
  492. if (error)
  493. return error;
  494. return 0;
  495. }
  496. static struct platform_driver cpg_mssr_driver = {
  497. .driver = {
  498. .name = "renesas-cpg-mssr",
  499. .of_match_table = cpg_mssr_match,
  500. },
  501. };
  502. static int __init cpg_mssr_init(void)
  503. {
  504. return platform_driver_probe(&cpg_mssr_driver, cpg_mssr_probe);
  505. }
  506. subsys_initcall(cpg_mssr_init);
  507. MODULE_DESCRIPTION("Renesas CPG/MSSR Driver");
  508. MODULE_LICENSE("GPL v2");