Home | History | Annotate | Line # | Download | only in obs405
obs600_machdep.c revision 1.12
      1 /*	$NetBSD: obs600_machdep.c,v 1.12 2021/03/06 08:29:19 rin Exp $	*/
      2 /*	Original: md_machdep.c,v 1.3 2005/01/24 18:47:37 shige Exp $	*/
      3 
      4 /*
      5  * Copyright 2001, 2002 Wasabi Systems, Inc.
      6  * All rights reserved.
      7  *
      8  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed for the NetBSD Project by
     21  *      Wasabi Systems, Inc.
     22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     41  * Copyright (C) 1995, 1996 TooLs GmbH.
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by TooLs GmbH.
     55  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     62  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     63  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     64  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     65  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     66  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     67  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: obs600_machdep.c,v 1.12 2021/03/06 08:29:19 rin Exp $");
     72 
     73 #include "opt_compat_netbsd.h"
     74 #include "opt_ddb.h"
     75 #include "opt_modular.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/bus.h>
     79 #include <sys/errno.h>
     80 #include <sys/kernel.h>
     81 #include <sys/ksyms.h>
     82 #include <sys/mount.h>
     83 #include <sys/reboot.h>
     84 #include <sys/systm.h>
     85 #include <sys/device.h>
     86 #include <sys/module.h>
     87 #include <sys/bus.h>
     88 #include <sys/cpu.h>
     89 
     90 #include <uvm/uvm_extern.h>
     91 
     92 #include <machine/obs600.h>
     93 
     94 #include <powerpc/ibm4xx/amcc405ex.h>
     95 #include <powerpc/ibm4xx/cpu.h>
     96 #include <powerpc/ibm4xx/dcr4xx.h>
     97 #include <powerpc/ibm4xx/dev/comopbvar.h>
     98 #include <powerpc/ibm4xx/dev/gpiicreg.h>
     99 #include <powerpc/ibm4xx/dev/opbvar.h>
    100 
    101 #include <powerpc/spr.h>
    102 #include <powerpc/ibm4xx/spr.h>
    103 
    104 #include <dev/ic/comreg.h>
    105 
    106 #include "ksyms.h"
    107 
    108 #include "com.h"
    109 #if (NCOM > 0)
    110 #include <sys/termios.h>
    111 
    112 #ifndef CONADDR
    113 #define CONADDR		AMCC405EX_UART0_BASE
    114 #endif
    115 #ifndef CONSPEED
    116 #define CONSPEED	B115200
    117 #endif
    118 #ifndef CONMODE
    119 			/* 8N1 */
    120 #define CONMODE		((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
    121 #endif
    122 #endif	/* NCOM */
    123 
    124 /*
    125  * XXXX:
    126  * It is very troublesome though we can calculate from various registers. X-<
    127  */
    128 #define OBS600_CPU_FREQ	(600 * 1000 * 1000)
    129 #define OBS600_MEM_SIZE	(1 * 1024 * 1024 * 1024)
    130 
    131 #define	TLB_PG_SIZE 	(16 * 1024 * 1024)
    132 
    133 /*
    134  * Global variables used here and there
    135  */
    136 char bootpath[256];
    137 
    138 extern paddr_t msgbuf_paddr;
    139 
    140 void initppc(vaddr_t, vaddr_t, int, char *[], char *);
    141 static int read_eeprom(int, char *);
    142 
    143 
    144 void
    145 initppc(vaddr_t startkernel, vaddr_t endkernel, int argc, char *argv[],
    146 	char *argstr)
    147 {
    148 	vaddr_t va;
    149 	u_int memsize;
    150 
    151 	memsize = OBS600_MEM_SIZE;
    152 
    153 	/* Linear map kernel memory */
    154 	for (va = 0; va < endkernel; va += TLB_PG_SIZE)
    155 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
    156 
    157 	/*
    158 	 * Map console and I2C after RAM. (see pmap_tlbmiss())
    159 	 * All peripherals mapped on a page.
    160 	 */
    161 	ppc4xx_tlb_reserve(AMCC405EX_OPB_BASE, roundup(memsize, TLB_PG_SIZE),
    162 	    TLB_PG_SIZE, TLB_I | TLB_G);
    163 
    164 	/* Initialize AMCC 405EX CPU */
    165 	ibm40x_memsize_init(memsize, startkernel);
    166 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
    167 
    168 #ifdef DDB
    169 	if (boothowto & RB_KDB)
    170 		Debugger();
    171 #endif
    172 
    173 	/*
    174 	 * Look for the ibm4xx modules in the right place.
    175 	 */
    176 	module_machine = module_machine_ibm4xx;
    177 }
    178 
    179 void
    180 consinit(void)
    181 {
    182 
    183 #if (NCOM > 0)
    184 	com_opb_cnattach(OBS600_COM_FREQ, CONADDR, CONSPEED, CONMODE);
    185 #endif /* NCOM */
    186 }
    187 
    188 /*
    189  * Machine dependent startup code.
    190  */
    191 void
    192 cpu_startup(void)
    193 {
    194 	prop_number_t pn;
    195 	prop_data_t pd;
    196 	u_char *macaddr, *macaddr1;
    197 	static u_char buf[16];	/* MAC address x2 buffer */
    198 
    199 	/*
    200 	 * cpu common startup
    201 	 */
    202 	ibm4xx_cpu_startup("OpenBlockS600 AMCC PowerPC 405EX Board");
    203 
    204 	/*
    205 	 * Set up the board properties database.
    206 	 */
    207 	board_info_init();
    208 
    209 	pn = prop_number_create_integer(OBS600_CPU_FREQ);
    210 	KASSERT(pn != NULL);
    211 	if (prop_dictionary_set(board_properties, "processor-frequency", pn) ==
    212 	    false)
    213 		panic("setting processor-frequency");
    214 	prop_object_release(pn);
    215 
    216 	pn = prop_number_create_integer(OBS600_MEM_SIZE);
    217 	KASSERT(pn != NULL);
    218 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
    219 		panic("setting mem-size");
    220 	prop_object_release(pn);
    221 
    222 	calc_delayconst(); /* required by read_eeprom() */
    223 
    224 #define ETHER_ADDR_LEN	6
    225 
    226 	read_eeprom(sizeof(buf), buf);
    227 	macaddr = &buf[0];
    228 	macaddr1 = &buf[8];
    229 
    230 	pd = prop_data_create_data_nocopy(macaddr, ETHER_ADDR_LEN);
    231 	KASSERT(pd != NULL);
    232 	if (prop_dictionary_set(board_properties, "emac0-mac-addr", pd) ==
    233 	    false)
    234 		panic("setting emac0-mac-addr");
    235 	prop_object_release(pd);
    236 	pd = prop_data_create_data_nocopy(macaddr1, ETHER_ADDR_LEN);
    237 	KASSERT(pd != NULL);
    238 	if (prop_dictionary_set(board_properties, "emac1-mac-addr", pd) ==
    239 	    false)
    240 		panic("setting emac1-mac-addr");
    241 	prop_object_release(pd);
    242 
    243 	/* emac0 connects to phy 2 and emac1 to phy 3 via RGMII. */
    244 	pn = prop_number_create_integer(2);
    245 	KASSERT(pn != NULL);
    246 	if (prop_dictionary_set(board_properties, "emac0-mii-phy", pn) == false)
    247 		panic("setting emac0-mii-phy");
    248 	prop_object_release(pn);
    249 	pn = prop_number_create_integer(3);
    250 	KASSERT(pn != NULL);
    251 	if (prop_dictionary_set(board_properties, "emac1-mii-phy", pn) == false)
    252 		panic("setting emac1-mii-phy");
    253 	prop_object_release(pn);
    254 
    255 	/*
    256 	 * Now that we have VM, malloc()s are OK in bus_space.
    257 	 */
    258 	bus_space_mallocok();
    259 
    260 	/*
    261 	 * no fake mapiodev
    262 	 */
    263 	fake_mapiodev = 0;
    264 
    265 	splraise(-1);
    266 }
    267 
    268 /*
    269  * Halt or reboot the machine after syncing/dumping according to howto.
    270  */
    271 void
    272 cpu_reboot(int howto, char *what)
    273 {
    274 	static int syncing;
    275 	static char str[256];
    276 	char *ap = str, *ap1 = ap;
    277 
    278 	boothowto = howto;
    279 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    280 		syncing = 1;
    281 		vfs_shutdown();		/* sync */
    282 		resettodr();		/* set wall clock */
    283 	}
    284 
    285 	splhigh();
    286 
    287 	if (!cold && (howto & RB_DUMP))
    288 		ibm4xx_dumpsys();
    289 
    290 	doshutdownhooks();
    291 
    292 	pmf_system_shutdown(boothowto);
    293 
    294 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    295 	  /* Power off here if we know how...*/
    296 	}
    297 
    298 	if (howto & RB_HALT) {
    299 		printf("halted\n\n");
    300 
    301 #if 0
    302 		goto reboot;	/* XXX for now... */
    303 #endif
    304 
    305 #ifdef DDB
    306 		printf("dropping to debugger\n");
    307 		while(1)
    308 			Debugger();
    309 #endif
    310 	}
    311 
    312 	printf("rebooting\n\n");
    313 	if (what && *what) {
    314 		if (strlen(what) > sizeof str - 5)
    315 			printf("boot string too large, ignored\n");
    316 		else {
    317 			strcpy(str, what);
    318 			ap1 = ap = str + strlen(str);
    319 			*ap++ = ' ';
    320 		}
    321 	}
    322 	*ap++ = '-';
    323 	if (howto & RB_SINGLE)
    324 		*ap++ = 's';
    325 	if (howto & RB_KDB)
    326 		*ap++ = 'd';
    327 	*ap++ = 0;
    328 	if (ap[-2] == '-')
    329 		*ap1 = 0;
    330 
    331 	/* flush cache for msgbuf */
    332 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    333 
    334 #if 0
    335  reboot:
    336 #endif
    337 	ppc4xx_reset();
    338 
    339 	printf("ppc4xx_reset() failed!\n");
    340 #ifdef DDB
    341 	while(1)
    342 		Debugger();
    343 #else
    344 	while (1)
    345 		/* nothing */;
    346 #endif
    347 }
    348 
    349 /*
    350  * Read EEPROM via I2C.  We don't use bus_space(9) here.  This is MD-part and,
    351  * don't support bus_space_unmap() to a space on reserved space? X-<
    352  *
    353  * XXXX: Also this function assume already initialized for I2C...
    354  */
    355 static int
    356 read_eeprom(int len, char *buf)
    357 {
    358 	volatile uint8_t *iic0;
    359 #define IIC0_READ(r)		(*(iic0 + (r)))
    360 #define IIC0_WRITE(r, v)	(*(iic0 + (r)) = (v))
    361 	uint8_t sts;
    362 	int cnt, i = 0;
    363 
    364 #define I2C_EEPROM_ADDR	0x52
    365 
    366 	if ((iic0 = ppc4xx_tlb_mapiodev(AMCC405EX_IIC0_BASE, IIC_NREG)) == NULL)
    367 		return ENOMEM; /* ??? */
    368 
    369 	/* Clear Stop Complete Bit */
    370 	IIC0_WRITE(IIC_STS, IIC_STS_SCMP);
    371 	/* Check init */
    372 	do {
    373 		/* Get status */
    374 		sts = IIC0_READ(IIC_STS);
    375 	} while ((sts & IIC_STS_PT));
    376 
    377 	IIC0_WRITE(IIC_MDCNTL,
    378 	    IIC0_READ(IIC_MDCNTL) | IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
    379 
    380 	/* 7-bit adressing */
    381 	IIC0_WRITE(IIC_HMADR, 0);
    382 	IIC0_WRITE(IIC_LMADR, I2C_EEPROM_ADDR << 1);
    383 
    384 	IIC0_WRITE(IIC_MDBUF, 0);
    385 	IIC0_WRITE(IIC_CNTL, IIC_CNTL_PT);
    386 	do {
    387 		/* Get status */
    388 		sts = IIC0_READ(IIC_STS);
    389 	} while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
    390 
    391 	cnt = 0;
    392 	while (cnt < len) {
    393 		/* always read 4byte */
    394 		IIC0_WRITE(IIC_CNTL,
    395 		    IIC_CNTL_PT				|
    396 		    IIC_CNTL_RW				|
    397 		    ((cnt == 0) ? IIC_CNTL_RPST : 0)	|
    398 		    IIC_CNTL_TCT);
    399 		do {
    400 			/* Get status */
    401 			sts = IIC0_READ(IIC_STS);
    402 		} while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
    403 
    404 		if ((sts & IIC_STS_PT) || (sts & IIC_STS_ERR))
    405 			break;
    406 		if (sts & IIC_STS_MDBS) {
    407 			delay(1);
    408 			/* read 4byte */
    409 			for (i = 0; i < 4 && cnt < len; i++, cnt++)
    410 				buf[cnt] = IIC0_READ(IIC_MDBUF);
    411 		}
    412 	}
    413 	for ( ; i < 4; i++)
    414 		(void) IIC0_READ(IIC_MDBUF);
    415 
    416 	return (cnt == len) ? 0 : EINVAL;
    417 }
    418