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/types.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>		/* for __init and __devinit */
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 
19 #include <asm/io.h>
20 #include <asm/system.h>
21 #include <asm/cache.h>		/* for L1_CACHE_BYTES */
22 
23 #define DEBUG_RESOURCES 0
24 #define DEBUG_CONFIG 0
25 
26 #if DEBUG_CONFIG
27 # define DBGC(x...)     printk(KERN_DEBUG x)
28 #else
29 # define DBGC(x...)
30 #endif
31 
32 
33 #if DEBUG_RESOURCES
34 #define DBG_RES(x...)	printk(KERN_DEBUG x)
35 #else
36 #define DBG_RES(x...)
37 #endif
38 
39 /* To be used as: mdelay(pci_post_reset_delay);
40 **
41 ** post_reset is the time the kernel should stall to prevent anyone from
42 ** accessing the PCI bus once #RESET is de-asserted.
43 ** PCI spec somewhere says 1 second but with multi-PCI bus systems,
44 ** this makes the boot time much longer than necessary.
45 ** 20ms seems to work for all the HP PCI implementations to date.
46 */
47 int pci_post_reset_delay = 50;
48 
49 struct pci_port_ops *pci_port;
50 struct pci_bios_ops *pci_bios;
51 
52 int pci_hba_count = 0;
53 
54 /*
55 ** parisc_pci_hba used by pci_port->in/out() ops to lookup bus data.
56 */
57 #define PCI_HBA_MAX 32
58 struct pci_hba_data *parisc_pci_hba[PCI_HBA_MAX];
59 
60 
61 /********************************************************************
62 **
63 ** I/O port space support
64 **
65 *********************************************************************/
66 
67 /* EISA port numbers and PCI port numbers share the same interface.  Some
68  * machines have both EISA and PCI adapters installed.  Rather than turn
69  * pci_port into an array, we reserve bus 0 for EISA and call the EISA
70  * routines if the access is to a port on bus 0.  We don't want to fix
71  * EISA and ISA drivers which assume port space is <= 0xffff.
72  */
73 
74 #ifdef CONFIG_EISA
75 #define EISA_IN(size) if (EISA_bus && (b == 0)) return eisa_in##size(addr)
76 #define EISA_OUT(size) if (EISA_bus && (b == 0)) return eisa_out##size(d, addr)
77 #else
78 #define EISA_IN(size)
79 #define EISA_OUT(size)
80 #endif
81 
82 #define PCI_PORT_IN(type, size) \
83 u##size in##type (int addr) \
84 { \
85 	int b = PCI_PORT_HBA(addr); \
86 	u##size d = (u##size) -1; \
87 	EISA_IN(size); \
88 	ASSERT(pci_port); /* make sure services are defined */ \
89 	ASSERT(parisc_pci_hba[b]); /* make sure ioaddr are "fixed up" */ \
90 	if (parisc_pci_hba[b] == NULL) { \
91 		printk(KERN_WARNING "\nPCI or EISA Host Bus Adapter %d not registered. in" #size "(0x%x) returning -1\n", b, addr); \
92 	} else { \
93 		d = pci_port->in##type(parisc_pci_hba[b], PCI_PORT_ADDR(addr)); \
94 	} \
95 	return d; \
96 }
97 
98 PCI_PORT_IN(b,  8)
99 PCI_PORT_IN(w, 16)
100 PCI_PORT_IN(l, 32)
101 
102 
103 #define PCI_PORT_OUT(type, size) \
104 void out##type (u##size d, int addr) \
105 { \
106 	int b = PCI_PORT_HBA(addr); \
107 	EISA_OUT(size); \
108 	ASSERT(pci_port); \
109 	pci_port->out##type(parisc_pci_hba[b], PCI_PORT_ADDR(addr), d); \
110 }
111 
112 PCI_PORT_OUT(b,  8)
113 PCI_PORT_OUT(w, 16)
114 PCI_PORT_OUT(l, 32)
115 
116 
117 
118 /*
119  * BIOS32 replacement.
120  */
pcibios_init(void)121 void pcibios_init(void)
122 {
123 	if (!pci_bios)
124 		return;
125 
126 	if (pci_bios->init) {
127 		pci_bios->init();
128 	} else {
129 		printk(KERN_WARNING "pci_bios != NULL but init() is!\n");
130 	}
131 }
132 
133 
134 /* Called from pci_do_scan_bus() *after* walking a bus but before walking PPBs. */
pcibios_fixup_bus(struct pci_bus * bus)135 void pcibios_fixup_bus(struct pci_bus *bus)
136 {
137 	ASSERT(pci_bios != NULL);
138 
139 	if (pci_bios->fixup_bus) {
140 		pci_bios->fixup_bus(bus);
141 	} else {
142 		printk(KERN_WARNING "pci_bios != NULL but fixup_bus() is!\n");
143 	}
144 }
145 
146 
pcibios_setup(char * str)147 char *pcibios_setup(char *str)
148 {
149 	return str;
150 }
151 
152 
153 /*
154 ** Used in drivers/pci/quirks.c
155 */
156 struct pci_fixup pcibios_fixups[] = { {0} };
157 
158 
159 /*
160 ** called by drivers/pci/setup.c:pdev_fixup_irq()
161 */
pcibios_update_irq(struct pci_dev * dev,int irq)162 void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
163 {
164 /*
165 ** updates IRQ_LINE cfg register to reflect PCI-PCI bridge skewing.
166 **
167 ** Calling path for Alpha is:
168 **  alpha/kernel/pci.c:common_init_pci(swizzle_func, pci_map_irq_func )
169 **	drivers/pci/setup.c:pci_fixup_irqs()
170 **	    drivers/pci/setup.c:pci_fixup_irq()	(for each PCI device)
171 **		invoke swizzle and map functions
172 **	        alpha/kernel/pci.c:pcibios_update_irq()
173 **
174 ** Don't need this for PA legacy PDC systems.
175 **
176 ** On PAT PDC systems, We only support one "swizzle" for any number
177 ** of PCI-PCI bridges deep. That's how bit3 PCI expansion chassis
178 ** are implemented. The IRQ lines are "skewed" for all devices but
179 ** *NOT* routed through the PCI-PCI bridge. Ie any device "0" will
180 ** share an IRQ line. Legacy PDC is expecting this IRQ line routing
181 ** as well.
182 **
183 ** Unfortunately, PCI spec allows the IRQ lines to be routed
184 ** around the PCI bridge as long as the IRQ lines are skewed
185 ** based on the device number...<sigh>...
186 **
187 ** Lastly, dino.c might be able to use pci_fixup_irq() to
188 ** support RS-232 and PS/2 children. Not sure how but it's
189 ** something to think about.
190 */
191 }
192 
193 
194 /* ------------------------------------
195 **
196 ** Program one BAR in PCI config space.
197 **
198 ** ------------------------------------
199 ** PAT PDC systems need this routine. PA legacy PDC does not.
200 **
201 ** When BAR's are configured by linux, this routine will update
202 ** configuration space with the "normalized" address. "root" indicates
203 ** where the range starts and res is some portion of that range.
204 **
205 ** VCLASS: For all PA-RISC systems except V-class, root->start would be zero.
206 **
207 ** PAT PDC can tell us which MMIO ranges are available or already in use.
208 ** I/O port space and such are not memory mapped anyway for PA-Risc.
209 */
210 void __devinit
pcibios_update_resource(struct pci_dev * dev,struct resource * root,struct resource * res,int barnum)211 pcibios_update_resource(
212 	struct pci_dev *dev,
213 	struct resource *root,
214 	struct resource *res,
215 	int barnum
216 	)
217 {
218 	int where;
219 	u32 barval = 0;
220 
221 	DBG_RES("pcibios_update_resource(%s, ..., %d) [%lx,%lx]/%x\n",
222 		dev->slot_name,
223 		barnum, res->start, res->end, (int) res->flags);
224 
225 	if (barnum >= PCI_BRIDGE_RESOURCES) {
226 		/* handled in PCI-PCI bridge specific support */
227 		return;
228 	}
229 
230 	if (barnum == PCI_ROM_RESOURCE) {
231 		where = PCI_ROM_ADDRESS;
232 	} else {
233 		/* 0-5  standard PCI "regions" */
234 		where = PCI_BASE_ADDRESS_0 + (barnum * 4);
235 	}
236 
237 	if (res->flags & IORESOURCE_IO) {
238 		barval = PCI_PORT_ADDR(res->start);
239 	} else if (res->flags & IORESOURCE_MEM) {
240 		barval = PCI_BUS_ADDR(HBA_DATA(dev->bus->sysdata), res->start);
241 	} else {
242 		panic("pcibios_update_resource() WTF? flags not IO or MEM");
243 	}
244 
245 	pci_write_config_dword(dev, where, barval);
246 
247 /* XXX FIXME - Elroy does support 64-bit (dual cycle) addressing.
248 ** But at least one device (Symbios 53c896) which has 64-bit BAR
249 ** doesn't actually work right with dual cycle addresses.
250 ** So ignore the whole mess for now.
251 */
252 
253 	if ((res->flags & (PCI_BASE_ADDRESS_SPACE
254 			   | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
255 	    == (PCI_BASE_ADDRESS_SPACE_MEMORY
256 		| PCI_BASE_ADDRESS_MEM_TYPE_64)) {
257 		pci_write_config_dword(dev, where+4, 0);
258 		DBGC("PCIBIOS: dev %s type 64-bit\n", dev->name);
259 	}
260 }
261 
262 /*
263 ** Called by pci_set_master() - a driver interface.
264 **
265 ** Legacy PDC guarantees to set:
266 **      Map Memory BAR's into PA IO space.
267 **      Map Expansion ROM BAR into one common PA IO space per bus.
268 **      Map IO BAR's into PCI IO space.
269 **      Command (see below)
270 **      Cache Line Size
271 **      Latency Timer
272 **      Interrupt Line
273 **	PPB: secondary latency timer, io/mmio base/limit,
274 **		bus numbers, bridge control
275 **
276 */
277 void
pcibios_set_master(struct pci_dev * dev)278 pcibios_set_master(struct pci_dev *dev)
279 {
280 	u8 lat;
281 
282 	/* If someone already mucked with this, don't touch it. */
283 	pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
284 	if (lat >= 16) return;
285 
286 	/*
287 	** HP generally has fewer devices on the bus than other architectures.
288 	** upper byte is PCI_LATENCY_TIMER.
289 	*/
290         pci_write_config_word(dev, PCI_CACHE_LINE_SIZE,
291 				(0x80 << 8) | (L1_CACHE_BYTES / sizeof(u32)));
292 }
293 
294 
295 void __init
pcibios_init_bus(struct pci_bus * bus)296 pcibios_init_bus(struct pci_bus *bus)
297 {
298 	struct pci_dev *dev = bus->self;
299 
300 	/* We deal only with pci controllers and pci-pci bridges. */
301 	if (dev && (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
302 		return;
303 
304 	if (dev) {
305 		/* PCI-PCI bridge - set the cache line and default latency
306 		   (32) for primary and secondary buses. */
307 		pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 32);
308 
309 		/* Read bridge control - force SERR/PERR on */
310 		pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bus->bridge_ctl);
311 		bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
312 		pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
313 	}
314 
315 	bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
316 }
317 
318 
319 /*
320 ** KLUGE: Link the child and parent resources - generic PCI didn't
321 */
322 static void
pcibios_link_hba_resources(struct resource * hba_res,struct resource * r)323 pcibios_link_hba_resources( struct resource *hba_res, struct resource *r)
324 {
325 	if (!r->parent) {
326 		r->parent = hba_res;
327 
328 		/* reverse link is harder *sigh*  */
329 		if (r->parent->child) {
330 			if (r->parent->sibling) {
331 				struct resource *next = r->parent->sibling;
332 				while (next->sibling)
333 					 next = next->sibling;
334 				next->sibling = r;
335 			} else {
336 				r->parent->sibling = r;
337 			}
338 		} else
339 			r->parent->child = r;
340 	}
341 }
342 
343 /*
344 ** called by drivers/pci/setup-res.c:pci_setup_bridge().
345 */
pcibios_fixup_pbus_ranges(struct pci_bus * bus,struct pbus_set_ranges_data * ranges)346 void pcibios_fixup_pbus_ranges(
347 	struct pci_bus *bus,
348 	struct pbus_set_ranges_data *ranges
349 	)
350 {
351 	struct pci_hba_data *hba = HBA_DATA(bus->sysdata);
352 
353 	/*
354 	** I/O space may see busnumbers here. Something
355 	** in the form of 0xbbxxxx where bb is the bus num
356 	** and xxxx is the I/O port space address.
357 	** Remaining address translation are done in the
358 	** PCI Host adapter specific code - ie dino_out8.
359 	*/
360 	ranges->io_start = PCI_PORT_ADDR(ranges->io_start);
361 	ranges->io_end   = PCI_PORT_ADDR(ranges->io_end);
362 
363 	/* Convert MMIO addr to PCI addr (undo global virtualization) */
364 	ranges->mem_start = PCI_BUS_ADDR(hba, ranges->mem_start);
365 	ranges->mem_end   = PCI_BUS_ADDR(hba, ranges->mem_end);
366 
367 	DBG_RES("pcibios_fixup_pbus_ranges(%02x, [%lx,%lx %lx,%lx])\n", bus->number,
368 		ranges->io_start, ranges->io_end,
369 		ranges->mem_start, ranges->mem_end);
370 
371 	/* KLUGE ALERT
372 	** if this resource isn't linked to a "parent", then it seems
373 	** to be a child of the HBA - lets link it in.
374 	*/
375 	pcibios_link_hba_resources(&hba->io_space, bus->resource[0]);
376 	pcibios_link_hba_resources(&hba->lmmio_space, bus->resource[1]);
377 }
378 
379 #define MAX(val1, val2)   ((val1) > (val2) ? (val1) : (val2))
380 
381 
382 /*
383 ** pcibios align resources() is called everytime generic PCI code
384 ** wants to generate a new address. The process of looking for
385 ** an available address, each candidate is first "aligned" and
386 ** then checked if the resource is available until a match is found.
387 **
388 ** Since we are just checking candidates, don't use any fields other
389 ** than res->start.
390 */
391 void __devinit
pcibios_align_resource(void * data,struct resource * res,unsigned long size,unsigned long alignment)392 pcibios_align_resource(void *data, struct resource *res,
393 			unsigned long size, unsigned long alignment)
394 {
395 	unsigned long mask, align;
396 
397 	DBG_RES("pcibios_align_resource(%s, (%p) [%lx,%lx]/%x, 0x%lx, 0x%lx)\n",
398 		((struct pci_dev *) data)->slot_name,
399 		res->parent, res->start, res->end,
400 		(int) res->flags, size, alignment);
401 
402 	/* has resource already been aligned/assigned? */
403 	if (res->parent)
404 		return;
405 
406 	/* If it's not IO, then it's gotta be MEM */
407 	align = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
408 
409 	/* Align to largest of MIN or input size */
410 	mask = MAX(alignment, align) - 1;
411 	res->start += mask;
412 	res->start &= ~mask;
413 
414 	/*
415 	** WARNING : caller is expected to update "end" field.
416 	** We can't since it might really represent the *size*.
417 	** The difference is "end = start + size" vs "end += start".
418 	*/
419 }
420 
421 
422 int __devinit
pcibios_enable_device(struct pci_dev * dev,int mask)423 pcibios_enable_device(struct pci_dev *dev, int mask)
424 {
425 	u16 cmd;
426 	int idx;
427 
428 	/*
429 	** The various platform PDC's (aka "BIOS" for PCs) don't
430 	** enable all the same bits. We just make sure they are here.
431 	*/
432 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
433 
434 	/*
435 	** See if any resources have been allocated
436 	** While "regular" PCI devices only use 0-5, Bridges use a few
437 	** beyond that for window registers.
438 	*/
439         for (idx=0; idx<DEVICE_COUNT_RESOURCE; idx++) {
440 		struct resource *r = &dev->resource[idx];
441 
442                 /* only setup requested resources */
443 		if (!(mask & (1<<idx)))
444 			continue;
445 
446 		if (r->flags & IORESOURCE_IO)
447 			cmd |= PCI_COMMAND_IO;
448 		if (r->flags & IORESOURCE_MEM)
449 			cmd |= PCI_COMMAND_MEMORY;
450 	}
451 
452 	/*
453 	** Enable System error and Parity Error reporting by default.
454 	** Devices that do NOT want those behaviors should clear them
455 	** (eg PCI graphics, possibly networking).
456 	** Interfaces like SCSI certainly should not. We want the
457 	** system to crash if a system or parity error is detected.
458 	** At least until the device driver can recover from such an error.
459 	*/
460 	cmd |= (PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
461 
462 #if 0
463 	/* If bridge/bus controller has FBB enabled, child must too. */
464 	if (dev->bus->bridge_ctl & PCI_BRIDGE_CTL_FAST_BACK)
465 		cmd |= PCI_COMMAND_FAST_BACK;
466 #endif
467 
468 	DBGC("PCIBIOS: Enabling device %s cmd 0x%04x\n", dev->slot_name, cmd);
469 	pci_write_config_word(dev, PCI_COMMAND, cmd);
470 	return 0;
471 }
472 
473 void __init
pcibios_setup_host_bridge(struct pci_bus * bus)474 pcibios_setup_host_bridge(struct pci_bus *bus)
475 {
476 	ASSERT(pci_bios != NULL);
477 
478 #if 0
479 	if (pci_bios)
480 	{
481 		if (pci_bios->setup_host_bridge) {
482 			(*pci_bios->setup_host_bridge)(bus);
483 		}
484 	}
485 #endif
486 }
487 
488 static void __devinit
pcibios_enable_ppb(struct pci_bus * bus)489 pcibios_enable_ppb(struct pci_bus *bus)
490 {
491 	struct list_head *list;
492 
493 	/* find a leaf of the PCI bus tree. */
494         list_for_each(list, &bus->children)
495 		pcibios_enable_ppb(pci_bus_b(list));
496 
497 	if (bus->self && (bus->self->class >> 8) == PCI_CLASS_BRIDGE_PCI)
498 		pdev_enable_device(bus->self);
499 }
500 
501 
502 /*
503 ** Mostly copied from drivers/pci/setup-bus.c:pci_assign_unassigned_resources()
504 */
505 void __devinit
pcibios_assign_unassigned_resources(struct pci_bus * bus)506 pcibios_assign_unassigned_resources(struct pci_bus *bus)
507 {
508 	/* from drivers/pci/setup-bus.c */
509 	extern void pbus_size_bridges(struct pci_bus *bus);
510 	extern void pbus_assign_resources(struct pci_bus *bus);
511 
512 	pbus_size_bridges(bus);
513 	pbus_assign_resources(bus);
514 
515 	pcibios_enable_ppb(bus);
516 }
517 
518 /*
519 ** PARISC specific (unfortunately)
520 */
pcibios_register_hba(struct pci_hba_data * hba)521 void pcibios_register_hba(struct pci_hba_data *hba)
522 {
523 	ASSERT(pci_hba_count < PCI_HBA_MAX);
524 
525 	/* pci_port->in/out() uses parisc_pci_hba to lookup parameter. */
526 	parisc_pci_hba[pci_hba_count] = hba;
527 	hba->hba_num = pci_hba_count++;
528 }
529