imx23_olinuxino_machdep.c revision 1.2.4.2 1 1.2.4.2 tls /* $Id: imx23_olinuxino_machdep.c,v 1.2.4.2 2013/02/25 00:28:36 tls Exp $ */
2 1.2.4.2 tls
3 1.2.4.2 tls /*
4 1.2.4.2 tls * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.2.4.2 tls * All rights reserved.
6 1.2.4.2 tls *
7 1.2.4.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 tls * by Petri Laakso.
9 1.2.4.2 tls *
10 1.2.4.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 tls * modification, are permitted provided that the following conditions
12 1.2.4.2 tls * are met:
13 1.2.4.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.4.2 tls *
19 1.2.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.4.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.4.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.4.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.4.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.4.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.4.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.4.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.4.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.4.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.2.4.2 tls */
31 1.2.4.2 tls
32 1.2.4.2 tls #include <sys/bus.h>
33 1.2.4.2 tls #include <sys/cdefs.h>
34 1.2.4.2 tls #include <sys/device.h>
35 1.2.4.2 tls #include <sys/lwp.h>
36 1.2.4.2 tls #include <sys/mount.h>
37 1.2.4.2 tls #include <sys/mutex.h>
38 1.2.4.2 tls #include <sys/param.h>
39 1.2.4.2 tls #include <sys/reboot.h>
40 1.2.4.2 tls #include <sys/rnd.h>
41 1.2.4.2 tls #include <sys/systm.h>
42 1.2.4.2 tls #include <sys/termios.h>
43 1.2.4.2 tls #include <sys/types.h>
44 1.2.4.2 tls
45 1.2.4.2 tls #include <uvm/uvm.h>
46 1.2.4.2 tls #include <uvm/uvm_prot.h>
47 1.2.4.2 tls #include <uvm/uvm_pmap.h>
48 1.2.4.2 tls
49 1.2.4.2 tls #include <machine/db_machdep.h>
50 1.2.4.2 tls #include <machine/bootconfig.h>
51 1.2.4.2 tls #include <machine/frame.h>
52 1.2.4.2 tls #include <machine/param.h>
53 1.2.4.2 tls #include <machine/pcb.h>
54 1.2.4.2 tls #include <machine/pmap.h>
55 1.2.4.2 tls
56 1.2.4.2 tls #include <arm/undefined.h>
57 1.2.4.2 tls #include <arm/arm32/machdep.h>
58 1.2.4.2 tls
59 1.2.4.2 tls #include <arm/imx/imx23_digctlreg.h>
60 1.2.4.2 tls #include <arm/imx/imx23_clkctrlreg.h>
61 1.2.4.2 tls #include <arm/imx/imx23_rtcreg.h>
62 1.2.4.2 tls #include <arm/imx/imx23_uartdbgreg.h>
63 1.2.4.2 tls #include <arm/imx/imx23var.h>
64 1.2.4.2 tls
65 1.2.4.2 tls #include "plcom.h"
66 1.2.4.2 tls #if (NPLCOM > 0)
67 1.2.4.2 tls #include <evbarm/dev/plcomreg.h>
68 1.2.4.2 tls #include <evbarm/dev/plcomvar.h>
69 1.2.4.2 tls #endif
70 1.2.4.2 tls
71 1.2.4.2 tls #include "opt_evbarm_boardtype.h"
72 1.2.4.2 tls
73 1.2.4.2 tls static vaddr_t get_ttb(void);
74 1.2.4.2 tls static void setup_real_page_tables(void);
75 1.2.4.2 tls //static void entropy_init(void);
76 1.2.4.2 tls
77 1.2.4.2 tls /*
78 1.2.4.2 tls * Static device map for i.MX23 peripheral address space.
79 1.2.4.2 tls */
80 1.2.4.2 tls #define _A(a) ((a) & ~L1_S_OFFSET)
81 1.2.4.2 tls #define _S(s) (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
82 1.2.4.2 tls static const struct pmap_devmap imx23_devmap[] = {
83 1.2.4.2 tls {
84 1.2.4.2 tls _A(APBH_BASE), /* Virtual address. */
85 1.2.4.2 tls _A(APBH_BASE), /* Physical address. */
86 1.2.4.2 tls _S(APBH_SIZE + APBX_SIZE), /* APBX located after APBH. */
87 1.2.4.2 tls VM_PROT_READ|VM_PROT_WRITE, /* Protection bits. */
88 1.2.4.2 tls PTE_NOCACHE /* Cache attributes. */
89 1.2.4.2 tls },
90 1.2.4.2 tls { 0, 0, 0, 0, 0 }
91 1.2.4.2 tls };
92 1.2.4.2 tls #undef _A
93 1.2.4.2 tls #undef _S
94 1.2.4.2 tls
95 1.2.4.2 tls static vm_offset_t physical_freestart;
96 1.2.4.2 tls static vm_offset_t physical_freeend;
97 1.2.4.2 tls static u_int free_pages;
98 1.2.4.2 tls
99 1.2.4.2 tls BootConfig bootconfig;
100 1.2.4.2 tls vm_offset_t physical_start;
101 1.2.4.2 tls vm_offset_t physical_end;
102 1.2.4.2 tls static char kernel_boot_args[MAX_BOOT_STRING];
103 1.2.4.2 tls char *boot_args;
104 1.2.4.2 tls paddr_t msgbufphys;
105 1.2.4.2 tls
106 1.2.4.2 tls extern char KERNEL_BASE_phys;
107 1.2.4.2 tls extern char KERNEL_BASE_virt;
108 1.2.4.2 tls extern char _end[];
109 1.2.4.2 tls extern char __data_start[];
110 1.2.4.2 tls extern char _edata[];
111 1.2.4.2 tls extern char __bss_start[];
112 1.2.4.2 tls extern char __bss_end__[];
113 1.2.4.2 tls extern pv_addr_t kernelstack;
114 1.2.4.2 tls
115 1.2.4.2 tls extern u_int data_abort_handler_address;
116 1.2.4.2 tls extern u_int prefetch_abort_handler_address;
117 1.2.4.2 tls
118 1.2.4.2 tls /* Define various stack sizes in pages. */
119 1.2.4.2 tls #define FIQ_STACK_SIZE 1
120 1.2.4.2 tls #define IRQ_STACK_SIZE 1
121 1.2.4.2 tls #define ABT_STACK_SIZE 1
122 1.2.4.2 tls #define UND_STACK_SIZE 1
123 1.2.4.2 tls
124 1.2.4.2 tls /* Macros to translate between physical and virtual addresses. */
125 1.2.4.2 tls #define KERNEL_BASE_PHYS ((paddr_t)&KERNEL_BASE_phys)
126 1.2.4.2 tls #define KERNEL_BASE_VIRT ((vaddr_t)&KERNEL_BASE_virt)
127 1.2.4.2 tls #define KERN_VTOPHYS(va) \
128 1.2.4.2 tls ((paddr_t)((vaddr_t)va - KERNEL_BASE_VIRT + KERNEL_BASE_PHYS))
129 1.2.4.2 tls #define KERN_PHYSTOV(pa) \
130 1.2.4.2 tls ((vaddr_t)((paddr_t)pa - KERNEL_BASE_PHYS + KERNEL_BASE_VIRT))
131 1.2.4.2 tls
132 1.2.4.2 tls #define KERNEL_PT_SYS 0 /* L2 table for mapping vectors page. */
133 1.2.4.2 tls #define KERNEL_PT_KERNEL 1 /* L2 table for mapping kernel. */
134 1.2.4.2 tls #define KERNEL_PT_KERNEL_NUM 4
135 1.2.4.2 tls
136 1.2.4.2 tls #define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
137 1.2.4.2 tls /* Page tables for mapping kernel VM */
138 1.2.4.2 tls #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
139 1.2.4.2 tls #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
140 1.2.4.2 tls
141 1.2.4.2 tls pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
142 1.2.4.2 tls
143 1.2.4.2 tls #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
144 1.2.4.2 tls #define KERNEL_VM_SIZE (0xf0000000 - KERNEL_VM_BASE)
145 1.2.4.2 tls
146 1.2.4.2 tls #define L1_PAGE_TABLE (DRAM_BASE + MEMSIZE * 1024 * 1024 - L1_TABLE_SIZE)
147 1.2.4.2 tls #define BOOTIMX23_ARGS (L1_PAGE_TABLE - MAX_BOOT_STRING - 1)
148 1.2.4.2 tls
149 1.2.4.2 tls #define REG_RD(reg) *(volatile uint32_t *)(reg)
150 1.2.4.2 tls #define REG_WR(reg, val) \
151 1.2.4.2 tls do { \
152 1.2.4.2 tls *(volatile uint32_t *)((reg)) = val; \
153 1.2.4.2 tls } while (0)
154 1.2.4.2 tls
155 1.2.4.2 tls /*
156 1.2.4.2 tls * Initialize everything and return new svc stack pointer.
157 1.2.4.2 tls */
158 1.2.4.2 tls u_int
159 1.2.4.2 tls initarm(void *arg)
160 1.2.4.2 tls {
161 1.2.4.2 tls
162 1.2.4.2 tls if (set_cpufuncs())
163 1.2.4.2 tls panic("set_cpufuncs failed");
164 1.2.4.2 tls
165 1.2.4.2 tls pmap_devmap_bootstrap(get_ttb(), imx23_devmap);
166 1.2.4.2 tls
167 1.2.4.2 tls cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
168 1.2.4.2 tls
169 1.2.4.2 tls consinit();
170 1.2.4.2 tls
171 1.2.4.2 tls /* Talk to the user. */
172 1.2.4.2 tls #define BDSTR(s) _BDSTR(s)
173 1.2.4.2 tls #define _BDSTR(s) #s
174 1.2.4.2 tls printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
175 1.2.4.2 tls #undef BDSTR
176 1.2.4.2 tls #undef _BDSTR
177 1.2.4.2 tls
178 1.2.4.2 tls /* Copy boot arguments passed from bootimx23. */
179 1.2.4.2 tls boot_args = (char *)BOOTIMX23_ARGS;
180 1.2.4.2 tls memcpy(kernel_boot_args, boot_args, MAX_BOOT_STRING);
181 1.2.4.2 tls boot_args = kernel_boot_args;
182 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
183 1.2.4.2 tls printf("boot_args: %s\n", boot_args);
184 1.2.4.2 tls #endif
185 1.2.4.2 tls parse_mi_bootargs(boot_args);
186 1.2.4.2 tls
187 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
188 1.2.4.2 tls printf("initarm: Configuring system ...\n");
189 1.2.4.2 tls #endif
190 1.2.4.2 tls
191 1.2.4.2 tls physical_start = DRAM_BASE;
192 1.2.4.2 tls physical_end = DRAM_BASE + MEMSIZE * 1024 * 1024;
193 1.2.4.2 tls physmem = (physical_end - physical_start) / PAGE_SIZE;
194 1.2.4.2 tls
195 1.2.4.2 tls /* bootconfig is used by cpu_dump() and cousins. */
196 1.2.4.2 tls bootconfig.dramblocks = 1;
197 1.2.4.2 tls bootconfig.dram[0].address = DRAM_BASE;
198 1.2.4.2 tls bootconfig.dram[0].pages = physmem;
199 1.2.4.2 tls
200 1.2.4.2 tls /*
201 1.2.4.2 tls * Our kernel is at the beginning of the DRAM, so set our free space to
202 1.2.4.2 tls * all the memory after the kernel.
203 1.2.4.2 tls */
204 1.2.4.2 tls physical_freestart = KERN_VTOPHYS(round_page((vaddr_t) _end));
205 1.2.4.2 tls physical_freeend = physical_end;
206 1.2.4.2 tls free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
207 1.2.4.2 tls
208 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
209 1.2.4.2 tls /* Tell the user about the memory. */
210 1.2.4.2 tls printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
211 1.2.4.2 tls physical_start, physical_end - 1);
212 1.2.4.2 tls #endif
213 1.2.4.2 tls
214 1.2.4.2 tls /*
215 1.2.4.2 tls * Set up first and second level page tables. Pages of memory will be
216 1.2.4.2 tls * allocated and mapped for structures required for system operation.
217 1.2.4.2 tls * kernel_l1pt, kernel_pt_table[], systempage, irqstack, abtstack,
218 1.2.4.2 tls * undstack, kernelstack, msgbufphys will be set to point to the memory
219 1.2.4.2 tls * that was allocated for them.
220 1.2.4.2 tls */
221 1.2.4.2 tls setup_real_page_tables();
222 1.2.4.2 tls
223 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
224 1.2.4.2 tls printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
225 1.2.4.2 tls physical_freestart, free_pages, free_pages);
226 1.2.4.2 tls #endif
227 1.2.4.2 tls
228 1.2.4.2 tls uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
229 1.2.4.2 tls
230 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
231 1.2.4.2 tls printf("bootstrap done.\n");
232 1.2.4.2 tls #endif
233 1.2.4.2 tls
234 1.2.4.2 tls /* Copy vectors from page0 to vectors page. */
235 1.2.4.2 tls arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
236 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
237 1.2.4.2 tls printf("init subsystems: stacks ");
238 1.2.4.2 tls #endif
239 1.2.4.2 tls set_stackptr(PSR_FIQ32_MODE,
240 1.2.4.2 tls fiqstack.pv_va + FIQ_STACK_SIZE * PAGE_SIZE);
241 1.2.4.2 tls set_stackptr(PSR_IRQ32_MODE,
242 1.2.4.2 tls irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
243 1.2.4.2 tls set_stackptr(PSR_ABT32_MODE,
244 1.2.4.2 tls abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
245 1.2.4.2 tls set_stackptr(PSR_UND32_MODE,
246 1.2.4.2 tls undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
247 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
248 1.2.4.2 tls printf("vectors ");
249 1.2.4.2 tls #endif
250 1.2.4.2 tls data_abort_handler_address = (u_int)data_abort_handler;
251 1.2.4.2 tls prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
252 1.2.4.2 tls undefined_handler_address = (u_int)undefinedinstruction_bounce;
253 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
254 1.2.4.2 tls printf("undefined ");
255 1.2.4.2 tls #endif
256 1.2.4.2 tls undefined_init();
257 1.2.4.2 tls /* Load memory into UVM. */
258 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
259 1.2.4.2 tls printf("page ");
260 1.2.4.2 tls #endif
261 1.2.4.2 tls uvm_setpagesize();
262 1.2.4.2 tls uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
263 1.2.4.2 tls atop(physical_freestart), atop(physical_freeend),
264 1.2.4.2 tls VM_FREELIST_DEFAULT);
265 1.2.4.2 tls
266 1.2.4.2 tls /* Boot strap pmap telling it where the kernel page table is. */
267 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
268 1.2.4.2 tls printf("pmap ");
269 1.2.4.2 tls #endif
270 1.2.4.2 tls pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
271 1.2.4.2 tls
272 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
273 1.2.4.2 tls printf("done.\n");
274 1.2.4.2 tls #endif
275 1.2.4.2 tls
276 1.2.4.2 tls #ifdef __HAVE_MEMORY_DISK__
277 1.2.4.2 tls md_root_setconf(memory_disk, sizeof memory_disk);
278 1.2.4.2 tls #endif
279 1.2.4.2 tls
280 1.2.4.2 tls #ifdef BOOTHOWTO
281 1.2.4.2 tls boothowto |= BOOTHOWTO;
282 1.2.4.2 tls #endif
283 1.2.4.2 tls
284 1.2.4.2 tls #ifdef KGDB
285 1.2.4.2 tls if (boothowto & RB_KDB) {
286 1.2.4.2 tls kgdb_debug_init = 1;
287 1.2.4.2 tls kgdb_connect(1);
288 1.2.4.2 tls }
289 1.2.4.2 tls #endif
290 1.2.4.2 tls
291 1.2.4.2 tls #ifdef DDB
292 1.2.4.2 tls db_machine_init();
293 1.2.4.2 tls if (boothowto & RB_KDB)
294 1.2.4.2 tls Debugger();
295 1.2.4.2 tls #endif
296 1.2.4.2 tls
297 1.2.4.2 tls return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
298 1.2.4.2 tls }
299 1.2.4.2 tls
300 1.2.4.2 tls /*
301 1.2.4.2 tls * Return TTBR (Translation Table Base Register) value from coprocessor.
302 1.2.4.2 tls */
303 1.2.4.2 tls static vaddr_t
304 1.2.4.2 tls get_ttb(void)
305 1.2.4.2 tls {
306 1.2.4.2 tls vaddr_t ttb;
307 1.2.4.2 tls
308 1.2.4.2 tls __asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
309 1.2.4.2 tls
310 1.2.4.2 tls return ttb;
311 1.2.4.2 tls }
312 1.2.4.2 tls
313 1.2.4.2 tls /*
314 1.2.4.2 tls * valloc_pages() is used to allocate free memory to be used for kernel pages.
315 1.2.4.2 tls * Virtual and physical addresses of the allocated memory are saved for the
316 1.2.4.2 tls * later use by the structures:
317 1.2.4.2 tls *
318 1.2.4.2 tls * - kernel_l1pt which holds the address of the kernel's L1 translaton table.
319 1.2.4.2 tls * - kernel_pt_table[] holds the addresses of the kernel's L2 page tables.
320 1.2.4.2 tls *
321 1.2.4.2 tls * pmap_link_l2pt() is used to create link from L1 table entry to the L2 page
322 1.2.4.2 tls * table. Link is a reference to coarse page table which has 256 entries,
323 1.2.4.2 tls * splitting the 1MB that the table describes into 4kB blocks.
324 1.2.4.2 tls *
325 1.2.4.2 tls * pmap_map_entry() updates the PTE in L2 PT for an VA to point to single
326 1.2.4.2 tls * physical page previously allocated.
327 1.2.4.2 tls *
328 1.2.4.2 tls * pmap_map_chunk() maps a chunk of memory using the most efficient
329 1.2.4.2 tls * mapping possible (section, large page, small page) into the provided L1 and
330 1.2.4.2 tls * L2 tables at the specified virtual address. pmap_map_chunk() excepts linking
331 1.2.4.2 tls * to be done before it is called for chunks smaller than a section.
332 1.2.4.2 tls */
333 1.2.4.2 tls static void
334 1.2.4.2 tls setup_real_page_tables(void)
335 1.2.4.2 tls {
336 1.2.4.2 tls /*
337 1.2.4.2 tls * Define a macro to simplify memory allocation. As we allocate the
338 1.2.4.2 tls * memory, make sure that we don't walk over our temporary first level
339 1.2.4.2 tls * translation table.
340 1.2.4.2 tls */
341 1.2.4.2 tls #define valloc_pages(var, np) \
342 1.2.4.2 tls (var).pv_pa = physical_freestart; \
343 1.2.4.2 tls physical_freestart += ((np) * PAGE_SIZE); \
344 1.2.4.2 tls if (physical_freestart > (physical_freeend - L1_TABLE_SIZE)) \
345 1.2.4.2 tls panic("%s: out of memory", __func__); \
346 1.2.4.2 tls free_pages -= (np); \
347 1.2.4.2 tls (var).pv_va = KERN_PHYSTOV((var).pv_pa); \
348 1.2.4.2 tls memset((char *)(var).pv_va, 0, ((np) * PAGE_SIZE));
349 1.2.4.2 tls
350 1.2.4.2 tls int loop, pt_index;
351 1.2.4.2 tls
352 1.2.4.2 tls pt_index = 0;
353 1.2.4.2 tls kernel_l1pt.pv_pa = 0;
354 1.2.4.2 tls kernel_l1pt.pv_va = 0;
355 1.2.4.2 tls for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
356 1.2.4.2 tls /* Are we 16kB aligned for an L1? */
357 1.2.4.2 tls if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0 &&
358 1.2.4.2 tls kernel_l1pt.pv_pa == 0) {
359 1.2.4.2 tls valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
360 1.2.4.2 tls } else {
361 1.2.4.2 tls valloc_pages(kernel_pt_table[pt_index],
362 1.2.4.2 tls L2_TABLE_SIZE / PAGE_SIZE);
363 1.2.4.2 tls ++pt_index;
364 1.2.4.2 tls }
365 1.2.4.2 tls }
366 1.2.4.2 tls
367 1.2.4.2 tls /* Make sure L1 page table is aligned to 16kB. */
368 1.2.4.2 tls if (!kernel_l1pt.pv_pa ||
369 1.2.4.2 tls (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
370 1.2.4.2 tls panic("%s: Failed to align the kernel page directory",
371 1.2.4.2 tls __func__);
372 1.2.4.2 tls
373 1.2.4.2 tls /*
374 1.2.4.2 tls * Allocate a page for the system page mapped to ARM_VECTORS_HIGH.
375 1.2.4.2 tls * This page will just contain the system vectors and can be shared by
376 1.2.4.2 tls * all processes.
377 1.2.4.2 tls */
378 1.2.4.2 tls valloc_pages(systempage, 1);
379 1.2.4.2 tls systempage.pv_va = ARM_VECTORS_HIGH;
380 1.2.4.2 tls
381 1.2.4.2 tls /* Allocate stacks for all modes. */
382 1.2.4.2 tls valloc_pages(fiqstack, FIQ_STACK_SIZE);
383 1.2.4.2 tls valloc_pages(irqstack, IRQ_STACK_SIZE);
384 1.2.4.2 tls valloc_pages(abtstack, ABT_STACK_SIZE);
385 1.2.4.2 tls valloc_pages(undstack, UND_STACK_SIZE);
386 1.2.4.2 tls valloc_pages(kernelstack, UPAGES);
387 1.2.4.2 tls
388 1.2.4.2 tls /* Allocate the message buffer. */
389 1.2.4.2 tls pv_addr_t msgbuf;
390 1.2.4.2 tls int msgbuf_pgs = round_page(MSGBUFSIZE) / PAGE_SIZE;
391 1.2.4.2 tls valloc_pages(msgbuf, msgbuf_pgs);
392 1.2.4.2 tls msgbufphys = msgbuf.pv_pa;
393 1.2.4.2 tls
394 1.2.4.2 tls vaddr_t l1_va = kernel_l1pt.pv_va;
395 1.2.4.2 tls vaddr_t l1_pa = kernel_l1pt.pv_pa;
396 1.2.4.2 tls
397 1.2.4.2 tls /* Map the L2 pages tables in the L1 page table. */
398 1.2.4.2 tls
399 1.2.4.2 tls pmap_link_l2pt(l1_va, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
400 1.2.4.2 tls &kernel_pt_table[KERNEL_PT_SYS]);
401 1.2.4.2 tls
402 1.2.4.2 tls for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
403 1.2.4.2 tls pmap_link_l2pt(l1_va, KERNEL_BASE + loop * 0x00400000,
404 1.2.4.2 tls &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
405 1.2.4.2 tls
406 1.2.4.2 tls for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
407 1.2.4.2 tls pmap_link_l2pt(l1_va, KERNEL_VM_BASE + loop * 0x00400000,
408 1.2.4.2 tls &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
409 1.2.4.2 tls
410 1.2.4.2 tls /* Update the top of the kernel VM. */
411 1.2.4.2 tls pmap_curmaxkvaddr =
412 1.2.4.2 tls KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
413 1.2.4.2 tls
414 1.2.4.2 tls extern char etext[];
415 1.2.4.2 tls size_t textsize = (uintptr_t)etext - KERNEL_BASE;
416 1.2.4.2 tls size_t totalsize = (uintptr_t)_end - KERNEL_BASE;
417 1.2.4.2 tls u_int logical;
418 1.2.4.2 tls
419 1.2.4.2 tls textsize = (textsize + PGOFSET) & ~PGOFSET;
420 1.2.4.2 tls totalsize = (totalsize + PGOFSET) & ~PGOFSET;
421 1.2.4.2 tls
422 1.2.4.2 tls logical = 0x00000000; /* offset of kernel in RAM */
423 1.2.4.2 tls
424 1.2.4.2 tls logical += pmap_map_chunk(l1_va, KERNEL_BASE + logical,
425 1.2.4.2 tls physical_start + logical, textsize,
426 1.2.4.2 tls VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
427 1.2.4.2 tls
428 1.2.4.2 tls logical += pmap_map_chunk(l1_va, KERNEL_BASE + logical,
429 1.2.4.2 tls physical_start + logical, totalsize - textsize,
430 1.2.4.2 tls VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
431 1.2.4.2 tls
432 1.2.4.2 tls /* Map the stack pages. */
433 1.2.4.2 tls pmap_map_chunk(l1_va, fiqstack.pv_va, fiqstack.pv_pa,
434 1.2.4.2 tls FIQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
435 1.2.4.2 tls
436 1.2.4.2 tls pmap_map_chunk(l1_va, irqstack.pv_va, irqstack.pv_pa,
437 1.2.4.2 tls IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
438 1.2.4.2 tls
439 1.2.4.2 tls pmap_map_chunk(l1_va, abtstack.pv_va, abtstack.pv_pa,
440 1.2.4.2 tls ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
441 1.2.4.2 tls
442 1.2.4.2 tls pmap_map_chunk(l1_va, undstack.pv_va, undstack.pv_pa,
443 1.2.4.2 tls UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
444 1.2.4.2 tls
445 1.2.4.2 tls pmap_map_chunk(l1_va, kernelstack.pv_va, kernelstack.pv_pa,
446 1.2.4.2 tls UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
447 1.2.4.2 tls
448 1.2.4.2 tls pmap_map_chunk(l1_va, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
449 1.2.4.2 tls L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
450 1.2.4.2 tls
451 1.2.4.2 tls for (loop = 0; loop < NUM_KERNEL_PTS; ++loop)
452 1.2.4.2 tls pmap_map_chunk(l1_va, kernel_pt_table[loop].pv_va,
453 1.2.4.2 tls kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
454 1.2.4.2 tls VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
455 1.2.4.2 tls
456 1.2.4.2 tls /* Map the vector page. */
457 1.2.4.2 tls pmap_map_entry(l1_va, ARM_VECTORS_HIGH, systempage.pv_pa,
458 1.2.4.2 tls VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
459 1.2.4.2 tls
460 1.2.4.2 tls pmap_devmap_bootstrap(l1_va, imx23_devmap);
461 1.2.4.2 tls
462 1.2.4.2 tls #ifdef VERBOSE_INIT_ARM
463 1.2.4.2 tls /* Tell the user about where all the bits and pieces live. */
464 1.2.4.2 tls printf("%22s Physical Virtual Num\n", " ");
465 1.2.4.2 tls printf("%22s Starting Ending Starting Ending Pages\n", " ");
466 1.2.4.2 tls
467 1.2.4.2 tls static const char mem_fmt[] =
468 1.2.4.2 tls "%20s: 0x%08lx 0x%08lx 0x%08lx 0x%08lx %d\n";
469 1.2.4.2 tls static const char mem_fmt_nov[] =
470 1.2.4.2 tls "%20s: 0x%08lx 0x%08lx %d\n";
471 1.2.4.2 tls
472 1.2.4.2 tls printf(mem_fmt, "SDRAM", physical_start, physical_end-1,
473 1.2.4.2 tls KERN_PHYSTOV(physical_start), KERN_PHYSTOV(physical_end-1),
474 1.2.4.2 tls physmem);
475 1.2.4.2 tls printf(mem_fmt, "text section",
476 1.2.4.2 tls KERN_VTOPHYS(KERNEL_BASE), KERN_VTOPHYS(etext-1),
477 1.2.4.2 tls (vaddr_t)KERNEL_BASE, (vaddr_t)etext-1,
478 1.2.4.2 tls (int)(textsize / PAGE_SIZE));
479 1.2.4.2 tls printf(mem_fmt, "data section",
480 1.2.4.2 tls KERN_VTOPHYS(__data_start), KERN_VTOPHYS(_edata),
481 1.2.4.2 tls (vaddr_t)__data_start, (vaddr_t)_edata,
482 1.2.4.2 tls (int)((round_page((vaddr_t)_edata)
483 1.2.4.2 tls - trunc_page((vaddr_t)__data_start)) / PAGE_SIZE));
484 1.2.4.2 tls printf(mem_fmt, "bss section",
485 1.2.4.2 tls KERN_VTOPHYS(__bss_start), KERN_VTOPHYS(__bss_end__),
486 1.2.4.2 tls (vaddr_t)__bss_start, (vaddr_t)__bss_end__,
487 1.2.4.2 tls (int)((round_page((vaddr_t)__bss_end__)
488 1.2.4.2 tls - trunc_page((vaddr_t)__bss_start)) / PAGE_SIZE));
489 1.2.4.2 tls printf(mem_fmt, "L1 page directory",
490 1.2.4.2 tls kernel_l1pt.pv_pa, kernel_l1pt.pv_pa + L1_TABLE_SIZE - 1,
491 1.2.4.2 tls kernel_l1pt.pv_va, kernel_l1pt.pv_va + L1_TABLE_SIZE - 1,
492 1.2.4.2 tls L1_TABLE_SIZE / PAGE_SIZE);
493 1.2.4.2 tls printf(mem_fmt, "Exception Vectors",
494 1.2.4.2 tls systempage.pv_pa, systempage.pv_pa + PAGE_SIZE - 1,
495 1.2.4.2 tls (vaddr_t)ARM_VECTORS_HIGH,
496 1.2.4.2 tls (vaddr_t)ARM_VECTORS_HIGH + PAGE_SIZE - 1, 1);
497 1.2.4.2 tls printf(mem_fmt, "FIQ stack",
498 1.2.4.2 tls fiqstack.pv_pa, fiqstack.pv_pa + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
499 1.2.4.2 tls fiqstack.pv_va, fiqstack.pv_va + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
500 1.2.4.2 tls FIQ_STACK_SIZE);
501 1.2.4.2 tls printf(mem_fmt, "IRQ stack",
502 1.2.4.2 tls irqstack.pv_pa, irqstack.pv_pa + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
503 1.2.4.2 tls irqstack.pv_va, irqstack.pv_va + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
504 1.2.4.2 tls IRQ_STACK_SIZE);
505 1.2.4.2 tls printf(mem_fmt, "ABT stack",
506 1.2.4.2 tls abtstack.pv_pa, abtstack.pv_pa + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
507 1.2.4.2 tls abtstack.pv_va, abtstack.pv_va + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
508 1.2.4.2 tls ABT_STACK_SIZE);
509 1.2.4.2 tls printf(mem_fmt, "UND stack",
510 1.2.4.2 tls undstack.pv_pa, undstack.pv_pa + (UND_STACK_SIZE * PAGE_SIZE) - 1,
511 1.2.4.2 tls undstack.pv_va, undstack.pv_va + (UND_STACK_SIZE * PAGE_SIZE) - 1,
512 1.2.4.2 tls UND_STACK_SIZE);
513 1.2.4.2 tls printf(mem_fmt, "SVC stack",
514 1.2.4.2 tls kernelstack.pv_pa, kernelstack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
515 1.2.4.2 tls kernelstack.pv_va, kernelstack.pv_va + (UPAGES * PAGE_SIZE) - 1,
516 1.2.4.2 tls UPAGES);
517 1.2.4.2 tls printf(mem_fmt_nov, "Message Buffer",
518 1.2.4.2 tls msgbufphys, msgbufphys + msgbuf_pgs * PAGE_SIZE - 1, msgbuf_pgs);
519 1.2.4.2 tls printf(mem_fmt, "Free Memory", physical_freestart, physical_freeend-1,
520 1.2.4.2 tls KERN_PHYSTOV(physical_freestart), KERN_PHYSTOV(physical_freeend-1),
521 1.2.4.2 tls free_pages);
522 1.2.4.2 tls #endif
523 1.2.4.2 tls
524 1.2.4.2 tls cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
525 1.2.4.2 tls cpu_setttb(l1_pa, FALSE);
526 1.2.4.2 tls cpu_tlb_flushID();
527 1.2.4.2 tls cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
528 1.2.4.2 tls
529 1.2.4.2 tls return;
530 1.2.4.2 tls }
531 1.2.4.2 tls
532 1.2.4.2 tls /*
533 1.2.4.2 tls * Initialize console.
534 1.2.4.2 tls */
535 1.2.4.2 tls static struct plcom_instance imx23_pi = {
536 1.2.4.2 tls .pi_type = PLCOM_TYPE_PL011,
537 1.2.4.2 tls .pi_iot = &imx23_bus_space,
538 1.2.4.2 tls .pi_size = PL011COM_UART_SIZE,
539 1.2.4.2 tls .pi_iobase = HW_UARTDBG_BASE
540 1.2.4.2 tls };
541 1.2.4.2 tls
542 1.2.4.2 tls #define PLCONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
543 1.2.4.2 tls #define PLCONSPEED 115200
544 1.2.4.2 tls void
545 1.2.4.2 tls consinit(void)
546 1.2.4.2 tls {
547 1.2.4.2 tls /* consinit() is called from also from the main(). */
548 1.2.4.2 tls static int consinit_called = 0;
549 1.2.4.2 tls
550 1.2.4.2 tls if (consinit_called)
551 1.2.4.2 tls return;
552 1.2.4.2 tls
553 1.2.4.2 tls plcomcnattach(&imx23_pi, PLCONSPEED, IMX23_UART_CLK, PLCONMODE, 0);
554 1.2.4.2 tls
555 1.2.4.2 tls consinit_called = 1;
556 1.2.4.2 tls
557 1.2.4.2 tls return;
558 1.2.4.2 tls }
559 1.2.4.2 tls
560 1.2.4.2 tls /*
561 1.2.4.2 tls * Reboot or halt the system.
562 1.2.4.2 tls */
563 1.2.4.2 tls void
564 1.2.4.2 tls cpu_reboot(int howto, char *bootstr)
565 1.2.4.2 tls {
566 1.2.4.2 tls static int cpu_reboot_called = 0;
567 1.2.4.2 tls
568 1.2.4.2 tls boothowto |= howto;
569 1.2.4.2 tls
570 1.2.4.2 tls /*
571 1.2.4.2 tls * If this is the first invocation of cpu_reboot() and the RB_NOSYNC
572 1.2.4.2 tls * flag is not set in howto; sync and unmount the system disks by
573 1.2.4.2 tls * calling vfs_shutdown(9) and set the time of day clock by calling
574 1.2.4.2 tls * resettodr(9).
575 1.2.4.2 tls */
576 1.2.4.2 tls if (!cpu_reboot_called && !(boothowto & RB_NOSYNC)) {
577 1.2.4.2 tls vfs_shutdown();
578 1.2.4.2 tls resettodr();
579 1.2.4.2 tls }
580 1.2.4.2 tls
581 1.2.4.2 tls cpu_reboot_called = 1;
582 1.2.4.2 tls
583 1.2.4.2 tls IRQdisable; /* FIQ's stays on because they are special. */
584 1.2.4.2 tls
585 1.2.4.2 tls /*
586 1.2.4.2 tls * If rebooting after a crash (i.e., if RB_DUMP is set in howto, but
587 1.2.4.2 tls * RB_HALT is not), save a system crash dump.
588 1.2.4.2 tls */
589 1.2.4.2 tls if ((boothowto & RB_DUMP) && !(boothowto & RB_HALT))
590 1.2.4.2 tls panic("please implement crash dump!"); // XXX
591 1.2.4.2 tls
592 1.2.4.2 tls /* Run any shutdown hooks by calling pmf_system_shutdown(9). */
593 1.2.4.2 tls pmf_system_shutdown(boothowto);
594 1.2.4.2 tls
595 1.2.4.2 tls printf("system %s.\n", boothowto & RB_HALT ? "halted" : "rebooted");
596 1.2.4.2 tls
597 1.2.4.2 tls if (boothowto & RB_HALT) {
598 1.2.4.2 tls /* Enable i.MX233 wait-for-interrupt mode. */
599 1.2.4.2 tls REG_WR(HW_CLKCTRL_BASE + HW_CLKCTRL_CPU,
600 1.2.4.2 tls (REG_RD(HW_CLKCTRL_BASE + HW_CLKCTRL_CPU) |
601 1.2.4.2 tls HW_CLKCTRL_CPU_INTERRUPT_WAIT));
602 1.2.4.2 tls
603 1.2.4.2 tls /* Disable FIQ's and wait for interrupt (which never arrives) */
604 1.2.4.2 tls __asm volatile( \
605 1.2.4.2 tls "mrs r0, cpsr\n\t" \
606 1.2.4.2 tls "orr r0, #0x40\n\t" \
607 1.2.4.2 tls "msr cpsr_c, r0\n\t" \
608 1.2.4.2 tls "mov r0, #0\n\t" \
609 1.2.4.2 tls "mcr p15, 0, r0, c7, c0, 4\n\t"
610 1.2.4.2 tls );
611 1.2.4.2 tls
612 1.2.4.2 tls for(;;);
613 1.2.4.2 tls
614 1.2.4.2 tls /* NOT REACHED */
615 1.2.4.2 tls }
616 1.2.4.2 tls
617 1.2.4.2 tls /* Reboot the system. */
618 1.2.4.2 tls REG_WR(HW_RTC_BASE + HW_RTC_WATCHDOG, 10000);
619 1.2.4.2 tls REG_WR(HW_RTC_BASE + HW_RTC_CTRL_SET, HW_RTC_CTRL_WATCHDOGEN);
620 1.2.4.2 tls REG_WR(HW_RTC_BASE + HW_RTC_WATCHDOG, 0);
621 1.2.4.2 tls
622 1.2.4.2 tls for(;;);
623 1.2.4.2 tls
624 1.2.4.2 tls /* NOT REACHED */
625 1.2.4.2 tls }
626 1.2.4.2 tls
627 1.2.4.2 tls /*
628 1.2.4.2 tls * Delay us microseconds.
629 1.2.4.2 tls */
630 1.2.4.2 tls void
631 1.2.4.2 tls delay(unsigned int us)
632 1.2.4.2 tls {
633 1.2.4.2 tls uint32_t start;
634 1.2.4.2 tls uint32_t now;
635 1.2.4.2 tls uint32_t elapsed;
636 1.2.4.2 tls uint32_t total;
637 1.2.4.2 tls uint32_t last;
638 1.2.4.2 tls
639 1.2.4.2 tls total = 0;
640 1.2.4.2 tls last = 0;
641 1.2.4.2 tls start = REG_RD(HW_DIGCTL_BASE + HW_DIGCTL_MICROSECONDS);
642 1.2.4.2 tls
643 1.2.4.2 tls do {
644 1.2.4.2 tls now = REG_RD(HW_DIGCTL_BASE + HW_DIGCTL_MICROSECONDS);
645 1.2.4.2 tls
646 1.2.4.2 tls if (start <= now)
647 1.2.4.2 tls elapsed = now - start;
648 1.2.4.2 tls else /* Take care of overflow. */
649 1.2.4.2 tls elapsed = (UINT32_MAX - start) + 1 + now;
650 1.2.4.2 tls
651 1.2.4.2 tls total += elapsed - last;
652 1.2.4.2 tls last = elapsed;
653 1.2.4.2 tls
654 1.2.4.2 tls } while (total < us);
655 1.2.4.2 tls
656 1.2.4.2 tls return;
657 1.2.4.2 tls }
658