pci-keystone.c 11 KB

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