Home | History | Annotate | Line # | Download | only in ingenic
machdep.c revision 1.1
      1  1.1  macallan /*	$NetBSD: machdep.c,v 1.1 2014/11/22 15:17:02 macallan Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2014 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.1  macallan 
     29  1.1  macallan #include <sys/cdefs.h>
     30  1.1  macallan __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.1 2014/11/22 15:17:02 macallan Exp $");
     31  1.1  macallan 
     32  1.1  macallan #include "opt_ddb.h"
     33  1.1  macallan #include "opt_kgdb.h"
     34  1.1  macallan #include "opt_modular.h"
     35  1.1  macallan 
     36  1.1  macallan #include <sys/param.h>
     37  1.1  macallan #include <sys/boot_flag.h>
     38  1.1  macallan #include <sys/device.h>
     39  1.1  macallan #include <sys/kernel.h>
     40  1.1  macallan #include <sys/kcore.h>
     41  1.1  macallan #include <sys/ksyms.h>
     42  1.1  macallan #include <sys/mount.h>
     43  1.1  macallan #include <sys/reboot.h>
     44  1.1  macallan #include <sys/cpu.h>
     45  1.1  macallan 
     46  1.1  macallan #include <uvm/uvm_extern.h>
     47  1.1  macallan 
     48  1.1  macallan #include <dev/cons.h>
     49  1.1  macallan 
     50  1.1  macallan #include <mips/ingenic/ingenic_regs.h>
     51  1.1  macallan 
     52  1.1  macallan #include "ksyms.h"
     53  1.1  macallan 
     54  1.1  macallan #if NKSYMS || defined(DDB) || defined(MODULAR)
     55  1.1  macallan #include <mips/db_machdep.h>
     56  1.1  macallan #include <ddb/db_extern.h>
     57  1.1  macallan #endif
     58  1.1  macallan 
     59  1.1  macallan #include <mips/cache.h>
     60  1.1  macallan #include <mips/locore.h>
     61  1.1  macallan #include <mips/cpuregs.h>
     62  1.1  macallan 
     63  1.1  macallan /* Maps for VM objects. */
     64  1.1  macallan struct vm_map *phys_map = NULL;
     65  1.1  macallan 
     66  1.1  macallan int maxmem;			/* max memory per process */
     67  1.1  macallan 
     68  1.1  macallan int mem_cluster_cnt;
     69  1.1  macallan phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
     70  1.1  macallan 
     71  1.1  macallan void	mach_init(void); /* XXX */
     72  1.1  macallan void	ingenic_reset(void);
     73  1.1  macallan 
     74  1.1  macallan void	ingenic_putchar_init(void);
     75  1.1  macallan void	ingenic_puts(const char *);
     76  1.1  macallan void	ingenic_com_cnattach(void);
     77  1.1  macallan 
     78  1.1  macallan static void
     79  1.1  macallan cal_timer(void)
     80  1.1  macallan {
     81  1.1  macallan 	uint32_t	cntfreq;
     82  1.1  macallan 	volatile uint32_t junk;
     83  1.1  macallan 
     84  1.1  macallan 	/*
     85  1.1  macallan 	 * The manual seems to imply that EXCCLK is 12MHz, although in real
     86  1.1  macallan 	 * life it appears to be 48MHz. Either way, we want a 12MHz counter.
     87  1.1  macallan 	 */
     88  1.1  macallan 	curcpu()->ci_cpu_freq = 1200000000;	/* for now */
     89  1.1  macallan 	cntfreq = 12000000;	/* EXTCLK / 4 */
     90  1.1  macallan 
     91  1.1  macallan 	curcpu()->ci_cctr_freq = cntfreq;
     92  1.1  macallan 	curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
     93  1.1  macallan 
     94  1.1  macallan 	/* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
     95  1.1  macallan 	curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
     96  1.1  macallan 
     97  1.1  macallan 	/* actually start the counter now */
     98  1.1  macallan 	/* stop OS timer */
     99  1.1  macallan 	writereg(JZ_TC_TECR, TESR_OST);
    100  1.1  macallan 	/* zero everything */
    101  1.1  macallan 	writereg(JZ_OST_CTRL, 0);
    102  1.1  macallan 	writereg(JZ_OST_CNT_LO, 0);
    103  1.1  macallan 	writereg(JZ_OST_CNT_HI, 0);
    104  1.1  macallan 	writereg(JZ_OST_DATA, 0xffffffff);
    105  1.1  macallan 	/* use EXTCLK, don't reset */
    106  1.1  macallan 	writereg(JZ_OST_CTRL, OSTC_EXT_EN | OSTC_MODE | OSTC_DIV_4);
    107  1.1  macallan 	/* start the timer */
    108  1.1  macallan 	writereg(JZ_TC_TESR, TESR_OST);
    109  1.1  macallan 	/* make sure the timer actually runs */
    110  1.1  macallan 	junk = readreg(JZ_OST_CNT_LO);
    111  1.1  macallan 	do {} while (junk == readreg(JZ_OST_CNT_LO));
    112  1.1  macallan }
    113  1.1  macallan 
    114  1.1  macallan void
    115  1.1  macallan mach_init(void)
    116  1.1  macallan {
    117  1.1  macallan 	void *kernend;
    118  1.1  macallan 	uint32_t memsize;
    119  1.1  macallan 	extern char edata[], end[];	/* XXX */
    120  1.1  macallan 
    121  1.1  macallan 	/* clear the BSS segment */
    122  1.1  macallan 	kernend = (void *)mips_round_page(end);
    123  1.1  macallan 
    124  1.1  macallan 	memset(edata, 0, (char *)kernend - edata);
    125  1.1  macallan 
    126  1.1  macallan 	/* setup early console */
    127  1.1  macallan 	ingenic_putchar_init();
    128  1.1  macallan 
    129  1.1  macallan 	/* set CPU model info for sysctl_hw */
    130  1.1  macallan 	cpu_setmodel("Ingenic XBurst");
    131  1.1  macallan 	mips_vector_init(NULL, false);
    132  1.1  macallan 	cal_timer();
    133  1.1  macallan 	uvm_setpagesize();
    134  1.1  macallan 	/*
    135  1.1  macallan 	 * Look at arguments passed to us and compute boothowto.
    136  1.1  macallan 	 */
    137  1.1  macallan 	boothowto = RB_AUTOBOOT;
    138  1.1  macallan #ifdef KADB
    139  1.1  macallan 	boothowto |= RB_KDB;
    140  1.1  macallan #endif
    141  1.1  macallan 
    142  1.1  macallan 	/*
    143  1.1  macallan 	 * Determine the memory size.
    144  1.1  macallan 	 *
    145  1.1  macallan 	 * Note: Reserve the first page!  That's where the trap
    146  1.1  macallan 	 * vectors are located.
    147  1.1  macallan 	 */
    148  1.1  macallan 	memsize = 0x40000000;
    149  1.1  macallan 
    150  1.1  macallan 	printf("Memory size: 0x%08x\n", memsize);
    151  1.1  macallan 	physmem = btoc(memsize);
    152  1.1  macallan 
    153  1.1  macallan 	/* XXX this is CI20 specific */
    154  1.1  macallan 	mem_clusters[0].start = PAGE_SIZE;
    155  1.1  macallan 	mem_clusters[0].size = 0x10000000 - PAGE_SIZE;
    156  1.1  macallan 	mem_clusters[1].start = 0x30000000;
    157  1.1  macallan 	mem_clusters[1].size = 0x30000000;
    158  1.1  macallan 	mem_cluster_cnt = 2;
    159  1.1  macallan 
    160  1.1  macallan 	/*
    161  1.1  macallan 	 * Load the available pages into the VM system.
    162  1.1  macallan 	 */
    163  1.1  macallan 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
    164  1.1  macallan 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    165  1.1  macallan 
    166  1.1  macallan 	/*
    167  1.1  macallan 	 * Initialize message buffer (at end of core).
    168  1.1  macallan 	 */
    169  1.1  macallan 	mips_init_msgbuf();
    170  1.1  macallan 
    171  1.1  macallan 	/*
    172  1.1  macallan 	 * Initialize the virtual memory system.
    173  1.1  macallan 	 */
    174  1.1  macallan 	pmap_bootstrap();
    175  1.1  macallan 
    176  1.1  macallan 	/*
    177  1.1  macallan 	 * Allocate uarea page for lwp0 and set it.
    178  1.1  macallan 	 */
    179  1.1  macallan 	mips_init_lwp0_uarea();
    180  1.1  macallan 
    181  1.1  macallan 	/*
    182  1.1  macallan 	 * Initialize debuggers, and break into them, if appropriate.
    183  1.1  macallan 	 */
    184  1.1  macallan #ifdef DDB
    185  1.1  macallan 	if (boothowto & RB_KDB)
    186  1.1  macallan 		Debugger();
    187  1.1  macallan #endif
    188  1.1  macallan }
    189  1.1  macallan 
    190  1.1  macallan void
    191  1.1  macallan consinit(void)
    192  1.1  macallan {
    193  1.1  macallan 	/*
    194  1.1  macallan 	 * Everything related to console initialization is done
    195  1.1  macallan 	 * in mach_init().
    196  1.1  macallan 	 */
    197  1.1  macallan 	ingenic_com_cnattach();
    198  1.1  macallan }
    199  1.1  macallan 
    200  1.1  macallan void
    201  1.1  macallan cpu_startup(void)
    202  1.1  macallan {
    203  1.1  macallan 	char pbuf[9];
    204  1.1  macallan 	vaddr_t minaddr, maxaddr;
    205  1.1  macallan #ifdef DEBUG
    206  1.1  macallan 	extern int pmapdebug;		/* XXX */
    207  1.1  macallan 	int opmapdebug = pmapdebug;
    208  1.1  macallan 
    209  1.1  macallan 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    210  1.1  macallan #endif
    211  1.1  macallan 
    212  1.1  macallan 	/*
    213  1.1  macallan 	 * Good {morning,afternoon,evening,night}.
    214  1.1  macallan 	 */
    215  1.1  macallan 	printf("%s%s", copyright, version);
    216  1.1  macallan 	printf("%s\n", cpu_getmodel());
    217  1.1  macallan 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    218  1.1  macallan 	printf("total memory = %s\n", pbuf);
    219  1.1  macallan 
    220  1.1  macallan 	minaddr = 0;
    221  1.1  macallan 	/*
    222  1.1  macallan 	 * Allocate a submap for physio
    223  1.1  macallan 	 */
    224  1.1  macallan 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    225  1.1  macallan 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    226  1.1  macallan 
    227  1.1  macallan 	/*
    228  1.1  macallan 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    229  1.1  macallan 	 * are allocated via the pool allocator, and we use KSEG to
    230  1.1  macallan 	 * map those pages.
    231  1.1  macallan 	 */
    232  1.1  macallan 
    233  1.1  macallan #ifdef DEBUG
    234  1.1  macallan 	pmapdebug = opmapdebug;
    235  1.1  macallan #endif
    236  1.1  macallan 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    237  1.1  macallan 	printf("avail memory = %s\n", pbuf);
    238  1.1  macallan }
    239  1.1  macallan 
    240  1.1  macallan void
    241  1.1  macallan cpu_reboot(int howto, char *bootstr)
    242  1.1  macallan {
    243  1.1  macallan 	static int waittime = -1;
    244  1.1  macallan 
    245  1.1  macallan 	/* Take a snapshot before clobbering any registers. */
    246  1.1  macallan 	savectx(curpcb);
    247  1.1  macallan 
    248  1.1  macallan 	/* If "always halt" was specified as a boot flag, obey. */
    249  1.1  macallan 	if (boothowto & RB_HALT)
    250  1.1  macallan 		howto |= RB_HALT;
    251  1.1  macallan 
    252  1.1  macallan 	boothowto = howto;
    253  1.1  macallan 
    254  1.1  macallan 	/* If system is cold, just halt. */
    255  1.1  macallan 	if (cold) {
    256  1.1  macallan 		boothowto |= RB_HALT;
    257  1.1  macallan 		goto haltsys;
    258  1.1  macallan 	}
    259  1.1  macallan 
    260  1.1  macallan 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    261  1.1  macallan 		waittime = 0;
    262  1.1  macallan 
    263  1.1  macallan 		/*
    264  1.1  macallan 		 * Synchronize the disks....
    265  1.1  macallan 		 */
    266  1.1  macallan 		vfs_shutdown();
    267  1.1  macallan 
    268  1.1  macallan 		/*
    269  1.1  macallan 		 * If we've been adjusting the clock, the todr
    270  1.1  macallan 		 * will be out of synch; adjust it now.
    271  1.1  macallan 		 */
    272  1.1  macallan 		resettodr();
    273  1.1  macallan 	}
    274  1.1  macallan 
    275  1.1  macallan 	/* Disable interrupts. */
    276  1.1  macallan 	splhigh();
    277  1.1  macallan 
    278  1.1  macallan 	if (boothowto & RB_DUMP)
    279  1.1  macallan 		dumpsys();
    280  1.1  macallan 
    281  1.1  macallan  haltsys:
    282  1.1  macallan 	/* Run any shutdown hooks. */
    283  1.1  macallan 	doshutdownhooks();
    284  1.1  macallan 
    285  1.1  macallan 	pmf_system_shutdown(boothowto);
    286  1.1  macallan 
    287  1.1  macallan #if 0
    288  1.1  macallan 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
    289  1.1  macallan 		if (board && board->ab_poweroff)
    290  1.1  macallan 			board->ab_poweroff();
    291  1.1  macallan #endif
    292  1.1  macallan 
    293  1.1  macallan 	/*
    294  1.1  macallan 	 * Firmware may autoboot (depending on settings), and we cannot pass
    295  1.1  macallan 	 * flags to it (at least I haven't figured out how to yet), so
    296  1.1  macallan 	 * we "pseudo-halt" now.
    297  1.1  macallan 	 */
    298  1.1  macallan 	if (boothowto & RB_HALT) {
    299  1.1  macallan 		printf("\n");
    300  1.1  macallan 		printf("The operating system has halted.\n");
    301  1.1  macallan 		printf("Please press any key to reboot.\n\n");
    302  1.1  macallan 		cnpollc(1);	/* For proper keyboard command handling */
    303  1.1  macallan 		cngetc();
    304  1.1  macallan 		cnpollc(0);
    305  1.1  macallan 	}
    306  1.1  macallan 
    307  1.1  macallan 	printf("reseting board...\n\n");
    308  1.1  macallan 	mips_icache_sync_all();
    309  1.1  macallan 	mips_dcache_wbinv_all();
    310  1.1  macallan 	ingenic_reset();
    311  1.1  macallan 	__asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    312  1.1  macallan 	printf("Oops, back from reset\n\nSpinning...");
    313  1.1  macallan 	for (;;)
    314  1.1  macallan 		/* spin forever */ ;	/* XXX */
    315  1.1  macallan 	/*NOTREACHED*/
    316  1.1  macallan }
    317  1.1  macallan 
    318  1.1  macallan void
    319  1.1  macallan ingenic_reset(void)
    320  1.1  macallan {
    321  1.1  macallan 	/*
    322  1.1  macallan 	 * for now, provoke a watchdog reset in about a second, so UART buffers
    323  1.1  macallan 	 * have a fighting chance to flush before we pull the plug
    324  1.1  macallan 	 */
    325  1.1  macallan 	writereg(JZ_WDOG_TCER, 0);	/* disable watchdog */
    326  1.1  macallan 	writereg(JZ_WDOG_TCNT, 0);	/* reset counter */
    327  1.1  macallan 	writereg(JZ_WDOG_TDR, 128);	/* wait for ~1s */
    328  1.1  macallan 	writereg(JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
    329  1.1  macallan 	writereg(JZ_WDOG_TCER, TCER_ENABLE);	/* fire! */
    330  1.1  macallan }
    331  1.1  macallan 
    332  1.1  macallan void
    333  1.1  macallan evbmips_intr_init(void)
    334  1.1  macallan {
    335  1.1  macallan #if notyet
    336  1.1  macallan 	(*platformsw->apsw_intr_init)();
    337  1.1  macallan #endif
    338  1.1  macallan }
    339  1.1  macallan 
    340  1.1  macallan void
    341  1.1  macallan evbmips_iointr(int ipl, vaddr_t pc, uint32_t ipending)
    342  1.1  macallan {
    343  1.1  macallan #if notyet
    344  1.1  macallan 	(*platformsw->apsw_intrsw->aisw_iointr)(ipl, pc, ipending);
    345  1.1  macallan #endif
    346  1.1  macallan }
    347