xref: /rk3399_rockchip-uboot/drivers/pci/pci.c (revision e8a552eb625f0b2d7a778d151af25a17c6d33b7b)
193a686eeSJean-Christophe PLAGNIOL-VILLARD /*
293a686eeSJean-Christophe PLAGNIOL-VILLARD  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
393a686eeSJean-Christophe PLAGNIOL-VILLARD  * Andreas Heppel <aheppel@sysgo.de>
493a686eeSJean-Christophe PLAGNIOL-VILLARD  *
593a686eeSJean-Christophe PLAGNIOL-VILLARD  * (C) Copyright 2002, 2003
693a686eeSJean-Christophe PLAGNIOL-VILLARD  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
793a686eeSJean-Christophe PLAGNIOL-VILLARD  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
993a686eeSJean-Christophe PLAGNIOL-VILLARD  */
1093a686eeSJean-Christophe PLAGNIOL-VILLARD 
1193a686eeSJean-Christophe PLAGNIOL-VILLARD /*
1293a686eeSJean-Christophe PLAGNIOL-VILLARD  * PCI routines
1393a686eeSJean-Christophe PLAGNIOL-VILLARD  */
1493a686eeSJean-Christophe PLAGNIOL-VILLARD 
1593a686eeSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
1693a686eeSJean-Christophe PLAGNIOL-VILLARD 
1793a686eeSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
1893a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/processor.h>
1993a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
2093a686eeSJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
2193a686eeSJean-Christophe PLAGNIOL-VILLARD 
2293a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_HOSE_OP(rw, size, type)					\
2393a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_##rw##_config_##size(struct pci_controller *hose,		\
2493a686eeSJean-Christophe PLAGNIOL-VILLARD 				  pci_dev_t dev,			\
2593a686eeSJean-Christophe PLAGNIOL-VILLARD 				  int offset, type value)		\
2693a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
2793a686eeSJean-Christophe PLAGNIOL-VILLARD 	return hose->rw##_##size(hose, dev, offset, value);		\
2893a686eeSJean-Christophe PLAGNIOL-VILLARD }
2993a686eeSJean-Christophe PLAGNIOL-VILLARD 
3093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, byte, u8 *)
3193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, word, u16 *)
3293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, dword, u32 *)
3393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, byte, u8)
3493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, word, u16)
3593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, dword, u32)
3693a686eeSJean-Christophe PLAGNIOL-VILLARD 
3793a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_OP(rw, size, type, error_code)				\
3893a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value)	\
3993a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
4093a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev));	\
4193a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
4293a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (!hose)							\
4393a686eeSJean-Christophe PLAGNIOL-VILLARD 	{								\
4493a686eeSJean-Christophe PLAGNIOL-VILLARD 		error_code;						\
4593a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
4693a686eeSJean-Christophe PLAGNIOL-VILLARD 	}								\
4793a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
4893a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_##rw##_config_##size(hose, dev, offset, value);	\
4993a686eeSJean-Christophe PLAGNIOL-VILLARD }
5093a686eeSJean-Christophe PLAGNIOL-VILLARD 
5193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, byte, u8 *, *value = 0xff)
5293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, word, u16 *, *value = 0xffff)
5393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, dword, u32 *, *value = 0xffffffff)
5493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, byte, u8, )
5593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, word, u16, )
5693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, dword, u32, )
5793a686eeSJean-Christophe PLAGNIOL-VILLARD 
5893a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_READ_VIA_DWORD_OP(size, type, off_mask)			\
5993a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
6093a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_dev_t dev,			\
6193a686eeSJean-Christophe PLAGNIOL-VILLARD 					int offset, type val)		\
6293a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
6393a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 val32;							\
6493a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
6593a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) {	\
6693a686eeSJean-Christophe PLAGNIOL-VILLARD 		*val = -1;						\
6793a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
6893a686eeSJean-Christophe PLAGNIOL-VILLARD 	}								\
6993a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
7093a686eeSJean-Christophe PLAGNIOL-VILLARD 	*val = (val32 >> ((offset & (int)off_mask) * 8));		\
7193a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
7293a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;							\
7393a686eeSJean-Christophe PLAGNIOL-VILLARD }
7493a686eeSJean-Christophe PLAGNIOL-VILLARD 
7593a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask)		\
7693a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
7793a686eeSJean-Christophe PLAGNIOL-VILLARD 					     pci_dev_t dev,		\
7893a686eeSJean-Christophe PLAGNIOL-VILLARD 					     int offset, type val)	\
7993a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
8093a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 val32, mask, ldata, shift;					\
8193a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8293a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
8393a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
8493a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8593a686eeSJean-Christophe PLAGNIOL-VILLARD 	shift = ((offset & (int)off_mask) * 8);				\
8693a686eeSJean-Christophe PLAGNIOL-VILLARD 	ldata = (((unsigned long)val) & val_mask) << shift;		\
8793a686eeSJean-Christophe PLAGNIOL-VILLARD 	mask = val_mask << shift;					\
8893a686eeSJean-Christophe PLAGNIOL-VILLARD 	val32 = (val32 & ~mask) | ldata;				\
8993a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
9093a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
9193a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
9293a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
9393a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;							\
9493a686eeSJean-Christophe PLAGNIOL-VILLARD }
9593a686eeSJean-Christophe PLAGNIOL-VILLARD 
9693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
9793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
9893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
9993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
10093a686eeSJean-Christophe PLAGNIOL-VILLARD 
1016e61fae4SBecky Bruce /* Get a virtual address associated with a BAR region */
1026e61fae4SBecky Bruce void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
1036e61fae4SBecky Bruce {
1046e61fae4SBecky Bruce 	pci_addr_t pci_bus_addr;
105cf5787f2SKumar Gala 	u32 bar_response;
1066e61fae4SBecky Bruce 
1076e61fae4SBecky Bruce 	/* read BAR address */
1086e61fae4SBecky Bruce 	pci_read_config_dword(pdev, bar, &bar_response);
109cf5787f2SKumar Gala 	pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1106e61fae4SBecky Bruce 
1116e61fae4SBecky Bruce 	/*
1126e61fae4SBecky Bruce 	 * Pass "0" as the length argument to pci_bus_to_virt.  The arg
1136e61fae4SBecky Bruce 	 * isn't actualy used on any platform because u-boot assumes a static
1146e61fae4SBecky Bruce 	 * linear mapping.  In the future, this could read the BAR size
1156e61fae4SBecky Bruce 	 * and pass that as the size if needed.
1166e61fae4SBecky Bruce 	 */
1176e61fae4SBecky Bruce 	return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1186e61fae4SBecky Bruce }
1196e61fae4SBecky Bruce 
12093a686eeSJean-Christophe PLAGNIOL-VILLARD /*
12193a686eeSJean-Christophe PLAGNIOL-VILLARD  *
12293a686eeSJean-Christophe PLAGNIOL-VILLARD  */
12393a686eeSJean-Christophe PLAGNIOL-VILLARD 
12496d61603SJohn Schmoller static struct pci_controller* hose_head;
12593a686eeSJean-Christophe PLAGNIOL-VILLARD 
12693a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose)
12793a686eeSJean-Christophe PLAGNIOL-VILLARD {
12893a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller **phose = &hose_head;
12993a686eeSJean-Christophe PLAGNIOL-VILLARD 
13093a686eeSJean-Christophe PLAGNIOL-VILLARD 	while(*phose)
13193a686eeSJean-Christophe PLAGNIOL-VILLARD 		phose = &(*phose)->next;
13293a686eeSJean-Christophe PLAGNIOL-VILLARD 
13393a686eeSJean-Christophe PLAGNIOL-VILLARD 	hose->next = NULL;
13493a686eeSJean-Christophe PLAGNIOL-VILLARD 
13593a686eeSJean-Christophe PLAGNIOL-VILLARD 	*phose = hose;
13693a686eeSJean-Christophe PLAGNIOL-VILLARD }
13793a686eeSJean-Christophe PLAGNIOL-VILLARD 
13893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose(int bus)
13993a686eeSJean-Christophe PLAGNIOL-VILLARD {
14093a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose;
14193a686eeSJean-Christophe PLAGNIOL-VILLARD 
142cb2bf931SAndrew Sharp 	for (hose = hose_head; hose; hose = hose->next) {
14393a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus >= hose->first_busno && bus <= hose->last_busno)
14493a686eeSJean-Christophe PLAGNIOL-VILLARD 			return hose;
145cb2bf931SAndrew Sharp 	}
14693a686eeSJean-Christophe PLAGNIOL-VILLARD 
14793a686eeSJean-Christophe PLAGNIOL-VILLARD 	printf("pci_bus_to_hose() failed\n");
14893a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
14993a686eeSJean-Christophe PLAGNIOL-VILLARD }
15093a686eeSJean-Christophe PLAGNIOL-VILLARD 
1513a0e3c27SKumar Gala struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
1523a0e3c27SKumar Gala {
1533a0e3c27SKumar Gala 	struct pci_controller *hose;
1543a0e3c27SKumar Gala 
1553a0e3c27SKumar Gala 	for (hose = hose_head; hose; hose = hose->next) {
1563a0e3c27SKumar Gala 		if (hose->cfg_addr == cfg_addr)
1573a0e3c27SKumar Gala 			return hose;
1583a0e3c27SKumar Gala 	}
1593a0e3c27SKumar Gala 
1603a0e3c27SKumar Gala 	return NULL;
1613a0e3c27SKumar Gala }
1623a0e3c27SKumar Gala 
163cc2a8c77SAnton Vorontsov int pci_last_busno(void)
164cc2a8c77SAnton Vorontsov {
165cc2a8c77SAnton Vorontsov 	struct pci_controller *hose = hose_head;
166cc2a8c77SAnton Vorontsov 
167cc2a8c77SAnton Vorontsov 	if (!hose)
168cc2a8c77SAnton Vorontsov 		return -1;
169cc2a8c77SAnton Vorontsov 
170cc2a8c77SAnton Vorontsov 	while (hose->next)
171cc2a8c77SAnton Vorontsov 		hose = hose->next;
172cc2a8c77SAnton Vorontsov 
173cc2a8c77SAnton Vorontsov 	return hose->last_busno;
174cc2a8c77SAnton Vorontsov }
175cc2a8c77SAnton Vorontsov 
17693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
17793a686eeSJean-Christophe PLAGNIOL-VILLARD {
17893a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller * hose;
17993a686eeSJean-Christophe PLAGNIOL-VILLARD 	u16 vendor, device;
18093a686eeSJean-Christophe PLAGNIOL-VILLARD 	u8 header_type;
18193a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t bdf;
18293a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i, bus, found_multi = 0;
18393a686eeSJean-Christophe PLAGNIOL-VILLARD 
184cb2bf931SAndrew Sharp 	for (hose = hose_head; hose; hose = hose->next) {
1856d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
18693a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
18793a686eeSJean-Christophe PLAGNIOL-VILLARD #else
18893a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
18993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
19093a686eeSJean-Christophe PLAGNIOL-VILLARD 			for (bdf = PCI_BDF(bus, 0, 0);
19193a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
192cb2bf931SAndrew Sharp 			     bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
193cb2bf931SAndrew Sharp 				PCI_MAX_PCI_FUNCTIONS - 1);
19493a686eeSJean-Christophe PLAGNIOL-VILLARD #else
19593a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf < PCI_BDF(bus + 1, 0, 0);
19693a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
197cb2bf931SAndrew Sharp 			     bdf += PCI_BDF(0, 0, 1)) {
1984efe52bfSThierry Reding 				if (pci_skip_dev(hose, bdf))
1994efe52bfSThierry Reding 					continue;
2004efe52bfSThierry Reding 
20193a686eeSJean-Christophe PLAGNIOL-VILLARD 				if (!PCI_FUNC(bdf)) {
20293a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_read_config_byte(bdf,
20393a686eeSJean-Christophe PLAGNIOL-VILLARD 							     PCI_HEADER_TYPE,
20493a686eeSJean-Christophe PLAGNIOL-VILLARD 							     &header_type);
20593a686eeSJean-Christophe PLAGNIOL-VILLARD 
20693a686eeSJean-Christophe PLAGNIOL-VILLARD 					found_multi = header_type & 0x80;
20793a686eeSJean-Christophe PLAGNIOL-VILLARD 				} else {
20893a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (!found_multi)
20993a686eeSJean-Christophe PLAGNIOL-VILLARD 						continue;
21093a686eeSJean-Christophe PLAGNIOL-VILLARD 				}
21193a686eeSJean-Christophe PLAGNIOL-VILLARD 
21293a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
21393a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_VENDOR_ID,
21493a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &vendor);
21593a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
21693a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_DEVICE_ID,
21793a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &device);
21893a686eeSJean-Christophe PLAGNIOL-VILLARD 
219cb2bf931SAndrew Sharp 				for (i = 0; ids[i].vendor != 0; i++) {
22093a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (vendor == ids[i].vendor &&
221cb2bf931SAndrew Sharp 					    device == ids[i].device) {
22293a686eeSJean-Christophe PLAGNIOL-VILLARD 						if (index <= 0)
22393a686eeSJean-Christophe PLAGNIOL-VILLARD 							return bdf;
22493a686eeSJean-Christophe PLAGNIOL-VILLARD 
22593a686eeSJean-Christophe PLAGNIOL-VILLARD 						index--;
22693a686eeSJean-Christophe PLAGNIOL-VILLARD 					}
22793a686eeSJean-Christophe PLAGNIOL-VILLARD 				}
22893a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
229cb2bf931SAndrew Sharp 	}
23093a686eeSJean-Christophe PLAGNIOL-VILLARD 
231cb2bf931SAndrew Sharp 	return -1;
23293a686eeSJean-Christophe PLAGNIOL-VILLARD }
23393a686eeSJean-Christophe PLAGNIOL-VILLARD 
23493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
23593a686eeSJean-Christophe PLAGNIOL-VILLARD {
23693a686eeSJean-Christophe PLAGNIOL-VILLARD 	static struct pci_device_id ids[2] = {{}, {0, 0}};
23793a686eeSJean-Christophe PLAGNIOL-VILLARD 
23893a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].vendor = vendor;
23993a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].device = device;
24093a686eeSJean-Christophe PLAGNIOL-VILLARD 
24193a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_find_devices(ids, index);
24293a686eeSJean-Christophe PLAGNIOL-VILLARD }
24393a686eeSJean-Christophe PLAGNIOL-VILLARD 
24493a686eeSJean-Christophe PLAGNIOL-VILLARD /*
24593a686eeSJean-Christophe PLAGNIOL-VILLARD  *
24693a686eeSJean-Christophe PLAGNIOL-VILLARD  */
24793a686eeSJean-Christophe PLAGNIOL-VILLARD 
2482d43e873SKumar Gala int __pci_hose_phys_to_bus(struct pci_controller *hose,
24936f32675SBecky Bruce 				phys_addr_t phys_addr,
2502d43e873SKumar Gala 				unsigned long flags,
2512d43e873SKumar Gala 				unsigned long skip_mask,
2522d43e873SKumar Gala 				pci_addr_t *ba)
25393a686eeSJean-Christophe PLAGNIOL-VILLARD {
25493a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
25530e76d5eSKumar Gala 	pci_addr_t bus_addr;
25693a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
25793a686eeSJean-Christophe PLAGNIOL-VILLARD 
25893a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
25993a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
26093a686eeSJean-Christophe PLAGNIOL-VILLARD 
26193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
26293a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
26393a686eeSJean-Christophe PLAGNIOL-VILLARD 
2642d43e873SKumar Gala 		if (res->flags & skip_mask)
2652d43e873SKumar Gala 			continue;
2662d43e873SKumar Gala 
26793a686eeSJean-Christophe PLAGNIOL-VILLARD 		bus_addr = phys_addr - res->phys_start + res->bus_start;
26893a686eeSJean-Christophe PLAGNIOL-VILLARD 
26993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
27093a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
2712d43e873SKumar Gala 			*ba = bus_addr;
27293a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
27393a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
2742d43e873SKumar Gala 	}
27593a686eeSJean-Christophe PLAGNIOL-VILLARD 
2762d43e873SKumar Gala 	return 1;
2772d43e873SKumar Gala }
2782d43e873SKumar Gala 
2792d43e873SKumar Gala pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
2802d43e873SKumar Gala 				    phys_addr_t phys_addr,
28193a686eeSJean-Christophe PLAGNIOL-VILLARD 				    unsigned long flags)
28293a686eeSJean-Christophe PLAGNIOL-VILLARD {
2832d43e873SKumar Gala 	pci_addr_t bus_addr = 0;
2842d43e873SKumar Gala 	int ret;
2852d43e873SKumar Gala 
2862d43e873SKumar Gala 	if (!hose) {
2872d43e873SKumar Gala 		puts("pci_hose_phys_to_bus: invalid hose\n");
2882d43e873SKumar Gala 		return bus_addr;
2892d43e873SKumar Gala 	}
2902d43e873SKumar Gala 
291cb2bf931SAndrew Sharp 	/*
292cb2bf931SAndrew Sharp 	 * if PCI_REGION_MEM is set we do a two pass search with preference
293cb2bf931SAndrew Sharp 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
294cb2bf931SAndrew Sharp 	 */
2952d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
2962d43e873SKumar Gala 		ret = __pci_hose_phys_to_bus(hose, phys_addr,
2972d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &bus_addr);
2982d43e873SKumar Gala 		if (!ret)
2992d43e873SKumar Gala 			return bus_addr;
3002d43e873SKumar Gala 	}
3012d43e873SKumar Gala 
3022d43e873SKumar Gala 	ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
3032d43e873SKumar Gala 
3042d43e873SKumar Gala 	if (ret)
3052d43e873SKumar Gala 		puts("pci_hose_phys_to_bus: invalid physical address\n");
3062d43e873SKumar Gala 
3072d43e873SKumar Gala 	return bus_addr;
3082d43e873SKumar Gala }
3092d43e873SKumar Gala 
3102d43e873SKumar Gala int __pci_hose_bus_to_phys(struct pci_controller *hose,
3112d43e873SKumar Gala 				pci_addr_t bus_addr,
3122d43e873SKumar Gala 				unsigned long flags,
3132d43e873SKumar Gala 				unsigned long skip_mask,
3142d43e873SKumar Gala 				phys_addr_t *pa)
3152d43e873SKumar Gala {
31693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
31793a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
31893a686eeSJean-Christophe PLAGNIOL-VILLARD 
31993a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
32093a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
32193a686eeSJean-Christophe PLAGNIOL-VILLARD 
32293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
32393a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
32493a686eeSJean-Christophe PLAGNIOL-VILLARD 
3252d43e873SKumar Gala 		if (res->flags & skip_mask)
3262d43e873SKumar Gala 			continue;
3272d43e873SKumar Gala 
32893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
329d878c9a9SStephen Warren 			(bus_addr - res->bus_start) < res->size) {
3302d43e873SKumar Gala 			*pa = (bus_addr - res->bus_start + res->phys_start);
33193a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
33293a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
3332d43e873SKumar Gala 	}
3342d43e873SKumar Gala 
3352d43e873SKumar Gala 	return 1;
3362d43e873SKumar Gala }
3372d43e873SKumar Gala 
3382d43e873SKumar Gala phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
3392d43e873SKumar Gala 				 pci_addr_t bus_addr,
3402d43e873SKumar Gala 				 unsigned long flags)
3412d43e873SKumar Gala {
3422d43e873SKumar Gala 	phys_addr_t phys_addr = 0;
3432d43e873SKumar Gala 	int ret;
3442d43e873SKumar Gala 
3452d43e873SKumar Gala 	if (!hose) {
3462d43e873SKumar Gala 		puts("pci_hose_bus_to_phys: invalid hose\n");
3472d43e873SKumar Gala 		return phys_addr;
3482d43e873SKumar Gala 	}
3492d43e873SKumar Gala 
350cb2bf931SAndrew Sharp 	/*
351cb2bf931SAndrew Sharp 	 * if PCI_REGION_MEM is set we do a two pass search with preference
352cb2bf931SAndrew Sharp 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
353cb2bf931SAndrew Sharp 	 */
3542d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
3552d43e873SKumar Gala 		ret = __pci_hose_bus_to_phys(hose, bus_addr,
3562d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &phys_addr);
3572d43e873SKumar Gala 		if (!ret)
3582d43e873SKumar Gala 			return phys_addr;
3592d43e873SKumar Gala 	}
3602d43e873SKumar Gala 
3612d43e873SKumar Gala 	ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
3622d43e873SKumar Gala 
3632d43e873SKumar Gala 	if (ret)
3642d43e873SKumar Gala 		puts("pci_hose_bus_to_phys: invalid physical address\n");
3652d43e873SKumar Gala 
3662d43e873SKumar Gala 	return phys_addr;
3672d43e873SKumar Gala }
36893a686eeSJean-Christophe PLAGNIOL-VILLARD 
369*e8a552ebSSimon Glass void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
370*e8a552ebSSimon Glass 		     u32 addr_and_ctrl)
371*e8a552ebSSimon Glass {
372*e8a552ebSSimon Glass 	int bar;
373*e8a552ebSSimon Glass 
374*e8a552ebSSimon Glass 	bar = PCI_BASE_ADDRESS_0 + barnum * 4;
375*e8a552ebSSimon Glass 	pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
376*e8a552ebSSimon Glass }
377*e8a552ebSSimon Glass 
378*e8a552ebSSimon Glass u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
379*e8a552ebSSimon Glass {
380*e8a552ebSSimon Glass 	u32 addr;
381*e8a552ebSSimon Glass 	int bar;
382*e8a552ebSSimon Glass 
383*e8a552ebSSimon Glass 	bar = PCI_BASE_ADDRESS_0 + barnum * 4;
384*e8a552ebSSimon Glass 	pci_hose_read_config_dword(hose, dev, bar, &addr);
385*e8a552ebSSimon Glass 	if (addr & PCI_BASE_ADDRESS_SPACE_IO)
386*e8a552ebSSimon Glass 		return addr & PCI_BASE_ADDRESS_IO_MASK;
387*e8a552ebSSimon Glass 	else
388*e8a552ebSSimon Glass 		return addr & PCI_BASE_ADDRESS_MEM_MASK;
389*e8a552ebSSimon Glass }
39093a686eeSJean-Christophe PLAGNIOL-VILLARD 
39193a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_config_device(struct pci_controller *hose,
39293a686eeSJean-Christophe PLAGNIOL-VILLARD 			   pci_dev_t dev,
39393a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long io,
39430e76d5eSKumar Gala 			   pci_addr_t mem,
39593a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long command)
39693a686eeSJean-Christophe PLAGNIOL-VILLARD {
397cf5787f2SKumar Gala 	u32 bar_response;
398af778c6dSAndrew Sharp 	unsigned int old_command;
39930e76d5eSKumar Gala 	pci_addr_t bar_value;
40030e76d5eSKumar Gala 	pci_size_t bar_size;
40193a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char pin;
40293a686eeSJean-Christophe PLAGNIOL-VILLARD 	int bar, found_mem64;
40393a686eeSJean-Christophe PLAGNIOL-VILLARD 
404cb2bf931SAndrew Sharp 	debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
405cb2bf931SAndrew Sharp 		(u64)mem, command);
40693a686eeSJean-Christophe PLAGNIOL-VILLARD 
40793a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
40893a686eeSJean-Christophe PLAGNIOL-VILLARD 
409252b404dSWolfgang Denk 	for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
41093a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
41193a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_dword(hose, dev, bar, &bar_response);
41293a686eeSJean-Christophe PLAGNIOL-VILLARD 
41393a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!bar_response)
41493a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
41593a686eeSJean-Christophe PLAGNIOL-VILLARD 
41693a686eeSJean-Christophe PLAGNIOL-VILLARD 		found_mem64 = 0;
41793a686eeSJean-Christophe PLAGNIOL-VILLARD 
41893a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Check the BAR type and set our address mask */
41993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bar_response & PCI_BASE_ADDRESS_SPACE) {
42093a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
42193a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to a multiple of size */
42293a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = ((io - 1) | (bar_size - 1)) + 1;
42393a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = io;
42493a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
42593a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = io + bar_size;
42693a686eeSJean-Christophe PLAGNIOL-VILLARD 		} else {
42793a686eeSJean-Christophe PLAGNIOL-VILLARD 			if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
42830e76d5eSKumar Gala 				PCI_BASE_ADDRESS_MEM_TYPE_64) {
42930e76d5eSKumar Gala 				u32 bar_response_upper;
43030e76d5eSKumar Gala 				u64 bar64;
431cb2bf931SAndrew Sharp 				pci_hose_write_config_dword(hose, dev, bar + 4,
432cb2bf931SAndrew Sharp 					0xffffffff);
433cb2bf931SAndrew Sharp 				pci_hose_read_config_dword(hose, dev, bar + 4,
434cb2bf931SAndrew Sharp 					&bar_response_upper);
43593a686eeSJean-Christophe PLAGNIOL-VILLARD 
43630e76d5eSKumar Gala 				bar64 = ((u64)bar_response_upper << 32) | bar_response;
43730e76d5eSKumar Gala 
43830e76d5eSKumar Gala 				bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
43930e76d5eSKumar Gala 				found_mem64 = 1;
44030e76d5eSKumar Gala 			} else {
44130e76d5eSKumar Gala 				bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
44230e76d5eSKumar Gala 			}
44393a686eeSJean-Christophe PLAGNIOL-VILLARD 
44493a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to multiple of size */
44593a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = ((mem - 1) | (bar_size - 1)) + 1;
44693a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = mem;
44793a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
44893a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = mem + bar_size;
44993a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
45093a686eeSJean-Christophe PLAGNIOL-VILLARD 
45193a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Write it out and update our limit */
45230e76d5eSKumar Gala 		pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
45393a686eeSJean-Christophe PLAGNIOL-VILLARD 
45493a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (found_mem64) {
45593a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar += 4;
45630e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
457cb2bf931SAndrew Sharp 			pci_hose_write_config_dword(hose, dev, bar,
458cb2bf931SAndrew Sharp 				(u32)(bar_value >> 32));
45930e76d5eSKumar Gala #else
46093a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
46130e76d5eSKumar Gala #endif
46293a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
46393a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
46493a686eeSJean-Christophe PLAGNIOL-VILLARD 
46593a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Cache Line Size Register */
46693a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
46793a686eeSJean-Christophe PLAGNIOL-VILLARD 
46893a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Latency Timer */
46993a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
47093a686eeSJean-Christophe PLAGNIOL-VILLARD 
47193a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Disable interrupt line, if device says it wants to use interrupts */
47293a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
47393a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pin != 0) {
47493a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff);
47593a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
47693a686eeSJean-Christophe PLAGNIOL-VILLARD 
47793a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
47893a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
47993a686eeSJean-Christophe PLAGNIOL-VILLARD 				     (old_command & 0xffff0000) | command);
48093a686eeSJean-Christophe PLAGNIOL-VILLARD 
48193a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;
48293a686eeSJean-Christophe PLAGNIOL-VILLARD }
48393a686eeSJean-Christophe PLAGNIOL-VILLARD 
48493a686eeSJean-Christophe PLAGNIOL-VILLARD /*
48593a686eeSJean-Christophe PLAGNIOL-VILLARD  *
48693a686eeSJean-Christophe PLAGNIOL-VILLARD  */
48793a686eeSJean-Christophe PLAGNIOL-VILLARD 
48893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose,
48993a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned short class,
49093a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int vendor,
49193a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int device,
49293a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int bus,
49393a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int dev,
49493a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int func)
49593a686eeSJean-Christophe PLAGNIOL-VILLARD {
49693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *table;
49793a686eeSJean-Christophe PLAGNIOL-VILLARD 
49893a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (table = hose->config_table; table && table->vendor; table++) {
49993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
50093a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->device == PCI_ANY_ID || table->device == device) &&
50193a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->class  == PCI_ANY_ID || table->class  == class)  &&
50293a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->bus    == PCI_ANY_ID || table->bus    == bus)    &&
50393a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->dev    == PCI_ANY_ID || table->dev    == dev)    &&
50493a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->func   == PCI_ANY_ID || table->func   == func)) {
50593a686eeSJean-Christophe PLAGNIOL-VILLARD 			return table;
50693a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
50793a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
50893a686eeSJean-Christophe PLAGNIOL-VILLARD 
50993a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
51093a686eeSJean-Christophe PLAGNIOL-VILLARD }
51193a686eeSJean-Christophe PLAGNIOL-VILLARD 
51293a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose,
51393a686eeSJean-Christophe PLAGNIOL-VILLARD 			       pci_dev_t dev,
51493a686eeSJean-Christophe PLAGNIOL-VILLARD 			       struct pci_config_table *entry)
51593a686eeSJean-Christophe PLAGNIOL-VILLARD {
516cb2bf931SAndrew Sharp 	pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
517cb2bf931SAndrew Sharp 		entry->priv[2]);
51893a686eeSJean-Christophe PLAGNIOL-VILLARD }
51993a686eeSJean-Christophe PLAGNIOL-VILLARD 
52093a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose,
52193a686eeSJean-Christophe PLAGNIOL-VILLARD 			    pci_dev_t dev, struct pci_config_table *entry)
52293a686eeSJean-Christophe PLAGNIOL-VILLARD {
52393a686eeSJean-Christophe PLAGNIOL-VILLARD }
52493a686eeSJean-Christophe PLAGNIOL-VILLARD 
52593a686eeSJean-Christophe PLAGNIOL-VILLARD /*
526cb2bf931SAndrew Sharp  * HJF: Changed this to return int. I think this is required
52793a686eeSJean-Christophe PLAGNIOL-VILLARD  * to get the correct result when scanning bridges
52893a686eeSJean-Christophe PLAGNIOL-VILLARD  */
52993a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
53093a686eeSJean-Christophe PLAGNIOL-VILLARD 
531983eb9d1SPeter Tyser #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW)
532983eb9d1SPeter Tyser const char * pci_class_str(u8 class)
533983eb9d1SPeter Tyser {
534983eb9d1SPeter Tyser 	switch (class) {
535983eb9d1SPeter Tyser 	case PCI_CLASS_NOT_DEFINED:
536983eb9d1SPeter Tyser 		return "Build before PCI Rev2.0";
537983eb9d1SPeter Tyser 		break;
538983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_STORAGE:
539983eb9d1SPeter Tyser 		return "Mass storage controller";
540983eb9d1SPeter Tyser 		break;
541983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_NETWORK:
542983eb9d1SPeter Tyser 		return "Network controller";
543983eb9d1SPeter Tyser 		break;
544983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_DISPLAY:
545983eb9d1SPeter Tyser 		return "Display controller";
546983eb9d1SPeter Tyser 		break;
547983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_MULTIMEDIA:
548983eb9d1SPeter Tyser 		return "Multimedia device";
549983eb9d1SPeter Tyser 		break;
550983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_MEMORY:
551983eb9d1SPeter Tyser 		return "Memory controller";
552983eb9d1SPeter Tyser 		break;
553983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_BRIDGE:
554983eb9d1SPeter Tyser 		return "Bridge device";
555983eb9d1SPeter Tyser 		break;
556983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_COMMUNICATION:
557983eb9d1SPeter Tyser 		return "Simple comm. controller";
558983eb9d1SPeter Tyser 		break;
559983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SYSTEM:
560983eb9d1SPeter Tyser 		return "Base system peripheral";
561983eb9d1SPeter Tyser 		break;
562983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_INPUT:
563983eb9d1SPeter Tyser 		return "Input device";
564983eb9d1SPeter Tyser 		break;
565983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_DOCKING:
566983eb9d1SPeter Tyser 		return "Docking station";
567983eb9d1SPeter Tyser 		break;
568983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_PROCESSOR:
569983eb9d1SPeter Tyser 		return "Processor";
570983eb9d1SPeter Tyser 		break;
571983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SERIAL:
572983eb9d1SPeter Tyser 		return "Serial bus controller";
573983eb9d1SPeter Tyser 		break;
574983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_INTELLIGENT:
575983eb9d1SPeter Tyser 		return "Intelligent controller";
576983eb9d1SPeter Tyser 		break;
577983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SATELLITE:
578983eb9d1SPeter Tyser 		return "Satellite controller";
579983eb9d1SPeter Tyser 		break;
580983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_CRYPT:
581983eb9d1SPeter Tyser 		return "Cryptographic device";
582983eb9d1SPeter Tyser 		break;
583983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SIGNAL_PROCESSING:
584983eb9d1SPeter Tyser 		return "DSP";
585983eb9d1SPeter Tyser 		break;
586983eb9d1SPeter Tyser 	case PCI_CLASS_OTHERS:
587983eb9d1SPeter Tyser 		return "Does not fit any class";
588983eb9d1SPeter Tyser 		break;
589983eb9d1SPeter Tyser 	default:
590983eb9d1SPeter Tyser 	return  "???";
591983eb9d1SPeter Tyser 		break;
592983eb9d1SPeter Tyser 	};
593983eb9d1SPeter Tyser }
594983eb9d1SPeter Tyser #endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */
595983eb9d1SPeter Tyser 
5967b19fd6dSJeroen Hofstee __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
597dc1da42fSStefan Roese {
598dc1da42fSStefan Roese 	/*
599dc1da42fSStefan Roese 	 * Check if pci device should be skipped in configuration
600dc1da42fSStefan Roese 	 */
601dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
602dc1da42fSStefan Roese #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
603dc1da42fSStefan Roese 		/*
604dc1da42fSStefan Roese 		 * Only skip configuration if "pciconfighost" is not set
605dc1da42fSStefan Roese 		 */
606dc1da42fSStefan Roese 		if (getenv("pciconfighost") == NULL)
607dc1da42fSStefan Roese 			return 1;
608dc1da42fSStefan Roese #else
609dc1da42fSStefan Roese 		return 1;
610dc1da42fSStefan Roese #endif
611dc1da42fSStefan Roese 	}
612dc1da42fSStefan Roese 
613dc1da42fSStefan Roese 	return 0;
614dc1da42fSStefan Roese }
615dc1da42fSStefan Roese 
616dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW
6177b19fd6dSJeroen Hofstee __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
618dc1da42fSStefan Roese {
619dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0))
620dc1da42fSStefan Roese 		return 0;
621dc1da42fSStefan Roese 
622dc1da42fSStefan Roese 	return 1;
623dc1da42fSStefan Roese }
624dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */
625dc1da42fSStefan Roese 
62693a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus)
62793a686eeSJean-Christophe PLAGNIOL-VILLARD {
62893a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned int sub_bus, found_multi = 0;
62993a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned short vendor, device, class;
63093a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char header_type;
63103992ac2SAndrew Sharp #ifndef CONFIG_PCI_PNP
63293a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *cfg;
63303992ac2SAndrew Sharp #endif
63493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t dev;
635009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
636009884aeSPeter Tyser 	static int indent = 0;
637009884aeSPeter Tyser #endif
63893a686eeSJean-Christophe PLAGNIOL-VILLARD 
63993a686eeSJean-Christophe PLAGNIOL-VILLARD 	sub_bus = bus;
64093a686eeSJean-Christophe PLAGNIOL-VILLARD 
64193a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (dev =  PCI_BDF(bus,0,0);
642cb2bf931SAndrew Sharp 	     dev <  PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
643cb2bf931SAndrew Sharp 				PCI_MAX_PCI_FUNCTIONS - 1);
644dc1da42fSStefan Roese 	     dev += PCI_BDF(0, 0, 1)) {
645dc1da42fSStefan Roese 
646dc1da42fSStefan Roese 		if (pci_skip_dev(hose, dev))
647dc1da42fSStefan Roese 			continue;
64893a686eeSJean-Christophe PLAGNIOL-VILLARD 
64993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (PCI_FUNC(dev) && !found_multi)
65093a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
65193a686eeSJean-Christophe PLAGNIOL-VILLARD 
65293a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
65393a686eeSJean-Christophe PLAGNIOL-VILLARD 
65493a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
65593a686eeSJean-Christophe PLAGNIOL-VILLARD 
656983eb9d1SPeter Tyser 		if (vendor == 0xffff || vendor == 0x0000)
657983eb9d1SPeter Tyser 			continue;
65893a686eeSJean-Christophe PLAGNIOL-VILLARD 
65993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!PCI_FUNC(dev))
66093a686eeSJean-Christophe PLAGNIOL-VILLARD 			found_multi = header_type & 0x80;
66193a686eeSJean-Christophe PLAGNIOL-VILLARD 
66293a686eeSJean-Christophe PLAGNIOL-VILLARD 		debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
66393a686eeSJean-Christophe PLAGNIOL-VILLARD 			PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
66493a686eeSJean-Christophe PLAGNIOL-VILLARD 
66593a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
66693a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
66793a686eeSJean-Christophe PLAGNIOL-VILLARD 
6680991866cSTim Harvey #ifdef CONFIG_PCI_FIXUP_DEV
6690991866cSTim Harvey 		board_pci_fixup_dev(hose, dev, vendor, device, class);
6700991866cSTim Harvey #endif
6710991866cSTim Harvey 
672a38d216eSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
673009884aeSPeter Tyser 		indent++;
674009884aeSPeter Tyser 
675009884aeSPeter Tyser 		/* Print leading space, including bus indentation */
676009884aeSPeter Tyser 		printf("%*c", indent + 1, ' ');
677009884aeSPeter Tyser 
678a38d216eSPeter Tyser 		if (pci_print_dev(hose, dev)) {
679009884aeSPeter Tyser 			printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
680009884aeSPeter Tyser 			       PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
681a38d216eSPeter Tyser 			       vendor, device, pci_class_str(class >> 8));
682a38d216eSPeter Tyser 		}
683a38d216eSPeter Tyser #endif
684a38d216eSPeter Tyser 
68503992ac2SAndrew Sharp #ifdef CONFIG_PCI_PNP
686b4141195SMasahiro Yamada 		sub_bus = max((unsigned int)pciauto_config_device(hose, dev),
687b4141195SMasahiro Yamada 			      sub_bus);
68803992ac2SAndrew Sharp #else
68993a686eeSJean-Christophe PLAGNIOL-VILLARD 		cfg = pci_find_config(hose, class, vendor, device,
69093a686eeSJean-Christophe PLAGNIOL-VILLARD 				      PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
69193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (cfg) {
69293a686eeSJean-Christophe PLAGNIOL-VILLARD 			cfg->config_device(hose, dev, cfg);
693b4141195SMasahiro Yamada 			sub_bus = max(sub_bus,
694b4141195SMasahiro Yamada 				      (unsigned int)hose->current_busno);
69593a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
69603992ac2SAndrew Sharp #endif
697a38d216eSPeter Tyser 
698009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
699009884aeSPeter Tyser 		indent--;
700009884aeSPeter Tyser #endif
701009884aeSPeter Tyser 
70293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (hose->fixup_irq)
70393a686eeSJean-Christophe PLAGNIOL-VILLARD 			hose->fixup_irq(hose, dev);
70493a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
70593a686eeSJean-Christophe PLAGNIOL-VILLARD 
70693a686eeSJean-Christophe PLAGNIOL-VILLARD 	return sub_bus;
70793a686eeSJean-Christophe PLAGNIOL-VILLARD }
70893a686eeSJean-Christophe PLAGNIOL-VILLARD 
70993a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose)
71093a686eeSJean-Christophe PLAGNIOL-VILLARD {
7110da1fb03SAnatolij Gustschin #if defined(CONFIG_PCI_BOOTDELAY)
7120da1fb03SAnatolij Gustschin 	static int pcidelay_done;
7130da1fb03SAnatolij Gustschin 	char *s;
7140da1fb03SAnatolij Gustschin 	int i;
7150da1fb03SAnatolij Gustschin 
7160da1fb03SAnatolij Gustschin 	if (!pcidelay_done) {
7170da1fb03SAnatolij Gustschin 		/* wait "pcidelay" ms (if defined)... */
7180da1fb03SAnatolij Gustschin 		s = getenv("pcidelay");
7190da1fb03SAnatolij Gustschin 		if (s) {
7200da1fb03SAnatolij Gustschin 			int val = simple_strtoul(s, NULL, 10);
7210da1fb03SAnatolij Gustschin 			for (i = 0; i < val; i++)
7220da1fb03SAnatolij Gustschin 				udelay(1000);
7230da1fb03SAnatolij Gustschin 		}
7240da1fb03SAnatolij Gustschin 		pcidelay_done = 1;
7250da1fb03SAnatolij Gustschin 	}
7260da1fb03SAnatolij Gustschin #endif /* CONFIG_PCI_BOOTDELAY */
7270da1fb03SAnatolij Gustschin 
728cb2bf931SAndrew Sharp 	/*
729cb2bf931SAndrew Sharp 	 * Start scan at current_busno.
73093a686eeSJean-Christophe PLAGNIOL-VILLARD 	 * PCIe will start scan at first_busno+1.
73193a686eeSJean-Christophe PLAGNIOL-VILLARD 	 */
73293a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* For legacy support, ensure current >= first */
73393a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (hose->first_busno > hose->current_busno)
73493a686eeSJean-Christophe PLAGNIOL-VILLARD 		hose->current_busno = hose->first_busno;
73593a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
73693a686eeSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_init(hose);
73793a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
73893a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_scan_bus(hose, hose->current_busno);
73993a686eeSJean-Christophe PLAGNIOL-VILLARD }
74093a686eeSJean-Christophe PLAGNIOL-VILLARD 
74193a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void)
74293a686eeSJean-Christophe PLAGNIOL-VILLARD {
74396d61603SJohn Schmoller 	hose_head = NULL;
74496d61603SJohn Schmoller 
74593a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* now call board specific pci_init()... */
74693a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_init_board();
74793a686eeSJean-Christophe PLAGNIOL-VILLARD }
748287df01eSZhao Qiang 
749287df01eSZhao Qiang /* Returns the address of the requested capability structure within the
750287df01eSZhao Qiang  * device's PCI configuration space or 0 in case the device does not
751287df01eSZhao Qiang  * support it.
752287df01eSZhao Qiang  * */
753287df01eSZhao Qiang int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
754287df01eSZhao Qiang 			     int cap)
755287df01eSZhao Qiang {
756287df01eSZhao Qiang 	int pos;
757287df01eSZhao Qiang 	u8 hdr_type;
758287df01eSZhao Qiang 
759287df01eSZhao Qiang 	pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type);
760287df01eSZhao Qiang 
761287df01eSZhao Qiang 	pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F);
762287df01eSZhao Qiang 
763287df01eSZhao Qiang 	if (pos)
764287df01eSZhao Qiang 		pos = pci_find_cap(hose, dev, pos, cap);
765287df01eSZhao Qiang 
766287df01eSZhao Qiang 	return pos;
767287df01eSZhao Qiang }
768287df01eSZhao Qiang 
769287df01eSZhao Qiang /* Find the header pointer to the Capabilities*/
770287df01eSZhao Qiang int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
771287df01eSZhao Qiang 			    u8 hdr_type)
772287df01eSZhao Qiang {
773287df01eSZhao Qiang 	u16 status;
774287df01eSZhao Qiang 
775287df01eSZhao Qiang 	pci_hose_read_config_word(hose, dev, PCI_STATUS, &status);
776287df01eSZhao Qiang 
777287df01eSZhao Qiang 	if (!(status & PCI_STATUS_CAP_LIST))
778287df01eSZhao Qiang 		return 0;
779287df01eSZhao Qiang 
780287df01eSZhao Qiang 	switch (hdr_type) {
781287df01eSZhao Qiang 	case PCI_HEADER_TYPE_NORMAL:
782287df01eSZhao Qiang 	case PCI_HEADER_TYPE_BRIDGE:
783287df01eSZhao Qiang 		return PCI_CAPABILITY_LIST;
784287df01eSZhao Qiang 	case PCI_HEADER_TYPE_CARDBUS:
785287df01eSZhao Qiang 		return PCI_CB_CAPABILITY_LIST;
786287df01eSZhao Qiang 	default:
787287df01eSZhao Qiang 		return 0;
788287df01eSZhao Qiang 	}
789287df01eSZhao Qiang }
790287df01eSZhao Qiang 
791287df01eSZhao Qiang int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap)
792287df01eSZhao Qiang {
793287df01eSZhao Qiang 	int ttl = PCI_FIND_CAP_TTL;
794287df01eSZhao Qiang 	u8 id;
795287df01eSZhao Qiang 	u8 next_pos;
796287df01eSZhao Qiang 
797287df01eSZhao Qiang 	while (ttl--) {
798287df01eSZhao Qiang 		pci_hose_read_config_byte(hose, dev, pos, &next_pos);
799287df01eSZhao Qiang 		if (next_pos < CAP_START_POS)
800287df01eSZhao Qiang 			break;
801287df01eSZhao Qiang 		next_pos &= ~3;
802287df01eSZhao Qiang 		pos = (int) next_pos;
803287df01eSZhao Qiang 		pci_hose_read_config_byte(hose, dev,
804287df01eSZhao Qiang 					  pos + PCI_CAP_LIST_ID, &id);
805287df01eSZhao Qiang 		if (id == 0xff)
806287df01eSZhao Qiang 			break;
807287df01eSZhao Qiang 		if (id == cap)
808287df01eSZhao Qiang 			return pos;
809287df01eSZhao Qiang 		pos += PCI_CAP_LIST_NEXT;
810287df01eSZhao Qiang 	}
811287df01eSZhao Qiang 	return 0;
812287df01eSZhao Qiang }
813