renesas-cpg-mssr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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/device.h>
  18. #include <linux/init.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/module.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_clock.h>
  25. #include <linux/pm_domain.h>
  26. #include <linux/slab.h>
  27. #include <dt-bindings/clock/renesas-cpg-mssr.h>
  28. #include "renesas-cpg-mssr.h"
  29. #include "clk-div6.h"
  30. #ifdef DEBUG
  31. #define WARN_DEBUG(x) do { } while (0)
  32. #else
  33. #define WARN_DEBUG(x) WARN_ON(x)
  34. #endif
  35. /*
  36. * Module Standby and Software Reset register offets.
  37. *
  38. * If the registers exist, these are valid for SH-Mobile, R-Mobile,
  39. * R-Car Gen 2, and R-Car Gen 3.
  40. * These are NOT valid for R-Car Gen1 and RZ/A1!
  41. */
  42. /*
  43. * Module Stop Status Register offsets
  44. */
  45. static const u16 mstpsr[] = {
  46. 0x030, 0x038, 0x040, 0x048, 0x04C, 0x03C, 0x1C0, 0x1C4,
  47. 0x9A0, 0x9A4, 0x9A8, 0x9AC,
  48. };
  49. #define MSTPSR(i) mstpsr[i]
  50. /*
  51. * System Module Stop Control Register offsets
  52. */
  53. static const u16 smstpcr[] = {
  54. 0x130, 0x134, 0x138, 0x13C, 0x140, 0x144, 0x148, 0x14C,
  55. 0x990, 0x994, 0x998, 0x99C,
  56. };
  57. #define SMSTPCR(i) smstpcr[i]
  58. /*
  59. * Software Reset Register offsets
  60. */
  61. static const u16 srcr[] = {
  62. 0x0A0, 0x0A8, 0x0B0, 0x0B8, 0x0BC, 0x0C4, 0x1C8, 0x1CC,
  63. 0x920, 0x924, 0x928, 0x92C,
  64. };
  65. #define SRCR(i) srcr[i]
  66. /* Realtime Module Stop Control Register offsets */
  67. #define RMSTPCR(i) (smstpcr[i] - 0x20)
  68. /* Modem Module Stop Control Register offsets (r8a73a4) */
  69. #define MMSTPCR(i) (smstpcr[i] + 0x20)
  70. /* Software Reset Clearing Register offsets */
  71. #define SRSTCLR(i) (0x940 + (i) * 4)
  72. /**
  73. * Clock Pulse Generator / Module Standby and Software Reset Private Data
  74. *
  75. * @dev: CPG/MSSR device
  76. * @base: CPG/MSSR register block base address
  77. * @mstp_lock: protects writes to SMSTPCR
  78. * @clks: Array containing all Core and Module Clocks
  79. * @num_core_clks: Number of Core Clocks in clks[]
  80. * @num_mod_clks: Number of Module Clocks in clks[]
  81. * @last_dt_core_clk: ID of the last Core Clock exported to DT
  82. */
  83. struct cpg_mssr_priv {
  84. struct device *dev;
  85. void __iomem *base;
  86. spinlock_t mstp_lock;
  87. struct clk **clks;
  88. unsigned int num_core_clks;
  89. unsigned int num_mod_clks;
  90. unsigned int last_dt_core_clk;
  91. };
  92. /**
  93. * struct mstp_clock - MSTP gating clock
  94. * @hw: handle between common and hardware-specific interfaces
  95. * @index: MSTP clock number
  96. * @priv: CPG/MSSR private data
  97. */
  98. struct mstp_clock {
  99. struct clk_hw hw;
  100. u32 index;
  101. struct cpg_mssr_priv *priv;
  102. };
  103. #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
  104. static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable)
  105. {
  106. struct mstp_clock *clock = to_mstp_clock(hw);
  107. struct cpg_mssr_priv *priv = clock->priv;
  108. unsigned int reg = clock->index / 32;
  109. unsigned int bit = clock->index % 32;
  110. struct device *dev = priv->dev;
  111. u32 bitmask = BIT(bit);
  112. unsigned long flags;
  113. unsigned int i;
  114. u32 value;
  115. dev_dbg(dev, "MSTP %u%02u/%pC %s\n", reg, bit, hw->clk,
  116. enable ? "ON" : "OFF");
  117. spin_lock_irqsave(&priv->mstp_lock, flags);
  118. value = clk_readl(priv->base + SMSTPCR(reg));
  119. if (enable)
  120. value &= ~bitmask;
  121. else
  122. value |= bitmask;
  123. clk_writel(value, priv->base + SMSTPCR(reg));
  124. spin_unlock_irqrestore(&priv->mstp_lock, flags);
  125. if (!enable)
  126. return 0;
  127. for (i = 1000; i > 0; --i) {
  128. if (!(clk_readl(priv->base + MSTPSR(reg)) &
  129. 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 = clk_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;
  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. WARN_DEBUG(core->parent >= priv->num_core_clks);
  220. parent = priv->clks[core->parent];
  221. if (IS_ERR(parent)) {
  222. clk = parent;
  223. goto fail;
  224. }
  225. parent_name = __clk_get_name(parent);
  226. if (core->type == CLK_TYPE_FF) {
  227. clk = clk_register_fixed_factor(NULL, core->name,
  228. parent_name, 0,
  229. core->mult, core->div);
  230. } else {
  231. clk = cpg_div6_register(core->name, 1, &parent_name,
  232. priv->base + core->offset);
  233. }
  234. break;
  235. default:
  236. if (info->cpg_clk_register)
  237. clk = info->cpg_clk_register(dev, core, info,
  238. priv->clks, priv->base);
  239. else
  240. dev_err(dev, "%s has unsupported core clock type %u\n",
  241. core->name, core->type);
  242. break;
  243. }
  244. if (IS_ERR_OR_NULL(clk))
  245. goto fail;
  246. dev_dbg(dev, "Core clock %pC at %pCr Hz\n", clk, clk);
  247. priv->clks[id] = clk;
  248. return;
  249. fail:
  250. dev_err(dev, "Failed to register %s clock %s: %ld\n", "core,",
  251. core->name, PTR_ERR(clk));
  252. }
  253. static void __init cpg_mssr_register_mod_clk(const struct mssr_mod_clk *mod,
  254. const struct cpg_mssr_info *info,
  255. struct cpg_mssr_priv *priv)
  256. {
  257. struct mstp_clock *clock = NULL;
  258. struct device *dev = priv->dev;
  259. unsigned int id = mod->id;
  260. struct clk_init_data init;
  261. struct clk *parent, *clk;
  262. const char *parent_name;
  263. unsigned int i;
  264. WARN_DEBUG(id < priv->num_core_clks);
  265. WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks);
  266. WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks);
  267. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  268. parent = priv->clks[mod->parent];
  269. if (IS_ERR(parent)) {
  270. clk = parent;
  271. goto fail;
  272. }
  273. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  274. if (!clock) {
  275. clk = ERR_PTR(-ENOMEM);
  276. goto fail;
  277. }
  278. init.name = mod->name;
  279. init.ops = &cpg_mstp_clock_ops;
  280. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  281. for (i = 0; i < info->num_crit_mod_clks; i++)
  282. if (id == info->crit_mod_clks[i]) {
  283. #ifdef CLK_ENABLE_HAND_OFF
  284. dev_dbg(dev, "MSTP %s setting CLK_ENABLE_HAND_OFF\n",
  285. mod->name);
  286. init.flags |= CLK_ENABLE_HAND_OFF;
  287. break;
  288. #else
  289. dev_dbg(dev, "Ignoring MSTP %s to prevent disabling\n",
  290. mod->name);
  291. kfree(clock);
  292. return;
  293. #endif
  294. }
  295. parent_name = __clk_get_name(parent);
  296. init.parent_names = &parent_name;
  297. init.num_parents = 1;
  298. clock->index = id - priv->num_core_clks;
  299. clock->priv = priv;
  300. clock->hw.init = &init;
  301. clk = clk_register(NULL, &clock->hw);
  302. if (IS_ERR(clk))
  303. goto fail;
  304. dev_dbg(dev, "Module clock %pC at %pCr Hz\n", clk, clk);
  305. priv->clks[id] = clk;
  306. return;
  307. fail:
  308. dev_err(dev, "Failed to register %s clock %s: %ld\n", "module,",
  309. mod->name, PTR_ERR(clk));
  310. kfree(clock);
  311. }
  312. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  313. struct cpg_mssr_clk_domain {
  314. struct generic_pm_domain genpd;
  315. struct device_node *np;
  316. unsigned int num_core_pm_clks;
  317. unsigned int core_pm_clks[0];
  318. };
  319. static bool cpg_mssr_is_pm_clk(const struct of_phandle_args *clkspec,
  320. struct cpg_mssr_clk_domain *pd)
  321. {
  322. unsigned int i;
  323. if (clkspec->np != pd->np || clkspec->args_count != 2)
  324. return false;
  325. switch (clkspec->args[0]) {
  326. case CPG_CORE:
  327. for (i = 0; i < pd->num_core_pm_clks; i++)
  328. if (clkspec->args[1] == pd->core_pm_clks[i])
  329. return true;
  330. return false;
  331. case CPG_MOD:
  332. return true;
  333. default:
  334. return false;
  335. }
  336. }
  337. static int cpg_mssr_attach_dev(struct generic_pm_domain *genpd,
  338. struct device *dev)
  339. {
  340. struct cpg_mssr_clk_domain *pd =
  341. container_of(genpd, struct cpg_mssr_clk_domain, genpd);
  342. struct device_node *np = dev->of_node;
  343. struct of_phandle_args clkspec;
  344. struct clk *clk;
  345. int i = 0;
  346. int error;
  347. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  348. &clkspec)) {
  349. if (cpg_mssr_is_pm_clk(&clkspec, pd))
  350. goto found;
  351. of_node_put(clkspec.np);
  352. i++;
  353. }
  354. return 0;
  355. found:
  356. clk = of_clk_get_from_provider(&clkspec);
  357. of_node_put(clkspec.np);
  358. if (IS_ERR(clk))
  359. return PTR_ERR(clk);
  360. error = pm_clk_create(dev);
  361. if (error) {
  362. dev_err(dev, "pm_clk_create failed %d\n", error);
  363. goto fail_put;
  364. }
  365. error = pm_clk_add_clk(dev, clk);
  366. if (error) {
  367. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  368. goto fail_destroy;
  369. }
  370. return 0;
  371. fail_destroy:
  372. pm_clk_destroy(dev);
  373. fail_put:
  374. clk_put(clk);
  375. return error;
  376. }
  377. static void cpg_mssr_detach_dev(struct generic_pm_domain *genpd,
  378. struct device *dev)
  379. {
  380. if (!list_empty(&dev->power.subsys_data->clock_list))
  381. pm_clk_destroy(dev);
  382. }
  383. static int __init cpg_mssr_add_clk_domain(struct device *dev,
  384. const unsigned int *core_pm_clks,
  385. unsigned int num_core_pm_clks)
  386. {
  387. struct device_node *np = dev->of_node;
  388. struct generic_pm_domain *genpd;
  389. struct cpg_mssr_clk_domain *pd;
  390. size_t pm_size = num_core_pm_clks * sizeof(core_pm_clks[0]);
  391. pd = devm_kzalloc(dev, sizeof(*pd) + pm_size, GFP_KERNEL);
  392. if (!pd)
  393. return -ENOMEM;
  394. pd->np = np;
  395. pd->num_core_pm_clks = num_core_pm_clks;
  396. memcpy(pd->core_pm_clks, core_pm_clks, pm_size);
  397. genpd = &pd->genpd;
  398. genpd->name = np->name;
  399. genpd->flags = GENPD_FLAG_PM_CLK;
  400. pm_genpd_init(genpd, &simple_qos_governor, false);
  401. genpd->attach_dev = cpg_mssr_attach_dev;
  402. genpd->detach_dev = cpg_mssr_detach_dev;
  403. of_genpd_add_provider_simple(np, genpd);
  404. return 0;
  405. }
  406. #else
  407. static inline int cpg_mssr_add_clk_domain(struct device *dev,
  408. const unsigned int *core_pm_clks,
  409. unsigned int num_core_pm_clks)
  410. {
  411. return 0;
  412. }
  413. #endif /* !CONFIG_PM_GENERIC_DOMAINS_OF */
  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. { /* sentinel */ }
  422. };
  423. static void cpg_mssr_del_clk_provider(void *data)
  424. {
  425. of_clk_del_provider(data);
  426. }
  427. static int __init cpg_mssr_probe(struct platform_device *pdev)
  428. {
  429. struct device *dev = &pdev->dev;
  430. struct device_node *np = dev->of_node;
  431. const struct cpg_mssr_info *info;
  432. struct cpg_mssr_priv *priv;
  433. unsigned int nclks, i;
  434. struct resource *res;
  435. struct clk **clks;
  436. int error;
  437. info = of_match_node(cpg_mssr_match, np)->data;
  438. if (info->init) {
  439. error = info->init(dev);
  440. if (error)
  441. return error;
  442. }
  443. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  444. if (!priv)
  445. return -ENOMEM;
  446. priv->dev = dev;
  447. spin_lock_init(&priv->mstp_lock);
  448. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  449. priv->base = devm_ioremap_resource(dev, res);
  450. if (IS_ERR(priv->base))
  451. return PTR_ERR(priv->base);
  452. nclks = info->num_total_core_clks + info->num_hw_mod_clks;
  453. clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL);
  454. if (!clks)
  455. return -ENOMEM;
  456. priv->clks = clks;
  457. priv->num_core_clks = info->num_total_core_clks;
  458. priv->num_mod_clks = info->num_hw_mod_clks;
  459. priv->last_dt_core_clk = info->last_dt_core_clk;
  460. for (i = 0; i < nclks; i++)
  461. clks[i] = ERR_PTR(-ENOENT);
  462. for (i = 0; i < info->num_core_clks; i++)
  463. cpg_mssr_register_core_clk(&info->core_clks[i], info, priv);
  464. for (i = 0; i < info->num_mod_clks; i++)
  465. cpg_mssr_register_mod_clk(&info->mod_clks[i], info, priv);
  466. error = of_clk_add_provider(np, cpg_mssr_clk_src_twocell_get, priv);
  467. if (error)
  468. return error;
  469. error = devm_add_action_or_reset(dev,
  470. cpg_mssr_del_clk_provider,
  471. np);
  472. if (error)
  473. return error;
  474. error = cpg_mssr_add_clk_domain(dev, info->core_pm_clks,
  475. info->num_core_pm_clks);
  476. if (error)
  477. return error;
  478. return 0;
  479. }
  480. static struct platform_driver cpg_mssr_driver = {
  481. .driver = {
  482. .name = "renesas-cpg-mssr",
  483. .of_match_table = cpg_mssr_match,
  484. },
  485. };
  486. static int __init cpg_mssr_init(void)
  487. {
  488. return platform_driver_probe(&cpg_mssr_driver, cpg_mssr_probe);
  489. }
  490. subsys_initcall(cpg_mssr_init);
  491. MODULE_DESCRIPTION("Renesas CPG/MSSR Driver");
  492. MODULE_LICENSE("GPL v2");