pci-imx6.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * PCIe host controller driver for Freescale i.MX6 SoCs
 *
 * Copyright (C) 2013 Kosagi
 *		http://www.kosagi.com
 *
 * Author: Sean Cross <xobs@kosagi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
22
#include <linux/of_device.h>
23 24 25 26 27 28
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/resource.h>
#include <linux/signal.h>
#include <linux/types.h>
29
#include <linux/interrupt.h>
30 31 32 33 34

#include "pcie-designware.h"

#define to_imx6_pcie(x)	container_of(x, struct imx6_pcie, pp)

35 36
enum imx6_pcie_variants {
	IMX6Q,
37 38
	IMX6SX,
	IMX6QP,
39 40
};

41
struct imx6_pcie {
42
	struct pcie_port	pp;	/* pp.dbi_base is DT 0th resource */
43
	int			reset_gpio;
44
	bool			gpio_active_high;
45 46
	struct clk		*pcie_bus;
	struct clk		*pcie_phy;
47
	struct clk		*pcie_inbound_axi;
48
	struct clk		*pcie;
49
	struct regmap		*iomuxc_gpr;
50
	enum imx6_pcie_variants variant;
51 52 53 54 55
	u32			tx_deemph_gen1;
	u32			tx_deemph_gen2_3p5db;
	u32			tx_deemph_gen2_6db;
	u32			tx_swing_full;
	u32			tx_swing_low;
56
	int			link_gen;
57 58
};

59 60 61 62 63 64
/* PCIe Root Complex registers (memory-mapped) */
#define PCIE_RC_LCR				0x7c
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1	0x1
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2	0x2
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK	0xf

65 66
#define PCIE_RC_LCSR				0x80

67 68
/* PCIe Port Logic registers (memory-mapped) */
#define PL_OFFSET 0x700
69 70 71
#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
#define PCIE_PL_PFLR_LINK_STATE_MASK		(0x3f << 16)
#define PCIE_PL_PFLR_FORCE_LINK			(1 << 15)
72 73
#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
74 75
#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING	(1 << 29)
#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP		(1 << 4)
76 77 78 79 80 81 82 83 84 85 86

#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
#define PCIE_PHY_CTRL_DATA_LOC 0
#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
#define PCIE_PHY_CTRL_WR_LOC 18
#define PCIE_PHY_CTRL_RD_LOC 19

#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
#define PCIE_PHY_STAT_ACK_LOC 16

87 88 89
#define PCIE_LINK_WIDTH_SPEED_CONTROL	0x80C
#define PORT_LOGIC_SPEED_CHANGE		(0x1 << 17)

90 91
/* PHY registers (not memory-mapped) */
#define PCIE_PHY_RX_ASIC_OUT 0x100D
92
#define PCIE_PHY_RX_ASIC_OUT_VALID	(1 << 0)
93 94 95 96 97

#define PHY_RX_OVRD_IN_LO 0x1005
#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)

98
static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
99
{
100
	struct pcie_port *pp = &imx6_pcie->pp;
101 102 103 104 105
	u32 val;
	u32 max_iterations = 10;
	u32 wait_counter = 0;

	do {
106
		val = dw_pcie_readl_rc(pp, PCIE_PHY_STAT);
107 108 109 110 111 112 113 114 115 116 117 118
		val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
		wait_counter++;

		if (val == exp_val)
			return 0;

		udelay(1);
	} while (wait_counter < max_iterations);

	return -ETIMEDOUT;
}

119
static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
120
{
121
	struct pcie_port *pp = &imx6_pcie->pp;
122 123 124 125
	u32 val;
	int ret;

	val = addr << PCIE_PHY_CTRL_DATA_LOC;
126
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, val);
127 128

	val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
129
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, val);
130

131
	ret = pcie_phy_poll_ack(imx6_pcie, 1);
132 133 134 135
	if (ret)
		return ret;

	val = addr << PCIE_PHY_CTRL_DATA_LOC;
136
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, val);
137

138
	return pcie_phy_poll_ack(imx6_pcie, 0);
139 140 141
}

/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
142
static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
143
{
144
	struct pcie_port *pp = &imx6_pcie->pp;
145 146 147
	u32 val, phy_ctl;
	int ret;

148
	ret = pcie_phy_wait_ack(imx6_pcie, addr);
149 150 151 152 153
	if (ret)
		return ret;

	/* assert Read signal */
	phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
154
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, phy_ctl);
155

156
	ret = pcie_phy_poll_ack(imx6_pcie, 1);
157 158 159
	if (ret)
		return ret;

160
	val = dw_pcie_readl_rc(pp, PCIE_PHY_STAT);
161 162 163
	*data = val & 0xffff;

	/* deassert Read signal */
164
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, 0x00);
165

166
	return pcie_phy_poll_ack(imx6_pcie, 0);
167 168
}

169
static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data)
170
{
171
	struct pcie_port *pp = &imx6_pcie->pp;
172 173 174 175 176
	u32 var;
	int ret;

	/* write addr */
	/* cap addr */
177
	ret = pcie_phy_wait_ack(imx6_pcie, addr);
178 179 180 181
	if (ret)
		return ret;

	var = data << PCIE_PHY_CTRL_DATA_LOC;
182
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
183 184 185

	/* capture data */
	var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
186
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
187

188
	ret = pcie_phy_poll_ack(imx6_pcie, 1);
189 190 191 192 193
	if (ret)
		return ret;

	/* deassert cap data */
	var = data << PCIE_PHY_CTRL_DATA_LOC;
194
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
195 196

	/* wait for ack de-assertion */
197
	ret = pcie_phy_poll_ack(imx6_pcie, 0);
198 199 200 201 202
	if (ret)
		return ret;

	/* assert wr signal */
	var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
203
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
204 205

	/* wait for ack */
206
	ret = pcie_phy_poll_ack(imx6_pcie, 1);
207 208 209 210 211
	if (ret)
		return ret;

	/* deassert wr signal */
	var = data << PCIE_PHY_CTRL_DATA_LOC;
212
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
213 214

	/* wait for ack de-assertion */
215
	ret = pcie_phy_poll_ack(imx6_pcie, 0);
216 217 218
	if (ret)
		return ret;

219
	dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, 0x0);
220 221 222 223

	return 0;
}

224
static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
225 226 227
{
	u32 tmp;

228
	pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
229 230
	tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
		PHY_RX_OVRD_IN_LO_RX_PLL_EN);
231
	pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
232 233 234

	usleep_range(2000, 3000);

235
	pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
236 237
	tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
		  PHY_RX_OVRD_IN_LO_RX_PLL_EN);
238
	pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
239 240
}

241 242 243 244 245 246 247
/*  Added for PCI abort handling */
static int imx6q_pcie_abort_handler(unsigned long addr,
		unsigned int fsr, struct pt_regs *regs)
{
	return 0;
}

248
static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
249
{
250
	struct pcie_port *pp = &imx6_pcie->pp;
251 252
	u32 val, gpr1, gpr12;

253 254
	switch (imx6_pcie->variant) {
	case IMX6SX:
255 256 257 258 259 260 261
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
		/* Force PCIe PHY reset */
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
				   IMX6SX_GPR5_PCIE_BTNRST_RESET,
				   IMX6SX_GPR5_PCIE_BTNRST_RESET);
262
		break;
263 264 265 266 267
	case IMX6QP:
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
				   IMX6Q_GPR1_PCIE_SW_RST,
				   IMX6Q_GPR1_PCIE_SW_RST);
		break;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	case IMX6Q:
		/*
		 * If the bootloader already enabled the link we need some
		 * special handling to get the core back into a state where
		 * it is safe to touch it for configuration.  As there is
		 * no dedicated reset signal wired up for MX6QDL, we need
		 * to manually force LTSSM into "detect" state before
		 * completely disabling LTSSM, which is a prerequisite for
		 * core configuration.
		 *
		 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we
		 * have a strong indication that the bootloader activated
		 * the link.
		 */
		regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
		regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);

		if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
		    (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
287
			val = dw_pcie_readl_rc(pp, PCIE_PL_PFLR);
288 289
			val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
			val |= PCIE_PL_PFLR_FORCE_LINK;
290
			dw_pcie_writel_rc(pp, PCIE_PL_PFLR, val);
291 292 293 294

			regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
					   IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
		}
295

296 297 298 299 300
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
				   IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
		break;
301
	}
302 303
}

304 305
static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
{
306
	struct pcie_port *pp = &imx6_pcie->pp;
307
	struct device *dev = pp->dev;
308
	int ret = 0;
309

310 311
	switch (imx6_pcie->variant) {
	case IMX6SX:
312 313
		ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
		if (ret) {
314
			dev_err(dev, "unable to enable pcie_axi clock\n");
315
			break;
316 317 318 319
		}

		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
				   IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
320
		break;
321
	case IMX6QP: 		/* FALLTHROUGH */
322 323 324 325 326 327 328 329 330 331 332 333 334 335
	case IMX6Q:
		/* power up core phy and enable ref clock */
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
				   IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
		/*
		 * the async reset input need ref clock to sync internally,
		 * when the ref clock comes after reset, internal synced
		 * reset time is too short, cannot meet the requirement.
		 * add one ~10us delay here.
		 */
		udelay(10);
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
				   IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
		break;
336 337
	}

338
	return ret;
339 340
}

341
static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
342
{
343
	struct pcie_port *pp = &imx6_pcie->pp;
344
	struct device *dev = pp->dev;
345 346
	int ret;

347
	ret = clk_prepare_enable(imx6_pcie->pcie_phy);
348
	if (ret) {
349
		dev_err(dev, "unable to enable pcie_phy clock\n");
350
		return;
351 352
	}

353
	ret = clk_prepare_enable(imx6_pcie->pcie_bus);
354
	if (ret) {
355
		dev_err(dev, "unable to enable pcie_bus clock\n");
356
		goto err_pcie_bus;
357 358
	}

359
	ret = clk_prepare_enable(imx6_pcie->pcie);
360
	if (ret) {
361
		dev_err(dev, "unable to enable pcie clock\n");
362
		goto err_pcie;
363 364
	}

365 366
	ret = imx6_pcie_enable_ref_clk(imx6_pcie);
	if (ret) {
367
		dev_err(dev, "unable to enable pcie ref clock\n");
368 369
		goto err_ref_clk;
	}
370

371 372 373
	/* allow the clocks to stabilize */
	usleep_range(200, 500);

374
	/* Some boards don't have PCIe reset GPIO. */
375
	if (gpio_is_valid(imx6_pcie->reset_gpio)) {
376 377
		gpio_set_value_cansleep(imx6_pcie->reset_gpio,
					imx6_pcie->gpio_active_high);
378
		msleep(100);
379 380
		gpio_set_value_cansleep(imx6_pcie->reset_gpio,
					!imx6_pcie->gpio_active_high);
381
	}
382

383 384
	switch (imx6_pcie->variant) {
	case IMX6SX:
385 386
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
				   IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
387 388 389 390 391 392 393 394 395 396
		break;
	case IMX6QP:
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
				   IMX6Q_GPR1_PCIE_SW_RST, 0);

		usleep_range(200, 500);
		break;
	case IMX6Q:		/* Nothing to do */
		break;
	}
397

398
	return;
399

400 401
err_ref_clk:
	clk_disable_unprepare(imx6_pcie->pcie);
402 403 404 405
err_pcie:
	clk_disable_unprepare(imx6_pcie->pcie_bus);
err_pcie_bus:
	clk_disable_unprepare(imx6_pcie->pcie_phy);
406 407
}

408
static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
409
{
410
	if (imx6_pcie->variant == IMX6SX)
411 412 413 414
		regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
				   IMX6SX_GPR12_PCIE_RX_EQ_MASK,
				   IMX6SX_GPR12_PCIE_RX_EQ_2);

415 416 417 418 419 420 421 422 423 424
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
			IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);

	/* configure constant input signal to the pcie ctrl and phy */
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
			IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
			IMX6Q_GPR12_LOS_LEVEL, 9 << 4);

	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
425 426
			   IMX6Q_GPR8_TX_DEEMPH_GEN1,
			   imx6_pcie->tx_deemph_gen1 << 0);
427
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
428 429
			   IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
			   imx6_pcie->tx_deemph_gen2_3p5db << 6);
430
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
431 432
			   IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
			   imx6_pcie->tx_deemph_gen2_6db << 12);
433
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
434 435
			   IMX6Q_GPR8_TX_SWING_FULL,
			   imx6_pcie->tx_swing_full << 18);
436
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
437 438
			   IMX6Q_GPR8_TX_SWING_LOW,
			   imx6_pcie->tx_swing_low << 25);
439 440
}

441
static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
442
{
443
	struct pcie_port *pp = &imx6_pcie->pp;
444 445
	struct device *dev = pp->dev;

446 447 448
	/* check if the link is up or not */
	if (!dw_pcie_wait_for_link(pp))
		return 0;
449

450
	dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
451 452
		dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R0),
		dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1));
453
	return -ETIMEDOUT;
454 455
}

456
static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
457
{
458
	struct pcie_port *pp = &imx6_pcie->pp;
459
	struct device *dev = pp->dev;
460
	u32 tmp;
461 462 463
	unsigned int retries;

	for (retries = 0; retries < 200; retries++) {
464
		tmp = dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL);
465 466 467 468 469 470
		/* Test if the speed change finished. */
		if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
			return 0;
		usleep_range(100, 1000);
	}

471
	dev_err(dev, "Speed change timeout\n");
472
	return -EINVAL;
473 474
}

475 476
static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
{
477 478
	struct imx6_pcie *imx6_pcie = arg;
	struct pcie_port *pp = &imx6_pcie->pp;
479 480 481 482

	return dw_handle_msi_irq(pp);
}

483
static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
484
{
485
	struct pcie_port *pp = &imx6_pcie->pp;
486
	struct device *dev = pp->dev;
487
	u32 tmp;
488
	int ret;
489 490 491 492 493 494

	/*
	 * Force Gen1 operation when starting the link.  In case the link is
	 * started in Gen2 mode, there is a possibility the devices on the
	 * bus will not be detected at all.  This happens with PCIe switches.
	 */
495
	tmp = dw_pcie_readl_rc(pp, PCIE_RC_LCR);
496 497
	tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
	tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
498
	dw_pcie_writel_rc(pp, PCIE_RC_LCR, tmp);
499 500 501 502 503

	/* Start LTSSM. */
	regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
			IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);

504
	ret = imx6_pcie_wait_for_link(imx6_pcie);
505
	if (ret) {
506
		dev_info(dev, "Link never came up\n");
507 508
		goto err_reset_phy;
	}
509

510 511
	if (imx6_pcie->link_gen == 2) {
		/* Allow Gen2 mode after the link is up. */
512
		tmp = dw_pcie_readl_rc(pp, PCIE_RC_LCR);
513 514
		tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
		tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
515
		dw_pcie_writel_rc(pp, PCIE_RC_LCR, tmp);
516
	} else {
517
		dev_info(dev, "Link: Gen2 disabled\n");
518
	}
519 520 521 522 523

	/*
	 * Start Directed Speed Change so the best possible speed both link
	 * partners support can be negotiated.
	 */
524
	tmp = dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL);
525
	tmp |= PORT_LOGIC_SPEED_CHANGE;
526
	dw_pcie_writel_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
527

528
	ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
529
	if (ret) {
530
		dev_err(dev, "Failed to bring link up!\n");
531
		goto err_reset_phy;
532 533 534
	}

	/* Make sure link training is finished as well! */
535
	ret = imx6_pcie_wait_for_link(imx6_pcie);
536
	if (ret) {
537
		dev_err(dev, "Failed to bring link up!\n");
538
		goto err_reset_phy;
539 540
	}

541
	tmp = dw_pcie_readl_rc(pp, PCIE_RC_LCSR);
542
	dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
543
	return 0;
544 545

err_reset_phy:
546
	dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
547 548 549
		dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R0),
		dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1));
	imx6_pcie_reset_phy(imx6_pcie);
550
	return ret;
551 552 553 554
}

static void imx6_pcie_host_init(struct pcie_port *pp)
{
555
	struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
556

557 558 559
	imx6_pcie_assert_core_reset(imx6_pcie);
	imx6_pcie_init_phy(imx6_pcie);
	imx6_pcie_deassert_core_reset(imx6_pcie);
560
	dw_pcie_setup_rc(pp);
561
	imx6_pcie_establish_link(imx6_pcie);
562 563 564

	if (IS_ENABLED(CONFIG_PCI_MSI))
		dw_pcie_msi_init(pp);
565 566 567 568
}

static int imx6_pcie_link_up(struct pcie_port *pp)
{
569
	return dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1) &
570
			PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
571 572 573 574 575 576 577
}

static struct pcie_host_ops imx6_pcie_host_ops = {
	.link_up = imx6_pcie_link_up,
	.host_init = imx6_pcie_host_init,
};

578 579
static int __init imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
				     struct platform_device *pdev)
580
{
581
	struct pcie_port *pp = &imx6_pcie->pp;
582
	struct device *dev = pp->dev;
583 584
	int ret;

585 586 587
	if (IS_ENABLED(CONFIG_PCI_MSI)) {
		pp->msi_irq = platform_get_irq_byname(pdev, "msi");
		if (pp->msi_irq <= 0) {
588
			dev_err(dev, "failed to get MSI irq\n");
589 590 591
			return -ENODEV;
		}

592
		ret = devm_request_irq(dev, pp->msi_irq,
593
				       imx6_pcie_msi_handler,
594
				       IRQF_SHARED | IRQF_NO_THREAD,
595
				       "mx6-pcie-msi", imx6_pcie);
596
		if (ret) {
597
			dev_err(dev, "failed to request MSI irq\n");
598
			return ret;
599 600 601
		}
	}

602 603 604 605 606
	pp->root_bus_nr = -1;
	pp->ops = &imx6_pcie_host_ops;

	ret = dw_pcie_host_init(pp);
	if (ret) {
607
		dev_err(dev, "failed to initialize host\n");
608 609 610 611 612 613 614 615
		return ret;
	}

	return 0;
}

static int __init imx6_pcie_probe(struct platform_device *pdev)
{
616
	struct device *dev = &pdev->dev;
617 618 619
	struct imx6_pcie *imx6_pcie;
	struct pcie_port *pp;
	struct resource *dbi_base;
620
	struct device_node *node = dev->of_node;
621 622
	int ret;

623
	imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
624 625 626 627
	if (!imx6_pcie)
		return -ENOMEM;

	pp = &imx6_pcie->pp;
628
	pp->dev = dev;
629

630
	imx6_pcie->variant =
631
		(enum imx6_pcie_variants)of_device_get_match_data(dev);
632

633 634 635 636 637
	/* Added for PCI abort handling */
	hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
		"imprecise external abort");

	dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
638
	pp->dbi_base = devm_ioremap_resource(dev, dbi_base);
639 640
	if (IS_ERR(pp->dbi_base))
		return PTR_ERR(pp->dbi_base);
641 642

	/* Fetch GPIOs */
643 644
	imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
	imx6_pcie->gpio_active_high = of_property_read_bool(node,
645
						"reset-gpio-active-high");
646
	if (gpio_is_valid(imx6_pcie->reset_gpio)) {
647
		ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
648 649 650 651
				imx6_pcie->gpio_active_high ?
					GPIOF_OUT_INIT_HIGH :
					GPIOF_OUT_INIT_LOW,
				"PCIe reset");
652
		if (ret) {
653
			dev_err(dev, "unable to get reset gpio\n");
654 655 656
			return ret;
		}
	}
657 658

	/* Fetch clocks */
659
	imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
660
	if (IS_ERR(imx6_pcie->pcie_phy)) {
661
		dev_err(dev, "pcie_phy clock source missing or invalid\n");
662
		return PTR_ERR(imx6_pcie->pcie_phy);
663 664
	}

665
	imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
666
	if (IS_ERR(imx6_pcie->pcie_bus)) {
667
		dev_err(dev, "pcie_bus clock source missing or invalid\n");
668
		return PTR_ERR(imx6_pcie->pcie_bus);
669 670
	}

671
	imx6_pcie->pcie = devm_clk_get(dev, "pcie");
672
	if (IS_ERR(imx6_pcie->pcie)) {
673
		dev_err(dev, "pcie clock source missing or invalid\n");
674
		return PTR_ERR(imx6_pcie->pcie);
675 676
	}

677
	if (imx6_pcie->variant == IMX6SX) {
678
		imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
679 680
							   "pcie_inbound_axi");
		if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
681
			dev_err(dev,
682 683 684 685 686
				"pcie_incbound_axi clock missing or invalid\n");
			return PTR_ERR(imx6_pcie->pcie_inbound_axi);
		}
	}

687 688 689 690
	/* Grab GPR config register range */
	imx6_pcie->iomuxc_gpr =
		 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
	if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
691
		dev_err(dev, "unable to find iomuxc registers\n");
692
		return PTR_ERR(imx6_pcie->iomuxc_gpr);
693
	}
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

	/* Grab PCIe PHY Tx Settings */
	if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
				 &imx6_pcie->tx_deemph_gen1))
		imx6_pcie->tx_deemph_gen1 = 0;

	if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
				 &imx6_pcie->tx_deemph_gen2_3p5db))
		imx6_pcie->tx_deemph_gen2_3p5db = 0;

	if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
				 &imx6_pcie->tx_deemph_gen2_6db))
		imx6_pcie->tx_deemph_gen2_6db = 20;

	if (of_property_read_u32(node, "fsl,tx-swing-full",
				 &imx6_pcie->tx_swing_full))
		imx6_pcie->tx_swing_full = 127;

	if (of_property_read_u32(node, "fsl,tx-swing-low",
				 &imx6_pcie->tx_swing_low))
		imx6_pcie->tx_swing_low = 127;
715

716
	/* Limit link speed */
717
	ret = of_property_read_u32(node, "fsl,max-link-speed",
718 719 720 721
				   &imx6_pcie->link_gen);
	if (ret)
		imx6_pcie->link_gen = 1;

722
	ret = imx6_add_pcie_port(imx6_pcie, pdev);
723
	if (ret < 0)
724
		return ret;
725 726 727 728 729

	platform_set_drvdata(pdev, imx6_pcie);
	return 0;
}

730 731 732 733 734
static void imx6_pcie_shutdown(struct platform_device *pdev)
{
	struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);

	/* bring down link, so bootloader gets clean state in case of reboot */
735
	imx6_pcie_assert_core_reset(imx6_pcie);
736 737
}

738
static const struct of_device_id imx6_pcie_of_match[] = {
739 740
	{ .compatible = "fsl,imx6q-pcie",  .data = (void *)IMX6Q,  },
	{ .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
741
	{ .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
742 743 744 745 746 747
	{},
};

static struct platform_driver imx6_pcie_driver = {
	.driver = {
		.name	= "imx6q-pcie",
748
		.of_match_table = imx6_pcie_of_match,
749
	},
750
	.shutdown = imx6_pcie_shutdown,
751 752 753 754 755 756
};

static int __init imx6_pcie_init(void)
{
	return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
}
757
device_initcall(imx6_pcie_init);
新手
引导
客服 返回
顶部