pcie.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * PCI-E support for CNS3xxx
  3. *
  4. * Copyright 2008 Cavium Networks
  5. * Richard Liu <richard.liu@caviumnetworks.com>
  6. * Copyright 2010 MontaVista Software, LLC.
  7. * Anton Vorontsov <avorontsov@mvista.com>
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/bug.h>
  16. #include <linux/pci.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ptrace.h>
  21. #include <asm/mach/map.h>
  22. #include "cns3xxx.h"
  23. #include "core.h"
  24. struct cns3xxx_pcie {
  25. void __iomem *host_regs; /* PCI config registers for host bridge */
  26. void __iomem *cfg0_regs; /* PCI Type 0 config registers */
  27. void __iomem *cfg1_regs; /* PCI Type 1 config registers */
  28. unsigned int irqs[2];
  29. struct resource res_io;
  30. struct resource res_mem;
  31. int port;
  32. bool linked;
  33. };
  34. static struct cns3xxx_pcie *sysdata_to_cnspci(void *sysdata)
  35. {
  36. struct pci_sys_data *root = sysdata;
  37. return root->private_data;
  38. }
  39. static struct cns3xxx_pcie *pdev_to_cnspci(const struct pci_dev *dev)
  40. {
  41. return sysdata_to_cnspci(dev->sysdata);
  42. }
  43. static struct cns3xxx_pcie *pbus_to_cnspci(struct pci_bus *bus)
  44. {
  45. return sysdata_to_cnspci(bus->sysdata);
  46. }
  47. static void __iomem *cns3xxx_pci_map_bus(struct pci_bus *bus,
  48. unsigned int devfn, int where)
  49. {
  50. struct cns3xxx_pcie *cnspci = pbus_to_cnspci(bus);
  51. int busno = bus->number;
  52. int slot = PCI_SLOT(devfn);
  53. void __iomem *base;
  54. /* If there is no link, just show the CNS PCI bridge. */
  55. if (!cnspci->linked && busno > 0)
  56. return NULL;
  57. /*
  58. * The CNS PCI bridge doesn't fit into the PCI hierarchy, though
  59. * we still want to access it.
  60. * We place the host bridge on bus 0, and the directly connected
  61. * device on bus 1, slot 0.
  62. */
  63. if (busno == 0) { /* internal PCIe bus, host bridge device */
  64. if (devfn == 0) /* device# and function# are ignored by hw */
  65. base = cnspci->host_regs;
  66. else
  67. return NULL; /* no such device */
  68. } else if (busno == 1) { /* directly connected PCIe device */
  69. if (slot == 0) /* device# is ignored by hw */
  70. base = cnspci->cfg0_regs;
  71. else
  72. return NULL; /* no such device */
  73. } else /* remote PCI bus */
  74. base = cnspci->cfg1_regs + ((busno & 0xf) << 20);
  75. return base + (where & 0xffc) + (devfn << 12);
  76. }
  77. static int cns3xxx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  78. int where, int size, u32 *val)
  79. {
  80. int ret;
  81. u32 mask = (0x1ull << (size * 8)) - 1;
  82. int shift = (where % 4) * 8;
  83. ret = pci_generic_config_read32(bus, devfn, where, size, val);
  84. if (ret == PCIBIOS_SUCCESSFUL && !bus->number && !devfn &&
  85. (where & 0xffc) == PCI_CLASS_REVISION)
  86. /*
  87. * RC's class is 0xb, but Linux PCI driver needs 0x604
  88. * for a PCIe bridge. So we must fixup the class code
  89. * to 0x604 here.
  90. */
  91. *val = ((((*val << shift) & 0xff) | (0x604 << 16)) >> shift) & mask;
  92. return ret;
  93. }
  94. static int cns3xxx_pci_setup(int nr, struct pci_sys_data *sys)
  95. {
  96. struct cns3xxx_pcie *cnspci = sysdata_to_cnspci(sys);
  97. struct resource *res_io = &cnspci->res_io;
  98. struct resource *res_mem = &cnspci->res_mem;
  99. BUG_ON(request_resource(&iomem_resource, res_io) ||
  100. request_resource(&iomem_resource, res_mem));
  101. pci_add_resource_offset(&sys->resources, res_io, sys->io_offset);
  102. pci_add_resource_offset(&sys->resources, res_mem, sys->mem_offset);
  103. return 1;
  104. }
  105. static struct pci_ops cns3xxx_pcie_ops = {
  106. .map_bus = cns3xxx_pci_map_bus,
  107. .read = cns3xxx_pci_read_config,
  108. .write = pci_generic_config_write,
  109. };
  110. static int cns3xxx_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  111. {
  112. struct cns3xxx_pcie *cnspci = pdev_to_cnspci(dev);
  113. int irq = cnspci->irqs[!!dev->bus->number];
  114. pr_info("PCIe map irq: %04d:%02x:%02x.%02x slot %d, pin %d, irq: %d\n",
  115. pci_domain_nr(dev->bus), dev->bus->number, PCI_SLOT(dev->devfn),
  116. PCI_FUNC(dev->devfn), slot, pin, irq);
  117. return irq;
  118. }
  119. static struct cns3xxx_pcie cns3xxx_pcie[] = {
  120. [0] = {
  121. .host_regs = (void __iomem *)CNS3XXX_PCIE0_HOST_BASE_VIRT,
  122. .cfg0_regs = (void __iomem *)CNS3XXX_PCIE0_CFG0_BASE_VIRT,
  123. .cfg1_regs = (void __iomem *)CNS3XXX_PCIE0_CFG1_BASE_VIRT,
  124. .res_io = {
  125. .name = "PCIe0 I/O space",
  126. .start = CNS3XXX_PCIE0_IO_BASE,
  127. .end = CNS3XXX_PCIE0_CFG0_BASE - 1, /* 16 MiB */
  128. .flags = IORESOURCE_IO,
  129. },
  130. .res_mem = {
  131. .name = "PCIe0 non-prefetchable",
  132. .start = CNS3XXX_PCIE0_MEM_BASE,
  133. .end = CNS3XXX_PCIE0_HOST_BASE - 1, /* 176 MiB */
  134. .flags = IORESOURCE_MEM,
  135. },
  136. .irqs = { IRQ_CNS3XXX_PCIE0_RC, IRQ_CNS3XXX_PCIE0_DEVICE, },
  137. .port = 0,
  138. },
  139. [1] = {
  140. .host_regs = (void __iomem *)CNS3XXX_PCIE1_HOST_BASE_VIRT,
  141. .cfg0_regs = (void __iomem *)CNS3XXX_PCIE1_CFG0_BASE_VIRT,
  142. .cfg1_regs = (void __iomem *)CNS3XXX_PCIE1_CFG1_BASE_VIRT,
  143. .res_io = {
  144. .name = "PCIe1 I/O space",
  145. .start = CNS3XXX_PCIE1_IO_BASE,
  146. .end = CNS3XXX_PCIE1_CFG0_BASE - 1, /* 16 MiB */
  147. .flags = IORESOURCE_IO,
  148. },
  149. .res_mem = {
  150. .name = "PCIe1 non-prefetchable",
  151. .start = CNS3XXX_PCIE1_MEM_BASE,
  152. .end = CNS3XXX_PCIE1_HOST_BASE - 1, /* 176 MiB */
  153. .flags = IORESOURCE_MEM,
  154. },
  155. .irqs = { IRQ_CNS3XXX_PCIE1_RC, IRQ_CNS3XXX_PCIE1_DEVICE, },
  156. .port = 1,
  157. },
  158. };
  159. static void __init cns3xxx_pcie_check_link(struct cns3xxx_pcie *cnspci)
  160. {
  161. int port = cnspci->port;
  162. u32 reg;
  163. unsigned long time;
  164. reg = __raw_readl(MISC_PCIE_CTRL(port));
  165. /*
  166. * Enable Application Request to 1, it will exit L1 automatically,
  167. * but when chip back, it will use another clock, still can use 0x1.
  168. */
  169. reg |= 0x3;
  170. __raw_writel(reg, MISC_PCIE_CTRL(port));
  171. pr_info("PCIe: Port[%d] Enable PCIe LTSSM\n", port);
  172. pr_info("PCIe: Port[%d] Check data link layer...", port);
  173. time = jiffies;
  174. while (1) {
  175. reg = __raw_readl(MISC_PCIE_PM_DEBUG(port));
  176. if (reg & 0x1) {
  177. pr_info("Link up.\n");
  178. cnspci->linked = 1;
  179. break;
  180. } else if (time_after(jiffies, time + 50)) {
  181. pr_info("Device not found.\n");
  182. break;
  183. }
  184. }
  185. }
  186. static void cns3xxx_write_config(struct cns3xxx_pcie *cnspci,
  187. int where, int size, u32 val)
  188. {
  189. void __iomem *base = cnspci->host_regs + (where & 0xffc);
  190. u32 v;
  191. u32 mask = (0x1ull << (size * 8)) - 1;
  192. int shift = (where % 4) * 8;
  193. v = readl_relaxed(base);
  194. v &= ~(mask << shift);
  195. v |= (val & mask) << shift;
  196. writel_relaxed(v, base);
  197. readl_relaxed(base);
  198. }
  199. static void __init cns3xxx_pcie_hw_init(struct cns3xxx_pcie *cnspci)
  200. {
  201. u16 mem_base = cnspci->res_mem.start >> 16;
  202. u16 mem_limit = cnspci->res_mem.end >> 16;
  203. u16 io_base = cnspci->res_io.start >> 16;
  204. u16 io_limit = cnspci->res_io.end >> 16;
  205. cns3xxx_write_config(cnspci, PCI_PRIMARY_BUS, 1, 0);
  206. cns3xxx_write_config(cnspci, PCI_SECONDARY_BUS, 1, 1);
  207. cns3xxx_write_config(cnspci, PCI_SUBORDINATE_BUS, 1, 1);
  208. cns3xxx_write_config(cnspci, PCI_MEMORY_BASE, 2, mem_base);
  209. cns3xxx_write_config(cnspci, PCI_MEMORY_LIMIT, 2, mem_limit);
  210. cns3xxx_write_config(cnspci, PCI_IO_BASE_UPPER16, 2, io_base);
  211. cns3xxx_write_config(cnspci, PCI_IO_LIMIT_UPPER16, 2, io_limit);
  212. if (!cnspci->linked)
  213. return;
  214. /* Set Device Max_Read_Request_Size to 128 byte */
  215. pcie_bus_config = PCIE_BUS_PEER2PEER;
  216. /* Disable PCIe0 Interrupt Mask INTA to INTD */
  217. __raw_writel(~0x3FFF, MISC_PCIE_INT_MASK(cnspci->port));
  218. }
  219. static int cns3xxx_pcie_abort_handler(unsigned long addr, unsigned int fsr,
  220. struct pt_regs *regs)
  221. {
  222. if (fsr & (1 << 10))
  223. regs->ARM_pc += 4;
  224. return 0;
  225. }
  226. void __init cns3xxx_pcie_init_late(void)
  227. {
  228. int i;
  229. void *private_data;
  230. struct hw_pci hw_pci = {
  231. .nr_controllers = 1,
  232. .ops = &cns3xxx_pcie_ops,
  233. .setup = cns3xxx_pci_setup,
  234. .map_irq = cns3xxx_pcie_map_irq,
  235. .private_data = &private_data,
  236. };
  237. pcibios_min_io = 0;
  238. pcibios_min_mem = 0;
  239. hook_fault_code(16 + 6, cns3xxx_pcie_abort_handler, SIGBUS, 0,
  240. "imprecise external abort");
  241. for (i = 0; i < ARRAY_SIZE(cns3xxx_pcie); i++) {
  242. cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_PCIE(i));
  243. cns3xxx_pwr_soft_rst(0x1 << PM_SOFT_RST_REG_OFFST_PCIE(i));
  244. cns3xxx_pcie_check_link(&cns3xxx_pcie[i]);
  245. cns3xxx_pcie_hw_init(&cns3xxx_pcie[i]);
  246. private_data = &cns3xxx_pcie[i];
  247. pci_common_init(&hw_pci);
  248. }
  249. pci_assign_unassigned_resources();
  250. }