blob: fde5b83adf44e2edc2f1afb420ac4364a96a5fca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassff3e0772015-03-05 12:25:25 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassff3e0772015-03-05 12:25:25 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Simon Glassff3e0772015-03-05 12:25:25 -070013#include <pci.h>
Simon Glass21d1fe72015-11-29 13:18:03 -070014#include <asm/io.h>
Simon Glassff3e0772015-03-05 12:25:25 -070015#include <dm/device-internal.h>
Simon Glassbf501592017-05-18 20:09:51 -060016#include <dm/lists.h>
Bin Meng348b7442015-08-20 06:40:23 -070017#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glass07f2f582019-08-24 14:19:05 -060018#include <asm/fsp/fsp_support.h>
Bin Meng348b7442015-08-20 06:40:23 -070019#endif
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glass5e23b8b2015-11-29 13:17:49 -070021#include "pci_internal.h"
Simon Glassff3e0772015-03-05 12:25:25 -070022
23DECLARE_GLOBAL_DATA_PTR;
24
Simon Glassa6eb93b2016-01-18 20:19:14 -070025int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass983c6ba22015-08-31 18:55:35 -060026{
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) {
Simon Glass3f603cb2016-02-11 13:23:26 -070033 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass983c6ba22015-08-31 18:55:35 -060034 if (ret)
35 return ret;
Simon Glass983c6ba22015-08-31 18:55:35 -060036 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
37 }
38
39 return ret;
40}
41
Simon Glass9f60fb02015-11-19 20:27:00 -070042struct 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
Simon Glass194fca92020-01-27 08:49:38 -070050pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060051{
52 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
53 struct udevice *bus = dev->parent;
54
Simon Glass48862872019-12-29 21:19:14 -070055 /*
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);
Simon Glass4b515e42015-07-06 16:47:46 -060068 return PCI_ADD_BUS(bus->seq, pplat->devfn);
69}
70
Simon Glassff3e0772015-03-05 12:25:25 -070071/**
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 */
76static 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
93int pci_last_busno(void)
94{
Bin Meng069155c2015-10-01 00:36:01 -070095 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -070096}
97
98int 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
Marek Vasut02e4d382018-10-10 21:27:06 +0200110static 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
Simon Glassc4e72c42020-01-27 08:49:37 -0700131int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassff3e0772015-03-05 12:25:25 -0700132 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
Simon Glassf3f1fae2015-11-29 13:17:48 -0700151int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700152{
153 struct udevice *bus;
154 int ret;
155
Simon Glass983c6ba22015-08-31 18:55:35 -0600156 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700157 if (ret)
158 return ret;
159 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
160}
161
162static 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
180int 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
200int 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
Simon Glass5c0bf642015-11-29 13:17:50 -0700217static 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
239int 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
Simon Glassa0eb8352015-11-29 13:17:52 -0700256int 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
Simon Glassff3e0772015-03-05 12:25:25 -0700276int 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
Simon Glass319dba12016-03-06 19:27:52 -0700287int 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
Simon Glassff3e0772015-03-05 12:25:25 -0700302int 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
Simon Glass983c6ba22015-08-31 18:55:35 -0600308 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700309 if (ret)
310 return ret;
311
Bin Meng4d8615c2015-07-19 00:20:04 +0800312 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700313}
314
Simon Glass66afb4e2015-08-10 07:05:03 -0600315int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
316 enum pci_size_t size)
317{
318 struct udevice *bus;
319
Bin Meng1e0f2262015-09-11 03:24:34 -0700320 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600321 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700322 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
323 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600324}
325
Simon Glassff3e0772015-03-05 12:25:25 -0700326int 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
331int 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
336int 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
Simon Glass66afb4e2015-08-10 07:05:03 -0600341int 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
346int 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
351int 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
Simon Glass194fca92020-01-27 08:49:38 -0700356int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassff3e0772015-03-05 12:25:25 -0700357 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
367int 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
Simon Glass983c6ba22015-08-31 18:55:35 -0600373 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700374 if (ret)
375 return ret;
376
Bin Meng4d8615c2015-07-19 00:20:04 +0800377 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700378}
379
Simon Glass194fca92020-01-27 08:49:38 -0700380int dm_pci_read_config(const struct udevice *dev, int offset,
381 unsigned long *valuep, enum pci_size_t size)
Simon Glass66afb4e2015-08-10 07:05:03 -0600382{
Simon Glass194fca92020-01-27 08:49:38 -0700383 const struct udevice *bus;
Simon Glass66afb4e2015-08-10 07:05:03 -0600384
Bin Meng1e0f2262015-09-11 03:24:34 -0700385 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600386 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700387 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600388 size);
389}
390
Simon Glassff3e0772015-03-05 12:25:25 -0700391int 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
404int 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
417int 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
Simon Glass194fca92020-01-27 08:49:38 -0700430int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600431{
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
Simon Glass194fca92020-01-27 08:49:38 -0700443int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600444{
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
Simon Glass194fca92020-01-27 08:49:38 -0700456int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600457{
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
Simon Glass319dba12016-03-06 19:27:52 -0700469int 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
483int 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
497int 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
Bin Mengbbbcb522015-10-01 00:36:02 -0700511static 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
Simon Glassff3e0772015-03-05 12:25:25 -0700524int pci_auto_config_devices(struct udevice *bus)
525{
526 struct pci_controller *hose = bus->uclass_priv;
Bin Mengbbbcb522015-10-01 00:36:02 -0700527 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700528 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)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700538 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600539 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700540
Simon Glassff3e0772015-03-05 12:25:25 -0700541 debug("%s: device %s\n", __func__, dev->name);
Simon Glassd8c7fb52020-04-08 16:57:26 -0600542 if (dev_read_bool(dev, "pci,no-autoconfig"))
543 continue;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700544 ret = dm_pciauto_config_device(dev);
Simon Glass4d214552015-09-08 17:52:47 -0600545 if (ret < 0)
546 return ret;
547 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700548 sub_bus = max(sub_bus, max_bus);
Bin Mengbbbcb522015-10-01 00:36:02 -0700549
550 pplat = dev_get_parent_platdata(dev);
551 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
552 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700553 }
554 debug("%s: done\n", __func__);
555
556 return sub_bus;
557}
558
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300559int pci_generic_mmap_write_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700560 const struct udevice *bus,
561 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
562 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300563 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
588int pci_generic_mmap_read_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700589 const struct udevice *bus,
590 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
591 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300592 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
Simon Glass5e23b8b2015-11-29 13:17:49 -0700619int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassff3e0772015-03-05 12:25:25 -0700620{
Simon Glassff3e0772015-03-05 12:25:25 -0700621 int sub_bus;
622 int ret;
623
624 debug("%s\n", __func__);
Simon Glassff3e0772015-03-05 12:25:25 -0700625
626 sub_bus = pci_get_bus_max() + 1;
627 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700628 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700629
630 ret = device_probe(bus);
631 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600632 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700633 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();
Simon Glass5e23b8b2015-11-29 13:17:49 -0700642 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700643
644 return sub_bus;
645}
646
Simon Glassaba92962015-07-06 16:47:44 -0600647/**
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
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800651 * @find: the PCI device id structure to match against
Simon Glassaba92962015-07-06 16:47:44 -0600652 *
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800653 * Returns true if the finding pci_device_id structure matched or false if
654 * there is no match.
Simon Glassaba92962015-07-06 16:47:44 -0600655 */
656static 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.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600673 *
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
Simon Glassaba92962015-07-06 16:47:44 -0600680 */
681static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600682 struct pci_device_id *find_id,
683 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600684{
685 struct pci_driver_entry *start, *entry;
Marek Vasut02e4d382018-10-10 21:27:06 +0200686 ofnode node = ofnode_null();
Simon Glassaba92962015-07-06 16:47:44 -0600687 const char *drv;
688 int n_ents;
689 int ret;
690 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700691 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600692
693 *devp = NULL;
694
695 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
696 find_id->vendor, find_id->device);
Marek Vasut02e4d382018-10-10 21:27:06 +0200697
698 /* Determine optional OF node */
699 pci_dev_find_ofnode(parent, bdf, &node);
700
Michael Wallea6cd5972019-12-01 17:45:18 +0100701 if (ofnode_valid(node) && !ofnode_is_available(node)) {
702 debug("%s: Ignoring disabled device\n", __func__);
703 return -EPERM;
704 }
705
Simon Glassaba92962015-07-06 16:47:44 -0600706 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
707 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
708 for (entry = start; entry != start + n_ents; entry++) {
709 const struct pci_device_id *id;
710 struct udevice *dev;
711 const struct driver *drv;
712
713 for (id = entry->match;
714 id->vendor || id->subvendor || id->class_mask;
715 id++) {
716 if (!pci_match_one_id(id, find_id))
717 continue;
718
719 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700720
721 /*
722 * In the pre-relocation phase, we only bind devices
723 * whose driver has the DM_FLAG_PRE_RELOC set, to save
724 * precious memory space as on some platforms as that
725 * space is pretty limited (ie: using Cache As RAM).
726 */
727 if (!(gd->flags & GD_FLG_RELOC) &&
728 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600729 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700730
Simon Glassaba92962015-07-06 16:47:44 -0600731 /*
732 * We could pass the descriptor to the driver as
733 * platdata (instead of NULL) and allow its bind()
734 * method to return -ENOENT if it doesn't support this
735 * device. That way we could continue the search to
736 * find another driver. For now this doesn't seem
737 * necesssary, so just bind the first match.
738 */
Marek Vasut02e4d382018-10-10 21:27:06 +0200739 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
740 node, &dev);
Simon Glassaba92962015-07-06 16:47:44 -0600741 if (ret)
742 goto error;
743 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menged698aa2018-08-03 01:14:44 -0700744 dev->driver_data = id->driver_data;
Simon Glassaba92962015-07-06 16:47:44 -0600745 *devp = dev;
746 return 0;
747 }
748 }
749
Bin Meng08fc7b82015-08-20 06:40:17 -0700750 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
751 /*
752 * In the pre-relocation phase, we only bind bridge devices to save
753 * precious memory space as on some platforms as that space is pretty
754 * limited (ie: using Cache As RAM).
755 */
756 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600757 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700758
Simon Glassaba92962015-07-06 16:47:44 -0600759 /* Bind a generic driver so that the device can be used */
Bin Meng4d8615c2015-07-19 00:20:04 +0800760 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
761 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600762 str = strdup(name);
763 if (!str)
764 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700765 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
766
Marek Vasut02e4d382018-10-10 21:27:06 +0200767 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glassaba92962015-07-06 16:47:44 -0600768 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600769 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dec42640c2017-05-08 20:40:16 +0200770 free(str);
Simon Glassaba92962015-07-06 16:47:44 -0600771 return ret;
772 }
773 debug("%s: No match found: bound generic driver instead\n", __func__);
774
775 return 0;
776
777error:
778 debug("%s: No match found: error %d\n", __func__, ret);
779 return ret;
780}
781
Simon Glassff3e0772015-03-05 12:25:25 -0700782int pci_bind_bus_devices(struct udevice *bus)
783{
784 ulong vendor, device;
785 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800786 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700787 bool found_multi;
788 int ret;
789
790 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800791 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
792 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato6d9f5b02016-04-25 15:41:01 +0900793 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng4d8615c2015-07-19 00:20:04 +0800794 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700795 struct pci_child_platdata *pplat;
796 struct udevice *dev;
797 ulong class;
798
Bin Meng64e45f72018-08-03 01:14:37 -0700799 if (!PCI_FUNC(bdf))
800 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800801 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700802 continue;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800803
Simon Glassff3e0772015-03-05 12:25:25 -0700804 /* Check only the first access, we don't expect problems */
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800805 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
806 PCI_SIZE_16);
Simon Glassff3e0772015-03-05 12:25:25 -0700807 if (ret)
808 goto error;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800809
Simon Glassff3e0772015-03-05 12:25:25 -0700810 if (vendor == 0xffff || vendor == 0x0000)
811 continue;
812
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800813 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
814 &header_type, PCI_SIZE_8);
815
Bin Meng4d8615c2015-07-19 00:20:04 +0800816 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700817 found_multi = header_type & 0x80;
818
Simon Glass09115692019-09-25 08:56:12 -0600819 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Bin Meng4d8615c2015-07-19 00:20:04 +0800820 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
821 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700822 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800823 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600824 PCI_SIZE_32);
825 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700826
827 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800828 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass09115692019-09-25 08:56:12 -0600829 debug(": find ret=%d\n", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700830
Simon Glass8bd42522015-11-29 13:18:09 -0700831 /* If nothing in the device tree, bind a device */
Simon Glassff3e0772015-03-05 12:25:25 -0700832 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600833 struct pci_device_id find_id;
834 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700835
Simon Glassaba92962015-07-06 16:47:44 -0600836 memset(&find_id, '\0', sizeof(find_id));
837 find_id.vendor = vendor;
838 find_id.device = device;
839 find_id.class = class;
840 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800841 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600842 PCI_SUBSYSTEM_VENDOR_ID,
843 &val, PCI_SIZE_32);
844 find_id.subvendor = val & 0xffff;
845 find_id.subdevice = val >> 16;
846 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800847 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600848 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700849 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600850 if (ret == -EPERM)
851 continue;
852 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700853 return ret;
854
855 /* Update the platform data */
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600856 pplat = dev_get_parent_platdata(dev);
857 pplat->devfn = PCI_MASK_BUS(bdf);
858 pplat->vendor = vendor;
859 pplat->device = device;
860 pplat->class = class;
Simon Glassff3e0772015-03-05 12:25:25 -0700861 }
862
863 return 0;
864error:
865 printf("Cannot read bus configuration: %d\n", ret);
866
867 return ret;
868}
869
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700870static void decode_regions(struct pci_controller *hose, ofnode parent_node,
871 ofnode node)
Simon Glassff3e0772015-03-05 12:25:25 -0700872{
873 int pci_addr_cells, addr_cells, size_cells;
Stefan Roese3b7cd262020-07-23 16:26:07 +0200874 struct bd_info *bd = gd->bd;
Simon Glassff3e0772015-03-05 12:25:25 -0700875 int cells_per_record;
876 const u32 *prop;
Stefan Roesee0024742020-07-23 16:34:10 +0200877 int max_regions;
Simon Glassff3e0772015-03-05 12:25:25 -0700878 int len;
879 int i;
880
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900881 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700882 if (!prop) {
883 debug("%s: Cannot decode regions\n", __func__);
884 return;
885 }
886
Simon Glass878d68c2017-06-12 06:21:31 -0600887 pci_addr_cells = ofnode_read_simple_addr_cells(node);
888 addr_cells = ofnode_read_simple_addr_cells(parent_node);
889 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassff3e0772015-03-05 12:25:25 -0700890
891 /* PCI addresses are always 3-cells */
892 len /= sizeof(u32);
893 cells_per_record = pci_addr_cells + addr_cells + size_cells;
894 hose->region_count = 0;
895 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
896 cells_per_record);
Stefan Roesee0024742020-07-23 16:34:10 +0200897
898 /* Dynamically allocate the regions array */
899 max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
900 hose->regions = (struct pci_region *)
901 calloc(1, max_regions * sizeof(struct pci_region));
902
903 for (i = 0; i < max_regions; i++, len -= cells_per_record) {
Simon Glassff3e0772015-03-05 12:25:25 -0700904 u64 pci_addr, addr, size;
905 int space_code;
906 u32 flags;
907 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700908 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700909
910 if (len < cells_per_record)
911 break;
912 flags = fdt32_to_cpu(prop[0]);
913 space_code = (flags >> 24) & 3;
914 pci_addr = fdtdec_get_number(prop + 1, 2);
915 prop += pci_addr_cells;
916 addr = fdtdec_get_number(prop, addr_cells);
917 prop += addr_cells;
918 size = fdtdec_get_number(prop, size_cells);
919 prop += size_cells;
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900920 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
921 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassff3e0772015-03-05 12:25:25 -0700922 if (space_code & 2) {
923 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
924 PCI_REGION_MEM;
925 } else if (space_code & 1) {
926 type = PCI_REGION_IO;
927 } else {
928 continue;
929 }
Tuomas Tynkkynen52ba9072018-05-14 18:47:50 +0300930
931 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
932 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
933 debug(" - beyond the 32-bit boundary, ignoring\n");
934 continue;
935 }
936
Simon Glass9526d832015-11-19 20:26:58 -0700937 pos = -1;
938 for (i = 0; i < hose->region_count; i++) {
939 if (hose->regions[i].flags == type)
940 pos = i;
941 }
942 if (pos == -1)
943 pos = hose->region_count++;
944 debug(" - type=%d, pos=%d\n", type, pos);
945 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -0700946 }
947
948 /* Add a region for our local memory */
Bin Meng1eaf7802018-03-27 00:46:05 -0700949 if (!bd)
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700950 return;
Bin Meng1eaf7802018-03-27 00:46:05 -0700951
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100952 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
953 if (bd->bi_dram[i].size) {
954 pci_set_region(hose->regions + hose->region_count++,
955 bd->bi_dram[i].start,
956 bd->bi_dram[i].start,
957 bd->bi_dram[i].size,
958 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
959 }
960 }
Simon Glassff3e0772015-03-05 12:25:25 -0700961
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700962 return;
Simon Glassff3e0772015-03-05 12:25:25 -0700963}
964
965static int pci_uclass_pre_probe(struct udevice *bus)
966{
967 struct pci_controller *hose;
Simon Glassff3e0772015-03-05 12:25:25 -0700968
969 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
970 bus->parent->name);
971 hose = bus->uclass_priv;
972
973 /* For bridges, use the top-level PCI controller */
Paul Burton65f62b12016-09-08 07:47:32 +0100974 if (!device_is_on_pci_bus(bus)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700975 hose->ctlr = bus;
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700976 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassff3e0772015-03-05 12:25:25 -0700977 } else {
978 struct pci_controller *parent_hose;
979
980 parent_hose = dev_get_uclass_priv(bus->parent);
981 hose->ctlr = parent_hose->bus;
982 }
983 hose->bus = bus;
984 hose->first_busno = bus->seq;
985 hose->last_busno = bus->seq;
Simon Glass2206ac22019-12-06 21:41:37 -0700986 hose->skip_auto_config_until_reloc =
987 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
Simon Glassff3e0772015-03-05 12:25:25 -0700988
989 return 0;
990}
991
992static int pci_uclass_post_probe(struct udevice *bus)
993{
Simon Glass2206ac22019-12-06 21:41:37 -0700994 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700995 int ret;
996
Simon Glassff3e0772015-03-05 12:25:25 -0700997 debug("%s: probing bus %d\n", __func__, bus->seq);
998 ret = pci_bind_bus_devices(bus);
999 if (ret)
1000 return ret;
1001
Simon Glassf1f44382020-04-26 09:12:56 -06001002 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass2206ac22019-12-06 21:41:37 -07001003 (!hose->skip_auto_config_until_reloc ||
1004 (gd->flags & GD_FLG_RELOC))) {
1005 ret = pci_auto_config_devices(bus);
1006 if (ret < 0)
1007 return log_msg_ret("pci auto-config", ret);
1008 }
Simon Glassff3e0772015-03-05 12:25:25 -07001009
Bin Meng348b7442015-08-20 06:40:23 -07001010#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1011 /*
1012 * Per Intel FSP specification, we should call FSP notify API to
1013 * inform FSP that PCI enumeration has been done so that FSP will
1014 * do any necessary initialization as required by the chipset's
1015 * BIOS Writer's Guide (BWG).
1016 *
1017 * Unfortunately we have to put this call here as with driver model,
1018 * the enumeration is all done on a lazy basis as needed, so until
1019 * something is touched on PCI it won't happen.
1020 *
1021 * Note we only call this 1) after U-Boot is relocated, and 2)
1022 * root bus has finished probing.
1023 */
Simon Glassf1f44382020-04-26 09:12:56 -06001024 if ((gd->flags & GD_FLG_RELOC) && bus->seq == 0 && ll_boot_init()) {
Bin Meng348b7442015-08-20 06:40:23 -07001025 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -06001026 if (ret)
1027 return ret;
1028 }
Bin Meng348b7442015-08-20 06:40:23 -07001029#endif
1030
Simon Glass4d214552015-09-08 17:52:47 -06001031 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -07001032}
1033
1034static int pci_uclass_child_post_bind(struct udevice *dev)
1035{
1036 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -07001037
Simon Glassbf501592017-05-18 20:09:51 -06001038 if (!dev_of_valid(dev))
Simon Glassff3e0772015-03-05 12:25:25 -07001039 return 0;
1040
Simon Glassff3e0772015-03-05 12:25:25 -07001041 pplat = dev_get_parent_platdata(dev);
Bin Meng1f6b08b2018-08-03 01:14:36 -07001042
1043 /* Extract vendor id and device id if available */
1044 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1045
1046 /* Extract the devfn from fdt_pci_addr */
Stefan Roeseb5214202019-01-25 11:52:42 +01001047 pplat->devfn = pci_get_devfn(dev);
Simon Glassff3e0772015-03-05 12:25:25 -07001048
1049 return 0;
1050}
1051
Simon Glassc4e72c42020-01-27 08:49:37 -07001052static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng4d8615c2015-07-19 00:20:04 +08001053 uint offset, ulong *valuep,
1054 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001055{
1056 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001057
1058 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1059}
1060
Bin Meng4d8615c2015-07-19 00:20:04 +08001061static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1062 uint offset, ulong value,
1063 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001064{
1065 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001066
1067 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1068}
1069
Simon Glass76c3fbc2015-08-10 07:05:04 -06001070static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1071{
1072 struct udevice *dev;
1073 int ret = 0;
1074
1075 /*
1076 * Scan through all the PCI controllers. On x86 there will only be one
1077 * but that is not necessarily true on other hardware.
1078 */
1079 do {
1080 device_find_first_child(bus, &dev);
1081 if (dev) {
1082 *devp = dev;
1083 return 0;
1084 }
1085 ret = uclass_next_device(&bus);
1086 if (ret)
1087 return ret;
1088 } while (bus);
1089
1090 return 0;
1091}
1092
1093int pci_find_next_device(struct udevice **devp)
1094{
1095 struct udevice *child = *devp;
1096 struct udevice *bus = child->parent;
1097 int ret;
1098
1099 /* First try all the siblings */
1100 *devp = NULL;
1101 while (child) {
1102 device_find_next_child(&child);
1103 if (child) {
1104 *devp = child;
1105 return 0;
1106 }
1107 }
1108
1109 /* We ran out of siblings. Try the next bus */
1110 ret = uclass_next_device(&bus);
1111 if (ret)
1112 return ret;
1113
1114 return bus ? skip_to_next_device(bus, devp) : 0;
1115}
1116
1117int pci_find_first_device(struct udevice **devp)
1118{
1119 struct udevice *bus;
1120 int ret;
1121
1122 *devp = NULL;
1123 ret = uclass_first_device(UCLASS_PCI, &bus);
1124 if (ret)
1125 return ret;
1126
1127 return skip_to_next_device(bus, devp);
1128}
1129
Simon Glass9289db62015-11-19 20:26:59 -07001130ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1131{
1132 switch (size) {
1133 case PCI_SIZE_8:
1134 return (value >> ((offset & 3) * 8)) & 0xff;
1135 case PCI_SIZE_16:
1136 return (value >> ((offset & 2) * 8)) & 0xffff;
1137 default:
1138 return value;
1139 }
1140}
1141
1142ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1143 enum pci_size_t size)
1144{
1145 uint off_mask;
1146 uint val_mask, shift;
1147 ulong ldata, mask;
1148
1149 switch (size) {
1150 case PCI_SIZE_8:
1151 off_mask = 3;
1152 val_mask = 0xff;
1153 break;
1154 case PCI_SIZE_16:
1155 off_mask = 2;
1156 val_mask = 0xffff;
1157 break;
1158 default:
1159 return value;
1160 }
1161 shift = (offset & off_mask) * 8;
1162 ldata = (value & val_mask) << shift;
1163 mask = val_mask << shift;
1164 value = (old & ~mask) | ldata;
1165
1166 return value;
1167}
1168
Rayagonda Kokatanur143eb5b2020-05-12 13:29:49 +05301169int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1170{
1171 int pci_addr_cells, addr_cells, size_cells;
1172 int cells_per_record;
1173 const u32 *prop;
1174 int len;
1175 int i = 0;
1176
1177 prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1178 if (!prop) {
1179 log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1180 dev->name);
1181 return -EINVAL;
1182 }
1183
1184 pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1185 addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1186 size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1187
1188 /* PCI addresses are always 3-cells */
1189 len /= sizeof(u32);
1190 cells_per_record = pci_addr_cells + addr_cells + size_cells;
1191 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1192 cells_per_record);
1193
1194 while (len) {
1195 memp->bus_start = fdtdec_get_number(prop + 1, 2);
1196 prop += pci_addr_cells;
1197 memp->phys_start = fdtdec_get_number(prop, addr_cells);
1198 prop += addr_cells;
1199 memp->size = fdtdec_get_number(prop, size_cells);
1200 prop += size_cells;
1201
1202 if (i == index)
1203 return 0;
1204 i++;
1205 len -= cells_per_record;
1206 }
1207
1208 return -EINVAL;
1209}
1210
Simon Glassf9260332015-11-19 20:27:01 -07001211int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1212 struct pci_region **memp, struct pci_region **prefp)
1213{
1214 struct udevice *bus = pci_get_controller(dev);
1215 struct pci_controller *hose = dev_get_uclass_priv(bus);
1216 int i;
1217
1218 *iop = NULL;
1219 *memp = NULL;
1220 *prefp = NULL;
1221 for (i = 0; i < hose->region_count; i++) {
1222 switch (hose->regions[i].flags) {
1223 case PCI_REGION_IO:
1224 if (!*iop || (*iop)->size < hose->regions[i].size)
1225 *iop = hose->regions + i;
1226 break;
1227 case PCI_REGION_MEM:
1228 if (!*memp || (*memp)->size < hose->regions[i].size)
1229 *memp = hose->regions + i;
1230 break;
1231 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1232 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1233 *prefp = hose->regions + i;
1234 break;
1235 }
1236 }
1237
1238 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1239}
1240
Simon Glass194fca92020-01-27 08:49:38 -07001241u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glassbab17cf2015-11-29 13:17:53 -07001242{
1243 u32 addr;
1244 int bar;
1245
1246 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1247 dm_pci_read_config32(dev, bar, &addr);
Simon Glass9ece4b02020-04-09 10:27:36 -06001248
1249 /*
1250 * If we get an invalid address, return this so that comparisons with
1251 * FDT_ADDR_T_NONE work correctly
1252 */
1253 if (addr == 0xffffffff)
1254 return addr;
1255 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glassbab17cf2015-11-29 13:17:53 -07001256 return addr & PCI_BASE_ADDRESS_IO_MASK;
1257 else
1258 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1259}
1260
Simon Glass9d731c82016-01-18 20:19:15 -07001261void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1262{
1263 int bar;
1264
1265 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1266 dm_pci_write_config32(dev, bar, addr);
1267}
1268
Simon Glass21d1fe72015-11-29 13:18:03 -07001269static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1270 pci_addr_t bus_addr, unsigned long flags,
1271 unsigned long skip_mask, phys_addr_t *pa)
1272{
1273 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1274 struct pci_region *res;
1275 int i;
1276
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001277 if (hose->region_count == 0) {
1278 *pa = bus_addr;
1279 return 0;
1280 }
1281
Simon Glass21d1fe72015-11-29 13:18:03 -07001282 for (i = 0; i < hose->region_count; i++) {
1283 res = &hose->regions[i];
1284
1285 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1286 continue;
1287
1288 if (res->flags & skip_mask)
1289 continue;
1290
1291 if (bus_addr >= res->bus_start &&
1292 (bus_addr - res->bus_start) < res->size) {
1293 *pa = (bus_addr - res->bus_start + res->phys_start);
1294 return 0;
1295 }
1296 }
1297
1298 return 1;
1299}
1300
1301phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1302 unsigned long flags)
1303{
1304 phys_addr_t phys_addr = 0;
1305 struct udevice *ctlr;
1306 int ret;
1307
1308 /* The root controller has the region information */
1309 ctlr = pci_get_controller(dev);
1310
1311 /*
1312 * if PCI_REGION_MEM is set we do a two pass search with preference
1313 * on matches that don't have PCI_REGION_SYS_MEMORY set
1314 */
1315 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1316 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1317 flags, PCI_REGION_SYS_MEMORY,
1318 &phys_addr);
1319 if (!ret)
1320 return phys_addr;
1321 }
1322
1323 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1324
1325 if (ret)
1326 puts("pci_hose_bus_to_phys: invalid physical address\n");
1327
1328 return phys_addr;
1329}
1330
1331int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1332 unsigned long flags, unsigned long skip_mask,
1333 pci_addr_t *ba)
1334{
1335 struct pci_region *res;
1336 struct udevice *ctlr;
1337 pci_addr_t bus_addr;
1338 int i;
1339 struct pci_controller *hose;
1340
1341 /* The root controller has the region information */
1342 ctlr = pci_get_controller(dev);
1343 hose = dev_get_uclass_priv(ctlr);
1344
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001345 if (hose->region_count == 0) {
1346 *ba = phys_addr;
1347 return 0;
1348 }
1349
Simon Glass21d1fe72015-11-29 13:18:03 -07001350 for (i = 0; i < hose->region_count; i++) {
1351 res = &hose->regions[i];
1352
1353 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1354 continue;
1355
1356 if (res->flags & skip_mask)
1357 continue;
1358
1359 bus_addr = phys_addr - res->phys_start + res->bus_start;
1360
1361 if (bus_addr >= res->bus_start &&
1362 (bus_addr - res->bus_start) < res->size) {
1363 *ba = bus_addr;
1364 return 0;
1365 }
1366 }
1367
1368 return 1;
1369}
1370
1371pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1372 unsigned long flags)
1373{
1374 pci_addr_t bus_addr = 0;
1375 int ret;
1376
1377 /*
1378 * if PCI_REGION_MEM is set we do a two pass search with preference
1379 * on matches that don't have PCI_REGION_SYS_MEMORY set
1380 */
1381 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1382 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1383 PCI_REGION_SYS_MEMORY, &bus_addr);
1384 if (!ret)
1385 return bus_addr;
1386 }
1387
1388 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1389
1390 if (ret)
1391 puts("pci_hose_phys_to_bus: invalid physical address\n");
1392
1393 return bus_addr;
1394}
1395
Alex Marginean0b143d82019-06-07 11:24:23 +03001396static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1397 int ea_off)
1398{
1399 int ea_cnt, i, entry_size;
1400 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1401 u32 ea_entry;
1402 phys_addr_t addr;
1403
1404 /* EA capability structure header */
1405 dm_pci_read_config32(dev, ea_off, &ea_entry);
1406 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1407 ea_off += PCI_EA_FIRST_ENT;
1408
1409 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1410 /* Entry header */
1411 dm_pci_read_config32(dev, ea_off, &ea_entry);
1412 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1413
1414 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1415 continue;
1416
1417 /* Base address, 1st DW */
1418 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1419 addr = ea_entry & PCI_EA_FIELD_MASK;
1420 if (ea_entry & PCI_EA_IS_64) {
1421 /* Base address, 2nd DW, skip over 4B MaxOffset */
1422 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1423 addr |= ((u64)ea_entry) << 32;
1424 }
1425
1426 /* size ignored for now */
Suneel Garapatib3699a12019-10-19 16:44:35 -07001427 return map_physmem(addr, 0, flags);
Alex Marginean0b143d82019-06-07 11:24:23 +03001428 }
1429
1430 return 0;
1431}
1432
Simon Glass21d1fe72015-11-29 13:18:03 -07001433void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1434{
1435 pci_addr_t pci_bus_addr;
1436 u32 bar_response;
Alex Marginean0b143d82019-06-07 11:24:23 +03001437 int ea_off;
1438
1439 /*
1440 * if the function supports Enhanced Allocation use that instead of
1441 * BARs
1442 */
1443 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1444 if (ea_off)
1445 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
Simon Glass21d1fe72015-11-29 13:18:03 -07001446
1447 /* read BAR address */
1448 dm_pci_read_config32(dev, bar, &bar_response);
1449 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1450
1451 /*
1452 * Pass "0" as the length argument to pci_bus_to_virt. The arg
Suneel Garapatib3699a12019-10-19 16:44:35 -07001453 * isn't actually used on any platform because U-Boot assumes a static
Simon Glass21d1fe72015-11-29 13:18:03 -07001454 * linear mapping. In the future, this could read the BAR size
1455 * and pass that as the size if needed.
1456 */
1457 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1458}
1459
Bin Menga8c5f8d2018-10-15 02:21:21 -07001460static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001461{
Bin Mengdac01fd2018-08-03 01:14:52 -07001462 int ttl = PCI_FIND_CAP_TTL;
1463 u8 id;
1464 u16 ent;
Bin Mengdac01fd2018-08-03 01:14:52 -07001465
1466 dm_pci_read_config8(dev, pos, &pos);
Bin Menga8c5f8d2018-10-15 02:21:21 -07001467
Bin Mengdac01fd2018-08-03 01:14:52 -07001468 while (ttl--) {
1469 if (pos < PCI_STD_HEADER_SIZEOF)
1470 break;
1471 pos &= ~3;
1472 dm_pci_read_config16(dev, pos, &ent);
1473
1474 id = ent & 0xff;
1475 if (id == 0xff)
1476 break;
1477 if (id == cap)
1478 return pos;
1479 pos = (ent >> 8);
1480 }
1481
1482 return 0;
1483}
1484
Bin Menga8c5f8d2018-10-15 02:21:21 -07001485int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1486{
1487 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1488 cap);
1489}
1490
1491int dm_pci_find_capability(struct udevice *dev, int cap)
1492{
1493 u16 status;
1494 u8 header_type;
1495 u8 pos;
1496
1497 dm_pci_read_config16(dev, PCI_STATUS, &status);
1498 if (!(status & PCI_STATUS_CAP_LIST))
1499 return 0;
1500
1501 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1502 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1503 pos = PCI_CB_CAPABILITY_LIST;
1504 else
1505 pos = PCI_CAPABILITY_LIST;
1506
1507 return _dm_pci_find_next_capability(dev, pos, cap);
1508}
1509
1510int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001511{
1512 u32 header;
1513 int ttl;
1514 int pos = PCI_CFG_SPACE_SIZE;
1515
1516 /* minimum 8 bytes per capability */
1517 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1518
Bin Menga8c5f8d2018-10-15 02:21:21 -07001519 if (start)
1520 pos = start;
1521
Bin Mengdac01fd2018-08-03 01:14:52 -07001522 dm_pci_read_config32(dev, pos, &header);
1523 /*
1524 * If we have no capabilities, this is indicated by cap ID,
1525 * cap version and next pointer all being 0.
1526 */
1527 if (header == 0)
1528 return 0;
1529
1530 while (ttl--) {
1531 if (PCI_EXT_CAP_ID(header) == cap)
1532 return pos;
1533
1534 pos = PCI_EXT_CAP_NEXT(header);
1535 if (pos < PCI_CFG_SPACE_SIZE)
1536 break;
1537
1538 dm_pci_read_config32(dev, pos, &header);
1539 }
1540
1541 return 0;
1542}
1543
Bin Menga8c5f8d2018-10-15 02:21:21 -07001544int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1545{
1546 return dm_pci_find_next_ext_capability(dev, 0, cap);
1547}
1548
Alex Margineanb8e1f822019-06-07 11:24:25 +03001549int dm_pci_flr(struct udevice *dev)
1550{
1551 int pcie_off;
1552 u32 cap;
1553
1554 /* look for PCI Express Capability */
1555 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1556 if (!pcie_off)
1557 return -ENOENT;
1558
1559 /* check FLR capability */
1560 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1561 if (!(cap & PCI_EXP_DEVCAP_FLR))
1562 return -ENOENT;
1563
1564 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1565 PCI_EXP_DEVCTL_BCR_FLR);
1566
1567 /* wait 100ms, per PCI spec */
1568 mdelay(100);
1569
1570 return 0;
1571}
1572
Simon Glassff3e0772015-03-05 12:25:25 -07001573UCLASS_DRIVER(pci) = {
1574 .id = UCLASS_PCI,
1575 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -06001576 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -06001577 .post_bind = dm_scan_fdt_dev,
Simon Glassff3e0772015-03-05 12:25:25 -07001578 .pre_probe = pci_uclass_pre_probe,
1579 .post_probe = pci_uclass_post_probe,
1580 .child_post_bind = pci_uclass_child_post_bind,
1581 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1582 .per_child_platdata_auto_alloc_size =
1583 sizeof(struct pci_child_platdata),
1584};
1585
1586static const struct dm_pci_ops pci_bridge_ops = {
1587 .read_config = pci_bridge_read_config,
1588 .write_config = pci_bridge_write_config,
1589};
1590
1591static const struct udevice_id pci_bridge_ids[] = {
1592 { .compatible = "pci-bridge" },
1593 { }
1594};
1595
1596U_BOOT_DRIVER(pci_bridge_drv) = {
1597 .name = "pci_bridge_drv",
1598 .id = UCLASS_PCI,
1599 .of_match = pci_bridge_ids,
1600 .ops = &pci_bridge_ops,
1601};
1602
1603UCLASS_DRIVER(pci_generic) = {
1604 .id = UCLASS_PCI_GENERIC,
1605 .name = "pci_generic",
1606};
1607
1608static const struct udevice_id pci_generic_ids[] = {
1609 { .compatible = "pci-generic" },
1610 { }
1611};
1612
1613U_BOOT_DRIVER(pci_generic_drv) = {
1614 .name = "pci_generic_drv",
1615 .id = UCLASS_PCI_GENERIC,
1616 .of_match = pci_generic_ids,
1617};
Stephen Warrene578b922016-01-26 11:10:11 -07001618
1619void pci_init(void)
1620{
1621 struct udevice *bus;
1622
1623 /*
1624 * Enumerate all known controller devices. Enumeration has the side-
1625 * effect of probing them, so PCIe devices will be enumerated too.
1626 */
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001627 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warrene578b922016-01-26 11:10:11 -07001628 bus;
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001629 uclass_next_device_check(&bus)) {
Stephen Warrene578b922016-01-26 11:10:11 -07001630 ;
1631 }
1632}