blob: 40f59c0c4c439a3a97a982590b64fcfc9c57e52d [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>
Simon Glassc5f053b2015-11-29 13:18:03 -070014#include <asm/io.h>
Simon Glassb94dc892015-03-05 12:25:25 -070015#include <dm/lists.h>
Simon Glassb94dc892015-03-05 12:25:25 -070016#include <dm/device-internal.h>
Bin Mengc0820a42015-08-20 06:40:23 -070017#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
18#include <asm/fsp/fsp_support.h>
19#endif
Simon Glass37a3f94b2015-11-29 13:17:49 -070020#include "pci_internal.h"
Simon Glassb94dc892015-03-05 12:25:25 -070021
22DECLARE_GLOBAL_DATA_PTR;
23
Simon Glass2e4e4432016-01-18 20:19:14 -070024int pci_get_bus(int busnum, struct udevice **busp)
Simon Glass7d07e592015-08-31 18:55:35 -060025{
26 int ret;
27
28 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
29
30 /* Since buses may not be numbered yet try a little harder with bus 0 */
31 if (ret == -ENODEV) {
Simon Glassc7298e72016-02-11 13:23:26 -070032 ret = uclass_first_device_err(UCLASS_PCI, busp);
Simon Glass7d07e592015-08-31 18:55:35 -060033 if (ret)
34 return ret;
Simon Glass7d07e592015-08-31 18:55:35 -060035 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
36 }
37
38 return ret;
39}
40
Simon Glass6256d672015-11-19 20:27:00 -070041struct udevice *pci_get_controller(struct udevice *dev)
42{
43 while (device_is_on_pci_bus(dev))
44 dev = dev->parent;
45
46 return dev;
47}
48
Simon Glasseaa14892015-11-29 13:17:47 -070049pci_dev_t dm_pci_get_bdf(struct udevice *dev)
Simon Glassc9118d42015-07-06 16:47:46 -060050{
51 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
52 struct udevice *bus = dev->parent;
53
54 return PCI_ADD_BUS(bus->seq, pplat->devfn);
55}
56
Simon Glassb94dc892015-03-05 12:25:25 -070057/**
58 * pci_get_bus_max() - returns the bus number of the last active bus
59 *
60 * @return last bus number, or -1 if no active buses
61 */
62static int pci_get_bus_max(void)
63{
64 struct udevice *bus;
65 struct uclass *uc;
66 int ret = -1;
67
68 ret = uclass_get(UCLASS_PCI, &uc);
69 uclass_foreach_dev(bus, uc) {
70 if (bus->seq > ret)
71 ret = bus->seq;
72 }
73
74 debug("%s: ret=%d\n", __func__, ret);
75
76 return ret;
77}
78
79int pci_last_busno(void)
80{
Bin Meng5bc3f8a2015-10-01 00:36:01 -070081 return pci_get_bus_max();
Simon Glassb94dc892015-03-05 12:25:25 -070082}
83
84int pci_get_ff(enum pci_size_t size)
85{
86 switch (size) {
87 case PCI_SIZE_8:
88 return 0xff;
89 case PCI_SIZE_16:
90 return 0xffff;
91 default:
92 return 0xffffffff;
93 }
94}
95
96int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
97 struct udevice **devp)
98{
99 struct udevice *dev;
100
101 for (device_find_first_child(bus, &dev);
102 dev;
103 device_find_next_child(&dev)) {
104 struct pci_child_platdata *pplat;
105
106 pplat = dev_get_parent_platdata(dev);
107 if (pplat && pplat->devfn == find_devfn) {
108 *devp = dev;
109 return 0;
110 }
111 }
112
113 return -ENODEV;
114}
115
Simon Glass84283d52015-11-29 13:17:48 -0700116int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
Simon Glassb94dc892015-03-05 12:25:25 -0700117{
118 struct udevice *bus;
119 int ret;
120
Simon Glass7d07e592015-08-31 18:55:35 -0600121 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700122 if (ret)
123 return ret;
124 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
125}
126
127static int pci_device_matches_ids(struct udevice *dev,
128 struct pci_device_id *ids)
129{
130 struct pci_child_platdata *pplat;
131 int i;
132
133 pplat = dev_get_parent_platdata(dev);
134 if (!pplat)
135 return -EINVAL;
136 for (i = 0; ids[i].vendor != 0; i++) {
137 if (pplat->vendor == ids[i].vendor &&
138 pplat->device == ids[i].device)
139 return i;
140 }
141
142 return -EINVAL;
143}
144
145int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
146 int *indexp, struct udevice **devp)
147{
148 struct udevice *dev;
149
150 /* Scan all devices on this bus */
151 for (device_find_first_child(bus, &dev);
152 dev;
153 device_find_next_child(&dev)) {
154 if (pci_device_matches_ids(dev, ids) >= 0) {
155 if ((*indexp)-- <= 0) {
156 *devp = dev;
157 return 0;
158 }
159 }
160 }
161
162 return -ENODEV;
163}
164
165int pci_find_device_id(struct pci_device_id *ids, int index,
166 struct udevice **devp)
167{
168 struct udevice *bus;
169
170 /* Scan all known buses */
171 for (uclass_first_device(UCLASS_PCI, &bus);
172 bus;
173 uclass_next_device(&bus)) {
174 if (!pci_bus_find_devices(bus, ids, &index, devp))
175 return 0;
176 }
177 *devp = NULL;
178
179 return -ENODEV;
180}
181
Simon Glass70e0c582015-11-29 13:17:50 -0700182static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
183 unsigned int device, int *indexp,
184 struct udevice **devp)
185{
186 struct pci_child_platdata *pplat;
187 struct udevice *dev;
188
189 for (device_find_first_child(bus, &dev);
190 dev;
191 device_find_next_child(&dev)) {
192 pplat = dev_get_parent_platdata(dev);
193 if (pplat->vendor == vendor && pplat->device == device) {
194 if (!(*indexp)--) {
195 *devp = dev;
196 return 0;
197 }
198 }
199 }
200
201 return -ENODEV;
202}
203
204int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
205 struct udevice **devp)
206{
207 struct udevice *bus;
208
209 /* Scan all known buses */
210 for (uclass_first_device(UCLASS_PCI, &bus);
211 bus;
212 uclass_next_device(&bus)) {
213 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
214 return device_probe(*devp);
215 }
216 *devp = NULL;
217
218 return -ENODEV;
219}
220
Simon Glassb639d512015-11-29 13:17:52 -0700221int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
222{
223 struct udevice *dev;
224
225 /* Scan all known buses */
226 for (pci_find_first_device(&dev);
227 dev;
228 pci_find_next_device(&dev)) {
229 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
230
231 if (pplat->class == find_class && !index--) {
232 *devp = dev;
233 return device_probe(*devp);
234 }
235 }
236 *devp = NULL;
237
238 return -ENODEV;
239}
240
Simon Glassb94dc892015-03-05 12:25:25 -0700241int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
242 unsigned long value, enum pci_size_t size)
243{
244 struct dm_pci_ops *ops;
245
246 ops = pci_get_ops(bus);
247 if (!ops->write_config)
248 return -ENOSYS;
249 return ops->write_config(bus, bdf, offset, value, size);
250}
251
Simon Glass9cec2df2016-03-06 19:27:52 -0700252int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
253 u32 clr, u32 set)
254{
255 ulong val;
256 int ret;
257
258 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
259 if (ret)
260 return ret;
261 val &= ~clr;
262 val |= set;
263
264 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
265}
266
Simon Glassb94dc892015-03-05 12:25:25 -0700267int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
268 enum pci_size_t size)
269{
270 struct udevice *bus;
271 int ret;
272
Simon Glass7d07e592015-08-31 18:55:35 -0600273 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700274 if (ret)
275 return ret;
276
Bin Meng0a721522015-07-19 00:20:04 +0800277 return pci_bus_write_config(bus, bdf, offset, value, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700278}
279
Simon Glass94ef2422015-08-10 07:05:03 -0600280int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
281 enum pci_size_t size)
282{
283 struct udevice *bus;
284
Bin Meng05bedb12015-09-11 03:24:34 -0700285 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600286 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700287 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
288 size);
Simon Glass94ef2422015-08-10 07:05:03 -0600289}
290
Simon Glassb94dc892015-03-05 12:25:25 -0700291int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
292{
293 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
294}
295
296int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
297{
298 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
299}
300
301int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
302{
303 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
304}
305
Simon Glass94ef2422015-08-10 07:05:03 -0600306int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
307{
308 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
309}
310
311int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
312{
313 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
314}
315
316int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
317{
318 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
319}
320
Simon Glassb94dc892015-03-05 12:25:25 -0700321int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
322 unsigned long *valuep, enum pci_size_t size)
323{
324 struct dm_pci_ops *ops;
325
326 ops = pci_get_ops(bus);
327 if (!ops->read_config)
328 return -ENOSYS;
329 return ops->read_config(bus, bdf, offset, valuep, size);
330}
331
332int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
333 enum pci_size_t size)
334{
335 struct udevice *bus;
336 int ret;
337
Simon Glass7d07e592015-08-31 18:55:35 -0600338 ret = pci_get_bus(PCI_BUS(bdf), &bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700339 if (ret)
340 return ret;
341
Bin Meng0a721522015-07-19 00:20:04 +0800342 return pci_bus_read_config(bus, bdf, offset, valuep, size);
Simon Glassb94dc892015-03-05 12:25:25 -0700343}
344
Simon Glass94ef2422015-08-10 07:05:03 -0600345int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
346 enum pci_size_t size)
347{
348 struct udevice *bus;
349
Bin Meng05bedb12015-09-11 03:24:34 -0700350 for (bus = dev; device_is_on_pci_bus(bus);)
Simon Glass94ef2422015-08-10 07:05:03 -0600351 bus = bus->parent;
Simon Glasseaa14892015-11-29 13:17:47 -0700352 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
Simon Glass94ef2422015-08-10 07:05:03 -0600353 size);
354}
355
Simon Glassb94dc892015-03-05 12:25:25 -0700356int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
357{
358 unsigned long value;
359 int ret;
360
361 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
362 if (ret)
363 return ret;
364 *valuep = value;
365
366 return 0;
367}
368
369int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
370{
371 unsigned long value;
372 int ret;
373
374 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
375 if (ret)
376 return ret;
377 *valuep = value;
378
379 return 0;
380}
381
382int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
383{
384 unsigned long value;
385 int ret;
386
387 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
388 if (ret)
389 return ret;
390 *valuep = value;
391
392 return 0;
393}
394
Simon Glass94ef2422015-08-10 07:05:03 -0600395int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
396{
397 unsigned long value;
398 int ret;
399
400 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
401 if (ret)
402 return ret;
403 *valuep = value;
404
405 return 0;
406}
407
408int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
409{
410 unsigned long value;
411 int ret;
412
413 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
414 if (ret)
415 return ret;
416 *valuep = value;
417
418 return 0;
419}
420
421int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
422{
423 unsigned long value;
424 int ret;
425
426 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
427 if (ret)
428 return ret;
429 *valuep = value;
430
431 return 0;
432}
433
Simon Glass9cec2df2016-03-06 19:27:52 -0700434int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
435{
436 u8 val;
437 int ret;
438
439 ret = dm_pci_read_config8(dev, offset, &val);
440 if (ret)
441 return ret;
442 val &= ~clr;
443 val |= set;
444
445 return dm_pci_write_config8(dev, offset, val);
446}
447
448int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
449{
450 u16 val;
451 int ret;
452
453 ret = dm_pci_read_config16(dev, offset, &val);
454 if (ret)
455 return ret;
456 val &= ~clr;
457 val |= set;
458
459 return dm_pci_write_config16(dev, offset, val);
460}
461
462int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
463{
464 u32 val;
465 int ret;
466
467 ret = dm_pci_read_config32(dev, offset, &val);
468 if (ret)
469 return ret;
470 val &= ~clr;
471 val |= set;
472
473 return dm_pci_write_config32(dev, offset, val);
474}
475
Bin Menga0705782015-10-01 00:36:02 -0700476static void set_vga_bridge_bits(struct udevice *dev)
477{
478 struct udevice *parent = dev->parent;
479 u16 bc;
480
481 while (parent->seq != 0) {
482 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
483 bc |= PCI_BRIDGE_CTL_VGA;
484 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
485 parent = parent->parent;
486 }
487}
488
Simon Glassb94dc892015-03-05 12:25:25 -0700489int pci_auto_config_devices(struct udevice *bus)
490{
491 struct pci_controller *hose = bus->uclass_priv;
Bin Menga0705782015-10-01 00:36:02 -0700492 struct pci_child_platdata *pplat;
Simon Glassb94dc892015-03-05 12:25:25 -0700493 unsigned int sub_bus;
494 struct udevice *dev;
495 int ret;
496
497 sub_bus = bus->seq;
498 debug("%s: start\n", __func__);
499 pciauto_config_init(hose);
500 for (ret = device_find_first_child(bus, &dev);
501 !ret && dev;
502 ret = device_find_next_child(&dev)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700503 unsigned int max_bus;
Simon Glassb072d522015-09-08 17:52:47 -0600504 int ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700505
Simon Glassb94dc892015-03-05 12:25:25 -0700506 debug("%s: device %s\n", __func__, dev->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700507 ret = dm_pciauto_config_device(dev);
Simon Glassb072d522015-09-08 17:52:47 -0600508 if (ret < 0)
509 return ret;
510 max_bus = ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700511 sub_bus = max(sub_bus, max_bus);
Bin Menga0705782015-10-01 00:36:02 -0700512
513 pplat = dev_get_parent_platdata(dev);
514 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
515 set_vga_bridge_bits(dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700516 }
517 debug("%s: done\n", __func__);
518
519 return sub_bus;
520}
521
Simon Glass37a3f94b2015-11-29 13:17:49 -0700522int dm_pci_hose_probe_bus(struct udevice *bus)
Simon Glassb94dc892015-03-05 12:25:25 -0700523{
Simon Glassb94dc892015-03-05 12:25:25 -0700524 int sub_bus;
525 int ret;
526
527 debug("%s\n", __func__);
Simon Glassb94dc892015-03-05 12:25:25 -0700528
529 sub_bus = pci_get_bus_max() + 1;
530 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
Simon Glass37a3f94b2015-11-29 13:17:49 -0700531 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700532
533 ret = device_probe(bus);
534 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600535 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
Simon Glassb94dc892015-03-05 12:25:25 -0700536 ret);
537 return ret;
538 }
539 if (sub_bus != bus->seq) {
540 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
541 __func__, bus->name, bus->seq, sub_bus);
542 return -EPIPE;
543 }
544 sub_bus = pci_get_bus_max();
Simon Glass37a3f94b2015-11-29 13:17:49 -0700545 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
Simon Glassb94dc892015-03-05 12:25:25 -0700546
547 return sub_bus;
548}
549
Simon Glass318d71c2015-07-06 16:47:44 -0600550/**
551 * pci_match_one_device - Tell if a PCI device structure has a matching
552 * PCI device id structure
553 * @id: single PCI device id structure to match
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800554 * @find: the PCI device id structure to match against
Simon Glass318d71c2015-07-06 16:47:44 -0600555 *
Hou Zhiqiangd19d0612017-03-22 16:07:24 +0800556 * Returns true if the finding pci_device_id structure matched or false if
557 * there is no match.
Simon Glass318d71c2015-07-06 16:47:44 -0600558 */
559static bool pci_match_one_id(const struct pci_device_id *id,
560 const struct pci_device_id *find)
561{
562 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
563 (id->device == PCI_ANY_ID || id->device == find->device) &&
564 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
565 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
566 !((id->class ^ find->class) & id->class_mask))
567 return true;
568
569 return false;
570}
571
572/**
573 * pci_find_and_bind_driver() - Find and bind the right PCI driver
574 *
575 * This only looks at certain fields in the descriptor.
Simon Glassc45abf12015-09-08 17:52:49 -0600576 *
577 * @parent: Parent bus
578 * @find_id: Specification of the driver to find
579 * @bdf: Bus/device/function addreess - see PCI_BDF()
580 * @devp: Returns a pointer to the device created
581 * @return 0 if OK, -EPERM if the device is not needed before relocation and
582 * therefore was not created, other -ve value on error
Simon Glass318d71c2015-07-06 16:47:44 -0600583 */
584static int pci_find_and_bind_driver(struct udevice *parent,
Simon Glassc45abf12015-09-08 17:52:49 -0600585 struct pci_device_id *find_id,
586 pci_dev_t bdf, struct udevice **devp)
Simon Glass318d71c2015-07-06 16:47:44 -0600587{
588 struct pci_driver_entry *start, *entry;
589 const char *drv;
590 int n_ents;
591 int ret;
592 char name[30], *str;
Bin Meng984c0dc2015-08-20 06:40:17 -0700593 bool bridge;
Simon Glass318d71c2015-07-06 16:47:44 -0600594
595 *devp = NULL;
596
597 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
598 find_id->vendor, find_id->device);
599 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
600 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
601 for (entry = start; entry != start + n_ents; entry++) {
602 const struct pci_device_id *id;
603 struct udevice *dev;
604 const struct driver *drv;
605
606 for (id = entry->match;
607 id->vendor || id->subvendor || id->class_mask;
608 id++) {
609 if (!pci_match_one_id(id, find_id))
610 continue;
611
612 drv = entry->driver;
Bin Meng984c0dc2015-08-20 06:40:17 -0700613
614 /*
615 * In the pre-relocation phase, we only bind devices
616 * whose driver has the DM_FLAG_PRE_RELOC set, to save
617 * precious memory space as on some platforms as that
618 * space is pretty limited (ie: using Cache As RAM).
619 */
620 if (!(gd->flags & GD_FLG_RELOC) &&
621 !(drv->flags & DM_FLAG_PRE_RELOC))
Simon Glassc45abf12015-09-08 17:52:49 -0600622 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700623
Simon Glass318d71c2015-07-06 16:47:44 -0600624 /*
625 * We could pass the descriptor to the driver as
626 * platdata (instead of NULL) and allow its bind()
627 * method to return -ENOENT if it doesn't support this
628 * device. That way we could continue the search to
629 * find another driver. For now this doesn't seem
630 * necesssary, so just bind the first match.
631 */
632 ret = device_bind(parent, drv, drv->name, NULL, -1,
633 &dev);
634 if (ret)
635 goto error;
636 debug("%s: Match found: %s\n", __func__, drv->name);
637 dev->driver_data = find_id->driver_data;
638 *devp = dev;
639 return 0;
640 }
641 }
642
Bin Meng984c0dc2015-08-20 06:40:17 -0700643 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
644 /*
645 * In the pre-relocation phase, we only bind bridge devices to save
646 * precious memory space as on some platforms as that space is pretty
647 * limited (ie: using Cache As RAM).
648 */
649 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
Simon Glassc45abf12015-09-08 17:52:49 -0600650 return -EPERM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700651
Simon Glass318d71c2015-07-06 16:47:44 -0600652 /* Bind a generic driver so that the device can be used */
Bin Meng0a721522015-07-19 00:20:04 +0800653 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
654 PCI_FUNC(bdf));
Simon Glass318d71c2015-07-06 16:47:44 -0600655 str = strdup(name);
656 if (!str)
657 return -ENOMEM;
Bin Meng984c0dc2015-08-20 06:40:17 -0700658 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
659
Simon Glass318d71c2015-07-06 16:47:44 -0600660 ret = device_bind_driver(parent, drv, str, devp);
661 if (ret) {
Simon Glass3b02d842015-09-08 17:52:48 -0600662 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
Simon Glass318d71c2015-07-06 16:47:44 -0600663 return ret;
664 }
665 debug("%s: No match found: bound generic driver instead\n", __func__);
666
667 return 0;
668
669error:
670 debug("%s: No match found: error %d\n", __func__, ret);
671 return ret;
672}
673
Simon Glassb94dc892015-03-05 12:25:25 -0700674int pci_bind_bus_devices(struct udevice *bus)
675{
676 ulong vendor, device;
677 ulong header_type;
Bin Meng0a721522015-07-19 00:20:04 +0800678 pci_dev_t bdf, end;
Simon Glassb94dc892015-03-05 12:25:25 -0700679 bool found_multi;
680 int ret;
681
682 found_multi = false;
Bin Meng0a721522015-07-19 00:20:04 +0800683 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
684 PCI_MAX_PCI_FUNCTIONS - 1);
Yoshinori Sato1e3bce22016-04-25 15:41:01 +0900685 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
Bin Meng0a721522015-07-19 00:20:04 +0800686 bdf += PCI_BDF(0, 0, 1)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700687 struct pci_child_platdata *pplat;
688 struct udevice *dev;
689 ulong class;
690
Bin Meng0a721522015-07-19 00:20:04 +0800691 if (PCI_FUNC(bdf) && !found_multi)
Simon Glassb94dc892015-03-05 12:25:25 -0700692 continue;
693 /* Check only the first access, we don't expect problems */
Bin Meng0a721522015-07-19 00:20:04 +0800694 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
Simon Glassb94dc892015-03-05 12:25:25 -0700695 &header_type, PCI_SIZE_8);
696 if (ret)
697 goto error;
Bin Meng0a721522015-07-19 00:20:04 +0800698 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
Simon Glassb94dc892015-03-05 12:25:25 -0700699 PCI_SIZE_16);
700 if (vendor == 0xffff || vendor == 0x0000)
701 continue;
702
Bin Meng0a721522015-07-19 00:20:04 +0800703 if (!PCI_FUNC(bdf))
Simon Glassb94dc892015-03-05 12:25:25 -0700704 found_multi = header_type & 0x80;
705
706 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
Bin Meng0a721522015-07-19 00:20:04 +0800707 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
708 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
Simon Glassb94dc892015-03-05 12:25:25 -0700709 PCI_SIZE_16);
Bin Meng0a721522015-07-19 00:20:04 +0800710 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
Simon Glass318d71c2015-07-06 16:47:44 -0600711 PCI_SIZE_32);
712 class >>= 8;
Simon Glassb94dc892015-03-05 12:25:25 -0700713
714 /* Find this device in the device tree */
Bin Meng0a721522015-07-19 00:20:04 +0800715 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700716
Simon Glass413ebdb2015-11-29 13:18:09 -0700717 /* If nothing in the device tree, bind a device */
Simon Glassb94dc892015-03-05 12:25:25 -0700718 if (ret == -ENODEV) {
Simon Glass318d71c2015-07-06 16:47:44 -0600719 struct pci_device_id find_id;
720 ulong val;
Simon Glassb94dc892015-03-05 12:25:25 -0700721
Simon Glass318d71c2015-07-06 16:47:44 -0600722 memset(&find_id, '\0', sizeof(find_id));
723 find_id.vendor = vendor;
724 find_id.device = device;
725 find_id.class = class;
726 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
Bin Meng0a721522015-07-19 00:20:04 +0800727 pci_bus_read_config(bus, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600728 PCI_SUBSYSTEM_VENDOR_ID,
729 &val, PCI_SIZE_32);
730 find_id.subvendor = val & 0xffff;
731 find_id.subdevice = val >> 16;
732 }
Bin Meng0a721522015-07-19 00:20:04 +0800733 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
Simon Glass318d71c2015-07-06 16:47:44 -0600734 &dev);
Simon Glassb94dc892015-03-05 12:25:25 -0700735 }
Simon Glassc45abf12015-09-08 17:52:49 -0600736 if (ret == -EPERM)
737 continue;
738 else if (ret)
Simon Glassb94dc892015-03-05 12:25:25 -0700739 return ret;
740
741 /* Update the platform data */
Simon Glassc45abf12015-09-08 17:52:49 -0600742 pplat = dev_get_parent_platdata(dev);
743 pplat->devfn = PCI_MASK_BUS(bdf);
744 pplat->vendor = vendor;
745 pplat->device = device;
746 pplat->class = class;
Simon Glassb94dc892015-03-05 12:25:25 -0700747 }
748
749 return 0;
750error:
751 printf("Cannot read bus configuration: %d\n", ret);
752
753 return ret;
754}
755
Simon Glassb94dc892015-03-05 12:25:25 -0700756static int decode_regions(struct pci_controller *hose, const void *blob,
757 int parent_node, int node)
758{
759 int pci_addr_cells, addr_cells, size_cells;
Simon Glass91de6c52015-11-19 20:26:57 -0700760 phys_addr_t base = 0, size;
Simon Glassb94dc892015-03-05 12:25:25 -0700761 int cells_per_record;
762 const u32 *prop;
763 int len;
764 int i;
765
766 prop = fdt_getprop(blob, node, "ranges", &len);
767 if (!prop)
768 return -EINVAL;
769 pci_addr_cells = fdt_address_cells(blob, node);
770 addr_cells = fdt_address_cells(blob, parent_node);
771 size_cells = fdt_size_cells(blob, node);
772
773 /* PCI addresses are always 3-cells */
774 len /= sizeof(u32);
775 cells_per_record = pci_addr_cells + addr_cells + size_cells;
776 hose->region_count = 0;
777 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
778 cells_per_record);
779 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
780 u64 pci_addr, addr, size;
781 int space_code;
782 u32 flags;
783 int type;
Simon Glass7efc9ba2015-11-19 20:26:58 -0700784 int pos;
Simon Glassb94dc892015-03-05 12:25:25 -0700785
786 if (len < cells_per_record)
787 break;
788 flags = fdt32_to_cpu(prop[0]);
789 space_code = (flags >> 24) & 3;
790 pci_addr = fdtdec_get_number(prop + 1, 2);
791 prop += pci_addr_cells;
792 addr = fdtdec_get_number(prop, addr_cells);
793 prop += addr_cells;
794 size = fdtdec_get_number(prop, size_cells);
795 prop += size_cells;
796 debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
797 ", size=%" PRIx64 ", space_code=%d\n", __func__,
798 hose->region_count, pci_addr, addr, size, space_code);
799 if (space_code & 2) {
800 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
801 PCI_REGION_MEM;
802 } else if (space_code & 1) {
803 type = PCI_REGION_IO;
804 } else {
805 continue;
806 }
Simon Glass7efc9ba2015-11-19 20:26:58 -0700807 pos = -1;
808 for (i = 0; i < hose->region_count; i++) {
809 if (hose->regions[i].flags == type)
810 pos = i;
811 }
812 if (pos == -1)
813 pos = hose->region_count++;
814 debug(" - type=%d, pos=%d\n", type, pos);
815 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
Simon Glassb94dc892015-03-05 12:25:25 -0700816 }
817
818 /* Add a region for our local memory */
Simon Glass91de6c52015-11-19 20:26:57 -0700819 size = gd->ram_size;
820#ifdef CONFIG_SYS_SDRAM_BASE
821 base = CONFIG_SYS_SDRAM_BASE;
822#endif
823 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
824 size = gd->pci_ram_top - base;
825 pci_set_region(hose->regions + hose->region_count++, base, base,
826 size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Simon Glassb94dc892015-03-05 12:25:25 -0700827
828 return 0;
829}
830
831static int pci_uclass_pre_probe(struct udevice *bus)
832{
833 struct pci_controller *hose;
834 int ret;
835
836 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
837 bus->parent->name);
838 hose = bus->uclass_priv;
839
840 /* For bridges, use the top-level PCI controller */
Paul Burtone3b106d2016-09-08 07:47:32 +0100841 if (!device_is_on_pci_bus(bus)) {
Simon Glassb94dc892015-03-05 12:25:25 -0700842 hose->ctlr = bus;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700843 ret = decode_regions(hose, gd->fdt_blob,
844 dev_of_offset(bus->parent),
845 dev_of_offset(bus));
Simon Glassb94dc892015-03-05 12:25:25 -0700846 if (ret) {
847 debug("%s: Cannot decode regions\n", __func__);
848 return ret;
849 }
850 } else {
851 struct pci_controller *parent_hose;
852
853 parent_hose = dev_get_uclass_priv(bus->parent);
854 hose->ctlr = parent_hose->bus;
855 }
856 hose->bus = bus;
857 hose->first_busno = bus->seq;
858 hose->last_busno = bus->seq;
859
860 return 0;
861}
862
863static int pci_uclass_post_probe(struct udevice *bus)
864{
865 int ret;
866
Simon Glassb94dc892015-03-05 12:25:25 -0700867 debug("%s: probing bus %d\n", __func__, bus->seq);
868 ret = pci_bind_bus_devices(bus);
869 if (ret)
870 return ret;
871
872#ifdef CONFIG_PCI_PNP
873 ret = pci_auto_config_devices(bus);
Simon Glassb072d522015-09-08 17:52:47 -0600874 if (ret < 0)
875 return ret;
Simon Glassb94dc892015-03-05 12:25:25 -0700876#endif
877
Bin Mengc0820a42015-08-20 06:40:23 -0700878#if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
879 /*
880 * Per Intel FSP specification, we should call FSP notify API to
881 * inform FSP that PCI enumeration has been done so that FSP will
882 * do any necessary initialization as required by the chipset's
883 * BIOS Writer's Guide (BWG).
884 *
885 * Unfortunately we have to put this call here as with driver model,
886 * the enumeration is all done on a lazy basis as needed, so until
887 * something is touched on PCI it won't happen.
888 *
889 * Note we only call this 1) after U-Boot is relocated, and 2)
890 * root bus has finished probing.
891 */
Simon Glassb072d522015-09-08 17:52:47 -0600892 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
Bin Mengc0820a42015-08-20 06:40:23 -0700893 ret = fsp_init_phase_pci();
Simon Glassb072d522015-09-08 17:52:47 -0600894 if (ret)
895 return ret;
896 }
Bin Mengc0820a42015-08-20 06:40:23 -0700897#endif
898
Simon Glassb072d522015-09-08 17:52:47 -0600899 return 0;
Simon Glassb94dc892015-03-05 12:25:25 -0700900}
901
902static int pci_uclass_child_post_bind(struct udevice *dev)
903{
904 struct pci_child_platdata *pplat;
905 struct fdt_pci_addr addr;
906 int ret;
907
Simon Glassdd79d6e2017-01-17 16:52:55 -0700908 if (dev_of_offset(dev) == -1)
Simon Glassb94dc892015-03-05 12:25:25 -0700909 return 0;
910
911 /*
912 * We could read vendor, device, class if available. But for now we
913 * just check the address.
914 */
915 pplat = dev_get_parent_platdata(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700916 ret = fdtdec_get_pci_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb94dc892015-03-05 12:25:25 -0700917 FDT_PCI_SPACE_CONFIG, "reg", &addr);
918
919 if (ret) {
920 if (ret != -ENOENT)
921 return -EINVAL;
922 } else {
Bin Meng44344a62015-08-20 06:40:26 -0700923 /* extract the devfn from fdt_pci_addr */
924 pplat->devfn = addr.phys_hi & 0xff00;
Simon Glassb94dc892015-03-05 12:25:25 -0700925 }
926
927 return 0;
928}
929
Bin Meng0a721522015-07-19 00:20:04 +0800930static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
931 uint offset, ulong *valuep,
932 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700933{
934 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -0700935
936 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
937}
938
Bin Meng0a721522015-07-19 00:20:04 +0800939static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
940 uint offset, ulong value,
941 enum pci_size_t size)
Simon Glassb94dc892015-03-05 12:25:25 -0700942{
943 struct pci_controller *hose = bus->uclass_priv;
Simon Glassb94dc892015-03-05 12:25:25 -0700944
945 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
946}
947
Simon Glass04c8b6a2015-08-10 07:05:04 -0600948static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
949{
950 struct udevice *dev;
951 int ret = 0;
952
953 /*
954 * Scan through all the PCI controllers. On x86 there will only be one
955 * but that is not necessarily true on other hardware.
956 */
957 do {
958 device_find_first_child(bus, &dev);
959 if (dev) {
960 *devp = dev;
961 return 0;
962 }
963 ret = uclass_next_device(&bus);
964 if (ret)
965 return ret;
966 } while (bus);
967
968 return 0;
969}
970
971int pci_find_next_device(struct udevice **devp)
972{
973 struct udevice *child = *devp;
974 struct udevice *bus = child->parent;
975 int ret;
976
977 /* First try all the siblings */
978 *devp = NULL;
979 while (child) {
980 device_find_next_child(&child);
981 if (child) {
982 *devp = child;
983 return 0;
984 }
985 }
986
987 /* We ran out of siblings. Try the next bus */
988 ret = uclass_next_device(&bus);
989 if (ret)
990 return ret;
991
992 return bus ? skip_to_next_device(bus, devp) : 0;
993}
994
995int pci_find_first_device(struct udevice **devp)
996{
997 struct udevice *bus;
998 int ret;
999
1000 *devp = NULL;
1001 ret = uclass_first_device(UCLASS_PCI, &bus);
1002 if (ret)
1003 return ret;
1004
1005 return skip_to_next_device(bus, devp);
1006}
1007
Simon Glass27a733f2015-11-19 20:26:59 -07001008ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1009{
1010 switch (size) {
1011 case PCI_SIZE_8:
1012 return (value >> ((offset & 3) * 8)) & 0xff;
1013 case PCI_SIZE_16:
1014 return (value >> ((offset & 2) * 8)) & 0xffff;
1015 default:
1016 return value;
1017 }
1018}
1019
1020ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1021 enum pci_size_t size)
1022{
1023 uint off_mask;
1024 uint val_mask, shift;
1025 ulong ldata, mask;
1026
1027 switch (size) {
1028 case PCI_SIZE_8:
1029 off_mask = 3;
1030 val_mask = 0xff;
1031 break;
1032 case PCI_SIZE_16:
1033 off_mask = 2;
1034 val_mask = 0xffff;
1035 break;
1036 default:
1037 return value;
1038 }
1039 shift = (offset & off_mask) * 8;
1040 ldata = (value & val_mask) << shift;
1041 mask = val_mask << shift;
1042 value = (old & ~mask) | ldata;
1043
1044 return value;
1045}
1046
Simon Glassdcdc0122015-11-19 20:27:01 -07001047int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1048 struct pci_region **memp, struct pci_region **prefp)
1049{
1050 struct udevice *bus = pci_get_controller(dev);
1051 struct pci_controller *hose = dev_get_uclass_priv(bus);
1052 int i;
1053
1054 *iop = NULL;
1055 *memp = NULL;
1056 *prefp = NULL;
1057 for (i = 0; i < hose->region_count; i++) {
1058 switch (hose->regions[i].flags) {
1059 case PCI_REGION_IO:
1060 if (!*iop || (*iop)->size < hose->regions[i].size)
1061 *iop = hose->regions + i;
1062 break;
1063 case PCI_REGION_MEM:
1064 if (!*memp || (*memp)->size < hose->regions[i].size)
1065 *memp = hose->regions + i;
1066 break;
1067 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1068 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1069 *prefp = hose->regions + i;
1070 break;
1071 }
1072 }
1073
1074 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1075}
1076
Simon Glass3452cb12015-11-29 13:17:53 -07001077u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
1078{
1079 u32 addr;
1080 int bar;
1081
1082 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1083 dm_pci_read_config32(dev, bar, &addr);
1084 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1085 return addr & PCI_BASE_ADDRESS_IO_MASK;
1086 else
1087 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1088}
1089
Simon Glasse2b6b562016-01-18 20:19:15 -07001090void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1091{
1092 int bar;
1093
1094 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1095 dm_pci_write_config32(dev, bar, addr);
1096}
1097
Simon Glassc5f053b2015-11-29 13:18:03 -07001098static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1099 pci_addr_t bus_addr, unsigned long flags,
1100 unsigned long skip_mask, phys_addr_t *pa)
1101{
1102 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1103 struct pci_region *res;
1104 int i;
1105
1106 for (i = 0; i < hose->region_count; i++) {
1107 res = &hose->regions[i];
1108
1109 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1110 continue;
1111
1112 if (res->flags & skip_mask)
1113 continue;
1114
1115 if (bus_addr >= res->bus_start &&
1116 (bus_addr - res->bus_start) < res->size) {
1117 *pa = (bus_addr - res->bus_start + res->phys_start);
1118 return 0;
1119 }
1120 }
1121
1122 return 1;
1123}
1124
1125phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1126 unsigned long flags)
1127{
1128 phys_addr_t phys_addr = 0;
1129 struct udevice *ctlr;
1130 int ret;
1131
1132 /* The root controller has the region information */
1133 ctlr = pci_get_controller(dev);
1134
1135 /*
1136 * if PCI_REGION_MEM is set we do a two pass search with preference
1137 * on matches that don't have PCI_REGION_SYS_MEMORY set
1138 */
1139 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1140 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1141 flags, PCI_REGION_SYS_MEMORY,
1142 &phys_addr);
1143 if (!ret)
1144 return phys_addr;
1145 }
1146
1147 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1148
1149 if (ret)
1150 puts("pci_hose_bus_to_phys: invalid physical address\n");
1151
1152 return phys_addr;
1153}
1154
1155int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1156 unsigned long flags, unsigned long skip_mask,
1157 pci_addr_t *ba)
1158{
1159 struct pci_region *res;
1160 struct udevice *ctlr;
1161 pci_addr_t bus_addr;
1162 int i;
1163 struct pci_controller *hose;
1164
1165 /* The root controller has the region information */
1166 ctlr = pci_get_controller(dev);
1167 hose = dev_get_uclass_priv(ctlr);
1168
1169 for (i = 0; i < hose->region_count; i++) {
1170 res = &hose->regions[i];
1171
1172 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1173 continue;
1174
1175 if (res->flags & skip_mask)
1176 continue;
1177
1178 bus_addr = phys_addr - res->phys_start + res->bus_start;
1179
1180 if (bus_addr >= res->bus_start &&
1181 (bus_addr - res->bus_start) < res->size) {
1182 *ba = bus_addr;
1183 return 0;
1184 }
1185 }
1186
1187 return 1;
1188}
1189
1190pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1191 unsigned long flags)
1192{
1193 pci_addr_t bus_addr = 0;
1194 int ret;
1195
1196 /*
1197 * if PCI_REGION_MEM is set we do a two pass search with preference
1198 * on matches that don't have PCI_REGION_SYS_MEMORY set
1199 */
1200 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1201 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1202 PCI_REGION_SYS_MEMORY, &bus_addr);
1203 if (!ret)
1204 return bus_addr;
1205 }
1206
1207 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1208
1209 if (ret)
1210 puts("pci_hose_phys_to_bus: invalid physical address\n");
1211
1212 return bus_addr;
1213}
1214
1215void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1216{
1217 pci_addr_t pci_bus_addr;
1218 u32 bar_response;
1219
1220 /* read BAR address */
1221 dm_pci_read_config32(dev, bar, &bar_response);
1222 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1223
1224 /*
1225 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1226 * isn't actualy used on any platform because u-boot assumes a static
1227 * linear mapping. In the future, this could read the BAR size
1228 * and pass that as the size if needed.
1229 */
1230 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1231}
1232
Simon Glassb94dc892015-03-05 12:25:25 -07001233UCLASS_DRIVER(pci) = {
1234 .id = UCLASS_PCI,
1235 .name = "pci",
Simon Glassa8149412015-05-10 21:08:06 -06001236 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -06001237 .post_bind = dm_scan_fdt_dev,
Simon Glassb94dc892015-03-05 12:25:25 -07001238 .pre_probe = pci_uclass_pre_probe,
1239 .post_probe = pci_uclass_post_probe,
1240 .child_post_bind = pci_uclass_child_post_bind,
1241 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1242 .per_child_platdata_auto_alloc_size =
1243 sizeof(struct pci_child_platdata),
1244};
1245
1246static const struct dm_pci_ops pci_bridge_ops = {
1247 .read_config = pci_bridge_read_config,
1248 .write_config = pci_bridge_write_config,
1249};
1250
1251static const struct udevice_id pci_bridge_ids[] = {
1252 { .compatible = "pci-bridge" },
1253 { }
1254};
1255
1256U_BOOT_DRIVER(pci_bridge_drv) = {
1257 .name = "pci_bridge_drv",
1258 .id = UCLASS_PCI,
1259 .of_match = pci_bridge_ids,
1260 .ops = &pci_bridge_ops,
1261};
1262
1263UCLASS_DRIVER(pci_generic) = {
1264 .id = UCLASS_PCI_GENERIC,
1265 .name = "pci_generic",
1266};
1267
1268static const struct udevice_id pci_generic_ids[] = {
1269 { .compatible = "pci-generic" },
1270 { }
1271};
1272
1273U_BOOT_DRIVER(pci_generic_drv) = {
1274 .name = "pci_generic_drv",
1275 .id = UCLASS_PCI_GENERIC,
1276 .of_match = pci_generic_ids,
1277};
Stephen Warren04eb2692016-01-26 11:10:11 -07001278
1279void pci_init(void)
1280{
1281 struct udevice *bus;
1282
1283 /*
1284 * Enumerate all known controller devices. Enumeration has the side-
1285 * effect of probing them, so PCIe devices will be enumerated too.
1286 */
1287 for (uclass_first_device(UCLASS_PCI, &bus);
1288 bus;
1289 uclass_next_device(&bus)) {
1290 ;
1291 }
1292}