pcie.c 4.2 KB

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