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