bcm53xx_machdep.c revision 1.1 1 /* $NetBSD: bcm53xx_machdep.c,v 1.1 2012/09/01 00:15:11 matt 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.1 2012/09/01 00:15:11 matt Exp $");
37
38 #include "opt_evbarm_boardtype.h"
39 #include "opt_broadcom.h"
40 #include "opt_kgdb.h"
41 #include "com.h"
42 #include "bcmrng_ccb.h"
43
44 #include <sys/param.h>
45 #include <sys/bus.h>
46 #include <sys/atomic.h>
47 #include <sys/device.h>
48 #include <sys/kernel.h>
49 #include <sys/reboot.h>
50 #include <sys/termios.h>
51
52 #include <dev/cons.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <arm/db_machdep.h>
57 #include <arm/undefined.h>
58 #include <arm/arm32/machdep.h>
59
60 #include <machine/autoconf.h>
61 #include <machine/bootconfig.h>
62
63 #define CCA_PRIVATE
64
65 #include <arm/cortex/scu_reg.h>
66 #include <arm/broadcom/bcm53xx_var.h>
67
68 #include <evbarm/bcm53xx/platform.h>
69
70 #if NCOM == 0
71 #error missing COM device for console
72 #endif
73
74 #include <dev/ic/comreg.h>
75 #include <dev/ic/comvar.h>
76
77 extern int _end[];
78 extern int KERNEL_BASE_phys[];
79 extern int KERNEL_BASE_virt[];
80
81 BootConfig bootconfig;
82 static char bootargs[MAX_BOOT_STRING];
83 char *boot_args = NULL;
84
85 static void bcm53xx_system_reset(void);
86
87 /*
88 * Macros to translate between physical and virtual for a subset of the
89 * kernel address space. *Not* for general use.
90 */
91 #define KERN_VTOPDIFF ((vaddr_t)KERNEL_BASE_phys - (vaddr_t)KERNEL_BASE_virt)
92 #define KERN_VTOPHYS(va) ((paddr_t)((vaddr_t)va + KERN_VTOPDIFF))
93 #define KERN_PHYSTOV(pa) ((vaddr_t)((paddr_t)pa - KERN_VTOPDIFF))
94
95 #ifndef CONADDR
96 #define CONADDR (BCM53XX_IOREG_PBASE + CCA_UART0_BASE)
97 #endif
98 #ifndef CONSPEED
99 #define CONSPEED B115200
100 #endif
101 #ifndef CONMODE
102 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
103 #endif
104
105 #if (NCOM > 0)
106 static const bus_addr_t comcnaddr = (bus_addr_t)CONADDR;
107
108 int comcnspeed = CONSPEED;
109 int comcnmode = CONMODE | CLOCAL;
110 #endif
111
112 #ifdef KGDB
113 #include <sys/kgdb.h>
114 #endif
115
116 /*
117 * Static device mappings. These peripheral registers are mapped at
118 * fixed virtual addresses very early in initarm() so that we can use
119 * them while booting the kernel, and stay at the same address
120 * throughout whole kernel's life time.
121 *
122 * We use this table twice; once with bootstrap page table, and once
123 * with kernel's page table which we build up in initarm().
124 *
125 * Since we map these registers into the bootstrap page table using
126 * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
127 * registers segment-aligned and segment-rounded in order to avoid
128 * using the 2nd page tables.
129 */
130
131 static const struct pmap_devmap devmap[] = {
132 {
133 KERNEL_IO_VBASE,
134 BCM53XX_IOREG_PBASE, /* 0x18000000 */
135 BCM53XX_IOREG_SIZE, /* 2MB */
136 VM_PROT_READ|VM_PROT_WRITE,
137 PTE_NOCACHE,
138 },
139 {
140 KERNEL_IO_VBASE + BCM53XX_IOREG_SIZE,
141 BCM53XX_ARMCORE_PBASE, /* 0x19000000 */
142 BCM53XX_ARMCORE_SIZE, /* 16MB */
143 VM_PROT_READ|VM_PROT_WRITE,
144 PTE_NOCACHE,
145 },
146 { 0, 0, 0, 0, 0 }
147 };
148
149 /*
150 * u_int initarm(...)
151 *
152 * Initial entry point on startup. This gets called before main() is
153 * entered.
154 * It should be responsible for setting up everything that must be
155 * in place when main is called.
156 * This includes
157 * Taking a copy of the boot configuration structure.
158 * Initialising the physical console so characters can be printed.
159 * Setting up page tables for the kernel
160 */
161 u_int
162 initarm(void *arg)
163 {
164 pmap_devmap_register(devmap);
165 bcm53xx_bootstrap(KERNEL_IO_VBASE);
166
167 #ifdef MULTIPROCESSOR
168 uint32_t scu_cfg = bus_space_read_4(bcm53xx_armcore_bst, bcm53xx_armcore_bsh,
169 ARMCORE_SCU_BASE + SCU_CFG);
170 arm_cpu_max = scu_cfg & SCU_CFG_CPUMAX;
171 membar_producer();
172 #endif
173 /*
174 * Heads up ... Setup the CPU / MMU / TLB functions
175 */
176 if (set_cpufuncs()) // starts PMC counter
177 panic("cpu not recognized!");
178
179 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
180
181 consinit();
182
183 bcm53xx_cpu_softc_init(curcpu());
184 bcm53xx_print_clocks();
185
186 #if NBCMRNG_CCB > 0
187 /*
188 * Start this early since it takes a while to startup up.
189 */
190 bcm53xx_rng_start(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh);
191 #endif
192
193 /* Talk to the user */
194 printf("\nNetBSD/evbarm (" ___STRING(EVBARM_BOARDTYPE) ") booting ...\n");
195
196 bootargs[0] = '\0';
197
198 #if defined(VERBOSE_INIT_ARM) || 1
199 printf("initarm: Configuring system");
200 #ifdef MULTIPROCESSOR
201 printf(" (%u cpu%s, hatched %#x)",
202 arm_cpu_max + 1, arm_cpu_max + 1 ? "s" : "",
203 arm_cpu_hatched);
204 #endif
205 printf("\n");
206 #endif
207
208 arm32_bootmem_init(KERN_VTOPHYS(KERNEL_BASE), bcm53xx_memprobe(),
209 (paddr_t)KERNEL_BASE_phys);
210
211 /*
212 * This is going to do all the hard work of setting up the first and
213 * and second level page tables. Pages of memory will be allocated
214 * and mapped for other structures that are required for system
215 * operation. When it returns, physical_freestart and free_pages will
216 * have been updated to reflect the allocations that were made. In
217 * addition, kernel_l1pt, kernel_pt_table[], systempage, irqstack,
218 * abtstack, undstack, kernelstack, msgbufphys will be set to point to
219 * the memory that was allocated for them.
220 */
221 arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0, devmap, true);
222
223 cpu_reset_address = bcm53xx_system_reset;
224 /* we've a specific device_register routine */
225 evbarm_device_register = bcm53xx_device_register;
226
227 return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
228 }
229
230 void
231 consinit(void)
232 {
233 static bool consinit_called = false;
234 uint32_t v;
235 if (consinit_called)
236 return;
237
238 consinit_called = true;
239
240 /*
241 * Force UART clock to the reference clock
242 */
243 v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
244 IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT);
245 v &= ~IO_CONTROL_DIRECT_UARTCLKSEL;
246 bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
247 IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT, v);
248
249 /*
250 * Switch to the reference clock
251 */
252 v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
253 CCA_MISC_BASE + MISC_CORECTL);
254 v &= ~CORECTL_UART_CLK_OVERRIDE;
255 bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
256 CCA_MISC_BASE + MISC_CORECTL, v);
257
258 if (comcnattach(bcm53xx_ioreg_bst, comcnaddr, comcnspeed,
259 BCM53XX_REF_CLK, COM_TYPE_NORMAL, comcnmode))
260 panic("Serial console can not be initialized.");
261 }
262
263 static void
264 bcm53xx_system_reset(void)
265 {
266 bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
267 MISC_WATCHDOG, 1);
268 }
269