2 * The PCI Library -- Generic Direct Access Functions
4 * Copyright (c) 1997--2022 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
14 pci_generic_scan_bus(struct pci_access *a, byte *busmap, int domain, int bus)
19 a->debug("Scanning bus %02x for devices...\n", bus);
22 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);
45 d->domain = t->domain;
49 d->vendor_id = vd & 0xffff;
50 d->device_id = vd >> 16U;
51 d->known_fields = PCI_FILL_IDENT;
56 case PCI_HEADER_TYPE_NORMAL:
58 case PCI_HEADER_TYPE_BRIDGE:
59 case PCI_HEADER_TYPE_CARDBUS:
60 pci_generic_scan_bus(a, busmap, domain, pci_read_byte(t, PCI_SECONDARY_BUS));
63 a->debug("Device %04x:%02x:%02x.%d has unknown header type %02x.\n", d->domain, d->bus, d->dev, d->func, ht);
71 pci_generic_scan_domain(struct pci_access *a, int domain)
75 memset(busmap, 0, sizeof(busmap));
76 pci_generic_scan_bus(a, busmap, domain, 0);
80 pci_generic_scan(struct pci_access *a)
82 pci_generic_scan_domain(a, 0);
86 get_hdr_type(struct pci_dev *d)
89 d->hdrtype = pci_read_byte(d, PCI_HEADER_TYPE) & 0x7f;
94 pci_generic_fill_info(struct pci_dev *d, unsigned int flags)
96 struct pci_access *a = d->access;
99 if (want_fill(d, flags, PCI_FILL_IDENT))
101 d->vendor_id = pci_read_word(d, PCI_VENDOR_ID);
102 d->device_id = pci_read_word(d, PCI_DEVICE_ID);
105 if (want_fill(d, flags, PCI_FILL_CLASS))
106 d->device_class = pci_read_word(d, PCI_CLASS_DEVICE);
108 if (want_fill(d, flags, PCI_FILL_CLASS_EXT))
110 d->prog_if = pci_read_byte(d, PCI_CLASS_PROG);
111 d->rev_id = pci_read_byte(d, PCI_REVISION_ID);
114 if (want_fill(d, flags, PCI_FILL_SUBSYS))
116 switch (get_hdr_type(d))
118 case PCI_HEADER_TYPE_NORMAL:
119 d->subsys_vendor_id = pci_read_word(d, PCI_SUBSYSTEM_VENDOR_ID);
120 d->subsys_id = pci_read_word(d, PCI_SUBSYSTEM_ID);
122 case PCI_HEADER_TYPE_BRIDGE:
123 cap = pci_find_cap(d, PCI_CAP_ID_SSVID, PCI_CAP_NORMAL);
126 d->subsys_vendor_id = pci_read_word(d, cap->addr + PCI_SSVID_VENDOR);
127 d->subsys_id = pci_read_word(d, cap->addr + PCI_SSVID_DEVICE);
130 case PCI_HEADER_TYPE_CARDBUS:
131 d->subsys_vendor_id = pci_read_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
132 d->subsys_id = pci_read_word(d, PCI_CB_SUBSYSTEM_ID);
135 clear_fill(d, PCI_FILL_SUBSYS);
139 if (want_fill(d, flags, PCI_FILL_IRQ))
140 d->irq = pci_read_byte(d, PCI_INTERRUPT_LINE);
142 if (want_fill(d, flags, PCI_FILL_BASES))
145 memset(d->base_addr, 0, sizeof(d->base_addr));
146 switch (get_hdr_type(d))
148 case PCI_HEADER_TYPE_NORMAL:
151 case PCI_HEADER_TYPE_BRIDGE:
154 case PCI_HEADER_TYPE_CARDBUS:
160 for (i=0; i<cnt; i++)
162 u32 x = pci_read_long(d, PCI_BASE_ADDRESS_0 + i*4);
163 if (!x || x == (u32) ~0)
165 if ((x & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
169 if ((x & PCI_BASE_ADDRESS_MEM_TYPE_MASK) != PCI_BASE_ADDRESS_MEM_TYPE_64)
172 a->warning("%04x:%02x:%02x.%d: Invalid 64-bit address seen for BAR %d.", d->domain, d->bus, d->dev, d->func, i);
175 u32 y = pci_read_long(d, PCI_BASE_ADDRESS_0 + (++i)*4);
176 #ifdef PCI_HAVE_64BIT_ADDRESS
177 d->base_addr[i-1] = x | (((pciaddr_t) y) << 32);
180 a->warning("%04x:%02x:%02x.%d 64-bit device address ignored.", d->domain, d->bus, d->dev, d->func);
182 d->base_addr[i-1] = x;
190 if (want_fill(d, flags, PCI_FILL_ROM_BASE))
193 d->rom_base_addr = 0;
194 switch (get_hdr_type(d))
196 case PCI_HEADER_TYPE_NORMAL:
197 reg = PCI_ROM_ADDRESS;
199 case PCI_HEADER_TYPE_BRIDGE:
200 reg = PCI_ROM_ADDRESS1;
205 u32 u = pci_read_long(d, reg);
207 d->rom_base_addr = u;
211 pci_scan_caps(d, flags);
215 pci_generic_block_op(struct pci_dev *d, int pos, byte *buf, int len,
216 int (*r)(struct pci_dev *d, int pos, byte *buf, int len))
218 if ((pos & 1) && len >= 1)
220 if (!r(d, pos, buf, 1))
224 if ((pos & 3) && len >= 2)
226 if (!r(d, pos, buf, 2))
228 pos += 2; buf += 2; len -= 2;
232 if (!r(d, pos, buf, 4))
234 pos += 4; buf += 4; len -= 4;
238 if (!r(d, pos, buf, 2))
240 pos += 2; buf += 2; len -= 2;
242 if (len && !r(d, pos, buf, 1))
248 pci_generic_block_read(struct pci_dev *d, int pos, byte *buf, int len)
250 return pci_generic_block_op(d, pos, buf, len, d->access->methods->read);
254 pci_generic_block_write(struct pci_dev *d, int pos, byte *buf, int len)
256 return pci_generic_block_op(d, pos, buf, len, d->access->methods->write);