bcm53xx_machdep.c revision 1.6.2.3 1 1.6.2.3 yamt /* $NetBSD: bcm53xx_machdep.c,v 1.6.2.3 2014/05/22 11:39:39 yamt Exp $ */
2 1.6.2.2 yamt
3 1.6.2.2 yamt /*-
4 1.6.2.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.6.2.2 yamt * All rights reserved.
6 1.6.2.2 yamt *
7 1.6.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.6.2.2 yamt * by Matt Thomas of 3am Software Foundry.
9 1.6.2.2 yamt *
10 1.6.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.6.2.2 yamt * modification, are permitted provided that the following conditions
12 1.6.2.2 yamt * are met:
13 1.6.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.6.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.6.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.6.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.6.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.6.2.2 yamt *
19 1.6.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.6.2.2 yamt */
31 1.6.2.2 yamt
32 1.6.2.2 yamt #define CCA_PRIVATE
33 1.6.2.2 yamt #define IDM_PRIVATE
34 1.6.2.2 yamt
35 1.6.2.2 yamt #include <sys/cdefs.h>
36 1.6.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: bcm53xx_machdep.c,v 1.6.2.3 2014/05/22 11:39:39 yamt Exp $");
37 1.6.2.2 yamt
38 1.6.2.2 yamt #include "opt_evbarm_boardtype.h"
39 1.6.2.2 yamt #include "opt_broadcom.h"
40 1.6.2.2 yamt #include "opt_kgdb.h"
41 1.6.2.2 yamt #include "com.h"
42 1.6.2.2 yamt #include "pci.h"
43 1.6.2.2 yamt #include "bcmrng_ccb.h"
44 1.6.2.2 yamt
45 1.6.2.2 yamt #include <sys/param.h>
46 1.6.2.2 yamt #include <sys/bus.h>
47 1.6.2.2 yamt #include <sys/atomic.h>
48 1.6.2.2 yamt #include <sys/device.h>
49 1.6.2.2 yamt #include <sys/kernel.h>
50 1.6.2.2 yamt #include <sys/reboot.h>
51 1.6.2.2 yamt #include <sys/termios.h>
52 1.6.2.2 yamt
53 1.6.2.2 yamt #include <dev/cons.h>
54 1.6.2.2 yamt
55 1.6.2.2 yamt #include <uvm/uvm_extern.h>
56 1.6.2.2 yamt
57 1.6.2.2 yamt #include <arm/db_machdep.h>
58 1.6.2.2 yamt #include <arm/undefined.h>
59 1.6.2.2 yamt #include <arm/arm32/machdep.h>
60 1.6.2.2 yamt
61 1.6.2.2 yamt #include <machine/autoconf.h>
62 1.6.2.2 yamt #include <machine/bootconfig.h>
63 1.6.2.2 yamt
64 1.6.2.2 yamt #define CCA_PRIVATE
65 1.6.2.2 yamt
66 1.6.2.2 yamt #include <arm/cortex/scu_reg.h>
67 1.6.2.2 yamt #include <arm/broadcom/bcm53xx_var.h>
68 1.6.2.2 yamt
69 1.6.2.2 yamt #include <evbarm/bcm53xx/platform.h>
70 1.6.2.2 yamt
71 1.6.2.2 yamt #if NCOM == 0
72 1.6.2.2 yamt #error missing COM device for console
73 1.6.2.2 yamt #endif
74 1.6.2.2 yamt
75 1.6.2.2 yamt #include <dev/ic/comreg.h>
76 1.6.2.2 yamt #include <dev/ic/comvar.h>
77 1.6.2.2 yamt
78 1.6.2.2 yamt extern int _end[];
79 1.6.2.2 yamt extern int KERNEL_BASE_phys[];
80 1.6.2.2 yamt extern int KERNEL_BASE_virt[];
81 1.6.2.2 yamt
82 1.6.2.2 yamt BootConfig bootconfig;
83 1.6.2.2 yamt static char bootargs[MAX_BOOT_STRING];
84 1.6.2.2 yamt char *boot_args = NULL;
85 1.6.2.2 yamt
86 1.6.2.2 yamt u_int uboot_args[4] = { 0 };
87 1.6.2.2 yamt
88 1.6.2.2 yamt static void bcm53xx_system_reset(void);
89 1.6.2.2 yamt
90 1.6.2.2 yamt /*
91 1.6.2.2 yamt * Macros to translate between physical and virtual for a subset of the
92 1.6.2.2 yamt * kernel address space. *Not* for general use.
93 1.6.2.2 yamt */
94 1.6.2.2 yamt #define KERN_VTOPDIFF ((vaddr_t)KERNEL_BASE_phys - (vaddr_t)KERNEL_BASE_virt)
95 1.6.2.2 yamt #define KERN_VTOPHYS(va) ((paddr_t)((vaddr_t)va + KERN_VTOPDIFF))
96 1.6.2.2 yamt #define KERN_PHYSTOV(pa) ((vaddr_t)((paddr_t)pa - KERN_VTOPDIFF))
97 1.6.2.2 yamt
98 1.6.2.2 yamt #ifndef CONADDR
99 1.6.2.2 yamt #define CONADDR (BCM53XX_IOREG_PBASE + CCA_UART0_BASE)
100 1.6.2.2 yamt #endif
101 1.6.2.2 yamt #ifndef CONSPEED
102 1.6.2.2 yamt #define CONSPEED B115200
103 1.6.2.2 yamt #endif
104 1.6.2.2 yamt #ifndef CONMODE
105 1.6.2.2 yamt #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
106 1.6.2.2 yamt #endif
107 1.6.2.2 yamt
108 1.6.2.2 yamt #if (NCOM > 0)
109 1.6.2.2 yamt static const bus_addr_t comcnaddr = (bus_addr_t)CONADDR;
110 1.6.2.2 yamt
111 1.6.2.2 yamt int comcnspeed = CONSPEED;
112 1.6.2.2 yamt int comcnmode = CONMODE | CLOCAL;
113 1.6.2.2 yamt #endif
114 1.6.2.2 yamt
115 1.6.2.2 yamt #ifdef KGDB
116 1.6.2.2 yamt #include <sys/kgdb.h>
117 1.6.2.2 yamt #endif
118 1.6.2.2 yamt
119 1.6.2.2 yamt /*
120 1.6.2.2 yamt * Static device mappings. These peripheral registers are mapped at
121 1.6.2.2 yamt * fixed virtual addresses very early in initarm() so that we can use
122 1.6.2.2 yamt * them while booting the kernel, and stay at the same address
123 1.6.2.2 yamt * throughout whole kernel's life time.
124 1.6.2.2 yamt *
125 1.6.2.2 yamt * We use this table twice; once with bootstrap page table, and once
126 1.6.2.2 yamt * with kernel's page table which we build up in initarm().
127 1.6.2.2 yamt *
128 1.6.2.2 yamt * Since we map these registers into the bootstrap page table using
129 1.6.2.2 yamt * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
130 1.6.2.2 yamt * registers segment-aligned and segment-rounded in order to avoid
131 1.6.2.2 yamt * using the 2nd page tables.
132 1.6.2.2 yamt */
133 1.6.2.2 yamt
134 1.6.2.2 yamt static const struct pmap_devmap devmap[] = {
135 1.6.2.2 yamt {
136 1.6.2.2 yamt KERNEL_IO_IOREG_VBASE,
137 1.6.2.2 yamt BCM53XX_IOREG_PBASE, /* 0x18000000 */
138 1.6.2.2 yamt BCM53XX_IOREG_SIZE, /* 2MB */
139 1.6.2.2 yamt VM_PROT_READ|VM_PROT_WRITE,
140 1.6.2.2 yamt PTE_NOCACHE,
141 1.6.2.2 yamt },
142 1.6.2.2 yamt {
143 1.6.2.2 yamt KERNEL_IO_ARMCORE_VBASE,
144 1.6.2.2 yamt BCM53XX_ARMCORE_PBASE, /* 0x19000000 */
145 1.6.2.2 yamt BCM53XX_ARMCORE_SIZE, /* 1MB */
146 1.6.2.2 yamt VM_PROT_READ|VM_PROT_WRITE,
147 1.6.2.2 yamt PTE_NOCACHE,
148 1.6.2.2 yamt },
149 1.6.2.2 yamt #if NPCI > 0
150 1.6.2.2 yamt {
151 1.6.2.2 yamt KERNEL_IO_PCIE0_OWIN_VBASE,
152 1.6.2.2 yamt BCM53XX_PCIE0_OWIN_PBASE, /* 0x08000000 */
153 1.6.2.2 yamt BCM53XX_PCIE0_OWIN_SIZE, /* 4MB */
154 1.6.2.2 yamt VM_PROT_READ|VM_PROT_WRITE,
155 1.6.2.2 yamt PTE_NOCACHE,
156 1.6.2.2 yamt },
157 1.6.2.2 yamt {
158 1.6.2.2 yamt KERNEL_IO_PCIE1_OWIN_VBASE,
159 1.6.2.2 yamt BCM53XX_PCIE1_OWIN_PBASE, /* 0x40000000 */
160 1.6.2.2 yamt BCM53XX_PCIE1_OWIN_SIZE, /* 4MB */
161 1.6.2.2 yamt VM_PROT_READ|VM_PROT_WRITE,
162 1.6.2.2 yamt PTE_NOCACHE,
163 1.6.2.2 yamt },
164 1.6.2.2 yamt {
165 1.6.2.2 yamt KERNEL_IO_PCIE2_OWIN_VBASE,
166 1.6.2.2 yamt BCM53XX_PCIE2_OWIN_PBASE, /* 0x48000000 */
167 1.6.2.2 yamt BCM53XX_PCIE2_OWIN_SIZE, /* 4MB */
168 1.6.2.2 yamt VM_PROT_READ|VM_PROT_WRITE,
169 1.6.2.2 yamt PTE_NOCACHE,
170 1.6.2.2 yamt },
171 1.6.2.2 yamt #endif /* NPCI > 0 */
172 1.6.2.2 yamt { 0, 0, 0, 0, 0 }
173 1.6.2.2 yamt };
174 1.6.2.2 yamt
175 1.6.2.2 yamt static const struct boot_physmem bp_first256 = {
176 1.6.2.2 yamt .bp_start = 0x80000000 / NBPG,
177 1.6.2.2 yamt .bp_pages = 0x10000000 / NBPG,
178 1.6.2.2 yamt .bp_freelist = VM_FREELIST_ISADMA,
179 1.6.2.2 yamt .bp_flags = 0,
180 1.6.2.2 yamt };
181 1.6.2.2 yamt
182 1.6.2.2 yamt /*
183 1.6.2.2 yamt * u_int initarm(...)
184 1.6.2.2 yamt *
185 1.6.2.2 yamt * Initial entry point on startup. This gets called before main() is
186 1.6.2.2 yamt * entered.
187 1.6.2.2 yamt * It should be responsible for setting up everything that must be
188 1.6.2.2 yamt * in place when main is called.
189 1.6.2.2 yamt * This includes
190 1.6.2.2 yamt * Taking a copy of the boot configuration structure.
191 1.6.2.2 yamt * Initialising the physical console so characters can be printed.
192 1.6.2.2 yamt * Setting up page tables for the kernel
193 1.6.2.2 yamt */
194 1.6.2.2 yamt u_int
195 1.6.2.2 yamt initarm(void *arg)
196 1.6.2.2 yamt {
197 1.6.2.2 yamt pmap_devmap_register(devmap);
198 1.6.2.2 yamt bcm53xx_bootstrap(KERNEL_IO_IOREG_VBASE);
199 1.6.2.2 yamt
200 1.6.2.2 yamt #ifdef MULTIPROCESSOR
201 1.6.2.2 yamt uint32_t scu_cfg = bus_space_read_4(bcm53xx_armcore_bst, bcm53xx_armcore_bsh,
202 1.6.2.2 yamt ARMCORE_SCU_BASE + SCU_CFG);
203 1.6.2.3 yamt arm_cpu_max = 1 + (scu_cfg & SCU_CFG_CPUMAX);
204 1.6.2.2 yamt membar_producer();
205 1.6.2.2 yamt #endif
206 1.6.2.2 yamt /*
207 1.6.2.2 yamt * Heads up ... Setup the CPU / MMU / TLB functions
208 1.6.2.2 yamt */
209 1.6.2.2 yamt if (set_cpufuncs()) // starts PMC counter
210 1.6.2.2 yamt panic("cpu not recognized!");
211 1.6.2.2 yamt
212 1.6.2.2 yamt cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
213 1.6.2.2 yamt
214 1.6.2.2 yamt consinit();
215 1.6.2.2 yamt
216 1.6.2.2 yamt bcm53xx_cpu_softc_init(curcpu());
217 1.6.2.2 yamt bcm53xx_print_clocks();
218 1.6.2.2 yamt
219 1.6.2.2 yamt #if NBCMRNG_CCB > 0
220 1.6.2.2 yamt /*
221 1.6.2.2 yamt * Start this early since it takes a while to startup up.
222 1.6.2.2 yamt */
223 1.6.2.2 yamt bcm53xx_rng_start(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh);
224 1.6.2.2 yamt #endif
225 1.6.2.2 yamt
226 1.6.2.2 yamt printf("uboot arg = %#x, %#x, %#x, %#x\n",
227 1.6.2.2 yamt uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
228 1.6.2.2 yamt
229 1.6.2.2 yamt /* Talk to the user */
230 1.6.2.2 yamt printf("\nNetBSD/evbarm (" ___STRING(EVBARM_BOARDTYPE) ") booting ...\n");
231 1.6.2.2 yamt
232 1.6.2.2 yamt bootargs[0] = '\0';
233 1.6.2.2 yamt
234 1.6.2.2 yamt #if defined(VERBOSE_INIT_ARM) || 1
235 1.6.2.2 yamt printf("initarm: Configuring system");
236 1.6.2.2 yamt #ifdef MULTIPROCESSOR
237 1.6.2.2 yamt printf(" (%u cpu%s, hatched %#x)",
238 1.6.2.2 yamt arm_cpu_max + 1, arm_cpu_max + 1 ? "s" : "",
239 1.6.2.2 yamt arm_cpu_hatched);
240 1.6.2.2 yamt #endif
241 1.6.2.3 yamt printf(", CLIDR=%010o CTR=%#x PMUSERSR=%#x",
242 1.6.2.3 yamt armreg_clidr_read(), armreg_ctr_read(), armreg_pmuserenr_read());
243 1.6.2.2 yamt printf("\n");
244 1.6.2.2 yamt #endif
245 1.6.2.2 yamt
246 1.6.2.2 yamt psize_t memsize = bcm53xx_memprobe();
247 1.6.2.2 yamt #ifdef MEMSIZE
248 1.6.2.2 yamt if ((memsize >> 20) > MEMSIZE)
249 1.6.2.2 yamt memsize = MEMSIZE*1024*1024;
250 1.6.2.2 yamt #endif
251 1.6.2.2 yamt const bool bigmem_p = (memsize >> 20) > 256;
252 1.6.2.2 yamt
253 1.6.2.2 yamt arm32_bootmem_init(KERN_VTOPHYS(KERNEL_BASE), memsize,
254 1.6.2.2 yamt (paddr_t)KERNEL_BASE_phys);
255 1.6.2.2 yamt
256 1.6.2.2 yamt bcm53xx_dma_bootstrap(memsize);
257 1.6.2.2 yamt
258 1.6.2.2 yamt /*
259 1.6.2.2 yamt * This is going to do all the hard work of setting up the first and
260 1.6.2.2 yamt * and second level page tables. Pages of memory will be allocated
261 1.6.2.2 yamt * and mapped for other structures that are required for system
262 1.6.2.2 yamt * operation. When it returns, physical_freestart and free_pages will
263 1.6.2.2 yamt * have been updated to reflect the allocations that were made. In
264 1.6.2.2 yamt * addition, kernel_l1pt, kernel_pt_table[], systempage, irqstack,
265 1.6.2.2 yamt * abtstack, undstack, kernelstack, msgbufphys will be set to point to
266 1.6.2.2 yamt * the memory that was allocated for them.
267 1.6.2.2 yamt */
268 1.6.2.2 yamt arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0, devmap, true);
269 1.6.2.2 yamt
270 1.6.2.2 yamt cpu_reset_address = bcm53xx_system_reset;
271 1.6.2.2 yamt /* we've a specific device_register routine */
272 1.6.2.2 yamt evbarm_device_register = bcm53xx_device_register;
273 1.6.2.2 yamt if (bigmem_p) {
274 1.6.2.2 yamt /*
275 1.6.2.2 yamt * If we have more than 256MB
276 1.6.2.2 yamt */
277 1.6.2.2 yamt arm_poolpage_vmfreelist = bp_first256.bp_freelist;
278 1.6.2.2 yamt }
279 1.6.2.2 yamt
280 1.6.2.2 yamt /*
281 1.6.2.2 yamt * If we have more than 256MB of RAM, set aside the first 256MB for
282 1.6.2.2 yamt * non-default VM allocations.
283 1.6.2.2 yamt */
284 1.6.2.2 yamt return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
285 1.6.2.2 yamt (bigmem_p ? &bp_first256 : NULL), (bigmem_p ? 1 : 0));
286 1.6.2.2 yamt }
287 1.6.2.2 yamt
288 1.6.2.2 yamt void
289 1.6.2.2 yamt consinit(void)
290 1.6.2.2 yamt {
291 1.6.2.2 yamt static bool consinit_called = false;
292 1.6.2.2 yamt uint32_t v;
293 1.6.2.2 yamt if (consinit_called)
294 1.6.2.2 yamt return;
295 1.6.2.2 yamt
296 1.6.2.2 yamt consinit_called = true;
297 1.6.2.2 yamt
298 1.6.2.2 yamt /*
299 1.6.2.2 yamt * Force UART clock to the reference clock
300 1.6.2.2 yamt */
301 1.6.2.2 yamt v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
302 1.6.2.2 yamt IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT);
303 1.6.2.2 yamt v &= ~IO_CONTROL_DIRECT_UARTCLKSEL;
304 1.6.2.2 yamt bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
305 1.6.2.2 yamt IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT, v);
306 1.6.2.2 yamt
307 1.6.2.2 yamt /*
308 1.6.2.2 yamt * Switch to the reference clock
309 1.6.2.2 yamt */
310 1.6.2.2 yamt v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
311 1.6.2.2 yamt CCA_MISC_BASE + MISC_CORECTL);
312 1.6.2.2 yamt v &= ~CORECTL_UART_CLK_OVERRIDE;
313 1.6.2.2 yamt bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
314 1.6.2.2 yamt CCA_MISC_BASE + MISC_CORECTL, v);
315 1.6.2.2 yamt
316 1.6.2.2 yamt if (comcnattach(bcm53xx_ioreg_bst, comcnaddr, comcnspeed,
317 1.6.2.2 yamt BCM53XX_REF_CLK, COM_TYPE_NORMAL, comcnmode))
318 1.6.2.2 yamt panic("Serial console can not be initialized.");
319 1.6.2.2 yamt }
320 1.6.2.2 yamt
321 1.6.2.2 yamt static void
322 1.6.2.2 yamt bcm53xx_system_reset(void)
323 1.6.2.2 yamt {
324 1.6.2.2 yamt bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
325 1.6.2.2 yamt MISC_WATCHDOG, 1);
326 1.6.2.2 yamt }
327