blob: aea3b6b3033a7ec5a1ea5ea8f34030d5e5f5228e [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
Paul Mundt959f85f2006-09-27 16:43:28 +090024static u8 __init simple_swizzle(struct pci_dev *dev, u8 *pinp)
25{
26 u8 pin = *pinp;
27
28 while (dev->bus->parent) {
Bjorn Helgaas6aa6e492008-12-09 16:12:32 -070029 pin = pci_swizzle_interrupt_pin(dev, pin);
Paul Mundt959f85f2006-09-27 16:43:28 +090030 /* Move up the chain of bridges. */
31 dev = dev->bus->self;
32 }
33 *pinp = pin;
34
35 /* The slot is the slot of the last bridge. */
36 return PCI_SLOT(dev->devfn);
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int __init pcibios_init(void)
40{
41 struct pci_channel *p;
42 struct pci_bus *bus;
43 int busno;
44
45#ifdef CONFIG_PCI_AUTO
46 /* assign resources */
47 busno = 0;
Jamie Lenehana09749d2006-09-27 15:05:39 +090048 for (p = board_pci_channels; p->pci_ops != NULL; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 busno = pciauto_assign_resources(busno, p) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
51
52 /* scan the buses */
53 busno = 0;
Paul Mundt959f85f2006-09-27 16:43:28 +090054 for (p = board_pci_channels; p->pci_ops != NULL; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 bus = pci_scan_bus(busno, p->pci_ops, p);
Paul Mundt959f85f2006-09-27 16:43:28 +090056 busno = bus->subordinate + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58
Paul Mundt959f85f2006-09-27 16:43:28 +090059 pci_fixup_irqs(simple_swizzle, pcibios_map_platform_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 return 0;
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063subsys_initcall(pcibios_init);
64
Paul Mundt959f85f2006-09-27 16:43:28 +090065/*
66 * Called after each bus is probed, but before its children
67 * are examined.
68 */
Paul Mundtb6d7b662007-11-22 16:29:10 +090069void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
Paul Mundt959f85f2006-09-27 16:43:28 +090070{
71 pci_read_bridge_bases(bus);
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070075 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 __attribute__ ((weak));
77
78/*
79 * We need to avoid collisions with `mirrored' VGA ports
80 * and other strange ISA hardware, so we always want the
81 * addresses to be allocated in the 0x000-0x0ff region
82 * modulo 0x400.
83 */
84void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070085 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070088 resource_size_t start = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (start & 0x300) {
91 start = (start + 0x3ff) & ~0x3ff;
92 res->start = start;
93 }
94 }
95}
96
97int pcibios_enable_device(struct pci_dev *dev, int mask)
98{
99 u16 cmd, old_cmd;
100 int idx;
101 struct resource *r;
102
103 pci_read_config_word(dev, PCI_COMMAND, &cmd);
104 old_cmd = cmd;
105 for(idx=0; idx<6; idx++) {
106 if (!(mask & (1 << idx)))
107 continue;
108 r = &dev->resource[idx];
109 if (!r->start && r->end) {
110 printk(KERN_ERR "PCI: Device %s not available because "
111 "of resource collisions\n", pci_name(dev));
112 return -EINVAL;
113 }
114 if (r->flags & IORESOURCE_IO)
115 cmd |= PCI_COMMAND_IO;
116 if (r->flags & IORESOURCE_MEM)
117 cmd |= PCI_COMMAND_MEMORY;
118 }
119 if (dev->resource[PCI_ROM_RESOURCE].start)
120 cmd |= PCI_COMMAND_MEMORY;
121 if (cmd != old_cmd) {
122 printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
123 pci_name(dev), old_cmd, cmd);
124 pci_write_config_word(dev, PCI_COMMAND, cmd);
125 }
126 return 0;
127}
128
129/*
130 * If we set up a device for bus mastering, we need to check and set
131 * the latency timer as it may not be properly set.
132 */
Adrian Bunk62410032008-06-18 01:33:40 +0300133static unsigned int pcibios_max_latency = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135void pcibios_set_master(struct pci_dev *dev)
136{
137 u8 lat;
138 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
139 if (lat < 16)
140 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
141 else if (lat > pcibios_max_latency)
142 lat = pcibios_max_latency;
143 else
144 return;
Jamie Lenehana09749d2006-09-27 15:05:39 +0900145 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
146 pci_name(dev), lat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
148}
149
150void __init pcibios_update_irq(struct pci_dev *dev, int irq)
151{
152 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
153}
Jamie Lenehana09749d2006-09-27 15:05:39 +0900154
155void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
156{
Benjamin Herrenschmidtb70d3a22008-04-29 00:59:11 -0700157 resource_size_t start = pci_resource_start(dev, bar);
158 resource_size_t len = pci_resource_len(dev, bar);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900159 unsigned long flags = pci_resource_flags(dev, bar);
160
Paul Mundta3e61d52006-09-27 16:45:22 +0900161 if (unlikely(!len || !start))
Jamie Lenehana09749d2006-09-27 15:05:39 +0900162 return NULL;
163 if (maxlen && len > maxlen)
164 len = maxlen;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900165
166 /*
167 * Presently the IORESOURCE_MEM case is a bit special, most
168 * SH7751 style PCI controllers have PCI memory at a fixed
169 * location in the address space where no remapping is desired
Paul Mundta3e61d52006-09-27 16:45:22 +0900170 * (typically at 0xfd000000, but is_pci_memaddr() will know
171 * best). With the IORESOURCE_MEM case more care has to be taken
172 * to inhibit page table mapping for legacy cores, but this is
173 * punted off to __ioremap().
174 * -- PFM.
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900175 */
Paul Mundta3e61d52006-09-27 16:45:22 +0900176 if (flags & IORESOURCE_IO)
Jamie Lenehana09749d2006-09-27 15:05:39 +0900177 return ioport_map(start, len);
Paul Mundta3e61d52006-09-27 16:45:22 +0900178 if (flags & IORESOURCE_MEM)
179 return ioremap(start, len);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900180
181 return NULL;
182}
Paul Mundt959f85f2006-09-27 16:43:28 +0900183EXPORT_SYMBOL(pci_iomap);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900184
185void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
186{
187 iounmap(addr);
188}
Jamie Lenehana09749d2006-09-27 15:05:39 +0900189EXPORT_SYMBOL(pci_iounmap);