1254885Sdumbbell/* $NetBSD: iyonix_machdep.c,v 1.34 2023/10/12 11:33:39 skrll Exp $ */ 2254885Sdumbbell 3254885Sdumbbell/* 4254885Sdumbbell * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc. 5254885Sdumbbell * All rights reserved. 6254885Sdumbbell * 7254885Sdumbbell * Based on code written by Jason R. Thorpe and Steve C. Woodford for 8254885Sdumbbell * Wasabi Systems, Inc. 9254885Sdumbbell * 10254885Sdumbbell * Redistribution and use in source and binary forms, with or without 11254885Sdumbbell * modification, are permitted provided that the following conditions 12254885Sdumbbell * are met: 13254885Sdumbbell * 1. Redistributions of source code must retain the above copyright 14254885Sdumbbell * notice, this list of conditions and the following disclaimer. 15254885Sdumbbell * 2. Redistributions in binary form must reproduce the above copyright 16254885Sdumbbell * notice, this list of conditions and the following disclaimer in the 17254885Sdumbbell * documentation and/or other materials provided with the distribution. 18254885Sdumbbell * 3. All advertising materials mentioning features or use of this software 19254885Sdumbbell * must display the following acknowledgement: 20254885Sdumbbell * This product includes software developed for the NetBSD Project by 21254885Sdumbbell * Wasabi Systems, Inc. 22254885Sdumbbell * 4. The name of Wasabi Systems, Inc. may not be used to endorse 23254885Sdumbbell * or promote products derived from this software without specific prior 24254885Sdumbbell * written permission. 25254885Sdumbbell * 26254885Sdumbbell * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 27254885Sdumbbell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28254885Sdumbbell * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29254885Sdumbbell * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 30254885Sdumbbell * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31254885Sdumbbell * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32254885Sdumbbell * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33254885Sdumbbell * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34254885Sdumbbell * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35282199Sdumbbell * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36282199Sdumbbell * POSSIBILITY OF SUCH DAMAGE. 37254885Sdumbbell */ 38254885Sdumbbell 39254885Sdumbbell/* 40254885Sdumbbell * Copyright (c) 1997,1998 Mark Brinicombe. 41254885Sdumbbell * Copyright (c) 1997,1998 Causality Limited. 42254885Sdumbbell * All rights reserved. 43254885Sdumbbell * 44254885Sdumbbell * Redistribution and use in source and binary forms, with or without 45254885Sdumbbell * modification, are permitted provided that the following conditions 46254885Sdumbbell * are met: 47254885Sdumbbell * 1. Redistributions of source code must retain the above copyright 48254885Sdumbbell * notice, this list of conditions and the following disclaimer. 49254885Sdumbbell * 2. Redistributions in binary form must reproduce the above copyright 50254885Sdumbbell * notice, this list of conditions and the following disclaimer in the 51254885Sdumbbell * documentation and/or other materials provided with the distribution. 52254885Sdumbbell * 3. All advertising materials mentioning features or use of this software 53254885Sdumbbell * must display the following acknowledgement: 54254885Sdumbbell * This product includes software developed by Mark Brinicombe 55254885Sdumbbell * for the NetBSD Project. 56254885Sdumbbell * 4. The name of the company nor the name of the author may be used to 57254885Sdumbbell * endorse or promote products derived from this software without specific 58254885Sdumbbell * prior written permission. 59254885Sdumbbell * 60254885Sdumbbell * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 61254885Sdumbbell * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 62254885Sdumbbell * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 63254885Sdumbbell * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 64254885Sdumbbell * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 65254885Sdumbbell * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 66254885Sdumbbell * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67254885Sdumbbell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68254885Sdumbbell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69254885Sdumbbell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70254885Sdumbbell * SUCH DAMAGE. 71254885Sdumbbell * 72254885Sdumbbell * Machine dependent functions for kernel setup for Iyonix. 73254885Sdumbbell */ 74254885Sdumbbell 75254885Sdumbbell#include <sys/cdefs.h> 76254885Sdumbbell__KERNEL_RCSID(0, "$NetBSD: iyonix_machdep.c,v 1.34 2023/10/12 11:33:39 skrll Exp $"); 77254885Sdumbbell 78254885Sdumbbell#include "opt_ddb.h" 79254885Sdumbbell#include "opt_kgdb.h" 80254885Sdumbbell 81254885Sdumbbell#include <sys/param.h> 82254885Sdumbbell#include <sys/device.h> 83254885Sdumbbell#include <sys/systm.h> 84254885Sdumbbell#include <sys/kernel.h> 85254885Sdumbbell#include <sys/exec.h> 86254885Sdumbbell#include <sys/proc.h> 87254885Sdumbbell#include <sys/msgbuf.h> 88254885Sdumbbell#include <sys/reboot.h> 89254885Sdumbbell#include <sys/termios.h> 90254885Sdumbbell#include <sys/ksyms.h> 91254885Sdumbbell#include <sys/bus.h> 92254885Sdumbbell#include <sys/cpu.h> 93254885Sdumbbell 94254885Sdumbbell#include <uvm/uvm_extern.h> 95254885Sdumbbell 96254885Sdumbbell#include <dev/cons.h> 97254885Sdumbbell 98254885Sdumbbell#include <dev/pci/ppbreg.h> 99254885Sdumbbell#include <dev/ic/i8259reg.h> 100254885Sdumbbell 101254885Sdumbbell#include <net/if.h> 102254885Sdumbbell#include <net/if_ether.h> 103254885Sdumbbell 104254885Sdumbbell#include <machine/db_machdep.h> 105254885Sdumbbell#include <ddb/db_sym.h> 106254885Sdumbbell#include <ddb/db_extern.h> 107254885Sdumbbell 108254885Sdumbbell#include <acorn32/include/bootconfig.h> 109254885Sdumbbell#include <arm/locore.h> 110254885Sdumbbell#include <arm/undefined.h> 111254885Sdumbbell 112254885Sdumbbell#include <arm/arm32/machdep.h> 113254885Sdumbbell 114254885Sdumbbell#include <arm/xscale/i80321reg.h> 115254885Sdumbbell#include <arm/xscale/i80321var.h> 116254885Sdumbbell 117254885Sdumbbell#include <iyonix/iyonix/iyonixreg.h> 118254885Sdumbbell#include <iyonix/iyonix/obiovar.h> 119254885Sdumbbell 120254885Sdumbbell#include "ksyms.h" 121254885Sdumbbell 122254885Sdumbbell#define KERNEL_TEXT_BASE KERNEL_BASE 123254885Sdumbbell#define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000) 124254885Sdumbbell 125254885Sdumbbell/* 126254885Sdumbbell * The range 0xc1000000 - 0xccffffff is available for kernel VM space 127254885Sdumbbell * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff 128254885Sdumbbell */ 129254885Sdumbbell#define KERNEL_VM_SIZE 0x0C000000 130254885Sdumbbell 131254885Sdumbbellstruct bootconfig bootconfig; /* Boot config storage */ 132254885Sdumbbell 133254885Sdumbbellchar *boot_args; 134254885Sdumbbell 135254885Sdumbbellvaddr_t physical_start; 136254885Sdumbbellvaddr_t physical_freestart; 137254885Sdumbbellvaddr_t physical_freeend; 138254885Sdumbbellvaddr_t physical_end; 139254885Sdumbbellu_int free_pages; 140254885Sdumbbellvaddr_t pagetables_start; 141254885Sdumbbell 142254885Sdumbbell/*int debug_flags;*/ 143254885Sdumbbell#ifndef PMAP_STATIC_L1S 144254885Sdumbbellint max_processes = 64; /* Default number */ 145254885Sdumbbell#endif /* !PMAP_STATIC_L1S */ 146254885Sdumbbell 147254885Sdumbbell/* Physical and virtual addresses for some global pages */ 148254885Sdumbbellpv_addr_t minidataclean; 149254885Sdumbbell 150254885Sdumbbellpaddr_t msgbufphys; 151254885Sdumbbell 152254885Sdumbbell#define KERNEL_PT_SYS 0 /* L2 table for mapping zero page */ 153254885Sdumbbell 154254885Sdumbbell#define KERNEL_PT_KERNEL 1 /* L2 table for mapping kernel */ 155254885Sdumbbell#define KERNEL_PT_KERNEL_NUM 4 156254885Sdumbbell 157254885Sdumbbell /* L2 table for mapping i80321 */ 158254885Sdumbbell#define KERNEL_PT_IOPXS (KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM) 159254885Sdumbbell 160254885Sdumbbell /* L2 tables for mapping kernel VM */ 161254885Sdumbbell#define KERNEL_PT_VMDATA (KERNEL_PT_IOPXS + 1) 162254885Sdumbbell#define KERNEL_PT_VMDATA_NUM 4 /* start with 16MB of KVM */ 163254885Sdumbbell#define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM) 164254885Sdumbbell 165254885Sdumbbellpv_addr_t kernel_pt_table[NUM_KERNEL_PTS]; 166254885Sdumbbell 167254885Sdumbbellchar iyonix_macaddr[ETHER_ADDR_LEN]; 168254885Sdumbbell 169254885Sdumbbellchar boot_consdev[16]; 170254885Sdumbbell 171254885Sdumbbell/* Prototypes */ 172254885Sdumbbell 173254885Sdumbbellvoid iyonix_pic_init(void); 174254885Sdumbbellvoid iyonix_read_machineid(void); 175254885Sdumbbell 176254885Sdumbbellvoid consinit(void); 177254885Sdumbbell 178254885Sdumbbellstatic void consinit_com(const char *consdev); 179254885Sdumbbellstatic void consinit_genfb(const char *consdev); 180254885Sdumbbellstatic void process_kernel_args(void); 181254885Sdumbbellstatic void parse_iyonix_bootargs(char *args); 182254885Sdumbbell 183254885Sdumbbell#include "com.h" 184254885Sdumbbell#if NCOM > 0 185254885Sdumbbell#include <dev/ic/comreg.h> 186254885Sdumbbell#include <dev/ic/comvar.h> 187254885Sdumbbell#endif 188254885Sdumbbell 189254885Sdumbbell#include "genfb.h" 190254885Sdumbbell 191254885Sdumbbell#if (NGENFB == 0) && (NCOM == 0) 192254885Sdumbbell# error "No valid console device (com or genfb)" 193254885Sdumbbell#elif defined(COMCONSOLE) || (NGENFB == 0) 194254885Sdumbbell# define DEFAULT_CONSDEV "com" 195254885Sdumbbell#else 196254885Sdumbbell# define DEFAULT_CONSDEV "genfb" 197254885Sdumbbell#endif 198254885Sdumbbell 199254885Sdumbbell/* 200254885Sdumbbell * Define the default console speed for the machine. 201254885Sdumbbell */ 202254885Sdumbbell#ifndef CONSPEED 203254885Sdumbbell#define CONSPEED B9600 204254885Sdumbbell#endif /* ! CONSPEED */ 205254885Sdumbbell 206254885Sdumbbell#ifndef CONUNIT 207254885Sdumbbell#define CONUNIT 0 208254885Sdumbbell#endif 209254885Sdumbbell 210254885Sdumbbell#ifndef CONMODE 211254885Sdumbbell#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 212254885Sdumbbell#endif 213254885Sdumbbell 214254885Sdumbbellint comcnspeed = CONSPEED; 215254885Sdumbbellint comcnmode = CONMODE; 216254885Sdumbbellint comcnunit = CONUNIT; 217254885Sdumbbell 218254885Sdumbbell#if KGDB 219254885Sdumbbell#ifndef KGDB_DEVNAME 220254885Sdumbbell#error Must define KGDB_DEVNAME 221254885Sdumbbell#endif 222254885Sdumbbellconst char kgdb_devname[] = KGDB_DEVNAME; 223254885Sdumbbell 224254885Sdumbbell#ifndef KGDB_DEVADDR 225254885Sdumbbell#error Must define KGDB_DEVADDR 226254885Sdumbbell#endif 227254885Sdumbbellunsigned long kgdb_devaddr = KGDB_DEVADDR; 228254885Sdumbbell 229254885Sdumbbell#ifndef KGDB_DEVRATE 230254885Sdumbbell#define KGDB_DEVRATE CONSPEED 231254885Sdumbbell#endif 232254885Sdumbbellint kgdb_devrate = KGDB_DEVRATE; 233254885Sdumbbell 234254885Sdumbbell#ifndef KGDB_DEVMODE 235254885Sdumbbell#define KGDB_DEVMODE CONMODE 236254885Sdumbbell#endif 237254885Sdumbbellint kgdb_devmode = KGDB_DEVMODE; 238254885Sdumbbell#endif /* KGDB */ 239254885Sdumbbell 240254885Sdumbbell/* 241254885Sdumbbell * void cpu_reboot(int howto, char *bootstr) 242254885Sdumbbell * 243254885Sdumbbell * Reboots the system 244254885Sdumbbell * 245254885Sdumbbell * Deal with any syncing, unmounting, dumping and shutdown hooks, 246254885Sdumbbell * then reset the CPU. 247254885Sdumbbell */ 248254885Sdumbbellvoid 249254885Sdumbbellcpu_reboot(int howto, char *bootstr) 250254885Sdumbbell{ 251254885Sdumbbell 252254885Sdumbbell /* 253254885Sdumbbell * If we are still cold then hit the air brakes 254254885Sdumbbell * and crash to earth fast 255254885Sdumbbell */ 256254885Sdumbbell if (cold) { 257254885Sdumbbell doshutdownhooks(); 258254885Sdumbbell pmf_system_shutdown(boothowto); 259254885Sdumbbell printf("The operating system has halted.\n"); 260254885Sdumbbell printf("Please press any key to reboot.\n\n"); 261254885Sdumbbell cngetc(); 262254885Sdumbbell printf("rebooting...\n"); 263254885Sdumbbell goto reset; 264254885Sdumbbell } 265254885Sdumbbell 266254885Sdumbbell /* Disable console buffering */ 267254885Sdumbbell 268254885Sdumbbell /* 269254885Sdumbbell * If RB_NOSYNC was not specified sync the discs. 270254885Sdumbbell * Note: Unless cold is set to 1 here, syslogd will die during the 271254885Sdumbbell * unmount. It looks like syslogd is getting woken up only to find 272254885Sdumbbell * that it cannot page part of the binary in as the filesystem has 273254885Sdumbbell * been unmounted. 274254885Sdumbbell */ 275254885Sdumbbell if (!(howto & RB_NOSYNC)) 276254885Sdumbbell bootsync(); 277254885Sdumbbell 278254885Sdumbbell /* Say NO to interrupts */ 279254885Sdumbbell splhigh(); 280254885Sdumbbell 281254885Sdumbbell /* Do a dump if requested. */ 282254885Sdumbbell if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 283254885Sdumbbell dumpsys(); 284254885Sdumbbell 285254885Sdumbbell /* Run any shutdown hooks */ 286254885Sdumbbell doshutdownhooks(); 287254885Sdumbbell 288254885Sdumbbell pmf_system_shutdown(boothowto); 289254885Sdumbbell 290254885Sdumbbell /* Make sure IRQ's are disabled */ 291254885Sdumbbell IRQdisable; 292254885Sdumbbell 293254885Sdumbbell if (howto & RB_HALT) { 294254885Sdumbbell printf("The operating system has halted.\n"); 295254885Sdumbbell printf("Please press any key to reboot.\n\n"); 296254885Sdumbbell cngetc(); 297254885Sdumbbell } 298254885Sdumbbell 299254885Sdumbbell printf("rebooting...\n\r"); 300254885Sdumbbell reset: 301254885Sdumbbell /* 302254885Sdumbbell * Make really really sure that all interrupts are disabled, 303254885Sdumbbell * and poke the Internal Bus and Peripheral Bus reset lines. 304254885Sdumbbell */ 305254885Sdumbbell (void) disable_interrupts(I32_bit|F32_bit); 306254885Sdumbbell *(volatile uint32_t *)(IYONIX_80321_VBASE + VERDE_ATU_BASE + 307254885Sdumbbell ATU_PCSR) = PCSR_RIB | PCSR_RPB; 308254885Sdumbbell 309254885Sdumbbell /* ...and if that didn't work, just croak. */ 310254885Sdumbbell printf("RESET FAILED!\n"); 311254885Sdumbbell for (;;); 312254885Sdumbbell} 313254885Sdumbbell 314254885Sdumbbell/* Static device mappings. */ 315254885Sdumbbellstatic const struct pmap_devmap iyonix_devmap[] = { 316254885Sdumbbell /* 317254885Sdumbbell * Map the on-board devices VA == PA so that we can access them 318254885Sdumbbell * with the MMU on or off. 319254885Sdumbbell */ 320254885Sdumbbell DEVMAP_ENTRY( 321254885Sdumbbell IYONIX_OBIO_BASE, 322254885Sdumbbell IYONIX_OBIO_BASE, 323254885Sdumbbell IYONIX_OBIO_SIZE 324254885Sdumbbell ), 325254885Sdumbbell 326254885Sdumbbell DEVMAP_ENTRY( 327254885Sdumbbell IYONIX_IOW_VBASE, 328254885Sdumbbell VERDE_OUT_XLATE_IO_WIN0_BASE, 329254885Sdumbbell VERDE_OUT_XLATE_IO_WIN_SIZE 330254885Sdumbbell ), 331254885Sdumbbell 332254885Sdumbbell DEVMAP_ENTRY( 333254885Sdumbbell IYONIX_80321_VBASE, 334254885Sdumbbell VERDE_PMMR_BASE, 335254885Sdumbbell VERDE_PMMR_SIZE 336254885Sdumbbell ), 337254885Sdumbbell 338254885Sdumbbell DEVMAP_ENTRY( 339254885Sdumbbell IYONIX_FLASH_BASE, 340254885Sdumbbell IYONIX_FLASH_BASE, 341254885Sdumbbell IYONIX_FLASH_SIZE 342254885Sdumbbell ), 343254885Sdumbbell 344254885Sdumbbell DEVMAP_ENTRY_END 345254885Sdumbbell}; 346254885Sdumbbell 347254885Sdumbbell/* Read out the Machine ID from the flash, and stash it away for later use. */ 348254885Sdumbbell 349254885Sdumbbellvoid 350254885Sdumbbelliyonix_read_machineid(void) 351254885Sdumbbell{ 352254885Sdumbbell volatile uint32_t *flashbase = (uint32_t *)IYONIX_FLASH_BASE; 353254885Sdumbbell volatile uint16_t *flashword = (uint16_t *)IYONIX_FLASH_BASE; 354254885Sdumbbell union { 355254885Sdumbbell uint32_t w[2]; 356254885Sdumbbell uint8_t b[8]; 357254885Sdumbbell } machid; 358254885Sdumbbell 359254885Sdumbbell /* Enter SecSi Sector Region */ 360254885Sdumbbell flashword[0x555] = 0xAA; 361254885Sdumbbell flashword[0x2AA] = 0x55; 362254885Sdumbbell flashword[0x555] = 0x88; 363282199Sdumbbell 364254885Sdumbbell machid.w[0] = flashbase[0]; 365254885Sdumbbell machid.w[1] = flashbase[1]; 366254885Sdumbbell 367254885Sdumbbell iyonix_macaddr[0] = machid.b[6]; 368254885Sdumbbell iyonix_macaddr[1] = machid.b[5]; 369254885Sdumbbell iyonix_macaddr[2] = machid.b[4]; 370254885Sdumbbell iyonix_macaddr[3] = machid.b[3]; 371254885Sdumbbell iyonix_macaddr[4] = machid.b[2]; 372254885Sdumbbell iyonix_macaddr[5] = machid.b[1]; 373282199Sdumbbell 374254885Sdumbbell /* Exit SecSi Sector Region */ 375254885Sdumbbell flashword[0x555] = 0xAA; 376254885Sdumbbell flashword[0x2AA] = 0x55; 377254885Sdumbbell flashword[0x555] = 0x90; 378254885Sdumbbell flashword[0x555] = 0x00; 379254885Sdumbbell} 380254885Sdumbbell 381254885Sdumbbell#define IYONIX_PIC_WRITE(a,v) (*((char *)IYONIX_OBIO_BASE + (a)) = (v)) 382254885Sdumbbell 383254885Sdumbbellvoid 384254885Sdumbbelliyonix_pic_init(void) 385254885Sdumbbell{ 386254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT); 387254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW2, ICW2_IRL(0)); 388254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW3, ICW3_CASCADE(2)); 389254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW4, ICW4_8086); 390254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_OCW1, 0x0); /* Unmask */ 391254885Sdumbbell 392254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT); 393254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW2, ICW2_IRL(0)); 394254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW3, ICW3_CASCADE(1)); 395254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW4, ICW4_8086); 396254885Sdumbbell IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_OCW1, 0x0); /* Unmask */ 397254885Sdumbbell 398254885Sdumbbell} 399254885Sdumbbell 400254885Sdumbbell/* 401254885Sdumbbell * vaddr_t initarm(...) 402254885Sdumbbell * 403254885Sdumbbell * Initial entry point on startup. This gets called before main() is 404254885Sdumbbell * entered. 405254885Sdumbbell * It should be responsible for setting up everything that must be 406254885Sdumbbell * in place when main is called. 407254885Sdumbbell * This includes 408254885Sdumbbell * Taking a copy of the boot configuration structure. 409254885Sdumbbell * Initialising the physical console so characters can be printed. 410254885Sdumbbell * Setting up page tables for the kernel 411254885Sdumbbell * Initialising interrupt controllers to a sane default state 412254885Sdumbbell */ 413254885Sdumbbellvaddr_t 414254885Sdumbbellinitarm(void *arg) 415254885Sdumbbell{ 416254885Sdumbbell struct bootconfig *passed_bootconfig = arg; 417254885Sdumbbell extern char _end[]; 418254885Sdumbbell int loop; 419254885Sdumbbell int loop1; 420254885Sdumbbell u_int l1pagetable; 421254885Sdumbbell paddr_t memstart = 0; 422254885Sdumbbell psize_t memsize = 0; 423254885Sdumbbell 424254885Sdumbbell /* Calibrate the delay loop. */ 425254885Sdumbbell i80321_calibrate_delay(); 426254885Sdumbbell 427254885Sdumbbell /* Ensure bootconfig has valid magic */ 428254885Sdumbbell if (passed_bootconfig->magic != BOOTCONFIG_MAGIC) 429254885Sdumbbell printf("Bad bootconfig magic: %x\n", bootconfig.magic); 430254885Sdumbbell 431254885Sdumbbell bootconfig = *passed_bootconfig; 432254885Sdumbbell 433254885Sdumbbell /* Fake bootconfig structure for anything that still needs it */ 434254885Sdumbbell /* XXX must make the memory description h/w independent */ 435254885Sdumbbell bootconfig.dram[0].address = memstart; 436254885Sdumbbell bootconfig.dram[0].pages = memsize / PAGE_SIZE; 437254885Sdumbbell bootconfig.dramblocks = 1; 438254885Sdumbbell 439254885Sdumbbell /* process arguments - can update boothowto */ 440254885Sdumbbell process_kernel_args(); 441254885Sdumbbell 442254885Sdumbbell /* 443254885Sdumbbell * Since we map the on-board devices VA==PA, and the kernel 444254885Sdumbbell * is running VA==PA, it's possible for us to initialize 445254885Sdumbbell * the console now. 446254885Sdumbbell */ 447254885Sdumbbell consinit(); 448254885Sdumbbell 449254885Sdumbbell#ifdef VERBOSE_INIT_ARM 450254885Sdumbbell /* Talk to the user */ 451254885Sdumbbell printf("\nNetBSD/iyonix booting ...\n"); 452254885Sdumbbell#endif 453254885Sdumbbell 454254885Sdumbbell /* 455254885Sdumbbell * Heads up ... Setup the CPU / MMU / TLB functions 456254885Sdumbbell */ 457254885Sdumbbell if (set_cpufuncs()) 458254885Sdumbbell panic("cpu not recognized!"); 459254885Sdumbbell 460254885Sdumbbell /* 461254885Sdumbbell * We are currently running with the MMU enabled and the 462254885Sdumbbell * entire address space mapped VA==PA. 463254885Sdumbbell */ 464254885Sdumbbell 465254885Sdumbbell /* 466254885Sdumbbell * Fetch the SDRAM start/size from the i80321 SDRAM configuration 467254885Sdumbbell * registers. 468254885Sdumbbell */ 469254885Sdumbbell i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE, 470254885Sdumbbell &memstart, &memsize); 471254885Sdumbbell 472254885Sdumbbell#ifdef VERBOSE_INIT_ARM 473254885Sdumbbell printf("initarm: Configuring system ...\n"); 474254885Sdumbbell#endif 475254885Sdumbbell 476254885Sdumbbell /* 477254885Sdumbbell * Set up the variables that define the availability of 478254885Sdumbbell * physical memory. 479254885Sdumbbell */ 480254885Sdumbbell physical_start = memstart; 481254885Sdumbbell physical_end = physical_start + memsize; 482254885Sdumbbell 483254885Sdumbbell physical_freestart = physical_start + 484254885Sdumbbell (((uintptr_t) _end - KERNEL_TEXT_BASE + PGOFSET) & ~PGOFSET); 485254885Sdumbbell physical_freeend = physical_end; 486254885Sdumbbell 487254885Sdumbbell physmem = (physical_end - physical_start) / PAGE_SIZE; 488254885Sdumbbell 489254885Sdumbbell#ifdef VERBOSE_INIT_ARM 490254885Sdumbbell /* Tell the user about the memory */ 491254885Sdumbbell printf("physmemory: %ld pages at 0x%08lx -> 0x%08lx\n", physmem, 492254885Sdumbbell physical_start, physical_end - 1); 493254885Sdumbbell#endif 494254885Sdumbbell 495254885Sdumbbell /* 496254885Sdumbbell * The kernel is loaded at the base of physical memory. We allocate 497254885Sdumbbell * pages upwards from the top of the kernel. 498254885Sdumbbell * 499254885Sdumbbell * We need to allocate some fixed page tables to get the kernel 500254885Sdumbbell * going. We allocate one page directory and a number of page 501254885Sdumbbell * tables and store the physical addresses in the kernel_pt_table 502254885Sdumbbell * array. 503254885Sdumbbell * 504254885Sdumbbell * The kernel page directory must be on a 16K boundary. The page 505254885Sdumbbell * tables must be on 4K boundaries. What we do is allocate the 506254885Sdumbbell * page directory on the first 16K boundary that we encounter, and 507254885Sdumbbell * the page tables on 4K boundaries otherwise. Since we allocate 508254885Sdumbbell * at least 3 L2 page tables, we are guaranteed to encounter at 509254885Sdumbbell * least one 16K aligned region. 510254885Sdumbbell */ 511254885Sdumbbell 512254885Sdumbbell#ifdef VERBOSE_INIT_ARM 513282199Sdumbbell printf("Allocating page tables\n"); 514254885Sdumbbell#endif 515254885Sdumbbell 516254885Sdumbbell free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE; 517254885Sdumbbell 518254885Sdumbbell#ifdef VERBOSE_INIT_ARM 519254885Sdumbbell printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n", 520254885Sdumbbell physical_freestart, free_pages, free_pages); 521254885Sdumbbell#endif 522282199Sdumbbell 523254885Sdumbbell /* Define a macro to simplify memory allocation */ 524254885Sdumbbell#define valloc_pages(var, np) \ 525254885Sdumbbell alloc_pages((var).pv_pa, (np)); \ 526254885Sdumbbell (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start; 527254885Sdumbbell 528254885Sdumbbell#define alloc_pages(var, np) \ 529254885Sdumbbell (var) = physical_freestart; \ 530254885Sdumbbell physical_freestart += ((np) * PAGE_SIZE); \ 531254885Sdumbbell if (physical_freeend < physical_freestart) \ 532254885Sdumbbell panic("initarm: out of memory"); \ 533254885Sdumbbell free_pages -= (np); \ 534254885Sdumbbell memset((char *)(var), 0, ((np) * PAGE_SIZE)); 535254885Sdumbbell 536254885Sdumbbell loop1 = 0; 537254885Sdumbbell kernel_l1pt.pv_pa = kernel_l1pt.pv_va = 0; 538254885Sdumbbell for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) { 539254885Sdumbbell /* Are we 16KB aligned for an L1 ? */ 540254885Sdumbbell if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0 541254885Sdumbbell && kernel_l1pt.pv_pa == 0) { 542254885Sdumbbell valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE); 543254885Sdumbbell } else { 544254885Sdumbbell valloc_pages(kernel_pt_table[loop1], 545254885Sdumbbell L2_TABLE_SIZE / PAGE_SIZE); 546254885Sdumbbell ++loop1; 547254885Sdumbbell } 548254885Sdumbbell } 549254885Sdumbbell 550254885Sdumbbell /* This should never be able to happen but better confirm that. */ 551254885Sdumbbell if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0) 552254885Sdumbbell panic("initarm: Failed to align the kernel page directory"); 553254885Sdumbbell 554254885Sdumbbell /* 555254885Sdumbbell * Allocate a page for the system page mapped to V0x00000000 556254885Sdumbbell * This page will just contain the system vectors and can be 557254885Sdumbbell * shared by all processes. 558254885Sdumbbell */ 559254885Sdumbbell alloc_pages(systempage.pv_pa, 1); 560254885Sdumbbell 561254885Sdumbbell /* Allocate stacks for all modes */ 562254885Sdumbbell valloc_pages(irqstack, IRQ_STACK_SIZE); 563254885Sdumbbell valloc_pages(abtstack, ABT_STACK_SIZE); 564254885Sdumbbell valloc_pages(undstack, UND_STACK_SIZE); 565254885Sdumbbell valloc_pages(kernelstack, UPAGES); 566254885Sdumbbell 567254885Sdumbbell /* Allocate enough pages for cleaning the Mini-Data cache. */ 568254885Sdumbbell#ifdef DIAGNOSTIC 569254885Sdumbbell KASSERT(xscale_minidata_clean_size <= PAGE_SIZE); 570254885Sdumbbell#endif 571254885Sdumbbell valloc_pages(minidataclean, 1); 572254885Sdumbbell 573254885Sdumbbell#ifdef VERBOSE_INIT_ARM 574254885Sdumbbell printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, 575254885Sdumbbell irqstack.pv_va); 576254885Sdumbbell printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, 577254885Sdumbbell abtstack.pv_va); 578254885Sdumbbell printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, 579254885Sdumbbell undstack.pv_va); 580254885Sdumbbell printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, 581254885Sdumbbell kernelstack.pv_va); 582254885Sdumbbell#endif 583254885Sdumbbell 584254885Sdumbbell alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE); 585254885Sdumbbell 586254885Sdumbbell /* 587254885Sdumbbell * Ok we have allocated physical pages for the primary kernel 588254885Sdumbbell * page tables 589254885Sdumbbell */ 590254885Sdumbbell 591254885Sdumbbell#ifdef VERBOSE_INIT_ARM 592254885Sdumbbell printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa); 593254885Sdumbbell#endif 594254885Sdumbbell 595254885Sdumbbell /* 596254885Sdumbbell * Now we start construction of the L1 page table 597254885Sdumbbell * We start by mapping the L2 page tables into the L1. 598254885Sdumbbell * This means that we can replace L1 mappings later on if necessary 599254885Sdumbbell */ 600254885Sdumbbell l1pagetable = kernel_l1pt.pv_pa; 601254885Sdumbbell 602254885Sdumbbell /* Map the L2 pages tables in the L1 page table */ 603254885Sdumbbell pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1), 604254885Sdumbbell &kernel_pt_table[KERNEL_PT_SYS]); 605254885Sdumbbell for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++) 606254885Sdumbbell pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000, 607254885Sdumbbell &kernel_pt_table[KERNEL_PT_KERNEL + loop]); 608254885Sdumbbell pmap_link_l2pt(l1pagetable, IYONIX_IOPXS_VBASE, 609254885Sdumbbell &kernel_pt_table[KERNEL_PT_IOPXS]); 610254885Sdumbbell for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++) 611254885Sdumbbell pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000, 612254885Sdumbbell &kernel_pt_table[KERNEL_PT_VMDATA + loop]); 613254885Sdumbbell 614254885Sdumbbell /* update the top of the kernel VM */ 615254885Sdumbbell pmap_curmaxkvaddr = 616254885Sdumbbell KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000); 617254885Sdumbbell 618254885Sdumbbell#ifdef VERBOSE_INIT_ARM 619254885Sdumbbell printf("Mapping kernel\n"); 620254885Sdumbbell#endif 621254885Sdumbbell 622254885Sdumbbell /* Now we fill in the L2 pagetable for the kernel static code/data */ 623254885Sdumbbell { 624254885Sdumbbell extern char etext[], _end[]; 625254885Sdumbbell size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE; 626254885Sdumbbell size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE; 627254885Sdumbbell u_int logical; 628254885Sdumbbell 629254885Sdumbbell textsize = (textsize + PGOFSET) & ~PGOFSET; 630254885Sdumbbell totalsize = (totalsize + PGOFSET) & ~PGOFSET; 631254885Sdumbbell 632254885Sdumbbell logical = 0; /* offset of kernel in RAM */ 633254885Sdumbbell logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical, 634254885Sdumbbell physical_start + logical, textsize, 635254885Sdumbbell VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 636254885Sdumbbell logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical, 637254885Sdumbbell physical_start + logical, totalsize - textsize, 638254885Sdumbbell VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 639254885Sdumbbell } 640254885Sdumbbell 641254885Sdumbbell#ifdef VERBOSE_INIT_ARM 642 printf("Constructing L2 page tables\n"); 643#endif 644 645 /* Map the stack pages */ 646 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa, 647 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 648 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa, 649 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 650 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa, 651 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 652 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa, 653 UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 654 655 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa, 656 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE); 657 658 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) { 659 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va, 660 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE, 661 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE); 662 } 663 664 /* Map the Mini-Data cache clean area. */ 665 xscale_setup_minidata(l1pagetable, minidataclean.pv_va, 666 minidataclean.pv_pa); 667 668 /* Map the vector page. */ 669 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa, 670 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 671 672 /* Map the statically mapped devices. */ 673 pmap_devmap_bootstrap(l1pagetable, iyonix_devmap); 674 675 /* 676 * Give the XScale global cache clean code an appropriately 677 * sized chunk of unmapped VA space starting at 0xff000000 678 * (our device mappings end before this address). 679 */ 680 xscale_cache_clean_addr = 0xff000000U; 681 682 /* 683 * Now we have the real page tables in place so we can switch to them. 684 * Once this is done we will be running with the REAL kernel page 685 * tables. 686 */ 687 688 /* Switch tables */ 689#ifdef VERBOSE_INIT_ARM 690 printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n", 691 physical_freestart, free_pages, free_pages); 692 printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa); 693#endif 694 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT); 695 cpu_setttb(kernel_l1pt.pv_pa, true); 696 cpu_tlb_flushID(); 697 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)); 698 699 iyonix_read_machineid(); 700 701 /* 702 * Moved from cpu_startup() as data_abort_handler() references 703 * this during uvm init 704 */ 705 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va); 706 707#ifdef VERBOSE_INIT_ARM 708 printf("done!\n"); 709#endif 710 711#ifdef VERBOSE_INIT_ARM 712 printf("bootstrap done.\n"); 713#endif 714 715 arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL); 716 717 /* 718 * Pages were allocated during the secondary bootstrap for the 719 * stacks for different CPU modes. 720 * We must now set the r13 registers in the different CPU modes to 721 * point to these stacks. 722 * Since the ARM stacks use STMFD etc. we must set r13 to the top end 723 * of the stack memory. 724 */ 725#ifdef VERBOSE_INIT_ARM 726 printf("init subsystems: stacks "); 727#endif 728 729 set_stackptr(PSR_IRQ32_MODE, 730 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE); 731 set_stackptr(PSR_ABT32_MODE, 732 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE); 733 set_stackptr(PSR_UND32_MODE, 734 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE); 735 736 /* 737 * Well we should set a data abort handler. 738 * Once things get going this will change as we will need a proper 739 * handler. 740 * Until then we will use a handler that just panics but tells us 741 * why. 742 * Initialisation of the vectors will just panic on a data abort. 743 * This just fills in a slightly better one. 744 */ 745#ifdef VERBOSE_INIT_ARM 746 printf("vectors "); 747#endif 748 data_abort_handler_address = (u_int)data_abort_handler; 749 prefetch_abort_handler_address = (u_int)prefetch_abort_handler; 750 undefined_handler_address = (u_int)undefinedinstruction_bounce; 751 752 /* Initialise the undefined instruction handlers */ 753#ifdef VERBOSE_INIT_ARM 754 printf("undefined "); 755#endif 756 undefined_init(); 757 758 /* Load memory into UVM. */ 759#ifdef VERBOSE_INIT_ARM 760 printf("page "); 761#endif 762 uvm_md_init(); 763 uvm_page_physload(atop(physical_freestart), atop(physical_freeend), 764 atop(physical_freestart), atop(physical_freeend), 765 VM_FREELIST_DEFAULT); 766 767 /* Boot strap pmap telling it where managed kernel virtual memory is */ 768#ifdef VERBOSE_INIT_ARM 769 printf("pmap "); 770#endif 771 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE); 772 773 /* Setup the IRQ system */ 774#ifdef VERBOSE_INIT_ARM 775 printf("irq "); 776#endif 777 i80321_intr_init(); 778 779#ifdef VERBOSE_INIT_ARM 780 printf("done.\n"); 781#endif 782 783#ifdef DDB 784 db_machine_init(); 785 if (boothowto & RB_KDB) 786 Debugger(); 787#endif 788 789 iyonix_pic_init(); 790 791 printf("args: %s\n", bootconfig.args); 792 printf("howto: %x\n", boothowto); 793 794 /* We return the new stack pointer address */ 795 return kernelstack.pv_va + USPACE_SVC_STACK_TOP; 796} 797 798void 799consinit(void) 800{ 801 static int consinit_called; 802 803 if (consinit_called != 0) 804 return; 805 806 consinit_called = 1; 807 808 /* We let consinit_<foo> worry about device numbers */ 809 if (strncmp(boot_consdev, "genfb", 5) && 810 strncmp(boot_consdev, "com", 3)) 811 strcpy(boot_consdev, DEFAULT_CONSDEV); 812 813 if (!strncmp(boot_consdev, "com", 3)) 814 consinit_com(boot_consdev); 815 else 816 consinit_genfb(boot_consdev); 817} 818 819static void 820consinit_com(const char *consdev) 821{ 822 static const bus_addr_t comcnaddrs[] = { 823 IYONIX_UART1, /* com0 */ 824 }; 825 /* 826 * Console devices are mapped VA==PA. Our devmap reflects 827 * this, so register it now so drivers can map the console 828 * device. 829 */ 830 pmap_devmap_register(iyonix_devmap); 831 832 /* When we support more than the first serial port as console, 833 * we should check consdev for a number. 834 */ 835#if NCOM > 0 836 if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed, 837 COM_FREQ, COM_TYPE_NORMAL, comcnmode)) 838 { 839 panic("can't init serial console @%lx", comcnaddrs[comcnunit]); 840 } 841#else 842 panic("serial console @%lx not configured", comcnaddrs[comcnunit]); 843#endif 844 845#if KGDB 846#if NCOM > 0 847 if (strcmp(kgdb_devname, "com") == 0) { 848 com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate, 849 COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode); 850 } 851#endif /* NCOM > 0 */ 852#endif /* KGDB */ 853} 854 855static void 856consinit_genfb(const char *consdev) 857{ 858 /* NOTYET */ 859} 860 861static void 862process_kernel_args(void) 863{ 864 char *args; 865 866 /* Ok now we will check the arguments for interesting parameters. */ 867 args = bootconfig.args; 868 869#ifdef BOOTHOWTO 870 boothowto = BOOTHOWTO; 871#else 872 boothowto = 0; 873#endif 874 875 /* Only arguments itself are passed from the bootloader */ 876 while (*args == ' ') 877 ++args; 878 879 boot_args = args; 880 parse_mi_bootargs(boot_args); 881 parse_iyonix_bootargs(boot_args); 882} 883 884static void 885parse_iyonix_bootargs(char *args) 886{ 887 char *ptr; 888 889 if (get_bootconf_option(args, "consdev", BOOTOPT_TYPE_STRING, &ptr)) 890 { 891 /* ptr may have trailing clutter */ 892 strlcpy(boot_consdev, ptr, sizeof(boot_consdev)); 893 if ( (ptr = strchr(boot_consdev, ' ')) ) 894 *ptr = 0; 895 } 896} 897