pcie.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * arch/arm/mach-dove/pcie.c
  3. *
  4. * PCIe functions for Marvell Dove 88AP510 SoC
  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 <asm/mach/arch.h>
  15. #include <asm/setup.h>
  16. #include <asm/delay.h>
  17. #include <plat/pcie.h>
  18. #include <mach/irqs.h>
  19. #include <mach/bridge-regs.h>
  20. #include "common.h"
  21. struct pcie_port {
  22. u8 index;
  23. u8 root_bus_nr;
  24. void __iomem *base;
  25. spinlock_t conf_lock;
  26. char io_space_name[16];
  27. char mem_space_name[16];
  28. struct resource res[2];
  29. };
  30. static struct pcie_port pcie_port[2];
  31. static int num_pcie_ports;
  32. static int __init dove_pcie_setup(int nr, struct pci_sys_data *sys)
  33. {
  34. struct pcie_port *pp;
  35. if (nr >= num_pcie_ports)
  36. return 0;
  37. pp = &pcie_port[nr];
  38. pp->root_bus_nr = sys->busnr;
  39. /*
  40. * Generic PCIe unit setup.
  41. */
  42. orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
  43. orion_pcie_setup(pp->base, &dove_mbus_dram_info);
  44. /*
  45. * IORESOURCE_IO
  46. */
  47. snprintf(pp->io_space_name, sizeof(pp->io_space_name),
  48. "PCIe %d I/O", pp->index);
  49. pp->io_space_name[sizeof(pp->io_space_name) - 1] = 0;
  50. pp->res[0].name = pp->io_space_name;
  51. if (pp->index == 0) {
  52. pp->res[0].start = DOVE_PCIE0_IO_PHYS_BASE;
  53. pp->res[0].end = pp->res[0].start + DOVE_PCIE0_IO_SIZE - 1;
  54. } else {
  55. pp->res[0].start = DOVE_PCIE1_IO_PHYS_BASE;
  56. pp->res[0].end = pp->res[0].start + DOVE_PCIE1_IO_SIZE - 1;
  57. }
  58. pp->res[0].flags = IORESOURCE_IO;
  59. if (request_resource(&ioport_resource, &pp->res[0]))
  60. panic("Request PCIe IO resource failed\n");
  61. sys->resource[0] = &pp->res[0];
  62. /*
  63. * IORESOURCE_MEM
  64. */
  65. snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
  66. "PCIe %d MEM", pp->index);
  67. pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
  68. pp->res[1].name = pp->mem_space_name;
  69. if (pp->index == 0) {
  70. pp->res[1].start = DOVE_PCIE0_MEM_PHYS_BASE;
  71. pp->res[1].end = pp->res[1].start + DOVE_PCIE0_MEM_SIZE - 1;
  72. } else {
  73. pp->res[1].start = DOVE_PCIE1_MEM_PHYS_BASE;
  74. pp->res[1].end = pp->res[1].start + DOVE_PCIE1_MEM_SIZE - 1;
  75. }
  76. pp->res[1].flags = IORESOURCE_MEM;
  77. if (request_resource(&iomem_resource, &pp->res[1]))
  78. panic("Request PCIe Memory resource failed\n");
  79. sys->resource[1] = &pp->res[1];
  80. sys->resource[2] = NULL;
  81. return 1;
  82. }
  83. static struct pcie_port *bus_to_port(int bus)
  84. {
  85. int i;
  86. for (i = num_pcie_ports - 1; i >= 0; i--) {
  87. int rbus = pcie_port[i].root_bus_nr;
  88. if (rbus != -1 && rbus <= bus)
  89. break;
  90. }
  91. return i >= 0 ? pcie_port + i : NULL;
  92. }
  93. static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
  94. {
  95. /*
  96. * Don't go out when trying to access nonexisting devices
  97. * on the local bus.
  98. */
  99. if (bus == pp->root_bus_nr && dev > 1)
  100. return 0;
  101. return 1;
  102. }
  103. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  104. int size, u32 *val)
  105. {
  106. struct pcie_port *pp = bus_to_port(bus->number);
  107. unsigned long flags;
  108. int ret;
  109. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
  110. *val = 0xffffffff;
  111. return PCIBIOS_DEVICE_NOT_FOUND;
  112. }
  113. spin_lock_irqsave(&pp->conf_lock, flags);
  114. ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
  115. spin_unlock_irqrestore(&pp->conf_lock, flags);
  116. return ret;
  117. }
  118. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  119. int where, int size, u32 val)
  120. {
  121. struct pcie_port *pp = bus_to_port(bus->number);
  122. unsigned long flags;
  123. int ret;
  124. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
  125. return PCIBIOS_DEVICE_NOT_FOUND;
  126. spin_lock_irqsave(&pp->conf_lock, flags);
  127. ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
  128. spin_unlock_irqrestore(&pp->conf_lock, flags);
  129. return ret;
  130. }
  131. static struct pci_ops pcie_ops = {
  132. .read = pcie_rd_conf,
  133. .write = pcie_wr_conf,
  134. };
  135. static void __devinit rc_pci_fixup(struct pci_dev *dev)
  136. {
  137. /*
  138. * Prevent enumeration of root complex.
  139. */
  140. if (dev->bus->parent == NULL && dev->devfn == 0) {
  141. int i;
  142. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  143. dev->resource[i].start = 0;
  144. dev->resource[i].end = 0;
  145. dev->resource[i].flags = 0;
  146. }
  147. }
  148. }
  149. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  150. static struct pci_bus __init *
  151. dove_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  152. {
  153. struct pci_bus *bus;
  154. if (nr < num_pcie_ports) {
  155. bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
  156. } else {
  157. bus = NULL;
  158. BUG();
  159. }
  160. return bus;
  161. }
  162. static int __init dove_pcie_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  163. {
  164. struct pcie_port *pp = bus_to_port(dev->bus->number);
  165. return pp->index ? IRQ_DOVE_PCIE1 : IRQ_DOVE_PCIE0;
  166. }
  167. static struct hw_pci dove_pci __initdata = {
  168. .nr_controllers = 2,
  169. .swizzle = pci_std_swizzle,
  170. .setup = dove_pcie_setup,
  171. .scan = dove_pcie_scan_bus,
  172. .map_irq = dove_pcie_map_irq,
  173. };
  174. static void __init add_pcie_port(int index, unsigned long base)
  175. {
  176. printk(KERN_INFO "Dove PCIe port %d: ", index);
  177. if (orion_pcie_link_up((void __iomem *)base)) {
  178. struct pcie_port *pp = &pcie_port[num_pcie_ports++];
  179. printk(KERN_INFO "link up\n");
  180. pp->index = index;
  181. pp->root_bus_nr = -1;
  182. pp->base = (void __iomem *)base;
  183. spin_lock_init(&pp->conf_lock);
  184. memset(pp->res, 0, sizeof(pp->res));
  185. } else {
  186. printk(KERN_INFO "link down, ignoring\n");
  187. }
  188. }
  189. void __init dove_pcie_init(int init_port0, int init_port1)
  190. {
  191. if (init_port0)
  192. add_pcie_port(0, DOVE_PCIE0_VIRT_BASE);
  193. if (init_port1)
  194. add_pcie_port(1, DOVE_PCIE1_VIRT_BASE);
  195. pci_common_init(&dove_pci);
  196. }