pci-imx6.c 19 KB

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