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