Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * PCIe host controller driver for Freescale i.MX6 SoCs
   4 *
   5 * Copyright (C) 2013 Kosagi
   6 *		https://www.kosagi.com
   7 *
   8 * Author: Sean Cross <xobs@kosagi.com>
   9 */
  10
  11#include <linux/bitfield.h>
  12#include <linux/clk.h>
  13#include <linux/delay.h>
  14#include <linux/gpio.h>
  15#include <linux/kernel.h>
  16#include <linux/mfd/syscon.h>
  17#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  18#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
  19#include <linux/module.h>
  20#include <linux/of.h>
  21#include <linux/of_gpio.h>
  22#include <linux/of_address.h>
  23#include <linux/pci.h>
  24#include <linux/platform_device.h>
  25#include <linux/regmap.h>
  26#include <linux/regulator/consumer.h>
  27#include <linux/resource.h>
  28#include <linux/signal.h>
  29#include <linux/types.h>
  30#include <linux/interrupt.h>
  31#include <linux/reset.h>
  32#include <linux/phy/phy.h>
  33#include <linux/pm_domain.h>
  34#include <linux/pm_runtime.h>
  35
  36#include "pcie-designware.h"
  37
  38#define IMX8MQ_GPR_PCIE_REF_USE_PAD		BIT(9)
  39#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN	BIT(10)
  40#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE	BIT(11)
  41#define IMX8MQ_GPR_PCIE_VREG_BYPASS		BIT(12)
  42#define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE	GENMASK(11, 8)
  43#define IMX8MQ_PCIE2_BASE_ADDR			0x33c00000
  44
 
 
 
 
 
 
 
 
 
 
 
 
 
  45#define to_imx6_pcie(x)	dev_get_drvdata((x)->dev)
  46
  47enum imx6_pcie_variants {
  48	IMX6Q,
  49	IMX6SX,
  50	IMX6QP,
  51	IMX7D,
  52	IMX8MQ,
  53	IMX8MM,
  54	IMX8MP,
 
  55	IMX8MQ_EP,
  56	IMX8MM_EP,
  57	IMX8MP_EP,
 
  58};
  59
  60#define IMX6_PCIE_FLAG_IMX6_PHY			BIT(0)
  61#define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE	BIT(1)
  62#define IMX6_PCIE_FLAG_SUPPORTS_SUSPEND		BIT(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
  63
  64struct imx6_pcie_drvdata {
  65	enum imx6_pcie_variants variant;
  66	enum dw_pcie_device_mode mode;
  67	u32 flags;
  68	int dbi_length;
  69	const char *gpr;
 
 
 
 
 
 
 
 
  70};
  71
  72struct imx6_pcie {
  73	struct dw_pcie		*pci;
  74	int			reset_gpio;
  75	bool			gpio_active_high;
  76	bool			link_is_up;
  77	struct clk		*pcie_bus;
  78	struct clk		*pcie_phy;
  79	struct clk		*pcie_inbound_axi;
  80	struct clk		*pcie;
  81	struct clk		*pcie_aux;
  82	struct regmap		*iomuxc_gpr;
  83	u16			msi_ctrl;
  84	u32			controller_id;
  85	struct reset_control	*pciephy_reset;
  86	struct reset_control	*apps_reset;
  87	struct reset_control	*turnoff_reset;
  88	u32			tx_deemph_gen1;
  89	u32			tx_deemph_gen2_3p5db;
  90	u32			tx_deemph_gen2_6db;
  91	u32			tx_swing_full;
  92	u32			tx_swing_low;
  93	struct regulator	*vpcie;
  94	struct regulator	*vph;
  95	void __iomem		*phy_base;
  96
  97	/* power domain for pcie */
  98	struct device		*pd_pcie;
  99	/* power domain for pcie phy */
 100	struct device		*pd_pcie_phy;
 101	struct phy		*phy;
 102	const struct imx6_pcie_drvdata *drvdata;
 103};
 104
 105/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
 106#define PHY_PLL_LOCK_WAIT_USLEEP_MAX	200
 107#define PHY_PLL_LOCK_WAIT_TIMEOUT	(2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
 108
 109/* PCIe Port Logic registers (memory-mapped) */
 110#define PL_OFFSET 0x700
 111
 112#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
 113#define PCIE_PHY_CTRL_DATA(x)		FIELD_PREP(GENMASK(15, 0), (x))
 114#define PCIE_PHY_CTRL_CAP_ADR		BIT(16)
 115#define PCIE_PHY_CTRL_CAP_DAT		BIT(17)
 116#define PCIE_PHY_CTRL_WR		BIT(18)
 117#define PCIE_PHY_CTRL_RD		BIT(19)
 118
 119#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
 120#define PCIE_PHY_STAT_ACK		BIT(16)
 121
 122/* PHY registers (not memory-mapped) */
 123#define PCIE_PHY_ATEOVRD			0x10
 124#define  PCIE_PHY_ATEOVRD_EN			BIT(2)
 125#define  PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT	0
 126#define  PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK	0x1
 127
 128#define PCIE_PHY_MPLL_OVRD_IN_LO		0x11
 129#define  PCIE_PHY_MPLL_MULTIPLIER_SHIFT		2
 130#define  PCIE_PHY_MPLL_MULTIPLIER_MASK		0x7f
 131#define  PCIE_PHY_MPLL_MULTIPLIER_OVRD		BIT(9)
 132
 133#define PCIE_PHY_RX_ASIC_OUT 0x100D
 134#define PCIE_PHY_RX_ASIC_OUT_VALID	(1 << 0)
 135
 136/* iMX7 PCIe PHY registers */
 137#define PCIE_PHY_CMN_REG4		0x14
 138/* These are probably the bits that *aren't* DCC_FB_EN */
 139#define PCIE_PHY_CMN_REG4_DCC_FB_EN	0x29
 140
 141#define PCIE_PHY_CMN_REG15	        0x54
 142#define PCIE_PHY_CMN_REG15_DLY_4	BIT(2)
 143#define PCIE_PHY_CMN_REG15_PLL_PD	BIT(5)
 144#define PCIE_PHY_CMN_REG15_OVRD_PLL_PD	BIT(7)
 145
 146#define PCIE_PHY_CMN_REG24		0x90
 147#define PCIE_PHY_CMN_REG24_RX_EQ	BIT(6)
 148#define PCIE_PHY_CMN_REG24_RX_EQ_SEL	BIT(3)
 149
 150#define PCIE_PHY_CMN_REG26		0x98
 151#define PCIE_PHY_CMN_REG26_ATT_MODE	0xBC
 152
 153#define PHY_RX_OVRD_IN_LO 0x1005
 154#define PHY_RX_OVRD_IN_LO_RX_DATA_EN		BIT(5)
 155#define PHY_RX_OVRD_IN_LO_RX_PLL_EN		BIT(3)
 156
 157static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
 158{
 159	WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ &&
 160		imx6_pcie->drvdata->variant != IMX8MQ_EP &&
 161		imx6_pcie->drvdata->variant != IMX8MM &&
 162		imx6_pcie->drvdata->variant != IMX8MM_EP &&
 163		imx6_pcie->drvdata->variant != IMX8MP &&
 164		imx6_pcie->drvdata->variant != IMX8MP_EP);
 165	return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
 166}
 167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 168static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie)
 169{
 170	unsigned int mask, val, mode;
 
 171
 172	if (imx6_pcie->drvdata->mode == DW_PCIE_EP_TYPE)
 173		mode = PCI_EXP_TYPE_ENDPOINT;
 174	else
 175		mode = PCI_EXP_TYPE_ROOT_PORT;
 176
 177	switch (imx6_pcie->drvdata->variant) {
 178	case IMX8MQ:
 179	case IMX8MQ_EP:
 180		if (imx6_pcie->controller_id == 1) {
 181			mask = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE;
 182			val  = FIELD_PREP(IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
 183					  mode);
 184		} else {
 185			mask = IMX6Q_GPR12_DEVICE_TYPE;
 186			val  = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE, mode);
 187		}
 188		break;
 189	default:
 190		mask = IMX6Q_GPR12_DEVICE_TYPE;
 191		val  = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE, mode);
 192		break;
 193	}
 194
 195	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, mask, val);
 
 
 
 196}
 197
 198static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, bool exp_val)
 199{
 200	struct dw_pcie *pci = imx6_pcie->pci;
 201	bool val;
 202	u32 max_iterations = 10;
 203	u32 wait_counter = 0;
 204
 205	do {
 206		val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT) &
 207			PCIE_PHY_STAT_ACK;
 208		wait_counter++;
 209
 210		if (val == exp_val)
 211			return 0;
 212
 213		udelay(1);
 214	} while (wait_counter < max_iterations);
 215
 216	return -ETIMEDOUT;
 217}
 218
 219static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
 220{
 221	struct dw_pcie *pci = imx6_pcie->pci;
 222	u32 val;
 223	int ret;
 224
 225	val = PCIE_PHY_CTRL_DATA(addr);
 226	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
 227
 228	val |= PCIE_PHY_CTRL_CAP_ADR;
 229	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
 230
 231	ret = pcie_phy_poll_ack(imx6_pcie, true);
 232	if (ret)
 233		return ret;
 234
 235	val = PCIE_PHY_CTRL_DATA(addr);
 236	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
 237
 238	return pcie_phy_poll_ack(imx6_pcie, false);
 239}
 240
 241/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
 242static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, u16 *data)
 243{
 244	struct dw_pcie *pci = imx6_pcie->pci;
 245	u32 phy_ctl;
 246	int ret;
 247
 248	ret = pcie_phy_wait_ack(imx6_pcie, addr);
 249	if (ret)
 250		return ret;
 251
 252	/* assert Read signal */
 253	phy_ctl = PCIE_PHY_CTRL_RD;
 254	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
 255
 256	ret = pcie_phy_poll_ack(imx6_pcie, true);
 257	if (ret)
 258		return ret;
 259
 260	*data = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
 261
 262	/* deassert Read signal */
 263	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
 264
 265	return pcie_phy_poll_ack(imx6_pcie, false);
 266}
 267
 268static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, u16 data)
 269{
 270	struct dw_pcie *pci = imx6_pcie->pci;
 271	u32 var;
 272	int ret;
 273
 274	/* write addr */
 275	/* cap addr */
 276	ret = pcie_phy_wait_ack(imx6_pcie, addr);
 277	if (ret)
 278		return ret;
 279
 280	var = PCIE_PHY_CTRL_DATA(data);
 281	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 282
 283	/* capture data */
 284	var |= PCIE_PHY_CTRL_CAP_DAT;
 285	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 286
 287	ret = pcie_phy_poll_ack(imx6_pcie, true);
 288	if (ret)
 289		return ret;
 290
 291	/* deassert cap data */
 292	var = PCIE_PHY_CTRL_DATA(data);
 293	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 294
 295	/* wait for ack de-assertion */
 296	ret = pcie_phy_poll_ack(imx6_pcie, false);
 297	if (ret)
 298		return ret;
 299
 300	/* assert wr signal */
 301	var = PCIE_PHY_CTRL_WR;
 302	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 303
 304	/* wait for ack */
 305	ret = pcie_phy_poll_ack(imx6_pcie, true);
 306	if (ret)
 307		return ret;
 308
 309	/* deassert wr signal */
 310	var = PCIE_PHY_CTRL_DATA(data);
 311	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 312
 313	/* wait for ack de-assertion */
 314	ret = pcie_phy_poll_ack(imx6_pcie, false);
 315	if (ret)
 316		return ret;
 317
 318	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
 319
 320	return 0;
 321}
 322
 323static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
 324{
 325	switch (imx6_pcie->drvdata->variant) {
 326	case IMX8MM:
 327	case IMX8MM_EP:
 328	case IMX8MP:
 329	case IMX8MP_EP:
 330		/*
 331		 * The PHY initialization had been done in the PHY
 332		 * driver, break here directly.
 333		 */
 334		break;
 335	case IMX8MQ:
 336	case IMX8MQ_EP:
 337		/*
 338		 * TODO: Currently this code assumes external
 339		 * oscillator is being used
 340		 */
 341		regmap_update_bits(imx6_pcie->iomuxc_gpr,
 342				   imx6_pcie_grp_offset(imx6_pcie),
 343				   IMX8MQ_GPR_PCIE_REF_USE_PAD,
 344				   IMX8MQ_GPR_PCIE_REF_USE_PAD);
 345		/*
 346		 * Regarding the datasheet, the PCIE_VPH is suggested
 347		 * to be 1.8V. If the PCIE_VPH is supplied by 3.3V, the
 348		 * VREG_BYPASS should be cleared to zero.
 349		 */
 350		if (imx6_pcie->vph &&
 351		    regulator_get_voltage(imx6_pcie->vph) > 3000000)
 352			regmap_update_bits(imx6_pcie->iomuxc_gpr,
 353					   imx6_pcie_grp_offset(imx6_pcie),
 354					   IMX8MQ_GPR_PCIE_VREG_BYPASS,
 355					   0);
 356		break;
 357	case IMX7D:
 358		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 359				   IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
 360		break;
 361	case IMX6SX:
 362		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 363				   IMX6SX_GPR12_PCIE_RX_EQ_MASK,
 364				   IMX6SX_GPR12_PCIE_RX_EQ_2);
 365		fallthrough;
 366	default:
 367		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 368				   IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
 369
 370		/* configure constant input signal to the pcie ctrl and phy */
 371		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 372				   IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 373
 374		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 375				   IMX6Q_GPR8_TX_DEEMPH_GEN1,
 376				   imx6_pcie->tx_deemph_gen1 << 0);
 377		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 378				   IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
 379				   imx6_pcie->tx_deemph_gen2_3p5db << 6);
 380		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 381				   IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
 382				   imx6_pcie->tx_deemph_gen2_6db << 12);
 383		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 384				   IMX6Q_GPR8_TX_SWING_FULL,
 385				   imx6_pcie->tx_swing_full << 18);
 386		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 387				   IMX6Q_GPR8_TX_SWING_LOW,
 388				   imx6_pcie->tx_swing_low << 25);
 389		break;
 390	}
 391
 392	imx6_pcie_configure_type(imx6_pcie);
 393}
 394
 395static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
 396{
 397	u32 val;
 398	struct device *dev = imx6_pcie->pci->dev;
 399
 400	if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
 401				     IOMUXC_GPR22, val,
 402				     val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
 403				     PHY_PLL_LOCK_WAIT_USLEEP_MAX,
 404				     PHY_PLL_LOCK_WAIT_TIMEOUT))
 405		dev_err(dev, "PCIe PLL lock timeout\n");
 406}
 407
 408static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
 409{
 410	unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
 411	int mult, div;
 412	u16 val;
 
 413
 414	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
 415		return 0;
 416
 
 
 
 
 417	switch (phy_rate) {
 418	case 125000000:
 419		/*
 420		 * The default settings of the MPLL are for a 125MHz input
 421		 * clock, so no need to reconfigure anything in that case.
 422		 */
 423		return 0;
 424	case 100000000:
 425		mult = 25;
 426		div = 0;
 427		break;
 428	case 200000000:
 429		mult = 25;
 430		div = 1;
 431		break;
 432	default:
 433		dev_err(imx6_pcie->pci->dev,
 434			"Unsupported PHY reference clock rate %lu\n", phy_rate);
 435		return -EINVAL;
 436	}
 437
 438	pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
 439	val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
 440		 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
 441	val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
 442	val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
 443	pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
 444
 445	pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
 446	val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
 447		 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
 448	val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
 449	val |= PCIE_PHY_ATEOVRD_EN;
 450	pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
 451
 452	return 0;
 453}
 454
 455static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
 456{
 457	u16 tmp;
 458
 459	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
 460		return;
 461
 462	pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
 463	tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
 464		PHY_RX_OVRD_IN_LO_RX_PLL_EN);
 465	pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
 466
 467	usleep_range(2000, 3000);
 468
 469	pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
 470	tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
 471		  PHY_RX_OVRD_IN_LO_RX_PLL_EN);
 472	pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
 473}
 474
 475#ifdef CONFIG_ARM
 476/*  Added for PCI abort handling */
 477static int imx6q_pcie_abort_handler(unsigned long addr,
 478		unsigned int fsr, struct pt_regs *regs)
 479{
 480	unsigned long pc = instruction_pointer(regs);
 481	unsigned long instr = *(unsigned long *)pc;
 482	int reg = (instr >> 12) & 15;
 483
 484	/*
 485	 * If the instruction being executed was a read,
 486	 * make it look like it read all-ones.
 487	 */
 488	if ((instr & 0x0c100000) == 0x04100000) {
 489		unsigned long val;
 490
 491		if (instr & 0x00400000)
 492			val = 255;
 493		else
 494			val = -1;
 495
 496		regs->uregs[reg] = val;
 497		regs->ARM_pc += 4;
 498		return 0;
 499	}
 500
 501	if ((instr & 0x0e100090) == 0x00100090) {
 502		regs->uregs[reg] = -1;
 503		regs->ARM_pc += 4;
 504		return 0;
 505	}
 506
 507	return 1;
 508}
 509#endif
 510
 511static int imx6_pcie_attach_pd(struct device *dev)
 512{
 513	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
 514	struct device_link *link;
 515
 516	/* Do nothing when in a single power domain */
 517	if (dev->pm_domain)
 518		return 0;
 519
 520	imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
 521	if (IS_ERR(imx6_pcie->pd_pcie))
 522		return PTR_ERR(imx6_pcie->pd_pcie);
 523	/* Do nothing when power domain missing */
 524	if (!imx6_pcie->pd_pcie)
 525		return 0;
 526	link = device_link_add(dev, imx6_pcie->pd_pcie,
 527			DL_FLAG_STATELESS |
 528			DL_FLAG_PM_RUNTIME |
 529			DL_FLAG_RPM_ACTIVE);
 530	if (!link) {
 531		dev_err(dev, "Failed to add device_link to pcie pd.\n");
 532		return -EINVAL;
 533	}
 534
 535	imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
 536	if (IS_ERR(imx6_pcie->pd_pcie_phy))
 537		return PTR_ERR(imx6_pcie->pd_pcie_phy);
 538
 539	link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
 540			DL_FLAG_STATELESS |
 541			DL_FLAG_PM_RUNTIME |
 542			DL_FLAG_RPM_ACTIVE);
 543	if (!link) {
 544		dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
 545		return -EINVAL;
 546	}
 547
 548	return 0;
 549}
 550
 551static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
 552{
 553	struct dw_pcie *pci = imx6_pcie->pci;
 554	struct device *dev = pci->dev;
 555	unsigned int offset;
 556	int ret = 0;
 557
 558	switch (imx6_pcie->drvdata->variant) {
 559	case IMX6SX:
 560		ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
 561		if (ret) {
 562			dev_err(dev, "unable to enable pcie_axi clock\n");
 563			break;
 564		}
 565
 566		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 567				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
 568		break;
 569	case IMX6QP:
 570	case IMX6Q:
 571		/* power up core phy and enable ref clock */
 572		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 573				   IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
 574		/*
 575		 * the async reset input need ref clock to sync internally,
 576		 * when the ref clock comes after reset, internal synced
 577		 * reset time is too short, cannot meet the requirement.
 578		 * add one ~10us delay here.
 579		 */
 580		usleep_range(10, 100);
 581		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 582				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
 583		break;
 584	case IMX7D:
 
 
 585		break;
 586	case IMX8MM:
 587	case IMX8MM_EP:
 588	case IMX8MQ:
 589	case IMX8MQ_EP:
 590	case IMX8MP:
 591	case IMX8MP_EP:
 592		ret = clk_prepare_enable(imx6_pcie->pcie_aux);
 593		if (ret) {
 594			dev_err(dev, "unable to enable pcie_aux clock\n");
 595			break;
 596		}
 597
 598		offset = imx6_pcie_grp_offset(imx6_pcie);
 599		/*
 600		 * Set the over ride low and enabled
 601		 * make sure that REF_CLK is turned on.
 602		 */
 603		regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
 604				   IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
 605				   0);
 606		regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
 607				   IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
 608				   IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
 609		break;
 610	}
 611
 612	return ret;
 613}
 614
 615static void imx6_pcie_disable_ref_clk(struct imx6_pcie *imx6_pcie)
 616{
 617	switch (imx6_pcie->drvdata->variant) {
 618	case IMX6SX:
 619		clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
 620		break;
 621	case IMX6QP:
 622	case IMX6Q:
 623		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 624				IMX6Q_GPR1_PCIE_REF_CLK_EN, 0);
 625		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 626				IMX6Q_GPR1_PCIE_TEST_PD,
 627				IMX6Q_GPR1_PCIE_TEST_PD);
 628		break;
 629	case IMX7D:
 630		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 631				   IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
 632				   IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
 633		break;
 634	case IMX8MM:
 635	case IMX8MM_EP:
 636	case IMX8MQ:
 637	case IMX8MQ_EP:
 638	case IMX8MP:
 639	case IMX8MP_EP:
 640		clk_disable_unprepare(imx6_pcie->pcie_aux);
 641		break;
 642	default:
 643		break;
 644	}
 645}
 646
 647static int imx6_pcie_clk_enable(struct imx6_pcie *imx6_pcie)
 648{
 649	struct dw_pcie *pci = imx6_pcie->pci;
 650	struct device *dev = pci->dev;
 651	int ret;
 652
 653	ret = clk_prepare_enable(imx6_pcie->pcie_phy);
 654	if (ret) {
 655		dev_err(dev, "unable to enable pcie_phy clock\n");
 656		return ret;
 657	}
 658
 659	ret = clk_prepare_enable(imx6_pcie->pcie_bus);
 660	if (ret) {
 661		dev_err(dev, "unable to enable pcie_bus clock\n");
 662		goto err_pcie_bus;
 663	}
 664
 665	ret = clk_prepare_enable(imx6_pcie->pcie);
 666	if (ret) {
 667		dev_err(dev, "unable to enable pcie clock\n");
 668		goto err_pcie;
 669	}
 670
 671	ret = imx6_pcie_enable_ref_clk(imx6_pcie);
 672	if (ret) {
 673		dev_err(dev, "unable to enable pcie ref clock\n");
 674		goto err_ref_clk;
 675	}
 676
 677	/* allow the clocks to stabilize */
 678	usleep_range(200, 500);
 679	return 0;
 680
 681err_ref_clk:
 682	clk_disable_unprepare(imx6_pcie->pcie);
 683err_pcie:
 684	clk_disable_unprepare(imx6_pcie->pcie_bus);
 685err_pcie_bus:
 686	clk_disable_unprepare(imx6_pcie->pcie_phy);
 687
 688	return ret;
 689}
 690
 691static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
 692{
 693	imx6_pcie_disable_ref_clk(imx6_pcie);
 694	clk_disable_unprepare(imx6_pcie->pcie);
 695	clk_disable_unprepare(imx6_pcie->pcie_bus);
 696	clk_disable_unprepare(imx6_pcie->pcie_phy);
 697}
 698
 699static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
 700{
 
 
 
 701	switch (imx6_pcie->drvdata->variant) {
 702	case IMX7D:
 703	case IMX8MQ:
 704	case IMX8MQ_EP:
 705		reset_control_assert(imx6_pcie->pciephy_reset);
 706		fallthrough;
 707	case IMX8MM:
 708	case IMX8MM_EP:
 709	case IMX8MP:
 710	case IMX8MP_EP:
 711		reset_control_assert(imx6_pcie->apps_reset);
 712		break;
 713	case IMX6SX:
 714		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 715				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
 716				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
 717		/* Force PCIe PHY reset */
 718		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
 719				   IMX6SX_GPR5_PCIE_BTNRST_RESET,
 720				   IMX6SX_GPR5_PCIE_BTNRST_RESET);
 721		break;
 722	case IMX6QP:
 723		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 724				   IMX6Q_GPR1_PCIE_SW_RST,
 725				   IMX6Q_GPR1_PCIE_SW_RST);
 726		break;
 727	case IMX6Q:
 728		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 729				   IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
 730		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 731				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
 732		break;
 
 
 733	}
 734
 735	/* Some boards don't have PCIe reset GPIO. */
 736	if (gpio_is_valid(imx6_pcie->reset_gpio))
 737		gpio_set_value_cansleep(imx6_pcie->reset_gpio,
 738					imx6_pcie->gpio_active_high);
 739}
 740
 741static int imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
 742{
 743	struct dw_pcie *pci = imx6_pcie->pci;
 744	struct device *dev = pci->dev;
 745
 
 
 746	switch (imx6_pcie->drvdata->variant) {
 747	case IMX8MQ:
 748	case IMX8MQ_EP:
 749		reset_control_deassert(imx6_pcie->pciephy_reset);
 750		break;
 751	case IMX7D:
 752		reset_control_deassert(imx6_pcie->pciephy_reset);
 753
 754		/* Workaround for ERR010728, failure of PCI-e PLL VCO to
 755		 * oscillate, especially when cold.  This turns off "Duty-cycle
 756		 * Corrector" and other mysterious undocumented things.
 757		 */
 758		if (likely(imx6_pcie->phy_base)) {
 759			/* De-assert DCC_FB_EN */
 760			writel(PCIE_PHY_CMN_REG4_DCC_FB_EN,
 761			       imx6_pcie->phy_base + PCIE_PHY_CMN_REG4);
 762			/* Assert RX_EQS and RX_EQS_SEL */
 763			writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL
 764				| PCIE_PHY_CMN_REG24_RX_EQ,
 765			       imx6_pcie->phy_base + PCIE_PHY_CMN_REG24);
 766			/* Assert ATT_MODE */
 767			writel(PCIE_PHY_CMN_REG26_ATT_MODE,
 768			       imx6_pcie->phy_base + PCIE_PHY_CMN_REG26);
 769		} else {
 770			dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n");
 771		}
 772
 773		imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
 774		break;
 775	case IMX6SX:
 776		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
 777				   IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
 778		break;
 779	case IMX6QP:
 780		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 781				   IMX6Q_GPR1_PCIE_SW_RST, 0);
 782
 783		usleep_range(200, 500);
 784		break;
 785	case IMX6Q:		/* Nothing to do */
 786	case IMX8MM:
 787	case IMX8MM_EP:
 788	case IMX8MP:
 789	case IMX8MP_EP:
 790		break;
 791	}
 792
 793	/* Some boards don't have PCIe reset GPIO. */
 794	if (gpio_is_valid(imx6_pcie->reset_gpio)) {
 795		msleep(100);
 796		gpio_set_value_cansleep(imx6_pcie->reset_gpio,
 797					!imx6_pcie->gpio_active_high);
 798		/* Wait for 100ms after PERST# deassertion (PCIe r5.0, 6.6.1) */
 799		msleep(100);
 800	}
 801
 802	return 0;
 803}
 804
 805static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
 806{
 807	struct dw_pcie *pci = imx6_pcie->pci;
 808	struct device *dev = pci->dev;
 809	u32 tmp;
 810	unsigned int retries;
 811
 812	for (retries = 0; retries < 200; retries++) {
 813		tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
 814		/* Test if the speed change finished. */
 815		if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
 816			return 0;
 817		usleep_range(100, 1000);
 818	}
 819
 820	dev_err(dev, "Speed change timeout\n");
 821	return -ETIMEDOUT;
 822}
 823
 824static void imx6_pcie_ltssm_enable(struct device *dev)
 825{
 826	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
 
 827
 828	switch (imx6_pcie->drvdata->variant) {
 829	case IMX6Q:
 830	case IMX6SX:
 831	case IMX6QP:
 832		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 833				   IMX6Q_GPR12_PCIE_CTL_2,
 834				   IMX6Q_GPR12_PCIE_CTL_2);
 835		break;
 836	case IMX7D:
 837	case IMX8MQ:
 838	case IMX8MQ_EP:
 839	case IMX8MM:
 840	case IMX8MM_EP:
 841	case IMX8MP:
 842	case IMX8MP_EP:
 843		reset_control_deassert(imx6_pcie->apps_reset);
 844		break;
 845	}
 846}
 847
 848static void imx6_pcie_ltssm_disable(struct device *dev)
 849{
 850	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
 
 851
 852	switch (imx6_pcie->drvdata->variant) {
 853	case IMX6Q:
 854	case IMX6SX:
 855	case IMX6QP:
 856		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 857				   IMX6Q_GPR12_PCIE_CTL_2, 0);
 858		break;
 859	case IMX7D:
 860	case IMX8MQ:
 861	case IMX8MQ_EP:
 862	case IMX8MM:
 863	case IMX8MM_EP:
 864	case IMX8MP:
 865	case IMX8MP_EP:
 866		reset_control_assert(imx6_pcie->apps_reset);
 867		break;
 868	}
 869}
 870
 871static int imx6_pcie_start_link(struct dw_pcie *pci)
 872{
 873	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
 874	struct device *dev = pci->dev;
 875	u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 876	u32 tmp;
 877	int ret;
 878
 879	/*
 880	 * Force Gen1 operation when starting the link.  In case the link is
 881	 * started in Gen2 mode, there is a possibility the devices on the
 882	 * bus will not be detected at all.  This happens with PCIe switches.
 883	 */
 884	dw_pcie_dbi_ro_wr_en(pci);
 885	tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
 886	tmp &= ~PCI_EXP_LNKCAP_SLS;
 887	tmp |= PCI_EXP_LNKCAP_SLS_2_5GB;
 888	dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
 889	dw_pcie_dbi_ro_wr_dis(pci);
 890
 891	/* Start LTSSM. */
 892	imx6_pcie_ltssm_enable(dev);
 893
 894	ret = dw_pcie_wait_for_link(pci);
 895	if (ret)
 896		goto err_reset_phy;
 897
 898	if (pci->link_gen > 1) {
 899		/* Allow faster modes after the link is up */
 900		dw_pcie_dbi_ro_wr_en(pci);
 901		tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
 902		tmp &= ~PCI_EXP_LNKCAP_SLS;
 903		tmp |= pci->link_gen;
 904		dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
 905
 906		/*
 907		 * Start Directed Speed Change so the best possible
 908		 * speed both link partners support can be negotiated.
 909		 */
 910		tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
 911		tmp |= PORT_LOGIC_SPEED_CHANGE;
 912		dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
 913		dw_pcie_dbi_ro_wr_dis(pci);
 914
 915		if (imx6_pcie->drvdata->flags &
 916		    IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) {
 917			/*
 918			 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
 919			 * from i.MX6 family when no link speed transition
 920			 * occurs and we go Gen1 -> yep, Gen1. The difference
 921			 * is that, in such case, it will not be cleared by HW
 922			 * which will cause the following code to report false
 923			 * failure.
 924			 */
 925
 926			ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
 927			if (ret) {
 928				dev_err(dev, "Failed to bring link up!\n");
 929				goto err_reset_phy;
 930			}
 931		}
 932
 933		/* Make sure link training is finished as well! */
 934		ret = dw_pcie_wait_for_link(pci);
 935		if (ret)
 936			goto err_reset_phy;
 937	} else {
 938		dev_info(dev, "Link: Only Gen1 is enabled\n");
 939	}
 940
 941	imx6_pcie->link_is_up = true;
 942	tmp = dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKSTA);
 943	dev_info(dev, "Link up, Gen%i\n", tmp & PCI_EXP_LNKSTA_CLS);
 944	return 0;
 945
 946err_reset_phy:
 947	imx6_pcie->link_is_up = false;
 948	dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
 949		dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0),
 950		dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG1));
 951	imx6_pcie_reset_phy(imx6_pcie);
 952	return 0;
 953}
 954
 955static void imx6_pcie_stop_link(struct dw_pcie *pci)
 956{
 957	struct device *dev = pci->dev;
 958
 959	/* Turn off PCIe LTSSM */
 960	imx6_pcie_ltssm_disable(dev);
 961}
 962
 963static int imx6_pcie_host_init(struct dw_pcie_rp *pp)
 964{
 965	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 966	struct device *dev = pci->dev;
 967	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
 968	int ret;
 969
 970	if (imx6_pcie->vpcie) {
 971		ret = regulator_enable(imx6_pcie->vpcie);
 972		if (ret) {
 973			dev_err(dev, "failed to enable vpcie regulator: %d\n",
 974				ret);
 975			return ret;
 976		}
 977	}
 978
 979	imx6_pcie_assert_core_reset(imx6_pcie);
 980	imx6_pcie_init_phy(imx6_pcie);
 
 
 
 
 981
 982	ret = imx6_pcie_clk_enable(imx6_pcie);
 983	if (ret) {
 984		dev_err(dev, "unable to enable pcie clocks: %d\n", ret);
 985		goto err_reg_disable;
 986	}
 987
 988	if (imx6_pcie->phy) {
 989		ret = phy_init(imx6_pcie->phy);
 990		if (ret) {
 991			dev_err(dev, "pcie PHY power up failed\n");
 992			goto err_clk_disable;
 993		}
 994	}
 995
 996	if (imx6_pcie->phy) {
 997		ret = phy_power_on(imx6_pcie->phy);
 998		if (ret) {
 999			dev_err(dev, "waiting for PHY ready timeout!\n");
1000			goto err_phy_off;
1001		}
1002	}
1003
1004	ret = imx6_pcie_deassert_core_reset(imx6_pcie);
1005	if (ret < 0) {
1006		dev_err(dev, "pcie deassert core reset failed: %d\n", ret);
1007		goto err_phy_off;
1008	}
1009
1010	imx6_setup_phy_mpll(imx6_pcie);
1011
1012	return 0;
1013
1014err_phy_off:
1015	if (imx6_pcie->phy)
1016		phy_exit(imx6_pcie->phy);
1017err_clk_disable:
1018	imx6_pcie_clk_disable(imx6_pcie);
1019err_reg_disable:
1020	if (imx6_pcie->vpcie)
1021		regulator_disable(imx6_pcie->vpcie);
1022	return ret;
1023}
1024
1025static void imx6_pcie_host_exit(struct dw_pcie_rp *pp)
1026{
1027	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1028	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1029
1030	if (imx6_pcie->phy) {
1031		if (phy_power_off(imx6_pcie->phy))
1032			dev_err(pci->dev, "unable to power off PHY\n");
1033		phy_exit(imx6_pcie->phy);
1034	}
1035	imx6_pcie_clk_disable(imx6_pcie);
1036
1037	if (imx6_pcie->vpcie)
1038		regulator_disable(imx6_pcie->vpcie);
1039}
1040
1041static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
1042	.init = imx6_pcie_host_init,
1043	.deinit = imx6_pcie_host_exit,
1044};
1045
1046static const struct dw_pcie_ops dw_pcie_ops = {
1047	.start_link = imx6_pcie_start_link,
1048	.stop_link = imx6_pcie_stop_link,
1049};
1050
1051static void imx6_pcie_ep_init(struct dw_pcie_ep *ep)
1052{
1053	enum pci_barno bar;
1054	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1055
1056	for (bar = BAR_0; bar <= BAR_5; bar++)
1057		dw_pcie_ep_reset_bar(pci, bar);
1058}
1059
1060static int imx6_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
1061				  unsigned int type, u16 interrupt_num)
1062{
1063	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1064
1065	switch (type) {
1066	case PCI_IRQ_INTX:
1067		return dw_pcie_ep_raise_intx_irq(ep, func_no);
1068	case PCI_IRQ_MSI:
1069		return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
1070	case PCI_IRQ_MSIX:
1071		return dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
1072	default:
1073		dev_err(pci->dev, "UNKNOWN IRQ type\n");
1074		return -EINVAL;
1075	}
1076
1077	return 0;
1078}
1079
1080static const struct pci_epc_features imx8m_pcie_epc_features = {
1081	.linkup_notifier = false,
1082	.msi_capable = true,
1083	.msix_capable = false,
1084	.reserved_bar = 1 << BAR_1 | 1 << BAR_3,
 
1085	.align = SZ_64K,
1086};
1087
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1088static const struct pci_epc_features*
1089imx6_pcie_ep_get_features(struct dw_pcie_ep *ep)
1090{
1091	return &imx8m_pcie_epc_features;
 
 
 
1092}
1093
1094static const struct dw_pcie_ep_ops pcie_ep_ops = {
1095	.init = imx6_pcie_ep_init,
1096	.raise_irq = imx6_pcie_ep_raise_irq,
1097	.get_features = imx6_pcie_ep_get_features,
1098};
1099
1100static int imx6_add_pcie_ep(struct imx6_pcie *imx6_pcie,
1101			   struct platform_device *pdev)
1102{
1103	int ret;
1104	unsigned int pcie_dbi2_offset;
1105	struct dw_pcie_ep *ep;
1106	struct resource *res;
1107	struct dw_pcie *pci = imx6_pcie->pci;
1108	struct dw_pcie_rp *pp = &pci->pp;
1109	struct device *dev = pci->dev;
1110
1111	imx6_pcie_host_init(pp);
1112	ep = &pci->ep;
1113	ep->ops = &pcie_ep_ops;
1114
1115	switch (imx6_pcie->drvdata->variant) {
1116	case IMX8MQ_EP:
1117	case IMX8MM_EP:
1118	case IMX8MP_EP:
1119		pcie_dbi2_offset = SZ_1M;
1120		break;
1121	default:
1122		pcie_dbi2_offset = SZ_4K;
1123		break;
1124	}
 
1125	pci->dbi_base2 = pci->dbi_base + pcie_dbi2_offset;
1126	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
1127	if (!res)
1128		return -EINVAL;
1129
1130	ep->phys_base = res->start;
1131	ep->addr_size = resource_size(res);
1132	ep->page_size = SZ_64K;
 
 
 
 
 
 
 
 
1133
1134	ret = dw_pcie_ep_init(ep);
1135	if (ret) {
1136		dev_err(dev, "failed to initialize endpoint\n");
1137		return ret;
1138	}
1139	/* Start LTSSM. */
1140	imx6_pcie_ltssm_enable(dev);
1141
1142	return 0;
1143}
1144
1145static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
1146{
1147	struct device *dev = imx6_pcie->pci->dev;
1148
1149	/* Some variants have a turnoff reset in DT */
1150	if (imx6_pcie->turnoff_reset) {
1151		reset_control_assert(imx6_pcie->turnoff_reset);
1152		reset_control_deassert(imx6_pcie->turnoff_reset);
1153		goto pm_turnoff_sleep;
1154	}
1155
1156	/* Others poke directly at IOMUXC registers */
1157	switch (imx6_pcie->drvdata->variant) {
1158	case IMX6SX:
1159	case IMX6QP:
1160		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
1161				IMX6SX_GPR12_PCIE_PM_TURN_OFF,
1162				IMX6SX_GPR12_PCIE_PM_TURN_OFF);
1163		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
1164				IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
1165		break;
1166	default:
1167		dev_err(dev, "PME_Turn_Off not implemented\n");
1168		return;
1169	}
1170
1171	/*
1172	 * Components with an upstream port must respond to
1173	 * PME_Turn_Off with PME_TO_Ack but we can't check.
1174	 *
1175	 * The standard recommends a 1-10ms timeout after which to
1176	 * proceed anyway as if acks were received.
1177	 */
1178pm_turnoff_sleep:
1179	usleep_range(1000, 10000);
1180}
1181
1182static void imx6_pcie_msi_save_restore(struct imx6_pcie *imx6_pcie, bool save)
1183{
1184	u8 offset;
1185	u16 val;
1186	struct dw_pcie *pci = imx6_pcie->pci;
1187
1188	if (pci_msi_enabled()) {
1189		offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
1190		if (save) {
1191			val = dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
1192			imx6_pcie->msi_ctrl = val;
1193		} else {
1194			dw_pcie_dbi_ro_wr_en(pci);
1195			val = imx6_pcie->msi_ctrl;
1196			dw_pcie_writew_dbi(pci, offset + PCI_MSI_FLAGS, val);
1197			dw_pcie_dbi_ro_wr_dis(pci);
1198		}
1199	}
1200}
1201
1202static int imx6_pcie_suspend_noirq(struct device *dev)
1203{
1204	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
1205	struct dw_pcie_rp *pp = &imx6_pcie->pci->pp;
1206
1207	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
1208		return 0;
1209
1210	imx6_pcie_msi_save_restore(imx6_pcie, true);
1211	imx6_pcie_pm_turnoff(imx6_pcie);
1212	imx6_pcie_stop_link(imx6_pcie->pci);
1213	imx6_pcie_host_exit(pp);
1214
1215	return 0;
1216}
1217
1218static int imx6_pcie_resume_noirq(struct device *dev)
1219{
1220	int ret;
1221	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
1222	struct dw_pcie_rp *pp = &imx6_pcie->pci->pp;
1223
1224	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
1225		return 0;
1226
1227	ret = imx6_pcie_host_init(pp);
1228	if (ret)
1229		return ret;
1230	imx6_pcie_msi_save_restore(imx6_pcie, false);
1231	dw_pcie_setup_rc(pp);
1232
1233	if (imx6_pcie->link_is_up)
1234		imx6_pcie_start_link(imx6_pcie->pci);
1235
1236	return 0;
1237}
1238
1239static const struct dev_pm_ops imx6_pcie_pm_ops = {
1240	NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
1241				  imx6_pcie_resume_noirq)
1242};
1243
1244static int imx6_pcie_probe(struct platform_device *pdev)
1245{
1246	struct device *dev = &pdev->dev;
1247	struct dw_pcie *pci;
1248	struct imx6_pcie *imx6_pcie;
1249	struct device_node *np;
1250	struct resource *dbi_base;
1251	struct device_node *node = dev->of_node;
1252	int ret;
1253	u16 val;
 
1254
1255	imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
1256	if (!imx6_pcie)
1257		return -ENOMEM;
1258
1259	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1260	if (!pci)
1261		return -ENOMEM;
1262
1263	pci->dev = dev;
1264	pci->ops = &dw_pcie_ops;
1265	pci->pp.ops = &imx6_pcie_host_ops;
1266
1267	imx6_pcie->pci = pci;
1268	imx6_pcie->drvdata = of_device_get_match_data(dev);
1269
1270	/* Find the PHY if one is defined, only imx7d uses it */
1271	np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0);
1272	if (np) {
1273		struct resource res;
1274
1275		ret = of_address_to_resource(np, 0, &res);
1276		if (ret) {
1277			dev_err(dev, "Unable to map PCIe PHY\n");
1278			return ret;
1279		}
1280		imx6_pcie->phy_base = devm_ioremap_resource(dev, &res);
1281		if (IS_ERR(imx6_pcie->phy_base))
1282			return PTR_ERR(imx6_pcie->phy_base);
1283	}
1284
1285	pci->dbi_base = devm_platform_get_and_ioremap_resource(pdev, 0, &dbi_base);
1286	if (IS_ERR(pci->dbi_base))
1287		return PTR_ERR(pci->dbi_base);
1288
1289	/* Fetch GPIOs */
1290	imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
1291	imx6_pcie->gpio_active_high = of_property_read_bool(node,
1292						"reset-gpio-active-high");
1293	if (gpio_is_valid(imx6_pcie->reset_gpio)) {
1294		ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
1295				imx6_pcie->gpio_active_high ?
1296					GPIOF_OUT_INIT_HIGH :
1297					GPIOF_OUT_INIT_LOW,
1298				"PCIe reset");
1299		if (ret) {
1300			dev_err(dev, "unable to get reset gpio\n");
1301			return ret;
1302		}
1303	} else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
1304		return imx6_pcie->reset_gpio;
1305	}
1306
1307	/* Fetch clocks */
1308	imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
1309	if (IS_ERR(imx6_pcie->pcie_bus))
1310		return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_bus),
1311				     "pcie_bus clock source missing or invalid\n");
1312
1313	imx6_pcie->pcie = devm_clk_get(dev, "pcie");
1314	if (IS_ERR(imx6_pcie->pcie))
1315		return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie),
1316				     "pcie clock source missing or invalid\n");
1317
1318	switch (imx6_pcie->drvdata->variant) {
1319	case IMX6SX:
1320		imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
1321							   "pcie_inbound_axi");
1322		if (IS_ERR(imx6_pcie->pcie_inbound_axi))
1323			return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_inbound_axi),
1324					     "pcie_inbound_axi clock missing or invalid\n");
1325		break;
1326	case IMX8MQ:
1327	case IMX8MQ_EP:
1328		imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux");
1329		if (IS_ERR(imx6_pcie->pcie_aux))
1330			return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_aux),
1331					     "pcie_aux clock source missing or invalid\n");
1332		fallthrough;
1333	case IMX7D:
1334		if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR)
1335			imx6_pcie->controller_id = 1;
1336
1337		imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
1338									    "pciephy");
1339		if (IS_ERR(imx6_pcie->pciephy_reset)) {
1340			dev_err(dev, "Failed to get PCIEPHY reset control\n");
1341			return PTR_ERR(imx6_pcie->pciephy_reset);
1342		}
1343
1344		imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
1345									 "apps");
1346		if (IS_ERR(imx6_pcie->apps_reset)) {
1347			dev_err(dev, "Failed to get PCIE APPS reset control\n");
1348			return PTR_ERR(imx6_pcie->apps_reset);
1349		}
1350		break;
1351	case IMX8MM:
1352	case IMX8MM_EP:
1353	case IMX8MP:
1354	case IMX8MP_EP:
1355		imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux");
1356		if (IS_ERR(imx6_pcie->pcie_aux))
1357			return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_aux),
1358					     "pcie_aux clock source missing or invalid\n");
1359		imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
1360									 "apps");
1361		if (IS_ERR(imx6_pcie->apps_reset))
1362			return dev_err_probe(dev, PTR_ERR(imx6_pcie->apps_reset),
1363					     "failed to get pcie apps reset control\n");
1364
 
1365		imx6_pcie->phy = devm_phy_get(dev, "pcie-phy");
1366		if (IS_ERR(imx6_pcie->phy))
1367			return dev_err_probe(dev, PTR_ERR(imx6_pcie->phy),
1368					     "failed to get pcie phy\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1369
 
 
 
 
 
 
1370		break;
1371	default:
1372		break;
1373	}
1374	/* Don't fetch the pcie_phy clock, if it has abstract PHY driver */
1375	if (imx6_pcie->phy == NULL) {
1376		imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
1377		if (IS_ERR(imx6_pcie->pcie_phy))
1378			return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_phy),
1379					     "pcie_phy clock source missing or invalid\n");
1380	}
1381
1382
1383	/* Grab turnoff reset */
1384	imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
1385	if (IS_ERR(imx6_pcie->turnoff_reset)) {
1386		dev_err(dev, "Failed to get TURNOFF reset control\n");
1387		return PTR_ERR(imx6_pcie->turnoff_reset);
1388	}
1389
 
1390	/* Grab GPR config register range */
1391	imx6_pcie->iomuxc_gpr =
1392		 syscon_regmap_lookup_by_compatible(imx6_pcie->drvdata->gpr);
1393	if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
1394		dev_err(dev, "unable to find iomuxc registers\n");
1395		return PTR_ERR(imx6_pcie->iomuxc_gpr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1396	}
1397
1398	/* Grab PCIe PHY Tx Settings */
1399	if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
1400				 &imx6_pcie->tx_deemph_gen1))
1401		imx6_pcie->tx_deemph_gen1 = 0;
1402
1403	if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
1404				 &imx6_pcie->tx_deemph_gen2_3p5db))
1405		imx6_pcie->tx_deemph_gen2_3p5db = 0;
1406
1407	if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
1408				 &imx6_pcie->tx_deemph_gen2_6db))
1409		imx6_pcie->tx_deemph_gen2_6db = 20;
1410
1411	if (of_property_read_u32(node, "fsl,tx-swing-full",
1412				 &imx6_pcie->tx_swing_full))
1413		imx6_pcie->tx_swing_full = 127;
1414
1415	if (of_property_read_u32(node, "fsl,tx-swing-low",
1416				 &imx6_pcie->tx_swing_low))
1417		imx6_pcie->tx_swing_low = 127;
1418
1419	/* Limit link speed */
1420	pci->link_gen = 1;
1421	of_property_read_u32(node, "fsl,max-link-speed", &pci->link_gen);
1422
1423	imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
1424	if (IS_ERR(imx6_pcie->vpcie)) {
1425		if (PTR_ERR(imx6_pcie->vpcie) != -ENODEV)
1426			return PTR_ERR(imx6_pcie->vpcie);
1427		imx6_pcie->vpcie = NULL;
1428	}
1429
1430	imx6_pcie->vph = devm_regulator_get_optional(&pdev->dev, "vph");
1431	if (IS_ERR(imx6_pcie->vph)) {
1432		if (PTR_ERR(imx6_pcie->vph) != -ENODEV)
1433			return PTR_ERR(imx6_pcie->vph);
1434		imx6_pcie->vph = NULL;
1435	}
1436
1437	platform_set_drvdata(pdev, imx6_pcie);
1438
1439	ret = imx6_pcie_attach_pd(dev);
1440	if (ret)
1441		return ret;
1442
1443	if (imx6_pcie->drvdata->mode == DW_PCIE_EP_TYPE) {
1444		ret = imx6_add_pcie_ep(imx6_pcie, pdev);
1445		if (ret < 0)
1446			return ret;
1447	} else {
1448		ret = dw_pcie_host_init(&pci->pp);
1449		if (ret < 0)
1450			return ret;
1451
1452		if (pci_msi_enabled()) {
1453			u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
1454
1455			val = dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
1456			val |= PCI_MSI_FLAGS_ENABLE;
1457			dw_pcie_writew_dbi(pci, offset + PCI_MSI_FLAGS, val);
1458		}
1459	}
1460
1461	return 0;
1462}
1463
1464static void imx6_pcie_shutdown(struct platform_device *pdev)
1465{
1466	struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1467
1468	/* bring down link, so bootloader gets clean state in case of reboot */
1469	imx6_pcie_assert_core_reset(imx6_pcie);
1470}
1471
 
 
 
 
 
1472static const struct imx6_pcie_drvdata drvdata[] = {
1473	[IMX6Q] = {
1474		.variant = IMX6Q,
1475		.flags = IMX6_PCIE_FLAG_IMX6_PHY |
1476			 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
1477		.dbi_length = 0x200,
1478		.gpr = "fsl,imx6q-iomuxc-gpr",
 
 
 
 
 
 
 
1479	},
1480	[IMX6SX] = {
1481		.variant = IMX6SX,
1482		.flags = IMX6_PCIE_FLAG_IMX6_PHY |
1483			 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1484			 IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1485		.gpr = "fsl,imx6q-iomuxc-gpr",
 
 
 
 
 
 
 
1486	},
1487	[IMX6QP] = {
1488		.variant = IMX6QP,
1489		.flags = IMX6_PCIE_FLAG_IMX6_PHY |
1490			 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1491			 IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1492		.dbi_length = 0x200,
1493		.gpr = "fsl,imx6q-iomuxc-gpr",
 
 
 
 
 
 
 
1494	},
1495	[IMX7D] = {
1496		.variant = IMX7D,
1497		.flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
 
 
1498		.gpr = "fsl,imx7d-iomuxc-gpr",
 
 
 
 
 
1499	},
1500	[IMX8MQ] = {
1501		.variant = IMX8MQ,
 
 
1502		.gpr = "fsl,imx8mq-iomuxc-gpr",
 
 
 
 
 
 
 
1503	},
1504	[IMX8MM] = {
1505		.variant = IMX8MM,
1506		.flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
 
 
1507		.gpr = "fsl,imx8mm-iomuxc-gpr",
 
 
 
 
1508	},
1509	[IMX8MP] = {
1510		.variant = IMX8MP,
1511		.flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
 
 
1512		.gpr = "fsl,imx8mp-iomuxc-gpr",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1513	},
1514	[IMX8MQ_EP] = {
1515		.variant = IMX8MQ_EP,
 
 
1516		.mode = DW_PCIE_EP_TYPE,
1517		.gpr = "fsl,imx8mq-iomuxc-gpr",
 
 
 
 
 
 
 
 
1518	},
1519	[IMX8MM_EP] = {
1520		.variant = IMX8MM_EP,
 
1521		.mode = DW_PCIE_EP_TYPE,
1522		.gpr = "fsl,imx8mm-iomuxc-gpr",
 
 
 
 
 
1523	},
1524	[IMX8MP_EP] = {
1525		.variant = IMX8MP_EP,
 
1526		.mode = DW_PCIE_EP_TYPE,
1527		.gpr = "fsl,imx8mp-iomuxc-gpr",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1528	},
1529};
1530
1531static const struct of_device_id imx6_pcie_of_match[] = {
1532	{ .compatible = "fsl,imx6q-pcie",  .data = &drvdata[IMX6Q],  },
1533	{ .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], },
1534	{ .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], },
1535	{ .compatible = "fsl,imx7d-pcie",  .data = &drvdata[IMX7D],  },
1536	{ .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], },
1537	{ .compatible = "fsl,imx8mm-pcie", .data = &drvdata[IMX8MM], },
1538	{ .compatible = "fsl,imx8mp-pcie", .data = &drvdata[IMX8MP], },
 
1539	{ .compatible = "fsl,imx8mq-pcie-ep", .data = &drvdata[IMX8MQ_EP], },
1540	{ .compatible = "fsl,imx8mm-pcie-ep", .data = &drvdata[IMX8MM_EP], },
1541	{ .compatible = "fsl,imx8mp-pcie-ep", .data = &drvdata[IMX8MP_EP], },
 
1542	{},
1543};
1544
1545static struct platform_driver imx6_pcie_driver = {
1546	.driver = {
1547		.name	= "imx6q-pcie",
1548		.of_match_table = imx6_pcie_of_match,
1549		.suppress_bind_attrs = true,
1550		.pm = &imx6_pcie_pm_ops,
1551		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1552	},
1553	.probe    = imx6_pcie_probe,
1554	.shutdown = imx6_pcie_shutdown,
1555};
1556
1557static void imx6_pcie_quirk(struct pci_dev *dev)
1558{
1559	struct pci_bus *bus = dev->bus;
1560	struct dw_pcie_rp *pp = bus->sysdata;
1561
1562	/* Bus parent is the PCI bridge, its parent is this platform driver */
1563	if (!bus->dev.parent || !bus->dev.parent->parent)
1564		return;
1565
1566	/* Make sure we only quirk devices associated with this driver */
1567	if (bus->dev.parent->parent->driver != &imx6_pcie_driver.driver)
1568		return;
1569
1570	if (pci_is_root_bus(bus)) {
1571		struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1572		struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1573
1574		/*
1575		 * Limit config length to avoid the kernel reading beyond
1576		 * the register set and causing an abort on i.MX 6Quad
1577		 */
1578		if (imx6_pcie->drvdata->dbi_length) {
1579			dev->cfg_size = imx6_pcie->drvdata->dbi_length;
1580			dev_info(&dev->dev, "Limiting cfg_size to %d\n",
1581					dev->cfg_size);
1582		}
1583	}
1584}
1585DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_SYNOPSYS, 0xabcd,
1586			PCI_CLASS_BRIDGE_PCI, 8, imx6_pcie_quirk);
1587
1588static int __init imx6_pcie_init(void)
1589{
1590#ifdef CONFIG_ARM
1591	struct device_node *np;
1592
1593	np = of_find_matching_node(NULL, imx6_pcie_of_match);
1594	if (!np)
1595		return -ENODEV;
1596	of_node_put(np);
1597
1598	/*
1599	 * Since probe() can be deferred we need to make sure that
1600	 * hook_fault_code is not called after __init memory is freed
1601	 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1602	 * we can install the handler here without risking it
1603	 * accessing some uninitialized driver state.
1604	 */
1605	hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1606			"external abort on non-linefetch");
1607#endif
1608
1609	return platform_driver_register(&imx6_pcie_driver);
1610}
1611device_initcall(imx6_pcie_init);
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * PCIe host controller driver for Freescale i.MX6 SoCs
   4 *
   5 * Copyright (C) 2013 Kosagi
   6 *		https://www.kosagi.com
   7 *
   8 * Author: Sean Cross <xobs@kosagi.com>
   9 */
  10
  11#include <linux/bitfield.h>
  12#include <linux/clk.h>
  13#include <linux/delay.h>
  14#include <linux/gpio.h>
  15#include <linux/kernel.h>
  16#include <linux/mfd/syscon.h>
  17#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  18#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
  19#include <linux/module.h>
  20#include <linux/of.h>
  21#include <linux/of_gpio.h>
  22#include <linux/of_address.h>
  23#include <linux/pci.h>
  24#include <linux/platform_device.h>
  25#include <linux/regmap.h>
  26#include <linux/regulator/consumer.h>
  27#include <linux/resource.h>
  28#include <linux/signal.h>
  29#include <linux/types.h>
  30#include <linux/interrupt.h>
  31#include <linux/reset.h>
  32#include <linux/phy/phy.h>
  33#include <linux/pm_domain.h>
  34#include <linux/pm_runtime.h>
  35
  36#include "pcie-designware.h"
  37
  38#define IMX8MQ_GPR_PCIE_REF_USE_PAD		BIT(9)
  39#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN	BIT(10)
  40#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE	BIT(11)
  41#define IMX8MQ_GPR_PCIE_VREG_BYPASS		BIT(12)
  42#define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE	GENMASK(11, 8)
  43#define IMX8MQ_PCIE2_BASE_ADDR			0x33c00000
  44
  45#define IMX95_PCIE_PHY_GEN_CTRL			0x0
  46#define IMX95_PCIE_REF_USE_PAD			BIT(17)
  47
  48#define IMX95_PCIE_SS_RW_REG_0			0xf0
  49#define IMX95_PCIE_REF_CLKEN			BIT(23)
  50#define IMX95_PCIE_PHY_CR_PARA_SEL		BIT(9)
  51
  52#define IMX95_PE0_GEN_CTRL_1			0x1050
  53#define IMX95_PCIE_DEVICE_TYPE			GENMASK(3, 0)
  54
  55#define IMX95_PE0_GEN_CTRL_3			0x1058
  56#define IMX95_PCIE_LTSSM_EN			BIT(0)
  57
  58#define to_imx6_pcie(x)	dev_get_drvdata((x)->dev)
  59
  60enum imx6_pcie_variants {
  61	IMX6Q,
  62	IMX6SX,
  63	IMX6QP,
  64	IMX7D,
  65	IMX8MQ,
  66	IMX8MM,
  67	IMX8MP,
  68	IMX95,
  69	IMX8MQ_EP,
  70	IMX8MM_EP,
  71	IMX8MP_EP,
  72	IMX95_EP,
  73};
  74
  75#define IMX6_PCIE_FLAG_IMX6_PHY			BIT(0)
  76#define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE	BIT(1)
  77#define IMX6_PCIE_FLAG_SUPPORTS_SUSPEND		BIT(2)
  78#define IMX6_PCIE_FLAG_HAS_PHYDRV			BIT(3)
  79#define IMX6_PCIE_FLAG_HAS_APP_RESET		BIT(4)
  80#define IMX6_PCIE_FLAG_HAS_PHY_RESET		BIT(5)
  81#define IMX6_PCIE_FLAG_HAS_SERDES		BIT(6)
  82#define IMX6_PCIE_FLAG_SUPPORT_64BIT		BIT(7)
  83
  84#define imx6_check_flag(pci, val)     (pci->drvdata->flags & val)
  85
  86#define IMX6_PCIE_MAX_CLKS       6
  87
  88#define IMX6_PCIE_MAX_INSTANCES			2
  89
  90struct imx6_pcie;
  91
  92struct imx6_pcie_drvdata {
  93	enum imx6_pcie_variants variant;
  94	enum dw_pcie_device_mode mode;
  95	u32 flags;
  96	int dbi_length;
  97	const char *gpr;
  98	const char * const *clk_names;
  99	const u32 clks_cnt;
 100	const u32 ltssm_off;
 101	const u32 ltssm_mask;
 102	const u32 mode_off[IMX6_PCIE_MAX_INSTANCES];
 103	const u32 mode_mask[IMX6_PCIE_MAX_INSTANCES];
 104	const struct pci_epc_features *epc_features;
 105	int (*init_phy)(struct imx6_pcie *pcie);
 106};
 107
 108struct imx6_pcie {
 109	struct dw_pcie		*pci;
 110	int			reset_gpio;
 111	bool			gpio_active_high;
 112	bool			link_is_up;
 113	struct clk_bulk_data	clks[IMX6_PCIE_MAX_CLKS];
 
 
 
 
 114	struct regmap		*iomuxc_gpr;
 115	u16			msi_ctrl;
 116	u32			controller_id;
 117	struct reset_control	*pciephy_reset;
 118	struct reset_control	*apps_reset;
 119	struct reset_control	*turnoff_reset;
 120	u32			tx_deemph_gen1;
 121	u32			tx_deemph_gen2_3p5db;
 122	u32			tx_deemph_gen2_6db;
 123	u32			tx_swing_full;
 124	u32			tx_swing_low;
 125	struct regulator	*vpcie;
 126	struct regulator	*vph;
 127	void __iomem		*phy_base;
 128
 129	/* power domain for pcie */
 130	struct device		*pd_pcie;
 131	/* power domain for pcie phy */
 132	struct device		*pd_pcie_phy;
 133	struct phy		*phy;
 134	const struct imx6_pcie_drvdata *drvdata;
 135};
 136
 137/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
 138#define PHY_PLL_LOCK_WAIT_USLEEP_MAX	200
 139#define PHY_PLL_LOCK_WAIT_TIMEOUT	(2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
 140
 141/* PCIe Port Logic registers (memory-mapped) */
 142#define PL_OFFSET 0x700
 143
 144#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
 145#define PCIE_PHY_CTRL_DATA(x)		FIELD_PREP(GENMASK(15, 0), (x))
 146#define PCIE_PHY_CTRL_CAP_ADR		BIT(16)
 147#define PCIE_PHY_CTRL_CAP_DAT		BIT(17)
 148#define PCIE_PHY_CTRL_WR		BIT(18)
 149#define PCIE_PHY_CTRL_RD		BIT(19)
 150
 151#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
 152#define PCIE_PHY_STAT_ACK		BIT(16)
 153
 154/* PHY registers (not memory-mapped) */
 155#define PCIE_PHY_ATEOVRD			0x10
 156#define  PCIE_PHY_ATEOVRD_EN			BIT(2)
 157#define  PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT	0
 158#define  PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK	0x1
 159
 160#define PCIE_PHY_MPLL_OVRD_IN_LO		0x11
 161#define  PCIE_PHY_MPLL_MULTIPLIER_SHIFT		2
 162#define  PCIE_PHY_MPLL_MULTIPLIER_MASK		0x7f
 163#define  PCIE_PHY_MPLL_MULTIPLIER_OVRD		BIT(9)
 164
 165#define PCIE_PHY_RX_ASIC_OUT 0x100D
 166#define PCIE_PHY_RX_ASIC_OUT_VALID	(1 << 0)
 167
 168/* iMX7 PCIe PHY registers */
 169#define PCIE_PHY_CMN_REG4		0x14
 170/* These are probably the bits that *aren't* DCC_FB_EN */
 171#define PCIE_PHY_CMN_REG4_DCC_FB_EN	0x29
 172
 173#define PCIE_PHY_CMN_REG15	        0x54
 174#define PCIE_PHY_CMN_REG15_DLY_4	BIT(2)
 175#define PCIE_PHY_CMN_REG15_PLL_PD	BIT(5)
 176#define PCIE_PHY_CMN_REG15_OVRD_PLL_PD	BIT(7)
 177
 178#define PCIE_PHY_CMN_REG24		0x90
 179#define PCIE_PHY_CMN_REG24_RX_EQ	BIT(6)
 180#define PCIE_PHY_CMN_REG24_RX_EQ_SEL	BIT(3)
 181
 182#define PCIE_PHY_CMN_REG26		0x98
 183#define PCIE_PHY_CMN_REG26_ATT_MODE	0xBC
 184
 185#define PHY_RX_OVRD_IN_LO 0x1005
 186#define PHY_RX_OVRD_IN_LO_RX_DATA_EN		BIT(5)
 187#define PHY_RX_OVRD_IN_LO_RX_PLL_EN		BIT(3)
 188
 189static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
 190{
 191	WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ &&
 192		imx6_pcie->drvdata->variant != IMX8MQ_EP &&
 193		imx6_pcie->drvdata->variant != IMX8MM &&
 194		imx6_pcie->drvdata->variant != IMX8MM_EP &&
 195		imx6_pcie->drvdata->variant != IMX8MP &&
 196		imx6_pcie->drvdata->variant != IMX8MP_EP);
 197	return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
 198}
 199
 200static int imx95_pcie_init_phy(struct imx6_pcie *imx6_pcie)
 201{
 202	regmap_update_bits(imx6_pcie->iomuxc_gpr,
 203			IMX95_PCIE_SS_RW_REG_0,
 204			IMX95_PCIE_PHY_CR_PARA_SEL,
 205			IMX95_PCIE_PHY_CR_PARA_SEL);
 206
 207	regmap_update_bits(imx6_pcie->iomuxc_gpr,
 208			   IMX95_PCIE_PHY_GEN_CTRL,
 209			   IMX95_PCIE_REF_USE_PAD, 0);
 210	regmap_update_bits(imx6_pcie->iomuxc_gpr,
 211			   IMX95_PCIE_SS_RW_REG_0,
 212			   IMX95_PCIE_REF_CLKEN,
 213			   IMX95_PCIE_REF_CLKEN);
 214
 215	return 0;
 216}
 217
 218static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie)
 219{
 220	const struct imx6_pcie_drvdata *drvdata = imx6_pcie->drvdata;
 221	unsigned int mask, val, mode, id;
 222
 223	if (drvdata->mode == DW_PCIE_EP_TYPE)
 224		mode = PCI_EXP_TYPE_ENDPOINT;
 225	else
 226		mode = PCI_EXP_TYPE_ROOT_PORT;
 227
 228	id = imx6_pcie->controller_id;
 229
 230	/* If mode_mask[id] is zero, means each controller have its individual gpr */
 231	if (!drvdata->mode_mask[id])
 232		id = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 233
 234	mask = drvdata->mode_mask[id];
 235	val = mode << (ffs(mask) - 1);
 236
 237	regmap_update_bits(imx6_pcie->iomuxc_gpr, drvdata->mode_off[id], mask, val);
 238}
 239
 240static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, bool exp_val)
 241{
 242	struct dw_pcie *pci = imx6_pcie->pci;
 243	bool val;
 244	u32 max_iterations = 10;
 245	u32 wait_counter = 0;
 246
 247	do {
 248		val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT) &
 249			PCIE_PHY_STAT_ACK;
 250		wait_counter++;
 251
 252		if (val == exp_val)
 253			return 0;
 254
 255		udelay(1);
 256	} while (wait_counter < max_iterations);
 257
 258	return -ETIMEDOUT;
 259}
 260
 261static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
 262{
 263	struct dw_pcie *pci = imx6_pcie->pci;
 264	u32 val;
 265	int ret;
 266
 267	val = PCIE_PHY_CTRL_DATA(addr);
 268	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
 269
 270	val |= PCIE_PHY_CTRL_CAP_ADR;
 271	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
 272
 273	ret = pcie_phy_poll_ack(imx6_pcie, true);
 274	if (ret)
 275		return ret;
 276
 277	val = PCIE_PHY_CTRL_DATA(addr);
 278	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
 279
 280	return pcie_phy_poll_ack(imx6_pcie, false);
 281}
 282
 283/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
 284static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, u16 *data)
 285{
 286	struct dw_pcie *pci = imx6_pcie->pci;
 287	u32 phy_ctl;
 288	int ret;
 289
 290	ret = pcie_phy_wait_ack(imx6_pcie, addr);
 291	if (ret)
 292		return ret;
 293
 294	/* assert Read signal */
 295	phy_ctl = PCIE_PHY_CTRL_RD;
 296	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
 297
 298	ret = pcie_phy_poll_ack(imx6_pcie, true);
 299	if (ret)
 300		return ret;
 301
 302	*data = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
 303
 304	/* deassert Read signal */
 305	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
 306
 307	return pcie_phy_poll_ack(imx6_pcie, false);
 308}
 309
 310static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, u16 data)
 311{
 312	struct dw_pcie *pci = imx6_pcie->pci;
 313	u32 var;
 314	int ret;
 315
 316	/* write addr */
 317	/* cap addr */
 318	ret = pcie_phy_wait_ack(imx6_pcie, addr);
 319	if (ret)
 320		return ret;
 321
 322	var = PCIE_PHY_CTRL_DATA(data);
 323	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 324
 325	/* capture data */
 326	var |= PCIE_PHY_CTRL_CAP_DAT;
 327	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 328
 329	ret = pcie_phy_poll_ack(imx6_pcie, true);
 330	if (ret)
 331		return ret;
 332
 333	/* deassert cap data */
 334	var = PCIE_PHY_CTRL_DATA(data);
 335	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 336
 337	/* wait for ack de-assertion */
 338	ret = pcie_phy_poll_ack(imx6_pcie, false);
 339	if (ret)
 340		return ret;
 341
 342	/* assert wr signal */
 343	var = PCIE_PHY_CTRL_WR;
 344	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 345
 346	/* wait for ack */
 347	ret = pcie_phy_poll_ack(imx6_pcie, true);
 348	if (ret)
 349		return ret;
 350
 351	/* deassert wr signal */
 352	var = PCIE_PHY_CTRL_DATA(data);
 353	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
 354
 355	/* wait for ack de-assertion */
 356	ret = pcie_phy_poll_ack(imx6_pcie, false);
 357	if (ret)
 358		return ret;
 359
 360	dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
 361
 362	return 0;
 363}
 364
 365static int imx8mq_pcie_init_phy(struct imx6_pcie *imx6_pcie)
 366{
 367	/* TODO: Currently this code assumes external oscillator is being used */
 368	regmap_update_bits(imx6_pcie->iomuxc_gpr,
 369			   imx6_pcie_grp_offset(imx6_pcie),
 370			   IMX8MQ_GPR_PCIE_REF_USE_PAD,
 371			   IMX8MQ_GPR_PCIE_REF_USE_PAD);
 372	/*
 373	 * Regarding the datasheet, the PCIE_VPH is suggested to be 1.8V. If the PCIE_VPH is
 374	 * supplied by 3.3V, the VREG_BYPASS should be cleared to zero.
 375	 */
 376	if (imx6_pcie->vph && regulator_get_voltage(imx6_pcie->vph) > 3000000)
 
 
 
 
 
 
 377		regmap_update_bits(imx6_pcie->iomuxc_gpr,
 378				   imx6_pcie_grp_offset(imx6_pcie),
 379				   IMX8MQ_GPR_PCIE_VREG_BYPASS,
 380				   0);
 381
 382	return 0;
 383}
 384
 385static int imx7d_pcie_init_phy(struct imx6_pcie *imx6_pcie)
 386{
 387	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
 388
 389	return 0;
 390}
 391
 392static int imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
 393{
 394	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 
 
 
 
 
 
 
 
 
 395				   IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
 396
 397	/* configure constant input signal to the pcie ctrl and phy */
 398	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 399			   IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
 400
 401	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 402			   IMX6Q_GPR8_TX_DEEMPH_GEN1,
 403			   imx6_pcie->tx_deemph_gen1 << 0);
 404	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 405			   IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
 406			   imx6_pcie->tx_deemph_gen2_3p5db << 6);
 407	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 408			   IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
 409			   imx6_pcie->tx_deemph_gen2_6db << 12);
 410	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 411			   IMX6Q_GPR8_TX_SWING_FULL,
 412			   imx6_pcie->tx_swing_full << 18);
 413	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
 414			   IMX6Q_GPR8_TX_SWING_LOW,
 415			   imx6_pcie->tx_swing_low << 25);
 416	return 0;
 417}
 418
 419static int imx6sx_pcie_init_phy(struct imx6_pcie *imx6_pcie)
 420{
 421	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 422			   IMX6SX_GPR12_PCIE_RX_EQ_MASK, IMX6SX_GPR12_PCIE_RX_EQ_2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 423
 424	return imx6_pcie_init_phy(imx6_pcie);
 425}
 426
 427static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
 428{
 429	u32 val;
 430	struct device *dev = imx6_pcie->pci->dev;
 431
 432	if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
 433				     IOMUXC_GPR22, val,
 434				     val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
 435				     PHY_PLL_LOCK_WAIT_USLEEP_MAX,
 436				     PHY_PLL_LOCK_WAIT_TIMEOUT))
 437		dev_err(dev, "PCIe PLL lock timeout\n");
 438}
 439
 440static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
 441{
 442	unsigned long phy_rate = 0;
 443	int mult, div;
 444	u16 val;
 445	int i;
 446
 447	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
 448		return 0;
 449
 450	for (i = 0; i < imx6_pcie->drvdata->clks_cnt; i++)
 451		if (strncmp(imx6_pcie->clks[i].id, "pcie_phy", 8) == 0)
 452			phy_rate = clk_get_rate(imx6_pcie->clks[i].clk);
 453
 454	switch (phy_rate) {
 455	case 125000000:
 456		/*
 457		 * The default settings of the MPLL are for a 125MHz input
 458		 * clock, so no need to reconfigure anything in that case.
 459		 */
 460		return 0;
 461	case 100000000:
 462		mult = 25;
 463		div = 0;
 464		break;
 465	case 200000000:
 466		mult = 25;
 467		div = 1;
 468		break;
 469	default:
 470		dev_err(imx6_pcie->pci->dev,
 471			"Unsupported PHY reference clock rate %lu\n", phy_rate);
 472		return -EINVAL;
 473	}
 474
 475	pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
 476	val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
 477		 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
 478	val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
 479	val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
 480	pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
 481
 482	pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
 483	val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
 484		 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
 485	val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
 486	val |= PCIE_PHY_ATEOVRD_EN;
 487	pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
 488
 489	return 0;
 490}
 491
 492static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
 493{
 494	u16 tmp;
 495
 496	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
 497		return;
 498
 499	pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
 500	tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
 501		PHY_RX_OVRD_IN_LO_RX_PLL_EN);
 502	pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
 503
 504	usleep_range(2000, 3000);
 505
 506	pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
 507	tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
 508		  PHY_RX_OVRD_IN_LO_RX_PLL_EN);
 509	pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
 510}
 511
 512#ifdef CONFIG_ARM
 513/*  Added for PCI abort handling */
 514static int imx6q_pcie_abort_handler(unsigned long addr,
 515		unsigned int fsr, struct pt_regs *regs)
 516{
 517	unsigned long pc = instruction_pointer(regs);
 518	unsigned long instr = *(unsigned long *)pc;
 519	int reg = (instr >> 12) & 15;
 520
 521	/*
 522	 * If the instruction being executed was a read,
 523	 * make it look like it read all-ones.
 524	 */
 525	if ((instr & 0x0c100000) == 0x04100000) {
 526		unsigned long val;
 527
 528		if (instr & 0x00400000)
 529			val = 255;
 530		else
 531			val = -1;
 532
 533		regs->uregs[reg] = val;
 534		regs->ARM_pc += 4;
 535		return 0;
 536	}
 537
 538	if ((instr & 0x0e100090) == 0x00100090) {
 539		regs->uregs[reg] = -1;
 540		regs->ARM_pc += 4;
 541		return 0;
 542	}
 543
 544	return 1;
 545}
 546#endif
 547
 548static int imx6_pcie_attach_pd(struct device *dev)
 549{
 550	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
 551	struct device_link *link;
 552
 553	/* Do nothing when in a single power domain */
 554	if (dev->pm_domain)
 555		return 0;
 556
 557	imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
 558	if (IS_ERR(imx6_pcie->pd_pcie))
 559		return PTR_ERR(imx6_pcie->pd_pcie);
 560	/* Do nothing when power domain missing */
 561	if (!imx6_pcie->pd_pcie)
 562		return 0;
 563	link = device_link_add(dev, imx6_pcie->pd_pcie,
 564			DL_FLAG_STATELESS |
 565			DL_FLAG_PM_RUNTIME |
 566			DL_FLAG_RPM_ACTIVE);
 567	if (!link) {
 568		dev_err(dev, "Failed to add device_link to pcie pd.\n");
 569		return -EINVAL;
 570	}
 571
 572	imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
 573	if (IS_ERR(imx6_pcie->pd_pcie_phy))
 574		return PTR_ERR(imx6_pcie->pd_pcie_phy);
 575
 576	link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
 577			DL_FLAG_STATELESS |
 578			DL_FLAG_PM_RUNTIME |
 579			DL_FLAG_RPM_ACTIVE);
 580	if (!link) {
 581		dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
 582		return -EINVAL;
 583	}
 584
 585	return 0;
 586}
 587
 588static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
 589{
 
 
 590	unsigned int offset;
 591	int ret = 0;
 592
 593	switch (imx6_pcie->drvdata->variant) {
 594	case IMX6SX:
 
 
 
 
 
 
 595		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 596				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
 597		break;
 598	case IMX6QP:
 599	case IMX6Q:
 600		/* power up core phy and enable ref clock */
 601		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 602				   IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
 603		/*
 604		 * the async reset input need ref clock to sync internally,
 605		 * when the ref clock comes after reset, internal synced
 606		 * reset time is too short, cannot meet the requirement.
 607		 * add one ~10us delay here.
 608		 */
 609		usleep_range(10, 100);
 610		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 611				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
 612		break;
 613	case IMX7D:
 614	case IMX95:
 615	case IMX95_EP:
 616		break;
 617	case IMX8MM:
 618	case IMX8MM_EP:
 619	case IMX8MQ:
 620	case IMX8MQ_EP:
 621	case IMX8MP:
 622	case IMX8MP_EP:
 
 
 
 
 
 
 623		offset = imx6_pcie_grp_offset(imx6_pcie);
 624		/*
 625		 * Set the over ride low and enabled
 626		 * make sure that REF_CLK is turned on.
 627		 */
 628		regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
 629				   IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
 630				   0);
 631		regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
 632				   IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
 633				   IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
 634		break;
 635	}
 636
 637	return ret;
 638}
 639
 640static void imx6_pcie_disable_ref_clk(struct imx6_pcie *imx6_pcie)
 641{
 642	switch (imx6_pcie->drvdata->variant) {
 
 
 
 643	case IMX6QP:
 644	case IMX6Q:
 645		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 646				IMX6Q_GPR1_PCIE_REF_CLK_EN, 0);
 647		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 648				IMX6Q_GPR1_PCIE_TEST_PD,
 649				IMX6Q_GPR1_PCIE_TEST_PD);
 650		break;
 651	case IMX7D:
 652		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 653				   IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
 654				   IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
 655		break;
 
 
 
 
 
 
 
 
 656	default:
 657		break;
 658	}
 659}
 660
 661static int imx6_pcie_clk_enable(struct imx6_pcie *imx6_pcie)
 662{
 663	struct dw_pcie *pci = imx6_pcie->pci;
 664	struct device *dev = pci->dev;
 665	int ret;
 666
 667	ret = clk_bulk_prepare_enable(imx6_pcie->drvdata->clks_cnt, imx6_pcie->clks);
 668	if (ret)
 
 669		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 670
 671	ret = imx6_pcie_enable_ref_clk(imx6_pcie);
 672	if (ret) {
 673		dev_err(dev, "unable to enable pcie ref clock\n");
 674		goto err_ref_clk;
 675	}
 676
 677	/* allow the clocks to stabilize */
 678	usleep_range(200, 500);
 679	return 0;
 680
 681err_ref_clk:
 682	clk_bulk_disable_unprepare(imx6_pcie->drvdata->clks_cnt, imx6_pcie->clks);
 
 
 
 
 683
 684	return ret;
 685}
 686
 687static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
 688{
 689	imx6_pcie_disable_ref_clk(imx6_pcie);
 690	clk_bulk_disable_unprepare(imx6_pcie->drvdata->clks_cnt, imx6_pcie->clks);
 
 
 691}
 692
 693static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
 694{
 695	reset_control_assert(imx6_pcie->pciephy_reset);
 696	reset_control_assert(imx6_pcie->apps_reset);
 697
 698	switch (imx6_pcie->drvdata->variant) {
 
 
 
 
 
 
 
 
 
 
 
 699	case IMX6SX:
 700		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
 701				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
 702				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
 703		/* Force PCIe PHY reset */
 704		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
 705				   IMX6SX_GPR5_PCIE_BTNRST_RESET,
 706				   IMX6SX_GPR5_PCIE_BTNRST_RESET);
 707		break;
 708	case IMX6QP:
 709		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 710				   IMX6Q_GPR1_PCIE_SW_RST,
 711				   IMX6Q_GPR1_PCIE_SW_RST);
 712		break;
 713	case IMX6Q:
 714		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 715				   IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
 716		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 717				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
 718		break;
 719	default:
 720		break;
 721	}
 722
 723	/* Some boards don't have PCIe reset GPIO. */
 724	if (gpio_is_valid(imx6_pcie->reset_gpio))
 725		gpio_set_value_cansleep(imx6_pcie->reset_gpio,
 726					imx6_pcie->gpio_active_high);
 727}
 728
 729static int imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
 730{
 731	struct dw_pcie *pci = imx6_pcie->pci;
 732	struct device *dev = pci->dev;
 733
 734	reset_control_deassert(imx6_pcie->pciephy_reset);
 735
 736	switch (imx6_pcie->drvdata->variant) {
 
 
 
 
 737	case IMX7D:
 
 
 738		/* Workaround for ERR010728, failure of PCI-e PLL VCO to
 739		 * oscillate, especially when cold.  This turns off "Duty-cycle
 740		 * Corrector" and other mysterious undocumented things.
 741		 */
 742		if (likely(imx6_pcie->phy_base)) {
 743			/* De-assert DCC_FB_EN */
 744			writel(PCIE_PHY_CMN_REG4_DCC_FB_EN,
 745			       imx6_pcie->phy_base + PCIE_PHY_CMN_REG4);
 746			/* Assert RX_EQS and RX_EQS_SEL */
 747			writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL
 748				| PCIE_PHY_CMN_REG24_RX_EQ,
 749			       imx6_pcie->phy_base + PCIE_PHY_CMN_REG24);
 750			/* Assert ATT_MODE */
 751			writel(PCIE_PHY_CMN_REG26_ATT_MODE,
 752			       imx6_pcie->phy_base + PCIE_PHY_CMN_REG26);
 753		} else {
 754			dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n");
 755		}
 756
 757		imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
 758		break;
 759	case IMX6SX:
 760		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
 761				   IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
 762		break;
 763	case IMX6QP:
 764		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
 765				   IMX6Q_GPR1_PCIE_SW_RST, 0);
 766
 767		usleep_range(200, 500);
 768		break;
 769	default:
 
 
 
 
 770		break;
 771	}
 772
 773	/* Some boards don't have PCIe reset GPIO. */
 774	if (gpio_is_valid(imx6_pcie->reset_gpio)) {
 775		msleep(100);
 776		gpio_set_value_cansleep(imx6_pcie->reset_gpio,
 777					!imx6_pcie->gpio_active_high);
 778		/* Wait for 100ms after PERST# deassertion (PCIe r5.0, 6.6.1) */
 779		msleep(100);
 780	}
 781
 782	return 0;
 783}
 784
 785static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
 786{
 787	struct dw_pcie *pci = imx6_pcie->pci;
 788	struct device *dev = pci->dev;
 789	u32 tmp;
 790	unsigned int retries;
 791
 792	for (retries = 0; retries < 200; retries++) {
 793		tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
 794		/* Test if the speed change finished. */
 795		if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
 796			return 0;
 797		usleep_range(100, 1000);
 798	}
 799
 800	dev_err(dev, "Speed change timeout\n");
 801	return -ETIMEDOUT;
 802}
 803
 804static void imx6_pcie_ltssm_enable(struct device *dev)
 805{
 806	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
 807	const struct imx6_pcie_drvdata *drvdata = imx6_pcie->drvdata;
 808
 809	if (drvdata->ltssm_mask)
 810		regmap_update_bits(imx6_pcie->iomuxc_gpr, drvdata->ltssm_off, drvdata->ltssm_mask,
 811				   drvdata->ltssm_mask);
 812
 813	reset_control_deassert(imx6_pcie->apps_reset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 814}
 815
 816static void imx6_pcie_ltssm_disable(struct device *dev)
 817{
 818	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
 819	const struct imx6_pcie_drvdata *drvdata = imx6_pcie->drvdata;
 820
 821	if (drvdata->ltssm_mask)
 822		regmap_update_bits(imx6_pcie->iomuxc_gpr, drvdata->ltssm_off,
 823				   drvdata->ltssm_mask, 0);
 824
 825	reset_control_assert(imx6_pcie->apps_reset);
 
 
 
 
 
 
 
 
 
 
 
 
 826}
 827
 828static int imx6_pcie_start_link(struct dw_pcie *pci)
 829{
 830	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
 831	struct device *dev = pci->dev;
 832	u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
 833	u32 tmp;
 834	int ret;
 835
 836	/*
 837	 * Force Gen1 operation when starting the link.  In case the link is
 838	 * started in Gen2 mode, there is a possibility the devices on the
 839	 * bus will not be detected at all.  This happens with PCIe switches.
 840	 */
 841	dw_pcie_dbi_ro_wr_en(pci);
 842	tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
 843	tmp &= ~PCI_EXP_LNKCAP_SLS;
 844	tmp |= PCI_EXP_LNKCAP_SLS_2_5GB;
 845	dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
 846	dw_pcie_dbi_ro_wr_dis(pci);
 847
 848	/* Start LTSSM. */
 849	imx6_pcie_ltssm_enable(dev);
 850
 851	ret = dw_pcie_wait_for_link(pci);
 852	if (ret)
 853		goto err_reset_phy;
 854
 855	if (pci->link_gen > 1) {
 856		/* Allow faster modes after the link is up */
 857		dw_pcie_dbi_ro_wr_en(pci);
 858		tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
 859		tmp &= ~PCI_EXP_LNKCAP_SLS;
 860		tmp |= pci->link_gen;
 861		dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
 862
 863		/*
 864		 * Start Directed Speed Change so the best possible
 865		 * speed both link partners support can be negotiated.
 866		 */
 867		tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
 868		tmp |= PORT_LOGIC_SPEED_CHANGE;
 869		dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
 870		dw_pcie_dbi_ro_wr_dis(pci);
 871
 872		if (imx6_pcie->drvdata->flags &
 873		    IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) {
 874			/*
 875			 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
 876			 * from i.MX6 family when no link speed transition
 877			 * occurs and we go Gen1 -> yep, Gen1. The difference
 878			 * is that, in such case, it will not be cleared by HW
 879			 * which will cause the following code to report false
 880			 * failure.
 881			 */
 882
 883			ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
 884			if (ret) {
 885				dev_err(dev, "Failed to bring link up!\n");
 886				goto err_reset_phy;
 887			}
 888		}
 889
 890		/* Make sure link training is finished as well! */
 891		ret = dw_pcie_wait_for_link(pci);
 892		if (ret)
 893			goto err_reset_phy;
 894	} else {
 895		dev_info(dev, "Link: Only Gen1 is enabled\n");
 896	}
 897
 898	imx6_pcie->link_is_up = true;
 899	tmp = dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKSTA);
 900	dev_info(dev, "Link up, Gen%i\n", tmp & PCI_EXP_LNKSTA_CLS);
 901	return 0;
 902
 903err_reset_phy:
 904	imx6_pcie->link_is_up = false;
 905	dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
 906		dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0),
 907		dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG1));
 908	imx6_pcie_reset_phy(imx6_pcie);
 909	return 0;
 910}
 911
 912static void imx6_pcie_stop_link(struct dw_pcie *pci)
 913{
 914	struct device *dev = pci->dev;
 915
 916	/* Turn off PCIe LTSSM */
 917	imx6_pcie_ltssm_disable(dev);
 918}
 919
 920static int imx6_pcie_host_init(struct dw_pcie_rp *pp)
 921{
 922	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 923	struct device *dev = pci->dev;
 924	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
 925	int ret;
 926
 927	if (imx6_pcie->vpcie) {
 928		ret = regulator_enable(imx6_pcie->vpcie);
 929		if (ret) {
 930			dev_err(dev, "failed to enable vpcie regulator: %d\n",
 931				ret);
 932			return ret;
 933		}
 934	}
 935
 936	imx6_pcie_assert_core_reset(imx6_pcie);
 937
 938	if (imx6_pcie->drvdata->init_phy)
 939		imx6_pcie->drvdata->init_phy(imx6_pcie);
 940
 941	imx6_pcie_configure_type(imx6_pcie);
 942
 943	ret = imx6_pcie_clk_enable(imx6_pcie);
 944	if (ret) {
 945		dev_err(dev, "unable to enable pcie clocks: %d\n", ret);
 946		goto err_reg_disable;
 947	}
 948
 949	if (imx6_pcie->phy) {
 950		ret = phy_init(imx6_pcie->phy);
 951		if (ret) {
 952			dev_err(dev, "pcie PHY power up failed\n");
 953			goto err_clk_disable;
 954		}
 955	}
 956
 957	if (imx6_pcie->phy) {
 958		ret = phy_power_on(imx6_pcie->phy);
 959		if (ret) {
 960			dev_err(dev, "waiting for PHY ready timeout!\n");
 961			goto err_phy_off;
 962		}
 963	}
 964
 965	ret = imx6_pcie_deassert_core_reset(imx6_pcie);
 966	if (ret < 0) {
 967		dev_err(dev, "pcie deassert core reset failed: %d\n", ret);
 968		goto err_phy_off;
 969	}
 970
 971	imx6_setup_phy_mpll(imx6_pcie);
 972
 973	return 0;
 974
 975err_phy_off:
 976	if (imx6_pcie->phy)
 977		phy_exit(imx6_pcie->phy);
 978err_clk_disable:
 979	imx6_pcie_clk_disable(imx6_pcie);
 980err_reg_disable:
 981	if (imx6_pcie->vpcie)
 982		regulator_disable(imx6_pcie->vpcie);
 983	return ret;
 984}
 985
 986static void imx6_pcie_host_exit(struct dw_pcie_rp *pp)
 987{
 988	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 989	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
 990
 991	if (imx6_pcie->phy) {
 992		if (phy_power_off(imx6_pcie->phy))
 993			dev_err(pci->dev, "unable to power off PHY\n");
 994		phy_exit(imx6_pcie->phy);
 995	}
 996	imx6_pcie_clk_disable(imx6_pcie);
 997
 998	if (imx6_pcie->vpcie)
 999		regulator_disable(imx6_pcie->vpcie);
1000}
1001
1002static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
1003	.init = imx6_pcie_host_init,
1004	.deinit = imx6_pcie_host_exit,
1005};
1006
1007static const struct dw_pcie_ops dw_pcie_ops = {
1008	.start_link = imx6_pcie_start_link,
1009	.stop_link = imx6_pcie_stop_link,
1010};
1011
1012static void imx6_pcie_ep_init(struct dw_pcie_ep *ep)
1013{
1014	enum pci_barno bar;
1015	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1016
1017	for (bar = BAR_0; bar <= BAR_5; bar++)
1018		dw_pcie_ep_reset_bar(pci, bar);
1019}
1020
1021static int imx6_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
1022				  unsigned int type, u16 interrupt_num)
1023{
1024	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1025
1026	switch (type) {
1027	case PCI_IRQ_INTX:
1028		return dw_pcie_ep_raise_intx_irq(ep, func_no);
1029	case PCI_IRQ_MSI:
1030		return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
1031	case PCI_IRQ_MSIX:
1032		return dw_pcie_ep_raise_msix_irq(ep, func_no, interrupt_num);
1033	default:
1034		dev_err(pci->dev, "UNKNOWN IRQ type\n");
1035		return -EINVAL;
1036	}
1037
1038	return 0;
1039}
1040
1041static const struct pci_epc_features imx8m_pcie_epc_features = {
1042	.linkup_notifier = false,
1043	.msi_capable = true,
1044	.msix_capable = false,
1045	.bar[BAR_1] = { .type = BAR_RESERVED, },
1046	.bar[BAR_3] = { .type = BAR_RESERVED, },
1047	.align = SZ_64K,
1048};
1049
1050/*
1051 * BAR#	| Default BAR enable	| Default BAR Type	| Default BAR Size	| BAR Sizing Scheme
1052 * ================================================================================================
1053 * BAR0	| Enable		| 64-bit		| 1 MB			| Programmable Size
1054 * BAR1	| Disable		| 32-bit		| 64 KB			| Fixed Size
1055 *        BAR1 should be disabled if BAR0 is 64bit.
1056 * BAR2	| Enable		| 32-bit		| 1 MB			| Programmable Size
1057 * BAR3	| Enable		| 32-bit		| 64 KB			| Programmable Size
1058 * BAR4	| Enable		| 32-bit		| 1M			| Programmable Size
1059 * BAR5	| Enable		| 32-bit		| 64 KB			| Programmable Size
1060 */
1061static const struct pci_epc_features imx95_pcie_epc_features = {
1062	.msi_capable = true,
1063	.bar[BAR_1] = { .type = BAR_FIXED, .fixed_size = SZ_64K, },
1064	.align = SZ_4K,
1065};
1066
1067static const struct pci_epc_features*
1068imx6_pcie_ep_get_features(struct dw_pcie_ep *ep)
1069{
1070	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
1071	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1072
1073	return imx6_pcie->drvdata->epc_features;
1074}
1075
1076static const struct dw_pcie_ep_ops pcie_ep_ops = {
1077	.init = imx6_pcie_ep_init,
1078	.raise_irq = imx6_pcie_ep_raise_irq,
1079	.get_features = imx6_pcie_ep_get_features,
1080};
1081
1082static int imx6_add_pcie_ep(struct imx6_pcie *imx6_pcie,
1083			   struct platform_device *pdev)
1084{
1085	int ret;
1086	unsigned int pcie_dbi2_offset;
1087	struct dw_pcie_ep *ep;
 
1088	struct dw_pcie *pci = imx6_pcie->pci;
1089	struct dw_pcie_rp *pp = &pci->pp;
1090	struct device *dev = pci->dev;
1091
1092	imx6_pcie_host_init(pp);
1093	ep = &pci->ep;
1094	ep->ops = &pcie_ep_ops;
1095
1096	switch (imx6_pcie->drvdata->variant) {
1097	case IMX8MQ_EP:
1098	case IMX8MM_EP:
1099	case IMX8MP_EP:
1100		pcie_dbi2_offset = SZ_1M;
1101		break;
1102	default:
1103		pcie_dbi2_offset = SZ_4K;
1104		break;
1105	}
1106
1107	pci->dbi_base2 = pci->dbi_base + pcie_dbi2_offset;
 
 
 
1108
1109	/*
1110	 * FIXME: Ideally, dbi2 base address should come from DT. But since only IMX95 is defining
1111	 * "dbi2" in DT, "dbi_base2" is set to NULL here for that platform alone so that the DWC
1112	 * core code can fetch that from DT. But once all platform DTs were fixed, this and the
1113	 * above "dbi_base2" setting should be removed.
1114	 */
1115	if (device_property_match_string(dev, "reg-names", "dbi2") >= 0)
1116		pci->dbi_base2 = NULL;
1117
1118	if (imx6_check_flag(imx6_pcie, IMX6_PCIE_FLAG_SUPPORT_64BIT))
1119		dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
1120
1121	ret = dw_pcie_ep_init(ep);
1122	if (ret) {
1123		dev_err(dev, "failed to initialize endpoint\n");
1124		return ret;
1125	}
1126	/* Start LTSSM. */
1127	imx6_pcie_ltssm_enable(dev);
1128
1129	return 0;
1130}
1131
1132static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
1133{
1134	struct device *dev = imx6_pcie->pci->dev;
1135
1136	/* Some variants have a turnoff reset in DT */
1137	if (imx6_pcie->turnoff_reset) {
1138		reset_control_assert(imx6_pcie->turnoff_reset);
1139		reset_control_deassert(imx6_pcie->turnoff_reset);
1140		goto pm_turnoff_sleep;
1141	}
1142
1143	/* Others poke directly at IOMUXC registers */
1144	switch (imx6_pcie->drvdata->variant) {
1145	case IMX6SX:
1146	case IMX6QP:
1147		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
1148				IMX6SX_GPR12_PCIE_PM_TURN_OFF,
1149				IMX6SX_GPR12_PCIE_PM_TURN_OFF);
1150		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
1151				IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
1152		break;
1153	default:
1154		dev_err(dev, "PME_Turn_Off not implemented\n");
1155		return;
1156	}
1157
1158	/*
1159	 * Components with an upstream port must respond to
1160	 * PME_Turn_Off with PME_TO_Ack but we can't check.
1161	 *
1162	 * The standard recommends a 1-10ms timeout after which to
1163	 * proceed anyway as if acks were received.
1164	 */
1165pm_turnoff_sleep:
1166	usleep_range(1000, 10000);
1167}
1168
1169static void imx6_pcie_msi_save_restore(struct imx6_pcie *imx6_pcie, bool save)
1170{
1171	u8 offset;
1172	u16 val;
1173	struct dw_pcie *pci = imx6_pcie->pci;
1174
1175	if (pci_msi_enabled()) {
1176		offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
1177		if (save) {
1178			val = dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
1179			imx6_pcie->msi_ctrl = val;
1180		} else {
1181			dw_pcie_dbi_ro_wr_en(pci);
1182			val = imx6_pcie->msi_ctrl;
1183			dw_pcie_writew_dbi(pci, offset + PCI_MSI_FLAGS, val);
1184			dw_pcie_dbi_ro_wr_dis(pci);
1185		}
1186	}
1187}
1188
1189static int imx6_pcie_suspend_noirq(struct device *dev)
1190{
1191	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
1192	struct dw_pcie_rp *pp = &imx6_pcie->pci->pp;
1193
1194	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
1195		return 0;
1196
1197	imx6_pcie_msi_save_restore(imx6_pcie, true);
1198	imx6_pcie_pm_turnoff(imx6_pcie);
1199	imx6_pcie_stop_link(imx6_pcie->pci);
1200	imx6_pcie_host_exit(pp);
1201
1202	return 0;
1203}
1204
1205static int imx6_pcie_resume_noirq(struct device *dev)
1206{
1207	int ret;
1208	struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
1209	struct dw_pcie_rp *pp = &imx6_pcie->pci->pp;
1210
1211	if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
1212		return 0;
1213
1214	ret = imx6_pcie_host_init(pp);
1215	if (ret)
1216		return ret;
1217	imx6_pcie_msi_save_restore(imx6_pcie, false);
1218	dw_pcie_setup_rc(pp);
1219
1220	if (imx6_pcie->link_is_up)
1221		imx6_pcie_start_link(imx6_pcie->pci);
1222
1223	return 0;
1224}
1225
1226static const struct dev_pm_ops imx6_pcie_pm_ops = {
1227	NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
1228				  imx6_pcie_resume_noirq)
1229};
1230
1231static int imx6_pcie_probe(struct platform_device *pdev)
1232{
1233	struct device *dev = &pdev->dev;
1234	struct dw_pcie *pci;
1235	struct imx6_pcie *imx6_pcie;
1236	struct device_node *np;
1237	struct resource *dbi_base;
1238	struct device_node *node = dev->of_node;
1239	int ret;
1240	u16 val;
1241	int i;
1242
1243	imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
1244	if (!imx6_pcie)
1245		return -ENOMEM;
1246
1247	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1248	if (!pci)
1249		return -ENOMEM;
1250
1251	pci->dev = dev;
1252	pci->ops = &dw_pcie_ops;
1253	pci->pp.ops = &imx6_pcie_host_ops;
1254
1255	imx6_pcie->pci = pci;
1256	imx6_pcie->drvdata = of_device_get_match_data(dev);
1257
1258	/* Find the PHY if one is defined, only imx7d uses it */
1259	np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0);
1260	if (np) {
1261		struct resource res;
1262
1263		ret = of_address_to_resource(np, 0, &res);
1264		if (ret) {
1265			dev_err(dev, "Unable to map PCIe PHY\n");
1266			return ret;
1267		}
1268		imx6_pcie->phy_base = devm_ioremap_resource(dev, &res);
1269		if (IS_ERR(imx6_pcie->phy_base))
1270			return PTR_ERR(imx6_pcie->phy_base);
1271	}
1272
1273	pci->dbi_base = devm_platform_get_and_ioremap_resource(pdev, 0, &dbi_base);
1274	if (IS_ERR(pci->dbi_base))
1275		return PTR_ERR(pci->dbi_base);
1276
1277	/* Fetch GPIOs */
1278	imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
1279	imx6_pcie->gpio_active_high = of_property_read_bool(node,
1280						"reset-gpio-active-high");
1281	if (gpio_is_valid(imx6_pcie->reset_gpio)) {
1282		ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
1283				imx6_pcie->gpio_active_high ?
1284					GPIOF_OUT_INIT_HIGH :
1285					GPIOF_OUT_INIT_LOW,
1286				"PCIe reset");
1287		if (ret) {
1288			dev_err(dev, "unable to get reset gpio\n");
1289			return ret;
1290		}
1291	} else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
1292		return imx6_pcie->reset_gpio;
1293	}
1294
1295	if (imx6_pcie->drvdata->clks_cnt >= IMX6_PCIE_MAX_CLKS)
1296		return dev_err_probe(dev, -ENOMEM, "clks_cnt is too big\n");
 
 
 
 
 
 
 
 
1297
1298	for (i = 0; i < imx6_pcie->drvdata->clks_cnt; i++)
1299		imx6_pcie->clks[i].id = imx6_pcie->drvdata->clk_names[i];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1300
1301	/* Fetch clocks */
1302	ret = devm_clk_bulk_get(dev, imx6_pcie->drvdata->clks_cnt, imx6_pcie->clks);
1303	if (ret)
1304		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1305
1306	if (imx6_check_flag(imx6_pcie, IMX6_PCIE_FLAG_HAS_PHYDRV)) {
1307		imx6_pcie->phy = devm_phy_get(dev, "pcie-phy");
1308		if (IS_ERR(imx6_pcie->phy))
1309			return dev_err_probe(dev, PTR_ERR(imx6_pcie->phy),
1310					     "failed to get pcie phy\n");
1311	}
1312
1313	if (imx6_check_flag(imx6_pcie, IMX6_PCIE_FLAG_HAS_APP_RESET)) {
1314		imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev, "apps");
1315		if (IS_ERR(imx6_pcie->apps_reset))
1316			return dev_err_probe(dev, PTR_ERR(imx6_pcie->apps_reset),
1317					     "failed to get pcie apps reset control\n");
1318	}
1319
1320	if (imx6_check_flag(imx6_pcie, IMX6_PCIE_FLAG_HAS_PHY_RESET)) {
1321		imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev, "pciephy");
1322		if (IS_ERR(imx6_pcie->pciephy_reset))
1323			return dev_err_probe(dev, PTR_ERR(imx6_pcie->pciephy_reset),
1324					     "Failed to get PCIEPHY reset control\n");
1325	}
1326
1327	switch (imx6_pcie->drvdata->variant) {
1328	case IMX8MQ:
1329	case IMX8MQ_EP:
1330	case IMX7D:
1331		if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR)
1332			imx6_pcie->controller_id = 1;
1333		break;
1334	default:
1335		break;
1336	}
 
 
 
 
 
 
 
 
1337
1338	/* Grab turnoff reset */
1339	imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
1340	if (IS_ERR(imx6_pcie->turnoff_reset)) {
1341		dev_err(dev, "Failed to get TURNOFF reset control\n");
1342		return PTR_ERR(imx6_pcie->turnoff_reset);
1343	}
1344
1345	if (imx6_pcie->drvdata->gpr) {
1346	/* Grab GPR config register range */
1347		imx6_pcie->iomuxc_gpr =
1348			 syscon_regmap_lookup_by_compatible(imx6_pcie->drvdata->gpr);
1349		if (IS_ERR(imx6_pcie->iomuxc_gpr))
1350			return dev_err_probe(dev, PTR_ERR(imx6_pcie->iomuxc_gpr),
1351					     "unable to find iomuxc registers\n");
1352	}
1353
1354	if (imx6_check_flag(imx6_pcie, IMX6_PCIE_FLAG_HAS_SERDES)) {
1355		void __iomem *off = devm_platform_ioremap_resource_byname(pdev, "app");
1356
1357		if (IS_ERR(off))
1358			return dev_err_probe(dev, PTR_ERR(off),
1359					     "unable to find serdes registers\n");
1360
1361		static const struct regmap_config regmap_config = {
1362			.reg_bits = 32,
1363			.val_bits = 32,
1364			.reg_stride = 4,
1365		};
1366
1367		imx6_pcie->iomuxc_gpr = devm_regmap_init_mmio(dev, off, &regmap_config);
1368		if (IS_ERR(imx6_pcie->iomuxc_gpr))
1369			return dev_err_probe(dev, PTR_ERR(imx6_pcie->iomuxc_gpr),
1370					     "unable to find iomuxc registers\n");
1371	}
1372
1373	/* Grab PCIe PHY Tx Settings */
1374	if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
1375				 &imx6_pcie->tx_deemph_gen1))
1376		imx6_pcie->tx_deemph_gen1 = 0;
1377
1378	if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
1379				 &imx6_pcie->tx_deemph_gen2_3p5db))
1380		imx6_pcie->tx_deemph_gen2_3p5db = 0;
1381
1382	if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
1383				 &imx6_pcie->tx_deemph_gen2_6db))
1384		imx6_pcie->tx_deemph_gen2_6db = 20;
1385
1386	if (of_property_read_u32(node, "fsl,tx-swing-full",
1387				 &imx6_pcie->tx_swing_full))
1388		imx6_pcie->tx_swing_full = 127;
1389
1390	if (of_property_read_u32(node, "fsl,tx-swing-low",
1391				 &imx6_pcie->tx_swing_low))
1392		imx6_pcie->tx_swing_low = 127;
1393
1394	/* Limit link speed */
1395	pci->link_gen = 1;
1396	of_property_read_u32(node, "fsl,max-link-speed", &pci->link_gen);
1397
1398	imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
1399	if (IS_ERR(imx6_pcie->vpcie)) {
1400		if (PTR_ERR(imx6_pcie->vpcie) != -ENODEV)
1401			return PTR_ERR(imx6_pcie->vpcie);
1402		imx6_pcie->vpcie = NULL;
1403	}
1404
1405	imx6_pcie->vph = devm_regulator_get_optional(&pdev->dev, "vph");
1406	if (IS_ERR(imx6_pcie->vph)) {
1407		if (PTR_ERR(imx6_pcie->vph) != -ENODEV)
1408			return PTR_ERR(imx6_pcie->vph);
1409		imx6_pcie->vph = NULL;
1410	}
1411
1412	platform_set_drvdata(pdev, imx6_pcie);
1413
1414	ret = imx6_pcie_attach_pd(dev);
1415	if (ret)
1416		return ret;
1417
1418	if (imx6_pcie->drvdata->mode == DW_PCIE_EP_TYPE) {
1419		ret = imx6_add_pcie_ep(imx6_pcie, pdev);
1420		if (ret < 0)
1421			return ret;
1422	} else {
1423		ret = dw_pcie_host_init(&pci->pp);
1424		if (ret < 0)
1425			return ret;
1426
1427		if (pci_msi_enabled()) {
1428			u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
1429
1430			val = dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
1431			val |= PCI_MSI_FLAGS_ENABLE;
1432			dw_pcie_writew_dbi(pci, offset + PCI_MSI_FLAGS, val);
1433		}
1434	}
1435
1436	return 0;
1437}
1438
1439static void imx6_pcie_shutdown(struct platform_device *pdev)
1440{
1441	struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1442
1443	/* bring down link, so bootloader gets clean state in case of reboot */
1444	imx6_pcie_assert_core_reset(imx6_pcie);
1445}
1446
1447static const char * const imx6q_clks[] = {"pcie_bus", "pcie", "pcie_phy"};
1448static const char * const imx8mm_clks[] = {"pcie_bus", "pcie", "pcie_aux"};
1449static const char * const imx8mq_clks[] = {"pcie_bus", "pcie", "pcie_phy", "pcie_aux"};
1450static const char * const imx6sx_clks[] = {"pcie_bus", "pcie", "pcie_phy", "pcie_inbound_axi"};
1451
1452static const struct imx6_pcie_drvdata drvdata[] = {
1453	[IMX6Q] = {
1454		.variant = IMX6Q,
1455		.flags = IMX6_PCIE_FLAG_IMX6_PHY |
1456			 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
1457		.dbi_length = 0x200,
1458		.gpr = "fsl,imx6q-iomuxc-gpr",
1459		.clk_names = imx6q_clks,
1460		.clks_cnt = ARRAY_SIZE(imx6q_clks),
1461		.ltssm_off = IOMUXC_GPR12,
1462		.ltssm_mask = IMX6Q_GPR12_PCIE_CTL_2,
1463		.mode_off[0] = IOMUXC_GPR12,
1464		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1465		.init_phy = imx6_pcie_init_phy,
1466	},
1467	[IMX6SX] = {
1468		.variant = IMX6SX,
1469		.flags = IMX6_PCIE_FLAG_IMX6_PHY |
1470			 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1471			 IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1472		.gpr = "fsl,imx6q-iomuxc-gpr",
1473		.clk_names = imx6sx_clks,
1474		.clks_cnt = ARRAY_SIZE(imx6sx_clks),
1475		.ltssm_off = IOMUXC_GPR12,
1476		.ltssm_mask = IMX6Q_GPR12_PCIE_CTL_2,
1477		.mode_off[0] = IOMUXC_GPR12,
1478		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1479		.init_phy = imx6sx_pcie_init_phy,
1480	},
1481	[IMX6QP] = {
1482		.variant = IMX6QP,
1483		.flags = IMX6_PCIE_FLAG_IMX6_PHY |
1484			 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1485			 IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1486		.dbi_length = 0x200,
1487		.gpr = "fsl,imx6q-iomuxc-gpr",
1488		.clk_names = imx6q_clks,
1489		.clks_cnt = ARRAY_SIZE(imx6q_clks),
1490		.ltssm_off = IOMUXC_GPR12,
1491		.ltssm_mask = IMX6Q_GPR12_PCIE_CTL_2,
1492		.mode_off[0] = IOMUXC_GPR12,
1493		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1494		.init_phy = imx6_pcie_init_phy,
1495	},
1496	[IMX7D] = {
1497		.variant = IMX7D,
1498		.flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND |
1499			 IMX6_PCIE_FLAG_HAS_APP_RESET |
1500			 IMX6_PCIE_FLAG_HAS_PHY_RESET,
1501		.gpr = "fsl,imx7d-iomuxc-gpr",
1502		.clk_names = imx6q_clks,
1503		.clks_cnt = ARRAY_SIZE(imx6q_clks),
1504		.mode_off[0] = IOMUXC_GPR12,
1505		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1506		.init_phy = imx7d_pcie_init_phy,
1507	},
1508	[IMX8MQ] = {
1509		.variant = IMX8MQ,
1510		.flags = IMX6_PCIE_FLAG_HAS_APP_RESET |
1511			 IMX6_PCIE_FLAG_HAS_PHY_RESET,
1512		.gpr = "fsl,imx8mq-iomuxc-gpr",
1513		.clk_names = imx8mq_clks,
1514		.clks_cnt = ARRAY_SIZE(imx8mq_clks),
1515		.mode_off[0] = IOMUXC_GPR12,
1516		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1517		.mode_off[1] = IOMUXC_GPR12,
1518		.mode_mask[1] = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
1519		.init_phy = imx8mq_pcie_init_phy,
1520	},
1521	[IMX8MM] = {
1522		.variant = IMX8MM,
1523		.flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND |
1524			 IMX6_PCIE_FLAG_HAS_PHYDRV |
1525			 IMX6_PCIE_FLAG_HAS_APP_RESET,
1526		.gpr = "fsl,imx8mm-iomuxc-gpr",
1527		.clk_names = imx8mm_clks,
1528		.clks_cnt = ARRAY_SIZE(imx8mm_clks),
1529		.mode_off[0] = IOMUXC_GPR12,
1530		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1531	},
1532	[IMX8MP] = {
1533		.variant = IMX8MP,
1534		.flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND |
1535			 IMX6_PCIE_FLAG_HAS_PHYDRV |
1536			 IMX6_PCIE_FLAG_HAS_APP_RESET,
1537		.gpr = "fsl,imx8mp-iomuxc-gpr",
1538		.clk_names = imx8mm_clks,
1539		.clks_cnt = ARRAY_SIZE(imx8mm_clks),
1540		.mode_off[0] = IOMUXC_GPR12,
1541		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1542	},
1543	[IMX95] = {
1544		.variant = IMX95,
1545		.flags = IMX6_PCIE_FLAG_HAS_SERDES,
1546		.clk_names = imx8mq_clks,
1547		.clks_cnt = ARRAY_SIZE(imx8mq_clks),
1548		.ltssm_off = IMX95_PE0_GEN_CTRL_3,
1549		.ltssm_mask = IMX95_PCIE_LTSSM_EN,
1550		.mode_off[0]  = IMX95_PE0_GEN_CTRL_1,
1551		.mode_mask[0] = IMX95_PCIE_DEVICE_TYPE,
1552		.init_phy = imx95_pcie_init_phy,
1553	},
1554	[IMX8MQ_EP] = {
1555		.variant = IMX8MQ_EP,
1556		.flags = IMX6_PCIE_FLAG_HAS_APP_RESET |
1557			 IMX6_PCIE_FLAG_HAS_PHY_RESET,
1558		.mode = DW_PCIE_EP_TYPE,
1559		.gpr = "fsl,imx8mq-iomuxc-gpr",
1560		.clk_names = imx8mq_clks,
1561		.clks_cnt = ARRAY_SIZE(imx8mq_clks),
1562		.mode_off[0] = IOMUXC_GPR12,
1563		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1564		.mode_off[1] = IOMUXC_GPR12,
1565		.mode_mask[1] = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
1566		.epc_features = &imx8m_pcie_epc_features,
1567		.init_phy = imx8mq_pcie_init_phy,
1568	},
1569	[IMX8MM_EP] = {
1570		.variant = IMX8MM_EP,
1571		.flags = IMX6_PCIE_FLAG_HAS_PHYDRV,
1572		.mode = DW_PCIE_EP_TYPE,
1573		.gpr = "fsl,imx8mm-iomuxc-gpr",
1574		.clk_names = imx8mm_clks,
1575		.clks_cnt = ARRAY_SIZE(imx8mm_clks),
1576		.mode_off[0] = IOMUXC_GPR12,
1577		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1578		.epc_features = &imx8m_pcie_epc_features,
1579	},
1580	[IMX8MP_EP] = {
1581		.variant = IMX8MP_EP,
1582		.flags = IMX6_PCIE_FLAG_HAS_PHYDRV,
1583		.mode = DW_PCIE_EP_TYPE,
1584		.gpr = "fsl,imx8mp-iomuxc-gpr",
1585		.clk_names = imx8mm_clks,
1586		.clks_cnt = ARRAY_SIZE(imx8mm_clks),
1587		.mode_off[0] = IOMUXC_GPR12,
1588		.mode_mask[0] = IMX6Q_GPR12_DEVICE_TYPE,
1589		.epc_features = &imx8m_pcie_epc_features,
1590	},
1591	[IMX95_EP] = {
1592		.variant = IMX95_EP,
1593		.flags = IMX6_PCIE_FLAG_HAS_SERDES |
1594			 IMX6_PCIE_FLAG_SUPPORT_64BIT,
1595		.clk_names = imx8mq_clks,
1596		.clks_cnt = ARRAY_SIZE(imx8mq_clks),
1597		.ltssm_off = IMX95_PE0_GEN_CTRL_3,
1598		.ltssm_mask = IMX95_PCIE_LTSSM_EN,
1599		.mode_off[0]  = IMX95_PE0_GEN_CTRL_1,
1600		.mode_mask[0] = IMX95_PCIE_DEVICE_TYPE,
1601		.init_phy = imx95_pcie_init_phy,
1602		.epc_features = &imx95_pcie_epc_features,
1603		.mode = DW_PCIE_EP_TYPE,
1604	},
1605};
1606
1607static const struct of_device_id imx6_pcie_of_match[] = {
1608	{ .compatible = "fsl,imx6q-pcie",  .data = &drvdata[IMX6Q],  },
1609	{ .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], },
1610	{ .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], },
1611	{ .compatible = "fsl,imx7d-pcie",  .data = &drvdata[IMX7D],  },
1612	{ .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], },
1613	{ .compatible = "fsl,imx8mm-pcie", .data = &drvdata[IMX8MM], },
1614	{ .compatible = "fsl,imx8mp-pcie", .data = &drvdata[IMX8MP], },
1615	{ .compatible = "fsl,imx95-pcie", .data = &drvdata[IMX95], },
1616	{ .compatible = "fsl,imx8mq-pcie-ep", .data = &drvdata[IMX8MQ_EP], },
1617	{ .compatible = "fsl,imx8mm-pcie-ep", .data = &drvdata[IMX8MM_EP], },
1618	{ .compatible = "fsl,imx8mp-pcie-ep", .data = &drvdata[IMX8MP_EP], },
1619	{ .compatible = "fsl,imx95-pcie-ep", .data = &drvdata[IMX95_EP], },
1620	{},
1621};
1622
1623static struct platform_driver imx6_pcie_driver = {
1624	.driver = {
1625		.name	= "imx6q-pcie",
1626		.of_match_table = imx6_pcie_of_match,
1627		.suppress_bind_attrs = true,
1628		.pm = &imx6_pcie_pm_ops,
1629		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1630	},
1631	.probe    = imx6_pcie_probe,
1632	.shutdown = imx6_pcie_shutdown,
1633};
1634
1635static void imx6_pcie_quirk(struct pci_dev *dev)
1636{
1637	struct pci_bus *bus = dev->bus;
1638	struct dw_pcie_rp *pp = bus->sysdata;
1639
1640	/* Bus parent is the PCI bridge, its parent is this platform driver */
1641	if (!bus->dev.parent || !bus->dev.parent->parent)
1642		return;
1643
1644	/* Make sure we only quirk devices associated with this driver */
1645	if (bus->dev.parent->parent->driver != &imx6_pcie_driver.driver)
1646		return;
1647
1648	if (pci_is_root_bus(bus)) {
1649		struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1650		struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1651
1652		/*
1653		 * Limit config length to avoid the kernel reading beyond
1654		 * the register set and causing an abort on i.MX 6Quad
1655		 */
1656		if (imx6_pcie->drvdata->dbi_length) {
1657			dev->cfg_size = imx6_pcie->drvdata->dbi_length;
1658			dev_info(&dev->dev, "Limiting cfg_size to %d\n",
1659					dev->cfg_size);
1660		}
1661	}
1662}
1663DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_SYNOPSYS, 0xabcd,
1664			PCI_CLASS_BRIDGE_PCI, 8, imx6_pcie_quirk);
1665
1666static int __init imx6_pcie_init(void)
1667{
1668#ifdef CONFIG_ARM
1669	struct device_node *np;
1670
1671	np = of_find_matching_node(NULL, imx6_pcie_of_match);
1672	if (!np)
1673		return -ENODEV;
1674	of_node_put(np);
1675
1676	/*
1677	 * Since probe() can be deferred we need to make sure that
1678	 * hook_fault_code is not called after __init memory is freed
1679	 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1680	 * we can install the handler here without risking it
1681	 * accessing some uninitialized driver state.
1682	 */
1683	hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1684			"external abort on non-linefetch");
1685#endif
1686
1687	return platform_driver_register(&imx6_pcie_driver);
1688}
1689device_initcall(imx6_pcie_init);