Lines Matching +full:can +full:- +full:r8a7791
1 // SPDX-License-Identifier: GPL-2.0
7 * Based on clk-mstp.c, clk-rcar-gen2.c, and clk-rcar-gen3.c
14 #include <linux/clk-provider.h>
28 #include <linux/reset-controller.h>
31 #include <dt-bindings/clock/renesas-cpg-mssr.h>
33 #include "renesas-cpg-mssr.h"
34 #include "clk-div6.h"
46 * If the registers exist, these are valid for SH-Mobile, R-Mobile,
47 * R-Car Gen2, R-Car Gen3, and RZ/G1.
48 * These are NOT valid for R-Car Gen1 and RZ/A1!
126 * struct cpg_mssr_priv - Clock Pulse Generator / Module Standby
177 * struct mstp_clock - MSTP gating clock
178 * @hw: handle between common and hardware-specific interfaces
193 struct cpg_mssr_priv *priv = clock->priv; in cpg_mstp_clock_endisable()
194 unsigned int reg = clock->index / 32; in cpg_mstp_clock_endisable()
195 unsigned int bit = clock->index % 32; in cpg_mstp_clock_endisable()
196 struct device *dev = priv->dev; in cpg_mstp_clock_endisable()
202 dev_dbg(dev, "MSTP %u%02u/%pC %s\n", reg, bit, hw->clk, in cpg_mstp_clock_endisable()
204 spin_lock_irqsave(&priv->rmw_lock, flags); in cpg_mstp_clock_endisable()
206 if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) { in cpg_mstp_clock_endisable()
207 value = readb(priv->base + priv->control_regs[reg]); in cpg_mstp_clock_endisable()
212 writeb(value, priv->base + priv->control_regs[reg]); in cpg_mstp_clock_endisable()
215 readb(priv->base + priv->control_regs[reg]); in cpg_mstp_clock_endisable()
216 barrier_data(priv->base + priv->control_regs[reg]); in cpg_mstp_clock_endisable()
218 value = readl(priv->base + priv->control_regs[reg]); in cpg_mstp_clock_endisable()
223 writel(value, priv->base + priv->control_regs[reg]); in cpg_mstp_clock_endisable()
226 spin_unlock_irqrestore(&priv->rmw_lock, flags); in cpg_mstp_clock_endisable()
228 if (!enable || priv->reg_layout == CLK_REG_LAYOUT_RZ_A) in cpg_mstp_clock_endisable()
231 for (i = 1000; i > 0; --i) { in cpg_mstp_clock_endisable()
232 if (!(readl(priv->base + priv->status_regs[reg]) & bitmask)) in cpg_mstp_clock_endisable()
239 priv->base + priv->control_regs[reg], bit); in cpg_mstp_clock_endisable()
240 return -ETIMEDOUT; in cpg_mstp_clock_endisable()
259 struct cpg_mssr_priv *priv = clock->priv; in cpg_mstp_clock_is_enabled()
262 if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) in cpg_mstp_clock_is_enabled()
263 value = readb(priv->base + priv->control_regs[clock->index / 32]); in cpg_mstp_clock_is_enabled()
265 value = readl(priv->base + priv->status_regs[clock->index / 32]); in cpg_mstp_clock_is_enabled()
267 return !(value & BIT(clock->index % 32)); in cpg_mstp_clock_is_enabled()
280 unsigned int clkidx = clkspec->args[1]; in cpg_mssr_clk_src_twocell_get()
282 struct device *dev = priv->dev; in cpg_mssr_clk_src_twocell_get()
288 switch (clkspec->args[0]) { in cpg_mssr_clk_src_twocell_get()
291 if (clkidx > priv->last_dt_core_clk) { in cpg_mssr_clk_src_twocell_get()
294 return ERR_PTR(-EINVAL); in cpg_mssr_clk_src_twocell_get()
296 clk = priv->clks[clkidx]; in cpg_mssr_clk_src_twocell_get()
301 if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) { in cpg_mssr_clk_src_twocell_get()
303 range_check = 7 - (clkidx % 10); in cpg_mssr_clk_src_twocell_get()
306 range_check = 31 - (clkidx % 100); in cpg_mssr_clk_src_twocell_get()
308 if (range_check < 0 || idx >= priv->num_mod_clks) { in cpg_mssr_clk_src_twocell_get()
311 return ERR_PTR(-EINVAL); in cpg_mssr_clk_src_twocell_get()
313 clk = priv->clks[priv->num_core_clks + idx]; in cpg_mssr_clk_src_twocell_get()
317 dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]); in cpg_mssr_clk_src_twocell_get()
318 return ERR_PTR(-EINVAL); in cpg_mssr_clk_src_twocell_get()
326 clkspec->args[0], clkspec->args[1], clk, in cpg_mssr_clk_src_twocell_get()
335 struct clk *clk = ERR_PTR(-ENOTSUPP), *parent; in cpg_mssr_register_core_clk()
336 struct device *dev = priv->dev; in cpg_mssr_register_core_clk()
337 unsigned int id = core->id, div = core->div; in cpg_mssr_register_core_clk()
340 WARN_DEBUG(id >= priv->num_core_clks); in cpg_mssr_register_core_clk()
341 WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT); in cpg_mssr_register_core_clk()
343 if (!core->name) { in cpg_mssr_register_core_clk()
348 switch (core->type) { in cpg_mssr_register_core_clk()
350 clk = of_clk_get_by_name(priv->np, core->name); in cpg_mssr_register_core_clk()
356 WARN_DEBUG(core->parent >= priv->num_core_clks); in cpg_mssr_register_core_clk()
357 parent = priv->clks[core->parent]; in cpg_mssr_register_core_clk()
365 if (core->type == CLK_TYPE_DIV6_RO) in cpg_mssr_register_core_clk()
367 div *= (readl(priv->base + core->offset) & 0x3f) + 1; in cpg_mssr_register_core_clk()
369 if (core->type == CLK_TYPE_DIV6P1) { in cpg_mssr_register_core_clk()
370 clk = cpg_div6_register(core->name, 1, &parent_name, in cpg_mssr_register_core_clk()
371 priv->base + core->offset, in cpg_mssr_register_core_clk()
372 &priv->notifiers); in cpg_mssr_register_core_clk()
374 clk = clk_register_fixed_factor(NULL, core->name, in cpg_mssr_register_core_clk()
376 core->mult, div); in cpg_mssr_register_core_clk()
381 clk = clk_register_fixed_rate(NULL, core->name, NULL, 0, in cpg_mssr_register_core_clk()
382 core->mult); in cpg_mssr_register_core_clk()
386 if (info->cpg_clk_register) in cpg_mssr_register_core_clk()
387 clk = info->cpg_clk_register(dev, core, info, in cpg_mssr_register_core_clk()
388 priv->clks, priv->base, in cpg_mssr_register_core_clk()
389 &priv->notifiers); in cpg_mssr_register_core_clk()
392 core->name, core->type); in cpg_mssr_register_core_clk()
400 priv->clks[id] = clk; in cpg_mssr_register_core_clk()
405 core->name, PTR_ERR(clk)); in cpg_mssr_register_core_clk()
413 struct device *dev = priv->dev; in cpg_mssr_register_mod_clk()
414 unsigned int id = mod->id; in cpg_mssr_register_mod_clk()
420 WARN_DEBUG(id < priv->num_core_clks); in cpg_mssr_register_mod_clk()
421 WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks); in cpg_mssr_register_mod_clk()
422 WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks); in cpg_mssr_register_mod_clk()
423 WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT); in cpg_mssr_register_mod_clk()
425 if (!mod->name) { in cpg_mssr_register_mod_clk()
430 parent = priv->clks[mod->parent]; in cpg_mssr_register_mod_clk()
438 clk = ERR_PTR(-ENOMEM); in cpg_mssr_register_mod_clk()
442 init.name = mod->name; in cpg_mssr_register_mod_clk()
449 clock->index = id - priv->num_core_clks; in cpg_mssr_register_mod_clk()
450 clock->priv = priv; in cpg_mssr_register_mod_clk()
451 clock->hw.init = &init; in cpg_mssr_register_mod_clk()
453 for (i = 0; i < info->num_crit_mod_clks; i++) in cpg_mssr_register_mod_clk()
454 if (id == info->crit_mod_clks[i] && in cpg_mssr_register_mod_clk()
455 cpg_mstp_clock_is_enabled(&clock->hw)) { in cpg_mssr_register_mod_clk()
457 mod->name); in cpg_mssr_register_mod_clk()
462 clk = clk_register(NULL, &clock->hw); in cpg_mssr_register_mod_clk()
467 priv->clks[id] = clk; in cpg_mssr_register_mod_clk()
468 priv->smstpcr_saved[clock->index / 32].mask |= BIT(clock->index % 32); in cpg_mssr_register_mod_clk()
473 mod->name, PTR_ERR(clk)); in cpg_mssr_register_mod_clk()
490 if (clkspec->np != pd->genpd.dev.of_node || clkspec->args_count != 2) in cpg_mssr_is_pm_clk()
493 switch (clkspec->args[0]) { in cpg_mssr_is_pm_clk()
495 for (i = 0; i < pd->num_core_pm_clks; i++) in cpg_mssr_is_pm_clk()
496 if (clkspec->args[1] == pd->core_pm_clks[i]) in cpg_mssr_is_pm_clk()
511 struct device_node *np = dev->of_node; in cpg_mssr_attach_dev()
519 return -EPROBE_DEFER; in cpg_mssr_attach_dev()
522 while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i, in cpg_mssr_attach_dev()
572 struct device_node *np = dev->of_node; in cpg_mssr_add_clk_domain()
580 return -ENOMEM; in cpg_mssr_add_clk_domain()
582 pd->num_core_pm_clks = num_core_pm_clks; in cpg_mssr_add_clk_domain()
583 memcpy(pd->core_pm_clks, core_pm_clks, pm_size); in cpg_mssr_add_clk_domain()
585 genpd = &pd->genpd; in cpg_mssr_add_clk_domain()
586 genpd->name = np->name; in cpg_mssr_add_clk_domain()
587 genpd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ALWAYS_ON | in cpg_mssr_add_clk_domain()
589 genpd->attach_dev = cpg_mssr_attach_dev; in cpg_mssr_add_clk_domain()
590 genpd->detach_dev = cpg_mssr_detach_dev; in cpg_mssr_add_clk_domain()
616 dev_dbg(priv->dev, "reset %u%02u\n", reg, bit); in cpg_mssr_reset()
619 writel(bitmask, priv->base + priv->reset_regs[reg]); in cpg_mssr_reset()
625 writel(bitmask, priv->base + priv->reset_clear_regs[reg]); in cpg_mssr_reset()
637 dev_dbg(priv->dev, "assert %u%02u\n", reg, bit); in cpg_mssr_assert()
639 writel(bitmask, priv->base + priv->reset_regs[reg]); in cpg_mssr_assert()
651 dev_dbg(priv->dev, "deassert %u%02u\n", reg, bit); in cpg_mssr_deassert()
653 writel(bitmask, priv->base + priv->reset_clear_regs[reg]); in cpg_mssr_deassert()
665 return !!(readl(priv->base + priv->reset_regs[reg]) & bitmask); in cpg_mssr_status()
679 unsigned int unpacked = reset_spec->args[0]; in cpg_mssr_reset_xlate()
682 if (unpacked % 100 > 31 || idx >= rcdev->nr_resets) { in cpg_mssr_reset_xlate()
683 dev_err(priv->dev, "Invalid reset index %u\n", unpacked); in cpg_mssr_reset_xlate()
684 return -EINVAL; in cpg_mssr_reset_xlate()
692 priv->rcdev.ops = &cpg_mssr_reset_ops; in cpg_mssr_reset_controller_register()
693 priv->rcdev.of_node = priv->dev->of_node; in cpg_mssr_reset_controller_register()
694 priv->rcdev.of_reset_n_cells = 1; in cpg_mssr_reset_controller_register()
695 priv->rcdev.of_xlate = cpg_mssr_reset_xlate; in cpg_mssr_reset_controller_register()
696 priv->rcdev.nr_resets = priv->num_mod_clks; in cpg_mssr_reset_controller_register()
697 return devm_reset_controller_register(priv->dev, &priv->rcdev); in cpg_mssr_reset_controller_register()
711 .compatible = "renesas,r7s9210-cpg-mssr",
717 .compatible = "renesas,r8a7742-cpg-mssr",
723 .compatible = "renesas,r8a7743-cpg-mssr",
728 .compatible = "renesas,r8a7744-cpg-mssr",
734 .compatible = "renesas,r8a7745-cpg-mssr",
740 .compatible = "renesas,r8a77470-cpg-mssr",
746 .compatible = "renesas,r8a774a1-cpg-mssr",
752 .compatible = "renesas,r8a774b1-cpg-mssr",
758 .compatible = "renesas,r8a774c0-cpg-mssr",
764 .compatible = "renesas,r8a774e1-cpg-mssr",
770 .compatible = "renesas,r8a7790-cpg-mssr",
776 .compatible = "renesas,r8a7791-cpg-mssr",
779 /* R-Car M2-N is (almost) identical to R-Car M2-W w.r.t. clocks. */
781 .compatible = "renesas,r8a7793-cpg-mssr",
787 .compatible = "renesas,r8a7792-cpg-mssr",
793 .compatible = "renesas,r8a7794-cpg-mssr",
799 .compatible = "renesas,r8a7795-cpg-mssr",
805 .compatible = "renesas,r8a7796-cpg-mssr",
811 .compatible = "renesas,r8a77961-cpg-mssr",
817 .compatible = "renesas,r8a77965-cpg-mssr",
823 .compatible = "renesas,r8a77970-cpg-mssr",
829 .compatible = "renesas,r8a77980-cpg-mssr",
835 .compatible = "renesas,r8a77990-cpg-mssr",
841 .compatible = "renesas,r8a77995-cpg-mssr",
847 .compatible = "renesas,r8a779a0-cpg-mssr",
853 .compatible = "renesas,r8a779f0-cpg-mssr",
859 .compatible = "renesas,r8a779g0-cpg-mssr",
877 /* This is the best we can do to check for the presence of PSCI */ in cpg_mssr_suspend_noirq()
882 for (reg = 0; reg < ARRAY_SIZE(priv->smstpcr_saved); reg++) { in cpg_mssr_suspend_noirq()
883 if (priv->smstpcr_saved[reg].mask) in cpg_mssr_suspend_noirq()
884 priv->smstpcr_saved[reg].val = in cpg_mssr_suspend_noirq()
885 priv->reg_layout == CLK_REG_LAYOUT_RZ_A ? in cpg_mssr_suspend_noirq()
886 readb(priv->base + priv->control_regs[reg]) : in cpg_mssr_suspend_noirq()
887 readl(priv->base + priv->control_regs[reg]); in cpg_mssr_suspend_noirq()
891 raw_notifier_call_chain(&priv->notifiers, PM_EVENT_SUSPEND, NULL); in cpg_mssr_suspend_noirq()
902 /* This is the best we can do to check for the presence of PSCI */ in cpg_mssr_resume_noirq()
907 raw_notifier_call_chain(&priv->notifiers, PM_EVENT_RESUME, NULL); in cpg_mssr_resume_noirq()
910 for (reg = 0; reg < ARRAY_SIZE(priv->smstpcr_saved); reg++) { in cpg_mssr_resume_noirq()
911 mask = priv->smstpcr_saved[reg].mask; in cpg_mssr_resume_noirq()
915 if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) in cpg_mssr_resume_noirq()
916 oldval = readb(priv->base + priv->control_regs[reg]); in cpg_mssr_resume_noirq()
918 oldval = readl(priv->base + priv->control_regs[reg]); in cpg_mssr_resume_noirq()
920 newval |= priv->smstpcr_saved[reg].val & mask; in cpg_mssr_resume_noirq()
924 if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) { in cpg_mssr_resume_noirq()
925 writeb(newval, priv->base + priv->control_regs[reg]); in cpg_mssr_resume_noirq()
927 readb(priv->base + priv->control_regs[reg]); in cpg_mssr_resume_noirq()
928 barrier_data(priv->base + priv->control_regs[reg]); in cpg_mssr_resume_noirq()
931 writel(newval, priv->base + priv->control_regs[reg]); in cpg_mssr_resume_noirq()
934 mask &= ~priv->smstpcr_saved[reg].val; in cpg_mssr_resume_noirq()
938 for (i = 1000; i > 0; --i) { in cpg_mssr_resume_noirq()
939 oldval = readl(priv->base + priv->status_regs[reg]); in cpg_mssr_resume_noirq()
947 priv->reg_layout == CLK_REG_LAYOUT_RZ_A ? in cpg_mssr_resume_noirq()
971 if (info->init) { in cpg_mssr_common_init()
972 error = info->init(dev); in cpg_mssr_common_init()
977 nclks = info->num_total_core_clks + info->num_hw_mod_clks; in cpg_mssr_common_init()
980 return -ENOMEM; in cpg_mssr_common_init()
982 priv->np = np; in cpg_mssr_common_init()
983 priv->dev = dev; in cpg_mssr_common_init()
984 spin_lock_init(&priv->rmw_lock); in cpg_mssr_common_init()
986 priv->base = of_iomap(np, 0); in cpg_mssr_common_init()
987 if (!priv->base) { in cpg_mssr_common_init()
988 error = -ENOMEM; in cpg_mssr_common_init()
993 priv->num_core_clks = info->num_total_core_clks; in cpg_mssr_common_init()
994 priv->num_mod_clks = info->num_hw_mod_clks; in cpg_mssr_common_init()
995 priv->last_dt_core_clk = info->last_dt_core_clk; in cpg_mssr_common_init()
996 RAW_INIT_NOTIFIER_HEAD(&priv->notifiers); in cpg_mssr_common_init()
997 priv->reg_layout = info->reg_layout; in cpg_mssr_common_init()
998 if (priv->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) { in cpg_mssr_common_init()
999 priv->status_regs = mstpsr; in cpg_mssr_common_init()
1000 priv->control_regs = smstpcr; in cpg_mssr_common_init()
1001 priv->reset_regs = srcr; in cpg_mssr_common_init()
1002 priv->reset_clear_regs = srstclr; in cpg_mssr_common_init()
1003 } else if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) { in cpg_mssr_common_init()
1004 priv->control_regs = stbcr; in cpg_mssr_common_init()
1005 } else if (priv->reg_layout == CLK_REG_LAYOUT_RCAR_GEN4) { in cpg_mssr_common_init()
1006 priv->status_regs = mstpsr_for_gen4; in cpg_mssr_common_init()
1007 priv->control_regs = mstpcr_for_gen4; in cpg_mssr_common_init()
1008 priv->reset_regs = srcr_for_gen4; in cpg_mssr_common_init()
1009 priv->reset_clear_regs = srstclr_for_gen4; in cpg_mssr_common_init()
1011 error = -EINVAL; in cpg_mssr_common_init()
1016 priv->clks[i] = ERR_PTR(-ENOENT); in cpg_mssr_common_init()
1025 if (priv->base) in cpg_mssr_common_init()
1026 iounmap(priv->base); in cpg_mssr_common_init()
1042 for (i = 0; i < info->num_early_core_clks; i++) in cpg_mssr_early_init()
1043 cpg_mssr_register_core_clk(&info->early_core_clks[i], info, in cpg_mssr_early_init()
1046 for (i = 0; i < info->num_early_mod_clks; i++) in cpg_mssr_early_init()
1047 cpg_mssr_register_mod_clk(&info->early_mod_clks[i], info, in cpg_mssr_early_init()
1054 struct device *dev = &pdev->dev; in cpg_mssr_probe()
1055 struct device_node *np = dev->of_node; in cpg_mssr_probe()
1064 error = cpg_mssr_common_init(dev, dev->of_node, info); in cpg_mssr_probe()
1070 priv->dev = dev; in cpg_mssr_probe()
1073 for (i = 0; i < info->num_core_clks; i++) in cpg_mssr_probe()
1074 cpg_mssr_register_core_clk(&info->core_clks[i], info, priv); in cpg_mssr_probe()
1076 for (i = 0; i < info->num_mod_clks; i++) in cpg_mssr_probe()
1077 cpg_mssr_register_mod_clk(&info->mod_clks[i], info, priv); in cpg_mssr_probe()
1085 error = cpg_mssr_add_clk_domain(dev, info->core_pm_clks, in cpg_mssr_probe()
1086 info->num_core_pm_clks); in cpg_mssr_probe()
1091 if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A) in cpg_mssr_probe()
1103 .name = "renesas-cpg-mssr",