renesas-cpg-mssr.c 14 KB

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