pci-dra7xx.c 12 KB

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