pci-keystone.c 11 KB

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