blob: b79927e453b4e48accd60032e4ba07959dc6d2aa [file] [log] [blame]
Simon Glassff3e0772015-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 Glass4b515e42015-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 Glassff3e0772015-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 Meng4d8615c2015-07-19 00:20:04 +0800210 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700211}
212
Simon Glass66afb4e2015-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 Glassff3e0772015-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 Glass66afb4e2015-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 Glassff3e0772015-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 Meng4d8615c2015-07-19 00:20:04 +0800275 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700276}
277
Simon Glass66afb4e2015-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 Glassff3e0772015-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 Glass66afb4e2015-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 Glassff3e0772015-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 Glassff3e0772015-03-05 12:25:25 -0700380 unsigned int max_bus;
Simon Glassff3e0772015-03-05 12:25:25 -0700381
Simon Glassff3e0772015-03-05 12:25:25 -0700382 debug("%s: device %s\n", __func__, dev->name);
Bin Mengd11d9ef2015-07-19 00:20:06 +0800383 max_bus = pciauto_config_device(hose, pci_get_bdf(dev));
Simon Glassff3e0772015-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 Meng8326f132015-07-19 00:20:05 +0800401 ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
Simon Glassff3e0772015-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 Glass5afeb4b2015-06-07 08:50:41 -0600410 pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
Simon Glassff3e0772015-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 Glassaba92962015-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 Meng4d8615c2015-07-19 00:20:04 +0800456 struct pci_device_id *find_id, pci_dev_t bdf,
Simon Glassaba92962015-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 Meng4d8615c2015-07-19 00:20:04 +0800503 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
504 PCI_FUNC(bdf));
Simon Glassaba92962015-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 Glassff3e0772015-03-05 12:25:25 -0700524int pci_bind_bus_devices(struct udevice *bus)
525{
526 ulong vendor, device;
527 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800528 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700529 bool found_multi;
530 int ret;
531
532 found_multi = false;
Bin Meng4d8615c2015-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 Glassff3e0772015-03-05 12:25:25 -0700537 struct pci_child_platdata *pplat;
538 struct udevice *dev;
539 ulong class;
540
Bin Meng4d8615c2015-07-19 00:20:04 +0800541 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700542 continue;
543 /* Check only the first access, we don't expect problems */
Bin Meng4d8615c2015-07-19 00:20:04 +0800544 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
Simon Glassff3e0772015-03-05 12:25:25 -0700545 &header_type, PCI_SIZE_8);
546 if (ret)
547 goto error;
Bin Meng4d8615c2015-07-19 00:20:04 +0800548 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
Simon Glassff3e0772015-03-05 12:25:25 -0700549 PCI_SIZE_16);
550 if (vendor == 0xffff || vendor == 0x0000)
551 continue;
552
Bin Meng4d8615c2015-07-19 00:20:04 +0800553 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-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 Meng4d8615c2015-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 Glassff3e0772015-03-05 12:25:25 -0700559 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800560 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600561 PCI_SIZE_32);
562 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700563
564 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800565 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700566
Simon Glassaba92962015-07-06 16:47:44 -0600567 /* Search for a driver */
568
Simon Glassff3e0772015-03-05 12:25:25 -0700569 /* If nothing in the device tree, bind a generic device */
570 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600571 struct pci_device_id find_id;
572 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700573
Simon Glassaba92962015-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 Meng4d8615c2015-07-19 00:20:04 +0800579 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-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 Meng4d8615c2015-07-19 00:20:04 +0800585 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600586 &dev);
Simon Glassff3e0772015-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 Meng4d8615c2015-07-19 00:20:04 +0800593 pplat->devfn = PCI_MASK_BUS(bdf);
Simon Glassff3e0772015-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 Glassb9da5082015-07-03 18:28:27 -0600625 phys_addr_t addr;
Simon Glassff3e0772015-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 Glassb9da5082015-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 Glassff3e0772015-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 Glassff3e0772015-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 Meng4d8615c2015-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 Glassff3e0772015-03-05 12:25:25 -0700763{
764 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -0700765
766 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
767}
768
Bin Meng4d8615c2015-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 Glassff3e0772015-03-05 12:25:25 -0700772{
773 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -0700774
775 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
776}
777
778UCLASS_DRIVER(pci) = {
779 .id = UCLASS_PCI,
780 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -0600781 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassff3e0772015-03-05 12:25:25 -0700782 .post_bind = pci_uclass_post_bind,
783 .pre_probe = pci_uclass_pre_probe,
784 .post_probe = pci_uclass_post_probe,
785 .child_post_bind = pci_uclass_child_post_bind,
786 .per_device_auto_alloc_size = sizeof(struct pci_controller),
787 .per_child_platdata_auto_alloc_size =
788 sizeof(struct pci_child_platdata),
789};
790
791static const struct dm_pci_ops pci_bridge_ops = {
792 .read_config = pci_bridge_read_config,
793 .write_config = pci_bridge_write_config,
794};
795
796static const struct udevice_id pci_bridge_ids[] = {
797 { .compatible = "pci-bridge" },
798 { }
799};
800
801U_BOOT_DRIVER(pci_bridge_drv) = {
802 .name = "pci_bridge_drv",
803 .id = UCLASS_PCI,
804 .of_match = pci_bridge_ids,
805 .ops = &pci_bridge_ops,
806};
807
808UCLASS_DRIVER(pci_generic) = {
809 .id = UCLASS_PCI_GENERIC,
810 .name = "pci_generic",
811};
812
813static const struct udevice_id pci_generic_ids[] = {
814 { .compatible = "pci-generic" },
815 { }
816};
817
818U_BOOT_DRIVER(pci_generic_drv) = {
819 .name = "pci_generic_drv",
820 .id = UCLASS_PCI_GENERIC,
821 .of_match = pci_generic_ids,
822};