blob: e2882e3b634f777afb8a988c55c602d88f51c8a8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb94dc892015-03-05 12:25:25 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassb94dc892015-03-05 12:25:25 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Simon Glassb94dc892015-03-05 12:25:25 -070011#include <pci.h>
Simon Glassc5f053b2015-11-29 13:18:03 -070012#include <asm/io.h>
Simon Glassb94dc892015-03-05 12:25:25 -070013#include <dm/device-internal.h>
Simon Glass89d83232017-05-18 20:09:51 -060014#include <dm/lists.h>
Bin Mengc0820a42015-08-20 06:40:23 -070015#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glassef8a2dd2019-08-24 14:19:05 -060016#include <asm/fsp/fsp_support.h>
Bin Mengc0820a42015-08-20 06:40:23 -070017#endif
Simon Glass37a3f94b2015-11-29 13:17:49 -070018#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass2e4e4432016-01-18 20:19:14 -070022int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-08-31 18:55:35 -060023{
24 int ret;
25
26 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
27
28 /* Since buses may not be numbered yet try a little harder with bus 0 */
29 if (ret == -ENODEV) {
Simon Glassc7298e72016-02-11 13:23:26 -070030 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060031 if (ret)
32 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060033 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
34 }
35
36 return ret;
37}
38
Simon Glass6256d672015-11-19 20:27:00 -070039struct udevice *pci_get_controller(struct udevice *dev)
40{
41 while (device_is_on_pci_bus(dev))
42 dev = dev->parent;
43
44 return dev;
45}
46
Simon Glassc92aac12020-01-27 08:49:38 -070047pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060048{
49 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
50 struct udevice *bus = dev->parent;
51
Simon Glass1c6449c2019-12-29 21:19:14 -070052 /*
53 * This error indicates that @dev is a device on an unprobed PCI bus.
54 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
55 * will produce a bad BDF>
56 *
57 * A common cause of this problem is that this function is called in the
58 * ofdata_to_platdata() method of @dev. Accessing the PCI bus in that
59 * method is not allowed, since it has not yet been probed. To fix this,
60 * move that access to the probe() method of @dev instead.
61 */
62 if (!device_active(bus))
63 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
64 bus->name);
Simon Glassc9118d42015-07-06 16:47:46 -060065 return PCI_ADD_BUS(bus->seq, pplat->devfn);
66}
67
Simon Glassb94dc892015-03-05 12:25:25 -070068/**
69 * pci_get_bus_max() - returns the bus number of the last active bus
70 *
71 * @return last bus number, or -1 if no active buses
72 */
73static int pci_get_bus_max(void)
74{
75 struct udevice *bus;
76 struct uclass *uc;
77 int ret = -1;
78
79 ret = uclass_get(UCLASS_PCI, &uc);
80 uclass_foreach_dev(bus, uc) {
81 if (bus->seq > ret)
82 ret = bus->seq;
83 }
84
85 debug("%s: ret=%d\n", __func__, ret);
86
87 return ret;
88}
89
90int pci_last_busno(void)
91{
Bin Meng5bc3f8a2015-10-01 00:36:01 -070092 return pci_get_bus_max();
Simon Glassb94dc892015-03-05 12:25:25 -070093}
94
95int pci_get_ff(enum pci_size_t size)
96{
97 switch (size) {
98 case PCI_SIZE_8:
99 return 0xff;
100 case PCI_SIZE_16:
101 return 0xffff;
102 default:
103 return 0xffffffff;
104 }
105}
106
Marek Vasutb4535792018-10-10 21:27:06 +0200107static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
108 ofnode *rnode)
109{
110 struct fdt_pci_addr addr;
111 ofnode node;
112 int ret;
113
114 dev_for_each_subnode(node, bus) {
115 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
116 &addr);
117 if (ret)
118 continue;
119
120 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
121 continue;
122
123 *rnode = node;
124 break;
125 }
126};
127
Simon Glass2a311e82020-01-27 08:49:37 -0700128int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassb94dc892015-03-05 12:25:25 -0700129 struct udevice **devp)
130{
131 struct udevice *dev;
132
133 for (device_find_first_child(bus, &dev);
134 dev;
135 device_find_next_child(&dev)) {
136 struct pci_child_platdata *pplat;
137
138 pplat = dev_get_parent_platdata(dev);
139 if (pplat && pplat->devfn == find_devfn) {
140 *devp = dev;
141 return 0;
142 }
143 }
144
145 return -ENODEV;
146}
147
Simon Glass84283d52015-11-29 13:17:48 -0700148int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700149{
150 struct udevice *bus;
151 int ret;
152
Simon Glass7d07e592015-08-31 18:55:35 -0600153 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700154 if (ret)
155 return ret;
156 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
157}
158
159static int pci_device_matches_ids(struct udevice *dev,
160 struct pci_device_id *ids)
161{
162 struct pci_child_platdata *pplat;
163 int i;
164
165 pplat = dev_get_parent_platdata(dev);
166 if (!pplat)
167 return -EINVAL;
168 for (i = 0; ids[i].vendor != 0; i++) {
169 if (pplat->vendor == ids[i].vendor &&
170 pplat->device == ids[i].device)
171 return i;
172 }
173
174 return -EINVAL;
175}
176
177int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
178 int *indexp, struct udevice **devp)
179{
180 struct udevice *dev;
181
182 /* Scan all devices on this bus */
183 for (device_find_first_child(bus, &dev);
184 dev;
185 device_find_next_child(&dev)) {
186 if (pci_device_matches_ids(dev, ids) >= 0) {
187 if ((*indexp)-- <= 0) {
188 *devp = dev;
189 return 0;
190 }
191 }
192 }
193
194 return -ENODEV;
195}
196
197int pci_find_device_id(struct pci_device_id *ids, int index,
198 struct udevice **devp)
199{
200 struct udevice *bus;
201
202 /* Scan all known buses */
203 for (uclass_first_device(UCLASS_PCI, &bus);
204 bus;
205 uclass_next_device(&bus)) {
206 if (!pci_bus_find_devices(bus, ids, &index, devp))
207 return 0;
208 }
209 *devp = NULL;
210
211 return -ENODEV;
212}
213
Simon Glass70e0c582015-11-29 13:17:50 -0700214static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
215 unsigned int device, int *indexp,
216 struct udevice **devp)
217{
218 struct pci_child_platdata *pplat;
219 struct udevice *dev;
220
221 for (device_find_first_child(bus, &dev);
222 dev;
223 device_find_next_child(&dev)) {
224 pplat = dev_get_parent_platdata(dev);
225 if (pplat->vendor == vendor && pplat->device == device) {
226 if (!(*indexp)--) {
227 *devp = dev;
228 return 0;
229 }
230 }
231 }
232
233 return -ENODEV;
234}
235
236int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
237 struct udevice **devp)
238{
239 struct udevice *bus;
240
241 /* Scan all known buses */
242 for (uclass_first_device(UCLASS_PCI, &bus);
243 bus;
244 uclass_next_device(&bus)) {
245 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
246 return device_probe(*devp);
247 }
248 *devp = NULL;
249
250 return -ENODEV;
251}
252
Simon Glassb639d512015-11-29 13:17:52 -0700253int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
254{
255 struct udevice *dev;
256
257 /* Scan all known buses */
258 for (pci_find_first_device(&dev);
259 dev;
260 pci_find_next_device(&dev)) {
261 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
262
263 if (pplat->class == find_class && !index--) {
264 *devp = dev;
265 return device_probe(*devp);
266 }
267 }
268 *devp = NULL;
269
270 return -ENODEV;
271}
272
Simon Glassb94dc892015-03-05 12:25:25 -0700273int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
274 unsigned long value, enum pci_size_t size)
275{
276 struct dm_pci_ops *ops;
277
278 ops = pci_get_ops(bus);
279 if (!ops->write_config)
280 return -ENOSYS;
281 return ops->write_config(bus, bdf, offset, value, size);
282}
283
Simon Glass9cec2df2016-03-06 19:27:52 -0700284int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
285 u32 clr, u32 set)
286{
287 ulong val;
288 int ret;
289
290 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
291 if (ret)
292 return ret;
293 val &= ~clr;
294 val |= set;
295
296 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
297}
298
Simon Glassb94dc892015-03-05 12:25:25 -0700299int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
300 enum pci_size_t size)
301{
302 struct udevice *bus;
303 int ret;
304
Simon Glass7d07e592015-08-31 18:55:35 -0600305 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700306 if (ret)
307 return ret;
308
Bin Meng0a721522015-07-19 00:20:04 +0800309 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700310}
311
Simon Glass94ef2422015-08-10 07:05:03 -0600312int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
313 enum pci_size_t size)
314{
315 struct udevice *bus;
316
Bin Meng05bedb12015-09-11 03:24:34 -0700317 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600318 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700319 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
320 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600321}
322
Simon Glassb94dc892015-03-05 12:25:25 -0700323int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
324{
325 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
326}
327
328int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
329{
330 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
331}
332
333int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
334{
335 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
336}
337
Simon Glass94ef2422015-08-10 07:05:03 -0600338int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
339{
340 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
341}
342
343int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
344{
345 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
346}
347
348int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
349{
350 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
351}
352
Simon Glassc92aac12020-01-27 08:49:38 -0700353int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassb94dc892015-03-05 12:25:25 -0700354 unsigned long *valuep, enum pci_size_t size)
355{
356 struct dm_pci_ops *ops;
357
358 ops = pci_get_ops(bus);
359 if (!ops->read_config)
360 return -ENOSYS;
361 return ops->read_config(bus, bdf, offset, valuep, size);
362}
363
364int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
365 enum pci_size_t size)
366{
367 struct udevice *bus;
368 int ret;
369
Simon Glass7d07e592015-08-31 18:55:35 -0600370 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700371 if (ret)
372 return ret;
373
Bin Meng0a721522015-07-19 00:20:04 +0800374 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700375}
376
Simon Glassc92aac12020-01-27 08:49:38 -0700377int dm_pci_read_config(const struct udevice *dev, int offset,
378 unsigned long *valuep, enum pci_size_t size)
Simon Glass94ef2422015-08-10 07:05:03 -0600379{
Simon Glassc92aac12020-01-27 08:49:38 -0700380 const struct udevice *bus;
Simon Glass94ef2422015-08-10 07:05:03 -0600381
Bin Meng05bedb12015-09-11 03:24:34 -0700382 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600383 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700384 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600385 size);
386}
387
Simon Glassb94dc892015-03-05 12:25:25 -0700388int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
389{
390 unsigned long value;
391 int ret;
392
393 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
394 if (ret)
395 return ret;
396 *valuep = value;
397
398 return 0;
399}
400
401int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
402{
403 unsigned long value;
404 int ret;
405
406 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
407 if (ret)
408 return ret;
409 *valuep = value;
410
411 return 0;
412}
413
414int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
415{
416 unsigned long value;
417 int ret;
418
419 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
420 if (ret)
421 return ret;
422 *valuep = value;
423
424 return 0;
425}
426
Simon Glassc92aac12020-01-27 08:49:38 -0700427int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600428{
429 unsigned long value;
430 int ret;
431
432 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
433 if (ret)
434 return ret;
435 *valuep = value;
436
437 return 0;
438}
439
Simon Glassc92aac12020-01-27 08:49:38 -0700440int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600441{
442 unsigned long value;
443 int ret;
444
445 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
446 if (ret)
447 return ret;
448 *valuep = value;
449
450 return 0;
451}
452
Simon Glassc92aac12020-01-27 08:49:38 -0700453int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass94ef2422015-08-10 07:05:03 -0600454{
455 unsigned long value;
456 int ret;
457
458 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
459 if (ret)
460 return ret;
461 *valuep = value;
462
463 return 0;
464}
465
Simon Glass9cec2df2016-03-06 19:27:52 -0700466int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
467{
468 u8 val;
469 int ret;
470
471 ret = dm_pci_read_config8(dev, offset, &val);
472 if (ret)
473 return ret;
474 val &= ~clr;
475 val |= set;
476
477 return dm_pci_write_config8(dev, offset, val);
478}
479
480int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
481{
482 u16 val;
483 int ret;
484
485 ret = dm_pci_read_config16(dev, offset, &val);
486 if (ret)
487 return ret;
488 val &= ~clr;
489 val |= set;
490
491 return dm_pci_write_config16(dev, offset, val);
492}
493
494int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
495{
496 u32 val;
497 int ret;
498
499 ret = dm_pci_read_config32(dev, offset, &val);
500 if (ret)
501 return ret;
502 val &= ~clr;
503 val |= set;
504
505 return dm_pci_write_config32(dev, offset, val);
506}
507
Bin Menga0705782015-10-01 00:36:02 -0700508static void set_vga_bridge_bits(struct udevice *dev)
509{
510 struct udevice *parent = dev->parent;
511 u16 bc;
512
513 while (parent->seq != 0) {
514 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
515 bc |= PCI_BRIDGE_CTL_VGA;
516 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
517 parent = parent->parent;
518 }
519}
520
Simon Glassb94dc892015-03-05 12:25:25 -0700521int pci_auto_config_devices(struct udevice *bus)
522{
523 struct pci_controller *hose = bus->uclass_priv;
Bin Menga0705782015-10-01 00:36:02 -0700524 struct pci_child_platdata *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700525 unsigned int sub_bus;
526 struct udevice *dev;
527 int ret;
528
529 sub_bus = bus->seq;
530 debug("%s: start\n", __func__);
531 pciauto_config_init(hose);
532 for (ret = device_find_first_child(bus, &dev);
533 !ret && dev;
534 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700535 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600536 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700537
Simon Glassb94dc892015-03-05 12:25:25 -0700538 debug("%s: device %s\n", __func__, dev->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700539 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600540 if (ret < 0)
541 return ret;
542 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700543 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700544
545 pplat = dev_get_parent_platdata(dev);
546 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
547 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700548 }
549 debug("%s: done\n", __func__);
550
551 return sub_bus;
552}
553
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300554int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700555 const struct udevice *bus,
556 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
557 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300558 pci_dev_t bdf,
559 uint offset,
560 ulong value,
561 enum pci_size_t size)
562{
563 void *address;
564
565 if (addr_f(bus, bdf, offset, &address) < 0)
566 return 0;
567
568 switch (size) {
569 case PCI_SIZE_8:
570 writeb(value, address);
571 return 0;
572 case PCI_SIZE_16:
573 writew(value, address);
574 return 0;
575 case PCI_SIZE_32:
576 writel(value, address);
577 return 0;
578 default:
579 return -EINVAL;
580 }
581}
582
583int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700584 const struct udevice *bus,
585 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
586 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300587 pci_dev_t bdf,
588 uint offset,
589 ulong *valuep,
590 enum pci_size_t size)
591{
592 void *address;
593
594 if (addr_f(bus, bdf, offset, &address) < 0) {
595 *valuep = pci_get_ff(size);
596 return 0;
597 }
598
599 switch (size) {
600 case PCI_SIZE_8:
601 *valuep = readb(address);
602 return 0;
603 case PCI_SIZE_16:
604 *valuep = readw(address);
605 return 0;
606 case PCI_SIZE_32:
607 *valuep = readl(address);
608 return 0;
609 default:
610 return -EINVAL;
611 }
612}
613
Simon Glass37a3f94b2015-11-29 13:17:49 -0700614int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700615{
Simon Glassb94dc892015-03-05 12:25:25 -0700616 int sub_bus;
617 int ret;
618
619 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700620
621 sub_bus = pci_get_bus_max() + 1;
622 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700623 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700624
625 ret = device_probe(bus);
626 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600627 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700628 ret);
629 return ret;
630 }
631 if (sub_bus != bus->seq) {
632 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
633 __func__, bus->name, bus->seq, sub_bus);
634 return -EPIPE;
635 }
636 sub_bus = pci_get_bus_max();
Simon Glass37a3f94b2015-11-29 13:17:49 -0700637 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700638
639 return sub_bus;
640}
641
Simon Glass318d71c2015-07-06 16:47:44 -0600642/**
643 * pci_match_one_device - Tell if a PCI device structure has a matching
644 * PCI device id structure
645 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800646 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600647 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800648 * Returns true if the finding pci_device_id structure matched or false if
649 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600650 */
651static bool pci_match_one_id(const struct pci_device_id *id,
652 const struct pci_device_id *find)
653{
654 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
655 (id->device == PCI_ANY_ID || id->device == find->device) &&
656 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
657 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
658 !((id->class ^ find->class) & id->class_mask))
659 return true;
660
661 return false;
662}
663
664/**
665 * pci_find_and_bind_driver() - Find and bind the right PCI driver
666 *
667 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600668 *
669 * @parent: Parent bus
670 * @find_id: Specification of the driver to find
671 * @bdf: Bus/device/function addreess - see PCI_BDF()
672 * @devp: Returns a pointer to the device created
673 * @return 0 if OK, -EPERM if the device is not needed before relocation and
674 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600675 */
676static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600677 struct pci_device_id *find_id,
678 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600679{
680 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200681 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600682 const char *drv;
683 int n_ents;
684 int ret;
685 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700686 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600687
688 *devp = NULL;
689
690 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
691 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200692
693 /* Determine optional OF node */
694 pci_dev_find_ofnode(parent, bdf, &node);
695
Michael Walle2e21f372019-12-01 17:45:18 +0100696 if (ofnode_valid(node) && !ofnode_is_available(node)) {
697 debug("%s: Ignoring disabled device\n", __func__);
698 return -EPERM;
699 }
700
Simon Glass318d71c2015-07-06 16:47:44 -0600701 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
702 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
703 for (entry = start; entry != start + n_ents; entry++) {
704 const struct pci_device_id *id;
705 struct udevice *dev;
706 const struct driver *drv;
707
708 for (id = entry->match;
709 id->vendor || id->subvendor || id->class_mask;
710 id++) {
711 if (!pci_match_one_id(id, find_id))
712 continue;
713
714 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700715
716 /*
717 * In the pre-relocation phase, we only bind devices
718 * whose driver has the DM_FLAG_PRE_RELOC set, to save
719 * precious memory space as on some platforms as that
720 * space is pretty limited (ie: using Cache As RAM).
721 */
722 if (!(gd->flags & GD_FLG_RELOC) &&
723 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassc45abf12015-09-08 17:52:49 -0600724 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700725
Simon Glass318d71c2015-07-06 16:47:44 -0600726 /*
727 * We could pass the descriptor to the driver as
728 * platdata (instead of NULL) and allow its bind()
729 * method to return -ENOENT if it doesn't support this
730 * device. That way we could continue the search to
731 * find another driver. For now this doesn't seem
732 * necesssary, so just bind the first match.
733 */
Marek Vasutb4535792018-10-10 21:27:06 +0200734 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
735 node, &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600736 if (ret)
737 goto error;
738 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700739 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600740 *devp = dev;
741 return 0;
742 }
743 }
744
Bin Meng984c0dc2015-08-20 06:40:17 -0700745 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
746 /*
747 * In the pre-relocation phase, we only bind bridge devices to save
748 * precious memory space as on some platforms as that space is pretty
749 * limited (ie: using Cache As RAM).
750 */
751 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glassc45abf12015-09-08 17:52:49 -0600752 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700753
Simon Glass318d71c2015-07-06 16:47:44 -0600754 /* Bind a generic driver so that the device can be used */
Bin Meng0a721522015-07-19 00:20:04 +0800755 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
756 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600757 str = strdup(name);
758 if (!str)
759 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700760 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
761
Marek Vasutb4535792018-10-10 21:27:06 +0200762 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600763 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600764 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200765 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600766 return ret;
767 }
768 debug("%s: No match found: bound generic driver instead\n", __func__);
769
770 return 0;
771
772error:
773 debug("%s: No match found: error %d\n", __func__, ret);
774 return ret;
775}
776
Simon Glassb94dc892015-03-05 12:25:25 -0700777int pci_bind_bus_devices(struct udevice *bus)
778{
779 ulong vendor, device;
780 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800781 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700782 bool found_multi;
783 int ret;
784
785 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800786 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
787 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato1e3bce22016-04-25 15:41:01 +0900788 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800789 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700790 struct pci_child_platdata *pplat;
791 struct udevice *dev;
792 ulong class;
793
Bin Meng20bdc1e2018-08-03 01:14:37 -0700794 if (!PCI_FUNC(bdf))
795 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800796 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700797 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800798
Simon Glassb94dc892015-03-05 12:25:25 -0700799 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800800 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
801 PCI_SIZE_16);
Simon Glassb94dc892015-03-05 12:25:25 -0700802 if (ret)
803 goto error;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800804
Simon Glassb94dc892015-03-05 12:25:25 -0700805 if (vendor == 0xffff || vendor == 0x0000)
806 continue;
807
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800808 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
809 &header_type, PCI_SIZE_8);
810
Bin Meng0a721522015-07-19 00:20:04 +0800811 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700812 found_multi = header_type & 0x80;
813
Simon Glass25916d62019-09-25 08:56:12 -0600814 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Bin Meng0a721522015-07-19 00:20:04 +0800815 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
816 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700817 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800818 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600819 PCI_SIZE_32);
820 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700821
822 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800823 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600824 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700825
Simon Glass413ebdb2015-11-29 13:18:09 -0700826 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700827 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600828 struct pci_device_id find_id;
829 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700830
Simon Glass318d71c2015-07-06 16:47:44 -0600831 memset(&find_id, '\0', sizeof(find_id));
832 find_id.vendor = vendor;
833 find_id.device = device;
834 find_id.class = class;
835 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800836 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600837 PCI_SUBSYSTEM_VENDOR_ID,
838 &val, PCI_SIZE_32);
839 find_id.subvendor = val & 0xffff;
840 find_id.subdevice = val >> 16;
841 }
Bin Meng0a721522015-07-19 00:20:04 +0800842 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600843 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700844 }
Simon Glassc45abf12015-09-08 17:52:49 -0600845 if (ret == -EPERM)
846 continue;
847 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700848 return ret;
849
850 /* Update the platform data */
Simon Glassc45abf12015-09-08 17:52:49 -0600851 pplat = dev_get_parent_platdata(dev);
852 pplat->devfn = PCI_MASK_BUS(bdf);
853 pplat->vendor = vendor;
854 pplat->device = device;
855 pplat->class = class;
Simon Glassb94dc892015-03-05 12:25:25 -0700856 }
857
858 return 0;
859error:
860 printf("Cannot read bus configuration: %d\n", ret);
861
862 return ret;
863}
864
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700865static void decode_regions(struct pci_controller *hose, ofnode parent_node,
866 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700867{
868 int pci_addr_cells, addr_cells, size_cells;
869 int cells_per_record;
870 const u32 *prop;
871 int len;
872 int i;
873
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900874 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700875 if (!prop) {
876 debug("%s: Cannot decode regions\n", __func__);
877 return;
878 }
879
Simon Glass4191dc12017-06-12 06:21:31 -0600880 pci_addr_cells = ofnode_read_simple_addr_cells(node);
881 addr_cells = ofnode_read_simple_addr_cells(parent_node);
882 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700883
884 /* PCI addresses are always 3-cells */
885 len /= sizeof(u32);
886 cells_per_record = pci_addr_cells + addr_cells + size_cells;
887 hose->region_count = 0;
888 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
889 cells_per_record);
890 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
891 u64 pci_addr, addr, size;
892 int space_code;
893 u32 flags;
894 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700895 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700896
897 if (len < cells_per_record)
898 break;
899 flags = fdt32_to_cpu(prop[0]);
900 space_code = (flags >> 24) & 3;
901 pci_addr = fdtdec_get_number(prop + 1, 2);
902 prop += pci_addr_cells;
903 addr = fdtdec_get_number(prop, addr_cells);
904 prop += addr_cells;
905 size = fdtdec_get_number(prop, size_cells);
906 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900907 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
908 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -0700909 if (space_code & 2) {
910 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
911 PCI_REGION_MEM;
912 } else if (space_code & 1) {
913 type = PCI_REGION_IO;
914 } else {
915 continue;
916 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +0300917
918 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
919 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
920 debug(" - beyond the 32-bit boundary, ignoring\n");
921 continue;
922 }
923
Simon Glass7efc9ba2015-11-19 20:26:58 -0700924 pos = -1;
925 for (i = 0; i < hose->region_count; i++) {
926 if (hose->regions[i].flags == type)
927 pos = i;
928 }
929 if (pos == -1)
930 pos = hose->region_count++;
931 debug(" - type=%d, pos=%d\n", type, pos);
932 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -0700933 }
934
935 /* Add a region for our local memory */
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100936#ifdef CONFIG_NR_DRAM_BANKS
937 bd_t *bd = gd->bd;
938
Bin Mengae0bdde2018-03-27 00:46:05 -0700939 if (!bd)
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700940 return;
Bin Mengae0bdde2018-03-27 00:46:05 -0700941
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100942 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
Thierry Redingadf0d992019-03-15 16:32:32 +0100943 if (hose->region_count == MAX_PCI_REGIONS) {
944 pr_err("maximum number of regions parsed, aborting\n");
945 break;
946 }
947
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100948 if (bd->bi_dram[i].size) {
949 pci_set_region(hose->regions + hose->region_count++,
950 bd->bi_dram[i].start,
951 bd->bi_dram[i].start,
952 bd->bi_dram[i].size,
953 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
954 }
955 }
956#else
957 phys_addr_t base = 0, size;
958
Simon Glass91de6c52015-11-19 20:26:57 -0700959 size = gd->ram_size;
960#ifdef CONFIG_SYS_SDRAM_BASE
961 base = CONFIG_SYS_SDRAM_BASE;
962#endif
963 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
964 size = gd->pci_ram_top - base;
Bin Meng6d532072018-03-27 00:46:06 -0700965 if (size)
966 pci_set_region(hose->regions + hose->region_count++, base,
967 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100968#endif
Simon Glassb94dc892015-03-05 12:25:25 -0700969
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700970 return;
Simon Glassb94dc892015-03-05 12:25:25 -0700971}
972
973static int pci_uclass_pre_probe(struct udevice *bus)
974{
975 struct pci_controller *hose;
Simon Glassb94dc892015-03-05 12:25:25 -0700976
977 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
978 bus->parent->name);
979 hose = bus->uclass_priv;
980
981 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +0100982 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700983 hose->ctlr = bus;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700984 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassb94dc892015-03-05 12:25:25 -0700985 } else {
986 struct pci_controller *parent_hose;
987
988 parent_hose = dev_get_uclass_priv(bus->parent);
989 hose->ctlr = parent_hose->bus;
990 }
991 hose->bus = bus;
992 hose->first_busno = bus->seq;
993 hose->last_busno = bus->seq;
Simon Glass68e35a72019-12-06 21:41:37 -0700994 hose->skip_auto_config_until_reloc =
995 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
Simon Glassb94dc892015-03-05 12:25:25 -0700996
997 return 0;
998}
999
1000static int pci_uclass_post_probe(struct udevice *bus)
1001{
Simon Glass68e35a72019-12-06 21:41:37 -07001002 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001003 int ret;
1004
Simon Glassb94dc892015-03-05 12:25:25 -07001005 debug("%s: probing bus %d\n", __func__, bus->seq);
1006 ret = pci_bind_bus_devices(bus);
1007 if (ret)
1008 return ret;
1009
Simon Glass68e35a72019-12-06 21:41:37 -07001010 if (CONFIG_IS_ENABLED(PCI_PNP) &&
1011 (!hose->skip_auto_config_until_reloc ||
1012 (gd->flags & GD_FLG_RELOC))) {
1013 ret = pci_auto_config_devices(bus);
1014 if (ret < 0)
1015 return log_msg_ret("pci auto-config", ret);
1016 }
Simon Glassb94dc892015-03-05 12:25:25 -07001017
Bin Mengc0820a42015-08-20 06:40:23 -07001018#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1019 /*
1020 * Per Intel FSP specification, we should call FSP notify API to
1021 * inform FSP that PCI enumeration has been done so that FSP will
1022 * do any necessary initialization as required by the chipset's
1023 * BIOS Writer's Guide (BWG).
1024 *
1025 * Unfortunately we have to put this call here as with driver model,
1026 * the enumeration is all done on a lazy basis as needed, so until
1027 * something is touched on PCI it won't happen.
1028 *
1029 * Note we only call this 1) after U-Boot is relocated, and 2)
1030 * root bus has finished probing.
1031 */
Simon Glassb072d522015-09-08 17:52:47 -06001032 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Mengc0820a42015-08-20 06:40:23 -07001033 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001034 if (ret)
1035 return ret;
1036 }
Bin Mengc0820a42015-08-20 06:40:23 -07001037#endif
1038
Simon Glassb072d522015-09-08 17:52:47 -06001039 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001040}
1041
1042static int pci_uclass_child_post_bind(struct udevice *dev)
1043{
1044 struct pci_child_platdata *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001045
Simon Glass89d83232017-05-18 20:09:51 -06001046 if (!dev_of_valid(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001047 return 0;
1048
Simon Glassb94dc892015-03-05 12:25:25 -07001049 pplat = dev_get_parent_platdata(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001050
1051 /* Extract vendor id and device id if available */
1052 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1053
1054 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001055 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001056
1057 return 0;
1058}
1059
Simon Glass2a311e82020-01-27 08:49:37 -07001060static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001061 uint offset, ulong *valuep,
1062 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001063{
1064 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -07001065
1066 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1067}
1068
Bin Meng0a721522015-07-19 00:20:04 +08001069static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1070 uint offset, ulong value,
1071 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001072{
1073 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -07001074
1075 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1076}
1077
Simon Glass04c8b6a2015-08-10 07:05:04 -06001078static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1079{
1080 struct udevice *dev;
1081 int ret = 0;
1082
1083 /*
1084 * Scan through all the PCI controllers. On x86 there will only be one
1085 * but that is not necessarily true on other hardware.
1086 */
1087 do {
1088 device_find_first_child(bus, &dev);
1089 if (dev) {
1090 *devp = dev;
1091 return 0;
1092 }
1093 ret = uclass_next_device(&bus);
1094 if (ret)
1095 return ret;
1096 } while (bus);
1097
1098 return 0;
1099}
1100
1101int pci_find_next_device(struct udevice **devp)
1102{
1103 struct udevice *child = *devp;
1104 struct udevice *bus = child->parent;
1105 int ret;
1106
1107 /* First try all the siblings */
1108 *devp = NULL;
1109 while (child) {
1110 device_find_next_child(&child);
1111 if (child) {
1112 *devp = child;
1113 return 0;
1114 }
1115 }
1116
1117 /* We ran out of siblings. Try the next bus */
1118 ret = uclass_next_device(&bus);
1119 if (ret)
1120 return ret;
1121
1122 return bus ? skip_to_next_device(bus, devp) : 0;
1123}
1124
1125int pci_find_first_device(struct udevice **devp)
1126{
1127 struct udevice *bus;
1128 int ret;
1129
1130 *devp = NULL;
1131 ret = uclass_first_device(UCLASS_PCI, &bus);
1132 if (ret)
1133 return ret;
1134
1135 return skip_to_next_device(bus, devp);
1136}
1137
Simon Glass27a733f2015-11-19 20:26:59 -07001138ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1139{
1140 switch (size) {
1141 case PCI_SIZE_8:
1142 return (value >> ((offset & 3) * 8)) & 0xff;
1143 case PCI_SIZE_16:
1144 return (value >> ((offset & 2) * 8)) & 0xffff;
1145 default:
1146 return value;
1147 }
1148}
1149
1150ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1151 enum pci_size_t size)
1152{
1153 uint off_mask;
1154 uint val_mask, shift;
1155 ulong ldata, mask;
1156
1157 switch (size) {
1158 case PCI_SIZE_8:
1159 off_mask = 3;
1160 val_mask = 0xff;
1161 break;
1162 case PCI_SIZE_16:
1163 off_mask = 2;
1164 val_mask = 0xffff;
1165 break;
1166 default:
1167 return value;
1168 }
1169 shift = (offset & off_mask) * 8;
1170 ldata = (value & val_mask) << shift;
1171 mask = val_mask << shift;
1172 value = (old & ~mask) | ldata;
1173
1174 return value;
1175}
1176
Simon Glassdcdc0122015-11-19 20:27:01 -07001177int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1178 struct pci_region **memp, struct pci_region **prefp)
1179{
1180 struct udevice *bus = pci_get_controller(dev);
1181 struct pci_controller *hose = dev_get_uclass_priv(bus);
1182 int i;
1183
1184 *iop = NULL;
1185 *memp = NULL;
1186 *prefp = NULL;
1187 for (i = 0; i < hose->region_count; i++) {
1188 switch (hose->regions[i].flags) {
1189 case PCI_REGION_IO:
1190 if (!*iop || (*iop)->size < hose->regions[i].size)
1191 *iop = hose->regions + i;
1192 break;
1193 case PCI_REGION_MEM:
1194 if (!*memp || (*memp)->size < hose->regions[i].size)
1195 *memp = hose->regions + i;
1196 break;
1197 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1198 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1199 *prefp = hose->regions + i;
1200 break;
1201 }
1202 }
1203
1204 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1205}
1206
Simon Glassc92aac12020-01-27 08:49:38 -07001207u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001208{
1209 u32 addr;
1210 int bar;
1211
1212 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1213 dm_pci_read_config32(dev, bar, &addr);
1214 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1215 return addr & PCI_BASE_ADDRESS_IO_MASK;
1216 else
1217 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1218}
1219
Simon Glasse2b6b562016-01-18 20:19:15 -07001220void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1221{
1222 int bar;
1223
1224 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1225 dm_pci_write_config32(dev, bar, addr);
1226}
1227
Simon Glassc5f053b2015-11-29 13:18:03 -07001228static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1229 pci_addr_t bus_addr, unsigned long flags,
1230 unsigned long skip_mask, phys_addr_t *pa)
1231{
1232 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1233 struct pci_region *res;
1234 int i;
1235
Christian Gmeiner7241f802018-06-10 06:25:06 -07001236 if (hose->region_count == 0) {
1237 *pa = bus_addr;
1238 return 0;
1239 }
1240
Simon Glassc5f053b2015-11-29 13:18:03 -07001241 for (i = 0; i < hose->region_count; i++) {
1242 res = &hose->regions[i];
1243
1244 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1245 continue;
1246
1247 if (res->flags & skip_mask)
1248 continue;
1249
1250 if (bus_addr >= res->bus_start &&
1251 (bus_addr - res->bus_start) < res->size) {
1252 *pa = (bus_addr - res->bus_start + res->phys_start);
1253 return 0;
1254 }
1255 }
1256
1257 return 1;
1258}
1259
1260phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1261 unsigned long flags)
1262{
1263 phys_addr_t phys_addr = 0;
1264 struct udevice *ctlr;
1265 int ret;
1266
1267 /* The root controller has the region information */
1268 ctlr = pci_get_controller(dev);
1269
1270 /*
1271 * if PCI_REGION_MEM is set we do a two pass search with preference
1272 * on matches that don't have PCI_REGION_SYS_MEMORY set
1273 */
1274 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1275 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1276 flags, PCI_REGION_SYS_MEMORY,
1277 &phys_addr);
1278 if (!ret)
1279 return phys_addr;
1280 }
1281
1282 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1283
1284 if (ret)
1285 puts("pci_hose_bus_to_phys: invalid physical address\n");
1286
1287 return phys_addr;
1288}
1289
1290int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1291 unsigned long flags, unsigned long skip_mask,
1292 pci_addr_t *ba)
1293{
1294 struct pci_region *res;
1295 struct udevice *ctlr;
1296 pci_addr_t bus_addr;
1297 int i;
1298 struct pci_controller *hose;
1299
1300 /* The root controller has the region information */
1301 ctlr = pci_get_controller(dev);
1302 hose = dev_get_uclass_priv(ctlr);
1303
Christian Gmeiner7241f802018-06-10 06:25:06 -07001304 if (hose->region_count == 0) {
1305 *ba = phys_addr;
1306 return 0;
1307 }
1308
Simon Glassc5f053b2015-11-29 13:18:03 -07001309 for (i = 0; i < hose->region_count; i++) {
1310 res = &hose->regions[i];
1311
1312 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1313 continue;
1314
1315 if (res->flags & skip_mask)
1316 continue;
1317
1318 bus_addr = phys_addr - res->phys_start + res->bus_start;
1319
1320 if (bus_addr >= res->bus_start &&
1321 (bus_addr - res->bus_start) < res->size) {
1322 *ba = bus_addr;
1323 return 0;
1324 }
1325 }
1326
1327 return 1;
1328}
1329
1330pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1331 unsigned long flags)
1332{
1333 pci_addr_t bus_addr = 0;
1334 int ret;
1335
1336 /*
1337 * if PCI_REGION_MEM is set we do a two pass search with preference
1338 * on matches that don't have PCI_REGION_SYS_MEMORY set
1339 */
1340 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1341 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1342 PCI_REGION_SYS_MEMORY, &bus_addr);
1343 if (!ret)
1344 return bus_addr;
1345 }
1346
1347 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1348
1349 if (ret)
1350 puts("pci_hose_phys_to_bus: invalid physical address\n");
1351
1352 return bus_addr;
1353}
1354
Alex Marginean1c934a62019-06-07 11:24:23 +03001355static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1356 int ea_off)
1357{
1358 int ea_cnt, i, entry_size;
1359 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1360 u32 ea_entry;
1361 phys_addr_t addr;
1362
1363 /* EA capability structure header */
1364 dm_pci_read_config32(dev, ea_off, &ea_entry);
1365 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1366 ea_off += PCI_EA_FIRST_ENT;
1367
1368 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1369 /* Entry header */
1370 dm_pci_read_config32(dev, ea_off, &ea_entry);
1371 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1372
1373 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1374 continue;
1375
1376 /* Base address, 1st DW */
1377 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1378 addr = ea_entry & PCI_EA_FIELD_MASK;
1379 if (ea_entry & PCI_EA_IS_64) {
1380 /* Base address, 2nd DW, skip over 4B MaxOffset */
1381 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1382 addr |= ((u64)ea_entry) << 32;
1383 }
1384
1385 /* size ignored for now */
1386 return map_physmem(addr, flags, 0);
1387 }
1388
1389 return 0;
1390}
1391
Simon Glassc5f053b2015-11-29 13:18:03 -07001392void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1393{
1394 pci_addr_t pci_bus_addr;
1395 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001396 int ea_off;
1397
1398 /*
1399 * if the function supports Enhanced Allocation use that instead of
1400 * BARs
1401 */
1402 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1403 if (ea_off)
1404 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
Simon Glassc5f053b2015-11-29 13:18:03 -07001405
1406 /* read BAR address */
1407 dm_pci_read_config32(dev, bar, &bar_response);
1408 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1409
1410 /*
1411 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1412 * isn't actualy used on any platform because u-boot assumes a static
1413 * linear mapping. In the future, this could read the BAR size
1414 * and pass that as the size if needed.
1415 */
1416 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1417}
1418
Bin Meng631f3482018-10-15 02:21:21 -07001419static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001420{
Bin Menga7366f02018-08-03 01:14:52 -07001421 int ttl = PCI_FIND_CAP_TTL;
1422 u8 id;
1423 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001424
1425 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001426
Bin Menga7366f02018-08-03 01:14:52 -07001427 while (ttl--) {
1428 if (pos < PCI_STD_HEADER_SIZEOF)
1429 break;
1430 pos &= ~3;
1431 dm_pci_read_config16(dev, pos, &ent);
1432
1433 id = ent & 0xff;
1434 if (id == 0xff)
1435 break;
1436 if (id == cap)
1437 return pos;
1438 pos = (ent >> 8);
1439 }
1440
1441 return 0;
1442}
1443
Bin Meng631f3482018-10-15 02:21:21 -07001444int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1445{
1446 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1447 cap);
1448}
1449
1450int dm_pci_find_capability(struct udevice *dev, int cap)
1451{
1452 u16 status;
1453 u8 header_type;
1454 u8 pos;
1455
1456 dm_pci_read_config16(dev, PCI_STATUS, &status);
1457 if (!(status & PCI_STATUS_CAP_LIST))
1458 return 0;
1459
1460 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1461 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1462 pos = PCI_CB_CAPABILITY_LIST;
1463 else
1464 pos = PCI_CAPABILITY_LIST;
1465
1466 return _dm_pci_find_next_capability(dev, pos, cap);
1467}
1468
1469int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001470{
1471 u32 header;
1472 int ttl;
1473 int pos = PCI_CFG_SPACE_SIZE;
1474
1475 /* minimum 8 bytes per capability */
1476 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1477
Bin Meng631f3482018-10-15 02:21:21 -07001478 if (start)
1479 pos = start;
1480
Bin Menga7366f02018-08-03 01:14:52 -07001481 dm_pci_read_config32(dev, pos, &header);
1482 /*
1483 * If we have no capabilities, this is indicated by cap ID,
1484 * cap version and next pointer all being 0.
1485 */
1486 if (header == 0)
1487 return 0;
1488
1489 while (ttl--) {
1490 if (PCI_EXT_CAP_ID(header) == cap)
1491 return pos;
1492
1493 pos = PCI_EXT_CAP_NEXT(header);
1494 if (pos < PCI_CFG_SPACE_SIZE)
1495 break;
1496
1497 dm_pci_read_config32(dev, pos, &header);
1498 }
1499
1500 return 0;
1501}
1502
Bin Meng631f3482018-10-15 02:21:21 -07001503int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1504{
1505 return dm_pci_find_next_ext_capability(dev, 0, cap);
1506}
1507
Alex Marginean09467d32019-06-07 11:24:25 +03001508int dm_pci_flr(struct udevice *dev)
1509{
1510 int pcie_off;
1511 u32 cap;
1512
1513 /* look for PCI Express Capability */
1514 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1515 if (!pcie_off)
1516 return -ENOENT;
1517
1518 /* check FLR capability */
1519 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1520 if (!(cap & PCI_EXP_DEVCAP_FLR))
1521 return -ENOENT;
1522
1523 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1524 PCI_EXP_DEVCTL_BCR_FLR);
1525
1526 /* wait 100ms, per PCI spec */
1527 mdelay(100);
1528
1529 return 0;
1530}
1531
Simon Glassb94dc892015-03-05 12:25:25 -07001532UCLASS_DRIVER(pci) = {
1533 .id = UCLASS_PCI,
1534 .name = "pci",
Simon Glassa8149412015-05-10 21:08:06 -06001535 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -06001536 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001537 .pre_probe = pci_uclass_pre_probe,
1538 .post_probe = pci_uclass_post_probe,
1539 .child_post_bind = pci_uclass_child_post_bind,
1540 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1541 .per_child_platdata_auto_alloc_size =
1542 sizeof(struct pci_child_platdata),
1543};
1544
1545static const struct dm_pci_ops pci_bridge_ops = {
1546 .read_config = pci_bridge_read_config,
1547 .write_config = pci_bridge_write_config,
1548};
1549
1550static const struct udevice_id pci_bridge_ids[] = {
1551 { .compatible = "pci-bridge" },
1552 { }
1553};
1554
1555U_BOOT_DRIVER(pci_bridge_drv) = {
1556 .name = "pci_bridge_drv",
1557 .id = UCLASS_PCI,
1558 .of_match = pci_bridge_ids,
1559 .ops = &pci_bridge_ops,
1560};
1561
1562UCLASS_DRIVER(pci_generic) = {
1563 .id = UCLASS_PCI_GENERIC,
1564 .name = "pci_generic",
1565};
1566
1567static const struct udevice_id pci_generic_ids[] = {
1568 { .compatible = "pci-generic" },
1569 { }
1570};
1571
1572U_BOOT_DRIVER(pci_generic_drv) = {
1573 .name = "pci_generic_drv",
1574 .id = UCLASS_PCI_GENERIC,
1575 .of_match = pci_generic_ids,
1576};
Stephen Warren04eb2692016-01-26 11:10:11 -07001577
1578void pci_init(void)
1579{
1580 struct udevice *bus;
1581
1582 /*
1583 * Enumerate all known controller devices. Enumeration has the side-
1584 * effect of probing them, so PCIe devices will be enumerated too.
1585 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001586 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001587 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001588 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001589 ;
1590 }
1591}