Home | History | Annotate | Line # | Download | only in ingenic
machdep.c revision 1.10
      1 /*	$NetBSD: machdep.c,v 1.10 2016/01/29 01:54:14 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 Michael Lorenz
      5  * All rights reserved.
      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 THE NETBSD FOUNDATION, 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 FOUNDATION 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>
     30 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.10 2016/01/29 01:54:14 macallan Exp $");
     31 
     32 #include "opt_ddb.h"
     33 #include "opt_kgdb.h"
     34 #include "opt_modular.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/boot_flag.h>
     38 #include <sys/device.h>
     39 #include <sys/kernel.h>
     40 #include <sys/kcore.h>
     41 #include <sys/ksyms.h>
     42 #include <sys/mount.h>
     43 #include <sys/reboot.h>
     44 #include <sys/cpu.h>
     45 #include <sys/bus.h>
     46 #include <sys/mutex.h>
     47 
     48 #include <uvm/uvm_extern.h>
     49 
     50 #include <dev/cons.h>
     51 
     52 #include "ksyms.h"
     53 
     54 #if NKSYMS || defined(DDB) || defined(MODULAR)
     55 #include <mips/db_machdep.h>
     56 #include <ddb/db_extern.h>
     57 #endif
     58 
     59 #include <mips/cache.h>
     60 #include <mips/locore.h>
     61 #include <mips/cpuregs.h>
     62 
     63 #include <mips/ingenic/ingenic_regs.h>
     64 #include <mips/ingenic/ingenic_var.h>
     65 
     66 #include "opt_ingenic.h"
     67 
     68 /* Maps for VM objects. */
     69 struct vm_map *phys_map = NULL;
     70 
     71 int maxmem;			/* max memory per process */
     72 
     73 int mem_cluster_cnt;
     74 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
     75 
     76 void	mach_init(void); /* XXX */
     77 void	ingenic_reset(void);
     78 
     79 void	ingenic_putchar_init(void);
     80 void	ingenic_puts(const char *);
     81 void	ingenic_com_cnattach(void);
     82 
     83 #ifdef MULTIPROCESSOR
     84 kmutex_t ingenic_ipi_lock;
     85 #endif
     86 
     87 static void
     88 cal_timer(void)
     89 {
     90 	uint32_t	cntfreq;
     91 	volatile uint32_t junk;
     92 
     93 	/*
     94 	 * The manual seems to imply that EXCCLK is 12MHz, although in real
     95 	 * life it appears to be 48MHz. Either way, we want a 12MHz counter.
     96 	 */
     97 	curcpu()->ci_cpu_freq = 1200000000;	/* for now */
     98 	cntfreq = 12000000;	/* EXTCLK / 4 */
     99 
    100 	curcpu()->ci_cctr_freq = cntfreq;
    101 	curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
    102 
    103 	/* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
    104 	curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
    105 
    106 	/* actually start the counter now */
    107 	/* stop OS timer */
    108 	writereg(JZ_TC_TECR, TESR_OST);
    109 	/* zero everything */
    110 	writereg(JZ_OST_CTRL, 0);
    111 	writereg(JZ_OST_CNT_LO, 0);
    112 	writereg(JZ_OST_CNT_HI, 0);
    113 	writereg(JZ_OST_DATA, 0xffffffff);
    114 	/* use EXTCLK, don't reset */
    115 	writereg(JZ_OST_CTRL, OSTC_EXT_EN | OSTC_MODE | OSTC_DIV_4);
    116 	/* start the timer */
    117 	writereg(JZ_TC_TESR, TESR_OST);
    118 	/* make sure the timer actually runs */
    119 	junk = readreg(JZ_OST_CNT_LO);
    120 	do {} while (junk == readreg(JZ_OST_CNT_LO));
    121 }
    122 
    123 #ifdef MULTIPROCESSOR
    124 static void
    125 ingenic_cpu_init(struct cpu_info *ci)
    126 {
    127 	uint32_t reg;
    128 
    129 	/* enable IPIs for this core */
    130 	reg = MFC0(12, 4);	/* reset entry and interrupts */
    131 	if (cpu_index(ci) == 1) {
    132 		reg |= REIM_MIRQ1_M;
    133 	} else
    134 		reg |= REIM_MIRQ0_M;
    135 	MTC0(reg, 12, 4);
    136 	printf("%s %d %08x\n", __func__, cpu_index(ci), reg);
    137 }
    138 
    139 static int
    140 ingenic_send_ipi(struct cpu_info *ci, int tag)
    141 {
    142 	uint32_t msg;
    143 
    144 	msg = 1 << tag;
    145 
    146 	mutex_enter(&ingenic_ipi_lock);
    147 	if (kcpuset_isset(cpus_running, cpu_index(ci))) {
    148 		if (cpu_index(ci) == 0) {
    149 			MTC0(msg, CP0_CORE_MBOX, 0);
    150 		} else {
    151 			MTC0(msg, CP0_CORE_MBOX, 1);
    152 		}
    153 	}
    154 	mutex_exit(&ingenic_ipi_lock);
    155 	return 0;
    156 }
    157 #endif /* MULTIPROCESSOR */
    158 
    159 void
    160 mach_init(void)
    161 {
    162 	void *kernend;
    163 	uint32_t memsize;
    164 	extern char edata[], end[];	/* XXX */
    165 
    166 	/* clear the BSS segment */
    167 	kernend = (void *)mips_round_page(end);
    168 
    169 	memset(edata, 0, (char *)kernend - edata);
    170 
    171 	/* setup early console */
    172 	ingenic_putchar_init();
    173 
    174 	/* set CPU model info for sysctl_hw */
    175 	cpu_setmodel("Ingenic XBurst");
    176 	mips_vector_init(NULL, false);
    177 	cal_timer();
    178 	uvm_setpagesize();
    179 	/*
    180 	 * Look at arguments passed to us and compute boothowto.
    181 	 */
    182 	boothowto = RB_AUTOBOOT;
    183 #ifdef KADB
    184 	boothowto |= RB_KDB;
    185 #endif
    186 
    187 	/*
    188 	 * Determine the memory size.
    189 	 *
    190 	 * Note: Reserve the first page!  That's where the trap
    191 	 * vectors are located.
    192 	 */
    193 	memsize = 0x40000000;
    194 
    195 	printf("Memory size: 0x%08x\n", memsize);
    196 	physmem = btoc(memsize);
    197 
    198 	/*
    199 	 * memory is at 0x20000000 with first 256MB mirrored to 0x00000000 so
    200 	 * we can see them through KSEG*
    201 	 * assume 1GB for now, the SoC can theoretically support up to 3GB
    202 	 */
    203 	mem_clusters[0].start = PAGE_SIZE;
    204 	mem_clusters[0].size = 0x10000000 - PAGE_SIZE;
    205 	mem_clusters[1].start = 0x30000000;
    206 	mem_clusters[1].size = 0x30000000;
    207 	mem_cluster_cnt = 2;
    208 
    209 	/*
    210 	 * Load the available pages into the VM system.
    211 	 */
    212 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
    213 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    214 
    215 	/*
    216 	 * Initialize message buffer (at end of core).
    217 	 */
    218 	mips_init_msgbuf();
    219 
    220 	/*
    221 	 * Initialize the virtual memory system.
    222 	 */
    223 	pmap_bootstrap();
    224 
    225 	/*
    226 	 * Allocate uarea page for lwp0 and set it.
    227 	 */
    228 	mips_init_lwp0_uarea();
    229 
    230 #ifdef MULTIPROCESSOR
    231 	mutex_init(&ingenic_ipi_lock, MUTEX_DEFAULT, IPL_HIGH);
    232 	mips_locoresw.lsw_send_ipi = ingenic_send_ipi;
    233 	mips_locoresw.lsw_cpu_init = ingenic_cpu_init;
    234 #endif
    235 
    236 	apbus_init();
    237 	/*
    238 	 * Initialize debuggers, and break into them, if appropriate.
    239 	 */
    240 #ifdef DDB
    241 	if (boothowto & RB_KDB)
    242 		Debugger();
    243 #endif
    244 }
    245 
    246 void
    247 consinit(void)
    248 {
    249 	/*
    250 	 * Everything related to console initialization is done
    251 	 * in mach_init().
    252 	 */
    253 	apbus_init();
    254 	ingenic_com_cnattach();
    255 }
    256 
    257 void
    258 cpu_startup(void)
    259 {
    260 	cpu_startup_common();
    261 }
    262 
    263 void
    264 cpu_reboot(int howto, char *bootstr)
    265 {
    266 	static int waittime = -1;
    267 
    268 	/* Take a snapshot before clobbering any registers. */
    269 	savectx(curpcb);
    270 
    271 	/* If "always halt" was specified as a boot flag, obey. */
    272 	if (boothowto & RB_HALT)
    273 		howto |= RB_HALT;
    274 
    275 	boothowto = howto;
    276 
    277 	/* If system is cold, just halt. */
    278 	if (cold) {
    279 		boothowto |= RB_HALT;
    280 		goto haltsys;
    281 	}
    282 
    283 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    284 		waittime = 0;
    285 
    286 		/*
    287 		 * Synchronize the disks....
    288 		 */
    289 		vfs_shutdown();
    290 
    291 		/*
    292 		 * If we've been adjusting the clock, the todr
    293 		 * will be out of synch; adjust it now.
    294 		 */
    295 		resettodr();
    296 	}
    297 
    298 	/* Disable interrupts. */
    299 	splhigh();
    300 
    301 	if (boothowto & RB_DUMP)
    302 		dumpsys();
    303 
    304 haltsys:
    305 	/* Run any shutdown hooks. */
    306 	doshutdownhooks();
    307 
    308 	pmf_system_shutdown(boothowto);
    309 
    310 #if 0
    311 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
    312 		if (board && board->ab_poweroff)
    313 			board->ab_poweroff();
    314 #endif
    315 
    316 	/*
    317 	 * Firmware may autoboot (depending on settings), and we cannot pass
    318 	 * flags to it (at least I haven't figured out how to yet), so
    319 	 * we "pseudo-halt" now.
    320 	 */
    321 	if (boothowto & RB_HALT) {
    322 		printf("\n");
    323 		printf("The operating system has halted.\n");
    324 		printf("Please press any key to reboot.\n\n");
    325 		cnpollc(1);	/* For proper keyboard command handling */
    326 		cngetc();
    327 		cnpollc(0);
    328 	}
    329 
    330 	printf("reseting board...\n\n");
    331 	mips_icache_sync_all();
    332 	mips_dcache_wbinv_all();
    333 	ingenic_reset();
    334 	__asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    335 	printf("Oops, back from reset\n\nSpinning...");
    336 	for (;;)
    337 		/* spin forever */ ;	/* XXX */
    338 	/*NOTREACHED*/
    339 }
    340 
    341 void
    342 ingenic_reset(void)
    343 {
    344 	/*
    345 	 * for now, provoke a watchdog reset in about a second, so UART buffers
    346 	 * have a fighting chance to flush before we pull the plug
    347 	 */
    348 	writereg(JZ_WDOG_TCER, 0);	/* disable watchdog */
    349 	writereg(JZ_WDOG_TCNT, 0);	/* reset counter */
    350 	writereg(JZ_WDOG_TDR, 128);	/* wait for ~1s */
    351 	writereg(JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
    352 	writereg(JZ_WDOG_TCER, TCER_ENABLE);	/* fire! */
    353 }
    354