xref: /rk3399_rockchip-uboot/drivers/pci/pci.c (revision cf5787f2a4f1af71e72f4faaee32685c8f82a3dd)
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  *
893a686eeSJean-Christophe PLAGNIOL-VILLARD  * See file CREDITS for list of people who contributed to this
993a686eeSJean-Christophe PLAGNIOL-VILLARD  * project.
1093a686eeSJean-Christophe PLAGNIOL-VILLARD  *
1193a686eeSJean-Christophe PLAGNIOL-VILLARD  * This program is free software; you can redistribute it and/or
1293a686eeSJean-Christophe PLAGNIOL-VILLARD  * modify it under the terms of the GNU General Public License as
1393a686eeSJean-Christophe PLAGNIOL-VILLARD  * published by the Free Software Foundation; either version 2 of
1493a686eeSJean-Christophe PLAGNIOL-VILLARD  * the License, or (at your option) any later version.
1593a686eeSJean-Christophe PLAGNIOL-VILLARD  *
1693a686eeSJean-Christophe PLAGNIOL-VILLARD  * This program is distributed in the hope that it will be useful,
1793a686eeSJean-Christophe PLAGNIOL-VILLARD  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1893a686eeSJean-Christophe PLAGNIOL-VILLARD  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1993a686eeSJean-Christophe PLAGNIOL-VILLARD  * GNU General Public License for more details.
2093a686eeSJean-Christophe PLAGNIOL-VILLARD  *
2193a686eeSJean-Christophe PLAGNIOL-VILLARD  * You should have received a copy of the GNU General Public License
2293a686eeSJean-Christophe PLAGNIOL-VILLARD  * along with this program; if not, write to the Free Software
2393a686eeSJean-Christophe PLAGNIOL-VILLARD  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2493a686eeSJean-Christophe PLAGNIOL-VILLARD  * MA 02111-1307 USA
2593a686eeSJean-Christophe PLAGNIOL-VILLARD  */
2693a686eeSJean-Christophe PLAGNIOL-VILLARD 
2793a686eeSJean-Christophe PLAGNIOL-VILLARD /*
2893a686eeSJean-Christophe PLAGNIOL-VILLARD  * PCI routines
2993a686eeSJean-Christophe PLAGNIOL-VILLARD  */
3093a686eeSJean-Christophe PLAGNIOL-VILLARD 
3193a686eeSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
3293a686eeSJean-Christophe PLAGNIOL-VILLARD 
3393a686eeSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
3493a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/processor.h>
3593a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
3693a686eeSJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
3793a686eeSJean-Christophe PLAGNIOL-VILLARD 
3893a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_HOSE_OP(rw, size, type)					\
3993a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_##rw##_config_##size(struct pci_controller *hose,		\
4093a686eeSJean-Christophe PLAGNIOL-VILLARD 				  pci_dev_t dev,			\
4193a686eeSJean-Christophe PLAGNIOL-VILLARD 				  int offset, type value)		\
4293a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
4393a686eeSJean-Christophe PLAGNIOL-VILLARD 	return hose->rw##_##size(hose, dev, offset, value);		\
4493a686eeSJean-Christophe PLAGNIOL-VILLARD }
4593a686eeSJean-Christophe PLAGNIOL-VILLARD 
4693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, byte, u8 *)
4793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, word, u16 *)
4893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, dword, u32 *)
4993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, byte, u8)
5093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, word, u16)
5193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, dword, u32)
5293a686eeSJean-Christophe PLAGNIOL-VILLARD 
5393a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_OP(rw, size, type, error_code)				\
5493a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value)	\
5593a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
5693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev));	\
5793a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
5893a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (!hose)							\
5993a686eeSJean-Christophe PLAGNIOL-VILLARD 	{								\
6093a686eeSJean-Christophe PLAGNIOL-VILLARD 		error_code;						\
6193a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
6293a686eeSJean-Christophe PLAGNIOL-VILLARD 	}								\
6393a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
6493a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_##rw##_config_##size(hose, dev, offset, value);	\
6593a686eeSJean-Christophe PLAGNIOL-VILLARD }
6693a686eeSJean-Christophe PLAGNIOL-VILLARD 
6793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, byte, u8 *, *value = 0xff)
6893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, word, u16 *, *value = 0xffff)
6993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, dword, u32 *, *value = 0xffffffff)
7093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, byte, u8, )
7193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, word, u16, )
7293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, dword, u32, )
7393a686eeSJean-Christophe PLAGNIOL-VILLARD 
7493a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_READ_VIA_DWORD_OP(size, type, off_mask)			\
7593a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
7693a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_dev_t dev,			\
7793a686eeSJean-Christophe PLAGNIOL-VILLARD 					int offset, type val)		\
7893a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
7993a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 val32;							\
8093a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8193a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) {	\
8293a686eeSJean-Christophe PLAGNIOL-VILLARD 		*val = -1;						\
8393a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
8493a686eeSJean-Christophe PLAGNIOL-VILLARD 	}								\
8593a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8693a686eeSJean-Christophe PLAGNIOL-VILLARD 	*val = (val32 >> ((offset & (int)off_mask) * 8));		\
8793a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8893a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;							\
8993a686eeSJean-Christophe PLAGNIOL-VILLARD }
9093a686eeSJean-Christophe PLAGNIOL-VILLARD 
9193a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask)		\
9293a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
9393a686eeSJean-Christophe PLAGNIOL-VILLARD 					     pci_dev_t dev,		\
9493a686eeSJean-Christophe PLAGNIOL-VILLARD 					     int offset, type val)	\
9593a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
9693a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 val32, mask, ldata, shift;					\
9793a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
9893a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
9993a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
10093a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
10193a686eeSJean-Christophe PLAGNIOL-VILLARD 	shift = ((offset & (int)off_mask) * 8);				\
10293a686eeSJean-Christophe PLAGNIOL-VILLARD 	ldata = (((unsigned long)val) & val_mask) << shift;		\
10393a686eeSJean-Christophe PLAGNIOL-VILLARD 	mask = val_mask << shift;					\
10493a686eeSJean-Christophe PLAGNIOL-VILLARD 	val32 = (val32 & ~mask) | ldata;				\
10593a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
10693a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
10793a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
10893a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
10993a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;							\
11093a686eeSJean-Christophe PLAGNIOL-VILLARD }
11193a686eeSJean-Christophe PLAGNIOL-VILLARD 
11293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
11393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
11493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
11593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
11693a686eeSJean-Christophe PLAGNIOL-VILLARD 
1176e61fae4SBecky Bruce /* Get a virtual address associated with a BAR region */
1186e61fae4SBecky Bruce void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
1196e61fae4SBecky Bruce {
1206e61fae4SBecky Bruce 	pci_addr_t pci_bus_addr;
121*cf5787f2SKumar Gala 	u32 bar_response;
1226e61fae4SBecky Bruce 
1236e61fae4SBecky Bruce 	/* read BAR address */
1246e61fae4SBecky Bruce 	pci_read_config_dword(pdev, bar, &bar_response);
125*cf5787f2SKumar Gala 	pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1266e61fae4SBecky Bruce 
1276e61fae4SBecky Bruce 	/*
1286e61fae4SBecky Bruce 	 * Pass "0" as the length argument to pci_bus_to_virt.  The arg
1296e61fae4SBecky Bruce 	 * isn't actualy used on any platform because u-boot assumes a static
1306e61fae4SBecky Bruce 	 * linear mapping.  In the future, this could read the BAR size
1316e61fae4SBecky Bruce 	 * and pass that as the size if needed.
1326e61fae4SBecky Bruce 	 */
1336e61fae4SBecky Bruce 	return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1346e61fae4SBecky Bruce }
1356e61fae4SBecky Bruce 
13693a686eeSJean-Christophe PLAGNIOL-VILLARD /*
13793a686eeSJean-Christophe PLAGNIOL-VILLARD  *
13893a686eeSJean-Christophe PLAGNIOL-VILLARD  */
13993a686eeSJean-Christophe PLAGNIOL-VILLARD 
14096d61603SJohn Schmoller static struct pci_controller* hose_head;
14193a686eeSJean-Christophe PLAGNIOL-VILLARD 
14293a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose)
14393a686eeSJean-Christophe PLAGNIOL-VILLARD {
14493a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller **phose = &hose_head;
14593a686eeSJean-Christophe PLAGNIOL-VILLARD 
14693a686eeSJean-Christophe PLAGNIOL-VILLARD 	while(*phose)
14793a686eeSJean-Christophe PLAGNIOL-VILLARD 		phose = &(*phose)->next;
14893a686eeSJean-Christophe PLAGNIOL-VILLARD 
14993a686eeSJean-Christophe PLAGNIOL-VILLARD 	hose->next = NULL;
15093a686eeSJean-Christophe PLAGNIOL-VILLARD 
15193a686eeSJean-Christophe PLAGNIOL-VILLARD 	*phose = hose;
15293a686eeSJean-Christophe PLAGNIOL-VILLARD }
15393a686eeSJean-Christophe PLAGNIOL-VILLARD 
15493a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose(int bus)
15593a686eeSJean-Christophe PLAGNIOL-VILLARD {
15693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose;
15793a686eeSJean-Christophe PLAGNIOL-VILLARD 
158cb2bf931SAndrew Sharp 	for (hose = hose_head; hose; hose = hose->next) {
15993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus >= hose->first_busno && bus <= hose->last_busno)
16093a686eeSJean-Christophe PLAGNIOL-VILLARD 			return hose;
161cb2bf931SAndrew Sharp 	}
16293a686eeSJean-Christophe PLAGNIOL-VILLARD 
16393a686eeSJean-Christophe PLAGNIOL-VILLARD 	printf("pci_bus_to_hose() failed\n");
16493a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
16593a686eeSJean-Christophe PLAGNIOL-VILLARD }
16693a686eeSJean-Christophe PLAGNIOL-VILLARD 
1673a0e3c27SKumar Gala struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
1683a0e3c27SKumar Gala {
1693a0e3c27SKumar Gala 	struct pci_controller *hose;
1703a0e3c27SKumar Gala 
1713a0e3c27SKumar Gala 	for (hose = hose_head; hose; hose = hose->next) {
1723a0e3c27SKumar Gala 		if (hose->cfg_addr == cfg_addr)
1733a0e3c27SKumar Gala 			return hose;
1743a0e3c27SKumar Gala 	}
1753a0e3c27SKumar Gala 
1763a0e3c27SKumar Gala 	return NULL;
1773a0e3c27SKumar Gala }
1783a0e3c27SKumar Gala 
179cc2a8c77SAnton Vorontsov int pci_last_busno(void)
180cc2a8c77SAnton Vorontsov {
181cc2a8c77SAnton Vorontsov 	struct pci_controller *hose = hose_head;
182cc2a8c77SAnton Vorontsov 
183cc2a8c77SAnton Vorontsov 	if (!hose)
184cc2a8c77SAnton Vorontsov 		return -1;
185cc2a8c77SAnton Vorontsov 
186cc2a8c77SAnton Vorontsov 	while (hose->next)
187cc2a8c77SAnton Vorontsov 		hose = hose->next;
188cc2a8c77SAnton Vorontsov 
189cc2a8c77SAnton Vorontsov 	return hose->last_busno;
190cc2a8c77SAnton Vorontsov }
191cc2a8c77SAnton Vorontsov 
19293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
19393a686eeSJean-Christophe PLAGNIOL-VILLARD {
19493a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller * hose;
19593a686eeSJean-Christophe PLAGNIOL-VILLARD 	u16 vendor, device;
19693a686eeSJean-Christophe PLAGNIOL-VILLARD 	u8 header_type;
19793a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t bdf;
19893a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i, bus, found_multi = 0;
19993a686eeSJean-Christophe PLAGNIOL-VILLARD 
200cb2bf931SAndrew Sharp 	for (hose = hose_head; hose; hose = hose->next) {
2016d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
20293a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
20393a686eeSJean-Christophe PLAGNIOL-VILLARD #else
20493a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
20593a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
20693a686eeSJean-Christophe PLAGNIOL-VILLARD 			for (bdf = PCI_BDF(bus, 0, 0);
20793a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
208cb2bf931SAndrew Sharp 			     bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
209cb2bf931SAndrew Sharp 				PCI_MAX_PCI_FUNCTIONS - 1);
21093a686eeSJean-Christophe PLAGNIOL-VILLARD #else
21193a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf < PCI_BDF(bus + 1, 0, 0);
21293a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
213cb2bf931SAndrew Sharp 			     bdf += PCI_BDF(0, 0, 1)) {
21493a686eeSJean-Christophe PLAGNIOL-VILLARD 				if (!PCI_FUNC(bdf)) {
21593a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_read_config_byte(bdf,
21693a686eeSJean-Christophe PLAGNIOL-VILLARD 							     PCI_HEADER_TYPE,
21793a686eeSJean-Christophe PLAGNIOL-VILLARD 							     &header_type);
21893a686eeSJean-Christophe PLAGNIOL-VILLARD 
21993a686eeSJean-Christophe PLAGNIOL-VILLARD 					found_multi = header_type & 0x80;
22093a686eeSJean-Christophe PLAGNIOL-VILLARD 				} else {
22193a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (!found_multi)
22293a686eeSJean-Christophe PLAGNIOL-VILLARD 						continue;
22393a686eeSJean-Christophe PLAGNIOL-VILLARD 				}
22493a686eeSJean-Christophe PLAGNIOL-VILLARD 
22593a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
22693a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_VENDOR_ID,
22793a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &vendor);
22893a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
22993a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_DEVICE_ID,
23093a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &device);
23193a686eeSJean-Christophe PLAGNIOL-VILLARD 
232cb2bf931SAndrew Sharp 				for (i = 0; ids[i].vendor != 0; i++) {
23393a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (vendor == ids[i].vendor &&
234cb2bf931SAndrew Sharp 					    device == ids[i].device) {
23593a686eeSJean-Christophe PLAGNIOL-VILLARD 						if (index <= 0)
23693a686eeSJean-Christophe PLAGNIOL-VILLARD 							return bdf;
23793a686eeSJean-Christophe PLAGNIOL-VILLARD 
23893a686eeSJean-Christophe PLAGNIOL-VILLARD 						index--;
23993a686eeSJean-Christophe PLAGNIOL-VILLARD 					}
24093a686eeSJean-Christophe PLAGNIOL-VILLARD 				}
24193a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
242cb2bf931SAndrew Sharp 	}
24393a686eeSJean-Christophe PLAGNIOL-VILLARD 
244cb2bf931SAndrew Sharp 	return -1;
24593a686eeSJean-Christophe PLAGNIOL-VILLARD }
24693a686eeSJean-Christophe PLAGNIOL-VILLARD 
24793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
24893a686eeSJean-Christophe PLAGNIOL-VILLARD {
24993a686eeSJean-Christophe PLAGNIOL-VILLARD 	static struct pci_device_id ids[2] = {{}, {0, 0}};
25093a686eeSJean-Christophe PLAGNIOL-VILLARD 
25193a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].vendor = vendor;
25293a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].device = device;
25393a686eeSJean-Christophe PLAGNIOL-VILLARD 
25493a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_find_devices(ids, index);
25593a686eeSJean-Christophe PLAGNIOL-VILLARD }
25693a686eeSJean-Christophe PLAGNIOL-VILLARD 
25793a686eeSJean-Christophe PLAGNIOL-VILLARD /*
25893a686eeSJean-Christophe PLAGNIOL-VILLARD  *
25993a686eeSJean-Christophe PLAGNIOL-VILLARD  */
26093a686eeSJean-Christophe PLAGNIOL-VILLARD 
2612d43e873SKumar Gala int __pci_hose_phys_to_bus(struct pci_controller *hose,
26236f32675SBecky Bruce 				phys_addr_t phys_addr,
2632d43e873SKumar Gala 				unsigned long flags,
2642d43e873SKumar Gala 				unsigned long skip_mask,
2652d43e873SKumar Gala 				pci_addr_t *ba)
26693a686eeSJean-Christophe PLAGNIOL-VILLARD {
26793a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
26830e76d5eSKumar Gala 	pci_addr_t bus_addr;
26993a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
27093a686eeSJean-Christophe PLAGNIOL-VILLARD 
27193a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
27293a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
27393a686eeSJean-Christophe PLAGNIOL-VILLARD 
27493a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
27593a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
27693a686eeSJean-Christophe PLAGNIOL-VILLARD 
2772d43e873SKumar Gala 		if (res->flags & skip_mask)
2782d43e873SKumar Gala 			continue;
2792d43e873SKumar Gala 
28093a686eeSJean-Christophe PLAGNIOL-VILLARD 		bus_addr = phys_addr - res->phys_start + res->bus_start;
28193a686eeSJean-Christophe PLAGNIOL-VILLARD 
28293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
28393a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
2842d43e873SKumar Gala 			*ba = bus_addr;
28593a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
28693a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
2872d43e873SKumar Gala 	}
28893a686eeSJean-Christophe PLAGNIOL-VILLARD 
2892d43e873SKumar Gala 	return 1;
2902d43e873SKumar Gala }
2912d43e873SKumar Gala 
2922d43e873SKumar Gala pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
2932d43e873SKumar Gala 				    phys_addr_t phys_addr,
29493a686eeSJean-Christophe PLAGNIOL-VILLARD 				    unsigned long flags)
29593a686eeSJean-Christophe PLAGNIOL-VILLARD {
2962d43e873SKumar Gala 	pci_addr_t bus_addr = 0;
2972d43e873SKumar Gala 	int ret;
2982d43e873SKumar Gala 
2992d43e873SKumar Gala 	if (!hose) {
3002d43e873SKumar Gala 		puts("pci_hose_phys_to_bus: invalid hose\n");
3012d43e873SKumar Gala 		return bus_addr;
3022d43e873SKumar Gala 	}
3032d43e873SKumar Gala 
304cb2bf931SAndrew Sharp 	/*
305cb2bf931SAndrew Sharp 	 * if PCI_REGION_MEM is set we do a two pass search with preference
306cb2bf931SAndrew Sharp 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
307cb2bf931SAndrew Sharp 	 */
3082d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
3092d43e873SKumar Gala 		ret = __pci_hose_phys_to_bus(hose, phys_addr,
3102d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &bus_addr);
3112d43e873SKumar Gala 		if (!ret)
3122d43e873SKumar Gala 			return bus_addr;
3132d43e873SKumar Gala 	}
3142d43e873SKumar Gala 
3152d43e873SKumar Gala 	ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
3162d43e873SKumar Gala 
3172d43e873SKumar Gala 	if (ret)
3182d43e873SKumar Gala 		puts("pci_hose_phys_to_bus: invalid physical address\n");
3192d43e873SKumar Gala 
3202d43e873SKumar Gala 	return bus_addr;
3212d43e873SKumar Gala }
3222d43e873SKumar Gala 
3232d43e873SKumar Gala int __pci_hose_bus_to_phys(struct pci_controller *hose,
3242d43e873SKumar Gala 				pci_addr_t bus_addr,
3252d43e873SKumar Gala 				unsigned long flags,
3262d43e873SKumar Gala 				unsigned long skip_mask,
3272d43e873SKumar Gala 				phys_addr_t *pa)
3282d43e873SKumar Gala {
32993a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
33093a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
33193a686eeSJean-Christophe PLAGNIOL-VILLARD 
33293a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
33393a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
33493a686eeSJean-Christophe PLAGNIOL-VILLARD 
33593a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
33693a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
33793a686eeSJean-Christophe PLAGNIOL-VILLARD 
3382d43e873SKumar Gala 		if (res->flags & skip_mask)
3392d43e873SKumar Gala 			continue;
3402d43e873SKumar Gala 
34193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
34293a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
3432d43e873SKumar Gala 			*pa = (bus_addr - res->bus_start + res->phys_start);
34493a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
34593a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
3462d43e873SKumar Gala 	}
3472d43e873SKumar Gala 
3482d43e873SKumar Gala 	return 1;
3492d43e873SKumar Gala }
3502d43e873SKumar Gala 
3512d43e873SKumar Gala phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
3522d43e873SKumar Gala 				 pci_addr_t bus_addr,
3532d43e873SKumar Gala 				 unsigned long flags)
3542d43e873SKumar Gala {
3552d43e873SKumar Gala 	phys_addr_t phys_addr = 0;
3562d43e873SKumar Gala 	int ret;
3572d43e873SKumar Gala 
3582d43e873SKumar Gala 	if (!hose) {
3592d43e873SKumar Gala 		puts("pci_hose_bus_to_phys: invalid hose\n");
3602d43e873SKumar Gala 		return phys_addr;
3612d43e873SKumar Gala 	}
3622d43e873SKumar Gala 
363cb2bf931SAndrew Sharp 	/*
364cb2bf931SAndrew Sharp 	 * if PCI_REGION_MEM is set we do a two pass search with preference
365cb2bf931SAndrew Sharp 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
366cb2bf931SAndrew Sharp 	 */
3672d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
3682d43e873SKumar Gala 		ret = __pci_hose_bus_to_phys(hose, bus_addr,
3692d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &phys_addr);
3702d43e873SKumar Gala 		if (!ret)
3712d43e873SKumar Gala 			return phys_addr;
3722d43e873SKumar Gala 	}
3732d43e873SKumar Gala 
3742d43e873SKumar Gala 	ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
3752d43e873SKumar Gala 
3762d43e873SKumar Gala 	if (ret)
3772d43e873SKumar Gala 		puts("pci_hose_bus_to_phys: invalid physical address\n");
3782d43e873SKumar Gala 
3792d43e873SKumar Gala 	return phys_addr;
3802d43e873SKumar Gala }
38193a686eeSJean-Christophe PLAGNIOL-VILLARD 
38293a686eeSJean-Christophe PLAGNIOL-VILLARD /*
38393a686eeSJean-Christophe PLAGNIOL-VILLARD  *
38493a686eeSJean-Christophe PLAGNIOL-VILLARD  */
38593a686eeSJean-Christophe PLAGNIOL-VILLARD 
38693a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_config_device(struct pci_controller *hose,
38793a686eeSJean-Christophe PLAGNIOL-VILLARD 			   pci_dev_t dev,
38893a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long io,
38930e76d5eSKumar Gala 			   pci_addr_t mem,
39093a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long command)
39193a686eeSJean-Christophe PLAGNIOL-VILLARD {
392*cf5787f2SKumar Gala 	u32 bar_response;
393af778c6dSAndrew Sharp 	unsigned int old_command;
39430e76d5eSKumar Gala 	pci_addr_t bar_value;
39530e76d5eSKumar Gala 	pci_size_t bar_size;
39693a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char pin;
39793a686eeSJean-Christophe PLAGNIOL-VILLARD 	int bar, found_mem64;
39893a686eeSJean-Christophe PLAGNIOL-VILLARD 
399cb2bf931SAndrew Sharp 	debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
400cb2bf931SAndrew Sharp 		(u64)mem, command);
40193a686eeSJean-Christophe PLAGNIOL-VILLARD 
40293a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
40393a686eeSJean-Christophe PLAGNIOL-VILLARD 
404252b404dSWolfgang Denk 	for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
40593a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
40693a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_dword(hose, dev, bar, &bar_response);
40793a686eeSJean-Christophe PLAGNIOL-VILLARD 
40893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!bar_response)
40993a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
41093a686eeSJean-Christophe PLAGNIOL-VILLARD 
41193a686eeSJean-Christophe PLAGNIOL-VILLARD 		found_mem64 = 0;
41293a686eeSJean-Christophe PLAGNIOL-VILLARD 
41393a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Check the BAR type and set our address mask */
41493a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bar_response & PCI_BASE_ADDRESS_SPACE) {
41593a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
41693a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to a multiple of size */
41793a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = ((io - 1) | (bar_size - 1)) + 1;
41893a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = io;
41993a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
42093a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = io + bar_size;
42193a686eeSJean-Christophe PLAGNIOL-VILLARD 		} else {
42293a686eeSJean-Christophe PLAGNIOL-VILLARD 			if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
42330e76d5eSKumar Gala 				PCI_BASE_ADDRESS_MEM_TYPE_64) {
42430e76d5eSKumar Gala 				u32 bar_response_upper;
42530e76d5eSKumar Gala 				u64 bar64;
426cb2bf931SAndrew Sharp 				pci_hose_write_config_dword(hose, dev, bar + 4,
427cb2bf931SAndrew Sharp 					0xffffffff);
428cb2bf931SAndrew Sharp 				pci_hose_read_config_dword(hose, dev, bar + 4,
429cb2bf931SAndrew Sharp 					&bar_response_upper);
43093a686eeSJean-Christophe PLAGNIOL-VILLARD 
43130e76d5eSKumar Gala 				bar64 = ((u64)bar_response_upper << 32) | bar_response;
43230e76d5eSKumar Gala 
43330e76d5eSKumar Gala 				bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
43430e76d5eSKumar Gala 				found_mem64 = 1;
43530e76d5eSKumar Gala 			} else {
43630e76d5eSKumar Gala 				bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
43730e76d5eSKumar Gala 			}
43893a686eeSJean-Christophe PLAGNIOL-VILLARD 
43993a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to multiple of size */
44093a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = ((mem - 1) | (bar_size - 1)) + 1;
44193a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = mem;
44293a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
44393a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = mem + bar_size;
44493a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
44593a686eeSJean-Christophe PLAGNIOL-VILLARD 
44693a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Write it out and update our limit */
44730e76d5eSKumar Gala 		pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
44893a686eeSJean-Christophe PLAGNIOL-VILLARD 
44993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (found_mem64) {
45093a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar += 4;
45130e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
452cb2bf931SAndrew Sharp 			pci_hose_write_config_dword(hose, dev, bar,
453cb2bf931SAndrew Sharp 				(u32)(bar_value >> 32));
45430e76d5eSKumar Gala #else
45593a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
45630e76d5eSKumar Gala #endif
45793a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
45893a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
45993a686eeSJean-Christophe PLAGNIOL-VILLARD 
46093a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Cache Line Size Register */
46193a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
46293a686eeSJean-Christophe PLAGNIOL-VILLARD 
46393a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Latency Timer */
46493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
46593a686eeSJean-Christophe PLAGNIOL-VILLARD 
46693a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Disable interrupt line, if device says it wants to use interrupts */
46793a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
46893a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pin != 0) {
46993a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff);
47093a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
47193a686eeSJean-Christophe PLAGNIOL-VILLARD 
47293a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
47393a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
47493a686eeSJean-Christophe PLAGNIOL-VILLARD 				     (old_command & 0xffff0000) | command);
47593a686eeSJean-Christophe PLAGNIOL-VILLARD 
47693a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;
47793a686eeSJean-Christophe PLAGNIOL-VILLARD }
47893a686eeSJean-Christophe PLAGNIOL-VILLARD 
47993a686eeSJean-Christophe PLAGNIOL-VILLARD /*
48093a686eeSJean-Christophe PLAGNIOL-VILLARD  *
48193a686eeSJean-Christophe PLAGNIOL-VILLARD  */
48293a686eeSJean-Christophe PLAGNIOL-VILLARD 
48393a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose,
48493a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned short class,
48593a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int vendor,
48693a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int device,
48793a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int bus,
48893a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int dev,
48993a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int func)
49093a686eeSJean-Christophe PLAGNIOL-VILLARD {
49193a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *table;
49293a686eeSJean-Christophe PLAGNIOL-VILLARD 
49393a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (table = hose->config_table; table && table->vendor; table++) {
49493a686eeSJean-Christophe PLAGNIOL-VILLARD 		if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
49593a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->device == PCI_ANY_ID || table->device == device) &&
49693a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->class  == PCI_ANY_ID || table->class  == class)  &&
49793a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->bus    == PCI_ANY_ID || table->bus    == bus)    &&
49893a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->dev    == PCI_ANY_ID || table->dev    == dev)    &&
49993a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->func   == PCI_ANY_ID || table->func   == func)) {
50093a686eeSJean-Christophe PLAGNIOL-VILLARD 			return table;
50193a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
50293a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
50393a686eeSJean-Christophe PLAGNIOL-VILLARD 
50493a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
50593a686eeSJean-Christophe PLAGNIOL-VILLARD }
50693a686eeSJean-Christophe PLAGNIOL-VILLARD 
50793a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose,
50893a686eeSJean-Christophe PLAGNIOL-VILLARD 			       pci_dev_t dev,
50993a686eeSJean-Christophe PLAGNIOL-VILLARD 			       struct pci_config_table *entry)
51093a686eeSJean-Christophe PLAGNIOL-VILLARD {
511cb2bf931SAndrew Sharp 	pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
512cb2bf931SAndrew Sharp 		entry->priv[2]);
51393a686eeSJean-Christophe PLAGNIOL-VILLARD }
51493a686eeSJean-Christophe PLAGNIOL-VILLARD 
51593a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose,
51693a686eeSJean-Christophe PLAGNIOL-VILLARD 			    pci_dev_t dev, struct pci_config_table *entry)
51793a686eeSJean-Christophe PLAGNIOL-VILLARD {
51893a686eeSJean-Christophe PLAGNIOL-VILLARD }
51993a686eeSJean-Christophe PLAGNIOL-VILLARD 
52093a686eeSJean-Christophe PLAGNIOL-VILLARD /*
521cb2bf931SAndrew Sharp  * HJF: Changed this to return int. I think this is required
52293a686eeSJean-Christophe PLAGNIOL-VILLARD  * to get the correct result when scanning bridges
52393a686eeSJean-Christophe PLAGNIOL-VILLARD  */
52493a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
52593a686eeSJean-Christophe PLAGNIOL-VILLARD 
526983eb9d1SPeter Tyser #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW)
527983eb9d1SPeter Tyser const char * pci_class_str(u8 class)
528983eb9d1SPeter Tyser {
529983eb9d1SPeter Tyser 	switch (class) {
530983eb9d1SPeter Tyser 	case PCI_CLASS_NOT_DEFINED:
531983eb9d1SPeter Tyser 		return "Build before PCI Rev2.0";
532983eb9d1SPeter Tyser 		break;
533983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_STORAGE:
534983eb9d1SPeter Tyser 		return "Mass storage controller";
535983eb9d1SPeter Tyser 		break;
536983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_NETWORK:
537983eb9d1SPeter Tyser 		return "Network controller";
538983eb9d1SPeter Tyser 		break;
539983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_DISPLAY:
540983eb9d1SPeter Tyser 		return "Display controller";
541983eb9d1SPeter Tyser 		break;
542983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_MULTIMEDIA:
543983eb9d1SPeter Tyser 		return "Multimedia device";
544983eb9d1SPeter Tyser 		break;
545983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_MEMORY:
546983eb9d1SPeter Tyser 		return "Memory controller";
547983eb9d1SPeter Tyser 		break;
548983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_BRIDGE:
549983eb9d1SPeter Tyser 		return "Bridge device";
550983eb9d1SPeter Tyser 		break;
551983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_COMMUNICATION:
552983eb9d1SPeter Tyser 		return "Simple comm. controller";
553983eb9d1SPeter Tyser 		break;
554983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SYSTEM:
555983eb9d1SPeter Tyser 		return "Base system peripheral";
556983eb9d1SPeter Tyser 		break;
557983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_INPUT:
558983eb9d1SPeter Tyser 		return "Input device";
559983eb9d1SPeter Tyser 		break;
560983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_DOCKING:
561983eb9d1SPeter Tyser 		return "Docking station";
562983eb9d1SPeter Tyser 		break;
563983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_PROCESSOR:
564983eb9d1SPeter Tyser 		return "Processor";
565983eb9d1SPeter Tyser 		break;
566983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SERIAL:
567983eb9d1SPeter Tyser 		return "Serial bus controller";
568983eb9d1SPeter Tyser 		break;
569983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_INTELLIGENT:
570983eb9d1SPeter Tyser 		return "Intelligent controller";
571983eb9d1SPeter Tyser 		break;
572983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SATELLITE:
573983eb9d1SPeter Tyser 		return "Satellite controller";
574983eb9d1SPeter Tyser 		break;
575983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_CRYPT:
576983eb9d1SPeter Tyser 		return "Cryptographic device";
577983eb9d1SPeter Tyser 		break;
578983eb9d1SPeter Tyser 	case PCI_BASE_CLASS_SIGNAL_PROCESSING:
579983eb9d1SPeter Tyser 		return "DSP";
580983eb9d1SPeter Tyser 		break;
581983eb9d1SPeter Tyser 	case PCI_CLASS_OTHERS:
582983eb9d1SPeter Tyser 		return "Does not fit any class";
583983eb9d1SPeter Tyser 		break;
584983eb9d1SPeter Tyser 	default:
585983eb9d1SPeter Tyser 	return  "???";
586983eb9d1SPeter Tyser 		break;
587983eb9d1SPeter Tyser 	};
588983eb9d1SPeter Tyser }
589983eb9d1SPeter Tyser #endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */
590983eb9d1SPeter Tyser 
591dc1da42fSStefan Roese int __pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
592dc1da42fSStefan Roese {
593dc1da42fSStefan Roese 	/*
594dc1da42fSStefan Roese 	 * Check if pci device should be skipped in configuration
595dc1da42fSStefan Roese 	 */
596dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
597dc1da42fSStefan Roese #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
598dc1da42fSStefan Roese 		/*
599dc1da42fSStefan Roese 		 * Only skip configuration if "pciconfighost" is not set
600dc1da42fSStefan Roese 		 */
601dc1da42fSStefan Roese 		if (getenv("pciconfighost") == NULL)
602dc1da42fSStefan Roese 			return 1;
603dc1da42fSStefan Roese #else
604dc1da42fSStefan Roese 		return 1;
605dc1da42fSStefan Roese #endif
606dc1da42fSStefan Roese 	}
607dc1da42fSStefan Roese 
608dc1da42fSStefan Roese 	return 0;
609dc1da42fSStefan Roese }
610dc1da42fSStefan Roese int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
611dc1da42fSStefan Roese 	__attribute__((weak, alias("__pci_skip_dev")));
612dc1da42fSStefan Roese 
613dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW
614dc1da42fSStefan Roese int __pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
615dc1da42fSStefan Roese {
616dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0))
617dc1da42fSStefan Roese 		return 0;
618dc1da42fSStefan Roese 
619dc1da42fSStefan Roese 	return 1;
620dc1da42fSStefan Roese }
621dc1da42fSStefan Roese int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
622dc1da42fSStefan Roese 	__attribute__((weak, alias("__pci_print_dev")));
623dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */
624dc1da42fSStefan Roese 
62593a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus)
62693a686eeSJean-Christophe PLAGNIOL-VILLARD {
62793a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned int sub_bus, found_multi = 0;
62893a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned short vendor, device, class;
62993a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char header_type;
63003992ac2SAndrew Sharp #ifndef CONFIG_PCI_PNP
63193a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *cfg;
63203992ac2SAndrew Sharp #endif
63393a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t dev;
634009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
635009884aeSPeter Tyser 	static int indent = 0;
636009884aeSPeter Tyser #endif
63793a686eeSJean-Christophe PLAGNIOL-VILLARD 
63893a686eeSJean-Christophe PLAGNIOL-VILLARD 	sub_bus = bus;
63993a686eeSJean-Christophe PLAGNIOL-VILLARD 
64093a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (dev =  PCI_BDF(bus,0,0);
641cb2bf931SAndrew Sharp 	     dev <  PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
642cb2bf931SAndrew Sharp 				PCI_MAX_PCI_FUNCTIONS - 1);
643dc1da42fSStefan Roese 	     dev += PCI_BDF(0, 0, 1)) {
644dc1da42fSStefan Roese 
645dc1da42fSStefan Roese 		if (pci_skip_dev(hose, dev))
646dc1da42fSStefan Roese 			continue;
64793a686eeSJean-Christophe PLAGNIOL-VILLARD 
64893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (PCI_FUNC(dev) && !found_multi)
64993a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
65093a686eeSJean-Christophe PLAGNIOL-VILLARD 
65193a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
65293a686eeSJean-Christophe PLAGNIOL-VILLARD 
65393a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
65493a686eeSJean-Christophe PLAGNIOL-VILLARD 
655983eb9d1SPeter Tyser 		if (vendor == 0xffff || vendor == 0x0000)
656983eb9d1SPeter Tyser 			continue;
65793a686eeSJean-Christophe PLAGNIOL-VILLARD 
65893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!PCI_FUNC(dev))
65993a686eeSJean-Christophe PLAGNIOL-VILLARD 			found_multi = header_type & 0x80;
66093a686eeSJean-Christophe PLAGNIOL-VILLARD 
66193a686eeSJean-Christophe PLAGNIOL-VILLARD 		debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
66293a686eeSJean-Christophe PLAGNIOL-VILLARD 			PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
66393a686eeSJean-Christophe PLAGNIOL-VILLARD 
66493a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
66593a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
66693a686eeSJean-Christophe PLAGNIOL-VILLARD 
667a38d216eSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
668009884aeSPeter Tyser 		indent++;
669009884aeSPeter Tyser 
670009884aeSPeter Tyser 		/* Print leading space, including bus indentation */
671009884aeSPeter Tyser 		printf("%*c", indent + 1, ' ');
672009884aeSPeter Tyser 
673a38d216eSPeter Tyser 		if (pci_print_dev(hose, dev)) {
674009884aeSPeter Tyser 			printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
675009884aeSPeter Tyser 			       PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
676a38d216eSPeter Tyser 			       vendor, device, pci_class_str(class >> 8));
677a38d216eSPeter Tyser 		}
678a38d216eSPeter Tyser #endif
679a38d216eSPeter Tyser 
68003992ac2SAndrew Sharp #ifdef CONFIG_PCI_PNP
68103992ac2SAndrew Sharp 		sub_bus = max(pciauto_config_device(hose, dev), sub_bus);
68203992ac2SAndrew Sharp #else
68393a686eeSJean-Christophe PLAGNIOL-VILLARD 		cfg = pci_find_config(hose, class, vendor, device,
68493a686eeSJean-Christophe PLAGNIOL-VILLARD 				      PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
68593a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (cfg) {
68693a686eeSJean-Christophe PLAGNIOL-VILLARD 			cfg->config_device(hose, dev, cfg);
68793a686eeSJean-Christophe PLAGNIOL-VILLARD 			sub_bus = max(sub_bus, hose->current_busno);
68893a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
68903992ac2SAndrew Sharp #endif
690a38d216eSPeter Tyser 
691009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
692009884aeSPeter Tyser 		indent--;
693009884aeSPeter Tyser #endif
694009884aeSPeter Tyser 
69593a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (hose->fixup_irq)
69693a686eeSJean-Christophe PLAGNIOL-VILLARD 			hose->fixup_irq(hose, dev);
69793a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
69893a686eeSJean-Christophe PLAGNIOL-VILLARD 
69993a686eeSJean-Christophe PLAGNIOL-VILLARD 	return sub_bus;
70093a686eeSJean-Christophe PLAGNIOL-VILLARD }
70193a686eeSJean-Christophe PLAGNIOL-VILLARD 
70293a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose)
70393a686eeSJean-Christophe PLAGNIOL-VILLARD {
7040da1fb03SAnatolij Gustschin #if defined(CONFIG_PCI_BOOTDELAY)
7050da1fb03SAnatolij Gustschin 	static int pcidelay_done;
7060da1fb03SAnatolij Gustschin 	char *s;
7070da1fb03SAnatolij Gustschin 	int i;
7080da1fb03SAnatolij Gustschin 
7090da1fb03SAnatolij Gustschin 	if (!pcidelay_done) {
7100da1fb03SAnatolij Gustschin 		/* wait "pcidelay" ms (if defined)... */
7110da1fb03SAnatolij Gustschin 		s = getenv("pcidelay");
7120da1fb03SAnatolij Gustschin 		if (s) {
7130da1fb03SAnatolij Gustschin 			int val = simple_strtoul(s, NULL, 10);
7140da1fb03SAnatolij Gustschin 			for (i = 0; i < val; i++)
7150da1fb03SAnatolij Gustschin 				udelay(1000);
7160da1fb03SAnatolij Gustschin 		}
7170da1fb03SAnatolij Gustschin 		pcidelay_done = 1;
7180da1fb03SAnatolij Gustschin 	}
7190da1fb03SAnatolij Gustschin #endif /* CONFIG_PCI_BOOTDELAY */
7200da1fb03SAnatolij Gustschin 
721cb2bf931SAndrew Sharp 	/*
722cb2bf931SAndrew Sharp 	 * Start scan at current_busno.
72393a686eeSJean-Christophe PLAGNIOL-VILLARD 	 * PCIe will start scan at first_busno+1.
72493a686eeSJean-Christophe PLAGNIOL-VILLARD 	 */
72593a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* For legacy support, ensure current >= first */
72693a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (hose->first_busno > hose->current_busno)
72793a686eeSJean-Christophe PLAGNIOL-VILLARD 		hose->current_busno = hose->first_busno;
72893a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
72993a686eeSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_init(hose);
73093a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
73193a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_scan_bus(hose, hose->current_busno);
73293a686eeSJean-Christophe PLAGNIOL-VILLARD }
73393a686eeSJean-Christophe PLAGNIOL-VILLARD 
73493a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void)
73593a686eeSJean-Christophe PLAGNIOL-VILLARD {
73696d61603SJohn Schmoller 	hose_head = NULL;
73796d61603SJohn Schmoller 
73893a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* now call board specific pci_init()... */
73993a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_init_board();
74093a686eeSJean-Christophe PLAGNIOL-VILLARD }
741