pcie.c 7.9 KB

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