pci-dra7xx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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 - https://www.ti.com
  6. *
  7. * Authors: Kishon Vijay Abraham I <kishon@ti.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqchip/chained_irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/pci.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/resource.h>
  26. #include <linux/types.h>
  27. #include <linux/mfd/syscon.h>
  28. #include <linux/regmap.h>
  29. #include <linux/gpio/consumer.h>
  30. #include "../../pci.h"
  31. #include "pcie-designware.h"
  32. /* PCIe controller wrapper DRA7XX configuration registers */
  33. #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024
  34. #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028
  35. #define ERR_SYS BIT(0)
  36. #define ERR_FATAL BIT(1)
  37. #define ERR_NONFATAL BIT(2)
  38. #define ERR_COR BIT(3)
  39. #define ERR_AXI BIT(4)
  40. #define ERR_ECRC BIT(5)
  41. #define PME_TURN_OFF BIT(8)
  42. #define PME_TO_ACK BIT(9)
  43. #define PM_PME BIT(10)
  44. #define LINK_REQ_RST BIT(11)
  45. #define LINK_UP_EVT BIT(12)
  46. #define CFG_BME_EVT BIT(13)
  47. #define CFG_MSE_EVT BIT(14)
  48. #define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
  49. ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
  50. LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
  51. #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034
  52. #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038
  53. #define INTA BIT(0)
  54. #define INTB BIT(1)
  55. #define INTC BIT(2)
  56. #define INTD BIT(3)
  57. #define MSI BIT(4)
  58. #define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
  59. #define PCIECTRL_TI_CONF_DEVICE_TYPE 0x0100
  60. #define DEVICE_TYPE_EP 0x0
  61. #define DEVICE_TYPE_LEG_EP 0x1
  62. #define DEVICE_TYPE_RC 0x4
  63. #define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104
  64. #define LTSSM_EN 0x1
  65. #define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C
  66. #define LINK_UP BIT(16)
  67. #define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF
  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. #define PCIE_1LANE_2LANE_SELECTION BIT(13)
  74. #define PCIE_B1C0_MODE_SEL BIT(2)
  75. #define PCIE_B0_B1_TSYNCEN BIT(0)
  76. struct dra7xx_pcie {
  77. struct dw_pcie *pci;
  78. void __iomem *base; /* DT ti_conf */
  79. int phy_count; /* DT phy-names count */
  80. struct phy **phy;
  81. struct irq_domain *irq_domain;
  82. struct clk *clk;
  83. enum dw_pcie_device_mode mode;
  84. };
  85. struct dra7xx_pcie_of_data {
  86. enum dw_pcie_device_mode mode;
  87. u32 b1co_mode_sel_mask;
  88. };
  89. #define to_dra7xx_pcie(x) dev_get_drvdata((x)->dev)
  90. static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
  91. {
  92. return readl(pcie->base + offset);
  93. }
  94. static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
  95. u32 value)
  96. {
  97. writel(value, pcie->base + offset);
  98. }
  99. static u64 dra7xx_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 cpu_addr)
  100. {
  101. return cpu_addr & DRA7XX_CPU_TO_BUS_ADDR;
  102. }
  103. static bool dra7xx_pcie_link_up(struct dw_pcie *pci)
  104. {
  105. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  106. u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
  107. return reg & LINK_UP;
  108. }
  109. static void dra7xx_pcie_stop_link(struct dw_pcie *pci)
  110. {
  111. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  112. u32 reg;
  113. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  114. reg &= ~LTSSM_EN;
  115. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  116. }
  117. static int dra7xx_pcie_establish_link(struct dw_pcie *pci)
  118. {
  119. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  120. struct device *dev = pci->dev;
  121. u32 reg;
  122. if (dw_pcie_link_up(pci)) {
  123. dev_err(dev, "link is already up\n");
  124. return 0;
  125. }
  126. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  127. reg |= LTSSM_EN;
  128. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  129. return 0;
  130. }
  131. static void dra7xx_pcie_enable_msi_interrupts(struct dra7xx_pcie *dra7xx)
  132. {
  133. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
  134. LEG_EP_INTERRUPTS | MSI);
  135. dra7xx_pcie_writel(dra7xx,
  136. PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
  137. MSI | LEG_EP_INTERRUPTS);
  138. }
  139. static void dra7xx_pcie_enable_wrapper_interrupts(struct dra7xx_pcie *dra7xx)
  140. {
  141. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
  142. INTERRUPTS);
  143. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN,
  144. INTERRUPTS);
  145. }
  146. static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
  147. {
  148. dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
  149. dra7xx_pcie_enable_msi_interrupts(dra7xx);
  150. }
  151. static int dra7xx_pcie_host_init(struct dw_pcie_rp *pp)
  152. {
  153. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  154. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  155. dra7xx_pcie_enable_interrupts(dra7xx);
  156. return 0;
  157. }
  158. static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  159. irq_hw_number_t hwirq)
  160. {
  161. irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
  162. irq_set_chip_data(irq, domain->host_data);
  163. return 0;
  164. }
  165. static const struct irq_domain_ops intx_domain_ops = {
  166. .map = dra7xx_pcie_intx_map,
  167. .xlate = pci_irqd_intx_xlate,
  168. };
  169. static int dra7xx_pcie_handle_msi(struct dw_pcie_rp *pp, int index)
  170. {
  171. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  172. unsigned long val;
  173. int pos;
  174. val = dw_pcie_readl_dbi(pci, PCIE_MSI_INTR0_STATUS +
  175. (index * MSI_REG_CTRL_BLOCK_SIZE));
  176. if (!val)
  177. return 0;
  178. pos = find_first_bit(&val, MAX_MSI_IRQS_PER_CTRL);
  179. while (pos != MAX_MSI_IRQS_PER_CTRL) {
  180. generic_handle_domain_irq(pp->irq_domain,
  181. (index * MAX_MSI_IRQS_PER_CTRL) + pos);
  182. pos++;
  183. pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL, pos);
  184. }
  185. return 1;
  186. }
  187. static void dra7xx_pcie_handle_msi_irq(struct dw_pcie_rp *pp)
  188. {
  189. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  190. int ret, i, count, num_ctrls;
  191. num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
  192. /**
  193. * Need to make sure all MSI status bits read 0 before exiting.
  194. * Else, new MSI IRQs are not registered by the wrapper. Have an
  195. * upperbound for the loop and exit the IRQ in case of IRQ flood
  196. * to avoid locking up system in interrupt context.
  197. */
  198. count = 0;
  199. do {
  200. ret = 0;
  201. for (i = 0; i < num_ctrls; i++)
  202. ret |= dra7xx_pcie_handle_msi(pp, i);
  203. count++;
  204. } while (ret && count <= 1000);
  205. if (count > 1000)
  206. dev_warn_ratelimited(pci->dev,
  207. "Too many MSI IRQs to handle\n");
  208. }
  209. static void dra7xx_pcie_msi_irq_handler(struct irq_desc *desc)
  210. {
  211. struct irq_chip *chip = irq_desc_get_chip(desc);
  212. struct dra7xx_pcie *dra7xx;
  213. struct dw_pcie_rp *pp;
  214. struct dw_pcie *pci;
  215. unsigned long reg;
  216. u32 bit;
  217. chained_irq_enter(chip, desc);
  218. pp = irq_desc_get_handler_data(desc);
  219. pci = to_dw_pcie_from_pp(pp);
  220. dra7xx = to_dra7xx_pcie(pci);
  221. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
  222. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
  223. switch (reg) {
  224. case MSI:
  225. dra7xx_pcie_handle_msi_irq(pp);
  226. break;
  227. case INTA:
  228. case INTB:
  229. case INTC:
  230. case INTD:
  231. for_each_set_bit(bit, &reg, PCI_NUM_INTX)
  232. generic_handle_domain_irq(dra7xx->irq_domain, bit);
  233. break;
  234. }
  235. chained_irq_exit(chip, desc);
  236. }
  237. static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
  238. {
  239. struct dra7xx_pcie *dra7xx = arg;
  240. struct dw_pcie *pci = dra7xx->pci;
  241. struct device *dev = pci->dev;
  242. struct dw_pcie_ep *ep = &pci->ep;
  243. u32 reg;
  244. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
  245. if (reg & ERR_SYS)
  246. dev_dbg(dev, "System Error\n");
  247. if (reg & ERR_FATAL)
  248. dev_dbg(dev, "Fatal Error\n");
  249. if (reg & ERR_NONFATAL)
  250. dev_dbg(dev, "Non Fatal Error\n");
  251. if (reg & ERR_COR)
  252. dev_dbg(dev, "Correctable Error\n");
  253. if (reg & ERR_AXI)
  254. dev_dbg(dev, "AXI tag lookup fatal Error\n");
  255. if (reg & ERR_ECRC)
  256. dev_dbg(dev, "ECRC Error\n");
  257. if (reg & PME_TURN_OFF)
  258. dev_dbg(dev,
  259. "Power Management Event Turn-Off message received\n");
  260. if (reg & PME_TO_ACK)
  261. dev_dbg(dev,
  262. "Power Management Turn-Off Ack message received\n");
  263. if (reg & PM_PME)
  264. dev_dbg(dev, "PM Power Management Event message received\n");
  265. if (reg & LINK_REQ_RST)
  266. dev_dbg(dev, "Link Request Reset\n");
  267. if (reg & LINK_UP_EVT) {
  268. if (dra7xx->mode == DW_PCIE_EP_TYPE)
  269. dw_pcie_ep_linkup(ep);
  270. dev_dbg(dev, "Link-up state change\n");
  271. }
  272. if (reg & CFG_BME_EVT)
  273. dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
  274. if (reg & CFG_MSE_EVT)
  275. dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
  276. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
  277. return IRQ_HANDLED;
  278. }
  279. static int dra7xx_pcie_init_irq_domain(struct dw_pcie_rp *pp)
  280. {
  281. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  282. struct device *dev = pci->dev;
  283. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  284. struct device_node *node = dev->of_node;
  285. struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
  286. if (!pcie_intc_node) {
  287. dev_err(dev, "No PCIe Intc node found\n");
  288. return -ENODEV;
  289. }
  290. irq_set_chained_handler_and_data(pp->irq, dra7xx_pcie_msi_irq_handler,
  291. pp);
  292. dra7xx->irq_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node),
  293. PCI_NUM_INTX, &intx_domain_ops, pp);
  294. of_node_put(pcie_intc_node);
  295. if (!dra7xx->irq_domain) {
  296. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  297. return -ENODEV;
  298. }
  299. return 0;
  300. }
  301. static const struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
  302. .init = dra7xx_pcie_host_init,
  303. };
  304. static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
  305. {
  306. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  307. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  308. enum pci_barno bar;
  309. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
  310. dw_pcie_ep_reset_bar(pci, bar);
  311. dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
  312. }
  313. static void dra7xx_pcie_raise_intx_irq(struct dra7xx_pcie *dra7xx)
  314. {
  315. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1);
  316. mdelay(1);
  317. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1);
  318. }
  319. static void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx,
  320. u8 interrupt_num)
  321. {
  322. u32 reg;
  323. reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT;
  324. reg |= MSI_REQ_GRANT;
  325. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg);
  326. }
  327. static int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  328. unsigned int type, u16 interrupt_num)
  329. {
  330. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  331. struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
  332. switch (type) {
  333. case PCI_IRQ_INTX:
  334. dra7xx_pcie_raise_intx_irq(dra7xx);
  335. break;
  336. case PCI_IRQ_MSI:
  337. dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num);
  338. break;
  339. default:
  340. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  341. }
  342. return 0;
  343. }
  344. static const struct pci_epc_features dra7xx_pcie_epc_features = {
  345. DWC_EPC_COMMON_FEATURES,
  346. .linkup_notifier = true,
  347. .msi_capable = true,
  348. };
  349. static const struct pci_epc_features*
  350. dra7xx_pcie_get_features(struct dw_pcie_ep *ep)
  351. {
  352. return &dra7xx_pcie_epc_features;
  353. }
  354. static const struct dw_pcie_ep_ops pcie_ep_ops = {
  355. .init = dra7xx_pcie_ep_init,
  356. .raise_irq = dra7xx_pcie_raise_irq,
  357. .get_features = dra7xx_pcie_get_features,
  358. };
  359. static int dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx,
  360. struct platform_device *pdev)
  361. {
  362. int ret;
  363. struct dw_pcie_ep *ep;
  364. struct device *dev = &pdev->dev;
  365. struct dw_pcie *pci = dra7xx->pci;
  366. ep = &pci->ep;
  367. ep->ops = &pcie_ep_ops;
  368. pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "ep_dbics");
  369. if (IS_ERR(pci->dbi_base))
  370. return PTR_ERR(pci->dbi_base);
  371. pci->dbi_base2 =
  372. devm_platform_ioremap_resource_byname(pdev, "ep_dbics2");
  373. if (IS_ERR(pci->dbi_base2))
  374. return PTR_ERR(pci->dbi_base2);
  375. ret = dw_pcie_ep_init(ep);
  376. if (ret) {
  377. dev_err(dev, "failed to initialize endpoint\n");
  378. return ret;
  379. }
  380. ret = dw_pcie_ep_init_registers(ep);
  381. if (ret) {
  382. dev_err(dev, "Failed to initialize DWC endpoint registers\n");
  383. dw_pcie_ep_deinit(ep);
  384. return ret;
  385. }
  386. pci_epc_init_notify(ep->epc);
  387. return 0;
  388. }
  389. static int dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  390. struct platform_device *pdev)
  391. {
  392. int ret;
  393. struct dw_pcie *pci = dra7xx->pci;
  394. struct dw_pcie_rp *pp = &pci->pp;
  395. struct device *dev = pci->dev;
  396. pp->irq = platform_get_irq(pdev, 1);
  397. if (pp->irq < 0)
  398. return pp->irq;
  399. /* MSI IRQ is muxed */
  400. pp->msi_irq[0] = -ENODEV;
  401. ret = dra7xx_pcie_init_irq_domain(pp);
  402. if (ret < 0)
  403. return ret;
  404. pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc_dbics");
  405. if (IS_ERR(pci->dbi_base))
  406. return PTR_ERR(pci->dbi_base);
  407. pp->ops = &dra7xx_pcie_host_ops;
  408. ret = dw_pcie_host_init(pp);
  409. if (ret) {
  410. dev_err(dev, "failed to initialize host\n");
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. static const struct dw_pcie_ops dw_pcie_ops = {
  416. .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
  417. .start_link = dra7xx_pcie_establish_link,
  418. .stop_link = dra7xx_pcie_stop_link,
  419. .link_up = dra7xx_pcie_link_up,
  420. };
  421. static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
  422. {
  423. int phy_count = dra7xx->phy_count;
  424. while (phy_count--) {
  425. phy_power_off(dra7xx->phy[phy_count]);
  426. phy_exit(dra7xx->phy[phy_count]);
  427. }
  428. }
  429. static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
  430. {
  431. int phy_count = dra7xx->phy_count;
  432. int ret;
  433. int i;
  434. for (i = 0; i < phy_count; i++) {
  435. ret = phy_set_mode(dra7xx->phy[i], PHY_MODE_PCIE);
  436. if (ret < 0)
  437. goto err_phy;
  438. ret = phy_init(dra7xx->phy[i]);
  439. if (ret < 0)
  440. goto err_phy;
  441. ret = phy_power_on(dra7xx->phy[i]);
  442. if (ret < 0) {
  443. phy_exit(dra7xx->phy[i]);
  444. goto err_phy;
  445. }
  446. }
  447. return 0;
  448. err_phy:
  449. while (--i >= 0) {
  450. phy_power_off(dra7xx->phy[i]);
  451. phy_exit(dra7xx->phy[i]);
  452. }
  453. return ret;
  454. }
  455. static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
  456. .mode = DW_PCIE_RC_TYPE,
  457. };
  458. static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
  459. .mode = DW_PCIE_EP_TYPE,
  460. };
  461. static const struct dra7xx_pcie_of_data dra746_pcie_rc_of_data = {
  462. .b1co_mode_sel_mask = BIT(2),
  463. .mode = DW_PCIE_RC_TYPE,
  464. };
  465. static const struct dra7xx_pcie_of_data dra726_pcie_rc_of_data = {
  466. .b1co_mode_sel_mask = GENMASK(3, 2),
  467. .mode = DW_PCIE_RC_TYPE,
  468. };
  469. static const struct dra7xx_pcie_of_data dra746_pcie_ep_of_data = {
  470. .b1co_mode_sel_mask = BIT(2),
  471. .mode = DW_PCIE_EP_TYPE,
  472. };
  473. static const struct dra7xx_pcie_of_data dra726_pcie_ep_of_data = {
  474. .b1co_mode_sel_mask = GENMASK(3, 2),
  475. .mode = DW_PCIE_EP_TYPE,
  476. };
  477. static const struct of_device_id of_dra7xx_pcie_match[] = {
  478. {
  479. .compatible = "ti,dra7-pcie",
  480. .data = &dra7xx_pcie_rc_of_data,
  481. },
  482. {
  483. .compatible = "ti,dra7-pcie-ep",
  484. .data = &dra7xx_pcie_ep_of_data,
  485. },
  486. {
  487. .compatible = "ti,dra746-pcie-rc",
  488. .data = &dra746_pcie_rc_of_data,
  489. },
  490. {
  491. .compatible = "ti,dra726-pcie-rc",
  492. .data = &dra726_pcie_rc_of_data,
  493. },
  494. {
  495. .compatible = "ti,dra746-pcie-ep",
  496. .data = &dra746_pcie_ep_of_data,
  497. },
  498. {
  499. .compatible = "ti,dra726-pcie-ep",
  500. .data = &dra726_pcie_ep_of_data,
  501. },
  502. {},
  503. };
  504. MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
  505. /*
  506. * dra7xx_pcie_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
  507. * @dra7xx: the dra7xx device where the workaround should be applied
  508. *
  509. * Access to the PCIe slave port that are not 32-bit aligned will result
  510. * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
  511. * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
  512. * 0x3.
  513. *
  514. * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
  515. */
  516. static int dra7xx_pcie_unaligned_memaccess(struct device *dev)
  517. {
  518. int ret;
  519. struct device_node *np = dev->of_node;
  520. unsigned int args[2];
  521. struct regmap *regmap;
  522. regmap = syscon_regmap_lookup_by_phandle_args(np, "ti,syscon-unaligned-access",
  523. 2, args);
  524. if (IS_ERR(regmap)) {
  525. dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
  526. return -EINVAL;
  527. }
  528. ret = regmap_update_bits(regmap, args[0], args[1], args[1]);
  529. if (ret)
  530. dev_err(dev, "failed to enable unaligned access\n");
  531. return ret;
  532. }
  533. static int dra7xx_pcie_configure_two_lane(struct device *dev,
  534. u32 b1co_mode_sel_mask)
  535. {
  536. struct device_node *np = dev->of_node;
  537. struct regmap *pcie_syscon;
  538. unsigned int pcie_reg;
  539. u32 mask;
  540. u32 val;
  541. pcie_syscon = syscon_regmap_lookup_by_phandle_args(np, "ti,syscon-lane-sel",
  542. 1, &pcie_reg);
  543. if (IS_ERR(pcie_syscon)) {
  544. dev_err(dev, "unable to get ti,syscon-lane-sel\n");
  545. return -EINVAL;
  546. }
  547. mask = b1co_mode_sel_mask | PCIE_B0_B1_TSYNCEN;
  548. val = PCIE_B1C0_MODE_SEL | PCIE_B0_B1_TSYNCEN;
  549. regmap_update_bits(pcie_syscon, pcie_reg, mask, val);
  550. return 0;
  551. }
  552. static int dra7xx_pcie_probe(struct platform_device *pdev)
  553. {
  554. u32 reg;
  555. int ret;
  556. int irq;
  557. int i;
  558. int phy_count;
  559. struct phy **phy;
  560. struct device_link **link;
  561. void __iomem *base;
  562. struct dw_pcie *pci;
  563. struct dra7xx_pcie *dra7xx;
  564. struct device *dev = &pdev->dev;
  565. struct device_node *np = dev->of_node;
  566. char name[10];
  567. struct gpio_desc *reset;
  568. const struct dra7xx_pcie_of_data *data;
  569. enum dw_pcie_device_mode mode;
  570. u32 b1co_mode_sel_mask;
  571. data = of_device_get_match_data(dev);
  572. if (!data)
  573. return -EINVAL;
  574. mode = (enum dw_pcie_device_mode)data->mode;
  575. b1co_mode_sel_mask = data->b1co_mode_sel_mask;
  576. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  577. if (!dra7xx)
  578. return -ENOMEM;
  579. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  580. if (!pci)
  581. return -ENOMEM;
  582. pci->dev = dev;
  583. pci->ops = &dw_pcie_ops;
  584. irq = platform_get_irq(pdev, 0);
  585. if (irq < 0)
  586. return irq;
  587. base = devm_platform_ioremap_resource_byname(pdev, "ti_conf");
  588. if (IS_ERR(base))
  589. return PTR_ERR(base);
  590. phy_count = of_property_count_strings(np, "phy-names");
  591. if (phy_count < 0) {
  592. dev_err(dev, "unable to find the strings\n");
  593. return phy_count;
  594. }
  595. phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
  596. if (!phy)
  597. return -ENOMEM;
  598. link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
  599. if (!link)
  600. return -ENOMEM;
  601. dra7xx->clk = devm_clk_get_optional(dev, NULL);
  602. if (IS_ERR(dra7xx->clk))
  603. return dev_err_probe(dev, PTR_ERR(dra7xx->clk),
  604. "clock request failed");
  605. ret = clk_prepare_enable(dra7xx->clk);
  606. if (ret)
  607. return ret;
  608. for (i = 0; i < phy_count; i++) {
  609. snprintf(name, sizeof(name), "pcie-phy%d", i);
  610. phy[i] = devm_phy_get(dev, name);
  611. if (IS_ERR(phy[i]))
  612. return PTR_ERR(phy[i]);
  613. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  614. if (!link[i]) {
  615. ret = -EINVAL;
  616. goto err_link;
  617. }
  618. }
  619. dra7xx->base = base;
  620. dra7xx->phy = phy;
  621. dra7xx->pci = pci;
  622. dra7xx->phy_count = phy_count;
  623. if (phy_count == 2) {
  624. ret = dra7xx_pcie_configure_two_lane(dev, b1co_mode_sel_mask);
  625. if (ret < 0)
  626. dra7xx->phy_count = 1; /* Fallback to x1 lane mode */
  627. }
  628. ret = dra7xx_pcie_enable_phy(dra7xx);
  629. if (ret) {
  630. dev_err(dev, "failed to enable phy\n");
  631. return ret;
  632. }
  633. platform_set_drvdata(pdev, dra7xx);
  634. pm_runtime_enable(dev);
  635. ret = pm_runtime_get_sync(dev);
  636. if (ret < 0) {
  637. dev_err(dev, "pm_runtime_get_sync failed\n");
  638. goto err_get_sync;
  639. }
  640. reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  641. if (IS_ERR(reset)) {
  642. ret = PTR_ERR(reset);
  643. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  644. goto err_gpio;
  645. }
  646. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  647. reg &= ~LTSSM_EN;
  648. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  649. switch (mode) {
  650. case DW_PCIE_RC_TYPE:
  651. if (!IS_ENABLED(CONFIG_PCI_DRA7XX_HOST)) {
  652. ret = -ENODEV;
  653. goto err_gpio;
  654. }
  655. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  656. DEVICE_TYPE_RC);
  657. ret = dra7xx_pcie_unaligned_memaccess(dev);
  658. if (ret)
  659. dev_err(dev, "WA for Errata i870 not applied\n");
  660. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  661. if (ret < 0)
  662. goto err_gpio;
  663. break;
  664. case DW_PCIE_EP_TYPE:
  665. if (!IS_ENABLED(CONFIG_PCI_DRA7XX_EP)) {
  666. ret = -ENODEV;
  667. goto err_gpio;
  668. }
  669. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  670. DEVICE_TYPE_EP);
  671. ret = dra7xx_pcie_unaligned_memaccess(dev);
  672. if (ret)
  673. goto err_gpio;
  674. ret = dra7xx_add_pcie_ep(dra7xx, pdev);
  675. if (ret < 0)
  676. goto err_gpio;
  677. break;
  678. default:
  679. dev_err(dev, "INVALID device type %d\n", mode);
  680. }
  681. dra7xx->mode = mode;
  682. ret = devm_request_threaded_irq(dev, irq, NULL, dra7xx_pcie_irq_handler,
  683. IRQF_SHARED | IRQF_ONESHOT,
  684. "dra7xx-pcie-main", dra7xx);
  685. if (ret) {
  686. dev_err(dev, "failed to request irq\n");
  687. goto err_deinit;
  688. }
  689. return 0;
  690. err_deinit:
  691. if (dra7xx->mode == DW_PCIE_RC_TYPE)
  692. dw_pcie_host_deinit(&dra7xx->pci->pp);
  693. else
  694. dw_pcie_ep_deinit(&dra7xx->pci->ep);
  695. err_gpio:
  696. err_get_sync:
  697. pm_runtime_put(dev);
  698. pm_runtime_disable(dev);
  699. dra7xx_pcie_disable_phy(dra7xx);
  700. err_link:
  701. while (--i >= 0)
  702. device_link_del(link[i]);
  703. return ret;
  704. }
  705. static int dra7xx_pcie_suspend(struct device *dev)
  706. {
  707. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  708. struct dw_pcie *pci = dra7xx->pci;
  709. u32 val;
  710. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  711. return 0;
  712. /* clear MSE */
  713. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  714. val &= ~PCI_COMMAND_MEMORY;
  715. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  716. return 0;
  717. }
  718. static int dra7xx_pcie_resume(struct device *dev)
  719. {
  720. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  721. struct dw_pcie *pci = dra7xx->pci;
  722. u32 val;
  723. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  724. return 0;
  725. /* set MSE */
  726. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  727. val |= PCI_COMMAND_MEMORY;
  728. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  729. return 0;
  730. }
  731. static int dra7xx_pcie_suspend_noirq(struct device *dev)
  732. {
  733. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  734. dra7xx_pcie_disable_phy(dra7xx);
  735. return 0;
  736. }
  737. static int dra7xx_pcie_resume_noirq(struct device *dev)
  738. {
  739. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  740. int ret;
  741. ret = dra7xx_pcie_enable_phy(dra7xx);
  742. if (ret) {
  743. dev_err(dev, "failed to enable phy\n");
  744. return ret;
  745. }
  746. return 0;
  747. }
  748. static void dra7xx_pcie_shutdown(struct platform_device *pdev)
  749. {
  750. struct device *dev = &pdev->dev;
  751. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  752. int ret;
  753. dra7xx_pcie_stop_link(dra7xx->pci);
  754. ret = pm_runtime_put_sync(dev);
  755. if (ret < 0)
  756. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  757. pm_runtime_disable(dev);
  758. dra7xx_pcie_disable_phy(dra7xx);
  759. clk_disable_unprepare(dra7xx->clk);
  760. }
  761. static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
  762. SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
  763. NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
  764. dra7xx_pcie_resume_noirq)
  765. };
  766. static struct platform_driver dra7xx_pcie_driver = {
  767. .probe = dra7xx_pcie_probe,
  768. .driver = {
  769. .name = "dra7-pcie",
  770. .of_match_table = of_dra7xx_pcie_match,
  771. .suppress_bind_attrs = true,
  772. .pm = &dra7xx_pcie_pm_ops,
  773. },
  774. .shutdown = dra7xx_pcie_shutdown,
  775. };
  776. module_platform_driver(dra7xx_pcie_driver);
  777. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  778. MODULE_DESCRIPTION("PCIe controller driver for TI DRA7xx SoCs");
  779. MODULE_LICENSE("GPL v2");