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