pcie.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/clk.h>
  13. #include <video/vga.h>
  14. #include <asm/mach/pci.h>
  15. #include <asm/mach/arch.h>
  16. #include <asm/setup.h>
  17. #include <asm/delay.h>
  18. #include <plat/pcie.h>
  19. #include <mach/irqs.h>
  20. #include <mach/bridge-regs.h>
  21. #include <plat/addr-map.h>
  22. #include "common.h"
  23. struct pcie_port {
  24. u8 index;
  25. u8 root_bus_nr;
  26. void __iomem *base;
  27. spinlock_t conf_lock;
  28. char mem_space_name[16];
  29. struct resource res;
  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. if (pp->index == 0)
  47. pci_ioremap_io(sys->busnr * SZ_64K, DOVE_PCIE0_IO_PHYS_BASE);
  48. else
  49. pci_ioremap_io(sys->busnr * SZ_64K, DOVE_PCIE1_IO_PHYS_BASE);
  50. /*
  51. * IORESOURCE_MEM
  52. */
  53. snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
  54. "PCIe %d MEM", pp->index);
  55. pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
  56. pp->res.name = pp->mem_space_name;
  57. if (pp->index == 0) {
  58. pp->res.start = DOVE_PCIE0_MEM_PHYS_BASE;
  59. pp->res.end = pp->res.start + DOVE_PCIE0_MEM_SIZE - 1;
  60. } else {
  61. pp->res.start = DOVE_PCIE1_MEM_PHYS_BASE;
  62. pp->res.end = pp->res.start + DOVE_PCIE1_MEM_SIZE - 1;
  63. }
  64. pp->res.flags = IORESOURCE_MEM;
  65. if (request_resource(&iomem_resource, &pp->res))
  66. panic("Request PCIe Memory resource failed\n");
  67. pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
  68. return 1;
  69. }
  70. static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
  71. {
  72. /*
  73. * Don't go out when trying to access nonexisting devices
  74. * on the local bus.
  75. */
  76. if (bus == pp->root_bus_nr && dev > 1)
  77. return 0;
  78. return 1;
  79. }
  80. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  81. int size, u32 *val)
  82. {
  83. struct pci_sys_data *sys = bus->sysdata;
  84. struct pcie_port *pp = sys->private_data;
  85. unsigned long flags;
  86. int ret;
  87. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
  88. *val = 0xffffffff;
  89. return PCIBIOS_DEVICE_NOT_FOUND;
  90. }
  91. spin_lock_irqsave(&pp->conf_lock, flags);
  92. ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
  93. spin_unlock_irqrestore(&pp->conf_lock, flags);
  94. return ret;
  95. }
  96. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  97. int where, int size, u32 val)
  98. {
  99. struct pci_sys_data *sys = bus->sysdata;
  100. struct pcie_port *pp = sys->private_data;
  101. unsigned long flags;
  102. int ret;
  103. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. spin_lock_irqsave(&pp->conf_lock, flags);
  106. ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
  107. spin_unlock_irqrestore(&pp->conf_lock, flags);
  108. return ret;
  109. }
  110. static struct pci_ops pcie_ops = {
  111. .read = pcie_rd_conf,
  112. .write = pcie_wr_conf,
  113. };
  114. static void 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. dove_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  131. {
  132. if (nr >= num_pcie_ports) {
  133. BUG();
  134. return NULL;
  135. }
  136. return pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys,
  137. &sys->resources);
  138. }
  139. static int __init dove_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  140. {
  141. struct pci_sys_data *sys = dev->sysdata;
  142. struct pcie_port *pp = sys->private_data;
  143. return pp->index ? IRQ_DOVE_PCIE1 : IRQ_DOVE_PCIE0;
  144. }
  145. static struct hw_pci dove_pci __initdata = {
  146. .nr_controllers = 2,
  147. .setup = dove_pcie_setup,
  148. .scan = dove_pcie_scan_bus,
  149. .map_irq = dove_pcie_map_irq,
  150. };
  151. static void __init add_pcie_port(int index, void __iomem *base)
  152. {
  153. printk(KERN_INFO "Dove PCIe port %d: ", index);
  154. if (orion_pcie_link_up(base)) {
  155. struct pcie_port *pp = &pcie_port[num_pcie_ports++];
  156. struct clk *clk = clk_get_sys("pcie", (index ? "1" : "0"));
  157. if (!IS_ERR(clk))
  158. clk_prepare_enable(clk);
  159. printk(KERN_INFO "link up\n");
  160. pp->index = index;
  161. pp->root_bus_nr = -1;
  162. pp->base = base;
  163. spin_lock_init(&pp->conf_lock);
  164. memset(&pp->res, 0, sizeof(pp->res));
  165. } else {
  166. printk(KERN_INFO "link down, ignoring\n");
  167. }
  168. }
  169. void __init dove_pcie_init(int init_port0, int init_port1)
  170. {
  171. vga_base = DOVE_PCIE0_MEM_PHYS_BASE;
  172. if (init_port0)
  173. add_pcie_port(0, DOVE_PCIE0_VIRT_BASE);
  174. if (init_port1)
  175. add_pcie_port(1, DOVE_PCIE1_VIRT_BASE);
  176. pci_common_init(&dove_pci);
  177. }