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