Revision: linuxbios@linuxbios.org--devel/freebios--devel--2.0--patch-30
[coreboot.git] / src / northbridge / amd / amdk8 / incoherent_ht.c
1 /*
2         This should be done by Eric
3         2004.12 yhlu add multi ht chain dynamically support
4 */
5 #include <device/pci_def.h>
6 #include <device/pci_ids.h>
7 #include <device/hypertransport_def.h>
8
9 #ifndef K8_HT_FREQ_1G_SUPPORT
10         #define K8_HT_FREQ_1G_SUPPORT 0
11 #endif
12
13 static inline void print_linkn_in (const char *strval, uint8_t byteval)
14 {
15 #if 0
16         print_debug(strval); print_debug_hex8(byteval); print_debug("\r\n");
17 #endif
18 }
19
20 static uint8_t ht_lookup_slave_capability(device_t dev)
21 {
22         uint8_t pos;
23         uint8_t hdr_type;
24
25         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
26         pos = 0;
27         hdr_type &= 0x7f;
28
29         if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
30             (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
31                 pos = PCI_CAPABILITY_LIST;
32         }
33         if (pos > PCI_CAP_LIST_NEXT) {
34                 pos = pci_read_config8(dev, pos);
35         }
36         while(pos != 0) { /* loop through the linked list */
37                 uint8_t cap;
38                 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
39                 if (cap == PCI_CAP_ID_HT) {
40                         uint16_t flags;
41
42                         flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
43                         if ((flags >> 13) == 0) {
44                                 /* Entry is a Slave secondary, success... */
45                                 break;
46                         }
47                 }
48                 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
49         }
50         return pos;
51 }
52
53 static void ht_collapse_previous_enumeration(uint8_t bus)
54 {
55         device_t dev;
56         uint32_t id;
57
58         /* Check if is already collapsed */
59         dev = PCI_DEV(bus, 0, 0);
60         id = pci_read_config32(dev, PCI_VENDOR_ID);
61         if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
62             (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
63                      return;
64         }
65
66         /* Spin through the devices and collapse any previous
67          * hypertransport enumeration.
68          */
69         for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
70                 uint32_t id;
71                 uint8_t pos;
72                 uint16_t flags;
73                 
74                 id = pci_read_config32(dev, PCI_VENDOR_ID);
75                 if ((id == 0xffffffff) || (id == 0x00000000) ||
76                     (id == 0x0000ffff) || (id == 0xffff0000)) {
77                         continue;
78                 }
79 #if 0
80 #if CK804_DEVN_BASE==0 
81                 //CK804 workaround: 
82                 // CK804 UnitID changes not use
83                 if(id == 0x005e10de) {
84                         break;
85                 }
86 #endif
87 #endif
88                 
89                 pos = ht_lookup_slave_capability(dev);
90                 if (!pos) {
91                         continue;
92                 }
93
94                 /* Clear the unitid */
95                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
96                 flags &= ~0x1f;
97                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
98         }
99 }
100
101 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
102 {
103         /* Handle bugs in valid hypertransport frequency reporting */
104         uint16_t freq_cap;
105         uint32_t id;
106
107         freq_cap = pci_read_config16(dev, pos);
108         freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
109
110         id = pci_read_config32(dev, 0);
111
112         /* AMD 8131 Errata 48 */
113         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
114                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
115                 return freq_cap;
116         } 
117
118         /* AMD 8151 Errata 23 */
119         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
120                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
121                 return freq_cap;
122         } 
123         
124         /* AMD K8 Unsupported 1Ghz? */
125         if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
126                         freq_cap &= ~(1 << HT_FREQ_1000Mhz);
127         }
128
129         return freq_cap;
130 }
131
132 #define LINK_OFFS(WIDTH,FREQ,FREQ_CAP)                                  \
133         (((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
134
135 #define LINK_WIDTH(OFFS)    ((OFFS >> 16) & 0xFF)
136 #define LINK_FREQ(OFFS)     ((OFFS >> 8) & 0xFF)
137 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
138
139 #define PCI_HT_HOST_OFFS LINK_OFFS(             \
140                 PCI_HT_CAP_HOST_WIDTH,          \
141                 PCI_HT_CAP_HOST_FREQ,           \
142                 PCI_HT_CAP_HOST_FREQ_CAP)
143
144 #define PCI_HT_SLAVE0_OFFS LINK_OFFS(           \
145                 PCI_HT_CAP_SLAVE_WIDTH0,        \
146                 PCI_HT_CAP_SLAVE_FREQ0,         \
147                 PCI_HT_CAP_SLAVE_FREQ_CAP0)
148
149 #define PCI_HT_SLAVE1_OFFS LINK_OFFS(           \
150                 PCI_HT_CAP_SLAVE_WIDTH1,        \
151                 PCI_HT_CAP_SLAVE_FREQ1,         \
152                 PCI_HT_CAP_SLAVE_FREQ_CAP1)
153
154 static int ht_optimize_link(
155         device_t dev1, uint8_t pos1, unsigned offs1,
156         device_t dev2, uint8_t pos2, unsigned offs2)
157 {
158         static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
159         static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
160         uint16_t freq_cap1, freq_cap2, freq_cap, freq_mask;
161         uint8_t width_cap1, width_cap2, width_cap, width, old_width, ln_width1, ln_width2;
162         uint8_t freq, old_freq;
163         int needs_reset;
164         /* Set link width and frequency */
165
166         /* Initially assume everything is already optimized and I don't need a reset */
167         needs_reset = 0;
168
169         /* Get the frequency capabilities */
170         freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
171         freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
172
173         /* Calculate the highest possible frequency */
174         freq = log2(freq_cap1 & freq_cap2);
175
176         /* See if I am changing the link freqency */
177         old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
178         needs_reset |= old_freq != freq;
179         old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
180         needs_reset |= old_freq != freq;
181
182         /* Set the Calulcated link frequency */
183         pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
184         pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
185
186         /* Get the width capabilities */
187         width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
188         width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
189
190         /* Calculate dev1's input width */
191         ln_width1 = link_width_to_pow2[width_cap1 & 7];
192         ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
193         if (ln_width1 > ln_width2) {
194                 ln_width1 = ln_width2;
195         }
196         width = pow2_to_link_width[ln_width1];
197         /* Calculate dev1's output width */
198         ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
199         ln_width2 = link_width_to_pow2[width_cap2 & 7];
200         if (ln_width1 > ln_width2) {
201                 ln_width1 = ln_width2;
202         }
203         width |= pow2_to_link_width[ln_width1] << 4;
204
205         /* See if I am changing dev1's width */
206         old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
207         needs_reset |= old_width != width;
208
209         /* Set dev1's widths */
210         pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
211
212         /* Calculate dev2's width */
213         width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
214
215         /* See if I am changing dev2's width */
216         old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
217         needs_reset |= old_width != width;
218
219         /* Set dev2's widths */
220         pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
221
222         return needs_reset;
223 }
224 static int ht_setup_chain(device_t udev, uint8_t upos)
225 {
226         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
227          * On most boards this just happens.  If a cpu has multiple
228          * non Coherent links the appropriate bus registers for the
229          * links needs to be programed to point at bus 0.
230          */
231         uint8_t next_unitid, last_unitid;
232         int reset_needed;
233         unsigned uoffs;
234
235         /* Make certain the HT bus is not enumerated */
236         ht_collapse_previous_enumeration(0);
237
238         reset_needed = 0;
239         uoffs = PCI_HT_HOST_OFFS;
240         next_unitid = 1;
241         do {
242                 uint32_t id;
243                 uint8_t pos;
244                 uint16_t flags;
245                 uint8_t count;
246                 unsigned offs;
247
248                 device_t dev = PCI_DEV(0, 0, 0);
249                 last_unitid = next_unitid;
250
251                 id = pci_read_config32(dev, PCI_VENDOR_ID);
252                 /* If the chain is enumerated quit */
253                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
254                     (((id >> 16) & 0xffff) == 0xffff) ||
255                     (((id >> 16) & 0xffff) == 0x0000)) {
256                         break;
257                 }
258
259                 pos = ht_lookup_slave_capability(dev);
260                 if (!pos) {
261                         print_err("HT link capability not found\r\n");
262                         break;
263                 }
264 #if CK804_DEVN_BASE==0 
265                 //CK804 workaround: 
266                 // CK804 UnitID changes not use
267                 id = pci_read_config32(dev, PCI_VENDOR_ID);
268                 if(id != 0x005e10de) {
269 #endif
270
271                 /* Update the Unitid of the current device */
272                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
273                 flags &= ~0x1f; /* mask out the bse Unit ID */
274                 flags |= next_unitid & 0x1f;
275                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
276
277                 dev = PCI_DEV(0, next_unitid, 0);
278 #if CK804_DEVN_BASE==0  
279                 }
280                 else {
281                         dev = PCI_DEV(0, 0, 0);
282                 }
283 #endif
284
285                 /* Compute the number of unitids consumed */
286                 count = (flags >> 5) & 0x1f;
287                 next_unitid += count;
288
289                 /* get ht direction */
290                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); // double read ??
291
292                 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
293
294                 /* Setup the Hypertransport link */
295                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
296
297 #if CK804_DEVN_BASE==0
298                 if(id == 0x005e10de) {
299                         break;
300                 }
301 #endif
302
303                 /* Remeber the location of the last device */
304                 udev = dev;
305                 upos = pos;
306                 uoffs = (offs != PCI_HT_SLAVE0_OFFS) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
307
308         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
309         return reset_needed;
310 }
311
312 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus)
313 {
314         uint8_t next_unitid, last_unitid;
315         unsigned uoffs;
316         int reset_needed=0;
317
318         uoffs = PCI_HT_HOST_OFFS;
319         next_unitid = 1;
320
321         do {
322                 uint32_t id;
323                 uint8_t pos;
324                 uint16_t flags;
325                 uint8_t count;
326                 unsigned offs;
327                 
328                 device_t dev = PCI_DEV(bus, 0, 0);
329                 last_unitid = next_unitid;
330
331                 id = pci_read_config32(dev, PCI_VENDOR_ID);
332
333                 /* If the chain is enumerated quit */
334                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
335                     (((id >> 16) & 0xffff) == 0xffff) ||
336                     (((id >> 16) & 0xffff) == 0x0000)) {
337                         break;
338                 }
339
340                 pos = ht_lookup_slave_capability(dev);
341                 if (!pos) {
342                         print_err(" HT link capability not found\r\n");
343                         break;
344                 }
345
346 #if CK804_DEVN_BASE==0 
347                 //CK804 workaround: 
348                 // CK804 UnitID changes not use
349                 id = pci_read_config32(dev, PCI_VENDOR_ID);
350                 if(id != 0x005e10de) {
351 #endif
352
353                 /* Update the Unitid of the current device */
354                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
355                 flags &= ~0x1f; /* mask out the bse Unit ID */
356                 flags |= next_unitid & 0x1f;
357                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
358
359                 dev = PCI_DEV(bus, next_unitid, 0);
360 #if CK804_DEVN_BASE==0  
361                 } 
362                 else {
363                         dev = PCI_DEV(bus, 0, 0);
364                 }
365 #endif
366
367                 /* Compute the number of unitids consumed */
368                 count = (flags >> 5) & 0x1f;
369                 next_unitid += count;
370
371                 /* get ht direction */
372                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); // double read ??
373
374                 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
375                 
376                 /* Setup the Hypertransport link */
377                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
378
379 #if CK804_DEVN_BASE==0
380                 if(id == 0x005e10de) {
381                         break;
382                 }
383 #endif
384
385                 /* Remeber the location of the last device */
386                 udev = dev;
387                 upos = pos;
388                 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
389
390         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
391         return reset_needed;
392 }
393
394 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
395 {
396         uint32_t dword, dword_old;
397         uint8_t link_type;
398         
399         /* This works on an Athlon64 because unimplemented links return 0 */
400         dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
401         link_type = dword & 0xff;
402         
403         dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
404         
405         if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
406                 dword &= ~( 0xff<<(linkn *8) );
407                 dword |= val << (linkn *8);
408         }
409         
410         if (dword != dword_old) {
411                 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
412                 return 1;
413         }
414         
415         return 0;
416 }
417
418 static int optimize_link_in_coherent(uint8_t ht_c_num)
419 {
420         int reset_needed; 
421         uint8_t i;
422
423         reset_needed = 0;
424
425         for (i = 0; i < ht_c_num; i++) {
426                 uint32_t reg;
427                 uint8_t nodeid, linkn;
428                 uint8_t busn;
429                 uint8_t val;
430
431                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
432                 
433                 nodeid = ((reg & 0xf0)>>4); // nodeid
434                 linkn = ((reg & 0xf00)>>8); // link n
435                 busn = (reg & 0xff0000)>>16; //busn
436         
437                 reg = pci_read_config32( PCI_DEV(busn, 1, 0), PCI_VENDOR_ID);
438                 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
439                         val = 0x25;
440                 } else if ( (reg & 0xffff) == 0x10de ) {
441                         val = 0x25;//???
442                 } else {
443                         continue;
444                 }
445
446                 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
447
448         }
449
450         return reset_needed;
451 }
452
453 static int ht_setup_chains(uint8_t ht_c_num)
454 {
455         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it. 
456          * On most boards this just happens.  If a cpu has multiple
457          * non Coherent links the appropriate bus registers for the
458          * links needs to be programed to point at bus 0.
459          */
460         int reset_needed; 
461         uint8_t upos;
462         device_t udev;
463         uint8_t i;
464
465         reset_needed = 0;
466
467         for (i = 0; i < ht_c_num; i++) {
468                 uint32_t reg;
469                 uint8_t devpos;
470                 unsigned regpos;
471                 uint32_t dword;
472                 uint8_t busn;
473                 
474                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
475
476                 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
477                 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
478                 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
479                 busn = (reg & 0xff0000)>>16;
480                 
481                 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
482                 dword &= ~(0xffff<<8);
483                 dword |= (reg & 0xffff0000)>>8;
484                 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
485                 
486                 /* Make certain the HT bus is not enumerated */
487                 ht_collapse_previous_enumeration(busn);
488
489                 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
490                 udev =  PCI_DEV(0, devpos, 0);
491                 
492                 reset_needed |= ht_setup_chainx(udev,upos,busn);
493
494
495         }
496
497         reset_needed |= optimize_link_in_coherent(ht_c_num);            
498         
499         return reset_needed;
500 }
501
502 static int ht_setup_chains_x(void)
503 {               
504         uint8_t nodeid;
505         uint32_t reg; 
506         uint32_t tempreg;
507         uint8_t next_busn;
508         uint8_t ht_c_num;
509         uint8_t nodes;
510       
511         /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
512         reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
513         /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=5+1 */
514         print_linkn_in("SBLink=", ((reg>>8) & 3) );
515         tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (5<<24);
516         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
517
518         next_busn=5+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
519         /* clean others */
520         for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
521                 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
522         }
523  
524         nodes = ((pci_read_config32(PCI_DEV(0, 0x18, 0), 0x60)>>4) & 7) + 1;
525
526         for(nodeid=0; nodeid<nodes; nodeid++) {
527                 device_t dev; 
528                 uint8_t linkn;
529                 dev = PCI_DEV(0, 0x18+nodeid,0);
530                 for(linkn = 0; linkn<3; linkn++) {
531                         unsigned regpos;
532                         regpos = 0x98 + 0x20 * linkn;
533                         reg = pci_read_config32(dev, regpos);
534                         if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
535                         print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
536                         tempreg = 3 | (nodeid <<4) | (linkn<<8);
537                         /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
538                         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
539                                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
540                                 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) {  /*we got it*/
541                                         break;
542                                 }
543                         }
544                         if(ht_c_num == 4) break; /*used up onle 4 non conherent allowed*/
545                         /*update to 0xe0...*/
546                         if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
547                         print_linkn_in("\tbusn=", next_busn);
548                         tempreg |= (next_busn<<16)|((next_busn+5)<<24);
549                         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
550                         next_busn+=5+1;
551                 }
552         }
553         /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
554
555         for(nodeid = 1; nodeid<nodes; nodeid++) {
556                 int i;
557                 device_t dev;
558                 dev = PCI_DEV(0, 0x18+nodeid,1);
559                 for(i = 0; i< 4; i++) {
560                         unsigned regpos;
561                         regpos = 0xe0 + i * 4;
562                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
563                         pci_write_config32(dev, regpos, reg);
564
565                 }
566         }
567         
568         /* recount ht_c_num*/
569         uint8_t i=0;
570         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
571                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
572                 if(((reg & 0xf) != 0x0)) {
573                         i++;
574                 }
575         }
576
577         return ht_setup_chains(i);
578
579 }