2 * This file is part of the LinuxBIOS project.
4 * Copyright (C) 2003-2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2004 David Hendricks <sc@flagen.com>
7 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8 * Copyright (C) 2005-2006 Tyan
9 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10 * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 2005.11 yhlu add let the real sb to use small uintid
31 #include <console/console.h>
32 #include <device/device.h>
33 #include <device/path.h>
34 #include <device/pci.h>
35 #include <device/pci_ids.h>
36 #include <device/hypertransport.h>
37 #include <part/hard_reset.h>
38 #include <part/fallback_boot.h>
43 #include <cpu/amd/model_fxx_rev.h>
46 static device_t ht_scan_get_devs(device_t *old_devices)
51 /* Extract the chain of devices to (first through last)
52 * for the next hypertransport device.
54 while(last && last->sibling &&
55 (last->sibling->path.type == DEVICE_PATH_PCI) &&
56 (last->sibling->path.u.pci.devfn > last->path.u.pci.devfn))
62 /* Unlink the chain from the list of old devices */
63 *old_devices = last->sibling;
66 /* Now add the device to the list of devices on the bus.
68 /* Find the last child of our parent */
69 for(child = first->bus->children; child && child->sibling; ) {
70 child = child->sibling;
72 /* Place the chain on the list of children of their parent. */
74 child->sibling = first;
76 first->bus->children = first;
83 static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
85 /* Handle bugs in valid hypertransport frequency reporting */
88 freq_cap = pci_read_config16(dev, pos);
89 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
91 /* AMD 8131 Errata 48 */
92 if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
93 (dev->device == PCI_DEVICE_ID_AMD_8131_PCIX)) {
94 freq_cap &= ~(1 << HT_FREQ_800Mhz);
96 /* AMD 8151 Errata 23 */
97 if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
98 (dev->device == PCI_DEVICE_ID_AMD_8151_SYSCTRL)) {
99 freq_cap &= ~(1 << HT_FREQ_800Mhz);
101 /* AMD K8 Unsupported 1Ghz? */
102 if ((dev->vendor == PCI_VENDOR_ID_AMD) && (dev->device == 0x1100)) {
103 #if K8_HT_FREQ_1G_SUPPORT == 1
104 #if K8_REV_F_SUPPORT == 0
105 if (is_cpu_pre_e0()) { // only e0 later suupport 1GHz HT
106 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
110 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
121 unsigned char ctrl_off, config_off, freq_off, freq_cap_off;
124 static int ht_setup_link(struct ht_link *prev, device_t dev, unsigned pos)
126 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
127 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
128 struct ht_link cur[1];
129 unsigned present_width_cap, upstream_width_cap;
130 unsigned present_freq_cap, upstream_freq_cap;
131 unsigned ln_present_width_in, ln_upstream_width_in;
132 unsigned ln_present_width_out, ln_upstream_width_out;
133 unsigned freq, old_freq;
134 unsigned present_width, upstream_width, old_width;
138 /* Set the hypertransport link width and frequency */
140 /* See which side of the device our previous write to
141 * set the unitid came from.
145 linkb_to_host = (pci_read_config16(cur->dev, cur->pos + PCI_CAP_FLAGS) >> 10) & 1;
146 if (!linkb_to_host) {
147 cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0;
148 cur->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
149 cur->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
150 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
153 cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1;
154 cur->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
155 cur->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
156 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
159 /* Read the capabilities */
160 present_freq_cap = ht_read_freq_cap(cur->dev, cur->pos + cur->freq_cap_off);
161 upstream_freq_cap = ht_read_freq_cap(prev->dev, prev->pos + prev->freq_cap_off);
162 present_width_cap = pci_read_config8(cur->dev, cur->pos + cur->config_off);
163 upstream_width_cap = pci_read_config8(prev->dev, prev->pos + prev->config_off);
165 /* Calculate the highest useable frequency */
166 freq = log2(present_freq_cap & upstream_freq_cap);
168 /* Calculate the highest width */
169 ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
170 ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
171 if (ln_upstream_width_in > ln_present_width_out) {
172 ln_upstream_width_in = ln_present_width_out;
174 upstream_width = pow2_to_link_width[ln_upstream_width_in];
175 present_width = pow2_to_link_width[ln_upstream_width_in] << 4;
177 ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7];
178 ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
179 if (ln_upstream_width_out > ln_present_width_in) {
180 ln_upstream_width_out = ln_present_width_in;
182 upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
183 present_width |= pow2_to_link_width[ln_upstream_width_out];
185 /* Set the current device */
186 old_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off);
188 if (freq != old_freq) {
190 pci_write_config8(cur->dev, cur->pos + cur->freq_off, freq);
192 printk_spew("HyperT FreqP old %x new %x\n",old_freq,freq);
193 new_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off);
195 if (new_freq != freq) {
196 printk_err("%s Hypertransport frequency would not set wanted: %x got: %x\n",
197 dev_path(dev), freq, new_freq);
200 old_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1);
201 if (present_width != old_width) {
203 pci_write_config8(cur->dev, cur->pos + cur->config_off + 1,
206 printk_spew("HyperT widthP old %x new %x\n",old_width, present_width);
207 new_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1);
208 if (new_width != present_width) {
209 printk_err("%s Hypertransport width would not set wanted: %x got: %x\n",
210 dev_path(dev), present_width, new_width);
214 /* Set the upstream device */
215 old_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
217 if (freq != old_freq) {
219 pci_write_config8(prev->dev, prev->pos + prev->freq_off, freq);
221 printk_spew("HyperT freqU old %x new %x\n", old_freq, freq);
222 new_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
224 if (new_freq != freq) {
225 printk_err("%s Hypertransport frequency would not set wanted: %x got: %x\n",
226 dev_path(prev->dev), freq, new_freq);
229 old_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
230 if (upstream_width != old_width) {
232 pci_write_config8(prev->dev, prev->pos + prev->config_off + 1, upstream_width);
234 printk_spew("HyperT widthU old %x new %x\n", old_width, upstream_width);
235 new_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
236 if (new_width != upstream_width) {
237 printk_err("%s Hypertransport width would not set wanted: %x got: %x\n",
238 dev_path(prev->dev), upstream_width, new_width);
243 /* Remember the current link as the previous link,
244 * But look at the other offsets.
246 prev->dev = cur->dev;
247 prev->pos = cur->pos;
248 if (cur->ctrl_off == PCI_HT_CAP_SLAVE_CTRL0) {
249 prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1;
250 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
251 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
252 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
254 prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0;
255 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
256 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
257 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
264 static unsigned ht_lookup_slave_capability(struct device *dev)
269 pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos);
272 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
273 printk_spew("flags: 0x%04x\n", flags);
274 if ((flags >> 13) == 0) {
275 /* Entry is a Slave secondary, success... */
283 static void ht_collapse_early_enumeration(struct bus *bus, unsigned offset_unitid)
289 /* Initialize the hypertransport enumeration state */
292 prev.ctrl_off = PCI_HT_CAP_HOST_CTRL;
293 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
294 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
295 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
297 /* Wait until the link initialization is complete */
299 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
300 /* Is this the end of the hypertransport chain */
301 if (ctrl & (1 << 6)) {
304 /* Has the link failed? */
305 if (ctrl & (1 << 4)) {
307 * Either the link has failed, or we have
309 * Sometimes this can happen due to link
310 * retrain, so lets knock it down and see
313 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
314 pci_write_config16(prev.dev, prev.pos + prev.ctrl_off, ctrl);
315 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
316 if (ctrl & ((1 << 4) | (1 << 8))) {
317 printk_alert("Detected error on Hypertransport Link\n");
321 } while((ctrl & (1 << 5)) == 0);
323 //actually, only for one HT device HT chain, and unitid is 0
324 #if HT_CHAIN_UNITID_BASE == 0
330 /* Check if is already collapsed */
331 if((!offset_unitid)|| (offset_unitid && (!((HT_CHAIN_END_UNITID_BASE == 0) && (HT_CHAIN_END_UNITID_BASE <HT_CHAIN_UNITID_BASE))))) {
335 dummy.path.type = DEVICE_PATH_PCI;
336 dummy.path.u.pci.devfn = PCI_DEVFN(0, 0);
337 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
338 if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
339 (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
344 /* Spin through the devices and collapse any early
345 * hypertransport enumeration.
347 for(devfn = PCI_DEVFN(1, 0); devfn <= 0xff; devfn += 8) {
352 dummy.path.type = DEVICE_PATH_PCI;
353 dummy.path.u.pci.devfn = devfn;
354 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
355 if ( (id == 0xffffffff) || (id == 0x00000000) ||
356 (id == 0x0000ffff) || (id == 0xffff0000)) {
359 dummy.vendor = id & 0xffff;
360 dummy.device = (id >> 16) & 0xffff;
361 dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
362 pos = ht_lookup_slave_capability(&dummy);
367 /* Clear the unitid */
368 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
370 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
371 printk_spew("Collapsing %s [%04x/%04x]\n",
372 dev_path(&dummy), dummy.vendor, dummy.device);
376 unsigned int hypertransport_scan_chain(struct bus *bus,
377 unsigned min_devfn, unsigned max_devfn, unsigned int max, unsigned *ht_unitid_base, unsigned offset_unitid)
379 //even HT_CHAIN_UNITID_BASE == 0, we still can go through this function, because of end_of_chain check, also We need it to optimize link
380 unsigned next_unitid, last_unitid;
381 device_t old_devices, dev, func;
382 unsigned min_unitid = (offset_unitid) ? HT_CHAIN_UNITID_BASE:1;
384 device_t last_func = 0;
386 unsigned temp_unitid;
387 unsigned not_use_count;
389 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
390 //let't record the device of last ht device, So we can set the Unitid to HT_CHAIN_END_UNITID_BASE
391 unsigned real_last_unitid;
392 uint8_t real_last_pos;
393 device_t real_last_dev;
396 /* Restore the hypertransport chain to it's unitialized state */
397 ht_collapse_early_enumeration(bus, offset_unitid);
399 /* See which static device nodes I have */
400 old_devices = bus->children;
403 /* Initialize the hypertransport enumeration state */
406 prev.ctrl_off = PCI_HT_CAP_HOST_CTRL;
407 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
408 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
409 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
411 /* If present assign unitid to a hypertransport chain */
412 last_unitid = min_unitid -1;
413 next_unitid = min_unitid;
417 unsigned count, static_count;
420 last_unitid = next_unitid;
422 /* Wait until the link initialization is complete */
424 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
427 goto end_of_chain; // End of chain
429 if (ctrl & ((1 << 4) | (1 << 8))) {
431 * Either the link has failed, or we have
433 * Sometimes this can happen due to link
434 * retrain, so lets knock it down and see
437 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
438 pci_write_config16(prev.dev, prev.pos + prev.ctrl_off, ctrl);
439 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
440 if (ctrl & ((1 << 4) | (1 << 8))) {
441 printk_alert("Detected error on Hypertransport Link\n");
445 } while((ctrl & (1 << 5)) == 0);
448 /* Get and setup the device_structure */
449 dev = ht_scan_get_devs(&old_devices);
451 /* See if a device is present and setup the
454 dev = pci_probe_dev(dev, bus, 0);
455 if (!dev || !dev->enabled) {
459 /* Find the hypertransport link capability */
460 pos = ht_lookup_slave_capability(dev);
462 printk_err("%s Hypertransport link capability not found",
467 /* Update the Unitid of the current device */
468 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
470 /* If the devices has a unitid set and is at devfn 0 we are done.
471 * This can happen with shadow hypertransport devices,
472 * or if we have reached the bottom of a
473 * hypertransport device chain.
478 flags &= ~0x1f; /* mask out base Unit ID */
480 count = (flags >> 5) & 0x1f; /* get unit count */
482 temp_unitid = next_unitid;
483 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
485 if((next_unitid+count)>=0x20) {
486 temp_unitid = HT_CHAIN_END_UNITID_BASE;
487 //still use the old next_unitid
494 flags |= temp_unitid & 0x1f;
495 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
497 /* Update the Unitd id in the device structure */
499 for(func = dev; func; func = func->sibling) {
500 func->path.u.pci.devfn += (temp_unitid << 3);
501 static_count = (func->path.u.pci.devfn >> 3)
502 - (dev->path.u.pci.devfn >> 3) + 1;
505 /* Compute the number of unitids consumed */
506 printk_spew("%s count: %04x static_count: %04x\n",
507 dev_path(dev), count, static_count);
508 if (count < static_count) {
509 count = static_count;
512 next_unitid += count;
514 /* Update the Unitid of the next device */
515 ht_unitid_base[ht_dev_num] = temp_unitid;
518 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
521 real_last_unitid = temp_unitid;
526 /* Setup the hypetransport link */
527 bus->reset_needed |= ht_setup_link(&prev, dev, pos);
529 printk_debug("%s [%04x/%04x] %s next_unitid: %04x\n",
531 dev->vendor, dev->device,
532 (dev->enabled? "enabled": "disabled"), next_unitid);
534 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f /*(max_devfn >> 3)*/ ));
537 if(bus->reset_needed) {
538 printk_info("HyperT reset needed\n");
541 printk_debug("HyperT reset not needed\n");
545 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
546 if(offset_unitid && (ht_dev_num>1) && (real_last_unitid != HT_CHAIN_END_UNITID_BASE) ) {
549 device_t last_func = 0;
550 flags = pci_read_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS);
552 flags |= HT_CHAIN_END_UNITID_BASE & 0x1f;
553 pci_write_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS, flags);
555 for(func = real_last_dev; func; func = func->sibling) {
556 func->path.u.pci.devfn -= ((real_last_unitid - HT_CHAIN_END_UNITID_BASE) << 3);
560 ht_unitid_base[ht_dev_num-1] = HT_CHAIN_END_UNITID_BASE; // update last one
562 next_unitid = real_last_unitid;
566 if (next_unitid > 0x1f) {
569 if( (bus->secondary == 0) && (next_unitid > 0x17)) {
570 next_unitid = 0x17; /* avoid K8 on bus 0 */
573 /* Die if any leftover Static devices are are found.
574 * There's probably a problem in the Config.lb.
578 for(left = old_devices; left; left = left->sibling) {
579 printk_debug("%s\n", dev_path(left));
581 printk_err("HT: Left over static devices. Check your Config.lb\n");
582 if(last_func && !last_func->sibling) // put back the left over static device, and let pci_scan_bus disable it
583 last_func->sibling = old_devices;
586 /* Now that nothing is overlapping it is safe to scan the
589 max = pci_scan_bus(bus, 0x00, ((next_unitid-1) << 3)|7, max);
594 * @brief Scan a PCI bridge and the buses behind the bridge.
596 * Determine the existence of buses behind the bridge. Set up the bridge
597 * according to the result of the scan.
599 * This function is the default scan_bus() method for PCI bridge devices.
601 * @param dev pointer to the bridge device
602 * @param max the highest bus number assgined up to now
604 * @return The maximum bus number found, after scanning all subordinate busses
606 unsigned int hypertransport_scan_chain_x(struct bus *bus,
607 unsigned min_devfn, unsigned max_devfn, unsigned int max)
609 unsigned ht_unitid_base[4];
610 unsigned offset_unitid = 1;
611 return hypertransport_scan_chain(bus, min_devfn, max_devfn, max, ht_unitid_base, offset_unitid);
614 unsigned int ht_scan_bridge(struct device *dev, unsigned int max)
616 return do_pci_scan_bridge(dev, max, hypertransport_scan_chain_x);
620 /** Default device operations for hypertransport bridges */
621 static struct pci_operations ht_bus_ops_pci = {
625 struct device_operations default_ht_ops_bus = {
626 .read_resources = pci_bus_read_resources,
627 .set_resources = pci_dev_set_resources,
628 .enable_resources = pci_bus_enable_resources,
630 .scan_bus = ht_scan_bridge,
632 .reset_bus = pci_bus_reset,
633 .ops_pci = &ht_bus_ops_pci,