pci-keystone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. ks_dw_pcie_initiate_link_train(ks_pcie);
  86. /* check if the link is up or not */
  87. for (retries = 0; retries < 200; retries++) {
  88. if (dw_pcie_link_up(pp))
  89. return 0;
  90. usleep_range(100, 1000);
  91. ks_dw_pcie_initiate_link_train(ks_pcie);
  92. }
  93. dev_err(pp->dev, "phy link never came up\n");
  94. return -EINVAL;
  95. }
  96. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  97. {
  98. unsigned int irq = irq_desc_get_irq(desc);
  99. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  100. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  101. struct pcie_port *pp = &ks_pcie->pp;
  102. struct irq_chip *chip = irq_desc_get_chip(desc);
  103. dev_dbg(pp->dev, "%s, irq %d\n", __func__, irq);
  104. /*
  105. * The chained irq handler installation would have replaced normal
  106. * interrupt driver handler so we need to take care of mask/unmask and
  107. * ack operation.
  108. */
  109. chained_irq_enter(chip, desc);
  110. ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
  111. chained_irq_exit(chip, desc);
  112. }
  113. /**
  114. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  115. * @irq: IRQ line for legacy interrupts
  116. * @desc: Pointer to irq descriptor
  117. *
  118. * Traverse through pending legacy interrupts and invoke handler for each. Also
  119. * takes care of interrupt controller level mask/ack operation.
  120. */
  121. static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
  122. {
  123. unsigned int irq = irq_desc_get_irq(desc);
  124. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  125. struct pcie_port *pp = &ks_pcie->pp;
  126. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  127. struct irq_chip *chip = irq_desc_get_chip(desc);
  128. dev_dbg(pp->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, ret = -EINVAL;
  142. struct device *dev = ks_pcie->pp.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 = MAX_LEGACY_HOST_IRQS;
  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_get_child_by_name(np_pcie, controller);
  157. if (!(*np_temp)) {
  158. dev_err(dev, "Node for %s is absent\n", controller);
  159. goto out;
  160. }
  161. temp = of_irq_count(*np_temp);
  162. if (!temp) {
  163. of_node_put(*np_temp);
  164. goto out;
  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. of_node_put(*np_temp);
  179. if (temp) {
  180. *num_irqs = temp;
  181. ret = 0;
  182. }
  183. out:
  184. return ret;
  185. }
  186. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  187. {
  188. int i;
  189. /* Legacy IRQ */
  190. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  191. irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  192. ks_pcie_legacy_irq_handler,
  193. ks_pcie);
  194. }
  195. ks_dw_pcie_enable_legacy_irqs(ks_pcie);
  196. /* MSI IRQ */
  197. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  198. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  199. irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
  200. ks_pcie_msi_irq_handler,
  201. ks_pcie);
  202. }
  203. }
  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 void __init ks_pcie_host_init(struct pcie_port *pp)
  222. {
  223. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  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. pp->dbi_base + PCI_IO_BASE);
  230. /* update the Vendor ID */
  231. writew(ks_pcie->device_id, pp->dbi_base + PCI_DEVICE_ID);
  232. /* update the DEV_STAT_CTRL to publish right mrrs */
  233. val = readl(pp->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, pp->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. }
  245. static struct pcie_host_ops keystone_pcie_host_ops = {
  246. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  247. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  248. .link_up = ks_dw_pcie_link_up,
  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 int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  257. struct platform_device *pdev)
  258. {
  259. struct pcie_port *pp = &ks_pcie->pp;
  260. int ret;
  261. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  262. "legacy-interrupt-controller",
  263. &ks_pcie->num_legacy_host_irqs);
  264. if (ret)
  265. return ret;
  266. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  267. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  268. "msi-interrupt-controller",
  269. &ks_pcie->num_msi_host_irqs);
  270. if (ret)
  271. return ret;
  272. }
  273. pp->root_bus_nr = -1;
  274. pp->ops = &keystone_pcie_host_ops;
  275. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  276. if (ret) {
  277. dev_err(&pdev->dev, "failed to initialize host\n");
  278. return ret;
  279. }
  280. return ret;
  281. }
  282. static const struct of_device_id ks_pcie_of_match[] = {
  283. {
  284. .type = "pci",
  285. .compatible = "ti,keystone-pcie",
  286. },
  287. { },
  288. };
  289. MODULE_DEVICE_TABLE(of, ks_pcie_of_match);
  290. static int __exit ks_pcie_remove(struct platform_device *pdev)
  291. {
  292. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  293. clk_disable_unprepare(ks_pcie->clk);
  294. return 0;
  295. }
  296. static int __init ks_pcie_probe(struct platform_device *pdev)
  297. {
  298. struct device *dev = &pdev->dev;
  299. struct keystone_pcie *ks_pcie;
  300. struct pcie_port *pp;
  301. struct resource *res;
  302. void __iomem *reg_p;
  303. struct phy *phy;
  304. int ret = 0;
  305. ks_pcie = devm_kzalloc(&pdev->dev, sizeof(*ks_pcie),
  306. GFP_KERNEL);
  307. if (!ks_pcie)
  308. return -ENOMEM;
  309. pp = &ks_pcie->pp;
  310. /* initialize SerDes Phy if present */
  311. phy = devm_phy_get(dev, "pcie-phy");
  312. if (!IS_ERR_OR_NULL(phy)) {
  313. ret = phy_init(phy);
  314. if (ret < 0)
  315. return ret;
  316. }
  317. /* index 2 is to read PCI DEVICE_ID */
  318. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  319. reg_p = devm_ioremap_resource(dev, res);
  320. if (IS_ERR(reg_p))
  321. return PTR_ERR(reg_p);
  322. ks_pcie->device_id = readl(reg_p) >> 16;
  323. devm_iounmap(dev, reg_p);
  324. devm_release_mem_region(dev, res->start, resource_size(res));
  325. pp->dev = dev;
  326. platform_set_drvdata(pdev, ks_pcie);
  327. ks_pcie->clk = devm_clk_get(dev, "pcie");
  328. if (IS_ERR(ks_pcie->clk)) {
  329. dev_err(dev, "Failed to get pcie rc clock\n");
  330. return PTR_ERR(ks_pcie->clk);
  331. }
  332. ret = clk_prepare_enable(ks_pcie->clk);
  333. if (ret)
  334. return ret;
  335. ret = ks_add_pcie_port(ks_pcie, pdev);
  336. if (ret < 0)
  337. goto fail_clk;
  338. return 0;
  339. fail_clk:
  340. clk_disable_unprepare(ks_pcie->clk);
  341. return ret;
  342. }
  343. static struct platform_driver ks_pcie_driver __refdata = {
  344. .probe = ks_pcie_probe,
  345. .remove = __exit_p(ks_pcie_remove),
  346. .driver = {
  347. .name = "keystone-pcie",
  348. .of_match_table = of_match_ptr(ks_pcie_of_match),
  349. },
  350. };
  351. module_platform_driver(ks_pcie_driver);
  352. MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
  353. MODULE_DESCRIPTION("Keystone PCIe host controller driver");
  354. MODULE_LICENSE("GPL v2");