pcie.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. struct hw_pci hw_pci;
  32. bool linked;
  33. };
  34. static struct cns3xxx_pcie cns3xxx_pcie[]; /* forward decl. */
  35. static struct cns3xxx_pcie *sysdata_to_cnspci(void *sysdata)
  36. {
  37. struct pci_sys_data *root = sysdata;
  38. return &cns3xxx_pcie[root->domain];
  39. }
  40. static struct cns3xxx_pcie *pdev_to_cnspci(const struct pci_dev *dev)
  41. {
  42. return sysdata_to_cnspci(dev->sysdata);
  43. }
  44. static struct cns3xxx_pcie *pbus_to_cnspci(struct pci_bus *bus)
  45. {
  46. return sysdata_to_cnspci(bus->sysdata);
  47. }
  48. static void __iomem *cns3xxx_pci_cfg_base(struct pci_bus *bus,
  49. unsigned int devfn, int where)
  50. {
  51. struct cns3xxx_pcie *cnspci = pbus_to_cnspci(bus);
  52. int busno = bus->number;
  53. int slot = PCI_SLOT(devfn);
  54. void __iomem *base;
  55. /* If there is no link, just show the CNS PCI bridge. */
  56. if (!cnspci->linked && busno > 0)
  57. return NULL;
  58. /*
  59. * The CNS PCI bridge doesn't fit into the PCI hierarchy, though
  60. * we still want to access it. For this to work, we must place
  61. * the first device on the same bus as the CNS PCI bridge.
  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. u32 v;
  81. void __iomem *base;
  82. u32 mask = (0x1ull << (size * 8)) - 1;
  83. int shift = (where % 4) * 8;
  84. base = cns3xxx_pci_cfg_base(bus, devfn, where);
  85. if (!base) {
  86. *val = 0xffffffff;
  87. return PCIBIOS_SUCCESSFUL;
  88. }
  89. v = __raw_readl(base);
  90. if (bus->number == 0 && devfn == 0 &&
  91. (where & 0xffc) == PCI_CLASS_REVISION) {
  92. /*
  93. * RC's class is 0xb, but Linux PCI driver needs 0x604
  94. * for a PCIe bridge. So we must fixup the class code
  95. * to 0x604 here.
  96. */
  97. v &= 0xff;
  98. v |= 0x604 << 16;
  99. }
  100. *val = (v >> shift) & mask;
  101. return PCIBIOS_SUCCESSFUL;
  102. }
  103. static int cns3xxx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  104. int where, int size, u32 val)
  105. {
  106. u32 v;
  107. void __iomem *base;
  108. u32 mask = (0x1ull << (size * 8)) - 1;
  109. int shift = (where % 4) * 8;
  110. base = cns3xxx_pci_cfg_base(bus, devfn, where);
  111. if (!base)
  112. return PCIBIOS_SUCCESSFUL;
  113. v = __raw_readl(base);
  114. v &= ~(mask << shift);
  115. v |= (val & mask) << shift;
  116. __raw_writel(v, base);
  117. return PCIBIOS_SUCCESSFUL;
  118. }
  119. static int cns3xxx_pci_setup(int nr, struct pci_sys_data *sys)
  120. {
  121. struct cns3xxx_pcie *cnspci = sysdata_to_cnspci(sys);
  122. struct resource *res_io = &cnspci->res_io;
  123. struct resource *res_mem = &cnspci->res_mem;
  124. BUG_ON(request_resource(&iomem_resource, res_io) ||
  125. request_resource(&iomem_resource, res_mem));
  126. pci_add_resource_offset(&sys->resources, res_io, sys->io_offset);
  127. pci_add_resource_offset(&sys->resources, res_mem, sys->mem_offset);
  128. return 1;
  129. }
  130. static struct pci_ops cns3xxx_pcie_ops = {
  131. .read = cns3xxx_pci_read_config,
  132. .write = cns3xxx_pci_write_config,
  133. };
  134. static int cns3xxx_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  135. {
  136. struct cns3xxx_pcie *cnspci = pdev_to_cnspci(dev);
  137. int irq = cnspci->irqs[!!dev->bus->number];
  138. pr_info("PCIe map irq: %04d:%02x:%02x.%02x slot %d, pin %d, irq: %d\n",
  139. pci_domain_nr(dev->bus), dev->bus->number, PCI_SLOT(dev->devfn),
  140. PCI_FUNC(dev->devfn), slot, pin, irq);
  141. return irq;
  142. }
  143. static struct cns3xxx_pcie cns3xxx_pcie[] = {
  144. [0] = {
  145. .host_regs = (void __iomem *)CNS3XXX_PCIE0_HOST_BASE_VIRT,
  146. .cfg0_regs = (void __iomem *)CNS3XXX_PCIE0_CFG0_BASE_VIRT,
  147. .cfg1_regs = (void __iomem *)CNS3XXX_PCIE0_CFG1_BASE_VIRT,
  148. .res_io = {
  149. .name = "PCIe0 I/O space",
  150. .start = CNS3XXX_PCIE0_IO_BASE,
  151. .end = CNS3XXX_PCIE0_CFG0_BASE - 1, /* 16 MiB */
  152. .flags = IORESOURCE_IO,
  153. },
  154. .res_mem = {
  155. .name = "PCIe0 non-prefetchable",
  156. .start = CNS3XXX_PCIE0_MEM_BASE,
  157. .end = CNS3XXX_PCIE0_HOST_BASE - 1, /* 176 MiB */
  158. .flags = IORESOURCE_MEM,
  159. },
  160. .irqs = { IRQ_CNS3XXX_PCIE0_RC, IRQ_CNS3XXX_PCIE0_DEVICE, },
  161. .hw_pci = {
  162. .domain = 0,
  163. .nr_controllers = 1,
  164. .ops = &cns3xxx_pcie_ops,
  165. .setup = cns3xxx_pci_setup,
  166. .map_irq = cns3xxx_pcie_map_irq,
  167. },
  168. },
  169. [1] = {
  170. .host_regs = (void __iomem *)CNS3XXX_PCIE1_HOST_BASE_VIRT,
  171. .cfg0_regs = (void __iomem *)CNS3XXX_PCIE1_CFG0_BASE_VIRT,
  172. .cfg1_regs = (void __iomem *)CNS3XXX_PCIE1_CFG1_BASE_VIRT,
  173. .res_io = {
  174. .name = "PCIe1 I/O space",
  175. .start = CNS3XXX_PCIE1_IO_BASE,
  176. .end = CNS3XXX_PCIE1_CFG0_BASE - 1, /* 16 MiB */
  177. .flags = IORESOURCE_IO,
  178. },
  179. .res_mem = {
  180. .name = "PCIe1 non-prefetchable",
  181. .start = CNS3XXX_PCIE1_MEM_BASE,
  182. .end = CNS3XXX_PCIE1_HOST_BASE - 1, /* 176 MiB */
  183. .flags = IORESOURCE_MEM,
  184. },
  185. .irqs = { IRQ_CNS3XXX_PCIE1_RC, IRQ_CNS3XXX_PCIE1_DEVICE, },
  186. .hw_pci = {
  187. .domain = 1,
  188. .nr_controllers = 1,
  189. .ops = &cns3xxx_pcie_ops,
  190. .setup = cns3xxx_pci_setup,
  191. .map_irq = cns3xxx_pcie_map_irq,
  192. },
  193. },
  194. };
  195. static void __init cns3xxx_pcie_check_link(struct cns3xxx_pcie *cnspci)
  196. {
  197. int port = cnspci->hw_pci.domain;
  198. u32 reg;
  199. unsigned long time;
  200. reg = __raw_readl(MISC_PCIE_CTRL(port));
  201. /*
  202. * Enable Application Request to 1, it will exit L1 automatically,
  203. * but when chip back, it will use another clock, still can use 0x1.
  204. */
  205. reg |= 0x3;
  206. __raw_writel(reg, MISC_PCIE_CTRL(port));
  207. pr_info("PCIe: Port[%d] Enable PCIe LTSSM\n", port);
  208. pr_info("PCIe: Port[%d] Check data link layer...", port);
  209. time = jiffies;
  210. while (1) {
  211. reg = __raw_readl(MISC_PCIE_PM_DEBUG(port));
  212. if (reg & 0x1) {
  213. pr_info("Link up.\n");
  214. cnspci->linked = 1;
  215. break;
  216. } else if (time_after(jiffies, time + 50)) {
  217. pr_info("Device not found.\n");
  218. break;
  219. }
  220. }
  221. }
  222. static void __init cns3xxx_pcie_hw_init(struct cns3xxx_pcie *cnspci)
  223. {
  224. int port = cnspci->hw_pci.domain;
  225. struct pci_sys_data sd = {
  226. .domain = port,
  227. };
  228. struct pci_bus bus = {
  229. .number = 0,
  230. .ops = &cns3xxx_pcie_ops,
  231. .sysdata = &sd,
  232. };
  233. u16 mem_base = cnspci->res_mem.start >> 16;
  234. u16 mem_limit = cnspci->res_mem.end >> 16;
  235. u16 io_base = cnspci->res_io.start >> 16;
  236. u16 io_limit = cnspci->res_io.end >> 16;
  237. u32 devfn = 0;
  238. u8 tmp8;
  239. u16 pos;
  240. u16 dc;
  241. pci_bus_write_config_byte(&bus, devfn, PCI_PRIMARY_BUS, 0);
  242. pci_bus_write_config_byte(&bus, devfn, PCI_SECONDARY_BUS, 1);
  243. pci_bus_write_config_byte(&bus, devfn, PCI_SUBORDINATE_BUS, 1);
  244. pci_bus_read_config_byte(&bus, devfn, PCI_PRIMARY_BUS, &tmp8);
  245. pci_bus_read_config_byte(&bus, devfn, PCI_SECONDARY_BUS, &tmp8);
  246. pci_bus_read_config_byte(&bus, devfn, PCI_SUBORDINATE_BUS, &tmp8);
  247. pci_bus_write_config_word(&bus, devfn, PCI_MEMORY_BASE, mem_base);
  248. pci_bus_write_config_word(&bus, devfn, PCI_MEMORY_LIMIT, mem_limit);
  249. pci_bus_write_config_word(&bus, devfn, PCI_IO_BASE_UPPER16, io_base);
  250. pci_bus_write_config_word(&bus, devfn, PCI_IO_LIMIT_UPPER16, io_limit);
  251. if (!cnspci->linked)
  252. return;
  253. /* Set Device Max_Read_Request_Size to 128 byte */
  254. bus.number = 1; /* directly connected PCIe device */
  255. devfn = PCI_DEVFN(0, 0);
  256. pos = pci_bus_find_capability(&bus, devfn, PCI_CAP_ID_EXP);
  257. pci_bus_read_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, &dc);
  258. if (dc & PCI_EXP_DEVCTL_READRQ) {
  259. dc &= ~PCI_EXP_DEVCTL_READRQ;
  260. pci_bus_write_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, dc);
  261. pci_bus_read_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, &dc);
  262. if (dc & PCI_EXP_DEVCTL_READRQ)
  263. pr_warn("PCIe: Unable to set device Max_Read_Request_Size\n");
  264. else
  265. pr_info("PCIe: Max_Read_Request_Size set to 128 bytes\n");
  266. }
  267. /* Disable PCIe0 Interrupt Mask INTA to INTD */
  268. __raw_writel(~0x3FFF, MISC_PCIE_INT_MASK(port));
  269. }
  270. static int cns3xxx_pcie_abort_handler(unsigned long addr, unsigned int fsr,
  271. struct pt_regs *regs)
  272. {
  273. if (fsr & (1 << 10))
  274. regs->ARM_pc += 4;
  275. return 0;
  276. }
  277. void __init cns3xxx_pcie_init_late(void)
  278. {
  279. int i;
  280. pcibios_min_io = 0;
  281. pcibios_min_mem = 0;
  282. hook_fault_code(16 + 6, cns3xxx_pcie_abort_handler, SIGBUS, 0,
  283. "imprecise external abort");
  284. for (i = 0; i < ARRAY_SIZE(cns3xxx_pcie); i++) {
  285. cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_PCIE(i));
  286. cns3xxx_pwr_soft_rst(0x1 << PM_SOFT_RST_REG_OFFST_PCIE(i));
  287. cns3xxx_pcie_check_link(&cns3xxx_pcie[i]);
  288. cns3xxx_pcie_hw_init(&cns3xxx_pcie[i]);
  289. pci_common_init(&cns3xxx_pcie[i].hw_pci);
  290. }
  291. pci_assign_unassigned_resources();
  292. }