Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_board.c revision 1.6
      1 /*	$NetBSD: bcm53xx_board.c,v 1.6 2012/10/17 20:18:55 matt Exp $	*/
      2 /*-
      3  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Matt Thomas of 3am Software Foundry.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "opt_broadcom.h"
     32 
     33 #define	_ARM32_BUS_DMA_PRIVATE
     34 
     35 #include <sys/cdefs.h>
     36 
     37 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_board.c,v 1.6 2012/10/17 20:18:55 matt Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/bus.h>
     41 #include <sys/cpu.h>
     42 #include <sys/device.h>
     43 
     44 #include <prop/proplib.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_ether.h>
     48 
     49 #define CRU_PRIVATE
     50 #define DDR_PRIVATE
     51 #define DMU_PRIVATE
     52 #define ARMCORE_PRIVATE
     53 #define SRAB_PRIVATE
     54 
     55 #include <arm/cortex/a9tmr_var.h>
     56 #include <arm/cortex/pl310_var.h>
     57 #include <arm/mainbus/mainbus.h>
     58 
     59 #include <arm/broadcom/bcm53xx_reg.h>
     60 #include <arm/broadcom/bcm53xx_var.h>
     61 
     62 bus_space_tag_t bcm53xx_ioreg_bst = &bcmgen_bs_tag;
     63 bus_space_handle_t bcm53xx_ioreg_bsh;
     64 bus_space_tag_t bcm53xx_armcore_bst = &bcmgen_bs_tag;
     65 bus_space_handle_t bcm53xx_armcore_bsh;
     66 
     67 static struct cpu_softc cpu_softc;
     68 static struct bcm53xx_clock_info clk_info;
     69 
     70 struct arm32_dma_range bcm53xx_dma_ranges[2];
     71 
     72 struct arm32_bus_dma_tag bcm53xx_dma_tag = {
     73 	_BUS_DMAMAP_FUNCS,
     74 	_BUS_DMAMEM_FUNCS,
     75 	_BUS_DMATAG_FUNCS,
     76 };
     77 
     78 struct arm32_dma_range bcm53xx_coherent_dma_ranges[2];
     79 
     80 struct arm32_bus_dma_tag bcm53xx_coherent_dma_tag = {
     81 	_BUS_DMAMAP_FUNCS,
     82 	_BUS_DMAMEM_FUNCS,
     83 	_BUS_DMATAG_FUNCS,
     84 };
     85 
     86 #ifdef BCM53XX_CONSOLE_EARLY
     87 #include <dev/ic/ns16550reg.h>
     88 #include <dev/ic/comreg.h>
     89 #include <dev/cons.h>
     90 
     91 static vaddr_t com_base;
     92 
     93 static inline uint32_t
     94 uart_read(bus_size_t o)
     95 {
     96 	return *(volatile uint8_t *)(com_base + o);
     97 }
     98 
     99 static inline void
    100 uart_write(bus_size_t o, uint32_t v)
    101 {
    102 	*(volatile uint8_t *)(com_base + o) = v;
    103 }
    104 
    105 static int
    106 bcm53xx_cngetc(dev_t dv)
    107 {
    108         if ((uart_read(com_lsr) & LSR_RXRDY) == 0)
    109 		return -1;
    110 
    111 	return uart_read(com_data) & 0xff;
    112 }
    113 
    114 static void
    115 bcm53xx_cnputc(dev_t dv, int c)
    116 {
    117 	int timo = 150000;
    118 
    119         while ((uart_read(com_lsr) & LSR_TXRDY) == 0 && --timo > 0)
    120 		;
    121 
    122 	uart_write(com_data, c);
    123 
    124 	timo = 150000;
    125         while ((uart_read(com_lsr) & LSR_TSRE) == 0 && --timo > 0)
    126 		;
    127 }
    128 
    129 static struct consdev bcm53xx_earlycons = {
    130 	.cn_putc = bcm53xx_cnputc,
    131 	.cn_getc = bcm53xx_cngetc,
    132 	.cn_pollc = nullcnpollc,
    133 };
    134 #endif /* BCM53XX_CONSOLE_EARLY */
    135 
    136 psize_t
    137 bcm53xx_memprobe(void)
    138 {
    139 	bus_space_tag_t bst = bcm53xx_ioreg_bst;
    140 	bus_space_handle_t bsh = bcm53xx_ioreg_bsh;
    141 
    142 	/*
    143 	 * First, let's read the magic DDR registers!
    144 	 */
    145 	const uint32_t v01 = bus_space_read_4(bst, bsh, DDR_BASE + DDR_CTL_01);
    146 	const uint32_t v82 = bus_space_read_4(bst, bsh, DDR_BASE + DDR_CTL_82);
    147 	const uint32_t v86 = bus_space_read_4(bst, bsh, DDR_BASE + DDR_CTL_86);
    148 	const uint32_t v87 = bus_space_read_4(bst, bsh, DDR_BASE + DDR_CTL_87);
    149 
    150 	/*
    151 	 * Calculate chip parameters
    152 	 * */
    153 	const u_int rows = __SHIFTOUT(v01, CTL_01_MAX_ROW)
    154 	    - __SHIFTOUT(v82, CTL_82_ROW_DIFF);
    155 	const u_int cols = __SHIFTOUT(v01, CTL_01_MAX_COL)
    156 	    - __SHIFTOUT(v82, CTL_82_COL_DIFF);
    157 	const u_int banks_log2 = 3 - __SHIFTOUT(v82, CTL_82_BANK_DIFF);
    158 
    159 	/*
    160 	 * For each chip select, increase the chip count if if is enabled.
    161 	 */
    162 	const u_int max_chips = __SHIFTOUT(v01, CTL_01_MAX_CHIP_SEL);
    163 	u_int cs_map = __SHIFTOUT(v86, CTL_86_CS_MAP);
    164 	u_int chips = 0;
    165 
    166 	for (u_int i = 0; cs_map != 0 && i < max_chips; i++, cs_map >>= 1) {
    167 		chips += (cs_map & 1);
    168 	}
    169 
    170 	/* get log2(ddr width) */
    171 
    172 	const u_int ddr_width_log2 = (v87 & CTL_87_REDUC) ? 1 : 2;
    173 
    174 	/*
    175 	 * Let's add up all the things that contribute to the size of a chip.
    176 	 */
    177 	const u_int chip_size_log2 = cols + rows + banks_log2 + ddr_width_log2;
    178 
    179 	/*
    180 	 * Now our memory size is simply the number of chip shifted by the
    181 	 * log2(chip_size).
    182 	 */
    183 	return (psize_t) chips << chip_size_log2;
    184 }
    185 
    186 static inline uint32_t
    187 bcm53xx_freq_calc(struct bcm53xx_clock_info *clk,
    188 	uint32_t pdiv, uint32_t ndiv_int, uint32_t ndiv_frac)
    189 {
    190 	if (ndiv_frac == 0 && pdiv == 1)
    191 		return ndiv_int * clk->clk_ref;
    192 
    193 	uint64_t freq64 = ((uint64_t)ndiv_int << 30) + ndiv_frac;
    194 	freq64 *= clk->clk_ref;
    195 	if (pdiv > 1)
    196 		freq64 /= pdiv;
    197 	return (uint32_t) (freq64 >> 30);
    198 }
    199 
    200 static uint32_t
    201 bcm53xx_value_wrap(uint32_t value, uint32_t mask)
    202 {
    203 	/*
    204 	 * n is n except when n is 0 then n = mask + 1.
    205 	 */
    206 	return ((__SHIFTOUT(value, mask) - 1) &  __SHIFTOUT(mask, mask)) + 1;
    207 }
    208 
    209 static void
    210 bcm53xx_genpll_clock_init(struct bcm53xx_clock_info *clk, uint32_t control5,
    211 	uint32_t control6, uint32_t control7)
    212 {
    213 	const uint32_t pdiv = bcm53xx_value_wrap(control6,
    214 	    GENPLL_CONTROL6_PDIV);
    215 	const uint32_t ndiv_int = bcm53xx_value_wrap(control5,
    216 	    GENPLL_CONTROL5_NDIV_INT);
    217 	const uint32_t ndiv_frac = __SHIFTOUT(control5,
    218 	    GENPLL_CONTROL5_NDIV_FRAC);
    219 
    220 	clk->clk_genpll = bcm53xx_freq_calc(clk, pdiv, ndiv_int, ndiv_frac);
    221 
    222 	const uint32_t ch0_mdiv = bcm53xx_value_wrap(control6,
    223 	    GENPLL_CONTROL6_CH0_MDIV);
    224 	const uint32_t ch1_mdiv = bcm53xx_value_wrap(control6,
    225 	    GENPLL_CONTROL6_CH1_MDIV);
    226 	const uint32_t ch2_mdiv = bcm53xx_value_wrap(control6,
    227 	    GENPLL_CONTROL6_CH2_MDIV);
    228 	const uint32_t ch3_mdiv = bcm53xx_value_wrap(control7,
    229 	    GENPLL_CONTROL7_CH3_MDIV);
    230 
    231 	clk->clk_mac = clk->clk_genpll / ch0_mdiv;	// GENPLL CH0
    232 	clk->clk_robo = clk->clk_genpll / ch1_mdiv;	// GENPLL CH1
    233 	clk->clk_usb2 = clk->clk_genpll / ch2_mdiv;	// GENPLL CH2
    234 	clk->clk_iproc = clk->clk_genpll / ch3_mdiv;	// GENPLL CH3
    235 }
    236 
    237 static void
    238 bcm53xx_lcpll_clock_init(struct bcm53xx_clock_info *clk, uint32_t control1,
    239 	uint32_t control2)
    240 {
    241 	const uint32_t pdiv = bcm53xx_value_wrap(control1,
    242 	    LCPLL_CONTROL1_PDIV);
    243 	const uint32_t ndiv_int = bcm53xx_value_wrap(control1,
    244 	    LCPLL_CONTROL1_NDIV_INT);
    245 	const uint32_t ndiv_frac = __SHIFTOUT(control1,
    246 	    LCPLL_CONTROL1_NDIV_FRAC);
    247 
    248 	clk->clk_lcpll = bcm53xx_freq_calc(clk, pdiv, ndiv_int, ndiv_frac);
    249 
    250 	const uint32_t ch0_mdiv = bcm53xx_value_wrap(control2,
    251 	    LCPLL_CONTROL2_CH0_MDIV);
    252 	const uint32_t ch1_mdiv = bcm53xx_value_wrap(control2,
    253 	    LCPLL_CONTROL2_CH1_MDIV);
    254 	const uint32_t ch2_mdiv = bcm53xx_value_wrap(control2,
    255 	    LCPLL_CONTROL2_CH2_MDIV);
    256 	const uint32_t ch3_mdiv = bcm53xx_value_wrap(control2,
    257 	    LCPLL_CONTROL2_CH3_MDIV);
    258 
    259 	clk->clk_pcie_ref = clk->clk_lcpll / ch0_mdiv;	// LCPLL CH0
    260 	clk->clk_sdio = clk->clk_lcpll / ch1_mdiv;	// LCPLL CH1
    261 	clk->clk_ddr_ref = clk->clk_lcpll / ch2_mdiv;	// LCPLL CH2
    262 	clk->clk_axi = clk->clk_lcpll / ch3_mdiv;	// LCPLL CH3
    263 }
    264 
    265 static void
    266 bcm53xx_usb_clock_init(struct bcm53xx_clock_info *clk, uint32_t usb2_control)
    267 {
    268 	const uint32_t pdiv = bcm53xx_value_wrap(usb2_control,
    269 	    USB2_CONTROL_PDIV);
    270 	const uint32_t ndiv = bcm53xx_value_wrap(usb2_control,
    271 	    USB2_CONTROL_NDIV_INT);
    272 
    273 	uint32_t usb_ref = (clk->clk_usb2 / pdiv) * ndiv;
    274 	if (usb_ref != USB2_REF_CLK) {
    275 		/*
    276 		 * USB Reference Clock isn't 1.92GHz.  So we need to modify
    277 		 * USB2_CONTROL to produce it.
    278 		 */
    279 		uint32_t new_ndiv = (USB2_REF_CLK / clk->clk_usb2) * pdiv;
    280 		usb2_control &= ~USB2_CONTROL_NDIV_INT;
    281 		usb2_control |= __SHIFTIN(new_ndiv, USB2_CONTROL_NDIV_INT);
    282 
    283 		// Allow Clocks to be modified
    284 		bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    285 		    CRU_BASE + CRU_CLKSET_KEY, CRU_CLKSET_KEY_MAGIC);
    286 
    287 		// Update USB2 clock generator
    288 		bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    289 		    CRU_BASE + CRU_USB2_CONTROL, usb2_control);
    290 
    291 		// Prevent Clock modification
    292 		bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    293 		    CRU_BASE + CRU_CLKSET_KEY, 0);
    294 
    295 		usb_ref = (clk->clk_usb2 / pdiv) * new_ndiv;
    296 	}
    297 
    298 	clk->clk_usb_ref = usb_ref;
    299 }
    300 
    301 
    302 static void
    303 bcm53xx_clock_init(struct bcm53xx_clock_info *clk)
    304 {
    305 	clk->clk_ref = BCM53XX_REF_CLK;
    306 	clk->clk_sys = 8*clk->clk_ref;
    307 }
    308 
    309 /*
    310  * F(ddr) = ((1 / pdiv) * ndiv * CH2) / (post_div * 2)
    311  */
    312 static void
    313 bcm53xx_get_ddr_freq(struct bcm53xx_clock_info *clk, uint32_t pll_status,
    314     uint32_t pll_dividers)
    315 {
    316 	const bool clocking_4x = (pll_status & PLL_STATUS_CLOCKING_4X) != 0;
    317 	u_int post_div = __SHIFTOUT(pll_dividers, PLL_DIVIDERS_POST_DIV);
    318 	u_int pdiv = __SHIFTOUT(pll_dividers, PLL_DIVIDERS_PDIV);
    319 	u_int ndiv = __SHIFTOUT(pll_dividers, PLL_DIVIDERS_NDIV);
    320 
    321 	pdiv = ((pdiv - (clocking_4x ? 1 : 5)) & 7) + 1;
    322 
    323 	clk->clk_ddr_mhz = __SHIFTOUT(pll_status, PLL_STATUS_MHZ);
    324 	clk->clk_ddr = (clk->clk_ddr_ref / pdiv) * ndiv / (2 + post_div);
    325 }
    326 
    327 /*
    328  * CPU_CLK = (1 / pdiv) * (ndiv_int + (ndiv_frac / 0x40000000)) x F(ref)
    329  */
    330 static void
    331 bcm53xx_get_cpu_freq(struct bcm53xx_clock_info *clk,
    332 	uint32_t pllarma, uint32_t pllarmb, uint32_t policy)
    333 {
    334 	policy = __SHIFTOUT(policy, CLK_POLICY_FREQ_POLICY2);
    335 
    336 	if (policy == CLK_POLICY_REF_CLK) {
    337 		clk->clk_cpu = clk->clk_ref;
    338 		clk->clk_apb = clk->clk_cpu;
    339 		return;
    340 	}
    341 
    342 	if (policy == CLK_POLICY_SYS_CLK) {
    343 		clk->clk_cpu = clk->clk_sys;
    344 		clk->clk_apb = clk->clk_cpu / 4;
    345 		return;
    346 	}
    347 
    348 	const u_int pdiv = bcm53xx_value_wrap(pllarma, CLK_PLLARMA_PDIV);
    349 	const u_int ndiv_int = bcm53xx_value_wrap(pllarma, CLK_PLLARMA_NDIV_INT);
    350 	const u_int ndiv_frac = __SHIFTOUT(pllarmb, CLK_PLLARMB_NDIV_FRAC);
    351 	// const u_int apb_clk_div = __SHIFTOUT(apb_clk_div, CLK_APB_DIV_VALUE)+1;
    352 
    353 	const u_int cpu_div = (policy == CLK_POLICY_ARM_PLL_CH0) ? 4 : 2;
    354 
    355 	clk->clk_cpu = bcm53xx_freq_calc(clk, pdiv, ndiv_int, ndiv_frac) / cpu_div;
    356 	clk->clk_apb = clk->clk_cpu / 4;
    357 }
    358 
    359 struct bcm53xx_chip_state {
    360 	uint32_t bcs_lcpll_control1;
    361 	uint32_t bcs_lcpll_control2;
    362 
    363 	uint32_t bcs_genpll_control5;
    364 	uint32_t bcs_genpll_control6;
    365 	uint32_t bcs_genpll_control7;
    366 
    367 	uint32_t bcs_usb2_control;
    368 
    369 	uint32_t bcs_ddr_phy_ctl_pll_status;
    370 	uint32_t bcs_ddr_phy_ctl_pll_dividers;
    371 
    372 	uint32_t bcs_armcore_clk_policy;
    373 	uint32_t bcs_armcore_clk_pllarma;
    374 	uint32_t bcs_armcore_clk_pllarmb;
    375 };
    376 
    377 static void
    378 bcm53xx_get_chip_ioreg_state(struct bcm53xx_chip_state *bcs,
    379 	bus_space_tag_t bst, bus_space_handle_t bsh)
    380 {
    381 	bcs->bcs_lcpll_control1 = bus_space_read_4(bst, bsh,
    382 	    DMU_BASE + DMU_LCPLL_CONTROL1);
    383 	bcs->bcs_lcpll_control2 = bus_space_read_4(bst, bsh,
    384 	    DMU_BASE + DMU_LCPLL_CONTROL2);
    385 
    386 	bcs->bcs_genpll_control5 = bus_space_read_4(bst, bsh,
    387 	    CRU_BASE + CRU_GENPLL_CONTROL5);
    388 	bcs->bcs_genpll_control6 = bus_space_read_4(bst, bsh,
    389 	    CRU_BASE + CRU_GENPLL_CONTROL6);
    390 	bcs->bcs_genpll_control7 = bus_space_read_4(bst, bsh,
    391 	    CRU_BASE + CRU_GENPLL_CONTROL7);
    392 
    393 	bcs->bcs_usb2_control = bus_space_read_4(bst, bsh,
    394 	    CRU_BASE + CRU_USB2_CONTROL);
    395 
    396 	bcs->bcs_ddr_phy_ctl_pll_status = bus_space_read_4(bst, bsh,
    397 	    DDR_BASE + DDR_PHY_CTL_PLL_STATUS);
    398 	bcs->bcs_ddr_phy_ctl_pll_dividers = bus_space_read_4(bst, bsh,
    399 	    DDR_BASE + DDR_PHY_CTL_PLL_DIVIDERS);
    400 }
    401 
    402 static void
    403 bcm53xx_get_chip_armcore_state(struct bcm53xx_chip_state *bcs,
    404 	bus_space_tag_t bst, bus_space_handle_t bsh)
    405 {
    406 	bcs->bcs_armcore_clk_policy = bus_space_read_4(bst, bsh,
    407 	    ARMCORE_CLK_POLICY_FREQ);
    408 	bcs->bcs_armcore_clk_pllarma = bus_space_read_4(bst, bsh,
    409 	    ARMCORE_CLK_PLLARMA);
    410 	bcs->bcs_armcore_clk_pllarmb = bus_space_read_4(bst, bsh,
    411 	    ARMCORE_CLK_PLLARMB);
    412 }
    413 
    414 void
    415 bcm53xx_cpu_softc_init(struct cpu_info *ci)
    416 {
    417 	struct cpu_softc * const cpu = ci->ci_softc;
    418 
    419 	cpu->cpu_ioreg_bst = bcm53xx_ioreg_bst;
    420 	cpu->cpu_ioreg_bsh = bcm53xx_ioreg_bsh;
    421 
    422 	cpu->cpu_armcore_bst = bcm53xx_armcore_bst;
    423 	cpu->cpu_armcore_bsh = bcm53xx_armcore_bsh;
    424 }
    425 
    426 void
    427 bcm53xx_print_clocks(void)
    428 {
    429 #if defined(VERBOSE_ARM_INIT)
    430 	printf("ref clk =	%u (%#x)\n", clk_info.clk_ref, clk_info.clk_ref);
    431 	printf("sys clk =	%u (%#x)\n", clk_info.clk_sys, clk_info.clk_sys);
    432 	printf("lcpll clk =	%u (%#x)\n", clk_info.clk_lcpll, clk_info.clk_lcpll);
    433 	printf("pcie ref clk =	%u (%#x) [CH0]\n", clk_info.clk_pcie_ref, clk_info.clk_pcie_ref);
    434 	printf("sdio clk =	%u (%#x) [CH1]\n", clk_info.clk_sdio, clk_info.clk_sdio);
    435 	printf("ddr ref clk =	%u (%#x) [CH2]\n", clk_info.clk_ddr_ref, clk_info.clk_ddr_ref);
    436 	printf("axi clk =	%u (%#x) [CH3]\n", clk_info.clk_axi, clk_info.clk_axi);
    437 	printf("genpll clk =	%u (%#x)\n", clk_info.clk_genpll, clk_info.clk_genpll);
    438 	printf("mac clk =	%u (%#x) [CH0]\n", clk_info.clk_mac, clk_info.clk_mac);
    439 	printf("robo clk =	%u (%#x) [CH1]\n", clk_info.clk_robo, clk_info.clk_robo);
    440 	printf("usb2 clk =	%u (%#x) [CH2]\n", clk_info.clk_usb2, clk_info.clk_usb2);
    441 	printf("iproc clk =	%u (%#x) [CH3]\n", clk_info.clk_iproc, clk_info.clk_iproc);
    442 	printf("ddr clk =	%u (%#x)\n", clk_info.clk_ddr, clk_info.clk_ddr);
    443 	printf("ddr mhz =	%u (%#x)\n", clk_info.clk_ddr_mhz, clk_info.clk_ddr_mhz);
    444 	printf("cpu clk =	%u (%#x)\n", clk_info.clk_cpu, clk_info.clk_cpu);
    445 	printf("apb clk =	%u (%#x)\n", clk_info.clk_apb, clk_info.clk_apb);
    446 	printf("usb ref clk =	%u (%#x)\n", clk_info.clk_usb_ref, clk_info.clk_usb_ref);
    447 #endif
    448 }
    449 
    450 void
    451 bcm53xx_bootstrap(vaddr_t iobase)
    452 {
    453 	struct bcm53xx_chip_state bcs;
    454 	int error;
    455 
    456 #ifdef BCM53XX_CONSOLE_EARLY
    457 	com_base = iobase + CCA_UART0_BASE;
    458 	cn_tab = &bcm53xx_earlycons;
    459 #endif
    460 
    461 	bcm53xx_ioreg_bsh = (bus_space_handle_t) iobase;
    462 	error = bus_space_map(bcm53xx_ioreg_bst, BCM53XX_IOREG_PBASE,
    463 	    BCM53XX_IOREG_SIZE, 0, &bcm53xx_ioreg_bsh);
    464 	if (error)
    465 		panic("%s: failed to map BCM53xx %s registers: %d",
    466 		    __func__, "io", error);
    467 
    468 	bcm53xx_armcore_bsh = (bus_space_handle_t) iobase + BCM53XX_IOREG_SIZE;
    469 	error = bus_space_map(bcm53xx_armcore_bst, BCM53XX_ARMCORE_PBASE,
    470 	    BCM53XX_ARMCORE_SIZE, 0, &bcm53xx_armcore_bsh);
    471 	if (error)
    472 		panic("%s: failed to map BCM53xx %s registers: %d",
    473 		    __func__, "armcore", error);
    474 
    475 	curcpu()->ci_softc = &cpu_softc;
    476 
    477 	bcm53xx_get_chip_ioreg_state(&bcs, bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh);
    478 	bcm53xx_get_chip_armcore_state(&bcs, bcm53xx_armcore_bst, bcm53xx_armcore_bsh);
    479 
    480 	struct bcm53xx_clock_info * const clk = &clk_info;
    481 
    482 	bcm53xx_clock_init(clk);
    483 	bcm53xx_lcpll_clock_init(clk, bcs.bcs_lcpll_control1,
    484 	    bcs.bcs_lcpll_control2);
    485 	bcm53xx_genpll_clock_init(clk, bcs.bcs_genpll_control5,
    486 	    bcs.bcs_genpll_control6, bcs.bcs_genpll_control7);
    487 	bcm53xx_usb_clock_init(clk, bcs.bcs_usb2_control);
    488 	bcm53xx_get_ddr_freq(clk, bcs.bcs_ddr_phy_ctl_pll_status,
    489 	    bcs.bcs_ddr_phy_ctl_pll_dividers);
    490 	bcm53xx_get_cpu_freq(clk, bcs.bcs_armcore_clk_pllarma,
    491 	    bcs.bcs_armcore_clk_pllarmb, bcs.bcs_armcore_clk_policy);
    492 
    493 	curcpu()->ci_data.cpu_cc_freq = clk->clk_cpu;
    494 
    495 	arml2cc_init(bcm53xx_armcore_bst, bcm53xx_armcore_bsh, ARMCORE_L2C_BASE);
    496 }
    497 
    498 void
    499 bcm53xx_dma_bootstrap(psize_t memsize)
    500 {
    501 	if (memsize > 256*1024*1024) {
    502 		/*
    503 		 * By setting up two ranges, bus_dmamem_alloc will always
    504 		 * try to allocate from range 0 first resulting in allocations
    505 		 * below 256MB which for PCI and GMAC are coherent.
    506 		 */
    507 		bcm53xx_dma_ranges[0].dr_sysbase = 0x80000000;
    508 		bcm53xx_dma_ranges[0].dr_busbase = 0x80000000;
    509 		bcm53xx_dma_ranges[0].dr_len = 0x10000000;
    510 		bcm53xx_dma_ranges[1].dr_sysbase = 0x90000000;
    511 		bcm53xx_dma_ranges[1].dr_busbase = 0x90000000;
    512 		bcm53xx_dma_ranges[1].dr_len = memsize - 0x10000000;
    513 		bcm53xx_dma_tag._ranges = bcm53xx_dma_ranges;
    514 		bcm53xx_dma_tag._nranges = __arraycount(bcm53xx_dma_ranges);
    515 		bcm53xx_coherent_dma_ranges[0] = bcm53xx_dma_ranges[0];
    516 		bcm53xx_coherent_dma_ranges[0].dr_flags = _BUS_DMAMAP_COHERENT;
    517 		bcm53xx_coherent_dma_ranges[1] = bcm53xx_dma_ranges[1];
    518 		bcm53xx_coherent_dma_tag._ranges = bcm53xx_coherent_dma_ranges;
    519 		bcm53xx_coherent_dma_tag._nranges =
    520 		    __arraycount(bcm53xx_coherent_dma_ranges);
    521 	} else {
    522 		bcm53xx_coherent_dma_ranges[0].dr_sysbase = 0x80000000;
    523 		bcm53xx_coherent_dma_ranges[0].dr_busbase = 0x80000000;
    524 		bcm53xx_coherent_dma_ranges[0].dr_len = memsize;
    525 		bcm53xx_coherent_dma_ranges[0].dr_flags = _BUS_DMAMAP_COHERENT;
    526 		bcm53xx_coherent_dma_tag._ranges = bcm53xx_coherent_dma_ranges;
    527 		bcm53xx_coherent_dma_tag._nranges = 1;
    528 	}
    529 }
    530 
    531 #ifdef MULTIPROCESSOR
    532 void
    533 bcm53xx_cpu_hatch(struct cpu_info *ci)
    534 {
    535 	a9tmr_init_cpu_clock(ci);
    536 }
    537 #endif
    538 
    539 void
    540 bcm53xx_device_register(device_t self, void *aux)
    541 {
    542 	prop_dictionary_t dict = device_properties(self);
    543 
    544 	if (device_is_a(self, "armperiph")
    545 	    && device_is_a(device_parent(self), "mainbus")) {
    546 		/*
    547 		 * XXX KLUDGE ALERT XXX
    548 		 * The iot mainbus supplies is completely wrong since it scales
    549 		 * addresses by 2.  The simpliest remedy is to replace with our
    550 		 * bus space used for the armcore regisers (which armperiph uses).
    551 		 */
    552 		struct mainbus_attach_args * const mb = aux;
    553 		mb->mb_iot = bcm53xx_armcore_bst;
    554 		return;
    555 	}
    556 
    557 	/*
    558 	 * We need to tell the A9 Global/Watchdog Timer
    559 	 * what frequency it runs at.
    560 	 */
    561 	if (device_is_a(self, "a9tmr") || device_is_a(self, "a9wdt")) {
    562 		/*
    563 		 * This clock always runs at (arm_clk div 2) and only goes
    564 		 * to timers that are part of the A9 MP core subsystem.
    565 		 */
    566                 prop_dictionary_set_uint32(dict, "frequency",
    567 		    clk_info.clk_cpu / 2);
    568 		return;
    569 	}
    570 
    571 	if (device_is_a(self, "bcmeth")) {
    572 		const struct bcmccb_attach_args * const ccbaa = aux;
    573 		const uint8_t enaddr[ETHER_ADDR_LEN] = {
    574 			0x00, 0x01, 0x02, 0x03, 0x04,
    575 			0x05 + 2 * ccbaa->ccbaa_loc.loc_port,
    576 		};
    577 		prop_data_t pd = prop_data_create_data(enaddr, ETHER_ADDR_LEN);
    578 		KASSERT(pd != NULL);
    579 		if (prop_dictionary_set(device_properties(self), "mac-address", pd) == false) {
    580 			printf("WARNING: Unable to set mac-address property for %s\n", device_xname(self));
    581 		}
    582 		prop_object_release(pd);
    583 	}
    584 }
    585 
    586 static kmutex_t srab_lock __cacheline_aligned;
    587 
    588 void
    589 bcm53xx_srab_init(void)
    590 {
    591 	mutex_init(&srab_lock, MUTEX_DEFAULT, IPL_VM);
    592 
    593 	bcm53xx_srab_write_4(0x0079, 0x90);	// reset switch
    594 	for (u_int port = 0; port < 8; port++) {
    595 		/* per port control: no stp */
    596 		bcm53xx_srab_write_4(port, 0x00);
    597 	}
    598 	bcm53xx_srab_write_4(0x0008, 0x1c);	// IMP port (enab UC/MC/BC)
    599 	bcm53xx_srab_write_4(0x000e, 0xbb);	// IMP port force-link 1G
    600 	bcm53xx_srab_write_4(0x005d, 0x7b);	// port5 force-link 1G
    601 	bcm53xx_srab_write_4(0x005f, 0x7b);	// port7 force-link 1G
    602 	bcm53xx_srab_write_4(0x000b, 0x7);	// management mode
    603 	bcm53xx_srab_write_4(0x0203, 0x0);	// disable BRCM tag
    604 	bcm53xx_srab_write_4(0x0200, 0x80);	// enable IMP=port8
    605 }
    606 
    607 static inline void
    608 bcm53xx_srab_busywait(bus_space_tag_t bst, bus_space_handle_t bsh)
    609 {
    610 	while (bus_space_read_4(bst, bsh, SRAB_BASE + SRAB_CMDSTAT) & SRA_GORDYN) {
    611 		delay(10);
    612 	}
    613 }
    614 
    615 uint32_t
    616 bcm53xx_srab_read_4(u_int pageoffset)
    617 {
    618 	bus_space_tag_t bst = bcm53xx_ioreg_bst;
    619 	bus_space_handle_t bsh = bcm53xx_ioreg_bsh;
    620 	uint32_t rv;
    621 
    622 	mutex_spin_enter(&srab_lock);
    623 
    624 	bcm53xx_srab_busywait(bst, bsh);
    625 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_CMDSTAT,
    626 	    __SHIFTIN(pageoffset, SRA_PAGEOFFSET) | SRA_GORDYN);
    627 	bcm53xx_srab_busywait(bst, bsh);
    628 	rv = bus_space_read_4(bst, bsh, SRAB_BASE + SRAB_RDL);
    629 
    630 	mutex_spin_exit(&srab_lock);
    631 	return rv;
    632 }
    633 
    634 uint64_t
    635 bcm53xx_srab_read_8(u_int pageoffset)
    636 {
    637 	bus_space_tag_t bst = bcm53xx_ioreg_bst;
    638 	bus_space_handle_t bsh = bcm53xx_ioreg_bsh;
    639 	uint64_t rv;
    640 
    641 	mutex_spin_enter(&srab_lock);
    642 
    643 	bcm53xx_srab_busywait(bst, bsh);
    644 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_CMDSTAT,
    645 	    __SHIFTIN(pageoffset, SRA_PAGEOFFSET) | SRA_GORDYN);
    646 	bcm53xx_srab_busywait(bst, bsh);
    647 	rv = bus_space_read_4(bst, bsh, SRAB_BASE + SRAB_RDH);
    648 	rv <<= 32;
    649 	rv |= bus_space_read_4(bst, bsh, SRAB_BASE + SRAB_RDL);
    650 
    651 	mutex_spin_exit(&srab_lock);
    652 	return rv;
    653 }
    654 
    655 void
    656 bcm53xx_srab_write_4(u_int pageoffset, uint32_t val)
    657 {
    658 	bus_space_tag_t bst = bcm53xx_ioreg_bst;
    659 	bus_space_handle_t bsh = bcm53xx_ioreg_bsh;
    660 
    661 	mutex_spin_enter(&srab_lock);
    662 
    663 	bcm53xx_srab_busywait(bst, bsh);
    664 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_WDL, val);
    665 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_CMDSTAT,
    666 	    __SHIFTIN(pageoffset, SRA_PAGEOFFSET) | SRA_WRITE | SRA_GORDYN);
    667 	bcm53xx_srab_busywait(bst, bsh);
    668 
    669 	mutex_spin_exit(&srab_lock);
    670 }
    671 
    672 void
    673 bcm53xx_srab_write_8(u_int pageoffset, uint64_t val)
    674 {
    675 	bus_space_tag_t bst = bcm53xx_ioreg_bst;
    676 	bus_space_handle_t bsh = bcm53xx_ioreg_bsh;
    677 
    678 	mutex_spin_enter(&srab_lock);
    679 
    680 	bcm53xx_srab_busywait(bst, bsh);
    681 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_WDL, val);
    682 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_WDH, val >> 32);
    683 	bus_space_write_4(bst, bsh, SRAB_BASE + SRAB_CMDSTAT,
    684 	    __SHIFTIN(pageoffset, SRA_PAGEOFFSET) | SRA_WRITE | SRA_GORDYN);
    685 	bcm53xx_srab_busywait(bst, bsh);
    686 	mutex_spin_exit(&srab_lock);
    687 }
    688