]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/host/pci-imx6.c
1b53010ea49c3baca3dd1cabce62645bc139d2bf
[karo-tx-linux.git] / drivers / pci / host / pci-imx6.c
1 /*
2  * PCIe host controller driver for Freescale i.MX6 SoCs
3  *
4  * Copyright (C) 2014-2015 Freescale Semiconductor, Inc. All Rights Reserved.
5  * Copyright (C) 2013 Kosagi
6  *              http://www.kosagi.com
7  *
8  * Author: Sean Cross <xobs@kosagi.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
22 #include <linux/module.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_address.h>
25 #include <linux/pci.h>
26 #include <linux/pci_regs.h>
27 #include <linux/platform_device.h>
28 #include <linux/regmap.h>
29 #include <linux/busfreq-imx6.h>
30 #include <linux/regulator/consumer.h>
31
32 #include "pcie-designware.h"
33
34 #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
35
36 /*
37  * The default value of the reserved ddr memory
38  * used to verify EP/RC memory space access operations.
39  * The layout of the 1G ddr on SD boards
40  * [others]0x1000_0000 ~ 0x4FFF_FFFF
41  * [imx6sx]0x8000_0000 ~ 0xBFFF_FFFF
42  *
43  */
44 static u32 ddr_test_region = 0, test_region_size = SZ_2M;
45
46 struct imx6_pcie {
47         int                     reset_gpio;
48         int                     power_on_gpio;
49         struct clk              *pcie_bus;
50         struct clk              *pcie_phy;
51         struct clk              *pcie_inbound_axi;
52         struct clk              *ref_100m;
53         struct clk              *pcie;
54         struct pcie_port        pp;
55         struct regmap           *iomuxc_gpr;
56         struct regulator        *pcie_phy_regulator;
57         void __iomem            *mem_base;
58 };
59
60 /* PCIe Root Complex registers (memory-mapped) */
61 #define PCIE_RC_LCR                             0x7c
62 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1        0x1
63 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2        0x2
64 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK        0xf
65
66 /* PCIe Port Logic registers (memory-mapped) */
67 #define PL_OFFSET 0x700
68 #define PCIE_PL_PFLR (PL_OFFSET + 0x08)
69 #define PCIE_PL_PFLR_LINK_STATE_MASK            (0x3f << 16)
70 #define PCIE_PL_PFLR_FORCE_LINK                 (1 << 15)
71 #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
72 #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
73 #define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
74 #define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP          (1 << 4)
75
76 #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
77 #define PCIE_PHY_CTRL_DATA_LOC 0
78 #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
79 #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
80 #define PCIE_PHY_CTRL_WR_LOC 18
81 #define PCIE_PHY_CTRL_RD_LOC 19
82
83 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
84 #define PCIE_PHY_STAT_ACK_LOC 16
85
86 #define PCIE_LINK_WIDTH_SPEED_CONTROL   0x80C
87 #define PORT_LOGIC_SPEED_CHANGE         (0x1 << 17)
88
89 /* PHY registers (not memory-mapped) */
90 #define PCIE_PHY_RX_ASIC_OUT 0x100D
91
92 #define PHY_RX_OVRD_IN_LO 0x1005
93 #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
94 #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
95
96 static inline bool is_imx6sx_pcie(struct imx6_pcie *imx6_pcie)
97 {
98         struct pcie_port *pp = &imx6_pcie->pp;
99         struct device_node *np = pp->dev->of_node;
100
101         return of_device_is_compatible(np, "fsl,imx6sx-pcie");
102 }
103
104 static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
105 {
106         u32 val;
107         u32 max_iterations = 10;
108         u32 wait_counter = 0;
109
110         do {
111                 val = readl(dbi_base + PCIE_PHY_STAT);
112                 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
113                 wait_counter++;
114
115                 if (val == exp_val)
116                         return 0;
117
118                 udelay(1);
119         } while (wait_counter < max_iterations);
120
121         return -ETIMEDOUT;
122 }
123
124 static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
125 {
126         u32 val;
127         int ret;
128
129         val = addr << PCIE_PHY_CTRL_DATA_LOC;
130         writel(val, dbi_base + PCIE_PHY_CTRL);
131
132         val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
133         writel(val, dbi_base + PCIE_PHY_CTRL);
134
135         ret = pcie_phy_poll_ack(dbi_base, 1);
136         if (ret)
137                 return ret;
138
139         val = addr << PCIE_PHY_CTRL_DATA_LOC;
140         writel(val, dbi_base + PCIE_PHY_CTRL);
141
142         ret = pcie_phy_poll_ack(dbi_base, 0);
143         if (ret)
144                 return ret;
145
146         return 0;
147 }
148
149 /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
150 static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
151 {
152         u32 val, phy_ctl;
153         int ret;
154
155         ret = pcie_phy_wait_ack(dbi_base, addr);
156         if (ret)
157                 return ret;
158
159         /* assert Read signal */
160         phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
161         writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
162
163         ret = pcie_phy_poll_ack(dbi_base, 1);
164         if (ret)
165                 return ret;
166
167         val = readl(dbi_base + PCIE_PHY_STAT);
168         *data = val & 0xffff;
169
170         /* deassert Read signal */
171         writel(0x00, dbi_base + PCIE_PHY_CTRL);
172
173         ret = pcie_phy_poll_ack(dbi_base, 0);
174         if (ret)
175                 return ret;
176
177         return 0;
178 }
179
180 static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
181 {
182         u32 var;
183         int ret;
184
185         /* write addr */
186         /* cap addr */
187         ret = pcie_phy_wait_ack(dbi_base, addr);
188         if (ret)
189                 return ret;
190
191         var = data << PCIE_PHY_CTRL_DATA_LOC;
192         writel(var, dbi_base + PCIE_PHY_CTRL);
193
194         /* capture data */
195         var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
196         writel(var, dbi_base + PCIE_PHY_CTRL);
197
198         ret = pcie_phy_poll_ack(dbi_base, 1);
199         if (ret)
200                 return ret;
201
202         /* deassert cap data */
203         var = data << PCIE_PHY_CTRL_DATA_LOC;
204         writel(var, dbi_base + PCIE_PHY_CTRL);
205
206         /* wait for ack de-assertion */
207         ret = pcie_phy_poll_ack(dbi_base, 0);
208         if (ret)
209                 return ret;
210
211         /* assert wr signal */
212         var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
213         writel(var, dbi_base + PCIE_PHY_CTRL);
214
215         /* wait for ack */
216         ret = pcie_phy_poll_ack(dbi_base, 1);
217         if (ret)
218                 return ret;
219
220         /* deassert wr signal */
221         var = data << PCIE_PHY_CTRL_DATA_LOC;
222         writel(var, dbi_base + PCIE_PHY_CTRL);
223
224         /* wait for ack de-assertion */
225         ret = pcie_phy_poll_ack(dbi_base, 0);
226         if (ret)
227                 return ret;
228
229         writel(0x0, dbi_base + PCIE_PHY_CTRL);
230
231         return 0;
232 }
233
234 /*  Added for PCI abort handling */
235 static int imx6q_pcie_abort_handler(unsigned long addr,
236                 unsigned int fsr, struct pt_regs *regs)
237 {
238         return 0;
239 }
240
241 static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
242 {
243         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
244         u32 val, gpr1, gpr12;
245
246         if (is_imx6sx_pcie(imx6_pcie)) {
247                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
248                                 IMX6SX_GPR12_PCIE_TEST_PD,
249                                 IMX6SX_GPR12_PCIE_TEST_PD);
250                 /* Force PCIe PHY reset */
251                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
252                                 IMX6SX_GPR5_PCIE_BTNRST,
253                                 IMX6SX_GPR5_PCIE_BTNRST);
254         } else {
255                 /*
256                  * If the bootloader already enabled the link we need
257                  * some special handling to get the core back into a
258                  * state where it is safe to touch it for configuration.
259                  * As there is no dedicated reset signal wired up for
260                  * MX6QDL, we need to manually force LTSSM into "detect"
261                  * state before completely disabling LTSSM, which is a
262                  * prerequisite for core configuration.
263                  *
264                  * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we
265                  * have a strong indication that the bootloader
266                  * activated the link.
267                  */
268                 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
269                 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
270
271                 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
272                     (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
273                         val = readl(pp->dbi_base + PCIE_PL_PFLR);
274                         val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
275                         val |= PCIE_PL_PFLR_FORCE_LINK;
276                         writel(val, pp->dbi_base + PCIE_PL_PFLR);
277
278                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
279                                         IMX6Q_GPR12_PCIE_CTL_2, 0);
280                 }
281
282                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
283                                 IMX6Q_GPR1_PCIE_TEST_PD,
284                                 IMX6Q_GPR1_PCIE_TEST_PD);
285                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
286                                 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0);
287         }
288
289         return 0;
290 }
291
292 static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
293 {
294         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
295         int ret;
296
297         if (gpio_is_valid(imx6_pcie->power_on_gpio))
298                 gpio_set_value(imx6_pcie->power_on_gpio, 1);
299
300         request_bus_freq(BUS_FREQ_HIGH);
301         ret = clk_prepare_enable(imx6_pcie->pcie_phy);
302         if (ret) {
303                 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
304                 goto err_pcie_phy;
305         }
306
307         if (!IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)
308                         && !IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS)) {
309                 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
310                 if (ret) {
311                         dev_err(pp->dev, "unable to enable pcie_bus clock\n");
312                         goto err_pcie_bus;
313                 }
314         }
315
316         ret = clk_prepare_enable(imx6_pcie->pcie);
317         if (ret) {
318                 dev_err(pp->dev, "unable to enable pcie clock\n");
319                 goto err_pcie;
320         }
321
322         if (is_imx6sx_pcie(imx6_pcie)) {
323                 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
324                 if (ret) {
325                         dev_err(pp->dev, "unable to enable pcie clock\n");
326                         goto err_inbound_axi;
327                 }
328
329                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
330                                 IMX6SX_GPR12_PCIE_TEST_PD, 0);
331         } else {
332                 /* power up core phy and enable ref clock */
333                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
334                                 IMX6Q_GPR1_PCIE_TEST_PD, 0);
335
336                 /* sata_ref is not used by pcie on imx6sx */
337                 ret = clk_prepare_enable(imx6_pcie->ref_100m);
338                 if (ret) {
339                         dev_err(pp->dev, "unable to enable ref_100m\n");
340                         goto err_inbound_axi;
341                 }
342                 /*
343                  * the async reset input need ref clock to sync internally,
344                  * when the ref clock comes after reset, internal synced
345                  * reset time is too short , cannot meet the requirement.
346                  * add one ~10us delay here.
347                  */
348                 udelay(10);
349                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
350                                 IMX6Q_GPR1_PCIE_REF_CLK_EN,
351                                 IMX6Q_GPR1_PCIE_REF_CLK_EN);
352         }
353
354         /* allow the clocks to stabilize */
355         udelay(200);
356
357         /* Some boards don't have PCIe reset GPIO. */
358         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
359                 gpio_set_value(imx6_pcie->reset_gpio, 0);
360                 mdelay(100);
361                 gpio_set_value(imx6_pcie->reset_gpio, 1);
362         }
363
364         /*
365          * Release the PCIe PHY reset here, that we have set in
366          * imx6_pcie_init_phy() now
367          */
368         if (is_imx6sx_pcie(imx6_pcie))
369                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
370                                 IMX6SX_GPR5_PCIE_BTNRST, 0);
371
372         return 0;
373
374 err_inbound_axi:
375         clk_disable_unprepare(imx6_pcie->pcie);
376 err_pcie:
377         if (!IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)
378                         && !IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS))
379                 clk_disable_unprepare(imx6_pcie->pcie_bus);
380 err_pcie_bus:
381         clk_disable_unprepare(imx6_pcie->pcie_phy);
382 err_pcie_phy:
383         return ret;
384
385 }
386
387 static int imx6_pcie_init_phy(struct pcie_port *pp)
388 {
389         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
390         int ret;
391
392         /* Power up the separate domain available on i.MX6SX */
393         if (is_imx6sx_pcie(imx6_pcie)) {
394                 /* Force PCIe PHY reset */
395                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
396                                 IMX6SX_GPR5_PCIE_BTNRST,
397                                 IMX6SX_GPR5_PCIE_BTNRST);
398
399                 regulator_set_voltage(imx6_pcie->pcie_phy_regulator,
400                                 1100000, 1100000);
401                 ret = regulator_enable(imx6_pcie->pcie_phy_regulator);
402                 if (ret) {
403                         dev_err(pp->dev, "failed to enable pcie regulator.\n");
404                         return ret;
405                 }
406                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
407                                 IMX6SX_GPR12_RX_EQ_MASK, IMX6SX_GPR12_RX_EQ_2);
408         }
409
410         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
411                         IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
412
413         /* configure constant input signal to the pcie ctrl and phy */
414         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS))
415                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
416                                 IMX6Q_GPR12_DEVICE_TYPE,
417                                 PCI_EXP_TYPE_ENDPOINT << 12);
418         else
419                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
420                                 IMX6Q_GPR12_DEVICE_TYPE,
421                                 PCI_EXP_TYPE_ROOT_PORT << 12);
422         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
423                         IMX6Q_GPR12_LOS_LEVEL, IMX6Q_GPR12_LOS_LEVEL_9);
424
425         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
426                         IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
427         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
428                         IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
429         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
430                         IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
431         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
432                         IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
433         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
434                         IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
435
436         return 0;
437 }
438
439 static int imx6_pcie_wait_for_link(struct pcie_port *pp)
440 {
441         int count = 200;
442
443         while (!dw_pcie_link_up(pp)) {
444                 udelay(100);
445                 if (--count)
446                         continue;
447
448                 dev_err(pp->dev, "phy link never came up\n");
449                 dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
450                         readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
451                         readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
452                 return -EINVAL;
453         }
454
455         return 0;
456 }
457
458 static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
459 {
460         struct pcie_port *pp = arg;
461
462         return dw_handle_msi_irq(pp);
463 }
464
465 static int imx6_pcie_start_link(struct pcie_port *pp)
466 {
467         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
468         uint32_t tmp;
469         int ret, count;
470
471         /*
472          * Force Gen1 operation when starting the link.  In case the link is
473          * started in Gen2 mode, there is a possibility the devices on the
474          * bus will not be detected at all.  This happens with PCIe switches.
475          */
476         tmp = readl(pp->dbi_base + PCIE_RC_LCR);
477         tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
478         tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
479         writel(tmp, pp->dbi_base + PCIE_RC_LCR);
480
481         /* Start LTSSM. */
482         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
483                         IMX6Q_GPR12_PCIE_CTL_2,
484                         IMX6Q_GPR12_PCIE_CTL_2);
485
486         ret = imx6_pcie_wait_for_link(pp);
487         if (ret)
488                 goto out;
489
490         /* Allow Gen2 mode after the link is up. */
491         tmp = readl(pp->dbi_base + PCIE_RC_LCR);
492         tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
493         tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
494         writel(tmp, pp->dbi_base + PCIE_RC_LCR);
495
496         /*
497          * Start Directed Speed Change so the best possible speed both link
498          * partners support can be negotiated.
499          */
500         tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
501         tmp |= PORT_LOGIC_SPEED_CHANGE;
502         writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
503
504         count = 200;
505         while (count--) {
506                 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
507                 /* Test if the speed change finished. */
508                 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
509                         break;
510                 udelay(100);
511         }
512
513         /* Make sure link training is finished as well! */
514         if (count)
515                 ret = imx6_pcie_wait_for_link(pp);
516         else
517                 ret = -EINVAL;
518
519 out:
520         if (ret) {
521                 dev_err(pp->dev, "Failed to bring link up!\n");
522                 clk_disable_unprepare(imx6_pcie->pcie);
523                 if (!IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)
524                         && !IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS))
525                         clk_disable_unprepare(imx6_pcie->pcie_bus);
526                 clk_disable_unprepare(imx6_pcie->pcie_phy);
527                 if (is_imx6sx_pcie(imx6_pcie)) {
528                         /* Disable clks and power down PCIe PHY */
529                         clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
530                         release_bus_freq(BUS_FREQ_HIGH);
531
532                         /* Put PCIe PHY to be isolation */
533                         regmap_update_bits(imx6_pcie->iomuxc_gpr,
534                                         IOMUXC_GPR0, BIT(6), 1 << 6);
535
536                         /*
537                          * Power down PCIe PHY.
538                          */
539                         regulator_disable(imx6_pcie->pcie_phy_regulator);
540                 } else {
541                         clk_disable_unprepare(imx6_pcie->ref_100m);
542                         release_bus_freq(BUS_FREQ_HIGH);
543                 }
544         } else {
545                 tmp = readl(pp->dbi_base + 0x80);
546                 dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
547         }
548
549         return ret;
550 }
551
552 static int imx6_pcie_host_init(struct pcie_port *pp)
553 {
554         int ret;
555
556         imx6_pcie_assert_core_reset(pp);
557
558         ret = imx6_pcie_init_phy(pp);
559         if (ret < 0)
560                 return ret;
561
562         ret = imx6_pcie_deassert_core_reset(pp);
563         if (ret < 0)
564                 return ret;
565
566         dw_pcie_setup_rc(pp);
567
568         ret = imx6_pcie_start_link(pp);
569         if (ret < 0)
570                 return ret;
571
572         if (IS_ENABLED(CONFIG_PCI_MSI))
573                 dw_pcie_msi_init(pp);
574
575         return 0;
576 }
577
578 static void imx6_pcie_reset_phy(struct pcie_port *pp)
579 {
580         uint32_t temp;
581
582         pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
583         temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
584                  PHY_RX_OVRD_IN_LO_RX_PLL_EN);
585         pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, temp);
586
587         udelay(2000);
588
589         pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
590         temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
591                   PHY_RX_OVRD_IN_LO_RX_PLL_EN);
592         pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, temp);
593 }
594
595 static int imx6_pcie_link_up(struct pcie_port *pp)
596 {
597         u32 rc, debug_r0, rx_valid;
598         int count = 500;
599
600         /*
601          * Test if the PHY reports that the link is up and also that the LTSSM
602          * training finished. There are three possible states of the link when
603          * this code is called:
604          * 1) The link is DOWN (unlikely)
605          *     The link didn't come up yet for some reason. This usually means
606          *     we have a real problem somewhere. Reset the PHY and exit. This
607          *     state calls for inspection of the DEBUG registers.
608          * 2) The link is UP, but still in LTSSM training
609          *     Wait for the training to finish, which should take a very short
610          *     time. If the training does not finish, we have a problem and we
611          *     need to inspect the DEBUG registers. If the training does finish,
612          *     the link is up and operating correctly.
613          * 3) The link is UP and no longer in LTSSM training
614          *     The link is up and operating correctly.
615          */
616         while (1) {
617                 rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
618                 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP))
619                         break;
620                 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING))
621                         return 1;
622                 if (!count--)
623                         break;
624                 dev_dbg(pp->dev, "Link is up, but still in training\n");
625                 /*
626                  * Wait a little bit, then re-check if the link finished
627                  * the training.
628                  */
629                 udelay(10);
630         }
631         /*
632          * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
633          * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
634          * If (MAC/LTSSM.state == Recovery.RcvrLock)
635          * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
636          * to gen2 is stuck
637          */
638         pcie_phy_read(pp->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
639         debug_r0 = readl(pp->dbi_base + PCIE_PHY_DEBUG_R0);
640
641         if (rx_valid & 0x01)
642                 return 0;
643
644         if ((debug_r0 & 0x3f) != 0x0d)
645                 return 0;
646
647         dev_err(pp->dev, "transition to gen2 is stuck, reset PHY!\n");
648         dev_dbg(pp->dev, "debug_r0=%08x debug_r1=%08x\n", debug_r0, rc);
649
650         imx6_pcie_reset_phy(pp);
651
652         return 0;
653 }
654
655 static struct pcie_host_ops imx6_pcie_host_ops = {
656         .link_up = imx6_pcie_link_up,
657         .host_init = imx6_pcie_host_init,
658 };
659
660 static int __init imx6_add_pcie_port(struct pcie_port *pp,
661                         struct platform_device *pdev)
662 {
663         int ret;
664
665         if (IS_ENABLED(CONFIG_PCI_MSI)) {
666                 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
667                 if (pp->msi_irq <= 0) {
668                         dev_err(&pdev->dev, "failed to get MSI irq\n");
669                         return -ENODEV;
670                 }
671
672                 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
673                                        imx6_pcie_msi_handler,
674                                        IRQF_SHARED, "mx6-pcie-msi", pp);
675                 if (ret) {
676                         dev_err(&pdev->dev, "failed to request MSI irq\n");
677                         return -ENODEV;
678                 }
679         }
680
681         pp->root_bus_nr = -1;
682         pp->ops = &imx6_pcie_host_ops;
683
684         ret = dw_pcie_host_init(pp);
685         if (ret) {
686                 dev_err(&pdev->dev, "failed to initialize host\n");
687                 return ret;
688         }
689
690         return 0;
691 }
692
693 static ssize_t imx_pcie_bar0_addr_info(struct device *dev,
694                 struct device_attribute *devattr, char *buf)
695 {
696         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
697         struct pcie_port *pp = &imx6_pcie->pp;
698
699         return sprintf(buf, "imx-pcie-bar0-addr-info start 0x%08x\n",
700                         readl(pp->dbi_base + PCI_BASE_ADDRESS_0));
701 }
702
703 static ssize_t imx_pcie_bar0_addr_start(struct device *dev,
704                 struct device_attribute *attr, const char *buf, size_t count)
705 {
706         u32 bar_start;
707         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
708         struct pcie_port *pp = &imx6_pcie->pp;
709
710         sscanf(buf, "%x\n", &bar_start);
711         writel(bar_start, pp->dbi_base + PCI_BASE_ADDRESS_0);
712
713         return count;
714 }
715
716 static void imx_pcie_regions_setup(struct device *dev)
717 {
718         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
719         struct pcie_port *pp = &imx6_pcie->pp;
720
721         if (is_imx6sx_pcie(imx6_pcie) && ddr_test_region == 0)
722                 ddr_test_region = 0xb0000000;
723         else if (ddr_test_region == 0)
724                 ddr_test_region = 0x40000000;
725
726         /*
727          * region2 outbound used to access rc/ep mem
728          * in imx6 pcie ep/rc validation system
729          */
730         writel(2, pp->dbi_base + 0x900);
731         writel(pp->mem_base, pp->dbi_base + 0x90c);
732         writel(0, pp->dbi_base + 0x910);
733         writel(pp->mem_base + test_region_size, pp->dbi_base + 0x914);
734
735         writel(ddr_test_region, pp->dbi_base + 0x918);
736         writel(0, pp->dbi_base + 0x91c);
737         writel(0, pp->dbi_base + 0x904);
738         writel(1 << 31, pp->dbi_base + 0x908);
739 }
740
741 static ssize_t imx_pcie_memw_info(struct device *dev,
742                 struct device_attribute *devattr, char *buf)
743 {
744         return sprintf(buf, "imx-pcie-rc-memw-info start 0x%08x, size 0x%08x\n",
745                         ddr_test_region, test_region_size);
746 }
747
748 static ssize_t
749 imx_pcie_memw_start(struct device *dev, struct device_attribute *attr,
750                 const char *buf, size_t count)
751 {
752         u32 memw_start;
753         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
754
755         sscanf(buf, "%x\n", &memw_start);
756
757         if (is_imx6sx_pcie(imx6_pcie)) {
758                 if (memw_start < 0x80000000 || memw_start > 0xb0000000) {
759                         dev_err(dev, "Invalid imx6sx sdb memory start addr.\n");
760                         dev_info(dev, "e.x: echo 0xb1000000 > /sys/...");
761                         return -1;
762                 }
763         } else {
764                 if (memw_start < 0x10000000 || memw_start > 0x40000000) {
765                         dev_err(dev, "Invalid imx6q sd memory start addr.\n");
766                         dev_info(dev, "e.x: echo 0xb1000000 > /sys/...");
767                         return -1;
768                 }
769         }
770
771         if (ddr_test_region != memw_start) {
772                 ddr_test_region = memw_start;
773                 imx_pcie_regions_setup(dev);
774         }
775
776         return count;
777 }
778
779 static ssize_t
780 imx_pcie_memw_size(struct device *dev, struct device_attribute *attr,
781                 const char *buf, size_t count)
782 {
783         u32 memw_size;
784
785         sscanf(buf, "%x\n", &memw_size);
786
787         if ((memw_size > (SZ_16M - SZ_1M)) || (memw_size < SZ_64K)) {
788                 dev_err(dev, "Invalid, should be [SZ_64K,SZ_16M - SZ_1MB].\n");
789                 dev_info(dev, "For example: echo 0x800000 > /sys/...");
790                 return -1;
791         }
792
793         if (test_region_size != memw_size) {
794                 test_region_size = memw_size;
795                 imx_pcie_regions_setup(dev);
796         }
797
798         return count;
799 }
800
801 static DEVICE_ATTR(memw_info, S_IRUGO, imx_pcie_memw_info, NULL);
802 static DEVICE_ATTR(memw_start_set, S_IWUGO, NULL, imx_pcie_memw_start);
803 static DEVICE_ATTR(memw_size_set, S_IWUGO, NULL, imx_pcie_memw_size);
804 static DEVICE_ATTR(ep_bar0_addr, S_IRWXUGO, imx_pcie_bar0_addr_info,
805                 imx_pcie_bar0_addr_start);
806
807 static struct attribute *imx_pcie_attrs[] = {
808         /*
809          * The start address, and the limitation (64KB ~ (16MB - 1MB))
810          * of the ddr mem window reserved by RC, and used for EP to access.
811          * BTW, these attrs are only configured at EP side.
812          */
813         &dev_attr_memw_info.attr,
814         &dev_attr_memw_start_set.attr,
815         &dev_attr_memw_size_set.attr,
816         &dev_attr_ep_bar0_addr.attr,
817         NULL
818 };
819
820 static struct attribute_group imx_pcie_attrgroup = {
821         .attrs  = imx_pcie_attrs,
822 };
823
824 static void imx6_pcie_setup_ep(struct pcie_port *pp)
825 {
826                 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
827                 writel(readl(pp->dbi_base + PCI_COMMAND)
828                                 | PCI_COMMAND_IO
829                                 | PCI_COMMAND_MEMORY
830                                 | PCI_COMMAND_MASTER,
831                                 pp->dbi_base + PCI_COMMAND);
832
833                 /*
834                  * configure the class_rev(emaluate one memory ram ep device),
835                  * bar0 and bar1 of ep
836                  */
837                 writel(0xdeadbeaf, pp->dbi_base + PCI_VENDOR_ID);
838                 writel(readl(pp->dbi_base + PCI_CLASS_REVISION)
839                                 | (PCI_CLASS_MEMORY_RAM << 16),
840                                 pp->dbi_base + PCI_CLASS_REVISION);
841                 writel(0xdeadbeaf, pp->dbi_base
842                                 + PCI_SUBSYSTEM_VENDOR_ID);
843
844                 /* 32bit none-prefetchable 8M bytes memory on bar0 */
845                 writel(0x0, pp->dbi_base + PCI_BASE_ADDRESS_0);
846                 writel(SZ_8M - 1, pp->dbi_base + (1 << 12)
847                                 + PCI_BASE_ADDRESS_0);
848
849                 /* None used bar1 */
850                 writel(0x0, pp->dbi_base + PCI_BASE_ADDRESS_1);
851                 writel(0, pp->dbi_base + (1 << 12) + PCI_BASE_ADDRESS_1);
852
853                 /* 4K bytes IO on bar2 */
854                 writel(0x1, pp->dbi_base + PCI_BASE_ADDRESS_2);
855                 writel(SZ_4K - 1, pp->dbi_base + (1 << 12) +
856                                 PCI_BASE_ADDRESS_2);
857
858                 /*
859                  * 32bit prefetchable 1M bytes memory on bar3
860                  * FIXME BAR MASK3 is not changable, the size
861                  * is fixed to 256 bytes.
862                  */
863                 writel(0x8, pp->dbi_base + PCI_BASE_ADDRESS_3);
864                 writel(SZ_1M - 1, pp->dbi_base + (1 << 12)
865                                 + PCI_BASE_ADDRESS_3);
866
867                 /*
868                  * 64bit prefetchable 1M bytes memory on bar4-5.
869                  * FIXME BAR4,5 are not enabled yet
870                  */
871                 writel(0xc, pp->dbi_base + PCI_BASE_ADDRESS_4);
872                 writel(SZ_1M - 1, pp->dbi_base + (1 << 12)
873                                 + PCI_BASE_ADDRESS_4);
874                 writel(0, pp->dbi_base + (1 << 12) + PCI_BASE_ADDRESS_5);
875 }
876
877 #ifdef CONFIG_PM_SLEEP
878 static int pci_imx_suspend_noirq(struct device *dev)
879 {
880         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
881         struct pcie_port *pp = &imx6_pcie->pp;
882
883         if (is_imx6sx_pcie(imx6_pcie)) {
884                 if (IS_ENABLED(CONFIG_PCI_MSI))
885                         dw_pcie_msi_cfg_store(pp);
886
887                 if (IS_ENABLED(CONFIG_PCI_IMX6SX_EXTREMELY_PWR_SAVE)) {
888                         /* Disable clks and power down PCIe PHY */
889                         clk_disable_unprepare(imx6_pcie->pcie);
890                         if (!IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)
891                                 && !IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS))
892                                 clk_disable_unprepare(imx6_pcie->pcie_bus);
893                         clk_disable_unprepare(imx6_pcie->pcie_phy);
894                         clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
895                         release_bus_freq(BUS_FREQ_HIGH);
896
897                         /* Put PCIe PHY to be isolation */
898                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR0,
899                                         BIT(6), 1 << 6);
900
901                         /*
902                          * Power down PCIe PHY.
903                          */
904                         regulator_disable(imx6_pcie->pcie_phy_regulator);
905                 } else {
906                         /* PM_TURN_OFF */
907                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
908                                         IMX6SX_GPR12_PCIE_PM_TURN_OFF,
909                                         IMX6SX_GPR12_PCIE_PM_TURN_OFF);
910                         udelay(10);
911                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
912                                         IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
913                         clk_disable_unprepare(imx6_pcie->pcie);
914                         clk_disable_unprepare(imx6_pcie->pcie_bus);
915                         clk_disable_unprepare(imx6_pcie->pcie_phy);
916                         clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
917                         release_bus_freq(BUS_FREQ_HIGH);
918                 }
919         } else {
920                 /*
921                  * L2 can exit by 'reset' or Inband beacon (from remote EP)
922                  * toggling phy_powerdown has same effect as 'inband beacon'
923                  * So, toggle bit18 of GPR1, used as a workaround of errata
924                  * "PCIe PCIe does not support L2 Power Down"
925                  */
926                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
927                                 IMX6Q_GPR1_PCIE_TEST_PD,
928                                 IMX6Q_GPR1_PCIE_TEST_PD);
929         }
930
931         return 0;
932 }
933
934 static int pci_imx_resume_noirq(struct device *dev)
935 {
936         int ret = 0;
937         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
938         struct pcie_port *pp = &imx6_pcie->pp;
939
940         if (is_imx6sx_pcie(imx6_pcie)) {
941                 if (IS_ENABLED(CONFIG_PCI_IMX6SX_EXTREMELY_PWR_SAVE)) {
942                         imx6_pcie_assert_core_reset(pp);
943
944                         ret = imx6_pcie_init_phy(pp);
945                         if (ret < 0)
946                                 return ret;
947
948                         ret = imx6_pcie_deassert_core_reset(pp);
949                         if (ret < 0)
950                                 return ret;
951
952                         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS))
953                                 imx6_pcie_setup_ep(pp);
954                         else
955                                 dw_pcie_setup_rc(pp);
956
957                         if (IS_ENABLED(CONFIG_PCI_MSI))
958                                 dw_pcie_msi_cfg_restore(pp);
959
960                         ret = imx6_pcie_start_link(pp);
961                         if (ret < 0)
962                                 return ret;
963                 } else {
964                         request_bus_freq(BUS_FREQ_HIGH);
965                         clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
966                         clk_prepare_enable(imx6_pcie->pcie_bus);
967                         clk_prepare_enable(imx6_pcie->pcie_phy);
968                         clk_prepare_enable(imx6_pcie->pcie);
969
970                         /* Reset iMX6SX PCIe */
971                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
972                                         IMX6SX_GPR5_PCIE_PERST,
973                                         IMX6SX_GPR5_PCIE_PERST);
974                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
975                                         IMX6SX_GPR5_PCIE_PERST, 0);
976                         /*
977                          * controller maybe turn off, re-configure again
978                          */
979                         dw_pcie_setup_rc(pp);
980
981                         if (IS_ENABLED(CONFIG_PCI_MSI))
982                                 dw_pcie_msi_cfg_restore(pp);
983                 }
984         } else {
985                 /*
986                  * L2 can exit by 'reset' or Inband beacon (from remote EP)
987                  * toggling phy_powerdown has same effect as 'inband beacon'
988                  * So, toggle bit18 of GPR1, used as a workaround of errata
989                  * "PCIe PCIe does not support L2 Power Down"
990                  */
991                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
992                                 IMX6Q_GPR1_PCIE_TEST_PD, 0);
993         }
994
995         return 0;
996 }
997
998 static const struct dev_pm_ops pci_imx_pm_ops = {
999         .suspend_noirq = pci_imx_suspend_noirq,
1000         .resume_noirq = pci_imx_resume_noirq,
1001         .freeze_noirq = pci_imx_suspend_noirq,
1002         .thaw_noirq = pci_imx_resume_noirq,
1003         .poweroff_noirq = pci_imx_suspend_noirq,
1004         .restore_noirq = pci_imx_resume_noirq,
1005 };
1006 #endif
1007
1008 static int __init imx6_pcie_probe(struct platform_device *pdev)
1009 {
1010         struct imx6_pcie *imx6_pcie;
1011         struct pcie_port *pp;
1012         struct device_node *np = pdev->dev.of_node;
1013         struct resource *dbi_base;
1014         int ret;
1015
1016         imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
1017         if (!imx6_pcie)
1018                 return -ENOMEM;
1019
1020         pp = &imx6_pcie->pp;
1021         pp->dev = &pdev->dev;
1022
1023         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)) {
1024                 /* add attributes for device */
1025                 ret = sysfs_create_group(&pdev->dev.kobj, &imx_pcie_attrgroup);
1026                 if (ret)
1027                         return -EINVAL;
1028         }
1029
1030         /* Added for PCI abort handling */
1031         hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
1032                 "imprecise external abort");
1033
1034         dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1035         pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
1036         if (IS_ERR(pp->dbi_base))
1037                 return PTR_ERR(pp->dbi_base);
1038
1039         /* Fetch GPIOs */
1040         imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
1041         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
1042                 ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
1043                                             GPIOF_OUT_INIT_LOW, "PCIe reset");
1044                 if (ret) {
1045                         dev_err(&pdev->dev, "unable to get reset gpio\n");
1046                         return ret;
1047                 }
1048         }
1049
1050         imx6_pcie->power_on_gpio = of_get_named_gpio(np, "power-on-gpio", 0);
1051         if (gpio_is_valid(imx6_pcie->power_on_gpio)) {
1052                 ret = devm_gpio_request_one(&pdev->dev,
1053                                         imx6_pcie->power_on_gpio,
1054                                         GPIOF_OUT_INIT_LOW,
1055                                         "PCIe power enable");
1056                 if (ret) {
1057                         dev_err(&pdev->dev, "unable to get power-on gpio\n");
1058                         return ret;
1059                 }
1060         }
1061
1062         /* Fetch clocks */
1063         imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
1064         if (IS_ERR(imx6_pcie->pcie_phy)) {
1065                 dev_err(&pdev->dev,
1066                         "pcie_phy clock source missing or invalid\n");
1067                 return PTR_ERR(imx6_pcie->pcie_phy);
1068         }
1069
1070         imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
1071         if (IS_ERR(imx6_pcie->pcie_bus)) {
1072                 dev_err(&pdev->dev,
1073                         "pcie_bus clock source missing or invalid\n");
1074                 return PTR_ERR(imx6_pcie->pcie_bus);
1075         }
1076
1077         imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
1078         if (IS_ERR(imx6_pcie->pcie)) {
1079                 dev_err(&pdev->dev,
1080                         "pcie clock source missing or invalid\n");
1081                 return PTR_ERR(imx6_pcie->pcie);
1082         }
1083
1084         if (is_imx6sx_pcie(imx6_pcie)) {
1085                 imx6_pcie->pcie_inbound_axi = devm_clk_get(&pdev->dev,
1086                                 "pcie_inbound_axi");
1087                 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
1088                         dev_err(&pdev->dev,
1089                                 "pcie clock source missing or invalid\n");
1090                         return PTR_ERR(imx6_pcie->pcie_inbound_axi);
1091                 }
1092
1093                 imx6_pcie->pcie_phy_regulator = devm_regulator_get(pp->dev,
1094                                 "pcie-phy");
1095
1096                 imx6_pcie->iomuxc_gpr =
1097                          syscon_regmap_lookup_by_compatible
1098                          ("fsl,imx6sx-iomuxc-gpr");
1099         } else {
1100                 /* sata_ref is not used by pcie on imx6sx */
1101                 imx6_pcie->ref_100m = devm_clk_get(&pdev->dev, "ref_100m");
1102                 if (IS_ERR(imx6_pcie->ref_100m)) {
1103                         dev_err(&pdev->dev,
1104                                 "ref_100m clock source missing or invalid\n");
1105                         return PTR_ERR(imx6_pcie->ref_100m);
1106                 }
1107
1108                 imx6_pcie->iomuxc_gpr =
1109                         syscon_regmap_lookup_by_compatible
1110                         ("fsl,imx6q-iomuxc-gpr");
1111         }
1112         if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
1113                 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
1114                 return PTR_ERR(imx6_pcie->iomuxc_gpr);
1115         }
1116
1117         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)) {
1118                 int i;
1119                 void *test_reg1, *test_reg2;
1120                 void __iomem *pcie_arb_base_addr;
1121                 struct timeval tv1, tv2, tv3;
1122                 u32 tv_count1, tv_count2;
1123                 struct device_node *np = pp->dev->of_node;
1124                 struct of_pci_range range;
1125                 struct of_pci_range_parser parser;
1126                 unsigned long restype;
1127
1128                 if (of_pci_range_parser_init(&parser, np)) {
1129                         dev_err(pp->dev, "missing ranges property\n");
1130                         return -EINVAL;
1131                 }
1132
1133                 /* Get the memory ranges from DT */
1134                 for_each_of_pci_range(&parser, &range) {
1135                         restype = range.flags & IORESOURCE_TYPE_BITS;
1136                         if (restype == IORESOURCE_MEM) {
1137                                 of_pci_range_to_resource(&range,
1138                                                 np, &pp->mem);
1139                                 pp->mem.name = "MEM";
1140                         }
1141                 }
1142
1143                 pp->mem_base = pp->mem.start;
1144
1145                 imx6_pcie_assert_core_reset(pp);
1146                 ret = imx6_pcie_init_phy(pp);
1147                 ret |= imx6_pcie_deassert_core_reset(pp);
1148                 if (ret < 0) {
1149                         dev_err(&pdev->dev, "unable to init pcie ep.\n");
1150                         return ret;
1151                 }
1152
1153                 /*
1154                  * iMX6SX PCIe has the stand-alone power domain.
1155                  * refer to the initialization for iMX6SX PCIe,
1156                  * release the PCIe PHY reset here,
1157                  * before LTSSM enable is set
1158                  * .
1159                  */
1160                 if (is_imx6sx_pcie(imx6_pcie))
1161                         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
1162                                         BIT(19), 0 << 19);
1163
1164                 /* assert LTSSM enable */
1165                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
1166                                 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
1167
1168                 dev_info(&pdev->dev, "PCIe EP: waiting for link up...\n");
1169
1170                 platform_set_drvdata(pdev, imx6_pcie);
1171                 /* link is indicated by the bit4 of DB_R1 register */
1172                 do {
1173                         usleep_range(10, 20);
1174                 } while ((readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & 0x10) == 0);
1175
1176                 imx6_pcie_setup_ep(pp);
1177
1178                 imx_pcie_regions_setup(&pdev->dev);
1179
1180                 /* self io test */
1181                 test_reg1 = devm_kzalloc(&pdev->dev,
1182                                 test_region_size, GFP_KERNEL);
1183                 if (!test_reg1) {
1184                         pr_err("pcie ep: can't alloc the test reg1.\n");
1185                         ret = PTR_ERR(test_reg1);
1186                         goto err;
1187                 }
1188
1189                 test_reg2 = devm_kzalloc(&pdev->dev,
1190                                 test_region_size, GFP_KERNEL);
1191                 if (!test_reg2) {
1192                         pr_err("pcie ep: can't alloc the test reg2.\n");
1193                         ret = PTR_ERR(test_reg1);
1194                         goto err;
1195                 }
1196
1197                 pcie_arb_base_addr = ioremap_cache(pp->mem_base,
1198                                 test_region_size);
1199
1200                 if (!pcie_arb_base_addr) {
1201                         pr_err("error with ioremap in ep selftest\n");
1202                         ret = PTR_ERR(pcie_arb_base_addr);
1203                         goto err;
1204                 }
1205
1206                 for (i = 0; i < test_region_size; i = i + 4) {
1207                         writel(0xE6600D00 + i, test_reg1 + i);
1208                         writel(0xDEADBEAF, test_reg2 + i);
1209                 }
1210
1211                 /* PCIe EP start the data transfer after link up */
1212                 pr_info("pcie ep: Starting data transfer...\n");
1213                 do_gettimeofday(&tv1);
1214
1215                 memcpy((unsigned long *)pcie_arb_base_addr,
1216                                 (unsigned long *)test_reg1,
1217                                 test_region_size);
1218
1219                 do_gettimeofday(&tv2);
1220
1221                 memcpy((unsigned long *)test_reg2,
1222                                 (unsigned long *)pcie_arb_base_addr,
1223                                 test_region_size);
1224
1225                 do_gettimeofday(&tv3);
1226
1227                 if (memcmp(test_reg2, test_reg1, test_region_size) == 0) {
1228                         tv_count1 = (tv2.tv_sec - tv1.tv_sec)
1229                                 * USEC_PER_SEC
1230                                 + tv2.tv_usec - tv1.tv_usec;
1231                         tv_count2 = (tv3.tv_sec - tv2.tv_sec)
1232                                 * USEC_PER_SEC
1233                                 + tv3.tv_usec - tv2.tv_usec;
1234
1235                         pr_info("pcie ep: Data transfer is successful."
1236                                         " tv_count1 %dus,"
1237                                         " tv_count2 %dus.\n",
1238                                         tv_count1, tv_count2);
1239                         pr_info("pcie ep: Data write speed:%ldMB/s.\n",
1240                                         ((test_region_size/1024)
1241                                            * MSEC_PER_SEC)
1242                                         /(tv_count1));
1243                         pr_info("pcie ep: Data read speed:%ldMB/s.\n",
1244                                         ((test_region_size/1024)
1245                                            * MSEC_PER_SEC)
1246                                         /(tv_count2));
1247                 } else {
1248                         pr_info("pcie ep: Data transfer is failed.\n");
1249                 } /* end of self io test. */
1250         } else {
1251                 ret = imx6_add_pcie_port(pp, pdev);
1252                 if (ret < 0)
1253                         return ret;
1254
1255                 platform_set_drvdata(pdev, imx6_pcie);
1256
1257                 if (IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS))
1258                         imx_pcie_regions_setup(&pdev->dev);
1259         }
1260
1261         return 0;
1262 err:
1263         return ret;
1264 }
1265
1266 static void imx6_pcie_shutdown(struct platform_device *pdev)
1267 {
1268         struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1269
1270         /* bring down link, so bootloader gets clean state in case of reboot */
1271         imx6_pcie_assert_core_reset(&imx6_pcie->pp);
1272 }
1273
1274 static const struct of_device_id imx6_pcie_of_match[] = {
1275         { .compatible = "fsl,imx6q-pcie", },
1276         { .compatible = "fsl,imx6sx-pcie", },
1277         {},
1278 };
1279 MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
1280
1281 static struct platform_driver imx6_pcie_driver = {
1282         .driver = {
1283                 .name   = "imx6q-pcie",
1284                 .owner  = THIS_MODULE,
1285                 .of_match_table = imx6_pcie_of_match,
1286                 .pm = &pci_imx_pm_ops,
1287         },
1288         .shutdown = imx6_pcie_shutdown,
1289 };
1290
1291 /* Freescale PCIe driver does not allow module unload */
1292
1293 static int __init imx6_pcie_init(void)
1294 {
1295         return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
1296 }
1297 module_init(imx6_pcie_init);
1298
1299 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
1300 MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
1301 MODULE_LICENSE("GPL v2");