pci-imx6.c 22 KB

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