pcie-rcar-host.c 26.9 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 device		*dev;
54
	struct phy		*phy;
55
	void __iomem		*base;
56
	struct list_head	resources;
57
	struct clk		*bus_clk;
58
	struct			rcar_msi msi;
59
	int			(*phy_init_fn)(struct rcar_pcie_host *host);
60 61 62 63
};

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

	return val >> shift;
}

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

	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;

102
		if (access_type == RCAR_PCI_ACCESS_READ)
103
			*data = rcar_pci_read_reg(pcie, PCICONF(index));
104
		else
105
			rcar_pci_write_reg(pcie, *data, PCICONF(index));
106 107 108 109 110

		return PCIBIOS_SUCCESSFUL;
	}

	/* Clear errors */
111
	rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
112 113

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

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

	/* Check for errors */
124
	if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
125 126 127 128 129 130 131
		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;

132 133
	if (access_type == RCAR_PCI_ACCESS_READ)
		*data = rcar_pci_read_reg(pcie, PCIECDR);
134
	else
135
		rcar_pci_write_reg(pcie, *data, PCIECDR);
136 137

	/* Disable the configuration access */
138
	rcar_pci_write_reg(pcie, 0, PCIECCTLR);
139 140 141 142 143 144 145

	return PCIBIOS_SUCCESSFUL;
}

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

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

	if (size == 1)
157
		*val = (*val >> (BITS_PER_BYTE * (where & 3))) & 0xff;
158
	else if (size == 2)
159
		*val = (*val >> (BITS_PER_BYTE * (where & 2))) & 0xffff;
160

M
Marek Vasut 已提交
161 162
	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);
163 164 165 166 167 168 169 170

	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)
{
171
	struct rcar_pcie_host *host = bus->sysdata;
172
	unsigned int shift;
173
	u32 data;
174
	int ret;
175

176
	ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_READ,
177 178 179 180
				      bus, devfn, where, &data);
	if (ret != PCIBIOS_SUCCESSFUL)
		return ret;

M
Marek Vasut 已提交
181 182
	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);
183 184

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

195
	ret = rcar_pcie_config_access(host, RCAR_PCI_ACCESS_WRITE,
196 197 198 199 200 201 202 203 204 205
				      bus, devfn, where, &data);

	return ret;
}

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

206 207
static int rcar_pcie_setup(struct list_head *resource,
			   struct rcar_pcie_host *host)
208
{
209
	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
210 211
	struct resource_entry *win;
	int i = 0;
212 213

	/* Setup PCI resources */
214
	resource_list_for_each_entry(win, &host->resources) {
215
		struct resource *res = win->res;
216 217 218 219

		if (!res->flags)
			continue;

220 221 222
		switch (resource_type(res)) {
		case IORESOURCE_IO:
		case IORESOURCE_MEM:
223
			rcar_pcie_set_outbound(&host->pcie, i, win);
224 225 226 227 228 229
			i++;
			break;
		case IORESOURCE_BUS:
			break;
		default:
			continue;
230 231
		}

232
		pci_add_resource(resource, res);
233 234 235 236 237
	}

	return 1;
}

238 239
static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
{
240
	struct device *dev = pcie->dev;
241 242 243 244 245 246 247
	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) {
248
		dev_err(dev, "Speed change already in progress\n");
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
		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)
277
				dev_err(dev, "Speed change failed\n");
278 279 280 281 282

			goto done;
		}

		msleep(1);
283
	}
284

285
	dev_err(dev, "Speed change timed out\n");
286 287

done:
288
	dev_info(dev, "Current link speed is %s GT/s\n",
289 290 291
		 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
}

292
static void rcar_pcie_hw_enable(struct rcar_pcie_host *host)
K
Kazufumi Ikeda 已提交
293
{
294
	struct rcar_pcie *pcie = &host->pcie;
K
Kazufumi Ikeda 已提交
295 296 297 298 299
	struct resource_entry *win;
	LIST_HEAD(res);
	int i = 0;

	/* Try setting 5 GT/s link speed */
300
	rcar_pcie_force_speedup(pcie);
K
Kazufumi Ikeda 已提交
301 302

	/* Setup PCI resources */
303
	resource_list_for_each_entry(win, &host->resources) {
K
Kazufumi Ikeda 已提交
304 305 306 307 308 309 310 311
		struct resource *res = win->res;

		if (!res->flags)
			continue;

		switch (resource_type(res)) {
		case IORESOURCE_IO:
		case IORESOURCE_MEM:
312
			rcar_pcie_set_outbound(pcie, i, win);
K
Kazufumi Ikeda 已提交
313 314 315 316 317 318
			i++;
			break;
		}
	}
}

319
static int rcar_pcie_enable(struct rcar_pcie_host *host)
320
{
321 322
	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
	struct rcar_pcie *pcie = &host->pcie;
323

324 325 326
	/* Try setting 5 GT/s link speed */
	rcar_pcie_force_speedup(pcie);

327
	rcar_pcie_setup(&bridge->windows, host);
328

329
	pci_add_flags(PCI_REASSIGN_ALL_BUS);
330

331
	bridge->sysdata = host;
332
	bridge->ops = &rcar_pcie_ops;
333 334
	bridge->map_irq = of_irq_parse_and_map_pci;
	bridge->swizzle_irq = pci_common_swizzle;
335
	if (IS_ENABLED(CONFIG_PCI_MSI))
336
		bridge->msi = &host->msi.chip;
337

338
	return pci_host_probe(bridge);
339 340 341 342
}

static int phy_wait_for_ack(struct rcar_pcie *pcie)
{
343
	struct device *dev = pcie->dev;
344 345 346
	unsigned int timeout = 100;

	while (timeout--) {
347
		if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
348 349 350 351 352
			return 0;

		udelay(100);
	}

353
	dev_err(dev, "Access to PCIe phy timed out\n");
354 355 356 357 358

	return -ETIMEDOUT;
}

static void phy_write_reg(struct rcar_pcie *pcie,
359 360
			  unsigned int rate, u32 addr,
			  unsigned int lane, u32 data)
361
{
362
	u32 phyaddr;
363 364 365 366 367 368 369

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

	/* Set write data */
370 371
	rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
	rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
372 373 374 375 376

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

	/* Clear command */
377 378
	rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
	rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
379 380 381 382 383 384 385 386 387 388

	/* 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 */
389
	rcar_pci_write_reg(pcie, 0, PCIETCTLR);
390 391

	/* Set mode */
392
	rcar_pci_write_reg(pcie, 1, PCIEMSR);
393

394 395 396 397
	err = rcar_pcie_wait_for_phyrdy(pcie);
	if (err)
		return err;

398 399 400 401 402
	/*
	 * 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.
	 */
403
	rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
404 405 406 407 408 409 410 411 412

	/*
	 * 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. */
413
	rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
414 415 416 417 418 419
	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 */
420 421
	rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
		PCI_EXP_LNKCAP_DLLLARC);
422 423 424 425 426

	/* 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. */
427
	rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
428 429

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

432 433
	/* Enable MSI */
	if (IS_ENABLED(CONFIG_PCI_MSI))
434
		rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
435

436 437
	rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);

438
	/* Finish initialization - establish a PCI Express link */
439
	rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
440 441 442 443 444 445 446 447 448 449 450 451 452 453

	/* 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;
}

454
static int rcar_pcie_phy_init_h1(struct rcar_pcie_host *host)
455
{
456 457
	struct rcar_pcie *pcie = &host->pcie;

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	/* 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);

476
	return 0;
477 478
}

479
static int rcar_pcie_phy_init_gen2(struct rcar_pcie_host *host)
480
{
481 482
	struct rcar_pcie *pcie = &host->pcie;

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	/*
	 * 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);

498
	return 0;
499 500
}

501
static int rcar_pcie_phy_init_gen3(struct rcar_pcie_host *host)
502 503 504
{
	int err;

505
	err = phy_init(host->phy);
506 507 508
	if (err)
		return err;

509
	err = phy_power_on(host->phy);
510
	if (err)
511
		phy_exit(host->phy);
512 513

	return err;
514 515
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
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;
}

533 534 535 536 537 538 539 540 541 542 543 544
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;
}

545 546 547 548 549 550 551 552 553
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)
{
554 555 556
	struct rcar_pcie_host *host = data;
	struct rcar_pcie *pcie = &host->pcie;
	struct rcar_msi *msi = &host->msi;
557
	struct device *dev = pcie->dev;
558 559
	unsigned long reg;

560
	reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
561 562 563 564 565 566 567

	/* 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);
568
		unsigned int msi_irq;
569 570

		/* clear the interrupt */
571
		rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
572

573 574
		msi_irq = irq_find_mapping(msi->domain, index);
		if (msi_irq) {
575
			if (test_bit(index, msi->used))
576
				generic_handle_irq(msi_irq);
577
			else
578
				dev_info(dev, "unhandled MSI\n");
579 580
		} else {
			/* Unknown MSI, just clear it */
581
			dev_dbg(dev, "unexpected MSI\n");
582 583 584
		}

		/* see if there's any more pending in this vector */
585
		reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
586 587 588 589 590
	}

	return IRQ_HANDLED;
}

591
static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
592 593 594
			      struct msi_desc *desc)
{
	struct rcar_msi *msi = to_rcar_msi(chip);
595 596 597
	struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
						   msi.chip);
	struct rcar_pcie *pcie = &host->pcie;
598 599 600 601 602 603 604 605
	struct msi_msg msg;
	unsigned int irq;
	int hwirq;

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

606
	irq = irq_find_mapping(msi->domain, hwirq);
607 608 609 610 611 612 613
	if (!irq) {
		rcar_msi_free(msi, hwirq);
		return -EINVAL;
	}

	irq_set_msi_desc(irq, desc);

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

618
	pci_write_msi_msg(irq, &msg);
619 620 621 622

	return 0;
}

623 624 625 626
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);
627 628 629
	struct rcar_pcie_host *host = container_of(chip, struct rcar_pcie_host,
						   msi.chip);
	struct rcar_pcie *pcie = &host->pcie;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
	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;
}

677
static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
678 679 680 681 682 683 684 685 686
{
	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",
687 688 689 690
	.irq_enable = pci_msi_unmask_irq,
	.irq_disable = pci_msi_mask_irq,
	.irq_mask = pci_msi_mask_irq,
	.irq_unmask = pci_msi_unmask_irq,
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
};

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,
};

706
static void rcar_pcie_unmap_msi(struct rcar_pcie_host *host)
707
{
708
	struct rcar_msi *msi = &host->msi;
709 710 711 712 713 714 715 716 717 718 719
	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);
}

720
static void rcar_pcie_hw_enable_msi(struct rcar_pcie_host *host)
K
Kazufumi Ikeda 已提交
721
{
722 723
	struct rcar_pcie *pcie = &host->pcie;
	struct rcar_msi *msi = &host->msi;
K
Kazufumi Ikeda 已提交
724 725 726 727 728 729 730 731 732 733 734 735
	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);
}

736
static int rcar_pcie_enable_msi(struct rcar_pcie_host *host)
737
{
738
	struct rcar_pcie *pcie = &host->pcie;
739
	struct device *dev = pcie->dev;
740
	struct rcar_msi *msi = &host->msi;
741
	int err, i;
742 743 744

	mutex_init(&msi->lock);

745
	msi->chip.dev = dev;
746
	msi->chip.setup_irq = rcar_msi_setup_irq;
747
	msi->chip.setup_irqs = rcar_msi_setup_irqs;
748 749
	msi->chip.teardown_irq = rcar_msi_teardown_irq;

750
	msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
751 752
					    &msi_domain_ops, &msi->chip);
	if (!msi->domain) {
753
		dev_err(dev, "failed to create IRQ domain\n");
754 755 756
		return -ENOMEM;
	}

757 758 759
	for (i = 0; i < INT_PCI_MSI_NR; i++)
		irq_create_mapping(msi->domain, i);

760
	/* Two irqs are for MSI, but they are also used for non-MSI irqs */
761
	err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
762
			       IRQF_SHARED | IRQF_NO_THREAD,
763
			       rcar_msi_irq_chip.name, host);
764
	if (err < 0) {
765
		dev_err(dev, "failed to request IRQ: %d\n", err);
766 767 768
		goto err;
	}

769
	err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
770
			       IRQF_SHARED | IRQF_NO_THREAD,
771
			       rcar_msi_irq_chip.name, host);
772
	if (err < 0) {
773
		dev_err(dev, "failed to request IRQ: %d\n", err);
774 775 776 777 778
		goto err;
	}

	/* setup MSI data target */
	msi->pages = __get_free_pages(GFP_KERNEL, 0);
779
	rcar_pcie_hw_enable_msi(host);
780 781 782 783

	return 0;

err:
784
	rcar_pcie_unmap_msi(host);
785 786 787
	return err;
}

788
static void rcar_pcie_teardown_msi(struct rcar_pcie_host *host)
789
{
790 791
	struct rcar_pcie *pcie = &host->pcie;
	struct rcar_msi *msi = &host->msi;
792 793 794 795 796 797 798 799 800

	/* 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);

801
	rcar_pcie_unmap_msi(host);
802 803
}

804
static int rcar_pcie_get_resources(struct rcar_pcie_host *host)
805
{
806
	struct rcar_pcie *pcie = &host->pcie;
807
	struct device *dev = pcie->dev;
808
	struct resource res;
809
	int err, i;
810

811 812 813
	host->phy = devm_phy_optional_get(dev, "pcie");
	if (IS_ERR(host->phy))
		return PTR_ERR(host->phy);
814

815
	err = of_address_to_resource(dev->of_node, 0, &res);
816 817 818
	if (err)
		return err;

819
	pcie->base = devm_ioremap_resource(dev, &res);
820 821 822
	if (IS_ERR(pcie->base))
		return PTR_ERR(pcie->base);

823 824
	host->bus_clk = devm_clk_get(dev, "pcie_bus");
	if (IS_ERR(host->bus_clk)) {
825
		dev_err(dev, "cannot get pcie bus clock\n");
826
		return PTR_ERR(host->bus_clk);
827 828
	}

829
	i = irq_of_parse_and_map(dev->of_node, 0);
830
	if (!i) {
831
		dev_err(dev, "cannot get platform resources for msi interrupt\n");
832
		err = -ENOENT;
833
		goto err_irq1;
834
	}
835
	host->msi.irq1 = i;
836

837
	i = irq_of_parse_and_map(dev->of_node, 1);
838
	if (!i) {
839
		dev_err(dev, "cannot get platform resources for msi interrupt\n");
840
		err = -ENOENT;
841
		goto err_irq2;
842
	}
843
	host->msi.irq2 = i;
844

845 846
	return 0;

847
err_irq2:
848
	irq_dispose_mapping(host->msi.irq1);
849
err_irq1:
850 851 852 853
	return err;
}

static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
854
				    struct resource_entry *entry,
855 856
				    int *index)
{
857 858 859 860
	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;
861 862
	u32 flags = LAM_64BIT | LAR_ENABLE;
	u64 mask;
863
	u64 size = resource_size(entry->res);
864 865 866 867 868 869
	int idx = *index;

	if (restype & IORESOURCE_PREFETCH)
		flags |= LAM_PREFETCH;

	while (cpu_addr < cpu_end) {
870 871 872 873
		if (idx >= MAX_NR_INBOUND_MAPS - 1) {
			dev_err(pcie->dev, "Failed to map inbound regions!\n");
			return -EINVAL;
		}
874 875 876 877 878 879 880 881 882
		/*
		 * 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;

883
			size = min(size, alignment);
884 885 886 887 888 889 890
		}
		/* Hardware supports max 4GiB inbound region */
		size = min(size, 1ULL << 32);

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

891 892
		rcar_pcie_set_inbound(pcie, cpu_addr, pci_addr,
				      lower_32_bits(mask) | flags, idx, true);
893 894 895 896 897 898 899 900 901 902

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

	return 0;
}

903
static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie_host *host)
904
{
905
	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(host);
906 907
	struct resource_entry *entry;
	int index = 0, err = 0;
908

909
	resource_list_for_each_entry(entry, &bridge->dma_ranges) {
910
		err = rcar_pcie_inbound_ranges(&host->pcie, entry, &index);
911
		if (err)
912
			break;
913 914
	}

915
	return err;
916 917 918
}

static const struct of_device_id rcar_pcie_of_match[] = {
919 920
	{ .compatible = "renesas,pcie-r8a7779",
	  .data = rcar_pcie_phy_init_h1 },
921
	{ .compatible = "renesas,pcie-r8a7790",
922
	  .data = rcar_pcie_phy_init_gen2 },
923
	{ .compatible = "renesas,pcie-r8a7791",
924
	  .data = rcar_pcie_phy_init_gen2 },
925
	{ .compatible = "renesas,pcie-rcar-gen2",
926
	  .data = rcar_pcie_phy_init_gen2 },
927
	{ .compatible = "renesas,pcie-r8a7795",
928
	  .data = rcar_pcie_phy_init_gen3 },
929
	{ .compatible = "renesas,pcie-rcar-gen3",
930
	  .data = rcar_pcie_phy_init_gen3 },
931 932
	{},
};
933

934 935
static int rcar_pcie_probe(struct platform_device *pdev)
{
936
	struct device *dev = &pdev->dev;
937
	struct rcar_pcie_host *host;
938
	struct rcar_pcie *pcie;
939
	u32 data;
940
	int err;
941
	struct pci_host_bridge *bridge;
942

943
	bridge = pci_alloc_host_bridge(sizeof(*host));
944
	if (!bridge)
945 946
		return -ENOMEM;

947 948
	host = pci_host_bridge_priv(bridge);
	pcie = &host->pcie;
949
	pcie->dev = dev;
950
	platform_set_drvdata(pdev, host);
951

952
	err = pci_parse_request_of_pci_ranges(dev, &host->resources,
953
					      &bridge->dma_ranges, NULL);
954 955
	if (err)
		goto err_free_bridge;
956

957 958 959 960 961 962 963
	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");
		goto err_pm_disable;
	}

964
	err = rcar_pcie_get_resources(host);
965
	if (err < 0) {
966
		dev_err(dev, "failed to request resources: %d\n", err);
967
		goto err_pm_put;
968 969
	}

970
	err = clk_prepare_enable(host->bus_clk);
971 972
	if (err) {
		dev_err(dev, "failed to enable bus clock: %d\n", err);
973
		goto err_unmap_msi_irqs;
974 975
	}

976
	err = rcar_pcie_parse_map_dma_ranges(host);
977
	if (err)
978
		goto err_clk_disable;
979

980 981
	host->phy_init_fn = of_device_get_match_data(dev);
	err = host->phy_init_fn(host);
982
	if (err) {
983
		dev_err(dev, "failed to init PCIe PHY\n");
984
		goto err_clk_disable;
985 986
	}

987
	/* Failure to get a link might just be that no cards are inserted */
988
	if (rcar_pcie_hw_init(pcie)) {
989
		dev_info(dev, "PCIe link down\n");
990
		err = -ENODEV;
991
		goto err_phy_shutdown;
992 993
	}

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

997
	if (IS_ENABLED(CONFIG_PCI_MSI)) {
998
		err = rcar_pcie_enable_msi(host);
999
		if (err < 0) {
1000
			dev_err(dev,
1001 1002
				"failed to enable MSI support: %d\n",
				err);
1003
			goto err_phy_shutdown;
1004 1005 1006
		}
	}

1007
	err = rcar_pcie_enable(host);
1008
	if (err)
1009
		goto err_msi_teardown;
1010 1011 1012

	return 0;

1013 1014
err_msi_teardown:
	if (IS_ENABLED(CONFIG_PCI_MSI))
1015
		rcar_pcie_teardown_msi(host);
1016

1017
err_phy_shutdown:
1018 1019 1020
	if (host->phy) {
		phy_power_off(host->phy);
		phy_exit(host->phy);
1021 1022
	}

1023
err_clk_disable:
1024
	clk_disable_unprepare(host->bus_clk);
1025

1026
err_unmap_msi_irqs:
1027 1028
	irq_dispose_mapping(host->msi.irq2);
	irq_dispose_mapping(host->msi.irq1);
1029

1030
err_pm_put:
1031
	pm_runtime_put(dev);
1032 1033

err_pm_disable:
1034
	pm_runtime_disable(dev);
1035
	pci_free_resource_list(&host->resources);
1036

1037
err_free_bridge:
1038
	pci_free_host_bridge(bridge);
1039

1040
	return err;
1041 1042
}

K
Kazufumi Ikeda 已提交
1043 1044
static int __maybe_unused rcar_pcie_resume(struct device *dev)
{
1045 1046
	struct rcar_pcie_host *host = dev_get_drvdata(dev);
	struct rcar_pcie *pcie = &host->pcie;
K
Kazufumi Ikeda 已提交
1047 1048 1049
	unsigned int data;
	int err;

1050
	err = rcar_pcie_parse_map_dma_ranges(host);
K
Kazufumi Ikeda 已提交
1051 1052 1053 1054
	if (err)
		return 0;

	/* Failure to get a link might just be that no cards are inserted */
1055
	err = host->phy_init_fn(host);
K
Kazufumi Ikeda 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	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))
1066
		rcar_pcie_hw_enable_msi(host);
K
Kazufumi Ikeda 已提交
1067

1068
	rcar_pcie_hw_enable(host);
K
Kazufumi Ikeda 已提交
1069 1070 1071 1072

	return 0;
}

1073 1074
static int rcar_pcie_resume_noirq(struct device *dev)
{
1075 1076
	struct rcar_pcie_host *host = dev_get_drvdata(dev);
	struct rcar_pcie *pcie = &host->pcie;
1077 1078 1079 1080 1081 1082

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

	/* Re-establish the PCIe link */
1083
	rcar_pci_write_reg(pcie, MACCTLR_INIT_VAL, MACCTLR);
1084 1085 1086 1087 1088
	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 已提交
1089
	SET_SYSTEM_SLEEP_PM_OPS(NULL, rcar_pcie_resume)
1090 1091 1092
	.resume_noirq = rcar_pcie_resume_noirq,
};

1093 1094
static struct platform_driver rcar_pcie_driver = {
	.driver = {
B
Bjorn Helgaas 已提交
1095
		.name = "rcar-pcie",
1096
		.of_match_table = rcar_pcie_of_match,
1097
		.pm = &rcar_pcie_pm_ops,
1098 1099 1100 1101
		.suppress_bind_attrs = true,
	},
	.probe = rcar_pcie_probe,
};
1102
builtin_platform_driver(rcar_pcie_driver);