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