blob: 7bc1c215b86cfc0a5aebe6c86f721f0b156f07df [file] [log] [blame]
Eric Biederman0ac6b412003-09-02 17:16:48 +00001#include <bitops.h>
2#include <console/console.h>
3#include <device/device.h>
4#include <device/path.h>
5#include <device/pci.h>
Eric Biederman5cd81732004-03-11 15:01:31 +00006#include <device/pci_ids.h>
Eric Biedermanff0e8462003-09-04 03:00:54 +00007#include <device/hypertransport.h>
Eric Biederman0ac6b412003-09-02 17:16:48 +00008#include <part/hard_reset.h>
9#include <part/fallback_boot.h>
10
Yinghai Lu90b3e092005-01-26 22:00:20 +000011#define OPT_HT_LINK 0
12
Eric Biederman0ac6b412003-09-02 17:16:48 +000013static device_t ht_scan_get_devs(device_t *old_devices)
14{
15 device_t first, last;
16 first = *old_devices;
17 last = first;
Eric Biederman03acab62004-10-14 21:25:53 +000018 while(last && last->sibling &&
19 (last->sibling->path.type == DEVICE_PATH_PCI) &&
20 (last->sibling->path.u.pci.devfn > last->path.u.pci.devfn)) {
Eric Biederman0ac6b412003-09-02 17:16:48 +000021 last = last->sibling;
22 }
23 if (first) {
24 *old_devices = last->sibling;
25 last->sibling = 0;
26 }
27 return first;
28}
Yinghai Lu90b3e092005-01-26 22:00:20 +000029#if OPT_HT_LINK == 1
Eric Biederman5cd81732004-03-11 15:01:31 +000030static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
31{
32 /* Handle bugs in valid hypertransport frequency reporting */
33 unsigned freq_cap;
34
35 freq_cap = pci_read_config16(dev, pos);
36 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
37
38 /* AMD 8131 Errata 48 */
39 if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
40 (dev->device == PCI_DEVICE_ID_AMD_8131_PCIX)) {
41 freq_cap &= ~(1 << HT_FREQ_800Mhz);
Yinghai Lu90b3e092005-01-26 22:00:20 +000042 } else
Eric Biederman5cd81732004-03-11 15:01:31 +000043 /* AMD 8151 Errata 23 */
44 if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
45 (dev->device == PCI_DEVICE_ID_AMD_8151_SYSCTRL)) {
46 freq_cap &= ~(1 << HT_FREQ_800Mhz);
Yinghai Lu90b3e092005-01-26 22:00:20 +000047 } else
Eric Biederman5cd81732004-03-11 15:01:31 +000048 /* AMD K8 Unsupported 1Ghz? */
49 if ((dev->vendor == PCI_VENDOR_ID_AMD) && (dev->device == 0x1100)) {
Yinghai Lu90b3e092005-01-26 22:00:20 +000050 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
Eric Biederman5cd81732004-03-11 15:01:31 +000051 }
52 return freq_cap;
53}
Eric Biederman0ac6b412003-09-02 17:16:48 +000054
55struct prev_link {
56 struct device *dev;
57 unsigned pos;
58 unsigned char config_off, freq_off, freq_cap_off;
59};
60
61static int ht_setup_link(struct prev_link *prev, device_t dev, unsigned pos)
62{
63 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
64 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
65 unsigned present_width_cap, upstream_width_cap;
66 unsigned present_freq_cap, upstream_freq_cap;
67 unsigned ln_present_width_in, ln_upstream_width_in;
68 unsigned ln_present_width_out, ln_upstream_width_out;
69 unsigned freq, old_freq;
70 unsigned present_width, upstream_width, old_width;
71 int reset_needed;
Yinghai Lu1f1085b2005-01-17 21:37:12 +000072 int linkb_to_host;
Eric Biederman0ac6b412003-09-02 17:16:48 +000073
74 /* Set the hypertransport link width and frequency */
75 reset_needed = 0;
Yinghai Lu26b29222005-01-19 01:21:05 +000076 linkb_to_host = (pci_read_config16(dev, pos + PCI_CAP_FLAGS) >> 10) & 1;
Eric Biederman0ac6b412003-09-02 17:16:48 +000077
78 /* Read the capabilities */
Yinghai Lu1f1085b2005-01-17 21:37:12 +000079 present_freq_cap = ht_read_freq_cap(dev, pos + (linkb_to_host ? PCI_HT_CAP_SLAVE_FREQ_CAP1: PCI_HT_CAP_SLAVE_FREQ_CAP0));
Eric Biederman5cd81732004-03-11 15:01:31 +000080 upstream_freq_cap = ht_read_freq_cap(prev->dev, prev->pos + prev->freq_cap_off);
Yinghai Lu1f1085b2005-01-17 21:37:12 +000081 present_width_cap = pci_read_config8(dev, pos + (linkb_to_host ? PCI_HT_CAP_SLAVE_WIDTH1: PCI_HT_CAP_SLAVE_WIDTH0));
82 upstream_width_cap = pci_read_config8(prev->dev, prev->pos + prev->config_off);
Eric Biederman0ac6b412003-09-02 17:16:48 +000083
84 /* Calculate the highest useable frequency */
Eric Biederman0ac6b412003-09-02 17:16:48 +000085 freq = log2(present_freq_cap & upstream_freq_cap);
Eric Biederman0ac6b412003-09-02 17:16:48 +000086
87 /* Calculate the highest width */
88 ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
89 ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
90 if (ln_upstream_width_in > ln_present_width_out) {
91 ln_upstream_width_in = ln_present_width_out;
92 }
93 upstream_width = pow2_to_link_width[ln_upstream_width_in];
94 present_width = pow2_to_link_width[ln_upstream_width_in] << 4;
95
96 ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7];
97 ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
98 if (ln_upstream_width_out > ln_present_width_in) {
99 ln_upstream_width_out = ln_present_width_in;
100 }
101 upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
102 present_width |= pow2_to_link_width[ln_upstream_width_out];
103
104 /* Set the current device */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000105 old_freq = pci_read_config8(dev, pos + (linkb_to_host ? PCI_HT_CAP_SLAVE_FREQ1:PCI_HT_CAP_SLAVE_FREQ0));
Eric Biederman0ac6b412003-09-02 17:16:48 +0000106 if (freq != old_freq) {
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000107 pci_write_config8(dev, pos + (linkb_to_host ? PCI_HT_CAP_SLAVE_FREQ1:PCI_HT_CAP_SLAVE_FREQ0), freq);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000108 reset_needed = 1;
109 printk_spew("HyperT FreqP old %x new %x\n",old_freq,freq);
110 }
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000111 old_width = pci_read_config8(dev, pos + (linkb_to_host ? PCI_HT_CAP_SLAVE_WIDTH1: PCI_HT_CAP_SLAVE_WIDTH0) + 1);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000112 if (present_width != old_width) {
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000113 pci_write_config8(dev, pos + (linkb_to_host ? PCI_HT_CAP_SLAVE_WIDTH1: PCI_HT_CAP_SLAVE_WIDTH0) + 1, present_width);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000114 reset_needed = 1;
115 printk_spew("HyperT widthP old %x new %x\n",old_width, present_width);
116 }
117
118 /* Set the upstream device */
119 old_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
120 old_freq &= 0x0f;
121 if (freq != old_freq) {
122 pci_write_config8(prev->dev, prev->pos + prev->freq_off, freq);
123 reset_needed = 1;
124 printk_spew("HyperT freqU old %x new %x\n", old_freq, freq);
125 }
126 old_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
127 if (upstream_width != old_width) {
128 pci_write_config8(prev->dev, prev->pos + prev->config_off + 1, upstream_width);
129 reset_needed = 1;
130 printk_spew("HyperT widthU old %x new %x\n", old_width, upstream_width);
131 }
132
133 /* Remember the current link as the previous link */
134 prev->dev = dev;
135 prev->pos = pos;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000136 if(linkb_to_host) {
137 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
138 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
139 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
140 }
141 else {
142 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
143 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
144 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
145 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000146
147 return reset_needed;
148
149}
Yinghai Lu90b3e092005-01-26 22:00:20 +0000150#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000151
152static unsigned ht_lookup_slave_capability(struct device *dev)
153{
154 unsigned pos;
155 pos = 0;
156 switch(dev->hdr_type & 0x7f) {
157 case PCI_HEADER_TYPE_NORMAL:
158 case PCI_HEADER_TYPE_BRIDGE:
159 pos = PCI_CAPABILITY_LIST;
160 break;
161 }
162 if (pos > PCI_CAP_LIST_NEXT) {
163 pos = pci_read_config8(dev, pos);
164 }
165 while(pos != 0) { /* loop through the linked list */
166 uint8_t cap;
167 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
168 printk_spew("Capability: 0x%02x @ 0x%02x\n", cap, pos);
169 if (cap == PCI_CAP_ID_HT) {
170 unsigned flags;
171 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
172 printk_spew("flags: 0x%04x\n", (unsigned)flags);
173 if ((flags >> 13) == 0) {
174 /* Entry is a Slave secondary, success...*/
175 break;
176 }
177 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000178 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000179 }
180 return pos;
181}
182
183static void ht_collapse_early_enumeration(struct bus *bus)
184{
185 unsigned int devfn;
186
187 /* Spin through the devices and collapse any early
188 * hypertransport enumeration.
189 */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000190 for(devfn = PCI_DEVFN(1, 0); devfn <= 0xff; devfn += 8) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000191 struct device dummy;
192 uint32_t id;
193 unsigned pos, flags;
194 dummy.bus = bus;
195 dummy.path.type = DEVICE_PATH_PCI;
196 dummy.path.u.pci.devfn = devfn;
197 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
198 if (id == 0xffffffff || id == 0x00000000 ||
199 id == 0x0000ffff || id == 0xffff0000) {
200 continue;
201 }
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000202
arch import user (historical)98d0d302005-07-06 17:13:46 +0000203#if 0
204#if CK804_DEVN_BASE==0
205 //CK804 workaround:
206 // CK804 UnitID changes not use
207 if(id == 0x005e10de) {
208 break;
209 }
210#endif
211#endif
212
Eric Biederman0ac6b412003-09-02 17:16:48 +0000213 dummy.vendor = id & 0xffff;
214 dummy.device = (id >> 16) & 0xffff;
215 dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
216 pos = ht_lookup_slave_capability(&dummy);
217 if (!pos){
218 continue;
219 }
220
221 /* Clear the unitid */
222 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
223 flags &= ~0x1f;
224 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
225 printk_spew("Collapsing %s [%04x/%04x]\n",
226 dev_path(&dummy), dummy.vendor, dummy.device);
227 }
228}
229
230unsigned int hypertransport_scan_chain(struct bus *bus, unsigned int max)
231{
232 unsigned next_unitid, last_unitid, previous_unitid;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000233 device_t old_devices, dev, func, *chain_last;
234 unsigned min_unitid = 1;
Yinghai Lu90b3e092005-01-26 22:00:20 +0000235#if OPT_HT_LINK == 1
Eric Biederman0ac6b412003-09-02 17:16:48 +0000236 int reset_needed;
237 struct prev_link prev;
Yinghai Lu90b3e092005-01-26 22:00:20 +0000238#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000239
240 /* Restore the hypertransport chain to it's unitialized state */
241 ht_collapse_early_enumeration(bus);
242
243 /* See which static device nodes I have */
244 old_devices = bus->children;
245 bus->children = 0;
246 chain_last = &bus->children;
247
248 /* Initialize the hypertransport enumeration state */
Yinghai Lu90b3e092005-01-26 22:00:20 +0000249#if OPT_HT_LINK == 1
Eric Biederman0ac6b412003-09-02 17:16:48 +0000250 reset_needed = 0;
251 prev.dev = bus->dev;
252 prev.pos = bus->cap;
253 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
254 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
255 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
Yinghai Lu90b3e092005-01-26 22:00:20 +0000256#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000257
258 /* If present assign unitid to a hypertransport chain */
259 last_unitid = min_unitid -1;
260 next_unitid = min_unitid;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000261 do {
262 uint32_t id, class;
Yinghai Lu90b3e092005-01-26 22:00:20 +0000263 uint8_t hdr_type;
264 unsigned pos;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000265 uint16_t flags;
266 unsigned count, static_count;
267
268 previous_unitid = last_unitid;
269 last_unitid = next_unitid;
270
271 /* Get setup the device_structure */
272 dev = ht_scan_get_devs(&old_devices);
273
274 if (!dev) {
275 struct device dummy;
276 dummy.bus = bus;
277 dummy.path.type = DEVICE_PATH_PCI;
278 dummy.path.u.pci.devfn = 0;
279 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
280 /* If the chain is fully enumerated quit */
281 if (id == 0xffffffff || id == 0x00000000 ||
282 id == 0x0000ffff || id == 0xffff0000) {
283 break;
284 }
285 dev = alloc_dev(bus, &dummy.path);
286 }
287 else {
288 /* Add this device to the pci bus chain */
289 *chain_last = dev;
Eric Biederman5cd81732004-03-11 15:01:31 +0000290 /* Run the magice enable sequence for the device */
Eric Biederman7003ba42004-10-16 06:20:29 +0000291 if (dev->chip_ops && dev->chip_ops->enable_dev) {
292 dev->chip_ops->enable_dev(dev);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000293 }
294 /* Now read the vendor and device id */
295 id = pci_read_config32(dev, PCI_VENDOR_ID);
Eric Biederman83b991a2003-10-11 06:20:25 +0000296
297 /* If the chain is fully enumerated quit */
298 if (id == 0xffffffff || id == 0x00000000 ||
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000299 id == 0x0000ffff || id == 0xffff0000) {
300 if (dev->enabled) {
301 printk_info("Disabling static device: %s\n",
302 dev_path(dev));
303 dev->enabled = 0;
304 }
Eric Biederman83b991a2003-10-11 06:20:25 +0000305 break;
306 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000307 }
308 /* Update the device chain tail */
309 for(func = dev; func; func = func->sibling) {
310 chain_last = &func->sibling;
311 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000312
313 /* Find the hypertransport link capability */
314 pos = ht_lookup_slave_capability(dev);
315 if (pos == 0) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000316 printk_err("%s Hypertransport link capability not found",
317 dev_path(dev));
Eric Biederman0ac6b412003-09-02 17:16:48 +0000318 break;
319 }
320
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000321
Eric Biederman0ac6b412003-09-02 17:16:48 +0000322 /* Update the Unitid of the current device */
323 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
324 flags &= ~0x1f; /* mask out base Unit ID */
arch import user (historical)98d0d302005-07-06 17:13:46 +0000325#if CK804_DEVN_BASE==0
326 if(id == 0x005e10de) {
327 next_unitid = 0;
328 }
329 else {
330#endif
331 flags |= next_unitid & 0x1f;
332 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
333#if CK804_DEVN_BASE==0
334 }
335#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000336
337 /* Update the Unitd id in the device structure */
338 static_count = 1;
339 for(func = dev; func; func = func->sibling) {
340 func->path.u.pci.devfn += (next_unitid << 3);
341 static_count = (func->path.u.pci.devfn >> 3)
342 - (dev->path.u.pci.devfn >> 3) + 1;
343 }
344
Yinghai Lu90b3e092005-01-26 22:00:20 +0000345 /* Read the rest of the pci configuration information */
346 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
347 class = pci_read_config32(dev, PCI_CLASS_REVISION);
348
349 /* Store the interesting information in the device structure */
350 dev->vendor = id & 0xffff;
351 dev->device = (id >> 16) & 0xffff;
352 dev->hdr_type = hdr_type;
353 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
354 dev->class = class >> 8;
355
Eric Biederman0ac6b412003-09-02 17:16:48 +0000356 /* Compute the number of unitids consumed */
357 count = (flags >> 5) & 0x1f; /* get unit count */
358 printk_spew("%s count: %04x static_count: %04x\n",
359 dev_path(dev), count, static_count);
360 if (count < static_count) {
361 count = static_count;
362 }
363
364 /* Update the Unitid of the next device */
365 next_unitid += count;
366
Yinghai Lu90b3e092005-01-26 22:00:20 +0000367#if OPT_HT_LINK == 1
Eric Biederman0ac6b412003-09-02 17:16:48 +0000368 /* Setup the hypetransport link */
369 reset_needed |= ht_setup_link(&prev, dev, pos);
Yinghai Lu90b3e092005-01-26 22:00:20 +0000370#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000371
372 printk_debug("%s [%04x/%04x] %s next_unitid: %04x\n",
373 dev_path(dev),
374 dev->vendor, dev->device,
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000375 (dev->enabled? "enabled": "disabled"), next_unitid);
arch import user (historical)98d0d302005-07-06 17:13:46 +0000376#if CK804_DEVN_BASE==0
377 if(id == 0x005e10de) {
378 break; // CK804 can not change unitid, so it only can be alone in the link
379 }
380#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000381
382 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
Yinghai Lu90b3e092005-01-26 22:00:20 +0000383
384#if OPT_HT_LINK == 1
Eric Biederman0ac6b412003-09-02 17:16:48 +0000385#if HAVE_HARD_RESET == 1
386 if(reset_needed) {
387 printk_info("HyperT reset needed\n");
Eric Biederman5cd81732004-03-11 15:01:31 +0000388 hard_reset();
389 }
390 else {
391 printk_debug("HyperT reset not needed\n");
392 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000393#endif
Yinghai Lu90b3e092005-01-26 22:00:20 +0000394#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000395 if (next_unitid > 0x1f) {
396 next_unitid = 0x1f;
397 }
398 return pci_scan_bus(bus, 0x00, (next_unitid << 3)|7, max);
399}