blob: 76878246f1e02b20d28a9fcbd6fc8d274d6fb0fc [file] [log] [blame]
Marek Vasut879b4a32018-10-16 12:49:19 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Renesas RCar Gen3 PCIEC driver
4 *
5 * Copyright (C) 2018-2019 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on Linux PCIe driver for Renesas R-Car SoCs
8 * Copyright (C) 2014 Renesas Electronics Europe Ltd
9 *
10 * Based on:
11 * arch/sh/drivers/pci/pcie-sh7786.c
12 * arch/sh/drivers/pci/ops-sh7786.c
13 * Copyright (C) 2009 - 2011 Paul Mundt
14 *
15 * Author: Phil Edworthy <phil.edworthy@renesas.com>
16 */
17
Marek Vasut879b4a32018-10-16 12:49:19 +020018#include <asm/io.h>
19#include <clk.h>
20#include <dm.h>
21#include <errno.h>
22#include <pci.h>
23#include <wait_bit.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Marek Vasutd18a5782021-01-24 18:37:11 +010025#include <linux/log2.h>
Marek Vasut879b4a32018-10-16 12:49:19 +020026
27#define PCIECAR 0x000010
28#define PCIECCTLR 0x000018
Tom Rini8187bd12022-12-04 10:14:18 -050029#define SEND_ENABLE BIT(31)
Marek Vasut879b4a32018-10-16 12:49:19 +020030#define TYPE0 (0 << 8)
31#define TYPE1 BIT(8)
32#define PCIECDR 0x000020
33#define PCIEMSR 0x000028
34#define PCIEINTXR 0x000400
35#define PCIEPHYSR 0x0007f0
36#define PHYRDY BIT(0)
37#define PCIEMSITXR 0x000840
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 BIT(4)
46#define PCIEMSIFR 0x02044
47#define PCIEMSIALR 0x02048
48#define MSIFE 1
49#define PCIEMSIAUR 0x0204c
50#define PCIEMSIIER 0x02050
51
52/* root port address */
53#define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
54
55/* local address reg & mask */
56#define PCIELAR(x) (0x02200 + ((x) * 0x20))
57#define PCIELAMR(x) (0x02208 + ((x) * 0x20))
58#define LAM_PREFETCH BIT(3)
59#define LAM_64BIT BIT(2)
60#define LAR_ENABLE BIT(1)
61
62/* PCIe address reg & mask */
63#define PCIEPALR(x) (0x03400 + ((x) * 0x20))
64#define PCIEPAUR(x) (0x03404 + ((x) * 0x20))
65#define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
66#define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
67#define PAR_ENABLE BIT(31)
68#define IO_SPACE BIT(8)
69
70/* Configuration */
71#define PCICONF(x) (0x010000 + ((x) * 0x4))
72#define PMCAP(x) (0x010040 + ((x) * 0x4))
73#define EXPCAP(x) (0x010070 + ((x) * 0x4))
74#define VCCAP(x) (0x010100 + ((x) * 0x4))
75
76/* link layer */
77#define IDSETR1 0x011004
78#define TLCTLR 0x011048
79#define MACSR 0x011054
80#define SPCHGFIN BIT(4)
81#define SPCHGFAIL BIT(6)
82#define SPCHGSUC BIT(7)
83#define LINK_SPEED (0xf << 16)
84#define LINK_SPEED_2_5GTS (1 << 16)
85#define LINK_SPEED_5_0GTS (2 << 16)
86#define MACCTLR 0x011058
87#define SPEED_CHANGE BIT(24)
88#define SCRAMBLE_DISABLE BIT(27)
89#define MACS2R 0x011078
90#define MACCGSPSETR 0x011084
91#define SPCNGRSN BIT(31)
92
93/* R-Car H1 PHY */
94#define H1_PCIEPHYADRR 0x04000c
95#define WRITE_CMD BIT(16)
96#define PHY_ACK BIT(24)
97#define RATE_POS 12
98#define LANE_POS 8
99#define ADR_POS 0
100#define H1_PCIEPHYDOUTR 0x040014
101
102/* R-Car Gen2 PHY */
103#define GEN2_PCIEPHYADDR 0x780
104#define GEN2_PCIEPHYDATA 0x784
105#define GEN2_PCIEPHYCTRL 0x78c
106
107#define INT_PCI_MSI_NR 32
108
109#define RCONF(x) (PCICONF(0) + (x))
110#define RPMCAP(x) (PMCAP(0) + (x))
111#define REXPCAP(x) (EXPCAP(0) + (x))
112#define RVCCAP(x) (VCCAP(0) + (x))
113
114#define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
115#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
116#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
117
118#define RCAR_PCI_MAX_RESOURCES 4
119#define MAX_NR_INBOUND_MAPS 6
120
Marek Vasut879b4a32018-10-16 12:49:19 +0200121enum {
122 RCAR_PCI_ACCESS_READ,
123 RCAR_PCI_ACCESS_WRITE,
124};
125
126struct rcar_gen3_pcie_priv {
127 fdt_addr_t regs;
128};
129
130static void rcar_rmw32(struct udevice *dev, int where, u32 mask, u32 data)
131{
Simon Glassfa20e932020-12-03 16:55:20 -0700132 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200133 int shift = 8 * (where & 3);
134
135 clrsetbits_le32(priv->regs + (where & ~3),
136 mask << shift, data << shift);
137}
138
Simon Glass2a311e82020-01-27 08:49:37 -0700139static u32 rcar_read_conf(const struct udevice *dev, int where)
Marek Vasut879b4a32018-10-16 12:49:19 +0200140{
Simon Glassfa20e932020-12-03 16:55:20 -0700141 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200142 int shift = 8 * (where & 3);
143
144 return readl(priv->regs + (where & ~3)) >> shift;
145}
146
Simon Glass2a311e82020-01-27 08:49:37 -0700147static int rcar_pcie_config_access(const struct udevice *udev,
Marek Vasut879b4a32018-10-16 12:49:19 +0200148 unsigned char access_type,
149 pci_dev_t bdf, int where, ulong *data)
150{
Simon Glassfa20e932020-12-03 16:55:20 -0700151 struct rcar_gen3_pcie_priv *priv = dev_get_plat(udev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200152 u32 reg = where & ~3;
153
Marek Vasut34caff92021-01-16 00:28:18 +0100154 /* Root bus */
155 if (PCI_DEV(bdf) == 0) {
156 if (access_type == RCAR_PCI_ACCESS_READ)
157 *data = readl(priv->regs + PCICONF(where / 4));
158 else
159 writel(*data, priv->regs + PCICONF(where / 4));
160
161 return 0;
162 }
163
Marek Vasut879b4a32018-10-16 12:49:19 +0200164 /* Clear errors */
165 clrbits_le32(priv->regs + PCIEERRFR, 0);
166
167 /* Set the PIO address */
168 writel((bdf << 8) | reg, priv->regs + PCIECAR);
169
170 /* Enable the configuration access */
171 if (!PCI_BUS(bdf))
Tom Rini8187bd12022-12-04 10:14:18 -0500172 writel(SEND_ENABLE | TYPE0, priv->regs + PCIECCTLR);
Marek Vasut879b4a32018-10-16 12:49:19 +0200173 else
Tom Rini8187bd12022-12-04 10:14:18 -0500174 writel(SEND_ENABLE | TYPE1, priv->regs + PCIECCTLR);
Marek Vasut879b4a32018-10-16 12:49:19 +0200175
176 /* Check for errors */
177 if (readl(priv->regs + PCIEERRFR) & UNSUPPORTED_REQUEST)
178 return -ENODEV;
179
180 /* Check for master and target aborts */
181 if (rcar_read_conf(udev, RCONF(PCI_STATUS)) &
182 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
183 return -ENODEV;
184
185 if (access_type == RCAR_PCI_ACCESS_READ)
186 *data = readl(priv->regs + PCIECDR);
187 else
188 writel(*data, priv->regs + PCIECDR);
189
190 /* Disable the configuration access */
191 writel(0, priv->regs + PCIECCTLR);
192
193 return 0;
194}
195
196static int rcar_gen3_pcie_addr_valid(pci_dev_t d, uint where)
197{
198 u32 slot;
199
Marek Vasut34caff92021-01-16 00:28:18 +0100200 if (PCI_BUS(d))
201 return -EINVAL;
202
Marek Vasut879b4a32018-10-16 12:49:19 +0200203 if (PCI_FUNC(d))
204 return -EINVAL;
205
206 slot = PCI_DEV(d);
Marek Vasut34caff92021-01-16 00:28:18 +0100207 if (slot > 1)
Marek Vasut879b4a32018-10-16 12:49:19 +0200208 return -EINVAL;
209
210 return 0;
211}
212
Simon Glass2a311e82020-01-27 08:49:37 -0700213static int rcar_gen3_pcie_read_config(const struct udevice *dev, pci_dev_t bdf,
Marek Vasut879b4a32018-10-16 12:49:19 +0200214 uint where, ulong *val,
215 enum pci_size_t size)
216{
217 ulong reg;
218 int ret;
219
220 ret = rcar_gen3_pcie_addr_valid(bdf, where);
221 if (ret) {
222 *val = pci_get_ff(size);
223 return 0;
224 }
225
226 ret = rcar_pcie_config_access(dev, RCAR_PCI_ACCESS_READ,
227 bdf, where, &reg);
228 if (ret != 0)
229 reg = 0xffffffffUL;
230
231 *val = pci_conv_32_to_size(reg, where, size);
232
233 return ret;
234}
235
236static int rcar_gen3_pcie_write_config(struct udevice *dev, pci_dev_t bdf,
237 uint where, ulong val,
238 enum pci_size_t size)
239{
240 ulong data;
241 int ret;
242
243 ret = rcar_gen3_pcie_addr_valid(bdf, where);
244 if (ret)
245 return ret;
246
247 data = pci_conv_32_to_size(val, where, size);
248
249 ret = rcar_pcie_config_access(dev, RCAR_PCI_ACCESS_WRITE,
250 bdf, where, &data);
251
252 return ret;
253}
254
255static int rcar_gen3_pcie_wait_for_phyrdy(struct udevice *dev)
256{
Simon Glassfa20e932020-12-03 16:55:20 -0700257 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200258
259 return wait_for_bit_le32((void *)priv->regs + PCIEPHYSR, PHYRDY,
260 true, 50, false);
261}
262
263static int rcar_gen3_pcie_wait_for_dl(struct udevice *dev)
264{
Simon Glassfa20e932020-12-03 16:55:20 -0700265 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200266
267 return wait_for_bit_le32((void *)priv->regs + PCIETSTR,
268 DATA_LINK_ACTIVE, true, 50, false);
269}
270
271static int rcar_gen3_pcie_hw_init(struct udevice *dev)
272{
Simon Glassfa20e932020-12-03 16:55:20 -0700273 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200274 int ret;
275
276 /* Begin initialization */
277 writel(0, priv->regs + PCIETCTLR);
278
279 /* Set mode */
280 writel(1, priv->regs + PCIEMSR);
281
282 ret = rcar_gen3_pcie_wait_for_phyrdy(dev);
283 if (ret)
284 return ret;
285
286 /*
287 * Initial header for port config space is type 1, set the device
288 * class to match. Hardware takes care of propagating the IDSETR
289 * settings, so there is no need to bother with a quirk.
290 */
Pali Rohár25781e22022-02-18 13:18:40 +0100291 writel(PCI_CLASS_BRIDGE_PCI_NORMAL << 8, priv->regs + IDSETR1);
Marek Vasut879b4a32018-10-16 12:49:19 +0200292
293 /*
294 * Setup Secondary Bus Number & Subordinate Bus Number, even though
295 * they aren't used, to avoid bridge being detected as broken.
296 */
297 rcar_rmw32(dev, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
298 rcar_rmw32(dev, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
299
300 /* Initialize default capabilities. */
301 rcar_rmw32(dev, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
302 rcar_rmw32(dev, REXPCAP(PCI_EXP_FLAGS),
303 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
304 rcar_rmw32(dev, RCONF(PCI_HEADER_TYPE), 0x7f,
305 PCI_HEADER_TYPE_BRIDGE);
306
307 /* Enable data link layer active state reporting */
308 rcar_rmw32(dev, REXPCAP(PCI_EXP_LNKCAP),
309 PCI_EXP_LNKCAP_DLLLARC, PCI_EXP_LNKCAP_DLLLARC);
310
311 /* Write out the physical slot number = 0 */
312 rcar_rmw32(dev, REXPCAP(PCI_EXP_SLTCAP),
313 PCI_EXP_SLTCAP_PSN, 0);
314
315 /* Set the completion timer timeout to the maximum 50ms. */
316 rcar_rmw32(dev, TLCTLR + 1, 0x3f, 50);
317
318 /* Terminate list of capabilities (Next Capability Offset=0) */
319 rcar_rmw32(dev, RVCCAP(0), 0xfff00000, 0);
320
321 /* Finish initialization - establish a PCI Express link */
322 writel(CFINIT, priv->regs + PCIETCTLR);
323
324 return rcar_gen3_pcie_wait_for_dl(dev);
325}
326
327static int rcar_gen3_pcie_probe(struct udevice *dev)
328{
Simon Glassfa20e932020-12-03 16:55:20 -0700329 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200330 struct pci_controller *hose = dev_get_uclass_priv(dev);
331 struct clk pci_clk;
332 u32 mask;
333 int i, cnt, ret;
334
335 ret = clk_get_by_index(dev, 0, &pci_clk);
336 if (ret)
337 return ret;
338
339 ret = clk_enable(&pci_clk);
340 if (ret)
341 return ret;
342
343 for (i = 0; i < hose->region_count; i++) {
344 if (hose->regions[i].flags != PCI_REGION_SYS_MEMORY)
345 continue;
346
347 if (hose->regions[i].phys_start == 0)
348 continue;
349
Marek Vasutd18a5782021-01-24 18:37:11 +0100350 mask = (roundup_pow_of_two(hose->regions[i].size) - 1) & ~0xf;
Marek Vasut879b4a32018-10-16 12:49:19 +0200351 mask |= LAR_ENABLE;
Marek Vasutd18a5782021-01-24 18:37:11 +0100352 writel(rounddown_pow_of_two(hose->regions[i].phys_start),
353 priv->regs + PCIEPRAR(0));
354 writel(rounddown_pow_of_two(hose->regions[i].phys_start),
355 priv->regs + PCIELAR(0));
Marek Vasut879b4a32018-10-16 12:49:19 +0200356 writel(mask, priv->regs + PCIELAMR(0));
357 break;
358 }
359
Marek Vasut84649712021-01-16 00:33:17 +0100360 writel(0, priv->regs + PCIEPRAR(1));
361 writel(0, priv->regs + PCIELAR(1));
362 writel(0, priv->regs + PCIELAMR(1));
Marek Vasut879b4a32018-10-16 12:49:19 +0200363
364 ret = rcar_gen3_pcie_hw_init(dev);
365 if (ret)
366 return ret;
367
368 for (i = 0, cnt = 0; i < hose->region_count; i++) {
369 if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY)
370 continue;
371
372 writel(0, priv->regs + PCIEPTCTLR(cnt));
373 writel((hose->regions[i].size - 1) & ~0x7f,
374 priv->regs + PCIEPAMR(cnt));
375 writel(upper_32_bits(hose->regions[i].phys_start),
376 priv->regs + PCIEPAUR(cnt));
377 writel(lower_32_bits(hose->regions[i].phys_start),
378 priv->regs + PCIEPALR(cnt));
379 mask = PAR_ENABLE;
380 if (hose->regions[i].flags == PCI_REGION_IO)
381 mask |= IO_SPACE;
382 writel(mask, priv->regs + PCIEPTCTLR(cnt));
383
384 cnt++;
385 }
386
387 return 0;
388}
389
Simon Glassaad29ae2020-12-03 16:55:21 -0700390static int rcar_gen3_pcie_of_to_plat(struct udevice *dev)
Marek Vasut879b4a32018-10-16 12:49:19 +0200391{
Simon Glassfa20e932020-12-03 16:55:20 -0700392 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
Marek Vasut879b4a32018-10-16 12:49:19 +0200393
394 priv->regs = devfdt_get_addr_index(dev, 0);
395 if (!priv->regs)
396 return -EINVAL;
397
398 return 0;
399}
400
401static const struct dm_pci_ops rcar_gen3_pcie_ops = {
402 .read_config = rcar_gen3_pcie_read_config,
403 .write_config = rcar_gen3_pcie_write_config,
404};
405
406static const struct udevice_id rcar_gen3_pcie_ids[] = {
407 { .compatible = "renesas,pcie-rcar-gen3" },
408 { }
409};
410
411U_BOOT_DRIVER(rcar_gen3_pcie) = {
412 .name = "rcar_gen3_pcie",
413 .id = UCLASS_PCI,
414 .of_match = rcar_gen3_pcie_ids,
415 .ops = &rcar_gen3_pcie_ops,
416 .probe = rcar_gen3_pcie_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700417 .of_to_plat = rcar_gen3_pcie_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700418 .plat_auto = sizeof(struct rcar_gen3_pcie_priv),
Marek Vasut879b4a32018-10-16 12:49:19 +0200419};