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/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of_gpio.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. struct dra7xx_pcie {
  59. void __iomem *base;
  60. struct phy **phy;
  61. int phy_count;
  62. struct device *dev;
  63. struct pcie_port pp;
  64. };
  65. #define to_dra7xx_pcie(x) container_of((x), struct dra7xx_pcie, pp)
  66. static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
  67. {
  68. return readl(pcie->base + offset);
  69. }
  70. static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
  71. u32 value)
  72. {
  73. writel(value, pcie->base + offset);
  74. }
  75. static inline u32 dra7xx_pcie_readl_rc(struct pcie_port *pp, u32 offset)
  76. {
  77. return readl(pp->dbi_base + offset);
  78. }
  79. static inline void dra7xx_pcie_writel_rc(struct pcie_port *pp, u32 offset,
  80. u32 value)
  81. {
  82. writel(value, pp->dbi_base + offset);
  83. }
  84. static int dra7xx_pcie_link_up(struct pcie_port *pp)
  85. {
  86. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  87. u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
  88. return !!(reg & LINK_UP);
  89. }
  90. static int dra7xx_pcie_establish_link(struct pcie_port *pp)
  91. {
  92. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  93. u32 reg;
  94. unsigned int retries;
  95. if (dw_pcie_link_up(pp)) {
  96. dev_err(pp->dev, "link is already up\n");
  97. return 0;
  98. }
  99. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  100. reg |= LTSSM_EN;
  101. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  102. for (retries = 0; retries < 1000; retries++) {
  103. if (dw_pcie_link_up(pp))
  104. return 0;
  105. usleep_range(10, 20);
  106. }
  107. dev_err(pp->dev, "link is not up\n");
  108. return -EINVAL;
  109. }
  110. static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)
  111. {
  112. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  113. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
  114. ~INTERRUPTS);
  115. dra7xx_pcie_writel(dra7xx,
  116. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
  117. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
  118. ~LEG_EP_INTERRUPTS & ~MSI);
  119. if (IS_ENABLED(CONFIG_PCI_MSI))
  120. dra7xx_pcie_writel(dra7xx,
  121. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI, MSI);
  122. else
  123. dra7xx_pcie_writel(dra7xx,
  124. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
  125. LEG_EP_INTERRUPTS);
  126. }
  127. static void dra7xx_pcie_host_init(struct pcie_port *pp)
  128. {
  129. dw_pcie_setup_rc(pp);
  130. dra7xx_pcie_establish_link(pp);
  131. if (IS_ENABLED(CONFIG_PCI_MSI))
  132. dw_pcie_msi_init(pp);
  133. dra7xx_pcie_enable_interrupts(pp);
  134. }
  135. static struct pcie_host_ops dra7xx_pcie_host_ops = {
  136. .link_up = dra7xx_pcie_link_up,
  137. .host_init = dra7xx_pcie_host_init,
  138. };
  139. static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  140. irq_hw_number_t hwirq)
  141. {
  142. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  143. irq_set_chip_data(irq, domain->host_data);
  144. return 0;
  145. }
  146. static const struct irq_domain_ops intx_domain_ops = {
  147. .map = dra7xx_pcie_intx_map,
  148. };
  149. static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
  150. {
  151. struct device *dev = pp->dev;
  152. struct device_node *node = dev->of_node;
  153. struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
  154. if (!pcie_intc_node) {
  155. dev_err(dev, "No PCIe Intc node found\n");
  156. return PTR_ERR(pcie_intc_node);
  157. }
  158. pp->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
  159. &intx_domain_ops, pp);
  160. if (!pp->irq_domain) {
  161. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  162. return PTR_ERR(pp->irq_domain);
  163. }
  164. return 0;
  165. }
  166. static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
  167. {
  168. struct pcie_port *pp = arg;
  169. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  170. u32 reg;
  171. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
  172. switch (reg) {
  173. case MSI:
  174. dw_handle_msi_irq(pp);
  175. break;
  176. case INTA:
  177. case INTB:
  178. case INTC:
  179. case INTD:
  180. generic_handle_irq(irq_find_mapping(pp->irq_domain, ffs(reg)));
  181. break;
  182. }
  183. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
  184. return IRQ_HANDLED;
  185. }
  186. static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
  187. {
  188. struct dra7xx_pcie *dra7xx = arg;
  189. u32 reg;
  190. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
  191. if (reg & ERR_SYS)
  192. dev_dbg(dra7xx->dev, "System Error\n");
  193. if (reg & ERR_FATAL)
  194. dev_dbg(dra7xx->dev, "Fatal Error\n");
  195. if (reg & ERR_NONFATAL)
  196. dev_dbg(dra7xx->dev, "Non Fatal Error\n");
  197. if (reg & ERR_COR)
  198. dev_dbg(dra7xx->dev, "Correctable Error\n");
  199. if (reg & ERR_AXI)
  200. dev_dbg(dra7xx->dev, "AXI tag lookup fatal Error\n");
  201. if (reg & ERR_ECRC)
  202. dev_dbg(dra7xx->dev, "ECRC Error\n");
  203. if (reg & PME_TURN_OFF)
  204. dev_dbg(dra7xx->dev,
  205. "Power Management Event Turn-Off message received\n");
  206. if (reg & PME_TO_ACK)
  207. dev_dbg(dra7xx->dev,
  208. "Power Management Turn-Off Ack message received\n");
  209. if (reg & PM_PME)
  210. dev_dbg(dra7xx->dev,
  211. "PM Power Management Event message received\n");
  212. if (reg & LINK_REQ_RST)
  213. dev_dbg(dra7xx->dev, "Link Request Reset\n");
  214. if (reg & LINK_UP_EVT)
  215. dev_dbg(dra7xx->dev, "Link-up state change\n");
  216. if (reg & CFG_BME_EVT)
  217. dev_dbg(dra7xx->dev, "CFG 'Bus Master Enable' change\n");
  218. if (reg & CFG_MSE_EVT)
  219. dev_dbg(dra7xx->dev, "CFG 'Memory Space Enable' change\n");
  220. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
  221. return IRQ_HANDLED;
  222. }
  223. static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  224. struct platform_device *pdev)
  225. {
  226. int ret;
  227. struct pcie_port *pp;
  228. struct resource *res;
  229. struct device *dev = &pdev->dev;
  230. pp = &dra7xx->pp;
  231. pp->dev = dev;
  232. pp->ops = &dra7xx_pcie_host_ops;
  233. pp->irq = platform_get_irq(pdev, 1);
  234. if (pp->irq < 0) {
  235. dev_err(dev, "missing IRQ resource\n");
  236. return -EINVAL;
  237. }
  238. ret = devm_request_irq(&pdev->dev, pp->irq,
  239. dra7xx_pcie_msi_irq_handler, IRQF_SHARED,
  240. "dra7-pcie-msi", pp);
  241. if (ret) {
  242. dev_err(&pdev->dev, "failed to request irq\n");
  243. return ret;
  244. }
  245. if (!IS_ENABLED(CONFIG_PCI_MSI)) {
  246. ret = dra7xx_pcie_init_irq_domain(pp);
  247. if (ret < 0)
  248. return ret;
  249. }
  250. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
  251. pp->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
  252. if (!pp->dbi_base)
  253. return -ENOMEM;
  254. ret = dw_pcie_host_init(pp);
  255. if (ret) {
  256. dev_err(dra7xx->dev, "failed to initialize host\n");
  257. return ret;
  258. }
  259. return 0;
  260. }
  261. static int __init dra7xx_pcie_probe(struct platform_device *pdev)
  262. {
  263. u32 reg;
  264. int ret;
  265. int irq;
  266. int i;
  267. int phy_count;
  268. struct phy **phy;
  269. void __iomem *base;
  270. struct resource *res;
  271. struct dra7xx_pcie *dra7xx;
  272. struct device *dev = &pdev->dev;
  273. struct device_node *np = dev->of_node;
  274. char name[10];
  275. int gpio_sel;
  276. enum of_gpio_flags flags;
  277. unsigned long gpio_flags;
  278. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  279. if (!dra7xx)
  280. return -ENOMEM;
  281. irq = platform_get_irq(pdev, 0);
  282. if (irq < 0) {
  283. dev_err(dev, "missing IRQ resource\n");
  284. return -EINVAL;
  285. }
  286. ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
  287. IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
  288. if (ret) {
  289. dev_err(dev, "failed to request irq\n");
  290. return ret;
  291. }
  292. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
  293. base = devm_ioremap_nocache(dev, res->start, resource_size(res));
  294. if (!base)
  295. return -ENOMEM;
  296. phy_count = of_property_count_strings(np, "phy-names");
  297. if (phy_count < 0) {
  298. dev_err(dev, "unable to find the strings\n");
  299. return phy_count;
  300. }
  301. phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
  302. if (!phy)
  303. return -ENOMEM;
  304. for (i = 0; i < phy_count; i++) {
  305. snprintf(name, sizeof(name), "pcie-phy%d", i);
  306. phy[i] = devm_phy_get(dev, name);
  307. if (IS_ERR(phy[i]))
  308. return PTR_ERR(phy[i]);
  309. ret = phy_init(phy[i]);
  310. if (ret < 0)
  311. goto err_phy;
  312. ret = phy_power_on(phy[i]);
  313. if (ret < 0) {
  314. phy_exit(phy[i]);
  315. goto err_phy;
  316. }
  317. }
  318. dra7xx->base = base;
  319. dra7xx->phy = phy;
  320. dra7xx->dev = dev;
  321. dra7xx->phy_count = phy_count;
  322. pm_runtime_enable(dev);
  323. ret = pm_runtime_get_sync(dev);
  324. if (ret < 0) {
  325. dev_err(dev, "pm_runtime_get_sync failed\n");
  326. goto err_get_sync;
  327. }
  328. gpio_sel = of_get_gpio_flags(dev->of_node, 0, &flags);
  329. if (gpio_is_valid(gpio_sel)) {
  330. gpio_flags = (flags & OF_GPIO_ACTIVE_LOW) ?
  331. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH;
  332. ret = devm_gpio_request_one(dev, gpio_sel, gpio_flags,
  333. "pcie_reset");
  334. if (ret) {
  335. dev_err(&pdev->dev, "gpio%d request failed, ret %d\n",
  336. gpio_sel, ret);
  337. goto err_gpio;
  338. }
  339. } else if (gpio_sel == -EPROBE_DEFER) {
  340. ret = -EPROBE_DEFER;
  341. goto err_gpio;
  342. }
  343. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  344. reg &= ~LTSSM_EN;
  345. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  346. platform_set_drvdata(pdev, dra7xx);
  347. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  348. if (ret < 0)
  349. goto err_gpio;
  350. return 0;
  351. err_gpio:
  352. pm_runtime_put(dev);
  353. err_get_sync:
  354. pm_runtime_disable(dev);
  355. err_phy:
  356. while (--i >= 0) {
  357. phy_power_off(phy[i]);
  358. phy_exit(phy[i]);
  359. }
  360. return ret;
  361. }
  362. static int __exit dra7xx_pcie_remove(struct platform_device *pdev)
  363. {
  364. struct dra7xx_pcie *dra7xx = platform_get_drvdata(pdev);
  365. struct pcie_port *pp = &dra7xx->pp;
  366. struct device *dev = &pdev->dev;
  367. int count = dra7xx->phy_count;
  368. if (pp->irq_domain)
  369. irq_domain_remove(pp->irq_domain);
  370. pm_runtime_put(dev);
  371. pm_runtime_disable(dev);
  372. while (count--) {
  373. phy_power_off(dra7xx->phy[count]);
  374. phy_exit(dra7xx->phy[count]);
  375. }
  376. return 0;
  377. }
  378. #ifdef CONFIG_PM_SLEEP
  379. static int dra7xx_pcie_suspend(struct device *dev)
  380. {
  381. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  382. struct pcie_port *pp = &dra7xx->pp;
  383. u32 val;
  384. /* clear MSE */
  385. val = dra7xx_pcie_readl_rc(pp, PCI_COMMAND);
  386. val &= ~PCI_COMMAND_MEMORY;
  387. dra7xx_pcie_writel_rc(pp, PCI_COMMAND, val);
  388. return 0;
  389. }
  390. static int dra7xx_pcie_resume(struct device *dev)
  391. {
  392. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  393. struct pcie_port *pp = &dra7xx->pp;
  394. u32 val;
  395. /* set MSE */
  396. val = dra7xx_pcie_readl_rc(pp, PCI_COMMAND);
  397. val |= PCI_COMMAND_MEMORY;
  398. dra7xx_pcie_writel_rc(pp, PCI_COMMAND, val);
  399. return 0;
  400. }
  401. static int dra7xx_pcie_suspend_noirq(struct device *dev)
  402. {
  403. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  404. int count = dra7xx->phy_count;
  405. while (count--) {
  406. phy_power_off(dra7xx->phy[count]);
  407. phy_exit(dra7xx->phy[count]);
  408. }
  409. return 0;
  410. }
  411. static int dra7xx_pcie_resume_noirq(struct device *dev)
  412. {
  413. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  414. int phy_count = dra7xx->phy_count;
  415. int ret;
  416. int i;
  417. for (i = 0; i < phy_count; i++) {
  418. ret = phy_init(dra7xx->phy[i]);
  419. if (ret < 0)
  420. goto err_phy;
  421. ret = phy_power_on(dra7xx->phy[i]);
  422. if (ret < 0) {
  423. phy_exit(dra7xx->phy[i]);
  424. goto err_phy;
  425. }
  426. }
  427. return 0;
  428. err_phy:
  429. while (--i >= 0) {
  430. phy_power_off(dra7xx->phy[i]);
  431. phy_exit(dra7xx->phy[i]);
  432. }
  433. return ret;
  434. }
  435. #endif
  436. static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
  437. SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
  438. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
  439. dra7xx_pcie_resume_noirq)
  440. };
  441. static const struct of_device_id of_dra7xx_pcie_match[] = {
  442. { .compatible = "ti,dra7-pcie", },
  443. {},
  444. };
  445. MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
  446. static struct platform_driver dra7xx_pcie_driver = {
  447. .remove = __exit_p(dra7xx_pcie_remove),
  448. .driver = {
  449. .name = "dra7-pcie",
  450. .of_match_table = of_dra7xx_pcie_match,
  451. .pm = &dra7xx_pcie_pm_ops,
  452. },
  453. };
  454. module_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);
  455. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  456. MODULE_DESCRIPTION("TI PCIe controller driver");
  457. MODULE_LICENSE("GPL v2");