xref: /openbmc/u-boot/drivers/pci/pci-uclass.c (revision 9c71a21d)
1 /*
2  * Copyright (c) 2014 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <fdtdec.h>
12 #include <inttypes.h>
13 #include <pci.h>
14 #include <dm/lists.h>
15 #include <dm/root.h>
16 #include <dm/device-internal.h>
17 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
18 #include <asm/fsp/fsp_support.h>
19 #endif
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 static int pci_get_bus(int busnum, struct udevice **busp)
24 {
25 	int ret;
26 
27 	ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
28 
29 	/* Since buses may not be numbered yet try a little harder with bus 0 */
30 	if (ret == -ENODEV) {
31 		ret = uclass_first_device(UCLASS_PCI, busp);
32 		if (ret)
33 			return ret;
34 		else if (!*busp)
35 			return -ENODEV;
36 		ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
37 	}
38 
39 	return ret;
40 }
41 
42 struct pci_controller *pci_bus_to_hose(int busnum)
43 {
44 	struct udevice *bus;
45 	int ret;
46 
47 	ret = pci_get_bus(busnum, &bus);
48 	if (ret) {
49 		debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
50 		return NULL;
51 	}
52 
53 	return dev_get_uclass_priv(bus);
54 }
55 
56 pci_dev_t pci_get_bdf(struct udevice *dev)
57 {
58 	struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
59 	struct udevice *bus = dev->parent;
60 
61 	return PCI_ADD_BUS(bus->seq, pplat->devfn);
62 }
63 
64 /**
65  * pci_get_bus_max() - returns the bus number of the last active bus
66  *
67  * @return last bus number, or -1 if no active buses
68  */
69 static int pci_get_bus_max(void)
70 {
71 	struct udevice *bus;
72 	struct uclass *uc;
73 	int ret = -1;
74 
75 	ret = uclass_get(UCLASS_PCI, &uc);
76 	uclass_foreach_dev(bus, uc) {
77 		if (bus->seq > ret)
78 			ret = bus->seq;
79 	}
80 
81 	debug("%s: ret=%d\n", __func__, ret);
82 
83 	return ret;
84 }
85 
86 int pci_last_busno(void)
87 {
88 	struct pci_controller *hose;
89 	struct udevice *bus;
90 	struct uclass *uc;
91 	int ret;
92 
93 	debug("pci_last_busno\n");
94 	ret = uclass_get(UCLASS_PCI, &uc);
95 	if (ret || list_empty(&uc->dev_head))
96 		return -1;
97 
98 	/* Probe the last bus */
99 	bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
100 	debug("bus = %p, %s\n", bus, bus->name);
101 	assert(bus);
102 	ret = device_probe(bus);
103 	if (ret)
104 		return ret;
105 
106 	/* If that bus has bridges, we may have new buses now. Get the last */
107 	bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
108 	hose = dev_get_uclass_priv(bus);
109 	debug("bus = %s, hose = %p\n", bus->name, hose);
110 
111 	return hose->last_busno;
112 }
113 
114 int pci_get_ff(enum pci_size_t size)
115 {
116 	switch (size) {
117 	case PCI_SIZE_8:
118 		return 0xff;
119 	case PCI_SIZE_16:
120 		return 0xffff;
121 	default:
122 		return 0xffffffff;
123 	}
124 }
125 
126 int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
127 		       struct udevice **devp)
128 {
129 	struct udevice *dev;
130 
131 	for (device_find_first_child(bus, &dev);
132 	     dev;
133 	     device_find_next_child(&dev)) {
134 		struct pci_child_platdata *pplat;
135 
136 		pplat = dev_get_parent_platdata(dev);
137 		if (pplat && pplat->devfn == find_devfn) {
138 			*devp = dev;
139 			return 0;
140 		}
141 	}
142 
143 	return -ENODEV;
144 }
145 
146 int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
147 {
148 	struct udevice *bus;
149 	int ret;
150 
151 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
152 	if (ret)
153 		return ret;
154 	return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
155 }
156 
157 static int pci_device_matches_ids(struct udevice *dev,
158 				  struct pci_device_id *ids)
159 {
160 	struct pci_child_platdata *pplat;
161 	int i;
162 
163 	pplat = dev_get_parent_platdata(dev);
164 	if (!pplat)
165 		return -EINVAL;
166 	for (i = 0; ids[i].vendor != 0; i++) {
167 		if (pplat->vendor == ids[i].vendor &&
168 		    pplat->device == ids[i].device)
169 			return i;
170 	}
171 
172 	return -EINVAL;
173 }
174 
175 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
176 			 int *indexp, struct udevice **devp)
177 {
178 	struct udevice *dev;
179 
180 	/* Scan all devices on this bus */
181 	for (device_find_first_child(bus, &dev);
182 	     dev;
183 	     device_find_next_child(&dev)) {
184 		if (pci_device_matches_ids(dev, ids) >= 0) {
185 			if ((*indexp)-- <= 0) {
186 				*devp = dev;
187 				return 0;
188 			}
189 		}
190 	}
191 
192 	return -ENODEV;
193 }
194 
195 int pci_find_device_id(struct pci_device_id *ids, int index,
196 		       struct udevice **devp)
197 {
198 	struct udevice *bus;
199 
200 	/* Scan all known buses */
201 	for (uclass_first_device(UCLASS_PCI, &bus);
202 	     bus;
203 	     uclass_next_device(&bus)) {
204 		if (!pci_bus_find_devices(bus, ids, &index, devp))
205 			return 0;
206 	}
207 	*devp = NULL;
208 
209 	return -ENODEV;
210 }
211 
212 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
213 			 unsigned long value, enum pci_size_t size)
214 {
215 	struct dm_pci_ops *ops;
216 
217 	ops = pci_get_ops(bus);
218 	if (!ops->write_config)
219 		return -ENOSYS;
220 	return ops->write_config(bus, bdf, offset, value, size);
221 }
222 
223 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
224 		     enum pci_size_t size)
225 {
226 	struct udevice *bus;
227 	int ret;
228 
229 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
230 	if (ret)
231 		return ret;
232 
233 	return pci_bus_write_config(bus, bdf, offset, value, size);
234 }
235 
236 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
237 			enum pci_size_t size)
238 {
239 	struct udevice *bus;
240 
241 	for (bus = dev; device_is_on_pci_bus(bus);)
242 		bus = bus->parent;
243 	return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
244 }
245 
246 
247 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
248 {
249 	return pci_write_config(bdf, offset, value, PCI_SIZE_32);
250 }
251 
252 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
253 {
254 	return pci_write_config(bdf, offset, value, PCI_SIZE_16);
255 }
256 
257 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
258 {
259 	return pci_write_config(bdf, offset, value, PCI_SIZE_8);
260 }
261 
262 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
263 {
264 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
265 }
266 
267 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
268 {
269 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
270 }
271 
272 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
273 {
274 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
275 }
276 
277 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
278 			unsigned long *valuep, enum pci_size_t size)
279 {
280 	struct dm_pci_ops *ops;
281 
282 	ops = pci_get_ops(bus);
283 	if (!ops->read_config)
284 		return -ENOSYS;
285 	return ops->read_config(bus, bdf, offset, valuep, size);
286 }
287 
288 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
289 		    enum pci_size_t size)
290 {
291 	struct udevice *bus;
292 	int ret;
293 
294 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
295 	if (ret)
296 		return ret;
297 
298 	return pci_bus_read_config(bus, bdf, offset, valuep, size);
299 }
300 
301 int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
302 		       enum pci_size_t size)
303 {
304 	struct udevice *bus;
305 
306 	for (bus = dev; device_is_on_pci_bus(bus);)
307 		bus = bus->parent;
308 	return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
309 				   size);
310 }
311 
312 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
313 {
314 	unsigned long value;
315 	int ret;
316 
317 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
318 	if (ret)
319 		return ret;
320 	*valuep = value;
321 
322 	return 0;
323 }
324 
325 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
326 {
327 	unsigned long value;
328 	int ret;
329 
330 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
331 	if (ret)
332 		return ret;
333 	*valuep = value;
334 
335 	return 0;
336 }
337 
338 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
339 {
340 	unsigned long value;
341 	int ret;
342 
343 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
344 	if (ret)
345 		return ret;
346 	*valuep = value;
347 
348 	return 0;
349 }
350 
351 int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
352 {
353 	unsigned long value;
354 	int ret;
355 
356 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
357 	if (ret)
358 		return ret;
359 	*valuep = value;
360 
361 	return 0;
362 }
363 
364 int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
365 {
366 	unsigned long value;
367 	int ret;
368 
369 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
370 	if (ret)
371 		return ret;
372 	*valuep = value;
373 
374 	return 0;
375 }
376 
377 int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
378 {
379 	unsigned long value;
380 	int ret;
381 
382 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
383 	if (ret)
384 		return ret;
385 	*valuep = value;
386 
387 	return 0;
388 }
389 
390 int pci_auto_config_devices(struct udevice *bus)
391 {
392 	struct pci_controller *hose = bus->uclass_priv;
393 	unsigned int sub_bus;
394 	struct udevice *dev;
395 	int ret;
396 
397 	sub_bus = bus->seq;
398 	debug("%s: start\n", __func__);
399 	pciauto_config_init(hose);
400 	for (ret = device_find_first_child(bus, &dev);
401 	     !ret && dev;
402 	     ret = device_find_next_child(&dev)) {
403 		unsigned int max_bus;
404 
405 		debug("%s: device %s\n", __func__, dev->name);
406 		max_bus = pciauto_config_device(hose, pci_get_bdf(dev));
407 		sub_bus = max(sub_bus, max_bus);
408 	}
409 	debug("%s: done\n", __func__);
410 
411 	return sub_bus;
412 }
413 
414 int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
415 {
416 	struct udevice *parent, *bus;
417 	int sub_bus;
418 	int ret;
419 
420 	debug("%s\n", __func__);
421 	parent = hose->bus;
422 
423 	/* Find the bus within the parent */
424 	ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
425 	if (ret) {
426 		debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
427 		      bdf, parent->name, ret);
428 		return ret;
429 	}
430 
431 	sub_bus = pci_get_bus_max() + 1;
432 	debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
433 	pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
434 
435 	ret = device_probe(bus);
436 	if (ret) {
437 		debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
438 		      ret);
439 		return ret;
440 	}
441 	if (sub_bus != bus->seq) {
442 		printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
443 		       __func__, bus->name, bus->seq, sub_bus);
444 		return -EPIPE;
445 	}
446 	sub_bus = pci_get_bus_max();
447 	pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
448 
449 	return sub_bus;
450 }
451 
452 /**
453  * pci_match_one_device - Tell if a PCI device structure has a matching
454  *                        PCI device id structure
455  * @id: single PCI device id structure to match
456  * @dev: the PCI device structure to match against
457  *
458  * Returns the matching pci_device_id structure or %NULL if there is no match.
459  */
460 static bool pci_match_one_id(const struct pci_device_id *id,
461 			     const struct pci_device_id *find)
462 {
463 	if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
464 	    (id->device == PCI_ANY_ID || id->device == find->device) &&
465 	    (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
466 	    (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
467 	    !((id->class ^ find->class) & id->class_mask))
468 		return true;
469 
470 	return false;
471 }
472 
473 /**
474  * pci_find_and_bind_driver() - Find and bind the right PCI driver
475  *
476  * This only looks at certain fields in the descriptor.
477  */
478 static int pci_find_and_bind_driver(struct udevice *parent,
479 				    struct pci_device_id *find_id, pci_dev_t bdf,
480 				    struct udevice **devp)
481 {
482 	struct pci_driver_entry *start, *entry;
483 	const char *drv;
484 	int n_ents;
485 	int ret;
486 	char name[30], *str;
487 	bool bridge;
488 
489 	*devp = NULL;
490 
491 	debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
492 	      find_id->vendor, find_id->device);
493 	start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
494 	n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
495 	for (entry = start; entry != start + n_ents; entry++) {
496 		const struct pci_device_id *id;
497 		struct udevice *dev;
498 		const struct driver *drv;
499 
500 		for (id = entry->match;
501 		     id->vendor || id->subvendor || id->class_mask;
502 		     id++) {
503 			if (!pci_match_one_id(id, find_id))
504 				continue;
505 
506 			drv = entry->driver;
507 
508 			/*
509 			 * In the pre-relocation phase, we only bind devices
510 			 * whose driver has the DM_FLAG_PRE_RELOC set, to save
511 			 * precious memory space as on some platforms as that
512 			 * space is pretty limited (ie: using Cache As RAM).
513 			 */
514 			if (!(gd->flags & GD_FLG_RELOC) &&
515 			    !(drv->flags & DM_FLAG_PRE_RELOC))
516 				return 0;
517 
518 			/*
519 			 * We could pass the descriptor to the driver as
520 			 * platdata (instead of NULL) and allow its bind()
521 			 * method to return -ENOENT if it doesn't support this
522 			 * device. That way we could continue the search to
523 			 * find another driver. For now this doesn't seem
524 			 * necesssary, so just bind the first match.
525 			 */
526 			ret = device_bind(parent, drv, drv->name, NULL, -1,
527 					  &dev);
528 			if (ret)
529 				goto error;
530 			debug("%s: Match found: %s\n", __func__, drv->name);
531 			dev->driver_data = find_id->driver_data;
532 			*devp = dev;
533 			return 0;
534 		}
535 	}
536 
537 	bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
538 	/*
539 	 * In the pre-relocation phase, we only bind bridge devices to save
540 	 * precious memory space as on some platforms as that space is pretty
541 	 * limited (ie: using Cache As RAM).
542 	 */
543 	if (!(gd->flags & GD_FLG_RELOC) && !bridge)
544 		return 0;
545 
546 	/* Bind a generic driver so that the device can be used */
547 	sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
548 		PCI_FUNC(bdf));
549 	str = strdup(name);
550 	if (!str)
551 		return -ENOMEM;
552 	drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
553 
554 	ret = device_bind_driver(parent, drv, str, devp);
555 	if (ret) {
556 		debug("%s: Failed to bind generic driver: %d", __func__, ret);
557 		return ret;
558 	}
559 	debug("%s: No match found: bound generic driver instead\n", __func__);
560 
561 	return 0;
562 
563 error:
564 	debug("%s: No match found: error %d\n", __func__, ret);
565 	return ret;
566 }
567 
568 int pci_bind_bus_devices(struct udevice *bus)
569 {
570 	ulong vendor, device;
571 	ulong header_type;
572 	pci_dev_t bdf, end;
573 	bool found_multi;
574 	int ret;
575 
576 	found_multi = false;
577 	end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
578 		      PCI_MAX_PCI_FUNCTIONS - 1);
579 	for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
580 	     bdf += PCI_BDF(0, 0, 1)) {
581 		struct pci_child_platdata *pplat;
582 		struct udevice *dev;
583 		ulong class;
584 
585 		if (PCI_FUNC(bdf) && !found_multi)
586 			continue;
587 		/* Check only the first access, we don't expect problems */
588 		ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
589 					  &header_type, PCI_SIZE_8);
590 		if (ret)
591 			goto error;
592 		pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
593 				    PCI_SIZE_16);
594 		if (vendor == 0xffff || vendor == 0x0000)
595 			continue;
596 
597 		if (!PCI_FUNC(bdf))
598 			found_multi = header_type & 0x80;
599 
600 		debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
601 		      bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
602 		pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
603 				    PCI_SIZE_16);
604 		pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
605 				    PCI_SIZE_32);
606 		class >>= 8;
607 
608 		/* Find this device in the device tree */
609 		ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
610 
611 		/* Search for a driver */
612 
613 		/* If nothing in the device tree, bind a generic device */
614 		if (ret == -ENODEV) {
615 			struct pci_device_id find_id;
616 			ulong val;
617 
618 			memset(&find_id, '\0', sizeof(find_id));
619 			find_id.vendor = vendor;
620 			find_id.device = device;
621 			find_id.class = class;
622 			if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
623 				pci_bus_read_config(bus, bdf,
624 						    PCI_SUBSYSTEM_VENDOR_ID,
625 						    &val, PCI_SIZE_32);
626 				find_id.subvendor = val & 0xffff;
627 				find_id.subdevice = val >> 16;
628 			}
629 			ret = pci_find_and_bind_driver(bus, &find_id, bdf,
630 						       &dev);
631 		}
632 		if (ret)
633 			return ret;
634 
635 		/* Update the platform data */
636 		if (dev) {
637 			pplat = dev_get_parent_platdata(dev);
638 			pplat->devfn = PCI_MASK_BUS(bdf);
639 			pplat->vendor = vendor;
640 			pplat->device = device;
641 			pplat->class = class;
642 		}
643 	}
644 
645 	return 0;
646 error:
647 	printf("Cannot read bus configuration: %d\n", ret);
648 
649 	return ret;
650 }
651 
652 static int pci_uclass_post_bind(struct udevice *bus)
653 {
654 	/*
655 	 * If there is no pci device listed in the device tree,
656 	 * don't bother scanning the device tree.
657 	 */
658 	if (bus->of_offset == -1)
659 		return 0;
660 
661 	/*
662 	 * Scan the device tree for devices. This does not probe the PCI bus,
663 	 * as this is not permitted while binding. It just finds devices
664 	 * mentioned in the device tree.
665 	 *
666 	 * Before relocation, only bind devices marked for pre-relocation
667 	 * use.
668 	 */
669 	return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
670 				gd->flags & GD_FLG_RELOC ? false : true);
671 }
672 
673 static int decode_regions(struct pci_controller *hose, const void *blob,
674 			  int parent_node, int node)
675 {
676 	int pci_addr_cells, addr_cells, size_cells;
677 	int cells_per_record;
678 	phys_addr_t addr;
679 	const u32 *prop;
680 	int len;
681 	int i;
682 
683 	prop = fdt_getprop(blob, node, "ranges", &len);
684 	if (!prop)
685 		return -EINVAL;
686 	pci_addr_cells = fdt_address_cells(blob, node);
687 	addr_cells = fdt_address_cells(blob, parent_node);
688 	size_cells = fdt_size_cells(blob, node);
689 
690 	/* PCI addresses are always 3-cells */
691 	len /= sizeof(u32);
692 	cells_per_record = pci_addr_cells + addr_cells + size_cells;
693 	hose->region_count = 0;
694 	debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
695 	      cells_per_record);
696 	for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
697 		u64 pci_addr, addr, size;
698 		int space_code;
699 		u32 flags;
700 		int type;
701 
702 		if (len < cells_per_record)
703 			break;
704 		flags = fdt32_to_cpu(prop[0]);
705 		space_code = (flags >> 24) & 3;
706 		pci_addr = fdtdec_get_number(prop + 1, 2);
707 		prop += pci_addr_cells;
708 		addr = fdtdec_get_number(prop, addr_cells);
709 		prop += addr_cells;
710 		size = fdtdec_get_number(prop, size_cells);
711 		prop += size_cells;
712 		debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
713 		      ", size=%" PRIx64 ", space_code=%d\n", __func__,
714 		      hose->region_count, pci_addr, addr, size, space_code);
715 		if (space_code & 2) {
716 			type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
717 					PCI_REGION_MEM;
718 		} else if (space_code & 1) {
719 			type = PCI_REGION_IO;
720 		} else {
721 			continue;
722 		}
723 		debug(" - type=%d\n", type);
724 		pci_set_region(hose->regions + hose->region_count++, pci_addr,
725 			       addr, size, type);
726 	}
727 
728 	/* Add a region for our local memory */
729 	addr = gd->ram_size;
730 	if (gd->pci_ram_top && gd->pci_ram_top < addr)
731 		addr = gd->pci_ram_top;
732 	pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
733 		       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
734 
735 	return 0;
736 }
737 
738 static int pci_uclass_pre_probe(struct udevice *bus)
739 {
740 	struct pci_controller *hose;
741 	int ret;
742 
743 	debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
744 	      bus->parent->name);
745 	hose = bus->uclass_priv;
746 
747 	/* For bridges, use the top-level PCI controller */
748 	if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
749 		hose->ctlr = bus;
750 		ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
751 				bus->of_offset);
752 		if (ret) {
753 			debug("%s: Cannot decode regions\n", __func__);
754 			return ret;
755 		}
756 	} else {
757 		struct pci_controller *parent_hose;
758 
759 		parent_hose = dev_get_uclass_priv(bus->parent);
760 		hose->ctlr = parent_hose->bus;
761 	}
762 	hose->bus = bus;
763 	hose->first_busno = bus->seq;
764 	hose->last_busno = bus->seq;
765 
766 	return 0;
767 }
768 
769 static int pci_uclass_post_probe(struct udevice *bus)
770 {
771 	int ret;
772 
773 	debug("%s: probing bus %d\n", __func__, bus->seq);
774 	ret = pci_bind_bus_devices(bus);
775 	if (ret)
776 		return ret;
777 
778 #ifdef CONFIG_PCI_PNP
779 	ret = pci_auto_config_devices(bus);
780 #endif
781 
782 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
783 	/*
784 	 * Per Intel FSP specification, we should call FSP notify API to
785 	 * inform FSP that PCI enumeration has been done so that FSP will
786 	 * do any necessary initialization as required by the chipset's
787 	 * BIOS Writer's Guide (BWG).
788 	 *
789 	 * Unfortunately we have to put this call here as with driver model,
790 	 * the enumeration is all done on a lazy basis as needed, so until
791 	 * something is touched on PCI it won't happen.
792 	 *
793 	 * Note we only call this 1) after U-Boot is relocated, and 2)
794 	 * root bus has finished probing.
795 	 */
796 	if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0))
797 		ret = fsp_init_phase_pci();
798 #endif
799 
800 	return ret < 0 ? ret : 0;
801 }
802 
803 static int pci_uclass_child_post_bind(struct udevice *dev)
804 {
805 	struct pci_child_platdata *pplat;
806 	struct fdt_pci_addr addr;
807 	int ret;
808 
809 	if (dev->of_offset == -1)
810 		return 0;
811 
812 	/*
813 	 * We could read vendor, device, class if available. But for now we
814 	 * just check the address.
815 	 */
816 	pplat = dev_get_parent_platdata(dev);
817 	ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
818 				  FDT_PCI_SPACE_CONFIG, "reg", &addr);
819 
820 	if (ret) {
821 		if (ret != -ENOENT)
822 			return -EINVAL;
823 	} else {
824 		/* extract the devfn from fdt_pci_addr */
825 		pplat->devfn = addr.phys_hi & 0xff00;
826 	}
827 
828 	return 0;
829 }
830 
831 static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
832 				  uint offset, ulong *valuep,
833 				  enum pci_size_t size)
834 {
835 	struct pci_controller *hose = bus->uclass_priv;
836 
837 	return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
838 }
839 
840 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
841 				   uint offset, ulong value,
842 				   enum pci_size_t size)
843 {
844 	struct pci_controller *hose = bus->uclass_priv;
845 
846 	return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
847 }
848 
849 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
850 {
851 	struct udevice *dev;
852 	int ret = 0;
853 
854 	/*
855 	 * Scan through all the PCI controllers. On x86 there will only be one
856 	 * but that is not necessarily true on other hardware.
857 	 */
858 	do {
859 		device_find_first_child(bus, &dev);
860 		if (dev) {
861 			*devp = dev;
862 			return 0;
863 		}
864 		ret = uclass_next_device(&bus);
865 		if (ret)
866 			return ret;
867 	} while (bus);
868 
869 	return 0;
870 }
871 
872 int pci_find_next_device(struct udevice **devp)
873 {
874 	struct udevice *child = *devp;
875 	struct udevice *bus = child->parent;
876 	int ret;
877 
878 	/* First try all the siblings */
879 	*devp = NULL;
880 	while (child) {
881 		device_find_next_child(&child);
882 		if (child) {
883 			*devp = child;
884 			return 0;
885 		}
886 	}
887 
888 	/* We ran out of siblings. Try the next bus */
889 	ret = uclass_next_device(&bus);
890 	if (ret)
891 		return ret;
892 
893 	return bus ? skip_to_next_device(bus, devp) : 0;
894 }
895 
896 int pci_find_first_device(struct udevice **devp)
897 {
898 	struct udevice *bus;
899 	int ret;
900 
901 	*devp = NULL;
902 	ret = uclass_first_device(UCLASS_PCI, &bus);
903 	if (ret)
904 		return ret;
905 
906 	return skip_to_next_device(bus, devp);
907 }
908 
909 UCLASS_DRIVER(pci) = {
910 	.id		= UCLASS_PCI,
911 	.name		= "pci",
912 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
913 	.post_bind	= pci_uclass_post_bind,
914 	.pre_probe	= pci_uclass_pre_probe,
915 	.post_probe	= pci_uclass_post_probe,
916 	.child_post_bind = pci_uclass_child_post_bind,
917 	.per_device_auto_alloc_size = sizeof(struct pci_controller),
918 	.per_child_platdata_auto_alloc_size =
919 			sizeof(struct pci_child_platdata),
920 };
921 
922 static const struct dm_pci_ops pci_bridge_ops = {
923 	.read_config	= pci_bridge_read_config,
924 	.write_config	= pci_bridge_write_config,
925 };
926 
927 static const struct udevice_id pci_bridge_ids[] = {
928 	{ .compatible = "pci-bridge" },
929 	{ }
930 };
931 
932 U_BOOT_DRIVER(pci_bridge_drv) = {
933 	.name		= "pci_bridge_drv",
934 	.id		= UCLASS_PCI,
935 	.of_match	= pci_bridge_ids,
936 	.ops		= &pci_bridge_ops,
937 };
938 
939 UCLASS_DRIVER(pci_generic) = {
940 	.id		= UCLASS_PCI_GENERIC,
941 	.name		= "pci_generic",
942 };
943 
944 static const struct udevice_id pci_generic_ids[] = {
945 	{ .compatible = "pci-generic" },
946 	{ }
947 };
948 
949 U_BOOT_DRIVER(pci_generic_drv) = {
950 	.name		= "pci_generic_drv",
951 	.id		= UCLASS_PCI_GENERIC,
952 	.of_match	= pci_generic_ids,
953 };
954