pcie.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm/mach-mv78xx0/pcie.c
  4. *
  5. * PCIe functions for Marvell MV78xx0 SoCs
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/pci.h>
  9. #include <linux/mbus.h>
  10. #include <video/vga.h>
  11. #include <asm/irq.h>
  12. #include <asm/mach/pci.h>
  13. #include <plat/pcie.h>
  14. #include "mv78xx0.h"
  15. #include "common.h"
  16. #define MV78XX0_MBUS_PCIE_MEM_TARGET(port, lane) ((port) ? 8 : 4)
  17. #define MV78XX0_MBUS_PCIE_MEM_ATTR(port, lane) (0xf8 & ~(0x10 << (lane)))
  18. #define MV78XX0_MBUS_PCIE_IO_TARGET(port, lane) ((port) ? 8 : 4)
  19. #define MV78XX0_MBUS_PCIE_IO_ATTR(port, lane) (0xf0 & ~(0x10 << (lane)))
  20. struct pcie_port {
  21. u8 maj;
  22. u8 min;
  23. u8 root_bus_nr;
  24. void __iomem *base;
  25. spinlock_t conf_lock;
  26. char mem_space_name[20];
  27. struct resource res;
  28. };
  29. static struct pcie_port pcie_port[8];
  30. static int num_pcie_ports;
  31. static struct resource pcie_io_space;
  32. void __init mv78xx0_pcie_id(u32 *dev, u32 *rev)
  33. {
  34. *dev = orion_pcie_dev_id(PCIE00_VIRT_BASE);
  35. *rev = orion_pcie_rev(PCIE00_VIRT_BASE);
  36. }
  37. u32 pcie_port_size[8] = {
  38. 0,
  39. 0x30000000,
  40. 0x10000000,
  41. 0x10000000,
  42. 0x08000000,
  43. 0x08000000,
  44. 0x08000000,
  45. 0x04000000,
  46. };
  47. static void __init mv78xx0_pcie_preinit(void)
  48. {
  49. int i;
  50. u32 size_each;
  51. u32 start;
  52. pcie_io_space.name = "PCIe I/O Space";
  53. pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0);
  54. pcie_io_space.end =
  55. MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1;
  56. pcie_io_space.flags = IORESOURCE_MEM;
  57. if (request_resource(&iomem_resource, &pcie_io_space))
  58. panic("can't allocate PCIe I/O space");
  59. if (num_pcie_ports > 7)
  60. panic("invalid number of PCIe ports");
  61. size_each = pcie_port_size[num_pcie_ports];
  62. start = MV78XX0_PCIE_MEM_PHYS_BASE;
  63. for (i = 0; i < num_pcie_ports; i++) {
  64. struct pcie_port *pp = pcie_port + i;
  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. mvebu_mbus_add_window_by_id(MV78XX0_MBUS_PCIE_MEM_TARGET(pp->maj, pp->min),
  76. MV78XX0_MBUS_PCIE_MEM_ATTR(pp->maj, pp->min),
  77. pp->res.start, resource_size(&pp->res));
  78. mvebu_mbus_add_window_remap_by_id(MV78XX0_MBUS_PCIE_IO_TARGET(pp->maj, pp->min),
  79. MV78XX0_MBUS_PCIE_IO_ATTR(pp->maj, pp->min),
  80. i * SZ_64K, SZ_64K, 0);
  81. }
  82. }
  83. static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
  84. {
  85. struct pcie_port *pp;
  86. struct resource realio;
  87. if (nr >= num_pcie_ports)
  88. return 0;
  89. pp = &pcie_port[nr];
  90. sys->private_data = pp;
  91. pp->root_bus_nr = sys->busnr;
  92. /*
  93. * Generic PCIe unit setup.
  94. */
  95. orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
  96. orion_pcie_setup(pp->base);
  97. realio.start = nr * SZ_64K;
  98. realio.end = realio.start + SZ_64K - 1;
  99. pci_remap_iospace(&realio, 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. /*
  148. * The root complex has a hardwired class of PCI_CLASS_MEMORY_OTHER, when it
  149. * is operating as a root complex this needs to be switched to
  150. * PCI_CLASS_BRIDGE_HOST or Linux will errantly try to process the BAR's on
  151. * the device. Decoding setup is handled by the orion code.
  152. */
  153. static void rc_pci_fixup(struct pci_dev *dev)
  154. {
  155. if (dev->bus->parent == NULL && dev->devfn == 0) {
  156. int i;
  157. dev->class &= 0xff;
  158. dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
  159. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  160. dev->resource[i].start = 0;
  161. dev->resource[i].end = 0;
  162. dev->resource[i].flags = 0;
  163. }
  164. }
  165. }
  166. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  167. static int __init mv78xx0_pcie_scan_bus(int nr, struct pci_host_bridge *bridge)
  168. {
  169. struct pci_sys_data *sys = pci_host_bridge_priv(bridge);
  170. if (nr >= num_pcie_ports) {
  171. BUG();
  172. return -EINVAL;
  173. }
  174. list_splice_init(&sys->resources, &bridge->windows);
  175. bridge->dev.parent = NULL;
  176. bridge->sysdata = sys;
  177. bridge->busnr = sys->busnr;
  178. bridge->ops = &pcie_ops;
  179. return pci_scan_root_bus_bridge(bridge);
  180. }
  181. static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
  182. u8 pin)
  183. {
  184. struct pci_sys_data *sys = dev->bus->sysdata;
  185. struct pcie_port *pp = sys->private_data;
  186. return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
  187. }
  188. static struct hw_pci mv78xx0_pci __initdata = {
  189. .nr_controllers = 8,
  190. .preinit = mv78xx0_pcie_preinit,
  191. .setup = mv78xx0_pcie_setup,
  192. .scan = mv78xx0_pcie_scan_bus,
  193. .map_irq = mv78xx0_pcie_map_irq,
  194. };
  195. static void __init add_pcie_port(int maj, int min, void __iomem *base)
  196. {
  197. printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
  198. if (orion_pcie_link_up(base)) {
  199. struct pcie_port *pp = &pcie_port[num_pcie_ports++];
  200. printk("link up\n");
  201. pp->maj = maj;
  202. pp->min = min;
  203. pp->root_bus_nr = -1;
  204. pp->base = base;
  205. spin_lock_init(&pp->conf_lock);
  206. memset(&pp->res, 0, sizeof(pp->res));
  207. } else {
  208. printk("link down, ignoring\n");
  209. }
  210. }
  211. void __init mv78xx0_pcie_init(int init_port0, int init_port1)
  212. {
  213. vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
  214. if (init_port0) {
  215. add_pcie_port(0, 0, PCIE00_VIRT_BASE);
  216. if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) {
  217. add_pcie_port(0, 1, PCIE01_VIRT_BASE);
  218. add_pcie_port(0, 2, PCIE02_VIRT_BASE);
  219. add_pcie_port(0, 3, PCIE03_VIRT_BASE);
  220. }
  221. }
  222. if (init_port1) {
  223. add_pcie_port(1, 0, PCIE10_VIRT_BASE);
  224. if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
  225. add_pcie_port(1, 1, PCIE11_VIRT_BASE);
  226. add_pcie_port(1, 2, PCIE12_VIRT_BASE);
  227. add_pcie_port(1, 3, PCIE13_VIRT_BASE);
  228. }
  229. }
  230. pci_common_init(&mv78xx0_pci);
  231. }