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