pcie.c 6.8 KB

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