pci-dra7xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/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. struct dra7xx_pcie {
  58. void __iomem *base;
  59. struct phy **phy;
  60. int phy_count;
  61. struct device *dev;
  62. struct pcie_port pp;
  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 pcie_port *pp)
  81. {
  82. u32 reg;
  83. unsigned int retries = 1000;
  84. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  85. if (dw_pcie_link_up(pp)) {
  86. dev_err(pp->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. while (retries--) {
  93. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
  94. if (reg & LINK_UP)
  95. break;
  96. usleep_range(10, 20);
  97. }
  98. if (retries == 0) {
  99. dev_err(pp->dev, "link is not up\n");
  100. return -ETIMEDOUT;
  101. }
  102. return 0;
  103. }
  104. static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)
  105. {
  106. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  107. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
  108. ~INTERRUPTS);
  109. dra7xx_pcie_writel(dra7xx,
  110. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
  111. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
  112. ~LEG_EP_INTERRUPTS & ~MSI);
  113. if (IS_ENABLED(CONFIG_PCI_MSI))
  114. dra7xx_pcie_writel(dra7xx,
  115. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI, MSI);
  116. else
  117. dra7xx_pcie_writel(dra7xx,
  118. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
  119. LEG_EP_INTERRUPTS);
  120. }
  121. static void dra7xx_pcie_host_init(struct pcie_port *pp)
  122. {
  123. dw_pcie_setup_rc(pp);
  124. dra7xx_pcie_establish_link(pp);
  125. if (IS_ENABLED(CONFIG_PCI_MSI))
  126. dw_pcie_msi_init(pp);
  127. dra7xx_pcie_enable_interrupts(pp);
  128. }
  129. static struct pcie_host_ops dra7xx_pcie_host_ops = {
  130. .link_up = dra7xx_pcie_link_up,
  131. .host_init = dra7xx_pcie_host_init,
  132. };
  133. static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  134. irq_hw_number_t hwirq)
  135. {
  136. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  137. irq_set_chip_data(irq, domain->host_data);
  138. set_irq_flags(irq, IRQF_VALID);
  139. return 0;
  140. }
  141. static const struct irq_domain_ops intx_domain_ops = {
  142. .map = dra7xx_pcie_intx_map,
  143. };
  144. static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
  145. {
  146. struct device *dev = pp->dev;
  147. struct device_node *node = dev->of_node;
  148. struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
  149. if (!pcie_intc_node) {
  150. dev_err(dev, "No PCIe Intc node found\n");
  151. return PTR_ERR(pcie_intc_node);
  152. }
  153. pp->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
  154. &intx_domain_ops, pp);
  155. if (!pp->irq_domain) {
  156. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  157. return PTR_ERR(pp->irq_domain);
  158. }
  159. return 0;
  160. }
  161. static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
  162. {
  163. struct pcie_port *pp = arg;
  164. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
  165. u32 reg;
  166. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
  167. switch (reg) {
  168. case MSI:
  169. dw_handle_msi_irq(pp);
  170. break;
  171. case INTA:
  172. case INTB:
  173. case INTC:
  174. case INTD:
  175. generic_handle_irq(irq_find_mapping(pp->irq_domain, ffs(reg)));
  176. break;
  177. }
  178. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
  179. return IRQ_HANDLED;
  180. }
  181. static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
  182. {
  183. struct dra7xx_pcie *dra7xx = arg;
  184. u32 reg;
  185. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
  186. if (reg & ERR_SYS)
  187. dev_dbg(dra7xx->dev, "System Error\n");
  188. if (reg & ERR_FATAL)
  189. dev_dbg(dra7xx->dev, "Fatal Error\n");
  190. if (reg & ERR_NONFATAL)
  191. dev_dbg(dra7xx->dev, "Non Fatal Error\n");
  192. if (reg & ERR_COR)
  193. dev_dbg(dra7xx->dev, "Correctable Error\n");
  194. if (reg & ERR_AXI)
  195. dev_dbg(dra7xx->dev, "AXI tag lookup fatal Error\n");
  196. if (reg & ERR_ECRC)
  197. dev_dbg(dra7xx->dev, "ECRC Error\n");
  198. if (reg & PME_TURN_OFF)
  199. dev_dbg(dra7xx->dev,
  200. "Power Management Event Turn-Off message received\n");
  201. if (reg & PME_TO_ACK)
  202. dev_dbg(dra7xx->dev,
  203. "Power Management Turn-Off Ack message received\n");
  204. if (reg & PM_PME)
  205. dev_dbg(dra7xx->dev,
  206. "PM Power Management Event message received\n");
  207. if (reg & LINK_REQ_RST)
  208. dev_dbg(dra7xx->dev, "Link Request Reset\n");
  209. if (reg & LINK_UP_EVT)
  210. dev_dbg(dra7xx->dev, "Link-up state change\n");
  211. if (reg & CFG_BME_EVT)
  212. dev_dbg(dra7xx->dev, "CFG 'Bus Master Enable' change\n");
  213. if (reg & CFG_MSE_EVT)
  214. dev_dbg(dra7xx->dev, "CFG 'Memory Space Enable' change\n");
  215. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
  216. return IRQ_HANDLED;
  217. }
  218. static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  219. struct platform_device *pdev)
  220. {
  221. int ret;
  222. struct pcie_port *pp;
  223. struct resource *res;
  224. struct device *dev = &pdev->dev;
  225. pp = &dra7xx->pp;
  226. pp->dev = dev;
  227. pp->ops = &dra7xx_pcie_host_ops;
  228. pp->irq = platform_get_irq(pdev, 1);
  229. if (pp->irq < 0) {
  230. dev_err(dev, "missing IRQ resource\n");
  231. return -EINVAL;
  232. }
  233. ret = devm_request_irq(&pdev->dev, pp->irq,
  234. dra7xx_pcie_msi_irq_handler, IRQF_SHARED,
  235. "dra7-pcie-msi", pp);
  236. if (ret) {
  237. dev_err(&pdev->dev, "failed to request irq\n");
  238. return ret;
  239. }
  240. if (!IS_ENABLED(CONFIG_PCI_MSI)) {
  241. ret = dra7xx_pcie_init_irq_domain(pp);
  242. if (ret < 0)
  243. return ret;
  244. }
  245. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
  246. pp->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
  247. if (!pp->dbi_base)
  248. return -ENOMEM;
  249. ret = dw_pcie_host_init(pp);
  250. if (ret) {
  251. dev_err(dra7xx->dev, "failed to initialize host\n");
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. static int __init dra7xx_pcie_probe(struct platform_device *pdev)
  257. {
  258. u32 reg;
  259. int ret;
  260. int irq;
  261. int i;
  262. int phy_count;
  263. struct phy **phy;
  264. void __iomem *base;
  265. struct resource *res;
  266. struct dra7xx_pcie *dra7xx;
  267. struct device *dev = &pdev->dev;
  268. struct device_node *np = dev->of_node;
  269. char name[10];
  270. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  271. if (!dra7xx)
  272. return -ENOMEM;
  273. irq = platform_get_irq(pdev, 0);
  274. if (irq < 0) {
  275. dev_err(dev, "missing IRQ resource\n");
  276. return -EINVAL;
  277. }
  278. ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
  279. IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
  280. if (ret) {
  281. dev_err(dev, "failed to request irq\n");
  282. return ret;
  283. }
  284. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
  285. base = devm_ioremap_nocache(dev, res->start, resource_size(res));
  286. if (!base)
  287. return -ENOMEM;
  288. phy_count = of_property_count_strings(np, "phy-names");
  289. if (phy_count < 0) {
  290. dev_err(dev, "unable to find the strings\n");
  291. return phy_count;
  292. }
  293. phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
  294. if (!phy)
  295. return -ENOMEM;
  296. for (i = 0; i < phy_count; i++) {
  297. snprintf(name, sizeof(name), "pcie-phy%d", i);
  298. phy[i] = devm_phy_get(dev, name);
  299. if (IS_ERR(phy[i]))
  300. return PTR_ERR(phy[i]);
  301. ret = phy_init(phy[i]);
  302. if (ret < 0)
  303. goto err_phy;
  304. ret = phy_power_on(phy[i]);
  305. if (ret < 0) {
  306. phy_exit(phy[i]);
  307. goto err_phy;
  308. }
  309. }
  310. dra7xx->base = base;
  311. dra7xx->phy = phy;
  312. dra7xx->dev = dev;
  313. dra7xx->phy_count = phy_count;
  314. pm_runtime_enable(dev);
  315. ret = pm_runtime_get_sync(dev);
  316. if (IS_ERR_VALUE(ret)) {
  317. dev_err(dev, "pm_runtime_get_sync failed\n");
  318. goto err_phy;
  319. }
  320. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  321. reg &= ~LTSSM_EN;
  322. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  323. platform_set_drvdata(pdev, dra7xx);
  324. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  325. if (ret < 0)
  326. goto err_add_port;
  327. return 0;
  328. err_add_port:
  329. pm_runtime_put(dev);
  330. pm_runtime_disable(dev);
  331. err_phy:
  332. while (--i >= 0) {
  333. phy_power_off(phy[i]);
  334. phy_exit(phy[i]);
  335. }
  336. return ret;
  337. }
  338. static int __exit dra7xx_pcie_remove(struct platform_device *pdev)
  339. {
  340. struct dra7xx_pcie *dra7xx = platform_get_drvdata(pdev);
  341. struct pcie_port *pp = &dra7xx->pp;
  342. struct device *dev = &pdev->dev;
  343. int count = dra7xx->phy_count;
  344. if (pp->irq_domain)
  345. irq_domain_remove(pp->irq_domain);
  346. pm_runtime_put(dev);
  347. pm_runtime_disable(dev);
  348. while (count--) {
  349. phy_power_off(dra7xx->phy[count]);
  350. phy_exit(dra7xx->phy[count]);
  351. }
  352. return 0;
  353. }
  354. static const struct of_device_id of_dra7xx_pcie_match[] = {
  355. { .compatible = "ti,dra7-pcie", },
  356. {},
  357. };
  358. MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
  359. static struct platform_driver dra7xx_pcie_driver = {
  360. .remove = __exit_p(dra7xx_pcie_remove),
  361. .driver = {
  362. .name = "dra7-pcie",
  363. .of_match_table = of_dra7xx_pcie_match,
  364. },
  365. };
  366. module_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);
  367. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  368. MODULE_DESCRIPTION("TI PCIe controller driver");
  369. MODULE_LICENSE("GPL v2");