2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
5 * (C) Copyright 2002, 2003
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
18 #include <asm/processor.h>
22 #define PCI_HOSE_OP(rw, size, type) \
23 int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
25 int offset, type value) \
27 return hose->rw##_##size(hose, dev, offset, value); \
30 PCI_HOSE_OP(read, byte, u8 *)
31 PCI_HOSE_OP(read, word, u16 *)
32 PCI_HOSE_OP(read, dword, u32 *)
33 PCI_HOSE_OP(write, byte, u8)
34 PCI_HOSE_OP(write, word, u16)
35 PCI_HOSE_OP(write, dword, u32)
37 #define PCI_OP(rw, size, type, error_code) \
38 int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
40 struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
48 return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
51 PCI_OP(read, byte, u8 *, *value = 0xff)
52 PCI_OP(read, word, u16 *, *value = 0xffff)
53 PCI_OP(read, dword, u32 *, *value = 0xffffffff)
54 PCI_OP(write, byte, u8, )
55 PCI_OP(write, word, u16, )
56 PCI_OP(write, dword, u32, )
58 #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
59 int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
61 int offset, type val) \
65 if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
70 *val = (val32 >> ((offset & (int)off_mask) * 8)); \
75 #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
76 int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
78 int offset, type val) \
80 u32 val32, mask, ldata, shift; \
82 if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
85 shift = ((offset & (int)off_mask) * 8); \
86 ldata = (((unsigned long)val) & val_mask) << shift; \
87 mask = val_mask << shift; \
88 val32 = (val32 & ~mask) | ldata; \
90 if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
96 PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
97 PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
98 PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
99 PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
101 /* Get a virtual address associated with a BAR region */
102 void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
104 pci_addr_t pci_bus_addr;
107 /* read BAR address */
108 pci_read_config_dword(pdev, bar, &bar_response);
109 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
112 * Pass "0" as the length argument to pci_bus_to_virt. The arg
113 * isn't actualy used on any platform because u-boot assumes a static
114 * linear mapping. In the future, this could read the BAR size
115 * and pass that as the size if needed.
117 return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
124 static struct pci_controller* hose_head;
126 void pci_register_hose(struct pci_controller* hose)
128 struct pci_controller **phose = &hose_head;
131 phose = &(*phose)->next;
138 struct pci_controller *pci_bus_to_hose(int bus)
140 struct pci_controller *hose;
142 for (hose = hose_head; hose; hose = hose->next) {
143 if (bus >= hose->first_busno && bus <= hose->last_busno)
147 printf("pci_bus_to_hose() failed\n");
151 struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
153 struct pci_controller *hose;
155 for (hose = hose_head; hose; hose = hose->next) {
156 if (hose->cfg_addr == cfg_addr)
163 int pci_last_busno(void)
165 struct pci_controller *hose = hose_head;
173 return hose->last_busno;
176 pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
178 struct pci_controller * hose;
182 int i, bus, found_multi = 0;
184 for (hose = hose_head; hose; hose = hose->next) {
185 #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
186 for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
188 for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
190 for (bdf = PCI_BDF(bus, 0, 0);
191 #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
192 bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
193 PCI_MAX_PCI_FUNCTIONS - 1);
195 bdf < PCI_BDF(bus + 1, 0, 0);
197 bdf += PCI_BDF(0, 0, 1)) {
198 if (!PCI_FUNC(bdf)) {
199 pci_read_config_byte(bdf,
203 found_multi = header_type & 0x80;
209 pci_read_config_word(bdf,
212 pci_read_config_word(bdf,
216 for (i = 0; ids[i].vendor != 0; i++) {
217 if (vendor == ids[i].vendor &&
218 device == ids[i].device) {
231 pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
233 static struct pci_device_id ids[2] = {{}, {0, 0}};
235 ids[0].vendor = vendor;
236 ids[0].device = device;
238 return pci_find_devices(ids, index);
245 int __pci_hose_phys_to_bus(struct pci_controller *hose,
246 phys_addr_t phys_addr,
248 unsigned long skip_mask,
251 struct pci_region *res;
255 for (i = 0; i < hose->region_count; i++) {
256 res = &hose->regions[i];
258 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
261 if (res->flags & skip_mask)
264 bus_addr = phys_addr - res->phys_start + res->bus_start;
266 if (bus_addr >= res->bus_start &&
267 bus_addr < res->bus_start + res->size) {
276 pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
277 phys_addr_t phys_addr,
280 pci_addr_t bus_addr = 0;
284 puts("pci_hose_phys_to_bus: invalid hose\n");
289 * if PCI_REGION_MEM is set we do a two pass search with preference
290 * on matches that don't have PCI_REGION_SYS_MEMORY set
292 if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
293 ret = __pci_hose_phys_to_bus(hose, phys_addr,
294 flags, PCI_REGION_SYS_MEMORY, &bus_addr);
299 ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
302 puts("pci_hose_phys_to_bus: invalid physical address\n");
307 int __pci_hose_bus_to_phys(struct pci_controller *hose,
310 unsigned long skip_mask,
313 struct pci_region *res;
316 for (i = 0; i < hose->region_count; i++) {
317 res = &hose->regions[i];
319 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
322 if (res->flags & skip_mask)
325 if (bus_addr >= res->bus_start &&
326 bus_addr < res->bus_start + res->size) {
327 *pa = (bus_addr - res->bus_start + res->phys_start);
335 phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
339 phys_addr_t phys_addr = 0;
343 puts("pci_hose_bus_to_phys: invalid hose\n");
348 * if PCI_REGION_MEM is set we do a two pass search with preference
349 * on matches that don't have PCI_REGION_SYS_MEMORY set
351 if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
352 ret = __pci_hose_bus_to_phys(hose, bus_addr,
353 flags, PCI_REGION_SYS_MEMORY, &phys_addr);
358 ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
361 puts("pci_hose_bus_to_phys: invalid physical address\n");
370 int pci_hose_config_device(struct pci_controller *hose,
374 unsigned long command)
377 unsigned int old_command;
378 pci_addr_t bar_value;
381 int bar, found_mem64;
383 debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
386 pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
388 for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
389 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
390 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
397 /* Check the BAR type and set our address mask */
398 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
399 bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
400 /* round up region base address to a multiple of size */
401 io = ((io - 1) | (bar_size - 1)) + 1;
403 /* compute new region base address */
406 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
407 PCI_BASE_ADDRESS_MEM_TYPE_64) {
408 u32 bar_response_upper;
410 pci_hose_write_config_dword(hose, dev, bar + 4,
412 pci_hose_read_config_dword(hose, dev, bar + 4,
413 &bar_response_upper);
415 bar64 = ((u64)bar_response_upper << 32) | bar_response;
417 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
420 bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
423 /* round up region base address to multiple of size */
424 mem = ((mem - 1) | (bar_size - 1)) + 1;
426 /* compute new region base address */
427 mem = mem + bar_size;
430 /* Write it out and update our limit */
431 pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
435 #ifdef CONFIG_SYS_PCI_64BIT
436 pci_hose_write_config_dword(hose, dev, bar,
437 (u32)(bar_value >> 32));
439 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
444 /* Configure Cache Line Size Register */
445 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
447 /* Configure Latency Timer */
448 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
450 /* Disable interrupt line, if device says it wants to use interrupts */
451 pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
453 pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff);
456 pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
457 pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
458 (old_command & 0xffff0000) | command);
467 struct pci_config_table *pci_find_config(struct pci_controller *hose,
468 unsigned short class,
475 struct pci_config_table *table;
477 for (table = hose->config_table; table && table->vendor; table++) {
478 if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
479 (table->device == PCI_ANY_ID || table->device == device) &&
480 (table->class == PCI_ANY_ID || table->class == class) &&
481 (table->bus == PCI_ANY_ID || table->bus == bus) &&
482 (table->dev == PCI_ANY_ID || table->dev == dev) &&
483 (table->func == PCI_ANY_ID || table->func == func)) {
491 void pci_cfgfunc_config_device(struct pci_controller *hose,
493 struct pci_config_table *entry)
495 pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
499 void pci_cfgfunc_do_nothing(struct pci_controller *hose,
500 pci_dev_t dev, struct pci_config_table *entry)
505 * HJF: Changed this to return int. I think this is required
506 * to get the correct result when scanning bridges
508 extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
510 #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW)
511 const char * pci_class_str(u8 class)
514 case PCI_CLASS_NOT_DEFINED:
515 return "Build before PCI Rev2.0";
517 case PCI_BASE_CLASS_STORAGE:
518 return "Mass storage controller";
520 case PCI_BASE_CLASS_NETWORK:
521 return "Network controller";
523 case PCI_BASE_CLASS_DISPLAY:
524 return "Display controller";
526 case PCI_BASE_CLASS_MULTIMEDIA:
527 return "Multimedia device";
529 case PCI_BASE_CLASS_MEMORY:
530 return "Memory controller";
532 case PCI_BASE_CLASS_BRIDGE:
533 return "Bridge device";
535 case PCI_BASE_CLASS_COMMUNICATION:
536 return "Simple comm. controller";
538 case PCI_BASE_CLASS_SYSTEM:
539 return "Base system peripheral";
541 case PCI_BASE_CLASS_INPUT:
542 return "Input device";
544 case PCI_BASE_CLASS_DOCKING:
545 return "Docking station";
547 case PCI_BASE_CLASS_PROCESSOR:
550 case PCI_BASE_CLASS_SERIAL:
551 return "Serial bus controller";
553 case PCI_BASE_CLASS_INTELLIGENT:
554 return "Intelligent controller";
556 case PCI_BASE_CLASS_SATELLITE:
557 return "Satellite controller";
559 case PCI_BASE_CLASS_CRYPT:
560 return "Cryptographic device";
562 case PCI_BASE_CLASS_SIGNAL_PROCESSING:
565 case PCI_CLASS_OTHERS:
566 return "Does not fit any class";
573 #endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */
575 int __pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
578 * Check if pci device should be skipped in configuration
580 if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
581 #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
583 * Only skip configuration if "pciconfighost" is not set
585 if (getenv("pciconfighost") == NULL)
594 int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
595 __attribute__((weak, alias("__pci_skip_dev")));
597 #ifdef CONFIG_PCI_SCAN_SHOW
598 int __pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
600 if (dev == PCI_BDF(hose->first_busno, 0, 0))
605 int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
606 __attribute__((weak, alias("__pci_print_dev")));
607 #endif /* CONFIG_PCI_SCAN_SHOW */
609 int pci_hose_scan_bus(struct pci_controller *hose, int bus)
611 unsigned int sub_bus, found_multi = 0;
612 unsigned short vendor, device, class;
613 unsigned char header_type;
614 #ifndef CONFIG_PCI_PNP
615 struct pci_config_table *cfg;
618 #ifdef CONFIG_PCI_SCAN_SHOW
619 static int indent = 0;
624 for (dev = PCI_BDF(bus,0,0);
625 dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
626 PCI_MAX_PCI_FUNCTIONS - 1);
627 dev += PCI_BDF(0, 0, 1)) {
629 if (pci_skip_dev(hose, dev))
632 if (PCI_FUNC(dev) && !found_multi)
635 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
637 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
639 if (vendor == 0xffff || vendor == 0x0000)
643 found_multi = header_type & 0x80;
645 debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
646 PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
648 pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
649 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
651 #ifdef CONFIG_PCI_SCAN_SHOW
654 /* Print leading space, including bus indentation */
655 printf("%*c", indent + 1, ' ');
657 if (pci_print_dev(hose, dev)) {
658 printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
659 PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
660 vendor, device, pci_class_str(class >> 8));
664 #ifdef CONFIG_PCI_PNP
665 sub_bus = max(pciauto_config_device(hose, dev), sub_bus);
667 cfg = pci_find_config(hose, class, vendor, device,
668 PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
670 cfg->config_device(hose, dev, cfg);
671 sub_bus = max(sub_bus, hose->current_busno);
675 #ifdef CONFIG_PCI_SCAN_SHOW
680 hose->fixup_irq(hose, dev);
686 int pci_hose_scan(struct pci_controller *hose)
688 #if defined(CONFIG_PCI_BOOTDELAY)
689 static int pcidelay_done;
693 if (!pcidelay_done) {
694 /* wait "pcidelay" ms (if defined)... */
695 s = getenv("pcidelay");
697 int val = simple_strtoul(s, NULL, 10);
698 for (i = 0; i < val; i++)
703 #endif /* CONFIG_PCI_BOOTDELAY */
706 * Start scan at current_busno.
707 * PCIe will start scan at first_busno+1.
709 /* For legacy support, ensure current >= first */
710 if (hose->first_busno > hose->current_busno)
711 hose->current_busno = hose->first_busno;
712 #ifdef CONFIG_PCI_PNP
713 pciauto_config_init(hose);
715 return pci_hose_scan_bus(hose, hose->current_busno);
722 /* now call board specific pci_init()... */