pci-keystone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/irqdomain.h>
  18. #include <linux/module.h>
  19. #include <linux/msi.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of.h>
  22. #include <linux/of_pci.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/resource.h>
  26. #include <linux/signal.h>
  27. #include "pcie-designware.h"
  28. #include "pci-keystone.h"
  29. #define DRIVER_NAME "keystone-pcie"
  30. /* driver specific constants */
  31. #define MAX_MSI_HOST_IRQS 8
  32. #define MAX_LEGACY_HOST_IRQS 4
  33. /* DEV_STAT_CTRL */
  34. #define PCIE_CAP_BASE 0x70
  35. /* PCIE controller device IDs */
  36. #define PCIE_RC_K2HK 0xb008
  37. #define PCIE_RC_K2E 0xb009
  38. #define PCIE_RC_K2L 0xb00a
  39. #define to_keystone_pcie(x) container_of(x, struct keystone_pcie, pp)
  40. static void quirk_limit_mrrs(struct pci_dev *dev)
  41. {
  42. struct pci_bus *bus = dev->bus;
  43. struct pci_dev *bridge = bus->self;
  44. static const struct pci_device_id rc_pci_devids[] = {
  45. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  46. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  47. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  48. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  49. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  50. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  51. { 0, },
  52. };
  53. if (pci_is_root_bus(bus))
  54. return;
  55. /* look for the host bridge */
  56. while (!pci_is_root_bus(bus)) {
  57. bridge = bus->self;
  58. bus = bus->parent;
  59. }
  60. if (bridge) {
  61. /*
  62. * Keystone PCI controller has a h/w limitation of
  63. * 256 bytes maximum read request size. It can't handle
  64. * anything higher than this. So force this limit on
  65. * all downstream devices.
  66. */
  67. if (pci_match_id(rc_pci_devids, bridge)) {
  68. if (pcie_get_readrq(dev) > 256) {
  69. dev_info(&dev->dev, "limiting MRRS to 256\n");
  70. pcie_set_readrq(dev, 256);
  71. }
  72. }
  73. }
  74. }
  75. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, quirk_limit_mrrs);
  76. static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
  77. {
  78. struct pcie_port *pp = &ks_pcie->pp;
  79. unsigned int retries;
  80. dw_pcie_setup_rc(pp);
  81. if (dw_pcie_link_up(pp)) {
  82. dev_err(pp->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(pp))
  89. return 0;
  90. }
  91. dev_err(pp->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 pcie_port *pp = &ks_pcie->pp;
  100. struct irq_chip *chip = irq_desc_get_chip(desc);
  101. dev_dbg(pp->dev, "%s, irq %d\n", __func__, irq);
  102. /*
  103. * The chained irq handler installation would have replaced normal
  104. * interrupt driver handler so we need to take care of mask/unmask and
  105. * ack operation.
  106. */
  107. chained_irq_enter(chip, desc);
  108. ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
  109. chained_irq_exit(chip, desc);
  110. }
  111. /**
  112. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  113. * @irq: IRQ line for legacy interrupts
  114. * @desc: Pointer to irq descriptor
  115. *
  116. * Traverse through pending legacy interrupts and invoke handler for each. Also
  117. * takes care of interrupt controller level mask/ack operation.
  118. */
  119. static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
  120. {
  121. unsigned int irq = irq_desc_get_irq(desc);
  122. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  123. struct pcie_port *pp = &ks_pcie->pp;
  124. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  125. struct irq_chip *chip = irq_desc_get_chip(desc);
  126. dev_dbg(pp->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, ret = -EINVAL;
  140. struct device *dev = ks_pcie->pp.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 = MAX_LEGACY_HOST_IRQS;
  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_find_node_by_name(np_pcie, controller);
  155. if (!(*np_temp)) {
  156. dev_err(dev, "Node for %s is absent\n", controller);
  157. goto out;
  158. }
  159. temp = of_irq_count(*np_temp);
  160. if (!temp)
  161. goto out;
  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. if (temp) {
  175. *num_irqs = temp;
  176. ret = 0;
  177. }
  178. out:
  179. return ret;
  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. }
  200. /*
  201. * When a PCI device does not exist during config cycles, keystone host gets a
  202. * bus error instead of returning 0xffffffff. This handler always returns 0
  203. * for this kind of faults.
  204. */
  205. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  206. struct pt_regs *regs)
  207. {
  208. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  209. if ((instr & 0x0e100090) == 0x00100090) {
  210. int reg = (instr >> 12) & 15;
  211. regs->uregs[reg] = -1;
  212. regs->ARM_pc += 4;
  213. }
  214. return 0;
  215. }
  216. static void __init ks_pcie_host_init(struct pcie_port *pp)
  217. {
  218. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  219. u32 val;
  220. ks_pcie_establish_link(ks_pcie);
  221. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  222. ks_pcie_setup_interrupts(ks_pcie);
  223. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  224. pp->dbi_base + PCI_IO_BASE);
  225. /* update the Vendor ID */
  226. writew(ks_pcie->device_id, pp->dbi_base + PCI_DEVICE_ID);
  227. /* update the DEV_STAT_CTRL to publish right mrrs */
  228. val = readl(pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  229. val &= ~PCI_EXP_DEVCTL_READRQ;
  230. /* set the mrrs to 256 bytes */
  231. val |= BIT(12);
  232. writel(val, pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  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. }
  240. static struct pcie_host_ops keystone_pcie_host_ops = {
  241. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  242. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  243. .link_up = ks_dw_pcie_link_up,
  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. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  250. };
  251. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  252. struct platform_device *pdev)
  253. {
  254. struct pcie_port *pp = &ks_pcie->pp;
  255. int ret;
  256. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  257. "legacy-interrupt-controller",
  258. &ks_pcie->num_legacy_host_irqs);
  259. if (ret)
  260. return ret;
  261. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  262. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  263. "msi-interrupt-controller",
  264. &ks_pcie->num_msi_host_irqs);
  265. if (ret)
  266. return ret;
  267. }
  268. pp->root_bus_nr = -1;
  269. pp->ops = &keystone_pcie_host_ops;
  270. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  271. if (ret) {
  272. dev_err(&pdev->dev, "failed to initialize host\n");
  273. return ret;
  274. }
  275. return ret;
  276. }
  277. static const struct of_device_id ks_pcie_of_match[] = {
  278. {
  279. .type = "pci",
  280. .compatible = "ti,keystone-pcie",
  281. },
  282. { },
  283. };
  284. MODULE_DEVICE_TABLE(of, ks_pcie_of_match);
  285. static int __exit ks_pcie_remove(struct platform_device *pdev)
  286. {
  287. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  288. clk_disable_unprepare(ks_pcie->clk);
  289. return 0;
  290. }
  291. static int __init ks_pcie_probe(struct platform_device *pdev)
  292. {
  293. struct device *dev = &pdev->dev;
  294. struct keystone_pcie *ks_pcie;
  295. struct pcie_port *pp;
  296. struct resource *res;
  297. void __iomem *reg_p;
  298. struct phy *phy;
  299. int ret = 0;
  300. ks_pcie = devm_kzalloc(&pdev->dev, sizeof(*ks_pcie),
  301. GFP_KERNEL);
  302. if (!ks_pcie)
  303. return -ENOMEM;
  304. pp = &ks_pcie->pp;
  305. /* initialize SerDes Phy if present */
  306. phy = devm_phy_get(dev, "pcie-phy");
  307. if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
  308. return PTR_ERR(phy);
  309. if (!IS_ERR_OR_NULL(phy)) {
  310. ret = phy_init(phy);
  311. if (ret < 0)
  312. return ret;
  313. }
  314. /* index 2 is to read PCI DEVICE_ID */
  315. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  316. reg_p = devm_ioremap_resource(dev, res);
  317. if (IS_ERR(reg_p))
  318. return PTR_ERR(reg_p);
  319. ks_pcie->device_id = readl(reg_p) >> 16;
  320. devm_iounmap(dev, reg_p);
  321. devm_release_mem_region(dev, res->start, resource_size(res));
  322. pp->dev = dev;
  323. platform_set_drvdata(pdev, ks_pcie);
  324. ks_pcie->clk = devm_clk_get(dev, "pcie");
  325. if (IS_ERR(ks_pcie->clk)) {
  326. dev_err(dev, "Failed to get pcie rc clock\n");
  327. return PTR_ERR(ks_pcie->clk);
  328. }
  329. ret = clk_prepare_enable(ks_pcie->clk);
  330. if (ret)
  331. return ret;
  332. ret = ks_add_pcie_port(ks_pcie, pdev);
  333. if (ret < 0)
  334. goto fail_clk;
  335. return 0;
  336. fail_clk:
  337. clk_disable_unprepare(ks_pcie->clk);
  338. return ret;
  339. }
  340. static struct platform_driver ks_pcie_driver __refdata = {
  341. .probe = ks_pcie_probe,
  342. .remove = __exit_p(ks_pcie_remove),
  343. .driver = {
  344. .name = "keystone-pcie",
  345. .of_match_table = of_match_ptr(ks_pcie_of_match),
  346. },
  347. };
  348. module_platform_driver(ks_pcie_driver);
  349. MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
  350. MODULE_DESCRIPTION("Keystone PCIe host controller driver");
  351. MODULE_LICENSE("GPL v2");