diff options
author | Phil Edworthy <phil.edworthy@renesas.com> | 2014-05-12 06:57:48 -0400 |
---|---|---|
committer | Bjorn Helgaas <bhelgaas@google.com> | 2014-05-27 22:37:37 -0400 |
commit | c25da4778803b41e11fd82dd5576c35c09b5f0e0 (patch) | |
tree | 9a492c6397f717c0a6b4d5b3f52dc050c1dc8de8 | |
parent | b9bfe1bca8ecccf3d1f015fd18fad52ed95f8033 (diff) |
PCI: rcar: Add Renesas R-Car PCIe driver
This PCIe Host driver currently does not support MSI, so cards fall back to
INTx interrupts.
Signed-off-by: Phil Edworthy <phil.edworthy@renesas.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Simon Horman <horms+renesas@verge.net.au>
-rw-r--r-- | drivers/pci/host/Kconfig | 6 | ||||
-rw-r--r-- | drivers/pci/host/Makefile | 1 | ||||
-rw-r--r-- | drivers/pci/host/pcie-rcar.c | 768 |
3 files changed, 775 insertions, 0 deletions
diff --git a/drivers/pci/host/Kconfig b/drivers/pci/host/Kconfig index a6f67ec8882f..24d290d097be 100644 --- a/drivers/pci/host/Kconfig +++ b/drivers/pci/host/Kconfig | |||
@@ -33,4 +33,10 @@ config PCI_RCAR_GEN2 | |||
33 | There are 3 internal PCI controllers available with a single | 33 | There are 3 internal PCI controllers available with a single |
34 | built-in EHCI/OHCI host controller present on each one. | 34 | built-in EHCI/OHCI host controller present on each one. |
35 | 35 | ||
36 | config PCI_RCAR_GEN2_PCIE | ||
37 | bool "Renesas R-Car PCIe controller" | ||
38 | depends on ARCH_SHMOBILE || (ARM && COMPILE_TEST) | ||
39 | help | ||
40 | Say Y here if you want PCIe controller support on R-Car Gen2 SoCs. | ||
41 | |||
36 | endmenu | 42 | endmenu |
diff --git a/drivers/pci/host/Makefile b/drivers/pci/host/Makefile index 13fb3333aa05..19946f9a4521 100644 --- a/drivers/pci/host/Makefile +++ b/drivers/pci/host/Makefile | |||
@@ -4,3 +4,4 @@ obj-$(CONFIG_PCI_IMX6) += pci-imx6.o | |||
4 | obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o | 4 | obj-$(CONFIG_PCI_MVEBU) += pci-mvebu.o |
5 | obj-$(CONFIG_PCI_TEGRA) += pci-tegra.o | 5 | obj-$(CONFIG_PCI_TEGRA) += pci-tegra.o |
6 | obj-$(CONFIG_PCI_RCAR_GEN2) += pci-rcar-gen2.o | 6 | obj-$(CONFIG_PCI_RCAR_GEN2) += pci-rcar-gen2.o |
7 | obj-$(CONFIG_PCI_RCAR_GEN2_PCIE) += pcie-rcar.o | ||
diff --git a/drivers/pci/host/pcie-rcar.c b/drivers/pci/host/pcie-rcar.c new file mode 100644 index 000000000000..3c524b9e60e5 --- /dev/null +++ b/drivers/pci/host/pcie-rcar.c | |||
@@ -0,0 +1,768 @@ | |||
1 | /* | ||
2 | * PCIe driver for Renesas R-Car SoCs | ||
3 | * Copyright (C) 2014 Renesas Electronics Europe Ltd | ||
4 | * | ||
5 | * Based on: | ||
6 | * arch/sh/drivers/pci/pcie-sh7786.c | ||
7 | * arch/sh/drivers/pci/ops-sh7786.c | ||
8 | * Copyright (C) 2009 - 2011 Paul Mundt | ||
9 | * | ||
10 | * This file is licensed under the terms of the GNU General Public | ||
11 | * License version 2. This program is licensed "as is" without any | ||
12 | * warranty of any kind, whether express or implied. | ||
13 | */ | ||
14 | |||
15 | #include <linux/clk.h> | ||
16 | #include <linux/delay.h> | ||
17 | #include <linux/interrupt.h> | ||
18 | #include <linux/kernel.h> | ||
19 | #include <linux/module.h> | ||
20 | #include <linux/of_address.h> | ||
21 | #include <linux/of_irq.h> | ||
22 | #include <linux/of_pci.h> | ||
23 | #include <linux/of_platform.h> | ||
24 | #include <linux/pci.h> | ||
25 | #include <linux/platform_device.h> | ||
26 | #include <linux/slab.h> | ||
27 | |||
28 | #define DRV_NAME "rcar-pcie" | ||
29 | |||
30 | #define PCIECAR 0x000010 | ||
31 | #define PCIECCTLR 0x000018 | ||
32 | #define CONFIG_SEND_ENABLE (1 << 31) | ||
33 | #define TYPE0 (0 << 8) | ||
34 | #define TYPE1 (1 << 8) | ||
35 | #define PCIECDR 0x000020 | ||
36 | #define PCIEMSR 0x000028 | ||
37 | #define PCIEINTXR 0x000400 | ||
38 | |||
39 | /* Transfer control */ | ||
40 | #define PCIETCTLR 0x02000 | ||
41 | #define CFINIT 1 | ||
42 | #define PCIETSTR 0x02004 | ||
43 | #define DATA_LINK_ACTIVE 1 | ||
44 | #define PCIEERRFR 0x02020 | ||
45 | #define UNSUPPORTED_REQUEST (1 << 4) | ||
46 | |||
47 | /* root port address */ | ||
48 | #define PCIEPRAR(x) (0x02080 + ((x) * 0x4)) | ||
49 | |||
50 | /* local address reg & mask */ | ||
51 | #define PCIELAR(x) (0x02200 + ((x) * 0x20)) | ||
52 | #define PCIELAMR(x) (0x02208 + ((x) * 0x20)) | ||
53 | #define LAM_PREFETCH (1 << 3) | ||
54 | #define LAM_64BIT (1 << 2) | ||
55 | #define LAR_ENABLE (1 << 1) | ||
56 | |||
57 | /* PCIe address reg & mask */ | ||
58 | #define PCIEPARL(x) (0x03400 + ((x) * 0x20)) | ||
59 | #define PCIEPARH(x) (0x03404 + ((x) * 0x20)) | ||
60 | #define PCIEPAMR(x) (0x03408 + ((x) * 0x20)) | ||
61 | #define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20)) | ||
62 | #define PAR_ENABLE (1 << 31) | ||
63 | #define IO_SPACE (1 << 8) | ||
64 | |||
65 | /* Configuration */ | ||
66 | #define PCICONF(x) (0x010000 + ((x) * 0x4)) | ||
67 | #define PMCAP(x) (0x010040 + ((x) * 0x4)) | ||
68 | #define EXPCAP(x) (0x010070 + ((x) * 0x4)) | ||
69 | #define VCCAP(x) (0x010100 + ((x) * 0x4)) | ||
70 | |||
71 | /* link layer */ | ||
72 | #define IDSETR1 0x011004 | ||
73 | #define TLCTLR 0x011048 | ||
74 | #define MACSR 0x011054 | ||
75 | #define MACCTLR 0x011058 | ||
76 | #define SCRAMBLE_DISABLE (1 << 27) | ||
77 | |||
78 | /* R-Car H1 PHY */ | ||
79 | #define H1_PCIEPHYADRR 0x04000c | ||
80 | #define WRITE_CMD (1 << 16) | ||
81 | #define PHY_ACK (1 << 24) | ||
82 | #define RATE_POS 12 | ||
83 | #define LANE_POS 8 | ||
84 | #define ADR_POS 0 | ||
85 | #define H1_PCIEPHYDOUTR 0x040014 | ||
86 | #define H1_PCIEPHYSR 0x040018 | ||
87 | |||
88 | #define RCONF(x) (PCICONF(0)+(x)) | ||
89 | #define RPMCAP(x) (PMCAP(0)+(x)) | ||
90 | #define REXPCAP(x) (EXPCAP(0)+(x)) | ||
91 | #define RVCCAP(x) (VCCAP(0)+(x)) | ||
92 | |||
93 | #define PCIE_CONF_BUS(b) (((b) & 0xff) << 24) | ||
94 | #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19) | ||
95 | #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16) | ||
96 | |||
97 | #define PCI_MAX_RESOURCES 4 | ||
98 | #define MAX_NR_INBOUND_MAPS 6 | ||
99 | |||
100 | /* Structure representing the PCIe interface */ | ||
101 | struct rcar_pcie { | ||
102 | struct device *dev; | ||
103 | void __iomem *base; | ||
104 | struct resource res[PCI_MAX_RESOURCES]; | ||
105 | struct resource busn; | ||
106 | int root_bus_nr; | ||
107 | struct clk *clk; | ||
108 | struct clk *bus_clk; | ||
109 | }; | ||
110 | |||
111 | static inline struct rcar_pcie *sys_to_pcie(struct pci_sys_data *sys) | ||
112 | { | ||
113 | return sys->private_data; | ||
114 | } | ||
115 | |||
116 | static void pci_write_reg(struct rcar_pcie *pcie, unsigned long val, | ||
117 | unsigned long reg) | ||
118 | { | ||
119 | writel(val, pcie->base + reg); | ||
120 | } | ||
121 | |||
122 | static unsigned long pci_read_reg(struct rcar_pcie *pcie, unsigned long reg) | ||
123 | { | ||
124 | return readl(pcie->base + reg); | ||
125 | } | ||
126 | |||
127 | enum { | ||
128 | PCI_ACCESS_READ, | ||
129 | PCI_ACCESS_WRITE, | ||
130 | }; | ||
131 | |||
132 | static void rcar_rmw32(struct rcar_pcie *pcie, int where, u32 mask, u32 data) | ||
133 | { | ||
134 | int shift = 8 * (where & 3); | ||
135 | u32 val = pci_read_reg(pcie, where & ~3); | ||
136 | |||
137 | val &= ~(mask << shift); | ||
138 | val |= data << shift; | ||
139 | pci_write_reg(pcie, val, where & ~3); | ||
140 | } | ||
141 | |||
142 | static u32 rcar_read_conf(struct rcar_pcie *pcie, int where) | ||
143 | { | ||
144 | int shift = 8 * (where & 3); | ||
145 | u32 val = pci_read_reg(pcie, where & ~3); | ||
146 | |||
147 | return val >> shift; | ||
148 | } | ||
149 | |||
150 | /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ | ||
151 | static int rcar_pcie_config_access(struct rcar_pcie *pcie, | ||
152 | unsigned char access_type, struct pci_bus *bus, | ||
153 | unsigned int devfn, int where, u32 *data) | ||
154 | { | ||
155 | int dev, func, reg, index; | ||
156 | |||
157 | dev = PCI_SLOT(devfn); | ||
158 | func = PCI_FUNC(devfn); | ||
159 | reg = where & ~3; | ||
160 | index = reg / 4; | ||
161 | |||
162 | /* | ||
163 | * While each channel has its own memory-mapped extended config | ||
164 | * space, it's generally only accessible when in endpoint mode. | ||
165 | * When in root complex mode, the controller is unable to target | ||
166 | * itself with either type 0 or type 1 accesses, and indeed, any | ||
167 | * controller initiated target transfer to its own config space | ||
168 | * result in a completer abort. | ||
169 | * | ||
170 | * Each channel effectively only supports a single device, but as | ||
171 | * the same channel <-> device access works for any PCI_SLOT() | ||
172 | * value, we cheat a bit here and bind the controller's config | ||
173 | * space to devfn 0 in order to enable self-enumeration. In this | ||
174 | * case the regular ECAR/ECDR path is sidelined and the mangled | ||
175 | * config access itself is initiated as an internal bus transaction. | ||
176 | */ | ||
177 | if (pci_is_root_bus(bus)) { | ||
178 | if (dev != 0) | ||
179 | return PCIBIOS_DEVICE_NOT_FOUND; | ||
180 | |||
181 | if (access_type == PCI_ACCESS_READ) { | ||
182 | *data = pci_read_reg(pcie, PCICONF(index)); | ||
183 | } else { | ||
184 | /* Keep an eye out for changes to the root bus number */ | ||
185 | if (pci_is_root_bus(bus) && (reg == PCI_PRIMARY_BUS)) | ||
186 | pcie->root_bus_nr = *data & 0xff; | ||
187 | |||
188 | pci_write_reg(pcie, *data, PCICONF(index)); | ||
189 | } | ||
190 | |||
191 | return PCIBIOS_SUCCESSFUL; | ||
192 | } | ||
193 | |||
194 | if (pcie->root_bus_nr < 0) | ||
195 | return PCIBIOS_DEVICE_NOT_FOUND; | ||
196 | |||
197 | /* Clear errors */ | ||
198 | pci_write_reg(pcie, pci_read_reg(pcie, PCIEERRFR), PCIEERRFR); | ||
199 | |||
200 | /* Set the PIO address */ | ||
201 | pci_write_reg(pcie, PCIE_CONF_BUS(bus->number) | PCIE_CONF_DEV(dev) | | ||
202 | PCIE_CONF_FUNC(func) | reg, PCIECAR); | ||
203 | |||
204 | /* Enable the configuration access */ | ||
205 | if (bus->parent->number == pcie->root_bus_nr) | ||
206 | pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE0, PCIECCTLR); | ||
207 | else | ||
208 | pci_write_reg(pcie, CONFIG_SEND_ENABLE | TYPE1, PCIECCTLR); | ||
209 | |||
210 | /* Check for errors */ | ||
211 | if (pci_read_reg(pcie, PCIEERRFR) & UNSUPPORTED_REQUEST) | ||
212 | return PCIBIOS_DEVICE_NOT_FOUND; | ||
213 | |||
214 | /* Check for master and target aborts */ | ||
215 | if (rcar_read_conf(pcie, RCONF(PCI_STATUS)) & | ||
216 | (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT)) | ||
217 | return PCIBIOS_DEVICE_NOT_FOUND; | ||
218 | |||
219 | if (access_type == PCI_ACCESS_READ) | ||
220 | *data = pci_read_reg(pcie, PCIECDR); | ||
221 | else | ||
222 | pci_write_reg(pcie, *data, PCIECDR); | ||
223 | |||
224 | /* Disable the configuration access */ | ||
225 | pci_write_reg(pcie, 0, PCIECCTLR); | ||
226 | |||
227 | return PCIBIOS_SUCCESSFUL; | ||
228 | } | ||
229 | |||
230 | static int rcar_pcie_read_conf(struct pci_bus *bus, unsigned int devfn, | ||
231 | int where, int size, u32 *val) | ||
232 | { | ||
233 | struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata); | ||
234 | int ret; | ||
235 | |||
236 | if ((size == 2) && (where & 1)) | ||
237 | return PCIBIOS_BAD_REGISTER_NUMBER; | ||
238 | else if ((size == 4) && (where & 3)) | ||
239 | return PCIBIOS_BAD_REGISTER_NUMBER; | ||
240 | |||
241 | ret = rcar_pcie_config_access(pcie, PCI_ACCESS_READ, | ||
242 | bus, devfn, where, val); | ||
243 | if (ret != PCIBIOS_SUCCESSFUL) { | ||
244 | *val = 0xffffffff; | ||
245 | return ret; | ||
246 | } | ||
247 | |||
248 | if (size == 1) | ||
249 | *val = (*val >> (8 * (where & 3))) & 0xff; | ||
250 | else if (size == 2) | ||
251 | *val = (*val >> (8 * (where & 2))) & 0xffff; | ||
252 | |||
253 | dev_dbg(&bus->dev, "pcie-config-read: bus=%3d devfn=0x%04x " | ||
254 | "where=0x%04x size=%d val=0x%08lx\n", bus->number, | ||
255 | devfn, where, size, (unsigned long)*val); | ||
256 | |||
257 | return ret; | ||
258 | } | ||
259 | |||
260 | /* Serialization is provided by 'pci_lock' in drivers/pci/access.c */ | ||
261 | static int rcar_pcie_write_conf(struct pci_bus *bus, unsigned int devfn, | ||
262 | int where, int size, u32 val) | ||
263 | { | ||
264 | struct rcar_pcie *pcie = sys_to_pcie(bus->sysdata); | ||
265 | int shift, ret; | ||
266 | u32 data; | ||
267 | |||
268 | if ((size == 2) && (where & 1)) | ||
269 | return PCIBIOS_BAD_REGISTER_NUMBER; | ||
270 | else if ((size == 4) && (where & 3)) | ||
271 | return PCIBIOS_BAD_REGISTER_NUMBER; | ||
272 | |||
273 | ret = rcar_pcie_config_access(pcie, PCI_ACCESS_READ, | ||
274 | bus, devfn, where, &data); | ||
275 | if (ret != PCIBIOS_SUCCESSFUL) | ||
276 | return ret; | ||
277 | |||
278 | dev_dbg(&bus->dev, "pcie-config-write: bus=%3d devfn=0x%04x " | ||
279 | "where=0x%04x size=%d val=0x%08lx\n", bus->number, | ||
280 | devfn, where, size, (unsigned long)val); | ||
281 | |||
282 | if (size == 1) { | ||
283 | shift = 8 * (where & 3); | ||
284 | data &= ~(0xff << shift); | ||
285 | data |= ((val & 0xff) << shift); | ||
286 | } else if (size == 2) { | ||
287 | shift = 8 * (where & 2); | ||
288 | data &= ~(0xffff << shift); | ||
289 | data |= ((val & 0xffff) << shift); | ||
290 | } else | ||
291 | data = val; | ||
292 | |||
293 | ret = rcar_pcie_config_access(pcie, PCI_ACCESS_WRITE, | ||
294 | bus, devfn, where, &data); | ||
295 | |||
296 | return ret; | ||
297 | } | ||
298 | |||
299 | static struct pci_ops rcar_pcie_ops = { | ||
300 | .read = rcar_pcie_read_conf, | ||
301 | .write = rcar_pcie_write_conf, | ||
302 | }; | ||
303 | |||
304 | static void rcar_pcie_setup_window(int win, struct resource *res, | ||
305 | struct rcar_pcie *pcie) | ||
306 | { | ||
307 | /* Setup PCIe address space mappings for each resource */ | ||
308 | resource_size_t size; | ||
309 | u32 mask; | ||
310 | |||
311 | pci_write_reg(pcie, 0x00000000, PCIEPTCTLR(win)); | ||
312 | |||
313 | /* | ||
314 | * The PAMR mask is calculated in units of 128Bytes, which | ||
315 | * keeps things pretty simple. | ||
316 | */ | ||
317 | size = resource_size(res); | ||
318 | mask = (roundup_pow_of_two(size) / SZ_128) - 1; | ||
319 | pci_write_reg(pcie, mask << 7, PCIEPAMR(win)); | ||
320 | |||
321 | pci_write_reg(pcie, upper_32_bits(res->start), PCIEPARH(win)); | ||
322 | pci_write_reg(pcie, lower_32_bits(res->start), PCIEPARL(win)); | ||
323 | |||
324 | /* First resource is for IO */ | ||
325 | mask = PAR_ENABLE; | ||
326 | if (res->flags & IORESOURCE_IO) | ||
327 | mask |= IO_SPACE; | ||
328 | |||
329 | pci_write_reg(pcie, mask, PCIEPTCTLR(win)); | ||
330 | } | ||
331 | |||
332 | static int rcar_pcie_setup(int nr, struct pci_sys_data *sys) | ||
333 | { | ||
334 | struct rcar_pcie *pcie = sys_to_pcie(sys); | ||
335 | struct resource *res; | ||
336 | int i; | ||
337 | |||
338 | pcie->root_bus_nr = -1; | ||
339 | |||
340 | /* Setup PCI resources */ | ||
341 | for (i = 0; i < PCI_MAX_RESOURCES; i++) { | ||
342 | |||
343 | res = &pcie->res[i]; | ||
344 | if (!res->flags) | ||
345 | continue; | ||
346 | |||
347 | rcar_pcie_setup_window(i, res, pcie); | ||
348 | |||
349 | if (res->flags & IORESOURCE_IO) | ||
350 | pci_ioremap_io(nr * SZ_64K, res->start); | ||
351 | else | ||
352 | pci_add_resource(&sys->resources, res); | ||
353 | } | ||
354 | pci_add_resource(&sys->resources, &pcie->busn); | ||
355 | |||
356 | return 1; | ||
357 | } | ||
358 | |||
359 | struct hw_pci rcar_pci = { | ||
360 | .setup = rcar_pcie_setup, | ||
361 | .map_irq = of_irq_parse_and_map_pci, | ||
362 | .ops = &rcar_pcie_ops, | ||
363 | }; | ||
364 | |||
365 | static void rcar_pcie_enable(struct rcar_pcie *pcie) | ||
366 | { | ||
367 | struct platform_device *pdev = to_platform_device(pcie->dev); | ||
368 | |||
369 | rcar_pci.nr_controllers = 1; | ||
370 | rcar_pci.private_data = (void **)&pcie; | ||
371 | |||
372 | pci_common_init_dev(&pdev->dev, &rcar_pci); | ||
373 | #ifdef CONFIG_PCI_DOMAINS | ||
374 | rcar_pci.domain++; | ||
375 | #endif | ||
376 | } | ||
377 | |||
378 | static int phy_wait_for_ack(struct rcar_pcie *pcie) | ||
379 | { | ||
380 | unsigned int timeout = 100; | ||
381 | |||
382 | while (timeout--) { | ||
383 | if (pci_read_reg(pcie, H1_PCIEPHYADRR) & PHY_ACK) | ||
384 | return 0; | ||
385 | |||
386 | udelay(100); | ||
387 | } | ||
388 | |||
389 | dev_err(pcie->dev, "Access to PCIe phy timed out\n"); | ||
390 | |||
391 | return -ETIMEDOUT; | ||
392 | } | ||
393 | |||
394 | static void phy_write_reg(struct rcar_pcie *pcie, | ||
395 | unsigned int rate, unsigned int addr, | ||
396 | unsigned int lane, unsigned int data) | ||
397 | { | ||
398 | unsigned long phyaddr; | ||
399 | |||
400 | phyaddr = WRITE_CMD | | ||
401 | ((rate & 1) << RATE_POS) | | ||
402 | ((lane & 0xf) << LANE_POS) | | ||
403 | ((addr & 0xff) << ADR_POS); | ||
404 | |||
405 | /* Set write data */ | ||
406 | pci_write_reg(pcie, data, H1_PCIEPHYDOUTR); | ||
407 | pci_write_reg(pcie, phyaddr, H1_PCIEPHYADRR); | ||
408 | |||
409 | /* Ignore errors as they will be dealt with if the data link is down */ | ||
410 | phy_wait_for_ack(pcie); | ||
411 | |||
412 | /* Clear command */ | ||
413 | pci_write_reg(pcie, 0, H1_PCIEPHYDOUTR); | ||
414 | pci_write_reg(pcie, 0, H1_PCIEPHYADRR); | ||
415 | |||
416 | /* Ignore errors as they will be dealt with if the data link is down */ | ||
417 | phy_wait_for_ack(pcie); | ||
418 | } | ||
419 | |||
420 | static int rcar_pcie_wait_for_dl(struct rcar_pcie *pcie) | ||
421 | { | ||
422 | unsigned int timeout = 10; | ||
423 | |||
424 | while (timeout--) { | ||
425 | if ((pci_read_reg(pcie, PCIETSTR) & DATA_LINK_ACTIVE)) | ||
426 | return 0; | ||
427 | |||
428 | msleep(5); | ||
429 | } | ||
430 | |||
431 | return -ETIMEDOUT; | ||
432 | } | ||
433 | |||
434 | static int rcar_pcie_hw_init(struct rcar_pcie *pcie) | ||
435 | { | ||
436 | int err; | ||
437 | |||
438 | /* Begin initialization */ | ||
439 | pci_write_reg(pcie, 0, PCIETCTLR); | ||
440 | |||
441 | /* Set mode */ | ||
442 | pci_write_reg(pcie, 1, PCIEMSR); | ||
443 | |||
444 | /* | ||
445 | * Initial header for port config space is type 1, set the device | ||
446 | * class to match. Hardware takes care of propagating the IDSETR | ||
447 | * settings, so there is no need to bother with a quirk. | ||
448 | */ | ||
449 | pci_write_reg(pcie, PCI_CLASS_BRIDGE_PCI << 16, IDSETR1); | ||
450 | |||
451 | /* | ||
452 | * Setup Secondary Bus Number & Subordinate Bus Number, even though | ||
453 | * they aren't used, to avoid bridge being detected as broken. | ||
454 | */ | ||
455 | rcar_rmw32(pcie, RCONF(PCI_SECONDARY_BUS), 0xff, 1); | ||
456 | rcar_rmw32(pcie, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1); | ||
457 | |||
458 | /* Initialize default capabilities. */ | ||
459 | rcar_rmw32(pcie, REXPCAP(0), 0, PCI_CAP_ID_EXP); | ||
460 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_FLAGS), | ||
461 | PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4); | ||
462 | rcar_rmw32(pcie, RCONF(PCI_HEADER_TYPE), 0x7f, | ||
463 | PCI_HEADER_TYPE_BRIDGE); | ||
464 | |||
465 | /* Enable data link layer active state reporting */ | ||
466 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_LNKCAP), 0, PCI_EXP_LNKCAP_DLLLARC); | ||
467 | |||
468 | /* Write out the physical slot number = 0 */ | ||
469 | rcar_rmw32(pcie, REXPCAP(PCI_EXP_SLTCAP), PCI_EXP_SLTCAP_PSN, 0); | ||
470 | |||
471 | /* Set the completion timer timeout to the maximum 50ms. */ | ||
472 | rcar_rmw32(pcie, TLCTLR+1, 0x3f, 50); | ||
473 | |||
474 | /* Terminate list of capabilities (Next Capability Offset=0) */ | ||
475 | rcar_rmw32(pcie, RVCCAP(0), 0xfff0, 0); | ||
476 | |||
477 | /* Enable MAC data scrambling. */ | ||
478 | rcar_rmw32(pcie, MACCTLR, SCRAMBLE_DISABLE, 0); | ||
479 | |||
480 | /* Finish initialization - establish a PCI Express link */ | ||
481 | pci_write_reg(pcie, CFINIT, PCIETCTLR); | ||
482 | |||
483 | /* This will timeout if we don't have a link. */ | ||
484 | err = rcar_pcie_wait_for_dl(pcie); | ||
485 | if (err) | ||
486 | return err; | ||
487 | |||
488 | /* Enable INTx interrupts */ | ||
489 | rcar_rmw32(pcie, PCIEINTXR, 0, 0xF << 8); | ||
490 | |||
491 | /* Enable slave Bus Mastering */ | ||
492 | rcar_rmw32(pcie, RCONF(PCI_STATUS), PCI_STATUS_DEVSEL_MASK, | ||
493 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | | ||
494 | PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST); | ||
495 | |||
496 | wmb(); | ||
497 | |||
498 | return 0; | ||
499 | } | ||
500 | |||
501 | static int rcar_pcie_hw_init_h1(struct rcar_pcie *pcie) | ||
502 | { | ||
503 | unsigned int timeout = 10; | ||
504 | |||
505 | /* Initialize the phy */ | ||
506 | phy_write_reg(pcie, 0, 0x42, 0x1, 0x0EC34191); | ||
507 | phy_write_reg(pcie, 1, 0x42, 0x1, 0x0EC34180); | ||
508 | phy_write_reg(pcie, 0, 0x43, 0x1, 0x00210188); | ||
509 | phy_write_reg(pcie, 1, 0x43, 0x1, 0x00210188); | ||
510 | phy_write_reg(pcie, 0, 0x44, 0x1, 0x015C0014); | ||
511 | phy_write_reg(pcie, 1, 0x44, 0x1, 0x015C0014); | ||
512 | phy_write_reg(pcie, 1, 0x4C, 0x1, 0x786174A0); | ||
513 | phy_write_reg(pcie, 1, 0x4D, 0x1, 0x048000BB); | ||
514 | phy_write_reg(pcie, 0, 0x51, 0x1, 0x079EC062); | ||
515 | phy_write_reg(pcie, 0, 0x52, 0x1, 0x20000000); | ||
516 | phy_write_reg(pcie, 1, 0x52, 0x1, 0x20000000); | ||
517 | phy_write_reg(pcie, 1, 0x56, 0x1, 0x00003806); | ||
518 | |||
519 | phy_write_reg(pcie, 0, 0x60, 0x1, 0x004B03A5); | ||
520 | phy_write_reg(pcie, 0, 0x64, 0x1, 0x3F0F1F0F); | ||
521 | phy_write_reg(pcie, 0, 0x66, 0x1, 0x00008000); | ||
522 | |||
523 | while (timeout--) { | ||
524 | if (pci_read_reg(pcie, H1_PCIEPHYSR)) | ||
525 | return rcar_pcie_hw_init(pcie); | ||
526 | |||
527 | msleep(5); | ||
528 | } | ||
529 | |||
530 | return -ETIMEDOUT; | ||
531 | } | ||
532 | |||
533 | static int rcar_pcie_get_resources(struct platform_device *pdev, | ||
534 | struct rcar_pcie *pcie) | ||
535 | { | ||
536 | struct resource res; | ||
537 | int err; | ||
538 | |||
539 | err = of_address_to_resource(pdev->dev.of_node, 0, &res); | ||
540 | if (err) | ||
541 | return err; | ||
542 | |||
543 | pcie->clk = devm_clk_get(&pdev->dev, "pcie"); | ||
544 | if (IS_ERR(pcie->clk)) { | ||
545 | dev_err(pcie->dev, "cannot get platform clock\n"); | ||
546 | return PTR_ERR(pcie->clk); | ||
547 | } | ||
548 | err = clk_prepare_enable(pcie->clk); | ||
549 | if (err) | ||
550 | goto fail_clk; | ||
551 | |||
552 | pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus"); | ||
553 | if (IS_ERR(pcie->bus_clk)) { | ||
554 | dev_err(pcie->dev, "cannot get pcie bus clock\n"); | ||
555 | err = PTR_ERR(pcie->bus_clk); | ||
556 | goto fail_clk; | ||
557 | } | ||
558 | err = clk_prepare_enable(pcie->bus_clk); | ||
559 | if (err) | ||
560 | goto err_map_reg; | ||
561 | |||
562 | pcie->base = devm_ioremap_resource(&pdev->dev, &res); | ||
563 | if (IS_ERR(pcie->base)) { | ||
564 | err = PTR_ERR(pcie->base); | ||
565 | goto err_map_reg; | ||
566 | } | ||
567 | |||
568 | return 0; | ||
569 | |||
570 | err_map_reg: | ||
571 | clk_disable_unprepare(pcie->bus_clk); | ||
572 | fail_clk: | ||
573 | clk_disable_unprepare(pcie->clk); | ||
574 | |||
575 | return err; | ||
576 | } | ||
577 | |||
578 | static int rcar_pcie_inbound_ranges(struct rcar_pcie *pcie, | ||
579 | struct of_pci_range *range, | ||
580 | int *index) | ||
581 | { | ||
582 | u64 restype = range->flags; | ||
583 | u64 cpu_addr = range->cpu_addr; | ||
584 | u64 cpu_end = range->cpu_addr + range->size; | ||
585 | u64 pci_addr = range->pci_addr; | ||
586 | u32 flags = LAM_64BIT | LAR_ENABLE; | ||
587 | u64 mask; | ||
588 | u64 size; | ||
589 | int idx = *index; | ||
590 | |||
591 | if (restype & IORESOURCE_PREFETCH) | ||
592 | flags |= LAM_PREFETCH; | ||
593 | |||
594 | /* | ||
595 | * If the size of the range is larger than the alignment of the start | ||
596 | * address, we have to use multiple entries to perform the mapping. | ||
597 | */ | ||
598 | if (cpu_addr > 0) { | ||
599 | unsigned long nr_zeros = __ffs64(cpu_addr); | ||
600 | u64 alignment = 1ULL << nr_zeros; | ||
601 | size = min(range->size, alignment); | ||
602 | } else { | ||
603 | size = range->size; | ||
604 | } | ||
605 | /* Hardware supports max 4GiB inbound region */ | ||
606 | size = min(size, 1ULL << 32); | ||
607 | |||
608 | mask = roundup_pow_of_two(size) - 1; | ||
609 | mask &= ~0xf; | ||
610 | |||
611 | while (cpu_addr < cpu_end) { | ||
612 | /* | ||
613 | * Set up 64-bit inbound regions as the range parser doesn't | ||
614 | * distinguish between 32 and 64-bit types. | ||
615 | */ | ||
616 | pci_write_reg(pcie, lower_32_bits(pci_addr), PCIEPRAR(idx)); | ||
617 | pci_write_reg(pcie, lower_32_bits(cpu_addr), PCIELAR(idx)); | ||
618 | pci_write_reg(pcie, lower_32_bits(mask) | flags, PCIELAMR(idx)); | ||
619 | |||
620 | pci_write_reg(pcie, upper_32_bits(pci_addr), PCIEPRAR(idx+1)); | ||
621 | pci_write_reg(pcie, upper_32_bits(cpu_addr), PCIELAR(idx+1)); | ||
622 | pci_write_reg(pcie, 0, PCIELAMR(idx+1)); | ||
623 | |||
624 | pci_addr += size; | ||
625 | cpu_addr += size; | ||
626 | idx += 2; | ||
627 | |||
628 | if (idx > MAX_NR_INBOUND_MAPS) { | ||
629 | dev_err(pcie->dev, "Failed to map inbound regions!\n"); | ||
630 | return -EINVAL; | ||
631 | } | ||
632 | } | ||
633 | *index = idx; | ||
634 | |||
635 | return 0; | ||
636 | } | ||
637 | |||
638 | static int pci_dma_range_parser_init(struct of_pci_range_parser *parser, | ||
639 | struct device_node *node) | ||
640 | { | ||
641 | const int na = 3, ns = 2; | ||
642 | int rlen; | ||
643 | |||
644 | parser->node = node; | ||
645 | parser->pna = of_n_addr_cells(node); | ||
646 | parser->np = parser->pna + na + ns; | ||
647 | |||
648 | parser->range = of_get_property(node, "dma-ranges", &rlen); | ||
649 | if (!parser->range) | ||
650 | return -ENOENT; | ||
651 | |||
652 | parser->end = parser->range + rlen / sizeof(__be32); | ||
653 | return 0; | ||
654 | } | ||
655 | |||
656 | static int rcar_pcie_parse_map_dma_ranges(struct rcar_pcie *pcie, | ||
657 | struct device_node *np) | ||
658 | { | ||
659 | struct of_pci_range range; | ||
660 | struct of_pci_range_parser parser; | ||
661 | int index = 0; | ||
662 | int err; | ||
663 | |||
664 | if (pci_dma_range_parser_init(&parser, np)) | ||
665 | return -EINVAL; | ||
666 | |||
667 | /* Get the dma-ranges from DT */ | ||
668 | for_each_of_pci_range(&parser, &range) { | ||
669 | u64 end = range.cpu_addr + range.size - 1; | ||
670 | dev_dbg(pcie->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n", | ||
671 | range.flags, range.cpu_addr, end, range.pci_addr); | ||
672 | |||
673 | err = rcar_pcie_inbound_ranges(pcie, &range, &index); | ||
674 | if (err) | ||
675 | return err; | ||
676 | } | ||
677 | |||
678 | return 0; | ||
679 | } | ||
680 | |||
681 | static const struct of_device_id rcar_pcie_of_match[] = { | ||
682 | { .compatible = "renesas,pcie-r8a7779", .data = rcar_pcie_hw_init_h1 }, | ||
683 | { .compatible = "renesas,pcie-r8a7790", .data = rcar_pcie_hw_init }, | ||
684 | { .compatible = "renesas,pcie-r8a7791", .data = rcar_pcie_hw_init }, | ||
685 | {}, | ||
686 | }; | ||
687 | MODULE_DEVICE_TABLE(of, rcar_pcie_of_match); | ||
688 | |||
689 | static int rcar_pcie_probe(struct platform_device *pdev) | ||
690 | { | ||
691 | struct rcar_pcie *pcie; | ||
692 | unsigned int data; | ||
693 | struct of_pci_range range; | ||
694 | struct of_pci_range_parser parser; | ||
695 | const struct of_device_id *of_id; | ||
696 | int err, win = 0; | ||
697 | int (*hw_init_fn)(struct rcar_pcie *); | ||
698 | |||
699 | pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL); | ||
700 | if (!pcie) | ||
701 | return -ENOMEM; | ||
702 | |||
703 | pcie->dev = &pdev->dev; | ||
704 | platform_set_drvdata(pdev, pcie); | ||
705 | |||
706 | /* Get the bus range */ | ||
707 | if (of_pci_parse_bus_range(pdev->dev.of_node, &pcie->busn)) { | ||
708 | dev_err(&pdev->dev, "failed to parse bus-range property\n"); | ||
709 | return -EINVAL; | ||
710 | } | ||
711 | |||
712 | if (of_pci_range_parser_init(&parser, pdev->dev.of_node)) { | ||
713 | dev_err(&pdev->dev, "missing ranges property\n"); | ||
714 | return -EINVAL; | ||
715 | } | ||
716 | |||
717 | err = rcar_pcie_get_resources(pdev, pcie); | ||
718 | if (err < 0) { | ||
719 | dev_err(&pdev->dev, "failed to request resources: %d\n", err); | ||
720 | return err; | ||
721 | } | ||
722 | |||
723 | for_each_of_pci_range(&parser, &range) { | ||
724 | of_pci_range_to_resource(&range, pdev->dev.of_node, | ||
725 | &pcie->res[win++]); | ||
726 | |||
727 | if (win > PCI_MAX_RESOURCES) | ||
728 | break; | ||
729 | } | ||
730 | |||
731 | err = rcar_pcie_parse_map_dma_ranges(pcie, pdev->dev.of_node); | ||
732 | if (err) | ||
733 | return err; | ||
734 | |||
735 | of_id = of_match_device(rcar_pcie_of_match, pcie->dev); | ||
736 | if (!of_id || !of_id->data) | ||
737 | return -EINVAL; | ||
738 | hw_init_fn = of_id->data; | ||
739 | |||
740 | /* Failure to get a link might just be that no cards are inserted */ | ||
741 | err = hw_init_fn(pcie); | ||
742 | if (err) { | ||
743 | dev_info(&pdev->dev, "PCIe link down\n"); | ||
744 | return 0; | ||
745 | } | ||
746 | |||
747 | data = pci_read_reg(pcie, MACSR); | ||
748 | dev_info(&pdev->dev, "PCIe x%d: link up\n", (data >> 20) & 0x3f); | ||
749 | |||
750 | rcar_pcie_enable(pcie); | ||
751 | |||
752 | return 0; | ||
753 | } | ||
754 | |||
755 | static struct platform_driver rcar_pcie_driver = { | ||
756 | .driver = { | ||
757 | .name = DRV_NAME, | ||
758 | .owner = THIS_MODULE, | ||
759 | .of_match_table = rcar_pcie_of_match, | ||
760 | .suppress_bind_attrs = true, | ||
761 | }, | ||
762 | .probe = rcar_pcie_probe, | ||
763 | }; | ||
764 | module_platform_driver(rcar_pcie_driver); | ||
765 | |||
766 | MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>"); | ||
767 | MODULE_DESCRIPTION("Renesas R-Car PCIe driver"); | ||
768 | MODULE_LICENSE("GPLv2"); | ||