pcie.c 4.2 KB

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