xref: /rk3399_rockchip-uboot/drivers/pci/pci.c (revision 2d43e873a29ca4959ba6a30fc7fb396d3fd0dccf)
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 
11993a686eeSJean-Christophe PLAGNIOL-VILLARD /*
12093a686eeSJean-Christophe PLAGNIOL-VILLARD  *
12193a686eeSJean-Christophe PLAGNIOL-VILLARD  */
12293a686eeSJean-Christophe PLAGNIOL-VILLARD 
12393a686eeSJean-Christophe PLAGNIOL-VILLARD static struct pci_controller* hose_head = NULL;
12493a686eeSJean-Christophe PLAGNIOL-VILLARD 
12593a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose)
12693a686eeSJean-Christophe PLAGNIOL-VILLARD {
12793a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller **phose = &hose_head;
12893a686eeSJean-Christophe PLAGNIOL-VILLARD 
12993a686eeSJean-Christophe PLAGNIOL-VILLARD 	while(*phose)
13093a686eeSJean-Christophe PLAGNIOL-VILLARD 		phose = &(*phose)->next;
13193a686eeSJean-Christophe PLAGNIOL-VILLARD 
13293a686eeSJean-Christophe PLAGNIOL-VILLARD 	hose->next = NULL;
13393a686eeSJean-Christophe PLAGNIOL-VILLARD 
13493a686eeSJean-Christophe PLAGNIOL-VILLARD 	*phose = hose;
13593a686eeSJean-Christophe PLAGNIOL-VILLARD }
13693a686eeSJean-Christophe PLAGNIOL-VILLARD 
13793a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose (int bus)
13893a686eeSJean-Christophe PLAGNIOL-VILLARD {
13993a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller *hose;
14093a686eeSJean-Christophe PLAGNIOL-VILLARD 
14193a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (hose = hose_head; hose; hose = hose->next)
14293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus >= hose->first_busno && bus <= hose->last_busno)
14393a686eeSJean-Christophe PLAGNIOL-VILLARD 			return hose;
14493a686eeSJean-Christophe PLAGNIOL-VILLARD 
14593a686eeSJean-Christophe PLAGNIOL-VILLARD 	printf("pci_bus_to_hose() failed\n");
14693a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
14793a686eeSJean-Christophe PLAGNIOL-VILLARD }
14893a686eeSJean-Christophe PLAGNIOL-VILLARD 
14993a686eeSJean-Christophe PLAGNIOL-VILLARD #ifndef CONFIG_IXP425
15093a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
15193a686eeSJean-Christophe PLAGNIOL-VILLARD {
15293a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_controller * hose;
15393a686eeSJean-Christophe PLAGNIOL-VILLARD 	u16 vendor, device;
15493a686eeSJean-Christophe PLAGNIOL-VILLARD 	u8 header_type;
15593a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t bdf;
15693a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i, bus, found_multi = 0;
15793a686eeSJean-Christophe PLAGNIOL-VILLARD 
15893a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (hose = hose_head; hose; hose = hose->next)
15993a686eeSJean-Christophe PLAGNIOL-VILLARD 	{
1606d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
16193a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
16293a686eeSJean-Christophe PLAGNIOL-VILLARD #else
16393a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
16493a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
16593a686eeSJean-Christophe PLAGNIOL-VILLARD 			for (bdf = PCI_BDF(bus,0,0);
16693a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
16793a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
16893a686eeSJean-Christophe PLAGNIOL-VILLARD #else
16993a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf < PCI_BDF(bus+1,0,0);
17093a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
17193a686eeSJean-Christophe PLAGNIOL-VILLARD 			     bdf += PCI_BDF(0,0,1))
17293a686eeSJean-Christophe PLAGNIOL-VILLARD 			{
17393a686eeSJean-Christophe PLAGNIOL-VILLARD 				if (!PCI_FUNC(bdf)) {
17493a686eeSJean-Christophe PLAGNIOL-VILLARD 					pci_read_config_byte(bdf,
17593a686eeSJean-Christophe PLAGNIOL-VILLARD 							     PCI_HEADER_TYPE,
17693a686eeSJean-Christophe PLAGNIOL-VILLARD 							     &header_type);
17793a686eeSJean-Christophe PLAGNIOL-VILLARD 
17893a686eeSJean-Christophe PLAGNIOL-VILLARD 					found_multi = header_type & 0x80;
17993a686eeSJean-Christophe PLAGNIOL-VILLARD 				} else {
18093a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (!found_multi)
18193a686eeSJean-Christophe PLAGNIOL-VILLARD 						continue;
18293a686eeSJean-Christophe PLAGNIOL-VILLARD 				}
18393a686eeSJean-Christophe PLAGNIOL-VILLARD 
18493a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
18593a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_VENDOR_ID,
18693a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &vendor);
18793a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_read_config_word(bdf,
18893a686eeSJean-Christophe PLAGNIOL-VILLARD 						     PCI_DEVICE_ID,
18993a686eeSJean-Christophe PLAGNIOL-VILLARD 						     &device);
19093a686eeSJean-Christophe PLAGNIOL-VILLARD 
19193a686eeSJean-Christophe PLAGNIOL-VILLARD 				for (i=0; ids[i].vendor != 0; i++)
19293a686eeSJean-Christophe PLAGNIOL-VILLARD 					if (vendor == ids[i].vendor &&
19393a686eeSJean-Christophe PLAGNIOL-VILLARD 					    device == ids[i].device)
19493a686eeSJean-Christophe PLAGNIOL-VILLARD 					{
19593a686eeSJean-Christophe PLAGNIOL-VILLARD 						if (index <= 0)
19693a686eeSJean-Christophe PLAGNIOL-VILLARD 							return bdf;
19793a686eeSJean-Christophe PLAGNIOL-VILLARD 
19893a686eeSJean-Christophe PLAGNIOL-VILLARD 						index--;
19993a686eeSJean-Christophe PLAGNIOL-VILLARD 					}
20093a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
20193a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
20293a686eeSJean-Christophe PLAGNIOL-VILLARD 
20393a686eeSJean-Christophe PLAGNIOL-VILLARD 	return (-1);
20493a686eeSJean-Christophe PLAGNIOL-VILLARD }
20593a686eeSJean-Christophe PLAGNIOL-VILLARD #endif	/* CONFIG_IXP425 */
20693a686eeSJean-Christophe PLAGNIOL-VILLARD 
20793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
20893a686eeSJean-Christophe PLAGNIOL-VILLARD {
20993a686eeSJean-Christophe PLAGNIOL-VILLARD 	static struct pci_device_id ids[2] = {{}, {0, 0}};
21093a686eeSJean-Christophe PLAGNIOL-VILLARD 
21193a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].vendor = vendor;
21293a686eeSJean-Christophe PLAGNIOL-VILLARD 	ids[0].device = device;
21393a686eeSJean-Christophe PLAGNIOL-VILLARD 
21493a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_find_devices(ids, index);
21593a686eeSJean-Christophe PLAGNIOL-VILLARD }
21693a686eeSJean-Christophe PLAGNIOL-VILLARD 
21793a686eeSJean-Christophe PLAGNIOL-VILLARD /*
21893a686eeSJean-Christophe PLAGNIOL-VILLARD  *
21993a686eeSJean-Christophe PLAGNIOL-VILLARD  */
22093a686eeSJean-Christophe PLAGNIOL-VILLARD 
221*2d43e873SKumar Gala int __pci_hose_phys_to_bus (struct pci_controller *hose,
22236f32675SBecky Bruce 				phys_addr_t phys_addr,
223*2d43e873SKumar Gala 				unsigned long flags,
224*2d43e873SKumar Gala 				unsigned long skip_mask,
225*2d43e873SKumar Gala 				pci_addr_t *ba)
22693a686eeSJean-Christophe PLAGNIOL-VILLARD {
22793a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
22830e76d5eSKumar Gala 	pci_addr_t bus_addr;
22993a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
23093a686eeSJean-Christophe PLAGNIOL-VILLARD 
23193a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
23293a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
23393a686eeSJean-Christophe PLAGNIOL-VILLARD 
23493a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
23593a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
23693a686eeSJean-Christophe PLAGNIOL-VILLARD 
237*2d43e873SKumar Gala 		if (res->flags & skip_mask)
238*2d43e873SKumar Gala 			continue;
239*2d43e873SKumar Gala 
24093a686eeSJean-Christophe PLAGNIOL-VILLARD 		bus_addr = phys_addr - res->phys_start + res->bus_start;
24193a686eeSJean-Christophe PLAGNIOL-VILLARD 
24293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
24393a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
244*2d43e873SKumar Gala 			*ba = bus_addr;
24593a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
24693a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
247*2d43e873SKumar Gala 	}
24893a686eeSJean-Christophe PLAGNIOL-VILLARD 
249*2d43e873SKumar Gala 	return 1;
250*2d43e873SKumar Gala }
251*2d43e873SKumar Gala 
252*2d43e873SKumar Gala pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
253*2d43e873SKumar Gala 				    phys_addr_t phys_addr,
25493a686eeSJean-Christophe PLAGNIOL-VILLARD 				    unsigned long flags)
25593a686eeSJean-Christophe PLAGNIOL-VILLARD {
256*2d43e873SKumar Gala 	pci_addr_t bus_addr = 0;
257*2d43e873SKumar Gala 	int ret;
258*2d43e873SKumar Gala 
259*2d43e873SKumar Gala 	if (!hose) {
260*2d43e873SKumar Gala 		puts ("pci_hose_phys_to_bus: invalid hose\n");
261*2d43e873SKumar Gala 		return bus_addr;
262*2d43e873SKumar Gala 	}
263*2d43e873SKumar Gala 
264*2d43e873SKumar Gala 	/* if PCI_REGION_MEM is set we do a two pass search with preference
265*2d43e873SKumar Gala 	 * on matches that don't have PCI_REGION_SYS_MEMORY set */
266*2d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
267*2d43e873SKumar Gala 		ret = __pci_hose_phys_to_bus(hose, phys_addr,
268*2d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &bus_addr);
269*2d43e873SKumar Gala 		if (!ret)
270*2d43e873SKumar Gala 			return bus_addr;
271*2d43e873SKumar Gala 	}
272*2d43e873SKumar Gala 
273*2d43e873SKumar Gala 	ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
274*2d43e873SKumar Gala 
275*2d43e873SKumar Gala 	if (ret)
276*2d43e873SKumar Gala 		puts ("pci_hose_phys_to_bus: invalid physical address\n");
277*2d43e873SKumar Gala 
278*2d43e873SKumar Gala 	return bus_addr;
279*2d43e873SKumar Gala }
280*2d43e873SKumar Gala 
281*2d43e873SKumar Gala int __pci_hose_bus_to_phys (struct pci_controller *hose,
282*2d43e873SKumar Gala 				pci_addr_t bus_addr,
283*2d43e873SKumar Gala 				unsigned long flags,
284*2d43e873SKumar Gala 				unsigned long skip_mask,
285*2d43e873SKumar Gala 				phys_addr_t *pa)
286*2d43e873SKumar Gala {
28793a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_region *res;
28893a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
28993a686eeSJean-Christophe PLAGNIOL-VILLARD 
29093a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < hose->region_count; i++) {
29193a686eeSJean-Christophe PLAGNIOL-VILLARD 		res = &hose->regions[i];
29293a686eeSJean-Christophe PLAGNIOL-VILLARD 
29393a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
29493a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
29593a686eeSJean-Christophe PLAGNIOL-VILLARD 
296*2d43e873SKumar Gala 		if (res->flags & skip_mask)
297*2d43e873SKumar Gala 			continue;
298*2d43e873SKumar Gala 
29993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bus_addr >= res->bus_start &&
30093a686eeSJean-Christophe PLAGNIOL-VILLARD 			bus_addr < res->bus_start + res->size) {
301*2d43e873SKumar Gala 			*pa = (bus_addr - res->bus_start + res->phys_start);
30293a686eeSJean-Christophe PLAGNIOL-VILLARD 			return 0;
30393a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
304*2d43e873SKumar Gala 	}
305*2d43e873SKumar Gala 
306*2d43e873SKumar Gala 	return 1;
307*2d43e873SKumar Gala }
308*2d43e873SKumar Gala 
309*2d43e873SKumar Gala phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
310*2d43e873SKumar Gala 				 pci_addr_t bus_addr,
311*2d43e873SKumar Gala 				 unsigned long flags)
312*2d43e873SKumar Gala {
313*2d43e873SKumar Gala 	phys_addr_t phys_addr = 0;
314*2d43e873SKumar Gala 	int ret;
315*2d43e873SKumar Gala 
316*2d43e873SKumar Gala 	if (!hose) {
317*2d43e873SKumar Gala 		puts ("pci_hose_bus_to_phys: invalid hose\n");
318*2d43e873SKumar Gala 		return phys_addr;
319*2d43e873SKumar Gala 	}
320*2d43e873SKumar Gala 
321*2d43e873SKumar Gala 	/* if PCI_REGION_MEM is set we do a two pass search with preference
322*2d43e873SKumar Gala 	 * on matches that don't have PCI_REGION_SYS_MEMORY set */
323*2d43e873SKumar Gala 	if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
324*2d43e873SKumar Gala 		ret = __pci_hose_bus_to_phys(hose, bus_addr,
325*2d43e873SKumar Gala 				flags, PCI_REGION_SYS_MEMORY, &phys_addr);
326*2d43e873SKumar Gala 		if (!ret)
327*2d43e873SKumar Gala 			return phys_addr;
328*2d43e873SKumar Gala 	}
329*2d43e873SKumar Gala 
330*2d43e873SKumar Gala 	ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
331*2d43e873SKumar Gala 
332*2d43e873SKumar Gala 	if (ret)
333*2d43e873SKumar Gala 		puts ("pci_hose_bus_to_phys: invalid physical address\n");
334*2d43e873SKumar Gala 
335*2d43e873SKumar Gala 	return phys_addr;
336*2d43e873SKumar Gala }
33793a686eeSJean-Christophe PLAGNIOL-VILLARD 
33893a686eeSJean-Christophe PLAGNIOL-VILLARD /*
33993a686eeSJean-Christophe PLAGNIOL-VILLARD  *
34093a686eeSJean-Christophe PLAGNIOL-VILLARD  */
34193a686eeSJean-Christophe PLAGNIOL-VILLARD 
34293a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_config_device(struct pci_controller *hose,
34393a686eeSJean-Christophe PLAGNIOL-VILLARD 			   pci_dev_t dev,
34493a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long io,
34530e76d5eSKumar Gala 			   pci_addr_t mem,
34693a686eeSJean-Christophe PLAGNIOL-VILLARD 			   unsigned long command)
34793a686eeSJean-Christophe PLAGNIOL-VILLARD {
34830e76d5eSKumar Gala 	unsigned int bar_response, old_command;
34930e76d5eSKumar Gala 	pci_addr_t bar_value;
35030e76d5eSKumar Gala 	pci_size_t bar_size;
35193a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char pin;
35293a686eeSJean-Christophe PLAGNIOL-VILLARD 	int bar, found_mem64;
35393a686eeSJean-Christophe PLAGNIOL-VILLARD 
35430e76d5eSKumar Gala 	debug ("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n",
35530e76d5eSKumar Gala 		io, (u64)mem, command);
35693a686eeSJean-Christophe PLAGNIOL-VILLARD 
35793a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0);
35893a686eeSJean-Christophe PLAGNIOL-VILLARD 
35993a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) {
36093a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_dword (hose, dev, bar, 0xffffffff);
36193a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_dword (hose, dev, bar, &bar_response);
36293a686eeSJean-Christophe PLAGNIOL-VILLARD 
36393a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (!bar_response)
36493a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
36593a686eeSJean-Christophe PLAGNIOL-VILLARD 
36693a686eeSJean-Christophe PLAGNIOL-VILLARD 		found_mem64 = 0;
36793a686eeSJean-Christophe PLAGNIOL-VILLARD 
36893a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Check the BAR type and set our address mask */
36993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (bar_response & PCI_BASE_ADDRESS_SPACE) {
37093a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
37193a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to a multiple of size */
37293a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = ((io - 1) | (bar_size - 1)) + 1;
37393a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = io;
37493a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
37593a686eeSJean-Christophe PLAGNIOL-VILLARD 			io = io + bar_size;
37693a686eeSJean-Christophe PLAGNIOL-VILLARD 		} else {
37793a686eeSJean-Christophe PLAGNIOL-VILLARD 			if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
37830e76d5eSKumar Gala 				PCI_BASE_ADDRESS_MEM_TYPE_64) {
37930e76d5eSKumar Gala 				u32 bar_response_upper;
38030e76d5eSKumar Gala 				u64 bar64;
38130e76d5eSKumar Gala 				pci_hose_write_config_dword(hose, dev, bar+4, 0xffffffff);
38230e76d5eSKumar Gala 				pci_hose_read_config_dword(hose, dev, bar+4, &bar_response_upper);
38393a686eeSJean-Christophe PLAGNIOL-VILLARD 
38430e76d5eSKumar Gala 				bar64 = ((u64)bar_response_upper << 32) | bar_response;
38530e76d5eSKumar Gala 
38630e76d5eSKumar Gala 				bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
38730e76d5eSKumar Gala 				found_mem64 = 1;
38830e76d5eSKumar Gala 			} else {
38930e76d5eSKumar Gala 				bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
39030e76d5eSKumar Gala 			}
39193a686eeSJean-Christophe PLAGNIOL-VILLARD 
39293a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* round up region base address to multiple of size */
39393a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = ((mem - 1) | (bar_size - 1)) + 1;
39493a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar_value = mem;
39593a686eeSJean-Christophe PLAGNIOL-VILLARD 			/* compute new region base address */
39693a686eeSJean-Christophe PLAGNIOL-VILLARD 			mem = mem + bar_size;
39793a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
39893a686eeSJean-Christophe PLAGNIOL-VILLARD 
39993a686eeSJean-Christophe PLAGNIOL-VILLARD 		/* Write it out and update our limit */
40030e76d5eSKumar Gala 		pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
40193a686eeSJean-Christophe PLAGNIOL-VILLARD 
40293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (found_mem64) {
40393a686eeSJean-Christophe PLAGNIOL-VILLARD 			bar += 4;
40430e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
40530e76d5eSKumar Gala 			pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
40630e76d5eSKumar Gala #else
40793a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_write_config_dword (hose, dev, bar, 0x00000000);
40830e76d5eSKumar Gala #endif
40993a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
41093a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
41193a686eeSJean-Christophe PLAGNIOL-VILLARD 
41293a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Cache Line Size Register */
41393a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
41493a686eeSJean-Christophe PLAGNIOL-VILLARD 
41593a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Configure Latency Timer */
41693a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80);
41793a686eeSJean-Christophe PLAGNIOL-VILLARD 
41893a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Disable interrupt line, if device says it wants to use interrupts */
41993a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin);
42093a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (pin != 0) {
42193a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff);
42293a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
42393a686eeSJean-Christophe PLAGNIOL-VILLARD 
42493a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command);
42593a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_write_config_dword (hose, dev, PCI_COMMAND,
42693a686eeSJean-Christophe PLAGNIOL-VILLARD 				     (old_command & 0xffff0000) | command);
42793a686eeSJean-Christophe PLAGNIOL-VILLARD 
42893a686eeSJean-Christophe PLAGNIOL-VILLARD 	return 0;
42993a686eeSJean-Christophe PLAGNIOL-VILLARD }
43093a686eeSJean-Christophe PLAGNIOL-VILLARD 
43193a686eeSJean-Christophe PLAGNIOL-VILLARD /*
43293a686eeSJean-Christophe PLAGNIOL-VILLARD  *
43393a686eeSJean-Christophe PLAGNIOL-VILLARD  */
43493a686eeSJean-Christophe PLAGNIOL-VILLARD 
43593a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose,
43693a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned short class,
43793a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int vendor,
43893a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int device,
43993a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int bus,
44093a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int dev,
44193a686eeSJean-Christophe PLAGNIOL-VILLARD 					 unsigned int func)
44293a686eeSJean-Christophe PLAGNIOL-VILLARD {
44393a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *table;
44493a686eeSJean-Christophe PLAGNIOL-VILLARD 
44593a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (table = hose->config_table; table && table->vendor; table++) {
44693a686eeSJean-Christophe PLAGNIOL-VILLARD 		if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
44793a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->device == PCI_ANY_ID || table->device == device) &&
44893a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->class  == PCI_ANY_ID || table->class  == class)  &&
44993a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->bus    == PCI_ANY_ID || table->bus    == bus)    &&
45093a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->dev    == PCI_ANY_ID || table->dev    == dev)    &&
45193a686eeSJean-Christophe PLAGNIOL-VILLARD 		    (table->func   == PCI_ANY_ID || table->func   == func)) {
45293a686eeSJean-Christophe PLAGNIOL-VILLARD 			return table;
45393a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
45493a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
45593a686eeSJean-Christophe PLAGNIOL-VILLARD 
45693a686eeSJean-Christophe PLAGNIOL-VILLARD 	return NULL;
45793a686eeSJean-Christophe PLAGNIOL-VILLARD }
45893a686eeSJean-Christophe PLAGNIOL-VILLARD 
45993a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose,
46093a686eeSJean-Christophe PLAGNIOL-VILLARD 			       pci_dev_t dev,
46193a686eeSJean-Christophe PLAGNIOL-VILLARD 			       struct pci_config_table *entry)
46293a686eeSJean-Christophe PLAGNIOL-VILLARD {
46393a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]);
46493a686eeSJean-Christophe PLAGNIOL-VILLARD }
46593a686eeSJean-Christophe PLAGNIOL-VILLARD 
46693a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose,
46793a686eeSJean-Christophe PLAGNIOL-VILLARD 			    pci_dev_t dev, struct pci_config_table *entry)
46893a686eeSJean-Christophe PLAGNIOL-VILLARD {
46993a686eeSJean-Christophe PLAGNIOL-VILLARD }
47093a686eeSJean-Christophe PLAGNIOL-VILLARD 
47193a686eeSJean-Christophe PLAGNIOL-VILLARD /*
47293a686eeSJean-Christophe PLAGNIOL-VILLARD  *
47393a686eeSJean-Christophe PLAGNIOL-VILLARD  */
47493a686eeSJean-Christophe PLAGNIOL-VILLARD 
47593a686eeSJean-Christophe PLAGNIOL-VILLARD /* HJF: Changed this to return int. I think this is required
47693a686eeSJean-Christophe PLAGNIOL-VILLARD  * to get the correct result when scanning bridges
47793a686eeSJean-Christophe PLAGNIOL-VILLARD  */
47893a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
47993a686eeSJean-Christophe PLAGNIOL-VILLARD extern void pciauto_config_init(struct pci_controller *hose);
48093a686eeSJean-Christophe PLAGNIOL-VILLARD 
481dc1da42fSStefan Roese int __pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
482dc1da42fSStefan Roese {
483dc1da42fSStefan Roese 	/*
484dc1da42fSStefan Roese 	 * Check if pci device should be skipped in configuration
485dc1da42fSStefan Roese 	 */
486dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
487dc1da42fSStefan Roese #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
488dc1da42fSStefan Roese 		/*
489dc1da42fSStefan Roese 		 * Only skip configuration if "pciconfighost" is not set
490dc1da42fSStefan Roese 		 */
491dc1da42fSStefan Roese 		if (getenv("pciconfighost") == NULL)
492dc1da42fSStefan Roese 			return 1;
493dc1da42fSStefan Roese #else
494dc1da42fSStefan Roese 		return 1;
495dc1da42fSStefan Roese #endif
496dc1da42fSStefan Roese 	}
497dc1da42fSStefan Roese 
498dc1da42fSStefan Roese 	return 0;
499dc1da42fSStefan Roese }
500dc1da42fSStefan Roese int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
501dc1da42fSStefan Roese 	__attribute__((weak, alias("__pci_skip_dev")));
502dc1da42fSStefan Roese 
503dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW
504dc1da42fSStefan Roese int __pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
505dc1da42fSStefan Roese {
506dc1da42fSStefan Roese 	if (dev == PCI_BDF(hose->first_busno, 0, 0))
507dc1da42fSStefan Roese 		return 0;
508dc1da42fSStefan Roese 
509dc1da42fSStefan Roese 	return 1;
510dc1da42fSStefan Roese }
511dc1da42fSStefan Roese int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
512dc1da42fSStefan Roese 	__attribute__((weak, alias("__pci_print_dev")));
513dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */
514dc1da42fSStefan Roese 
51593a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus)
51693a686eeSJean-Christophe PLAGNIOL-VILLARD {
51793a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned int sub_bus, found_multi=0;
51893a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned short vendor, device, class;
51993a686eeSJean-Christophe PLAGNIOL-VILLARD 	unsigned char header_type;
52093a686eeSJean-Christophe PLAGNIOL-VILLARD 	struct pci_config_table *cfg;
52193a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_dev_t dev;
52293a686eeSJean-Christophe PLAGNIOL-VILLARD 
52393a686eeSJean-Christophe PLAGNIOL-VILLARD 	sub_bus = bus;
52493a686eeSJean-Christophe PLAGNIOL-VILLARD 
52593a686eeSJean-Christophe PLAGNIOL-VILLARD 	for (dev =  PCI_BDF(bus,0,0);
52693a686eeSJean-Christophe PLAGNIOL-VILLARD 	     dev <  PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
527dc1da42fSStefan Roese 	     dev += PCI_BDF(0,0,1)) {
528dc1da42fSStefan Roese 
529dc1da42fSStefan Roese 		if (pci_skip_dev(hose, dev))
530dc1da42fSStefan Roese 			continue;
53193a686eeSJean-Christophe PLAGNIOL-VILLARD 
53293a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (PCI_FUNC(dev) && !found_multi)
53393a686eeSJean-Christophe PLAGNIOL-VILLARD 			continue;
53493a686eeSJean-Christophe PLAGNIOL-VILLARD 
53593a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
53693a686eeSJean-Christophe PLAGNIOL-VILLARD 
53793a686eeSJean-Christophe PLAGNIOL-VILLARD 		pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
53893a686eeSJean-Christophe PLAGNIOL-VILLARD 
53993a686eeSJean-Christophe PLAGNIOL-VILLARD 		if (vendor != 0xffff && vendor != 0x0000) {
54093a686eeSJean-Christophe PLAGNIOL-VILLARD 
54193a686eeSJean-Christophe PLAGNIOL-VILLARD 			if (!PCI_FUNC(dev))
54293a686eeSJean-Christophe PLAGNIOL-VILLARD 				found_multi = header_type & 0x80;
54393a686eeSJean-Christophe PLAGNIOL-VILLARD 
54493a686eeSJean-Christophe PLAGNIOL-VILLARD 			debug ("PCI Scan: Found Bus %d, Device %d, Function %d\n",
54593a686eeSJean-Christophe PLAGNIOL-VILLARD 				PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) );
54693a686eeSJean-Christophe PLAGNIOL-VILLARD 
54793a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
54893a686eeSJean-Christophe PLAGNIOL-VILLARD 			pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
54993a686eeSJean-Christophe PLAGNIOL-VILLARD 
55093a686eeSJean-Christophe PLAGNIOL-VILLARD 			cfg = pci_find_config(hose, class, vendor, device,
55193a686eeSJean-Christophe PLAGNIOL-VILLARD 					      PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
55293a686eeSJean-Christophe PLAGNIOL-VILLARD 			if (cfg) {
55393a686eeSJean-Christophe PLAGNIOL-VILLARD 				cfg->config_device(hose, dev, cfg);
55493a686eeSJean-Christophe PLAGNIOL-VILLARD 				sub_bus = max(sub_bus, hose->current_busno);
55593a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
55693a686eeSJean-Christophe PLAGNIOL-VILLARD 			} else {
55793a686eeSJean-Christophe PLAGNIOL-VILLARD 				int n = pciauto_config_device(hose, dev);
55893a686eeSJean-Christophe PLAGNIOL-VILLARD 
55993a686eeSJean-Christophe PLAGNIOL-VILLARD 				sub_bus = max(sub_bus, n);
56093a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
56193a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
56293a686eeSJean-Christophe PLAGNIOL-VILLARD 			if (hose->fixup_irq)
56393a686eeSJean-Christophe PLAGNIOL-VILLARD 				hose->fixup_irq(hose, dev);
56493a686eeSJean-Christophe PLAGNIOL-VILLARD 
56593a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_SCAN_SHOW
566dc1da42fSStefan Roese 			if (pci_print_dev(hose, dev)) {
56793a686eeSJean-Christophe PLAGNIOL-VILLARD 				unsigned char int_line;
56893a686eeSJean-Christophe PLAGNIOL-VILLARD 
56993a686eeSJean-Christophe PLAGNIOL-VILLARD 				pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE,
57093a686eeSJean-Christophe PLAGNIOL-VILLARD 							  &int_line);
57193a686eeSJean-Christophe PLAGNIOL-VILLARD 				printf("        %02x  %02x  %04x  %04x  %04x  %02x\n",
57293a686eeSJean-Christophe PLAGNIOL-VILLARD 				       PCI_BUS(dev), PCI_DEV(dev), vendor, device, class,
57393a686eeSJean-Christophe PLAGNIOL-VILLARD 				       int_line);
57493a686eeSJean-Christophe PLAGNIOL-VILLARD 			}
57593a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
57693a686eeSJean-Christophe PLAGNIOL-VILLARD 		}
57793a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
57893a686eeSJean-Christophe PLAGNIOL-VILLARD 
57993a686eeSJean-Christophe PLAGNIOL-VILLARD 	return sub_bus;
58093a686eeSJean-Christophe PLAGNIOL-VILLARD }
58193a686eeSJean-Christophe PLAGNIOL-VILLARD 
58293a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose)
58393a686eeSJean-Christophe PLAGNIOL-VILLARD {
58493a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* Start scan at current_busno.
58593a686eeSJean-Christophe PLAGNIOL-VILLARD 	 * PCIe will start scan at first_busno+1.
58693a686eeSJean-Christophe PLAGNIOL-VILLARD 	 */
58793a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* For legacy support, ensure current>=first */
58893a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (hose->first_busno > hose->current_busno)
58993a686eeSJean-Christophe PLAGNIOL-VILLARD 		hose->current_busno = hose->first_busno;
59093a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
59193a686eeSJean-Christophe PLAGNIOL-VILLARD 	pciauto_config_init(hose);
59293a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
59393a686eeSJean-Christophe PLAGNIOL-VILLARD 	return pci_hose_scan_bus(hose, hose->current_busno);
59493a686eeSJean-Christophe PLAGNIOL-VILLARD }
59593a686eeSJean-Christophe PLAGNIOL-VILLARD 
59693a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void)
59793a686eeSJean-Christophe PLAGNIOL-VILLARD {
59893a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_PCI_BOOTDELAY)
59993a686eeSJean-Christophe PLAGNIOL-VILLARD 	char *s;
60093a686eeSJean-Christophe PLAGNIOL-VILLARD 	int i;
60193a686eeSJean-Christophe PLAGNIOL-VILLARD 
60293a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* wait "pcidelay" ms (if defined)... */
60393a686eeSJean-Christophe PLAGNIOL-VILLARD 	s = getenv ("pcidelay");
60493a686eeSJean-Christophe PLAGNIOL-VILLARD 	if (s) {
60593a686eeSJean-Christophe PLAGNIOL-VILLARD 		int val = simple_strtoul (s, NULL, 10);
60693a686eeSJean-Christophe PLAGNIOL-VILLARD 		for (i=0; i<val; i++)
60793a686eeSJean-Christophe PLAGNIOL-VILLARD 			udelay (1000);
60893a686eeSJean-Christophe PLAGNIOL-VILLARD 	}
60993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif /* CONFIG_PCI_BOOTDELAY */
61093a686eeSJean-Christophe PLAGNIOL-VILLARD 
61193a686eeSJean-Christophe PLAGNIOL-VILLARD 	/* now call board specific pci_init()... */
61293a686eeSJean-Christophe PLAGNIOL-VILLARD 	pci_init_board();
61393a686eeSJean-Christophe PLAGNIOL-VILLARD }
614