blob: bb912499082fe2dc00e6689f6a2a301087cc01ac [file] [log] [blame]
Stefan Reinauer7ce8c542005-12-02 21:52:30 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
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 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000025
Uwe Hermannb80dbf02007-04-22 19:08:13 +000026/*
27 2005.11 yhlu add let the real sb to use small uintid
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000028*/
29
Eric Biederman0ac6b412003-09-02 17:16:48 +000030#include <bitops.h>
31#include <console/console.h>
32#include <device/device.h>
33#include <device/path.h>
34#include <device/pci.h>
Eric Biederman5cd81732004-03-11 15:01:31 +000035#include <device/pci_ids.h>
Eric Biedermanff0e8462003-09-04 03:00:54 +000036#include <device/hypertransport.h>
Eric Biederman0ac6b412003-09-02 17:16:48 +000037
Stefan Reinauer0dff6e32007-10-23 22:17:45 +000038/* The hypertransport link is already optimized in pre-ram code
39 * so don't do it again
40 */
Yinghai Lu90b3e092005-01-26 22:00:20 +000041#define OPT_HT_LINK 0
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000042
arch import user (historical)ef03afa2005-07-06 17:15:30 +000043#if OPT_HT_LINK == 1
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000044#include <cpu/amd/model_fxx_rev.h>
arch import user (historical)ef03afa2005-07-06 17:15:30 +000045#endif
46
Eric Biederman0ac6b412003-09-02 17:16:48 +000047static device_t ht_scan_get_devs(device_t *old_devices)
48{
49 device_t first, last;
50 first = *old_devices;
51 last = first;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000052 /* Extract the chain of devices to (first through last)
53 * for the next hypertransport device.
54 */
Eric Biederman03acab62004-10-14 21:25:53 +000055 while(last && last->sibling &&
56 (last->sibling->path.type == DEVICE_PATH_PCI) &&
Stefan Reinauer2b34db82009-02-28 20:10:20 +000057 (last->sibling->path.pci.devfn > last->path.pci.devfn))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000058 {
Eric Biederman0ac6b412003-09-02 17:16:48 +000059 last = last->sibling;
60 }
61 if (first) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000062 device_t child;
63 /* Unlink the chain from the list of old devices */
Eric Biederman0ac6b412003-09-02 17:16:48 +000064 *old_devices = last->sibling;
65 last->sibling = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000066
67 /* Now add the device to the list of devices on the bus.
68 */
69 /* Find the last child of our parent */
70 for(child = first->bus->children; child && child->sibling; ) {
71 child = child->sibling;
72 }
73 /* Place the chain on the list of children of their parent. */
74 if (child) {
75 child->sibling = first;
76 } else {
77 first->bus->children = first;
78 }
Eric Biederman0ac6b412003-09-02 17:16:48 +000079 }
80 return first;
81}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000082
Yinghai Lu90b3e092005-01-26 22:00:20 +000083#if OPT_HT_LINK == 1
Eric Biederman5cd81732004-03-11 15:01:31 +000084static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
85{
86 /* Handle bugs in valid hypertransport frequency reporting */
87 unsigned freq_cap;
88
89 freq_cap = pci_read_config16(dev, pos);
90 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
91
92 /* AMD 8131 Errata 48 */
93 if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
94 (dev->device == PCI_DEVICE_ID_AMD_8131_PCIX)) {
95 freq_cap &= ~(1 << HT_FREQ_800Mhz);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000096 }
Eric Biederman5cd81732004-03-11 15:01:31 +000097 /* AMD 8151 Errata 23 */
98 if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
99 (dev->device == PCI_DEVICE_ID_AMD_8151_SYSCTRL)) {
100 freq_cap &= ~(1 << HT_FREQ_800Mhz);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000101 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000102 /* AMD K8 Unsupported 1Ghz? */
103 if ((dev->vendor == PCI_VENDOR_ID_AMD) && (dev->device == 0x1100)) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000104#if CONFIG_K8_HT_FREQ_1G_SUPPORT == 1
105 #if CONFIG_K8_REV_F_SUPPORT == 0
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000106 if (is_cpu_pre_e0()) { // only e0 later suupport 1GHz HT
Yinghai Lu90b3e092005-01-26 22:00:20 +0000107 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000108 }
Yinghai Lud4b278c2006-10-04 20:46:15 +0000109 #endif
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000110#else
111 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
112#endif
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000113
Eric Biederman5cd81732004-03-11 15:01:31 +0000114 }
115 return freq_cap;
116}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000117#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000118
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000119struct ht_link {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000120 struct device *dev;
121 unsigned pos;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000122 unsigned char ctrl_off, config_off, freq_off, freq_cap_off;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000123};
124
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000125static int ht_setup_link(struct ht_link *prev, device_t dev, unsigned pos)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000126{
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000127#if OPT_HT_LINK == 1
Eric Biederman0ac6b412003-09-02 17:16:48 +0000128 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
129 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
130 unsigned present_width_cap, upstream_width_cap;
131 unsigned present_freq_cap, upstream_freq_cap;
132 unsigned ln_present_width_in, ln_upstream_width_in;
133 unsigned ln_present_width_out, ln_upstream_width_out;
134 unsigned freq, old_freq;
135 unsigned present_width, upstream_width, old_width;
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000136#endif
137 struct ht_link cur[1];
Eric Biederman0ac6b412003-09-02 17:16:48 +0000138 int reset_needed;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000139 int linkb_to_host;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000140
141 /* Set the hypertransport link width and frequency */
142 reset_needed = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000143 /* See which side of the device our previous write to
144 * set the unitid came from.
145 */
146 cur->dev = dev;
147 cur->pos = pos;
148 linkb_to_host = (pci_read_config16(cur->dev, cur->pos + PCI_CAP_FLAGS) >> 10) & 1;
149 if (!linkb_to_host) {
150 cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0;
151 cur->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
152 cur->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
153 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
154 }
155 else {
156 cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1;
157 cur->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
158 cur->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
159 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
160 }
161#if OPT_HT_LINK == 1
Eric Biederman0ac6b412003-09-02 17:16:48 +0000162 /* Read the capabilities */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000163 present_freq_cap = ht_read_freq_cap(cur->dev, cur->pos + cur->freq_cap_off);
Eric Biederman5cd81732004-03-11 15:01:31 +0000164 upstream_freq_cap = ht_read_freq_cap(prev->dev, prev->pos + prev->freq_cap_off);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000165 present_width_cap = pci_read_config8(cur->dev, cur->pos + cur->config_off);
166 upstream_width_cap = pci_read_config8(prev->dev, prev->pos + prev->config_off);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000167
168 /* Calculate the highest useable frequency */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000169 freq = log2(present_freq_cap & upstream_freq_cap);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000170
171 /* Calculate the highest width */
172 ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
173 ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
174 if (ln_upstream_width_in > ln_present_width_out) {
175 ln_upstream_width_in = ln_present_width_out;
176 }
177 upstream_width = pow2_to_link_width[ln_upstream_width_in];
178 present_width = pow2_to_link_width[ln_upstream_width_in] << 4;
179
180 ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7];
181 ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
182 if (ln_upstream_width_out > ln_present_width_in) {
183 ln_upstream_width_out = ln_present_width_in;
184 }
185 upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
186 present_width |= pow2_to_link_width[ln_upstream_width_out];
187
188 /* Set the current device */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000189 old_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off);
190 old_freq &= 0x0f;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000191 if (freq != old_freq) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000192 unsigned new_freq;
193 pci_write_config8(cur->dev, cur->pos + cur->freq_off, freq);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000194 reset_needed = 1;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000195 printk(BIOS_SPEW, "HyperT FreqP old %x new %x\n",old_freq,freq);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000196 new_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off);
197 new_freq &= 0x0f;
198 if (new_freq != freq) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000199 printk(BIOS_ERR, "%s Hypertransport frequency would not set wanted: %x got: %x\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000200 dev_path(dev), freq, new_freq);
201 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000202 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000203 old_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000204 if (present_width != old_width) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000205 unsigned new_width;
206 pci_write_config8(cur->dev, cur->pos + cur->config_off + 1,
207 present_width);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000208 reset_needed = 1;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000209 printk(BIOS_SPEW, "HyperT widthP old %x new %x\n",old_width, present_width);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000210 new_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1);
211 if (new_width != present_width) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000212 printk(BIOS_ERR, "%s Hypertransport width would not set wanted: %x got: %x\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000213 dev_path(dev), present_width, new_width);
214 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000215 }
216
217 /* Set the upstream device */
218 old_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
219 old_freq &= 0x0f;
220 if (freq != old_freq) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000221 unsigned new_freq;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000222 pci_write_config8(prev->dev, prev->pos + prev->freq_off, freq);
223 reset_needed = 1;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000224 printk(BIOS_SPEW, "HyperT freqU old %x new %x\n", old_freq, freq);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000225 new_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
226 new_freq &= 0x0f;
227 if (new_freq != freq) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000228 printk(BIOS_ERR, "%s Hypertransport frequency would not set wanted: %x got: %x\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000229 dev_path(prev->dev), freq, new_freq);
230 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000231 }
232 old_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
233 if (upstream_width != old_width) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000234 unsigned new_width;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000235 pci_write_config8(prev->dev, prev->pos + prev->config_off + 1, upstream_width);
236 reset_needed = 1;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000237 printk(BIOS_SPEW, "HyperT widthU old %x new %x\n", old_width, upstream_width);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000238 new_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
239 if (new_width != upstream_width) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000240 printk(BIOS_ERR, "%s Hypertransport width would not set wanted: %x got: %x\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000241 dev_path(prev->dev), upstream_width, new_width);
242 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000243 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000244#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000245
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000246 /* Remember the current link as the previous link,
247 * But look at the other offsets.
248 */
249 prev->dev = cur->dev;
250 prev->pos = cur->pos;
251 if (cur->ctrl_off == PCI_HT_CAP_SLAVE_CTRL0) {
252 prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1;
253 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH1;
254 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ1;
255 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
256 } else {
257 prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0;
258 prev->config_off = PCI_HT_CAP_SLAVE_WIDTH0;
259 prev->freq_off = PCI_HT_CAP_SLAVE_FREQ0;
260 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
261 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000262
263 return reset_needed;
264
265}
266
267static unsigned ht_lookup_slave_capability(struct device *dev)
268{
269 unsigned pos;
270 pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000271 do {
272 pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos);
273 if (pos) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000274 unsigned flags;
275 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000276 printk(BIOS_SPEW, "flags: 0x%04x\n", flags);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000277 if ((flags >> 13) == 0) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000278 /* Entry is a Slave secondary, success... */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000279 break;
280 }
281 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000282 } while(pos);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000283 return pos;
284}
285
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000286static void ht_collapse_early_enumeration(struct bus *bus, unsigned offset_unitid)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000287{
288 unsigned int devfn;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000289 struct ht_link prev;
290 unsigned ctrl;
291
292 /* Initialize the hypertransport enumeration state */
293 prev.dev = bus->dev;
294 prev.pos = bus->cap;
295 prev.ctrl_off = PCI_HT_CAP_HOST_CTRL;
296 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
297 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
298 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
299
300 /* Wait until the link initialization is complete */
301 do {
302 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
303 /* Is this the end of the hypertransport chain */
304 if (ctrl & (1 << 6)) {
305 return;
306 }
307 /* Has the link failed? */
308 if (ctrl & (1 << 4)) {
Stefan Reinauercf648c92006-04-11 19:23:57 +0000309 /*
310 * Either the link has failed, or we have
311 * a CRC error.
312 * Sometimes this can happen due to link
313 * retrain, so lets knock it down and see
314 * if its transient
315 */
316 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
317 pci_write_config16(prev.dev, prev.pos + prev.ctrl_off, ctrl);
318 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
319 if (ctrl & ((1 << 4) | (1 << 8))) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000320 printk(BIOS_ALERT, "Detected error on Hypertransport Link\n");
Stefan Reinauercf648c92006-04-11 19:23:57 +0000321 return;
322 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000323 }
324 } while((ctrl & (1 << 5)) == 0);
325
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000326 //actually, only for one HT device HT chain, and unitid is 0
Stefan Reinauer08670622009-06-30 15:17:49 +0000327#if CONFIG_HT_CHAIN_UNITID_BASE == 0
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000328 if(offset_unitid) {
329 return;
330 }
331#endif
332
333 /* Check if is already collapsed */
Stefan Reinauer08670622009-06-30 15:17:49 +0000334 if((!offset_unitid)|| (offset_unitid && (!((CONFIG_HT_CHAIN_END_UNITID_BASE == 0) && (CONFIG_HT_CHAIN_END_UNITID_BASE <CONFIG_HT_CHAIN_UNITID_BASE))))) {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000335 struct device dummy;
336 uint32_t id;
337 dummy.bus = bus;
338 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000339 dummy.path.pci.devfn = PCI_DEVFN(0, 0);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000340 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
341 if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
342 (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
343 return;
344 }
345 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000346
347 /* Spin through the devices and collapse any early
348 * hypertransport enumeration.
349 */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000350 for(devfn = PCI_DEVFN(1, 0); devfn <= 0xff; devfn += 8) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000351 struct device dummy;
352 uint32_t id;
353 unsigned pos, flags;
354 dummy.bus = bus;
355 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000356 dummy.path.pci.devfn = devfn;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000357 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000358 if ( (id == 0xffffffff) || (id == 0x00000000) ||
359 (id == 0x0000ffff) || (id == 0xffff0000)) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000360 continue;
361 }
362 dummy.vendor = id & 0xffff;
363 dummy.device = (id >> 16) & 0xffff;
364 dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
365 pos = ht_lookup_slave_capability(&dummy);
366 if (!pos){
367 continue;
368 }
369
370 /* Clear the unitid */
371 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
372 flags &= ~0x1f;
373 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000374 printk(BIOS_SPEW, "Collapsing %s [%04x/%04x]\n",
Eric Biederman0ac6b412003-09-02 17:16:48 +0000375 dev_path(&dummy), dummy.vendor, dummy.device);
376 }
377}
378
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000379unsigned int hypertransport_scan_chain(struct bus *bus,
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000380 unsigned min_devfn, unsigned max_devfn, unsigned int max, unsigned *ht_unitid_base, unsigned offset_unitid)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000381{
Stefan Reinauer08670622009-06-30 15:17:49 +0000382 //even CONFIG_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
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000383 unsigned next_unitid, last_unitid;
384 device_t old_devices, dev, func;
Stefan Reinauer08670622009-06-30 15:17:49 +0000385 unsigned min_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE:1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000386 struct ht_link prev;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000387 device_t last_func = 0;
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000388 int ht_dev_num = 0;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000389 unsigned max_unitid;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000390
Stefan Reinauer08670622009-06-30 15:17:49 +0000391#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
392 //let't record the device of last ht device, So we can set the Unitid to CONFIG_HT_CHAIN_END_UNITID_BASE
Myles Watson6e235762009-09-29 14:56:15 +0000393 unsigned real_last_unitid=0;
394 uint8_t real_last_pos=0;
395 device_t real_last_dev=NULL;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000396 unsigned end_used = 0;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000397#endif
Eric Biederman0ac6b412003-09-02 17:16:48 +0000398
399 /* Restore the hypertransport chain to it's unitialized state */
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000400 ht_collapse_early_enumeration(bus, offset_unitid);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000401
402 /* See which static device nodes I have */
403 old_devices = bus->children;
404 bus->children = 0;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000405
406 /* Initialize the hypertransport enumeration state */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000407 prev.dev = bus->dev;
408 prev.pos = bus->cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000409 prev.ctrl_off = PCI_HT_CAP_HOST_CTRL;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000410 prev.config_off = PCI_HT_CAP_HOST_WIDTH;
411 prev.freq_off = PCI_HT_CAP_HOST_FREQ;
412 prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
413
414 /* If present assign unitid to a hypertransport chain */
415 last_unitid = min_unitid -1;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000416 max_unitid = next_unitid = min_unitid;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000417 do {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000418 uint8_t pos;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000419 uint16_t flags;
420 unsigned count, static_count;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000421 unsigned ctrl;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000422
Eric Biederman0ac6b412003-09-02 17:16:48 +0000423 last_unitid = next_unitid;
424
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000425 /* Wait until the link initialization is complete */
426 do {
427 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
Stefan Reinauercf648c92006-04-11 19:23:57 +0000428
429 if (ctrl & (1 << 6))
430 goto end_of_chain; // End of chain
431
432 if (ctrl & ((1 << 4) | (1 << 8))) {
433 /*
434 * Either the link has failed, or we have
435 * a CRC error.
436 * Sometimes this can happen due to link
437 * retrain, so lets knock it down and see
438 * if its transient
439 */
440 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
441 pci_write_config16(prev.dev, prev.pos + prev.ctrl_off, ctrl);
442 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
443 if (ctrl & ((1 << 4) | (1 << 8))) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000444 printk(BIOS_ALERT, "Detected error on Hypertransport Link\n");
Stefan Reinauercf648c92006-04-11 19:23:57 +0000445 goto end_of_chain;
446 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000447 }
448 } while((ctrl & (1 << 5)) == 0);
449
450
451 /* Get and setup the device_structure */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000452 dev = ht_scan_get_devs(&old_devices);
453
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000454 /* See if a device is present and setup the
455 * device structure.
456 */
457 dev = pci_probe_dev(dev, bus, 0);
458 if (!dev || !dev->enabled) {
459 break;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000460 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000461
462 /* Find the hypertransport link capability */
463 pos = ht_lookup_slave_capability(dev);
464 if (pos == 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000465 printk(BIOS_ERR, "%s Hypertransport link capability not found",
Eric Biederman83b991a2003-10-11 06:20:25 +0000466 dev_path(dev));
Eric Biederman0ac6b412003-09-02 17:16:48 +0000467 break;
468 }
469
470 /* Update the Unitid of the current device */
471 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000472
473 /* If the devices has a unitid set and is at devfn 0 we are done.
474 * This can happen with shadow hypertransport devices,
475 * or if we have reached the bottom of a
476 * hypertransport device chain.
477 */
478 if (flags & 0x1f) {
479 break;
480 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000481 flags &= ~0x1f; /* mask out base Unit ID */
Yinghai Luecad5df2007-05-21 18:11:17 +0000482
483 count = (flags >> 5) & 0x1f; /* get unit count */
Stefan Reinauer08670622009-06-30 15:17:49 +0000484#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
Yinghai Luecad5df2007-05-21 18:11:17 +0000485 if(offset_unitid) {
Yinghai Lu18c70d72007-09-14 14:58:33 +0000486 if(next_unitid > (max_devfn>>3)) { // max_devfn will be (0x17<<3)|7 or (0x1f<<3)|7
487 if(!end_used) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000488 next_unitid = CONFIG_HT_CHAIN_END_UNITID_BASE;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000489 end_used = 1;
490 } else {
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000491 goto end_of_chain;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000492 }
Yinghai Luecad5df2007-05-21 18:11:17 +0000493 }
494
495 }
496#endif
497
Yinghai Lu18c70d72007-09-14 14:58:33 +0000498 flags |= next_unitid & 0x1f;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000499 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000500
501 /* Update the Unitd id in the device structure */
502 static_count = 1;
503 for(func = dev; func; func = func->sibling) {
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000504 func->path.pci.devfn += (next_unitid << 3);
505 static_count = (func->path.pci.devfn >> 3)
506 - (dev->path.pci.devfn >> 3) + 1;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000507 last_func = func;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000508 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000509 /* Compute the number of unitids consumed */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000510 printk(BIOS_SPEW, "%s count: %04x static_count: %04x\n",
Eric Biederman0ac6b412003-09-02 17:16:48 +0000511 dev_path(dev), count, static_count);
512 if (count < static_count) {
513 count = static_count;
514 }
515
516 /* Update the Unitid of the next device */
Yinghai Lu18c70d72007-09-14 14:58:33 +0000517 ht_unitid_base[ht_dev_num] = next_unitid;
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000518 ht_dev_num++;
Yinghai Luecad5df2007-05-21 18:11:17 +0000519
Stefan Reinauer08670622009-06-30 15:17:49 +0000520#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
Yinghai Lu18c70d72007-09-14 14:58:33 +0000521 if (offset_unitid) {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000522 real_last_pos = pos;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000523 real_last_unitid = next_unitid;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000524 real_last_dev = dev;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000525 }
526#endif
Yinghai Lu18c70d72007-09-14 14:58:33 +0000527 next_unitid += count;
528 if (next_unitid > max_unitid) {
529 max_unitid = next_unitid;
530 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000531
532 /* Setup the hypetransport link */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000533 bus->reset_needed |= ht_setup_link(&prev, dev, pos);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000534
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000535 printk(BIOS_DEBUG, "%s [%04x/%04x] %s next_unitid: %04x\n",
Eric Biederman0ac6b412003-09-02 17:16:48 +0000536 dev_path(dev),
537 dev->vendor, dev->device,
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000538 (dev->enabled? "enabled": "disabled"), next_unitid);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000539
Yinghai Lu18c70d72007-09-14 14:58:33 +0000540 } while (last_unitid != next_unitid);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000541 end_of_chain:
Yinghai Lu90b3e092005-01-26 22:00:20 +0000542#if OPT_HT_LINK == 1
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000543 if(bus->reset_needed) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000544 printk(BIOS_INFO, "HyperT reset needed\n");
Eric Biederman5cd81732004-03-11 15:01:31 +0000545 }
546 else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000547 printk(BIOS_DEBUG, "HyperT reset not needed\n");
Eric Biederman5cd81732004-03-11 15:01:31 +0000548 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000549#endif
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000550
Stefan Reinauer08670622009-06-30 15:17:49 +0000551#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
552 if(offset_unitid && (ht_dev_num>1) && (real_last_unitid != CONFIG_HT_CHAIN_END_UNITID_BASE) && !end_used) {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000553 uint16_t flags;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000554 flags = pci_read_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS);
555 flags &= ~0x1f;
Stefan Reinauer08670622009-06-30 15:17:49 +0000556 flags |= CONFIG_HT_CHAIN_END_UNITID_BASE & 0x1f;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000557 pci_write_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS, flags);
558
559 for(func = real_last_dev; func; func = func->sibling) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000560 func->path.pci.devfn -= ((real_last_unitid - CONFIG_HT_CHAIN_END_UNITID_BASE) << 3);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000561 last_func = func;
562 }
Yinghai Luecad5df2007-05-21 18:11:17 +0000563
Stefan Reinauer08670622009-06-30 15:17:49 +0000564 ht_unitid_base[ht_dev_num-1] = CONFIG_HT_CHAIN_END_UNITID_BASE; // update last one
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000565
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000566 printk(BIOS_DEBUG, " unitid: %04x --> %04x\n",
Stefan Reinauer08670622009-06-30 15:17:49 +0000567 real_last_unitid, CONFIG_HT_CHAIN_END_UNITID_BASE);
Yinghai Lu18c70d72007-09-14 14:58:33 +0000568
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000569 }
570#endif
Yinghai Lu18c70d72007-09-14 14:58:33 +0000571 next_unitid = max_unitid;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000572
Yinghai Lu18c70d72007-09-14 14:58:33 +0000573 if (next_unitid > 0x20) {
574 next_unitid = 0x20;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000575 }
Yinghai Lu18c70d72007-09-14 14:58:33 +0000576 if( (bus->secondary == 0) && (next_unitid > 0x18)) {
577 next_unitid = 0x18; /* avoid K8 on bus 0 */
Yinghai Luecad5df2007-05-21 18:11:17 +0000578 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000579
580 /* Die if any leftover Static devices are are found.
581 * There's probably a problem in the Config.lb.
582 */
583 if(old_devices) {
584 device_t left;
585 for(left = old_devices; left; left = left->sibling) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000586 printk(BIOS_DEBUG, "%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000587 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000588 printk(BIOS_ERR, "HT: Left over static devices. Check your Config.lb\n");
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000589 if(last_func && !last_func->sibling) // put back the left over static device, and let pci_scan_bus disable it
590 last_func->sibling = old_devices;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000591 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000592
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000593 /* Now that nothing is overlapping it is safe to scan the
594 * children.
595 */
Yinghai Luecad5df2007-05-21 18:11:17 +0000596 max = pci_scan_bus(bus, 0x00, ((next_unitid-1) << 3)|7, max);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000597 return max;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000598}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000599
600/**
601 * @brief Scan a PCI bridge and the buses behind the bridge.
602 *
603 * Determine the existence of buses behind the bridge. Set up the bridge
604 * according to the result of the scan.
605 *
606 * This function is the default scan_bus() method for PCI bridge devices.
607 *
608 * @param dev pointer to the bridge device
609 * @param max the highest bus number assgined up to now
610 *
611 * @return The maximum bus number found, after scanning all subordinate busses
612 */
Myles Watson7943fe62009-10-30 02:08:07 +0000613static unsigned int hypertransport_scan_chain_x(struct bus *bus,
Yinghai Lud4b278c2006-10-04 20:46:15 +0000614 unsigned min_devfn, unsigned max_devfn, unsigned int max)
615{
616 unsigned ht_unitid_base[4];
617 unsigned offset_unitid = 1;
618 return hypertransport_scan_chain(bus, min_devfn, max_devfn, max, ht_unitid_base, offset_unitid);
619}
620
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000621unsigned int ht_scan_bridge(struct device *dev, unsigned int max)
622{
Yinghai Lud4b278c2006-10-04 20:46:15 +0000623 return do_pci_scan_bridge(dev, max, hypertransport_scan_chain_x);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000624}
625
626
627/** Default device operations for hypertransport bridges */
628static struct pci_operations ht_bus_ops_pci = {
629 .set_subsystem = 0,
630};
631
632struct device_operations default_ht_ops_bus = {
633 .read_resources = pci_bus_read_resources,
634 .set_resources = pci_dev_set_resources,
635 .enable_resources = pci_bus_enable_resources,
636 .init = 0,
637 .scan_bus = ht_scan_bridge,
638 .enable = 0,
639 .reset_bus = pci_bus_reset,
640 .ops_pci = &ht_bus_ops_pci,
641};