Resync with changes in grub-experimental
[grub-extras.git] / include_wrap / gpxe / pci.h
blobe064bebb9a205f7a37c2b73e65c10d6cab06eeb8
1 /*
2 * Copyright © 2009 Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
15 * distribution.
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.
29 */
31 FILE_LICENCE ( BSD2 );
33 #ifndef _GPXE_PCI_H
34 #define _GPXE_PCI_H
36 #include <grub/pci.h>
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);
46 static inline void
47 outb (grub_uint8_t data, grub_uint16_t port)
49 return grub_outb (data, GRUB_MACHINE_PCI_IO_BASE + port);
52 static inline void
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);
64 static inline void
65 insw (grub_uint16_t port, grub_uint16_t *data, int count)
67 while (count--)
68 *data++ = grub_inw (GRUB_MACHINE_PCI_IO_BASE + port);
71 static inline void
72 outsw (grub_uint16_t port, grub_uint16_t *data, int count)
74 while (count--)
75 grub_outw (*data++, GRUB_MACHINE_PCI_IO_BASE + port);
78 static inline void
79 outsb (grub_uint16_t port, grub_uint8_t *data, int count)
81 while (count--)
82 grub_outb (*data++, GRUB_MACHINE_PCI_IO_BASE + port);
85 static inline void
86 insb (grub_uint16_t port, grub_uint8_t *data, int count)
88 while (count--)
89 *data++ = grub_inb (GRUB_MACHINE_PCI_IO_BASE + port);
92 static inline void
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 int bus_type;
107 int bus;
108 int location;
109 grub_uint16_t vendor;
110 grub_uint16_t device;
113 struct device
115 struct device_description desc;
116 char *name;
117 union
119 grub_pci_device_t pci_dev;
123 struct pci_device
125 struct device dev;
127 grub_uint16_t ioaddr;
128 grub_uint16_t vendor;
129 grub_uint16_t device;
131 int irq;
133 void *priv;
135 void *drvdata;
138 struct pci_device_id
140 grub_pci_id_t devid;
143 #define PCI_ROM(vendor, model, short_name, long_name, num) {.devid = ((vendor) | ((model) << 16))}
144 #define __pci_driver
146 struct pci_driver
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);
155 static inline void
156 pci_read_config_byte (struct pci_device *dev, grub_uint32_t reg,
157 grub_uint8_t *val)
159 grub_pci_address_t addr;
160 addr = grub_pci_make_address (dev->dev.pci_dev, reg);
161 *val = grub_pci_read_byte (addr);
164 static inline void
165 pci_read_config_word (struct pci_device *dev, grub_uint32_t reg,
166 grub_uint16_t *val)
168 grub_pci_address_t addr;
169 addr = grub_pci_make_address (dev->dev.pci_dev, reg);
170 *val = grub_pci_read_word (addr);
173 static inline void
174 pci_read_config_dword (struct pci_device *dev, grub_uint32_t reg,
175 grub_uint32_t *val)
177 grub_pci_address_t addr;
178 addr = grub_pci_make_address (dev->dev.pci_dev, reg);
179 *val = grub_pci_read (addr);
182 static inline void
183 pci_write_config_byte (struct pci_device *dev, grub_uint32_t reg,
184 grub_uint8_t val)
186 grub_pci_address_t addr;
187 addr = grub_pci_make_address (dev->dev.pci_dev, reg);
188 grub_pci_write_byte (addr, val);
191 static inline void
192 pci_write_config_word (struct pci_device *dev, grub_uint32_t reg,
193 grub_uint16_t val)
195 grub_pci_address_t addr;
196 addr = grub_pci_make_address (dev->dev.pci_dev, reg);
197 grub_pci_write_word (addr, val);
200 static inline void
201 pci_write_config_dword (struct pci_device *dev, grub_uint32_t reg,
202 grub_uint32_t val)
204 grub_pci_address_t addr;
205 addr = grub_pci_make_address (dev->dev.pci_dev, reg);
206 grub_pci_write (addr, val);
209 static inline void *
210 pci_get_drvdata (struct pci_device *dev)
212 return dev->drvdata;
215 static inline void
216 pci_set_drvdata (struct pci_device *dev, void *data)
218 dev->drvdata = data;
221 static inline grub_uint32_t
222 readl (volatile void *ptr)
224 return *(volatile grub_uint32_t *) ptr;
227 static inline void
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;
237 grub_uint64_t space;
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. */
255 static inline void *
256 bus_to_virt (grub_uint32_t bus)
258 return bus;
261 static inline void *
262 ioremap (grub_uint32_t bus, grub_size_t size __attribute__ ((unused)))
264 return bus;
267 static inline grub_uint32_t
268 virt_to_bus (void *virt)
270 return 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
288 #endif