pcie.c 6.5 KB

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