Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / southbridge / amd / amd8131 / amd8131_bridge.c
1 /*
2  * (C) 2003-2004 Linux Networx
3  */
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pci.h>
7 #include <device/pci_ids.h>
8 #include <device/pci_ops.h>
9 #include <pc80/mc146818rtc.h>
10 #include <device/pci_def.h>
11 #include <device/pcix.h>
12
13 #define NMI_OFF 0
14
15 #define NPUML 0xD9      /* Non prefetchable upper memory limit */
16 #define NPUMB 0xD8      /* Non prefetchable upper memory base */
17
18 static void amd8131_walk_children(struct bus *bus,
19         void (*visit)(device_t dev, void *ptr), void *ptr)
20 {
21         device_t child;
22         for(child = bus->children; child; child = child->sibling)
23         {
24                 if (child->path.type != DEVICE_PATH_PCI) {
25                         continue;
26                 }
27                 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
28                         amd8131_walk_children(&child->link[0], visit, ptr);
29                 }
30                 visit(child, ptr);
31         }
32 }
33
34 struct amd8131_bus_info {
35         unsigned sstatus;
36         unsigned rev;
37         int errata_56;
38         int master_devices;
39         int max_func;
40 };
41
42 static void amd8131_count_dev(device_t dev, void *ptr)
43 {
44         struct amd8131_bus_info *info = ptr;
45         /* Don't count pci bridges */
46         if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
47                 info->master_devices++;
48         }
49         if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
50                 info->max_func = PCI_FUNC(dev->path.pci.devfn);
51         }
52 }
53
54
55 static void amd8131_pcix_tune_dev(device_t dev, void *ptr)
56 {
57         struct amd8131_bus_info *info = ptr;
58         unsigned cap;
59         unsigned status, cmd, orig_cmd;
60         unsigned max_read, max_tran;
61         int sib_funcs, sibs;
62         device_t sib;
63
64         if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
65                 return;
66         }
67         cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
68         if (!cap) {
69                 return;
70         }
71         /* How many siblings does this device have? */
72         sibs = info->master_devices - 1;
73         /* Count how many sibling functions this device has */
74         sib_funcs = 0;
75         for(sib = dev->bus->children; sib; sib = sib->sibling) {
76                 if (sib == dev) {
77                         continue;
78                 }
79                 if (PCI_SLOT(sib->path.pci.devfn) != PCI_SLOT(dev->path.pci.devfn)) {
80                         continue;
81                 }
82                 sib_funcs++;
83         }
84
85
86         printk(BIOS_DEBUG, "%s AMD8131 PCI-X tuning\n", dev_path(dev));
87         status = pci_read_config32(dev, cap + PCI_X_STATUS);
88         orig_cmd = cmd = pci_read_config16(dev,cap + PCI_X_CMD);
89
90         max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
91         max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
92
93         /* Errata #49 don't allow 4K transactions */
94         if (max_read >= 2) {
95                 max_read = 2;
96         }
97
98         /* Errata #37 Limit the number of split transactions to avoid starvation */
99         if (sibs >= 2) {
100                 /* At most 2 outstanding split transactions when we have
101                  * 3 or more bus master devices on the bus.
102                  */
103                 if (max_tran > 1) {
104                         max_tran = 1;
105                 }
106         }
107         else if (sibs == 1) {
108                 /* At most 4 outstanding split transactions when we have
109                  * 2 bus master devices on the bus.
110                  */
111                 if (max_tran > 3) {
112                         max_tran = 3;
113                 }
114         }
115         else {
116                 /* At most 8 outstanding split transactions when we have
117                  * only one bus master device on the bus.
118                  */
119                 if (max_tran > 4) {
120                         max_tran = 4;
121                 }
122         }
123         /* Errata #56 additional limits when the bus runs at 133Mhz */
124         if (info->errata_56 &&
125                 (PCI_X_SSTATUS_MFREQ(info->sstatus) == PCI_X_SSTATUS_MODE1_133MHZ))
126         {
127                 unsigned limit_read;
128                 /* Look at the number of siblings and compute the
129                  * largest legal read size.
130                  */
131                 if (sib_funcs == 0) {
132                         /* 2k reads */
133                         limit_read = 2;
134                 }
135                 else if (sib_funcs <= 1) {
136                         /* 1k reads */
137                         limit_read = 1;
138                 }
139                 else {
140                         /* 512 byte reads */
141                         limit_read = 0;
142                 }
143                 if (max_read > limit_read) {
144                         max_read = limit_read;
145                 }
146                 /* Look at the read size and the nubmer of siblings
147                  * and compute how many outstanding transactions I can have.
148                  */
149                 if (max_read == 2) {
150                         /* 2K reads */
151                         if (max_tran > 0) {
152                                 /* Only 1 outstanding transaction allowed */
153                                 max_tran = 0;
154                         }
155                 }
156                 else if (max_read == 1) {
157                         /* 1K reads */
158                         if (max_tran > (1 - sib_funcs)) {
159                                 /* At most 2 outstanding transactions */
160                                 max_tran = 1 - sib_funcs;
161                         }
162                 }
163                 else {
164                         /* 512 byte reads */
165                         max_read = 0;
166                         if (max_tran > (2 - sib_funcs)) {
167                                 /* At most 3 outstanding transactions */
168                                 max_tran = 2 - sib_funcs;
169                         }
170                 }
171         }
172 #if 0
173         printk(BIOS_DEBUG, "%s max_read: %d max_tran: %d sibs: %d sib_funcs: %d\n",
174                 dev_path(dev), max_read, max_tran, sibs, sib_funcs, sib_funcs);
175 #endif
176         if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
177                 cmd &= ~PCI_X_CMD_MAX_READ;
178                 cmd |= max_read << 2;
179                 }
180         if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
181                 cmd &= ~PCI_X_CMD_MAX_SPLIT;
182                 cmd |= max_tran << 4;
183         }
184
185         /* Don't attempt to handle PCI-X errors */
186         cmd &= ~PCI_X_CMD_DPERR_E;
187         /* The 8131 does not work properly with relax ordering enabled.
188          * Errata #58
189          */
190         cmd &= ~PCI_X_CMD_ERO;
191         if (orig_cmd != cmd) {
192                 pci_write_config16(dev, cap + PCI_X_CMD, cmd);
193         }
194 }
195 static unsigned int amd8131_scan_bus(struct bus *bus,
196         unsigned min_devfn, unsigned max_devfn, unsigned int max)
197 {
198         struct amd8131_bus_info info;
199         struct bus *pbus;
200         unsigned pos;
201
202
203         /* Find the children on the bus */
204         max = pci_scan_bus(bus, min_devfn, max_devfn, max);
205
206         /* Find the revision of the 8131 */
207         info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
208
209         /* See which errata apply */
210         info.errata_56 = info.rev <= 0x12;
211
212         /* Find the pcix capability and get the secondary bus status */
213         pos = pci_find_capability(bus->dev, PCI_CAP_ID_PCIX);
214         info.sstatus = pci_read_config16(bus->dev, pos + PCI_X_SEC_STATUS);
215
216         /* Print the PCI-X bus speed */
217         printk(BIOS_DEBUG, "PCI: %02x: %s\n", bus->secondary, pcix_speed(info.sstatus));
218
219
220         /* Examine the bus and find out how loaded it is */
221         info.max_func = 0;
222         info.master_devices  = 0;
223         amd8131_walk_children(bus, amd8131_count_dev, &info);
224
225         /* Disable the bus if there are no devices on it or
226          * we are running at 133Mhz and have a 4 function device.
227          * see errata #56
228          */
229         if (!bus->children ||
230                 (info.errata_56 &&
231                         (info.max_func >= 3) &&
232                         (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_MODE1_133MHZ)))
233         {
234                 unsigned pcix_misc;
235                 /* Disable all of my children */
236                 disable_children(bus);
237
238                 /* Remember the device is disabled */
239                 bus->dev->enabled = 0;
240
241                 /* Disable the PCI-X clocks */
242                 pcix_misc = pci_read_config32(bus->dev, 0x40);
243                 pcix_misc &= ~(0x1f << 16);
244                 pci_write_config32(bus->dev, 0x40, pcix_misc);
245
246                 return max;
247         }
248
249         /* If we are in conventional PCI mode nothing more is necessary.
250          */
251         if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
252                 return max;
253         }
254
255
256         /* Tune the devices on the bus */
257         amd8131_walk_children(bus, amd8131_pcix_tune_dev, &info);
258
259         /* Don't allow the 8131 or any of it's parent busses to
260          * implement relaxed ordering.  Errata #58
261          */
262         for(pbus = bus; !pbus->disable_relaxed_ordering; pbus = pbus->dev->bus) {
263                 printk(BIOS_SPEW, "%s disabling relaxed ordering\n",
264                         bus_path(pbus));
265                 pbus->disable_relaxed_ordering = 1;
266         }
267         return max;
268 }
269
270 static unsigned int amd8131_scan_bridge(device_t dev, unsigned int max)
271 {
272         return do_pci_scan_bridge(dev, max, amd8131_scan_bus);
273 }
274
275
276 static void amd8131_pcix_init(device_t dev)
277 {
278         uint32_t dword;
279         uint16_t word;
280         uint8_t byte;
281         int nmi_option;
282
283         /* Enable memory write and invalidate ??? */
284         byte = pci_read_config8(dev, 0x04);
285         byte |= 0x10;
286         pci_write_config8(dev, 0x04, byte);
287
288         /* Set drive strength */
289         word = pci_read_config16(dev, 0xe0);
290         word = 0x0404;
291         pci_write_config16(dev, 0xe0, word);
292         word = pci_read_config16(dev, 0xe4);
293         word = 0x0404;
294         pci_write_config16(dev, 0xe4, word);
295
296         /* Set impedance */
297         word = pci_read_config16(dev, 0xe8);
298         word = 0x0404;
299         pci_write_config16(dev, 0xe8, word);
300
301         /* Set discard unrequested prefetch data */
302         /* Errata #51 */
303         word = pci_read_config16(dev, 0x4c);
304         word |= 1;
305         pci_write_config16(dev, 0x4c, word);
306
307         /* Set split transaction limits */
308         word = pci_read_config16(dev, 0xa8);
309         pci_write_config16(dev, 0xaa, word);
310         word = pci_read_config16(dev, 0xac);
311         pci_write_config16(dev, 0xae, word);
312
313         /* Set up error reporting, enable all */
314         /* system error enable */
315         dword = pci_read_config32(dev, 0x04);
316         dword |= (1<<8);
317         pci_write_config32(dev, 0x04, dword);
318
319         /* system and error parity enable */
320         dword = pci_read_config32(dev, 0x3c);
321         dword |= (3<<16);
322         pci_write_config32(dev, 0x3c, dword);
323
324         /* NMI enable */
325         nmi_option = NMI_OFF;
326         get_option(&nmi_option, "nmi");
327         if(nmi_option) {
328                 dword = pci_read_config32(dev, 0x44);
329                 dword |= (1<<0);
330                 pci_write_config32(dev, 0x44, dword);
331         }
332
333         /* Set up CRC flood enable */
334         dword = pci_read_config32(dev, 0xc0);
335         if(dword) {  /* do device A only */
336                 dword = pci_read_config32(dev, 0xc4);
337                 dword |= (1<<1);
338                 pci_write_config32(dev, 0xc4, dword);
339                 dword = pci_read_config32(dev, 0xc8);
340                 dword |= (1<<1);
341                 pci_write_config32(dev, 0xc8, dword);
342         }
343         return;
344 }
345
346 #define BRIDGE_40_BIT_SUPPORT 0
347 #if BRIDGE_40_BIT_SUPPORT
348 static void bridge_read_resources(struct device *dev)
349 {
350         struct resource *res;
351         pci_bus_read_resources(dev);
352         res = find_resource(dev, PCI_MEMORY_BASE);
353         if (res) {
354                 res->limit = 0xffffffffffULL;
355         }
356 }
357
358 static void bridge_set_resources(struct device *dev)
359 {
360         struct resource *res;
361         res = find_resource(dev, PCI_MEMORY_BASE);
362         if (res) {
363                 resource_t base, end;
364                 /* set the memory range */
365                 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
366                 res->flags |= IORESOURCE_STORED;
367                 base = res->base;
368                 end  = resource_end(res);
369                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
370                 pci_write_config8(dev, NPUML, (base >> 32) & 0xff);
371                 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
372                 pci_write_config8(dev, NPUMB, (end >> 32) & 0xff);
373
374                 report_resource_stored(dev, res, "");
375         }
376         pci_dev_set_resources(dev);
377 }
378 #endif /* BRIDGE_40_BIT_SUPPORT */
379
380 static struct device_operations pcix_ops  = {
381 #if BRIDGE_40_BIT_SUPPORT
382         .read_resources   = bridge_read_resources,
383         .set_resources    = bridge_set_resources,
384 #else
385         .read_resources   = pci_bus_read_resources,
386         .set_resources    = pci_dev_set_resources,
387 #endif
388         .enable_resources = pci_bus_enable_resources,
389         .init             = amd8131_pcix_init,
390         .scan_bus         = amd8131_scan_bridge,
391         .reset_bus        = pci_bus_reset,
392 };
393
394 static const struct pci_driver pcix_driver __pci_driver = {
395         .ops    = &pcix_ops,
396         .vendor = PCI_VENDOR_ID_AMD,
397         .device = 0x7450,
398 };
399
400
401 static void ioapic_enable(device_t dev)
402 {
403         uint32_t value;
404
405         value = pci_read_config32(dev, 0x44);
406         if (dev->enabled) {
407                 value |= ((1 << 1) | (1 << 0));
408         } else {
409                 value &= ~((1 << 1) | (1 << 0));
410         }
411         pci_write_config32(dev, 0x44, value);
412 }
413
414 static struct pci_operations pci_ops_pci_dev = {
415         .set_subsystem    = pci_dev_set_subsystem,
416 };
417 static struct device_operations ioapic_ops = {
418         .read_resources   = pci_dev_read_resources,
419         .set_resources    = pci_dev_set_resources,
420         .enable_resources = pci_dev_enable_resources,
421         .init             = 0,
422         .scan_bus         = 0,
423         .enable           = ioapic_enable,
424         .ops_pci          = &pci_ops_pci_dev,
425 };
426
427 static const struct pci_driver ioapic_driver __pci_driver = {
428         .ops    = &ioapic_ops,
429         .vendor = PCI_VENDOR_ID_AMD,
430         .device = 0x7451,
431
432 };