2 #include <console/console.h>
3 #include <device/device.h>
4 #include <device/path.h>
5 #include <device/pci.h>
6 #include <part/hard_reset.h>
7 #include <part/fallback_boot.h>
9 static device_t ht_scan_get_devs(device_t *old_devices)
14 while(last && last->sibling &&
15 (last->sibling->path.u.pci.devfn > last->path.u.pci.devfn)) {
19 *old_devices = last->sibling;
29 unsigned char config_off, freq_off, freq_cap_off;
32 static int ht_setup_link(struct prev_link *prev, device_t dev, unsigned pos)
34 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
35 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
36 unsigned present_width_cap, upstream_width_cap;
37 unsigned present_freq_cap, upstream_freq_cap;
38 unsigned ln_present_width_in, ln_upstream_width_in;
39 unsigned ln_present_width_out, ln_upstream_width_out;
40 unsigned freq, old_freq;
41 unsigned present_width, upstream_width, old_width;
44 /* Set the hypertransport link width and frequency */
47 /* Read the capabilities */
48 present_freq_cap = pci_read_config16(dev, pos + PCI_HT_CAP_SLAVE_FREQ_CAP0);
49 upstream_freq_cap = pci_read_config16(prev->dev, prev->pos + prev->freq_cap_off);
50 present_width_cap = pci_read_config8(dev, pos + PCI_HT_CAP_SLAVE_WIDTH0);
51 upstream_width_cap = pci_read_config8(prev->dev, prev->pos + prev->config_off);
53 /* Calculate the highest useable frequency */
55 freq = log2(present_freq_cap & upstream_freq_cap);
57 /* Errata for 8131 - freq 5 has hardware problems don't support it */
58 freq = log2(present_freq_cap & upstream_freq_cap & 0x1f);
61 /* Calculate the highest width */
62 ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
63 ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
64 if (ln_upstream_width_in > ln_present_width_out) {
65 ln_upstream_width_in = ln_present_width_out;
67 upstream_width = pow2_to_link_width[ln_upstream_width_in];
68 present_width = pow2_to_link_width[ln_upstream_width_in] << 4;
70 ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7];
71 ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
72 if (ln_upstream_width_out > ln_present_width_in) {
73 ln_upstream_width_out = ln_present_width_in;
75 upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
76 present_width |= pow2_to_link_width[ln_upstream_width_out];
78 /* Set the current device */
79 old_freq = pci_read_config8(dev, pos + PCI_HT_CAP_SLAVE_FREQ0);
80 if (freq != old_freq) {
81 pci_write_config8(dev, pos + PCI_HT_CAP_SLAVE_FREQ0, freq);
83 printk_spew("HyperT FreqP old %x new %x\n",old_freq,freq);
85 old_width = pci_read_config8(dev, pos + PCI_HT_CAP_SLAVE_WIDTH0 + 1);
86 if (present_width != old_width) {
87 pci_write_config8(dev, pos + PCI_HT_CAP_SLAVE_WIDTH0 + 1, present_width);
89 printk_spew("HyperT widthP old %x new %x\n",old_width, present_width);
92 /* Set the upstream device */
93 old_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
95 if (freq != old_freq) {
96 pci_write_config8(prev->dev, prev->pos + prev->freq_off, freq);
98 printk_spew("HyperT freqU old %x new %x\n", old_freq, freq);
100 old_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
101 if (upstream_width != old_width) {
102 pci_write_config8(prev->dev, prev->pos + prev->config_off + 1, upstream_width);
104 printk_spew("HyperT widthU old %x new %x\n", old_width, upstream_width);
107 /* Remember the current link as the previous link */
110 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
111 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
112 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
118 static unsigned ht_lookup_slave_capability(struct device *dev)
122 switch(dev->hdr_type & 0x7f) {
123 case PCI_HEADER_TYPE_NORMAL:
124 case PCI_HEADER_TYPE_BRIDGE:
125 pos = PCI_CAPABILITY_LIST;
128 if (pos > PCI_CAP_LIST_NEXT) {
129 pos = pci_read_config8(dev, pos);
131 while(pos != 0) { /* loop through the linked list */
133 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
134 printk_spew("Capability: 0x%02x @ 0x%02x\n", cap, pos);
135 if (cap == PCI_CAP_ID_HT) {
137 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
138 printk_spew("flags: 0x%04x\n", (unsigned)flags);
139 if ((flags >> 13) == 0) {
140 /* Entry is a Slave secondary, success...*/
145 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
151 static void ht_collapse_early_enumeration(struct bus *bus)
155 /* Spin through the devices and collapse any early
156 * hypertransport enumeration.
158 for(devfn = 0; devfn <= 0xff; devfn += 8) {
163 dummy.path.type = DEVICE_PATH_PCI;
164 dummy.path.u.pci.devfn = devfn;
165 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
166 if (id == 0xffffffff || id == 0x00000000 ||
167 id == 0x0000ffff || id == 0xffff0000) {
170 dummy.vendor = id & 0xffff;
171 dummy.device = (id >> 16) & 0xffff;
172 dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
173 pos = ht_lookup_slave_capability(&dummy);
178 /* Clear the unitid */
179 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
181 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
182 printk_spew("Collapsing %s [%04x/%04x]\n",
183 dev_path(&dummy), dummy.vendor, dummy.device);
187 unsigned int hypertransport_scan_chain(struct bus *bus, unsigned int max)
189 unsigned next_unitid, last_unitid, previous_unitid;
190 uint8_t previous_pos;
191 device_t old_devices, dev, func, *chain_last;
192 unsigned min_unitid = 1;
194 struct prev_link prev;
196 /* Restore the hypertransport chain to it's unitialized state */
197 ht_collapse_early_enumeration(bus);
199 /* See which static device nodes I have */
200 old_devices = bus->children;
202 chain_last = &bus->children;
204 /* Initialize the hypertransport enumeration state */
208 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
209 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
210 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
212 /* If present assign unitid to a hypertransport chain */
213 last_unitid = min_unitid -1;
214 next_unitid = min_unitid;
218 uint8_t hdr_type, pos;
220 unsigned count, static_count;
222 previous_unitid = last_unitid;
223 last_unitid = next_unitid;
225 /* Get setup the device_structure */
226 dev = ht_scan_get_devs(&old_devices);
231 dummy.path.type = DEVICE_PATH_PCI;
232 dummy.path.u.pci.devfn = 0;
233 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
234 /* If the chain is fully enumerated quit */
235 if (id == 0xffffffff || id == 0x00000000 ||
236 id == 0x0000ffff || id == 0xffff0000) {
239 dev = alloc_dev(bus, &dummy.path);
242 /* Add this device to the pci bus chain */
244 /* Run the magice enable/disable sequence for the device */
245 if (dev->ops && dev->ops->enable) {
246 dev->ops->enable(dev);
248 /* Now read the vendor and device id */
249 id = pci_read_config32(dev, PCI_VENDOR_ID);
251 /* Update the device chain tail */
252 for(func = dev; func; func = func->sibling) {
253 chain_last = &func->sibling;
256 /* Read the rest of the pci configuration information */
257 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
258 class = pci_read_config32(dev, PCI_CLASS_REVISION);
260 /* Store the interesting information in the device structure */
261 dev->vendor = id & 0xffff;
262 dev->device = (id >> 16) & 0xffff;
263 dev->hdr_type = hdr_type;
264 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
265 dev->class = class >> 8;
267 /* Find the hypertransport link capability */
268 pos = ht_lookup_slave_capability(dev);
270 printk_err("Hypertransport link capability not found");
274 /* Update the Unitid of the current device */
275 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
276 flags &= ~0x1f; /* mask out base Unit ID */
277 flags |= next_unitid & 0x1f;
278 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
280 /* Update the Unitd id in the device structure */
282 for(func = dev; func; func = func->sibling) {
283 func->path.u.pci.devfn += (next_unitid << 3);
284 static_count = (func->path.u.pci.devfn >> 3)
285 - (dev->path.u.pci.devfn >> 3) + 1;
288 /* Compute the number of unitids consumed */
289 count = (flags >> 5) & 0x1f; /* get unit count */
290 printk_spew("%s count: %04x static_count: %04x\n",
291 dev_path(dev), count, static_count);
292 if (count < static_count) {
293 count = static_count;
296 /* Update the Unitid of the next device */
297 next_unitid += count;
299 /* Setup the hypetransport link */
300 reset_needed |= ht_setup_link(&prev, dev, pos);
302 printk_debug("%s [%04x/%04x] %s next_unitid: %04x\n",
304 dev->vendor, dev->device,
305 (dev->enable? "enabled": "disabled"), next_unitid);
307 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
308 #if HAVE_HARD_RESET == 1
310 printk_info("HyperT reset needed\n");
313 printk_debug("HyperT reset not needed\n");
315 if (next_unitid > 0x1f) {
318 return pci_scan_bus(bus, 0x00, (next_unitid << 3)|7, max);