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