Add support for AMD IMC controller.
[coreboot.git] / util / romcc / tests / simple_test6.c
1 #define HAVE_CONSTANT_PROPOGATION    1
2
3 void outb(unsigned char value, unsigned short port)
4 {
5         __builtin_outb(value, port);
6 }
7
8 void outw(unsigned short value, unsigned short port)
9 {
10         __builtin_outw(value, port);
11 }
12
13 void outl(unsigned int value, unsigned short port)
14 {
15         __builtin_outl(value, port);
16 }
17
18 unsigned char inb(unsigned short port)
19 {
20         return __builtin_inb(port);
21 }
22
23 unsigned char inw(unsigned short port)
24 {
25         return __builtin_inw(port);
26 }
27
28 unsigned char inl(unsigned short port)
29 {
30         return __builtin_inl(port);
31 }
32
33 static unsigned int config_cmd(unsigned char bus, unsigned devfn, unsigned where)
34 {
35         return 0x80000000 | (bus << 16) | (devfn << 8) | (where & ~3);
36 }
37
38 static unsigned char pcibios_read_config_byte(
39         unsigned char bus, unsigned devfn, unsigned where)
40 {
41         outl(config_cmd(bus, devfn, where), 0xCF8);
42         return inb(0xCFC + (where & 3));
43 }
44
45 static unsigned short pcibios_read_config_word(
46         unsigned char bus, unsigned devfn, unsigned where)
47 {
48         outl(config_cmd(bus, devfn, where), 0xCF8);
49         return inw(0xCFC + (where & 2));
50 }
51
52 static unsigned int pcibios_read_config_dword(
53         unsigned char bus, unsigned devfn, unsigned where)
54 {
55         outl(config_cmd(bus, devfn, where), 0xCF8);
56         return inl(0xCFC);
57 }
58
59
60 static void pcibios_write_config_byte(
61         unsigned char bus, unsigned devfn, unsigned where, unsigned char value)
62 {
63         outl(config_cmd(bus, devfn, where), 0xCF8);
64         outb(value, 0xCFC + (where & 3));
65 }
66
67 static void pcibios_write_config_word(
68         unsigned char bus, unsigned devfn, unsigned where, unsigned short value)
69 {
70         outl(config_cmd(bus, devfn, where), 0xCF8);
71         outw(value, 0xCFC + (where & 2));
72 }
73
74 static void pcibios_write_config_dword(
75         unsigned char bus, unsigned devfn, unsigned where, unsigned int value)
76 {
77         outl(config_cmd(bus, devfn, where), 0xCF8);
78         outl(value, 0xCFC);
79 }
80
81 int log2(int value)
82 {
83         /* __builtin_bsr is a exactly equivalent to the x86 machine
84          * instruction with the exception that it returns -1
85          * when the value presented to it is zero.
86          * Otherwise __builtin_bsr returns the zero based index of
87          * the highest bit set.
88          */
89         return __builtin_bsr(value);
90 }
91
92 #define PIIX4_DEVFN 0x90
93 #define SMBUS_MEM_DEVICE_START 0x50
94 #define SMBUS_MEM_DEVICE_END 0x53
95 #define SMBUS_MEM_DEVICE_INC 1
96
97
98 #define PM_BUS 0
99 #define PM_DEVFN (PIIX4_DEVFN+3)
100
101 #if HAVE_CONSTANT_PROPOGATION
102 #define SMBUS_IO_BASE 0x1000
103 #define SMBHSTSTAT 0
104 #define SMBHSTCTL  2
105 #define SMBHSTCMD  3
106 #define SMBHSTADD  4
107 #define SMBHSTDAT0 5
108 #define SMBHSTDAT1 6
109 #define SMBBLKDAT  7
110
111 static void smbus_wait_until_ready(void)
112 {
113         while((inb(SMBUS_IO_BASE + SMBHSTSTAT) & 1) == 1) {
114                 /* nop */
115         }
116 }
117
118 static void smbus_wait_until_done(void)
119 {
120         unsigned char byte;
121         do {
122                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
123         }while((byte &1) == 1);
124         while( (byte & ~1) == 0) {
125                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
126         }
127 }
128
129 int smbus_read_byte(unsigned device, unsigned address)
130 {
131         unsigned char host_status_register;
132         unsigned char byte;
133         int result;
134
135         smbus_wait_until_ready();
136
137         /* setup transaction */
138         /* disable interrupts */
139         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
140         /* set the device I'm talking too */
141         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBHSTADD);
142         /* set the command/address... */
143         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
144         /* set up for a byte data read */
145         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x2 << 2), SMBUS_IO_BASE + SMBHSTCTL);
146
147         /* clear any lingering errors, so the transaction will run */
148         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
149
150         /* clear the data byte...*/
151         outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
152
153         /* start the command */
154         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40), SMBUS_IO_BASE + SMBHSTCTL);
155
156         /* poll for transaction completion */
157         smbus_wait_until_done();
158
159         host_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
160
161         /* read results of transaction */
162         byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
163
164         result = byte;
165         if (host_status_register != 0x02) {
166                 result = -1;
167         }
168         return result;
169 }
170
171 #else /* !HAVE_CONSTANT_PROPOGATION */
172
173 #define SMBUS_IO_HSTSTAT   0x1000
174 #define SMBUS_IO_HSTCTL    0x1002
175 #define SMBUS_IO_HSTCMD    0x1003
176 #define SMBUS_IO_HSTADD    0x1004
177 #define SMBUS_IO_HSTDAT0   0x1005
178 #define SMBUS_IO_HSTDAT1   0x1006
179 #define SMBUS_IO_HSTBLKDAT 0x1007
180
181
182 static void smbus_wait_until_ready(void)
183 {
184         while((inb(SMBUS_IO_HSTSTAT) & '\1') == '\1') {
185                 /* nop */
186         }
187 }
188
189 static void smbus_wait_until_done(void)
190 {
191         unsigned char byte;
192         do {
193                 byte = inb(SMBUS_IO_HSTSTAT);
194         }while((byte &1) == 1);
195         while( (byte & ~1) == 0) {
196                 byte = inb(SMBUS_IO_HSTSTAT);
197         }
198 }
199
200 short smbus_read_byte(unsigned char device, unsigned char address)
201 {
202         unsigned char host_status_register;
203         short result;
204
205         smbus_wait_until_ready(); /* 2 */
206
207         /* setup transaction */
208         /* disable interrupts */
209         outb(inb(SMBUS_IO_HSTCTL) & (~1), SMBUS_IO_HSTCTL); /* 3 */
210         /* set the device I'm talking too */
211         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_HSTADD); /* 1 + 3 */
212         /* set the command/address... */
213         outb(address & 0xFF, SMBUS_IO_HSTCMD); /* 1 + 3 */
214         /* set up for a byte data read */
215         outb((inb(SMBUS_IO_HSTCTL) & 0xE3) | 8, SMBUS_IO_HSTCTL); /* 3 */
216
217         /* clear any lingering errors, so the transaction will run */
218         outb(inb(SMBUS_IO_HSTSTAT), SMBUS_IO_HSTSTAT); /* 3 */
219
220         /* clear the data byte...*/
221         outb(0, SMBUS_IO_HSTDAT0); /* 3 */
222
223         /* start the command */
224         outb((inb(SMBUS_IO_HSTCTL) | 0x40), SMBUS_IO_HSTCTL);
225
226         /* poll for transaction completion */
227         smbus_wait_until_done();
228
229         host_status_register = inb(SMBUS_IO_HSTSTAT);
230
231         /* read results of transaction */
232         result = inb(SMBUS_IO_HSTDAT0);
233
234         if (host_status_register != 0x02) {
235                 result = -1;
236         }
237         return result;
238 }
239 #endif /* HAVE_CONSTANT_PROPOGATION */
240
241
242 static void test(void)
243 {
244         short byte;
245         unsigned device;
246         unsigned char i, j, k, l, m, n, o;
247         i = 1;
248         j = 2;
249         k = 3;
250         l = 4;
251         m = 5;
252         n = 6;
253         o = 7;
254         device = inb(SMBUS_MEM_DEVICE_START);
255         byte = smbus_read_byte(device, 3);
256         outb(i, 0xab);
257         outb(j, 0xab);
258         outb(k, 0xab);
259         outb(l, 0x1234);
260 #if 1
261         outb(m, 0xab);
262 #endif
263 #if 1
264         outb(n, 0xab);
265 #endif
266 #if 0
267         outb(o, 0xab);
268 #endif
269 }