Home | History | Annotate | Line # | Download | only in mmnet
mmnet_machdep.c revision 1.4
      1 /*	$Id: mmnet_machdep.c,v 1.4 2019/07/16 14:41:47 skrll Exp $	*/
      2 /*	$NetBSD: mmnet_machdep.c,v 1.4 2019/07/16 14:41:47 skrll Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2007 Embedtronics Oy
      6  * Based on tsarm_machdep.c
      7  *
      8  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      9  * All rights reserved.
     10  *
     11  * Based on code written by Jason R. Thorpe and Steve C. Woodford for
     12  * Wasabi Systems, Inc.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed for the NetBSD Project by
     25  *	Wasabi Systems, Inc.
     26  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     27  *    or promote products derived from this software without specific prior
     28  *    written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 /*
     44  * Copyright (c) 1997,1998 Mark Brinicombe.
     45  * Copyright (c) 1997,1998 Causality Limited.
     46  * All rights reserved.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by Mark Brinicombe
     59  *	for the NetBSD Project.
     60  * 4. The name of the company nor the name of the author may be used to
     61  *    endorse or promote products derived from this software without specific
     62  *    prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     65  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     66  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     67  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     68  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     69  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     70  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  * Machine dependant functions for kernel setup for Iyonix.
     77  */
     78 /* Adaptation for Propox MMnet by Aymeric Vincent is in the public domain */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: mmnet_machdep.c,v 1.4 2019/07/16 14:41:47 skrll Exp $");
     82 
     83 #include "opt_ddb.h"
     84 #include "opt_kgdb.h"
     85 #include "opt_pmap_debug.h"
     86 
     87 #include <sys/param.h>
     88 #include <sys/bus.h>
     89 #include <sys/device.h>
     90 #include <sys/systm.h>
     91 #include <sys/kernel.h>
     92 #include <sys/exec.h>
     93 #include <sys/proc.h>
     94 #include <sys/msgbuf.h>
     95 #include <sys/reboot.h>
     96 #include <sys/termios.h>
     97 #include <sys/ksyms.h>
     98 
     99 #include <uvm/uvm_extern.h>
    100 
    101 #include <dev/cons.h>
    102 
    103 #include <machine/db_machdep.h>
    104 #include <ddb/db_sym.h>
    105 #include <ddb/db_extern.h>
    106 
    107 #define	DRAM_BLOCKS	1
    108 #include <machine/bootconfig.h>
    109 #include <machine/cpu.h>
    110 #include <machine/frame.h>
    111 #include <arm/undefined.h>
    112 
    113 #include <arm/arm32/machdep.h>
    114 
    115 #include <arm/at91/at91reg.h>
    116 #include <arm/at91/at91var.h>
    117 
    118 #include <dev/ic/comreg.h>
    119 #include <dev/ic/comvar.h>
    120 
    121 #include <machine/autoconf.h>
    122 
    123 #include "isa.h"
    124 
    125 #include <arm/at91/at91sam9260reg.h>
    126 #include <arm/at91/at91sam9260busvar.h>
    127 
    128 #include "ksyms.h"
    129 
    130 #include <arm/at91/at91busvar.h>
    131 #include <arm/at91/at91pdcreg.h>
    132 #include <arm/at91/at91dbgureg.h>
    133 #include <arm/at91/at91reg.h>
    134 #include <arm/at91/at91streg.h>
    135 
    136 /* boot configuration: */
    137 BootConfig bootconfig;		/* Boot config storage */
    138 char *boot_args = NULL;
    139 char *boot_file = NULL;
    140 
    141 /* hmmm */
    142 static struct arm32_dma_range mmnet_dma_ranges[4];
    143 static struct at91bus_machdep mmnetbus;
    144 static at91bus_tag_t old_at91bus_tag;
    145 
    146 /* Prototypes */
    147 static void mmnet_device_register(device_t dev, void *aux);
    148 static void mmnetbus_init(struct at91bus_clocks *);
    149 static void mmnetbus_peripheral_clock(int pid, int enable);
    150 static uint32_t mmnetbus_gpio_mask(int pid);
    151 
    152 
    153 /*
    154  * void cpu_reboot(int howto, char *bootstr)
    155  *
    156  * Reboots the system
    157  *
    158  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    159  * then reset the CPU.
    160  */
    161 void
    162 cpu_reboot(int howto, char *bootstr)
    163 {
    164 
    165 	/*
    166 	 * If we are still cold then hit the air brakes
    167 	 * and crash to earth fast
    168 	 */
    169 	if (cold) {
    170 		doshutdownhooks();
    171 		pmf_system_shutdown(boothowto);
    172 		printf("\r\n");
    173 		printf("The operating system has halted.\r\n");
    174 		printf("Please press any key to reboot.\r\n");
    175 		cngetc();
    176 		printf("\r\nrebooting...\r\n");
    177 		goto reset;
    178 	}
    179 
    180 	/* Disable console buffering */
    181 
    182 	/*
    183 	 * If RB_NOSYNC was not specified sync the discs.
    184 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    185 	 * unmount.  It looks like syslogd is getting woken up only to find
    186 	 * that it cannot page part of the binary in as the filesystem has
    187 	 * been unmounted.
    188 	 */
    189 	if (!(howto & RB_NOSYNC))
    190 		bootsync();
    191 
    192 	/* Say NO to interrupts */
    193 	splhigh();
    194 
    195 	/* Do a dump if requested. */
    196 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    197 		dumpsys();
    198 
    199 	/* Run any shutdown hooks */
    200 	doshutdownhooks();
    201 
    202 	pmf_system_shutdown(boothowto);
    203 
    204 	/* Make sure IRQ's are disabled */
    205 	IRQdisable;
    206 
    207 	if (howto & RB_HALT) {
    208 		printf("\r\n");
    209 		printf("The operating system has halted.\r\n");
    210 		printf("Please press any key to reboot.\r\n");
    211 		cngetc();
    212 	}
    213 
    214 	printf("\r\nrebooting...\r\n");
    215  reset:
    216 	/*
    217 	 * Make really really sure that all interrupts are disabled,
    218 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    219 	 */
    220 	(void) disable_interrupts(I32_bit|F32_bit);
    221 	STREG(ST_WDMR)	= ST_WDMR_EXTEN | ST_WDMR_RSTEN | 1;
    222 	STREG(ST_CR)	= ST_CR_WDRST;
    223 	for (;;);
    224 }
    225 
    226 /*
    227  * vaddr_t initarm(...)
    228  *
    229  * Initial entry point on startup. This gets called before main() is
    230  * entered.
    231  * It should be responsible for setting up everything that must be
    232  * in place when main is called.
    233  * This includes
    234  *   Taking a copy of the boot configuration structure.
    235  *   Initialising the physical console so characters can be printed.
    236  *   Setting up page tables for the kernel
    237  *   Initialising interrupt controllers to a sane default state
    238  */
    239 vaddr_t
    240 initarm(void *arg)
    241 {
    242 	vaddr_t sp;
    243 	/*
    244 	 * basic AT91 initialization:
    245 	 */
    246 	if (at91bus_init())
    247 		panic("%s: at91bus_init() failed", __FUNCTION__);
    248 
    249 	if (AT91_CHIP_ID() == AT91SAM9260_CHIP_ID) {
    250 		memcpy(&mmnetbus, at91bus_tag, sizeof(mmnetbus));
    251 		mmnetbus.init = mmnetbus_init;
    252 		mmnetbus.peripheral_clock = mmnetbus_peripheral_clock;
    253 		mmnetbus.gpio_mask = mmnetbus_gpio_mask;
    254 		old_at91bus_tag = at91bus_tag;
    255 		at91bus_tag = &mmnetbus;
    256 	}
    257 
    258  	/* Fake bootconfig structure for the benefit of pmap.c */
    259  	/* XXX must make the memory description h/w independent */
    260  	bootconfig.dramblocks = 1;
    261  	bootconfig.dram[0].address = 0x20000000UL;
    262  	bootconfig.dram[0].pages =   0x04000000UL / PAGE_SIZE;
    263 	sp = at91bus_setup(&bootconfig);
    264 
    265 	if (AT91_CHIP_ID() != AT91SAM9260_CHIP_ID)
    266 		panic("%s: processor is not AT91SAM9260", __FUNCTION__);
    267 
    268 	/* we've a specific device_register routine */
    269 	evbarm_device_register = mmnet_device_register;
    270 
    271 	/* We return the new stack pointer address */
    272 	return sp;
    273 }
    274 
    275 
    276 bus_dma_tag_t
    277 at91_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    278 {
    279 	int i;
    280 	struct arm32_bus_dma_tag *dmat;
    281 
    282 	for (i = 0; i < bootconfig.dramblocks; i++) {
    283 		mmnet_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    284 		mmnet_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    285 		mmnet_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    286 			PAGE_SIZE;
    287 	}
    288 
    289 	dmat = dma_tag_template;
    290 
    291 	dmat->_ranges = mmnet_dma_ranges;
    292 	dmat->_nranges = bootconfig.dramblocks;
    293 
    294 	return dmat;
    295 }
    296 
    297 void mmnetbus_init(struct at91bus_clocks *clocks)
    298 {
    299 	(*old_at91bus_tag->init)(clocks);
    300 }
    301 
    302 uint32_t mmnetbus_gpio_mask(int pid)
    303 {
    304 	switch (pid) {
    305 	case PID_PIOA:	return ~0x00000300U;
    306 	case PID_PIOB:	return ~0x0000783FU;
    307 	case PID_PIOC:	return ~0x00000000U;
    308 	default:	return ~0x00000000U;
    309 	}
    310 }
    311 
    312 void mmnetbus_peripheral_clock(int pid, int enable)
    313 {
    314 	switch (pid) {
    315 	case PID_TWI:
    316 		if (enable) {
    317 			PIOA_WRITE(PIO_ASR, 0x06000000);
    318 			PIOA_WRITE(PIO_PDR, 0x06000000);
    319 			PIOA_WRITE(PIO_MDER, 0x06000000);
    320 		}
    321 		break;
    322 
    323 	case PID_SPI0:
    324 		if (enable) {
    325 			PIOA_WRITE(PIO_ASR, 0x0000000f);
    326 			PIOA_WRITE(PIO_PDR, 0x0000000f);
    327 		}
    328 		break;
    329 
    330 	case PID_SPI1:
    331 		if (enable) {
    332 			PIOB_WRITE(PIO_ASR, 0x0000000f);
    333 			PIOB_WRITE(PIO_PDR, 0x0000000f);
    334 		}
    335 		break;
    336 	}
    337 	(*old_at91bus_tag->peripheral_clock)(pid, enable);
    338 }
    339 
    340 
    341 static void mmnet_device_register(device_t dev, void *aux)
    342 {
    343 #if 0
    344 	if (device_is_a(dev, "at91semac")) {
    345 		// propagate mac address from u-boot
    346 	}
    347 #endif
    348 }
    349