pci-dra7xx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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/init.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/of_pci.h>
  22. #include <linux/pci.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/resource.h>
  27. #include <linux/types.h>
  28. #include <linux/mfd/syscon.h>
  29. #include <linux/regmap.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(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. };
  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. u32 reg;
  215. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
  216. switch (reg) {
  217. case MSI:
  218. dw_handle_msi_irq(pp);
  219. break;
  220. case INTA:
  221. case INTB:
  222. case INTC:
  223. case INTD:
  224. generic_handle_irq(irq_find_mapping(dra7xx->irq_domain,
  225. ffs(reg)));
  226. break;
  227. }
  228. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
  229. return IRQ_HANDLED;
  230. }
  231. static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
  232. {
  233. struct dra7xx_pcie *dra7xx = arg;
  234. struct dw_pcie *pci = dra7xx->pci;
  235. struct device *dev = pci->dev;
  236. struct dw_pcie_ep *ep = &pci->ep;
  237. u32 reg;
  238. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
  239. if (reg & ERR_SYS)
  240. dev_dbg(dev, "System Error\n");
  241. if (reg & ERR_FATAL)
  242. dev_dbg(dev, "Fatal Error\n");
  243. if (reg & ERR_NONFATAL)
  244. dev_dbg(dev, "Non Fatal Error\n");
  245. if (reg & ERR_COR)
  246. dev_dbg(dev, "Correctable Error\n");
  247. if (reg & ERR_AXI)
  248. dev_dbg(dev, "AXI tag lookup fatal Error\n");
  249. if (reg & ERR_ECRC)
  250. dev_dbg(dev, "ECRC Error\n");
  251. if (reg & PME_TURN_OFF)
  252. dev_dbg(dev,
  253. "Power Management Event Turn-Off message received\n");
  254. if (reg & PME_TO_ACK)
  255. dev_dbg(dev,
  256. "Power Management Turn-Off Ack message received\n");
  257. if (reg & PM_PME)
  258. dev_dbg(dev, "PM Power Management Event message received\n");
  259. if (reg & LINK_REQ_RST)
  260. dev_dbg(dev, "Link Request Reset\n");
  261. if (reg & LINK_UP_EVT) {
  262. if (dra7xx->mode == DW_PCIE_EP_TYPE)
  263. dw_pcie_ep_linkup(ep);
  264. dev_dbg(dev, "Link-up state change\n");
  265. }
  266. if (reg & CFG_BME_EVT)
  267. dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
  268. if (reg & CFG_MSE_EVT)
  269. dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
  270. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
  271. return IRQ_HANDLED;
  272. }
  273. static void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
  274. {
  275. u32 reg;
  276. reg = PCI_BASE_ADDRESS_0 + (4 * bar);
  277. dw_pcie_writel_dbi2(pci, reg, 0x0);
  278. dw_pcie_writel_dbi(pci, reg, 0x0);
  279. }
  280. static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
  281. {
  282. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  283. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  284. enum pci_barno bar;
  285. for (bar = BAR_0; bar <= BAR_5; bar++)
  286. dw_pcie_ep_reset_bar(pci, bar);
  287. dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
  288. }
  289. static void dra7xx_pcie_raise_legacy_irq(struct dra7xx_pcie *dra7xx)
  290. {
  291. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1);
  292. mdelay(1);
  293. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1);
  294. }
  295. static void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx,
  296. u8 interrupt_num)
  297. {
  298. u32 reg;
  299. reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT;
  300. reg |= MSI_REQ_GRANT;
  301. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg);
  302. }
  303. static int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep,
  304. enum pci_epc_irq_type type, u8 interrupt_num)
  305. {
  306. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  307. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  308. switch (type) {
  309. case PCI_EPC_IRQ_LEGACY:
  310. dra7xx_pcie_raise_legacy_irq(dra7xx);
  311. break;
  312. case PCI_EPC_IRQ_MSI:
  313. dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num);
  314. break;
  315. default:
  316. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  317. }
  318. return 0;
  319. }
  320. static struct dw_pcie_ep_ops pcie_ep_ops = {
  321. .ep_init = dra7xx_pcie_ep_init,
  322. .raise_irq = dra7xx_pcie_raise_irq,
  323. };
  324. static int __init dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx,
  325. struct platform_device *pdev)
  326. {
  327. int ret;
  328. struct dw_pcie_ep *ep;
  329. struct resource *res;
  330. struct device *dev = &pdev->dev;
  331. struct dw_pcie *pci = dra7xx->pci;
  332. ep = &pci->ep;
  333. ep->ops = &pcie_ep_ops;
  334. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics");
  335. pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
  336. if (!pci->dbi_base)
  337. return -ENOMEM;
  338. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics2");
  339. pci->dbi_base2 = devm_ioremap(dev, res->start, resource_size(res));
  340. if (!pci->dbi_base2)
  341. return -ENOMEM;
  342. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
  343. if (!res)
  344. return -EINVAL;
  345. ep->phys_base = res->start;
  346. ep->addr_size = resource_size(res);
  347. ret = dw_pcie_ep_init(ep);
  348. if (ret) {
  349. dev_err(dev, "failed to initialize endpoint\n");
  350. return ret;
  351. }
  352. return 0;
  353. }
  354. static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  355. struct platform_device *pdev)
  356. {
  357. int ret;
  358. struct dw_pcie *pci = dra7xx->pci;
  359. struct pcie_port *pp = &pci->pp;
  360. struct device *dev = pci->dev;
  361. struct resource *res;
  362. pp->irq = platform_get_irq(pdev, 1);
  363. if (pp->irq < 0) {
  364. dev_err(dev, "missing IRQ resource\n");
  365. return pp->irq;
  366. }
  367. ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler,
  368. IRQF_SHARED | IRQF_NO_THREAD,
  369. "dra7-pcie-msi", dra7xx);
  370. if (ret) {
  371. dev_err(dev, "failed to request irq\n");
  372. return ret;
  373. }
  374. ret = dra7xx_pcie_init_irq_domain(pp);
  375. if (ret < 0)
  376. return ret;
  377. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
  378. pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
  379. if (!pci->dbi_base)
  380. return -ENOMEM;
  381. ret = dw_pcie_host_init(pp);
  382. if (ret) {
  383. dev_err(dev, "failed to initialize host\n");
  384. return ret;
  385. }
  386. return 0;
  387. }
  388. static const struct dw_pcie_ops dw_pcie_ops = {
  389. .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
  390. .start_link = dra7xx_pcie_establish_link,
  391. .stop_link = dra7xx_pcie_stop_link,
  392. .link_up = dra7xx_pcie_link_up,
  393. };
  394. static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
  395. {
  396. int phy_count = dra7xx->phy_count;
  397. while (phy_count--) {
  398. phy_power_off(dra7xx->phy[phy_count]);
  399. phy_exit(dra7xx->phy[phy_count]);
  400. }
  401. }
  402. static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
  403. {
  404. int phy_count = dra7xx->phy_count;
  405. int ret;
  406. int i;
  407. for (i = 0; i < phy_count; i++) {
  408. ret = phy_init(dra7xx->phy[i]);
  409. if (ret < 0)
  410. goto err_phy;
  411. ret = phy_power_on(dra7xx->phy[i]);
  412. if (ret < 0) {
  413. phy_exit(dra7xx->phy[i]);
  414. goto err_phy;
  415. }
  416. }
  417. return 0;
  418. err_phy:
  419. while (--i >= 0) {
  420. phy_power_off(dra7xx->phy[i]);
  421. phy_exit(dra7xx->phy[i]);
  422. }
  423. return ret;
  424. }
  425. static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
  426. .mode = DW_PCIE_RC_TYPE,
  427. };
  428. static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
  429. .mode = DW_PCIE_EP_TYPE,
  430. };
  431. static const struct of_device_id of_dra7xx_pcie_match[] = {
  432. {
  433. .compatible = "ti,dra7-pcie",
  434. .data = &dra7xx_pcie_rc_of_data,
  435. },
  436. {
  437. .compatible = "ti,dra7-pcie-ep",
  438. .data = &dra7xx_pcie_ep_of_data,
  439. },
  440. {},
  441. };
  442. /*
  443. * dra7xx_pcie_ep_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
  444. * @dra7xx: the dra7xx device where the workaround should be applied
  445. *
  446. * Access to the PCIe slave port that are not 32-bit aligned will result
  447. * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
  448. * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
  449. * 0x3.
  450. *
  451. * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
  452. */
  453. static int dra7xx_pcie_ep_unaligned_memaccess(struct device *dev)
  454. {
  455. int ret;
  456. struct device_node *np = dev->of_node;
  457. struct of_phandle_args args;
  458. struct regmap *regmap;
  459. regmap = syscon_regmap_lookup_by_phandle(np,
  460. "ti,syscon-unaligned-access");
  461. if (IS_ERR(regmap)) {
  462. dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
  463. return -EINVAL;
  464. }
  465. ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access",
  466. 2, 0, &args);
  467. if (ret) {
  468. dev_err(dev, "failed to parse ti,syscon-unaligned-access\n");
  469. return ret;
  470. }
  471. ret = regmap_update_bits(regmap, args.args[0], args.args[1],
  472. args.args[1]);
  473. if (ret)
  474. dev_err(dev, "failed to enable unaligned access\n");
  475. of_node_put(args.np);
  476. return ret;
  477. }
  478. static int __init dra7xx_pcie_probe(struct platform_device *pdev)
  479. {
  480. u32 reg;
  481. int ret;
  482. int irq;
  483. int i;
  484. int phy_count;
  485. struct phy **phy;
  486. void __iomem *base;
  487. struct resource *res;
  488. struct dw_pcie *pci;
  489. struct pcie_port *pp;
  490. struct dra7xx_pcie *dra7xx;
  491. struct device *dev = &pdev->dev;
  492. struct device_node *np = dev->of_node;
  493. char name[10];
  494. struct gpio_desc *reset;
  495. const struct of_device_id *match;
  496. const struct dra7xx_pcie_of_data *data;
  497. enum dw_pcie_device_mode mode;
  498. match = of_match_device(of_match_ptr(of_dra7xx_pcie_match), dev);
  499. if (!match)
  500. return -EINVAL;
  501. data = (struct dra7xx_pcie_of_data *)match->data;
  502. mode = (enum dw_pcie_device_mode)data->mode;
  503. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  504. if (!dra7xx)
  505. return -ENOMEM;
  506. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  507. if (!pci)
  508. return -ENOMEM;
  509. pci->dev = dev;
  510. pci->ops = &dw_pcie_ops;
  511. pp = &pci->pp;
  512. pp->ops = &dra7xx_pcie_host_ops;
  513. irq = platform_get_irq(pdev, 0);
  514. if (irq < 0) {
  515. dev_err(dev, "missing IRQ resource: %d\n", irq);
  516. return irq;
  517. }
  518. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
  519. base = devm_ioremap_nocache(dev, res->start, resource_size(res));
  520. if (!base)
  521. return -ENOMEM;
  522. phy_count = of_property_count_strings(np, "phy-names");
  523. if (phy_count < 0) {
  524. dev_err(dev, "unable to find the strings\n");
  525. return phy_count;
  526. }
  527. phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
  528. if (!phy)
  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. }
  536. dra7xx->base = base;
  537. dra7xx->phy = phy;
  538. dra7xx->pci = pci;
  539. dra7xx->phy_count = phy_count;
  540. ret = dra7xx_pcie_enable_phy(dra7xx);
  541. if (ret) {
  542. dev_err(dev, "failed to enable phy\n");
  543. return ret;
  544. }
  545. platform_set_drvdata(pdev, dra7xx);
  546. pm_runtime_enable(dev);
  547. ret = pm_runtime_get_sync(dev);
  548. if (ret < 0) {
  549. dev_err(dev, "pm_runtime_get_sync failed\n");
  550. goto err_get_sync;
  551. }
  552. reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  553. if (IS_ERR(reset)) {
  554. ret = PTR_ERR(reset);
  555. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  556. goto err_gpio;
  557. }
  558. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  559. reg &= ~LTSSM_EN;
  560. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  561. dra7xx->link_gen = of_pci_get_max_link_speed(np);
  562. if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2)
  563. dra7xx->link_gen = 2;
  564. switch (mode) {
  565. case DW_PCIE_RC_TYPE:
  566. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  567. DEVICE_TYPE_RC);
  568. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  569. if (ret < 0)
  570. goto err_gpio;
  571. break;
  572. case DW_PCIE_EP_TYPE:
  573. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  574. DEVICE_TYPE_EP);
  575. ret = dra7xx_pcie_ep_unaligned_memaccess(dev);
  576. if (ret)
  577. goto err_gpio;
  578. ret = dra7xx_add_pcie_ep(dra7xx, pdev);
  579. if (ret < 0)
  580. goto err_gpio;
  581. break;
  582. default:
  583. dev_err(dev, "INVALID device type %d\n", mode);
  584. }
  585. dra7xx->mode = mode;
  586. ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
  587. IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
  588. if (ret) {
  589. dev_err(dev, "failed to request irq\n");
  590. goto err_gpio;
  591. }
  592. return 0;
  593. err_gpio:
  594. pm_runtime_put(dev);
  595. err_get_sync:
  596. pm_runtime_disable(dev);
  597. dra7xx_pcie_disable_phy(dra7xx);
  598. return ret;
  599. }
  600. #ifdef CONFIG_PM_SLEEP
  601. static int dra7xx_pcie_suspend(struct device *dev)
  602. {
  603. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  604. struct dw_pcie *pci = dra7xx->pci;
  605. u32 val;
  606. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  607. return 0;
  608. /* clear MSE */
  609. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  610. val &= ~PCI_COMMAND_MEMORY;
  611. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  612. return 0;
  613. }
  614. static int dra7xx_pcie_resume(struct device *dev)
  615. {
  616. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  617. struct dw_pcie *pci = dra7xx->pci;
  618. u32 val;
  619. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  620. return 0;
  621. /* set MSE */
  622. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  623. val |= PCI_COMMAND_MEMORY;
  624. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  625. return 0;
  626. }
  627. static int dra7xx_pcie_suspend_noirq(struct device *dev)
  628. {
  629. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  630. dra7xx_pcie_disable_phy(dra7xx);
  631. return 0;
  632. }
  633. static int dra7xx_pcie_resume_noirq(struct device *dev)
  634. {
  635. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  636. int ret;
  637. ret = dra7xx_pcie_enable_phy(dra7xx);
  638. if (ret) {
  639. dev_err(dev, "failed to enable phy\n");
  640. return ret;
  641. }
  642. return 0;
  643. }
  644. #endif
  645. static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
  646. SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
  647. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
  648. dra7xx_pcie_resume_noirq)
  649. };
  650. static struct platform_driver dra7xx_pcie_driver = {
  651. .driver = {
  652. .name = "dra7-pcie",
  653. .of_match_table = of_dra7xx_pcie_match,
  654. .suppress_bind_attrs = true,
  655. .pm = &dra7xx_pcie_pm_ops,
  656. },
  657. };
  658. builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);