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> |
| 27 | #include <dm/device.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 28 | #include <dm/devres.h> |
Lukasz Majewski | ebd3f1f | 2020-08-24 11:12:18 +0200 | [diff] [blame^] | 29 | #include <dm/uclass.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 30 | #include <linux/bitops.h> |
Dario Binacchi | 76eaa2d | 2020-05-02 17:58:31 +0200 | [diff] [blame] | 31 | #include <malloc.h> |
| 32 | #include <asm/io.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> |
Dario Binacchi | 76eaa2d | 2020-05-02 17:58:31 +0200 | [diff] [blame] | 35 | #include "clk.h" |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 36 | |
| 37 | #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux" |
| 38 | |
| 39 | int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags, |
| 40 | unsigned int val) |
| 41 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 42 | struct clk_mux *mux = to_clk_mux(clk); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 43 | int num_parents = mux->num_parents; |
| 44 | |
| 45 | if (table) { |
| 46 | int i; |
| 47 | |
| 48 | for (i = 0; i < num_parents; i++) |
| 49 | if (table[i] == val) |
| 50 | return i; |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | |
| 54 | if (val && (flags & CLK_MUX_INDEX_BIT)) |
| 55 | val = ffs(val) - 1; |
| 56 | |
| 57 | if (val && (flags & CLK_MUX_INDEX_ONE)) |
| 58 | val--; |
| 59 | |
| 60 | if (val >= num_parents) |
| 61 | return -EINVAL; |
| 62 | |
| 63 | return val; |
| 64 | } |
| 65 | |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 66 | unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index) |
| 67 | { |
| 68 | unsigned int val = index; |
| 69 | |
| 70 | if (table) { |
| 71 | val = table[index]; |
| 72 | } else { |
| 73 | if (flags & CLK_MUX_INDEX_BIT) |
| 74 | val = 1 << index; |
| 75 | |
| 76 | if (flags & CLK_MUX_INDEX_ONE) |
| 77 | val++; |
| 78 | } |
| 79 | |
| 80 | return val; |
| 81 | } |
| 82 | |
| 83 | u8 clk_mux_get_parent(struct clk *clk) |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 84 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 85 | struct clk_mux *mux = to_clk_mux(clk); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 86 | u32 val; |
| 87 | |
Lukasz Majewski | 5da0095 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 88 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 89 | val = mux->io_mux_val; |
| 90 | #else |
| 91 | val = readl(mux->reg); |
| 92 | #endif |
| 93 | val >>= mux->shift; |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 94 | val &= mux->mask; |
| 95 | |
| 96 | return clk_mux_val_to_index(clk, mux->table, mux->flags, val); |
| 97 | } |
| 98 | |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 99 | static int clk_fetch_parent_index(struct clk *clk, |
| 100 | struct clk *parent) |
| 101 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 102 | struct clk_mux *mux = to_clk_mux(clk); |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 103 | |
| 104 | int i; |
| 105 | |
| 106 | if (!parent) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | for (i = 0; i < mux->num_parents; i++) { |
| 110 | if (!strcmp(parent->dev->name, mux->parent_names[i])) |
| 111 | return i; |
| 112 | } |
| 113 | |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | static int clk_mux_set_parent(struct clk *clk, struct clk *parent) |
| 118 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 119 | struct clk_mux *mux = to_clk_mux(clk); |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 120 | int index; |
| 121 | u32 val; |
| 122 | u32 reg; |
| 123 | |
| 124 | index = clk_fetch_parent_index(clk, parent); |
| 125 | if (index < 0) { |
| 126 | printf("Could not fetch index\n"); |
| 127 | return index; |
| 128 | } |
| 129 | |
| 130 | val = clk_mux_index_to_val(mux->table, mux->flags, index); |
| 131 | |
| 132 | if (mux->flags & CLK_MUX_HIWORD_MASK) { |
| 133 | reg = mux->mask << (mux->shift + 16); |
| 134 | } else { |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 135 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 136 | reg = mux->io_mux_val; |
| 137 | #else |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 138 | reg = readl(mux->reg); |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 139 | #endif |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 140 | reg &= ~(mux->mask << mux->shift); |
| 141 | } |
| 142 | val = val << mux->shift; |
| 143 | reg |= val; |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 144 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 145 | mux->io_mux_val = reg; |
| 146 | #else |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 147 | writel(reg, mux->reg); |
Dario Binacchi | e3b5d74 | 2020-05-02 17:58:33 +0200 | [diff] [blame] | 148 | #endif |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Dario Binacchi | 12d1526 | 2020-06-03 15:36:25 +0200 | [diff] [blame] | 153 | static ulong clk_mux_get_rate(struct clk *clk) |
| 154 | { |
| 155 | struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ? |
| 156 | dev_get_clk_ptr(clk->dev) : clk); |
| 157 | struct udevice *parent; |
| 158 | struct clk *pclk; |
| 159 | int err, index; |
| 160 | |
| 161 | index = clk_mux_get_parent(clk); |
| 162 | if (index >= mux->num_parents) |
| 163 | return -EFAULT; |
| 164 | |
| 165 | err = uclass_get_device_by_name(UCLASS_CLK, mux->parent_names[index], |
| 166 | &parent); |
| 167 | if (err) |
| 168 | return err; |
| 169 | |
| 170 | pclk = dev_get_clk_ptr(parent); |
| 171 | if (!pclk) |
| 172 | return -ENODEV; |
| 173 | |
| 174 | return clk_get_rate(pclk); |
| 175 | } |
| 176 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 177 | const struct clk_ops clk_mux_ops = { |
Dario Binacchi | 12d1526 | 2020-06-03 15:36:25 +0200 | [diff] [blame] | 178 | .get_rate = clk_mux_get_rate, |
Peng Fan | 4b04408 | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 179 | .set_parent = clk_mux_set_parent, |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | struct clk *clk_hw_register_mux_table(struct device *dev, const char *name, |
| 183 | const char * const *parent_names, u8 num_parents, |
| 184 | unsigned long flags, |
| 185 | void __iomem *reg, u8 shift, u32 mask, |
| 186 | u8 clk_mux_flags, u32 *table) |
| 187 | { |
| 188 | struct clk_mux *mux; |
| 189 | struct clk *clk; |
| 190 | u8 width = 0; |
| 191 | int ret; |
| 192 | |
| 193 | if (clk_mux_flags & CLK_MUX_HIWORD_MASK) { |
| 194 | width = fls(mask) - ffs(mask) + 1; |
| 195 | if (width + shift > 16) { |
| 196 | pr_err("mux value exceeds LOWORD field\n"); |
| 197 | return ERR_PTR(-EINVAL); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* allocate the mux */ |
| 202 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); |
| 203 | if (!mux) |
| 204 | return ERR_PTR(-ENOMEM); |
| 205 | |
| 206 | /* U-boot specific assignments */ |
| 207 | mux->parent_names = parent_names; |
| 208 | mux->num_parents = num_parents; |
| 209 | |
| 210 | /* struct clk_mux assignments */ |
| 211 | mux->reg = reg; |
| 212 | mux->shift = shift; |
| 213 | mux->mask = mask; |
| 214 | mux->flags = clk_mux_flags; |
| 215 | mux->table = table; |
Lukasz Majewski | 5da0095 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 216 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 217 | mux->io_mux_val = *(u32 *)reg; |
| 218 | #endif |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 219 | |
| 220 | clk = &mux->clk; |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 221 | clk->flags = flags; |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Read the current mux setup - so we assign correct parent. |
| 225 | * |
| 226 | * Changing parent would require changing internals of udevice struct |
Dario Binacchi | 40559d2 | 2020-05-02 17:58:32 +0200 | [diff] [blame] | 227 | * for the corresponding clock (to do that define .set_parent() method). |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 228 | */ |
| 229 | ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name, |
| 230 | parent_names[clk_mux_get_parent(clk)]); |
| 231 | if (ret) { |
| 232 | kfree(mux); |
| 233 | return ERR_PTR(ret); |
| 234 | } |
| 235 | |
| 236 | return clk; |
| 237 | } |
| 238 | |
| 239 | struct clk *clk_register_mux_table(struct device *dev, const char *name, |
| 240 | const char * const *parent_names, u8 num_parents, |
| 241 | unsigned long flags, |
| 242 | void __iomem *reg, u8 shift, u32 mask, |
| 243 | u8 clk_mux_flags, u32 *table) |
| 244 | { |
| 245 | struct clk *clk; |
| 246 | |
| 247 | clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents, |
| 248 | flags, reg, shift, mask, clk_mux_flags, |
| 249 | table); |
| 250 | if (IS_ERR(clk)) |
| 251 | return ERR_CAST(clk); |
| 252 | return clk; |
| 253 | } |
| 254 | |
| 255 | struct clk *clk_register_mux(struct device *dev, const char *name, |
| 256 | const char * const *parent_names, u8 num_parents, |
| 257 | unsigned long flags, |
| 258 | void __iomem *reg, u8 shift, u8 width, |
| 259 | u8 clk_mux_flags) |
| 260 | { |
| 261 | u32 mask = BIT(width) - 1; |
| 262 | |
| 263 | return clk_register_mux_table(dev, name, parent_names, num_parents, |
| 264 | flags, reg, shift, mask, clk_mux_flags, |
| 265 | NULL); |
| 266 | } |
| 267 | |
| 268 | U_BOOT_DRIVER(ccf_clk_mux) = { |
| 269 | .name = UBOOT_DM_CLK_CCF_MUX, |
| 270 | .id = UCLASS_CLK, |
| 271 | .ops = &clk_mux_ops, |
| 272 | .flags = DM_FLAG_PRE_RELOC, |
| 273 | }; |