blob: 2f4368fa1bf762fdfba1c6e80ef668ee7c6c13e3 [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>
Bin Meng348b7442015-08-20 06:40:23 -070017#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
18#include <asm/fsp/fsp_support.h>
19#endif
Simon Glassff3e0772015-03-05 12:25:25 -070020
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass983c6ba22015-08-31 18:55:35 -060023static int pci_get_bus(int busnum, struct udevice **busp)
24{
25 int ret;
26
27 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
28
29 /* Since buses may not be numbered yet try a little harder with bus 0 */
30 if (ret == -ENODEV) {
31 ret = uclass_first_device(UCLASS_PCI, busp);
32 if (ret)
33 return ret;
34 else if (!*busp)
35 return -ENODEV;
36 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
37 }
38
39 return ret;
40}
41
Simon Glassff3e0772015-03-05 12:25:25 -070042struct pci_controller *pci_bus_to_hose(int busnum)
43{
44 struct udevice *bus;
45 int ret;
46
Simon Glass983c6ba22015-08-31 18:55:35 -060047 ret = pci_get_bus(busnum, &bus);
Simon Glassff3e0772015-03-05 12:25:25 -070048 if (ret) {
49 debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
50 return NULL;
51 }
Simon Glass983c6ba22015-08-31 18:55:35 -060052
Simon Glassff3e0772015-03-05 12:25:25 -070053 return dev_get_uclass_priv(bus);
54}
55
Simon Glass4b515e42015-07-06 16:47:46 -060056pci_dev_t pci_get_bdf(struct udevice *dev)
57{
58 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
59 struct udevice *bus = dev->parent;
60
61 return PCI_ADD_BUS(bus->seq, pplat->devfn);
62}
63
Simon Glassff3e0772015-03-05 12:25:25 -070064/**
65 * pci_get_bus_max() - returns the bus number of the last active bus
66 *
67 * @return last bus number, or -1 if no active buses
68 */
69static int pci_get_bus_max(void)
70{
71 struct udevice *bus;
72 struct uclass *uc;
73 int ret = -1;
74
75 ret = uclass_get(UCLASS_PCI, &uc);
76 uclass_foreach_dev(bus, uc) {
77 if (bus->seq > ret)
78 ret = bus->seq;
79 }
80
81 debug("%s: ret=%d\n", __func__, ret);
82
83 return ret;
84}
85
86int pci_last_busno(void)
87{
88 struct pci_controller *hose;
89 struct udevice *bus;
90 struct uclass *uc;
91 int ret;
92
93 debug("pci_last_busno\n");
94 ret = uclass_get(UCLASS_PCI, &uc);
95 if (ret || list_empty(&uc->dev_head))
96 return -1;
97
98 /* Probe the last bus */
99 bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
100 debug("bus = %p, %s\n", bus, bus->name);
101 assert(bus);
102 ret = device_probe(bus);
103 if (ret)
104 return ret;
105
106 /* If that bus has bridges, we may have new buses now. Get the last */
107 bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
108 hose = dev_get_uclass_priv(bus);
109 debug("bus = %s, hose = %p\n", bus->name, hose);
110
111 return hose->last_busno;
112}
113
114int pci_get_ff(enum pci_size_t size)
115{
116 switch (size) {
117 case PCI_SIZE_8:
118 return 0xff;
119 case PCI_SIZE_16:
120 return 0xffff;
121 default:
122 return 0xffffffff;
123 }
124}
125
126int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
127 struct udevice **devp)
128{
129 struct udevice *dev;
130
131 for (device_find_first_child(bus, &dev);
132 dev;
133 device_find_next_child(&dev)) {
134 struct pci_child_platdata *pplat;
135
136 pplat = dev_get_parent_platdata(dev);
137 if (pplat && pplat->devfn == find_devfn) {
138 *devp = dev;
139 return 0;
140 }
141 }
142
143 return -ENODEV;
144}
145
146int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
147{
148 struct udevice *bus;
149 int ret;
150
Simon Glass983c6ba22015-08-31 18:55:35 -0600151 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700152 if (ret)
153 return ret;
154 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
155}
156
157static int pci_device_matches_ids(struct udevice *dev,
158 struct pci_device_id *ids)
159{
160 struct pci_child_platdata *pplat;
161 int i;
162
163 pplat = dev_get_parent_platdata(dev);
164 if (!pplat)
165 return -EINVAL;
166 for (i = 0; ids[i].vendor != 0; i++) {
167 if (pplat->vendor == ids[i].vendor &&
168 pplat->device == ids[i].device)
169 return i;
170 }
171
172 return -EINVAL;
173}
174
175int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
176 int *indexp, struct udevice **devp)
177{
178 struct udevice *dev;
179
180 /* Scan all devices on this bus */
181 for (device_find_first_child(bus, &dev);
182 dev;
183 device_find_next_child(&dev)) {
184 if (pci_device_matches_ids(dev, ids) >= 0) {
185 if ((*indexp)-- <= 0) {
186 *devp = dev;
187 return 0;
188 }
189 }
190 }
191
192 return -ENODEV;
193}
194
195int pci_find_device_id(struct pci_device_id *ids, int index,
196 struct udevice **devp)
197{
198 struct udevice *bus;
199
200 /* Scan all known buses */
201 for (uclass_first_device(UCLASS_PCI, &bus);
202 bus;
203 uclass_next_device(&bus)) {
204 if (!pci_bus_find_devices(bus, ids, &index, devp))
205 return 0;
206 }
207 *devp = NULL;
208
209 return -ENODEV;
210}
211
212int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
213 unsigned long value, enum pci_size_t size)
214{
215 struct dm_pci_ops *ops;
216
217 ops = pci_get_ops(bus);
218 if (!ops->write_config)
219 return -ENOSYS;
220 return ops->write_config(bus, bdf, offset, value, size);
221}
222
223int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
224 enum pci_size_t size)
225{
226 struct udevice *bus;
227 int ret;
228
Simon Glass983c6ba22015-08-31 18:55:35 -0600229 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700230 if (ret)
231 return ret;
232
Bin Meng4d8615c2015-07-19 00:20:04 +0800233 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700234}
235
Simon Glass66afb4e2015-08-10 07:05:03 -0600236int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
237 enum pci_size_t size)
238{
239 struct udevice *bus;
240
Bin Meng1e0f2262015-09-11 03:24:34 -0700241 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600242 bus = bus->parent;
243 return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
244}
245
246
Simon Glassff3e0772015-03-05 12:25:25 -0700247int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
248{
249 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
250}
251
252int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
253{
254 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
255}
256
257int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
258{
259 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
260}
261
Simon Glass66afb4e2015-08-10 07:05:03 -0600262int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
263{
264 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
265}
266
267int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
268{
269 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
270}
271
272int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
273{
274 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
275}
276
Simon Glassff3e0772015-03-05 12:25:25 -0700277int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
278 unsigned long *valuep, enum pci_size_t size)
279{
280 struct dm_pci_ops *ops;
281
282 ops = pci_get_ops(bus);
283 if (!ops->read_config)
284 return -ENOSYS;
285 return ops->read_config(bus, bdf, offset, valuep, size);
286}
287
288int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
289 enum pci_size_t size)
290{
291 struct udevice *bus;
292 int ret;
293
Simon Glass983c6ba22015-08-31 18:55:35 -0600294 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700295 if (ret)
296 return ret;
297
Bin Meng4d8615c2015-07-19 00:20:04 +0800298 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700299}
300
Simon Glass66afb4e2015-08-10 07:05:03 -0600301int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
302 enum pci_size_t size)
303{
304 struct udevice *bus;
305
Bin Meng1e0f2262015-09-11 03:24:34 -0700306 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600307 bus = bus->parent;
308 return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
309 size);
310}
311
Simon Glassff3e0772015-03-05 12:25:25 -0700312int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
313{
314 unsigned long value;
315 int ret;
316
317 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
318 if (ret)
319 return ret;
320 *valuep = value;
321
322 return 0;
323}
324
325int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
326{
327 unsigned long value;
328 int ret;
329
330 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
331 if (ret)
332 return ret;
333 *valuep = value;
334
335 return 0;
336}
337
338int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
339{
340 unsigned long value;
341 int ret;
342
343 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
344 if (ret)
345 return ret;
346 *valuep = value;
347
348 return 0;
349}
350
Simon Glass66afb4e2015-08-10 07:05:03 -0600351int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
352{
353 unsigned long value;
354 int ret;
355
356 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
357 if (ret)
358 return ret;
359 *valuep = value;
360
361 return 0;
362}
363
364int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
365{
366 unsigned long value;
367 int ret;
368
369 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
370 if (ret)
371 return ret;
372 *valuep = value;
373
374 return 0;
375}
376
377int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
378{
379 unsigned long value;
380 int ret;
381
382 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
383 if (ret)
384 return ret;
385 *valuep = value;
386
387 return 0;
388}
389
Simon Glassff3e0772015-03-05 12:25:25 -0700390int pci_auto_config_devices(struct udevice *bus)
391{
392 struct pci_controller *hose = bus->uclass_priv;
393 unsigned int sub_bus;
394 struct udevice *dev;
395 int ret;
396
397 sub_bus = bus->seq;
398 debug("%s: start\n", __func__);
399 pciauto_config_init(hose);
400 for (ret = device_find_first_child(bus, &dev);
401 !ret && dev;
402 ret = device_find_next_child(&dev)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700403 unsigned int max_bus;
Simon Glass4d214552015-09-08 17:52:47 -0600404 int ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700405
Simon Glassff3e0772015-03-05 12:25:25 -0700406 debug("%s: device %s\n", __func__, dev->name);
Simon Glass4d214552015-09-08 17:52:47 -0600407 ret = pciauto_config_device(hose, pci_get_bdf(dev));
408 if (ret < 0)
409 return ret;
410 max_bus = ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700411 sub_bus = max(sub_bus, max_bus);
412 }
413 debug("%s: done\n", __func__);
414
415 return sub_bus;
416}
417
418int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
419{
420 struct udevice *parent, *bus;
421 int sub_bus;
422 int ret;
423
424 debug("%s\n", __func__);
425 parent = hose->bus;
426
427 /* Find the bus within the parent */
Bin Meng8326f132015-07-19 00:20:05 +0800428 ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700429 if (ret) {
430 debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
431 bdf, parent->name, ret);
432 return ret;
433 }
434
435 sub_bus = pci_get_bus_max() + 1;
436 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5afeb4b2015-06-07 08:50:41 -0600437 pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700438
439 ret = device_probe(bus);
440 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600441 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700442 ret);
443 return ret;
444 }
445 if (sub_bus != bus->seq) {
446 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
447 __func__, bus->name, bus->seq, sub_bus);
448 return -EPIPE;
449 }
450 sub_bus = pci_get_bus_max();
451 pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
452
453 return sub_bus;
454}
455
Simon Glassaba92962015-07-06 16:47:44 -0600456/**
457 * pci_match_one_device - Tell if a PCI device structure has a matching
458 * PCI device id structure
459 * @id: single PCI device id structure to match
460 * @dev: the PCI device structure to match against
461 *
462 * Returns the matching pci_device_id structure or %NULL if there is no match.
463 */
464static bool pci_match_one_id(const struct pci_device_id *id,
465 const struct pci_device_id *find)
466{
467 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
468 (id->device == PCI_ANY_ID || id->device == find->device) &&
469 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
470 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
471 !((id->class ^ find->class) & id->class_mask))
472 return true;
473
474 return false;
475}
476
477/**
478 * pci_find_and_bind_driver() - Find and bind the right PCI driver
479 *
480 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600481 *
482 * @parent: Parent bus
483 * @find_id: Specification of the driver to find
484 * @bdf: Bus/device/function addreess - see PCI_BDF()
485 * @devp: Returns a pointer to the device created
486 * @return 0 if OK, -EPERM if the device is not needed before relocation and
487 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600488 */
489static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600490 struct pci_device_id *find_id,
491 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600492{
493 struct pci_driver_entry *start, *entry;
494 const char *drv;
495 int n_ents;
496 int ret;
497 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700498 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600499
500 *devp = NULL;
501
502 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
503 find_id->vendor, find_id->device);
504 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
505 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
506 for (entry = start; entry != start + n_ents; entry++) {
507 const struct pci_device_id *id;
508 struct udevice *dev;
509 const struct driver *drv;
510
511 for (id = entry->match;
512 id->vendor || id->subvendor || id->class_mask;
513 id++) {
514 if (!pci_match_one_id(id, find_id))
515 continue;
516
517 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700518
519 /*
520 * In the pre-relocation phase, we only bind devices
521 * whose driver has the DM_FLAG_PRE_RELOC set, to save
522 * precious memory space as on some platforms as that
523 * space is pretty limited (ie: using Cache As RAM).
524 */
525 if (!(gd->flags & GD_FLG_RELOC) &&
526 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600527 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700528
Simon Glassaba92962015-07-06 16:47:44 -0600529 /*
530 * We could pass the descriptor to the driver as
531 * platdata (instead of NULL) and allow its bind()
532 * method to return -ENOENT if it doesn't support this
533 * device. That way we could continue the search to
534 * find another driver. For now this doesn't seem
535 * necesssary, so just bind the first match.
536 */
537 ret = device_bind(parent, drv, drv->name, NULL, -1,
538 &dev);
539 if (ret)
540 goto error;
541 debug("%s: Match found: %s\n", __func__, drv->name);
542 dev->driver_data = find_id->driver_data;
543 *devp = dev;
544 return 0;
545 }
546 }
547
Bin Meng08fc7b82015-08-20 06:40:17 -0700548 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
549 /*
550 * In the pre-relocation phase, we only bind bridge devices to save
551 * precious memory space as on some platforms as that space is pretty
552 * limited (ie: using Cache As RAM).
553 */
554 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600555 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700556
Simon Glassaba92962015-07-06 16:47:44 -0600557 /* Bind a generic driver so that the device can be used */
Bin Meng4d8615c2015-07-19 00:20:04 +0800558 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
559 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600560 str = strdup(name);
561 if (!str)
562 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700563 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
564
Simon Glassaba92962015-07-06 16:47:44 -0600565 ret = device_bind_driver(parent, drv, str, devp);
566 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600567 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
Simon Glassaba92962015-07-06 16:47:44 -0600568 return ret;
569 }
570 debug("%s: No match found: bound generic driver instead\n", __func__);
571
572 return 0;
573
574error:
575 debug("%s: No match found: error %d\n", __func__, ret);
576 return ret;
577}
578
Simon Glassff3e0772015-03-05 12:25:25 -0700579int pci_bind_bus_devices(struct udevice *bus)
580{
581 ulong vendor, device;
582 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800583 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700584 bool found_multi;
585 int ret;
586
587 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800588 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
589 PCI_MAX_PCI_FUNCTIONS - 1);
590 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
591 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700592 struct pci_child_platdata *pplat;
593 struct udevice *dev;
594 ulong class;
595
Bin Meng4d8615c2015-07-19 00:20:04 +0800596 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700597 continue;
598 /* Check only the first access, we don't expect problems */
Bin Meng4d8615c2015-07-19 00:20:04 +0800599 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
Simon Glassff3e0772015-03-05 12:25:25 -0700600 &header_type, PCI_SIZE_8);
601 if (ret)
602 goto error;
Bin Meng4d8615c2015-07-19 00:20:04 +0800603 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
Simon Glassff3e0772015-03-05 12:25:25 -0700604 PCI_SIZE_16);
605 if (vendor == 0xffff || vendor == 0x0000)
606 continue;
607
Bin Meng4d8615c2015-07-19 00:20:04 +0800608 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700609 found_multi = header_type & 0x80;
610
611 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
Bin Meng4d8615c2015-07-19 00:20:04 +0800612 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
613 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700614 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800615 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600616 PCI_SIZE_32);
617 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700618
619 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800620 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700621
Simon Glassaba92962015-07-06 16:47:44 -0600622 /* Search for a driver */
623
Simon Glassff3e0772015-03-05 12:25:25 -0700624 /* If nothing in the device tree, bind a generic device */
625 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600626 struct pci_device_id find_id;
627 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700628
Simon Glassaba92962015-07-06 16:47:44 -0600629 memset(&find_id, '\0', sizeof(find_id));
630 find_id.vendor = vendor;
631 find_id.device = device;
632 find_id.class = class;
633 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800634 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600635 PCI_SUBSYSTEM_VENDOR_ID,
636 &val, PCI_SIZE_32);
637 find_id.subvendor = val & 0xffff;
638 find_id.subdevice = val >> 16;
639 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800640 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600641 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700642 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600643 if (ret == -EPERM)
644 continue;
645 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700646 return ret;
647
648 /* Update the platform data */
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600649 pplat = dev_get_parent_platdata(dev);
650 pplat->devfn = PCI_MASK_BUS(bdf);
651 pplat->vendor = vendor;
652 pplat->device = device;
653 pplat->class = class;
Simon Glassff3e0772015-03-05 12:25:25 -0700654 }
655
656 return 0;
657error:
658 printf("Cannot read bus configuration: %d\n", ret);
659
660 return ret;
661}
662
663static int pci_uclass_post_bind(struct udevice *bus)
664{
665 /*
Bin Meng1887ed32015-08-24 01:14:01 -0700666 * If there is no pci device listed in the device tree,
667 * don't bother scanning the device tree.
668 */
669 if (bus->of_offset == -1)
670 return 0;
671
672 /*
Simon Glassff3e0772015-03-05 12:25:25 -0700673 * Scan the device tree for devices. This does not probe the PCI bus,
674 * as this is not permitted while binding. It just finds devices
675 * mentioned in the device tree.
676 *
677 * Before relocation, only bind devices marked for pre-relocation
678 * use.
679 */
680 return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
681 gd->flags & GD_FLG_RELOC ? false : true);
682}
683
684static int decode_regions(struct pci_controller *hose, const void *blob,
685 int parent_node, int node)
686{
687 int pci_addr_cells, addr_cells, size_cells;
688 int cells_per_record;
Simon Glassb9da5082015-07-03 18:28:27 -0600689 phys_addr_t addr;
Simon Glassff3e0772015-03-05 12:25:25 -0700690 const u32 *prop;
691 int len;
692 int i;
693
694 prop = fdt_getprop(blob, node, "ranges", &len);
695 if (!prop)
696 return -EINVAL;
697 pci_addr_cells = fdt_address_cells(blob, node);
698 addr_cells = fdt_address_cells(blob, parent_node);
699 size_cells = fdt_size_cells(blob, node);
700
701 /* PCI addresses are always 3-cells */
702 len /= sizeof(u32);
703 cells_per_record = pci_addr_cells + addr_cells + size_cells;
704 hose->region_count = 0;
705 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
706 cells_per_record);
707 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
708 u64 pci_addr, addr, size;
709 int space_code;
710 u32 flags;
711 int type;
712
713 if (len < cells_per_record)
714 break;
715 flags = fdt32_to_cpu(prop[0]);
716 space_code = (flags >> 24) & 3;
717 pci_addr = fdtdec_get_number(prop + 1, 2);
718 prop += pci_addr_cells;
719 addr = fdtdec_get_number(prop, addr_cells);
720 prop += addr_cells;
721 size = fdtdec_get_number(prop, size_cells);
722 prop += size_cells;
723 debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
724 ", size=%" PRIx64 ", space_code=%d\n", __func__,
725 hose->region_count, pci_addr, addr, size, space_code);
726 if (space_code & 2) {
727 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
728 PCI_REGION_MEM;
729 } else if (space_code & 1) {
730 type = PCI_REGION_IO;
731 } else {
732 continue;
733 }
734 debug(" - type=%d\n", type);
735 pci_set_region(hose->regions + hose->region_count++, pci_addr,
736 addr, size, type);
737 }
738
739 /* Add a region for our local memory */
Simon Glassb9da5082015-07-03 18:28:27 -0600740 addr = gd->ram_size;
741 if (gd->pci_ram_top && gd->pci_ram_top < addr)
742 addr = gd->pci_ram_top;
743 pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
744 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Simon Glassff3e0772015-03-05 12:25:25 -0700745
746 return 0;
747}
748
749static int pci_uclass_pre_probe(struct udevice *bus)
750{
751 struct pci_controller *hose;
752 int ret;
753
754 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
755 bus->parent->name);
756 hose = bus->uclass_priv;
757
758 /* For bridges, use the top-level PCI controller */
759 if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
760 hose->ctlr = bus;
761 ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
762 bus->of_offset);
763 if (ret) {
764 debug("%s: Cannot decode regions\n", __func__);
765 return ret;
766 }
767 } else {
768 struct pci_controller *parent_hose;
769
770 parent_hose = dev_get_uclass_priv(bus->parent);
771 hose->ctlr = parent_hose->bus;
772 }
773 hose->bus = bus;
774 hose->first_busno = bus->seq;
775 hose->last_busno = bus->seq;
776
777 return 0;
778}
779
780static int pci_uclass_post_probe(struct udevice *bus)
781{
782 int ret;
783
Simon Glassff3e0772015-03-05 12:25:25 -0700784 debug("%s: probing bus %d\n", __func__, bus->seq);
785 ret = pci_bind_bus_devices(bus);
786 if (ret)
787 return ret;
788
789#ifdef CONFIG_PCI_PNP
790 ret = pci_auto_config_devices(bus);
Simon Glass4d214552015-09-08 17:52:47 -0600791 if (ret < 0)
792 return ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700793#endif
794
Bin Meng348b7442015-08-20 06:40:23 -0700795#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
796 /*
797 * Per Intel FSP specification, we should call FSP notify API to
798 * inform FSP that PCI enumeration has been done so that FSP will
799 * do any necessary initialization as required by the chipset's
800 * BIOS Writer's Guide (BWG).
801 *
802 * Unfortunately we have to put this call here as with driver model,
803 * the enumeration is all done on a lazy basis as needed, so until
804 * something is touched on PCI it won't happen.
805 *
806 * Note we only call this 1) after U-Boot is relocated, and 2)
807 * root bus has finished probing.
808 */
Simon Glass4d214552015-09-08 17:52:47 -0600809 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Meng348b7442015-08-20 06:40:23 -0700810 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -0600811 if (ret)
812 return ret;
813 }
Bin Meng348b7442015-08-20 06:40:23 -0700814#endif
815
Simon Glass4d214552015-09-08 17:52:47 -0600816 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -0700817}
818
819static int pci_uclass_child_post_bind(struct udevice *dev)
820{
821 struct pci_child_platdata *pplat;
822 struct fdt_pci_addr addr;
823 int ret;
824
825 if (dev->of_offset == -1)
826 return 0;
827
828 /*
829 * We could read vendor, device, class if available. But for now we
830 * just check the address.
831 */
832 pplat = dev_get_parent_platdata(dev);
833 ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
834 FDT_PCI_SPACE_CONFIG, "reg", &addr);
835
836 if (ret) {
837 if (ret != -ENOENT)
838 return -EINVAL;
839 } else {
Bin Mengdce54dd2015-08-20 06:40:26 -0700840 /* extract the devfn from fdt_pci_addr */
841 pplat->devfn = addr.phys_hi & 0xff00;
Simon Glassff3e0772015-03-05 12:25:25 -0700842 }
843
844 return 0;
845}
846
Bin Meng4d8615c2015-07-19 00:20:04 +0800847static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
848 uint offset, ulong *valuep,
849 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -0700850{
851 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -0700852
853 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
854}
855
Bin Meng4d8615c2015-07-19 00:20:04 +0800856static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
857 uint offset, ulong value,
858 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -0700859{
860 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -0700861
862 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
863}
864
Simon Glass76c3fbc2015-08-10 07:05:04 -0600865static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
866{
867 struct udevice *dev;
868 int ret = 0;
869
870 /*
871 * Scan through all the PCI controllers. On x86 there will only be one
872 * but that is not necessarily true on other hardware.
873 */
874 do {
875 device_find_first_child(bus, &dev);
876 if (dev) {
877 *devp = dev;
878 return 0;
879 }
880 ret = uclass_next_device(&bus);
881 if (ret)
882 return ret;
883 } while (bus);
884
885 return 0;
886}
887
888int pci_find_next_device(struct udevice **devp)
889{
890 struct udevice *child = *devp;
891 struct udevice *bus = child->parent;
892 int ret;
893
894 /* First try all the siblings */
895 *devp = NULL;
896 while (child) {
897 device_find_next_child(&child);
898 if (child) {
899 *devp = child;
900 return 0;
901 }
902 }
903
904 /* We ran out of siblings. Try the next bus */
905 ret = uclass_next_device(&bus);
906 if (ret)
907 return ret;
908
909 return bus ? skip_to_next_device(bus, devp) : 0;
910}
911
912int pci_find_first_device(struct udevice **devp)
913{
914 struct udevice *bus;
915 int ret;
916
917 *devp = NULL;
918 ret = uclass_first_device(UCLASS_PCI, &bus);
919 if (ret)
920 return ret;
921
922 return skip_to_next_device(bus, devp);
923}
924
Simon Glassff3e0772015-03-05 12:25:25 -0700925UCLASS_DRIVER(pci) = {
926 .id = UCLASS_PCI,
927 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -0600928 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassff3e0772015-03-05 12:25:25 -0700929 .post_bind = pci_uclass_post_bind,
930 .pre_probe = pci_uclass_pre_probe,
931 .post_probe = pci_uclass_post_probe,
932 .child_post_bind = pci_uclass_child_post_bind,
933 .per_device_auto_alloc_size = sizeof(struct pci_controller),
934 .per_child_platdata_auto_alloc_size =
935 sizeof(struct pci_child_platdata),
936};
937
938static const struct dm_pci_ops pci_bridge_ops = {
939 .read_config = pci_bridge_read_config,
940 .write_config = pci_bridge_write_config,
941};
942
943static const struct udevice_id pci_bridge_ids[] = {
944 { .compatible = "pci-bridge" },
945 { }
946};
947
948U_BOOT_DRIVER(pci_bridge_drv) = {
949 .name = "pci_bridge_drv",
950 .id = UCLASS_PCI,
951 .of_match = pci_bridge_ids,
952 .ops = &pci_bridge_ops,
953};
954
955UCLASS_DRIVER(pci_generic) = {
956 .id = UCLASS_PCI_GENERIC,
957 .name = "pci_generic",
958};
959
960static const struct udevice_id pci_generic_ids[] = {
961 { .compatible = "pci-generic" },
962 { }
963};
964
965U_BOOT_DRIVER(pci_generic_drv) = {
966 .name = "pci_generic_drv",
967 .id = UCLASS_PCI_GENERIC,
968 .of_match = pci_generic_ids,
969};