Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 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> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 11 | #include <linux/bitfield.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 12 | #include <linux/clk.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/mfd/syscon.h> |
| 17 | #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 18 | #include <linux/mfd/syscon/imx7-iomuxc-gpr.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/of_gpio.h> |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Trent Piepho | 1df82ec | 2019-02-05 00:17:41 +0000 | [diff] [blame] | 22 | #include <linux/of_address.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 23 | #include <linux/pci.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/regmap.h> |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 26 | #include <linux/regulator/consumer.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 27 | #include <linux/resource.h> |
| 28 | #include <linux/signal.h> |
| 29 | #include <linux/types.h> |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 31 | #include <linux/reset.h> |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 32 | #include <linux/pm_domain.h> |
| 33 | #include <linux/pm_runtime.h> |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 34 | |
| 35 | #include "pcie-designware.h" |
| 36 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 37 | #define IMX8MQ_GPR_PCIE_REF_USE_PAD BIT(9) |
| 38 | #define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN BIT(10) |
| 39 | #define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE BIT(11) |
| 40 | #define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE GENMASK(11, 8) |
| 41 | #define IMX8MQ_PCIE2_BASE_ADDR 0x33c00000 |
| 42 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 43 | #define to_imx6_pcie(x) dev_get_drvdata((x)->dev) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 44 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 45 | enum imx6_pcie_variants { |
| 46 | IMX6Q, |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 47 | IMX6SX, |
| 48 | IMX6QP, |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 49 | IMX7D, |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 50 | IMX8MQ, |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
Andrey Smirnov | 2f532d07 | 2019-02-01 16:15:21 -0800 | [diff] [blame] | 53 | #define IMX6_PCIE_FLAG_IMX6_PHY BIT(0) |
Andrey Smirnov | 4c458bb | 2019-02-01 16:15:22 -0800 | [diff] [blame] | 54 | #define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE BIT(1) |
Andrey Smirnov | 2f532d07 | 2019-02-01 16:15:21 -0800 | [diff] [blame] | 55 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 56 | struct imx6_pcie_drvdata { |
| 57 | enum imx6_pcie_variants variant; |
Andrey Smirnov | 2f532d07 | 2019-02-01 16:15:21 -0800 | [diff] [blame] | 58 | u32 flags; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct imx6_pcie { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 62 | struct dw_pcie *pci; |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 63 | int reset_gpio; |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 64 | bool gpio_active_high; |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 65 | struct clk *pcie_bus; |
| 66 | struct clk *pcie_phy; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 67 | struct clk *pcie_inbound_axi; |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 68 | struct clk *pcie; |
Andrey Smirnov | 5278f65 | 2019-02-11 17:51:08 -0800 | [diff] [blame] | 69 | struct clk *pcie_aux; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 70 | struct regmap *iomuxc_gpr; |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 71 | u32 controller_id; |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 72 | struct reset_control *pciephy_reset; |
| 73 | struct reset_control *apps_reset; |
Leonard Crestez | f4e833b | 2018-07-19 17:02:10 +0300 | [diff] [blame] | 74 | struct reset_control *turnoff_reset; |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 75 | u32 tx_deemph_gen1; |
| 76 | u32 tx_deemph_gen2_3p5db; |
| 77 | u32 tx_deemph_gen2_6db; |
| 78 | u32 tx_swing_full; |
| 79 | u32 tx_swing_low; |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 80 | int link_gen; |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 81 | struct regulator *vpcie; |
Trent Piepho | 1df82ec | 2019-02-05 00:17:41 +0000 | [diff] [blame] | 82 | void __iomem *phy_base; |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 83 | |
| 84 | /* power domain for pcie */ |
| 85 | struct device *pd_pcie; |
| 86 | /* power domain for pcie phy */ |
| 87 | struct device *pd_pcie_phy; |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 88 | const struct imx6_pcie_drvdata *drvdata; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 89 | }; |
| 90 | |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 91 | /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */ |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 92 | #define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200 |
Andrey Smirnov | 9e303be | 2019-04-14 17:46:22 -0700 | [diff] [blame] | 93 | #define PHY_PLL_LOCK_WAIT_TIMEOUT (2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX) |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 94 | |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 95 | /* PCIe Root Complex registers (memory-mapped) */ |
Richard Zhu | 75cb8d2 | 2018-12-21 04:33:38 +0000 | [diff] [blame] | 96 | #define PCIE_RC_IMX6_MSI_CAP 0x50 |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 97 | #define PCIE_RC_LCR 0x7c |
| 98 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1 |
| 99 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2 |
| 100 | #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf |
| 101 | |
Bjorn Helgaas | 2393f79 | 2015-06-12 17:27:43 -0500 | [diff] [blame] | 102 | #define PCIE_RC_LCSR 0x80 |
| 103 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 104 | /* PCIe Port Logic registers (memory-mapped) */ |
| 105 | #define PL_OFFSET 0x700 |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 106 | #define PCIE_PL_PFLR (PL_OFFSET + 0x08) |
| 107 | #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16) |
| 108 | #define PCIE_PL_PFLR_FORCE_LINK (1 << 15) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 109 | #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28) |
| 110 | #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c) |
| 111 | |
| 112 | #define PCIE_PHY_CTRL (PL_OFFSET + 0x114) |
| 113 | #define PCIE_PHY_CTRL_DATA_LOC 0 |
| 114 | #define PCIE_PHY_CTRL_CAP_ADR_LOC 16 |
| 115 | #define PCIE_PHY_CTRL_CAP_DAT_LOC 17 |
| 116 | #define PCIE_PHY_CTRL_WR_LOC 18 |
| 117 | #define PCIE_PHY_CTRL_RD_LOC 19 |
| 118 | |
| 119 | #define PCIE_PHY_STAT (PL_OFFSET + 0x110) |
| 120 | #define PCIE_PHY_STAT_ACK_LOC 16 |
| 121 | |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 122 | #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 123 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 124 | /* PHY registers (not memory-mapped) */ |
Lucas Stach | f18f42d | 2018-07-31 12:21:49 +0200 | [diff] [blame] | 125 | #define PCIE_PHY_ATEOVRD 0x10 |
| 126 | #define PCIE_PHY_ATEOVRD_EN (0x1 << 2) |
| 127 | #define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0 |
| 128 | #define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1 |
| 129 | |
| 130 | #define PCIE_PHY_MPLL_OVRD_IN_LO 0x11 |
| 131 | #define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2 |
| 132 | #define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f |
| 133 | #define PCIE_PHY_MPLL_MULTIPLIER_OVRD (0x1 << 9) |
| 134 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 135 | #define PCIE_PHY_RX_ASIC_OUT 0x100D |
Fabio Estevam | 111feb7 | 2015-09-11 09:08:53 -0300 | [diff] [blame] | 136 | #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 137 | |
Trent Piepho | 1df82ec | 2019-02-05 00:17:41 +0000 | [diff] [blame] | 138 | /* iMX7 PCIe PHY registers */ |
| 139 | #define PCIE_PHY_CMN_REG4 0x14 |
| 140 | /* These are probably the bits that *aren't* DCC_FB_EN */ |
| 141 | #define PCIE_PHY_CMN_REG4_DCC_FB_EN 0x29 |
| 142 | |
| 143 | #define PCIE_PHY_CMN_REG15 0x54 |
| 144 | #define PCIE_PHY_CMN_REG15_DLY_4 BIT(2) |
| 145 | #define PCIE_PHY_CMN_REG15_PLL_PD BIT(5) |
| 146 | #define PCIE_PHY_CMN_REG15_OVRD_PLL_PD BIT(7) |
| 147 | |
| 148 | #define PCIE_PHY_CMN_REG24 0x90 |
| 149 | #define PCIE_PHY_CMN_REG24_RX_EQ BIT(6) |
| 150 | #define PCIE_PHY_CMN_REG24_RX_EQ_SEL BIT(3) |
| 151 | |
| 152 | #define PCIE_PHY_CMN_REG26 0x98 |
| 153 | #define PCIE_PHY_CMN_REG26_ATT_MODE 0xBC |
| 154 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 155 | #define PHY_RX_OVRD_IN_LO 0x1005 |
| 156 | #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5) |
| 157 | #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3) |
| 158 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 159 | static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 160 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 161 | struct dw_pcie *pci = imx6_pcie->pci; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 162 | u32 val; |
| 163 | u32 max_iterations = 10; |
| 164 | u32 wait_counter = 0; |
| 165 | |
| 166 | do { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 167 | val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 168 | val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1; |
| 169 | wait_counter++; |
| 170 | |
| 171 | if (val == exp_val) |
| 172 | return 0; |
| 173 | |
| 174 | udelay(1); |
| 175 | } while (wait_counter < max_iterations); |
| 176 | |
| 177 | return -ETIMEDOUT; |
| 178 | } |
| 179 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 180 | static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 181 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 182 | struct dw_pcie *pci = imx6_pcie->pci; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 183 | u32 val; |
| 184 | int ret; |
| 185 | |
| 186 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 187 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 188 | |
| 189 | val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 190 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 191 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 192 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 197 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 198 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 199 | return pcie_phy_poll_ack(imx6_pcie, 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */ |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 203 | static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 204 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 205 | struct dw_pcie *pci = imx6_pcie->pci; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 206 | u32 val, phy_ctl; |
| 207 | int ret; |
| 208 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 209 | ret = pcie_phy_wait_ack(imx6_pcie, addr); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | /* assert Read signal */ |
| 214 | phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 215 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 216 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 217 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 218 | if (ret) |
| 219 | return ret; |
| 220 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 221 | val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 222 | *data = val & 0xffff; |
| 223 | |
| 224 | /* deassert Read signal */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 225 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 226 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 227 | return pcie_phy_poll_ack(imx6_pcie, 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 228 | } |
| 229 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 230 | static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 231 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 232 | struct dw_pcie *pci = imx6_pcie->pci; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 233 | u32 var; |
| 234 | int ret; |
| 235 | |
| 236 | /* write addr */ |
| 237 | /* cap addr */ |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 238 | ret = pcie_phy_wait_ack(imx6_pcie, addr); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 243 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 244 | |
| 245 | /* capture data */ |
| 246 | var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 247 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 248 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 249 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | /* deassert cap data */ |
| 254 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 255 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 256 | |
| 257 | /* wait for ack de-assertion */ |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 258 | ret = pcie_phy_poll_ack(imx6_pcie, 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 259 | if (ret) |
| 260 | return ret; |
| 261 | |
| 262 | /* assert wr signal */ |
| 263 | var = 0x1 << PCIE_PHY_CTRL_WR_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 264 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 265 | |
| 266 | /* wait for ack */ |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 267 | ret = pcie_phy_poll_ack(imx6_pcie, 1); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 268 | if (ret) |
| 269 | return ret; |
| 270 | |
| 271 | /* deassert wr signal */ |
| 272 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 273 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 274 | |
| 275 | /* wait for ack de-assertion */ |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 276 | ret = pcie_phy_poll_ack(imx6_pcie, 0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 277 | if (ret) |
| 278 | return ret; |
| 279 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 280 | dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 285 | static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie) |
Lucas Stach | 53eeb48 | 2016-01-15 19:56:47 +0100 | [diff] [blame] | 286 | { |
| 287 | u32 tmp; |
| 288 | |
Andrey Smirnov | 2f532d07 | 2019-02-01 16:15:21 -0800 | [diff] [blame] | 289 | if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY)) |
| 290 | return; |
| 291 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 292 | pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp); |
Lucas Stach | 53eeb48 | 2016-01-15 19:56:47 +0100 | [diff] [blame] | 293 | tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | |
| 294 | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 295 | pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp); |
Lucas Stach | 53eeb48 | 2016-01-15 19:56:47 +0100 | [diff] [blame] | 296 | |
| 297 | usleep_range(2000, 3000); |
| 298 | |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 299 | pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp); |
Lucas Stach | 53eeb48 | 2016-01-15 19:56:47 +0100 | [diff] [blame] | 300 | tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | |
| 301 | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
Bjorn Helgaas | 8bad7f2 | 2016-10-11 22:09:32 -0500 | [diff] [blame] | 302 | pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp); |
Lucas Stach | 53eeb48 | 2016-01-15 19:56:47 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 305 | #ifdef CONFIG_ARM |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 306 | /* Added for PCI abort handling */ |
| 307 | static int imx6q_pcie_abort_handler(unsigned long addr, |
| 308 | unsigned int fsr, struct pt_regs *regs) |
| 309 | { |
Lucas Stach | 415b618 | 2017-05-22 17:06:30 -0500 | [diff] [blame] | 310 | unsigned long pc = instruction_pointer(regs); |
| 311 | unsigned long instr = *(unsigned long *)pc; |
| 312 | int reg = (instr >> 12) & 15; |
| 313 | |
| 314 | /* |
| 315 | * If the instruction being executed was a read, |
| 316 | * make it look like it read all-ones. |
| 317 | */ |
| 318 | if ((instr & 0x0c100000) == 0x04100000) { |
| 319 | unsigned long val; |
| 320 | |
| 321 | if (instr & 0x00400000) |
| 322 | val = 255; |
| 323 | else |
| 324 | val = -1; |
| 325 | |
| 326 | regs->uregs[reg] = val; |
| 327 | regs->ARM_pc += 4; |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | if ((instr & 0x0e100090) == 0x00100090) { |
| 332 | regs->uregs[reg] = -1; |
| 333 | regs->ARM_pc += 4; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | return 1; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 338 | } |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 339 | #endif |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 340 | |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 341 | static int imx6_pcie_attach_pd(struct device *dev) |
| 342 | { |
| 343 | struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev); |
| 344 | struct device_link *link; |
| 345 | |
| 346 | /* Do nothing when in a single power domain */ |
| 347 | if (dev->pm_domain) |
| 348 | return 0; |
| 349 | |
| 350 | imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie"); |
| 351 | if (IS_ERR(imx6_pcie->pd_pcie)) |
| 352 | return PTR_ERR(imx6_pcie->pd_pcie); |
Leonard Crestez | a6093ad | 2019-01-31 14:59:50 -0600 | [diff] [blame] | 353 | /* Do nothing when power domain missing */ |
| 354 | if (!imx6_pcie->pd_pcie) |
| 355 | return 0; |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 356 | link = device_link_add(dev, imx6_pcie->pd_pcie, |
| 357 | DL_FLAG_STATELESS | |
| 358 | DL_FLAG_PM_RUNTIME | |
| 359 | DL_FLAG_RPM_ACTIVE); |
| 360 | if (!link) { |
| 361 | dev_err(dev, "Failed to add device_link to pcie pd.\n"); |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | |
| 365 | imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy"); |
| 366 | if (IS_ERR(imx6_pcie->pd_pcie_phy)) |
| 367 | return PTR_ERR(imx6_pcie->pd_pcie_phy); |
| 368 | |
Leonard Crestez | a4ace4f | 2019-01-31 14:59:56 -0600 | [diff] [blame] | 369 | link = device_link_add(dev, imx6_pcie->pd_pcie_phy, |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 370 | DL_FLAG_STATELESS | |
| 371 | DL_FLAG_PM_RUNTIME | |
| 372 | DL_FLAG_RPM_ACTIVE); |
Leonard Crestez | a4ace4f | 2019-01-31 14:59:56 -0600 | [diff] [blame] | 373 | if (!link) { |
| 374 | dev_err(dev, "Failed to add device_link to pcie_phy pd.\n"); |
| 375 | return -EINVAL; |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
Bjorn Helgaas | 9ab021b | 2016-10-06 13:35:17 -0500 | [diff] [blame] | 381 | static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 382 | { |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 383 | struct device *dev = imx6_pcie->pci->dev; |
| 384 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 385 | switch (imx6_pcie->drvdata->variant) { |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 386 | case IMX7D: |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 387 | case IMX8MQ: |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 388 | reset_control_assert(imx6_pcie->pciephy_reset); |
| 389 | reset_control_assert(imx6_pcie->apps_reset); |
| 390 | break; |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 391 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 392 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 393 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN, |
| 394 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN); |
| 395 | /* Force PCIe PHY reset */ |
| 396 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5, |
| 397 | IMX6SX_GPR5_PCIE_BTNRST_RESET, |
| 398 | IMX6SX_GPR5_PCIE_BTNRST_RESET); |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 399 | break; |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 400 | case IMX6QP: |
| 401 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 402 | IMX6Q_GPR1_PCIE_SW_RST, |
| 403 | IMX6Q_GPR1_PCIE_SW_RST); |
| 404 | break; |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 405 | case IMX6Q: |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 406 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 407 | IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18); |
| 408 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 409 | IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16); |
| 410 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 411 | } |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 412 | |
| 413 | if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) { |
| 414 | int ret = regulator_disable(imx6_pcie->vpcie); |
| 415 | |
| 416 | if (ret) |
| 417 | dev_err(dev, "failed to disable vpcie regulator: %d\n", |
| 418 | ret); |
| 419 | } |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 420 | } |
| 421 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 422 | static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie) |
| 423 | { |
| 424 | WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ); |
| 425 | return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14; |
| 426 | } |
| 427 | |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 428 | static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie) |
| 429 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 430 | struct dw_pcie *pci = imx6_pcie->pci; |
| 431 | struct device *dev = pci->dev; |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 432 | unsigned int offset; |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 433 | int ret = 0; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 434 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 435 | switch (imx6_pcie->drvdata->variant) { |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 436 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 437 | ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi); |
| 438 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 439 | dev_err(dev, "unable to enable pcie_axi clock\n"); |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 440 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 444 | IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0); |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 445 | break; |
Fabio Estevam | c27fd68 | 2018-05-09 14:01:48 -0300 | [diff] [blame] | 446 | case IMX6QP: /* FALLTHROUGH */ |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 447 | case IMX6Q: |
| 448 | /* power up core phy and enable ref clock */ |
| 449 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 450 | IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18); |
| 451 | /* |
| 452 | * the async reset input need ref clock to sync internally, |
| 453 | * when the ref clock comes after reset, internal synced |
| 454 | * reset time is too short, cannot meet the requirement. |
| 455 | * add one ~10us delay here. |
| 456 | */ |
| 457 | udelay(10); |
| 458 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 459 | IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16); |
| 460 | break; |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 461 | case IMX7D: |
| 462 | break; |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 463 | case IMX8MQ: |
Andrey Smirnov | 5278f65 | 2019-02-11 17:51:08 -0800 | [diff] [blame] | 464 | ret = clk_prepare_enable(imx6_pcie->pcie_aux); |
| 465 | if (ret) { |
| 466 | dev_err(dev, "unable to enable pcie_aux clock\n"); |
| 467 | break; |
| 468 | } |
| 469 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 470 | offset = imx6_pcie_grp_offset(imx6_pcie); |
| 471 | /* |
| 472 | * Set the over ride low and enabled |
| 473 | * make sure that REF_CLK is turned on. |
| 474 | */ |
| 475 | regmap_update_bits(imx6_pcie->iomuxc_gpr, offset, |
| 476 | IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE, |
| 477 | 0); |
| 478 | regmap_update_bits(imx6_pcie->iomuxc_gpr, offset, |
| 479 | IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN, |
| 480 | IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN); |
| 481 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Andrey Smirnov | e6f1fef | 2016-05-02 14:08:21 -0500 | [diff] [blame] | 484 | return ret; |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 487 | static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie) |
| 488 | { |
| 489 | u32 val; |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 490 | struct device *dev = imx6_pcie->pci->dev; |
| 491 | |
Andrey Smirnov | 9e303be | 2019-04-14 17:46:22 -0700 | [diff] [blame] | 492 | if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr, |
| 493 | IOMUXC_GPR22, val, |
| 494 | val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED, |
| 495 | PHY_PLL_LOCK_WAIT_USLEEP_MAX, |
| 496 | PHY_PLL_LOCK_WAIT_TIMEOUT)) |
| 497 | dev_err(dev, "PCIe PLL lock timeout\n"); |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Bjorn Helgaas | 9ab021b | 2016-10-06 13:35:17 -0500 | [diff] [blame] | 500 | static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 501 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 502 | struct dw_pcie *pci = imx6_pcie->pci; |
| 503 | struct device *dev = pci->dev; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 504 | int ret; |
| 505 | |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 506 | if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) { |
| 507 | ret = regulator_enable(imx6_pcie->vpcie); |
| 508 | if (ret) { |
| 509 | dev_err(dev, "failed to enable vpcie regulator: %d\n", |
| 510 | ret); |
| 511 | return; |
| 512 | } |
| 513 | } |
| 514 | |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 515 | ret = clk_prepare_enable(imx6_pcie->pcie_phy); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 516 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 517 | dev_err(dev, "unable to enable pcie_phy clock\n"); |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 518 | goto err_pcie_phy; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 519 | } |
| 520 | |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 521 | ret = clk_prepare_enable(imx6_pcie->pcie_bus); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 522 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 523 | dev_err(dev, "unable to enable pcie_bus clock\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 524 | goto err_pcie_bus; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 525 | } |
| 526 | |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 527 | ret = clk_prepare_enable(imx6_pcie->pcie); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 528 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 529 | dev_err(dev, "unable to enable pcie clock\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 530 | goto err_pcie; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 531 | } |
| 532 | |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 533 | ret = imx6_pcie_enable_ref_clk(imx6_pcie); |
| 534 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 535 | dev_err(dev, "unable to enable pcie ref clock\n"); |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 536 | goto err_ref_clk; |
| 537 | } |
Tim Harvey | 3fce0e8 | 2014-08-07 23:36:40 -0700 | [diff] [blame] | 538 | |
Richard Zhu | a2fa6f6 | 2014-10-27 13:17:32 +0800 | [diff] [blame] | 539 | /* allow the clocks to stabilize */ |
| 540 | usleep_range(200, 500); |
| 541 | |
Richard Zhu | bc9ef77 | 2013-12-12 22:50:03 +0100 | [diff] [blame] | 542 | /* Some boards don't have PCIe reset GPIO. */ |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 543 | if (gpio_is_valid(imx6_pcie->reset_gpio)) { |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 544 | gpio_set_value_cansleep(imx6_pcie->reset_gpio, |
| 545 | imx6_pcie->gpio_active_high); |
Richard Zhu | bc9ef77 | 2013-12-12 22:50:03 +0100 | [diff] [blame] | 546 | msleep(100); |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 547 | gpio_set_value_cansleep(imx6_pcie->reset_gpio, |
| 548 | !imx6_pcie->gpio_active_high); |
Richard Zhu | bc9ef77 | 2013-12-12 22:50:03 +0100 | [diff] [blame] | 549 | } |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 550 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 551 | switch (imx6_pcie->drvdata->variant) { |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 552 | case IMX8MQ: |
| 553 | reset_control_deassert(imx6_pcie->pciephy_reset); |
| 554 | break; |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 555 | case IMX7D: |
| 556 | reset_control_deassert(imx6_pcie->pciephy_reset); |
Trent Piepho | 1df82ec | 2019-02-05 00:17:41 +0000 | [diff] [blame] | 557 | |
| 558 | /* Workaround for ERR010728, failure of PCI-e PLL VCO to |
| 559 | * oscillate, especially when cold. This turns off "Duty-cycle |
| 560 | * Corrector" and other mysterious undocumented things. |
| 561 | */ |
| 562 | if (likely(imx6_pcie->phy_base)) { |
| 563 | /* De-assert DCC_FB_EN */ |
| 564 | writel(PCIE_PHY_CMN_REG4_DCC_FB_EN, |
| 565 | imx6_pcie->phy_base + PCIE_PHY_CMN_REG4); |
| 566 | /* Assert RX_EQS and RX_EQS_SEL */ |
| 567 | writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL |
| 568 | | PCIE_PHY_CMN_REG24_RX_EQ, |
| 569 | imx6_pcie->phy_base + PCIE_PHY_CMN_REG24); |
| 570 | /* Assert ATT_MODE */ |
| 571 | writel(PCIE_PHY_CMN_REG26_ATT_MODE, |
| 572 | imx6_pcie->phy_base + PCIE_PHY_CMN_REG26); |
| 573 | } else { |
| 574 | dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n"); |
| 575 | } |
| 576 | |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 577 | imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie); |
| 578 | break; |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 579 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 580 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5, |
| 581 | IMX6SX_GPR5_PCIE_BTNRST_RESET, 0); |
Andrey Smirnov | 4d31c61 | 2016-05-02 14:09:10 -0500 | [diff] [blame] | 582 | break; |
| 583 | case IMX6QP: |
| 584 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, |
| 585 | IMX6Q_GPR1_PCIE_SW_RST, 0); |
| 586 | |
| 587 | usleep_range(200, 500); |
| 588 | break; |
| 589 | case IMX6Q: /* Nothing to do */ |
| 590 | break; |
| 591 | } |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 592 | |
Bjorn Helgaas | 9ab021b | 2016-10-06 13:35:17 -0500 | [diff] [blame] | 593 | return; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 594 | |
Bjorn Helgaas | 4d1821e | 2016-03-14 00:30:55 +0100 | [diff] [blame] | 595 | err_ref_clk: |
| 596 | clk_disable_unprepare(imx6_pcie->pcie); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 597 | err_pcie: |
| 598 | clk_disable_unprepare(imx6_pcie->pcie_bus); |
| 599 | err_pcie_bus: |
| 600 | clk_disable_unprepare(imx6_pcie->pcie_phy); |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 601 | err_pcie_phy: |
| 602 | if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) { |
| 603 | ret = regulator_disable(imx6_pcie->vpcie); |
| 604 | if (ret) |
| 605 | dev_err(dev, "failed to disable vpcie regulator: %d\n", |
| 606 | ret); |
| 607 | } |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 608 | } |
| 609 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 610 | static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie) |
| 611 | { |
| 612 | unsigned int mask, val; |
| 613 | |
| 614 | if (imx6_pcie->drvdata->variant == IMX8MQ && |
| 615 | imx6_pcie->controller_id == 1) { |
| 616 | mask = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE; |
| 617 | val = FIELD_PREP(IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE, |
| 618 | PCI_EXP_TYPE_ROOT_PORT); |
| 619 | } else { |
| 620 | mask = IMX6Q_GPR12_DEVICE_TYPE; |
| 621 | val = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE, |
| 622 | PCI_EXP_TYPE_ROOT_PORT); |
| 623 | } |
| 624 | |
| 625 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, mask, val); |
| 626 | } |
| 627 | |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 628 | static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 629 | { |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 630 | switch (imx6_pcie->drvdata->variant) { |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 631 | case IMX8MQ: |
| 632 | /* |
| 633 | * TODO: Currently this code assumes external |
| 634 | * oscillator is being used |
| 635 | */ |
| 636 | regmap_update_bits(imx6_pcie->iomuxc_gpr, |
| 637 | imx6_pcie_grp_offset(imx6_pcie), |
| 638 | IMX8MQ_GPR_PCIE_REF_USE_PAD, |
| 639 | IMX8MQ_GPR_PCIE_REF_USE_PAD); |
| 640 | break; |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 641 | case IMX7D: |
| 642 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 643 | IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0); |
| 644 | break; |
| 645 | case IMX6SX: |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 646 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 647 | IMX6SX_GPR12_PCIE_RX_EQ_MASK, |
| 648 | IMX6SX_GPR12_PCIE_RX_EQ_2); |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 649 | /* FALLTHROUGH */ |
| 650 | default: |
| 651 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 652 | IMX6Q_GPR12_PCIE_CTL_2, 0 << 10); |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 653 | |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 654 | /* configure constant input signal to the pcie ctrl and phy */ |
| 655 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 656 | IMX6Q_GPR12_LOS_LEVEL, 9 << 4); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 657 | |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 658 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 659 | IMX6Q_GPR8_TX_DEEMPH_GEN1, |
| 660 | imx6_pcie->tx_deemph_gen1 << 0); |
| 661 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 662 | IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, |
| 663 | imx6_pcie->tx_deemph_gen2_3p5db << 6); |
| 664 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 665 | IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, |
| 666 | imx6_pcie->tx_deemph_gen2_6db << 12); |
| 667 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 668 | IMX6Q_GPR8_TX_SWING_FULL, |
| 669 | imx6_pcie->tx_swing_full << 18); |
| 670 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8, |
| 671 | IMX6Q_GPR8_TX_SWING_LOW, |
| 672 | imx6_pcie->tx_swing_low << 25); |
| 673 | break; |
| 674 | } |
| 675 | |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 676 | imx6_pcie_configure_type(imx6_pcie); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Lucas Stach | f18f42d | 2018-07-31 12:21:49 +0200 | [diff] [blame] | 679 | static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie) |
| 680 | { |
| 681 | unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy); |
| 682 | int mult, div; |
| 683 | u32 val; |
| 684 | |
Andrey Smirnov | 2f532d07 | 2019-02-01 16:15:21 -0800 | [diff] [blame] | 685 | if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY)) |
| 686 | return 0; |
| 687 | |
Lucas Stach | f18f42d | 2018-07-31 12:21:49 +0200 | [diff] [blame] | 688 | switch (phy_rate) { |
| 689 | case 125000000: |
| 690 | /* |
| 691 | * The default settings of the MPLL are for a 125MHz input |
| 692 | * clock, so no need to reconfigure anything in that case. |
| 693 | */ |
| 694 | return 0; |
| 695 | case 100000000: |
| 696 | mult = 25; |
| 697 | div = 0; |
| 698 | break; |
| 699 | case 200000000: |
| 700 | mult = 25; |
| 701 | div = 1; |
| 702 | break; |
| 703 | default: |
| 704 | dev_err(imx6_pcie->pci->dev, |
| 705 | "Unsupported PHY reference clock rate %lu\n", phy_rate); |
| 706 | return -EINVAL; |
| 707 | } |
| 708 | |
| 709 | pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val); |
| 710 | val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK << |
| 711 | PCIE_PHY_MPLL_MULTIPLIER_SHIFT); |
| 712 | val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT; |
| 713 | val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD; |
| 714 | pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val); |
| 715 | |
| 716 | pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val); |
| 717 | val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK << |
| 718 | PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT); |
| 719 | val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT; |
| 720 | val |= PCIE_PHY_ATEOVRD_EN; |
| 721 | pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 726 | static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie) |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 727 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 728 | struct dw_pcie *pci = imx6_pcie->pci; |
| 729 | struct device *dev = pci->dev; |
Bjorn Helgaas | 1c7fae1 | 2015-06-12 15:02:49 -0500 | [diff] [blame] | 730 | u32 tmp; |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 731 | unsigned int retries; |
| 732 | |
| 733 | for (retries = 0; retries < 200; retries++) { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 734 | tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 735 | /* Test if the speed change finished. */ |
| 736 | if (!(tmp & PORT_LOGIC_SPEED_CHANGE)) |
| 737 | return 0; |
| 738 | usleep_range(100, 1000); |
| 739 | } |
| 740 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 741 | dev_err(dev, "Speed change timeout\n"); |
Andrey Smirnov | c377690 | 2019-04-14 17:46:24 -0700 | [diff] [blame^] | 742 | return -ETIMEDOUT; |
Marek Vasut | 66a60f9 | 2013-12-12 22:50:01 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 745 | static void imx6_pcie_ltssm_enable(struct device *dev) |
| 746 | { |
| 747 | struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev); |
| 748 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 749 | switch (imx6_pcie->drvdata->variant) { |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 750 | case IMX6Q: |
| 751 | case IMX6SX: |
| 752 | case IMX6QP: |
| 753 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 754 | IMX6Q_GPR12_PCIE_CTL_2, |
| 755 | IMX6Q_GPR12_PCIE_CTL_2); |
| 756 | break; |
| 757 | case IMX7D: |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 758 | case IMX8MQ: |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 759 | reset_control_deassert(imx6_pcie->apps_reset); |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 764 | static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie) |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 765 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 766 | struct dw_pcie *pci = imx6_pcie->pci; |
| 767 | struct device *dev = pci->dev; |
Bjorn Helgaas | 1c7fae1 | 2015-06-12 15:02:49 -0500 | [diff] [blame] | 768 | u32 tmp; |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 769 | int ret; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * Force Gen1 operation when starting the link. In case the link is |
| 773 | * started in Gen2 mode, there is a possibility the devices on the |
| 774 | * bus will not be detected at all. This happens with PCIe switches. |
| 775 | */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 776 | tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR); |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 777 | tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK; |
| 778 | tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 779 | dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp); |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 780 | |
| 781 | /* Start LTSSM. */ |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 782 | imx6_pcie_ltssm_enable(dev); |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 783 | |
Andrey Smirnov | ee6f371 | 2019-04-14 17:46:23 -0700 | [diff] [blame] | 784 | ret = dw_pcie_wait_for_link(pci); |
Fabio Estevam | caf3f56 | 2016-12-27 12:40:43 -0200 | [diff] [blame] | 785 | if (ret) |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 786 | goto err_reset_phy; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 787 | |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 788 | if (imx6_pcie->link_gen == 2) { |
| 789 | /* Allow Gen2 mode after the link is up. */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 790 | tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR); |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 791 | tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK; |
| 792 | tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 793 | dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp); |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 794 | |
Andrey Smirnov | e6dcd87 | 2017-03-28 08:42:51 -0700 | [diff] [blame] | 795 | /* |
Andrey Smirnov | 93b226f | 2017-03-28 08:42:52 -0700 | [diff] [blame] | 796 | * Start Directed Speed Change so the best possible |
| 797 | * speed both link partners support can be negotiated. |
Andrey Smirnov | e6dcd87 | 2017-03-28 08:42:51 -0700 | [diff] [blame] | 798 | */ |
Andrey Smirnov | 93b226f | 2017-03-28 08:42:52 -0700 | [diff] [blame] | 799 | tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); |
| 800 | tmp |= PORT_LOGIC_SPEED_CHANGE; |
| 801 | dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp); |
Andrey Smirnov | e6dcd87 | 2017-03-28 08:42:51 -0700 | [diff] [blame] | 802 | |
Andrey Smirnov | 4c458bb | 2019-02-01 16:15:22 -0800 | [diff] [blame] | 803 | if (imx6_pcie->drvdata->flags & |
| 804 | IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) { |
Andrey Smirnov | 93b226f | 2017-03-28 08:42:52 -0700 | [diff] [blame] | 805 | /* |
| 806 | * On i.MX7, DIRECT_SPEED_CHANGE behaves differently |
| 807 | * from i.MX6 family when no link speed transition |
| 808 | * occurs and we go Gen1 -> yep, Gen1. The difference |
| 809 | * is that, in such case, it will not be cleared by HW |
| 810 | * which will cause the following code to report false |
| 811 | * failure. |
| 812 | */ |
| 813 | |
| 814 | ret = imx6_pcie_wait_for_speed_change(imx6_pcie); |
| 815 | if (ret) { |
| 816 | dev_err(dev, "Failed to bring link up!\n"); |
| 817 | goto err_reset_phy; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /* Make sure link training is finished as well! */ |
Andrey Smirnov | ee6f371 | 2019-04-14 17:46:23 -0700 | [diff] [blame] | 822 | ret = dw_pcie_wait_for_link(pci); |
Andrey Smirnov | e6dcd87 | 2017-03-28 08:42:51 -0700 | [diff] [blame] | 823 | if (ret) { |
| 824 | dev_err(dev, "Failed to bring link up!\n"); |
| 825 | goto err_reset_phy; |
| 826 | } |
Andrey Smirnov | 93b226f | 2017-03-28 08:42:52 -0700 | [diff] [blame] | 827 | } else { |
| 828 | dev_info(dev, "Link: Gen2 disabled\n"); |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 831 | tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR); |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 832 | dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf); |
Troy Kisky | a042746 | 2015-06-12 14:30:16 -0500 | [diff] [blame] | 833 | return 0; |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 834 | |
| 835 | err_reset_phy: |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 836 | dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n", |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 837 | dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0), |
| 838 | dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1)); |
Bjorn Helgaas | 2a6a85d | 2016-10-11 22:18:26 -0500 | [diff] [blame] | 839 | imx6_pcie_reset_phy(imx6_pcie); |
Lucas Stach | 54a47a8 | 2016-01-25 16:49:53 -0600 | [diff] [blame] | 840 | return ret; |
Marek Vasut | fa33a6d | 2013-12-12 22:50:02 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 843 | static int imx6_pcie_host_init(struct pcie_port *pp) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 844 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 845 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 846 | struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 847 | |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 848 | imx6_pcie_assert_core_reset(imx6_pcie); |
| 849 | imx6_pcie_init_phy(imx6_pcie); |
| 850 | imx6_pcie_deassert_core_reset(imx6_pcie); |
Lucas Stach | f18f42d | 2018-07-31 12:21:49 +0200 | [diff] [blame] | 851 | imx6_setup_phy_mpll(imx6_pcie); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 852 | dw_pcie_setup_rc(pp); |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 853 | imx6_pcie_establish_link(imx6_pcie); |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 854 | |
| 855 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 856 | dw_pcie_msi_init(pp); |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 857 | |
| 858 | return 0; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 859 | } |
| 860 | |
Jisheng Zhang | 4ab2e7c | 2017-06-05 16:53:46 +0800 | [diff] [blame] | 861 | static const struct dw_pcie_host_ops imx6_pcie_host_ops = { |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 862 | .host_init = imx6_pcie_host_init, |
| 863 | }; |
| 864 | |
Andrey Smirnov | bde4a5a | 2017-03-28 08:42:50 -0700 | [diff] [blame] | 865 | static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie, |
| 866 | struct platform_device *pdev) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 867 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 868 | struct dw_pcie *pci = imx6_pcie->pci; |
| 869 | struct pcie_port *pp = &pci->pp; |
| 870 | struct device *dev = &pdev->dev; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 871 | int ret; |
| 872 | |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 873 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 874 | pp->msi_irq = platform_get_irq_byname(pdev, "msi"); |
| 875 | if (pp->msi_irq <= 0) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 876 | dev_err(dev, "failed to get MSI irq\n"); |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 877 | return -ENODEV; |
| 878 | } |
Lucas Stach | d1dc974 | 2014-03-28 17:52:59 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 881 | pp->ops = &imx6_pcie_host_ops; |
| 882 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 883 | ret = dw_pcie_host_init(pp); |
| 884 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 885 | dev_err(dev, "failed to initialize host\n"); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 892 | static const struct dw_pcie_ops dw_pcie_ops = { |
Trent Piepho | 68bc10b | 2018-11-05 18:11:36 +0000 | [diff] [blame] | 893 | /* No special ops needed, but pcie-designware still expects this struct */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 894 | }; |
| 895 | |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 896 | #ifdef CONFIG_PM_SLEEP |
| 897 | static void imx6_pcie_ltssm_disable(struct device *dev) |
| 898 | { |
| 899 | struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev); |
| 900 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 901 | switch (imx6_pcie->drvdata->variant) { |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 902 | case IMX6SX: |
| 903 | case IMX6QP: |
| 904 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 905 | IMX6Q_GPR12_PCIE_CTL_2, 0); |
| 906 | break; |
| 907 | case IMX7D: |
| 908 | reset_control_assert(imx6_pcie->apps_reset); |
| 909 | break; |
| 910 | default: |
| 911 | dev_err(dev, "ltssm_disable not supported\n"); |
| 912 | } |
| 913 | } |
| 914 | |
Leonard Crestez | f4e833b | 2018-07-19 17:02:10 +0300 | [diff] [blame] | 915 | static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie) |
| 916 | { |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 917 | struct device *dev = imx6_pcie->pci->dev; |
| 918 | |
| 919 | /* Some variants have a turnoff reset in DT */ |
| 920 | if (imx6_pcie->turnoff_reset) { |
| 921 | reset_control_assert(imx6_pcie->turnoff_reset); |
| 922 | reset_control_deassert(imx6_pcie->turnoff_reset); |
| 923 | goto pm_turnoff_sleep; |
| 924 | } |
| 925 | |
| 926 | /* Others poke directly at IOMUXC registers */ |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 927 | switch (imx6_pcie->drvdata->variant) { |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 928 | case IMX6SX: |
| 929 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 930 | IMX6SX_GPR12_PCIE_PM_TURN_OFF, |
| 931 | IMX6SX_GPR12_PCIE_PM_TURN_OFF); |
| 932 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 933 | IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0); |
| 934 | break; |
| 935 | default: |
| 936 | dev_err(dev, "PME_Turn_Off not implemented\n"); |
| 937 | return; |
| 938 | } |
Leonard Crestez | f4e833b | 2018-07-19 17:02:10 +0300 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * Components with an upstream port must respond to |
| 942 | * PME_Turn_Off with PME_TO_Ack but we can't check. |
| 943 | * |
| 944 | * The standard recommends a 1-10ms timeout after which to |
| 945 | * proceed anyway as if acks were received. |
| 946 | */ |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 947 | pm_turnoff_sleep: |
Leonard Crestez | f4e833b | 2018-07-19 17:02:10 +0300 | [diff] [blame] | 948 | usleep_range(1000, 10000); |
| 949 | } |
| 950 | |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 951 | static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie) |
| 952 | { |
| 953 | clk_disable_unprepare(imx6_pcie->pcie); |
| 954 | clk_disable_unprepare(imx6_pcie->pcie_phy); |
| 955 | clk_disable_unprepare(imx6_pcie->pcie_bus); |
| 956 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 957 | switch (imx6_pcie->drvdata->variant) { |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 958 | case IMX6SX: |
| 959 | clk_disable_unprepare(imx6_pcie->pcie_inbound_axi); |
| 960 | break; |
| 961 | case IMX7D: |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 962 | regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, |
| 963 | IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, |
| 964 | IMX7D_GPR12_PCIE_PHY_REFCLK_SEL); |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 965 | break; |
Andrey Smirnov | 5278f65 | 2019-02-11 17:51:08 -0800 | [diff] [blame] | 966 | case IMX8MQ: |
| 967 | clk_disable_unprepare(imx6_pcie->pcie_aux); |
| 968 | break; |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 969 | default: |
| 970 | break; |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 974 | static inline bool imx6_pcie_supports_suspend(struct imx6_pcie *imx6_pcie) |
| 975 | { |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 976 | return (imx6_pcie->drvdata->variant == IMX7D || |
| 977 | imx6_pcie->drvdata->variant == IMX6SX); |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 980 | static int imx6_pcie_suspend_noirq(struct device *dev) |
| 981 | { |
| 982 | struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev); |
| 983 | |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 984 | if (!imx6_pcie_supports_suspend(imx6_pcie)) |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 985 | return 0; |
| 986 | |
Leonard Crestez | f4e833b | 2018-07-19 17:02:10 +0300 | [diff] [blame] | 987 | imx6_pcie_pm_turnoff(imx6_pcie); |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 988 | imx6_pcie_clk_disable(imx6_pcie); |
| 989 | imx6_pcie_ltssm_disable(dev); |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static int imx6_pcie_resume_noirq(struct device *dev) |
| 995 | { |
| 996 | int ret; |
| 997 | struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev); |
| 998 | struct pcie_port *pp = &imx6_pcie->pci->pp; |
| 999 | |
Leonard Crestez | 9e56f0d | 2018-11-07 13:57:03 +0000 | [diff] [blame] | 1000 | if (!imx6_pcie_supports_suspend(imx6_pcie)) |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 1001 | return 0; |
| 1002 | |
| 1003 | imx6_pcie_assert_core_reset(imx6_pcie); |
| 1004 | imx6_pcie_init_phy(imx6_pcie); |
| 1005 | imx6_pcie_deassert_core_reset(imx6_pcie); |
| 1006 | dw_pcie_setup_rc(pp); |
| 1007 | |
| 1008 | ret = imx6_pcie_establish_link(imx6_pcie); |
| 1009 | if (ret < 0) |
| 1010 | dev_info(dev, "pcie link is down after resume.\n"); |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | #endif |
| 1015 | |
| 1016 | static const struct dev_pm_ops imx6_pcie_pm_ops = { |
| 1017 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq, |
| 1018 | imx6_pcie_resume_noirq) |
| 1019 | }; |
| 1020 | |
Andrey Smirnov | bde4a5a | 2017-03-28 08:42:50 -0700 | [diff] [blame] | 1021 | static int imx6_pcie_probe(struct platform_device *pdev) |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1022 | { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1023 | struct device *dev = &pdev->dev; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 1024 | struct dw_pcie *pci; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1025 | struct imx6_pcie *imx6_pcie; |
Trent Piepho | 1df82ec | 2019-02-05 00:17:41 +0000 | [diff] [blame] | 1026 | struct device_node *np; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1027 | struct resource *dbi_base; |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1028 | struct device_node *node = dev->of_node; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1029 | int ret; |
Richard Zhu | 75cb8d2 | 2018-12-21 04:33:38 +0000 | [diff] [blame] | 1030 | u16 val; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1031 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1032 | imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1033 | if (!imx6_pcie) |
| 1034 | return -ENOMEM; |
| 1035 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 1036 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 1037 | if (!pci) |
| 1038 | return -ENOMEM; |
| 1039 | |
| 1040 | pci->dev = dev; |
| 1041 | pci->ops = &dw_pcie_ops; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1042 | |
Guenter Roeck | c046406 | 2017-02-25 02:08:12 -0800 | [diff] [blame] | 1043 | imx6_pcie->pci = pci; |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1044 | imx6_pcie->drvdata = of_device_get_match_data(dev); |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 1045 | |
Trent Piepho | 1df82ec | 2019-02-05 00:17:41 +0000 | [diff] [blame] | 1046 | /* Find the PHY if one is defined, only imx7d uses it */ |
| 1047 | np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0); |
| 1048 | if (np) { |
| 1049 | struct resource res; |
| 1050 | |
| 1051 | ret = of_address_to_resource(np, 0, &res); |
| 1052 | if (ret) { |
| 1053 | dev_err(dev, "Unable to map PCIe PHY\n"); |
| 1054 | return ret; |
| 1055 | } |
| 1056 | imx6_pcie->phy_base = devm_ioremap_resource(dev, &res); |
| 1057 | if (IS_ERR(imx6_pcie->phy_base)) { |
| 1058 | dev_err(dev, "Unable to map PCIe PHY\n"); |
| 1059 | return PTR_ERR(imx6_pcie->phy_base); |
| 1060 | } |
| 1061 | } |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1062 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1063 | dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 1064 | pci->dbi_base = devm_ioremap_resource(dev, dbi_base); |
| 1065 | if (IS_ERR(pci->dbi_base)) |
| 1066 | return PTR_ERR(pci->dbi_base); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1067 | |
| 1068 | /* Fetch GPIOs */ |
Bjorn Helgaas | c5af407 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1069 | imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0); |
| 1070 | imx6_pcie->gpio_active_high = of_property_read_bool(node, |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 1071 | "reset-gpio-active-high"); |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 1072 | if (gpio_is_valid(imx6_pcie->reset_gpio)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1073 | ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio, |
Petr Štetiar | 3ea8529a | 2016-04-19 19:42:07 -0500 | [diff] [blame] | 1074 | imx6_pcie->gpio_active_high ? |
| 1075 | GPIOF_OUT_INIT_HIGH : |
| 1076 | GPIOF_OUT_INIT_LOW, |
| 1077 | "PCIe reset"); |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 1078 | if (ret) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1079 | dev_err(dev, "unable to get reset gpio\n"); |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
Andrey Smirnov | bde4a5a | 2017-03-28 08:42:50 -0700 | [diff] [blame] | 1082 | } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) { |
| 1083 | return imx6_pcie->reset_gpio; |
Fabio Estevam | b2d7a9c | 2016-03-28 18:45:36 -0300 | [diff] [blame] | 1084 | } |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1085 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1086 | /* Fetch clocks */ |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1087 | imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 1088 | if (IS_ERR(imx6_pcie->pcie_phy)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1089 | dev_err(dev, "pcie_phy clock source missing or invalid\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 1090 | return PTR_ERR(imx6_pcie->pcie_phy); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1093 | imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 1094 | if (IS_ERR(imx6_pcie->pcie_bus)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1095 | dev_err(dev, "pcie_bus clock source missing or invalid\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 1096 | return PTR_ERR(imx6_pcie->pcie_bus); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1099 | imx6_pcie->pcie = devm_clk_get(dev, "pcie"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 1100 | if (IS_ERR(imx6_pcie->pcie)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1101 | dev_err(dev, "pcie clock source missing or invalid\n"); |
Lucas Stach | 5752613 | 2014-03-28 17:52:55 +0100 | [diff] [blame] | 1102 | return PTR_ERR(imx6_pcie->pcie); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1105 | switch (imx6_pcie->drvdata->variant) { |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1106 | case IMX6SX: |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1107 | imx6_pcie->pcie_inbound_axi = devm_clk_get(dev, |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 1108 | "pcie_inbound_axi"); |
| 1109 | if (IS_ERR(imx6_pcie->pcie_inbound_axi)) { |
Andrey Smirnov | 21b7245 | 2017-02-07 07:50:25 -0800 | [diff] [blame] | 1110 | dev_err(dev, "pcie_inbound_axi clock missing or invalid\n"); |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 1111 | return PTR_ERR(imx6_pcie->pcie_inbound_axi); |
| 1112 | } |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1113 | break; |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 1114 | case IMX8MQ: |
Andrey Smirnov | 5278f65 | 2019-02-11 17:51:08 -0800 | [diff] [blame] | 1115 | imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux"); |
| 1116 | if (IS_ERR(imx6_pcie->pcie_aux)) { |
| 1117 | dev_err(dev, "pcie_aux clock source missing or invalid\n"); |
| 1118 | return PTR_ERR(imx6_pcie->pcie_aux); |
| 1119 | } |
| 1120 | /* fall through */ |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1121 | case IMX7D: |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 1122 | if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR) |
| 1123 | imx6_pcie->controller_id = 1; |
| 1124 | |
Philipp Zabel | 7c18058 | 2017-07-19 17:25:56 +0200 | [diff] [blame] | 1125 | imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev, |
| 1126 | "pciephy"); |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1127 | if (IS_ERR(imx6_pcie->pciephy_reset)) { |
Colin Ian King | 7221547 | 2017-04-21 08:02:30 +0100 | [diff] [blame] | 1128 | dev_err(dev, "Failed to get PCIEPHY reset control\n"); |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1129 | return PTR_ERR(imx6_pcie->pciephy_reset); |
| 1130 | } |
| 1131 | |
Philipp Zabel | 7c18058 | 2017-07-19 17:25:56 +0200 | [diff] [blame] | 1132 | imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev, |
| 1133 | "apps"); |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1134 | if (IS_ERR(imx6_pcie->apps_reset)) { |
Colin Ian King | 7221547 | 2017-04-21 08:02:30 +0100 | [diff] [blame] | 1135 | dev_err(dev, "Failed to get PCIE APPS reset control\n"); |
Andrey Smirnov | 9b3fe67 | 2017-03-28 08:42:49 -0700 | [diff] [blame] | 1136 | return PTR_ERR(imx6_pcie->apps_reset); |
| 1137 | } |
| 1138 | break; |
| 1139 | default: |
| 1140 | break; |
Christoph Fritz | e3c06cd | 2016-04-05 16:53:27 -0500 | [diff] [blame] | 1141 | } |
| 1142 | |
Leonard Crestez | f4e833b | 2018-07-19 17:02:10 +0300 | [diff] [blame] | 1143 | /* Grab turnoff reset */ |
| 1144 | imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff"); |
| 1145 | if (IS_ERR(imx6_pcie->turnoff_reset)) { |
| 1146 | dev_err(dev, "Failed to get TURNOFF reset control\n"); |
| 1147 | return PTR_ERR(imx6_pcie->turnoff_reset); |
| 1148 | } |
| 1149 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1150 | /* Grab GPR config register range */ |
| 1151 | imx6_pcie->iomuxc_gpr = |
| 1152 | syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr"); |
| 1153 | if (IS_ERR(imx6_pcie->iomuxc_gpr)) { |
Bjorn Helgaas | 1395765 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1154 | dev_err(dev, "unable to find iomuxc registers\n"); |
Fabio Estevam | b391bf3 | 2013-12-02 01:39:35 -0200 | [diff] [blame] | 1155 | return PTR_ERR(imx6_pcie->iomuxc_gpr); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Justin Waters | 28e3abe | 2016-01-15 10:24:35 -0500 | [diff] [blame] | 1158 | /* Grab PCIe PHY Tx Settings */ |
| 1159 | if (of_property_read_u32(node, "fsl,tx-deemph-gen1", |
| 1160 | &imx6_pcie->tx_deemph_gen1)) |
| 1161 | imx6_pcie->tx_deemph_gen1 = 0; |
| 1162 | |
| 1163 | if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db", |
| 1164 | &imx6_pcie->tx_deemph_gen2_3p5db)) |
| 1165 | imx6_pcie->tx_deemph_gen2_3p5db = 0; |
| 1166 | |
| 1167 | if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db", |
| 1168 | &imx6_pcie->tx_deemph_gen2_6db)) |
| 1169 | imx6_pcie->tx_deemph_gen2_6db = 20; |
| 1170 | |
| 1171 | if (of_property_read_u32(node, "fsl,tx-swing-full", |
| 1172 | &imx6_pcie->tx_swing_full)) |
| 1173 | imx6_pcie->tx_swing_full = 127; |
| 1174 | |
| 1175 | if (of_property_read_u32(node, "fsl,tx-swing-low", |
| 1176 | &imx6_pcie->tx_swing_low)) |
| 1177 | imx6_pcie->tx_swing_low = 127; |
| 1178 | |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 1179 | /* Limit link speed */ |
Bjorn Helgaas | c5af407 | 2016-10-06 13:35:18 -0500 | [diff] [blame] | 1180 | ret = of_property_read_u32(node, "fsl,max-link-speed", |
Tim Harvey | a5fcec4 | 2016-04-19 19:52:44 -0500 | [diff] [blame] | 1181 | &imx6_pcie->link_gen); |
| 1182 | if (ret) |
| 1183 | imx6_pcie->link_gen = 1; |
| 1184 | |
Quentin Schulz | c26ebe9 | 2017-06-08 10:07:42 +0200 | [diff] [blame] | 1185 | imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie"); |
| 1186 | if (IS_ERR(imx6_pcie->vpcie)) { |
| 1187 | if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER) |
| 1188 | return -EPROBE_DEFER; |
| 1189 | imx6_pcie->vpcie = NULL; |
| 1190 | } |
| 1191 | |
Kishon Vijay Abraham I | 9bcf0a6 | 2017-02-15 18:48:11 +0530 | [diff] [blame] | 1192 | platform_set_drvdata(pdev, imx6_pcie); |
| 1193 | |
Leonard Crestez | 3f7ccee | 2018-10-08 18:06:21 +0000 | [diff] [blame] | 1194 | ret = imx6_pcie_attach_pd(dev); |
| 1195 | if (ret) |
| 1196 | return ret; |
| 1197 | |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 1198 | ret = imx6_add_pcie_port(imx6_pcie, pdev); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1199 | if (ret < 0) |
Fabio Estevam | b391bf3 | 2013-12-02 01:39:35 -0200 | [diff] [blame] | 1200 | return ret; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1201 | |
Richard Zhu | 75cb8d2 | 2018-12-21 04:33:38 +0000 | [diff] [blame] | 1202 | if (pci_msi_enabled()) { |
| 1203 | val = dw_pcie_readw_dbi(pci, PCIE_RC_IMX6_MSI_CAP + |
| 1204 | PCI_MSI_FLAGS); |
| 1205 | val |= PCI_MSI_FLAGS_ENABLE; |
| 1206 | dw_pcie_writew_dbi(pci, PCIE_RC_IMX6_MSI_CAP + PCI_MSI_FLAGS, |
| 1207 | val); |
| 1208 | } |
| 1209 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1210 | return 0; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 1213 | static void imx6_pcie_shutdown(struct platform_device *pdev) |
| 1214 | { |
| 1215 | struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev); |
| 1216 | |
| 1217 | /* bring down link, so bootloader gets clean state in case of reboot */ |
Bjorn Helgaas | e7d7705 | 2016-10-11 22:06:47 -0500 | [diff] [blame] | 1218 | imx6_pcie_assert_core_reset(imx6_pcie); |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1221 | static const struct imx6_pcie_drvdata drvdata[] = { |
| 1222 | [IMX6Q] = { |
| 1223 | .variant = IMX6Q, |
Andrey Smirnov | 4c458bb | 2019-02-01 16:15:22 -0800 | [diff] [blame] | 1224 | .flags = IMX6_PCIE_FLAG_IMX6_PHY | |
| 1225 | IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE, |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1226 | }, |
| 1227 | [IMX6SX] = { |
| 1228 | .variant = IMX6SX, |
Andrey Smirnov | 4c458bb | 2019-02-01 16:15:22 -0800 | [diff] [blame] | 1229 | .flags = IMX6_PCIE_FLAG_IMX6_PHY | |
| 1230 | IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE, |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1231 | }, |
| 1232 | [IMX6QP] = { |
| 1233 | .variant = IMX6QP, |
Andrey Smirnov | 4c458bb | 2019-02-01 16:15:22 -0800 | [diff] [blame] | 1234 | .flags = IMX6_PCIE_FLAG_IMX6_PHY | |
| 1235 | IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE, |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1236 | }, |
| 1237 | [IMX7D] = { |
| 1238 | .variant = IMX7D, |
| 1239 | }, |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 1240 | [IMX8MQ] = { |
| 1241 | .variant = IMX8MQ, |
| 1242 | }, |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1243 | }; |
| 1244 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1245 | static const struct of_device_id imx6_pcie_of_match[] = { |
Andrey Smirnov | e8e4d4e | 2019-02-01 16:15:20 -0800 | [diff] [blame] | 1246 | { .compatible = "fsl,imx6q-pcie", .data = &drvdata[IMX6Q], }, |
| 1247 | { .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], }, |
| 1248 | { .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], }, |
| 1249 | { .compatible = "fsl,imx7d-pcie", .data = &drvdata[IMX7D], }, |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 1250 | { .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], } , |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1251 | {}, |
| 1252 | }; |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1253 | |
| 1254 | static struct platform_driver imx6_pcie_driver = { |
| 1255 | .driver = { |
| 1256 | .name = "imx6q-pcie", |
Sachin Kamat | 8bcadbe | 2013-10-21 14:36:41 +0530 | [diff] [blame] | 1257 | .of_match_table = imx6_pcie_of_match, |
Brian Norris | a5f40e8 | 2017-04-20 15:36:25 -0500 | [diff] [blame] | 1258 | .suppress_bind_attrs = true, |
Leonard Crestez | 0ee2c1f | 2018-08-27 14:28:37 +0300 | [diff] [blame] | 1259 | .pm = &imx6_pcie_pm_ops, |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1260 | }, |
Andrey Smirnov | bde4a5a | 2017-03-28 08:42:50 -0700 | [diff] [blame] | 1261 | .probe = imx6_pcie_probe, |
Lucas Stach | 3e3e406 | 2014-07-31 20:16:05 +0200 | [diff] [blame] | 1262 | .shutdown = imx6_pcie_shutdown, |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1263 | }; |
| 1264 | |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1265 | static int __init imx6_pcie_init(void) |
| 1266 | { |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 1267 | #ifdef CONFIG_ARM |
Andrey Smirnov | bde4a5a | 2017-03-28 08:42:50 -0700 | [diff] [blame] | 1268 | /* |
| 1269 | * Since probe() can be deferred we need to make sure that |
| 1270 | * hook_fault_code is not called after __init memory is freed |
| 1271 | * by kernel and since imx6q_pcie_abort_handler() is a no-op, |
| 1272 | * we can install the handler here without risking it |
| 1273 | * accessing some uninitialized driver state. |
| 1274 | */ |
Lucas Stach | 415b618 | 2017-05-22 17:06:30 -0500 | [diff] [blame] | 1275 | hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0, |
| 1276 | "external abort on non-linefetch"); |
Andrey Smirnov | 2d8ed46 | 2019-02-01 16:15:23 -0800 | [diff] [blame] | 1277 | #endif |
Andrey Smirnov | bde4a5a | 2017-03-28 08:42:50 -0700 | [diff] [blame] | 1278 | |
| 1279 | return platform_driver_register(&imx6_pcie_driver); |
Sean Cross | bb38919 | 2013-09-26 11:24:47 +0800 | [diff] [blame] | 1280 | } |
Paul Gortmaker | f90d8e8 | 2016-08-22 17:59:43 -0400 | [diff] [blame] | 1281 | device_initcall(imx6_pcie_init); |