2 * Low-Level PCI Support for the SH7751
4 * Dustin McIntire (dustin@sensoria.com)
5 * Derived from arch/i386/kernel/pci-*.c which bore the message:
6 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
8 * May be copied or modified under the terms of the GNU General Public
9 * License. See linux/COPYING for more information.
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/sched.h>
19 #include <linux/ioport.h>
20 #include <linux/errno.h>
21 #include <linux/irq.h>
23 #include <asm/machvec.h>
25 #include <asm/pci-sh7751.h>
27 struct pci_ops
*pci_check_direct(void);
28 void pcibios_resource_survey(void);
29 static u8
pcibios_swizzle(struct pci_dev
*dev
, u8
*pin
);
30 static int pcibios_lookup_irq(struct pci_dev
*dev
, u8 slot
, u8 pin
);
32 unsigned int pci_probe
= PCI_PROBE_BIOS
| PCI_PROBE_CONF1
;
33 int pcibios_last_bus
= -1;
34 struct pci_bus
*pci_root_bus
;
35 struct pci_ops
*pci_root_ops
;
38 * Direct access to PCI hardware...
41 #ifdef CONFIG_PCI_DIRECT
44 #define CONFIG_CMD(bus, devfn, where) (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
46 #define PCI_REG(reg) (SH7751_PCIREG_BASE+reg)
49 * Functions for accessing PCI configuration space with type 1 accesses
51 static int pci_conf1_read(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
, u32
*value
)
57 * PCIPDR may only be accessed as 32 bit words,
58 * so we must do byte alignment by hand
60 local_irq_save(flags
);
61 outl(CONFIG_CMD(bus
,devfn
,where
), PCI_REG(SH7751_PCIPAR
));
62 word
= inl(PCI_REG(SH7751_PCIPDR
));
63 local_irq_restore(flags
);
67 switch (where
& 0x3) {
68 case 3: *value
= (u8
)(word
>> 24); break;
69 case 2: *value
= (u8
)(word
>> 16); break;
70 case 1: *value
= (u8
)(word
>> 8); break;
74 switch (where
& 0x2) {
75 case 2: *value
= (u16
)(word
>> 16); break;
76 case 1: *value
= (u16
)(word
>> 8); break;
84 PCIDBG(4,"pci_conf1_read@0x%08x=0x%x\n", CONFIG_CMD(bus
,devfn
,where
),*value
);
86 return PCIBIOS_SUCCESSFUL
;
90 * Since SH7751 only does 32bit access we'll have to do a read,mask,write operation.
91 * We'll allow an odd byte offset, though it should be illegal.
93 static int pci_conf1_write(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
, u32 value
)
97 u32 shift
= (where
& 3) * 8;
100 mask
= ((1 << 8) - 1) << shift
; // create the byte mask
101 } else if(size
== 2){
103 return PCIBIOS_BAD_REGISTER_NUMBER
;
104 mask
= ((1 << 16) - 1) << shift
; // create the word mask
107 local_irq_save(flags
);
108 outl(CONFIG_CMD(bus
,devfn
,where
), PCI_REG(SH7751_PCIPAR
));
111 outl(value
, PCI_REG(SH7751_PCIPDR
));
112 local_irq_restore(flags
);
114 PCIDBG(4,"pci_conf1_write@0x%08x=0x%x\n", CONFIG_CMD(bus
,devfn
,where
),value
);
116 return PCIBIOS_SUCCESSFUL
;
119 word
= inl(PCI_REG(SH7751_PCIPDR
)) ;
121 word
|= value
<< shift
;
122 outl(word
, PCI_REG(SH7751_PCIPDR
));
123 local_irq_restore(flags
);
125 PCIDBG(4,"pci_conf1_write@0x%08x=0x%x\n", CONFIG_CMD(bus
,devfn
,where
),word
);
127 return PCIBIOS_SUCCESSFUL
;
132 static struct pci_ops pci_direct_conf1
= {
133 .read
= pci_conf1_read
,
134 .write
= pci_conf1_write
,
137 struct pci_ops
* __init
pci_check_direct(void)
139 unsigned int tmp
, id
;
141 /* check for SH7751 hardware */
142 id
= (SH7751_DEVICE_ID
<< 16) | SH7751_VENDOR_ID
;
143 if(inl(SH7751_PCIREG_BASE
+SH7751_PCICONF0
) != id
) {
144 PCIDBG(2,"PCI: This is not an SH7751\n");
148 * Check if configuration works.
150 if (pci_probe
& PCI_PROBE_CONF1
) {
151 tmp
= inl (PCI_REG(SH7751_PCIPAR
));
152 outl (0x80000000, PCI_REG(SH7751_PCIPAR
));
153 if (inl (PCI_REG(SH7751_PCIPAR
)) == 0x80000000) {
154 outl (tmp
, PCI_REG(SH7751_PCIPAR
));
155 printk(KERN_INFO
"PCI: Using configuration type 1\n");
156 request_region(PCI_REG(SH7751_PCIPAR
), 8, "PCI conf1");
157 return &pci_direct_conf1
;
159 outl (tmp
, PCI_REG(SH7751_PCIPAR
));
162 PCIDBG(2,"PCI: pci_check_direct failed\n");
169 * BIOS32 and PCI BIOS handling.
171 * The BIOS version of the pci functions is not yet implemented but it is left
172 * in for completeness. Currently an error will be generated at compile time.
175 #ifdef CONFIG_PCI_BIOS
177 #error PCI BIOS is not yet supported on SH7751
179 #endif /* CONFIG_PCI_BIOS */
181 /***************************************************************************************/
184 * Handle bus scanning and fixups ....
189 * Discover remaining PCI buses in case there are peer host bridges.
190 * We use the number of last PCI bus provided by the PCI BIOS.
192 static void __init
pcibios_fixup_peer_bridges(void)
199 if (pcibios_last_bus
<= 0 || pcibios_last_bus
>= 0xff)
201 PCIDBG(2,"PCI: Peer bridge fixup\n");
202 for (n
=0; n
<= pcibios_last_bus
; n
++) {
203 if (pci_bus_exists(&pci_root_buses
, n
))
206 bus
.ops
= pci_root_ops
;
208 for(dev
.devfn
=0; dev
.devfn
<256; dev
.devfn
+= 8)
209 if (!pci_read_config_word(&dev
, PCI_VENDOR_ID
, &l
) &&
210 l
!= 0x0000 && l
!= 0xffff) {
211 PCIDBG(3,"Found device at %02x:%02x [%04x]\n", n
, dev
.devfn
, l
);
212 printk(KERN_INFO
"PCI: Discovered peer bus %02x\n", n
);
213 pci_scan_bus(n
, pci_root_ops
, NULL
);
220 static void __init
pci_fixup_ide_bases(struct pci_dev
*d
)
225 * PCI IDE controllers use non-standard I/O port decoding, respect it.
227 if ((d
->class >> 8) != PCI_CLASS_STORAGE_IDE
)
229 PCIDBG(3,"PCI: IDE base address fixup for %s\n", d
->slot_name
);
231 struct resource
*r
= &d
->resource
[i
];
232 if ((r
->start
& ~0x80) == 0x374) {
240 /* Add future fixups here... */
241 struct pci_fixup pcibios_fixups
[] = {
242 { PCI_FIXUP_HEADER
, PCI_ANY_ID
, PCI_ANY_ID
, pci_fixup_ide_bases
},
247 * Called after each bus is probed, but before its children
251 void __init
pcibios_fixup_bus(struct pci_bus
*b
)
253 pci_read_bridge_bases(b
);
257 * Initialization. Try all known PCI access methods. Note that we support
258 * using both PCI BIOS and direct access: in such cases, we use I/O ports
259 * to access config space.
261 * Note that the platform specific initialization (BSC registers, and memory
262 * space mapping) will be called via the machine vectors (sh_mv.mv_pci_init()) if it
263 * exitst and via the platform defined function pcibios_init_platform().
264 * See pci_bigsur.c for implementation;
266 * The BIOS version of the pci functions is not yet implemented but it is left
267 * in for completeness. Currently an error will be genereated at compile time.
270 void __init
pcibios_init(void)
272 struct pci_ops
*bios
= NULL
;
273 struct pci_ops
*dir
= NULL
;
275 PCIDBG(1,"PCI: Starting intialization.\n");
276 #ifdef CONFIG_PCI_BIOS
277 if ((pci_probe
& PCI_PROBE_BIOS
) && ((bios
= pci_find_bios()))) {
278 pci_probe
|= PCI_BIOS_SORT
;
279 pci_bios_present
= 1;
282 #ifdef CONFIG_PCI_DIRECT
283 if (pci_probe
& PCI_PROBE_CONF1
)
284 dir
= pci_check_direct();
288 if(!pcibios_init_platform())
289 PCIDBG(1,"PCI: Initialization failed\n");
290 if (sh_mv
.mv_init_pci
!= NULL
)
296 PCIDBG(1,"PCI: No PCI bus detected\n");
300 PCIDBG(1,"PCI: Probing PCI hardware\n");
301 pci_root_bus
= pci_scan_bus(0, pci_root_ops
, NULL
);
302 //pci_assign_unassigned_resources();
303 pci_fixup_irqs(pcibios_swizzle
, pcibios_lookup_irq
);
304 pcibios_fixup_peer_bridges();
305 pcibios_resource_survey();
307 #ifdef CONFIG_PCI_BIOS
308 if ((pci_probe
& PCI_BIOS_SORT
) && !(pci_probe
& PCI_NO_SORT
))
313 char * __init
pcibios_setup(char *str
)
315 if (!strcmp(str
, "off")) {
319 #ifdef CONFIG_PCI_BIOS
320 else if (!strcmp(str
, "bios")) {
321 pci_probe
= PCI_PROBE_BIOS
;
323 } else if (!strcmp(str
, "nobios")) {
324 pci_probe
&= ~PCI_PROBE_BIOS
;
326 } else if (!strcmp(str
, "nosort")) {
327 pci_probe
|= PCI_NO_SORT
;
329 } else if (!strcmp(str
, "biosirq")) {
330 pci_probe
|= PCI_BIOS_IRQ_SCAN
;
334 #ifdef CONFIG_PCI_DIRECT
335 else if (!strcmp(str
, "conf1")) {
336 pci_probe
= PCI_PROBE_CONF1
| PCI_NO_CHECKS
;
340 else if (!strcmp(str
, "rom")) {
341 pci_probe
|= PCI_ASSIGN_ROMS
;
343 } else if (!strncmp(str
, "lastbus=", 8)) {
344 pcibios_last_bus
= simple_strtol(str
+8, NULL
, 0);
351 * Allocate the bridge and device resources
354 static void __init
pcibios_allocate_bus_resources(struct list_head
*bus_list
)
356 struct list_head
*ln
;
360 struct resource
*r
, *pr
;
362 PCIDBG(2,"PCI: pcibios_allocate_bus_reasources called\n" );
363 /* Depth-First Search on bus tree */
364 for (ln
=bus_list
->next
; ln
!= bus_list
; ln
=ln
->next
) {
366 if ((dev
= bus
->self
)) {
367 for (idx
= PCI_BRIDGE_RESOURCES
; idx
< PCI_NUM_RESOURCES
; idx
++) {
368 r
= &dev
->resource
[idx
];
371 pr
= pci_find_parent_resource(dev
, r
);
372 if (!pr
|| request_resource(pr
, r
) < 0)
373 printk(KERN_ERR
"PCI: Cannot allocate resource region %d of bridge %s\n", idx
, dev
->slot_name
);
376 pcibios_allocate_bus_resources(&bus
->children
);
380 static void __init
pcibios_allocate_resources(int pass
)
385 struct resource
*r
, *pr
;
387 PCIDBG(2,"PCI: pcibios_allocate_resources pass %d called\n", pass
);
388 pci_for_each_dev(dev
) {
389 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
390 for(idx
= 0; idx
< 6; idx
++) {
391 r
= &dev
->resource
[idx
];
392 if (r
->parent
) /* Already allocated */
394 if (!r
->start
) /* Address not assigned at all */
396 if (r
->flags
& IORESOURCE_IO
)
397 disabled
= !(command
& PCI_COMMAND_IO
);
399 disabled
= !(command
& PCI_COMMAND_MEMORY
);
400 if (pass
== disabled
) {
401 PCIDBG(3,"PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
402 r
->start
, r
->end
, r
->flags
, disabled
, pass
);
403 pr
= pci_find_parent_resource(dev
, r
);
404 if (!pr
|| request_resource(pr
, r
) < 0) {
405 printk(KERN_ERR
"PCI: Cannot allocate resource region %d of device %s\n", idx
, dev
->slot_name
);
406 /* We'll assign a new address later */
413 r
= &dev
->resource
[PCI_ROM_RESOURCE
];
414 if (r
->flags
& PCI_ROM_ADDRESS_ENABLE
) {
415 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
417 PCIDBG(3,"PCI: Switching off ROM of %s\n", dev
->slot_name
);
418 r
->flags
&= ~PCI_ROM_ADDRESS_ENABLE
;
419 pci_read_config_dword(dev
, dev
->rom_base_reg
, ®
);
420 pci_write_config_dword(dev
, dev
->rom_base_reg
, reg
& ~PCI_ROM_ADDRESS_ENABLE
);
426 static void __init
pcibios_assign_resources(void)
432 PCIDBG(2,"PCI: pcibios_assign_resources called\n");
433 pci_for_each_dev(dev
) {
434 int class = dev
->class >> 8;
436 /* Don't touch classless devices and host bridges */
437 if (!class || class == PCI_CLASS_BRIDGE_HOST
)
440 for(idx
=0; idx
<6; idx
++) {
441 r
= &dev
->resource
[idx
];
444 * Don't touch IDE controllers and I/O ports of video cards!
446 if ((class == PCI_CLASS_STORAGE_IDE
&& idx
< 4) ||
447 (class == PCI_CLASS_DISPLAY_VGA
&& (r
->flags
& IORESOURCE_IO
)))
451 * We shall assign a new address to this resource, either because
452 * the BIOS forgot to do so or because we have decided the old
453 * address was unusable for some reason.
455 if (!r
->start
&& r
->end
)
456 pci_assign_resource(dev
, idx
);
459 if (pci_probe
& PCI_ASSIGN_ROMS
) {
460 r
= &dev
->resource
[PCI_ROM_RESOURCE
];
464 pci_assign_resource(dev
, PCI_ROM_RESOURCE
);
469 void __init
pcibios_resource_survey(void)
471 PCIDBG(1,"PCI: Allocating resources\n");
472 pcibios_allocate_bus_resources(&pci_root_buses
);
473 pcibios_allocate_resources(0);
474 pcibios_allocate_resources(1);
475 pcibios_assign_resources();
479 /***************************************************************************************/
483 static u8 __init
pcibios_swizzle(struct pci_dev
*dev
, u8
*pin
)
486 return PCI_SLOT(dev
->devfn
);
489 static int pcibios_lookup_irq(struct pci_dev
*dev
, u8 slot
, u8 pin
)
493 /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
494 irq
= pcibios_map_platform_irq(slot
,pin
);
496 PCIDBG(3,"PCI: Error mapping IRQ on device %s\n", dev
->name
);
500 PCIDBG(2,"Setting IRQ for slot %s to %d\n", dev
->slot_name
, irq
);