blob: 76dfc1a957e603c712ba2a936d30906156b81c60 [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 Glass5e23b8b2015-11-29 13:17:49 -070020#include "pci_internal.h"
Simon Glassff3e0772015-03-05 12:25:25 -070021
22DECLARE_GLOBAL_DATA_PTR;
23
Simon Glassa6eb93b2016-01-18 20:19:14 -070024int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass983c6ba22015-08-31 18:55:35 -060025{
26 int ret;
27
28 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
29
30 /* Since buses may not be numbered yet try a little harder with bus 0 */
31 if (ret == -ENODEV) {
Simon Glass3f603cb2016-02-11 13:23:26 -070032 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass983c6ba22015-08-31 18:55:35 -060033 if (ret)
34 return ret;
Simon Glass983c6ba22015-08-31 18:55:35 -060035 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
36 }
37
38 return ret;
39}
40
Simon Glass9f60fb02015-11-19 20:27:00 -070041struct udevice *pci_get_controller(struct udevice *dev)
42{
43 while (device_is_on_pci_bus(dev))
44 dev = dev->parent;
45
46 return dev;
47}
48
Simon Glass194fca92020-01-27 08:49:38 -070049pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060050{
51 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
52 struct udevice *bus = dev->parent;
53
Simon Glass48862872019-12-29 21:19:14 -070054 /*
55 * This error indicates that @dev is a device on an unprobed PCI bus.
56 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
57 * will produce a bad BDF>
58 *
59 * A common cause of this problem is that this function is called in the
60 * ofdata_to_platdata() method of @dev. Accessing the PCI bus in that
61 * method is not allowed, since it has not yet been probed. To fix this,
62 * move that access to the probe() method of @dev instead.
63 */
64 if (!device_active(bus))
65 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
66 bus->name);
Simon Glass4b515e42015-07-06 16:47:46 -060067 return PCI_ADD_BUS(bus->seq, pplat->devfn);
68}
69
Simon Glassff3e0772015-03-05 12:25:25 -070070/**
71 * pci_get_bus_max() - returns the bus number of the last active bus
72 *
73 * @return last bus number, or -1 if no active buses
74 */
75static int pci_get_bus_max(void)
76{
77 struct udevice *bus;
78 struct uclass *uc;
79 int ret = -1;
80
81 ret = uclass_get(UCLASS_PCI, &uc);
82 uclass_foreach_dev(bus, uc) {
83 if (bus->seq > ret)
84 ret = bus->seq;
85 }
86
87 debug("%s: ret=%d\n", __func__, ret);
88
89 return ret;
90}
91
92int pci_last_busno(void)
93{
Bin Meng069155c2015-10-01 00:36:01 -070094 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -070095}
96
97int pci_get_ff(enum pci_size_t size)
98{
99 switch (size) {
100 case PCI_SIZE_8:
101 return 0xff;
102 case PCI_SIZE_16:
103 return 0xffff;
104 default:
105 return 0xffffffff;
106 }
107}
108
Marek Vasut02e4d382018-10-10 21:27:06 +0200109static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
110 ofnode *rnode)
111{
112 struct fdt_pci_addr addr;
113 ofnode node;
114 int ret;
115
116 dev_for_each_subnode(node, bus) {
117 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
118 &addr);
119 if (ret)
120 continue;
121
122 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
123 continue;
124
125 *rnode = node;
126 break;
127 }
128};
129
Simon Glassc4e72c42020-01-27 08:49:37 -0700130int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassff3e0772015-03-05 12:25:25 -0700131 struct udevice **devp)
132{
133 struct udevice *dev;
134
135 for (device_find_first_child(bus, &dev);
136 dev;
137 device_find_next_child(&dev)) {
138 struct pci_child_platdata *pplat;
139
140 pplat = dev_get_parent_platdata(dev);
141 if (pplat && pplat->devfn == find_devfn) {
142 *devp = dev;
143 return 0;
144 }
145 }
146
147 return -ENODEV;
148}
149
Simon Glassf3f1fae2015-11-29 13:17:48 -0700150int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700151{
152 struct udevice *bus;
153 int ret;
154
Simon Glass983c6ba22015-08-31 18:55:35 -0600155 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700156 if (ret)
157 return ret;
158 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
159}
160
161static int pci_device_matches_ids(struct udevice *dev,
162 struct pci_device_id *ids)
163{
164 struct pci_child_platdata *pplat;
165 int i;
166
167 pplat = dev_get_parent_platdata(dev);
168 if (!pplat)
169 return -EINVAL;
170 for (i = 0; ids[i].vendor != 0; i++) {
171 if (pplat->vendor == ids[i].vendor &&
172 pplat->device == ids[i].device)
173 return i;
174 }
175
176 return -EINVAL;
177}
178
179int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
180 int *indexp, struct udevice **devp)
181{
182 struct udevice *dev;
183
184 /* Scan all devices on this bus */
185 for (device_find_first_child(bus, &dev);
186 dev;
187 device_find_next_child(&dev)) {
188 if (pci_device_matches_ids(dev, ids) >= 0) {
189 if ((*indexp)-- <= 0) {
190 *devp = dev;
191 return 0;
192 }
193 }
194 }
195
196 return -ENODEV;
197}
198
199int pci_find_device_id(struct pci_device_id *ids, int index,
200 struct udevice **devp)
201{
202 struct udevice *bus;
203
204 /* Scan all known buses */
205 for (uclass_first_device(UCLASS_PCI, &bus);
206 bus;
207 uclass_next_device(&bus)) {
208 if (!pci_bus_find_devices(bus, ids, &index, devp))
209 return 0;
210 }
211 *devp = NULL;
212
213 return -ENODEV;
214}
215
Simon Glass5c0bf642015-11-29 13:17:50 -0700216static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
217 unsigned int device, int *indexp,
218 struct udevice **devp)
219{
220 struct pci_child_platdata *pplat;
221 struct udevice *dev;
222
223 for (device_find_first_child(bus, &dev);
224 dev;
225 device_find_next_child(&dev)) {
226 pplat = dev_get_parent_platdata(dev);
227 if (pplat->vendor == vendor && pplat->device == device) {
228 if (!(*indexp)--) {
229 *devp = dev;
230 return 0;
231 }
232 }
233 }
234
235 return -ENODEV;
236}
237
238int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
239 struct udevice **devp)
240{
241 struct udevice *bus;
242
243 /* Scan all known buses */
244 for (uclass_first_device(UCLASS_PCI, &bus);
245 bus;
246 uclass_next_device(&bus)) {
247 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
248 return device_probe(*devp);
249 }
250 *devp = NULL;
251
252 return -ENODEV;
253}
254
Simon Glassa0eb8352015-11-29 13:17:52 -0700255int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
256{
257 struct udevice *dev;
258
259 /* Scan all known buses */
260 for (pci_find_first_device(&dev);
261 dev;
262 pci_find_next_device(&dev)) {
263 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
264
265 if (pplat->class == find_class && !index--) {
266 *devp = dev;
267 return device_probe(*devp);
268 }
269 }
270 *devp = NULL;
271
272 return -ENODEV;
273}
274
Simon Glassff3e0772015-03-05 12:25:25 -0700275int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
276 unsigned long value, enum pci_size_t size)
277{
278 struct dm_pci_ops *ops;
279
280 ops = pci_get_ops(bus);
281 if (!ops->write_config)
282 return -ENOSYS;
283 return ops->write_config(bus, bdf, offset, value, size);
284}
285
Simon Glass319dba12016-03-06 19:27:52 -0700286int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
287 u32 clr, u32 set)
288{
289 ulong val;
290 int ret;
291
292 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
293 if (ret)
294 return ret;
295 val &= ~clr;
296 val |= set;
297
298 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
299}
300
Simon Glassff3e0772015-03-05 12:25:25 -0700301int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
302 enum pci_size_t size)
303{
304 struct udevice *bus;
305 int ret;
306
Simon Glass983c6ba22015-08-31 18:55:35 -0600307 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700308 if (ret)
309 return ret;
310
Bin Meng4d8615c2015-07-19 00:20:04 +0800311 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700312}
313
Simon Glass66afb4e2015-08-10 07:05:03 -0600314int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
315 enum pci_size_t size)
316{
317 struct udevice *bus;
318
Bin Meng1e0f2262015-09-11 03:24:34 -0700319 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600320 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700321 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
322 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600323}
324
Simon Glassff3e0772015-03-05 12:25:25 -0700325int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
326{
327 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
328}
329
330int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
331{
332 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
333}
334
335int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
336{
337 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
338}
339
Simon Glass66afb4e2015-08-10 07:05:03 -0600340int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
341{
342 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
343}
344
345int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
346{
347 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
348}
349
350int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
351{
352 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
353}
354
Simon Glass194fca92020-01-27 08:49:38 -0700355int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassff3e0772015-03-05 12:25:25 -0700356 unsigned long *valuep, enum pci_size_t size)
357{
358 struct dm_pci_ops *ops;
359
360 ops = pci_get_ops(bus);
361 if (!ops->read_config)
362 return -ENOSYS;
363 return ops->read_config(bus, bdf, offset, valuep, size);
364}
365
366int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
367 enum pci_size_t size)
368{
369 struct udevice *bus;
370 int ret;
371
Simon Glass983c6ba22015-08-31 18:55:35 -0600372 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700373 if (ret)
374 return ret;
375
Bin Meng4d8615c2015-07-19 00:20:04 +0800376 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700377}
378
Simon Glass194fca92020-01-27 08:49:38 -0700379int dm_pci_read_config(const struct udevice *dev, int offset,
380 unsigned long *valuep, enum pci_size_t size)
Simon Glass66afb4e2015-08-10 07:05:03 -0600381{
Simon Glass194fca92020-01-27 08:49:38 -0700382 const struct udevice *bus;
Simon Glass66afb4e2015-08-10 07:05:03 -0600383
Bin Meng1e0f2262015-09-11 03:24:34 -0700384 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600385 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700386 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600387 size);
388}
389
Simon Glassff3e0772015-03-05 12:25:25 -0700390int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
391{
392 unsigned long value;
393 int ret;
394
395 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
396 if (ret)
397 return ret;
398 *valuep = value;
399
400 return 0;
401}
402
403int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
404{
405 unsigned long value;
406 int ret;
407
408 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
409 if (ret)
410 return ret;
411 *valuep = value;
412
413 return 0;
414}
415
416int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
417{
418 unsigned long value;
419 int ret;
420
421 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
422 if (ret)
423 return ret;
424 *valuep = value;
425
426 return 0;
427}
428
Simon Glass194fca92020-01-27 08:49:38 -0700429int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600430{
431 unsigned long value;
432 int ret;
433
434 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
435 if (ret)
436 return ret;
437 *valuep = value;
438
439 return 0;
440}
441
Simon Glass194fca92020-01-27 08:49:38 -0700442int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600443{
444 unsigned long value;
445 int ret;
446
447 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
448 if (ret)
449 return ret;
450 *valuep = value;
451
452 return 0;
453}
454
Simon Glass194fca92020-01-27 08:49:38 -0700455int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600456{
457 unsigned long value;
458 int ret;
459
460 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
461 if (ret)
462 return ret;
463 *valuep = value;
464
465 return 0;
466}
467
Simon Glass319dba12016-03-06 19:27:52 -0700468int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
469{
470 u8 val;
471 int ret;
472
473 ret = dm_pci_read_config8(dev, offset, &val);
474 if (ret)
475 return ret;
476 val &= ~clr;
477 val |= set;
478
479 return dm_pci_write_config8(dev, offset, val);
480}
481
482int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
483{
484 u16 val;
485 int ret;
486
487 ret = dm_pci_read_config16(dev, offset, &val);
488 if (ret)
489 return ret;
490 val &= ~clr;
491 val |= set;
492
493 return dm_pci_write_config16(dev, offset, val);
494}
495
496int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
497{
498 u32 val;
499 int ret;
500
501 ret = dm_pci_read_config32(dev, offset, &val);
502 if (ret)
503 return ret;
504 val &= ~clr;
505 val |= set;
506
507 return dm_pci_write_config32(dev, offset, val);
508}
509
Bin Mengbbbcb522015-10-01 00:36:02 -0700510static void set_vga_bridge_bits(struct udevice *dev)
511{
512 struct udevice *parent = dev->parent;
513 u16 bc;
514
515 while (parent->seq != 0) {
516 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
517 bc |= PCI_BRIDGE_CTL_VGA;
518 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
519 parent = parent->parent;
520 }
521}
522
Simon Glassff3e0772015-03-05 12:25:25 -0700523int pci_auto_config_devices(struct udevice *bus)
524{
525 struct pci_controller *hose = bus->uclass_priv;
Bin Mengbbbcb522015-10-01 00:36:02 -0700526 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700527 unsigned int sub_bus;
528 struct udevice *dev;
529 int ret;
530
531 sub_bus = bus->seq;
532 debug("%s: start\n", __func__);
533 pciauto_config_init(hose);
534 for (ret = device_find_first_child(bus, &dev);
535 !ret && dev;
536 ret = device_find_next_child(&dev)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700537 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600538 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700539
Simon Glassff3e0772015-03-05 12:25:25 -0700540 debug("%s: device %s\n", __func__, dev->name);
Simon Glassd8c7fb52020-04-08 16:57:26 -0600541 if (dev_read_bool(dev, "pci,no-autoconfig"))
542 continue;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700543 ret = dm_pciauto_config_device(dev);
Simon Glass4d214552015-09-08 17:52:47 -0600544 if (ret < 0)
545 return ret;
546 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700547 sub_bus = max(sub_bus, max_bus);
Bin Mengbbbcb522015-10-01 00:36:02 -0700548
549 pplat = dev_get_parent_platdata(dev);
550 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
551 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700552 }
553 debug("%s: done\n", __func__);
554
555 return sub_bus;
556}
557
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300558int pci_generic_mmap_write_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700559 const struct udevice *bus,
560 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
561 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300562 pci_dev_t bdf,
563 uint offset,
564 ulong value,
565 enum pci_size_t size)
566{
567 void *address;
568
569 if (addr_f(bus, bdf, offset, &address) < 0)
570 return 0;
571
572 switch (size) {
573 case PCI_SIZE_8:
574 writeb(value, address);
575 return 0;
576 case PCI_SIZE_16:
577 writew(value, address);
578 return 0;
579 case PCI_SIZE_32:
580 writel(value, address);
581 return 0;
582 default:
583 return -EINVAL;
584 }
585}
586
587int pci_generic_mmap_read_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700588 const struct udevice *bus,
589 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
590 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300591 pci_dev_t bdf,
592 uint offset,
593 ulong *valuep,
594 enum pci_size_t size)
595{
596 void *address;
597
598 if (addr_f(bus, bdf, offset, &address) < 0) {
599 *valuep = pci_get_ff(size);
600 return 0;
601 }
602
603 switch (size) {
604 case PCI_SIZE_8:
605 *valuep = readb(address);
606 return 0;
607 case PCI_SIZE_16:
608 *valuep = readw(address);
609 return 0;
610 case PCI_SIZE_32:
611 *valuep = readl(address);
612 return 0;
613 default:
614 return -EINVAL;
615 }
616}
617
Simon Glass5e23b8b2015-11-29 13:17:49 -0700618int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassff3e0772015-03-05 12:25:25 -0700619{
Simon Glassff3e0772015-03-05 12:25:25 -0700620 int sub_bus;
621 int ret;
622
623 debug("%s\n", __func__);
Simon Glassff3e0772015-03-05 12:25:25 -0700624
625 sub_bus = pci_get_bus_max() + 1;
626 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700627 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700628
629 ret = device_probe(bus);
630 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600631 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700632 ret);
633 return ret;
634 }
635 if (sub_bus != bus->seq) {
636 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
637 __func__, bus->name, bus->seq, sub_bus);
638 return -EPIPE;
639 }
640 sub_bus = pci_get_bus_max();
Simon Glass5e23b8b2015-11-29 13:17:49 -0700641 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700642
643 return sub_bus;
644}
645
Simon Glassaba92962015-07-06 16:47:44 -0600646/**
647 * pci_match_one_device - Tell if a PCI device structure has a matching
648 * PCI device id structure
649 * @id: single PCI device id structure to match
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800650 * @find: the PCI device id structure to match against
Simon Glassaba92962015-07-06 16:47:44 -0600651 *
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800652 * Returns true if the finding pci_device_id structure matched or false if
653 * there is no match.
Simon Glassaba92962015-07-06 16:47:44 -0600654 */
655static bool pci_match_one_id(const struct pci_device_id *id,
656 const struct pci_device_id *find)
657{
658 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
659 (id->device == PCI_ANY_ID || id->device == find->device) &&
660 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
661 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
662 !((id->class ^ find->class) & id->class_mask))
663 return true;
664
665 return false;
666}
667
668/**
669 * pci_find_and_bind_driver() - Find and bind the right PCI driver
670 *
671 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600672 *
673 * @parent: Parent bus
674 * @find_id: Specification of the driver to find
675 * @bdf: Bus/device/function addreess - see PCI_BDF()
676 * @devp: Returns a pointer to the device created
677 * @return 0 if OK, -EPERM if the device is not needed before relocation and
678 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600679 */
680static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600681 struct pci_device_id *find_id,
682 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600683{
684 struct pci_driver_entry *start, *entry;
Marek Vasut02e4d382018-10-10 21:27:06 +0200685 ofnode node = ofnode_null();
Simon Glassaba92962015-07-06 16:47:44 -0600686 const char *drv;
687 int n_ents;
688 int ret;
689 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700690 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600691
692 *devp = NULL;
693
694 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
695 find_id->vendor, find_id->device);
Marek Vasut02e4d382018-10-10 21:27:06 +0200696
697 /* Determine optional OF node */
698 pci_dev_find_ofnode(parent, bdf, &node);
699
Michael Wallea6cd5972019-12-01 17:45:18 +0100700 if (ofnode_valid(node) && !ofnode_is_available(node)) {
701 debug("%s: Ignoring disabled device\n", __func__);
702 return -EPERM;
703 }
704
Simon Glassaba92962015-07-06 16:47:44 -0600705 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
706 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
707 for (entry = start; entry != start + n_ents; entry++) {
708 const struct pci_device_id *id;
709 struct udevice *dev;
710 const struct driver *drv;
711
712 for (id = entry->match;
713 id->vendor || id->subvendor || id->class_mask;
714 id++) {
715 if (!pci_match_one_id(id, find_id))
716 continue;
717
718 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700719
720 /*
721 * In the pre-relocation phase, we only bind devices
722 * whose driver has the DM_FLAG_PRE_RELOC set, to save
723 * precious memory space as on some platforms as that
724 * space is pretty limited (ie: using Cache As RAM).
725 */
726 if (!(gd->flags & GD_FLG_RELOC) &&
727 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600728 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700729
Simon Glassaba92962015-07-06 16:47:44 -0600730 /*
731 * We could pass the descriptor to the driver as
732 * platdata (instead of NULL) and allow its bind()
733 * method to return -ENOENT if it doesn't support this
734 * device. That way we could continue the search to
735 * find another driver. For now this doesn't seem
736 * necesssary, so just bind the first match.
737 */
Marek Vasut02e4d382018-10-10 21:27:06 +0200738 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
739 node, &dev);
Simon Glassaba92962015-07-06 16:47:44 -0600740 if (ret)
741 goto error;
742 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menged698aa2018-08-03 01:14:44 -0700743 dev->driver_data = id->driver_data;
Simon Glassaba92962015-07-06 16:47:44 -0600744 *devp = dev;
745 return 0;
746 }
747 }
748
Bin Meng08fc7b82015-08-20 06:40:17 -0700749 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
750 /*
751 * In the pre-relocation phase, we only bind bridge devices to save
752 * precious memory space as on some platforms as that space is pretty
753 * limited (ie: using Cache As RAM).
754 */
755 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600756 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700757
Simon Glassaba92962015-07-06 16:47:44 -0600758 /* Bind a generic driver so that the device can be used */
Bin Meng4d8615c2015-07-19 00:20:04 +0800759 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
760 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600761 str = strdup(name);
762 if (!str)
763 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700764 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
765
Marek Vasut02e4d382018-10-10 21:27:06 +0200766 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glassaba92962015-07-06 16:47:44 -0600767 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600768 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dec42640c2017-05-08 20:40:16 +0200769 free(str);
Simon Glassaba92962015-07-06 16:47:44 -0600770 return ret;
771 }
772 debug("%s: No match found: bound generic driver instead\n", __func__);
773
774 return 0;
775
776error:
777 debug("%s: No match found: error %d\n", __func__, ret);
778 return ret;
779}
780
Simon Glassff3e0772015-03-05 12:25:25 -0700781int pci_bind_bus_devices(struct udevice *bus)
782{
783 ulong vendor, device;
784 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800785 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700786 bool found_multi;
787 int ret;
788
789 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800790 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
791 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato6d9f5b02016-04-25 15:41:01 +0900792 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng4d8615c2015-07-19 00:20:04 +0800793 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700794 struct pci_child_platdata *pplat;
795 struct udevice *dev;
796 ulong class;
797
Bin Meng64e45f72018-08-03 01:14:37 -0700798 if (!PCI_FUNC(bdf))
799 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800800 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700801 continue;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800802
Simon Glassff3e0772015-03-05 12:25:25 -0700803 /* Check only the first access, we don't expect problems */
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800804 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
805 PCI_SIZE_16);
Simon Glassff3e0772015-03-05 12:25:25 -0700806 if (ret)
807 goto error;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800808
Simon Glassff3e0772015-03-05 12:25:25 -0700809 if (vendor == 0xffff || vendor == 0x0000)
810 continue;
811
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800812 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
813 &header_type, PCI_SIZE_8);
814
Bin Meng4d8615c2015-07-19 00:20:04 +0800815 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700816 found_multi = header_type & 0x80;
817
Simon Glass09115692019-09-25 08:56:12 -0600818 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Bin Meng4d8615c2015-07-19 00:20:04 +0800819 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
820 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700821 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800822 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600823 PCI_SIZE_32);
824 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700825
826 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800827 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass09115692019-09-25 08:56:12 -0600828 debug(": find ret=%d\n", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700829
Simon Glass8bd42522015-11-29 13:18:09 -0700830 /* If nothing in the device tree, bind a device */
Simon Glassff3e0772015-03-05 12:25:25 -0700831 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600832 struct pci_device_id find_id;
833 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700834
Simon Glassaba92962015-07-06 16:47:44 -0600835 memset(&find_id, '\0', sizeof(find_id));
836 find_id.vendor = vendor;
837 find_id.device = device;
838 find_id.class = class;
839 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800840 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600841 PCI_SUBSYSTEM_VENDOR_ID,
842 &val, PCI_SIZE_32);
843 find_id.subvendor = val & 0xffff;
844 find_id.subdevice = val >> 16;
845 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800846 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600847 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700848 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600849 if (ret == -EPERM)
850 continue;
851 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700852 return ret;
853
854 /* Update the platform data */
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600855 pplat = dev_get_parent_platdata(dev);
856 pplat->devfn = PCI_MASK_BUS(bdf);
857 pplat->vendor = vendor;
858 pplat->device = device;
859 pplat->class = class;
Simon Glassff3e0772015-03-05 12:25:25 -0700860 }
861
862 return 0;
863error:
864 printf("Cannot read bus configuration: %d\n", ret);
865
866 return ret;
867}
868
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700869static void decode_regions(struct pci_controller *hose, ofnode parent_node,
870 ofnode node)
Simon Glassff3e0772015-03-05 12:25:25 -0700871{
872 int pci_addr_cells, addr_cells, size_cells;
873 int cells_per_record;
874 const u32 *prop;
875 int len;
876 int i;
877
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900878 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700879 if (!prop) {
880 debug("%s: Cannot decode regions\n", __func__);
881 return;
882 }
883
Simon Glass878d68c2017-06-12 06:21:31 -0600884 pci_addr_cells = ofnode_read_simple_addr_cells(node);
885 addr_cells = ofnode_read_simple_addr_cells(parent_node);
886 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassff3e0772015-03-05 12:25:25 -0700887
888 /* PCI addresses are always 3-cells */
889 len /= sizeof(u32);
890 cells_per_record = pci_addr_cells + addr_cells + size_cells;
891 hose->region_count = 0;
892 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
893 cells_per_record);
894 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
895 u64 pci_addr, addr, size;
896 int space_code;
897 u32 flags;
898 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700899 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700900
901 if (len < cells_per_record)
902 break;
903 flags = fdt32_to_cpu(prop[0]);
904 space_code = (flags >> 24) & 3;
905 pci_addr = fdtdec_get_number(prop + 1, 2);
906 prop += pci_addr_cells;
907 addr = fdtdec_get_number(prop, addr_cells);
908 prop += addr_cells;
909 size = fdtdec_get_number(prop, size_cells);
910 prop += size_cells;
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900911 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
912 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassff3e0772015-03-05 12:25:25 -0700913 if (space_code & 2) {
914 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
915 PCI_REGION_MEM;
916 } else if (space_code & 1) {
917 type = PCI_REGION_IO;
918 } else {
919 continue;
920 }
Tuomas Tynkkynen52ba9072018-05-14 18:47:50 +0300921
922 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
923 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
924 debug(" - beyond the 32-bit boundary, ignoring\n");
925 continue;
926 }
927
Simon Glass9526d832015-11-19 20:26:58 -0700928 pos = -1;
929 for (i = 0; i < hose->region_count; i++) {
930 if (hose->regions[i].flags == type)
931 pos = i;
932 }
933 if (pos == -1)
934 pos = hose->region_count++;
935 debug(" - type=%d, pos=%d\n", type, pos);
936 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -0700937 }
938
939 /* Add a region for our local memory */
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100940#ifdef CONFIG_NR_DRAM_BANKS
941 bd_t *bd = gd->bd;
942
Bin Meng1eaf7802018-03-27 00:46:05 -0700943 if (!bd)
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700944 return;
Bin Meng1eaf7802018-03-27 00:46:05 -0700945
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100946 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
Thierry Redingd94d9aa2019-03-15 16:32:32 +0100947 if (hose->region_count == MAX_PCI_REGIONS) {
948 pr_err("maximum number of regions parsed, aborting\n");
949 break;
950 }
951
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100952 if (bd->bi_dram[i].size) {
953 pci_set_region(hose->regions + hose->region_count++,
954 bd->bi_dram[i].start,
955 bd->bi_dram[i].start,
956 bd->bi_dram[i].size,
957 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
958 }
959 }
960#else
961 phys_addr_t base = 0, size;
962
Simon Glass2084c5a2015-11-19 20:26:57 -0700963 size = gd->ram_size;
964#ifdef CONFIG_SYS_SDRAM_BASE
965 base = CONFIG_SYS_SDRAM_BASE;
966#endif
967 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
968 size = gd->pci_ram_top - base;
Bin Mengee1109b2018-03-27 00:46:06 -0700969 if (size)
970 pci_set_region(hose->regions + hose->region_count++, base,
971 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100972#endif
Simon Glassff3e0772015-03-05 12:25:25 -0700973
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700974 return;
Simon Glassff3e0772015-03-05 12:25:25 -0700975}
976
977static int pci_uclass_pre_probe(struct udevice *bus)
978{
979 struct pci_controller *hose;
Simon Glassff3e0772015-03-05 12:25:25 -0700980
981 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
982 bus->parent->name);
983 hose = bus->uclass_priv;
984
985 /* For bridges, use the top-level PCI controller */
Paul Burton65f62b12016-09-08 07:47:32 +0100986 if (!device_is_on_pci_bus(bus)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700987 hose->ctlr = bus;
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700988 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassff3e0772015-03-05 12:25:25 -0700989 } else {
990 struct pci_controller *parent_hose;
991
992 parent_hose = dev_get_uclass_priv(bus->parent);
993 hose->ctlr = parent_hose->bus;
994 }
995 hose->bus = bus;
996 hose->first_busno = bus->seq;
997 hose->last_busno = bus->seq;
Simon Glass2206ac22019-12-06 21:41:37 -0700998 hose->skip_auto_config_until_reloc =
999 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
Simon Glassff3e0772015-03-05 12:25:25 -07001000
1001 return 0;
1002}
1003
1004static int pci_uclass_post_probe(struct udevice *bus)
1005{
Simon Glass2206ac22019-12-06 21:41:37 -07001006 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001007 int ret;
1008
Simon Glassff3e0772015-03-05 12:25:25 -07001009 debug("%s: probing bus %d\n", __func__, bus->seq);
1010 ret = pci_bind_bus_devices(bus);
1011 if (ret)
1012 return ret;
1013
Simon Glassf1f44382020-04-26 09:12:56 -06001014 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass2206ac22019-12-06 21:41:37 -07001015 (!hose->skip_auto_config_until_reloc ||
1016 (gd->flags & GD_FLG_RELOC))) {
1017 ret = pci_auto_config_devices(bus);
1018 if (ret < 0)
1019 return log_msg_ret("pci auto-config", ret);
1020 }
Simon Glassff3e0772015-03-05 12:25:25 -07001021
Bin Meng348b7442015-08-20 06:40:23 -07001022#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1023 /*
1024 * Per Intel FSP specification, we should call FSP notify API to
1025 * inform FSP that PCI enumeration has been done so that FSP will
1026 * do any necessary initialization as required by the chipset's
1027 * BIOS Writer's Guide (BWG).
1028 *
1029 * Unfortunately we have to put this call here as with driver model,
1030 * the enumeration is all done on a lazy basis as needed, so until
1031 * something is touched on PCI it won't happen.
1032 *
1033 * Note we only call this 1) after U-Boot is relocated, and 2)
1034 * root bus has finished probing.
1035 */
Simon Glassf1f44382020-04-26 09:12:56 -06001036 if ((gd->flags & GD_FLG_RELOC) && bus->seq == 0 && ll_boot_init()) {
Bin Meng348b7442015-08-20 06:40:23 -07001037 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -06001038 if (ret)
1039 return ret;
1040 }
Bin Meng348b7442015-08-20 06:40:23 -07001041#endif
1042
Simon Glass4d214552015-09-08 17:52:47 -06001043 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -07001044}
1045
1046static int pci_uclass_child_post_bind(struct udevice *dev)
1047{
1048 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -07001049
Simon Glassbf501592017-05-18 20:09:51 -06001050 if (!dev_of_valid(dev))
Simon Glassff3e0772015-03-05 12:25:25 -07001051 return 0;
1052
Simon Glassff3e0772015-03-05 12:25:25 -07001053 pplat = dev_get_parent_platdata(dev);
Bin Meng1f6b08b2018-08-03 01:14:36 -07001054
1055 /* Extract vendor id and device id if available */
1056 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1057
1058 /* Extract the devfn from fdt_pci_addr */
Stefan Roeseb5214202019-01-25 11:52:42 +01001059 pplat->devfn = pci_get_devfn(dev);
Simon Glassff3e0772015-03-05 12:25:25 -07001060
1061 return 0;
1062}
1063
Simon Glassc4e72c42020-01-27 08:49:37 -07001064static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng4d8615c2015-07-19 00:20:04 +08001065 uint offset, ulong *valuep,
1066 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001067{
1068 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001069
1070 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1071}
1072
Bin Meng4d8615c2015-07-19 00:20:04 +08001073static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1074 uint offset, ulong value,
1075 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001076{
1077 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001078
1079 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1080}
1081
Simon Glass76c3fbc2015-08-10 07:05:04 -06001082static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1083{
1084 struct udevice *dev;
1085 int ret = 0;
1086
1087 /*
1088 * Scan through all the PCI controllers. On x86 there will only be one
1089 * but that is not necessarily true on other hardware.
1090 */
1091 do {
1092 device_find_first_child(bus, &dev);
1093 if (dev) {
1094 *devp = dev;
1095 return 0;
1096 }
1097 ret = uclass_next_device(&bus);
1098 if (ret)
1099 return ret;
1100 } while (bus);
1101
1102 return 0;
1103}
1104
1105int pci_find_next_device(struct udevice **devp)
1106{
1107 struct udevice *child = *devp;
1108 struct udevice *bus = child->parent;
1109 int ret;
1110
1111 /* First try all the siblings */
1112 *devp = NULL;
1113 while (child) {
1114 device_find_next_child(&child);
1115 if (child) {
1116 *devp = child;
1117 return 0;
1118 }
1119 }
1120
1121 /* We ran out of siblings. Try the next bus */
1122 ret = uclass_next_device(&bus);
1123 if (ret)
1124 return ret;
1125
1126 return bus ? skip_to_next_device(bus, devp) : 0;
1127}
1128
1129int pci_find_first_device(struct udevice **devp)
1130{
1131 struct udevice *bus;
1132 int ret;
1133
1134 *devp = NULL;
1135 ret = uclass_first_device(UCLASS_PCI, &bus);
1136 if (ret)
1137 return ret;
1138
1139 return skip_to_next_device(bus, devp);
1140}
1141
Simon Glass9289db62015-11-19 20:26:59 -07001142ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1143{
1144 switch (size) {
1145 case PCI_SIZE_8:
1146 return (value >> ((offset & 3) * 8)) & 0xff;
1147 case PCI_SIZE_16:
1148 return (value >> ((offset & 2) * 8)) & 0xffff;
1149 default:
1150 return value;
1151 }
1152}
1153
1154ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1155 enum pci_size_t size)
1156{
1157 uint off_mask;
1158 uint val_mask, shift;
1159 ulong ldata, mask;
1160
1161 switch (size) {
1162 case PCI_SIZE_8:
1163 off_mask = 3;
1164 val_mask = 0xff;
1165 break;
1166 case PCI_SIZE_16:
1167 off_mask = 2;
1168 val_mask = 0xffff;
1169 break;
1170 default:
1171 return value;
1172 }
1173 shift = (offset & off_mask) * 8;
1174 ldata = (value & val_mask) << shift;
1175 mask = val_mask << shift;
1176 value = (old & ~mask) | ldata;
1177
1178 return value;
1179}
1180
Simon Glassf9260332015-11-19 20:27:01 -07001181int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1182 struct pci_region **memp, struct pci_region **prefp)
1183{
1184 struct udevice *bus = pci_get_controller(dev);
1185 struct pci_controller *hose = dev_get_uclass_priv(bus);
1186 int i;
1187
1188 *iop = NULL;
1189 *memp = NULL;
1190 *prefp = NULL;
1191 for (i = 0; i < hose->region_count; i++) {
1192 switch (hose->regions[i].flags) {
1193 case PCI_REGION_IO:
1194 if (!*iop || (*iop)->size < hose->regions[i].size)
1195 *iop = hose->regions + i;
1196 break;
1197 case PCI_REGION_MEM:
1198 if (!*memp || (*memp)->size < hose->regions[i].size)
1199 *memp = hose->regions + i;
1200 break;
1201 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1202 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1203 *prefp = hose->regions + i;
1204 break;
1205 }
1206 }
1207
1208 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1209}
1210
Simon Glass194fca92020-01-27 08:49:38 -07001211u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glassbab17cf2015-11-29 13:17:53 -07001212{
1213 u32 addr;
1214 int bar;
1215
1216 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1217 dm_pci_read_config32(dev, bar, &addr);
Simon Glass9ece4b02020-04-09 10:27:36 -06001218
1219 /*
1220 * If we get an invalid address, return this so that comparisons with
1221 * FDT_ADDR_T_NONE work correctly
1222 */
1223 if (addr == 0xffffffff)
1224 return addr;
1225 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glassbab17cf2015-11-29 13:17:53 -07001226 return addr & PCI_BASE_ADDRESS_IO_MASK;
1227 else
1228 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1229}
1230
Simon Glass9d731c82016-01-18 20:19:15 -07001231void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1232{
1233 int bar;
1234
1235 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1236 dm_pci_write_config32(dev, bar, addr);
1237}
1238
Simon Glass21d1fe72015-11-29 13:18:03 -07001239static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1240 pci_addr_t bus_addr, unsigned long flags,
1241 unsigned long skip_mask, phys_addr_t *pa)
1242{
1243 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1244 struct pci_region *res;
1245 int i;
1246
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001247 if (hose->region_count == 0) {
1248 *pa = bus_addr;
1249 return 0;
1250 }
1251
Simon Glass21d1fe72015-11-29 13:18:03 -07001252 for (i = 0; i < hose->region_count; i++) {
1253 res = &hose->regions[i];
1254
1255 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1256 continue;
1257
1258 if (res->flags & skip_mask)
1259 continue;
1260
1261 if (bus_addr >= res->bus_start &&
1262 (bus_addr - res->bus_start) < res->size) {
1263 *pa = (bus_addr - res->bus_start + res->phys_start);
1264 return 0;
1265 }
1266 }
1267
1268 return 1;
1269}
1270
1271phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1272 unsigned long flags)
1273{
1274 phys_addr_t phys_addr = 0;
1275 struct udevice *ctlr;
1276 int ret;
1277
1278 /* The root controller has the region information */
1279 ctlr = pci_get_controller(dev);
1280
1281 /*
1282 * if PCI_REGION_MEM is set we do a two pass search with preference
1283 * on matches that don't have PCI_REGION_SYS_MEMORY set
1284 */
1285 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1286 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1287 flags, PCI_REGION_SYS_MEMORY,
1288 &phys_addr);
1289 if (!ret)
1290 return phys_addr;
1291 }
1292
1293 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1294
1295 if (ret)
1296 puts("pci_hose_bus_to_phys: invalid physical address\n");
1297
1298 return phys_addr;
1299}
1300
1301int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1302 unsigned long flags, unsigned long skip_mask,
1303 pci_addr_t *ba)
1304{
1305 struct pci_region *res;
1306 struct udevice *ctlr;
1307 pci_addr_t bus_addr;
1308 int i;
1309 struct pci_controller *hose;
1310
1311 /* The root controller has the region information */
1312 ctlr = pci_get_controller(dev);
1313 hose = dev_get_uclass_priv(ctlr);
1314
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001315 if (hose->region_count == 0) {
1316 *ba = phys_addr;
1317 return 0;
1318 }
1319
Simon Glass21d1fe72015-11-29 13:18:03 -07001320 for (i = 0; i < hose->region_count; i++) {
1321 res = &hose->regions[i];
1322
1323 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1324 continue;
1325
1326 if (res->flags & skip_mask)
1327 continue;
1328
1329 bus_addr = phys_addr - res->phys_start + res->bus_start;
1330
1331 if (bus_addr >= res->bus_start &&
1332 (bus_addr - res->bus_start) < res->size) {
1333 *ba = bus_addr;
1334 return 0;
1335 }
1336 }
1337
1338 return 1;
1339}
1340
1341pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1342 unsigned long flags)
1343{
1344 pci_addr_t bus_addr = 0;
1345 int ret;
1346
1347 /*
1348 * if PCI_REGION_MEM is set we do a two pass search with preference
1349 * on matches that don't have PCI_REGION_SYS_MEMORY set
1350 */
1351 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1352 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1353 PCI_REGION_SYS_MEMORY, &bus_addr);
1354 if (!ret)
1355 return bus_addr;
1356 }
1357
1358 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1359
1360 if (ret)
1361 puts("pci_hose_phys_to_bus: invalid physical address\n");
1362
1363 return bus_addr;
1364}
1365
Alex Marginean0b143d82019-06-07 11:24:23 +03001366static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1367 int ea_off)
1368{
1369 int ea_cnt, i, entry_size;
1370 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1371 u32 ea_entry;
1372 phys_addr_t addr;
1373
1374 /* EA capability structure header */
1375 dm_pci_read_config32(dev, ea_off, &ea_entry);
1376 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1377 ea_off += PCI_EA_FIRST_ENT;
1378
1379 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1380 /* Entry header */
1381 dm_pci_read_config32(dev, ea_off, &ea_entry);
1382 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1383
1384 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1385 continue;
1386
1387 /* Base address, 1st DW */
1388 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1389 addr = ea_entry & PCI_EA_FIELD_MASK;
1390 if (ea_entry & PCI_EA_IS_64) {
1391 /* Base address, 2nd DW, skip over 4B MaxOffset */
1392 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1393 addr |= ((u64)ea_entry) << 32;
1394 }
1395
1396 /* size ignored for now */
1397 return map_physmem(addr, flags, 0);
1398 }
1399
1400 return 0;
1401}
1402
Simon Glass21d1fe72015-11-29 13:18:03 -07001403void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1404{
1405 pci_addr_t pci_bus_addr;
1406 u32 bar_response;
Alex Marginean0b143d82019-06-07 11:24:23 +03001407 int ea_off;
1408
1409 /*
1410 * if the function supports Enhanced Allocation use that instead of
1411 * BARs
1412 */
1413 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1414 if (ea_off)
1415 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
Simon Glass21d1fe72015-11-29 13:18:03 -07001416
1417 /* read BAR address */
1418 dm_pci_read_config32(dev, bar, &bar_response);
1419 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1420
1421 /*
1422 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1423 * isn't actualy used on any platform because u-boot assumes a static
1424 * linear mapping. In the future, this could read the BAR size
1425 * and pass that as the size if needed.
1426 */
1427 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1428}
1429
Bin Menga8c5f8d2018-10-15 02:21:21 -07001430static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001431{
Bin Mengdac01fd2018-08-03 01:14:52 -07001432 int ttl = PCI_FIND_CAP_TTL;
1433 u8 id;
1434 u16 ent;
Bin Mengdac01fd2018-08-03 01:14:52 -07001435
1436 dm_pci_read_config8(dev, pos, &pos);
Bin Menga8c5f8d2018-10-15 02:21:21 -07001437
Bin Mengdac01fd2018-08-03 01:14:52 -07001438 while (ttl--) {
1439 if (pos < PCI_STD_HEADER_SIZEOF)
1440 break;
1441 pos &= ~3;
1442 dm_pci_read_config16(dev, pos, &ent);
1443
1444 id = ent & 0xff;
1445 if (id == 0xff)
1446 break;
1447 if (id == cap)
1448 return pos;
1449 pos = (ent >> 8);
1450 }
1451
1452 return 0;
1453}
1454
Bin Menga8c5f8d2018-10-15 02:21:21 -07001455int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1456{
1457 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1458 cap);
1459}
1460
1461int dm_pci_find_capability(struct udevice *dev, int cap)
1462{
1463 u16 status;
1464 u8 header_type;
1465 u8 pos;
1466
1467 dm_pci_read_config16(dev, PCI_STATUS, &status);
1468 if (!(status & PCI_STATUS_CAP_LIST))
1469 return 0;
1470
1471 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1472 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1473 pos = PCI_CB_CAPABILITY_LIST;
1474 else
1475 pos = PCI_CAPABILITY_LIST;
1476
1477 return _dm_pci_find_next_capability(dev, pos, cap);
1478}
1479
1480int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001481{
1482 u32 header;
1483 int ttl;
1484 int pos = PCI_CFG_SPACE_SIZE;
1485
1486 /* minimum 8 bytes per capability */
1487 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1488
Bin Menga8c5f8d2018-10-15 02:21:21 -07001489 if (start)
1490 pos = start;
1491
Bin Mengdac01fd2018-08-03 01:14:52 -07001492 dm_pci_read_config32(dev, pos, &header);
1493 /*
1494 * If we have no capabilities, this is indicated by cap ID,
1495 * cap version and next pointer all being 0.
1496 */
1497 if (header == 0)
1498 return 0;
1499
1500 while (ttl--) {
1501 if (PCI_EXT_CAP_ID(header) == cap)
1502 return pos;
1503
1504 pos = PCI_EXT_CAP_NEXT(header);
1505 if (pos < PCI_CFG_SPACE_SIZE)
1506 break;
1507
1508 dm_pci_read_config32(dev, pos, &header);
1509 }
1510
1511 return 0;
1512}
1513
Bin Menga8c5f8d2018-10-15 02:21:21 -07001514int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1515{
1516 return dm_pci_find_next_ext_capability(dev, 0, cap);
1517}
1518
Alex Margineanb8e1f822019-06-07 11:24:25 +03001519int dm_pci_flr(struct udevice *dev)
1520{
1521 int pcie_off;
1522 u32 cap;
1523
1524 /* look for PCI Express Capability */
1525 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1526 if (!pcie_off)
1527 return -ENOENT;
1528
1529 /* check FLR capability */
1530 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1531 if (!(cap & PCI_EXP_DEVCAP_FLR))
1532 return -ENOENT;
1533
1534 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1535 PCI_EXP_DEVCTL_BCR_FLR);
1536
1537 /* wait 100ms, per PCI spec */
1538 mdelay(100);
1539
1540 return 0;
1541}
1542
Simon Glassff3e0772015-03-05 12:25:25 -07001543UCLASS_DRIVER(pci) = {
1544 .id = UCLASS_PCI,
1545 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -06001546 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -06001547 .post_bind = dm_scan_fdt_dev,
Simon Glassff3e0772015-03-05 12:25:25 -07001548 .pre_probe = pci_uclass_pre_probe,
1549 .post_probe = pci_uclass_post_probe,
1550 .child_post_bind = pci_uclass_child_post_bind,
1551 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1552 .per_child_platdata_auto_alloc_size =
1553 sizeof(struct pci_child_platdata),
1554};
1555
1556static const struct dm_pci_ops pci_bridge_ops = {
1557 .read_config = pci_bridge_read_config,
1558 .write_config = pci_bridge_write_config,
1559};
1560
1561static const struct udevice_id pci_bridge_ids[] = {
1562 { .compatible = "pci-bridge" },
1563 { }
1564};
1565
1566U_BOOT_DRIVER(pci_bridge_drv) = {
1567 .name = "pci_bridge_drv",
1568 .id = UCLASS_PCI,
1569 .of_match = pci_bridge_ids,
1570 .ops = &pci_bridge_ops,
1571};
1572
1573UCLASS_DRIVER(pci_generic) = {
1574 .id = UCLASS_PCI_GENERIC,
1575 .name = "pci_generic",
1576};
1577
1578static const struct udevice_id pci_generic_ids[] = {
1579 { .compatible = "pci-generic" },
1580 { }
1581};
1582
1583U_BOOT_DRIVER(pci_generic_drv) = {
1584 .name = "pci_generic_drv",
1585 .id = UCLASS_PCI_GENERIC,
1586 .of_match = pci_generic_ids,
1587};
Stephen Warrene578b922016-01-26 11:10:11 -07001588
1589void pci_init(void)
1590{
1591 struct udevice *bus;
1592
1593 /*
1594 * Enumerate all known controller devices. Enumeration has the side-
1595 * effect of probing them, so PCIe devices will be enumerated too.
1596 */
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001597 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warrene578b922016-01-26 11:10:11 -07001598 bus;
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001599 uclass_next_device_check(&bus)) {
Stephen Warrene578b922016-01-26 11:10:11 -07001600 ;
1601 }
1602}