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