blob: 5f318120e16a81806b3c4cfee4f8ad16f8504c1d [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 Glassff3e0772015-03-05 12:25:25 -070010#include <pci.h>
Simon Glass21d1fe72015-11-29 13:18:03 -070011#include <asm/io.h>
Simon Glassff3e0772015-03-05 12:25:25 -070012#include <dm/device-internal.h>
Simon Glassbf501592017-05-18 20:09:51 -060013#include <dm/lists.h>
Bin Meng348b7442015-08-20 06:40:23 -070014#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glass07f2f582019-08-24 14:19:05 -060015#include <asm/fsp/fsp_support.h>
Bin Meng348b7442015-08-20 06:40:23 -070016#endif
Simon Glass5e23b8b2015-11-29 13:17:49 -070017#include "pci_internal.h"
Simon Glassff3e0772015-03-05 12:25:25 -070018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassa6eb93b2016-01-18 20:19:14 -070021int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass983c6ba22015-08-31 18:55:35 -060022{
23 int ret;
24
25 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
26
27 /* Since buses may not be numbered yet try a little harder with bus 0 */
28 if (ret == -ENODEV) {
Simon Glass3f603cb2016-02-11 13:23:26 -070029 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass983c6ba22015-08-31 18:55:35 -060030 if (ret)
31 return ret;
Simon Glass983c6ba22015-08-31 18:55:35 -060032 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
33 }
34
35 return ret;
36}
37
Simon Glass9f60fb02015-11-19 20:27:00 -070038struct udevice *pci_get_controller(struct udevice *dev)
39{
40 while (device_is_on_pci_bus(dev))
41 dev = dev->parent;
42
43 return dev;
44}
45
Simon Glass194fca92020-01-27 08:49:38 -070046pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060047{
48 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
49 struct udevice *bus = dev->parent;
50
Simon Glass48862872019-12-29 21:19:14 -070051 /*
52 * This error indicates that @dev is a device on an unprobed PCI bus.
53 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
54 * will produce a bad BDF>
55 *
56 * A common cause of this problem is that this function is called in the
57 * ofdata_to_platdata() method of @dev. Accessing the PCI bus in that
58 * method is not allowed, since it has not yet been probed. To fix this,
59 * move that access to the probe() method of @dev instead.
60 */
61 if (!device_active(bus))
62 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
63 bus->name);
Simon Glass4b515e42015-07-06 16:47:46 -060064 return PCI_ADD_BUS(bus->seq, pplat->devfn);
65}
66
Simon Glassff3e0772015-03-05 12:25:25 -070067/**
68 * pci_get_bus_max() - returns the bus number of the last active bus
69 *
70 * @return last bus number, or -1 if no active buses
71 */
72static int pci_get_bus_max(void)
73{
74 struct udevice *bus;
75 struct uclass *uc;
76 int ret = -1;
77
78 ret = uclass_get(UCLASS_PCI, &uc);
79 uclass_foreach_dev(bus, uc) {
80 if (bus->seq > ret)
81 ret = bus->seq;
82 }
83
84 debug("%s: ret=%d\n", __func__, ret);
85
86 return ret;
87}
88
89int pci_last_busno(void)
90{
Bin Meng069155c2015-10-01 00:36:01 -070091 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -070092}
93
94int pci_get_ff(enum pci_size_t size)
95{
96 switch (size) {
97 case PCI_SIZE_8:
98 return 0xff;
99 case PCI_SIZE_16:
100 return 0xffff;
101 default:
102 return 0xffffffff;
103 }
104}
105
Marek Vasut02e4d382018-10-10 21:27:06 +0200106static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
107 ofnode *rnode)
108{
109 struct fdt_pci_addr addr;
110 ofnode node;
111 int ret;
112
113 dev_for_each_subnode(node, bus) {
114 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
115 &addr);
116 if (ret)
117 continue;
118
119 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
120 continue;
121
122 *rnode = node;
123 break;
124 }
125};
126
Simon Glassc4e72c42020-01-27 08:49:37 -0700127int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassff3e0772015-03-05 12:25:25 -0700128 struct udevice **devp)
129{
130 struct udevice *dev;
131
132 for (device_find_first_child(bus, &dev);
133 dev;
134 device_find_next_child(&dev)) {
135 struct pci_child_platdata *pplat;
136
137 pplat = dev_get_parent_platdata(dev);
138 if (pplat && pplat->devfn == find_devfn) {
139 *devp = dev;
140 return 0;
141 }
142 }
143
144 return -ENODEV;
145}
146
Simon Glassf3f1fae2015-11-29 13:17:48 -0700147int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700148{
149 struct udevice *bus;
150 int ret;
151
Simon Glass983c6ba22015-08-31 18:55:35 -0600152 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700153 if (ret)
154 return ret;
155 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
156}
157
158static int pci_device_matches_ids(struct udevice *dev,
159 struct pci_device_id *ids)
160{
161 struct pci_child_platdata *pplat;
162 int i;
163
164 pplat = dev_get_parent_platdata(dev);
165 if (!pplat)
166 return -EINVAL;
167 for (i = 0; ids[i].vendor != 0; i++) {
168 if (pplat->vendor == ids[i].vendor &&
169 pplat->device == ids[i].device)
170 return i;
171 }
172
173 return -EINVAL;
174}
175
176int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
177 int *indexp, struct udevice **devp)
178{
179 struct udevice *dev;
180
181 /* Scan all devices on this bus */
182 for (device_find_first_child(bus, &dev);
183 dev;
184 device_find_next_child(&dev)) {
185 if (pci_device_matches_ids(dev, ids) >= 0) {
186 if ((*indexp)-- <= 0) {
187 *devp = dev;
188 return 0;
189 }
190 }
191 }
192
193 return -ENODEV;
194}
195
196int pci_find_device_id(struct pci_device_id *ids, int index,
197 struct udevice **devp)
198{
199 struct udevice *bus;
200
201 /* Scan all known buses */
202 for (uclass_first_device(UCLASS_PCI, &bus);
203 bus;
204 uclass_next_device(&bus)) {
205 if (!pci_bus_find_devices(bus, ids, &index, devp))
206 return 0;
207 }
208 *devp = NULL;
209
210 return -ENODEV;
211}
212
Simon Glass5c0bf642015-11-29 13:17:50 -0700213static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
214 unsigned int device, int *indexp,
215 struct udevice **devp)
216{
217 struct pci_child_platdata *pplat;
218 struct udevice *dev;
219
220 for (device_find_first_child(bus, &dev);
221 dev;
222 device_find_next_child(&dev)) {
223 pplat = dev_get_parent_platdata(dev);
224 if (pplat->vendor == vendor && pplat->device == device) {
225 if (!(*indexp)--) {
226 *devp = dev;
227 return 0;
228 }
229 }
230 }
231
232 return -ENODEV;
233}
234
235int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
236 struct udevice **devp)
237{
238 struct udevice *bus;
239
240 /* Scan all known buses */
241 for (uclass_first_device(UCLASS_PCI, &bus);
242 bus;
243 uclass_next_device(&bus)) {
244 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
245 return device_probe(*devp);
246 }
247 *devp = NULL;
248
249 return -ENODEV;
250}
251
Simon Glassa0eb8352015-11-29 13:17:52 -0700252int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
253{
254 struct udevice *dev;
255
256 /* Scan all known buses */
257 for (pci_find_first_device(&dev);
258 dev;
259 pci_find_next_device(&dev)) {
260 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
261
262 if (pplat->class == find_class && !index--) {
263 *devp = dev;
264 return device_probe(*devp);
265 }
266 }
267 *devp = NULL;
268
269 return -ENODEV;
270}
271
Simon Glassff3e0772015-03-05 12:25:25 -0700272int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
273 unsigned long value, enum pci_size_t size)
274{
275 struct dm_pci_ops *ops;
276
277 ops = pci_get_ops(bus);
278 if (!ops->write_config)
279 return -ENOSYS;
280 return ops->write_config(bus, bdf, offset, value, size);
281}
282
Simon Glass319dba12016-03-06 19:27:52 -0700283int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
284 u32 clr, u32 set)
285{
286 ulong val;
287 int ret;
288
289 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
290 if (ret)
291 return ret;
292 val &= ~clr;
293 val |= set;
294
295 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
296}
297
Simon Glassff3e0772015-03-05 12:25:25 -0700298int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
299 enum pci_size_t size)
300{
301 struct udevice *bus;
302 int ret;
303
Simon Glass983c6ba22015-08-31 18:55:35 -0600304 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700305 if (ret)
306 return ret;
307
Bin Meng4d8615c2015-07-19 00:20:04 +0800308 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700309}
310
Simon Glass66afb4e2015-08-10 07:05:03 -0600311int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
312 enum pci_size_t size)
313{
314 struct udevice *bus;
315
Bin Meng1e0f2262015-09-11 03:24:34 -0700316 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600317 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700318 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
319 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600320}
321
Simon Glassff3e0772015-03-05 12:25:25 -0700322int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
323{
324 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
325}
326
327int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
328{
329 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
330}
331
332int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
333{
334 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
335}
336
Simon Glass66afb4e2015-08-10 07:05:03 -0600337int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
338{
339 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
340}
341
342int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
343{
344 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
345}
346
347int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
348{
349 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
350}
351
Simon Glass194fca92020-01-27 08:49:38 -0700352int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassff3e0772015-03-05 12:25:25 -0700353 unsigned long *valuep, enum pci_size_t size)
354{
355 struct dm_pci_ops *ops;
356
357 ops = pci_get_ops(bus);
358 if (!ops->read_config)
359 return -ENOSYS;
360 return ops->read_config(bus, bdf, offset, valuep, size);
361}
362
363int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
364 enum pci_size_t size)
365{
366 struct udevice *bus;
367 int ret;
368
Simon Glass983c6ba22015-08-31 18:55:35 -0600369 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700370 if (ret)
371 return ret;
372
Bin Meng4d8615c2015-07-19 00:20:04 +0800373 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700374}
375
Simon Glass194fca92020-01-27 08:49:38 -0700376int dm_pci_read_config(const struct udevice *dev, int offset,
377 unsigned long *valuep, enum pci_size_t size)
Simon Glass66afb4e2015-08-10 07:05:03 -0600378{
Simon Glass194fca92020-01-27 08:49:38 -0700379 const struct udevice *bus;
Simon Glass66afb4e2015-08-10 07:05:03 -0600380
Bin Meng1e0f2262015-09-11 03:24:34 -0700381 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600382 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700383 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600384 size);
385}
386
Simon Glassff3e0772015-03-05 12:25:25 -0700387int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
388{
389 unsigned long value;
390 int ret;
391
392 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
393 if (ret)
394 return ret;
395 *valuep = value;
396
397 return 0;
398}
399
400int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
401{
402 unsigned long value;
403 int ret;
404
405 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
406 if (ret)
407 return ret;
408 *valuep = value;
409
410 return 0;
411}
412
413int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
414{
415 unsigned long value;
416 int ret;
417
418 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
419 if (ret)
420 return ret;
421 *valuep = value;
422
423 return 0;
424}
425
Simon Glass194fca92020-01-27 08:49:38 -0700426int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600427{
428 unsigned long value;
429 int ret;
430
431 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
432 if (ret)
433 return ret;
434 *valuep = value;
435
436 return 0;
437}
438
Simon Glass194fca92020-01-27 08:49:38 -0700439int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600440{
441 unsigned long value;
442 int ret;
443
444 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
445 if (ret)
446 return ret;
447 *valuep = value;
448
449 return 0;
450}
451
Simon Glass194fca92020-01-27 08:49:38 -0700452int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600453{
454 unsigned long value;
455 int ret;
456
457 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
458 if (ret)
459 return ret;
460 *valuep = value;
461
462 return 0;
463}
464
Simon Glass319dba12016-03-06 19:27:52 -0700465int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
466{
467 u8 val;
468 int ret;
469
470 ret = dm_pci_read_config8(dev, offset, &val);
471 if (ret)
472 return ret;
473 val &= ~clr;
474 val |= set;
475
476 return dm_pci_write_config8(dev, offset, val);
477}
478
479int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
480{
481 u16 val;
482 int ret;
483
484 ret = dm_pci_read_config16(dev, offset, &val);
485 if (ret)
486 return ret;
487 val &= ~clr;
488 val |= set;
489
490 return dm_pci_write_config16(dev, offset, val);
491}
492
493int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
494{
495 u32 val;
496 int ret;
497
498 ret = dm_pci_read_config32(dev, offset, &val);
499 if (ret)
500 return ret;
501 val &= ~clr;
502 val |= set;
503
504 return dm_pci_write_config32(dev, offset, val);
505}
506
Bin Mengbbbcb522015-10-01 00:36:02 -0700507static void set_vga_bridge_bits(struct udevice *dev)
508{
509 struct udevice *parent = dev->parent;
510 u16 bc;
511
512 while (parent->seq != 0) {
513 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
514 bc |= PCI_BRIDGE_CTL_VGA;
515 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
516 parent = parent->parent;
517 }
518}
519
Simon Glassff3e0772015-03-05 12:25:25 -0700520int pci_auto_config_devices(struct udevice *bus)
521{
522 struct pci_controller *hose = bus->uclass_priv;
Bin Mengbbbcb522015-10-01 00:36:02 -0700523 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700524 unsigned int sub_bus;
525 struct udevice *dev;
526 int ret;
527
528 sub_bus = bus->seq;
529 debug("%s: start\n", __func__);
530 pciauto_config_init(hose);
531 for (ret = device_find_first_child(bus, &dev);
532 !ret && dev;
533 ret = device_find_next_child(&dev)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700534 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600535 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700536
Simon Glassff3e0772015-03-05 12:25:25 -0700537 debug("%s: device %s\n", __func__, dev->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700538 ret = dm_pciauto_config_device(dev);
Simon Glass4d214552015-09-08 17:52:47 -0600539 if (ret < 0)
540 return ret;
541 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700542 sub_bus = max(sub_bus, max_bus);
Bin Mengbbbcb522015-10-01 00:36:02 -0700543
544 pplat = dev_get_parent_platdata(dev);
545 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
546 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700547 }
548 debug("%s: done\n", __func__);
549
550 return sub_bus;
551}
552
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300553int pci_generic_mmap_write_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700554 const struct udevice *bus,
555 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
556 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300557 pci_dev_t bdf,
558 uint offset,
559 ulong value,
560 enum pci_size_t size)
561{
562 void *address;
563
564 if (addr_f(bus, bdf, offset, &address) < 0)
565 return 0;
566
567 switch (size) {
568 case PCI_SIZE_8:
569 writeb(value, address);
570 return 0;
571 case PCI_SIZE_16:
572 writew(value, address);
573 return 0;
574 case PCI_SIZE_32:
575 writel(value, address);
576 return 0;
577 default:
578 return -EINVAL;
579 }
580}
581
582int pci_generic_mmap_read_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700583 const struct udevice *bus,
584 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
585 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300586 pci_dev_t bdf,
587 uint offset,
588 ulong *valuep,
589 enum pci_size_t size)
590{
591 void *address;
592
593 if (addr_f(bus, bdf, offset, &address) < 0) {
594 *valuep = pci_get_ff(size);
595 return 0;
596 }
597
598 switch (size) {
599 case PCI_SIZE_8:
600 *valuep = readb(address);
601 return 0;
602 case PCI_SIZE_16:
603 *valuep = readw(address);
604 return 0;
605 case PCI_SIZE_32:
606 *valuep = readl(address);
607 return 0;
608 default:
609 return -EINVAL;
610 }
611}
612
Simon Glass5e23b8b2015-11-29 13:17:49 -0700613int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassff3e0772015-03-05 12:25:25 -0700614{
Simon Glassff3e0772015-03-05 12:25:25 -0700615 int sub_bus;
616 int ret;
617
618 debug("%s\n", __func__);
Simon Glassff3e0772015-03-05 12:25:25 -0700619
620 sub_bus = pci_get_bus_max() + 1;
621 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700622 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700623
624 ret = device_probe(bus);
625 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600626 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700627 ret);
628 return ret;
629 }
630 if (sub_bus != bus->seq) {
631 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
632 __func__, bus->name, bus->seq, sub_bus);
633 return -EPIPE;
634 }
635 sub_bus = pci_get_bus_max();
Simon Glass5e23b8b2015-11-29 13:17:49 -0700636 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700637
638 return sub_bus;
639}
640
Simon Glassaba92962015-07-06 16:47:44 -0600641/**
642 * pci_match_one_device - Tell if a PCI device structure has a matching
643 * PCI device id structure
644 * @id: single PCI device id structure to match
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800645 * @find: the PCI device id structure to match against
Simon Glassaba92962015-07-06 16:47:44 -0600646 *
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800647 * Returns true if the finding pci_device_id structure matched or false if
648 * there is no match.
Simon Glassaba92962015-07-06 16:47:44 -0600649 */
650static bool pci_match_one_id(const struct pci_device_id *id,
651 const struct pci_device_id *find)
652{
653 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
654 (id->device == PCI_ANY_ID || id->device == find->device) &&
655 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
656 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
657 !((id->class ^ find->class) & id->class_mask))
658 return true;
659
660 return false;
661}
662
663/**
664 * pci_find_and_bind_driver() - Find and bind the right PCI driver
665 *
666 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600667 *
668 * @parent: Parent bus
669 * @find_id: Specification of the driver to find
670 * @bdf: Bus/device/function addreess - see PCI_BDF()
671 * @devp: Returns a pointer to the device created
672 * @return 0 if OK, -EPERM if the device is not needed before relocation and
673 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600674 */
675static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600676 struct pci_device_id *find_id,
677 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600678{
679 struct pci_driver_entry *start, *entry;
Marek Vasut02e4d382018-10-10 21:27:06 +0200680 ofnode node = ofnode_null();
Simon Glassaba92962015-07-06 16:47:44 -0600681 const char *drv;
682 int n_ents;
683 int ret;
684 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700685 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600686
687 *devp = NULL;
688
689 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
690 find_id->vendor, find_id->device);
Marek Vasut02e4d382018-10-10 21:27:06 +0200691
692 /* Determine optional OF node */
693 pci_dev_find_ofnode(parent, bdf, &node);
694
Michael Wallea6cd5972019-12-01 17:45:18 +0100695 if (ofnode_valid(node) && !ofnode_is_available(node)) {
696 debug("%s: Ignoring disabled device\n", __func__);
697 return -EPERM;
698 }
699
Simon Glassaba92962015-07-06 16:47:44 -0600700 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
701 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
702 for (entry = start; entry != start + n_ents; entry++) {
703 const struct pci_device_id *id;
704 struct udevice *dev;
705 const struct driver *drv;
706
707 for (id = entry->match;
708 id->vendor || id->subvendor || id->class_mask;
709 id++) {
710 if (!pci_match_one_id(id, find_id))
711 continue;
712
713 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700714
715 /*
716 * In the pre-relocation phase, we only bind devices
717 * whose driver has the DM_FLAG_PRE_RELOC set, to save
718 * precious memory space as on some platforms as that
719 * space is pretty limited (ie: using Cache As RAM).
720 */
721 if (!(gd->flags & GD_FLG_RELOC) &&
722 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600723 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700724
Simon Glassaba92962015-07-06 16:47:44 -0600725 /*
726 * We could pass the descriptor to the driver as
727 * platdata (instead of NULL) and allow its bind()
728 * method to return -ENOENT if it doesn't support this
729 * device. That way we could continue the search to
730 * find another driver. For now this doesn't seem
731 * necesssary, so just bind the first match.
732 */
Marek Vasut02e4d382018-10-10 21:27:06 +0200733 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
734 node, &dev);
Simon Glassaba92962015-07-06 16:47:44 -0600735 if (ret)
736 goto error;
737 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menged698aa2018-08-03 01:14:44 -0700738 dev->driver_data = id->driver_data;
Simon Glassaba92962015-07-06 16:47:44 -0600739 *devp = dev;
740 return 0;
741 }
742 }
743
Bin Meng08fc7b82015-08-20 06:40:17 -0700744 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
745 /*
746 * In the pre-relocation phase, we only bind bridge devices to save
747 * precious memory space as on some platforms as that space is pretty
748 * limited (ie: using Cache As RAM).
749 */
750 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600751 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700752
Simon Glassaba92962015-07-06 16:47:44 -0600753 /* Bind a generic driver so that the device can be used */
Bin Meng4d8615c2015-07-19 00:20:04 +0800754 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
755 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600756 str = strdup(name);
757 if (!str)
758 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700759 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
760
Marek Vasut02e4d382018-10-10 21:27:06 +0200761 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glassaba92962015-07-06 16:47:44 -0600762 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600763 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dec42640c2017-05-08 20:40:16 +0200764 free(str);
Simon Glassaba92962015-07-06 16:47:44 -0600765 return ret;
766 }
767 debug("%s: No match found: bound generic driver instead\n", __func__);
768
769 return 0;
770
771error:
772 debug("%s: No match found: error %d\n", __func__, ret);
773 return ret;
774}
775
Simon Glassff3e0772015-03-05 12:25:25 -0700776int pci_bind_bus_devices(struct udevice *bus)
777{
778 ulong vendor, device;
779 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800780 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700781 bool found_multi;
782 int ret;
783
784 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800785 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
786 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato6d9f5b02016-04-25 15:41:01 +0900787 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng4d8615c2015-07-19 00:20:04 +0800788 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700789 struct pci_child_platdata *pplat;
790 struct udevice *dev;
791 ulong class;
792
Bin Meng64e45f72018-08-03 01:14:37 -0700793 if (!PCI_FUNC(bdf))
794 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800795 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700796 continue;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800797
Simon Glassff3e0772015-03-05 12:25:25 -0700798 /* Check only the first access, we don't expect problems */
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800799 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
800 PCI_SIZE_16);
Simon Glassff3e0772015-03-05 12:25:25 -0700801 if (ret)
802 goto error;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800803
Simon Glassff3e0772015-03-05 12:25:25 -0700804 if (vendor == 0xffff || vendor == 0x0000)
805 continue;
806
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800807 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
808 &header_type, PCI_SIZE_8);
809
Bin Meng4d8615c2015-07-19 00:20:04 +0800810 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700811 found_multi = header_type & 0x80;
812
Simon Glass09115692019-09-25 08:56:12 -0600813 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Bin Meng4d8615c2015-07-19 00:20:04 +0800814 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
815 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700816 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800817 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600818 PCI_SIZE_32);
819 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700820
821 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800822 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass09115692019-09-25 08:56:12 -0600823 debug(": find ret=%d\n", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700824
Simon Glass8bd42522015-11-29 13:18:09 -0700825 /* If nothing in the device tree, bind a device */
Simon Glassff3e0772015-03-05 12:25:25 -0700826 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600827 struct pci_device_id find_id;
828 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700829
Simon Glassaba92962015-07-06 16:47:44 -0600830 memset(&find_id, '\0', sizeof(find_id));
831 find_id.vendor = vendor;
832 find_id.device = device;
833 find_id.class = class;
834 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800835 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600836 PCI_SUBSYSTEM_VENDOR_ID,
837 &val, PCI_SIZE_32);
838 find_id.subvendor = val & 0xffff;
839 find_id.subdevice = val >> 16;
840 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800841 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600842 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700843 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600844 if (ret == -EPERM)
845 continue;
846 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700847 return ret;
848
849 /* Update the platform data */
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600850 pplat = dev_get_parent_platdata(dev);
851 pplat->devfn = PCI_MASK_BUS(bdf);
852 pplat->vendor = vendor;
853 pplat->device = device;
854 pplat->class = class;
Simon Glassff3e0772015-03-05 12:25:25 -0700855 }
856
857 return 0;
858error:
859 printf("Cannot read bus configuration: %d\n", ret);
860
861 return ret;
862}
863
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700864static void decode_regions(struct pci_controller *hose, ofnode parent_node,
865 ofnode node)
Simon Glassff3e0772015-03-05 12:25:25 -0700866{
867 int pci_addr_cells, addr_cells, size_cells;
868 int cells_per_record;
869 const u32 *prop;
870 int len;
871 int i;
872
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900873 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700874 if (!prop) {
875 debug("%s: Cannot decode regions\n", __func__);
876 return;
877 }
878
Simon Glass878d68c2017-06-12 06:21:31 -0600879 pci_addr_cells = ofnode_read_simple_addr_cells(node);
880 addr_cells = ofnode_read_simple_addr_cells(parent_node);
881 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassff3e0772015-03-05 12:25:25 -0700882
883 /* PCI addresses are always 3-cells */
884 len /= sizeof(u32);
885 cells_per_record = pci_addr_cells + addr_cells + size_cells;
886 hose->region_count = 0;
887 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
888 cells_per_record);
889 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
890 u64 pci_addr, addr, size;
891 int space_code;
892 u32 flags;
893 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700894 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700895
896 if (len < cells_per_record)
897 break;
898 flags = fdt32_to_cpu(prop[0]);
899 space_code = (flags >> 24) & 3;
900 pci_addr = fdtdec_get_number(prop + 1, 2);
901 prop += pci_addr_cells;
902 addr = fdtdec_get_number(prop, addr_cells);
903 prop += addr_cells;
904 size = fdtdec_get_number(prop, size_cells);
905 prop += size_cells;
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900906 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
907 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassff3e0772015-03-05 12:25:25 -0700908 if (space_code & 2) {
909 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
910 PCI_REGION_MEM;
911 } else if (space_code & 1) {
912 type = PCI_REGION_IO;
913 } else {
914 continue;
915 }
Tuomas Tynkkynen52ba9072018-05-14 18:47:50 +0300916
917 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
918 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
919 debug(" - beyond the 32-bit boundary, ignoring\n");
920 continue;
921 }
922
Simon Glass9526d832015-11-19 20:26:58 -0700923 pos = -1;
924 for (i = 0; i < hose->region_count; i++) {
925 if (hose->regions[i].flags == type)
926 pos = i;
927 }
928 if (pos == -1)
929 pos = hose->region_count++;
930 debug(" - type=%d, pos=%d\n", type, pos);
931 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -0700932 }
933
934 /* Add a region for our local memory */
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100935#ifdef CONFIG_NR_DRAM_BANKS
936 bd_t *bd = gd->bd;
937
Bin Meng1eaf7802018-03-27 00:46:05 -0700938 if (!bd)
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700939 return;
Bin Meng1eaf7802018-03-27 00:46:05 -0700940
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100941 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
Thierry Redingd94d9aa2019-03-15 16:32:32 +0100942 if (hose->region_count == MAX_PCI_REGIONS) {
943 pr_err("maximum number of regions parsed, aborting\n");
944 break;
945 }
946
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100947 if (bd->bi_dram[i].size) {
948 pci_set_region(hose->regions + hose->region_count++,
949 bd->bi_dram[i].start,
950 bd->bi_dram[i].start,
951 bd->bi_dram[i].size,
952 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
953 }
954 }
955#else
956 phys_addr_t base = 0, size;
957
Simon Glass2084c5a2015-11-19 20:26:57 -0700958 size = gd->ram_size;
959#ifdef CONFIG_SYS_SDRAM_BASE
960 base = CONFIG_SYS_SDRAM_BASE;
961#endif
962 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
963 size = gd->pci_ram_top - base;
Bin Mengee1109b2018-03-27 00:46:06 -0700964 if (size)
965 pci_set_region(hose->regions + hose->region_count++, base,
966 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100967#endif
Simon Glassff3e0772015-03-05 12:25:25 -0700968
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700969 return;
Simon Glassff3e0772015-03-05 12:25:25 -0700970}
971
972static int pci_uclass_pre_probe(struct udevice *bus)
973{
974 struct pci_controller *hose;
Simon Glassff3e0772015-03-05 12:25:25 -0700975
976 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
977 bus->parent->name);
978 hose = bus->uclass_priv;
979
980 /* For bridges, use the top-level PCI controller */
Paul Burton65f62b12016-09-08 07:47:32 +0100981 if (!device_is_on_pci_bus(bus)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700982 hose->ctlr = bus;
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700983 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassff3e0772015-03-05 12:25:25 -0700984 } else {
985 struct pci_controller *parent_hose;
986
987 parent_hose = dev_get_uclass_priv(bus->parent);
988 hose->ctlr = parent_hose->bus;
989 }
990 hose->bus = bus;
991 hose->first_busno = bus->seq;
992 hose->last_busno = bus->seq;
Simon Glass2206ac22019-12-06 21:41:37 -0700993 hose->skip_auto_config_until_reloc =
994 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
Simon Glassff3e0772015-03-05 12:25:25 -0700995
996 return 0;
997}
998
999static int pci_uclass_post_probe(struct udevice *bus)
1000{
Simon Glass2206ac22019-12-06 21:41:37 -07001001 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001002 int ret;
1003
Simon Glassff3e0772015-03-05 12:25:25 -07001004 debug("%s: probing bus %d\n", __func__, bus->seq);
1005 ret = pci_bind_bus_devices(bus);
1006 if (ret)
1007 return ret;
1008
Simon Glass2206ac22019-12-06 21:41:37 -07001009 if (CONFIG_IS_ENABLED(PCI_PNP) &&
1010 (!hose->skip_auto_config_until_reloc ||
1011 (gd->flags & GD_FLG_RELOC))) {
1012 ret = pci_auto_config_devices(bus);
1013 if (ret < 0)
1014 return log_msg_ret("pci auto-config", ret);
1015 }
Simon Glassff3e0772015-03-05 12:25:25 -07001016
Bin Meng348b7442015-08-20 06:40:23 -07001017#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1018 /*
1019 * Per Intel FSP specification, we should call FSP notify API to
1020 * inform FSP that PCI enumeration has been done so that FSP will
1021 * do any necessary initialization as required by the chipset's
1022 * BIOS Writer's Guide (BWG).
1023 *
1024 * Unfortunately we have to put this call here as with driver model,
1025 * the enumeration is all done on a lazy basis as needed, so until
1026 * something is touched on PCI it won't happen.
1027 *
1028 * Note we only call this 1) after U-Boot is relocated, and 2)
1029 * root bus has finished probing.
1030 */
Simon Glass4d214552015-09-08 17:52:47 -06001031 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Meng348b7442015-08-20 06:40:23 -07001032 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -06001033 if (ret)
1034 return ret;
1035 }
Bin Meng348b7442015-08-20 06:40:23 -07001036#endif
1037
Simon Glass4d214552015-09-08 17:52:47 -06001038 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -07001039}
1040
1041static int pci_uclass_child_post_bind(struct udevice *dev)
1042{
1043 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -07001044
Simon Glassbf501592017-05-18 20:09:51 -06001045 if (!dev_of_valid(dev))
Simon Glassff3e0772015-03-05 12:25:25 -07001046 return 0;
1047
Simon Glassff3e0772015-03-05 12:25:25 -07001048 pplat = dev_get_parent_platdata(dev);
Bin Meng1f6b08b2018-08-03 01:14:36 -07001049
1050 /* Extract vendor id and device id if available */
1051 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1052
1053 /* Extract the devfn from fdt_pci_addr */
Stefan Roeseb5214202019-01-25 11:52:42 +01001054 pplat->devfn = pci_get_devfn(dev);
Simon Glassff3e0772015-03-05 12:25:25 -07001055
1056 return 0;
1057}
1058
Simon Glassc4e72c42020-01-27 08:49:37 -07001059static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng4d8615c2015-07-19 00:20:04 +08001060 uint offset, ulong *valuep,
1061 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001062{
1063 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001064
1065 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1066}
1067
Bin Meng4d8615c2015-07-19 00:20:04 +08001068static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1069 uint offset, ulong value,
1070 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001071{
1072 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001073
1074 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1075}
1076
Simon Glass76c3fbc2015-08-10 07:05:04 -06001077static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1078{
1079 struct udevice *dev;
1080 int ret = 0;
1081
1082 /*
1083 * Scan through all the PCI controllers. On x86 there will only be one
1084 * but that is not necessarily true on other hardware.
1085 */
1086 do {
1087 device_find_first_child(bus, &dev);
1088 if (dev) {
1089 *devp = dev;
1090 return 0;
1091 }
1092 ret = uclass_next_device(&bus);
1093 if (ret)
1094 return ret;
1095 } while (bus);
1096
1097 return 0;
1098}
1099
1100int pci_find_next_device(struct udevice **devp)
1101{
1102 struct udevice *child = *devp;
1103 struct udevice *bus = child->parent;
1104 int ret;
1105
1106 /* First try all the siblings */
1107 *devp = NULL;
1108 while (child) {
1109 device_find_next_child(&child);
1110 if (child) {
1111 *devp = child;
1112 return 0;
1113 }
1114 }
1115
1116 /* We ran out of siblings. Try the next bus */
1117 ret = uclass_next_device(&bus);
1118 if (ret)
1119 return ret;
1120
1121 return bus ? skip_to_next_device(bus, devp) : 0;
1122}
1123
1124int pci_find_first_device(struct udevice **devp)
1125{
1126 struct udevice *bus;
1127 int ret;
1128
1129 *devp = NULL;
1130 ret = uclass_first_device(UCLASS_PCI, &bus);
1131 if (ret)
1132 return ret;
1133
1134 return skip_to_next_device(bus, devp);
1135}
1136
Simon Glass9289db62015-11-19 20:26:59 -07001137ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1138{
1139 switch (size) {
1140 case PCI_SIZE_8:
1141 return (value >> ((offset & 3) * 8)) & 0xff;
1142 case PCI_SIZE_16:
1143 return (value >> ((offset & 2) * 8)) & 0xffff;
1144 default:
1145 return value;
1146 }
1147}
1148
1149ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1150 enum pci_size_t size)
1151{
1152 uint off_mask;
1153 uint val_mask, shift;
1154 ulong ldata, mask;
1155
1156 switch (size) {
1157 case PCI_SIZE_8:
1158 off_mask = 3;
1159 val_mask = 0xff;
1160 break;
1161 case PCI_SIZE_16:
1162 off_mask = 2;
1163 val_mask = 0xffff;
1164 break;
1165 default:
1166 return value;
1167 }
1168 shift = (offset & off_mask) * 8;
1169 ldata = (value & val_mask) << shift;
1170 mask = val_mask << shift;
1171 value = (old & ~mask) | ldata;
1172
1173 return value;
1174}
1175
Simon Glassf9260332015-11-19 20:27:01 -07001176int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1177 struct pci_region **memp, struct pci_region **prefp)
1178{
1179 struct udevice *bus = pci_get_controller(dev);
1180 struct pci_controller *hose = dev_get_uclass_priv(bus);
1181 int i;
1182
1183 *iop = NULL;
1184 *memp = NULL;
1185 *prefp = NULL;
1186 for (i = 0; i < hose->region_count; i++) {
1187 switch (hose->regions[i].flags) {
1188 case PCI_REGION_IO:
1189 if (!*iop || (*iop)->size < hose->regions[i].size)
1190 *iop = hose->regions + i;
1191 break;
1192 case PCI_REGION_MEM:
1193 if (!*memp || (*memp)->size < hose->regions[i].size)
1194 *memp = hose->regions + i;
1195 break;
1196 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1197 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1198 *prefp = hose->regions + i;
1199 break;
1200 }
1201 }
1202
1203 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1204}
1205
Simon Glass194fca92020-01-27 08:49:38 -07001206u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glassbab17cf2015-11-29 13:17:53 -07001207{
1208 u32 addr;
1209 int bar;
1210
1211 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1212 dm_pci_read_config32(dev, bar, &addr);
1213 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1214 return addr & PCI_BASE_ADDRESS_IO_MASK;
1215 else
1216 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1217}
1218
Simon Glass9d731c82016-01-18 20:19:15 -07001219void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1220{
1221 int bar;
1222
1223 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1224 dm_pci_write_config32(dev, bar, addr);
1225}
1226
Simon Glass21d1fe72015-11-29 13:18:03 -07001227static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1228 pci_addr_t bus_addr, unsigned long flags,
1229 unsigned long skip_mask, phys_addr_t *pa)
1230{
1231 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1232 struct pci_region *res;
1233 int i;
1234
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001235 if (hose->region_count == 0) {
1236 *pa = bus_addr;
1237 return 0;
1238 }
1239
Simon Glass21d1fe72015-11-29 13:18:03 -07001240 for (i = 0; i < hose->region_count; i++) {
1241 res = &hose->regions[i];
1242
1243 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1244 continue;
1245
1246 if (res->flags & skip_mask)
1247 continue;
1248
1249 if (bus_addr >= res->bus_start &&
1250 (bus_addr - res->bus_start) < res->size) {
1251 *pa = (bus_addr - res->bus_start + res->phys_start);
1252 return 0;
1253 }
1254 }
1255
1256 return 1;
1257}
1258
1259phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1260 unsigned long flags)
1261{
1262 phys_addr_t phys_addr = 0;
1263 struct udevice *ctlr;
1264 int ret;
1265
1266 /* The root controller has the region information */
1267 ctlr = pci_get_controller(dev);
1268
1269 /*
1270 * if PCI_REGION_MEM is set we do a two pass search with preference
1271 * on matches that don't have PCI_REGION_SYS_MEMORY set
1272 */
1273 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1274 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1275 flags, PCI_REGION_SYS_MEMORY,
1276 &phys_addr);
1277 if (!ret)
1278 return phys_addr;
1279 }
1280
1281 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1282
1283 if (ret)
1284 puts("pci_hose_bus_to_phys: invalid physical address\n");
1285
1286 return phys_addr;
1287}
1288
1289int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1290 unsigned long flags, unsigned long skip_mask,
1291 pci_addr_t *ba)
1292{
1293 struct pci_region *res;
1294 struct udevice *ctlr;
1295 pci_addr_t bus_addr;
1296 int i;
1297 struct pci_controller *hose;
1298
1299 /* The root controller has the region information */
1300 ctlr = pci_get_controller(dev);
1301 hose = dev_get_uclass_priv(ctlr);
1302
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001303 if (hose->region_count == 0) {
1304 *ba = phys_addr;
1305 return 0;
1306 }
1307
Simon Glass21d1fe72015-11-29 13:18:03 -07001308 for (i = 0; i < hose->region_count; i++) {
1309 res = &hose->regions[i];
1310
1311 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1312 continue;
1313
1314 if (res->flags & skip_mask)
1315 continue;
1316
1317 bus_addr = phys_addr - res->phys_start + res->bus_start;
1318
1319 if (bus_addr >= res->bus_start &&
1320 (bus_addr - res->bus_start) < res->size) {
1321 *ba = bus_addr;
1322 return 0;
1323 }
1324 }
1325
1326 return 1;
1327}
1328
1329pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1330 unsigned long flags)
1331{
1332 pci_addr_t bus_addr = 0;
1333 int ret;
1334
1335 /*
1336 * if PCI_REGION_MEM is set we do a two pass search with preference
1337 * on matches that don't have PCI_REGION_SYS_MEMORY set
1338 */
1339 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1340 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1341 PCI_REGION_SYS_MEMORY, &bus_addr);
1342 if (!ret)
1343 return bus_addr;
1344 }
1345
1346 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1347
1348 if (ret)
1349 puts("pci_hose_phys_to_bus: invalid physical address\n");
1350
1351 return bus_addr;
1352}
1353
Alex Marginean0b143d82019-06-07 11:24:23 +03001354static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1355 int ea_off)
1356{
1357 int ea_cnt, i, entry_size;
1358 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1359 u32 ea_entry;
1360 phys_addr_t addr;
1361
1362 /* EA capability structure header */
1363 dm_pci_read_config32(dev, ea_off, &ea_entry);
1364 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1365 ea_off += PCI_EA_FIRST_ENT;
1366
1367 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1368 /* Entry header */
1369 dm_pci_read_config32(dev, ea_off, &ea_entry);
1370 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1371
1372 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1373 continue;
1374
1375 /* Base address, 1st DW */
1376 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1377 addr = ea_entry & PCI_EA_FIELD_MASK;
1378 if (ea_entry & PCI_EA_IS_64) {
1379 /* Base address, 2nd DW, skip over 4B MaxOffset */
1380 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1381 addr |= ((u64)ea_entry) << 32;
1382 }
1383
1384 /* size ignored for now */
1385 return map_physmem(addr, flags, 0);
1386 }
1387
1388 return 0;
1389}
1390
Simon Glass21d1fe72015-11-29 13:18:03 -07001391void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1392{
1393 pci_addr_t pci_bus_addr;
1394 u32 bar_response;
Alex Marginean0b143d82019-06-07 11:24:23 +03001395 int ea_off;
1396
1397 /*
1398 * if the function supports Enhanced Allocation use that instead of
1399 * BARs
1400 */
1401 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1402 if (ea_off)
1403 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
Simon Glass21d1fe72015-11-29 13:18:03 -07001404
1405 /* read BAR address */
1406 dm_pci_read_config32(dev, bar, &bar_response);
1407 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1408
1409 /*
1410 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1411 * isn't actualy used on any platform because u-boot assumes a static
1412 * linear mapping. In the future, this could read the BAR size
1413 * and pass that as the size if needed.
1414 */
1415 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1416}
1417
Bin Menga8c5f8d2018-10-15 02:21:21 -07001418static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001419{
Bin Mengdac01fd2018-08-03 01:14:52 -07001420 int ttl = PCI_FIND_CAP_TTL;
1421 u8 id;
1422 u16 ent;
Bin Mengdac01fd2018-08-03 01:14:52 -07001423
1424 dm_pci_read_config8(dev, pos, &pos);
Bin Menga8c5f8d2018-10-15 02:21:21 -07001425
Bin Mengdac01fd2018-08-03 01:14:52 -07001426 while (ttl--) {
1427 if (pos < PCI_STD_HEADER_SIZEOF)
1428 break;
1429 pos &= ~3;
1430 dm_pci_read_config16(dev, pos, &ent);
1431
1432 id = ent & 0xff;
1433 if (id == 0xff)
1434 break;
1435 if (id == cap)
1436 return pos;
1437 pos = (ent >> 8);
1438 }
1439
1440 return 0;
1441}
1442
Bin Menga8c5f8d2018-10-15 02:21:21 -07001443int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1444{
1445 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1446 cap);
1447}
1448
1449int dm_pci_find_capability(struct udevice *dev, int cap)
1450{
1451 u16 status;
1452 u8 header_type;
1453 u8 pos;
1454
1455 dm_pci_read_config16(dev, PCI_STATUS, &status);
1456 if (!(status & PCI_STATUS_CAP_LIST))
1457 return 0;
1458
1459 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1460 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1461 pos = PCI_CB_CAPABILITY_LIST;
1462 else
1463 pos = PCI_CAPABILITY_LIST;
1464
1465 return _dm_pci_find_next_capability(dev, pos, cap);
1466}
1467
1468int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001469{
1470 u32 header;
1471 int ttl;
1472 int pos = PCI_CFG_SPACE_SIZE;
1473
1474 /* minimum 8 bytes per capability */
1475 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1476
Bin Menga8c5f8d2018-10-15 02:21:21 -07001477 if (start)
1478 pos = start;
1479
Bin Mengdac01fd2018-08-03 01:14:52 -07001480 dm_pci_read_config32(dev, pos, &header);
1481 /*
1482 * If we have no capabilities, this is indicated by cap ID,
1483 * cap version and next pointer all being 0.
1484 */
1485 if (header == 0)
1486 return 0;
1487
1488 while (ttl--) {
1489 if (PCI_EXT_CAP_ID(header) == cap)
1490 return pos;
1491
1492 pos = PCI_EXT_CAP_NEXT(header);
1493 if (pos < PCI_CFG_SPACE_SIZE)
1494 break;
1495
1496 dm_pci_read_config32(dev, pos, &header);
1497 }
1498
1499 return 0;
1500}
1501
Bin Menga8c5f8d2018-10-15 02:21:21 -07001502int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1503{
1504 return dm_pci_find_next_ext_capability(dev, 0, cap);
1505}
1506
Alex Margineanb8e1f822019-06-07 11:24:25 +03001507int dm_pci_flr(struct udevice *dev)
1508{
1509 int pcie_off;
1510 u32 cap;
1511
1512 /* look for PCI Express Capability */
1513 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1514 if (!pcie_off)
1515 return -ENOENT;
1516
1517 /* check FLR capability */
1518 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1519 if (!(cap & PCI_EXP_DEVCAP_FLR))
1520 return -ENOENT;
1521
1522 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1523 PCI_EXP_DEVCTL_BCR_FLR);
1524
1525 /* wait 100ms, per PCI spec */
1526 mdelay(100);
1527
1528 return 0;
1529}
1530
Simon Glassff3e0772015-03-05 12:25:25 -07001531UCLASS_DRIVER(pci) = {
1532 .id = UCLASS_PCI,
1533 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -06001534 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -06001535 .post_bind = dm_scan_fdt_dev,
Simon Glassff3e0772015-03-05 12:25:25 -07001536 .pre_probe = pci_uclass_pre_probe,
1537 .post_probe = pci_uclass_post_probe,
1538 .child_post_bind = pci_uclass_child_post_bind,
1539 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1540 .per_child_platdata_auto_alloc_size =
1541 sizeof(struct pci_child_platdata),
1542};
1543
1544static const struct dm_pci_ops pci_bridge_ops = {
1545 .read_config = pci_bridge_read_config,
1546 .write_config = pci_bridge_write_config,
1547};
1548
1549static const struct udevice_id pci_bridge_ids[] = {
1550 { .compatible = "pci-bridge" },
1551 { }
1552};
1553
1554U_BOOT_DRIVER(pci_bridge_drv) = {
1555 .name = "pci_bridge_drv",
1556 .id = UCLASS_PCI,
1557 .of_match = pci_bridge_ids,
1558 .ops = &pci_bridge_ops,
1559};
1560
1561UCLASS_DRIVER(pci_generic) = {
1562 .id = UCLASS_PCI_GENERIC,
1563 .name = "pci_generic",
1564};
1565
1566static const struct udevice_id pci_generic_ids[] = {
1567 { .compatible = "pci-generic" },
1568 { }
1569};
1570
1571U_BOOT_DRIVER(pci_generic_drv) = {
1572 .name = "pci_generic_drv",
1573 .id = UCLASS_PCI_GENERIC,
1574 .of_match = pci_generic_ids,
1575};
Stephen Warrene578b922016-01-26 11:10:11 -07001576
1577void pci_init(void)
1578{
1579 struct udevice *bus;
1580
1581 /*
1582 * Enumerate all known controller devices. Enumeration has the side-
1583 * effect of probing them, so PCIe devices will be enumerated too.
1584 */
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001585 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warrene578b922016-01-26 11:10:11 -07001586 bus;
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001587 uclass_next_device_check(&bus)) {
Stephen Warrene578b922016-01-26 11:10:11 -07001588 ;
1589 }
1590}