2 * PCIe host controller driver for Freescale i.MX6 SoCs
4 * Copyright (C) 2014-2015 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Copyright (C) 2013 Kosagi
6 * http://www.kosagi.com
8 * Author: Sean Cross <xobs@kosagi.com>
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.
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>
32 #include "pcie-designware.h"
34 #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
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
44 static u32 ddr_test_region = 0, test_region_size = SZ_2M;
51 struct clk *pcie_inbound_axi;
55 struct regmap *iomuxc_gpr;
56 struct regulator *pcie_phy_regulator;
57 void __iomem *mem_base;
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
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)
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
83 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
84 #define PCIE_PHY_STAT_ACK_LOC 16
86 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
87 #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
89 /* PHY registers (not memory-mapped) */
90 #define PCIE_PHY_RX_ASIC_OUT 0x100D
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)
96 static inline bool is_imx6sx_pcie(struct imx6_pcie *imx6_pcie)
98 struct pcie_port *pp = &imx6_pcie->pp;
99 struct device_node *np = pp->dev->of_node;
101 return of_device_is_compatible(np, "fsl,imx6sx-pcie");
104 static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
107 u32 max_iterations = 10;
108 u32 wait_counter = 0;
111 val = readl(dbi_base + PCIE_PHY_STAT);
112 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
119 } while (wait_counter < max_iterations);
124 static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
129 val = addr << PCIE_PHY_CTRL_DATA_LOC;
130 writel(val, dbi_base + PCIE_PHY_CTRL);
132 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
133 writel(val, dbi_base + PCIE_PHY_CTRL);
135 ret = pcie_phy_poll_ack(dbi_base, 1);
139 val = addr << PCIE_PHY_CTRL_DATA_LOC;
140 writel(val, dbi_base + PCIE_PHY_CTRL);
142 ret = pcie_phy_poll_ack(dbi_base, 0);
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)
155 ret = pcie_phy_wait_ack(dbi_base, addr);
159 /* assert Read signal */
160 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
161 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
163 ret = pcie_phy_poll_ack(dbi_base, 1);
167 val = readl(dbi_base + PCIE_PHY_STAT);
168 *data = val & 0xffff;
170 /* deassert Read signal */
171 writel(0x00, dbi_base + PCIE_PHY_CTRL);
173 ret = pcie_phy_poll_ack(dbi_base, 0);
180 static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
187 ret = pcie_phy_wait_ack(dbi_base, addr);
191 var = data << PCIE_PHY_CTRL_DATA_LOC;
192 writel(var, dbi_base + PCIE_PHY_CTRL);
195 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
196 writel(var, dbi_base + PCIE_PHY_CTRL);
198 ret = pcie_phy_poll_ack(dbi_base, 1);
202 /* deassert cap data */
203 var = data << PCIE_PHY_CTRL_DATA_LOC;
204 writel(var, dbi_base + PCIE_PHY_CTRL);
206 /* wait for ack de-assertion */
207 ret = pcie_phy_poll_ack(dbi_base, 0);
211 /* assert wr signal */
212 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
213 writel(var, dbi_base + PCIE_PHY_CTRL);
216 ret = pcie_phy_poll_ack(dbi_base, 1);
220 /* deassert wr signal */
221 var = data << PCIE_PHY_CTRL_DATA_LOC;
222 writel(var, dbi_base + PCIE_PHY_CTRL);
224 /* wait for ack de-assertion */
225 ret = pcie_phy_poll_ack(dbi_base, 0);
229 writel(0x0, dbi_base + PCIE_PHY_CTRL);
234 /* Added for PCI abort handling */
235 static int imx6q_pcie_abort_handler(unsigned long addr,
236 unsigned int fsr, struct pt_regs *regs)
241 static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
243 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
244 u32 val, gpr1, gpr12;
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);
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.
264 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we
265 * have a strong indication that the bootloader
266 * activated the link.
268 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
269 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
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);
278 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
279 IMX6Q_GPR12_PCIE_CTL_2, 0);
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);
292 static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
294 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
297 if (gpio_is_valid(imx6_pcie->power_on_gpio))
298 gpio_set_value(imx6_pcie->power_on_gpio, 1);
300 request_bus_freq(BUS_FREQ_HIGH);
301 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
303 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
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);
311 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
316 ret = clk_prepare_enable(imx6_pcie->pcie);
318 dev_err(pp->dev, "unable to enable pcie clock\n");
322 if (is_imx6sx_pcie(imx6_pcie)) {
323 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
325 dev_err(pp->dev, "unable to enable pcie clock\n");
326 goto err_inbound_axi;
329 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
330 IMX6SX_GPR12_PCIE_TEST_PD, 0);
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);
336 /* sata_ref is not used by pcie on imx6sx */
337 ret = clk_prepare_enable(imx6_pcie->ref_100m);
339 dev_err(pp->dev, "unable to enable ref_100m\n");
340 goto err_inbound_axi;
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.
349 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
350 IMX6Q_GPR1_PCIE_REF_CLK_EN,
351 IMX6Q_GPR1_PCIE_REF_CLK_EN);
354 /* allow the clocks to stabilize */
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);
361 gpio_set_value(imx6_pcie->reset_gpio, 1);
365 * Release the PCIe PHY reset here, that we have set in
366 * imx6_pcie_init_phy() now
368 if (is_imx6sx_pcie(imx6_pcie))
369 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
370 IMX6SX_GPR5_PCIE_BTNRST, 0);
375 clk_disable_unprepare(imx6_pcie->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);
381 clk_disable_unprepare(imx6_pcie->pcie_phy);
387 static int imx6_pcie_init_phy(struct pcie_port *pp)
389 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
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);
399 regulator_set_voltage(imx6_pcie->pcie_phy_regulator,
401 ret = regulator_enable(imx6_pcie->pcie_phy_regulator);
403 dev_err(pp->dev, "failed to enable pcie regulator.\n");
406 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
407 IMX6SX_GPR12_RX_EQ_MASK, IMX6SX_GPR12_RX_EQ_2);
410 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
411 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
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);
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);
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);
439 static int imx6_pcie_wait_for_link(struct pcie_port *pp)
443 while (!dw_pcie_link_up(pp)) {
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));
458 static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
460 struct pcie_port *pp = arg;
462 return dw_handle_msi_irq(pp);
465 static int imx6_pcie_start_link(struct pcie_port *pp)
467 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
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.
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);
482 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
483 IMX6Q_GPR12_PCIE_CTL_2,
484 IMX6Q_GPR12_PCIE_CTL_2);
486 ret = imx6_pcie_wait_for_link(pp);
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);
497 * Start Directed Speed Change so the best possible speed both link
498 * partners support can be negotiated.
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);
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))
513 /* Make sure link training is finished as well! */
515 ret = imx6_pcie_wait_for_link(pp);
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);
532 /* Put PCIe PHY to be isolation */
533 regmap_update_bits(imx6_pcie->iomuxc_gpr,
534 IOMUXC_GPR0, BIT(6), 1 << 6);
537 * Power down PCIe PHY.
539 regulator_disable(imx6_pcie->pcie_phy_regulator);
541 clk_disable_unprepare(imx6_pcie->ref_100m);
542 release_bus_freq(BUS_FREQ_HIGH);
545 tmp = readl(pp->dbi_base + 0x80);
546 dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
552 static int imx6_pcie_host_init(struct pcie_port *pp)
556 imx6_pcie_assert_core_reset(pp);
558 ret = imx6_pcie_init_phy(pp);
562 ret = imx6_pcie_deassert_core_reset(pp);
566 dw_pcie_setup_rc(pp);
568 ret = imx6_pcie_start_link(pp);
572 if (IS_ENABLED(CONFIG_PCI_MSI))
573 dw_pcie_msi_init(pp);
578 static void imx6_pcie_reset_phy(struct pcie_port *pp)
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);
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);
595 static int imx6_pcie_link_up(struct pcie_port *pp)
597 u32 rc, debug_r0, rx_valid;
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.
617 rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
618 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP))
620 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING))
624 dev_dbg(pp->dev, "Link is up, but still in training\n");
626 * Wait a little bit, then re-check if the link finished
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
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);
644 if ((debug_r0 & 0x3f) != 0x0d)
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);
650 imx6_pcie_reset_phy(pp);
655 static struct pcie_host_ops imx6_pcie_host_ops = {
656 .link_up = imx6_pcie_link_up,
657 .host_init = imx6_pcie_host_init,
660 static int __init imx6_add_pcie_port(struct pcie_port *pp,
661 struct platform_device *pdev)
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");
672 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
673 imx6_pcie_msi_handler,
674 IRQF_SHARED, "mx6-pcie-msi", pp);
676 dev_err(&pdev->dev, "failed to request MSI irq\n");
681 pp->root_bus_nr = -1;
682 pp->ops = &imx6_pcie_host_ops;
684 ret = dw_pcie_host_init(pp);
686 dev_err(&pdev->dev, "failed to initialize host\n");
693 static ssize_t imx_pcie_bar0_addr_info(struct device *dev,
694 struct device_attribute *devattr, char *buf)
696 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
697 struct pcie_port *pp = &imx6_pcie->pp;
699 return sprintf(buf, "imx-pcie-bar0-addr-info start 0x%08x\n",
700 readl(pp->dbi_base + PCI_BASE_ADDRESS_0));
703 static ssize_t imx_pcie_bar0_addr_start(struct device *dev,
704 struct device_attribute *attr, const char *buf, size_t count)
707 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
708 struct pcie_port *pp = &imx6_pcie->pp;
710 sscanf(buf, "%x\n", &bar_start);
711 writel(bar_start, pp->dbi_base + PCI_BASE_ADDRESS_0);
716 static void imx_pcie_regions_setup(struct device *dev)
718 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
719 struct pcie_port *pp = &imx6_pcie->pp;
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;
727 * region2 outbound used to access rc/ep mem
728 * in imx6 pcie ep/rc validation system
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);
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);
741 static ssize_t imx_pcie_memw_info(struct device *dev,
742 struct device_attribute *devattr, char *buf)
744 return sprintf(buf, "imx-pcie-rc-memw-info start 0x%08x, size 0x%08x\n",
745 ddr_test_region, test_region_size);
749 imx_pcie_memw_start(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
753 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
755 sscanf(buf, "%x\n", &memw_start);
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/...");
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/...");
771 if (ddr_test_region != memw_start) {
772 ddr_test_region = memw_start;
773 imx_pcie_regions_setup(dev);
780 imx_pcie_memw_size(struct device *dev, struct device_attribute *attr,
781 const char *buf, size_t count)
785 sscanf(buf, "%x\n", &memw_size);
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/...");
793 if (test_region_size != memw_size) {
794 test_region_size = memw_size;
795 imx_pcie_regions_setup(dev);
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);
807 static struct attribute *imx_pcie_attrs[] = {
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.
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,
820 static struct attribute_group imx_pcie_attrgroup = {
821 .attrs = imx_pcie_attrs,
824 static void imx6_pcie_setup_ep(struct pcie_port *pp)
826 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
827 writel(readl(pp->dbi_base + PCI_COMMAND)
830 | PCI_COMMAND_MASTER,
831 pp->dbi_base + PCI_COMMAND);
834 * configure the class_rev(emaluate one memory ram ep device),
835 * bar0 and bar1 of ep
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);
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);
850 writel(0x0, pp->dbi_base + PCI_BASE_ADDRESS_1);
851 writel(0, pp->dbi_base + (1 << 12) + PCI_BASE_ADDRESS_1);
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) +
859 * 32bit prefetchable 1M bytes memory on bar3
860 * FIXME BAR MASK3 is not changable, the size
861 * is fixed to 256 bytes.
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);
868 * 64bit prefetchable 1M bytes memory on bar4-5.
869 * FIXME BAR4,5 are not enabled yet
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);
877 #ifdef CONFIG_PM_SLEEP
878 static int pci_imx_suspend_noirq(struct device *dev)
880 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
881 struct pcie_port *pp = &imx6_pcie->pp;
883 if (is_imx6sx_pcie(imx6_pcie)) {
884 if (IS_ENABLED(CONFIG_PCI_MSI))
885 dw_pcie_msi_cfg_store(pp);
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);
897 /* Put PCIe PHY to be isolation */
898 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR0,
902 * Power down PCIe PHY.
904 regulator_disable(imx6_pcie->pcie_phy_regulator);
907 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
908 IMX6SX_GPR12_PCIE_PM_TURN_OFF,
909 IMX6SX_GPR12_PCIE_PM_TURN_OFF);
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);
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"
926 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
927 IMX6Q_GPR1_PCIE_TEST_PD,
928 IMX6Q_GPR1_PCIE_TEST_PD);
934 static int pci_imx_resume_noirq(struct device *dev)
937 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
938 struct pcie_port *pp = &imx6_pcie->pp;
940 if (is_imx6sx_pcie(imx6_pcie)) {
941 if (IS_ENABLED(CONFIG_PCI_IMX6SX_EXTREMELY_PWR_SAVE)) {
942 imx6_pcie_assert_core_reset(pp);
944 ret = imx6_pcie_init_phy(pp);
948 ret = imx6_pcie_deassert_core_reset(pp);
952 if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS))
953 imx6_pcie_setup_ep(pp);
955 dw_pcie_setup_rc(pp);
957 if (IS_ENABLED(CONFIG_PCI_MSI))
958 dw_pcie_msi_cfg_restore(pp);
960 ret = imx6_pcie_start_link(pp);
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);
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);
977 * controller maybe turn off, re-configure again
979 dw_pcie_setup_rc(pp);
981 if (IS_ENABLED(CONFIG_PCI_MSI))
982 dw_pcie_msi_cfg_restore(pp);
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"
991 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
992 IMX6Q_GPR1_PCIE_TEST_PD, 0);
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,
1008 static int __init imx6_pcie_probe(struct platform_device *pdev)
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;
1016 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
1020 pp = &imx6_pcie->pp;
1021 pp->dev = &pdev->dev;
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);
1030 /* Added for PCI abort handling */
1031 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
1032 "imprecise external abort");
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);
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");
1045 dev_err(&pdev->dev, "unable to get reset gpio\n");
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,
1055 "PCIe power enable");
1057 dev_err(&pdev->dev, "unable to get power-on gpio\n");
1063 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
1064 if (IS_ERR(imx6_pcie->pcie_phy)) {
1066 "pcie_phy clock source missing or invalid\n");
1067 return PTR_ERR(imx6_pcie->pcie_phy);
1070 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
1071 if (IS_ERR(imx6_pcie->pcie_bus)) {
1073 "pcie_bus clock source missing or invalid\n");
1074 return PTR_ERR(imx6_pcie->pcie_bus);
1077 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
1078 if (IS_ERR(imx6_pcie->pcie)) {
1080 "pcie clock source missing or invalid\n");
1081 return PTR_ERR(imx6_pcie->pcie);
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)) {
1089 "pcie clock source missing or invalid\n");
1090 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
1093 imx6_pcie->pcie_phy_regulator = devm_regulator_get(pp->dev,
1096 imx6_pcie->iomuxc_gpr =
1097 syscon_regmap_lookup_by_compatible
1098 ("fsl,imx6sx-iomuxc-gpr");
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)) {
1104 "ref_100m clock source missing or invalid\n");
1105 return PTR_ERR(imx6_pcie->ref_100m);
1108 imx6_pcie->iomuxc_gpr =
1109 syscon_regmap_lookup_by_compatible
1110 ("fsl,imx6q-iomuxc-gpr");
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);
1117 if (IS_ENABLED(CONFIG_EP_MODE_IN_EP_RC_SYS)) {
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;
1128 if (of_pci_range_parser_init(&parser, np)) {
1129 dev_err(pp->dev, "missing ranges property\n");
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,
1139 pp->mem.name = "MEM";
1143 pp->mem_base = pp->mem.start;
1145 imx6_pcie_assert_core_reset(pp);
1146 ret = imx6_pcie_init_phy(pp);
1147 ret |= imx6_pcie_deassert_core_reset(pp);
1149 dev_err(&pdev->dev, "unable to init pcie ep.\n");
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
1160 if (is_imx6sx_pcie(imx6_pcie))
1161 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
1164 /* assert LTSSM enable */
1165 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
1166 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
1168 dev_info(&pdev->dev, "PCIe EP: waiting for link up...\n");
1170 platform_set_drvdata(pdev, imx6_pcie);
1171 /* link is indicated by the bit4 of DB_R1 register */
1173 usleep_range(10, 20);
1174 } while ((readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & 0x10) == 0);
1176 imx6_pcie_setup_ep(pp);
1178 imx_pcie_regions_setup(&pdev->dev);
1181 test_reg1 = devm_kzalloc(&pdev->dev,
1182 test_region_size, GFP_KERNEL);
1184 pr_err("pcie ep: can't alloc the test reg1.\n");
1185 ret = PTR_ERR(test_reg1);
1189 test_reg2 = devm_kzalloc(&pdev->dev,
1190 test_region_size, GFP_KERNEL);
1192 pr_err("pcie ep: can't alloc the test reg2.\n");
1193 ret = PTR_ERR(test_reg1);
1197 pcie_arb_base_addr = ioremap_cache(pp->mem_base,
1200 if (!pcie_arb_base_addr) {
1201 pr_err("error with ioremap in ep selftest\n");
1202 ret = PTR_ERR(pcie_arb_base_addr);
1206 for (i = 0; i < test_region_size; i = i + 4) {
1207 writel(0xE6600D00 + i, test_reg1 + i);
1208 writel(0xDEADBEAF, test_reg2 + i);
1211 /* PCIe EP start the data transfer after link up */
1212 pr_info("pcie ep: Starting data transfer...\n");
1213 do_gettimeofday(&tv1);
1215 memcpy((unsigned long *)pcie_arb_base_addr,
1216 (unsigned long *)test_reg1,
1219 do_gettimeofday(&tv2);
1221 memcpy((unsigned long *)test_reg2,
1222 (unsigned long *)pcie_arb_base_addr,
1225 do_gettimeofday(&tv3);
1227 if (memcmp(test_reg2, test_reg1, test_region_size) == 0) {
1228 tv_count1 = (tv2.tv_sec - tv1.tv_sec)
1230 + tv2.tv_usec - tv1.tv_usec;
1231 tv_count2 = (tv3.tv_sec - tv2.tv_sec)
1233 + tv3.tv_usec - tv2.tv_usec;
1235 pr_info("pcie ep: Data transfer is successful."
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)
1243 pr_info("pcie ep: Data read speed:%ldMB/s.\n",
1244 ((test_region_size/1024)
1248 pr_info("pcie ep: Data transfer is failed.\n");
1249 } /* end of self io test. */
1251 ret = imx6_add_pcie_port(pp, pdev);
1255 platform_set_drvdata(pdev, imx6_pcie);
1257 if (IS_ENABLED(CONFIG_RC_MODE_IN_EP_RC_SYS))
1258 imx_pcie_regions_setup(&pdev->dev);
1266 static void imx6_pcie_shutdown(struct platform_device *pdev)
1268 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1270 /* bring down link, so bootloader gets clean state in case of reboot */
1271 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
1274 static const struct of_device_id imx6_pcie_of_match[] = {
1275 { .compatible = "fsl,imx6q-pcie", },
1276 { .compatible = "fsl,imx6sx-pcie", },
1279 MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
1281 static struct platform_driver imx6_pcie_driver = {
1283 .name = "imx6q-pcie",
1284 .owner = THIS_MODULE,
1285 .of_match_table = imx6_pcie_of_match,
1286 .pm = &pci_imx_pm_ops,
1288 .shutdown = imx6_pcie_shutdown,
1291 /* Freescale PCIe driver does not allow module unload */
1293 static int __init imx6_pcie_init(void)
1295 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
1297 module_init(imx6_pcie_init);
1299 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
1300 MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
1301 MODULE_LICENSE("GPL v2");