a445f253c725ebb05ada21c17a70256f4a66d4c7
[coreboot.git] / src / northbridge / amd / amdk8 / debug.c
1 /*
2  * generic K8 debug code, used by mainboard specific auto.c
3  *
4  */
5
6 #ifndef CACHE_AS_RAM_ADDRESS_DEBUG
7 #define CACHE_AS_RAM_ADDRESS_DEBUG 0
8 #endif
9
10 static inline void print_debug_addr(const char *str, void *val)
11 {
12 #if CACHE_AS_RAM_ADDRESS_DEBUG == 1
13         #if CONFIG_USE_PRINTK_IN_CAR
14                 printk_debug("------Address debug: %s%x------\r\n", str, val);
15         #else
16                 print_debug ("------Address debug: "); print_debug(str); print_debug_hex32(val); print_debug("------\r\n");
17         #endif
18 #endif
19 }
20
21 #if 1
22 static void print_debug_pci_dev(unsigned dev)
23 {
24 #if CONFIG_USE_PRINTK_IN_CAR
25         printk_debug("PCI: %02x:%02x.%02x", (dev>>20) & 0xff, (dev>>15) & 0x1f, (dev>>12) & 0x7);
26 #else
27         print_debug("PCI: ");
28         print_debug_hex8((dev >> 20) & 0xff);
29         print_debug_char(':');
30         print_debug_hex8((dev >> 15) & 0x1f);
31         print_debug_char('.');
32         print_debug_hex8((dev >> 12) & 7);
33 #endif
34 }
35
36 static void print_pci_devices(void)
37 {
38         device_t dev;
39         for(dev = PCI_DEV(0, 0, 0);
40                 dev <= PCI_DEV(0xff, 0x1f, 0x7);
41                 dev += PCI_DEV(0,0,1)) {
42                 uint32_t id;
43                 id = pci_read_config32(dev, PCI_VENDOR_ID);
44                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
45                         (((id >> 16) & 0xffff) == 0xffff) ||
46                         (((id >> 16) & 0xffff) == 0x0000)) {
47                         continue;
48                 }
49                 print_debug_pci_dev(dev);
50 #if CONFIG_USE_PRINTK_IN_CAR
51                 printk_debug(" %04x:%04x\r\n", (id & 0xffff), (id>>16));
52 #else
53                 print_debug(" ");
54                 print_debug_hex32(id);
55                 print_debug("\r\n");
56 #endif
57                 if(((dev>>12) & 0x07) == 0) {
58                         uint8_t hdr_type;
59                         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
60                         if((hdr_type & 0x80) != 0x80) {
61                                 dev += PCI_DEV(0,0,7);
62                         }
63                 }
64         }
65 }
66
67 static void dump_pci_device(unsigned dev)
68 {
69         int i;
70         print_debug_pci_dev(dev);
71
72         for(i = 0; i < 256; i++) {
73                 unsigned char val;
74                 if ((i & 0x0f) == 0) {
75 #if CONFIG_USE_PRINTK_IN_CAR
76                         printk_debug("\r\n%02x:",i);
77 #else
78                         print_debug("\r\n");
79                         print_debug_hex8(i);
80                         print_debug_char(':');
81 #endif
82                 }
83                 val = pci_read_config8(dev, i);
84 #if CONFIG_USE_PRINTK_IN_CAR
85                 printk_debug(" %02x", val);
86 #else
87                 print_debug_char(' ');
88                 print_debug_hex8(val);
89 #endif
90         }
91         print_debug("\r\n");
92 }
93
94 #if CONFIG_K8_REV_F_SUPPORT == 1
95 static uint32_t pci_read_config32_index_wait(device_t dev, uint32_t index_reg, uint32_t index);
96 static void dump_pci_device_index_wait(unsigned dev, uint32_t index_reg)
97 {
98         int i;
99         print_debug_pci_dev(dev);
100         print_debug(" -- index_reg="); print_debug_hex32(index_reg);
101
102         for(i = 0; i < 0x40; i++) {
103                 uint32_t val;
104                 int j;
105 #if CONFIG_USE_PRINTK_IN_CAR
106                 printk_debug("\r\n%02x:",i);
107 #else
108                 print_debug("\r\n");
109                 print_debug_hex8(i);
110                 print_debug_char(':');
111 #endif
112                 val = pci_read_config32_index_wait(dev, index_reg, i);
113                 for(j=0;j<4;j++) {
114 #if CONFIG_USE_PRINTK_IN_CAR
115                         printk_debug(" %02x", val & 0xff);
116 #else
117                         print_debug_char(' '); print_debug_hex8(val&0xff);
118 #endif
119                         val >>= 8;
120                 }
121
122         }
123         print_debug("\r\n");
124 }
125 #endif
126
127 static void dump_pci_devices(void)
128 {
129         device_t dev;
130         for(dev = PCI_DEV(0, 0, 0);
131                 dev <= PCI_DEV(0xff, 0x1f, 0x7);
132                 dev += PCI_DEV(0,0,1)) {
133                 uint32_t id;
134                 id = pci_read_config32(dev, PCI_VENDOR_ID);
135                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
136                         (((id >> 16) & 0xffff) == 0xffff) ||
137                         (((id >> 16) & 0xffff) == 0x0000)) {
138                         continue;
139                 }
140                 dump_pci_device(dev);
141
142                 if(((dev>>12) & 0x07) == 0) {
143                         uint8_t hdr_type;
144                         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
145                         if((hdr_type & 0x80) != 0x80) {
146                                 dev += PCI_DEV(0,0,7);
147                         }
148                 }
149         }
150 }
151
152 static void dump_pci_devices_on_bus(unsigned busn)
153 {
154         device_t dev;
155         for(dev = PCI_DEV(busn, 0, 0);
156                 dev <= PCI_DEV(busn, 0x1f, 0x7);
157                 dev += PCI_DEV(0,0,1)) {
158                 uint32_t id;
159                 id = pci_read_config32(dev, PCI_VENDOR_ID);
160                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
161                         (((id >> 16) & 0xffff) == 0xffff) ||
162                         (((id >> 16) & 0xffff) == 0x0000)) {
163                         continue;
164                 }
165                 dump_pci_device(dev);
166
167                 if(((dev>>12) & 0x07) == 0) {
168                         uint8_t hdr_type;
169                         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
170                         if((hdr_type & 0x80) != 0x80) {
171                                 dev += PCI_DEV(0,0,7);
172                         }
173                 }
174         }
175 }
176
177 #ifndef DEBUG_SMBUS
178 #define DEBUG_SMBUS 0
179 #endif
180
181 #if DEBUG_SMBUS == 1
182 static void dump_spd_registers(const struct mem_controller *ctrl)
183 {
184         int i;
185         print_debug("\r\n");
186         for(i = 0; i < 4; i++) {
187                 unsigned device;
188                 device = ctrl->channel0[i];
189                 if (device) {
190                         int j;
191 #if CONFIG_USE_PRINTK_IN_CAR
192                         printk_debug("dimm: %02x.0: %02x", i, device);
193 #else
194                         print_debug("dimm: ");
195                         print_debug_hex8(i);
196                         print_debug(".0: ");
197                         print_debug_hex8(device);
198 #endif
199                         for(j = 0; j < 128; j++) {
200                                 int status;
201                                 unsigned char byte;
202                                 if ((j & 0xf) == 0) {
203 #if CONFIG_USE_PRINTK_IN_CAR
204                                         printk_debug("\r\n%02x: ", j);
205 #else
206                                         print_debug("\r\n");
207                                         print_debug_hex8(j);
208                                         print_debug(": ");
209 #endif
210                                 }
211                                 status = smbus_read_byte(device, j);
212                                 if (status < 0) {
213                                         break;
214                                 }
215                                 byte = status & 0xff;
216 #if CONFIG_USE_PRINTK_IN_CAR
217                                 printk_debug("%02x ", byte);
218 #else
219                                 print_debug_hex8(byte);
220                                 print_debug_char(' ');
221 #endif
222                         }
223                         print_debug("\r\n");
224                 }
225                 device = ctrl->channel1[i];
226                 if (device) {
227                         int j;
228 #if CONFIG_USE_PRINTK_IN_CAR
229                         printk_debug("dimm: %02x.1: %02x", i, device);
230 #else
231                         print_debug("dimm: ");
232                         print_debug_hex8(i);
233                         print_debug(".1: ");
234                         print_debug_hex8(device);
235 #endif
236                         for(j = 0; j < 128; j++) {
237                                 int status;
238                                 unsigned char byte;
239                                 if ((j & 0xf) == 0) {
240 #if CONFIG_USE_PRINTK_IN_CAR
241                                         printk_debug("\r\n%02x: ", j);
242 #else
243                                         print_debug("\r\n");
244                                         print_debug_hex8(j);
245                                         print_debug(": ");
246 #endif
247                                 }
248                                 status = smbus_read_byte(device, j);
249                                 if (status < 0) {
250                                         break;
251                                 }
252                                 byte = status & 0xff;
253 #if CONFIG_USE_PRINTK_IN_CAR
254                                 printk_debug("%02x ", byte);
255 #else
256                                 print_debug_hex8(byte);
257                                 print_debug_char(' ');
258 #endif
259                         }
260                         print_debug("\r\n");
261                 }
262         }
263 }
264 static void dump_smbus_registers(void)
265 {
266         unsigned device;
267         print_debug("\r\n");
268         for(device = 1; device < 0x80; device++) {
269                 int j;
270                 if( smbus_read_byte(device, 0) < 0 ) continue;
271 #if CONFIG_USE_PRINTK_IN_CAR
272                 printk_debug("smbus: %02x", device);
273 #else
274                 print_debug("smbus: ");
275                 print_debug_hex8(device);
276 #endif
277                 for(j = 0; j < 256; j++) {
278                         int status;
279                         unsigned char byte;
280                         status = smbus_read_byte(device, j);
281                         if (status < 0) {
282                                 break;
283                         }
284                         if ((j & 0xf) == 0) {
285 #if CONFIG_USE_PRINTK_IN_CAR
286                                 printk_debug("\r\n%02x: ",j);
287 #else
288                                 print_debug("\r\n");
289                                 print_debug_hex8(j);
290                                 print_debug(": ");
291 #endif
292                         }
293                         byte = status & 0xff;
294 #if CONFIG_USE_PRINTK_IN_CAR
295                         printk_debug("%02x ", byte);
296 #else
297                         print_debug_hex8(byte);
298                         print_debug_char(' ');
299 #endif
300                 }
301                 print_debug("\r\n");
302         }
303 }
304 #endif
305
306 static void dump_io_resources(unsigned port)
307 {
308
309         int i;
310         udelay(2000);
311 #if CONFIG_USE_PRINTK_IN_CAR
312         printk_debug("%04x:\r\n", port);
313 #else
314         print_debug_hex16(port);
315         print_debug(":\r\n");
316 #endif
317         for(i=0;i<256;i++) {
318                 uint8_t val;
319                 if ((i & 0x0f) == 0) {
320 #if CONFIG_USE_PRINTK_IN_CAR
321                         printk_debug("%02x:", i);
322 #else
323                         print_debug_hex8(i);
324                         print_debug_char(':');
325 #endif
326                 }
327                 val = inb(port);
328 #if CONFIG_USE_PRINTK_IN_CAR
329                 printk_debug(" %02x",val);
330 #else
331                 print_debug_char(' ');
332                 print_debug_hex8(val);
333 #endif
334                 if ((i & 0x0f) == 0x0f) {
335                         print_debug("\r\n");
336                 }
337                 port++;
338         }
339 }
340
341 static void dump_mem(unsigned start, unsigned end)
342 {
343         unsigned i;
344         print_debug("dump_mem:");
345         for(i=start;i<end;i++) {
346                 if((i & 0xf)==0) {
347 #if CONFIG_USE_PRINTK_IN_CAR
348                         printk_debug("\r\n%08x:", i);
349 #else
350                         print_debug("\r\n");
351                         print_debug_hex32(i);
352                         print_debug(":");
353 #endif
354                 }
355 #if CONFIG_USE_PRINTK_IN_CAR
356                 printk_debug(" %02x", (unsigned char)*((unsigned char *)i));
357 #else
358                 print_debug(" ");
359                 print_debug_hex8((unsigned char)*((unsigned char *)i));
360 #endif
361         }
362         print_debug("\r\n");
363  }
364 #endif