1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * (C) Copyright 2002, 2003
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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 __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
84 * Check if pci device should be skipped in configuration
86 if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
87 #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
89 * Only skip configuration if "pciconfighost" is not set
91 if (env_get("pciconfighost") == NULL)
101 #if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
102 /* Get a virtual address associated with a BAR region */
103 void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
105 pci_addr_t pci_bus_addr;
108 /* read BAR address */
109 pci_read_config_dword(pdev, bar, &bar_response);
110 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
113 * Pass "0" as the length argument to pci_bus_to_virt. The arg
114 * isn't actualy used on any platform because u-boot assumes a static
115 * linear mapping. In the future, this could read the BAR size
116 * and pass that as the size if needed.
118 return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
121 void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
126 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
127 pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
130 u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
135 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
136 pci_hose_read_config_dword(hose, dev, bar, &addr);
137 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
138 return addr & PCI_BASE_ADDRESS_IO_MASK;
140 return addr & PCI_BASE_ADDRESS_MEM_MASK;
143 int __pci_hose_bus_to_phys(struct pci_controller *hose,
146 unsigned long skip_mask,
149 struct pci_region *res;
152 for (i = 0; i < hose->region_count; i++) {
153 res = &hose->regions[i];
155 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
158 if (res->flags & skip_mask)
161 if (bus_addr >= res->bus_start &&
162 (bus_addr - res->bus_start) < res->size) {
163 *pa = (bus_addr - res->bus_start + res->phys_start);
171 phys_addr_t pci_hose_bus_to_phys(struct pci_controller *hose,
175 phys_addr_t phys_addr = 0;
179 puts("pci_hose_bus_to_phys: invalid hose\n");
184 * if PCI_REGION_MEM is set we do a two pass search with preference
185 * on matches that don't have PCI_REGION_SYS_MEMORY set
187 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
188 ret = __pci_hose_bus_to_phys(hose, bus_addr,
189 flags, PCI_REGION_SYS_MEMORY, &phys_addr);
194 ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
197 puts("pci_hose_bus_to_phys: invalid physical address\n");
202 int __pci_hose_phys_to_bus(struct pci_controller *hose,
203 phys_addr_t phys_addr,
205 unsigned long skip_mask,
208 struct pci_region *res;
212 for (i = 0; i < hose->region_count; i++) {
213 res = &hose->regions[i];
215 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
218 if (res->flags & skip_mask)
221 bus_addr = phys_addr - res->phys_start + res->bus_start;
223 if (bus_addr >= res->bus_start &&
224 (bus_addr - res->bus_start) < res->size) {
234 * pci_hose_phys_to_bus(): Convert physical address to bus address
235 * @hose: PCI hose of the root PCI controller
236 * @phys_addr: physical address to convert
237 * @flags: flags of pci regions
238 * @return bus address if OK, 0 on error
240 pci_addr_t pci_hose_phys_to_bus(struct pci_controller *hose,
241 phys_addr_t phys_addr,
244 pci_addr_t bus_addr = 0;
248 puts("pci_hose_phys_to_bus: invalid hose\n");
253 * if PCI_REGION_MEM is set we do a two pass search with preference
254 * on matches that don't have PCI_REGION_SYS_MEMORY set
256 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
257 ret = __pci_hose_phys_to_bus(hose, phys_addr,
258 flags, PCI_REGION_SYS_MEMORY, &bus_addr);
263 ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
266 puts("pci_hose_phys_to_bus: invalid physical address\n");
271 pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
273 struct pci_device_id ids[2] = { {}, {0, 0} };
275 ids[0].vendor = vendor;
276 ids[0].device = device;
278 return pci_find_devices(ids, index);
281 pci_dev_t pci_hose_find_devices(struct pci_controller *hose, int busnum,
282 struct pci_device_id *ids, int *indexp)
290 for (bdf = PCI_BDF(busnum, 0, 0);
291 bdf < PCI_BDF(busnum + 1, 0, 0);
292 bdf += PCI_BDF(0, 0, 1)) {
293 if (pci_skip_dev(hose, bdf))
296 if (!PCI_FUNC(bdf)) {
297 pci_read_config_byte(bdf, PCI_HEADER_TYPE,
299 found_multi = header_type & 0x80;
305 pci_read_config_word(bdf, PCI_VENDOR_ID, &vendor);
306 pci_read_config_word(bdf, PCI_DEVICE_ID, &device);
308 for (i = 0; ids[i].vendor != 0; i++) {
309 if (vendor == ids[i].vendor &&
310 device == ids[i].device) {
322 pci_dev_t pci_find_class(uint find_class, int index)
329 for (bus = 0; bus <= pci_last_busno(); bus++) {
330 for (devnum = 0; devnum < PCI_MAX_PCI_DEVICES - 1; devnum++) {
331 pci_read_config_dword(PCI_BDF(bus, devnum, 0),
332 PCI_CLASS_REVISION, &class);
333 if (class >> 16 == 0xffff)
336 for (bdf = PCI_BDF(bus, devnum, 0);
337 bdf <= PCI_BDF(bus, devnum,
338 PCI_MAX_PCI_FUNCTIONS - 1);
339 bdf += PCI_BDF(0, 0, 1)) {
340 pci_read_config_dword(bdf, PCI_CLASS_REVISION,
344 if (class != find_class)
347 * Decrement the index. We want to return the
348 * correct device, so index is 0 for the first
349 * matching device, 1 for the second, etc.
355 /* Return index'th controller. */
363 #endif /* !CONFIG_DM_PCI || CONFIG_DM_PCI_COMPAT */