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