]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/host/pci-imx6.c
0b963c88f8197b14ea871c21a75ba2cef751f850
[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) 2013 Kosagi
5  *              http://www.kosagi.com
6  *
7  * Author: Sean Cross <xobs@kosagi.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
21 #include <linux/module.h>
22 #include <linux/of_gpio.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/resource.h>
27 #include <linux/signal.h>
28 #include <linux/types.h>
29 #include <linux/busfreq-imx6.h>
30
31 #include "pcie-designware.h"
32
33 #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
34
35 /*
36  * The default values of the RC's reserved ddr memory
37  * used to verify EP mode.
38  * BTW, here is the layout of the 1G ddr on SD boards
39  * 0x1000_0000 ~ 0x4FFF_FFFF
40  */
41 static u32 rc_ddr_test_region = 0x40000000;
42 static u32 test_region_size = SZ_2M;
43
44 struct imx6_pcie {
45         int                     reset_gpio;
46         int                     power_on_gpio;
47         int                     wake_up_gpio;
48         int                     disable_gpio;
49         struct clk              *lvds_gate;
50         struct clk              *sata_ref_100m;
51         struct clk              *pcie_ref_125m;
52         struct clk              *pcie_axi;
53         struct pcie_port        pp;
54         struct regmap           *iomuxc_gpr;
55         void __iomem            *mem_base;
56 };
57
58 /* PCIe Port Logic registers (memory-mapped) */
59 #define PL_OFFSET 0x700
60 #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
61 #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
62
63 #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
64 #define PCIE_PHY_CTRL_DATA_LOC 0
65 #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
66 #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
67 #define PCIE_PHY_CTRL_WR_LOC 18
68 #define PCIE_PHY_CTRL_RD_LOC 19
69
70 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
71 #define PCIE_PHY_STAT_ACK_LOC 16
72
73 /* PHY registers (not memory-mapped) */
74 #define PCIE_PHY_RX_ASIC_OUT 0x100D
75
76 #define PHY_RX_OVRD_IN_LO 0x1005
77 #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
78 #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
79
80 static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
81 {
82         u32 val;
83         u32 max_iterations = 10;
84         u32 wait_counter = 0;
85
86         do {
87                 val = readl(dbi_base + PCIE_PHY_STAT);
88                 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
89                 wait_counter++;
90
91                 if (val == exp_val)
92                         return 0;
93
94                 udelay(1);
95         } while (wait_counter < max_iterations);
96
97         return -ETIMEDOUT;
98 }
99
100 static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
101 {
102         u32 val;
103         int ret;
104
105         val = addr << PCIE_PHY_CTRL_DATA_LOC;
106         writel(val, dbi_base + PCIE_PHY_CTRL);
107
108         val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
109         writel(val, dbi_base + PCIE_PHY_CTRL);
110
111         ret = pcie_phy_poll_ack(dbi_base, 1);
112         if (ret)
113                 return ret;
114
115         val = addr << PCIE_PHY_CTRL_DATA_LOC;
116         writel(val, dbi_base + PCIE_PHY_CTRL);
117
118         ret = pcie_phy_poll_ack(dbi_base, 0);
119         if (ret)
120                 return ret;
121
122         return 0;
123 }
124
125 /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
126 static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
127 {
128         u32 val, phy_ctl;
129         int ret;
130
131         ret = pcie_phy_wait_ack(dbi_base, addr);
132         if (ret)
133                 return ret;
134
135         /* assert Read signal */
136         phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
137         writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
138
139         ret = pcie_phy_poll_ack(dbi_base, 1);
140         if (ret)
141                 return ret;
142
143         val = readl(dbi_base + PCIE_PHY_STAT);
144         *data = val & 0xffff;
145
146         /* deassert Read signal */
147         writel(0x00, dbi_base + PCIE_PHY_CTRL);
148
149         ret = pcie_phy_poll_ack(dbi_base, 0);
150         if (ret)
151                 return ret;
152
153         return 0;
154 }
155
156 static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
157 {
158         u32 var;
159         int ret;
160
161         /* write addr */
162         /* cap addr */
163         ret = pcie_phy_wait_ack(dbi_base, addr);
164         if (ret)
165                 return ret;
166
167         var = data << PCIE_PHY_CTRL_DATA_LOC;
168         writel(var, dbi_base + PCIE_PHY_CTRL);
169
170         /* capture data */
171         var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
172         writel(var, dbi_base + PCIE_PHY_CTRL);
173
174         ret = pcie_phy_poll_ack(dbi_base, 1);
175         if (ret)
176                 return ret;
177
178         /* deassert cap data */
179         var = data << PCIE_PHY_CTRL_DATA_LOC;
180         writel(var, dbi_base + PCIE_PHY_CTRL);
181
182         /* wait for ack de-assertion */
183         ret = pcie_phy_poll_ack(dbi_base, 0);
184         if (ret)
185                 return ret;
186
187         /* assert wr signal */
188         var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
189         writel(var, dbi_base + PCIE_PHY_CTRL);
190
191         /* wait for ack */
192         ret = pcie_phy_poll_ack(dbi_base, 1);
193         if (ret)
194                 return ret;
195
196         /* deassert wr signal */
197         var = data << PCIE_PHY_CTRL_DATA_LOC;
198         writel(var, dbi_base + PCIE_PHY_CTRL);
199
200         /* wait for ack de-assertion */
201         ret = pcie_phy_poll_ack(dbi_base, 0);
202         if (ret)
203                 return ret;
204
205         writel(0x0, dbi_base + PCIE_PHY_CTRL);
206
207         return 0;
208 }
209
210 /*  Added for PCI abort handling */
211 static int imx6q_pcie_abort_handler(unsigned long addr,
212                 unsigned int fsr, struct pt_regs *regs)
213 {
214         return 0;
215 }
216
217 static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
218 {
219         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
220         int ret;
221
222         if (gpio_is_valid(imx6_pcie->power_on_gpio))
223                 gpio_set_value(imx6_pcie->power_on_gpio, 1);
224
225         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
226                         IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
227         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
228                         IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
229         request_bus_freq(BUS_FREQ_HIGH);
230
231         ret = clk_prepare_enable(imx6_pcie->sata_ref_100m);
232         if (ret) {
233                 dev_err(pp->dev, "unable to enable sata_ref_100m\n");
234                 goto err_sata_ref;
235         }
236
237         ret = clk_prepare_enable(imx6_pcie->pcie_ref_125m);
238         if (ret) {
239                 dev_err(pp->dev, "unable to enable pcie_ref_125m\n");
240                 goto err_pcie_ref;
241         }
242
243         if (!IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)
244                         && !IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS)) {
245                 ret = clk_prepare_enable(imx6_pcie->lvds_gate);
246                 if (ret) {
247                         dev_err(pp->dev, "unable to enable lvds_gate\n");
248                         goto err_lvds_gate;
249                 }
250         }
251
252         ret = clk_prepare_enable(imx6_pcie->pcie_axi);
253         if (ret) {
254                 dev_err(pp->dev, "unable to enable pcie_axi\n");
255                 goto err_pcie_axi;
256         }
257
258         /* allow the clocks to stabilize */
259         usleep_range(200, 500);
260
261         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
262                 gpio_set_value(imx6_pcie->reset_gpio, 0);
263                 msleep(100);
264                 gpio_set_value(imx6_pcie->reset_gpio, 1);
265         }
266
267         return 0;
268
269 err_pcie_axi:
270         clk_disable_unprepare(imx6_pcie->lvds_gate);
271 err_lvds_gate:
272         clk_disable_unprepare(imx6_pcie->pcie_ref_125m);
273 err_pcie_ref:
274         clk_disable_unprepare(imx6_pcie->sata_ref_100m);
275 err_sata_ref:
276         release_bus_freq(BUS_FREQ_HIGH);
277         return ret;
278
279 }
280
281 static void imx6_pcie_init_phy(struct pcie_port *pp)
282 {
283         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
284
285         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
286                         IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
287
288         /* configure constant input signal to the pcie ctrl and phy */
289         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS))
290                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
291                                 IMX6Q_GPR12_DEVICE_TYPE,
292                                 PCI_EXP_TYPE_ENDPOINT << 12);
293         else
294                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
295                                 IMX6Q_GPR12_DEVICE_TYPE,
296                                 PCI_EXP_TYPE_ROOT_PORT << 12);
297         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
298                         IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
299
300         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
301                         IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
302         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
303                         IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
304         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
305                         IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
306         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
307                         IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
308         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
309                         IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
310 }
311
312 static irqreturn_t imx_pcie_msi_irq_handler(int irq, void *arg)
313 {
314         struct pcie_port *pp = arg;
315
316         dw_handle_msi_irq(pp);
317
318         return IRQ_HANDLED;
319 }
320
321 static void imx6_pcie_host_init(struct pcie_port *pp)
322 {
323         int count = 0;
324         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
325
326         imx6_pcie_init_phy(pp);
327
328         imx6_pcie_deassert_core_reset(pp);
329
330         dw_pcie_setup_rc(pp);
331
332         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
333                         IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
334
335         while (!dw_pcie_link_up(pp)) {
336                 usleep_range(100, 1000);
337                 count++;
338                 if (count >= 200) {
339                         dev_err(pp->dev, "phy link never came up\n");
340                         dev_dbg(pp->dev,
341                                 "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
342                                 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
343                                 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
344                         return;
345                 }
346         }
347
348         if (IS_ENABLED(CONFIG_PCI_MSI)) {
349                 pp->quirks |= DW_PCIE_QUIRK_NO_MSI_VEC;
350                 pp->quirks |= DW_PCIE_QUIRK_MSI_SELF_EN;
351                 dw_pcie_msi_init(pp);
352         }
353
354         return;
355 }
356
357 static int imx6_pcie_link_up(struct pcie_port *pp)
358 {
359         u32 rc, ltssm, rx_valid, temp;
360
361         /* link is debug bit 36, debug register 1 starts at bit 32 */
362         rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & (0x1 << (36 - 32));
363         if (rc)
364                 return -EAGAIN;
365
366         /*
367          * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
368          * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
369          * If (MAC/LTSSM.state == Recovery.RcvrLock)
370          * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
371          * to gen2 is stuck
372          */
373         pcie_phy_read(pp->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
374         ltssm = readl(pp->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
375
376         if (rx_valid & 0x01)
377                 return 0;
378
379         if (ltssm != 0x0d)
380                 return 0;
381
382         dev_err(pp->dev, "transition to gen2 is stuck, reset PHY!\n");
383
384         pcie_phy_read(pp->dbi_base,
385                 PHY_RX_OVRD_IN_LO, &temp);
386         temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN
387                 | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
388         pcie_phy_write(pp->dbi_base,
389                 PHY_RX_OVRD_IN_LO, temp);
390
391         usleep_range(2000, 3000);
392
393         pcie_phy_read(pp->dbi_base,
394                 PHY_RX_OVRD_IN_LO, &temp);
395         temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN
396                 | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
397         pcie_phy_write(pp->dbi_base,
398                 PHY_RX_OVRD_IN_LO, temp);
399
400         return 0;
401 }
402
403 static struct pcie_host_ops imx6_pcie_host_ops = {
404         .link_up = imx6_pcie_link_up,
405         .host_init = imx6_pcie_host_init,
406 };
407
408 static int imx6_add_pcie_port(struct pcie_port *pp,
409                         struct platform_device *pdev)
410 {
411         int ret;
412
413         pp->irq = platform_get_irq(pdev, 0);
414         if (!pp->irq) {
415                 dev_err(&pdev->dev, "failed to get irq\n");
416                 return -ENODEV;
417         }
418
419         if (IS_ENABLED(CONFIG_PCI_MSI)) {
420                 pp->msi_irq = pp->irq - 3;
421                 if (!pp->msi_irq) {
422                         dev_err(&pdev->dev, "failed to get msi irq\n");
423                         return -ENODEV;
424                 }
425
426                 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
427                                         imx_pcie_msi_irq_handler,
428                                         IRQF_SHARED, "imx6q-pcie", pp);
429                 if (ret) {
430                         dev_err(&pdev->dev, "failed to request msi irq\n");
431                         return ret;
432                 }
433         }
434
435         pp->root_bus_nr = -1;
436         pp->ops = &imx6_pcie_host_ops;
437
438         spin_lock_init(&pp->conf_lock);
439         ret = dw_pcie_host_init(pp);
440         if (ret) {
441                 dev_err(&pdev->dev, "failed to initialize host\n");
442                 return ret;
443         }
444
445         return 0;
446 }
447
448 static void imx_pcie_regions_setup(struct device *dev)
449 {
450         struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
451         struct pcie_port *pp = &imx6_pcie->pp;
452
453         /*
454          * region0 outbound used to access RC's reserved ddr memory
455          */
456         writel(0, pp->dbi_base + PCIE_ATU_VIEWPORT);
457         writel(0x01000000, pp->dbi_base + PCIE_ATU_LOWER_BASE);
458         writel(0, pp->dbi_base + PCIE_ATU_UPPER_BASE);
459         writel(0x01000000 + test_region_size,
460                         pp->dbi_base + PCIE_ATU_LIMIT);
461
462         writel(rc_ddr_test_region,
463                         pp->dbi_base + PCIE_ATU_LOWER_TARGET);
464         writel(0, pp->dbi_base + PCIE_ATU_UPPER_TARGET);
465         writel(PCIE_ATU_TYPE_MEM, pp->dbi_base + PCIE_ATU_CR1);
466         writel(PCIE_ATU_ENABLE, pp->dbi_base + PCIE_ATU_CR2);
467 }
468
469 static ssize_t imx_pcie_rc_memw_info(struct device *dev,
470                 struct device_attribute *devattr, char *buf)
471 {
472         return sprintf(buf, "imx-pcie-rc-memw-info start 0x%08x, size 0x%08x\n",
473                         rc_ddr_test_region, test_region_size);
474 }
475
476 static ssize_t
477 imx_pcie_rc_memw_start(struct device *dev, struct device_attribute *attr,
478                 const char *buf, size_t count)
479 {
480         u32 memw_start;
481
482         sscanf(buf, "%x\n", &memw_start);
483
484         if (memw_start < 0x10000000) {
485                 dev_err(dev, "Invalid memory start address.\n");
486                 dev_info(dev, "For example: echo 0x41000000 > /sys/...");
487                 return -1;
488         }
489
490         if (rc_ddr_test_region != memw_start) {
491                 rc_ddr_test_region = memw_start;
492                 /* Re-setup the iATU */
493                 imx_pcie_regions_setup(dev);
494         }
495
496         return count;
497 }
498
499 static ssize_t
500 imx_pcie_rc_memw_size(struct device *dev, struct device_attribute *attr,
501                 const char *buf, size_t count)
502 {
503         u32 memw_size;
504
505         sscanf(buf, "%x\n", &memw_size);
506
507         if ((memw_size > (SZ_16M - SZ_16K)) || (memw_size < SZ_64K)) {
508                 dev_err(dev, "Invalid, should be [SZ_64K,SZ_16M - SZ_16KB].\n");
509                 dev_info(dev, "For example: echo 0x800000 > /sys/...");
510                 return -1;
511         }
512
513         if (test_region_size != memw_size) {
514                 test_region_size = memw_size;
515                 /* Re-setup the iATU */
516                 imx_pcie_regions_setup(dev);
517         }
518
519         return count;
520 }
521
522 static DEVICE_ATTR(rc_memw_info, S_IRUGO, imx_pcie_rc_memw_info, NULL);
523 static DEVICE_ATTR(rc_memw_start_set, S_IWUGO, NULL, imx_pcie_rc_memw_start);
524 static DEVICE_ATTR(rc_memw_size_set, S_IWUGO, NULL, imx_pcie_rc_memw_size);
525
526 static struct attribute *imx_pcie_attrs[] = {
527         /*
528          * The start address, and the limitation (64KB ~ (16MB - 16KB))
529          * of the ddr mem window reserved by RC, and used for EP to access.
530          * BTW, these attrs are only configured at EP side.
531          */
532         &dev_attr_rc_memw_info.attr,
533         &dev_attr_rc_memw_start_set.attr,
534         &dev_attr_rc_memw_size_set.attr,
535         NULL
536 };
537
538 static struct attribute_group imx_pcie_attrgroup = {
539         .attrs  = imx_pcie_attrs,
540 };
541
542 static int __init imx6_pcie_probe(struct platform_device *pdev)
543 {
544         struct imx6_pcie *imx6_pcie;
545         struct pcie_port *pp;
546         struct device_node *np = pdev->dev.of_node;
547         struct resource *dbi_base;
548         int ret, i;
549         void *test_reg1, *test_reg2;
550         void __iomem *pcie_arb_base_addr;
551         struct timeval tv1, tv2, tv3;
552         u32 tv_count1, tv_count2;
553
554         imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
555         if (!imx6_pcie)
556                 return -ENOMEM;
557
558         pp = &imx6_pcie->pp;
559         pp->dev = &pdev->dev;
560
561         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)) {
562                 /* add attributes for device */
563                 ret = sysfs_create_group(&pdev->dev.kobj, &imx_pcie_attrgroup);
564                 if (ret)
565                         return -EINVAL;
566         }
567
568         /* Added for PCI abort handling */
569         hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
570                 "imprecise external abort");
571
572         dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
573         if (!dbi_base) {
574                 dev_err(&pdev->dev, "dbi_base memory resource not found\n");
575                 return -ENODEV;
576         }
577
578         pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
579         if (IS_ERR(pp->dbi_base)) {
580                 ret = PTR_ERR(pp->dbi_base);
581                 goto err;
582         }
583
584         /* Fetch GPIOs */
585         imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
586         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
587                 ret = devm_gpio_request_one(&pdev->dev,
588                                         imx6_pcie->reset_gpio,
589                                         GPIOF_OUT_INIT_LOW,
590                                         "PCIe reset");
591                 if (ret) {
592                         dev_err(&pdev->dev, "unable to get reset gpio\n");
593                         goto err;
594                 }
595         }
596
597         imx6_pcie->power_on_gpio = of_get_named_gpio(np, "power-on-gpio", 0);
598         if (gpio_is_valid(imx6_pcie->power_on_gpio)) {
599                 ret = devm_gpio_request_one(&pdev->dev,
600                                         imx6_pcie->power_on_gpio,
601                                         GPIOF_OUT_INIT_LOW,
602                                         "PCIe power enable");
603                 if (ret) {
604                         dev_err(&pdev->dev, "unable to get power-on gpio\n");
605                         goto err;
606                 }
607         }
608
609         imx6_pcie->wake_up_gpio = of_get_named_gpio(np, "wake-up-gpio", 0);
610         if (gpio_is_valid(imx6_pcie->wake_up_gpio)) {
611                 ret = devm_gpio_request_one(&pdev->dev,
612                                         imx6_pcie->wake_up_gpio,
613                                         GPIOF_IN,
614                                         "PCIe wake up");
615                 if (ret) {
616                         dev_err(&pdev->dev, "unable to get wake-up gpio\n");
617                         goto err;
618                 }
619         }
620
621         imx6_pcie->disable_gpio = of_get_named_gpio(np, "disable-gpio", 0);
622         if (gpio_is_valid(imx6_pcie->disable_gpio)) {
623                 ret = devm_gpio_request_one(&pdev->dev,
624                                         imx6_pcie->disable_gpio,
625                                         GPIOF_OUT_INIT_HIGH,
626                                         "PCIe disable endpoint");
627                 if (ret) {
628                         dev_err(&pdev->dev, "unable to get disable-ep gpio\n");
629                         goto err;
630                 }
631         }
632
633         /* Fetch clocks */
634         imx6_pcie->lvds_gate = devm_clk_get(&pdev->dev, "lvds_gate");
635         if (IS_ERR(imx6_pcie->lvds_gate)) {
636                 dev_err(&pdev->dev,
637                         "lvds_gate clock select missing or invalid\n");
638                 ret = PTR_ERR(imx6_pcie->lvds_gate);
639                 goto err;
640         }
641
642         imx6_pcie->sata_ref_100m = devm_clk_get(&pdev->dev, "sata_ref_100m");
643         if (IS_ERR(imx6_pcie->sata_ref_100m)) {
644                 dev_err(&pdev->dev,
645                         "sata_ref_100m clock source missing or invalid\n");
646                 ret = PTR_ERR(imx6_pcie->sata_ref_100m);
647                 goto err;
648         }
649
650         imx6_pcie->pcie_ref_125m = devm_clk_get(&pdev->dev, "pcie_ref_125m");
651         if (IS_ERR(imx6_pcie->pcie_ref_125m)) {
652                 dev_err(&pdev->dev,
653                         "pcie_ref_125m clock source missing or invalid\n");
654                 ret = PTR_ERR(imx6_pcie->pcie_ref_125m);
655                 goto err;
656         }
657
658         imx6_pcie->pcie_axi = devm_clk_get(&pdev->dev, "pcie_axi");
659         if (IS_ERR(imx6_pcie->pcie_axi)) {
660                 dev_err(&pdev->dev,
661                         "pcie_axi clock source missing or invalid\n");
662                 ret = PTR_ERR(imx6_pcie->pcie_axi);
663                 goto err;
664         }
665
666         /* Grab GPR config register range */
667         imx6_pcie->iomuxc_gpr =
668                  syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
669         if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
670                 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
671                 ret = PTR_ERR(imx6_pcie->iomuxc_gpr);
672                 goto err;
673         }
674
675         if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)) {
676                 if (IS_ENABLED(CONFIG_EP_SELF_IO_TEST)) {
677                         /* Prepare the test regions and data */
678                         test_reg1 = devm_kzalloc(&pdev->dev,
679                                         test_region_size, GFP_KERNEL);
680                         if (!test_reg1) {
681                                 pr_err("pcie ep: can't alloc the test reg1.\n");
682                                 ret = PTR_ERR(test_reg1);
683                                 goto err;
684                         }
685
686                         test_reg2 = devm_kzalloc(&pdev->dev,
687                                         test_region_size, GFP_KERNEL);
688                         if (!test_reg2) {
689                                 pr_err("pcie ep: can't alloc the test reg2.\n");
690                                 ret = PTR_ERR(test_reg1);
691                                 goto err;
692                         }
693
694                         pcie_arb_base_addr = ioremap_cached(0x01000000,
695                                         test_region_size);
696
697                         if (!pcie_arb_base_addr) {
698                                 pr_err("error with ioremap in ep selftest\n");
699                                 ret = PTR_ERR(pcie_arb_base_addr);
700                                 goto err;
701                         }
702
703                         for (i = 0; i < test_region_size; i = i + 4) {
704                                 writel(0xE6600D00 + i, test_reg1 + i);
705                                 writel(0xDEADBEAF, test_reg2 + i);
706                         }
707                 }
708
709                 imx6_pcie_init_phy(pp);
710
711                 imx6_pcie_deassert_core_reset(pp);
712
713                 /* assert LTSSM enable */
714                 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
715                                 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
716
717
718                 dev_info(&pdev->dev, "PCIe EP: waiting for link up...\n");
719
720                 platform_set_drvdata(pdev, imx6_pcie);
721                 /* link is indicated by the bit4 of DB_R1 register */
722                 do {
723                         usleep_range(10, 20);
724                 } while ((readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & 0x10) == 0);
725
726                 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
727                 writel(readl(pp->dbi_base + PCI_COMMAND)
728                                 | PCI_COMMAND_IO
729                                 | PCI_COMMAND_MEMORY
730                                 | PCI_COMMAND_MASTER,
731                                 pp->dbi_base + PCI_COMMAND);
732
733                 /*
734                  * configure the class_rev(emaluate one memory ram ep device),
735                  * bar0 and bar1 of ep
736                  */
737                 writel(0xdeadbeaf, pp->dbi_base + PCI_VENDOR_ID);
738                 writel(readl(pp->dbi_base + PCI_CLASS_REVISION)
739                                 | (PCI_CLASS_MEMORY_RAM << 16),
740                                 pp->dbi_base + PCI_CLASS_REVISION);
741                 writel(0xdeadbeaf, pp->dbi_base
742                                 + PCI_SUBSYSTEM_VENDOR_ID);
743
744                 /* 32bit none-prefetchable 8M bytes memory on bar0 */
745                 writel(0x0, pp->dbi_base + PCI_BASE_ADDRESS_0);
746                 writel(SZ_8M - 1, pp->dbi_base + (1 << 12)
747                                 + PCI_BASE_ADDRESS_0);
748
749                 /* None used bar1 */
750                 writel(0x0, pp->dbi_base + PCI_BASE_ADDRESS_1);
751                 writel(0, pp->dbi_base + (1 << 12) + PCI_BASE_ADDRESS_1);
752
753                 /* 4K bytes IO on bar2 */
754                 writel(0x1, pp->dbi_base + PCI_BASE_ADDRESS_2);
755                 writel(SZ_4K - 1, pp->dbi_base + (1 << 12) +
756                                 PCI_BASE_ADDRESS_2);
757
758                 /*
759                  * 32bit prefetchable 1M bytes memory on bar3
760                  * FIXME BAR MASK3 is not changable, the size
761                  * is fixed to 256 bytes.
762                  */
763                 writel(0x8, pp->dbi_base + PCI_BASE_ADDRESS_3);
764                 writel(SZ_1M - 1, pp->dbi_base + (1 << 12)
765                                 + PCI_BASE_ADDRESS_3);
766
767                 /*
768                  * 64bit prefetchable 1M bytes memory on bar4-5.
769                  * FIXME BAR4,5 are not enabled yet
770                  */
771                 writel(0xc, pp->dbi_base + PCI_BASE_ADDRESS_4);
772                 writel(SZ_1M - 1, pp->dbi_base + (1 << 12)
773                                 + PCI_BASE_ADDRESS_4);
774                 writel(0, pp->dbi_base + (1 << 12) + PCI_BASE_ADDRESS_5);
775
776                 /* Re-setup the iATU */
777                 imx_pcie_regions_setup(&pdev->dev);
778
779                 if (IS_ENABLED(CONFIG_EP_SELF_IO_TEST)) {
780                         /* PCIe EP start the data transfer after link up */
781                         pr_info("pcie ep: Starting data transfer...\n");
782                         do_gettimeofday(&tv1);
783
784                         memcpy((unsigned long *)pcie_arb_base_addr,
785                                         (unsigned long *)test_reg1,
786                                         test_region_size);
787
788                         do_gettimeofday(&tv2);
789
790                         memcpy((unsigned long *)test_reg2,
791                                         (unsigned long *)pcie_arb_base_addr,
792                                         test_region_size);
793
794                         do_gettimeofday(&tv3);
795
796                         if (memcmp(test_reg2, test_reg1, test_region_size) == 0) {
797                                 tv_count1 = (tv2.tv_sec - tv1.tv_sec)
798                                         * USEC_PER_SEC
799                                         + tv2.tv_usec - tv1.tv_usec;
800                                 tv_count2 = (tv3.tv_sec - tv2.tv_sec)
801                                         * USEC_PER_SEC
802                                         + tv3.tv_usec - tv2.tv_usec;
803
804                                 pr_info("pcie ep: Data transfer is successful."
805                                                 " tv_count1 %dus,"
806                                                 " tv_count2 %dus.\n",
807                                                 tv_count1, tv_count2);
808                                 pr_info("pcie ep: Data write speed:%ldMB/s.\n",
809                                                 ((test_region_size/1024)
810                                                    * MSEC_PER_SEC)
811                                                 /(tv_count1));
812                                 pr_info("pcie ep: Data read speed:%ldMB/s.\n",
813                                                 ((test_region_size/1024)
814                                                    * MSEC_PER_SEC)
815                                                 /(tv_count2));
816                         } else {
817                                 pr_info("pcie ep: Data transfer is failed.\n");
818                         }
819                 }
820         } else {
821                 ret = imx6_add_pcie_port(pp, pdev);
822                 if (ret < 0)
823                         goto err;
824
825                 platform_set_drvdata(pdev, imx6_pcie);
826         }
827         return 0;
828
829 err:
830         return ret;
831 }
832
833 static const struct of_device_id imx6_pcie_of_match[] = {
834         { .compatible = "fsl,imx6q-pcie", },
835         {},
836 };
837 MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
838
839 static struct platform_driver imx6_pcie_driver = {
840         .driver = {
841                 .name   = "imx6q-pcie",
842                 .owner  = THIS_MODULE,
843                 .of_match_table = imx6_pcie_of_match,
844         },
845 };
846
847 /* Freescale PCIe driver does not allow module unload */
848
849 static int __init imx6_pcie_init(void)
850 {
851         return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
852 }
853 fs_initcall(imx6_pcie_init);
854
855 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
856 MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
857 MODULE_LICENSE("GPL v2");