Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2019 DENX Software Engineering |
| 4 | * Lukasz Majewski, DENX Software Engineering, lukma@denx.de |
| 5 | * |
| 6 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 7 | * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> |
| 8 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 9 | * |
| 10 | * Simple multiplexer clock implementation |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * U-Boot CCF porting node: |
| 15 | * |
| 16 | * The Linux kernel - as of tag: 5.0-rc3 is using also the imx_clk_fixup_mux() |
| 17 | * version of CCF mux. It is used on e.g. imx6q to provide fixes (like |
| 18 | * imx_cscmr1_fixup) for broken HW. |
| 19 | * |
| 20 | * At least for IMX6Q (but NOT IMX6QP) it is important when we set the parent |
| 21 | * clock. |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Dario Binacchi | 76eaa2d | 2020-05-02 17:58:31 +0200 | [diff] [blame] | 25 | #include <clk.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 26 | #include <clk-uclass.h> |
Patrick Delaunay | 572c446 | 2021-11-19 15:12:06 +0100 | [diff] [blame^] | 27 | #include <malloc.h> |
| 28 | #include <asm/io.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 29 | #include <dm/device.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 30 | #include <dm/devres.h> |
Lukasz Majewski | ebd3f1f | 2020-08-24 11:12:18 +0200 | [diff] [blame] | 31 | #include <dm/uclass.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 32 | #include <linux/bitops.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 33 | #include <linux/clk-provider.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 34 | #include <linux/err.h> |
Patrick Delaunay | 572c446 | 2021-11-19 15:12:06 +0100 | [diff] [blame^] | 35 | |
Dario Binacchi | 76eaa2d | 2020-05-02 17:58:31 +0200 | [diff] [blame] | 36 | #include "clk.h" |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 37 | |
| 38 | #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux" |
| 39 | |
| 40 | int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags, |
| 41 | unsigned int val) |
| 42 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 43 | struct clk_mux *mux = to_clk_mux(clk); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 44 | int num_parents = mux->num_parents; |
| 45 | |
| 46 | if (table) { |
| 47 | int i; |
| 48 | |
| 49 | for (i = 0; i < num_parents; i++) |
| 50 | if (table[i] == val) |
| 51 | return i; |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | if (val && (flags & CLK_MUX_INDEX_BIT)) |
| 56 | val = ffs(val) - 1; |
| 57 | |
| 58 | if (val && (flags & CLK_MUX_INDEX_ONE)) |
| 59 | val--; |
| 60 | |
| 61 | if (val >= num_parents) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | return val; |
| 65 | } |
| 66 | |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 67 | unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index) |
| 68 | { |
| 69 | unsigned int val = index; |
| 70 | |
| 71 | if (table) { |
| 72 | val = table[index]; |
| 73 | } else { |
| 74 | if (flags & CLK_MUX_INDEX_BIT) |
| 75 | val = 1 << index; |
| 76 | |
| 77 | if (flags & CLK_MUX_INDEX_ONE) |
| 78 | val++; |
| 79 | } |
| 80 | |
| 81 | return val; |
| 82 | } |
| 83 | |
| 84 | u8 clk_mux_get_parent(struct clk *clk) |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 85 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 86 | struct clk_mux *mux = to_clk_mux(clk); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 87 | u32 val; |
| 88 | |
Lukasz Majewski | 5da0095 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 89 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 90 | val = mux->io_mux_val; |
| 91 | #else |
| 92 | val = readl(mux->reg); |
| 93 | #endif |
| 94 | val >>= mux->shift; |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 95 | val &= mux->mask; |
| 96 | |
| 97 | return clk_mux_val_to_index(clk, mux->table, mux->flags, val); |
| 98 | } |
| 99 | |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 100 | static int clk_fetch_parent_index(struct clk *clk, |
| 101 | struct clk *parent) |
| 102 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 103 | struct clk_mux *mux = to_clk_mux(clk); |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 104 | |
| 105 | int i; |
| 106 | |
| 107 | if (!parent) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | for (i = 0; i < mux->num_parents; i++) { |
| 111 | if (!strcmp(parent->dev->name, mux->parent_names[i])) |
| 112 | return i; |
| 113 | } |
| 114 | |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | |
| 118 | static int clk_mux_set_parent(struct clk *clk, struct clk *parent) |
| 119 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 120 | struct clk_mux *mux = to_clk_mux(clk); |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 121 | int index; |
| 122 | u32 val; |
| 123 | u32 reg; |
| 124 | |
| 125 | index = clk_fetch_parent_index(clk, parent); |
| 126 | if (index < 0) { |
| 127 | printf("Could not fetch index\n"); |
| 128 | return index; |
| 129 | } |
| 130 | |
| 131 | val = clk_mux_index_to_val(mux->table, mux->flags, index); |
| 132 | |
| 133 | if (mux->flags & CLK_MUX_HIWORD_MASK) { |
| 134 | reg = mux->mask << (mux->shift + 16); |
| 135 | } else { |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 136 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 137 | reg = mux->io_mux_val; |
| 138 | #else |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 139 | reg = readl(mux->reg); |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 140 | #endif |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 141 | reg &= ~(mux->mask << mux->shift); |
| 142 | } |
| 143 | val = val << mux->shift; |
| 144 | reg |= val; |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 145 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 146 | mux->io_mux_val = reg; |
| 147 | #else |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 148 | writel(reg, mux->reg); |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 149 | #endif |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 154 | const struct clk_ops clk_mux_ops = { |
Dario Binacchi | fa181d1 | 2020-10-14 23:42:17 +0200 | [diff] [blame] | 155 | .get_rate = clk_generic_get_rate, |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 156 | .set_parent = clk_mux_set_parent, |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | struct clk *clk_hw_register_mux_table(struct device *dev, const char *name, |
| 160 | const char * const *parent_names, u8 num_parents, |
| 161 | unsigned long flags, |
| 162 | void __iomem *reg, u8 shift, u32 mask, |
| 163 | u8 clk_mux_flags, u32 *table) |
| 164 | { |
| 165 | struct clk_mux *mux; |
| 166 | struct clk *clk; |
| 167 | u8 width = 0; |
| 168 | int ret; |
| 169 | |
| 170 | if (clk_mux_flags & CLK_MUX_HIWORD_MASK) { |
| 171 | width = fls(mask) - ffs(mask) + 1; |
| 172 | if (width + shift > 16) { |
| 173 | pr_err("mux value exceeds LOWORD field\n"); |
| 174 | return ERR_PTR(-EINVAL); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* allocate the mux */ |
| 179 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); |
| 180 | if (!mux) |
| 181 | return ERR_PTR(-ENOMEM); |
| 182 | |
| 183 | /* U-boot specific assignments */ |
| 184 | mux->parent_names = parent_names; |
| 185 | mux->num_parents = num_parents; |
| 186 | |
| 187 | /* struct clk_mux assignments */ |
| 188 | mux->reg = reg; |
| 189 | mux->shift = shift; |
| 190 | mux->mask = mask; |
| 191 | mux->flags = clk_mux_flags; |
| 192 | mux->table = table; |
Lukasz Majewski | 5da0095 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 193 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 194 | mux->io_mux_val = *(u32 *)reg; |
| 195 | #endif |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 196 | |
| 197 | clk = &mux->clk; |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 198 | clk->flags = flags; |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Read the current mux setup - so we assign correct parent. |
| 202 | * |
| 203 | * Changing parent would require changing internals of udevice struct |
Dario Binacchi | 40559d2 | 2020-05-02 17:58:32 +0200 | [diff] [blame] | 204 | * for the corresponding clock (to do that define .set_parent() method). |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 205 | */ |
| 206 | ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name, |
| 207 | parent_names[clk_mux_get_parent(clk)]); |
| 208 | if (ret) { |
| 209 | kfree(mux); |
| 210 | return ERR_PTR(ret); |
| 211 | } |
| 212 | |
| 213 | return clk; |
| 214 | } |
| 215 | |
| 216 | struct clk *clk_register_mux_table(struct device *dev, const char *name, |
| 217 | const char * const *parent_names, u8 num_parents, |
| 218 | unsigned long flags, |
| 219 | void __iomem *reg, u8 shift, u32 mask, |
| 220 | u8 clk_mux_flags, u32 *table) |
| 221 | { |
| 222 | struct clk *clk; |
| 223 | |
| 224 | clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents, |
| 225 | flags, reg, shift, mask, clk_mux_flags, |
| 226 | table); |
| 227 | if (IS_ERR(clk)) |
| 228 | return ERR_CAST(clk); |
| 229 | return clk; |
| 230 | } |
| 231 | |
| 232 | struct clk *clk_register_mux(struct device *dev, const char *name, |
| 233 | const char * const *parent_names, u8 num_parents, |
| 234 | unsigned long flags, |
| 235 | void __iomem *reg, u8 shift, u8 width, |
| 236 | u8 clk_mux_flags) |
| 237 | { |
| 238 | u32 mask = BIT(width) - 1; |
| 239 | |
| 240 | return clk_register_mux_table(dev, name, parent_names, num_parents, |
| 241 | flags, reg, shift, mask, clk_mux_flags, |
| 242 | NULL); |
| 243 | } |
| 244 | |
| 245 | U_BOOT_DRIVER(ccf_clk_mux) = { |
| 246 | .name = UBOOT_DM_CLK_CCF_MUX, |
| 247 | .id = UCLASS_CLK, |
| 248 | .ops = &clk_mux_ops, |
| 249 | .flags = DM_FLAG_PRE_RELOC, |
| 250 | }; |