xref: /rk3399_rockchip-uboot/drivers/pci/pci.c (revision 6e61fae4d360a1380b63e7d007b31477e366bcce)
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 #ifndef CONFIG_IXP425
5493a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_OP(rw, size, type, error_code)				\
5593a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value)	\
5693a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
5793a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev));	\
5893a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
5993a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (!hose)							\
6093a686eeSJean-Christophe PLAGNIOL-VILLARD 	{								\
6193a686eeSJean-Christophe PLAGNIOL-VILLARD 		error_code;						\
6293a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
6393a686eeSJean-Christophe PLAGNIOL-VILLARD 	}								\
6493a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
6593a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_##rw##_config_##size(hose, dev, offset, value);	\
6693a686eeSJean-Christophe PLAGNIOL-VILLARD }
6793a686eeSJean-Christophe PLAGNIOL-VILLARD 
6893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, byte, u8 *, *value = 0xff)
6993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, word, u16 *, *value = 0xffff)
7093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, dword, u32 *, *value = 0xffffffff)
7193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, byte, u8, )
7293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, word, u16, )
7393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, dword, u32, )
7493a686eeSJean-Christophe PLAGNIOL-VILLARD #endif	/* CONFIG_IXP425 */
7593a686eeSJean-Christophe PLAGNIOL-VILLARD 
7693a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_READ_VIA_DWORD_OP(size, type, off_mask)			\
7793a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
7893a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_dev_t dev,			\
7993a686eeSJean-Christophe PLAGNIOL-VILLARD 					int offset, type val)		\
8093a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
8193a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 val32;							\
8293a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8393a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) {	\
8493a686eeSJean-Christophe PLAGNIOL-VILLARD 		*val = -1;						\
8593a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
8693a686eeSJean-Christophe PLAGNIOL-VILLARD 	}								\
8793a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
8893a686eeSJean-Christophe PLAGNIOL-VILLARD 	*val = (val32 >> ((offset & (int)off_mask) * 8));		\
8993a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
9093a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;							\
9193a686eeSJean-Christophe PLAGNIOL-VILLARD }
9293a686eeSJean-Christophe PLAGNIOL-VILLARD 
9393a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask)		\
9493a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
9593a686eeSJean-Christophe PLAGNIOL-VILLARD 					     pci_dev_t dev,		\
9693a686eeSJean-Christophe PLAGNIOL-VILLARD 					     int offset, type val)	\
9793a686eeSJean-Christophe PLAGNIOL-VILLARD {									\
9893a686eeSJean-Christophe PLAGNIOL-VILLARD 	u32 val32, mask, ldata, shift;					\
9993a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
10093a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
10193a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
10293a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
10393a686eeSJean-Christophe PLAGNIOL-VILLARD 	shift = ((offset & (int)off_mask) * 8);				\
10493a686eeSJean-Christophe PLAGNIOL-VILLARD 	ldata = (((unsigned long)val) & val_mask) << shift;		\
10593a686eeSJean-Christophe PLAGNIOL-VILLARD 	mask = val_mask << shift;					\
10693a686eeSJean-Christophe PLAGNIOL-VILLARD 	val32 = (val32 & ~mask) | ldata;				\
10793a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
10893a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
10993a686eeSJean-Christophe PLAGNIOL-VILLARD 		return -1;						\
11093a686eeSJean-Christophe PLAGNIOL-VILLARD 									\
11193a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;							\
11293a686eeSJean-Christophe PLAGNIOL-VILLARD }
11393a686eeSJean-Christophe PLAGNIOL-VILLARD 
11493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
11593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
11693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
11793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
11893a686eeSJean-Christophe PLAGNIOL-VILLARD 
119*6e61fae4SBecky Bruce /* Get a virtual address associated with a BAR region */
120*6e61fae4SBecky Bruce void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
121*6e61fae4SBecky Bruce {
122*6e61fae4SBecky Bruce 	pci_addr_t pci_bus_addr;
123*6e61fae4SBecky Bruce 	u32 bar_response;
124*6e61fae4SBecky Bruce 
125*6e61fae4SBecky Bruce 	/* read BAR address */
126*6e61fae4SBecky Bruce 	pci_read_config_dword(pdev, bar, &bar_response);
127*6e61fae4SBecky Bruce 	pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
128*6e61fae4SBecky Bruce 
129*6e61fae4SBecky Bruce 	/*
130*6e61fae4SBecky Bruce 	 * Pass "0" as the length argument to pci_bus_to_virt.  The arg
131*6e61fae4SBecky Bruce 	 * isn't actualy used on any platform because u-boot assumes a static
132*6e61fae4SBecky Bruce 	 * linear mapping.  In the future, this could read the BAR size
133*6e61fae4SBecky Bruce 	 * and pass that as the size if needed.
134*6e61fae4SBecky Bruce 	 */
135*6e61fae4SBecky Bruce 	return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
136*6e61fae4SBecky Bruce }
137*6e61fae4SBecky Bruce 
13893a686eeSJean-Christophe PLAGNIOL-VILLARD /*
13993a686eeSJean-Christophe PLAGNIOL-VILLARD  *
14093a686eeSJean-Christophe PLAGNIOL-VILLARD  */
14193a686eeSJean-Christophe PLAGNIOL-VILLARD 
14293a686eeSJean-Christophe PLAGNIOL-VILLARD static struct pci_controller* hose_head = NULL;
14393a686eeSJean-Christophe PLAGNIOL-VILLARD 
14493a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose)
14593a686eeSJean-Christophe PLAGNIOL-VILLARD {
14693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller **phose = &hose_head;
14793a686eeSJean-Christophe PLAGNIOL-VILLARD 
14893a686eeSJean-Christophe PLAGNIOL-VILLARD 	while(*phose)
14993a686eeSJean-Christophe PLAGNIOL-VILLARD 		phose = &(*phose)->next;
15093a686eeSJean-Christophe PLAGNIOL-VILLARD 
15193a686eeSJean-Christophe PLAGNIOL-VILLARD 	hose->next = NULL;
15293a686eeSJean-Christophe PLAGNIOL-VILLARD 
15393a686eeSJean-Christophe PLAGNIOL-VILLARD 	*phose = hose;
15493a686eeSJean-Christophe PLAGNIOL-VILLARD }
15593a686eeSJean-Christophe PLAGNIOL-VILLARD 
15693a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose (int bus)
15793a686eeSJean-Christophe PLAGNIOL-VILLARD {
15893a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose;
15993a686eeSJean-Christophe PLAGNIOL-VILLARD 
16093a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (hose = hose_head; hose; hose = hose->next)
16193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus >= hose->first_busno && bus <= hose->last_busno)
16293a686eeSJean-Christophe PLAGNIOL-VILLARD 			return hose;
16393a686eeSJean-Christophe PLAGNIOL-VILLARD 
16493a686eeSJean-Christophe PLAGNIOL-VILLARD 	printf("pci_bus_to_hose() failed\n");
16593a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
16693a686eeSJean-Christophe PLAGNIOL-VILLARD }
16793a686eeSJean-Christophe PLAGNIOL-VILLARD 
16893a686eeSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_IXP425
16993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
17093a686eeSJean-Christophe PLAGNIOL-VILLARD {
17193a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller * hose;
17293a686eeSJean-Christophe PLAGNIOL-VILLARD 	u16 vendor, device;
17393a686eeSJean-Christophe PLAGNIOL-VILLARD 	u8 header_type;
17493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t bdf;
17593a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i, bus, found_multi = 0;
17693a686eeSJean-Christophe PLAGNIOL-VILLARD 
17793a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (hose = hose_head; hose; hose = hose->next)
17893a686eeSJean-Christophe PLAGNIOL-VILLARD 	{
1796d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
18093a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
18193a686eeSJean-Christophe PLAGNIOL-VILLARD #else
18293a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
18393a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
18493a686eeSJean-Christophe PLAGNIOL-VILLARD 			for (bdf = PCI_BDF(bus,0,0);
18593a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
18693a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
18793a686eeSJean-Christophe PLAGNIOL-VILLARD #else
18893a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf < PCI_BDF(bus+1,0,0);
18993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
19093a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf += PCI_BDF(0,0,1))
19193a686eeSJean-Christophe PLAGNIOL-VILLARD 			{
19293a686eeSJean-Christophe PLAGNIOL-VILLARD 				if (!PCI_FUNC(bdf)) {
19393a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_read_config_byte(bdf,
19493a686eeSJean-Christophe PLAGNIOL-VILLARD 							     PCI_HEADER_TYPE,
19593a686eeSJean-Christophe PLAGNIOL-VILLARD 							     &header_type);
19693a686eeSJean-Christophe PLAGNIOL-VILLARD 
19793a686eeSJean-Christophe PLAGNIOL-VILLARD 					found_multi = header_type & 0x80;
19893a686eeSJean-Christophe PLAGNIOL-VILLARD 				} else {
19993a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (!found_multi)
20093a686eeSJean-Christophe PLAGNIOL-VILLARD 						continue;
20193a686eeSJean-Christophe PLAGNIOL-VILLARD 				}
20293a686eeSJean-Christophe PLAGNIOL-VILLARD 
20393a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
20493a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_VENDOR_ID,
20593a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &vendor);
20693a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
20793a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_DEVICE_ID,
20893a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &device);
20993a686eeSJean-Christophe PLAGNIOL-VILLARD 
21093a686eeSJean-Christophe PLAGNIOL-VILLARD 				for (i=0; ids[i].vendor != 0; i++)
21193a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (vendor == ids[i].vendor &&
21293a686eeSJean-Christophe PLAGNIOL-VILLARD 					    device == ids[i].device)
21393a686eeSJean-Christophe PLAGNIOL-VILLARD 					{
21493a686eeSJean-Christophe PLAGNIOL-VILLARD 						if (index <= 0)
21593a686eeSJean-Christophe PLAGNIOL-VILLARD 							return bdf;
21693a686eeSJean-Christophe PLAGNIOL-VILLARD 
21793a686eeSJean-Christophe PLAGNIOL-VILLARD 						index--;
21893a686eeSJean-Christophe PLAGNIOL-VILLARD 					}
21993a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
22093a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
22193a686eeSJean-Christophe PLAGNIOL-VILLARD 
22293a686eeSJean-Christophe PLAGNIOL-VILLARD 	return (-1);
22393a686eeSJean-Christophe PLAGNIOL-VILLARD }
22493a686eeSJean-Christophe PLAGNIOL-VILLARD #endif	/* CONFIG_IXP425 */
22593a686eeSJean-Christophe PLAGNIOL-VILLARD 
22693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
22793a686eeSJean-Christophe PLAGNIOL-VILLARD {
22893a686eeSJean-Christophe PLAGNIOL-VILLARD 	static struct pci_device_id ids[2] = {{}, {0, 0}};
22993a686eeSJean-Christophe PLAGNIOL-VILLARD 
23093a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].vendor = vendor;
23193a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].device = device;
23293a686eeSJean-Christophe PLAGNIOL-VILLARD 
23393a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_find_devices(ids, index);
23493a686eeSJean-Christophe PLAGNIOL-VILLARD }
23593a686eeSJean-Christophe PLAGNIOL-VILLARD 
23693a686eeSJean-Christophe PLAGNIOL-VILLARD /*
23793a686eeSJean-Christophe PLAGNIOL-VILLARD  *
23893a686eeSJean-Christophe PLAGNIOL-VILLARD  */
23993a686eeSJean-Christophe PLAGNIOL-VILLARD 
2402d43e873SKumar Gala int __pci_hose_phys_to_bus (struct pci_controller *hose,
24136f32675SBecky Bruce 				phys_addr_t phys_addr,
2422d43e873SKumar Gala 				unsigned long flags,
2432d43e873SKumar Gala 				unsigned long skip_mask,
2442d43e873SKumar Gala 				pci_addr_t *ba)
24593a686eeSJean-Christophe PLAGNIOL-VILLARD {
24693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
24730e76d5eSKumar Gala 	pci_addr_t bus_addr;
24893a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
24993a686eeSJean-Christophe PLAGNIOL-VILLARD 
25093a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
25193a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
25293a686eeSJean-Christophe PLAGNIOL-VILLARD 
25393a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
25493a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
25593a686eeSJean-Christophe PLAGNIOL-VILLARD 
2562d43e873SKumar Gala 		if (res->flags & skip_mask)
2572d43e873SKumar Gala 			continue;
2582d43e873SKumar Gala 
25993a686eeSJean-Christophe PLAGNIOL-VILLARD 		bus_addr = phys_addr - res->phys_start + res->bus_start;
26093a686eeSJean-Christophe PLAGNIOL-VILLARD 
26193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
26293a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
2632d43e873SKumar Gala 			*ba = bus_addr;
26493a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
26593a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
2662d43e873SKumar Gala 	}
26793a686eeSJean-Christophe PLAGNIOL-VILLARD 
2682d43e873SKumar Gala 	return 1;
2692d43e873SKumar Gala }
2702d43e873SKumar Gala 
2712d43e873SKumar Gala pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
2722d43e873SKumar Gala 				    phys_addr_t phys_addr,
27393a686eeSJean-Christophe PLAGNIOL-VILLARD 				    unsigned long flags)
27493a686eeSJean-Christophe PLAGNIOL-VILLARD {
2752d43e873SKumar Gala 	pci_addr_t bus_addr = 0;
2762d43e873SKumar Gala 	int ret;
2772d43e873SKumar Gala 
2782d43e873SKumar Gala 	if (!hose) {
2792d43e873SKumar Gala 		puts ("pci_hose_phys_to_bus: invalid hose\n");
2802d43e873SKumar Gala 		return bus_addr;
2812d43e873SKumar Gala 	}
2822d43e873SKumar Gala 
2832d43e873SKumar Gala 	/* if PCI_REGION_MEM is set we do a two pass search with preference
2842d43e873SKumar Gala 	 * on matches that don't have PCI_REGION_SYS_MEMORY set */
2852d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
2862d43e873SKumar Gala 		ret = __pci_hose_phys_to_bus(hose, phys_addr,
2872d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &bus_addr);
2882d43e873SKumar Gala 		if (!ret)
2892d43e873SKumar Gala 			return bus_addr;
2902d43e873SKumar Gala 	}
2912d43e873SKumar Gala 
2922d43e873SKumar Gala 	ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
2932d43e873SKumar Gala 
2942d43e873SKumar Gala 	if (ret)
2952d43e873SKumar Gala 		puts ("pci_hose_phys_to_bus: invalid physical address\n");
2962d43e873SKumar Gala 
2972d43e873SKumar Gala 	return bus_addr;
2982d43e873SKumar Gala }
2992d43e873SKumar Gala 
3002d43e873SKumar Gala int __pci_hose_bus_to_phys (struct pci_controller *hose,
3012d43e873SKumar Gala 				pci_addr_t bus_addr,
3022d43e873SKumar Gala 				unsigned long flags,
3032d43e873SKumar Gala 				unsigned long skip_mask,
3042d43e873SKumar Gala 				phys_addr_t *pa)
3052d43e873SKumar Gala {
30693a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
30793a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
30893a686eeSJean-Christophe PLAGNIOL-VILLARD 
30993a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
31093a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
31193a686eeSJean-Christophe PLAGNIOL-VILLARD 
31293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
31393a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
31493a686eeSJean-Christophe PLAGNIOL-VILLARD 
3152d43e873SKumar Gala 		if (res->flags & skip_mask)
3162d43e873SKumar Gala 			continue;
3172d43e873SKumar Gala 
31893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
31993a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
3202d43e873SKumar Gala 			*pa = (bus_addr - res->bus_start + res->phys_start);
32193a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
32293a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
3232d43e873SKumar Gala 	}
3242d43e873SKumar Gala 
3252d43e873SKumar Gala 	return 1;
3262d43e873SKumar Gala }
3272d43e873SKumar Gala 
3282d43e873SKumar Gala phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
3292d43e873SKumar Gala 				 pci_addr_t bus_addr,
3302d43e873SKumar Gala 				 unsigned long flags)
3312d43e873SKumar Gala {
3322d43e873SKumar Gala 	phys_addr_t phys_addr = 0;
3332d43e873SKumar Gala 	int ret;
3342d43e873SKumar Gala 
3352d43e873SKumar Gala 	if (!hose) {
3362d43e873SKumar Gala 		puts ("pci_hose_bus_to_phys: invalid hose\n");
3372d43e873SKumar Gala 		return phys_addr;
3382d43e873SKumar Gala 	}
3392d43e873SKumar Gala 
3402d43e873SKumar Gala 	/* if PCI_REGION_MEM is set we do a two pass search with preference
3412d43e873SKumar Gala 	 * on matches that don't have PCI_REGION_SYS_MEMORY set */
3422d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
3432d43e873SKumar Gala 		ret = __pci_hose_bus_to_phys(hose, bus_addr,
3442d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &phys_addr);
3452d43e873SKumar Gala 		if (!ret)
3462d43e873SKumar Gala 			return phys_addr;
3472d43e873SKumar Gala 	}
3482d43e873SKumar Gala 
3492d43e873SKumar Gala 	ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
3502d43e873SKumar Gala 
3512d43e873SKumar Gala 	if (ret)
3522d43e873SKumar Gala 		puts ("pci_hose_bus_to_phys: invalid physical address\n");
3532d43e873SKumar Gala 
3542d43e873SKumar Gala 	return phys_addr;
3552d43e873SKumar Gala }
35693a686eeSJean-Christophe PLAGNIOL-VILLARD 
35793a686eeSJean-Christophe PLAGNIOL-VILLARD /*
35893a686eeSJean-Christophe PLAGNIOL-VILLARD  *
35993a686eeSJean-Christophe PLAGNIOL-VILLARD  */
36093a686eeSJean-Christophe PLAGNIOL-VILLARD 
36193a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_config_device(struct pci_controller *hose,
36293a686eeSJean-Christophe PLAGNIOL-VILLARD 			   pci_dev_t dev,
36393a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long io,
36430e76d5eSKumar Gala 			   pci_addr_t mem,
36593a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long command)
36693a686eeSJean-Christophe PLAGNIOL-VILLARD {
36730e76d5eSKumar Gala 	unsigned int bar_response, old_command;
36830e76d5eSKumar Gala 	pci_addr_t bar_value;
36930e76d5eSKumar Gala 	pci_size_t bar_size;
37093a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char pin;
37193a686eeSJean-Christophe PLAGNIOL-VILLARD 	int bar, found_mem64;
37293a686eeSJean-Christophe PLAGNIOL-VILLARD 
37330e76d5eSKumar Gala 	debug ("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n",
37430e76d5eSKumar Gala 		io, (u64)mem, command);
37593a686eeSJean-Christophe PLAGNIOL-VILLARD 
37693a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0);
37793a686eeSJean-Christophe PLAGNIOL-VILLARD 
37893a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) {
37993a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_dword (hose, dev, bar, 0xffffffff);
38093a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_dword (hose, dev, bar, &bar_response);
38193a686eeSJean-Christophe PLAGNIOL-VILLARD 
38293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!bar_response)
38393a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
38493a686eeSJean-Christophe PLAGNIOL-VILLARD 
38593a686eeSJean-Christophe PLAGNIOL-VILLARD 		found_mem64 = 0;
38693a686eeSJean-Christophe PLAGNIOL-VILLARD 
38793a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Check the BAR type and set our address mask */
38893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bar_response & PCI_BASE_ADDRESS_SPACE) {
38993a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
39093a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to a multiple of size */
39193a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = ((io - 1) | (bar_size - 1)) + 1;
39293a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = io;
39393a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
39493a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = io + bar_size;
39593a686eeSJean-Christophe PLAGNIOL-VILLARD 		} else {
39693a686eeSJean-Christophe PLAGNIOL-VILLARD 			if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
39730e76d5eSKumar Gala 				PCI_BASE_ADDRESS_MEM_TYPE_64) {
39830e76d5eSKumar Gala 				u32 bar_response_upper;
39930e76d5eSKumar Gala 				u64 bar64;
40030e76d5eSKumar Gala 				pci_hose_write_config_dword(hose, dev, bar+4, 0xffffffff);
40130e76d5eSKumar Gala 				pci_hose_read_config_dword(hose, dev, bar+4, &bar_response_upper);
40293a686eeSJean-Christophe PLAGNIOL-VILLARD 
40330e76d5eSKumar Gala 				bar64 = ((u64)bar_response_upper << 32) | bar_response;
40430e76d5eSKumar Gala 
40530e76d5eSKumar Gala 				bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
40630e76d5eSKumar Gala 				found_mem64 = 1;
40730e76d5eSKumar Gala 			} else {
40830e76d5eSKumar Gala 				bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
40930e76d5eSKumar Gala 			}
41093a686eeSJean-Christophe PLAGNIOL-VILLARD 
41193a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to multiple of size */
41293a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = ((mem - 1) | (bar_size - 1)) + 1;
41393a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = mem;
41493a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
41593a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = mem + bar_size;
41693a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
41793a686eeSJean-Christophe PLAGNIOL-VILLARD 
41893a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Write it out and update our limit */
41930e76d5eSKumar Gala 		pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
42093a686eeSJean-Christophe PLAGNIOL-VILLARD 
42193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (found_mem64) {
42293a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar += 4;
42330e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
42430e76d5eSKumar Gala 			pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
42530e76d5eSKumar Gala #else
42693a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_write_config_dword (hose, dev, bar, 0x00000000);
42730e76d5eSKumar Gala #endif
42893a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
42993a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
43093a686eeSJean-Christophe PLAGNIOL-VILLARD 
43193a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Cache Line Size Register */
43293a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
43393a686eeSJean-Christophe PLAGNIOL-VILLARD 
43493a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Latency Timer */
43593a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80);
43693a686eeSJean-Christophe PLAGNIOL-VILLARD 
43793a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Disable interrupt line, if device says it wants to use interrupts */
43893a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin);
43993a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pin != 0) {
44093a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff);
44193a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
44293a686eeSJean-Christophe PLAGNIOL-VILLARD 
44393a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command);
44493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword (hose, dev, PCI_COMMAND,
44593a686eeSJean-Christophe PLAGNIOL-VILLARD 				     (old_command & 0xffff0000) | command);
44693a686eeSJean-Christophe PLAGNIOL-VILLARD 
44793a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;
44893a686eeSJean-Christophe PLAGNIOL-VILLARD }
44993a686eeSJean-Christophe PLAGNIOL-VILLARD 
45093a686eeSJean-Christophe PLAGNIOL-VILLARD /*
45193a686eeSJean-Christophe PLAGNIOL-VILLARD  *
45293a686eeSJean-Christophe PLAGNIOL-VILLARD  */
45393a686eeSJean-Christophe PLAGNIOL-VILLARD 
45493a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose,
45593a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned short class,
45693a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int vendor,
45793a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int device,
45893a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int bus,
45993a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int dev,
46093a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int func)
46193a686eeSJean-Christophe PLAGNIOL-VILLARD {
46293a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *table;
46393a686eeSJean-Christophe PLAGNIOL-VILLARD 
46493a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (table = hose->config_table; table && table->vendor; table++) {
46593a686eeSJean-Christophe PLAGNIOL-VILLARD 		if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
46693a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->device == PCI_ANY_ID || table->device == device) &&
46793a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->class  == PCI_ANY_ID || table->class  == class)  &&
46893a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->bus    == PCI_ANY_ID || table->bus    == bus)    &&
46993a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->dev    == PCI_ANY_ID || table->dev    == dev)    &&
47093a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->func   == PCI_ANY_ID || table->func   == func)) {
47193a686eeSJean-Christophe PLAGNIOL-VILLARD 			return table;
47293a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
47393a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
47493a686eeSJean-Christophe PLAGNIOL-VILLARD 
47593a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
47693a686eeSJean-Christophe PLAGNIOL-VILLARD }
47793a686eeSJean-Christophe PLAGNIOL-VILLARD 
47893a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose,
47993a686eeSJean-Christophe PLAGNIOL-VILLARD 			       pci_dev_t dev,
48093a686eeSJean-Christophe PLAGNIOL-VILLARD 			       struct pci_config_table *entry)
48193a686eeSJean-Christophe PLAGNIOL-VILLARD {
48293a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]);
48393a686eeSJean-Christophe PLAGNIOL-VILLARD }
48493a686eeSJean-Christophe PLAGNIOL-VILLARD 
48593a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose,
48693a686eeSJean-Christophe PLAGNIOL-VILLARD 			    pci_dev_t dev, struct pci_config_table *entry)
48793a686eeSJean-Christophe PLAGNIOL-VILLARD {
48893a686eeSJean-Christophe PLAGNIOL-VILLARD }
48993a686eeSJean-Christophe PLAGNIOL-VILLARD 
49093a686eeSJean-Christophe PLAGNIOL-VILLARD /*
49193a686eeSJean-Christophe PLAGNIOL-VILLARD  *
49293a686eeSJean-Christophe PLAGNIOL-VILLARD  */
49393a686eeSJean-Christophe PLAGNIOL-VILLARD 
49493a686eeSJean-Christophe PLAGNIOL-VILLARD /* HJF: Changed this to return int. I think this is required
49593a686eeSJean-Christophe PLAGNIOL-VILLARD  * to get the correct result when scanning bridges
49693a686eeSJean-Christophe PLAGNIOL-VILLARD  */
49793a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
49893a686eeSJean-Christophe PLAGNIOL-VILLARD extern void pciauto_config_init(struct pci_controller *hose);
49993a686eeSJean-Christophe PLAGNIOL-VILLARD 
500dc1da42fSStefan Roese int __pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
501dc1da42fSStefan Roese {
502dc1da42fSStefan Roese 	/*
503dc1da42fSStefan Roese 	 * Check if pci device should be skipped in configuration
504dc1da42fSStefan Roese 	 */
505dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
506dc1da42fSStefan Roese #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
507dc1da42fSStefan Roese 		/*
508dc1da42fSStefan Roese 		 * Only skip configuration if "pciconfighost" is not set
509dc1da42fSStefan Roese 		 */
510dc1da42fSStefan Roese 		if (getenv("pciconfighost") == NULL)
511dc1da42fSStefan Roese 			return 1;
512dc1da42fSStefan Roese #else
513dc1da42fSStefan Roese 		return 1;
514dc1da42fSStefan Roese #endif
515dc1da42fSStefan Roese 	}
516dc1da42fSStefan Roese 
517dc1da42fSStefan Roese 	return 0;
518dc1da42fSStefan Roese }
519dc1da42fSStefan Roese int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
520dc1da42fSStefan Roese 	__attribute__((weak, alias("__pci_skip_dev")));
521dc1da42fSStefan Roese 
522dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW
523dc1da42fSStefan Roese int __pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
524dc1da42fSStefan Roese {
525dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0))
526dc1da42fSStefan Roese 		return 0;
527dc1da42fSStefan Roese 
528dc1da42fSStefan Roese 	return 1;
529dc1da42fSStefan Roese }
530dc1da42fSStefan Roese int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
531dc1da42fSStefan Roese 	__attribute__((weak, alias("__pci_print_dev")));
532dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */
533dc1da42fSStefan Roese 
53493a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus)
53593a686eeSJean-Christophe PLAGNIOL-VILLARD {
53693a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned int sub_bus, found_multi=0;
53793a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned short vendor, device, class;
53893a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char header_type;
53993a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *cfg;
54093a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t dev;
54193a686eeSJean-Christophe PLAGNIOL-VILLARD 
54293a686eeSJean-Christophe PLAGNIOL-VILLARD 	sub_bus = bus;
54393a686eeSJean-Christophe PLAGNIOL-VILLARD 
54493a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (dev =  PCI_BDF(bus,0,0);
54593a686eeSJean-Christophe PLAGNIOL-VILLARD 	     dev <  PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
546dc1da42fSStefan Roese 	     dev += PCI_BDF(0,0,1)) {
547dc1da42fSStefan Roese 
548dc1da42fSStefan Roese 		if (pci_skip_dev(hose, dev))
549dc1da42fSStefan Roese 			continue;
55093a686eeSJean-Christophe PLAGNIOL-VILLARD 
55193a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (PCI_FUNC(dev) && !found_multi)
55293a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
55393a686eeSJean-Christophe PLAGNIOL-VILLARD 
55493a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
55593a686eeSJean-Christophe PLAGNIOL-VILLARD 
55693a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
55793a686eeSJean-Christophe PLAGNIOL-VILLARD 
55893a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (vendor != 0xffff && vendor != 0x0000) {
55993a686eeSJean-Christophe PLAGNIOL-VILLARD 
56093a686eeSJean-Christophe PLAGNIOL-VILLARD 			if (!PCI_FUNC(dev))
56193a686eeSJean-Christophe PLAGNIOL-VILLARD 				found_multi = header_type & 0x80;
56293a686eeSJean-Christophe PLAGNIOL-VILLARD 
56393a686eeSJean-Christophe PLAGNIOL-VILLARD 			debug ("PCI Scan: Found Bus %d, Device %d, Function %d\n",
56493a686eeSJean-Christophe PLAGNIOL-VILLARD 				PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) );
56593a686eeSJean-Christophe PLAGNIOL-VILLARD 
56693a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
56793a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
56893a686eeSJean-Christophe PLAGNIOL-VILLARD 
56993a686eeSJean-Christophe PLAGNIOL-VILLARD 			cfg = pci_find_config(hose, class, vendor, device,
57093a686eeSJean-Christophe PLAGNIOL-VILLARD 					      PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
57193a686eeSJean-Christophe PLAGNIOL-VILLARD 			if (cfg) {
57293a686eeSJean-Christophe PLAGNIOL-VILLARD 				cfg->config_device(hose, dev, cfg);
57393a686eeSJean-Christophe PLAGNIOL-VILLARD 				sub_bus = max(sub_bus, hose->current_busno);
57493a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
57593a686eeSJean-Christophe PLAGNIOL-VILLARD 			} else {
57693a686eeSJean-Christophe PLAGNIOL-VILLARD 				int n = pciauto_config_device(hose, dev);
57793a686eeSJean-Christophe PLAGNIOL-VILLARD 
57893a686eeSJean-Christophe PLAGNIOL-VILLARD 				sub_bus = max(sub_bus, n);
57993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
58093a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
58193a686eeSJean-Christophe PLAGNIOL-VILLARD 			if (hose->fixup_irq)
58293a686eeSJean-Christophe PLAGNIOL-VILLARD 				hose->fixup_irq(hose, dev);
58393a686eeSJean-Christophe PLAGNIOL-VILLARD 
58493a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_SCAN_SHOW
585dc1da42fSStefan Roese 			if (pci_print_dev(hose, dev)) {
58693a686eeSJean-Christophe PLAGNIOL-VILLARD 				unsigned char int_line;
58793a686eeSJean-Christophe PLAGNIOL-VILLARD 
58893a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE,
58993a686eeSJean-Christophe PLAGNIOL-VILLARD 							  &int_line);
59093a686eeSJean-Christophe PLAGNIOL-VILLARD 				printf("        %02x  %02x  %04x  %04x  %04x  %02x\n",
59193a686eeSJean-Christophe PLAGNIOL-VILLARD 				       PCI_BUS(dev), PCI_DEV(dev), vendor, device, class,
59293a686eeSJean-Christophe PLAGNIOL-VILLARD 				       int_line);
59393a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
59493a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
59593a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
59693a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
59793a686eeSJean-Christophe PLAGNIOL-VILLARD 
59893a686eeSJean-Christophe PLAGNIOL-VILLARD 	return sub_bus;
59993a686eeSJean-Christophe PLAGNIOL-VILLARD }
60093a686eeSJean-Christophe PLAGNIOL-VILLARD 
60193a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose)
60293a686eeSJean-Christophe PLAGNIOL-VILLARD {
60393a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Start scan at current_busno.
60493a686eeSJean-Christophe PLAGNIOL-VILLARD 	 * PCIe will start scan at first_busno+1.
60593a686eeSJean-Christophe PLAGNIOL-VILLARD 	 */
60693a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* For legacy support, ensure current>=first */
60793a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (hose->first_busno > hose->current_busno)
60893a686eeSJean-Christophe PLAGNIOL-VILLARD 		hose->current_busno = hose->first_busno;
60993a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
61093a686eeSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_init(hose);
61193a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
61293a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_scan_bus(hose, hose->current_busno);
61393a686eeSJean-Christophe PLAGNIOL-VILLARD }
61493a686eeSJean-Christophe PLAGNIOL-VILLARD 
61593a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void)
61693a686eeSJean-Christophe PLAGNIOL-VILLARD {
61793a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_PCI_BOOTDELAY)
61893a686eeSJean-Christophe PLAGNIOL-VILLARD 	char *s;
61993a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
62093a686eeSJean-Christophe PLAGNIOL-VILLARD 
62193a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* wait "pcidelay" ms (if defined)... */
62293a686eeSJean-Christophe PLAGNIOL-VILLARD 	s = getenv ("pcidelay");
62393a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (s) {
62493a686eeSJean-Christophe PLAGNIOL-VILLARD 		int val = simple_strtoul (s, NULL, 10);
62593a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (i=0; i<val; i++)
62693a686eeSJean-Christophe PLAGNIOL-VILLARD 			udelay (1000);
62793a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
62893a686eeSJean-Christophe PLAGNIOL-VILLARD #endif /* CONFIG_PCI_BOOTDELAY */
62993a686eeSJean-Christophe PLAGNIOL-VILLARD 
63093a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* now call board specific pci_init()... */
63193a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_init_board();
63293a686eeSJean-Christophe PLAGNIOL-VILLARD }
633