pci-dra7xx.c 20 KB

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