pcie.c 6.5 KB

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