1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * arch/xtensa/lib/pci-auto.c
5 * PCI autoconfiguration library
7 * Copyright (C) 2001 - 2005 Tensilica Inc.
9 * Chris Zankel <zankel@tensilica.com, cez@zankel.net>
11 * Based on work from Matt Porter <mporter@mvista.com>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/pci.h>
18 #include <asm/pci-bridge.h>
25 * pci_ctrl->first_busno = <first bus number (0)>
26 * pci_ctrl->last_busno = <last bus number (0xff)>
27 * pci_ctrl->ops = <PCI config operations>
28 * pci_ctrl->map_irq = <function to return the interrupt number for a device>
30 * pci_ctrl->io_space.start = <IO space start address (PCI view)>
31 * pci_ctrl->io_space.end = <IO space end address (PCI view)>
32 * pci_ctrl->io_space.base = <IO space offset: address 0 from CPU space>
33 * pci_ctrl->mem_space.start = <MEM space start address (PCI view)>
34 * pci_ctrl->mem_space.end = <MEM space end address (PCI view)>
35 * pci_ctrl->mem_space.base = <MEM space offset: address 0 from CPU space>
37 * pcibios_init_resource(&pci_ctrl->io_resource, <IO space start>,
38 * <IO space end>, IORESOURCE_IO, "PCI host bridge");
39 * pcibios_init_resource(&pci_ctrl->mem_resources[0], <MEM space start>,
40 * <MEM space end>, IORESOURCE_MEM, "PCI host bridge");
42 * pci_ctrl->last_busno = pciauto_bus_scan(pci_ctrl,pci_ctrl->first_busno);
44 * int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
48 static int pciauto_upper_iospc
;
49 static int pciauto_upper_memspc
;
51 static struct pci_dev pciauto_dev
;
52 static struct pci_bus pciauto_bus
;
58 /* Initialize the bars of a PCI device. */
61 pciauto_setup_bars(struct pci_dev
*dev
, int bar_limit
)
68 for (bar
= PCI_BASE_ADDRESS_0
, bar_nr
= 0;
72 /* Tickle the BAR and get the size */
73 pci_write_config_dword(dev
, bar
, 0xffffffff);
74 pci_read_config_dword(dev
, bar
, &bar_size
);
76 /* If BAR is not implemented go to the next BAR */
80 /* Check the BAR type and set our address mask */
81 if (bar_size
& PCI_BASE_ADDRESS_SPACE_IO
)
83 bar_size
&= PCI_BASE_ADDRESS_IO_MASK
;
84 upper_limit
= &pciauto_upper_iospc
;
85 pr_debug("PCI Autoconfig: BAR %d, I/O, ", bar_nr
);
89 if ((bar_size
& PCI_BASE_ADDRESS_MEM_TYPE_MASK
) ==
90 PCI_BASE_ADDRESS_MEM_TYPE_64
)
93 bar_size
&= PCI_BASE_ADDRESS_MEM_MASK
;
94 upper_limit
= &pciauto_upper_memspc
;
95 pr_debug("PCI Autoconfig: BAR %d, Mem, ", bar_nr
);
98 /* Allocate a base address (bar_size is negative!) */
99 *upper_limit
= (*upper_limit
+ bar_size
) & bar_size
;
101 /* Write it out and update our limit */
102 pci_write_config_dword(dev
, bar
, *upper_limit
);
105 * If we are a 64-bit decoder then increment to the
106 * upper 32 bits of the bar and force it to locate
107 * in the lower 4GB of memory.
111 pci_write_config_dword(dev
, (bar
+=4), 0x00000000);
113 pr_debug("size=0x%x, address=0x%x\n",
114 ~bar_size
+ 1, *upper_limit
);
118 /* Initialize the interrupt number. */
121 pciauto_setup_irq(struct pci_controller
* pci_ctrl
,struct pci_dev
*dev
,int devfn
)
126 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
128 /* Fix illegal pin numbers. */
130 if (pin
== 0 || pin
> 4)
133 if (pci_ctrl
->map_irq
)
134 irq
= pci_ctrl
->map_irq(dev
, PCI_SLOT(devfn
), pin
);
139 pr_debug("PCI Autoconfig: Interrupt %d, pin %d\n", irq
, pin
);
141 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, irq
);
146 pciauto_prescan_setup_bridge(struct pci_dev
*dev
, int current_bus
,
147 int sub_bus
, int *iosave
, int *memsave
)
149 /* Configure bus number registers */
150 pci_write_config_byte(dev
, PCI_PRIMARY_BUS
, current_bus
);
151 pci_write_config_byte(dev
, PCI_SECONDARY_BUS
, sub_bus
+ 1);
152 pci_write_config_byte(dev
, PCI_SUBORDINATE_BUS
, 0xff);
154 /* Round memory allocator to 1MB boundary */
155 pciauto_upper_memspc
&= ~(0x100000 - 1);
156 *memsave
= pciauto_upper_memspc
;
158 /* Round I/O allocator to 4KB boundary */
159 pciauto_upper_iospc
&= ~(0x1000 - 1);
160 *iosave
= pciauto_upper_iospc
;
162 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
163 pci_write_config_word(dev
, PCI_MEMORY_LIMIT
,
164 ((pciauto_upper_memspc
- 1) & 0xfff00000) >> 16);
165 pci_write_config_byte(dev
, PCI_IO_LIMIT
,
166 ((pciauto_upper_iospc
- 1) & 0x0000f000) >> 8);
167 pci_write_config_word(dev
, PCI_IO_LIMIT_UPPER16
,
168 ((pciauto_upper_iospc
- 1) & 0xffff0000) >> 16);
172 pciauto_postscan_setup_bridge(struct pci_dev
*dev
, int current_bus
, int sub_bus
,
173 int *iosave
, int *memsave
)
177 /* Configure bus number registers */
178 pci_write_config_byte(dev
, PCI_SUBORDINATE_BUS
, sub_bus
);
181 * Round memory allocator to 1MB boundary.
182 * If no space used, allocate minimum.
184 pciauto_upper_memspc
&= ~(0x100000 - 1);
185 if (*memsave
== pciauto_upper_memspc
)
186 pciauto_upper_memspc
-= 0x00100000;
188 pci_write_config_word(dev
, PCI_MEMORY_BASE
, pciauto_upper_memspc
>> 16);
190 /* Allocate 1MB for pre-fretch */
191 pci_write_config_word(dev
, PCI_PREF_MEMORY_LIMIT
,
192 ((pciauto_upper_memspc
- 1) & 0xfff00000) >> 16);
194 pciauto_upper_memspc
-= 0x100000;
196 pci_write_config_word(dev
, PCI_PREF_MEMORY_BASE
,
197 pciauto_upper_memspc
>> 16);
199 /* Round I/O allocator to 4KB boundary */
200 pciauto_upper_iospc
&= ~(0x1000 - 1);
201 if (*iosave
== pciauto_upper_iospc
)
202 pciauto_upper_iospc
-= 0x1000;
204 pci_write_config_byte(dev
, PCI_IO_BASE
,
205 (pciauto_upper_iospc
& 0x0000f000) >> 8);
206 pci_write_config_word(dev
, PCI_IO_BASE_UPPER16
,
207 pciauto_upper_iospc
>> 16);
209 /* Enable memory and I/O accesses, enable bus master */
210 pci_read_config_dword(dev
, PCI_COMMAND
, &cmdstat
);
211 pci_write_config_dword(dev
, PCI_COMMAND
,
219 * Scan the current PCI bus.
223 int __init
pciauto_bus_scan(struct pci_controller
*pci_ctrl
, int current_bus
)
225 int sub_bus
, pci_devfn
, pci_class
, cmdstat
, found_multi
=0;
227 unsigned char header_type
;
228 struct pci_dev
*dev
= &pciauto_dev
;
230 pciauto_dev
.bus
= &pciauto_bus
;
231 pciauto_dev
.sysdata
= pci_ctrl
;
232 pciauto_bus
.ops
= pci_ctrl
->ops
;
235 * Fetch our I/O and memory space upper boundaries used
236 * to allocated base addresses on this pci_controller.
239 if (current_bus
== pci_ctrl
->first_busno
)
241 pciauto_upper_iospc
= pci_ctrl
->io_resource
.end
+ 1;
242 pciauto_upper_memspc
= pci_ctrl
->mem_resources
[0].end
+ 1;
245 sub_bus
= current_bus
;
247 for (pci_devfn
= 0; pci_devfn
< 0xff; pci_devfn
++)
249 /* Skip our host bridge */
250 if ((current_bus
== pci_ctrl
->first_busno
) && (pci_devfn
== 0))
253 if (PCI_FUNC(pci_devfn
) && !found_multi
)
256 pciauto_bus
.number
= current_bus
;
257 pciauto_dev
.devfn
= pci_devfn
;
259 /* If config space read fails from this device, move on */
260 if (pci_read_config_byte(dev
, PCI_HEADER_TYPE
, &header_type
))
263 if (!PCI_FUNC(pci_devfn
))
264 found_multi
= header_type
& 0x80;
265 pci_read_config_word(dev
, PCI_VENDOR_ID
, &vid
);
267 if (vid
== 0xffff || vid
== 0x0000) {
272 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &pci_class
);
274 if ((pci_class
>> 16) == PCI_CLASS_BRIDGE_PCI
) {
278 pr_debug("PCI Autoconfig: Found P2P bridge, device %d\n",
279 PCI_SLOT(pci_devfn
));
281 /* Allocate PCI I/O and/or memory space */
282 pciauto_setup_bars(dev
, PCI_BASE_ADDRESS_1
);
284 pciauto_prescan_setup_bridge(dev
, current_bus
, sub_bus
,
286 sub_bus
= pciauto_bus_scan(pci_ctrl
, sub_bus
+1);
287 pciauto_postscan_setup_bridge(dev
, current_bus
, sub_bus
,
289 pciauto_bus
.number
= current_bus
;
296 * Found a peripheral, enable some standard
300 pci_read_config_dword(dev
, PCI_COMMAND
, &cmdstat
);
301 pci_write_config_dword(dev
, PCI_COMMAND
,
306 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 0x80);
308 /* Allocate PCI I/O and/or memory space */
309 pr_debug("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
310 current_bus
, PCI_SLOT(pci_devfn
), PCI_FUNC(pci_devfn
));
312 pciauto_setup_bars(dev
, PCI_BASE_ADDRESS_5
);
313 pciauto_setup_irq(pci_ctrl
, dev
, pci_devfn
);