pci-imx6.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/resource.h>
  25. #include <linux/signal.h>
  26. #include <linux/types.h>
  27. #include <linux/interrupt.h>
  28. #include "pcie-designware.h"
  29. #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
  30. struct imx6_pcie {
  31. int reset_gpio;
  32. struct clk *pcie_bus;
  33. struct clk *pcie_phy;
  34. struct clk *pcie;
  35. struct pcie_port pp;
  36. struct regmap *iomuxc_gpr;
  37. void __iomem *mem_base;
  38. };
  39. /* PCIe Root Complex registers (memory-mapped) */
  40. #define PCIE_RC_LCR 0x7c
  41. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
  42. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
  43. #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
  44. #define PCIE_RC_LCSR 0x80
  45. /* PCIe Port Logic registers (memory-mapped) */
  46. #define PL_OFFSET 0x700
  47. #define PCIE_PL_PFLR (PL_OFFSET + 0x08)
  48. #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
  49. #define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
  50. #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
  51. #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
  52. #define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
  53. #define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
  54. #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
  55. #define PCIE_PHY_CTRL_DATA_LOC 0
  56. #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
  57. #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
  58. #define PCIE_PHY_CTRL_WR_LOC 18
  59. #define PCIE_PHY_CTRL_RD_LOC 19
  60. #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
  61. #define PCIE_PHY_STAT_ACK_LOC 16
  62. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  63. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  64. /* PHY registers (not memory-mapped) */
  65. #define PCIE_PHY_RX_ASIC_OUT 0x100D
  66. #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
  67. #define PHY_RX_OVRD_IN_LO 0x1005
  68. #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
  69. #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
  70. static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
  71. {
  72. u32 val;
  73. u32 max_iterations = 10;
  74. u32 wait_counter = 0;
  75. do {
  76. val = readl(dbi_base + PCIE_PHY_STAT);
  77. val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
  78. wait_counter++;
  79. if (val == exp_val)
  80. return 0;
  81. udelay(1);
  82. } while (wait_counter < max_iterations);
  83. return -ETIMEDOUT;
  84. }
  85. static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
  86. {
  87. u32 val;
  88. int ret;
  89. val = addr << PCIE_PHY_CTRL_DATA_LOC;
  90. writel(val, dbi_base + PCIE_PHY_CTRL);
  91. val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
  92. writel(val, dbi_base + PCIE_PHY_CTRL);
  93. ret = pcie_phy_poll_ack(dbi_base, 1);
  94. if (ret)
  95. return ret;
  96. val = addr << PCIE_PHY_CTRL_DATA_LOC;
  97. writel(val, dbi_base + PCIE_PHY_CTRL);
  98. return pcie_phy_poll_ack(dbi_base, 0);
  99. }
  100. /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
  101. static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
  102. {
  103. u32 val, phy_ctl;
  104. int ret;
  105. ret = pcie_phy_wait_ack(dbi_base, addr);
  106. if (ret)
  107. return ret;
  108. /* assert Read signal */
  109. phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
  110. writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
  111. ret = pcie_phy_poll_ack(dbi_base, 1);
  112. if (ret)
  113. return ret;
  114. val = readl(dbi_base + PCIE_PHY_STAT);
  115. *data = val & 0xffff;
  116. /* deassert Read signal */
  117. writel(0x00, dbi_base + PCIE_PHY_CTRL);
  118. return pcie_phy_poll_ack(dbi_base, 0);
  119. }
  120. static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
  121. {
  122. u32 var;
  123. int ret;
  124. /* write addr */
  125. /* cap addr */
  126. ret = pcie_phy_wait_ack(dbi_base, addr);
  127. if (ret)
  128. return ret;
  129. var = data << PCIE_PHY_CTRL_DATA_LOC;
  130. writel(var, dbi_base + PCIE_PHY_CTRL);
  131. /* capture data */
  132. var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
  133. writel(var, dbi_base + PCIE_PHY_CTRL);
  134. ret = pcie_phy_poll_ack(dbi_base, 1);
  135. if (ret)
  136. return ret;
  137. /* deassert cap data */
  138. var = data << PCIE_PHY_CTRL_DATA_LOC;
  139. writel(var, dbi_base + PCIE_PHY_CTRL);
  140. /* wait for ack de-assertion */
  141. ret = pcie_phy_poll_ack(dbi_base, 0);
  142. if (ret)
  143. return ret;
  144. /* assert wr signal */
  145. var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
  146. writel(var, dbi_base + PCIE_PHY_CTRL);
  147. /* wait for ack */
  148. ret = pcie_phy_poll_ack(dbi_base, 1);
  149. if (ret)
  150. return ret;
  151. /* deassert wr signal */
  152. var = data << PCIE_PHY_CTRL_DATA_LOC;
  153. writel(var, dbi_base + PCIE_PHY_CTRL);
  154. /* wait for ack de-assertion */
  155. ret = pcie_phy_poll_ack(dbi_base, 0);
  156. if (ret)
  157. return ret;
  158. writel(0x0, dbi_base + PCIE_PHY_CTRL);
  159. return 0;
  160. }
  161. /* Added for PCI abort handling */
  162. static int imx6q_pcie_abort_handler(unsigned long addr,
  163. unsigned int fsr, struct pt_regs *regs)
  164. {
  165. return 0;
  166. }
  167. static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
  168. {
  169. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  170. u32 val, gpr1, gpr12;
  171. /*
  172. * If the bootloader already enabled the link we need some special
  173. * handling to get the core back into a state where it is safe to
  174. * touch it for configuration. As there is no dedicated reset signal
  175. * wired up for MX6QDL, we need to manually force LTSSM into "detect"
  176. * state before completely disabling LTSSM, which is a prerequisite
  177. * for core configuration.
  178. *
  179. * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
  180. * indication that the bootloader activated the link.
  181. */
  182. regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
  183. regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
  184. if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
  185. (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
  186. val = readl(pp->dbi_base + PCIE_PL_PFLR);
  187. val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
  188. val |= PCIE_PL_PFLR_FORCE_LINK;
  189. writel(val, pp->dbi_base + PCIE_PL_PFLR);
  190. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  191. IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
  192. }
  193. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  194. IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
  195. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  196. IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
  197. return 0;
  198. }
  199. static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
  200. {
  201. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  202. int ret;
  203. ret = clk_prepare_enable(imx6_pcie->pcie_phy);
  204. if (ret) {
  205. dev_err(pp->dev, "unable to enable pcie_phy clock\n");
  206. goto err_pcie_phy;
  207. }
  208. ret = clk_prepare_enable(imx6_pcie->pcie_bus);
  209. if (ret) {
  210. dev_err(pp->dev, "unable to enable pcie_bus clock\n");
  211. goto err_pcie_bus;
  212. }
  213. ret = clk_prepare_enable(imx6_pcie->pcie);
  214. if (ret) {
  215. dev_err(pp->dev, "unable to enable pcie clock\n");
  216. goto err_pcie;
  217. }
  218. /* power up core phy and enable ref clock */
  219. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  220. IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
  221. /*
  222. * the async reset input need ref clock to sync internally,
  223. * when the ref clock comes after reset, internal synced
  224. * reset time is too short, cannot meet the requirement.
  225. * add one ~10us delay here.
  226. */
  227. udelay(10);
  228. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
  229. IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
  230. /* allow the clocks to stabilize */
  231. usleep_range(200, 500);
  232. /* Some boards don't have PCIe reset GPIO. */
  233. if (gpio_is_valid(imx6_pcie->reset_gpio)) {
  234. gpio_set_value(imx6_pcie->reset_gpio, 0);
  235. msleep(100);
  236. gpio_set_value(imx6_pcie->reset_gpio, 1);
  237. }
  238. return 0;
  239. err_pcie:
  240. clk_disable_unprepare(imx6_pcie->pcie_bus);
  241. err_pcie_bus:
  242. clk_disable_unprepare(imx6_pcie->pcie_phy);
  243. err_pcie_phy:
  244. return ret;
  245. }
  246. static void imx6_pcie_init_phy(struct pcie_port *pp)
  247. {
  248. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  249. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  250. IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
  251. /* configure constant input signal to the pcie ctrl and phy */
  252. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  253. IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
  254. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  255. IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
  256. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  257. IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
  258. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  259. IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
  260. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  261. IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
  262. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  263. IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
  264. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
  265. IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
  266. }
  267. static int imx6_pcie_wait_for_link(struct pcie_port *pp)
  268. {
  269. unsigned int retries;
  270. for (retries = 0; retries < 200; retries++) {
  271. if (dw_pcie_link_up(pp))
  272. return 0;
  273. usleep_range(100, 1000);
  274. }
  275. dev_err(pp->dev, "phy link never came up\n");
  276. dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
  277. readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
  278. readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
  279. return -EINVAL;
  280. }
  281. static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
  282. {
  283. u32 tmp;
  284. unsigned int retries;
  285. for (retries = 0; retries < 200; retries++) {
  286. tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  287. /* Test if the speed change finished. */
  288. if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
  289. return 0;
  290. usleep_range(100, 1000);
  291. }
  292. dev_err(pp->dev, "Speed change timeout\n");
  293. return -EINVAL;
  294. }
  295. static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
  296. {
  297. struct pcie_port *pp = arg;
  298. return dw_handle_msi_irq(pp);
  299. }
  300. static int imx6_pcie_establish_link(struct pcie_port *pp)
  301. {
  302. struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
  303. u32 tmp;
  304. int ret;
  305. /*
  306. * Force Gen1 operation when starting the link. In case the link is
  307. * started in Gen2 mode, there is a possibility the devices on the
  308. * bus will not be detected at all. This happens with PCIe switches.
  309. */
  310. tmp = readl(pp->dbi_base + PCIE_RC_LCR);
  311. tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
  312. tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
  313. writel(tmp, pp->dbi_base + PCIE_RC_LCR);
  314. /* Start LTSSM. */
  315. regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
  316. IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
  317. ret = imx6_pcie_wait_for_link(pp);
  318. if (ret)
  319. return ret;
  320. /* Allow Gen2 mode after the link is up. */
  321. tmp = readl(pp->dbi_base + PCIE_RC_LCR);
  322. tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
  323. tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
  324. writel(tmp, pp->dbi_base + PCIE_RC_LCR);
  325. /*
  326. * Start Directed Speed Change so the best possible speed both link
  327. * partners support can be negotiated.
  328. */
  329. tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  330. tmp |= PORT_LOGIC_SPEED_CHANGE;
  331. writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  332. ret = imx6_pcie_wait_for_speed_change(pp);
  333. if (ret) {
  334. dev_err(pp->dev, "Failed to bring link up!\n");
  335. return ret;
  336. }
  337. /* Make sure link training is finished as well! */
  338. ret = imx6_pcie_wait_for_link(pp);
  339. if (ret) {
  340. dev_err(pp->dev, "Failed to bring link up!\n");
  341. return ret;
  342. }
  343. tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
  344. dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
  345. return 0;
  346. }
  347. static void imx6_pcie_host_init(struct pcie_port *pp)
  348. {
  349. imx6_pcie_assert_core_reset(pp);
  350. imx6_pcie_init_phy(pp);
  351. imx6_pcie_deassert_core_reset(pp);
  352. dw_pcie_setup_rc(pp);
  353. imx6_pcie_establish_link(pp);
  354. if (IS_ENABLED(CONFIG_PCI_MSI))
  355. dw_pcie_msi_init(pp);
  356. }
  357. static void imx6_pcie_reset_phy(struct pcie_port *pp)
  358. {
  359. u32 tmp;
  360. pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
  361. tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
  362. PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  363. pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
  364. usleep_range(2000, 3000);
  365. pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
  366. tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
  367. PHY_RX_OVRD_IN_LO_RX_PLL_EN);
  368. pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
  369. }
  370. static int imx6_pcie_link_up(struct pcie_port *pp)
  371. {
  372. u32 rc, debug_r0, rx_valid;
  373. int count = 5;
  374. /*
  375. * Test if the PHY reports that the link is up and also that the LTSSM
  376. * training finished. There are three possible states of the link when
  377. * this code is called:
  378. * 1) The link is DOWN (unlikely)
  379. * The link didn't come up yet for some reason. This usually means
  380. * we have a real problem somewhere. Reset the PHY and exit. This
  381. * state calls for inspection of the DEBUG registers.
  382. * 2) The link is UP, but still in LTSSM training
  383. * Wait for the training to finish, which should take a very short
  384. * time. If the training does not finish, we have a problem and we
  385. * need to inspect the DEBUG registers. If the training does finish,
  386. * the link is up and operating correctly.
  387. * 3) The link is UP and no longer in LTSSM training
  388. * The link is up and operating correctly.
  389. */
  390. while (1) {
  391. rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
  392. if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP))
  393. break;
  394. if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING))
  395. return 1;
  396. if (!count--)
  397. break;
  398. dev_dbg(pp->dev, "Link is up, but still in training\n");
  399. /*
  400. * Wait a little bit, then re-check if the link finished
  401. * the training.
  402. */
  403. usleep_range(1000, 2000);
  404. }
  405. /*
  406. * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
  407. * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
  408. * If (MAC/LTSSM.state == Recovery.RcvrLock)
  409. * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
  410. * to gen2 is stuck
  411. */
  412. pcie_phy_read(pp->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
  413. debug_r0 = readl(pp->dbi_base + PCIE_PHY_DEBUG_R0);
  414. if (rx_valid & PCIE_PHY_RX_ASIC_OUT_VALID)
  415. return 0;
  416. if ((debug_r0 & 0x3f) != 0x0d)
  417. return 0;
  418. dev_err(pp->dev, "transition to gen2 is stuck, reset PHY!\n");
  419. dev_dbg(pp->dev, "debug_r0=%08x debug_r1=%08x\n", debug_r0, rc);
  420. imx6_pcie_reset_phy(pp);
  421. return 0;
  422. }
  423. static struct pcie_host_ops imx6_pcie_host_ops = {
  424. .link_up = imx6_pcie_link_up,
  425. .host_init = imx6_pcie_host_init,
  426. };
  427. static int __init imx6_add_pcie_port(struct pcie_port *pp,
  428. struct platform_device *pdev)
  429. {
  430. int ret;
  431. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  432. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  433. if (pp->msi_irq <= 0) {
  434. dev_err(&pdev->dev, "failed to get MSI irq\n");
  435. return -ENODEV;
  436. }
  437. ret = devm_request_irq(&pdev->dev, pp->msi_irq,
  438. imx6_pcie_msi_handler,
  439. IRQF_SHARED | IRQF_NO_THREAD,
  440. "mx6-pcie-msi", pp);
  441. if (ret) {
  442. dev_err(&pdev->dev, "failed to request MSI irq\n");
  443. return ret;
  444. }
  445. }
  446. pp->root_bus_nr = -1;
  447. pp->ops = &imx6_pcie_host_ops;
  448. ret = dw_pcie_host_init(pp);
  449. if (ret) {
  450. dev_err(&pdev->dev, "failed to initialize host\n");
  451. return ret;
  452. }
  453. return 0;
  454. }
  455. static int __init imx6_pcie_probe(struct platform_device *pdev)
  456. {
  457. struct imx6_pcie *imx6_pcie;
  458. struct pcie_port *pp;
  459. struct device_node *np = pdev->dev.of_node;
  460. struct resource *dbi_base;
  461. int ret;
  462. imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
  463. if (!imx6_pcie)
  464. return -ENOMEM;
  465. pp = &imx6_pcie->pp;
  466. pp->dev = &pdev->dev;
  467. /* Added for PCI abort handling */
  468. hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
  469. "imprecise external abort");
  470. dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  471. pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
  472. if (IS_ERR(pp->dbi_base))
  473. return PTR_ERR(pp->dbi_base);
  474. /* Fetch GPIOs */
  475. imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
  476. if (gpio_is_valid(imx6_pcie->reset_gpio)) {
  477. ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
  478. GPIOF_OUT_INIT_LOW, "PCIe reset");
  479. if (ret) {
  480. dev_err(&pdev->dev, "unable to get reset gpio\n");
  481. return ret;
  482. }
  483. }
  484. /* Fetch clocks */
  485. imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
  486. if (IS_ERR(imx6_pcie->pcie_phy)) {
  487. dev_err(&pdev->dev,
  488. "pcie_phy clock source missing or invalid\n");
  489. return PTR_ERR(imx6_pcie->pcie_phy);
  490. }
  491. imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
  492. if (IS_ERR(imx6_pcie->pcie_bus)) {
  493. dev_err(&pdev->dev,
  494. "pcie_bus clock source missing or invalid\n");
  495. return PTR_ERR(imx6_pcie->pcie_bus);
  496. }
  497. imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
  498. if (IS_ERR(imx6_pcie->pcie)) {
  499. dev_err(&pdev->dev,
  500. "pcie clock source missing or invalid\n");
  501. return PTR_ERR(imx6_pcie->pcie);
  502. }
  503. /* Grab GPR config register range */
  504. imx6_pcie->iomuxc_gpr =
  505. syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
  506. if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
  507. dev_err(&pdev->dev, "unable to find iomuxc registers\n");
  508. return PTR_ERR(imx6_pcie->iomuxc_gpr);
  509. }
  510. ret = imx6_add_pcie_port(pp, pdev);
  511. if (ret < 0)
  512. return ret;
  513. platform_set_drvdata(pdev, imx6_pcie);
  514. return 0;
  515. }
  516. static void imx6_pcie_shutdown(struct platform_device *pdev)
  517. {
  518. struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
  519. /* bring down link, so bootloader gets clean state in case of reboot */
  520. imx6_pcie_assert_core_reset(&imx6_pcie->pp);
  521. }
  522. static const struct of_device_id imx6_pcie_of_match[] = {
  523. { .compatible = "fsl,imx6q-pcie", },
  524. {},
  525. };
  526. MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
  527. static struct platform_driver imx6_pcie_driver = {
  528. .driver = {
  529. .name = "imx6q-pcie",
  530. .of_match_table = imx6_pcie_of_match,
  531. },
  532. .shutdown = imx6_pcie_shutdown,
  533. };
  534. /* Freescale PCIe driver does not allow module unload */
  535. static int __init imx6_pcie_init(void)
  536. {
  537. return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
  538. }
  539. module_init(imx6_pcie_init);
  540. MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
  541. MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
  542. MODULE_LICENSE("GPL v2");