blob: ad65427801f9cc4dd94fdf9a55615b4a165d237d [file] [log] [blame]
Simon Glassb94dc892015-03-05 12:25:25 -07001/*
2 * Copyright (c) 2014 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <fdtdec.h>
12#include <inttypes.h>
13#include <pci.h>
14#include <dm/lists.h>
15#include <dm/root.h>
16#include <dm/device-internal.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20struct pci_controller *pci_bus_to_hose(int busnum)
21{
22 struct udevice *bus;
23 int ret;
24
25 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
26 if (ret) {
27 debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
28 return NULL;
29 }
30 return dev_get_uclass_priv(bus);
31}
32
Simon Glassc9118d42015-07-06 16:47:46 -060033pci_dev_t pci_get_bdf(struct udevice *dev)
34{
35 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
36 struct udevice *bus = dev->parent;
37
38 return PCI_ADD_BUS(bus->seq, pplat->devfn);
39}
40
Simon Glassb94dc892015-03-05 12:25:25 -070041/**
42 * pci_get_bus_max() - returns the bus number of the last active bus
43 *
44 * @return last bus number, or -1 if no active buses
45 */
46static int pci_get_bus_max(void)
47{
48 struct udevice *bus;
49 struct uclass *uc;
50 int ret = -1;
51
52 ret = uclass_get(UCLASS_PCI, &uc);
53 uclass_foreach_dev(bus, uc) {
54 if (bus->seq > ret)
55 ret = bus->seq;
56 }
57
58 debug("%s: ret=%d\n", __func__, ret);
59
60 return ret;
61}
62
63int pci_last_busno(void)
64{
65 struct pci_controller *hose;
66 struct udevice *bus;
67 struct uclass *uc;
68 int ret;
69
70 debug("pci_last_busno\n");
71 ret = uclass_get(UCLASS_PCI, &uc);
72 if (ret || list_empty(&uc->dev_head))
73 return -1;
74
75 /* Probe the last bus */
76 bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
77 debug("bus = %p, %s\n", bus, bus->name);
78 assert(bus);
79 ret = device_probe(bus);
80 if (ret)
81 return ret;
82
83 /* If that bus has bridges, we may have new buses now. Get the last */
84 bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
85 hose = dev_get_uclass_priv(bus);
86 debug("bus = %s, hose = %p\n", bus->name, hose);
87
88 return hose->last_busno;
89}
90
91int pci_get_ff(enum pci_size_t size)
92{
93 switch (size) {
94 case PCI_SIZE_8:
95 return 0xff;
96 case PCI_SIZE_16:
97 return 0xffff;
98 default:
99 return 0xffffffff;
100 }
101}
102
103int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
104 struct udevice **devp)
105{
106 struct udevice *dev;
107
108 for (device_find_first_child(bus, &dev);
109 dev;
110 device_find_next_child(&dev)) {
111 struct pci_child_platdata *pplat;
112
113 pplat = dev_get_parent_platdata(dev);
114 if (pplat && pplat->devfn == find_devfn) {
115 *devp = dev;
116 return 0;
117 }
118 }
119
120 return -ENODEV;
121}
122
123int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
124{
125 struct udevice *bus;
126 int ret;
127
128 ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
129 if (ret)
130 return ret;
131 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
132}
133
134static int pci_device_matches_ids(struct udevice *dev,
135 struct pci_device_id *ids)
136{
137 struct pci_child_platdata *pplat;
138 int i;
139
140 pplat = dev_get_parent_platdata(dev);
141 if (!pplat)
142 return -EINVAL;
143 for (i = 0; ids[i].vendor != 0; i++) {
144 if (pplat->vendor == ids[i].vendor &&
145 pplat->device == ids[i].device)
146 return i;
147 }
148
149 return -EINVAL;
150}
151
152int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
153 int *indexp, struct udevice **devp)
154{
155 struct udevice *dev;
156
157 /* Scan all devices on this bus */
158 for (device_find_first_child(bus, &dev);
159 dev;
160 device_find_next_child(&dev)) {
161 if (pci_device_matches_ids(dev, ids) >= 0) {
162 if ((*indexp)-- <= 0) {
163 *devp = dev;
164 return 0;
165 }
166 }
167 }
168
169 return -ENODEV;
170}
171
172int pci_find_device_id(struct pci_device_id *ids, int index,
173 struct udevice **devp)
174{
175 struct udevice *bus;
176
177 /* Scan all known buses */
178 for (uclass_first_device(UCLASS_PCI, &bus);
179 bus;
180 uclass_next_device(&bus)) {
181 if (!pci_bus_find_devices(bus, ids, &index, devp))
182 return 0;
183 }
184 *devp = NULL;
185
186 return -ENODEV;
187}
188
189int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
190 unsigned long value, enum pci_size_t size)
191{
192 struct dm_pci_ops *ops;
193
194 ops = pci_get_ops(bus);
195 if (!ops->write_config)
196 return -ENOSYS;
197 return ops->write_config(bus, bdf, offset, value, size);
198}
199
200int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
201 enum pci_size_t size)
202{
203 struct udevice *bus;
204 int ret;
205
206 ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
207 if (ret)
208 return ret;
209
Bin Meng0a721522015-07-19 00:20:04 +0800210 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700211}
212
Simon Glass94ef2422015-08-10 07:05:03 -0600213int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
214 enum pci_size_t size)
215{
216 struct udevice *bus;
217
218 for (bus = dev; device_get_uclass_id(bus->parent) == UCLASS_PCI;)
219 bus = bus->parent;
220 return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
221}
222
223
Simon Glassb94dc892015-03-05 12:25:25 -0700224int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
225{
226 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
227}
228
229int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
230{
231 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
232}
233
234int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
235{
236 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
237}
238
Simon Glass94ef2422015-08-10 07:05:03 -0600239int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
240{
241 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
242}
243
244int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
245{
246 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
247}
248
249int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
250{
251 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
252}
253
Simon Glassb94dc892015-03-05 12:25:25 -0700254int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
255 unsigned long *valuep, enum pci_size_t size)
256{
257 struct dm_pci_ops *ops;
258
259 ops = pci_get_ops(bus);
260 if (!ops->read_config)
261 return -ENOSYS;
262 return ops->read_config(bus, bdf, offset, valuep, size);
263}
264
265int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
266 enum pci_size_t size)
267{
268 struct udevice *bus;
269 int ret;
270
271 ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
272 if (ret)
273 return ret;
274
Bin Meng0a721522015-07-19 00:20:04 +0800275 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700276}
277
Simon Glass94ef2422015-08-10 07:05:03 -0600278int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
279 enum pci_size_t size)
280{
281 struct udevice *bus;
282
283 for (bus = dev; device_get_uclass_id(bus->parent) == UCLASS_PCI;)
284 bus = bus->parent;
285 return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
286 size);
287}
288
Simon Glassb94dc892015-03-05 12:25:25 -0700289int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
290{
291 unsigned long value;
292 int ret;
293
294 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
295 if (ret)
296 return ret;
297 *valuep = value;
298
299 return 0;
300}
301
302int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
303{
304 unsigned long value;
305 int ret;
306
307 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
308 if (ret)
309 return ret;
310 *valuep = value;
311
312 return 0;
313}
314
315int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
316{
317 unsigned long value;
318 int ret;
319
320 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
321 if (ret)
322 return ret;
323 *valuep = value;
324
325 return 0;
326}
327
Simon Glass94ef2422015-08-10 07:05:03 -0600328int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
329{
330 unsigned long value;
331 int ret;
332
333 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
334 if (ret)
335 return ret;
336 *valuep = value;
337
338 return 0;
339}
340
341int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
342{
343 unsigned long value;
344 int ret;
345
346 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
347 if (ret)
348 return ret;
349 *valuep = value;
350
351 return 0;
352}
353
354int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
355{
356 unsigned long value;
357 int ret;
358
359 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
360 if (ret)
361 return ret;
362 *valuep = value;
363
364 return 0;
365}
366
Simon Glassb94dc892015-03-05 12:25:25 -0700367int pci_auto_config_devices(struct udevice *bus)
368{
369 struct pci_controller *hose = bus->uclass_priv;
370 unsigned int sub_bus;
371 struct udevice *dev;
372 int ret;
373
374 sub_bus = bus->seq;
375 debug("%s: start\n", __func__);
376 pciauto_config_init(hose);
377 for (ret = device_find_first_child(bus, &dev);
378 !ret && dev;
379 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700380 unsigned int max_bus;
Simon Glassb94dc892015-03-05 12:25:25 -0700381
Simon Glassb94dc892015-03-05 12:25:25 -0700382 debug("%s: device %s\n", __func__, dev->name);
Bin Meng39164092015-07-19 00:20:06 +0800383 max_bus = pciauto_config_device(hose, pci_get_bdf(dev));
Simon Glassb94dc892015-03-05 12:25:25 -0700384 sub_bus = max(sub_bus, max_bus);
385 }
386 debug("%s: done\n", __func__);
387
388 return sub_bus;
389}
390
391int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
392{
393 struct udevice *parent, *bus;
394 int sub_bus;
395 int ret;
396
397 debug("%s\n", __func__);
398 parent = hose->bus;
399
400 /* Find the bus within the parent */
Bin Meng877bff82015-07-19 00:20:05 +0800401 ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700402 if (ret) {
403 debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
404 bdf, parent->name, ret);
405 return ret;
406 }
407
408 sub_bus = pci_get_bus_max() + 1;
409 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glassb87014c2015-06-07 08:50:41 -0600410 pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700411
412 ret = device_probe(bus);
413 if (ret) {
414 debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
415 ret);
416 return ret;
417 }
418 if (sub_bus != bus->seq) {
419 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
420 __func__, bus->name, bus->seq, sub_bus);
421 return -EPIPE;
422 }
423 sub_bus = pci_get_bus_max();
424 pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
425
426 return sub_bus;
427}
428
Simon Glass318d71c2015-07-06 16:47:44 -0600429/**
430 * pci_match_one_device - Tell if a PCI device structure has a matching
431 * PCI device id structure
432 * @id: single PCI device id structure to match
433 * @dev: the PCI device structure to match against
434 *
435 * Returns the matching pci_device_id structure or %NULL if there is no match.
436 */
437static bool pci_match_one_id(const struct pci_device_id *id,
438 const struct pci_device_id *find)
439{
440 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
441 (id->device == PCI_ANY_ID || id->device == find->device) &&
442 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
443 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
444 !((id->class ^ find->class) & id->class_mask))
445 return true;
446
447 return false;
448}
449
450/**
451 * pci_find_and_bind_driver() - Find and bind the right PCI driver
452 *
453 * This only looks at certain fields in the descriptor.
454 */
455static int pci_find_and_bind_driver(struct udevice *parent,
Bin Meng0a721522015-07-19 00:20:04 +0800456 struct pci_device_id *find_id, pci_dev_t bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600457 struct udevice **devp)
458{
459 struct pci_driver_entry *start, *entry;
460 const char *drv;
461 int n_ents;
462 int ret;
463 char name[30], *str;
464
465 *devp = NULL;
466
467 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
468 find_id->vendor, find_id->device);
469 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
470 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
471 for (entry = start; entry != start + n_ents; entry++) {
472 const struct pci_device_id *id;
473 struct udevice *dev;
474 const struct driver *drv;
475
476 for (id = entry->match;
477 id->vendor || id->subvendor || id->class_mask;
478 id++) {
479 if (!pci_match_one_id(id, find_id))
480 continue;
481
482 drv = entry->driver;
483 /*
484 * We could pass the descriptor to the driver as
485 * platdata (instead of NULL) and allow its bind()
486 * method to return -ENOENT if it doesn't support this
487 * device. That way we could continue the search to
488 * find another driver. For now this doesn't seem
489 * necesssary, so just bind the first match.
490 */
491 ret = device_bind(parent, drv, drv->name, NULL, -1,
492 &dev);
493 if (ret)
494 goto error;
495 debug("%s: Match found: %s\n", __func__, drv->name);
496 dev->driver_data = find_id->driver_data;
497 *devp = dev;
498 return 0;
499 }
500 }
501
502 /* Bind a generic driver so that the device can be used */
Bin Meng0a721522015-07-19 00:20:04 +0800503 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
504 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600505 str = strdup(name);
506 if (!str)
507 return -ENOMEM;
508 drv = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI ? "pci_bridge_drv" :
509 "pci_generic_drv";
510 ret = device_bind_driver(parent, drv, str, devp);
511 if (ret) {
512 debug("%s: Failed to bind generic driver: %d", __func__, ret);
513 return ret;
514 }
515 debug("%s: No match found: bound generic driver instead\n", __func__);
516
517 return 0;
518
519error:
520 debug("%s: No match found: error %d\n", __func__, ret);
521 return ret;
522}
523
Simon Glassb94dc892015-03-05 12:25:25 -0700524int pci_bind_bus_devices(struct udevice *bus)
525{
526 ulong vendor, device;
527 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800528 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700529 bool found_multi;
530 int ret;
531
532 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800533 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
534 PCI_MAX_PCI_FUNCTIONS - 1);
535 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
536 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700537 struct pci_child_platdata *pplat;
538 struct udevice *dev;
539 ulong class;
540
Bin Meng0a721522015-07-19 00:20:04 +0800541 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700542 continue;
543 /* Check only the first access, we don't expect problems */
Bin Meng0a721522015-07-19 00:20:04 +0800544 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
Simon Glassb94dc892015-03-05 12:25:25 -0700545 &header_type, PCI_SIZE_8);
546 if (ret)
547 goto error;
Bin Meng0a721522015-07-19 00:20:04 +0800548 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
Simon Glassb94dc892015-03-05 12:25:25 -0700549 PCI_SIZE_16);
550 if (vendor == 0xffff || vendor == 0x0000)
551 continue;
552
Bin Meng0a721522015-07-19 00:20:04 +0800553 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700554 found_multi = header_type & 0x80;
555
556 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
Bin Meng0a721522015-07-19 00:20:04 +0800557 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
558 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700559 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800560 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600561 PCI_SIZE_32);
562 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700563
564 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800565 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700566
Simon Glass318d71c2015-07-06 16:47:44 -0600567 /* Search for a driver */
568
Simon Glassb94dc892015-03-05 12:25:25 -0700569 /* If nothing in the device tree, bind a generic device */
570 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600571 struct pci_device_id find_id;
572 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700573
Simon Glass318d71c2015-07-06 16:47:44 -0600574 memset(&find_id, '\0', sizeof(find_id));
575 find_id.vendor = vendor;
576 find_id.device = device;
577 find_id.class = class;
578 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800579 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600580 PCI_SUBSYSTEM_VENDOR_ID,
581 &val, PCI_SIZE_32);
582 find_id.subvendor = val & 0xffff;
583 find_id.subdevice = val >> 16;
584 }
Bin Meng0a721522015-07-19 00:20:04 +0800585 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600586 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700587 }
588 if (ret)
589 return ret;
590
591 /* Update the platform data */
592 pplat = dev_get_parent_platdata(dev);
Bin Meng0a721522015-07-19 00:20:04 +0800593 pplat->devfn = PCI_MASK_BUS(bdf);
Simon Glassb94dc892015-03-05 12:25:25 -0700594 pplat->vendor = vendor;
595 pplat->device = device;
596 pplat->class = class;
597 }
598
599 return 0;
600error:
601 printf("Cannot read bus configuration: %d\n", ret);
602
603 return ret;
604}
605
606static int pci_uclass_post_bind(struct udevice *bus)
607{
608 /*
609 * Scan the device tree for devices. This does not probe the PCI bus,
610 * as this is not permitted while binding. It just finds devices
611 * mentioned in the device tree.
612 *
613 * Before relocation, only bind devices marked for pre-relocation
614 * use.
615 */
616 return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
617 gd->flags & GD_FLG_RELOC ? false : true);
618}
619
620static int decode_regions(struct pci_controller *hose, const void *blob,
621 int parent_node, int node)
622{
623 int pci_addr_cells, addr_cells, size_cells;
624 int cells_per_record;
Simon Glass5888bd22015-07-03 18:28:27 -0600625 phys_addr_t addr;
Simon Glassb94dc892015-03-05 12:25:25 -0700626 const u32 *prop;
627 int len;
628 int i;
629
630 prop = fdt_getprop(blob, node, "ranges", &len);
631 if (!prop)
632 return -EINVAL;
633 pci_addr_cells = fdt_address_cells(blob, node);
634 addr_cells = fdt_address_cells(blob, parent_node);
635 size_cells = fdt_size_cells(blob, node);
636
637 /* PCI addresses are always 3-cells */
638 len /= sizeof(u32);
639 cells_per_record = pci_addr_cells + addr_cells + size_cells;
640 hose->region_count = 0;
641 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
642 cells_per_record);
643 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
644 u64 pci_addr, addr, size;
645 int space_code;
646 u32 flags;
647 int type;
648
649 if (len < cells_per_record)
650 break;
651 flags = fdt32_to_cpu(prop[0]);
652 space_code = (flags >> 24) & 3;
653 pci_addr = fdtdec_get_number(prop + 1, 2);
654 prop += pci_addr_cells;
655 addr = fdtdec_get_number(prop, addr_cells);
656 prop += addr_cells;
657 size = fdtdec_get_number(prop, size_cells);
658 prop += size_cells;
659 debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
660 ", size=%" PRIx64 ", space_code=%d\n", __func__,
661 hose->region_count, pci_addr, addr, size, space_code);
662 if (space_code & 2) {
663 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
664 PCI_REGION_MEM;
665 } else if (space_code & 1) {
666 type = PCI_REGION_IO;
667 } else {
668 continue;
669 }
670 debug(" - type=%d\n", type);
671 pci_set_region(hose->regions + hose->region_count++, pci_addr,
672 addr, size, type);
673 }
674
675 /* Add a region for our local memory */
Simon Glass5888bd22015-07-03 18:28:27 -0600676 addr = gd->ram_size;
677 if (gd->pci_ram_top && gd->pci_ram_top < addr)
678 addr = gd->pci_ram_top;
679 pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
680 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Simon Glassb94dc892015-03-05 12:25:25 -0700681
682 return 0;
683}
684
685static int pci_uclass_pre_probe(struct udevice *bus)
686{
687 struct pci_controller *hose;
688 int ret;
689
690 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
691 bus->parent->name);
692 hose = bus->uclass_priv;
693
694 /* For bridges, use the top-level PCI controller */
695 if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
696 hose->ctlr = bus;
697 ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
698 bus->of_offset);
699 if (ret) {
700 debug("%s: Cannot decode regions\n", __func__);
701 return ret;
702 }
703 } else {
704 struct pci_controller *parent_hose;
705
706 parent_hose = dev_get_uclass_priv(bus->parent);
707 hose->ctlr = parent_hose->bus;
708 }
709 hose->bus = bus;
710 hose->first_busno = bus->seq;
711 hose->last_busno = bus->seq;
712
713 return 0;
714}
715
716static int pci_uclass_post_probe(struct udevice *bus)
717{
718 int ret;
719
Simon Glassb94dc892015-03-05 12:25:25 -0700720 debug("%s: probing bus %d\n", __func__, bus->seq);
721 ret = pci_bind_bus_devices(bus);
722 if (ret)
723 return ret;
724
725#ifdef CONFIG_PCI_PNP
726 ret = pci_auto_config_devices(bus);
727#endif
728
729 return ret < 0 ? ret : 0;
730}
731
732static int pci_uclass_child_post_bind(struct udevice *dev)
733{
734 struct pci_child_platdata *pplat;
735 struct fdt_pci_addr addr;
736 int ret;
737
738 if (dev->of_offset == -1)
739 return 0;
740
741 /*
742 * We could read vendor, device, class if available. But for now we
743 * just check the address.
744 */
745 pplat = dev_get_parent_platdata(dev);
746 ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
747 FDT_PCI_SPACE_CONFIG, "reg", &addr);
748
749 if (ret) {
750 if (ret != -ENOENT)
751 return -EINVAL;
752 } else {
753 /* extract the bdf from fdt_pci_addr */
754 pplat->devfn = addr.phys_hi & 0xffff00;
755 }
756
757 return 0;
758}
759
Bin Meng0a721522015-07-19 00:20:04 +0800760static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
761 uint offset, ulong *valuep,
762 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700763{
764 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -0700765
766 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
767}
768
Bin Meng0a721522015-07-19 00:20:04 +0800769static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
770 uint offset, ulong value,
771 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700772{
773 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -0700774
775 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
776}
777
Simon Glass04c8b6a2015-08-10 07:05:04 -0600778static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
779{
780 struct udevice *dev;
781 int ret = 0;
782
783 /*
784 * Scan through all the PCI controllers. On x86 there will only be one
785 * but that is not necessarily true on other hardware.
786 */
787 do {
788 device_find_first_child(bus, &dev);
789 if (dev) {
790 *devp = dev;
791 return 0;
792 }
793 ret = uclass_next_device(&bus);
794 if (ret)
795 return ret;
796 } while (bus);
797
798 return 0;
799}
800
801int pci_find_next_device(struct udevice **devp)
802{
803 struct udevice *child = *devp;
804 struct udevice *bus = child->parent;
805 int ret;
806
807 /* First try all the siblings */
808 *devp = NULL;
809 while (child) {
810 device_find_next_child(&child);
811 if (child) {
812 *devp = child;
813 return 0;
814 }
815 }
816
817 /* We ran out of siblings. Try the next bus */
818 ret = uclass_next_device(&bus);
819 if (ret)
820 return ret;
821
822 return bus ? skip_to_next_device(bus, devp) : 0;
823}
824
825int pci_find_first_device(struct udevice **devp)
826{
827 struct udevice *bus;
828 int ret;
829
830 *devp = NULL;
831 ret = uclass_first_device(UCLASS_PCI, &bus);
832 if (ret)
833 return ret;
834
835 return skip_to_next_device(bus, devp);
836}
837
Simon Glassb94dc892015-03-05 12:25:25 -0700838UCLASS_DRIVER(pci) = {
839 .id = UCLASS_PCI,
840 .name = "pci",
Simon Glassa8149412015-05-10 21:08:06 -0600841 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassb94dc892015-03-05 12:25:25 -0700842 .post_bind = pci_uclass_post_bind,
843 .pre_probe = pci_uclass_pre_probe,
844 .post_probe = pci_uclass_post_probe,
845 .child_post_bind = pci_uclass_child_post_bind,
846 .per_device_auto_alloc_size = sizeof(struct pci_controller),
847 .per_child_platdata_auto_alloc_size =
848 sizeof(struct pci_child_platdata),
849};
850
851static const struct dm_pci_ops pci_bridge_ops = {
852 .read_config = pci_bridge_read_config,
853 .write_config = pci_bridge_write_config,
854};
855
856static const struct udevice_id pci_bridge_ids[] = {
857 { .compatible = "pci-bridge" },
858 { }
859};
860
861U_BOOT_DRIVER(pci_bridge_drv) = {
862 .name = "pci_bridge_drv",
863 .id = UCLASS_PCI,
864 .of_match = pci_bridge_ids,
865 .ops = &pci_bridge_ops,
866};
867
868UCLASS_DRIVER(pci_generic) = {
869 .id = UCLASS_PCI_GENERIC,
870 .name = "pci_generic",
871};
872
873static const struct udevice_id pci_generic_ids[] = {
874 { .compatible = "pci-generic" },
875 { }
876};
877
878U_BOOT_DRIVER(pci_generic_drv) = {
879 .name = "pci_generic_drv",
880 .id = UCLASS_PCI_GENERIC,
881 .of_match = pci_generic_ids,
882};