blob: 6d659cd93c9d47dd4ce21ecf05bfd0997188686c [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>
Paul Mundte588a002009-04-14 15:23:40 +090022#include <linux/dma-debug.h>
Jamie Lenehana09749d2006-09-27 15:05:39 +090023#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static int __init pcibios_init(void)
26{
27 struct pci_channel *p;
28 struct pci_bus *bus;
29 int busno;
30
Magnus Dammd0e3db42009-03-11 15:46:14 +090031 /* init channels */
32 busno = 0;
33 for (p = board_pci_channels; p->init; p++) {
34 if (p->init(p) == 0)
35 p->enabled = 1;
36 else
37 pr_err("Unable to init pci channel %d\n", busno);
38 busno++;
39 }
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef CONFIG_PCI_AUTO
42 /* assign resources */
43 busno = 0;
Magnus Dammd0e3db42009-03-11 15:46:14 +090044 for (p = board_pci_channels; p->init; p++)
45 if (p->enabled)
46 busno = pciauto_assign_resources(busno, p) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
48
49 /* scan the buses */
50 busno = 0;
Magnus Dammd0e3db42009-03-11 15:46:14 +090051 for (p = board_pci_channels; p->init; p++) {
52 if (p->enabled) {
53 bus = pci_scan_bus(busno, p->pci_ops, p);
54 busno = bus->subordinate + 1;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
57
Bjorn Helgaase5582342008-12-16 21:37:15 -070058 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paul Mundte588a002009-04-14 15:23:40 +090060 dma_debug_add_bus(&pci_bus_type);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064subsys_initcall(pcibios_init);
65
Paul Mundt959f85f2006-09-27 16:43:28 +090066/*
67 * Called after each bus is probed, but before its children
68 * are examined.
69 */
Paul Mundtb6d7b662007-11-22 16:29:10 +090070void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
Paul Mundt959f85f2006-09-27 16:43:28 +090071{
72 pci_read_bridge_bases(bus);
73}
74
Paul Mundt9ade1212009-04-20 15:38:25 +090075void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
76 struct resource *res)
77{
78 region->start = res->start;
79 region->end = res->end;
80}
81
82void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
83 struct pci_bus_region *region)
84{
85 res->start = region->start;
86 res->end = region->end;
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070090 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 __attribute__ ((weak));
92
93/*
94 * We need to avoid collisions with `mirrored' VGA ports
95 * and other strange ISA hardware, so we always want the
96 * addresses to be allocated in the 0x000-0x0ff region
97 * modulo 0x400.
98 */
99void pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700100 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700103 resource_size_t start = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (start & 0x300) {
106 start = (start + 0x3ff) & ~0x3ff;
107 res->start = start;
108 }
109 }
110}
111
112int pcibios_enable_device(struct pci_dev *dev, int mask)
113{
114 u16 cmd, old_cmd;
115 int idx;
116 struct resource *r;
117
118 pci_read_config_word(dev, PCI_COMMAND, &cmd);
119 old_cmd = cmd;
120 for(idx=0; idx<6; idx++) {
121 if (!(mask & (1 << idx)))
122 continue;
123 r = &dev->resource[idx];
124 if (!r->start && r->end) {
125 printk(KERN_ERR "PCI: Device %s not available because "
126 "of resource collisions\n", pci_name(dev));
127 return -EINVAL;
128 }
129 if (r->flags & IORESOURCE_IO)
130 cmd |= PCI_COMMAND_IO;
131 if (r->flags & IORESOURCE_MEM)
132 cmd |= PCI_COMMAND_MEMORY;
133 }
134 if (dev->resource[PCI_ROM_RESOURCE].start)
135 cmd |= PCI_COMMAND_MEMORY;
136 if (cmd != old_cmd) {
137 printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
138 pci_name(dev), old_cmd, cmd);
139 pci_write_config_word(dev, PCI_COMMAND, cmd);
140 }
141 return 0;
142}
143
144/*
145 * If we set up a device for bus mastering, we need to check and set
146 * the latency timer as it may not be properly set.
147 */
Adrian Bunk62410032008-06-18 01:33:40 +0300148static unsigned int pcibios_max_latency = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150void pcibios_set_master(struct pci_dev *dev)
151{
152 u8 lat;
153 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
154 if (lat < 16)
155 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
156 else if (lat > pcibios_max_latency)
157 lat = pcibios_max_latency;
158 else
159 return;
Jamie Lenehana09749d2006-09-27 15:05:39 +0900160 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
161 pci_name(dev), lat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
163}
164
165void __init pcibios_update_irq(struct pci_dev *dev, int irq)
166{
167 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
168}
Jamie Lenehana09749d2006-09-27 15:05:39 +0900169
170void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
171{
Benjamin Herrenschmidtb70d3a22008-04-29 00:59:11 -0700172 resource_size_t start = pci_resource_start(dev, bar);
173 resource_size_t len = pci_resource_len(dev, bar);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900174 unsigned long flags = pci_resource_flags(dev, bar);
175
Paul Mundta3e61d52006-09-27 16:45:22 +0900176 if (unlikely(!len || !start))
Jamie Lenehana09749d2006-09-27 15:05:39 +0900177 return NULL;
178 if (maxlen && len > maxlen)
179 len = maxlen;
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900180
181 /*
182 * Presently the IORESOURCE_MEM case is a bit special, most
183 * SH7751 style PCI controllers have PCI memory at a fixed
Magnus Dammef339f22008-02-19 21:35:22 +0900184 * location in the address space where no remapping is desired.
185 * With the IORESOURCE_MEM case more care has to be taken
Paul Mundta3e61d52006-09-27 16:45:22 +0900186 * to inhibit page table mapping for legacy cores, but this is
187 * punted off to __ioremap().
188 * -- PFM.
Paul Mundtd7cdc9e2006-09-27 15:16:42 +0900189 */
Paul Mundta3e61d52006-09-27 16:45:22 +0900190 if (flags & IORESOURCE_IO)
Jamie Lenehana09749d2006-09-27 15:05:39 +0900191 return ioport_map(start, len);
Paul Mundta3e61d52006-09-27 16:45:22 +0900192 if (flags & IORESOURCE_MEM)
193 return ioremap(start, len);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900194
195 return NULL;
196}
Paul Mundt959f85f2006-09-27 16:43:28 +0900197EXPORT_SYMBOL(pci_iomap);
Jamie Lenehana09749d2006-09-27 15:05:39 +0900198
199void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
200{
201 iounmap(addr);
202}
Jamie Lenehana09749d2006-09-27 15:05:39 +0900203EXPORT_SYMBOL(pci_iounmap);
Magnus Dammaa5d3ff2008-02-19 21:35:40 +0900204
205EXPORT_SYMBOL(board_pci_channels);