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> |
| 25 | #include <asm/io.h> |
| 26 | #include <malloc.h> |
| 27 | #include <clk-uclass.h> |
| 28 | #include <dm/device.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 29 | #include <dm/devres.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 30 | #include <linux/clk-provider.h> |
| 31 | #include <clk.h> |
| 32 | #include "clk.h" |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 33 | #include <linux/err.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 34 | |
| 35 | #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux" |
| 36 | |
| 37 | int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags, |
| 38 | unsigned int val) |
| 39 | { |
Peng Fan | 5106fbb | 2019-07-31 07:01:26 +0000 | [diff] [blame] | 40 | struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ? |
| 41 | dev_get_clk_ptr(clk->dev) : 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 | { |
Peng Fan | 5106fbb | 2019-07-31 07:01:26 +0000 | [diff] [blame] | 84 | struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ? |
| 85 | dev_get_clk_ptr(clk->dev) : clk); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 86 | u32 val; |
| 87 | |
Lukasz Majewski | 669b773 | 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 | 4de44bb | 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 | 6a8c2ad | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 99 | static int clk_fetch_parent_index(struct clk *clk, |
| 100 | struct clk *parent) |
| 101 | { |
| 102 | struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ? |
| 103 | dev_get_clk_ptr(clk->dev) : clk); |
| 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 | { |
| 120 | struct clk_mux *mux = to_clk_mux(clk_dev_binded(clk) ? |
| 121 | dev_get_clk_ptr(clk->dev) : clk); |
| 122 | int index; |
| 123 | u32 val; |
| 124 | u32 reg; |
| 125 | |
| 126 | index = clk_fetch_parent_index(clk, parent); |
| 127 | if (index < 0) { |
| 128 | printf("Could not fetch index\n"); |
| 129 | return index; |
| 130 | } |
| 131 | |
| 132 | val = clk_mux_index_to_val(mux->table, mux->flags, index); |
| 133 | |
| 134 | if (mux->flags & CLK_MUX_HIWORD_MASK) { |
| 135 | reg = mux->mask << (mux->shift + 16); |
| 136 | } else { |
| 137 | reg = readl(mux->reg); |
| 138 | reg &= ~(mux->mask << mux->shift); |
| 139 | } |
| 140 | val = val << mux->shift; |
| 141 | reg |= val; |
| 142 | writel(reg, mux->reg); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 147 | const struct clk_ops clk_mux_ops = { |
Peng Fan | 6a8c2ad | 2019-07-31 07:01:28 +0000 | [diff] [blame] | 148 | .get_rate = clk_generic_get_rate, |
| 149 | .set_parent = clk_mux_set_parent, |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | struct clk *clk_hw_register_mux_table(struct device *dev, const char *name, |
| 153 | const char * const *parent_names, u8 num_parents, |
| 154 | unsigned long flags, |
| 155 | void __iomem *reg, u8 shift, u32 mask, |
| 156 | u8 clk_mux_flags, u32 *table) |
| 157 | { |
| 158 | struct clk_mux *mux; |
| 159 | struct clk *clk; |
| 160 | u8 width = 0; |
| 161 | int ret; |
| 162 | |
| 163 | if (clk_mux_flags & CLK_MUX_HIWORD_MASK) { |
| 164 | width = fls(mask) - ffs(mask) + 1; |
| 165 | if (width + shift > 16) { |
| 166 | pr_err("mux value exceeds LOWORD field\n"); |
| 167 | return ERR_PTR(-EINVAL); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* allocate the mux */ |
| 172 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); |
| 173 | if (!mux) |
| 174 | return ERR_PTR(-ENOMEM); |
| 175 | |
| 176 | /* U-boot specific assignments */ |
| 177 | mux->parent_names = parent_names; |
| 178 | mux->num_parents = num_parents; |
| 179 | |
| 180 | /* struct clk_mux assignments */ |
| 181 | mux->reg = reg; |
| 182 | mux->shift = shift; |
| 183 | mux->mask = mask; |
| 184 | mux->flags = clk_mux_flags; |
| 185 | mux->table = table; |
Lukasz Majewski | 669b773 | 2019-06-24 15:50:49 +0200 | [diff] [blame] | 186 | #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF) |
| 187 | mux->io_mux_val = *(u32 *)reg; |
| 188 | #endif |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 189 | |
| 190 | clk = &mux->clk; |
| 191 | |
| 192 | /* |
| 193 | * Read the current mux setup - so we assign correct parent. |
| 194 | * |
| 195 | * Changing parent would require changing internals of udevice struct |
| 196 | * for the corresponding clock (to do that define .set_parent() method. |
| 197 | */ |
| 198 | ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name, |
| 199 | parent_names[clk_mux_get_parent(clk)]); |
| 200 | if (ret) { |
| 201 | kfree(mux); |
| 202 | return ERR_PTR(ret); |
| 203 | } |
| 204 | |
| 205 | return clk; |
| 206 | } |
| 207 | |
| 208 | struct clk *clk_register_mux_table(struct device *dev, const char *name, |
| 209 | const char * const *parent_names, u8 num_parents, |
| 210 | unsigned long flags, |
| 211 | void __iomem *reg, u8 shift, u32 mask, |
| 212 | u8 clk_mux_flags, u32 *table) |
| 213 | { |
| 214 | struct clk *clk; |
| 215 | |
| 216 | clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents, |
| 217 | flags, reg, shift, mask, clk_mux_flags, |
| 218 | table); |
| 219 | if (IS_ERR(clk)) |
| 220 | return ERR_CAST(clk); |
| 221 | return clk; |
| 222 | } |
| 223 | |
| 224 | struct clk *clk_register_mux(struct device *dev, const char *name, |
| 225 | const char * const *parent_names, u8 num_parents, |
| 226 | unsigned long flags, |
| 227 | void __iomem *reg, u8 shift, u8 width, |
| 228 | u8 clk_mux_flags) |
| 229 | { |
| 230 | u32 mask = BIT(width) - 1; |
| 231 | |
| 232 | return clk_register_mux_table(dev, name, parent_names, num_parents, |
| 233 | flags, reg, shift, mask, clk_mux_flags, |
| 234 | NULL); |
| 235 | } |
| 236 | |
| 237 | U_BOOT_DRIVER(ccf_clk_mux) = { |
| 238 | .name = UBOOT_DM_CLK_CCF_MUX, |
| 239 | .id = UCLASS_CLK, |
| 240 | .ops = &clk_mux_ops, |
| 241 | .flags = DM_FLAG_PRE_RELOC, |
| 242 | }; |