blob: 28280553dadd80f4fa02cb697bd69fb5f9e4d8df [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 Glass9f60fb02015-11-19 20:27:00 -070056struct udevice *pci_get_controller(struct udevice *dev)
57{
58 while (device_is_on_pci_bus(dev))
59 dev = dev->parent;
60
61 return dev;
62}
63
Simon Glass21ccce12015-11-29 13:17:47 -070064pci_dev_t dm_pci_get_bdf(struct udevice *dev)
Simon Glass4b515e42015-07-06 16:47:46 -060065{
66 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
67 struct udevice *bus = dev->parent;
68
69 return PCI_ADD_BUS(bus->seq, pplat->devfn);
70}
71
Simon Glassff3e0772015-03-05 12:25:25 -070072/**
73 * pci_get_bus_max() - returns the bus number of the last active bus
74 *
75 * @return last bus number, or -1 if no active buses
76 */
77static int pci_get_bus_max(void)
78{
79 struct udevice *bus;
80 struct uclass *uc;
81 int ret = -1;
82
83 ret = uclass_get(UCLASS_PCI, &uc);
84 uclass_foreach_dev(bus, uc) {
85 if (bus->seq > ret)
86 ret = bus->seq;
87 }
88
89 debug("%s: ret=%d\n", __func__, ret);
90
91 return ret;
92}
93
94int pci_last_busno(void)
95{
Bin Meng069155c2015-10-01 00:36:01 -070096 return pci_get_bus_max();
Simon Glassff3e0772015-03-05 12:25:25 -070097}
98
99int pci_get_ff(enum pci_size_t size)
100{
101 switch (size) {
102 case PCI_SIZE_8:
103 return 0xff;
104 case PCI_SIZE_16:
105 return 0xffff;
106 default:
107 return 0xffffffff;
108 }
109}
110
111int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
112 struct udevice **devp)
113{
114 struct udevice *dev;
115
116 for (device_find_first_child(bus, &dev);
117 dev;
118 device_find_next_child(&dev)) {
119 struct pci_child_platdata *pplat;
120
121 pplat = dev_get_parent_platdata(dev);
122 if (pplat && pplat->devfn == find_devfn) {
123 *devp = dev;
124 return 0;
125 }
126 }
127
128 return -ENODEV;
129}
130
Simon Glassf3f1fae2015-11-29 13:17:48 -0700131int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassff3e0772015-03-05 12:25:25 -0700132{
133 struct udevice *bus;
134 int ret;
135
Simon Glass983c6ba22015-08-31 18:55:35 -0600136 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700137 if (ret)
138 return ret;
139 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
140}
141
142static int pci_device_matches_ids(struct udevice *dev,
143 struct pci_device_id *ids)
144{
145 struct pci_child_platdata *pplat;
146 int i;
147
148 pplat = dev_get_parent_platdata(dev);
149 if (!pplat)
150 return -EINVAL;
151 for (i = 0; ids[i].vendor != 0; i++) {
152 if (pplat->vendor == ids[i].vendor &&
153 pplat->device == ids[i].device)
154 return i;
155 }
156
157 return -EINVAL;
158}
159
160int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
161 int *indexp, struct udevice **devp)
162{
163 struct udevice *dev;
164
165 /* Scan all devices on this bus */
166 for (device_find_first_child(bus, &dev);
167 dev;
168 device_find_next_child(&dev)) {
169 if (pci_device_matches_ids(dev, ids) >= 0) {
170 if ((*indexp)-- <= 0) {
171 *devp = dev;
172 return 0;
173 }
174 }
175 }
176
177 return -ENODEV;
178}
179
180int pci_find_device_id(struct pci_device_id *ids, int index,
181 struct udevice **devp)
182{
183 struct udevice *bus;
184
185 /* Scan all known buses */
186 for (uclass_first_device(UCLASS_PCI, &bus);
187 bus;
188 uclass_next_device(&bus)) {
189 if (!pci_bus_find_devices(bus, ids, &index, devp))
190 return 0;
191 }
192 *devp = NULL;
193
194 return -ENODEV;
195}
196
197int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
198 unsigned long value, enum pci_size_t size)
199{
200 struct dm_pci_ops *ops;
201
202 ops = pci_get_ops(bus);
203 if (!ops->write_config)
204 return -ENOSYS;
205 return ops->write_config(bus, bdf, offset, value, size);
206}
207
208int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
209 enum pci_size_t size)
210{
211 struct udevice *bus;
212 int ret;
213
Simon Glass983c6ba22015-08-31 18:55:35 -0600214 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700215 if (ret)
216 return ret;
217
Bin Meng4d8615c2015-07-19 00:20:04 +0800218 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700219}
220
Simon Glass66afb4e2015-08-10 07:05:03 -0600221int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
222 enum pci_size_t size)
223{
224 struct udevice *bus;
225
Bin Meng1e0f2262015-09-11 03:24:34 -0700226 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600227 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700228 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
229 size);
Simon Glass66afb4e2015-08-10 07:05:03 -0600230}
231
232
Simon Glassff3e0772015-03-05 12:25:25 -0700233int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
234{
235 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
236}
237
238int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
239{
240 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
241}
242
243int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
244{
245 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
246}
247
Simon Glass66afb4e2015-08-10 07:05:03 -0600248int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
249{
250 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
251}
252
253int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
254{
255 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
256}
257
258int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
259{
260 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
261}
262
Simon Glassff3e0772015-03-05 12:25:25 -0700263int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
264 unsigned long *valuep, enum pci_size_t size)
265{
266 struct dm_pci_ops *ops;
267
268 ops = pci_get_ops(bus);
269 if (!ops->read_config)
270 return -ENOSYS;
271 return ops->read_config(bus, bdf, offset, valuep, size);
272}
273
274int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
275 enum pci_size_t size)
276{
277 struct udevice *bus;
278 int ret;
279
Simon Glass983c6ba22015-08-31 18:55:35 -0600280 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700281 if (ret)
282 return ret;
283
Bin Meng4d8615c2015-07-19 00:20:04 +0800284 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassff3e0772015-03-05 12:25:25 -0700285}
286
Simon Glass66afb4e2015-08-10 07:05:03 -0600287int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
288 enum pci_size_t size)
289{
290 struct udevice *bus;
291
Bin Meng1e0f2262015-09-11 03:24:34 -0700292 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass66afb4e2015-08-10 07:05:03 -0600293 bus = bus->parent;
Simon Glass21ccce12015-11-29 13:17:47 -0700294 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass66afb4e2015-08-10 07:05:03 -0600295 size);
296}
297
Simon Glassff3e0772015-03-05 12:25:25 -0700298int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
299{
300 unsigned long value;
301 int ret;
302
303 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
304 if (ret)
305 return ret;
306 *valuep = value;
307
308 return 0;
309}
310
311int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
312{
313 unsigned long value;
314 int ret;
315
316 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
317 if (ret)
318 return ret;
319 *valuep = value;
320
321 return 0;
322}
323
324int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
325{
326 unsigned long value;
327 int ret;
328
329 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
330 if (ret)
331 return ret;
332 *valuep = value;
333
334 return 0;
335}
336
Simon Glass66afb4e2015-08-10 07:05:03 -0600337int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
338{
339 unsigned long value;
340 int ret;
341
342 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
343 if (ret)
344 return ret;
345 *valuep = value;
346
347 return 0;
348}
349
350int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
351{
352 unsigned long value;
353 int ret;
354
355 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
356 if (ret)
357 return ret;
358 *valuep = value;
359
360 return 0;
361}
362
363int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
364{
365 unsigned long value;
366 int ret;
367
368 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
369 if (ret)
370 return ret;
371 *valuep = value;
372
373 return 0;
374}
375
Bin Mengbbbcb522015-10-01 00:36:02 -0700376static void set_vga_bridge_bits(struct udevice *dev)
377{
378 struct udevice *parent = dev->parent;
379 u16 bc;
380
381 while (parent->seq != 0) {
382 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
383 bc |= PCI_BRIDGE_CTL_VGA;
384 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
385 parent = parent->parent;
386 }
387}
388
Simon Glassff3e0772015-03-05 12:25:25 -0700389int pci_auto_config_devices(struct udevice *bus)
390{
391 struct pci_controller *hose = bus->uclass_priv;
Bin Mengbbbcb522015-10-01 00:36:02 -0700392 struct pci_child_platdata *pplat;
Simon Glassff3e0772015-03-05 12:25:25 -0700393 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 Glass21ccce12015-11-29 13:17:47 -0700407 ret = pciauto_config_device(hose, dm_pci_get_bdf(dev));
Simon Glass4d214552015-09-08 17:52:47 -0600408 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);
Bin Mengbbbcb522015-10-01 00:36:02 -0700412
413 pplat = dev_get_parent_platdata(dev);
414 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
415 set_vga_bridge_bits(dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700416 }
417 debug("%s: done\n", __func__);
418
419 return sub_bus;
420}
421
422int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
423{
424 struct udevice *parent, *bus;
425 int sub_bus;
426 int ret;
427
428 debug("%s\n", __func__);
429 parent = hose->bus;
430
431 /* Find the bus within the parent */
Bin Meng8326f132015-07-19 00:20:05 +0800432 ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700433 if (ret) {
434 debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
435 bdf, parent->name, ret);
436 return ret;
437 }
438
439 sub_bus = pci_get_bus_max() + 1;
440 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass5afeb4b2015-06-07 08:50:41 -0600441 pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
Simon Glassff3e0772015-03-05 12:25:25 -0700442
443 ret = device_probe(bus);
444 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600445 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassff3e0772015-03-05 12:25:25 -0700446 ret);
447 return ret;
448 }
449 if (sub_bus != bus->seq) {
450 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
451 __func__, bus->name, bus->seq, sub_bus);
452 return -EPIPE;
453 }
454 sub_bus = pci_get_bus_max();
455 pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
456
457 return sub_bus;
458}
459
Simon Glassaba92962015-07-06 16:47:44 -0600460/**
461 * pci_match_one_device - Tell if a PCI device structure has a matching
462 * PCI device id structure
463 * @id: single PCI device id structure to match
464 * @dev: the PCI device structure to match against
465 *
466 * Returns the matching pci_device_id structure or %NULL if there is no match.
467 */
468static bool pci_match_one_id(const struct pci_device_id *id,
469 const struct pci_device_id *find)
470{
471 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
472 (id->device == PCI_ANY_ID || id->device == find->device) &&
473 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
474 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
475 !((id->class ^ find->class) & id->class_mask))
476 return true;
477
478 return false;
479}
480
481/**
482 * pci_find_and_bind_driver() - Find and bind the right PCI driver
483 *
484 * This only looks at certain fields in the descriptor.
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600485 *
486 * @parent: Parent bus
487 * @find_id: Specification of the driver to find
488 * @bdf: Bus/device/function addreess - see PCI_BDF()
489 * @devp: Returns a pointer to the device created
490 * @return 0 if OK, -EPERM if the device is not needed before relocation and
491 * therefore was not created, other -ve value on error
Simon Glassaba92962015-07-06 16:47:44 -0600492 */
493static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600494 struct pci_device_id *find_id,
495 pci_dev_t bdf, struct udevice **devp)
Simon Glassaba92962015-07-06 16:47:44 -0600496{
497 struct pci_driver_entry *start, *entry;
498 const char *drv;
499 int n_ents;
500 int ret;
501 char name[30], *str;
Bin Meng08fc7b82015-08-20 06:40:17 -0700502 bool bridge;
Simon Glassaba92962015-07-06 16:47:44 -0600503
504 *devp = NULL;
505
506 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
507 find_id->vendor, find_id->device);
508 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
509 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
510 for (entry = start; entry != start + n_ents; entry++) {
511 const struct pci_device_id *id;
512 struct udevice *dev;
513 const struct driver *drv;
514
515 for (id = entry->match;
516 id->vendor || id->subvendor || id->class_mask;
517 id++) {
518 if (!pci_match_one_id(id, find_id))
519 continue;
520
521 drv = entry->driver;
Bin Meng08fc7b82015-08-20 06:40:17 -0700522
523 /*
524 * In the pre-relocation phase, we only bind devices
525 * whose driver has the DM_FLAG_PRE_RELOC set, to save
526 * precious memory space as on some platforms as that
527 * space is pretty limited (ie: using Cache As RAM).
528 */
529 if (!(gd->flags & GD_FLG_RELOC) &&
530 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600531 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700532
Simon Glassaba92962015-07-06 16:47:44 -0600533 /*
534 * We could pass the descriptor to the driver as
535 * platdata (instead of NULL) and allow its bind()
536 * method to return -ENOENT if it doesn't support this
537 * device. That way we could continue the search to
538 * find another driver. For now this doesn't seem
539 * necesssary, so just bind the first match.
540 */
541 ret = device_bind(parent, drv, drv->name, NULL, -1,
542 &dev);
543 if (ret)
544 goto error;
545 debug("%s: Match found: %s\n", __func__, drv->name);
546 dev->driver_data = find_id->driver_data;
547 *devp = dev;
548 return 0;
549 }
550 }
551
Bin Meng08fc7b82015-08-20 06:40:17 -0700552 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
553 /*
554 * In the pre-relocation phase, we only bind bridge devices to save
555 * precious memory space as on some platforms as that space is pretty
556 * limited (ie: using Cache As RAM).
557 */
558 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600559 return -EPERM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700560
Simon Glassaba92962015-07-06 16:47:44 -0600561 /* Bind a generic driver so that the device can be used */
Bin Meng4d8615c2015-07-19 00:20:04 +0800562 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
563 PCI_FUNC(bdf));
Simon Glassaba92962015-07-06 16:47:44 -0600564 str = strdup(name);
565 if (!str)
566 return -ENOMEM;
Bin Meng08fc7b82015-08-20 06:40:17 -0700567 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
568
Simon Glassaba92962015-07-06 16:47:44 -0600569 ret = device_bind_driver(parent, drv, str, devp);
570 if (ret) {
Simon Glass3129ace2015-09-08 17:52:48 -0600571 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
Simon Glassaba92962015-07-06 16:47:44 -0600572 return ret;
573 }
574 debug("%s: No match found: bound generic driver instead\n", __func__);
575
576 return 0;
577
578error:
579 debug("%s: No match found: error %d\n", __func__, ret);
580 return ret;
581}
582
Simon Glassff3e0772015-03-05 12:25:25 -0700583int pci_bind_bus_devices(struct udevice *bus)
584{
585 ulong vendor, device;
586 ulong header_type;
Bin Meng4d8615c2015-07-19 00:20:04 +0800587 pci_dev_t bdf, end;
Simon Glassff3e0772015-03-05 12:25:25 -0700588 bool found_multi;
589 int ret;
590
591 found_multi = false;
Bin Meng4d8615c2015-07-19 00:20:04 +0800592 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
593 PCI_MAX_PCI_FUNCTIONS - 1);
594 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
595 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassff3e0772015-03-05 12:25:25 -0700596 struct pci_child_platdata *pplat;
597 struct udevice *dev;
598 ulong class;
599
Bin Meng4d8615c2015-07-19 00:20:04 +0800600 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassff3e0772015-03-05 12:25:25 -0700601 continue;
602 /* Check only the first access, we don't expect problems */
Bin Meng4d8615c2015-07-19 00:20:04 +0800603 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
Simon Glassff3e0772015-03-05 12:25:25 -0700604 &header_type, PCI_SIZE_8);
605 if (ret)
606 goto error;
Bin Meng4d8615c2015-07-19 00:20:04 +0800607 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
Simon Glassff3e0772015-03-05 12:25:25 -0700608 PCI_SIZE_16);
609 if (vendor == 0xffff || vendor == 0x0000)
610 continue;
611
Bin Meng4d8615c2015-07-19 00:20:04 +0800612 if (!PCI_FUNC(bdf))
Simon Glassff3e0772015-03-05 12:25:25 -0700613 found_multi = header_type & 0x80;
614
615 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
Bin Meng4d8615c2015-07-19 00:20:04 +0800616 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
617 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassff3e0772015-03-05 12:25:25 -0700618 PCI_SIZE_16);
Bin Meng4d8615c2015-07-19 00:20:04 +0800619 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glassaba92962015-07-06 16:47:44 -0600620 PCI_SIZE_32);
621 class >>= 8;
Simon Glassff3e0772015-03-05 12:25:25 -0700622
623 /* Find this device in the device tree */
Bin Meng4d8615c2015-07-19 00:20:04 +0800624 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700625
Simon Glassaba92962015-07-06 16:47:44 -0600626 /* Search for a driver */
627
Simon Glassff3e0772015-03-05 12:25:25 -0700628 /* If nothing in the device tree, bind a generic device */
629 if (ret == -ENODEV) {
Simon Glassaba92962015-07-06 16:47:44 -0600630 struct pci_device_id find_id;
631 ulong val;
Simon Glassff3e0772015-03-05 12:25:25 -0700632
Simon Glassaba92962015-07-06 16:47:44 -0600633 memset(&find_id, '\0', sizeof(find_id));
634 find_id.vendor = vendor;
635 find_id.device = device;
636 find_id.class = class;
637 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng4d8615c2015-07-19 00:20:04 +0800638 pci_bus_read_config(bus, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600639 PCI_SUBSYSTEM_VENDOR_ID,
640 &val, PCI_SIZE_32);
641 find_id.subvendor = val & 0xffff;
642 find_id.subdevice = val >> 16;
643 }
Bin Meng4d8615c2015-07-19 00:20:04 +0800644 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glassaba92962015-07-06 16:47:44 -0600645 &dev);
Simon Glassff3e0772015-03-05 12:25:25 -0700646 }
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600647 if (ret == -EPERM)
648 continue;
649 else if (ret)
Simon Glassff3e0772015-03-05 12:25:25 -0700650 return ret;
651
652 /* Update the platform data */
Simon Glass5dbcf3a2015-09-08 17:52:49 -0600653 pplat = dev_get_parent_platdata(dev);
654 pplat->devfn = PCI_MASK_BUS(bdf);
655 pplat->vendor = vendor;
656 pplat->device = device;
657 pplat->class = class;
Simon Glassff3e0772015-03-05 12:25:25 -0700658 }
659
660 return 0;
661error:
662 printf("Cannot read bus configuration: %d\n", ret);
663
664 return ret;
665}
666
667static int pci_uclass_post_bind(struct udevice *bus)
668{
669 /*
Bin Meng1887ed32015-08-24 01:14:01 -0700670 * If there is no pci device listed in the device tree,
671 * don't bother scanning the device tree.
672 */
673 if (bus->of_offset == -1)
674 return 0;
675
676 /*
Simon Glassff3e0772015-03-05 12:25:25 -0700677 * Scan the device tree for devices. This does not probe the PCI bus,
678 * as this is not permitted while binding. It just finds devices
679 * mentioned in the device tree.
680 *
681 * Before relocation, only bind devices marked for pre-relocation
682 * use.
683 */
684 return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
685 gd->flags & GD_FLG_RELOC ? false : true);
686}
687
688static int decode_regions(struct pci_controller *hose, const void *blob,
689 int parent_node, int node)
690{
691 int pci_addr_cells, addr_cells, size_cells;
Simon Glass2084c5a2015-11-19 20:26:57 -0700692 phys_addr_t base = 0, size;
Simon Glassff3e0772015-03-05 12:25:25 -0700693 int cells_per_record;
694 const u32 *prop;
695 int len;
696 int i;
697
698 prop = fdt_getprop(blob, node, "ranges", &len);
699 if (!prop)
700 return -EINVAL;
701 pci_addr_cells = fdt_address_cells(blob, node);
702 addr_cells = fdt_address_cells(blob, parent_node);
703 size_cells = fdt_size_cells(blob, node);
704
705 /* PCI addresses are always 3-cells */
706 len /= sizeof(u32);
707 cells_per_record = pci_addr_cells + addr_cells + size_cells;
708 hose->region_count = 0;
709 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
710 cells_per_record);
711 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
712 u64 pci_addr, addr, size;
713 int space_code;
714 u32 flags;
715 int type;
Simon Glass9526d832015-11-19 20:26:58 -0700716 int pos;
Simon Glassff3e0772015-03-05 12:25:25 -0700717
718 if (len < cells_per_record)
719 break;
720 flags = fdt32_to_cpu(prop[0]);
721 space_code = (flags >> 24) & 3;
722 pci_addr = fdtdec_get_number(prop + 1, 2);
723 prop += pci_addr_cells;
724 addr = fdtdec_get_number(prop, addr_cells);
725 prop += addr_cells;
726 size = fdtdec_get_number(prop, size_cells);
727 prop += size_cells;
728 debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
729 ", size=%" PRIx64 ", space_code=%d\n", __func__,
730 hose->region_count, pci_addr, addr, size, space_code);
731 if (space_code & 2) {
732 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
733 PCI_REGION_MEM;
734 } else if (space_code & 1) {
735 type = PCI_REGION_IO;
736 } else {
737 continue;
738 }
Simon Glass9526d832015-11-19 20:26:58 -0700739 pos = -1;
740 for (i = 0; i < hose->region_count; i++) {
741 if (hose->regions[i].flags == type)
742 pos = i;
743 }
744 if (pos == -1)
745 pos = hose->region_count++;
746 debug(" - type=%d, pos=%d\n", type, pos);
747 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassff3e0772015-03-05 12:25:25 -0700748 }
749
750 /* Add a region for our local memory */
Simon Glass2084c5a2015-11-19 20:26:57 -0700751 size = gd->ram_size;
752#ifdef CONFIG_SYS_SDRAM_BASE
753 base = CONFIG_SYS_SDRAM_BASE;
754#endif
755 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
756 size = gd->pci_ram_top - base;
757 pci_set_region(hose->regions + hose->region_count++, base, base,
758 size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Simon Glassff3e0772015-03-05 12:25:25 -0700759
760 return 0;
761}
762
763static int pci_uclass_pre_probe(struct udevice *bus)
764{
765 struct pci_controller *hose;
766 int ret;
767
768 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
769 bus->parent->name);
770 hose = bus->uclass_priv;
771
772 /* For bridges, use the top-level PCI controller */
773 if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
774 hose->ctlr = bus;
775 ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
776 bus->of_offset);
777 if (ret) {
778 debug("%s: Cannot decode regions\n", __func__);
779 return ret;
780 }
781 } else {
782 struct pci_controller *parent_hose;
783
784 parent_hose = dev_get_uclass_priv(bus->parent);
785 hose->ctlr = parent_hose->bus;
786 }
787 hose->bus = bus;
788 hose->first_busno = bus->seq;
789 hose->last_busno = bus->seq;
790
791 return 0;
792}
793
794static int pci_uclass_post_probe(struct udevice *bus)
795{
796 int ret;
797
Simon Glassff3e0772015-03-05 12:25:25 -0700798 debug("%s: probing bus %d\n", __func__, bus->seq);
799 ret = pci_bind_bus_devices(bus);
800 if (ret)
801 return ret;
802
803#ifdef CONFIG_PCI_PNP
804 ret = pci_auto_config_devices(bus);
Simon Glass4d214552015-09-08 17:52:47 -0600805 if (ret < 0)
806 return ret;
Simon Glassff3e0772015-03-05 12:25:25 -0700807#endif
808
Bin Meng348b7442015-08-20 06:40:23 -0700809#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
810 /*
811 * Per Intel FSP specification, we should call FSP notify API to
812 * inform FSP that PCI enumeration has been done so that FSP will
813 * do any necessary initialization as required by the chipset's
814 * BIOS Writer's Guide (BWG).
815 *
816 * Unfortunately we have to put this call here as with driver model,
817 * the enumeration is all done on a lazy basis as needed, so until
818 * something is touched on PCI it won't happen.
819 *
820 * Note we only call this 1) after U-Boot is relocated, and 2)
821 * root bus has finished probing.
822 */
Simon Glass4d214552015-09-08 17:52:47 -0600823 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Meng348b7442015-08-20 06:40:23 -0700824 ret = fsp_init_phase_pci();
Simon Glass4d214552015-09-08 17:52:47 -0600825 if (ret)
826 return ret;
827 }
Bin Meng348b7442015-08-20 06:40:23 -0700828#endif
829
Simon Glass4d214552015-09-08 17:52:47 -0600830 return 0;
Simon Glassff3e0772015-03-05 12:25:25 -0700831}
832
833static int pci_uclass_child_post_bind(struct udevice *dev)
834{
835 struct pci_child_platdata *pplat;
836 struct fdt_pci_addr addr;
837 int ret;
838
839 if (dev->of_offset == -1)
840 return 0;
841
842 /*
843 * We could read vendor, device, class if available. But for now we
844 * just check the address.
845 */
846 pplat = dev_get_parent_platdata(dev);
847 ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
848 FDT_PCI_SPACE_CONFIG, "reg", &addr);
849
850 if (ret) {
851 if (ret != -ENOENT)
852 return -EINVAL;
853 } else {
Bin Mengdce54dd2015-08-20 06:40:26 -0700854 /* extract the devfn from fdt_pci_addr */
855 pplat->devfn = addr.phys_hi & 0xff00;
Simon Glassff3e0772015-03-05 12:25:25 -0700856 }
857
858 return 0;
859}
860
Bin Meng4d8615c2015-07-19 00:20:04 +0800861static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
862 uint offset, ulong *valuep,
863 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -0700864{
865 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -0700866
867 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
868}
869
Bin Meng4d8615c2015-07-19 00:20:04 +0800870static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
871 uint offset, ulong value,
872 enum pci_size_t size)
Simon Glassff3e0772015-03-05 12:25:25 -0700873{
874 struct pci_controller *hose = bus->uclass_priv;
Simon Glassff3e0772015-03-05 12:25:25 -0700875
876 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
877}
878
Simon Glass76c3fbc2015-08-10 07:05:04 -0600879static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
880{
881 struct udevice *dev;
882 int ret = 0;
883
884 /*
885 * Scan through all the PCI controllers. On x86 there will only be one
886 * but that is not necessarily true on other hardware.
887 */
888 do {
889 device_find_first_child(bus, &dev);
890 if (dev) {
891 *devp = dev;
892 return 0;
893 }
894 ret = uclass_next_device(&bus);
895 if (ret)
896 return ret;
897 } while (bus);
898
899 return 0;
900}
901
902int pci_find_next_device(struct udevice **devp)
903{
904 struct udevice *child = *devp;
905 struct udevice *bus = child->parent;
906 int ret;
907
908 /* First try all the siblings */
909 *devp = NULL;
910 while (child) {
911 device_find_next_child(&child);
912 if (child) {
913 *devp = child;
914 return 0;
915 }
916 }
917
918 /* We ran out of siblings. Try the next bus */
919 ret = uclass_next_device(&bus);
920 if (ret)
921 return ret;
922
923 return bus ? skip_to_next_device(bus, devp) : 0;
924}
925
926int pci_find_first_device(struct udevice **devp)
927{
928 struct udevice *bus;
929 int ret;
930
931 *devp = NULL;
932 ret = uclass_first_device(UCLASS_PCI, &bus);
933 if (ret)
934 return ret;
935
936 return skip_to_next_device(bus, devp);
937}
938
Simon Glass9289db62015-11-19 20:26:59 -0700939ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
940{
941 switch (size) {
942 case PCI_SIZE_8:
943 return (value >> ((offset & 3) * 8)) & 0xff;
944 case PCI_SIZE_16:
945 return (value >> ((offset & 2) * 8)) & 0xffff;
946 default:
947 return value;
948 }
949}
950
951ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
952 enum pci_size_t size)
953{
954 uint off_mask;
955 uint val_mask, shift;
956 ulong ldata, mask;
957
958 switch (size) {
959 case PCI_SIZE_8:
960 off_mask = 3;
961 val_mask = 0xff;
962 break;
963 case PCI_SIZE_16:
964 off_mask = 2;
965 val_mask = 0xffff;
966 break;
967 default:
968 return value;
969 }
970 shift = (offset & off_mask) * 8;
971 ldata = (value & val_mask) << shift;
972 mask = val_mask << shift;
973 value = (old & ~mask) | ldata;
974
975 return value;
976}
977
Simon Glassf9260332015-11-19 20:27:01 -0700978int pci_get_regions(struct udevice *dev, struct pci_region **iop,
979 struct pci_region **memp, struct pci_region **prefp)
980{
981 struct udevice *bus = pci_get_controller(dev);
982 struct pci_controller *hose = dev_get_uclass_priv(bus);
983 int i;
984
985 *iop = NULL;
986 *memp = NULL;
987 *prefp = NULL;
988 for (i = 0; i < hose->region_count; i++) {
989 switch (hose->regions[i].flags) {
990 case PCI_REGION_IO:
991 if (!*iop || (*iop)->size < hose->regions[i].size)
992 *iop = hose->regions + i;
993 break;
994 case PCI_REGION_MEM:
995 if (!*memp || (*memp)->size < hose->regions[i].size)
996 *memp = hose->regions + i;
997 break;
998 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
999 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1000 *prefp = hose->regions + i;
1001 break;
1002 }
1003 }
1004
1005 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1006}
1007
Simon Glassff3e0772015-03-05 12:25:25 -07001008UCLASS_DRIVER(pci) = {
1009 .id = UCLASS_PCI,
1010 .name = "pci",
Simon Glass2bb02e42015-05-10 21:08:06 -06001011 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassff3e0772015-03-05 12:25:25 -07001012 .post_bind = pci_uclass_post_bind,
1013 .pre_probe = pci_uclass_pre_probe,
1014 .post_probe = pci_uclass_post_probe,
1015 .child_post_bind = pci_uclass_child_post_bind,
1016 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1017 .per_child_platdata_auto_alloc_size =
1018 sizeof(struct pci_child_platdata),
1019};
1020
1021static const struct dm_pci_ops pci_bridge_ops = {
1022 .read_config = pci_bridge_read_config,
1023 .write_config = pci_bridge_write_config,
1024};
1025
1026static const struct udevice_id pci_bridge_ids[] = {
1027 { .compatible = "pci-bridge" },
1028 { }
1029};
1030
1031U_BOOT_DRIVER(pci_bridge_drv) = {
1032 .name = "pci_bridge_drv",
1033 .id = UCLASS_PCI,
1034 .of_match = pci_bridge_ids,
1035 .ops = &pci_bridge_ops,
1036};
1037
1038UCLASS_DRIVER(pci_generic) = {
1039 .id = UCLASS_PCI_GENERIC,
1040 .name = "pci_generic",
1041};
1042
1043static const struct udevice_id pci_generic_ids[] = {
1044 { .compatible = "pci-generic" },
1045 { }
1046};
1047
1048U_BOOT_DRIVER(pci_generic_drv) = {
1049 .name = "pci_generic_drv",
1050 .id = UCLASS_PCI_GENERIC,
1051 .of_match = pci_generic_ids,
1052};