| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <delay.h> |
| 7 |
#include <device/mmio.h> |
| 8 |
#include <soc/addressmap.h> |
| 9 |
#include <soc/clock.h> |
| 10 |
#include <gpio.h> |
| 11 |
#include <stdint.h> |
| 12 |
|
| 13 |
|
| 14 |
#define FU740_HFCLK_FREQ (26 * MHz) |
| 15 |
|
| 16 |
struct prci_ctlr { |
| 17 |
u32 hfxosccfg; |
| 18 |
u32 core_pllcfg; |
| 19 |
u32 core_plloutdiv; |
| 20 |
u32 ddr_pllcfg; |
| 21 |
u32 ddr_plloutdiv; |
| 22 |
u32 pcieaux_plloutdiv; |
| 23 |
u32 reserved18; |
| 24 |
u32 gemgxl_pllcfg; |
| 25 |
u32 gemgxl_plloutdiv; |
| 26 |
u32 core_clk_sel_reg; |
| 27 |
u32 devices_reset_n; |
| 28 |
u32 clk_mux_status; |
| 29 |
u32 cltx_pllcfg; |
| 30 |
u32 cltx_plloutdiv; |
| 31 |
u32 dvfs_core_pllcfg; |
| 32 |
u32 dvfs_core_plloutdiv; |
| 33 |
u32 corepllsel; |
| 34 |
u8 reserved44[12]; |
| 35 |
u32 hfpclk_pllcfg; |
| 36 |
u32 hfpclk_plloutdiv; |
| 37 |
u32 hfpclkpllsel; |
| 38 |
u32 hfpclk_div_reg; |
| 39 |
u8 reserved60[128]; |
| 40 |
u32 prci_plls; |
| 41 |
u8 reservedE4[12]; |
| 42 |
u32 procmoncfg_core_clock; |
| 43 |
} __packed; |
| 44 |
|
| 45 |
static struct prci_ctlr *prci = (void *)FU740_PRCI; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define PRCI_COREPLLSEL_MASK 1 |
| 52 |
#define PRCI_COREPLLSEL_COREPLL 0 |
| 53 |
#define PRCI_COREPLLSEL_DVFSCOREPLL 1 |
| 54 |
|
| 55 |
#define PRCI_CORECLKSEL_MASK 1 |
| 56 |
#define PRCI_CORECLKSEL_CORECLKPLL 0 |
| 57 |
#define PRCI_CORECLKSEL_HFCLK 1 |
| 58 |
|
| 59 |
#define PRCI_HFPCLKSEL_MASK 1 |
| 60 |
#define PRCI_HFPCLKSEL_PLL 0 |
| 61 |
#define PRCI_HFPCLKSEL_HFCLK 1 |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#define PRCI_PLLCFG_DIVR_SHIFT 0 |
| 68 |
#define PRCI_PLLCFG_DIVF_SHIFT 6 |
| 69 |
#define PRCI_PLLCFG_DIVQ_SHIFT 15 |
| 70 |
#define PRCI_PLLCFG_RANGE_SHIFT 18 |
| 71 |
#define PRCI_PLLCFG_BYPASS_SHIFT 24 |
| 72 |
#define PRCI_PLLCFG_FSEBYPASS_SHIFT 25 |
| 73 |
#define PRCI_PLLCFG_LOCK_SHIFT 31 |
| 74 |
|
| 75 |
#define PRCI_PLLCFG_DIVR_MASK (0x03f << PRCI_PLLCFG_DIVR_SHIFT) |
| 76 |
#define PRCI_PLLCFG_DIVF_MASK (0x1ff << PRCI_PLLCFG_DIVF_SHIFT) |
| 77 |
#define PRCI_PLLCFG_DIVQ_MASK (0x007 << PRCI_PLLCFG_DIVQ_SHIFT) |
| 78 |
#define PRCI_PLLCFG_RANGE_MASK (0x007 << PRCI_PLLCFG_RANGE_SHIFT) |
| 79 |
#define PRCI_PLLCFG_BYPASS_MASK (0x001 << PRCI_PLLCFG_BYPASS_SHIFT) |
| 80 |
#define PRCI_PLLCFG_FSEBYPASS_MASK (0x001 << PRCI_PLLCFG_FSEBYPASS_SHIFT) |
| 81 |
#define PRCI_PLLCFG_LOCK_MASK (0x001 << PRCI_PLLCFG_LOCK_SHIFT) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#define PRCI_DVFSCORE_PLLOUTDIV_MASK (1 << 24) // Note: u-boot and fu740 manual differ here ... |
| 89 |
#define PRCI_HFPCLK_PLLOUTDIV_MASK (1 << 31) // Note: according to u-boot it is (1 << 24) but if I use that it gets stuck |
| 90 |
#define PRCI_DDR_PLLOUTDIV_MASK (1 << 31) |
| 91 |
#define PRCI_GEMGXL_PLLOUTDIV_MASK (1 << 31) |
| 92 |
#define PRCI_CLTX_PLLOUTDIV_MASK (1 << 24) // undocumented (chiplink tx) |
| 93 |
#define PRCI_PCIEAUX_PLLOUTDIV_MASK (1 << 0) // undocumented |
| 94 |
#define PRCI_CORE_PLLOUTDIV_MASK (1 << 31) // undocumented |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
#define PRCI_DEVICES_RESET_DDR_CTRL_RST (1 << 0) // DDR Controller |
| 102 |
#define PRCI_DEVICES_RESET_DDR_AXI_RST (1 << 1) // DDR Controller AXI Interface |
| 103 |
#define PRCI_DEVICES_RESET_DDR_AHB_RST (1 << 2) // DDR Controller AHB Interface |
| 104 |
#define PRCI_DEVICES_RESET_DDR_PHY_RST (1 << 3) // DDR PHY |
| 105 |
#define PRCI_DEVICES_RESET_PCIEAUX_RST (1 << 4) |
| 106 |
#define PRCI_DEVICES_RESET_GEMGXL_RST (1 << 5) // Gigabit Ethernet Subsystem |
| 107 |
#define PRCI_DEVICES_RESET_CLTX_RST (1 << 6) // chiplink reset (undocumented) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
#define PRCI_PLLS_CLTXPLL (1 << 0) |
| 115 |
#define PRCI_PLLS_GEMGXLPLL (1 << 1) |
| 116 |
#define PRCI_PLLS_DDRPLL (1 << 2) |
| 117 |
#define PRCI_PLLS_HFPCLKPLL (1 << 3) |
| 118 |
#define PRCI_PLLS_DVFSCOREPLL (1 << 4) |
| 119 |
#define PRCI_PLLS_COREPLL (1 << 5) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#define PRCI_CLK_MUX_STATUS_CORECLKPLLSEL (1 << 0) |
| 129 |
|
| 130 |
|
| 131 |
#define PRCI_CLK_MUX_STATUS_TLCLKSEL (1 << 1) |
| 132 |
|
| 133 |
|
| 134 |
#define PRCI_CLK_MUX_STATUS_RTCXSEL (1 << 2) |
| 135 |
|
| 136 |
|
| 137 |
#define PRCI_CLK_MUX_STATUS_DDRCTRLCLKSEL (1 << 3) |
| 138 |
#define PRCI_CLK_MUX_STATUS_DDRPHYCLKSEL (1 << 4) |
| 139 |
#define PRCI_CLK_MUX_STATUS_RESERVED (1 << 5) |
| 140 |
#define PRCI_CLK_MUX_STATUS_GEMGXLCLKSEL (1 << 6) |
| 141 |
#define PRCI_CLK_MUX_STATUS_MAINMEMCLKSEL (1 << 7) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#define PRCI_HFXOSCCFG_HFXOSEN (1 << 30) // Crystal oscillator enable |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
#define PRCI_HFXOSCCFG_HFXOSCRDY (1 << 31) // Crystal oscillator ready |
| 152 |
|
| 153 |
struct pll_settings { |
| 154 |
unsigned int divr:6; |
| 155 |
unsigned int divf:9; |
| 156 |
unsigned int divq:3; |
| 157 |
|
| 158 |
unsigned int range:3; |
| 159 |
unsigned int bypass:1; |
| 160 |
|
| 161 |
|
| 162 |
unsigned int fsebypass:1; |
| 163 |
}; |
| 164 |
|
| 165 |
static void configure_pll(u32 *reg, const struct pll_settings *s) |
| 166 |
{ |
| 167 |
|
| 168 |
u32 c = read32(reg); |
| 169 |
clrsetbits32(&c, PRCI_PLLCFG_DIVR_MASK |
| 170 |
| PRCI_PLLCFG_DIVF_MASK |
| 171 |
| PRCI_PLLCFG_DIVQ_MASK |
| 172 |
| PRCI_PLLCFG_RANGE_MASK |
| 173 |
| PRCI_PLLCFG_BYPASS_MASK |
| 174 |
| PRCI_PLLCFG_FSEBYPASS_MASK, |
| 175 |
(s->divr << PRCI_PLLCFG_DIVR_SHIFT) |
| 176 |
| (s->divf << PRCI_PLLCFG_DIVF_SHIFT) |
| 177 |
| (s->divq << PRCI_PLLCFG_DIVQ_SHIFT) |
| 178 |
| (s->range << PRCI_PLLCFG_RANGE_SHIFT) |
| 179 |
| (s->bypass << PRCI_PLLCFG_BYPASS_SHIFT) |
| 180 |
| (s->fsebypass << PRCI_PLLCFG_FSEBYPASS_SHIFT)); |
| 181 |
write32(reg, c); |
| 182 |
|
| 183 |
|
| 184 |
while (!(read32(reg) & PRCI_PLLCFG_LOCK_MASK)) |
| 185 |
; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static const struct pll_settings corepll_settings = { |
| 196 |
.divr = 0, |
| 197 |
.divf = 76, |
| 198 |
.divq = 2, |
| 199 |
.range = 4, |
| 200 |
.bypass = 0, |
| 201 |
.fsebypass = 1, |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
static const struct pll_settings ddrpll_settings = { |
| 211 |
.divr = 0, |
| 212 |
.divf = 71, |
| 213 |
.divq = 2, |
| 214 |
.range = 4, |
| 215 |
.bypass = 0, |
| 216 |
.fsebypass = 1, |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static const struct pll_settings gemgxlpll_settings = { |
| 224 |
.divr = 0, |
| 225 |
.divf = 76, |
| 226 |
.divq = 5, |
| 227 |
.range = 4, |
| 228 |
.bypass = 0, |
| 229 |
.fsebypass = 1, |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
static const struct pll_settings hfpclkpll_settings = { |
| 238 |
.divr = 1, |
| 239 |
|
| 240 |
.divf = 39, |
| 241 |
.divq = 2, |
| 242 |
.range = 4, |
| 243 |
.bypass = 0, |
| 244 |
.fsebypass = 1, |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static const struct pll_settings cltxpll_settings = { |
| 252 |
.divr = 1, |
| 253 |
.divf = 39, |
| 254 |
.divq = 2, |
| 255 |
.range = 4, |
| 256 |
.bypass = 0, |
| 257 |
.fsebypass = 1, |
| 258 |
}; |
| 259 |
|
| 260 |
static void init_coreclk(void) |
| 261 |
{ |
| 262 |
|
| 263 |
|
| 264 |
clrsetbits32(&prci->core_clk_sel_reg, PRCI_CORECLKSEL_MASK, PRCI_CORECLKSEL_HFCLK); |
| 265 |
|
| 266 |
|
| 267 |
if (!(read32(&prci->prci_plls) & PRCI_PLLS_COREPLL)) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
configure_pll(&prci->core_pllcfg, &corepll_settings); |
| 272 |
|
| 273 |
|
| 274 |
clrsetbits32(&prci->core_clk_sel_reg, PRCI_CORECLKSEL_MASK, PRCI_CORECLKSEL_CORECLKPLL); |
| 275 |
} |
| 276 |
|
| 277 |
static void init_ddrclk(void) |
| 278 |
{ |
| 279 |
|
| 280 |
if (!(read32(&prci->prci_plls) & PRCI_PLLS_DDRPLL)) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
u32 cfg1 = read32(&prci->ddr_plloutdiv); |
| 286 |
clrbits32(&cfg1, PRCI_DDR_PLLOUTDIV_MASK); |
| 287 |
write32(&prci->ddr_plloutdiv, cfg1); |
| 288 |
|
| 289 |
configure_pll(&prci->ddr_pllcfg, &ddrpll_settings); |
| 290 |
|
| 291 |
|
| 292 |
setbits32(&cfg1, PRCI_DDR_PLLOUTDIV_MASK); |
| 293 |
write32(&prci->ddr_plloutdiv, cfg1); |
| 294 |
} |
| 295 |
|
| 296 |
static void init_gemgxlclk(void) |
| 297 |
{ |
| 298 |
|
| 299 |
if (!(read32(&prci->prci_plls) & PRCI_PLLS_GEMGXLPLL)) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
u32 cfg1 = read32(&prci->gemgxl_plloutdiv); |
| 305 |
clrbits32(&cfg1, PRCI_GEMGXL_PLLOUTDIV_MASK); |
| 306 |
write32(&prci->gemgxl_plloutdiv, cfg1); |
| 307 |
|
| 308 |
configure_pll(&prci->gemgxl_pllcfg, &gemgxlpll_settings); |
| 309 |
|
| 310 |
|
| 311 |
setbits32(&cfg1, PRCI_GEMGXL_PLLOUTDIV_MASK); |
| 312 |
write32(&prci->gemgxl_plloutdiv, cfg1); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
static void init_hfpclk(void) |
| 320 |
{ |
| 321 |
|
| 322 |
|
| 323 |
u32 hfpclksel = read32(&prci->hfpclkpllsel); |
| 324 |
hfpclksel |= PRCI_HFPCLKSEL_HFCLK; |
| 325 |
write32(&prci->hfpclkpllsel, hfpclksel); |
| 326 |
|
| 327 |
configure_pll(&prci->hfpclk_pllcfg, &hfpclkpll_settings); |
| 328 |
|
| 329 |
|
| 330 |
u32 hfpclk_plloutdiv = read32(&prci->hfpclk_plloutdiv); |
| 331 |
hfpclk_plloutdiv |= PRCI_HFPCLK_PLLOUTDIV_MASK; |
| 332 |
write32(&prci->hfpclk_plloutdiv, hfpclk_plloutdiv); |
| 333 |
|
| 334 |
mdelay(1); |
| 335 |
|
| 336 |
|
| 337 |
clrbits32(&prci->hfpclkpllsel, PRCI_HFPCLKSEL_MASK); |
| 338 |
|
| 339 |
udelay(70); |
| 340 |
} |
| 341 |
|
| 342 |
static void reset_deassert(u8 reset_index) |
| 343 |
{ |
| 344 |
u32 device_reset = read32(&prci->devices_reset_n); |
| 345 |
device_reset |= reset_index; |
| 346 |
write32(&prci->devices_reset_n, device_reset); |
| 347 |
} |
| 348 |
|
| 349 |
static void init_cltx(void) |
| 350 |
{ |
| 351 |
|
| 352 |
u32 cfg1 = read32(&prci->cltx_plloutdiv); |
| 353 |
clrbits32(&cfg1, PRCI_CLTX_PLLOUTDIV_MASK); |
| 354 |
write32(&prci->cltx_plloutdiv, cfg1); |
| 355 |
|
| 356 |
configure_pll(&prci->cltx_pllcfg, &cltxpll_settings); |
| 357 |
|
| 358 |
|
| 359 |
setbits32(&cfg1, PRCI_CLTX_PLLOUTDIV_MASK); |
| 360 |
write32(&prci->cltx_plloutdiv, cfg1); |
| 361 |
|
| 362 |
|
| 363 |
reset_deassert(PRCI_DEVICES_RESET_CLTX_RST); |
| 364 |
|
| 365 |
udelay(70); |
| 366 |
} |
| 367 |
|
| 368 |
void clock_init(void) |
| 369 |
{ |
| 370 |
|
| 371 |
init_coreclk(); |
| 372 |
|
| 373 |
|
| 374 |
write32(&prci->devices_reset_n, 0); |
| 375 |
|
| 376 |
|
| 377 |
init_ddrclk(); |
| 378 |
|
| 379 |
|
| 380 |
reset_deassert(PRCI_DEVICES_RESET_DDR_CTRL_RST); |
| 381 |
|
| 382 |
|
| 383 |
asm volatile ("fence"); |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
reset_deassert(PRCI_DEVICES_RESET_DDR_AXI_RST | |
| 388 |
PRCI_DEVICES_RESET_DDR_AHB_RST | |
| 389 |
PRCI_DEVICES_RESET_DDR_PHY_RST); |
| 390 |
|
| 391 |
|
| 392 |
for (int i = 0; i < 256; i++) |
| 393 |
asm volatile ("nop"); |
| 394 |
|
| 395 |
if (read32(&prci->prci_plls) & PRCI_PLLS_HFPCLKPLL) { |
| 396 |
|
| 397 |
|
| 398 |
init_hfpclk(); |
| 399 |
} else if (read32(&prci->prci_plls) & PRCI_PLLS_CLTXPLL) { |
| 400 |
|
| 401 |
|
| 402 |
init_cltx(); |
| 403 |
|
| 404 |
reset_deassert(PRCI_DEVICES_RESET_CLTX_RST); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
gpio_set_direction(GEMGXL_RST, GPIO_OUTPUT); |
| 409 |
gpio_set(GEMGXL_RST, 1); |
| 410 |
|
| 411 |
udelay(1); |
| 412 |
|
| 413 |
|
| 414 |
gpio_set(GEMGXL_RST, 0); |
| 415 |
udelay(1); |
| 416 |
gpio_set(GEMGXL_RST, 1); |
| 417 |
mdelay(15); |
| 418 |
|
| 419 |
init_gemgxlclk(); |
| 420 |
|
| 421 |
|
| 422 |
reset_deassert(PRCI_DEVICES_RESET_GEMGXL_RST); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
int clock_get_pclk(void) |
| 427 |
{ |
| 428 |
u64 pclk = FU740_HFCLK_FREQ; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if ((read32(&prci->prci_plls) & PRCI_PLLS_HFPCLKPLL) && |
| 434 |
(read32(&prci->hfpclk_plloutdiv) & PRCI_HFPCLK_PLLOUTDIV_MASK)) { |
| 435 |
int hfpclk_pllcfg = read32(&prci->hfpclk_pllcfg); |
| 436 |
int divr = (hfpclk_pllcfg & PRCI_PLLCFG_DIVR_MASK) >> PRCI_PLLCFG_DIVR_SHIFT; |
| 437 |
int divf = (hfpclk_pllcfg & PRCI_PLLCFG_DIVF_MASK) >> PRCI_PLLCFG_DIVF_SHIFT; |
| 438 |
int divq = (hfpclk_pllcfg & PRCI_PLLCFG_DIVQ_MASK) >> PRCI_PLLCFG_DIVQ_SHIFT; |
| 439 |
pclk /= (divr + 1); |
| 440 |
pclk *= (2 * (divf + 1)); |
| 441 |
pclk /= (1 << divq); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
return pclk / (read32(&prci->hfpclk_div_reg) + 2); |
| 446 |
} |