pcie.c 6.9 KB

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