pci-imx6.c 22 KB

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