pci-keystone.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Texas Instruments Keystone SoCs
  4. *
  5. * Copyright (C) 2013-2014 Texas Instruments., Ltd.
  6. * http://www.ti.com
  7. *
  8. * Author: Murali Karicheri <m-karicheri2@ti.com>
  9. * Implementation based on pci-exynos.c and pcie-designware.c
  10. */
  11. #include <linux/irqchip/chained_irq.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/init.h>
  17. #include <linux/msi.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/resource.h>
  24. #include <linux/signal.h>
  25. #include "pcie-designware.h"
  26. #include "pci-keystone.h"
  27. #define DRIVER_NAME "keystone-pcie"
  28. /* DEV_STAT_CTRL */
  29. #define PCIE_CAP_BASE 0x70
  30. /* PCIE controller device IDs */
  31. #define PCIE_RC_K2HK 0xb008
  32. #define PCIE_RC_K2E 0xb009
  33. #define PCIE_RC_K2L 0xb00a
  34. #define PCIE_RC_K2G 0xb00b
  35. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  36. static void quirk_limit_mrrs(struct pci_dev *dev)
  37. {
  38. struct pci_bus *bus = dev->bus;
  39. struct pci_dev *bridge = bus->self;
  40. static const struct pci_device_id rc_pci_devids[] = {
  41. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  42. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  43. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  44. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  45. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  46. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  47. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
  48. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  49. { 0, },
  50. };
  51. if (pci_is_root_bus(bus))
  52. return;
  53. /* look for the host bridge */
  54. while (!pci_is_root_bus(bus)) {
  55. bridge = bus->self;
  56. bus = bus->parent;
  57. }
  58. if (bridge) {
  59. /*
  60. * Keystone PCI controller has a h/w limitation of
  61. * 256 bytes maximum read request size. It can't handle
  62. * anything higher than this. So force this limit on
  63. * all downstream devices.
  64. */
  65. if (pci_match_id(rc_pci_devids, bridge)) {
  66. if (pcie_get_readrq(dev) > 256) {
  67. dev_info(&dev->dev, "limiting MRRS to 256\n");
  68. pcie_set_readrq(dev, 256);
  69. }
  70. }
  71. }
  72. }
  73. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, quirk_limit_mrrs);
  74. static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
  75. {
  76. struct dw_pcie *pci = ks_pcie->pci;
  77. struct pcie_port *pp = &pci->pp;
  78. struct device *dev = pci->dev;
  79. unsigned int retries;
  80. dw_pcie_setup_rc(pp);
  81. if (dw_pcie_link_up(pci)) {
  82. dev_info(dev, "Link already up\n");
  83. return 0;
  84. }
  85. /* check if the link is up or not */
  86. for (retries = 0; retries < 5; retries++) {
  87. ks_dw_pcie_initiate_link_train(ks_pcie);
  88. if (!dw_pcie_wait_for_link(pci))
  89. return 0;
  90. }
  91. dev_err(dev, "phy link never came up\n");
  92. return -ETIMEDOUT;
  93. }
  94. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  95. {
  96. unsigned int irq = irq_desc_get_irq(desc);
  97. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  98. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  99. struct dw_pcie *pci = ks_pcie->pci;
  100. struct device *dev = pci->dev;
  101. struct irq_chip *chip = irq_desc_get_chip(desc);
  102. dev_dbg(dev, "%s, irq %d\n", __func__, irq);
  103. /*
  104. * The chained irq handler installation would have replaced normal
  105. * interrupt driver handler so we need to take care of mask/unmask and
  106. * ack operation.
  107. */
  108. chained_irq_enter(chip, desc);
  109. ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
  110. chained_irq_exit(chip, desc);
  111. }
  112. /**
  113. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  114. * @irq: IRQ line for legacy interrupts
  115. * @desc: Pointer to irq descriptor
  116. *
  117. * Traverse through pending legacy interrupts and invoke handler for each. Also
  118. * takes care of interrupt controller level mask/ack operation.
  119. */
  120. static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
  121. {
  122. unsigned int irq = irq_desc_get_irq(desc);
  123. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  124. struct dw_pcie *pci = ks_pcie->pci;
  125. struct device *dev = pci->dev;
  126. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  127. struct irq_chip *chip = irq_desc_get_chip(desc);
  128. dev_dbg(dev, ": Handling legacy irq %d\n", irq);
  129. /*
  130. * The chained irq handler installation would have replaced normal
  131. * interrupt driver handler so we need to take care of mask/unmask and
  132. * ack operation.
  133. */
  134. chained_irq_enter(chip, desc);
  135. ks_dw_pcie_handle_legacy_irq(ks_pcie, irq_offset);
  136. chained_irq_exit(chip, desc);
  137. }
  138. static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
  139. char *controller, int *num_irqs)
  140. {
  141. int temp, max_host_irqs, legacy = 1, *host_irqs;
  142. struct device *dev = ks_pcie->pci->dev;
  143. struct device_node *np_pcie = dev->of_node, **np_temp;
  144. if (!strcmp(controller, "msi-interrupt-controller"))
  145. legacy = 0;
  146. if (legacy) {
  147. np_temp = &ks_pcie->legacy_intc_np;
  148. max_host_irqs = PCI_NUM_INTX;
  149. host_irqs = &ks_pcie->legacy_host_irqs[0];
  150. } else {
  151. np_temp = &ks_pcie->msi_intc_np;
  152. max_host_irqs = MAX_MSI_HOST_IRQS;
  153. host_irqs = &ks_pcie->msi_host_irqs[0];
  154. }
  155. /* interrupt controller is in a child node */
  156. *np_temp = of_get_child_by_name(np_pcie, controller);
  157. if (!(*np_temp)) {
  158. dev_err(dev, "Node for %s is absent\n", controller);
  159. return -EINVAL;
  160. }
  161. temp = of_irq_count(*np_temp);
  162. if (!temp) {
  163. dev_err(dev, "No IRQ entries in %s\n", controller);
  164. of_node_put(*np_temp);
  165. return -EINVAL;
  166. }
  167. if (temp > max_host_irqs)
  168. dev_warn(dev, "Too many %s interrupts defined %u\n",
  169. (legacy ? "legacy" : "MSI"), temp);
  170. /*
  171. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  172. * 7 (MSI)
  173. */
  174. for (temp = 0; temp < max_host_irqs; temp++) {
  175. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  176. if (!host_irqs[temp])
  177. break;
  178. }
  179. of_node_put(*np_temp);
  180. if (temp) {
  181. *num_irqs = temp;
  182. return 0;
  183. }
  184. return -EINVAL;
  185. }
  186. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  187. {
  188. int i;
  189. /* Legacy IRQ */
  190. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  191. irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  192. ks_pcie_legacy_irq_handler,
  193. ks_pcie);
  194. }
  195. ks_dw_pcie_enable_legacy_irqs(ks_pcie);
  196. /* MSI IRQ */
  197. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  198. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  199. irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
  200. ks_pcie_msi_irq_handler,
  201. ks_pcie);
  202. }
  203. }
  204. if (ks_pcie->error_irq > 0)
  205. ks_dw_pcie_enable_error_irq(ks_pcie);
  206. }
  207. #ifdef CONFIG_ARM
  208. /*
  209. * When a PCI device does not exist during config cycles, keystone host gets a
  210. * bus error instead of returning 0xffffffff. This handler always returns 0
  211. * for this kind of faults.
  212. */
  213. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  214. struct pt_regs *regs)
  215. {
  216. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  217. if ((instr & 0x0e100090) == 0x00100090) {
  218. int reg = (instr >> 12) & 15;
  219. regs->uregs[reg] = -1;
  220. regs->ARM_pc += 4;
  221. }
  222. return 0;
  223. }
  224. #endif
  225. static int __init ks_pcie_host_init(struct pcie_port *pp)
  226. {
  227. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  228. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  229. u32 val;
  230. ks_pcie_establish_link(ks_pcie);
  231. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  232. ks_pcie_setup_interrupts(ks_pcie);
  233. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  234. pci->dbi_base + PCI_IO_BASE);
  235. /* update the Vendor ID */
  236. writew(ks_pcie->device_id, pci->dbi_base + PCI_DEVICE_ID);
  237. /* update the DEV_STAT_CTRL to publish right mrrs */
  238. val = readl(pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  239. val &= ~PCI_EXP_DEVCTL_READRQ;
  240. /* set the mrrs to 256 bytes */
  241. val |= BIT(12);
  242. writel(val, pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  243. #ifdef CONFIG_ARM
  244. /*
  245. * PCIe access errors that result into OCP errors are caught by ARM as
  246. * "External aborts"
  247. */
  248. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  249. "Asynchronous external abort");
  250. #endif
  251. return 0;
  252. }
  253. static const struct dw_pcie_host_ops keystone_pcie_host_ops = {
  254. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  255. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  256. .host_init = ks_pcie_host_init,
  257. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  258. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  259. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  260. .msi_host_init = ks_dw_pcie_msi_host_init,
  261. .msi_irq_ack = ks_dw_pcie_msi_irq_ack,
  262. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  263. };
  264. static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
  265. {
  266. struct keystone_pcie *ks_pcie = priv;
  267. return ks_dw_pcie_handle_error_irq(ks_pcie);
  268. }
  269. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  270. struct platform_device *pdev)
  271. {
  272. struct dw_pcie *pci = ks_pcie->pci;
  273. struct pcie_port *pp = &pci->pp;
  274. struct device *dev = &pdev->dev;
  275. int ret;
  276. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  277. "legacy-interrupt-controller",
  278. &ks_pcie->num_legacy_host_irqs);
  279. if (ret)
  280. return ret;
  281. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  282. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  283. "msi-interrupt-controller",
  284. &ks_pcie->num_msi_host_irqs);
  285. if (ret)
  286. return ret;
  287. }
  288. /*
  289. * Index 0 is the platform interrupt for error interrupt
  290. * from RC. This is optional.
  291. */
  292. ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
  293. if (ks_pcie->error_irq <= 0)
  294. dev_info(dev, "no error IRQ defined\n");
  295. else {
  296. ret = request_irq(ks_pcie->error_irq, pcie_err_irq_handler,
  297. IRQF_SHARED, "pcie-error-irq", ks_pcie);
  298. if (ret < 0) {
  299. dev_err(dev, "failed to request error IRQ %d\n",
  300. ks_pcie->error_irq);
  301. return ret;
  302. }
  303. }
  304. pp->ops = &keystone_pcie_host_ops;
  305. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  306. if (ret) {
  307. dev_err(dev, "failed to initialize host\n");
  308. return ret;
  309. }
  310. return 0;
  311. }
  312. static const struct of_device_id ks_pcie_of_match[] = {
  313. {
  314. .type = "pci",
  315. .compatible = "ti,keystone-pcie",
  316. },
  317. { },
  318. };
  319. static const struct dw_pcie_ops dw_pcie_ops = {
  320. .link_up = ks_dw_pcie_link_up,
  321. };
  322. static int __exit ks_pcie_remove(struct platform_device *pdev)
  323. {
  324. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  325. clk_disable_unprepare(ks_pcie->clk);
  326. return 0;
  327. }
  328. static int __init ks_pcie_probe(struct platform_device *pdev)
  329. {
  330. struct device *dev = &pdev->dev;
  331. struct dw_pcie *pci;
  332. struct keystone_pcie *ks_pcie;
  333. struct resource *res;
  334. void __iomem *reg_p;
  335. struct phy *phy;
  336. int ret;
  337. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  338. if (!ks_pcie)
  339. return -ENOMEM;
  340. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  341. if (!pci)
  342. return -ENOMEM;
  343. pci->dev = dev;
  344. pci->ops = &dw_pcie_ops;
  345. ks_pcie->pci = pci;
  346. /* initialize SerDes Phy if present */
  347. phy = devm_phy_get(dev, "pcie-phy");
  348. if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
  349. return PTR_ERR(phy);
  350. if (!IS_ERR_OR_NULL(phy)) {
  351. ret = phy_init(phy);
  352. if (ret < 0)
  353. return ret;
  354. }
  355. /* index 2 is to read PCI DEVICE_ID */
  356. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  357. reg_p = devm_ioremap_resource(dev, res);
  358. if (IS_ERR(reg_p))
  359. return PTR_ERR(reg_p);
  360. ks_pcie->device_id = readl(reg_p) >> 16;
  361. devm_iounmap(dev, reg_p);
  362. devm_release_mem_region(dev, res->start, resource_size(res));
  363. ks_pcie->np = dev->of_node;
  364. platform_set_drvdata(pdev, ks_pcie);
  365. ks_pcie->clk = devm_clk_get(dev, "pcie");
  366. if (IS_ERR(ks_pcie->clk)) {
  367. dev_err(dev, "Failed to get pcie rc clock\n");
  368. return PTR_ERR(ks_pcie->clk);
  369. }
  370. ret = clk_prepare_enable(ks_pcie->clk);
  371. if (ret)
  372. return ret;
  373. platform_set_drvdata(pdev, ks_pcie);
  374. ret = ks_add_pcie_port(ks_pcie, pdev);
  375. if (ret < 0)
  376. goto fail_clk;
  377. return 0;
  378. fail_clk:
  379. clk_disable_unprepare(ks_pcie->clk);
  380. return ret;
  381. }
  382. static struct platform_driver ks_pcie_driver __refdata = {
  383. .probe = ks_pcie_probe,
  384. .remove = __exit_p(ks_pcie_remove),
  385. .driver = {
  386. .name = "keystone-pcie",
  387. .of_match_table = of_match_ptr(ks_pcie_of_match),
  388. },
  389. };
  390. builtin_platform_driver(ks_pcie_driver);