6cb6a767d9145e20b751aa5b96763b846ddbbfab
[coreboot.git] / src / arch / i386 / include / arch / romcc_io.h
1 #ifndef ARCH_ROMCC_IO_H
2 #define ARCH_ROMCC_IO_H 1
3
4 #include <stdint.h>
5
6
7 static inline __attribute__((always_inline)) uint8_t read8(unsigned long addr)
8 {
9         return *((volatile uint8_t *)(addr));
10 }
11
12 static inline __attribute__((always_inline)) uint16_t read16(unsigned long addr)
13 {
14         return *((volatile uint16_t *)(addr));
15 }
16
17 static inline __attribute__((always_inline)) uint32_t read32(unsigned long addr)
18 {
19         return *((volatile uint32_t *)(addr));
20 }
21
22 static inline __attribute__((always_inline)) void write8(unsigned long addr, uint8_t value)
23 {
24         *((volatile uint8_t *)(addr)) = value;
25 }
26
27 static inline __attribute__((always_inline)) void write16(unsigned long addr, uint16_t value)
28 {
29         *((volatile uint16_t *)(addr)) = value;
30 }
31
32 static inline __attribute__((always_inline)) void write32(unsigned long addr, uint32_t value)
33 {
34         *((volatile uint32_t *)(addr)) = value;
35 }
36
37 #if MMCONF_SUPPORT
38
39 #include <arch/mmio_conf.h>
40
41 #endif
42
43 static inline int log2(int value)
44 {
45         unsigned int r = 0;
46         __asm__ volatile (
47                 "bsrl %1, %0\n\t"
48                 "jnz 1f\n\t"
49                 "movl $-1, %0\n\t"
50                 "1:\n\t"
51                 : "=r" (r) : "r" (value));
52         return r;
53
54 }
55 static inline int log2f(int value)
56 {
57         unsigned int r = 0;
58         __asm__ volatile (
59                 "bsfl %1, %0\n\t"
60                 "jnz 1f\n\t"
61                 "movl $-1, %0\n\t"
62                 "1:\n\t"
63                 : "=r" (r) : "r" (value));
64         return r;
65
66 }
67
68 #define PCI_ADDR(SEGBUS, DEV, FN, WHERE) ( \
69         (((SEGBUS) & 0xFFF) << 20) | \
70         (((DEV) & 0x1F) << 15) | \
71         (((FN) & 0x07) << 12) | \
72         ((WHERE) & 0xFFF))
73
74 #define PCI_DEV(SEGBUS, DEV, FN) ( \
75         (((SEGBUS) & 0xFFF) << 20) | \
76         (((DEV) & 0x1F) << 15) | \
77         (((FN)  & 0x07) << 12))
78
79 #define PCI_ID(VENDOR_ID, DEVICE_ID) \
80         ((((DEVICE_ID) & 0xFFFF) << 16) | ((VENDOR_ID) & 0xFFFF))
81
82
83 #define PNP_DEV(PORT, FUNC) (((PORT) << 8) | (FUNC))
84
85 typedef unsigned device_t; /* pci and pci_mmio need to have different ways to have dev */
86
87 /* FIXME: We need to make the LinuxBIOS to run at 64bit mode, So when read/write memory above 4G, 
88  * We don't need to set %fs, and %gs anymore
89  * Before that We need to use %gs, and leave %fs to other RAM access
90  */
91
92 static inline __attribute__((always_inline)) uint8_t pci_io_read_config8(device_t dev, unsigned where)
93 {
94         unsigned addr;
95 #if PCI_IO_CFG_EXT == 0
96         addr = (dev>>4) | where;
97 #else
98         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16); //seg == 0
99 #endif
100         outl(0x80000000 | (addr & ~3), 0xCF8);
101         return inb(0xCFC + (addr & 3));
102 }
103
104 #if MMCONF_SUPPORT
105 static inline __attribute__((always_inline)) uint8_t pci_mmio_read_config8(device_t dev, unsigned where)
106 {
107         unsigned addr;
108         addr = dev | where;
109         return read8x(addr);
110 }
111 #endif
112 static inline __attribute__((always_inline)) uint8_t pci_read_config8(device_t dev, unsigned where)
113 {
114 #if MMCONF_SUPPORT
115         return pci_mmio_read_config8(dev, where);
116 #else
117         return pci_io_read_config8(dev, where);
118 #endif
119 }
120
121 static inline __attribute__((always_inline)) uint16_t pci_io_read_config16(device_t dev, unsigned where)
122 {
123         unsigned addr;
124 #if PCI_IO_CFG_EXT == 0
125         addr = (dev>>4) | where;
126 #else
127         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
128 #endif
129         outl(0x80000000 | (addr & ~3), 0xCF8);
130         return inw(0xCFC + (addr & 2));
131 }
132
133 #if MMCONF_SUPPORT
134 static inline __attribute__((always_inline)) uint16_t pci_mmio_read_config16(device_t dev, unsigned where)
135 {
136         unsigned addr;
137         addr = dev | where;
138         return read16x(addr);
139 }
140 #endif
141
142 static inline __attribute__((always_inline)) uint16_t pci_read_config16(device_t dev, unsigned where)
143 {
144 #if MMCONF_SUPPORT
145         return pci_mmio_read_config16(dev, where);
146 #else
147         return pci_io_read_config16(dev, where);
148 #endif
149 }
150
151
152 static inline __attribute__((always_inline)) uint32_t pci_io_read_config32(device_t dev, unsigned where)
153 {
154         unsigned addr;
155 #if PCI_IO_CFG_EXT == 0
156         addr = (dev>>4) | where;
157 #else
158         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
159 #endif
160         outl(0x80000000 | (addr & ~3), 0xCF8);
161         return inl(0xCFC);
162 }
163
164 #if MMCONF_SUPPORT
165 static inline __attribute__((always_inline)) uint32_t pci_mmio_read_config32(device_t dev, unsigned where)
166 {
167         unsigned addr;
168         addr = dev | where;
169         return read32x(addr);
170 }
171 #endif
172
173 static inline __attribute__((always_inline)) uint32_t pci_read_config32(device_t dev, unsigned where)
174 {
175 #if MMCONF_SUPPORT
176         return pci_mmio_read_config32(dev, where);
177 #else
178         return pci_io_read_config32(dev, where);
179 #endif
180 }
181
182 static inline __attribute__((always_inline)) void pci_io_write_config8(device_t dev, unsigned where, uint8_t value)
183 {
184         unsigned addr;
185 #if PCI_IO_CFG_EXT == 0
186         addr = (dev>>4) | where;
187 #else
188         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
189 #endif
190         outl(0x80000000 | (addr & ~3), 0xCF8);
191         outb(value, 0xCFC + (addr & 3));
192 }
193
194 #if MMCONF_SUPPORT
195 static inline __attribute__((always_inline)) void pci_mmio_write_config8(device_t dev, unsigned where, uint8_t value)
196 {
197         unsigned addr;
198         addr = dev | where;
199         write8x(addr, value);
200 }
201 #endif
202
203 static inline __attribute__((always_inline)) void pci_write_config8(device_t dev, unsigned where, uint8_t value)
204 {
205 #if MMCONF_SUPPORT
206         pci_mmio_write_config8(dev, where, value);
207 #else
208         pci_io_write_config8(dev, where, value);
209 #endif
210 }
211
212
213 static inline __attribute__((always_inline)) void pci_io_write_config16(device_t dev, unsigned where, uint16_t value)
214 {
215         unsigned addr;
216 #if PCI_IO_CFG_EXT == 0
217         addr = (dev>>4) | where;
218 #else
219         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
220 #endif
221         outl(0x80000000 | (addr & ~3), 0xCF8);
222         outw(value, 0xCFC + (addr & 2));
223 }
224
225 #if MMCONF_SUPPORT
226 static inline __attribute__((always_inline)) void pci_mmio_write_config16(device_t dev, unsigned where, uint16_t value)
227 {
228         unsigned addr;
229         addr = dev | where;
230         write16x(addr, value);
231 }
232 #endif
233
234 static inline __attribute__((always_inline)) void pci_write_config16(device_t dev, unsigned where, uint16_t value)
235 {
236 #if MMCONF_SUPPORT
237         pci_mmio_write_config16(dev, where, value);
238 #else
239         pci_io_write_config16(dev, where, value);
240 #endif
241 }
242
243
244 static inline __attribute__((always_inline)) void pci_io_write_config32(device_t dev, unsigned where, uint32_t value)
245 {
246         unsigned addr;
247 #if PCI_IO_CFG_EXT == 0
248         addr = (dev>>4) | where;
249 #else
250         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
251 #endif
252         outl(0x80000000 | (addr & ~3), 0xCF8);
253         outl(value, 0xCFC);
254 }
255
256 #if MMCONF_SUPPORT
257 static inline __attribute__((always_inline)) void pci_mmio_write_config32(device_t dev, unsigned where, uint32_t value)
258 {
259         unsigned addr;
260         addr = dev | where;
261         write32x(addr, value);
262 }
263 #endif
264
265 static inline __attribute__((always_inline)) void pci_write_config32(device_t dev, unsigned where, uint32_t value)
266 {
267 #if MMCONF_SUPPORT
268         pci_mmio_write_config32(dev, where, value);
269 #else
270         pci_io_write_config32(dev, where, value);
271 #endif
272 }
273
274 #define PCI_DEV_INVALID (0xffffffffU)
275 static device_t pci_io_locate_device(unsigned pci_id, device_t dev)
276 {
277         for(; dev <= PCI_DEV(255, 31, 7); dev += PCI_DEV(0,0,1)) {
278                 unsigned int id;
279                 id = pci_io_read_config32(dev, 0);
280                 if (id == pci_id) {
281                         return dev;
282                 }
283         }
284         return PCI_DEV_INVALID;
285 }
286
287 static device_t pci_locate_device(unsigned pci_id, device_t dev)
288 {
289         for(; dev <= PCI_DEV(255|(((1<<PCI_BUS_SEGN_BITS)-1)<<8), 31, 7); dev += PCI_DEV(0,0,1)) {
290                 unsigned int id;
291                 id = pci_read_config32(dev, 0);
292                 if (id == pci_id) {
293                         return dev;
294                 }
295         }
296         return PCI_DEV_INVALID;
297 }
298
299 static device_t pci_locate_device_on_bus(unsigned pci_id, unsigned bus)
300 {
301         device_t dev, last;
302
303         dev = PCI_DEV(bus, 0, 0);
304         last = PCI_DEV(bus, 31, 7);
305         
306         for(; dev <=last; dev += PCI_DEV(0,0,1)) {
307                 unsigned int id;
308                 id = pci_read_config32(dev, 0);
309                 if (id == pci_id) {
310                         return dev;
311                 }
312         }
313         return PCI_DEV_INVALID;
314 }
315
316 /* Generic functions for pnp devices */
317 static inline __attribute__((always_inline)) void pnp_write_config(device_t dev, uint8_t reg, uint8_t value)
318 {
319         unsigned port = dev >> 8;
320         outb(reg, port );
321         outb(value, port +1);
322 }
323
324 static inline __attribute__((always_inline)) uint8_t pnp_read_config(device_t dev, uint8_t reg)
325 {
326         unsigned port = dev >> 8;
327         outb(reg, port);
328         return inb(port +1);
329 }
330
331 static inline __attribute__((always_inline)) void pnp_set_logical_device(device_t dev)
332 {
333         unsigned device = dev & 0xff;
334         pnp_write_config(dev, 0x07, device);
335 }
336
337 static inline __attribute__((always_inline)) void pnp_set_enable(device_t dev, int enable)
338 {
339         pnp_write_config(dev, 0x30, enable?0x1:0x0);
340 }
341
342 static inline __attribute__((always_inline)) int pnp_read_enable(device_t dev)
343 {
344         return !!pnp_read_config(dev, 0x30);
345 }
346
347 static inline __attribute__((always_inline)) void pnp_set_iobase(device_t dev, unsigned index, unsigned iobase)
348 {
349         pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
350         pnp_write_config(dev, index + 1, iobase & 0xff);
351 }
352
353 static inline __attribute__((always_inline)) uint16_t pnp_read_iobase(device_t dev, unsigned index)
354 {
355         return ((uint16_t)(pnp_read_config(dev, index)) << 8) | pnp_read_config(dev, index + 1);
356 }
357
358 static inline __attribute__((always_inline)) void pnp_set_irq(device_t dev, unsigned index, unsigned irq)
359 {
360         pnp_write_config(dev, index, irq);
361 }
362
363 static inline __attribute__((always_inline)) void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
364 {
365         pnp_write_config(dev, index, drq & 0xff);
366 }
367
368 #endif /* ARCH_ROMCC_IO_H */