1 /*
2  *   OpenBIOS pci driver
3  *
4  *   This driver is compliant to the
5  *   PCI bus binding to IEEE 1275-1994 Rev 2.1
6  *
7  *   (C) 2004 Stefan Reinauer
8  *   (C) 2005 Ed Schouten <ed@fxq.nl>
9  *
10  *   Some parts from OpenHackWare-0.4, Copyright (c) 2004-2005 Jocelyn Mayer
11  *
12  *   This program is free software; you can redistribute it and/or
13  *   modify it under the terms of the GNU General Public License
14  *   version 2
15  *
16  */
17 
18 #include "config.h"
19 #include "libopenbios/bindings.h"
20 #include "libopenbios/ofmem.h"
21 #include "kernel/kernel.h"
22 #include "drivers/pci.h"
23 #include "libc/byteorder.h"
24 #include "libc/vsprintf.h"
25 
26 #include "drivers/drivers.h"
27 #include "drivers/vga.h"
28 #include "packages/video.h"
29 #include "libopenbios/video.h"
30 #include "timer.h"
31 #include "pci.h"
32 #include "pci_database.h"
33 #ifdef CONFIG_DRIVER_MACIO
34 #include "macio.h"
35 #endif
36 #ifdef CONFIG_DRIVER_USB
37 #include "drivers/usb.h"
38 #endif
39 #ifdef CONFIG_DRIVER_VIRTIO_BLK
40 #include "virtio.h"
41 #endif
42 
43 #if defined (CONFIG_DEBUG_PCI)
44 # define PCI_DPRINTF(format, ...) printk(format, ## __VA_ARGS__)
45 #else
46 # define PCI_DPRINTF(format, ...) do { } while (0)
47 #endif
48 
49 #define set_bool_property(ph, name) set_property(ph, name, NULL, 0);
50 
51 /* DECLARE data structures for the nodes.  */
52 
53 DECLARE_UNNAMED_NODE( ob_pci_bus_node, INSTALL_OPEN, 2*sizeof(int) );
54 DECLARE_UNNAMED_NODE( ob_pci_bridge_node, INSTALL_OPEN, 2*sizeof(int) );
55 DECLARE_UNNAMED_NODE( ob_pci_simple_node, 0, 2*sizeof(int) );
56 
57 const pci_arch_t *arch;
58 
59 #define IS_NOT_RELOCATABLE	0x80000000
60 #define IS_PREFETCHABLE		0x40000000
61 #define IS_ALIASED		0x20000000
62 
encode_int32_cells(int num_cells,u32 * prop,ucell val)63 static int encode_int32_cells(int num_cells, u32 *prop, ucell val)
64 {
65     int i = 0;
66 
67     /* hi ... lo */
68     for (i=0; i < num_cells; ++i) {
69         prop[num_cells - i - 1] = val;
70         val >>= 16;
71         val >>= 16;
72     }
73 
74     return num_cells;
75 }
76 
pci_encode_phys_addr(u32 * phys,int flags,int space_code,pci_addr dev,uint8_t reg,uint64_t addr)77 static inline int pci_encode_phys_addr(u32 *phys, int flags, int space_code,
78 				 pci_addr dev, uint8_t reg, uint64_t addr)
79 {
80 
81 	/* phys.hi */
82 
83 	phys[0] = flags | (space_code << 24) | dev | reg;
84 
85 	/* phys.mid */
86 
87 	phys[1] = addr >> 32;
88 
89 	/* phys.lo */
90 
91 	phys[2] = addr;
92 
93 	return 3;
94 }
95 
pci_encode_size(u32 * prop,uint64_t size)96 static inline int pci_encode_size(u32 *prop, uint64_t size)
97 {
98     return encode_int32_cells(2, prop, size);
99 }
100 
host_address_cells(void)101 static int host_address_cells(void)
102 {
103     return get_int_property(find_dev("/"), "#address-cells", NULL);
104 }
105 
host_encode_phys_addr(u32 * prop,ucell addr)106 static int host_encode_phys_addr(u32 *prop, ucell addr)
107 {
108     return encode_int32_cells(host_address_cells(), prop, addr);
109 }
110 
host_size_cells(void)111 static int host_size_cells(void)
112 {
113     return get_int_property(find_dev("/"), "#size-cells", NULL);
114 }
115 
116 /*
117 static int parent_address_cells(void)
118 {
119     phandle_t parent_ph = ih_to_phandle(my_parent());
120     return get_int_property(parent_ph, "#address-cells", NULL);
121 }
122 
123 static int parent_size_cells(void)
124 {
125     phandle_t parent_ph = ih_to_phandle(my_parent());
126     return get_int_property(parent_ph, "#size-cells", NULL);
127 }
128 */
129 
130 #if defined(CONFIG_DEBUG_PCI)
dump_reg_property(const char * description,int nreg,u32 * reg)131 static void dump_reg_property(const char* description, int nreg, u32 *reg)
132 {
133     int i;
134     printk("%s reg", description);
135     for (i=0; i < nreg; ++i) {
136         printk(" %08X", reg[i]);
137     }
138     printk("\n");
139 }
140 #endif
141 
pci_bus_addr_to_host_addr(int space,uint32_t ba)142 static unsigned long pci_bus_addr_to_host_addr(int space, uint32_t ba)
143 {
144     if (space == IO_SPACE) {
145         return arch->io_base + (unsigned long)ba;
146     } else if (space == MEMORY_SPACE_32) {
147         return arch->host_pci_base + (unsigned long)ba;
148     } else {
149         /* Return unaltered to aid debugging property values */
150         return (unsigned long)ba;
151     }
152 }
153 
pci_decode_pci_addr(pci_addr addr,int * flags,int * space_code,uint32_t * mask)154 static inline void pci_decode_pci_addr(pci_addr addr, int *flags,
155                                        int *space_code, uint32_t *mask)
156 {
157     *flags = 0;
158 
159     if (addr & 0x01) {
160         *space_code = IO_SPACE;
161         *mask = 0x00000001;
162     } else {
163         if (addr & 0x04) {
164             *space_code = MEMORY_SPACE_64;
165             *flags |= IS_NOT_RELOCATABLE; /* XXX: why not relocatable? */
166         } else {
167             *space_code = MEMORY_SPACE_32;
168         }
169 
170         if (addr & 0x08) {
171             *flags |= IS_PREFETCHABLE;
172         }
173 
174         *mask = 0x0000000F;
175     }
176 }
177 
178 static void
ob_pci_open(int * idx)179 ob_pci_open(int *idx)
180 {
181 	int ret=1;
182 	RET ( -ret );
183 }
184 
185 static void
ob_pci_close(int * idx)186 ob_pci_close(int *idx)
187 {
188 }
189 
190 /* ( str len -- phys.lo phys.mid phys.hi ) */
191 
192 static void
ob_pci_decode_unit(int * idx)193 ob_pci_decode_unit(int *idx)
194 {
195 	ucell hi, mid, lo;
196 	const char *arg = pop_fstr_copy();
197 	int dev, fn, reg, ss, n, p, t;
198 	int bus, len;
199 	char *ptr;
200 
201 	PCI_DPRINTF("ob_pci_decode_unit idx=%p\n", idx);
202 
203 	fn = 0;
204 	reg = 0;
205 	n = 0;
206 	p = 0;
207 	t = 0;
208 
209 	ptr = (char*)arg;
210 	if (*ptr == 'n') {
211 		n = IS_NOT_RELOCATABLE;
212 		ptr++;
213 	}
214 	if (*ptr == 'i') {
215 		ss = IO_SPACE;
216 		ptr++;
217 		if (*ptr == 't') {
218 			t = IS_ALIASED;
219 			ptr++;
220 		}
221 
222 		/* DD,F,RR,NNNNNNNN */
223 
224 		dev = strtol(ptr, &ptr, 16);
225 		ptr++;
226 		fn = strtol(ptr, &ptr, 16);
227 		ptr++;
228 		reg = strtol(ptr, &ptr, 16);
229 		ptr++;
230 		lo = strtol(ptr, &ptr, 16);
231 		mid = 0;
232 
233 	} else if (*ptr == 'm') {
234 		ss = MEMORY_SPACE_32;
235 		ptr++;
236 		if (*ptr == 't') {
237 			t = IS_ALIASED;
238 			ptr++;
239 		}
240 		if (*ptr == 'p') {
241 			p = IS_PREFETCHABLE;
242 			ptr++;
243 		}
244 
245 		/* DD,F,RR,NNNNNNNN */
246 
247 		dev = strtol(ptr, &ptr, 16);
248 		ptr++;
249 		fn = strtol(ptr, &ptr, 16);
250 		ptr++;
251 		reg = strtol(ptr, &ptr, 16);
252 		ptr++;
253 		lo = strtol(ptr, &ptr, 16);
254 		mid = 0;
255 
256 	} else if (*ptr == 'x') {
257 		unsigned long long addr64;
258 		ss = MEMORY_SPACE_64;
259 		ptr++;
260 		if (*ptr == 'p') {
261 			p = IS_PREFETCHABLE;
262 			ptr++;
263 		}
264 
265 		/* DD,F,RR,NNNNNNNNNNNNNNNN */
266 
267 		dev = strtol(ptr, &ptr, 16);
268 		ptr++;
269 		fn = strtol(ptr, &ptr, 16);
270 		ptr++;
271 		reg = strtol(ptr, &ptr, 16);
272 		ptr++;
273 		addr64 = strtoll(ptr, &ptr, 16);
274 		lo = (ucell)addr64;
275 		mid = addr64 >> 32;
276 
277 	} else {
278 		ss = CONFIGURATION_SPACE;
279 		/* "DD" or "DD,FF" */
280 		dev = strtol(ptr, &ptr, 16);
281 		if (*ptr == ',') {
282 			ptr++;
283 			fn = strtol(ptr, NULL, 16);
284 		}
285 		lo = 0;
286 		mid = 0;
287 	}
288 	free((char*)arg);
289 
290 	bus = get_int_property(get_cur_dev(), "bus-range", &len);
291 
292 	hi = n | p | t | (ss << 24) | (bus << 16) | (dev << 11) | (fn << 8) | reg;
293 
294 	PUSH(lo);
295 	PUSH(mid);
296 	PUSH(hi);
297 
298 	PCI_DPRINTF("ob_pci_decode_unit idx=%p addr="
299 	        FMT_ucellx " " FMT_ucellx " " FMT_ucellx "\n",
300 	        idx, lo, mid, hi);
301 }
302 
303 /*  ( phys.lo phy.mid phys.hi -- str len ) */
304 
305 static void
ob_pci_encode_unit(int * idx)306 ob_pci_encode_unit(int *idx)
307 {
308 	char buf[28];
309 	cell hi = POP();
310 	cell mid = POP();
311 	cell lo = POP();
312         int n, p, t, ss, dev, fn, reg;
313 
314 	n = hi & IS_NOT_RELOCATABLE;
315 	p = hi & IS_PREFETCHABLE;
316 	t = hi & IS_ALIASED;
317 	ss = (hi >> 24) & 0x03;
318 
319 	dev = (hi >> 11) & 0x1F;
320 	fn = (hi >> 8) & 0x07;
321 	reg = hi & 0xFF;
322 
323 	switch(ss) {
324 	case CONFIGURATION_SPACE:
325 
326 		if (fn == 0)	/* DD */
327         		snprintf(buf, sizeof(buf), "%x", dev);
328 		else		/* DD,F */
329         		snprintf(buf, sizeof(buf), "%x,%x", dev, fn);
330 		break;
331 
332 	case IO_SPACE:
333 
334 		/* [n]i[t]DD,F,RR,NNNNNNNN */
335                 snprintf(buf, sizeof(buf), "%si%s%x,%x,%x," FMT_ucellx,
336 			 n ? "n" : "",	/* relocatable */
337 			 t ? "t" : "",	/* aliased */
338 			 dev, fn, reg, t ? lo & 0x03FF : lo);
339 		break;
340 
341 	case MEMORY_SPACE_32:
342 
343 		/* [n]m[t][p]DD,F,RR,NNNNNNNN */
344                 snprintf(buf, sizeof(buf), "%sm%s%s%x,%x,%x," FMT_ucellx,
345 			 n ? "n" : "",	/* relocatable */
346 			 t ? "t" : "",	/* aliased */
347 			 p ? "p" : "",	/* prefetchable */
348 			 dev, fn, reg, lo );
349 		break;
350 
351 	case MEMORY_SPACE_64:
352 
353 		/* [n]x[p]DD,F,RR,NNNNNNNNNNNNNNNN */
354         	snprintf(buf, sizeof(buf), "%sx%s%x,%x,%x,%llx",
355 			 n ? "n" : "",	/* relocatable */
356 			 p ? "p" : "",	/* prefetchable */
357                          dev, fn, reg, ((long long)mid << 32) | (long long)lo);
358 		break;
359 	}
360 	push_str(buf);
361 
362 	PCI_DPRINTF("ob_pci_encode_unit space=%d dev=%d fn=%d buf=%s\n",
363 	        ss, dev, fn, buf);
364 }
365 
366 /* Map PCI MMIO or IO space from the BAR address. Note it is up to the caller
367    to understand whether the resulting address is in MEM or IO space and
368    use the appropriate accesses */
ob_pci_map(uint32_t ba,ucell size)369 static ucell ob_pci_map(uint32_t ba, ucell size) {
370 	phys_addr_t phys;
371 	uint32_t mask;
372 	int flags, space_code;
373 	ucell virt;
374 
375 	pci_decode_pci_addr(ba, &flags, &space_code, &mask);
376 
377 	phys = pci_bus_addr_to_host_addr(space_code,
378 			ba & ~mask);
379 
380 #if defined(CONFIG_OFMEM)
381 	ofmem_claim_phys(phys, size, 0);
382 
383 #if defined(CONFIG_PPC)
384 	/* For some reason PPC gets upset when virt != phys for map-in... */
385 	virt = ofmem_claim_virt(phys, size, 0);
386 #else
387 	virt = ofmem_claim_virt(-1, size, size);
388 #endif
389 
390 	ofmem_map(phys, virt, size, ofmem_arch_io_translation_mode(phys));
391 
392 #else
393 	virt = size;	/* Keep compiler quiet */
394 	virt = phys;
395 #endif
396 
397 	return virt;
398 }
399 
ob_pci_unmap(ucell virt,ucell size)400 static void ob_pci_unmap(ucell virt, ucell size) {
401 #if defined(CONFIG_OFMEM)
402 	ofmem_unmap(virt, size);
403 #endif
404 }
405 
406 /* ( pci-addr.lo pci-addr.mid pci-addr.hi size -- virt ) */
407 
408 static void
ob_pci_bus_map_in(int * idx)409 ob_pci_bus_map_in(int *idx)
410 {
411 	uint32_t ba;
412 	ucell size;
413 	ucell virt;
414 
415 	PCI_DPRINTF("ob_pci_bar_map_in idx=%p\n", idx);
416 
417 	size = POP();
418 	POP();
419 	POP();
420 	ba = POP();
421 
422 	virt = ob_pci_map(ba, size);
423 
424 	PUSH(virt);
425 }
426 
427 static void
ob_pci_dma_alloc(int * idx)428 ob_pci_dma_alloc(int *idx)
429 {
430     call_parent_method("dma-alloc");
431 }
432 
433 static void
ob_pci_dma_free(int * idx)434 ob_pci_dma_free(int *idx)
435 {
436     call_parent_method("dma-free");
437 }
438 
439 static void
ob_pci_dma_map_in(int * idx)440 ob_pci_dma_map_in(int *idx)
441 {
442     call_parent_method("dma-map-in");
443 }
444 
445 static void
ob_pci_dma_map_out(int * idx)446 ob_pci_dma_map_out(int *idx)
447 {
448     call_parent_method("dma-map-out");
449 }
450 
451 static void
ob_pci_dma_sync(int * idx)452 ob_pci_dma_sync(int *idx)
453 {
454     call_parent_method("dma-sync");
455 }
456 
457 NODE_METHODS(ob_pci_bus_node) = {
458 	{ "open",		ob_pci_open		},
459 	{ "close",		ob_pci_close		},
460 	{ "decode-unit",	ob_pci_decode_unit	},
461 	{ "encode-unit",	ob_pci_encode_unit	},
462 	{ "pci-map-in",		ob_pci_bus_map_in	},
463 	{ "dma-alloc",		ob_pci_dma_alloc	},
464 	{ "dma-free",		ob_pci_dma_free		},
465 	{ "dma-map-in",		ob_pci_dma_map_in	},
466 	{ "dma-map-out",	ob_pci_dma_map_out	},
467 	{ "dma-sync",		ob_pci_dma_sync		},
468 };
469 
470 /* ( pci-addr.lo pci-addr.mid pci-addr.hi size -- virt ) */
471 
472 static void
ob_pci_bridge_map_in(int * idx)473 ob_pci_bridge_map_in(int *idx)
474 {
475 	/* As per the IEEE-1275 PCI specification, chain up to the parent */
476 	call_parent_method("pci-map-in");
477 }
478 
479 NODE_METHODS(ob_pci_bridge_node) = {
480 	{ "open",		ob_pci_open		},
481 	{ "close",		ob_pci_close		},
482 	{ "decode-unit",	ob_pci_decode_unit	},
483 	{ "encode-unit",	ob_pci_encode_unit	},
484 	{ "pci-map-in",		ob_pci_bridge_map_in	},
485 	{ "dma-alloc",		ob_pci_dma_alloc	},
486 	{ "dma-free",		ob_pci_dma_free		},
487 	{ "dma-map-in",		ob_pci_dma_map_in	},
488 	{ "dma-map-out",	ob_pci_dma_map_out	},
489 	{ "dma-sync",		ob_pci_dma_sync		},
490 };
491 
492 NODE_METHODS(ob_pci_simple_node) = {
493 	{ "open",		ob_pci_open		},
494 	{ "close",		ob_pci_close		},
495 };
496 
pci_set_bus_range(const pci_config_t * config)497 static void pci_set_bus_range(const pci_config_t *config)
498 {
499 	phandle_t dev = find_dev(config->path);
500 	u32 props[2];
501 
502 	props[0] = config->secondary_bus;
503 	props[1] = config->subordinate_bus;
504 
505 	PCI_DPRINTF("setting bus range for %s PCI device, "
506 	        "package handle " FMT_ucellx " "
507             "bus primary=%d secondary=%d subordinate=%d\n",
508             config->path,
509             dev,
510             config->primary_bus,
511             config->secondary_bus,
512             config->subordinate_bus);
513 
514 
515 	set_property(dev, "bus-range", (char *)props, 2 * sizeof(props[0]));
516 }
517 
518 static void ob_pci_reload_device_path(phandle_t phandle, pci_config_t *config);
519 
pci_host_set_reg(phandle_t phandle,pci_config_t * config)520 static void pci_host_set_reg(phandle_t phandle, pci_config_t *config)
521 {
522     phandle_t dev = phandle;
523 
524     /* at most 2 integers for address and size */
525     u32 props[4];
526     int ncells = 0;
527 
528     ncells += encode_int32_cells(host_address_cells(), props + ncells,
529             arch->cfg_base);
530 
531     ncells += encode_int32_cells(host_size_cells(), props + ncells,
532             arch->cfg_len);
533 
534     set_property(dev, "reg", (char *)props, ncells * sizeof(props[0]));
535 
536     ob_pci_reload_device_path(dev, config);
537 
538 #if defined(CONFIG_DEBUG_PCI)
539     dump_reg_property("pci_host_set_reg", 4, props);
540 #endif
541 }
542 
543 /* child-phys : parent-phys : size */
544 /* 3 cells for PCI : 2 cells for 64bit parent : 2 cells for PCI */
545 
pci_host_set_ranges(const pci_config_t * config)546 static void pci_host_set_ranges(const pci_config_t *config)
547 {
548 	phandle_t dev = get_cur_dev();
549 	u32 props[32];
550 	int ncells = 0;
551 	pci_range_t range;
552 	int i;
553 
554 	for (i = 0; i < 4; i++) {
555 		range = arch->host_ranges[i];
556 
557 		/* End of range list reached */
558 		if (range.type == 0x0 && range.len == 0x0) {
559 			break;
560 		}
561 
562 		ncells += pci_encode_phys_addr(props + ncells, 0, range.type,
563 						0, 0, range.parentaddr);
564 		ncells += host_encode_phys_addr(props + ncells, range.childaddr);
565 		ncells += pci_encode_size(props + ncells, range.len);
566 	}
567 
568 	set_property(dev, "ranges", (char *)props, ncells * sizeof(props[0]));
569 }
570 
host_config_cb(const pci_config_t * config)571 int host_config_cb(const pci_config_t *config)
572 {
573 	pci_host_set_ranges(config);
574 
575 	return 0;
576 }
577 
sabre_configure(phandle_t dev)578 static int sabre_configure(phandle_t dev)
579 {
580         uint32_t props[28];
581 
582         /* Sabre has a custom reg property from the default */
583         props[0] = 0x1fe;
584         props[1] = 0x0;
585         props[2] = 0x0;
586         props[3] = 0x10000;
587         props[4] = 0x1fe;
588         props[5] = 0x1000000;
589         props[6] = 0x0;
590         props[7] = 0x100;
591         set_property(dev, "reg", (char *)props, 8 * sizeof(props[0]));
592 
593         props[0] = 0xc0000000;
594         props[1] = 0x20000000;
595         set_property(dev, "virtual-dma", (char *)props, 2 * sizeof(props[0]));
596         props[0] = 1;
597         set_property(dev, "#virtual-dma-size-cells", (char *)props,
598                      sizeof(props[0]));
599         set_property(dev, "#virtual-dma-addr-cells", (char *)props,
600                      sizeof(props[0]));
601 
602         set_property(dev, "no-streaming-cache", (char *)props, 0);
603 
604         props[0] = 0x000007f0;
605         props[1] = 0x000007ee;
606         props[2] = 0x000007ef;
607         props[3] = 0x000007e5;
608         set_property(dev, "interrupts", (char *)props, 4 * sizeof(props[0]));
609         props[0] = 0x0000001f;
610         set_property(dev, "upa-portid", (char *)props, 1 * sizeof(props[0]));
611         return 0;
612 }
613 
sabre_config_cb(const pci_config_t * config)614 int sabre_config_cb(const pci_config_t *config)
615 {
616     host_config_cb(config);
617 
618     return sabre_configure(get_cur_dev());
619 }
620 
bridge_config_cb(const pci_config_t * config)621 int bridge_config_cb(const pci_config_t *config)
622 {
623 	phandle_t aliases;
624 
625 	aliases = find_dev("/aliases");
626 	set_property(aliases, "bridge", config->path, strlen(config->path) + 1);
627 
628 	return 0;
629 }
630 
simba_config_cb(const pci_config_t * config)631 int simba_config_cb(const pci_config_t *config)
632 {
633     u32 props[128];
634     int ncells = 0;
635 
636     bridge_config_cb(config);
637 
638     /* Configure the simba ranges as per the mostly undocumented
639        PCI config register in Linux's apb_fake_ranges():
640 
641        pci@1,1 (pciA):
642            IO: 0x1fe02000000-0x1fe027fffff
643           MEM: 0x1ff20000000-0x1ff5fffffff
644 
645        pci@1 (pciB):
646            IO: 0x1fe02800000-0x1fe02ffffff
647           MEM: 0x1ff60000000-0x1ff9fffffff
648     */
649 
650     switch (PCI_FN(config->dev)) {
651     case 1:
652         /* IO: 0x1fe02000000-0x1fe027fffff */
653         pci_config_write8(config->dev, 0xde, 0x0f);
654 
655         /* MEM: 0x1ff20000000-0x1ff5fffffff */
656         pci_config_write8(config->dev, 0xdf, 0x06);
657 
658         /* Onboard NIC: slot 1, intno 0x21 */
659         ncells += pci_encode_phys_addr(props + ncells, 0, 0, PCI_ADDR(1, 1, 0), 0, 0);
660         props[ncells++] = 0x1;
661         props[ncells++] = find_dev("/pci");
662         props[ncells++] = 0x21;
663 
664         /* Onboard IDE: slot 3, intno 0x20 */
665         ncells += pci_encode_phys_addr(props + ncells, 0, 0, PCI_ADDR(1, 3, 0), 0, 0);
666         props[ncells++] = 0x1;
667         props[ncells++] = find_dev("/pci");
668         props[ncells++] = 0x20;
669         set_property(get_cur_dev(), "interrupt-map", (char *)props, ncells * sizeof(props[0]));
670 
671         props[0] = 0x00fff800;
672         props[1] = 0x0;
673         props[2] = 0x0;
674         props[3] = 0x7;
675         set_property(get_cur_dev(), "interrupt-map-mask", (char *)props, 4 * sizeof(props[0]));
676         break;
677 
678     case 0:
679         /* IO: 0x1fe02800000-0x1fe02ffffff */
680         pci_config_write8(config->dev, 0xde, 0xf0);
681 
682         /* MEM: 0x1ff60000000-0x1ff9fffffff */
683         pci_config_write8(config->dev, 0xdf, 0x18);
684         break;
685     }
686 
687     return 0;
688 }
689 
ide_config_cb2(const pci_config_t * config)690 int ide_config_cb2 (const pci_config_t *config)
691 {
692 	ob_ide_init(config->path,
693 		    config->assigned[0] & ~0x0000000F,
694 		    (config->assigned[1] & ~0x0000000F) + 2,
695 		    config->assigned[2] & ~0x0000000F,
696 		    (config->assigned[3] & ~0x0000000F) + 2);
697 	return 0;
698 }
699 
eth_config_cb(const pci_config_t * config)700 int eth_config_cb (const pci_config_t *config)
701 {
702 	phandle_t ph = get_cur_dev();
703 
704 	set_property(ph, "network-type", "ethernet", 9);
705 	set_property(ph, "removable", "network", 8);
706 	set_property(ph, "category", "net", 4);
707 
708         return 0;
709 }
710 
sunhme_config_cb(const pci_config_t * config)711 int sunhme_config_cb(const pci_config_t *config)
712 {
713 	phandle_t ph = get_cur_dev();
714 
715 	set_int_property(ph, "hm-rev", 0x21);
716 
717 	return eth_config_cb(config);
718 }
719 
rtl8139_config_cb(const pci_config_t * config)720 int rtl8139_config_cb(const pci_config_t *config)
721 {
722 #ifdef CONFIG_PPC
723 	/* Apple's OF seemingly enables bus mastering on some cards by
724 	 * default, which means that some buggy drivers forget to
725 	 * explicitly set it (OS X, MorphOS). Mimic this behaviour so
726 	 * that these buggy drivers work under emulation. */
727 	if (is_apple()) {
728 		ob_pci_enable_bus_master(config);
729 	}
730 #endif
731 
732 	return eth_config_cb(config);
733 }
734 
sungem_config_cb(const pci_config_t * config)735 int sungem_config_cb (const pci_config_t *config)
736 {
737 	phandle_t ph = get_cur_dev();
738 	uint32_t val, *mmio;
739 	uint8_t mac[6];
740 	ucell virt;
741 
742 #define MAC_ADDR0	(0x6080UL/4)	/* MAC Address 0 Register	*/
743 #define MAC_ADDR1	(0x6084UL/4)	/* MAC Address 1 Register	*/
744 #define MAC_ADDR2	(0x6088UL/4)	/* MAC Address 2 Register	*/
745 
746 	/* Map PCI memory BAR 0 to access the sungem registers */
747 	virt = ob_pci_map(config->assigned[0], 0x8000);
748 	mmio = (void *)(uintptr_t)virt;
749 
750 	val = __le32_to_cpu(*(mmio + MAC_ADDR0));
751 	mac[5] = val & 0xff;
752 	mac[4] = (val >> 8) & 0xff;
753 	val = __le32_to_cpu(*(mmio + MAC_ADDR1));
754 	mac[3] = val & 0xff;
755 	mac[2] = (val >> 8) & 0xff;
756 	val = __le32_to_cpu(*(mmio + MAC_ADDR2));
757 	mac[1] = val & 0xff;
758 	mac[0] = (val >> 8) & 0xff;
759 	set_property(ph, "local-mac-address", (char *)mac, 6);
760 
761 	ob_pci_unmap(virt, 0x8000);
762 	return 0;
763 }
764 
virtio_blk_config_cb(const pci_config_t * config)765 int virtio_blk_config_cb(const pci_config_t *config)
766 {
767 #ifdef CONFIG_DRIVER_VIRTIO_BLK
768 	pci_addr addr;
769 	uint8_t idx, cap_idx, cap_vndr;
770 	uint8_t cfg_type, bar;
771 	uint16_t status;
772 	uint32_t offset, notify_mult = 0;
773 	uint64_t common_cfg = 0, device_cfg = 0, notify_base = 0;
774 
775 	addr = PCI_ADDR(
776 		PCI_BUS(config->dev),
777 		PCI_DEV(config->dev),
778 		PCI_FN(config->dev));
779 
780 	idx = (uint8_t)(pci_config_read16(addr, PCI_DEVICE_ID) & 0xff) - 1;
781 
782 	/*  Check PCI capabilties: if they don't exist then we're certainly not
783 		a 1.0 device */
784 	status = pci_config_read16(addr, PCI_STATUS);
785 	if (!(status & PCI_STATUS_CAP_LIST)) {
786 		return 0;
787 	}
788 
789 	/* Locate VIRTIO_PCI_CAP_COMMON_CFG and VIRTIO_PCI_CAP_DEVICE_CFG */
790 	cap_idx = pci_config_read8(addr, PCI_CAPABILITY_LIST);
791 	while ((cap_vndr = pci_config_read8(addr, cap_idx)) != 0) {
792 		if (cap_vndr == PCI_CAP_ID_VNDR) {
793 			cfg_type = pci_config_read8(addr, cap_idx + 0x3);
794 			bar = pci_config_read8(addr, cap_idx + 0x4);
795 			offset = pci_config_read32(addr, cap_idx + 0x8);
796 
797 			switch (cfg_type) {
798 			case VIRTIO_PCI_CAP_COMMON_CFG:
799 				common_cfg = arch->host_pci_base + (config->assigned[bar] & ~0x0000000F) + offset;
800 				break;
801 			case VIRTIO_PCI_CAP_NOTIFY_CFG:
802 				notify_base = arch->host_pci_base + (config->assigned[bar] & ~0x0000000F) + offset;
803 				notify_mult = pci_config_read32(addr, cap_idx + 16);
804 				break;
805 			case VIRTIO_PCI_CAP_DEVICE_CFG:
806 				device_cfg = arch->host_pci_base + (config->assigned[bar] & ~0x0000000F) + offset;
807 				break;
808 			}
809 		}
810 
811 		cap_idx = pci_config_read8(addr, cap_idx + 1);
812 	}
813 
814 	/* If we didn't find the required configuration then exit */
815 	if (common_cfg == 0 || device_cfg == 0 || notify_base == 0) {
816 		return 0;
817 	}
818 
819 	/* Enable bus mastering to ensure vring processing will run. */
820 	ob_pci_enable_bus_master(config);
821 
822 	ob_virtio_init(config->path, "virtio-blk", common_cfg, device_cfg,
823 					notify_base, notify_mult, idx);
824 #endif
825 	return 0;
826 }
827 
828 /*
829  * "Designing PCI Cards and Drivers for Power Macintosh Computers", p. 454
830  *
831  *  "AAPL,address" provides an array of 32-bit logical addresses
832  *  Nth entry corresponding to Nth "assigned-address" base address entry.
833  */
834 
pci_set_AAPL_address(const pci_config_t * config)835 static void pci_set_AAPL_address(const pci_config_t *config)
836 {
837 	phandle_t dev = get_cur_dev();
838 	cell props[7];
839 	uint32_t mask;
840 	int ncells, i, flags, space_code;
841 
842 	ncells = 0;
843 	for (i = 0; i < 6; i++) {
844 		if (!config->assigned[i] || !config->sizes[i])
845 			continue;
846 		pci_decode_pci_addr(config->assigned[i],
847 				    &flags, &space_code, &mask);
848 
849 		props[ncells++] = pci_bus_addr_to_host_addr(space_code,
850 					config->assigned[i] & ~mask);
851 	}
852 	if (ncells)
853 		set_property(dev, "AAPL,address", (char *)props,
854 			     ncells * sizeof(cell));
855 }
856 
pci_set_assigned_addresses(phandle_t phandle,const pci_config_t * config,int num_bars)857 static void pci_set_assigned_addresses(phandle_t phandle,
858                                        const pci_config_t *config, int num_bars)
859 {
860 	phandle_t dev = phandle;
861 	u32 props[32];
862 	int ncells;
863 	int i;
864 	uint32_t mask;
865 	int flags, space_code;
866 
867 	ncells = 0;
868 	for (i = 0; i < num_bars; i++) {
869 		/* consider only bars with non-zero region size */
870 		if (!config->sizes[i])
871 			continue;
872 		pci_decode_pci_addr(config->assigned[i],
873 				    &flags, &space_code, &mask);
874 
875 		ncells += pci_encode_phys_addr(props + ncells,
876 				     flags, space_code, config->dev,
877 				     PCI_BASE_ADDR_0 + (i * sizeof(uint32_t)),
878 				     config->assigned[i] & ~mask);
879 
880 		props[ncells++] = 0x00000000;
881 		props[ncells++] = config->sizes[i];
882 	}
883 	if (ncells)
884 		set_property(dev, "assigned-addresses", (char *)props,
885 			     ncells * sizeof(props[0]));
886 }
887 
888 /* call after writing "reg" property to update config->path */
ob_pci_reload_device_path(phandle_t phandle,pci_config_t * config)889 static void ob_pci_reload_device_path(phandle_t phandle, pci_config_t *config)
890 {
891     /* since "name" and "reg" are now assigned
892        we need to reload current node name */
893     char *new_path = get_path_from_ph(phandle);
894     if (new_path) {
895         if (0 != strcmp(config->path, new_path)) {
896             PCI_DPRINTF("\n=== CHANGED === package path old=%s new=%s\n",
897                     config->path, new_path);
898             strncpy(config->path, new_path, sizeof(config->path));
899             config->path[sizeof(config->path)-1] = '\0';
900         }
901         free(new_path);
902     } else {
903         PCI_DPRINTF("\n=== package path old=%s new=NULL\n", config->path);
904     }
905 }
906 
pci_set_reg(phandle_t phandle,pci_config_t * config,int num_bars)907 static void pci_set_reg(phandle_t phandle,
908                         pci_config_t *config, int num_bars)
909 {
910 	phandle_t dev = phandle;
911 	u32 props[38];
912 	int ncells;
913 	int i;
914 	uint32_t mask;
915 	int space_code, flags;
916 
917     ncells = 0;
918 
919     /* first (addr, size) pair is the beginning of configuration address space */
920 	ncells += pci_encode_phys_addr(props + ncells, 0, CONFIGURATION_SPACE,
921 			     config->dev, 0, 0);
922 
923 	ncells += pci_encode_size(props + ncells, 0);
924 
925 	for (i = 0; i < num_bars; i++) {
926 		/* consider only bars with non-zero region size */
927 		if (!config->sizes[i])
928 			continue;
929 
930 		pci_decode_pci_addr(config->regions[i],
931 				    &flags, &space_code, &mask);
932 
933 		ncells += pci_encode_phys_addr(props + ncells,
934 				     flags, space_code, config->dev,
935 				     PCI_BASE_ADDR_0 + (i * sizeof(uint32_t)),
936 				     config->regions[i] & ~mask);
937 
938 		/* set size */
939 		ncells += pci_encode_size(props + ncells, config->sizes[i]);
940 	}
941 
942 	set_property(dev, "reg", (char *)props, ncells * sizeof(props[0]));
943     ob_pci_reload_device_path(dev, config);
944 
945 #if defined(CONFIG_DEBUG_PCI)
946     dump_reg_property("pci_set_reg", ncells, props);
947 #endif
948 }
949 
950 
pci_set_ranges(const pci_config_t * config)951 static void pci_set_ranges(const pci_config_t *config)
952 {
953 	phandle_t dev = get_cur_dev();
954 	u32 props[32];
955 	int ncells;
956   	int i;
957 	uint32_t mask;
958 	int flags;
959 	int space_code;
960 
961 	ncells = 0;
962 	for (i = 0; i < 6; i++) {
963 		if (!config->assigned[i] || !config->sizes[i])
964 			continue;
965 
966 		/* child address */
967 
968 		props[ncells++] = 0x00000000;
969 
970 		/* parent address */
971 
972 		pci_decode_pci_addr(config->assigned[i],
973 				    &flags, &space_code, &mask);
974 		ncells += pci_encode_phys_addr(props + ncells, flags, space_code,
975 				     config->dev, 0x10 + i * 4,
976 				     config->assigned[i] & ~mask);
977 
978 		/* size */
979 
980 		props[ncells++] = config->sizes[i];
981   	}
982 	set_property(dev, "ranges", (char *)props, ncells * sizeof(props[0]));
983 }
984 
macio_heathrow_config_cb(const pci_config_t * config)985 int macio_heathrow_config_cb (const pci_config_t *config)
986 {
987 	pci_set_ranges(config);
988 
989 #ifdef CONFIG_DRIVER_MACIO
990         ob_macio_heathrow_init(config->path, config->assigned[0] & ~0x0000000F);
991 #endif
992 	return 0;
993 }
994 
macio_keylargo_config_cb(const pci_config_t * config)995 int macio_keylargo_config_cb (const pci_config_t *config)
996 {
997         pci_set_ranges(config);
998 
999 #ifdef CONFIG_DRIVER_MACIO
1000         ob_macio_keylargo_init(config->path, config->assigned[0] & ~0x0000000F);
1001 #endif
1002         return 0;
1003 }
1004 
vga_config_cb(const pci_config_t * config)1005 int vga_config_cb (const pci_config_t *config)
1006 {
1007 #ifdef CONFIG_PPC
1008         unsigned long rom;
1009         uint32_t rom_size, size, bar;
1010         phandle_t ph;
1011 #endif
1012         if (config->assigned[0] != 0x00000000) {
1013             setup_video();
1014 
1015 #ifdef CONFIG_PPC
1016             if (config->assigned[6]) {
1017                     rom = pci_bus_addr_to_host_addr(MEMORY_SPACE_32,
1018                                                     config->assigned[6] & ~0x0000000F);
1019                     rom_size = config->sizes[6];
1020 
1021                     bar = pci_config_read32(config->dev, PCI_ROM_ADDRESS);
1022                     bar |= PCI_ROM_ADDRESS_ENABLE;
1023                     pci_config_write32(config->dev, PCI_COMMAND, bar);
1024                     ph = get_cur_dev();
1025 
1026                     if (rom_size >= 8) {
1027                             const char *p;
1028 
1029                             p = (const char *)rom;
1030                             if (p[0] == 'N' && p[1] == 'D' && p[2] == 'R' && p[3] == 'V') {
1031                                     size = *(uint32_t*)(p + 4);
1032                                     set_property(ph, "driver,AAPL,MacOS,PowerPC",
1033                                                  p + 8, size);
1034                             } else if (p[0] == 'J' && p[1] == 'o' &&
1035                                        p[2] == 'y' && p[3] == '!') {
1036                                     set_property(ph, "driver,AAPL,MacOS,PowerPC",
1037                                                  p, rom_size);
1038                             }
1039                     }
1040             }
1041 #endif
1042 
1043             /* Currently we don't read FCode from the hardware but execute
1044              * it directly */
1045             feval("['] vga-driver-fcode 2 cells + 1 byte-load");
1046 
1047 #ifdef CONFIG_MOL
1048             /* Install special words for Mac On Linux */
1049             molvideo_init();
1050 #endif
1051         }
1052 
1053 	return 0;
1054 }
1055 
ebus_config_cb(const pci_config_t * config)1056 int ebus_config_cb(const pci_config_t *config)
1057 {
1058 #ifdef CONFIG_DRIVER_EBUS
1059     phandle_t dev = get_cur_dev();
1060     uint32_t props[12];
1061     int ncells;
1062     int i;
1063     uint32_t mask;
1064     int flags, space_code;
1065     ucell virt;
1066     phys_addr_t io_phys_base = 0;
1067 
1068     /* Serial */
1069     props[0] = 0x14;
1070     props[1] = 0x3f8;
1071     props[2] = 1;
1072     props[3] = find_dev("/pci");
1073     props[4] = 0x2b;
1074 
1075     /* PS2 keyboard */
1076     props[5] = 0x14;
1077     props[6] = 0x60;
1078     props[7] = 1;
1079     props[8] = find_dev("/pci");
1080     props[9] = 0x29;
1081 
1082     set_property(dev, "interrupt-map", (char *)props, 10 * sizeof(props[0]));
1083 
1084     props[0] = 0x000001ff;
1085     props[1] = 0xffffffff;
1086     props[2] = 3;
1087     set_property(dev, "interrupt-map-mask", (char *)props, 3 * sizeof(props[0]));
1088 
1089     /* Build ranges property from the BARs */
1090     ncells = 0;
1091     for (i = 0; i < 6; i++) {
1092         /* consider only bars with non-zero region size */
1093         if (!config->sizes[i])
1094             continue;
1095 
1096         pci_decode_pci_addr(config->assigned[i],
1097                             &flags, &space_code, &mask);
1098 
1099         props[ncells++] = PCI_BASE_ADDR_0 + (i * sizeof(uint32_t));
1100         props[ncells++] = 0x0;
1101 
1102         ncells += pci_encode_phys_addr(props + ncells,
1103                                        flags, space_code, config->dev,
1104                                        PCI_BASE_ADDR_0 + (i * sizeof(uint32_t)),
1105                                        config->assigned[i] & ~mask);
1106 
1107         props[ncells++] = config->sizes[i];
1108 
1109         /* Store base of IO space for NVRAM */
1110         if (io_phys_base == 0x0 && space_code == IO_SPACE) {
1111             io_phys_base = pci_bus_addr_to_host_addr(space_code, config->assigned[i] & ~mask);
1112         }
1113     }
1114 
1115     set_property(dev, "ranges", (char *)props, ncells * sizeof(props[0]));
1116 
1117     /*  Build eeprom node */
1118     fword("new-device");
1119     PUSH(0x14);
1120     fword("encode-int");
1121     PUSH(0x2000);
1122     fword("encode-int");
1123     fword("encode+");
1124     PUSH(0x2000);
1125     fword("encode-int");
1126     fword("encode+");
1127     push_str("reg");
1128     fword("property");
1129 
1130     push_str("mk48t59");
1131     fword("model");
1132 
1133     /* OpenSolaris (e.g. Milax) requires the RTC to be pre-mapped by the PROM */
1134     virt = ofmem_map_io(io_phys_base + 0x2000, 0x2000);
1135     PUSH(virt);
1136     fword("encode-int");
1137     push_str("address");
1138     fword("property");
1139 
1140     push_str("eeprom");
1141     fword("device-name");
1142     fword("finish-device");
1143 
1144     /* Build power node */
1145     fword("new-device");
1146     PUSH(0x14);
1147     fword("encode-int");
1148     PUSH(0x7240);
1149     fword("encode-int");
1150     fword("encode+");
1151     PUSH(0x4);
1152     fword("encode-int");
1153     fword("encode+");
1154     push_str("reg");
1155     fword("property");
1156 
1157     PUSH(0);
1158     PUSH(0);
1159     push_str("button");
1160     fword("property");
1161 
1162     PUSH(1);
1163     fword("encode-int");
1164     push_str("interrupts");
1165     fword("property");
1166 
1167     /* Map the power registers so we can use them */
1168     virt = ofmem_map_io(io_phys_base + 0x7240, 0x4);
1169     PUSH(virt);
1170     fword("encode-int");
1171     push_str("address");
1172     fword("property");
1173 
1174     push_str("power");
1175     fword("device-name");
1176     fword("finish-device");
1177 
1178 #ifdef CONFIG_DRIVER_FLOPPY
1179     ob_floppy_init(config->path, "fdthree", 0x3f0ULL, 0);
1180 #endif
1181 #ifdef CONFIG_DRIVER_PC_SERIAL
1182     ob_pc_serial_init(config->path, "su", (PCI_BASE_ADDR_1 | 0ULL) << 32, 0x3f8ULL, 0);
1183 #endif
1184 #ifdef CONFIG_DRIVER_PC_KBD
1185     ob_pc_kbd_init(config->path, "kb_ps2", NULL, (PCI_BASE_ADDR_1 | 0ULL) << 32, 0x60ULL, 1, 0);
1186 #endif
1187 #endif
1188     return 0;
1189 }
1190 
i82378_config_cb(const pci_config_t * config)1191 int i82378_config_cb(const pci_config_t *config)
1192 {
1193 #ifdef CONFIG_DRIVER_PC_SERIAL
1194     ob_pc_serial_init(config->path, "serial", arch->io_base, 0x3f8ULL, 0);
1195 #endif
1196 #ifdef CONFIG_DRIVER_PC_KBD
1197     ob_pc_kbd_init(config->path, "keyboard", NULL, arch->io_base, 0x60ULL, 0, 0);
1198 #endif
1199 #ifdef CONFIG_DRIVER_IDE
1200     ob_ide_init(config->path, 0x1f0, 0x3f6, 0x170, 0x376);
1201 #endif
1202 
1203     return 0;
1204 }
1205 
usb_ohci_config_cb(const pci_config_t * config)1206 int usb_ohci_config_cb(const pci_config_t *config)
1207 {
1208 #ifdef CONFIG_DRIVER_USB
1209     pci_addr addr = PCI_ADDR(
1210         PCI_BUS(config->dev), PCI_DEV(config->dev), PCI_FN(config->dev));
1211 
1212     ob_usb_ohci_init(config->path, addr);
1213 #endif
1214     return 0;
1215 }
1216 
lsi53c810_config_cb(const pci_config_t * config)1217 int lsi53c810_config_cb(const pci_config_t *config)
1218 {
1219 #ifdef CONFIG_DRIVER_LSI_53C810
1220     uint64_t mmio, ram;
1221 
1222     /* Enable PCI bus mastering */
1223     ob_pci_enable_bus_master(config);
1224 
1225     /* Map PCI memory BAR 1: LSI MMIO */
1226     mmio = ob_pci_map(config->assigned[1], 0x400);
1227 
1228     /* Map PCI memory BAR 2: LSI RAM */
1229     ram = ob_pci_map(config->assigned[2], 0x400);
1230 
1231     ob_lsi_init(config->path, mmio, ram);
1232 #endif
1233     return 0;
1234 }
1235 
ob_pci_enable_bus_master(const pci_config_t * config)1236 void ob_pci_enable_bus_master(const pci_config_t *config)
1237 {
1238 	/* Enable bus mastering for the PCI device */
1239 	uint16_t cmd;
1240 	pci_addr addr = PCI_ADDR(
1241 		PCI_BUS(config->dev), PCI_DEV(config->dev), PCI_FN(config->dev));
1242 
1243 	cmd = pci_config_read16(addr, PCI_COMMAND);
1244 	cmd |= PCI_COMMAND_BUS_MASTER;
1245 	pci_config_write16(addr, PCI_COMMAND, cmd);
1246 }
1247 
ob_pci_add_properties(phandle_t phandle,pci_addr addr,const pci_dev_t * pci_dev,const pci_config_t * config,int num_bars)1248 static void ob_pci_add_properties(phandle_t phandle,
1249                                   pci_addr addr, const pci_dev_t *pci_dev,
1250                                   const pci_config_t *config, int num_bars)
1251 {
1252 	/* cannot use get_cur_dev() path resolution since "name" and "reg"
1253 	   properties are being changed */
1254 	phandle_t dev=phandle;
1255 	int status,id;
1256 	uint16_t vendor_id, device_id;
1257 	uint8_t rev;
1258 	uint8_t class_prog;
1259 	uint32_t class_code;
1260 	char path[256];
1261 
1262 	vendor_id = pci_config_read16(addr, PCI_VENDOR_ID);
1263 	device_id = pci_config_read16(addr, PCI_DEVICE_ID);
1264 	rev = pci_config_read8(addr, PCI_REVISION_ID);
1265 	class_prog = pci_config_read8(addr, PCI_CLASS_PROG);
1266 	class_code = pci_config_read16(addr, PCI_CLASS_DEVICE);
1267 
1268     /* Default path if we don't match anything */
1269     snprintf(path, sizeof(path), "pci%x,%x", vendor_id, device_id);
1270 
1271     if (pci_dev) {
1272         /**/
1273         if (pci_dev->name) {
1274             push_str(pci_dev->name);
1275             fword("device-name");
1276         } else {
1277             push_str(path);
1278             fword("device-name");
1279         }
1280     } else {
1281         PCI_DPRINTF("*** missing pci_dev\n");
1282         push_str(path);
1283         fword("device-name");
1284     }
1285 
1286 	/* create properties as described in 2.5 */
1287 
1288 	set_int_property(dev, "vendor-id", vendor_id);
1289 	set_int_property(dev, "device-id", device_id);
1290 	set_int_property(dev, "revision-id", rev);
1291 	set_int_property(dev, "class-code", class_code << 8 | class_prog);
1292 
1293 	if (config->irq_pin) {
1294 		if (is_oldworld()) {
1295 			set_int_property(dev, "AAPL,interrupts", config->irq_line);
1296 		} else {
1297 			set_int_property(dev, "interrupts", config->irq_pin);
1298 		}
1299 	}
1300 
1301 	set_int_property(dev, "min-grant", pci_config_read8(addr, PCI_MIN_GNT));
1302 	set_int_property(dev, "max-latency", pci_config_read8(addr, PCI_MAX_LAT));
1303 
1304 	status=pci_config_read16(addr, PCI_STATUS);
1305 
1306 	set_int_property(dev, "devsel-speed",
1307 			(status&PCI_STATUS_DEVSEL_MASK)>>10);
1308 
1309 	if(status&PCI_STATUS_FAST_BACK)
1310 		set_bool_property(dev, "fast-back-to-back");
1311 	if(status&PCI_STATUS_66MHZ)
1312 		set_bool_property(dev, "66mhz-capable");
1313 	if(status&PCI_STATUS_UDF)
1314 		set_bool_property(dev, "udf-supported");
1315 
1316 	id=pci_config_read16(addr, PCI_SUBSYSTEM_VENDOR_ID);
1317 	if(id)
1318 		set_int_property(dev, "subsystem-vendor-id", id);
1319 	id=pci_config_read16(addr, PCI_SUBSYSTEM_ID);
1320 	if(id)
1321 		set_int_property(dev, "subsystem-id", id);
1322 
1323 	set_int_property(dev, "cache-line-size",
1324 			pci_config_read16(addr, PCI_CACHE_LINE_SIZE));
1325 
1326 	if (pci_dev) {
1327 		if (pci_dev->type) {
1328 			push_str(pci_dev->type);
1329 			fword("device-type");
1330 		}
1331 		if (pci_dev->model) {
1332 			push_str(pci_dev->model);
1333 			fword("model");
1334 		}
1335 		if (pci_dev->compat)
1336 			set_property(dev, "compatible",
1337 				     pci_dev->compat, pci_compat_len(pci_dev));
1338 
1339 		if (pci_dev->acells)
1340 			set_int_property(dev, "#address-cells",
1341 					      pci_dev->acells);
1342 		if (pci_dev->scells)
1343 			set_int_property(dev, "#size-cells",
1344 					       pci_dev->scells);
1345 		if (pci_dev->icells)
1346 			set_int_property(dev, "#interrupt-cells",
1347 					      pci_dev->icells);
1348 	}
1349 
1350 	pci_set_assigned_addresses(phandle, config, num_bars);
1351 
1352 	if (is_apple() && is_oldworld())
1353 		pci_set_AAPL_address(config);
1354 
1355 	PCI_DPRINTF("\n");
1356 }
1357 
1358 #ifdef CONFIG_XBOX
pci_xbox_ignore_device(int bus,int devnum,int fn)1359 static char pci_xbox_ignore_device (int bus, int devnum, int fn)
1360 {
1361 	/*
1362 	 * The Xbox MCPX chipset is a derivative of the nForce 1
1363 	 * chipset. It almost has the same bus layout; some devices
1364 	 * cannot be used, because they have been removed.
1365 	 */
1366 
1367 	/*
1368 	 * Devices 00:00.1 and 00:00.2 used to be memory controllers on
1369 	 * the nForce chipset, but on the Xbox, using them will lockup
1370 	 * the chipset.
1371 	 */
1372 	if ((bus == 0) && (devnum == 0) && ((fn == 1) || (fn == 2)))
1373 		return 1;
1374 
1375 	/*
1376 	 * Bus 1 only contains a VGA controller at 01:00.0. When you try
1377 	 * to probe beyond that device, you only get garbage, which
1378 	 * could cause lockups.
1379 	 */
1380 	if ((bus == 1) && ((devnum != 0) || (fn != 0)))
1381 		return 1;
1382 
1383 	/*
1384 	 * Bus 2 used to contain the AGP controller, but the Xbox MCPX
1385 	 * doesn't have one. Probing it can cause lockups.
1386 	 */
1387 	if (bus >= 2)
1388 		return 1;
1389 
1390 	return 0;
1391 }
1392 #endif
1393 
ob_pci_configure_bar(pci_addr addr,pci_config_t * config,int reg,int config_addr,uint32_t * p_omask,unsigned long * mem_base,unsigned long * io_base)1394 static void ob_pci_configure_bar(pci_addr addr, pci_config_t *config,
1395                                  int reg, int config_addr,
1396                                  uint32_t *p_omask,
1397                                  unsigned long *mem_base,
1398                                  unsigned long *io_base)
1399 {
1400         uint32_t smask, amask, size, reloc, min_align;
1401         unsigned long base;
1402 
1403         config->assigned[reg] = 0x00000000;
1404         config->sizes[reg] = 0x00000000;
1405 
1406         if ((*p_omask & 0x0000000f) == 0x4) {
1407                 /* 64 bits memory mapping */
1408                 PCI_DPRINTF("Skipping 64 bit BARs for %s\n", config->path);
1409                 return;
1410         }
1411 
1412         config->regions[reg] = pci_config_read32(addr, config_addr);
1413 
1414         /* get region size */
1415 
1416         pci_config_write32(addr, config_addr, 0xffffffff);
1417         smask = pci_config_read32(addr, config_addr);
1418         if (smask == 0x00000000 || smask == 0xffffffff)
1419                 return;
1420 
1421         if (smask & 0x00000001 && reg != 6) {
1422                 /* I/O space */
1423                 base = *io_base;
1424                 min_align = 1 << 7;
1425                 amask = 0x00000001;
1426         } else {
1427                 /* Memory Space */
1428                 base = *mem_base;
1429                 min_align = 1 << 16;
1430                 amask = 0x0000000F;
1431                 if (reg == 6) {
1432                         smask |= 1; /* ROM */
1433                 }
1434         }
1435         *p_omask = smask & amask;
1436         smask &= ~amask;
1437         size = (~smask) + 1;
1438         config->sizes[reg] = size;
1439         reloc = base;
1440         if (size < min_align)
1441                 size = min_align;
1442         reloc = (reloc + size -1) & ~(size - 1);
1443         if (*io_base == base) {
1444                 PCI_DPRINTF("changing io_base from 0x%lx to 0x%x\n",
1445                             *io_base, reloc + size);
1446                 *io_base = reloc + size;
1447         } else {
1448                 PCI_DPRINTF("changing mem_base from 0x%lx to 0x%x\n",
1449                             *mem_base, reloc + size);
1450                 *mem_base = reloc + size;
1451         }
1452         PCI_DPRINTF("Configuring BARs for %s: reloc 0x%x omask 0x%x "
1453                     "io_base 0x%lx mem_base 0x%lx size 0x%x\n",
1454                     config->path, reloc, *p_omask, *io_base, *mem_base, size);
1455         pci_config_write32(addr, config_addr, reloc | *p_omask);
1456         config->assigned[reg] = reloc | *p_omask;
1457 }
1458 
ob_pci_configure_irq(pci_addr addr,pci_config_t * config)1459 static void ob_pci_configure_irq(pci_addr addr, pci_config_t *config)
1460 {
1461         uint8_t irq_pin, irq_line;
1462 
1463         irq_pin =  pci_config_read8(addr, PCI_INTERRUPT_PIN);
1464         if (irq_pin) {
1465                 config->irq_pin = irq_pin;
1466                 irq_pin = (((config->dev >> 11) & 0x1F) + irq_pin - 1) & 3;
1467                 irq_line = arch->irqs[irq_pin];
1468                 pci_config_write8(addr, PCI_INTERRUPT_LINE, irq_line);
1469                 config->irq_line = irq_line;
1470         } else
1471                 config->irq_line = -1;
1472 }
1473 
1474 static void
ob_pci_configure(pci_addr addr,pci_config_t * config,int num_regs,int rom_bar,unsigned long * mem_base,unsigned long * io_base)1475 ob_pci_configure(pci_addr addr, pci_config_t *config, int num_regs, int rom_bar,
1476                  unsigned long *mem_base, unsigned long *io_base)
1477 
1478 {
1479         uint32_t omask, mask;
1480         uint16_t cmd;
1481         int reg, flags, space_code;
1482         pci_addr config_addr;
1483 
1484         ob_pci_configure_irq(addr, config);
1485 
1486         omask = 0x00000000;
1487         for (reg = 0; reg < num_regs; ++reg) {
1488                 config_addr = PCI_BASE_ADDR_0 + reg * 4;
1489 
1490                 ob_pci_configure_bar(addr, config, reg, config_addr,
1491                                      &omask, mem_base,
1492                                      io_base);
1493 
1494                 /* Ignore 64-bit BAR MSBs (always map in 32-bit space) */
1495                 pci_decode_pci_addr(config->assigned[reg],
1496                                     &flags, &space_code, &mask);
1497 
1498                 if (space_code == MEMORY_SPACE_64) {
1499                     reg++;
1500                 }
1501         }
1502 
1503         if (rom_bar) {
1504                 config_addr = rom_bar;
1505                 ob_pci_configure_bar(addr, config, reg, config_addr,
1506                                      &omask, mem_base, io_base);
1507         }
1508         cmd = pci_config_read16(addr, PCI_COMMAND);
1509         cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
1510         pci_config_write16(addr, PCI_COMMAND, cmd);
1511 }
1512 
1513 static phandle_t ob_configure_pci_device(const char* parent_path,
1514         int *bus_num, unsigned long *mem_base, unsigned long *io_base,
1515         int bus, int devnum, int fn, int *p_is_multi);
1516 
ob_scan_pci_bus(int * bus_num,unsigned long * mem_base,unsigned long * io_base,const char * path,int bus)1517 static void ob_scan_pci_bus(int *bus_num, unsigned long *mem_base,
1518                             unsigned long *io_base, const char *path,
1519                             int bus)
1520 {
1521 	int devnum, fn, is_multi;
1522 
1523 	PCI_DPRINTF("\nScanning bus %d at %s...\n", bus, path);
1524 
1525 	for (devnum = 0; devnum < 32; devnum++) {
1526 		is_multi = 0;
1527 		for (fn = 0; fn==0 || (is_multi && fn<8); fn++) {
1528 		    ob_configure_pci_device(path, bus_num, mem_base, io_base,
1529 		            bus, devnum, fn, &is_multi);
1530 
1531 		}
1532 	}
1533 }
1534 
1535 #if defined(CONFIG_SPARC64)
1536 
1537 /* Convert device/irq pin to interrupt property */
1538 #define SUN4U_PCIAINTERRUPT(dev, irq_pin) \
1539             ((((dev >> 11) << 2) + irq_pin - 1) & 0x1f)
1540 
1541 #define SUN4U_PCIBINTERRUPT(dev, irq_pin) \
1542             ((0x10 + (((dev >> 11) << 2) + irq_pin - 1)) & 0x1f)
1543 
ob_pci_simbaB_bus_interrupt(ucell dnode,u32 * props,int * ncells,u32 addr,u32 intno)1544 static void ob_pci_simbaB_bus_interrupt(ucell dnode, u32 *props, int *ncells, u32 addr, u32 intno)
1545 {
1546     *ncells += pci_encode_phys_addr(props + *ncells, 0, 0, addr, 0, 0);
1547     props[(*ncells)++] = intno;
1548     props[(*ncells)++] = dnode;
1549     props[(*ncells)++] = SUN4U_PCIBINTERRUPT(addr, intno);
1550 }
1551 
1552 static void ob_pci_bus_set_interrupt_map(phandle_t pcibus, phandle_t dnode,
1553               void (*func)(ucell dnode, u32 *props, int *ncells, u32 addr, u32 intno));
1554 
ob_scan_sabre_pci_bus(int * bus_num,unsigned long * mem_base,unsigned long * io_base,const char * path,int bus)1555 static void ob_scan_sabre_pci_bus(int *bus_num, unsigned long *mem_base,
1556                             unsigned long *io_base, const char *path,
1557                             int bus)
1558 {
1559 	int devnum, fn, is_multi;
1560 	phandle_t ph;
1561 
1562 	PCI_DPRINTF("\nScanning sabre bus %d at %s...\n", bus, path);
1563 
1564 	/* Horrible sabre hack: the PCI bridge with the on-board devices
1565 	   is located at devfn (1,1) so if we use the standard scan function
1566 	   we end up with our ioports not mapped at io_base == 0x0 which
1567 	   breaks many assumptions in OpenBIOS. Hence do a custom scan for
1568 	   sabre which does things in the right order. */
1569 	for (devnum = 0; devnum < 32; devnum++) {
1570 		is_multi = 0;
1571 
1572 		if (devnum == 1) {
1573 			/* Force io_base/mem_base to match the pciA simba range */
1574 			*io_base = 0x0;       /* because of arch->iobase */
1575 			*mem_base = 0x20000000;
1576 
1577 			ob_configure_pci_device(path, bus_num, mem_base, io_base,
1578 				bus, 1, 1, &is_multi);
1579 
1580 			/* Force io_base/mem_base to match the pciB simba range */
1581 			*io_base = 0x800000;  /* because of arch->iobase */
1582 			*mem_base = 0x60000000;
1583 
1584 			ph = ob_configure_pci_device(path, bus_num, mem_base, io_base,
1585 				bus, 1, 0, &is_multi);
1586 
1587 			/* Set up pciB interrupt map */
1588                         ob_pci_bus_set_interrupt_map(ph, find_dev("/pci"), ob_pci_simbaB_bus_interrupt);
1589 		} else {
1590 			for (fn = 0; fn==0 || (is_multi && fn<8); fn++) {
1591 				ob_configure_pci_device(path, bus_num, mem_base, io_base,
1592 					bus, devnum, fn, &is_multi);
1593 			}
1594 		}
1595 	}
1596 }
1597 #endif
1598 
ob_configure_pci_bridge(pci_addr addr,int * bus_num,unsigned long * mem_base,unsigned long * io_base,int primary_bus,pci_config_t * config)1599 static void ob_configure_pci_bridge(pci_addr addr,
1600                                     int *bus_num, unsigned long *mem_base,
1601                                     unsigned long *io_base,
1602                                     int primary_bus, pci_config_t *config)
1603 {
1604     unsigned long old_mem_base, old_io_base, io_scan_limit;
1605     uint16_t cmd;
1606     phandle_t ph;
1607 
1608     config->primary_bus = primary_bus;
1609     pci_config_write8(addr, PCI_PRIMARY_BUS, config->primary_bus);
1610 
1611     config->secondary_bus = *bus_num;
1612     pci_config_write8(addr, PCI_SECONDARY_BUS, config->secondary_bus);
1613 
1614     config->subordinate_bus = 0xff;
1615     pci_config_write8(addr, PCI_SUBORDINATE_BUS, config->subordinate_bus);
1616 
1617     PCI_DPRINTF("scanning new pci bus %u under bridge %s\n",
1618             config->secondary_bus, config->path);
1619 
1620     /* Temporarily add bus-range property to allow the secondary bus to
1621        determine its bus num */
1622     ph = find_dev(config->path);
1623     set_int_property(ph, "bus-range", *bus_num);
1624 
1625     /* Always expose the legacy ioports on the first PCI bridge. If we
1626        must have legacy devices behind a PCI bridge then they must be
1627        on the first one discovered to ensure that the ioports will work. */
1628     if (primary_bus > 0 && *io_base < 0x1000) {
1629         *io_base = 0x0;
1630     }
1631 
1632     /* Align mem_base up to nearest MB, io_base up to nearest 4K */
1633     old_mem_base = (*mem_base + 0xfffff - 1) & ~(0xfffff - 1);
1634     *mem_base = old_mem_base;
1635     old_io_base = (*io_base + 0xfff - 1) & ~(0xfff - 1);
1636     *io_base = old_io_base;
1637 
1638     /* Set the base limit registers */
1639     pci_config_write16(addr, PCI_MEMORY_BASE, ((*mem_base >> 16) & ~(0xf)));
1640     pci_config_write16(addr, PCI_IO_BASE_UPPER, (*io_base >> 16));
1641     pci_config_write8(addr, PCI_IO_BASE, ((*io_base >> 8) & ~(0xf)));
1642 
1643     /* Always ensure legacy ioports are accessible during enumeration.
1644        Some drivers (e.g. IDE) will attempt ioport access as part of
1645        the configuration process, so we allow them during the secondary
1646        bus scan and then set the correct IO limit below. */
1647     io_scan_limit = *io_base + 0xffff;
1648     pci_config_write16(addr, PCI_IO_LIMIT_UPPER, (io_scan_limit >> 16));
1649     pci_config_write8(addr, PCI_IO_LIMIT, (io_scan_limit >> 8) & ~(0xf));
1650 
1651     /* make pci bridge parent device, prepare for recursion */
1652 
1653 #if defined(CONFIG_SPARC64)
1654     /* Horrible hack for sabre */
1655     int vid = pci_config_read16(addr, PCI_VENDOR_ID);
1656     int did = pci_config_read16(addr, PCI_DEVICE_ID);
1657 
1658     if (vid == PCI_VENDOR_ID_SUN && did == PCI_DEVICE_ID_SUN_SABRE) {
1659         ob_scan_sabre_pci_bus(bus_num, mem_base, io_base,
1660                         config->path, config->secondary_bus);
1661     } else {
1662         ob_scan_pci_bus(bus_num, mem_base, io_base,
1663                         config->path, config->secondary_bus);
1664     }
1665 #else
1666     ob_scan_pci_bus(bus_num, mem_base, io_base,
1667                     config->path, config->secondary_bus);
1668 #endif
1669     /* bus scan updates *bus_num to last revealed pci bus number */
1670     config->subordinate_bus = *bus_num;
1671     pci_config_write8(addr, PCI_SUBORDINATE_BUS, config->subordinate_bus);
1672 
1673     PCI_DPRINTF("bridge %s PCI bus primary=%d secondary=%d subordinate=%d\n",
1674             config->path, config->primary_bus, config->secondary_bus,
1675             config->subordinate_bus);
1676 
1677     /* Align mem_base up to nearest MB, io_base up to nearest 4K */
1678     *mem_base = (*mem_base + 0xfffff - 1) & ~(0xfffff - 1);
1679     *io_base = (*io_base + 0xfff - 1) & ~(0xfff - 1);
1680 
1681     /* Set the limit registers */
1682     pci_config_write16(addr, PCI_MEMORY_LIMIT, (((*mem_base - 1) >> 16) & ~(0xf)));
1683     pci_config_write16(addr, PCI_IO_LIMIT_UPPER, ((*io_base - 1) >> 16));
1684     pci_config_write8(addr, PCI_IO_LIMIT, (((*io_base - 1) >> 8) & ~(0xf)));
1685 
1686     /* Disable unused address spaces */
1687     cmd = pci_config_read16(addr, PCI_COMMAND);
1688     if (*mem_base == old_mem_base) {
1689         pci_config_write16(addr, PCI_COMMAND, (cmd & ~PCI_COMMAND_MEMORY));
1690     }
1691 
1692     if (*io_base == old_io_base) {
1693         pci_config_write16(addr, PCI_COMMAND, (cmd & ~PCI_COMMAND_IO));
1694     }
1695 
1696     pci_set_bus_range(config);
1697 }
1698 
ob_pci_read_identification(int bus,int devnum,int fn,int * p_vid,int * p_did,uint8_t * p_class,uint8_t * p_subclass)1699 static int ob_pci_read_identification(int bus, int devnum, int fn,
1700                                  int *p_vid, int *p_did,
1701                                  uint8_t *p_class, uint8_t *p_subclass)
1702 {
1703     int vid, did;
1704     uint32_t ccode;
1705     pci_addr addr;
1706 
1707 #ifdef CONFIG_XBOX
1708     if (pci_xbox_ignore_device (bus, devnum, fn))
1709         return;
1710 #endif
1711     addr = PCI_ADDR(bus, devnum, fn);
1712     vid = pci_config_read16(addr, PCI_VENDOR_ID);
1713     did = pci_config_read16(addr, PCI_DEVICE_ID);
1714 
1715     if (vid==0xffff || vid==0) {
1716         return 0;
1717     }
1718 
1719     if (p_vid) {
1720         *p_vid = vid;
1721     }
1722 
1723     if (p_did) {
1724         *p_did = did;
1725     }
1726 
1727     ccode = pci_config_read16(addr, PCI_CLASS_DEVICE);
1728 
1729     if (p_class) {
1730         *p_class = ccode >> 8;
1731     }
1732 
1733     if (p_subclass) {
1734         *p_subclass = ccode;
1735     }
1736 
1737     return 1;
1738 }
1739 
ob_configure_pci_device(const char * parent_path,int * bus_num,unsigned long * mem_base,unsigned long * io_base,int bus,int devnum,int fn,int * p_is_multi)1740 static phandle_t ob_configure_pci_device(const char* parent_path,
1741         int *bus_num, unsigned long *mem_base, unsigned long *io_base,
1742         int bus, int devnum, int fn, int *p_is_multi)
1743 {
1744     int vid, did;
1745     unsigned int htype;
1746     pci_addr addr;
1747     pci_config_t config = {};
1748         const pci_dev_t *pci_dev;
1749     uint8_t class, subclass, iface;
1750     int num_bars, rom_bar;
1751     uint32_t props[3];
1752     char *unit_str;
1753 
1754     phandle_t phandle = 0, t;
1755     int is_host_bridge = 0;
1756 
1757     if (!ob_pci_read_identification(bus, devnum, fn, &vid, &did, &class, &subclass)) {
1758         return 0;
1759     }
1760 
1761     addr = PCI_ADDR(bus, devnum, fn);
1762     iface = pci_config_read8(addr, PCI_CLASS_PROG);
1763 
1764     pci_dev = pci_find_device(class, subclass, iface,
1765                   vid, did);
1766 
1767     PCI_DPRINTF("%x:%x.%x - %x:%x - ", bus, devnum, fn,
1768             vid, did);
1769 
1770     htype = pci_config_read8(addr, PCI_HEADER_TYPE);
1771 
1772     if (fn == 0) {
1773         if (p_is_multi) {
1774             *p_is_multi = htype & 0x80;
1775         }
1776     }
1777 
1778     if (class == PCI_BASE_CLASS_BRIDGE &&
1779             subclass == PCI_SUBCLASS_BRIDGE_HOST) {
1780         is_host_bridge = 1;
1781     }
1782 
1783     /* stop adding host bridge accessible from it's primary bus
1784        PCI host bridge is to be added by host code
1785     */
1786     if (is_host_bridge) {
1787         /* reuse device tree node */
1788         PCI_DPRINTF("host bridge found - ");
1789 
1790         t = find_dev(parent_path);
1791         if (get_property(t, "vendor-id", NULL)) {
1792             PCI_DPRINTF("host bridge already configured\n");
1793             return 0;
1794         }
1795     } else if (pci_dev == NULL || pci_dev->name == NULL) {
1796         snprintf(config.path, sizeof(config.path),
1797                 "%s/pci%x,%x", parent_path, vid, did);
1798     } else {
1799         snprintf(config.path, sizeof(config.path),
1800                 "%s/%s", parent_path, pci_dev->name);
1801     }
1802 
1803     fword("new-device");
1804 
1805     PCI_DPRINTF("%s - ", config.path);
1806 
1807     config.dev = addr & 0x00FFFFFF;
1808 
1809     if (!is_host_bridge) {
1810         /* Get encoded address for set-args */
1811         pci_encode_phys_addr(props, 0, CONFIGURATION_SPACE, config.dev, 0, 0);
1812         PUSH(props[2]);
1813         PUSH(props[1]);
1814         PUSH(props[0]);
1815         call_parent_method("encode-unit");
1816 
1817         unit_str = pop_fstr_copy();
1818 
1819         /* Call set-args to set up my-space and my-address */
1820         PUSH(0);
1821         PUSH(0);
1822         push_str(unit_str);
1823         fword("set-args");
1824 
1825         free(unit_str);
1826     }
1827 
1828     phandle = get_cur_dev();
1829 
1830     switch (class) {
1831     case PCI_BASE_CLASS_BRIDGE:
1832         if (subclass != PCI_SUBCLASS_BRIDGE_HOST) {
1833             BIND_NODE_METHODS(phandle, ob_pci_bridge_node);
1834         } else {
1835             BIND_NODE_METHODS(phandle, ob_pci_bus_node);
1836         }
1837         break;
1838     }
1839 
1840     if (htype & PCI_HEADER_TYPE_BRIDGE) {
1841         num_bars = 2;
1842         rom_bar  = PCI_ROM_ADDRESS1;
1843     } else {
1844         num_bars = 6;
1845         rom_bar  = PCI_ROM_ADDRESS;
1846     }
1847 
1848     ob_pci_configure(addr, &config, num_bars, rom_bar,
1849                      mem_base, io_base);
1850 
1851     ob_pci_add_properties(phandle, addr, pci_dev, &config, num_bars);
1852 
1853     if (!is_host_bridge) {
1854         pci_set_reg(phandle, &config, num_bars);
1855     } else {
1856         pci_host_set_reg(phandle, &config);
1857     }
1858 
1859     /* call device-specific configuration callback */
1860     if (pci_dev && pci_dev->config_cb) {
1861         //activate_device(config.path);
1862         pci_dev->config_cb(&config);
1863     }
1864 
1865     /* if devices haven't supplied open/close words then supply them with simple defaults */
1866     if (!find_package_method("open", phandle) && !find_package_method("close", phandle)) {
1867         BIND_NODE_METHODS(phandle, ob_pci_simple_node);
1868     }
1869 
1870     /* scan bus behind bridge device */
1871     //if (htype & PCI_HEADER_TYPE_BRIDGE && class == PCI_BASE_CLASS_BRIDGE) {
1872     if ( class == PCI_BASE_CLASS_BRIDGE &&
1873             ( subclass == PCI_SUBCLASS_BRIDGE_PCI ||
1874               subclass == PCI_SUBCLASS_BRIDGE_HOST ) ) {
1875 
1876         if (subclass == PCI_SUBCLASS_BRIDGE_PCI) {
1877             /* reserve next pci bus number for this PCI bridge */
1878             ++(*bus_num);
1879         }
1880 
1881         ob_configure_pci_bridge(addr, bus_num, mem_base, io_base, bus, &config);
1882     }
1883 
1884     fword("finish-device");
1885 
1886     return phandle;
1887 }
1888 
ob_pci_set_available(phandle_t host,unsigned long mem_base,unsigned long io_base)1889 static void ob_pci_set_available(phandle_t host, unsigned long mem_base, unsigned long io_base)
1890 {
1891     /* Create an available property for both memory and IO space */
1892     uint32_t props[10];
1893     int ncells;
1894 
1895     ncells = 0;
1896     ncells += pci_encode_phys_addr(props + ncells, 0, MEMORY_SPACE_32, 0, 0, mem_base);
1897     ncells += pci_encode_size(props + ncells, arch->mem_len - mem_base);
1898     ncells += pci_encode_phys_addr(props + ncells, 0, IO_SPACE, 0, 0, io_base);
1899     ncells += pci_encode_size(props + ncells, arch->io_len - io_base);
1900 
1901     set_property(host, "available", (char *)props, ncells * sizeof(props[0]));
1902 }
1903 
1904 #if defined(CONFIG_PPC)
ob_pci_host_set_interrupt_map(phandle_t host)1905 static phandle_t ob_pci_host_set_interrupt_map(phandle_t host)
1906 {
1907     /* Set the host bridge interrupt map, returning the phandle
1908        of the interrupt controller */
1909     phandle_t dnode, target_node;
1910     char *path, buf[256];
1911 
1912     /* Oldworld macs do interrupt maps differently */
1913     if (is_oldworld()) {
1914         return 0;
1915     }
1916 
1917     PCI_DPRINTF("setting up interrupt map for host %x\n", host);
1918     dnode = dt_iterate_type(0, "open-pic");
1919     path = get_path_from_ph(host);
1920     if (dnode && path) {
1921         /* patch in openpic interrupt-parent properties */
1922         snprintf(buf, sizeof(buf), "%s/mac-io", path);
1923         target_node = find_dev(buf);
1924         set_int_property(target_node, "interrupt-parent", dnode);
1925 
1926         snprintf(buf, sizeof(buf), "%s/mac-io/escc/ch-a", path);
1927         target_node = find_dev(buf);
1928         set_int_property(target_node, "interrupt-parent", dnode);
1929 
1930         snprintf(buf, sizeof(buf), "%s/mac-io/escc/ch-b", path);
1931         target_node = find_dev(buf);
1932         set_int_property(target_node, "interrupt-parent", dnode);
1933 
1934         snprintf(buf, sizeof(buf), "%s/mac-io/escc-legacy/ch-a", path);
1935         target_node = find_dev(buf);
1936         set_int_property(target_node, "interrupt-parent", dnode);
1937 
1938         snprintf(buf, sizeof(buf), "%s/mac-io/escc-legacy/ch-b", path);
1939         target_node = find_dev(buf);
1940         set_int_property(target_node, "interrupt-parent", dnode);
1941 
1942         /* QEMU only emulates 2 of the 3 ata buses currently */
1943         /* On a new world Mac these are not numbered but named by the
1944          * ATA version they support. Thus we have: ata-3, ata-3, ata-4
1945          * On g3beige they all called just ide.
1946          * We take 2 x ata-3 buses which seems to work for
1947          * at least the clients we care about */
1948         snprintf(buf, sizeof(buf), "%s/mac-io/ata-3@20000", path);
1949         target_node = find_dev(buf);
1950         set_int_property(target_node, "interrupt-parent", dnode);
1951 
1952         snprintf(buf, sizeof(buf), "%s/mac-io/ata-3@21000", path);
1953         target_node = find_dev(buf);
1954         set_int_property(target_node, "interrupt-parent", dnode);
1955 
1956         snprintf(buf, sizeof(buf), "%s/mac-io/via-cuda", path);
1957         target_node = find_dev(buf);
1958         set_int_property(target_node, "interrupt-parent", dnode);
1959 
1960         snprintf(buf, sizeof(buf), "%s/mac-io/via-pmu", path);
1961         target_node = find_dev(buf);
1962         if (target_node) {
1963             set_int_property(target_node, "interrupt-parent", dnode);
1964         }
1965 
1966         snprintf(buf, sizeof(buf), "%s/mac-io/gpio/extint-gpio1", path);
1967         target_node = find_dev(buf);
1968         if (target_node) {
1969             set_int_property(target_node, "interrupt-parent", dnode);
1970         }
1971 
1972         snprintf(buf, sizeof(buf), "%s/mac-io/gpio/programmer-switch", path);
1973         target_node = find_dev(buf);
1974         if (target_node) {
1975             set_int_property(target_node, "interrupt-parent", dnode);
1976         }
1977 
1978         target_node = find_dev(path);
1979         set_int_property(target_node, "interrupt-parent", dnode);
1980 
1981         return dnode;
1982     }
1983 
1984     return host;
1985 }
1986 
ob_pci_host_bus_interrupt(ucell dnode,u32 * props,int * ncells,u32 addr,u32 intno)1987 static void ob_pci_host_bus_interrupt(ucell dnode, u32 *props, int *ncells, u32 addr, u32 intno)
1988 {
1989     *ncells += pci_encode_phys_addr(props + *ncells, 0, 0, addr, 0, 0);
1990 
1991     props[(*ncells)++] = intno;
1992     props[(*ncells)++] = dnode;
1993     if (!is_apple() && (PCI_DEV(addr) == 1 && PCI_FN(addr) == 0)) {
1994         /* On PReP machine the LSI SCSI has fixed routing to IRQ 13 */
1995         props[(*ncells)++] = 13;
1996     } else {
1997         props[(*ncells)++] = arch->irqs[((intno - 1) + (addr >> 11)) & 3];
1998     }
1999     props[(*ncells)++] = 1;
2000 }
2001 
2002 #elif defined(CONFIG_SPARC64)
2003 
ob_pci_host_set_interrupt_map(phandle_t host)2004 static phandle_t ob_pci_host_set_interrupt_map(phandle_t host)
2005 {
2006     return host;
2007 }
2008 
ob_pci_host_bus_interrupt(ucell dnode,u32 * props,int * ncells,u32 addr,u32 intno)2009 static void ob_pci_host_bus_interrupt(ucell dnode, u32 *props, int *ncells, u32 addr, u32 intno)
2010 {
2011     /* Note: this is invalid when the Simba bridges are in place
2012        as it is impossible to physically plug hardware into the PCI
2013        root bus (and no hardware support for mapping these interrupts
2014        either) */
2015     return;
2016 }
2017 
2018 #else
2019 
ob_pci_host_set_interrupt_map(phandle_t host)2020 static phandle_t ob_pci_host_set_interrupt_map(phandle_t host)
2021 {
2022     return host;
2023 }
2024 
ob_pci_host_bus_interrupt(ucell dnode,u32 * props,int * ncells,u32 addr,u32 intno)2025 static void ob_pci_host_bus_interrupt(ucell dnode, u32 *props, int *ncells, u32 addr, u32 intno)
2026 {
2027     return;
2028 }
2029 #endif
2030 
ob_pci_bus_set_interrupt_map(phandle_t pcibus,phandle_t dnode,void (* func)(ucell dnode,u32 * props,int * ncells,u32 addr,u32 intno))2031 static void ob_pci_bus_set_interrupt_map(phandle_t pcibus, phandle_t dnode,
2032               void (*func)(ucell dnode, u32 *props, int *ncells, u32 addr, u32 intno))
2033 {
2034     /* Set interrupt-map for PCI devices with an interrupt pin present */
2035     phandle_t pci_childnode = 0;
2036     u32 props[128], intno;
2037     int i, ncells = 0, len;
2038     u32 *val, addr;
2039     char *reg;
2040 
2041     /* If no destination node specified, do nothing */
2042     if (dnode == 0) {
2043         return;
2044     }
2045 
2046     PUSH(pcibus);
2047     fword("child");
2048     pci_childnode = POP();
2049     while (pci_childnode) {
2050         intno = get_int_property(pci_childnode, "interrupts", &len);
2051         if (len && intno) {
2052             reg = get_property(pci_childnode, "reg", &len);
2053             if (len && reg) {
2054                 val = (u32 *)reg;
2055 
2056                 for (i = 0; i < (len / sizeof(u32)); i += 5) {
2057                     addr = val[i];
2058 
2059                     /* Device address is in 1st 32-bit word of encoded PCI address for config space */
2060                     if ((addr & PCI_RANGE_TYPE_MASK) == PCI_RANGE_CONFIG) {
2061                         (*func)(dnode, props, &ncells, addr, intno);
2062                     }
2063                 }
2064             }
2065         }
2066 
2067         PUSH(pci_childnode);
2068         fword("peer");
2069         pci_childnode = POP();
2070     }
2071 
2072     if (ncells) {
2073         set_property(pcibus, "interrupt-map", (char *)props, ncells * sizeof(props[0]));
2074 
2075         props[0] = 0x00fff800;
2076         props[1] = 0x0;
2077         props[2] = 0x0;
2078         props[3] = 0x7;
2079         set_property(pcibus, "interrupt-map-mask", (char *)props, 4 * sizeof(props[0]));
2080     }
2081 }
2082 
ob_pci_init(void)2083 int ob_pci_init(void)
2084 {
2085     int bus, devnum, fn;
2086     uint8_t class, subclass;
2087     unsigned long mem_base, io_base;
2088 
2089     pci_config_t config = {}; /* host bridge */
2090     phandle_t phandle_host = 0, intc;
2091 
2092     PCI_DPRINTF("Initializing PCI host bridge...\n");
2093 
2094     /* Find all PCI bridges */
2095 
2096     mem_base = arch->pci_mem_base;
2097     /* I/O ports under 0x400 are used by devices mapped at fixed
2098        location. */
2099     io_base = 0x400;
2100 
2101     bus = 0;
2102 
2103     for (devnum = 0; devnum < 32; devnum++) {
2104         /* scan only fn 0 */
2105         fn = 0;
2106 
2107         if (!ob_pci_read_identification(bus, devnum, fn,
2108                                         0, 0, &class, &subclass)) {
2109             continue;
2110         }
2111 
2112         if (class != PCI_BASE_CLASS_BRIDGE || subclass != PCI_SUBCLASS_BRIDGE_HOST) {
2113             continue;
2114         }
2115 
2116         /* create root node for host PCI bridge */
2117 
2118         /* configure  */
2119         strncpy(config.path, "", sizeof(config.path));
2120 
2121         phandle_host = ob_configure_pci_device(config.path, &bus, &mem_base, &io_base,
2122                 bus, devnum, fn, 0);
2123 
2124         /* we expect single host PCI bridge
2125            but this may be machine-specific */
2126         break;
2127     }
2128 
2129     /* create available attributes for the PCI bridge */
2130     ob_pci_set_available(phandle_host, mem_base, io_base);
2131 
2132     /* configure the host bridge interrupt map */
2133     intc = ob_pci_host_set_interrupt_map(phandle_host);
2134     ob_pci_bus_set_interrupt_map(phandle_host, intc, ob_pci_host_bus_interrupt);
2135 
2136     return 0;
2137 }
2138