ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / kernel / pci.c
1 /* $Id: pci.c,v 1.6 2000/01/29 00:12:05 grundler Exp $
2  *
3  * This file is subject to the terms and conditions of the GNU General Public
4  * License.  See the file "COPYING" in the main directory of this archive
5  * for more details.
6  *
7  * Copyright (C) 1997, 1998 Ralf Baechle
8  * Copyright (C) 1999 SuSE GmbH
9  * Copyright (C) 1999-2001 Hewlett-Packard Company
10  * Copyright (C) 1999-2001 Grant Grundler
11  */
12 #include <linux/config.h>
13 #include <linux/eisa.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20
21 #include <asm/io.h>
22 #include <asm/system.h>
23 #include <asm/cache.h>          /* for L1_CACHE_BYTES */
24 #include <asm/superio.h>
25
26 #define DEBUG_RESOURCES 0
27 #define DEBUG_CONFIG 0
28
29 #if DEBUG_CONFIG
30 # define DBGC(x...)     printk(KERN_DEBUG x)
31 #else
32 # define DBGC(x...)
33 #endif
34
35
36 #if DEBUG_RESOURCES
37 #define DBG_RES(x...)   printk(KERN_DEBUG x)
38 #else
39 #define DBG_RES(x...)
40 #endif
41
42 /* To be used as: mdelay(pci_post_reset_delay);
43  *
44  * post_reset is the time the kernel should stall to prevent anyone from
45  * accessing the PCI bus once #RESET is de-asserted. 
46  * PCI spec somewhere says 1 second but with multi-PCI bus systems,
47  * this makes the boot time much longer than necessary.
48  * 20ms seems to work for all the HP PCI implementations to date.
49  *
50  * XXX: turn into a #defined constant in <asm/pci.h> ?
51  */
52 int pci_post_reset_delay = 50;
53
54 struct pci_port_ops *pci_port;
55 struct pci_bios_ops *pci_bios;
56
57 int pci_hba_count = 0;
58
59 /* parisc_pci_hba used by pci_port->in/out() ops to lookup bus data.  */
60 #define PCI_HBA_MAX 32
61 struct pci_hba_data *parisc_pci_hba[PCI_HBA_MAX];
62
63
64 /********************************************************************
65 **
66 ** I/O port space support
67 **
68 *********************************************************************/
69
70 /* EISA port numbers and PCI port numbers share the same interface.  Some
71  * machines have both EISA and PCI adapters installed.  Rather than turn
72  * pci_port into an array, we reserve bus 0 for EISA and call the EISA
73  * routines if the access is to a port on bus 0.  We don't want to fix
74  * EISA and ISA drivers which assume port space is <= 0xffff.
75  */
76
77 #ifdef CONFIG_EISA
78 #define EISA_IN(size) if (EISA_bus && (b == 0)) return eisa_in##size(addr)
79 #define EISA_OUT(size) if (EISA_bus && (b == 0)) return eisa_out##size(d, addr)
80 #else
81 #define EISA_IN(size)
82 #define EISA_OUT(size)
83 #endif
84
85 #define PCI_PORT_IN(type, size) \
86 u##size in##type (int addr) \
87 { \
88         int b = PCI_PORT_HBA(addr); \
89         EISA_IN(size); \
90         if (!parisc_pci_hba[b]) return (u##size) -1; \
91         return pci_port->in##type(parisc_pci_hba[b], PCI_PORT_ADDR(addr)); \
92 } \
93 EXPORT_SYMBOL(in##type);
94
95 PCI_PORT_IN(b,  8)
96 PCI_PORT_IN(w, 16)
97 PCI_PORT_IN(l, 32)
98
99
100 #define PCI_PORT_OUT(type, size) \
101 void out##type (u##size d, int addr) \
102 { \
103         int b = PCI_PORT_HBA(addr); \
104         EISA_OUT(size); \
105         if (!parisc_pci_hba[b]) return; \
106         pci_port->out##type(parisc_pci_hba[b], PCI_PORT_ADDR(addr), d); \
107 } \
108 EXPORT_SYMBOL(out##type);
109
110 PCI_PORT_OUT(b,  8)
111 PCI_PORT_OUT(w, 16)
112 PCI_PORT_OUT(l, 32)
113
114
115
116 /*
117  * BIOS32 replacement.
118  */
119 static int __init pcibios_init(void)
120 {
121         if (!pci_bios)
122                 return -1;
123
124         if (pci_bios->init) {
125                 pci_bios->init();
126         } else {
127                 printk(KERN_WARNING "pci_bios != NULL but init() is!\n");
128         }
129         return 0;
130 }
131
132
133 /* Called from pci_do_scan_bus() *after* walking a bus but before walking PPBs. */
134 void pcibios_fixup_bus(struct pci_bus *bus)
135 {
136         if (pci_bios->fixup_bus) {
137                 pci_bios->fixup_bus(bus);
138         } else {
139                 printk(KERN_WARNING "pci_bios != NULL but fixup_bus() is!\n");
140         }
141 }
142
143
144 char *pcibios_setup(char *str)
145 {
146         return str;
147 }
148
149 /* Used in drivers/pci/quirks.c */
150 struct pci_fixup pcibios_fixups[] = { 
151 #ifdef CONFIG_SUPERIO
152         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_NS,       PCI_DEVICE_ID_NS_87415, superio_fixup_pci },
153 #endif
154         { 0 }
155 };
156
157
158 /*
159  * Called by pci_set_master() - a driver interface.
160  *
161  * Legacy PDC guarantees to set:
162  *      Map Memory BAR's into PA IO space.
163  *      Map Expansion ROM BAR into one common PA IO space per bus.
164  *      Map IO BAR's into PCI IO space.
165  *      Command (see below)
166  *      Cache Line Size
167  *      Latency Timer
168  *      Interrupt Line
169  *      PPB: secondary latency timer, io/mmio base/limit,
170  *              bus numbers, bridge control
171  *
172  */
173 void pcibios_set_master(struct pci_dev *dev)
174 {
175         u8 lat;
176
177         /* If someone already mucked with this, don't touch it. */
178         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
179         if (lat >= 16) return;
180
181         /*
182         ** HP generally has fewer devices on the bus than other architectures.
183         ** upper byte is PCI_LATENCY_TIMER.
184         */
185         pci_write_config_word(dev, PCI_CACHE_LINE_SIZE,
186                                 (0x80 << 8) | (L1_CACHE_BYTES / sizeof(u32)));
187 }
188
189
190 void __init pcibios_init_bus(struct pci_bus *bus)
191 {
192         struct pci_dev *dev = bus->self;
193         unsigned short bridge_ctl;
194
195         /* We deal only with pci controllers and pci-pci bridges. */
196         if (!dev || (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
197                 return;
198
199         /* PCI-PCI bridge - set the cache line and default latency
200            (32) for primary and secondary buses. */
201         pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 32);
202
203         pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bridge_ctl);
204         bridge_ctl |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
205         pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bridge_ctl);
206 }
207
208
209 /* KLUGE: Link the child and parent resources - generic PCI didn't */
210 static void
211 pcibios_link_hba_resources( struct resource *hba_res, struct resource *r)
212 {
213         if (!r->parent) {
214                 printk(KERN_EMERG "PCI: Tell willy he's wrong\n");
215                 r->parent = hba_res;
216
217                 /* reverse link is harder *sigh*  */
218                 if (r->parent->child) {
219                         if (r->parent->sibling) {
220                                 struct resource *next = r->parent->sibling;
221                                 while (next->sibling)
222                                          next = next->sibling;
223                                 next->sibling = r;
224                         } else {
225                                 r->parent->sibling = r;
226                         }
227                 } else
228                         r->parent->child = r;
229         }
230 }
231
232 /* called by drivers/pci/setup-bus.c:pci_setup_bridge().  */
233 void __devinit pcibios_resource_to_bus(struct pci_dev *dev,
234                 struct pci_bus_region *region, struct resource *res)
235 {
236         struct pci_bus *bus = dev->bus;
237         struct pci_hba_data *hba = HBA_DATA(bus->bridge->platform_data);
238
239         if (res->flags & IORESOURCE_IO) {
240                 /*
241                 ** I/O space may see busnumbers here. Something
242                 ** in the form of 0xbbxxxx where bb is the bus num
243                 ** and xxxx is the I/O port space address.
244                 ** Remaining address translation are done in the
245                 ** PCI Host adapter specific code - ie dino_out8.
246                 */
247                 region->start = PCI_PORT_ADDR(res->start);
248                 region->end   = PCI_PORT_ADDR(res->end);
249         } else if (res->flags & IORESOURCE_MEM) {
250                 /* Convert MMIO addr to PCI addr (undo global virtualization) */
251                 region->start = PCI_BUS_ADDR(hba, res->start);
252                 region->end   = PCI_BUS_ADDR(hba, res->end);
253         }
254
255         DBG_RES("pcibios_resource_to_bus(%02x %s [%lx,%lx])\n",
256                 bus->number, res->flags & IORESOURCE_IO ? "IO" : "MEM",
257                 region->start, region->end);
258
259         /* KLUGE ALERT
260         ** if this resource isn't linked to a "parent", then it seems
261         ** to be a child of the HBA - lets link it in.
262         */
263         pcibios_link_hba_resources(&hba->io_space, bus->resource[0]);
264         pcibios_link_hba_resources(&hba->lmmio_space, bus->resource[1]);
265 }
266
267 #ifdef CONFIG_HOTPLUG
268 EXPORT_SYMBOL(pcibios_resource_to_bus);
269 #endif
270
271 /*
272  * pcibios align resources() is called every time generic PCI code
273  * wants to generate a new address. The process of looking for
274  * an available address, each candidate is first "aligned" and
275  * then checked if the resource is available until a match is found.
276  *
277  * Since we are just checking candidates, don't use any fields other
278  * than res->start.
279  */
280 void pcibios_align_resource(void *data, struct resource *res,
281                                 unsigned long size, unsigned long alignment)
282 {
283         unsigned long mask, align;
284
285         DBG_RES("pcibios_align_resource(%s, (%p) [%lx,%lx]/%x, 0x%lx, 0x%lx)\n",
286                 pci_name(((struct pci_dev *) data)),
287                 res->parent, res->start, res->end,
288                 (int) res->flags, size, alignment);
289
290         /* If it's not IO, then it's gotta be MEM */
291         align = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
292
293         /* Align to largest of MIN or input size */
294         mask = max(alignment, align) - 1;
295         res->start += mask;
296         res->start &= ~mask;
297
298         /* The caller updates the end field, we don't.  */
299 }
300
301
302 /*
303  * A driver is enabling the device.  We make sure that all the appropriate
304  * bits are set to allow the device to operate as the driver is expecting.
305  * We enable the port IO and memory IO bits if the device has any BARs of
306  * that type, and we enable the PERR and SERR bits unconditionally.
307  * Drivers that do not need parity (eg graphics and possibly networking)
308  * can clear these bits if they want.
309  */
310 int pcibios_enable_device(struct pci_dev *dev, int mask)
311 {
312         u16 cmd;
313         int idx;
314
315         pci_read_config_word(dev, PCI_COMMAND, &cmd);
316
317         for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
318                 struct resource *r = &dev->resource[idx];
319
320                 /* only setup requested resources */
321                 if (!(mask & (1<<idx)))
322                         continue;
323
324                 if (r->flags & IORESOURCE_IO)
325                         cmd |= PCI_COMMAND_IO;
326                 if (r->flags & IORESOURCE_MEM)
327                         cmd |= PCI_COMMAND_MEMORY;
328         }
329
330         cmd |= (PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
331
332 #if 0
333         /* If bridge/bus controller has FBB enabled, child must too. */
334         if (dev->bus->bridge_ctl & PCI_BRIDGE_CTL_FAST_BACK)
335                 cmd |= PCI_COMMAND_FAST_BACK;
336 #endif
337         DBGC("PCIBIOS: Enabling device %s cmd 0x%04x\n", pci_name(dev), cmd);
338         pci_write_config_word(dev, PCI_COMMAND, cmd);
339         return 0;
340 }
341
342
343 /* PA-RISC specific */
344 void pcibios_register_hba(struct pci_hba_data *hba)
345 {
346         if (pci_hba_count >= PCI_HBA_MAX) {
347                 printk(KERN_ERR "PCI: Too many Host Bus Adapters\n");
348                 return;
349         }
350
351         parisc_pci_hba[pci_hba_count] = hba;
352         hba->hba_num = pci_hba_count++;
353 }
354
355 subsys_initcall(pcibios_init);