renesas-cpg-mssr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 = clk_readl(priv->base + SMSTPCR(reg));
  120. if (enable)
  121. value &= ~bitmask;
  122. else
  123. value |= bitmask;
  124. clk_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 (!(clk_readl(priv->base + MSTPSR(reg)) &
  130. bitmask))
  131. break;
  132. cpu_relax();
  133. }
  134. if (!i) {
  135. dev_err(dev, "Failed to enable SMSTP %p[%d]\n",
  136. priv->base + SMSTPCR(reg), bit);
  137. return -ETIMEDOUT;
  138. }
  139. return 0;
  140. }
  141. static int cpg_mstp_clock_enable(struct clk_hw *hw)
  142. {
  143. return cpg_mstp_clock_endisable(hw, true);
  144. }
  145. static void cpg_mstp_clock_disable(struct clk_hw *hw)
  146. {
  147. cpg_mstp_clock_endisable(hw, false);
  148. }
  149. static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
  150. {
  151. struct mstp_clock *clock = to_mstp_clock(hw);
  152. struct cpg_mssr_priv *priv = clock->priv;
  153. u32 value;
  154. value = clk_readl(priv->base + MSTPSR(clock->index / 32));
  155. return !(value & BIT(clock->index % 32));
  156. }
  157. static const struct clk_ops cpg_mstp_clock_ops = {
  158. .enable = cpg_mstp_clock_enable,
  159. .disable = cpg_mstp_clock_disable,
  160. .is_enabled = cpg_mstp_clock_is_enabled,
  161. };
  162. static
  163. struct clk *cpg_mssr_clk_src_twocell_get(struct of_phandle_args *clkspec,
  164. void *data)
  165. {
  166. unsigned int clkidx = clkspec->args[1];
  167. struct cpg_mssr_priv *priv = data;
  168. struct device *dev = priv->dev;
  169. unsigned int idx;
  170. const char *type;
  171. struct clk *clk;
  172. switch (clkspec->args[0]) {
  173. case CPG_CORE:
  174. type = "core";
  175. if (clkidx > priv->last_dt_core_clk) {
  176. dev_err(dev, "Invalid %s clock index %u\n", type,
  177. clkidx);
  178. return ERR_PTR(-EINVAL);
  179. }
  180. clk = priv->clks[clkidx];
  181. break;
  182. case CPG_MOD:
  183. type = "module";
  184. idx = MOD_CLK_PACK(clkidx);
  185. if (clkidx % 100 > 31 || idx >= priv->num_mod_clks) {
  186. dev_err(dev, "Invalid %s clock index %u\n", type,
  187. clkidx);
  188. return ERR_PTR(-EINVAL);
  189. }
  190. clk = priv->clks[priv->num_core_clks + idx];
  191. break;
  192. default:
  193. dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]);
  194. return ERR_PTR(-EINVAL);
  195. }
  196. if (IS_ERR(clk))
  197. dev_err(dev, "Cannot get %s clock %u: %ld", type, clkidx,
  198. PTR_ERR(clk));
  199. else
  200. dev_dbg(dev, "clock (%u, %u) is %pC at %pCr Hz\n",
  201. clkspec->args[0], clkspec->args[1], clk, clk);
  202. return clk;
  203. }
  204. static void __init cpg_mssr_register_core_clk(const struct cpg_core_clk *core,
  205. const struct cpg_mssr_info *info,
  206. struct cpg_mssr_priv *priv)
  207. {
  208. struct clk *clk = NULL, *parent;
  209. struct device *dev = priv->dev;
  210. unsigned int id = core->id, div = core->div;
  211. const char *parent_name;
  212. WARN_DEBUG(id >= priv->num_core_clks);
  213. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  214. switch (core->type) {
  215. case CLK_TYPE_IN:
  216. clk = of_clk_get_by_name(priv->dev->of_node, core->name);
  217. break;
  218. case CLK_TYPE_FF:
  219. case CLK_TYPE_DIV6P1:
  220. case CLK_TYPE_DIV6_RO:
  221. WARN_DEBUG(core->parent >= priv->num_core_clks);
  222. parent = priv->clks[core->parent];
  223. if (IS_ERR(parent)) {
  224. clk = parent;
  225. goto fail;
  226. }
  227. parent_name = __clk_get_name(parent);
  228. if (core->type == CLK_TYPE_DIV6_RO)
  229. /* Multiply with the DIV6 register value */
  230. div *= (readl(priv->base + core->offset) & 0x3f) + 1;
  231. if (core->type == CLK_TYPE_DIV6P1) {
  232. clk = cpg_div6_register(core->name, 1, &parent_name,
  233. priv->base + core->offset);
  234. } else {
  235. clk = clk_register_fixed_factor(NULL, core->name,
  236. parent_name, 0,
  237. core->mult, div);
  238. }
  239. break;
  240. default:
  241. if (info->cpg_clk_register)
  242. clk = info->cpg_clk_register(dev, core, info,
  243. priv->clks, priv->base);
  244. else
  245. dev_err(dev, "%s has unsupported core clock type %u\n",
  246. core->name, core->type);
  247. break;
  248. }
  249. if (IS_ERR_OR_NULL(clk))
  250. goto fail;
  251. dev_dbg(dev, "Core clock %pC at %pCr Hz\n", clk, clk);
  252. priv->clks[id] = clk;
  253. return;
  254. fail:
  255. dev_err(dev, "Failed to register %s clock %s: %ld\n", "core,",
  256. core->name, PTR_ERR(clk));
  257. }
  258. static void __init cpg_mssr_register_mod_clk(const struct mssr_mod_clk *mod,
  259. const struct cpg_mssr_info *info,
  260. struct cpg_mssr_priv *priv)
  261. {
  262. struct mstp_clock *clock = NULL;
  263. struct device *dev = priv->dev;
  264. unsigned int id = mod->id;
  265. struct clk_init_data init;
  266. struct clk *parent, *clk;
  267. const char *parent_name;
  268. unsigned int i;
  269. WARN_DEBUG(id < priv->num_core_clks);
  270. WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks);
  271. WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks);
  272. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  273. parent = priv->clks[mod->parent];
  274. if (IS_ERR(parent)) {
  275. clk = parent;
  276. goto fail;
  277. }
  278. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  279. if (!clock) {
  280. clk = ERR_PTR(-ENOMEM);
  281. goto fail;
  282. }
  283. init.name = mod->name;
  284. init.ops = &cpg_mstp_clock_ops;
  285. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  286. for (i = 0; i < info->num_crit_mod_clks; i++)
  287. if (id == info->crit_mod_clks[i]) {
  288. #ifdef CLK_ENABLE_HAND_OFF
  289. dev_dbg(dev, "MSTP %s setting CLK_ENABLE_HAND_OFF\n",
  290. mod->name);
  291. init.flags |= CLK_ENABLE_HAND_OFF;
  292. break;
  293. #else
  294. dev_dbg(dev, "Ignoring MSTP %s to prevent disabling\n",
  295. mod->name);
  296. kfree(clock);
  297. return;
  298. #endif
  299. }
  300. parent_name = __clk_get_name(parent);
  301. init.parent_names = &parent_name;
  302. init.num_parents = 1;
  303. clock->index = id - priv->num_core_clks;
  304. clock->priv = priv;
  305. clock->hw.init = &init;
  306. clk = clk_register(NULL, &clock->hw);
  307. if (IS_ERR(clk))
  308. goto fail;
  309. dev_dbg(dev, "Module clock %pC at %pCr Hz\n", clk, clk);
  310. priv->clks[id] = clk;
  311. return;
  312. fail:
  313. dev_err(dev, "Failed to register %s clock %s: %ld\n", "module,",
  314. mod->name, PTR_ERR(clk));
  315. kfree(clock);
  316. }
  317. struct cpg_mssr_clk_domain {
  318. struct generic_pm_domain genpd;
  319. struct device_node *np;
  320. unsigned int num_core_pm_clks;
  321. unsigned int core_pm_clks[0];
  322. };
  323. static struct cpg_mssr_clk_domain *cpg_mssr_clk_domain;
  324. static bool cpg_mssr_is_pm_clk(const struct of_phandle_args *clkspec,
  325. struct cpg_mssr_clk_domain *pd)
  326. {
  327. unsigned int i;
  328. if (clkspec->np != pd->np || clkspec->args_count != 2)
  329. return false;
  330. switch (clkspec->args[0]) {
  331. case CPG_CORE:
  332. for (i = 0; i < pd->num_core_pm_clks; i++)
  333. if (clkspec->args[1] == pd->core_pm_clks[i])
  334. return true;
  335. return false;
  336. case CPG_MOD:
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. int cpg_mssr_attach_dev(struct generic_pm_domain *unused, struct device *dev)
  343. {
  344. struct cpg_mssr_clk_domain *pd = cpg_mssr_clk_domain;
  345. struct device_node *np = dev->of_node;
  346. struct of_phandle_args clkspec;
  347. struct clk *clk;
  348. int i = 0;
  349. int error;
  350. if (!pd) {
  351. dev_dbg(dev, "CPG/MSSR clock domain not yet available\n");
  352. return -EPROBE_DEFER;
  353. }
  354. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  355. &clkspec)) {
  356. if (cpg_mssr_is_pm_clk(&clkspec, pd))
  357. goto found;
  358. of_node_put(clkspec.np);
  359. i++;
  360. }
  361. return 0;
  362. found:
  363. clk = of_clk_get_from_provider(&clkspec);
  364. of_node_put(clkspec.np);
  365. if (IS_ERR(clk))
  366. return PTR_ERR(clk);
  367. error = pm_clk_create(dev);
  368. if (error) {
  369. dev_err(dev, "pm_clk_create failed %d\n", error);
  370. goto fail_put;
  371. }
  372. error = pm_clk_add_clk(dev, clk);
  373. if (error) {
  374. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  375. goto fail_destroy;
  376. }
  377. return 0;
  378. fail_destroy:
  379. pm_clk_destroy(dev);
  380. fail_put:
  381. clk_put(clk);
  382. return error;
  383. }
  384. void cpg_mssr_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  385. {
  386. if (!list_empty(&dev->power.subsys_data->clock_list))
  387. pm_clk_destroy(dev);
  388. }
  389. static int __init cpg_mssr_add_clk_domain(struct device *dev,
  390. const unsigned int *core_pm_clks,
  391. unsigned int num_core_pm_clks)
  392. {
  393. struct device_node *np = dev->of_node;
  394. struct generic_pm_domain *genpd;
  395. struct cpg_mssr_clk_domain *pd;
  396. size_t pm_size = num_core_pm_clks * sizeof(core_pm_clks[0]);
  397. pd = devm_kzalloc(dev, sizeof(*pd) + pm_size, GFP_KERNEL);
  398. if (!pd)
  399. return -ENOMEM;
  400. pd->np = np;
  401. pd->num_core_pm_clks = num_core_pm_clks;
  402. memcpy(pd->core_pm_clks, core_pm_clks, pm_size);
  403. genpd = &pd->genpd;
  404. genpd->name = np->name;
  405. genpd->flags = GENPD_FLAG_PM_CLK;
  406. genpd->attach_dev = cpg_mssr_attach_dev;
  407. genpd->detach_dev = cpg_mssr_detach_dev;
  408. pm_genpd_init(genpd, &pm_domain_always_on_gov, false);
  409. cpg_mssr_clk_domain = pd;
  410. of_genpd_add_provider_simple(np, genpd);
  411. return 0;
  412. }
  413. static const struct of_device_id cpg_mssr_match[] = {
  414. #ifdef CONFIG_ARCH_R8A7795
  415. {
  416. .compatible = "renesas,r8a7795-cpg-mssr",
  417. .data = &r8a7795_cpg_mssr_info,
  418. },
  419. #endif
  420. #ifdef CONFIG_ARCH_R8A7796
  421. {
  422. .compatible = "renesas,r8a7796-cpg-mssr",
  423. .data = &r8a7796_cpg_mssr_info,
  424. },
  425. #endif
  426. { /* sentinel */ }
  427. };
  428. static void cpg_mssr_del_clk_provider(void *data)
  429. {
  430. of_clk_del_provider(data);
  431. }
  432. static int __init cpg_mssr_probe(struct platform_device *pdev)
  433. {
  434. struct device *dev = &pdev->dev;
  435. struct device_node *np = dev->of_node;
  436. const struct cpg_mssr_info *info;
  437. struct cpg_mssr_priv *priv;
  438. unsigned int nclks, i;
  439. struct resource *res;
  440. struct clk **clks;
  441. int error;
  442. info = of_match_node(cpg_mssr_match, np)->data;
  443. if (info->init) {
  444. error = info->init(dev);
  445. if (error)
  446. return error;
  447. }
  448. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  449. if (!priv)
  450. return -ENOMEM;
  451. priv->dev = dev;
  452. spin_lock_init(&priv->mstp_lock);
  453. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  454. priv->base = devm_ioremap_resource(dev, res);
  455. if (IS_ERR(priv->base))
  456. return PTR_ERR(priv->base);
  457. nclks = info->num_total_core_clks + info->num_hw_mod_clks;
  458. clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL);
  459. if (!clks)
  460. return -ENOMEM;
  461. priv->clks = clks;
  462. priv->num_core_clks = info->num_total_core_clks;
  463. priv->num_mod_clks = info->num_hw_mod_clks;
  464. priv->last_dt_core_clk = info->last_dt_core_clk;
  465. for (i = 0; i < nclks; i++)
  466. clks[i] = ERR_PTR(-ENOENT);
  467. for (i = 0; i < info->num_core_clks; i++)
  468. cpg_mssr_register_core_clk(&info->core_clks[i], info, priv);
  469. for (i = 0; i < info->num_mod_clks; i++)
  470. cpg_mssr_register_mod_clk(&info->mod_clks[i], info, priv);
  471. error = of_clk_add_provider(np, cpg_mssr_clk_src_twocell_get, priv);
  472. if (error)
  473. return error;
  474. error = devm_add_action_or_reset(dev,
  475. cpg_mssr_del_clk_provider,
  476. np);
  477. if (error)
  478. return error;
  479. error = cpg_mssr_add_clk_domain(dev, info->core_pm_clks,
  480. info->num_core_pm_clks);
  481. if (error)
  482. return error;
  483. return 0;
  484. }
  485. static struct platform_driver cpg_mssr_driver = {
  486. .driver = {
  487. .name = "renesas-cpg-mssr",
  488. .of_match_table = cpg_mssr_match,
  489. },
  490. };
  491. static int __init cpg_mssr_init(void)
  492. {
  493. return platform_driver_probe(&cpg_mssr_driver, cpg_mssr_probe);
  494. }
  495. subsys_initcall(cpg_mssr_init);
  496. MODULE_DESCRIPTION("Renesas CPG/MSSR Driver");
  497. MODULE_LICENSE("GPL v2");