blob: ceb64517047653f4052b87f8b6a30a16f93aa3f0 [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 Glassf3005fb2020-04-08 16:57:26 -0600539 if (dev_read_bool(dev, "pci,no-autoconfig"))
540 continue;
Simon Glass37a3f94b2015-11-29 13:17:49 -0700541 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600542 if (ret < 0)
543 return ret;
544 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700545 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700546
547 pplat = dev_get_parent_platdata(dev);
548 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
549 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700550 }
551 debug("%s: done\n", __func__);
552
553 return sub_bus;
554}
555
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300556int pci_generic_mmap_write_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700557 const struct udevice *bus,
558 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
559 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300560 pci_dev_t bdf,
561 uint offset,
562 ulong value,
563 enum pci_size_t size)
564{
565 void *address;
566
567 if (addr_f(bus, bdf, offset, &address) < 0)
568 return 0;
569
570 switch (size) {
571 case PCI_SIZE_8:
572 writeb(value, address);
573 return 0;
574 case PCI_SIZE_16:
575 writew(value, address);
576 return 0;
577 case PCI_SIZE_32:
578 writel(value, address);
579 return 0;
580 default:
581 return -EINVAL;
582 }
583}
584
585int pci_generic_mmap_read_config(
Simon Glass2a311e82020-01-27 08:49:37 -0700586 const struct udevice *bus,
587 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
588 void **addrp),
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300589 pci_dev_t bdf,
590 uint offset,
591 ulong *valuep,
592 enum pci_size_t size)
593{
594 void *address;
595
596 if (addr_f(bus, bdf, offset, &address) < 0) {
597 *valuep = pci_get_ff(size);
598 return 0;
599 }
600
601 switch (size) {
602 case PCI_SIZE_8:
603 *valuep = readb(address);
604 return 0;
605 case PCI_SIZE_16:
606 *valuep = readw(address);
607 return 0;
608 case PCI_SIZE_32:
609 *valuep = readl(address);
610 return 0;
611 default:
612 return -EINVAL;
613 }
614}
615
Simon Glass37a3f94b2015-11-29 13:17:49 -0700616int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700617{
Simon Glassb94dc892015-03-05 12:25:25 -0700618 int sub_bus;
619 int ret;
620
621 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700622
623 sub_bus = pci_get_bus_max() + 1;
624 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700625 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700626
627 ret = device_probe(bus);
628 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600629 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700630 ret);
631 return ret;
632 }
633 if (sub_bus != bus->seq) {
634 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
635 __func__, bus->name, bus->seq, sub_bus);
636 return -EPIPE;
637 }
638 sub_bus = pci_get_bus_max();
Simon Glass37a3f94b2015-11-29 13:17:49 -0700639 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700640
641 return sub_bus;
642}
643
Simon Glass318d71c2015-07-06 16:47:44 -0600644/**
645 * pci_match_one_device - Tell if a PCI device structure has a matching
646 * PCI device id structure
647 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800648 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600649 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800650 * Returns true if the finding pci_device_id structure matched or false if
651 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600652 */
653static bool pci_match_one_id(const struct pci_device_id *id,
654 const struct pci_device_id *find)
655{
656 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
657 (id->device == PCI_ANY_ID || id->device == find->device) &&
658 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
659 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
660 !((id->class ^ find->class) & id->class_mask))
661 return true;
662
663 return false;
664}
665
666/**
667 * pci_find_and_bind_driver() - Find and bind the right PCI driver
668 *
669 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600670 *
671 * @parent: Parent bus
672 * @find_id: Specification of the driver to find
673 * @bdf: Bus/device/function addreess - see PCI_BDF()
674 * @devp: Returns a pointer to the device created
675 * @return 0 if OK, -EPERM if the device is not needed before relocation and
676 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600677 */
678static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600679 struct pci_device_id *find_id,
680 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600681{
682 struct pci_driver_entry *start, *entry;
Marek Vasutb4535792018-10-10 21:27:06 +0200683 ofnode node = ofnode_null();
Simon Glass318d71c2015-07-06 16:47:44 -0600684 const char *drv;
685 int n_ents;
686 int ret;
687 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700688 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600689
690 *devp = NULL;
691
692 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
693 find_id->vendor, find_id->device);
Marek Vasutb4535792018-10-10 21:27:06 +0200694
695 /* Determine optional OF node */
696 pci_dev_find_ofnode(parent, bdf, &node);
697
Michael Walle2e21f372019-12-01 17:45:18 +0100698 if (ofnode_valid(node) && !ofnode_is_available(node)) {
699 debug("%s: Ignoring disabled device\n", __func__);
700 return -EPERM;
701 }
702
Simon Glass318d71c2015-07-06 16:47:44 -0600703 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
704 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
705 for (entry = start; entry != start + n_ents; entry++) {
706 const struct pci_device_id *id;
707 struct udevice *dev;
708 const struct driver *drv;
709
710 for (id = entry->match;
711 id->vendor || id->subvendor || id->class_mask;
712 id++) {
713 if (!pci_match_one_id(id, find_id))
714 continue;
715
716 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700717
718 /*
719 * In the pre-relocation phase, we only bind devices
720 * whose driver has the DM_FLAG_PRE_RELOC set, to save
721 * precious memory space as on some platforms as that
722 * space is pretty limited (ie: using Cache As RAM).
723 */
724 if (!(gd->flags & GD_FLG_RELOC) &&
725 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassc45abf12015-09-08 17:52:49 -0600726 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700727
Simon Glass318d71c2015-07-06 16:47:44 -0600728 /*
729 * We could pass the descriptor to the driver as
730 * platdata (instead of NULL) and allow its bind()
731 * method to return -ENOENT if it doesn't support this
732 * device. That way we could continue the search to
733 * find another driver. For now this doesn't seem
734 * necesssary, so just bind the first match.
735 */
Marek Vasutb4535792018-10-10 21:27:06 +0200736 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
737 node, &dev);
Simon Glass318d71c2015-07-06 16:47:44 -0600738 if (ret)
739 goto error;
740 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700741 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600742 *devp = dev;
743 return 0;
744 }
745 }
746
Bin Meng984c0dc2015-08-20 06:40:17 -0700747 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
748 /*
749 * In the pre-relocation phase, we only bind bridge devices to save
750 * precious memory space as on some platforms as that space is pretty
751 * limited (ie: using Cache As RAM).
752 */
753 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glassc45abf12015-09-08 17:52:49 -0600754 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700755
Simon Glass318d71c2015-07-06 16:47:44 -0600756 /* Bind a generic driver so that the device can be used */
Bin Meng0a721522015-07-19 00:20:04 +0800757 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
758 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600759 str = strdup(name);
760 if (!str)
761 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700762 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
763
Marek Vasutb4535792018-10-10 21:27:06 +0200764 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glass318d71c2015-07-06 16:47:44 -0600765 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600766 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200767 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600768 return ret;
769 }
770 debug("%s: No match found: bound generic driver instead\n", __func__);
771
772 return 0;
773
774error:
775 debug("%s: No match found: error %d\n", __func__, ret);
776 return ret;
777}
778
Simon Glassb94dc892015-03-05 12:25:25 -0700779int pci_bind_bus_devices(struct udevice *bus)
780{
781 ulong vendor, device;
782 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800783 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700784 bool found_multi;
785 int ret;
786
787 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800788 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
789 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato1e3bce22016-04-25 15:41:01 +0900790 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800791 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700792 struct pci_child_platdata *pplat;
793 struct udevice *dev;
794 ulong class;
795
Bin Meng20bdc1e2018-08-03 01:14:37 -0700796 if (!PCI_FUNC(bdf))
797 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800798 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700799 continue;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800800
Simon Glassb94dc892015-03-05 12:25:25 -0700801 /* Check only the first access, we don't expect problems */
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800802 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
803 PCI_SIZE_16);
Simon Glassb94dc892015-03-05 12:25:25 -0700804 if (ret)
805 goto error;
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800806
Simon Glassb94dc892015-03-05 12:25:25 -0700807 if (vendor == 0xffff || vendor == 0x0000)
808 continue;
809
Hou Zhiqiangfb862b052018-10-08 16:35:47 +0800810 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
811 &header_type, PCI_SIZE_8);
812
Bin Meng0a721522015-07-19 00:20:04 +0800813 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700814 found_multi = header_type & 0x80;
815
Simon Glass25916d62019-09-25 08:56:12 -0600816 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Bin Meng0a721522015-07-19 00:20:04 +0800817 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
818 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700819 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800820 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600821 PCI_SIZE_32);
822 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700823
824 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800825 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass25916d62019-09-25 08:56:12 -0600826 debug(": find ret=%d\n", ret);
Simon Glassb94dc892015-03-05 12:25:25 -0700827
Simon Glass413ebdb2015-11-29 13:18:09 -0700828 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700829 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600830 struct pci_device_id find_id;
831 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700832
Simon Glass318d71c2015-07-06 16:47:44 -0600833 memset(&find_id, '\0', sizeof(find_id));
834 find_id.vendor = vendor;
835 find_id.device = device;
836 find_id.class = class;
837 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800838 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600839 PCI_SUBSYSTEM_VENDOR_ID,
840 &val, PCI_SIZE_32);
841 find_id.subvendor = val & 0xffff;
842 find_id.subdevice = val >> 16;
843 }
Bin Meng0a721522015-07-19 00:20:04 +0800844 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600845 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700846 }
Simon Glassc45abf12015-09-08 17:52:49 -0600847 if (ret == -EPERM)
848 continue;
849 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700850 return ret;
851
852 /* Update the platform data */
Simon Glassc45abf12015-09-08 17:52:49 -0600853 pplat = dev_get_parent_platdata(dev);
854 pplat->devfn = PCI_MASK_BUS(bdf);
855 pplat->vendor = vendor;
856 pplat->device = device;
857 pplat->class = class;
Simon Glassb94dc892015-03-05 12:25:25 -0700858 }
859
860 return 0;
861error:
862 printf("Cannot read bus configuration: %d\n", ret);
863
864 return ret;
865}
866
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700867static void decode_regions(struct pci_controller *hose, ofnode parent_node,
868 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700869{
870 int pci_addr_cells, addr_cells, size_cells;
871 int cells_per_record;
872 const u32 *prop;
873 int len;
874 int i;
875
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900876 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700877 if (!prop) {
878 debug("%s: Cannot decode regions\n", __func__);
879 return;
880 }
881
Simon Glass4191dc12017-06-12 06:21:31 -0600882 pci_addr_cells = ofnode_read_simple_addr_cells(node);
883 addr_cells = ofnode_read_simple_addr_cells(parent_node);
884 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700885
886 /* PCI addresses are always 3-cells */
887 len /= sizeof(u32);
888 cells_per_record = pci_addr_cells + addr_cells + size_cells;
889 hose->region_count = 0;
890 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
891 cells_per_record);
892 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
893 u64 pci_addr, addr, size;
894 int space_code;
895 u32 flags;
896 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700897 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700898
899 if (len < cells_per_record)
900 break;
901 flags = fdt32_to_cpu(prop[0]);
902 space_code = (flags >> 24) & 3;
903 pci_addr = fdtdec_get_number(prop + 1, 2);
904 prop += pci_addr_cells;
905 addr = fdtdec_get_number(prop, addr_cells);
906 prop += addr_cells;
907 size = fdtdec_get_number(prop, size_cells);
908 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900909 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
910 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -0700911 if (space_code & 2) {
912 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
913 PCI_REGION_MEM;
914 } else if (space_code & 1) {
915 type = PCI_REGION_IO;
916 } else {
917 continue;
918 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +0300919
920 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
921 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
922 debug(" - beyond the 32-bit boundary, ignoring\n");
923 continue;
924 }
925
Simon Glass7efc9ba2015-11-19 20:26:58 -0700926 pos = -1;
927 for (i = 0; i < hose->region_count; i++) {
928 if (hose->regions[i].flags == type)
929 pos = i;
930 }
931 if (pos == -1)
932 pos = hose->region_count++;
933 debug(" - type=%d, pos=%d\n", type, pos);
934 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -0700935 }
936
937 /* Add a region for our local memory */
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100938#ifdef CONFIG_NR_DRAM_BANKS
939 bd_t *bd = gd->bd;
940
Bin Mengae0bdde2018-03-27 00:46:05 -0700941 if (!bd)
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700942 return;
Bin Mengae0bdde2018-03-27 00:46:05 -0700943
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100944 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
Thierry Redingadf0d992019-03-15 16:32:32 +0100945 if (hose->region_count == MAX_PCI_REGIONS) {
946 pr_err("maximum number of regions parsed, aborting\n");
947 break;
948 }
949
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100950 if (bd->bi_dram[i].size) {
951 pci_set_region(hose->regions + hose->region_count++,
952 bd->bi_dram[i].start,
953 bd->bi_dram[i].start,
954 bd->bi_dram[i].size,
955 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
956 }
957 }
958#else
959 phys_addr_t base = 0, size;
960
Simon Glass91de6c52015-11-19 20:26:57 -0700961 size = gd->ram_size;
962#ifdef CONFIG_SYS_SDRAM_BASE
963 base = CONFIG_SYS_SDRAM_BASE;
964#endif
965 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
966 size = gd->pci_ram_top - base;
Bin Meng6d532072018-03-27 00:46:06 -0700967 if (size)
968 pci_set_region(hose->regions + hose->region_count++, base,
969 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100970#endif
Simon Glassb94dc892015-03-05 12:25:25 -0700971
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700972 return;
Simon Glassb94dc892015-03-05 12:25:25 -0700973}
974
975static int pci_uclass_pre_probe(struct udevice *bus)
976{
977 struct pci_controller *hose;
Simon Glassb94dc892015-03-05 12:25:25 -0700978
979 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
980 bus->parent->name);
981 hose = bus->uclass_priv;
982
983 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +0100984 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700985 hose->ctlr = bus;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700986 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassb94dc892015-03-05 12:25:25 -0700987 } else {
988 struct pci_controller *parent_hose;
989
990 parent_hose = dev_get_uclass_priv(bus->parent);
991 hose->ctlr = parent_hose->bus;
992 }
993 hose->bus = bus;
994 hose->first_busno = bus->seq;
995 hose->last_busno = bus->seq;
Simon Glass68e35a72019-12-06 21:41:37 -0700996 hose->skip_auto_config_until_reloc =
997 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
Simon Glassb94dc892015-03-05 12:25:25 -0700998
999 return 0;
1000}
1001
1002static int pci_uclass_post_probe(struct udevice *bus)
1003{
Simon Glass68e35a72019-12-06 21:41:37 -07001004 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassb94dc892015-03-05 12:25:25 -07001005 int ret;
1006
Simon Glassb94dc892015-03-05 12:25:25 -07001007 debug("%s: probing bus %d\n", __func__, bus->seq);
1008 ret = pci_bind_bus_devices(bus);
1009 if (ret)
1010 return ret;
1011
Simon Glass68e35a72019-12-06 21:41:37 -07001012 if (CONFIG_IS_ENABLED(PCI_PNP) &&
1013 (!hose->skip_auto_config_until_reloc ||
1014 (gd->flags & GD_FLG_RELOC))) {
1015 ret = pci_auto_config_devices(bus);
1016 if (ret < 0)
1017 return log_msg_ret("pci auto-config", ret);
1018 }
Simon Glassb94dc892015-03-05 12:25:25 -07001019
Bin Mengc0820a42015-08-20 06:40:23 -07001020#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1021 /*
1022 * Per Intel FSP specification, we should call FSP notify API to
1023 * inform FSP that PCI enumeration has been done so that FSP will
1024 * do any necessary initialization as required by the chipset's
1025 * BIOS Writer's Guide (BWG).
1026 *
1027 * Unfortunately we have to put this call here as with driver model,
1028 * the enumeration is all done on a lazy basis as needed, so until
1029 * something is touched on PCI it won't happen.
1030 *
1031 * Note we only call this 1) after U-Boot is relocated, and 2)
1032 * root bus has finished probing.
1033 */
Simon Glassb072d522015-09-08 17:52:47 -06001034 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Mengc0820a42015-08-20 06:40:23 -07001035 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -06001036 if (ret)
1037 return ret;
1038 }
Bin Mengc0820a42015-08-20 06:40:23 -07001039#endif
1040
Simon Glassb072d522015-09-08 17:52:47 -06001041 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -07001042}
1043
1044static int pci_uclass_child_post_bind(struct udevice *dev)
1045{
1046 struct pci_child_platdata *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -07001047
Simon Glass89d83232017-05-18 20:09:51 -06001048 if (!dev_of_valid(dev))
Simon Glassb94dc892015-03-05 12:25:25 -07001049 return 0;
1050
Simon Glassb94dc892015-03-05 12:25:25 -07001051 pplat = dev_get_parent_platdata(dev);
Bin Meng00d808e2018-08-03 01:14:36 -07001052
1053 /* Extract vendor id and device id if available */
1054 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1055
1056 /* Extract the devfn from fdt_pci_addr */
Stefan Roesea74eb552019-01-25 11:52:42 +01001057 pplat->devfn = pci_get_devfn(dev);
Simon Glassb94dc892015-03-05 12:25:25 -07001058
1059 return 0;
1060}
1061
Simon Glass2a311e82020-01-27 08:49:37 -07001062static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng0a721522015-07-19 00:20:04 +08001063 uint offset, ulong *valuep,
1064 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001065{
1066 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -07001067
1068 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1069}
1070
Bin Meng0a721522015-07-19 00:20:04 +08001071static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1072 uint offset, ulong value,
1073 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001074{
1075 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -07001076
1077 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1078}
1079
Simon Glass04c8b6a2015-08-10 07:05:04 -06001080static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1081{
1082 struct udevice *dev;
1083 int ret = 0;
1084
1085 /*
1086 * Scan through all the PCI controllers. On x86 there will only be one
1087 * but that is not necessarily true on other hardware.
1088 */
1089 do {
1090 device_find_first_child(bus, &dev);
1091 if (dev) {
1092 *devp = dev;
1093 return 0;
1094 }
1095 ret = uclass_next_device(&bus);
1096 if (ret)
1097 return ret;
1098 } while (bus);
1099
1100 return 0;
1101}
1102
1103int pci_find_next_device(struct udevice **devp)
1104{
1105 struct udevice *child = *devp;
1106 struct udevice *bus = child->parent;
1107 int ret;
1108
1109 /* First try all the siblings */
1110 *devp = NULL;
1111 while (child) {
1112 device_find_next_child(&child);
1113 if (child) {
1114 *devp = child;
1115 return 0;
1116 }
1117 }
1118
1119 /* We ran out of siblings. Try the next bus */
1120 ret = uclass_next_device(&bus);
1121 if (ret)
1122 return ret;
1123
1124 return bus ? skip_to_next_device(bus, devp) : 0;
1125}
1126
1127int pci_find_first_device(struct udevice **devp)
1128{
1129 struct udevice *bus;
1130 int ret;
1131
1132 *devp = NULL;
1133 ret = uclass_first_device(UCLASS_PCI, &bus);
1134 if (ret)
1135 return ret;
1136
1137 return skip_to_next_device(bus, devp);
1138}
1139
Simon Glass27a733f2015-11-19 20:26:59 -07001140ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1141{
1142 switch (size) {
1143 case PCI_SIZE_8:
1144 return (value >> ((offset & 3) * 8)) & 0xff;
1145 case PCI_SIZE_16:
1146 return (value >> ((offset & 2) * 8)) & 0xffff;
1147 default:
1148 return value;
1149 }
1150}
1151
1152ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1153 enum pci_size_t size)
1154{
1155 uint off_mask;
1156 uint val_mask, shift;
1157 ulong ldata, mask;
1158
1159 switch (size) {
1160 case PCI_SIZE_8:
1161 off_mask = 3;
1162 val_mask = 0xff;
1163 break;
1164 case PCI_SIZE_16:
1165 off_mask = 2;
1166 val_mask = 0xffff;
1167 break;
1168 default:
1169 return value;
1170 }
1171 shift = (offset & off_mask) * 8;
1172 ldata = (value & val_mask) << shift;
1173 mask = val_mask << shift;
1174 value = (old & ~mask) | ldata;
1175
1176 return value;
1177}
1178
Simon Glassdcdc0122015-11-19 20:27:01 -07001179int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1180 struct pci_region **memp, struct pci_region **prefp)
1181{
1182 struct udevice *bus = pci_get_controller(dev);
1183 struct pci_controller *hose = dev_get_uclass_priv(bus);
1184 int i;
1185
1186 *iop = NULL;
1187 *memp = NULL;
1188 *prefp = NULL;
1189 for (i = 0; i < hose->region_count; i++) {
1190 switch (hose->regions[i].flags) {
1191 case PCI_REGION_IO:
1192 if (!*iop || (*iop)->size < hose->regions[i].size)
1193 *iop = hose->regions + i;
1194 break;
1195 case PCI_REGION_MEM:
1196 if (!*memp || (*memp)->size < hose->regions[i].size)
1197 *memp = hose->regions + i;
1198 break;
1199 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1200 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1201 *prefp = hose->regions + i;
1202 break;
1203 }
1204 }
1205
1206 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1207}
1208
Simon Glassc92aac12020-01-27 08:49:38 -07001209u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glass3452cb12015-11-29 13:17:53 -07001210{
1211 u32 addr;
1212 int bar;
1213
1214 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1215 dm_pci_read_config32(dev, bar, &addr);
1216 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1217 return addr & PCI_BASE_ADDRESS_IO_MASK;
1218 else
1219 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1220}
1221
Simon Glasse2b6b562016-01-18 20:19:15 -07001222void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1223{
1224 int bar;
1225
1226 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1227 dm_pci_write_config32(dev, bar, addr);
1228}
1229
Simon Glassc5f053b2015-11-29 13:18:03 -07001230static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1231 pci_addr_t bus_addr, unsigned long flags,
1232 unsigned long skip_mask, phys_addr_t *pa)
1233{
1234 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1235 struct pci_region *res;
1236 int i;
1237
Christian Gmeiner7241f802018-06-10 06:25:06 -07001238 if (hose->region_count == 0) {
1239 *pa = bus_addr;
1240 return 0;
1241 }
1242
Simon Glassc5f053b2015-11-29 13:18:03 -07001243 for (i = 0; i < hose->region_count; i++) {
1244 res = &hose->regions[i];
1245
1246 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1247 continue;
1248
1249 if (res->flags & skip_mask)
1250 continue;
1251
1252 if (bus_addr >= res->bus_start &&
1253 (bus_addr - res->bus_start) < res->size) {
1254 *pa = (bus_addr - res->bus_start + res->phys_start);
1255 return 0;
1256 }
1257 }
1258
1259 return 1;
1260}
1261
1262phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1263 unsigned long flags)
1264{
1265 phys_addr_t phys_addr = 0;
1266 struct udevice *ctlr;
1267 int ret;
1268
1269 /* The root controller has the region information */
1270 ctlr = pci_get_controller(dev);
1271
1272 /*
1273 * if PCI_REGION_MEM is set we do a two pass search with preference
1274 * on matches that don't have PCI_REGION_SYS_MEMORY set
1275 */
1276 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1277 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1278 flags, PCI_REGION_SYS_MEMORY,
1279 &phys_addr);
1280 if (!ret)
1281 return phys_addr;
1282 }
1283
1284 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1285
1286 if (ret)
1287 puts("pci_hose_bus_to_phys: invalid physical address\n");
1288
1289 return phys_addr;
1290}
1291
1292int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1293 unsigned long flags, unsigned long skip_mask,
1294 pci_addr_t *ba)
1295{
1296 struct pci_region *res;
1297 struct udevice *ctlr;
1298 pci_addr_t bus_addr;
1299 int i;
1300 struct pci_controller *hose;
1301
1302 /* The root controller has the region information */
1303 ctlr = pci_get_controller(dev);
1304 hose = dev_get_uclass_priv(ctlr);
1305
Christian Gmeiner7241f802018-06-10 06:25:06 -07001306 if (hose->region_count == 0) {
1307 *ba = phys_addr;
1308 return 0;
1309 }
1310
Simon Glassc5f053b2015-11-29 13:18:03 -07001311 for (i = 0; i < hose->region_count; i++) {
1312 res = &hose->regions[i];
1313
1314 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1315 continue;
1316
1317 if (res->flags & skip_mask)
1318 continue;
1319
1320 bus_addr = phys_addr - res->phys_start + res->bus_start;
1321
1322 if (bus_addr >= res->bus_start &&
1323 (bus_addr - res->bus_start) < res->size) {
1324 *ba = bus_addr;
1325 return 0;
1326 }
1327 }
1328
1329 return 1;
1330}
1331
1332pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1333 unsigned long flags)
1334{
1335 pci_addr_t bus_addr = 0;
1336 int ret;
1337
1338 /*
1339 * if PCI_REGION_MEM is set we do a two pass search with preference
1340 * on matches that don't have PCI_REGION_SYS_MEMORY set
1341 */
1342 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1343 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1344 PCI_REGION_SYS_MEMORY, &bus_addr);
1345 if (!ret)
1346 return bus_addr;
1347 }
1348
1349 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1350
1351 if (ret)
1352 puts("pci_hose_phys_to_bus: invalid physical address\n");
1353
1354 return bus_addr;
1355}
1356
Alex Marginean1c934a62019-06-07 11:24:23 +03001357static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1358 int ea_off)
1359{
1360 int ea_cnt, i, entry_size;
1361 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1362 u32 ea_entry;
1363 phys_addr_t addr;
1364
1365 /* EA capability structure header */
1366 dm_pci_read_config32(dev, ea_off, &ea_entry);
1367 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1368 ea_off += PCI_EA_FIRST_ENT;
1369
1370 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1371 /* Entry header */
1372 dm_pci_read_config32(dev, ea_off, &ea_entry);
1373 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1374
1375 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1376 continue;
1377
1378 /* Base address, 1st DW */
1379 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1380 addr = ea_entry & PCI_EA_FIELD_MASK;
1381 if (ea_entry & PCI_EA_IS_64) {
1382 /* Base address, 2nd DW, skip over 4B MaxOffset */
1383 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1384 addr |= ((u64)ea_entry) << 32;
1385 }
1386
1387 /* size ignored for now */
1388 return map_physmem(addr, flags, 0);
1389 }
1390
1391 return 0;
1392}
1393
Simon Glassc5f053b2015-11-29 13:18:03 -07001394void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1395{
1396 pci_addr_t pci_bus_addr;
1397 u32 bar_response;
Alex Marginean1c934a62019-06-07 11:24:23 +03001398 int ea_off;
1399
1400 /*
1401 * if the function supports Enhanced Allocation use that instead of
1402 * BARs
1403 */
1404 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1405 if (ea_off)
1406 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
Simon Glassc5f053b2015-11-29 13:18:03 -07001407
1408 /* read BAR address */
1409 dm_pci_read_config32(dev, bar, &bar_response);
1410 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1411
1412 /*
1413 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1414 * isn't actualy used on any platform because u-boot assumes a static
1415 * linear mapping. In the future, this could read the BAR size
1416 * and pass that as the size if needed.
1417 */
1418 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1419}
1420
Bin Meng631f3482018-10-15 02:21:21 -07001421static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001422{
Bin Menga7366f02018-08-03 01:14:52 -07001423 int ttl = PCI_FIND_CAP_TTL;
1424 u8 id;
1425 u16 ent;
Bin Menga7366f02018-08-03 01:14:52 -07001426
1427 dm_pci_read_config8(dev, pos, &pos);
Bin Meng631f3482018-10-15 02:21:21 -07001428
Bin Menga7366f02018-08-03 01:14:52 -07001429 while (ttl--) {
1430 if (pos < PCI_STD_HEADER_SIZEOF)
1431 break;
1432 pos &= ~3;
1433 dm_pci_read_config16(dev, pos, &ent);
1434
1435 id = ent & 0xff;
1436 if (id == 0xff)
1437 break;
1438 if (id == cap)
1439 return pos;
1440 pos = (ent >> 8);
1441 }
1442
1443 return 0;
1444}
1445
Bin Meng631f3482018-10-15 02:21:21 -07001446int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1447{
1448 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1449 cap);
1450}
1451
1452int dm_pci_find_capability(struct udevice *dev, int cap)
1453{
1454 u16 status;
1455 u8 header_type;
1456 u8 pos;
1457
1458 dm_pci_read_config16(dev, PCI_STATUS, &status);
1459 if (!(status & PCI_STATUS_CAP_LIST))
1460 return 0;
1461
1462 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1463 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1464 pos = PCI_CB_CAPABILITY_LIST;
1465 else
1466 pos = PCI_CAPABILITY_LIST;
1467
1468 return _dm_pci_find_next_capability(dev, pos, cap);
1469}
1470
1471int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Menga7366f02018-08-03 01:14:52 -07001472{
1473 u32 header;
1474 int ttl;
1475 int pos = PCI_CFG_SPACE_SIZE;
1476
1477 /* minimum 8 bytes per capability */
1478 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1479
Bin Meng631f3482018-10-15 02:21:21 -07001480 if (start)
1481 pos = start;
1482
Bin Menga7366f02018-08-03 01:14:52 -07001483 dm_pci_read_config32(dev, pos, &header);
1484 /*
1485 * If we have no capabilities, this is indicated by cap ID,
1486 * cap version and next pointer all being 0.
1487 */
1488 if (header == 0)
1489 return 0;
1490
1491 while (ttl--) {
1492 if (PCI_EXT_CAP_ID(header) == cap)
1493 return pos;
1494
1495 pos = PCI_EXT_CAP_NEXT(header);
1496 if (pos < PCI_CFG_SPACE_SIZE)
1497 break;
1498
1499 dm_pci_read_config32(dev, pos, &header);
1500 }
1501
1502 return 0;
1503}
1504
Bin Meng631f3482018-10-15 02:21:21 -07001505int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1506{
1507 return dm_pci_find_next_ext_capability(dev, 0, cap);
1508}
1509
Alex Marginean09467d32019-06-07 11:24:25 +03001510int dm_pci_flr(struct udevice *dev)
1511{
1512 int pcie_off;
1513 u32 cap;
1514
1515 /* look for PCI Express Capability */
1516 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1517 if (!pcie_off)
1518 return -ENOENT;
1519
1520 /* check FLR capability */
1521 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1522 if (!(cap & PCI_EXP_DEVCAP_FLR))
1523 return -ENOENT;
1524
1525 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1526 PCI_EXP_DEVCTL_BCR_FLR);
1527
1528 /* wait 100ms, per PCI spec */
1529 mdelay(100);
1530
1531 return 0;
1532}
1533
Simon Glassb94dc892015-03-05 12:25:25 -07001534UCLASS_DRIVER(pci) = {
1535 .id = UCLASS_PCI,
1536 .name = "pci",
Simon Glassa8149412015-05-10 21:08:06 -06001537 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -06001538 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001539 .pre_probe = pci_uclass_pre_probe,
1540 .post_probe = pci_uclass_post_probe,
1541 .child_post_bind = pci_uclass_child_post_bind,
1542 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1543 .per_child_platdata_auto_alloc_size =
1544 sizeof(struct pci_child_platdata),
1545};
1546
1547static const struct dm_pci_ops pci_bridge_ops = {
1548 .read_config = pci_bridge_read_config,
1549 .write_config = pci_bridge_write_config,
1550};
1551
1552static const struct udevice_id pci_bridge_ids[] = {
1553 { .compatible = "pci-bridge" },
1554 { }
1555};
1556
1557U_BOOT_DRIVER(pci_bridge_drv) = {
1558 .name = "pci_bridge_drv",
1559 .id = UCLASS_PCI,
1560 .of_match = pci_bridge_ids,
1561 .ops = &pci_bridge_ops,
1562};
1563
1564UCLASS_DRIVER(pci_generic) = {
1565 .id = UCLASS_PCI_GENERIC,
1566 .name = "pci_generic",
1567};
1568
1569static const struct udevice_id pci_generic_ids[] = {
1570 { .compatible = "pci-generic" },
1571 { }
1572};
1573
1574U_BOOT_DRIVER(pci_generic_drv) = {
1575 .name = "pci_generic_drv",
1576 .id = UCLASS_PCI_GENERIC,
1577 .of_match = pci_generic_ids,
1578};
Stephen Warren04eb2692016-01-26 11:10:11 -07001579
1580void pci_init(void)
1581{
1582 struct udevice *bus;
1583
1584 /*
1585 * Enumerate all known controller devices. Enumeration has the side-
1586 * effect of probing them, so PCIe devices will be enumerated too.
1587 */
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001588 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warren04eb2692016-01-26 11:10:11 -07001589 bus;
Marek BehĂșn5df208d2019-05-21 12:04:31 +02001590 uclass_next_device_check(&bus)) {
Stephen Warren04eb2692016-01-26 11:10:11 -07001591 ;
1592 }
1593}