Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments K3 clock driver |
| 4 | * |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 5 | * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/ |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 6 | * Tero Kristo <t-kristo@ti.com> |
| 7 | */ |
| 8 | |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <soc.h> |
| 12 | #include <clk-uclass.h> |
Udit Kumar | c648daa | 2023-09-21 22:30:38 +0530 | [diff] [blame] | 13 | #include <k3-avs.h> |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 14 | #include "k3-clk.h" |
| 15 | |
| 16 | #define PLL_MIN_FREQ 800000000 |
| 17 | #define PLL_MAX_FREQ 3200000000UL |
| 18 | #define PLL_MAX_DIV 127 |
| 19 | |
| 20 | /** |
| 21 | * struct clk_map - mapping from dev/clk id tuples towards physical clocks |
| 22 | * @dev_id: device ID for the clock |
| 23 | * @clk_id: clock ID for the clock |
| 24 | * @clk: pointer to the registered clock entry for the mapping |
| 25 | */ |
| 26 | struct clk_map { |
| 27 | u16 dev_id; |
| 28 | u32 clk_id; |
| 29 | struct clk *clk; |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * struct ti_clk_data - clock controller information structure |
| 34 | * @map: mapping from dev/clk id tuples to physical clock entries |
| 35 | * @size: number of entries in the map |
| 36 | */ |
| 37 | struct ti_clk_data { |
| 38 | struct clk_map *map; |
| 39 | int size; |
| 40 | }; |
| 41 | |
| 42 | static ulong osc_freq; |
| 43 | |
| 44 | static void clk_add_map(struct ti_clk_data *data, struct clk *clk, |
| 45 | u32 dev_id, u32 clk_id) |
| 46 | { |
| 47 | struct clk_map *map; |
| 48 | |
| 49 | debug("%s: added clk=%p, data=%p, dev=%d, clk=%d\n", __func__, |
| 50 | clk, data, dev_id, clk_id); |
| 51 | if (!clk) |
| 52 | return; |
| 53 | |
| 54 | map = data->map + data->size++; |
| 55 | |
| 56 | map->dev_id = dev_id; |
| 57 | map->clk_id = clk_id; |
| 58 | map->clk = clk; |
| 59 | } |
| 60 | |
| 61 | static const struct soc_attr ti_k3_soc_clk_data[] = { |
Jayesh Choudhary | 21b2177 | 2024-06-12 14:41:13 +0530 | [diff] [blame] | 62 | #if IS_ENABLED(CONFIG_SOC_K3_AM625) |
| 63 | { |
| 64 | .family = "AM62X", |
| 65 | .data = &am62x_clk_platdata, |
| 66 | }, |
| 67 | #endif |
| 68 | #if IS_ENABLED(CONFIG_SOC_K3_AM62A7) |
| 69 | { |
| 70 | .family = "AM62AX", |
| 71 | .data = &am62ax_clk_platdata, |
| 72 | }, |
| 73 | #endif |
| 74 | #if IS_ENABLED(CONFIG_SOC_K3_AM62P5) |
| 75 | { |
| 76 | .family = "AM62PX", |
| 77 | .data = &am62px_clk_platdata, |
| 78 | }, |
| 79 | #endif |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 80 | #if IS_ENABLED(CONFIG_SOC_K3_J721E) |
| 81 | { |
| 82 | .family = "J721E", |
| 83 | .data = &j721e_clk_platdata, |
| 84 | }, |
| 85 | { |
| 86 | .family = "J7200", |
| 87 | .data = &j7200_clk_platdata, |
| 88 | }, |
Jayesh Choudhary | 21b2177 | 2024-06-12 14:41:13 +0530 | [diff] [blame] | 89 | #endif |
| 90 | #if IS_ENABLED(CONFIG_SOC_K3_J721S2) |
David Huang | e04854b | 2022-01-25 20:56:33 +0530 | [diff] [blame] | 91 | { |
| 92 | .family = "J721S2", |
| 93 | .data = &j721s2_clk_platdata, |
| 94 | }, |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 95 | #endif |
Jayesh Choudhary | 9332a637 | 2024-06-12 14:41:16 +0530 | [diff] [blame] | 96 | #if IS_ENABLED(CONFIG_SOC_K3_J722S) |
| 97 | { |
| 98 | .family = "J722S", |
| 99 | .data = &j722s_clk_platdata, |
| 100 | }, |
| 101 | #endif |
Jayesh Choudhary | 21b2177 | 2024-06-12 14:41:13 +0530 | [diff] [blame] | 102 | #if IS_ENABLED(CONFIG_SOC_K3_J784S4) |
Apurva Nandan | b93ab92 | 2024-02-24 01:51:44 +0530 | [diff] [blame] | 103 | { |
| 104 | .family = "J784S4", |
| 105 | .data = &j784s4_clk_platdata, |
| 106 | }, |
| 107 | #endif |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 108 | { /* sentinel */ } |
| 109 | }; |
| 110 | |
| 111 | static int ti_clk_probe(struct udevice *dev) |
| 112 | { |
| 113 | struct ti_clk_data *data = dev_get_priv(dev); |
| 114 | struct clk *clk; |
| 115 | const char *name; |
| 116 | const struct clk_data *ti_clk_data; |
| 117 | int i, j; |
| 118 | const struct soc_attr *soc_match_data; |
| 119 | const struct ti_k3_clk_platdata *pdata; |
| 120 | |
| 121 | debug("%s(dev=%p)\n", __func__, dev); |
| 122 | |
| 123 | soc_match_data = soc_device_match(ti_k3_soc_clk_data); |
| 124 | if (!soc_match_data) |
| 125 | return -ENODEV; |
| 126 | |
| 127 | pdata = (const struct ti_k3_clk_platdata *)soc_match_data->data; |
| 128 | |
| 129 | data->map = kcalloc(pdata->soc_dev_clk_data_cnt, sizeof(*data->map), |
| 130 | GFP_KERNEL); |
| 131 | data->size = 0; |
| 132 | |
| 133 | for (i = 0; i < pdata->clk_list_cnt; i++) { |
| 134 | ti_clk_data = &pdata->clk_list[i]; |
| 135 | |
| 136 | switch (ti_clk_data->type) { |
| 137 | case CLK_TYPE_FIXED_RATE: |
| 138 | name = ti_clk_data->clk.fixed_rate.name; |
| 139 | clk = clk_register_fixed_rate(NULL, |
| 140 | name, |
| 141 | ti_clk_data->clk.fixed_rate.rate); |
| 142 | break; |
| 143 | case CLK_TYPE_DIV: |
| 144 | name = ti_clk_data->clk.div.name; |
| 145 | clk = clk_register_divider(NULL, name, |
| 146 | ti_clk_data->clk.div.parent, |
| 147 | ti_clk_data->clk.div.flags, |
| 148 | map_physmem(ti_clk_data->clk.div.reg, 0, MAP_NOCACHE), |
| 149 | ti_clk_data->clk.div.shift, |
| 150 | ti_clk_data->clk.div.width, |
Suman Anna | db4c2dc | 2021-09-07 17:16:58 -0500 | [diff] [blame] | 151 | ti_clk_data->clk.div.div_flags); |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 152 | break; |
| 153 | case CLK_TYPE_MUX: |
| 154 | name = ti_clk_data->clk.mux.name; |
| 155 | clk = clk_register_mux(NULL, name, |
| 156 | ti_clk_data->clk.mux.parents, |
| 157 | ti_clk_data->clk.mux.num_parents, |
| 158 | ti_clk_data->clk.mux.flags, |
| 159 | map_physmem(ti_clk_data->clk.mux.reg, 0, MAP_NOCACHE), |
| 160 | ti_clk_data->clk.mux.shift, |
| 161 | ti_clk_data->clk.mux.width, |
| 162 | 0); |
| 163 | break; |
| 164 | case CLK_TYPE_PLL: |
| 165 | name = ti_clk_data->clk.pll.name; |
| 166 | clk = clk_register_ti_pll(name, |
| 167 | ti_clk_data->clk.pll.parent, |
| 168 | map_physmem(ti_clk_data->clk.pll.reg, 0, MAP_NOCACHE)); |
| 169 | |
| 170 | if (!osc_freq) |
| 171 | osc_freq = clk_get_rate(clk_get_parent(clk)); |
| 172 | break; |
| 173 | default: |
| 174 | name = NULL; |
| 175 | clk = NULL; |
| 176 | printf("WARNING: %s has encountered unknown clk type %d\n", |
| 177 | __func__, ti_clk_data->type); |
| 178 | } |
| 179 | |
| 180 | if (clk && ti_clk_data->default_freq) |
| 181 | clk_set_rate(clk, ti_clk_data->default_freq); |
| 182 | |
| 183 | if (clk && name) { |
| 184 | for (j = 0; j < pdata->soc_dev_clk_data_cnt; j++) { |
| 185 | if (!strcmp(name, pdata->soc_dev_clk_data[j].clk_name)) { |
| 186 | clk_add_map(data, clk, pdata->soc_dev_clk_data[j].dev_id, |
| 187 | pdata->soc_dev_clk_data[j].clk_id); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int _clk_cmp(u32 dev_id, u32 clk_id, const struct clk_map *map) |
| 197 | { |
| 198 | if (map->dev_id == dev_id && map->clk_id == clk_id) |
| 199 | return 0; |
| 200 | if (map->dev_id > dev_id || |
| 201 | (map->dev_id == dev_id && map->clk_id > clk_id)) |
| 202 | return -1; |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | static int bsearch(u32 dev_id, u32 clk_id, struct clk_map *map, int num) |
| 207 | { |
| 208 | int result; |
| 209 | int idx; |
| 210 | |
| 211 | for (idx = 0; idx < num; idx++) { |
| 212 | result = _clk_cmp(dev_id, clk_id, &map[idx]); |
| 213 | |
| 214 | if (result == 0) |
| 215 | return idx; |
| 216 | } |
| 217 | |
| 218 | return -ENOENT; |
| 219 | } |
| 220 | |
| 221 | static int ti_clk_of_xlate(struct clk *clk, |
| 222 | struct ofnode_phandle_args *args) |
| 223 | { |
| 224 | struct ti_clk_data *data = dev_get_priv(clk->dev); |
| 225 | int idx; |
| 226 | |
| 227 | debug("%s(clk=%p, args_count=%d [0]=%d [1]=%d)\n", __func__, clk, |
| 228 | args->args_count, args->args[0], args->args[1]); |
| 229 | |
| 230 | if (args->args_count != 2) { |
| 231 | debug("Invalid args_count: %d\n", args->args_count); |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | if (!data->size) |
| 236 | return -EPROBE_DEFER; |
| 237 | |
| 238 | idx = bsearch(args->args[0], args->args[1], data->map, data->size); |
| 239 | if (idx < 0) |
| 240 | return idx; |
| 241 | |
| 242 | clk->id = idx; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static ulong ti_clk_get_rate(struct clk *clk) |
| 248 | { |
| 249 | struct ti_clk_data *data = dev_get_priv(clk->dev); |
| 250 | struct clk *clkp = data->map[clk->id].clk; |
| 251 | |
| 252 | return clk_get_rate(clkp); |
| 253 | } |
| 254 | |
| 255 | static ulong ti_clk_set_rate(struct clk *clk, ulong rate) |
| 256 | { |
| 257 | struct ti_clk_data *data = dev_get_priv(clk->dev); |
| 258 | struct clk *clkp = data->map[clk->id].clk; |
| 259 | int div = 1; |
| 260 | ulong child_rate; |
| 261 | const struct clk_ops *ops; |
| 262 | ulong new_rate, rem; |
| 263 | ulong diff, new_diff; |
Udit Kumar | c648daa | 2023-09-21 22:30:38 +0530 | [diff] [blame] | 264 | int freq_scale_up = rate >= ti_clk_get_rate(clk) ? 1 : 0; |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 265 | |
Udit Kumar | c648daa | 2023-09-21 22:30:38 +0530 | [diff] [blame] | 266 | if (IS_ENABLED(CONFIG_K3_AVS0) && freq_scale_up) |
| 267 | k3_avs_notify_freq(data->map[clk->id].dev_id, |
| 268 | data->map[clk->id].clk_id, rate); |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 269 | /* |
| 270 | * We must propagate rate change to parent if current clock type |
| 271 | * does not allow setting it. |
| 272 | */ |
| 273 | while (clkp) { |
| 274 | ops = clkp->dev->driver->ops; |
| 275 | if (ops->set_rate) |
| 276 | break; |
| 277 | |
| 278 | /* |
| 279 | * Store child rate so we can calculate the clock rate |
| 280 | * that must be passed to parent |
| 281 | */ |
| 282 | child_rate = clk_get_rate(clkp); |
| 283 | clkp = clk_get_parent(clkp); |
| 284 | if (clkp) { |
| 285 | debug("%s: propagating rate change to parent %s, rate=%u.\n", |
| 286 | __func__, clkp->dev->name, (u32)rate / div); |
| 287 | div *= clk_get_rate(clkp) / child_rate; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (!clkp) |
| 292 | return -ENOSYS; |
| 293 | |
| 294 | child_rate = clk_get_rate(clkp); |
| 295 | |
| 296 | new_rate = clk_set_rate(clkp, rate / div); |
| 297 | |
| 298 | diff = abs(new_rate - rate / div); |
| 299 | |
| 300 | debug("%s: clk=%s, div=%d, rate=%u, new_rate=%u, diff=%u\n", __func__, |
| 301 | clkp->dev->name, div, (u32)rate, (u32)new_rate, (u32)diff); |
| 302 | |
| 303 | /* |
| 304 | * If the new rate differs by 50% of the target, |
| 305 | * modify parent. This handles typical cases where we have a hsdiv |
| 306 | * following directly a PLL |
| 307 | */ |
| 308 | |
| 309 | if (diff > rate / div / 2) { |
| 310 | ulong pll_tgt; |
| 311 | int pll_div = 0; |
| 312 | |
| 313 | clk = clkp; |
| 314 | |
| 315 | debug("%s: propagating rate change to parent, rate=%u.\n", |
| 316 | __func__, (u32)rate / div); |
| 317 | |
| 318 | clkp = clk_get_parent(clkp); |
| 319 | |
| 320 | if (rate > osc_freq) { |
| 321 | if (rate > PLL_MAX_FREQ / 2 && rate < PLL_MAX_FREQ) { |
| 322 | pll_tgt = rate; |
| 323 | pll_div = 1; |
| 324 | } else { |
| 325 | for (pll_div = 2; pll_div < PLL_MAX_DIV; pll_div++) { |
| 326 | pll_tgt = rate / div * pll_div; |
| 327 | if (pll_tgt >= PLL_MIN_FREQ && pll_tgt <= PLL_MAX_FREQ) |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } else { |
| 332 | pll_tgt = osc_freq; |
| 333 | pll_div = rate / div / osc_freq; |
| 334 | } |
| 335 | |
| 336 | debug("%s: pll_tgt=%u, rate=%u, div=%u\n", __func__, |
| 337 | (u32)pll_tgt, (u32)rate, pll_div); |
| 338 | |
| 339 | clk_set_rate(clkp, pll_tgt); |
| 340 | |
| 341 | return clk_set_rate(clk, rate / div) * div; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * If the new rate differs by at least 5% of the target, |
| 346 | * we must check for rounding error in a divider, so try |
| 347 | * set rate with rate + (parent_freq % rate). |
| 348 | */ |
| 349 | |
| 350 | if (diff > rate / div / 20) { |
| 351 | u64 parent_freq = clk_get_parent_rate(clkp); |
| 352 | |
| 353 | rem = parent_freq % rate; |
| 354 | new_rate = clk_set_rate(clkp, (rate / div) + rem); |
| 355 | new_diff = abs(new_rate - rate / div); |
| 356 | |
| 357 | if (new_diff > diff) { |
| 358 | new_rate = clk_set_rate(clkp, rate / div); |
| 359 | } else { |
| 360 | debug("%s: Using better rate %lu that gives diff %lu\n", |
| 361 | __func__, new_rate, new_diff); |
| 362 | } |
| 363 | } |
| 364 | |
Udit Kumar | c648daa | 2023-09-21 22:30:38 +0530 | [diff] [blame] | 365 | if (IS_ENABLED(CONFIG_K3_AVS0) && !freq_scale_up) |
| 366 | k3_avs_notify_freq(data->map[clk->id].dev_id, |
| 367 | data->map[clk->id].clk_id, rate); |
| 368 | |
Tero Kristo | 82ceb0d | 2021-06-11 11:45:14 +0300 | [diff] [blame] | 369 | return new_rate; |
| 370 | } |
| 371 | |
| 372 | static int ti_clk_set_parent(struct clk *clk, struct clk *parent) |
| 373 | { |
| 374 | struct ti_clk_data *data = dev_get_priv(clk->dev); |
| 375 | struct clk *clkp = data->map[clk->id].clk; |
| 376 | struct clk *parentp = data->map[parent->id].clk; |
| 377 | |
| 378 | return clk_set_parent(clkp, parentp); |
| 379 | } |
| 380 | |
| 381 | static int ti_clk_enable(struct clk *clk) |
| 382 | { |
| 383 | struct ti_clk_data *data = dev_get_priv(clk->dev); |
| 384 | struct clk *clkp = data->map[clk->id].clk; |
| 385 | |
| 386 | return clk_enable(clkp); |
| 387 | } |
| 388 | |
| 389 | static int ti_clk_disable(struct clk *clk) |
| 390 | { |
| 391 | struct ti_clk_data *data = dev_get_priv(clk->dev); |
| 392 | struct clk *clkp = data->map[clk->id].clk; |
| 393 | |
| 394 | return clk_disable(clkp); |
| 395 | } |
| 396 | |
| 397 | static const struct udevice_id ti_clk_of_match[] = { |
| 398 | { .compatible = "ti,k2g-sci-clk" }, |
| 399 | { /* sentinel */ }, |
| 400 | }; |
| 401 | |
| 402 | static const struct clk_ops ti_clk_ops = { |
| 403 | .of_xlate = ti_clk_of_xlate, |
| 404 | .set_rate = ti_clk_set_rate, |
| 405 | .get_rate = ti_clk_get_rate, |
| 406 | .enable = ti_clk_enable, |
| 407 | .disable = ti_clk_disable, |
| 408 | .set_parent = ti_clk_set_parent, |
| 409 | }; |
| 410 | |
| 411 | U_BOOT_DRIVER(ti_clk) = { |
| 412 | .name = "ti-clk", |
| 413 | .id = UCLASS_CLK, |
| 414 | .of_match = ti_clk_of_match, |
| 415 | .probe = ti_clk_probe, |
| 416 | .priv_auto = sizeof(struct ti_clk_data), |
| 417 | .ops = &ti_clk_ops, |
| 418 | }; |