pcie.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/clk.h>
  14. #include <linux/mbus.h>
  15. #include <video/vga.h>
  16. #include <asm/irq.h>
  17. #include <asm/mach/pci.h>
  18. #include <plat/pcie.h>
  19. #include <mach/bridge-regs.h>
  20. #include "common.h"
  21. /* These can go away once Kirkwood uses the mvebu-mbus DT binding */
  22. #define KIRKWOOD_MBUS_PCIE0_MEM_TARGET 0x4
  23. #define KIRKWOOD_MBUS_PCIE0_MEM_ATTR 0xe8
  24. #define KIRKWOOD_MBUS_PCIE0_IO_TARGET 0x4
  25. #define KIRKWOOD_MBUS_PCIE0_IO_ATTR 0xe0
  26. #define KIRKWOOD_MBUS_PCIE1_MEM_TARGET 0x4
  27. #define KIRKWOOD_MBUS_PCIE1_MEM_ATTR 0xd8
  28. #define KIRKWOOD_MBUS_PCIE1_IO_TARGET 0x4
  29. #define KIRKWOOD_MBUS_PCIE1_IO_ATTR 0xd0
  30. static void kirkwood_enable_pcie_clk(const char *port)
  31. {
  32. struct clk *clk;
  33. clk = clk_get_sys("pcie", port);
  34. if (IS_ERR(clk)) {
  35. pr_err("PCIE clock %s missing\n", port);
  36. return;
  37. }
  38. clk_prepare_enable(clk);
  39. clk_put(clk);
  40. }
  41. /* This function is called very early in the boot when probing the
  42. hardware to determine what we actually are, and what rate tclk is
  43. ticking at. Hence calling kirkwood_enable_pcie_clk() is not
  44. possible since the clk tree has not been created yet. */
  45. void kirkwood_enable_pcie(void)
  46. {
  47. u32 curr = readl(CLOCK_GATING_CTRL);
  48. if (!(curr & CGC_PEX0))
  49. writel(curr | CGC_PEX0, CLOCK_GATING_CTRL);
  50. }
  51. void kirkwood_pcie_id(u32 *dev, u32 *rev)
  52. {
  53. kirkwood_enable_pcie();
  54. *dev = orion_pcie_dev_id(PCIE_VIRT_BASE);
  55. *rev = orion_pcie_rev(PCIE_VIRT_BASE);
  56. }
  57. struct pcie_port {
  58. u8 root_bus_nr;
  59. void __iomem *base;
  60. spinlock_t conf_lock;
  61. int irq;
  62. struct resource res;
  63. };
  64. static int pcie_port_map[2];
  65. static int num_pcie_ports;
  66. static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
  67. {
  68. /*
  69. * Don't go out when trying to access --
  70. * 1. nonexisting device on local bus
  71. * 2. where there's no device connected (no link)
  72. */
  73. if (bus == pp->root_bus_nr && dev == 0)
  74. return 1;
  75. if (!orion_pcie_link_up(pp->base))
  76. return 0;
  77. if (bus == pp->root_bus_nr && dev != 1)
  78. return 0;
  79. return 1;
  80. }
  81. /*
  82. * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
  83. * and then reading the PCIE_CONF_DATA register. Need to make sure these
  84. * transactions are atomic.
  85. */
  86. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  87. int size, u32 *val)
  88. {
  89. struct pci_sys_data *sys = bus->sysdata;
  90. struct pcie_port *pp = sys->private_data;
  91. unsigned long flags;
  92. int ret;
  93. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
  94. *val = 0xffffffff;
  95. return PCIBIOS_DEVICE_NOT_FOUND;
  96. }
  97. spin_lock_irqsave(&pp->conf_lock, flags);
  98. ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
  99. spin_unlock_irqrestore(&pp->conf_lock, flags);
  100. return ret;
  101. }
  102. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  103. int where, int size, u32 val)
  104. {
  105. struct pci_sys_data *sys = bus->sysdata;
  106. struct pcie_port *pp = sys->private_data;
  107. unsigned long flags;
  108. int ret;
  109. if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
  110. return PCIBIOS_DEVICE_NOT_FOUND;
  111. spin_lock_irqsave(&pp->conf_lock, flags);
  112. ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
  113. spin_unlock_irqrestore(&pp->conf_lock, flags);
  114. return ret;
  115. }
  116. static struct pci_ops pcie_ops = {
  117. .read = pcie_rd_conf,
  118. .write = pcie_wr_conf,
  119. };
  120. static void __init pcie0_ioresources_init(struct pcie_port *pp)
  121. {
  122. pp->base = PCIE_VIRT_BASE;
  123. pp->irq = IRQ_KIRKWOOD_PCIE;
  124. /*
  125. * IORESOURCE_MEM
  126. */
  127. pp->res.name = "PCIe 0 MEM";
  128. pp->res.start = KIRKWOOD_PCIE_MEM_PHYS_BASE;
  129. pp->res.end = pp->res.start + KIRKWOOD_PCIE_MEM_SIZE - 1;
  130. pp->res.flags = IORESOURCE_MEM;
  131. }
  132. static void __init pcie1_ioresources_init(struct pcie_port *pp)
  133. {
  134. pp->base = PCIE1_VIRT_BASE;
  135. pp->irq = IRQ_KIRKWOOD_PCIE1;
  136. /*
  137. * IORESOURCE_MEM
  138. */
  139. pp->res.name = "PCIe 1 MEM";
  140. pp->res.start = KIRKWOOD_PCIE1_MEM_PHYS_BASE;
  141. pp->res.end = pp->res.start + KIRKWOOD_PCIE1_MEM_SIZE - 1;
  142. pp->res.flags = IORESOURCE_MEM;
  143. }
  144. static int __init kirkwood_pcie_setup(int nr, struct pci_sys_data *sys)
  145. {
  146. struct pcie_port *pp;
  147. int index;
  148. if (nr >= num_pcie_ports)
  149. return 0;
  150. index = pcie_port_map[nr];
  151. pr_info("PCI: bus%d uses PCIe port %d\n", sys->busnr, index);
  152. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  153. if (!pp)
  154. panic("PCIe: failed to allocate pcie_port data");
  155. sys->private_data = pp;
  156. pp->root_bus_nr = sys->busnr;
  157. spin_lock_init(&pp->conf_lock);
  158. switch (index) {
  159. case 0:
  160. kirkwood_enable_pcie_clk("0");
  161. pcie0_ioresources_init(pp);
  162. pci_ioremap_io(SZ_64K * sys->busnr, KIRKWOOD_PCIE_IO_PHYS_BASE);
  163. break;
  164. case 1:
  165. kirkwood_enable_pcie_clk("1");
  166. pcie1_ioresources_init(pp);
  167. pci_ioremap_io(SZ_64K * sys->busnr,
  168. KIRKWOOD_PCIE1_IO_PHYS_BASE);
  169. break;
  170. default:
  171. panic("PCIe setup: invalid controller %d", index);
  172. }
  173. if (request_resource(&iomem_resource, &pp->res))
  174. panic("Request PCIe%d Memory resource failed\n", index);
  175. pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
  176. /*
  177. * Generic PCIe unit setup.
  178. */
  179. orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
  180. orion_pcie_setup(pp->base);
  181. return 1;
  182. }
  183. /*
  184. * The root complex has a hardwired class of PCI_CLASS_MEMORY_OTHER, when it
  185. * is operating as a root complex this needs to be switched to
  186. * PCI_CLASS_BRIDGE_HOST or Linux will errantly try to process the BAR's on
  187. * the device. Decoding setup is handled by the orion code.
  188. */
  189. static void rc_pci_fixup(struct pci_dev *dev)
  190. {
  191. if (dev->bus->parent == NULL && dev->devfn == 0) {
  192. int i;
  193. dev->class &= 0xff;
  194. dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
  195. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  196. dev->resource[i].start = 0;
  197. dev->resource[i].end = 0;
  198. dev->resource[i].flags = 0;
  199. }
  200. }
  201. }
  202. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  203. static int __init kirkwood_pcie_map_irq(const struct pci_dev *dev, u8 slot,
  204. u8 pin)
  205. {
  206. struct pci_sys_data *sys = dev->sysdata;
  207. struct pcie_port *pp = sys->private_data;
  208. return pp->irq;
  209. }
  210. static struct hw_pci kirkwood_pci __initdata = {
  211. .setup = kirkwood_pcie_setup,
  212. .map_irq = kirkwood_pcie_map_irq,
  213. .ops = &pcie_ops,
  214. };
  215. static void __init add_pcie_port(int index, void __iomem *base)
  216. {
  217. pcie_port_map[num_pcie_ports++] = index;
  218. pr_info("Kirkwood PCIe port %d: link %s\n", index,
  219. orion_pcie_link_up(base) ? "up" : "down");
  220. }
  221. void __init kirkwood_pcie_init(unsigned int portmask)
  222. {
  223. mvebu_mbus_add_window_remap_by_id(KIRKWOOD_MBUS_PCIE0_IO_TARGET,
  224. KIRKWOOD_MBUS_PCIE0_IO_ATTR,
  225. KIRKWOOD_PCIE_IO_PHYS_BASE,
  226. KIRKWOOD_PCIE_IO_SIZE,
  227. KIRKWOOD_PCIE_IO_BUS_BASE);
  228. mvebu_mbus_add_window_by_id(KIRKWOOD_MBUS_PCIE0_MEM_TARGET,
  229. KIRKWOOD_MBUS_PCIE0_MEM_ATTR,
  230. KIRKWOOD_PCIE_MEM_PHYS_BASE,
  231. KIRKWOOD_PCIE_MEM_SIZE);
  232. mvebu_mbus_add_window_remap_by_id(KIRKWOOD_MBUS_PCIE1_IO_TARGET,
  233. KIRKWOOD_MBUS_PCIE1_IO_ATTR,
  234. KIRKWOOD_PCIE1_IO_PHYS_BASE,
  235. KIRKWOOD_PCIE1_IO_SIZE,
  236. KIRKWOOD_PCIE1_IO_BUS_BASE);
  237. mvebu_mbus_add_window_by_id(KIRKWOOD_MBUS_PCIE1_MEM_TARGET,
  238. KIRKWOOD_MBUS_PCIE1_MEM_ATTR,
  239. KIRKWOOD_PCIE1_MEM_PHYS_BASE,
  240. KIRKWOOD_PCIE1_MEM_SIZE);
  241. vga_base = KIRKWOOD_PCIE_MEM_PHYS_BASE;
  242. if (portmask & KW_PCIE0)
  243. add_pcie_port(0, PCIE_VIRT_BASE);
  244. if (portmask & KW_PCIE1)
  245. add_pcie_port(1, PCIE1_VIRT_BASE);
  246. kirkwood_pci.nr_controllers = num_pcie_ports;
  247. pci_common_init(&kirkwood_pci);
  248. }