0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/export.h>
0010 #include <linux/kernel.h>
0011 #include <linux/pci.h>
0012 #include <linux/slab.h>
0013 #include <linux/init.h>
0014 #include <linux/io.h>
0015
0016 #include <asm/mach-types.h>
0017 #include <asm/mach/map.h>
0018 #include <asm/mach/pci.h>
0019
0020 static int debug_pci;
0021
0022
0023
0024
0025
0026 static void pcibios_bus_report_status(struct pci_bus *bus, u_int status_mask, int warn)
0027 {
0028 struct pci_dev *dev;
0029
0030 list_for_each_entry(dev, &bus->devices, bus_list) {
0031 u16 status;
0032
0033
0034
0035
0036
0037 if (dev->bus->number == 0 && dev->devfn == 0)
0038 continue;
0039
0040 pci_read_config_word(dev, PCI_STATUS, &status);
0041 if (status == 0xffff)
0042 continue;
0043
0044 if ((status & status_mask) == 0)
0045 continue;
0046
0047
0048 pci_write_config_word(dev, PCI_STATUS, status & status_mask);
0049
0050 if (warn)
0051 printk("(%s: %04X) ", pci_name(dev), status);
0052 }
0053
0054 list_for_each_entry(dev, &bus->devices, bus_list)
0055 if (dev->subordinate)
0056 pcibios_bus_report_status(dev->subordinate, status_mask, warn);
0057 }
0058
0059 void pcibios_report_status(u_int status_mask, int warn)
0060 {
0061 struct pci_bus *bus;
0062
0063 list_for_each_entry(bus, &pci_root_buses, node)
0064 pcibios_bus_report_status(bus, status_mask, warn);
0065 }
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 static void pci_fixup_83c553(struct pci_dev *dev)
0080 {
0081
0082
0083
0084 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_SPACE_MEMORY);
0085 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_IO);
0086
0087 dev->resource[0].end -= dev->resource[0].start;
0088 dev->resource[0].start = 0;
0089
0090
0091
0092
0093 pci_write_config_byte(dev, 0x48, 0xff);
0094
0095
0096
0097
0098
0099
0100 pci_write_config_byte(dev, 0x42, 0x01);
0101
0102
0103
0104
0105 pci_write_config_byte(dev, 0x40, 0x22);
0106
0107
0108
0109
0110
0111
0112
0113 pci_write_config_byte(dev, 0x83, 0x02);
0114
0115
0116
0117
0118
0119 pci_write_config_byte(dev, 0x80, 0x11);
0120 pci_write_config_byte(dev, 0x81, 0x00);
0121
0122
0123
0124
0125
0126 pci_write_config_word(dev, 0x44, 0xb000);
0127 outb(0x08, 0x4d1);
0128 }
0129 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_83C553, pci_fixup_83c553);
0130
0131 static void pci_fixup_unassign(struct pci_dev *dev)
0132 {
0133 dev->resource[0].end -= dev->resource[0].start;
0134 dev->resource[0].start = 0;
0135 }
0136 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_89C940F, pci_fixup_unassign);
0137
0138
0139
0140
0141
0142
0143 static void pci_fixup_dec21285(struct pci_dev *dev)
0144 {
0145 int i;
0146
0147 if (dev->devfn == 0) {
0148 dev->class &= 0xff;
0149 dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
0150 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
0151 dev->resource[i].start = 0;
0152 dev->resource[i].end = 0;
0153 dev->resource[i].flags = 0;
0154 }
0155 }
0156 }
0157 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21285, pci_fixup_dec21285);
0158
0159
0160
0161
0162 static void pci_fixup_ide_bases(struct pci_dev *dev)
0163 {
0164 struct resource *r;
0165 int i;
0166
0167 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
0168 return;
0169
0170 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
0171 r = dev->resource + i;
0172 if ((r->start & ~0x80) == 0x374) {
0173 r->start |= 2;
0174 r->end = r->start;
0175 }
0176 }
0177 }
0178 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
0179
0180
0181
0182
0183 static void pci_fixup_dec21142(struct pci_dev *dev)
0184 {
0185 pci_write_config_dword(dev, 0x40, 0x80000000);
0186 }
0187 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142, pci_fixup_dec21142);
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 static void pci_fixup_cy82c693(struct pci_dev *dev)
0206 {
0207 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
0208 u32 base0, base1;
0209
0210 if (dev->class & 0x80) {
0211 base0 = 0x1f0;
0212 base1 = 0x3f4;
0213 } else {
0214 base0 = 0x170;
0215 base1 = 0x374;
0216 }
0217
0218 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0,
0219 base0 | PCI_BASE_ADDRESS_SPACE_IO);
0220 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1,
0221 base1 | PCI_BASE_ADDRESS_SPACE_IO);
0222
0223 dev->resource[0].start = 0;
0224 dev->resource[0].end = 0;
0225 dev->resource[0].flags = 0;
0226
0227 dev->resource[1].start = 0;
0228 dev->resource[1].end = 0;
0229 dev->resource[1].flags = 0;
0230 } else if (PCI_FUNC(dev->devfn) == 0) {
0231
0232
0233
0234 pci_write_config_byte(dev, 0x4b, 14);
0235 pci_write_config_byte(dev, 0x4c, 15);
0236
0237
0238
0239
0240 pci_write_config_byte(dev, 0x4d, 0x41);
0241
0242
0243
0244
0245 pci_write_config_byte(dev, 0x44, 0x17);
0246
0247
0248
0249
0250 pci_write_config_byte(dev, 0x45, 0x03);
0251 }
0252 }
0253 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, pci_fixup_cy82c693);
0254
0255
0256
0257
0258
0259 static inline int pdev_bad_for_parity(struct pci_dev *dev)
0260 {
0261 return ((dev->vendor == PCI_VENDOR_ID_INTERG &&
0262 (dev->device == PCI_DEVICE_ID_INTERG_2000 ||
0263 dev->device == PCI_DEVICE_ID_INTERG_2010)) ||
0264 (dev->vendor == PCI_VENDOR_ID_ITE &&
0265 dev->device == PCI_DEVICE_ID_ITE_8152));
0266
0267 }
0268
0269
0270
0271
0272
0273 void pcibios_fixup_bus(struct pci_bus *bus)
0274 {
0275 struct pci_dev *dev;
0276 u16 features = PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_FAST_BACK;
0277
0278
0279
0280
0281
0282 list_for_each_entry(dev, &bus->devices, bus_list) {
0283 u16 status;
0284
0285 pci_read_config_word(dev, PCI_STATUS, &status);
0286
0287
0288
0289
0290
0291
0292
0293 if (!(status & PCI_STATUS_FAST_BACK))
0294 features &= ~PCI_COMMAND_FAST_BACK;
0295
0296 if (pdev_bad_for_parity(dev))
0297 features &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
0298
0299 switch (dev->class >> 8) {
0300 case PCI_CLASS_BRIDGE_PCI:
0301 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &status);
0302 status |= PCI_BRIDGE_CTL_PARITY|PCI_BRIDGE_CTL_MASTER_ABORT;
0303 status &= ~(PCI_BRIDGE_CTL_BUS_RESET|PCI_BRIDGE_CTL_FAST_BACK);
0304 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, status);
0305 break;
0306
0307 case PCI_CLASS_BRIDGE_CARDBUS:
0308 pci_read_config_word(dev, PCI_CB_BRIDGE_CONTROL, &status);
0309 status |= PCI_CB_BRIDGE_CTL_PARITY|PCI_CB_BRIDGE_CTL_MASTER_ABORT;
0310 pci_write_config_word(dev, PCI_CB_BRIDGE_CONTROL, status);
0311 break;
0312 }
0313 }
0314
0315
0316
0317
0318 list_for_each_entry(dev, &bus->devices, bus_list) {
0319 u16 cmd;
0320
0321 pci_read_config_word(dev, PCI_COMMAND, &cmd);
0322 cmd |= features;
0323 pci_write_config_word(dev, PCI_COMMAND, cmd);
0324
0325 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
0326 L1_CACHE_BYTES >> 2);
0327 }
0328
0329
0330
0331
0332 if (bus->self && bus->self->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
0333 if (features & PCI_COMMAND_FAST_BACK)
0334 bus->bridge_ctl |= PCI_BRIDGE_CTL_FAST_BACK;
0335 if (features & PCI_COMMAND_PARITY)
0336 bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY;
0337 }
0338
0339
0340
0341
0342 pr_info("PCI: bus%d: Fast back to back transfers %sabled\n",
0343 bus->number, (features & PCI_COMMAND_FAST_BACK) ? "en" : "dis");
0344 }
0345 EXPORT_SYMBOL(pcibios_fixup_bus);
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362 static u8 pcibios_swizzle(struct pci_dev *dev, u8 *pin)
0363 {
0364 struct pci_sys_data *sys = dev->sysdata;
0365 int slot, oldpin = *pin;
0366
0367 if (sys->swizzle)
0368 slot = sys->swizzle(dev, pin);
0369 else
0370 slot = pci_common_swizzle(dev, pin);
0371
0372 if (debug_pci)
0373 printk("PCI: %s swizzling pin %d => pin %d slot %d\n",
0374 pci_name(dev), oldpin, *pin, slot);
0375
0376 return slot;
0377 }
0378
0379
0380
0381
0382 static int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
0383 {
0384 struct pci_sys_data *sys = dev->sysdata;
0385 int irq = -1;
0386
0387 if (sys->map_irq)
0388 irq = sys->map_irq(dev, slot, pin);
0389
0390 if (debug_pci)
0391 printk("PCI: %s mapping slot %d pin %d => irq %d\n",
0392 pci_name(dev), slot, pin, irq);
0393
0394 return irq;
0395 }
0396
0397 static int pcibios_init_resource(int busnr, struct pci_sys_data *sys)
0398 {
0399 int ret;
0400 struct resource_entry *window;
0401
0402 if (list_empty(&sys->resources)) {
0403 pci_add_resource_offset(&sys->resources,
0404 &iomem_resource, sys->mem_offset);
0405 }
0406
0407 resource_list_for_each_entry(window, &sys->resources)
0408 if (resource_type(window->res) == IORESOURCE_IO)
0409 return 0;
0410
0411 sys->io_res.start = (busnr * SZ_64K) ? : pcibios_min_io;
0412 sys->io_res.end = (busnr + 1) * SZ_64K - 1;
0413 sys->io_res.flags = IORESOURCE_IO;
0414 sys->io_res.name = sys->io_res_name;
0415 sprintf(sys->io_res_name, "PCI%d I/O", busnr);
0416
0417 ret = request_resource(&ioport_resource, &sys->io_res);
0418 if (ret) {
0419 pr_err("PCI: unable to allocate I/O port region (%d)\n", ret);
0420 return ret;
0421 }
0422 pci_add_resource_offset(&sys->resources, &sys->io_res,
0423 sys->io_offset);
0424
0425 return 0;
0426 }
0427
0428 static void pcibios_init_hw(struct device *parent, struct hw_pci *hw,
0429 struct list_head *head)
0430 {
0431 struct pci_sys_data *sys = NULL;
0432 int ret;
0433 int nr, busnr;
0434
0435 for (nr = busnr = 0; nr < hw->nr_controllers; nr++) {
0436 struct pci_host_bridge *bridge;
0437
0438 bridge = pci_alloc_host_bridge(sizeof(struct pci_sys_data));
0439 if (WARN(!bridge, "PCI: unable to allocate bridge!"))
0440 break;
0441
0442 sys = pci_host_bridge_priv(bridge);
0443
0444 sys->busnr = busnr;
0445 sys->swizzle = hw->swizzle;
0446 sys->map_irq = hw->map_irq;
0447 INIT_LIST_HEAD(&sys->resources);
0448
0449 if (hw->private_data)
0450 sys->private_data = hw->private_data[nr];
0451
0452 ret = hw->setup(nr, sys);
0453
0454 if (ret > 0) {
0455
0456 ret = pcibios_init_resource(nr, sys);
0457 if (ret) {
0458 pci_free_host_bridge(bridge);
0459 break;
0460 }
0461
0462 bridge->map_irq = pcibios_map_irq;
0463 bridge->swizzle_irq = pcibios_swizzle;
0464
0465 if (hw->scan)
0466 ret = hw->scan(nr, bridge);
0467 else {
0468 list_splice_init(&sys->resources,
0469 &bridge->windows);
0470 bridge->dev.parent = parent;
0471 bridge->sysdata = sys;
0472 bridge->busnr = sys->busnr;
0473 bridge->ops = hw->ops;
0474
0475 ret = pci_scan_root_bus_bridge(bridge);
0476 }
0477
0478 if (WARN(ret < 0, "PCI: unable to scan bus!")) {
0479 pci_free_host_bridge(bridge);
0480 break;
0481 }
0482
0483 sys->bus = bridge->bus;
0484
0485 busnr = sys->bus->busn_res.end + 1;
0486
0487 list_add(&sys->node, head);
0488 } else {
0489 pci_free_host_bridge(bridge);
0490 if (ret < 0)
0491 break;
0492 }
0493 }
0494 }
0495
0496 void pci_common_init_dev(struct device *parent, struct hw_pci *hw)
0497 {
0498 struct pci_sys_data *sys;
0499 LIST_HEAD(head);
0500
0501 pci_add_flags(PCI_REASSIGN_ALL_BUS);
0502 if (hw->preinit)
0503 hw->preinit();
0504 pcibios_init_hw(parent, hw, &head);
0505 if (hw->postinit)
0506 hw->postinit();
0507
0508 list_for_each_entry(sys, &head, node) {
0509 struct pci_bus *bus = sys->bus;
0510
0511
0512
0513
0514
0515
0516 if (pci_has_flag(PCI_PROBE_ONLY)) {
0517 pci_bus_claim_resources(bus);
0518 } else {
0519 struct pci_bus *child;
0520
0521 pci_bus_size_bridges(bus);
0522 pci_bus_assign_resources(bus);
0523
0524 list_for_each_entry(child, &bus->children, node)
0525 pcie_bus_configure_settings(child);
0526 }
0527
0528 pci_bus_add_devices(bus);
0529 }
0530 }
0531
0532 #ifndef CONFIG_PCI_HOST_ITE8152
0533 void pcibios_set_master(struct pci_dev *dev)
0534 {
0535
0536 }
0537 #endif
0538
0539 char * __init pcibios_setup(char *str)
0540 {
0541 if (!strcmp(str, "debug")) {
0542 debug_pci = 1;
0543 return NULL;
0544 }
0545 return str;
0546 }
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
0564 resource_size_t size, resource_size_t align)
0565 {
0566 struct pci_dev *dev = data;
0567 resource_size_t start = res->start;
0568 struct pci_host_bridge *host_bridge;
0569
0570 if (res->flags & IORESOURCE_IO && start & 0x300)
0571 start = (start + 0x3ff) & ~0x3ff;
0572
0573 start = (start + align - 1) & ~(align - 1);
0574
0575 host_bridge = pci_find_host_bridge(dev->bus);
0576
0577 if (host_bridge->align_resource)
0578 return host_bridge->align_resource(dev, res,
0579 start, size, align);
0580
0581 return start;
0582 }
0583
0584 void __init pci_map_io_early(unsigned long pfn)
0585 {
0586 struct map_desc pci_io_desc = {
0587 .virtual = PCI_IO_VIRT_BASE,
0588 .type = MT_DEVICE,
0589 .length = SZ_64K,
0590 };
0591
0592 pci_io_desc.pfn = pfn;
0593 iotable_init(&pci_io_desc, 1);
0594 }