machdep.c revision 1.54 1 /* $NetBSD: machdep.c,v 1.54 2011/06/22 18:06:32 matt Exp $ */
2
3 /*
4 * Copyright 2001, 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 * Copyright (C) 1995, 1996 TooLs GmbH.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by TooLs GmbH.
54 * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.54 2011/06/22 18:06:32 matt Exp $");
71
72 #include "opt_compat_netbsd.h"
73 #include "opt_ddb.h"
74 #include "opt_ipkdb.h"
75
76 #include <sys/param.h>
77 #include <sys/boot_flag.h>
78 #include <sys/buf.h>
79 #include <sys/bus.h>
80 #include <sys/cpu.h>
81 #include <sys/device.h>
82 #include <sys/exec.h>
83 #include <sys/kernel.h>
84 #include <sys/ksyms.h>
85 #include <sys/malloc.h>
86 #include <sys/mbuf.h>
87 #include <sys/module.h>
88 #include <sys/mount.h>
89 #include <sys/msgbuf.h>
90 #include <sys/proc.h>
91 #include <sys/reboot.h>
92 #include <sys/syscallargs.h>
93 #include <sys/syslog.h>
94 #include <sys/systm.h>
95
96 #include <uvm/uvm_extern.h>
97
98 #include <prop/proplib.h>
99
100 #include <machine/powerpc.h>
101 #include <machine/walnut.h>
102
103 #include <powerpc/trap.h>
104 #include <powerpc/pcb.h>
105
106 #include <powerpc/spr.h>
107 #include <powerpc/ibm4xx/spr.h>
108 #include <powerpc/ibm4xx/cpu.h>
109 #include <powerpc/ibm4xx/dcr4xx.h>
110
111 #include <powerpc/ibm4xx/pci_machdep.h>
112
113 #include <powerpc/pic/picvar.h>
114
115 #include <dev/cons.h>
116 #include <dev/pci/pcivar.h>
117 #include <dev/pci/pciconf.h>
118
119 #include "ksyms.h"
120
121 #if defined(DDB)
122 #include <powerpc/db_machdep.h>
123 #include <ddb/db_extern.h>
124 #endif
125
126
127 #define TLB_PG_SIZE (16*1024*1024)
128
129 /*
130 * Global variables used here and there
131 */
132 struct vm_map *phys_map = NULL;
133
134 /*
135 * This should probably be in autoconf! XXX
136 */
137 char cpu_model[80];
138 char machine[] = MACHINE; /* from <machine/param.h> */
139 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
140
141 char bootpath[256];
142 paddr_t msgbuf_paddr;
143 vaddr_t msgbuf_vaddr;
144
145 void initppc(vaddr_t, vaddr_t, char *, void *);
146
147 static void dumpsys(void);
148
149 #define MEMREGIONS 8
150 struct mem_region physmemr[MEMREGIONS]; /* Hard code memory */
151 struct mem_region availmemr[MEMREGIONS]; /* Who's supposed to set these up? */
152
153 struct board_cfg_data board_data;
154
155 void
156 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block)
157 {
158 /* Disable all external interrupts */
159 mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
160
161 /* Initialize cache info for memcpy, etc. */
162 cpu_probe_cache();
163
164 /* Save info block */
165 memcpy(&board_data, info_block, sizeof(board_data));
166
167 memset(physmemr, 0, sizeof physmemr);
168 memset(availmemr, 0, sizeof availmemr);
169 physmemr[0].start = 0;
170 physmemr[0].size = board_data.mem_size & ~PGOFSET;
171 /* Lower memory reserved by eval board BIOS */
172 availmemr[0].start = startkernel;
173 availmemr[0].size = board_data.mem_size - availmemr[0].start;
174
175 /* Linear map kernel memory */
176 for (vaddr_t va = 0; va < endkernel; va += TLB_PG_SIZE) {
177 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
178 }
179
180 /* Map console after physmem (see pmap_tlbmiss()) */
181 ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
182 TLB_PG_SIZE, TLB_I | TLB_G);
183
184 mtspr(SPR_TCR, 0); /* disable all timers */
185
186 ibm4xx_init(startkernel, endkernel, pic_ext_intr);
187
188 #ifdef DEBUG
189 printf("Board config data:\n");
190 printf(" usr_config_ver = %s\n", board_data.usr_config_ver);
191 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver);
192 printf(" mem_size = %u\n", board_data.mem_size);
193 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
194 board_data.mac_address_local[0], board_data.mac_address_local[1],
195 board_data.mac_address_local[2], board_data.mac_address_local[3],
196 board_data.mac_address_local[4], board_data.mac_address_local[5]);
197 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
198 board_data.mac_address_pci[0], board_data.mac_address_pci[1],
199 board_data.mac_address_pci[2], board_data.mac_address_pci[3],
200 board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
201 printf(" processor_speed = %u\n", board_data.processor_speed);
202 printf(" plb_speed = %u\n", board_data.plb_speed);
203 printf(" pci_speed = %u\n", board_data.pci_speed);
204 #endif
205
206 #ifdef DDB
207 if (boothowto & RB_KDB)
208 Debugger();
209 #endif
210 #ifdef IPKDB
211 /*
212 * Now trap to IPKDB
213 */
214 ipkdb_init();
215 if (boothowto & RB_KDB)
216 ipkdb_connect(0);
217 #endif
218
219 /*
220 * Look for the ibm4xx modules in the right place.
221 */
222 module_machine = module_machine_ibm4xx;
223 }
224
225 /*
226 * Machine dependent startup code.
227 */
228
229 char msgbuf[MSGBUFSIZE];
230
231 void
232 cpu_startup(void)
233 {
234 vaddr_t minaddr, maxaddr;
235 prop_number_t pn;
236 prop_data_t pd;
237 char pbuf[9];
238
239 /*
240 * Initialize error message buffer (at end of core).
241 */
242 #if 0 /* For some reason this fails... --Artem
243 * Besides, do we really have to put it at the end of core?
244 * Let's use static buffer for now
245 */
246 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
247 UVM_KMF_VAONLY)))
248 panic("startup: no room for message buffer");
249 for (i = 0; i < btoc(MSGBUFSIZE); i++)
250 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
251 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
252 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
253 #else
254 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
255 #endif
256
257 printf("%s%s", copyright, version);
258 printf("Walnut PowerPC 405GP Evaluation Board\n");
259
260 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
261 printf("total memory = %s\n", pbuf);
262
263 minaddr = 0;
264 /*
265 * Allocate a submap for physio
266 */
267 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
268 VM_PHYS_SIZE, 0, false, NULL);
269
270 /*
271 * No need to allocate an mbuf cluster submap. Mbuf clusters
272 * are allocated via the pool allocator, and we use direct-mapped
273 * pool pages.
274 */
275
276 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
277 printf("avail memory = %s\n", pbuf);
278
279 /*
280 * Set up the board properties dictionary.
281 */
282 board_properties = prop_dictionary_create();
283 KASSERT(board_properties != NULL);
284
285 pn = prop_number_create_integer(board_data.mem_size);
286 KASSERT(pn != NULL);
287 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
288 panic("setting mem-size");
289 prop_object_release(pn);
290
291 pd = prop_data_create_data_nocopy(board_data.mac_address_local,
292 sizeof(board_data.mac_address_local));
293 KASSERT(pd != NULL);
294 if (prop_dictionary_set(board_properties, "emac0-mac-addr",
295 pd) == false)
296 panic("setting emac0-mac-addr");
297 prop_object_release(pd);
298
299 pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
300 sizeof(board_data.mac_address_pci));
301 KASSERT(pd != NULL);
302 if (prop_dictionary_set(board_properties, "sip0-mac-addr",
303 pd) == false)
304 panic("setting sip0-mac-addr");
305 prop_object_release(pd);
306
307 pn = prop_number_create_integer(board_data.processor_speed);
308 KASSERT(pn != NULL);
309 if (prop_dictionary_set(board_properties, "processor-frequency",
310 pn) == false)
311 panic("setting processor-frequency");
312 prop_object_release(pn);
313
314 /*
315 * Now that we have VM, malloc()s are OK in bus_space.
316 */
317 bus_space_mallocok();
318 fake_mapiodev = 0;
319 }
320
321
322 static void
323 dumpsys(void)
324 {
325
326 printf("dumpsys: TBD\n");
327 }
328
329 /*
330 * Halt or reboot the machine after syncing/dumping according to howto.
331 */
332 void
333 cpu_reboot(int howto, char *what)
334 {
335 static int syncing;
336 static char str[256];
337 char *ap = str, *ap1 = ap;
338
339 boothowto = howto;
340 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
341 syncing = 1;
342 vfs_shutdown(); /* sync */
343 resettodr(); /* set wall clock */
344 }
345
346 splhigh();
347
348 if (!cold && (howto & RB_DUMP))
349 dumpsys();
350
351 doshutdownhooks();
352
353 pmf_system_shutdown(boothowto);
354
355 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
356 /* Power off here if we know how...*/
357 }
358
359 if (howto & RB_HALT) {
360 printf("halted\n\n");
361
362 goto reboot; /* XXX for now... */
363
364 #ifdef DDB
365 printf("dropping to debugger\n");
366 while(1)
367 Debugger();
368 #endif
369 }
370
371 printf("rebooting\n\n");
372 if (what && *what) {
373 if (strlen(what) > sizeof str - 5)
374 printf("boot string too large, ignored\n");
375 else {
376 strcpy(str, what);
377 ap1 = ap = str + strlen(str);
378 *ap++ = ' ';
379 }
380 }
381 *ap++ = '-';
382 if (howto & RB_SINGLE)
383 *ap++ = 's';
384 if (howto & RB_KDB)
385 *ap++ = 'd';
386 *ap++ = 0;
387 if (ap[-2] == '-')
388 *ap1 = 0;
389
390 /* flush cache for msgbuf */
391 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
392
393 reboot:
394 ppc4xx_reset();
395
396 printf("ppc4xx_reset() failed!\n");
397 #ifdef DDB
398 while(1)
399 Debugger();
400 #else
401 while (1)
402 /* nothing */;
403 #endif
404 }
405
406 void
407 mem_regions(struct mem_region **mem, struct mem_region **avail)
408 {
409
410 *mem = physmemr;
411 *avail = availmemr;
412 }
413
414
415 int
416 ibm4xx_pci_bus_maxdevs(void *v, int busno)
417 {
418
419 /*
420 * Bus number is irrelevant. Configuration Mechanism 1 is in
421 * use, can have devices 0-32 (i.e. the `normal' range).
422 */
423 return 5;
424 }
425
426 int
427 ibm4xx_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
428 {
429 int pin = pa->pa_intrpin;
430 int dev = pa->pa_device;
431
432 if (pin == 0)
433 /* No IRQ used. */
434 goto bad;
435
436 if (pin > 4) {
437 printf("%s: bad interrupt pin %d\n", __func__, pin);
438 goto bad;
439 }
440
441 /*
442 * We need to map the interrupt pin to the interrupt bit in the UIC
443 * associated with it. This is highly machine-dependent.
444 */
445 switch(dev) {
446 case 1:
447 case 2:
448 case 3:
449 case 4:
450 *ihp = 27 + dev;
451 break;
452 default:
453 printf("Hmm.. PCI device %d should not exist on this board\n",
454 dev);
455 goto bad;
456 }
457 return 0;
458
459 bad:
460 *ihp = -1;
461 return 1;
462 }
463
464 void
465 ibm4xx_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
466 int *iline)
467 {
468
469 if (bus == 0) {
470 switch(dev) {
471 case 1:
472 case 2:
473 case 3:
474 case 4:
475 *iline = 31 - dev;
476 }
477 } else
478 *iline = 20 + ((swiz + dev + 1) & 3);
479 }
480