pcie.c 5.5 KB

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