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