193a686eeSJean-Christophe PLAGNIOL-VILLARD /* 293a686eeSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> 393a686eeSJean-Christophe PLAGNIOL-VILLARD * Andreas Heppel <aheppel@sysgo.de> 493a686eeSJean-Christophe PLAGNIOL-VILLARD * 593a686eeSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2002, 2003 693a686eeSJean-Christophe PLAGNIOL-VILLARD * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 793a686eeSJean-Christophe PLAGNIOL-VILLARD * 81a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 993a686eeSJean-Christophe PLAGNIOL-VILLARD */ 1093a686eeSJean-Christophe PLAGNIOL-VILLARD 1193a686eeSJean-Christophe PLAGNIOL-VILLARD /* 1293a686eeSJean-Christophe PLAGNIOL-VILLARD * PCI routines 1393a686eeSJean-Christophe PLAGNIOL-VILLARD */ 1493a686eeSJean-Christophe PLAGNIOL-VILLARD 1593a686eeSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 1693a686eeSJean-Christophe PLAGNIOL-VILLARD 1793a686eeSJean-Christophe PLAGNIOL-VILLARD #include <command.h> 1893a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/processor.h> 1993a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h> 2093a686eeSJean-Christophe PLAGNIOL-VILLARD #include <pci.h> 2193a686eeSJean-Christophe PLAGNIOL-VILLARD 2293a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_HOSE_OP(rw, size, type) \ 2393a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_##rw##_config_##size(struct pci_controller *hose, \ 2493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \ 2593a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type value) \ 2693a686eeSJean-Christophe PLAGNIOL-VILLARD { \ 2793a686eeSJean-Christophe PLAGNIOL-VILLARD return hose->rw##_##size(hose, dev, offset, value); \ 2893a686eeSJean-Christophe PLAGNIOL-VILLARD } 2993a686eeSJean-Christophe PLAGNIOL-VILLARD 3093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, byte, u8 *) 3193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, word, u16 *) 3293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, dword, u32 *) 3393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, byte, u8) 3493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, word, u16) 3593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, dword, u32) 3693a686eeSJean-Christophe PLAGNIOL-VILLARD 3793a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_OP(rw, size, type, error_code) \ 3893a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \ 3993a686eeSJean-Christophe PLAGNIOL-VILLARD { \ 4093a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ 4193a686eeSJean-Christophe PLAGNIOL-VILLARD \ 4293a686eeSJean-Christophe PLAGNIOL-VILLARD if (!hose) \ 4393a686eeSJean-Christophe PLAGNIOL-VILLARD { \ 4493a686eeSJean-Christophe PLAGNIOL-VILLARD error_code; \ 4593a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \ 4693a686eeSJean-Christophe PLAGNIOL-VILLARD } \ 4793a686eeSJean-Christophe PLAGNIOL-VILLARD \ 4893a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_hose_##rw##_config_##size(hose, dev, offset, value); \ 4993a686eeSJean-Christophe PLAGNIOL-VILLARD } 5093a686eeSJean-Christophe PLAGNIOL-VILLARD 5193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, byte, u8 *, *value = 0xff) 5293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, word, u16 *, *value = 0xffff) 5393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, dword, u32 *, *value = 0xffffffff) 5493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, byte, u8, ) 5593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, word, u16, ) 5693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, dword, u32, ) 5793a686eeSJean-Christophe PLAGNIOL-VILLARD 5893a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \ 5993a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\ 6093a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \ 6193a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type val) \ 6293a686eeSJean-Christophe PLAGNIOL-VILLARD { \ 6393a686eeSJean-Christophe PLAGNIOL-VILLARD u32 val32; \ 6493a686eeSJean-Christophe PLAGNIOL-VILLARD \ 6593a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \ 6693a686eeSJean-Christophe PLAGNIOL-VILLARD *val = -1; \ 6793a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \ 6893a686eeSJean-Christophe PLAGNIOL-VILLARD } \ 6993a686eeSJean-Christophe PLAGNIOL-VILLARD \ 7093a686eeSJean-Christophe PLAGNIOL-VILLARD *val = (val32 >> ((offset & (int)off_mask) * 8)); \ 7193a686eeSJean-Christophe PLAGNIOL-VILLARD \ 7293a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; \ 7393a686eeSJean-Christophe PLAGNIOL-VILLARD } 7493a686eeSJean-Christophe PLAGNIOL-VILLARD 7593a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \ 7693a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\ 7793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \ 7893a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type val) \ 7993a686eeSJean-Christophe PLAGNIOL-VILLARD { \ 8093a686eeSJean-Christophe PLAGNIOL-VILLARD u32 val32, mask, ldata, shift; \ 8193a686eeSJean-Christophe PLAGNIOL-VILLARD \ 8293a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\ 8393a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \ 8493a686eeSJean-Christophe PLAGNIOL-VILLARD \ 8593a686eeSJean-Christophe PLAGNIOL-VILLARD shift = ((offset & (int)off_mask) * 8); \ 8693a686eeSJean-Christophe PLAGNIOL-VILLARD ldata = (((unsigned long)val) & val_mask) << shift; \ 8793a686eeSJean-Christophe PLAGNIOL-VILLARD mask = val_mask << shift; \ 8893a686eeSJean-Christophe PLAGNIOL-VILLARD val32 = (val32 & ~mask) | ldata; \ 8993a686eeSJean-Christophe PLAGNIOL-VILLARD \ 9093a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\ 9193a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \ 9293a686eeSJean-Christophe PLAGNIOL-VILLARD \ 9393a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; \ 9493a686eeSJean-Christophe PLAGNIOL-VILLARD } 9593a686eeSJean-Christophe PLAGNIOL-VILLARD 9693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03) 9793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02) 9893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff) 9993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff) 10093a686eeSJean-Christophe PLAGNIOL-VILLARD 1016e61fae4SBecky Bruce /* Get a virtual address associated with a BAR region */ 1026e61fae4SBecky Bruce void *pci_map_bar(pci_dev_t pdev, int bar, int flags) 1036e61fae4SBecky Bruce { 1046e61fae4SBecky Bruce pci_addr_t pci_bus_addr; 105cf5787f2SKumar Gala u32 bar_response; 1066e61fae4SBecky Bruce 1076e61fae4SBecky Bruce /* read BAR address */ 1086e61fae4SBecky Bruce pci_read_config_dword(pdev, bar, &bar_response); 109cf5787f2SKumar Gala pci_bus_addr = (pci_addr_t)(bar_response & ~0xf); 1106e61fae4SBecky Bruce 1116e61fae4SBecky Bruce /* 1126e61fae4SBecky Bruce * Pass "0" as the length argument to pci_bus_to_virt. The arg 1136e61fae4SBecky Bruce * isn't actualy used on any platform because u-boot assumes a static 1146e61fae4SBecky Bruce * linear mapping. In the future, this could read the BAR size 1156e61fae4SBecky Bruce * and pass that as the size if needed. 1166e61fae4SBecky Bruce */ 1176e61fae4SBecky Bruce return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE); 1186e61fae4SBecky Bruce } 1196e61fae4SBecky Bruce 12093a686eeSJean-Christophe PLAGNIOL-VILLARD /* 12193a686eeSJean-Christophe PLAGNIOL-VILLARD * 12293a686eeSJean-Christophe PLAGNIOL-VILLARD */ 12393a686eeSJean-Christophe PLAGNIOL-VILLARD 12496d61603SJohn Schmoller static struct pci_controller* hose_head; 12593a686eeSJean-Christophe PLAGNIOL-VILLARD 12693a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose) 12793a686eeSJean-Christophe PLAGNIOL-VILLARD { 12893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller **phose = &hose_head; 12993a686eeSJean-Christophe PLAGNIOL-VILLARD 13093a686eeSJean-Christophe PLAGNIOL-VILLARD while(*phose) 13193a686eeSJean-Christophe PLAGNIOL-VILLARD phose = &(*phose)->next; 13293a686eeSJean-Christophe PLAGNIOL-VILLARD 13393a686eeSJean-Christophe PLAGNIOL-VILLARD hose->next = NULL; 13493a686eeSJean-Christophe PLAGNIOL-VILLARD 13593a686eeSJean-Christophe PLAGNIOL-VILLARD *phose = hose; 13693a686eeSJean-Christophe PLAGNIOL-VILLARD } 13793a686eeSJean-Christophe PLAGNIOL-VILLARD 13893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose(int bus) 13993a686eeSJean-Christophe PLAGNIOL-VILLARD { 14093a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *hose; 14193a686eeSJean-Christophe PLAGNIOL-VILLARD 142cb2bf931SAndrew Sharp for (hose = hose_head; hose; hose = hose->next) { 14393a686eeSJean-Christophe PLAGNIOL-VILLARD if (bus >= hose->first_busno && bus <= hose->last_busno) 14493a686eeSJean-Christophe PLAGNIOL-VILLARD return hose; 145cb2bf931SAndrew Sharp } 14693a686eeSJean-Christophe PLAGNIOL-VILLARD 14793a686eeSJean-Christophe PLAGNIOL-VILLARD printf("pci_bus_to_hose() failed\n"); 14893a686eeSJean-Christophe PLAGNIOL-VILLARD return NULL; 14993a686eeSJean-Christophe PLAGNIOL-VILLARD } 15093a686eeSJean-Christophe PLAGNIOL-VILLARD 1513a0e3c27SKumar Gala struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr) 1523a0e3c27SKumar Gala { 1533a0e3c27SKumar Gala struct pci_controller *hose; 1543a0e3c27SKumar Gala 1553a0e3c27SKumar Gala for (hose = hose_head; hose; hose = hose->next) { 1563a0e3c27SKumar Gala if (hose->cfg_addr == cfg_addr) 1573a0e3c27SKumar Gala return hose; 1583a0e3c27SKumar Gala } 1593a0e3c27SKumar Gala 1603a0e3c27SKumar Gala return NULL; 1613a0e3c27SKumar Gala } 1623a0e3c27SKumar Gala 163cc2a8c77SAnton Vorontsov int pci_last_busno(void) 164cc2a8c77SAnton Vorontsov { 165cc2a8c77SAnton Vorontsov struct pci_controller *hose = hose_head; 166cc2a8c77SAnton Vorontsov 167cc2a8c77SAnton Vorontsov if (!hose) 168cc2a8c77SAnton Vorontsov return -1; 169cc2a8c77SAnton Vorontsov 170cc2a8c77SAnton Vorontsov while (hose->next) 171cc2a8c77SAnton Vorontsov hose = hose->next; 172cc2a8c77SAnton Vorontsov 173cc2a8c77SAnton Vorontsov return hose->last_busno; 174cc2a8c77SAnton Vorontsov } 175cc2a8c77SAnton Vorontsov 17693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index) 17793a686eeSJean-Christophe PLAGNIOL-VILLARD { 17893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller * hose; 17993a686eeSJean-Christophe PLAGNIOL-VILLARD u16 vendor, device; 18093a686eeSJean-Christophe PLAGNIOL-VILLARD u8 header_type; 18193a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t bdf; 18293a686eeSJean-Christophe PLAGNIOL-VILLARD int i, bus, found_multi = 0; 18393a686eeSJean-Christophe PLAGNIOL-VILLARD 184cb2bf931SAndrew Sharp for (hose = hose_head; hose; hose = hose->next) { 1856d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE 18693a686eeSJean-Christophe PLAGNIOL-VILLARD for (bus = hose->last_busno; bus >= hose->first_busno; bus--) 18793a686eeSJean-Christophe PLAGNIOL-VILLARD #else 18893a686eeSJean-Christophe PLAGNIOL-VILLARD for (bus = hose->first_busno; bus <= hose->last_busno; bus++) 18993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif 19093a686eeSJean-Christophe PLAGNIOL-VILLARD for (bdf = PCI_BDF(bus, 0, 0); 19193a686eeSJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX) 192cb2bf931SAndrew Sharp bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1, 193cb2bf931SAndrew Sharp PCI_MAX_PCI_FUNCTIONS - 1); 19493a686eeSJean-Christophe PLAGNIOL-VILLARD #else 19593a686eeSJean-Christophe PLAGNIOL-VILLARD bdf < PCI_BDF(bus + 1, 0, 0); 19693a686eeSJean-Christophe PLAGNIOL-VILLARD #endif 197cb2bf931SAndrew Sharp bdf += PCI_BDF(0, 0, 1)) { 198*4efe52bfSThierry Reding if (pci_skip_dev(hose, bdf)) 199*4efe52bfSThierry Reding continue; 200*4efe52bfSThierry Reding 20193a686eeSJean-Christophe PLAGNIOL-VILLARD if (!PCI_FUNC(bdf)) { 20293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_read_config_byte(bdf, 20393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HEADER_TYPE, 20493a686eeSJean-Christophe PLAGNIOL-VILLARD &header_type); 20593a686eeSJean-Christophe PLAGNIOL-VILLARD 20693a686eeSJean-Christophe PLAGNIOL-VILLARD found_multi = header_type & 0x80; 20793a686eeSJean-Christophe PLAGNIOL-VILLARD } else { 20893a686eeSJean-Christophe PLAGNIOL-VILLARD if (!found_multi) 20993a686eeSJean-Christophe PLAGNIOL-VILLARD continue; 21093a686eeSJean-Christophe PLAGNIOL-VILLARD } 21193a686eeSJean-Christophe PLAGNIOL-VILLARD 21293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_read_config_word(bdf, 21393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_VENDOR_ID, 21493a686eeSJean-Christophe PLAGNIOL-VILLARD &vendor); 21593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_read_config_word(bdf, 21693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_DEVICE_ID, 21793a686eeSJean-Christophe PLAGNIOL-VILLARD &device); 21893a686eeSJean-Christophe PLAGNIOL-VILLARD 219cb2bf931SAndrew Sharp for (i = 0; ids[i].vendor != 0; i++) { 22093a686eeSJean-Christophe PLAGNIOL-VILLARD if (vendor == ids[i].vendor && 221cb2bf931SAndrew Sharp device == ids[i].device) { 22293a686eeSJean-Christophe PLAGNIOL-VILLARD if (index <= 0) 22393a686eeSJean-Christophe PLAGNIOL-VILLARD return bdf; 22493a686eeSJean-Christophe PLAGNIOL-VILLARD 22593a686eeSJean-Christophe PLAGNIOL-VILLARD index--; 22693a686eeSJean-Christophe PLAGNIOL-VILLARD } 22793a686eeSJean-Christophe PLAGNIOL-VILLARD } 22893a686eeSJean-Christophe PLAGNIOL-VILLARD } 229cb2bf931SAndrew Sharp } 23093a686eeSJean-Christophe PLAGNIOL-VILLARD 231cb2bf931SAndrew Sharp return -1; 23293a686eeSJean-Christophe PLAGNIOL-VILLARD } 23393a686eeSJean-Christophe PLAGNIOL-VILLARD 23493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index) 23593a686eeSJean-Christophe PLAGNIOL-VILLARD { 23693a686eeSJean-Christophe PLAGNIOL-VILLARD static struct pci_device_id ids[2] = {{}, {0, 0}}; 23793a686eeSJean-Christophe PLAGNIOL-VILLARD 23893a686eeSJean-Christophe PLAGNIOL-VILLARD ids[0].vendor = vendor; 23993a686eeSJean-Christophe PLAGNIOL-VILLARD ids[0].device = device; 24093a686eeSJean-Christophe PLAGNIOL-VILLARD 24193a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_find_devices(ids, index); 24293a686eeSJean-Christophe PLAGNIOL-VILLARD } 24393a686eeSJean-Christophe PLAGNIOL-VILLARD 24493a686eeSJean-Christophe PLAGNIOL-VILLARD /* 24593a686eeSJean-Christophe PLAGNIOL-VILLARD * 24693a686eeSJean-Christophe PLAGNIOL-VILLARD */ 24793a686eeSJean-Christophe PLAGNIOL-VILLARD 2482d43e873SKumar Gala int __pci_hose_phys_to_bus(struct pci_controller *hose, 24936f32675SBecky Bruce phys_addr_t phys_addr, 2502d43e873SKumar Gala unsigned long flags, 2512d43e873SKumar Gala unsigned long skip_mask, 2522d43e873SKumar Gala pci_addr_t *ba) 25393a686eeSJean-Christophe PLAGNIOL-VILLARD { 25493a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_region *res; 25530e76d5eSKumar Gala pci_addr_t bus_addr; 25693a686eeSJean-Christophe PLAGNIOL-VILLARD int i; 25793a686eeSJean-Christophe PLAGNIOL-VILLARD 25893a686eeSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < hose->region_count; i++) { 25993a686eeSJean-Christophe PLAGNIOL-VILLARD res = &hose->regions[i]; 26093a686eeSJean-Christophe PLAGNIOL-VILLARD 26193a686eeSJean-Christophe PLAGNIOL-VILLARD if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) 26293a686eeSJean-Christophe PLAGNIOL-VILLARD continue; 26393a686eeSJean-Christophe PLAGNIOL-VILLARD 2642d43e873SKumar Gala if (res->flags & skip_mask) 2652d43e873SKumar Gala continue; 2662d43e873SKumar Gala 26793a686eeSJean-Christophe PLAGNIOL-VILLARD bus_addr = phys_addr - res->phys_start + res->bus_start; 26893a686eeSJean-Christophe PLAGNIOL-VILLARD 26993a686eeSJean-Christophe PLAGNIOL-VILLARD if (bus_addr >= res->bus_start && 27093a686eeSJean-Christophe PLAGNIOL-VILLARD bus_addr < res->bus_start + res->size) { 2712d43e873SKumar Gala *ba = bus_addr; 27293a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; 27393a686eeSJean-Christophe PLAGNIOL-VILLARD } 2742d43e873SKumar Gala } 27593a686eeSJean-Christophe PLAGNIOL-VILLARD 2762d43e873SKumar Gala return 1; 2772d43e873SKumar Gala } 2782d43e873SKumar Gala 2792d43e873SKumar Gala pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose, 2802d43e873SKumar Gala phys_addr_t phys_addr, 28193a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned long flags) 28293a686eeSJean-Christophe PLAGNIOL-VILLARD { 2832d43e873SKumar Gala pci_addr_t bus_addr = 0; 2842d43e873SKumar Gala int ret; 2852d43e873SKumar Gala 2862d43e873SKumar Gala if (!hose) { 2872d43e873SKumar Gala puts("pci_hose_phys_to_bus: invalid hose\n"); 2882d43e873SKumar Gala return bus_addr; 2892d43e873SKumar Gala } 2902d43e873SKumar Gala 291cb2bf931SAndrew Sharp /* 292cb2bf931SAndrew Sharp * if PCI_REGION_MEM is set we do a two pass search with preference 293cb2bf931SAndrew Sharp * on matches that don't have PCI_REGION_SYS_MEMORY set 294cb2bf931SAndrew Sharp */ 2952d43e873SKumar Gala if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) { 2962d43e873SKumar Gala ret = __pci_hose_phys_to_bus(hose, phys_addr, 2972d43e873SKumar Gala flags, PCI_REGION_SYS_MEMORY, &bus_addr); 2982d43e873SKumar Gala if (!ret) 2992d43e873SKumar Gala return bus_addr; 3002d43e873SKumar Gala } 3012d43e873SKumar Gala 3022d43e873SKumar Gala ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr); 3032d43e873SKumar Gala 3042d43e873SKumar Gala if (ret) 3052d43e873SKumar Gala puts("pci_hose_phys_to_bus: invalid physical address\n"); 3062d43e873SKumar Gala 3072d43e873SKumar Gala return bus_addr; 3082d43e873SKumar Gala } 3092d43e873SKumar Gala 3102d43e873SKumar Gala int __pci_hose_bus_to_phys(struct pci_controller *hose, 3112d43e873SKumar Gala pci_addr_t bus_addr, 3122d43e873SKumar Gala unsigned long flags, 3132d43e873SKumar Gala unsigned long skip_mask, 3142d43e873SKumar Gala phys_addr_t *pa) 3152d43e873SKumar Gala { 31693a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_region *res; 31793a686eeSJean-Christophe PLAGNIOL-VILLARD int i; 31893a686eeSJean-Christophe PLAGNIOL-VILLARD 31993a686eeSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < hose->region_count; i++) { 32093a686eeSJean-Christophe PLAGNIOL-VILLARD res = &hose->regions[i]; 32193a686eeSJean-Christophe PLAGNIOL-VILLARD 32293a686eeSJean-Christophe PLAGNIOL-VILLARD if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) 32393a686eeSJean-Christophe PLAGNIOL-VILLARD continue; 32493a686eeSJean-Christophe PLAGNIOL-VILLARD 3252d43e873SKumar Gala if (res->flags & skip_mask) 3262d43e873SKumar Gala continue; 3272d43e873SKumar Gala 32893a686eeSJean-Christophe PLAGNIOL-VILLARD if (bus_addr >= res->bus_start && 329d878c9a9SStephen Warren (bus_addr - res->bus_start) < res->size) { 3302d43e873SKumar Gala *pa = (bus_addr - res->bus_start + res->phys_start); 33193a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; 33293a686eeSJean-Christophe PLAGNIOL-VILLARD } 3332d43e873SKumar Gala } 3342d43e873SKumar Gala 3352d43e873SKumar Gala return 1; 3362d43e873SKumar Gala } 3372d43e873SKumar Gala 3382d43e873SKumar Gala phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose, 3392d43e873SKumar Gala pci_addr_t bus_addr, 3402d43e873SKumar Gala unsigned long flags) 3412d43e873SKumar Gala { 3422d43e873SKumar Gala phys_addr_t phys_addr = 0; 3432d43e873SKumar Gala int ret; 3442d43e873SKumar Gala 3452d43e873SKumar Gala if (!hose) { 3462d43e873SKumar Gala puts("pci_hose_bus_to_phys: invalid hose\n"); 3472d43e873SKumar Gala return phys_addr; 3482d43e873SKumar Gala } 3492d43e873SKumar Gala 350cb2bf931SAndrew Sharp /* 351cb2bf931SAndrew Sharp * if PCI_REGION_MEM is set we do a two pass search with preference 352cb2bf931SAndrew Sharp * on matches that don't have PCI_REGION_SYS_MEMORY set 353cb2bf931SAndrew Sharp */ 3542d43e873SKumar Gala if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) { 3552d43e873SKumar Gala ret = __pci_hose_bus_to_phys(hose, bus_addr, 3562d43e873SKumar Gala flags, PCI_REGION_SYS_MEMORY, &phys_addr); 3572d43e873SKumar Gala if (!ret) 3582d43e873SKumar Gala return phys_addr; 3592d43e873SKumar Gala } 3602d43e873SKumar Gala 3612d43e873SKumar Gala ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr); 3622d43e873SKumar Gala 3632d43e873SKumar Gala if (ret) 3642d43e873SKumar Gala puts("pci_hose_bus_to_phys: invalid physical address\n"); 3652d43e873SKumar Gala 3662d43e873SKumar Gala return phys_addr; 3672d43e873SKumar Gala } 36893a686eeSJean-Christophe PLAGNIOL-VILLARD 36993a686eeSJean-Christophe PLAGNIOL-VILLARD /* 37093a686eeSJean-Christophe PLAGNIOL-VILLARD * 37193a686eeSJean-Christophe PLAGNIOL-VILLARD */ 37293a686eeSJean-Christophe PLAGNIOL-VILLARD 37393a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_config_device(struct pci_controller *hose, 37493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, 37593a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned long io, 37630e76d5eSKumar Gala pci_addr_t mem, 37793a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned long command) 37893a686eeSJean-Christophe PLAGNIOL-VILLARD { 379cf5787f2SKumar Gala u32 bar_response; 380af778c6dSAndrew Sharp unsigned int old_command; 38130e76d5eSKumar Gala pci_addr_t bar_value; 38230e76d5eSKumar Gala pci_size_t bar_size; 38393a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned char pin; 38493a686eeSJean-Christophe PLAGNIOL-VILLARD int bar, found_mem64; 38593a686eeSJean-Christophe PLAGNIOL-VILLARD 386cb2bf931SAndrew Sharp debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io, 387cb2bf931SAndrew Sharp (u64)mem, command); 38893a686eeSJean-Christophe PLAGNIOL-VILLARD 38993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0); 39093a686eeSJean-Christophe PLAGNIOL-VILLARD 391252b404dSWolfgang Denk for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) { 39293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); 39393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_dword(hose, dev, bar, &bar_response); 39493a686eeSJean-Christophe PLAGNIOL-VILLARD 39593a686eeSJean-Christophe PLAGNIOL-VILLARD if (!bar_response) 39693a686eeSJean-Christophe PLAGNIOL-VILLARD continue; 39793a686eeSJean-Christophe PLAGNIOL-VILLARD 39893a686eeSJean-Christophe PLAGNIOL-VILLARD found_mem64 = 0; 39993a686eeSJean-Christophe PLAGNIOL-VILLARD 40093a686eeSJean-Christophe PLAGNIOL-VILLARD /* Check the BAR type and set our address mask */ 40193a686eeSJean-Christophe PLAGNIOL-VILLARD if (bar_response & PCI_BASE_ADDRESS_SPACE) { 40293a686eeSJean-Christophe PLAGNIOL-VILLARD bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1; 40393a686eeSJean-Christophe PLAGNIOL-VILLARD /* round up region base address to a multiple of size */ 40493a686eeSJean-Christophe PLAGNIOL-VILLARD io = ((io - 1) | (bar_size - 1)) + 1; 40593a686eeSJean-Christophe PLAGNIOL-VILLARD bar_value = io; 40693a686eeSJean-Christophe PLAGNIOL-VILLARD /* compute new region base address */ 40793a686eeSJean-Christophe PLAGNIOL-VILLARD io = io + bar_size; 40893a686eeSJean-Christophe PLAGNIOL-VILLARD } else { 40993a686eeSJean-Christophe PLAGNIOL-VILLARD if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == 41030e76d5eSKumar Gala PCI_BASE_ADDRESS_MEM_TYPE_64) { 41130e76d5eSKumar Gala u32 bar_response_upper; 41230e76d5eSKumar Gala u64 bar64; 413cb2bf931SAndrew Sharp pci_hose_write_config_dword(hose, dev, bar + 4, 414cb2bf931SAndrew Sharp 0xffffffff); 415cb2bf931SAndrew Sharp pci_hose_read_config_dword(hose, dev, bar + 4, 416cb2bf931SAndrew Sharp &bar_response_upper); 41793a686eeSJean-Christophe PLAGNIOL-VILLARD 41830e76d5eSKumar Gala bar64 = ((u64)bar_response_upper << 32) | bar_response; 41930e76d5eSKumar Gala 42030e76d5eSKumar Gala bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; 42130e76d5eSKumar Gala found_mem64 = 1; 42230e76d5eSKumar Gala } else { 42330e76d5eSKumar Gala bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); 42430e76d5eSKumar Gala } 42593a686eeSJean-Christophe PLAGNIOL-VILLARD 42693a686eeSJean-Christophe PLAGNIOL-VILLARD /* round up region base address to multiple of size */ 42793a686eeSJean-Christophe PLAGNIOL-VILLARD mem = ((mem - 1) | (bar_size - 1)) + 1; 42893a686eeSJean-Christophe PLAGNIOL-VILLARD bar_value = mem; 42993a686eeSJean-Christophe PLAGNIOL-VILLARD /* compute new region base address */ 43093a686eeSJean-Christophe PLAGNIOL-VILLARD mem = mem + bar_size; 43193a686eeSJean-Christophe PLAGNIOL-VILLARD } 43293a686eeSJean-Christophe PLAGNIOL-VILLARD 43393a686eeSJean-Christophe PLAGNIOL-VILLARD /* Write it out and update our limit */ 43430e76d5eSKumar Gala pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value); 43593a686eeSJean-Christophe PLAGNIOL-VILLARD 43693a686eeSJean-Christophe PLAGNIOL-VILLARD if (found_mem64) { 43793a686eeSJean-Christophe PLAGNIOL-VILLARD bar += 4; 43830e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT 439cb2bf931SAndrew Sharp pci_hose_write_config_dword(hose, dev, bar, 440cb2bf931SAndrew Sharp (u32)(bar_value >> 32)); 44130e76d5eSKumar Gala #else 44293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, bar, 0x00000000); 44330e76d5eSKumar Gala #endif 44493a686eeSJean-Christophe PLAGNIOL-VILLARD } 44593a686eeSJean-Christophe PLAGNIOL-VILLARD } 44693a686eeSJean-Christophe PLAGNIOL-VILLARD 44793a686eeSJean-Christophe PLAGNIOL-VILLARD /* Configure Cache Line Size Register */ 44893a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08); 44993a686eeSJean-Christophe PLAGNIOL-VILLARD 45093a686eeSJean-Christophe PLAGNIOL-VILLARD /* Configure Latency Timer */ 45193a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); 45293a686eeSJean-Christophe PLAGNIOL-VILLARD 45393a686eeSJean-Christophe PLAGNIOL-VILLARD /* Disable interrupt line, if device says it wants to use interrupts */ 45493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin); 45593a686eeSJean-Christophe PLAGNIOL-VILLARD if (pin != 0) { 45693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff); 45793a686eeSJean-Christophe PLAGNIOL-VILLARD } 45893a686eeSJean-Christophe PLAGNIOL-VILLARD 45993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command); 46093a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 46193a686eeSJean-Christophe PLAGNIOL-VILLARD (old_command & 0xffff0000) | command); 46293a686eeSJean-Christophe PLAGNIOL-VILLARD 46393a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; 46493a686eeSJean-Christophe PLAGNIOL-VILLARD } 46593a686eeSJean-Christophe PLAGNIOL-VILLARD 46693a686eeSJean-Christophe PLAGNIOL-VILLARD /* 46793a686eeSJean-Christophe PLAGNIOL-VILLARD * 46893a686eeSJean-Christophe PLAGNIOL-VILLARD */ 46993a686eeSJean-Christophe PLAGNIOL-VILLARD 47093a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose, 47193a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned short class, 47293a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int vendor, 47393a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int device, 47493a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int bus, 47593a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int dev, 47693a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int func) 47793a686eeSJean-Christophe PLAGNIOL-VILLARD { 47893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *table; 47993a686eeSJean-Christophe PLAGNIOL-VILLARD 48093a686eeSJean-Christophe PLAGNIOL-VILLARD for (table = hose->config_table; table && table->vendor; table++) { 48193a686eeSJean-Christophe PLAGNIOL-VILLARD if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) && 48293a686eeSJean-Christophe PLAGNIOL-VILLARD (table->device == PCI_ANY_ID || table->device == device) && 48393a686eeSJean-Christophe PLAGNIOL-VILLARD (table->class == PCI_ANY_ID || table->class == class) && 48493a686eeSJean-Christophe PLAGNIOL-VILLARD (table->bus == PCI_ANY_ID || table->bus == bus) && 48593a686eeSJean-Christophe PLAGNIOL-VILLARD (table->dev == PCI_ANY_ID || table->dev == dev) && 48693a686eeSJean-Christophe PLAGNIOL-VILLARD (table->func == PCI_ANY_ID || table->func == func)) { 48793a686eeSJean-Christophe PLAGNIOL-VILLARD return table; 48893a686eeSJean-Christophe PLAGNIOL-VILLARD } 48993a686eeSJean-Christophe PLAGNIOL-VILLARD } 49093a686eeSJean-Christophe PLAGNIOL-VILLARD 49193a686eeSJean-Christophe PLAGNIOL-VILLARD return NULL; 49293a686eeSJean-Christophe PLAGNIOL-VILLARD } 49393a686eeSJean-Christophe PLAGNIOL-VILLARD 49493a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose, 49593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, 49693a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *entry) 49793a686eeSJean-Christophe PLAGNIOL-VILLARD { 498cb2bf931SAndrew Sharp pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], 499cb2bf931SAndrew Sharp entry->priv[2]); 50093a686eeSJean-Christophe PLAGNIOL-VILLARD } 50193a686eeSJean-Christophe PLAGNIOL-VILLARD 50293a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose, 50393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, struct pci_config_table *entry) 50493a686eeSJean-Christophe PLAGNIOL-VILLARD { 50593a686eeSJean-Christophe PLAGNIOL-VILLARD } 50693a686eeSJean-Christophe PLAGNIOL-VILLARD 50793a686eeSJean-Christophe PLAGNIOL-VILLARD /* 508cb2bf931SAndrew Sharp * HJF: Changed this to return int. I think this is required 50993a686eeSJean-Christophe PLAGNIOL-VILLARD * to get the correct result when scanning bridges 51093a686eeSJean-Christophe PLAGNIOL-VILLARD */ 51193a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev); 51293a686eeSJean-Christophe PLAGNIOL-VILLARD 513983eb9d1SPeter Tyser #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW) 514983eb9d1SPeter Tyser const char * pci_class_str(u8 class) 515983eb9d1SPeter Tyser { 516983eb9d1SPeter Tyser switch (class) { 517983eb9d1SPeter Tyser case PCI_CLASS_NOT_DEFINED: 518983eb9d1SPeter Tyser return "Build before PCI Rev2.0"; 519983eb9d1SPeter Tyser break; 520983eb9d1SPeter Tyser case PCI_BASE_CLASS_STORAGE: 521983eb9d1SPeter Tyser return "Mass storage controller"; 522983eb9d1SPeter Tyser break; 523983eb9d1SPeter Tyser case PCI_BASE_CLASS_NETWORK: 524983eb9d1SPeter Tyser return "Network controller"; 525983eb9d1SPeter Tyser break; 526983eb9d1SPeter Tyser case PCI_BASE_CLASS_DISPLAY: 527983eb9d1SPeter Tyser return "Display controller"; 528983eb9d1SPeter Tyser break; 529983eb9d1SPeter Tyser case PCI_BASE_CLASS_MULTIMEDIA: 530983eb9d1SPeter Tyser return "Multimedia device"; 531983eb9d1SPeter Tyser break; 532983eb9d1SPeter Tyser case PCI_BASE_CLASS_MEMORY: 533983eb9d1SPeter Tyser return "Memory controller"; 534983eb9d1SPeter Tyser break; 535983eb9d1SPeter Tyser case PCI_BASE_CLASS_BRIDGE: 536983eb9d1SPeter Tyser return "Bridge device"; 537983eb9d1SPeter Tyser break; 538983eb9d1SPeter Tyser case PCI_BASE_CLASS_COMMUNICATION: 539983eb9d1SPeter Tyser return "Simple comm. controller"; 540983eb9d1SPeter Tyser break; 541983eb9d1SPeter Tyser case PCI_BASE_CLASS_SYSTEM: 542983eb9d1SPeter Tyser return "Base system peripheral"; 543983eb9d1SPeter Tyser break; 544983eb9d1SPeter Tyser case PCI_BASE_CLASS_INPUT: 545983eb9d1SPeter Tyser return "Input device"; 546983eb9d1SPeter Tyser break; 547983eb9d1SPeter Tyser case PCI_BASE_CLASS_DOCKING: 548983eb9d1SPeter Tyser return "Docking station"; 549983eb9d1SPeter Tyser break; 550983eb9d1SPeter Tyser case PCI_BASE_CLASS_PROCESSOR: 551983eb9d1SPeter Tyser return "Processor"; 552983eb9d1SPeter Tyser break; 553983eb9d1SPeter Tyser case PCI_BASE_CLASS_SERIAL: 554983eb9d1SPeter Tyser return "Serial bus controller"; 555983eb9d1SPeter Tyser break; 556983eb9d1SPeter Tyser case PCI_BASE_CLASS_INTELLIGENT: 557983eb9d1SPeter Tyser return "Intelligent controller"; 558983eb9d1SPeter Tyser break; 559983eb9d1SPeter Tyser case PCI_BASE_CLASS_SATELLITE: 560983eb9d1SPeter Tyser return "Satellite controller"; 561983eb9d1SPeter Tyser break; 562983eb9d1SPeter Tyser case PCI_BASE_CLASS_CRYPT: 563983eb9d1SPeter Tyser return "Cryptographic device"; 564983eb9d1SPeter Tyser break; 565983eb9d1SPeter Tyser case PCI_BASE_CLASS_SIGNAL_PROCESSING: 566983eb9d1SPeter Tyser return "DSP"; 567983eb9d1SPeter Tyser break; 568983eb9d1SPeter Tyser case PCI_CLASS_OTHERS: 569983eb9d1SPeter Tyser return "Does not fit any class"; 570983eb9d1SPeter Tyser break; 571983eb9d1SPeter Tyser default: 572983eb9d1SPeter Tyser return "???"; 573983eb9d1SPeter Tyser break; 574983eb9d1SPeter Tyser }; 575983eb9d1SPeter Tyser } 576983eb9d1SPeter Tyser #endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */ 577983eb9d1SPeter Tyser 5787b19fd6dSJeroen Hofstee __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev) 579dc1da42fSStefan Roese { 580dc1da42fSStefan Roese /* 581dc1da42fSStefan Roese * Check if pci device should be skipped in configuration 582dc1da42fSStefan Roese */ 583dc1da42fSStefan Roese if (dev == PCI_BDF(hose->first_busno, 0, 0)) { 584dc1da42fSStefan Roese #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */ 585dc1da42fSStefan Roese /* 586dc1da42fSStefan Roese * Only skip configuration if "pciconfighost" is not set 587dc1da42fSStefan Roese */ 588dc1da42fSStefan Roese if (getenv("pciconfighost") == NULL) 589dc1da42fSStefan Roese return 1; 590dc1da42fSStefan Roese #else 591dc1da42fSStefan Roese return 1; 592dc1da42fSStefan Roese #endif 593dc1da42fSStefan Roese } 594dc1da42fSStefan Roese 595dc1da42fSStefan Roese return 0; 596dc1da42fSStefan Roese } 597dc1da42fSStefan Roese 598dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW 5997b19fd6dSJeroen Hofstee __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev) 600dc1da42fSStefan Roese { 601dc1da42fSStefan Roese if (dev == PCI_BDF(hose->first_busno, 0, 0)) 602dc1da42fSStefan Roese return 0; 603dc1da42fSStefan Roese 604dc1da42fSStefan Roese return 1; 605dc1da42fSStefan Roese } 606dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */ 607dc1da42fSStefan Roese 60893a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus) 60993a686eeSJean-Christophe PLAGNIOL-VILLARD { 61093a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int sub_bus, found_multi = 0; 61193a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned short vendor, device, class; 61293a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned char header_type; 61303992ac2SAndrew Sharp #ifndef CONFIG_PCI_PNP 61493a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *cfg; 61503992ac2SAndrew Sharp #endif 61693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev; 617009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW 618009884aeSPeter Tyser static int indent = 0; 619009884aeSPeter Tyser #endif 62093a686eeSJean-Christophe PLAGNIOL-VILLARD 62193a686eeSJean-Christophe PLAGNIOL-VILLARD sub_bus = bus; 62293a686eeSJean-Christophe PLAGNIOL-VILLARD 62393a686eeSJean-Christophe PLAGNIOL-VILLARD for (dev = PCI_BDF(bus,0,0); 624cb2bf931SAndrew Sharp dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1, 625cb2bf931SAndrew Sharp PCI_MAX_PCI_FUNCTIONS - 1); 626dc1da42fSStefan Roese dev += PCI_BDF(0, 0, 1)) { 627dc1da42fSStefan Roese 628dc1da42fSStefan Roese if (pci_skip_dev(hose, dev)) 629dc1da42fSStefan Roese continue; 63093a686eeSJean-Christophe PLAGNIOL-VILLARD 63193a686eeSJean-Christophe PLAGNIOL-VILLARD if (PCI_FUNC(dev) && !found_multi) 63293a686eeSJean-Christophe PLAGNIOL-VILLARD continue; 63393a686eeSJean-Christophe PLAGNIOL-VILLARD 63493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); 63593a686eeSJean-Christophe PLAGNIOL-VILLARD 63693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor); 63793a686eeSJean-Christophe PLAGNIOL-VILLARD 638983eb9d1SPeter Tyser if (vendor == 0xffff || vendor == 0x0000) 639983eb9d1SPeter Tyser continue; 64093a686eeSJean-Christophe PLAGNIOL-VILLARD 64193a686eeSJean-Christophe PLAGNIOL-VILLARD if (!PCI_FUNC(dev)) 64293a686eeSJean-Christophe PLAGNIOL-VILLARD found_multi = header_type & 0x80; 64393a686eeSJean-Christophe PLAGNIOL-VILLARD 64493a686eeSJean-Christophe PLAGNIOL-VILLARD debug("PCI Scan: Found Bus %d, Device %d, Function %d\n", 64593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); 64693a686eeSJean-Christophe PLAGNIOL-VILLARD 64793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device); 64893a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); 64993a686eeSJean-Christophe PLAGNIOL-VILLARD 6500991866cSTim Harvey #ifdef CONFIG_PCI_FIXUP_DEV 6510991866cSTim Harvey board_pci_fixup_dev(hose, dev, vendor, device, class); 6520991866cSTim Harvey #endif 6530991866cSTim Harvey 654a38d216eSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW 655009884aeSPeter Tyser indent++; 656009884aeSPeter Tyser 657009884aeSPeter Tyser /* Print leading space, including bus indentation */ 658009884aeSPeter Tyser printf("%*c", indent + 1, ' '); 659009884aeSPeter Tyser 660a38d216eSPeter Tyser if (pci_print_dev(hose, dev)) { 661009884aeSPeter Tyser printf("%02x:%02x.%-*x - %04x:%04x - %s\n", 662009884aeSPeter Tyser PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev), 663a38d216eSPeter Tyser vendor, device, pci_class_str(class >> 8)); 664a38d216eSPeter Tyser } 665a38d216eSPeter Tyser #endif 666a38d216eSPeter Tyser 66703992ac2SAndrew Sharp #ifdef CONFIG_PCI_PNP 668b4141195SMasahiro Yamada sub_bus = max((unsigned int)pciauto_config_device(hose, dev), 669b4141195SMasahiro Yamada sub_bus); 67003992ac2SAndrew Sharp #else 67193a686eeSJean-Christophe PLAGNIOL-VILLARD cfg = pci_find_config(hose, class, vendor, device, 67293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); 67393a686eeSJean-Christophe PLAGNIOL-VILLARD if (cfg) { 67493a686eeSJean-Christophe PLAGNIOL-VILLARD cfg->config_device(hose, dev, cfg); 675b4141195SMasahiro Yamada sub_bus = max(sub_bus, 676b4141195SMasahiro Yamada (unsigned int)hose->current_busno); 67793a686eeSJean-Christophe PLAGNIOL-VILLARD } 67803992ac2SAndrew Sharp #endif 679a38d216eSPeter Tyser 680009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW 681009884aeSPeter Tyser indent--; 682009884aeSPeter Tyser #endif 683009884aeSPeter Tyser 68493a686eeSJean-Christophe PLAGNIOL-VILLARD if (hose->fixup_irq) 68593a686eeSJean-Christophe PLAGNIOL-VILLARD hose->fixup_irq(hose, dev); 68693a686eeSJean-Christophe PLAGNIOL-VILLARD } 68793a686eeSJean-Christophe PLAGNIOL-VILLARD 68893a686eeSJean-Christophe PLAGNIOL-VILLARD return sub_bus; 68993a686eeSJean-Christophe PLAGNIOL-VILLARD } 69093a686eeSJean-Christophe PLAGNIOL-VILLARD 69193a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose) 69293a686eeSJean-Christophe PLAGNIOL-VILLARD { 6930da1fb03SAnatolij Gustschin #if defined(CONFIG_PCI_BOOTDELAY) 6940da1fb03SAnatolij Gustschin static int pcidelay_done; 6950da1fb03SAnatolij Gustschin char *s; 6960da1fb03SAnatolij Gustschin int i; 6970da1fb03SAnatolij Gustschin 6980da1fb03SAnatolij Gustschin if (!pcidelay_done) { 6990da1fb03SAnatolij Gustschin /* wait "pcidelay" ms (if defined)... */ 7000da1fb03SAnatolij Gustschin s = getenv("pcidelay"); 7010da1fb03SAnatolij Gustschin if (s) { 7020da1fb03SAnatolij Gustschin int val = simple_strtoul(s, NULL, 10); 7030da1fb03SAnatolij Gustschin for (i = 0; i < val; i++) 7040da1fb03SAnatolij Gustschin udelay(1000); 7050da1fb03SAnatolij Gustschin } 7060da1fb03SAnatolij Gustschin pcidelay_done = 1; 7070da1fb03SAnatolij Gustschin } 7080da1fb03SAnatolij Gustschin #endif /* CONFIG_PCI_BOOTDELAY */ 7090da1fb03SAnatolij Gustschin 710cb2bf931SAndrew Sharp /* 711cb2bf931SAndrew Sharp * Start scan at current_busno. 71293a686eeSJean-Christophe PLAGNIOL-VILLARD * PCIe will start scan at first_busno+1. 71393a686eeSJean-Christophe PLAGNIOL-VILLARD */ 71493a686eeSJean-Christophe PLAGNIOL-VILLARD /* For legacy support, ensure current >= first */ 71593a686eeSJean-Christophe PLAGNIOL-VILLARD if (hose->first_busno > hose->current_busno) 71693a686eeSJean-Christophe PLAGNIOL-VILLARD hose->current_busno = hose->first_busno; 71793a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP 71893a686eeSJean-Christophe PLAGNIOL-VILLARD pciauto_config_init(hose); 71993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif 72093a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_hose_scan_bus(hose, hose->current_busno); 72193a686eeSJean-Christophe PLAGNIOL-VILLARD } 72293a686eeSJean-Christophe PLAGNIOL-VILLARD 72393a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void) 72493a686eeSJean-Christophe PLAGNIOL-VILLARD { 72596d61603SJohn Schmoller hose_head = NULL; 72696d61603SJohn Schmoller 72793a686eeSJean-Christophe PLAGNIOL-VILLARD /* now call board specific pci_init()... */ 72893a686eeSJean-Christophe PLAGNIOL-VILLARD pci_init_board(); 72993a686eeSJean-Christophe PLAGNIOL-VILLARD } 730287df01eSZhao Qiang 731287df01eSZhao Qiang /* Returns the address of the requested capability structure within the 732287df01eSZhao Qiang * device's PCI configuration space or 0 in case the device does not 733287df01eSZhao Qiang * support it. 734287df01eSZhao Qiang * */ 735287df01eSZhao Qiang int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev, 736287df01eSZhao Qiang int cap) 737287df01eSZhao Qiang { 738287df01eSZhao Qiang int pos; 739287df01eSZhao Qiang u8 hdr_type; 740287df01eSZhao Qiang 741287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type); 742287df01eSZhao Qiang 743287df01eSZhao Qiang pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F); 744287df01eSZhao Qiang 745287df01eSZhao Qiang if (pos) 746287df01eSZhao Qiang pos = pci_find_cap(hose, dev, pos, cap); 747287df01eSZhao Qiang 748287df01eSZhao Qiang return pos; 749287df01eSZhao Qiang } 750287df01eSZhao Qiang 751287df01eSZhao Qiang /* Find the header pointer to the Capabilities*/ 752287df01eSZhao Qiang int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev, 753287df01eSZhao Qiang u8 hdr_type) 754287df01eSZhao Qiang { 755287df01eSZhao Qiang u16 status; 756287df01eSZhao Qiang 757287df01eSZhao Qiang pci_hose_read_config_word(hose, dev, PCI_STATUS, &status); 758287df01eSZhao Qiang 759287df01eSZhao Qiang if (!(status & PCI_STATUS_CAP_LIST)) 760287df01eSZhao Qiang return 0; 761287df01eSZhao Qiang 762287df01eSZhao Qiang switch (hdr_type) { 763287df01eSZhao Qiang case PCI_HEADER_TYPE_NORMAL: 764287df01eSZhao Qiang case PCI_HEADER_TYPE_BRIDGE: 765287df01eSZhao Qiang return PCI_CAPABILITY_LIST; 766287df01eSZhao Qiang case PCI_HEADER_TYPE_CARDBUS: 767287df01eSZhao Qiang return PCI_CB_CAPABILITY_LIST; 768287df01eSZhao Qiang default: 769287df01eSZhao Qiang return 0; 770287df01eSZhao Qiang } 771287df01eSZhao Qiang } 772287df01eSZhao Qiang 773287df01eSZhao Qiang int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap) 774287df01eSZhao Qiang { 775287df01eSZhao Qiang int ttl = PCI_FIND_CAP_TTL; 776287df01eSZhao Qiang u8 id; 777287df01eSZhao Qiang u8 next_pos; 778287df01eSZhao Qiang 779287df01eSZhao Qiang while (ttl--) { 780287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, pos, &next_pos); 781287df01eSZhao Qiang if (next_pos < CAP_START_POS) 782287df01eSZhao Qiang break; 783287df01eSZhao Qiang next_pos &= ~3; 784287df01eSZhao Qiang pos = (int) next_pos; 785287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, 786287df01eSZhao Qiang pos + PCI_CAP_LIST_ID, &id); 787287df01eSZhao Qiang if (id == 0xff) 788287df01eSZhao Qiang break; 789287df01eSZhao Qiang if (id == cap) 790287df01eSZhao Qiang return pos; 791287df01eSZhao Qiang pos += PCI_CAP_LIST_NEXT; 792287df01eSZhao Qiang } 793287df01eSZhao Qiang return 0; 794287df01eSZhao Qiang } 795