blob: 4b5209267d90f8a8e22591e2bfa6dbadbacf81a0 [file] [log] [blame]
Jason Schildt043b4092005-08-10 15:16:44 +00001/*
Yinghai Lud4b278c2006-10-04 20:46:15 +00002 This should be done by Eric
3 2004.12 yhlu add multi ht chain dynamically support
4 2005.11 yhlu add let real sb to use small unitid
5*/
Eric Biederman5cd81732004-03-11 15:01:31 +00006#include <device/pci_def.h>
7#include <device/pci_ids.h>
8#include <device/hypertransport_def.h>
9
Myles Watsond61ada62008-10-02 19:20:22 +000010// Do we need allocate MMIO? Current We direct last 64M to sblink only, We can not lose access to last 4M range to ROM
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000011#ifndef K8_ALLOCATE_MMIO_RANGE
Myles Watsond61ada62008-10-02 19:20:22 +000012 #define K8_ALLOCATE_MMIO_RANGE 0
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000013#endif
14
Jason Schildt043b4092005-08-10 15:16:44 +000015static inline void print_linkn_in (const char *strval, uint8_t byteval)
Yinghai Lu1bc56542005-01-06 02:23:31 +000016{
Stefan Reinauer64ed2b72010-03-31 14:47:43 +000017 printk(BIOS_DEBUG, "%s%02x\n", strval, byteval);
Yinghai Lu1bc56542005-01-06 02:23:31 +000018}
19
Jason Schildt043b4092005-08-10 15:16:44 +000020static uint8_t ht_lookup_capability(device_t dev, uint16_t val)
Eric Biederman5cd81732004-03-11 15:01:31 +000021{
Jason Schildt043b4092005-08-10 15:16:44 +000022 uint8_t pos;
23 uint8_t hdr_type;
Eric Biederman5cd81732004-03-11 15:01:31 +000024
25 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
26 pos = 0;
27 hdr_type &= 0x7f;
28
29 if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
Jason Schildt043b4092005-08-10 15:16:44 +000030 (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
Eric Biederman5cd81732004-03-11 15:01:31 +000031 pos = PCI_CAPABILITY_LIST;
32 }
33 if (pos > PCI_CAP_LIST_NEXT) {
34 pos = pci_read_config8(dev, pos);
35 }
Yinghai Lud4b278c2006-10-04 20:46:15 +000036 while(pos != 0) { /* loop through the linked list */
Jason Schildt043b4092005-08-10 15:16:44 +000037 uint8_t cap;
Eric Biederman5cd81732004-03-11 15:01:31 +000038 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
39 if (cap == PCI_CAP_ID_HT) {
Jason Schildt043b4092005-08-10 15:16:44 +000040 uint16_t flags;
Eric Biederman5cd81732004-03-11 15:01:31 +000041
42 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
Jason Schildt043b4092005-08-10 15:16:44 +000043 if ((flags >> 13) == val) {
44 /* Entry is a slave or host , success... */
Eric Biederman5cd81732004-03-11 15:01:31 +000045 break;
46 }
47 }
48 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
49 }
50 return pos;
51}
52
Jason Schildt043b4092005-08-10 15:16:44 +000053static uint8_t ht_lookup_slave_capability(device_t dev)
Myles Watsond61ada62008-10-02 19:20:22 +000054{
Jason Schildt043b4092005-08-10 15:16:44 +000055 return ht_lookup_capability(dev, 0); // Slave/Primary Interface Block Format
56}
57
Stefan Reinauereea66b72010-04-07 15:32:52 +000058#if 0
Jason Schildt043b4092005-08-10 15:16:44 +000059static uint8_t ht_lookup_host_capability(device_t dev)
60{
Myles Watsond61ada62008-10-02 19:20:22 +000061 return ht_lookup_capability(dev, 1); // Host/Secondary Interface Block Format
Jason Schildt043b4092005-08-10 15:16:44 +000062}
Stefan Reinauereea66b72010-04-07 15:32:52 +000063#endif
Jason Schildt043b4092005-08-10 15:16:44 +000064
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000065static void ht_collapse_previous_enumeration(uint8_t bus, unsigned offset_unitid)
Eric Biederman5cd81732004-03-11 15:01:31 +000066{
67 device_t dev;
Jason Schildt043b4092005-08-10 15:16:44 +000068
Yinghai Lud4b278c2006-10-04 20:46:15 +000069 //actually, only for one HT device HT chain, and unitid is 0
Stefan Reinauer08670622009-06-30 15:17:49 +000070#if CONFIG_HT_CHAIN_UNITID_BASE == 0
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000071 if(offset_unitid) {
72 return;
73 }
74#endif
75
Jason Schildt043b4092005-08-10 15:16:44 +000076 /* Check if is already collapsed */
Stefan Reinauer08670622009-06-30 15:17:49 +000077 if((!offset_unitid) || (offset_unitid && (!((CONFIG_HT_CHAIN_END_UNITID_BASE == 0) && (CONFIG_HT_CHAIN_END_UNITID_BASE <CONFIG_HT_CHAIN_UNITID_BASE))))) {
Myles Watsonfa12b672009-04-30 22:45:41 +000078 uint32_t id;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000079 dev = PCI_DEV(bus, 0, 0);
Myles Watsond61ada62008-10-02 19:20:22 +000080 id = pci_read_config32(dev, PCI_VENDOR_ID);
81 if (!((id == 0xffffffff) || (id == 0x00000000) ||
82 (id == 0x0000ffff) || (id == 0xffff0000))) {
83 return;
84 }
85 }
Jason Schildt043b4092005-08-10 15:16:44 +000086
Eric Biederman5cd81732004-03-11 15:01:31 +000087 /* Spin through the devices and collapse any previous
88 * hypertransport enumeration.
89 */
Yinghai Lu2c956bb2004-12-17 21:08:16 +000090 for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
Jason Schildt043b4092005-08-10 15:16:44 +000091 uint32_t id;
92 uint8_t pos;
93 uint16_t flags;
Myles Watsond61ada62008-10-02 19:20:22 +000094
Eric Biederman5cd81732004-03-11 15:01:31 +000095 id = pci_read_config32(dev, PCI_VENDOR_ID);
Jason Schildt043b4092005-08-10 15:16:44 +000096 if ((id == 0xffffffff) || (id == 0x00000000) ||
97 (id == 0x0000ffff) || (id == 0xffff0000)) {
Eric Biederman5cd81732004-03-11 15:01:31 +000098 continue;
99 }
Myles Watsond61ada62008-10-02 19:20:22 +0000100
Eric Biederman5cd81732004-03-11 15:01:31 +0000101 pos = ht_lookup_slave_capability(dev);
102 if (!pos) {
103 continue;
104 }
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000105
Eric Biederman5cd81732004-03-11 15:01:31 +0000106 /* Clear the unitid */
107 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
108 flags &= ~0x1f;
109 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
110 }
111}
112
Jason Schildt043b4092005-08-10 15:16:44 +0000113static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
Eric Biederman5cd81732004-03-11 15:01:31 +0000114{
115 /* Handle bugs in valid hypertransport frequency reporting */
Jason Schildt043b4092005-08-10 15:16:44 +0000116 uint16_t freq_cap;
117 uint32_t id;
Eric Biederman5cd81732004-03-11 15:01:31 +0000118
119 freq_cap = pci_read_config16(dev, pos);
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000120 printk(BIOS_SPEW, "pos=0x%x, unfiltered freq_cap=0x%x\n", pos, freq_cap);
Eric Biederman5cd81732004-03-11 15:01:31 +0000121 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
122
123 id = pci_read_config32(dev, 0);
124
125 /* AMD 8131 Errata 48 */
126 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
127 freq_cap &= ~(1 << HT_FREQ_800Mhz);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000128 return freq_cap;
129 }
arch import user (historical)98d0d302005-07-06 17:13:46 +0000130
Eric Biederman5cd81732004-03-11 15:01:31 +0000131 /* AMD 8151 Errata 23 */
132 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
133 freq_cap &= ~(1 << HT_FREQ_800Mhz);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000134 return freq_cap;
Myles Watsond61ada62008-10-02 19:20:22 +0000135 }
136
Eric Biederman5cd81732004-03-11 15:01:31 +0000137 /* AMD K8 Unsupported 1Ghz? */
138 if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000139 #if CONFIG_K8_HT_FREQ_1G_SUPPORT == 1
140 #if CONFIG_K8_REV_F_SUPPORT == 0
Myles Watsond61ada62008-10-02 19:20:22 +0000141 if (is_cpu_pre_e0()) { // only E0 later support 1GHz
Jason Schildtcf6df2a2005-10-25 21:41:45 +0000142 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000143 }
Yinghai Lud4b278c2006-10-04 20:46:15 +0000144 #endif
Myles Watsond61ada62008-10-02 19:20:22 +0000145 #else
146 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000147 #endif
Eric Biederman5cd81732004-03-11 15:01:31 +0000148 }
arch import user (historical)98d0d302005-07-06 17:13:46 +0000149
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000150 printk(BIOS_SPEW, "pos=0x%x, filtered freq_cap=0x%x\n", pos, freq_cap);
151 //printk(BIOS_SPEW, "capping to 800/600/400/200 MHz\n");
Carl-Daniel Hailfingerc589e5a2008-12-23 02:05:55 +0000152 //freq_cap &= 0x3f;
Eric Biederman5cd81732004-03-11 15:01:31 +0000153 return freq_cap;
154}
Myles Watsond61ada62008-10-02 19:20:22 +0000155
Yinghai Lu00a018f2007-04-06 21:06:44 +0000156static uint8_t ht_read_width_cap(device_t dev, uint8_t pos)
157{
158 uint8_t width_cap = pci_read_config8(dev, pos);
159
160 uint32_t id;
161
162 id = pci_read_config32(dev, 0);
163
164 /* netlogic micro cap doesn't support 16 bit yet */
165 if (id == (0x184e | (0x0001 << 16))) {
166 if((width_cap & 0x77) == 0x11) {
167 width_cap &= 0x88;
168 }
169 }
Myles Watsond61ada62008-10-02 19:20:22 +0000170
Yinghai Lu00a018f2007-04-06 21:06:44 +0000171 return width_cap;
Myles Watsond61ada62008-10-02 19:20:22 +0000172
Yinghai Lu00a018f2007-04-06 21:06:44 +0000173}
Myles Watsond61ada62008-10-02 19:20:22 +0000174
Jason Schildt6e44b422005-08-09 21:53:07 +0000175#define LINK_OFFS(CTRL, WIDTH,FREQ,FREQ_CAP) \
Myles Watsond61ada62008-10-02 19:20:22 +0000176 (((CTRL & 0xff) << 24) | ((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
Jason Schildt6e44b422005-08-09 21:53:07 +0000177
Jason Schildt043b4092005-08-10 15:16:44 +0000178#define LINK_CTRL(OFFS) ((OFFS >> 24) & 0xFF)
Eric Biederman5cd81732004-03-11 15:01:31 +0000179#define LINK_WIDTH(OFFS) ((OFFS >> 16) & 0xFF)
Jason Schildt043b4092005-08-10 15:16:44 +0000180#define LINK_FREQ(OFFS) ((OFFS >> 8) & 0xFF)
Eric Biederman5cd81732004-03-11 15:01:31 +0000181#define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
182
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000183#define PCI_HT_HOST_OFFS LINK_OFFS( \
Myles Watsond61ada62008-10-02 19:20:22 +0000184 PCI_HT_CAP_HOST_CTRL, \
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000185 PCI_HT_CAP_HOST_WIDTH, \
186 PCI_HT_CAP_HOST_FREQ, \
187 PCI_HT_CAP_HOST_FREQ_CAP)
Eric Biederman5cd81732004-03-11 15:01:31 +0000188
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000189#define PCI_HT_SLAVE0_OFFS LINK_OFFS( \
Myles Watsond61ada62008-10-02 19:20:22 +0000190 PCI_HT_CAP_SLAVE_CTRL0, \
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000191 PCI_HT_CAP_SLAVE_WIDTH0, \
192 PCI_HT_CAP_SLAVE_FREQ0, \
193 PCI_HT_CAP_SLAVE_FREQ_CAP0)
Eric Biederman5cd81732004-03-11 15:01:31 +0000194
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000195#define PCI_HT_SLAVE1_OFFS LINK_OFFS( \
Myles Watsond61ada62008-10-02 19:20:22 +0000196 PCI_HT_CAP_SLAVE_CTRL1, \
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000197 PCI_HT_CAP_SLAVE_WIDTH1, \
198 PCI_HT_CAP_SLAVE_FREQ1, \
199 PCI_HT_CAP_SLAVE_FREQ_CAP1)
Eric Biederman5cd81732004-03-11 15:01:31 +0000200
201static int ht_optimize_link(
Jason Schildt043b4092005-08-10 15:16:44 +0000202 device_t dev1, uint8_t pos1, unsigned offs1,
203 device_t dev2, uint8_t pos2, unsigned offs2)
Eric Biederman5cd81732004-03-11 15:01:31 +0000204{
205 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
206 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
Jason Schildt043b4092005-08-10 15:16:44 +0000207 uint16_t freq_cap1, freq_cap2;
208 uint8_t width_cap1, width_cap2, width, old_width, ln_width1, ln_width2;
209 uint8_t freq, old_freq;
Eric Biederman5cd81732004-03-11 15:01:31 +0000210 int needs_reset;
211 /* Set link width and frequency */
212
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000213 printk(BIOS_SPEW, "entering ht_optimize_link\n");
Eric Biederman5cd81732004-03-11 15:01:31 +0000214 /* Initially assume everything is already optimized and I don't need a reset */
215 needs_reset = 0;
216
217 /* Get the frequency capabilities */
218 freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
219 freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000220 printk(BIOS_SPEW, "freq_cap1=0x%x, freq_cap2=0x%x\n", freq_cap1, freq_cap2);
Eric Biederman5cd81732004-03-11 15:01:31 +0000221
222 /* Calculate the highest possible frequency */
223 freq = log2(freq_cap1 & freq_cap2);
224
225 /* See if I am changing the link freqency */
226 old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
Jason Schildtcf6df2a2005-10-25 21:41:45 +0000227 old_freq &= 0x0f;
Eric Biederman5cd81732004-03-11 15:01:31 +0000228 needs_reset |= old_freq != freq;
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000229 printk(BIOS_SPEW, "dev1 old_freq=0x%x, freq=0x%x, needs_reset=0x%0x\n", old_freq, freq, needs_reset);
Eric Biederman5cd81732004-03-11 15:01:31 +0000230 old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
Jason Schildtcf6df2a2005-10-25 21:41:45 +0000231 old_freq &= 0x0f;
Eric Biederman5cd81732004-03-11 15:01:31 +0000232 needs_reset |= old_freq != freq;
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000233 printk(BIOS_SPEW, "dev2 old_freq=0x%x, freq=0x%x, needs_reset=0x%0x\n", old_freq, freq, needs_reset);
Eric Biederman5cd81732004-03-11 15:01:31 +0000234
Carl-Daniel Hailfingerc589e5a2008-12-23 02:05:55 +0000235 /* Set the Calculated link frequency */
Eric Biederman5cd81732004-03-11 15:01:31 +0000236 pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
237 pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
238
239 /* Get the width capabilities */
Yinghai Lu00a018f2007-04-06 21:06:44 +0000240 width_cap1 = ht_read_width_cap(dev1, pos1 + LINK_WIDTH(offs1));
241 width_cap2 = ht_read_width_cap(dev2, pos2 + LINK_WIDTH(offs2));
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000242 printk(BIOS_SPEW, "width_cap1=0x%x, width_cap2=0x%x\n", width_cap1, width_cap2);
Eric Biederman5cd81732004-03-11 15:01:31 +0000243
244 /* Calculate dev1's input width */
245 ln_width1 = link_width_to_pow2[width_cap1 & 7];
246 ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000247 printk(BIOS_SPEW, "dev1 input ln_width1=0x%x, ln_width2=0x%x\n", ln_width1, ln_width2);
Eric Biederman5cd81732004-03-11 15:01:31 +0000248 if (ln_width1 > ln_width2) {
249 ln_width1 = ln_width2;
250 }
251 width = pow2_to_link_width[ln_width1];
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000252 printk(BIOS_SPEW, "dev1 input width=0x%x\n", width);
Eric Biederman5cd81732004-03-11 15:01:31 +0000253 /* Calculate dev1's output width */
254 ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
255 ln_width2 = link_width_to_pow2[width_cap2 & 7];
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000256 printk(BIOS_SPEW, "dev1 output ln_width1=0x%x, ln_width2=0x%x\n", ln_width1, ln_width2);
Eric Biederman5cd81732004-03-11 15:01:31 +0000257 if (ln_width1 > ln_width2) {
258 ln_width1 = ln_width2;
259 }
260 width |= pow2_to_link_width[ln_width1] << 4;
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000261 printk(BIOS_SPEW, "dev1 input|output width=0x%x\n", width);
Eric Biederman5cd81732004-03-11 15:01:31 +0000262
263 /* See if I am changing dev1's width */
264 old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000265 old_width &= 0x77;
Eric Biederman5cd81732004-03-11 15:01:31 +0000266 needs_reset |= old_width != width;
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000267 printk(BIOS_SPEW, "old dev1 input|output width=0x%x\n", width);
Eric Biederman5cd81732004-03-11 15:01:31 +0000268
269 /* Set dev1's widths */
270 pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
271
272 /* Calculate dev2's width */
273 width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000274 printk(BIOS_SPEW, "dev2 input|output width=0x%x\n", width);
Eric Biederman5cd81732004-03-11 15:01:31 +0000275
276 /* See if I am changing dev2's width */
277 old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000278 old_width &= 0x77;
Eric Biederman5cd81732004-03-11 15:01:31 +0000279 needs_reset |= old_width != width;
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000280 printk(BIOS_SPEW, "old dev2 input|output width=0x%x\n", width);
Eric Biederman5cd81732004-03-11 15:01:31 +0000281
282 /* Set dev2's widths */
283 pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
284
285 return needs_reset;
286}
Myles Watsond61ada62008-10-02 19:20:22 +0000287
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000288#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000289static void ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid, struct sys_info *sysinfo)
290#else
291static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid)
292#endif
Jason Schildt043b4092005-08-10 15:16:44 +0000293{
Stefan Reinauer08670622009-06-30 15:17:49 +0000294 //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
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000295
Jason Schildt043b4092005-08-10 15:16:44 +0000296 uint8_t next_unitid, last_unitid;
297 unsigned uoffs;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000298
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000299#if !CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000300 int reset_needed = 0;
301#endif
302
Stefan Reinauer08670622009-06-30 15:17:49 +0000303#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
304 //let't record the device of last ht device, So we can set the Unitid to CONFIG_HT_CHAIN_END_UNITID_BASE
Myles Watsond61ada62008-10-02 19:20:22 +0000305 unsigned real_last_unitid;
306 uint8_t real_last_pos;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000307 int ht_dev_num = 0;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000308 uint8_t end_used = 0;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000309#endif
Jason Schildt043b4092005-08-10 15:16:44 +0000310
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000311 uoffs = PCI_HT_HOST_OFFS;
Stefan Reinauer08670622009-06-30 15:17:49 +0000312 next_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE:1;
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000313
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000314 do {
Jason Schildt043b4092005-08-10 15:16:44 +0000315 uint32_t id;
316 uint8_t pos;
317 uint16_t flags, ctrl;
318 uint8_t count;
319 unsigned offs;
Myles Watsond61ada62008-10-02 19:20:22 +0000320
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000321 /* Wait until the link initialization is complete */
322 do {
323 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
324 /* Is this the end of the hypertransport chain? */
325 if (ctrl & (1 << 6)) {
Myles Watsond61ada62008-10-02 19:20:22 +0000326 goto end_of_chain;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000327 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000328
329 if (ctrl & ((1 << 4) | (1 << 8))) {
Myles Watsond61ada62008-10-02 19:20:22 +0000330 /*
331 * Either the link has failed, or we have
332 * a CRC error.
333 * Sometimes this can happen due to link
334 * retrain, so lets knock it down and see
335 * if its transient
336 */
Stefan Reinauerfbce0ff2006-04-11 18:36:42 +0000337 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
Myles Watsond61ada62008-10-02 19:20:22 +0000338 pci_write_config16(udev, upos + LINK_CTRL(uoffs), ctrl);
339 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
340 if (ctrl & ((1 << 4) | (1 << 8))) {
341 print_err("Detected error on Hypertransport Link\n");
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000342 break;
Myles Watsond61ada62008-10-02 19:20:22 +0000343 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000344 }
345 } while((ctrl & (1 << 5)) == 0);
Myles Watsond61ada62008-10-02 19:20:22 +0000346
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000347 device_t dev = PCI_DEV(bus, 0, 0);
Jason Schildt043b4092005-08-10 15:16:44 +0000348 last_unitid = next_unitid;
349
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000350 id = pci_read_config32(dev, PCI_VENDOR_ID);
Jason Schildt043b4092005-08-10 15:16:44 +0000351
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000352 /* If the chain is enumerated quit */
Myles Watsond61ada62008-10-02 19:20:22 +0000353 if ((id == 0xffffffff) || (id == 0x00000000) ||
354 (id == 0x0000ffff) || (id == 0xffff0000))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000355 {
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000356 break;
357 }
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000358
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000359 pos = ht_lookup_slave_capability(dev);
360 if (!pos) {
Myles Watsond61ada62008-10-02 19:20:22 +0000361 print_err("udev="); print_err_hex32(udev);
362 print_err("\tupos="); print_err_hex32(upos);
363 print_err("\tuoffs="); print_err_hex32(uoffs);
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000364 print_err("\tHT link capability not found\n");
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000365 break;
366 }
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000367
Yinghai Lu00a018f2007-04-06 21:06:44 +0000368
Stefan Reinauer08670622009-06-30 15:17:49 +0000369#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
Yinghai Lu00a018f2007-04-06 21:06:44 +0000370 if(offset_unitid) {
Yinghai Lu18c70d72007-09-14 14:58:33 +0000371 if(next_unitid>= (bus ? 0x20:0x18) ) {
372 if(!end_used) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000373 next_unitid = CONFIG_HT_CHAIN_END_UNITID_BASE;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000374 end_used = 1;
375 } else {
376 goto out;
377 }
Myles Watsond61ada62008-10-02 19:20:22 +0000378
379 }
380 real_last_pos = pos;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000381 real_last_unitid = next_unitid;
Yinghai Lu00a018f2007-04-06 21:06:44 +0000382 ht_dev_num++;
Myles Watsond61ada62008-10-02 19:20:22 +0000383 }
Yinghai Lu00a018f2007-04-06 21:06:44 +0000384#endif
Yinghai Lu18c70d72007-09-14 14:58:33 +0000385 /* Update the Unitid of the current device */
386 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
387 flags &= ~0x1f; /* mask out the base Unit ID */
388 flags |= next_unitid & 0x1f;
arch import user (historical)98d0d302005-07-06 17:13:46 +0000389 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
390
Myles Watsond61ada62008-10-02 19:20:22 +0000391 /* Compute the number of unitids consumed */
392 count = (flags >> 5) & 0x1f;
Yinghai Lu18c70d72007-09-14 14:58:33 +0000393
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000394 /* Note the change in device number */
Yinghai Lu18c70d72007-09-14 14:58:33 +0000395 dev = PCI_DEV(bus, next_unitid, 0);
Yinghai Lue3247312005-01-20 20:41:17 +0000396
Myles Watsond61ada62008-10-02 19:20:22 +0000397 next_unitid += count;
Yinghai Lue3247312005-01-20 20:41:17 +0000398
Yinghai Lud4b278c2006-10-04 20:46:15 +0000399 /* Find which side of the ht link we are on,
400 * by reading which direction our last write to PCI_CAP_FLAGS
401 * came from.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000402 */
403 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
Myles Watsond61ada62008-10-02 19:20:22 +0000404 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
405
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000406 #if CONFIG_RAMINIT_SYSINFO
Myles Watsond61ada62008-10-02 19:20:22 +0000407 /* store the link pair here and we will Setup the Hypertransport link later, after we get final FID/VID */
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000408 {
409 struct link_pair_st *link_pair = &sysinfo->link_pair[sysinfo->link_pair_num];
410 link_pair->udev = udev;
411 link_pair->upos = upos;
412 link_pair->uoffs = uoffs;
413 link_pair->dev = dev;
414 link_pair->pos = pos;
415 link_pair->offs = offs;
416 sysinfo->link_pair_num++;
417 }
418 #else
419 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
420 #endif
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000421
Yinghai Lud4b278c2006-10-04 20:46:15 +0000422 /* Remeber the location of the last device */
arch import user (historical)98d0d302005-07-06 17:13:46 +0000423 udev = dev;
424 upos = pos;
425 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000426
Yinghai Lu18c70d72007-09-14 14:58:33 +0000427 } while (last_unitid != next_unitid );
Jason Schildt043b4092005-08-10 15:16:44 +0000428
Stefan Reinauer08670622009-06-30 15:17:49 +0000429#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
Yinghai Lu18c70d72007-09-14 14:58:33 +0000430out:
Myles Watsonfa12b672009-04-30 22:45:41 +0000431#endif
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000432end_of_chain: ;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000433
Stefan Reinauer08670622009-06-30 15:17:49 +0000434#if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
435 if(offset_unitid && (ht_dev_num>1) && (real_last_unitid != CONFIG_HT_CHAIN_END_UNITID_BASE) && !end_used ) {
Myles Watsond61ada62008-10-02 19:20:22 +0000436 uint16_t flags;
Myles Watsond61ada62008-10-02 19:20:22 +0000437 flags = pci_read_config16(PCI_DEV(bus,real_last_unitid,0), real_last_pos + PCI_CAP_FLAGS);
438 flags &= ~0x1f;
Stefan Reinauer08670622009-06-30 15:17:49 +0000439 flags |= CONFIG_HT_CHAIN_END_UNITID_BASE & 0x1f;
Myles Watsond61ada62008-10-02 19:20:22 +0000440 pci_write_config16(PCI_DEV(bus, real_last_unitid, 0), real_last_pos + PCI_CAP_FLAGS, flags);
441
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000442 #if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000443 // Here need to change the dev in the array
Stefan Reinauerc51dc442010-04-07 01:44:04 +0000444 int i;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000445 for(i=0;i<sysinfo->link_pair_num;i++)
Myles Watsond61ada62008-10-02 19:20:22 +0000446 {
447 struct link_pair_st *link_pair = &sysinfo->link_pair[i];
448 if(link_pair->udev == PCI_DEV(bus, real_last_unitid, 0)) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000449 link_pair->udev = PCI_DEV(bus, CONFIG_HT_CHAIN_END_UNITID_BASE, 0);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000450 continue;
451 }
Myles Watsond61ada62008-10-02 19:20:22 +0000452 if(link_pair->dev == PCI_DEV(bus, real_last_unitid, 0)) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000453 link_pair->dev = PCI_DEV(bus, CONFIG_HT_CHAIN_END_UNITID_BASE, 0);
Myles Watsond61ada62008-10-02 19:20:22 +0000454 }
455 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000456 #endif
457
Myles Watsond61ada62008-10-02 19:20:22 +0000458 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000459#endif
460
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000461#if !CONFIG_RAMINIT_SYSINFO
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000462 return reset_needed;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000463#endif
464
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000465}
Li-Ta Loedeff592004-03-25 17:50:06 +0000466
Stefan Reinauerd4f53732010-04-09 14:46:51 +0000467#if 0
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000468#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000469static void ht_setup_chain(device_t udev, unsigned upos, struct sys_info *sysinfo)
470#else
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000471static int ht_setup_chain(device_t udev, unsigned upos)
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000472#endif
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000473{
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000474 unsigned offset_unitid = 0;
Stefan Reinauer08670622009-06-30 15:17:49 +0000475#if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
Myles Watsond61ada62008-10-02 19:20:22 +0000476 offset_unitid = 1;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000477#endif
478
Myles Watsond61ada62008-10-02 19:20:22 +0000479 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
480 * On most boards this just happens. If a cpu has multiple
481 * non Coherent links the appropriate bus registers for the
482 * links needs to be programed to point at bus 0.
483 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000484
Myles Watsond61ada62008-10-02 19:20:22 +0000485 /* Make certain the HT bus is not enumerated */
486 ht_collapse_previous_enumeration(0, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000487
Stefan Reinauer08670622009-06-30 15:17:49 +0000488#if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
Myles Watsond61ada62008-10-02 19:20:22 +0000489 offset_unitid = 1;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000490#endif
491
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000492#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000493 ht_setup_chainx(udev, upos, 0, offset_unitid, sysinfo);
494#else
Myles Watsond61ada62008-10-02 19:20:22 +0000495 return ht_setup_chainx(udev, upos, 0, offset_unitid);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000496#endif
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000497}
Stefan Reinauerd4f53732010-04-09 14:46:51 +0000498#endif
499
Yinghai Lud4b278c2006-10-04 20:46:15 +0000500static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
Yinghai Lu1bc56542005-01-06 02:23:31 +0000501{
Jason Schildt043b4092005-08-10 15:16:44 +0000502 uint32_t dword, dword_old;
503 uint8_t link_type;
Myles Watsond61ada62008-10-02 19:20:22 +0000504
Yinghai Lu1bc56542005-01-06 02:23:31 +0000505 /* This works on an Athlon64 because unimplemented links return 0 */
506 dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
507 link_type = dword & 0xff;
Myles Watsond61ada62008-10-02 19:20:22 +0000508
509
Yinghai Lud4b278c2006-10-04 20:46:15 +0000510 if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
511 dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
Yinghai Lu1bc56542005-01-06 02:23:31 +0000512 dword &= ~( 0xff<<(linkn *8) );
513 dword |= val << (linkn *8);
Myles Watsond61ada62008-10-02 19:20:22 +0000514
Yinghai Lud4b278c2006-10-04 20:46:15 +0000515 if (dword != dword_old) {
516 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
517 return 1;
518 }
Yinghai Lu1bc56542005-01-06 02:23:31 +0000519 }
Myles Watsond61ada62008-10-02 19:20:22 +0000520
Yinghai Lu1bc56542005-01-06 02:23:31 +0000521 return 0;
522}
523
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000524static int optimize_link_read_pointers_chain(uint8_t ht_c_num)
Yinghai Lu1bc56542005-01-06 02:23:31 +0000525{
Myles Watsond61ada62008-10-02 19:20:22 +0000526 int reset_needed;
Jason Schildt043b4092005-08-10 15:16:44 +0000527 uint8_t i;
Yinghai Lu1bc56542005-01-06 02:23:31 +0000528
529 reset_needed = 0;
530
531 for (i = 0; i < ht_c_num; i++) {
Jason Schildt043b4092005-08-10 15:16:44 +0000532 uint32_t reg;
533 uint8_t nodeid, linkn;
534 uint8_t busn;
535 uint8_t val;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000536 unsigned devn = 1;
537
Stefan Reinauer08670622009-06-30 15:17:49 +0000538 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
539 #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
Myles Watsond61ada62008-10-02 19:20:22 +0000540 if(i==0) // to check if it is sb ht chain
541 #endif
Stefan Reinauer08670622009-06-30 15:17:49 +0000542 devn = CONFIG_HT_CHAIN_UNITID_BASE;
Myles Watsond61ada62008-10-02 19:20:22 +0000543 #endif
Yinghai Lu1bc56542005-01-06 02:23:31 +0000544
545 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
Myles Watsond61ada62008-10-02 19:20:22 +0000546
Yinghai Lu1bc56542005-01-06 02:23:31 +0000547 nodeid = ((reg & 0xf0)>>4); // nodeid
548 linkn = ((reg & 0xf00)>>8); // link n
549 busn = (reg & 0xff0000)>>16; //busn
Yinghai Lud4b278c2006-10-04 20:46:15 +0000550
551 reg = pci_read_config32( PCI_DEV(busn, devn, 0), PCI_VENDOR_ID); // ? the chain dev maybe offseted
Yinghai Lu1bc56542005-01-06 02:23:31 +0000552 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
553 val = 0x25;
Jason Schildt043b4092005-08-10 15:16:44 +0000554 } else if ( (reg & 0xffff) == PCI_VENDOR_ID_NVIDIA ) {
Yinghai Lu1bc56542005-01-06 02:23:31 +0000555 val = 0x25;//???
Jason Schildt043b4092005-08-10 15:16:44 +0000556 } else {
Yinghai Lu1bc56542005-01-06 02:23:31 +0000557 continue;
558 }
559
560 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
561
562 }
563
564 return reset_needed;
565}
566
Stefan Reinauer432461e2011-04-19 00:36:39 +0000567#if CONFIG_SOUTHBRIDGE_NVIDIA_CK804 // || CONFIG_SOUTHBRIDGE_NVIDIA_MCP55
Yinghai Lud4b278c2006-10-04 20:46:15 +0000568static int set_ht_link_buffer_count(uint8_t node, uint8_t linkn, uint8_t linkt, unsigned val)
569{
Myles Watsond61ada62008-10-02 19:20:22 +0000570 uint32_t dword;
571 uint8_t link_type;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000572 unsigned regpos;
573 device_t dev;
574
Myles Watsond61ada62008-10-02 19:20:22 +0000575 /* This works on an Athlon64 because unimplemented links return 0 */
Yinghai Lud4b278c2006-10-04 20:46:15 +0000576 regpos = 0x98 + (linkn * 0x20);
577 dev = PCI_DEV(0,0x18+node,0);
Myles Watsond61ada62008-10-02 19:20:22 +0000578 dword = pci_read_config32(dev, regpos);
579 link_type = dword & 0xff;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000580
Myles Watsond61ada62008-10-02 19:20:22 +0000581 if ( (link_type & 0x7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
Yinghai Lud4b278c2006-10-04 20:46:15 +0000582 regpos = 0x90 + (linkn * 0x20);
Myles Watsond61ada62008-10-02 19:20:22 +0000583 dword = pci_read_config32(dev, regpos );
Yinghai Lud4b278c2006-10-04 20:46:15 +0000584
Myles Watsond61ada62008-10-02 19:20:22 +0000585 if (dword != val) {
586 pci_write_config32(dev, regpos, val);
587 return 1;
588 }
Yinghai Lud4b278c2006-10-04 20:46:15 +0000589 }
590
Myles Watsond61ada62008-10-02 19:20:22 +0000591 return 0;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000592}
Stefan Reinauer116ec612010-04-23 19:16:30 +0000593
Yinghai Lud4b278c2006-10-04 20:46:15 +0000594static int set_ht_link_buffer_counts_chain(uint8_t ht_c_num, unsigned vendorid, unsigned val)
595{
Myles Watsond61ada62008-10-02 19:20:22 +0000596 int reset_needed;
597 uint8_t i;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000598
Myles Watsond61ada62008-10-02 19:20:22 +0000599 reset_needed = 0;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000600
Myles Watsond61ada62008-10-02 19:20:22 +0000601 for (i = 0; i < ht_c_num; i++) {
602 uint32_t reg;
603 uint8_t nodeid, linkn;
604 uint8_t busn;
605 unsigned devn;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000606
Myles Watsond61ada62008-10-02 19:20:22 +0000607 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
608 if((reg & 3) != 3) continue; // not enabled
Yinghai Lud4b278c2006-10-04 20:46:15 +0000609
Myles Watsond61ada62008-10-02 19:20:22 +0000610 nodeid = ((reg & 0xf0)>>4); // nodeid
611 linkn = ((reg & 0xf00)>>8); // link n
612 busn = (reg & 0xff0000)>>16; //busn
Yinghai Lud4b278c2006-10-04 20:46:15 +0000613
Yinghai Lu00a018f2007-04-06 21:06:44 +0000614 for(devn = 0; devn < 0x20; devn++) {
Myles Watsond61ada62008-10-02 19:20:22 +0000615 reg = pci_read_config32( PCI_DEV(busn, devn, 0), PCI_VENDOR_ID); //1?
616 if ( (reg & 0xffff) == vendorid ) {
617 reset_needed |= set_ht_link_buffer_count(nodeid, linkn, 0x07,val);
Yinghai Lu00a018f2007-04-06 21:06:44 +0000618 break;
Myles Watsond61ada62008-10-02 19:20:22 +0000619 }
Yinghai Lu00a018f2007-04-06 21:06:44 +0000620 }
Myles Watsond61ada62008-10-02 19:20:22 +0000621 }
Yinghai Lud4b278c2006-10-04 20:46:15 +0000622
Myles Watsond61ada62008-10-02 19:20:22 +0000623 return reset_needed;
Yinghai Lud4b278c2006-10-04 20:46:15 +0000624}
Stefan Reinauer116ec612010-04-23 19:16:30 +0000625#endif
Yinghai Lud4b278c2006-10-04 20:46:15 +0000626
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000627#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000628static void ht_setup_chains(uint8_t ht_c_num, struct sys_info *sysinfo)
629#else
Jason Schildt043b4092005-08-10 15:16:44 +0000630static int ht_setup_chains(uint8_t ht_c_num)
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000631#endif
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000632{
Myles Watsond61ada62008-10-02 19:20:22 +0000633 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
Yinghai Lud4b278c2006-10-04 20:46:15 +0000634 * On most boards this just happens. If a cpu has multiple
635 * non Coherent links the appropriate bus registers for the
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000636 * links needs to be programed to point at bus 0.
637 */
Myles Watsond61ada62008-10-02 19:20:22 +0000638 uint8_t upos;
639 device_t udev;
Jason Schildt043b4092005-08-10 15:16:44 +0000640 uint8_t i;
Li-Ta Loedeff592004-03-25 17:50:06 +0000641
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000642#if !CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000643 int reset_needed = 0;
644#else
645 sysinfo->link_pair_num = 0;
646#endif
Li-Ta Loedeff592004-03-25 17:50:06 +0000647
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000648 // first one is SB Chain
Li-Ta Lo8e79fc32004-04-15 17:33:21 +0000649 for (i = 0; i < ht_c_num; i++) {
Jason Schildt043b4092005-08-10 15:16:44 +0000650 uint32_t reg;
651 uint8_t devpos;
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000652 unsigned regpos;
Jason Schildt043b4092005-08-10 15:16:44 +0000653 uint32_t dword;
654 uint8_t busn;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000655 unsigned offset_unitid = 0;
Myles Watsond61ada62008-10-02 19:20:22 +0000656
Yinghai Lu2c956bb2004-12-17 21:08:16 +0000657 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
Li-Ta Loedeff592004-03-25 17:50:06 +0000658
Yinghai Lud4b278c2006-10-04 20:46:15 +0000659 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
660 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
661 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000662 busn = (reg & 0xff0000)>>16;
Myles Watsond61ada62008-10-02 19:20:22 +0000663
Yinghai Lu6a61d6a2004-10-20 05:07:16 +0000664 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
665 dword &= ~(0xffff<<8);
666 dword |= (reg & 0xffff0000)>>8;
667 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
Myles Watsond61ada62008-10-02 19:20:22 +0000668
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000669
Stefan Reinauer08670622009-06-30 15:17:49 +0000670 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
671 #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
Myles Watsond61ada62008-10-02 19:20:22 +0000672 if(i==0) // to check if it is sb ht chain
673 #endif
674 offset_unitid = 1;
675 #endif
676
677 /* Make certain the HT bus is not enumerated */
678 ht_collapse_previous_enumeration(busn, offset_unitid);
Li-Ta Loedeff592004-03-25 17:50:06 +0000679
Yinghai Lu4403f602004-11-03 00:47:40 +0000680 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
Jason Schildt043b4092005-08-10 15:16:44 +0000681 udev = PCI_DEV(0, devpos, 0);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000682
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000683#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000684 ht_setup_chainx(udev,upos,busn, offset_unitid, sysinfo); // all not
685#else
686 reset_needed |= ht_setup_chainx(udev,upos,busn, offset_unitid); //all not
687#endif
Jason Schildt043b4092005-08-10 15:16:44 +0000688
Li-Ta Lo4cd79f32004-03-26 21:34:04 +0000689 }
Li-Ta Loedeff592004-03-25 17:50:06 +0000690
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000691#if !CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000692 reset_needed |= optimize_link_read_pointers_chain(ht_c_num);
Yinghai Lu2c956bb2004-12-17 21:08:16 +0000693
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000694 return reset_needed;
Jason Schildt043b4092005-08-10 15:16:44 +0000695#endif
Yinghai Lu1bc56542005-01-06 02:23:31 +0000696
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000697}
698
Yinghai Lud4b278c2006-10-04 20:46:15 +0000699#if defined (__GNUC__)
700static inline unsigned get_nodes(void);
701#endif
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000702
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000703#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000704static void ht_setup_chains_x(struct sys_info *sysinfo)
705#else
Jason Schildt043b4092005-08-10 15:16:44 +0000706static int ht_setup_chains_x(void)
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000707#endif
Myles Watsond61ada62008-10-02 19:20:22 +0000708{
709 uint8_t nodeid;
710 uint32_t reg;
Jason Schildt043b4092005-08-10 15:16:44 +0000711 uint32_t tempreg;
Myles Watsond61ada62008-10-02 19:20:22 +0000712 uint8_t next_busn;
713 uint8_t ht_c_num;
Jason Schildt043b4092005-08-10 15:16:44 +0000714 uint8_t nodes;
Patrick Georgi7bbd7f22010-11-07 18:20:51 +0000715#if CONFIG_K8_ALLOCATE_IO_RANGE
Jason Schildt043b4092005-08-10 15:16:44 +0000716 unsigned next_io_base;
717#endif
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000718
Myles Watsond61ada62008-10-02 19:20:22 +0000719 nodes = get_nodes();
720
721 /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
722 reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
723 /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=0x3f+1 */
Jason Schildt043b4092005-08-10 15:16:44 +0000724 print_linkn_in("SBLink=", ((reg>>8) & 3) );
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000725#if CONFIG_RAMINIT_SYSINFO
Yinghai Lud4b278c2006-10-04 20:46:15 +0000726 sysinfo->sblk = (reg>>8) & 3;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000727 sysinfo->sbbusn = 0;
728 sysinfo->nodes = nodes;
729#endif
Myles Watsond61ada62008-10-02 19:20:22 +0000730 tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (0x3f<<24);
731 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
Jason Schildt043b4092005-08-10 15:16:44 +0000732
Yinghai Lud4b278c2006-10-04 20:46:15 +0000733 next_busn=0x3f+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
Jason Schildt043b4092005-08-10 15:16:44 +0000734
Patrick Georgi7bbd7f22010-11-07 18:20:51 +0000735#if CONFIG_K8_ALLOCATE_IO_RANGE
Jason Schildt043b4092005-08-10 15:16:44 +0000736 /* io range allocation */
737 tempreg = 0 | (((reg>>8) & 0x3) << 4 )| (0x3<<12); //limit
738 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4, tempreg);
739 tempreg = 3 | ( 3<<4) | (0<<12); //base
740 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0, tempreg);
741 next_io_base = 0x3+0x1;
742#endif
743
Yinghai Lu1bc56542005-01-06 02:23:31 +0000744 /* clean others */
Myles Watsond61ada62008-10-02 19:20:22 +0000745 for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
746 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000747
Patrick Georgi7bbd7f22010-11-07 18:20:51 +0000748#if CONFIG_K8_ALLOCATE_IO_RANGE
Jason Schildt043b4092005-08-10 15:16:44 +0000749 /* io range allocation */
750 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc4 + ht_c_num * 8, 0);
751 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc0 + ht_c_num * 8, 0);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000752#endif
Myles Watsond61ada62008-10-02 19:20:22 +0000753 }
754
755 for(nodeid=0; nodeid<nodes; nodeid++) {
756 device_t dev;
757 uint8_t linkn;
758 dev = PCI_DEV(0, 0x18+nodeid,0);
759 for(linkn = 0; linkn<3; linkn++) {
760 unsigned regpos;
761 regpos = 0x98 + 0x20 * linkn;
762 reg = pci_read_config32(dev, regpos);
763 if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
Yinghai Lud4b278c2006-10-04 20:46:15 +0000764 print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
Myles Watsond61ada62008-10-02 19:20:22 +0000765 tempreg = 3 | (nodeid <<4) | (linkn<<8);
766 /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
767 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
768 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
769 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) { /*we got it*/
770 break;
771 }
772 }
773 if(ht_c_num == 4) break; /*used up only 4 non conherent allowed*/
774 /*update to 0xe0...*/
Yinghai Lud4b278c2006-10-04 20:46:15 +0000775 if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
Yinghai Lu1bc56542005-01-06 02:23:31 +0000776 print_linkn_in("\tbusn=", next_busn);
Myles Watsond61ada62008-10-02 19:20:22 +0000777 tempreg |= (next_busn<<16)|((next_busn+0x3f)<<24);
778 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
Jason Schildt043b4092005-08-10 15:16:44 +0000779 next_busn+=0x3f+1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000780
Patrick Georgi7bbd7f22010-11-07 18:20:51 +0000781#if CONFIG_K8_ALLOCATE_IO_RANGE
Jason Schildt043b4092005-08-10 15:16:44 +0000782 /* io range allocation */
Myles Watsond61ada62008-10-02 19:20:22 +0000783 tempreg = nodeid | (linkn<<4) | ((next_io_base+0x3)<<12); //limit
784 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4 + ht_c_num * 8, tempreg);
785 tempreg = 3 /*| ( 3<<4)*/ | (next_io_base<<12); //base :ISA and VGA ?
786 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0 + ht_c_num * 8, tempreg);
787 next_io_base += 0x3+0x1;
Jason Schildt043b4092005-08-10 15:16:44 +0000788#endif
Yinghai Lud4b278c2006-10-04 20:46:15 +0000789
Myles Watsond61ada62008-10-02 19:20:22 +0000790 }
791 }
792 /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
Jason Schildt043b4092005-08-10 15:16:44 +0000793
Myles Watsond61ada62008-10-02 19:20:22 +0000794 for(nodeid = 1; nodeid<nodes; nodeid++) {
795 int i;
796 device_t dev;
797 dev = PCI_DEV(0, 0x18+nodeid,1);
798 for(i = 0; i< 4; i++) {
799 unsigned regpos;
800 regpos = 0xe0 + i * 4;
801 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
802 pci_write_config32(dev, regpos, reg);
803 }
Jason Schildt043b4092005-08-10 15:16:44 +0000804
Patrick Georgi7bbd7f22010-11-07 18:20:51 +0000805#if CONFIG_K8_ALLOCATE_IO_RANGE
Jason Schildt043b4092005-08-10 15:16:44 +0000806 /* io range allocation */
Myles Watsond61ada62008-10-02 19:20:22 +0000807 for(i = 0; i< 4; i++) {
808 unsigned regpos;
809 regpos = 0xc4 + i * 8;
810 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
811 pci_write_config32(dev, regpos, reg);
812 }
813 for(i = 0; i< 4; i++) {
814 unsigned regpos;
815 regpos = 0xc0 + i * 8;
816 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
817 pci_write_config32(dev, regpos, reg);
818 }
Jason Schildt043b4092005-08-10 15:16:44 +0000819#endif
Myles Watsond61ada62008-10-02 19:20:22 +0000820 }
821
Yinghai Lu1bc56542005-01-06 02:23:31 +0000822 /* recount ht_c_num*/
Jason Schildt043b4092005-08-10 15:16:44 +0000823 uint8_t i=0;
Myles Watsond61ada62008-10-02 19:20:22 +0000824 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
Yinghai Lu2c956bb2004-12-17 21:08:16 +0000825 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
Myles Watsond61ada62008-10-02 19:20:22 +0000826 if(((reg & 0xf) != 0x0)) {
Yinghai Lu2c956bb2004-12-17 21:08:16 +0000827 i++;
828 }
Myles Watsond61ada62008-10-02 19:20:22 +0000829 }
Yinghai Lu2c956bb2004-12-17 21:08:16 +0000830
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000831#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000832 sysinfo->ht_c_num = i;
Myles Watsond61ada62008-10-02 19:20:22 +0000833 ht_setup_chains(i, sysinfo);
Yinghai Lud4b278c2006-10-04 20:46:15 +0000834 sysinfo->sbdn = get_sbdn(sysinfo->sbbusn);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000835#else
836 return ht_setup_chains(i);
837#endif
Yinghai Lu2c956bb2004-12-17 21:08:16 +0000838
839}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000840
Jonathan Kollasche5b75072010-10-07 23:02:06 +0000841#if CONFIG_RAMINIT_SYSINFO
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000842static int optimize_link_incoherent_ht(struct sys_info *sysinfo)
843{
Yinghai Lud4b278c2006-10-04 20:46:15 +0000844 // We need to use recorded link pair info to optimize the link
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000845 int i;
846 int reset_needed = 0;
Myles Watsond61ada62008-10-02 19:20:22 +0000847
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000848 unsigned link_pair_num = sysinfo->link_pair_num;
849
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000850 printk(BIOS_SPEW, "entering optimize_link_incoherent_ht\n");
851 printk(BIOS_SPEW, "sysinfo->link_pair_num=0x%x\n", link_pair_num);
Myles Watsond61ada62008-10-02 19:20:22 +0000852 for(i=0; i< link_pair_num; i++) {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000853 struct link_pair_st *link_pair= &sysinfo->link_pair[i];
Yinghai Lud4b278c2006-10-04 20:46:15 +0000854 reset_needed |= ht_optimize_link(link_pair->udev, link_pair->upos, link_pair->uoffs, link_pair->dev, link_pair->pos, link_pair->offs);
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000855 printk(BIOS_SPEW, "after ht_optimize_link for link pair %d, reset_needed=0x%x\n", i, reset_needed);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000856 }
857
Yinghai Lud4b278c2006-10-04 20:46:15 +0000858 reset_needed |= optimize_link_read_pointers_chain(sysinfo->ht_c_num);
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000859 printk(BIOS_SPEW, "after optimize_link_read_pointers_chain, reset_needed=0x%x\n", reset_needed);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000860
861 return reset_needed;
862
863}
864#endif
865
866
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000867