machdep.c revision 1.55.40.1 1 /* $NetBSD: machdep.c,v 1.55.40.1 2019/06/10 22:06:14 christos 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.55.40.1 2019/06/10 22:06:14 christos Exp $");
71
72 #include "opt_compat_netbsd.h"
73 #include "opt_ddb.h"
74
75 #include <sys/param.h>
76 #include <sys/boot_flag.h>
77 #include <sys/buf.h>
78 #include <sys/bus.h>
79 #include <sys/cpu.h>
80 #include <sys/device.h>
81 #include <sys/exec.h>
82 #include <sys/kernel.h>
83 #include <sys/ksyms.h>
84 #include <sys/malloc.h>
85 #include <sys/mbuf.h>
86 #include <sys/module.h>
87 #include <sys/mount.h>
88 #include <sys/msgbuf.h>
89 #include <sys/proc.h>
90 #include <sys/reboot.h>
91 #include <sys/syscallargs.h>
92 #include <sys/syslog.h>
93 #include <sys/systm.h>
94
95 #include <uvm/uvm_extern.h>
96
97 #include <prop/proplib.h>
98
99 #include <machine/powerpc.h>
100 #include <machine/walnut.h>
101
102 #include <powerpc/trap.h>
103 #include <powerpc/pcb.h>
104
105 #include <powerpc/spr.h>
106 #include <powerpc/ibm4xx/spr.h>
107 #include <powerpc/ibm4xx/cpu.h>
108 #include <powerpc/ibm4xx/dcr4xx.h>
109
110 #include <powerpc/ibm4xx/pci_machdep.h>
111
112 #include <powerpc/pic/picvar.h>
113
114 #include <dev/cons.h>
115 #include <dev/pci/pcivar.h>
116 #include <dev/pci/pciconf.h>
117
118 #include "ksyms.h"
119
120 #if defined(DDB)
121 #include <powerpc/db_machdep.h>
122 #include <ddb/db_extern.h>
123 #endif
124
125
126 #define TLB_PG_SIZE (16*1024*1024)
127
128 /*
129 * Global variables used here and there
130 */
131 struct vm_map *phys_map = NULL;
132
133 /*
134 * This should probably be in autoconf! XXX
135 */
136 char machine[] = MACHINE; /* from <machine/param.h> */
137 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
138
139 char bootpath[256];
140
141 void initppc(vaddr_t, vaddr_t, char *, void *);
142
143 static void dumpsys(void);
144
145 #define MEMREGIONS 8
146 struct mem_region physmemr[MEMREGIONS]; /* Hard code memory */
147 struct mem_region availmemr[MEMREGIONS]; /* Who's supposed to set these up? */
148
149 struct board_cfg_data board_data;
150
151 void
152 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block)
153 {
154 /* Disable all external interrupts */
155 mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
156
157 /* Initialize cache info for memcpy, etc. */
158 cpu_probe_cache();
159
160 /* Save info block */
161 memcpy(&board_data, info_block, sizeof(board_data));
162
163 memset(physmemr, 0, sizeof physmemr);
164 memset(availmemr, 0, sizeof availmemr);
165 physmemr[0].start = 0;
166 physmemr[0].size = board_data.mem_size & ~PGOFSET;
167 /* Lower memory reserved by eval board BIOS */
168 availmemr[0].start = startkernel;
169 availmemr[0].size = board_data.mem_size - availmemr[0].start;
170
171 /* Linear map kernel memory */
172 for (vaddr_t va = 0; va < endkernel; va += TLB_PG_SIZE) {
173 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
174 }
175
176 /* Map console after physmem (see pmap_tlbmiss()) */
177 ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
178 TLB_PG_SIZE, TLB_I | TLB_G);
179
180 mtspr(SPR_TCR, 0); /* disable all timers */
181
182 ibm4xx_init(startkernel, endkernel, pic_ext_intr);
183
184 #ifdef DEBUG
185 printf("Board config data:\n");
186 printf(" usr_config_ver = %s\n", board_data.usr_config_ver);
187 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver);
188 printf(" mem_size = %u\n", board_data.mem_size);
189 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
190 board_data.mac_address_local[0], board_data.mac_address_local[1],
191 board_data.mac_address_local[2], board_data.mac_address_local[3],
192 board_data.mac_address_local[4], board_data.mac_address_local[5]);
193 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
194 board_data.mac_address_pci[0], board_data.mac_address_pci[1],
195 board_data.mac_address_pci[2], board_data.mac_address_pci[3],
196 board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
197 printf(" processor_speed = %u\n", board_data.processor_speed);
198 printf(" plb_speed = %u\n", board_data.plb_speed);
199 printf(" pci_speed = %u\n", board_data.pci_speed);
200 #endif
201
202 #ifdef DDB
203 if (boothowto & RB_KDB)
204 Debugger();
205 #endif
206
207 /*
208 * Look for the ibm4xx modules in the right place.
209 */
210 module_machine = module_machine_ibm4xx;
211 }
212
213 /*
214 * Machine dependent startup code.
215 */
216
217 void
218 cpu_startup(void)
219 {
220 vaddr_t minaddr, maxaddr;
221 prop_number_t pn;
222 prop_data_t pd;
223 char pbuf[9];
224
225 /*
226 * Initialize error message buffer (at end of core).
227 */
228 #if 0 /* For some reason this fails... --Artem
229 * Besides, do we really have to put it at the end of core?
230 * Let's use static buffer for now
231 */
232 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
233 UVM_KMF_VAONLY)))
234 panic("startup: no room for message buffer");
235 for (i = 0; i < btoc(MSGBUFSIZE); i++)
236 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
237 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
238 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
239 #else
240 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
241 #endif
242
243 printf("%s%s", copyright, version);
244 printf("Walnut PowerPC 405GP Evaluation Board\n");
245
246 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
247 printf("total memory = %s\n", pbuf);
248
249 minaddr = 0;
250 /*
251 * Allocate a submap for physio
252 */
253 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
254 VM_PHYS_SIZE, 0, false, NULL);
255
256 /*
257 * No need to allocate an mbuf cluster submap. Mbuf clusters
258 * are allocated via the pool allocator, and we use direct-mapped
259 * pool pages.
260 */
261
262 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
263 printf("avail memory = %s\n", pbuf);
264
265 /*
266 * Set up the board properties dictionary.
267 */
268 board_properties = prop_dictionary_create();
269 KASSERT(board_properties != NULL);
270
271 pn = prop_number_create_integer(board_data.mem_size);
272 KASSERT(pn != NULL);
273 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
274 panic("setting mem-size");
275 prop_object_release(pn);
276
277 pd = prop_data_create_data_nocopy(board_data.mac_address_local,
278 sizeof(board_data.mac_address_local));
279 KASSERT(pd != NULL);
280 if (prop_dictionary_set(board_properties, "emac0-mac-addr",
281 pd) == false)
282 panic("setting emac0-mac-addr");
283 prop_object_release(pd);
284
285 pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
286 sizeof(board_data.mac_address_pci));
287 KASSERT(pd != NULL);
288 if (prop_dictionary_set(board_properties, "sip0-mac-addr",
289 pd) == false)
290 panic("setting sip0-mac-addr");
291 prop_object_release(pd);
292
293 pn = prop_number_create_integer(board_data.processor_speed);
294 KASSERT(pn != NULL);
295 if (prop_dictionary_set(board_properties, "processor-frequency",
296 pn) == false)
297 panic("setting processor-frequency");
298 prop_object_release(pn);
299
300 /*
301 * Now that we have VM, malloc()s are OK in bus_space.
302 */
303 bus_space_mallocok();
304 fake_mapiodev = 0;
305 }
306
307
308 static void
309 dumpsys(void)
310 {
311
312 printf("dumpsys: TBD\n");
313 }
314
315 /*
316 * Halt or reboot the machine after syncing/dumping according to howto.
317 */
318 void
319 cpu_reboot(int howto, char *what)
320 {
321 static int syncing;
322 static char str[256];
323 char *ap = str, *ap1 = ap;
324
325 boothowto = howto;
326 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
327 syncing = 1;
328 vfs_shutdown(); /* sync */
329 resettodr(); /* set wall clock */
330 }
331
332 splhigh();
333
334 if (!cold && (howto & RB_DUMP))
335 dumpsys();
336
337 doshutdownhooks();
338
339 pmf_system_shutdown(boothowto);
340
341 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
342 /* Power off here if we know how...*/
343 }
344
345 if (howto & RB_HALT) {
346 printf("halted\n\n");
347
348 goto reboot; /* XXX for now... */
349
350 #ifdef DDB
351 printf("dropping to debugger\n");
352 while(1)
353 Debugger();
354 #endif
355 }
356
357 printf("rebooting\n\n");
358 if (what && *what) {
359 if (strlen(what) > sizeof str - 5)
360 printf("boot string too large, ignored\n");
361 else {
362 strcpy(str, what);
363 ap1 = ap = str + strlen(str);
364 *ap++ = ' ';
365 }
366 }
367 *ap++ = '-';
368 if (howto & RB_SINGLE)
369 *ap++ = 's';
370 if (howto & RB_KDB)
371 *ap++ = 'd';
372 *ap++ = 0;
373 if (ap[-2] == '-')
374 *ap1 = 0;
375
376 /* flush cache for msgbuf */
377 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
378
379 reboot:
380 ppc4xx_reset();
381
382 printf("ppc4xx_reset() failed!\n");
383 #ifdef DDB
384 while(1)
385 Debugger();
386 #else
387 while (1)
388 /* nothing */;
389 #endif
390 }
391
392 void
393 mem_regions(struct mem_region **mem, struct mem_region **avail)
394 {
395
396 *mem = physmemr;
397 *avail = availmemr;
398 }
399
400
401 int
402 ibm4xx_pci_bus_maxdevs(void *v, int busno)
403 {
404
405 /*
406 * Bus number is irrelevant. Configuration Mechanism 1 is in
407 * use, can have devices 0-32 (i.e. the `normal' range).
408 */
409 return 5;
410 }
411
412 int
413 ibm4xx_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
414 {
415 int pin = pa->pa_intrpin;
416 int dev = pa->pa_device;
417
418 if (pin == 0)
419 /* No IRQ used. */
420 goto bad;
421
422 if (pin > 4) {
423 printf("%s: bad interrupt pin %d\n", __func__, pin);
424 goto bad;
425 }
426
427 /*
428 * We need to map the interrupt pin to the interrupt bit in the UIC
429 * associated with it. This is highly machine-dependent.
430 */
431 switch(dev) {
432 case 1:
433 case 2:
434 case 3:
435 case 4:
436 *ihp = 27 + dev;
437 break;
438 default:
439 printf("Hmm.. PCI device %d should not exist on this board\n",
440 dev);
441 goto bad;
442 }
443 return 0;
444
445 bad:
446 *ihp = -1;
447 return 1;
448 }
449
450 void
451 ibm4xx_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
452 int *iline)
453 {
454
455 if (bus == 0) {
456 switch(dev) {
457 case 1:
458 case 2:
459 case 3:
460 case 4:
461 *iline = 31 - dev;
462 }
463 } else
464 *iline = 20 + ((swiz + dev + 1) & 3);
465 }
466