pcie_imx.c 17 KB

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