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 /*
122b81e8a3SSimon Glass * Old PCI routines
132b81e8a3SSimon Glass *
142b81e8a3SSimon Glass * Do not change this file. Instead, convert your board to use CONFIG_DM_PCI
152b81e8a3SSimon Glass * and change pci-uclass.c.
1693a686eeSJean-Christophe PLAGNIOL-VILLARD */
1793a686eeSJean-Christophe PLAGNIOL-VILLARD
1893a686eeSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
1993a686eeSJean-Christophe PLAGNIOL-VILLARD
2093a686eeSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
21250e039dSSimon Glass #include <errno.h>
2293a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/processor.h>
2393a686eeSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
2493a686eeSJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
2593a686eeSJean-Christophe PLAGNIOL-VILLARD
268f9052fdSBin Meng DECLARE_GLOBAL_DATA_PTR;
278f9052fdSBin Meng
2893a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_HOSE_OP(rw, size, type) \
2993a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
3093a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \
3193a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type value) \
3293a686eeSJean-Christophe PLAGNIOL-VILLARD { \
3393a686eeSJean-Christophe PLAGNIOL-VILLARD return hose->rw##_##size(hose, dev, offset, value); \
3493a686eeSJean-Christophe PLAGNIOL-VILLARD }
3593a686eeSJean-Christophe PLAGNIOL-VILLARD
3693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, byte, u8 *)
3793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, word, u16 *)
3893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(read, dword, u32 *)
3993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, byte, u8)
4093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, word, u16)
4193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_HOSE_OP(write, dword, u32)
4293a686eeSJean-Christophe PLAGNIOL-VILLARD
4393a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_OP(rw, size, type, error_code) \
4493a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
4593a686eeSJean-Christophe PLAGNIOL-VILLARD { \
4693a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
4793a686eeSJean-Christophe PLAGNIOL-VILLARD \
4893a686eeSJean-Christophe PLAGNIOL-VILLARD if (!hose) \
4993a686eeSJean-Christophe PLAGNIOL-VILLARD { \
5093a686eeSJean-Christophe PLAGNIOL-VILLARD error_code; \
5193a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
5293a686eeSJean-Christophe PLAGNIOL-VILLARD } \
5393a686eeSJean-Christophe PLAGNIOL-VILLARD \
5493a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
5593a686eeSJean-Christophe PLAGNIOL-VILLARD }
5693a686eeSJean-Christophe PLAGNIOL-VILLARD
5793a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, byte, u8 *, *value = 0xff)
5893a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, word, u16 *, *value = 0xffff)
5993a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(read, dword, u32 *, *value = 0xffffffff)
6093a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, byte, u8, )
6193a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, word, u16, )
6293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_OP(write, dword, u32, )
6393a686eeSJean-Christophe PLAGNIOL-VILLARD
6493a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
6593a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
6693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \
6793a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type val) \
6893a686eeSJean-Christophe PLAGNIOL-VILLARD { \
6993a686eeSJean-Christophe PLAGNIOL-VILLARD u32 val32; \
7093a686eeSJean-Christophe PLAGNIOL-VILLARD \
7193a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
7293a686eeSJean-Christophe PLAGNIOL-VILLARD *val = -1; \
7393a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
7493a686eeSJean-Christophe PLAGNIOL-VILLARD } \
7593a686eeSJean-Christophe PLAGNIOL-VILLARD \
7693a686eeSJean-Christophe PLAGNIOL-VILLARD *val = (val32 >> ((offset & (int)off_mask) * 8)); \
7793a686eeSJean-Christophe PLAGNIOL-VILLARD \
7893a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; \
7993a686eeSJean-Christophe PLAGNIOL-VILLARD }
8093a686eeSJean-Christophe PLAGNIOL-VILLARD
8193a686eeSJean-Christophe PLAGNIOL-VILLARD #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
8293a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
8393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, \
8493a686eeSJean-Christophe PLAGNIOL-VILLARD int offset, type val) \
8593a686eeSJean-Christophe PLAGNIOL-VILLARD { \
8693a686eeSJean-Christophe PLAGNIOL-VILLARD u32 val32, mask, ldata, shift; \
8793a686eeSJean-Christophe PLAGNIOL-VILLARD \
8893a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
8993a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
9093a686eeSJean-Christophe PLAGNIOL-VILLARD \
9193a686eeSJean-Christophe PLAGNIOL-VILLARD shift = ((offset & (int)off_mask) * 8); \
9293a686eeSJean-Christophe PLAGNIOL-VILLARD ldata = (((unsigned long)val) & val_mask) << shift; \
9393a686eeSJean-Christophe PLAGNIOL-VILLARD mask = val_mask << shift; \
9493a686eeSJean-Christophe PLAGNIOL-VILLARD val32 = (val32 & ~mask) | ldata; \
9593a686eeSJean-Christophe PLAGNIOL-VILLARD \
9693a686eeSJean-Christophe PLAGNIOL-VILLARD if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
9793a686eeSJean-Christophe PLAGNIOL-VILLARD return -1; \
9893a686eeSJean-Christophe PLAGNIOL-VILLARD \
9993a686eeSJean-Christophe PLAGNIOL-VILLARD return 0; \
10093a686eeSJean-Christophe PLAGNIOL-VILLARD }
10193a686eeSJean-Christophe PLAGNIOL-VILLARD
10293a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
10393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
10493a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
10593a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
10693a686eeSJean-Christophe PLAGNIOL-VILLARD
10793a686eeSJean-Christophe PLAGNIOL-VILLARD /*
10893a686eeSJean-Christophe PLAGNIOL-VILLARD *
10993a686eeSJean-Christophe PLAGNIOL-VILLARD */
11093a686eeSJean-Christophe PLAGNIOL-VILLARD
11196d61603SJohn Schmoller static struct pci_controller* hose_head;
11293a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_get_hose_head(void)1138f9052fdSBin Meng struct pci_controller *pci_get_hose_head(void)
1148f9052fdSBin Meng {
1158f9052fdSBin Meng if (gd->hose)
1168f9052fdSBin Meng return gd->hose;
1178f9052fdSBin Meng
1188f9052fdSBin Meng return hose_head;
1198f9052fdSBin Meng }
1208f9052fdSBin Meng
pci_register_hose(struct pci_controller * hose)12193a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_register_hose(struct pci_controller* hose)
12293a686eeSJean-Christophe PLAGNIOL-VILLARD {
12393a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller **phose = &hose_head;
12493a686eeSJean-Christophe PLAGNIOL-VILLARD
12593a686eeSJean-Christophe PLAGNIOL-VILLARD while(*phose)
12693a686eeSJean-Christophe PLAGNIOL-VILLARD phose = &(*phose)->next;
12793a686eeSJean-Christophe PLAGNIOL-VILLARD
12893a686eeSJean-Christophe PLAGNIOL-VILLARD hose->next = NULL;
12993a686eeSJean-Christophe PLAGNIOL-VILLARD
13093a686eeSJean-Christophe PLAGNIOL-VILLARD *phose = hose;
13193a686eeSJean-Christophe PLAGNIOL-VILLARD }
13293a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_bus_to_hose(int bus)13393a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *pci_bus_to_hose(int bus)
13493a686eeSJean-Christophe PLAGNIOL-VILLARD {
13593a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller *hose;
13693a686eeSJean-Christophe PLAGNIOL-VILLARD
1378f9052fdSBin Meng for (hose = pci_get_hose_head(); hose; hose = hose->next) {
13893a686eeSJean-Christophe PLAGNIOL-VILLARD if (bus >= hose->first_busno && bus <= hose->last_busno)
13993a686eeSJean-Christophe PLAGNIOL-VILLARD return hose;
140cb2bf931SAndrew Sharp }
14193a686eeSJean-Christophe PLAGNIOL-VILLARD
14293a686eeSJean-Christophe PLAGNIOL-VILLARD printf("pci_bus_to_hose() failed\n");
14393a686eeSJean-Christophe PLAGNIOL-VILLARD return NULL;
14493a686eeSJean-Christophe PLAGNIOL-VILLARD }
14593a686eeSJean-Christophe PLAGNIOL-VILLARD
find_hose_by_cfg_addr(void * cfg_addr)1463a0e3c27SKumar Gala struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
1473a0e3c27SKumar Gala {
1483a0e3c27SKumar Gala struct pci_controller *hose;
1493a0e3c27SKumar Gala
1508f9052fdSBin Meng for (hose = pci_get_hose_head(); hose; hose = hose->next) {
1513a0e3c27SKumar Gala if (hose->cfg_addr == cfg_addr)
1523a0e3c27SKumar Gala return hose;
1533a0e3c27SKumar Gala }
1543a0e3c27SKumar Gala
1553a0e3c27SKumar Gala return NULL;
1563a0e3c27SKumar Gala }
1573a0e3c27SKumar Gala
pci_last_busno(void)158cc2a8c77SAnton Vorontsov int pci_last_busno(void)
159cc2a8c77SAnton Vorontsov {
1608f9052fdSBin Meng struct pci_controller *hose = pci_get_hose_head();
161cc2a8c77SAnton Vorontsov
162cc2a8c77SAnton Vorontsov if (!hose)
163cc2a8c77SAnton Vorontsov return -1;
164cc2a8c77SAnton Vorontsov
165cc2a8c77SAnton Vorontsov while (hose->next)
166cc2a8c77SAnton Vorontsov hose = hose->next;
167cc2a8c77SAnton Vorontsov
168cc2a8c77SAnton Vorontsov return hose->last_busno;
169cc2a8c77SAnton Vorontsov }
170cc2a8c77SAnton Vorontsov
pci_find_devices(struct pci_device_id * ids,int index)17193a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
17293a686eeSJean-Christophe PLAGNIOL-VILLARD {
17393a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_controller * hose;
17493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t bdf;
175aab6724cSSimon Glass int bus;
17693a686eeSJean-Christophe PLAGNIOL-VILLARD
1778f9052fdSBin Meng for (hose = pci_get_hose_head(); hose; hose = hose->next) {
178aab6724cSSimon Glass for (bus = hose->first_busno; bus <= hose->last_busno; bus++) {
179aab6724cSSimon Glass bdf = pci_hose_find_devices(hose, bus, ids, &index);
180aab6724cSSimon Glass if (bdf != -1)
18193a686eeSJean-Christophe PLAGNIOL-VILLARD return bdf;
18293a686eeSJean-Christophe PLAGNIOL-VILLARD }
183cb2bf931SAndrew Sharp }
18493a686eeSJean-Christophe PLAGNIOL-VILLARD
185cb2bf931SAndrew Sharp return -1;
18693a686eeSJean-Christophe PLAGNIOL-VILLARD }
18793a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_hose_config_device(struct pci_controller * hose,pci_dev_t dev,unsigned long io,pci_addr_t mem,unsigned long command)18893a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_config_device(struct pci_controller *hose,
18993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev,
19093a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned long io,
19130e76d5eSKumar Gala pci_addr_t mem,
19293a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned long command)
19393a686eeSJean-Christophe PLAGNIOL-VILLARD {
194cf5787f2SKumar Gala u32 bar_response;
195af778c6dSAndrew Sharp unsigned int old_command;
19630e76d5eSKumar Gala pci_addr_t bar_value;
19730e76d5eSKumar Gala pci_size_t bar_size;
19893a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned char pin;
19993a686eeSJean-Christophe PLAGNIOL-VILLARD int bar, found_mem64;
20093a686eeSJean-Christophe PLAGNIOL-VILLARD
201cb2bf931SAndrew Sharp debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
202cb2bf931SAndrew Sharp (u64)mem, command);
20393a686eeSJean-Christophe PLAGNIOL-VILLARD
20493a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
20593a686eeSJean-Christophe PLAGNIOL-VILLARD
206252b404dSWolfgang Denk for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
20793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
20893a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_dword(hose, dev, bar, &bar_response);
20993a686eeSJean-Christophe PLAGNIOL-VILLARD
21093a686eeSJean-Christophe PLAGNIOL-VILLARD if (!bar_response)
21193a686eeSJean-Christophe PLAGNIOL-VILLARD continue;
21293a686eeSJean-Christophe PLAGNIOL-VILLARD
21393a686eeSJean-Christophe PLAGNIOL-VILLARD found_mem64 = 0;
21493a686eeSJean-Christophe PLAGNIOL-VILLARD
21593a686eeSJean-Christophe PLAGNIOL-VILLARD /* Check the BAR type and set our address mask */
21693a686eeSJean-Christophe PLAGNIOL-VILLARD if (bar_response & PCI_BASE_ADDRESS_SPACE) {
21793a686eeSJean-Christophe PLAGNIOL-VILLARD bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
21893a686eeSJean-Christophe PLAGNIOL-VILLARD /* round up region base address to a multiple of size */
21993a686eeSJean-Christophe PLAGNIOL-VILLARD io = ((io - 1) | (bar_size - 1)) + 1;
22093a686eeSJean-Christophe PLAGNIOL-VILLARD bar_value = io;
22193a686eeSJean-Christophe PLAGNIOL-VILLARD /* compute new region base address */
22293a686eeSJean-Christophe PLAGNIOL-VILLARD io = io + bar_size;
22393a686eeSJean-Christophe PLAGNIOL-VILLARD } else {
22493a686eeSJean-Christophe PLAGNIOL-VILLARD if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
22530e76d5eSKumar Gala PCI_BASE_ADDRESS_MEM_TYPE_64) {
22630e76d5eSKumar Gala u32 bar_response_upper;
22730e76d5eSKumar Gala u64 bar64;
228cb2bf931SAndrew Sharp pci_hose_write_config_dword(hose, dev, bar + 4,
229cb2bf931SAndrew Sharp 0xffffffff);
230cb2bf931SAndrew Sharp pci_hose_read_config_dword(hose, dev, bar + 4,
231cb2bf931SAndrew Sharp &bar_response_upper);
23293a686eeSJean-Christophe PLAGNIOL-VILLARD
23330e76d5eSKumar Gala bar64 = ((u64)bar_response_upper << 32) | bar_response;
23430e76d5eSKumar Gala
23530e76d5eSKumar Gala bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
23630e76d5eSKumar Gala found_mem64 = 1;
23730e76d5eSKumar Gala } else {
23830e76d5eSKumar Gala bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
23930e76d5eSKumar Gala }
24093a686eeSJean-Christophe PLAGNIOL-VILLARD
24193a686eeSJean-Christophe PLAGNIOL-VILLARD /* round up region base address to multiple of size */
24293a686eeSJean-Christophe PLAGNIOL-VILLARD mem = ((mem - 1) | (bar_size - 1)) + 1;
24393a686eeSJean-Christophe PLAGNIOL-VILLARD bar_value = mem;
24493a686eeSJean-Christophe PLAGNIOL-VILLARD /* compute new region base address */
24593a686eeSJean-Christophe PLAGNIOL-VILLARD mem = mem + bar_size;
24693a686eeSJean-Christophe PLAGNIOL-VILLARD }
24793a686eeSJean-Christophe PLAGNIOL-VILLARD
24893a686eeSJean-Christophe PLAGNIOL-VILLARD /* Write it out and update our limit */
24930e76d5eSKumar Gala pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
25093a686eeSJean-Christophe PLAGNIOL-VILLARD
25193a686eeSJean-Christophe PLAGNIOL-VILLARD if (found_mem64) {
25293a686eeSJean-Christophe PLAGNIOL-VILLARD bar += 4;
25330e76d5eSKumar Gala #ifdef CONFIG_SYS_PCI_64BIT
254cb2bf931SAndrew Sharp pci_hose_write_config_dword(hose, dev, bar,
255cb2bf931SAndrew Sharp (u32)(bar_value >> 32));
25630e76d5eSKumar Gala #else
25793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
25830e76d5eSKumar Gala #endif
25993a686eeSJean-Christophe PLAGNIOL-VILLARD }
26093a686eeSJean-Christophe PLAGNIOL-VILLARD }
26193a686eeSJean-Christophe PLAGNIOL-VILLARD
26293a686eeSJean-Christophe PLAGNIOL-VILLARD /* Configure Cache Line Size Register */
26393a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
26493a686eeSJean-Christophe PLAGNIOL-VILLARD
26593a686eeSJean-Christophe PLAGNIOL-VILLARD /* Configure Latency Timer */
26693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
26793a686eeSJean-Christophe PLAGNIOL-VILLARD
26893a686eeSJean-Christophe PLAGNIOL-VILLARD /* Disable interrupt line, if device says it wants to use interrupts */
26993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
27093a686eeSJean-Christophe PLAGNIOL-VILLARD if (pin != 0) {
2715f48d798SSimon Glass pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE,
2725f48d798SSimon Glass PCI_INTERRUPT_LINE_DISABLE);
27393a686eeSJean-Christophe PLAGNIOL-VILLARD }
27493a686eeSJean-Christophe PLAGNIOL-VILLARD
27593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
27693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
27793a686eeSJean-Christophe PLAGNIOL-VILLARD (old_command & 0xffff0000) | command);
27893a686eeSJean-Christophe PLAGNIOL-VILLARD
27993a686eeSJean-Christophe PLAGNIOL-VILLARD return 0;
28093a686eeSJean-Christophe PLAGNIOL-VILLARD }
28193a686eeSJean-Christophe PLAGNIOL-VILLARD
28293a686eeSJean-Christophe PLAGNIOL-VILLARD /*
28393a686eeSJean-Christophe PLAGNIOL-VILLARD *
28493a686eeSJean-Christophe PLAGNIOL-VILLARD */
28593a686eeSJean-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)28693a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *pci_find_config(struct pci_controller *hose,
28793a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned short class,
28893a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int vendor,
28993a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int device,
29093a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int bus,
29193a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int dev,
29293a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int func)
29393a686eeSJean-Christophe PLAGNIOL-VILLARD {
29493a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *table;
29593a686eeSJean-Christophe PLAGNIOL-VILLARD
29693a686eeSJean-Christophe PLAGNIOL-VILLARD for (table = hose->config_table; table && table->vendor; table++) {
29793a686eeSJean-Christophe PLAGNIOL-VILLARD if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
29893a686eeSJean-Christophe PLAGNIOL-VILLARD (table->device == PCI_ANY_ID || table->device == device) &&
29993a686eeSJean-Christophe PLAGNIOL-VILLARD (table->class == PCI_ANY_ID || table->class == class) &&
30093a686eeSJean-Christophe PLAGNIOL-VILLARD (table->bus == PCI_ANY_ID || table->bus == bus) &&
30193a686eeSJean-Christophe PLAGNIOL-VILLARD (table->dev == PCI_ANY_ID || table->dev == dev) &&
30293a686eeSJean-Christophe PLAGNIOL-VILLARD (table->func == PCI_ANY_ID || table->func == func)) {
30393a686eeSJean-Christophe PLAGNIOL-VILLARD return table;
30493a686eeSJean-Christophe PLAGNIOL-VILLARD }
30593a686eeSJean-Christophe PLAGNIOL-VILLARD }
30693a686eeSJean-Christophe PLAGNIOL-VILLARD
30793a686eeSJean-Christophe PLAGNIOL-VILLARD return NULL;
30893a686eeSJean-Christophe PLAGNIOL-VILLARD }
30993a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_cfgfunc_config_device(struct pci_controller * hose,pci_dev_t dev,struct pci_config_table * entry)31093a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_config_device(struct pci_controller *hose,
31193a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev,
31293a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *entry)
31393a686eeSJean-Christophe PLAGNIOL-VILLARD {
314cb2bf931SAndrew Sharp pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
315cb2bf931SAndrew Sharp entry->priv[2]);
31693a686eeSJean-Christophe PLAGNIOL-VILLARD }
31793a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_cfgfunc_do_nothing(struct pci_controller * hose,pci_dev_t dev,struct pci_config_table * entry)31893a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_cfgfunc_do_nothing(struct pci_controller *hose,
31993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev, struct pci_config_table *entry)
32093a686eeSJean-Christophe PLAGNIOL-VILLARD {
32193a686eeSJean-Christophe PLAGNIOL-VILLARD }
32293a686eeSJean-Christophe PLAGNIOL-VILLARD
32393a686eeSJean-Christophe PLAGNIOL-VILLARD /*
324cb2bf931SAndrew Sharp * HJF: Changed this to return int. I think this is required
32593a686eeSJean-Christophe PLAGNIOL-VILLARD * to get the correct result when scanning bridges
32693a686eeSJean-Christophe PLAGNIOL-VILLARD */
32793a686eeSJean-Christophe PLAGNIOL-VILLARD extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
32893a686eeSJean-Christophe PLAGNIOL-VILLARD
329dc1da42fSStefan Roese #ifdef CONFIG_PCI_SCAN_SHOW
pci_print_dev(struct pci_controller * hose,pci_dev_t dev)3307b19fd6dSJeroen Hofstee __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
331dc1da42fSStefan Roese {
332dc1da42fSStefan Roese if (dev == PCI_BDF(hose->first_busno, 0, 0))
333dc1da42fSStefan Roese return 0;
334dc1da42fSStefan Roese
335dc1da42fSStefan Roese return 1;
336dc1da42fSStefan Roese }
337dc1da42fSStefan Roese #endif /* CONFIG_PCI_SCAN_SHOW */
338dc1da42fSStefan Roese
pci_hose_scan_bus(struct pci_controller * hose,int bus)33993a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan_bus(struct pci_controller *hose, int bus)
34093a686eeSJean-Christophe PLAGNIOL-VILLARD {
34193a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned int sub_bus, found_multi = 0;
34293a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned short vendor, device, class;
34393a686eeSJean-Christophe PLAGNIOL-VILLARD unsigned char header_type;
34403992ac2SAndrew Sharp #ifndef CONFIG_PCI_PNP
34593a686eeSJean-Christophe PLAGNIOL-VILLARD struct pci_config_table *cfg;
34603992ac2SAndrew Sharp #endif
34793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_dev_t dev;
348009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
349009884aeSPeter Tyser static int indent = 0;
350009884aeSPeter Tyser #endif
35193a686eeSJean-Christophe PLAGNIOL-VILLARD
35293a686eeSJean-Christophe PLAGNIOL-VILLARD sub_bus = bus;
35393a686eeSJean-Christophe PLAGNIOL-VILLARD
35493a686eeSJean-Christophe PLAGNIOL-VILLARD for (dev = PCI_BDF(bus,0,0);
355cb2bf931SAndrew Sharp dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
356cb2bf931SAndrew Sharp PCI_MAX_PCI_FUNCTIONS - 1);
357dc1da42fSStefan Roese dev += PCI_BDF(0, 0, 1)) {
358dc1da42fSStefan Roese
359dc1da42fSStefan Roese if (pci_skip_dev(hose, dev))
360dc1da42fSStefan Roese continue;
36193a686eeSJean-Christophe PLAGNIOL-VILLARD
36293a686eeSJean-Christophe PLAGNIOL-VILLARD if (PCI_FUNC(dev) && !found_multi)
36393a686eeSJean-Christophe PLAGNIOL-VILLARD continue;
36493a686eeSJean-Christophe PLAGNIOL-VILLARD
36593a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
36693a686eeSJean-Christophe PLAGNIOL-VILLARD
36793a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
36893a686eeSJean-Christophe PLAGNIOL-VILLARD
369983eb9d1SPeter Tyser if (vendor == 0xffff || vendor == 0x0000)
370983eb9d1SPeter Tyser continue;
37193a686eeSJean-Christophe PLAGNIOL-VILLARD
37293a686eeSJean-Christophe PLAGNIOL-VILLARD if (!PCI_FUNC(dev))
37393a686eeSJean-Christophe PLAGNIOL-VILLARD found_multi = header_type & 0x80;
37493a686eeSJean-Christophe PLAGNIOL-VILLARD
37593a686eeSJean-Christophe PLAGNIOL-VILLARD debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
37693a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
37793a686eeSJean-Christophe PLAGNIOL-VILLARD
37893a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
37993a686eeSJean-Christophe PLAGNIOL-VILLARD pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
38093a686eeSJean-Christophe PLAGNIOL-VILLARD
3810991866cSTim Harvey #ifdef CONFIG_PCI_FIXUP_DEV
3820991866cSTim Harvey board_pci_fixup_dev(hose, dev, vendor, device, class);
3830991866cSTim Harvey #endif
3840991866cSTim Harvey
385a38d216eSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
386009884aeSPeter Tyser indent++;
387009884aeSPeter Tyser
388009884aeSPeter Tyser /* Print leading space, including bus indentation */
389009884aeSPeter Tyser printf("%*c", indent + 1, ' ');
390009884aeSPeter Tyser
391a38d216eSPeter Tyser if (pci_print_dev(hose, dev)) {
392009884aeSPeter Tyser printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
393009884aeSPeter Tyser PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
394a38d216eSPeter Tyser vendor, device, pci_class_str(class >> 8));
395a38d216eSPeter Tyser }
396a38d216eSPeter Tyser #endif
397a38d216eSPeter Tyser
39803992ac2SAndrew Sharp #ifdef CONFIG_PCI_PNP
399b4141195SMasahiro Yamada sub_bus = max((unsigned int)pciauto_config_device(hose, dev),
400b4141195SMasahiro Yamada sub_bus);
40103992ac2SAndrew Sharp #else
40293a686eeSJean-Christophe PLAGNIOL-VILLARD cfg = pci_find_config(hose, class, vendor, device,
40393a686eeSJean-Christophe PLAGNIOL-VILLARD PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
40493a686eeSJean-Christophe PLAGNIOL-VILLARD if (cfg) {
40593a686eeSJean-Christophe PLAGNIOL-VILLARD cfg->config_device(hose, dev, cfg);
406b4141195SMasahiro Yamada sub_bus = max(sub_bus,
407b4141195SMasahiro Yamada (unsigned int)hose->current_busno);
40893a686eeSJean-Christophe PLAGNIOL-VILLARD }
40903992ac2SAndrew Sharp #endif
410a38d216eSPeter Tyser
411009884aeSPeter Tyser #ifdef CONFIG_PCI_SCAN_SHOW
412009884aeSPeter Tyser indent--;
413009884aeSPeter Tyser #endif
414009884aeSPeter Tyser
41593a686eeSJean-Christophe PLAGNIOL-VILLARD if (hose->fixup_irq)
41693a686eeSJean-Christophe PLAGNIOL-VILLARD hose->fixup_irq(hose, dev);
41793a686eeSJean-Christophe PLAGNIOL-VILLARD }
41893a686eeSJean-Christophe PLAGNIOL-VILLARD
41993a686eeSJean-Christophe PLAGNIOL-VILLARD return sub_bus;
42093a686eeSJean-Christophe PLAGNIOL-VILLARD }
42193a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_hose_scan(struct pci_controller * hose)42293a686eeSJean-Christophe PLAGNIOL-VILLARD int pci_hose_scan(struct pci_controller *hose)
42393a686eeSJean-Christophe PLAGNIOL-VILLARD {
4240da1fb03SAnatolij Gustschin #if defined(CONFIG_PCI_BOOTDELAY)
4250da1fb03SAnatolij Gustschin char *s;
4260da1fb03SAnatolij Gustschin int i;
4270da1fb03SAnatolij Gustschin
4288f9052fdSBin Meng if (!gd->pcidelay_done) {
4290da1fb03SAnatolij Gustschin /* wait "pcidelay" ms (if defined)... */
430*00caae6dSSimon Glass s = env_get("pcidelay");
4310da1fb03SAnatolij Gustschin if (s) {
4320da1fb03SAnatolij Gustschin int val = simple_strtoul(s, NULL, 10);
4330da1fb03SAnatolij Gustschin for (i = 0; i < val; i++)
4340da1fb03SAnatolij Gustschin udelay(1000);
4350da1fb03SAnatolij Gustschin }
4368f9052fdSBin Meng gd->pcidelay_done = 1;
4370da1fb03SAnatolij Gustschin }
4380da1fb03SAnatolij Gustschin #endif /* CONFIG_PCI_BOOTDELAY */
4390da1fb03SAnatolij Gustschin
4400373a7e9STim Harvey #ifdef CONFIG_PCI_SCAN_SHOW
4410373a7e9STim Harvey puts("PCI:\n");
4420373a7e9STim Harvey #endif
4430373a7e9STim Harvey
444cb2bf931SAndrew Sharp /*
445cb2bf931SAndrew Sharp * Start scan at current_busno.
44693a686eeSJean-Christophe PLAGNIOL-VILLARD * PCIe will start scan at first_busno+1.
44793a686eeSJean-Christophe PLAGNIOL-VILLARD */
44893a686eeSJean-Christophe PLAGNIOL-VILLARD /* For legacy support, ensure current >= first */
44993a686eeSJean-Christophe PLAGNIOL-VILLARD if (hose->first_busno > hose->current_busno)
45093a686eeSJean-Christophe PLAGNIOL-VILLARD hose->current_busno = hose->first_busno;
45193a686eeSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_PCI_PNP
45293a686eeSJean-Christophe PLAGNIOL-VILLARD pciauto_config_init(hose);
45393a686eeSJean-Christophe PLAGNIOL-VILLARD #endif
45493a686eeSJean-Christophe PLAGNIOL-VILLARD return pci_hose_scan_bus(hose, hose->current_busno);
45593a686eeSJean-Christophe PLAGNIOL-VILLARD }
45693a686eeSJean-Christophe PLAGNIOL-VILLARD
pci_init(void)45793a686eeSJean-Christophe PLAGNIOL-VILLARD void pci_init(void)
45893a686eeSJean-Christophe PLAGNIOL-VILLARD {
45996d61603SJohn Schmoller hose_head = NULL;
46096d61603SJohn Schmoller
461ec21aee6STim Harvey /* allow env to disable pci init/enum */
462*00caae6dSSimon Glass if (env_get("pcidisable") != NULL)
463ec21aee6STim Harvey return;
464ec21aee6STim Harvey
46593a686eeSJean-Christophe PLAGNIOL-VILLARD /* now call board specific pci_init()... */
46693a686eeSJean-Christophe PLAGNIOL-VILLARD pci_init_board();
46793a686eeSJean-Christophe PLAGNIOL-VILLARD }
468287df01eSZhao Qiang
469287df01eSZhao Qiang /* Returns the address of the requested capability structure within the
470287df01eSZhao Qiang * device's PCI configuration space or 0 in case the device does not
471287df01eSZhao Qiang * support it.
472287df01eSZhao Qiang * */
pci_hose_find_capability(struct pci_controller * hose,pci_dev_t dev,int cap)473287df01eSZhao Qiang int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
474287df01eSZhao Qiang int cap)
475287df01eSZhao Qiang {
476287df01eSZhao Qiang int pos;
477287df01eSZhao Qiang u8 hdr_type;
478287df01eSZhao Qiang
479287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type);
480287df01eSZhao Qiang
481287df01eSZhao Qiang pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F);
482287df01eSZhao Qiang
483287df01eSZhao Qiang if (pos)
484287df01eSZhao Qiang pos = pci_find_cap(hose, dev, pos, cap);
485287df01eSZhao Qiang
486287df01eSZhao Qiang return pos;
487287df01eSZhao Qiang }
488287df01eSZhao Qiang
489287df01eSZhao Qiang /* Find the header pointer to the Capabilities*/
pci_hose_find_cap_start(struct pci_controller * hose,pci_dev_t dev,u8 hdr_type)490287df01eSZhao Qiang int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
491287df01eSZhao Qiang u8 hdr_type)
492287df01eSZhao Qiang {
493287df01eSZhao Qiang u16 status;
494287df01eSZhao Qiang
495287df01eSZhao Qiang pci_hose_read_config_word(hose, dev, PCI_STATUS, &status);
496287df01eSZhao Qiang
497287df01eSZhao Qiang if (!(status & PCI_STATUS_CAP_LIST))
498287df01eSZhao Qiang return 0;
499287df01eSZhao Qiang
500287df01eSZhao Qiang switch (hdr_type) {
501287df01eSZhao Qiang case PCI_HEADER_TYPE_NORMAL:
502287df01eSZhao Qiang case PCI_HEADER_TYPE_BRIDGE:
503287df01eSZhao Qiang return PCI_CAPABILITY_LIST;
504287df01eSZhao Qiang case PCI_HEADER_TYPE_CARDBUS:
505287df01eSZhao Qiang return PCI_CB_CAPABILITY_LIST;
506287df01eSZhao Qiang default:
507287df01eSZhao Qiang return 0;
508287df01eSZhao Qiang }
509287df01eSZhao Qiang }
510287df01eSZhao Qiang
pci_find_cap(struct pci_controller * hose,pci_dev_t dev,int pos,int cap)511287df01eSZhao Qiang int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap)
512287df01eSZhao Qiang {
513287df01eSZhao Qiang int ttl = PCI_FIND_CAP_TTL;
514287df01eSZhao Qiang u8 id;
515287df01eSZhao Qiang u8 next_pos;
516287df01eSZhao Qiang
517287df01eSZhao Qiang while (ttl--) {
518287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev, pos, &next_pos);
519287df01eSZhao Qiang if (next_pos < CAP_START_POS)
520287df01eSZhao Qiang break;
521287df01eSZhao Qiang next_pos &= ~3;
522287df01eSZhao Qiang pos = (int) next_pos;
523287df01eSZhao Qiang pci_hose_read_config_byte(hose, dev,
524287df01eSZhao Qiang pos + PCI_CAP_LIST_ID, &id);
525287df01eSZhao Qiang if (id == 0xff)
526287df01eSZhao Qiang break;
527287df01eSZhao Qiang if (id == cap)
528287df01eSZhao Qiang return pos;
529287df01eSZhao Qiang pos += PCI_CAP_LIST_NEXT;
530287df01eSZhao Qiang }
531287df01eSZhao Qiang return 0;
532287df01eSZhao Qiang }
533ed5b580bSMinghuan Lian
534ed5b580bSMinghuan Lian /**
535ed5b580bSMinghuan Lian * pci_find_next_ext_capability - Find an extended capability
536ed5b580bSMinghuan Lian *
537ed5b580bSMinghuan Lian * Returns the address of the next matching extended capability structure
538ed5b580bSMinghuan Lian * within the device's PCI configuration space or 0 if the device does
539ed5b580bSMinghuan Lian * not support it. Some capabilities can occur several times, e.g., the
540ed5b580bSMinghuan Lian * vendor-specific capability, and this provides a way to find them all.
541ed5b580bSMinghuan Lian */
pci_find_next_ext_capability(struct pci_controller * hose,pci_dev_t dev,int start,int cap)542ed5b580bSMinghuan Lian int pci_find_next_ext_capability(struct pci_controller *hose, pci_dev_t dev,
543ed5b580bSMinghuan Lian int start, int cap)
544ed5b580bSMinghuan Lian {
545ed5b580bSMinghuan Lian u32 header;
546ed5b580bSMinghuan Lian int ttl, pos = PCI_CFG_SPACE_SIZE;
547ed5b580bSMinghuan Lian
548ed5b580bSMinghuan Lian /* minimum 8 bytes per capability */
549ed5b580bSMinghuan Lian ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
550ed5b580bSMinghuan Lian
551ed5b580bSMinghuan Lian if (start)
552ed5b580bSMinghuan Lian pos = start;
553ed5b580bSMinghuan Lian
554ed5b580bSMinghuan Lian pci_hose_read_config_dword(hose, dev, pos, &header);
555ed5b580bSMinghuan Lian if (header == 0xffffffff || header == 0)
556ed5b580bSMinghuan Lian return 0;
557ed5b580bSMinghuan Lian
558ed5b580bSMinghuan Lian while (ttl-- > 0) {
559ed5b580bSMinghuan Lian if (PCI_EXT_CAP_ID(header) == cap && pos != start)
560ed5b580bSMinghuan Lian return pos;
561ed5b580bSMinghuan Lian
562ed5b580bSMinghuan Lian pos = PCI_EXT_CAP_NEXT(header);
563ed5b580bSMinghuan Lian if (pos < PCI_CFG_SPACE_SIZE)
564ed5b580bSMinghuan Lian break;
565ed5b580bSMinghuan Lian
566ed5b580bSMinghuan Lian pci_hose_read_config_dword(hose, dev, pos, &header);
567ed5b580bSMinghuan Lian if (header == 0xffffffff || header == 0)
568ed5b580bSMinghuan Lian break;
569ed5b580bSMinghuan Lian }
570ed5b580bSMinghuan Lian
571ed5b580bSMinghuan Lian return 0;
572ed5b580bSMinghuan Lian }
573ed5b580bSMinghuan Lian
574ed5b580bSMinghuan Lian /**
575ed5b580bSMinghuan Lian * pci_hose_find_ext_capability - Find an extended capability
576ed5b580bSMinghuan Lian *
577ed5b580bSMinghuan Lian * Returns the address of the requested extended capability structure
578ed5b580bSMinghuan Lian * within the device's PCI configuration space or 0 if the device does
579ed5b580bSMinghuan Lian * not support it.
580ed5b580bSMinghuan Lian */
pci_hose_find_ext_capability(struct pci_controller * hose,pci_dev_t dev,int cap)581ed5b580bSMinghuan Lian int pci_hose_find_ext_capability(struct pci_controller *hose, pci_dev_t dev,
582ed5b580bSMinghuan Lian int cap)
583ed5b580bSMinghuan Lian {
584ed5b580bSMinghuan Lian return pci_find_next_ext_capability(hose, dev, 0, cap);
585ed5b580bSMinghuan Lian }
586