1 #ifndef ARCH_ROMCC_IO_H
2 #define ARCH_ROMCC_IO_H 1
7 static inline uint8_t read8(unsigned long addr)
9 return *((volatile uint8_t *)(addr));
12 static inline uint16_t read16(unsigned long addr)
14 return *((volatile uint16_t *)(addr));
17 static inline uint32_t read32(unsigned long addr)
19 return *((volatile uint32_t *)(addr));
22 static inline void write8(unsigned long addr, uint8_t value)
24 *((volatile uint8_t *)(addr)) = value;
27 static inline void write16(unsigned long addr, uint16_t value)
29 *((volatile uint16_t *)(addr)) = value;
32 static inline void write32(unsigned long addr, uint32_t value)
34 *((volatile uint32_t *)(addr)) = value;
37 typedef __builtin_div_t div_t;
38 typedef __builtin_ldiv_t ldiv_t;
39 typedef __builtin_udiv_t udiv_t;
40 typedef __builtin_uldiv_t uldiv_t;
42 static inline div_t div(int numer, int denom)
44 return __builtin_div(numer, denom);
47 static inline ldiv_t ldiv(long numer, long denom)
49 return __builtin_ldiv(numer, denom);
52 static inline udiv_t udiv(unsigned numer, unsigned denom)
54 return __builtin_udiv(numer, denom);
57 static inline uldiv_t uldiv(unsigned long numer, unsigned long denom)
59 return __builtin_uldiv(numer, denom);
64 inline int log2(int value)
66 /* __builtin_bsr is a exactly equivalent to the x86 machine
67 * instruction with the exception that it returns -1
68 * when the value presented to it is zero.
69 * Otherwise __builtin_bsr returns the zero based index of
70 * the highest bit set.
72 return __builtin_bsr(value);
76 static inline int log2(int value)
84 : "=r" (r) : "r" (value));
88 static inline int log2f(int value)
96 : "=r" (r) : "r" (value));
101 #define PCI_ADDR(BUS, DEV, FN, WHERE) ( \
102 (((BUS) & 0xFF) << 16) | \
103 (((DEV) & 0x1f) << 11) | \
104 (((FN) & 0x07) << 8) | \
107 #define PCI_DEV(BUS, DEV, FN) ( \
108 (((BUS) & 0xFF) << 16) | \
109 (((DEV) & 0x1f) << 11) | \
112 #define PCI_ID(VENDOR_ID, DEVICE_ID) \
113 ((((DEVICE_ID) & 0xFFFF) << 16) | ((VENDOR_ID) & 0xFFFF))
116 #define PNP_DEV(PORT, FUNC) (((PORT) << 8) | (FUNC))
118 typedef unsigned device_t;
120 static inline uint8_t pci_read_config8(device_t dev, unsigned where)
124 outl(0x80000000 | (addr & ~3), 0xCF8);
125 return inb(0xCFC + (addr & 3));
128 static inline uint16_t pci_read_config16(device_t dev, unsigned where)
132 outl(0x80000000 | (addr & ~3), 0xCF8);
133 return inw(0xCFC + (addr & 2));
136 static inline uint32_t pci_read_config32(device_t dev, unsigned where)
140 outl(0x80000000 | (addr & ~3), 0xCF8);
144 static inline void pci_write_config8(device_t dev, unsigned where, uint8_t value)
148 outl(0x80000000 | (addr & ~3), 0xCF8);
149 outb(value, 0xCFC + (addr & 3));
152 static inline void pci_write_config16(device_t dev, unsigned where, uint16_t value)
156 outl(0x80000000 | (addr & ~3), 0xCF8);
157 outw(value, 0xCFC + (addr & 2));
160 static inline void pci_write_config32(device_t dev, unsigned where, uint32_t value)
164 outl(0x80000000 | (addr & ~3), 0xCF8);
168 #define PCI_DEV_INVALID (0xffffffffU)
169 static device_t pci_locate_device(unsigned pci_id, device_t dev)
171 for(; dev <= PCI_DEV(255, 31, 7); dev += PCI_DEV(0,0,1)) {
173 id = pci_read_config32(dev, 0);
178 return PCI_DEV_INVALID;
182 /* Generic functions for pnp devices */
183 static inline void pnp_write_config(device_t dev, uint8_t reg, uint8_t value)
185 unsigned port = dev >> 8;
187 outb(value, port +1);
190 static inline uint8_t pnp_read_config(device_t dev, uint8_t reg)
192 unsigned port = dev >> 8;
197 static inline void pnp_set_logical_device(device_t dev)
199 unsigned device = dev & 0xff;
200 pnp_write_config(dev, 0x07, device);
203 static inline void pnp_set_enable(device_t dev, int enable)
205 pnp_write_config(dev, 0x30, enable?0x1:0x0);
208 static inline int pnp_read_enable(device_t dev)
210 return !!pnp_read_config(dev, 0x30);
213 static inline void pnp_set_iobase(device_t dev, unsigned index, unsigned iobase)
215 pnp_write_config(dev, index + 0, (iobase >> 8) & 0xff);
216 pnp_write_config(dev, index + 1, iobase & 0xff);
219 static inline uint16_t pnp_read_iobase(device_t dev, unsigned index)
221 return (uint16_t)((pnp_read_config(dev, index) << 8) | pnp_read_config(dev, index + 1));
224 static inline void pnp_set_irq(device_t dev, unsigned index, unsigned irq)
226 pnp_write_config(dev, index, irq);
229 static inline void pnp_set_drq(device_t dev, unsigned index, unsigned drq)
231 pnp_write_config(dev, index, drq & 0xff);
234 #endif /* ARCH_ROMCC_IO_H */