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
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))}
148 struct pci_device_id
*ids
;
149 grub_size_t id_count
;
150 int (*probe
) (struct pci_device
*pci
, const struct pci_device_id
*id
);
151 void (*remove
) (struct pci_device
*pci
);
152 void (*irq
) (struct nic
*nic
, int action
);
156 pci_read_config_byte (struct pci_device
*dev
, grub_uint32_t reg
,
159 grub_pci_address_t addr
;
160 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
161 *val
= grub_pci_read_byte (addr
);
165 pci_read_config_word (struct pci_device
*dev
, grub_uint32_t reg
,
168 grub_pci_address_t addr
;
169 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
170 *val
= grub_pci_read_word (addr
);
174 pci_read_config_dword (struct pci_device
*dev
, grub_uint32_t reg
,
177 grub_pci_address_t addr
;
178 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
179 *val
= grub_pci_read (addr
);
183 pci_write_config_byte (struct pci_device
*dev
, grub_uint32_t reg
,
186 grub_pci_address_t addr
;
187 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
188 grub_pci_write_byte (addr
, val
);
192 pci_write_config_word (struct pci_device
*dev
, grub_uint32_t reg
,
195 grub_pci_address_t addr
;
196 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
197 grub_pci_write_word (addr
, val
);
201 pci_write_config_dword (struct pci_device
*dev
, grub_uint32_t reg
,
204 grub_pci_address_t addr
;
205 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
);
206 grub_pci_write (addr
, val
);
210 pci_get_drvdata (struct pci_device
*dev
)
216 pci_set_drvdata (struct pci_device
*dev
, void *data
)
221 static inline grub_uint32_t
222 readl (volatile void *ptr
)
224 return *(volatile grub_uint32_t
*) ptr
;
228 writel (grub_uint32_t data
, volatile void *ptr
)
230 *(volatile grub_uint32_t
*) ptr
= data
;
233 static inline grub_addr_t
234 pci_bar_start (struct pci_device
*dev
, grub_uint32_t reg
)
236 grub_pci_address_t addr
;
239 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, reg
>> 2);
240 space
= grub_pci_read (addr
);
242 if ((space
& GRUB_PCI_ADDR_SPACE_MASK
) == GRUB_PCI_ADDR_SPACE_IO
)
243 return space
& GRUB_PCI_ADDR_IO_MASK
;
245 if ((space
& GRUB_PCI_ADDR_MEM_TYPE_MASK
) == GRUB_PCI_ADDR_MEM_TYPE_64
)
247 addr
= grub_pci_make_address (dev
->dev
.pci_dev
, (reg
>> 2) + 1);
248 space
|= ((grub_uint64_t
) grub_pci_read (addr
)) << 32;
251 return space
& GRUB_PCI_ADDR_MEM_MASK
;
254 /* XXX: make it use grub_pci_device_map_range. */
256 bus_to_virt (grub_uint32_t bus
)
262 ioremap (grub_uint32_t bus
, grub_size_t size
__attribute__ ((unused
)))
267 static inline grub_uint32_t
268 virt_to_bus (void *virt
)
273 void adjust_pci_device ( struct pci_device
*pci
);
275 #define PCI_VENDOR_ID_DAVICOM 0x0291
276 #define PCI_VENDOR_ID_WINBOND2 0x1050
277 #define PCI_VENDOR_ID_COMPEX 0x11f6
278 #define PCI_COMMAND 0x04
279 #define PCI_REVISION_ID 0x8
280 #define PCI_REVISION PCI_REVISION_ID
281 #define PCI_LATENCY_TIMER 0xd
282 #define PCI_BASE_ADDRESS_0 0x10
283 #define PCI_BASE_ADDRESS_1 0x14
284 #define PCI_COMMAND_IO 0x1
285 #define PCI_COMMAND_MEM 0x2
286 #define PCI_COMMAND_MASTER 0x4