pci-keystone.c 12 KB

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