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