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