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