marvell_machdep.c revision 1.15 1 /* $NetBSD: marvell_machdep.c,v 1.15 2012/08/16 18:22:45 matt Exp $ */
2 /*
3 * Copyright (c) 2007, 2008, 2010 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: marvell_machdep.c,v 1.15 2012/08/16 18:22:45 matt Exp $");
29
30 #include "opt_evbarm_boardtype.h"
31 #include "opt_ddb.h"
32 #include "opt_pci.h"
33 #include "opt_mvsoc.h"
34 #include "com.h"
35 #include "gtpci.h"
36 #include "mvpex.h"
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
41 #include <sys/systm.h>
42 #include <sys/termios.h>
43
44 #include <prop/proplib.h>
45
46 #include <dev/cons.h>
47 #include <dev/md.h>
48
49 #include <dev/marvell/marvellreg.h>
50 #include <dev/marvell/marvellvar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53
54 #include <machine/autoconf.h>
55 #include <machine/bootconfig.h>
56 #include <machine/pci_machdep.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <arm/db_machdep.h>
61 #include <arm/undefined.h>
62 #include <arm/arm32/machdep.h>
63
64 #include <arm/marvell/mvsocreg.h>
65 #include <arm/marvell/mvsocvar.h>
66 #include <arm/marvell/orionreg.h>
67 #include <arm/marvell/kirkwoodreg.h>
68 #include <arm/marvell/mvsocgppvar.h>
69
70 #include <evbarm/marvell/marvellreg.h>
71 #include <evbarm/marvell/marvellvar.h>
72
73 #include <ddb/db_extern.h>
74 #include <ddb/db_sym.h>
75
76 #include "ksyms.h"
77
78
79 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
80 #define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00000000)
81 #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
82
83 /*
84 * The range 0xc1000000 - 0xccffffff is available for kernel VM space
85 * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
86 */
87 #define KERNEL_VM_SIZE 0x0c000000
88
89 BootConfig bootconfig; /* Boot config storage */
90 static char bootargs[MAX_BOOT_STRING];
91 char *boot_args = NULL;
92
93 vm_offset_t physical_start;
94 vm_offset_t physical_freestart;
95 vm_offset_t physical_freeend;
96 vm_offset_t physical_end;
97 u_int free_pages;
98
99 vm_offset_t msgbufphys;
100
101 extern char _end[];
102
103 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
104 #define KERNEL_PT_KERNEL 1 /* Page table for mapping kernel */
105 #define KERNEL_PT_KERNEL_NUM 4
106 #define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
107 /* Page tables for mapping kernel VM */
108 #define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */
109 #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
110
111 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
112
113 /*
114 * Macros to translate between physical and virtual for a subset of the
115 * kernel address space. *Not* for general use.
116 */
117 #define KERNEL_BASE_PHYS physical_start
118 #define KERN_VTOPHYS(va) \
119 ((paddr_t)((vaddr_t)va - KERNEL_BASE + KERNEL_BASE_PHYS))
120 #define KERN_PHYSTOV(pa) \
121 ((vaddr_t)((paddr_t)pa - KERNEL_BASE_PHYS + KERNEL_BASE))
122
123
124 #include "com.h"
125 #if NCOM > 0
126 #include <dev/ic/comreg.h>
127 #include <dev/ic/comvar.h>
128 #endif
129
130 #ifndef CONSPEED
131 #define CONSPEED B115200 /* It's a setting of the default of u-boot */
132 #endif
133 #ifndef CONMODE
134 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
135
136 int comcnspeed = CONSPEED;
137 int comcnmode = CONMODE;
138 #endif
139
140 #include "opt_kgdb.h"
141 #ifdef KGDB
142 #include <sys/kgdb.h>
143 #endif
144
145 static void marvell_device_register(device_t, void *);
146 #if NGTPCI > 0 || NMVPEX > 0
147 static void marvell_startend_by_tag(int, uint64_t *, uint64_t *);
148 #endif
149
150 static void
151 marvell_system_reset(void)
152 {
153 /* unmask soft reset */
154 write_mlmbreg(MVSOC_MLMB_RSTOUTNMASKR,
155 MVSOC_MLMB_RSTOUTNMASKR_SOFTRSTOUTEN);
156 /* assert soft reset */
157 write_mlmbreg(MVSOC_MLMB_SSRR, MVSOC_MLMB_SSRR_SYSTEMSOFTRST);
158 /* if we're still running, jump to the reset address */
159 cpu_reset();
160 /*NOTREACHED*/
161 }
162
163 void
164 cpu_reboot(int howto, char *bootstr)
165 {
166
167 /*
168 * If we are still cold then hit the air brakes
169 * and crash to earth fast
170 */
171 if (cold) {
172 doshutdownhooks();
173 printf("The operating system has halted.\r\n");
174 printf("Please press any key to reboot.\r\n");
175 cngetc();
176 printf("rebooting...\r\n");
177 marvell_system_reset();
178 }
179
180 /*
181 * If RB_NOSYNC was not specified sync the discs.
182 * Note: Unless cold is set to 1 here, syslogd will die during the
183 * unmount. It looks like syslogd is getting woken up only to find
184 * that it cannot page part of the binary in as the filesystem has
185 * been unmounted.
186 */
187 if (!(howto & RB_NOSYNC))
188 bootsync();
189
190 /* Say NO to interrupts */
191 splhigh();
192
193 /* Do a dump if requested. */
194 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
195 dumpsys();
196
197 /* Run any shutdown hooks */
198 doshutdownhooks();
199
200 /* Make sure IRQ's are disabled */
201 IRQdisable;
202
203 if (howto & RB_HALT) {
204 printf("The operating system has halted.\r\n");
205 printf("Please press any key to reboot.\r\n");
206 cngetc();
207 }
208
209 printf("rebooting...\r\n");
210 marvell_system_reset();
211
212 /*NOTREACHED*/
213 }
214
215 static inline
216 pd_entry_t *
217 read_ttb(void)
218 {
219 long ttb;
220
221 __asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
222
223 return (pd_entry_t *)(ttb & ~((1<<14)-1));
224 }
225
226 /*
227 * Static device mappings. These peripheral registers are mapped at
228 * fixed virtual addresses very early in initarm() so that we can use
229 * them while booting the kernel, and stay at the same address
230 * throughout whole kernel's life time.
231 *
232 * We use this table twice; once with bootstrap page table, and once
233 * with kernel's page table which we build up in initarm().
234 *
235 * Since we map these registers into the bootstrap page table using
236 * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
237 * registers segment-aligned and segment-rounded in order to avoid
238 * using the 2nd page tables.
239 */
240 #define _A(a) ((a) & ~L1_S_OFFSET)
241 #define _S(s) (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
242
243 static const struct pmap_devmap marvell_devmap[] = {
244 {
245 MARVELL_INTERREGS_VBASE,
246 _A(MARVELL_INTERREGS_PBASE),
247 _S(MARVELL_INTERREGS_SIZE),
248 VM_PROT_READ|VM_PROT_WRITE,
249 PTE_NOCACHE,
250 },
251
252 { 0, 0, 0, 0, 0 }
253 };
254
255 #undef _A
256 #undef _S
257
258 extern uint32_t *u_boot_args[];
259
260 /*
261 * u_int initarm(...)
262 *
263 * Initial entry point on startup. This gets called before main() is
264 * entered.
265 * It should be responsible for setting up everything that must be
266 * in place when main is called.
267 * This includes
268 * Taking a copy of the boot configuration structure.
269 * Initialising the physical console so characters can be printed.
270 * Setting up page tables for the kernel
271 * Relocating the kernel to the bottom of physical memory
272 */
273 u_int
274 initarm(void *arg)
275 {
276 uint32_t target, attr, base, size;
277 u_int l1pagetable;
278 int loop, pt_index, cs, memtag = 0, iotag = 0, window;
279
280 cpu_reset_address_paddr = 0xffff0000;
281
282 mvsoc_bootstrap(MARVELL_INTERREGS_VBASE);
283
284 /* map some peripheral registers */
285 pmap_devmap_bootstrap((vaddr_t)read_ttb(), marvell_devmap);
286
287 /* Get ready for splfoo() */
288 switch (mvsoc_model()) {
289 #ifdef ORION
290 case MARVELL_ORION_1_88F1181:
291 case MARVELL_ORION_1_88F5082:
292 case MARVELL_ORION_1_88F5180N:
293 case MARVELL_ORION_1_88F5181:
294 case MARVELL_ORION_1_88F5182:
295 case MARVELL_ORION_1_88F6082:
296 case MARVELL_ORION_1_88F6183:
297 case MARVELL_ORION_1_88W8660:
298 case MARVELL_ORION_2_88F1281:
299 case MARVELL_ORION_2_88F5281:
300 orion_intr_bootstrap();
301
302 memtag = ORION_TAG_PEX0_MEM;
303 iotag = ORION_TAG_PEX0_IO;
304 nwindow = ORION_MLMB_NWINDOW;
305 nremap = ORION_MLMB_NREMAP;
306
307 orion_getclks(MARVELL_INTERREGS_VBASE);
308 break;
309 #endif /* ORION */
310
311 #ifdef KIRKWOOD
312 case MARVELL_KIRKWOOD_88F6180:
313 case MARVELL_KIRKWOOD_88F6192:
314 case MARVELL_KIRKWOOD_88F6281:
315 case MARVELL_KIRKWOOD_88F6282:
316 kirkwood_intr_bootstrap();
317
318 memtag = KIRKWOOD_TAG_PEX_MEM;
319 iotag = KIRKWOOD_TAG_PEX_IO;
320 nwindow = KIRKWOOD_MLMB_NWINDOW;
321 nremap = KIRKWOOD_MLMB_NREMAP;
322
323 kirkwood_getclks(MARVELL_INTERREGS_VBASE);
324 break;
325 #endif /* KIRKWOOD */
326
327 #ifdef MV78XX0
328 case MARVELL_MV78XX0_MV78100:
329 case MARVELL_MV78XX0_MV78200:
330 mv78xx0_intr_bootstrap();
331
332 memtag = MV78XX0_TAG_PEX_MEM;
333 iotag = MV78XX0_TAG_PEX_IO;
334 nwindow = MV78XX0_MLMB_NWINDOW;
335 nremap = MV78XX0_MLMB_NREMAP;
336
337 mv78xx0_getclks(MARVELL_INTERREGS_VBASE);
338 break;
339 #endif /* MV78XX0 */
340
341 default:
342 /* We can't output console here yet... */
343 panic("unknown model...\n");
344
345 /* NOTREACHED */
346 }
347
348 /* Reset PCI-Express space to window register. */
349 window = mvsoc_target(memtag, &target, &attr, NULL, NULL);
350 write_mlmbreg(MVSOC_MLMB_WCR(window),
351 MVSOC_MLMB_WCR_WINEN |
352 MVSOC_MLMB_WCR_TARGET(target) |
353 MVSOC_MLMB_WCR_ATTR(attr) |
354 MVSOC_MLMB_WCR_SIZE(MARVELL_PEXMEM_SIZE));
355 write_mlmbreg(MVSOC_MLMB_WBR(window),
356 MARVELL_PEXMEM_PBASE & MVSOC_MLMB_WBR_BASE_MASK);
357 #ifdef PCI_NETBSD_CONFIGURE
358 if (window < nremap) {
359 write_mlmbreg(MVSOC_MLMB_WRLR(window),
360 MARVELL_PEXMEM_PBASE & MVSOC_MLMB_WRLR_REMAP_MASK);
361 write_mlmbreg(MVSOC_MLMB_WRHR(window), 0);
362 }
363 #endif
364 window = mvsoc_target(iotag, &target, &attr, NULL, NULL);
365 write_mlmbreg(MVSOC_MLMB_WCR(window),
366 MVSOC_MLMB_WCR_WINEN |
367 MVSOC_MLMB_WCR_TARGET(target) |
368 MVSOC_MLMB_WCR_ATTR(attr) |
369 MVSOC_MLMB_WCR_SIZE(MARVELL_PEXIO_SIZE));
370 write_mlmbreg(MVSOC_MLMB_WBR(window),
371 MARVELL_PEXIO_PBASE & MVSOC_MLMB_WBR_BASE_MASK);
372 #ifdef PCI_NETBSD_CONFIGURE
373 if (window < nremap) {
374 write_mlmbreg(MVSOC_MLMB_WRLR(window),
375 MARVELL_PEXIO_PBASE & MVSOC_MLMB_WRLR_REMAP_MASK);
376 write_mlmbreg(MVSOC_MLMB_WRHR(window), 0);
377 }
378 #endif
379
380 /*
381 * Heads up ... Setup the CPU / MMU / TLB functions
382 */
383 if (set_cpufuncs())
384 panic("cpu not recognized!");
385
386 /*
387 * U-Boot doesn't use the virtual memory.
388 *
389 * Physical Address Range Description
390 * ----------------------- ----------------------------------
391 * 0x00000000 - 0x0fffffff SDRAM Bank 0 (max 256MB)
392 * 0x10000000 - 0x1fffffff SDRAM Bank 1 (max 256MB)
393 * 0x20000000 - 0x2fffffff SDRAM Bank 2 (max 256MB)
394 * 0x30000000 - 0x3fffffff SDRAM Bank 3 (max 256MB)
395 * 0xf1000000 - 0xf10fffff SoC Internal Registers
396 */
397
398 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
399
400 consinit();
401
402 /* Talk to the user */
403 #ifndef EVBARM_BOARDTYPE
404 #define EVBARM_BOARDTYPE Marvell
405 #endif
406 #define BDSTR(s) _BDSTR(s)
407 #define _BDSTR(s) #s
408 printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
409
410 /* copy command line U-Boot gave us, if args is valid. */
411 if (u_boot_args[3] != 0) /* XXXXX: need more check?? */
412 strncpy(bootargs, (char *)u_boot_args[3], sizeof(bootargs));
413
414 #ifdef VERBOSE_INIT_ARM
415 printf("initarm: Configuring system ...\n");
416 #endif
417
418 bootconfig.dramblocks = 0;
419 physical_end = physmem = 0;
420 for (cs = MARVELL_TAG_SDRAM_CS0; cs <= MARVELL_TAG_SDRAM_CS3; cs++) {
421 mvsoc_target(cs, &target, &attr, &base, &size);
422 if (size == 0)
423 continue;
424
425 bootconfig.dram[bootconfig.dramblocks].address = base;
426 bootconfig.dram[bootconfig.dramblocks].pages = size / PAGE_SIZE;
427
428 if (base != physical_end)
429 panic("memory hole not support");
430
431 physical_end += size;
432 physmem += size / PAGE_SIZE;
433
434 bootconfig.dramblocks++;
435 }
436
437 /*
438 * Set up the variables that define the availablilty of
439 * physical memory. For now, we're going to set
440 * physical_freestart to 0xa0008000 (where the kernel
441 * was loaded), and allocate the memory we need downwards.
442 * If we get too close to the L1 table that we set up, we
443 * will panic. We will update physical_freestart and
444 * physical_freeend later to reflect what pmap_bootstrap()
445 * wants to see.
446 *
447 * XXX pmap_bootstrap() needs an enema.
448 */
449 physical_start = bootconfig.dram[0].address;
450
451 /*
452 * Our kernel is at the beginning of memory, so set our free space to
453 * all the memory after the kernel.
454 */
455 physical_freestart = KERN_VTOPHYS(round_page((vaddr_t)_end));
456 physical_freeend = physical_end;
457
458 #ifdef VERBOSE_INIT_ARM
459 /* Tell the user about the memory */
460 printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
461 physical_start, physical_end - 1);
462 #endif
463
464 /*
465 * Okay, the kernel starts 8kB in from the bottom of physical
466 * memory. We are going to allocate our bootstrap pages upwards
467 * from physical_freestart.
468 *
469 * We need to allocate some fixed page tables to get the kernel
470 * going. We allocate one page directory and a number of page
471 * tables and store the physical addresses in the kernel_pt_table
472 * array.
473 *
474 * The kernel page directory must be on a 16K boundary. The page
475 * tables must be on 4K bounaries. What we do is allocate the
476 * page directory on the first 16K boundary that we encounter, and
477 * the page tables on 4K boundaries otherwise. Since we allocate
478 * at least 3 L2 page tables, we are guaranteed to encounter at
479 * least one 16K aligned region.
480 */
481
482 #ifdef VERBOSE_INIT_ARM
483 printf("Allocating page tables\n");
484 #endif
485
486 free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
487
488 #ifdef VERBOSE_INIT_ARM
489 printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
490 physical_freestart, free_pages, free_pages);
491 #endif
492
493 /*
494 * Define a macro to simplify memory allocation. As we allocate the
495 * memory, make sure that we don't walk over our temporary first level
496 * translation table.
497 */
498 #define valloc_pages(var, np) \
499 (var).pv_pa = physical_freestart; \
500 physical_freestart += ((np) * PAGE_SIZE); \
501 if (physical_freestart > (physical_freeend - L1_TABLE_SIZE)) \
502 panic("initarm: out of memory"); \
503 free_pages -= (np); \
504 (var).pv_va = KERN_PHYSTOV((var).pv_pa); \
505 memset((char *)(var).pv_va, 0, ((np) * PAGE_SIZE));
506
507 pt_index = 0;
508 kernel_l1pt.pv_pa = 0;
509 kernel_l1pt.pv_va = 0;
510 for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
511 /* Are we 16KB aligned for an L1 ? */
512 if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0 &&
513 kernel_l1pt.pv_pa == 0) {
514 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
515 } else {
516 valloc_pages(kernel_pt_table[pt_index],
517 L2_TABLE_SIZE / PAGE_SIZE);
518 ++pt_index;
519 }
520 }
521
522 /* This should never be able to happen but better confirm that. */
523 if (!kernel_l1pt.pv_pa ||
524 (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
525 panic("initarm: Failed to align the kernel page directory");
526
527 /*
528 * Allocate a page for the system page mapped to V0x00000000
529 * This page will just contain the system vectors and can be
530 * shared by all processes.
531 */
532 valloc_pages(systempage, 1);
533 systempage.pv_va = 0x00000000;
534
535 /* Allocate stacks for all modes */
536 valloc_pages(irqstack, IRQ_STACK_SIZE);
537 valloc_pages(abtstack, ABT_STACK_SIZE);
538 valloc_pages(undstack, UND_STACK_SIZE);
539 valloc_pages(kernelstack, UPAGES);
540
541 #ifdef VERBOSE_INIT_ARM
542 printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
543 irqstack.pv_va);
544 printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
545 abtstack.pv_va);
546 printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
547 undstack.pv_va);
548 printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
549 kernelstack.pv_va);
550 #endif
551
552 /* Allocate the message buffer. */
553 {
554 pv_addr_t msgbuf;
555
556 valloc_pages(msgbuf, round_page(MSGBUFSIZE) / PAGE_SIZE);
557 msgbufphys = msgbuf.pv_pa;
558 }
559
560 /*
561 * Ok we have allocated physical pages for the primary kernel
562 * page tables
563 */
564
565 #ifdef VERBOSE_INIT_ARM
566 printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
567 #endif
568
569 /*
570 * Now we start construction of the L1 page table
571 * We start by mapping the L2 page tables into the L1.
572 * This means that we can replace L1 mappings later on if necessary
573 */
574 l1pagetable = kernel_l1pt.pv_va;
575
576 /* Map the L2 pages tables in the L1 page table */
577 pmap_link_l2pt(l1pagetable, 0x00000000,
578 &kernel_pt_table[KERNEL_PT_SYS]);
579 for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
580 pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
581 &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
582 for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
583 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
584 &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
585
586 /* update the top of the kernel VM */
587 pmap_curmaxkvaddr =
588 KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
589
590 #ifdef VERBOSE_INIT_ARM
591 printf("Mapping kernel\n");
592 #endif
593
594 /* Now we fill in the L2 pagetable for the kernel static code/data */
595 {
596 extern char etext[], _end[];
597 size_t textsize = (uintptr_t)etext - KERNEL_TEXT_BASE;
598 size_t totalsize = (uintptr_t)_end - KERNEL_TEXT_BASE;
599 u_int logical;
600
601 textsize = (textsize + PGOFSET) & ~PGOFSET;
602 totalsize = (totalsize + PGOFSET) & ~PGOFSET;
603
604 logical = 0x00000000; /* offset of kernel in RAM */
605
606 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
607 physical_start + logical, textsize,
608 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
609 logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
610 physical_start + logical, totalsize - textsize,
611 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
612 }
613
614 #ifdef VERBOSE_INIT_ARM
615 printf("Constructing L2 page tables\n");
616 #endif
617
618 /* Map the stack pages */
619 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
620 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
621 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
622 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
623 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
624 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
625 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
626 UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
627
628 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
629 L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
630
631 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop)
632 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
633 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
634 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
635
636 /* Map the vector page. */
637 pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
638 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
639
640 /*
641 * Map integrated peripherals at same address in first level page
642 * table so that we can continue to use console.
643 */
644 pmap_devmap_bootstrap(l1pagetable, marvell_devmap);
645
646 /*
647 * Now we have the real page tables in place so we can switch to them.
648 * Once this is done we will be running with the REAL kernel page
649 * tables.
650 */
651
652 /* Switch tables */
653 #ifdef VERBOSE_INIT_ARM
654 printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
655 #endif
656
657 cpu_setttb(kernel_l1pt.pv_pa);
658 cpu_tlb_flushID();
659 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
660
661 /*
662 * Moved from cpu_startup() as data_abort_handler() references
663 * this during uvm init.
664 */
665 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
666
667 #ifdef VERBOSE_INIT_ARM
668 printf("bootstrap done.\n");
669 #endif
670
671 arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
672
673 /*
674 * Pages were allocated during the secondary bootstrap for the
675 * stacks for different CPU modes.
676 * We must now set the r13 registers in the different CPU modes to
677 * point to these stacks.
678 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
679 * of the stack memory.
680 */
681 #ifdef VERBOSE_INIT_ARM
682 printf("init subsystems: stacks ");
683 #endif
684
685 set_stackptr(PSR_IRQ32_MODE,
686 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
687 set_stackptr(PSR_ABT32_MODE,
688 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
689 set_stackptr(PSR_UND32_MODE,
690 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
691
692 /*
693 * Well we should set a data abort handler.
694 * Once things get going this will change as we will need a proper
695 * handler.
696 * Until then we will use a handler that just panics but tells us
697 * why.
698 * Initialisation of the vectors will just panic on a data abort.
699 * This just fills in a slightly better one.
700 */
701 #ifdef VERBOSE_INIT_ARM
702 printf("vectors ");
703 #endif
704 data_abort_handler_address = (u_int)data_abort_handler;
705 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
706 undefined_handler_address = (u_int)undefinedinstruction_bounce;
707
708 /* Initialise the undefined instruction handlers */
709 #ifdef VERBOSE_INIT_ARM
710 printf("undefined ");
711 #endif
712 undefined_init();
713
714 /* Load memory into UVM. */
715 #ifdef VERBOSE_INIT_ARM
716 printf("page ");
717 #endif
718 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
719 uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
720 atop(physical_freestart), atop(physical_freeend),
721 VM_FREELIST_DEFAULT);
722
723 /* Boot strap pmap telling it where the kernel page table is */
724 #ifdef VERBOSE_INIT_ARM
725 printf("pmap ");
726 #endif
727 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
728
729 #ifdef VERBOSE_INIT_ARM
730 printf("done.\n");
731 #endif
732
733 #ifdef __HAVE_MEMORY_DISK__
734 md_root_setconf(memory_disk, sizeof memory_disk);
735 #endif
736
737 boot_args = bootargs;
738 parse_mi_bootargs(boot_args);
739
740 #ifdef BOOTHOWTO
741 boothowto |= BOOTHOWTO;
742 #endif
743
744 #ifdef KGDB
745 if (boothowto & RB_KDB) {
746 kgdb_debug_init = 1;
747 kgdb_connect(1);
748 }
749 #endif
750
751 #ifdef DDB
752 db_machine_init();
753 if (boothowto & RB_KDB)
754 Debugger();
755 #endif
756
757 /* we've a specific device_register routine */
758 evbarm_device_register = marvell_device_register;
759
760 /* We return the new stack pointer address */
761 return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
762 }
763
764 void
765 consinit(void)
766 {
767 static int consinit_called = 0;
768
769 if (consinit_called != 0)
770 return;
771
772 consinit_called = 1;
773
774 #if NCOM > 0
775 {
776 extern int mvuart_cnattach(bus_space_tag_t, bus_addr_t, int,
777 uint32_t, int);
778
779 if (mvuart_cnattach(&mvsoc_bs_tag,
780 MARVELL_INTERREGS_VBASE + MVSOC_COM0_BASE,
781 comcnspeed, mvTclk, comcnmode))
782 panic("can't init serial console");
783 }
784 #else
785 panic("serial console not configured");
786 #endif
787 }
788
789
790 static void
791 marvell_device_register(device_t dev, void *aux)
792 {
793 prop_dictionary_t dict = device_properties(dev);
794
795 #if NCOM > 0
796 if (device_is_a(dev, "com") &&
797 device_is_a(device_parent(dev), "mvsoc"))
798 prop_dictionary_set_uint32(dict, "frequency", mvTclk);
799 #endif
800 if (device_is_a(dev, "gtidmac"))
801 prop_dictionary_set_uint32(dict,
802 "dmb_speed", mvTclk * sizeof(uint32_t)); /* XXXXXX */
803 #if NGTPCI > 0 && defined(ORION)
804 if (device_is_a(dev, "gtpci")) {
805 extern struct bus_space
806 orion_pci_io_bs_tag, orion_pci_mem_bs_tag;
807 extern struct arm32_pci_chipset arm32_gtpci_chipset;
808
809 prop_data_t io_bs_tag, mem_bs_tag, pc;
810 prop_array_t int2gpp;
811 prop_number_t gpp;
812 uint64_t start, end;
813 int i, j;
814 static struct {
815 const char *boardtype;
816 int pin[PCI_INTERRUPT_PIN_MAX];
817 } hints[] = {
818 { "kuronas_x4",
819 { 11, PCI_INTERRUPT_PIN_NONE } },
820
821 { NULL,
822 { PCI_INTERRUPT_PIN_NONE } },
823 };
824
825 arm32_gtpci_chipset.pc_conf_v = device_private(dev);
826 arm32_gtpci_chipset.pc_intr_v = device_private(dev);
827
828 io_bs_tag = prop_data_create_data_nocopy(
829 &orion_pci_io_bs_tag, sizeof(struct bus_space));
830 KASSERT(io_bs_tag != NULL);
831 prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
832 prop_object_release(io_bs_tag);
833 mem_bs_tag = prop_data_create_data_nocopy(
834 &orion_pci_mem_bs_tag, sizeof(struct bus_space));
835 KASSERT(mem_bs_tag != NULL);
836 prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
837 prop_object_release(mem_bs_tag);
838
839 pc = prop_data_create_data_nocopy(&arm32_gtpci_chipset,
840 sizeof(struct arm32_pci_chipset));
841 KASSERT(pc != NULL);
842 prop_dictionary_set(dict, "pci-chipset", pc);
843 prop_object_release(pc);
844
845 marvell_startend_by_tag(ORION_TAG_PCI_IO, &start, &end);
846 prop_dictionary_set_uint64(dict, "iostart", start);
847 prop_dictionary_set_uint64(dict, "ioend", end);
848 marvell_startend_by_tag(ORION_TAG_PCI_MEM, &start, &end);
849 prop_dictionary_set_uint64(dict, "memstart", start);
850 prop_dictionary_set_uint64(dict, "memend", end);
851 prop_dictionary_set_uint32(dict,
852 "cache-line-size", arm_dcache_align);
853
854 /* Setup the hint for interrupt-pin. */
855 #define BDSTR(s) _BDSTR(s)
856 #define _BDSTR(s) #s
857 #define THIS_BOARD(str) (strcmp(str, BDSTR(EVBARM_BOARDTYPE)) == 0)
858 for (i = 0; hints[i].boardtype != NULL; i++)
859 if (THIS_BOARD(hints[i].boardtype))
860 break;
861 if (hints[i].boardtype == NULL)
862 return;
863
864 int2gpp =
865 prop_array_create_with_capacity(PCI_INTERRUPT_PIN_MAX + 1);
866
867 /* first set dummy */
868 gpp = prop_number_create_integer(0);
869 prop_array_add(int2gpp, gpp);
870 prop_object_release(gpp);
871
872 for (j = 0; hints[i].pin[j] != PCI_INTERRUPT_PIN_NONE; j++) {
873 gpp = prop_number_create_integer(hints[i].pin[j]);
874 prop_array_add(int2gpp, gpp);
875 prop_object_release(gpp);
876 }
877 prop_dictionary_set(dict, "int2gpp", int2gpp);
878 }
879 #endif /* NGTPCI > 0 && defined(ORION) */
880 #if NMVPEX > 0
881 if (device_is_a(dev, "mvpex")) {
882 #ifdef ORION
883 extern struct bus_space
884 orion_pex0_io_bs_tag, orion_pex0_mem_bs_tag,
885 orion_pex1_io_bs_tag, orion_pex1_mem_bs_tag;
886 #endif
887 #ifdef KIRKWOOD
888 extern struct bus_space
889 kirkwood_pex_io_bs_tag, kirkwood_pex_mem_bs_tag,
890 kirkwood_pex1_io_bs_tag, kirkwood_pex1_mem_bs_tag;
891 #endif
892 extern struct arm32_pci_chipset arm32_mvpex0_chipset;
893 #if defined(ORION) || defined(KIRKWOOD)
894 extern struct arm32_pci_chipset arm32_mvpex1_chipset;
895
896 struct marvell_attach_args *mva = aux;
897 #endif
898 struct bus_space *mvpex_io_bs_tag, *mvpex_mem_bs_tag;
899 struct arm32_pci_chipset *arm32_mvpex_chipset;
900 prop_data_t io_bs_tag, mem_bs_tag, pc;
901 uint64_t start, end;
902 int iotag, memtag;
903
904 switch (mvsoc_model()) {
905 #ifdef ORION
906 case MARVELL_ORION_1_88F5180N:
907 case MARVELL_ORION_1_88F5181:
908 case MARVELL_ORION_1_88F5182:
909 case MARVELL_ORION_1_88W8660:
910 case MARVELL_ORION_2_88F5281:
911 if (mva->mva_offset == MVSOC_PEX_BASE) {
912 mvpex_io_bs_tag = &orion_pex0_io_bs_tag;
913 mvpex_mem_bs_tag = &orion_pex0_mem_bs_tag;
914 arm32_mvpex_chipset = &arm32_mvpex0_chipset;
915 iotag = ORION_TAG_PEX0_IO;
916 memtag = ORION_TAG_PEX0_MEM;
917 } else {
918 mvpex_io_bs_tag = &orion_pex1_io_bs_tag;
919 mvpex_mem_bs_tag = &orion_pex1_mem_bs_tag;
920 arm32_mvpex_chipset = &arm32_mvpex1_chipset;
921 iotag = ORION_TAG_PEX1_IO;
922 memtag = ORION_TAG_PEX1_MEM;
923 }
924 break;
925 #endif
926
927 #ifdef KIRKWOOD
928 case MARVELL_KIRKWOOD_88F6282:
929 if (mva->mva_offset != MVSOC_PEX_BASE) {
930 mvpex_io_bs_tag = &kirkwood_pex1_io_bs_tag;
931 mvpex_mem_bs_tag = &kirkwood_pex1_mem_bs_tag;
932 arm32_mvpex_chipset = &arm32_mvpex1_chipset;
933 iotag = KIRKWOOD_TAG_PEX1_IO;
934 memtag = KIRKWOOD_TAG_PEX1_MEM;
935 break;
936 }
937
938 /* FALLTHROUGH */
939
940 case MARVELL_KIRKWOOD_88F6180:
941 case MARVELL_KIRKWOOD_88F6192:
942 case MARVELL_KIRKWOOD_88F6281:
943 mvpex_io_bs_tag = &kirkwood_pex_io_bs_tag;
944 mvpex_mem_bs_tag = &kirkwood_pex_mem_bs_tag;
945 arm32_mvpex_chipset = &arm32_mvpex0_chipset;
946 iotag = KIRKWOOD_TAG_PEX_IO;
947 memtag = KIRKWOOD_TAG_PEX_MEM;
948 break;
949 #endif
950
951 default:
952 return;
953 }
954
955 arm32_mvpex_chipset->pc_conf_v = device_private(dev);
956 arm32_mvpex_chipset->pc_intr_v = device_private(dev);
957
958 io_bs_tag = prop_data_create_data_nocopy(
959 mvpex_io_bs_tag, sizeof(struct bus_space));
960 KASSERT(io_bs_tag != NULL);
961 prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
962 prop_object_release(io_bs_tag);
963 mem_bs_tag = prop_data_create_data_nocopy(
964 mvpex_mem_bs_tag, sizeof(struct bus_space));
965 KASSERT(mem_bs_tag != NULL);
966 prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
967 prop_object_release(mem_bs_tag);
968
969 pc = prop_data_create_data_nocopy(arm32_mvpex_chipset,
970 sizeof(struct arm32_pci_chipset));
971 KASSERT(pc != NULL);
972 prop_dictionary_set(dict, "pci-chipset", pc);
973 prop_object_release(pc);
974
975 marvell_startend_by_tag(iotag, &start, &end);
976 prop_dictionary_set_uint64(dict, "iostart", start);
977 prop_dictionary_set_uint64(dict, "ioend", end);
978 marvell_startend_by_tag(memtag, &start, &end);
979 prop_dictionary_set_uint64(dict, "memstart", start);
980 prop_dictionary_set_uint64(dict, "memend", end);
981 prop_dictionary_set_uint32(dict,
982 "cache-line-size", arm_dcache_align);
983 }
984 #endif
985 }
986
987 #if NGTPCI > 0 || NMVPEX > 0
988 static void
989 marvell_startend_by_tag(int tag, uint64_t *start, uint64_t *end)
990 {
991 uint32_t base, size;
992 int win;
993
994 win = mvsoc_target(tag, NULL, NULL, &base, &size);
995 if (size != 0) {
996 if (win < nremap)
997 *start = read_mlmbreg(MVSOC_MLMB_WRLR(win)) |
998 ((read_mlmbreg(MVSOC_MLMB_WRHR(win)) << 16) << 16);
999 else
1000 *start = base;
1001 *end = *start + size - 1;
1002 }
1003 }
1004 #endif
1005