183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
293a686eeSJean-Christophe PLAGNIOL-VILLARD /*
393a686eeSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
493a686eeSJean-Christophe PLAGNIOL-VILLARD * Andreas Heppel <aheppel@sysgo.de>
593a686eeSJean-Christophe PLAGNIOL-VILLARD *
693a686eeSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2002, 2003
793a686eeSJean-Christophe PLAGNIOL-VILLARD * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
893a686eeSJean-Christophe PLAGNIOL-VILLARD */
993a686eeSJean-Christophe PLAGNIOL-VILLARD
1093a686eeSJean-Christophe PLAGNIOL-VILLARD /*
112b81e8a3SSimon Glass * Old PCI routines
122b81e8a3SSimon Glass *
132b81e8a3SSimon Glass * Do not change this file. Instead, convert your board to use CONFIG_DM_PCI
142b81e8a3SSimon Glass * and change pci-uclass.c.
1593a686eeSJean-Christophe PLAGNIOL-VILLARD */
1693a686eeSJean-Christophe PLAGNIOL-VILLARD
1793a686eeSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
1893a686eeSJean-Christophe PLAGNIOL-VILLARD
1993a686eeSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
20250e039dSSimon Glass #include <errno.h>
2193a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/processor.h>
2293a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
2393a686eeSJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
2493a686eeSJean-Christophe PLAGNIOL-VILLARD
258f9052fdSBin Meng DECLARE_GLOBAL_DATA_PTR;
268f9052fdSBin Meng
2793a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_HOSE_OP(rw, size, type) \
2893a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
2993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \
3093a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type value) \
3193a686eeSJean-Christophe PLAGNIOL-VILLARD { \
3293a686eeSJean-Christophe PLAGNIOL-VILLARD return hose->rw##_##size(hose, dev, offset, value); \
3393a686eeSJean-Christophe PLAGNIOL-VILLARD }
3493a686eeSJean-Christophe PLAGNIOL-VILLARD
3593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, byte, u8 *)
3693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, word, u16 *)
3793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, dword, u32 *)
3893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, byte, u8)
3993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, word, u16)
4093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, dword, u32)
4193a686eeSJean-Christophe PLAGNIOL-VILLARD
4293a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_OP(rw, size, type, error_code) \
4393a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
4493a686eeSJean-Christophe PLAGNIOL-VILLARD { \
4593a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
4693a686eeSJean-Christophe PLAGNIOL-VILLARD \
4793a686eeSJean-Christophe PLAGNIOL-VILLARD if (!hose) \
4893a686eeSJean-Christophe PLAGNIOL-VILLARD { \
4993a686eeSJean-Christophe PLAGNIOL-VILLARD error_code; \
5093a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
5193a686eeSJean-Christophe PLAGNIOL-VILLARD } \
5293a686eeSJean-Christophe PLAGNIOL-VILLARD \
5393a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
5493a686eeSJean-Christophe PLAGNIOL-VILLARD }
5593a686eeSJean-Christophe PLAGNIOL-VILLARD
5693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, byte, u8 *, *value = 0xff)
5793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, word, u16 *, *value = 0xffff)
5893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, dword, u32 *, *value = 0xffffffff)
5993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, byte, u8, )
6093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, word, u16, )
6193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, dword, u32, )
6293a686eeSJean-Christophe PLAGNIOL-VILLARD
6393a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
6493a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
6593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \
6693a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type val) \
6793a686eeSJean-Christophe PLAGNIOL-VILLARD { \
6893a686eeSJean-Christophe PLAGNIOL-VILLARD u32 val32; \
6993a686eeSJean-Christophe PLAGNIOL-VILLARD \
7093a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
7193a686eeSJean-Christophe PLAGNIOL-VILLARD *val = -1; \
7293a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
7393a686eeSJean-Christophe PLAGNIOL-VILLARD } \
7493a686eeSJean-Christophe PLAGNIOL-VILLARD \
7593a686eeSJean-Christophe PLAGNIOL-VILLARD *val = (val32 >> ((offset & (int)off_mask) * 8)); \
7693a686eeSJean-Christophe PLAGNIOL-VILLARD \
7793a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; \
7893a686eeSJean-Christophe PLAGNIOL-VILLARD }
7993a686eeSJean-Christophe PLAGNIOL-VILLARD
8093a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
8193a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
8293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \
8393a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type val) \
8493a686eeSJean-Christophe PLAGNIOL-VILLARD { \
8593a686eeSJean-Christophe PLAGNIOL-VILLARD u32 val32, mask, ldata, shift; \
8693a686eeSJean-Christophe PLAGNIOL-VILLARD \
8793a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
8893a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
8993a686eeSJean-Christophe PLAGNIOL-VILLARD \
9093a686eeSJean-Christophe PLAGNIOL-VILLARD shift = ((offset & (int)off_mask) * 8); \
9193a686eeSJean-Christophe PLAGNIOL-VILLARD ldata = (((unsigned long)val) & val_mask) << shift; \
9293a686eeSJean-Christophe PLAGNIOL-VILLARD mask = val_mask << shift; \
9393a686eeSJean-Christophe PLAGNIOL-VILLARD val32 = (val32 & ~mask) | ldata; \
9493a686eeSJean-Christophe PLAGNIOL-VILLARD \
9593a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
9693a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
9793a686eeSJean-Christophe PLAGNIOL-VILLARD \
9893a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; \
9993a686eeSJean-Christophe PLAGNIOL-VILLARD }
10093a686eeSJean-Christophe PLAGNIOL-VILLARD
10193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
10293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
10393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
10493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
10593a686eeSJean-Christophe PLAGNIOL-VILLARD
10693a686eeSJean-Christophe PLAGNIOL-VILLARD /*
10793a686eeSJean-Christophe PLAGNIOL-VILLARD *
10893a686eeSJean-Christophe PLAGNIOL-VILLARD */
10993a686eeSJean-Christophe PLAGNIOL-VILLARD
11096d61603SJohn Schmoller static struct pci_controller* hose_head;
11193a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_get_hose_head(void)1128f9052fdSBin Meng struct pci_controller *pci_get_hose_head(void)
1138f9052fdSBin Meng {
1148f9052fdSBin Meng if (gd->hose)
1158f9052fdSBin Meng return gd->hose;
1168f9052fdSBin Meng
1178f9052fdSBin Meng return hose_head;
1188f9052fdSBin Meng }
1198f9052fdSBin Meng
pci_register_hose(struct pci_controller * hose)12093a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose)
12193a686eeSJean-Christophe PLAGNIOL-VILLARD {
12293a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller **phose = &hose_head;
12393a686eeSJean-Christophe PLAGNIOL-VILLARD
12493a686eeSJean-Christophe PLAGNIOL-VILLARD while(*phose)
12593a686eeSJean-Christophe PLAGNIOL-VILLARD phose = &(*phose)->next;
12693a686eeSJean-Christophe PLAGNIOL-VILLARD
12793a686eeSJean-Christophe PLAGNIOL-VILLARD hose->next = NULL;
12893a686eeSJean-Christophe PLAGNIOL-VILLARD
12993a686eeSJean-Christophe PLAGNIOL-VILLARD *phose = hose;
13093a686eeSJean-Christophe PLAGNIOL-VILLARD }
13193a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_bus_to_hose(int bus)13293a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose(int bus)
13393a686eeSJean-Christophe PLAGNIOL-VILLARD {
13493a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *hose;
13593a686eeSJean-Christophe PLAGNIOL-VILLARD
1368f9052fdSBin Meng for (hose = pci_get_hose_head(); hose; hose = hose->next) {
13793a686eeSJean-Christophe PLAGNIOL-VILLARD if (bus >= hose->first_busno && bus <= hose->last_busno)
13893a686eeSJean-Christophe PLAGNIOL-VILLARD return hose;
139cb2bf931SAndrew Sharp }
14093a686eeSJean-Christophe PLAGNIOL-VILLARD
14193a686eeSJean-Christophe PLAGNIOL-VILLARD printf("pci_bus_to_hose() failed\n");
14293a686eeSJean-Christophe PLAGNIOL-VILLARD return NULL;
14393a686eeSJean-Christophe PLAGNIOL-VILLARD }
14493a686eeSJean-Christophe PLAGNIOL-VILLARD
find_hose_by_cfg_addr(void * cfg_addr)1453a0e3c27SKumar Gala struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
1463a0e3c27SKumar Gala {
1473a0e3c27SKumar Gala struct pci_controller *hose;
1483a0e3c27SKumar Gala
1498f9052fdSBin Meng for (hose = pci_get_hose_head(); hose; hose = hose->next) {
1503a0e3c27SKumar Gala if (hose->cfg_addr == cfg_addr)
1513a0e3c27SKumar Gala return hose;
1523a0e3c27SKumar Gala }
1533a0e3c27SKumar Gala
1543a0e3c27SKumar Gala return NULL;
1553a0e3c27SKumar Gala }
1563a0e3c27SKumar Gala
pci_last_busno(void)157cc2a8c77SAnton Vorontsov int pci_last_busno(void)
158cc2a8c77SAnton Vorontsov {
1598f9052fdSBin Meng struct pci_controller *hose = pci_get_hose_head();
160cc2a8c77SAnton Vorontsov
161cc2a8c77SAnton Vorontsov if (!hose)
162cc2a8c77SAnton Vorontsov return -1;
163cc2a8c77SAnton Vorontsov
164cc2a8c77SAnton Vorontsov while (hose->next)
165cc2a8c77SAnton Vorontsov hose = hose->next;
166cc2a8c77SAnton Vorontsov
167cc2a8c77SAnton Vorontsov return hose->last_busno;
168cc2a8c77SAnton Vorontsov }
169cc2a8c77SAnton Vorontsov
pci_find_devices(struct pci_device_id * ids,int index)17093a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
17193a686eeSJean-Christophe PLAGNIOL-VILLARD {
17293a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller * hose;
17393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t bdf;
174aab6724cSSimon Glass int bus;
17593a686eeSJean-Christophe PLAGNIOL-VILLARD
1768f9052fdSBin Meng for (hose = pci_get_hose_head(); hose; hose = hose->next) {
177aab6724cSSimon Glass for (bus = hose->first_busno; bus <= hose->last_busno; bus++) {
178aab6724cSSimon Glass bdf = pci_hose_find_devices(hose, bus, ids, &index);
179aab6724cSSimon Glass if (bdf != -1)
18093a686eeSJean-Christophe PLAGNIOL-VILLARD return bdf;
18193a686eeSJean-Christophe PLAGNIOL-VILLARD }
182cb2bf931SAndrew Sharp }
18393a686eeSJean-Christophe PLAGNIOL-VILLARD
184cb2bf931SAndrew Sharp return -1;
18593a686eeSJean-Christophe PLAGNIOL-VILLARD }
18693a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_hose_config_device(struct pci_controller * hose,pci_dev_t dev,ulong io,pci_addr_t mem,ulong command)187*11503be4SSimon Glass static int pci_hose_config_device(struct pci_controller *hose, pci_dev_t dev,
188*11503be4SSimon Glass ulong io, pci_addr_t mem, ulong command)
18993a686eeSJean-Christophe PLAGNIOL-VILLARD {
190cf5787f2SKumar Gala u32 bar_response;
191af778c6dSAndrew Sharp unsigned int old_command;
19230e76d5eSKumar Gala pci_addr_t bar_value;
19330e76d5eSKumar Gala pci_size_t bar_size;
19493a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned char pin;
19593a686eeSJean-Christophe PLAGNIOL-VILLARD int bar, found_mem64;
19693a686eeSJean-Christophe PLAGNIOL-VILLARD
197cb2bf931SAndrew Sharp debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
198cb2bf931SAndrew Sharp (u64)mem, command);
19993a686eeSJean-Christophe PLAGNIOL-VILLARD
20093a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
20193a686eeSJean-Christophe PLAGNIOL-VILLARD
202252b404dSWolfgang Denk for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
20393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
20493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_dword(hose, dev, bar, &bar_response);
20593a686eeSJean-Christophe PLAGNIOL-VILLARD
20693a686eeSJean-Christophe PLAGNIOL-VILLARD if (!bar_response)
20793a686eeSJean-Christophe PLAGNIOL-VILLARD continue;
20893a686eeSJean-Christophe PLAGNIOL-VILLARD
20993a686eeSJean-Christophe PLAGNIOL-VILLARD found_mem64 = 0;
21093a686eeSJean-Christophe PLAGNIOL-VILLARD
21193a686eeSJean-Christophe PLAGNIOL-VILLARD /* Check the BAR type and set our address mask */
21293a686eeSJean-Christophe PLAGNIOL-VILLARD if (bar_response & PCI_BASE_ADDRESS_SPACE) {
21393a686eeSJean-Christophe PLAGNIOL-VILLARD bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
21493a686eeSJean-Christophe PLAGNIOL-VILLARD /* round up region base address to a multiple of size */
21593a686eeSJean-Christophe PLAGNIOL-VILLARD io = ((io - 1) | (bar_size - 1)) + 1;
21693a686eeSJean-Christophe PLAGNIOL-VILLARD bar_value = io;
21793a686eeSJean-Christophe PLAGNIOL-VILLARD /* compute new region base address */
21893a686eeSJean-Christophe PLAGNIOL-VILLARD io = io + bar_size;
21993a686eeSJean-Christophe PLAGNIOL-VILLARD } else {
22093a686eeSJean-Christophe PLAGNIOL-VILLARD if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
22130e76d5eSKumar Gala PCI_BASE_ADDRESS_MEM_TYPE_64) {
22230e76d5eSKumar Gala u32 bar_response_upper;
22330e76d5eSKumar Gala u64 bar64;
224cb2bf931SAndrew Sharp pci_hose_write_config_dword(hose, dev, bar + 4,
225cb2bf931SAndrew Sharp 0xffffffff);
226cb2bf931SAndrew Sharp pci_hose_read_config_dword(hose, dev, bar + 4,
227cb2bf931SAndrew Sharp &bar_response_upper);
22893a686eeSJean-Christophe PLAGNIOL-VILLARD
22930e76d5eSKumar Gala bar64 = ((u64)bar_response_upper << 32) | bar_response;
23030e76d5eSKumar Gala
23130e76d5eSKumar Gala bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
23230e76d5eSKumar Gala found_mem64 = 1;
23330e76d5eSKumar Gala } else {
23430e76d5eSKumar Gala bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
23530e76d5eSKumar Gala }
23693a686eeSJean-Christophe PLAGNIOL-VILLARD
23793a686eeSJean-Christophe PLAGNIOL-VILLARD /* round up region base address to multiple of size */
23893a686eeSJean-Christophe PLAGNIOL-VILLARD mem = ((mem - 1) | (bar_size - 1)) + 1;
23993a686eeSJean-Christophe PLAGNIOL-VILLARD bar_value = mem;
24093a686eeSJean-Christophe PLAGNIOL-VILLARD /* compute new region base address */
24193a686eeSJean-Christophe PLAGNIOL-VILLARD mem = mem + bar_size;
24293a686eeSJean-Christophe PLAGNIOL-VILLARD }
24393a686eeSJean-Christophe PLAGNIOL-VILLARD
24493a686eeSJean-Christophe PLAGNIOL-VILLARD /* Write it out and update our limit */
24530e76d5eSKumar Gala pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
24693a686eeSJean-Christophe PLAGNIOL-VILLARD
24793a686eeSJean-Christophe PLAGNIOL-VILLARD if (found_mem64) {
24893a686eeSJean-Christophe PLAGNIOL-VILLARD bar += 4;
24930e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
250cb2bf931SAndrew Sharp pci_hose_write_config_dword(hose, dev, bar,
251cb2bf931SAndrew Sharp (u32)(bar_value >> 32));
25230e76d5eSKumar Gala #else
25393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
25430e76d5eSKumar Gala #endif
25593a686eeSJean-Christophe PLAGNIOL-VILLARD }
25693a686eeSJean-Christophe PLAGNIOL-VILLARD }
25793a686eeSJean-Christophe PLAGNIOL-VILLARD
25893a686eeSJean-Christophe PLAGNIOL-VILLARD /* Configure Cache Line Size Register */
25993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
26093a686eeSJean-Christophe PLAGNIOL-VILLARD
26193a686eeSJean-Christophe PLAGNIOL-VILLARD /* Configure Latency Timer */
26293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
26393a686eeSJean-Christophe PLAGNIOL-VILLARD
26493a686eeSJean-Christophe PLAGNIOL-VILLARD /* Disable interrupt line, if device says it wants to use interrupts */
26593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
26693a686eeSJean-Christophe PLAGNIOL-VILLARD if (pin != 0) {
2675f48d798SSimon Glass pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE,
2685f48d798SSimon Glass PCI_INTERRUPT_LINE_DISABLE);
26993a686eeSJean-Christophe PLAGNIOL-VILLARD }
27093a686eeSJean-Christophe PLAGNIOL-VILLARD
27193a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
27293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
27393a686eeSJean-Christophe PLAGNIOL-VILLARD (old_command & 0xffff0000) | command);
27493a686eeSJean-Christophe PLAGNIOL-VILLARD
27593a686eeSJean-Christophe PLAGNIOL-VILLARD return 0;
27693a686eeSJean-Christophe PLAGNIOL-VILLARD }
27793a686eeSJean-Christophe PLAGNIOL-VILLARD
27893a686eeSJean-Christophe PLAGNIOL-VILLARD /*
27993a686eeSJean-Christophe PLAGNIOL-VILLARD *
28093a686eeSJean-Christophe PLAGNIOL-VILLARD */
28193a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_find_config(struct pci_controller * hose,unsigned short class,unsigned int vendor,unsigned int device,unsigned int bus,unsigned int dev,unsigned int func)28293a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose,
28393a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned short class,
28493a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int vendor,
28593a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int device,
28693a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int bus,
28793a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int dev,
28893a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int func)
28993a686eeSJean-Christophe PLAGNIOL-VILLARD {
29093a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *table;
29193a686eeSJean-Christophe PLAGNIOL-VILLARD
29293a686eeSJean-Christophe PLAGNIOL-VILLARD for (table = hose->config_table; table && table->vendor; table++) {
29393a686eeSJean-Christophe PLAGNIOL-VILLARD if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
29493a686eeSJean-Christophe PLAGNIOL-VILLARD (table->device == PCI_ANY_ID || table->device == device) &&
29593a686eeSJean-Christophe PLAGNIOL-VILLARD (table->class == PCI_ANY_ID || table->class == class) &&
29693a686eeSJean-Christophe PLAGNIOL-VILLARD (table->bus == PCI_ANY_ID || table->bus == bus) &&
29793a686eeSJean-Christophe PLAGNIOL-VILLARD (table->dev == PCI_ANY_ID || table->dev == dev) &&
29893a686eeSJean-Christophe PLAGNIOL-VILLARD (table->func == PCI_ANY_ID || table->func == func)) {
29993a686eeSJean-Christophe PLAGNIOL-VILLARD return table;
30093a686eeSJean-Christophe PLAGNIOL-VILLARD }
30193a686eeSJean-Christophe PLAGNIOL-VILLARD }
30293a686eeSJean-Christophe PLAGNIOL-VILLARD
30393a686eeSJean-Christophe PLAGNIOL-VILLARD return NULL;
30493a686eeSJean-Christophe PLAGNIOL-VILLARD }
30593a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_cfgfunc_config_device(struct pci_controller * hose,pci_dev_t dev,struct pci_config_table * entry)30693a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose,
30793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev,
30893a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *entry)
30993a686eeSJean-Christophe PLAGNIOL-VILLARD {
310cb2bf931SAndrew Sharp pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
311cb2bf931SAndrew Sharp entry->priv[2]);
31293a686eeSJean-Christophe PLAGNIOL-VILLARD }
31393a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_cfgfunc_do_nothing(struct pci_controller * hose,pci_dev_t dev,struct pci_config_table * entry)31493a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose,
31593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, struct pci_config_table *entry)
31693a686eeSJean-Christophe PLAGNIOL-VILLARD {
31793a686eeSJean-Christophe PLAGNIOL-VILLARD }
31893a686eeSJean-Christophe PLAGNIOL-VILLARD
31993a686eeSJean-Christophe PLAGNIOL-VILLARD /*
320cb2bf931SAndrew Sharp * HJF: Changed this to return int. I think this is required
32193a686eeSJean-Christophe PLAGNIOL-VILLARD * to get the correct result when scanning bridges
32293a686eeSJean-Christophe PLAGNIOL-VILLARD */
32393a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
32493a686eeSJean-Christophe PLAGNIOL-VILLARD
325dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW
pci_print_dev(struct pci_controller * hose,pci_dev_t dev)3267b19fd6dSJeroen Hofstee __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
327dc1da42fSStefan Roese {
328dc1da42fSStefan Roese if (dev == PCI_BDF(hose->first_busno, 0, 0))
329dc1da42fSStefan Roese return 0;
330dc1da42fSStefan Roese
331dc1da42fSStefan Roese return 1;
332dc1da42fSStefan Roese }
333dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */
334dc1da42fSStefan Roese
pci_hose_scan_bus(struct pci_controller * hose,int bus)33593a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus)
33693a686eeSJean-Christophe PLAGNIOL-VILLARD {
33793a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int sub_bus, found_multi = 0;
33893a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned short vendor, device, class;
33993a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned char header_type;
34003992ac2SAndrew Sharp #ifndef CONFIG_PCI_PNP
34193a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *cfg;
34203992ac2SAndrew Sharp #endif
34393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev;
344009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
345009884aeSPeter Tyser static int indent = 0;
346009884aeSPeter Tyser #endif
34793a686eeSJean-Christophe PLAGNIOL-VILLARD
34893a686eeSJean-Christophe PLAGNIOL-VILLARD sub_bus = bus;
34993a686eeSJean-Christophe PLAGNIOL-VILLARD
35093a686eeSJean-Christophe PLAGNIOL-VILLARD for (dev = PCI_BDF(bus,0,0);
351cb2bf931SAndrew Sharp dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
352cb2bf931SAndrew Sharp PCI_MAX_PCI_FUNCTIONS - 1);
353dc1da42fSStefan Roese dev += PCI_BDF(0, 0, 1)) {
354dc1da42fSStefan Roese
355dc1da42fSStefan Roese if (pci_skip_dev(hose, dev))
356dc1da42fSStefan Roese continue;
35793a686eeSJean-Christophe PLAGNIOL-VILLARD
35893a686eeSJean-Christophe PLAGNIOL-VILLARD if (PCI_FUNC(dev) && !found_multi)
35993a686eeSJean-Christophe PLAGNIOL-VILLARD continue;
36093a686eeSJean-Christophe PLAGNIOL-VILLARD
36193a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
36293a686eeSJean-Christophe PLAGNIOL-VILLARD
36393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
36493a686eeSJean-Christophe PLAGNIOL-VILLARD
365983eb9d1SPeter Tyser if (vendor == 0xffff || vendor == 0x0000)
366983eb9d1SPeter Tyser continue;
36793a686eeSJean-Christophe PLAGNIOL-VILLARD
36893a686eeSJean-Christophe PLAGNIOL-VILLARD if (!PCI_FUNC(dev))
36993a686eeSJean-Christophe PLAGNIOL-VILLARD found_multi = header_type & 0x80;
37093a686eeSJean-Christophe PLAGNIOL-VILLARD
37193a686eeSJean-Christophe PLAGNIOL-VILLARD debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
37293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
37393a686eeSJean-Christophe PLAGNIOL-VILLARD
37493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
37593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
37693a686eeSJean-Christophe PLAGNIOL-VILLARD
3770991866cSTim Harvey #ifdef CONFIG_PCI_FIXUP_DEV
3780991866cSTim Harvey board_pci_fixup_dev(hose, dev, vendor, device, class);
3790991866cSTim Harvey #endif
3800991866cSTim Harvey
381a38d216eSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
382009884aeSPeter Tyser indent++;
383009884aeSPeter Tyser
384009884aeSPeter Tyser /* Print leading space, including bus indentation */
385009884aeSPeter Tyser printf("%*c", indent + 1, ' ');
386009884aeSPeter Tyser
387a38d216eSPeter Tyser if (pci_print_dev(hose, dev)) {
388009884aeSPeter Tyser printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
389009884aeSPeter Tyser PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
390a38d216eSPeter Tyser vendor, device, pci_class_str(class >> 8));
391a38d216eSPeter Tyser }
392a38d216eSPeter Tyser #endif
393a38d216eSPeter Tyser
39403992ac2SAndrew Sharp #ifdef CONFIG_PCI_PNP
395b4141195SMasahiro Yamada sub_bus = max((unsigned int)pciauto_config_device(hose, dev),
396b4141195SMasahiro Yamada sub_bus);
39703992ac2SAndrew Sharp #else
39893a686eeSJean-Christophe PLAGNIOL-VILLARD cfg = pci_find_config(hose, class, vendor, device,
39993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
40093a686eeSJean-Christophe PLAGNIOL-VILLARD if (cfg) {
40193a686eeSJean-Christophe PLAGNIOL-VILLARD cfg->config_device(hose, dev, cfg);
402b4141195SMasahiro Yamada sub_bus = max(sub_bus,
403b4141195SMasahiro Yamada (unsigned int)hose->current_busno);
40493a686eeSJean-Christophe PLAGNIOL-VILLARD }
40503992ac2SAndrew Sharp #endif
406a38d216eSPeter Tyser
407009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
408009884aeSPeter Tyser indent--;
409009884aeSPeter Tyser #endif
410009884aeSPeter Tyser
41193a686eeSJean-Christophe PLAGNIOL-VILLARD if (hose->fixup_irq)
41293a686eeSJean-Christophe PLAGNIOL-VILLARD hose->fixup_irq(hose, dev);
41393a686eeSJean-Christophe PLAGNIOL-VILLARD }
41493a686eeSJean-Christophe PLAGNIOL-VILLARD
41593a686eeSJean-Christophe PLAGNIOL-VILLARD return sub_bus;
41693a686eeSJean-Christophe PLAGNIOL-VILLARD }
41793a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_hose_scan(struct pci_controller * hose)41893a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose)
41993a686eeSJean-Christophe PLAGNIOL-VILLARD {
4200da1fb03SAnatolij Gustschin #if defined(CONFIG_PCI_BOOTDELAY)
4210da1fb03SAnatolij Gustschin char *s;
4220da1fb03SAnatolij Gustschin int i;
4230da1fb03SAnatolij Gustschin
4248f9052fdSBin Meng if (!gd->pcidelay_done) {
4250da1fb03SAnatolij Gustschin /* wait "pcidelay" ms (if defined)... */
42600caae6dSSimon Glass s = env_get("pcidelay");
4270da1fb03SAnatolij Gustschin if (s) {
4280da1fb03SAnatolij Gustschin int val = simple_strtoul(s, NULL, 10);
4290da1fb03SAnatolij Gustschin for (i = 0; i < val; i++)
4300da1fb03SAnatolij Gustschin udelay(1000);
4310da1fb03SAnatolij Gustschin }
4328f9052fdSBin Meng gd->pcidelay_done = 1;
4330da1fb03SAnatolij Gustschin }
4340da1fb03SAnatolij Gustschin #endif /* CONFIG_PCI_BOOTDELAY */
4350da1fb03SAnatolij Gustschin
4360373a7e9STim Harvey #ifdef CONFIG_PCI_SCAN_SHOW
4370373a7e9STim Harvey puts("PCI:\n");
4380373a7e9STim Harvey #endif
4390373a7e9STim Harvey
440cb2bf931SAndrew Sharp /*
441cb2bf931SAndrew Sharp * Start scan at current_busno.
44293a686eeSJean-Christophe PLAGNIOL-VILLARD * PCIe will start scan at first_busno+1.
44393a686eeSJean-Christophe PLAGNIOL-VILLARD */
44493a686eeSJean-Christophe PLAGNIOL-VILLARD /* For legacy support, ensure current >= first */
44593a686eeSJean-Christophe PLAGNIOL-VILLARD if (hose->first_busno > hose->current_busno)
44693a686eeSJean-Christophe PLAGNIOL-VILLARD hose->current_busno = hose->first_busno;
44793a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
44893a686eeSJean-Christophe PLAGNIOL-VILLARD pciauto_config_init(hose);
44993a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
45093a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_hose_scan_bus(hose, hose->current_busno);
45193a686eeSJean-Christophe PLAGNIOL-VILLARD }
45293a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_init(void)45393a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void)
45493a686eeSJean-Christophe PLAGNIOL-VILLARD {
45596d61603SJohn Schmoller hose_head = NULL;
45696d61603SJohn Schmoller
457ec21aee6STim Harvey /* allow env to disable pci init/enum */
45800caae6dSSimon Glass if (env_get("pcidisable") != NULL)
459ec21aee6STim Harvey return;
460ec21aee6STim Harvey
46193a686eeSJean-Christophe PLAGNIOL-VILLARD /* now call board specific pci_init()... */
46293a686eeSJean-Christophe PLAGNIOL-VILLARD pci_init_board();
46393a686eeSJean-Christophe PLAGNIOL-VILLARD }
464287df01eSZhao Qiang
465287df01eSZhao Qiang /* Returns the address of the requested capability structure within the
466287df01eSZhao Qiang * device's PCI configuration space or 0 in case the device does not
467287df01eSZhao Qiang * support it.
468287df01eSZhao Qiang * */
pci_hose_find_capability(struct pci_controller * hose,pci_dev_t dev,int cap)469287df01eSZhao Qiang int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
470287df01eSZhao Qiang int cap)
471287df01eSZhao Qiang {
472287df01eSZhao Qiang int pos;
473287df01eSZhao Qiang u8 hdr_type;
474287df01eSZhao Qiang
475287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type);
476287df01eSZhao Qiang
477287df01eSZhao Qiang pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F);
478287df01eSZhao Qiang
479287df01eSZhao Qiang if (pos)
480287df01eSZhao Qiang pos = pci_find_cap(hose, dev, pos, cap);
481287df01eSZhao Qiang
482287df01eSZhao Qiang return pos;
483287df01eSZhao Qiang }
484287df01eSZhao Qiang
485287df01eSZhao Qiang /* Find the header pointer to the Capabilities*/
pci_hose_find_cap_start(struct pci_controller * hose,pci_dev_t dev,u8 hdr_type)486287df01eSZhao Qiang int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
487287df01eSZhao Qiang u8 hdr_type)
488287df01eSZhao Qiang {
489287df01eSZhao Qiang u16 status;
490287df01eSZhao Qiang
491287df01eSZhao Qiang pci_hose_read_config_word(hose, dev, PCI_STATUS, &status);
492287df01eSZhao Qiang
493287df01eSZhao Qiang if (!(status & PCI_STATUS_CAP_LIST))
494287df01eSZhao Qiang return 0;
495287df01eSZhao Qiang
496287df01eSZhao Qiang switch (hdr_type) {
497287df01eSZhao Qiang case PCI_HEADER_TYPE_NORMAL:
498287df01eSZhao Qiang case PCI_HEADER_TYPE_BRIDGE:
499287df01eSZhao Qiang return PCI_CAPABILITY_LIST;
500287df01eSZhao Qiang case PCI_HEADER_TYPE_CARDBUS:
501287df01eSZhao Qiang return PCI_CB_CAPABILITY_LIST;
502287df01eSZhao Qiang default:
503287df01eSZhao Qiang return 0;
504287df01eSZhao Qiang }
505287df01eSZhao Qiang }
506287df01eSZhao Qiang
pci_find_cap(struct pci_controller * hose,pci_dev_t dev,int pos,int cap)507287df01eSZhao Qiang int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap)
508287df01eSZhao Qiang {
509287df01eSZhao Qiang int ttl = PCI_FIND_CAP_TTL;
510287df01eSZhao Qiang u8 id;
511287df01eSZhao Qiang u8 next_pos;
512287df01eSZhao Qiang
513287df01eSZhao Qiang while (ttl--) {
514287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, pos, &next_pos);
515287df01eSZhao Qiang if (next_pos < CAP_START_POS)
516287df01eSZhao Qiang break;
517287df01eSZhao Qiang next_pos &= ~3;
518287df01eSZhao Qiang pos = (int) next_pos;
519287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev,
520287df01eSZhao Qiang pos + PCI_CAP_LIST_ID, &id);
521287df01eSZhao Qiang if (id == 0xff)
522287df01eSZhao Qiang break;
523287df01eSZhao Qiang if (id == cap)
524287df01eSZhao Qiang return pos;
525287df01eSZhao Qiang pos += PCI_CAP_LIST_NEXT;
526287df01eSZhao Qiang }
527287df01eSZhao Qiang return 0;
528287df01eSZhao Qiang }
529ed5b580bSMinghuan Lian
530ed5b580bSMinghuan Lian /**
531ed5b580bSMinghuan Lian * pci_find_next_ext_capability - Find an extended capability
532ed5b580bSMinghuan Lian *
533ed5b580bSMinghuan Lian * Returns the address of the next matching extended capability structure
534ed5b580bSMinghuan Lian * within the device's PCI configuration space or 0 if the device does
535ed5b580bSMinghuan Lian * not support it. Some capabilities can occur several times, e.g., the
536ed5b580bSMinghuan Lian * vendor-specific capability, and this provides a way to find them all.
537ed5b580bSMinghuan Lian */
pci_find_next_ext_capability(struct pci_controller * hose,pci_dev_t dev,int start,int cap)538ed5b580bSMinghuan Lian int pci_find_next_ext_capability(struct pci_controller *hose, pci_dev_t dev,
539ed5b580bSMinghuan Lian int start, int cap)
540ed5b580bSMinghuan Lian {
541ed5b580bSMinghuan Lian u32 header;
542ed5b580bSMinghuan Lian int ttl, pos = PCI_CFG_SPACE_SIZE;
543ed5b580bSMinghuan Lian
544ed5b580bSMinghuan Lian /* minimum 8 bytes per capability */
545ed5b580bSMinghuan Lian ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
546ed5b580bSMinghuan Lian
547ed5b580bSMinghuan Lian if (start)
548ed5b580bSMinghuan Lian pos = start;
549ed5b580bSMinghuan Lian
550ed5b580bSMinghuan Lian pci_hose_read_config_dword(hose, dev, pos, &header);
551ed5b580bSMinghuan Lian if (header == 0xffffffff || header == 0)
552ed5b580bSMinghuan Lian return 0;
553ed5b580bSMinghuan Lian
554ed5b580bSMinghuan Lian while (ttl-- > 0) {
555ed5b580bSMinghuan Lian if (PCI_EXT_CAP_ID(header) == cap && pos != start)
556ed5b580bSMinghuan Lian return pos;
557ed5b580bSMinghuan Lian
558ed5b580bSMinghuan Lian pos = PCI_EXT_CAP_NEXT(header);
559ed5b580bSMinghuan Lian if (pos < PCI_CFG_SPACE_SIZE)
560ed5b580bSMinghuan Lian break;
561ed5b580bSMinghuan Lian
562ed5b580bSMinghuan Lian pci_hose_read_config_dword(hose, dev, pos, &header);
563ed5b580bSMinghuan Lian if (header == 0xffffffff || header == 0)
564ed5b580bSMinghuan Lian break;
565ed5b580bSMinghuan Lian }
566ed5b580bSMinghuan Lian
567ed5b580bSMinghuan Lian return 0;
568ed5b580bSMinghuan Lian }
569ed5b580bSMinghuan Lian
570ed5b580bSMinghuan Lian /**
571ed5b580bSMinghuan Lian * pci_hose_find_ext_capability - Find an extended capability
572ed5b580bSMinghuan Lian *
573ed5b580bSMinghuan Lian * Returns the address of the requested extended capability structure
574ed5b580bSMinghuan Lian * within the device's PCI configuration space or 0 if the device does
575ed5b580bSMinghuan Lian * not support it.
576ed5b580bSMinghuan Lian */
pci_hose_find_ext_capability(struct pci_controller * hose,pci_dev_t dev,int cap)577ed5b580bSMinghuan Lian int pci_hose_find_ext_capability(struct pci_controller *hose, pci_dev_t dev,
578ed5b580bSMinghuan Lian int cap)
579ed5b580bSMinghuan Lian {
580ed5b580bSMinghuan Lian return pci_find_next_ext_capability(hose, dev, 0, cap);
581ed5b580bSMinghuan Lian }
582