2 * arch/powerpc/kernel/pci_auto.c
4 * PCI autoconfiguration library
8 * Copyright 2000 MontaVista Software Inc.
10 * SPDX-License-Identifier: GPL-2.0+
18 #define DEBUGF(x...) printf(x)
23 #define PCIAUTO_IDE_MODE_MASK 0x05
25 /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
26 #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
27 #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
34 void pciauto_region_init(struct pci_region *res)
37 * Avoid allocating PCI resources from address 0 -- this is illegal
38 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
39 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
41 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
44 void pciauto_region_align(struct pci_region *res, pci_size_t size)
46 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
49 int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
55 DEBUGF("No resource");
59 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
61 if (addr - res->bus_start + size > res->size) {
62 DEBUGF("No room in resource");
66 res->bus_lower = addr + size;
68 DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower);
74 *bar = (pci_addr_t)-1;
82 void pciauto_setup_device(struct pci_controller *hose,
83 pci_dev_t dev, int bars_num,
84 struct pci_region *mem,
85 struct pci_region *prefetch,
86 struct pci_region *io)
92 #ifndef CONFIG_PCI_ENUM_ONLY
94 struct pci_region *bar_res;
98 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
99 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
101 for (bar = PCI_BASE_ADDRESS_0;
102 bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
103 /* Tickle the BAR and get the response */
104 #ifndef CONFIG_PCI_ENUM_ONLY
105 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
107 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
109 /* If BAR is not implemented go to the next BAR */
113 #ifndef CONFIG_PCI_ENUM_ONLY
117 /* Check the BAR type and set our address mask */
118 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
119 bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
121 #ifndef CONFIG_PCI_ENUM_ONLY
125 DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", bar_nr, (u64)bar_size);
127 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
128 PCI_BASE_ADDRESS_MEM_TYPE_64) {
129 u32 bar_response_upper;
132 #ifndef CONFIG_PCI_ENUM_ONLY
133 pci_hose_write_config_dword(hose, dev, bar + 4,
136 pci_hose_read_config_dword(hose, dev, bar + 4,
137 &bar_response_upper);
139 bar64 = ((u64)bar_response_upper << 32) | bar_response;
141 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
142 #ifndef CONFIG_PCI_ENUM_ONLY
146 bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
148 #ifndef CONFIG_PCI_ENUM_ONLY
149 if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
155 DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%llx, ", bar_nr, (u64)bar_size);
158 #ifndef CONFIG_PCI_ENUM_ONLY
159 if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
160 /* Write it out and update our limit */
161 pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value);
165 #ifdef CONFIG_SYS_PCI_64BIT
166 pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
169 * If we are a 64-bit decoder then increment to the
170 * upper 32 bits of the bar and force it to locate
171 * in the lower 4GB of memory.
173 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
179 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
180 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
187 pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
188 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
189 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
190 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
193 int pciauto_setup_rom(struct pci_controller *hose, pci_dev_t dev)
195 pci_addr_t bar_value;
200 pci_hose_write_config_dword(hose, dev, PCI_ROM_ADDRESS, 0xfffffffe);
201 pci_hose_read_config_dword(hose, dev, PCI_ROM_ADDRESS, &bar_response);
205 bar_size = -(bar_response & ~1);
206 DEBUGF("PCI Autoconfig: ROM, size=%#x, ", bar_size);
207 if (pciauto_region_allocate(hose->pci_mem, bar_size, &bar_value) == 0) {
208 pci_hose_write_config_dword(hose, dev, PCI_ROM_ADDRESS,
212 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
213 cmdstat |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
214 pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
219 void pciauto_prescan_setup_bridge(struct pci_controller *hose,
220 pci_dev_t dev, int sub_bus)
222 struct pci_region *pci_mem = hose->pci_mem;
223 struct pci_region *pci_prefetch = hose->pci_prefetch;
224 struct pci_region *pci_io = hose->pci_io;
225 u16 cmdstat, prefechable_64;
227 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
228 pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
230 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
232 /* Configure bus number registers */
233 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
234 PCI_BUS(dev) - hose->first_busno);
235 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
236 sub_bus - hose->first_busno);
237 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
240 /* Round memory allocator to 1MB boundary */
241 pciauto_region_align(pci_mem, 0x100000);
243 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
244 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
245 (pci_mem->bus_lower & 0xfff00000) >> 16);
247 cmdstat |= PCI_COMMAND_MEMORY;
251 /* Round memory allocator to 1MB boundary */
252 pciauto_region_align(pci_prefetch, 0x100000);
254 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
255 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
256 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
257 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
258 #ifdef CONFIG_SYS_PCI_64BIT
259 pci_hose_write_config_dword(hose, dev,
260 PCI_PREF_BASE_UPPER32,
261 pci_prefetch->bus_lower >> 32);
263 pci_hose_write_config_dword(hose, dev,
264 PCI_PREF_BASE_UPPER32,
268 cmdstat |= PCI_COMMAND_MEMORY;
270 /* We don't support prefetchable memory for now, so disable */
271 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
272 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
273 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
274 pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
275 pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
280 /* Round I/O allocator to 4KB boundary */
281 pciauto_region_align(pci_io, 0x1000);
283 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
284 (pci_io->bus_lower & 0x0000f000) >> 8);
285 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
286 (pci_io->bus_lower & 0xffff0000) >> 16);
288 cmdstat |= PCI_COMMAND_IO;
291 /* Enable memory and I/O accesses, enable bus master */
292 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
293 cmdstat | PCI_COMMAND_MASTER);
296 void pciauto_postscan_setup_bridge(struct pci_controller *hose,
297 pci_dev_t dev, int sub_bus)
299 struct pci_region *pci_mem = hose->pci_mem;
300 struct pci_region *pci_prefetch = hose->pci_prefetch;
301 struct pci_region *pci_io = hose->pci_io;
303 /* Configure bus number registers */
304 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
305 sub_bus - hose->first_busno);
308 /* Round memory allocator to 1MB boundary */
309 pciauto_region_align(pci_mem, 0x100000);
311 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
312 (pci_mem->bus_lower - 1) >> 16);
318 pci_hose_read_config_word(hose, dev,
319 PCI_PREF_MEMORY_LIMIT,
321 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
323 /* Round memory allocator to 1MB boundary */
324 pciauto_region_align(pci_prefetch, 0x100000);
326 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
327 (pci_prefetch->bus_lower - 1) >> 16);
328 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
329 #ifdef CONFIG_SYS_PCI_64BIT
330 pci_hose_write_config_dword(hose, dev,
331 PCI_PREF_LIMIT_UPPER32,
332 (pci_prefetch->bus_lower - 1) >> 32);
334 pci_hose_write_config_dword(hose, dev,
335 PCI_PREF_LIMIT_UPPER32,
341 /* Round I/O allocator to 4KB boundary */
342 pciauto_region_align(pci_io, 0x1000);
344 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
345 ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
346 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
347 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
355 void pciauto_config_init(struct pci_controller *hose)
359 hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
361 for (i = 0; i < hose->region_count; i++) {
362 switch(hose->regions[i].flags) {
365 hose->pci_io->size < hose->regions[i].size)
366 hose->pci_io = hose->regions + i;
369 if (!hose->pci_mem ||
370 hose->pci_mem->size < hose->regions[i].size)
371 hose->pci_mem = hose->regions + i;
373 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
374 if (!hose->pci_prefetch ||
375 hose->pci_prefetch->size < hose->regions[i].size)
376 hose->pci_prefetch = hose->regions + i;
383 pciauto_region_init(hose->pci_mem);
385 DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
386 "\t\tPhysical Memory [%llx-%llxx]\n",
387 (u64)hose->pci_mem->bus_start,
388 (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
389 (u64)hose->pci_mem->phys_start,
390 (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
393 if (hose->pci_prefetch) {
394 pciauto_region_init(hose->pci_prefetch);
396 DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
397 "\t\tPhysical Memory [%llx-%llx]\n",
398 (u64)hose->pci_prefetch->bus_start,
399 (u64)(hose->pci_prefetch->bus_start +
400 hose->pci_prefetch->size - 1),
401 (u64)hose->pci_prefetch->phys_start,
402 (u64)(hose->pci_prefetch->phys_start +
403 hose->pci_prefetch->size - 1));
407 pciauto_region_init(hose->pci_io);
409 DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
410 "\t\tPhysical Memory: [%llx-%llx]\n",
411 (u64)hose->pci_io->bus_start,
412 (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
413 (u64)hose->pci_io->phys_start,
414 (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
420 * HJF: Changed this to return int. I think this is required
421 * to get the correct result when scanning bridges
423 int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
425 unsigned int sub_bus = PCI_BUS(dev);
426 unsigned short class;
427 unsigned char prg_iface;
430 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
433 case PCI_CLASS_BRIDGE_PCI:
434 DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n",
437 pciauto_setup_device(hose, dev, 2, hose->pci_mem,
438 hose->pci_prefetch, hose->pci_io);
441 n = dm_pci_hose_probe_bus(hose, dev);
444 sub_bus = (unsigned int)n;
446 /* Passing in current_busno allows for sibling P2P bridges */
447 hose->current_busno++;
448 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
450 * need to figure out if this is a subordinate bridge on the bus
451 * to be able to properly set the pri/sec/sub bridge registers.
453 n = pci_hose_scan_bus(hose, hose->current_busno);
455 /* figure out the deepest we've gone for this leg */
456 sub_bus = max((unsigned int)n, sub_bus);
457 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
459 sub_bus = hose->current_busno;
463 case PCI_CLASS_STORAGE_IDE:
464 pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface);
465 if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
466 DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
470 pciauto_setup_device(hose, dev, 6, hose->pci_mem,
471 hose->pci_prefetch, hose->pci_io);
474 case PCI_CLASS_BRIDGE_CARDBUS:
476 * just do a minimal setup of the bridge,
477 * let the OS take care of the rest
479 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
480 hose->pci_prefetch, hose->pci_io);
482 DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
485 #ifndef CONFIG_DM_PCI
486 hose->current_busno++;
490 #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
491 case PCI_CLASS_BRIDGE_OTHER:
492 DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
496 #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
497 case PCI_CLASS_BRIDGE_OTHER:
499 * The host/PCI bridge 1 seems broken in 8349 - it presents
500 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
501 * device claiming resources io/mem/irq.. we only allow for
502 * the PIMMR window to be allocated (BAR0 - 1MB size)
504 DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
505 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
506 hose->pci_prefetch, hose->pci_io);
510 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
511 DEBUGF("PCI AutoConfig: Found PowerPC device\n");
514 pciauto_setup_device(hose, dev, 6, hose->pci_mem,
515 hose->pci_prefetch, hose->pci_io);