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.
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/path.h>
27 #include <device/pci.h>
28 #include <device/pci_ids.h>
29 #include <device/hypertransport.h>
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
;
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
))
58 /* Unlink the chain from the list of old devices. */
59 *old_devices
= last
->sibling
;
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. */
69 child
->sibling
= first
;
71 first
->bus
->children
= first
;
76 static int ht_setup_link(struct ht_link
*prev
, struct device
*dev
, unsigned pos
)
78 struct ht_link cur
[1];
81 /* Set the hypertransport link width and frequency. */
84 * See which side of the device our previous write to set the unitid
90 (pci_read_config16(cur
->dev
, cur
->pos
+ PCI_CAP_FLAGS
) >> 10) & 1;
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
;
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
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
;
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
;
125 static unsigned ht_lookup_slave_capability(struct device
*dev
)
131 pos
= pci_find_next_capability(dev
, PCI_CAP_ID_HT
, pos
);
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... */
146 static void ht_collapse_early_enumeration(struct bus
*bus
,
147 unsigned offset_unitid
)
153 /* Initialize the hypertransport enumeration state. */
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. */
163 ctrl
= pci_read_config16(prev
.dev
, prev
.pos
+ prev
.ctrl_off
);
165 /* Is this the end of the hypertransport chain? */
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
,
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");
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
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
))))) {
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))) {
215 /* Spin through the devices and collapse any early HT enumeration. */
216 for (devfn
= PCI_DEVFN(1, 0); devfn
<= 0xff; devfn
+= 8) {
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)) {
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
);
239 /* Clear the unitid. */
240 flags
= pci_read_config16(&dummy
, pos
+ PCI_CAP_FLAGS
);
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
,
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
258 unsigned int next_unitid
, last_unitid
, min_unitid
, max_unitid
;
259 struct device
*old_devices
, *dev
, *func
, *last_func
= NULL
;
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
;
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
;
284 /* Initialize the hypertransport enumeration state. */
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 last_unitid
= min_unitid
-1;
295 max_unitid
= next_unitid
= min_unitid
;
299 unsigned int count
, static_count
;
301 last_unitid
= next_unitid
;
303 /* Wait until the link initialization is complete. */
305 ctrl
= pci_read_config16(prev
.dev
,
306 prev
.pos
+ prev
.ctrl_off
);
312 if (ctrl
& ((1 << 4) | (1 << 8))) {
314 * Either the link has failed, or we have a CRC
315 * error. Sometimes this can happen due to link
316 * retrain, so lets knock it down and see if
319 ctrl
|= ((1 << 4) | (1 <<8)); // Link fail + CRC
320 pci_write_config16(prev
.dev
,
321 prev
.pos
+ prev
.ctrl_off
, ctrl
);
322 ctrl
= pci_read_config16(prev
.dev
,
323 prev
.pos
+ prev
.ctrl_off
);
324 if (ctrl
& ((1 << 4) | (1 << 8))) {
325 printk(BIOS_ALERT
, "Detected error on "
326 "hypertransport link\n");
330 } while ((ctrl
& (1 << 5)) == 0);
333 /* Get and setup the device_structure. */
334 dev
= ht_scan_get_devs(&old_devices
);
336 /* See if a device is present and setup the device structure. */
337 dev
= pci_probe_dev(dev
, bus
, 0);
338 if (!dev
|| !dev
->enabled
)
341 /* Find the hypertransport link capability. */
342 pos
= ht_lookup_slave_capability(dev
);
344 printk(BIOS_ERR
, "%s Hypertransport link capability "
345 "not found", dev_path(dev
));
349 /* Update the unitid of the current device. */
350 flags
= pci_read_config16(dev
, pos
+ PCI_CAP_FLAGS
);
353 * If the device has a unitid set and is at devfn 0 we are
354 * done. This can happen with shadow hypertransport devices,
355 * or if we have reached the bottom of a HT device chain.
360 flags
&= ~0x1f; /* Mask out base Unit ID. */
362 count
= (flags
>> 5) & 0x1f; /* Het unit count. */
364 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
366 /* max_devfn will be (0x17<<3)|7 or (0x1f<<3)|7. */
367 if (next_unitid
> (max_devfn
>> 3)) {
370 CONFIG_HT_CHAIN_END_UNITID_BASE
;
379 flags
|= next_unitid
& 0x1f;
380 pci_write_config16(dev
, pos
+ PCI_CAP_FLAGS
, flags
);
382 /* Update the unitid in the device structure. */
384 for (func
= dev
; func
; func
= func
->sibling
) {
385 func
->path
.pci
.devfn
+= (next_unitid
<< 3);
386 static_count
= (func
->path
.pci
.devfn
>> 3)
387 - (dev
->path
.pci
.devfn
>> 3) + 1;
391 /* Compute the number of unitids consumed. */
392 printk(BIOS_SPEW
, "%s count: %04x static_count: %04x\n",
393 dev_path(dev
), count
, static_count
);
394 if (count
< static_count
)
395 count
= static_count
;
397 /* Update the unitid of the next device. */
398 ht_unitid_base
[ht_dev_num
] = next_unitid
;
401 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
404 real_last_unitid
= next_unitid
;
408 next_unitid
+= count
;
409 if (next_unitid
> max_unitid
)
410 max_unitid
= next_unitid
;
412 /* Setup the hypertransport link. */
413 bus
->reset_needed
|= ht_setup_link(&prev
, dev
, pos
);
415 printk(BIOS_DEBUG
, "%s [%04x/%04x] %s next_unitid: %04x\n",
416 dev_path(dev
), dev
->vendor
, dev
->device
,
417 (dev
->enabled
? "enabled" : "disabled"), next_unitid
);
419 } while (last_unitid
!= next_unitid
);
423 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
424 if (offset_unitid
&& (ht_dev_num
> 1)
425 && (real_last_unitid
!= CONFIG_HT_CHAIN_END_UNITID_BASE
)
428 flags
= pci_read_config16(real_last_dev
,
429 real_last_pos
+ PCI_CAP_FLAGS
);
431 flags
|= CONFIG_HT_CHAIN_END_UNITID_BASE
& 0x1f;
432 pci_write_config16(real_last_dev
,
433 real_last_pos
+ PCI_CAP_FLAGS
, flags
);
435 for (func
= real_last_dev
; func
; func
= func
->sibling
) {
436 func
->path
.pci
.devfn
-= ((real_last_unitid
437 - CONFIG_HT_CHAIN_END_UNITID_BASE
) << 3);
441 /* Update last one. */
442 ht_unitid_base
[ht_dev_num
-1] = CONFIG_HT_CHAIN_END_UNITID_BASE
;
444 printk(BIOS_DEBUG
, " unitid: %04x --> %04x\n",
445 real_last_unitid
, CONFIG_HT_CHAIN_END_UNITID_BASE
);
448 next_unitid
= max_unitid
;
450 if (next_unitid
> 0x20)
453 if ((bus
->secondary
== 0) && (next_unitid
> 0x18))
454 next_unitid
= 0x18; /* Avoid K8 on bus 0. */
457 * Die if any leftover static devices are are found. There's probably
458 * a problem in devicetree.cb.
462 for (left
= old_devices
; left
; left
= left
->sibling
)
463 printk(BIOS_DEBUG
, "%s\n", dev_path(left
));
465 printk(BIOS_ERR
, "HT: Leftover static devices. "
466 "Check your devicetree.cb\n");
469 * Put back the leftover static device, and let pci_scan_bus()
472 if (last_func
&& !last_func
->sibling
)
473 last_func
->sibling
= old_devices
;
479 unsigned int hypertransport_scan_chain(struct bus
*bus
)
482 unsigned int max_devfn
;
483 u32 ht_unitid_base
[4];
485 for (i
= 0; i
< 4; i
++)
486 ht_unitid_base
[i
] = 0x20;
488 if (bus
->secondary
== 0)
489 max_devfn
= (CONFIG_CDB
<< 3) - 1;
491 max_devfn
= (0x20 << 3) - 1;
493 unsigned int next_unitid
= do_hypertransport_scan_chain(bus
, 0, max_devfn
,
494 ht_unitid_base
, offset_unit_id(bus
->secondary
== 0));
497 for (i
= 0; i
< 4; i
++)
498 bus
->hcdn_reg
|= (ht_unitid_base
[i
] & 0xff) << (i
*8);
504 * Scan a PCI bridge and the buses behind the bridge.
506 * Determine the existence of buses behind the bridge. Set up the bridge
507 * according to the result of the scan.
509 * This function is the default scan_bus() method for PCI bridge devices.
512 * @param min_devfn TODO
513 * @param max_devfn TODO
515 static void hypertransport_scan_chain_x(struct bus
*bus
,
516 unsigned int min_devfn
, unsigned int max_devfn
)
518 unsigned int ht_unitid_base
[4];
519 unsigned int offset_unitid
= 1;
521 unsigned int next_unitid
= do_hypertransport_scan_chain(bus
, min_devfn
, max_devfn
,
522 ht_unitid_base
, offset_unitid
);
524 /* Now that nothing is overlapping it is safe to scan the children. */
525 pci_scan_bus(bus
, 0x00, ((next_unitid
- 1) << 3) | 7);
528 void ht_scan_bridge(struct device
*dev
)
530 do_pci_scan_bridge(dev
, hypertransport_scan_chain_x
);
533 bool ht_is_non_coherent_link(struct bus
*link
)
537 link_type
= pci_read_config32(link
->dev
, link
->cap
+ 0x18);
538 } while (link_type
& ConnectionPending
);
540 if (!(link_type
& LinkConnected
))
544 link_type
= pci_read_config32(link
->dev
, link
->cap
+ 0x18);
545 } while (!(link_type
& InitComplete
));
547 return !!(link_type
& NonCoherent
);
550 /** Default device operations for hypertransport bridges */
551 static struct pci_operations ht_bus_ops_pci
= {
555 struct device_operations default_ht_ops_bus
= {
556 .read_resources
= pci_bus_read_resources
,
557 .set_resources
= pci_dev_set_resources
,
558 .enable_resources
= pci_bus_enable_resources
,
560 .scan_bus
= ht_scan_bridge
,
562 .reset_bus
= pci_bus_reset
,
563 .ops_pci
= &ht_bus_ops_pci
,