pci-imx6.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * PCIe host controller driver for Freescale i.MX6 SoCs
  3. *
  4. * Copyright (C) 2013 Kosagi
  5. * http://www.kosagi.com
  6. *
  7. * Author: Sean Cross <xobs@kosagi.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  19. #include <linux/module.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/of_device.h>
  22. #include <linux/pci.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/resource.h>
  26. #include <linux/signal.h>
  27. #include <linux/types.h>
  28. #include <linux/interrupt.h>
  29. #include "pcie-designware.h"
  30. #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
  31. enum imx6_pcie_variants {
  32. IMX6Q,
  33. IMX6SX,
  34. IMX6QP,
  35. };
  36. struct imx6_pcie {
  37. int reset_gpio;
  38. bool gpio_active_high;
  39. struct clk *pcie_bus;
  40. struct clk *pcie_phy;
  41. struct clk *pcie_inbound_axi;
  42. struct clk *pcie;
  43. struct pcie_port pp;
  44. struct regmap *iomuxc_gpr;
  45. enum imx6_pcie_variants variant;
  46. void __iomem *mem_base;
  47. u32 tx_deemph_gen1;
  48. u32 tx_deemph_gen2_3p5db;
  49. u32 tx_deemph_gen2_6db;
  50. u32 tx_swing_full;
  51. u32 tx_swing_low;
  52. int link_gen;
  53. };
  54. /* PCIe Root Complex registers (memory-mapped) */
  55. #define PCIE_RC_LCR 0x7c
  56. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
  57. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
  58. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
  59. #define PCIE_RC_LCSR 0x80
  60. /* PCIe Port Logic registers (memory-mapped) */
  61. #define PL_OFFSET 0x700
  62. #define PCIE_PL_PFLR (PL_OFFSET + 0x08)
  63. #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
  64. #define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
  65. #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
  66. #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
  67. #define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
  68. #define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
  69. #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
  70. #define PCIE_PHY_CTRL_DATA_LOC 0
  71. #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
  72. #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
  73. #define PCIE_PHY_CTRL_WR_LOC 18
  74. #define PCIE_PHY_CTRL_RD_LOC 19
  75. #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
  76. #define PCIE_PHY_STAT_ACK_LOC 16
  77. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  78. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  79. /* PHY registers (not memory-mapped) */
  80. #define PCIE_PHY_RX_ASIC_OUT 0x100D
  81. #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
  82. #define PHY_RX_OVRD_IN_LO 0x1005
  83. #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
  84. #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
  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. return pcie_phy_poll_ack(dbi_base, 0);
  114. }
  115. /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
  116. static int pcie_phy_read(void __iomem *dbi_base, int addr, int *data)
  117. {
  118. u32 val, phy_ctl;
  119. int ret;
  120. ret = pcie_phy_wait_ack(dbi_base, addr);
  121. if (ret)
  122. return ret;
  123. /* assert Read signal */
  124. phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
  125. writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
  126. ret = pcie_phy_poll_ack(dbi_base, 1);
  127. if (ret)
  128. return ret;
  129. val = readl(dbi_base + PCIE_PHY_STAT);
  130. *data = val & 0xffff;
  131. /* deassert Read signal */
  132. writel(0x00, dbi_base + PCIE_PHY_CTRL);
  133. return pcie_phy_poll_ack(dbi_base, 0);
  134. }
  135. static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
  136. {
  137. u32 var;
  138. int ret;
  139. /* write addr */
  140. /* cap addr */
  141. ret = pcie_phy_wait_ack(dbi_base, addr);
  142. if (ret)
  143. return ret;
  144. var = data << PCIE_PHY_CTRL_DATA_LOC;
  145. writel(var, dbi_base + PCIE_PHY_CTRL);
  146. /* capture data */
  147. var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
  148. writel(var, dbi_base + PCIE_PHY_CTRL);
  149. ret = pcie_phy_poll_ack(dbi_base, 1);
  150. if (ret)
  151. return ret;
  152. /* deassert cap data */
  153. var = data << PCIE_PHY_CTRL_DATA_LOC;
  154. writel(var, dbi_base + PCIE_PHY_CTRL);
  155. /* wait for ack de-assertion */
  156. ret = pcie_phy_poll_ack(dbi_base, 0);
  157. if (ret)
  158. return ret;
  159. /* assert wr signal */
  160. var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
  161. writel(var, dbi_base + PCIE_PHY_CTRL);
  162. /* wait for ack */
  163. ret = pcie_phy_poll_ack(dbi_base, 1);
  164. if (ret)
  165. return ret;
  166. /* deassert wr signal */
  167. var = data << PCIE_PHY_CTRL_DATA_LOC;
  168. writel(var, dbi_base + PCIE_PHY_CTRL);
  169. /* wait for ack de-assertion */
  170. ret = pcie_phy_poll_ack(dbi_base, 0);
  171. if (ret)
  172. return ret;
  173. writel(0x0, dbi_base + PCIE_PHY_CTRL);
  174. return 0;
  175. }
  176. static void imx6_pcie_reset_phy(struct pcie_port *pp)
  177. {
  178. u32 tmp;
  179. pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
  180. tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
  181. PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  182. pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
  183. usleep_range(2000, 3000);
  184. pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
  185. tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
  186. PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  187. pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
  188. }
  189. /* Added for PCI abort handling */
  190. static int imx6q_pcie_abort_handler(unsigned long addr,
  191. unsigned int fsr, struct pt_regs *regs)
  192. {
  193. return 0;
  194. }
  195. static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
  196. {
  197. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  198. u32 val, gpr1, gpr12;
  199. switch (imx6_pcie->variant) {
  200. case IMX6SX:
  201. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  202. IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
  203. IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
  204. /* Force PCIe PHY reset */
  205. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
  206. IMX6SX_GPR5_PCIE_BTNRST_RESET,
  207. IMX6SX_GPR5_PCIE_BTNRST_RESET);
  208. break;
  209. case IMX6QP:
  210. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  211. IMX6Q_GPR1_PCIE_SW_RST,
  212. IMX6Q_GPR1_PCIE_SW_RST);
  213. break;
  214. case IMX6Q:
  215. /*
  216. * If the bootloader already enabled the link we need some
  217. * special handling to get the core back into a state where
  218. * it is safe to touch it for configuration. As there is
  219. * no dedicated reset signal wired up for MX6QDL, we need
  220. * to manually force LTSSM into "detect" state before
  221. * completely disabling LTSSM, which is a prerequisite for
  222. * core configuration.
  223. *
  224. * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we
  225. * have a strong indication that the bootloader activated
  226. * the link.
  227. */
  228. regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
  229. regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
  230. if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
  231. (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
  232. val = readl(pp->dbi_base + PCIE_PL_PFLR);
  233. val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
  234. val |= PCIE_PL_PFLR_FORCE_LINK;
  235. writel(val, pp->dbi_base + PCIE_PL_PFLR);
  236. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  237. IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
  238. }
  239. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  240. IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
  241. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  242. IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
  243. break;
  244. }
  245. return 0;
  246. }
  247. static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
  248. {
  249. struct pcie_port *pp = &imx6_pcie->pp;
  250. int ret = 0;
  251. switch (imx6_pcie->variant) {
  252. case IMX6SX:
  253. ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
  254. if (ret) {
  255. dev_err(pp->dev, "unable to enable pcie_axi clock\n");
  256. break;
  257. }
  258. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  259. IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
  260. break;
  261. case IMX6QP: /* FALLTHROUGH */
  262. case IMX6Q:
  263. /* power up core phy and enable ref clock */
  264. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  265. IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
  266. /*
  267. * the async reset input need ref clock to sync internally,
  268. * when the ref clock comes after reset, internal synced
  269. * reset time is too short, cannot meet the requirement.
  270. * add one ~10us delay here.
  271. */
  272. udelay(10);
  273. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  274. IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
  275. break;
  276. }
  277. return ret;
  278. }
  279. static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
  280. {
  281. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  282. int ret;
  283. ret = clk_prepare_enable(imx6_pcie->pcie_phy);
  284. if (ret) {
  285. dev_err(pp->dev, "unable to enable pcie_phy clock\n");
  286. goto err_pcie_phy;
  287. }
  288. ret = clk_prepare_enable(imx6_pcie->pcie_bus);
  289. if (ret) {
  290. dev_err(pp->dev, "unable to enable pcie_bus clock\n");
  291. goto err_pcie_bus;
  292. }
  293. ret = clk_prepare_enable(imx6_pcie->pcie);
  294. if (ret) {
  295. dev_err(pp->dev, "unable to enable pcie clock\n");
  296. goto err_pcie;
  297. }
  298. ret = imx6_pcie_enable_ref_clk(imx6_pcie);
  299. if (ret) {
  300. dev_err(pp->dev, "unable to enable pcie ref clock\n");
  301. goto err_ref_clk;
  302. }
  303. /* allow the clocks to stabilize */
  304. usleep_range(200, 500);
  305. /* Some boards don't have PCIe reset GPIO. */
  306. if (gpio_is_valid(imx6_pcie->reset_gpio)) {
  307. gpio_set_value_cansleep(imx6_pcie->reset_gpio,
  308. imx6_pcie->gpio_active_high);
  309. msleep(100);
  310. gpio_set_value_cansleep(imx6_pcie->reset_gpio,
  311. !imx6_pcie->gpio_active_high);
  312. }
  313. switch (imx6_pcie->variant) {
  314. case IMX6SX:
  315. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
  316. IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
  317. break;
  318. case IMX6QP:
  319. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  320. IMX6Q_GPR1_PCIE_SW_RST, 0);
  321. usleep_range(200, 500);
  322. break;
  323. case IMX6Q: /* Nothing to do */
  324. break;
  325. }
  326. return 0;
  327. err_ref_clk:
  328. clk_disable_unprepare(imx6_pcie->pcie);
  329. err_pcie:
  330. clk_disable_unprepare(imx6_pcie->pcie_bus);
  331. err_pcie_bus:
  332. clk_disable_unprepare(imx6_pcie->pcie_phy);
  333. err_pcie_phy:
  334. return ret;
  335. }
  336. static void imx6_pcie_init_phy(struct pcie_port *pp)
  337. {
  338. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  339. if (imx6_pcie->variant == IMX6SX)
  340. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  341. IMX6SX_GPR12_PCIE_RX_EQ_MASK,
  342. IMX6SX_GPR12_PCIE_RX_EQ_2);
  343. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  344. IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
  345. /* configure constant input signal to the pcie ctrl and phy */
  346. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  347. IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
  348. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  349. IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
  350. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  351. IMX6Q_GPR8_TX_DEEMPH_GEN1,
  352. imx6_pcie->tx_deemph_gen1 << 0);
  353. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  354. IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
  355. imx6_pcie->tx_deemph_gen2_3p5db << 6);
  356. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  357. IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
  358. imx6_pcie->tx_deemph_gen2_6db << 12);
  359. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  360. IMX6Q_GPR8_TX_SWING_FULL,
  361. imx6_pcie->tx_swing_full << 18);
  362. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  363. IMX6Q_GPR8_TX_SWING_LOW,
  364. imx6_pcie->tx_swing_low << 25);
  365. }
  366. static int imx6_pcie_wait_for_link(struct pcie_port *pp)
  367. {
  368. /* check if the link is up or not */
  369. if (!dw_pcie_wait_for_link(pp))
  370. return 0;
  371. dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
  372. readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
  373. readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
  374. return -ETIMEDOUT;
  375. }
  376. static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
  377. {
  378. u32 tmp;
  379. unsigned int retries;
  380. for (retries = 0; retries < 200; retries++) {
  381. tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  382. /* Test if the speed change finished. */
  383. if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
  384. return 0;
  385. usleep_range(100, 1000);
  386. }
  387. dev_err(pp->dev, "Speed change timeout\n");
  388. return -EINVAL;
  389. }
  390. static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
  391. {
  392. struct pcie_port *pp = arg;
  393. return dw_handle_msi_irq(pp);
  394. }
  395. static int imx6_pcie_establish_link(struct pcie_port *pp)
  396. {
  397. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  398. u32 tmp;
  399. int ret;
  400. /*
  401. * Force Gen1 operation when starting the link. In case the link is
  402. * started in Gen2 mode, there is a possibility the devices on the
  403. * bus will not be detected at all. This happens with PCIe switches.
  404. */
  405. tmp = readl(pp->dbi_base + PCIE_RC_LCR);
  406. tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
  407. tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
  408. writel(tmp, pp->dbi_base + PCIE_RC_LCR);
  409. /* Start LTSSM. */
  410. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  411. IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
  412. ret = imx6_pcie_wait_for_link(pp);
  413. if (ret) {
  414. dev_info(pp->dev, "Link never came up\n");
  415. goto err_reset_phy;
  416. }
  417. if (imx6_pcie->link_gen == 2) {
  418. /* Allow Gen2 mode after the link is up. */
  419. tmp = readl(pp->dbi_base + PCIE_RC_LCR);
  420. tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
  421. tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
  422. writel(tmp, pp->dbi_base + PCIE_RC_LCR);
  423. } else {
  424. dev_info(pp->dev, "Link: Gen2 disabled\n");
  425. }
  426. /*
  427. * Start Directed Speed Change so the best possible speed both link
  428. * partners support can be negotiated.
  429. */
  430. tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  431. tmp |= PORT_LOGIC_SPEED_CHANGE;
  432. writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  433. ret = imx6_pcie_wait_for_speed_change(pp);
  434. if (ret) {
  435. dev_err(pp->dev, "Failed to bring link up!\n");
  436. goto err_reset_phy;
  437. }
  438. /* Make sure link training is finished as well! */
  439. ret = imx6_pcie_wait_for_link(pp);
  440. if (ret) {
  441. dev_err(pp->dev, "Failed to bring link up!\n");
  442. goto err_reset_phy;
  443. }
  444. tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
  445. dev_info(pp->dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
  446. return 0;
  447. err_reset_phy:
  448. dev_dbg(pp->dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
  449. readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
  450. readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
  451. imx6_pcie_reset_phy(pp);
  452. return ret;
  453. }
  454. static void imx6_pcie_host_init(struct pcie_port *pp)
  455. {
  456. imx6_pcie_assert_core_reset(pp);
  457. imx6_pcie_init_phy(pp);
  458. imx6_pcie_deassert_core_reset(pp);
  459. dw_pcie_setup_rc(pp);
  460. imx6_pcie_establish_link(pp);
  461. if (IS_ENABLED(CONFIG_PCI_MSI))
  462. dw_pcie_msi_init(pp);
  463. }
  464. static int imx6_pcie_link_up(struct pcie_port *pp)
  465. {
  466. return readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) &
  467. PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
  468. }
  469. static struct pcie_host_ops imx6_pcie_host_ops = {
  470. .link_up = imx6_pcie_link_up,
  471. .host_init = imx6_pcie_host_init,
  472. };
  473. static int __init imx6_add_pcie_port(struct pcie_port *pp,
  474. struct platform_device *pdev)
  475. {
  476. int ret;
  477. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  478. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  479. if (pp->msi_irq <= 0) {
  480. dev_err(&pdev->dev, "failed to get MSI irq\n");
  481. return -ENODEV;
  482. }
  483. ret = devm_request_irq(&pdev->dev, pp->msi_irq,
  484. imx6_pcie_msi_handler,
  485. IRQF_SHARED | IRQF_NO_THREAD,
  486. "mx6-pcie-msi", pp);
  487. if (ret) {
  488. dev_err(&pdev->dev, "failed to request MSI irq\n");
  489. return ret;
  490. }
  491. }
  492. pp->root_bus_nr = -1;
  493. pp->ops = &imx6_pcie_host_ops;
  494. ret = dw_pcie_host_init(pp);
  495. if (ret) {
  496. dev_err(&pdev->dev, "failed to initialize host\n");
  497. return ret;
  498. }
  499. return 0;
  500. }
  501. static int __init imx6_pcie_probe(struct platform_device *pdev)
  502. {
  503. struct imx6_pcie *imx6_pcie;
  504. struct pcie_port *pp;
  505. struct device_node *np = pdev->dev.of_node;
  506. struct resource *dbi_base;
  507. struct device_node *node = pdev->dev.of_node;
  508. int ret;
  509. imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
  510. if (!imx6_pcie)
  511. return -ENOMEM;
  512. pp = &imx6_pcie->pp;
  513. pp->dev = &pdev->dev;
  514. imx6_pcie->variant =
  515. (enum imx6_pcie_variants)of_device_get_match_data(&pdev->dev);
  516. /* Added for PCI abort handling */
  517. hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
  518. "imprecise external abort");
  519. dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  520. pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
  521. if (IS_ERR(pp->dbi_base))
  522. return PTR_ERR(pp->dbi_base);
  523. /* Fetch GPIOs */
  524. imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  525. imx6_pcie->gpio_active_high = of_property_read_bool(np,
  526. "reset-gpio-active-high");
  527. if (gpio_is_valid(imx6_pcie->reset_gpio)) {
  528. ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
  529. imx6_pcie->gpio_active_high ?
  530. GPIOF_OUT_INIT_HIGH :
  531. GPIOF_OUT_INIT_LOW,
  532. "PCIe reset");
  533. if (ret) {
  534. dev_err(&pdev->dev, "unable to get reset gpio\n");
  535. return ret;
  536. }
  537. }
  538. /* Fetch clocks */
  539. imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
  540. if (IS_ERR(imx6_pcie->pcie_phy)) {
  541. dev_err(&pdev->dev,
  542. "pcie_phy clock source missing or invalid\n");
  543. return PTR_ERR(imx6_pcie->pcie_phy);
  544. }
  545. imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
  546. if (IS_ERR(imx6_pcie->pcie_bus)) {
  547. dev_err(&pdev->dev,
  548. "pcie_bus clock source missing or invalid\n");
  549. return PTR_ERR(imx6_pcie->pcie_bus);
  550. }
  551. imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
  552. if (IS_ERR(imx6_pcie->pcie)) {
  553. dev_err(&pdev->dev,
  554. "pcie clock source missing or invalid\n");
  555. return PTR_ERR(imx6_pcie->pcie);
  556. }
  557. if (imx6_pcie->variant == IMX6SX) {
  558. imx6_pcie->pcie_inbound_axi = devm_clk_get(&pdev->dev,
  559. "pcie_inbound_axi");
  560. if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
  561. dev_err(&pdev->dev,
  562. "pcie_incbound_axi clock missing or invalid\n");
  563. return PTR_ERR(imx6_pcie->pcie_inbound_axi);
  564. }
  565. }
  566. /* Grab GPR config register range */
  567. imx6_pcie->iomuxc_gpr =
  568. syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
  569. if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
  570. dev_err(&pdev->dev, "unable to find iomuxc registers\n");
  571. return PTR_ERR(imx6_pcie->iomuxc_gpr);
  572. }
  573. /* Grab PCIe PHY Tx Settings */
  574. if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
  575. &imx6_pcie->tx_deemph_gen1))
  576. imx6_pcie->tx_deemph_gen1 = 0;
  577. if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
  578. &imx6_pcie->tx_deemph_gen2_3p5db))
  579. imx6_pcie->tx_deemph_gen2_3p5db = 0;
  580. if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
  581. &imx6_pcie->tx_deemph_gen2_6db))
  582. imx6_pcie->tx_deemph_gen2_6db = 20;
  583. if (of_property_read_u32(node, "fsl,tx-swing-full",
  584. &imx6_pcie->tx_swing_full))
  585. imx6_pcie->tx_swing_full = 127;
  586. if (of_property_read_u32(node, "fsl,tx-swing-low",
  587. &imx6_pcie->tx_swing_low))
  588. imx6_pcie->tx_swing_low = 127;
  589. /* Limit link speed */
  590. ret = of_property_read_u32(pp->dev->of_node, "fsl,max-link-speed",
  591. &imx6_pcie->link_gen);
  592. if (ret)
  593. imx6_pcie->link_gen = 1;
  594. ret = imx6_add_pcie_port(pp, pdev);
  595. if (ret < 0)
  596. return ret;
  597. platform_set_drvdata(pdev, imx6_pcie);
  598. return 0;
  599. }
  600. static void imx6_pcie_shutdown(struct platform_device *pdev)
  601. {
  602. struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
  603. /* bring down link, so bootloader gets clean state in case of reboot */
  604. imx6_pcie_assert_core_reset(&imx6_pcie->pp);
  605. }
  606. static const struct of_device_id imx6_pcie_of_match[] = {
  607. { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
  608. { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
  609. { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
  610. {},
  611. };
  612. MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
  613. static struct platform_driver imx6_pcie_driver = {
  614. .driver = {
  615. .name = "imx6q-pcie",
  616. .of_match_table = imx6_pcie_of_match,
  617. },
  618. .shutdown = imx6_pcie_shutdown,
  619. };
  620. /* Freescale PCIe driver does not allow module unload */
  621. static int __init imx6_pcie_init(void)
  622. {
  623. return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
  624. }
  625. module_init(imx6_pcie_init);
  626. MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
  627. MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
  628. MODULE_LICENSE("GPL v2");