ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / pci / pci.c
1 /*
2  * This program is free software; you can redistribute  it and/or modify it
3  * under  the terms of  the GNU General  Public License as published by the
4  * Free Software Foundation;  either version 2 of the  License, or (at your
5  * option) any later version.
6  *
7  * Copyright (C) 2003 Ralf Baechle (ralf@linux-mips.org)
8  */
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/bootmem.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/pci.h>
16
17 #include <asm/pci_channel.h>
18
19 /*
20  * Indicate whether we respect the PCI setup left by the firmware.
21  *
22  * Make this long-lived  so that we know when shutting down
23  * whether we probed only or not.
24  */
25 int pci_probe_only;
26
27 #define PCI_ASSIGN_ALL_BUSSES   1
28
29 unsigned int pci_probe = PCI_ASSIGN_ALL_BUSSES;
30
31 /*
32  * The PCI controller list.
33  */
34
35 struct pci_controller *hose_head, **hose_tail = &hose_head;
36 struct pci_controller *pci_isa_hose;
37
38 unsigned long PCIBIOS_MIN_IO    = 0x0000;
39 unsigned long PCIBIOS_MIN_MEM   = 0;
40
41 /*
42  * We need to avoid collisions with `mirrored' VGA ports
43  * and other strange ISA hardware, so we always want the
44  * addresses to be allocated in the 0x000-0x0ff region
45  * modulo 0x400.
46  *
47  * Why? Because some silly external IO cards only decode
48  * the low 10 bits of the IO address. The 0x00-0xff region
49  * is reserved for motherboard devices that decode all 16
50  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
51  * but we want to try to avoid allocating at 0x2900-0x2bff
52  * which might have be mirrored at 0x0100-0x03ff..
53  */
54 void
55 pcibios_align_resource(void *data, struct resource *res,
56                        unsigned long size, unsigned long align)
57 {
58         struct pci_dev *dev = data;
59         struct pci_controller *hose = dev->sysdata;
60         unsigned long start = res->start;
61
62         if (res->flags & IORESOURCE_IO) {
63                 /* Make sure we start at our min on all hoses */
64                 if (start - hose->io_resource->start < PCIBIOS_MIN_IO)
65                         start = PCIBIOS_MIN_IO + hose->io_resource->start;
66
67                 /*
68                  * Put everything into 0x00-0xff region modulo 0x400
69                  */
70                 if (start & 0x300)
71                         start = (start + 0x3ff) & ~0x3ff;
72         } else if (res->flags & IORESOURCE_MEM) {
73                 /* Make sure we start at our min on all hoses */
74                 if (start - hose->mem_resource->start < PCIBIOS_MIN_MEM)
75                         start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
76         }
77
78         res->start = start;
79 }
80
81 struct pci_controller * __init alloc_pci_controller(void)
82 {
83         return alloc_bootmem(sizeof(struct pci_controller));
84 }
85
86 void __init register_pci_controller(struct pci_controller *hose)
87 {
88         *hose_tail = hose;
89         hose_tail = &hose->next;
90 }
91
92 /* Most MIPS systems have straight-forward swizzling needs.  */
93
94 static inline u8 bridge_swizzle(u8 pin, u8 slot)
95 {
96         return (((pin - 1) + slot) % 4) + 1;
97 }
98
99 static u8 __init common_swizzle(struct pci_dev *dev, u8 *pinp)
100 {
101         u8 pin = *pinp;
102
103         while (dev->bus->parent) {
104                 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
105                 /* Move up the chain of bridges. */
106                 dev = dev->bus->self;
107         }
108         *pinp = pin;
109
110         /* The slot is the slot of the last bridge. */
111         return PCI_SLOT(dev->devfn);
112 }
113
114 static int __init pcibios_init(void)
115 {
116         struct pci_controller *hose;
117         struct pci_bus *bus;
118         int next_busno;
119         int need_domain_info = 0;
120
121         /* Scan all of the recorded PCI controllers.  */
122         for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
123
124                 if (request_resource(&iomem_resource, hose->mem_resource) < 0)
125                         goto out;
126                 if (request_resource(&ioport_resource, hose->io_resource) < 0)
127                         goto out_free_mem_resource;
128
129                 if (!hose->iommu)
130                         PCI_DMA_BUS_IS_PHYS = 1;
131
132                 bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
133                 hose->bus = bus;
134                 hose->need_domain_info = need_domain_info;
135                 next_busno = bus->subordinate + 1;
136                 /* Don't allow 8-bit bus number overflow inside the hose -
137                    reserve some space for bridges. */ 
138                 if (next_busno > 224) {
139                         next_busno = 0;
140                         need_domain_info = 1;
141                 }
142                 continue;
143
144 out_free_mem_resource:
145                 release_resource(hose->mem_resource);
146
147 out:
148                 printk(KERN_WARNING
149                        "Skipping PCI bus scan due to resource conflict\n");
150         }
151
152         if (!pci_probe_only)
153                 pci_assign_unassigned_resources();
154         pci_fixup_irqs(common_swizzle, pcibios_map_irq);
155
156         return 0;
157 }
158
159 subsys_initcall(pcibios_init);
160
161 static int pcibios_enable_resources(struct pci_dev *dev, int mask)
162 {
163         u16 cmd, old_cmd;
164         int idx;
165         struct resource *r;
166
167         pci_read_config_word(dev, PCI_COMMAND, &cmd);
168         old_cmd = cmd;
169         for(idx=0; idx<6; idx++) {
170                 /* Only set up the requested stuff */
171                 if (!(mask & (1<<idx)))
172                         continue;
173
174                 r = &dev->resource[idx];
175                 if (!r->start && r->end) {
176                         printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
177                         return -EINVAL;
178                 }
179                 if (r->flags & IORESOURCE_IO)
180                         cmd |= PCI_COMMAND_IO;
181                 if (r->flags & IORESOURCE_MEM)
182                         cmd |= PCI_COMMAND_MEMORY;
183         }
184         if (dev->resource[PCI_ROM_RESOURCE].start)
185                 cmd |= PCI_COMMAND_MEMORY;
186         if (cmd != old_cmd) {
187                 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
188                 pci_write_config_word(dev, PCI_COMMAND, cmd);
189         }
190         return 0;
191 }
192
193 /*
194  *  If we set up a device for bus mastering, we need to check the latency
195  *  timer as certain crappy BIOSes forget to set it properly.
196  */
197 unsigned int pcibios_max_latency = 255;
198
199 void pcibios_set_master(struct pci_dev *dev)
200 {
201         u8 lat;
202         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
203         if (lat < 16)
204                 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
205         else if (lat > pcibios_max_latency)
206                 lat = pcibios_max_latency;
207         else
208                 return;
209         printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
210                pci_name(dev), lat);
211         pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
212 }
213
214 unsigned int pcibios_assign_all_busses(void)
215 {
216         return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
217 }
218
219 int pcibios_enable_device(struct pci_dev *dev, int mask)
220 {
221         int err;
222
223         if ((err = pcibios_enable_resources(dev, mask)) < 0)
224                 return err;
225
226         return 0;
227 }
228
229 static void __init pcibios_fixup_device_resources(struct pci_dev *dev,
230         struct pci_bus *bus)
231 {
232         /* Update device resources.  */
233         struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
234         unsigned long offset;
235         int i;
236
237         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
238                 if (!dev->resource[i].start)
239                         continue;
240                 if (dev->resource[i].flags & IORESOURCE_IO)
241                         offset = hose->io_offset;
242                 else if (dev->resource[i].flags & IORESOURCE_MEM)
243                         offset = hose->mem_offset;
244
245                 dev->resource[i].start += offset;
246                 dev->resource[i].end += offset;
247         }
248 }
249
250 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
251 {
252         /* Propagate hose info into the subordinate devices.  */
253
254         struct pci_controller *hose = bus->sysdata;
255         struct list_head *ln;
256         struct pci_dev *dev = bus->self;
257
258         if (!dev) {
259                 bus->resource[0] = hose->io_resource;
260                 bus->resource[1] = hose->mem_resource;
261         } else if (pci_probe_only &&
262                    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
263                 pci_read_bridge_bases(bus);
264                 pcibios_fixup_device_resources(dev, bus);
265         } 
266
267         for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
268                 struct pci_dev *dev = pci_dev_b(ln);
269
270                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
271                         pcibios_fixup_device_resources(dev, bus);
272         }
273 }
274
275 void __init
276 pcibios_update_irq(struct pci_dev *dev, int irq)
277 {
278         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
279 }
280
281 void __devinit
282 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
283                          struct resource *res)
284 {
285         struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
286         unsigned long offset = 0;
287
288         if (res->flags & IORESOURCE_IO)
289                 offset = hose->io_offset;
290         else if (res->flags & IORESOURCE_MEM)
291                 offset = hose->mem_offset;
292
293         region->start = res->start - offset;
294         region->end = res->end - offset;
295 }
296
297 #ifdef CONFIG_HOTPLUG
298 EXPORT_SYMBOL(pcibios_resource_to_bus);
299 #endif
300
301 char *pcibios_setup(char *str)
302 {
303         return str;
304 }