2 * arch/powerpc/kernel/pci_auto.c
4 * PCI autoconfiguration library
6 * Author: Matt Porter <mporter@mvista.com>
8 * Copyright 2000 MontaVista Software Inc.
10 * SPDX-License-Identifier: GPL-2.0+
18 #define DEBUGF(x...) printf(x)
23 /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
24 #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
25 #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
32 void pciauto_region_init(struct pci_region *res)
35 * Avoid allocating PCI resources from address 0 -- this is illegal
36 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
37 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
39 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
42 void pciauto_region_align(struct pci_region *res, pci_size_t size)
44 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
47 int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
53 DEBUGF("No resource");
57 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
59 if (addr - res->bus_start + size > res->size) {
60 DEBUGF("No room in resource");
64 res->bus_lower = addr + size;
66 DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower);
72 *bar = (pci_addr_t)-1;
80 void pciauto_setup_device(struct pci_controller *hose,
81 pci_dev_t dev, int bars_num,
82 struct pci_region *mem,
83 struct pci_region *prefetch,
84 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 /* Configure the expansion ROM address */
188 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
189 if (header_type != PCI_HEADER_TYPE_CARDBUS) {
190 rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
191 PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
192 pci_hose_write_config_dword(hose, dev, rom_addr, 0xfffffffe);
193 pci_hose_read_config_dword(hose, dev, rom_addr, &bar_response);
195 bar_size = -(bar_response & ~1);
196 DEBUGF("PCI Autoconfig: ROM, size=%#x, ", bar_size);
197 if (pciauto_region_allocate(mem, bar_size,
199 pci_hose_write_config_dword(hose, dev, rom_addr,
202 cmdstat |= PCI_COMMAND_MEMORY;
207 pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
208 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
209 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
210 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
213 void pciauto_prescan_setup_bridge(struct pci_controller *hose,
214 pci_dev_t dev, int sub_bus)
216 struct pci_region *pci_mem = hose->pci_mem;
217 struct pci_region *pci_prefetch = hose->pci_prefetch;
218 struct pci_region *pci_io = hose->pci_io;
219 u16 cmdstat, prefechable_64;
221 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
222 pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
224 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
226 /* Configure bus number registers */
228 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, PCI_BUS(dev));
229 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, sub_bus);
231 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
232 PCI_BUS(dev) - hose->first_busno);
233 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
234 sub_bus - hose->first_busno);
236 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
239 /* Round memory allocator to 1MB boundary */
240 pciauto_region_align(pci_mem, 0x100000);
242 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
243 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
244 (pci_mem->bus_lower & 0xfff00000) >> 16);
246 cmdstat |= PCI_COMMAND_MEMORY;
250 /* Round memory allocator to 1MB boundary */
251 pciauto_region_align(pci_prefetch, 0x100000);
253 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
254 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
255 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
256 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
257 #ifdef CONFIG_SYS_PCI_64BIT
258 pci_hose_write_config_dword(hose, dev,
259 PCI_PREF_BASE_UPPER32,
260 pci_prefetch->bus_lower >> 32);
262 pci_hose_write_config_dword(hose, dev,
263 PCI_PREF_BASE_UPPER32,
267 cmdstat |= PCI_COMMAND_MEMORY;
269 /* We don't support prefetchable memory for now, so disable */
270 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
271 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
272 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
273 pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
274 pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
279 /* Round I/O allocator to 4KB boundary */
280 pciauto_region_align(pci_io, 0x1000);
282 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
283 (pci_io->bus_lower & 0x0000f000) >> 8);
284 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
285 (pci_io->bus_lower & 0xffff0000) >> 16);
287 cmdstat |= PCI_COMMAND_IO;
290 /* Enable memory and I/O accesses, enable bus master */
291 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
292 cmdstat | PCI_COMMAND_MASTER);
295 void pciauto_postscan_setup_bridge(struct pci_controller *hose,
296 pci_dev_t dev, int sub_bus)
298 struct pci_region *pci_mem = hose->pci_mem;
299 struct pci_region *pci_prefetch = hose->pci_prefetch;
300 struct pci_region *pci_io = hose->pci_io;
302 /* Configure bus number registers */
304 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, sub_bus);
306 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
307 sub_bus - hose->first_busno);
311 /* Round memory allocator to 1MB boundary */
312 pciauto_region_align(pci_mem, 0x100000);
314 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
315 (pci_mem->bus_lower - 1) >> 16);
321 pci_hose_read_config_word(hose, dev,
322 PCI_PREF_MEMORY_LIMIT,
324 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
326 /* Round memory allocator to 1MB boundary */
327 pciauto_region_align(pci_prefetch, 0x100000);
329 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
330 (pci_prefetch->bus_lower - 1) >> 16);
331 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
332 #ifdef CONFIG_SYS_PCI_64BIT
333 pci_hose_write_config_dword(hose, dev,
334 PCI_PREF_LIMIT_UPPER32,
335 (pci_prefetch->bus_lower - 1) >> 32);
337 pci_hose_write_config_dword(hose, dev,
338 PCI_PREF_LIMIT_UPPER32,
344 /* Round I/O allocator to 4KB boundary */
345 pciauto_region_align(pci_io, 0x1000);
347 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
348 ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
349 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
350 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
358 void pciauto_config_init(struct pci_controller *hose)
362 hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
364 for (i = 0; i < hose->region_count; i++) {
365 switch(hose->regions[i].flags) {
368 hose->pci_io->size < hose->regions[i].size)
369 hose->pci_io = hose->regions + i;
372 if (!hose->pci_mem ||
373 hose->pci_mem->size < hose->regions[i].size)
374 hose->pci_mem = hose->regions + i;
376 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
377 if (!hose->pci_prefetch ||
378 hose->pci_prefetch->size < hose->regions[i].size)
379 hose->pci_prefetch = hose->regions + i;
386 pciauto_region_init(hose->pci_mem);
388 DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
389 "\t\tPhysical Memory [%llx-%llxx]\n",
390 (u64)hose->pci_mem->bus_start,
391 (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
392 (u64)hose->pci_mem->phys_start,
393 (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
396 if (hose->pci_prefetch) {
397 pciauto_region_init(hose->pci_prefetch);
399 DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
400 "\t\tPhysical Memory [%llx-%llx]\n",
401 (u64)hose->pci_prefetch->bus_start,
402 (u64)(hose->pci_prefetch->bus_start +
403 hose->pci_prefetch->size - 1),
404 (u64)hose->pci_prefetch->phys_start,
405 (u64)(hose->pci_prefetch->phys_start +
406 hose->pci_prefetch->size - 1));
410 pciauto_region_init(hose->pci_io);
412 DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
413 "\t\tPhysical Memory: [%llx-%llx]\n",
414 (u64)hose->pci_io->bus_start,
415 (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
416 (u64)hose->pci_io->phys_start,
417 (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
423 * HJF: Changed this to return int. I think this is required
424 * to get the correct result when scanning bridges
426 int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
428 unsigned int sub_bus = PCI_BUS(dev);
429 unsigned short class;
432 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
435 case PCI_CLASS_BRIDGE_PCI:
436 DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n",
439 pciauto_setup_device(hose, dev, 2, hose->pci_mem,
440 hose->pci_prefetch, hose->pci_io);
443 n = dm_pci_hose_probe_bus(hose, dev);
446 sub_bus = (unsigned int)n;
448 /* Passing in current_busno allows for sibling P2P bridges */
449 hose->current_busno++;
450 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
452 * need to figure out if this is a subordinate bridge on the bus
453 * to be able to properly set the pri/sec/sub bridge registers.
455 n = pci_hose_scan_bus(hose, hose->current_busno);
457 /* figure out the deepest we've gone for this leg */
458 sub_bus = max((unsigned int)n, sub_bus);
459 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
461 sub_bus = hose->current_busno;
465 case PCI_CLASS_BRIDGE_CARDBUS:
467 * just do a minimal setup of the bridge,
468 * let the OS take care of the rest
470 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
471 hose->pci_prefetch, hose->pci_io);
473 DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
476 #ifndef CONFIG_DM_PCI
477 hose->current_busno++;
481 #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
482 case PCI_CLASS_BRIDGE_OTHER:
483 DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
487 #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
488 case PCI_CLASS_BRIDGE_OTHER:
490 * The host/PCI bridge 1 seems broken in 8349 - it presents
491 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
492 * device claiming resources io/mem/irq.. we only allow for
493 * the PIMMR window to be allocated (BAR0 - 1MB size)
495 DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
496 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
497 hose->pci_prefetch, hose->pci_io);
501 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
502 DEBUGF("PCI AutoConfig: Found PowerPC device\n");
505 pciauto_setup_device(hose, dev, 6, hose->pci_mem,
506 hose->pci_prefetch, hose->pci_io);