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