pcie.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * arch/arm/mach-kirkwood/pcie.c
  3. *
  4. * PCIe functions for Marvell Kirkwood SoCs
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include <linux/mbus.h>
  13. #include <asm/mach/pci.h>
  14. #include <plat/pcie.h>
  15. #include "common.h"
  16. #define PCIE_BASE ((void __iomem *)PCIE_VIRT_BASE)
  17. void __init kirkwood_pcie_id(u32 *dev, u32 *rev)
  18. {
  19. *dev = orion_pcie_dev_id(PCIE_BASE);
  20. *rev = orion_pcie_rev(PCIE_BASE);
  21. }
  22. static int pcie_valid_config(int bus, int dev)
  23. {
  24. /*
  25. * Don't go out when trying to access --
  26. * 1. nonexisting device on local bus
  27. * 2. where there's no device connected (no link)
  28. */
  29. if (bus == 0 && dev == 0)
  30. return 1;
  31. if (!orion_pcie_link_up(PCIE_BASE))
  32. return 0;
  33. if (bus == 0 && dev != 1)
  34. return 0;
  35. return 1;
  36. }
  37. /*
  38. * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
  39. * and then reading the PCIE_CONF_DATA register. Need to make sure these
  40. * transactions are atomic.
  41. */
  42. static DEFINE_SPINLOCK(kirkwood_pcie_lock);
  43. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  44. int size, u32 *val)
  45. {
  46. unsigned long flags;
  47. int ret;
  48. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
  49. *val = 0xffffffff;
  50. return PCIBIOS_DEVICE_NOT_FOUND;
  51. }
  52. spin_lock_irqsave(&kirkwood_pcie_lock, flags);
  53. ret = orion_pcie_rd_conf(PCIE_BASE, bus, devfn, where, size, val);
  54. spin_unlock_irqrestore(&kirkwood_pcie_lock, flags);
  55. return ret;
  56. }
  57. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  58. int where, int size, u32 val)
  59. {
  60. unsigned long flags;
  61. int ret;
  62. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0)
  63. return PCIBIOS_DEVICE_NOT_FOUND;
  64. spin_lock_irqsave(&kirkwood_pcie_lock, flags);
  65. ret = orion_pcie_wr_conf(PCIE_BASE, bus, devfn, where, size, val);
  66. spin_unlock_irqrestore(&kirkwood_pcie_lock, flags);
  67. return ret;
  68. }
  69. static struct pci_ops pcie_ops = {
  70. .read = pcie_rd_conf,
  71. .write = pcie_wr_conf,
  72. };
  73. static int kirkwood_pcie_setup(int nr, struct pci_sys_data *sys)
  74. {
  75. struct resource *res;
  76. /*
  77. * Generic PCIe unit setup.
  78. */
  79. orion_pcie_setup(PCIE_BASE, &kirkwood_mbus_dram_info);
  80. /*
  81. * Request resources.
  82. */
  83. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  84. if (!res)
  85. panic("pcie_setup unable to alloc resources");
  86. /*
  87. * IORESOURCE_IO
  88. */
  89. res[0].name = "PCIe I/O Space";
  90. res[0].flags = IORESOURCE_IO;
  91. res[0].start = KIRKWOOD_PCIE_IO_PHYS_BASE;
  92. res[0].end = res[0].start + KIRKWOOD_PCIE_IO_SIZE - 1;
  93. if (request_resource(&ioport_resource, &res[0]))
  94. panic("Request PCIe IO resource failed\n");
  95. sys->resource[0] = &res[0];
  96. /*
  97. * IORESOURCE_MEM
  98. */
  99. res[1].name = "PCIe Memory Space";
  100. res[1].flags = IORESOURCE_MEM;
  101. res[1].start = KIRKWOOD_PCIE_MEM_PHYS_BASE;
  102. res[1].end = res[1].start + KIRKWOOD_PCIE_MEM_SIZE - 1;
  103. if (request_resource(&iomem_resource, &res[1]))
  104. panic("Request PCIe Memory resource failed\n");
  105. sys->resource[1] = &res[1];
  106. sys->resource[2] = NULL;
  107. sys->io_offset = 0;
  108. return 1;
  109. }
  110. static void __devinit rc_pci_fixup(struct pci_dev *dev)
  111. {
  112. /*
  113. * Prevent enumeration of root complex.
  114. */
  115. if (dev->bus->parent == NULL && dev->devfn == 0) {
  116. int i;
  117. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  118. dev->resource[i].start = 0;
  119. dev->resource[i].end = 0;
  120. dev->resource[i].flags = 0;
  121. }
  122. }
  123. }
  124. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  125. static struct pci_bus __init *
  126. kirkwood_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  127. {
  128. struct pci_bus *bus;
  129. if (nr == 0) {
  130. bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
  131. } else {
  132. bus = NULL;
  133. BUG();
  134. }
  135. return bus;
  136. }
  137. static int __init kirkwood_pcie_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  138. {
  139. return IRQ_KIRKWOOD_PCIE;
  140. }
  141. static struct hw_pci kirkwood_pci __initdata = {
  142. .nr_controllers = 1,
  143. .swizzle = pci_std_swizzle,
  144. .setup = kirkwood_pcie_setup,
  145. .scan = kirkwood_pcie_scan_bus,
  146. .map_irq = kirkwood_pcie_map_irq,
  147. };
  148. void __init kirkwood_pcie_init(void)
  149. {
  150. pci_common_init(&kirkwood_pci);
  151. }