Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Renesas RCar Gen3 CPG MSSR driver |
| 4 | * |
| 5 | * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com> |
| 6 | * |
| 7 | * Based on the following driver from Linux kernel: |
| 8 | * r8a7796 Clock Pulse Generator / Module Standby and Software Reset |
| 9 | * |
| 10 | * Copyright (C) 2016 Glider bvba |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 11 | */ |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 12 | #include <clk-uclass.h> |
| 13 | #include <dm.h> |
| 14 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 16 | #include <wait_bit.h> |
| 17 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 19 | |
| 20 | #include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 21 | |
| 22 | #include "renesas-cpg-mssr.h" |
| 23 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 24 | bool renesas_clk_is_mod(struct clk *clk) |
| 25 | { |
| 26 | return (clk->id >> 16) == CPG_MOD; |
| 27 | } |
| 28 | |
| 29 | int renesas_clk_get_mod(struct clk *clk, struct cpg_mssr_info *info, |
| 30 | const struct mssr_mod_clk **mssr) |
| 31 | { |
| 32 | const unsigned long clkid = clk->id & 0xffff; |
| 33 | int i; |
| 34 | |
| 35 | for (i = 0; i < info->mod_clk_size; i++) { |
| 36 | if (info->mod_clk[i].id != |
| 37 | (info->mod_clk_base + MOD_CLK_PACK(clkid))) |
| 38 | continue; |
| 39 | |
| 40 | *mssr = &info->mod_clk[i]; |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | return -ENODEV; |
| 45 | } |
| 46 | |
| 47 | int renesas_clk_get_core(struct clk *clk, struct cpg_mssr_info *info, |
| 48 | const struct cpg_core_clk **core) |
| 49 | { |
| 50 | const unsigned long clkid = clk->id & 0xffff; |
| 51 | int i; |
| 52 | |
| 53 | for (i = 0; i < info->core_clk_size; i++) { |
| 54 | if (info->core_clk[i].id != clkid) |
| 55 | continue; |
| 56 | |
| 57 | *core = &info->core_clk[i]; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | return -ENODEV; |
| 62 | } |
| 63 | |
| 64 | int renesas_clk_get_parent(struct clk *clk, struct cpg_mssr_info *info, |
| 65 | struct clk *parent) |
| 66 | { |
| 67 | const struct cpg_core_clk *core; |
| 68 | const struct mssr_mod_clk *mssr; |
| 69 | int ret; |
| 70 | |
| 71 | if (renesas_clk_is_mod(clk)) { |
| 72 | ret = renesas_clk_get_mod(clk, info, &mssr); |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | |
| 76 | parent->id = mssr->parent; |
| 77 | } else { |
| 78 | ret = renesas_clk_get_core(clk, info, &core); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | |
| 82 | if (core->type == CLK_TYPE_IN) |
| 83 | parent->id = ~0; /* Top-level clock */ |
| 84 | else |
| 85 | parent->id = core->parent; |
| 86 | } |
| 87 | |
| 88 | parent->dev = clk->dev; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Hai Pham | 5460ee0 | 2020-05-22 10:39:04 +0700 | [diff] [blame] | 93 | int renesas_clk_endisable(struct clk *clk, void __iomem *base, |
| 94 | struct cpg_mssr_info *info, bool enable) |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 95 | { |
| 96 | const unsigned long clkid = clk->id & 0xffff; |
| 97 | const unsigned int reg = clkid / 100; |
| 98 | const unsigned int bit = clkid % 100; |
| 99 | const u32 bitmask = BIT(bit); |
| 100 | |
| 101 | if (!renesas_clk_is_mod(clk)) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | debug("%s[%i] MSTP %lu=%02u/%02u %s\n", __func__, __LINE__, |
| 105 | clkid, reg, bit, enable ? "ON" : "OFF"); |
| 106 | |
| 107 | if (enable) { |
Hai Pham | 9480346 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 108 | clrbits_le32(base + info->control_regs[reg], bitmask); |
| 109 | return wait_for_bit_le32(base + info->status_regs[reg], |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 110 | bitmask, 0, 100, 0); |
| 111 | } else { |
Hai Pham | 9480346 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 112 | setbits_le32(base + info->control_regs[reg], bitmask); |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | int renesas_clk_remove(void __iomem *base, struct cpg_mssr_info *info) |
| 118 | { |
| 119 | unsigned int i; |
| 120 | |
| 121 | /* Stop TMU0 */ |
| 122 | clrbits_le32(TMU_BASE + TSTR0, TSTR0_STR0); |
| 123 | |
| 124 | /* Stop module clock */ |
| 125 | for (i = 0; i < info->mstp_table_size; i++) { |
Hai Pham | 9480346 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 126 | clrsetbits_le32(base + info->control_regs[i], |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 127 | info->mstp_table[i].sdis, |
| 128 | info->mstp_table[i].sen); |
Hai Pham | 86d59f3 | 2020-08-11 10:46:34 +0700 | [diff] [blame] | 129 | |
Marek Vasut | ba2c7d2 | 2023-02-28 22:34:38 +0100 | [diff] [blame] | 130 | if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN4) |
Hai Pham | 86d59f3 | 2020-08-11 10:46:34 +0700 | [diff] [blame] | 131 | continue; |
| 132 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 133 | clrsetbits_le32(base + RMSTPCR(i), |
| 134 | info->mstp_table[i].rdis, |
| 135 | info->mstp_table[i].ren); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |