Merge https://gitlab.denx.de/u-boot/custodians/u-boot-pmic
[platform/kernel/u-boot.git] / drivers / pci / pcie_imx.c
1 // SPDX-License-Identifier: GPL-2.0
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>
10  */
11
12 #include <common.h>
13 #include <init.h>
14 #include <log.h>
15 #include <malloc.h>
16 #include <pci.h>
17 #include <power/regulator.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/iomux.h>
20 #include <asm/arch/crm_regs.h>
21 #include <asm/gpio.h>
22 #include <asm/io.h>
23 #include <dm.h>
24 #include <linux/delay.h>
25 #include <linux/sizes.h>
26 #include <errno.h>
27 #include <asm/arch/sys_proto.h>
28
29 #define PCI_ACCESS_READ  0
30 #define PCI_ACCESS_WRITE 1
31
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
38 #define MX6_DBI_ADDR    0x01ffc000
39 #define MX6_IO_ADDR     0x01000000
40 #define MX6_MEM_ADDR    0x01100000
41 #define MX6_ROOT_ADDR   0x01f00000
42 #endif
43 #define MX6_DBI_SIZE    0x4000
44 #define MX6_IO_SIZE     0x100000
45 #define MX6_MEM_SIZE    0xe00000
46 #define MX6_ROOT_SIZE   0xfc000
47
48 /* PCIe Port Logic registers (memory-mapped) */
49 #define PL_OFFSET 0x700
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)
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
76 #define PCIE_PHY_PUP_REQ                (1 << 7)
77
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
101 struct imx_pcie_priv {
102         void __iomem            *dbi_base;
103         void __iomem            *cfg_base;
104         struct gpio_desc        reset_gpio;
105         bool                    reset_active_high;
106         struct udevice          *vpcie;
107 };
108
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
242 static int imx6_pcie_link_up(struct imx_pcie_priv *priv)
243 {
244         u32 rc, ltssm;
245         int rx_valid, temp;
246
247         /* link is debug bit 36, debug register 1 starts at bit 32 */
248         rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1);
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          */
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;
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
271         pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
272         temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
273         pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
274
275         udelay(3000);
276
277         pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
278         temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
279         pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
280
281         return 0;
282 }
283
284 /*
285  * iATU region setup
286  */
287 static int imx_pcie_regions_setup(struct imx_pcie_priv *priv)
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 */
302         setbits_le32(priv->dbi_base + PCI_COMMAND,
303                      PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
304
305         /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */
306         setbits_le32(priv->dbi_base + PCI_CLASS_REVISION,
307                      PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
308
309         /* Region #0 is used for Outbound CFG space access. */
310         writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
311
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),
317                priv->dbi_base + PCIE_ATU_LIMIT);
318
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);
323
324         return 0;
325 }
326
327 /*
328  * PCI Express accessors
329  */
330 static void __iomem *get_bus_address(struct imx_pcie_priv *priv,
331                                      pci_dev_t d, int where)
332 {
333         void __iomem *va_address;
334
335         /* Reconfigure Region #0 */
336         writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
337
338         if (PCI_BUS(d) < 2)
339                 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
340         else
341                 writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1);
342
343         if (PCI_BUS(d) == 0) {
344                 va_address = priv->dbi_base;
345         } else {
346                 writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
347                 va_address = priv->cfg_base;
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
395 static int imx_pcie_read_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
396                              int where, u32 *val)
397 {
398         void __iomem *va_address;
399         int ret;
400
401         ret = imx_pcie_addr_valid(d);
402         if (ret) {
403                 *val = 0xffffffff;
404                 return 0;
405         }
406
407         va_address = get_bus_address(priv, d, where);
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
424 static int imx_pcie_write_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
425                               int where, u32 val)
426 {
427         void __iomem *va_address = NULL;
428         int ret;
429
430         ret = imx_pcie_addr_valid(d);
431         if (ret)
432                 return ret;
433
434         va_address = get_bus_address(priv, d, where);
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  */
451 static int imx6_pcie_assert_core_reset(struct imx_pcie_priv *priv,
452                                        bool prepare_for_boot)
453 {
454         struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
455
456         if (is_mx6dqp())
457                 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
458
459 #if defined(CONFIG_MX6SX)
460         struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR;
461
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
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          */
480         if ((is_mx6dq() || is_mx6sdl()) && prepare_for_boot) {
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)) {
487                         val = readl(priv->dbi_base + PCIE_PL_PFLR);
488                         val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
489                         val |= PCIE_PL_PFLR_FORCE_LINK;
490
491                         imx_pcie_fix_dabt_handler(true);
492                         writel(val, priv->dbi_base + PCIE_PL_PFLR);
493                         imx_pcie_fix_dabt_handler(false);
494
495                         gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2;
496                         writel(val, &iomuxc_regs->gpr[12]);
497                 }
498         }
499         setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
500         clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
501 #endif
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
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
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
535 int imx6_pcie_toggle_power(struct udevice *vpcie)
536 {
537 #ifdef CONFIG_PCIE_IMX_POWER_GPIO
538         gpio_request(CONFIG_PCIE_IMX_POWER_GPIO, "pcie_power");
539         gpio_direction_output(CONFIG_PCIE_IMX_POWER_GPIO, 0);
540         mdelay(20);
541         gpio_set_value(CONFIG_PCIE_IMX_POWER_GPIO, 1);
542         mdelay(20);
543         gpio_free(CONFIG_PCIE_IMX_POWER_GPIO);
544 #endif
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
554         return 0;
555 }
556
557 int imx6_pcie_toggle_reset(struct gpio_desc *gpio, bool active_high)
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
565          * least 20 ms after de-asserting the #PERST so the EP device can
566          * do self-initialisation.
567          *
568          * In case your #PERST pin is connected to a plain GPIO pin of the
569          * CPU, you can define CONFIG_PCIE_IMX_PERST_GPIO in your board's
570          * configuration file and the condition below will handle the rest
571          * of the reset toggling.
572          *
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          */
581 #ifdef CONFIG_PCIE_IMX_PERST_GPIO
582         gpio_request(CONFIG_PCIE_IMX_PERST_GPIO, "pcie_reset");
583         gpio_direction_output(CONFIG_PCIE_IMX_PERST_GPIO, 0);
584         mdelay(20);
585         gpio_set_value(CONFIG_PCIE_IMX_PERST_GPIO, 1);
586         mdelay(20);
587         gpio_free(CONFIG_PCIE_IMX_PERST_GPIO);
588 #else
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         }
598 #endif
599         return 0;
600 }
601
602 static int imx6_pcie_deassert_core_reset(struct imx_pcie_priv *priv)
603 {
604         struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
605
606         imx6_pcie_toggle_power(priv->vpcie);
607
608         enable_pcie_clock();
609
610         if (is_mx6dqp())
611                 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
612
613         /*
614          * Wait for the clock to settle a bit, when the clock are sourced
615          * from the CPU, we need about 30 ms to settle.
616          */
617         mdelay(50);
618
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
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);
628 #endif
629
630         imx6_pcie_toggle_reset(&priv->reset_gpio, priv->reset_active_high);
631
632         return 0;
633 }
634
635 static int imx_pcie_link_up(struct imx_pcie_priv *priv)
636 {
637         struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
638         uint32_t tmp;
639         int count = 0;
640
641         imx6_pcie_assert_core_reset(priv, false);
642         imx6_pcie_init_phy();
643         imx6_pcie_deassert_core_reset(priv);
644
645         imx_pcie_regions_setup(priv);
646
647         /*
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          */
654         tmp = readl(priv->dbi_base + 0x18);
655         tmp |= (0xff << 16);
656         writel(tmp, priv->dbi_base + 0x18);
657
658         /*
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          */
664         tmp = readl(priv->dbi_base + 0x7c);
665         tmp &= ~0xf;
666         tmp |= 0x1;
667         writel(tmp, priv->dbi_base + 0x7c);
668
669         /* LTSSM enable, starting link. */
670         setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
671
672         while (!imx6_pcie_link_up(priv)) {
673                 udelay(10);
674                 count++;
675                 if (count >= 4000) {
676 #ifdef CONFIG_PCI_SCAN_SHOW
677                         puts("PCI:   pcie phy link never came up\n");
678 #endif
679                         debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
680                               readl(priv->dbi_base + PCIE_PHY_DEBUG_R0),
681                               readl(priv->dbi_base + PCIE_PHY_DEBUG_R1));
682                         return -EINVAL;
683                 }
684         }
685
686         return 0;
687 }
688
689 static int imx_pcie_dm_read_config(const struct udevice *dev, pci_dev_t bdf,
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
725 #if CONFIG_IS_ENABLED(DM_REGULATOR)
726         device_get_supply_regulator(dev, "vpcie-supply", &priv->vpcie);
727 #endif
728
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
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
750 static int imx_pcie_of_to_plat(struct udevice *dev)
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" },
769         { .compatible = "fsl,imx6sx-pcie" },
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,
780         .of_to_plat     = imx_pcie_of_to_plat,
781         .priv_auto      = sizeof(struct imx_pcie_priv),
782         .flags                  = DM_FLAG_OS_PREPARE,
783 };