pci-dra7xx.c 13 KB

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