add the CAFE IRQ support
[coreboot.git] / src / devices / hypertransport.c
index a30c8f6a8dec2cdc19333ac18c395120be78dad0..f0bd817bf49db35ede09532959ddf37db79490fd 100644 (file)
@@ -1,3 +1,8 @@
+/*
+       2005.11 yhlu add let the real sb to use small uintid
+
+*/
+
 #include <bitops.h>
 #include <console/console.h>
 #include <device/device.h>
@@ -11,7 +16,7 @@
 #define OPT_HT_LINK 0
         
 #if OPT_HT_LINK == 1
-#include "../northbridge/amd/amdk8/cpu_rev.c"
+#include <cpu/amd/model_fxx_rev.h>
 #endif
 
 static device_t ht_scan_get_devs(device_t *old_devices)
@@ -71,12 +76,15 @@ static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
        }
        /* AMD K8 Unsupported 1Ghz? */
        if ((dev->vendor == PCI_VENDOR_ID_AMD) && (dev->device == 0x1100)) {
-#if K8_HT_FREQ_1G_SUPPORT == 1  
-               if (is_cpu_pre_e0()) 
-#endif
-               {
+#if K8_HT_FREQ_1G_SUPPORT == 1 
+       #if K8_REV_F_SUPPORT == 0 
+               if (is_cpu_pre_e0()) { // only e0 later suupport 1GHz HT
                        freq_cap &= ~(1 << HT_FREQ_1000Mhz);
-               }
+               } 
+       #endif
+#else
+               freq_cap &= ~(1 << HT_FREQ_1000Mhz);
+#endif
 
        }
        return freq_cap;
@@ -248,7 +256,7 @@ static unsigned ht_lookup_slave_capability(struct device *dev)
        return pos;
 }
 
-static void ht_collapse_early_enumeration(struct bus *bus)
+static void ht_collapse_early_enumeration(struct bus *bus, unsigned offset_unitid)
 {
        unsigned int devfn;
        struct ht_link prev;
@@ -271,10 +279,43 @@ static void ht_collapse_early_enumeration(struct bus *bus)
                }
                /* Has the link failed? */
                if (ctrl & (1 << 4)) {
-                       return;
+                       /*
+                        * Either the link has failed, or we have
+                        * a CRC error.
+                        * Sometimes this can happen due to link
+                        * retrain, so lets knock it down and see
+                        * if its transient
+                        */
+                       ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
+                       pci_write_config16(prev.dev, prev.pos + prev.ctrl_off, ctrl);
+                       ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
+                       if (ctrl & ((1 << 4) | (1 << 8))) {
+                               printk_alert("Detected error on Hypertransport Link\n");
+                               return;
+                       }
                }
        } while((ctrl & (1 << 5)) == 0);
 
+               //actually, only for one HT device HT chain, and unitid is 0
+#if HT_CHAIN_UNITID_BASE == 0
+        if(offset_unitid) {
+                return;
+        }
+#endif
+
+        /* Check if is already collapsed */
+        if((!offset_unitid)|| (offset_unitid && (!((HT_CHAIN_END_UNITID_BASE == 0) && (HT_CHAIN_END_UNITID_BASE <HT_CHAIN_UNITID_BASE))))) {
+                struct device dummy;
+                uint32_t id;
+                dummy.bus              = bus;
+                dummy.path.type        = DEVICE_PATH_PCI;
+                dummy.path.u.pci.devfn = PCI_DEVFN(0, 0);
+                id = pci_read_config32(&dummy, PCI_VENDOR_ID);
+                if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
+                    (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
+                             return;
+                }
+        }
 
        /* Spin through the devices and collapse any early
         * hypertransport enumeration.
@@ -309,15 +350,25 @@ static void ht_collapse_early_enumeration(struct bus *bus)
 }
 
 unsigned int hypertransport_scan_chain(struct bus *bus, 
-       unsigned min_devfn, unsigned max_devfn, unsigned int max)
+       unsigned min_devfn, unsigned max_devfn, unsigned int max, unsigned *ht_unitid_base, unsigned offset_unitid)
 {
+       //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
        unsigned next_unitid, last_unitid;
        device_t old_devices, dev, func;
-       unsigned min_unitid = 1;
+       unsigned min_unitid = (offset_unitid) ? HT_CHAIN_UNITID_BASE:1;
        struct ht_link prev;
+       device_t last_func = 0;
+       int ht_dev_num = 0;
+
+#if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
+        //let't record the device of last ht device, So we can set the Unitid to HT_CHAIN_END_UNITID_BASE
+        unsigned real_last_unitid; 
+        uint8_t real_last_pos;
+       device_t real_last_dev;
+#endif
 
        /* Restore the hypertransport chain to it's unitialized state */
-       ht_collapse_early_enumeration(bus);
+       ht_collapse_early_enumeration(bus, offset_unitid);
 
        /* See which static device nodes I have */
        old_devices = bus->children;
@@ -345,12 +396,25 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                /* Wait until the link initialization is complete */
                do {
                        ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
-                       /* Is this the end of the hypertransport chain?
-                        * Has the link failed?
-                        * If so further scanning is pointless.
-                        */
-                       if (ctrl & ((1 << 6) | (1 << 4))) {
-                               goto end_of_chain;
+
+                       if (ctrl & (1 << 6))
+                               goto end_of_chain;      // End of chain
+
+                       if (ctrl & ((1 << 4) | (1 << 8))) {
+                               /*
+                                * Either the link has failed, or we have
+                                * a CRC error.
+                                * Sometimes this can happen due to link
+                                * retrain, so lets knock it down and see
+                                * if its transient
+                                */
+                               ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
+                               pci_write_config16(prev.dev, prev.pos + prev.ctrl_off, ctrl);
+                               ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
+                               if (ctrl & ((1 << 4) | (1 << 8))) {
+                                       printk_alert("Detected error on Hypertransport Link\n");
+                                       goto end_of_chain;
+                               }
                        }
                } while((ctrl & (1 << 5)) == 0);
                
@@ -387,17 +451,8 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                }
 
                flags &= ~0x1f; /* mask out base Unit ID */
-#if CK804_DEVN_BASE==0  
-               if((dev->vendor == 0x10de) && (dev->device == 0x005e)) {
-                       next_unitid = 0;
-               } 
-               else {
-#endif
-                       flags |= next_unitid & 0x1f;
-                       pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
-#if CK804_DEVN_BASE==0 
-               }
-#endif
+                flags |= next_unitid & 0x1f;
+                pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
 
                /* Update the Unitd id in the device structure */
                static_count = 1;
@@ -405,6 +460,7 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                        func->path.u.pci.devfn += (next_unitid << 3);
                        static_count = (func->path.u.pci.devfn >> 3) 
                                - (dev->path.u.pci.devfn >> 3) + 1;
+                       last_func = func;
                }
 
                /* Compute the number of unitids consumed */
@@ -416,6 +472,15 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                }
 
                /* Update the Unitid of the next device */
+               ht_unitid_base[ht_dev_num] = next_unitid;
+               ht_dev_num++;
+#if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
+               if(offset_unitid) {
+                       real_last_unitid = next_unitid;
+                       real_last_pos = pos;
+                       real_last_dev = dev;
+               }
+#endif
                next_unitid += count;
 
                /* Setup the hypetransport link */
@@ -426,12 +491,6 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                        dev->vendor, dev->device, 
                        (dev->enabled? "enabled": "disabled"), next_unitid);
 
-#if CK804_DEVN_BASE==0 
-               if ((dev->vendor == 0x10de) && (dev->device == 0x005e)) {
-                      break; // CK804 can not change unitid, so it only can be alone in the link
-                }
-#endif
-
        } while((last_unitid != next_unitid) && (next_unitid <= (max_devfn >> 3)));
  end_of_chain:
 #if OPT_HT_LINK == 1
@@ -442,6 +501,28 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                printk_debug("HyperT reset not needed\n");
        }
 #endif
+
+#if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
+        if(offset_unitid && (ht_dev_num>0)) {
+                uint16_t flags;
+                int i;
+               device_t last_func = 0;
+                flags = pci_read_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS);
+                flags &= ~0x1f;
+                flags |= HT_CHAIN_END_UNITID_BASE & 0x1f;
+                pci_write_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS, flags);
+
+                for(func = real_last_dev; func; func = func->sibling) {
+                        func->path.u.pci.devfn -= ((real_last_unitid - HT_CHAIN_END_UNITID_BASE) << 3);
+                       last_func = func;
+                }
+               
+               ht_unitid_base[ht_dev_num-1] = HT_CHAIN_END_UNITID_BASE; // update last one
+               
+                next_unitid = real_last_unitid;
+        }
+#endif
+
        if (next_unitid > 0x1f) {
                next_unitid = 0x1f;
        }
@@ -454,13 +535,15 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
                for(left = old_devices; left; left = left->sibling) {
                        printk_debug("%s\n", dev_path(left));
                }
-               die("Left over static devices.  Check your Config.lb\n");
+               printk_err("HT: Left over static devices.  Check your Config.lb\n");
+               if(last_func  && !last_func->sibling) // put back the left over static device, and let pci_scan_bus disable it
+                       last_func->sibling = old_devices; 
        }
-       
+
        /* Now that nothing is overlapping it is safe to scan the
         * children. 
         */
-       max = pci_scan_bus(bus, 0x00, (next_unitid << 3)|7, max);
+       max = pci_scan_bus(bus, 0x00, (next_unitid << 3)|7, max); 
        return max; 
 }
 
@@ -477,9 +560,17 @@ unsigned int hypertransport_scan_chain(struct bus *bus,
  *
  * @return The maximum bus number found, after scanning all subordinate busses
  */
+unsigned int hypertransport_scan_chain_x(struct bus *bus,
+        unsigned min_devfn, unsigned max_devfn, unsigned int max)
+{
+       unsigned ht_unitid_base[4];
+       unsigned offset_unitid = 1;
+       return hypertransport_scan_chain(bus, min_devfn, max_devfn, max, ht_unitid_base, offset_unitid);
+}
+
 unsigned int ht_scan_bridge(struct device *dev, unsigned int max)
 {
-       return do_pci_scan_bridge(dev, max, hypertransport_scan_chain);
+       return do_pci_scan_bridge(dev, max, hypertransport_scan_chain_x);
 }