Home | History | Annotate | Line # | Download | only in rasoc
machdep.c revision 1.7
      1 /*	$NetBSD: machdep.c,v 1.7 2012/03/02 16:20:55 matt Exp $	*/
      2 /*-
      3  * Copyright (c) 2011 CradlePoint Technology, Inc.
      4  * All rights reserved.
      5  *
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     30 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.7 2012/03/02 16:20:55 matt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/boot_flag.h>
     34 #include <sys/buf.h>
     35 #include <sys/device.h>
     36 #include <sys/mount.h>
     37 #include <sys/kcore.h>
     38 #include <sys/reboot.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/termios.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <dev/cons.h>
     46 
     47 #include <mips/cache.h>
     48 #include <mips/locore.h>
     49 #include <mips/cpuregs.h>
     50 
     51 #include <mips/ralink/ralink_reg.h>
     52 #include <mips/ralink/ralink_var.h>
     53 
     54 /* structures we define/alloc for other files in the kernel */
     55 struct vm_map *phys_map = NULL;
     56 
     57 int mem_cluster_cnt = 0;
     58 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
     59 
     60 void mach_init(void);
     61 
     62 static inline uint32_t
     63 sysctl_read(u_int offset)
     64 {
     65 	return *RA_IOREG_VADDR(RA_SYSCTL_BASE, offset);
     66 }
     67 
     68 static inline void
     69 sysctl_write(u_int offset, uint32_t val)
     70 {
     71 	*RA_IOREG_VADDR(RA_SYSCTL_BASE, offset) = val;
     72 }
     73 
     74 static void
     75 cal_timer(void)
     76 {
     77 	uint32_t cntfreq;
     78 
     79 	cntfreq = curcpu()->ci_cpu_freq = RA_CLOCK_RATE;
     80 
     81 	/* MIPS 4Kc CP0 counts every other clock */
     82 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
     83 		cntfreq /= 2;
     84 
     85 	curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
     86 
     87 	/* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
     88 	curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
     89 }
     90 
     91 void
     92 mach_init(void)
     93 {
     94 	vaddr_t kernend;
     95 	psize_t memsize;
     96 
     97 	extern char kernel_text[];
     98 	extern char edata[], end[];	/* From Linker */
     99 
    100 	/* clear the BSS segment */
    101 	kernend = mips_round_page(end);
    102 
    103 	memset(edata, 0, kernend - (vaddr_t)edata);
    104 
    105 #ifdef RALINK_CONSOLE_EARLY
    106 	/*
    107 	 * set up early console
    108 	 *  cannot printf until sometime (?) in mips_vector_init
    109 	 *  meanwhile can use the ra_console_putc primitive if necessary
    110 	 */
    111 	ralink_console_early();
    112 #endif
    113 
    114 	/* set CPU model info for sysctl_hw */
    115 	uint32_t tmp;
    116 	tmp = sysctl_read(RA_SYSCTL_ID0);
    117 	memcpy(&cpu_model[0], &tmp, 4);
    118 	tmp = sysctl_read(RA_SYSCTL_ID1);
    119 	memcpy(&cpu_model[4], &tmp, 4);
    120 	cpu_model[9] = 0;
    121 
    122 	/*
    123 	 * Set up the exception vectors and CPU-specific function
    124 	 * vectors early on.  We need the wbflush() vector set up
    125 	 * before comcnattach() is called (or at least before the
    126 	 * first printf() after that is called).
    127 	 * Sets up mips_cpu_flags that may be queried by other
    128 	 * functions called during startup.
    129 	 * Also clears the I+D caches.
    130 	 */
    131 	mips_vector_init(NULL, false);
    132 
    133 	/*
    134 	 * Calibrate timers.
    135 	 */
    136 	cal_timer();
    137 
    138 	/*
    139 	 * Set the VM page size.
    140 	 */
    141 	uvm_setpagesize();
    142 
    143 	/*
    144 	 * Look at arguments passed to us and compute boothowto.
    145 	 */
    146 	boothowto = RB_AUTOBOOT;
    147 #ifdef KADB
    148 	boothowto |= RB_KDB;
    149 #endif
    150 
    151 	/*
    152 	 * Determine the memory size.
    153 	 */
    154 	memsize = *(volatile uint32_t *)
    155 	    MIPS_PHYS_TO_KSEG1(RA_SYSCTL_BASE + RA_SYSCTL_CFG0);
    156 	memsize = __SHIFTOUT(memsize, SYSCTL_CFG0_DRAM_SIZE);
    157 	if (__predict_false(memsize == 0)) {
    158 		memsize = 2 << 20;
    159 	} else {
    160 		memsize = 4 << (20 + memsize);
    161 	}
    162 
    163 	physmem = btoc(memsize);
    164 
    165 	mem_clusters[mem_cluster_cnt].start = 0;
    166 	mem_clusters[mem_cluster_cnt].size = memsize;
    167 	mem_cluster_cnt++;
    168 
    169 	/*
    170 	 * Load the memory into the VM system
    171 	 */
    172 	mips_page_physload((vaddr_t)kernel_text, kernend,
    173 	    mem_clusters, mem_cluster_cnt,
    174 	    NULL, 0);
    175 
    176 	/*
    177 	 * Initialize message buffer (at end of core).
    178 	 */
    179 	mips_init_msgbuf();
    180 
    181 	/*
    182 	 * Initialize the virtual memory system.
    183 	 */
    184 	pmap_bootstrap();
    185 
    186 	/*
    187 	 * Init mapping for u page(s) for proc0.
    188 	 */
    189 	mips_init_lwp0_uarea();
    190 
    191 	/*
    192 	 * Initialize busses.
    193 	 */
    194 	ra_bus_init();
    195 
    196 #ifdef DDB
    197 	if (boothowto & RB_KDB)
    198 		Debugger();
    199 #endif
    200 }
    201 
    202 void
    203 cpu_startup(void)
    204 {
    205 #ifdef DEBUG
    206 	extern int pmapdebug;
    207 	const int opmapdebug = pmapdebug;
    208 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    209 #endif
    210 
    211 	cpu_startup_common();
    212 
    213 #ifdef DEBUG
    214 	pmapdebug = opmapdebug;
    215 #endif
    216 }
    217 
    218 void
    219 cpu_reboot(int howto, char *bootstr)
    220 {
    221 	static int waittime = -1;
    222 
    223 	/* Take a snapshot before clobbering any registers. */
    224 	savectx(lwp_getpcb(curlwp));
    225 
    226 	/* If "always halt" was specified as a boot flag, obey. */
    227 	if (boothowto & RB_HALT)
    228 		howto |= RB_HALT;
    229 
    230 	boothowto = howto;
    231 
    232 	/* If system is cold, just halt. */
    233 	if (cold) {
    234 		boothowto |= RB_HALT;
    235 		goto haltsys;
    236 	}
    237 
    238 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    239 		waittime = 0;
    240 
    241 		/*
    242 		 * Synchronize the disks....
    243 		 */
    244 		vfs_shutdown();
    245 
    246 		/*
    247 		 * If we've been adjusting the clock, the todr
    248 		 * will be out of synch; adjust it now.
    249 		 */
    250 		resettodr();
    251 	}
    252 
    253 	/* Disable interrupts. */
    254 	splhigh();
    255 
    256 	if (boothowto & RB_DUMP)
    257 		dumpsys();
    258 
    259  haltsys:
    260 	/* Run any shutdown hooks. */
    261 	doshutdownhooks();
    262 
    263 	pmf_system_shutdown(boothowto);
    264 
    265 	/*
    266 	 * Firmware may autoboot (depending on settings), and we cannot pass
    267 	 * flags to it (at least I haven't figured out how to yet), so
    268 	 * we "pseudo-halt" now.
    269 	 */
    270 	if (boothowto & RB_HALT) {
    271 		printf("\n");
    272 		printf("The operating system has halted.\n");
    273 		printf("Please press any key to reboot.\n\n");
    274 		cnpollc(1);	/* For proper keyboard command handling */
    275 		cngetc();
    276 		cnpollc(0);
    277 	}
    278 
    279 	printf("reseting board...\n\n");
    280 	mips_icache_sync_all();
    281 	mips_dcache_wbinv_all();
    282 
    283 	sysctl_write(RA_SYSCTL_RST, 1);  /* SoC Reset */
    284 	sysctl_write(RA_SYSCTL_RST, 0);
    285 
    286 #if 0
    287 	__asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    288 #endif
    289 	printf("Oops, back from reset\n\nSpinning...");
    290 	for (;;)
    291 		/* spin forever */ ;	/* XXX */
    292 	/*NOTREACHED*/
    293 }
    294 
    295 #define NO_SECURITY_MAGIC	0x27051958
    296 #define SERIAL_MAGIC		0x100000
    297 int
    298 ra_check_memo_reg(int key)
    299 {
    300 	uint32_t magic;
    301 
    302 	/*
    303 	 * These registers may be overwritten.  Keep the value around in case
    304 	 * it is used later.  Bitmask 1 == security, 2 = serial
    305 	 */
    306 	static int keyvalue;
    307 
    308 	switch (key) {
    309 	case NO_SECURITY:
    310 		magic = sysctl_read(RA_SYSCTL_MEMO0);
    311 		if ((NO_SECURITY_MAGIC == magic) || ((keyvalue & 1) != 0)) {
    312 			keyvalue |= 1;
    313 			return 1;
    314 		}
    315 		return 0;
    316 		break;
    317 
    318 	case SERIAL_CONSOLE:
    319 		magic = sysctl_read(RA_SYSCTL_MEMO1);
    320 		if (((SERIAL_MAGIC & magic) != 0) || ((keyvalue & 2) != 0)) {
    321 			keyvalue |= 2;
    322 			return 1;
    323 		}
    324 		return 0;
    325 		break;
    326 
    327 	default:
    328 		return 0;
    329 	}
    330 
    331 }
    332