blob: eb118f3496ef5f90e3193b3dbc90b482edc6bb9a [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 Glassb94dc892015-03-05 12:25:25 -070010#include <pci.h>
Simon Glassc5f053b2015-11-29 13:18:03 -070011#include <asm/io.h>
Simon Glassb94dc892015-03-05 12:25:25 -070012#include <dm/device-internal.h>
Simon Glass89d83232017-05-18 20:09:51 -060013#include <dm/lists.h>
Bin Mengc0820a42015-08-20 06:40:23 -070014#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
15#include <asm/fsp/fsp_support.h>
16#endif
Simon Glass37a3f94b2015-11-29 13:17:49 -070017#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass2e4e4432016-01-18 20:19:14 -070021int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-08-31 18:55:35 -060022{
23 int ret;
24
25 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
26
27 /* Since buses may not be numbered yet try a little harder with bus 0 */
28 if (ret == -ENODEV) {
Simon Glassc7298e72016-02-11 13:23:26 -070029 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060030 if (ret)
31 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060032 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
33 }
34
35 return ret;
36}
37
Simon Glass6256d672015-11-19 20:27:00 -070038struct udevice *pci_get_controller(struct udevice *dev)
39{
40 while (device_is_on_pci_bus(dev))
41 dev = dev->parent;
42
43 return dev;
44}
45
Simon Glasseaa14892015-11-29 13:17:47 -070046pci_dev_t dm_pci_get_bdf(struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060047{
48 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
49 struct udevice *bus = dev->parent;
50
51 return PCI_ADD_BUS(bus->seq, pplat->devfn);
52}
53
Simon Glassb94dc892015-03-05 12:25:25 -070054/**
55 * pci_get_bus_max() - returns the bus number of the last active bus
56 *
57 * @return last bus number, or -1 if no active buses
58 */
59static int pci_get_bus_max(void)
60{
61 struct udevice *bus;
62 struct uclass *uc;
63 int ret = -1;
64
65 ret = uclass_get(UCLASS_PCI, &uc);
66 uclass_foreach_dev(bus, uc) {
67 if (bus->seq > ret)
68 ret = bus->seq;
69 }
70
71 debug("%s: ret=%d\n", __func__, ret);
72
73 return ret;
74}
75
76int pci_last_busno(void)
77{
Bin Meng5bc3f8a2015-10-01 00:36:01 -070078 return pci_get_bus_max();
Simon Glassb94dc892015-03-05 12:25:25 -070079}
80
81int pci_get_ff(enum pci_size_t size)
82{
83 switch (size) {
84 case PCI_SIZE_8:
85 return 0xff;
86 case PCI_SIZE_16:
87 return 0xffff;
88 default:
89 return 0xffffffff;
90 }
91}
92
93int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
94 struct udevice **devp)
95{
96 struct udevice *dev;
97
98 for (device_find_first_child(bus, &dev);
99 dev;
100 device_find_next_child(&dev)) {
101 struct pci_child_platdata *pplat;
102
103 pplat = dev_get_parent_platdata(dev);
104 if (pplat && pplat->devfn == find_devfn) {
105 *devp = dev;
106 return 0;
107 }
108 }
109
110 return -ENODEV;
111}
112
Simon Glass84283d52015-11-29 13:17:48 -0700113int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700114{
115 struct udevice *bus;
116 int ret;
117
Simon Glass7d07e592015-08-31 18:55:35 -0600118 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700119 if (ret)
120 return ret;
121 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
122}
123
124static int pci_device_matches_ids(struct udevice *dev,
125 struct pci_device_id *ids)
126{
127 struct pci_child_platdata *pplat;
128 int i;
129
130 pplat = dev_get_parent_platdata(dev);
131 if (!pplat)
132 return -EINVAL;
133 for (i = 0; ids[i].vendor != 0; i++) {
134 if (pplat->vendor == ids[i].vendor &&
135 pplat->device == ids[i].device)
136 return i;
137 }
138
139 return -EINVAL;
140}
141
142int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
143 int *indexp, struct udevice **devp)
144{
145 struct udevice *dev;
146
147 /* Scan all devices on this bus */
148 for (device_find_first_child(bus, &dev);
149 dev;
150 device_find_next_child(&dev)) {
151 if (pci_device_matches_ids(dev, ids) >= 0) {
152 if ((*indexp)-- <= 0) {
153 *devp = dev;
154 return 0;
155 }
156 }
157 }
158
159 return -ENODEV;
160}
161
162int pci_find_device_id(struct pci_device_id *ids, int index,
163 struct udevice **devp)
164{
165 struct udevice *bus;
166
167 /* Scan all known buses */
168 for (uclass_first_device(UCLASS_PCI, &bus);
169 bus;
170 uclass_next_device(&bus)) {
171 if (!pci_bus_find_devices(bus, ids, &index, devp))
172 return 0;
173 }
174 *devp = NULL;
175
176 return -ENODEV;
177}
178
Simon Glass70e0c582015-11-29 13:17:50 -0700179static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
180 unsigned int device, int *indexp,
181 struct udevice **devp)
182{
183 struct pci_child_platdata *pplat;
184 struct udevice *dev;
185
186 for (device_find_first_child(bus, &dev);
187 dev;
188 device_find_next_child(&dev)) {
189 pplat = dev_get_parent_platdata(dev);
190 if (pplat->vendor == vendor && pplat->device == device) {
191 if (!(*indexp)--) {
192 *devp = dev;
193 return 0;
194 }
195 }
196 }
197
198 return -ENODEV;
199}
200
201int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
202 struct udevice **devp)
203{
204 struct udevice *bus;
205
206 /* Scan all known buses */
207 for (uclass_first_device(UCLASS_PCI, &bus);
208 bus;
209 uclass_next_device(&bus)) {
210 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
211 return device_probe(*devp);
212 }
213 *devp = NULL;
214
215 return -ENODEV;
216}
217
Simon Glassb639d512015-11-29 13:17:52 -0700218int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
219{
220 struct udevice *dev;
221
222 /* Scan all known buses */
223 for (pci_find_first_device(&dev);
224 dev;
225 pci_find_next_device(&dev)) {
226 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
227
228 if (pplat->class == find_class && !index--) {
229 *devp = dev;
230 return device_probe(*devp);
231 }
232 }
233 *devp = NULL;
234
235 return -ENODEV;
236}
237
Simon Glassb94dc892015-03-05 12:25:25 -0700238int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
239 unsigned long value, enum pci_size_t size)
240{
241 struct dm_pci_ops *ops;
242
243 ops = pci_get_ops(bus);
244 if (!ops->write_config)
245 return -ENOSYS;
246 return ops->write_config(bus, bdf, offset, value, size);
247}
248
Simon Glass9cec2df2016-03-06 19:27:52 -0700249int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
250 u32 clr, u32 set)
251{
252 ulong val;
253 int ret;
254
255 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
256 if (ret)
257 return ret;
258 val &= ~clr;
259 val |= set;
260
261 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
262}
263
Simon Glassb94dc892015-03-05 12:25:25 -0700264int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
265 enum pci_size_t size)
266{
267 struct udevice *bus;
268 int ret;
269
Simon Glass7d07e592015-08-31 18:55:35 -0600270 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700271 if (ret)
272 return ret;
273
Bin Meng0a721522015-07-19 00:20:04 +0800274 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700275}
276
Simon Glass94ef2422015-08-10 07:05:03 -0600277int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
278 enum pci_size_t size)
279{
280 struct udevice *bus;
281
Bin Meng05bedb12015-09-11 03:24:34 -0700282 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600283 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700284 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
285 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600286}
287
Simon Glassb94dc892015-03-05 12:25:25 -0700288int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
289{
290 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
291}
292
293int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
294{
295 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
296}
297
298int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
299{
300 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
301}
302
Simon Glass94ef2422015-08-10 07:05:03 -0600303int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
304{
305 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
306}
307
308int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
309{
310 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
311}
312
313int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
314{
315 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
316}
317
Simon Glassb94dc892015-03-05 12:25:25 -0700318int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
319 unsigned long *valuep, enum pci_size_t size)
320{
321 struct dm_pci_ops *ops;
322
323 ops = pci_get_ops(bus);
324 if (!ops->read_config)
325 return -ENOSYS;
326 return ops->read_config(bus, bdf, offset, valuep, size);
327}
328
329int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
330 enum pci_size_t size)
331{
332 struct udevice *bus;
333 int ret;
334
Simon Glass7d07e592015-08-31 18:55:35 -0600335 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700336 if (ret)
337 return ret;
338
Bin Meng0a721522015-07-19 00:20:04 +0800339 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700340}
341
Simon Glass94ef2422015-08-10 07:05:03 -0600342int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
343 enum pci_size_t size)
344{
345 struct udevice *bus;
346
Bin Meng05bedb12015-09-11 03:24:34 -0700347 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600348 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700349 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600350 size);
351}
352
Simon Glassb94dc892015-03-05 12:25:25 -0700353int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
354{
355 unsigned long value;
356 int ret;
357
358 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
359 if (ret)
360 return ret;
361 *valuep = value;
362
363 return 0;
364}
365
366int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
367{
368 unsigned long value;
369 int ret;
370
371 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
372 if (ret)
373 return ret;
374 *valuep = value;
375
376 return 0;
377}
378
379int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
380{
381 unsigned long value;
382 int ret;
383
384 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
385 if (ret)
386 return ret;
387 *valuep = value;
388
389 return 0;
390}
391
Simon Glass94ef2422015-08-10 07:05:03 -0600392int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
393{
394 unsigned long value;
395 int ret;
396
397 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
398 if (ret)
399 return ret;
400 *valuep = value;
401
402 return 0;
403}
404
405int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
406{
407 unsigned long value;
408 int ret;
409
410 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
411 if (ret)
412 return ret;
413 *valuep = value;
414
415 return 0;
416}
417
418int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
419{
420 unsigned long value;
421 int ret;
422
423 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
424 if (ret)
425 return ret;
426 *valuep = value;
427
428 return 0;
429}
430
Simon Glass9cec2df2016-03-06 19:27:52 -0700431int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
432{
433 u8 val;
434 int ret;
435
436 ret = dm_pci_read_config8(dev, offset, &val);
437 if (ret)
438 return ret;
439 val &= ~clr;
440 val |= set;
441
442 return dm_pci_write_config8(dev, offset, val);
443}
444
445int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
446{
447 u16 val;
448 int ret;
449
450 ret = dm_pci_read_config16(dev, offset, &val);
451 if (ret)
452 return ret;
453 val &= ~clr;
454 val |= set;
455
456 return dm_pci_write_config16(dev, offset, val);
457}
458
459int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
460{
461 u32 val;
462 int ret;
463
464 ret = dm_pci_read_config32(dev, offset, &val);
465 if (ret)
466 return ret;
467 val &= ~clr;
468 val |= set;
469
470 return dm_pci_write_config32(dev, offset, val);
471}
472
Bin Menga0705782015-10-01 00:36:02 -0700473static void set_vga_bridge_bits(struct udevice *dev)
474{
475 struct udevice *parent = dev->parent;
476 u16 bc;
477
478 while (parent->seq != 0) {
479 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
480 bc |= PCI_BRIDGE_CTL_VGA;
481 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
482 parent = parent->parent;
483 }
484}
485
Simon Glassb94dc892015-03-05 12:25:25 -0700486int pci_auto_config_devices(struct udevice *bus)
487{
488 struct pci_controller *hose = bus->uclass_priv;
Bin Menga0705782015-10-01 00:36:02 -0700489 struct pci_child_platdata *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700490 unsigned int sub_bus;
491 struct udevice *dev;
492 int ret;
493
494 sub_bus = bus->seq;
495 debug("%s: start\n", __func__);
496 pciauto_config_init(hose);
497 for (ret = device_find_first_child(bus, &dev);
498 !ret && dev;
499 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700500 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600501 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700502
Simon Glassb94dc892015-03-05 12:25:25 -0700503 debug("%s: device %s\n", __func__, dev->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700504 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600505 if (ret < 0)
506 return ret;
507 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700508 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700509
510 pplat = dev_get_parent_platdata(dev);
511 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
512 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700513 }
514 debug("%s: done\n", __func__);
515
516 return sub_bus;
517}
518
Tuomas Tynkkynen8cce4cf2017-09-19 23:18:03 +0300519int pci_generic_mmap_write_config(
520 struct udevice *bus,
521 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
522 pci_dev_t bdf,
523 uint offset,
524 ulong value,
525 enum pci_size_t size)
526{
527 void *address;
528
529 if (addr_f(bus, bdf, offset, &address) < 0)
530 return 0;
531
532 switch (size) {
533 case PCI_SIZE_8:
534 writeb(value, address);
535 return 0;
536 case PCI_SIZE_16:
537 writew(value, address);
538 return 0;
539 case PCI_SIZE_32:
540 writel(value, address);
541 return 0;
542 default:
543 return -EINVAL;
544 }
545}
546
547int pci_generic_mmap_read_config(
548 struct udevice *bus,
549 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
550 pci_dev_t bdf,
551 uint offset,
552 ulong *valuep,
553 enum pci_size_t size)
554{
555 void *address;
556
557 if (addr_f(bus, bdf, offset, &address) < 0) {
558 *valuep = pci_get_ff(size);
559 return 0;
560 }
561
562 switch (size) {
563 case PCI_SIZE_8:
564 *valuep = readb(address);
565 return 0;
566 case PCI_SIZE_16:
567 *valuep = readw(address);
568 return 0;
569 case PCI_SIZE_32:
570 *valuep = readl(address);
571 return 0;
572 default:
573 return -EINVAL;
574 }
575}
576
Simon Glass37a3f94b2015-11-29 13:17:49 -0700577int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700578{
Simon Glassb94dc892015-03-05 12:25:25 -0700579 int sub_bus;
580 int ret;
581
582 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700583
584 sub_bus = pci_get_bus_max() + 1;
585 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700586 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700587
588 ret = device_probe(bus);
589 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600590 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700591 ret);
592 return ret;
593 }
594 if (sub_bus != bus->seq) {
595 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
596 __func__, bus->name, bus->seq, sub_bus);
597 return -EPIPE;
598 }
599 sub_bus = pci_get_bus_max();
Simon Glass37a3f94b2015-11-29 13:17:49 -0700600 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700601
602 return sub_bus;
603}
604
Simon Glass318d71c2015-07-06 16:47:44 -0600605/**
606 * pci_match_one_device - Tell if a PCI device structure has a matching
607 * PCI device id structure
608 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800609 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600610 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800611 * Returns true if the finding pci_device_id structure matched or false if
612 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600613 */
614static bool pci_match_one_id(const struct pci_device_id *id,
615 const struct pci_device_id *find)
616{
617 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
618 (id->device == PCI_ANY_ID || id->device == find->device) &&
619 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
620 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
621 !((id->class ^ find->class) & id->class_mask))
622 return true;
623
624 return false;
625}
626
627/**
628 * pci_find_and_bind_driver() - Find and bind the right PCI driver
629 *
630 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600631 *
632 * @parent: Parent bus
633 * @find_id: Specification of the driver to find
634 * @bdf: Bus/device/function addreess - see PCI_BDF()
635 * @devp: Returns a pointer to the device created
636 * @return 0 if OK, -EPERM if the device is not needed before relocation and
637 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600638 */
639static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600640 struct pci_device_id *find_id,
641 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600642{
643 struct pci_driver_entry *start, *entry;
644 const char *drv;
645 int n_ents;
646 int ret;
647 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700648 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600649
650 *devp = NULL;
651
652 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
653 find_id->vendor, find_id->device);
654 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
655 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
656 for (entry = start; entry != start + n_ents; entry++) {
657 const struct pci_device_id *id;
658 struct udevice *dev;
659 const struct driver *drv;
660
661 for (id = entry->match;
662 id->vendor || id->subvendor || id->class_mask;
663 id++) {
664 if (!pci_match_one_id(id, find_id))
665 continue;
666
667 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700668
669 /*
670 * In the pre-relocation phase, we only bind devices
671 * whose driver has the DM_FLAG_PRE_RELOC set, to save
672 * precious memory space as on some platforms as that
673 * space is pretty limited (ie: using Cache As RAM).
674 */
675 if (!(gd->flags & GD_FLG_RELOC) &&
676 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassc45abf12015-09-08 17:52:49 -0600677 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700678
Simon Glass318d71c2015-07-06 16:47:44 -0600679 /*
680 * We could pass the descriptor to the driver as
681 * platdata (instead of NULL) and allow its bind()
682 * method to return -ENOENT if it doesn't support this
683 * device. That way we could continue the search to
684 * find another driver. For now this doesn't seem
685 * necesssary, so just bind the first match.
686 */
687 ret = device_bind(parent, drv, drv->name, NULL, -1,
688 &dev);
689 if (ret)
690 goto error;
691 debug("%s: Match found: %s\n", __func__, drv->name);
Bin Menga8d27802018-08-03 01:14:44 -0700692 dev->driver_data = id->driver_data;
Simon Glass318d71c2015-07-06 16:47:44 -0600693 *devp = dev;
694 return 0;
695 }
696 }
697
Bin Meng984c0dc2015-08-20 06:40:17 -0700698 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
699 /*
700 * In the pre-relocation phase, we only bind bridge devices to save
701 * precious memory space as on some platforms as that space is pretty
702 * limited (ie: using Cache As RAM).
703 */
704 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glassc45abf12015-09-08 17:52:49 -0600705 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700706
Simon Glass318d71c2015-07-06 16:47:44 -0600707 /* Bind a generic driver so that the device can be used */
Bin Meng0a721522015-07-19 00:20:04 +0800708 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
709 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600710 str = strdup(name);
711 if (!str)
712 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700713 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
714
Simon Glass318d71c2015-07-06 16:47:44 -0600715 ret = device_bind_driver(parent, drv, str, devp);
716 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600717 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
xypron.glpk@gmx.dea89009c2017-05-08 20:40:16 +0200718 free(str);
Simon Glass318d71c2015-07-06 16:47:44 -0600719 return ret;
720 }
721 debug("%s: No match found: bound generic driver instead\n", __func__);
722
723 return 0;
724
725error:
726 debug("%s: No match found: error %d\n", __func__, ret);
727 return ret;
728}
729
Simon Glassb94dc892015-03-05 12:25:25 -0700730int pci_bind_bus_devices(struct udevice *bus)
731{
732 ulong vendor, device;
733 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800734 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700735 bool found_multi;
736 int ret;
737
738 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800739 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
740 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato1e3bce22016-04-25 15:41:01 +0900741 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800742 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700743 struct pci_child_platdata *pplat;
744 struct udevice *dev;
745 ulong class;
746
Bin Meng20bdc1e2018-08-03 01:14:37 -0700747 if (!PCI_FUNC(bdf))
748 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800749 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700750 continue;
751 /* Check only the first access, we don't expect problems */
Bin Meng0a721522015-07-19 00:20:04 +0800752 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
Simon Glassb94dc892015-03-05 12:25:25 -0700753 &header_type, PCI_SIZE_8);
754 if (ret)
755 goto error;
Bin Meng0a721522015-07-19 00:20:04 +0800756 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
Simon Glassb94dc892015-03-05 12:25:25 -0700757 PCI_SIZE_16);
758 if (vendor == 0xffff || vendor == 0x0000)
759 continue;
760
Bin Meng0a721522015-07-19 00:20:04 +0800761 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700762 found_multi = header_type & 0x80;
763
764 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
Bin Meng0a721522015-07-19 00:20:04 +0800765 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
766 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700767 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800768 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600769 PCI_SIZE_32);
770 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700771
772 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800773 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700774
Simon Glass413ebdb2015-11-29 13:18:09 -0700775 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700776 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600777 struct pci_device_id find_id;
778 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700779
Simon Glass318d71c2015-07-06 16:47:44 -0600780 memset(&find_id, '\0', sizeof(find_id));
781 find_id.vendor = vendor;
782 find_id.device = device;
783 find_id.class = class;
784 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800785 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600786 PCI_SUBSYSTEM_VENDOR_ID,
787 &val, PCI_SIZE_32);
788 find_id.subvendor = val & 0xffff;
789 find_id.subdevice = val >> 16;
790 }
Bin Meng0a721522015-07-19 00:20:04 +0800791 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600792 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700793 }
Simon Glassc45abf12015-09-08 17:52:49 -0600794 if (ret == -EPERM)
795 continue;
796 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700797 return ret;
798
799 /* Update the platform data */
Simon Glassc45abf12015-09-08 17:52:49 -0600800 pplat = dev_get_parent_platdata(dev);
801 pplat->devfn = PCI_MASK_BUS(bdf);
802 pplat->vendor = vendor;
803 pplat->device = device;
804 pplat->class = class;
Simon Glassb94dc892015-03-05 12:25:25 -0700805 }
806
807 return 0;
808error:
809 printf("Cannot read bus configuration: %d\n", ret);
810
811 return ret;
812}
813
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700814static void decode_regions(struct pci_controller *hose, ofnode parent_node,
815 ofnode node)
Simon Glassb94dc892015-03-05 12:25:25 -0700816{
817 int pci_addr_cells, addr_cells, size_cells;
818 int cells_per_record;
819 const u32 *prop;
820 int len;
821 int i;
822
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900823 prop = ofnode_get_property(node, "ranges", &len);
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700824 if (!prop) {
825 debug("%s: Cannot decode regions\n", __func__);
826 return;
827 }
828
Simon Glass4191dc12017-06-12 06:21:31 -0600829 pci_addr_cells = ofnode_read_simple_addr_cells(node);
830 addr_cells = ofnode_read_simple_addr_cells(parent_node);
831 size_cells = ofnode_read_simple_size_cells(node);
Simon Glassb94dc892015-03-05 12:25:25 -0700832
833 /* PCI addresses are always 3-cells */
834 len /= sizeof(u32);
835 cells_per_record = pci_addr_cells + addr_cells + size_cells;
836 hose->region_count = 0;
837 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
838 cells_per_record);
839 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
840 u64 pci_addr, addr, size;
841 int space_code;
842 u32 flags;
843 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700844 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700845
846 if (len < cells_per_record)
847 break;
848 flags = fdt32_to_cpu(prop[0]);
849 space_code = (flags >> 24) & 3;
850 pci_addr = fdtdec_get_number(prop + 1, 2);
851 prop += pci_addr_cells;
852 addr = fdtdec_get_number(prop, addr_cells);
853 prop += addr_cells;
854 size = fdtdec_get_number(prop, size_cells);
855 prop += size_cells;
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900856 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
857 __func__, hose->region_count, pci_addr, addr, size, space_code);
Simon Glassb94dc892015-03-05 12:25:25 -0700858 if (space_code & 2) {
859 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
860 PCI_REGION_MEM;
861 } else if (space_code & 1) {
862 type = PCI_REGION_IO;
863 } else {
864 continue;
865 }
Tuomas Tynkkynenc307e172018-05-14 18:47:50 +0300866
867 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
868 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
869 debug(" - beyond the 32-bit boundary, ignoring\n");
870 continue;
871 }
872
Simon Glass7efc9ba2015-11-19 20:26:58 -0700873 pos = -1;
874 for (i = 0; i < hose->region_count; i++) {
875 if (hose->regions[i].flags == type)
876 pos = i;
877 }
878 if (pos == -1)
879 pos = hose->region_count++;
880 debug(" - type=%d, pos=%d\n", type, pos);
881 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -0700882 }
883
884 /* Add a region for our local memory */
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100885#ifdef CONFIG_NR_DRAM_BANKS
886 bd_t *bd = gd->bd;
887
Bin Mengae0bdde2018-03-27 00:46:05 -0700888 if (!bd)
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700889 return;
Bin Mengae0bdde2018-03-27 00:46:05 -0700890
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100891 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
892 if (bd->bi_dram[i].size) {
893 pci_set_region(hose->regions + hose->region_count++,
894 bd->bi_dram[i].start,
895 bd->bi_dram[i].start,
896 bd->bi_dram[i].size,
897 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
898 }
899 }
900#else
901 phys_addr_t base = 0, size;
902
Simon Glass91de6c52015-11-19 20:26:57 -0700903 size = gd->ram_size;
904#ifdef CONFIG_SYS_SDRAM_BASE
905 base = CONFIG_SYS_SDRAM_BASE;
906#endif
907 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
908 size = gd->pci_ram_top - base;
Bin Meng6d532072018-03-27 00:46:06 -0700909 if (size)
910 pci_set_region(hose->regions + hose->region_count++, base,
911 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Bernhard Messerklinger9c5df382018-02-15 08:59:53 +0100912#endif
Simon Glassb94dc892015-03-05 12:25:25 -0700913
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700914 return;
Simon Glassb94dc892015-03-05 12:25:25 -0700915}
916
917static int pci_uclass_pre_probe(struct udevice *bus)
918{
919 struct pci_controller *hose;
Simon Glassb94dc892015-03-05 12:25:25 -0700920
921 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
922 bus->parent->name);
923 hose = bus->uclass_priv;
924
925 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +0100926 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700927 hose->ctlr = bus;
Christian Gmeiner5f4e0942018-06-10 06:25:05 -0700928 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
Simon Glassb94dc892015-03-05 12:25:25 -0700929 } else {
930 struct pci_controller *parent_hose;
931
932 parent_hose = dev_get_uclass_priv(bus->parent);
933 hose->ctlr = parent_hose->bus;
934 }
935 hose->bus = bus;
936 hose->first_busno = bus->seq;
937 hose->last_busno = bus->seq;
938
939 return 0;
940}
941
942static int pci_uclass_post_probe(struct udevice *bus)
943{
944 int ret;
945
Simon Glassb94dc892015-03-05 12:25:25 -0700946 debug("%s: probing bus %d\n", __func__, bus->seq);
947 ret = pci_bind_bus_devices(bus);
948 if (ret)
949 return ret;
950
951#ifdef CONFIG_PCI_PNP
952 ret = pci_auto_config_devices(bus);
Simon Glassb072d522015-09-08 17:52:47 -0600953 if (ret < 0)
954 return ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700955#endif
956
Bin Mengc0820a42015-08-20 06:40:23 -0700957#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
958 /*
959 * Per Intel FSP specification, we should call FSP notify API to
960 * inform FSP that PCI enumeration has been done so that FSP will
961 * do any necessary initialization as required by the chipset's
962 * BIOS Writer's Guide (BWG).
963 *
964 * Unfortunately we have to put this call here as with driver model,
965 * the enumeration is all done on a lazy basis as needed, so until
966 * something is touched on PCI it won't happen.
967 *
968 * Note we only call this 1) after U-Boot is relocated, and 2)
969 * root bus has finished probing.
970 */
Simon Glassb072d522015-09-08 17:52:47 -0600971 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Mengc0820a42015-08-20 06:40:23 -0700972 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -0600973 if (ret)
974 return ret;
975 }
Bin Mengc0820a42015-08-20 06:40:23 -0700976#endif
977
Simon Glassb072d522015-09-08 17:52:47 -0600978 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -0700979}
980
981static int pci_uclass_child_post_bind(struct udevice *dev)
982{
983 struct pci_child_platdata *pplat;
984 struct fdt_pci_addr addr;
985 int ret;
986
Simon Glass89d83232017-05-18 20:09:51 -0600987 if (!dev_of_valid(dev))
Simon Glassb94dc892015-03-05 12:25:25 -0700988 return 0;
989
Simon Glassb94dc892015-03-05 12:25:25 -0700990 pplat = dev_get_parent_platdata(dev);
Bin Meng00d808e2018-08-03 01:14:36 -0700991
992 /* Extract vendor id and device id if available */
993 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
994
995 /* Extract the devfn from fdt_pci_addr */
Simon Glass89d83232017-05-18 20:09:51 -0600996 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg",
997 &addr);
Simon Glassb94dc892015-03-05 12:25:25 -0700998 if (ret) {
999 if (ret != -ENOENT)
1000 return -EINVAL;
1001 } else {
Bin Meng44344a62015-08-20 06:40:26 -07001002 pplat->devfn = addr.phys_hi & 0xff00;
Simon Glassb94dc892015-03-05 12:25:25 -07001003 }
1004
1005 return 0;
1006}
1007
Bin Meng0a721522015-07-19 00:20:04 +08001008static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
1009 uint offset, ulong *valuep,
1010 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001011{
1012 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -07001013
1014 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1015}
1016
Bin Meng0a721522015-07-19 00:20:04 +08001017static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1018 uint offset, ulong value,
1019 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -07001020{
1021 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -07001022
1023 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1024}
1025
Simon Glass04c8b6a2015-08-10 07:05:04 -06001026static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1027{
1028 struct udevice *dev;
1029 int ret = 0;
1030
1031 /*
1032 * Scan through all the PCI controllers. On x86 there will only be one
1033 * but that is not necessarily true on other hardware.
1034 */
1035 do {
1036 device_find_first_child(bus, &dev);
1037 if (dev) {
1038 *devp = dev;
1039 return 0;
1040 }
1041 ret = uclass_next_device(&bus);
1042 if (ret)
1043 return ret;
1044 } while (bus);
1045
1046 return 0;
1047}
1048
1049int pci_find_next_device(struct udevice **devp)
1050{
1051 struct udevice *child = *devp;
1052 struct udevice *bus = child->parent;
1053 int ret;
1054
1055 /* First try all the siblings */
1056 *devp = NULL;
1057 while (child) {
1058 device_find_next_child(&child);
1059 if (child) {
1060 *devp = child;
1061 return 0;
1062 }
1063 }
1064
1065 /* We ran out of siblings. Try the next bus */
1066 ret = uclass_next_device(&bus);
1067 if (ret)
1068 return ret;
1069
1070 return bus ? skip_to_next_device(bus, devp) : 0;
1071}
1072
1073int pci_find_first_device(struct udevice **devp)
1074{
1075 struct udevice *bus;
1076 int ret;
1077
1078 *devp = NULL;
1079 ret = uclass_first_device(UCLASS_PCI, &bus);
1080 if (ret)
1081 return ret;
1082
1083 return skip_to_next_device(bus, devp);
1084}
1085
Simon Glass27a733f2015-11-19 20:26:59 -07001086ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1087{
1088 switch (size) {
1089 case PCI_SIZE_8:
1090 return (value >> ((offset & 3) * 8)) & 0xff;
1091 case PCI_SIZE_16:
1092 return (value >> ((offset & 2) * 8)) & 0xffff;
1093 default:
1094 return value;
1095 }
1096}
1097
1098ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1099 enum pci_size_t size)
1100{
1101 uint off_mask;
1102 uint val_mask, shift;
1103 ulong ldata, mask;
1104
1105 switch (size) {
1106 case PCI_SIZE_8:
1107 off_mask = 3;
1108 val_mask = 0xff;
1109 break;
1110 case PCI_SIZE_16:
1111 off_mask = 2;
1112 val_mask = 0xffff;
1113 break;
1114 default:
1115 return value;
1116 }
1117 shift = (offset & off_mask) * 8;
1118 ldata = (value & val_mask) << shift;
1119 mask = val_mask << shift;
1120 value = (old & ~mask) | ldata;
1121
1122 return value;
1123}
1124
Simon Glassdcdc0122015-11-19 20:27:01 -07001125int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1126 struct pci_region **memp, struct pci_region **prefp)
1127{
1128 struct udevice *bus = pci_get_controller(dev);
1129 struct pci_controller *hose = dev_get_uclass_priv(bus);
1130 int i;
1131
1132 *iop = NULL;
1133 *memp = NULL;
1134 *prefp = NULL;
1135 for (i = 0; i < hose->region_count; i++) {
1136 switch (hose->regions[i].flags) {
1137 case PCI_REGION_IO:
1138 if (!*iop || (*iop)->size < hose->regions[i].size)
1139 *iop = hose->regions + i;
1140 break;
1141 case PCI_REGION_MEM:
1142 if (!*memp || (*memp)->size < hose->regions[i].size)
1143 *memp = hose->regions + i;
1144 break;
1145 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1146 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1147 *prefp = hose->regions + i;
1148 break;
1149 }
1150 }
1151
1152 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1153}
1154
Simon Glass3452cb12015-11-29 13:17:53 -07001155u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
1156{
1157 u32 addr;
1158 int bar;
1159
1160 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1161 dm_pci_read_config32(dev, bar, &addr);
1162 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1163 return addr & PCI_BASE_ADDRESS_IO_MASK;
1164 else
1165 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1166}
1167
Simon Glasse2b6b562016-01-18 20:19:15 -07001168void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1169{
1170 int bar;
1171
1172 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1173 dm_pci_write_config32(dev, bar, addr);
1174}
1175
Simon Glassc5f053b2015-11-29 13:18:03 -07001176static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1177 pci_addr_t bus_addr, unsigned long flags,
1178 unsigned long skip_mask, phys_addr_t *pa)
1179{
1180 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1181 struct pci_region *res;
1182 int i;
1183
Christian Gmeiner7241f802018-06-10 06:25:06 -07001184 if (hose->region_count == 0) {
1185 *pa = bus_addr;
1186 return 0;
1187 }
1188
Simon Glassc5f053b2015-11-29 13:18:03 -07001189 for (i = 0; i < hose->region_count; i++) {
1190 res = &hose->regions[i];
1191
1192 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1193 continue;
1194
1195 if (res->flags & skip_mask)
1196 continue;
1197
1198 if (bus_addr >= res->bus_start &&
1199 (bus_addr - res->bus_start) < res->size) {
1200 *pa = (bus_addr - res->bus_start + res->phys_start);
1201 return 0;
1202 }
1203 }
1204
1205 return 1;
1206}
1207
1208phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1209 unsigned long flags)
1210{
1211 phys_addr_t phys_addr = 0;
1212 struct udevice *ctlr;
1213 int ret;
1214
1215 /* The root controller has the region information */
1216 ctlr = pci_get_controller(dev);
1217
1218 /*
1219 * if PCI_REGION_MEM is set we do a two pass search with preference
1220 * on matches that don't have PCI_REGION_SYS_MEMORY set
1221 */
1222 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1223 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1224 flags, PCI_REGION_SYS_MEMORY,
1225 &phys_addr);
1226 if (!ret)
1227 return phys_addr;
1228 }
1229
1230 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1231
1232 if (ret)
1233 puts("pci_hose_bus_to_phys: invalid physical address\n");
1234
1235 return phys_addr;
1236}
1237
1238int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1239 unsigned long flags, unsigned long skip_mask,
1240 pci_addr_t *ba)
1241{
1242 struct pci_region *res;
1243 struct udevice *ctlr;
1244 pci_addr_t bus_addr;
1245 int i;
1246 struct pci_controller *hose;
1247
1248 /* The root controller has the region information */
1249 ctlr = pci_get_controller(dev);
1250 hose = dev_get_uclass_priv(ctlr);
1251
Christian Gmeiner7241f802018-06-10 06:25:06 -07001252 if (hose->region_count == 0) {
1253 *ba = phys_addr;
1254 return 0;
1255 }
1256
Simon Glassc5f053b2015-11-29 13:18:03 -07001257 for (i = 0; i < hose->region_count; i++) {
1258 res = &hose->regions[i];
1259
1260 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1261 continue;
1262
1263 if (res->flags & skip_mask)
1264 continue;
1265
1266 bus_addr = phys_addr - res->phys_start + res->bus_start;
1267
1268 if (bus_addr >= res->bus_start &&
1269 (bus_addr - res->bus_start) < res->size) {
1270 *ba = bus_addr;
1271 return 0;
1272 }
1273 }
1274
1275 return 1;
1276}
1277
1278pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1279 unsigned long flags)
1280{
1281 pci_addr_t bus_addr = 0;
1282 int ret;
1283
1284 /*
1285 * if PCI_REGION_MEM is set we do a two pass search with preference
1286 * on matches that don't have PCI_REGION_SYS_MEMORY set
1287 */
1288 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1289 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1290 PCI_REGION_SYS_MEMORY, &bus_addr);
1291 if (!ret)
1292 return bus_addr;
1293 }
1294
1295 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1296
1297 if (ret)
1298 puts("pci_hose_phys_to_bus: invalid physical address\n");
1299
1300 return bus_addr;
1301}
1302
1303void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1304{
1305 pci_addr_t pci_bus_addr;
1306 u32 bar_response;
1307
1308 /* read BAR address */
1309 dm_pci_read_config32(dev, bar, &bar_response);
1310 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1311
1312 /*
1313 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1314 * isn't actualy used on any platform because u-boot assumes a static
1315 * linear mapping. In the future, this could read the BAR size
1316 * and pass that as the size if needed.
1317 */
1318 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1319}
1320
Bin Menga7366f02018-08-03 01:14:52 -07001321int dm_pci_find_capability(struct udevice *dev, int cap)
1322{
1323 u16 status;
1324 u8 header_type;
1325 int ttl = PCI_FIND_CAP_TTL;
1326 u8 id;
1327 u16 ent;
1328 u8 pos;
1329
1330 dm_pci_read_config16(dev, PCI_STATUS, &status);
1331 if (!(status & PCI_STATUS_CAP_LIST))
1332 return 0;
1333
1334 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1335 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1336 pos = PCI_CB_CAPABILITY_LIST;
1337 else
1338 pos = PCI_CAPABILITY_LIST;
1339
1340 dm_pci_read_config8(dev, pos, &pos);
1341 while (ttl--) {
1342 if (pos < PCI_STD_HEADER_SIZEOF)
1343 break;
1344 pos &= ~3;
1345 dm_pci_read_config16(dev, pos, &ent);
1346
1347 id = ent & 0xff;
1348 if (id == 0xff)
1349 break;
1350 if (id == cap)
1351 return pos;
1352 pos = (ent >> 8);
1353 }
1354
1355 return 0;
1356}
1357
1358int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1359{
1360 u32 header;
1361 int ttl;
1362 int pos = PCI_CFG_SPACE_SIZE;
1363
1364 /* minimum 8 bytes per capability */
1365 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1366
1367 dm_pci_read_config32(dev, pos, &header);
1368 /*
1369 * If we have no capabilities, this is indicated by cap ID,
1370 * cap version and next pointer all being 0.
1371 */
1372 if (header == 0)
1373 return 0;
1374
1375 while (ttl--) {
1376 if (PCI_EXT_CAP_ID(header) == cap)
1377 return pos;
1378
1379 pos = PCI_EXT_CAP_NEXT(header);
1380 if (pos < PCI_CFG_SPACE_SIZE)
1381 break;
1382
1383 dm_pci_read_config32(dev, pos, &header);
1384 }
1385
1386 return 0;
1387}
1388
Simon Glassb94dc892015-03-05 12:25:25 -07001389UCLASS_DRIVER(pci) = {
1390 .id = UCLASS_PCI,
1391 .name = "pci",
Simon Glassa8149412015-05-10 21:08:06 -06001392 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -06001393 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001394 .pre_probe = pci_uclass_pre_probe,
1395 .post_probe = pci_uclass_post_probe,
1396 .child_post_bind = pci_uclass_child_post_bind,
1397 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1398 .per_child_platdata_auto_alloc_size =
1399 sizeof(struct pci_child_platdata),
1400};
1401
1402static const struct dm_pci_ops pci_bridge_ops = {
1403 .read_config = pci_bridge_read_config,
1404 .write_config = pci_bridge_write_config,
1405};
1406
1407static const struct udevice_id pci_bridge_ids[] = {
1408 { .compatible = "pci-bridge" },
1409 { }
1410};
1411
1412U_BOOT_DRIVER(pci_bridge_drv) = {
1413 .name = "pci_bridge_drv",
1414 .id = UCLASS_PCI,
1415 .of_match = pci_bridge_ids,
1416 .ops = &pci_bridge_ops,
1417};
1418
1419UCLASS_DRIVER(pci_generic) = {
1420 .id = UCLASS_PCI_GENERIC,
1421 .name = "pci_generic",
1422};
1423
1424static const struct udevice_id pci_generic_ids[] = {
1425 { .compatible = "pci-generic" },
1426 { }
1427};
1428
1429U_BOOT_DRIVER(pci_generic_drv) = {
1430 .name = "pci_generic_drv",
1431 .id = UCLASS_PCI_GENERIC,
1432 .of_match = pci_generic_ids,
1433};
Stephen Warren04eb2692016-01-26 11:10:11 -07001434
1435void pci_init(void)
1436{
1437 struct udevice *bus;
1438
1439 /*
1440 * Enumerate all known controller devices. Enumeration has the side-
1441 * effect of probing them, so PCIe devices will be enumerated too.
1442 */
1443 for (uclass_first_device(UCLASS_PCI, &bus);
1444 bus;
1445 uclass_next_device(&bus)) {
1446 ;
1447 }
1448}