PCI: dwc/imx6: Use common PCI register definitions
[linux-2.6-microblaze.git] / drivers / pci / controller / dwc / pci-imx6.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe host controller driver for Freescale i.MX6 SoCs
4  *
5  * Copyright (C) 2013 Kosagi
6  *              https://www.kosagi.com
7  *
8  * Author: Sean Cross <xobs@kosagi.com>
9  */
10
11 #include <linux/bitfield.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
18 #include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
19 #include <linux/module.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_device.h>
22 #include <linux/of_address.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/resource.h>
28 #include <linux/signal.h>
29 #include <linux/types.h>
30 #include <linux/interrupt.h>
31 #include <linux/reset.h>
32 #include <linux/pm_domain.h>
33 #include <linux/pm_runtime.h>
34
35 #include "pcie-designware.h"
36
37 #define IMX8MQ_GPR_PCIE_REF_USE_PAD             BIT(9)
38 #define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN     BIT(10)
39 #define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE        BIT(11)
40 #define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE     GENMASK(11, 8)
41 #define IMX8MQ_PCIE2_BASE_ADDR                  0x33c00000
42
43 #define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
44
45 enum imx6_pcie_variants {
46         IMX6Q,
47         IMX6SX,
48         IMX6QP,
49         IMX7D,
50         IMX8MQ,
51 };
52
53 #define IMX6_PCIE_FLAG_IMX6_PHY                 BIT(0)
54 #define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE        BIT(1)
55 #define IMX6_PCIE_FLAG_SUPPORTS_SUSPEND         BIT(2)
56
57 struct imx6_pcie_drvdata {
58         enum imx6_pcie_variants variant;
59         u32 flags;
60         int dbi_length;
61 };
62
63 struct imx6_pcie {
64         struct dw_pcie          *pci;
65         int                     reset_gpio;
66         bool                    gpio_active_high;
67         struct clk              *pcie_bus;
68         struct clk              *pcie_phy;
69         struct clk              *pcie_inbound_axi;
70         struct clk              *pcie;
71         struct clk              *pcie_aux;
72         struct regmap           *iomuxc_gpr;
73         u32                     controller_id;
74         struct reset_control    *pciephy_reset;
75         struct reset_control    *apps_reset;
76         struct reset_control    *turnoff_reset;
77         u32                     tx_deemph_gen1;
78         u32                     tx_deemph_gen2_3p5db;
79         u32                     tx_deemph_gen2_6db;
80         u32                     tx_swing_full;
81         u32                     tx_swing_low;
82         int                     link_gen;
83         struct regulator        *vpcie;
84         void __iomem            *phy_base;
85
86         /* power domain for pcie */
87         struct device           *pd_pcie;
88         /* power domain for pcie phy */
89         struct device           *pd_pcie_phy;
90         const struct imx6_pcie_drvdata *drvdata;
91 };
92
93 /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
94 #define PHY_PLL_LOCK_WAIT_USLEEP_MAX    200
95 #define PHY_PLL_LOCK_WAIT_TIMEOUT       (2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
96
97 /* PCIe Port Logic registers (memory-mapped) */
98 #define PL_OFFSET 0x700
99
100 #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
101 #define PCIE_PHY_CTRL_DATA(x)           FIELD_PREP(GENMASK(15, 0), (x))
102 #define PCIE_PHY_CTRL_CAP_ADR           BIT(16)
103 #define PCIE_PHY_CTRL_CAP_DAT           BIT(17)
104 #define PCIE_PHY_CTRL_WR                BIT(18)
105 #define PCIE_PHY_CTRL_RD                BIT(19)
106
107 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
108 #define PCIE_PHY_STAT_ACK               BIT(16)
109
110 /* PHY registers (not memory-mapped) */
111 #define PCIE_PHY_ATEOVRD                        0x10
112 #define  PCIE_PHY_ATEOVRD_EN                    BIT(2)
113 #define  PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT      0
114 #define  PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK       0x1
115
116 #define PCIE_PHY_MPLL_OVRD_IN_LO                0x11
117 #define  PCIE_PHY_MPLL_MULTIPLIER_SHIFT         2
118 #define  PCIE_PHY_MPLL_MULTIPLIER_MASK          0x7f
119 #define  PCIE_PHY_MPLL_MULTIPLIER_OVRD          BIT(9)
120
121 #define PCIE_PHY_RX_ASIC_OUT 0x100D
122 #define PCIE_PHY_RX_ASIC_OUT_VALID      (1 << 0)
123
124 /* iMX7 PCIe PHY registers */
125 #define PCIE_PHY_CMN_REG4               0x14
126 /* These are probably the bits that *aren't* DCC_FB_EN */
127 #define PCIE_PHY_CMN_REG4_DCC_FB_EN     0x29
128
129 #define PCIE_PHY_CMN_REG15              0x54
130 #define PCIE_PHY_CMN_REG15_DLY_4        BIT(2)
131 #define PCIE_PHY_CMN_REG15_PLL_PD       BIT(5)
132 #define PCIE_PHY_CMN_REG15_OVRD_PLL_PD  BIT(7)
133
134 #define PCIE_PHY_CMN_REG24              0x90
135 #define PCIE_PHY_CMN_REG24_RX_EQ        BIT(6)
136 #define PCIE_PHY_CMN_REG24_RX_EQ_SEL    BIT(3)
137
138 #define PCIE_PHY_CMN_REG26              0x98
139 #define PCIE_PHY_CMN_REG26_ATT_MODE     0xBC
140
141 #define PHY_RX_OVRD_IN_LO 0x1005
142 #define PHY_RX_OVRD_IN_LO_RX_DATA_EN            BIT(5)
143 #define PHY_RX_OVRD_IN_LO_RX_PLL_EN             BIT(3)
144
145 static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, bool exp_val)
146 {
147         struct dw_pcie *pci = imx6_pcie->pci;
148         bool val;
149         u32 max_iterations = 10;
150         u32 wait_counter = 0;
151
152         do {
153                 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT) &
154                         PCIE_PHY_STAT_ACK;
155                 wait_counter++;
156
157                 if (val == exp_val)
158                         return 0;
159
160                 udelay(1);
161         } while (wait_counter < max_iterations);
162
163         return -ETIMEDOUT;
164 }
165
166 static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
167 {
168         struct dw_pcie *pci = imx6_pcie->pci;
169         u32 val;
170         int ret;
171
172         val = PCIE_PHY_CTRL_DATA(addr);
173         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
174
175         val |= PCIE_PHY_CTRL_CAP_ADR;
176         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
177
178         ret = pcie_phy_poll_ack(imx6_pcie, true);
179         if (ret)
180                 return ret;
181
182         val = PCIE_PHY_CTRL_DATA(addr);
183         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
184
185         return pcie_phy_poll_ack(imx6_pcie, false);
186 }
187
188 /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
189 static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, u16 *data)
190 {
191         struct dw_pcie *pci = imx6_pcie->pci;
192         u32 phy_ctl;
193         int ret;
194
195         ret = pcie_phy_wait_ack(imx6_pcie, addr);
196         if (ret)
197                 return ret;
198
199         /* assert Read signal */
200         phy_ctl = PCIE_PHY_CTRL_RD;
201         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
202
203         ret = pcie_phy_poll_ack(imx6_pcie, true);
204         if (ret)
205                 return ret;
206
207         *data = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
208
209         /* deassert Read signal */
210         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
211
212         return pcie_phy_poll_ack(imx6_pcie, false);
213 }
214
215 static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, u16 data)
216 {
217         struct dw_pcie *pci = imx6_pcie->pci;
218         u32 var;
219         int ret;
220
221         /* write addr */
222         /* cap addr */
223         ret = pcie_phy_wait_ack(imx6_pcie, addr);
224         if (ret)
225                 return ret;
226
227         var = PCIE_PHY_CTRL_DATA(data);
228         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
229
230         /* capture data */
231         var |= PCIE_PHY_CTRL_CAP_DAT;
232         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
233
234         ret = pcie_phy_poll_ack(imx6_pcie, true);
235         if (ret)
236                 return ret;
237
238         /* deassert cap data */
239         var = PCIE_PHY_CTRL_DATA(data);
240         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
241
242         /* wait for ack de-assertion */
243         ret = pcie_phy_poll_ack(imx6_pcie, false);
244         if (ret)
245                 return ret;
246
247         /* assert wr signal */
248         var = PCIE_PHY_CTRL_WR;
249         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
250
251         /* wait for ack */
252         ret = pcie_phy_poll_ack(imx6_pcie, true);
253         if (ret)
254                 return ret;
255
256         /* deassert wr signal */
257         var = PCIE_PHY_CTRL_DATA(data);
258         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
259
260         /* wait for ack de-assertion */
261         ret = pcie_phy_poll_ack(imx6_pcie, false);
262         if (ret)
263                 return ret;
264
265         dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
266
267         return 0;
268 }
269
270 static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
271 {
272         u16 tmp;
273
274         if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
275                 return;
276
277         pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
278         tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
279                 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
280         pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
281
282         usleep_range(2000, 3000);
283
284         pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
285         tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
286                   PHY_RX_OVRD_IN_LO_RX_PLL_EN);
287         pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
288 }
289
290 #ifdef CONFIG_ARM
291 /*  Added for PCI abort handling */
292 static int imx6q_pcie_abort_handler(unsigned long addr,
293                 unsigned int fsr, struct pt_regs *regs)
294 {
295         unsigned long pc = instruction_pointer(regs);
296         unsigned long instr = *(unsigned long *)pc;
297         int reg = (instr >> 12) & 15;
298
299         /*
300          * If the instruction being executed was a read,
301          * make it look like it read all-ones.
302          */
303         if ((instr & 0x0c100000) == 0x04100000) {
304                 unsigned long val;
305
306                 if (instr & 0x00400000)
307                         val = 255;
308                 else
309                         val = -1;
310
311                 regs->uregs[reg] = val;
312                 regs->ARM_pc += 4;
313                 return 0;
314         }
315
316         if ((instr & 0x0e100090) == 0x00100090) {
317                 regs->uregs[reg] = -1;
318                 regs->ARM_pc += 4;
319                 return 0;
320         }
321
322         return 1;
323 }
324 #endif
325
326 static int imx6_pcie_attach_pd(struct device *dev)
327 {
328         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
329         struct device_link *link;
330
331         /* Do nothing when in a single power domain */
332         if (dev->pm_domain)
333                 return 0;
334
335         imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
336         if (IS_ERR(imx6_pcie->pd_pcie))
337                 return PTR_ERR(imx6_pcie->pd_pcie);
338         /* Do nothing when power domain missing */
339         if (!imx6_pcie->pd_pcie)
340                 return 0;
341         link = device_link_add(dev, imx6_pcie->pd_pcie,
342                         DL_FLAG_STATELESS |
343                         DL_FLAG_PM_RUNTIME |
344                         DL_FLAG_RPM_ACTIVE);
345         if (!link) {
346                 dev_err(dev, "Failed to add device_link to pcie pd.\n");
347                 return -EINVAL;
348         }
349
350         imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
351         if (IS_ERR(imx6_pcie->pd_pcie_phy))
352                 return PTR_ERR(imx6_pcie->pd_pcie_phy);
353
354         link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
355                         DL_FLAG_STATELESS |
356                         DL_FLAG_PM_RUNTIME |
357                         DL_FLAG_RPM_ACTIVE);
358         if (!link) {
359                 dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
360                 return -EINVAL;
361         }
362
363         return 0;
364 }
365
366 static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
367 {
368         struct device *dev = imx6_pcie->pci->dev;
369
370         switch (imx6_pcie->drvdata->variant) {
371         case IMX7D:
372         case IMX8MQ:
373                 reset_control_assert(imx6_pcie->pciephy_reset);
374                 reset_control_assert(imx6_pcie->apps_reset);
375                 break;
376         case IMX6SX:
377                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
378                                    IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
379                                    IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
380                 /* Force PCIe PHY reset */
381                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
382                                    IMX6SX_GPR5_PCIE_BTNRST_RESET,
383                                    IMX6SX_GPR5_PCIE_BTNRST_RESET);
384                 break;
385         case IMX6QP:
386                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
387                                    IMX6Q_GPR1_PCIE_SW_RST,
388                                    IMX6Q_GPR1_PCIE_SW_RST);
389                 break;
390         case IMX6Q:
391                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
392                                    IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
393                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
394                                    IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
395                 break;
396         }
397
398         if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
399                 int ret = regulator_disable(imx6_pcie->vpcie);
400
401                 if (ret)
402                         dev_err(dev, "failed to disable vpcie regulator: %d\n",
403                                 ret);
404         }
405 }
406
407 static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
408 {
409         WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ);
410         return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
411 }
412
413 static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
414 {
415         struct dw_pcie *pci = imx6_pcie->pci;
416         struct device *dev = pci->dev;
417         unsigned int offset;
418         int ret = 0;
419
420         switch (imx6_pcie->drvdata->variant) {
421         case IMX6SX:
422                 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
423                 if (ret) {
424                         dev_err(dev, "unable to enable pcie_axi clock\n");
425                         break;
426                 }
427
428                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
429                                    IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
430                 break;
431         case IMX6QP:            /* FALLTHROUGH */
432         case IMX6Q:
433                 /* power up core phy and enable ref clock */
434                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
435                                    IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
436                 /*
437                  * the async reset input need ref clock to sync internally,
438                  * when the ref clock comes after reset, internal synced
439                  * reset time is too short, cannot meet the requirement.
440                  * add one ~10us delay here.
441                  */
442                 usleep_range(10, 100);
443                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
444                                    IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
445                 break;
446         case IMX7D:
447                 break;
448         case IMX8MQ:
449                 ret = clk_prepare_enable(imx6_pcie->pcie_aux);
450                 if (ret) {
451                         dev_err(dev, "unable to enable pcie_aux clock\n");
452                         break;
453                 }
454
455                 offset = imx6_pcie_grp_offset(imx6_pcie);
456                 /*
457                  * Set the over ride low and enabled
458                  * make sure that REF_CLK is turned on.
459                  */
460                 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
461                                    IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
462                                    0);
463                 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
464                                    IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
465                                    IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
466                 break;
467         }
468
469         return ret;
470 }
471
472 static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
473 {
474         u32 val;
475         struct device *dev = imx6_pcie->pci->dev;
476
477         if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
478                                      IOMUXC_GPR22, val,
479                                      val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
480                                      PHY_PLL_LOCK_WAIT_USLEEP_MAX,
481                                      PHY_PLL_LOCK_WAIT_TIMEOUT))
482                 dev_err(dev, "PCIe PLL lock timeout\n");
483 }
484
485 static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
486 {
487         struct dw_pcie *pci = imx6_pcie->pci;
488         struct device *dev = pci->dev;
489         int ret;
490
491         if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
492                 ret = regulator_enable(imx6_pcie->vpcie);
493                 if (ret) {
494                         dev_err(dev, "failed to enable vpcie regulator: %d\n",
495                                 ret);
496                         return;
497                 }
498         }
499
500         ret = clk_prepare_enable(imx6_pcie->pcie_phy);
501         if (ret) {
502                 dev_err(dev, "unable to enable pcie_phy clock\n");
503                 goto err_pcie_phy;
504         }
505
506         ret = clk_prepare_enable(imx6_pcie->pcie_bus);
507         if (ret) {
508                 dev_err(dev, "unable to enable pcie_bus clock\n");
509                 goto err_pcie_bus;
510         }
511
512         ret = clk_prepare_enable(imx6_pcie->pcie);
513         if (ret) {
514                 dev_err(dev, "unable to enable pcie clock\n");
515                 goto err_pcie;
516         }
517
518         ret = imx6_pcie_enable_ref_clk(imx6_pcie);
519         if (ret) {
520                 dev_err(dev, "unable to enable pcie ref clock\n");
521                 goto err_ref_clk;
522         }
523
524         /* allow the clocks to stabilize */
525         usleep_range(200, 500);
526
527         /* Some boards don't have PCIe reset GPIO. */
528         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
529                 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
530                                         imx6_pcie->gpio_active_high);
531                 msleep(100);
532                 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
533                                         !imx6_pcie->gpio_active_high);
534         }
535
536         switch (imx6_pcie->drvdata->variant) {
537         case IMX8MQ:
538                 reset_control_deassert(imx6_pcie->pciephy_reset);
539                 break;
540         case IMX7D:
541                 reset_control_deassert(imx6_pcie->pciephy_reset);
542
543                 /* Workaround for ERR010728, failure of PCI-e PLL VCO to
544                  * oscillate, especially when cold.  This turns off "Duty-cycle
545                  * Corrector" and other mysterious undocumented things.
546                  */
547                 if (likely(imx6_pcie->phy_base)) {
548                         /* De-assert DCC_FB_EN */
549                         writel(PCIE_PHY_CMN_REG4_DCC_FB_EN,
550                                imx6_pcie->phy_base + PCIE_PHY_CMN_REG4);
551                         /* Assert RX_EQS and RX_EQS_SEL */
552                         writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL
553                                 | PCIE_PHY_CMN_REG24_RX_EQ,
554                                imx6_pcie->phy_base + PCIE_PHY_CMN_REG24);
555                         /* Assert ATT_MODE */
556                         writel(PCIE_PHY_CMN_REG26_ATT_MODE,
557                                imx6_pcie->phy_base + PCIE_PHY_CMN_REG26);
558                 } else {
559                         dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n");
560                 }
561
562                 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
563                 break;
564         case IMX6SX:
565                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
566                                    IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
567                 break;
568         case IMX6QP:
569                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
570                                    IMX6Q_GPR1_PCIE_SW_RST, 0);
571
572                 usleep_range(200, 500);
573                 break;
574         case IMX6Q:             /* Nothing to do */
575                 break;
576         }
577
578         return;
579
580 err_ref_clk:
581         clk_disable_unprepare(imx6_pcie->pcie);
582 err_pcie:
583         clk_disable_unprepare(imx6_pcie->pcie_bus);
584 err_pcie_bus:
585         clk_disable_unprepare(imx6_pcie->pcie_phy);
586 err_pcie_phy:
587         if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
588                 ret = regulator_disable(imx6_pcie->vpcie);
589                 if (ret)
590                         dev_err(dev, "failed to disable vpcie regulator: %d\n",
591                                 ret);
592         }
593 }
594
595 static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie)
596 {
597         unsigned int mask, val;
598
599         if (imx6_pcie->drvdata->variant == IMX8MQ &&
600             imx6_pcie->controller_id == 1) {
601                 mask   = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE;
602                 val    = FIELD_PREP(IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
603                                     PCI_EXP_TYPE_ROOT_PORT);
604         } else {
605                 mask = IMX6Q_GPR12_DEVICE_TYPE;
606                 val  = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE,
607                                   PCI_EXP_TYPE_ROOT_PORT);
608         }
609
610         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, mask, val);
611 }
612
613 static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
614 {
615         switch (imx6_pcie->drvdata->variant) {
616         case IMX8MQ:
617                 /*
618                  * TODO: Currently this code assumes external
619                  * oscillator is being used
620                  */
621                 regmap_update_bits(imx6_pcie->iomuxc_gpr,
622                                    imx6_pcie_grp_offset(imx6_pcie),
623                                    IMX8MQ_GPR_PCIE_REF_USE_PAD,
624                                    IMX8MQ_GPR_PCIE_REF_USE_PAD);
625                 break;
626         case IMX7D:
627                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
628                                    IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
629                 break;
630         case IMX6SX:
631                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
632                                    IMX6SX_GPR12_PCIE_RX_EQ_MASK,
633                                    IMX6SX_GPR12_PCIE_RX_EQ_2);
634                 /* FALLTHROUGH */
635         default:
636                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
637                                    IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
638
639                 /* configure constant input signal to the pcie ctrl and phy */
640                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
641                                    IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
642
643                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
644                                    IMX6Q_GPR8_TX_DEEMPH_GEN1,
645                                    imx6_pcie->tx_deemph_gen1 << 0);
646                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
647                                    IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
648                                    imx6_pcie->tx_deemph_gen2_3p5db << 6);
649                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
650                                    IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
651                                    imx6_pcie->tx_deemph_gen2_6db << 12);
652                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
653                                    IMX6Q_GPR8_TX_SWING_FULL,
654                                    imx6_pcie->tx_swing_full << 18);
655                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
656                                    IMX6Q_GPR8_TX_SWING_LOW,
657                                    imx6_pcie->tx_swing_low << 25);
658                 break;
659         }
660
661         imx6_pcie_configure_type(imx6_pcie);
662 }
663
664 static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
665 {
666         unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
667         int mult, div;
668         u16 val;
669
670         if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
671                 return 0;
672
673         switch (phy_rate) {
674         case 125000000:
675                 /*
676                  * The default settings of the MPLL are for a 125MHz input
677                  * clock, so no need to reconfigure anything in that case.
678                  */
679                 return 0;
680         case 100000000:
681                 mult = 25;
682                 div = 0;
683                 break;
684         case 200000000:
685                 mult = 25;
686                 div = 1;
687                 break;
688         default:
689                 dev_err(imx6_pcie->pci->dev,
690                         "Unsupported PHY reference clock rate %lu\n", phy_rate);
691                 return -EINVAL;
692         }
693
694         pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
695         val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
696                  PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
697         val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
698         val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
699         pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
700
701         pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
702         val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
703                  PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
704         val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
705         val |= PCIE_PHY_ATEOVRD_EN;
706         pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
707
708         return 0;
709 }
710
711 static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
712 {
713         struct dw_pcie *pci = imx6_pcie->pci;
714         struct device *dev = pci->dev;
715         u32 tmp;
716         unsigned int retries;
717
718         for (retries = 0; retries < 200; retries++) {
719                 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
720                 /* Test if the speed change finished. */
721                 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
722                         return 0;
723                 usleep_range(100, 1000);
724         }
725
726         dev_err(dev, "Speed change timeout\n");
727         return -ETIMEDOUT;
728 }
729
730 static void imx6_pcie_ltssm_enable(struct device *dev)
731 {
732         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
733
734         switch (imx6_pcie->drvdata->variant) {
735         case IMX6Q:
736         case IMX6SX:
737         case IMX6QP:
738                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
739                                    IMX6Q_GPR12_PCIE_CTL_2,
740                                    IMX6Q_GPR12_PCIE_CTL_2);
741                 break;
742         case IMX7D:
743         case IMX8MQ:
744                 reset_control_deassert(imx6_pcie->apps_reset);
745                 break;
746         }
747 }
748
749 static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
750 {
751         struct dw_pcie *pci = imx6_pcie->pci;
752         struct device *dev = pci->dev;
753         u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
754         u32 tmp;
755         int ret;
756
757         /*
758          * Force Gen1 operation when starting the link.  In case the link is
759          * started in Gen2 mode, there is a possibility the devices on the
760          * bus will not be detected at all.  This happens with PCIe switches.
761          */
762         tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
763         tmp &= ~PCI_EXP_LNKCAP_SLS;
764         tmp |= PCI_EXP_LNKCAP_SLS_2_5GB;
765         dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
766
767         /* Start LTSSM. */
768         imx6_pcie_ltssm_enable(dev);
769
770         ret = dw_pcie_wait_for_link(pci);
771         if (ret)
772                 goto err_reset_phy;
773
774         if (imx6_pcie->link_gen == 2) {
775                 /* Allow Gen2 mode after the link is up. */
776                 tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
777                 tmp &= ~PCI_EXP_LNKCAP_SLS;
778                 tmp |= PCI_EXP_LNKCAP_SLS_5_0GB;
779                 dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
780
781                 /*
782                  * Start Directed Speed Change so the best possible
783                  * speed both link partners support can be negotiated.
784                  */
785                 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
786                 tmp |= PORT_LOGIC_SPEED_CHANGE;
787                 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
788
789                 if (imx6_pcie->drvdata->flags &
790                     IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) {
791                         /*
792                          * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
793                          * from i.MX6 family when no link speed transition
794                          * occurs and we go Gen1 -> yep, Gen1. The difference
795                          * is that, in such case, it will not be cleared by HW
796                          * which will cause the following code to report false
797                          * failure.
798                          */
799
800                         ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
801                         if (ret) {
802                                 dev_err(dev, "Failed to bring link up!\n");
803                                 goto err_reset_phy;
804                         }
805                 }
806
807                 /* Make sure link training is finished as well! */
808                 ret = dw_pcie_wait_for_link(pci);
809                 if (ret) {
810                         dev_err(dev, "Failed to bring link up!\n");
811                         goto err_reset_phy;
812                 }
813         } else {
814                 dev_info(dev, "Link: Gen2 disabled\n");
815         }
816
817         tmp = dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKSTA);
818         dev_info(dev, "Link up, Gen%i\n", tmp & PCI_EXP_LNKSTA_CLS);
819         return 0;
820
821 err_reset_phy:
822         dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
823                 dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0),
824                 dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG1));
825         imx6_pcie_reset_phy(imx6_pcie);
826         return ret;
827 }
828
829 static int imx6_pcie_host_init(struct pcie_port *pp)
830 {
831         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
832         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
833
834         imx6_pcie_assert_core_reset(imx6_pcie);
835         imx6_pcie_init_phy(imx6_pcie);
836         imx6_pcie_deassert_core_reset(imx6_pcie);
837         imx6_setup_phy_mpll(imx6_pcie);
838         dw_pcie_setup_rc(pp);
839         imx6_pcie_establish_link(imx6_pcie);
840         dw_pcie_msi_init(pp);
841
842         return 0;
843 }
844
845 static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
846         .host_init = imx6_pcie_host_init,
847 };
848
849 static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
850                               struct platform_device *pdev)
851 {
852         struct dw_pcie *pci = imx6_pcie->pci;
853         struct pcie_port *pp = &pci->pp;
854         struct device *dev = &pdev->dev;
855         int ret;
856
857         if (IS_ENABLED(CONFIG_PCI_MSI)) {
858                 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
859                 if (pp->msi_irq < 0)
860                         return pp->msi_irq;
861         }
862
863         pp->ops = &imx6_pcie_host_ops;
864
865         ret = dw_pcie_host_init(pp);
866         if (ret) {
867                 dev_err(dev, "failed to initialize host\n");
868                 return ret;
869         }
870
871         return 0;
872 }
873
874 static const struct dw_pcie_ops dw_pcie_ops = {
875         /* No special ops needed, but pcie-designware still expects this struct */
876 };
877
878 #ifdef CONFIG_PM_SLEEP
879 static void imx6_pcie_ltssm_disable(struct device *dev)
880 {
881         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
882
883         switch (imx6_pcie->drvdata->variant) {
884         case IMX6SX:
885         case IMX6QP:
886                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
887                                    IMX6Q_GPR12_PCIE_CTL_2, 0);
888                 break;
889         case IMX7D:
890                 reset_control_assert(imx6_pcie->apps_reset);
891                 break;
892         default:
893                 dev_err(dev, "ltssm_disable not supported\n");
894         }
895 }
896
897 static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
898 {
899         struct device *dev = imx6_pcie->pci->dev;
900
901         /* Some variants have a turnoff reset in DT */
902         if (imx6_pcie->turnoff_reset) {
903                 reset_control_assert(imx6_pcie->turnoff_reset);
904                 reset_control_deassert(imx6_pcie->turnoff_reset);
905                 goto pm_turnoff_sleep;
906         }
907
908         /* Others poke directly at IOMUXC registers */
909         switch (imx6_pcie->drvdata->variant) {
910         case IMX6SX:
911                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
912                                 IMX6SX_GPR12_PCIE_PM_TURN_OFF,
913                                 IMX6SX_GPR12_PCIE_PM_TURN_OFF);
914                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
915                                 IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
916                 break;
917         default:
918                 dev_err(dev, "PME_Turn_Off not implemented\n");
919                 return;
920         }
921
922         /*
923          * Components with an upstream port must respond to
924          * PME_Turn_Off with PME_TO_Ack but we can't check.
925          *
926          * The standard recommends a 1-10ms timeout after which to
927          * proceed anyway as if acks were received.
928          */
929 pm_turnoff_sleep:
930         usleep_range(1000, 10000);
931 }
932
933 static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
934 {
935         clk_disable_unprepare(imx6_pcie->pcie);
936         clk_disable_unprepare(imx6_pcie->pcie_phy);
937         clk_disable_unprepare(imx6_pcie->pcie_bus);
938
939         switch (imx6_pcie->drvdata->variant) {
940         case IMX6SX:
941                 clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
942                 break;
943         case IMX7D:
944                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
945                                    IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
946                                    IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
947                 break;
948         case IMX8MQ:
949                 clk_disable_unprepare(imx6_pcie->pcie_aux);
950                 break;
951         default:
952                 break;
953         }
954 }
955
956 static int imx6_pcie_suspend_noirq(struct device *dev)
957 {
958         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
959
960         if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
961                 return 0;
962
963         imx6_pcie_pm_turnoff(imx6_pcie);
964         imx6_pcie_clk_disable(imx6_pcie);
965         imx6_pcie_ltssm_disable(dev);
966
967         return 0;
968 }
969
970 static int imx6_pcie_resume_noirq(struct device *dev)
971 {
972         int ret;
973         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
974         struct pcie_port *pp = &imx6_pcie->pci->pp;
975
976         if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
977                 return 0;
978
979         imx6_pcie_assert_core_reset(imx6_pcie);
980         imx6_pcie_init_phy(imx6_pcie);
981         imx6_pcie_deassert_core_reset(imx6_pcie);
982         dw_pcie_setup_rc(pp);
983
984         ret = imx6_pcie_establish_link(imx6_pcie);
985         if (ret < 0)
986                 dev_info(dev, "pcie link is down after resume.\n");
987
988         return 0;
989 }
990 #endif
991
992 static const struct dev_pm_ops imx6_pcie_pm_ops = {
993         SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
994                                       imx6_pcie_resume_noirq)
995 };
996
997 static int imx6_pcie_probe(struct platform_device *pdev)
998 {
999         struct device *dev = &pdev->dev;
1000         struct dw_pcie *pci;
1001         struct imx6_pcie *imx6_pcie;
1002         struct device_node *np;
1003         struct resource *dbi_base;
1004         struct device_node *node = dev->of_node;
1005         int ret;
1006         u16 val;
1007
1008         imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
1009         if (!imx6_pcie)
1010                 return -ENOMEM;
1011
1012         pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1013         if (!pci)
1014                 return -ENOMEM;
1015
1016         pci->dev = dev;
1017         pci->ops = &dw_pcie_ops;
1018
1019         imx6_pcie->pci = pci;
1020         imx6_pcie->drvdata = of_device_get_match_data(dev);
1021
1022         /* Find the PHY if one is defined, only imx7d uses it */
1023         np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0);
1024         if (np) {
1025                 struct resource res;
1026
1027                 ret = of_address_to_resource(np, 0, &res);
1028                 if (ret) {
1029                         dev_err(dev, "Unable to map PCIe PHY\n");
1030                         return ret;
1031                 }
1032                 imx6_pcie->phy_base = devm_ioremap_resource(dev, &res);
1033                 if (IS_ERR(imx6_pcie->phy_base)) {
1034                         dev_err(dev, "Unable to map PCIe PHY\n");
1035                         return PTR_ERR(imx6_pcie->phy_base);
1036                 }
1037         }
1038
1039         dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1040         pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
1041         if (IS_ERR(pci->dbi_base))
1042                 return PTR_ERR(pci->dbi_base);
1043
1044         /* Fetch GPIOs */
1045         imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
1046         imx6_pcie->gpio_active_high = of_property_read_bool(node,
1047                                                 "reset-gpio-active-high");
1048         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
1049                 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
1050                                 imx6_pcie->gpio_active_high ?
1051                                         GPIOF_OUT_INIT_HIGH :
1052                                         GPIOF_OUT_INIT_LOW,
1053                                 "PCIe reset");
1054                 if (ret) {
1055                         dev_err(dev, "unable to get reset gpio\n");
1056                         return ret;
1057                 }
1058         } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
1059                 return imx6_pcie->reset_gpio;
1060         }
1061
1062         /* Fetch clocks */
1063         imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
1064         if (IS_ERR(imx6_pcie->pcie_phy)) {
1065                 dev_err(dev, "pcie_phy clock source missing or invalid\n");
1066                 return PTR_ERR(imx6_pcie->pcie_phy);
1067         }
1068
1069         imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
1070         if (IS_ERR(imx6_pcie->pcie_bus)) {
1071                 dev_err(dev, "pcie_bus clock source missing or invalid\n");
1072                 return PTR_ERR(imx6_pcie->pcie_bus);
1073         }
1074
1075         imx6_pcie->pcie = devm_clk_get(dev, "pcie");
1076         if (IS_ERR(imx6_pcie->pcie)) {
1077                 dev_err(dev, "pcie clock source missing or invalid\n");
1078                 return PTR_ERR(imx6_pcie->pcie);
1079         }
1080
1081         switch (imx6_pcie->drvdata->variant) {
1082         case IMX6SX:
1083                 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
1084                                                            "pcie_inbound_axi");
1085                 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
1086                         dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
1087                         return PTR_ERR(imx6_pcie->pcie_inbound_axi);
1088                 }
1089                 break;
1090         case IMX8MQ:
1091                 imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux");
1092                 if (IS_ERR(imx6_pcie->pcie_aux)) {
1093                         dev_err(dev, "pcie_aux clock source missing or invalid\n");
1094                         return PTR_ERR(imx6_pcie->pcie_aux);
1095                 }
1096                 /* fall through */
1097         case IMX7D:
1098                 if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR)
1099                         imx6_pcie->controller_id = 1;
1100
1101                 imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
1102                                                                             "pciephy");
1103                 if (IS_ERR(imx6_pcie->pciephy_reset)) {
1104                         dev_err(dev, "Failed to get PCIEPHY reset control\n");
1105                         return PTR_ERR(imx6_pcie->pciephy_reset);
1106                 }
1107
1108                 imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
1109                                                                          "apps");
1110                 if (IS_ERR(imx6_pcie->apps_reset)) {
1111                         dev_err(dev, "Failed to get PCIE APPS reset control\n");
1112                         return PTR_ERR(imx6_pcie->apps_reset);
1113                 }
1114                 break;
1115         default:
1116                 break;
1117         }
1118
1119         /* Grab turnoff reset */
1120         imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
1121         if (IS_ERR(imx6_pcie->turnoff_reset)) {
1122                 dev_err(dev, "Failed to get TURNOFF reset control\n");
1123                 return PTR_ERR(imx6_pcie->turnoff_reset);
1124         }
1125
1126         /* Grab GPR config register range */
1127         imx6_pcie->iomuxc_gpr =
1128                  syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
1129         if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
1130                 dev_err(dev, "unable to find iomuxc registers\n");
1131                 return PTR_ERR(imx6_pcie->iomuxc_gpr);
1132         }
1133
1134         /* Grab PCIe PHY Tx Settings */
1135         if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
1136                                  &imx6_pcie->tx_deemph_gen1))
1137                 imx6_pcie->tx_deemph_gen1 = 0;
1138
1139         if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
1140                                  &imx6_pcie->tx_deemph_gen2_3p5db))
1141                 imx6_pcie->tx_deemph_gen2_3p5db = 0;
1142
1143         if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
1144                                  &imx6_pcie->tx_deemph_gen2_6db))
1145                 imx6_pcie->tx_deemph_gen2_6db = 20;
1146
1147         if (of_property_read_u32(node, "fsl,tx-swing-full",
1148                                  &imx6_pcie->tx_swing_full))
1149                 imx6_pcie->tx_swing_full = 127;
1150
1151         if (of_property_read_u32(node, "fsl,tx-swing-low",
1152                                  &imx6_pcie->tx_swing_low))
1153                 imx6_pcie->tx_swing_low = 127;
1154
1155         /* Limit link speed */
1156         ret = of_property_read_u32(node, "fsl,max-link-speed",
1157                                    &imx6_pcie->link_gen);
1158         if (ret)
1159                 imx6_pcie->link_gen = 1;
1160
1161         imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
1162         if (IS_ERR(imx6_pcie->vpcie)) {
1163                 if (PTR_ERR(imx6_pcie->vpcie) != -ENODEV)
1164                         return PTR_ERR(imx6_pcie->vpcie);
1165                 imx6_pcie->vpcie = NULL;
1166         }
1167
1168         platform_set_drvdata(pdev, imx6_pcie);
1169
1170         ret = imx6_pcie_attach_pd(dev);
1171         if (ret)
1172                 return ret;
1173
1174         ret = imx6_add_pcie_port(imx6_pcie, pdev);
1175         if (ret < 0)
1176                 return ret;
1177
1178         if (pci_msi_enabled()) {
1179                 u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
1180                 val = dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
1181                 val |= PCI_MSI_FLAGS_ENABLE;
1182                 dw_pcie_writew_dbi(pci, offset + PCI_MSI_FLAGS, val);
1183         }
1184
1185         return 0;
1186 }
1187
1188 static void imx6_pcie_shutdown(struct platform_device *pdev)
1189 {
1190         struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1191
1192         /* bring down link, so bootloader gets clean state in case of reboot */
1193         imx6_pcie_assert_core_reset(imx6_pcie);
1194 }
1195
1196 static const struct imx6_pcie_drvdata drvdata[] = {
1197         [IMX6Q] = {
1198                 .variant = IMX6Q,
1199                 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1200                          IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
1201                 .dbi_length = 0x200,
1202         },
1203         [IMX6SX] = {
1204                 .variant = IMX6SX,
1205                 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1206                          IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1207                          IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1208         },
1209         [IMX6QP] = {
1210                 .variant = IMX6QP,
1211                 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1212                          IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
1213         },
1214         [IMX7D] = {
1215                 .variant = IMX7D,
1216                 .flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1217         },
1218         [IMX8MQ] = {
1219                 .variant = IMX8MQ,
1220         },
1221 };
1222
1223 static const struct of_device_id imx6_pcie_of_match[] = {
1224         { .compatible = "fsl,imx6q-pcie",  .data = &drvdata[IMX6Q],  },
1225         { .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], },
1226         { .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], },
1227         { .compatible = "fsl,imx7d-pcie",  .data = &drvdata[IMX7D],  },
1228         { .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], } ,
1229         {},
1230 };
1231
1232 static struct platform_driver imx6_pcie_driver = {
1233         .driver = {
1234                 .name   = "imx6q-pcie",
1235                 .of_match_table = imx6_pcie_of_match,
1236                 .suppress_bind_attrs = true,
1237                 .pm = &imx6_pcie_pm_ops,
1238                 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1239         },
1240         .probe    = imx6_pcie_probe,
1241         .shutdown = imx6_pcie_shutdown,
1242 };
1243
1244 static void imx6_pcie_quirk(struct pci_dev *dev)
1245 {
1246         struct pci_bus *bus = dev->bus;
1247         struct pcie_port *pp = bus->sysdata;
1248
1249         /* Bus parent is the PCI bridge, its parent is this platform driver */
1250         if (!bus->dev.parent || !bus->dev.parent->parent)
1251                 return;
1252
1253         /* Make sure we only quirk devices associated with this driver */
1254         if (bus->dev.parent->parent->driver != &imx6_pcie_driver.driver)
1255                 return;
1256
1257         if (pci_is_root_bus(bus)) {
1258                 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1259                 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1260
1261                 /*
1262                  * Limit config length to avoid the kernel reading beyond
1263                  * the register set and causing an abort on i.MX 6Quad
1264                  */
1265                 if (imx6_pcie->drvdata->dbi_length) {
1266                         dev->cfg_size = imx6_pcie->drvdata->dbi_length;
1267                         dev_info(&dev->dev, "Limiting cfg_size to %d\n",
1268                                         dev->cfg_size);
1269                 }
1270         }
1271 }
1272 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_SYNOPSYS, 0xabcd,
1273                         PCI_CLASS_BRIDGE_PCI, 8, imx6_pcie_quirk);
1274
1275 static int __init imx6_pcie_init(void)
1276 {
1277 #ifdef CONFIG_ARM
1278         /*
1279          * Since probe() can be deferred we need to make sure that
1280          * hook_fault_code is not called after __init memory is freed
1281          * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1282          * we can install the handler here without risking it
1283          * accessing some uninitialized driver state.
1284          */
1285         hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1286                         "external abort on non-linefetch");
1287 #endif
1288
1289         return platform_driver_register(&imx6_pcie_driver);
1290 }
1291 device_initcall(imx6_pcie_init);