2 * $Id: generic.c,v 1.8 2002/12/27 19:01:51 mj Exp $
4 * The PCI Library -- Generic Direct Access Functions
6 * Copyright (c) 1997--2000 Martin Mares <mj@ucw.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
16 pci_generic_scan_bus(struct pci_access *a, byte *busmap, int bus)
19 struct pci_dev *t = pci_alloc_dev(a);
21 a->debug("Scanning bus %02x for devices...\n", bus);
24 a->warning("Bus %02x seen twice (firmware bug). Ignored.", bus);
29 for(dev=0; dev<32; dev++)
33 for(t->func=0; !t->func || multi && t->func<8; t->func++)
35 u32 vd = pci_read_long(t, PCI_VENDOR_ID);
38 if (!vd || vd == 0xffffffff)
40 ht = pci_read_byte(t, PCI_HEADER_TYPE);
48 d->vendor_id = vd & 0xffff;
49 d->device_id = vd >> 16U;
50 d->known_fields = PCI_FILL_IDENT;
55 case PCI_HEADER_TYPE_NORMAL:
57 case PCI_HEADER_TYPE_BRIDGE:
58 case PCI_HEADER_TYPE_CARDBUS:
59 pci_generic_scan_bus(a, busmap, pci_read_byte(t, PCI_SECONDARY_BUS));
62 a->debug("Device %02x:%02x.%d has unknown header type %02x.\n", d->bus, d->dev, d->func, ht);
69 pci_generic_scan(struct pci_access *a)
73 bzero(busmap, sizeof(busmap));
74 pci_generic_scan_bus(a, busmap, 0);
78 pci_generic_fill_info(struct pci_dev *d, int flags)
80 struct pci_access *a = d->access;
82 if (flags & PCI_FILL_IDENT)
84 d->vendor_id = pci_read_word(d, PCI_VENDOR_ID);
85 d->device_id = pci_read_word(d, PCI_DEVICE_ID);
87 if (flags & PCI_FILL_IRQ)
88 d->irq = pci_read_byte(d, PCI_INTERRUPT_LINE);
89 if (flags & PCI_FILL_BASES)
92 bzero(d->base_addr, sizeof(d->base_addr));
95 case PCI_HEADER_TYPE_NORMAL:
98 case PCI_HEADER_TYPE_BRIDGE:
101 case PCI_HEADER_TYPE_CARDBUS:
107 u16 cmd = pci_read_word(d, PCI_COMMAND);
110 u32 x = pci_read_long(d, PCI_BASE_ADDRESS_0 + i*4);
111 if (!x || x == (u32) ~0)
114 if (x & PCI_BASE_ADDRESS_SPACE_IO)
116 if (!a->buscentric && !(cmd & PCI_COMMAND_IO))
119 else if (a->buscentric || (cmd & PCI_COMMAND_MEMORY))
121 if ((x & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64)
124 a->warning("%02x:%02x.%d: Invalid 64-bit address seen.", d->bus, d->dev, d->func);
127 u32 y = pci_read_long(d, PCI_BASE_ADDRESS_0 + (++i)*4);
128 #ifdef HAVE_64BIT_ADDRESS
129 d->base_addr[i-1] |= ((pciaddr_t) y) << 32;
133 a->warning("%02x:%02x.%d 64-bit device address ignored.", d->bus, d->dev, d->func);
134 d->base_addr[i-1] = 0;
145 if (flags & PCI_FILL_ROM_BASE)
148 d->rom_base_addr = 0;
151 case PCI_HEADER_TYPE_NORMAL:
152 reg = PCI_ROM_ADDRESS;
154 case PCI_HEADER_TYPE_BRIDGE:
155 reg = PCI_ROM_ADDRESS1;
160 u32 a = pci_read_long(d, reg);
161 if (a & PCI_ROM_ADDRESS_ENABLE)
162 d->rom_base_addr = a;
165 return flags & ~PCI_FILL_SIZES;
169 pci_generic_block_op(struct pci_dev *d, int pos, byte *buf, int len,
170 int (*r)(struct pci_dev *d, int pos, byte *buf, int len))
172 if ((pos & 1) && len >= 1)
174 if (!r(d, pos, buf, 1))
178 if ((pos & 3) && len >= 2)
180 if (!r(d, pos, buf, 2))
182 pos += 2; buf += 2; len -= 2;
186 if (!r(d, pos, buf, 4))
188 pos += 4; buf += 4; len -= 4;
192 if (!r(d, pos, buf, 2))
194 pos += 2; buf += 2; len -= 2;
196 if (len && !r(d, pos, buf, 1))
202 pci_generic_block_read(struct pci_dev *d, int pos, byte *buf, int len)
204 return pci_generic_block_op(d, pos, buf, len, d->access->methods->read);
208 pci_generic_block_write(struct pci_dev *d, int pos, byte *buf, int len)
210 return pci_generic_block_op(d, pos, buf, len, d->access->methods->write);