blob: c92e65045c68197404aa9525010a3b18cb038ad8 [file] [log] [blame]
Paul Mundt4c5107e2009-04-20 15:43:36 +09001/*
2 * New-style PCI core.
3 *
4 * Copyright (c) 2002 M. R. Brown
5 * Copyright (c) 2004 - 2009 Paul Mundt
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
11#include <linux/kernel.h>
12#include <linux/pci.h>
13#include <linux/init.h>
14#include <linux/dma-debug.h>
15#include <linux/io.h>
16
17static int __init pcibios_init(void)
18{
19 struct pci_channel *p;
20 struct pci_bus *bus;
21 int busno;
22
23 /* init channels */
24 busno = 0;
25 for (p = board_pci_channels; p->init; p++) {
26 if (p->init(p) == 0)
27 p->enabled = 1;
28 else
29 pr_err("Unable to init pci channel %d\n", busno);
30 busno++;
31 }
32
33 /* scan the buses */
34 busno = 0;
35 for (p = board_pci_channels; p->init; p++) {
36 if (p->enabled) {
37 bus = pci_scan_bus(busno, p->pci_ops, p);
38 busno = bus->subordinate + 1;
39
40 pci_bus_size_bridges(bus);
41 pci_bus_assign_resources(bus);
42 pci_enable_bridges(bus);
43 }
44 }
45
46 pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
47
48 dma_debug_add_bus(&pci_bus_type);
49
50 return 0;
51}
52subsys_initcall(pcibios_init);
53
54static void pcibios_fixup_device_resources(struct pci_dev *dev,
55 struct pci_bus *bus)
56{
57 /* Update device resources. */
58 struct pci_channel *chan = bus->sysdata;
59 unsigned long offset = 0;
60 int i;
61
62 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
63 if (!dev->resource[i].start)
64 continue;
65 if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
66 continue;
67 if (dev->resource[i].flags & IORESOURCE_IO)
68 offset = chan->io_base;
69 else if (dev->resource[i].flags & IORESOURCE_MEM)
70 offset = 0;
71
72 dev->resource[i].start += offset;
73 dev->resource[i].end += offset;
74 }
75}
76
77
78/*
79 * Called after each bus is probed, but before its children
80 * are examined.
81 */
82void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
83{
84 struct pci_dev *dev = bus->self;
85 struct list_head *ln;
86 struct pci_channel *chan = bus->sysdata;
87
88 if (!dev) {
89 bus->resource[0] = chan->io_resource;
90 bus->resource[1] = chan->mem_resource;
91 }
92
93 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
94 dev = pci_dev_b(ln);
95
96 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
97 pcibios_fixup_device_resources(dev, bus);
98 }
99}
100
101void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
102 struct resource *res)
103{
104 struct pci_channel *chan = dev->sysdata;
105 unsigned long offset = 0;
106
107 if (res->flags & IORESOURCE_IO)
108 offset = chan->io_base;
109 else if (res->flags & IORESOURCE_MEM)
110 offset = 0;
111
112 region->start = res->start - offset;
113 region->end = res->end - offset;
114}
115
116void __devinit
117pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
118 struct pci_bus_region *region)
119{
120 struct pci_channel *chan = dev->sysdata;
121 unsigned long offset = 0;
122
123 if (res->flags & IORESOURCE_IO)
124 offset = chan->io_base;
125 else if (res->flags & IORESOURCE_MEM)
126 offset = 0;
127
128 res->start = region->start + offset;
129 res->end = region->end + offset;
130}
131
Paul Mundt4c5107e2009-04-20 15:43:36 +0900132int pcibios_enable_device(struct pci_dev *dev, int mask)
133{
134 u16 cmd, old_cmd;
135 int idx;
136 struct resource *r;
137
138 pci_read_config_word(dev, PCI_COMMAND, &cmd);
139 old_cmd = cmd;
140 for(idx=0; idx<6; idx++) {
141 if (!(mask & (1 << idx)))
142 continue;
143 r = &dev->resource[idx];
144 if (!r->start && r->end) {
145 printk(KERN_ERR "PCI: Device %s not available because "
146 "of resource collisions\n", pci_name(dev));
147 return -EINVAL;
148 }
149 if (r->flags & IORESOURCE_IO)
150 cmd |= PCI_COMMAND_IO;
151 if (r->flags & IORESOURCE_MEM)
152 cmd |= PCI_COMMAND_MEMORY;
153 }
154 if (dev->resource[PCI_ROM_RESOURCE].start)
155 cmd |= PCI_COMMAND_MEMORY;
156 if (cmd != old_cmd) {
157 printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
158 pci_name(dev), old_cmd, cmd);
159 pci_write_config_word(dev, PCI_COMMAND, cmd);
160 }
161 return 0;
162}
163
164/*
165 * If we set up a device for bus mastering, we need to check and set
166 * the latency timer as it may not be properly set.
167 */
168static unsigned int pcibios_max_latency = 255;
169
170void pcibios_set_master(struct pci_dev *dev)
171{
172 u8 lat;
173 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
174 if (lat < 16)
175 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
176 else if (lat > pcibios_max_latency)
177 lat = pcibios_max_latency;
178 else
179 return;
180 printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
181 pci_name(dev), lat);
182 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
183}
184
185void __init pcibios_update_irq(struct pci_dev *dev, int irq)
186{
187 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
188}
189
Paul Mundt4c5107e2009-04-20 15:43:36 +0900190EXPORT_SYMBOL(board_pci_channels);