blob: 93cfc1cade5d8501e2f769b430a928e102bc2c5a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassff3e0772015-03-05 12:25:25 -07002/*
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassff3e0772015-03-05 12:25:25 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060010#include <init.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glassff3e0772015-03-05 12:25:25 -070012#include <pci.h>
Simon Glass21d1fe72015-11-29 13:18:03 -070013#include <asm/io.h>
Simon Glassff3e0772015-03-05 12:25:25 -070014#include <dm/device-internal.h>
Simon Glassbf501592017-05-18 20:09:51 -060015#include <dm/lists.h>
Bin Meng348b7442015-08-20 06:40:23 -070016#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
Simon Glass07f2f582019-08-24 14:19:05 -060017#include <asm/fsp/fsp_support.h>
Bin Meng348b7442015-08-20 06:40:23 -070018#endif
Simon Glass5e23b8b2015-11-29 13:17:49 -070019#include "pci_internal.h"
Simon Glassff3e0772015-03-05 12:25:25 -070020
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Glassa6eb93b2016-01-18 20:19:14 -070023int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass983c6ba22015-08-31 18:55:35 -060024{
25 int ret;
26
27 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
28
29 /* Since buses may not be numbered yet try a little harder with bus 0 */
30 if (ret == -ENODEV) {
Simon Glass3f603cb2016-02-11 13:23:26 -070031 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass983c6ba22015-08-31 18:55:35 -060032 if (ret)
33 return ret;
Simon Glass983c6ba22015-08-31 18:55:35 -060034 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
35 }
36
37 return ret;
38}
39
Simon Glass9f60fb02015-11-19 20:27:00 -070040struct udevice *pci_get_controller(struct udevice *dev)
41{
42 while (device_is_on_pci_bus(dev))
43 dev = dev->parent;
44
45 return dev;
46}
47
Simon Glass194fca92020-01-27 08:49:38 -070048pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060049{
50 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
51 struct udevice *bus = dev->parent;
52
Simon Glass48862872019-12-29 21:19:14 -070053 /*
54 * This error indicates that @dev is a device on an unprobed PCI bus.
55 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
56 * will produce a bad BDF>
57 *
58 * A common cause of this problem is that this function is called in the
59 * ofdata_to_platdata() method of @dev. Accessing the PCI bus in that
60 * method is not allowed, since it has not yet been probed. To fix this,
61 * move that access to the probe() method of @dev instead.
62 */
63 if (!device_active(bus))
64 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
65 bus->name);
Simon Glass4b515e42015-07-06 16:47:46 -060066 return PCI_ADD_BUS(bus->seq, pplat->devfn);
67}
68
Simon Glassff3e0772015-03-05 12:25:25 -070069/**
70 * pci_get_bus_max() - returns the bus number of the last active bus
71 *
72 * @return last bus number, or -1 if no active buses
73 */
74static int pci_get_bus_max(void)
75{
76 struct udevice *bus;
77 struct uclass *uc;
78 int ret = -1;
79
80 ret = uclass_get(UCLASS_PCI, &uc);
81 uclass_foreach_dev(bus, uc) {
82 if (bus->seq > ret)
83 ret = bus->seq;
84 }
85
86 debug("%s: ret=%d\n", __func__, ret);
87
88 return ret;
89}
90
91int pci_last_busno(void)
92{
Bin Meng069155c2015-10-01 00:36:01 -070093 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -070094}
95
96int pci_get_ff(enum pci_size_t size)
97{
98 switch (size) {
99 case PCI_SIZE_8:
100 return 0xff;
101 case PCI_SIZE_16:
102 return 0xffff;
103 default:
104 return 0xffffffff;
105 }
106}
107
Marek Vasut02e4d382018-10-10 21:27:06 +0200108static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
109 ofnode *rnode)
110{
111 struct fdt_pci_addr addr;
112 ofnode node;
113 int ret;
114
115 dev_for_each_subnode(node, bus) {
116 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
117 &addr);
118 if (ret)
119 continue;
120
121 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
122 continue;
123
124 *rnode = node;
125 break;
126 }
127};
128
Simon Glassc4e72c42020-01-27 08:49:37 -0700129int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
Simon Glassff3e0772015-03-05 12:25:25 -0700130 struct udevice **devp)
131{
132 struct udevice *dev;
133
134 for (device_find_first_child(bus, &dev);
135 dev;
136 device_find_next_child(&dev)) {
137 struct pci_child_platdata *pplat;
138
139 pplat = dev_get_parent_platdata(dev);
140 if (pplat && pplat->devfn == find_devfn) {
141 *devp = dev;
142 return 0;
143 }
144 }
145
146 return -ENODEV;
147}
148
Simon Glassf3f1fae2015-11-29 13:17:48 -0700149int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700150{
151 struct udevice *bus;
152 int ret;
153
Simon Glass983c6ba22015-08-31 18:55:35 -0600154 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700155 if (ret)
156 return ret;
157 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
158}
159
160static int pci_device_matches_ids(struct udevice *dev,
161 struct pci_device_id *ids)
162{
163 struct pci_child_platdata *pplat;
164 int i;
165
166 pplat = dev_get_parent_platdata(dev);
167 if (!pplat)
168 return -EINVAL;
169 for (i = 0; ids[i].vendor != 0; i++) {
170 if (pplat->vendor == ids[i].vendor &&
171 pplat->device == ids[i].device)
172 return i;
173 }
174
175 return -EINVAL;
176}
177
178int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
179 int *indexp, struct udevice **devp)
180{
181 struct udevice *dev;
182
183 /* Scan all devices on this bus */
184 for (device_find_first_child(bus, &dev);
185 dev;
186 device_find_next_child(&dev)) {
187 if (pci_device_matches_ids(dev, ids) >= 0) {
188 if ((*indexp)-- <= 0) {
189 *devp = dev;
190 return 0;
191 }
192 }
193 }
194
195 return -ENODEV;
196}
197
198int pci_find_device_id(struct pci_device_id *ids, int index,
199 struct udevice **devp)
200{
201 struct udevice *bus;
202
203 /* Scan all known buses */
204 for (uclass_first_device(UCLASS_PCI, &bus);
205 bus;
206 uclass_next_device(&bus)) {
207 if (!pci_bus_find_devices(bus, ids, &index, devp))
208 return 0;
209 }
210 *devp = NULL;
211
212 return -ENODEV;
213}
214
Simon Glass5c0bf642015-11-29 13:17:50 -0700215static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
216 unsigned int device, int *indexp,
217 struct udevice **devp)
218{
219 struct pci_child_platdata *pplat;
220 struct udevice *dev;
221
222 for (device_find_first_child(bus, &dev);
223 dev;
224 device_find_next_child(&dev)) {
225 pplat = dev_get_parent_platdata(dev);
226 if (pplat->vendor == vendor && pplat->device == device) {
227 if (!(*indexp)--) {
228 *devp = dev;
229 return 0;
230 }
231 }
232 }
233
234 return -ENODEV;
235}
236
237int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
238 struct udevice **devp)
239{
240 struct udevice *bus;
241
242 /* Scan all known buses */
243 for (uclass_first_device(UCLASS_PCI, &bus);
244 bus;
245 uclass_next_device(&bus)) {
246 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
247 return device_probe(*devp);
248 }
249 *devp = NULL;
250
251 return -ENODEV;
252}
253
Simon Glassa0eb8352015-11-29 13:17:52 -0700254int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
255{
256 struct udevice *dev;
257
258 /* Scan all known buses */
259 for (pci_find_first_device(&dev);
260 dev;
261 pci_find_next_device(&dev)) {
262 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
263
264 if (pplat->class == find_class && !index--) {
265 *devp = dev;
266 return device_probe(*devp);
267 }
268 }
269 *devp = NULL;
270
271 return -ENODEV;
272}
273
Simon Glassff3e0772015-03-05 12:25:25 -0700274int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
275 unsigned long value, enum pci_size_t size)
276{
277 struct dm_pci_ops *ops;
278
279 ops = pci_get_ops(bus);
280 if (!ops->write_config)
281 return -ENOSYS;
282 return ops->write_config(bus, bdf, offset, value, size);
283}
284
Simon Glass319dba12016-03-06 19:27:52 -0700285int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
286 u32 clr, u32 set)
287{
288 ulong val;
289 int ret;
290
291 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
292 if (ret)
293 return ret;
294 val &= ~clr;
295 val |= set;
296
297 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
298}
299
Simon Glassff3e0772015-03-05 12:25:25 -0700300int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
301 enum pci_size_t size)
302{
303 struct udevice *bus;
304 int ret;
305
Simon Glass983c6ba22015-08-31 18:55:35 -0600306 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700307 if (ret)
308 return ret;
309
Bin Meng4d8615c2015-07-19 00:20:04 +0800310 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700311}
312
Simon Glass66afb4e2015-08-10 07:05:03 -0600313int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
314 enum pci_size_t size)
315{
316 struct udevice *bus;
317
Bin Meng1e0f2262015-09-11 03:24:34 -0700318 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600319 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700320 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
321 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600322}
323
Simon Glassff3e0772015-03-05 12:25:25 -0700324int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
325{
326 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
327}
328
329int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
330{
331 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
332}
333
334int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
335{
336 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
337}
338
Simon Glass66afb4e2015-08-10 07:05:03 -0600339int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
340{
341 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
342}
343
344int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
345{
346 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
347}
348
349int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
350{
351 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
352}
353
Simon Glass194fca92020-01-27 08:49:38 -0700354int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
Simon Glassff3e0772015-03-05 12:25:25 -0700355 unsigned long *valuep, enum pci_size_t size)
356{
357 struct dm_pci_ops *ops;
358
359 ops = pci_get_ops(bus);
360 if (!ops->read_config)
361 return -ENOSYS;
362 return ops->read_config(bus, bdf, offset, valuep, size);
363}
364
365int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
366 enum pci_size_t size)
367{
368 struct udevice *bus;
369 int ret;
370
Simon Glass983c6ba22015-08-31 18:55:35 -0600371 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700372 if (ret)
373 return ret;
374
Bin Meng4d8615c2015-07-19 00:20:04 +0800375 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700376}
377
Simon Glass194fca92020-01-27 08:49:38 -0700378int dm_pci_read_config(const struct udevice *dev, int offset,
379 unsigned long *valuep, enum pci_size_t size)
Simon Glass66afb4e2015-08-10 07:05:03 -0600380{
Simon Glass194fca92020-01-27 08:49:38 -0700381 const struct udevice *bus;
Simon Glass66afb4e2015-08-10 07:05:03 -0600382
Bin Meng1e0f2262015-09-11 03:24:34 -0700383 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600384 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700385 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600386 size);
387}
388
Simon Glassff3e0772015-03-05 12:25:25 -0700389int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
390{
391 unsigned long value;
392 int ret;
393
394 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
395 if (ret)
396 return ret;
397 *valuep = value;
398
399 return 0;
400}
401
402int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
403{
404 unsigned long value;
405 int ret;
406
407 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
408 if (ret)
409 return ret;
410 *valuep = value;
411
412 return 0;
413}
414
415int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
416{
417 unsigned long value;
418 int ret;
419
420 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
421 if (ret)
422 return ret;
423 *valuep = value;
424
425 return 0;
426}
427
Simon Glass194fca92020-01-27 08:49:38 -0700428int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600429{
430 unsigned long value;
431 int ret;
432
433 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
434 if (ret)
435 return ret;
436 *valuep = value;
437
438 return 0;
439}
440
Simon Glass194fca92020-01-27 08:49:38 -0700441int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600442{
443 unsigned long value;
444 int ret;
445
446 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
447 if (ret)
448 return ret;
449 *valuep = value;
450
451 return 0;
452}
453
Simon Glass194fca92020-01-27 08:49:38 -0700454int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
Simon Glass66afb4e2015-08-10 07:05:03 -0600455{
456 unsigned long value;
457 int ret;
458
459 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
460 if (ret)
461 return ret;
462 *valuep = value;
463
464 return 0;
465}
466
Simon Glass319dba12016-03-06 19:27:52 -0700467int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
468{
469 u8 val;
470 int ret;
471
472 ret = dm_pci_read_config8(dev, offset, &val);
473 if (ret)
474 return ret;
475 val &= ~clr;
476 val |= set;
477
478 return dm_pci_write_config8(dev, offset, val);
479}
480
481int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
482{
483 u16 val;
484 int ret;
485
486 ret = dm_pci_read_config16(dev, offset, &val);
487 if (ret)
488 return ret;
489 val &= ~clr;
490 val |= set;
491
492 return dm_pci_write_config16(dev, offset, val);
493}
494
495int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
496{
497 u32 val;
498 int ret;
499
500 ret = dm_pci_read_config32(dev, offset, &val);
501 if (ret)
502 return ret;
503 val &= ~clr;
504 val |= set;
505
506 return dm_pci_write_config32(dev, offset, val);
507}
508
Bin Mengbbbcb522015-10-01 00:36:02 -0700509static void set_vga_bridge_bits(struct udevice *dev)
510{
511 struct udevice *parent = dev->parent;
512 u16 bc;
513
514 while (parent->seq != 0) {
515 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
516 bc |= PCI_BRIDGE_CTL_VGA;
517 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
518 parent = parent->parent;
519 }
520}
521
Simon Glassff3e0772015-03-05 12:25:25 -0700522int pci_auto_config_devices(struct udevice *bus)
523{
524 struct pci_controller *hose = bus->uclass_priv;
Bin Mengbbbcb522015-10-01 00:36:02 -0700525 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700526 unsigned int sub_bus;
527 struct udevice *dev;
528 int ret;
529
530 sub_bus = bus->seq;
531 debug("%s: start\n", __func__);
532 pciauto_config_init(hose);
533 for (ret = device_find_first_child(bus, &dev);
534 !ret && dev;
535 ret = device_find_next_child(&dev)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700536 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600537 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700538
Simon Glassff3e0772015-03-05 12:25:25 -0700539 debug("%s: device %s\n", __func__, dev->name);
Simon Glassd8c7fb52020-04-08 16:57:26 -0600540 if (dev_read_bool(dev, "pci,no-autoconfig"))
541 continue;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700542 ret = dm_pciauto_config_device(dev);
Simon Glass4d214552015-09-08 17:52:47 -0600543 if (ret < 0)
544 return ret;
545 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700546 sub_bus = max(sub_bus, max_bus);
Bin Mengbbbcb522015-10-01 00:36:02 -0700547
548 pplat = dev_get_parent_platdata(dev);
549 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
550 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700551 }
552 debug("%s: done\n", __func__);
553
554 return sub_bus;
555}
556
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300557int pci_generic_mmap_write_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700558 const struct udevice *bus,
559 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
560 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300561 pci_dev_t bdf,
562 uint offset,
563 ulong value,
564 enum pci_size_t size)
565{
566 void *address;
567
568 if (addr_f(bus, bdf, offset, &address) < 0)
569 return 0;
570
571 switch (size) {
572 case PCI_SIZE_8:
573 writeb(value, address);
574 return 0;
575 case PCI_SIZE_16:
576 writew(value, address);
577 return 0;
578 case PCI_SIZE_32:
579 writel(value, address);
580 return 0;
581 default:
582 return -EINVAL;
583 }
584}
585
586int pci_generic_mmap_read_config(
Simon Glassc4e72c42020-01-27 08:49:37 -0700587 const struct udevice *bus,
588 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
589 void **addrp),
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +0300590 pci_dev_t bdf,
591 uint offset,
592 ulong *valuep,
593 enum pci_size_t size)
594{
595 void *address;
596
597 if (addr_f(bus, bdf, offset, &address) < 0) {
598 *valuep = pci_get_ff(size);
599 return 0;
600 }
601
602 switch (size) {
603 case PCI_SIZE_8:
604 *valuep = readb(address);
605 return 0;
606 case PCI_SIZE_16:
607 *valuep = readw(address);
608 return 0;
609 case PCI_SIZE_32:
610 *valuep = readl(address);
611 return 0;
612 default:
613 return -EINVAL;
614 }
615}
616
Simon Glass5e23b8b2015-11-29 13:17:49 -0700617int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassff3e0772015-03-05 12:25:25 -0700618{
Simon Glassff3e0772015-03-05 12:25:25 -0700619 int sub_bus;
620 int ret;
621
622 debug("%s\n", __func__);
Simon Glassff3e0772015-03-05 12:25:25 -0700623
624 sub_bus = pci_get_bus_max() + 1;
625 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700626 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700627
628 ret = device_probe(bus);
629 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600630 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700631 ret);
632 return ret;
633 }
634 if (sub_bus != bus->seq) {
635 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
636 __func__, bus->name, bus->seq, sub_bus);
637 return -EPIPE;
638 }
639 sub_bus = pci_get_bus_max();
Simon Glass5e23b8b2015-11-29 13:17:49 -0700640 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700641
642 return sub_bus;
643}
644
Simon Glassaba92962015-07-06 16:47:44 -0600645/**
646 * pci_match_one_device - Tell if a PCI device structure has a matching
647 * PCI device id structure
648 * @id: single PCI device id structure to match
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800649 * @find: the PCI device id structure to match against
Simon Glassaba92962015-07-06 16:47:44 -0600650 *
Hou Zhiqiang0367bd42017-03-22 16:07:24 +0800651 * Returns true if the finding pci_device_id structure matched or false if
652 * there is no match.
Simon Glassaba92962015-07-06 16:47:44 -0600653 */
654static bool pci_match_one_id(const struct pci_device_id *id,
655 const struct pci_device_id *find)
656{
657 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
658 (id->device == PCI_ANY_ID || id->device == find->device) &&
659 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
660 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
661 !((id->class ^ find->class) & id->class_mask))
662 return true;
663
664 return false;
665}
666
667/**
668 * pci_find_and_bind_driver() - Find and bind the right PCI driver
669 *
670 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600671 *
672 * @parent: Parent bus
673 * @find_id: Specification of the driver to find
674 * @bdf: Bus/device/function addreess - see PCI_BDF()
675 * @devp: Returns a pointer to the device created
676 * @return 0 if OK, -EPERM if the device is not needed before relocation and
677 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600678 */
679static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600680 struct pci_device_id *find_id,
681 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600682{
683 struct pci_driver_entry *start, *entry;
Marek Vasut02e4d382018-10-10 21:27:06 +0200684 ofnode node = ofnode_null();
Simon Glassaba92962015-07-06 16:47:44 -0600685 const char *drv;
686 int n_ents;
687 int ret;
688 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700689 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600690
691 *devp = NULL;
692
693 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
694 find_id->vendor, find_id->device);
Marek Vasut02e4d382018-10-10 21:27:06 +0200695
696 /* Determine optional OF node */
697 pci_dev_find_ofnode(parent, bdf, &node);
698
Michael Wallea6cd5972019-12-01 17:45:18 +0100699 if (ofnode_valid(node) && !ofnode_is_available(node)) {
700 debug("%s: Ignoring disabled device\n", __func__);
701 return -EPERM;
702 }
703
Simon Glassaba92962015-07-06 16:47:44 -0600704 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
705 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
706 for (entry = start; entry != start + n_ents; entry++) {
707 const struct pci_device_id *id;
708 struct udevice *dev;
709 const struct driver *drv;
710
711 for (id = entry->match;
712 id->vendor || id->subvendor || id->class_mask;
713 id++) {
714 if (!pci_match_one_id(id, find_id))
715 continue;
716
717 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700718
719 /*
720 * In the pre-relocation phase, we only bind devices
721 * whose driver has the DM_FLAG_PRE_RELOC set, to save
722 * precious memory space as on some platforms as that
723 * space is pretty limited (ie: using Cache As RAM).
724 */
725 if (!(gd->flags & GD_FLG_RELOC) &&
726 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600727 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700728
Simon Glassaba92962015-07-06 16:47:44 -0600729 /*
730 * We could pass the descriptor to the driver as
731 * platdata (instead of NULL) and allow its bind()
732 * method to return -ENOENT if it doesn't support this
733 * device. That way we could continue the search to
734 * find another driver. For now this doesn't seem
735 * necesssary, so just bind the first match.
736 */
Marek Vasut02e4d382018-10-10 21:27:06 +0200737 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
738 node, &dev);
Simon Glassaba92962015-07-06 16:47:44 -0600739 if (ret)
740 goto error;
741 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menged698aa2018-08-03 01:14:44 -0700742 dev->driver_data = id->driver_data;
Simon Glassaba92962015-07-06 16:47:44 -0600743 *devp = dev;
744 return 0;
745 }
746 }
747
Bin Meng08fc7b82015-08-20 06:40:17 -0700748 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
749 /*
750 * In the pre-relocation phase, we only bind bridge devices to save
751 * precious memory space as on some platforms as that space is pretty
752 * limited (ie: using Cache As RAM).
753 */
754 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600755 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700756
Simon Glassaba92962015-07-06 16:47:44 -0600757 /* Bind a generic driver so that the device can be used */
Bin Meng4d8615c2015-07-19 00:20:04 +0800758 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
759 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600760 str = strdup(name);
761 if (!str)
762 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700763 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
764
Marek Vasut02e4d382018-10-10 21:27:06 +0200765 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
Simon Glassaba92962015-07-06 16:47:44 -0600766 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600767 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dec42640c2017-05-08 20:40:16 +0200768 free(str);
Simon Glassaba92962015-07-06 16:47:44 -0600769 return ret;
770 }
771 debug("%s: No match found: bound generic driver instead\n", __func__);
772
773 return 0;
774
775error:
776 debug("%s: No match found: error %d\n", __func__, ret);
777 return ret;
778}
779
Simon Glassff3e0772015-03-05 12:25:25 -0700780int pci_bind_bus_devices(struct udevice *bus)
781{
782 ulong vendor, device;
783 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800784 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700785 bool found_multi;
786 int ret;
787
788 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800789 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
790 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato6d9f5b02016-04-25 15:41:01 +0900791 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng4d8615c2015-07-19 00:20:04 +0800792 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700793 struct pci_child_platdata *pplat;
794 struct udevice *dev;
795 ulong class;
796
Bin Meng64e45f72018-08-03 01:14:37 -0700797 if (!PCI_FUNC(bdf))
798 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800799 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700800 continue;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800801
Simon Glassff3e0772015-03-05 12:25:25 -0700802 /* Check only the first access, we don't expect problems */
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800803 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
804 PCI_SIZE_16);
Simon Glassff3e0772015-03-05 12:25:25 -0700805 if (ret)
806 goto error;
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800807
Simon Glassff3e0772015-03-05 12:25:25 -0700808 if (vendor == 0xffff || vendor == 0x0000)
809 continue;
810
Hou Zhiqiang2a87f7f2018-10-08 16:35:47 +0800811 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
812 &header_type, PCI_SIZE_8);
813
Bin Meng4d8615c2015-07-19 00:20:04 +0800814 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700815 found_multi = header_type & 0x80;
816
Simon Glass09115692019-09-25 08:56:12 -0600817 debug("%s: bus %d/%s: found device %x, function %d", __func__,
Bin Meng4d8615c2015-07-19 00:20:04 +0800818 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
819 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700820 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800821 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600822 PCI_SIZE_32);
823 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700824
825 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800826 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glass09115692019-09-25 08:56:12 -0600827 debug(": find ret=%d\n", ret);
Simon Glassff3e0772015-03-05 12:25:25 -0700828
Simon Glass8bd42522015-11-29 13:18:09 -0700829 /* If nothing in the device tree, bind a device */
Simon Glassff3e0772015-03-05 12:25:25 -0700830 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600831 struct pci_device_id find_id;
832 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700833
Simon Glassaba92962015-07-06 16:47:44 -0600834 memset(&find_id, '\0', sizeof(find_id));
835 find_id.vendor = vendor;
836 find_id.device = device;
837 find_id.class = class;
838 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800839 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600840 PCI_SUBSYSTEM_VENDOR_ID,
841 &val, PCI_SIZE_32);
842 find_id.subvendor = val & 0xffff;
843 find_id.subdevice = val >> 16;
844 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800845 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600846 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700847 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600848 if (ret == -EPERM)
849 continue;
850 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700851 return ret;
852
853 /* Update the platform data */
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600854 pplat = dev_get_parent_platdata(dev);
855 pplat->devfn = PCI_MASK_BUS(bdf);
856 pplat->vendor = vendor;
857 pplat->device = device;
858 pplat->class = class;
Simon Glassff3e0772015-03-05 12:25:25 -0700859 }
860
861 return 0;
862error:
863 printf("Cannot read bus configuration: %d\n", ret);
864
865 return ret;
866}
867
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700868static void decode_regions(struct pci_controller *hose, ofnode parent_node,
869 ofnode node)
Simon Glassff3e0772015-03-05 12:25:25 -0700870{
871 int pci_addr_cells, addr_cells, size_cells;
872 int cells_per_record;
873 const u32 *prop;
874 int len;
875 int i;
876
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900877 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700878 if (!prop) {
879 debug("%s: Cannot decode regions\n", __func__);
880 return;
881 }
882
Simon Glass878d68c2017-06-12 06:21:31 -0600883 pci_addr_cells = ofnode_read_simple_addr_cells(node);
884 addr_cells = ofnode_read_simple_addr_cells(parent_node);
885 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassff3e0772015-03-05 12:25:25 -0700886
887 /* PCI addresses are always 3-cells */
888 len /= sizeof(u32);
889 cells_per_record = pci_addr_cells + addr_cells + size_cells;
890 hose->region_count = 0;
891 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
892 cells_per_record);
893 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
894 u64 pci_addr, addr, size;
895 int space_code;
896 u32 flags;
897 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700898 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700899
900 if (len < cells_per_record)
901 break;
902 flags = fdt32_to_cpu(prop[0]);
903 space_code = (flags >> 24) & 3;
904 pci_addr = fdtdec_get_number(prop + 1, 2);
905 prop += pci_addr_cells;
906 addr = fdtdec_get_number(prop, addr_cells);
907 prop += addr_cells;
908 size = fdtdec_get_number(prop, size_cells);
909 prop += size_cells;
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900910 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
911 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassff3e0772015-03-05 12:25:25 -0700912 if (space_code & 2) {
913 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
914 PCI_REGION_MEM;
915 } else if (space_code & 1) {
916 type = PCI_REGION_IO;
917 } else {
918 continue;
919 }
Tuomas Tynkkynen52ba9072018-05-14 18:47:50 +0300920
921 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
922 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
923 debug(" - beyond the 32-bit boundary, ignoring\n");
924 continue;
925 }
926
Simon Glass9526d832015-11-19 20:26:58 -0700927 pos = -1;
928 for (i = 0; i < hose->region_count; i++) {
929 if (hose->regions[i].flags == type)
930 pos = i;
931 }
932 if (pos == -1)
933 pos = hose->region_count++;
934 debug(" - type=%d, pos=%d\n", type, pos);
935 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -0700936 }
937
938 /* Add a region for our local memory */
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100939#ifdef CONFIG_NR_DRAM_BANKS
940 bd_t *bd = gd->bd;
941
Bin Meng1eaf7802018-03-27 00:46:05 -0700942 if (!bd)
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700943 return;
Bin Meng1eaf7802018-03-27 00:46:05 -0700944
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100945 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
Thierry Redingd94d9aa2019-03-15 16:32:32 +0100946 if (hose->region_count == MAX_PCI_REGIONS) {
947 pr_err("maximum number of regions parsed, aborting\n");
948 break;
949 }
950
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100951 if (bd->bi_dram[i].size) {
952 pci_set_region(hose->regions + hose->region_count++,
953 bd->bi_dram[i].start,
954 bd->bi_dram[i].start,
955 bd->bi_dram[i].size,
956 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
957 }
958 }
959#else
960 phys_addr_t base = 0, size;
961
Simon Glass2084c5a2015-11-19 20:26:57 -0700962 size = gd->ram_size;
963#ifdef CONFIG_SYS_SDRAM_BASE
964 base = CONFIG_SYS_SDRAM_BASE;
965#endif
966 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
967 size = gd->pci_ram_top - base;
Bin Mengee1109b2018-03-27 00:46:06 -0700968 if (size)
969 pci_set_region(hose->regions + hose->region_count++, base,
970 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Bernhard Messerklinger664758c2018-02-15 08:59:53 +0100971#endif
Simon Glassff3e0772015-03-05 12:25:25 -0700972
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700973 return;
Simon Glassff3e0772015-03-05 12:25:25 -0700974}
975
976static int pci_uclass_pre_probe(struct udevice *bus)
977{
978 struct pci_controller *hose;
Simon Glassff3e0772015-03-05 12:25:25 -0700979
980 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
981 bus->parent->name);
982 hose = bus->uclass_priv;
983
984 /* For bridges, use the top-level PCI controller */
Paul Burton65f62b12016-09-08 07:47:32 +0100985 if (!device_is_on_pci_bus(bus)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700986 hose->ctlr = bus;
Christian Gmeinerf2825f62018-06-10 06:25:05 -0700987 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassff3e0772015-03-05 12:25:25 -0700988 } else {
989 struct pci_controller *parent_hose;
990
991 parent_hose = dev_get_uclass_priv(bus->parent);
992 hose->ctlr = parent_hose->bus;
993 }
994 hose->bus = bus;
995 hose->first_busno = bus->seq;
996 hose->last_busno = bus->seq;
Simon Glass2206ac22019-12-06 21:41:37 -0700997 hose->skip_auto_config_until_reloc =
998 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
Simon Glassff3e0772015-03-05 12:25:25 -0700999
1000 return 0;
1001}
1002
1003static int pci_uclass_post_probe(struct udevice *bus)
1004{
Simon Glass2206ac22019-12-06 21:41:37 -07001005 struct pci_controller *hose = dev_get_uclass_priv(bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001006 int ret;
1007
Simon Glassff3e0772015-03-05 12:25:25 -07001008 debug("%s: probing bus %d\n", __func__, bus->seq);
1009 ret = pci_bind_bus_devices(bus);
1010 if (ret)
1011 return ret;
1012
Simon Glassf1f44382020-04-26 09:12:56 -06001013 if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
Simon Glass2206ac22019-12-06 21:41:37 -07001014 (!hose->skip_auto_config_until_reloc ||
1015 (gd->flags & GD_FLG_RELOC))) {
1016 ret = pci_auto_config_devices(bus);
1017 if (ret < 0)
1018 return log_msg_ret("pci auto-config", ret);
1019 }
Simon Glassff3e0772015-03-05 12:25:25 -07001020
Bin Meng348b7442015-08-20 06:40:23 -07001021#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1022 /*
1023 * Per Intel FSP specification, we should call FSP notify API to
1024 * inform FSP that PCI enumeration has been done so that FSP will
1025 * do any necessary initialization as required by the chipset's
1026 * BIOS Writer's Guide (BWG).
1027 *
1028 * Unfortunately we have to put this call here as with driver model,
1029 * the enumeration is all done on a lazy basis as needed, so until
1030 * something is touched on PCI it won't happen.
1031 *
1032 * Note we only call this 1) after U-Boot is relocated, and 2)
1033 * root bus has finished probing.
1034 */
Simon Glassf1f44382020-04-26 09:12:56 -06001035 if ((gd->flags & GD_FLG_RELOC) && bus->seq == 0 && ll_boot_init()) {
Bin Meng348b7442015-08-20 06:40:23 -07001036 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -06001037 if (ret)
1038 return ret;
1039 }
Bin Meng348b7442015-08-20 06:40:23 -07001040#endif
1041
Simon Glass4d214552015-09-08 17:52:47 -06001042 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -07001043}
1044
1045static int pci_uclass_child_post_bind(struct udevice *dev)
1046{
1047 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -07001048
Simon Glassbf501592017-05-18 20:09:51 -06001049 if (!dev_of_valid(dev))
Simon Glassff3e0772015-03-05 12:25:25 -07001050 return 0;
1051
Simon Glassff3e0772015-03-05 12:25:25 -07001052 pplat = dev_get_parent_platdata(dev);
Bin Meng1f6b08b2018-08-03 01:14:36 -07001053
1054 /* Extract vendor id and device id if available */
1055 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1056
1057 /* Extract the devfn from fdt_pci_addr */
Stefan Roeseb5214202019-01-25 11:52:42 +01001058 pplat->devfn = pci_get_devfn(dev);
Simon Glassff3e0772015-03-05 12:25:25 -07001059
1060 return 0;
1061}
1062
Simon Glassc4e72c42020-01-27 08:49:37 -07001063static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
Bin Meng4d8615c2015-07-19 00:20:04 +08001064 uint offset, ulong *valuep,
1065 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001066{
1067 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001068
1069 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1070}
1071
Bin Meng4d8615c2015-07-19 00:20:04 +08001072static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1073 uint offset, ulong value,
1074 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -07001075{
1076 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -07001077
1078 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1079}
1080
Simon Glass76c3fbc2015-08-10 07:05:04 -06001081static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1082{
1083 struct udevice *dev;
1084 int ret = 0;
1085
1086 /*
1087 * Scan through all the PCI controllers. On x86 there will only be one
1088 * but that is not necessarily true on other hardware.
1089 */
1090 do {
1091 device_find_first_child(bus, &dev);
1092 if (dev) {
1093 *devp = dev;
1094 return 0;
1095 }
1096 ret = uclass_next_device(&bus);
1097 if (ret)
1098 return ret;
1099 } while (bus);
1100
1101 return 0;
1102}
1103
1104int pci_find_next_device(struct udevice **devp)
1105{
1106 struct udevice *child = *devp;
1107 struct udevice *bus = child->parent;
1108 int ret;
1109
1110 /* First try all the siblings */
1111 *devp = NULL;
1112 while (child) {
1113 device_find_next_child(&child);
1114 if (child) {
1115 *devp = child;
1116 return 0;
1117 }
1118 }
1119
1120 /* We ran out of siblings. Try the next bus */
1121 ret = uclass_next_device(&bus);
1122 if (ret)
1123 return ret;
1124
1125 return bus ? skip_to_next_device(bus, devp) : 0;
1126}
1127
1128int pci_find_first_device(struct udevice **devp)
1129{
1130 struct udevice *bus;
1131 int ret;
1132
1133 *devp = NULL;
1134 ret = uclass_first_device(UCLASS_PCI, &bus);
1135 if (ret)
1136 return ret;
1137
1138 return skip_to_next_device(bus, devp);
1139}
1140
Simon Glass9289db62015-11-19 20:26:59 -07001141ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1142{
1143 switch (size) {
1144 case PCI_SIZE_8:
1145 return (value >> ((offset & 3) * 8)) & 0xff;
1146 case PCI_SIZE_16:
1147 return (value >> ((offset & 2) * 8)) & 0xffff;
1148 default:
1149 return value;
1150 }
1151}
1152
1153ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1154 enum pci_size_t size)
1155{
1156 uint off_mask;
1157 uint val_mask, shift;
1158 ulong ldata, mask;
1159
1160 switch (size) {
1161 case PCI_SIZE_8:
1162 off_mask = 3;
1163 val_mask = 0xff;
1164 break;
1165 case PCI_SIZE_16:
1166 off_mask = 2;
1167 val_mask = 0xffff;
1168 break;
1169 default:
1170 return value;
1171 }
1172 shift = (offset & off_mask) * 8;
1173 ldata = (value & val_mask) << shift;
1174 mask = val_mask << shift;
1175 value = (old & ~mask) | ldata;
1176
1177 return value;
1178}
1179
Simon Glassf9260332015-11-19 20:27:01 -07001180int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1181 struct pci_region **memp, struct pci_region **prefp)
1182{
1183 struct udevice *bus = pci_get_controller(dev);
1184 struct pci_controller *hose = dev_get_uclass_priv(bus);
1185 int i;
1186
1187 *iop = NULL;
1188 *memp = NULL;
1189 *prefp = NULL;
1190 for (i = 0; i < hose->region_count; i++) {
1191 switch (hose->regions[i].flags) {
1192 case PCI_REGION_IO:
1193 if (!*iop || (*iop)->size < hose->regions[i].size)
1194 *iop = hose->regions + i;
1195 break;
1196 case PCI_REGION_MEM:
1197 if (!*memp || (*memp)->size < hose->regions[i].size)
1198 *memp = hose->regions + i;
1199 break;
1200 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1201 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1202 *prefp = hose->regions + i;
1203 break;
1204 }
1205 }
1206
1207 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1208}
1209
Simon Glass194fca92020-01-27 08:49:38 -07001210u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
Simon Glassbab17cf2015-11-29 13:17:53 -07001211{
1212 u32 addr;
1213 int bar;
1214
1215 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1216 dm_pci_read_config32(dev, bar, &addr);
Simon Glass9ece4b02020-04-09 10:27:36 -06001217
1218 /*
1219 * If we get an invalid address, return this so that comparisons with
1220 * FDT_ADDR_T_NONE work correctly
1221 */
1222 if (addr == 0xffffffff)
1223 return addr;
1224 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
Simon Glassbab17cf2015-11-29 13:17:53 -07001225 return addr & PCI_BASE_ADDRESS_IO_MASK;
1226 else
1227 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1228}
1229
Simon Glass9d731c82016-01-18 20:19:15 -07001230void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1231{
1232 int bar;
1233
1234 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1235 dm_pci_write_config32(dev, bar, addr);
1236}
1237
Simon Glass21d1fe72015-11-29 13:18:03 -07001238static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1239 pci_addr_t bus_addr, unsigned long flags,
1240 unsigned long skip_mask, phys_addr_t *pa)
1241{
1242 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1243 struct pci_region *res;
1244 int i;
1245
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001246 if (hose->region_count == 0) {
1247 *pa = bus_addr;
1248 return 0;
1249 }
1250
Simon Glass21d1fe72015-11-29 13:18:03 -07001251 for (i = 0; i < hose->region_count; i++) {
1252 res = &hose->regions[i];
1253
1254 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1255 continue;
1256
1257 if (res->flags & skip_mask)
1258 continue;
1259
1260 if (bus_addr >= res->bus_start &&
1261 (bus_addr - res->bus_start) < res->size) {
1262 *pa = (bus_addr - res->bus_start + res->phys_start);
1263 return 0;
1264 }
1265 }
1266
1267 return 1;
1268}
1269
1270phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1271 unsigned long flags)
1272{
1273 phys_addr_t phys_addr = 0;
1274 struct udevice *ctlr;
1275 int ret;
1276
1277 /* The root controller has the region information */
1278 ctlr = pci_get_controller(dev);
1279
1280 /*
1281 * if PCI_REGION_MEM is set we do a two pass search with preference
1282 * on matches that don't have PCI_REGION_SYS_MEMORY set
1283 */
1284 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1285 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1286 flags, PCI_REGION_SYS_MEMORY,
1287 &phys_addr);
1288 if (!ret)
1289 return phys_addr;
1290 }
1291
1292 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1293
1294 if (ret)
1295 puts("pci_hose_bus_to_phys: invalid physical address\n");
1296
1297 return phys_addr;
1298}
1299
1300int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1301 unsigned long flags, unsigned long skip_mask,
1302 pci_addr_t *ba)
1303{
1304 struct pci_region *res;
1305 struct udevice *ctlr;
1306 pci_addr_t bus_addr;
1307 int i;
1308 struct pci_controller *hose;
1309
1310 /* The root controller has the region information */
1311 ctlr = pci_get_controller(dev);
1312 hose = dev_get_uclass_priv(ctlr);
1313
Christian Gmeiner6f95d892018-06-10 06:25:06 -07001314 if (hose->region_count == 0) {
1315 *ba = phys_addr;
1316 return 0;
1317 }
1318
Simon Glass21d1fe72015-11-29 13:18:03 -07001319 for (i = 0; i < hose->region_count; i++) {
1320 res = &hose->regions[i];
1321
1322 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1323 continue;
1324
1325 if (res->flags & skip_mask)
1326 continue;
1327
1328 bus_addr = phys_addr - res->phys_start + res->bus_start;
1329
1330 if (bus_addr >= res->bus_start &&
1331 (bus_addr - res->bus_start) < res->size) {
1332 *ba = bus_addr;
1333 return 0;
1334 }
1335 }
1336
1337 return 1;
1338}
1339
1340pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1341 unsigned long flags)
1342{
1343 pci_addr_t bus_addr = 0;
1344 int ret;
1345
1346 /*
1347 * if PCI_REGION_MEM is set we do a two pass search with preference
1348 * on matches that don't have PCI_REGION_SYS_MEMORY set
1349 */
1350 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1351 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1352 PCI_REGION_SYS_MEMORY, &bus_addr);
1353 if (!ret)
1354 return bus_addr;
1355 }
1356
1357 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1358
1359 if (ret)
1360 puts("pci_hose_phys_to_bus: invalid physical address\n");
1361
1362 return bus_addr;
1363}
1364
Alex Marginean0b143d82019-06-07 11:24:23 +03001365static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1366 int ea_off)
1367{
1368 int ea_cnt, i, entry_size;
1369 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1370 u32 ea_entry;
1371 phys_addr_t addr;
1372
1373 /* EA capability structure header */
1374 dm_pci_read_config32(dev, ea_off, &ea_entry);
1375 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1376 ea_off += PCI_EA_FIRST_ENT;
1377
1378 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1379 /* Entry header */
1380 dm_pci_read_config32(dev, ea_off, &ea_entry);
1381 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1382
1383 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1384 continue;
1385
1386 /* Base address, 1st DW */
1387 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1388 addr = ea_entry & PCI_EA_FIELD_MASK;
1389 if (ea_entry & PCI_EA_IS_64) {
1390 /* Base address, 2nd DW, skip over 4B MaxOffset */
1391 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1392 addr |= ((u64)ea_entry) << 32;
1393 }
1394
1395 /* size ignored for now */
1396 return map_physmem(addr, flags, 0);
1397 }
1398
1399 return 0;
1400}
1401
Simon Glass21d1fe72015-11-29 13:18:03 -07001402void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1403{
1404 pci_addr_t pci_bus_addr;
1405 u32 bar_response;
Alex Marginean0b143d82019-06-07 11:24:23 +03001406 int ea_off;
1407
1408 /*
1409 * if the function supports Enhanced Allocation use that instead of
1410 * BARs
1411 */
1412 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1413 if (ea_off)
1414 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
Simon Glass21d1fe72015-11-29 13:18:03 -07001415
1416 /* read BAR address */
1417 dm_pci_read_config32(dev, bar, &bar_response);
1418 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1419
1420 /*
1421 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1422 * isn't actualy used on any platform because u-boot assumes a static
1423 * linear mapping. In the future, this could read the BAR size
1424 * and pass that as the size if needed.
1425 */
1426 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1427}
1428
Bin Menga8c5f8d2018-10-15 02:21:21 -07001429static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001430{
Bin Mengdac01fd2018-08-03 01:14:52 -07001431 int ttl = PCI_FIND_CAP_TTL;
1432 u8 id;
1433 u16 ent;
Bin Mengdac01fd2018-08-03 01:14:52 -07001434
1435 dm_pci_read_config8(dev, pos, &pos);
Bin Menga8c5f8d2018-10-15 02:21:21 -07001436
Bin Mengdac01fd2018-08-03 01:14:52 -07001437 while (ttl--) {
1438 if (pos < PCI_STD_HEADER_SIZEOF)
1439 break;
1440 pos &= ~3;
1441 dm_pci_read_config16(dev, pos, &ent);
1442
1443 id = ent & 0xff;
1444 if (id == 0xff)
1445 break;
1446 if (id == cap)
1447 return pos;
1448 pos = (ent >> 8);
1449 }
1450
1451 return 0;
1452}
1453
Bin Menga8c5f8d2018-10-15 02:21:21 -07001454int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1455{
1456 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1457 cap);
1458}
1459
1460int dm_pci_find_capability(struct udevice *dev, int cap)
1461{
1462 u16 status;
1463 u8 header_type;
1464 u8 pos;
1465
1466 dm_pci_read_config16(dev, PCI_STATUS, &status);
1467 if (!(status & PCI_STATUS_CAP_LIST))
1468 return 0;
1469
1470 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1471 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1472 pos = PCI_CB_CAPABILITY_LIST;
1473 else
1474 pos = PCI_CAPABILITY_LIST;
1475
1476 return _dm_pci_find_next_capability(dev, pos, cap);
1477}
1478
1479int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
Bin Mengdac01fd2018-08-03 01:14:52 -07001480{
1481 u32 header;
1482 int ttl;
1483 int pos = PCI_CFG_SPACE_SIZE;
1484
1485 /* minimum 8 bytes per capability */
1486 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1487
Bin Menga8c5f8d2018-10-15 02:21:21 -07001488 if (start)
1489 pos = start;
1490
Bin Mengdac01fd2018-08-03 01:14:52 -07001491 dm_pci_read_config32(dev, pos, &header);
1492 /*
1493 * If we have no capabilities, this is indicated by cap ID,
1494 * cap version and next pointer all being 0.
1495 */
1496 if (header == 0)
1497 return 0;
1498
1499 while (ttl--) {
1500 if (PCI_EXT_CAP_ID(header) == cap)
1501 return pos;
1502
1503 pos = PCI_EXT_CAP_NEXT(header);
1504 if (pos < PCI_CFG_SPACE_SIZE)
1505 break;
1506
1507 dm_pci_read_config32(dev, pos, &header);
1508 }
1509
1510 return 0;
1511}
1512
Bin Menga8c5f8d2018-10-15 02:21:21 -07001513int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1514{
1515 return dm_pci_find_next_ext_capability(dev, 0, cap);
1516}
1517
Alex Margineanb8e1f822019-06-07 11:24:25 +03001518int dm_pci_flr(struct udevice *dev)
1519{
1520 int pcie_off;
1521 u32 cap;
1522
1523 /* look for PCI Express Capability */
1524 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1525 if (!pcie_off)
1526 return -ENOENT;
1527
1528 /* check FLR capability */
1529 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1530 if (!(cap & PCI_EXP_DEVCAP_FLR))
1531 return -ENOENT;
1532
1533 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1534 PCI_EXP_DEVCTL_BCR_FLR);
1535
1536 /* wait 100ms, per PCI spec */
1537 mdelay(100);
1538
1539 return 0;
1540}
1541
Simon Glassff3e0772015-03-05 12:25:25 -07001542UCLASS_DRIVER(pci) = {
1543 .id = UCLASS_PCI,
1544 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -06001545 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -06001546 .post_bind = dm_scan_fdt_dev,
Simon Glassff3e0772015-03-05 12:25:25 -07001547 .pre_probe = pci_uclass_pre_probe,
1548 .post_probe = pci_uclass_post_probe,
1549 .child_post_bind = pci_uclass_child_post_bind,
1550 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1551 .per_child_platdata_auto_alloc_size =
1552 sizeof(struct pci_child_platdata),
1553};
1554
1555static const struct dm_pci_ops pci_bridge_ops = {
1556 .read_config = pci_bridge_read_config,
1557 .write_config = pci_bridge_write_config,
1558};
1559
1560static const struct udevice_id pci_bridge_ids[] = {
1561 { .compatible = "pci-bridge" },
1562 { }
1563};
1564
1565U_BOOT_DRIVER(pci_bridge_drv) = {
1566 .name = "pci_bridge_drv",
1567 .id = UCLASS_PCI,
1568 .of_match = pci_bridge_ids,
1569 .ops = &pci_bridge_ops,
1570};
1571
1572UCLASS_DRIVER(pci_generic) = {
1573 .id = UCLASS_PCI_GENERIC,
1574 .name = "pci_generic",
1575};
1576
1577static const struct udevice_id pci_generic_ids[] = {
1578 { .compatible = "pci-generic" },
1579 { }
1580};
1581
1582U_BOOT_DRIVER(pci_generic_drv) = {
1583 .name = "pci_generic_drv",
1584 .id = UCLASS_PCI_GENERIC,
1585 .of_match = pci_generic_ids,
1586};
Stephen Warrene578b922016-01-26 11:10:11 -07001587
1588void pci_init(void)
1589{
1590 struct udevice *bus;
1591
1592 /*
1593 * Enumerate all known controller devices. Enumeration has the side-
1594 * effect of probing them, so PCIe devices will be enumerated too.
1595 */
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001596 for (uclass_first_device_check(UCLASS_PCI, &bus);
Stephen Warrene578b922016-01-26 11:10:11 -07001597 bus;
Marek BehĂșn60ee6092019-05-21 12:04:31 +02001598 uclass_next_device_check(&bus)) {
Stephen Warrene578b922016-01-26 11:10:11 -07001599 ;
1600 }
1601}