pcie_imx.c 17 KB

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