ec767456e88f8a0f742e31e5fdbc597b085c4a45
[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         #if K8_HT_FREQ_1G_SUPPORT == 1  
127                 if (is_cpu_pre_e0())  // CK804 support 1G?
128         #endif  
129                         freq_cap &= ~(1 << HT_FREQ_1000Mhz);
130         }
131
132         return freq_cap;
133 }
134
135 #define LINK_OFFS(WIDTH,FREQ,FREQ_CAP)                                  \
136         (((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
137
138 #define LINK_WIDTH(OFFS)    ((OFFS >> 16) & 0xFF)
139 #define LINK_FREQ(OFFS)     ((OFFS >> 8) & 0xFF)
140 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
141
142 #define PCI_HT_HOST_OFFS LINK_OFFS(             \
143                 PCI_HT_CAP_HOST_WIDTH,          \
144                 PCI_HT_CAP_HOST_FREQ,           \
145                 PCI_HT_CAP_HOST_FREQ_CAP)
146
147 #define PCI_HT_SLAVE0_OFFS LINK_OFFS(           \
148                 PCI_HT_CAP_SLAVE_WIDTH0,        \
149                 PCI_HT_CAP_SLAVE_FREQ0,         \
150                 PCI_HT_CAP_SLAVE_FREQ_CAP0)
151
152 #define PCI_HT_SLAVE1_OFFS LINK_OFFS(           \
153                 PCI_HT_CAP_SLAVE_WIDTH1,        \
154                 PCI_HT_CAP_SLAVE_FREQ1,         \
155                 PCI_HT_CAP_SLAVE_FREQ_CAP1)
156
157 static int ht_optimize_link(
158         device_t dev1, uint8_t pos1, unsigned offs1,
159         device_t dev2, uint8_t pos2, unsigned offs2)
160 {
161         static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
162         static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
163         uint16_t freq_cap1, freq_cap2, freq_cap, freq_mask;
164         uint8_t width_cap1, width_cap2, width_cap, width, old_width, ln_width1, ln_width2;
165         uint8_t freq, old_freq;
166         int needs_reset;
167         /* Set link width and frequency */
168
169         /* Initially assume everything is already optimized and I don't need a reset */
170         needs_reset = 0;
171
172         /* Get the frequency capabilities */
173         freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
174         freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
175
176         /* Calculate the highest possible frequency */
177         freq = log2(freq_cap1 & freq_cap2);
178
179         /* See if I am changing the link freqency */
180         old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
181         needs_reset |= old_freq != freq;
182         old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
183         needs_reset |= old_freq != freq;
184
185         /* Set the Calulcated link frequency */
186         pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
187         pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
188
189         /* Get the width capabilities */
190         width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
191         width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
192
193         /* Calculate dev1's input width */
194         ln_width1 = link_width_to_pow2[width_cap1 & 7];
195         ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
196         if (ln_width1 > ln_width2) {
197                 ln_width1 = ln_width2;
198         }
199         width = pow2_to_link_width[ln_width1];
200         /* Calculate dev1's output width */
201         ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
202         ln_width2 = link_width_to_pow2[width_cap2 & 7];
203         if (ln_width1 > ln_width2) {
204                 ln_width1 = ln_width2;
205         }
206         width |= pow2_to_link_width[ln_width1] << 4;
207
208         /* See if I am changing dev1's width */
209         old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
210         needs_reset |= old_width != width;
211
212         /* Set dev1's widths */
213         pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
214
215         /* Calculate dev2's width */
216         width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
217
218         /* See if I am changing dev2's width */
219         old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
220         needs_reset |= old_width != width;
221
222         /* Set dev2's widths */
223         pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
224
225         return needs_reset;
226 }
227 static int ht_setup_chain(device_t udev, uint8_t upos)
228 {
229         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
230          * On most boards this just happens.  If a cpu has multiple
231          * non Coherent links the appropriate bus registers for the
232          * links needs to be programed to point at bus 0.
233          */
234         uint8_t next_unitid, last_unitid;
235         int reset_needed;
236         unsigned uoffs;
237
238         /* Make certain the HT bus is not enumerated */
239         ht_collapse_previous_enumeration(0);
240
241         reset_needed = 0;
242         uoffs = PCI_HT_HOST_OFFS;
243         next_unitid = 1;
244         do {
245                 uint32_t id;
246                 uint8_t pos;
247                 uint16_t flags;
248                 uint8_t count;
249                 unsigned offs;
250
251                 device_t dev = PCI_DEV(0, 0, 0);
252                 last_unitid = next_unitid;
253
254                 id = pci_read_config32(dev, PCI_VENDOR_ID);
255                 /* If the chain is enumerated quit */
256                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
257                     (((id >> 16) & 0xffff) == 0xffff) ||
258                     (((id >> 16) & 0xffff) == 0x0000)) {
259                         break;
260                 }
261
262                 pos = ht_lookup_slave_capability(dev);
263                 if (!pos) {
264                         print_err("HT link capability not found\r\n");
265                         break;
266                 }
267 #if CK804_DEVN_BASE==0 
268                 //CK804 workaround: 
269                 // CK804 UnitID changes not use
270                 id = pci_read_config32(dev, PCI_VENDOR_ID);
271                 if(id != 0x005e10de) {
272 #endif
273
274                 /* Update the Unitid of the current device */
275                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
276                 flags &= ~0x1f; /* mask out the bse Unit ID */
277                 flags |= next_unitid & 0x1f;
278                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
279
280                 dev = PCI_DEV(0, next_unitid, 0);
281 #if CK804_DEVN_BASE==0  
282                 }
283                 else {
284                         dev = PCI_DEV(0, 0, 0);
285                 }
286 #endif
287
288                 /* Compute the number of unitids consumed */
289                 count = (flags >> 5) & 0x1f;
290                 next_unitid += count;
291
292                 /* get ht direction */
293                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); // double read ??
294
295                 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
296
297                 /* Setup the Hypertransport link */
298                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
299
300 #if CK804_DEVN_BASE==0
301                 if(id == 0x005e10de) {
302                         break;
303                 }
304 #endif
305
306                 /* Remeber the location of the last device */
307                 udev = dev;
308                 upos = pos;
309                 uoffs = (offs != PCI_HT_SLAVE0_OFFS) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
310
311         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
312         return reset_needed;
313 }
314
315 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus)
316 {
317         uint8_t next_unitid, last_unitid;
318         unsigned uoffs;
319         int reset_needed=0;
320
321         uoffs = PCI_HT_HOST_OFFS;
322         next_unitid = 1;
323
324         do {
325                 uint32_t id;
326                 uint8_t pos;
327                 uint16_t flags;
328                 uint8_t count;
329                 unsigned offs;
330                 
331                 device_t dev = PCI_DEV(bus, 0, 0);
332                 last_unitid = next_unitid;
333
334                 id = pci_read_config32(dev, PCI_VENDOR_ID);
335
336                 /* If the chain is enumerated quit */
337                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
338                     (((id >> 16) & 0xffff) == 0xffff) ||
339                     (((id >> 16) & 0xffff) == 0x0000)) {
340                         break;
341                 }
342
343                 pos = ht_lookup_slave_capability(dev);
344                 if (!pos) {
345                         print_err(" HT link capability not found\r\n");
346                         break;
347                 }
348
349 #if CK804_DEVN_BASE==0 
350                 //CK804 workaround: 
351                 // CK804 UnitID changes not use
352                 id = pci_read_config32(dev, PCI_VENDOR_ID);
353                 if(id != 0x005e10de) {
354 #endif
355
356                 /* Update the Unitid of the current device */
357                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
358                 flags &= ~0x1f; /* mask out the bse Unit ID */
359                 flags |= next_unitid & 0x1f;
360                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
361
362                 dev = PCI_DEV(bus, next_unitid, 0);
363 #if CK804_DEVN_BASE==0  
364                 } 
365                 else {
366                         dev = PCI_DEV(bus, 0, 0);
367                 }
368 #endif
369
370                 /* Compute the number of unitids consumed */
371                 count = (flags >> 5) & 0x1f;
372                 next_unitid += count;
373
374                 /* get ht direction */
375                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); // double read ??
376
377                 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
378                 
379                 /* Setup the Hypertransport link */
380                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
381
382 #if CK804_DEVN_BASE==0
383                 if(id == 0x005e10de) {
384                         break;
385                 }
386 #endif
387
388                 /* Remeber the location of the last device */
389                 udev = dev;
390                 upos = pos;
391                 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
392
393         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
394         return reset_needed;
395 }
396
397 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
398 {
399         uint32_t dword, dword_old;
400         uint8_t link_type;
401         
402         /* This works on an Athlon64 because unimplemented links return 0 */
403         dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
404         link_type = dword & 0xff;
405         
406         dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
407         
408         if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
409                 dword &= ~( 0xff<<(linkn *8) );
410                 dword |= val << (linkn *8);
411         }
412         
413         if (dword != dword_old) {
414                 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
415                 return 1;
416         }
417         
418         return 0;
419 }
420
421 static int optimize_link_in_coherent(uint8_t ht_c_num)
422 {
423         int reset_needed; 
424         uint8_t i;
425
426         reset_needed = 0;
427
428         for (i = 0; i < ht_c_num; i++) {
429                 uint32_t reg;
430                 uint8_t nodeid, linkn;
431                 uint8_t busn;
432                 uint8_t val;
433
434                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
435                 
436                 nodeid = ((reg & 0xf0)>>4); // nodeid
437                 linkn = ((reg & 0xf00)>>8); // link n
438                 busn = (reg & 0xff0000)>>16; //busn
439         
440                 reg = pci_read_config32( PCI_DEV(busn, 1, 0), PCI_VENDOR_ID);
441                 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
442                         val = 0x25;
443                 } else if ( (reg & 0xffff) == 0x10de ) {
444                         val = 0x25;//???
445                 } else {
446                         continue;
447                 }
448
449                 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
450
451         }
452
453         return reset_needed;
454 }
455
456 static int ht_setup_chains(uint8_t ht_c_num)
457 {
458         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it. 
459          * On most boards this just happens.  If a cpu has multiple
460          * non Coherent links the appropriate bus registers for the
461          * links needs to be programed to point at bus 0.
462          */
463         int reset_needed; 
464         uint8_t upos;
465         device_t udev;
466         uint8_t i;
467
468         reset_needed = 0;
469
470         for (i = 0; i < ht_c_num; i++) {
471                 uint32_t reg;
472                 uint8_t devpos;
473                 unsigned regpos;
474                 uint32_t dword;
475                 uint8_t busn;
476                 
477                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
478
479                 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
480                 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
481                 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
482                 busn = (reg & 0xff0000)>>16;
483                 
484                 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
485                 dword &= ~(0xffff<<8);
486                 dword |= (reg & 0xffff0000)>>8;
487                 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
488                 
489                 /* Make certain the HT bus is not enumerated */
490                 ht_collapse_previous_enumeration(busn);
491
492                 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
493                 udev =  PCI_DEV(0, devpos, 0);
494                 
495                 reset_needed |= ht_setup_chainx(udev,upos,busn);
496
497
498         }
499
500         reset_needed |= optimize_link_in_coherent(ht_c_num);            
501         
502         return reset_needed;
503 }
504
505 static int ht_setup_chains_x(void)
506 {               
507         uint8_t nodeid;
508         uint32_t reg; 
509         uint32_t tempreg;
510         uint8_t next_busn;
511         uint8_t ht_c_num;
512         uint8_t nodes;
513       
514         /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
515         reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
516         /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=5+1 */
517         print_linkn_in("SBLink=", ((reg>>8) & 3) );
518         tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (5<<24);
519         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
520
521         next_busn=5+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
522         /* clean others */
523         for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
524                 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
525         }
526  
527         nodes = ((pci_read_config32(PCI_DEV(0, 0x18, 0), 0x60)>>4) & 7) + 1;
528
529         for(nodeid=0; nodeid<nodes; nodeid++) {
530                 device_t dev; 
531                 uint8_t linkn;
532                 dev = PCI_DEV(0, 0x18+nodeid,0);
533                 for(linkn = 0; linkn<3; linkn++) {
534                         unsigned regpos;
535                         regpos = 0x98 + 0x20 * linkn;
536                         reg = pci_read_config32(dev, regpos);
537                         if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
538                         print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
539                         tempreg = 3 | (nodeid <<4) | (linkn<<8);
540                         /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
541                         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
542                                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
543                                 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) {  /*we got it*/
544                                         break;
545                                 }
546                         }
547                         if(ht_c_num == 4) break; /*used up onle 4 non conherent allowed*/
548                         /*update to 0xe0...*/
549                         if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
550                         print_linkn_in("\tbusn=", next_busn);
551                         tempreg |= (next_busn<<16)|((next_busn+5)<<24);
552                         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
553                         next_busn+=5+1;
554                 }
555         }
556         /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
557
558         for(nodeid = 1; nodeid<nodes; nodeid++) {
559                 int i;
560                 device_t dev;
561                 dev = PCI_DEV(0, 0x18+nodeid,1);
562                 for(i = 0; i< 4; i++) {
563                         unsigned regpos;
564                         regpos = 0xe0 + i * 4;
565                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
566                         pci_write_config32(dev, regpos, reg);
567
568                 }
569         }
570         
571         /* recount ht_c_num*/
572         uint8_t i=0;
573         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
574                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
575                 if(((reg & 0xf) != 0x0)) {
576                         i++;
577                 }
578         }
579
580         return ht_setup_chains(i);
581
582 }