1*94c73a8fSJosh Boyer /* 2*94c73a8fSJosh Boyer * Old U-boot compatibility for Acadia 3*94c73a8fSJosh Boyer * 4*94c73a8fSJosh Boyer * Author: Josh Boyer <jwboyer@linux.vnet.ibm.com> 5*94c73a8fSJosh Boyer * 6*94c73a8fSJosh Boyer * Copyright 2008 IBM Corporation 7*94c73a8fSJosh Boyer * 8*94c73a8fSJosh Boyer * This program is free software; you can redistribute it and/or modify it 9*94c73a8fSJosh Boyer * under the terms of the GNU General Public License version 2 as published 10*94c73a8fSJosh Boyer * by the Free Software Foundation. 11*94c73a8fSJosh Boyer */ 12*94c73a8fSJosh Boyer 13*94c73a8fSJosh Boyer #include "ops.h" 14*94c73a8fSJosh Boyer #include "io.h" 15*94c73a8fSJosh Boyer #include "dcr.h" 16*94c73a8fSJosh Boyer #include "stdio.h" 17*94c73a8fSJosh Boyer #include "4xx.h" 18*94c73a8fSJosh Boyer #include "44x.h" 19*94c73a8fSJosh Boyer #include "cuboot.h" 20*94c73a8fSJosh Boyer 21*94c73a8fSJosh Boyer #define TARGET_4xx 22*94c73a8fSJosh Boyer #include "ppcboot.h" 23*94c73a8fSJosh Boyer 24*94c73a8fSJosh Boyer static bd_t bd; 25*94c73a8fSJosh Boyer 26*94c73a8fSJosh Boyer #define CPR_PERD0_SPIDV_MASK 0x000F0000 /* SPI Clock Divider */ 27*94c73a8fSJosh Boyer 28*94c73a8fSJosh Boyer #define PLLC_SRC_MASK 0x20000000 /* PLL feedback source */ 29*94c73a8fSJosh Boyer 30*94c73a8fSJosh Boyer #define PLLD_FBDV_MASK 0x1F000000 /* PLL feedback divider value */ 31*94c73a8fSJosh Boyer #define PLLD_FWDVA_MASK 0x000F0000 /* PLL forward divider A value */ 32*94c73a8fSJosh Boyer #define PLLD_FWDVB_MASK 0x00000700 /* PLL forward divider B value */ 33*94c73a8fSJosh Boyer 34*94c73a8fSJosh Boyer #define PRIMAD_CPUDV_MASK 0x0F000000 /* CPU Clock Divisor Mask */ 35*94c73a8fSJosh Boyer #define PRIMAD_PLBDV_MASK 0x000F0000 /* PLB Clock Divisor Mask */ 36*94c73a8fSJosh Boyer #define PRIMAD_OPBDV_MASK 0x00000F00 /* OPB Clock Divisor Mask */ 37*94c73a8fSJosh Boyer #define PRIMAD_EBCDV_MASK 0x0000000F /* EBC Clock Divisor Mask */ 38*94c73a8fSJosh Boyer 39*94c73a8fSJosh Boyer #define PERD0_PWMDV_MASK 0xFF000000 /* PWM Divider Mask */ 40*94c73a8fSJosh Boyer #define PERD0_SPIDV_MASK 0x000F0000 /* SPI Divider Mask */ 41*94c73a8fSJosh Boyer #define PERD0_U0DV_MASK 0x0000FF00 /* UART 0 Divider Mask */ 42*94c73a8fSJosh Boyer #define PERD0_U1DV_MASK 0x000000FF /* UART 1 Divider Mask */ 43*94c73a8fSJosh Boyer 44*94c73a8fSJosh Boyer static void get_clocks(void) 45*94c73a8fSJosh Boyer { 46*94c73a8fSJosh Boyer unsigned long sysclk, cpr_plld, cpr_pllc, cpr_primad, plloutb, i; 47*94c73a8fSJosh Boyer unsigned long pllFwdDiv, pllFwdDivB, pllFbkDiv, pllPlbDiv, pllExtBusDiv; 48*94c73a8fSJosh Boyer unsigned long pllOpbDiv, freqEBC, freqUART, freqOPB; 49*94c73a8fSJosh Boyer unsigned long div; /* total divisor udiv * bdiv */ 50*94c73a8fSJosh Boyer unsigned long umin; /* minimum udiv */ 51*94c73a8fSJosh Boyer unsigned short diff; /* smallest diff */ 52*94c73a8fSJosh Boyer unsigned long udiv; /* best udiv */ 53*94c73a8fSJosh Boyer unsigned short idiff; /* current diff */ 54*94c73a8fSJosh Boyer unsigned short ibdiv; /* current bdiv */ 55*94c73a8fSJosh Boyer unsigned long est; /* current estimate */ 56*94c73a8fSJosh Boyer unsigned long baud; 57*94c73a8fSJosh Boyer void *np; 58*94c73a8fSJosh Boyer 59*94c73a8fSJosh Boyer /* read the sysclk value from the CPLD */ 60*94c73a8fSJosh Boyer sysclk = (in_8((unsigned char *)0x80000000) == 0xc) ? 66666666 : 33333000; 61*94c73a8fSJosh Boyer 62*94c73a8fSJosh Boyer /* 63*94c73a8fSJosh Boyer * Read PLL Mode registers 64*94c73a8fSJosh Boyer */ 65*94c73a8fSJosh Boyer cpr_plld = CPR0_READ(DCRN_CPR0_PLLD); 66*94c73a8fSJosh Boyer cpr_pllc = CPR0_READ(DCRN_CPR0_PLLC); 67*94c73a8fSJosh Boyer 68*94c73a8fSJosh Boyer /* 69*94c73a8fSJosh Boyer * Determine forward divider A 70*94c73a8fSJosh Boyer */ 71*94c73a8fSJosh Boyer pllFwdDiv = ((cpr_plld & PLLD_FWDVA_MASK) >> 16); 72*94c73a8fSJosh Boyer 73*94c73a8fSJosh Boyer /* 74*94c73a8fSJosh Boyer * Determine forward divider B 75*94c73a8fSJosh Boyer */ 76*94c73a8fSJosh Boyer pllFwdDivB = ((cpr_plld & PLLD_FWDVB_MASK) >> 8); 77*94c73a8fSJosh Boyer if (pllFwdDivB == 0) 78*94c73a8fSJosh Boyer pllFwdDivB = 8; 79*94c73a8fSJosh Boyer 80*94c73a8fSJosh Boyer /* 81*94c73a8fSJosh Boyer * Determine FBK_DIV. 82*94c73a8fSJosh Boyer */ 83*94c73a8fSJosh Boyer pllFbkDiv = ((cpr_plld & PLLD_FBDV_MASK) >> 24); 84*94c73a8fSJosh Boyer if (pllFbkDiv == 0) 85*94c73a8fSJosh Boyer pllFbkDiv = 256; 86*94c73a8fSJosh Boyer 87*94c73a8fSJosh Boyer /* 88*94c73a8fSJosh Boyer * Read CPR_PRIMAD register 89*94c73a8fSJosh Boyer */ 90*94c73a8fSJosh Boyer cpr_primad = CPR0_READ(DCRN_CPR0_PRIMAD); 91*94c73a8fSJosh Boyer 92*94c73a8fSJosh Boyer /* 93*94c73a8fSJosh Boyer * Determine PLB_DIV. 94*94c73a8fSJosh Boyer */ 95*94c73a8fSJosh Boyer pllPlbDiv = ((cpr_primad & PRIMAD_PLBDV_MASK) >> 16); 96*94c73a8fSJosh Boyer if (pllPlbDiv == 0) 97*94c73a8fSJosh Boyer pllPlbDiv = 16; 98*94c73a8fSJosh Boyer 99*94c73a8fSJosh Boyer /* 100*94c73a8fSJosh Boyer * Determine EXTBUS_DIV. 101*94c73a8fSJosh Boyer */ 102*94c73a8fSJosh Boyer pllExtBusDiv = (cpr_primad & PRIMAD_EBCDV_MASK); 103*94c73a8fSJosh Boyer if (pllExtBusDiv == 0) 104*94c73a8fSJosh Boyer pllExtBusDiv = 16; 105*94c73a8fSJosh Boyer 106*94c73a8fSJosh Boyer /* 107*94c73a8fSJosh Boyer * Determine OPB_DIV. 108*94c73a8fSJosh Boyer */ 109*94c73a8fSJosh Boyer pllOpbDiv = ((cpr_primad & PRIMAD_OPBDV_MASK) >> 8); 110*94c73a8fSJosh Boyer if (pllOpbDiv == 0) 111*94c73a8fSJosh Boyer pllOpbDiv = 16; 112*94c73a8fSJosh Boyer 113*94c73a8fSJosh Boyer /* There is a bug in U-Boot that prevents us from using 114*94c73a8fSJosh Boyer * bd.bi_opbfreq because U-Boot doesn't populate it for 115*94c73a8fSJosh Boyer * 405EZ. We get to calculate it, yay! 116*94c73a8fSJosh Boyer */ 117*94c73a8fSJosh Boyer freqOPB = (sysclk *pllFbkDiv) /pllOpbDiv; 118*94c73a8fSJosh Boyer 119*94c73a8fSJosh Boyer freqEBC = (sysclk * pllFbkDiv) / pllExtBusDiv; 120*94c73a8fSJosh Boyer 121*94c73a8fSJosh Boyer plloutb = ((sysclk * ((cpr_pllc & PLLC_SRC_MASK) ? 122*94c73a8fSJosh Boyer pllFwdDivB : pllFwdDiv) * 123*94c73a8fSJosh Boyer pllFbkDiv) / pllFwdDivB); 124*94c73a8fSJosh Boyer 125*94c73a8fSJosh Boyer np = find_node_by_alias("serial0"); 126*94c73a8fSJosh Boyer if (getprop(np, "current-speed", &baud, sizeof(baud)) != sizeof(baud)) 127*94c73a8fSJosh Boyer fatal("no current-speed property\n\r"); 128*94c73a8fSJosh Boyer 129*94c73a8fSJosh Boyer udiv = 256; /* Assume lowest possible serial clk */ 130*94c73a8fSJosh Boyer div = plloutb / (16 * baud); /* total divisor */ 131*94c73a8fSJosh Boyer umin = (plloutb / freqOPB) << 1; /* 2 x OPB divisor */ 132*94c73a8fSJosh Boyer diff = 256; /* highest possible */ 133*94c73a8fSJosh Boyer 134*94c73a8fSJosh Boyer /* i is the test udiv value -- start with the largest 135*94c73a8fSJosh Boyer * possible (256) to minimize serial clock and constrain 136*94c73a8fSJosh Boyer * search to umin. 137*94c73a8fSJosh Boyer */ 138*94c73a8fSJosh Boyer for (i = 256; i > umin; i--) { 139*94c73a8fSJosh Boyer ibdiv = div / i; 140*94c73a8fSJosh Boyer est = i * ibdiv; 141*94c73a8fSJosh Boyer idiff = (est > div) ? (est-div) : (div-est); 142*94c73a8fSJosh Boyer if (idiff == 0) { 143*94c73a8fSJosh Boyer udiv = i; 144*94c73a8fSJosh Boyer break; /* can't do better */ 145*94c73a8fSJosh Boyer } else if (idiff < diff) { 146*94c73a8fSJosh Boyer udiv = i; /* best so far */ 147*94c73a8fSJosh Boyer diff = idiff; /* update lowest diff*/ 148*94c73a8fSJosh Boyer } 149*94c73a8fSJosh Boyer } 150*94c73a8fSJosh Boyer freqUART = plloutb / udiv; 151*94c73a8fSJosh Boyer 152*94c73a8fSJosh Boyer dt_fixup_cpu_clocks(bd.bi_procfreq, bd.bi_intfreq, bd.bi_plb_busfreq); 153*94c73a8fSJosh Boyer dt_fixup_clock("/plb/ebc", freqEBC); 154*94c73a8fSJosh Boyer dt_fixup_clock("/plb/opb", freqOPB); 155*94c73a8fSJosh Boyer dt_fixup_clock("/plb/opb/serial@ef600300", freqUART); 156*94c73a8fSJosh Boyer dt_fixup_clock("/plb/opb/serial@ef600400", freqUART); 157*94c73a8fSJosh Boyer } 158*94c73a8fSJosh Boyer 159*94c73a8fSJosh Boyer static void acadia_fixups(void) 160*94c73a8fSJosh Boyer { 161*94c73a8fSJosh Boyer dt_fixup_memory(bd.bi_memstart, bd.bi_memsize); 162*94c73a8fSJosh Boyer get_clocks(); 163*94c73a8fSJosh Boyer dt_fixup_mac_address_by_alias("ethernet0", bd.bi_enetaddr); 164*94c73a8fSJosh Boyer } 165*94c73a8fSJosh Boyer 166*94c73a8fSJosh Boyer void platform_init(unsigned long r3, unsigned long r4, unsigned long r5, 167*94c73a8fSJosh Boyer unsigned long r6, unsigned long r7) 168*94c73a8fSJosh Boyer { 169*94c73a8fSJosh Boyer CUBOOT_INIT(); 170*94c73a8fSJosh Boyer platform_ops.fixups = acadia_fixups; 171*94c73a8fSJosh Boyer platform_ops.exit = ibm40x_dbcr_reset; 172*94c73a8fSJosh Boyer fdt_init(_dtb_start); 173*94c73a8fSJosh Boyer serial_console_init(); 174*94c73a8fSJosh Boyer } 175