Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / util / romcc / tests / simple_test5.c
1 #define HAVE_STRING_SUPPORT          0
2 #define HAVE_CAST_SUPPORT            0
3 #define HAVE_STATIC_ARRAY_SUPPORT    0
4 #define HAVE_POINTER_SUPPORT         0
5 #define HAVE_CONSTANT_PROPOGATION    0
6
7 void outb(unsigned char value, unsigned short port)
8 {
9         __builtin_outb(value, port);
10 }
11
12 void outw(unsigned short value, unsigned short port)
13 {
14         __builtin_outw(value, port);
15 }
16
17 void outl(unsigned int value, unsigned short port)
18 {
19         __builtin_outl(value, port);
20 }
21
22 unsigned char inb(unsigned short port)
23 {
24         return __builtin_inb(port);
25 }
26
27 unsigned char inw(unsigned short port)
28 {
29         return __builtin_inw(port);
30 }
31
32 unsigned char inl(unsigned short port)
33 {
34         return __builtin_inl(port);
35 }
36
37 static unsigned int config_cmd(unsigned char bus, unsigned devfn, unsigned where)
38 {
39         return 0x80000000 | (bus << 16) | (devfn << 8) | (where & ~3);
40 }
41
42 static unsigned char pcibios_read_config_byte(
43         unsigned char bus, unsigned devfn, unsigned where)
44 {
45         outl(config_cmd(bus, devfn, where), 0xCF8);
46         return inb(0xCFC + (where & 3));
47 }
48
49 static unsigned short pcibios_read_config_word(
50         unsigned char bus, unsigned devfn, unsigned where)
51 {
52         outl(config_cmd(bus, devfn, where), 0xCF8);
53         return inw(0xCFC + (where & 2));
54 }
55
56 static unsigned int pcibios_read_config_dword(
57         unsigned char bus, unsigned devfn, unsigned where)
58 {
59         outl(config_cmd(bus, devfn, where), 0xCF8);
60         return inl(0xCFC);
61 }
62
63
64 static void pcibios_write_config_byte(
65         unsigned char bus, unsigned devfn, unsigned where, unsigned char value)
66 {
67         outl(config_cmd(bus, devfn, where), 0xCF8);
68         outb(value, 0xCFC + (where & 3));
69 }
70
71 static void pcibios_write_config_word(
72         unsigned char bus, unsigned devfn, unsigned where, unsigned short value)
73 {
74         outl(config_cmd(bus, devfn, where), 0xCF8);
75         outw(value, 0xCFC + (where & 2));
76 }
77
78 static void pcibios_write_config_dword(
79         unsigned char bus, unsigned devfn, unsigned where, unsigned int value)
80 {
81         outl(config_cmd(bus, devfn, where), 0xCF8);
82         outl(value, 0xCFC);
83 }
84
85 int log2(int value)
86 {
87         /* __builtin_bsr is a exactly equivalent to the x86 machine
88          * instruction with the exception that it returns -1
89          * when the value presented to it is zero.
90          * Otherwise __builtin_bsr returns the zero based index of
91          * the highest bit set.
92          */
93         return __builtin_bsr(value);
94 }
95
96 #define PIIX4_DEVFN 0x90
97 #define SMBUS_MEM_DEVICE_START 0x50
98 #define SMBUS_MEM_DEVICE_END 0x53
99 #define SMBUS_MEM_DEVICE_INC 1
100
101
102 #define PM_BUS 0
103 #define PM_DEVFN (PIIX4_DEVFN+3)
104
105 #if HAVE_CONSTANT_PROPOGATION
106 #define SMBUS_IO_BASE 0x1000
107 #define SMBHSTSTAT 0
108 #define SMBHSTCTL  2
109 #define SMBHSTCMD  3
110 #define SMBHSTADD  4
111 #define SMBHSTDAT0 5
112 #define SMBHSTDAT1 6
113 #define SMBBLKDAT  7
114
115 static void smbus_wait_until_ready(void)
116 {
117         while((inb(SMBUS_IO_BASE + SMBHSTSTAT) & 1) == 1) {
118                 /* nop */
119         }
120 }
121
122 static void smbus_wait_until_done(void)
123 {
124         unsigned char byte;
125         do {
126                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
127         }while((byte &1) == 1);
128         while( (byte & ~1) == 0) {
129                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
130         }
131 }
132
133 int smbus_read_byte(unsigned device, unsigned address)
134 {
135         unsigned char host_status_register;
136         unsigned char byte;
137         int result;
138
139         smbus_wait_until_ready();
140
141         /* setup transaction */
142         /* disable interrupts */
143         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
144         /* set the device I'm talking too */
145         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBHSTADD);
146         /* set the command/address... */
147         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
148         /* set up for a byte data read */
149         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x2 << 2), SMBUS_IO_BASE + SMBHSTCTL);
150
151         /* clear any lingering errors, so the transaction will run */
152         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
153
154         /* clear the data byte...*/
155         outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
156
157         /* start the command */
158         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40), SMBUS_IO_BASE + SMBHSTCTL);
159
160         /* poll for transaction completion */
161         smbus_wait_until_done();
162
163         host_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
164
165         /* read results of transaction */
166         byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
167
168         result = byte;
169         if (host_status_register != 0x02) {
170                 result = -1;
171         }
172         return result;
173 }
174
175 #else /* !HAVE_CONSTANT_PROPOGATION */
176
177 #define SMBUS_IO_HSTSTAT   0x1000
178 #define SMBUS_IO_HSTCTL    0x1002
179 #define SMBUS_IO_HSTCMD    0x1003
180 #define SMBUS_IO_HSTADD    0x1004
181 #define SMBUS_IO_HSTDAT0   0x1005
182 #define SMBUS_IO_HSTDAT1   0x1006
183 #define SMBUS_IO_HSTBLKDAT 0x1007
184
185
186 static void smbus_wait_until_ready(void)
187 {
188         while((inb(SMBUS_IO_HSTSTAT) & 1) == 1) {
189                 /* nop */
190         }
191 }
192
193 static void smbus_wait_until_done(void)
194 {
195         unsigned char byte;
196         do {
197                 byte = inb(SMBUS_IO_HSTSTAT);
198         }while((byte &1) == 1);
199         while( (byte & ~1) == 0) {
200                 byte = inb(SMBUS_IO_HSTSTAT);
201         }
202 }
203
204 int smbus_read_byte(unsigned device, unsigned address)
205 {
206         unsigned char host_status_register;
207         int result;
208
209         smbus_wait_until_ready();
210
211         /* setup transaction */
212         /* disable interrupts */
213         outb(inb(SMBUS_IO_HSTCTL) & (~1), SMBUS_IO_HSTCTL);
214         /* set the device I'm talking too */
215         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_HSTADD);
216         /* set the command/address... */
217         outb(address & 0xFF, SMBUS_IO_HSTCMD);
218         /* set up for a byte data read */
219         outb((inb(SMBUS_IO_HSTCTL) & 0xE3) | 8, SMBUS_IO_HSTCTL);
220
221         /* clear any lingering errors, so the transaction will run */
222         outb(inb(SMBUS_IO_HSTSTAT), SMBUS_IO_HSTSTAT);
223
224         /* clear the data byte...*/
225         outb(0, SMBUS_IO_HSTDAT0);
226
227         /* start the command */
228         outb((inb(SMBUS_IO_HSTCTL) | 0x40), SMBUS_IO_HSTCTL);
229
230         /* poll for transaction completion */
231         smbus_wait_until_done();
232
233         host_status_register = inb(SMBUS_IO_HSTSTAT);
234
235         /* read results of transaction */
236         result = inb(SMBUS_IO_HSTDAT0);
237
238         if (host_status_register != 0x02) {
239                 result = -1;
240         }
241         return result;
242 }
243 #endif /* HAVE_CONSTANT_PROPOGATION */
244
245
246 #define I440GX_BUS 0
247 #define I440GX_DEVFN ((0x00 << 3) + 0)
248
249 void sdram_no_memory(void)
250 {
251 #if HAVE_STRING_SUPPORT
252         print_err("No memory!!\n");
253 #endif
254         while(1) ;
255 }
256
257 static void spd_enable_refresh(void)
258 {
259         /*
260          * Effects:     Uses serial presence detect to set the
261          *              refresh rate in the DRAMC register.
262          *              see spd_set_dramc for the other values.
263          * FIXME:       Check for illegal/unsupported ram configurations and abort
264          */
265 #if HAVE_STATIC_ARRAY_SUPPORT
266         static const unsigned char refresh_rates[] = {
267                 0x01, /* Normal        15.625 us -> 15.6 us */
268                 0x05, /* Reduced(.25X) 3.9 us    -> 7.8 us */
269                 0x05, /* Reduced(.5X)  7.8 us    -> 7.8 us */
270                 0x02, /* Extended(2x)  31.3 us   -> 31.2 us */
271                 0x03, /* Extended(4x)  62.5 us   -> 62.4 us */
272                 0x04, /* Extended(8x)  125 us    -> 124.8 us */
273         };
274 #endif
275         /* Find the first dimm and assume the rest are the same */
276         int status;
277         int byte;
278         unsigned device;
279         unsigned refresh_rate;
280         byte = -1;
281         status = -1;
282         device = SMBUS_MEM_DEVICE_START;
283         while ((byte < 0) && (device <= SMBUS_MEM_DEVICE_END)) {
284                 byte = smbus_read_byte(device, 12);
285                 device += SMBUS_MEM_DEVICE_INC;
286         }
287         if (byte < 0) {
288                 /* We couldn't find anything we must have no memory */
289                 sdram_no_memory();
290         }
291         byte &= 0x7f;
292         /* Default refresh rate be conservative */
293         refresh_rate = 5;
294         /* see if the ram refresh is a supported one */
295         if (byte < 6) {
296 #if HAVE_STATIC_ARRAY_SUPPORT
297                 refresh_rate = refresh_rates[byte];
298 #endif
299         }
300         byte = pcibios_read_config_byte(I440GX_BUS, I440GX_DEVFN, 0x57);
301         byte &= 0xf8;
302         byte |= refresh_rate;
303         pcibios_write_config_byte(I440GX_BUS, I440GX_DEVFN, 0x57, byte);
304 }
305
306 void sdram_enable_refresh(void)
307 {
308         spd_enable_refresh();
309 }
310