pcie.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * arch/arm/mach-kirkwood/pcie.c
  3. *
  4. * PCIe functions for Marvell Kirkwood 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/slab.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/bridge-regs.h>
  18. #include <plat/addr-map.h>
  19. #include "common.h"
  20. void kirkwood_enable_pcie(void)
  21. {
  22. u32 curr = readl(CLOCK_GATING_CTRL);
  23. if (!(curr & CGC_PEX0))
  24. writel(curr | CGC_PEX0, CLOCK_GATING_CTRL);
  25. }
  26. void __init kirkwood_pcie_id(u32 *dev, u32 *rev)
  27. {
  28. kirkwood_enable_pcie();
  29. *dev = orion_pcie_dev_id((void __iomem *)PCIE_VIRT_BASE);
  30. *rev = orion_pcie_rev((void __iomem *)PCIE_VIRT_BASE);
  31. }
  32. struct pcie_port {
  33. u8 root_bus_nr;
  34. void __iomem *base;
  35. spinlock_t conf_lock;
  36. int irq;
  37. struct resource res[2];
  38. };
  39. static int pcie_port_map[2];
  40. static int num_pcie_ports;
  41. static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
  42. {
  43. /*
  44. * Don't go out when trying to access --
  45. * 1. nonexisting device on local bus
  46. * 2. where there's no device connected (no link)
  47. */
  48. if (bus == pp->root_bus_nr && dev == 0)
  49. return 1;
  50. if (!orion_pcie_link_up(pp->base))
  51. return 0;
  52. if (bus == pp->root_bus_nr && dev != 1)
  53. return 0;
  54. return 1;
  55. }
  56. /*
  57. * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
  58. * and then reading the PCIE_CONF_DATA register. Need to make sure these
  59. * transactions are atomic.
  60. */
  61. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  62. int size, u32 *val)
  63. {
  64. struct pci_sys_data *sys = bus->sysdata;
  65. struct pcie_port *pp = sys->private_data;
  66. unsigned long flags;
  67. int ret;
  68. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
  69. *val = 0xffffffff;
  70. return PCIBIOS_DEVICE_NOT_FOUND;
  71. }
  72. spin_lock_irqsave(&pp->conf_lock, flags);
  73. ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
  74. spin_unlock_irqrestore(&pp->conf_lock, flags);
  75. return ret;
  76. }
  77. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  78. int where, int size, u32 val)
  79. {
  80. struct pci_sys_data *sys = bus->sysdata;
  81. struct pcie_port *pp = sys->private_data;
  82. unsigned long flags;
  83. int ret;
  84. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
  85. return PCIBIOS_DEVICE_NOT_FOUND;
  86. spin_lock_irqsave(&pp->conf_lock, flags);
  87. ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
  88. spin_unlock_irqrestore(&pp->conf_lock, flags);
  89. return ret;
  90. }
  91. static struct pci_ops pcie_ops = {
  92. .read = pcie_rd_conf,
  93. .write = pcie_wr_conf,
  94. };
  95. static void __init pcie0_ioresources_init(struct pcie_port *pp)
  96. {
  97. pp->base = (void __iomem *)PCIE_VIRT_BASE;
  98. pp->irq = IRQ_KIRKWOOD_PCIE;
  99. /*
  100. * IORESOURCE_IO
  101. */
  102. pp->res[0].name = "PCIe 0 I/O Space";
  103. pp->res[0].start = KIRKWOOD_PCIE_IO_BUS_BASE;
  104. pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE_IO_SIZE - 1;
  105. pp->res[0].flags = IORESOURCE_IO;
  106. /*
  107. * IORESOURCE_MEM
  108. */
  109. pp->res[1].name = "PCIe 0 MEM";
  110. pp->res[1].start = KIRKWOOD_PCIE_MEM_PHYS_BASE;
  111. pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE_MEM_SIZE - 1;
  112. pp->res[1].flags = IORESOURCE_MEM;
  113. }
  114. static void __init pcie1_ioresources_init(struct pcie_port *pp)
  115. {
  116. pp->base = (void __iomem *)PCIE1_VIRT_BASE;
  117. pp->irq = IRQ_KIRKWOOD_PCIE1;
  118. /*
  119. * IORESOURCE_IO
  120. */
  121. pp->res[0].name = "PCIe 1 I/O Space";
  122. pp->res[0].start = KIRKWOOD_PCIE1_IO_BUS_BASE;
  123. pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE1_IO_SIZE - 1;
  124. pp->res[0].flags = IORESOURCE_IO;
  125. /*
  126. * IORESOURCE_MEM
  127. */
  128. pp->res[1].name = "PCIe 1 MEM";
  129. pp->res[1].start = KIRKWOOD_PCIE1_MEM_PHYS_BASE;
  130. pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE1_MEM_SIZE - 1;
  131. pp->res[1].flags = IORESOURCE_MEM;
  132. }
  133. static int __init kirkwood_pcie_setup(int nr, struct pci_sys_data *sys)
  134. {
  135. extern unsigned int kirkwood_clk_ctrl;
  136. struct pcie_port *pp;
  137. int index;
  138. if (nr >= num_pcie_ports)
  139. return 0;
  140. index = pcie_port_map[nr];
  141. printk(KERN_INFO "PCI: bus%d uses PCIe port %d\n", sys->busnr, index);
  142. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  143. if (!pp)
  144. panic("PCIe: failed to allocate pcie_port data");
  145. sys->private_data = pp;
  146. pp->root_bus_nr = sys->busnr;
  147. spin_lock_init(&pp->conf_lock);
  148. switch (index) {
  149. case 0:
  150. kirkwood_clk_ctrl |= CGC_PEX0;
  151. pcie0_ioresources_init(pp);
  152. break;
  153. case 1:
  154. kirkwood_clk_ctrl |= CGC_PEX1;
  155. pcie1_ioresources_init(pp);
  156. break;
  157. default:
  158. panic("PCIe setup: invalid controller %d", index);
  159. }
  160. if (request_resource(&ioport_resource, &pp->res[0]))
  161. panic("Request PCIe%d IO resource failed\n", index);
  162. if (request_resource(&iomem_resource, &pp->res[1]))
  163. panic("Request PCIe%d Memory resource failed\n", index);
  164. sys->io_offset = 0;
  165. pci_add_resource_offset(&sys->resources, &pp->res[0], sys->io_offset);
  166. pci_add_resource_offset(&sys->resources, &pp->res[1], sys->mem_offset);
  167. /*
  168. * Generic PCIe unit setup.
  169. */
  170. orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
  171. orion_pcie_setup(pp->base);
  172. return 1;
  173. }
  174. static void __devinit rc_pci_fixup(struct pci_dev *dev)
  175. {
  176. /*
  177. * Prevent enumeration of root complex.
  178. */
  179. if (dev->bus->parent == NULL && dev->devfn == 0) {
  180. int i;
  181. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  182. dev->resource[i].start = 0;
  183. dev->resource[i].end = 0;
  184. dev->resource[i].flags = 0;
  185. }
  186. }
  187. }
  188. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  189. static struct pci_bus __init *
  190. kirkwood_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  191. {
  192. struct pci_bus *bus;
  193. if (nr < num_pcie_ports) {
  194. bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys,
  195. &sys->resources);
  196. } else {
  197. bus = NULL;
  198. BUG();
  199. }
  200. return bus;
  201. }
  202. static int __init kirkwood_pcie_map_irq(const struct pci_dev *dev, u8 slot,
  203. u8 pin)
  204. {
  205. struct pci_sys_data *sys = dev->sysdata;
  206. struct pcie_port *pp = sys->private_data;
  207. return pp->irq;
  208. }
  209. static struct hw_pci kirkwood_pci __initdata = {
  210. .setup = kirkwood_pcie_setup,
  211. .scan = kirkwood_pcie_scan_bus,
  212. .map_irq = kirkwood_pcie_map_irq,
  213. };
  214. static void __init add_pcie_port(int index, unsigned long base)
  215. {
  216. printk(KERN_INFO "Kirkwood PCIe port %d: ", index);
  217. if (orion_pcie_link_up((void __iomem *)base)) {
  218. printk(KERN_INFO "link up\n");
  219. pcie_port_map[num_pcie_ports++] = index;
  220. } else
  221. printk(KERN_INFO "link down, ignoring\n");
  222. }
  223. void __init kirkwood_pcie_init(unsigned int portmask)
  224. {
  225. vga_base = KIRKWOOD_PCIE_MEM_PHYS_BASE;
  226. if (portmask & KW_PCIE0)
  227. add_pcie_port(0, PCIE_VIRT_BASE);
  228. if (portmask & KW_PCIE1)
  229. add_pcie_port(1, PCIE1_VIRT_BASE);
  230. kirkwood_pci.nr_controllers = num_pcie_ports;
  231. pci_common_init(&kirkwood_pci);
  232. }