pcie_imx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Freescale i.MX6 PCI Express Root-Complex driver
  4. *
  5. * Copyright (C) 2013 Marek Vasut <marex@denx.de>
  6. *
  7. * Based on upstream Linux kernel driver:
  8. * pci-imx6.c: Sean Cross <xobs@kosagi.com>
  9. * pcie-designware.c: Jingoo Han <jg1.han@samsung.com>
  10. */
  11. #include <common.h>
  12. #include <init.h>
  13. #include <log.h>
  14. #include <malloc.h>
  15. #include <pci.h>
  16. #include <power/regulator.h>
  17. #include <asm/arch/clock.h>
  18. #include <asm/arch/iomux.h>
  19. #include <asm/arch/crm_regs.h>
  20. #include <asm/gpio.h>
  21. #include <asm/io.h>
  22. #include <dm.h>
  23. #include <linux/delay.h>
  24. #include <linux/sizes.h>
  25. #include <errno.h>
  26. #include <asm/arch/sys_proto.h>
  27. #define PCI_ACCESS_READ 0
  28. #define PCI_ACCESS_WRITE 1
  29. #ifdef CONFIG_MX6SX
  30. #define MX6_DBI_ADDR 0x08ffc000
  31. #define MX6_IO_ADDR 0x08000000
  32. #define MX6_MEM_ADDR 0x08100000
  33. #define MX6_ROOT_ADDR 0x08f00000
  34. #else
  35. #define MX6_DBI_ADDR 0x01ffc000
  36. #define MX6_IO_ADDR 0x01000000
  37. #define MX6_MEM_ADDR 0x01100000
  38. #define MX6_ROOT_ADDR 0x01f00000
  39. #endif
  40. #define MX6_DBI_SIZE 0x4000
  41. #define MX6_IO_SIZE 0x100000
  42. #define MX6_MEM_SIZE 0xe00000
  43. #define MX6_ROOT_SIZE 0xfc000
  44. /* PCIe Port Logic registers (memory-mapped) */
  45. #define PL_OFFSET 0x700
  46. #define PCIE_PL_PFLR (PL_OFFSET + 0x08)
  47. #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
  48. #define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
  49. #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
  50. #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
  51. #define PCIE_PHY_DEBUG_R1_LINK_UP (1 << 4)
  52. #define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (1 << 29)
  53. #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
  54. #define PCIE_PHY_CTRL_DATA_LOC 0
  55. #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
  56. #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
  57. #define PCIE_PHY_CTRL_WR_LOC 18
  58. #define PCIE_PHY_CTRL_RD_LOC 19
  59. #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
  60. #define PCIE_PHY_STAT_DATA_LOC 0
  61. #define PCIE_PHY_STAT_ACK_LOC 16
  62. /* PHY registers (not memory-mapped) */
  63. #define PCIE_PHY_RX_ASIC_OUT 0x100D
  64. #define PHY_RX_OVRD_IN_LO 0x1005
  65. #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
  66. #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
  67. #define PCIE_PHY_PUP_REQ (1 << 7)
  68. /* iATU registers */
  69. #define PCIE_ATU_VIEWPORT 0x900
  70. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  71. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  72. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  73. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  74. #define PCIE_ATU_CR1 0x904
  75. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  76. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  77. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  78. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  79. #define PCIE_ATU_CR2 0x908
  80. #define PCIE_ATU_ENABLE (0x1 << 31)
  81. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  82. #define PCIE_ATU_LOWER_BASE 0x90C
  83. #define PCIE_ATU_UPPER_BASE 0x910
  84. #define PCIE_ATU_LIMIT 0x914
  85. #define PCIE_ATU_LOWER_TARGET 0x918
  86. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  87. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  88. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  89. #define PCIE_ATU_UPPER_TARGET 0x91C
  90. struct imx_pcie_priv {
  91. void __iomem *dbi_base;
  92. void __iomem *cfg_base;
  93. struct gpio_desc reset_gpio;
  94. bool reset_active_high;
  95. struct udevice *vpcie;
  96. };
  97. /*
  98. * PHY access functions
  99. */
  100. static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
  101. {
  102. u32 val;
  103. u32 max_iterations = 10;
  104. u32 wait_counter = 0;
  105. do {
  106. val = readl(dbi_base + PCIE_PHY_STAT);
  107. val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
  108. wait_counter++;
  109. if (val == exp_val)
  110. return 0;
  111. udelay(1);
  112. } while (wait_counter < max_iterations);
  113. return -ETIMEDOUT;
  114. }
  115. static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
  116. {
  117. u32 val;
  118. int ret;
  119. val = addr << PCIE_PHY_CTRL_DATA_LOC;
  120. writel(val, dbi_base + PCIE_PHY_CTRL);
  121. val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
  122. writel(val, dbi_base + PCIE_PHY_CTRL);
  123. ret = pcie_phy_poll_ack(dbi_base, 1);
  124. if (ret)
  125. return ret;
  126. val = addr << PCIE_PHY_CTRL_DATA_LOC;
  127. writel(val, dbi_base + PCIE_PHY_CTRL);
  128. ret = pcie_phy_poll_ack(dbi_base, 0);
  129. if (ret)
  130. return ret;
  131. return 0;
  132. }
  133. /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
  134. static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
  135. {
  136. u32 val, phy_ctl;
  137. int ret;
  138. ret = pcie_phy_wait_ack(dbi_base, addr);
  139. if (ret)
  140. return ret;
  141. /* assert Read signal */
  142. phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
  143. writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
  144. ret = pcie_phy_poll_ack(dbi_base, 1);
  145. if (ret)
  146. return ret;
  147. val = readl(dbi_base + PCIE_PHY_STAT);
  148. *data = val & 0xffff;
  149. /* deassert Read signal */
  150. writel(0x00, dbi_base + PCIE_PHY_CTRL);
  151. ret = pcie_phy_poll_ack(dbi_base, 0);
  152. if (ret)
  153. return ret;
  154. return 0;
  155. }
  156. static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
  157. {
  158. u32 var;
  159. int ret;
  160. /* write addr */
  161. /* cap addr */
  162. ret = pcie_phy_wait_ack(dbi_base, addr);
  163. if (ret)
  164. return ret;
  165. var = data << PCIE_PHY_CTRL_DATA_LOC;
  166. writel(var, dbi_base + PCIE_PHY_CTRL);
  167. /* capture data */
  168. var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
  169. writel(var, dbi_base + PCIE_PHY_CTRL);
  170. ret = pcie_phy_poll_ack(dbi_base, 1);
  171. if (ret)
  172. return ret;
  173. /* deassert cap data */
  174. var = data << PCIE_PHY_CTRL_DATA_LOC;
  175. writel(var, dbi_base + PCIE_PHY_CTRL);
  176. /* wait for ack de-assertion */
  177. ret = pcie_phy_poll_ack(dbi_base, 0);
  178. if (ret)
  179. return ret;
  180. /* assert wr signal */
  181. var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
  182. writel(var, dbi_base + PCIE_PHY_CTRL);
  183. /* wait for ack */
  184. ret = pcie_phy_poll_ack(dbi_base, 1);
  185. if (ret)
  186. return ret;
  187. /* deassert wr signal */
  188. var = data << PCIE_PHY_CTRL_DATA_LOC;
  189. writel(var, dbi_base + PCIE_PHY_CTRL);
  190. /* wait for ack de-assertion */
  191. ret = pcie_phy_poll_ack(dbi_base, 0);
  192. if (ret)
  193. return ret;
  194. writel(0x0, dbi_base + PCIE_PHY_CTRL);
  195. return 0;
  196. }
  197. static int imx6_pcie_link_up(struct imx_pcie_priv *priv)
  198. {
  199. u32 rc, ltssm;
  200. int rx_valid, temp;
  201. /* link is debug bit 36, debug register 1 starts at bit 32 */
  202. rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1);
  203. if ((rc & PCIE_PHY_DEBUG_R1_LINK_UP) &&
  204. !(rc & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING))
  205. return -EAGAIN;
  206. /*
  207. * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
  208. * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
  209. * If (MAC/LTSSM.state == Recovery.RcvrLock)
  210. * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
  211. * to gen2 is stuck
  212. */
  213. pcie_phy_read(priv->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
  214. ltssm = readl(priv->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
  215. if (rx_valid & 0x01)
  216. return 0;
  217. if (ltssm != 0x0d)
  218. return 0;
  219. printf("transition to gen2 is stuck, reset PHY!\n");
  220. pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
  221. temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  222. pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
  223. udelay(3000);
  224. pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
  225. temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  226. pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
  227. return 0;
  228. }
  229. /*
  230. * iATU region setup
  231. */
  232. static int imx_pcie_regions_setup(struct imx_pcie_priv *priv)
  233. {
  234. /*
  235. * i.MX6 defines 16MB in the AXI address map for PCIe.
  236. *
  237. * That address space excepted the pcie registers is
  238. * split and defined into different regions by iATU,
  239. * with sizes and offsets as follows:
  240. *
  241. * 0x0100_0000 --- 0x010F_FFFF 1MB IORESOURCE_IO
  242. * 0x0110_0000 --- 0x01EF_FFFF 14MB IORESOURCE_MEM
  243. * 0x01F0_0000 --- 0x01FF_FFFF 1MB Cfg + Registers
  244. */
  245. /* CMD reg:I/O space, MEM space, and Bus Master Enable */
  246. setbits_le32(priv->dbi_base + PCI_COMMAND,
  247. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  248. /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */
  249. setbits_le32(priv->dbi_base + PCI_CLASS_REVISION,
  250. PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
  251. /* Region #0 is used for Outbound CFG space access. */
  252. writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
  253. writel(lower_32_bits((uintptr_t)priv->cfg_base),
  254. priv->dbi_base + PCIE_ATU_LOWER_BASE);
  255. writel(upper_32_bits((uintptr_t)priv->cfg_base),
  256. priv->dbi_base + PCIE_ATU_UPPER_BASE);
  257. writel(lower_32_bits((uintptr_t)priv->cfg_base + MX6_ROOT_SIZE),
  258. priv->dbi_base + PCIE_ATU_LIMIT);
  259. writel(0, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
  260. writel(0, priv->dbi_base + PCIE_ATU_UPPER_TARGET);
  261. writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
  262. writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2);
  263. return 0;
  264. }
  265. /*
  266. * PCI Express accessors
  267. */
  268. static void __iomem *get_bus_address(struct imx_pcie_priv *priv,
  269. pci_dev_t d, int where)
  270. {
  271. void __iomem *va_address;
  272. /* Reconfigure Region #0 */
  273. writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
  274. if (PCI_BUS(d) < 2)
  275. writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
  276. else
  277. writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1);
  278. if (PCI_BUS(d) == 0) {
  279. va_address = priv->dbi_base;
  280. } else {
  281. writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
  282. va_address = priv->cfg_base;
  283. }
  284. va_address += (where & ~0x3);
  285. return va_address;
  286. }
  287. static int imx_pcie_addr_valid(pci_dev_t d)
  288. {
  289. if ((PCI_BUS(d) == 0) && (PCI_DEV(d) > 1))
  290. return -EINVAL;
  291. if ((PCI_BUS(d) == 1) && (PCI_DEV(d) > 0))
  292. return -EINVAL;
  293. return 0;
  294. }
  295. /*
  296. * Replace the original ARM DABT handler with a simple jump-back one.
  297. *
  298. * The problem here is that if we have a PCIe bridge attached to this PCIe
  299. * controller, but no PCIe device is connected to the bridges' downstream
  300. * port, the attempt to read/write from/to the config space will produce
  301. * a DABT. This is a behavior of the controller and can not be disabled
  302. * unfortuatelly.
  303. *
  304. * To work around the problem, we backup the current DABT handler address
  305. * and replace it with our own DABT handler, which only bounces right back
  306. * into the code.
  307. */
  308. static void imx_pcie_fix_dabt_handler(bool set)
  309. {
  310. extern uint32_t *_data_abort;
  311. uint32_t *data_abort_addr = (uint32_t *)&_data_abort;
  312. static const uint32_t data_abort_bounce_handler = 0xe25ef004;
  313. uint32_t data_abort_bounce_addr = (uint32_t)&data_abort_bounce_handler;
  314. static uint32_t data_abort_backup;
  315. if (set) {
  316. data_abort_backup = *data_abort_addr;
  317. *data_abort_addr = data_abort_bounce_addr;
  318. } else {
  319. *data_abort_addr = data_abort_backup;
  320. }
  321. }
  322. static int imx_pcie_read_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
  323. int where, u32 *val)
  324. {
  325. void __iomem *va_address;
  326. int ret;
  327. ret = imx_pcie_addr_valid(d);
  328. if (ret) {
  329. *val = 0xffffffff;
  330. return 0;
  331. }
  332. va_address = get_bus_address(priv, d, where);
  333. /*
  334. * Read the PCIe config space. We must replace the DABT handler
  335. * here in case we got data abort from the PCIe controller, see
  336. * imx_pcie_fix_dabt_handler() description. Note that writing the
  337. * "val" with valid value is also imperative here as in case we
  338. * did got DABT, the val would contain random value.
  339. */
  340. imx_pcie_fix_dabt_handler(true);
  341. writel(0xffffffff, val);
  342. *val = readl(va_address);
  343. imx_pcie_fix_dabt_handler(false);
  344. return 0;
  345. }
  346. static int imx_pcie_write_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
  347. int where, u32 val)
  348. {
  349. void __iomem *va_address = NULL;
  350. int ret;
  351. ret = imx_pcie_addr_valid(d);
  352. if (ret)
  353. return ret;
  354. va_address = get_bus_address(priv, d, where);
  355. /*
  356. * Write the PCIe config space. We must replace the DABT handler
  357. * here in case we got data abort from the PCIe controller, see
  358. * imx_pcie_fix_dabt_handler() description.
  359. */
  360. imx_pcie_fix_dabt_handler(true);
  361. writel(val, va_address);
  362. imx_pcie_fix_dabt_handler(false);
  363. return 0;
  364. }
  365. /*
  366. * Initial bus setup
  367. */
  368. static int imx6_pcie_assert_core_reset(struct imx_pcie_priv *priv,
  369. bool prepare_for_boot)
  370. {
  371. struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
  372. if (is_mx6dqp())
  373. setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
  374. #if defined(CONFIG_MX6SX)
  375. struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR;
  376. /* SSP_EN is not used on MX6SX anymore */
  377. setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
  378. /* Force PCIe PHY reset */
  379. setbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
  380. /* Power up PCIe PHY */
  381. setbits_le32(&gpc_regs->cntr, PCIE_PHY_PUP_REQ);
  382. #else
  383. /*
  384. * If the bootloader already enabled the link we need some special
  385. * handling to get the core back into a state where it is safe to
  386. * touch it for configuration. As there is no dedicated reset signal
  387. * wired up for MX6QDL, we need to manually force LTSSM into "detect"
  388. * state before completely disabling LTSSM, which is a prerequisite
  389. * for core configuration.
  390. *
  391. * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
  392. * indication that the bootloader activated the link.
  393. */
  394. if ((is_mx6dq() || is_mx6sdl()) && prepare_for_boot) {
  395. u32 val, gpr1, gpr12;
  396. gpr1 = readl(&iomuxc_regs->gpr[1]);
  397. gpr12 = readl(&iomuxc_regs->gpr[12]);
  398. if ((gpr1 & IOMUXC_GPR1_PCIE_REF_CLK_EN) &&
  399. (gpr12 & IOMUXC_GPR12_PCIE_CTL_2)) {
  400. val = readl(priv->dbi_base + PCIE_PL_PFLR);
  401. val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
  402. val |= PCIE_PL_PFLR_FORCE_LINK;
  403. imx_pcie_fix_dabt_handler(true);
  404. writel(val, priv->dbi_base + PCIE_PL_PFLR);
  405. imx_pcie_fix_dabt_handler(false);
  406. gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2;
  407. writel(val, &iomuxc_regs->gpr[12]);
  408. }
  409. }
  410. setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
  411. clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
  412. #endif
  413. return 0;
  414. }
  415. static int imx6_pcie_init_phy(void)
  416. {
  417. struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
  418. clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
  419. clrsetbits_le32(&iomuxc_regs->gpr[12],
  420. IOMUXC_GPR12_DEVICE_TYPE_MASK,
  421. IOMUXC_GPR12_DEVICE_TYPE_RC);
  422. clrsetbits_le32(&iomuxc_regs->gpr[12],
  423. IOMUXC_GPR12_LOS_LEVEL_MASK,
  424. IOMUXC_GPR12_LOS_LEVEL_9);
  425. #ifdef CONFIG_MX6SX
  426. clrsetbits_le32(&iomuxc_regs->gpr[12],
  427. IOMUXC_GPR12_RX_EQ_MASK,
  428. IOMUXC_GPR12_RX_EQ_2);
  429. #endif
  430. writel((0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN1_OFFSET) |
  431. (0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_3P5DB_OFFSET) |
  432. (20 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_6DB_OFFSET) |
  433. (127 << IOMUXC_GPR8_PCS_TX_SWING_FULL_OFFSET) |
  434. (127 << IOMUXC_GPR8_PCS_TX_SWING_LOW_OFFSET),
  435. &iomuxc_regs->gpr[8]);
  436. return 0;
  437. }
  438. int imx6_pcie_toggle_power(struct udevice *vpcie)
  439. {
  440. #ifdef CFG_PCIE_IMX_POWER_GPIO
  441. gpio_request(CFG_PCIE_IMX_POWER_GPIO, "pcie_power");
  442. gpio_direction_output(CFG_PCIE_IMX_POWER_GPIO, 0);
  443. mdelay(20);
  444. gpio_set_value(CFG_PCIE_IMX_POWER_GPIO, 1);
  445. mdelay(20);
  446. gpio_free(CFG_PCIE_IMX_POWER_GPIO);
  447. #endif
  448. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  449. if (vpcie) {
  450. regulator_set_enable(vpcie, false);
  451. mdelay(20);
  452. regulator_set_enable(vpcie, true);
  453. mdelay(20);
  454. }
  455. #endif
  456. return 0;
  457. }
  458. int imx6_pcie_toggle_reset(struct gpio_desc *gpio, bool active_high)
  459. {
  460. /*
  461. * See 'PCI EXPRESS BASE SPECIFICATION, REV 3.0, SECTION 6.6.1'
  462. * for detailed understanding of the PCIe CR reset logic.
  463. *
  464. * The PCIe #PERST reset line _MUST_ be connected, otherwise your
  465. * design does not conform to the specification. You must wait at
  466. * least 20 ms after de-asserting the #PERST so the EP device can
  467. * do self-initialisation.
  468. *
  469. * In case your #PERST pin is connected to a plain GPIO pin of the
  470. * CPU, you can define CFG_PCIE_IMX_PERST_GPIO in your board's
  471. * configuration file and the condition below will handle the rest
  472. * of the reset toggling.
  473. *
  474. * In case your #PERST line of the PCIe EP device is not connected
  475. * at all, your design is broken and you should fix your design,
  476. * otherwise you will observe problems like for example the link
  477. * not coming up after rebooting the system back from running Linux
  478. * that uses the PCIe as well OR the PCIe link might not come up in
  479. * Linux at all in the first place since it's in some non-reset
  480. * state due to being previously used in U-Boot.
  481. */
  482. #ifdef CFG_PCIE_IMX_PERST_GPIO
  483. gpio_request(CFG_PCIE_IMX_PERST_GPIO, "pcie_reset");
  484. gpio_direction_output(CFG_PCIE_IMX_PERST_GPIO, 0);
  485. mdelay(20);
  486. gpio_set_value(CFG_PCIE_IMX_PERST_GPIO, 1);
  487. mdelay(20);
  488. gpio_free(CFG_PCIE_IMX_PERST_GPIO);
  489. #else
  490. if (dm_gpio_is_valid(gpio)) {
  491. /* Assert PERST# for 20ms then de-assert */
  492. dm_gpio_set_value(gpio, active_high ? 0 : 1);
  493. mdelay(20);
  494. dm_gpio_set_value(gpio, active_high ? 1 : 0);
  495. mdelay(20);
  496. } else {
  497. puts("WARNING: Make sure the PCIe #PERST line is connected!\n");
  498. }
  499. #endif
  500. return 0;
  501. }
  502. static int imx6_pcie_deassert_core_reset(struct imx_pcie_priv *priv)
  503. {
  504. struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
  505. imx6_pcie_toggle_power(priv->vpcie);
  506. enable_pcie_clock();
  507. if (is_mx6dqp())
  508. clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
  509. /*
  510. * Wait for the clock to settle a bit, when the clock are sourced
  511. * from the CPU, we need about 30 ms to settle.
  512. */
  513. mdelay(50);
  514. #if defined(CONFIG_MX6SX)
  515. /* SSP_EN is not used on MX6SX anymore */
  516. clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
  517. /* Clear PCIe PHY reset bit */
  518. clrbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
  519. #else
  520. /* Enable PCIe */
  521. clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
  522. setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
  523. #endif
  524. imx6_pcie_toggle_reset(&priv->reset_gpio, priv->reset_active_high);
  525. return 0;
  526. }
  527. static int imx_pcie_link_up(struct imx_pcie_priv *priv)
  528. {
  529. struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
  530. uint32_t tmp;
  531. int count = 0;
  532. imx6_pcie_assert_core_reset(priv, false);
  533. imx6_pcie_init_phy();
  534. imx6_pcie_deassert_core_reset(priv);
  535. imx_pcie_regions_setup(priv);
  536. /*
  537. * By default, the subordinate is set equally to the secondary
  538. * bus (0x01) when the RC boots.
  539. * This means that theoretically, only bus 1 is reachable from the RC.
  540. * Force the PCIe RC subordinate to 0xff, otherwise no downstream
  541. * devices will be detected if the enumeration is applied strictly.
  542. */
  543. tmp = readl(priv->dbi_base + 0x18);
  544. tmp |= (0xff << 16);
  545. writel(tmp, priv->dbi_base + 0x18);
  546. /*
  547. * FIXME: Force the PCIe RC to Gen1 operation
  548. * The RC must be forced into Gen1 mode before bringing the link
  549. * up, otherwise no downstream devices are detected. After the
  550. * link is up, a managed Gen1->Gen2 transition can be initiated.
  551. */
  552. tmp = readl(priv->dbi_base + 0x7c);
  553. tmp &= ~0xf;
  554. tmp |= 0x1;
  555. writel(tmp, priv->dbi_base + 0x7c);
  556. /* LTSSM enable, starting link. */
  557. setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
  558. while (!imx6_pcie_link_up(priv)) {
  559. udelay(10);
  560. count++;
  561. if (count >= 4000) {
  562. #ifdef CONFIG_PCI_SCAN_SHOW
  563. puts("PCI: pcie phy link never came up\n");
  564. #endif
  565. debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
  566. readl(priv->dbi_base + PCIE_PHY_DEBUG_R0),
  567. readl(priv->dbi_base + PCIE_PHY_DEBUG_R1));
  568. return -EINVAL;
  569. }
  570. }
  571. return 0;
  572. }
  573. static int imx_pcie_dm_read_config(const struct udevice *dev, pci_dev_t bdf,
  574. uint offset, ulong *value,
  575. enum pci_size_t size)
  576. {
  577. struct imx_pcie_priv *priv = dev_get_priv(dev);
  578. u32 tmpval;
  579. int ret;
  580. ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval);
  581. if (ret)
  582. return ret;
  583. *value = pci_conv_32_to_size(tmpval, offset, size);
  584. return 0;
  585. }
  586. static int imx_pcie_dm_write_config(struct udevice *dev, pci_dev_t bdf,
  587. uint offset, ulong value,
  588. enum pci_size_t size)
  589. {
  590. struct imx_pcie_priv *priv = dev_get_priv(dev);
  591. u32 tmpval, newval;
  592. int ret;
  593. ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval);
  594. if (ret)
  595. return ret;
  596. newval = pci_conv_size_to_32(tmpval, value, offset, size);
  597. return imx_pcie_write_cfg(priv, bdf, offset, newval);
  598. }
  599. static int imx_pcie_dm_probe(struct udevice *dev)
  600. {
  601. struct imx_pcie_priv *priv = dev_get_priv(dev);
  602. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  603. device_get_supply_regulator(dev, "vpcie-supply", &priv->vpcie);
  604. #endif
  605. /* if PERST# valid from dt then assert it */
  606. gpio_request_by_name(dev, "reset-gpio", 0, &priv->reset_gpio,
  607. GPIOD_IS_OUT);
  608. priv->reset_active_high = dev_read_bool(dev, "reset-gpio-active-high");
  609. if (dm_gpio_is_valid(&priv->reset_gpio)) {
  610. dm_gpio_set_value(&priv->reset_gpio,
  611. priv->reset_active_high ? 0 : 1);
  612. }
  613. return imx_pcie_link_up(priv);
  614. }
  615. static int imx_pcie_dm_remove(struct udevice *dev)
  616. {
  617. struct imx_pcie_priv *priv = dev_get_priv(dev);
  618. imx6_pcie_assert_core_reset(priv, true);
  619. return 0;
  620. }
  621. static int imx_pcie_of_to_plat(struct udevice *dev)
  622. {
  623. struct imx_pcie_priv *priv = dev_get_priv(dev);
  624. priv->dbi_base = devfdt_get_addr_index_ptr(dev, 0);
  625. priv->cfg_base = devfdt_get_addr_index_ptr(dev, 1);
  626. if (!priv->dbi_base || !priv->cfg_base)
  627. return -EINVAL;
  628. return 0;
  629. }
  630. static const struct dm_pci_ops imx_pcie_ops = {
  631. .read_config = imx_pcie_dm_read_config,
  632. .write_config = imx_pcie_dm_write_config,
  633. };
  634. static const struct udevice_id imx_pcie_ids[] = {
  635. { .compatible = "fsl,imx6q-pcie" },
  636. { .compatible = "fsl,imx6sx-pcie" },
  637. { }
  638. };
  639. U_BOOT_DRIVER(imx_pcie) = {
  640. .name = "imx_pcie",
  641. .id = UCLASS_PCI,
  642. .of_match = imx_pcie_ids,
  643. .ops = &imx_pcie_ops,
  644. .probe = imx_pcie_dm_probe,
  645. .remove = imx_pcie_dm_remove,
  646. .of_to_plat = imx_pcie_of_to_plat,
  647. .priv_auto = sizeof(struct imx_pcie_priv),
  648. .flags = DM_FLAG_OS_PREPARE,
  649. };