device/hypertransport.c: Remove dead assignment
[coreboot.git] / src / device / hypertransport.c
blob027c3ef1d4249a24b77e931cce4354464198a2a5
1 /*
2 * This file is part of the coreboot 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>
11 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 2 of the License.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 #include <console/console.h>
24 #include <device/device.h>
25 #include <device/path.h>
26 #include <device/pci.h>
27 #include <device/pci_ops.h>
28 #include <device/pci_ids.h>
29 #include <device/hypertransport.h>
31 struct ht_link {
32 struct device *dev;
33 unsigned pos;
34 unsigned char ctrl_off, config_off, freq_off, freq_cap_off;
37 static struct device *ht_scan_get_devs(struct device **old_devices)
39 struct device *first, *last;
41 first = *old_devices;
42 last = first;
45 * Extract the chain of devices to (first through last) for the next
46 * hypertransport device.
48 while (last && last->sibling &&
49 (last->sibling->path.type == DEVICE_PATH_PCI) &&
50 (last->sibling->path.pci.devfn > last->path.pci.devfn))
52 last = last->sibling;
55 if (first) {
56 struct device *child;
58 /* Unlink the chain from the list of old devices. */
59 *old_devices = last->sibling;
60 last->sibling = 0;
62 /* Now add the device to the list of devices on the bus. */
63 /* Find the last child of our parent. */
64 for (child = first->bus->children; child && child->sibling;)
65 child = child->sibling;
67 /* Place the chain on the list of children of their parent. */
68 if (child)
69 child->sibling = first;
70 else
71 first->bus->children = first;
73 return first;
76 static int ht_setup_link(struct ht_link *prev, struct device *dev, unsigned pos)
78 struct ht_link cur[1];
79 int linkb_to_host;
81 /* Set the hypertransport link width and frequency. */
84 * See which side of the device our previous write to set the unitid
85 * came from.
87 cur->dev = dev;
88 cur->pos = pos;
89 linkb_to_host =
90 (pci_read_config16(cur->dev, cur->pos + PCI_CAP_FLAGS) >> 10) & 1;
92 if (!linkb_to_host) {
93 cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0;
94 cur->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
95 cur->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
96 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
97 } else {
98 cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1;
99 cur->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
100 cur->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
101 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
105 * Remember the current link as the previous link, but look at the
106 * other offsets.
108 prev->dev = cur->dev;
109 prev->pos = cur->pos;
110 if (cur->ctrl_off == PCI_HT_CAP_SLAVE_CTRL0) {
111 prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1;
112 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
113 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
114 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
115 } else {
116 prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0;
117 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
118 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
119 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
122 return 0;
125 static unsigned ht_lookup_slave_capability(struct device *dev)
127 unsigned pos;
129 pos = 0;
130 do {
131 pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos);
132 if (pos) {
133 u16 flags;
134 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
135 printk(BIOS_SPEW, "flags: 0x%04x\n", flags);
136 if ((flags >> 13) == 0) {
137 /* Entry is a slave secondary, success... */
138 break;
141 } while (pos);
143 return pos;
146 static void ht_collapse_early_enumeration(struct bus *bus,
147 unsigned offset_unitid)
149 unsigned int devfn;
150 struct ht_link prev;
151 u16 ctrl;
153 /* Initialize the hypertransport enumeration state. */
154 prev.dev = bus->dev;
155 prev.pos = bus->cap;
156 prev.ctrl_off = PCI_HT_CAP_HOST_CTRL;
157 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
158 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
159 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
161 /* Wait until the link initialization is complete. */
162 do {
163 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
165 /* Is this the end of the hypertransport chain? */
166 if (ctrl & (1 << 6))
167 return;
169 /* Has the link failed? */
170 if (ctrl & (1 << 4)) {
172 * Either the link has failed, or we have a CRC error.
173 * Sometimes this can happen due to link retrain, so
174 * lets knock it down and see if its transient.
176 ctrl |= ((1 << 4) | (1 << 8)); /* Link fail + CRC */
177 pci_write_config16(prev.dev, prev.pos + prev.ctrl_off,
178 ctrl);
179 ctrl = pci_read_config16(prev.dev,
180 prev.pos + prev.ctrl_off);
181 if (ctrl & ((1 << 4) | (1 << 8))) {
182 printk(BIOS_ALERT, "Detected error on "
183 "Hypertransport link\n");
184 return;
187 } while ((ctrl & (1 << 5)) == 0);
189 /* Actually, only for one HT device HT chain, and unitid is 0. */
190 #if !CONFIG_HT_CHAIN_UNITID_BASE
191 if (offset_unitid)
192 return;
193 #endif
195 /* Check if is already collapsed. */
196 if ((!offset_unitid) || (offset_unitid
197 && (!((CONFIG_HT_CHAIN_END_UNITID_BASE == 0)
198 && (CONFIG_HT_CHAIN_END_UNITID_BASE
199 < CONFIG_HT_CHAIN_UNITID_BASE))))) {
201 struct device dummy;
202 u32 id;
204 dummy.bus = bus;
205 dummy.path.type = DEVICE_PATH_PCI;
206 dummy.path.pci.devfn = PCI_DEVFN(0, 0);
208 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
209 if (!((id == 0xffffffff) || (id == 0x00000000)
210 || (id == 0x0000ffff) || (id == 0xffff0000))) {
211 return;
215 /* Spin through the devices and collapse any early HT enumeration. */
216 for (devfn = PCI_DEVFN(1, 0); devfn <= 0xff; devfn += 8) {
217 struct device dummy;
218 u32 id;
219 unsigned pos, flags;
221 dummy.bus = bus;
222 dummy.path.type = DEVICE_PATH_PCI;
223 dummy.path.pci.devfn = devfn;
225 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
226 if ((id == 0xffffffff) || (id == 0x00000000)
227 || (id == 0x0000ffff) || (id == 0xffff0000)) {
228 continue;
231 dummy.vendor = id & 0xffff;
232 dummy.device = (id >> 16) & 0xffff;
233 dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
235 pos = ht_lookup_slave_capability(&dummy);
236 if (!pos)
237 continue;
239 /* Clear the unitid. */
240 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
241 flags &= ~0x1f;
242 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
243 printk(BIOS_SPEW, "Collapsing %s [%04x/%04x]\n",
244 dev_path(&dummy), dummy.vendor, dummy.device);
248 static unsigned int do_hypertransport_scan_chain(struct bus *bus, unsigned min_devfn,
249 unsigned max_devfn,
250 unsigned *ht_unitid_base,
251 unsigned offset_unitid)
254 * Even CONFIG_HT_CHAIN_UNITID_BASE == 0, we still can go through this
255 * function, because of end_of_chain check. Also, we need it to
256 * optimize link.
258 unsigned int next_unitid, last_unitid, min_unitid, max_unitid;
259 struct device *old_devices, *dev, *func, *last_func = NULL;
260 struct ht_link prev;
261 int ht_dev_num = 0;
263 printk(BIOS_SPEW, "%s for bus %02x\n", __func__, bus->secondary);
265 min_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE : 1;
267 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
269 * Let's record the device of last HT device, so we can set the unitid
270 * to CONFIG_HT_CHAIN_END_UNITID_BASE.
272 unsigned int real_last_unitid = 0, end_used = 0;
273 u8 real_last_pos = 0;
274 struct device *real_last_dev = NULL;
275 #endif
277 /* Restore the hypertransport chain to it's uninitialized state. */
278 ht_collapse_early_enumeration(bus, offset_unitid);
280 /* See which static device nodes I have. */
281 old_devices = bus->children;
282 bus->children = 0;
284 /* Initialize the hypertransport enumeration state. */
285 prev.dev = bus->dev;
286 prev.pos = bus->cap;
288 prev.ctrl_off = PCI_HT_CAP_HOST_CTRL;
289 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
290 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
291 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
293 /* If present, assign unitid to a hypertransport chain. */
294 max_unitid = next_unitid = min_unitid;
295 do {
296 u8 pos;
297 u16 flags, ctrl;
298 unsigned int count, static_count;
300 last_unitid = next_unitid;
302 /* Wait until the link initialization is complete. */
303 do {
304 ctrl = pci_read_config16(prev.dev,
305 prev.pos + prev.ctrl_off);
307 /* End of chain? */
308 if (ctrl & (1 << 6))
309 goto end_of_chain;
311 if (ctrl & ((1 << 4) | (1 << 8))) {
313 * Either the link has failed, or we have a CRC
314 * error. Sometimes this can happen due to link
315 * retrain, so lets knock it down and see if
316 * it's transient.
318 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + CRC
319 pci_write_config16(prev.dev,
320 prev.pos + prev.ctrl_off, ctrl);
321 ctrl = pci_read_config16(prev.dev,
322 prev.pos + prev.ctrl_off);
323 if (ctrl & ((1 << 4) | (1 << 8))) {
324 printk(BIOS_ALERT, "Detected error on "
325 "hypertransport link\n");
326 goto end_of_chain;
329 } while ((ctrl & (1 << 5)) == 0);
332 /* Get and setup the device_structure. */
333 dev = ht_scan_get_devs(&old_devices);
335 /* See if a device is present and setup the device structure. */
336 dev = pci_probe_dev(dev, bus, 0);
337 if (!dev || !dev->enabled)
338 break;
340 /* Find the hypertransport link capability. */
341 pos = ht_lookup_slave_capability(dev);
342 if (pos == 0) {
343 printk(BIOS_ERR, "%s Hypertransport link capability "
344 "not found", dev_path(dev));
345 break;
348 /* Update the unitid of the current device. */
349 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
352 * If the device has a unitid set and is at devfn 0 we are
353 * done. This can happen with shadow hypertransport devices,
354 * or if we have reached the bottom of a HT device chain.
356 if (flags & 0x1f)
357 break;
359 flags &= ~0x1f; /* Mask out base Unit ID. */
361 count = (flags >> 5) & 0x1f; /* Het unit count. */
363 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
364 if (offset_unitid) {
365 /* max_devfn will be (0x17<<3)|7 or (0x1f<<3)|7. */
366 if (next_unitid > (max_devfn >> 3)) {
367 if (!end_used) {
368 next_unitid =
369 CONFIG_HT_CHAIN_END_UNITID_BASE;
370 end_used = 1;
371 } else {
372 goto end_of_chain;
376 #endif
378 flags |= next_unitid & 0x1f;
379 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
381 /* Update the unitid in the device structure. */
382 static_count = 1;
383 for (func = dev; func; func = func->sibling) {
384 func->path.pci.devfn += (next_unitid << 3);
385 static_count = (func->path.pci.devfn >> 3)
386 - (dev->path.pci.devfn >> 3) + 1;
387 last_func = func;
390 /* Compute the number of unitids consumed. */
391 printk(BIOS_SPEW, "%s count: %04x static_count: %04x\n",
392 dev_path(dev), count, static_count);
393 if (count < static_count)
394 count = static_count;
396 /* Update the unitid of the next device. */
397 ht_unitid_base[ht_dev_num] = next_unitid;
398 ht_dev_num++;
400 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
401 if (offset_unitid) {
402 real_last_pos = pos;
403 real_last_unitid = next_unitid;
404 real_last_dev = dev;
406 #endif
407 next_unitid += count;
408 if (next_unitid > max_unitid)
409 max_unitid = next_unitid;
411 /* Setup the hypertransport link. */
412 bus->reset_needed |= ht_setup_link(&prev, dev, pos);
414 printk(BIOS_DEBUG, "%s [%04x/%04x] %s next_unitid: %04x\n",
415 dev_path(dev), dev->vendor, dev->device,
416 (dev->enabled? "enabled" : "disabled"), next_unitid);
418 } while (last_unitid != next_unitid);
420 end_of_chain:
422 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
423 if (offset_unitid && (ht_dev_num > 1)
424 && (real_last_unitid != CONFIG_HT_CHAIN_END_UNITID_BASE)
425 && !end_used) {
426 u16 flags;
427 flags = pci_read_config16(real_last_dev,
428 real_last_pos + PCI_CAP_FLAGS);
429 flags &= ~0x1f;
430 flags |= CONFIG_HT_CHAIN_END_UNITID_BASE & 0x1f;
431 pci_write_config16(real_last_dev,
432 real_last_pos + PCI_CAP_FLAGS, flags);
434 for (func = real_last_dev; func; func = func->sibling) {
435 func->path.pci.devfn -= ((real_last_unitid
436 - CONFIG_HT_CHAIN_END_UNITID_BASE) << 3);
437 last_func = func;
440 /* Update last one. */
441 ht_unitid_base[ht_dev_num-1] = CONFIG_HT_CHAIN_END_UNITID_BASE;
443 printk(BIOS_DEBUG, " unitid: %04x --> %04x\n",
444 real_last_unitid, CONFIG_HT_CHAIN_END_UNITID_BASE);
446 #endif
447 next_unitid = max_unitid;
449 if (next_unitid > 0x20)
450 next_unitid = 0x20;
452 if ((bus->secondary == 0) && (next_unitid > 0x18))
453 next_unitid = 0x18; /* Avoid K8 on bus 0. */
456 * Die if any leftover static devices are are found. There's probably
457 * a problem in devicetree.cb.
459 if (old_devices) {
460 struct device *left;
461 for (left = old_devices; left; left = left->sibling)
462 printk(BIOS_DEBUG, "%s\n", dev_path(left));
464 printk(BIOS_ERR, "HT: Leftover static devices. "
465 "Check your devicetree.cb\n");
468 * Put back the leftover static device, and let pci_scan_bus()
469 * disable it.
471 if (last_func && !last_func->sibling)
472 last_func->sibling = old_devices;
475 return next_unitid;
478 unsigned int hypertransport_scan_chain(struct bus *bus)
480 int i;
481 unsigned int max_devfn;
482 u32 ht_unitid_base[4];
484 for (i = 0; i < 4; i++)
485 ht_unitid_base[i] = 0x20;
487 if (bus->secondary == 0)
488 max_devfn = (CONFIG_CDB << 3) - 1;
489 else
490 max_devfn = (0x20 << 3) - 1;
492 unsigned int next_unitid = do_hypertransport_scan_chain(bus, 0, max_devfn,
493 ht_unitid_base, offset_unit_id(bus->secondary == 0));
495 bus->hcdn_reg = 0;
496 for (i = 0; i < 4; i++)
497 bus->hcdn_reg |= (ht_unitid_base[i] & 0xff) << (i*8);
499 return next_unitid;
503 * Scan a PCI bridge and the buses behind the bridge.
505 * Determine the existence of buses behind the bridge. Set up the bridge
506 * according to the result of the scan.
508 * This function is the default scan_bus() method for PCI bridge devices.
510 * @param bus TODO
511 * @param min_devfn TODO
512 * @param max_devfn TODO
514 static void hypertransport_scan_chain_x(struct bus *bus,
515 unsigned int min_devfn, unsigned int max_devfn)
517 unsigned int ht_unitid_base[4];
518 unsigned int offset_unitid = 1;
520 unsigned int next_unitid = do_hypertransport_scan_chain(bus, min_devfn, max_devfn,
521 ht_unitid_base, offset_unitid);
523 /* Now that nothing is overlapping it is safe to scan the children. */
524 pci_scan_bus(bus, 0x00, ((next_unitid - 1) << 3) | 7);
527 void ht_scan_bridge(struct device *dev)
529 do_pci_scan_bridge(dev, hypertransport_scan_chain_x);
532 bool ht_is_non_coherent_link(struct bus *link)
534 u32 link_type;
535 do {
536 link_type = pci_read_config32(link->dev, link->cap + 0x18);
537 } while (link_type & ConnectionPending);
539 if (!(link_type & LinkConnected))
540 return false;
542 do {
543 link_type = pci_read_config32(link->dev, link->cap + 0x18);
544 } while (!(link_type & InitComplete));
546 return !!(link_type & NonCoherent);
549 /** Default device operations for hypertransport bridges */
550 static struct pci_operations ht_bus_ops_pci = {
551 .set_subsystem = 0,
554 struct device_operations default_ht_ops_bus = {
555 .read_resources = pci_bus_read_resources,
556 .set_resources = pci_dev_set_resources,
557 .enable_resources = pci_bus_enable_resources,
558 .init = 0,
559 .scan_bus = ht_scan_bridge,
560 .enable = 0,
561 .reset_bus = pci_bus_reset,
562 .ops_pci = &ht_bus_ops_pci,