Home | History | Annotate | Line # | Download | only in mpcsa
mpcsa_machdep.c revision 1.2
      1 /*	$Id: mpcsa_machdep.c,v 1.2 2008/07/03 01:15:39 matt Exp $	*/
      2 /*	$NetBSD: mpcsa_machdep.c,v 1.2 2008/07/03 01:15:39 matt 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 
     79 #include <sys/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: mpcsa_machdep.c,v 1.2 2008/07/03 01:15:39 matt Exp $");
     81 
     82 #include "opt_ddb.h"
     83 #include "opt_kgdb.h"
     84 #include "opt_pmap_debug.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/device.h>
     88 #include <sys/systm.h>
     89 #include <sys/kernel.h>
     90 #include <sys/exec.h>
     91 #include <sys/proc.h>
     92 #include <sys/msgbuf.h>
     93 #include <sys/reboot.h>
     94 #include <sys/termios.h>
     95 #include <sys/ksyms.h>
     96 
     97 #include <uvm/uvm_extern.h>
     98 
     99 #include <dev/cons.h>
    100 
    101 #include <machine/db_machdep.h>
    102 #include <ddb/db_sym.h>
    103 #include <ddb/db_extern.h>
    104 
    105 #define	DRAM_BLOCKS	1
    106 #include <machine/bootconfig.h>
    107 #include <machine/bus.h>
    108 #include <machine/cpu.h>
    109 #include <machine/frame.h>
    110 #include <arm/undefined.h>
    111 
    112 #include <arm/arm32/machdep.h>
    113 
    114 #include <arm/at91/at91reg.h>
    115 #include <arm/at91/at91var.h>
    116 
    117 #include <dev/ic/comreg.h>
    118 #include <dev/ic/comvar.h>
    119 
    120 #include <machine/autoconf.h>
    121 
    122 #include "seeprom.h"
    123 #if NSEEPROM > 0
    124 #include <net/if.h>
    125 #include <net/if_ether.h>
    126 #include <dev/i2c/i2cvar.h>
    127 #include <dev/i2c/at24cxxvar.h>
    128 #endif
    129 #include <arm/at91/at91twivar.h>
    130 
    131 #if TODO
    132 #include "epcom.h"
    133 #if NEPCOM > 0
    134 #include <arm/ep93xx/epcomvar.h>
    135 #endif
    136 
    137 #include "isa.h"
    138 #if NISA > 0
    139 #include <dev/isa/isareg.h>
    140 #include <dev/isa/isavar.h>
    141 #endif
    142 
    143 #include <machine/isa_machdep.h>
    144 
    145 #include <evbarm/mpcsa/mpcsareg.h>
    146 #endif	// TODO
    147 
    148 #include <arm/at91/at91rm9200reg.h>
    149 #include <arm/at91/at91rm9200busvar.h>
    150 
    151 #include "opt_ipkdb.h"
    152 #include "ksyms.h"
    153 
    154 #include <arm/at91/at91busvar.h>
    155 #include <arm/at91/at91pdcreg.h>
    156 #include <arm/at91/at91dbgureg.h>
    157 #include <arm/at91/at91reg.h>
    158 #include <arm/at91/at91streg.h>
    159 
    160 /*
    161  * Address to call from cpu_reset() to reset the machine.
    162  * This is machine architecture dependant as it varies depending
    163  * on where the ROM appears when you turn the MMU off.
    164  */
    165 
    166 u_int cpu_reset_address = 0x00000000;
    167 
    168 
    169 /* boot configuration: */
    170 BootConfig bootconfig;		/* Boot config storage */
    171 char *boot_args = NULL;
    172 char *boot_file = NULL;
    173 
    174 /* hmmm */
    175 static struct arm32_dma_range mpcsa_dma_ranges[4];
    176 static struct at91bus_machdep mpcsabus;
    177 static at91bus_tag_t old_at91bus_tag;
    178 
    179 /* Prototypes */
    180 static void mpcsa_device_register(device_t dev, void *aux);
    181 static void mpcsabus_init(struct at91bus_clocks *);
    182 static void mpcsabus_peripheral_clock(int pid, int enable);
    183 static uint32_t mpcsabus_gpio_mask(int pid);
    184 
    185 
    186 /*
    187  * void cpu_reboot(int howto, char *bootstr)
    188  *
    189  * Reboots the system
    190  *
    191  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    192  * then reset the CPU.
    193  */
    194 void
    195 cpu_reboot(int howto, char *bootstr)
    196 {
    197 
    198 	/*
    199 	 * If we are still cold then hit the air brakes
    200 	 * and crash to earth fast
    201 	 */
    202 	if (cold) {
    203 		doshutdownhooks();
    204 		printf("\r\n");
    205 		printf("The operating system has halted.\r\n");
    206 		printf("Please press any key to reboot.\r\n");
    207 		cngetc();
    208 		printf("\r\nrebooting...\r\n");
    209 		goto reset;
    210 	}
    211 
    212 	/* Disable console buffering */
    213 
    214 	/*
    215 	 * If RB_NOSYNC was not specified sync the discs.
    216 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    217 	 * unmount.  It looks like syslogd is getting woken up only to find
    218 	 * that it cannot page part of the binary in as the filesystem has
    219 	 * been unmounted.
    220 	 */
    221 	if (!(howto & RB_NOSYNC))
    222 		bootsync();
    223 
    224 	/* Say NO to interrupts */
    225 	splhigh();
    226 
    227 	/* Do a dump if requested. */
    228 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    229 		dumpsys();
    230 
    231 	/* Run any shutdown hooks */
    232 	doshutdownhooks();
    233 
    234 	/* Make sure IRQ's are disabled */
    235 	IRQdisable;
    236 
    237 	if (howto & RB_HALT) {
    238 		printf("\r\n");
    239 		printf("The operating system has halted.\r\n");
    240 		printf("Please press any key to reboot.\r\n");
    241 		cngetc();
    242 	}
    243 
    244 	printf("\r\nrebooting...\r\n");
    245  reset:
    246 	/*
    247 	 * Make really really sure that all interrupts are disabled,
    248 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    249 	 */
    250 	(void) disable_interrupts(I32_bit|F32_bit);
    251 	STREG(ST_WDMR)	= ST_WDMR_EXTEN | ST_WDMR_RSTEN | 1;
    252 	STREG(ST_CR)	= ST_CR_WDRST;
    253 	for (;;);
    254 }
    255 
    256 /*
    257  * u_int initarm(...)
    258  *
    259  * Initial entry point on startup. This gets called before main() is
    260  * entered.
    261  * It should be responsible for setting up everything that must be
    262  * in place when main is called.
    263  * This includes
    264  *   Taking a copy of the boot configuration structure.
    265  *   Initialising the physical console so characters can be printed.
    266  *   Setting up page tables for the kernel
    267  *   Initialising interrupt controllers to a sane default state
    268  */
    269 u_int
    270 initarm(void *arg)
    271 {
    272 	u_int ret;
    273 	/*
    274 	 * basic AT91 initialization:
    275 	 */
    276 	if (at91bus_init())
    277 		panic("%s: at91bus_init() failed", __FUNCTION__);
    278 
    279 	if (AT91_CHIP_ID() == AT91RM9200_CHIP_ID) {
    280 		memcpy(&mpcsabus, at91bus_tag, sizeof(mpcsabus));
    281 		mpcsabus.init = mpcsabus_init;
    282 		mpcsabus.peripheral_clock = mpcsabus_peripheral_clock;
    283 		mpcsabus.gpio_mask = mpcsabus_gpio_mask;
    284 		old_at91bus_tag = at91bus_tag;
    285 		at91bus_tag = &mpcsabus;
    286 	}
    287 
    288  	/* Fake bootconfig structure for the benefit of pmap.c */
    289  	/* XXX must make the memory description h/w independent */
    290  	bootconfig.dramblocks = 1;
    291  	bootconfig.dram[0].address = 0x20000000UL;
    292  	bootconfig.dram[0].pages =   0x04000000UL / PAGE_SIZE;
    293 	ret = at91bus_setup(&bootconfig);
    294 
    295 	if (AT91_CHIP_ID() != AT91RM9200_CHIP_ID)
    296 		panic("%s: processor is not AT91RM9200", __FUNCTION__);
    297 
    298 	/* we've a specific device_register routine */
    299 	evbarm_device_register = mpcsa_device_register;
    300 
    301 	/* We return the new stack pointer address */
    302 	return ret;
    303 }
    304 
    305 
    306 bus_dma_tag_t
    307 at91_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
    308 {
    309 	int i;
    310 	struct arm32_bus_dma_tag *dmat;
    311 
    312 	for (i = 0; i < bootconfig.dramblocks; i++) {
    313 		mpcsa_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
    314 		mpcsa_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
    315 		mpcsa_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
    316 			PAGE_SIZE;
    317 	}
    318 
    319 	dmat = dma_tag_template;
    320 
    321 	dmat->_ranges = mpcsa_dma_ranges;
    322 	dmat->_nranges = bootconfig.dramblocks;
    323 
    324 	return dmat;
    325 }
    326 
    327 void mpcsabus_init(struct at91bus_clocks *clocks)
    328 {
    329 	(*old_at91bus_tag->init)(clocks);
    330 }
    331 
    332 uint32_t mpcsabus_gpio_mask(int pid)
    333 {
    334 	switch (pid) {
    335 	case PID_PIOA:	return ~0x00000300U;
    336 	case PID_PIOB:	return ~0x0000783FU;
    337 	case PID_PIOC:	return ~0x00000000U;
    338 	case PID_PIOD:	return ~0x0003F000U;
    339 	default:	return ~0x00000000U;
    340 	}
    341 }
    342 
    343 void mpcsabus_peripheral_clock(int pid, int enable)
    344 {
    345 	switch (pid) {
    346 	case PID_TWI:
    347 		if (enable) {
    348 			PIOA_WRITE(PIO_ASR, 0x06000000);	// assign to peripheral A
    349 			PIOA_WRITE(PIO_PDR, 0x06000000);	// assign to peripherals
    350 			PIOA_WRITE(PIO_MDER, 0x06000000);	// I2C pins in open-drain mode
    351 		}
    352 		break;
    353 
    354 	case PID_SPI:
    355 		if (enable) {
    356 			PIOA_WRITE(PIO_ASR, 0x00000007);	// assign to peripheral A
    357 			PIOA_WRITE(PIO_PDR, 0x00000007);	// assign to peripherals
    358 		}
    359 		break;
    360 	}
    361 	(*old_at91bus_tag->peripheral_clock)(pid, enable);
    362 }
    363 
    364 
    365 static void mpcsa_device_register(device_t dev, void *aux)
    366 {
    367 	static uint8_t eth_addr[ETHER_ADDR_LEN];
    368 
    369 	if (device_is_a(dev, "at91emac")) {
    370 		cfdriver_t cd = config_cfdriver_lookup("at91twi");
    371 		device_t twi_dev = 0;
    372 		i2c_tag_t i2c = 0;
    373 		if (cd && (twi_dev = device_lookup(cd, 0)) != NULL) {
    374 			struct at91twi_softc *sc = (struct at91twi_softc *)twi_dev;
    375 			i2c = &sc->sc_i2c;
    376 		}
    377 		if (i2c && seeprom_bootstrap_read(i2c, 0x50, 0x00, 4096,
    378 					   eth_addr, ETHER_ADDR_LEN) == 0) {
    379 			prop_data_t pd = prop_data_create_data_nocopy(
    380 				eth_addr, ETHER_ADDR_LEN);
    381 			KASSERT(pd != NULL);
    382 			if (prop_dictionary_set(device_properties(dev),
    383 						"mac-addr", pd) == FALSE) {
    384 				printf("WARNING: unable to set mac-addr property "
    385 				       "for %s\n", dev->dv_xname);
    386 			}
    387 		} else {
    388 			printf("%s: WARNING: unable to read MAC address from SEEPROM\n",
    389 			       dev->dv_xname);
    390 		}
    391 	}
    392 }
    393 
    394