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