renesas-cpg-mssr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/init.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/module.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_clock.h>
  27. #include <linux/pm_domain.h>
  28. #include <linux/reset-controller.h>
  29. #include <linux/slab.h>
  30. #include <dt-bindings/clock/renesas-cpg-mssr.h>
  31. #include "renesas-cpg-mssr.h"
  32. #include "clk-div6.h"
  33. #ifdef DEBUG
  34. #define WARN_DEBUG(x) WARN_ON(x)
  35. #else
  36. #define WARN_DEBUG(x) do { } while (0)
  37. #endif
  38. /*
  39. * Module Standby and Software Reset register offets.
  40. *
  41. * If the registers exist, these are valid for SH-Mobile, R-Mobile,
  42. * R-Car Gen2, R-Car Gen3, and RZ/G1.
  43. * These are NOT valid for R-Car Gen1 and RZ/A1!
  44. */
  45. /*
  46. * Module Stop Status Register offsets
  47. */
  48. static const u16 mstpsr[] = {
  49. 0x030, 0x038, 0x040, 0x048, 0x04C, 0x03C, 0x1C0, 0x1C4,
  50. 0x9A0, 0x9A4, 0x9A8, 0x9AC,
  51. };
  52. #define MSTPSR(i) mstpsr[i]
  53. /*
  54. * System Module Stop Control Register offsets
  55. */
  56. static const u16 smstpcr[] = {
  57. 0x130, 0x134, 0x138, 0x13C, 0x140, 0x144, 0x148, 0x14C,
  58. 0x990, 0x994, 0x998, 0x99C,
  59. };
  60. #define SMSTPCR(i) smstpcr[i]
  61. /*
  62. * Software Reset Register offsets
  63. */
  64. static const u16 srcr[] = {
  65. 0x0A0, 0x0A8, 0x0B0, 0x0B8, 0x0BC, 0x0C4, 0x1C8, 0x1CC,
  66. 0x920, 0x924, 0x928, 0x92C,
  67. };
  68. #define SRCR(i) srcr[i]
  69. /* Realtime Module Stop Control Register offsets */
  70. #define RMSTPCR(i) (smstpcr[i] - 0x20)
  71. /* Modem Module Stop Control Register offsets (r8a73a4) */
  72. #define MMSTPCR(i) (smstpcr[i] + 0x20)
  73. /* Software Reset Clearing Register offsets */
  74. #define SRSTCLR(i) (0x940 + (i) * 4)
  75. /**
  76. * Clock Pulse Generator / Module Standby and Software Reset Private Data
  77. *
  78. * @rcdev: Optional reset controller entity
  79. * @dev: CPG/MSSR device
  80. * @base: CPG/MSSR register block base address
  81. * @rmw_lock: protects RMW register accesses
  82. * @clks: Array containing all Core and Module Clocks
  83. * @num_core_clks: Number of Core Clocks in clks[]
  84. * @num_mod_clks: Number of Module Clocks in clks[]
  85. * @last_dt_core_clk: ID of the last Core Clock exported to DT
  86. */
  87. struct cpg_mssr_priv {
  88. #ifdef CONFIG_RESET_CONTROLLER
  89. struct reset_controller_dev rcdev;
  90. #endif
  91. struct device *dev;
  92. void __iomem *base;
  93. spinlock_t rmw_lock;
  94. struct clk **clks;
  95. unsigned int num_core_clks;
  96. unsigned int num_mod_clks;
  97. unsigned int last_dt_core_clk;
  98. };
  99. /**
  100. * struct mstp_clock - MSTP gating clock
  101. * @hw: handle between common and hardware-specific interfaces
  102. * @index: MSTP clock number
  103. * @priv: CPG/MSSR private data
  104. */
  105. struct mstp_clock {
  106. struct clk_hw hw;
  107. u32 index;
  108. struct cpg_mssr_priv *priv;
  109. };
  110. #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
  111. static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable)
  112. {
  113. struct mstp_clock *clock = to_mstp_clock(hw);
  114. struct cpg_mssr_priv *priv = clock->priv;
  115. unsigned int reg = clock->index / 32;
  116. unsigned int bit = clock->index % 32;
  117. struct device *dev = priv->dev;
  118. u32 bitmask = BIT(bit);
  119. unsigned long flags;
  120. unsigned int i;
  121. u32 value;
  122. dev_dbg(dev, "MSTP %u%02u/%pC %s\n", reg, bit, hw->clk,
  123. enable ? "ON" : "OFF");
  124. spin_lock_irqsave(&priv->rmw_lock, flags);
  125. value = readl(priv->base + SMSTPCR(reg));
  126. if (enable)
  127. value &= ~bitmask;
  128. else
  129. value |= bitmask;
  130. writel(value, priv->base + SMSTPCR(reg));
  131. spin_unlock_irqrestore(&priv->rmw_lock, flags);
  132. if (!enable)
  133. return 0;
  134. for (i = 1000; i > 0; --i) {
  135. if (!(readl(priv->base + MSTPSR(reg)) & bitmask))
  136. break;
  137. cpu_relax();
  138. }
  139. if (!i) {
  140. dev_err(dev, "Failed to enable SMSTP %p[%d]\n",
  141. priv->base + SMSTPCR(reg), bit);
  142. return -ETIMEDOUT;
  143. }
  144. return 0;
  145. }
  146. static int cpg_mstp_clock_enable(struct clk_hw *hw)
  147. {
  148. return cpg_mstp_clock_endisable(hw, true);
  149. }
  150. static void cpg_mstp_clock_disable(struct clk_hw *hw)
  151. {
  152. cpg_mstp_clock_endisable(hw, false);
  153. }
  154. static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
  155. {
  156. struct mstp_clock *clock = to_mstp_clock(hw);
  157. struct cpg_mssr_priv *priv = clock->priv;
  158. u32 value;
  159. value = readl(priv->base + MSTPSR(clock->index / 32));
  160. return !(value & BIT(clock->index % 32));
  161. }
  162. static const struct clk_ops cpg_mstp_clock_ops = {
  163. .enable = cpg_mstp_clock_enable,
  164. .disable = cpg_mstp_clock_disable,
  165. .is_enabled = cpg_mstp_clock_is_enabled,
  166. };
  167. static
  168. struct clk *cpg_mssr_clk_src_twocell_get(struct of_phandle_args *clkspec,
  169. void *data)
  170. {
  171. unsigned int clkidx = clkspec->args[1];
  172. struct cpg_mssr_priv *priv = data;
  173. struct device *dev = priv->dev;
  174. unsigned int idx;
  175. const char *type;
  176. struct clk *clk;
  177. switch (clkspec->args[0]) {
  178. case CPG_CORE:
  179. type = "core";
  180. if (clkidx > priv->last_dt_core_clk) {
  181. dev_err(dev, "Invalid %s clock index %u\n", type,
  182. clkidx);
  183. return ERR_PTR(-EINVAL);
  184. }
  185. clk = priv->clks[clkidx];
  186. break;
  187. case CPG_MOD:
  188. type = "module";
  189. idx = MOD_CLK_PACK(clkidx);
  190. if (clkidx % 100 > 31 || idx >= priv->num_mod_clks) {
  191. dev_err(dev, "Invalid %s clock index %u\n", type,
  192. clkidx);
  193. return ERR_PTR(-EINVAL);
  194. }
  195. clk = priv->clks[priv->num_core_clks + idx];
  196. break;
  197. default:
  198. dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]);
  199. return ERR_PTR(-EINVAL);
  200. }
  201. if (IS_ERR(clk))
  202. dev_err(dev, "Cannot get %s clock %u: %ld", type, clkidx,
  203. PTR_ERR(clk));
  204. else
  205. dev_dbg(dev, "clock (%u, %u) is %pC at %lu Hz\n",
  206. clkspec->args[0], clkspec->args[1], clk,
  207. clk_get_rate(clk));
  208. return clk;
  209. }
  210. static void __init cpg_mssr_register_core_clk(const struct cpg_core_clk *core,
  211. const struct cpg_mssr_info *info,
  212. struct cpg_mssr_priv *priv)
  213. {
  214. struct clk *clk = ERR_PTR(-ENOTSUPP), *parent;
  215. struct device *dev = priv->dev;
  216. unsigned int id = core->id, div = core->div;
  217. const char *parent_name;
  218. WARN_DEBUG(id >= priv->num_core_clks);
  219. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  220. if (!core->name) {
  221. /* Skip NULLified clock */
  222. return;
  223. }
  224. switch (core->type) {
  225. case CLK_TYPE_IN:
  226. clk = of_clk_get_by_name(priv->dev->of_node, core->name);
  227. break;
  228. case CLK_TYPE_FF:
  229. case CLK_TYPE_DIV6P1:
  230. case CLK_TYPE_DIV6_RO:
  231. WARN_DEBUG(core->parent >= priv->num_core_clks);
  232. parent = priv->clks[core->parent];
  233. if (IS_ERR(parent)) {
  234. clk = parent;
  235. goto fail;
  236. }
  237. parent_name = __clk_get_name(parent);
  238. if (core->type == CLK_TYPE_DIV6_RO)
  239. /* Multiply with the DIV6 register value */
  240. div *= (readl(priv->base + core->offset) & 0x3f) + 1;
  241. if (core->type == CLK_TYPE_DIV6P1) {
  242. clk = cpg_div6_register(core->name, 1, &parent_name,
  243. priv->base + core->offset);
  244. } else {
  245. clk = clk_register_fixed_factor(NULL, core->name,
  246. parent_name, 0,
  247. core->mult, div);
  248. }
  249. break;
  250. default:
  251. if (info->cpg_clk_register)
  252. clk = info->cpg_clk_register(dev, core, info,
  253. priv->clks, priv->base);
  254. else
  255. dev_err(dev, "%s has unsupported core clock type %u\n",
  256. core->name, core->type);
  257. break;
  258. }
  259. if (IS_ERR_OR_NULL(clk))
  260. goto fail;
  261. dev_dbg(dev, "Core clock %pC at %lu Hz\n", clk, clk_get_rate(clk));
  262. priv->clks[id] = clk;
  263. return;
  264. fail:
  265. dev_err(dev, "Failed to register %s clock %s: %ld\n", "core",
  266. core->name, PTR_ERR(clk));
  267. }
  268. static void __init cpg_mssr_register_mod_clk(const struct mssr_mod_clk *mod,
  269. const struct cpg_mssr_info *info,
  270. struct cpg_mssr_priv *priv)
  271. {
  272. struct mstp_clock *clock = NULL;
  273. struct device *dev = priv->dev;
  274. unsigned int id = mod->id;
  275. struct clk_init_data init;
  276. struct clk *parent, *clk;
  277. const char *parent_name;
  278. unsigned int i;
  279. WARN_DEBUG(id < priv->num_core_clks);
  280. WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks);
  281. WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks);
  282. WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
  283. if (!mod->name) {
  284. /* Skip NULLified clock */
  285. return;
  286. }
  287. parent = priv->clks[mod->parent];
  288. if (IS_ERR(parent)) {
  289. clk = parent;
  290. goto fail;
  291. }
  292. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  293. if (!clock) {
  294. clk = ERR_PTR(-ENOMEM);
  295. goto fail;
  296. }
  297. init.name = mod->name;
  298. init.ops = &cpg_mstp_clock_ops;
  299. init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
  300. for (i = 0; i < info->num_crit_mod_clks; i++)
  301. if (id == info->crit_mod_clks[i]) {
  302. dev_dbg(dev, "MSTP %s setting CLK_IS_CRITICAL\n",
  303. mod->name);
  304. init.flags |= CLK_IS_CRITICAL;
  305. break;
  306. }
  307. parent_name = __clk_get_name(parent);
  308. init.parent_names = &parent_name;
  309. init.num_parents = 1;
  310. clock->index = id - priv->num_core_clks;
  311. clock->priv = priv;
  312. clock->hw.init = &init;
  313. clk = clk_register(NULL, &clock->hw);
  314. if (IS_ERR(clk))
  315. goto fail;
  316. dev_dbg(dev, "Module clock %pC at %lu Hz\n", clk, clk_get_rate(clk));
  317. priv->clks[id] = clk;
  318. return;
  319. fail:
  320. dev_err(dev, "Failed to register %s clock %s: %ld\n", "module",
  321. mod->name, PTR_ERR(clk));
  322. kfree(clock);
  323. }
  324. struct cpg_mssr_clk_domain {
  325. struct generic_pm_domain genpd;
  326. struct device_node *np;
  327. unsigned int num_core_pm_clks;
  328. unsigned int core_pm_clks[0];
  329. };
  330. static struct cpg_mssr_clk_domain *cpg_mssr_clk_domain;
  331. static bool cpg_mssr_is_pm_clk(const struct of_phandle_args *clkspec,
  332. struct cpg_mssr_clk_domain *pd)
  333. {
  334. unsigned int i;
  335. if (clkspec->np != pd->np || clkspec->args_count != 2)
  336. return false;
  337. switch (clkspec->args[0]) {
  338. case CPG_CORE:
  339. for (i = 0; i < pd->num_core_pm_clks; i++)
  340. if (clkspec->args[1] == pd->core_pm_clks[i])
  341. return true;
  342. return false;
  343. case CPG_MOD:
  344. return true;
  345. default:
  346. return false;
  347. }
  348. }
  349. int cpg_mssr_attach_dev(struct generic_pm_domain *unused, struct device *dev)
  350. {
  351. struct cpg_mssr_clk_domain *pd = cpg_mssr_clk_domain;
  352. struct device_node *np = dev->of_node;
  353. struct of_phandle_args clkspec;
  354. struct clk *clk;
  355. int i = 0;
  356. int error;
  357. if (!pd) {
  358. dev_dbg(dev, "CPG/MSSR clock domain not yet available\n");
  359. return -EPROBE_DEFER;
  360. }
  361. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
  362. &clkspec)) {
  363. if (cpg_mssr_is_pm_clk(&clkspec, pd))
  364. goto found;
  365. of_node_put(clkspec.np);
  366. i++;
  367. }
  368. return 0;
  369. found:
  370. clk = of_clk_get_from_provider(&clkspec);
  371. of_node_put(clkspec.np);
  372. if (IS_ERR(clk))
  373. return PTR_ERR(clk);
  374. error = pm_clk_create(dev);
  375. if (error) {
  376. dev_err(dev, "pm_clk_create failed %d\n", error);
  377. goto fail_put;
  378. }
  379. error = pm_clk_add_clk(dev, clk);
  380. if (error) {
  381. dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error);
  382. goto fail_destroy;
  383. }
  384. return 0;
  385. fail_destroy:
  386. pm_clk_destroy(dev);
  387. fail_put:
  388. clk_put(clk);
  389. return error;
  390. }
  391. void cpg_mssr_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  392. {
  393. if (!pm_clk_no_clocks(dev))
  394. pm_clk_destroy(dev);
  395. }
  396. static int __init cpg_mssr_add_clk_domain(struct device *dev,
  397. const unsigned int *core_pm_clks,
  398. unsigned int num_core_pm_clks)
  399. {
  400. struct device_node *np = dev->of_node;
  401. struct generic_pm_domain *genpd;
  402. struct cpg_mssr_clk_domain *pd;
  403. size_t pm_size = num_core_pm_clks * sizeof(core_pm_clks[0]);
  404. pd = devm_kzalloc(dev, sizeof(*pd) + pm_size, GFP_KERNEL);
  405. if (!pd)
  406. return -ENOMEM;
  407. pd->np = np;
  408. pd->num_core_pm_clks = num_core_pm_clks;
  409. memcpy(pd->core_pm_clks, core_pm_clks, pm_size);
  410. genpd = &pd->genpd;
  411. genpd->name = np->name;
  412. genpd->flags = GENPD_FLAG_PM_CLK;
  413. genpd->attach_dev = cpg_mssr_attach_dev;
  414. genpd->detach_dev = cpg_mssr_detach_dev;
  415. pm_genpd_init(genpd, &pm_domain_always_on_gov, false);
  416. cpg_mssr_clk_domain = pd;
  417. of_genpd_add_provider_simple(np, genpd);
  418. return 0;
  419. }
  420. #ifdef CONFIG_RESET_CONTROLLER
  421. #define rcdev_to_priv(x) container_of(x, struct cpg_mssr_priv, rcdev)
  422. static int cpg_mssr_reset(struct reset_controller_dev *rcdev,
  423. unsigned long id)
  424. {
  425. struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev);
  426. unsigned int reg = id / 32;
  427. unsigned int bit = id % 32;
  428. u32 bitmask = BIT(bit);
  429. dev_dbg(priv->dev, "reset %u%02u\n", reg, bit);
  430. /* Reset module */
  431. writel(bitmask, priv->base + SRCR(reg));
  432. /* Wait for at least one cycle of the RCLK clock (@ ca. 32 kHz) */
  433. udelay(35);
  434. /* Release module from reset state */
  435. writel(bitmask, priv->base + SRSTCLR(reg));
  436. return 0;
  437. }
  438. static int cpg_mssr_assert(struct reset_controller_dev *rcdev, unsigned long id)
  439. {
  440. struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev);
  441. unsigned int reg = id / 32;
  442. unsigned int bit = id % 32;
  443. u32 bitmask = BIT(bit);
  444. dev_dbg(priv->dev, "assert %u%02u\n", reg, bit);
  445. writel(bitmask, priv->base + SRCR(reg));
  446. return 0;
  447. }
  448. static int cpg_mssr_deassert(struct reset_controller_dev *rcdev,
  449. unsigned long id)
  450. {
  451. struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev);
  452. unsigned int reg = id / 32;
  453. unsigned int bit = id % 32;
  454. u32 bitmask = BIT(bit);
  455. dev_dbg(priv->dev, "deassert %u%02u\n", reg, bit);
  456. writel(bitmask, priv->base + SRSTCLR(reg));
  457. return 0;
  458. }
  459. static int cpg_mssr_status(struct reset_controller_dev *rcdev,
  460. unsigned long id)
  461. {
  462. struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev);
  463. unsigned int reg = id / 32;
  464. unsigned int bit = id % 32;
  465. u32 bitmask = BIT(bit);
  466. return !!(readl(priv->base + SRCR(reg)) & bitmask);
  467. }
  468. static const struct reset_control_ops cpg_mssr_reset_ops = {
  469. .reset = cpg_mssr_reset,
  470. .assert = cpg_mssr_assert,
  471. .deassert = cpg_mssr_deassert,
  472. .status = cpg_mssr_status,
  473. };
  474. static int cpg_mssr_reset_xlate(struct reset_controller_dev *rcdev,
  475. const struct of_phandle_args *reset_spec)
  476. {
  477. struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev);
  478. unsigned int unpacked = reset_spec->args[0];
  479. unsigned int idx = MOD_CLK_PACK(unpacked);
  480. if (unpacked % 100 > 31 || idx >= rcdev->nr_resets) {
  481. dev_err(priv->dev, "Invalid reset index %u\n", unpacked);
  482. return -EINVAL;
  483. }
  484. return idx;
  485. }
  486. static int cpg_mssr_reset_controller_register(struct cpg_mssr_priv *priv)
  487. {
  488. priv->rcdev.ops = &cpg_mssr_reset_ops;
  489. priv->rcdev.of_node = priv->dev->of_node;
  490. priv->rcdev.of_reset_n_cells = 1;
  491. priv->rcdev.of_xlate = cpg_mssr_reset_xlate;
  492. priv->rcdev.nr_resets = priv->num_mod_clks;
  493. return devm_reset_controller_register(priv->dev, &priv->rcdev);
  494. }
  495. #else /* !CONFIG_RESET_CONTROLLER */
  496. static inline int cpg_mssr_reset_controller_register(struct cpg_mssr_priv *priv)
  497. {
  498. return 0;
  499. }
  500. #endif /* !CONFIG_RESET_CONTROLLER */
  501. static const struct of_device_id cpg_mssr_match[] = {
  502. #ifdef CONFIG_CLK_R8A7743
  503. {
  504. .compatible = "renesas,r8a7743-cpg-mssr",
  505. .data = &r8a7743_cpg_mssr_info,
  506. },
  507. #endif
  508. #ifdef CONFIG_CLK_R8A7745
  509. {
  510. .compatible = "renesas,r8a7745-cpg-mssr",
  511. .data = &r8a7745_cpg_mssr_info,
  512. },
  513. #endif
  514. #ifdef CONFIG_CLK_R8A7790
  515. {
  516. .compatible = "renesas,r8a7790-cpg-mssr",
  517. .data = &r8a7790_cpg_mssr_info,
  518. },
  519. #endif
  520. #ifdef CONFIG_CLK_R8A7791
  521. {
  522. .compatible = "renesas,r8a7791-cpg-mssr",
  523. .data = &r8a7791_cpg_mssr_info,
  524. },
  525. /* R-Car M2-N is (almost) identical to R-Car M2-W w.r.t. clocks. */
  526. {
  527. .compatible = "renesas,r8a7793-cpg-mssr",
  528. .data = &r8a7791_cpg_mssr_info,
  529. },
  530. #endif
  531. #ifdef CONFIG_CLK_R8A7792
  532. {
  533. .compatible = "renesas,r8a7792-cpg-mssr",
  534. .data = &r8a7792_cpg_mssr_info,
  535. },
  536. #endif
  537. #ifdef CONFIG_CLK_R8A7794
  538. {
  539. .compatible = "renesas,r8a7794-cpg-mssr",
  540. .data = &r8a7794_cpg_mssr_info,
  541. },
  542. #endif
  543. #ifdef CONFIG_CLK_R8A7795
  544. {
  545. .compatible = "renesas,r8a7795-cpg-mssr",
  546. .data = &r8a7795_cpg_mssr_info,
  547. },
  548. #endif
  549. #ifdef CONFIG_CLK_R8A7796
  550. {
  551. .compatible = "renesas,r8a7796-cpg-mssr",
  552. .data = &r8a7796_cpg_mssr_info,
  553. },
  554. #endif
  555. #ifdef CONFIG_CLK_R8A77995
  556. {
  557. .compatible = "renesas,r8a77995-cpg-mssr",
  558. .data = &r8a77995_cpg_mssr_info,
  559. },
  560. #endif
  561. { /* sentinel */ }
  562. };
  563. static void cpg_mssr_del_clk_provider(void *data)
  564. {
  565. of_clk_del_provider(data);
  566. }
  567. static int __init cpg_mssr_probe(struct platform_device *pdev)
  568. {
  569. struct device *dev = &pdev->dev;
  570. struct device_node *np = dev->of_node;
  571. const struct cpg_mssr_info *info;
  572. struct cpg_mssr_priv *priv;
  573. unsigned int nclks, i;
  574. struct resource *res;
  575. struct clk **clks;
  576. int error;
  577. info = of_device_get_match_data(dev);
  578. if (info->init) {
  579. error = info->init(dev);
  580. if (error)
  581. return error;
  582. }
  583. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  584. if (!priv)
  585. return -ENOMEM;
  586. priv->dev = dev;
  587. spin_lock_init(&priv->rmw_lock);
  588. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  589. priv->base = devm_ioremap_resource(dev, res);
  590. if (IS_ERR(priv->base))
  591. return PTR_ERR(priv->base);
  592. nclks = info->num_total_core_clks + info->num_hw_mod_clks;
  593. clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL);
  594. if (!clks)
  595. return -ENOMEM;
  596. priv->clks = clks;
  597. priv->num_core_clks = info->num_total_core_clks;
  598. priv->num_mod_clks = info->num_hw_mod_clks;
  599. priv->last_dt_core_clk = info->last_dt_core_clk;
  600. for (i = 0; i < nclks; i++)
  601. clks[i] = ERR_PTR(-ENOENT);
  602. for (i = 0; i < info->num_core_clks; i++)
  603. cpg_mssr_register_core_clk(&info->core_clks[i], info, priv);
  604. for (i = 0; i < info->num_mod_clks; i++)
  605. cpg_mssr_register_mod_clk(&info->mod_clks[i], info, priv);
  606. error = of_clk_add_provider(np, cpg_mssr_clk_src_twocell_get, priv);
  607. if (error)
  608. return error;
  609. error = devm_add_action_or_reset(dev,
  610. cpg_mssr_del_clk_provider,
  611. np);
  612. if (error)
  613. return error;
  614. error = cpg_mssr_add_clk_domain(dev, info->core_pm_clks,
  615. info->num_core_pm_clks);
  616. if (error)
  617. return error;
  618. error = cpg_mssr_reset_controller_register(priv);
  619. if (error)
  620. return error;
  621. return 0;
  622. }
  623. static struct platform_driver cpg_mssr_driver = {
  624. .driver = {
  625. .name = "renesas-cpg-mssr",
  626. .of_match_table = cpg_mssr_match,
  627. },
  628. };
  629. static int __init cpg_mssr_init(void)
  630. {
  631. return platform_driver_probe(&cpg_mssr_driver, cpg_mssr_probe);
  632. }
  633. subsys_initcall(cpg_mssr_init);
  634. void __init cpg_core_nullify_range(struct cpg_core_clk *core_clks,
  635. unsigned int num_core_clks,
  636. unsigned int first_clk,
  637. unsigned int last_clk)
  638. {
  639. unsigned int i;
  640. for (i = 0; i < num_core_clks; i++)
  641. if (core_clks[i].id >= first_clk &&
  642. core_clks[i].id <= last_clk)
  643. core_clks[i].name = NULL;
  644. }
  645. void __init mssr_mod_nullify(struct mssr_mod_clk *mod_clks,
  646. unsigned int num_mod_clks,
  647. const unsigned int *clks, unsigned int n)
  648. {
  649. unsigned int i, j;
  650. for (i = 0, j = 0; i < num_mod_clks && j < n; i++)
  651. if (mod_clks[i].id == clks[j]) {
  652. mod_clks[i].name = NULL;
  653. j++;
  654. }
  655. }
  656. void __init mssr_mod_reparent(struct mssr_mod_clk *mod_clks,
  657. unsigned int num_mod_clks,
  658. const struct mssr_mod_reparent *clks,
  659. unsigned int n)
  660. {
  661. unsigned int i, j;
  662. for (i = 0, j = 0; i < num_mod_clks && j < n; i++)
  663. if (mod_clks[i].id == clks[j].clk) {
  664. mod_clks[i].parent = clks[j].parent;
  665. j++;
  666. }
  667. }
  668. MODULE_DESCRIPTION("Renesas CPG/MSSR Driver");
  669. MODULE_LICENSE("GPL v2");