pci-dra7xx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 <[email protected]>
  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/irqdomain.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_gpio.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 pci_addr)
  100. {
  101. return pci_addr & DRA7XX_CPU_TO_BUS_ADDR;
  102. }
  103. static int 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_add_linear(pcie_intc_node, PCI_NUM_INTX,
  293. &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. .host_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_legacy_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. enum pci_epc_irq_type 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_EPC_IRQ_LEGACY:
  334. dra7xx_pcie_raise_legacy_irq(dra7xx);
  335. break;
  336. case PCI_EPC_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. .linkup_notifier = true,
  346. .msi_capable = true,
  347. .msix_capable = false,
  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. .ep_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. return 0;
  381. }
  382. static int dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
  383. struct platform_device *pdev)
  384. {
  385. int ret;
  386. struct dw_pcie *pci = dra7xx->pci;
  387. struct dw_pcie_rp *pp = &pci->pp;
  388. struct device *dev = pci->dev;
  389. pp->irq = platform_get_irq(pdev, 1);
  390. if (pp->irq < 0)
  391. return pp->irq;
  392. /* MSI IRQ is muxed */
  393. pp->msi_irq[0] = -ENODEV;
  394. ret = dra7xx_pcie_init_irq_domain(pp);
  395. if (ret < 0)
  396. return ret;
  397. pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc_dbics");
  398. if (IS_ERR(pci->dbi_base))
  399. return PTR_ERR(pci->dbi_base);
  400. pp->ops = &dra7xx_pcie_host_ops;
  401. ret = dw_pcie_host_init(pp);
  402. if (ret) {
  403. dev_err(dev, "failed to initialize host\n");
  404. return ret;
  405. }
  406. return 0;
  407. }
  408. static const struct dw_pcie_ops dw_pcie_ops = {
  409. .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
  410. .start_link = dra7xx_pcie_establish_link,
  411. .stop_link = dra7xx_pcie_stop_link,
  412. .link_up = dra7xx_pcie_link_up,
  413. };
  414. static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
  415. {
  416. int phy_count = dra7xx->phy_count;
  417. while (phy_count--) {
  418. phy_power_off(dra7xx->phy[phy_count]);
  419. phy_exit(dra7xx->phy[phy_count]);
  420. }
  421. }
  422. static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
  423. {
  424. int phy_count = dra7xx->phy_count;
  425. int ret;
  426. int i;
  427. for (i = 0; i < phy_count; i++) {
  428. ret = phy_set_mode(dra7xx->phy[i], PHY_MODE_PCIE);
  429. if (ret < 0)
  430. goto err_phy;
  431. ret = phy_init(dra7xx->phy[i]);
  432. if (ret < 0)
  433. goto err_phy;
  434. ret = phy_power_on(dra7xx->phy[i]);
  435. if (ret < 0) {
  436. phy_exit(dra7xx->phy[i]);
  437. goto err_phy;
  438. }
  439. }
  440. return 0;
  441. err_phy:
  442. while (--i >= 0) {
  443. phy_power_off(dra7xx->phy[i]);
  444. phy_exit(dra7xx->phy[i]);
  445. }
  446. return ret;
  447. }
  448. static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
  449. .mode = DW_PCIE_RC_TYPE,
  450. };
  451. static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
  452. .mode = DW_PCIE_EP_TYPE,
  453. };
  454. static const struct dra7xx_pcie_of_data dra746_pcie_rc_of_data = {
  455. .b1co_mode_sel_mask = BIT(2),
  456. .mode = DW_PCIE_RC_TYPE,
  457. };
  458. static const struct dra7xx_pcie_of_data dra726_pcie_rc_of_data = {
  459. .b1co_mode_sel_mask = GENMASK(3, 2),
  460. .mode = DW_PCIE_RC_TYPE,
  461. };
  462. static const struct dra7xx_pcie_of_data dra746_pcie_ep_of_data = {
  463. .b1co_mode_sel_mask = BIT(2),
  464. .mode = DW_PCIE_EP_TYPE,
  465. };
  466. static const struct dra7xx_pcie_of_data dra726_pcie_ep_of_data = {
  467. .b1co_mode_sel_mask = GENMASK(3, 2),
  468. .mode = DW_PCIE_EP_TYPE,
  469. };
  470. static const struct of_device_id of_dra7xx_pcie_match[] = {
  471. {
  472. .compatible = "ti,dra7-pcie",
  473. .data = &dra7xx_pcie_rc_of_data,
  474. },
  475. {
  476. .compatible = "ti,dra7-pcie-ep",
  477. .data = &dra7xx_pcie_ep_of_data,
  478. },
  479. {
  480. .compatible = "ti,dra746-pcie-rc",
  481. .data = &dra746_pcie_rc_of_data,
  482. },
  483. {
  484. .compatible = "ti,dra726-pcie-rc",
  485. .data = &dra726_pcie_rc_of_data,
  486. },
  487. {
  488. .compatible = "ti,dra746-pcie-ep",
  489. .data = &dra746_pcie_ep_of_data,
  490. },
  491. {
  492. .compatible = "ti,dra726-pcie-ep",
  493. .data = &dra726_pcie_ep_of_data,
  494. },
  495. {},
  496. };
  497. MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
  498. /*
  499. * dra7xx_pcie_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
  500. * @dra7xx: the dra7xx device where the workaround should be applied
  501. *
  502. * Access to the PCIe slave port that are not 32-bit aligned will result
  503. * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
  504. * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
  505. * 0x3.
  506. *
  507. * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
  508. */
  509. static int dra7xx_pcie_unaligned_memaccess(struct device *dev)
  510. {
  511. int ret;
  512. struct device_node *np = dev->of_node;
  513. struct of_phandle_args args;
  514. struct regmap *regmap;
  515. regmap = syscon_regmap_lookup_by_phandle(np,
  516. "ti,syscon-unaligned-access");
  517. if (IS_ERR(regmap)) {
  518. dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
  519. return -EINVAL;
  520. }
  521. ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access",
  522. 2, 0, &args);
  523. if (ret) {
  524. dev_err(dev, "failed to parse ti,syscon-unaligned-access\n");
  525. return ret;
  526. }
  527. ret = regmap_update_bits(regmap, args.args[0], args.args[1],
  528. args.args[1]);
  529. if (ret)
  530. dev_err(dev, "failed to enable unaligned access\n");
  531. of_node_put(args.np);
  532. return ret;
  533. }
  534. static int dra7xx_pcie_configure_two_lane(struct device *dev,
  535. u32 b1co_mode_sel_mask)
  536. {
  537. struct device_node *np = dev->of_node;
  538. struct regmap *pcie_syscon;
  539. unsigned int pcie_reg;
  540. u32 mask;
  541. u32 val;
  542. pcie_syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-lane-sel");
  543. if (IS_ERR(pcie_syscon)) {
  544. dev_err(dev, "unable to get ti,syscon-lane-sel\n");
  545. return -EINVAL;
  546. }
  547. if (of_property_read_u32_index(np, "ti,syscon-lane-sel", 1,
  548. &pcie_reg)) {
  549. dev_err(dev, "couldn't get lane selection reg offset\n");
  550. return -EINVAL;
  551. }
  552. mask = b1co_mode_sel_mask | PCIE_B0_B1_TSYNCEN;
  553. val = PCIE_B1C0_MODE_SEL | PCIE_B0_B1_TSYNCEN;
  554. regmap_update_bits(pcie_syscon, pcie_reg, mask, val);
  555. return 0;
  556. }
  557. static int dra7xx_pcie_probe(struct platform_device *pdev)
  558. {
  559. u32 reg;
  560. int ret;
  561. int irq;
  562. int i;
  563. int phy_count;
  564. struct phy **phy;
  565. struct device_link **link;
  566. void __iomem *base;
  567. struct dw_pcie *pci;
  568. struct dra7xx_pcie *dra7xx;
  569. struct device *dev = &pdev->dev;
  570. struct device_node *np = dev->of_node;
  571. char name[10];
  572. struct gpio_desc *reset;
  573. const struct dra7xx_pcie_of_data *data;
  574. enum dw_pcie_device_mode mode;
  575. u32 b1co_mode_sel_mask;
  576. data = of_device_get_match_data(dev);
  577. if (!data)
  578. return -EINVAL;
  579. mode = (enum dw_pcie_device_mode)data->mode;
  580. b1co_mode_sel_mask = data->b1co_mode_sel_mask;
  581. dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
  582. if (!dra7xx)
  583. return -ENOMEM;
  584. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  585. if (!pci)
  586. return -ENOMEM;
  587. pci->dev = dev;
  588. pci->ops = &dw_pcie_ops;
  589. irq = platform_get_irq(pdev, 0);
  590. if (irq < 0)
  591. return irq;
  592. base = devm_platform_ioremap_resource_byname(pdev, "ti_conf");
  593. if (IS_ERR(base))
  594. return PTR_ERR(base);
  595. phy_count = of_property_count_strings(np, "phy-names");
  596. if (phy_count < 0) {
  597. dev_err(dev, "unable to find the strings\n");
  598. return phy_count;
  599. }
  600. phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
  601. if (!phy)
  602. return -ENOMEM;
  603. link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
  604. if (!link)
  605. return -ENOMEM;
  606. dra7xx->clk = devm_clk_get_optional(dev, NULL);
  607. if (IS_ERR(dra7xx->clk))
  608. return dev_err_probe(dev, PTR_ERR(dra7xx->clk),
  609. "clock request failed");
  610. ret = clk_prepare_enable(dra7xx->clk);
  611. if (ret)
  612. return ret;
  613. for (i = 0; i < phy_count; i++) {
  614. snprintf(name, sizeof(name), "pcie-phy%d", i);
  615. phy[i] = devm_phy_get(dev, name);
  616. if (IS_ERR(phy[i]))
  617. return PTR_ERR(phy[i]);
  618. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  619. if (!link[i]) {
  620. ret = -EINVAL;
  621. goto err_link;
  622. }
  623. }
  624. dra7xx->base = base;
  625. dra7xx->phy = phy;
  626. dra7xx->pci = pci;
  627. dra7xx->phy_count = phy_count;
  628. if (phy_count == 2) {
  629. ret = dra7xx_pcie_configure_two_lane(dev, b1co_mode_sel_mask);
  630. if (ret < 0)
  631. dra7xx->phy_count = 1; /* Fallback to x1 lane mode */
  632. }
  633. ret = dra7xx_pcie_enable_phy(dra7xx);
  634. if (ret) {
  635. dev_err(dev, "failed to enable phy\n");
  636. return ret;
  637. }
  638. platform_set_drvdata(pdev, dra7xx);
  639. pm_runtime_enable(dev);
  640. ret = pm_runtime_get_sync(dev);
  641. if (ret < 0) {
  642. dev_err(dev, "pm_runtime_get_sync failed\n");
  643. goto err_get_sync;
  644. }
  645. reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  646. if (IS_ERR(reset)) {
  647. ret = PTR_ERR(reset);
  648. dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
  649. goto err_gpio;
  650. }
  651. reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
  652. reg &= ~LTSSM_EN;
  653. dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
  654. switch (mode) {
  655. case DW_PCIE_RC_TYPE:
  656. if (!IS_ENABLED(CONFIG_PCI_DRA7XX_HOST)) {
  657. ret = -ENODEV;
  658. goto err_gpio;
  659. }
  660. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  661. DEVICE_TYPE_RC);
  662. ret = dra7xx_pcie_unaligned_memaccess(dev);
  663. if (ret)
  664. dev_err(dev, "WA for Errata i870 not applied\n");
  665. ret = dra7xx_add_pcie_port(dra7xx, pdev);
  666. if (ret < 0)
  667. goto err_gpio;
  668. break;
  669. case DW_PCIE_EP_TYPE:
  670. if (!IS_ENABLED(CONFIG_PCI_DRA7XX_EP)) {
  671. ret = -ENODEV;
  672. goto err_gpio;
  673. }
  674. dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
  675. DEVICE_TYPE_EP);
  676. ret = dra7xx_pcie_unaligned_memaccess(dev);
  677. if (ret)
  678. goto err_gpio;
  679. ret = dra7xx_add_pcie_ep(dra7xx, pdev);
  680. if (ret < 0)
  681. goto err_gpio;
  682. break;
  683. default:
  684. dev_err(dev, "INVALID device type %d\n", mode);
  685. }
  686. dra7xx->mode = mode;
  687. ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
  688. IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
  689. if (ret) {
  690. dev_err(dev, "failed to request irq\n");
  691. goto err_gpio;
  692. }
  693. return 0;
  694. err_gpio:
  695. err_get_sync:
  696. pm_runtime_put(dev);
  697. pm_runtime_disable(dev);
  698. dra7xx_pcie_disable_phy(dra7xx);
  699. err_link:
  700. while (--i >= 0)
  701. device_link_del(link[i]);
  702. return ret;
  703. }
  704. static int dra7xx_pcie_suspend(struct device *dev)
  705. {
  706. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  707. struct dw_pcie *pci = dra7xx->pci;
  708. u32 val;
  709. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  710. return 0;
  711. /* clear MSE */
  712. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  713. val &= ~PCI_COMMAND_MEMORY;
  714. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  715. return 0;
  716. }
  717. static int dra7xx_pcie_resume(struct device *dev)
  718. {
  719. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  720. struct dw_pcie *pci = dra7xx->pci;
  721. u32 val;
  722. if (dra7xx->mode != DW_PCIE_RC_TYPE)
  723. return 0;
  724. /* set MSE */
  725. val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
  726. val |= PCI_COMMAND_MEMORY;
  727. dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
  728. return 0;
  729. }
  730. static int dra7xx_pcie_suspend_noirq(struct device *dev)
  731. {
  732. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  733. dra7xx_pcie_disable_phy(dra7xx);
  734. return 0;
  735. }
  736. static int dra7xx_pcie_resume_noirq(struct device *dev)
  737. {
  738. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  739. int ret;
  740. ret = dra7xx_pcie_enable_phy(dra7xx);
  741. if (ret) {
  742. dev_err(dev, "failed to enable phy\n");
  743. return ret;
  744. }
  745. return 0;
  746. }
  747. static void dra7xx_pcie_shutdown(struct platform_device *pdev)
  748. {
  749. struct device *dev = &pdev->dev;
  750. struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
  751. int ret;
  752. dra7xx_pcie_stop_link(dra7xx->pci);
  753. ret = pm_runtime_put_sync(dev);
  754. if (ret < 0)
  755. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  756. pm_runtime_disable(dev);
  757. dra7xx_pcie_disable_phy(dra7xx);
  758. clk_disable_unprepare(dra7xx->clk);
  759. }
  760. static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
  761. SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
  762. NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
  763. dra7xx_pcie_resume_noirq)
  764. };
  765. static struct platform_driver dra7xx_pcie_driver = {
  766. .probe = dra7xx_pcie_probe,
  767. .driver = {
  768. .name = "dra7-pcie",
  769. .of_match_table = of_dra7xx_pcie_match,
  770. .suppress_bind_attrs = true,
  771. .pm = &dra7xx_pcie_pm_ops,
  772. },
  773. .shutdown = dra7xx_pcie_shutdown,
  774. };
  775. module_platform_driver(dra7xx_pcie_driver);
  776. MODULE_AUTHOR("Kishon Vijay Abraham I <[email protected]>");
  777. MODULE_DESCRIPTION("PCIe controller driver for TI DRA7xx SoCs");
  778. MODULE_LICENSE("GPL v2");