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 | */ |
| 11 | |
Patrick Delaunay | 560e1e0 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 12 | #define LOG_CATEGORY UCLASS_CLK |
| 13 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 14 | #include <common.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <malloc.h> |
| 17 | #include <clk-uclass.h> |
Patrick Delaunay | 560e1e0 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 18 | #include <log.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 19 | #include <dm/device.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <dm/devres.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 21 | #include <dm/uclass.h> |
| 22 | #include <dm/lists.h> |
Patrick Delaunay | 560e1e0 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 23 | #include <dm/device_compat.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 24 | #include <dm/device-internal.h> |
Simon Glass | eb41d8a | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 25 | #include <linux/bug.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 26 | #include <linux/clk-provider.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 27 | #include <linux/err.h> |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 28 | #include <linux/log2.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 29 | #include <div64.h> |
| 30 | #include <clk.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 31 | #include <linux/printk.h> |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 32 | #include "clk.h" |
| 33 | |
| 34 | #define UBOOT_DM_CLK_CCF_DIVIDER "ccf_clk_divider" |
| 35 | |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 36 | unsigned int clk_divider_get_table_div(const struct clk_div_table *table, |
| 37 | unsigned int val) |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 38 | { |
| 39 | const struct clk_div_table *clkt; |
| 40 | |
| 41 | for (clkt = table; clkt->div; clkt++) |
| 42 | if (clkt->val == val) |
| 43 | return clkt->div; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static unsigned int _get_div(const struct clk_div_table *table, |
| 48 | unsigned int val, unsigned long flags, u8 width) |
| 49 | { |
| 50 | if (flags & CLK_DIVIDER_ONE_BASED) |
| 51 | return val; |
| 52 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
| 53 | return 1 << val; |
| 54 | if (flags & CLK_DIVIDER_MAX_AT_ZERO) |
| 55 | return val ? val : clk_div_mask(width) + 1; |
| 56 | if (table) |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 57 | return clk_divider_get_table_div(table, val); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 58 | return val + 1; |
| 59 | } |
| 60 | |
| 61 | unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate, |
| 62 | unsigned int val, |
| 63 | const struct clk_div_table *table, |
| 64 | unsigned long flags, unsigned long width) |
| 65 | { |
| 66 | unsigned int div; |
| 67 | |
| 68 | div = _get_div(table, val, flags, width); |
| 69 | if (!div) { |
| 70 | WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), |
| 71 | "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", |
| 72 | clk_hw_get_name(hw)); |
| 73 | return parent_rate; |
| 74 | } |
| 75 | |
| 76 | return DIV_ROUND_UP_ULL((u64)parent_rate, div); |
| 77 | } |
| 78 | |
| 79 | static ulong clk_divider_recalc_rate(struct clk *clk) |
| 80 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 81 | struct clk_divider *divider = to_clk_divider(clk); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 82 | unsigned long parent_rate = clk_get_parent_rate(clk); |
| 83 | unsigned int val; |
| 84 | |
Simon Glass | 4051c40 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 85 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Lukasz Majewski | 6bb15d6 | 2019-06-24 15:50:48 +0200 | [diff] [blame] | 86 | val = divider->io_divider_val; |
| 87 | #else |
| 88 | val = readl(divider->reg); |
| 89 | #endif |
| 90 | val >>= divider->shift; |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 91 | val &= clk_div_mask(divider->width); |
| 92 | |
| 93 | return divider_recalc_rate(clk, parent_rate, val, divider->table, |
| 94 | divider->flags, divider->width); |
| 95 | } |
| 96 | |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 97 | bool clk_divider_is_valid_table_div(const struct clk_div_table *table, |
| 98 | unsigned int div) |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 99 | { |
| 100 | const struct clk_div_table *clkt; |
| 101 | |
| 102 | for (clkt = table; clkt->div; clkt++) |
| 103 | if (clkt->div == div) |
| 104 | return true; |
| 105 | return false; |
| 106 | } |
| 107 | |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 108 | bool clk_divider_is_valid_div(const struct clk_div_table *table, |
| 109 | unsigned int div, unsigned long flags) |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 110 | { |
| 111 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
| 112 | return is_power_of_2(div); |
| 113 | if (table) |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 114 | return clk_divider_is_valid_table_div(table, div); |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 118 | unsigned int clk_divider_get_table_val(const struct clk_div_table *table, |
| 119 | unsigned int div) |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 120 | { |
| 121 | const struct clk_div_table *clkt; |
| 122 | |
| 123 | for (clkt = table; clkt->div; clkt++) |
| 124 | if (clkt->div == div) |
| 125 | return clkt->val; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static unsigned int _get_val(const struct clk_div_table *table, |
| 130 | unsigned int div, unsigned long flags, u8 width) |
| 131 | { |
| 132 | if (flags & CLK_DIVIDER_ONE_BASED) |
| 133 | return div; |
| 134 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
| 135 | return __ffs(div); |
| 136 | if (flags & CLK_DIVIDER_MAX_AT_ZERO) |
| 137 | return (div == clk_div_mask(width) + 1) ? 0 : div; |
| 138 | if (table) |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 139 | return clk_divider_get_table_val(table, div); |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 140 | return div - 1; |
| 141 | } |
| 142 | int divider_get_val(unsigned long rate, unsigned long parent_rate, |
| 143 | const struct clk_div_table *table, u8 width, |
| 144 | unsigned long flags) |
| 145 | { |
| 146 | unsigned int div, value; |
| 147 | |
| 148 | div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
| 149 | |
Dario Binacchi | 5688f3b | 2020-12-30 00:06:27 +0100 | [diff] [blame] | 150 | if (!clk_divider_is_valid_div(table, div, flags)) |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | |
| 153 | value = _get_val(table, div, flags, width); |
| 154 | |
| 155 | return min_t(unsigned int, value, clk_div_mask(width)); |
| 156 | } |
| 157 | |
| 158 | static ulong clk_divider_set_rate(struct clk *clk, unsigned long rate) |
| 159 | { |
Sean Anderson | 78ce0bd | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 160 | struct clk_divider *divider = to_clk_divider(clk); |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 161 | unsigned long parent_rate = clk_get_parent_rate(clk); |
| 162 | int value; |
| 163 | u32 val; |
| 164 | |
| 165 | value = divider_get_val(rate, parent_rate, divider->table, |
| 166 | divider->width, divider->flags); |
| 167 | if (value < 0) |
| 168 | return value; |
| 169 | |
| 170 | if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { |
| 171 | val = clk_div_mask(divider->width) << (divider->shift + 16); |
| 172 | } else { |
| 173 | val = readl(divider->reg); |
| 174 | val &= ~(clk_div_mask(divider->width) << divider->shift); |
| 175 | } |
| 176 | val |= (u32)value << divider->shift; |
| 177 | writel(val, divider->reg); |
| 178 | |
| 179 | return clk_get_rate(clk); |
| 180 | } |
| 181 | |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 182 | const struct clk_ops clk_divider_ops = { |
| 183 | .get_rate = clk_divider_recalc_rate, |
Peng Fan | fe69b03 | 2019-07-31 07:01:37 +0000 | [diff] [blame] | 184 | .set_rate = clk_divider_set_rate, |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | static struct clk *_register_divider(struct device *dev, const char *name, |
| 188 | const char *parent_name, unsigned long flags, |
| 189 | void __iomem *reg, u8 shift, u8 width, |
| 190 | u8 clk_divider_flags, const struct clk_div_table *table) |
| 191 | { |
| 192 | struct clk_divider *div; |
| 193 | struct clk *clk; |
| 194 | int ret; |
| 195 | |
| 196 | if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { |
| 197 | if (width + shift > 16) { |
Patrick Delaunay | 560e1e0 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 198 | dev_warn(dev, "divider value exceeds LOWORD field\n"); |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 199 | return ERR_PTR(-EINVAL); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* allocate the divider */ |
| 204 | div = kzalloc(sizeof(*div), GFP_KERNEL); |
| 205 | if (!div) |
| 206 | return ERR_PTR(-ENOMEM); |
| 207 | |
| 208 | /* struct clk_divider assignments */ |
| 209 | div->reg = reg; |
| 210 | div->shift = shift; |
| 211 | div->width = width; |
| 212 | div->flags = clk_divider_flags; |
| 213 | div->table = table; |
Simon Glass | 4051c40 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 214 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Lukasz Majewski | 6bb15d6 | 2019-06-24 15:50:48 +0200 | [diff] [blame] | 215 | div->io_divider_val = *(u32 *)reg; |
| 216 | #endif |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 217 | |
| 218 | /* register the clock */ |
| 219 | clk = &div->clk; |
Dario Binacchi | 16bdc85 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 220 | clk->flags = flags; |
Lukasz Majewski | 1d7993d | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 221 | |
| 222 | ret = clk_register(clk, UBOOT_DM_CLK_CCF_DIVIDER, name, parent_name); |
| 223 | if (ret) { |
| 224 | kfree(div); |
| 225 | return ERR_PTR(ret); |
| 226 | } |
| 227 | |
| 228 | return clk; |
| 229 | } |
| 230 | |
| 231 | struct clk *clk_register_divider(struct device *dev, const char *name, |
| 232 | const char *parent_name, unsigned long flags, |
| 233 | void __iomem *reg, u8 shift, u8 width, |
| 234 | u8 clk_divider_flags) |
| 235 | { |
| 236 | struct clk *clk; |
| 237 | |
| 238 | clk = _register_divider(dev, name, parent_name, flags, reg, shift, |
| 239 | width, clk_divider_flags, NULL); |
| 240 | if (IS_ERR(clk)) |
| 241 | return ERR_CAST(clk); |
| 242 | return clk; |
| 243 | } |
| 244 | |
| 245 | U_BOOT_DRIVER(ccf_clk_divider) = { |
| 246 | .name = UBOOT_DM_CLK_CCF_DIVIDER, |
| 247 | .id = UCLASS_CLK, |
| 248 | .ops = &clk_divider_ops, |
| 249 | .flags = DM_FLAG_PRE_RELOC, |
| 250 | }; |