pci-dra7xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
  3. *
  4. * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Kishon Vijay Abraham I <kishon@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/pci.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/resource.h>
  25. #include <linux/types.h>
  26. #include "pcie-designware.h"
  27. /* PCIe controller wrapper DRA7XX configuration registers */
  28. #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024
  29. #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028
  30. #define ERR_SYS BIT(0)
  31. #define ERR_FATAL BIT(1)
  32. #define ERR_NONFATAL BIT(2)
  33. #define ERR_COR BIT(3)
  34. #define ERR_AXI BIT(4)
  35. #define ERR_ECRC BIT(5)
  36. #define PME_TURN_OFF BIT(8)
  37. #define PME_TO_ACK BIT(9)
  38. #define PM_PME BIT(10)
  39. #define LINK_REQ_RST BIT(11)
  40. #define LINK_UP_EVT BIT(12)
  41. #define CFG_BME_EVT BIT(13)
  42. #define CFG_MSE_EVT BIT(14)
  43. #define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
  44. ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
  45. LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
  46. #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034
  47. #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038
  48. #define INTA BIT(0)
  49. #define INTB BIT(1)
  50. #define INTC BIT(2)
  51. #define INTD BIT(3)
  52. #define MSI BIT(4)
  53. #define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
  54. #define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104
  55. #define LTSSM_EN 0x1
  56. #define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C
  57. #define LINK_UP BIT(16)
  58. #define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF
  59. #define EXP_CAP_ID_OFFSET 0x70
  60. struct dra7xx_pcie {
  61. struct dw_pcie *pci;
  62. void __iomem *base; /* DT ti_conf */
  63. int phy_count; /* DT phy-names count */
  64. struct phy **phy;
  65. int link_gen;
  66. struct irq_domain *irq_domain;
  67. };
  68. #define to_dra7xx_pcie(x) dev_get_drvdata((x)->dev)
  69. static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
  70. {
  71. return readl(pcie->base + offset);
  72. }
  73. static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
  74. u32 value)
  75. {
  76. writel(value, pcie->base + offset);
  77. }
  78. static int dra7xx_pcie_link_up(struct dw_pcie *pci)
  79. {
  80. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  81. u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
  82. return !!(reg & LINK_UP);
  83. }
  84. static int dra7xx_pcie_establish_link(struct dra7xx_pcie *dra7xx)
  85. {
  86. struct dw_pcie *pci = dra7xx->pci;
  87. struct device *dev = pci->dev;
  88. u32 reg;
  89. u32 exp_cap_off = EXP_CAP_ID_OFFSET;
  90. if (dw_pcie_link_up(pci)) {
  91. dev_err(dev, "link is already up\n");
  92. return 0;
  93. }
  94. if (dra7xx->link_gen == 1) {
  95. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
  96. 4, &reg);
  97. if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  98. reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
  99. reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
  100. dw_pcie_write(pci->dbi_base + exp_cap_off +
  101. PCI_EXP_LNKCAP, 4, reg);
  102. }
  103. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
  104. 2, &reg);
  105. if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  106. reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
  107. reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
  108. dw_pcie_write(pci->dbi_base + exp_cap_off +
  109. PCI_EXP_LNKCTL2, 2, reg);
  110. }
  111. }
  112. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  113. reg |= LTSSM_EN;
  114. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  115. return dw_pcie_wait_for_link(pci);
  116. }
  117. static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
  118. {
  119. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
  120. ~INTERRUPTS);
  121. dra7xx_pcie_writel(dra7xx,
  122. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
  123. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
  124. ~LEG_EP_INTERRUPTS & ~MSI);
  125. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
  126. MSI | LEG_EP_INTERRUPTS);
  127. }
  128. static void dra7xx_pcie_host_init(struct pcie_port *pp)
  129. {
  130. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  131. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  132. pp->io_base &= DRA7XX_CPU_TO_BUS_ADDR;
  133. pp->mem_base &= DRA7XX_CPU_TO_BUS_ADDR;
  134. pp->cfg0_base &= DRA7XX_CPU_TO_BUS_ADDR;
  135. pp->cfg1_base &= DRA7XX_CPU_TO_BUS_ADDR;
  136. dw_pcie_setup_rc(pp);
  137. dra7xx_pcie_establish_link(dra7xx);
  138. dw_pcie_msi_init(pp);
  139. dra7xx_pcie_enable_interrupts(dra7xx);
  140. }
  141. static struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
  142. .host_init = dra7xx_pcie_host_init,
  143. };
  144. static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  145. irq_hw_number_t hwirq)
  146. {
  147. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  148. irq_set_chip_data(irq, domain->host_data);
  149. return 0;
  150. }
  151. static const struct irq_domain_ops intx_domain_ops = {
  152. .map = dra7xx_pcie_intx_map,
  153. };
  154. static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
  155. {
  156. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  157. struct device *dev = pci->dev;
  158. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  159. struct device_node *node = dev->of_node;
  160. struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
  161. if (!pcie_intc_node) {
  162. dev_err(dev, "No PCIe Intc node found\n");
  163. return -ENODEV;
  164. }
  165. dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
  166. &intx_domain_ops, pp);
  167. if (!dra7xx->irq_domain) {
  168. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  169. return -ENODEV;
  170. }
  171. return 0;
  172. }
  173. static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
  174. {
  175. struct dra7xx_pcie *dra7xx = arg;
  176. struct dw_pcie *pci = dra7xx->pci;
  177. struct pcie_port *pp = &pci->pp;
  178. u32 reg;
  179. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
  180. switch (reg) {
  181. case MSI:
  182. dw_handle_msi_irq(pp);
  183. break;
  184. case INTA:
  185. case INTB:
  186. case INTC:
  187. case INTD:
  188. generic_handle_irq(irq_find_mapping(dra7xx->irq_domain,
  189. ffs(reg)));
  190. break;
  191. }
  192. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
  193. return IRQ_HANDLED;
  194. }
  195. static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
  196. {
  197. struct dra7xx_pcie *dra7xx = arg;
  198. struct dw_pcie *pci = dra7xx->pci;
  199. struct device *dev = pci->dev;
  200. u32 reg;
  201. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
  202. if (reg & ERR_SYS)
  203. dev_dbg(dev, "System Error\n");
  204. if (reg & ERR_FATAL)
  205. dev_dbg(dev, "Fatal Error\n");
  206. if (reg & ERR_NONFATAL)
  207. dev_dbg(dev, "Non Fatal Error\n");
  208. if (reg & ERR_COR)
  209. dev_dbg(dev, "Correctable Error\n");
  210. if (reg & ERR_AXI)
  211. dev_dbg(dev, "AXI tag lookup fatal Error\n");
  212. if (reg & ERR_ECRC)
  213. dev_dbg(dev, "ECRC Error\n");
  214. if (reg & PME_TURN_OFF)
  215. dev_dbg(dev,
  216. "Power Management Event Turn-Off message received\n");
  217. if (reg & PME_TO_ACK)
  218. dev_dbg(dev,
  219. "Power Management Turn-Off Ack message received\n");
  220. if (reg & PM_PME)
  221. dev_dbg(dev, "PM Power Management Event message received\n");
  222. if (reg & LINK_REQ_RST)
  223. dev_dbg(dev, "Link Request Reset\n");
  224. if (reg & LINK_UP_EVT)
  225. dev_dbg(dev, "Link-up state change\n");
  226. if (reg & CFG_BME_EVT)
  227. dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
  228. if (reg & CFG_MSE_EVT)
  229. dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
  230. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
  231. return IRQ_HANDLED;
  232. }
  233. static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  234. struct platform_device *pdev)
  235. {
  236. int ret;
  237. struct dw_pcie *pci = dra7xx->pci;
  238. struct pcie_port *pp = &pci->pp;
  239. struct device *dev = pci->dev;
  240. struct resource *res;
  241. pp->irq = platform_get_irq(pdev, 1);
  242. if (pp->irq < 0) {
  243. dev_err(dev, "missing IRQ resource\n");
  244. return -EINVAL;
  245. }
  246. ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler,
  247. IRQF_SHARED | IRQF_NO_THREAD,
  248. "dra7-pcie-msi", dra7xx);
  249. if (ret) {
  250. dev_err(dev, "failed to request irq\n");
  251. return ret;
  252. }
  253. ret = dra7xx_pcie_init_irq_domain(pp);
  254. if (ret < 0)
  255. return ret;
  256. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
  257. pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
  258. if (!pci->dbi_base)
  259. return -ENOMEM;
  260. ret = dw_pcie_host_init(pp);
  261. if (ret) {
  262. dev_err(dev, "failed to initialize host\n");
  263. return ret;
  264. }
  265. return 0;
  266. }
  267. static const struct dw_pcie_ops dw_pcie_ops = {
  268. .link_up = dra7xx_pcie_link_up,
  269. };
  270. static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
  271. {
  272. int phy_count = dra7xx->phy_count;
  273. while (phy_count--) {
  274. phy_power_off(dra7xx->phy[phy_count]);
  275. phy_exit(dra7xx->phy[phy_count]);
  276. }
  277. }
  278. static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
  279. {
  280. int phy_count = dra7xx->phy_count;
  281. int ret;
  282. int i;
  283. for (i = 0; i < phy_count; i++) {
  284. ret = phy_init(dra7xx->phy[i]);
  285. if (ret < 0)
  286. goto err_phy;
  287. ret = phy_power_on(dra7xx->phy[i]);
  288. if (ret < 0) {
  289. phy_exit(dra7xx->phy[i]);
  290. goto err_phy;
  291. }
  292. }
  293. return 0;
  294. err_phy:
  295. while (--i >= 0) {
  296. phy_power_off(dra7xx->phy[i]);
  297. phy_exit(dra7xx->phy[i]);
  298. }
  299. return ret;
  300. }
  301. static int __init dra7xx_pcie_probe(struct platform_device *pdev)
  302. {
  303. u32 reg;
  304. int ret;
  305. int irq;
  306. int i;
  307. int phy_count;
  308. struct phy **phy;
  309. void __iomem *base;
  310. struct resource *res;
  311. struct dw_pcie *pci;
  312. struct pcie_port *pp;
  313. struct dra7xx_pcie *dra7xx;
  314. struct device *dev = &pdev->dev;
  315. struct device_node *np = dev->of_node;
  316. char name[10];
  317. struct gpio_desc *reset;
  318. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  319. if (!dra7xx)
  320. return -ENOMEM;
  321. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  322. if (!pci)
  323. return -ENOMEM;
  324. pci->dev = dev;
  325. pci->ops = &dw_pcie_ops;
  326. pp = &pci->pp;
  327. pp->ops = &dra7xx_pcie_host_ops;
  328. irq = platform_get_irq(pdev, 0);
  329. if (irq < 0) {
  330. dev_err(dev, "missing IRQ resource\n");
  331. return -EINVAL;
  332. }
  333. ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
  334. IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
  335. if (ret) {
  336. dev_err(dev, "failed to request irq\n");
  337. return ret;
  338. }
  339. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
  340. base = devm_ioremap_nocache(dev, res->start, resource_size(res));
  341. if (!base)
  342. return -ENOMEM;
  343. phy_count = of_property_count_strings(np, "phy-names");
  344. if (phy_count < 0) {
  345. dev_err(dev, "unable to find the strings\n");
  346. return phy_count;
  347. }
  348. phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
  349. if (!phy)
  350. return -ENOMEM;
  351. for (i = 0; i < phy_count; i++) {
  352. snprintf(name, sizeof(name), "pcie-phy%d", i);
  353. phy[i] = devm_phy_get(dev, name);
  354. if (IS_ERR(phy[i]))
  355. return PTR_ERR(phy[i]);
  356. }
  357. dra7xx->base = base;
  358. dra7xx->phy = phy;
  359. dra7xx->pci = pci;
  360. dra7xx->phy_count = phy_count;
  361. ret = dra7xx_pcie_enable_phy(dra7xx);
  362. if (ret) {
  363. dev_err(dev, "failed to enable phy\n");
  364. return ret;
  365. }
  366. platform_set_drvdata(pdev, dra7xx);
  367. pm_runtime_enable(dev);
  368. ret = pm_runtime_get_sync(dev);
  369. if (ret < 0) {
  370. dev_err(dev, "pm_runtime_get_sync failed\n");
  371. goto err_get_sync;
  372. }
  373. reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  374. if (IS_ERR(reset)) {
  375. ret = PTR_ERR(reset);
  376. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  377. goto err_gpio;
  378. }
  379. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  380. reg &= ~LTSSM_EN;
  381. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  382. dra7xx->link_gen = of_pci_get_max_link_speed(np);
  383. if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2)
  384. dra7xx->link_gen = 2;
  385. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  386. if (ret < 0)
  387. goto err_gpio;
  388. return 0;
  389. err_gpio:
  390. pm_runtime_put(dev);
  391. err_get_sync:
  392. pm_runtime_disable(dev);
  393. dra7xx_pcie_disable_phy(dra7xx);
  394. return ret;
  395. }
  396. #ifdef CONFIG_PM_SLEEP
  397. static int dra7xx_pcie_suspend(struct device *dev)
  398. {
  399. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  400. struct dw_pcie *pci = dra7xx->pci;
  401. u32 val;
  402. /* clear MSE */
  403. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  404. val &= ~PCI_COMMAND_MEMORY;
  405. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  406. return 0;
  407. }
  408. static int dra7xx_pcie_resume(struct device *dev)
  409. {
  410. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  411. struct dw_pcie *pci = dra7xx->pci;
  412. u32 val;
  413. /* set MSE */
  414. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  415. val |= PCI_COMMAND_MEMORY;
  416. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  417. return 0;
  418. }
  419. static int dra7xx_pcie_suspend_noirq(struct device *dev)
  420. {
  421. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  422. dra7xx_pcie_disable_phy(dra7xx);
  423. return 0;
  424. }
  425. static int dra7xx_pcie_resume_noirq(struct device *dev)
  426. {
  427. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  428. int ret;
  429. ret = dra7xx_pcie_enable_phy(dra7xx);
  430. if (ret) {
  431. dev_err(dev, "failed to enable phy\n");
  432. return ret;
  433. }
  434. return 0;
  435. }
  436. #endif
  437. static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
  438. SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
  439. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
  440. dra7xx_pcie_resume_noirq)
  441. };
  442. static const struct of_device_id of_dra7xx_pcie_match[] = {
  443. { .compatible = "ti,dra7-pcie", },
  444. {},
  445. };
  446. static struct platform_driver dra7xx_pcie_driver = {
  447. .driver = {
  448. .name = "dra7-pcie",
  449. .of_match_table = of_dra7xx_pcie_match,
  450. .suppress_bind_attrs = true,
  451. .pm = &dra7xx_pcie_pm_ops,
  452. },
  453. };
  454. builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);