pcie-rcar-host.c 25.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3
/*
 * PCIe driver for Renesas R-Car SoCs
4
 *  Copyright (C) 2014-2020 Renesas Electronics Europe Ltd
5 6 7 8 9 10
 *
 * Based on:
 *  arch/sh/drivers/pci/pcie-sh7786.c
 *  arch/sh/drivers/pci/ops-sh7786.c
 *  Copyright (C) 2009 - 2011  Paul Mundt
 *
11
 * Author: Phil Edworthy <phil.edworthy@renesas.com>
12 13
 */

M
Marek Vasut 已提交
14
#include <linux/bitops.h>
15 16 17
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
18 19
#include <linux/irq.h>
#include <linux/irqdomain.h>
20
#include <linux/kernel.h>
21
#include <linux/init.h>
22
#include <linux/msi.h>
23 24 25 26 27
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of_pci.h>
#include <linux/of_platform.h>
#include <linux/pci.h>
28
#include <linux/phy/phy.h>
29
#include <linux/platform_device.h>
30
#include <linux/pm_runtime.h>
31 32
#include <linux/slab.h>

33
#include "pcie-rcar.h"
34

35 36 37
struct rcar_msi {
	DECLARE_BITMAP(used, INT_PCI_MSI_NR);
	struct irq_domain *domain;
38
	struct msi_controller chip;
39 40 41 42 43 44
	unsigned long pages;
	struct mutex lock;
	int irq1;
	int irq2;
};

45
static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
46 47 48 49
{
	return container_of(chip, struct rcar_msi, chip);
}

50
/* Structure representing the PCIe interface */
51 52
struct rcar_pcie_host {
	struct rcar_pcie	pcie;
53
	struct phy		*phy;
54
	struct clk		*bus_clk;
55
	struct			rcar_msi msi;
56
	int			(*phy_init_fn)(struct rcar_pcie_host *host);
57 58 59 60
};

static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
{
61
	unsigned int shift = BITS_PER_BYTE * (where & 3);
62
	u32 val = rcar_pci_read_reg(pcie, where & ~3);
63 64 65 66 67

	return val >> shift;
}

/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
68
static int rcar_pcie_config_access(struct rcar_pcie_host *host,
69 70 71
		unsigned char access_type, struct pci_bus *bus,
		unsigned int devfn, int where, u32 *data)
{
72
	struct rcar_pcie *pcie = &host->pcie;
73
	unsigned int dev, func, reg, index;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	dev = PCI_SLOT(devfn);
	func = PCI_FUNC(devfn);
	reg = where & ~3;
	index = reg / 4;

	/*
	 * While each channel has its own memory-mapped extended config
	 * space, it's generally only accessible when in endpoint mode.
	 * When in root complex mode, the controller is unable to target
	 * itself with either type 0 or type 1 accesses, and indeed, any
	 * controller initiated target transfer to its own config space
	 * result in a completer abort.
	 *
	 * Each channel effectively only supports a single device, but as
	 * the same channel <-> device access works for any PCI_SLOT()
	 * value, we cheat a bit here and bind the controller's config
	 * space to devfn 0 in order to enable self-enumeration. In this
	 * case the regular ECAR/ECDR path is sidelined and the mangled
	 * config access itself is initiated as an internal bus transaction.
	 */
	if (pci_is_root_bus(bus)) {
		if (dev != 0)
			return PCIBIOS_DEVICE_NOT_FOUND;

99
		if (access_type == RCAR_PCI_ACCESS_READ)
100
			*data = rcar_pci_read_reg(pcie, PCICONF(index));
101
		else
102
			rcar_pci_write_reg(pcie, *data, PCICONF(index));
103 104 105 106 107

		return PCIBIOS_SUCCESSFUL;
	}

	/* Clear errors */
108
	rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
109 110

	/* Set the PIO address */
111 112
	rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
		PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
113 114

	/* Enable the configuration access */
115
	if (pci_is_root_bus(bus->parent))
116
		rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
117
	else
118
		rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
119 120

	/* Check for errors */
121
	if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
122 123 124 125 126 127 128
		return PCIBIOS_DEVICE_NOT_FOUND;

	/* Check for master and target aborts */
	if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) &
		(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
		return PCIBIOS_DEVICE_NOT_FOUND;

129 130
	if (access_type == RCAR_PCI_ACCESS_READ)
		*data = rcar_pci_read_reg(pcie, PCIECDR);
131
	else
132
		rcar_pci_write_reg(pcie, *data, PCIECDR);
133 134

	/* Disable the configuration access */
135
	rcar_pci_write_reg(pcie, 0, PCIECCTLR);
136 137 138 139 140 141 142

	return PCIBIOS_SUCCESSFUL;
}

static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
			       int where, int size, u32 *val)
{
143
	struct rcar_pcie_host *host = bus->sysdata;
144 145
	int ret;

146
	ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
147 148 149 150 151 152 153
				      bus, devfn, where, val);
	if (ret != PCIBIOS_SUCCESSFUL) {
		*val = 0xffffffff;
		return ret;
	}

	if (size == 1)
154
		*val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
155
	else if (size == 2)
156
		*val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
157

M
Marek Vasut 已提交
158 159
	dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
		bus->number, devfn, where, size, *val);
160 161 162 163 164 165 166 167

	return ret;
}

/* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
				int where, int size, u32 val)
{
168
	struct rcar_pcie_host *host = bus->sysdata;
169
	unsigned int shift;
170
	u32 data;
171
	int ret;
172

173
	ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
174 175 176 177
				      bus, devfn, where, &data);
	if (ret != PCIBIOS_SUCCESSFUL)
		return ret;

M
Marek Vasut 已提交
178 179
	dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08x\n",
		bus->number, devfn, where, size, val);
180 181

	if (size == 1) {
182
		shift = BITS_PER_BYTE * (where & 3);
183 184 185
		data &= ~(0xff << shift);
		data |= ((val & 0xff) << shift);
	} else if (size == 2) {
186
		shift = BITS_PER_BYTE * (where & 2);
187 188 189 190 191
		data &= ~(0xffff << shift);
		data |= ((val & 0xffff) << shift);
	} else
		data = val;

192
	ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
193 194 195 196 197 198 199 200 201 202
				      bus, devfn, where, &data);

	return ret;
}

static struct pci_ops rcar_pcie_ops = {
	.read	= rcar_pcie_read_conf,
	.write	= rcar_pcie_write_conf,
};

203 204
static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
{
205
	struct device *dev = pcie->dev;
206 207 208 209 210 211 212
	unsigned int timeout = 1000;
	u32 macsr;

	if ((rcar_pci_read_reg(pcie, MACS2R) & LINK_SPEED) != LINK_SPEED_5_0GTS)
		return;

	if (rcar_pci_read_reg(pcie, MACCTLR) & SPEED_CHANGE) {
213
		dev_err(dev, "Speed change already in progress\n");
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		return;
	}

	macsr = rcar_pci_read_reg(pcie, MACSR);
	if ((macsr & LINK_SPEED) == LINK_SPEED_5_0GTS)
		goto done;

	/* Set target link speed to 5.0 GT/s */
	rcar_rmw32(pcie, EXPCAP(12), PCI_EXP_LNKSTA_CLS,
		   PCI_EXP_LNKSTA_CLS_5_0GB);

	/* Set speed change reason as intentional factor */
	rcar_rmw32(pcie, MACCGSPSETR, SPCNGRSN, 0);

	/* Clear SPCHGFIN, SPCHGSUC, and SPCHGFAIL */
	if (macsr & (SPCHGFIN | SPCHGSUC | SPCHGFAIL))
		rcar_pci_write_reg(pcie, macsr, MACSR);

	/* Start link speed change */
	rcar_rmw32(pcie, MACCTLR, SPEED_CHANGE, SPEED_CHANGE);

	while (timeout--) {
		macsr = rcar_pci_read_reg(pcie, MACSR);
		if (macsr & SPCHGFIN) {
			/* Clear the interrupt bits */
			rcar_pci_write_reg(pcie, macsr, MACSR);

			if (macsr & SPCHGFAIL)
242
				dev_err(dev, "Speed change failed\n");
243 244 245 246 247

			goto done;
		}

		msleep(1);
248
	}
249

250
	dev_err(dev, "Speed change timed out\n");
251 252

done:
253
	dev_info(dev, "Current link speed is %s GT/s\n",
254 255 256
		 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
}

257
static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
K
Kazufumi Ikeda 已提交
258
{
259
	struct rcar_pcie *pcie = &host->pcie;
260
	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
K
Kazufumi Ikeda 已提交
261 262 263 264 265
	struct resource_entry *win;
	LIST_HEAD(res);
	int i = 0;

	/* Try setting 5 GT/s link speed */
266
	rcar_pcie_force_speedup(pcie);
K
Kazufumi Ikeda 已提交
267 268

	/* Setup PCI resources */
269
	resource_list_for_each_entry(win, &bridge->windows) {
K
Kazufumi Ikeda 已提交
270 271 272 273 274 275 276 277
		struct resource *res = win->res;

		if (!res->flags)
			continue;

		switch (resource_type(res)) {
		case IORESOURCE_IO:
		case IORESOURCE_MEM:
278
			rcar_pcie_set_outbound(pcie, i, win);
K
Kazufumi Ikeda 已提交
279 280 281 282 283 284
			i++;
			break;
		}
	}
}

285
static int rcar_pcie_enable(struct rcar_pcie_host *host)
286
{
287
	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
288

289
	rcar_pcie_hw_enable(host);
290

291
	pci_add_flags(PCI_REASSIGN_ALL_BUS);
292

293
	bridge->sysdata = host;
294
	bridge->ops = &rcar_pcie_ops;
295
	if (IS_ENABLED(CONFIG_PCI_MSI))
296
		bridge->msi = &host->msi.chip;
297

298
	return pci_host_probe(bridge);
299 300 301 302
}

static int phy_wait_for_ack(struct rcar_pcie *pcie)
{
303
	struct device *dev = pcie->dev;
304 305 306
	unsigned int timeout = 100;

	while (timeout--) {
307
		if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
308 309 310 311 312
			return 0;

		udelay(100);
	}

313
	dev_err(dev, "Access to PCIe phy timed out\n");
314 315 316 317 318

	return -ETIMEDOUT;
}

static void phy_write_reg(struct rcar_pcie *pcie,
319 320
			  unsigned int rate, u32 addr,
			  unsigned int lane, u32 data)
321
{
322
	u32 phyaddr;
323 324 325 326 327 328 329

	phyaddr = WRITE_CMD |
		((rate & 1) << RATE_POS) |
		((lane & 0xf) << LANE_POS) |
		((addr & 0xff) << ADR_POS);

	/* Set write data */
330 331
	rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
	rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
332 333 334 335 336

	/* Ignore errors as they will be dealt with if the data link is down */
	phy_wait_for_ack(pcie);

	/* Clear command */
337 338
	rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
	rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
339 340 341 342 343 344 345 346 347 348

	/* Ignore errors as they will be dealt with if the data link is down */
	phy_wait_for_ack(pcie);
}

static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
{
	int err;

	/* Begin initialization */
349
	rcar_pci_write_reg(pcie, 0, PCIETCTLR);
350 351

	/* Set mode */
352
	rcar_pci_write_reg(pcie, 1, PCIEMSR);
353

354 355 356 357
	err = rcar_pcie_wait_for_phyrdy(pcie);
	if (err)
		return err;

358 359 360 361 362
	/*
	 * Initial header for port config space is type 1, set the device
	 * class to match. Hardware takes care of propagating the IDSETR
	 * settings, so there is no need to bother with a quirk.
	 */
363
	rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
364 365 366 367 368 369 370 371 372

	/*
	 * Setup Secondary Bus Number & Subordinate Bus Number, even though
	 * they aren't used, to avoid bridge being detected as broken.
	 */
	rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
	rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);

	/* Initialize default capabilities. */
373
	rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
374 375 376 377 378 379
	rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS),
		PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
	rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f,
		PCI_HEADER_TYPE_BRIDGE);

	/* Enable data link layer active state reporting */
380 381
	rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
		PCI_EXP_LNKCAP_DLLLARC);
382 383 384 385 386

	/* Write out the physical slot number = 0 */
	rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0);

	/* Set the completion timer timeout to the maximum 50ms. */
387
	rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
388 389

	/* Terminate list of capabilities (Next Capability Offset=0) */
390
	rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
391

392 393
	/* Enable MSI */
	if (IS_ENABLED(CONFIG_PCI_MSI))
394
		rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
395

396 397
	rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);

398
	/* Finish initialization - establish a PCI Express link */
399
	rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
400 401 402 403 404 405 406 407 408 409 410 411 412 413

	/* This will timeout if we don't have a link. */
	err = rcar_pcie_wait_for_dl(pcie);
	if (err)
		return err;

	/* Enable INTx interrupts */
	rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8);

	wmb();

	return 0;
}

414
static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
415
{
416 417
	struct rcar_pcie *pcie = &host->pcie;

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	/* Initialize the phy */
	phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191);
	phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180);
	phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188);
	phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188);
	phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014);
	phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014);
	phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0);
	phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB);
	phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062);
	phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000);
	phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000);
	phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806);

	phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5);
	phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F);
	phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000);

436
	return 0;
437 438
}

439
static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
440
{
441 442
	struct rcar_pcie *pcie = &host->pcie;

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	/*
	 * These settings come from the R-Car Series, 2nd Generation User's
	 * Manual, section 50.3.1 (2) Initialization of the physical layer.
	 */
	rcar_pci_write_reg(pcie, 0x000f0030, GEN2_PCIEPHYADDR);
	rcar_pci_write_reg(pcie, 0x00381203, GEN2_PCIEPHYDATA);
	rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
	rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);

	rcar_pci_write_reg(pcie, 0x000f0054, GEN2_PCIEPHYADDR);
	/* The following value is for DC connection, no termination resistor */
	rcar_pci_write_reg(pcie, 0x13802007, GEN2_PCIEPHYDATA);
	rcar_pci_write_reg(pcie, 0x00000001, GEN2_PCIEPHYCTRL);
	rcar_pci_write_reg(pcie, 0x00000006, GEN2_PCIEPHYCTRL);

458
	return 0;
459 460
}

461
static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
462 463 464
{
	int err;

465
	err = phy_init(host->phy);
466 467 468
	if (err)
		return err;

469
	err = phy_power_on(host->phy);
470
	if (err)
471
		phy_exit(host->phy);
472 473

	return err;
474 475
}

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
static int rcar_msi_alloc(struct rcar_msi *chip)
{
	int msi;

	mutex_lock(&chip->lock);

	msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
	if (msi < INT_PCI_MSI_NR)
		set_bit(msi, chip->used);
	else
		msi = -ENOSPC;

	mutex_unlock(&chip->lock);

	return msi;
}

493 494 495 496 497 498 499 500 501 502 503 504
static int rcar_msi_alloc_region(struct rcar_msi *chip, int no_irqs)
{
	int msi;

	mutex_lock(&chip->lock);
	msi = bitmap_find_free_region(chip->used, INT_PCI_MSI_NR,
				      order_base_2(no_irqs));
	mutex_unlock(&chip->lock);

	return msi;
}

505 506 507 508 509 510 511 512 513
static void rcar_msi_free(struct rcar_msi *chip, unsigned long irq)
{
	mutex_lock(&chip->lock);
	clear_bit(irq, chip->used);
	mutex_unlock(&chip->lock);
}

static irqreturn_t rcar_pcie_msi_irq(int irq, void *data)
{
514 515 516
	struct rcar_pcie_host *host = data;
	struct rcar_pcie *pcie = &host->pcie;
	struct rcar_msi *msi = &host->msi;
517
	struct device *dev = pcie->dev;
518 519
	unsigned long reg;

520
	reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
521 522 523 524 525 526 527

	/* MSI & INTx share an interrupt - we only handle MSI here */
	if (!reg)
		return IRQ_NONE;

	while (reg) {
		unsigned int index = find_first_bit(&reg, 32);
528
		unsigned int msi_irq;
529 530

		/* clear the interrupt */
531
		rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
532

533 534
		msi_irq = irq_find_mapping(msi->domain, index);
		if (msi_irq) {
535
			if (test_bit(index, msi->used))
536
				generic_handle_irq(msi_irq);
537
			else
538
				dev_info(dev, "unhandled MSI\n");
539 540
		} else {
			/* Unknown MSI, just clear it */
541
			dev_dbg(dev, "unexpected MSI\n");
542 543 544
		}

		/* see if there's any more pending in this vector */
545
		reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
546 547 548 549 550
	}

	return IRQ_HANDLED;
}

551
static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
552 553 554
			      struct msi_desc *desc)
{
	struct rcar_msi *msi = to_rcar_msi(chip);
555 556 557
	struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
						   msi.chip);
	struct rcar_pcie *pcie = &host->pcie;
558 559 560 561 562 563 564 565
	struct msi_msg msg;
	unsigned int irq;
	int hwirq;

	hwirq = rcar_msi_alloc(msi);
	if (hwirq < 0)
		return hwirq;

566
	irq = irq_find_mapping(msi->domain, hwirq);
567 568 569 570 571 572 573
	if (!irq) {
		rcar_msi_free(msi, hwirq);
		return -EINVAL;
	}

	irq_set_msi_desc(irq, desc);

574 575
	msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
	msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
576 577
	msg.data = hwirq;

578
	pci_write_msi_msg(irq, &msg);
579 580 581 582

	return 0;
}

583 584 585 586
static int rcar_msi_setup_irqs(struct msi_controller *chip,
			       struct pci_dev *pdev, int nvec, int type)
{
	struct rcar_msi *msi = to_rcar_msi(chip);
587 588 589
	struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
						   msi.chip);
	struct rcar_pcie *pcie = &host->pcie;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	struct msi_desc *desc;
	struct msi_msg msg;
	unsigned int irq;
	int hwirq;
	int i;

	/* MSI-X interrupts are not supported */
	if (type == PCI_CAP_ID_MSIX)
		return -EINVAL;

	WARN_ON(!list_is_singular(&pdev->dev.msi_list));
	desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);

	hwirq = rcar_msi_alloc_region(msi, nvec);
	if (hwirq < 0)
		return -ENOSPC;

	irq = irq_find_mapping(msi->domain, hwirq);
	if (!irq)
		return -ENOSPC;

	for (i = 0; i < nvec; i++) {
		/*
		 * irq_create_mapping() called from rcar_pcie_probe() pre-
		 * allocates descs,  so there is no need to allocate descs here.
		 * We can therefore assume that if irq_find_mapping() above
		 * returns non-zero, then the descs are also successfully
		 * allocated.
		 */
		if (irq_set_msi_desc_off(irq, i, desc)) {
			/* TODO: clear */
			return -EINVAL;
		}
	}

	desc->nvec_used = nvec;
	desc->msi_attrib.multiple = order_base_2(nvec);

	msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
	msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
	msg.data = hwirq;

	pci_write_msi_msg(irq, &msg);

	return 0;
}

637
static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
638 639 640 641 642 643 644 645 646
{
	struct rcar_msi *msi = to_rcar_msi(chip);
	struct irq_data *d = irq_get_irq_data(irq);

	rcar_msi_free(msi, d->hwirq);
}

static struct irq_chip rcar_msi_irq_chip = {
	.name = "R-Car PCIe MSI",
647 648 649 650
	.irq_enable = pci_msi_unmask_irq,
	.irq_disable = pci_msi_mask_irq,
	.irq_mask = pci_msi_mask_irq,
	.irq_unmask = pci_msi_unmask_irq,
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
};

static int rcar_msi_map(struct irq_domain *domain, unsigned int irq,
			irq_hw_number_t hwirq)
{
	irq_set_chip_and_handler(irq, &rcar_msi_irq_chip, handle_simple_irq);
	irq_set_chip_data(irq, domain->host_data);

	return 0;
}

static const struct irq_domain_ops msi_domain_ops = {
	.map = rcar_msi_map,
};

666
static void rcar_pcie_unmap_msi(struct rcar_pcie_host *host)
667
{
668
	struct rcar_msi *msi = &host->msi;
669 670 671 672 673 674 675 676 677 678 679
	int i, irq;

	for (i = 0; i < INT_PCI_MSI_NR; i++) {
		irq = irq_find_mapping(msi->domain, i);
		if (irq > 0)
			irq_dispose_mapping(irq);
	}

	irq_domain_remove(msi->domain);
}

680
static void rcar_pcie_hw_enable_msi(struct rcar_pcie_host *host)
K
Kazufumi Ikeda 已提交
681
{
682 683
	struct rcar_pcie *pcie = &host->pcie;
	struct rcar_msi *msi = &host->msi;
K
Kazufumi Ikeda 已提交
684 685 686 687 688 689 690 691 692 693 694 695
	unsigned long base;

	/* setup MSI data target */
	base = virt_to_phys((void *)msi->pages);

	rcar_pci_write_reg(pcie, lower_32_bits(base) | MSIFE, PCIEMSIALR);
	rcar_pci_write_reg(pcie, upper_32_bits(base), PCIEMSIAUR);

	/* enable all MSI interrupts */
	rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
}

696
static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
697
{
698
	struct rcar_pcie *pcie = &host->pcie;
699
	struct device *dev = pcie->dev;
700
	struct rcar_msi *msi = &host->msi;
701
	int err, i;
702 703 704

	mutex_init(&msi->lock);

705
	msi->chip.dev = dev;
706
	msi->chip.setup_irq = rcar_msi_setup_irq;
707
	msi->chip.setup_irqs = rcar_msi_setup_irqs;
708 709
	msi->chip.teardown_irq = rcar_msi_teardown_irq;

710
	msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
711 712
					    &msi_domain_ops, &msi->chip);
	if (!msi->domain) {
713
		dev_err(dev, "failed to create IRQ domain\n");
714 715 716
		return -ENOMEM;
	}

717 718 719
	for (i = 0; i < INT_PCI_MSI_NR; i++)
		irq_create_mapping(msi->domain, i);

720
	/* Two irqs are for MSI, but they are also used for non-MSI irqs */
721
	err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
722
			       IRQF_SHARED | IRQF_NO_THREAD,
723
			       rcar_msi_irq_chip.name, host);
724
	if (err < 0) {
725
		dev_err(dev, "failed to request IRQ: %d\n", err);
726 727 728
		goto err;
	}

729
	err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
730
			       IRQF_SHARED | IRQF_NO_THREAD,
731
			       rcar_msi_irq_chip.name, host);
732
	if (err < 0) {
733
		dev_err(dev, "failed to request IRQ: %d\n", err);
734 735 736 737 738
		goto err;
	}

	/* setup MSI data target */
	msi->pages = __get_free_pages(GFP_KERNEL, 0);
739
	rcar_pcie_hw_enable_msi(host);
740 741 742 743

	return 0;

err:
744
	rcar_pcie_unmap_msi(host);
745 746 747
	return err;
}

748
static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
749
{
750 751
	struct rcar_pcie *pcie = &host->pcie;
	struct rcar_msi *msi = &host->msi;
752 753 754 755 756 757 758 759 760

	/* Disable all MSI interrupts */
	rcar_pci_write_reg(pcie, 0, PCIEMSIIER);

	/* Disable address decoding of the MSI interrupt, MSIFE */
	rcar_pci_write_reg(pcie, 0, PCIEMSIALR);

	free_pages(msi->pages, 0);

761
	rcar_pcie_unmap_msi(host);
762 763
}

764
static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
765
{
766
	struct rcar_pcie *pcie = &host->pcie;
767
	struct device *dev = pcie->dev;
768
	struct resource res;
769
	int err, i;
770

771 772 773
	host->phy = devm_phy_optional_get(dev, "pcie");
	if (IS_ERR(host->phy))
		return PTR_ERR(host->phy);
774

775
	err = of_address_to_resource(dev->of_node, 0, &res);
776 777 778
	if (err)
		return err;

779
	pcie->base = devm_ioremap_resource(dev, &res);
780 781 782
	if (IS_ERR(pcie->base))
		return PTR_ERR(pcie->base);

783 784
	host->bus_clk = devm_clk_get(dev, "pcie_bus");
	if (IS_ERR(host->bus_clk)) {
785
		dev_err(dev, "cannot get pcie bus clock\n");
786
		return PTR_ERR(host->bus_clk);
787 788
	}

789
	i = irq_of_parse_and_map(dev->of_node, 0);
790
	if (!i) {
791
		dev_err(dev, "cannot get platform resources for msi interrupt\n");
792
		err = -ENOENT;
793
		goto err_irq1;
794
	}
795
	host->msi.irq1 = i;
796

797
	i = irq_of_parse_and_map(dev->of_node, 1);
798
	if (!i) {
799
		dev_err(dev, "cannot get platform resources for msi interrupt\n");
800
		err = -ENOENT;
801
		goto err_irq2;
802
	}
803
	host->msi.irq2 = i;
804

805 806
	return 0;

807
err_irq2:
808
	irq_dispose_mapping(host->msi.irq1);
809
err_irq1:
810 811 812 813
	return err;
}

static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
814
				    struct resource_entry *entry,
815 816
				    int *index)
{
817 818 819 820
	u64 restype = entry->res->flags;
	u64 cpu_addr = entry->res->start;
	u64 cpu_end = entry->res->end;
	u64 pci_addr = entry->res->start - entry->offset;
821 822
	u32 flags = LAM_64BIT | LAR_ENABLE;
	u64 mask;
823
	u64 size = resource_size(entry->res);
824 825 826 827 828 829
	int idx = *index;

	if (restype & IORESOURCE_PREFETCH)
		flags |= LAM_PREFETCH;

	while (cpu_addr < cpu_end) {
830 831 832 833
		if (idx >= MAX_NR_INBOUND_MAPS - 1) {
			dev_err(pcie->dev, "Failed to map inbound regions!\n");
			return -EINVAL;
		}
834 835 836 837 838 839 840 841 842
		/*
		 * If the size of the range is larger than the alignment of
		 * the start address, we have to use multiple entries to
		 * perform the mapping.
		 */
		if (cpu_addr > 0) {
			unsigned long nr_zeros = __ffs64(cpu_addr);
			u64 alignment = 1ULL << nr_zeros;

843
			size = min(size, alignment);
844 845 846 847 848 849 850
		}
		/* Hardware supports max 4GiB inbound region */
		size = min(size, 1ULL << 32);

		mask = roundup_pow_of_two(size) - 1;
		mask &= ~0xf;

851 852
		rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
				      lower_32_bits(mask) | flags, idx, true);
853 854 855 856 857 858 859 860 861 862

		pci_addr += size;
		cpu_addr += size;
		idx += 2;
	}
	*index = idx;

	return 0;
}

863
static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
864
{
865
	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
866 867
	struct resource_entry *entry;
	int index = 0, err = 0;
868

869
	resource_list_for_each_entry(entry, &bridge->dma_ranges) {
870
		err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
871
		if (err)
872
			break;
873 874
	}

875
	return err;
876 877 878
}

static const struct of_device_id rcar_pcie_of_match[] = {
879 880
	{ .compatible = "renesas,pcie-r8a7779",
	  .data = rcar_pcie_phy_init_h1 },
881
	{ .compatible = "renesas,pcie-r8a7790",
882
	  .data = rcar_pcie_phy_init_gen2 },
883
	{ .compatible = "renesas,pcie-r8a7791",
884
	  .data = rcar_pcie_phy_init_gen2 },
885
	{ .compatible = "renesas,pcie-rcar-gen2",
886
	  .data = rcar_pcie_phy_init_gen2 },
887
	{ .compatible = "renesas,pcie-r8a7795",
888
	  .data = rcar_pcie_phy_init_gen3 },
889
	{ .compatible = "renesas,pcie-rcar-gen3",
890
	  .data = rcar_pcie_phy_init_gen3 },
891 892
	{},
};
893

894 895
static int rcar_pcie_probe(struct platform_device *pdev)
{
896
	struct device *dev = &pdev->dev;
897
	struct rcar_pcie_host *host;
898
	struct rcar_pcie *pcie;
899
	u32 data;
900
	int err;
901
	struct pci_host_bridge *bridge;
902

903
	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*host));
904
	if (!bridge)
905 906
		return -ENOMEM;

907 908
	host = pci_host_bridge_priv(bridge);
	pcie = &host->pcie;
909
	pcie->dev = dev;
910
	platform_set_drvdata(pdev, host);
911

912 913 914 915
	pm_runtime_enable(pcie->dev);
	err = pm_runtime_get_sync(pcie->dev);
	if (err < 0) {
		dev_err(pcie->dev, "pm_runtime_get_sync failed\n");
916
		goto err_pm_put;
917 918
	}

919
	err = rcar_pcie_get_resources(host);
920
	if (err < 0) {
921
		dev_err(dev, "failed to request resources: %d\n", err);
922
		goto err_pm_put;
923 924
	}

925
	err = clk_prepare_enable(host->bus_clk);
926 927
	if (err) {
		dev_err(dev, "failed to enable bus clock: %d\n", err);
928
		goto err_unmap_msi_irqs;
929 930
	}

931
	err = rcar_pcie_parse_map_dma_ranges(host);
932
	if (err)
933
		goto err_clk_disable;
934

935 936
	host->phy_init_fn = of_device_get_match_data(dev);
	err = host->phy_init_fn(host);
937
	if (err) {
938
		dev_err(dev, "failed to init PCIe PHY\n");
939
		goto err_clk_disable;
940 941
	}

942
	/* Failure to get a link might just be that no cards are inserted */
943
	if (rcar_pcie_hw_init(pcie)) {
944
		dev_info(dev, "PCIe link down\n");
945
		err = -ENODEV;
946
		goto err_phy_shutdown;
947 948
	}

949
	data = rcar_pci_read_reg(pcie, MACSR);
950
	dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);
951

952
	if (IS_ENABLED(CONFIG_PCI_MSI)) {
953
		err = rcar_pcie_enable_msi(host);
954
		if (err < 0) {
955
			dev_err(dev,
956 957
				"failed to enable MSI support: %d\n",
				err);
958
			goto err_phy_shutdown;
959 960 961
		}
	}

962
	err = rcar_pcie_enable(host);
963
	if (err)
964
		goto err_msi_teardown;
965 966 967

	return 0;

968 969
err_msi_teardown:
	if (IS_ENABLED(CONFIG_PCI_MSI))
970
		rcar_pcie_teardown_msi(host);
971

972
err_phy_shutdown:
973 974 975
	if (host->phy) {
		phy_power_off(host->phy);
		phy_exit(host->phy);
976 977
	}

978
err_clk_disable:
979
	clk_disable_unprepare(host->bus_clk);
980

981
err_unmap_msi_irqs:
982 983
	irq_dispose_mapping(host->msi.irq2);
	irq_dispose_mapping(host->msi.irq1);
984

985
err_pm_put:
986 987
	pm_runtime_put(dev);
	pm_runtime_disable(dev);
988

989
	return err;
990 991
}

K
Kazufumi Ikeda 已提交
992 993
static int __maybe_unused rcar_pcie_resume(struct device *dev)
{
994 995
	struct rcar_pcie_host *host = dev_get_drvdata(dev);
	struct rcar_pcie *pcie = &host->pcie;
K
Kazufumi Ikeda 已提交
996 997 998
	unsigned int data;
	int err;

999
	err = rcar_pcie_parse_map_dma_ranges(host);
K
Kazufumi Ikeda 已提交
1000 1001 1002 1003
	if (err)
		return 0;

	/* Failure to get a link might just be that no cards are inserted */
1004
	err = host->phy_init_fn(host);
K
Kazufumi Ikeda 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	if (err) {
		dev_info(dev, "PCIe link down\n");
		return 0;
	}

	data = rcar_pci_read_reg(pcie, MACSR);
	dev_info(dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f);

	/* Enable MSI */
	if (IS_ENABLED(CONFIG_PCI_MSI))
1015
		rcar_pcie_hw_enable_msi(host);
K
Kazufumi Ikeda 已提交
1016

1017
	rcar_pcie_hw_enable(host);
K
Kazufumi Ikeda 已提交
1018 1019 1020 1021

	return 0;
}

1022 1023
static int rcar_pcie_resume_noirq(struct device *dev)
{
1024 1025
	struct rcar_pcie_host *host = dev_get_drvdata(dev);
	struct rcar_pcie *pcie = &host->pcie;
1026 1027 1028 1029 1030 1031

	if (rcar_pci_read_reg(pcie, PMSR) &&
	    !(rcar_pci_read_reg(pcie, PCIETCTLR) & DL_DOWN))
		return 0;

	/* Re-establish the PCIe link */
1032
	rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1033 1034 1035 1036 1037
	rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
	return rcar_pcie_wait_for_dl(pcie);
}

static const struct dev_pm_ops rcar_pcie_pm_ops = {
K
Kazufumi Ikeda 已提交
1038
	SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
1039 1040 1041
	.resume_noirq = rcar_pcie_resume_noirq,
};

1042 1043
static struct platform_driver rcar_pcie_driver = {
	.driver = {
B
Bjorn Helgaas 已提交
1044
		.name = "rcar-pcie",
1045
		.of_match_table = rcar_pcie_of_match,
1046
		.pm = &rcar_pcie_pm_ops,
1047 1048 1049 1050
		.suppress_bind_attrs = true,
	},
	.probe = rcar_pcie_probe,
};
1051
builtin_platform_driver(rcar_pcie_driver);