pci-keystone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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) container_of(x, struct keystone_pcie, pp)
  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 pcie_port *pp = &ks_pcie->pp;
  80. unsigned int retries;
  81. dw_pcie_setup_rc(pp);
  82. if (dw_pcie_link_up(pp)) {
  83. dev_err(pp->dev, "Link already up\n");
  84. return 0;
  85. }
  86. /* check if the link is up or not */
  87. for (retries = 0; retries < 5; retries++) {
  88. ks_dw_pcie_initiate_link_train(ks_pcie);
  89. if (!dw_pcie_wait_for_link(pp))
  90. return 0;
  91. }
  92. dev_err(pp->dev, "phy link never came up\n");
  93. return -ETIMEDOUT;
  94. }
  95. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  96. {
  97. unsigned int irq = irq_desc_get_irq(desc);
  98. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  99. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  100. struct pcie_port *pp = &ks_pcie->pp;
  101. struct irq_chip *chip = irq_desc_get_chip(desc);
  102. dev_dbg(pp->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 pcie_port *pp = &ks_pcie->pp;
  125. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  126. struct irq_chip *chip = irq_desc_get_chip(desc);
  127. dev_dbg(pp->dev, ": Handling legacy irq %d\n", irq);
  128. /*
  129. * The chained irq handler installation would have replaced normal
  130. * interrupt driver handler so we need to take care of mask/unmask and
  131. * ack operation.
  132. */
  133. chained_irq_enter(chip, desc);
  134. ks_dw_pcie_handle_legacy_irq(ks_pcie, irq_offset);
  135. chained_irq_exit(chip, desc);
  136. }
  137. static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
  138. char *controller, int *num_irqs)
  139. {
  140. int temp, max_host_irqs, legacy = 1, *host_irqs;
  141. struct device *dev = ks_pcie->pp.dev;
  142. struct device_node *np_pcie = dev->of_node, **np_temp;
  143. if (!strcmp(controller, "msi-interrupt-controller"))
  144. legacy = 0;
  145. if (legacy) {
  146. np_temp = &ks_pcie->legacy_intc_np;
  147. max_host_irqs = MAX_LEGACY_HOST_IRQS;
  148. host_irqs = &ks_pcie->legacy_host_irqs[0];
  149. } else {
  150. np_temp = &ks_pcie->msi_intc_np;
  151. max_host_irqs = MAX_MSI_HOST_IRQS;
  152. host_irqs = &ks_pcie->msi_host_irqs[0];
  153. }
  154. /* interrupt controller is in a child node */
  155. *np_temp = of_find_node_by_name(np_pcie, controller);
  156. if (!(*np_temp)) {
  157. dev_err(dev, "Node for %s is absent\n", controller);
  158. return -EINVAL;
  159. }
  160. temp = of_irq_count(*np_temp);
  161. if (!temp) {
  162. dev_err(dev, "No IRQ entries in %s\n", controller);
  163. return -EINVAL;
  164. }
  165. if (temp > max_host_irqs)
  166. dev_warn(dev, "Too many %s interrupts defined %u\n",
  167. (legacy ? "legacy" : "MSI"), temp);
  168. /*
  169. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  170. * 7 (MSI)
  171. */
  172. for (temp = 0; temp < max_host_irqs; temp++) {
  173. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  174. if (!host_irqs[temp])
  175. break;
  176. }
  177. if (temp) {
  178. *num_irqs = temp;
  179. return 0;
  180. }
  181. return -EINVAL;
  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_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  189. ks_pcie_legacy_irq_handler,
  190. ks_pcie);
  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_and_data(ks_pcie->msi_host_irqs[i],
  197. ks_pcie_msi_irq_handler,
  198. ks_pcie);
  199. }
  200. }
  201. if (ks_pcie->error_irq > 0)
  202. ks_dw_pcie_enable_error_irq(ks_pcie->va_app_base);
  203. }
  204. /*
  205. * When a PCI device does not exist during config cycles, keystone host gets a
  206. * bus error instead of returning 0xffffffff. This handler always returns 0
  207. * for this kind of faults.
  208. */
  209. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  210. struct pt_regs *regs)
  211. {
  212. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  213. if ((instr & 0x0e100090) == 0x00100090) {
  214. int reg = (instr >> 12) & 15;
  215. regs->uregs[reg] = -1;
  216. regs->ARM_pc += 4;
  217. }
  218. return 0;
  219. }
  220. static void __init ks_pcie_host_init(struct pcie_port *pp)
  221. {
  222. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  223. u32 val;
  224. ks_pcie_establish_link(ks_pcie);
  225. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  226. ks_pcie_setup_interrupts(ks_pcie);
  227. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  228. pp->dbi_base + PCI_IO_BASE);
  229. /* update the Vendor ID */
  230. writew(ks_pcie->device_id, pp->dbi_base + PCI_DEVICE_ID);
  231. /* update the DEV_STAT_CTRL to publish right mrrs */
  232. val = readl(pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  233. val &= ~PCI_EXP_DEVCTL_READRQ;
  234. /* set the mrrs to 256 bytes */
  235. val |= BIT(12);
  236. writel(val, pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  237. /*
  238. * PCIe access errors that result into OCP errors are caught by ARM as
  239. * "External aborts"
  240. */
  241. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  242. "Asynchronous external abort");
  243. }
  244. static struct pcie_host_ops keystone_pcie_host_ops = {
  245. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  246. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  247. .link_up = ks_dw_pcie_link_up,
  248. .host_init = ks_pcie_host_init,
  249. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  250. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  251. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  252. .msi_host_init = ks_dw_pcie_msi_host_init,
  253. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  254. };
  255. static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
  256. {
  257. struct keystone_pcie *ks_pcie = priv;
  258. return ks_dw_pcie_handle_error_irq(ks_pcie->pp.dev,
  259. ks_pcie->va_app_base);
  260. }
  261. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  262. struct platform_device *pdev)
  263. {
  264. struct pcie_port *pp = &ks_pcie->pp;
  265. int ret;
  266. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  267. "legacy-interrupt-controller",
  268. &ks_pcie->num_legacy_host_irqs);
  269. if (ret)
  270. return ret;
  271. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  272. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  273. "msi-interrupt-controller",
  274. &ks_pcie->num_msi_host_irqs);
  275. if (ret)
  276. return ret;
  277. }
  278. /*
  279. * Index 0 is the platform interrupt for error interrupt
  280. * from RC. This is optional.
  281. */
  282. ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
  283. if (ks_pcie->error_irq <= 0)
  284. dev_info(&pdev->dev, "no error IRQ defined\n");
  285. else {
  286. if (request_irq(ks_pcie->error_irq, pcie_err_irq_handler,
  287. IRQF_SHARED, "pcie-error-irq", ks_pcie) < 0) {
  288. dev_err(&pdev->dev, "failed to request error IRQ %d\n",
  289. ks_pcie->error_irq);
  290. return ret;
  291. }
  292. }
  293. pp->root_bus_nr = -1;
  294. pp->ops = &keystone_pcie_host_ops;
  295. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  296. if (ret) {
  297. dev_err(&pdev->dev, "failed to initialize host\n");
  298. return ret;
  299. }
  300. return 0;
  301. }
  302. static const struct of_device_id ks_pcie_of_match[] = {
  303. {
  304. .type = "pci",
  305. .compatible = "ti,keystone-pcie",
  306. },
  307. { },
  308. };
  309. static int __exit ks_pcie_remove(struct platform_device *pdev)
  310. {
  311. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  312. clk_disable_unprepare(ks_pcie->clk);
  313. return 0;
  314. }
  315. static int __init ks_pcie_probe(struct platform_device *pdev)
  316. {
  317. struct device *dev = &pdev->dev;
  318. struct keystone_pcie *ks_pcie;
  319. struct pcie_port *pp;
  320. struct resource *res;
  321. void __iomem *reg_p;
  322. struct phy *phy;
  323. int ret;
  324. ks_pcie = devm_kzalloc(&pdev->dev, sizeof(*ks_pcie),
  325. GFP_KERNEL);
  326. if (!ks_pcie)
  327. return -ENOMEM;
  328. pp = &ks_pcie->pp;
  329. /* initialize SerDes Phy if present */
  330. phy = devm_phy_get(dev, "pcie-phy");
  331. if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
  332. return PTR_ERR(phy);
  333. if (!IS_ERR_OR_NULL(phy)) {
  334. ret = phy_init(phy);
  335. if (ret < 0)
  336. return ret;
  337. }
  338. /* index 2 is to read PCI DEVICE_ID */
  339. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  340. reg_p = devm_ioremap_resource(dev, res);
  341. if (IS_ERR(reg_p))
  342. return PTR_ERR(reg_p);
  343. ks_pcie->device_id = readl(reg_p) >> 16;
  344. devm_iounmap(dev, reg_p);
  345. devm_release_mem_region(dev, res->start, resource_size(res));
  346. pp->dev = dev;
  347. ks_pcie->np = dev->of_node;
  348. platform_set_drvdata(pdev, ks_pcie);
  349. ks_pcie->clk = devm_clk_get(dev, "pcie");
  350. if (IS_ERR(ks_pcie->clk)) {
  351. dev_err(dev, "Failed to get pcie rc clock\n");
  352. return PTR_ERR(ks_pcie->clk);
  353. }
  354. ret = clk_prepare_enable(ks_pcie->clk);
  355. if (ret)
  356. return ret;
  357. ret = ks_add_pcie_port(ks_pcie, pdev);
  358. if (ret < 0)
  359. goto fail_clk;
  360. return 0;
  361. fail_clk:
  362. clk_disable_unprepare(ks_pcie->clk);
  363. return ret;
  364. }
  365. static struct platform_driver ks_pcie_driver __refdata = {
  366. .probe = ks_pcie_probe,
  367. .remove = __exit_p(ks_pcie_remove),
  368. .driver = {
  369. .name = "keystone-pcie",
  370. .of_match_table = of_match_ptr(ks_pcie_of_match),
  371. },
  372. };
  373. builtin_platform_driver(ks_pcie_driver);