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