• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <pci.h>
11 #include <asm/io.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
15 #include <asm/fsp/fsp_support.h>
16 #endif
17 #include "pci_internal.h"
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
pci_get_bus(int busnum,struct udevice ** busp)21 int pci_get_bus(int busnum, struct udevice **busp)
22 {
23 	int ret;
24 
25 	ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
26 
27 	/* Since buses may not be numbered yet try a little harder with bus 0 */
28 	if (ret == -ENODEV) {
29 		ret = uclass_first_device_err(UCLASS_PCI, busp);
30 		if (ret)
31 			return ret;
32 		ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
33 	}
34 
35 	return ret;
36 }
37 
pci_get_controller(struct udevice * dev)38 struct udevice *pci_get_controller(struct udevice *dev)
39 {
40 	while (device_is_on_pci_bus(dev))
41 		dev = dev->parent;
42 
43 	return dev;
44 }
45 
dm_pci_get_bdf(struct udevice * dev)46 pci_dev_t dm_pci_get_bdf(struct udevice *dev)
47 {
48 	struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
49 	struct udevice *bus = dev->parent;
50 
51 	return PCI_ADD_BUS(bus->seq, pplat->devfn);
52 }
53 
54 /**
55  * pci_get_bus_max() - returns the bus number of the last active bus
56  *
57  * @return last bus number, or -1 if no active buses
58  */
pci_get_bus_max(void)59 static int pci_get_bus_max(void)
60 {
61 	struct udevice *bus;
62 	struct uclass *uc;
63 	int ret = -1;
64 
65 	ret = uclass_get(UCLASS_PCI, &uc);
66 	uclass_foreach_dev(bus, uc) {
67 		if (bus->seq > ret)
68 			ret = bus->seq;
69 	}
70 
71 	debug("%s: ret=%d\n", __func__, ret);
72 
73 	return ret;
74 }
75 
pci_last_busno(void)76 int pci_last_busno(void)
77 {
78 	return pci_get_bus_max();
79 }
80 
pci_get_ff(enum pci_size_t size)81 int pci_get_ff(enum pci_size_t size)
82 {
83 	switch (size) {
84 	case PCI_SIZE_8:
85 		return 0xff;
86 	case PCI_SIZE_16:
87 		return 0xffff;
88 	default:
89 		return 0xffffffff;
90 	}
91 }
92 
pci_dev_find_ofnode(struct udevice * bus,phys_addr_t bdf,ofnode * rnode)93 static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
94 				ofnode *rnode)
95 {
96 	struct fdt_pci_addr addr;
97 	ofnode node;
98 	int ret;
99 
100 	dev_for_each_subnode(node, bus) {
101 		ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
102 					   &addr);
103 		if (ret)
104 			continue;
105 
106 		if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
107 			continue;
108 
109 		*rnode = node;
110 		break;
111 	}
112 };
113 
pci_bus_find_devfn(struct udevice * bus,pci_dev_t find_devfn,struct udevice ** devp)114 int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
115 		       struct udevice **devp)
116 {
117 	struct udevice *dev;
118 
119 	for (device_find_first_child(bus, &dev);
120 	     dev;
121 	     device_find_next_child(&dev)) {
122 		struct pci_child_platdata *pplat;
123 
124 		pplat = dev_get_parent_platdata(dev);
125 		if (pplat && pplat->devfn == find_devfn) {
126 			*devp = dev;
127 			return 0;
128 		}
129 	}
130 
131 	return -ENODEV;
132 }
133 
dm_pci_bus_find_bdf(pci_dev_t bdf,struct udevice ** devp)134 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
135 {
136 	struct udevice *bus;
137 	int ret;
138 
139 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
140 	if (ret)
141 		return ret;
142 	return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
143 }
144 
pci_device_matches_ids(struct udevice * dev,struct pci_device_id * ids)145 static int pci_device_matches_ids(struct udevice *dev,
146 				  struct pci_device_id *ids)
147 {
148 	struct pci_child_platdata *pplat;
149 	int i;
150 
151 	pplat = dev_get_parent_platdata(dev);
152 	if (!pplat)
153 		return -EINVAL;
154 	for (i = 0; ids[i].vendor != 0; i++) {
155 		if (pplat->vendor == ids[i].vendor &&
156 		    pplat->device == ids[i].device)
157 			return i;
158 	}
159 
160 	return -EINVAL;
161 }
162 
pci_bus_find_devices(struct udevice * bus,struct pci_device_id * ids,int * indexp,struct udevice ** devp)163 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
164 			 int *indexp, struct udevice **devp)
165 {
166 	struct udevice *dev;
167 
168 	/* Scan all devices on this bus */
169 	for (device_find_first_child(bus, &dev);
170 	     dev;
171 	     device_find_next_child(&dev)) {
172 		if (pci_device_matches_ids(dev, ids) >= 0) {
173 			if ((*indexp)-- <= 0) {
174 				*devp = dev;
175 				return 0;
176 			}
177 		}
178 	}
179 
180 	return -ENODEV;
181 }
182 
pci_find_device_id(struct pci_device_id * ids,int index,struct udevice ** devp)183 int pci_find_device_id(struct pci_device_id *ids, int index,
184 		       struct udevice **devp)
185 {
186 	struct udevice *bus;
187 
188 	/* Scan all known buses */
189 	for (uclass_first_device(UCLASS_PCI, &bus);
190 	     bus;
191 	     uclass_next_device(&bus)) {
192 		if (!pci_bus_find_devices(bus, ids, &index, devp))
193 			return 0;
194 	}
195 	*devp = NULL;
196 
197 	return -ENODEV;
198 }
199 
dm_pci_bus_find_device(struct udevice * bus,unsigned int vendor,unsigned int device,int * indexp,struct udevice ** devp)200 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
201 				  unsigned int device, int *indexp,
202 				  struct udevice **devp)
203 {
204 	struct pci_child_platdata *pplat;
205 	struct udevice *dev;
206 
207 	for (device_find_first_child(bus, &dev);
208 	     dev;
209 	     device_find_next_child(&dev)) {
210 		pplat = dev_get_parent_platdata(dev);
211 		if (pplat->vendor == vendor && pplat->device == device) {
212 			if (!(*indexp)--) {
213 				*devp = dev;
214 				return 0;
215 			}
216 		}
217 	}
218 
219 	return -ENODEV;
220 }
221 
dm_pci_find_device(unsigned int vendor,unsigned int device,int index,struct udevice ** devp)222 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
223 		       struct udevice **devp)
224 {
225 	struct udevice *bus;
226 
227 	/* Scan all known buses */
228 	for (uclass_first_device(UCLASS_PCI, &bus);
229 	     bus;
230 	     uclass_next_device(&bus)) {
231 		if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
232 			return device_probe(*devp);
233 	}
234 	*devp = NULL;
235 
236 	return -ENODEV;
237 }
238 
dm_pci_find_class(uint find_class,int index,struct udevice ** devp)239 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
240 {
241 	struct udevice *dev;
242 
243 	/* Scan all known buses */
244 	for (pci_find_first_device(&dev);
245 	     dev;
246 	     pci_find_next_device(&dev)) {
247 		struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
248 
249 		if (pplat->class == find_class && !index--) {
250 			*devp = dev;
251 			return device_probe(*devp);
252 		}
253 	}
254 	*devp = NULL;
255 
256 	return -ENODEV;
257 }
258 
pci_bus_write_config(struct udevice * bus,pci_dev_t bdf,int offset,unsigned long value,enum pci_size_t size)259 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
260 			 unsigned long value, enum pci_size_t size)
261 {
262 	struct dm_pci_ops *ops;
263 
264 	ops = pci_get_ops(bus);
265 	if (!ops->write_config)
266 		return -ENOSYS;
267 	return ops->write_config(bus, bdf, offset, value, size);
268 }
269 
pci_bus_clrset_config32(struct udevice * bus,pci_dev_t bdf,int offset,u32 clr,u32 set)270 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
271 			    u32 clr, u32 set)
272 {
273 	ulong val;
274 	int ret;
275 
276 	ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
277 	if (ret)
278 		return ret;
279 	val &= ~clr;
280 	val |= set;
281 
282 	return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
283 }
284 
pci_write_config(pci_dev_t bdf,int offset,unsigned long value,enum pci_size_t size)285 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
286 		     enum pci_size_t size)
287 {
288 	struct udevice *bus;
289 	int ret;
290 
291 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
292 	if (ret)
293 		return ret;
294 
295 	return pci_bus_write_config(bus, bdf, offset, value, size);
296 }
297 
dm_pci_write_config(struct udevice * dev,int offset,unsigned long value,enum pci_size_t size)298 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
299 			enum pci_size_t size)
300 {
301 	struct udevice *bus;
302 
303 	for (bus = dev; device_is_on_pci_bus(bus);)
304 		bus = bus->parent;
305 	return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
306 				    size);
307 }
308 
pci_write_config32(pci_dev_t bdf,int offset,u32 value)309 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
310 {
311 	return pci_write_config(bdf, offset, value, PCI_SIZE_32);
312 }
313 
pci_write_config16(pci_dev_t bdf,int offset,u16 value)314 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
315 {
316 	return pci_write_config(bdf, offset, value, PCI_SIZE_16);
317 }
318 
pci_write_config8(pci_dev_t bdf,int offset,u8 value)319 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
320 {
321 	return pci_write_config(bdf, offset, value, PCI_SIZE_8);
322 }
323 
dm_pci_write_config8(struct udevice * dev,int offset,u8 value)324 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
325 {
326 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
327 }
328 
dm_pci_write_config16(struct udevice * dev,int offset,u16 value)329 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
330 {
331 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
332 }
333 
dm_pci_write_config32(struct udevice * dev,int offset,u32 value)334 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
335 {
336 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
337 }
338 
pci_bus_read_config(struct udevice * bus,pci_dev_t bdf,int offset,unsigned long * valuep,enum pci_size_t size)339 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
340 			unsigned long *valuep, enum pci_size_t size)
341 {
342 	struct dm_pci_ops *ops;
343 
344 	ops = pci_get_ops(bus);
345 	if (!ops->read_config)
346 		return -ENOSYS;
347 	return ops->read_config(bus, bdf, offset, valuep, size);
348 }
349 
pci_read_config(pci_dev_t bdf,int offset,unsigned long * valuep,enum pci_size_t size)350 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
351 		    enum pci_size_t size)
352 {
353 	struct udevice *bus;
354 	int ret;
355 
356 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
357 	if (ret)
358 		return ret;
359 
360 	return pci_bus_read_config(bus, bdf, offset, valuep, size);
361 }
362 
dm_pci_read_config(struct udevice * dev,int offset,unsigned long * valuep,enum pci_size_t size)363 int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
364 		       enum pci_size_t size)
365 {
366 	struct udevice *bus;
367 
368 	for (bus = dev; device_is_on_pci_bus(bus);)
369 		bus = bus->parent;
370 	return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
371 				   size);
372 }
373 
pci_read_config32(pci_dev_t bdf,int offset,u32 * valuep)374 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
375 {
376 	unsigned long value;
377 	int ret;
378 
379 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
380 	if (ret)
381 		return ret;
382 	*valuep = value;
383 
384 	return 0;
385 }
386 
pci_read_config16(pci_dev_t bdf,int offset,u16 * valuep)387 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
388 {
389 	unsigned long value;
390 	int ret;
391 
392 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
393 	if (ret)
394 		return ret;
395 	*valuep = value;
396 
397 	return 0;
398 }
399 
pci_read_config8(pci_dev_t bdf,int offset,u8 * valuep)400 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
401 {
402 	unsigned long value;
403 	int ret;
404 
405 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
406 	if (ret)
407 		return ret;
408 	*valuep = value;
409 
410 	return 0;
411 }
412 
dm_pci_read_config8(struct udevice * dev,int offset,u8 * valuep)413 int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
414 {
415 	unsigned long value;
416 	int ret;
417 
418 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
419 	if (ret)
420 		return ret;
421 	*valuep = value;
422 
423 	return 0;
424 }
425 
dm_pci_read_config16(struct udevice * dev,int offset,u16 * valuep)426 int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
427 {
428 	unsigned long value;
429 	int ret;
430 
431 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
432 	if (ret)
433 		return ret;
434 	*valuep = value;
435 
436 	return 0;
437 }
438 
dm_pci_read_config32(struct udevice * dev,int offset,u32 * valuep)439 int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
440 {
441 	unsigned long value;
442 	int ret;
443 
444 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
445 	if (ret)
446 		return ret;
447 	*valuep = value;
448 
449 	return 0;
450 }
451 
dm_pci_clrset_config8(struct udevice * dev,int offset,u32 clr,u32 set)452 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
453 {
454 	u8 val;
455 	int ret;
456 
457 	ret = dm_pci_read_config8(dev, offset, &val);
458 	if (ret)
459 		return ret;
460 	val &= ~clr;
461 	val |= set;
462 
463 	return dm_pci_write_config8(dev, offset, val);
464 }
465 
dm_pci_clrset_config16(struct udevice * dev,int offset,u32 clr,u32 set)466 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
467 {
468 	u16 val;
469 	int ret;
470 
471 	ret = dm_pci_read_config16(dev, offset, &val);
472 	if (ret)
473 		return ret;
474 	val &= ~clr;
475 	val |= set;
476 
477 	return dm_pci_write_config16(dev, offset, val);
478 }
479 
dm_pci_clrset_config32(struct udevice * dev,int offset,u32 clr,u32 set)480 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
481 {
482 	u32 val;
483 	int ret;
484 
485 	ret = dm_pci_read_config32(dev, offset, &val);
486 	if (ret)
487 		return ret;
488 	val &= ~clr;
489 	val |= set;
490 
491 	return dm_pci_write_config32(dev, offset, val);
492 }
493 
set_vga_bridge_bits(struct udevice * dev)494 static void set_vga_bridge_bits(struct udevice *dev)
495 {
496 	struct udevice *parent = dev->parent;
497 	u16 bc;
498 
499 	while (parent->seq != 0) {
500 		dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
501 		bc |= PCI_BRIDGE_CTL_VGA;
502 		dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
503 		parent = parent->parent;
504 	}
505 }
506 
pci_auto_config_devices(struct udevice * bus)507 int pci_auto_config_devices(struct udevice *bus)
508 {
509 	struct pci_controller *hose = bus->uclass_priv;
510 	struct pci_child_platdata *pplat;
511 	unsigned int sub_bus;
512 	struct udevice *dev;
513 	int ret;
514 
515 	sub_bus = bus->seq;
516 	debug("%s: start\n", __func__);
517 	pciauto_config_init(hose);
518 	for (ret = device_find_first_child(bus, &dev);
519 	     !ret && dev;
520 	     ret = device_find_next_child(&dev)) {
521 		unsigned int max_bus;
522 		int ret;
523 
524 		debug("%s: device %s\n", __func__, dev->name);
525 		ret = dm_pciauto_config_device(dev);
526 		if (ret < 0)
527 			return ret;
528 		max_bus = ret;
529 		sub_bus = max(sub_bus, max_bus);
530 
531 		pplat = dev_get_parent_platdata(dev);
532 		if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
533 			set_vga_bridge_bits(dev);
534 	}
535 	debug("%s: done\n", __func__);
536 
537 	return sub_bus;
538 }
539 
pci_generic_mmap_write_config(struct udevice * bus,int (* addr_f)(struct udevice * bus,pci_dev_t bdf,uint offset,void ** addrp),pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)540 int pci_generic_mmap_write_config(
541 	struct udevice *bus,
542 	int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
543 	pci_dev_t bdf,
544 	uint offset,
545 	ulong value,
546 	enum pci_size_t size)
547 {
548 	void *address;
549 
550 	if (addr_f(bus, bdf, offset, &address) < 0)
551 		return 0;
552 
553 	switch (size) {
554 	case PCI_SIZE_8:
555 		writeb(value, address);
556 		return 0;
557 	case PCI_SIZE_16:
558 		writew(value, address);
559 		return 0;
560 	case PCI_SIZE_32:
561 		writel(value, address);
562 		return 0;
563 	default:
564 		return -EINVAL;
565 	}
566 }
567 
pci_generic_mmap_read_config(struct udevice * bus,int (* addr_f)(struct udevice * bus,pci_dev_t bdf,uint offset,void ** addrp),pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)568 int pci_generic_mmap_read_config(
569 	struct udevice *bus,
570 	int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
571 	pci_dev_t bdf,
572 	uint offset,
573 	ulong *valuep,
574 	enum pci_size_t size)
575 {
576 	void *address;
577 
578 	if (addr_f(bus, bdf, offset, &address) < 0) {
579 		*valuep = pci_get_ff(size);
580 		return 0;
581 	}
582 
583 	switch (size) {
584 	case PCI_SIZE_8:
585 		*valuep = readb(address);
586 		return 0;
587 	case PCI_SIZE_16:
588 		*valuep = readw(address);
589 		return 0;
590 	case PCI_SIZE_32:
591 		*valuep = readl(address);
592 		return 0;
593 	default:
594 		return -EINVAL;
595 	}
596 }
597 
dm_pci_hose_probe_bus(struct udevice * bus)598 int dm_pci_hose_probe_bus(struct udevice *bus)
599 {
600 	int sub_bus;
601 	int ret;
602 
603 	debug("%s\n", __func__);
604 
605 	sub_bus = pci_get_bus_max() + 1;
606 	debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
607 	dm_pciauto_prescan_setup_bridge(bus, sub_bus);
608 
609 	ret = device_probe(bus);
610 	if (ret) {
611 		debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
612 		      ret);
613 		return ret;
614 	}
615 	if (sub_bus != bus->seq) {
616 		printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
617 		       __func__, bus->name, bus->seq, sub_bus);
618 		return -EPIPE;
619 	}
620 	sub_bus = pci_get_bus_max();
621 	dm_pciauto_postscan_setup_bridge(bus, sub_bus);
622 
623 	return sub_bus;
624 }
625 
626 /**
627  * pci_match_one_device - Tell if a PCI device structure has a matching
628  *                        PCI device id structure
629  * @id: single PCI device id structure to match
630  * @find: the PCI device id structure to match against
631  *
632  * Returns true if the finding pci_device_id structure matched or false if
633  * there is no match.
634  */
pci_match_one_id(const struct pci_device_id * id,const struct pci_device_id * find)635 static bool pci_match_one_id(const struct pci_device_id *id,
636 			     const struct pci_device_id *find)
637 {
638 	if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
639 	    (id->device == PCI_ANY_ID || id->device == find->device) &&
640 	    (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
641 	    (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
642 	    !((id->class ^ find->class) & id->class_mask))
643 		return true;
644 
645 	return false;
646 }
647 
648 /**
649  * pci_find_and_bind_driver() - Find and bind the right PCI driver
650  *
651  * This only looks at certain fields in the descriptor.
652  *
653  * @parent:	Parent bus
654  * @find_id:	Specification of the driver to find
655  * @bdf:	Bus/device/function addreess - see PCI_BDF()
656  * @devp:	Returns a pointer to the device created
657  * @return 0 if OK, -EPERM if the device is not needed before relocation and
658  *	   therefore was not created, other -ve value on error
659  */
pci_find_and_bind_driver(struct udevice * parent,struct pci_device_id * find_id,pci_dev_t bdf,struct udevice ** devp)660 static int pci_find_and_bind_driver(struct udevice *parent,
661 				    struct pci_device_id *find_id,
662 				    pci_dev_t bdf, struct udevice **devp)
663 {
664 	struct pci_driver_entry *start, *entry;
665 	ofnode node = ofnode_null();
666 	const char *drv;
667 	int n_ents;
668 	int ret;
669 	char name[30], *str;
670 	bool bridge;
671 
672 	*devp = NULL;
673 
674 	debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
675 	      find_id->vendor, find_id->device);
676 
677 	/* Determine optional OF node */
678 	pci_dev_find_ofnode(parent, bdf, &node);
679 
680 	if (ofnode_valid(node) && !ofnode_is_available(node)) {
681 		debug("%s: Ignoring disabled device\n", __func__);
682 		return -EPERM;
683 	}
684 
685 	start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
686 	n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
687 	for (entry = start; entry != start + n_ents; entry++) {
688 		const struct pci_device_id *id;
689 		struct udevice *dev;
690 		const struct driver *drv;
691 
692 		for (id = entry->match;
693 		     id->vendor || id->subvendor || id->class_mask;
694 		     id++) {
695 			if (!pci_match_one_id(id, find_id))
696 				continue;
697 
698 			drv = entry->driver;
699 
700 			/*
701 			 * In the pre-relocation phase, we only bind devices
702 			 * whose driver has the DM_FLAG_PRE_RELOC set, to save
703 			 * precious memory space as on some platforms as that
704 			 * space is pretty limited (ie: using Cache As RAM).
705 			 */
706 			if (!(gd->flags & GD_FLG_RELOC) &&
707 			    !(drv->flags & DM_FLAG_PRE_RELOC))
708 				return -EPERM;
709 
710 			/*
711 			 * We could pass the descriptor to the driver as
712 			 * platdata (instead of NULL) and allow its bind()
713 			 * method to return -ENOENT if it doesn't support this
714 			 * device. That way we could continue the search to
715 			 * find another driver. For now this doesn't seem
716 			 * necesssary, so just bind the first match.
717 			 */
718 			ret = device_bind_ofnode(parent, drv, drv->name, NULL,
719 						 node, &dev);
720 			if (ret)
721 				goto error;
722 			debug("%s: Match found: %s\n", __func__, drv->name);
723 			dev->driver_data = id->driver_data;
724 			*devp = dev;
725 			return 0;
726 		}
727 	}
728 
729 	bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
730 	/*
731 	 * In the pre-relocation phase, we only bind bridge devices to save
732 	 * precious memory space as on some platforms as that space is pretty
733 	 * limited (ie: using Cache As RAM).
734 	 */
735 	if (!(gd->flags & GD_FLG_RELOC) && !bridge)
736 		return -EPERM;
737 
738 	/* Bind a generic driver so that the device can be used */
739 	sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
740 		PCI_FUNC(bdf));
741 	str = strdup(name);
742 	if (!str)
743 		return -ENOMEM;
744 	drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
745 
746 	ret = device_bind_driver_to_node(parent, drv, str, node, devp);
747 	if (ret) {
748 		debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
749 		free(str);
750 		return ret;
751 	}
752 	debug("%s: No match found: bound generic driver instead\n", __func__);
753 
754 	return 0;
755 
756 error:
757 	debug("%s: No match found: error %d\n", __func__, ret);
758 	return ret;
759 }
760 
pci_bind_bus_devices(struct udevice * bus)761 int pci_bind_bus_devices(struct udevice *bus)
762 {
763 	ulong vendor, device;
764 	ulong header_type;
765 	pci_dev_t bdf, end;
766 	bool found_multi;
767 	int ret;
768 
769 	found_multi = false;
770 	end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
771 		      PCI_MAX_PCI_FUNCTIONS - 1);
772 	for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
773 	     bdf += PCI_BDF(0, 0, 1)) {
774 		struct pci_child_platdata *pplat;
775 		struct udevice *dev;
776 		ulong class;
777 
778 		if (!PCI_FUNC(bdf))
779 			found_multi = false;
780 		if (PCI_FUNC(bdf) && !found_multi)
781 			continue;
782 
783 		/* Check only the first access, we don't expect problems */
784 		ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
785 					  PCI_SIZE_16);
786 		if (ret)
787 			goto error;
788 
789 		if (vendor == 0xffff || vendor == 0x0000)
790 			continue;
791 
792 		pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
793 				    &header_type, PCI_SIZE_8);
794 
795 		if (!PCI_FUNC(bdf))
796 			found_multi = header_type & 0x80;
797 
798 		debug("%s: bus %d/%s: found device %x, function %d", __func__,
799 		      bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
800 		pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
801 				    PCI_SIZE_16);
802 		pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
803 				    PCI_SIZE_32);
804 		class >>= 8;
805 
806 		/* Find this device in the device tree */
807 		ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
808 		debug(": find ret=%d\n", ret);
809 
810 		/* If nothing in the device tree, bind a device */
811 		if (ret == -ENODEV) {
812 			struct pci_device_id find_id;
813 			ulong val;
814 
815 			memset(&find_id, '\0', sizeof(find_id));
816 			find_id.vendor = vendor;
817 			find_id.device = device;
818 			find_id.class = class;
819 			if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
820 				pci_bus_read_config(bus, bdf,
821 						    PCI_SUBSYSTEM_VENDOR_ID,
822 						    &val, PCI_SIZE_32);
823 				find_id.subvendor = val & 0xffff;
824 				find_id.subdevice = val >> 16;
825 			}
826 			ret = pci_find_and_bind_driver(bus, &find_id, bdf,
827 						       &dev);
828 		}
829 		if (ret == -EPERM)
830 			continue;
831 		else if (ret)
832 			return ret;
833 
834 		/* Update the platform data */
835 		pplat = dev_get_parent_platdata(dev);
836 		pplat->devfn = PCI_MASK_BUS(bdf);
837 		pplat->vendor = vendor;
838 		pplat->device = device;
839 		pplat->class = class;
840 	}
841 
842 	return 0;
843 error:
844 	printf("Cannot read bus configuration: %d\n", ret);
845 
846 	return ret;
847 }
848 
decode_regions(struct pci_controller * hose,ofnode parent_node,ofnode node)849 static void decode_regions(struct pci_controller *hose, ofnode parent_node,
850 			   ofnode node)
851 {
852 	int pci_addr_cells, addr_cells, size_cells;
853 	int cells_per_record;
854 	const u32 *prop;
855 	int len;
856 	int i;
857 
858 	prop = ofnode_get_property(node, "ranges", &len);
859 	if (!prop) {
860 		debug("%s: Cannot decode regions\n", __func__);
861 		return;
862 	}
863 
864 	pci_addr_cells = ofnode_read_simple_addr_cells(node);
865 	addr_cells = ofnode_read_simple_addr_cells(parent_node);
866 	size_cells = ofnode_read_simple_size_cells(node);
867 
868 	/* PCI addresses are always 3-cells */
869 	len /= sizeof(u32);
870 	cells_per_record = pci_addr_cells + addr_cells + size_cells;
871 	hose->region_count = 0;
872 	debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
873 	      cells_per_record);
874 	for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
875 		u64 pci_addr, addr, size;
876 		int space_code;
877 		u32 flags;
878 		int type;
879 		int pos;
880 
881 		if (len < cells_per_record)
882 			break;
883 		flags = fdt32_to_cpu(prop[0]);
884 		space_code = (flags >> 24) & 3;
885 		pci_addr = fdtdec_get_number(prop + 1, 2);
886 		prop += pci_addr_cells;
887 		addr = fdtdec_get_number(prop, addr_cells);
888 		prop += addr_cells;
889 		size = fdtdec_get_number(prop, size_cells);
890 		prop += size_cells;
891 		debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
892 		      __func__, hose->region_count, pci_addr, addr, size, space_code);
893 		if (space_code & 2) {
894 			type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
895 					PCI_REGION_MEM;
896 		} else if (space_code & 1) {
897 			type = PCI_REGION_IO;
898 		} else {
899 			continue;
900 		}
901 
902 		if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
903 		    type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
904 			debug(" - beyond the 32-bit boundary, ignoring\n");
905 			continue;
906 		}
907 
908 		pos = -1;
909 		for (i = 0; i < hose->region_count; i++) {
910 			if (hose->regions[i].flags == type)
911 				pos = i;
912 		}
913 		if (pos == -1)
914 			pos = hose->region_count++;
915 		debug(" - type=%d, pos=%d\n", type, pos);
916 		pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
917 	}
918 
919 	/* Add a region for our local memory */
920 #ifdef CONFIG_NR_DRAM_BANKS
921 	bd_t *bd = gd->bd;
922 
923 	if (!bd)
924 		return;
925 
926 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
927 		if (hose->region_count == MAX_PCI_REGIONS) {
928 			pr_err("maximum number of regions parsed, aborting\n");
929 			break;
930 		}
931 
932 		if (bd->bi_dram[i].size) {
933 			pci_set_region(hose->regions + hose->region_count++,
934 				       bd->bi_dram[i].start,
935 				       bd->bi_dram[i].start,
936 				       bd->bi_dram[i].size,
937 				       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
938 		}
939 	}
940 #else
941 	phys_addr_t base = 0, size;
942 
943 	size = gd->ram_size;
944 #ifdef CONFIG_SYS_SDRAM_BASE
945 	base = CONFIG_SYS_SDRAM_BASE;
946 #endif
947 	if (gd->pci_ram_top && gd->pci_ram_top < base + size)
948 		size = gd->pci_ram_top - base;
949 	if (size)
950 		pci_set_region(hose->regions + hose->region_count++, base,
951 			base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
952 #endif
953 
954 	return;
955 }
956 
pci_uclass_pre_probe(struct udevice * bus)957 static int pci_uclass_pre_probe(struct udevice *bus)
958 {
959 	struct pci_controller *hose;
960 
961 	debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
962 	      bus->parent->name);
963 	hose = bus->uclass_priv;
964 
965 	/* For bridges, use the top-level PCI controller */
966 	if (!device_is_on_pci_bus(bus)) {
967 		hose->ctlr = bus;
968 		decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
969 	} else {
970 		struct pci_controller *parent_hose;
971 
972 		parent_hose = dev_get_uclass_priv(bus->parent);
973 		hose->ctlr = parent_hose->bus;
974 	}
975 	hose->bus = bus;
976 	hose->first_busno = bus->seq;
977 	hose->last_busno = bus->seq;
978 
979 	return 0;
980 }
981 
pci_uclass_post_probe(struct udevice * bus)982 static int pci_uclass_post_probe(struct udevice *bus)
983 {
984 	int ret;
985 
986 	debug("%s: probing bus %d\n", __func__, bus->seq);
987 	ret = pci_bind_bus_devices(bus);
988 	if (ret)
989 		return ret;
990 
991 #if CONFIG_IS_ENABLED(PCI_PNP)
992 	ret = pci_auto_config_devices(bus);
993 	if (ret < 0)
994 		return ret;
995 #endif
996 
997 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
998 	/*
999 	 * Per Intel FSP specification, we should call FSP notify API to
1000 	 * inform FSP that PCI enumeration has been done so that FSP will
1001 	 * do any necessary initialization as required by the chipset's
1002 	 * BIOS Writer's Guide (BWG).
1003 	 *
1004 	 * Unfortunately we have to put this call here as with driver model,
1005 	 * the enumeration is all done on a lazy basis as needed, so until
1006 	 * something is touched on PCI it won't happen.
1007 	 *
1008 	 * Note we only call this 1) after U-Boot is relocated, and 2)
1009 	 * root bus has finished probing.
1010 	 */
1011 	if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
1012 		ret = fsp_init_phase_pci();
1013 		if (ret)
1014 			return ret;
1015 	}
1016 #endif
1017 
1018 	return 0;
1019 }
1020 
pci_get_devfn(struct udevice * dev)1021 int pci_get_devfn(struct udevice *dev)
1022 {
1023 	struct fdt_pci_addr addr;
1024 	int ret;
1025 
1026 	/* Extract the devfn from fdt_pci_addr */
1027 	ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG,
1028 				   "reg", &addr);
1029 	if (ret) {
1030 		if (ret != -ENOENT)
1031 			return -EINVAL;
1032 	}
1033 
1034 	return addr.phys_hi & 0xff00;
1035 }
1036 
pci_uclass_child_post_bind(struct udevice * dev)1037 static int pci_uclass_child_post_bind(struct udevice *dev)
1038 {
1039 	struct pci_child_platdata *pplat;
1040 
1041 	if (!dev_of_valid(dev))
1042 		return 0;
1043 
1044 	pplat = dev_get_parent_platdata(dev);
1045 
1046 	/* Extract vendor id and device id if available */
1047 	ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1048 
1049 	/* Extract the devfn from fdt_pci_addr */
1050 	pplat->devfn = pci_get_devfn(dev);
1051 
1052 	return 0;
1053 }
1054 
pci_bridge_read_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)1055 static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
1056 				  uint offset, ulong *valuep,
1057 				  enum pci_size_t size)
1058 {
1059 	struct pci_controller *hose = bus->uclass_priv;
1060 
1061 	return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1062 }
1063 
pci_bridge_write_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)1064 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1065 				   uint offset, ulong value,
1066 				   enum pci_size_t size)
1067 {
1068 	struct pci_controller *hose = bus->uclass_priv;
1069 
1070 	return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1071 }
1072 
skip_to_next_device(struct udevice * bus,struct udevice ** devp)1073 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1074 {
1075 	struct udevice *dev;
1076 	int ret = 0;
1077 
1078 	/*
1079 	 * Scan through all the PCI controllers. On x86 there will only be one
1080 	 * but that is not necessarily true on other hardware.
1081 	 */
1082 	do {
1083 		device_find_first_child(bus, &dev);
1084 		if (dev) {
1085 			*devp = dev;
1086 			return 0;
1087 		}
1088 		ret = uclass_next_device(&bus);
1089 		if (ret)
1090 			return ret;
1091 	} while (bus);
1092 
1093 	return 0;
1094 }
1095 
pci_find_next_device(struct udevice ** devp)1096 int pci_find_next_device(struct udevice **devp)
1097 {
1098 	struct udevice *child = *devp;
1099 	struct udevice *bus = child->parent;
1100 	int ret;
1101 
1102 	/* First try all the siblings */
1103 	*devp = NULL;
1104 	while (child) {
1105 		device_find_next_child(&child);
1106 		if (child) {
1107 			*devp = child;
1108 			return 0;
1109 		}
1110 	}
1111 
1112 	/* We ran out of siblings. Try the next bus */
1113 	ret = uclass_next_device(&bus);
1114 	if (ret)
1115 		return ret;
1116 
1117 	return bus ? skip_to_next_device(bus, devp) : 0;
1118 }
1119 
pci_find_first_device(struct udevice ** devp)1120 int pci_find_first_device(struct udevice **devp)
1121 {
1122 	struct udevice *bus;
1123 	int ret;
1124 
1125 	*devp = NULL;
1126 	ret = uclass_first_device(UCLASS_PCI, &bus);
1127 	if (ret)
1128 		return ret;
1129 
1130 	return skip_to_next_device(bus, devp);
1131 }
1132 
pci_conv_32_to_size(ulong value,uint offset,enum pci_size_t size)1133 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1134 {
1135 	switch (size) {
1136 	case PCI_SIZE_8:
1137 		return (value >> ((offset & 3) * 8)) & 0xff;
1138 	case PCI_SIZE_16:
1139 		return (value >> ((offset & 2) * 8)) & 0xffff;
1140 	default:
1141 		return value;
1142 	}
1143 }
1144 
pci_conv_size_to_32(ulong old,ulong value,uint offset,enum pci_size_t size)1145 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1146 			  enum pci_size_t size)
1147 {
1148 	uint off_mask;
1149 	uint val_mask, shift;
1150 	ulong ldata, mask;
1151 
1152 	switch (size) {
1153 	case PCI_SIZE_8:
1154 		off_mask = 3;
1155 		val_mask = 0xff;
1156 		break;
1157 	case PCI_SIZE_16:
1158 		off_mask = 2;
1159 		val_mask = 0xffff;
1160 		break;
1161 	default:
1162 		return value;
1163 	}
1164 	shift = (offset & off_mask) * 8;
1165 	ldata = (value & val_mask) << shift;
1166 	mask = val_mask << shift;
1167 	value = (old & ~mask) | ldata;
1168 
1169 	return value;
1170 }
1171 
pci_get_regions(struct udevice * dev,struct pci_region ** iop,struct pci_region ** memp,struct pci_region ** prefp)1172 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1173 		    struct pci_region **memp, struct pci_region **prefp)
1174 {
1175 	struct udevice *bus = pci_get_controller(dev);
1176 	struct pci_controller *hose = dev_get_uclass_priv(bus);
1177 	int i;
1178 
1179 	*iop = NULL;
1180 	*memp = NULL;
1181 	*prefp = NULL;
1182 	for (i = 0; i < hose->region_count; i++) {
1183 		switch (hose->regions[i].flags) {
1184 		case PCI_REGION_IO:
1185 			if (!*iop || (*iop)->size < hose->regions[i].size)
1186 				*iop = hose->regions + i;
1187 			break;
1188 		case PCI_REGION_MEM:
1189 			if (!*memp || (*memp)->size < hose->regions[i].size)
1190 				*memp = hose->regions + i;
1191 			break;
1192 		case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1193 			if (!*prefp || (*prefp)->size < hose->regions[i].size)
1194 				*prefp = hose->regions + i;
1195 			break;
1196 		}
1197 	}
1198 
1199 	return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1200 }
1201 
dm_pci_read_bar32(struct udevice * dev,int barnum)1202 u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
1203 {
1204 	u32 addr;
1205 	int bar;
1206 
1207 	bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1208 	dm_pci_read_config32(dev, bar, &addr);
1209 	if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1210 		return addr & PCI_BASE_ADDRESS_IO_MASK;
1211 	else
1212 		return addr & PCI_BASE_ADDRESS_MEM_MASK;
1213 }
1214 
dm_pci_write_bar32(struct udevice * dev,int barnum,u32 addr)1215 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1216 {
1217 	int bar;
1218 
1219 	bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1220 	dm_pci_write_config32(dev, bar, addr);
1221 }
1222 
_dm_pci_bus_to_phys(struct udevice * ctlr,pci_addr_t bus_addr,unsigned long flags,unsigned long skip_mask,phys_addr_t * pa)1223 static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1224 			       pci_addr_t bus_addr, unsigned long flags,
1225 			       unsigned long skip_mask, phys_addr_t *pa)
1226 {
1227 	struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1228 	struct pci_region *res;
1229 	int i;
1230 
1231 	if (hose->region_count == 0) {
1232 		*pa = bus_addr;
1233 		return 0;
1234 	}
1235 
1236 	for (i = 0; i < hose->region_count; i++) {
1237 		res = &hose->regions[i];
1238 
1239 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1240 			continue;
1241 
1242 		if (res->flags & skip_mask)
1243 			continue;
1244 
1245 		if (bus_addr >= res->bus_start &&
1246 		    (bus_addr - res->bus_start) < res->size) {
1247 			*pa = (bus_addr - res->bus_start + res->phys_start);
1248 			return 0;
1249 		}
1250 	}
1251 
1252 	return 1;
1253 }
1254 
dm_pci_bus_to_phys(struct udevice * dev,pci_addr_t bus_addr,unsigned long flags)1255 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1256 			       unsigned long flags)
1257 {
1258 	phys_addr_t phys_addr = 0;
1259 	struct udevice *ctlr;
1260 	int ret;
1261 
1262 	/* The root controller has the region information */
1263 	ctlr = pci_get_controller(dev);
1264 
1265 	/*
1266 	 * if PCI_REGION_MEM is set we do a two pass search with preference
1267 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
1268 	 */
1269 	if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1270 		ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1271 					  flags, PCI_REGION_SYS_MEMORY,
1272 					  &phys_addr);
1273 		if (!ret)
1274 			return phys_addr;
1275 	}
1276 
1277 	ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1278 
1279 	if (ret)
1280 		puts("pci_hose_bus_to_phys: invalid physical address\n");
1281 
1282 	return phys_addr;
1283 }
1284 
_dm_pci_phys_to_bus(struct udevice * dev,phys_addr_t phys_addr,unsigned long flags,unsigned long skip_mask,pci_addr_t * ba)1285 int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1286 			unsigned long flags, unsigned long skip_mask,
1287 			pci_addr_t *ba)
1288 {
1289 	struct pci_region *res;
1290 	struct udevice *ctlr;
1291 	pci_addr_t bus_addr;
1292 	int i;
1293 	struct pci_controller *hose;
1294 
1295 	/* The root controller has the region information */
1296 	ctlr = pci_get_controller(dev);
1297 	hose = dev_get_uclass_priv(ctlr);
1298 
1299 	if (hose->region_count == 0) {
1300 		*ba = phys_addr;
1301 		return 0;
1302 	}
1303 
1304 	for (i = 0; i < hose->region_count; i++) {
1305 		res = &hose->regions[i];
1306 
1307 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1308 			continue;
1309 
1310 		if (res->flags & skip_mask)
1311 			continue;
1312 
1313 		bus_addr = phys_addr - res->phys_start + res->bus_start;
1314 
1315 		if (bus_addr >= res->bus_start &&
1316 		    (bus_addr - res->bus_start) < res->size) {
1317 			*ba = bus_addr;
1318 			return 0;
1319 		}
1320 	}
1321 
1322 	return 1;
1323 }
1324 
dm_pci_phys_to_bus(struct udevice * dev,phys_addr_t phys_addr,unsigned long flags)1325 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1326 			      unsigned long flags)
1327 {
1328 	pci_addr_t bus_addr = 0;
1329 	int ret;
1330 
1331 	/*
1332 	 * if PCI_REGION_MEM is set we do a two pass search with preference
1333 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
1334 	 */
1335 	if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1336 		ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1337 					  PCI_REGION_SYS_MEMORY, &bus_addr);
1338 		if (!ret)
1339 			return bus_addr;
1340 	}
1341 
1342 	ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1343 
1344 	if (ret)
1345 		puts("pci_hose_phys_to_bus: invalid physical address\n");
1346 
1347 	return bus_addr;
1348 }
1349 
dm_pci_map_ea_bar(struct udevice * dev,int bar,int flags,int ea_off)1350 static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1351 			       int ea_off)
1352 {
1353 	int ea_cnt, i, entry_size;
1354 	int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1355 	u32 ea_entry;
1356 	phys_addr_t addr;
1357 
1358 	/* EA capability structure header */
1359 	dm_pci_read_config32(dev, ea_off, &ea_entry);
1360 	ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1361 	ea_off += PCI_EA_FIRST_ENT;
1362 
1363 	for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1364 		/* Entry header */
1365 		dm_pci_read_config32(dev, ea_off, &ea_entry);
1366 		entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1367 
1368 		if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1369 			continue;
1370 
1371 		/* Base address, 1st DW */
1372 		dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1373 		addr = ea_entry & PCI_EA_FIELD_MASK;
1374 		if (ea_entry & PCI_EA_IS_64) {
1375 			/* Base address, 2nd DW, skip over 4B MaxOffset */
1376 			dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1377 			addr |= ((u64)ea_entry) << 32;
1378 		}
1379 
1380 		/* size ignored for now */
1381 		return map_physmem(addr, flags, 0);
1382 	}
1383 
1384 	return 0;
1385 }
1386 
dm_pci_map_bar(struct udevice * dev,int bar,int flags)1387 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1388 {
1389 	pci_addr_t pci_bus_addr;
1390 	u32 bar_response;
1391 	int ea_off;
1392 
1393 	/*
1394 	 * if the function supports Enhanced Allocation use that instead of
1395 	 * BARs
1396 	 */
1397 	ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1398 	if (ea_off)
1399 		return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
1400 
1401 	/* read BAR address */
1402 	dm_pci_read_config32(dev, bar, &bar_response);
1403 	pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1404 
1405 	/*
1406 	 * Pass "0" as the length argument to pci_bus_to_virt.  The arg
1407 	 * isn't actualy used on any platform because u-boot assumes a static
1408 	 * linear mapping.  In the future, this could read the BAR size
1409 	 * and pass that as the size if needed.
1410 	 */
1411 	return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1412 }
1413 
_dm_pci_find_next_capability(struct udevice * dev,u8 pos,int cap)1414 static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1415 {
1416 	int ttl = PCI_FIND_CAP_TTL;
1417 	u8 id;
1418 	u16 ent;
1419 
1420 	dm_pci_read_config8(dev, pos, &pos);
1421 
1422 	while (ttl--) {
1423 		if (pos < PCI_STD_HEADER_SIZEOF)
1424 			break;
1425 		pos &= ~3;
1426 		dm_pci_read_config16(dev, pos, &ent);
1427 
1428 		id = ent & 0xff;
1429 		if (id == 0xff)
1430 			break;
1431 		if (id == cap)
1432 			return pos;
1433 		pos = (ent >> 8);
1434 	}
1435 
1436 	return 0;
1437 }
1438 
dm_pci_find_next_capability(struct udevice * dev,u8 start,int cap)1439 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1440 {
1441 	return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1442 					    cap);
1443 }
1444 
dm_pci_find_capability(struct udevice * dev,int cap)1445 int dm_pci_find_capability(struct udevice *dev, int cap)
1446 {
1447 	u16 status;
1448 	u8 header_type;
1449 	u8 pos;
1450 
1451 	dm_pci_read_config16(dev, PCI_STATUS, &status);
1452 	if (!(status & PCI_STATUS_CAP_LIST))
1453 		return 0;
1454 
1455 	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1456 	if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1457 		pos = PCI_CB_CAPABILITY_LIST;
1458 	else
1459 		pos = PCI_CAPABILITY_LIST;
1460 
1461 	return _dm_pci_find_next_capability(dev, pos, cap);
1462 }
1463 
dm_pci_find_next_ext_capability(struct udevice * dev,int start,int cap)1464 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1465 {
1466 	u32 header;
1467 	int ttl;
1468 	int pos = PCI_CFG_SPACE_SIZE;
1469 
1470 	/* minimum 8 bytes per capability */
1471 	ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1472 
1473 	if (start)
1474 		pos = start;
1475 
1476 	dm_pci_read_config32(dev, pos, &header);
1477 	/*
1478 	 * If we have no capabilities, this is indicated by cap ID,
1479 	 * cap version and next pointer all being 0.
1480 	 */
1481 	if (header == 0)
1482 		return 0;
1483 
1484 	while (ttl--) {
1485 		if (PCI_EXT_CAP_ID(header) == cap)
1486 			return pos;
1487 
1488 		pos = PCI_EXT_CAP_NEXT(header);
1489 		if (pos < PCI_CFG_SPACE_SIZE)
1490 			break;
1491 
1492 		dm_pci_read_config32(dev, pos, &header);
1493 	}
1494 
1495 	return 0;
1496 }
1497 
dm_pci_find_ext_capability(struct udevice * dev,int cap)1498 int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1499 {
1500 	return dm_pci_find_next_ext_capability(dev, 0, cap);
1501 }
1502 
dm_pci_flr(struct udevice * dev)1503 int dm_pci_flr(struct udevice *dev)
1504 {
1505 	int pcie_off;
1506 	u32 cap;
1507 
1508 	/* look for PCI Express Capability */
1509 	pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1510 	if (!pcie_off)
1511 		return -ENOENT;
1512 
1513 	/* check FLR capability */
1514 	dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1515 	if (!(cap & PCI_EXP_DEVCAP_FLR))
1516 		return -ENOENT;
1517 
1518 	dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1519 			       PCI_EXP_DEVCTL_BCR_FLR);
1520 
1521 	/* wait 100ms, per PCI spec */
1522 	mdelay(100);
1523 
1524 	return 0;
1525 }
1526 
1527 UCLASS_DRIVER(pci) = {
1528 	.id		= UCLASS_PCI,
1529 	.name		= "pci",
1530 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
1531 	.post_bind	= dm_scan_fdt_dev,
1532 	.pre_probe	= pci_uclass_pre_probe,
1533 	.post_probe	= pci_uclass_post_probe,
1534 	.child_post_bind = pci_uclass_child_post_bind,
1535 	.per_device_auto_alloc_size = sizeof(struct pci_controller),
1536 	.per_child_platdata_auto_alloc_size =
1537 			sizeof(struct pci_child_platdata),
1538 };
1539 
1540 static const struct dm_pci_ops pci_bridge_ops = {
1541 	.read_config	= pci_bridge_read_config,
1542 	.write_config	= pci_bridge_write_config,
1543 };
1544 
1545 static const struct udevice_id pci_bridge_ids[] = {
1546 	{ .compatible = "pci-bridge" },
1547 	{ }
1548 };
1549 
1550 U_BOOT_DRIVER(pci_bridge_drv) = {
1551 	.name		= "pci_bridge_drv",
1552 	.id		= UCLASS_PCI,
1553 	.of_match	= pci_bridge_ids,
1554 	.ops		= &pci_bridge_ops,
1555 };
1556 
1557 UCLASS_DRIVER(pci_generic) = {
1558 	.id		= UCLASS_PCI_GENERIC,
1559 	.name		= "pci_generic",
1560 };
1561 
1562 static const struct udevice_id pci_generic_ids[] = {
1563 	{ .compatible = "pci-generic" },
1564 	{ }
1565 };
1566 
1567 U_BOOT_DRIVER(pci_generic_drv) = {
1568 	.name		= "pci_generic_drv",
1569 	.id		= UCLASS_PCI_GENERIC,
1570 	.of_match	= pci_generic_ids,
1571 };
1572 
pci_init(void)1573 void pci_init(void)
1574 {
1575 	struct udevice *bus;
1576 
1577 	/*
1578 	 * Enumerate all known controller devices. Enumeration has the side-
1579 	 * effect of probing them, so PCIe devices will be enumerated too.
1580 	 */
1581 	for (uclass_first_device_check(UCLASS_PCI, &bus);
1582 	     bus;
1583 	     uclass_next_device_check(&bus)) {
1584 		;
1585 	}
1586 }
1587