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