pci-imx6.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Freescale i.MX6 SoCs
  4. *
  5. * Copyright (C) 2013 Kosagi
  6. * http://www.kosagi.com
  7. *
  8. * Author: Sean Cross <xobs@kosagi.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  16. #include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
  17. #include <linux/module.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_device.h>
  20. #include <linux/pci.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/resource.h>
  25. #include <linux/signal.h>
  26. #include <linux/types.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/reset.h>
  29. #include "pcie-designware.h"
  30. #define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
  31. enum imx6_pcie_variants {
  32. IMX6Q,
  33. IMX6SX,
  34. IMX6QP,
  35. IMX7D,
  36. };
  37. struct imx6_pcie {
  38. struct dw_pcie *pci;
  39. int reset_gpio;
  40. bool gpio_active_high;
  41. struct clk *pcie_bus;
  42. struct clk *pcie_phy;
  43. struct clk *pcie_inbound_axi;
  44. struct clk *pcie;
  45. struct regmap *iomuxc_gpr;
  46. struct reset_control *pciephy_reset;
  47. struct reset_control *apps_reset;
  48. enum imx6_pcie_variants variant;
  49. u32 tx_deemph_gen1;
  50. u32 tx_deemph_gen2_3p5db;
  51. u32 tx_deemph_gen2_6db;
  52. u32 tx_swing_full;
  53. u32 tx_swing_low;
  54. int link_gen;
  55. struct regulator *vpcie;
  56. };
  57. /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
  58. #define PHY_PLL_LOCK_WAIT_MAX_RETRIES 2000
  59. #define PHY_PLL_LOCK_WAIT_USLEEP_MIN 50
  60. #define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
  61. /* PCIe Root Complex registers (memory-mapped) */
  62. #define PCIE_RC_LCR 0x7c
  63. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
  64. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
  65. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
  66. #define PCIE_RC_LCSR 0x80
  67. /* PCIe Port Logic registers (memory-mapped) */
  68. #define PL_OFFSET 0x700
  69. #define PCIE_PL_PFLR (PL_OFFSET + 0x08)
  70. #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
  71. #define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
  72. #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
  73. #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
  74. #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
  75. #define PCIE_PHY_CTRL_DATA_LOC 0
  76. #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
  77. #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
  78. #define PCIE_PHY_CTRL_WR_LOC 18
  79. #define PCIE_PHY_CTRL_RD_LOC 19
  80. #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
  81. #define PCIE_PHY_STAT_ACK_LOC 16
  82. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  83. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  84. /* PHY registers (not memory-mapped) */
  85. #define PCIE_PHY_RX_ASIC_OUT 0x100D
  86. #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
  87. #define PHY_RX_OVRD_IN_LO 0x1005
  88. #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
  89. #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
  90. static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
  91. {
  92. struct dw_pcie *pci = imx6_pcie->pci;
  93. u32 val;
  94. u32 max_iterations = 10;
  95. u32 wait_counter = 0;
  96. do {
  97. val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
  98. val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
  99. wait_counter++;
  100. if (val == exp_val)
  101. return 0;
  102. udelay(1);
  103. } while (wait_counter < max_iterations);
  104. return -ETIMEDOUT;
  105. }
  106. static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
  107. {
  108. struct dw_pcie *pci = imx6_pcie->pci;
  109. u32 val;
  110. int ret;
  111. val = addr << PCIE_PHY_CTRL_DATA_LOC;
  112. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
  113. val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
  114. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
  115. ret = pcie_phy_poll_ack(imx6_pcie, 1);
  116. if (ret)
  117. return ret;
  118. val = addr << PCIE_PHY_CTRL_DATA_LOC;
  119. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
  120. return pcie_phy_poll_ack(imx6_pcie, 0);
  121. }
  122. /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
  123. static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
  124. {
  125. struct dw_pcie *pci = imx6_pcie->pci;
  126. u32 val, phy_ctl;
  127. int ret;
  128. ret = pcie_phy_wait_ack(imx6_pcie, addr);
  129. if (ret)
  130. return ret;
  131. /* assert Read signal */
  132. phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
  133. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
  134. ret = pcie_phy_poll_ack(imx6_pcie, 1);
  135. if (ret)
  136. return ret;
  137. val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
  138. *data = val & 0xffff;
  139. /* deassert Read signal */
  140. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
  141. return pcie_phy_poll_ack(imx6_pcie, 0);
  142. }
  143. static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data)
  144. {
  145. struct dw_pcie *pci = imx6_pcie->pci;
  146. u32 var;
  147. int ret;
  148. /* write addr */
  149. /* cap addr */
  150. ret = pcie_phy_wait_ack(imx6_pcie, addr);
  151. if (ret)
  152. return ret;
  153. var = data << PCIE_PHY_CTRL_DATA_LOC;
  154. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
  155. /* capture data */
  156. var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
  157. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
  158. ret = pcie_phy_poll_ack(imx6_pcie, 1);
  159. if (ret)
  160. return ret;
  161. /* deassert cap data */
  162. var = data << PCIE_PHY_CTRL_DATA_LOC;
  163. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
  164. /* wait for ack de-assertion */
  165. ret = pcie_phy_poll_ack(imx6_pcie, 0);
  166. if (ret)
  167. return ret;
  168. /* assert wr signal */
  169. var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
  170. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
  171. /* wait for ack */
  172. ret = pcie_phy_poll_ack(imx6_pcie, 1);
  173. if (ret)
  174. return ret;
  175. /* deassert wr signal */
  176. var = data << PCIE_PHY_CTRL_DATA_LOC;
  177. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
  178. /* wait for ack de-assertion */
  179. ret = pcie_phy_poll_ack(imx6_pcie, 0);
  180. if (ret)
  181. return ret;
  182. dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
  183. return 0;
  184. }
  185. static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
  186. {
  187. u32 tmp;
  188. pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
  189. tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
  190. PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  191. pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
  192. usleep_range(2000, 3000);
  193. pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
  194. tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
  195. PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  196. pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
  197. }
  198. /* Added for PCI abort handling */
  199. static int imx6q_pcie_abort_handler(unsigned long addr,
  200. unsigned int fsr, struct pt_regs *regs)
  201. {
  202. unsigned long pc = instruction_pointer(regs);
  203. unsigned long instr = *(unsigned long *)pc;
  204. int reg = (instr >> 12) & 15;
  205. /*
  206. * If the instruction being executed was a read,
  207. * make it look like it read all-ones.
  208. */
  209. if ((instr & 0x0c100000) == 0x04100000) {
  210. unsigned long val;
  211. if (instr & 0x00400000)
  212. val = 255;
  213. else
  214. val = -1;
  215. regs->uregs[reg] = val;
  216. regs->ARM_pc += 4;
  217. return 0;
  218. }
  219. if ((instr & 0x0e100090) == 0x00100090) {
  220. regs->uregs[reg] = -1;
  221. regs->ARM_pc += 4;
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
  227. {
  228. struct device *dev = imx6_pcie->pci->dev;
  229. switch (imx6_pcie->variant) {
  230. case IMX7D:
  231. reset_control_assert(imx6_pcie->pciephy_reset);
  232. reset_control_assert(imx6_pcie->apps_reset);
  233. break;
  234. case IMX6SX:
  235. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  236. IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
  237. IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
  238. /* Force PCIe PHY reset */
  239. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
  240. IMX6SX_GPR5_PCIE_BTNRST_RESET,
  241. IMX6SX_GPR5_PCIE_BTNRST_RESET);
  242. break;
  243. case IMX6QP:
  244. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  245. IMX6Q_GPR1_PCIE_SW_RST,
  246. IMX6Q_GPR1_PCIE_SW_RST);
  247. break;
  248. case IMX6Q:
  249. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  250. IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
  251. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  252. IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
  253. break;
  254. }
  255. if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
  256. int ret = regulator_disable(imx6_pcie->vpcie);
  257. if (ret)
  258. dev_err(dev, "failed to disable vpcie regulator: %d\n",
  259. ret);
  260. }
  261. }
  262. static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
  263. {
  264. struct dw_pcie *pci = imx6_pcie->pci;
  265. struct device *dev = pci->dev;
  266. int ret = 0;
  267. switch (imx6_pcie->variant) {
  268. case IMX6SX:
  269. ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
  270. if (ret) {
  271. dev_err(dev, "unable to enable pcie_axi clock\n");
  272. break;
  273. }
  274. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  275. IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
  276. break;
  277. case IMX6QP: /* FALLTHROUGH */
  278. case IMX6Q:
  279. /* power up core phy and enable ref clock */
  280. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  281. IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
  282. /*
  283. * the async reset input need ref clock to sync internally,
  284. * when the ref clock comes after reset, internal synced
  285. * reset time is too short, cannot meet the requirement.
  286. * add one ~10us delay here.
  287. */
  288. udelay(10);
  289. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  290. IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
  291. break;
  292. case IMX7D:
  293. break;
  294. }
  295. return ret;
  296. }
  297. static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
  298. {
  299. u32 val;
  300. unsigned int retries;
  301. struct device *dev = imx6_pcie->pci->dev;
  302. for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES; retries++) {
  303. regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR22, &val);
  304. if (val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED)
  305. return;
  306. usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN,
  307. PHY_PLL_LOCK_WAIT_USLEEP_MAX);
  308. }
  309. dev_err(dev, "PCIe PLL lock timeout\n");
  310. }
  311. static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
  312. {
  313. struct dw_pcie *pci = imx6_pcie->pci;
  314. struct device *dev = pci->dev;
  315. int ret;
  316. if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
  317. ret = regulator_enable(imx6_pcie->vpcie);
  318. if (ret) {
  319. dev_err(dev, "failed to enable vpcie regulator: %d\n",
  320. ret);
  321. return;
  322. }
  323. }
  324. ret = clk_prepare_enable(imx6_pcie->pcie_phy);
  325. if (ret) {
  326. dev_err(dev, "unable to enable pcie_phy clock\n");
  327. goto err_pcie_phy;
  328. }
  329. ret = clk_prepare_enable(imx6_pcie->pcie_bus);
  330. if (ret) {
  331. dev_err(dev, "unable to enable pcie_bus clock\n");
  332. goto err_pcie_bus;
  333. }
  334. ret = clk_prepare_enable(imx6_pcie->pcie);
  335. if (ret) {
  336. dev_err(dev, "unable to enable pcie clock\n");
  337. goto err_pcie;
  338. }
  339. ret = imx6_pcie_enable_ref_clk(imx6_pcie);
  340. if (ret) {
  341. dev_err(dev, "unable to enable pcie ref clock\n");
  342. goto err_ref_clk;
  343. }
  344. /* allow the clocks to stabilize */
  345. usleep_range(200, 500);
  346. /* Some boards don't have PCIe reset GPIO. */
  347. if (gpio_is_valid(imx6_pcie->reset_gpio)) {
  348. gpio_set_value_cansleep(imx6_pcie->reset_gpio,
  349. imx6_pcie->gpio_active_high);
  350. msleep(100);
  351. gpio_set_value_cansleep(imx6_pcie->reset_gpio,
  352. !imx6_pcie->gpio_active_high);
  353. }
  354. switch (imx6_pcie->variant) {
  355. case IMX7D:
  356. reset_control_deassert(imx6_pcie->pciephy_reset);
  357. imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
  358. break;
  359. case IMX6SX:
  360. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
  361. IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
  362. break;
  363. case IMX6QP:
  364. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  365. IMX6Q_GPR1_PCIE_SW_RST, 0);
  366. usleep_range(200, 500);
  367. break;
  368. case IMX6Q: /* Nothing to do */
  369. break;
  370. }
  371. return;
  372. err_ref_clk:
  373. clk_disable_unprepare(imx6_pcie->pcie);
  374. err_pcie:
  375. clk_disable_unprepare(imx6_pcie->pcie_bus);
  376. err_pcie_bus:
  377. clk_disable_unprepare(imx6_pcie->pcie_phy);
  378. err_pcie_phy:
  379. if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
  380. ret = regulator_disable(imx6_pcie->vpcie);
  381. if (ret)
  382. dev_err(dev, "failed to disable vpcie regulator: %d\n",
  383. ret);
  384. }
  385. }
  386. static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
  387. {
  388. switch (imx6_pcie->variant) {
  389. case IMX7D:
  390. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  391. IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
  392. break;
  393. case IMX6SX:
  394. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  395. IMX6SX_GPR12_PCIE_RX_EQ_MASK,
  396. IMX6SX_GPR12_PCIE_RX_EQ_2);
  397. /* FALLTHROUGH */
  398. default:
  399. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  400. IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
  401. /* configure constant input signal to the pcie ctrl and phy */
  402. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  403. IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
  404. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  405. IMX6Q_GPR8_TX_DEEMPH_GEN1,
  406. imx6_pcie->tx_deemph_gen1 << 0);
  407. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  408. IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
  409. imx6_pcie->tx_deemph_gen2_3p5db << 6);
  410. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  411. IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
  412. imx6_pcie->tx_deemph_gen2_6db << 12);
  413. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  414. IMX6Q_GPR8_TX_SWING_FULL,
  415. imx6_pcie->tx_swing_full << 18);
  416. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  417. IMX6Q_GPR8_TX_SWING_LOW,
  418. imx6_pcie->tx_swing_low << 25);
  419. break;
  420. }
  421. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  422. IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
  423. }
  424. static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
  425. {
  426. struct dw_pcie *pci = imx6_pcie->pci;
  427. struct device *dev = pci->dev;
  428. /* check if the link is up or not */
  429. if (!dw_pcie_wait_for_link(pci))
  430. return 0;
  431. dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
  432. dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
  433. dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
  434. return -ETIMEDOUT;
  435. }
  436. static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
  437. {
  438. struct dw_pcie *pci = imx6_pcie->pci;
  439. struct device *dev = pci->dev;
  440. u32 tmp;
  441. unsigned int retries;
  442. for (retries = 0; retries < 200; retries++) {
  443. tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  444. /* Test if the speed change finished. */
  445. if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
  446. return 0;
  447. usleep_range(100, 1000);
  448. }
  449. dev_err(dev, "Speed change timeout\n");
  450. return -EINVAL;
  451. }
  452. static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
  453. {
  454. struct dw_pcie *pci = imx6_pcie->pci;
  455. struct device *dev = pci->dev;
  456. u32 tmp;
  457. int ret;
  458. /*
  459. * Force Gen1 operation when starting the link. In case the link is
  460. * started in Gen2 mode, there is a possibility the devices on the
  461. * bus will not be detected at all. This happens with PCIe switches.
  462. */
  463. tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
  464. tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
  465. tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
  466. dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
  467. /* Start LTSSM. */
  468. if (imx6_pcie->variant == IMX7D)
  469. reset_control_deassert(imx6_pcie->apps_reset);
  470. else
  471. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  472. IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
  473. ret = imx6_pcie_wait_for_link(imx6_pcie);
  474. if (ret)
  475. goto err_reset_phy;
  476. if (imx6_pcie->link_gen == 2) {
  477. /* Allow Gen2 mode after the link is up. */
  478. tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
  479. tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
  480. tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
  481. dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
  482. /*
  483. * Start Directed Speed Change so the best possible
  484. * speed both link partners support can be negotiated.
  485. */
  486. tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  487. tmp |= PORT_LOGIC_SPEED_CHANGE;
  488. dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
  489. if (imx6_pcie->variant != IMX7D) {
  490. /*
  491. * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
  492. * from i.MX6 family when no link speed transition
  493. * occurs and we go Gen1 -> yep, Gen1. The difference
  494. * is that, in such case, it will not be cleared by HW
  495. * which will cause the following code to report false
  496. * failure.
  497. */
  498. ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
  499. if (ret) {
  500. dev_err(dev, "Failed to bring link up!\n");
  501. goto err_reset_phy;
  502. }
  503. }
  504. /* Make sure link training is finished as well! */
  505. ret = imx6_pcie_wait_for_link(imx6_pcie);
  506. if (ret) {
  507. dev_err(dev, "Failed to bring link up!\n");
  508. goto err_reset_phy;
  509. }
  510. } else {
  511. dev_info(dev, "Link: Gen2 disabled\n");
  512. }
  513. tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
  514. dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
  515. return 0;
  516. err_reset_phy:
  517. dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
  518. dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
  519. dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
  520. imx6_pcie_reset_phy(imx6_pcie);
  521. return ret;
  522. }
  523. static int imx6_pcie_host_init(struct pcie_port *pp)
  524. {
  525. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  526. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
  527. imx6_pcie_assert_core_reset(imx6_pcie);
  528. imx6_pcie_init_phy(imx6_pcie);
  529. imx6_pcie_deassert_core_reset(imx6_pcie);
  530. dw_pcie_setup_rc(pp);
  531. imx6_pcie_establish_link(imx6_pcie);
  532. if (IS_ENABLED(CONFIG_PCI_MSI))
  533. dw_pcie_msi_init(pp);
  534. return 0;
  535. }
  536. static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
  537. .host_init = imx6_pcie_host_init,
  538. };
  539. static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
  540. struct platform_device *pdev)
  541. {
  542. struct dw_pcie *pci = imx6_pcie->pci;
  543. struct pcie_port *pp = &pci->pp;
  544. struct device *dev = &pdev->dev;
  545. int ret;
  546. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  547. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  548. if (pp->msi_irq <= 0) {
  549. dev_err(dev, "failed to get MSI irq\n");
  550. return -ENODEV;
  551. }
  552. }
  553. pp->ops = &imx6_pcie_host_ops;
  554. ret = dw_pcie_host_init(pp);
  555. if (ret) {
  556. dev_err(dev, "failed to initialize host\n");
  557. return ret;
  558. }
  559. return 0;
  560. }
  561. static const struct dw_pcie_ops dw_pcie_ops = {
  562. /* No special ops needed, but pcie-designware still expects this struct */
  563. };
  564. static int imx6_pcie_probe(struct platform_device *pdev)
  565. {
  566. struct device *dev = &pdev->dev;
  567. struct dw_pcie *pci;
  568. struct imx6_pcie *imx6_pcie;
  569. struct resource *dbi_base;
  570. struct device_node *node = dev->of_node;
  571. int ret;
  572. imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
  573. if (!imx6_pcie)
  574. return -ENOMEM;
  575. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  576. if (!pci)
  577. return -ENOMEM;
  578. pci->dev = dev;
  579. pci->ops = &dw_pcie_ops;
  580. imx6_pcie->pci = pci;
  581. imx6_pcie->variant =
  582. (enum imx6_pcie_variants)of_device_get_match_data(dev);
  583. dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  584. pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
  585. if (IS_ERR(pci->dbi_base))
  586. return PTR_ERR(pci->dbi_base);
  587. /* Fetch GPIOs */
  588. imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
  589. imx6_pcie->gpio_active_high = of_property_read_bool(node,
  590. "reset-gpio-active-high");
  591. if (gpio_is_valid(imx6_pcie->reset_gpio)) {
  592. ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
  593. imx6_pcie->gpio_active_high ?
  594. GPIOF_OUT_INIT_HIGH :
  595. GPIOF_OUT_INIT_LOW,
  596. "PCIe reset");
  597. if (ret) {
  598. dev_err(dev, "unable to get reset gpio\n");
  599. return ret;
  600. }
  601. } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
  602. return imx6_pcie->reset_gpio;
  603. }
  604. /* Fetch clocks */
  605. imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
  606. if (IS_ERR(imx6_pcie->pcie_phy)) {
  607. dev_err(dev, "pcie_phy clock source missing or invalid\n");
  608. return PTR_ERR(imx6_pcie->pcie_phy);
  609. }
  610. imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
  611. if (IS_ERR(imx6_pcie->pcie_bus)) {
  612. dev_err(dev, "pcie_bus clock source missing or invalid\n");
  613. return PTR_ERR(imx6_pcie->pcie_bus);
  614. }
  615. imx6_pcie->pcie = devm_clk_get(dev, "pcie");
  616. if (IS_ERR(imx6_pcie->pcie)) {
  617. dev_err(dev, "pcie clock source missing or invalid\n");
  618. return PTR_ERR(imx6_pcie->pcie);
  619. }
  620. switch (imx6_pcie->variant) {
  621. case IMX6SX:
  622. imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
  623. "pcie_inbound_axi");
  624. if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
  625. dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
  626. return PTR_ERR(imx6_pcie->pcie_inbound_axi);
  627. }
  628. break;
  629. case IMX7D:
  630. imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
  631. "pciephy");
  632. if (IS_ERR(imx6_pcie->pciephy_reset)) {
  633. dev_err(dev, "Failed to get PCIEPHY reset control\n");
  634. return PTR_ERR(imx6_pcie->pciephy_reset);
  635. }
  636. imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
  637. "apps");
  638. if (IS_ERR(imx6_pcie->apps_reset)) {
  639. dev_err(dev, "Failed to get PCIE APPS reset control\n");
  640. return PTR_ERR(imx6_pcie->apps_reset);
  641. }
  642. break;
  643. default:
  644. break;
  645. }
  646. /* Grab GPR config register range */
  647. imx6_pcie->iomuxc_gpr =
  648. syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
  649. if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
  650. dev_err(dev, "unable to find iomuxc registers\n");
  651. return PTR_ERR(imx6_pcie->iomuxc_gpr);
  652. }
  653. /* Grab PCIe PHY Tx Settings */
  654. if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
  655. &imx6_pcie->tx_deemph_gen1))
  656. imx6_pcie->tx_deemph_gen1 = 0;
  657. if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
  658. &imx6_pcie->tx_deemph_gen2_3p5db))
  659. imx6_pcie->tx_deemph_gen2_3p5db = 0;
  660. if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
  661. &imx6_pcie->tx_deemph_gen2_6db))
  662. imx6_pcie->tx_deemph_gen2_6db = 20;
  663. if (of_property_read_u32(node, "fsl,tx-swing-full",
  664. &imx6_pcie->tx_swing_full))
  665. imx6_pcie->tx_swing_full = 127;
  666. if (of_property_read_u32(node, "fsl,tx-swing-low",
  667. &imx6_pcie->tx_swing_low))
  668. imx6_pcie->tx_swing_low = 127;
  669. /* Limit link speed */
  670. ret = of_property_read_u32(node, "fsl,max-link-speed",
  671. &imx6_pcie->link_gen);
  672. if (ret)
  673. imx6_pcie->link_gen = 1;
  674. imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
  675. if (IS_ERR(imx6_pcie->vpcie)) {
  676. if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER)
  677. return -EPROBE_DEFER;
  678. imx6_pcie->vpcie = NULL;
  679. }
  680. platform_set_drvdata(pdev, imx6_pcie);
  681. ret = imx6_add_pcie_port(imx6_pcie, pdev);
  682. if (ret < 0)
  683. return ret;
  684. return 0;
  685. }
  686. static void imx6_pcie_shutdown(struct platform_device *pdev)
  687. {
  688. struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
  689. /* bring down link, so bootloader gets clean state in case of reboot */
  690. imx6_pcie_assert_core_reset(imx6_pcie);
  691. }
  692. static const struct of_device_id imx6_pcie_of_match[] = {
  693. { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
  694. { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
  695. { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
  696. { .compatible = "fsl,imx7d-pcie", .data = (void *)IMX7D, },
  697. {},
  698. };
  699. static struct platform_driver imx6_pcie_driver = {
  700. .driver = {
  701. .name = "imx6q-pcie",
  702. .of_match_table = imx6_pcie_of_match,
  703. .suppress_bind_attrs = true,
  704. },
  705. .probe = imx6_pcie_probe,
  706. .shutdown = imx6_pcie_shutdown,
  707. };
  708. static int __init imx6_pcie_init(void)
  709. {
  710. /*
  711. * Since probe() can be deferred we need to make sure that
  712. * hook_fault_code is not called after __init memory is freed
  713. * by kernel and since imx6q_pcie_abort_handler() is a no-op,
  714. * we can install the handler here without risking it
  715. * accessing some uninitialized driver state.
  716. */
  717. hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
  718. "external abort on non-linefetch");
  719. return platform_driver_register(&imx6_pcie_driver);
  720. }
  721. device_initcall(imx6_pcie_init);