1 #include <device/pci_def.h>
2 #include <device/pci_ids.h>
3 #include <device/hypertransport_def.h>
5 static unsigned ht_lookup_slave_capability(device_t dev)
10 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
14 if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
15 (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
16 pos = PCI_CAPABILITY_LIST;
18 if (pos > PCI_CAP_LIST_NEXT) {
19 pos = pci_read_config8(dev, pos);
21 while(pos != 0) { /* loop through the linked list */
23 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
24 if (cap == PCI_CAP_ID_HT) {
27 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
28 if ((flags >> 13) == 0) {
29 /* Entry is a Slave secondary, success... */
33 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
38 static void ht_collapse_previous_enumeration(unsigned bus)
42 /* Spin through the devices and collapse any previous
43 * hypertransport enumeration.
45 for(dev = PCI_DEV(bus, 0, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
49 id = pci_read_config32(dev, PCI_VENDOR_ID);
50 if ((id == 0xffffffff) || (id == 0x00000000) ||
51 (id == 0x0000ffff) || (id == 0xffff0000)) {
54 pos = ht_lookup_slave_capability(dev);
59 /* Clear the unitid */
60 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
62 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
66 static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
68 /* Handle bugs in valid hypertransport frequency reporting */
72 freq_cap = pci_read_config16(dev, pos);
73 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
75 id = pci_read_config32(dev, 0);
77 /* AMD 8131 Errata 48 */
78 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
79 freq_cap &= ~(1 << HT_FREQ_800Mhz);
81 /* AMD 8151 Errata 23 */
82 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
83 freq_cap &= ~(1 << HT_FREQ_800Mhz);
85 /* AMD K8 Unsupported 1Ghz? */
86 if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
87 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
92 #define LINK_OFFS(WIDTH,FREQ,FREQ_CAP) \
93 (((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
95 #define LINK_WIDTH(OFFS) ((OFFS >> 16) & 0xFF)
96 #define LINK_FREQ(OFFS) ((OFFS >> 8) & 0xFF)
97 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
99 #define PCI_HT_HOST_OFFS LINK_OFFS( \
100 PCI_HT_CAP_HOST_WIDTH, \
101 PCI_HT_CAP_HOST_FREQ, \
102 PCI_HT_CAP_HOST_FREQ_CAP)
104 #define PCI_HT_SLAVE0_OFFS LINK_OFFS( \
105 PCI_HT_CAP_SLAVE_WIDTH0, \
106 PCI_HT_CAP_SLAVE_FREQ0, \
107 PCI_HT_CAP_SLAVE_FREQ_CAP0)
109 #define PCI_HT_SLAVE1_OFFS LINK_OFFS( \
110 PCI_HT_CAP_SLAVE_WIDTH1, \
111 PCI_HT_CAP_SLAVE_FREQ1, \
112 PCI_HT_CAP_SLAVE_FREQ_CAP1)
114 static int ht_optimize_link(
115 device_t dev1, uint8_t pos1, unsigned offs1,
116 device_t dev2, uint8_t pos2, unsigned offs2)
118 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
119 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
120 uint16_t freq_cap1, freq_cap2, freq_cap, freq_mask;
121 uint8_t width_cap1, width_cap2, width_cap, width, old_width, ln_width1, ln_width2;
122 uint8_t freq, old_freq;
124 /* Set link width and frequency */
126 /* Initially assume everything is already optimized and I don't need a reset */
129 /* Get the frequency capabilities */
130 freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
131 freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
133 /* Calculate the highest possible frequency */
134 freq = log2(freq_cap1 & freq_cap2);
136 /* See if I am changing the link freqency */
137 old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
138 needs_reset |= old_freq != freq;
139 old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
140 needs_reset |= old_freq != freq;
142 /* Set the Calulcated link frequency */
143 pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
144 pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
146 /* Get the width capabilities */
147 width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
148 width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
150 /* Calculate dev1's input width */
151 ln_width1 = link_width_to_pow2[width_cap1 & 7];
152 ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
153 if (ln_width1 > ln_width2) {
154 ln_width1 = ln_width2;
156 width = pow2_to_link_width[ln_width1];
157 /* Calculate dev1's output width */
158 ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
159 ln_width2 = link_width_to_pow2[width_cap2 & 7];
160 if (ln_width1 > ln_width2) {
161 ln_width1 = ln_width2;
163 width |= pow2_to_link_width[ln_width1] << 4;
165 /* See if I am changing dev1's width */
166 old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
167 needs_reset |= old_width != width;
169 /* Set dev1's widths */
170 pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
172 /* Calculate dev2's width */
173 width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
175 /* See if I am changing dev2's width */
176 old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
177 needs_reset |= old_width != width;
179 /* Set dev2's widths */
180 pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
185 static int ht_setup_chain(device_t udev, unsigned upos)
187 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
188 * On most boards this just happens. If a cpu has multiple
189 * non Coherent links the appropriate bus registers for the
190 * links needs to be programed to point at bus 0.
192 unsigned next_unitid, last_unitid;
196 #warning "FIXME handle multiple chains!"
198 /* Make certain the HT bus is not enumerated */
199 ht_collapse_previous_enumeration(0);
202 uoffs = PCI_HT_HOST_OFFS;
207 unsigned flags, count;
208 device_t dev = PCI_DEV(0, 0, 0);
209 last_unitid = next_unitid;
211 id = pci_read_config32(dev, PCI_VENDOR_ID);
212 /* If the chain is enumerated quit */
213 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
214 (((id >> 16) & 0xffff) == 0xffff) ||
215 (((id >> 16) & 0xffff) == 0x0000)) {
218 pos = ht_lookup_slave_capability(dev);
220 print_err("HT link capability not found\r\n");
223 /* Setup the Hypertransport link */
224 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, PCI_HT_SLAVE0_OFFS);
226 /* Update the Unitid of the current device */
227 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
228 flags &= ~0x1f; /* mask out the bse Unit ID */
229 flags |= next_unitid & 0x1f;
230 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
232 /* Remeber the location of the last device */
233 udev = PCI_DEV(0, next_unitid, 0);
235 uoffs = PCI_HT_SLAVE1_OFFS;
237 /* Compute the number of unitids consumed */
238 count = (flags >> 5) & 0x1f;
239 next_unitid += count;
241 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
251 static int ht_setup_chainx(device_t udev, unsigned upos, unsigned next_unitid)
253 unsigned last_unitid;
257 uoffs = PCI_HT_HOST_OFFS;
261 unsigned flags, count;
262 device_t dev = PCI_DEV(0, 0, 0);
263 last_unitid = next_unitid;
265 id = pci_read_config32(dev, PCI_VENDOR_ID);
266 /* If the chain is enumerated quit */
267 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
268 (((id >> 16) & 0xffff) == 0xffff) ||
269 (((id >> 16) & 0xffff) == 0x0000)) {
272 pos = ht_lookup_slave_capability(dev);
274 print_err("HT link capability not found\r\n");
277 /* Setup the Hypertransport link */
278 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, PCI_HT_SLAVE0_OFFS);
280 /* Update the Unitid of the current device */
281 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
282 flags &= ~0x1f; /* mask out the bse Unit ID */
283 flags |= next_unitid & 0x1f;
284 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
286 /* Remeber the location of the last device */
287 udev = PCI_DEV(0, next_unitid, 0);
289 uoffs = PCI_HT_SLAVE1_OFFS;
291 /* Compute the number of unitids consumed */
292 count = (flags >> 5) & 0x1f;
293 next_unitid += count;
295 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
296 if(reset_needed!=0) next_unitid |= 0xffff0000;
300 static int ht_setup_chains(const struct ht_chain *ht_c, int ht_c_num)
302 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
303 * On most boards this just happens. If a cpu has multiple
304 * non Coherent links the appropriate bus registers for the
305 * links needs to be programed to point at bus 0.
307 unsigned next_unitid;
313 /* Make certain the HT bus is not enumerated */
314 ht_collapse_previous_enumeration(0);
319 for (i = 0; i < ht_c_num; i++) {
322 reg = pci_read_config32(PCI_DEV(0,0x18,1), ht_c[i].devreg);
323 reg |= (0xff<<24) | 7;
325 pci_write_config32(PCI_DEV(0,0x18,1), ht_c[i].devreg, reg);
327 #if CONFIG_MAX_CPUS > 1
328 pci_write_config32(PCI_DEV(0,0x19,1), ht_c[i].devreg, reg);
330 #if CONFIG_MAX_CPUS > 2
331 pci_write_config32(PCI_DEV(0,0x1a,1), ht_c[i].devreg, reg);
332 pci_write_config32(PCI_DEV(0,0x1b,1), ht_c[i].devreg, reg);
336 reg8 = next_unitid & 0xff ;
340 next_unitid = ht_setup_chainx(udev,upos,next_unitid);
341 if((next_unitid & 0xffff0000) == 0xffff0000) {
343 next_unitid &=0x0000ffff;
347 pci_write_config8(PCI_DEV(0,0x18,1), ht_c[i].devreg+2, reg8);
348 #if CONFIG_MAX_CPUS > 1
349 pci_write_config8(PCI_DEV(0,0x19,1), ht_c[i].devreg+2, reg8);
351 #if CONFIG_MAX_CPUS > 2
352 pci_write_config8(PCI_DEV(0,0x1a,1), ht_c[i].devreg+2, reg8);
353 pci_write_config8(PCI_DEV(0,0x1b,1), ht_c[i].devreg+2, reg8);
357 reg8 = (next_unitid-1) & 0xff ;
358 pci_write_config8(PCI_DEV(0,0x18,1), ht_c[i].devreg+3, reg8);
359 #if CONFIG_MAX_CPUS > 1
360 pci_write_config8(PCI_DEV(0,0x19,1), ht_c[i].devreg+3, reg8);
362 #if CONFIG_MAX_CPUS > 2
363 pci_write_config8(PCI_DEV(0,0x1a,1), ht_c[i].devreg+3, reg8);
364 pci_write_config8(PCI_DEV(0,0x1b,1), ht_c[i].devreg+3, reg8);