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