2 * (C) 2003-2004 Linux Networx
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>
15 #define NPUML 0xD9 /* Non prefetchable upper memory limit */
16 #define NPUMB 0xD8 /* Non prefetchable upper memory base */
18 static void amd8131_walk_children(struct bus *bus,
19 void (*visit)(device_t dev, void *ptr), void *ptr)
22 for(child = bus->children; child; child = child->sibling)
24 if (child->path.type != DEVICE_PATH_PCI) {
27 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
28 amd8131_walk_children(&child->link[0], visit, ptr);
34 struct amd8131_bus_info {
42 static void amd8131_count_dev(device_t dev, void *ptr)
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++;
49 if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
50 info->max_func = PCI_FUNC(dev->path.pci.devfn);
55 static void amd8131_pcix_tune_dev(device_t dev, void *ptr)
57 struct amd8131_bus_info *info = ptr;
59 unsigned status, cmd, orig_cmd;
60 unsigned max_read, max_tran;
64 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
67 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
71 /* How many siblings does this device have? */
72 sibs = info->master_devices - 1;
73 /* Count how many sibling functions this device has */
75 for(sib = dev->bus->children; sib; sib = sib->sibling) {
79 if (PCI_SLOT(sib->path.pci.devfn) != PCI_SLOT(dev->path.pci.devfn)) {
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);
90 max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
91 max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
93 /* Errata #49 don't allow 4K transactions */
98 /* Errata #37 Limit the number of split transactions to avoid starvation */
100 /* At most 2 outstanding split transactions when we have
101 * 3 or more bus master devices on the bus.
107 else if (sibs == 1) {
108 /* At most 4 outstanding split transactions when we have
109 * 2 bus master devices on the bus.
116 /* At most 8 outstanding split transactions when we have
117 * only one bus master device on the bus.
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))
128 /* Look at the number of siblings and compute the
129 * largest legal read size.
131 if (sib_funcs == 0) {
135 else if (sib_funcs <= 1) {
143 if (max_read > limit_read) {
144 max_read = limit_read;
146 /* Look at the read size and the nubmer of siblings
147 * and compute how many outstanding transactions I can have.
152 /* Only 1 outstanding transaction allowed */
156 else if (max_read == 1) {
158 if (max_tran > (1 - sib_funcs)) {
159 /* At most 2 outstanding transactions */
160 max_tran = 1 - sib_funcs;
166 if (max_tran > (2 - sib_funcs)) {
167 /* At most 3 outstanding transactions */
168 max_tran = 2 - sib_funcs;
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);
176 if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
177 cmd &= ~PCI_X_CMD_MAX_READ;
178 cmd |= max_read << 2;
180 if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
181 cmd &= ~PCI_X_CMD_MAX_SPLIT;
182 cmd |= max_tran << 4;
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.
190 cmd &= ~PCI_X_CMD_ERO;
191 if (orig_cmd != cmd) {
192 pci_write_config16(dev, cap + PCI_X_CMD, cmd);
195 static unsigned int amd8131_scan_bus(struct bus *bus,
196 unsigned min_devfn, unsigned max_devfn, unsigned int max)
198 struct amd8131_bus_info info;
203 /* Find the children on the bus */
204 max = pci_scan_bus(bus, min_devfn, max_devfn, max);
206 /* Find the revision of the 8131 */
207 info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
209 /* See which errata apply */
210 info.errata_56 = info.rev <= 0x12;
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);
216 /* Print the PCI-X bus speed */
217 printk(BIOS_DEBUG, "PCI: %02x: %s\n", bus->secondary, pcix_speed(info.sstatus));
220 /* Examine the bus and find out how loaded it is */
222 info.master_devices = 0;
223 amd8131_walk_children(bus, amd8131_count_dev, &info);
225 /* Disable the bus if there are no devices on it or
226 * we are running at 133Mhz and have a 4 function device.
229 if (!bus->children ||
231 (info.max_func >= 3) &&
232 (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_MODE1_133MHZ)))
235 /* Disable all of my children */
236 disable_children(bus);
238 /* Remember the device is disabled */
239 bus->dev->enabled = 0;
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);
249 /* If we are in conventional PCI mode nothing more is necessary.
251 if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
256 /* Tune the devices on the bus */
257 amd8131_walk_children(bus, amd8131_pcix_tune_dev, &info);
259 /* Don't allow the 8131 or any of it's parent busses to
260 * implement relaxed ordering. Errata #58
262 for(pbus = bus; !pbus->disable_relaxed_ordering; pbus = pbus->dev->bus) {
263 printk(BIOS_SPEW, "%s disabling relaxed ordering\n",
265 pbus->disable_relaxed_ordering = 1;
270 static unsigned int amd8131_scan_bridge(device_t dev, unsigned int max)
272 return do_pci_scan_bridge(dev, max, amd8131_scan_bus);
276 static void amd8131_pcix_init(device_t dev)
283 /* Enable memory write and invalidate ??? */
284 byte = pci_read_config8(dev, 0x04);
286 pci_write_config8(dev, 0x04, byte);
288 /* Set drive strength */
289 word = pci_read_config16(dev, 0xe0);
291 pci_write_config16(dev, 0xe0, word);
292 word = pci_read_config16(dev, 0xe4);
294 pci_write_config16(dev, 0xe4, word);
297 word = pci_read_config16(dev, 0xe8);
299 pci_write_config16(dev, 0xe8, word);
301 /* Set discard unrequested prefetch data */
303 word = pci_read_config16(dev, 0x4c);
305 pci_write_config16(dev, 0x4c, word);
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);
313 /* Set up error reporting, enable all */
314 /* system error enable */
315 dword = pci_read_config32(dev, 0x04);
317 pci_write_config32(dev, 0x04, dword);
319 /* system and error parity enable */
320 dword = pci_read_config32(dev, 0x3c);
322 pci_write_config32(dev, 0x3c, dword);
325 nmi_option = NMI_OFF;
326 get_option(&nmi_option, "nmi");
328 dword = pci_read_config32(dev, 0x44);
330 pci_write_config32(dev, 0x44, dword);
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);
338 pci_write_config32(dev, 0xc4, dword);
339 dword = pci_read_config32(dev, 0xc8);
341 pci_write_config32(dev, 0xc8, dword);
346 #define BRIDGE_40_BIT_SUPPORT 0
347 #if BRIDGE_40_BIT_SUPPORT
348 static void bridge_read_resources(struct device *dev)
350 struct resource *res;
351 pci_bus_read_resources(dev);
352 res = find_resource(dev, PCI_MEMORY_BASE);
354 res->limit = 0xffffffffffULL;
358 static void bridge_set_resources(struct device *dev)
360 struct resource *res;
361 res = find_resource(dev, PCI_MEMORY_BASE);
363 resource_t base, end;
364 /* set the memory range */
365 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
366 res->flags |= IORESOURCE_STORED;
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);
374 report_resource_stored(dev, res, "");
376 pci_dev_set_resources(dev);
378 #endif /* BRIDGE_40_BIT_SUPPORT */
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,
385 .read_resources = pci_bus_read_resources,
386 .set_resources = pci_dev_set_resources,
388 .enable_resources = pci_bus_enable_resources,
389 .init = amd8131_pcix_init,
390 .scan_bus = amd8131_scan_bridge,
391 .reset_bus = pci_bus_reset,
394 static const struct pci_driver pcix_driver __pci_driver = {
396 .vendor = PCI_VENDOR_ID_AMD,
401 static void ioapic_enable(device_t dev)
405 value = pci_read_config32(dev, 0x44);
407 value |= ((1 << 1) | (1 << 0));
409 value &= ~((1 << 1) | (1 << 0));
411 pci_write_config32(dev, 0x44, value);
414 static struct pci_operations pci_ops_pci_dev = {
415 .set_subsystem = pci_dev_set_subsystem,
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,
423 .enable = ioapic_enable,
424 .ops_pci = &pci_ops_pci_dev,
427 static const struct pci_driver ioapic_driver __pci_driver = {
429 .vendor = PCI_VENDOR_ID_AMD,