1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/init.h>
17#include <linux/ioport.h>
18#include <linux/errno.h>
19
20#include "pci-frv.h"
21
22#if 0
23void
24pcibios_update_resource(struct pci_dev *dev, struct resource *root,
25 struct resource *res, int resource)
26{
27 u32 new, check;
28 int reg;
29
30 new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
31 if (resource < 6) {
32 reg = PCI_BASE_ADDRESS_0 + 4*resource;
33 } else if (resource == PCI_ROM_RESOURCE) {
34 res->flags |= IORESOURCE_ROM_ENABLE;
35 new |= PCI_ROM_ADDRESS_ENABLE;
36 reg = dev->rom_base_reg;
37 } else {
38
39 return;
40 }
41
42 pci_write_config_dword(dev, reg, new);
43 pci_read_config_dword(dev, reg, &check);
44 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
45 printk(KERN_ERR "PCI: Error while updating region "
46 "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
47 new, check);
48 }
49}
50#endif
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65void
66pcibios_align_resource(void *data, struct resource *res,
67 resource_size_t size, resource_size_t align)
68{
69 if (res->flags & IORESOURCE_IO) {
70 resource_size_t start = res->start;
71
72 if (start & 0x300) {
73 start = (start + 0x3ff) & ~0x3ff;
74 res->start = start;
75 }
76 }
77}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
114{
115 struct list_head *ln;
116 struct pci_bus *bus;
117 struct pci_dev *dev;
118 int idx;
119 struct resource *r, *pr;
120
121
122 for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
123 bus = pci_bus_b(ln);
124 if ((dev = bus->self)) {
125 for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
126 r = &dev->resource[idx];
127 if (!r->start)
128 continue;
129 pr = pci_find_parent_resource(dev, r);
130 if (!pr || request_resource(pr, r) < 0)
131 printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, pci_name(dev));
132 }
133 }
134 pcibios_allocate_bus_resources(&bus->children);
135 }
136}
137
138static void __init pcibios_allocate_resources(int pass)
139{
140 struct pci_dev *dev = NULL;
141 int idx, disabled;
142 u16 command;
143 struct resource *r, *pr;
144
145 for_each_pci_dev(dev) {
146 pci_read_config_word(dev, PCI_COMMAND, &command);
147 for(idx = 0; idx < 6; idx++) {
148 r = &dev->resource[idx];
149 if (r->parent)
150 continue;
151 if (!r->start)
152 continue;
153 if (r->flags & IORESOURCE_IO)
154 disabled = !(command & PCI_COMMAND_IO);
155 else
156 disabled = !(command & PCI_COMMAND_MEMORY);
157 if (pass == disabled) {
158 DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
159 r->start, r->end, r->flags, disabled, pass);
160 pr = pci_find_parent_resource(dev, r);
161 if (!pr || request_resource(pr, r) < 0) {
162 printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, pci_name(dev));
163
164 r->end -= r->start;
165 r->start = 0;
166 }
167 }
168 }
169 if (!pass) {
170 r = &dev->resource[PCI_ROM_RESOURCE];
171 if (r->flags & IORESOURCE_ROM_ENABLE) {
172
173 u32 reg;
174 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
175 r->flags &= ~IORESOURCE_ROM_ENABLE;
176 pci_read_config_dword(dev, dev->rom_base_reg, ®);
177 pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
178 }
179 }
180 }
181}
182
183static void __init pcibios_assign_resources(void)
184{
185 struct pci_dev *dev = NULL;
186 int idx;
187 struct resource *r;
188
189 for_each_pci_dev(dev) {
190 int class = dev->class >> 8;
191
192
193 if (!class || class == PCI_CLASS_BRIDGE_HOST)
194 continue;
195
196 for(idx=0; idx<6; idx++) {
197 r = &dev->resource[idx];
198
199
200
201
202 if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
203 (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
204 continue;
205
206
207
208
209
210
211 if (!r->start && r->end)
212 pci_assign_resource(dev, idx);
213 }
214
215 if (pci_probe & PCI_ASSIGN_ROMS) {
216 r = &dev->resource[PCI_ROM_RESOURCE];
217 r->end -= r->start;
218 r->start = 0;
219 if (r->end)
220 pci_assign_resource(dev, PCI_ROM_RESOURCE);
221 }
222 }
223}
224
225void __init pcibios_resource_survey(void)
226{
227 DBG("PCI: Allocating resources\n");
228 pcibios_allocate_bus_resources(&pci_root_buses);
229 pcibios_allocate_resources(0);
230 pcibios_allocate_resources(1);
231 pcibios_assign_resources();
232}
233
234int pcibios_enable_resources(struct pci_dev *dev, int mask)
235{
236 u16 cmd, old_cmd;
237 int idx;
238 struct resource *r;
239
240 pci_read_config_word(dev, PCI_COMMAND, &cmd);
241 old_cmd = cmd;
242 for(idx=0; idx<6; idx++) {
243
244 if (!(mask & (1<<idx)))
245 continue;
246
247 r = &dev->resource[idx];
248 if (!r->start && r->end) {
249 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
250 return -EINVAL;
251 }
252 if (r->flags & IORESOURCE_IO)
253 cmd |= PCI_COMMAND_IO;
254 if (r->flags & IORESOURCE_MEM)
255 cmd |= PCI_COMMAND_MEMORY;
256 }
257 if (dev->resource[PCI_ROM_RESOURCE].start)
258 cmd |= PCI_COMMAND_MEMORY;
259 if (cmd != old_cmd) {
260 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
261 pci_write_config_word(dev, PCI_COMMAND, cmd);
262 }
263 return 0;
264}
265
266
267
268
269
270unsigned int pcibios_max_latency = 255;
271
272void pcibios_set_master(struct pci_dev *dev)
273{
274 u8 lat;
275 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
276 if (lat < 16)
277 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
278 else if (lat > pcibios_max_latency)
279 lat = pcibios_max_latency;
280 else
281 return;
282 printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
283 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
284}
285