1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PLL clock driver for the Mobileye EyeQ5, EyeQ6L and EyeQ6H platforms. 4 * 5 * This controller handles read-only PLLs, all derived from the same main 6 * crystal clock. It also exposes divider clocks, those are children to PLLs. 7 * Parent clock is expected to be constant. This driver's registers live in 8 * a shared region called OLB. Some PLLs are initialised early by of_clk_init(); 9 * if so, two clk providers are registered. 10 * 11 * We use eqc_ as prefix, as-in "EyeQ Clock", but way shorter. 12 * 13 * Copyright (C) 2024 Mobileye Vision Technologies Ltd. 14 */ 15 16 /* 17 * Set pr_fmt() for printing from eqc_early_init(). 18 * It is called at of_clk_init() stage (read: really early). 19 */ 20 #define pr_fmt(fmt) "clk-eyeq: " fmt 21 22 #include <linux/array_size.h> 23 #include <linux/auxiliary_bus.h> 24 #include <linux/bitfield.h> 25 #include <linux/bits.h> 26 #include <linux/clk-provider.h> 27 #include <linux/device.h> 28 #include <linux/err.h> 29 #include <linux/errno.h> 30 #include <linux/init.h> 31 #include <linux/io-64-nonatomic-hi-lo.h> 32 #include <linux/io.h> 33 #include <linux/mod_devicetable.h> 34 #include <linux/module.h> 35 #include <linux/of.h> 36 #include <linux/of_address.h> 37 #include <linux/overflow.h> 38 #include <linux/platform_device.h> 39 #include <linux/printk.h> 40 #include <linux/slab.h> 41 #include <linux/spinlock.h> 42 #include <linux/types.h> 43 44 #include <dt-bindings/clock/mobileye,eyeq5-clk.h> 45 46 /* In frac mode, it enables fractional noise canceling DAC. Else, no function. */ 47 #define PCSR0_DAC_EN BIT(0) 48 /* Fractional or integer mode */ 49 #define PCSR0_DSM_EN BIT(1) 50 #define PCSR0_PLL_EN BIT(2) 51 /* All clocks output held at 0 */ 52 #define PCSR0_FOUTPOSTDIV_EN BIT(3) 53 #define PCSR0_POST_DIV1 GENMASK(6, 4) 54 #define PCSR0_POST_DIV2 GENMASK(9, 7) 55 #define PCSR0_REF_DIV GENMASK(15, 10) 56 #define PCSR0_INTIN GENMASK(27, 16) 57 #define PCSR0_BYPASS BIT(28) 58 /* Bits 30..29 are reserved */ 59 #define PCSR0_PLL_LOCKED BIT(31) 60 61 #define PCSR1_RESET BIT(0) 62 #define PCSR1_SSGC_DIV GENMASK(4, 1) 63 /* Spread amplitude (% = 0.1 * SPREAD[4:0]) */ 64 #define PCSR1_SPREAD GENMASK(9, 5) 65 #define PCSR1_DIS_SSCG BIT(10) 66 /* Down-spread or center-spread */ 67 #define PCSR1_DOWN_SPREAD BIT(11) 68 #define PCSR1_FRAC_IN GENMASK(31, 12) 69 70 struct eqc_pll { 71 unsigned int index; 72 const char *name; 73 unsigned int reg64; 74 }; 75 76 /* 77 * Divider clock. Divider is 2*(v+1), with v the register value. 78 * Min divider is 2, max is 2*(2^width). 79 */ 80 struct eqc_div { 81 unsigned int index; 82 const char *name; 83 unsigned int parent; 84 unsigned int reg; 85 u8 shift; 86 u8 width; 87 }; 88 89 struct eqc_match_data { 90 unsigned int pll_count; 91 const struct eqc_pll *plls; 92 93 unsigned int div_count; 94 const struct eqc_div *divs; 95 96 const char *reset_auxdev_name; 97 const char *pinctrl_auxdev_name; 98 99 unsigned int early_clk_count; 100 }; 101 102 struct eqc_early_match_data { 103 unsigned int early_pll_count; 104 const struct eqc_pll *early_plls; 105 106 /* 107 * We want our of_xlate callback to EPROBE_DEFER instead of dev_err() 108 * and EINVAL. For that, we must know the total clock count. 109 */ 110 unsigned int late_clk_count; 111 }; 112 113 /* 114 * Both factors (mult and div) must fit in 32 bits. When an operation overflows, 115 * this function throws away low bits so that factors still fit in 32 bits. 116 * 117 * Precision loss depends on amplitude of mult and div. Worst theorical 118 * loss is: (UINT_MAX+1) / UINT_MAX - 1 = 2.3e-10. 119 * This is 1Hz every 4.3GHz. 120 */ 121 static void eqc_pll_downshift_factors(unsigned long *mult, unsigned long *div) 122 { 123 unsigned long biggest; 124 unsigned int shift; 125 126 /* This function can be removed if mult/div switch to unsigned long. */ 127 static_assert(sizeof_field(struct clk_fixed_factor, mult) == sizeof(unsigned int)); 128 static_assert(sizeof_field(struct clk_fixed_factor, div) == sizeof(unsigned int)); 129 130 /* No overflow, nothing to be done. */ 131 if (*mult <= UINT_MAX && *div <= UINT_MAX) 132 return; 133 134 /* 135 * Compute the shift required to bring the biggest factor into unsigned 136 * int range. That is, shift its highest set bit to the unsigned int 137 * most significant bit. 138 */ 139 biggest = max(*mult, *div); 140 shift = __fls(biggest) - (BITS_PER_BYTE * sizeof(unsigned int)) + 1; 141 142 *mult >>= shift; 143 *div >>= shift; 144 } 145 146 static int eqc_pll_parse_registers(u32 r0, u32 r1, unsigned long *mult, 147 unsigned long *div, unsigned long *acc) 148 { 149 u32 spread; 150 151 if (r0 & PCSR0_BYPASS) { 152 *mult = 1; 153 *div = 1; 154 *acc = 0; 155 return 0; 156 } 157 158 if (!(r0 & PCSR0_PLL_LOCKED)) 159 return -EINVAL; 160 161 *mult = FIELD_GET(PCSR0_INTIN, r0); 162 *div = FIELD_GET(PCSR0_REF_DIV, r0); 163 if (r0 & PCSR0_FOUTPOSTDIV_EN) 164 *div *= FIELD_GET(PCSR0_POST_DIV1, r0) * FIELD_GET(PCSR0_POST_DIV2, r0); 165 166 /* Fractional mode, in 2^20 (0x100000) parts. */ 167 if (r0 & PCSR0_DSM_EN) { 168 *div *= (1ULL << 20); 169 *mult = *mult * (1ULL << 20) + FIELD_GET(PCSR1_FRAC_IN, r1); 170 } 171 172 if (!*mult || !*div) 173 return -EINVAL; 174 175 if (r1 & (PCSR1_RESET | PCSR1_DIS_SSCG)) { 176 *acc = 0; 177 return 0; 178 } 179 180 /* 181 * Spread spectrum. 182 * 183 * Spread is 1/1000 parts of frequency, accuracy is half of 184 * that. To get accuracy, convert to ppb (parts per billion). 185 * 186 * acc = spread * 1e6 / 2 187 * with acc in parts per billion and, 188 * spread in parts per thousand. 189 */ 190 spread = FIELD_GET(PCSR1_SPREAD, r1); 191 *acc = spread * 500000; 192 193 if (r1 & PCSR1_DOWN_SPREAD) { 194 /* 195 * Downspreading: the central frequency is half a 196 * spread lower. 197 */ 198 *mult *= 2000 - spread; 199 *div *= 2000; 200 201 /* 202 * Previous operation might overflow 32 bits. If it 203 * does, throw away the least amount of low bits. 204 */ 205 eqc_pll_downshift_factors(mult, div); 206 } 207 208 return 0; 209 } 210 211 static void eqc_probe_init_plls(struct device *dev, const struct eqc_match_data *data, 212 void __iomem *base, struct clk_hw_onecell_data *cells) 213 { 214 unsigned long mult, div, acc; 215 const struct eqc_pll *pll; 216 struct clk_hw *hw; 217 unsigned int i; 218 u32 r0, r1; 219 u64 val; 220 int ret; 221 222 for (i = 0; i < data->pll_count; i++) { 223 pll = &data->plls[i]; 224 225 val = readq(base + pll->reg64); 226 r0 = val; 227 r1 = val >> 32; 228 229 ret = eqc_pll_parse_registers(r0, r1, &mult, &div, &acc); 230 if (ret) { 231 dev_warn(dev, "failed parsing state of %s\n", pll->name); 232 cells->hws[pll->index] = ERR_PTR(ret); 233 continue; 234 } 235 236 hw = clk_hw_register_fixed_factor_with_accuracy_fwname(dev, 237 dev->of_node, pll->name, "ref", 0, mult, div, acc); 238 cells->hws[pll->index] = hw; 239 if (IS_ERR(hw)) 240 dev_warn(dev, "failed registering %s: %pe\n", pll->name, hw); 241 } 242 } 243 244 static void eqc_probe_init_divs(struct device *dev, const struct eqc_match_data *data, 245 void __iomem *base, struct clk_hw_onecell_data *cells) 246 { 247 struct clk_parent_data parent_data = { }; 248 const struct eqc_div *div; 249 struct clk_hw *parent; 250 void __iomem *reg; 251 struct clk_hw *hw; 252 unsigned int i; 253 254 for (i = 0; i < data->div_count; i++) { 255 div = &data->divs[i]; 256 reg = base + div->reg; 257 parent = cells->hws[div->parent]; 258 259 if (IS_ERR(parent)) { 260 /* Parent is in early clk provider. */ 261 parent_data.index = div->parent; 262 parent_data.hw = NULL; 263 } else { 264 /* Avoid clock lookup when we already have the hw reference. */ 265 parent_data.index = 0; 266 parent_data.hw = parent; 267 } 268 269 hw = clk_hw_register_divider_table_parent_data(dev, div->name, 270 &parent_data, 0, reg, div->shift, div->width, 271 CLK_DIVIDER_EVEN_INTEGERS, NULL, NULL); 272 cells->hws[div->index] = hw; 273 if (IS_ERR(hw)) 274 dev_warn(dev, "failed registering %s: %pe\n", 275 div->name, hw); 276 } 277 } 278 279 static void eqc_auxdev_release(struct device *dev) 280 { 281 struct auxiliary_device *adev = to_auxiliary_dev(dev); 282 283 kfree(adev); 284 } 285 286 static int eqc_auxdev_create(struct device *dev, void __iomem *base, 287 const char *name, u32 id) 288 { 289 struct auxiliary_device *adev; 290 int ret; 291 292 adev = kzalloc(sizeof(*adev), GFP_KERNEL); 293 if (!adev) 294 return -ENOMEM; 295 296 adev->name = name; 297 adev->dev.parent = dev; 298 adev->dev.platform_data = (void __force *)base; 299 adev->dev.release = eqc_auxdev_release; 300 adev->id = id; 301 302 ret = auxiliary_device_init(adev); 303 if (ret) 304 return ret; 305 306 ret = auxiliary_device_add(adev); 307 if (ret) 308 auxiliary_device_uninit(adev); 309 310 return ret; 311 } 312 313 static int eqc_probe(struct platform_device *pdev) 314 { 315 struct device *dev = &pdev->dev; 316 struct device_node *np = dev->of_node; 317 const struct eqc_match_data *data; 318 struct clk_hw_onecell_data *cells; 319 unsigned int i, clk_count; 320 struct resource *res; 321 void __iomem *base; 322 int ret; 323 324 data = device_get_match_data(dev); 325 if (!data) 326 return 0; /* No clocks nor auxdevs, we are done. */ 327 328 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 329 if (!res) 330 return -ENODEV; 331 332 base = ioremap(res->start, resource_size(res)); 333 if (!base) 334 return -ENOMEM; 335 336 /* Init optional reset auxiliary device. */ 337 if (data->reset_auxdev_name) { 338 ret = eqc_auxdev_create(dev, base, data->reset_auxdev_name, 0); 339 if (ret) 340 dev_warn(dev, "failed creating auxiliary device %s.%s: %d\n", 341 KBUILD_MODNAME, data->reset_auxdev_name, ret); 342 } 343 344 /* Init optional pinctrl auxiliary device. */ 345 if (data->pinctrl_auxdev_name) { 346 ret = eqc_auxdev_create(dev, base, data->pinctrl_auxdev_name, 0); 347 if (ret) 348 dev_warn(dev, "failed creating auxiliary device %s.%s: %d\n", 349 KBUILD_MODNAME, data->pinctrl_auxdev_name, ret); 350 } 351 352 if (data->pll_count + data->div_count == 0) 353 return 0; /* Zero clocks, we are done. */ 354 355 clk_count = data->pll_count + data->div_count + data->early_clk_count; 356 cells = kzalloc(struct_size(cells, hws, clk_count), GFP_KERNEL); 357 if (!cells) 358 return -ENOMEM; 359 360 cells->num = clk_count; 361 362 /* Early PLLs are marked as errors: the early provider will get queried. */ 363 for (i = 0; i < clk_count; i++) 364 cells->hws[i] = ERR_PTR(-EINVAL); 365 366 eqc_probe_init_plls(dev, data, base, cells); 367 368 eqc_probe_init_divs(dev, data, base, cells); 369 370 return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, cells); 371 } 372 373 /* Required early for GIC timer (pll-cpu) and UARTs (pll-per). */ 374 static const struct eqc_pll eqc_eyeq5_early_plls[] = { 375 { .index = EQ5C_PLL_CPU, .name = "pll-cpu", .reg64 = 0x02C }, 376 { .index = EQ5C_PLL_PER, .name = "pll-per", .reg64 = 0x05C }, 377 }; 378 379 static const struct eqc_pll eqc_eyeq5_plls[] = { 380 { .index = EQ5C_PLL_VMP, .name = "pll-vmp", .reg64 = 0x034 }, 381 { .index = EQ5C_PLL_PMA, .name = "pll-pma", .reg64 = 0x03C }, 382 { .index = EQ5C_PLL_VDI, .name = "pll-vdi", .reg64 = 0x044 }, 383 { .index = EQ5C_PLL_DDR0, .name = "pll-ddr0", .reg64 = 0x04C }, 384 { .index = EQ5C_PLL_PCI, .name = "pll-pci", .reg64 = 0x054 }, 385 { .index = EQ5C_PLL_PMAC, .name = "pll-pmac", .reg64 = 0x064 }, 386 { .index = EQ5C_PLL_MPC, .name = "pll-mpc", .reg64 = 0x06C }, 387 { .index = EQ5C_PLL_DDR1, .name = "pll-ddr1", .reg64 = 0x074 }, 388 }; 389 390 static const struct eqc_div eqc_eyeq5_divs[] = { 391 { 392 .index = EQ5C_DIV_OSPI, 393 .name = "div-ospi", 394 .parent = EQ5C_PLL_PER, 395 .reg = 0x11C, 396 .shift = 0, 397 .width = 4, 398 }, 399 }; 400 401 static const struct eqc_early_match_data eqc_eyeq5_early_match_data __initconst = { 402 .early_pll_count = ARRAY_SIZE(eqc_eyeq5_early_plls), 403 .early_plls = eqc_eyeq5_early_plls, 404 405 .late_clk_count = ARRAY_SIZE(eqc_eyeq5_plls) + ARRAY_SIZE(eqc_eyeq5_divs), 406 }; 407 408 static const struct eqc_match_data eqc_eyeq5_match_data = { 409 .pll_count = ARRAY_SIZE(eqc_eyeq5_plls), 410 .plls = eqc_eyeq5_plls, 411 412 .div_count = ARRAY_SIZE(eqc_eyeq5_divs), 413 .divs = eqc_eyeq5_divs, 414 415 .reset_auxdev_name = "reset", 416 .pinctrl_auxdev_name = "pinctrl", 417 418 .early_clk_count = ARRAY_SIZE(eqc_eyeq5_early_plls), 419 }; 420 421 static const struct eqc_pll eqc_eyeq6l_plls[] = { 422 { .index = EQ6LC_PLL_DDR, .name = "pll-ddr", .reg64 = 0x02C }, 423 { .index = EQ6LC_PLL_CPU, .name = "pll-cpu", .reg64 = 0x034 }, /* also acc */ 424 { .index = EQ6LC_PLL_PER, .name = "pll-per", .reg64 = 0x03C }, 425 { .index = EQ6LC_PLL_VDI, .name = "pll-vdi", .reg64 = 0x044 }, 426 }; 427 428 static const struct eqc_match_data eqc_eyeq6l_match_data = { 429 .pll_count = ARRAY_SIZE(eqc_eyeq6l_plls), 430 .plls = eqc_eyeq6l_plls, 431 432 .reset_auxdev_name = "reset", 433 }; 434 435 static const struct eqc_match_data eqc_eyeq6h_west_match_data = { 436 .reset_auxdev_name = "reset_west", 437 }; 438 439 static const struct eqc_pll eqc_eyeq6h_east_plls[] = { 440 { .index = 0, .name = "pll-east", .reg64 = 0x074 }, 441 }; 442 443 static const struct eqc_match_data eqc_eyeq6h_east_match_data = { 444 .pll_count = ARRAY_SIZE(eqc_eyeq6h_east_plls), 445 .plls = eqc_eyeq6h_east_plls, 446 447 .reset_auxdev_name = "reset_east", 448 }; 449 450 static const struct eqc_pll eqc_eyeq6h_south_plls[] = { 451 { .index = EQ6HC_SOUTH_PLL_VDI, .name = "pll-vdi", .reg64 = 0x000 }, 452 { .index = EQ6HC_SOUTH_PLL_PCIE, .name = "pll-pcie", .reg64 = 0x008 }, 453 { .index = EQ6HC_SOUTH_PLL_PER, .name = "pll-per", .reg64 = 0x010 }, 454 { .index = EQ6HC_SOUTH_PLL_ISP, .name = "pll-isp", .reg64 = 0x018 }, 455 }; 456 457 static const struct eqc_div eqc_eyeq6h_south_divs[] = { 458 { 459 .index = EQ6HC_SOUTH_DIV_EMMC, 460 .name = "div-emmc", 461 .parent = EQ6HC_SOUTH_PLL_PER, 462 .reg = 0x070, 463 .shift = 4, 464 .width = 4, 465 }, 466 { 467 .index = EQ6HC_SOUTH_DIV_OSPI_REF, 468 .name = "div-ospi-ref", 469 .parent = EQ6HC_SOUTH_PLL_PER, 470 .reg = 0x090, 471 .shift = 4, 472 .width = 4, 473 }, 474 { 475 .index = EQ6HC_SOUTH_DIV_OSPI_SYS, 476 .name = "div-ospi-sys", 477 .parent = EQ6HC_SOUTH_PLL_PER, 478 .reg = 0x090, 479 .shift = 8, 480 .width = 1, 481 }, 482 { 483 .index = EQ6HC_SOUTH_DIV_TSU, 484 .name = "div-tsu", 485 .parent = EQ6HC_SOUTH_PLL_PCIE, 486 .reg = 0x098, 487 .shift = 4, 488 .width = 8, 489 }, 490 }; 491 492 static const struct eqc_match_data eqc_eyeq6h_south_match_data = { 493 .pll_count = ARRAY_SIZE(eqc_eyeq6h_south_plls), 494 .plls = eqc_eyeq6h_south_plls, 495 496 .div_count = ARRAY_SIZE(eqc_eyeq6h_south_divs), 497 .divs = eqc_eyeq6h_south_divs, 498 }; 499 500 static const struct eqc_pll eqc_eyeq6h_ddr0_plls[] = { 501 { .index = 0, .name = "pll-ddr0", .reg64 = 0x074 }, 502 }; 503 504 static const struct eqc_match_data eqc_eyeq6h_ddr0_match_data = { 505 .pll_count = ARRAY_SIZE(eqc_eyeq6h_ddr0_plls), 506 .plls = eqc_eyeq6h_ddr0_plls, 507 }; 508 509 static const struct eqc_pll eqc_eyeq6h_ddr1_plls[] = { 510 { .index = 0, .name = "pll-ddr1", .reg64 = 0x074 }, 511 }; 512 513 static const struct eqc_match_data eqc_eyeq6h_ddr1_match_data = { 514 .pll_count = ARRAY_SIZE(eqc_eyeq6h_ddr1_plls), 515 .plls = eqc_eyeq6h_ddr1_plls, 516 }; 517 518 static const struct eqc_pll eqc_eyeq6h_acc_plls[] = { 519 { .index = EQ6HC_ACC_PLL_XNN, .name = "pll-xnn", .reg64 = 0x040 }, 520 { .index = EQ6HC_ACC_PLL_VMP, .name = "pll-vmp", .reg64 = 0x050 }, 521 { .index = EQ6HC_ACC_PLL_PMA, .name = "pll-pma", .reg64 = 0x05C }, 522 { .index = EQ6HC_ACC_PLL_MPC, .name = "pll-mpc", .reg64 = 0x068 }, 523 { .index = EQ6HC_ACC_PLL_NOC, .name = "pll-noc", .reg64 = 0x070 }, 524 }; 525 526 static const struct eqc_match_data eqc_eyeq6h_acc_match_data = { 527 .pll_count = ARRAY_SIZE(eqc_eyeq6h_acc_plls), 528 .plls = eqc_eyeq6h_acc_plls, 529 530 .reset_auxdev_name = "reset_acc", 531 }; 532 533 static const struct of_device_id eqc_match_table[] = { 534 { .compatible = "mobileye,eyeq5-olb", .data = &eqc_eyeq5_match_data }, 535 { .compatible = "mobileye,eyeq6l-olb", .data = &eqc_eyeq6l_match_data }, 536 { .compatible = "mobileye,eyeq6h-west-olb", .data = &eqc_eyeq6h_west_match_data }, 537 { .compatible = "mobileye,eyeq6h-east-olb", .data = &eqc_eyeq6h_east_match_data }, 538 { .compatible = "mobileye,eyeq6h-south-olb", .data = &eqc_eyeq6h_south_match_data }, 539 { .compatible = "mobileye,eyeq6h-ddr0-olb", .data = &eqc_eyeq6h_ddr0_match_data }, 540 { .compatible = "mobileye,eyeq6h-ddr1-olb", .data = &eqc_eyeq6h_ddr1_match_data }, 541 { .compatible = "mobileye,eyeq6h-acc-olb", .data = &eqc_eyeq6h_acc_match_data }, 542 {} 543 }; 544 545 static struct platform_driver eqc_driver = { 546 .probe = eqc_probe, 547 .driver = { 548 .name = "clk-eyeq", 549 .of_match_table = eqc_match_table, 550 .suppress_bind_attrs = true, 551 }, 552 }; 553 builtin_platform_driver(eqc_driver); 554 555 /* Required early for GIC timer. */ 556 static const struct eqc_pll eqc_eyeq6h_central_early_plls[] = { 557 { .index = 0, .name = "pll-cpu", .reg64 = 0x02C }, 558 }; 559 560 static const struct eqc_early_match_data eqc_eyeq6h_central_early_match_data __initconst = { 561 .early_pll_count = ARRAY_SIZE(eqc_eyeq6h_central_early_plls), 562 .early_plls = eqc_eyeq6h_central_early_plls, 563 }; 564 565 /* Required early for UART. */ 566 static const struct eqc_pll eqc_eyeq6h_west_early_plls[] = { 567 { .index = 0, .name = "pll-west", .reg64 = 0x074 }, 568 }; 569 570 static const struct eqc_early_match_data eqc_eyeq6h_west_early_match_data __initconst = { 571 .early_pll_count = ARRAY_SIZE(eqc_eyeq6h_west_early_plls), 572 .early_plls = eqc_eyeq6h_west_early_plls, 573 }; 574 575 static void __init eqc_early_init(struct device_node *np, 576 const struct eqc_early_match_data *early_data) 577 { 578 struct clk_hw_onecell_data *cells; 579 unsigned int i, clk_count; 580 void __iomem *base; 581 int ret; 582 583 clk_count = early_data->early_pll_count + early_data->late_clk_count; 584 cells = kzalloc(struct_size(cells, hws, clk_count), GFP_KERNEL); 585 if (!cells) { 586 ret = -ENOMEM; 587 goto err; 588 } 589 590 cells->num = clk_count; 591 592 /* 593 * Mark all clocks as deferred; some are registered here, the rest at 594 * platform device probe. 595 * 596 * Once the platform device is probed, its provider will take priority 597 * when looking up clocks. 598 */ 599 for (i = 0; i < clk_count; i++) 600 cells->hws[i] = ERR_PTR(-EPROBE_DEFER); 601 602 /* Offsets (reg64) of early PLLs are relative to OLB block. */ 603 base = of_iomap(np, 0); 604 if (!base) { 605 ret = -ENODEV; 606 goto err; 607 } 608 609 for (i = 0; i < early_data->early_pll_count; i++) { 610 const struct eqc_pll *pll = &early_data->early_plls[i]; 611 unsigned long mult, div, acc; 612 struct clk_hw *hw; 613 u32 r0, r1; 614 u64 val; 615 616 val = readq(base + pll->reg64); 617 r0 = val; 618 r1 = val >> 32; 619 620 ret = eqc_pll_parse_registers(r0, r1, &mult, &div, &acc); 621 if (ret) { 622 pr_err("failed parsing state of %s\n", pll->name); 623 goto err; 624 } 625 626 hw = clk_hw_register_fixed_factor_with_accuracy_fwname(NULL, 627 np, pll->name, "ref", 0, mult, div, acc); 628 cells->hws[pll->index] = hw; 629 if (IS_ERR(hw)) { 630 pr_err("failed registering %s: %pe\n", pll->name, hw); 631 ret = PTR_ERR(hw); 632 goto err; 633 } 634 } 635 636 ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, cells); 637 if (ret) { 638 pr_err("failed registering clk provider: %d\n", ret); 639 goto err; 640 } 641 642 return; 643 644 err: 645 /* 646 * We are doomed. The system will not be able to boot. 647 * 648 * Let's still try to be good citizens by freeing resources and print 649 * a last error message that might help debugging. 650 */ 651 652 pr_err("failed clk init: %d\n", ret); 653 654 if (cells) { 655 of_clk_del_provider(np); 656 657 for (i = 0; i < early_data->early_pll_count; i++) { 658 const struct eqc_pll *pll = &early_data->early_plls[i]; 659 struct clk_hw *hw = cells->hws[pll->index]; 660 661 if (!IS_ERR_OR_NULL(hw)) 662 clk_hw_unregister_fixed_factor(hw); 663 } 664 665 kfree(cells); 666 } 667 } 668 669 static void __init eqc_eyeq5_early_init(struct device_node *np) 670 { 671 eqc_early_init(np, &eqc_eyeq5_early_match_data); 672 } 673 CLK_OF_DECLARE_DRIVER(eqc_eyeq5, "mobileye,eyeq5-olb", eqc_eyeq5_early_init); 674 675 static void __init eqc_eyeq6h_central_early_init(struct device_node *np) 676 { 677 eqc_early_init(np, &eqc_eyeq6h_central_early_match_data); 678 } 679 CLK_OF_DECLARE_DRIVER(eqc_eyeq6h_central, "mobileye,eyeq6h-central-olb", 680 eqc_eyeq6h_central_early_init); 681 682 static void __init eqc_eyeq6h_west_early_init(struct device_node *np) 683 { 684 eqc_early_init(np, &eqc_eyeq6h_west_early_match_data); 685 } 686 CLK_OF_DECLARE_DRIVER(eqc_eyeq6h_west, "mobileye,eyeq6h-west-olb", 687 eqc_eyeq6h_west_early_init); 688