*** empty log message ***
[coreboot.git] / src / northbridge / amd / amdk8 / incoherent_ht.c
1 #include <device/pci_def.h>
2 #include <device/pci_ids.h>
3 #include <device/hypertransport_def.h>
4
5 static unsigned ht_lookup_slave_capability(device_t dev)
6 {
7         unsigned pos;
8         uint8_t hdr_type;
9
10         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
11         pos = 0;
12         hdr_type &= 0x7f;
13
14         if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
15             (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
16                 pos = PCI_CAPABILITY_LIST;
17         }
18         if (pos > PCI_CAP_LIST_NEXT) {
19                 pos = pci_read_config8(dev, pos);
20         }
21         while(pos != 0) { /* loop through the linked list */
22                 uint8_t cap;
23                 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
24                 if (cap == PCI_CAP_ID_HT) {
25                         uint16_t flags;
26
27                         flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
28                         if ((flags >> 13) == 0) {
29                                 /* Entry is a Slave secondary, success... */
30                                 break;
31                         }
32                 }
33                 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
34         }
35         return pos;
36 }
37
38 static void ht_collapse_previous_enumeration(unsigned bus)
39 {
40         device_t dev;
41         
42         /* Spin through the devices and collapse any previous
43          * hypertransport enumeration.
44          */
45         for(dev = PCI_DEV(bus, 0, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
46                 uint32_t id;
47                 unsigned pos, flags;
48                 
49                 id = pci_read_config32(dev, PCI_VENDOR_ID);
50                 if ((id == 0xffffffff) || (id == 0x00000000) ||
51                     (id == 0x0000ffff) || (id == 0xffff0000)) {
52                         continue;
53                 }
54                 
55                 pos = ht_lookup_slave_capability(dev);
56                 if (!pos) {
57                         continue;
58                 }
59
60                 /* Clear the unitid */
61                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
62                 flags &= ~0x1f;
63                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
64         }
65 }
66
67 static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
68 {
69         /* Handle bugs in valid hypertransport frequency reporting */
70         unsigned freq_cap;
71         uint32_t id;
72
73         freq_cap = pci_read_config16(dev, pos);
74         freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
75
76         id = pci_read_config32(dev, 0);
77
78         /* AMD 8131 Errata 48 */
79         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
80                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
81         }
82         /* AMD 8151 Errata 23 */
83         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
84                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
85         }
86         /* AMD K8 Unsupported 1Ghz? */
87         if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
88                 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
89         }
90         return freq_cap;
91 }
92
93 #define LINK_OFFS(WIDTH,FREQ,FREQ_CAP)                                  \
94         (((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
95
96 #define LINK_WIDTH(OFFS)    ((OFFS >> 16) & 0xFF)
97 #define LINK_FREQ(OFFS)     ((OFFS >> 8) & 0xFF)
98 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
99
100 #define PCI_HT_HOST_OFFS LINK_OFFS(             \
101                 PCI_HT_CAP_HOST_WIDTH,          \
102                 PCI_HT_CAP_HOST_FREQ,           \
103                 PCI_HT_CAP_HOST_FREQ_CAP)
104
105 #define PCI_HT_SLAVE0_OFFS LINK_OFFS(           \
106                 PCI_HT_CAP_SLAVE_WIDTH0,        \
107                 PCI_HT_CAP_SLAVE_FREQ0,         \
108                 PCI_HT_CAP_SLAVE_FREQ_CAP0)
109
110 #define PCI_HT_SLAVE1_OFFS LINK_OFFS(           \
111                 PCI_HT_CAP_SLAVE_WIDTH1,        \
112                 PCI_HT_CAP_SLAVE_FREQ1,         \
113                 PCI_HT_CAP_SLAVE_FREQ_CAP1)
114
115 static int ht_optimize_link(
116         device_t dev1, uint8_t pos1, unsigned offs1,
117         device_t dev2, uint8_t pos2, unsigned offs2)
118 {
119         static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
120         static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
121         uint16_t freq_cap1, freq_cap2, freq_cap, freq_mask;
122         uint8_t width_cap1, width_cap2, width_cap, width, old_width, ln_width1, ln_width2;
123         uint8_t freq, old_freq;
124         int needs_reset;
125         /* Set link width and frequency */
126
127         /* Initially assume everything is already optimized and I don't need a reset */
128         needs_reset = 0;
129
130         /* Get the frequency capabilities */
131         freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
132         freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
133
134         /* Calculate the highest possible frequency */
135         freq = log2(freq_cap1 & freq_cap2);
136
137         /* See if I am changing the link freqency */
138         old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
139         needs_reset |= old_freq != freq;
140         old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
141         needs_reset |= old_freq != freq;
142
143         /* Set the Calulcated link frequency */
144         pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
145         pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
146
147         /* Get the width capabilities */
148         width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
149         width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
150
151         /* Calculate dev1's input width */
152         ln_width1 = link_width_to_pow2[width_cap1 & 7];
153         ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
154         if (ln_width1 > ln_width2) {
155                 ln_width1 = ln_width2;
156         }
157         width = pow2_to_link_width[ln_width1];
158         /* Calculate dev1's output width */
159         ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
160         ln_width2 = link_width_to_pow2[width_cap2 & 7];
161         if (ln_width1 > ln_width2) {
162                 ln_width1 = ln_width2;
163         }
164         width |= pow2_to_link_width[ln_width1] << 4;
165
166         /* See if I am changing dev1's width */
167         old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
168         needs_reset |= old_width != width;
169
170         /* Set dev1's widths */
171         pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
172
173         /* Calculate dev2's width */
174         width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
175
176         /* See if I am changing dev2's width */
177         old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
178         needs_reset |= old_width != width;
179
180         /* Set dev2's widths */
181         pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
182
183         return needs_reset;
184 }
185
186 static int ht_setup_chain(device_t udev, unsigned upos)
187 {
188         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
189          * On most boards this just happens.  If a cpu has multiple
190          * non Coherent links the appropriate bus registers for the
191          * links needs to be programed to point at bus 0.
192          */
193         unsigned next_unitid, last_unitid;
194         int reset_needed;
195         unsigned uoffs;
196
197 #warning "FIXME handle multiple chains!"
198
199         /* Make certain the HT bus is not enumerated */
200         ht_collapse_previous_enumeration(0);
201
202         reset_needed = 0;
203         uoffs = PCI_HT_HOST_OFFS;
204         next_unitid = 1;
205         do {
206                 uint32_t id;
207                 uint8_t pos;
208                 unsigned flags, count;
209                 device_t dev = PCI_DEV(0, 0, 0);
210                 last_unitid = next_unitid;
211
212                 id = pci_read_config32(dev, PCI_VENDOR_ID);
213                 /* If the chain is enumerated quit */
214                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
215                     (((id >> 16) & 0xffff) == 0xffff) ||
216                     (((id >> 16) & 0xffff) == 0x0000)) {
217                         break;
218                 }
219
220                 pos = ht_lookup_slave_capability(dev);
221                 if (!pos) {
222                         print_err("HT link capability not found\r\n");
223                         break;
224                 }
225
226                 /* Setup the Hypertransport link */
227                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, PCI_HT_SLAVE0_OFFS);
228
229                 /* Update the Unitid of the current device */
230                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
231                 flags &= ~0x1f; /* mask out the bse Unit ID */
232                 flags |= next_unitid & 0x1f;
233                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
234
235                 /* Remeber the location of the last device */
236                 udev = PCI_DEV(0, next_unitid, 0);
237                 upos = pos;
238                 uoffs = PCI_HT_SLAVE1_OFFS;
239
240                 /* Compute the number of unitids consumed */
241                 count = (flags >> 5) & 0x1f;
242                 next_unitid += count;
243
244         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
245         return reset_needed;
246 }
247
248 struct ht_chain {
249         unsigned devreg; 
250 };
251 static int ht_setup_chainx(device_t udev, unsigned upos, unsigned bus)
252 {
253         unsigned next_unitid, last_unitid;
254         unsigned uoffs;
255         int reset_needed=0;
256
257         uoffs = PCI_HT_HOST_OFFS;
258         next_unitid = 1;
259
260         do {
261                 uint32_t id;
262                 uint8_t pos;
263                 unsigned flags, count;
264                 
265                 device_t dev = PCI_DEV(bus, 0, 0);
266                 last_unitid = next_unitid;
267
268                 id = pci_read_config32(dev, PCI_VENDOR_ID);
269                 /* If the chain is enumerated quit */
270                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
271                     (((id >> 16) & 0xffff) == 0xffff) ||
272                     (((id >> 16) & 0xffff) == 0x0000)) {
273                         break;
274                 }
275 #if 0
276                 print_debug("bus=");
277                 print_debug_hex8(bus);
278                 print_debug(" id =");
279                 print_debug_hex32(id);
280                 print_debug("\r\n");
281 #endif
282
283                 
284                 pos = ht_lookup_slave_capability(dev);
285                 if (!pos) {
286                         print_err("HT link capability not found\r\n");
287                         break;
288                 }
289
290                 /* Setup the Hypertransport link */
291                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, PCI_HT_SLAVE0_OFFS);
292
293                 /* Update the Unitid of the current device */
294                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
295                 flags &= ~0x1f; /* mask out the bse Unit ID */
296                 flags |= next_unitid & 0x1f;
297                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
298
299                 /* Remeber the location of the last device */
300                 udev = PCI_DEV(0, next_unitid, 0);
301                 upos = pos;
302                 uoffs = PCI_HT_SLAVE1_OFFS;
303
304                 /* Compute the number of unitids consumed */
305                 count = (flags >> 5) & 0x1f;
306                 next_unitid += count;
307
308         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
309         return reset_needed;
310 }
311
312 static int ht_setup_chains(const struct ht_chain *ht_c, int ht_c_num)
313 {
314         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it. 
315          * On most boards this just happens.  If a cpu has multiple
316          * non Coherent links the appropriate bus registers for the
317          * links needs to be programed to point at bus 0.
318          */
319         int reset_needed; 
320         unsigned upos;
321         device_t udev;
322         int i;
323
324         reset_needed = 0;
325
326         for (i = 0; i < ht_c_num; i++) {
327                 uint32_t reg;
328                 unsigned devpos;
329                 unsigned regpos;
330                 uint32_t dword;
331                 unsigned busn;
332                 
333                 reg = pci_read_config32(PCI_DEV(0,0x18,1), ht_c[i].devreg);
334
335                 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
336                 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
337                 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
338                 busn = (reg & 0xff0000)>>16;
339                 
340                 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
341                 dword &= ~(0xffff<<8);
342                 dword |= (reg & 0xffff0000)>>8;
343                 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
344 #if 0
345                 print_debug("udev=(0,0x");
346                 print_debug_hex8(devpos);
347                 print_debug(",0) 0x");
348                 print_debug_hex8(regpos);
349                 print_debug("=");
350                 print_debug_hex32(dword);
351 #endif
352                 
353                 /* Make certain the HT bus is not enumerated */
354                 ht_collapse_previous_enumeration(busn);
355
356                 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
357                 udev =  PCI_DEV(0, devpos, 0);
358 #if 0
359                 print_debug("\tupos=0x");
360                 print_debug_hex32(upos);
361                 print_debug("\r\n");
362 #endif          
363                 
364                 reset_needed |= ht_setup_chainx(udev,upos,busn );
365
366         }
367
368         return reset_needed;
369 }