pcie.c 4.1 KB

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