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