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