2 * Copyright (c) 2014 Google, Inc
4 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5 * Andreas Heppel <aheppel@sysgo.de>
7 * (C) Copyright 2002, 2003
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 * SPDX-License-Identifier: GPL-2.0+
18 const char *pci_class_str(u8 class)
21 case PCI_CLASS_NOT_DEFINED:
22 return "Build before PCI Rev2.0";
24 case PCI_BASE_CLASS_STORAGE:
25 return "Mass storage controller";
27 case PCI_BASE_CLASS_NETWORK:
28 return "Network controller";
30 case PCI_BASE_CLASS_DISPLAY:
31 return "Display controller";
33 case PCI_BASE_CLASS_MULTIMEDIA:
34 return "Multimedia device";
36 case PCI_BASE_CLASS_MEMORY:
37 return "Memory controller";
39 case PCI_BASE_CLASS_BRIDGE:
40 return "Bridge device";
42 case PCI_BASE_CLASS_COMMUNICATION:
43 return "Simple comm. controller";
45 case PCI_BASE_CLASS_SYSTEM:
46 return "Base system peripheral";
48 case PCI_BASE_CLASS_INPUT:
49 return "Input device";
51 case PCI_BASE_CLASS_DOCKING:
52 return "Docking station";
54 case PCI_BASE_CLASS_PROCESSOR:
57 case PCI_BASE_CLASS_SERIAL:
58 return "Serial bus controller";
60 case PCI_BASE_CLASS_INTELLIGENT:
61 return "Intelligent controller";
63 case PCI_BASE_CLASS_SATELLITE:
64 return "Satellite controller";
66 case PCI_BASE_CLASS_CRYPT:
67 return "Cryptographic device";
69 case PCI_BASE_CLASS_SIGNAL_PROCESSING:
72 case PCI_CLASS_OTHERS:
73 return "Does not fit any class";
81 pci_dev_t pci_find_class(uint find_class, int index)
88 for (bus = 0; bus <= pci_last_busno(); bus++) {
89 for (devnum = 0; devnum < PCI_MAX_PCI_DEVICES - 1; devnum++) {
90 pci_read_config_dword(PCI_BDF(bus, devnum, 0),
91 PCI_CLASS_REVISION, &class);
92 if (class >> 16 == 0xffff)
95 for (bdf = PCI_BDF(bus, devnum, 0);
96 bdf <= PCI_BDF(bus, devnum,
97 PCI_MAX_PCI_FUNCTIONS - 1);
98 bdf += PCI_BDF(0, 0, 1)) {
99 pci_read_config_dword(bdf, PCI_CLASS_REVISION,
103 if (class != find_class)
106 * Decrement the index. We want to return the
107 * correct device, so index is 0 for the first
108 * matching device, 1 for the second, etc.
114 /* Return index'th controller. */
123 __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
126 * Check if pci device should be skipped in configuration
128 if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
129 #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
131 * Only skip configuration if "pciconfighost" is not set
133 if (getenv("pciconfighost") == NULL)
143 /* Get a virtual address associated with a BAR region */
144 void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
146 pci_addr_t pci_bus_addr;
149 /* read BAR address */
150 pci_read_config_dword(pdev, bar, &bar_response);
151 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
154 * Pass "0" as the length argument to pci_bus_to_virt. The arg
155 * isn't actualy used on any platform because u-boot assumes a static
156 * linear mapping. In the future, this could read the BAR size
157 * and pass that as the size if needed.
159 return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
162 void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
167 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
168 pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
171 u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
176 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
177 pci_hose_read_config_dword(hose, dev, bar, &addr);
178 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
179 return addr & PCI_BASE_ADDRESS_IO_MASK;
181 return addr & PCI_BASE_ADDRESS_MEM_MASK;
184 int __pci_hose_bus_to_phys(struct pci_controller *hose,
187 unsigned long skip_mask,
190 struct pci_region *res;
193 for (i = 0; i < hose->region_count; i++) {
194 res = &hose->regions[i];
196 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
199 if (res->flags & skip_mask)
202 if (bus_addr >= res->bus_start &&
203 (bus_addr - res->bus_start) < res->size) {
204 *pa = (bus_addr - res->bus_start + res->phys_start);
212 phys_addr_t pci_hose_bus_to_phys(struct pci_controller *hose,
216 phys_addr_t phys_addr = 0;
220 puts("pci_hose_bus_to_phys: invalid hose\n");
225 * if PCI_REGION_MEM is set we do a two pass search with preference
226 * on matches that don't have PCI_REGION_SYS_MEMORY set
228 if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
229 ret = __pci_hose_bus_to_phys(hose, bus_addr,
230 flags, PCI_REGION_SYS_MEMORY, &phys_addr);
235 ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
238 puts("pci_hose_bus_to_phys: invalid physical address\n");
243 int __pci_hose_phys_to_bus(struct pci_controller *hose,
244 phys_addr_t phys_addr,
246 unsigned long skip_mask,
249 struct pci_region *res;
253 for (i = 0; i < hose->region_count; i++) {
254 res = &hose->regions[i];
256 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
259 if (res->flags & skip_mask)
262 bus_addr = phys_addr - res->phys_start + res->bus_start;
264 if (bus_addr >= res->bus_start &&
265 bus_addr < res->bus_start + res->size) {
274 pci_addr_t pci_hose_phys_to_bus(struct pci_controller *hose,
275 phys_addr_t phys_addr,
278 pci_addr_t bus_addr = 0;
282 puts("pci_hose_phys_to_bus: invalid hose\n");
287 * if PCI_REGION_MEM is set we do a two pass search with preference
288 * on matches that don't have PCI_REGION_SYS_MEMORY set
290 if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
291 ret = __pci_hose_phys_to_bus(hose, phys_addr,
292 flags, PCI_REGION_SYS_MEMORY, &bus_addr);
297 ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
300 puts("pci_hose_phys_to_bus: invalid physical address\n");
305 pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
307 struct pci_device_id ids[2] = { {}, {0, 0} };
309 ids[0].vendor = vendor;
310 ids[0].device = device;
312 return pci_find_devices(ids, index);
315 pci_dev_t pci_hose_find_devices(struct pci_controller *hose, int busnum,
316 struct pci_device_id *ids, int *indexp)
324 for (bdf = PCI_BDF(busnum, 0, 0);
325 bdf < PCI_BDF(busnum + 1, 0, 0);
326 bdf += PCI_BDF(0, 0, 1)) {
327 if (pci_skip_dev(hose, bdf))
330 if (!PCI_FUNC(bdf)) {
331 pci_read_config_byte(bdf, PCI_HEADER_TYPE,
333 found_multi = header_type & 0x80;
339 pci_read_config_word(bdf, PCI_VENDOR_ID, &vendor);
340 pci_read_config_word(bdf, PCI_DEVICE_ID, &device);
342 for (i = 0; ids[i].vendor != 0; i++) {
343 if (vendor == ids[i].vendor &&
344 device == ids[i].device) {