2 * Copyright 2007 David Gibson, IBM Corporation.
4 * Based on earlier code:
5 * Matt Porter <mporter@kernel.crashing.org>
6 * Copyright 2002-2005 MontaVista Software Inc.
8 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Copyright (c) 2003, 2004 Zultys Technologies
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
24 static unsigned long chip_11_errata(unsigned long memsize)
30 switch (pvr & 0xf0000ff0) {
43 /* Read the 4xx SDRAM controller to get size of system memory. */
44 void ibm4xx_sdram_fixup_memsize(void)
47 unsigned long memsize, bank_config;
50 for (i = 0; i < ARRAY_SIZE(sdram_bxcr); i++) {
51 bank_config = SDRAM0_READ(sdram_bxcr[i]);
52 if (bank_config & SDRAM_CONFIG_BANK_ENABLE)
53 memsize += SDRAM_CONFIG_BANK_SIZE(bank_config);
56 memsize = chip_11_errata(memsize);
57 dt_fixup_memory(0, memsize);
60 /* Read the 440SPe MQ controller to get size of system memory. */
61 #define DCRN_MQ0_B0BAS 0x40
62 #define DCRN_MQ0_B1BAS 0x41
63 #define DCRN_MQ0_B2BAS 0x42
64 #define DCRN_MQ0_B3BAS 0x43
66 static u64 ibm440spe_decode_bas(u32 bas)
68 u64 base = ((u64)(bas & 0xFFE00000u)) << 2;
70 /* open coded because I'm paranoid about invalid values */
71 switch ((bas >> 4) & 0xFFF) {
75 return base + 0x000800000ull;
77 return base + 0x001000000ull;
79 return base + 0x002000000ull;
81 return base + 0x004000000ull;
83 return base + 0x008000000ull;
85 return base + 0x010000000ull;
87 return base + 0x020000000ull;
89 return base + 0x040000000ull;
91 return base + 0x080000000ull;
93 return base + 0x100000000ull;
95 printf("Memory BAS value 0x%08x unsupported !\n", bas);
99 void ibm440spe_fixup_memsize(void)
101 u64 banktop, memsize = 0;
103 /* Ultimately, we should directly construct the memory node
104 * so we are able to handle holes in the memory address space
106 banktop = ibm440spe_decode_bas(mfdcr(DCRN_MQ0_B0BAS));
107 if (banktop > memsize)
109 banktop = ibm440spe_decode_bas(mfdcr(DCRN_MQ0_B1BAS));
110 if (banktop > memsize)
112 banktop = ibm440spe_decode_bas(mfdcr(DCRN_MQ0_B2BAS));
113 if (banktop > memsize)
115 banktop = ibm440spe_decode_bas(mfdcr(DCRN_MQ0_B3BAS));
116 if (banktop > memsize)
119 dt_fixup_memory(0, memsize);
123 /* 4xx DDR1/2 Denali memory controller support */
133 #define DDR_START 0x1
134 #define DDR_START_SHIFT 0
135 #define DDR_MAX_CS_REG 0x3
136 #define DDR_MAX_CS_REG_SHIFT 24
137 #define DDR_MAX_COL_REG 0xf
138 #define DDR_MAX_COL_REG_SHIFT 16
139 #define DDR_MAX_ROW_REG 0xf
140 #define DDR_MAX_ROW_REG_SHIFT 8
142 #define DDR_DDR2_MODE 0x1
143 #define DDR_DDR2_MODE_SHIFT 0
145 #define DDR_CS_MAP 0x3
146 #define DDR_CS_MAP_SHIFT 8
148 #define DDR_REDUC 0x1
149 #define DDR_REDUC_SHIFT 16
152 #define DDR_APIN_SHIFT 24
154 #define DDR_COL_SZ 0x7
155 #define DDR_COL_SZ_SHIFT 8
156 #define DDR_BANK8 0x1
157 #define DDR_BANK8_SHIFT 0
159 #define DDR_GET_VAL(val, mask, shift) (((val) >> (shift)) & (mask))
161 void ibm4xx_denali_fixup_memsize(void)
163 u32 val, max_cs, max_col, max_row;
164 u32 cs, col, row, bank, dpath;
165 unsigned long memsize;
167 val = SDRAM0_READ(DDR0_02);
168 if (!DDR_GET_VAL(val, DDR_START, DDR_START_SHIFT))
169 fatal("DDR controller is not initialized\n");
171 /* get maximum cs col and row values */
172 max_cs = DDR_GET_VAL(val, DDR_MAX_CS_REG, DDR_MAX_CS_REG_SHIFT);
173 max_col = DDR_GET_VAL(val, DDR_MAX_COL_REG, DDR_MAX_COL_REG_SHIFT);
174 max_row = DDR_GET_VAL(val, DDR_MAX_ROW_REG, DDR_MAX_ROW_REG_SHIFT);
177 val = SDRAM0_READ(DDR0_10);
179 val = DDR_GET_VAL(val, DDR_CS_MAP, DDR_CS_MAP_SHIFT);
188 fatal("No memory installed\n");
190 fatal("DDR wrong CS configuration\n");
192 /* get data path bytes */
193 val = SDRAM0_READ(DDR0_14);
195 if (DDR_GET_VAL(val, DDR_REDUC, DDR_REDUC_SHIFT))
196 dpath = 8; /* 64 bits */
198 dpath = 4; /* 32 bits */
200 /* get address pins (rows) */
201 val = SDRAM0_READ(DDR0_42);
203 row = DDR_GET_VAL(val, DDR_APIN, DDR_APIN_SHIFT);
205 fatal("DDR wrong APIN configuration\n");
208 /* get collomn size and banks */
209 val = SDRAM0_READ(DDR0_43);
211 col = DDR_GET_VAL(val, DDR_COL_SZ, DDR_COL_SZ_SHIFT);
213 fatal("DDR wrong COL configuration\n");
216 if (DDR_GET_VAL(val, DDR_BANK8, DDR_BANK8_SHIFT))
217 bank = 8; /* 8 banks */
219 bank = 4; /* 4 banks */
221 memsize = cs * (1 << (col+row)) * bank * dpath;
222 memsize = chip_11_errata(memsize);
223 dt_fixup_memory(0, memsize);
226 #define SPRN_DBCR0_40X 0x3F2
227 #define SPRN_DBCR0_44X 0x134
228 #define DBCR0_RST_SYSTEM 0x30000000
230 void ibm44x_dbcr_reset(void)
238 : "=&r"(tmp) : "i"(SPRN_DBCR0_44X), "i"(DBCR0_RST_SYSTEM)
243 void ibm40x_dbcr_reset(void)
251 : "=&r"(tmp) : "i"(SPRN_DBCR0_40X), "i"(DBCR0_RST_SYSTEM)
255 #define EMAC_RESET 0x20000000
256 void ibm4xx_quiesce_eth(u32 *emac0, u32 *emac1)
258 /* Quiesce the MAL and EMAC(s) since PIBS/OpenBIOS don't
266 mtdcr(DCRN_MAL0_CFG, MAL_RESET);
267 while (mfdcr(DCRN_MAL0_CFG) & MAL_RESET)
268 ; /* loop until reset takes effect */
271 /* Read 4xx EBC bus bridge registers to get mappings of the peripheral
272 * banks into the OPB address space */
273 void ibm4xx_fixup_ebc_ranges(const char *ebc)
277 u32 ranges[EBC_NUM_BANKS*4];
281 for (i = 0; i < EBC_NUM_BANKS; i++) {
282 mtdcr(DCRN_EBC0_CFGADDR, EBC_BXCR(i));
283 bxcr = mfdcr(DCRN_EBC0_CFGDATA);
285 if ((bxcr & EBC_BXCR_BU) != EBC_BXCR_BU_OFF) {
288 *p++ = bxcr & EBC_BXCR_BAS;
289 *p++ = EBC_BXCR_BANK_SIZE(bxcr);
293 devp = finddevice(ebc);
295 fatal("Couldn't locate EBC node %s\n\r", ebc);
297 setprop(devp, "ranges", ranges, (p - ranges) * sizeof(u32));
300 /* Calculate 440GP clocks */
301 void ibm440gp_fixup_clocks(unsigned int sys_clk, unsigned int ser_clk)
303 u32 sys0 = mfdcr(DCRN_CPC0_SYS0);
304 u32 cr0 = mfdcr(DCRN_CPC0_CR0);
305 u32 cpu, plb, opb, ebc, tb, uart0, uart1, m;
306 u32 opdv = CPC0_SYS0_OPDV(sys0);
307 u32 epdv = CPC0_SYS0_EPDV(sys0);
309 if (sys0 & CPC0_SYS0_BYPASS) {
310 /* Bypass system PLL */
313 if (sys0 & CPC0_SYS0_EXTSL)
315 m = CPC0_SYS0_FWDVB(sys0) * opdv * epdv;
318 m = CPC0_SYS0_FBDV(sys0) * CPC0_SYS0_FWDVA(sys0);
319 cpu = sys_clk * m / CPC0_SYS0_FWDVA(sys0);
320 plb = sys_clk * m / CPC0_SYS0_FWDVB(sys0);
326 /* FIXME: Check if this is for all 440GP, or just Ebony */
327 if ((mfpvr() & 0xf0000fff) == 0x40000440)
328 /* Rev. B 440GP, use external system clock */
331 /* Rev. C 440GP, errata force us to use internal clock */
334 if (cr0 & CPC0_CR0_U0EC)
335 /* External UART clock */
338 /* Internal UART clock */
339 uart0 = plb / CPC0_CR0_UDIV(cr0);
341 if (cr0 & CPC0_CR0_U1EC)
342 /* External UART clock */
345 /* Internal UART clock */
346 uart1 = plb / CPC0_CR0_UDIV(cr0);
348 printf("PPC440GP: SysClk = %dMHz (%x)\n\r",
349 (sys_clk + 500000) / 1000000, sys_clk);
351 dt_fixup_cpu_clocks(cpu, tb, 0);
353 dt_fixup_clock("/plb", plb);
354 dt_fixup_clock("/plb/opb", opb);
355 dt_fixup_clock("/plb/opb/ebc", ebc);
356 dt_fixup_clock("/plb/opb/serial@40000200", uart0);
357 dt_fixup_clock("/plb/opb/serial@40000300", uart1);
360 #define SPRN_CCR1 0x378
362 static inline u32 __fix_zero(u32 v, u32 def)
367 static unsigned int __ibm440eplike_fixup_clocks(unsigned int sys_clk,
368 unsigned int tmr_clk,
369 int per_clk_from_opb)
372 u32 pllc = CPR0_READ(DCRN_CPR0_PLLC);
373 u32 plld = CPR0_READ(DCRN_CPR0_PLLD);
376 u32 fbdv = __fix_zero((plld >> 24) & 0x1f, 32);
377 u32 fwdva = __fix_zero((plld >> 16) & 0xf, 16);
378 u32 fwdvb = __fix_zero((plld >> 8) & 7, 8);
379 u32 lfbdv = __fix_zero(plld & 0x3f, 64);
380 u32 pradv0 = __fix_zero((CPR0_READ(DCRN_CPR0_PRIMAD) >> 24) & 7, 8);
381 u32 prbdv0 = __fix_zero((CPR0_READ(DCRN_CPR0_PRIMBD) >> 24) & 7, 8);
382 u32 opbdv0 = __fix_zero((CPR0_READ(DCRN_CPR0_OPBD) >> 24) & 3, 4);
383 u32 perdv0 = __fix_zero((CPR0_READ(DCRN_CPR0_PERD) >> 24) & 3, 4);
385 /* Input clocks for primary dividers */
388 /* Resulting clocks */
389 u32 cpu, plb, opb, ebc, vco;
392 u32 ccr1, tb = tmr_clk;
394 if (pllc & 0x40000000) {
398 switch ((pllc >> 24) & 7) {
401 m = ((pllc & 0x20000000) ? fwdvb : fwdva) * lfbdv;
409 m = fwdvb * prbdv0 * opbdv0 * perdv0;
412 printf("WARNING ! Invalid PLL feedback source !\n");
421 /* Bypass system PLL */
423 clk_a = clk_b = sys_clk;
426 cpu = clk_a / pradv0;
427 plb = clk_b / prbdv0;
429 ebc = (per_clk_from_opb ? opb : plb) / perdv0;
431 /* Figure out timebase. Either CPU or default TmrClk */
432 ccr1 = mfspr(SPRN_CCR1);
434 /* If passed a 0 tmr_clk, force CPU clock */
437 mtspr(SPRN_CCR1, ccr1);
439 if ((ccr1 & 0x0080) == 0)
442 dt_fixup_cpu_clocks(cpu, tb, 0);
443 dt_fixup_clock("/plb", plb);
444 dt_fixup_clock("/plb/opb", opb);
445 dt_fixup_clock("/plb/opb/ebc", ebc);
450 static void eplike_fixup_uart_clk(int index, const char *path,
451 unsigned int ser_clk,
452 unsigned int plb_clk)
459 sdr = SDR0_READ(DCRN_SDR0_UART0);
462 sdr = SDR0_READ(DCRN_SDR0_UART1);
465 sdr = SDR0_READ(DCRN_SDR0_UART2);
468 sdr = SDR0_READ(DCRN_SDR0_UART3);
474 if (sdr & 0x00800000u)
477 clock = plb_clk / __fix_zero(sdr & 0xff, 256);
479 dt_fixup_clock(path, clock);
482 void ibm440ep_fixup_clocks(unsigned int sys_clk,
483 unsigned int ser_clk,
484 unsigned int tmr_clk)
486 unsigned int plb_clk = __ibm440eplike_fixup_clocks(sys_clk, tmr_clk, 0);
488 /* serial clocks beed fixup based on int/ext */
489 eplike_fixup_uart_clk(0, "/plb/opb/serial@ef600300", ser_clk, plb_clk);
490 eplike_fixup_uart_clk(1, "/plb/opb/serial@ef600400", ser_clk, plb_clk);
491 eplike_fixup_uart_clk(2, "/plb/opb/serial@ef600500", ser_clk, plb_clk);
492 eplike_fixup_uart_clk(3, "/plb/opb/serial@ef600600", ser_clk, plb_clk);
495 void ibm440gx_fixup_clocks(unsigned int sys_clk,
496 unsigned int ser_clk,
497 unsigned int tmr_clk)
499 unsigned int plb_clk = __ibm440eplike_fixup_clocks(sys_clk, tmr_clk, 1);
501 /* serial clocks beed fixup based on int/ext */
502 eplike_fixup_uart_clk(0, "/plb/opb/serial@40000200", ser_clk, plb_clk);
503 eplike_fixup_uart_clk(1, "/plb/opb/serial@40000300", ser_clk, plb_clk);
506 void ibm440spe_fixup_clocks(unsigned int sys_clk,
507 unsigned int ser_clk,
508 unsigned int tmr_clk)
510 unsigned int plb_clk = __ibm440eplike_fixup_clocks(sys_clk, tmr_clk, 1);
512 /* serial clocks beed fixup based on int/ext */
513 eplike_fixup_uart_clk(0, "/plb/opb/serial@10000200", ser_clk, plb_clk);
514 eplike_fixup_uart_clk(1, "/plb/opb/serial@10000300", ser_clk, plb_clk);
515 eplike_fixup_uart_clk(2, "/plb/opb/serial@10000600", ser_clk, plb_clk);
518 void ibm405gp_fixup_clocks(unsigned int sys_clk, unsigned int ser_clk)
520 u32 pllmr = mfdcr(DCRN_CPC0_PLLMR);
521 u32 cpc0_cr0 = mfdcr(DCRN_405_CPC0_CR0);
522 u32 cpc0_cr1 = mfdcr(DCRN_405_CPC0_CR1);
523 u32 psr = mfdcr(DCRN_405_CPC0_PSR);
524 u32 cpu, plb, opb, ebc, tb, uart0, uart1, m;
525 u32 fwdv, fwdvb, fbdv, cbdv, opdv, epdv, ppdv, udiv;
527 fwdv = (8 - ((pllmr & 0xe0000000) >> 29));
528 fbdv = (pllmr & 0x1e000000) >> 25;
531 cbdv = ((pllmr & 0x00060000) >> 17) + 1; /* CPU:PLB */
532 opdv = ((pllmr & 0x00018000) >> 15) + 1; /* PLB:OPB */
533 ppdv = ((pllmr & 0x00001800) >> 13) + 1; /* PLB:PCI */
534 epdv = ((pllmr & 0x00001800) >> 11) + 2; /* PLB:EBC */
535 udiv = ((cpc0_cr0 & 0x3e) >> 1) + 1;
537 /* check for 405GPr */
538 if ((mfpvr() & 0xfffffff0) == (0x50910951 & 0xfffffff0)) {
539 fwdvb = 8 - (pllmr & 0x00000007);
540 if (!(psr & 0x00001000)) /* PCI async mode enable == 0 */
541 if (psr & 0x00000020) /* New mode enable */
542 m = fwdvb * 2 * ppdv;
544 m = fwdvb * cbdv * ppdv;
545 else if (psr & 0x00000020) /* New mode enable */
546 if (psr & 0x00000800) /* PerClk synch mode */
547 m = fwdvb * 2 * epdv;
550 else if (epdv == fbdv)
551 m = fbdv * cbdv * epdv;
553 m = fbdv * fwdvb * cbdv;
555 cpu = sys_clk * m / fwdv;
556 plb = sys_clk * m / (fwdvb * cbdv);
558 m = fwdv * fbdv * cbdv;
559 cpu = sys_clk * m / fwdv;
566 /* uart0 uses the external clock */
572 /* uart1 uses the external clock */
577 /* setup the timebase clock to tick at the cpu frequency */
578 cpc0_cr1 = cpc0_cr1 & ~0x00800000;
579 mtdcr(DCRN_405_CPC0_CR1, cpc0_cr1);
582 dt_fixup_cpu_clocks(cpu, tb, 0);
583 dt_fixup_clock("/plb", plb);
584 dt_fixup_clock("/plb/opb", opb);
585 dt_fixup_clock("/plb/ebc", ebc);
586 dt_fixup_clock("/plb/opb/serial@ef600300", uart0);
587 dt_fixup_clock("/plb/opb/serial@ef600400", uart1);
591 void ibm405ep_fixup_clocks(unsigned int sys_clk)
593 u32 pllmr0 = mfdcr(DCRN_CPC0_PLLMR0);
594 u32 pllmr1 = mfdcr(DCRN_CPC0_PLLMR1);
595 u32 cpc0_ucr = mfdcr(DCRN_CPC0_UCR);
596 u32 cpu, plb, opb, ebc, uart0, uart1;
597 u32 fwdva, fwdvb, fbdv, cbdv, opdv, epdv;
598 u32 pllmr0_ccdv, tb, m;
600 fwdva = 8 - ((pllmr1 & 0x00070000) >> 16);
601 fwdvb = 8 - ((pllmr1 & 0x00007000) >> 12);
602 fbdv = (pllmr1 & 0x00f00000) >> 20;
606 cbdv = ((pllmr0 & 0x00030000) >> 16) + 1; /* CPU:PLB */
607 epdv = ((pllmr0 & 0x00000300) >> 8) + 2; /* PLB:EBC */
608 opdv = ((pllmr0 & 0x00003000) >> 12) + 1; /* PLB:OPB */
612 pllmr0_ccdv = ((pllmr0 & 0x00300000) >> 20) + 1;
613 if (pllmr1 & 0x80000000)
614 cpu = sys_clk * m / (fwdva * pllmr0_ccdv);
616 cpu = sys_clk / pllmr0_ccdv;
622 uart0 = cpu / (cpc0_ucr & 0x0000007f);
623 uart1 = cpu / ((cpc0_ucr & 0x00007f00) >> 8);
625 dt_fixup_cpu_clocks(cpu, tb, 0);
626 dt_fixup_clock("/plb", plb);
627 dt_fixup_clock("/plb/opb", opb);
628 dt_fixup_clock("/plb/ebc", ebc);
629 dt_fixup_clock("/plb/opb/serial@ef600300", uart0);
630 dt_fixup_clock("/plb/opb/serial@ef600400", uart1);