1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RCar Gen3 PCIEC driver
7 * Based on Linux PCIe driver for Renesas R-Car SoCs
8 * Copyright (C) 2014 Renesas Electronics Europe Ltd
11 * arch/sh/drivers/pci/pcie-sh7786.c
12 * arch/sh/drivers/pci/ops-sh7786.c
13 * Copyright (C) 2009 - 2011 Paul Mundt
25 #include <linux/bitops.h>
26 #include <linux/log2.h>
28 #define PCIECAR 0x000010
29 #define PCIECCTLR 0x000018
30 #define SEND_ENABLE BIT(31)
31 #define TYPE0 (0 << 8)
33 #define PCIECDR 0x000020
34 #define PCIEMSR 0x000028
35 #define PCIEINTXR 0x000400
36 #define PCIEPHYSR 0x0007f0
38 #define PCIEMSITXR 0x000840
40 /* Transfer control */
41 #define PCIETCTLR 0x02000
43 #define PCIETSTR 0x02004
44 #define DATA_LINK_ACTIVE 1
45 #define PCIEERRFR 0x02020
46 #define UNSUPPORTED_REQUEST BIT(4)
47 #define PCIEMSIFR 0x02044
48 #define PCIEMSIALR 0x02048
50 #define PCIEMSIAUR 0x0204c
51 #define PCIEMSIIER 0x02050
53 /* root port address */
54 #define PCIEPRAR(x) (0x02080 + ((x) * 0x4))
56 /* local address reg & mask */
57 #define PCIELAR(x) (0x02200 + ((x) * 0x20))
58 #define PCIELAMR(x) (0x02208 + ((x) * 0x20))
59 #define LAM_PREFETCH BIT(3)
60 #define LAM_64BIT BIT(2)
61 #define LAR_ENABLE BIT(1)
63 /* PCIe address reg & mask */
64 #define PCIEPALR(x) (0x03400 + ((x) * 0x20))
65 #define PCIEPAUR(x) (0x03404 + ((x) * 0x20))
66 #define PCIEPAMR(x) (0x03408 + ((x) * 0x20))
67 #define PCIEPTCTLR(x) (0x0340c + ((x) * 0x20))
68 #define PAR_ENABLE BIT(31)
69 #define IO_SPACE BIT(8)
72 #define PCICONF(x) (0x010000 + ((x) * 0x4))
73 #define PMCAP(x) (0x010040 + ((x) * 0x4))
74 #define EXPCAP(x) (0x010070 + ((x) * 0x4))
75 #define VCCAP(x) (0x010100 + ((x) * 0x4))
78 #define IDSETR1 0x011004
79 #define TLCTLR 0x011048
80 #define MACSR 0x011054
81 #define SPCHGFIN BIT(4)
82 #define SPCHGFAIL BIT(6)
83 #define SPCHGSUC BIT(7)
84 #define LINK_SPEED (0xf << 16)
85 #define LINK_SPEED_2_5GTS (1 << 16)
86 #define LINK_SPEED_5_0GTS (2 << 16)
87 #define MACCTLR 0x011058
88 #define SPEED_CHANGE BIT(24)
89 #define SCRAMBLE_DISABLE BIT(27)
90 #define MACS2R 0x011078
91 #define MACCGSPSETR 0x011084
92 #define SPCNGRSN BIT(31)
95 #define H1_PCIEPHYADRR 0x04000c
96 #define WRITE_CMD BIT(16)
97 #define PHY_ACK BIT(24)
101 #define H1_PCIEPHYDOUTR 0x040014
104 #define GEN2_PCIEPHYADDR 0x780
105 #define GEN2_PCIEPHYDATA 0x784
106 #define GEN2_PCIEPHYCTRL 0x78c
108 #define INT_PCI_MSI_NR 32
110 #define RCONF(x) (PCICONF(0) + (x))
111 #define RPMCAP(x) (PMCAP(0) + (x))
112 #define REXPCAP(x) (EXPCAP(0) + (x))
113 #define RVCCAP(x) (VCCAP(0) + (x))
115 #define PCIE_CONF_BUS(b) (((b) & 0xff) << 24)
116 #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 19)
117 #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 16)
119 #define RCAR_PCI_MAX_RESOURCES 4
120 #define MAX_NR_INBOUND_MAPS 6
123 RCAR_PCI_ACCESS_READ,
124 RCAR_PCI_ACCESS_WRITE,
127 struct rcar_gen3_pcie_priv {
131 static void rcar_rmw32(struct udevice *dev, int where, u32 mask, u32 data)
133 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
134 int shift = 8 * (where & 3);
136 clrsetbits_le32(priv->regs + (where & ~3),
137 mask << shift, data << shift);
140 static u32 rcar_read_conf(const struct udevice *dev, int where)
142 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
143 int shift = 8 * (where & 3);
145 return readl(priv->regs + (where & ~3)) >> shift;
148 static int rcar_pcie_config_access(const struct udevice *udev,
149 unsigned char access_type,
150 pci_dev_t bdf, int where, ulong *data)
152 struct rcar_gen3_pcie_priv *priv = dev_get_plat(udev);
153 u32 reg = where & ~3;
156 if (PCI_DEV(bdf) == 0) {
157 if (access_type == RCAR_PCI_ACCESS_READ)
158 *data = readl(priv->regs + PCICONF(where / 4));
160 writel(*data, priv->regs + PCICONF(where / 4));
166 clrbits_le32(priv->regs + PCIEERRFR, 0);
168 /* Set the PIO address */
169 writel((bdf << 8) | reg, priv->regs + PCIECAR);
171 /* Enable the configuration access */
173 writel(SEND_ENABLE | TYPE0, priv->regs + PCIECCTLR);
175 writel(SEND_ENABLE | TYPE1, priv->regs + PCIECCTLR);
177 /* Check for errors */
178 if (readl(priv->regs + PCIEERRFR) & UNSUPPORTED_REQUEST)
181 /* Check for master and target aborts */
182 if (rcar_read_conf(udev, RCONF(PCI_STATUS)) &
183 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
186 if (access_type == RCAR_PCI_ACCESS_READ)
187 *data = readl(priv->regs + PCIECDR);
189 writel(*data, priv->regs + PCIECDR);
191 /* Disable the configuration access */
192 writel(0, priv->regs + PCIECCTLR);
197 static int rcar_gen3_pcie_addr_valid(pci_dev_t d, uint where)
214 static int rcar_gen3_pcie_read_config(const struct udevice *dev, pci_dev_t bdf,
215 uint where, ulong *val,
216 enum pci_size_t size)
221 ret = rcar_gen3_pcie_addr_valid(bdf, where);
223 *val = pci_get_ff(size);
227 ret = rcar_pcie_config_access(dev, RCAR_PCI_ACCESS_READ,
232 *val = pci_conv_32_to_size(reg, where, size);
237 static int rcar_gen3_pcie_write_config(struct udevice *dev, pci_dev_t bdf,
238 uint where, ulong val,
239 enum pci_size_t size)
244 ret = rcar_gen3_pcie_addr_valid(bdf, where);
248 data = pci_conv_32_to_size(val, where, size);
250 ret = rcar_pcie_config_access(dev, RCAR_PCI_ACCESS_WRITE,
256 static int rcar_gen3_pcie_wait_for_phyrdy(struct udevice *dev)
258 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
260 return wait_for_bit_le32((void *)priv->regs + PCIEPHYSR, PHYRDY,
264 static int rcar_gen3_pcie_wait_for_dl(struct udevice *dev)
266 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
268 return wait_for_bit_le32((void *)priv->regs + PCIETSTR,
269 DATA_LINK_ACTIVE, true, 50, false);
272 static int rcar_gen3_pcie_hw_init(struct udevice *dev)
274 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
277 /* Begin initialization */
278 writel(0, priv->regs + PCIETCTLR);
281 writel(1, priv->regs + PCIEMSR);
283 ret = rcar_gen3_pcie_wait_for_phyrdy(dev);
288 * Initial header for port config space is type 1, set the device
289 * class to match. Hardware takes care of propagating the IDSETR
290 * settings, so there is no need to bother with a quirk.
292 writel(PCI_CLASS_BRIDGE_PCI_NORMAL << 8, priv->regs + IDSETR1);
295 * Setup Secondary Bus Number & Subordinate Bus Number, even though
296 * they aren't used, to avoid bridge being detected as broken.
298 rcar_rmw32(dev, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
299 rcar_rmw32(dev, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
301 /* Initialize default capabilities. */
302 rcar_rmw32(dev, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
303 rcar_rmw32(dev, REXPCAP(PCI_EXP_FLAGS),
304 PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
305 rcar_rmw32(dev, RCONF(PCI_HEADER_TYPE), 0x7f,
306 PCI_HEADER_TYPE_BRIDGE);
308 /* Enable data link layer active state reporting */
309 rcar_rmw32(dev, REXPCAP(PCI_EXP_LNKCAP),
310 PCI_EXP_LNKCAP_DLLLARC, PCI_EXP_LNKCAP_DLLLARC);
312 /* Write out the physical slot number = 0 */
313 rcar_rmw32(dev, REXPCAP(PCI_EXP_SLTCAP),
314 PCI_EXP_SLTCAP_PSN, 0);
316 /* Set the completion timer timeout to the maximum 50ms. */
317 rcar_rmw32(dev, TLCTLR + 1, 0x3f, 50);
319 /* Terminate list of capabilities (Next Capability Offset=0) */
320 rcar_rmw32(dev, RVCCAP(0), 0xfff00000, 0);
322 /* Finish initialization - establish a PCI Express link */
323 writel(CFINIT, priv->regs + PCIETCTLR);
325 return rcar_gen3_pcie_wait_for_dl(dev);
328 static int rcar_gen3_pcie_probe(struct udevice *dev)
330 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
331 struct pci_controller *hose = dev_get_uclass_priv(dev);
336 ret = clk_get_by_index(dev, 0, &pci_clk);
340 ret = clk_enable(&pci_clk);
344 for (i = 0; i < hose->region_count; i++) {
345 if (hose->regions[i].flags != PCI_REGION_SYS_MEMORY)
348 if (hose->regions[i].phys_start == 0)
351 mask = (roundup_pow_of_two(hose->regions[i].size) - 1) & ~0xf;
353 writel(rounddown_pow_of_two(hose->regions[i].phys_start),
354 priv->regs + PCIEPRAR(0));
355 writel(rounddown_pow_of_two(hose->regions[i].phys_start),
356 priv->regs + PCIELAR(0));
357 writel(mask, priv->regs + PCIELAMR(0));
361 writel(0, priv->regs + PCIEPRAR(1));
362 writel(0, priv->regs + PCIELAR(1));
363 writel(0, priv->regs + PCIELAMR(1));
365 ret = rcar_gen3_pcie_hw_init(dev);
369 for (i = 0, cnt = 0; i < hose->region_count; i++) {
370 if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY)
373 writel(0, priv->regs + PCIEPTCTLR(cnt));
374 writel((hose->regions[i].size - 1) & ~0x7f,
375 priv->regs + PCIEPAMR(cnt));
376 writel(upper_32_bits(hose->regions[i].phys_start),
377 priv->regs + PCIEPAUR(cnt));
378 writel(lower_32_bits(hose->regions[i].phys_start),
379 priv->regs + PCIEPALR(cnt));
381 if (hose->regions[i].flags == PCI_REGION_IO)
383 writel(mask, priv->regs + PCIEPTCTLR(cnt));
391 static int rcar_gen3_pcie_of_to_plat(struct udevice *dev)
393 struct rcar_gen3_pcie_priv *priv = dev_get_plat(dev);
395 priv->regs = devfdt_get_addr_index(dev, 0);
402 static const struct dm_pci_ops rcar_gen3_pcie_ops = {
403 .read_config = rcar_gen3_pcie_read_config,
404 .write_config = rcar_gen3_pcie_write_config,
407 static const struct udevice_id rcar_gen3_pcie_ids[] = {
408 { .compatible = "renesas,pcie-rcar-gen3" },
412 U_BOOT_DRIVER(rcar_gen3_pcie) = {
413 .name = "rcar_gen3_pcie",
415 .of_match = rcar_gen3_pcie_ids,
416 .ops = &rcar_gen3_pcie_ops,
417 .probe = rcar_gen3_pcie_probe,
418 .of_to_plat = rcar_gen3_pcie_of_to_plat,
419 .plat_auto = sizeof(struct rcar_gen3_pcie_priv),