soc/intel/common/block: Move common uart function to block/uart
[coreboot.git] / src / device / hypertransport.c
blob75e18208239e0fd777d0062b289ed01e48953b6a
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 <lib.h>
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>
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 last_unitid = min_unitid -1;
295 max_unitid = next_unitid = min_unitid;
296 do {
297 u8 pos;
298 u16 flags, ctrl;
299 unsigned int count, static_count;
301 last_unitid = next_unitid;
303 /* Wait until the link initialization is complete. */
304 do {
305 ctrl = pci_read_config16(prev.dev,
306 prev.pos + prev.ctrl_off);
308 /* End of chain? */
309 if (ctrl & (1 << 6))
310 goto end_of_chain;
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
317 * it's transient.
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");
327 goto end_of_chain;
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)
339 break;
341 /* Find the hypertransport link capability. */
342 pos = ht_lookup_slave_capability(dev);
343 if (pos == 0) {
344 printk(BIOS_ERR, "%s Hypertransport link capability "
345 "not found", dev_path(dev));
346 break;
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.
357 if (flags & 0x1f)
358 break;
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
365 if (offset_unitid) {
366 /* max_devfn will be (0x17<<3)|7 or (0x1f<<3)|7. */
367 if (next_unitid > (max_devfn >> 3)) {
368 if (!end_used) {
369 next_unitid =
370 CONFIG_HT_CHAIN_END_UNITID_BASE;
371 end_used = 1;
372 } else {
373 goto end_of_chain;
377 #endif
379 flags |= next_unitid & 0x1f;
380 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
382 /* Update the unitid in the device structure. */
383 static_count = 1;
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;
388 last_func = func;
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;
399 ht_dev_num++;
401 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
402 if (offset_unitid) {
403 real_last_pos = pos;
404 real_last_unitid = next_unitid;
405 real_last_dev = dev;
407 #endif
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);
421 end_of_chain:
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)
426 && !end_used) {
427 u16 flags;
428 flags = pci_read_config16(real_last_dev,
429 real_last_pos + PCI_CAP_FLAGS);
430 flags &= ~0x1f;
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);
438 last_func = func;
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);
447 #endif
448 next_unitid = max_unitid;
450 if (next_unitid > 0x20)
451 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.
460 if (old_devices) {
461 struct device *left;
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()
470 * disable it.
472 if (last_func && !last_func->sibling)
473 last_func->sibling = old_devices;
476 return next_unitid;
479 unsigned int hypertransport_scan_chain(struct bus *bus)
481 int i;
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;
490 else
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));
496 bus->hcdn_reg = 0;
497 for (i = 0; i < 4; i++)
498 bus->hcdn_reg |= (ht_unitid_base[i] & 0xff) << (i*8);
500 return next_unitid;
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.
511 * @param bus TODO
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)
535 u32 link_type;
536 do {
537 link_type = pci_read_config32(link->dev, link->cap + 0x18);
538 } while (link_type & ConnectionPending);
540 if (!(link_type & LinkConnected))
541 return false;
543 do {
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 = {
552 .set_subsystem = 0,
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,
559 .init = 0,
560 .scan_bus = ht_scan_bridge,
561 .enable = 0,
562 .reset_bus = pci_bus_reset,
563 .ops_pci = &ht_bus_ops_pci,