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