blob: 644b2d4b299d52cf87554cc8b965ad0462b002a8 [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * arch/xtensa/kernel/pci.c
Chris Zankel5a0015d2005-06-23 22:01:16 -07003 *
4 * PCI bios-type initialisation for PCI machines
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * Copyright (C) 2001-2005 Tensilica Inc.
12 *
13 * Based largely on work from Cort (ppc/kernel/pci.c)
14 * IO functions copied from sparc.
15 *
16 * Chris Zankel <chris@zankel.net>
17 *
18 */
19
Chris Zankel5a0015d2005-06-23 22:01:16 -070020#include <linux/kernel.h>
21#include <linux/pci.h>
22#include <linux/delay.h>
23#include <linux/string.h>
24#include <linux/init.h>
25#include <linux/sched.h>
26#include <linux/errno.h>
27#include <linux/bootmem.h>
28
29#include <asm/pci-bridge.h>
30#include <asm/platform.h>
31
32#undef DEBUG
33
34#ifdef DEBUG
35#define DBG(x...) printk(x)
36#else
37#define DBG(x...)
38#endif
39
40/* PCI Controller */
41
42
43/*
44 * pcibios_alloc_controller
45 * pcibios_enable_device
46 * pcibios_fixups
47 * pcibios_align_resource
48 * pcibios_fixup_bus
49 * pcibios_setup
50 * pci_bus_add_device
51 * pci_mmap_page_range
52 */
53
54struct pci_controller* pci_ctrl_head;
55struct pci_controller** pci_ctrl_tail = &pci_ctrl_head;
56
57static int pci_bus_count;
58
Chris Zankel5a0015d2005-06-23 22:01:16 -070059/*
60 * We need to avoid collisions with `mirrored' VGA ports
61 * and other strange ISA hardware, so we always want the
62 * addresses to be allocated in the 0x000-0x0ff region
63 * modulo 0x400.
64 *
65 * Why? Because some silly external IO cards only decode
66 * the low 10 bits of the IO address. The 0x00-0xff region
67 * is reserved for motherboard devices that decode all 16
68 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
69 * but we want to try to avoid allocating at 0x2900-0x2bff
70 * which might have be mirrored at 0x0100-0x03ff..
71 */
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010072resource_size_t
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +010073pcibios_align_resource(void *data, const struct resource *res,
74 resource_size_t size, resource_size_t align)
Chris Zankel5a0015d2005-06-23 22:01:16 -070075{
76 struct pci_dev *dev = data;
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010077 resource_size_t start = res->start;
Chris Zankel5a0015d2005-06-23 22:01:16 -070078
79 if (res->flags & IORESOURCE_IO) {
Chris Zankel5a0015d2005-06-23 22:01:16 -070080 if (size > 0x100) {
81 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
Chris Zankel9ec55a92005-06-30 02:59:00 -070082 " (%ld bytes)\n", pci_name(dev),
Chris Zankel5a0015d2005-06-23 22:01:16 -070083 dev->resource - res, size);
84 }
85
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010086 if (start & 0x300)
Chris Zankel5a0015d2005-06-23 22:01:16 -070087 start = (start + 0x3ff) & ~0x3ff;
Chris Zankel5a0015d2005-06-23 22:01:16 -070088 }
Dominik Brodowskib26b2d42010-01-01 17:40:49 +010089
90 return start;
Chris Zankel5a0015d2005-06-23 22:01:16 -070091}
92
93int
94pcibios_enable_resources(struct pci_dev *dev, int mask)
95{
96 u16 cmd, old_cmd;
97 int idx;
98 struct resource *r;
99
100 pci_read_config_word(dev, PCI_COMMAND, &cmd);
101 old_cmd = cmd;
102 for(idx=0; idx<6; idx++) {
103 r = &dev->resource[idx];
104 if (!r->start && r->end) {
105 printk (KERN_ERR "PCI: Device %s not available because "
Chris Zankel9ec55a92005-06-30 02:59:00 -0700106 "of resource collisions\n", pci_name(dev));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700107 return -EINVAL;
108 }
109 if (r->flags & IORESOURCE_IO)
110 cmd |= PCI_COMMAND_IO;
111 if (r->flags & IORESOURCE_MEM)
112 cmd |= PCI_COMMAND_MEMORY;
113 }
114 if (dev->resource[PCI_ROM_RESOURCE].start)
115 cmd |= PCI_COMMAND_MEMORY;
116 if (cmd != old_cmd) {
117 printk("PCI: Enabling device %s (%04x -> %04x)\n",
Chris Zankel9ec55a92005-06-30 02:59:00 -0700118 pci_name(dev), old_cmd, cmd);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700119 pci_write_config_word(dev, PCI_COMMAND, cmd);
120 }
121 return 0;
122}
123
124struct pci_controller * __init pcibios_alloc_controller(void)
125{
126 struct pci_controller *pci_ctrl;
127
128 pci_ctrl = (struct pci_controller *)alloc_bootmem(sizeof(*pci_ctrl));
129 memset(pci_ctrl, 0, sizeof(struct pci_controller));
130
131 *pci_ctrl_tail = pci_ctrl;
132 pci_ctrl_tail = &pci_ctrl->next;
133
134 return pci_ctrl;
135}
136
137static int __init pcibios_init(void)
138{
139 struct pci_controller *pci_ctrl;
140 struct pci_bus *bus;
141 int next_busno = 0, i;
142
143 printk("PCI: Probing PCI hardware\n");
144
145 /* Scan all of the recorded PCI controllers. */
146 for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
147 pci_ctrl->last_busno = 0xff;
148 bus = pci_scan_bus(pci_ctrl->first_busno, pci_ctrl->ops,
149 pci_ctrl);
150 if (pci_ctrl->io_resource.flags) {
151 unsigned long offs;
152
153 offs = (unsigned long)pci_ctrl->io_space.base;
154 pci_ctrl->io_resource.start += offs;
155 pci_ctrl->io_resource.end += offs;
156 bus->resource[0] = &pci_ctrl->io_resource;
157 }
158 for (i = 0; i < 3; ++i)
159 if (pci_ctrl->mem_resources[i].flags)
160 bus->resource[i+1] =&pci_ctrl->mem_resources[i];
161 pci_ctrl->bus = bus;
162 pci_ctrl->last_busno = bus->subordinate;
163 if (next_busno <= pci_ctrl->last_busno)
164 next_busno = pci_ctrl->last_busno+1;
165 }
166 pci_bus_count = next_busno;
167
168 return platform_pcibios_fixup();
169}
170
171subsys_initcall(pcibios_init);
172
173void __init pcibios_fixup_bus(struct pci_bus *bus)
174{
175 struct pci_controller *pci_ctrl = bus->sysdata;
176 struct resource *res;
177 unsigned long io_offset;
178 int i;
179
180 io_offset = (unsigned long)pci_ctrl->io_space.base;
181 if (bus->parent == NULL) {
182 /* this is a host bridge - fill in its resources */
183 pci_ctrl->bus = bus;
184
185 bus->resource[0] = res = &pci_ctrl->io_resource;
186 if (!res->flags) {
187 if (io_offset)
188 printk (KERN_ERR "I/O resource not set for host"
189 " bridge %d\n", pci_ctrl->index);
190 res->start = 0;
191 res->end = IO_SPACE_LIMIT;
192 res->flags = IORESOURCE_IO;
193 }
194 res->start += io_offset;
195 res->end += io_offset;
196
197 for (i = 0; i < 3; i++) {
198 res = &pci_ctrl->mem_resources[i];
199 if (!res->flags) {
200 if (i > 0)
201 continue;
202 printk(KERN_ERR "Memory resource not set for "
203 "host bridge %d\n", pci_ctrl->index);
204 res->start = 0;
205 res->end = ~0U;
206 res->flags = IORESOURCE_MEM;
207 }
208 bus->resource[i+1] = res;
209 }
210 } else {
211 /* This is a subordinate bridge */
212 pci_read_bridge_bases(bus);
213
214 for (i = 0; i < 4; i++) {
215 if ((res = bus->resource[i]) == NULL || !res->flags)
216 continue;
217 if (io_offset && (res->flags & IORESOURCE_IO)) {
218 res->start += io_offset;
219 res->end += io_offset;
220 }
221 }
222 }
223}
224
225char __init *pcibios_setup(char *str)
226{
227 return str;
228}
229
Myron Stowe9cdce182011-10-28 15:48:31 -0600230void pcibios_set_master(struct pci_dev *dev)
231{
232 /* No special bus mastering setup handling */
233}
234
Chris Zankel5a0015d2005-06-23 22:01:16 -0700235/* the next one is stolen from the alpha port... */
236
237void __init
238pcibios_update_irq(struct pci_dev *dev, int irq)
239{
240 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
241}
242
243int pcibios_enable_device(struct pci_dev *dev, int mask)
244{
245 u16 cmd, old_cmd;
246 int idx;
247 struct resource *r;
248
249 pci_read_config_word(dev, PCI_COMMAND, &cmd);
250 old_cmd = cmd;
251 for (idx=0; idx<6; idx++) {
252 r = &dev->resource[idx];
253 if (!r->start && r->end) {
254 printk(KERN_ERR "PCI: Device %s not available because "
Chris Zankel9ec55a92005-06-30 02:59:00 -0700255 "of resource collisions\n", pci_name(dev));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700256 return -EINVAL;
257 }
258 if (r->flags & IORESOURCE_IO)
259 cmd |= PCI_COMMAND_IO;
260 if (r->flags & IORESOURCE_MEM)
261 cmd |= PCI_COMMAND_MEMORY;
262 }
263 if (cmd != old_cmd) {
264 printk("PCI: Enabling device %s (%04x -> %04x)\n",
Chris Zankel9ec55a92005-06-30 02:59:00 -0700265 pci_name(dev), old_cmd, cmd);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700266 pci_write_config_word(dev, PCI_COMMAND, cmd);
267 }
268
269 return 0;
270}
271
272#ifdef CONFIG_PROC_FS
273
274/*
275 * Return the index of the PCI controller for device pdev.
276 */
277
278int
279pci_controller_num(struct pci_dev *dev)
280{
281 struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
282 return pci_ctrl->index;
283}
284
285#endif /* CONFIG_PROC_FS */
286
Chris Zankel5a0015d2005-06-23 22:01:16 -0700287/*
288 * Platform support for /proc/bus/pci/X/Y mmap()s,
289 * modelled on the sparc64 implementation by Dave Miller.
290 * -- paulus.
291 */
292
293/*
294 * Adjust vm_pgoff of VMA such that it is the physical page offset
295 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
296 *
297 * Basically, the user finds the base address for his device which he wishes
298 * to mmap. They read the 32-bit value from the config space base register,
299 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
300 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
301 *
302 * Returns negative error code on failure, zero on success.
303 */
304static __inline__ int
305__pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
306 enum pci_mmap_state mmap_state)
307{
308 struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
309 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
310 unsigned long io_offset = 0;
311 int i, res_bit;
312
313 if (pci_ctrl == 0)
314 return -EINVAL; /* should never happen */
315
316 /* If memory, add on the PCI bridge address offset */
317 if (mmap_state == pci_mmap_mem) {
318 res_bit = IORESOURCE_MEM;
319 } else {
320 io_offset = (unsigned long)pci_ctrl->io_space.base;
321 offset += io_offset;
322 res_bit = IORESOURCE_IO;
323 }
324
325 /*
326 * Check that the offset requested corresponds to one of the
327 * resources of the device.
328 */
329 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
330 struct resource *rp = &dev->resource[i];
331 int flags = rp->flags;
332
333 /* treat ROM as memory (should be already) */
334 if (i == PCI_ROM_RESOURCE)
335 flags |= IORESOURCE_MEM;
336
337 /* Active and same type? */
338 if ((flags & res_bit) == 0)
339 continue;
340
341 /* In the range of this resource? */
342 if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
343 continue;
344
345 /* found it! construct the final physical address */
346 if (mmap_state == pci_mmap_io)
347 offset += pci_ctrl->io_space.start - io_offset;
348 vma->vm_pgoff = offset >> PAGE_SHIFT;
349 return 0;
350 }
351
352 return -EINVAL;
353}
354
355/*
Chris Zankel5a0015d2005-06-23 22:01:16 -0700356 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
357 * device mapping.
358 */
359static __inline__ void
360__pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
361 enum pci_mmap_state mmap_state, int write_combine)
362{
363 int prot = pgprot_val(vma->vm_page_prot);
364
365 /* Set to write-through */
366 prot &= ~_PAGE_NO_CACHE;
367#if 0
368 if (!write_combine)
369 prot |= _PAGE_WRITETHRU;
370#endif
371 vma->vm_page_prot = __pgprot(prot);
372}
373
374/*
375 * Perform the actual remap of the pages for a PCI device mapping, as
376 * appropriate for this architecture. The region in the process to map
377 * is described by vm_start and vm_end members of VMA, the base physical
378 * address is found in vm_pgoff.
379 * The pci device structure is provided so that architectures may make mapping
380 * decisions on a per-device or per-bus basis.
381 *
382 * Returns a negative error code on failure, zero on success.
383 */
384int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
385 enum pci_mmap_state mmap_state,
386 int write_combine)
387{
388 int ret;
389
390 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
391 if (ret < 0)
392 return ret;
393
Chris Zankel5a0015d2005-06-23 22:01:16 -0700394 __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
395
Chris Zankel288a60c2005-09-22 21:44:23 -0700396 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
397 vma->vm_end - vma->vm_start,vma->vm_page_prot);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700398
399 return ret;
400}