xref: /netbsd-src/sys/arch/evbppc/obs405/obs600_machdep.c (revision c505c4429840c353a86d4eb53b5e2bfc0092264e)
1 /*	$NetBSD: obs600_machdep.c,v 1.1 2010/03/18 14:15:38 kiyohara 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.1 2010/03/18 14:15:38 kiyohara Exp $");
72 
73 #include "opt_compat_netbsd.h"
74 #include "opt_ddb.h"
75 #include "opt_ipkdb.h"
76 #include "opt_modular.h"
77 
78 #include <sys/param.h>
79 #include <sys/bus.h>
80 #include <sys/errno.h>
81 #include <sys/kernel.h>
82 #include <sys/ksyms.h>
83 #include <sys/mount.h>
84 #include <sys/reboot.h>
85 #include <sys/systm.h>
86 #include <sys/device.h>
87 
88 #include <uvm/uvm.h>
89 #include <uvm/uvm_extern.h>
90 
91 #include <machine/bus.h>
92 #include <machine/cpu.h>
93 #include <machine/obs600.h>
94 
95 #include <powerpc/ibm4xx/amcc405ex.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 #include <powerpc/ibm4xx/spr.h>
101 #include <powerpc/spr.h>
102 
103 #include <dev/ic/comreg.h>
104 
105 #include "ksyms.h"
106 
107 #include "com.h"
108 #if (NCOM > 0)
109 #include <sys/termios.h>
110 
111 #ifndef CONADDR
112 #define CONADDR		AMCC405EX_UART0_BASE
113 #endif
114 #ifndef CONSPEED
115 #define CONSPEED	B115200
116 #endif
117 #ifndef CONMODE
118 			/* 8N1 */
119 #define CONMODE		((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
120 #endif
121 #endif	/* NCOM */
122 
123 /*
124  * XXXX:
125  * It is very troublesome though we can calculate from various registers. X-<
126  */
127 #define OBS600_CPU_FREQ	(600 * 1000 * 1000)
128 #define OBS600_MEM_SIZE	(1 * 1024 * 1024 * 1024)
129 
130 #define	TLB_PG_SIZE 	(16 * 1024 * 1024)
131 
132 /*
133  * Global variables used here and there
134  */
135 char bootpath[256];
136 
137 extern paddr_t msgbuf_paddr;
138 
139 #if NKSYMS || defined(DDB) || defined(MODULAR)
140 void *startsym, *endsym;
141 #endif
142 
143 void initppc(u_int, u_int, int, char *[], char *);
144 int lcsplx(int);
145 static int read_eeprom(int, char *);
146 
147 
148 void
149 initppc(u_int startkernel, u_int endkernel, int argc, char *argv[],
150 	char *argstr)
151 {
152 	vaddr_t va;
153 	u_int memsize;
154 
155 	memsize = OBS600_MEM_SIZE;
156 
157 	/* Linear map kernel memory */
158 	for (va = 0; va < endkernel; va += TLB_PG_SIZE)
159 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
160 
161 	/*
162 	 * Map console and I2C after RAM. (see pmap_tlbmiss())
163 	 * All peripherals mapped on a page.
164 	 */
165 	ppc4xx_tlb_reserve(AMCC405EX_OPB_BASE, roundup(memsize, TLB_PG_SIZE),
166 	    TLB_PG_SIZE, TLB_I | TLB_G);
167 
168 	/* Initialize AMCC 405EX CPU */
169 	ibm40x_memsize_init(memsize, startkernel);
170 	ibm4xx_init((void (*)(void))ext_intr);
171 
172 	/* Disable Watchdog, PIT and FIT interrupts. (u-boot uses PIT...) */
173 	mtspr(SPR_TCR, 0);
174 
175 	/*
176 	 * Set the page size.
177 	 */
178 	uvm_setpagesize();
179 
180 	/*
181 	 * Initialize pmap module.
182 	 */
183 	pmap_bootstrap(startkernel, endkernel);
184 
185 
186 #if NKSYMS || defined(DDB) || defined(MODULAR)
187 	ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
188 #endif
189 #ifdef DDB
190 	if (boothowto & RB_KDB)
191 		Debugger();
192 #endif
193 #ifdef IPKDB
194 	/*
195 	 * Now trap to IPKDB
196 	 */
197 	ipkdb_init();
198 	if (boothowto & RB_KDB)
199 		ipkdb_connect(0);
200 #endif
201 }
202 
203 void
204 consinit(void)
205 {
206 
207 #if (NCOM > 0)
208 	com_opb_cnattach(OBS600_COM_FREQ, CONADDR, CONSPEED, CONMODE);
209 #endif /* NCOM */
210 }
211 
212 int
213 lcsplx(int ipl)
214 {
215 
216 	return spllower(ipl); 	/* XXX */
217 }
218 
219 
220 /*
221  * Machine dependent startup code.
222  */
223 void
224 cpu_startup(void)
225 {
226 	prop_number_t pn;
227 	prop_data_t pd;
228 	u_char *macaddr, *macaddr1;
229 	static u_char buf[16];	/* MAC address x2 buffer */
230 
231 	/*
232 	 * cpu common startup
233 	 */
234 	ibm4xx_cpu_startup("OpenBlockS600 AMCC PowerPC 405EX Board");
235 
236 	/*
237 	 * Set up the board properties database.
238 	 */
239 	board_info_init();
240 
241 	read_eeprom(sizeof(buf), buf);
242 	macaddr = &buf[0];
243 	macaddr1 = &buf[8];
244 
245 	pn = prop_number_create_integer(OBS600_CPU_FREQ);
246 	KASSERT(pn != NULL);
247 	if (prop_dictionary_set(board_properties, "processor-frequency", pn) ==
248 	    false)
249 		panic("setting processor-frequency");
250 	prop_object_release(pn);
251 
252 	pn = prop_number_create_integer(OBS600_MEM_SIZE);
253 	KASSERT(pn != NULL);
254 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
255 		panic("setting mem-size");
256 	prop_object_release(pn);
257 
258 #define ETHER_ADDR_LEN	6
259 
260 	pd = prop_data_create_data_nocopy(macaddr, ETHER_ADDR_LEN);
261 	KASSERT(pd != NULL);
262 	if (prop_dictionary_set(board_properties, "emac0-mac-addr", pd) ==
263 	    false)
264 		panic("setting emac0-mac-addr");
265 	prop_object_release(pd);
266 	pd = prop_data_create_data_nocopy(macaddr1, ETHER_ADDR_LEN);
267 	KASSERT(pd != NULL);
268 	if (prop_dictionary_set(board_properties, "emac1-mac-addr", pd) ==
269 	    false)
270 		panic("setting emac1-mac-addr");
271 	prop_object_release(pd);
272 
273 	/* emac0 connects to phy 2 and emac1 to phy 3 via RGMII. */
274 	pn = prop_number_create_integer(2);
275 	KASSERT(pn != NULL);
276 	if (prop_dictionary_set(board_properties, "emac0-mii-phy", pn) == false)
277 		panic("setting emac0-mii-phy");
278 	prop_object_release(pn);
279 	pn = prop_number_create_integer(3);
280 	KASSERT(pn != NULL);
281 	if (prop_dictionary_set(board_properties, "emac1-mii-phy", pn) == false)
282 		panic("setting emac1-mii-phy");
283 	prop_object_release(pn);
284 
285 	/*
286 	 * Now that we have VM, malloc()s are OK in bus_space.
287 	 */
288 	bus_space_mallocok();
289 
290 	/*
291 	 * no fake mapiodev
292 	 */
293 	fake_mapiodev = 0;
294 }
295 
296 /*
297  * Halt or reboot the machine after syncing/dumping according to howto.
298  */
299 void
300 cpu_reboot(int howto, char *what)
301 {
302 	static int syncing;
303 	static char str[256];
304 	char *ap = str, *ap1 = ap;
305 
306 	boothowto = howto;
307 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
308 		syncing = 1;
309 		vfs_shutdown();		/* sync */
310 		resettodr();		/* set wall clock */
311 	}
312 
313 	splhigh();
314 
315 	if (!cold && (howto & RB_DUMP))
316 		ibm4xx_dumpsys();
317 
318 	doshutdownhooks();
319 
320 	pmf_system_shutdown(boothowto);
321 
322 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
323 	  /* Power off here if we know how...*/
324 	}
325 
326 	if (howto & RB_HALT) {
327 		printf("halted\n\n");
328 
329 #if 0
330 		goto reboot;	/* XXX for now... */
331 #endif
332 
333 #ifdef DDB
334 		printf("dropping to debugger\n");
335 		while(1)
336 			Debugger();
337 #endif
338 	}
339 
340 	printf("rebooting\n\n");
341 	if (what && *what) {
342 		if (strlen(what) > sizeof str - 5)
343 			printf("boot string too large, ignored\n");
344 		else {
345 			strcpy(str, what);
346 			ap1 = ap = str + strlen(str);
347 			*ap++ = ' ';
348 		}
349 	}
350 	*ap++ = '-';
351 	if (howto & RB_SINGLE)
352 		*ap++ = 's';
353 	if (howto & RB_KDB)
354 		*ap++ = 'd';
355 	*ap++ = 0;
356 	if (ap[-2] == '-')
357 		*ap1 = 0;
358 
359 	/* flush cache for msgbuf */
360 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
361 
362 #if 0
363  reboot:
364 #endif
365 	ppc4xx_reset();
366 
367 	printf("ppc4xx_reset() failed!\n");
368 #ifdef DDB
369 	while(1)
370 		Debugger();
371 #else
372 	while (1)
373 		/* nothing */;
374 #endif
375 }
376 
377 /* This function assume already initialized for I2C... */
378 static int
379 read_eeprom(int len, char *buf)
380 {
381 	bus_space_tag_t bst = opb_get_bus_space_tag();
382 	bus_space_handle_t bsh;
383 	uint8_t mdcntl, sts;
384 	int cnt, i = 0;
385 
386 #define I2C_EEPROM_ADDR	0x52
387 
388 	if (bus_space_map(bst, AMCC405EX_IIC0_BASE, IIC_NREG, 0, &bsh))
389 		return ENOMEM; /* ??? */
390 
391 	/* Clear Stop Complete Bit */
392 	bus_space_write_1(bst, bsh, IIC_STS, IIC_STS_SCMP);
393 	/* Check init */
394 	do {
395 		/* Get status */
396 		sts = bus_space_read_1(bst, bsh, IIC_STS);
397 	} while ((sts & IIC_STS_PT));
398 
399 	mdcntl = bus_space_read_1(bst, bsh, IIC_MDCNTL);
400 	bus_space_write_1(bst, bsh, IIC_MDCNTL,
401 	    mdcntl | IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
402 
403 	/* 7-bit adressing */
404 	bus_space_write_1(bst, bsh, IIC_HMADR, 0);
405 	bus_space_write_1(bst, bsh, IIC_LMADR, I2C_EEPROM_ADDR << 1);
406 
407 	bus_space_write_1(bst, bsh, IIC_MDBUF, 0);
408 	bus_space_write_1(bst, bsh, IIC_CNTL, IIC_CNTL_PT);
409 	do {
410 		/* Get status */
411 		sts = bus_space_read_1(bst, bsh, IIC_STS);
412 	} while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
413 
414 	cnt = 0;
415 	while (cnt < len) {
416 		/* always read 4byte */
417 		bus_space_write_1(bst, bsh, IIC_CNTL,
418 		    IIC_CNTL_PT | IIC_CNTL_RW | IIC_CNTL_TCT);
419 		do {
420 			/* Get status */
421 			sts = bus_space_read_1(bst, bsh, IIC_STS);
422 		} while ((sts & IIC_STS_PT) && !(sts & IIC_STS_ERR));
423 
424 		if ((sts & IIC_STS_PT) || (sts & IIC_STS_ERR))
425 			break;
426 		if (sts & IIC_STS_MDBS) {
427 			delay(1);
428 			/* read 4byte */
429 			for (i = 0; i < 4 && cnt < len; i++, cnt++)
430 				buf[cnt] =
431 				    bus_space_read_1(bst, bsh, IIC_MDBUF);
432 		}
433 	}
434 	for ( ; i < 4; i++)
435 		(void) bus_space_read_1(bst, bsh, IIC_MDBUF);
436 
437 	bus_space_unmap(bst, bsh, IIC_NREG);
438 
439 	return (cnt == len) ? 0 : EINVAL;
440 }
441