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