pcie_imx.c 19 KB

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