blob: e36c7b870861ada365ccacad17b94108a851a6d4 [file] [log] [blame]
Jamie Lenehana09749d2006-09-27 15:05:39 +09001/*
2 * arch/sh/drivers/pci/pci.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2002 M. R. Brown <mrbrown@linux-sh.org>
Paul Mundtd7cdc9e2006-09-27 15:16:42 +09005 * Copyright (c) 2004 - 2006 Paul Mundt <lethal@linux-sh.org>
Jamie Lenehana09749d2006-09-27 15:05:39 +09006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * These functions are collected here to reduce duplication of common
8 * code amongst the many platform-specific PCI support code files.
Jamie Lenehana09749d2006-09-27 15:05:39 +09009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * These routines require the following board-specific routines:
11 * void pcibios_fixup_irqs();
12 *
13 * See include/asm-sh/pci.h for more information.
Jamie Lenehana09749d2006-09-27 15:05:39 +090014 *
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file "COPYING" in the main directory of this archive
17 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/pci.h>
21#include <linux/init.h>
Jamie Lenehana09749d2006-09-27 15:05:39 +090022#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static int __init pcibios_init(void)
25{
26 struct pci_channel *p;
27 struct pci_bus *bus;
28 int busno;
29
30#ifdef CONFIG_PCI_AUTO
31 /* assign resources */
32 busno = 0;
Jamie Lenehana09749d2006-09-27 15:05:39 +090033 for (p = board_pci_channels; p->pci_ops != NULL; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 busno = pciauto_assign_resources(busno, p) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#endif
36
37 /* scan the buses */
38 busno = 0;
Paul Mundt959f85f2006-09-27 16:43:28 +090039 for (p = board_pci_channels; p->pci_ops != NULL; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 bus = pci_scan_bus(busno, p->pci_ops, p);
Paul Mundt959f85f2006-09-27 16:43:28 +090041 busno = bus->subordinate + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43
Bjorn Helgaase5582342008-12-16 21:37:15 -070044 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 return 0;
47}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048subsys_initcall(pcibios_init);
49
Paul Mundt959f85f2006-09-27 16:43:28 +090050/*
51 * Called after each bus is probed, but before its children
52 * are examined.
53 */
Paul Mundtb6d7b662007-11-22 16:29:10 +090054void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
Paul Mundt959f85f2006-09-27 16:43:28 +090055{
56 pci_read_bridge_bases(bus);
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070060 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 __attribute__ ((weak));
62
63/*
64 * We need to avoid collisions with `mirrored' VGA ports
65 * and other strange ISA hardware, so we always want the
66 * addresses to be allocated in the 0x000-0x0ff region
67 * modulo 0x400.
68 */
69void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070070 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070073 resource_size_t start = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (start & 0x300) {
76 start = (start + 0x3ff) & ~0x3ff;
77 res->start = start;
78 }
79 }
80}
81
82int pcibios_enable_device(struct pci_dev *dev, int mask)
83{
84 u16 cmd, old_cmd;
85 int idx;
86 struct resource *r;
87
88 pci_read_config_word(dev, PCI_COMMAND, &cmd);
89 old_cmd = cmd;
90 for(idx=0; idx<6; idx++) {
91 if (!(mask & (1 << idx)))
92 continue;
93 r = &dev->resource[idx];
94 if (!r->start && r->end) {
95 printk(KERN_ERR "PCI: Device %s not available because "
96 "of resource collisions\n", pci_name(dev));
97 return -EINVAL;
98 }
99 if (r->flags & IORESOURCE_IO)
100 cmd |= PCI_COMMAND_IO;
101 if (r->flags & IORESOURCE_MEM)
102 cmd |= PCI_COMMAND_MEMORY;
103 }
104 if (dev->resource[PCI_ROM_RESOURCE].start)
105 cmd |= PCI_COMMAND_MEMORY;
106 if (cmd != old_cmd) {
107 printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
108 pci_name(dev), old_cmd, cmd);
109 pci_write_config_word(dev, PCI_COMMAND, cmd);
110 }
111 return 0;
112}
113
114/*
115 * If we set up a device for bus mastering, we need to check and set
116 * the latency timer as it may not be properly set.
117 */
Adrian Bunk62410032008-06-18 01:33:40 +0300118static unsigned int pcibios_max_latency = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120void pcibios_set_master(struct pci_dev *dev)
121{
122 u8 lat;
123 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
124 if (lat < 16)
125 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
126 else if (lat > pcibios_max_latency)
127 lat = pcibios_max_latency;
128 else
129 return;
Jamie Lenehana09749d2006-09-27 15:05:39 +0900130 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
131 pci_name(dev), lat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
133}
134
135void __init pcibios_update_irq(struct pci_dev *dev, int irq)
136{
137 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
138}
Jamie Lenehana09749d2006-09-27 15:05:39 +0900139
140void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
141{
Benjamin Herrenschmidtb70d3a22008-04-29 00:59:11 -0700142 resource_size_t start = pci_resource_start(dev, bar);
143 resource_size_t len = pci_resource_len(dev, bar);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900144 unsigned long flags = pci_resource_flags(dev, bar);
145
Paul Mundta3e61d52006-09-27 16:45:22 +0900146 if (unlikely(!len || !start))
Jamie Lenehana09749d2006-09-27 15:05:39 +0900147 return NULL;
148 if (maxlen && len > maxlen)
149 len = maxlen;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900150
151 /*
152 * Presently the IORESOURCE_MEM case is a bit special, most
153 * SH7751 style PCI controllers have PCI memory at a fixed
154 * location in the address space where no remapping is desired
Paul Mundta3e61d52006-09-27 16:45:22 +0900155 * (typically at 0xfd000000, but is_pci_memaddr() will know
156 * best). With the IORESOURCE_MEM case more care has to be taken
157 * to inhibit page table mapping for legacy cores, but this is
158 * punted off to __ioremap().
159 * -- PFM.
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900160 */
Paul Mundta3e61d52006-09-27 16:45:22 +0900161 if (flags & IORESOURCE_IO)
Jamie Lenehana09749d2006-09-27 15:05:39 +0900162 return ioport_map(start, len);
Paul Mundta3e61d52006-09-27 16:45:22 +0900163 if (flags & IORESOURCE_MEM)
164 return ioremap(start, len);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900165
166 return NULL;
167}
Paul Mundt959f85f2006-09-27 16:43:28 +0900168EXPORT_SYMBOL(pci_iomap);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900169
170void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
171{
172 iounmap(addr);
173}
Jamie Lenehana09749d2006-09-27 15:05:39 +0900174EXPORT_SYMBOL(pci_iounmap);