pcie.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * arch/arm/mach-mv78xx0/pcie.c
  3. *
  4. * PCIe functions for Marvell MV78xx0 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 <video/vga.h>
  14. #include <asm/irq.h>
  15. #include <asm/mach/pci.h>
  16. #include <plat/pcie.h>
  17. #include <mach/mv78xx0.h>
  18. #include "common.h"
  19. struct pcie_port {
  20. u8 maj;
  21. u8 min;
  22. u8 root_bus_nr;
  23. void __iomem *base;
  24. spinlock_t conf_lock;
  25. char mem_space_name[16];
  26. struct resource res;
  27. };
  28. static struct pcie_port pcie_port[8];
  29. static int num_pcie_ports;
  30. static struct resource pcie_io_space;
  31. void __init mv78xx0_pcie_id(u32 *dev, u32 *rev)
  32. {
  33. *dev = orion_pcie_dev_id(PCIE00_VIRT_BASE);
  34. *rev = orion_pcie_rev(PCIE00_VIRT_BASE);
  35. }
  36. u32 pcie_port_size[8] = {
  37. 0,
  38. 0x30000000,
  39. 0x10000000,
  40. 0x10000000,
  41. 0x08000000,
  42. 0x08000000,
  43. 0x08000000,
  44. 0x04000000,
  45. };
  46. static void __init mv78xx0_pcie_preinit(void)
  47. {
  48. int i;
  49. u32 size_each;
  50. u32 start;
  51. pcie_io_space.name = "PCIe I/O Space";
  52. pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0);
  53. pcie_io_space.end =
  54. MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1;
  55. pcie_io_space.flags = IORESOURCE_MEM;
  56. if (request_resource(&iomem_resource, &pcie_io_space))
  57. panic("can't allocate PCIe I/O space");
  58. if (num_pcie_ports > 7)
  59. panic("invalid number of PCIe ports");
  60. size_each = pcie_port_size[num_pcie_ports];
  61. start = MV78XX0_PCIE_MEM_PHYS_BASE;
  62. for (i = 0; i < num_pcie_ports; i++) {
  63. struct pcie_port *pp = pcie_port + i;
  64. char winname[MVEBU_MBUS_MAX_WINNAME_SZ];
  65. snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
  66. "PCIe %d.%d MEM", pp->maj, pp->min);
  67. pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
  68. pp->res.name = pp->mem_space_name;
  69. pp->res.flags = IORESOURCE_MEM;
  70. pp->res.start = start;
  71. pp->res.end = start + size_each - 1;
  72. start += size_each;
  73. if (request_resource(&iomem_resource, &pp->res))
  74. panic("can't allocate PCIe MEM sub-space");
  75. snprintf(winname, sizeof(winname), "pcie%d.%d",
  76. pp->maj, pp->min);
  77. mvebu_mbus_add_window_remap_flags(winname,
  78. pp->res.start,
  79. resource_size(&pp->res),
  80. MVEBU_MBUS_NO_REMAP,
  81. MVEBU_MBUS_PCI_MEM);
  82. mvebu_mbus_add_window_remap_flags(winname,
  83. i * SZ_64K, SZ_64K,
  84. 0, MVEBU_MBUS_PCI_IO);
  85. }
  86. }
  87. static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
  88. {
  89. struct pcie_port *pp;
  90. if (nr >= num_pcie_ports)
  91. return 0;
  92. pp = &pcie_port[nr];
  93. sys->private_data = pp;
  94. pp->root_bus_nr = sys->busnr;
  95. /*
  96. * Generic PCIe unit setup.
  97. */
  98. orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
  99. orion_pcie_setup(pp->base);
  100. pci_ioremap_io(nr * SZ_64K, MV78XX0_PCIE_IO_PHYS_BASE(nr));
  101. pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
  102. return 1;
  103. }
  104. static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
  105. {
  106. /*
  107. * Don't go out when trying to access nonexisting devices
  108. * on the local bus.
  109. */
  110. if (bus == pp->root_bus_nr && dev > 1)
  111. return 0;
  112. return 1;
  113. }
  114. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  115. int size, u32 *val)
  116. {
  117. struct pci_sys_data *sys = bus->sysdata;
  118. struct pcie_port *pp = sys->private_data;
  119. unsigned long flags;
  120. int ret;
  121. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
  122. *val = 0xffffffff;
  123. return PCIBIOS_DEVICE_NOT_FOUND;
  124. }
  125. spin_lock_irqsave(&pp->conf_lock, flags);
  126. ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
  127. spin_unlock_irqrestore(&pp->conf_lock, flags);
  128. return ret;
  129. }
  130. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  131. int where, int size, u32 val)
  132. {
  133. struct pci_sys_data *sys = bus->sysdata;
  134. struct pcie_port *pp = sys->private_data;
  135. unsigned long flags;
  136. int ret;
  137. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
  138. return PCIBIOS_DEVICE_NOT_FOUND;
  139. spin_lock_irqsave(&pp->conf_lock, flags);
  140. ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
  141. spin_unlock_irqrestore(&pp->conf_lock, flags);
  142. return ret;
  143. }
  144. static struct pci_ops pcie_ops = {
  145. .read = pcie_rd_conf,
  146. .write = pcie_wr_conf,
  147. };
  148. static void rc_pci_fixup(struct pci_dev *dev)
  149. {
  150. /*
  151. * Prevent enumeration of root complex.
  152. */
  153. if (dev->bus->parent == NULL && dev->devfn == 0) {
  154. int i;
  155. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  156. dev->resource[i].start = 0;
  157. dev->resource[i].end = 0;
  158. dev->resource[i].flags = 0;
  159. }
  160. }
  161. }
  162. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  163. static struct pci_bus __init *
  164. mv78xx0_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  165. {
  166. struct pci_bus *bus;
  167. if (nr < num_pcie_ports) {
  168. bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys,
  169. &sys->resources);
  170. } else {
  171. bus = NULL;
  172. BUG();
  173. }
  174. return bus;
  175. }
  176. static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
  177. u8 pin)
  178. {
  179. struct pci_sys_data *sys = dev->bus->sysdata;
  180. struct pcie_port *pp = sys->private_data;
  181. return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
  182. }
  183. static struct hw_pci mv78xx0_pci __initdata = {
  184. .nr_controllers = 8,
  185. .preinit = mv78xx0_pcie_preinit,
  186. .setup = mv78xx0_pcie_setup,
  187. .scan = mv78xx0_pcie_scan_bus,
  188. .map_irq = mv78xx0_pcie_map_irq,
  189. };
  190. static void __init add_pcie_port(int maj, int min, void __iomem *base)
  191. {
  192. printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
  193. if (orion_pcie_link_up(base)) {
  194. struct pcie_port *pp = &pcie_port[num_pcie_ports++];
  195. printk("link up\n");
  196. pp->maj = maj;
  197. pp->min = min;
  198. pp->root_bus_nr = -1;
  199. pp->base = base;
  200. spin_lock_init(&pp->conf_lock);
  201. memset(&pp->res, 0, sizeof(pp->res));
  202. } else {
  203. printk("link down, ignoring\n");
  204. }
  205. }
  206. void __init mv78xx0_pcie_init(int init_port0, int init_port1)
  207. {
  208. vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
  209. if (init_port0) {
  210. add_pcie_port(0, 0, PCIE00_VIRT_BASE);
  211. if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) {
  212. add_pcie_port(0, 1, PCIE01_VIRT_BASE);
  213. add_pcie_port(0, 2, PCIE02_VIRT_BASE);
  214. add_pcie_port(0, 3, PCIE03_VIRT_BASE);
  215. }
  216. }
  217. if (init_port1) {
  218. add_pcie_port(1, 0, PCIE10_VIRT_BASE);
  219. if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
  220. add_pcie_port(1, 1, PCIE11_VIRT_BASE);
  221. add_pcie_port(1, 2, PCIE12_VIRT_BASE);
  222. add_pcie_port(1, 3, PCIE13_VIRT_BASE);
  223. }
  224. }
  225. pci_common_init(&mv78xx0_pci);
  226. }