pci-dra7xx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
  4. *
  5. * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Kishon Vijay Abraham I <kishon@ti.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/of_device.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 <linux/mfd/syscon.h>
  27. #include <linux/regmap.h>
  28. #include <linux/gpio/consumer.h>
  29. #include "../../pci.h"
  30. #include "pcie-designware.h"
  31. /* PCIe controller wrapper DRA7XX configuration registers */
  32. #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024
  33. #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028
  34. #define ERR_SYS BIT(0)
  35. #define ERR_FATAL BIT(1)
  36. #define ERR_NONFATAL BIT(2)
  37. #define ERR_COR BIT(3)
  38. #define ERR_AXI BIT(4)
  39. #define ERR_ECRC BIT(5)
  40. #define PME_TURN_OFF BIT(8)
  41. #define PME_TO_ACK BIT(9)
  42. #define PM_PME BIT(10)
  43. #define LINK_REQ_RST BIT(11)
  44. #define LINK_UP_EVT BIT(12)
  45. #define CFG_BME_EVT BIT(13)
  46. #define CFG_MSE_EVT BIT(14)
  47. #define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
  48. ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
  49. LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
  50. #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034
  51. #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038
  52. #define INTA BIT(0)
  53. #define INTB BIT(1)
  54. #define INTC BIT(2)
  55. #define INTD BIT(3)
  56. #define MSI BIT(4)
  57. #define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
  58. #define PCIECTRL_TI_CONF_DEVICE_TYPE 0x0100
  59. #define DEVICE_TYPE_EP 0x0
  60. #define DEVICE_TYPE_LEG_EP 0x1
  61. #define DEVICE_TYPE_RC 0x4
  62. #define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104
  63. #define LTSSM_EN 0x1
  64. #define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C
  65. #define LINK_UP BIT(16)
  66. #define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF
  67. #define EXP_CAP_ID_OFFSET 0x70
  68. #define PCIECTRL_TI_CONF_INTX_ASSERT 0x0124
  69. #define PCIECTRL_TI_CONF_INTX_DEASSERT 0x0128
  70. #define PCIECTRL_TI_CONF_MSI_XMT 0x012c
  71. #define MSI_REQ_GRANT BIT(0)
  72. #define MSI_VECTOR_SHIFT 7
  73. struct dra7xx_pcie {
  74. struct dw_pcie *pci;
  75. void __iomem *base; /* DT ti_conf */
  76. int phy_count; /* DT phy-names count */
  77. struct phy **phy;
  78. int link_gen;
  79. struct irq_domain *irq_domain;
  80. enum dw_pcie_device_mode mode;
  81. };
  82. struct dra7xx_pcie_of_data {
  83. enum dw_pcie_device_mode mode;
  84. };
  85. #define to_dra7xx_pcie(x) dev_get_drvdata((x)->dev)
  86. static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
  87. {
  88. return readl(pcie->base + offset);
  89. }
  90. static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
  91. u32 value)
  92. {
  93. writel(value, pcie->base + offset);
  94. }
  95. static u64 dra7xx_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 pci_addr)
  96. {
  97. return pci_addr & DRA7XX_CPU_TO_BUS_ADDR;
  98. }
  99. static int dra7xx_pcie_link_up(struct dw_pcie *pci)
  100. {
  101. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  102. u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
  103. return !!(reg & LINK_UP);
  104. }
  105. static void dra7xx_pcie_stop_link(struct dw_pcie *pci)
  106. {
  107. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  108. u32 reg;
  109. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  110. reg &= ~LTSSM_EN;
  111. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  112. }
  113. static int dra7xx_pcie_establish_link(struct dw_pcie *pci)
  114. {
  115. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  116. struct device *dev = pci->dev;
  117. u32 reg;
  118. u32 exp_cap_off = EXP_CAP_ID_OFFSET;
  119. if (dw_pcie_link_up(pci)) {
  120. dev_err(dev, "link is already up\n");
  121. return 0;
  122. }
  123. if (dra7xx->link_gen == 1) {
  124. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
  125. 4, &reg);
  126. if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  127. reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
  128. reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
  129. dw_pcie_write(pci->dbi_base + exp_cap_off +
  130. PCI_EXP_LNKCAP, 4, reg);
  131. }
  132. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
  133. 2, &reg);
  134. if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  135. reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
  136. reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
  137. dw_pcie_write(pci->dbi_base + exp_cap_off +
  138. PCI_EXP_LNKCTL2, 2, reg);
  139. }
  140. }
  141. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  142. reg |= LTSSM_EN;
  143. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  144. return 0;
  145. }
  146. static void dra7xx_pcie_enable_msi_interrupts(struct dra7xx_pcie *dra7xx)
  147. {
  148. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
  149. LEG_EP_INTERRUPTS | MSI);
  150. dra7xx_pcie_writel(dra7xx,
  151. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
  152. MSI | LEG_EP_INTERRUPTS);
  153. }
  154. static void dra7xx_pcie_enable_wrapper_interrupts(struct dra7xx_pcie *dra7xx)
  155. {
  156. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
  157. INTERRUPTS);
  158. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN,
  159. INTERRUPTS);
  160. }
  161. static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
  162. {
  163. dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
  164. dra7xx_pcie_enable_msi_interrupts(dra7xx);
  165. }
  166. static int dra7xx_pcie_host_init(struct pcie_port *pp)
  167. {
  168. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  169. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  170. dw_pcie_setup_rc(pp);
  171. dra7xx_pcie_establish_link(pci);
  172. dw_pcie_wait_for_link(pci);
  173. dw_pcie_msi_init(pp);
  174. dra7xx_pcie_enable_interrupts(dra7xx);
  175. return 0;
  176. }
  177. static const struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
  178. .host_init = dra7xx_pcie_host_init,
  179. };
  180. static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  181. irq_hw_number_t hwirq)
  182. {
  183. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  184. irq_set_chip_data(irq, domain->host_data);
  185. return 0;
  186. }
  187. static const struct irq_domain_ops intx_domain_ops = {
  188. .map = dra7xx_pcie_intx_map,
  189. .xlate = pci_irqd_intx_xlate,
  190. };
  191. static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
  192. {
  193. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  194. struct device *dev = pci->dev;
  195. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  196. struct device_node *node = dev->of_node;
  197. struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
  198. if (!pcie_intc_node) {
  199. dev_err(dev, "No PCIe Intc node found\n");
  200. return -ENODEV;
  201. }
  202. dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
  203. &intx_domain_ops, pp);
  204. if (!dra7xx->irq_domain) {
  205. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  206. return -ENODEV;
  207. }
  208. return 0;
  209. }
  210. static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
  211. {
  212. struct dra7xx_pcie *dra7xx = arg;
  213. struct dw_pcie *pci = dra7xx->pci;
  214. struct pcie_port *pp = &pci->pp;
  215. unsigned long reg;
  216. u32 virq, bit;
  217. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
  218. switch (reg) {
  219. case MSI:
  220. dw_handle_msi_irq(pp);
  221. break;
  222. case INTA:
  223. case INTB:
  224. case INTC:
  225. case INTD:
  226. for_each_set_bit(bit, &reg, PCI_NUM_INTX) {
  227. virq = irq_find_mapping(dra7xx->irq_domain, bit);
  228. if (virq)
  229. generic_handle_irq(virq);
  230. }
  231. break;
  232. }
  233. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
  234. return IRQ_HANDLED;
  235. }
  236. static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
  237. {
  238. struct dra7xx_pcie *dra7xx = arg;
  239. struct dw_pcie *pci = dra7xx->pci;
  240. struct device *dev = pci->dev;
  241. struct dw_pcie_ep *ep = &pci->ep;
  242. u32 reg;
  243. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
  244. if (reg & ERR_SYS)
  245. dev_dbg(dev, "System Error\n");
  246. if (reg & ERR_FATAL)
  247. dev_dbg(dev, "Fatal Error\n");
  248. if (reg & ERR_NONFATAL)
  249. dev_dbg(dev, "Non Fatal Error\n");
  250. if (reg & ERR_COR)
  251. dev_dbg(dev, "Correctable Error\n");
  252. if (reg & ERR_AXI)
  253. dev_dbg(dev, "AXI tag lookup fatal Error\n");
  254. if (reg & ERR_ECRC)
  255. dev_dbg(dev, "ECRC Error\n");
  256. if (reg & PME_TURN_OFF)
  257. dev_dbg(dev,
  258. "Power Management Event Turn-Off message received\n");
  259. if (reg & PME_TO_ACK)
  260. dev_dbg(dev,
  261. "Power Management Turn-Off Ack message received\n");
  262. if (reg & PM_PME)
  263. dev_dbg(dev, "PM Power Management Event message received\n");
  264. if (reg & LINK_REQ_RST)
  265. dev_dbg(dev, "Link Request Reset\n");
  266. if (reg & LINK_UP_EVT) {
  267. if (dra7xx->mode == DW_PCIE_EP_TYPE)
  268. dw_pcie_ep_linkup(ep);
  269. dev_dbg(dev, "Link-up state change\n");
  270. }
  271. if (reg & CFG_BME_EVT)
  272. dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
  273. if (reg & CFG_MSE_EVT)
  274. dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
  275. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
  276. return IRQ_HANDLED;
  277. }
  278. static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
  279. {
  280. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  281. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  282. enum pci_barno bar;
  283. for (bar = BAR_0; bar <= BAR_5; bar++)
  284. dw_pcie_ep_reset_bar(pci, bar);
  285. dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
  286. }
  287. static void dra7xx_pcie_raise_legacy_irq(struct dra7xx_pcie *dra7xx)
  288. {
  289. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1);
  290. mdelay(1);
  291. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1);
  292. }
  293. static void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx,
  294. u8 interrupt_num)
  295. {
  296. u32 reg;
  297. reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT;
  298. reg |= MSI_REQ_GRANT;
  299. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg);
  300. }
  301. static int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  302. enum pci_epc_irq_type type, u16 interrupt_num)
  303. {
  304. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  305. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  306. switch (type) {
  307. case PCI_EPC_IRQ_LEGACY:
  308. dra7xx_pcie_raise_legacy_irq(dra7xx);
  309. break;
  310. case PCI_EPC_IRQ_MSI:
  311. dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num);
  312. break;
  313. default:
  314. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  315. }
  316. return 0;
  317. }
  318. static struct dw_pcie_ep_ops pcie_ep_ops = {
  319. .ep_init = dra7xx_pcie_ep_init,
  320. .raise_irq = dra7xx_pcie_raise_irq,
  321. };
  322. static int __init dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx,
  323. struct platform_device *pdev)
  324. {
  325. int ret;
  326. struct dw_pcie_ep *ep;
  327. struct resource *res;
  328. struct device *dev = &pdev->dev;
  329. struct dw_pcie *pci = dra7xx->pci;
  330. ep = &pci->ep;
  331. ep->ops = &pcie_ep_ops;
  332. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics");
  333. pci->dbi_base = devm_ioremap_resource(dev, res);
  334. if (IS_ERR(pci->dbi_base))
  335. return PTR_ERR(pci->dbi_base);
  336. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics2");
  337. pci->dbi_base2 = devm_ioremap_resource(dev, res);
  338. if (IS_ERR(pci->dbi_base2))
  339. return PTR_ERR(pci->dbi_base2);
  340. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
  341. if (!res)
  342. return -EINVAL;
  343. ep->phys_base = res->start;
  344. ep->addr_size = resource_size(res);
  345. ret = dw_pcie_ep_init(ep);
  346. if (ret) {
  347. dev_err(dev, "failed to initialize endpoint\n");
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  353. struct platform_device *pdev)
  354. {
  355. int ret;
  356. struct dw_pcie *pci = dra7xx->pci;
  357. struct pcie_port *pp = &pci->pp;
  358. struct device *dev = pci->dev;
  359. struct resource *res;
  360. pp->irq = platform_get_irq(pdev, 1);
  361. if (pp->irq < 0) {
  362. dev_err(dev, "missing IRQ resource\n");
  363. return pp->irq;
  364. }
  365. ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler,
  366. IRQF_SHARED | IRQF_NO_THREAD,
  367. "dra7-pcie-msi", dra7xx);
  368. if (ret) {
  369. dev_err(dev, "failed to request irq\n");
  370. return ret;
  371. }
  372. ret = dra7xx_pcie_init_irq_domain(pp);
  373. if (ret < 0)
  374. return ret;
  375. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
  376. pci->dbi_base = devm_ioremap_resource(dev, res);
  377. if (IS_ERR(pci->dbi_base))
  378. return PTR_ERR(pci->dbi_base);
  379. pp->ops = &dra7xx_pcie_host_ops;
  380. ret = dw_pcie_host_init(pp);
  381. if (ret) {
  382. dev_err(dev, "failed to initialize host\n");
  383. return ret;
  384. }
  385. return 0;
  386. }
  387. static const struct dw_pcie_ops dw_pcie_ops = {
  388. .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
  389. .start_link = dra7xx_pcie_establish_link,
  390. .stop_link = dra7xx_pcie_stop_link,
  391. .link_up = dra7xx_pcie_link_up,
  392. };
  393. static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
  394. {
  395. int phy_count = dra7xx->phy_count;
  396. while (phy_count--) {
  397. phy_power_off(dra7xx->phy[phy_count]);
  398. phy_exit(dra7xx->phy[phy_count]);
  399. }
  400. }
  401. static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
  402. {
  403. int phy_count = dra7xx->phy_count;
  404. int ret;
  405. int i;
  406. for (i = 0; i < phy_count; i++) {
  407. ret = phy_init(dra7xx->phy[i]);
  408. if (ret < 0)
  409. goto err_phy;
  410. ret = phy_power_on(dra7xx->phy[i]);
  411. if (ret < 0) {
  412. phy_exit(dra7xx->phy[i]);
  413. goto err_phy;
  414. }
  415. }
  416. return 0;
  417. err_phy:
  418. while (--i >= 0) {
  419. phy_power_off(dra7xx->phy[i]);
  420. phy_exit(dra7xx->phy[i]);
  421. }
  422. return ret;
  423. }
  424. static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
  425. .mode = DW_PCIE_RC_TYPE,
  426. };
  427. static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
  428. .mode = DW_PCIE_EP_TYPE,
  429. };
  430. static const struct of_device_id of_dra7xx_pcie_match[] = {
  431. {
  432. .compatible = "ti,dra7-pcie",
  433. .data = &dra7xx_pcie_rc_of_data,
  434. },
  435. {
  436. .compatible = "ti,dra7-pcie-ep",
  437. .data = &dra7xx_pcie_ep_of_data,
  438. },
  439. {},
  440. };
  441. /*
  442. * dra7xx_pcie_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
  443. * @dra7xx: the dra7xx device where the workaround should be applied
  444. *
  445. * Access to the PCIe slave port that are not 32-bit aligned will result
  446. * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
  447. * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
  448. * 0x3.
  449. *
  450. * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
  451. */
  452. static int dra7xx_pcie_unaligned_memaccess(struct device *dev)
  453. {
  454. int ret;
  455. struct device_node *np = dev->of_node;
  456. struct of_phandle_args args;
  457. struct regmap *regmap;
  458. regmap = syscon_regmap_lookup_by_phandle(np,
  459. "ti,syscon-unaligned-access");
  460. if (IS_ERR(regmap)) {
  461. dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
  462. return -EINVAL;
  463. }
  464. ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access",
  465. 2, 0, &args);
  466. if (ret) {
  467. dev_err(dev, "failed to parse ti,syscon-unaligned-access\n");
  468. return ret;
  469. }
  470. ret = regmap_update_bits(regmap, args.args[0], args.args[1],
  471. args.args[1]);
  472. if (ret)
  473. dev_err(dev, "failed to enable unaligned access\n");
  474. of_node_put(args.np);
  475. return ret;
  476. }
  477. static int __init dra7xx_pcie_probe(struct platform_device *pdev)
  478. {
  479. u32 reg;
  480. int ret;
  481. int irq;
  482. int i;
  483. int phy_count;
  484. struct phy **phy;
  485. struct device_link **link;
  486. void __iomem *base;
  487. struct resource *res;
  488. struct dw_pcie *pci;
  489. struct dra7xx_pcie *dra7xx;
  490. struct device *dev = &pdev->dev;
  491. struct device_node *np = dev->of_node;
  492. char name[10];
  493. struct gpio_desc *reset;
  494. const struct of_device_id *match;
  495. const struct dra7xx_pcie_of_data *data;
  496. enum dw_pcie_device_mode mode;
  497. match = of_match_device(of_match_ptr(of_dra7xx_pcie_match), dev);
  498. if (!match)
  499. return -EINVAL;
  500. data = (struct dra7xx_pcie_of_data *)match->data;
  501. mode = (enum dw_pcie_device_mode)data->mode;
  502. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  503. if (!dra7xx)
  504. return -ENOMEM;
  505. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  506. if (!pci)
  507. return -ENOMEM;
  508. pci->dev = dev;
  509. pci->ops = &dw_pcie_ops;
  510. irq = platform_get_irq(pdev, 0);
  511. if (irq < 0) {
  512. dev_err(dev, "missing IRQ resource: %d\n", irq);
  513. return irq;
  514. }
  515. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
  516. base = devm_ioremap_nocache(dev, res->start, resource_size(res));
  517. if (!base)
  518. return -ENOMEM;
  519. phy_count = of_property_count_strings(np, "phy-names");
  520. if (phy_count < 0) {
  521. dev_err(dev, "unable to find the strings\n");
  522. return phy_count;
  523. }
  524. phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
  525. if (!phy)
  526. return -ENOMEM;
  527. link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
  528. if (!link)
  529. return -ENOMEM;
  530. for (i = 0; i < phy_count; i++) {
  531. snprintf(name, sizeof(name), "pcie-phy%d", i);
  532. phy[i] = devm_phy_get(dev, name);
  533. if (IS_ERR(phy[i]))
  534. return PTR_ERR(phy[i]);
  535. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  536. if (!link[i]) {
  537. ret = -EINVAL;
  538. goto err_link;
  539. }
  540. }
  541. dra7xx->base = base;
  542. dra7xx->phy = phy;
  543. dra7xx->pci = pci;
  544. dra7xx->phy_count = phy_count;
  545. ret = dra7xx_pcie_enable_phy(dra7xx);
  546. if (ret) {
  547. dev_err(dev, "failed to enable phy\n");
  548. return ret;
  549. }
  550. platform_set_drvdata(pdev, dra7xx);
  551. pm_runtime_enable(dev);
  552. ret = pm_runtime_get_sync(dev);
  553. if (ret < 0) {
  554. dev_err(dev, "pm_runtime_get_sync failed\n");
  555. goto err_get_sync;
  556. }
  557. reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  558. if (IS_ERR(reset)) {
  559. ret = PTR_ERR(reset);
  560. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  561. goto err_gpio;
  562. }
  563. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  564. reg &= ~LTSSM_EN;
  565. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  566. dra7xx->link_gen = of_pci_get_max_link_speed(np);
  567. if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2)
  568. dra7xx->link_gen = 2;
  569. switch (mode) {
  570. case DW_PCIE_RC_TYPE:
  571. if (!IS_ENABLED(CONFIG_PCI_DRA7XX_HOST)) {
  572. ret = -ENODEV;
  573. goto err_gpio;
  574. }
  575. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  576. DEVICE_TYPE_RC);
  577. ret = dra7xx_pcie_unaligned_memaccess(dev);
  578. if (ret)
  579. dev_err(dev, "WA for Errata i870 not applied\n");
  580. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  581. if (ret < 0)
  582. goto err_gpio;
  583. break;
  584. case DW_PCIE_EP_TYPE:
  585. if (!IS_ENABLED(CONFIG_PCI_DRA7XX_EP)) {
  586. ret = -ENODEV;
  587. goto err_gpio;
  588. }
  589. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  590. DEVICE_TYPE_EP);
  591. ret = dra7xx_pcie_unaligned_memaccess(dev);
  592. if (ret)
  593. goto err_gpio;
  594. ret = dra7xx_add_pcie_ep(dra7xx, pdev);
  595. if (ret < 0)
  596. goto err_gpio;
  597. break;
  598. default:
  599. dev_err(dev, "INVALID device type %d\n", mode);
  600. }
  601. dra7xx->mode = mode;
  602. ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
  603. IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
  604. if (ret) {
  605. dev_err(dev, "failed to request irq\n");
  606. goto err_gpio;
  607. }
  608. return 0;
  609. err_gpio:
  610. pm_runtime_put(dev);
  611. err_get_sync:
  612. pm_runtime_disable(dev);
  613. dra7xx_pcie_disable_phy(dra7xx);
  614. err_link:
  615. while (--i >= 0)
  616. device_link_del(link[i]);
  617. return ret;
  618. }
  619. #ifdef CONFIG_PM_SLEEP
  620. static int dra7xx_pcie_suspend(struct device *dev)
  621. {
  622. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  623. struct dw_pcie *pci = dra7xx->pci;
  624. u32 val;
  625. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  626. return 0;
  627. /* clear MSE */
  628. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  629. val &= ~PCI_COMMAND_MEMORY;
  630. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  631. return 0;
  632. }
  633. static int dra7xx_pcie_resume(struct device *dev)
  634. {
  635. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  636. struct dw_pcie *pci = dra7xx->pci;
  637. u32 val;
  638. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  639. return 0;
  640. /* set MSE */
  641. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  642. val |= PCI_COMMAND_MEMORY;
  643. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  644. return 0;
  645. }
  646. static int dra7xx_pcie_suspend_noirq(struct device *dev)
  647. {
  648. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  649. dra7xx_pcie_disable_phy(dra7xx);
  650. return 0;
  651. }
  652. static int dra7xx_pcie_resume_noirq(struct device *dev)
  653. {
  654. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  655. int ret;
  656. ret = dra7xx_pcie_enable_phy(dra7xx);
  657. if (ret) {
  658. dev_err(dev, "failed to enable phy\n");
  659. return ret;
  660. }
  661. return 0;
  662. }
  663. #endif
  664. static void dra7xx_pcie_shutdown(struct platform_device *pdev)
  665. {
  666. struct device *dev = &pdev->dev;
  667. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  668. int ret;
  669. dra7xx_pcie_stop_link(dra7xx->pci);
  670. ret = pm_runtime_put_sync(dev);
  671. if (ret < 0)
  672. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  673. pm_runtime_disable(dev);
  674. dra7xx_pcie_disable_phy(dra7xx);
  675. }
  676. static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
  677. SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
  678. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
  679. dra7xx_pcie_resume_noirq)
  680. };
  681. static struct platform_driver dra7xx_pcie_driver = {
  682. .driver = {
  683. .name = "dra7-pcie",
  684. .of_match_table = of_dra7xx_pcie_match,
  685. .suppress_bind_attrs = true,
  686. .pm = &dra7xx_pcie_pm_ops,
  687. },
  688. .shutdown = dra7xx_pcie_shutdown,
  689. };
  690. builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);