Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 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 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/gpio.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/mfd/syscon.h> |
| 19 | #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of_gpio.h> |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 22 | #include <linux/of_device.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 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> |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 30 | |
| 31 | #include "pcie-designware.h" |
| 32 | |
| 33 | #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp) |
| 34 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 35 | enum imx6_pcie_variants { |
| 36 | IMX6Q, |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 37 | IMX6SX, |
| 38 | IMX6QP, |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 39 | }; |
| 40 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 41 | struct imx6_pcie { |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 42 | int reset_gpio; |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 43 | bool gpio_active_high; |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 44 | struct clk *pcie_bus; |
| 45 | struct clk *pcie_phy; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 46 | struct clk *pcie_inbound_axi; |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 47 | struct clk *pcie; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 48 | struct pcie_port pp; |
| 49 | struct regmap *iomuxc_gpr; |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 50 | enum imx6_pcie_variants variant; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 51 | void __iomem *mem_base; |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 52 | u32 tx_deemph_gen1; |
| 53 | u32 tx_deemph_gen2_3p5db; |
| 54 | u32 tx_deemph_gen2_6db; |
| 55 | u32 tx_swing_full; |
| 56 | u32 tx_swing_low; |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 57 | int link_gen; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 58 | }; |
| 59 | |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 60 | /* PCIe Root Complex registers (memory-mapped) */ |
| 61 | #define PCIE_RC_LCR 0x7c |
| 62 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1 |
| 63 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2 |
| 64 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf |
| 65 | |
Bjorn Helgaas | 2393f79 | 2015-06-12 17:27:43 -0500 | [diff] [blame] | 66 | #define PCIE_RC_LCSR 0x80 |
| 67 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 68 | /* PCIe Port Logic registers (memory-mapped) */ |
| 69 | #define PL_OFFSET 0x700 |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 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) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 73 | #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28) |
| 74 | #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c) |
Marek Vasut | 7f9f40c | 2013-12-12 22:49:59 +0100 | [diff] [blame] | 75 | #define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29) |
| 76 | #define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 77 | |
| 78 | #define PCIE_PHY_CTRL (PL_OFFSET + 0x114) |
| 79 | #define PCIE_PHY_CTRL_DATA_LOC 0 |
| 80 | #define PCIE_PHY_CTRL_CAP_ADR_LOC 16 |
| 81 | #define PCIE_PHY_CTRL_CAP_DAT_LOC 17 |
| 82 | #define PCIE_PHY_CTRL_WR_LOC 18 |
| 83 | #define PCIE_PHY_CTRL_RD_LOC 19 |
| 84 | |
| 85 | #define PCIE_PHY_STAT (PL_OFFSET + 0x110) |
| 86 | #define PCIE_PHY_STAT_ACK_LOC 16 |
| 87 | |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 88 | #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C |
| 89 | #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17) |
| 90 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 91 | /* PHY registers (not memory-mapped) */ |
| 92 | #define PCIE_PHY_RX_ASIC_OUT 0x100D |
Fabio Estevam | 111feb7 | 2015-09-11 09:08:53 -0300 | [diff] [blame] | 93 | #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 94 | |
| 95 | #define PHY_RX_OVRD_IN_LO 0x1005 |
| 96 | #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5) |
| 97 | #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3) |
| 98 | |
| 99 | static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val) |
| 100 | { |
| 101 | u32 val; |
| 102 | u32 max_iterations = 10; |
| 103 | u32 wait_counter = 0; |
| 104 | |
| 105 | do { |
| 106 | val = readl(dbi_base + PCIE_PHY_STAT); |
| 107 | val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1; |
| 108 | wait_counter++; |
| 109 | |
| 110 | if (val == exp_val) |
| 111 | return 0; |
| 112 | |
| 113 | udelay(1); |
| 114 | } while (wait_counter < max_iterations); |
| 115 | |
| 116 | return -ETIMEDOUT; |
| 117 | } |
| 118 | |
| 119 | static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr) |
| 120 | { |
| 121 | u32 val; |
| 122 | int ret; |
| 123 | |
| 124 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
| 125 | writel(val, dbi_base + PCIE_PHY_CTRL); |
| 126 | |
| 127 | val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC); |
| 128 | writel(val, dbi_base + PCIE_PHY_CTRL); |
| 129 | |
| 130 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
| 135 | writel(val, dbi_base + PCIE_PHY_CTRL); |
| 136 | |
Fabio Estevam | 8d1ceb5 | 2015-08-20 01:31:58 -0500 | [diff] [blame] | 137 | return pcie_phy_poll_ack(dbi_base, 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */ |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 141 | static int pcie_phy_read(void __iomem *dbi_base, int addr, int *data) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 142 | { |
| 143 | u32 val, phy_ctl; |
| 144 | int ret; |
| 145 | |
| 146 | ret = pcie_phy_wait_ack(dbi_base, addr); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | /* assert Read signal */ |
| 151 | phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC; |
| 152 | writel(phy_ctl, dbi_base + PCIE_PHY_CTRL); |
| 153 | |
| 154 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | |
| 158 | val = readl(dbi_base + PCIE_PHY_STAT); |
| 159 | *data = val & 0xffff; |
| 160 | |
| 161 | /* deassert Read signal */ |
| 162 | writel(0x00, dbi_base + PCIE_PHY_CTRL); |
| 163 | |
Fabio Estevam | 8d1ceb5 | 2015-08-20 01:31:58 -0500 | [diff] [blame] | 164 | return pcie_phy_poll_ack(dbi_base, 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int pcie_phy_write(void __iomem *dbi_base, int addr, int data) |
| 168 | { |
| 169 | u32 var; |
| 170 | int ret; |
| 171 | |
| 172 | /* write addr */ |
| 173 | /* cap addr */ |
| 174 | ret = pcie_phy_wait_ack(dbi_base, addr); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
| 178 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 179 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 180 | |
| 181 | /* capture data */ |
| 182 | var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC); |
| 183 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 184 | |
| 185 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | /* deassert cap data */ |
| 190 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 191 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 192 | |
| 193 | /* wait for ack de-assertion */ |
| 194 | ret = pcie_phy_poll_ack(dbi_base, 0); |
| 195 | if (ret) |
| 196 | return ret; |
| 197 | |
| 198 | /* assert wr signal */ |
| 199 | var = 0x1 << PCIE_PHY_CTRL_WR_LOC; |
| 200 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 201 | |
| 202 | /* wait for ack */ |
| 203 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | /* deassert wr signal */ |
| 208 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 209 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 210 | |
| 211 | /* wait for ack de-assertion */ |
| 212 | ret = pcie_phy_poll_ack(dbi_base, 0); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
| 216 | writel(0x0, dbi_base + PCIE_PHY_CTRL); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Lucas Stach | 53eeb48 | 2016-01-15 19:56:47 +0100 | [diff] [blame] | 221 | static void imx6_pcie_reset_phy(struct pcie_port *pp) |
| 222 | { |
| 223 | u32 tmp; |
| 224 | |
| 225 | pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp); |
| 226 | tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | |
| 227 | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
| 228 | pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp); |
| 229 | |
| 230 | usleep_range(2000, 3000); |
| 231 | |
| 232 | pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp); |
| 233 | tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | |
| 234 | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
| 235 | pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp); |
| 236 | } |
| 237 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 238 | /* Added for PCI abort handling */ |
| 239 | static int imx6q_pcie_abort_handler(unsigned long addr, |
| 240 | unsigned int fsr, struct pt_regs *regs) |
| 241 | { |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int imx6_pcie_assert_core_reset(struct pcie_port *pp) |
| 246 | { |
| 247 | struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp); |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 248 | u32 val, gpr1, gpr12; |
| 249 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 250 | switch (imx6_pcie->variant) { |
| 251 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 252 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 253 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN, |
| 254 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN); |
| 255 | /* Force PCIe PHY reset */ |
| 256 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5, |
| 257 | IMX6SX_GPR5_PCIE_BTNRST_RESET, |
| 258 | IMX6SX_GPR5_PCIE_BTNRST_RESET); |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 259 | break; |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 260 | case IMX6QP: |
| 261 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 262 | IMX6Q_GPR1_PCIE_SW_RST, |
| 263 | IMX6Q_GPR1_PCIE_SW_RST); |
| 264 | break; |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 265 | case IMX6Q: |
| 266 | /* |
| 267 | * If the bootloader already enabled the link we need some |
| 268 | * special handling to get the core back into a state where |
| 269 | * it is safe to touch it for configuration. As there is |
| 270 | * no dedicated reset signal wired up for MX6QDL, we need |
| 271 | * to manually force LTSSM into "detect" state before |
| 272 | * completely disabling LTSSM, which is a prerequisite for |
| 273 | * core configuration. |
| 274 | * |
| 275 | * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we |
| 276 | * have a strong indication that the bootloader activated |
| 277 | * the link. |
| 278 | */ |
| 279 | regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1); |
| 280 | regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12); |
| 281 | |
| 282 | if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) && |
| 283 | (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) { |
| 284 | val = readl(pp->dbi_base + PCIE_PL_PFLR); |
| 285 | val &= ~PCIE_PL_PFLR_LINK_STATE_MASK; |
| 286 | val |= PCIE_PL_PFLR_FORCE_LINK; |
| 287 | writel(val, pp->dbi_base + PCIE_PL_PFLR); |
| 288 | |
| 289 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 290 | IMX6Q_GPR12_PCIE_CTL_2, 0 << 10); |
| 291 | } |
| 292 | |
| 293 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 294 | IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18); |
| 295 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 296 | IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16); |
| 297 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 303 | static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie) |
| 304 | { |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 305 | struct pcie_port *pp = &imx6_pcie->pp; |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 306 | struct device *dev = pp->dev; |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 307 | int ret = 0; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 308 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 309 | switch (imx6_pcie->variant) { |
| 310 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 311 | ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi); |
| 312 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 313 | dev_err(dev, "unable to enable pcie_axi clock\n"); |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 314 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 318 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0); |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 319 | break; |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 320 | case IMX6QP: /* FALLTHROUGH */ |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 321 | case IMX6Q: |
| 322 | /* power up core phy and enable ref clock */ |
| 323 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 324 | IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18); |
| 325 | /* |
| 326 | * the async reset input need ref clock to sync internally, |
| 327 | * when the ref clock comes after reset, internal synced |
| 328 | * reset time is too short, cannot meet the requirement. |
| 329 | * add one ~10us delay here. |
| 330 | */ |
| 331 | udelay(10); |
| 332 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 333 | IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16); |
| 334 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 335 | } |
| 336 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 337 | return ret; |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 340 | static int imx6_pcie_deassert_core_reset(struct pcie_port *pp) |
| 341 | { |
| 342 | struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp); |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 343 | struct device *dev = pp->dev; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 344 | int ret; |
| 345 | |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 346 | ret = clk_prepare_enable(imx6_pcie->pcie_phy); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 347 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 348 | dev_err(dev, "unable to enable pcie_phy clock\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 349 | goto err_pcie_phy; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 350 | } |
| 351 | |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 352 | ret = clk_prepare_enable(imx6_pcie->pcie_bus); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 353 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 354 | dev_err(dev, "unable to enable pcie_bus clock\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 355 | goto err_pcie_bus; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 356 | } |
| 357 | |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 358 | ret = clk_prepare_enable(imx6_pcie->pcie); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 359 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 360 | dev_err(dev, "unable to enable pcie clock\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 361 | goto err_pcie; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 362 | } |
| 363 | |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 364 | ret = imx6_pcie_enable_ref_clk(imx6_pcie); |
| 365 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 366 | dev_err(dev, "unable to enable pcie ref clock\n"); |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 367 | goto err_ref_clk; |
| 368 | } |
Tim Harvey | 3fce0e8 | 2014-08-07 23:36:40 -0700 | [diff] [blame] | 369 | |
Richard Zhu | a2fa6f6 | 2014-10-27 13:17:32 +0800 | [diff] [blame] | 370 | /* allow the clocks to stabilize */ |
| 371 | usleep_range(200, 500); |
| 372 | |
Richard Zhu | bc9ef77 | 2013-12-12 22:50:03 +0100 | [diff] [blame] | 373 | /* Some boards don't have PCIe reset GPIO. */ |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 374 | if (gpio_is_valid(imx6_pcie->reset_gpio)) { |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 375 | gpio_set_value_cansleep(imx6_pcie->reset_gpio, |
| 376 | imx6_pcie->gpio_active_high); |
Richard Zhu | bc9ef77 | 2013-12-12 22:50:03 +0100 | [diff] [blame] | 377 | msleep(100); |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 378 | gpio_set_value_cansleep(imx6_pcie->reset_gpio, |
| 379 | !imx6_pcie->gpio_active_high); |
Richard Zhu | bc9ef77 | 2013-12-12 22:50:03 +0100 | [diff] [blame] | 380 | } |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 381 | |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 382 | switch (imx6_pcie->variant) { |
| 383 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 384 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5, |
| 385 | IMX6SX_GPR5_PCIE_BTNRST_RESET, 0); |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 386 | break; |
| 387 | case IMX6QP: |
| 388 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 389 | IMX6Q_GPR1_PCIE_SW_RST, 0); |
| 390 | |
| 391 | usleep_range(200, 500); |
| 392 | break; |
| 393 | case IMX6Q: /* Nothing to do */ |
| 394 | break; |
| 395 | } |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 396 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 397 | return 0; |
| 398 | |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 399 | err_ref_clk: |
| 400 | clk_disable_unprepare(imx6_pcie->pcie); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 401 | err_pcie: |
| 402 | clk_disable_unprepare(imx6_pcie->pcie_bus); |
| 403 | err_pcie_bus: |
| 404 | clk_disable_unprepare(imx6_pcie->pcie_phy); |
| 405 | err_pcie_phy: |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 406 | return ret; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static void imx6_pcie_init_phy(struct pcie_port *pp) |
| 410 | { |
| 411 | struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp); |
| 412 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 413 | if (imx6_pcie->variant == IMX6SX) |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 414 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 415 | IMX6SX_GPR12_PCIE_RX_EQ_MASK, |
| 416 | IMX6SX_GPR12_PCIE_RX_EQ_2); |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 417 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 418 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 419 | IMX6Q_GPR12_PCIE_CTL_2, 0 << 10); |
| 420 | |
| 421 | /* configure constant input signal to the pcie ctrl and phy */ |
| 422 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 423 | IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12); |
| 424 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 425 | IMX6Q_GPR12_LOS_LEVEL, 9 << 4); |
| 426 | |
| 427 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 428 | IMX6Q_GPR8_TX_DEEMPH_GEN1, |
| 429 | imx6_pcie->tx_deemph_gen1 << 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 430 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 431 | IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, |
| 432 | imx6_pcie->tx_deemph_gen2_3p5db << 6); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 433 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 434 | IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, |
| 435 | imx6_pcie->tx_deemph_gen2_6db << 12); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 436 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 437 | IMX6Q_GPR8_TX_SWING_FULL, |
| 438 | imx6_pcie->tx_swing_full << 18); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 439 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 440 | IMX6Q_GPR8_TX_SWING_LOW, |
| 441 | imx6_pcie->tx_swing_low << 25); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 442 | } |
| 443 | |
Marek Vasut | 66a60f9 | 2013-12-12 22:50:01 +0100 | [diff] [blame] | 444 | static int imx6_pcie_wait_for_link(struct pcie_port *pp) |
| 445 | { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 446 | struct device *dev = pp->dev; |
| 447 | |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 448 | /* check if the link is up or not */ |
| 449 | if (!dw_pcie_wait_for_link(pp)) |
| 450 | return 0; |
Marek Vasut | 66a60f9 | 2013-12-12 22:50:01 +0100 | [diff] [blame] | 451 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 452 | dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n", |
Bjorn Helgaas | 6cbb247 | 2015-06-02 16:47:17 -0500 | [diff] [blame] | 453 | readl(pp->dbi_base + PCIE_PHY_DEBUG_R0), |
| 454 | readl(pp->dbi_base + PCIE_PHY_DEBUG_R1)); |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 455 | return -ETIMEDOUT; |
Marek Vasut | 66a60f9 | 2013-12-12 22:50:01 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 458 | static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp) |
| 459 | { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 460 | struct device *dev = pp->dev; |
Bjorn Helgaas | 1c7fae1 | 2015-06-12 15:02:49 -0500 | [diff] [blame] | 461 | u32 tmp; |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 462 | unsigned int retries; |
| 463 | |
| 464 | for (retries = 0; retries < 200; retries++) { |
| 465 | tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 466 | /* Test if the speed change finished. */ |
| 467 | if (!(tmp & PORT_LOGIC_SPEED_CHANGE)) |
| 468 | return 0; |
| 469 | usleep_range(100, 1000); |
| 470 | } |
| 471 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 472 | dev_err(dev, "Speed change timeout\n"); |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 473 | return -EINVAL; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 474 | } |
| 475 | |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 476 | static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg) |
| 477 | { |
| 478 | struct pcie_port *pp = arg; |
| 479 | |
| 480 | return dw_handle_msi_irq(pp); |
| 481 | } |
| 482 | |
Bjorn Helgaas | fd5da20 | 2015-06-02 16:16:44 -0500 | [diff] [blame] | 483 | static int imx6_pcie_establish_link(struct pcie_port *pp) |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 484 | { |
| 485 | struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp); |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 486 | struct device *dev = pp->dev; |
Bjorn Helgaas | 1c7fae1 | 2015-06-12 15:02:49 -0500 | [diff] [blame] | 487 | u32 tmp; |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 488 | int ret; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Force Gen1 operation when starting the link. In case the link is |
| 492 | * started in Gen2 mode, there is a possibility the devices on the |
| 493 | * bus will not be detected at all. This happens with PCIe switches. |
| 494 | */ |
| 495 | tmp = readl(pp->dbi_base + PCIE_RC_LCR); |
| 496 | tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK; |
| 497 | tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1; |
| 498 | writel(tmp, pp->dbi_base + PCIE_RC_LCR); |
| 499 | |
| 500 | /* Start LTSSM. */ |
| 501 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 502 | IMX6Q_GPR12_PCIE_CTL_2, 1 << 10); |
| 503 | |
| 504 | ret = imx6_pcie_wait_for_link(pp); |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 505 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 506 | dev_info(dev, "Link never came up\n"); |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 507 | goto err_reset_phy; |
| 508 | } |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 509 | |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 510 | if (imx6_pcie->link_gen == 2) { |
| 511 | /* Allow Gen2 mode after the link is up. */ |
| 512 | tmp = readl(pp->dbi_base + PCIE_RC_LCR); |
| 513 | tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK; |
| 514 | tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2; |
| 515 | writel(tmp, pp->dbi_base + PCIE_RC_LCR); |
| 516 | } else { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 517 | dev_info(dev, "Link: Gen2 disabled\n"); |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 518 | } |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * Start Directed Speed Change so the best possible speed both link |
| 522 | * partners support can be negotiated. |
| 523 | */ |
| 524 | tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 525 | tmp |= PORT_LOGIC_SPEED_CHANGE; |
| 526 | writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 527 | |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 528 | ret = imx6_pcie_wait_for_speed_change(pp); |
| 529 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 530 | dev_err(dev, "Failed to bring link up!\n"); |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 531 | goto err_reset_phy; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* Make sure link training is finished as well! */ |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 535 | ret = imx6_pcie_wait_for_link(pp); |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 536 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 537 | dev_err(dev, "Failed to bring link up!\n"); |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 538 | goto err_reset_phy; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Bjorn Helgaas | 2393f79 | 2015-06-12 17:27:43 -0500 | [diff] [blame] | 541 | tmp = readl(pp->dbi_base + PCIE_RC_LCSR); |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 542 | dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf); |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 543 | return 0; |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 544 | |
| 545 | err_reset_phy: |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 546 | dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n", |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 547 | readl(pp->dbi_base + PCIE_PHY_DEBUG_R0), |
| 548 | readl(pp->dbi_base + PCIE_PHY_DEBUG_R1)); |
| 549 | imx6_pcie_reset_phy(pp); |
| 550 | |
| 551 | return ret; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 554 | static void imx6_pcie_host_init(struct pcie_port *pp) |
| 555 | { |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 556 | imx6_pcie_assert_core_reset(pp); |
| 557 | |
| 558 | imx6_pcie_init_phy(pp); |
| 559 | |
| 560 | imx6_pcie_deassert_core_reset(pp); |
| 561 | |
| 562 | dw_pcie_setup_rc(pp); |
| 563 | |
Bjorn Helgaas | fd5da20 | 2015-06-02 16:16:44 -0500 | [diff] [blame] | 564 | imx6_pcie_establish_link(pp); |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 565 | |
| 566 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 567 | dw_pcie_msi_init(pp); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int imx6_pcie_link_up(struct pcie_port *pp) |
| 571 | { |
Lucas Stach | 4d107d3 | 2016-01-25 16:50:02 -0600 | [diff] [blame] | 572 | return readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & |
| 573 | PCIE_PHY_DEBUG_R1_XMLH_LINK_UP; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static struct pcie_host_ops imx6_pcie_host_ops = { |
| 577 | .link_up = imx6_pcie_link_up, |
| 578 | .host_init = imx6_pcie_host_init, |
| 579 | }; |
| 580 | |
Sachin Kamat | 44cb5e9 | 2014-05-30 12:08:48 +0530 | [diff] [blame] | 581 | static int __init imx6_add_pcie_port(struct pcie_port *pp, |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 582 | struct platform_device *pdev) |
| 583 | { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 584 | struct device *dev = pp->dev; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 585 | int ret; |
| 586 | |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 587 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 588 | pp->msi_irq = platform_get_irq_byname(pdev, "msi"); |
| 589 | if (pp->msi_irq <= 0) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 590 | dev_err(dev, "failed to get MSI irq\n"); |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 591 | return -ENODEV; |
| 592 | } |
| 593 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 594 | ret = devm_request_irq(dev, pp->msi_irq, |
Jingoo Han | d88a7ef | 2014-11-12 12:25:09 +0900 | [diff] [blame] | 595 | imx6_pcie_msi_handler, |
Grygorii Strashko | 8ff0ef9 | 2015-12-10 21:18:20 +0200 | [diff] [blame] | 596 | IRQF_SHARED | IRQF_NO_THREAD, |
| 597 | "mx6-pcie-msi", pp); |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 598 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 599 | dev_err(dev, "failed to request MSI irq\n"); |
Fabio Estevam | 89b2d4f | 2015-09-11 09:08:52 -0300 | [diff] [blame] | 600 | return ret; |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 604 | pp->root_bus_nr = -1; |
| 605 | pp->ops = &imx6_pcie_host_ops; |
| 606 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 607 | ret = dw_pcie_host_init(pp); |
| 608 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 609 | dev_err(dev, "failed to initialize host\n"); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static int __init imx6_pcie_probe(struct platform_device *pdev) |
| 617 | { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 618 | struct device *dev = &pdev->dev; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 619 | struct imx6_pcie *imx6_pcie; |
| 620 | struct pcie_port *pp; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 621 | struct resource *dbi_base; |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 622 | struct device_node *node = dev->of_node; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 623 | int ret; |
| 624 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 625 | imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 626 | if (!imx6_pcie) |
| 627 | return -ENOMEM; |
| 628 | |
| 629 | pp = &imx6_pcie->pp; |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 630 | pp->dev = dev; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 631 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 632 | imx6_pcie->variant = |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 633 | (enum imx6_pcie_variants)of_device_get_match_data(dev); |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 634 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 635 | /* Added for PCI abort handling */ |
| 636 | hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0, |
| 637 | "imprecise external abort"); |
| 638 | |
| 639 | dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 640 | pp->dbi_base = devm_ioremap_resource(dev, dbi_base); |
Fabio Estevam | b391bf3 | 2013-12-02 01:39:35 -0200 | [diff] [blame] | 641 | if (IS_ERR(pp->dbi_base)) |
| 642 | return PTR_ERR(pp->dbi_base); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 643 | |
| 644 | /* Fetch GPIOs */ |
Bjorn Helgaas | c5af407 | 2016-10-06 13:35:18 -0500 | [diff] [blame^] | 645 | imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0); |
| 646 | imx6_pcie->gpio_active_high = of_property_read_bool(node, |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 647 | "reset-gpio-active-high"); |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 648 | if (gpio_is_valid(imx6_pcie->reset_gpio)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 649 | ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio, |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 650 | imx6_pcie->gpio_active_high ? |
| 651 | GPIOF_OUT_INIT_HIGH : |
| 652 | GPIOF_OUT_INIT_LOW, |
| 653 | "PCIe reset"); |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 654 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 655 | dev_err(dev, "unable to get reset gpio\n"); |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 656 | return ret; |
| 657 | } |
| 658 | } |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 659 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 660 | /* Fetch clocks */ |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 661 | imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 662 | if (IS_ERR(imx6_pcie->pcie_phy)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 663 | dev_err(dev, "pcie_phy clock source missing or invalid\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 664 | return PTR_ERR(imx6_pcie->pcie_phy); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 665 | } |
| 666 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 667 | imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 668 | if (IS_ERR(imx6_pcie->pcie_bus)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 669 | dev_err(dev, "pcie_bus clock source missing or invalid\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 670 | return PTR_ERR(imx6_pcie->pcie_bus); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 671 | } |
| 672 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 673 | imx6_pcie->pcie = devm_clk_get(dev, "pcie"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 674 | if (IS_ERR(imx6_pcie->pcie)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 675 | dev_err(dev, "pcie clock source missing or invalid\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 676 | return PTR_ERR(imx6_pcie->pcie); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 679 | if (imx6_pcie->variant == IMX6SX) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 680 | imx6_pcie->pcie_inbound_axi = devm_clk_get(dev, |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 681 | "pcie_inbound_axi"); |
| 682 | if (IS_ERR(imx6_pcie->pcie_inbound_axi)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 683 | dev_err(dev, |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 684 | "pcie_incbound_axi clock missing or invalid\n"); |
| 685 | return PTR_ERR(imx6_pcie->pcie_inbound_axi); |
| 686 | } |
| 687 | } |
| 688 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 689 | /* Grab GPR config register range */ |
| 690 | imx6_pcie->iomuxc_gpr = |
| 691 | syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr"); |
| 692 | if (IS_ERR(imx6_pcie->iomuxc_gpr)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 693 | dev_err(dev, "unable to find iomuxc registers\n"); |
Fabio Estevam | b391bf3 | 2013-12-02 01:39:35 -0200 | [diff] [blame] | 694 | return PTR_ERR(imx6_pcie->iomuxc_gpr); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 695 | } |
| 696 | |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 697 | /* Grab PCIe PHY Tx Settings */ |
| 698 | if (of_property_read_u32(node, "fsl,tx-deemph-gen1", |
| 699 | &imx6_pcie->tx_deemph_gen1)) |
| 700 | imx6_pcie->tx_deemph_gen1 = 0; |
| 701 | |
| 702 | if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db", |
| 703 | &imx6_pcie->tx_deemph_gen2_3p5db)) |
| 704 | imx6_pcie->tx_deemph_gen2_3p5db = 0; |
| 705 | |
| 706 | if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db", |
| 707 | &imx6_pcie->tx_deemph_gen2_6db)) |
| 708 | imx6_pcie->tx_deemph_gen2_6db = 20; |
| 709 | |
| 710 | if (of_property_read_u32(node, "fsl,tx-swing-full", |
| 711 | &imx6_pcie->tx_swing_full)) |
| 712 | imx6_pcie->tx_swing_full = 127; |
| 713 | |
| 714 | if (of_property_read_u32(node, "fsl,tx-swing-low", |
| 715 | &imx6_pcie->tx_swing_low)) |
| 716 | imx6_pcie->tx_swing_low = 127; |
| 717 | |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 718 | /* Limit link speed */ |
Bjorn Helgaas | c5af407 | 2016-10-06 13:35:18 -0500 | [diff] [blame^] | 719 | ret = of_property_read_u32(node, "fsl,max-link-speed", |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 720 | &imx6_pcie->link_gen); |
| 721 | if (ret) |
| 722 | imx6_pcie->link_gen = 1; |
| 723 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 724 | ret = imx6_add_pcie_port(pp, pdev); |
| 725 | if (ret < 0) |
Fabio Estevam | b391bf3 | 2013-12-02 01:39:35 -0200 | [diff] [blame] | 726 | return ret; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 727 | |
| 728 | platform_set_drvdata(pdev, imx6_pcie); |
| 729 | return 0; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 730 | } |
| 731 | |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 732 | static void imx6_pcie_shutdown(struct platform_device *pdev) |
| 733 | { |
| 734 | struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev); |
| 735 | |
| 736 | /* bring down link, so bootloader gets clean state in case of reboot */ |
| 737 | imx6_pcie_assert_core_reset(&imx6_pcie->pp); |
| 738 | } |
| 739 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 740 | static const struct of_device_id imx6_pcie_of_match[] = { |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 741 | { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, }, |
| 742 | { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, }, |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 743 | { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, }, |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 744 | {}, |
| 745 | }; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 746 | |
| 747 | static struct platform_driver imx6_pcie_driver = { |
| 748 | .driver = { |
| 749 | .name = "imx6q-pcie", |
Sachin Kamat | 8bcadbe | 2013-10-21 14:36:41 +0530 | [diff] [blame] | 750 | .of_match_table = imx6_pcie_of_match, |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 751 | }, |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 752 | .shutdown = imx6_pcie_shutdown, |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 753 | }; |
| 754 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 755 | static int __init imx6_pcie_init(void) |
| 756 | { |
| 757 | return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe); |
| 758 | } |
Paul Gortmaker | f90d8e8 | 2016-08-22 17:59:43 -0400 | [diff] [blame] | 759 | device_initcall(imx6_pcie_init); |