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