pci-keystone.c 11 KB

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