blob: e8ac8daafc33ba997ddbdaa0bf815e58be27afda [file] [log] [blame]
Paul Mundt4c5107e2009-04-20 15:43:36 +09001/*
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>
Paul Mundte79066a2009-04-20 18:29:22 +090016#include <linux/mutex.h>
17
18/*
19 * The PCI controller list.
20 */
21static struct pci_channel *hose_head, **hose_tail = &hose_head;
22
23static int pci_initialized;
24
25static void __devinit pcibios_scanbus(struct pci_channel *hose)
26{
27 static int next_busno;
28 struct pci_bus *bus;
29
30 /* Catch botched conversion attempts */
31 BUG_ON(hose->init);
32
33 bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
34 if (bus) {
35 next_busno = bus->subordinate + 1;
36 /* Don't allow 8-bit bus number overflow inside the hose -
37 reserve some space for bridges. */
38 if (next_busno > 224)
39 next_busno = 0;
40
41 pci_bus_size_bridges(bus);
42 pci_bus_assign_resources(bus);
43 pci_enable_bridges(bus);
44 }
45}
46
47static DEFINE_MUTEX(pci_scan_mutex);
48
49void __devinit register_pci_controller(struct pci_channel *hose)
50{
51 if (request_resource(&iomem_resource, hose->mem_resource) < 0)
52 goto out;
53 if (request_resource(&ioport_resource, hose->io_resource) < 0) {
54 release_resource(hose->mem_resource);
55 goto out;
56 }
57
58 *hose_tail = hose;
59 hose_tail = &hose->next;
60
61 /*
62 * Do not panic here but later - this might hapen before console init.
63 */
64 if (!hose->io_map_base) {
65 printk(KERN_WARNING
66 "registering PCI controller with io_map_base unset\n");
67 }
68
69 /*
70 * Scan the bus if it is register after the PCI subsystem
71 * initialization.
72 */
73 if (pci_initialized) {
74 mutex_lock(&pci_scan_mutex);
75 pcibios_scanbus(hose);
76 mutex_unlock(&pci_scan_mutex);
77 }
78
79 return;
80
81out:
82 printk(KERN_WARNING
83 "Skipping PCI bus scan due to resource conflict\n");
84}
Paul Mundt4c5107e2009-04-20 15:43:36 +090085
86static int __init pcibios_init(void)
87{
Paul Mundte79066a2009-04-20 18:29:22 +090088 struct pci_channel *hose;
Paul Mundt4c5107e2009-04-20 15:43:36 +090089
Paul Mundte79066a2009-04-20 18:29:22 +090090 /* Scan all of the recorded PCI controllers. */
91 for (hose = hose_head; hose; hose = hose->next)
92 pcibios_scanbus(hose);
Paul Mundt4c5107e2009-04-20 15:43:36 +090093
94 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
95
96 dma_debug_add_bus(&pci_bus_type);
97
Paul Mundte79066a2009-04-20 18:29:22 +090098 pci_initialized = 1;
99
Paul Mundt4c5107e2009-04-20 15:43:36 +0900100 return 0;
101}
102subsys_initcall(pcibios_init);
103
104static void pcibios_fixup_device_resources(struct pci_dev *dev,
105 struct pci_bus *bus)
106{
107 /* Update device resources. */
Paul Mundt09cfeb12009-04-20 18:42:00 +0900108 struct pci_channel *hose = bus->sysdata;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900109 unsigned long offset = 0;
110 int i;
111
112 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
113 if (!dev->resource[i].start)
114 continue;
115 if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
116 continue;
117 if (dev->resource[i].flags & IORESOURCE_IO)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900118 offset = hose->io_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900119 else if (dev->resource[i].flags & IORESOURCE_MEM)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900120 offset = hose->mem_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900121
122 dev->resource[i].start += offset;
123 dev->resource[i].end += offset;
124 }
125}
126
Paul Mundt4c5107e2009-04-20 15:43:36 +0900127/*
128 * Called after each bus is probed, but before its children
129 * are examined.
130 */
131void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
132{
133 struct pci_dev *dev = bus->self;
134 struct list_head *ln;
135 struct pci_channel *chan = bus->sysdata;
136
137 if (!dev) {
138 bus->resource[0] = chan->io_resource;
139 bus->resource[1] = chan->mem_resource;
140 }
141
142 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
143 dev = pci_dev_b(ln);
144
145 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
146 pcibios_fixup_device_resources(dev, bus);
147 }
148}
149
150void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
151 struct resource *res)
152{
Paul Mundt09cfeb12009-04-20 18:42:00 +0900153 struct pci_channel *hose = dev->sysdata;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900154 unsigned long offset = 0;
155
156 if (res->flags & IORESOURCE_IO)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900157 offset = hose->io_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900158 else if (res->flags & IORESOURCE_MEM)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900159 offset = hose->mem_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900160
161 region->start = res->start - offset;
162 region->end = res->end - offset;
163}
164
165void __devinit
166pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
167 struct pci_bus_region *region)
168{
Paul Mundt09cfeb12009-04-20 18:42:00 +0900169 struct pci_channel *hose = dev->sysdata;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900170 unsigned long offset = 0;
171
172 if (res->flags & IORESOURCE_IO)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900173 offset = hose->io_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900174 else if (res->flags & IORESOURCE_MEM)
Paul Mundt09cfeb12009-04-20 18:42:00 +0900175 offset = hose->mem_offset;
Paul Mundt4c5107e2009-04-20 15:43:36 +0900176
177 res->start = region->start + offset;
178 res->end = region->end + offset;
179}
180
Paul Mundt4c5107e2009-04-20 15:43:36 +0900181int pcibios_enable_device(struct pci_dev *dev, int mask)
182{
183 u16 cmd, old_cmd;
184 int idx;
185 struct resource *r;
186
187 pci_read_config_word(dev, PCI_COMMAND, &cmd);
188 old_cmd = cmd;
189 for(idx=0; idx<6; idx++) {
190 if (!(mask & (1 << idx)))
191 continue;
192 r = &dev->resource[idx];
193 if (!r->start && r->end) {
194 printk(KERN_ERR "PCI: Device %s not available because "
195 "of resource collisions\n", pci_name(dev));
196 return -EINVAL;
197 }
198 if (r->flags & IORESOURCE_IO)
199 cmd |= PCI_COMMAND_IO;
200 if (r->flags & IORESOURCE_MEM)
201 cmd |= PCI_COMMAND_MEMORY;
202 }
203 if (dev->resource[PCI_ROM_RESOURCE].start)
204 cmd |= PCI_COMMAND_MEMORY;
205 if (cmd != old_cmd) {
206 printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
207 pci_name(dev), old_cmd, cmd);
208 pci_write_config_word(dev, PCI_COMMAND, cmd);
209 }
210 return 0;
211}
212
213/*
214 * If we set up a device for bus mastering, we need to check and set
215 * the latency timer as it may not be properly set.
216 */
217static unsigned int pcibios_max_latency = 255;
218
219void pcibios_set_master(struct pci_dev *dev)
220{
221 u8 lat;
222 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
223 if (lat < 16)
224 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
225 else if (lat > pcibios_max_latency)
226 lat = pcibios_max_latency;
227 else
228 return;
229 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
230 pci_name(dev), lat);
231 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
232}
233
234void __init pcibios_update_irq(struct pci_dev *dev, int irq)
235{
236 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
237}