Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * TI divider clock support |
| 4 | * |
| 5 | * Copyright (C) 2020 Dario Binacchi <dariobin@libero.it> |
| 6 | * |
| 7 | * Loosely based on Linux kernel drivers/clk/ti/divider.c |
| 8 | */ |
| 9 | |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 10 | #include <clk.h> |
| 11 | #include <clk-uclass.h> |
| 12 | #include <div64.h> |
| 13 | #include <dm.h> |
| 14 | #include <dm/device_compat.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <linux/clk-provider.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/log2.h> |
| 19 | #include "clk.h" |
| 20 | |
| 21 | /* |
| 22 | * The reverse of DIV_ROUND_UP: The maximum number which |
| 23 | * divided by m is r |
| 24 | */ |
| 25 | #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1) |
| 26 | |
| 27 | struct clk_ti_divider_priv { |
| 28 | struct clk parent; |
Dario Binacchi | ac7c705 | 2021-05-01 17:05:23 +0200 | [diff] [blame] | 29 | struct clk_ti_reg reg; |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 30 | const struct clk_div_table *table; |
| 31 | u8 shift; |
| 32 | u8 flags; |
| 33 | u8 div_flags; |
| 34 | s8 latch; |
| 35 | u16 min; |
| 36 | u16 max; |
| 37 | u16 mask; |
| 38 | }; |
| 39 | |
| 40 | static unsigned int _get_div(const struct clk_div_table *table, ulong flags, |
| 41 | unsigned int val) |
| 42 | { |
| 43 | if (flags & CLK_DIVIDER_ONE_BASED) |
| 44 | return val; |
| 45 | |
| 46 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
| 47 | return 1 << val; |
| 48 | |
| 49 | if (table) |
| 50 | return clk_divider_get_table_div(table, val); |
| 51 | |
| 52 | return val + 1; |
| 53 | } |
| 54 | |
| 55 | static unsigned int _get_val(const struct clk_div_table *table, ulong flags, |
| 56 | unsigned int div) |
| 57 | { |
| 58 | if (flags & CLK_DIVIDER_ONE_BASED) |
| 59 | return div; |
| 60 | |
| 61 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
| 62 | return __ffs(div); |
| 63 | |
| 64 | if (table) |
| 65 | return clk_divider_get_table_val(table, div); |
| 66 | |
| 67 | return div - 1; |
| 68 | } |
| 69 | |
| 70 | static int _div_round_up(const struct clk_div_table *table, ulong parent_rate, |
| 71 | ulong rate) |
| 72 | { |
| 73 | const struct clk_div_table *clkt; |
| 74 | int up = INT_MAX; |
| 75 | int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
| 76 | |
| 77 | for (clkt = table; clkt->div; clkt++) { |
| 78 | if (clkt->div == div) |
| 79 | return clkt->div; |
| 80 | else if (clkt->div < div) |
| 81 | continue; |
| 82 | |
| 83 | if ((clkt->div - div) < (up - div)) |
| 84 | up = clkt->div; |
| 85 | } |
| 86 | |
| 87 | return up; |
| 88 | } |
| 89 | |
| 90 | static int _div_round(const struct clk_div_table *table, ulong parent_rate, |
| 91 | ulong rate) |
| 92 | { |
| 93 | if (table) |
| 94 | return _div_round_up(table, parent_rate, rate); |
| 95 | |
| 96 | return DIV_ROUND_UP(parent_rate, rate); |
| 97 | } |
| 98 | |
| 99 | static int clk_ti_divider_best_div(struct clk *clk, ulong rate, |
| 100 | ulong *best_parent_rate) |
| 101 | { |
| 102 | struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev); |
| 103 | ulong parent_rate, parent_round_rate, max_div; |
| 104 | ulong best_rate, r; |
| 105 | int i, best_div = 0; |
| 106 | |
| 107 | parent_rate = clk_get_rate(&priv->parent); |
| 108 | if (IS_ERR_VALUE(parent_rate)) |
| 109 | return parent_rate; |
| 110 | |
| 111 | if (!rate) |
| 112 | rate = 1; |
| 113 | |
| 114 | if (!(clk->flags & CLK_SET_RATE_PARENT)) { |
| 115 | best_div = _div_round(priv->table, parent_rate, rate); |
| 116 | if (best_div == 0) |
| 117 | best_div = 1; |
| 118 | |
| 119 | if (best_div > priv->max) |
| 120 | best_div = priv->max; |
| 121 | |
| 122 | *best_parent_rate = parent_rate; |
| 123 | return best_div; |
| 124 | } |
| 125 | |
| 126 | max_div = min(ULONG_MAX / rate, (ulong)priv->max); |
| 127 | for (best_rate = 0, i = 1; i <= max_div; i++) { |
| 128 | if (!clk_divider_is_valid_div(priv->table, priv->div_flags, i)) |
| 129 | continue; |
| 130 | |
| 131 | /* |
| 132 | * It's the most ideal case if the requested rate can be |
| 133 | * divided from parent clock without needing to change |
| 134 | * parent rate, so return the divider immediately. |
| 135 | */ |
| 136 | if ((rate * i) == parent_rate) { |
| 137 | *best_parent_rate = parent_rate; |
| 138 | dev_dbg(clk->dev, "rate=%ld, best_rate=%ld, div=%d\n", |
| 139 | rate, rate, i); |
| 140 | return i; |
| 141 | } |
| 142 | |
| 143 | parent_round_rate = clk_round_rate(&priv->parent, |
| 144 | MULT_ROUND_UP(rate, i)); |
| 145 | if (IS_ERR_VALUE(parent_round_rate)) |
| 146 | continue; |
| 147 | |
| 148 | r = DIV_ROUND_UP(parent_round_rate, i); |
| 149 | if (r <= rate && r > best_rate) { |
| 150 | best_div = i; |
| 151 | best_rate = r; |
| 152 | *best_parent_rate = parent_round_rate; |
| 153 | if (best_rate == rate) |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (best_div == 0) { |
| 159 | best_div = priv->max; |
| 160 | parent_round_rate = clk_round_rate(&priv->parent, 1); |
| 161 | if (IS_ERR_VALUE(parent_round_rate)) |
| 162 | return parent_round_rate; |
| 163 | } |
| 164 | |
| 165 | dev_dbg(clk->dev, "rate=%ld, best_rate=%ld, div=%d\n", rate, best_rate, |
| 166 | best_div); |
| 167 | |
| 168 | return best_div; |
| 169 | } |
| 170 | |
| 171 | static ulong clk_ti_divider_round_rate(struct clk *clk, ulong rate) |
| 172 | { |
| 173 | ulong parent_rate; |
| 174 | int div; |
| 175 | |
| 176 | div = clk_ti_divider_best_div(clk, rate, &parent_rate); |
| 177 | if (div < 0) |
| 178 | return div; |
| 179 | |
| 180 | return DIV_ROUND_UP(parent_rate, div); |
| 181 | } |
| 182 | |
| 183 | static ulong clk_ti_divider_set_rate(struct clk *clk, ulong rate) |
| 184 | { |
| 185 | struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev); |
| 186 | ulong parent_rate; |
| 187 | int div; |
| 188 | u32 val, v; |
| 189 | |
| 190 | div = clk_ti_divider_best_div(clk, rate, &parent_rate); |
| 191 | if (div < 0) |
| 192 | return div; |
| 193 | |
| 194 | if (clk->flags & CLK_SET_RATE_PARENT) { |
| 195 | parent_rate = clk_set_rate(&priv->parent, parent_rate); |
| 196 | if (IS_ERR_VALUE(parent_rate)) |
| 197 | return parent_rate; |
| 198 | } |
| 199 | |
| 200 | val = _get_val(priv->table, priv->div_flags, div); |
| 201 | |
Dario Binacchi | ac7c705 | 2021-05-01 17:05:23 +0200 | [diff] [blame] | 202 | v = clk_ti_readl(&priv->reg); |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 203 | v &= ~(priv->mask << priv->shift); |
| 204 | v |= val << priv->shift; |
Dario Binacchi | ac7c705 | 2021-05-01 17:05:23 +0200 | [diff] [blame] | 205 | clk_ti_writel(v, &priv->reg); |
| 206 | clk_ti_latch(&priv->reg, priv->latch); |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 207 | |
| 208 | return clk_get_rate(clk); |
| 209 | } |
| 210 | |
| 211 | static ulong clk_ti_divider_get_rate(struct clk *clk) |
| 212 | { |
| 213 | struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev); |
| 214 | ulong rate, parent_rate; |
| 215 | unsigned int div; |
| 216 | u32 v; |
| 217 | |
| 218 | parent_rate = clk_get_rate(&priv->parent); |
| 219 | if (IS_ERR_VALUE(parent_rate)) |
| 220 | return parent_rate; |
| 221 | |
Dario Binacchi | ac7c705 | 2021-05-01 17:05:23 +0200 | [diff] [blame] | 222 | v = clk_ti_readl(&priv->reg) >> priv->shift; |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 223 | v &= priv->mask; |
| 224 | |
| 225 | div = _get_div(priv->table, priv->div_flags, v); |
| 226 | if (!div) { |
| 227 | if (!(priv->div_flags & CLK_DIVIDER_ALLOW_ZERO)) |
| 228 | dev_warn(clk->dev, |
| 229 | "zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n"); |
| 230 | return parent_rate; |
| 231 | } |
| 232 | |
| 233 | rate = DIV_ROUND_UP(parent_rate, div); |
| 234 | dev_dbg(clk->dev, "rate=%ld\n", rate); |
| 235 | return rate; |
| 236 | } |
| 237 | |
| 238 | static int clk_ti_divider_request(struct clk *clk) |
| 239 | { |
| 240 | struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev); |
| 241 | |
| 242 | clk->flags = priv->flags; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | const struct clk_ops clk_ti_divider_ops = { |
| 247 | .request = clk_ti_divider_request, |
| 248 | .round_rate = clk_ti_divider_round_rate, |
| 249 | .get_rate = clk_ti_divider_get_rate, |
| 250 | .set_rate = clk_ti_divider_set_rate |
| 251 | }; |
| 252 | |
| 253 | static int clk_ti_divider_remove(struct udevice *dev) |
| 254 | { |
| 255 | struct clk_ti_divider_priv *priv = dev_get_priv(dev); |
| 256 | int err; |
| 257 | |
| 258 | err = clk_release_all(&priv->parent, 1); |
| 259 | if (err) { |
| 260 | dev_err(dev, "failed to release parent clock\n"); |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int clk_ti_divider_probe(struct udevice *dev) |
| 268 | { |
| 269 | struct clk_ti_divider_priv *priv = dev_get_priv(dev); |
| 270 | int err; |
| 271 | |
| 272 | err = clk_get_by_index(dev, 0, &priv->parent); |
| 273 | if (err) { |
| 274 | dev_err(dev, "failed to get parent clock\n"); |
| 275 | return err; |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int clk_ti_divider_of_to_plat(struct udevice *dev) |
| 282 | { |
| 283 | struct clk_ti_divider_priv *priv = dev_get_priv(dev); |
| 284 | struct clk_div_table *table = NULL; |
| 285 | u32 val, valid_div; |
| 286 | u32 min_div = 0; |
| 287 | u32 max_val, max_div = 0; |
| 288 | u16 mask; |
Dario Binacchi | ac7c705 | 2021-05-01 17:05:23 +0200 | [diff] [blame] | 289 | int i, div_num, err; |
| 290 | |
| 291 | err = clk_ti_get_reg_addr(dev, 0, &priv->reg); |
| 292 | if (err) { |
| 293 | dev_err(dev, "failed to get register address\n"); |
| 294 | return err; |
| 295 | } |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 296 | |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 297 | priv->shift = dev_read_u32_default(dev, "ti,bit-shift", 0); |
| 298 | priv->latch = dev_read_s32_default(dev, "ti,latch-bit", -EINVAL); |
| 299 | if (dev_read_bool(dev, "ti,index-starts-at-one")) |
| 300 | priv->div_flags |= CLK_DIVIDER_ONE_BASED; |
| 301 | |
| 302 | if (dev_read_bool(dev, "ti,index-power-of-two")) |
| 303 | priv->div_flags |= CLK_DIVIDER_POWER_OF_TWO; |
| 304 | |
| 305 | if (dev_read_bool(dev, "ti,set-rate-parent")) |
| 306 | priv->flags |= CLK_SET_RATE_PARENT; |
| 307 | |
| 308 | if (dev_read_prop(dev, "ti,dividers", &div_num)) { |
| 309 | div_num /= sizeof(u32); |
| 310 | |
| 311 | /* Determine required size for divider table */ |
| 312 | for (i = 0, valid_div = 0; i < div_num; i++) { |
| 313 | dev_read_u32_index(dev, "ti,dividers", i, &val); |
| 314 | if (val) |
| 315 | valid_div++; |
| 316 | } |
| 317 | |
| 318 | if (!valid_div) { |
| 319 | dev_err(dev, "no valid dividers\n"); |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | |
| 323 | table = calloc(valid_div + 1, sizeof(*table)); |
| 324 | if (!table) |
| 325 | return -ENOMEM; |
| 326 | |
| 327 | for (i = 0, valid_div = 0; i < div_num; i++) { |
| 328 | dev_read_u32_index(dev, "ti,dividers", i, &val); |
| 329 | if (!val) |
| 330 | continue; |
| 331 | |
| 332 | table[valid_div].div = val; |
| 333 | table[valid_div].val = i; |
| 334 | valid_div++; |
| 335 | if (val > max_div) |
| 336 | max_div = val; |
| 337 | |
| 338 | if (!min_div || val < min_div) |
| 339 | min_div = val; |
| 340 | } |
| 341 | |
| 342 | max_val = max_div; |
| 343 | } else { |
| 344 | /* Divider table not provided, determine min/max divs */ |
| 345 | min_div = dev_read_u32_default(dev, "ti,min-div", 1); |
| 346 | if (dev_read_u32(dev, "ti,max-div", &max_div)) { |
| 347 | dev_err(dev, "missing 'max-div' property\n"); |
| 348 | return -EFAULT; |
| 349 | } |
| 350 | |
| 351 | max_val = max_div; |
| 352 | if (!(priv->div_flags & CLK_DIVIDER_ONE_BASED) && |
| 353 | !(priv->div_flags & CLK_DIVIDER_POWER_OF_TWO)) |
| 354 | max_val--; |
| 355 | } |
| 356 | |
| 357 | priv->table = table; |
| 358 | priv->min = min_div; |
| 359 | priv->max = max_div; |
| 360 | |
| 361 | if (priv->div_flags & CLK_DIVIDER_POWER_OF_TWO) |
| 362 | mask = fls(max_val) - 1; |
| 363 | else |
| 364 | mask = max_val; |
| 365 | |
| 366 | priv->mask = (1 << fls(mask)) - 1; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static const struct udevice_id clk_ti_divider_of_match[] = { |
| 371 | {.compatible = "ti,divider-clock"}, |
| 372 | {} |
| 373 | }; |
| 374 | |
| 375 | U_BOOT_DRIVER(clk_ti_divider) = { |
| 376 | .name = "ti_divider_clock", |
| 377 | .id = UCLASS_CLK, |
| 378 | .of_match = clk_ti_divider_of_match, |
Dario Binacchi | f686e4d | 2021-01-15 09:10:26 +0100 | [diff] [blame] | 379 | .of_to_plat = clk_ti_divider_of_to_plat, |
Dario Binacchi | d284157 | 2020-12-30 00:06:35 +0100 | [diff] [blame] | 380 | .probe = clk_ti_divider_probe, |
| 381 | .remove = clk_ti_divider_remove, |
| 382 | .priv_auto = sizeof(struct clk_ti_divider_priv), |
| 383 | .ops = &clk_ti_divider_ops, |
| 384 | }; |