1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <types.h>
21#include <lib.h>
22#include <console.h>
23#include <device/pci.h>
24#include <mc146818rtc.h>
25#include <device/pcix.h>
26#include <device/pci_ids.h>
27
28#define NMI_OFF 0
29
30static void amd8131_walk_children(struct bus *bus,
31 void (*visit)(struct device * dev, void *ptr), void *ptr)
32{
33 struct device * child;
34 for(child = bus->children; child; child = child->sibling)
35 {
36 if (child->path.type != DEVICE_PATH_PCI) {
37 continue;
38 }
39 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
40 amd8131_walk_children(&child->link[0], visit, ptr);
41 }
42 visit(child, ptr);
43 }
44}
45
46struct amd8131_bus_info {
47 unsigned sstatus;
48 unsigned rev;
49 int errata_56;
50 int master_devices;
51 int max_func;
52};
53
54static void amd8131_count_dev(struct device * dev, void *ptr)
55{
56 struct amd8131_bus_info *info = ptr;
57
58 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
59 info->master_devices++;
60 }
61 if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
62 info->max_func = PCI_FUNC(dev->path.pci.devfn);
63 }
64}
65
66
67static void amd8131_pcix_tune_dev(struct device * dev, void *ptr)
68{
69 struct amd8131_bus_info *info = ptr;
70 unsigned cap;
71 unsigned status, cmd, orig_cmd;
72 unsigned max_read, max_tran;
73 int sib_funcs, sibs;
74 struct device * sib;
75
76 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
77 return;
78 }
79 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
80 if (!cap) {
81 return;
82 }
83
84 sibs = info->master_devices - 1;
85
86 sib_funcs = 0;
87 for(sib = dev->bus->children; sib; sib = sib->sibling) {
88 if (sib == dev) {
89 continue;
90 }
91 if (PCI_SLOT(sib->path.pci.devfn) != PCI_SLOT(dev->path.pci.devfn)) {
92 continue;
93 }
94 sib_funcs++;
95 }
96
97 printk(BIOS_DEBUG,"%s AMD8131 PCI-X tuning\n", dev_path(dev));
98 status = pci_read_config32(dev, cap + PCI_X_STATUS);
99 orig_cmd = cmd = pci_read_config16(dev,cap + PCI_X_CMD);
100
101 max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
102 max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
103
104
105 if (max_read >= 2) {
106 max_read = 2;
107 }
108
109
110 if (sibs >= 2) {
111
112
113
114 if (max_tran > 1) {
115 max_tran = 1;
116 }
117 }
118 else if (sibs == 1) {
119
120
121
122 if (max_tran > 3) {
123 max_tran = 3;
124 }
125 }
126 else {
127
128
129
130 if (max_tran > 4) {
131 max_tran = 4;
132 }
133 }
134
135 if (info->errata_56 &&
136 (PCI_X_SSTATUS_MFREQ(info->sstatus) == PCI_X_SSTATUS_MODE1_133MHZ))
137 {
138 unsigned limit_read;
139
140
141
142 if (sib_funcs == 0) {
143
144 limit_read = 2;
145 }
146 else if (sib_funcs <= 1) {
147
148 limit_read = 1;
149 }
150 else {
151
152 limit_read = 0;
153 }
154 if (max_read > limit_read) {
155 max_read = limit_read;
156 }
157
158
159
160 if (max_read == 2) {
161
162 if (max_tran > 0) {
163
164 max_tran = 0;
165 }
166 }
167 else if (max_read == 1) {
168
169 if (max_tran > (1 - sib_funcs)) {
170
171 max_tran = 1 - sib_funcs;
172 }
173 }
174 else {
175
176 max_read = 0;
177 if (max_tran > (2 - sib_funcs)) {
178
179 max_tran = 2 - sib_funcs;
180 }
181 }
182 }
183#if 0
184 printk(BIOS_DEBUG, "%s max_read: %d max_tran: %d sibs: %d sib_funcs: %d\n",
185 dev_path(dev), max_read, max_tran, sibs, sib_funcs, sib_funcs);
186#endif
187 if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
188 cmd &= ~PCI_X_CMD_MAX_READ;
189 cmd |= max_read << 2;
190 }
191 if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
192 cmd &= ~PCI_X_CMD_MAX_SPLIT;
193 cmd |= max_tran << 4;
194 }
195
196
197 cmd &= ~PCI_X_CMD_DPERR_E;
198
199
200
201 cmd &= ~PCI_X_CMD_ERO;
202 if (orig_cmd != cmd) {
203 pci_write_config16(dev, cap + PCI_X_CMD, cmd);
204 }
205}
206static unsigned int amd8131_scan_bus(struct bus *bus,
207 unsigned min_devfn, unsigned max_devfn, unsigned int max)
208{
209 struct amd8131_bus_info info;
210 struct bus *pbus;
211 unsigned pos;
212
213
214
215 max = pci_scan_bus(bus, min_devfn, max_devfn, max);
216
217
218 info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
219
220
221 info.errata_56 = info.rev <= 0x12;
222
223
224 pos = pci_find_capability(bus->dev, PCI_CAP_ID_PCIX);
225 info.sstatus = pci_read_config16(bus->dev, pos + PCI_X_SEC_STATUS);
226
227
228 printk(BIOS_DEBUG, "PCI: %02x: %s\n", bus->secondary, pcix_speed(info.sstatus));
229
230
231
232 info.max_func = 0;
233 info.master_devices = 0;
234 amd8131_walk_children(bus, amd8131_count_dev, &info);
235
236
237
238
239
240 if (!bus->children ||
241 (info.errata_56 &&
242 (info.max_func >= 3) &&
243 (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_MODE1_133MHZ)))
244 {
245 unsigned pcix_misc;
246
247 disable_children(bus);
248
249
250 bus->dev->enabled = 0;
251
252
253 pcix_misc = pci_read_config32(bus->dev, 0x40);
254 pcix_misc &= ~(0x1f << 16);
255 pci_write_config32(bus->dev, 0x40, pcix_misc);
256
257 return max;
258 }
259
260
261
262 if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
263 return max;
264 }
265
266
267
268 amd8131_walk_children(bus, amd8131_pcix_tune_dev, &info);
269
270
271
272
273 for(pbus = bus; !pbus->disable_relaxed_ordering; pbus = pbus->dev->bus) {
274 printk(BIOS_SPEW, "%s disabling relaxed ordering\n",
275 bus_path(pbus));
276 pbus->disable_relaxed_ordering = 1;
277 }
278 return max;
279}
280
281static unsigned int amd8131_scan_bridge(struct device * dev, unsigned int max)
282{
283 return do_pci_scan_bridge(dev, max, amd8131_scan_bus);
284}
285
286
287static void amd8131_pcix_init(struct device * dev)
288{
289 u32 dword;
290 u16 word;
291 u8 byte;
292 int nmi_option;
293
294
295 byte = pci_read_config8(dev, 0x04);
296 byte |= 0x10;
297 pci_write_config8(dev, 0x04, byte);
298
299
300 word = pci_read_config16(dev, 0xe0);
301 word = 0x0404;
302 pci_write_config16(dev, 0xe0, word);
303 word = pci_read_config16(dev, 0xe4);
304 word = 0x0404;
305 pci_write_config16(dev, 0xe4, word);
306
307
308 word = pci_read_config16(dev, 0xe8);
309 word = 0x0404;
310 pci_write_config16(dev, 0xe8, word);
311
312
313
314 word = pci_read_config16(dev, 0x4c);
315 word |= 1;
316 pci_write_config16(dev, 0x4c, word);
317
318
319 word = pci_read_config16(dev, 0xa8);
320 pci_write_config16(dev, 0xaa, word);
321 word = pci_read_config16(dev, 0xac);
322 pci_write_config16(dev, 0xae, word);
323
324
325
326 dword = pci_read_config32(dev, 0x04);
327 dword |= (1<<8);
328 pci_write_config32(dev, 0x04, dword);
329
330
331 dword = pci_read_config32(dev, 0x3c);
332 dword |= (3<<16);
333 pci_write_config32(dev, 0x3c, dword);
334
335
336 nmi_option = NMI_OFF;
337 get_option(&nmi_option, "nmi");
338 if(nmi_option) {
339 dword = pci_read_config32(dev, 0x44);
340 dword |= (1<<0);
341 pci_write_config32(dev, 0x44, dword);
342 }
343
344
345 dword = pci_read_config32(dev, 0xc0);
346 if(dword) {
347 dword = pci_read_config32(dev, 0xc4);
348 dword |= (1<<1);
349 pci_write_config32(dev, 0xc4, dword);
350 dword = pci_read_config32(dev, 0xc8);
351 dword |= (1<<1);
352 pci_write_config32(dev, 0xc8, dword);
353 }
354 return;
355}
356
357struct device_operations amd8131_pcix = {
358 .id = {.type = DEVICE_ID_PCI,
359 {.pci = {.vendor = PCI_VENDOR_ID_AMD,
360 .device = PCI_DEVICE_ID_AMD_8131_PCIX}}},
361 .constructor = default_device_constructor,
362 .reset_bus = pci_bus_reset,
363 .phase3_scan = amd8131_scan_bridge,
364 .phase4_read_resources = pci_bus_read_resources,
365 .phase4_set_resources = pci_set_resources,
366 .phase5_enable_resources = pci_bus_enable_resources,
367 .phase6_init = amd8131_pcix_init,
368 .ops_pci = &pci_bus_ops_pci,
369};
370
371static void ioapic_enable(struct device * dev)
372{
373 u32 value;
374
375 value = pci_read_config32(dev, 0x44);
376 if (dev->enabled) {
377 value |= ((1 << 1) | (1 << 0));
378 } else {
379 value &= ~((1 << 1) | (1 << 0));
380 }
381 pci_write_config32(dev, 0x44, value);
382}
383
384static struct pci_operations pci_ops_pci_dev = {
385 .set_subsystem = pci_dev_set_subsystem,
386};
387
388struct device_operations amd8131_apic = {
389 .id = {.type = DEVICE_ID_PCI,
390 {.pci = {.vendor = PCI_VENDOR_ID_AMD,
391 .device = PCI_DEVICE_ID_AMD_8131_IOAPIC}}},
392 .constructor = default_device_constructor,
393 .phase3_scan = 0,
394 .phase3_chip_setup_dev = ioapic_enable,
395 .phase4_read_resources = pci_dev_read_resources,
396 .phase4_set_resources = pci_set_resources,
397 .phase5_enable_resources = pci_dev_enable_resources,
398 .ops_pci = &pci_ops_pci_dev,
399};
400