2 * arch/xtensa/kernel/pci.c
4 * PCI bios-type initialisation for PCI machines
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.
11 * Copyright (C) 2001-2005 Tensilica Inc.
13 * Based largely on work from Cort (ppc/kernel/pci.c)
14 * IO functions copied from sparc.
16 * Chris Zankel <chris@zankel.net>
20 #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>
29 #include <asm/pci-bridge.h>
30 #include <asm/platform.h>
35 #define DBG(x...) printk(x)
44 * pcibios_alloc_controller
45 * pcibios_enable_device
47 * pcibios_align_resource
53 struct pci_controller
* pci_ctrl_head
;
54 struct pci_controller
** pci_ctrl_tail
= &pci_ctrl_head
;
56 static int pci_bus_count
;
59 * We need to avoid collisions with `mirrored' VGA ports
60 * and other strange ISA hardware, so we always want the
61 * addresses to be allocated in the 0x000-0x0ff region
64 * Why? Because some silly external IO cards only decode
65 * the low 10 bits of the IO address. The 0x00-0xff region
66 * is reserved for motherboard devices that decode all 16
67 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
68 * but we want to try to avoid allocating at 0x2900-0x2bff
69 * which might have be mirrored at 0x0100-0x03ff..
72 pcibios_align_resource(void *data
, const struct resource
*res
,
73 resource_size_t size
, resource_size_t align
)
75 struct pci_dev
*dev
= data
;
76 resource_size_t start
= res
->start
;
78 if (res
->flags
& IORESOURCE_IO
) {
80 printk(KERN_ERR
"PCI: I/O Region %s/%d too large"
81 " (%ld bytes)\n", pci_name(dev
),
82 dev
->resource
- res
, size
);
86 start
= (start
+ 0x3ff) & ~0x3ff;
93 pcibios_enable_resources(struct pci_dev
*dev
, int mask
)
99 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
101 for(idx
=0; idx
<6; idx
++) {
102 r
= &dev
->resource
[idx
];
103 if (!r
->start
&& r
->end
) {
104 printk (KERN_ERR
"PCI: Device %s not available because "
105 "of resource collisions\n", pci_name(dev
));
108 if (r
->flags
& IORESOURCE_IO
)
109 cmd
|= PCI_COMMAND_IO
;
110 if (r
->flags
& IORESOURCE_MEM
)
111 cmd
|= PCI_COMMAND_MEMORY
;
113 if (dev
->resource
[PCI_ROM_RESOURCE
].start
)
114 cmd
|= PCI_COMMAND_MEMORY
;
115 if (cmd
!= old_cmd
) {
116 printk("PCI: Enabling device %s (%04x -> %04x)\n",
117 pci_name(dev
), old_cmd
, cmd
);
118 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
123 struct pci_controller
* __init
pcibios_alloc_controller(void)
125 struct pci_controller
*pci_ctrl
;
127 pci_ctrl
= (struct pci_controller
*)alloc_bootmem(sizeof(*pci_ctrl
));
128 memset(pci_ctrl
, 0, sizeof(struct pci_controller
));
130 *pci_ctrl_tail
= pci_ctrl
;
131 pci_ctrl_tail
= &pci_ctrl
->next
;
136 static void __init
pci_controller_apertures(struct pci_controller
*pci_ctrl
,
137 struct list_head
*resources
)
139 struct resource
*res
;
140 unsigned long io_offset
;
143 io_offset
= (unsigned long)pci_ctrl
->io_space
.base
;
144 res
= &pci_ctrl
->io_resource
;
147 printk (KERN_ERR
"I/O resource not set for host"
148 " bridge %d\n", pci_ctrl
->index
);
150 res
->end
= IO_SPACE_LIMIT
;
151 res
->flags
= IORESOURCE_IO
;
153 res
->start
+= io_offset
;
154 res
->end
+= io_offset
;
155 pci_add_resource_offset(resources
, res
, io_offset
);
157 for (i
= 0; i
< 3; i
++) {
158 res
= &pci_ctrl
->mem_resources
[i
];
162 printk(KERN_ERR
"Memory resource not set for "
163 "host bridge %d\n", pci_ctrl
->index
);
166 res
->flags
= IORESOURCE_MEM
;
168 pci_add_resource(resources
, res
);
172 static int __init
pcibios_init(void)
174 struct pci_controller
*pci_ctrl
;
175 struct list_head resources
;
177 int next_busno
= 0, i
;
179 printk("PCI: Probing PCI hardware\n");
181 /* Scan all of the recorded PCI controllers. */
182 for (pci_ctrl
= pci_ctrl_head
; pci_ctrl
; pci_ctrl
= pci_ctrl
->next
) {
183 pci_ctrl
->last_busno
= 0xff;
184 INIT_LIST_HEAD(&resources
);
185 pci_controller_apertures(pci_ctrl
, &resources
);
186 bus
= pci_scan_root_bus(NULL
, pci_ctrl
->first_busno
,
187 pci_ctrl
->ops
, pci_ctrl
, &resources
);
189 pci_ctrl
->last_busno
= bus
->busn_res
.end
;
190 if (next_busno
<= pci_ctrl
->last_busno
)
191 next_busno
= pci_ctrl
->last_busno
+1;
193 pci_bus_count
= next_busno
;
195 return platform_pcibios_fixup();
198 subsys_initcall(pcibios_init
);
200 void __init
pcibios_fixup_bus(struct pci_bus
*bus
)
203 /* This is a subordinate bridge */
204 pci_read_bridge_bases(bus
);
208 void pcibios_set_master(struct pci_dev
*dev
)
210 /* No special bus mastering setup handling */
213 /* the next one is stolen from the alpha port... */
216 pcibios_update_irq(struct pci_dev
*dev
, int irq
)
218 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, irq
);
221 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
227 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
229 for (idx
=0; idx
<6; idx
++) {
230 r
= &dev
->resource
[idx
];
231 if (!r
->start
&& r
->end
) {
232 printk(KERN_ERR
"PCI: Device %s not available because "
233 "of resource collisions\n", pci_name(dev
));
236 if (r
->flags
& IORESOURCE_IO
)
237 cmd
|= PCI_COMMAND_IO
;
238 if (r
->flags
& IORESOURCE_MEM
)
239 cmd
|= PCI_COMMAND_MEMORY
;
241 if (cmd
!= old_cmd
) {
242 printk("PCI: Enabling device %s (%04x -> %04x)\n",
243 pci_name(dev
), old_cmd
, cmd
);
244 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
250 #ifdef CONFIG_PROC_FS
253 * Return the index of the PCI controller for device pdev.
257 pci_controller_num(struct pci_dev
*dev
)
259 struct pci_controller
*pci_ctrl
= (struct pci_controller
*) dev
->sysdata
;
260 return pci_ctrl
->index
;
263 #endif /* CONFIG_PROC_FS */
266 * Platform support for /proc/bus/pci/X/Y mmap()s,
267 * modelled on the sparc64 implementation by Dave Miller.
272 * Adjust vm_pgoff of VMA such that it is the physical page offset
273 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
275 * Basically, the user finds the base address for his device which he wishes
276 * to mmap. They read the 32-bit value from the config space base register,
277 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
278 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
280 * Returns negative error code on failure, zero on success.
282 static __inline__
int
283 __pci_mmap_make_offset(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
284 enum pci_mmap_state mmap_state
)
286 struct pci_controller
*pci_ctrl
= (struct pci_controller
*) dev
->sysdata
;
287 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
288 unsigned long io_offset
= 0;
292 return -EINVAL
; /* should never happen */
294 /* If memory, add on the PCI bridge address offset */
295 if (mmap_state
== pci_mmap_mem
) {
296 res_bit
= IORESOURCE_MEM
;
298 io_offset
= (unsigned long)pci_ctrl
->io_space
.base
;
300 res_bit
= IORESOURCE_IO
;
304 * Check that the offset requested corresponds to one of the
305 * resources of the device.
307 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++) {
308 struct resource
*rp
= &dev
->resource
[i
];
309 int flags
= rp
->flags
;
311 /* treat ROM as memory (should be already) */
312 if (i
== PCI_ROM_RESOURCE
)
313 flags
|= IORESOURCE_MEM
;
315 /* Active and same type? */
316 if ((flags
& res_bit
) == 0)
319 /* In the range of this resource? */
320 if (offset
< (rp
->start
& PAGE_MASK
) || offset
> rp
->end
)
323 /* found it! construct the final physical address */
324 if (mmap_state
== pci_mmap_io
)
325 offset
+= pci_ctrl
->io_space
.start
- io_offset
;
326 vma
->vm_pgoff
= offset
>> PAGE_SHIFT
;
334 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
337 static __inline__
void
338 __pci_mmap_set_pgprot(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
339 enum pci_mmap_state mmap_state
, int write_combine
)
341 int prot
= pgprot_val(vma
->vm_page_prot
);
343 /* Set to write-through */
344 prot
&= ~_PAGE_NO_CACHE
;
347 prot
|= _PAGE_WRITETHRU
;
349 vma
->vm_page_prot
= __pgprot(prot
);
353 * Perform the actual remap of the pages for a PCI device mapping, as
354 * appropriate for this architecture. The region in the process to map
355 * is described by vm_start and vm_end members of VMA, the base physical
356 * address is found in vm_pgoff.
357 * The pci device structure is provided so that architectures may make mapping
358 * decisions on a per-device or per-bus basis.
360 * Returns a negative error code on failure, zero on success.
362 int pci_mmap_page_range(struct pci_dev
*dev
, struct vm_area_struct
*vma
,
363 enum pci_mmap_state mmap_state
,
368 ret
= __pci_mmap_make_offset(dev
, vma
, mmap_state
);
372 __pci_mmap_set_pgprot(dev
, vma
, mmap_state
, write_combine
);
374 ret
= io_remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
375 vma
->vm_end
- vma
->vm_start
,vma
->vm_page_prot
);