pcie.c 7.2 KB

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