Blame view

drivers/pci/host/pcie-rcar.c 29.6 KB
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1
2
3
4
5
6
7
8
9
  /*
   * PCIe driver for Renesas R-Car SoCs
   *  Copyright (C) 2014 Renesas Electronics Europe Ltd
   *
   * Based on:
   *  arch/sh/drivers/pci/pcie-sh7786.c
   *  arch/sh/drivers/pci/ops-sh7786.c
   *  Copyright (C) 2009 - 2011  Paul Mundt
   *
42d107198   Paul Gortmaker   PCI: rcar: Make e...
10
11
   * Author: Phil Edworthy <phil.edworthy@renesas.com>
   *
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
12
13
14
15
16
17
18
19
   * This file is licensed under the terms of the GNU General Public
   * License version 2.  This program is licensed "as is" without any
   * warranty of any kind, whether express or implied.
   */
  
  #include <linux/clk.h>
  #include <linux/delay.h>
  #include <linux/interrupt.h>
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
20
21
  #include <linux/irq.h>
  #include <linux/irqdomain.h>
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
22
  #include <linux/kernel.h>
42d107198   Paul Gortmaker   PCI: rcar: Make e...
23
  #include <linux/init.h>
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
24
  #include <linux/msi.h>
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
25
26
27
28
29
30
  #include <linux/of_address.h>
  #include <linux/of_irq.h>
  #include <linux/of_pci.h>
  #include <linux/of_platform.h>
  #include <linux/pci.h>
  #include <linux/platform_device.h>
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
31
  #include <linux/pm_runtime.h>
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
32
  #include <linux/slab.h>
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
33
34
35
36
37
38
39
40
  #define PCIECAR			0x000010
  #define PCIECCTLR		0x000018
  #define  CONFIG_SEND_ENABLE	(1 << 31)
  #define  TYPE0			(0 << 8)
  #define  TYPE1			(1 << 8)
  #define PCIECDR			0x000020
  #define PCIEMSR			0x000028
  #define PCIEINTXR		0x000400
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
41
  #define PCIEMSITXR		0x000840
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
42
43
44
45
46
47
48
49
  
  /* Transfer control */
  #define PCIETCTLR		0x02000
  #define  CFINIT			1
  #define PCIETSTR		0x02004
  #define  DATA_LINK_ACTIVE	1
  #define PCIEERRFR		0x02020
  #define  UNSUPPORTED_REQUEST	(1 << 4)
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
50
51
52
53
54
  #define PCIEMSIFR		0x02044
  #define PCIEMSIALR		0x02048
  #define  MSIFE			1
  #define PCIEMSIAUR		0x0204c
  #define PCIEMSIIER		0x02050
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
55
56
57
58
59
60
61
62
63
64
65
66
  
  /* root port address */
  #define PCIEPRAR(x)		(0x02080 + ((x) * 0x4))
  
  /* local address reg & mask */
  #define PCIELAR(x)		(0x02200 + ((x) * 0x20))
  #define PCIELAMR(x)		(0x02208 + ((x) * 0x20))
  #define  LAM_PREFETCH		(1 << 3)
  #define  LAM_64BIT		(1 << 2)
  #define  LAR_ENABLE		(1 << 1)
  
  /* PCIe address reg & mask */
ecd06305c   Nobuhiro Iwamatsu   PCI: rcar: Change...
67
68
  #define PCIEPALR(x)		(0x03400 + ((x) * 0x20))
  #define PCIEPAUR(x)		(0x03404 + ((x) * 0x20))
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #define PCIEPAMR(x)		(0x03408 + ((x) * 0x20))
  #define PCIEPTCTLR(x)		(0x0340c + ((x) * 0x20))
  #define  PAR_ENABLE		(1 << 31)
  #define  IO_SPACE		(1 << 8)
  
  /* Configuration */
  #define PCICONF(x)		(0x010000 + ((x) * 0x4))
  #define PMCAP(x)		(0x010040 + ((x) * 0x4))
  #define EXPCAP(x)		(0x010070 + ((x) * 0x4))
  #define VCCAP(x)		(0x010100 + ((x) * 0x4))
  
  /* link layer */
  #define IDSETR1			0x011004
  #define TLCTLR			0x011048
  #define MACSR			0x011054
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
84
85
86
87
88
89
  #define  SPCHGFIN		(1 << 4)
  #define  SPCHGFAIL		(1 << 6)
  #define  SPCHGSUC		(1 << 7)
  #define  LINK_SPEED		(0xf << 16)
  #define  LINK_SPEED_2_5GTS	(1 << 16)
  #define  LINK_SPEED_5_0GTS	(2 << 16)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
90
  #define MACCTLR			0x011058
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
91
  #define  SPEED_CHANGE		(1 << 24)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
92
  #define  SCRAMBLE_DISABLE	(1 << 27)
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
93
94
95
  #define MACS2R			0x011078
  #define MACCGSPSETR		0x011084
  #define  SPCNGRSN		(1 << 31)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
96
97
98
99
100
101
102
103
104
105
  
  /* R-Car H1 PHY */
  #define H1_PCIEPHYADRR		0x04000c
  #define  WRITE_CMD		(1 << 16)
  #define  PHY_ACK		(1 << 24)
  #define  RATE_POS		12
  #define  LANE_POS		8
  #define  ADR_POS		0
  #define H1_PCIEPHYDOUTR		0x040014
  #define H1_PCIEPHYSR		0x040018
581d9434a   Phil Edworthy   PCI: rcar: Add Ge...
106
107
108
109
  /* R-Car Gen2 PHY */
  #define GEN2_PCIEPHYADDR	0x780
  #define GEN2_PCIEPHYDATA	0x784
  #define GEN2_PCIEPHYCTRL	0x78c
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
110
  #define INT_PCI_MSI_NR	32
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
111
112
113
114
115
116
117
118
  #define RCONF(x)	(PCICONF(0)+(x))
  #define RPMCAP(x)	(PMCAP(0)+(x))
  #define REXPCAP(x)	(EXPCAP(0)+(x))
  #define RVCCAP(x)	(VCCAP(0)+(x))
  
  #define  PCIE_CONF_BUS(b)	(((b) & 0xff) << 24)
  #define  PCIE_CONF_DEV(d)	(((d) & 0x1f) << 19)
  #define  PCIE_CONF_FUNC(f)	(((f) & 0x7) << 16)
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
119
  #define RCAR_PCI_MAX_RESOURCES 4
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
120
  #define MAX_NR_INBOUND_MAPS 6
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
121
122
123
  struct rcar_msi {
  	DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  	struct irq_domain *domain;
c2791b806   Yijing Wang   PCI/MSI: Rename "...
124
  	struct msi_controller chip;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
125
126
127
128
129
  	unsigned long pages;
  	struct mutex lock;
  	int irq1;
  	int irq2;
  };
c2791b806   Yijing Wang   PCI/MSI: Rename "...
130
  static inline struct rcar_msi *to_rcar_msi(struct msi_controller *chip)
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
131
132
133
  {
  	return container_of(chip, struct rcar_msi, chip);
  }
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
134
135
136
137
  /* Structure representing the PCIe interface */
  struct rcar_pcie {
  	struct device		*dev;
  	void __iomem		*base;
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
138
  	struct list_head	resources;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
139
140
141
  	int			root_bus_nr;
  	struct clk		*clk;
  	struct clk		*bus_clk;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
142
  	struct			rcar_msi msi;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
143
  };
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
144
145
  static void rcar_pci_write_reg(struct rcar_pcie *pcie, unsigned long val,
  			       unsigned long reg)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
146
147
148
  {
  	writel(val, pcie->base + reg);
  }
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
149
150
  static unsigned long rcar_pci_read_reg(struct rcar_pcie *pcie,
  				       unsigned long reg)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
151
152
153
154
155
  {
  	return readl(pcie->base + reg);
  }
  
  enum {
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
156
157
  	RCAR_PCI_ACCESS_READ,
  	RCAR_PCI_ACCESS_WRITE,
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
158
159
160
161
162
  };
  
  static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data)
  {
  	int shift = 8 * (where & 3);
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
163
  	u32 val = rcar_pci_read_reg(pcie, where & ~3);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
164
165
166
  
  	val &= ~(mask << shift);
  	val |= data << shift;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
167
  	rcar_pci_write_reg(pcie, val, where & ~3);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
168
169
170
171
172
  }
  
  static u32 rcar_read_conf(struct rcar_pcie *pcie, int where)
  {
  	int shift = 8 * (where & 3);
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
173
  	u32 val = rcar_pci_read_reg(pcie, where & ~3);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  
  	return val >> shift;
  }
  
  /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */
  static int rcar_pcie_config_access(struct rcar_pcie *pcie,
  		unsigned char access_type, struct pci_bus *bus,
  		unsigned int devfn, int where, u32 *data)
  {
  	int dev, func, reg, index;
  
  	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;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
208
209
  		if (access_type == RCAR_PCI_ACCESS_READ) {
  			*data = rcar_pci_read_reg(pcie, PCICONF(index));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
210
211
212
213
  		} else {
  			/* Keep an eye out for changes to the root bus number */
  			if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS))
  				pcie->root_bus_nr = *data & 0xff;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
214
  			rcar_pci_write_reg(pcie, *data, PCICONF(index));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
215
216
217
218
219
220
221
222
223
  		}
  
  		return PCIBIOS_SUCCESSFUL;
  	}
  
  	if (pcie->root_bus_nr < 0)
  		return PCIBIOS_DEVICE_NOT_FOUND;
  
  	/* Clear errors */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
224
  	rcar_pci_write_reg(pcie, rcar_pci_read_reg(pcie, PCIEERRFR), PCIEERRFR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
225
226
  
  	/* Set the PIO address */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
227
228
  	rcar_pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) |
  		PCIE_CONF_DEV(dev) | PCIE_CONF_FUNC(func) | reg, PCIECAR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
229
230
231
  
  	/* Enable the configuration access */
  	if (bus->parent->number == pcie->root_bus_nr)
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
232
  		rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
233
  	else
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
234
  		rcar_pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
235
236
  
  	/* Check for errors */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
237
  	if (rcar_pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
238
239
240
241
242
243
  		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;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
244
245
  	if (access_type == RCAR_PCI_ACCESS_READ)
  		*data = rcar_pci_read_reg(pcie, PCIECDR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
246
  	else
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
247
  		rcar_pci_write_reg(pcie, *data, PCIECDR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
248
249
  
  	/* Disable the configuration access */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
250
  	rcar_pci_write_reg(pcie, 0, PCIECCTLR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
251
252
253
254
255
256
257
  
  	return PCIBIOS_SUCCESSFUL;
  }
  
  static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  			       int where, int size, u32 *val)
  {
79953dd22   Phil Edworthy   PCI: rcar: Remove...
258
  	struct rcar_pcie *pcie = bus->sysdata;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
259
  	int ret;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
260
  	ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
261
262
263
264
265
266
267
268
269
270
  				      bus, devfn, where, val);
  	if (ret != PCIBIOS_SUCCESSFUL) {
  		*val = 0xffffffff;
  		return ret;
  	}
  
  	if (size == 1)
  		*val = (*val >> (8 * (where & 3))) & 0xff;
  	else if (size == 2)
  		*val = (*val >> (8 * (where & 2))) & 0xffff;
227f06470   Ryan Desfosses   PCI: Merge multi-...
271
272
273
  	dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx
  ",
  		bus->number, devfn, where, size, (unsigned long)*val);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
274
275
276
277
278
279
280
281
  
  	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)
  {
79953dd22   Phil Edworthy   PCI: rcar: Remove...
282
  	struct rcar_pcie *pcie = bus->sysdata;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
283
284
  	int shift, ret;
  	u32 data;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
285
  	ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_READ,
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
286
287
288
  				      bus, devfn, where, &data);
  	if (ret != PCIBIOS_SUCCESSFUL)
  		return ret;
227f06470   Ryan Desfosses   PCI: Merge multi-...
289
290
291
  	dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x where=0x%04x size=%d val=0x%08lx
  ",
  		bus->number, devfn, where, size, (unsigned long)val);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
292
293
294
295
296
297
298
299
300
301
302
  
  	if (size == 1) {
  		shift = 8 * (where & 3);
  		data &= ~(0xff << shift);
  		data |= ((val & 0xff) << shift);
  	} else if (size == 2) {
  		shift = 8 * (where & 2);
  		data &= ~(0xffff << shift);
  		data |= ((val & 0xffff) << shift);
  	} else
  		data = val;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
303
  	ret = rcar_pcie_config_access(pcie, RCAR_PCI_ACCESS_WRITE,
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
304
305
306
307
308
309
310
311
312
  				      bus, devfn, where, &data);
  
  	return ret;
  }
  
  static struct pci_ops rcar_pcie_ops = {
  	.read	= rcar_pcie_read_conf,
  	.write	= rcar_pcie_write_conf,
  };
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
313
314
  static void rcar_pcie_setup_window(int win, struct rcar_pcie *pcie,
  				   struct resource *res)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
315
316
317
  {
  	/* Setup PCIe address space mappings for each resource */
  	resource_size_t size;
0b0b0893d   Liviu Dudau   of/pci: Fix the c...
318
  	resource_size_t res_start;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
319
  	u32 mask;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
320
  	rcar_pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
321
322
323
324
325
326
327
  
  	/*
  	 * The PAMR mask is calculated in units of 128Bytes, which
  	 * keeps things pretty simple.
  	 */
  	size = resource_size(res);
  	mask = (roundup_pow_of_two(size) / SZ_128) - 1;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
328
  	rcar_pci_write_reg(pcie, mask << 7, PCIEPAMR(win));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
329

0b0b0893d   Liviu Dudau   of/pci: Fix the c...
330
331
332
333
  	if (res->flags & IORESOURCE_IO)
  		res_start = pci_pio_to_address(res->start);
  	else
  		res_start = res->start;
ecd06305c   Nobuhiro Iwamatsu   PCI: rcar: Change...
334
  	rcar_pci_write_reg(pcie, upper_32_bits(res_start), PCIEPAUR(win));
2ea2a2734   Nobuhiro Iwamatsu   PCI: rcar: Write ...
335
  	rcar_pci_write_reg(pcie, lower_32_bits(res_start) & ~0x7F,
ecd06305c   Nobuhiro Iwamatsu   PCI: rcar: Change...
336
  			   PCIEPALR(win));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
337
338
339
340
341
  
  	/* First resource is for IO */
  	mask = PAR_ENABLE;
  	if (res->flags & IORESOURCE_IO)
  		mask |= IO_SPACE;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
342
  	rcar_pci_write_reg(pcie, mask, PCIEPTCTLR(win));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
343
  }
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
344
  static int rcar_pcie_setup(struct list_head *resource, struct rcar_pcie *pci)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
345
  {
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
346
347
  	struct resource_entry *win;
  	int i = 0;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
348
349
  
  	/* Setup PCI resources */
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
350
351
  	resource_list_for_each_entry(win, &pci->resources) {
  		struct resource *res = win->res;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
352

c25da4778   Phil Edworthy   PCI: rcar: Add Re...
353
354
  		if (!res->flags)
  			continue;
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
355
356
357
358
359
360
361
362
363
364
365
  		switch (resource_type(res)) {
  		case IORESOURCE_IO:
  		case IORESOURCE_MEM:
  			rcar_pcie_setup_window(i, pci, res);
  			i++;
  			break;
  		case IORESOURCE_BUS:
  			pci->root_bus_nr = res->start;
  			break;
  		default:
  			continue;
d0c3f4dbd   Phil Edworthy   PCI: rcar: Make P...
366
  		}
79953dd22   Phil Edworthy   PCI: rcar: Remove...
367
  		pci_add_resource(resource, res);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
368
  	}
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
369
370
371
  
  	return 1;
  }
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
372
373
  static void rcar_pcie_force_speedup(struct rcar_pcie *pcie)
  {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
374
  	struct device *dev = pcie->dev;
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
375
376
377
378
379
380
381
  	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) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
382
383
  		dev_err(dev, "Speed change already in progress
  ");
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  		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)
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
412
413
  				dev_err(dev, "Speed change failed
  ");
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
414
415
416
417
418
419
  
  			goto done;
  		}
  
  		msleep(1);
  	};
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
420
421
  	dev_err(dev, "Speed change timed out
  ");
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
422
423
  
  done:
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
424
425
  	dev_info(dev, "Current link speed is %s GT/s
  ",
b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
426
427
  		 (macsr & LINK_SPEED) == LINK_SPEED_5_0GTS ? "5" : "2.5");
  }
79953dd22   Phil Edworthy   PCI: rcar: Remove...
428
  static int rcar_pcie_enable(struct rcar_pcie *pcie)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
429
  {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
430
  	struct device *dev = pcie->dev;
79953dd22   Phil Edworthy   PCI: rcar: Remove...
431
432
  	struct pci_bus *bus, *child;
  	LIST_HEAD(res);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
433

b3327f7fa   Sergei Shtylyov   PCI: rcar: Try in...
434
435
  	/* Try setting 5 GT/s link speed */
  	rcar_pcie_force_speedup(pcie);
8c53e8ed0   Phil Edworthy   PCI: rcar: Fix I/...
436
  	rcar_pcie_setup(&res, pcie);
79953dd22   Phil Edworthy   PCI: rcar: Remove...
437

3487c6565   Lorenzo Pieralisi   PCI: rcar: Remove...
438
  	pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
79953dd22   Phil Edworthy   PCI: rcar: Remove...
439
440
  
  	if (IS_ENABLED(CONFIG_PCI_MSI))
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
441
  		bus = pci_scan_root_bus_msi(dev, pcie->root_bus_nr,
79953dd22   Phil Edworthy   PCI: rcar: Remove...
442
443
  				&rcar_pcie_ops, pcie, &res, &pcie->msi.chip);
  	else
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
444
  		bus = pci_scan_root_bus(dev, pcie->root_bus_nr,
79953dd22   Phil Edworthy   PCI: rcar: Remove...
445
446
447
  				&rcar_pcie_ops, pcie, &res);
  
  	if (!bus) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
448
  		dev_err(dev, "Scanning rootbus failed");
79953dd22   Phil Edworthy   PCI: rcar: Remove...
449
450
451
452
  		return -ENODEV;
  	}
  
  	pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
3487c6565   Lorenzo Pieralisi   PCI: rcar: Remove...
453
454
  	pci_bus_size_bridges(bus);
  	pci_bus_assign_resources(bus);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
455

3487c6565   Lorenzo Pieralisi   PCI: rcar: Remove...
456
457
  	list_for_each_entry(child, &bus->children, node)
  		pcie_bus_configure_settings(child);
79953dd22   Phil Edworthy   PCI: rcar: Remove...
458
459
460
461
  
  	pci_bus_add_devices(bus);
  
  	return 0;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
462
463
464
465
  }
  
  static int phy_wait_for_ack(struct rcar_pcie *pcie)
  {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
466
  	struct device *dev = pcie->dev;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
467
468
469
  	unsigned int timeout = 100;
  
  	while (timeout--) {
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
470
  		if (rcar_pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
471
472
473
474
  			return 0;
  
  		udelay(100);
  	}
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
475
476
  	dev_err(dev, "Access to PCIe phy timed out
  ");
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  
  	return -ETIMEDOUT;
  }
  
  static void phy_write_reg(struct rcar_pcie *pcie,
  				 unsigned int rate, unsigned int addr,
  				 unsigned int lane, unsigned int data)
  {
  	unsigned long phyaddr;
  
  	phyaddr = WRITE_CMD |
  		((rate & 1) << RATE_POS) |
  		((lane & 0xf) << LANE_POS) |
  		((addr & 0xff) << ADR_POS);
  
  	/* Set write data */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
493
494
  	rcar_pci_write_reg(pcie, data, H1_PCIEPHYDOUTR);
  	rcar_pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
495
496
497
498
499
  
  	/* Ignore errors as they will be dealt with if the data link is down */
  	phy_wait_for_ack(pcie);
  
  	/* Clear command */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
500
501
  	rcar_pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR);
  	rcar_pci_write_reg(pcie, 0, H1_PCIEPHYADRR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
502
503
504
505
506
507
508
509
510
511
  
  	/* Ignore errors as they will be dealt with if the data link is down */
  	phy_wait_for_ack(pcie);
  }
  
  static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie)
  {
  	unsigned int timeout = 10;
  
  	while (timeout--) {
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
512
  		if ((rcar_pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE))
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
513
514
515
516
517
518
519
520
521
522
523
524
525
  			return 0;
  
  		msleep(5);
  	}
  
  	return -ETIMEDOUT;
  }
  
  static int rcar_pcie_hw_init(struct rcar_pcie *pcie)
  {
  	int err;
  
  	/* Begin initialization */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
526
  	rcar_pci_write_reg(pcie, 0, PCIETCTLR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
527
528
  
  	/* Set mode */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
529
  	rcar_pci_write_reg(pcie, 1, PCIEMSR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
530
531
532
533
534
535
  
  	/*
  	 * 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.
  	 */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
536
  	rcar_pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
537
538
539
540
541
542
543
544
545
  
  	/*
  	 * 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. */
2c3fd4c93   Phil Edworthy   PCI: rcar: Use co...
546
  	rcar_rmw32(pcie, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
547
548
549
550
551
552
  	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 */
2c3fd4c93   Phil Edworthy   PCI: rcar: Use co...
553
554
  	rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), PCI_EXP_LNKCAP_DLLLARC,
  		PCI_EXP_LNKCAP_DLLLARC);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
555
556
557
558
559
  
  	/* 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. */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
560
  	rcar_rmw32(pcie, TLCTLR + 1, 0x3f, 50);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
561
562
  
  	/* Terminate list of capabilities (Next Capability Offset=0) */
2c3fd4c93   Phil Edworthy   PCI: rcar: Use co...
563
  	rcar_rmw32(pcie, RVCCAP(0), 0xfff00000, 0);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
564

290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
565
566
  	/* Enable MSI */
  	if (IS_ENABLED(CONFIG_PCI_MSI))
1fc6aa96e   Nobuhiro Iwamatsu   PCI: rcar: Fix po...
567
  		rcar_pci_write_reg(pcie, 0x801f0000, PCIEMSITXR);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
568

c25da4778   Phil Edworthy   PCI: rcar: Add Re...
569
  	/* Finish initialization - establish a PCI Express link */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
570
  	rcar_pci_write_reg(pcie, CFINIT, PCIETCTLR);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
571
572
573
574
575
576
577
578
  
  	/* 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);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  	wmb();
  
  	return 0;
  }
  
  static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie)
  {
  	unsigned int timeout = 10;
  
  	/* 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);
  
  	while (timeout--) {
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
607
  		if (rcar_pci_read_reg(pcie, H1_PCIEPHYSR))
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
608
609
610
611
612
613
614
  			return rcar_pcie_hw_init(pcie);
  
  		msleep(5);
  	}
  
  	return -ETIMEDOUT;
  }
581d9434a   Phil Edworthy   PCI: rcar: Add Ge...
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
  static int rcar_pcie_hw_init_gen2(struct rcar_pcie *pcie)
  {
  	/*
  	 * 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);
  
  	return rcar_pcie_hw_init(pcie);
  }
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  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;
  }
e3123c20c   Grigory Kletsko   PCI: rcar: Add mu...
650
651
652
653
654
655
656
657
658
659
660
  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;
  }
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
661
662
663
664
665
666
667
668
669
670
671
  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)
  {
  	struct rcar_pcie *pcie = data;
  	struct rcar_msi *msi = &pcie->msi;
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
672
  	struct device *dev = pcie->dev;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
673
  	unsigned long reg;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
674
  	reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
675
676
677
678
679
680
681
682
683
684
  
  	/* 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);
  		unsigned int irq;
  
  		/* clear the interrupt */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
685
  		rcar_pci_write_reg(pcie, 1 << index, PCIEMSIFR);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
686
687
688
689
690
691
  
  		irq = irq_find_mapping(msi->domain, index);
  		if (irq) {
  			if (test_bit(index, msi->used))
  				generic_handle_irq(irq);
  			else
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
692
693
  				dev_info(dev, "unhandled MSI
  ");
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
694
695
  		} else {
  			/* Unknown MSI, just clear it */
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
696
697
  			dev_dbg(dev, "unexpected MSI
  ");
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
698
699
700
  		}
  
  		/* see if there's any more pending in this vector */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
701
  		reg = rcar_pci_read_reg(pcie, PCIEMSIFR);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
702
703
704
705
  	}
  
  	return IRQ_HANDLED;
  }
c2791b806   Yijing Wang   PCI/MSI: Rename "...
706
  static int rcar_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
707
708
709
710
711
712
713
714
715
716
717
  			      struct msi_desc *desc)
  {
  	struct rcar_msi *msi = to_rcar_msi(chip);
  	struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
  	struct msi_msg msg;
  	unsigned int irq;
  	int hwirq;
  
  	hwirq = rcar_msi_alloc(msi);
  	if (hwirq < 0)
  		return hwirq;
e3123c20c   Grigory Kletsko   PCI: rcar: Add mu...
718
  	irq = irq_find_mapping(msi->domain, hwirq);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
719
720
721
722
723
724
  	if (!irq) {
  		rcar_msi_free(msi, hwirq);
  		return -EINVAL;
  	}
  
  	irq_set_msi_desc(irq, desc);
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
725
726
  	msg.address_lo = rcar_pci_read_reg(pcie, PCIEMSIALR) & ~MSIFE;
  	msg.address_hi = rcar_pci_read_reg(pcie, PCIEMSIAUR);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
727
  	msg.data = hwirq;
83a18912b   Jiang Liu   PCI/MSI: Rename w...
728
  	pci_write_msi_msg(irq, &msg);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
729
730
731
  
  	return 0;
  }
e3123c20c   Grigory Kletsko   PCI: rcar: Add mu...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
  static int rcar_msi_setup_irqs(struct msi_controller *chip,
  			       struct pci_dev *pdev, int nvec, int type)
  {
  	struct rcar_pcie *pcie = container_of(chip, struct rcar_pcie, msi.chip);
  	struct rcar_msi *msi = to_rcar_msi(chip);
  	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;
  }
c2791b806   Yijing Wang   PCI/MSI: Rename "...
783
  static void rcar_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
784
785
786
787
788
789
790
791
792
  {
  	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",
280510f10   Thomas Gleixner   PCI/MSI: Rename m...
793
794
795
796
  	.irq_enable = pci_msi_unmask_irq,
  	.irq_disable = pci_msi_mask_irq,
  	.irq_mask = pci_msi_mask_irq,
  	.irq_unmask = pci_msi_unmask_irq,
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
797
798
799
800
801
802
803
  };
  
  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);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
804
805
806
807
808
809
810
811
812
813
  
  	return 0;
  }
  
  static const struct irq_domain_ops msi_domain_ops = {
  	.map = rcar_msi_map,
  };
  
  static int rcar_pcie_enable_msi(struct rcar_pcie *pcie)
  {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
814
  	struct device *dev = pcie->dev;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
815
816
  	struct rcar_msi *msi = &pcie->msi;
  	unsigned long base;
e3123c20c   Grigory Kletsko   PCI: rcar: Add mu...
817
  	int err, i;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
818
819
  
  	mutex_init(&msi->lock);
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
820
  	msi->chip.dev = dev;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
821
  	msi->chip.setup_irq = rcar_msi_setup_irq;
e3123c20c   Grigory Kletsko   PCI: rcar: Add mu...
822
  	msi->chip.setup_irqs = rcar_msi_setup_irqs;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
823
  	msi->chip.teardown_irq = rcar_msi_teardown_irq;
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
824
  	msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
825
826
  					    &msi_domain_ops, &msi->chip);
  	if (!msi->domain) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
827
828
  		dev_err(dev, "failed to create IRQ domain
  ");
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
829
830
  		return -ENOMEM;
  	}
e3123c20c   Grigory Kletsko   PCI: rcar: Add mu...
831
832
  	for (i = 0; i < INT_PCI_MSI_NR; i++)
  		irq_create_mapping(msi->domain, i);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
833
  	/* Two irqs are for MSI, but they are also used for non-MSI irqs */
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
834
  	err = devm_request_irq(dev, msi->irq1, rcar_pcie_msi_irq,
8ff0ef996   Grygorii Strashko   PCI: host: Mark P...
835
836
  			       IRQF_SHARED | IRQF_NO_THREAD,
  			       rcar_msi_irq_chip.name, pcie);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
837
  	if (err < 0) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
838
839
  		dev_err(dev, "failed to request IRQ: %d
  ", err);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
840
841
  		goto err;
  	}
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
842
  	err = devm_request_irq(dev, msi->irq2, rcar_pcie_msi_irq,
8ff0ef996   Grygorii Strashko   PCI: host: Mark P...
843
844
  			       IRQF_SHARED | IRQF_NO_THREAD,
  			       rcar_msi_irq_chip.name, pcie);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
845
  	if (err < 0) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
846
847
  		dev_err(dev, "failed to request IRQ: %d
  ", err);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
848
849
850
851
852
853
  		goto err;
  	}
  
  	/* setup MSI data target */
  	msi->pages = __get_free_pages(GFP_KERNEL, 0);
  	base = virt_to_phys((void *)msi->pages);
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
854
855
  	rcar_pci_write_reg(pcie, base | MSIFE, PCIEMSIALR);
  	rcar_pci_write_reg(pcie, 0, PCIEMSIAUR);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
856
857
  
  	/* enable all MSI interrupts */
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
858
  	rcar_pci_write_reg(pcie, 0xffffffff, PCIEMSIIER);
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
859
860
861
862
863
864
865
  
  	return 0;
  
  err:
  	irq_domain_remove(msi->domain);
  	return err;
  }
d0206f212   Bjorn Helgaas   PCI: rcar: Remove...
866
  static int rcar_pcie_get_resources(struct rcar_pcie *pcie)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
867
  {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
868
  	struct device *dev = pcie->dev;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
869
  	struct resource res;
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
870
  	int err, i;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
871

4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
872
  	err = of_address_to_resource(dev->of_node, 0, &res);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
873
874
  	if (err)
  		return err;
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
875
  	pcie->base = devm_ioremap_resource(dev, &res);
51afa3cc2   Bjorn Helgaas   PCI: rcar: Consol...
876
877
  	if (IS_ERR(pcie->base))
  		return PTR_ERR(pcie->base);
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
878
  	pcie->clk = devm_clk_get(dev, "pcie");
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
879
  	if (IS_ERR(pcie->clk)) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
880
881
  		dev_err(dev, "cannot get platform clock
  ");
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
882
883
884
885
  		return PTR_ERR(pcie->clk);
  	}
  	err = clk_prepare_enable(pcie->clk);
  	if (err)
3d664b070   Geert Uytterhoeven   PCI: rcar: Don't ...
886
  		return err;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
887

4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
888
  	pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
889
  	if (IS_ERR(pcie->bus_clk)) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
890
891
  		dev_err(dev, "cannot get pcie bus clock
  ");
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
892
893
894
895
896
  		err = PTR_ERR(pcie->bus_clk);
  		goto fail_clk;
  	}
  	err = clk_prepare_enable(pcie->bus_clk);
  	if (err)
3d664b070   Geert Uytterhoeven   PCI: rcar: Don't ...
897
  		goto fail_clk;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
898

4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
899
  	i = irq_of_parse_and_map(dev->of_node, 0);
c51d411fe   Dmitry Torokhov   PCI: rcar: Fix er...
900
  	if (!i) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
901
902
  		dev_err(dev, "cannot get platform resources for msi interrupt
  ");
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
903
904
905
906
  		err = -ENOENT;
  		goto err_map_reg;
  	}
  	pcie->msi.irq1 = i;
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
907
  	i = irq_of_parse_and_map(dev->of_node, 1);
c51d411fe   Dmitry Torokhov   PCI: rcar: Fix er...
908
  	if (!i) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
909
910
  		dev_err(dev, "cannot get platform resources for msi interrupt
  ");
290c1fb35   Phil Edworthy   PCI: rcar: Add MS...
911
912
913
914
  		err = -ENOENT;
  		goto err_map_reg;
  	}
  	pcie->msi.irq2 = i;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
  	return 0;
  
  err_map_reg:
  	clk_disable_unprepare(pcie->bus_clk);
  fail_clk:
  	clk_disable_unprepare(pcie->clk);
  
  	return err;
  }
  
  static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie,
  				    struct of_pci_range *range,
  				    int *index)
  {
  	u64 restype = range->flags;
  	u64 cpu_addr = range->cpu_addr;
  	u64 cpu_end = range->cpu_addr + range->size;
  	u64 pci_addr = range->pci_addr;
  	u32 flags = LAM_64BIT | LAR_ENABLE;
  	u64 mask;
  	u64 size;
  	int idx = *index;
  
  	if (restype & IORESOURCE_PREFETCH)
  		flags |= LAM_PREFETCH;
  
  	/*
  	 * 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;
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
948

c25da4778   Phil Edworthy   PCI: rcar: Add Re...
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
  		size = min(range->size, alignment);
  	} else {
  		size = range->size;
  	}
  	/* Hardware supports max 4GiB inbound region */
  	size = min(size, 1ULL << 32);
  
  	mask = roundup_pow_of_two(size) - 1;
  	mask &= ~0xf;
  
  	while (cpu_addr < cpu_end) {
  		/*
  		 * Set up 64-bit inbound regions as the range parser doesn't
  		 * distinguish between 32 and 64-bit types.
  		 */
f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
964
965
  		rcar_pci_write_reg(pcie, lower_32_bits(pci_addr),
  				   PCIEPRAR(idx));
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
966
  		rcar_pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx));
f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
967
968
  		rcar_pci_write_reg(pcie, lower_32_bits(mask) | flags,
  				   PCIELAMR(idx));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
969

f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
970
971
972
973
  		rcar_pci_write_reg(pcie, upper_32_bits(pci_addr),
  				   PCIEPRAR(idx + 1));
  		rcar_pci_write_reg(pcie, upper_32_bits(cpu_addr),
  				   PCIELAR(idx + 1));
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
974
  		rcar_pci_write_reg(pcie, 0, PCIELAMR(idx + 1));
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
  
  		pci_addr += size;
  		cpu_addr += size;
  		idx += 2;
  
  		if (idx > MAX_NR_INBOUND_MAPS) {
  			dev_err(pcie->dev, "Failed to map inbound regions!
  ");
  			return -EINVAL;
  		}
  	}
  	*index = idx;
  
  	return 0;
  }
  
  static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  				     struct device_node *node)
  {
  	const int na = 3, ns = 2;
  	int rlen;
  
  	parser->node = node;
  	parser->pna = of_n_addr_cells(node);
  	parser->np = parser->pna + na + ns;
  
  	parser->range = of_get_property(node, "dma-ranges", &rlen);
  	if (!parser->range)
  		return -ENOENT;
  
  	parser->end = parser->range + rlen / sizeof(__be32);
  	return 0;
  }
  
  static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie,
  					  struct device_node *np)
  {
  	struct of_pci_range range;
  	struct of_pci_range_parser parser;
  	int index = 0;
  	int err;
  
  	if (pci_dma_range_parser_init(&parser, np))
  		return -EINVAL;
  
  	/* Get the dma-ranges from DT */
  	for_each_of_pci_range(&parser, &range) {
  		u64 end = range.cpu_addr + range.size - 1;
f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
1023

c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
  		dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx
  ",
  			range.flags, range.cpu_addr, end, range.pci_addr);
  
  		err = rcar_pcie_inbound_ranges(pcie, &range, &index);
  		if (err)
  			return err;
  	}
  
  	return 0;
  }
  
  static const struct of_device_id rcar_pcie_of_match[] = {
  	{ .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 },
f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
1038
1039
1040
1041
  	{ .compatible = "renesas,pcie-r8a7790",
  	  .data = rcar_pcie_hw_init_gen2 },
  	{ .compatible = "renesas,pcie-r8a7791",
  	  .data = rcar_pcie_hw_init_gen2 },
d83a328ad   Simon Horman   PCI: rcar: Use ge...
1042
1043
  	{ .compatible = "renesas,pcie-rcar-gen2",
  	  .data = rcar_pcie_hw_init_gen2 },
e015f88c3   Harunobu Kurokawa   PCI: rcar: Add su...
1044
  	{ .compatible = "renesas,pcie-r8a7795", .data = rcar_pcie_hw_init },
49da21108   Simon Horman   PCI: rcar: Add ge...
1045
  	{ .compatible = "renesas,pcie-rcar-gen3", .data = rcar_pcie_hw_init },
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1046
1047
  	{},
  };
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1048
1049
1050
1051
1052
1053
1054
  
  static int rcar_pcie_parse_request_of_pci_ranges(struct rcar_pcie *pci)
  {
  	int err;
  	struct device *dev = pci->dev;
  	struct device_node *np = dev->of_node;
  	resource_size_t iobase;
5e8c87327   Lorenzo Pieralisi   PCI: rcar: Fix pc...
1055
  	struct resource_entry *win, *tmp;
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1056

f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
1057
1058
  	err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
  					       &iobase);
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1059
1060
  	if (err)
  		return err;
6fd7f5509   Bjorn Helgaas   PCI: rcar: Reques...
1061
1062
1063
  	err = devm_request_pci_bus_resources(dev, &pci->resources);
  	if (err)
  		goto out_release_res;
5e8c87327   Lorenzo Pieralisi   PCI: rcar: Fix pc...
1064
  	resource_list_for_each_entry_safe(win, tmp, &pci->resources) {
6fd7f5509   Bjorn Helgaas   PCI: rcar: Reques...
1065
  		struct resource *res = win->res;
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1066

4c540a35c   Bjorn Helgaas   PCI: rcar: Simpli...
1067
  		if (resource_type(res) == IORESOURCE_IO) {
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1068
  			err = pci_remap_iospace(res, iobase);
5e8c87327   Lorenzo Pieralisi   PCI: rcar: Fix pc...
1069
  			if (err) {
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1070
1071
1072
  				dev_warn(dev, "error %d: failed to map resource %pR
  ",
  					 err, res);
5e8c87327   Lorenzo Pieralisi   PCI: rcar: Fix pc...
1073
1074
1075
  
  				resource_list_destroy_entry(win);
  			}
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1076
  		}
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1077
1078
1079
1080
1081
  	}
  
  	return 0;
  
  out_release_res:
4c540a35c   Bjorn Helgaas   PCI: rcar: Simpli...
1082
  	pci_free_resource_list(&pci->resources);
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1083
1084
  	return err;
  }
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1085
1086
  static int rcar_pcie_probe(struct platform_device *pdev)
  {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1087
  	struct device *dev = &pdev->dev;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1088
1089
  	struct rcar_pcie *pcie;
  	unsigned int data;
5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1090
  	int err;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1091
  	int (*hw_init_fn)(struct rcar_pcie *);
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1092
  	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1093
1094
  	if (!pcie)
  		return -ENOMEM;
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1095
  	pcie->dev = dev;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1096

5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1097
  	INIT_LIST_HEAD(&pcie->resources);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1098

5d2917d46   Phil Edworthy   PCI: rcar: Conver...
1099
  	rcar_pcie_parse_request_of_pci_ranges(pcie);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1100

d0206f212   Bjorn Helgaas   PCI: rcar: Remove...
1101
  	err = rcar_pcie_get_resources(pcie);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1102
  	if (err < 0) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1103
1104
  		dev_err(dev, "failed to request resources: %d
  ", err);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1105
1106
  		return err;
  	}
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1107
  	err = rcar_pcie_parse_map_dma_ranges(pcie, dev->of_node);
f7bc63802   Sergei Shtylyov   PCI: rcar: Fix so...
1108
  	if (err)
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1109
  		return err;
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1110
1111
  	pm_runtime_enable(dev);
  	err = pm_runtime_get_sync(dev);
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1112
  	if (err < 0) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1113
1114
  		dev_err(dev, "pm_runtime_get_sync failed
  ");
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1115
1116
  		goto err_pm_disable;
  	}
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1117
  	/* Failure to get a link might just be that no cards are inserted */
ff1677e23   Bjorn Helgaas   PCI: rcar: Use of...
1118
  	hw_init_fn = of_device_get_match_data(dev);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1119
1120
  	err = hw_init_fn(pcie);
  	if (err) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1121
1122
  		dev_info(dev, "PCIe link down
  ");
e94888d23   Harunobu Kurokawa   PCI: rcar: Return...
1123
  		err = -ENODEV;
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1124
  		goto err_pm_put;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1125
  	}
b77188495   Phil Edworthy   PCI: rcar: Cleanu...
1126
  	data = rcar_pci_read_reg(pcie, MACSR);
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1127
1128
  	dev_info(dev, "PCIe x%d: link up
  ", (data >> 20) & 0x3f);
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1129

de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1130
1131
1132
  	if (IS_ENABLED(CONFIG_PCI_MSI)) {
  		err = rcar_pcie_enable_msi(pcie);
  		if (err < 0) {
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1133
  			dev_err(dev,
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  				"failed to enable MSI support: %d
  ",
  				err);
  			goto err_pm_put;
  		}
  	}
  
  	err = rcar_pcie_enable(pcie);
  	if (err)
  		goto err_pm_put;
  
  	return 0;
  
  err_pm_put:
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1148
  	pm_runtime_put(dev);
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1149
1150
  
  err_pm_disable:
4ef80d72a   Bjorn Helgaas   PCI: rcar: Add lo...
1151
  	pm_runtime_disable(dev);
de1be9a88   Phil Edworthy   PCI: rcar: Add ru...
1152
  	return err;
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1153
1154
1155
1156
  }
  
  static struct platform_driver rcar_pcie_driver = {
  	.driver = {
3ff8e4ac3   Bjorn Helgaas   PCI: rcar: Remove...
1157
  		.name = "rcar-pcie",
c25da4778   Phil Edworthy   PCI: rcar: Add Re...
1158
1159
1160
1161
1162
  		.of_match_table = rcar_pcie_of_match,
  		.suppress_bind_attrs = true,
  	},
  	.probe = rcar_pcie_probe,
  };
42d107198   Paul Gortmaker   PCI: rcar: Make e...
1163
  builtin_platform_driver(rcar_pcie_driver);