2 * Copyright © 2009 Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 FILE_LICENCE ( BSD2
);
37 #include <gpxe/wrap.h>
38 #include <gpxe/timer.h>
40 static inline grub_uint8_t
41 inb (grub_uint16_t port
)
43 return grub_inb (GRUB_MACHINE_PCI_IO_BASE
+ port
);
47 outb (grub_uint8_t data
, grub_uint16_t port
)
49 return grub_outb (data
, GRUB_MACHINE_PCI_IO_BASE
+ port
);
53 outw (grub_uint16_t data
, grub_uint16_t port
)
55 return grub_outw (data
, GRUB_MACHINE_PCI_IO_BASE
+ port
);
58 static inline grub_uint16_t
59 inw (grub_uint16_t port
)
61 return grub_inw (GRUB_MACHINE_PCI_IO_BASE
+ port
);
65 insw (grub_uint16_t port
, grub_uint16_t
*data
, int count
)
68 *data
++ = grub_inw (GRUB_MACHINE_PCI_IO_BASE
+ port
);
72 outsw (grub_uint16_t port
, grub_uint16_t
*data
, int count
)
75 grub_outw (*data
++, GRUB_MACHINE_PCI_IO_BASE
+ port
);
79 outsb (grub_uint16_t port
, grub_uint8_t
*data
, int count
)
82 grub_outb (*data
++, GRUB_MACHINE_PCI_IO_BASE
+ port
);
86 insb (grub_uint16_t port
, grub_uint8_t
*data
, int count
)
89 *data
++ = grub_inb (GRUB_MACHINE_PCI_IO_BASE
+ port
);
93 outl (grub_uint32_t data
, grub_uint16_t port
)
95 return grub_outw (data
, GRUB_MACHINE_PCI_IO_BASE
+ port
);
98 static inline grub_uint16_t
99 inl (grub_uint32_t port
)
101 return grub_inw (GRUB_MACHINE_PCI_IO_BASE
+ port
);
104 struct device_description
106 enum {BUS_TYPE_PCI
, BUS_TYPE_ISA
} bus_type
;
109 grub_uint16_t vendor
;
110 grub_uint16_t device
;
115 struct device_description desc
;
119 grub_pci_device_t pci_dev
;
127 grub_uint16_t ioaddr
;
128 grub_uint16_t vendor
;
129 grub_uint16_t device
;
143 #define PCI_ROM(vendor, model, short_name, long_name, num) {.devid = ((vendor) | ((model) << 16))}
150 struct pci_device_id
*ids
;
151 grub_size_t id_count
;
152 int (*probe
) (struct pci_device
*pci
, const struct pci_device_id
*id
);
153 void (*remove
) (struct pci_device
*pci
);
154 void (*irq
) (struct nic
*nic
, int action
);
158 pci_read_config_byte (struct pci_device
*dev
, grub_uint32_t reg
,
161 grub_pci_address_t addr
;
162 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
163 *val
= grub_pci_read_byte (addr
);
167 pci_read_config_word (struct pci_device
*dev
, grub_uint32_t reg
,
170 grub_pci_address_t addr
;
171 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
172 *val
= grub_pci_read_word (addr
);
176 pci_read_config_dword (struct pci_device
*dev
, grub_uint32_t reg
,
179 grub_pci_address_t addr
;
180 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
181 *val
= grub_pci_read (addr
);
185 pci_write_config_byte (struct pci_device
*dev
, grub_uint32_t reg
,
188 grub_pci_address_t addr
;
189 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
190 grub_pci_write_byte (addr
, val
);
194 pci_write_config_word (struct pci_device
*dev
, grub_uint32_t reg
,
197 grub_pci_address_t addr
;
198 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
199 grub_pci_write_word (addr
, val
);
203 pci_write_config_dword (struct pci_device
*dev
, grub_uint32_t reg
,
206 grub_pci_address_t addr
;
207 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
208 grub_pci_write (addr
, val
);
212 pci_get_drvdata (struct pci_device
*dev
)
218 pci_set_drvdata (struct pci_device
*dev
, void *data
)
223 static inline grub_uint32_t
224 readl (volatile void *ptr
)
226 return *(volatile grub_uint32_t
*) ptr
;
230 writel (grub_uint32_t data
, volatile void *ptr
)
232 *(volatile grub_uint32_t
*) ptr
= data
;
235 static inline grub_addr_t
236 pci_bar_start (struct pci_device
*dev
, grub_uint32_t reg
)
238 grub_pci_address_t addr
;
241 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
>> 2);
242 space
= grub_pci_read (addr
);
244 if ((space
& GRUB_PCI_ADDR_SPACE_MASK
) == GRUB_PCI_ADDR_SPACE_IO
)
245 return space
& GRUB_PCI_ADDR_IO_MASK
;
247 if ((space
& GRUB_PCI_ADDR_MEM_TYPE_MASK
) == GRUB_PCI_ADDR_MEM_TYPE_64
)
249 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, (reg
>> 2) + 1);
250 space
|= ((grub_uint64_t
) grub_pci_read (addr
)) << 32;
253 return space
& GRUB_PCI_ADDR_MEM_MASK
;
256 /* XXX: make it use grub_pci_device_map_range. */
258 bus_to_virt (grub_uint32_t bus
)
264 ioremap (grub_uint32_t bus
, grub_size_t size
__attribute__ ((unused
)))
269 static inline grub_uint32_t
270 virt_to_bus (void *virt
)
272 return (grub_addr_t
) virt
;
276 grub_gpxe_register_pci_nic (struct pci_driver
*nic
);
279 grub_gpxe_unregister_pci_nic (struct pci_driver
*nic
);
281 void adjust_pci_device ( struct pci_device
*pci
);
283 #define PCI_VENDOR_ID_DAVICOM 0x0291
284 #define PCI_VENDOR_ID_WINBOND2 0x1050
285 #define PCI_VENDOR_ID_COMPEX 0x11f6
286 #define PCI_COMMAND GRUB_PCI_REG_COMMAND
287 #define PCI_REVISION_ID GRUB_PCI_REG_REVISION
288 #define PCI_REVISION PCI_REVISION_ID
289 #define PCI_LATENCY_TIMER GRUB_PCI_REG_LAT_TIMER
290 #define PCI_BASE_ADDRESS_0 GRUB_PCI_REG_ADDRESS_REG0
291 #define PCI_BASE_ADDRESS_1 GRUB_PCI_REG_ADDRESS_REG1
292 #define PCI_COMMAND_IO 0x1
293 #define PCI_COMMAND_MEM 0x2
294 #define PCI_COMMAND_MASTER 0x4