pci-keystone.c 11 KB

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