Paul Mundt | 4c5107e | 2009-04-20 15:43:36 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * New-style PCI core. |
| 3 | * |
| 4 | * Copyright (c) 2002 M. R. Brown |
| 5 | * Copyright (c) 2004 - 2009 Paul Mundt |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/pci.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/dma-debug.h> |
| 15 | #include <linux/io.h> |
| 16 | |
| 17 | static int __init pcibios_init(void) |
| 18 | { |
| 19 | struct pci_channel *p; |
| 20 | struct pci_bus *bus; |
| 21 | int busno; |
| 22 | |
| 23 | /* init channels */ |
| 24 | busno = 0; |
| 25 | for (p = board_pci_channels; p->init; p++) { |
| 26 | if (p->init(p) == 0) |
| 27 | p->enabled = 1; |
| 28 | else |
| 29 | pr_err("Unable to init pci channel %d\n", busno); |
| 30 | busno++; |
| 31 | } |
| 32 | |
| 33 | /* scan the buses */ |
| 34 | busno = 0; |
| 35 | for (p = board_pci_channels; p->init; p++) { |
| 36 | if (p->enabled) { |
| 37 | bus = pci_scan_bus(busno, p->pci_ops, p); |
| 38 | busno = bus->subordinate + 1; |
| 39 | |
| 40 | pci_bus_size_bridges(bus); |
| 41 | pci_bus_assign_resources(bus); |
| 42 | pci_enable_bridges(bus); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq); |
| 47 | |
| 48 | dma_debug_add_bus(&pci_bus_type); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | subsys_initcall(pcibios_init); |
| 53 | |
| 54 | static void pcibios_fixup_device_resources(struct pci_dev *dev, |
| 55 | struct pci_bus *bus) |
| 56 | { |
| 57 | /* Update device resources. */ |
| 58 | struct pci_channel *chan = bus->sysdata; |
| 59 | unsigned long offset = 0; |
| 60 | int i; |
| 61 | |
| 62 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 63 | if (!dev->resource[i].start) |
| 64 | continue; |
| 65 | if (dev->resource[i].flags & IORESOURCE_PCI_FIXED) |
| 66 | continue; |
| 67 | if (dev->resource[i].flags & IORESOURCE_IO) |
| 68 | offset = chan->io_base; |
| 69 | else if (dev->resource[i].flags & IORESOURCE_MEM) |
| 70 | offset = 0; |
| 71 | |
| 72 | dev->resource[i].start += offset; |
| 73 | dev->resource[i].end += offset; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * Called after each bus is probed, but before its children |
| 80 | * are examined. |
| 81 | */ |
| 82 | void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus) |
| 83 | { |
| 84 | struct pci_dev *dev = bus->self; |
| 85 | struct list_head *ln; |
| 86 | struct pci_channel *chan = bus->sysdata; |
| 87 | |
| 88 | if (!dev) { |
| 89 | bus->resource[0] = chan->io_resource; |
| 90 | bus->resource[1] = chan->mem_resource; |
| 91 | } |
| 92 | |
| 93 | for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) { |
| 94 | dev = pci_dev_b(ln); |
| 95 | |
| 96 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 97 | pcibios_fixup_device_resources(dev, bus); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 102 | struct resource *res) |
| 103 | { |
| 104 | struct pci_channel *chan = dev->sysdata; |
| 105 | unsigned long offset = 0; |
| 106 | |
| 107 | if (res->flags & IORESOURCE_IO) |
| 108 | offset = chan->io_base; |
| 109 | else if (res->flags & IORESOURCE_MEM) |
| 110 | offset = 0; |
| 111 | |
| 112 | region->start = res->start - offset; |
| 113 | region->end = res->end - offset; |
| 114 | } |
| 115 | |
| 116 | void __devinit |
| 117 | pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 118 | struct pci_bus_region *region) |
| 119 | { |
| 120 | struct pci_channel *chan = dev->sysdata; |
| 121 | unsigned long offset = 0; |
| 122 | |
| 123 | if (res->flags & IORESOURCE_IO) |
| 124 | offset = chan->io_base; |
| 125 | else if (res->flags & IORESOURCE_MEM) |
| 126 | offset = 0; |
| 127 | |
| 128 | res->start = region->start + offset; |
| 129 | res->end = region->end + offset; |
| 130 | } |
| 131 | |
| 132 | void pcibios_align_resource(void *data, struct resource *res, |
| 133 | resource_size_t size, resource_size_t align) |
| 134 | __attribute__ ((weak)); |
| 135 | |
| 136 | /* |
| 137 | * We need to avoid collisions with `mirrored' VGA ports |
| 138 | * and other strange ISA hardware, so we always want the |
| 139 | * addresses to be allocated in the 0x000-0x0ff region |
| 140 | * modulo 0x400. |
| 141 | */ |
| 142 | void pcibios_align_resource(void *data, struct resource *res, |
| 143 | resource_size_t size, resource_size_t align) |
| 144 | { |
| 145 | if (res->flags & IORESOURCE_IO) { |
| 146 | resource_size_t start = res->start; |
| 147 | |
| 148 | if (start & 0x300) { |
| 149 | start = (start + 0x3ff) & ~0x3ff; |
| 150 | res->start = start; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 156 | { |
| 157 | u16 cmd, old_cmd; |
| 158 | int idx; |
| 159 | struct resource *r; |
| 160 | |
| 161 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 162 | old_cmd = cmd; |
| 163 | for(idx=0; idx<6; idx++) { |
| 164 | if (!(mask & (1 << idx))) |
| 165 | continue; |
| 166 | r = &dev->resource[idx]; |
| 167 | if (!r->start && r->end) { |
| 168 | printk(KERN_ERR "PCI: Device %s not available because " |
| 169 | "of resource collisions\n", pci_name(dev)); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | if (r->flags & IORESOURCE_IO) |
| 173 | cmd |= PCI_COMMAND_IO; |
| 174 | if (r->flags & IORESOURCE_MEM) |
| 175 | cmd |= PCI_COMMAND_MEMORY; |
| 176 | } |
| 177 | if (dev->resource[PCI_ROM_RESOURCE].start) |
| 178 | cmd |= PCI_COMMAND_MEMORY; |
| 179 | if (cmd != old_cmd) { |
| 180 | printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n", |
| 181 | pci_name(dev), old_cmd, cmd); |
| 182 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * If we set up a device for bus mastering, we need to check and set |
| 189 | * the latency timer as it may not be properly set. |
| 190 | */ |
| 191 | static unsigned int pcibios_max_latency = 255; |
| 192 | |
| 193 | void pcibios_set_master(struct pci_dev *dev) |
| 194 | { |
| 195 | u8 lat; |
| 196 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); |
| 197 | if (lat < 16) |
| 198 | lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; |
| 199 | else if (lat > pcibios_max_latency) |
| 200 | lat = pcibios_max_latency; |
| 201 | else |
| 202 | return; |
| 203 | printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n", |
| 204 | pci_name(dev), lat); |
| 205 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); |
| 206 | } |
| 207 | |
| 208 | void __init pcibios_update_irq(struct pci_dev *dev, int irq) |
| 209 | { |
| 210 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
| 211 | } |
| 212 | |
| 213 | void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) |
| 214 | { |
| 215 | resource_size_t start = pci_resource_start(dev, bar); |
| 216 | resource_size_t len = pci_resource_len(dev, bar); |
| 217 | unsigned long flags = pci_resource_flags(dev, bar); |
| 218 | |
| 219 | if (unlikely(!len || !start)) |
| 220 | return NULL; |
| 221 | if (maxlen && len > maxlen) |
| 222 | len = maxlen; |
| 223 | |
| 224 | /* |
| 225 | * Presently the IORESOURCE_MEM case is a bit special, most |
| 226 | * SH7751 style PCI controllers have PCI memory at a fixed |
| 227 | * location in the address space where no remapping is desired. |
| 228 | * With the IORESOURCE_MEM case more care has to be taken |
| 229 | * to inhibit page table mapping for legacy cores, but this is |
| 230 | * punted off to __ioremap(). |
| 231 | * -- PFM. |
| 232 | */ |
| 233 | if (flags & IORESOURCE_IO) |
| 234 | return ioport_map(start, len); |
| 235 | if (flags & IORESOURCE_MEM) |
| 236 | return ioremap(start, len); |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | EXPORT_SYMBOL(pci_iomap); |
| 241 | |
| 242 | void pci_iounmap(struct pci_dev *dev, void __iomem *addr) |
| 243 | { |
| 244 | iounmap(addr); |
| 245 | } |
| 246 | EXPORT_SYMBOL(pci_iounmap); |
| 247 | |
| 248 | EXPORT_SYMBOL(board_pci_channels); |