2 * arch/ppc/kernel/pci_auto.c
4 * PCI autoconfiguration library
6 * Author: Matt Porter <mporter@mvista.com>
8 * Copyright 2000 MontaVista Software Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
22 #define DEBUGF(x...) printf(x)
27 #define PCIAUTO_IDE_MODE_MASK 0x05
29 /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
30 #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
31 #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
38 void pciauto_region_init(struct pci_region* res)
41 * Avoid allocating PCI resources from address 0 -- this is illegal
42 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
43 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
45 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
48 void pciauto_region_align(struct pci_region *res, unsigned long size)
50 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
53 int pciauto_region_allocate(struct pci_region* res, unsigned int size, unsigned int *bar)
58 DEBUGF("No resource");
62 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
64 if (addr - res->bus_start + size > res->size) {
65 DEBUGF("No room in resource");
69 res->bus_lower = addr + size;
71 DEBUGF("address=0x%lx bus_lower=%x", addr, res->bus_lower);
85 void pciauto_setup_device(struct pci_controller *hose,
86 pci_dev_t dev, int bars_num,
87 struct pci_region *mem,
88 struct pci_region *prefetch,
89 struct pci_region *io)
91 unsigned int bar_value, bar_response, bar_size;
92 unsigned int cmdstat = 0;
93 struct pci_region *bar_res;
97 pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat);
98 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
100 for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_0 + (bars_num*4); bar += 4) {
101 /* Tickle the BAR and get the response */
102 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
103 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
105 /* If BAR is not implemented go to the next BAR */
111 /* Check the BAR type and set our address mask */
112 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
113 bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
117 DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%x, ", bar_nr, bar_size);
119 if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
120 PCI_BASE_ADDRESS_MEM_TYPE_64)
123 bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1;
124 if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
129 DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%x, ", bar_nr, bar_size);
132 if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
133 /* Write it out and update our limit */
134 pci_hose_write_config_dword(hose, dev, bar, bar_value);
137 * If we are a 64-bit decoder then increment to the
138 * upper 32 bits of the bar and force it to locate
139 * in the lower 4GB of memory.
143 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
146 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
147 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
155 pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat);
156 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
157 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
158 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
161 void pciauto_prescan_setup_bridge(struct pci_controller *hose,
162 pci_dev_t dev, int sub_bus)
164 struct pci_region *pci_mem = hose->pci_mem;
165 struct pci_region *pci_prefetch = hose->pci_prefetch;
166 struct pci_region *pci_io = hose->pci_io;
167 unsigned int cmdstat;
169 pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat);
171 /* Configure bus number registers */
172 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
173 PCI_BUS(dev) - hose->first_busno);
174 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
175 sub_bus - hose->first_busno);
176 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
179 /* Round memory allocator to 1MB boundary */
180 pciauto_region_align(pci_mem, 0x100000);
182 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
183 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
184 (pci_mem->bus_lower & 0xfff00000) >> 16);
186 cmdstat |= PCI_COMMAND_MEMORY;
190 /* Round memory allocator to 1MB boundary */
191 pciauto_region_align(pci_prefetch, 0x100000);
193 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
194 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
195 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
197 cmdstat |= PCI_COMMAND_MEMORY;
199 /* We don't support prefetchable memory for now, so disable */
200 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
201 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
205 /* Round I/O allocator to 4KB boundary */
206 pciauto_region_align(pci_io, 0x1000);
208 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
209 (pci_io->bus_lower & 0x0000f000) >> 8);
210 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
211 (pci_io->bus_lower & 0xffff0000) >> 16);
213 cmdstat |= PCI_COMMAND_IO;
216 /* Enable memory and I/O accesses, enable bus master */
217 pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
220 void pciauto_postscan_setup_bridge(struct pci_controller *hose,
221 pci_dev_t dev, int sub_bus)
223 struct pci_region *pci_mem = hose->pci_mem;
224 struct pci_region *pci_prefetch = hose->pci_prefetch;
225 struct pci_region *pci_io = hose->pci_io;
227 /* Configure bus number registers */
228 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
229 sub_bus - hose->first_busno);
232 /* Round memory allocator to 1MB boundary */
233 pciauto_region_align(pci_mem, 0x100000);
235 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
236 (pci_mem->bus_lower-1) >> 16);
240 /* Round memory allocator to 1MB boundary */
241 pciauto_region_align(pci_prefetch, 0x100000);
243 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
244 (pci_prefetch->bus_lower-1) >> 16);
248 /* Round I/O allocator to 4KB boundary */
249 pciauto_region_align(pci_io, 0x1000);
251 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
252 ((pci_io->bus_lower-1) & 0x0000f000) >> 8);
253 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
254 ((pci_io->bus_lower-1) & 0xffff0000) >> 16);
262 void pciauto_config_init(struct pci_controller *hose)
266 hose->pci_io = hose->pci_mem = NULL;
268 for (i=0; i<hose->region_count; i++) {
269 switch(hose->regions[i].flags) {
272 hose->pci_io->size < hose->regions[i].size)
273 hose->pci_io = hose->regions + i;
276 if (!hose->pci_mem ||
277 hose->pci_mem->size < hose->regions[i].size)
278 hose->pci_mem = hose->regions + i;
280 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
281 if (!hose->pci_prefetch ||
282 hose->pci_prefetch->size < hose->regions[i].size)
283 hose->pci_prefetch = hose->regions + i;
290 pciauto_region_init(hose->pci_mem);
292 DEBUGF("PCI Autoconfig: Bus Memory region: [%lx-%lx],\n"
293 "\t\tPhysical Memory [%x-%x]\n",
294 hose->pci_mem->bus_start,
295 hose->pci_mem->bus_start + hose->pci_mem->size - 1,
296 hose->pci_mem->phys_start,
297 hose->pci_mem->phys_start + hose->pci_mem->size - 1);
300 if (hose->pci_prefetch) {
301 pciauto_region_init(hose->pci_prefetch);
303 DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [%lx-%lx],\n"
304 "\t\tPhysical Memory [%x-%x]\n",
305 hose->pci_prefetch->bus_start,
306 hose->pci_prefetch->bus_start + hose->pci_prefetch->size - 1,
307 hose->pci_prefetch->phys_start,
308 hose->pci_prefetch->phys_start +
309 hose->pci_prefetch->size - 1);
313 pciauto_region_init(hose->pci_io);
315 DEBUGF("PCI Autoconfig: Bus I/O region: [%lx-%lx],\n"
316 "\t\tPhysical Memory: [%x-%x]\n",
317 hose->pci_io->bus_start,
318 hose->pci_io->bus_start + hose->pci_io->size - 1,
319 hose->pci_io->phys_start,
320 hose->pci_io->phys_start + hose->pci_io->size - 1);
325 /* HJF: Changed this to return int. I think this is required
326 * to get the correct result when scanning bridges
328 int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
330 unsigned int sub_bus = PCI_BUS(dev);
331 unsigned short class;
332 unsigned char prg_iface;
335 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
338 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
339 DEBUGF("PCI AutoConfig: Found PowerPC device\n");
340 pciauto_setup_device(hose, dev, 6, hose->pci_mem,
341 hose->pci_prefetch, hose->pci_io);
344 case PCI_CLASS_BRIDGE_PCI:
345 hose->current_busno++;
346 pciauto_setup_device(hose, dev, 2, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
348 DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev));
350 /* Passing in current_busno allows for sibling P2P bridges */
351 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
353 * need to figure out if this is a subordinate bridge on the bus
354 * to be able to properly set the pri/sec/sub bridge registers.
356 n = pci_hose_scan_bus(hose, hose->current_busno);
358 /* figure out the deepest we've gone for this leg */
359 sub_bus = max(n, sub_bus);
360 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
362 sub_bus = hose->current_busno;
365 case PCI_CLASS_STORAGE_IDE:
366 pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface);
367 if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
368 DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
372 pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
375 case PCI_CLASS_BRIDGE_CARDBUS:
376 /* just do a minimal setup of the bridge, let the OS take care of the rest */
377 pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
379 DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n", PCI_DEV(dev));
381 hose->current_busno++;
384 #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
385 case PCI_CLASS_BRIDGE_OTHER:
386 DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
390 #ifdef CONFIG_MPC834X
391 case PCI_CLASS_BRIDGE_OTHER:
393 * The host/PCI bridge 1 seems broken in 8349 - it presents
394 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
395 * device claiming resources io/mem/irq.. we only allow for
396 * the PIMMR window to be allocated (BAR0 - 1MB size)
398 DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
399 pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
403 pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_prefetch, hose->pci_io);