Paul Mundt | 9833385 | 2009-04-20 15:51:45 +0900 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/mm.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/pci.h> |
| 6 | |
Paul Mundt | a3c0e0d | 2009-04-20 16:14:29 +0900 | [diff] [blame] | 7 | unsigned long PCIBIOS_MIN_IO = 0x0000; |
| 8 | unsigned long PCIBIOS_MIN_MEM = 0; |
| 9 | |
| 10 | /* |
| 11 | * We need to avoid collisions with `mirrored' VGA ports |
| 12 | * and other strange ISA hardware, so we always want the |
| 13 | * addresses to be allocated in the 0x000-0x0ff region |
| 14 | * modulo 0x400. |
| 15 | */ |
| 16 | void pcibios_align_resource(void *data, struct resource *res, |
| 17 | resource_size_t size, resource_size_t align) |
| 18 | { |
| 19 | struct pci_dev *dev = data; |
| 20 | struct pci_channel *chan = dev->sysdata; |
| 21 | resource_size_t start = res->start; |
| 22 | |
| 23 | if (res->flags & IORESOURCE_IO) { |
| 24 | if (start < PCIBIOS_MIN_IO + chan->io_resource->start) |
| 25 | start = PCIBIOS_MIN_IO + chan->io_resource->start; |
| 26 | |
| 27 | /* |
| 28 | * Put everything into 0x00-0xff region modulo 0x400. |
| 29 | */ |
| 30 | if (start & 0x300) { |
| 31 | start = (start + 0x3ff) & ~0x3ff; |
| 32 | res->start = start; |
| 33 | } |
| 34 | } else if (res->flags & IORESOURCE_MEM) { |
| 35 | if (start < PCIBIOS_MIN_MEM + chan->mem_resource->start) |
| 36 | start = PCIBIOS_MIN_MEM + chan->mem_resource->start; |
| 37 | } |
| 38 | |
| 39 | res->start = start; |
| 40 | } |
| 41 | |
Paul Mundt | 5160d3f | 2009-04-20 18:47:21 +0900 | [diff] [blame] | 42 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 43 | struct resource *res) |
| 44 | { |
| 45 | struct pci_channel *hose = dev->sysdata; |
| 46 | unsigned long offset = 0; |
| 47 | |
| 48 | if (res->flags & IORESOURCE_IO) |
| 49 | offset = hose->io_offset; |
| 50 | else if (res->flags & IORESOURCE_MEM) |
| 51 | offset = hose->mem_offset; |
| 52 | |
| 53 | region->start = res->start - offset; |
| 54 | region->end = res->end - offset; |
| 55 | } |
| 56 | |
| 57 | void __devinit |
| 58 | pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 59 | struct pci_bus_region *region) |
| 60 | { |
| 61 | struct pci_channel *hose = dev->sysdata; |
| 62 | unsigned long offset = 0; |
| 63 | |
| 64 | if (res->flags & IORESOURCE_IO) |
| 65 | offset = hose->io_offset; |
| 66 | else if (res->flags & IORESOURCE_MEM) |
| 67 | offset = hose->mem_offset; |
| 68 | |
| 69 | res->start = region->start + offset; |
| 70 | res->end = region->end + offset; |
| 71 | } |
| 72 | |
Paul Mundt | 3f8daea | 2009-04-20 18:53:41 +0900 | [diff] [blame] | 73 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 74 | { |
| 75 | u16 cmd, old_cmd; |
| 76 | int idx; |
| 77 | struct resource *r; |
| 78 | |
| 79 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 80 | old_cmd = cmd; |
| 81 | for (idx=0; idx < PCI_NUM_RESOURCES; idx++) { |
| 82 | /* Only set up the requested stuff */ |
| 83 | if (!(mask & (1<<idx))) |
| 84 | continue; |
| 85 | |
| 86 | r = &dev->resource[idx]; |
| 87 | if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) |
| 88 | continue; |
| 89 | if ((idx == PCI_ROM_RESOURCE) && |
| 90 | (!(r->flags & IORESOURCE_ROM_ENABLE))) |
| 91 | continue; |
| 92 | if (!r->start && r->end) { |
| 93 | printk(KERN_ERR "PCI: Device %s not available " |
| 94 | "because of resource collisions\n", |
| 95 | pci_name(dev)); |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | if (r->flags & IORESOURCE_IO) |
| 99 | cmd |= PCI_COMMAND_IO; |
| 100 | if (r->flags & IORESOURCE_MEM) |
| 101 | cmd |= PCI_COMMAND_MEMORY; |
| 102 | } |
| 103 | if (cmd != old_cmd) { |
| 104 | printk("PCI: Enabling device %s (%04x -> %04x)\n", |
| 105 | pci_name(dev), old_cmd, cmd); |
| 106 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * If we set up a device for bus mastering, we need to check and set |
| 113 | * the latency timer as it may not be properly set. |
| 114 | */ |
| 115 | static unsigned int pcibios_max_latency = 255; |
| 116 | |
| 117 | void pcibios_set_master(struct pci_dev *dev) |
| 118 | { |
| 119 | u8 lat; |
| 120 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); |
| 121 | if (lat < 16) |
| 122 | lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; |
| 123 | else if (lat > pcibios_max_latency) |
| 124 | lat = pcibios_max_latency; |
| 125 | else |
| 126 | return; |
| 127 | printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n", |
| 128 | pci_name(dev), lat); |
| 129 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); |
| 130 | } |
| 131 | |
| 132 | void __init pcibios_update_irq(struct pci_dev *dev, int irq) |
| 133 | { |
| 134 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
| 135 | } |
| 136 | |
Paul Mundt | 9833385 | 2009-04-20 15:51:45 +0900 | [diff] [blame] | 137 | int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, |
| 138 | enum pci_mmap_state mmap_state, int write_combine) |
| 139 | { |
| 140 | /* |
| 141 | * I/O space can be accessed via normal processor loads and stores on |
| 142 | * this platform but for now we elect not to do this and portable |
| 143 | * drivers should not do this anyway. |
| 144 | */ |
| 145 | if (mmap_state == pci_mmap_io) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | /* |
| 149 | * Ignore write-combine; for now only return uncached mappings. |
| 150 | */ |
| 151 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 152 | |
| 153 | return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 154 | vma->vm_end - vma->vm_start, |
| 155 | vma->vm_page_prot); |
| 156 | } |
Paul Mundt | a3c0e0d | 2009-04-20 16:14:29 +0900 | [diff] [blame] | 157 | |
Paul Mundt | 0bb34a6 | 2009-04-20 16:38:00 +0900 | [diff] [blame] | 158 | static void __iomem *ioport_map_pci(struct pci_dev *dev, |
| 159 | unsigned long port, unsigned int nr) |
| 160 | { |
| 161 | struct pci_channel *chan = dev->sysdata; |
| 162 | |
| 163 | if (!chan->io_map_base) |
| 164 | chan->io_map_base = generic_io_base; |
| 165 | |
| 166 | return (void __iomem *)(chan->io_map_base + port); |
| 167 | } |
| 168 | |
| 169 | void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) |
| 170 | { |
| 171 | resource_size_t start = pci_resource_start(dev, bar); |
| 172 | resource_size_t len = pci_resource_len(dev, bar); |
| 173 | unsigned long flags = pci_resource_flags(dev, bar); |
| 174 | |
| 175 | if (unlikely(!len || !start)) |
| 176 | return NULL; |
| 177 | if (maxlen && len > maxlen) |
| 178 | len = maxlen; |
| 179 | |
| 180 | if (flags & IORESOURCE_IO) |
| 181 | return ioport_map_pci(dev, start, len); |
| 182 | |
| 183 | /* |
| 184 | * Presently the IORESOURCE_MEM case is a bit special, most |
| 185 | * SH7751 style PCI controllers have PCI memory at a fixed |
| 186 | * location in the address space where no remapping is desired. |
| 187 | * With the IORESOURCE_MEM case more care has to be taken |
| 188 | * to inhibit page table mapping for legacy cores, but this is |
| 189 | * punted off to __ioremap(). |
| 190 | * -- PFM. |
| 191 | */ |
| 192 | if (flags & IORESOURCE_MEM) { |
| 193 | if (flags & IORESOURCE_CACHEABLE) |
| 194 | return ioremap(start, len); |
| 195 | |
| 196 | return ioremap_nocache(start, len); |
| 197 | } |
| 198 | |
| 199 | return NULL; |
| 200 | } |
| 201 | EXPORT_SYMBOL(pci_iomap); |
| 202 | |
| 203 | void pci_iounmap(struct pci_dev *dev, void __iomem *addr) |
| 204 | { |
| 205 | iounmap(addr); |
| 206 | } |
| 207 | EXPORT_SYMBOL(pci_iounmap); |
| 208 | |
Paul Mundt | a3c0e0d | 2009-04-20 16:14:29 +0900 | [diff] [blame] | 209 | #ifdef CONFIG_HOTPLUG |
| 210 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
| 211 | EXPORT_SYMBOL(pcibios_bus_to_resource); |
| 212 | EXPORT_SYMBOL(PCIBIOS_MIN_IO); |
| 213 | EXPORT_SYMBOL(PCIBIOS_MIN_MEM); |
| 214 | #endif |