pci-keystone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_get_child_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. of_node_put(*np_temp);
  167. return -EINVAL;
  168. }
  169. if (temp > max_host_irqs)
  170. dev_warn(dev, "Too many %s interrupts defined %u\n",
  171. (legacy ? "legacy" : "MSI"), temp);
  172. /*
  173. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  174. * 7 (MSI)
  175. */
  176. for (temp = 0; temp < max_host_irqs; temp++) {
  177. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  178. if (!host_irqs[temp])
  179. break;
  180. }
  181. of_node_put(*np_temp);
  182. if (temp) {
  183. *num_irqs = temp;
  184. return 0;
  185. }
  186. return -EINVAL;
  187. }
  188. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  189. {
  190. int i;
  191. /* Legacy IRQ */
  192. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  193. irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  194. ks_pcie_legacy_irq_handler,
  195. ks_pcie);
  196. }
  197. ks_dw_pcie_enable_legacy_irqs(ks_pcie);
  198. /* MSI IRQ */
  199. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  200. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  201. irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
  202. ks_pcie_msi_irq_handler,
  203. ks_pcie);
  204. }
  205. }
  206. if (ks_pcie->error_irq > 0)
  207. ks_dw_pcie_enable_error_irq(ks_pcie);
  208. }
  209. /*
  210. * When a PCI device does not exist during config cycles, keystone host gets a
  211. * bus error instead of returning 0xffffffff. This handler always returns 0
  212. * for this kind of faults.
  213. */
  214. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  215. struct pt_regs *regs)
  216. {
  217. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  218. if ((instr & 0x0e100090) == 0x00100090) {
  219. int reg = (instr >> 12) & 15;
  220. regs->uregs[reg] = -1;
  221. regs->ARM_pc += 4;
  222. }
  223. return 0;
  224. }
  225. static void __init ks_pcie_host_init(struct pcie_port *pp)
  226. {
  227. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  228. u32 val;
  229. ks_pcie_establish_link(ks_pcie);
  230. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  231. ks_pcie_setup_interrupts(ks_pcie);
  232. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  233. pp->dbi_base + PCI_IO_BASE);
  234. /* update the Vendor ID */
  235. writew(ks_pcie->device_id, pp->dbi_base + PCI_DEVICE_ID);
  236. /* update the DEV_STAT_CTRL to publish right mrrs */
  237. val = readl(pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  238. val &= ~PCI_EXP_DEVCTL_READRQ;
  239. /* set the mrrs to 256 bytes */
  240. val |= BIT(12);
  241. writel(val, pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  242. /*
  243. * PCIe access errors that result into OCP errors are caught by ARM as
  244. * "External aborts"
  245. */
  246. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  247. "Asynchronous external abort");
  248. }
  249. static struct pcie_host_ops keystone_pcie_host_ops = {
  250. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  251. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  252. .link_up = ks_dw_pcie_link_up,
  253. .host_init = ks_pcie_host_init,
  254. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  255. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  256. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  257. .msi_host_init = ks_dw_pcie_msi_host_init,
  258. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  259. };
  260. static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
  261. {
  262. struct keystone_pcie *ks_pcie = priv;
  263. return ks_dw_pcie_handle_error_irq(ks_pcie);
  264. }
  265. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  266. struct platform_device *pdev)
  267. {
  268. struct pcie_port *pp = &ks_pcie->pp;
  269. struct device *dev = pp->dev;
  270. int ret;
  271. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  272. "legacy-interrupt-controller",
  273. &ks_pcie->num_legacy_host_irqs);
  274. if (ret)
  275. return ret;
  276. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  277. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  278. "msi-interrupt-controller",
  279. &ks_pcie->num_msi_host_irqs);
  280. if (ret)
  281. return ret;
  282. }
  283. /*
  284. * Index 0 is the platform interrupt for error interrupt
  285. * from RC. This is optional.
  286. */
  287. ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
  288. if (ks_pcie->error_irq <= 0)
  289. dev_info(dev, "no error IRQ defined\n");
  290. else {
  291. ret = request_irq(ks_pcie->error_irq, pcie_err_irq_handler,
  292. IRQF_SHARED, "pcie-error-irq", ks_pcie);
  293. if (ret < 0) {
  294. dev_err(dev, "failed to request error IRQ %d\n",
  295. ks_pcie->error_irq);
  296. return ret;
  297. }
  298. }
  299. pp->root_bus_nr = -1;
  300. pp->ops = &keystone_pcie_host_ops;
  301. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  302. if (ret) {
  303. dev_err(dev, "failed to initialize host\n");
  304. return ret;
  305. }
  306. return 0;
  307. }
  308. static const struct of_device_id ks_pcie_of_match[] = {
  309. {
  310. .type = "pci",
  311. .compatible = "ti,keystone-pcie",
  312. },
  313. { },
  314. };
  315. static int __exit ks_pcie_remove(struct platform_device *pdev)
  316. {
  317. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  318. clk_disable_unprepare(ks_pcie->clk);
  319. return 0;
  320. }
  321. static int __init ks_pcie_probe(struct platform_device *pdev)
  322. {
  323. struct device *dev = &pdev->dev;
  324. struct keystone_pcie *ks_pcie;
  325. struct pcie_port *pp;
  326. struct resource *res;
  327. void __iomem *reg_p;
  328. struct phy *phy;
  329. int ret;
  330. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  331. if (!ks_pcie)
  332. return -ENOMEM;
  333. pp = &ks_pcie->pp;
  334. pp->dev = dev;
  335. /* initialize SerDes Phy if present */
  336. phy = devm_phy_get(dev, "pcie-phy");
  337. if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
  338. return PTR_ERR(phy);
  339. if (!IS_ERR_OR_NULL(phy)) {
  340. ret = phy_init(phy);
  341. if (ret < 0)
  342. return ret;
  343. }
  344. /* index 2 is to read PCI DEVICE_ID */
  345. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  346. reg_p = devm_ioremap_resource(dev, res);
  347. if (IS_ERR(reg_p))
  348. return PTR_ERR(reg_p);
  349. ks_pcie->device_id = readl(reg_p) >> 16;
  350. devm_iounmap(dev, reg_p);
  351. devm_release_mem_region(dev, res->start, resource_size(res));
  352. ks_pcie->np = dev->of_node;
  353. platform_set_drvdata(pdev, ks_pcie);
  354. ks_pcie->clk = devm_clk_get(dev, "pcie");
  355. if (IS_ERR(ks_pcie->clk)) {
  356. dev_err(dev, "Failed to get pcie rc clock\n");
  357. return PTR_ERR(ks_pcie->clk);
  358. }
  359. ret = clk_prepare_enable(ks_pcie->clk);
  360. if (ret)
  361. return ret;
  362. ret = ks_add_pcie_port(ks_pcie, pdev);
  363. if (ret < 0)
  364. goto fail_clk;
  365. return 0;
  366. fail_clk:
  367. clk_disable_unprepare(ks_pcie->clk);
  368. return ret;
  369. }
  370. static struct platform_driver ks_pcie_driver __refdata = {
  371. .probe = ks_pcie_probe,
  372. .remove = __exit_p(ks_pcie_remove),
  373. .driver = {
  374. .name = "keystone-pcie",
  375. .of_match_table = of_match_ptr(ks_pcie_of_match),
  376. },
  377. };
  378. builtin_platform_driver(ks_pcie_driver);