2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
5 * (C) Copyright 2002, 2003
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 #include <asm/processor.h>
42 #define DEBUGF(x...) printf(x)
51 #define PCI_HOSE_OP(rw, size, type) \
52 int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
54 int offset, type value) \
56 return hose->rw##_##size(hose, dev, offset, value); \
59 PCI_HOSE_OP(read, byte, u8 *)
60 PCI_HOSE_OP(read, word, u16 *)
61 PCI_HOSE_OP(read, dword, u32 *)
62 PCI_HOSE_OP(write, byte, u8)
63 PCI_HOSE_OP(write, word, u16)
64 PCI_HOSE_OP(write, dword, u32)
66 #define PCI_OP(rw, size, type, error_code) \
67 int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
69 struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
77 return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
80 PCI_OP(read, byte, u8 *, *value = 0xff)
81 PCI_OP(read, word, u16 *, *value = 0xffff)
82 PCI_OP(read, dword, u32 *, *value = 0xffffffff)
83 PCI_OP(write, byte, u8, )
84 PCI_OP(write, word, u16, )
85 PCI_OP(write, dword, u32, )
87 #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
88 int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
90 int offset, type val) \
94 if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
97 *val = (val32 >> ((offset & (int)off_mask) * 8)); \
102 #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
103 int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
105 int offset, type val) \
107 u32 val32, mask, ldata; \
109 if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
113 ldata = (((unsigned long)val) & mask) << ((offset & (int)off_mask) * 8);\
114 mask <<= ((mask & (int)off_mask) * 8); \
115 val32 = (val32 & ~mask) | ldata; \
117 if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
123 PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
124 PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
125 PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
126 PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
132 static struct pci_controller* hose_head = NULL;
134 void pci_register_hose(struct pci_controller* hose)
136 struct pci_controller **phose = &hose_head;
139 phose = &(*phose)->next;
146 struct pci_controller *pci_bus_to_hose (int bus)
148 struct pci_controller *hose;
150 for (hose = hose_head; hose; hose = hose->next)
151 if (bus >= hose->first_busno && bus <= hose->last_busno)
157 pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
159 struct pci_controller * hose;
163 int i, bus, found_multi = 0;
165 for (hose = hose_head; hose; hose = hose->next)
167 #if CFG_SCSI_SCAN_BUS_REVERSE
168 for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
170 for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
172 for (bdf = PCI_BDF(bus,0,0);
174 bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
176 bdf < PCI_BDF(bus+1,0,0);
178 bdf += PCI_BDF(0,0,1))
180 if (!PCI_FUNC(bdf)) {
181 pci_read_config_byte(bdf,
185 found_multi = header_type & 0x80;
191 pci_read_config_word(bdf,
194 pci_read_config_word(bdf,
198 for (i=0; ids[i].vendor != 0; i++)
199 if (vendor == ids[i].vendor &&
200 device == ids[i].device)
213 pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
215 static struct pci_device_id ids[2] = {{}, {0, 0}};
217 ids[0].vendor = vendor;
218 ids[0].device = device;
220 return pci_find_devices(ids, index);
227 unsigned long pci_hose_phys_to_bus (struct pci_controller *hose,
228 unsigned long phys_addr,
231 struct pci_region *res;
232 unsigned long bus_addr;
236 printf ("pci_hose_phys_to_bus: %s\n", "invalid hose");
240 for (i = 0; i < hose->region_count; i++) {
241 res = &hose->regions[i];
243 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
246 bus_addr = phys_addr - res->phys_start + res->bus_start;
248 if (bus_addr >= res->bus_start &&
249 bus_addr < res->bus_start + res->size) {
254 printf ("pci_hose_phys_to_bus: %s\n", "invalid physical address");
260 unsigned long pci_hose_bus_to_phys(struct pci_controller* hose,
261 unsigned long bus_addr,
264 struct pci_region *res;
268 printf ("pci_hose_bus_to_phys: %s\n", "invalid hose");
272 for (i = 0; i < hose->region_count; i++) {
273 res = &hose->regions[i];
275 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
278 if (bus_addr >= res->bus_start &&
279 bus_addr < res->bus_start + res->size) {
280 return bus_addr - res->bus_start + res->phys_start;
284 printf ("pci_hose_bus_to_phys: %s\n", "invalid physical address");
294 int pci_hose_config_device(struct pci_controller *hose,
298 unsigned long command)
300 unsigned int bar_response, bar_size, bar_value, old_command;
302 int bar, found_mem64;
304 DEBUGF ("PCI Config: I/O=0x%lx, Memory=0x%lx, Command=0x%lx\n",
307 pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0);
309 for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) {
310 pci_hose_write_config_dword (hose, dev, bar, 0xffffffff);
311 pci_hose_read_config_dword (hose, dev, bar, &bar_response);
318 /* Check the BAR type and set our address mask */
319 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
320 bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
321 /* round up region base address to a multiple of size */
322 io = ((io - 1) | (bar_size - 1)) + 1;
324 /* compute new region base address */
327 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
328 PCI_BASE_ADDRESS_MEM_TYPE_64)
331 bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1;
333 /* round up region base address to multiple of size */
334 mem = ((mem - 1) | (bar_size - 1)) + 1;
336 /* compute new region base address */
337 mem = mem + bar_size;
340 /* Write it out and update our limit */
341 pci_hose_write_config_dword (hose, dev, bar, bar_value);
345 pci_hose_write_config_dword (hose, dev, bar, 0x00000000);
349 /* Configure Cache Line Size Register */
350 pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
352 /* Configure Latency Timer */
353 pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80);
355 /* Disable interrupt line, if device says it wants to use interrupts */
356 pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin);
358 pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff);
361 pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command);
362 pci_hose_write_config_dword (hose, dev, PCI_COMMAND,
363 (old_command & 0xffff0000) | command);
372 struct pci_config_table *pci_find_config(struct pci_controller *hose,
373 unsigned short class,
380 struct pci_config_table *table;
382 for (table = hose->config_table; table && table->vendor; table++) {
383 if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
384 (table->device == PCI_ANY_ID || table->device == device) &&
385 (table->class == PCI_ANY_ID || table->class == class) &&
386 (table->bus == PCI_ANY_ID || table->bus == bus) &&
387 (table->dev == PCI_ANY_ID || table->dev == dev) &&
388 (table->func == PCI_ANY_ID || table->func == func)) {
396 void pci_cfgfunc_config_device(struct pci_controller *hose,
398 struct pci_config_table *entry)
400 pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]);
403 void pci_cfgfunc_do_nothing(struct pci_controller *hose,
404 pci_dev_t dev, struct pci_config_table *entry)
412 /* HJF: Changed this to return int. I think this is required
413 * to get the correct result when scanning bridges
415 extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
416 extern void pciauto_config_init(struct pci_controller *hose);
418 int pci_hose_scan_bus(struct pci_controller *hose, int bus)
420 unsigned int sub_bus, found_multi=0;
421 unsigned short vendor, device, class;
422 unsigned char header_type;
423 struct pci_config_table *cfg;
428 for (dev = PCI_BDF(bus,0,0);
429 dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
430 dev += PCI_BDF(0,0,1))
432 #ifndef CONFIG_405GP /* don't skip host bridge on ppc405gp */
433 /* Skip our host bridge */
434 if ( dev == PCI_BDF(hose->first_busno,0,0) )
438 if (PCI_FUNC(dev) && !found_multi)
441 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
443 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
445 if (vendor != 0xffff && vendor != 0x0000) {
448 found_multi = header_type & 0x80;
450 DEBUGF("PCI Scan: Found Bus %d, Device %d, Function %d\n",
451 PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) );
453 pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
454 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
456 cfg = pci_find_config(hose, class, vendor, device,
457 PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
459 cfg->config_device(hose, dev, cfg);
460 #ifdef CONFIG_PCI_PNP
462 int n = pciauto_config_device(hose, dev);
464 sub_bus = max(sub_bus, n);
468 hose->fixup_irq(hose, dev);
470 #ifdef CONFIG_PCI_SCAN_SHOW
471 /* Skip our host bridge */
472 if ( dev != PCI_BDF(hose->first_busno,0,0) ) {
473 unsigned char int_line;
475 pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE,
477 printf(" %02x %02x %04x %04x %04x %02x\n",
478 PCI_BUS(dev), PCI_DEV(dev), vendor, device, class,
488 int pci_hose_scan(struct pci_controller *hose)
490 #ifdef CONFIG_PCI_PNP
491 pciauto_config_init(hose);
493 return pci_hose_scan_bus(hose, hose->first_busno);
498 #if defined(CONFIG_PCI_BOOTDELAY)
502 /* wait "pcidelay" ms (if defined)... */
503 s = getenv ("pcidelay");
505 int val = simple_strtoul (s, NULL, 10);
506 for (i=0; i<val; i++)
509 #endif /* CONFIG_PCI_BOOTDELAY */
511 /* now call board specific pci_init()... */
515 #endif /* CONFIG_PCI */