Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Freescale i.MX6 PCI Express Root-Complex driver |
| 4 | * |
| 5 | * Copyright (C) 2013 Marek Vasut <marex@denx.de> |
| 6 | * |
| 7 | * Based on upstream Linux kernel driver: |
| 8 | * pci-imx6.c: Sean Cross <xobs@kosagi.com> |
| 9 | * pcie-designware.c: Jingoo Han <jg1.han@samsung.com> |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | 18afe10 | 2019-11-14 12:57:47 -0700 | [diff] [blame] | 13 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 16 | #include <pci.h> |
Tim Harvey | 541e273 | 2022-04-13 15:54:37 -0700 | [diff] [blame] | 17 | #include <power/regulator.h> |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 18 | #include <asm/arch/clock.h> |
| 19 | #include <asm/arch/iomux.h> |
| 20 | #include <asm/arch/crm_regs.h> |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 21 | #include <asm/gpio.h> |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 22 | #include <asm/io.h> |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 23 | #include <dm.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
Alexey Brodkin | 267d8e2 | 2014-02-26 17:47:58 +0400 | [diff] [blame] | 25 | #include <linux/sizes.h> |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 26 | #include <errno.h> |
Fabio Estevam | b5272bd | 2015-10-13 11:01:27 -0300 | [diff] [blame] | 27 | #include <asm/arch/sys_proto.h> |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 28 | |
| 29 | #define PCI_ACCESS_READ 0 |
| 30 | #define PCI_ACCESS_WRITE 1 |
| 31 | |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 32 | #ifdef CONFIG_MX6SX |
| 33 | #define MX6_DBI_ADDR 0x08ffc000 |
| 34 | #define MX6_IO_ADDR 0x08000000 |
| 35 | #define MX6_MEM_ADDR 0x08100000 |
| 36 | #define MX6_ROOT_ADDR 0x08f00000 |
| 37 | #else |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 38 | #define MX6_DBI_ADDR 0x01ffc000 |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 39 | #define MX6_IO_ADDR 0x01000000 |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 40 | #define MX6_MEM_ADDR 0x01100000 |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 41 | #define MX6_ROOT_ADDR 0x01f00000 |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 42 | #endif |
| 43 | #define MX6_DBI_SIZE 0x4000 |
| 44 | #define MX6_IO_SIZE 0x100000 |
| 45 | #define MX6_MEM_SIZE 0xe00000 |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 46 | #define MX6_ROOT_SIZE 0xfc000 |
| 47 | |
| 48 | /* PCIe Port Logic registers (memory-mapped) */ |
| 49 | #define PL_OFFSET 0x700 |
Tim Harvey | c22f2ea | 2017-05-12 12:58:41 -0700 | [diff] [blame] | 50 | #define PCIE_PL_PFLR (PL_OFFSET + 0x08) |
| 51 | #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16) |
| 52 | #define PCIE_PL_PFLR_FORCE_LINK (1 << 15) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 53 | #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28) |
| 54 | #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c) |
| 55 | #define PCIE_PHY_DEBUG_R1_LINK_UP (1 << 4) |
| 56 | #define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (1 << 29) |
| 57 | |
| 58 | #define PCIE_PHY_CTRL (PL_OFFSET + 0x114) |
| 59 | #define PCIE_PHY_CTRL_DATA_LOC 0 |
| 60 | #define PCIE_PHY_CTRL_CAP_ADR_LOC 16 |
| 61 | #define PCIE_PHY_CTRL_CAP_DAT_LOC 17 |
| 62 | #define PCIE_PHY_CTRL_WR_LOC 18 |
| 63 | #define PCIE_PHY_CTRL_RD_LOC 19 |
| 64 | |
| 65 | #define PCIE_PHY_STAT (PL_OFFSET + 0x110) |
| 66 | #define PCIE_PHY_STAT_DATA_LOC 0 |
| 67 | #define PCIE_PHY_STAT_ACK_LOC 16 |
| 68 | |
| 69 | /* PHY registers (not memory-mapped) */ |
| 70 | #define PCIE_PHY_RX_ASIC_OUT 0x100D |
| 71 | |
| 72 | #define PHY_RX_OVRD_IN_LO 0x1005 |
| 73 | #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5) |
| 74 | #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3) |
| 75 | |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 76 | #define PCIE_PHY_PUP_REQ (1 << 7) |
| 77 | |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 78 | /* iATU registers */ |
| 79 | #define PCIE_ATU_VIEWPORT 0x900 |
| 80 | #define PCIE_ATU_REGION_INBOUND (0x1 << 31) |
| 81 | #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31) |
| 82 | #define PCIE_ATU_REGION_INDEX1 (0x1 << 0) |
| 83 | #define PCIE_ATU_REGION_INDEX0 (0x0 << 0) |
| 84 | #define PCIE_ATU_CR1 0x904 |
| 85 | #define PCIE_ATU_TYPE_MEM (0x0 << 0) |
| 86 | #define PCIE_ATU_TYPE_IO (0x2 << 0) |
| 87 | #define PCIE_ATU_TYPE_CFG0 (0x4 << 0) |
| 88 | #define PCIE_ATU_TYPE_CFG1 (0x5 << 0) |
| 89 | #define PCIE_ATU_CR2 0x908 |
| 90 | #define PCIE_ATU_ENABLE (0x1 << 31) |
| 91 | #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30) |
| 92 | #define PCIE_ATU_LOWER_BASE 0x90C |
| 93 | #define PCIE_ATU_UPPER_BASE 0x910 |
| 94 | #define PCIE_ATU_LIMIT 0x914 |
| 95 | #define PCIE_ATU_LOWER_TARGET 0x918 |
| 96 | #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24) |
| 97 | #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19) |
| 98 | #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16) |
| 99 | #define PCIE_ATU_UPPER_TARGET 0x91C |
| 100 | |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 101 | struct imx_pcie_priv { |
| 102 | void __iomem *dbi_base; |
| 103 | void __iomem *cfg_base; |
Tim Harvey | 7367b39 | 2021-07-06 10:19:09 -0700 | [diff] [blame] | 104 | struct gpio_desc reset_gpio; |
| 105 | bool reset_active_high; |
Tim Harvey | 541e273 | 2022-04-13 15:54:37 -0700 | [diff] [blame] | 106 | struct udevice *vpcie; |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 109 | /* |
| 110 | * PHY access functions |
| 111 | */ |
| 112 | static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val) |
| 113 | { |
| 114 | u32 val; |
| 115 | u32 max_iterations = 10; |
| 116 | u32 wait_counter = 0; |
| 117 | |
| 118 | do { |
| 119 | val = readl(dbi_base + PCIE_PHY_STAT); |
| 120 | val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1; |
| 121 | wait_counter++; |
| 122 | |
| 123 | if (val == exp_val) |
| 124 | return 0; |
| 125 | |
| 126 | udelay(1); |
| 127 | } while (wait_counter < max_iterations); |
| 128 | |
| 129 | return -ETIMEDOUT; |
| 130 | } |
| 131 | |
| 132 | static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr) |
| 133 | { |
| 134 | u32 val; |
| 135 | int ret; |
| 136 | |
| 137 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
| 138 | writel(val, dbi_base + PCIE_PHY_CTRL); |
| 139 | |
| 140 | val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC); |
| 141 | writel(val, dbi_base + PCIE_PHY_CTRL); |
| 142 | |
| 143 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | val = addr << PCIE_PHY_CTRL_DATA_LOC; |
| 148 | writel(val, dbi_base + PCIE_PHY_CTRL); |
| 149 | |
| 150 | ret = pcie_phy_poll_ack(dbi_base, 0); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */ |
| 158 | static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data) |
| 159 | { |
| 160 | u32 val, phy_ctl; |
| 161 | int ret; |
| 162 | |
| 163 | ret = pcie_phy_wait_ack(dbi_base, addr); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | /* assert Read signal */ |
| 168 | phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC; |
| 169 | writel(phy_ctl, dbi_base + PCIE_PHY_CTRL); |
| 170 | |
| 171 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | val = readl(dbi_base + PCIE_PHY_STAT); |
| 176 | *data = val & 0xffff; |
| 177 | |
| 178 | /* deassert Read signal */ |
| 179 | writel(0x00, dbi_base + PCIE_PHY_CTRL); |
| 180 | |
| 181 | ret = pcie_phy_poll_ack(dbi_base, 0); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int pcie_phy_write(void __iomem *dbi_base, int addr, int data) |
| 189 | { |
| 190 | u32 var; |
| 191 | int ret; |
| 192 | |
| 193 | /* write addr */ |
| 194 | /* cap addr */ |
| 195 | ret = pcie_phy_wait_ack(dbi_base, addr); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 200 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 201 | |
| 202 | /* capture data */ |
| 203 | var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC); |
| 204 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 205 | |
| 206 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | |
| 210 | /* deassert cap data */ |
| 211 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 212 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 213 | |
| 214 | /* wait for ack de-assertion */ |
| 215 | ret = pcie_phy_poll_ack(dbi_base, 0); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | |
| 219 | /* assert wr signal */ |
| 220 | var = 0x1 << PCIE_PHY_CTRL_WR_LOC; |
| 221 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 222 | |
| 223 | /* wait for ack */ |
| 224 | ret = pcie_phy_poll_ack(dbi_base, 1); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | /* deassert wr signal */ |
| 229 | var = data << PCIE_PHY_CTRL_DATA_LOC; |
| 230 | writel(var, dbi_base + PCIE_PHY_CTRL); |
| 231 | |
| 232 | /* wait for ack de-assertion */ |
| 233 | ret = pcie_phy_poll_ack(dbi_base, 0); |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | |
| 237 | writel(0x0, dbi_base + PCIE_PHY_CTRL); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 242 | static int imx6_pcie_link_up(struct imx_pcie_priv *priv) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 243 | { |
| 244 | u32 rc, ltssm; |
| 245 | int rx_valid, temp; |
| 246 | |
| 247 | /* link is debug bit 36, debug register 1 starts at bit 32 */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 248 | rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 249 | if ((rc & PCIE_PHY_DEBUG_R1_LINK_UP) && |
| 250 | !(rc & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING)) |
| 251 | return -EAGAIN; |
| 252 | |
| 253 | /* |
| 254 | * From L0, initiate MAC entry to gen2 if EP/RC supports gen2. |
| 255 | * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2). |
| 256 | * If (MAC/LTSSM.state == Recovery.RcvrLock) |
| 257 | * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition |
| 258 | * to gen2 is stuck |
| 259 | */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 260 | pcie_phy_read(priv->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid); |
| 261 | ltssm = readl(priv->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 262 | |
| 263 | if (rx_valid & 0x01) |
| 264 | return 0; |
| 265 | |
| 266 | if (ltssm != 0x0d) |
| 267 | return 0; |
| 268 | |
| 269 | printf("transition to gen2 is stuck, reset PHY!\n"); |
| 270 | |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 271 | pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 272 | temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 273 | pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 274 | |
| 275 | udelay(3000); |
| 276 | |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 277 | pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 278 | temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN); |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 279 | pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * iATU region setup |
| 286 | */ |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 287 | static int imx_pcie_regions_setup(struct imx_pcie_priv *priv) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 288 | { |
| 289 | /* |
| 290 | * i.MX6 defines 16MB in the AXI address map for PCIe. |
| 291 | * |
| 292 | * That address space excepted the pcie registers is |
| 293 | * split and defined into different regions by iATU, |
| 294 | * with sizes and offsets as follows: |
| 295 | * |
| 296 | * 0x0100_0000 --- 0x010F_FFFF 1MB IORESOURCE_IO |
| 297 | * 0x0110_0000 --- 0x01EF_FFFF 14MB IORESOURCE_MEM |
| 298 | * 0x01F0_0000 --- 0x01FF_FFFF 1MB Cfg + Registers |
| 299 | */ |
| 300 | |
| 301 | /* CMD reg:I/O space, MEM space, and Bus Master Enable */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 302 | setbits_le32(priv->dbi_base + PCI_COMMAND, |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 303 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
| 304 | |
Pali Rohár | 25781e2 | 2022-02-18 13:18:40 +0100 | [diff] [blame] | 305 | /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 306 | setbits_le32(priv->dbi_base + PCI_CLASS_REVISION, |
Pali Rohár | 25781e2 | 2022-02-18 13:18:40 +0100 | [diff] [blame] | 307 | PCI_CLASS_BRIDGE_PCI_NORMAL << 8); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 308 | |
| 309 | /* Region #0 is used for Outbound CFG space access. */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 310 | writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 311 | |
Marek Vasut | 2c20b75 | 2019-06-09 03:50:53 +0200 | [diff] [blame] | 312 | writel(lower_32_bits((uintptr_t)priv->cfg_base), |
| 313 | priv->dbi_base + PCIE_ATU_LOWER_BASE); |
| 314 | writel(upper_32_bits((uintptr_t)priv->cfg_base), |
| 315 | priv->dbi_base + PCIE_ATU_UPPER_BASE); |
| 316 | writel(lower_32_bits((uintptr_t)priv->cfg_base + MX6_ROOT_SIZE), |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 317 | priv->dbi_base + PCIE_ATU_LIMIT); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 318 | |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 319 | writel(0, priv->dbi_base + PCIE_ATU_LOWER_TARGET); |
| 320 | writel(0, priv->dbi_base + PCIE_ATU_UPPER_TARGET); |
| 321 | writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1); |
| 322 | writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * PCI Express accessors |
| 329 | */ |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 330 | static void __iomem *get_bus_address(struct imx_pcie_priv *priv, |
| 331 | pci_dev_t d, int where) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 332 | { |
Marek Vasut | 2c20b75 | 2019-06-09 03:50:53 +0200 | [diff] [blame] | 333 | void __iomem *va_address; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 334 | |
| 335 | /* Reconfigure Region #0 */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 336 | writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 337 | |
| 338 | if (PCI_BUS(d) < 2) |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 339 | writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 340 | else |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 341 | writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 342 | |
| 343 | if (PCI_BUS(d) == 0) { |
Marek Vasut | 2c20b75 | 2019-06-09 03:50:53 +0200 | [diff] [blame] | 344 | va_address = priv->dbi_base; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 345 | } else { |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 346 | writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET); |
Marek Vasut | 2c20b75 | 2019-06-09 03:50:53 +0200 | [diff] [blame] | 347 | va_address = priv->cfg_base; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | va_address += (where & ~0x3); |
| 351 | |
| 352 | return va_address; |
| 353 | } |
| 354 | |
| 355 | static int imx_pcie_addr_valid(pci_dev_t d) |
| 356 | { |
| 357 | if ((PCI_BUS(d) == 0) && (PCI_DEV(d) > 1)) |
| 358 | return -EINVAL; |
| 359 | if ((PCI_BUS(d) == 1) && (PCI_DEV(d) > 0)) |
| 360 | return -EINVAL; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Replace the original ARM DABT handler with a simple jump-back one. |
| 366 | * |
| 367 | * The problem here is that if we have a PCIe bridge attached to this PCIe |
| 368 | * controller, but no PCIe device is connected to the bridges' downstream |
| 369 | * port, the attempt to read/write from/to the config space will produce |
| 370 | * a DABT. This is a behavior of the controller and can not be disabled |
| 371 | * unfortuatelly. |
| 372 | * |
| 373 | * To work around the problem, we backup the current DABT handler address |
| 374 | * and replace it with our own DABT handler, which only bounces right back |
| 375 | * into the code. |
| 376 | */ |
| 377 | static void imx_pcie_fix_dabt_handler(bool set) |
| 378 | { |
| 379 | extern uint32_t *_data_abort; |
| 380 | uint32_t *data_abort_addr = (uint32_t *)&_data_abort; |
| 381 | |
| 382 | static const uint32_t data_abort_bounce_handler = 0xe25ef004; |
| 383 | uint32_t data_abort_bounce_addr = (uint32_t)&data_abort_bounce_handler; |
| 384 | |
| 385 | static uint32_t data_abort_backup; |
| 386 | |
| 387 | if (set) { |
| 388 | data_abort_backup = *data_abort_addr; |
| 389 | *data_abort_addr = data_abort_bounce_addr; |
| 390 | } else { |
| 391 | *data_abort_addr = data_abort_backup; |
| 392 | } |
| 393 | } |
| 394 | |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 395 | static int imx_pcie_read_cfg(struct imx_pcie_priv *priv, pci_dev_t d, |
| 396 | int where, u32 *val) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 397 | { |
Marek Vasut | 2c20b75 | 2019-06-09 03:50:53 +0200 | [diff] [blame] | 398 | void __iomem *va_address; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 399 | int ret; |
| 400 | |
| 401 | ret = imx_pcie_addr_valid(d); |
| 402 | if (ret) { |
| 403 | *val = 0xffffffff; |
Bin Meng | 122c766 | 2016-01-08 01:03:20 -0800 | [diff] [blame] | 404 | return 0; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 407 | va_address = get_bus_address(priv, d, where); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * Read the PCIe config space. We must replace the DABT handler |
| 411 | * here in case we got data abort from the PCIe controller, see |
| 412 | * imx_pcie_fix_dabt_handler() description. Note that writing the |
| 413 | * "val" with valid value is also imperative here as in case we |
| 414 | * did got DABT, the val would contain random value. |
| 415 | */ |
| 416 | imx_pcie_fix_dabt_handler(true); |
| 417 | writel(0xffffffff, val); |
| 418 | *val = readl(va_address); |
| 419 | imx_pcie_fix_dabt_handler(false); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 424 | static int imx_pcie_write_cfg(struct imx_pcie_priv *priv, pci_dev_t d, |
| 425 | int where, u32 val) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 426 | { |
Marek Vasut | 2c20b75 | 2019-06-09 03:50:53 +0200 | [diff] [blame] | 427 | void __iomem *va_address = NULL; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 428 | int ret; |
| 429 | |
| 430 | ret = imx_pcie_addr_valid(d); |
| 431 | if (ret) |
| 432 | return ret; |
| 433 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 434 | va_address = get_bus_address(priv, d, where); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * Write the PCIe config space. We must replace the DABT handler |
| 438 | * here in case we got data abort from the PCIe controller, see |
| 439 | * imx_pcie_fix_dabt_handler() description. |
| 440 | */ |
| 441 | imx_pcie_fix_dabt_handler(true); |
| 442 | writel(val, va_address); |
| 443 | imx_pcie_fix_dabt_handler(false); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Initial bus setup |
| 450 | */ |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 451 | static int imx6_pcie_assert_core_reset(struct imx_pcie_priv *priv, |
| 452 | bool prepare_for_boot) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 453 | { |
| 454 | struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR; |
Fabio Estevam | b5272bd | 2015-10-13 11:01:27 -0300 | [diff] [blame] | 455 | |
| 456 | if (is_mx6dqp()) |
| 457 | setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST); |
| 458 | |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 459 | #if defined(CONFIG_MX6SX) |
| 460 | struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR; |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 461 | |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 462 | /* SSP_EN is not used on MX6SX anymore */ |
| 463 | setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN); |
| 464 | /* Force PCIe PHY reset */ |
| 465 | setbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST); |
| 466 | /* Power up PCIe PHY */ |
| 467 | setbits_le32(&gpc_regs->cntr, PCIE_PHY_PUP_REQ); |
| 468 | #else |
Tim Harvey | c22f2ea | 2017-05-12 12:58:41 -0700 | [diff] [blame] | 469 | /* |
| 470 | * If the bootloader already enabled the link we need some special |
| 471 | * handling to get the core back into a state where it is safe to |
| 472 | * touch it for configuration. As there is no dedicated reset signal |
| 473 | * wired up for MX6QDL, we need to manually force LTSSM into "detect" |
| 474 | * state before completely disabling LTSSM, which is a prerequisite |
| 475 | * for core configuration. |
| 476 | * |
| 477 | * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong |
| 478 | * indication that the bootloader activated the link. |
| 479 | */ |
Tim Harvey | 3b10a83 | 2021-04-16 13:30:41 -0700 | [diff] [blame] | 480 | if ((is_mx6dq() || is_mx6sdl()) && prepare_for_boot) { |
Tim Harvey | c22f2ea | 2017-05-12 12:58:41 -0700 | [diff] [blame] | 481 | u32 val, gpr1, gpr12; |
| 482 | |
| 483 | gpr1 = readl(&iomuxc_regs->gpr[1]); |
| 484 | gpr12 = readl(&iomuxc_regs->gpr[12]); |
| 485 | if ((gpr1 & IOMUXC_GPR1_PCIE_REF_CLK_EN) && |
| 486 | (gpr12 & IOMUXC_GPR12_PCIE_CTL_2)) { |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 487 | val = readl(priv->dbi_base + PCIE_PL_PFLR); |
Tim Harvey | c22f2ea | 2017-05-12 12:58:41 -0700 | [diff] [blame] | 488 | val &= ~PCIE_PL_PFLR_LINK_STATE_MASK; |
| 489 | val |= PCIE_PL_PFLR_FORCE_LINK; |
| 490 | |
| 491 | imx_pcie_fix_dabt_handler(true); |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 492 | writel(val, priv->dbi_base + PCIE_PL_PFLR); |
Tim Harvey | c22f2ea | 2017-05-12 12:58:41 -0700 | [diff] [blame] | 493 | imx_pcie_fix_dabt_handler(false); |
| 494 | |
| 495 | gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2; |
| 496 | writel(val, &iomuxc_regs->gpr[12]); |
| 497 | } |
| 498 | } |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 499 | setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN); |
| 500 | clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN); |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 501 | #endif |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int imx6_pcie_init_phy(void) |
| 507 | { |
| 508 | struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR; |
| 509 | |
| 510 | clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE); |
| 511 | |
| 512 | clrsetbits_le32(&iomuxc_regs->gpr[12], |
| 513 | IOMUXC_GPR12_DEVICE_TYPE_MASK, |
| 514 | IOMUXC_GPR12_DEVICE_TYPE_RC); |
| 515 | clrsetbits_le32(&iomuxc_regs->gpr[12], |
| 516 | IOMUXC_GPR12_LOS_LEVEL_MASK, |
| 517 | IOMUXC_GPR12_LOS_LEVEL_9); |
| 518 | |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 519 | #ifdef CONFIG_MX6SX |
| 520 | clrsetbits_le32(&iomuxc_regs->gpr[12], |
| 521 | IOMUXC_GPR12_RX_EQ_MASK, |
| 522 | IOMUXC_GPR12_RX_EQ_2); |
| 523 | #endif |
| 524 | |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 525 | writel((0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN1_OFFSET) | |
| 526 | (0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_3P5DB_OFFSET) | |
| 527 | (20 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_6DB_OFFSET) | |
| 528 | (127 << IOMUXC_GPR8_PCS_TX_SWING_FULL_OFFSET) | |
| 529 | (127 << IOMUXC_GPR8_PCS_TX_SWING_LOW_OFFSET), |
| 530 | &iomuxc_regs->gpr[8]); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
Tim Harvey | bc6b7f1 | 2022-04-13 15:57:37 -0700 | [diff] [blame] | 535 | int imx6_pcie_toggle_power(struct udevice *vpcie) |
Marek Vasut | 77779a4 | 2014-03-23 22:45:40 +0100 | [diff] [blame] | 536 | { |
Tom Rini | c43e27a | 2022-12-04 10:13:26 -0500 | [diff] [blame] | 537 | #ifdef CFG_PCIE_IMX_POWER_GPIO |
| 538 | gpio_request(CFG_PCIE_IMX_POWER_GPIO, "pcie_power"); |
| 539 | gpio_direction_output(CFG_PCIE_IMX_POWER_GPIO, 0); |
Marek Vasut | 77779a4 | 2014-03-23 22:45:40 +0100 | [diff] [blame] | 540 | mdelay(20); |
Tom Rini | c43e27a | 2022-12-04 10:13:26 -0500 | [diff] [blame] | 541 | gpio_set_value(CFG_PCIE_IMX_POWER_GPIO, 1); |
Marek Vasut | 77779a4 | 2014-03-23 22:45:40 +0100 | [diff] [blame] | 542 | mdelay(20); |
Tom Rini | c43e27a | 2022-12-04 10:13:26 -0500 | [diff] [blame] | 543 | gpio_free(CFG_PCIE_IMX_POWER_GPIO); |
Marek Vasut | 77779a4 | 2014-03-23 22:45:40 +0100 | [diff] [blame] | 544 | #endif |
Tim Harvey | 541e273 | 2022-04-13 15:54:37 -0700 | [diff] [blame] | 545 | |
| 546 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 547 | if (vpcie) { |
| 548 | regulator_set_enable(vpcie, false); |
| 549 | mdelay(20); |
| 550 | regulator_set_enable(vpcie, true); |
| 551 | mdelay(20); |
| 552 | } |
| 553 | #endif |
Marek Vasut | 77779a4 | 2014-03-23 22:45:40 +0100 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Tim Harvey | bc6b7f1 | 2022-04-13 15:57:37 -0700 | [diff] [blame] | 557 | int imx6_pcie_toggle_reset(struct gpio_desc *gpio, bool active_high) |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 558 | { |
| 559 | /* |
| 560 | * See 'PCI EXPRESS BASE SPECIFICATION, REV 3.0, SECTION 6.6.1' |
| 561 | * for detailed understanding of the PCIe CR reset logic. |
| 562 | * |
| 563 | * The PCIe #PERST reset line _MUST_ be connected, otherwise your |
| 564 | * design does not conform to the specification. You must wait at |
Fabio Estevam | 1d0b7e3 | 2015-09-10 20:45:25 -0300 | [diff] [blame] | 565 | * least 20 ms after de-asserting the #PERST so the EP device can |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 566 | * do self-initialisation. |
| 567 | * |
| 568 | * In case your #PERST pin is connected to a plain GPIO pin of the |
Tom Rini | 9fa71af | 2022-12-04 10:13:25 -0500 | [diff] [blame] | 569 | * CPU, you can define CFG_PCIE_IMX_PERST_GPIO in your board's |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 570 | * configuration file and the condition below will handle the rest |
| 571 | * of the reset toggling. |
| 572 | * |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 573 | * In case your #PERST line of the PCIe EP device is not connected |
| 574 | * at all, your design is broken and you should fix your design, |
| 575 | * otherwise you will observe problems like for example the link |
| 576 | * not coming up after rebooting the system back from running Linux |
| 577 | * that uses the PCIe as well OR the PCIe link might not come up in |
| 578 | * Linux at all in the first place since it's in some non-reset |
| 579 | * state due to being previously used in U-Boot. |
| 580 | */ |
Tom Rini | 9fa71af | 2022-12-04 10:13:25 -0500 | [diff] [blame] | 581 | #ifdef CFG_PCIE_IMX_PERST_GPIO |
| 582 | gpio_request(CFG_PCIE_IMX_PERST_GPIO, "pcie_reset"); |
| 583 | gpio_direction_output(CFG_PCIE_IMX_PERST_GPIO, 0); |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 584 | mdelay(20); |
Tom Rini | 9fa71af | 2022-12-04 10:13:25 -0500 | [diff] [blame] | 585 | gpio_set_value(CFG_PCIE_IMX_PERST_GPIO, 1); |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 586 | mdelay(20); |
Tom Rini | 9fa71af | 2022-12-04 10:13:25 -0500 | [diff] [blame] | 587 | gpio_free(CFG_PCIE_IMX_PERST_GPIO); |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 588 | #else |
Tim Harvey | 7367b39 | 2021-07-06 10:19:09 -0700 | [diff] [blame] | 589 | if (dm_gpio_is_valid(gpio)) { |
| 590 | /* Assert PERST# for 20ms then de-assert */ |
| 591 | dm_gpio_set_value(gpio, active_high ? 0 : 1); |
| 592 | mdelay(20); |
| 593 | dm_gpio_set_value(gpio, active_high ? 1 : 0); |
| 594 | mdelay(20); |
| 595 | } else { |
| 596 | puts("WARNING: Make sure the PCIe #PERST line is connected!\n"); |
| 597 | } |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 598 | #endif |
| 599 | return 0; |
| 600 | } |
| 601 | |
Tim Harvey | 7367b39 | 2021-07-06 10:19:09 -0700 | [diff] [blame] | 602 | static int imx6_pcie_deassert_core_reset(struct imx_pcie_priv *priv) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 603 | { |
| 604 | struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR; |
| 605 | |
Tim Harvey | 541e273 | 2022-04-13 15:54:37 -0700 | [diff] [blame] | 606 | imx6_pcie_toggle_power(priv->vpcie); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 607 | |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 608 | enable_pcie_clock(); |
| 609 | |
Fabio Estevam | b5272bd | 2015-10-13 11:01:27 -0300 | [diff] [blame] | 610 | if (is_mx6dqp()) |
| 611 | clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST); |
| 612 | |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 613 | /* |
| 614 | * Wait for the clock to settle a bit, when the clock are sourced |
Fabio Estevam | 1d0b7e3 | 2015-09-10 20:45:25 -0300 | [diff] [blame] | 615 | * from the CPU, we need about 30 ms to settle. |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 616 | */ |
Marek Vasut | d9056c2 | 2014-02-03 21:46:22 +0100 | [diff] [blame] | 617 | mdelay(50); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 618 | |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 619 | #if defined(CONFIG_MX6SX) |
| 620 | /* SSP_EN is not used on MX6SX anymore */ |
| 621 | clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN); |
| 622 | /* Clear PCIe PHY reset bit */ |
| 623 | clrbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST); |
| 624 | #else |
Tim Harvey | d53204a | 2014-08-07 22:57:29 -0700 | [diff] [blame] | 625 | /* Enable PCIe */ |
| 626 | clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN); |
| 627 | setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN); |
Fabio Estevam | 211a490 | 2014-08-25 14:26:45 -0300 | [diff] [blame] | 628 | #endif |
Tim Harvey | d53204a | 2014-08-07 22:57:29 -0700 | [diff] [blame] | 629 | |
Tim Harvey | 7367b39 | 2021-07-06 10:19:09 -0700 | [diff] [blame] | 630 | imx6_pcie_toggle_reset(&priv->reset_gpio, priv->reset_active_high); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 635 | static int imx_pcie_link_up(struct imx_pcie_priv *priv) |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 636 | { |
| 637 | struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR; |
| 638 | uint32_t tmp; |
| 639 | int count = 0; |
| 640 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 641 | imx6_pcie_assert_core_reset(priv, false); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 642 | imx6_pcie_init_phy(); |
Tim Harvey | 7367b39 | 2021-07-06 10:19:09 -0700 | [diff] [blame] | 643 | imx6_pcie_deassert_core_reset(priv); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 644 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 645 | imx_pcie_regions_setup(priv); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 646 | |
| 647 | /* |
Koen Vandeputte | 6be3505 | 2018-01-04 14:54:34 +0100 | [diff] [blame] | 648 | * By default, the subordinate is set equally to the secondary |
| 649 | * bus (0x01) when the RC boots. |
| 650 | * This means that theoretically, only bus 1 is reachable from the RC. |
| 651 | * Force the PCIe RC subordinate to 0xff, otherwise no downstream |
| 652 | * devices will be detected if the enumeration is applied strictly. |
| 653 | */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 654 | tmp = readl(priv->dbi_base + 0x18); |
Koen Vandeputte | 6be3505 | 2018-01-04 14:54:34 +0100 | [diff] [blame] | 655 | tmp |= (0xff << 16); |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 656 | writel(tmp, priv->dbi_base + 0x18); |
Koen Vandeputte | 6be3505 | 2018-01-04 14:54:34 +0100 | [diff] [blame] | 657 | |
| 658 | /* |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 659 | * FIXME: Force the PCIe RC to Gen1 operation |
| 660 | * The RC must be forced into Gen1 mode before bringing the link |
| 661 | * up, otherwise no downstream devices are detected. After the |
| 662 | * link is up, a managed Gen1->Gen2 transition can be initiated. |
| 663 | */ |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 664 | tmp = readl(priv->dbi_base + 0x7c); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 665 | tmp &= ~0xf; |
| 666 | tmp |= 0x1; |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 667 | writel(tmp, priv->dbi_base + 0x7c); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 668 | |
| 669 | /* LTSSM enable, starting link. */ |
| 670 | setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE); |
| 671 | |
Marek Vasut | 120efeb | 2019-06-09 03:50:54 +0200 | [diff] [blame] | 672 | while (!imx6_pcie_link_up(priv)) { |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 673 | udelay(10); |
| 674 | count++; |
Stefano Babic | d145792 | 2016-06-06 11:14:19 +0200 | [diff] [blame] | 675 | if (count >= 4000) { |
Tim Harvey | c326000 | 2015-05-08 15:17:10 -0700 | [diff] [blame] | 676 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 677 | puts("PCI: pcie phy link never came up\n"); |
| 678 | #endif |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 679 | debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n", |
Marek Vasut | 6ce573f | 2019-06-09 03:50:52 +0200 | [diff] [blame] | 680 | readl(priv->dbi_base + PCIE_PHY_DEBUG_R0), |
| 681 | readl(priv->dbi_base + PCIE_PHY_DEBUG_R1)); |
Marek Vasut | 988ee70 | 2013-12-14 05:55:28 +0100 | [diff] [blame] | 682 | return -EINVAL; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 689 | static int imx_pcie_dm_read_config(const struct udevice *dev, pci_dev_t bdf, |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 690 | uint offset, ulong *value, |
| 691 | enum pci_size_t size) |
| 692 | { |
| 693 | struct imx_pcie_priv *priv = dev_get_priv(dev); |
| 694 | u32 tmpval; |
| 695 | int ret; |
| 696 | |
| 697 | ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval); |
| 698 | if (ret) |
| 699 | return ret; |
| 700 | |
| 701 | *value = pci_conv_32_to_size(tmpval, offset, size); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int imx_pcie_dm_write_config(struct udevice *dev, pci_dev_t bdf, |
| 706 | uint offset, ulong value, |
| 707 | enum pci_size_t size) |
| 708 | { |
| 709 | struct imx_pcie_priv *priv = dev_get_priv(dev); |
| 710 | u32 tmpval, newval; |
| 711 | int ret; |
| 712 | |
| 713 | ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval); |
| 714 | if (ret) |
| 715 | return ret; |
| 716 | |
| 717 | newval = pci_conv_size_to_32(tmpval, value, offset, size); |
| 718 | return imx_pcie_write_cfg(priv, bdf, offset, newval); |
| 719 | } |
| 720 | |
| 721 | static int imx_pcie_dm_probe(struct udevice *dev) |
| 722 | { |
| 723 | struct imx_pcie_priv *priv = dev_get_priv(dev); |
| 724 | |
Tim Harvey | 541e273 | 2022-04-13 15:54:37 -0700 | [diff] [blame] | 725 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 726 | device_get_supply_regulator(dev, "vpcie-supply", &priv->vpcie); |
| 727 | #endif |
| 728 | |
Tim Harvey | 7367b39 | 2021-07-06 10:19:09 -0700 | [diff] [blame] | 729 | /* if PERST# valid from dt then assert it */ |
| 730 | gpio_request_by_name(dev, "reset-gpio", 0, &priv->reset_gpio, |
| 731 | GPIOD_IS_OUT); |
| 732 | priv->reset_active_high = dev_read_bool(dev, "reset-gpio-active-high"); |
| 733 | if (dm_gpio_is_valid(&priv->reset_gpio)) { |
| 734 | dm_gpio_set_value(&priv->reset_gpio, |
| 735 | priv->reset_active_high ? 0 : 1); |
| 736 | } |
| 737 | |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 738 | return imx_pcie_link_up(priv); |
| 739 | } |
| 740 | |
| 741 | static int imx_pcie_dm_remove(struct udevice *dev) |
| 742 | { |
| 743 | struct imx_pcie_priv *priv = dev_get_priv(dev); |
| 744 | |
| 745 | imx6_pcie_assert_core_reset(priv, true); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 750 | static int imx_pcie_of_to_plat(struct udevice *dev) |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 751 | { |
| 752 | struct imx_pcie_priv *priv = dev_get_priv(dev); |
| 753 | |
| 754 | priv->dbi_base = (void __iomem *)devfdt_get_addr_index(dev, 0); |
| 755 | priv->cfg_base = (void __iomem *)devfdt_get_addr_index(dev, 1); |
| 756 | if (!priv->dbi_base || !priv->cfg_base) |
| 757 | return -EINVAL; |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static const struct dm_pci_ops imx_pcie_ops = { |
| 763 | .read_config = imx_pcie_dm_read_config, |
| 764 | .write_config = imx_pcie_dm_write_config, |
| 765 | }; |
| 766 | |
| 767 | static const struct udevice_id imx_pcie_ids[] = { |
| 768 | { .compatible = "fsl,imx6q-pcie" }, |
Marek Vasut | bec7be1 | 2019-11-26 09:33:29 +0100 | [diff] [blame] | 769 | { .compatible = "fsl,imx6sx-pcie" }, |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 770 | { } |
| 771 | }; |
| 772 | |
| 773 | U_BOOT_DRIVER(imx_pcie) = { |
| 774 | .name = "imx_pcie", |
| 775 | .id = UCLASS_PCI, |
| 776 | .of_match = imx_pcie_ids, |
| 777 | .ops = &imx_pcie_ops, |
| 778 | .probe = imx_pcie_dm_probe, |
| 779 | .remove = imx_pcie_dm_remove, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 780 | .of_to_plat = imx_pcie_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 781 | .priv_auto = sizeof(struct imx_pcie_priv), |
Marek Vasut | 3c9f33e | 2019-06-09 03:50:55 +0200 | [diff] [blame] | 782 | .flags = DM_FLAG_OS_PREPARE, |
| 783 | }; |