2 * arch/xtensa/lib/pci-auto.c
4 * PCI autoconfiguration library
6 * Copyright (C) 2001 - 2005 Tensilica Inc.
8 * Chris Zankel <zankel@tensilica.com, cez@zankel.net>
10 * Based on work from Matt Porter <mporter@mvista.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/pci.h>
22 #include <asm/pci-bridge.h>
29 * pci_ctrl->first_busno = <first bus number (0)>
30 * pci_ctrl->last_busno = <last bus number (0xff)>
31 * pci_ctrl->ops = <PCI config operations>
32 * pci_ctrl->map_irq = <function to return the interrupt number for a device>
34 * pci_ctrl->io_space.start = <IO space start address (PCI view)>
35 * pci_ctrl->io_space.end = <IO space end address (PCI view)>
36 * pci_ctrl->io_space.base = <IO space offset: address 0 from CPU space>
37 * pci_ctrl->mem_space.start = <MEM space start address (PCI view)>
38 * pci_ctrl->mem_space.end = <MEM space end address (PCI view)>
39 * pci_ctrl->mem_space.base = <MEM space offset: address 0 from CPU space>
41 * pcibios_init_resource(&pci_ctrl->io_resource, <IO space start>,
42 * <IO space end>, IORESOURCE_IO, "PCI host bridge");
43 * pcibios_init_resource(&pci_ctrl->mem_resources[0], <MEM space start>,
44 * <MEM space end>, IORESOURCE_MEM, "PCI host bridge");
46 * pci_ctrl->last_busno = pciauto_bus_scan(pci_ctrl,pci_ctrl->first_busno);
48 * int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
53 /* define DEBUG to print some debugging messages. */
58 # define DBG(x...) printk(x)
63 static int pciauto_upper_iospc
;
64 static int pciauto_upper_memspc
;
66 static struct pci_dev pciauto_dev
;
67 static struct pci_bus pciauto_bus
;
73 /* Initialize the bars of a PCI device. */
76 pciauto_setup_bars(struct pci_dev
*dev
, int bar_limit
)
83 for (bar
= PCI_BASE_ADDRESS_0
, bar_nr
= 0;
87 /* Tickle the BAR and get the size */
88 pci_write_config_dword(dev
, bar
, 0xffffffff);
89 pci_read_config_dword(dev
, bar
, &bar_size
);
91 /* If BAR is not implemented go to the next BAR */
95 /* Check the BAR type and set our address mask */
96 if (bar_size
& PCI_BASE_ADDRESS_SPACE_IO
)
98 bar_size
&= PCI_BASE_ADDRESS_IO_MASK
;
99 upper_limit
= &pciauto_upper_iospc
;
100 DBG("PCI Autoconfig: BAR %d, I/O, ", bar_nr
);
104 if ((bar_size
& PCI_BASE_ADDRESS_MEM_TYPE_MASK
) ==
105 PCI_BASE_ADDRESS_MEM_TYPE_64
)
108 bar_size
&= PCI_BASE_ADDRESS_MEM_MASK
;
109 upper_limit
= &pciauto_upper_memspc
;
110 DBG("PCI Autoconfig: BAR %d, Mem, ", bar_nr
);
113 /* Allocate a base address (bar_size is negative!) */
114 *upper_limit
= (*upper_limit
+ bar_size
) & bar_size
;
116 /* Write it out and update our limit */
117 pci_write_config_dword(dev
, bar
, *upper_limit
);
120 * If we are a 64-bit decoder then increment to the
121 * upper 32 bits of the bar and force it to locate
122 * in the lower 4GB of memory.
126 pci_write_config_dword(dev
, (bar
+=4), 0x00000000);
128 DBG("size=0x%x, address=0x%x\n", ~bar_size
+ 1, *upper_limit
);
132 /* Initialize the interrupt number. */
135 pciauto_setup_irq(struct pci_controller
* pci_ctrl
,struct pci_dev
*dev
,int devfn
)
140 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
142 /* Fix illegal pin numbers. */
144 if (pin
== 0 || pin
> 4)
147 if (pci_ctrl
->map_irq
)
148 irq
= pci_ctrl
->map_irq(dev
, PCI_SLOT(devfn
), pin
);
153 DBG("PCI Autoconfig: Interrupt %d, pin %d\n", irq
, pin
);
155 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, irq
);
160 pciauto_prescan_setup_bridge(struct pci_dev
*dev
, int current_bus
,
161 int sub_bus
, int *iosave
, int *memsave
)
163 /* Configure bus number registers */
164 pci_write_config_byte(dev
, PCI_PRIMARY_BUS
, current_bus
);
165 pci_write_config_byte(dev
, PCI_SECONDARY_BUS
, sub_bus
+ 1);
166 pci_write_config_byte(dev
, PCI_SUBORDINATE_BUS
, 0xff);
168 /* Round memory allocator to 1MB boundary */
169 pciauto_upper_memspc
&= ~(0x100000 - 1);
170 *memsave
= pciauto_upper_memspc
;
172 /* Round I/O allocator to 4KB boundary */
173 pciauto_upper_iospc
&= ~(0x1000 - 1);
174 *iosave
= pciauto_upper_iospc
;
176 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
177 pci_write_config_word(dev
, PCI_MEMORY_LIMIT
,
178 ((pciauto_upper_memspc
- 1) & 0xfff00000) >> 16);
179 pci_write_config_byte(dev
, PCI_IO_LIMIT
,
180 ((pciauto_upper_iospc
- 1) & 0x0000f000) >> 8);
181 pci_write_config_word(dev
, PCI_IO_LIMIT_UPPER16
,
182 ((pciauto_upper_iospc
- 1) & 0xffff0000) >> 16);
186 pciauto_postscan_setup_bridge(struct pci_dev
*dev
, int current_bus
, int sub_bus
,
187 int *iosave
, int *memsave
)
191 /* Configure bus number registers */
192 pci_write_config_byte(dev
, PCI_SUBORDINATE_BUS
, sub_bus
);
195 * Round memory allocator to 1MB boundary.
196 * If no space used, allocate minimum.
198 pciauto_upper_memspc
&= ~(0x100000 - 1);
199 if (*memsave
== pciauto_upper_memspc
)
200 pciauto_upper_memspc
-= 0x00100000;
202 pci_write_config_word(dev
, PCI_MEMORY_BASE
, pciauto_upper_memspc
>> 16);
204 /* Allocate 1MB for pre-fretch */
205 pci_write_config_word(dev
, PCI_PREF_MEMORY_LIMIT
,
206 ((pciauto_upper_memspc
- 1) & 0xfff00000) >> 16);
208 pciauto_upper_memspc
-= 0x100000;
210 pci_write_config_word(dev
, PCI_PREF_MEMORY_BASE
,
211 pciauto_upper_memspc
>> 16);
213 /* Round I/O allocator to 4KB boundary */
214 pciauto_upper_iospc
&= ~(0x1000 - 1);
215 if (*iosave
== pciauto_upper_iospc
)
216 pciauto_upper_iospc
-= 0x1000;
218 pci_write_config_byte(dev
, PCI_IO_BASE
,
219 (pciauto_upper_iospc
& 0x0000f000) >> 8);
220 pci_write_config_word(dev
, PCI_IO_BASE_UPPER16
,
221 pciauto_upper_iospc
>> 16);
223 /* Enable memory and I/O accesses, enable bus master */
224 pci_read_config_dword(dev
, PCI_COMMAND
, &cmdstat
);
225 pci_write_config_dword(dev
, PCI_COMMAND
,
233 * Scan the current PCI bus.
237 int __init
pciauto_bus_scan(struct pci_controller
*pci_ctrl
, int current_bus
)
239 int sub_bus
, pci_devfn
, pci_class
, cmdstat
, found_multi
=0;
241 unsigned char header_type
;
242 struct pci_dev
*dev
= &pciauto_dev
;
244 pciauto_dev
.bus
= &pciauto_bus
;
245 pciauto_dev
.sysdata
= pci_ctrl
;
246 pciauto_bus
.ops
= pci_ctrl
->ops
;
249 * Fetch our I/O and memory space upper boundaries used
250 * to allocated base addresses on this pci_controller.
253 if (current_bus
== pci_ctrl
->first_busno
)
255 pciauto_upper_iospc
= pci_ctrl
->io_resource
.end
+ 1;
256 pciauto_upper_memspc
= pci_ctrl
->mem_resources
[0].end
+ 1;
259 sub_bus
= current_bus
;
261 for (pci_devfn
= 0; pci_devfn
< 0xff; pci_devfn
++)
263 /* Skip our host bridge */
264 if ((current_bus
== pci_ctrl
->first_busno
) && (pci_devfn
== 0))
267 if (PCI_FUNC(pci_devfn
) && !found_multi
)
270 pciauto_bus
.number
= current_bus
;
271 pciauto_dev
.devfn
= pci_devfn
;
273 /* If config space read fails from this device, move on */
274 if (pci_read_config_byte(dev
, PCI_HEADER_TYPE
, &header_type
))
277 if (!PCI_FUNC(pci_devfn
))
278 found_multi
= header_type
& 0x80;
279 pci_read_config_word(dev
, PCI_VENDOR_ID
, &vid
);
281 if (vid
== 0xffff || vid
== 0x0000) {
286 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &pci_class
);
288 if ((pci_class
>> 16) == PCI_CLASS_BRIDGE_PCI
) {
292 DBG("PCI Autoconfig: Found P2P bridge, device %d\n",
293 PCI_SLOT(pci_devfn
));
295 /* Allocate PCI I/O and/or memory space */
296 pciauto_setup_bars(dev
, PCI_BASE_ADDRESS_1
);
298 pciauto_prescan_setup_bridge(dev
, current_bus
, sub_bus
,
300 sub_bus
= pciauto_bus_scan(pci_ctrl
, sub_bus
+1);
301 pciauto_postscan_setup_bridge(dev
, current_bus
, sub_bus
,
303 pciauto_bus
.number
= current_bus
;
311 /* Skip legacy mode IDE controller */
313 if ((pci_class
>> 16) == PCI_CLASS_STORAGE_IDE
) {
315 unsigned char prg_iface
;
316 pci_read_config_byte(dev
, PCI_CLASS_PROG
, &prg_iface
);
318 if (!(prg_iface
& PCIAUTO_IDE_MODE_MASK
)) {
319 DBG("PCI Autoconfig: Skipping legacy mode "
327 * Found a peripheral, enable some standard
331 pci_read_config_dword(dev
, PCI_COMMAND
, &cmdstat
);
332 pci_write_config_dword(dev
, PCI_COMMAND
,
337 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0x80);
339 /* Allocate PCI I/O and/or memory space */
340 DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
341 current_bus
, PCI_SLOT(pci_devfn
), PCI_FUNC(pci_devfn
) );
343 pciauto_setup_bars(dev
, PCI_BASE_ADDRESS_5
);
344 pciauto_setup_irq(pci_ctrl
, dev
, pci_devfn
);