Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/drivers/pci/pci.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2002 M. R. Brown <mrbrown@linux-sh.org> |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 5 | * Copyright (c) 2004 - 2006 Paul Mundt <lethal@linux-sh.org> |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * These functions are collected here to reduce duplication of common |
| 8 | * code amongst the many platform-specific PCI support code files. |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * These routines require the following board-specific routines: |
| 11 | * void pcibios_fixup_irqs(); |
| 12 | * |
| 13 | * See include/asm-sh/pci.h for more information. |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 14 | * |
| 15 | * This file is subject to the terms and conditions of the GNU General Public |
| 16 | * License. See the file "COPYING" in the main directory of this archive |
| 17 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/pci.h> |
| 21 | #include <linux/init.h> |
Paul Mundt | e588a00 | 2009-04-14 15:23:40 +0900 | [diff] [blame] | 22 | #include <linux/dma-debug.h> |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 23 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static int __init pcibios_init(void) |
| 26 | { |
| 27 | struct pci_channel *p; |
| 28 | struct pci_bus *bus; |
| 29 | int busno; |
| 30 | |
Magnus Damm | d0e3db4 | 2009-03-11 15:46:14 +0900 | [diff] [blame] | 31 | /* init channels */ |
| 32 | busno = 0; |
| 33 | for (p = board_pci_channels; p->init; p++) { |
| 34 | if (p->init(p) == 0) |
| 35 | p->enabled = 1; |
| 36 | else |
| 37 | pr_err("Unable to init pci channel %d\n", busno); |
| 38 | busno++; |
| 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #ifdef CONFIG_PCI_AUTO |
| 42 | /* assign resources */ |
| 43 | busno = 0; |
Magnus Damm | d0e3db4 | 2009-03-11 15:46:14 +0900 | [diff] [blame] | 44 | for (p = board_pci_channels; p->init; p++) |
| 45 | if (p->enabled) |
| 46 | busno = pciauto_assign_resources(busno, p) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | /* scan the buses */ |
| 50 | busno = 0; |
Magnus Damm | d0e3db4 | 2009-03-11 15:46:14 +0900 | [diff] [blame] | 51 | for (p = board_pci_channels; p->init; p++) { |
| 52 | if (p->enabled) { |
| 53 | bus = pci_scan_bus(busno, p->pci_ops, p); |
| 54 | busno = bus->subordinate + 1; |
| 55 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Bjorn Helgaas | e558234 | 2008-12-16 21:37:15 -0700 | [diff] [blame] | 58 | pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Paul Mundt | e588a00 | 2009-04-14 15:23:40 +0900 | [diff] [blame] | 60 | dma_debug_add_bus(&pci_bus_type); |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return 0; |
| 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | subsys_initcall(pcibios_init); |
| 65 | |
Paul Mundt | 959f85f | 2006-09-27 16:43:28 +0900 | [diff] [blame] | 66 | /* |
| 67 | * Called after each bus is probed, but before its children |
| 68 | * are examined. |
| 69 | */ |
Paul Mundt | b6d7b66 | 2007-11-22 16:29:10 +0900 | [diff] [blame] | 70 | void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus) |
Paul Mundt | 959f85f | 2006-09-27 16:43:28 +0900 | [diff] [blame] | 71 | { |
| 72 | pci_read_bridge_bases(bus); |
| 73 | } |
| 74 | |
Paul Mundt | 9ade121 | 2009-04-20 15:38:25 +0900 | [diff] [blame] | 75 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 76 | struct resource *res) |
| 77 | { |
| 78 | region->start = res->start; |
| 79 | region->end = res->end; |
| 80 | } |
| 81 | |
| 82 | void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 83 | struct pci_bus_region *region) |
| 84 | { |
| 85 | res->start = region->start; |
| 86 | res->end = region->end; |
| 87 | } |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 90 | { |
| 91 | u16 cmd, old_cmd; |
| 92 | int idx; |
| 93 | struct resource *r; |
| 94 | |
| 95 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 96 | old_cmd = cmd; |
| 97 | for(idx=0; idx<6; idx++) { |
| 98 | if (!(mask & (1 << idx))) |
| 99 | continue; |
| 100 | r = &dev->resource[idx]; |
| 101 | if (!r->start && r->end) { |
| 102 | printk(KERN_ERR "PCI: Device %s not available because " |
| 103 | "of resource collisions\n", pci_name(dev)); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | if (r->flags & IORESOURCE_IO) |
| 107 | cmd |= PCI_COMMAND_IO; |
| 108 | if (r->flags & IORESOURCE_MEM) |
| 109 | cmd |= PCI_COMMAND_MEMORY; |
| 110 | } |
| 111 | if (dev->resource[PCI_ROM_RESOURCE].start) |
| 112 | cmd |= PCI_COMMAND_MEMORY; |
| 113 | if (cmd != old_cmd) { |
| 114 | printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n", |
| 115 | pci_name(dev), old_cmd, cmd); |
| 116 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * If we set up a device for bus mastering, we need to check and set |
| 123 | * the latency timer as it may not be properly set. |
| 124 | */ |
Adrian Bunk | 6241003 | 2008-06-18 01:33:40 +0300 | [diff] [blame] | 125 | static unsigned int pcibios_max_latency = 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | void pcibios_set_master(struct pci_dev *dev) |
| 128 | { |
| 129 | u8 lat; |
| 130 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); |
| 131 | if (lat < 16) |
| 132 | lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; |
| 133 | else if (lat > pcibios_max_latency) |
| 134 | lat = pcibios_max_latency; |
| 135 | else |
| 136 | return; |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 137 | printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n", |
| 138 | pci_name(dev), lat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); |
| 140 | } |
| 141 | |
| 142 | void __init pcibios_update_irq(struct pci_dev *dev, int irq) |
| 143 | { |
| 144 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); |
| 145 | } |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 146 | |
| 147 | void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) |
| 148 | { |
Benjamin Herrenschmidt | b70d3a2 | 2008-04-29 00:59:11 -0700 | [diff] [blame] | 149 | resource_size_t start = pci_resource_start(dev, bar); |
| 150 | resource_size_t len = pci_resource_len(dev, bar); |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 151 | unsigned long flags = pci_resource_flags(dev, bar); |
| 152 | |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame] | 153 | if (unlikely(!len || !start)) |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 154 | return NULL; |
| 155 | if (maxlen && len > maxlen) |
| 156 | len = maxlen; |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * Presently the IORESOURCE_MEM case is a bit special, most |
| 160 | * SH7751 style PCI controllers have PCI memory at a fixed |
Magnus Damm | ef339f2 | 2008-02-19 21:35:22 +0900 | [diff] [blame] | 161 | * location in the address space where no remapping is desired. |
| 162 | * With the IORESOURCE_MEM case more care has to be taken |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame] | 163 | * to inhibit page table mapping for legacy cores, but this is |
| 164 | * punted off to __ioremap(). |
| 165 | * -- PFM. |
Paul Mundt | d7cdc9e | 2006-09-27 15:16:42 +0900 | [diff] [blame] | 166 | */ |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame] | 167 | if (flags & IORESOURCE_IO) |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 168 | return ioport_map(start, len); |
Paul Mundt | a3e61d5 | 2006-09-27 16:45:22 +0900 | [diff] [blame] | 169 | if (flags & IORESOURCE_MEM) |
| 170 | return ioremap(start, len); |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 171 | |
| 172 | return NULL; |
| 173 | } |
Paul Mundt | 959f85f | 2006-09-27 16:43:28 +0900 | [diff] [blame] | 174 | EXPORT_SYMBOL(pci_iomap); |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 175 | |
| 176 | void pci_iounmap(struct pci_dev *dev, void __iomem *addr) |
| 177 | { |
| 178 | iounmap(addr); |
| 179 | } |
Jamie Lenehan | a09749d | 2006-09-27 15:05:39 +0900 | [diff] [blame] | 180 | EXPORT_SYMBOL(pci_iounmap); |
Magnus Damm | aa5d3ff | 2008-02-19 21:35:40 +0900 | [diff] [blame] | 181 | |
| 182 | EXPORT_SYMBOL(board_pci_channels); |