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