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