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