pci-keystone.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. /* DEV_STAT_CTRL */
  32. #define PCIE_CAP_BASE 0x70
  33. /* PCIE controller device IDs */
  34. #define PCIE_RC_K2HK 0xb008
  35. #define PCIE_RC_K2E 0xb009
  36. #define PCIE_RC_K2L 0xb00a
  37. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  38. static void quirk_limit_mrrs(struct pci_dev *dev)
  39. {
  40. struct pci_bus *bus = dev->bus;
  41. struct pci_dev *bridge = bus->self;
  42. static const struct pci_device_id rc_pci_devids[] = {
  43. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  44. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  45. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  46. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  47. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  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_err(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_find_node_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. return -EINVAL;
  165. }
  166. if (temp > max_host_irqs)
  167. dev_warn(dev, "Too many %s interrupts defined %u\n",
  168. (legacy ? "legacy" : "MSI"), temp);
  169. /*
  170. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  171. * 7 (MSI)
  172. */
  173. for (temp = 0; temp < max_host_irqs; temp++) {
  174. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  175. if (!host_irqs[temp])
  176. break;
  177. }
  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. u32 val;
  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. /* update the DEV_STAT_CTRL to publish right mrrs */
  234. val = readl(pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  235. val &= ~PCI_EXP_DEVCTL_READRQ;
  236. /* set the mrrs to 256 bytes */
  237. val |= BIT(12);
  238. writel(val, pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  239. /*
  240. * PCIe access errors that result into OCP errors are caught by ARM as
  241. * "External aborts"
  242. */
  243. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  244. "Asynchronous external abort");
  245. return 0;
  246. }
  247. static const struct dw_pcie_host_ops keystone_pcie_host_ops = {
  248. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  249. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  250. .host_init = ks_pcie_host_init,
  251. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  252. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  253. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  254. .msi_host_init = ks_dw_pcie_msi_host_init,
  255. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  256. };
  257. static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
  258. {
  259. struct keystone_pcie *ks_pcie = priv;
  260. return ks_dw_pcie_handle_error_irq(ks_pcie);
  261. }
  262. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  263. struct platform_device *pdev)
  264. {
  265. struct dw_pcie *pci = ks_pcie->pci;
  266. struct pcie_port *pp = &pci->pp;
  267. struct device *dev = &pdev->dev;
  268. int ret;
  269. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  270. "legacy-interrupt-controller",
  271. &ks_pcie->num_legacy_host_irqs);
  272. if (ret)
  273. return ret;
  274. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  275. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  276. "msi-interrupt-controller",
  277. &ks_pcie->num_msi_host_irqs);
  278. if (ret)
  279. return ret;
  280. }
  281. /*
  282. * Index 0 is the platform interrupt for error interrupt
  283. * from RC. This is optional.
  284. */
  285. ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
  286. if (ks_pcie->error_irq <= 0)
  287. dev_info(dev, "no error IRQ defined\n");
  288. else {
  289. ret = request_irq(ks_pcie->error_irq, pcie_err_irq_handler,
  290. IRQF_SHARED, "pcie-error-irq", ks_pcie);
  291. if (ret < 0) {
  292. dev_err(dev, "failed to request error IRQ %d\n",
  293. ks_pcie->error_irq);
  294. return ret;
  295. }
  296. }
  297. pp->root_bus_nr = -1;
  298. pp->ops = &keystone_pcie_host_ops;
  299. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  300. if (ret) {
  301. dev_err(dev, "failed to initialize host\n");
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. static const struct of_device_id ks_pcie_of_match[] = {
  307. {
  308. .type = "pci",
  309. .compatible = "ti,keystone-pcie",
  310. },
  311. { },
  312. };
  313. static const struct dw_pcie_ops dw_pcie_ops = {
  314. .link_up = ks_dw_pcie_link_up,
  315. };
  316. static int __exit ks_pcie_remove(struct platform_device *pdev)
  317. {
  318. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  319. clk_disable_unprepare(ks_pcie->clk);
  320. return 0;
  321. }
  322. static int __init ks_pcie_probe(struct platform_device *pdev)
  323. {
  324. struct device *dev = &pdev->dev;
  325. struct dw_pcie *pci;
  326. struct keystone_pcie *ks_pcie;
  327. struct resource *res;
  328. void __iomem *reg_p;
  329. struct phy *phy;
  330. int ret;
  331. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  332. if (!ks_pcie)
  333. return -ENOMEM;
  334. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  335. if (!pci)
  336. return -ENOMEM;
  337. pci->dev = dev;
  338. pci->ops = &dw_pcie_ops;
  339. ks_pcie->pci = pci;
  340. /* initialize SerDes Phy if present */
  341. phy = devm_phy_get(dev, "pcie-phy");
  342. if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
  343. return PTR_ERR(phy);
  344. if (!IS_ERR_OR_NULL(phy)) {
  345. ret = phy_init(phy);
  346. if (ret < 0)
  347. return ret;
  348. }
  349. /* index 2 is to read PCI DEVICE_ID */
  350. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  351. reg_p = devm_ioremap_resource(dev, res);
  352. if (IS_ERR(reg_p))
  353. return PTR_ERR(reg_p);
  354. ks_pcie->device_id = readl(reg_p) >> 16;
  355. devm_iounmap(dev, reg_p);
  356. devm_release_mem_region(dev, res->start, resource_size(res));
  357. ks_pcie->np = dev->of_node;
  358. platform_set_drvdata(pdev, ks_pcie);
  359. ks_pcie->clk = devm_clk_get(dev, "pcie");
  360. if (IS_ERR(ks_pcie->clk)) {
  361. dev_err(dev, "Failed to get pcie rc clock\n");
  362. return PTR_ERR(ks_pcie->clk);
  363. }
  364. ret = clk_prepare_enable(ks_pcie->clk);
  365. if (ret)
  366. return ret;
  367. platform_set_drvdata(pdev, ks_pcie);
  368. ret = ks_add_pcie_port(ks_pcie, pdev);
  369. if (ret < 0)
  370. goto fail_clk;
  371. return 0;
  372. fail_clk:
  373. clk_disable_unprepare(ks_pcie->clk);
  374. return ret;
  375. }
  376. static struct platform_driver ks_pcie_driver __refdata = {
  377. .probe = ks_pcie_probe,
  378. .remove = __exit_p(ks_pcie_remove),
  379. .driver = {
  380. .name = "keystone-pcie",
  381. .of_match_table = of_match_ptr(ks_pcie_of_match),
  382. },
  383. };
  384. builtin_platform_driver(ks_pcie_driver);