pcie_imx.c 18 KB

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