leon_pci.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * leon_pci.c: LEON Host PCI support
  4. *
  5. * Copyright (C) 2011 Aeroflex Gaisler AB, Daniel Hellstrom
  6. *
  7. * Code is partially derived from pcic.c
  8. */
  9. #include <linux/of_device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include <linux/export.h>
  13. #include <asm/leon.h>
  14. #include <asm/leon_pci.h>
  15. /* The LEON architecture does not rely on a BIOS or bootloader to setup
  16. * PCI for us. The Linux generic routines are used to setup resources,
  17. * reset values of configuration-space register settings are preserved.
  18. *
  19. * PCI Memory and Prefetchable Memory is direct-mapped. However I/O Space is
  20. * accessed through a Window which is translated to low 64KB in PCI space, the
  21. * first 4KB is not used so 60KB is available.
  22. */
  23. void leon_pci_init(struct platform_device *ofdev, struct leon_pci_info *info)
  24. {
  25. LIST_HEAD(resources);
  26. struct pci_bus *root_bus;
  27. struct pci_host_bridge *bridge;
  28. int ret;
  29. bridge = pci_alloc_host_bridge(0);
  30. if (!bridge)
  31. return;
  32. pci_add_resource_offset(&resources, &info->io_space,
  33. info->io_space.start - 0x1000);
  34. pci_add_resource(&resources, &info->mem_space);
  35. info->busn.flags = IORESOURCE_BUS;
  36. pci_add_resource(&resources, &info->busn);
  37. list_splice_init(&resources, &bridge->windows);
  38. bridge->dev.parent = &ofdev->dev;
  39. bridge->sysdata = info;
  40. bridge->busnr = 0;
  41. bridge->ops = info->ops;
  42. bridge->swizzle_irq = pci_common_swizzle;
  43. bridge->map_irq = info->map_irq;
  44. ret = pci_scan_root_bus_bridge(bridge);
  45. if (ret) {
  46. pci_free_host_bridge(bridge);
  47. return;
  48. }
  49. root_bus = bridge->bus;
  50. /* Assign devices with resources */
  51. pci_assign_unassigned_resources();
  52. pci_bus_add_devices(root_bus);
  53. }
  54. void pcibios_fixup_bus(struct pci_bus *pbus)
  55. {
  56. struct pci_dev *dev;
  57. int i, has_io, has_mem;
  58. u16 cmd;
  59. list_for_each_entry(dev, &pbus->devices, bus_list) {
  60. /*
  61. * We can not rely on that the bootloader has enabled I/O
  62. * or memory access to PCI devices. Instead we enable it here
  63. * if the device has BARs of respective type.
  64. */
  65. has_io = has_mem = 0;
  66. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  67. unsigned long f = dev->resource[i].flags;
  68. if (f & IORESOURCE_IO)
  69. has_io = 1;
  70. else if (f & IORESOURCE_MEM)
  71. has_mem = 1;
  72. }
  73. /* ROM BARs are mapped into 32-bit memory space */
  74. if (dev->resource[PCI_ROM_RESOURCE].end != 0) {
  75. dev->resource[PCI_ROM_RESOURCE].flags |=
  76. IORESOURCE_ROM_ENABLE;
  77. has_mem = 1;
  78. }
  79. pci_bus_read_config_word(pbus, dev->devfn, PCI_COMMAND, &cmd);
  80. if (has_io && !(cmd & PCI_COMMAND_IO)) {
  81. #ifdef CONFIG_PCI_DEBUG
  82. printk(KERN_INFO "LEONPCI: Enabling I/O for dev %s\n",
  83. pci_name(dev));
  84. #endif
  85. cmd |= PCI_COMMAND_IO;
  86. pci_bus_write_config_word(pbus, dev->devfn, PCI_COMMAND,
  87. cmd);
  88. }
  89. if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
  90. #ifdef CONFIG_PCI_DEBUG
  91. printk(KERN_INFO "LEONPCI: Enabling MEMORY for dev"
  92. "%s\n", pci_name(dev));
  93. #endif
  94. cmd |= PCI_COMMAND_MEMORY;
  95. pci_bus_write_config_word(pbus, dev->devfn, PCI_COMMAND,
  96. cmd);
  97. }
  98. }
  99. }