- Factoring of auto.c
[coreboot.git] / src / arch / i386 / include / arch / romcc_io.h
1 static void outb(unsigned char value, unsigned short port)
2 {
3         __builtin_outb(value, port);
4 }
5
6 static void outw(unsigned short value, unsigned short port)
7 {
8         __builtin_outw(value, port);
9 }
10
11 static void outl(unsigned int value, unsigned short port)
12 {
13         __builtin_outl(value, port);
14 }
15
16
17 static unsigned char inb(unsigned short port)
18 {
19         return __builtin_inb(port);
20 }
21
22
23 static unsigned char inw(unsigned short port)
24 {
25         return __builtin_inw(port);
26 }
27
28 static unsigned char inl(unsigned short port)
29 {
30         return __builtin_inl(port);
31 }
32
33 static void hlt(void)
34 {
35         __builtin_hlt();
36 }
37
38 typedef __builtin_msr_t msr_t;
39
40 static msr_t rdmsr(unsigned long index)
41 {
42         return __builtin_rdmsr(index);
43 }
44
45 static void wrmsr(unsigned long index, msr_t msr)
46 {
47         __builtin_wrmsr(index, msr.lo, msr.hi);
48 }
49
50 #define PCI_ADDR(BUS, DEV, FN, WHERE) ( \
51         (((BUS) & 0xFF) << 16) | \
52         (((DEV) & 0x1f) << 11) | \
53         (((FN) & 0x07) << 8) | \
54         ((WHERE) & 0xFF))
55
56 #define PCI_ID(VENDOR_ID, DEVICE_ID) \
57         ((((DEVICE_ID) & 0xFFFF) << 16) | ((VENDOR_ID) & 0xFFFF))
58
59 static unsigned char pci_read_config8(unsigned addr)
60 {
61         outl(0x80000000 | (addr & ~3), 0xCF8);
62         return inb(0xCFC + (addr & 3));
63 }
64
65 static unsigned short pci_read_config16(unsigned addr)
66 {
67         outl(0x80000000 | (addr & ~3), 0xCF8);
68         return inw(0xCFC + (addr & 2));
69 }
70
71 static unsigned int pci_read_config32(unsigned addr)
72 {
73         outl(0x80000000 | (addr & ~3), 0xCF8);
74         return inl(0xCFC);
75 }
76
77 static void pci_write_config8(unsigned addr, unsigned char value)
78 {
79         outl(0x80000000 | (addr & ~3), 0xCF8);
80         outb(value, 0xCFC + (addr & 3));
81 }
82
83 static void pci_write_config16(unsigned addr, unsigned short value)
84 {
85         outl(0x80000000 | (addr & ~3), 0xCF8);
86         outw(value, 0xCFC + (addr & 2));
87 }
88
89 static void pci_write_config32(unsigned addr, unsigned int value)
90 {
91         outl(0x80000000 | (addr & ~3), 0xCF8);
92         outl(value, 0xCFC);
93 }
94
95 static unsigned pci_locate_device(unsigned pci_id, unsigned addr)
96 {
97         addr &= ~0xff;
98         for(; addr <= PCI_ADDR(255, 31, 7, 0); addr += PCI_ADDR(0,0,1,0)) {
99                 unsigned int id;
100                 id = pci_read_config32(addr);
101                 if (id == pci_id) {
102                         return addr;
103                 }
104         }
105         return ~0U;
106 }