pcie.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <video/vga.h>
  13. #include <asm/irq.h>
  14. #include <asm/mach/pci.h>
  15. #include <plat/pcie.h>
  16. #include <plat/addr-map.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. int win = 0;
  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. mv78xx0_setup_pcie_mem_win(win + i + 8, pp->res.start,
  76. resource_size(&pp->res),
  77. pp->maj, pp->min);
  78. mv78xx0_setup_pcie_io_win(win + i, i * SZ_64K, SZ_64K,
  79. pp->maj, pp->min);
  80. }
  81. }
  82. static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
  83. {
  84. struct pcie_port *pp;
  85. if (nr >= num_pcie_ports)
  86. return 0;
  87. pp = &pcie_port[nr];
  88. sys->private_data = pp;
  89. pp->root_bus_nr = sys->busnr;
  90. /*
  91. * Generic PCIe unit setup.
  92. */
  93. orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
  94. orion_pcie_setup(pp->base);
  95. pci_ioremap_io(nr * SZ_64K, MV78XX0_PCIE_IO_PHYS_BASE(nr));
  96. pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
  97. return 1;
  98. }
  99. static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
  100. {
  101. /*
  102. * Don't go out when trying to access nonexisting devices
  103. * on the local bus.
  104. */
  105. if (bus == pp->root_bus_nr && dev > 1)
  106. return 0;
  107. return 1;
  108. }
  109. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  110. int size, u32 *val)
  111. {
  112. struct pci_sys_data *sys = bus->sysdata;
  113. struct pcie_port *pp = sys->private_data;
  114. unsigned long flags;
  115. int ret;
  116. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
  117. *val = 0xffffffff;
  118. return PCIBIOS_DEVICE_NOT_FOUND;
  119. }
  120. spin_lock_irqsave(&pp->conf_lock, flags);
  121. ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
  122. spin_unlock_irqrestore(&pp->conf_lock, flags);
  123. return ret;
  124. }
  125. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  126. int where, int size, u32 val)
  127. {
  128. struct pci_sys_data *sys = bus->sysdata;
  129. struct pcie_port *pp = sys->private_data;
  130. unsigned long flags;
  131. int ret;
  132. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
  133. return PCIBIOS_DEVICE_NOT_FOUND;
  134. spin_lock_irqsave(&pp->conf_lock, flags);
  135. ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
  136. spin_unlock_irqrestore(&pp->conf_lock, flags);
  137. return ret;
  138. }
  139. static struct pci_ops pcie_ops = {
  140. .read = pcie_rd_conf,
  141. .write = pcie_wr_conf,
  142. };
  143. static void rc_pci_fixup(struct pci_dev *dev)
  144. {
  145. /*
  146. * Prevent enumeration of root complex.
  147. */
  148. if (dev->bus->parent == NULL && dev->devfn == 0) {
  149. int i;
  150. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  151. dev->resource[i].start = 0;
  152. dev->resource[i].end = 0;
  153. dev->resource[i].flags = 0;
  154. }
  155. }
  156. }
  157. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  158. static struct pci_bus __init *
  159. mv78xx0_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  160. {
  161. struct pci_bus *bus;
  162. if (nr < num_pcie_ports) {
  163. bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys,
  164. &sys->resources);
  165. } else {
  166. bus = NULL;
  167. BUG();
  168. }
  169. return bus;
  170. }
  171. static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
  172. u8 pin)
  173. {
  174. struct pci_sys_data *sys = dev->bus->sysdata;
  175. struct pcie_port *pp = sys->private_data;
  176. return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
  177. }
  178. static struct hw_pci mv78xx0_pci __initdata = {
  179. .nr_controllers = 8,
  180. .preinit = mv78xx0_pcie_preinit,
  181. .setup = mv78xx0_pcie_setup,
  182. .scan = mv78xx0_pcie_scan_bus,
  183. .map_irq = mv78xx0_pcie_map_irq,
  184. };
  185. static void __init add_pcie_port(int maj, int min, void __iomem *base)
  186. {
  187. printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
  188. if (orion_pcie_link_up(base)) {
  189. struct pcie_port *pp = &pcie_port[num_pcie_ports++];
  190. printk("link up\n");
  191. pp->maj = maj;
  192. pp->min = min;
  193. pp->root_bus_nr = -1;
  194. pp->base = base;
  195. spin_lock_init(&pp->conf_lock);
  196. memset(&pp->res, 0, sizeof(pp->res));
  197. } else {
  198. printk("link down, ignoring\n");
  199. }
  200. }
  201. void __init mv78xx0_pcie_init(int init_port0, int init_port1)
  202. {
  203. vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
  204. if (init_port0) {
  205. add_pcie_port(0, 0, PCIE00_VIRT_BASE);
  206. if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) {
  207. add_pcie_port(0, 1, PCIE01_VIRT_BASE);
  208. add_pcie_port(0, 2, PCIE02_VIRT_BASE);
  209. add_pcie_port(0, 3, PCIE03_VIRT_BASE);
  210. }
  211. }
  212. if (init_port1) {
  213. add_pcie_port(1, 0, PCIE10_VIRT_BASE);
  214. if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
  215. add_pcie_port(1, 1, PCIE11_VIRT_BASE);
  216. add_pcie_port(1, 2, PCIE12_VIRT_BASE);
  217. add_pcie_port(1, 3, PCIE13_VIRT_BASE);
  218. }
  219. }
  220. pci_common_init(&mv78xx0_pci);
  221. }