xref: /openbmc/linux/arch/xtensa/lib/pci-auto.c (revision f30c2269544bffc7bf1b0d7c0abe5be1be83b8cb)
1249ac17eSChris Zankel /*
2*f30c2269SUwe Zeisberger  * arch/xtensa/lib/pci-auto.c
3249ac17eSChris Zankel  *
4249ac17eSChris Zankel  * PCI autoconfiguration library
5249ac17eSChris Zankel  *
6249ac17eSChris Zankel  * Copyright (C) 2001 - 2005 Tensilica Inc.
7249ac17eSChris Zankel  *
8249ac17eSChris Zankel  * Chris Zankel <zankel@tensilica.com, cez@zankel.net>
9249ac17eSChris Zankel  *
10249ac17eSChris Zankel  * Based on work from Matt Porter <mporter@mvista.com>
11249ac17eSChris Zankel  *
12249ac17eSChris Zankel  * This program is free software; you can redistribute  it and/or modify it
13249ac17eSChris Zankel  * under  the terms of  the GNU General  Public License as published by the
14249ac17eSChris Zankel  * Free Software Foundation;  either version 2 of the  License, or (at your
15249ac17eSChris Zankel  * option) any later version.
16249ac17eSChris Zankel  */
17249ac17eSChris Zankel 
18249ac17eSChris Zankel #include <linux/kernel.h>
19249ac17eSChris Zankel #include <linux/init.h>
20249ac17eSChris Zankel #include <linux/pci.h>
21249ac17eSChris Zankel 
22249ac17eSChris Zankel #include <asm/pci-bridge.h>
23249ac17eSChris Zankel 
24249ac17eSChris Zankel 
25249ac17eSChris Zankel /*
26249ac17eSChris Zankel  *
27249ac17eSChris Zankel  * Setting up a PCI
28249ac17eSChris Zankel  *
29249ac17eSChris Zankel  * pci_ctrl->first_busno = <first bus number (0)>
30249ac17eSChris Zankel  * pci_ctrl->last_busno = <last bus number (0xff)>
31249ac17eSChris Zankel  * pci_ctrl->ops = <PCI config operations>
32249ac17eSChris Zankel  * pci_ctrl->map_irq = <function to return the interrupt number for a device>
33249ac17eSChris Zankel  *
34249ac17eSChris Zankel  * pci_ctrl->io_space.start = <IO space start address (PCI view)>
35249ac17eSChris Zankel  * pci_ctrl->io_space.end = <IO space end address (PCI view)>
36249ac17eSChris Zankel  * pci_ctrl->io_space.base = <IO space offset: address 0 from CPU space>
37249ac17eSChris Zankel  * pci_ctrl->mem_space.start = <MEM space start address (PCI view)>
38249ac17eSChris Zankel  * pci_ctrl->mem_space.end = <MEM space end address (PCI view)>
39249ac17eSChris Zankel  * pci_ctrl->mem_space.base = <MEM space offset: address 0 from CPU space>
40249ac17eSChris Zankel  *
41249ac17eSChris Zankel  * pcibios_init_resource(&pci_ctrl->io_resource, <IO space start>,
42249ac17eSChris Zankel  * 			 <IO space end>, IORESOURCE_IO, "PCI host bridge");
43249ac17eSChris Zankel  * pcibios_init_resource(&pci_ctrl->mem_resources[0], <MEM space start>,
44249ac17eSChris Zankel  * 			 <MEM space end>, IORESOURCE_MEM, "PCI host bridge");
45249ac17eSChris Zankel  *
46249ac17eSChris Zankel  * pci_ctrl->last_busno = pciauto_bus_scan(pci_ctrl,pci_ctrl->first_busno);
47249ac17eSChris Zankel  *
48249ac17eSChris Zankel  * int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
49249ac17eSChris Zankel  *
50249ac17eSChris Zankel  */
51249ac17eSChris Zankel 
52249ac17eSChris Zankel 
53249ac17eSChris Zankel /* define DEBUG to print some debugging messages. */
54249ac17eSChris Zankel 
55249ac17eSChris Zankel #undef DEBUG
56249ac17eSChris Zankel 
57249ac17eSChris Zankel #ifdef DEBUG
58249ac17eSChris Zankel # define DBG(x...) printk(x)
59249ac17eSChris Zankel #else
60249ac17eSChris Zankel # define DBG(x...)
61249ac17eSChris Zankel #endif
62249ac17eSChris Zankel 
63249ac17eSChris Zankel static int pciauto_upper_iospc;
64249ac17eSChris Zankel static int pciauto_upper_memspc;
65249ac17eSChris Zankel 
66249ac17eSChris Zankel static struct pci_dev pciauto_dev;
67249ac17eSChris Zankel static struct pci_bus pciauto_bus;
68249ac17eSChris Zankel 
69249ac17eSChris Zankel /*
70249ac17eSChris Zankel  * Helper functions
71249ac17eSChris Zankel  */
72249ac17eSChris Zankel 
73249ac17eSChris Zankel /* Initialize the bars of a PCI device.  */
74249ac17eSChris Zankel 
75249ac17eSChris Zankel static void __init
76249ac17eSChris Zankel pciauto_setup_bars(struct pci_dev *dev, int bar_limit)
77249ac17eSChris Zankel {
78249ac17eSChris Zankel 	int bar_size;
79249ac17eSChris Zankel 	int bar, bar_nr;
80249ac17eSChris Zankel 	int *upper_limit;
81249ac17eSChris Zankel 	int found_mem64 = 0;
82249ac17eSChris Zankel 
83249ac17eSChris Zankel 	for (bar = PCI_BASE_ADDRESS_0, bar_nr = 0;
84249ac17eSChris Zankel 	     bar <= bar_limit;
85249ac17eSChris Zankel 	     bar+=4, bar_nr++)
86249ac17eSChris Zankel 	{
87249ac17eSChris Zankel 		/* Tickle the BAR and get the size */
88249ac17eSChris Zankel 		pci_write_config_dword(dev, bar, 0xffffffff);
89249ac17eSChris Zankel 		pci_read_config_dword(dev, bar, &bar_size);
90249ac17eSChris Zankel 
91249ac17eSChris Zankel 		/* If BAR is not implemented go to the next BAR */
92249ac17eSChris Zankel 		if (!bar_size)
93249ac17eSChris Zankel 			continue;
94249ac17eSChris Zankel 
95249ac17eSChris Zankel 		/* Check the BAR type and set our address mask */
96249ac17eSChris Zankel 		if (bar_size & PCI_BASE_ADDRESS_SPACE_IO)
97249ac17eSChris Zankel 		{
98249ac17eSChris Zankel 			bar_size &= PCI_BASE_ADDRESS_IO_MASK;
99249ac17eSChris Zankel 			upper_limit = &pciauto_upper_iospc;
100249ac17eSChris Zankel 			DBG("PCI Autoconfig: BAR %d, I/O, ", bar_nr);
101249ac17eSChris Zankel 		}
102249ac17eSChris Zankel 		else
103249ac17eSChris Zankel 		{
104249ac17eSChris Zankel 			if ((bar_size & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
105249ac17eSChris Zankel 			    PCI_BASE_ADDRESS_MEM_TYPE_64)
106249ac17eSChris Zankel 				found_mem64 = 1;
107249ac17eSChris Zankel 
108249ac17eSChris Zankel 			bar_size &= PCI_BASE_ADDRESS_MEM_MASK;
109249ac17eSChris Zankel 			upper_limit = &pciauto_upper_memspc;
110249ac17eSChris Zankel 			DBG("PCI Autoconfig: BAR %d, Mem, ", bar_nr);
111249ac17eSChris Zankel 		}
112249ac17eSChris Zankel 
113249ac17eSChris Zankel 		/* Allocate a base address (bar_size is negative!) */
114249ac17eSChris Zankel 		*upper_limit = (*upper_limit + bar_size) & bar_size;
115249ac17eSChris Zankel 
116249ac17eSChris Zankel 		/* Write it out and update our limit */
117249ac17eSChris Zankel 		pci_write_config_dword(dev, bar, *upper_limit);
118249ac17eSChris Zankel 
119249ac17eSChris Zankel 		/*
120249ac17eSChris Zankel 		 * If we are a 64-bit decoder then increment to the
121249ac17eSChris Zankel 		 * upper 32 bits of the bar and force it to locate
122249ac17eSChris Zankel 		 * in the lower 4GB of memory.
123249ac17eSChris Zankel 		 */
124249ac17eSChris Zankel 
125249ac17eSChris Zankel 		if (found_mem64)
126249ac17eSChris Zankel 			pci_write_config_dword(dev, (bar+=4), 0x00000000);
127249ac17eSChris Zankel 
128249ac17eSChris Zankel 		DBG("size=0x%x, address=0x%x\n", ~bar_size + 1, *upper_limit);
129249ac17eSChris Zankel 	}
130249ac17eSChris Zankel }
131249ac17eSChris Zankel 
132249ac17eSChris Zankel /* Initialize the interrupt number. */
133249ac17eSChris Zankel 
134249ac17eSChris Zankel static void __init
135249ac17eSChris Zankel pciauto_setup_irq(struct pci_controller* pci_ctrl,struct pci_dev *dev,int devfn)
136249ac17eSChris Zankel {
137249ac17eSChris Zankel 	u8 pin;
138249ac17eSChris Zankel 	int irq = 0;
139249ac17eSChris Zankel 
140249ac17eSChris Zankel 	pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
141249ac17eSChris Zankel 
142249ac17eSChris Zankel 	/* Fix illegal pin numbers. */
143249ac17eSChris Zankel 
144249ac17eSChris Zankel 	if (pin == 0 || pin > 4)
145249ac17eSChris Zankel 		pin = 1;
146249ac17eSChris Zankel 
147249ac17eSChris Zankel 	if (pci_ctrl->map_irq)
148249ac17eSChris Zankel 		irq = pci_ctrl->map_irq(dev, PCI_SLOT(devfn), pin);
149249ac17eSChris Zankel 
150249ac17eSChris Zankel 	if (irq == -1)
151249ac17eSChris Zankel 		irq = 0;
152249ac17eSChris Zankel 
153249ac17eSChris Zankel 	DBG("PCI Autoconfig: Interrupt %d, pin %d\n", irq, pin);
154249ac17eSChris Zankel 
155249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
156249ac17eSChris Zankel }
157249ac17eSChris Zankel 
158249ac17eSChris Zankel 
159249ac17eSChris Zankel static void __init
160249ac17eSChris Zankel pciauto_prescan_setup_bridge(struct pci_dev *dev, int current_bus,
161249ac17eSChris Zankel 			     int sub_bus, int *iosave, int *memsave)
162249ac17eSChris Zankel {
163249ac17eSChris Zankel 	/* Configure bus number registers */
164249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_PRIMARY_BUS, current_bus);
165249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_SECONDARY_BUS, sub_bus + 1);
166249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_SUBORDINATE_BUS,	0xff);
167249ac17eSChris Zankel 
168249ac17eSChris Zankel 	/* Round memory allocator to 1MB boundary */
169249ac17eSChris Zankel 	pciauto_upper_memspc &= ~(0x100000 - 1);
170249ac17eSChris Zankel 	*memsave = pciauto_upper_memspc;
171249ac17eSChris Zankel 
172249ac17eSChris Zankel 	/* Round I/O allocator to 4KB boundary */
173249ac17eSChris Zankel 	pciauto_upper_iospc &= ~(0x1000 - 1);
174249ac17eSChris Zankel 	*iosave = pciauto_upper_iospc;
175249ac17eSChris Zankel 
176249ac17eSChris Zankel 	/* Set up memory and I/O filter limits, assume 32-bit I/O space */
177249ac17eSChris Zankel 	pci_write_config_word(dev, PCI_MEMORY_LIMIT,
178249ac17eSChris Zankel 			      ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
179249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_IO_LIMIT,
180249ac17eSChris Zankel 			      ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8);
181249ac17eSChris Zankel 	pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
182249ac17eSChris Zankel 			      ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16);
183249ac17eSChris Zankel }
184249ac17eSChris Zankel 
185249ac17eSChris Zankel static void __init
186249ac17eSChris Zankel pciauto_postscan_setup_bridge(struct pci_dev *dev, int current_bus, int sub_bus,
187249ac17eSChris Zankel 			      int *iosave, int *memsave)
188249ac17eSChris Zankel {
189249ac17eSChris Zankel 	int cmdstat;
190249ac17eSChris Zankel 
191249ac17eSChris Zankel 	/* Configure bus number registers */
192249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_SUBORDINATE_BUS,	sub_bus);
193249ac17eSChris Zankel 
194249ac17eSChris Zankel 	/*
195249ac17eSChris Zankel 	 * Round memory allocator to 1MB boundary.
196249ac17eSChris Zankel 	 * If no space used, allocate minimum.
197249ac17eSChris Zankel 	 */
198249ac17eSChris Zankel 	pciauto_upper_memspc &= ~(0x100000 - 1);
199249ac17eSChris Zankel 	if (*memsave == pciauto_upper_memspc)
200249ac17eSChris Zankel 		pciauto_upper_memspc -= 0x00100000;
201249ac17eSChris Zankel 
202249ac17eSChris Zankel 	pci_write_config_word(dev, PCI_MEMORY_BASE, pciauto_upper_memspc >> 16);
203249ac17eSChris Zankel 
204249ac17eSChris Zankel 	/* Allocate 1MB for pre-fretch */
205249ac17eSChris Zankel 	pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT,
206249ac17eSChris Zankel 			      ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
207249ac17eSChris Zankel 
208249ac17eSChris Zankel 	pciauto_upper_memspc -= 0x100000;
209249ac17eSChris Zankel 
210249ac17eSChris Zankel 	pci_write_config_word(dev, PCI_PREF_MEMORY_BASE,
211249ac17eSChris Zankel 			      pciauto_upper_memspc >> 16);
212249ac17eSChris Zankel 
213249ac17eSChris Zankel 	/* Round I/O allocator to 4KB boundary */
214249ac17eSChris Zankel 	pciauto_upper_iospc &= ~(0x1000 - 1);
215249ac17eSChris Zankel 	if (*iosave == pciauto_upper_iospc)
216249ac17eSChris Zankel 		pciauto_upper_iospc -= 0x1000;
217249ac17eSChris Zankel 
218249ac17eSChris Zankel 	pci_write_config_byte(dev, PCI_IO_BASE,
219249ac17eSChris Zankel 			      (pciauto_upper_iospc & 0x0000f000) >> 8);
220249ac17eSChris Zankel 	pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
221249ac17eSChris Zankel 			      pciauto_upper_iospc >> 16);
222249ac17eSChris Zankel 
223249ac17eSChris Zankel 	/* Enable memory and I/O accesses, enable bus master */
224249ac17eSChris Zankel 	pci_read_config_dword(dev, PCI_COMMAND, &cmdstat);
225249ac17eSChris Zankel 	pci_write_config_dword(dev, PCI_COMMAND,
226249ac17eSChris Zankel 			       cmdstat |
227249ac17eSChris Zankel 			       PCI_COMMAND_IO |
228249ac17eSChris Zankel 			       PCI_COMMAND_MEMORY |
229249ac17eSChris Zankel 			       PCI_COMMAND_MASTER);
230249ac17eSChris Zankel }
231249ac17eSChris Zankel 
232249ac17eSChris Zankel /*
233249ac17eSChris Zankel  * Scan the current PCI bus.
234249ac17eSChris Zankel  */
235249ac17eSChris Zankel 
236249ac17eSChris Zankel 
237249ac17eSChris Zankel int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
238249ac17eSChris Zankel {
239249ac17eSChris Zankel 	int sub_bus, pci_devfn, pci_class, cmdstat, found_multi=0;
240249ac17eSChris Zankel 	unsigned short vid;
241249ac17eSChris Zankel 	unsigned char header_type;
242249ac17eSChris Zankel 	struct pci_dev *dev = &pciauto_dev;
243249ac17eSChris Zankel 
244249ac17eSChris Zankel         pciauto_dev.bus = &pciauto_bus;
245249ac17eSChris Zankel         pciauto_dev.sysdata = pci_ctrl;
246249ac17eSChris Zankel 	pciauto_bus.ops = pci_ctrl->ops;
247249ac17eSChris Zankel 
248249ac17eSChris Zankel 	/*
249249ac17eSChris Zankel 	 * Fetch our I/O and memory space upper boundaries used
250249ac17eSChris Zankel 	 * to allocated base addresses on this pci_controller.
251249ac17eSChris Zankel 	 */
252249ac17eSChris Zankel 
253249ac17eSChris Zankel 	if (current_bus == pci_ctrl->first_busno)
254249ac17eSChris Zankel 	{
255249ac17eSChris Zankel 		pciauto_upper_iospc = pci_ctrl->io_resource.end + 1;
256249ac17eSChris Zankel 		pciauto_upper_memspc = pci_ctrl->mem_resources[0].end + 1;
257249ac17eSChris Zankel 	}
258249ac17eSChris Zankel 
259249ac17eSChris Zankel 	sub_bus = current_bus;
260249ac17eSChris Zankel 
261249ac17eSChris Zankel 	for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++)
262249ac17eSChris Zankel 	{
263249ac17eSChris Zankel 		/* Skip our host bridge */
264249ac17eSChris Zankel 		if ((current_bus == pci_ctrl->first_busno) && (pci_devfn == 0))
265249ac17eSChris Zankel 			continue;
266249ac17eSChris Zankel 
267249ac17eSChris Zankel 		if (PCI_FUNC(pci_devfn) && !found_multi)
268249ac17eSChris Zankel 			continue;
269249ac17eSChris Zankel 
270249ac17eSChris Zankel 		pciauto_bus.number = current_bus;
271249ac17eSChris Zankel 		pciauto_dev.devfn = pci_devfn;
272249ac17eSChris Zankel 
273249ac17eSChris Zankel 		/* If config space read fails from this device, move on */
274249ac17eSChris Zankel 		if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type))
275249ac17eSChris Zankel 			continue;
276249ac17eSChris Zankel 
277249ac17eSChris Zankel 		if (!PCI_FUNC(pci_devfn))
278249ac17eSChris Zankel 			found_multi = header_type & 0x80;
279249ac17eSChris Zankel 		pci_read_config_word(dev, PCI_VENDOR_ID, &vid);
280249ac17eSChris Zankel 
281249ac17eSChris Zankel 		if (vid == 0xffff || vid == 0x0000) {
282249ac17eSChris Zankel 			found_multi = 0;
283249ac17eSChris Zankel 			continue;
284249ac17eSChris Zankel 		}
285249ac17eSChris Zankel 
286249ac17eSChris Zankel 		pci_read_config_dword(dev, PCI_CLASS_REVISION, &pci_class);
287249ac17eSChris Zankel 
288249ac17eSChris Zankel 		if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) {
289249ac17eSChris Zankel 
290249ac17eSChris Zankel 			int iosave, memsave;
291249ac17eSChris Zankel 
292249ac17eSChris Zankel 			DBG("PCI Autoconfig: Found P2P bridge, device %d\n",
293249ac17eSChris Zankel 			    PCI_SLOT(pci_devfn));
294249ac17eSChris Zankel 
295249ac17eSChris Zankel 			/* Allocate PCI I/O and/or memory space */
296249ac17eSChris Zankel 			pciauto_setup_bars(dev, PCI_BASE_ADDRESS_1);
297249ac17eSChris Zankel 
298249ac17eSChris Zankel 			pciauto_prescan_setup_bridge(dev, current_bus, sub_bus,
299249ac17eSChris Zankel 					&iosave, &memsave);
300249ac17eSChris Zankel 			sub_bus = pciauto_bus_scan(pci_ctrl, sub_bus+1);
301249ac17eSChris Zankel 			pciauto_postscan_setup_bridge(dev, current_bus, sub_bus,
302249ac17eSChris Zankel 					&iosave, &memsave);
303249ac17eSChris Zankel 			pciauto_bus.number = current_bus;
304249ac17eSChris Zankel 
305249ac17eSChris Zankel 			continue;
306249ac17eSChris Zankel 
307249ac17eSChris Zankel 		}
308249ac17eSChris Zankel 
309249ac17eSChris Zankel 
310249ac17eSChris Zankel #if 0
311249ac17eSChris Zankel 		/* Skip legacy mode IDE controller */
312249ac17eSChris Zankel 
313249ac17eSChris Zankel 		if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
314249ac17eSChris Zankel 
315249ac17eSChris Zankel 			unsigned char prg_iface;
316249ac17eSChris Zankel 			pci_read_config_byte(dev, PCI_CLASS_PROG, &prg_iface);
317249ac17eSChris Zankel 
318249ac17eSChris Zankel 			if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
319249ac17eSChris Zankel 				DBG("PCI Autoconfig: Skipping legacy mode "
320249ac17eSChris Zankel 				    "IDE controller\n");
321249ac17eSChris Zankel 				continue;
322249ac17eSChris Zankel 			}
323249ac17eSChris Zankel 		}
324249ac17eSChris Zankel #endif
325249ac17eSChris Zankel 
326249ac17eSChris Zankel 		/*
327249ac17eSChris Zankel 		 * Found a peripheral, enable some standard
328249ac17eSChris Zankel 		 * settings
329249ac17eSChris Zankel 		 */
330249ac17eSChris Zankel 
331249ac17eSChris Zankel 		pci_read_config_dword(dev, PCI_COMMAND,	&cmdstat);
332249ac17eSChris Zankel 		pci_write_config_dword(dev, PCI_COMMAND,
333249ac17eSChris Zankel 				cmdstat |
334249ac17eSChris Zankel 					PCI_COMMAND_IO |
335249ac17eSChris Zankel 					PCI_COMMAND_MEMORY |
336249ac17eSChris Zankel 					PCI_COMMAND_MASTER);
337249ac17eSChris Zankel 		pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x80);
338249ac17eSChris Zankel 
339249ac17eSChris Zankel 		/* Allocate PCI I/O and/or memory space */
340249ac17eSChris Zankel 		DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
341249ac17eSChris Zankel 		    current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn) );
342249ac17eSChris Zankel 
343249ac17eSChris Zankel 		pciauto_setup_bars(dev, PCI_BASE_ADDRESS_5);
344249ac17eSChris Zankel 		pciauto_setup_irq(pci_ctrl, dev, pci_devfn);
345249ac17eSChris Zankel 	}
346249ac17eSChris Zankel 	return sub_bus;
347249ac17eSChris Zankel }
348249ac17eSChris Zankel 
349249ac17eSChris Zankel 
350249ac17eSChris Zankel 
351249ac17eSChris Zankel 
352249ac17eSChris Zankel 
353