2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Semihalf.
5 * Copyright (c) 1994-1998 Mark Brinicombe.
6 * Copyright (c) 1994 Brini.
9 * This code is derived from software written for Brini by Mark Brinicombe
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Brini.
22 * 4. The name of the company nor the name of the author may be used to
23 * endorse or promote products derived from this software without specific
24 * prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: FreeBSD: //depot/projects/arm/src/sys/arm/at91/kb920x_machdep.c, rev 45
42 #include "opt_platform.h"
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #define _ARM32_BUS_DMA_PRIVATE
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/devmap.h>
52 #include <sys/kernel.h>
57 #include <arm/arm/mpcore_timervar.h>
58 #include <arm/arm/nexusvar.h>
60 #include <machine/bus.h>
61 #include <machine/fdt.h>
62 #include <machine/machdep.h>
63 #include <machine/platform.h>
64 #include <machine/platformvar.h>
65 #include <machine/pte-v6.h>
67 #include <arm/mv/mvreg.h>
68 #include <arm/mv/mvvar.h>
69 #include <arm/mv/mvwin.h>
71 #include <dev/fdt/fdt_common.h>
72 #include <dev/ofw/ofw_bus_subr.h>
74 #include "opt_platform.h"
75 #include "platform_if.h"
77 #if defined(SOC_MV_ARMADA38X)
78 #include "platform_pl310_if.h"
79 #include "armada38x/armada38x_pl310.h"
82 static int platform_mpp_init(void);
83 int armada38x_win_set_iosync_barrier(void);
84 int armada38x_scu_enable(void);
85 int armada38x_open_bootrom_win(void);
86 int armada38x_mbus_optimization(void);
88 static vm_offset_t mv_platform_lastaddr(platform_t plate);
89 static int mv_platform_probe_and_attach(platform_t plate);
90 static void mv_platform_gpio_init(platform_t plate);
91 static void mv_cpu_reset(platform_t plat);
93 static void mv_a38x_platform_late_init(platform_t plate);
94 static int mv_a38x_platform_devmap_init(platform_t plate);
95 static void mv_axp_platform_late_init(platform_t plate);
96 static int mv_axp_platform_devmap_init(platform_t plate);
98 void armadaxp_init_coher_fabric(void);
99 void armadaxp_l2_init(void);
102 void mv_a38x_platform_mp_setmaxid(platform_t plate);
103 void mv_a38x_platform_mp_start_ap(platform_t plate);
104 void mv_axp_platform_mp_setmaxid(platform_t plate);
105 void mv_axp_platform_mp_start_ap(platform_t plate);
108 #define MPP_PIN_MAX 68
109 #define MPP_PIN_CELLS 2
110 #define MPP_PINS_PER_REG 8
111 #define MPP_SEL(pin,func) (((func) & 0xf) << \
112 (((pin) % MPP_PINS_PER_REG) * 4))
115 mv_busdma_tag_init(void *arg __unused)
121 * If this platform has coherent DMA, create the parent DMA tag to pass
122 * down the coherent flag to all busses and devices on the platform,
123 * otherwise return without doing anything. By default create tag
124 * for all A38x-based platforms only.
126 if ((node = OF_finddevice("/")) == -1){
131 if (ofw_bus_node_is_compatible(node, "marvell,armada380") == 0)
134 bus_dma_tag_create(NULL, /* No parent tag */
135 1, 0, /* alignment, bounds */
136 BUS_SPACE_MAXADDR, /* lowaddr */
137 BUS_SPACE_MAXADDR, /* highaddr */
138 NULL, NULL, /* filter, filterarg */
139 BUS_SPACE_MAXSIZE, /* maxsize */
140 BUS_SPACE_UNRESTRICTED, /* nsegments */
141 BUS_SPACE_MAXSIZE, /* maxsegsize */
142 BUS_DMA_COHERENT, /* flags */
143 NULL, NULL, /* lockfunc, lockarg */
146 nexus_set_dma_tag(dmat);
149 SYSINIT(mv_busdma_tag, SI_SUB_DRIVERS, SI_ORDER_ANY, mv_busdma_tag_init, NULL);
152 platform_mpp_init(void)
154 pcell_t pinmap[MPP_PIN_MAX * MPP_PIN_CELLS];
155 int mpp[MPP_PIN_MAX];
156 uint32_t ctrl_val, ctrl_offset;
160 pcell_t pin_cells, *pinmap_ptr, pin_count;
162 int par_addr_cells, par_size_cells;
163 int tuple_size, tuples, rv, pins, i, j;
164 int mpp_pin, mpp_function;
167 * Try to access the MPP node directly i.e. through /aliases/mpp.
169 if ((node = OF_finddevice("mpp")) != -1)
170 if (ofw_bus_node_is_compatible(node, "mrvl,mpp"))
173 * Find the node the long way.
175 if ((node = OF_finddevice("/")) == -1)
178 if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0)
181 if ((node = fdt_find_compatible(node, "mrvl,mpp", 0)) == 0)
183 * No MPP node. Fall back to how MPP got set by the
184 * first-stage loader and try to continue booting.
189 * Process 'reg' prop.
191 if ((rv = fdt_addrsize_cells(OF_parent(node), &par_addr_cells,
192 &par_size_cells)) != 0)
195 tuple_size = sizeof(pcell_t) * (par_addr_cells + par_size_cells);
196 len = OF_getprop(node, "reg", reg, sizeof(reg));
197 tuples = len / tuple_size;
201 rv = fdt_data_to_res(reg, par_addr_cells, par_size_cells,
205 start += fdt_immr_va;
208 * Process 'pin-count' and 'pin-map' props.
210 if (OF_getencprop(node, "pin-count", &pin_count, sizeof(pin_count)) <= 0)
212 if (pin_count > MPP_PIN_MAX)
215 if (OF_getencprop(node, "#pin-cells", &pin_cells, sizeof(pin_cells)) <= 0)
216 pin_cells = MPP_PIN_CELLS;
217 if (pin_cells > MPP_PIN_CELLS)
219 tuple_size = sizeof(pcell_t) * pin_cells;
221 bzero(pinmap, sizeof(pinmap));
222 len = OF_getencprop(node, "pin-map", pinmap, sizeof(pinmap));
225 if (len % tuple_size)
227 pins = len / tuple_size;
228 if (pins > pin_count)
231 * Fill out a "mpp[pin] => function" table. All pins unspecified in
232 * the 'pin-map' property are defaulted to 0 function i.e. GPIO.
234 bzero(mpp, sizeof(mpp));
236 for (i = 0; i < pins; i++) {
237 mpp_pin = *pinmap_ptr;
238 mpp_function = *(pinmap_ptr + 1);
239 mpp[mpp_pin] = mpp_function;
240 pinmap_ptr += pin_cells;
244 * Prepare and program MPP control register values.
247 for (i = 0; i < pin_count;) {
250 for (j = 0; j < MPP_PINS_PER_REG; j++) {
251 if (i + j == pin_count - 1)
253 ctrl_val |= MPP_SEL(i + j, mpp[i + j]);
255 i += MPP_PINS_PER_REG;
256 bus_space_write_4(fdtbus_bs_tag, start, ctrl_offset,
266 mv_platform_lastaddr(platform_t plat)
269 return (fdt_immr_va);
273 mv_platform_probe_and_attach(platform_t plate)
276 if (fdt_immr_addr(MV_BASE) != 0)
282 mv_platform_gpio_init(platform_t plate)
286 * Re-initialise MPP. It is important to call this prior to using
287 * console as the physical connection can be routed via MPP.
289 if (platform_mpp_init() != 0)
294 mv_a38x_platform_late_init(platform_t plate)
298 * Re-initialise decode windows
300 if (mv_check_soc_family() == MV_SOC_UNSUPPORTED)
301 panic("Unsupported SoC family\n");
303 if (soc_decode_win() != 0)
304 printf("WARNING: could not re-initialise decode windows! "
305 "Running with existing settings...\n");
307 /* Configure timers' base frequency */
308 arm_tmr_change_frequency(get_cpu_freq() / 2);
311 * Workaround for Marvell Armada38X family HW issue
312 * between Cortex-A9 CPUs and on-chip devices that may
313 * cause hang on heavy load.
314 * To avoid that, map all registers including PCIe IO
315 * as strongly ordered instead of device memory.
317 pmap_remap_vm_attr(VM_MEMATTR_DEVICE, VM_MEMATTR_SO);
319 /* Set IO Sync Barrier bit for all Mbus devices */
320 if (armada38x_win_set_iosync_barrier() != 0)
321 printf("WARNING: could not map CPU Subsystem registers\n");
322 if (armada38x_mbus_optimization() != 0)
323 printf("WARNING: could not enable mbus optimization\n");
324 if (armada38x_scu_enable() != 0)
325 printf("WARNING: could not enable SCU\n");
327 /* Open window to bootROM memory - needed for SMP */
328 if (armada38x_open_bootrom_win() != 0)
329 printf("WARNING: could not open window to bootROM\n");
334 mv_axp_platform_late_init(platform_t plate)
338 * Re-initialise decode windows
340 if (soc_decode_win() != 0)
341 printf("WARNING: could not re-initialise decode windows! "
342 "Running with existing settings...\n");
343 if ((node = OF_finddevice("/")) == -1)
347 /* For SMP case it should be initialized after APs are booted */
348 armadaxp_init_coher_fabric();
353 #define FDT_DEVMAP_MAX (MV_WIN_CPU_MAX_ARMV7 + 2)
354 static struct devmap_entry fdt_devmap[FDT_DEVMAP_MAX] = {
359 platform_sram_devmap(struct devmap_entry *map)
366 * Construct devmap table with DT-derived config data.
369 mv_a38x_platform_devmap_init(platform_t plat)
371 phandle_t root, child;
375 devmap_register_table(&fdt_devmap[0]);
377 if ((root = OF_finddevice("/")) == -1)
383 fdt_devmap[i].pd_va = fdt_immr_va;
384 fdt_devmap[i].pd_pa = fdt_immr_pa;
385 fdt_devmap[i].pd_size = fdt_immr_size;
391 if (i < FDT_DEVMAP_MAX)
392 if (platform_sram_devmap(&fdt_devmap[i]) == 0)
397 * PCI range(s) and localbus.
399 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
400 if (mv_fdt_is_type(child, "pci") ||
401 mv_fdt_is_type(child, "pciep")) {
403 * Check space: each PCI node will consume 2 devmap
406 if (i + 1 >= FDT_DEVMAP_MAX)
409 if (mv_pci_devmap(child, &fdt_devmap[i], MV_PCI_VA_IO_BASE,
410 MV_PCI_VA_MEM_BASE) != 0)
420 mv_axp_platform_devmap_init(platform_t plate)
422 vm_paddr_t cur_immr_pa;
425 * Acquire SoC registers' base passed by u-boot and fill devmap
426 * accordingly. DTB is going to be modified basing on this data
429 __asm __volatile("mrc p15, 4, %0, c15, c0, 0" : "=r" (cur_immr_pa));
430 cur_immr_pa = (cur_immr_pa << 13) & 0xff000000;
431 if (cur_immr_pa != 0)
432 fdt_immr_pa = cur_immr_pa;
434 mv_a38x_platform_devmap_init(plate);
440 mv_cpu_reset(platform_t plat)
443 write_cpu_misc(RSTOUTn_MASK_ARMV7, SOFT_RST_OUT_EN_ARMV7);
444 write_cpu_misc(SYSTEM_SOFT_RESET_ARMV7, SYS_SOFT_RST_ARMV7);
447 #if defined(SOC_MV_ARMADA38X)
448 static platform_method_t mv_a38x_methods[] = {
449 PLATFORMMETHOD(platform_devmap_init, mv_a38x_platform_devmap_init),
450 PLATFORMMETHOD(platform_cpu_reset, mv_cpu_reset),
451 PLATFORMMETHOD(platform_lastaddr, mv_platform_lastaddr),
452 PLATFORMMETHOD(platform_attach, mv_platform_probe_and_attach),
453 PLATFORMMETHOD(platform_gpio_init, mv_platform_gpio_init),
454 PLATFORMMETHOD(platform_late_init, mv_a38x_platform_late_init),
455 PLATFORMMETHOD(platform_pl310_init, mv_a38x_platform_pl310_init),
456 PLATFORMMETHOD(platform_pl310_write_ctrl, mv_a38x_platform_pl310_write_ctrl),
457 PLATFORMMETHOD(platform_pl310_write_debug, mv_a38x_platform_pl310_write_debug),
459 PLATFORMMETHOD(platform_mp_start_ap, mv_a38x_platform_mp_start_ap),
460 PLATFORMMETHOD(platform_mp_setmaxid, mv_a38x_platform_mp_setmaxid),
465 FDT_PLATFORM_DEF(mv_a38x, "mv_a38x", 0, "marvell,armada380", 100);
468 static platform_method_t mv_axp_methods[] = {
469 PLATFORMMETHOD(platform_devmap_init, mv_axp_platform_devmap_init),
470 PLATFORMMETHOD(platform_cpu_reset, mv_cpu_reset),
471 PLATFORMMETHOD(platform_lastaddr, mv_platform_lastaddr),
472 PLATFORMMETHOD(platform_attach, mv_platform_probe_and_attach),
473 PLATFORMMETHOD(platform_gpio_init, mv_platform_gpio_init),
474 PLATFORMMETHOD(platform_late_init, mv_axp_platform_late_init),
476 PLATFORMMETHOD(platform_mp_start_ap, mv_axp_platform_mp_start_ap),
477 PLATFORMMETHOD(platform_mp_setmaxid, mv_axp_platform_mp_setmaxid),
482 FDT_PLATFORM_DEF(mv_axp, "mv_axp", 0, "marvell,armadaxp", 100);