Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * PCIe host controller driver for Freescale i.MX6 SoCs |
| 4 | * |
| 5 | * Copyright (C) 2013 Kosagi |
| 6 | * http://www.kosagi.com |
| 7 | * |
| 8 | * Author: Sean Cross <xobs@kosagi.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/gpio.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/mfd/syscon.h> |
| 16 | #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> |
| 17 | #include <linux/mfd/syscon/imx7-iomuxc-gpr.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_gpio.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | #include <linux/resource.h> |
| 26 | #include <linux/signal.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/reset.h> |
| 30 | |
| 31 | #include "pcie-designware.h" |
| 32 | |
| 33 | #define to_imx6_pcie(x) dev_get_drvdata((x)->dev) |
| 34 | |
| 35 | enum imx6_pcie_variants { |
| 36 | IMX6Q, |
| 37 | IMX6SX, |
| 38 | IMX6QP, |
| 39 | IMX7D, |
| 40 | }; |
| 41 | |
| 42 | struct imx6_pcie { |
| 43 | struct dw_pcie *pci; |
| 44 | int reset_gpio; |
| 45 | bool gpio_active_high; |
| 46 | struct clk *pcie_bus; |
| 47 | struct clk *pcie_phy; |
| 48 | struct clk *pcie_inbound_axi; |
| 49 | struct clk *pcie; |
| 50 | struct regmap *iomuxc_gpr; |
| 51 | struct reset_control *pciephy_reset; |
| 52 | struct reset_control *apps_reset; |
| 53 | enum imx6_pcie_variants variant; |
| 54 | u32 tx_deemph_gen1; |
| 55 | u32 tx_deemph_gen2_3p5db; |
| 56 | u32 tx_deemph_gen2_6db; |
| 57 | u32 tx_swing_full; |
| 58 | u32 tx_swing_low; |
| 59 | int link_gen; |
| 60 | struct regulator *vpcie; |
| 61 | }; |
| 62 | |
| 63 | /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */ |
| 64 | #define PHY_PLL_LOCK_WAIT_MAX_RETRIES 2000 |
| 65 | #define PHY_PLL_LOCK_WAIT_USLEEP_MIN 50 |
| 66 | #define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200 |
| 67 | |
| 68 | /* PCIe Root Complex registers (memory-mapped) */ |
| 69 | #define PCIE_RC_LCR 0x7c |
| 70 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1 |
| 71 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2 |
| 72 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf |
| 73 | |
| 74 | #define PCIE_RC_LCSR 0x80 |
| 75 | |
| 76 | /* PCIe Port Logic registers (memory-mapped) */ |
| 77 | #define PL_OFFSET 0x700 |
| 78 | #define PCIE_PL_PFLR (PL_OFFSET + 0x08) |
| 79 | #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16) |
| 80 | #define PCIE_PL_PFLR_FORCE_LINK (1 << 15) |
| 81 | #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28) |
| 82 | #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c) |
| 83 | |
| 84 | #define PCIE_PHY_CTRL (PL_OFFSET + 0x114) |
| 85 | #define PCIE_PHY_CTRL_DATA_LOC 0 |
| 86 | #define PCIE_PHY_CTRL_CAP_ADR_LOC 16 |
| 87 | #define PCIE_PHY_CTRL_CAP_DAT_LOC 17 |
| 88 | #define PCIE_PHY_CTRL_WR_LOC 18 |
| 89 | #define PCIE_PHY_CTRL_RD_LOC 19 |
| 90 | |
| 91 | #define PCIE_PHY_STAT (PL_OFFSET + 0x110) |
| 92 | #define PCIE_PHY_STAT_ACK_LOC 16 |
| 93 | |
| 94 | #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C |
| 95 | #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17) |
| 96 | |
| 97 | /* PHY registers (not memory-mapped) */ |
| 98 | #define PCIE_PHY_RX_ASIC_OUT 0x100D |
| 99 | #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0) |
| 100 | |
| 101 | #define PHY_RX_OVRD_IN_LO 0x1005 |
| 102 | #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5) |
| 103 | #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3) |
| 104 | |
| 105 | static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val) |
| 106 | { |
| 107 | struct dw_pcie *pci = imx6_pcie->pci; |
| 108 | u32 val; |
| 109 | u32 max_iterations = 10; |
| 110 | u32 wait_counter = 0; |
| 111 | |
| 112 | do { |
| 113 | val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT); |
| 114 | val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1; |
| 115 | wait_counter++; |
| 116 | |
| 117 | if (val == exp_val) |
| 118 | return 0; |
| 119 | |
| 120 | udelay(1); |
| 121 | } while (wait_counter < max_iterations); |
| 122 | |
| 123 | return -ETIMEDOUT; |
| 124 | } |
| 125 | |
| 126 | static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr) |
| 127 | { |
| 128 | struct dw_pcie *pci = imx6_pcie->pci; |
| 129 | u32 val; |
| 130 | int ret; |
| 131 | |
| 132 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
| 133 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val); |
| 134 | |
| 135 | val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC); |
| 136 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val); |
| 137 | |
| 138 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
| 139 | if (ret) |
| 140 | return ret; |
| 141 | |
| 142 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
| 143 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val); |
| 144 | |
| 145 | return pcie_phy_poll_ack(imx6_pcie, 0); |
| 146 | } |
| 147 | |
| 148 | /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */ |
| 149 | static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data) |
| 150 | { |
| 151 | struct dw_pcie *pci = imx6_pcie->pci; |
| 152 | u32 val, phy_ctl; |
| 153 | int ret; |
| 154 | |
| 155 | ret = pcie_phy_wait_ack(imx6_pcie, addr); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | /* assert Read signal */ |
| 160 | phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC; |
| 161 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl); |
| 162 | |
| 163 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT); |
| 168 | *data = val & 0xffff; |
| 169 | |
| 170 | /* deassert Read signal */ |
| 171 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00); |
| 172 | |
| 173 | return pcie_phy_poll_ack(imx6_pcie, 0); |
| 174 | } |
| 175 | |
| 176 | static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data) |
| 177 | { |
| 178 | struct dw_pcie *pci = imx6_pcie->pci; |
| 179 | u32 var; |
| 180 | int ret; |
| 181 | |
| 182 | /* write addr */ |
| 183 | /* cap addr */ |
| 184 | ret = pcie_phy_wait_ack(imx6_pcie, addr); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 189 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
| 190 | |
| 191 | /* capture data */ |
| 192 | var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC); |
| 193 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
| 194 | |
| 195 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | /* deassert cap data */ |
| 200 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 201 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
| 202 | |
| 203 | /* wait for ack de-assertion */ |
| 204 | ret = pcie_phy_poll_ack(imx6_pcie, 0); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
| 208 | /* assert wr signal */ |
| 209 | var = 0x1 << PCIE_PHY_CTRL_WR_LOC; |
| 210 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
| 211 | |
| 212 | /* wait for ack */ |
| 213 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | /* deassert wr signal */ |
| 218 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 219 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
| 220 | |
| 221 | /* wait for ack de-assertion */ |
| 222 | ret = pcie_phy_poll_ack(imx6_pcie, 0); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie) |
| 232 | { |
| 233 | u32 tmp; |
| 234 | |
| 235 | pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp); |
| 236 | tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | |
| 237 | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
| 238 | pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp); |
| 239 | |
| 240 | usleep_range(2000, 3000); |
| 241 | |
| 242 | pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp); |
| 243 | tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | |
| 244 | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
| 245 | pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp); |
| 246 | } |
| 247 | |
| 248 | /* Added for PCI abort handling */ |
| 249 | static int imx6q_pcie_abort_handler(unsigned long addr, |
| 250 | unsigned int fsr, struct pt_regs *regs) |
| 251 | { |
| 252 | unsigned long pc = instruction_pointer(regs); |
| 253 | unsigned long instr = *(unsigned long *)pc; |
| 254 | int reg = (instr >> 12) & 15; |
| 255 | |
| 256 | /* |
| 257 | * If the instruction being executed was a read, |
| 258 | * make it look like it read all-ones. |
| 259 | */ |
| 260 | if ((instr & 0x0c100000) == 0x04100000) { |
| 261 | unsigned long val; |
| 262 | |
| 263 | if (instr & 0x00400000) |
| 264 | val = 255; |
| 265 | else |
| 266 | val = -1; |
| 267 | |
| 268 | regs->uregs[reg] = val; |
| 269 | regs->ARM_pc += 4; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | if ((instr & 0x0e100090) == 0x00100090) { |
| 274 | regs->uregs[reg] = -1; |
| 275 | regs->ARM_pc += 4; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie) |
| 283 | { |
| 284 | struct device *dev = imx6_pcie->pci->dev; |
| 285 | |
| 286 | switch (imx6_pcie->variant) { |
| 287 | case IMX7D: |
| 288 | reset_control_assert(imx6_pcie->pciephy_reset); |
| 289 | reset_control_assert(imx6_pcie->apps_reset); |
| 290 | break; |
| 291 | case IMX6SX: |
| 292 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 293 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN, |
| 294 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN); |
| 295 | /* Force PCIe PHY reset */ |
| 296 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5, |
| 297 | IMX6SX_GPR5_PCIE_BTNRST_RESET, |
| 298 | IMX6SX_GPR5_PCIE_BTNRST_RESET); |
| 299 | break; |
| 300 | case IMX6QP: |
| 301 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 302 | IMX6Q_GPR1_PCIE_SW_RST, |
| 303 | IMX6Q_GPR1_PCIE_SW_RST); |
| 304 | break; |
| 305 | case IMX6Q: |
| 306 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 307 | IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18); |
| 308 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 309 | IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) { |
| 314 | int ret = regulator_disable(imx6_pcie->vpcie); |
| 315 | |
| 316 | if (ret) |
| 317 | dev_err(dev, "failed to disable vpcie regulator: %d\n", |
| 318 | ret); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie) |
| 323 | { |
| 324 | struct dw_pcie *pci = imx6_pcie->pci; |
| 325 | struct device *dev = pci->dev; |
| 326 | int ret = 0; |
| 327 | |
| 328 | switch (imx6_pcie->variant) { |
| 329 | case IMX6SX: |
| 330 | ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi); |
| 331 | if (ret) { |
| 332 | dev_err(dev, "unable to enable pcie_axi clock\n"); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 337 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0); |
| 338 | break; |
| 339 | case IMX6QP: /* FALLTHROUGH */ |
| 340 | case IMX6Q: |
| 341 | /* power up core phy and enable ref clock */ |
| 342 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 343 | IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18); |
| 344 | /* |
| 345 | * the async reset input need ref clock to sync internally, |
| 346 | * when the ref clock comes after reset, internal synced |
| 347 | * reset time is too short, cannot meet the requirement. |
| 348 | * add one ~10us delay here. |
| 349 | */ |
| 350 | udelay(10); |
| 351 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 352 | IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16); |
| 353 | break; |
| 354 | case IMX7D: |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie) |
| 362 | { |
| 363 | u32 val; |
| 364 | unsigned int retries; |
| 365 | struct device *dev = imx6_pcie->pci->dev; |
| 366 | |
| 367 | for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES; retries++) { |
| 368 | regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR22, &val); |
| 369 | |
| 370 | if (val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED) |
| 371 | return; |
| 372 | |
| 373 | usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN, |
| 374 | PHY_PLL_LOCK_WAIT_USLEEP_MAX); |
| 375 | } |
| 376 | |
| 377 | dev_err(dev, "PCIe PLL lock timeout\n"); |
| 378 | } |
| 379 | |
| 380 | static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie) |
| 381 | { |
| 382 | struct dw_pcie *pci = imx6_pcie->pci; |
| 383 | struct device *dev = pci->dev; |
| 384 | int ret; |
| 385 | |
| 386 | if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) { |
| 387 | ret = regulator_enable(imx6_pcie->vpcie); |
| 388 | if (ret) { |
| 389 | dev_err(dev, "failed to enable vpcie regulator: %d\n", |
| 390 | ret); |
| 391 | return; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | ret = clk_prepare_enable(imx6_pcie->pcie_phy); |
| 396 | if (ret) { |
| 397 | dev_err(dev, "unable to enable pcie_phy clock\n"); |
| 398 | goto err_pcie_phy; |
| 399 | } |
| 400 | |
| 401 | ret = clk_prepare_enable(imx6_pcie->pcie_bus); |
| 402 | if (ret) { |
| 403 | dev_err(dev, "unable to enable pcie_bus clock\n"); |
| 404 | goto err_pcie_bus; |
| 405 | } |
| 406 | |
| 407 | ret = clk_prepare_enable(imx6_pcie->pcie); |
| 408 | if (ret) { |
| 409 | dev_err(dev, "unable to enable pcie clock\n"); |
| 410 | goto err_pcie; |
| 411 | } |
| 412 | |
| 413 | ret = imx6_pcie_enable_ref_clk(imx6_pcie); |
| 414 | if (ret) { |
| 415 | dev_err(dev, "unable to enable pcie ref clock\n"); |
| 416 | goto err_ref_clk; |
| 417 | } |
| 418 | |
| 419 | /* allow the clocks to stabilize */ |
| 420 | usleep_range(200, 500); |
| 421 | |
| 422 | /* Some boards don't have PCIe reset GPIO. */ |
| 423 | if (gpio_is_valid(imx6_pcie->reset_gpio)) { |
| 424 | gpio_set_value_cansleep(imx6_pcie->reset_gpio, |
| 425 | imx6_pcie->gpio_active_high); |
| 426 | msleep(100); |
| 427 | gpio_set_value_cansleep(imx6_pcie->reset_gpio, |
| 428 | !imx6_pcie->gpio_active_high); |
| 429 | } |
| 430 | |
| 431 | switch (imx6_pcie->variant) { |
| 432 | case IMX7D: |
| 433 | reset_control_deassert(imx6_pcie->pciephy_reset); |
| 434 | imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie); |
| 435 | break; |
| 436 | case IMX6SX: |
| 437 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5, |
| 438 | IMX6SX_GPR5_PCIE_BTNRST_RESET, 0); |
| 439 | break; |
| 440 | case IMX6QP: |
| 441 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 442 | IMX6Q_GPR1_PCIE_SW_RST, 0); |
| 443 | |
| 444 | usleep_range(200, 500); |
| 445 | break; |
| 446 | case IMX6Q: /* Nothing to do */ |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | return; |
| 451 | |
| 452 | err_ref_clk: |
| 453 | clk_disable_unprepare(imx6_pcie->pcie); |
| 454 | err_pcie: |
| 455 | clk_disable_unprepare(imx6_pcie->pcie_bus); |
| 456 | err_pcie_bus: |
| 457 | clk_disable_unprepare(imx6_pcie->pcie_phy); |
| 458 | err_pcie_phy: |
| 459 | if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) { |
| 460 | ret = regulator_disable(imx6_pcie->vpcie); |
| 461 | if (ret) |
| 462 | dev_err(dev, "failed to disable vpcie regulator: %d\n", |
| 463 | ret); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie) |
| 468 | { |
| 469 | switch (imx6_pcie->variant) { |
| 470 | case IMX7D: |
| 471 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 472 | IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0); |
| 473 | break; |
| 474 | case IMX6SX: |
| 475 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 476 | IMX6SX_GPR12_PCIE_RX_EQ_MASK, |
| 477 | IMX6SX_GPR12_PCIE_RX_EQ_2); |
| 478 | /* FALLTHROUGH */ |
| 479 | default: |
| 480 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 481 | IMX6Q_GPR12_PCIE_CTL_2, 0 << 10); |
| 482 | |
| 483 | /* configure constant input signal to the pcie ctrl and phy */ |
| 484 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 485 | IMX6Q_GPR12_LOS_LEVEL, 9 << 4); |
| 486 | |
| 487 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 488 | IMX6Q_GPR8_TX_DEEMPH_GEN1, |
| 489 | imx6_pcie->tx_deemph_gen1 << 0); |
| 490 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 491 | IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, |
| 492 | imx6_pcie->tx_deemph_gen2_3p5db << 6); |
| 493 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 494 | IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, |
| 495 | imx6_pcie->tx_deemph_gen2_6db << 12); |
| 496 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 497 | IMX6Q_GPR8_TX_SWING_FULL, |
| 498 | imx6_pcie->tx_swing_full << 18); |
| 499 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 500 | IMX6Q_GPR8_TX_SWING_LOW, |
| 501 | imx6_pcie->tx_swing_low << 25); |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 506 | IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12); |
| 507 | } |
| 508 | |
| 509 | static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie) |
| 510 | { |
| 511 | struct dw_pcie *pci = imx6_pcie->pci; |
| 512 | struct device *dev = pci->dev; |
| 513 | |
| 514 | /* check if the link is up or not */ |
| 515 | if (!dw_pcie_wait_for_link(pci)) |
| 516 | return 0; |
| 517 | |
| 518 | dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n", |
| 519 | dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0), |
| 520 | dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1)); |
| 521 | return -ETIMEDOUT; |
| 522 | } |
| 523 | |
| 524 | static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie) |
| 525 | { |
| 526 | struct dw_pcie *pci = imx6_pcie->pci; |
| 527 | struct device *dev = pci->dev; |
| 528 | u32 tmp; |
| 529 | unsigned int retries; |
| 530 | |
| 531 | for (retries = 0; retries < 200; retries++) { |
| 532 | tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 533 | /* Test if the speed change finished. */ |
| 534 | if (!(tmp & PORT_LOGIC_SPEED_CHANGE)) |
| 535 | return 0; |
| 536 | usleep_range(100, 1000); |
| 537 | } |
| 538 | |
| 539 | dev_err(dev, "Speed change timeout\n"); |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
| 543 | static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie) |
| 544 | { |
| 545 | struct dw_pcie *pci = imx6_pcie->pci; |
| 546 | struct device *dev = pci->dev; |
| 547 | u32 tmp; |
| 548 | int ret; |
| 549 | |
| 550 | /* |
| 551 | * Force Gen1 operation when starting the link. In case the link is |
| 552 | * started in Gen2 mode, there is a possibility the devices on the |
| 553 | * bus will not be detected at all. This happens with PCIe switches. |
| 554 | */ |
| 555 | tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR); |
| 556 | tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK; |
| 557 | tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1; |
| 558 | dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp); |
| 559 | |
| 560 | /* Start LTSSM. */ |
| 561 | if (imx6_pcie->variant == IMX7D) |
| 562 | reset_control_deassert(imx6_pcie->apps_reset); |
| 563 | else |
| 564 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 565 | IMX6Q_GPR12_PCIE_CTL_2, 1 << 10); |
| 566 | |
| 567 | ret = imx6_pcie_wait_for_link(imx6_pcie); |
| 568 | if (ret) |
| 569 | goto err_reset_phy; |
| 570 | |
| 571 | if (imx6_pcie->link_gen == 2) { |
| 572 | /* Allow Gen2 mode after the link is up. */ |
| 573 | tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR); |
| 574 | tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK; |
| 575 | tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2; |
| 576 | dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp); |
| 577 | |
| 578 | /* |
| 579 | * Start Directed Speed Change so the best possible |
| 580 | * speed both link partners support can be negotiated. |
| 581 | */ |
| 582 | tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 583 | tmp |= PORT_LOGIC_SPEED_CHANGE; |
| 584 | dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp); |
| 585 | |
| 586 | if (imx6_pcie->variant != IMX7D) { |
| 587 | /* |
| 588 | * On i.MX7, DIRECT_SPEED_CHANGE behaves differently |
| 589 | * from i.MX6 family when no link speed transition |
| 590 | * occurs and we go Gen1 -> yep, Gen1. The difference |
| 591 | * is that, in such case, it will not be cleared by HW |
| 592 | * which will cause the following code to report false |
| 593 | * failure. |
| 594 | */ |
| 595 | |
| 596 | ret = imx6_pcie_wait_for_speed_change(imx6_pcie); |
| 597 | if (ret) { |
| 598 | dev_err(dev, "Failed to bring link up!\n"); |
| 599 | goto err_reset_phy; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /* Make sure link training is finished as well! */ |
| 604 | ret = imx6_pcie_wait_for_link(imx6_pcie); |
| 605 | if (ret) { |
| 606 | dev_err(dev, "Failed to bring link up!\n"); |
| 607 | goto err_reset_phy; |
| 608 | } |
| 609 | } else { |
| 610 | dev_info(dev, "Link: Gen2 disabled\n"); |
| 611 | } |
| 612 | |
| 613 | tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR); |
| 614 | dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf); |
| 615 | return 0; |
| 616 | |
| 617 | err_reset_phy: |
| 618 | dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n", |
| 619 | dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0), |
| 620 | dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1)); |
| 621 | imx6_pcie_reset_phy(imx6_pcie); |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | static int imx6_pcie_host_init(struct pcie_port *pp) |
| 626 | { |
| 627 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 628 | struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci); |
| 629 | |
| 630 | imx6_pcie_assert_core_reset(imx6_pcie); |
| 631 | imx6_pcie_init_phy(imx6_pcie); |
| 632 | imx6_pcie_deassert_core_reset(imx6_pcie); |
| 633 | dw_pcie_setup_rc(pp); |
| 634 | imx6_pcie_establish_link(imx6_pcie); |
| 635 | |
| 636 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 637 | dw_pcie_msi_init(pp); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static const struct dw_pcie_host_ops imx6_pcie_host_ops = { |
| 643 | .host_init = imx6_pcie_host_init, |
| 644 | }; |
| 645 | |
| 646 | static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie, |
| 647 | struct platform_device *pdev) |
| 648 | { |
| 649 | struct dw_pcie *pci = imx6_pcie->pci; |
| 650 | struct pcie_port *pp = &pci->pp; |
| 651 | struct device *dev = &pdev->dev; |
| 652 | int ret; |
| 653 | |
| 654 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 655 | pp->msi_irq = platform_get_irq_byname(pdev, "msi"); |
| 656 | if (pp->msi_irq <= 0) { |
| 657 | dev_err(dev, "failed to get MSI irq\n"); |
| 658 | return -ENODEV; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | pp->ops = &imx6_pcie_host_ops; |
| 663 | |
| 664 | ret = dw_pcie_host_init(pp); |
| 665 | if (ret) { |
| 666 | dev_err(dev, "failed to initialize host\n"); |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static const struct dw_pcie_ops dw_pcie_ops = { |
| 674 | /* No special ops needed, but pcie-designware still expects this struct */ |
| 675 | }; |
| 676 | |
| 677 | static int imx6_pcie_probe(struct platform_device *pdev) |
| 678 | { |
| 679 | struct device *dev = &pdev->dev; |
| 680 | struct dw_pcie *pci; |
| 681 | struct imx6_pcie *imx6_pcie; |
| 682 | struct resource *dbi_base; |
| 683 | struct device_node *node = dev->of_node; |
| 684 | int ret; |
| 685 | |
| 686 | imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL); |
| 687 | if (!imx6_pcie) |
| 688 | return -ENOMEM; |
| 689 | |
| 690 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 691 | if (!pci) |
| 692 | return -ENOMEM; |
| 693 | |
| 694 | pci->dev = dev; |
| 695 | pci->ops = &dw_pcie_ops; |
| 696 | |
| 697 | imx6_pcie->pci = pci; |
| 698 | imx6_pcie->variant = |
| 699 | (enum imx6_pcie_variants)of_device_get_match_data(dev); |
| 700 | |
| 701 | dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 702 | pci->dbi_base = devm_ioremap_resource(dev, dbi_base); |
| 703 | if (IS_ERR(pci->dbi_base)) |
| 704 | return PTR_ERR(pci->dbi_base); |
| 705 | |
| 706 | /* Fetch GPIOs */ |
| 707 | imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0); |
| 708 | imx6_pcie->gpio_active_high = of_property_read_bool(node, |
| 709 | "reset-gpio-active-high"); |
| 710 | if (gpio_is_valid(imx6_pcie->reset_gpio)) { |
| 711 | ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio, |
| 712 | imx6_pcie->gpio_active_high ? |
| 713 | GPIOF_OUT_INIT_HIGH : |
| 714 | GPIOF_OUT_INIT_LOW, |
| 715 | "PCIe reset"); |
| 716 | if (ret) { |
| 717 | dev_err(dev, "unable to get reset gpio\n"); |
| 718 | return ret; |
| 719 | } |
| 720 | } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) { |
| 721 | return imx6_pcie->reset_gpio; |
| 722 | } |
| 723 | |
| 724 | /* Fetch clocks */ |
| 725 | imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy"); |
| 726 | if (IS_ERR(imx6_pcie->pcie_phy)) { |
| 727 | dev_err(dev, "pcie_phy clock source missing or invalid\n"); |
| 728 | return PTR_ERR(imx6_pcie->pcie_phy); |
| 729 | } |
| 730 | |
| 731 | imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus"); |
| 732 | if (IS_ERR(imx6_pcie->pcie_bus)) { |
| 733 | dev_err(dev, "pcie_bus clock source missing or invalid\n"); |
| 734 | return PTR_ERR(imx6_pcie->pcie_bus); |
| 735 | } |
| 736 | |
| 737 | imx6_pcie->pcie = devm_clk_get(dev, "pcie"); |
| 738 | if (IS_ERR(imx6_pcie->pcie)) { |
| 739 | dev_err(dev, "pcie clock source missing or invalid\n"); |
| 740 | return PTR_ERR(imx6_pcie->pcie); |
| 741 | } |
| 742 | |
| 743 | switch (imx6_pcie->variant) { |
| 744 | case IMX6SX: |
| 745 | imx6_pcie->pcie_inbound_axi = devm_clk_get(dev, |
| 746 | "pcie_inbound_axi"); |
| 747 | if (IS_ERR(imx6_pcie->pcie_inbound_axi)) { |
| 748 | dev_err(dev, "pcie_inbound_axi clock missing or invalid\n"); |
| 749 | return PTR_ERR(imx6_pcie->pcie_inbound_axi); |
| 750 | } |
| 751 | break; |
| 752 | case IMX7D: |
| 753 | imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev, |
| 754 | "pciephy"); |
| 755 | if (IS_ERR(imx6_pcie->pciephy_reset)) { |
| 756 | dev_err(dev, "Failed to get PCIEPHY reset control\n"); |
| 757 | return PTR_ERR(imx6_pcie->pciephy_reset); |
| 758 | } |
| 759 | |
| 760 | imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev, |
| 761 | "apps"); |
| 762 | if (IS_ERR(imx6_pcie->apps_reset)) { |
| 763 | dev_err(dev, "Failed to get PCIE APPS reset control\n"); |
| 764 | return PTR_ERR(imx6_pcie->apps_reset); |
| 765 | } |
| 766 | break; |
| 767 | default: |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | /* Grab GPR config register range */ |
| 772 | imx6_pcie->iomuxc_gpr = |
| 773 | syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr"); |
| 774 | if (IS_ERR(imx6_pcie->iomuxc_gpr)) { |
| 775 | dev_err(dev, "unable to find iomuxc registers\n"); |
| 776 | return PTR_ERR(imx6_pcie->iomuxc_gpr); |
| 777 | } |
| 778 | |
| 779 | /* Grab PCIe PHY Tx Settings */ |
| 780 | if (of_property_read_u32(node, "fsl,tx-deemph-gen1", |
| 781 | &imx6_pcie->tx_deemph_gen1)) |
| 782 | imx6_pcie->tx_deemph_gen1 = 0; |
| 783 | |
| 784 | if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db", |
| 785 | &imx6_pcie->tx_deemph_gen2_3p5db)) |
| 786 | imx6_pcie->tx_deemph_gen2_3p5db = 0; |
| 787 | |
| 788 | if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db", |
| 789 | &imx6_pcie->tx_deemph_gen2_6db)) |
| 790 | imx6_pcie->tx_deemph_gen2_6db = 20; |
| 791 | |
| 792 | if (of_property_read_u32(node, "fsl,tx-swing-full", |
| 793 | &imx6_pcie->tx_swing_full)) |
| 794 | imx6_pcie->tx_swing_full = 127; |
| 795 | |
| 796 | if (of_property_read_u32(node, "fsl,tx-swing-low", |
| 797 | &imx6_pcie->tx_swing_low)) |
| 798 | imx6_pcie->tx_swing_low = 127; |
| 799 | |
| 800 | /* Limit link speed */ |
| 801 | ret = of_property_read_u32(node, "fsl,max-link-speed", |
| 802 | &imx6_pcie->link_gen); |
| 803 | if (ret) |
| 804 | imx6_pcie->link_gen = 1; |
| 805 | |
| 806 | imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie"); |
| 807 | if (IS_ERR(imx6_pcie->vpcie)) { |
| 808 | if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER) |
| 809 | return -EPROBE_DEFER; |
| 810 | imx6_pcie->vpcie = NULL; |
| 811 | } |
| 812 | |
| 813 | platform_set_drvdata(pdev, imx6_pcie); |
| 814 | |
| 815 | ret = imx6_add_pcie_port(imx6_pcie, pdev); |
| 816 | if (ret < 0) |
| 817 | return ret; |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static void imx6_pcie_shutdown(struct platform_device *pdev) |
| 823 | { |
| 824 | struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev); |
| 825 | |
| 826 | /* bring down link, so bootloader gets clean state in case of reboot */ |
| 827 | imx6_pcie_assert_core_reset(imx6_pcie); |
| 828 | } |
| 829 | |
| 830 | static const struct of_device_id imx6_pcie_of_match[] = { |
| 831 | { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, }, |
| 832 | { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, }, |
| 833 | { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, }, |
| 834 | { .compatible = "fsl,imx7d-pcie", .data = (void *)IMX7D, }, |
| 835 | {}, |
| 836 | }; |
| 837 | |
| 838 | static struct platform_driver imx6_pcie_driver = { |
| 839 | .driver = { |
| 840 | .name = "imx6q-pcie", |
| 841 | .of_match_table = imx6_pcie_of_match, |
| 842 | .suppress_bind_attrs = true, |
| 843 | }, |
| 844 | .probe = imx6_pcie_probe, |
| 845 | .shutdown = imx6_pcie_shutdown, |
| 846 | }; |
| 847 | |
| 848 | static int __init imx6_pcie_init(void) |
| 849 | { |
| 850 | /* |
| 851 | * Since probe() can be deferred we need to make sure that |
| 852 | * hook_fault_code is not called after __init memory is freed |
| 853 | * by kernel and since imx6q_pcie_abort_handler() is a no-op, |
| 854 | * we can install the handler here without risking it |
| 855 | * accessing some uninitialized driver state. |
| 856 | */ |
| 857 | hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0, |
| 858 | "external abort on non-linefetch"); |
| 859 | |
| 860 | return platform_driver_register(&imx6_pcie_driver); |
| 861 | } |
| 862 | device_initcall(imx6_pcie_init); |