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. int count = 200;
  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. while (!dw_pcie_link_up(pp)) {
  88. usleep_range(100, 1000);
  89. if (--count) {
  90. ks_dw_pcie_initiate_link_train(ks_pcie);
  91. continue;
  92. }
  93. dev_err(pp->dev, "phy link never came up\n");
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. static void ks_pcie_msi_irq_handler(unsigned int irq, struct irq_desc *desc)
  99. {
  100. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  101. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  102. struct pcie_port *pp = &ks_pcie->pp;
  103. struct irq_chip *chip = irq_desc_get_chip(desc);
  104. dev_dbg(pp->dev, "%s, irq %d\n", __func__, irq);
  105. /*
  106. * The chained irq handler installation would have replaced normal
  107. * interrupt driver handler so we need to take care of mask/unmask and
  108. * ack operation.
  109. */
  110. chained_irq_enter(chip, desc);
  111. ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
  112. chained_irq_exit(chip, desc);
  113. }
  114. /**
  115. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  116. * @irq: IRQ line for legacy interrupts
  117. * @desc: Pointer to irq descriptor
  118. *
  119. * Traverse through pending legacy interrupts and invoke handler for each. Also
  120. * takes care of interrupt controller level mask/ack operation.
  121. */
  122. static void ks_pcie_legacy_irq_handler(unsigned int irq, struct irq_desc *desc)
  123. {
  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_find_node_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. goto out;
  164. if (temp > max_host_irqs)
  165. dev_warn(dev, "Too many %s interrupts defined %u\n",
  166. (legacy ? "legacy" : "MSI"), temp);
  167. /*
  168. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  169. * 7 (MSI)
  170. */
  171. for (temp = 0; temp < max_host_irqs; temp++) {
  172. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  173. if (!host_irqs[temp])
  174. break;
  175. }
  176. if (temp) {
  177. *num_irqs = temp;
  178. ret = 0;
  179. }
  180. out:
  181. return ret;
  182. }
  183. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  184. {
  185. int i;
  186. /* Legacy IRQ */
  187. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  188. irq_set_handler_data(ks_pcie->legacy_host_irqs[i], ks_pcie);
  189. irq_set_chained_handler(ks_pcie->legacy_host_irqs[i],
  190. ks_pcie_legacy_irq_handler);
  191. }
  192. ks_dw_pcie_enable_legacy_irqs(ks_pcie);
  193. /* MSI IRQ */
  194. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  195. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  196. irq_set_chained_handler(ks_pcie->msi_host_irqs[i],
  197. ks_pcie_msi_irq_handler);
  198. irq_set_handler_data(ks_pcie->msi_host_irqs[i],
  199. ks_pcie);
  200. }
  201. }
  202. }
  203. /*
  204. * When a PCI device does not exist during config cycles, keystone host gets a
  205. * bus error instead of returning 0xffffffff. This handler always returns 0
  206. * for this kind of faults.
  207. */
  208. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  209. struct pt_regs *regs)
  210. {
  211. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  212. if ((instr & 0x0e100090) == 0x00100090) {
  213. int reg = (instr >> 12) & 15;
  214. regs->uregs[reg] = -1;
  215. regs->ARM_pc += 4;
  216. }
  217. return 0;
  218. }
  219. static void __init ks_pcie_host_init(struct pcie_port *pp)
  220. {
  221. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  222. u32 val;
  223. ks_pcie_establish_link(ks_pcie);
  224. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  225. ks_pcie_setup_interrupts(ks_pcie);
  226. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  227. pp->dbi_base + PCI_IO_BASE);
  228. /* update the Vendor ID */
  229. writew(ks_pcie->device_id, pp->dbi_base + PCI_DEVICE_ID);
  230. /* update the DEV_STAT_CTRL to publish right mrrs */
  231. val = readl(pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  232. val &= ~PCI_EXP_DEVCTL_READRQ;
  233. /* set the mrrs to 256 bytes */
  234. val |= BIT(12);
  235. writel(val, pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  236. /*
  237. * PCIe access errors that result into OCP errors are caught by ARM as
  238. * "External aborts"
  239. */
  240. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  241. "Asynchronous external abort");
  242. }
  243. static struct pcie_host_ops keystone_pcie_host_ops = {
  244. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  245. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  246. .link_up = ks_dw_pcie_link_up,
  247. .host_init = ks_pcie_host_init,
  248. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  249. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  250. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  251. .msi_host_init = ks_dw_pcie_msi_host_init,
  252. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  253. };
  254. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  255. struct platform_device *pdev)
  256. {
  257. struct pcie_port *pp = &ks_pcie->pp;
  258. int ret;
  259. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  260. "legacy-interrupt-controller",
  261. &ks_pcie->num_legacy_host_irqs);
  262. if (ret)
  263. return ret;
  264. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  265. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  266. "msi-interrupt-controller",
  267. &ks_pcie->num_msi_host_irqs);
  268. if (ret)
  269. return ret;
  270. }
  271. pp->root_bus_nr = -1;
  272. pp->ops = &keystone_pcie_host_ops;
  273. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  274. if (ret) {
  275. dev_err(&pdev->dev, "failed to initialize host\n");
  276. return ret;
  277. }
  278. return ret;
  279. }
  280. static const struct of_device_id ks_pcie_of_match[] = {
  281. {
  282. .type = "pci",
  283. .compatible = "ti,keystone-pcie",
  284. },
  285. { },
  286. };
  287. MODULE_DEVICE_TABLE(of, ks_pcie_of_match);
  288. static int __exit ks_pcie_remove(struct platform_device *pdev)
  289. {
  290. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  291. clk_disable_unprepare(ks_pcie->clk);
  292. return 0;
  293. }
  294. static int __init ks_pcie_probe(struct platform_device *pdev)
  295. {
  296. struct device *dev = &pdev->dev;
  297. struct keystone_pcie *ks_pcie;
  298. struct pcie_port *pp;
  299. struct resource *res;
  300. void __iomem *reg_p;
  301. struct phy *phy;
  302. int ret = 0;
  303. ks_pcie = devm_kzalloc(&pdev->dev, sizeof(*ks_pcie),
  304. GFP_KERNEL);
  305. if (!ks_pcie)
  306. return -ENOMEM;
  307. pp = &ks_pcie->pp;
  308. /* initialize SerDes Phy if present */
  309. phy = devm_phy_get(dev, "pcie-phy");
  310. if (!IS_ERR_OR_NULL(phy)) {
  311. ret = phy_init(phy);
  312. if (ret < 0)
  313. return ret;
  314. }
  315. /* index 2 is to read PCI DEVICE_ID */
  316. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  317. reg_p = devm_ioremap_resource(dev, res);
  318. if (IS_ERR(reg_p))
  319. return PTR_ERR(reg_p);
  320. ks_pcie->device_id = readl(reg_p) >> 16;
  321. devm_iounmap(dev, reg_p);
  322. devm_release_mem_region(dev, res->start, resource_size(res));
  323. pp->dev = dev;
  324. platform_set_drvdata(pdev, ks_pcie);
  325. ks_pcie->clk = devm_clk_get(dev, "pcie");
  326. if (IS_ERR(ks_pcie->clk)) {
  327. dev_err(dev, "Failed to get pcie rc clock\n");
  328. return PTR_ERR(ks_pcie->clk);
  329. }
  330. ret = clk_prepare_enable(ks_pcie->clk);
  331. if (ret)
  332. return ret;
  333. ret = ks_add_pcie_port(ks_pcie, pdev);
  334. if (ret < 0)
  335. goto fail_clk;
  336. return 0;
  337. fail_clk:
  338. clk_disable_unprepare(ks_pcie->clk);
  339. return ret;
  340. }
  341. static struct platform_driver ks_pcie_driver __refdata = {
  342. .probe = ks_pcie_probe,
  343. .remove = __exit_p(ks_pcie_remove),
  344. .driver = {
  345. .name = "keystone-pcie",
  346. .of_match_table = of_match_ptr(ks_pcie_of_match),
  347. },
  348. };
  349. module_platform_driver(ks_pcie_driver);
  350. MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
  351. MODULE_DESCRIPTION("Keystone PCIe host controller driver");
  352. MODULE_LICENSE("GPL v2");