1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2010,2015 Broadcom 4 * Copyright (C) 2012 Stephen Warren 5 */ 6 7 /** 8 * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain) 9 * 10 * The clock tree on the 2835 has several levels. There's a root 11 * oscillator running at 19.2Mhz. After the oscillator there are 5 12 * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays", 13 * and "HDMI displays". Those 5 PLLs each can divide their output to 14 * produce up to 4 channels. Finally, there is the level of clocks to 15 * be consumed by other hardware components (like "H264" or "HDMI 16 * state machine"), which divide off of some subset of the PLL 17 * channels. 18 * 19 * All of the clocks in the tree are exposed in the DT, because the DT 20 * may want to make assignments of the final layer of clocks to the 21 * PLL channels, and some components of the hardware will actually 22 * skip layers of the tree (for example, the pixel clock comes 23 * directly from the PLLH PIX channel without using a CM_*CTL clock 24 * generator). 25 */ 26 27 #include <linux/clk-provider.h> 28 #include <linux/clkdev.h> 29 #include <linux/clk.h> 30 #include <linux/debugfs.h> 31 #include <linux/delay.h> 32 #include <linux/io.h> 33 #include <linux/module.h> 34 #include <linux/of_device.h> 35 #include <linux/platform_device.h> 36 #include <linux/slab.h> 37 #include <dt-bindings/clock/bcm2835.h> 38 39 #define CM_PASSWORD 0x5a000000 40 41 #define CM_GNRICCTL 0x000 42 #define CM_GNRICDIV 0x004 43 # define CM_DIV_FRAC_BITS 12 44 # define CM_DIV_FRAC_MASK GENMASK(CM_DIV_FRAC_BITS - 1, 0) 45 46 #define CM_VPUCTL 0x008 47 #define CM_VPUDIV 0x00c 48 #define CM_SYSCTL 0x010 49 #define CM_SYSDIV 0x014 50 #define CM_PERIACTL 0x018 51 #define CM_PERIADIV 0x01c 52 #define CM_PERIICTL 0x020 53 #define CM_PERIIDIV 0x024 54 #define CM_H264CTL 0x028 55 #define CM_H264DIV 0x02c 56 #define CM_ISPCTL 0x030 57 #define CM_ISPDIV 0x034 58 #define CM_V3DCTL 0x038 59 #define CM_V3DDIV 0x03c 60 #define CM_CAM0CTL 0x040 61 #define CM_CAM0DIV 0x044 62 #define CM_CAM1CTL 0x048 63 #define CM_CAM1DIV 0x04c 64 #define CM_CCP2CTL 0x050 65 #define CM_CCP2DIV 0x054 66 #define CM_DSI0ECTL 0x058 67 #define CM_DSI0EDIV 0x05c 68 #define CM_DSI0PCTL 0x060 69 #define CM_DSI0PDIV 0x064 70 #define CM_DPICTL 0x068 71 #define CM_DPIDIV 0x06c 72 #define CM_GP0CTL 0x070 73 #define CM_GP0DIV 0x074 74 #define CM_GP1CTL 0x078 75 #define CM_GP1DIV 0x07c 76 #define CM_GP2CTL 0x080 77 #define CM_GP2DIV 0x084 78 #define CM_HSMCTL 0x088 79 #define CM_HSMDIV 0x08c 80 #define CM_OTPCTL 0x090 81 #define CM_OTPDIV 0x094 82 #define CM_PCMCTL 0x098 83 #define CM_PCMDIV 0x09c 84 #define CM_PWMCTL 0x0a0 85 #define CM_PWMDIV 0x0a4 86 #define CM_SLIMCTL 0x0a8 87 #define CM_SLIMDIV 0x0ac 88 #define CM_SMICTL 0x0b0 89 #define CM_SMIDIV 0x0b4 90 /* no definition for 0x0b8 and 0x0bc */ 91 #define CM_TCNTCTL 0x0c0 92 # define CM_TCNT_SRC1_SHIFT 12 93 #define CM_TCNTCNT 0x0c4 94 #define CM_TECCTL 0x0c8 95 #define CM_TECDIV 0x0cc 96 #define CM_TD0CTL 0x0d0 97 #define CM_TD0DIV 0x0d4 98 #define CM_TD1CTL 0x0d8 99 #define CM_TD1DIV 0x0dc 100 #define CM_TSENSCTL 0x0e0 101 #define CM_TSENSDIV 0x0e4 102 #define CM_TIMERCTL 0x0e8 103 #define CM_TIMERDIV 0x0ec 104 #define CM_UARTCTL 0x0f0 105 #define CM_UARTDIV 0x0f4 106 #define CM_VECCTL 0x0f8 107 #define CM_VECDIV 0x0fc 108 #define CM_PULSECTL 0x190 109 #define CM_PULSEDIV 0x194 110 #define CM_SDCCTL 0x1a8 111 #define CM_SDCDIV 0x1ac 112 #define CM_ARMCTL 0x1b0 113 #define CM_AVEOCTL 0x1b8 114 #define CM_AVEODIV 0x1bc 115 #define CM_EMMCCTL 0x1c0 116 #define CM_EMMCDIV 0x1c4 117 #define CM_EMMC2CTL 0x1d0 118 #define CM_EMMC2DIV 0x1d4 119 120 /* General bits for the CM_*CTL regs */ 121 # define CM_ENABLE BIT(4) 122 # define CM_KILL BIT(5) 123 # define CM_GATE_BIT 6 124 # define CM_GATE BIT(CM_GATE_BIT) 125 # define CM_BUSY BIT(7) 126 # define CM_BUSYD BIT(8) 127 # define CM_FRAC BIT(9) 128 # define CM_SRC_SHIFT 0 129 # define CM_SRC_BITS 4 130 # define CM_SRC_MASK 0xf 131 # define CM_SRC_GND 0 132 # define CM_SRC_OSC 1 133 # define CM_SRC_TESTDEBUG0 2 134 # define CM_SRC_TESTDEBUG1 3 135 # define CM_SRC_PLLA_CORE 4 136 # define CM_SRC_PLLA_PER 4 137 # define CM_SRC_PLLC_CORE0 5 138 # define CM_SRC_PLLC_PER 5 139 # define CM_SRC_PLLC_CORE1 8 140 # define CM_SRC_PLLD_CORE 6 141 # define CM_SRC_PLLD_PER 6 142 # define CM_SRC_PLLH_AUX 7 143 # define CM_SRC_PLLC_CORE1 8 144 # define CM_SRC_PLLC_CORE2 9 145 146 #define CM_OSCCOUNT 0x100 147 148 #define CM_PLLA 0x104 149 # define CM_PLL_ANARST BIT(8) 150 # define CM_PLLA_HOLDPER BIT(7) 151 # define CM_PLLA_LOADPER BIT(6) 152 # define CM_PLLA_HOLDCORE BIT(5) 153 # define CM_PLLA_LOADCORE BIT(4) 154 # define CM_PLLA_HOLDCCP2 BIT(3) 155 # define CM_PLLA_LOADCCP2 BIT(2) 156 # define CM_PLLA_HOLDDSI0 BIT(1) 157 # define CM_PLLA_LOADDSI0 BIT(0) 158 159 #define CM_PLLC 0x108 160 # define CM_PLLC_HOLDPER BIT(7) 161 # define CM_PLLC_LOADPER BIT(6) 162 # define CM_PLLC_HOLDCORE2 BIT(5) 163 # define CM_PLLC_LOADCORE2 BIT(4) 164 # define CM_PLLC_HOLDCORE1 BIT(3) 165 # define CM_PLLC_LOADCORE1 BIT(2) 166 # define CM_PLLC_HOLDCORE0 BIT(1) 167 # define CM_PLLC_LOADCORE0 BIT(0) 168 169 #define CM_PLLD 0x10c 170 # define CM_PLLD_HOLDPER BIT(7) 171 # define CM_PLLD_LOADPER BIT(6) 172 # define CM_PLLD_HOLDCORE BIT(5) 173 # define CM_PLLD_LOADCORE BIT(4) 174 # define CM_PLLD_HOLDDSI1 BIT(3) 175 # define CM_PLLD_LOADDSI1 BIT(2) 176 # define CM_PLLD_HOLDDSI0 BIT(1) 177 # define CM_PLLD_LOADDSI0 BIT(0) 178 179 #define CM_PLLH 0x110 180 # define CM_PLLH_LOADRCAL BIT(2) 181 # define CM_PLLH_LOADAUX BIT(1) 182 # define CM_PLLH_LOADPIX BIT(0) 183 184 #define CM_LOCK 0x114 185 # define CM_LOCK_FLOCKH BIT(12) 186 # define CM_LOCK_FLOCKD BIT(11) 187 # define CM_LOCK_FLOCKC BIT(10) 188 # define CM_LOCK_FLOCKB BIT(9) 189 # define CM_LOCK_FLOCKA BIT(8) 190 191 #define CM_EVENT 0x118 192 #define CM_DSI1ECTL 0x158 193 #define CM_DSI1EDIV 0x15c 194 #define CM_DSI1PCTL 0x160 195 #define CM_DSI1PDIV 0x164 196 #define CM_DFTCTL 0x168 197 #define CM_DFTDIV 0x16c 198 199 #define CM_PLLB 0x170 200 # define CM_PLLB_HOLDARM BIT(1) 201 # define CM_PLLB_LOADARM BIT(0) 202 203 #define A2W_PLLA_CTRL 0x1100 204 #define A2W_PLLC_CTRL 0x1120 205 #define A2W_PLLD_CTRL 0x1140 206 #define A2W_PLLH_CTRL 0x1160 207 #define A2W_PLLB_CTRL 0x11e0 208 # define A2W_PLL_CTRL_PRST_DISABLE BIT(17) 209 # define A2W_PLL_CTRL_PWRDN BIT(16) 210 # define A2W_PLL_CTRL_PDIV_MASK 0x000007000 211 # define A2W_PLL_CTRL_PDIV_SHIFT 12 212 # define A2W_PLL_CTRL_NDIV_MASK 0x0000003ff 213 # define A2W_PLL_CTRL_NDIV_SHIFT 0 214 215 #define A2W_PLLA_ANA0 0x1010 216 #define A2W_PLLC_ANA0 0x1030 217 #define A2W_PLLD_ANA0 0x1050 218 #define A2W_PLLH_ANA0 0x1070 219 #define A2W_PLLB_ANA0 0x10f0 220 221 #define A2W_PLL_KA_SHIFT 7 222 #define A2W_PLL_KA_MASK GENMASK(9, 7) 223 #define A2W_PLL_KI_SHIFT 19 224 #define A2W_PLL_KI_MASK GENMASK(21, 19) 225 #define A2W_PLL_KP_SHIFT 15 226 #define A2W_PLL_KP_MASK GENMASK(18, 15) 227 228 #define A2W_PLLH_KA_SHIFT 19 229 #define A2W_PLLH_KA_MASK GENMASK(21, 19) 230 #define A2W_PLLH_KI_LOW_SHIFT 22 231 #define A2W_PLLH_KI_LOW_MASK GENMASK(23, 22) 232 #define A2W_PLLH_KI_HIGH_SHIFT 0 233 #define A2W_PLLH_KI_HIGH_MASK GENMASK(0, 0) 234 #define A2W_PLLH_KP_SHIFT 1 235 #define A2W_PLLH_KP_MASK GENMASK(4, 1) 236 237 #define A2W_XOSC_CTRL 0x1190 238 # define A2W_XOSC_CTRL_PLLB_ENABLE BIT(7) 239 # define A2W_XOSC_CTRL_PLLA_ENABLE BIT(6) 240 # define A2W_XOSC_CTRL_PLLD_ENABLE BIT(5) 241 # define A2W_XOSC_CTRL_DDR_ENABLE BIT(4) 242 # define A2W_XOSC_CTRL_CPR1_ENABLE BIT(3) 243 # define A2W_XOSC_CTRL_USB_ENABLE BIT(2) 244 # define A2W_XOSC_CTRL_HDMI_ENABLE BIT(1) 245 # define A2W_XOSC_CTRL_PLLC_ENABLE BIT(0) 246 247 #define A2W_PLLA_FRAC 0x1200 248 #define A2W_PLLC_FRAC 0x1220 249 #define A2W_PLLD_FRAC 0x1240 250 #define A2W_PLLH_FRAC 0x1260 251 #define A2W_PLLB_FRAC 0x12e0 252 # define A2W_PLL_FRAC_MASK ((1 << A2W_PLL_FRAC_BITS) - 1) 253 # define A2W_PLL_FRAC_BITS 20 254 255 #define A2W_PLL_CHANNEL_DISABLE BIT(8) 256 #define A2W_PLL_DIV_BITS 8 257 #define A2W_PLL_DIV_SHIFT 0 258 259 #define A2W_PLLA_DSI0 0x1300 260 #define A2W_PLLA_CORE 0x1400 261 #define A2W_PLLA_PER 0x1500 262 #define A2W_PLLA_CCP2 0x1600 263 264 #define A2W_PLLC_CORE2 0x1320 265 #define A2W_PLLC_CORE1 0x1420 266 #define A2W_PLLC_PER 0x1520 267 #define A2W_PLLC_CORE0 0x1620 268 269 #define A2W_PLLD_DSI0 0x1340 270 #define A2W_PLLD_CORE 0x1440 271 #define A2W_PLLD_PER 0x1540 272 #define A2W_PLLD_DSI1 0x1640 273 274 #define A2W_PLLH_AUX 0x1360 275 #define A2W_PLLH_RCAL 0x1460 276 #define A2W_PLLH_PIX 0x1560 277 #define A2W_PLLH_STS 0x1660 278 279 #define A2W_PLLH_CTRLR 0x1960 280 #define A2W_PLLH_FRACR 0x1a60 281 #define A2W_PLLH_AUXR 0x1b60 282 #define A2W_PLLH_RCALR 0x1c60 283 #define A2W_PLLH_PIXR 0x1d60 284 #define A2W_PLLH_STSR 0x1e60 285 286 #define A2W_PLLB_ARM 0x13e0 287 #define A2W_PLLB_SP0 0x14e0 288 #define A2W_PLLB_SP1 0x15e0 289 #define A2W_PLLB_SP2 0x16e0 290 291 #define LOCK_TIMEOUT_NS 100000000 292 #define BCM2835_MAX_FB_RATE 1750000000u 293 294 #define SOC_BCM2835 BIT(0) 295 #define SOC_BCM2711 BIT(1) 296 #define SOC_ALL (SOC_BCM2835 | SOC_BCM2711) 297 298 /* 299 * Names of clocks used within the driver that need to be replaced 300 * with an external parent's name. This array is in the order that 301 * the clocks node in the DT references external clocks. 302 */ 303 static const char *const cprman_parent_names[] = { 304 "xosc", 305 "dsi0_byte", 306 "dsi0_ddr2", 307 "dsi0_ddr", 308 "dsi1_byte", 309 "dsi1_ddr2", 310 "dsi1_ddr", 311 }; 312 313 struct bcm2835_cprman { 314 struct device *dev; 315 void __iomem *regs; 316 spinlock_t regs_lock; /* spinlock for all clocks */ 317 318 /* 319 * Real names of cprman clock parents looked up through 320 * of_clk_get_parent_name(), which will be used in the 321 * parent_names[] arrays for clock registration. 322 */ 323 const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)]; 324 325 /* Must be last */ 326 struct clk_hw_onecell_data onecell; 327 }; 328 329 struct cprman_plat_data { 330 unsigned int soc; 331 }; 332 333 static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val) 334 { 335 writel(CM_PASSWORD | val, cprman->regs + reg); 336 } 337 338 static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg) 339 { 340 return readl(cprman->regs + reg); 341 } 342 343 /* Does a cycle of measuring a clock through the TCNT clock, which may 344 * source from many other clocks in the system. 345 */ 346 static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman, 347 u32 tcnt_mux) 348 { 349 u32 osccount = 19200; /* 1ms */ 350 u32 count; 351 ktime_t timeout; 352 353 spin_lock(&cprman->regs_lock); 354 355 cprman_write(cprman, CM_TCNTCTL, CM_KILL); 356 357 cprman_write(cprman, CM_TCNTCTL, 358 (tcnt_mux & CM_SRC_MASK) | 359 (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT); 360 361 cprman_write(cprman, CM_OSCCOUNT, osccount); 362 363 /* do a kind delay at the start */ 364 mdelay(1); 365 366 /* Finish off whatever is left of OSCCOUNT */ 367 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); 368 while (cprman_read(cprman, CM_OSCCOUNT)) { 369 if (ktime_after(ktime_get(), timeout)) { 370 dev_err(cprman->dev, "timeout waiting for OSCCOUNT\n"); 371 count = 0; 372 goto out; 373 } 374 cpu_relax(); 375 } 376 377 /* Wait for BUSY to clear. */ 378 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); 379 while (cprman_read(cprman, CM_TCNTCTL) & CM_BUSY) { 380 if (ktime_after(ktime_get(), timeout)) { 381 dev_err(cprman->dev, "timeout waiting for !BUSY\n"); 382 count = 0; 383 goto out; 384 } 385 cpu_relax(); 386 } 387 388 count = cprman_read(cprman, CM_TCNTCNT); 389 390 cprman_write(cprman, CM_TCNTCTL, 0); 391 392 out: 393 spin_unlock(&cprman->regs_lock); 394 395 return count * 1000; 396 } 397 398 static void bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base, 399 const struct debugfs_reg32 *regs, 400 size_t nregs, struct dentry *dentry) 401 { 402 struct debugfs_regset32 *regset; 403 404 regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL); 405 if (!regset) 406 return; 407 408 regset->regs = regs; 409 regset->nregs = nregs; 410 regset->base = cprman->regs + base; 411 412 debugfs_create_regset32("regdump", S_IRUGO, dentry, regset); 413 } 414 415 struct bcm2835_pll_data { 416 const char *name; 417 u32 cm_ctrl_reg; 418 u32 a2w_ctrl_reg; 419 u32 frac_reg; 420 u32 ana_reg_base; 421 u32 reference_enable_mask; 422 /* Bit in CM_LOCK to indicate when the PLL has locked. */ 423 u32 lock_mask; 424 u32 flags; 425 426 const struct bcm2835_pll_ana_bits *ana; 427 428 unsigned long min_rate; 429 unsigned long max_rate; 430 /* 431 * Highest rate for the VCO before we have to use the 432 * pre-divide-by-2. 433 */ 434 unsigned long max_fb_rate; 435 }; 436 437 struct bcm2835_pll_ana_bits { 438 u32 mask0; 439 u32 set0; 440 u32 mask1; 441 u32 set1; 442 u32 mask3; 443 u32 set3; 444 u32 fb_prediv_mask; 445 }; 446 447 static const struct bcm2835_pll_ana_bits bcm2835_ana_default = { 448 .mask0 = 0, 449 .set0 = 0, 450 .mask1 = A2W_PLL_KI_MASK | A2W_PLL_KP_MASK, 451 .set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT), 452 .mask3 = A2W_PLL_KA_MASK, 453 .set3 = (2 << A2W_PLL_KA_SHIFT), 454 .fb_prediv_mask = BIT(14), 455 }; 456 457 static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = { 458 .mask0 = A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK, 459 .set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT), 460 .mask1 = A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK, 461 .set1 = (6 << A2W_PLLH_KP_SHIFT), 462 .mask3 = 0, 463 .set3 = 0, 464 .fb_prediv_mask = BIT(11), 465 }; 466 467 struct bcm2835_pll_divider_data { 468 const char *name; 469 const char *source_pll; 470 471 u32 cm_reg; 472 u32 a2w_reg; 473 474 u32 load_mask; 475 u32 hold_mask; 476 u32 fixed_divider; 477 u32 flags; 478 }; 479 480 struct bcm2835_clock_data { 481 const char *name; 482 483 const char *const *parents; 484 int num_mux_parents; 485 486 /* Bitmap encoding which parents accept rate change propagation. */ 487 unsigned int set_rate_parent; 488 489 u32 ctl_reg; 490 u32 div_reg; 491 492 /* Number of integer bits in the divider */ 493 u32 int_bits; 494 /* Number of fractional bits in the divider */ 495 u32 frac_bits; 496 497 u32 flags; 498 499 bool is_vpu_clock; 500 bool is_mash_clock; 501 bool low_jitter; 502 503 u32 tcnt_mux; 504 }; 505 506 struct bcm2835_gate_data { 507 const char *name; 508 const char *parent; 509 510 u32 ctl_reg; 511 }; 512 513 struct bcm2835_pll { 514 struct clk_hw hw; 515 struct bcm2835_cprman *cprman; 516 const struct bcm2835_pll_data *data; 517 }; 518 519 static int bcm2835_pll_is_on(struct clk_hw *hw) 520 { 521 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 522 struct bcm2835_cprman *cprman = pll->cprman; 523 const struct bcm2835_pll_data *data = pll->data; 524 525 return cprman_read(cprman, data->a2w_ctrl_reg) & 526 A2W_PLL_CTRL_PRST_DISABLE; 527 } 528 529 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate, 530 unsigned long parent_rate, 531 u32 *ndiv, u32 *fdiv) 532 { 533 u64 div; 534 535 div = (u64)rate << A2W_PLL_FRAC_BITS; 536 do_div(div, parent_rate); 537 538 *ndiv = div >> A2W_PLL_FRAC_BITS; 539 *fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1); 540 } 541 542 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate, 543 u32 ndiv, u32 fdiv, u32 pdiv) 544 { 545 u64 rate; 546 547 if (pdiv == 0) 548 return 0; 549 550 rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv); 551 do_div(rate, pdiv); 552 return rate >> A2W_PLL_FRAC_BITS; 553 } 554 555 static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate, 556 unsigned long *parent_rate) 557 { 558 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 559 const struct bcm2835_pll_data *data = pll->data; 560 u32 ndiv, fdiv; 561 562 rate = clamp(rate, data->min_rate, data->max_rate); 563 564 bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv); 565 566 return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1); 567 } 568 569 static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw, 570 unsigned long parent_rate) 571 { 572 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 573 struct bcm2835_cprman *cprman = pll->cprman; 574 const struct bcm2835_pll_data *data = pll->data; 575 u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg); 576 u32 ndiv, pdiv, fdiv; 577 bool using_prediv; 578 579 if (parent_rate == 0) 580 return 0; 581 582 fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK; 583 ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT; 584 pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT; 585 using_prediv = cprman_read(cprman, data->ana_reg_base + 4) & 586 data->ana->fb_prediv_mask; 587 588 if (using_prediv) { 589 ndiv *= 2; 590 fdiv *= 2; 591 } 592 593 return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv); 594 } 595 596 static void bcm2835_pll_off(struct clk_hw *hw) 597 { 598 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 599 struct bcm2835_cprman *cprman = pll->cprman; 600 const struct bcm2835_pll_data *data = pll->data; 601 602 spin_lock(&cprman->regs_lock); 603 cprman_write(cprman, data->cm_ctrl_reg, CM_PLL_ANARST); 604 cprman_write(cprman, data->a2w_ctrl_reg, 605 cprman_read(cprman, data->a2w_ctrl_reg) | 606 A2W_PLL_CTRL_PWRDN); 607 spin_unlock(&cprman->regs_lock); 608 } 609 610 static int bcm2835_pll_on(struct clk_hw *hw) 611 { 612 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 613 struct bcm2835_cprman *cprman = pll->cprman; 614 const struct bcm2835_pll_data *data = pll->data; 615 ktime_t timeout; 616 617 cprman_write(cprman, data->a2w_ctrl_reg, 618 cprman_read(cprman, data->a2w_ctrl_reg) & 619 ~A2W_PLL_CTRL_PWRDN); 620 621 /* Take the PLL out of reset. */ 622 spin_lock(&cprman->regs_lock); 623 cprman_write(cprman, data->cm_ctrl_reg, 624 cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST); 625 spin_unlock(&cprman->regs_lock); 626 627 /* Wait for the PLL to lock. */ 628 timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); 629 while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) { 630 if (ktime_after(ktime_get(), timeout)) { 631 dev_err(cprman->dev, "%s: couldn't lock PLL\n", 632 clk_hw_get_name(hw)); 633 return -ETIMEDOUT; 634 } 635 636 cpu_relax(); 637 } 638 639 cprman_write(cprman, data->a2w_ctrl_reg, 640 cprman_read(cprman, data->a2w_ctrl_reg) | 641 A2W_PLL_CTRL_PRST_DISABLE); 642 643 return 0; 644 } 645 646 static void 647 bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana) 648 { 649 int i; 650 651 /* 652 * ANA register setup is done as a series of writes to 653 * ANA3-ANA0, in that order. This lets us write all 4 654 * registers as a single cycle of the serdes interface (taking 655 * 100 xosc clocks), whereas if we were to update ana0, 1, and 656 * 3 individually through their partial-write registers, each 657 * would be their own serdes cycle. 658 */ 659 for (i = 3; i >= 0; i--) 660 cprman_write(cprman, ana_reg_base + i * 4, ana[i]); 661 } 662 663 static int bcm2835_pll_set_rate(struct clk_hw *hw, 664 unsigned long rate, unsigned long parent_rate) 665 { 666 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 667 struct bcm2835_cprman *cprman = pll->cprman; 668 const struct bcm2835_pll_data *data = pll->data; 669 bool was_using_prediv, use_fb_prediv, do_ana_setup_first; 670 u32 ndiv, fdiv, a2w_ctl; 671 u32 ana[4]; 672 int i; 673 674 if (rate > data->max_fb_rate) { 675 use_fb_prediv = true; 676 rate /= 2; 677 } else { 678 use_fb_prediv = false; 679 } 680 681 bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv); 682 683 for (i = 3; i >= 0; i--) 684 ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4); 685 686 was_using_prediv = ana[1] & data->ana->fb_prediv_mask; 687 688 ana[0] &= ~data->ana->mask0; 689 ana[0] |= data->ana->set0; 690 ana[1] &= ~data->ana->mask1; 691 ana[1] |= data->ana->set1; 692 ana[3] &= ~data->ana->mask3; 693 ana[3] |= data->ana->set3; 694 695 if (was_using_prediv && !use_fb_prediv) { 696 ana[1] &= ~data->ana->fb_prediv_mask; 697 do_ana_setup_first = true; 698 } else if (!was_using_prediv && use_fb_prediv) { 699 ana[1] |= data->ana->fb_prediv_mask; 700 do_ana_setup_first = false; 701 } else { 702 do_ana_setup_first = true; 703 } 704 705 /* Unmask the reference clock from the oscillator. */ 706 spin_lock(&cprman->regs_lock); 707 cprman_write(cprman, A2W_XOSC_CTRL, 708 cprman_read(cprman, A2W_XOSC_CTRL) | 709 data->reference_enable_mask); 710 spin_unlock(&cprman->regs_lock); 711 712 if (do_ana_setup_first) 713 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana); 714 715 /* Set the PLL multiplier from the oscillator. */ 716 cprman_write(cprman, data->frac_reg, fdiv); 717 718 a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg); 719 a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK; 720 a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT; 721 a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK; 722 a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT; 723 cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl); 724 725 if (!do_ana_setup_first) 726 bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana); 727 728 return 0; 729 } 730 731 static void bcm2835_pll_debug_init(struct clk_hw *hw, 732 struct dentry *dentry) 733 { 734 struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw); 735 struct bcm2835_cprman *cprman = pll->cprman; 736 const struct bcm2835_pll_data *data = pll->data; 737 struct debugfs_reg32 *regs; 738 739 regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL); 740 if (!regs) 741 return; 742 743 regs[0].name = "cm_ctrl"; 744 regs[0].offset = data->cm_ctrl_reg; 745 regs[1].name = "a2w_ctrl"; 746 regs[1].offset = data->a2w_ctrl_reg; 747 regs[2].name = "frac"; 748 regs[2].offset = data->frac_reg; 749 regs[3].name = "ana0"; 750 regs[3].offset = data->ana_reg_base + 0 * 4; 751 regs[4].name = "ana1"; 752 regs[4].offset = data->ana_reg_base + 1 * 4; 753 regs[5].name = "ana2"; 754 regs[5].offset = data->ana_reg_base + 2 * 4; 755 regs[6].name = "ana3"; 756 regs[6].offset = data->ana_reg_base + 3 * 4; 757 758 bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry); 759 } 760 761 static const struct clk_ops bcm2835_pll_clk_ops = { 762 .is_prepared = bcm2835_pll_is_on, 763 .prepare = bcm2835_pll_on, 764 .unprepare = bcm2835_pll_off, 765 .recalc_rate = bcm2835_pll_get_rate, 766 .set_rate = bcm2835_pll_set_rate, 767 .round_rate = bcm2835_pll_round_rate, 768 .debug_init = bcm2835_pll_debug_init, 769 }; 770 771 struct bcm2835_pll_divider { 772 struct clk_divider div; 773 struct bcm2835_cprman *cprman; 774 const struct bcm2835_pll_divider_data *data; 775 }; 776 777 static struct bcm2835_pll_divider * 778 bcm2835_pll_divider_from_hw(struct clk_hw *hw) 779 { 780 return container_of(hw, struct bcm2835_pll_divider, div.hw); 781 } 782 783 static int bcm2835_pll_divider_is_on(struct clk_hw *hw) 784 { 785 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); 786 struct bcm2835_cprman *cprman = divider->cprman; 787 const struct bcm2835_pll_divider_data *data = divider->data; 788 789 return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE); 790 } 791 792 static long bcm2835_pll_divider_round_rate(struct clk_hw *hw, 793 unsigned long rate, 794 unsigned long *parent_rate) 795 { 796 return clk_divider_ops.round_rate(hw, rate, parent_rate); 797 } 798 799 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw, 800 unsigned long parent_rate) 801 { 802 return clk_divider_ops.recalc_rate(hw, parent_rate); 803 } 804 805 static void bcm2835_pll_divider_off(struct clk_hw *hw) 806 { 807 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); 808 struct bcm2835_cprman *cprman = divider->cprman; 809 const struct bcm2835_pll_divider_data *data = divider->data; 810 811 spin_lock(&cprman->regs_lock); 812 cprman_write(cprman, data->cm_reg, 813 (cprman_read(cprman, data->cm_reg) & 814 ~data->load_mask) | data->hold_mask); 815 cprman_write(cprman, data->a2w_reg, 816 cprman_read(cprman, data->a2w_reg) | 817 A2W_PLL_CHANNEL_DISABLE); 818 spin_unlock(&cprman->regs_lock); 819 } 820 821 static int bcm2835_pll_divider_on(struct clk_hw *hw) 822 { 823 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); 824 struct bcm2835_cprman *cprman = divider->cprman; 825 const struct bcm2835_pll_divider_data *data = divider->data; 826 827 spin_lock(&cprman->regs_lock); 828 cprman_write(cprman, data->a2w_reg, 829 cprman_read(cprman, data->a2w_reg) & 830 ~A2W_PLL_CHANNEL_DISABLE); 831 832 cprman_write(cprman, data->cm_reg, 833 cprman_read(cprman, data->cm_reg) & ~data->hold_mask); 834 spin_unlock(&cprman->regs_lock); 835 836 return 0; 837 } 838 839 static int bcm2835_pll_divider_set_rate(struct clk_hw *hw, 840 unsigned long rate, 841 unsigned long parent_rate) 842 { 843 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); 844 struct bcm2835_cprman *cprman = divider->cprman; 845 const struct bcm2835_pll_divider_data *data = divider->data; 846 u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS; 847 848 div = DIV_ROUND_UP_ULL(parent_rate, rate); 849 850 div = min(div, max_div); 851 if (div == max_div) 852 div = 0; 853 854 cprman_write(cprman, data->a2w_reg, div); 855 cm = cprman_read(cprman, data->cm_reg); 856 cprman_write(cprman, data->cm_reg, cm | data->load_mask); 857 cprman_write(cprman, data->cm_reg, cm & ~data->load_mask); 858 859 return 0; 860 } 861 862 static void bcm2835_pll_divider_debug_init(struct clk_hw *hw, 863 struct dentry *dentry) 864 { 865 struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw); 866 struct bcm2835_cprman *cprman = divider->cprman; 867 const struct bcm2835_pll_divider_data *data = divider->data; 868 struct debugfs_reg32 *regs; 869 870 regs = devm_kcalloc(cprman->dev, 7, sizeof(*regs), GFP_KERNEL); 871 if (!regs) 872 return; 873 874 regs[0].name = "cm"; 875 regs[0].offset = data->cm_reg; 876 regs[1].name = "a2w"; 877 regs[1].offset = data->a2w_reg; 878 879 bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry); 880 } 881 882 static const struct clk_ops bcm2835_pll_divider_clk_ops = { 883 .is_prepared = bcm2835_pll_divider_is_on, 884 .prepare = bcm2835_pll_divider_on, 885 .unprepare = bcm2835_pll_divider_off, 886 .recalc_rate = bcm2835_pll_divider_get_rate, 887 .set_rate = bcm2835_pll_divider_set_rate, 888 .round_rate = bcm2835_pll_divider_round_rate, 889 .debug_init = bcm2835_pll_divider_debug_init, 890 }; 891 892 /* 893 * The CM dividers do fixed-point division, so we can't use the 894 * generic integer divider code like the PLL dividers do (and we can't 895 * fake it by having some fixed shifts preceding it in the clock tree, 896 * because we'd run out of bits in a 32-bit unsigned long). 897 */ 898 struct bcm2835_clock { 899 struct clk_hw hw; 900 struct bcm2835_cprman *cprman; 901 const struct bcm2835_clock_data *data; 902 }; 903 904 static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw) 905 { 906 return container_of(hw, struct bcm2835_clock, hw); 907 } 908 909 static int bcm2835_clock_is_on(struct clk_hw *hw) 910 { 911 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 912 struct bcm2835_cprman *cprman = clock->cprman; 913 const struct bcm2835_clock_data *data = clock->data; 914 915 return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0; 916 } 917 918 static u32 bcm2835_clock_choose_div(struct clk_hw *hw, 919 unsigned long rate, 920 unsigned long parent_rate, 921 bool round_up) 922 { 923 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 924 const struct bcm2835_clock_data *data = clock->data; 925 u32 unused_frac_mask = 926 GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1; 927 u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS; 928 u64 rem; 929 u32 div, mindiv, maxdiv; 930 931 rem = do_div(temp, rate); 932 div = temp; 933 934 /* Round up and mask off the unused bits */ 935 if (round_up && ((div & unused_frac_mask) != 0 || rem != 0)) 936 div += unused_frac_mask + 1; 937 div &= ~unused_frac_mask; 938 939 /* different clamping limits apply for a mash clock */ 940 if (data->is_mash_clock) { 941 /* clamp to min divider of 2 */ 942 mindiv = 2 << CM_DIV_FRAC_BITS; 943 /* clamp to the highest possible integer divider */ 944 maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS; 945 } else { 946 /* clamp to min divider of 1 */ 947 mindiv = 1 << CM_DIV_FRAC_BITS; 948 /* clamp to the highest possible fractional divider */ 949 maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1, 950 CM_DIV_FRAC_BITS - data->frac_bits); 951 } 952 953 /* apply the clamping limits */ 954 div = max_t(u32, div, mindiv); 955 div = min_t(u32, div, maxdiv); 956 957 return div; 958 } 959 960 static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock, 961 unsigned long parent_rate, 962 u32 div) 963 { 964 const struct bcm2835_clock_data *data = clock->data; 965 u64 temp; 966 967 if (data->int_bits == 0 && data->frac_bits == 0) 968 return parent_rate; 969 970 /* 971 * The divisor is a 12.12 fixed point field, but only some of 972 * the bits are populated in any given clock. 973 */ 974 div >>= CM_DIV_FRAC_BITS - data->frac_bits; 975 div &= (1 << (data->int_bits + data->frac_bits)) - 1; 976 977 if (div == 0) 978 return 0; 979 980 temp = (u64)parent_rate << data->frac_bits; 981 982 do_div(temp, div); 983 984 return temp; 985 } 986 987 static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw, 988 unsigned long parent_rate) 989 { 990 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 991 struct bcm2835_cprman *cprman = clock->cprman; 992 const struct bcm2835_clock_data *data = clock->data; 993 u32 div; 994 995 if (data->int_bits == 0 && data->frac_bits == 0) 996 return parent_rate; 997 998 div = cprman_read(cprman, data->div_reg); 999 1000 return bcm2835_clock_rate_from_divisor(clock, parent_rate, div); 1001 } 1002 1003 static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock) 1004 { 1005 struct bcm2835_cprman *cprman = clock->cprman; 1006 const struct bcm2835_clock_data *data = clock->data; 1007 ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS); 1008 1009 while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) { 1010 if (ktime_after(ktime_get(), timeout)) { 1011 dev_err(cprman->dev, "%s: couldn't lock PLL\n", 1012 clk_hw_get_name(&clock->hw)); 1013 return; 1014 } 1015 cpu_relax(); 1016 } 1017 } 1018 1019 static void bcm2835_clock_off(struct clk_hw *hw) 1020 { 1021 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1022 struct bcm2835_cprman *cprman = clock->cprman; 1023 const struct bcm2835_clock_data *data = clock->data; 1024 1025 spin_lock(&cprman->regs_lock); 1026 cprman_write(cprman, data->ctl_reg, 1027 cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE); 1028 spin_unlock(&cprman->regs_lock); 1029 1030 /* BUSY will remain high until the divider completes its cycle. */ 1031 bcm2835_clock_wait_busy(clock); 1032 } 1033 1034 static int bcm2835_clock_on(struct clk_hw *hw) 1035 { 1036 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1037 struct bcm2835_cprman *cprman = clock->cprman; 1038 const struct bcm2835_clock_data *data = clock->data; 1039 1040 spin_lock(&cprman->regs_lock); 1041 cprman_write(cprman, data->ctl_reg, 1042 cprman_read(cprman, data->ctl_reg) | 1043 CM_ENABLE | 1044 CM_GATE); 1045 spin_unlock(&cprman->regs_lock); 1046 1047 /* Debug code to measure the clock once it's turned on to see 1048 * if it's ticking at the rate we expect. 1049 */ 1050 if (data->tcnt_mux && false) { 1051 dev_info(cprman->dev, 1052 "clk %s: rate %ld, measure %ld\n", 1053 data->name, 1054 clk_hw_get_rate(hw), 1055 bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux)); 1056 } 1057 1058 return 0; 1059 } 1060 1061 static int bcm2835_clock_set_rate(struct clk_hw *hw, 1062 unsigned long rate, unsigned long parent_rate) 1063 { 1064 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1065 struct bcm2835_cprman *cprman = clock->cprman; 1066 const struct bcm2835_clock_data *data = clock->data; 1067 u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false); 1068 u32 ctl; 1069 1070 spin_lock(&cprman->regs_lock); 1071 1072 /* 1073 * Setting up frac support 1074 * 1075 * In principle it is recommended to stop/start the clock first, 1076 * but as we set CLK_SET_RATE_GATE during registration of the 1077 * clock this requirement should be take care of by the 1078 * clk-framework. 1079 */ 1080 ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC; 1081 ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0; 1082 cprman_write(cprman, data->ctl_reg, ctl); 1083 1084 cprman_write(cprman, data->div_reg, div); 1085 1086 spin_unlock(&cprman->regs_lock); 1087 1088 return 0; 1089 } 1090 1091 static bool 1092 bcm2835_clk_is_pllc(struct clk_hw *hw) 1093 { 1094 if (!hw) 1095 return false; 1096 1097 return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0; 1098 } 1099 1100 static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw, 1101 int parent_idx, 1102 unsigned long rate, 1103 u32 *div, 1104 unsigned long *prate, 1105 unsigned long *avgrate) 1106 { 1107 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1108 struct bcm2835_cprman *cprman = clock->cprman; 1109 const struct bcm2835_clock_data *data = clock->data; 1110 unsigned long best_rate = 0; 1111 u32 curdiv, mindiv, maxdiv; 1112 struct clk_hw *parent; 1113 1114 parent = clk_hw_get_parent_by_index(hw, parent_idx); 1115 1116 if (!(BIT(parent_idx) & data->set_rate_parent)) { 1117 *prate = clk_hw_get_rate(parent); 1118 *div = bcm2835_clock_choose_div(hw, rate, *prate, true); 1119 1120 *avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div); 1121 1122 if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) { 1123 unsigned long high, low; 1124 u32 int_div = *div & ~CM_DIV_FRAC_MASK; 1125 1126 high = bcm2835_clock_rate_from_divisor(clock, *prate, 1127 int_div); 1128 int_div += CM_DIV_FRAC_MASK + 1; 1129 low = bcm2835_clock_rate_from_divisor(clock, *prate, 1130 int_div); 1131 1132 /* 1133 * Return a value which is the maximum deviation 1134 * below the ideal rate, for use as a metric. 1135 */ 1136 return *avgrate - max(*avgrate - low, high - *avgrate); 1137 } 1138 return *avgrate; 1139 } 1140 1141 if (data->frac_bits) 1142 dev_warn(cprman->dev, 1143 "frac bits are not used when propagating rate change"); 1144 1145 /* clamp to min divider of 2 if we're dealing with a mash clock */ 1146 mindiv = data->is_mash_clock ? 2 : 1; 1147 maxdiv = BIT(data->int_bits) - 1; 1148 1149 /* TODO: Be smart, and only test a subset of the available divisors. */ 1150 for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) { 1151 unsigned long tmp_rate; 1152 1153 tmp_rate = clk_hw_round_rate(parent, rate * curdiv); 1154 tmp_rate /= curdiv; 1155 if (curdiv == mindiv || 1156 (tmp_rate > best_rate && tmp_rate <= rate)) 1157 best_rate = tmp_rate; 1158 1159 if (best_rate == rate) 1160 break; 1161 } 1162 1163 *div = curdiv << CM_DIV_FRAC_BITS; 1164 *prate = curdiv * best_rate; 1165 *avgrate = best_rate; 1166 1167 return best_rate; 1168 } 1169 1170 static int bcm2835_clock_determine_rate(struct clk_hw *hw, 1171 struct clk_rate_request *req) 1172 { 1173 struct clk_hw *parent, *best_parent = NULL; 1174 bool current_parent_is_pllc; 1175 unsigned long rate, best_rate = 0; 1176 unsigned long prate, best_prate = 0; 1177 unsigned long avgrate, best_avgrate = 0; 1178 size_t i; 1179 u32 div; 1180 1181 current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw)); 1182 1183 /* 1184 * Select parent clock that results in the closest but lower rate 1185 */ 1186 for (i = 0; i < clk_hw_get_num_parents(hw); ++i) { 1187 parent = clk_hw_get_parent_by_index(hw, i); 1188 if (!parent) 1189 continue; 1190 1191 /* 1192 * Don't choose a PLLC-derived clock as our parent 1193 * unless it had been manually set that way. PLLC's 1194 * frequency gets adjusted by the firmware due to 1195 * over-temp or under-voltage conditions, without 1196 * prior notification to our clock consumer. 1197 */ 1198 if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc) 1199 continue; 1200 1201 rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate, 1202 &div, &prate, 1203 &avgrate); 1204 if (rate > best_rate && rate <= req->rate) { 1205 best_parent = parent; 1206 best_prate = prate; 1207 best_rate = rate; 1208 best_avgrate = avgrate; 1209 } 1210 } 1211 1212 if (!best_parent) 1213 return -EINVAL; 1214 1215 req->best_parent_hw = best_parent; 1216 req->best_parent_rate = best_prate; 1217 1218 req->rate = best_avgrate; 1219 1220 return 0; 1221 } 1222 1223 static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index) 1224 { 1225 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1226 struct bcm2835_cprman *cprman = clock->cprman; 1227 const struct bcm2835_clock_data *data = clock->data; 1228 u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK; 1229 1230 cprman_write(cprman, data->ctl_reg, src); 1231 return 0; 1232 } 1233 1234 static u8 bcm2835_clock_get_parent(struct clk_hw *hw) 1235 { 1236 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1237 struct bcm2835_cprman *cprman = clock->cprman; 1238 const struct bcm2835_clock_data *data = clock->data; 1239 u32 src = cprman_read(cprman, data->ctl_reg); 1240 1241 return (src & CM_SRC_MASK) >> CM_SRC_SHIFT; 1242 } 1243 1244 static const struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = { 1245 { 1246 .name = "ctl", 1247 .offset = 0, 1248 }, 1249 { 1250 .name = "div", 1251 .offset = 4, 1252 }, 1253 }; 1254 1255 static void bcm2835_clock_debug_init(struct clk_hw *hw, 1256 struct dentry *dentry) 1257 { 1258 struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw); 1259 struct bcm2835_cprman *cprman = clock->cprman; 1260 const struct bcm2835_clock_data *data = clock->data; 1261 1262 bcm2835_debugfs_regset(cprman, data->ctl_reg, 1263 bcm2835_debugfs_clock_reg32, 1264 ARRAY_SIZE(bcm2835_debugfs_clock_reg32), 1265 dentry); 1266 } 1267 1268 static const struct clk_ops bcm2835_clock_clk_ops = { 1269 .is_prepared = bcm2835_clock_is_on, 1270 .prepare = bcm2835_clock_on, 1271 .unprepare = bcm2835_clock_off, 1272 .recalc_rate = bcm2835_clock_get_rate, 1273 .set_rate = bcm2835_clock_set_rate, 1274 .determine_rate = bcm2835_clock_determine_rate, 1275 .set_parent = bcm2835_clock_set_parent, 1276 .get_parent = bcm2835_clock_get_parent, 1277 .debug_init = bcm2835_clock_debug_init, 1278 }; 1279 1280 static int bcm2835_vpu_clock_is_on(struct clk_hw *hw) 1281 { 1282 return true; 1283 } 1284 1285 /* 1286 * The VPU clock can never be disabled (it doesn't have an ENABLE 1287 * bit), so it gets its own set of clock ops. 1288 */ 1289 static const struct clk_ops bcm2835_vpu_clock_clk_ops = { 1290 .is_prepared = bcm2835_vpu_clock_is_on, 1291 .recalc_rate = bcm2835_clock_get_rate, 1292 .set_rate = bcm2835_clock_set_rate, 1293 .determine_rate = bcm2835_clock_determine_rate, 1294 .set_parent = bcm2835_clock_set_parent, 1295 .get_parent = bcm2835_clock_get_parent, 1296 .debug_init = bcm2835_clock_debug_init, 1297 }; 1298 1299 static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman, 1300 const void *data) 1301 { 1302 const struct bcm2835_pll_data *pll_data = data; 1303 struct bcm2835_pll *pll; 1304 struct clk_init_data init; 1305 int ret; 1306 1307 memset(&init, 0, sizeof(init)); 1308 1309 /* All of the PLLs derive from the external oscillator. */ 1310 init.parent_names = &cprman->real_parent_names[0]; 1311 init.num_parents = 1; 1312 init.name = pll_data->name; 1313 init.ops = &bcm2835_pll_clk_ops; 1314 init.flags = pll_data->flags | CLK_IGNORE_UNUSED; 1315 1316 pll = kzalloc(sizeof(*pll), GFP_KERNEL); 1317 if (!pll) 1318 return NULL; 1319 1320 pll->cprman = cprman; 1321 pll->data = pll_data; 1322 pll->hw.init = &init; 1323 1324 ret = devm_clk_hw_register(cprman->dev, &pll->hw); 1325 if (ret) 1326 return NULL; 1327 return &pll->hw; 1328 } 1329 1330 static struct clk_hw * 1331 bcm2835_register_pll_divider(struct bcm2835_cprman *cprman, 1332 const void *data) 1333 { 1334 const struct bcm2835_pll_divider_data *divider_data = data; 1335 struct bcm2835_pll_divider *divider; 1336 struct clk_init_data init; 1337 const char *divider_name; 1338 int ret; 1339 1340 if (divider_data->fixed_divider != 1) { 1341 divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL, 1342 "%s_prediv", divider_data->name); 1343 if (!divider_name) 1344 return NULL; 1345 } else { 1346 divider_name = divider_data->name; 1347 } 1348 1349 memset(&init, 0, sizeof(init)); 1350 1351 init.parent_names = ÷r_data->source_pll; 1352 init.num_parents = 1; 1353 init.name = divider_name; 1354 init.ops = &bcm2835_pll_divider_clk_ops; 1355 init.flags = divider_data->flags | CLK_IGNORE_UNUSED; 1356 1357 divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL); 1358 if (!divider) 1359 return NULL; 1360 1361 divider->div.reg = cprman->regs + divider_data->a2w_reg; 1362 divider->div.shift = A2W_PLL_DIV_SHIFT; 1363 divider->div.width = A2W_PLL_DIV_BITS; 1364 divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO; 1365 divider->div.lock = &cprman->regs_lock; 1366 divider->div.hw.init = &init; 1367 divider->div.table = NULL; 1368 1369 divider->cprman = cprman; 1370 divider->data = divider_data; 1371 1372 ret = devm_clk_hw_register(cprman->dev, ÷r->div.hw); 1373 if (ret) 1374 return ERR_PTR(ret); 1375 1376 /* 1377 * PLLH's channels have a fixed divide by 10 afterwards, which 1378 * is what our consumers are actually using. 1379 */ 1380 if (divider_data->fixed_divider != 1) { 1381 return clk_hw_register_fixed_factor(cprman->dev, 1382 divider_data->name, 1383 divider_name, 1384 CLK_SET_RATE_PARENT, 1385 1, 1386 divider_data->fixed_divider); 1387 } 1388 1389 return ÷r->div.hw; 1390 } 1391 1392 static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman, 1393 const void *data) 1394 { 1395 const struct bcm2835_clock_data *clock_data = data; 1396 struct bcm2835_clock *clock; 1397 struct clk_init_data init; 1398 const char *parents[1 << CM_SRC_BITS]; 1399 size_t i; 1400 int ret; 1401 1402 /* 1403 * Replace our strings referencing parent clocks with the 1404 * actual clock-output-name of the parent. 1405 */ 1406 for (i = 0; i < clock_data->num_mux_parents; i++) { 1407 parents[i] = clock_data->parents[i]; 1408 1409 ret = match_string(cprman_parent_names, 1410 ARRAY_SIZE(cprman_parent_names), 1411 parents[i]); 1412 if (ret >= 0) 1413 parents[i] = cprman->real_parent_names[ret]; 1414 } 1415 1416 memset(&init, 0, sizeof(init)); 1417 init.parent_names = parents; 1418 init.num_parents = clock_data->num_mux_parents; 1419 init.name = clock_data->name; 1420 init.flags = clock_data->flags | CLK_IGNORE_UNUSED; 1421 1422 /* 1423 * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate 1424 * rate changes on at least of the parents. 1425 */ 1426 if (clock_data->set_rate_parent) 1427 init.flags |= CLK_SET_RATE_PARENT; 1428 1429 if (clock_data->is_vpu_clock) { 1430 init.ops = &bcm2835_vpu_clock_clk_ops; 1431 } else { 1432 init.ops = &bcm2835_clock_clk_ops; 1433 init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE; 1434 1435 /* If the clock wasn't actually enabled at boot, it's not 1436 * critical. 1437 */ 1438 if (!(cprman_read(cprman, clock_data->ctl_reg) & CM_ENABLE)) 1439 init.flags &= ~CLK_IS_CRITICAL; 1440 } 1441 1442 clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL); 1443 if (!clock) 1444 return NULL; 1445 1446 clock->cprman = cprman; 1447 clock->data = clock_data; 1448 clock->hw.init = &init; 1449 1450 ret = devm_clk_hw_register(cprman->dev, &clock->hw); 1451 if (ret) 1452 return ERR_PTR(ret); 1453 return &clock->hw; 1454 } 1455 1456 static struct clk_hw *bcm2835_register_gate(struct bcm2835_cprman *cprman, 1457 const void *data) 1458 { 1459 const struct bcm2835_gate_data *gate_data = data; 1460 1461 return clk_hw_register_gate(cprman->dev, gate_data->name, 1462 gate_data->parent, 1463 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE, 1464 cprman->regs + gate_data->ctl_reg, 1465 CM_GATE_BIT, 0, &cprman->regs_lock); 1466 } 1467 1468 struct bcm2835_clk_desc { 1469 struct clk_hw *(*clk_register)(struct bcm2835_cprman *cprman, 1470 const void *data); 1471 unsigned int supported; 1472 const void *data; 1473 }; 1474 1475 /* assignment helper macros for different clock types */ 1476 #define _REGISTER(f, s, ...) { .clk_register = f, \ 1477 .supported = s, \ 1478 .data = __VA_ARGS__ } 1479 #define REGISTER_PLL(s, ...) _REGISTER(&bcm2835_register_pll, \ 1480 s, \ 1481 &(struct bcm2835_pll_data) \ 1482 {__VA_ARGS__}) 1483 #define REGISTER_PLL_DIV(s, ...) _REGISTER(&bcm2835_register_pll_divider, \ 1484 s, \ 1485 &(struct bcm2835_pll_divider_data) \ 1486 {__VA_ARGS__}) 1487 #define REGISTER_CLK(s, ...) _REGISTER(&bcm2835_register_clock, \ 1488 s, \ 1489 &(struct bcm2835_clock_data) \ 1490 {__VA_ARGS__}) 1491 #define REGISTER_GATE(s, ...) _REGISTER(&bcm2835_register_gate, \ 1492 s, \ 1493 &(struct bcm2835_gate_data) \ 1494 {__VA_ARGS__}) 1495 1496 /* parent mux arrays plus helper macros */ 1497 1498 /* main oscillator parent mux */ 1499 static const char *const bcm2835_clock_osc_parents[] = { 1500 "gnd", 1501 "xosc", 1502 "testdebug0", 1503 "testdebug1" 1504 }; 1505 1506 #define REGISTER_OSC_CLK(s, ...) REGISTER_CLK( \ 1507 s, \ 1508 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents), \ 1509 .parents = bcm2835_clock_osc_parents, \ 1510 __VA_ARGS__) 1511 1512 /* main peripherial parent mux */ 1513 static const char *const bcm2835_clock_per_parents[] = { 1514 "gnd", 1515 "xosc", 1516 "testdebug0", 1517 "testdebug1", 1518 "plla_per", 1519 "pllc_per", 1520 "plld_per", 1521 "pllh_aux", 1522 }; 1523 1524 #define REGISTER_PER_CLK(s, ...) REGISTER_CLK( \ 1525 s, \ 1526 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents), \ 1527 .parents = bcm2835_clock_per_parents, \ 1528 __VA_ARGS__) 1529 1530 /* 1531 * Restrict clock sources for the PCM peripheral to the oscillator and 1532 * PLLD_PER because other source may have varying rates or be switched 1533 * off. 1534 * 1535 * Prevent other sources from being selected by replacing their names in 1536 * the list of potential parents with dummy entries (entry index is 1537 * significant). 1538 */ 1539 static const char *const bcm2835_pcm_per_parents[] = { 1540 "-", 1541 "xosc", 1542 "-", 1543 "-", 1544 "-", 1545 "-", 1546 "plld_per", 1547 "-", 1548 }; 1549 1550 #define REGISTER_PCM_CLK(s, ...) REGISTER_CLK( \ 1551 s, \ 1552 .num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents), \ 1553 .parents = bcm2835_pcm_per_parents, \ 1554 __VA_ARGS__) 1555 1556 /* main vpu parent mux */ 1557 static const char *const bcm2835_clock_vpu_parents[] = { 1558 "gnd", 1559 "xosc", 1560 "testdebug0", 1561 "testdebug1", 1562 "plla_core", 1563 "pllc_core0", 1564 "plld_core", 1565 "pllh_aux", 1566 "pllc_core1", 1567 "pllc_core2", 1568 }; 1569 1570 #define REGISTER_VPU_CLK(s, ...) REGISTER_CLK( \ 1571 s, \ 1572 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents), \ 1573 .parents = bcm2835_clock_vpu_parents, \ 1574 __VA_ARGS__) 1575 1576 /* 1577 * DSI parent clocks. The DSI byte/DDR/DDR2 clocks come from the DSI 1578 * analog PHY. The _inv variants are generated internally to cprman, 1579 * but we don't use them so they aren't hooked up. 1580 */ 1581 static const char *const bcm2835_clock_dsi0_parents[] = { 1582 "gnd", 1583 "xosc", 1584 "testdebug0", 1585 "testdebug1", 1586 "dsi0_ddr", 1587 "dsi0_ddr_inv", 1588 "dsi0_ddr2", 1589 "dsi0_ddr2_inv", 1590 "dsi0_byte", 1591 "dsi0_byte_inv", 1592 }; 1593 1594 static const char *const bcm2835_clock_dsi1_parents[] = { 1595 "gnd", 1596 "xosc", 1597 "testdebug0", 1598 "testdebug1", 1599 "dsi1_ddr", 1600 "dsi1_ddr_inv", 1601 "dsi1_ddr2", 1602 "dsi1_ddr2_inv", 1603 "dsi1_byte", 1604 "dsi1_byte_inv", 1605 }; 1606 1607 #define REGISTER_DSI0_CLK(s, ...) REGISTER_CLK( \ 1608 s, \ 1609 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents), \ 1610 .parents = bcm2835_clock_dsi0_parents, \ 1611 __VA_ARGS__) 1612 1613 #define REGISTER_DSI1_CLK(s, ...) REGISTER_CLK( \ 1614 s, \ 1615 .num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents), \ 1616 .parents = bcm2835_clock_dsi1_parents, \ 1617 __VA_ARGS__) 1618 1619 /* 1620 * the real definition of all the pll, pll_dividers and clocks 1621 * these make use of the above REGISTER_* macros 1622 */ 1623 static const struct bcm2835_clk_desc clk_desc_array[] = { 1624 /* the PLL + PLL dividers */ 1625 1626 /* 1627 * PLLA is the auxiliary PLL, used to drive the CCP2 1628 * (Compact Camera Port 2) transmitter clock. 1629 * 1630 * It is in the PX LDO power domain, which is on when the 1631 * AUDIO domain is on. 1632 */ 1633 [BCM2835_PLLA] = REGISTER_PLL( 1634 SOC_ALL, 1635 .name = "plla", 1636 .cm_ctrl_reg = CM_PLLA, 1637 .a2w_ctrl_reg = A2W_PLLA_CTRL, 1638 .frac_reg = A2W_PLLA_FRAC, 1639 .ana_reg_base = A2W_PLLA_ANA0, 1640 .reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE, 1641 .lock_mask = CM_LOCK_FLOCKA, 1642 1643 .ana = &bcm2835_ana_default, 1644 1645 .min_rate = 600000000u, 1646 .max_rate = 2400000000u, 1647 .max_fb_rate = BCM2835_MAX_FB_RATE), 1648 [BCM2835_PLLA_CORE] = REGISTER_PLL_DIV( 1649 SOC_ALL, 1650 .name = "plla_core", 1651 .source_pll = "plla", 1652 .cm_reg = CM_PLLA, 1653 .a2w_reg = A2W_PLLA_CORE, 1654 .load_mask = CM_PLLA_LOADCORE, 1655 .hold_mask = CM_PLLA_HOLDCORE, 1656 .fixed_divider = 1, 1657 .flags = CLK_SET_RATE_PARENT), 1658 [BCM2835_PLLA_PER] = REGISTER_PLL_DIV( 1659 SOC_ALL, 1660 .name = "plla_per", 1661 .source_pll = "plla", 1662 .cm_reg = CM_PLLA, 1663 .a2w_reg = A2W_PLLA_PER, 1664 .load_mask = CM_PLLA_LOADPER, 1665 .hold_mask = CM_PLLA_HOLDPER, 1666 .fixed_divider = 1, 1667 .flags = CLK_SET_RATE_PARENT), 1668 [BCM2835_PLLA_DSI0] = REGISTER_PLL_DIV( 1669 SOC_ALL, 1670 .name = "plla_dsi0", 1671 .source_pll = "plla", 1672 .cm_reg = CM_PLLA, 1673 .a2w_reg = A2W_PLLA_DSI0, 1674 .load_mask = CM_PLLA_LOADDSI0, 1675 .hold_mask = CM_PLLA_HOLDDSI0, 1676 .fixed_divider = 1), 1677 [BCM2835_PLLA_CCP2] = REGISTER_PLL_DIV( 1678 SOC_ALL, 1679 .name = "plla_ccp2", 1680 .source_pll = "plla", 1681 .cm_reg = CM_PLLA, 1682 .a2w_reg = A2W_PLLA_CCP2, 1683 .load_mask = CM_PLLA_LOADCCP2, 1684 .hold_mask = CM_PLLA_HOLDCCP2, 1685 .fixed_divider = 1, 1686 .flags = CLK_SET_RATE_PARENT), 1687 1688 /* PLLB is used for the ARM's clock. */ 1689 [BCM2835_PLLB] = REGISTER_PLL( 1690 SOC_ALL, 1691 .name = "pllb", 1692 .cm_ctrl_reg = CM_PLLB, 1693 .a2w_ctrl_reg = A2W_PLLB_CTRL, 1694 .frac_reg = A2W_PLLB_FRAC, 1695 .ana_reg_base = A2W_PLLB_ANA0, 1696 .reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE, 1697 .lock_mask = CM_LOCK_FLOCKB, 1698 1699 .ana = &bcm2835_ana_default, 1700 1701 .min_rate = 600000000u, 1702 .max_rate = 3000000000u, 1703 .max_fb_rate = BCM2835_MAX_FB_RATE, 1704 .flags = CLK_GET_RATE_NOCACHE), 1705 [BCM2835_PLLB_ARM] = REGISTER_PLL_DIV( 1706 SOC_ALL, 1707 .name = "pllb_arm", 1708 .source_pll = "pllb", 1709 .cm_reg = CM_PLLB, 1710 .a2w_reg = A2W_PLLB_ARM, 1711 .load_mask = CM_PLLB_LOADARM, 1712 .hold_mask = CM_PLLB_HOLDARM, 1713 .fixed_divider = 1, 1714 .flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE), 1715 1716 /* 1717 * PLLC is the core PLL, used to drive the core VPU clock. 1718 * 1719 * It is in the PX LDO power domain, which is on when the 1720 * AUDIO domain is on. 1721 */ 1722 [BCM2835_PLLC] = REGISTER_PLL( 1723 SOC_ALL, 1724 .name = "pllc", 1725 .cm_ctrl_reg = CM_PLLC, 1726 .a2w_ctrl_reg = A2W_PLLC_CTRL, 1727 .frac_reg = A2W_PLLC_FRAC, 1728 .ana_reg_base = A2W_PLLC_ANA0, 1729 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE, 1730 .lock_mask = CM_LOCK_FLOCKC, 1731 1732 .ana = &bcm2835_ana_default, 1733 1734 .min_rate = 600000000u, 1735 .max_rate = 3000000000u, 1736 .max_fb_rate = BCM2835_MAX_FB_RATE), 1737 [BCM2835_PLLC_CORE0] = REGISTER_PLL_DIV( 1738 SOC_ALL, 1739 .name = "pllc_core0", 1740 .source_pll = "pllc", 1741 .cm_reg = CM_PLLC, 1742 .a2w_reg = A2W_PLLC_CORE0, 1743 .load_mask = CM_PLLC_LOADCORE0, 1744 .hold_mask = CM_PLLC_HOLDCORE0, 1745 .fixed_divider = 1, 1746 .flags = CLK_SET_RATE_PARENT), 1747 [BCM2835_PLLC_CORE1] = REGISTER_PLL_DIV( 1748 SOC_ALL, 1749 .name = "pllc_core1", 1750 .source_pll = "pllc", 1751 .cm_reg = CM_PLLC, 1752 .a2w_reg = A2W_PLLC_CORE1, 1753 .load_mask = CM_PLLC_LOADCORE1, 1754 .hold_mask = CM_PLLC_HOLDCORE1, 1755 .fixed_divider = 1, 1756 .flags = CLK_SET_RATE_PARENT), 1757 [BCM2835_PLLC_CORE2] = REGISTER_PLL_DIV( 1758 SOC_ALL, 1759 .name = "pllc_core2", 1760 .source_pll = "pllc", 1761 .cm_reg = CM_PLLC, 1762 .a2w_reg = A2W_PLLC_CORE2, 1763 .load_mask = CM_PLLC_LOADCORE2, 1764 .hold_mask = CM_PLLC_HOLDCORE2, 1765 .fixed_divider = 1, 1766 .flags = CLK_SET_RATE_PARENT), 1767 [BCM2835_PLLC_PER] = REGISTER_PLL_DIV( 1768 SOC_ALL, 1769 .name = "pllc_per", 1770 .source_pll = "pllc", 1771 .cm_reg = CM_PLLC, 1772 .a2w_reg = A2W_PLLC_PER, 1773 .load_mask = CM_PLLC_LOADPER, 1774 .hold_mask = CM_PLLC_HOLDPER, 1775 .fixed_divider = 1, 1776 .flags = CLK_SET_RATE_PARENT), 1777 1778 /* 1779 * PLLD is the display PLL, used to drive DSI display panels. 1780 * 1781 * It is in the PX LDO power domain, which is on when the 1782 * AUDIO domain is on. 1783 */ 1784 [BCM2835_PLLD] = REGISTER_PLL( 1785 SOC_ALL, 1786 .name = "plld", 1787 .cm_ctrl_reg = CM_PLLD, 1788 .a2w_ctrl_reg = A2W_PLLD_CTRL, 1789 .frac_reg = A2W_PLLD_FRAC, 1790 .ana_reg_base = A2W_PLLD_ANA0, 1791 .reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE, 1792 .lock_mask = CM_LOCK_FLOCKD, 1793 1794 .ana = &bcm2835_ana_default, 1795 1796 .min_rate = 600000000u, 1797 .max_rate = 2400000000u, 1798 .max_fb_rate = BCM2835_MAX_FB_RATE), 1799 [BCM2835_PLLD_CORE] = REGISTER_PLL_DIV( 1800 SOC_ALL, 1801 .name = "plld_core", 1802 .source_pll = "plld", 1803 .cm_reg = CM_PLLD, 1804 .a2w_reg = A2W_PLLD_CORE, 1805 .load_mask = CM_PLLD_LOADCORE, 1806 .hold_mask = CM_PLLD_HOLDCORE, 1807 .fixed_divider = 1, 1808 .flags = CLK_SET_RATE_PARENT), 1809 /* 1810 * VPU firmware assumes that PLLD_PER isn't disabled by the ARM core. 1811 * Otherwise this could cause firmware lookups. That's why we mark 1812 * it as critical. 1813 */ 1814 [BCM2835_PLLD_PER] = REGISTER_PLL_DIV( 1815 SOC_ALL, 1816 .name = "plld_per", 1817 .source_pll = "plld", 1818 .cm_reg = CM_PLLD, 1819 .a2w_reg = A2W_PLLD_PER, 1820 .load_mask = CM_PLLD_LOADPER, 1821 .hold_mask = CM_PLLD_HOLDPER, 1822 .fixed_divider = 1, 1823 .flags = CLK_IS_CRITICAL | CLK_SET_RATE_PARENT), 1824 [BCM2835_PLLD_DSI0] = REGISTER_PLL_DIV( 1825 SOC_ALL, 1826 .name = "plld_dsi0", 1827 .source_pll = "plld", 1828 .cm_reg = CM_PLLD, 1829 .a2w_reg = A2W_PLLD_DSI0, 1830 .load_mask = CM_PLLD_LOADDSI0, 1831 .hold_mask = CM_PLLD_HOLDDSI0, 1832 .fixed_divider = 1), 1833 [BCM2835_PLLD_DSI1] = REGISTER_PLL_DIV( 1834 SOC_ALL, 1835 .name = "plld_dsi1", 1836 .source_pll = "plld", 1837 .cm_reg = CM_PLLD, 1838 .a2w_reg = A2W_PLLD_DSI1, 1839 .load_mask = CM_PLLD_LOADDSI1, 1840 .hold_mask = CM_PLLD_HOLDDSI1, 1841 .fixed_divider = 1), 1842 1843 /* 1844 * PLLH is used to supply the pixel clock or the AUX clock for the 1845 * TV encoder. 1846 * 1847 * It is in the HDMI power domain. 1848 */ 1849 [BCM2835_PLLH] = REGISTER_PLL( 1850 SOC_BCM2835, 1851 "pllh", 1852 .cm_ctrl_reg = CM_PLLH, 1853 .a2w_ctrl_reg = A2W_PLLH_CTRL, 1854 .frac_reg = A2W_PLLH_FRAC, 1855 .ana_reg_base = A2W_PLLH_ANA0, 1856 .reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE, 1857 .lock_mask = CM_LOCK_FLOCKH, 1858 1859 .ana = &bcm2835_ana_pllh, 1860 1861 .min_rate = 600000000u, 1862 .max_rate = 3000000000u, 1863 .max_fb_rate = BCM2835_MAX_FB_RATE), 1864 [BCM2835_PLLH_RCAL] = REGISTER_PLL_DIV( 1865 SOC_BCM2835, 1866 .name = "pllh_rcal", 1867 .source_pll = "pllh", 1868 .cm_reg = CM_PLLH, 1869 .a2w_reg = A2W_PLLH_RCAL, 1870 .load_mask = CM_PLLH_LOADRCAL, 1871 .hold_mask = 0, 1872 .fixed_divider = 10, 1873 .flags = CLK_SET_RATE_PARENT), 1874 [BCM2835_PLLH_AUX] = REGISTER_PLL_DIV( 1875 SOC_BCM2835, 1876 .name = "pllh_aux", 1877 .source_pll = "pllh", 1878 .cm_reg = CM_PLLH, 1879 .a2w_reg = A2W_PLLH_AUX, 1880 .load_mask = CM_PLLH_LOADAUX, 1881 .hold_mask = 0, 1882 .fixed_divider = 1, 1883 .flags = CLK_SET_RATE_PARENT), 1884 [BCM2835_PLLH_PIX] = REGISTER_PLL_DIV( 1885 SOC_BCM2835, 1886 .name = "pllh_pix", 1887 .source_pll = "pllh", 1888 .cm_reg = CM_PLLH, 1889 .a2w_reg = A2W_PLLH_PIX, 1890 .load_mask = CM_PLLH_LOADPIX, 1891 .hold_mask = 0, 1892 .fixed_divider = 10, 1893 .flags = CLK_SET_RATE_PARENT), 1894 1895 /* the clocks */ 1896 1897 /* clocks with oscillator parent mux */ 1898 1899 /* One Time Programmable Memory clock. Maximum 10Mhz. */ 1900 [BCM2835_CLOCK_OTP] = REGISTER_OSC_CLK( 1901 SOC_ALL, 1902 .name = "otp", 1903 .ctl_reg = CM_OTPCTL, 1904 .div_reg = CM_OTPDIV, 1905 .int_bits = 4, 1906 .frac_bits = 0, 1907 .tcnt_mux = 6), 1908 /* 1909 * Used for a 1Mhz clock for the system clocksource, and also used 1910 * bythe watchdog timer and the camera pulse generator. 1911 */ 1912 [BCM2835_CLOCK_TIMER] = REGISTER_OSC_CLK( 1913 SOC_ALL, 1914 .name = "timer", 1915 .ctl_reg = CM_TIMERCTL, 1916 .div_reg = CM_TIMERDIV, 1917 .int_bits = 6, 1918 .frac_bits = 12), 1919 /* 1920 * Clock for the temperature sensor. 1921 * Generally run at 2Mhz, max 5Mhz. 1922 */ 1923 [BCM2835_CLOCK_TSENS] = REGISTER_OSC_CLK( 1924 SOC_ALL, 1925 .name = "tsens", 1926 .ctl_reg = CM_TSENSCTL, 1927 .div_reg = CM_TSENSDIV, 1928 .int_bits = 5, 1929 .frac_bits = 0), 1930 [BCM2835_CLOCK_TEC] = REGISTER_OSC_CLK( 1931 SOC_ALL, 1932 .name = "tec", 1933 .ctl_reg = CM_TECCTL, 1934 .div_reg = CM_TECDIV, 1935 .int_bits = 6, 1936 .frac_bits = 0), 1937 1938 /* clocks with vpu parent mux */ 1939 [BCM2835_CLOCK_H264] = REGISTER_VPU_CLK( 1940 SOC_ALL, 1941 .name = "h264", 1942 .ctl_reg = CM_H264CTL, 1943 .div_reg = CM_H264DIV, 1944 .int_bits = 4, 1945 .frac_bits = 8, 1946 .tcnt_mux = 1), 1947 [BCM2835_CLOCK_ISP] = REGISTER_VPU_CLK( 1948 SOC_ALL, 1949 .name = "isp", 1950 .ctl_reg = CM_ISPCTL, 1951 .div_reg = CM_ISPDIV, 1952 .int_bits = 4, 1953 .frac_bits = 8, 1954 .tcnt_mux = 2), 1955 1956 /* 1957 * Secondary SDRAM clock. Used for low-voltage modes when the PLL 1958 * in the SDRAM controller can't be used. 1959 */ 1960 [BCM2835_CLOCK_SDRAM] = REGISTER_VPU_CLK( 1961 SOC_ALL, 1962 .name = "sdram", 1963 .ctl_reg = CM_SDCCTL, 1964 .div_reg = CM_SDCDIV, 1965 .int_bits = 6, 1966 .frac_bits = 0, 1967 .tcnt_mux = 3), 1968 [BCM2835_CLOCK_V3D] = REGISTER_VPU_CLK( 1969 SOC_ALL, 1970 .name = "v3d", 1971 .ctl_reg = CM_V3DCTL, 1972 .div_reg = CM_V3DDIV, 1973 .int_bits = 4, 1974 .frac_bits = 8, 1975 .tcnt_mux = 4), 1976 /* 1977 * VPU clock. This doesn't have an enable bit, since it drives 1978 * the bus for everything else, and is special so it doesn't need 1979 * to be gated for rate changes. It is also known as "clk_audio" 1980 * in various hardware documentation. 1981 */ 1982 [BCM2835_CLOCK_VPU] = REGISTER_VPU_CLK( 1983 SOC_ALL, 1984 .name = "vpu", 1985 .ctl_reg = CM_VPUCTL, 1986 .div_reg = CM_VPUDIV, 1987 .int_bits = 12, 1988 .frac_bits = 8, 1989 .flags = CLK_IS_CRITICAL, 1990 .is_vpu_clock = true, 1991 .tcnt_mux = 5), 1992 1993 /* clocks with per parent mux */ 1994 [BCM2835_CLOCK_AVEO] = REGISTER_PER_CLK( 1995 SOC_ALL, 1996 .name = "aveo", 1997 .ctl_reg = CM_AVEOCTL, 1998 .div_reg = CM_AVEODIV, 1999 .int_bits = 4, 2000 .frac_bits = 0, 2001 .tcnt_mux = 38), 2002 [BCM2835_CLOCK_CAM0] = REGISTER_PER_CLK( 2003 SOC_ALL, 2004 .name = "cam0", 2005 .ctl_reg = CM_CAM0CTL, 2006 .div_reg = CM_CAM0DIV, 2007 .int_bits = 4, 2008 .frac_bits = 8, 2009 .tcnt_mux = 14), 2010 [BCM2835_CLOCK_CAM1] = REGISTER_PER_CLK( 2011 SOC_ALL, 2012 .name = "cam1", 2013 .ctl_reg = CM_CAM1CTL, 2014 .div_reg = CM_CAM1DIV, 2015 .int_bits = 4, 2016 .frac_bits = 8, 2017 .tcnt_mux = 15), 2018 [BCM2835_CLOCK_DFT] = REGISTER_PER_CLK( 2019 SOC_ALL, 2020 .name = "dft", 2021 .ctl_reg = CM_DFTCTL, 2022 .div_reg = CM_DFTDIV, 2023 .int_bits = 5, 2024 .frac_bits = 0), 2025 [BCM2835_CLOCK_DPI] = REGISTER_PER_CLK( 2026 SOC_ALL, 2027 .name = "dpi", 2028 .ctl_reg = CM_DPICTL, 2029 .div_reg = CM_DPIDIV, 2030 .int_bits = 4, 2031 .frac_bits = 8, 2032 .tcnt_mux = 17), 2033 2034 /* Arasan EMMC clock */ 2035 [BCM2835_CLOCK_EMMC] = REGISTER_PER_CLK( 2036 SOC_ALL, 2037 .name = "emmc", 2038 .ctl_reg = CM_EMMCCTL, 2039 .div_reg = CM_EMMCDIV, 2040 .int_bits = 4, 2041 .frac_bits = 8, 2042 .tcnt_mux = 39), 2043 2044 /* EMMC2 clock (only available for BCM2711) */ 2045 [BCM2711_CLOCK_EMMC2] = REGISTER_PER_CLK( 2046 SOC_BCM2711, 2047 .name = "emmc2", 2048 .ctl_reg = CM_EMMC2CTL, 2049 .div_reg = CM_EMMC2DIV, 2050 .int_bits = 4, 2051 .frac_bits = 8, 2052 .tcnt_mux = 42), 2053 2054 /* General purpose (GPIO) clocks */ 2055 [BCM2835_CLOCK_GP0] = REGISTER_PER_CLK( 2056 SOC_ALL, 2057 .name = "gp0", 2058 .ctl_reg = CM_GP0CTL, 2059 .div_reg = CM_GP0DIV, 2060 .int_bits = 12, 2061 .frac_bits = 12, 2062 .is_mash_clock = true, 2063 .tcnt_mux = 20), 2064 [BCM2835_CLOCK_GP1] = REGISTER_PER_CLK( 2065 SOC_ALL, 2066 .name = "gp1", 2067 .ctl_reg = CM_GP1CTL, 2068 .div_reg = CM_GP1DIV, 2069 .int_bits = 12, 2070 .frac_bits = 12, 2071 .flags = CLK_IS_CRITICAL, 2072 .is_mash_clock = true, 2073 .tcnt_mux = 21), 2074 [BCM2835_CLOCK_GP2] = REGISTER_PER_CLK( 2075 SOC_ALL, 2076 .name = "gp2", 2077 .ctl_reg = CM_GP2CTL, 2078 .div_reg = CM_GP2DIV, 2079 .int_bits = 12, 2080 .frac_bits = 12, 2081 .flags = CLK_IS_CRITICAL), 2082 2083 /* HDMI state machine */ 2084 [BCM2835_CLOCK_HSM] = REGISTER_PER_CLK( 2085 SOC_ALL, 2086 .name = "hsm", 2087 .ctl_reg = CM_HSMCTL, 2088 .div_reg = CM_HSMDIV, 2089 .int_bits = 4, 2090 .frac_bits = 8, 2091 .tcnt_mux = 22), 2092 [BCM2835_CLOCK_PCM] = REGISTER_PCM_CLK( 2093 SOC_ALL, 2094 .name = "pcm", 2095 .ctl_reg = CM_PCMCTL, 2096 .div_reg = CM_PCMDIV, 2097 .int_bits = 12, 2098 .frac_bits = 12, 2099 .is_mash_clock = true, 2100 .low_jitter = true, 2101 .tcnt_mux = 23), 2102 [BCM2835_CLOCK_PWM] = REGISTER_PER_CLK( 2103 SOC_ALL, 2104 .name = "pwm", 2105 .ctl_reg = CM_PWMCTL, 2106 .div_reg = CM_PWMDIV, 2107 .int_bits = 12, 2108 .frac_bits = 12, 2109 .is_mash_clock = true, 2110 .tcnt_mux = 24), 2111 [BCM2835_CLOCK_SLIM] = REGISTER_PER_CLK( 2112 SOC_ALL, 2113 .name = "slim", 2114 .ctl_reg = CM_SLIMCTL, 2115 .div_reg = CM_SLIMDIV, 2116 .int_bits = 12, 2117 .frac_bits = 12, 2118 .is_mash_clock = true, 2119 .tcnt_mux = 25), 2120 [BCM2835_CLOCK_SMI] = REGISTER_PER_CLK( 2121 SOC_ALL, 2122 .name = "smi", 2123 .ctl_reg = CM_SMICTL, 2124 .div_reg = CM_SMIDIV, 2125 .int_bits = 4, 2126 .frac_bits = 8, 2127 .tcnt_mux = 27), 2128 [BCM2835_CLOCK_UART] = REGISTER_PER_CLK( 2129 SOC_ALL, 2130 .name = "uart", 2131 .ctl_reg = CM_UARTCTL, 2132 .div_reg = CM_UARTDIV, 2133 .int_bits = 10, 2134 .frac_bits = 12, 2135 .tcnt_mux = 28), 2136 2137 /* TV encoder clock. Only operating frequency is 108Mhz. */ 2138 [BCM2835_CLOCK_VEC] = REGISTER_PER_CLK( 2139 SOC_ALL, 2140 .name = "vec", 2141 .ctl_reg = CM_VECCTL, 2142 .div_reg = CM_VECDIV, 2143 .int_bits = 4, 2144 .frac_bits = 0, 2145 /* 2146 * Allow rate change propagation only on PLLH_AUX which is 2147 * assigned index 7 in the parent array. 2148 */ 2149 .set_rate_parent = BIT(7), 2150 .tcnt_mux = 29), 2151 2152 /* dsi clocks */ 2153 [BCM2835_CLOCK_DSI0E] = REGISTER_PER_CLK( 2154 SOC_ALL, 2155 .name = "dsi0e", 2156 .ctl_reg = CM_DSI0ECTL, 2157 .div_reg = CM_DSI0EDIV, 2158 .int_bits = 4, 2159 .frac_bits = 8, 2160 .tcnt_mux = 18), 2161 [BCM2835_CLOCK_DSI1E] = REGISTER_PER_CLK( 2162 SOC_ALL, 2163 .name = "dsi1e", 2164 .ctl_reg = CM_DSI1ECTL, 2165 .div_reg = CM_DSI1EDIV, 2166 .int_bits = 4, 2167 .frac_bits = 8, 2168 .tcnt_mux = 19), 2169 [BCM2835_CLOCK_DSI0P] = REGISTER_DSI0_CLK( 2170 SOC_ALL, 2171 .name = "dsi0p", 2172 .ctl_reg = CM_DSI0PCTL, 2173 .div_reg = CM_DSI0PDIV, 2174 .int_bits = 0, 2175 .frac_bits = 0, 2176 .tcnt_mux = 12), 2177 [BCM2835_CLOCK_DSI1P] = REGISTER_DSI1_CLK( 2178 SOC_ALL, 2179 .name = "dsi1p", 2180 .ctl_reg = CM_DSI1PCTL, 2181 .div_reg = CM_DSI1PDIV, 2182 .int_bits = 0, 2183 .frac_bits = 0, 2184 .tcnt_mux = 13), 2185 2186 /* the gates */ 2187 2188 /* 2189 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if 2190 * you have the debug bit set in the power manager, which we 2191 * don't bother exposing) are individual gates off of the 2192 * non-stop vpu clock. 2193 */ 2194 [BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE( 2195 SOC_ALL, 2196 .name = "peri_image", 2197 .parent = "vpu", 2198 .ctl_reg = CM_PERIICTL), 2199 }; 2200 2201 /* 2202 * Permanently take a reference on the parent of the SDRAM clock. 2203 * 2204 * While the SDRAM is being driven by its dedicated PLL most of the 2205 * time, there is a little loop running in the firmware that 2206 * periodically switches the SDRAM to using our CM clock to do PVT 2207 * recalibration, with the assumption that the previously configured 2208 * SDRAM parent is still enabled and running. 2209 */ 2210 static int bcm2835_mark_sdc_parent_critical(struct clk *sdc) 2211 { 2212 struct clk *parent = clk_get_parent(sdc); 2213 2214 if (IS_ERR(parent)) 2215 return PTR_ERR(parent); 2216 2217 return clk_prepare_enable(parent); 2218 } 2219 2220 static int bcm2835_clk_probe(struct platform_device *pdev) 2221 { 2222 struct device *dev = &pdev->dev; 2223 struct clk_hw **hws; 2224 struct bcm2835_cprman *cprman; 2225 const struct bcm2835_clk_desc *desc; 2226 const size_t asize = ARRAY_SIZE(clk_desc_array); 2227 const struct cprman_plat_data *pdata; 2228 size_t i; 2229 int ret; 2230 2231 pdata = of_device_get_match_data(&pdev->dev); 2232 if (!pdata) 2233 return -ENODEV; 2234 2235 cprman = devm_kzalloc(dev, 2236 struct_size(cprman, onecell.hws, asize), 2237 GFP_KERNEL); 2238 if (!cprman) 2239 return -ENOMEM; 2240 2241 spin_lock_init(&cprman->regs_lock); 2242 cprman->dev = dev; 2243 cprman->regs = devm_platform_ioremap_resource(pdev, 0); 2244 if (IS_ERR(cprman->regs)) 2245 return PTR_ERR(cprman->regs); 2246 2247 memcpy(cprman->real_parent_names, cprman_parent_names, 2248 sizeof(cprman_parent_names)); 2249 of_clk_parent_fill(dev->of_node, cprman->real_parent_names, 2250 ARRAY_SIZE(cprman_parent_names)); 2251 2252 /* 2253 * Make sure the external oscillator has been registered. 2254 * 2255 * The other (DSI) clocks are not present on older device 2256 * trees, which we still need to support for backwards 2257 * compatibility. 2258 */ 2259 if (!cprman->real_parent_names[0]) 2260 return -ENODEV; 2261 2262 platform_set_drvdata(pdev, cprman); 2263 2264 cprman->onecell.num = asize; 2265 hws = cprman->onecell.hws; 2266 2267 for (i = 0; i < asize; i++) { 2268 desc = &clk_desc_array[i]; 2269 if (desc->clk_register && desc->data && 2270 (desc->supported & pdata->soc)) { 2271 hws[i] = desc->clk_register(cprman, desc->data); 2272 } 2273 } 2274 2275 ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk); 2276 if (ret) 2277 return ret; 2278 2279 return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get, 2280 &cprman->onecell); 2281 } 2282 2283 static const struct cprman_plat_data cprman_bcm2835_plat_data = { 2284 .soc = SOC_BCM2835, 2285 }; 2286 2287 static const struct cprman_plat_data cprman_bcm2711_plat_data = { 2288 .soc = SOC_BCM2711, 2289 }; 2290 2291 static const struct of_device_id bcm2835_clk_of_match[] = { 2292 { .compatible = "brcm,bcm2835-cprman", .data = &cprman_bcm2835_plat_data }, 2293 { .compatible = "brcm,bcm2711-cprman", .data = &cprman_bcm2711_plat_data }, 2294 {} 2295 }; 2296 MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match); 2297 2298 static struct platform_driver bcm2835_clk_driver = { 2299 .driver = { 2300 .name = "bcm2835-clk", 2301 .of_match_table = bcm2835_clk_of_match, 2302 }, 2303 .probe = bcm2835_clk_probe, 2304 }; 2305 2306 builtin_platform_driver(bcm2835_clk_driver); 2307 2308 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>"); 2309 MODULE_DESCRIPTION("BCM2835 clock driver"); 2310 MODULE_LICENSE("GPL"); 2311