mtd: rawnand: denali: deassert write protect pin
[oweals/u-boot.git] / drivers / pci / pci-rcar-gen3.c
1 // 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
18 #include <common.h>
19 #include <asm/io.h>
20 #include <clk.h>
21 #include <dm.h>
22 #include <errno.h>
23 #include <pci.h>
24 #include <wait_bit.h>
25 #include <linux/bitops.h>
26
27 #define PCIECAR                 0x000010
28 #define PCIECCTLR               0x000018
29 #define  CONFIG_SEND_ENABLE     BIT(31)
30 #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
121 #define PCI_EXP_FLAGS           2               /* Capabilities register */
122 #define PCI_EXP_FLAGS_TYPE      0x00f0          /* Device/Port type */
123 #define PCI_EXP_TYPE_ROOT_PORT  0x4             /* Root Port */
124 #define PCI_EXP_LNKCAP          12              /* Link Capabilities */
125 #define PCI_EXP_LNKCAP_DLLLARC  0x00100000      /* Data Link Layer Link Active Reporting Capable */
126 #define PCI_EXP_SLTCAP          20              /* Slot Capabilities */
127 #define PCI_EXP_SLTCAP_PSN      0xfff80000      /* Physical Slot Number */
128
129 enum {
130         RCAR_PCI_ACCESS_READ,
131         RCAR_PCI_ACCESS_WRITE,
132 };
133
134 struct rcar_gen3_pcie_priv {
135         fdt_addr_t              regs;
136 };
137
138 static void rcar_rmw32(struct udevice *dev, int where, u32 mask, u32 data)
139 {
140         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
141         int shift = 8 * (where & 3);
142
143         clrsetbits_le32(priv->regs + (where & ~3),
144                         mask << shift, data << shift);
145 }
146
147 static u32 rcar_read_conf(const struct udevice *dev, int where)
148 {
149         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
150         int shift = 8 * (where & 3);
151
152         return readl(priv->regs + (where & ~3)) >> shift;
153 }
154
155 static int rcar_pcie_config_access(const struct udevice *udev,
156                                    unsigned char access_type,
157                                    pci_dev_t bdf, int where, ulong *data)
158 {
159         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(udev);
160         u32 reg = where & ~3;
161
162         /* Clear errors */
163         clrbits_le32(priv->regs + PCIEERRFR, 0);
164
165         /* Set the PIO address */
166         writel((bdf << 8) | reg, priv->regs + PCIECAR);
167
168         /* Enable the configuration access */
169         if (!PCI_BUS(bdf))
170                 writel(CONFIG_SEND_ENABLE | TYPE0, priv->regs + PCIECCTLR);
171         else
172                 writel(CONFIG_SEND_ENABLE | TYPE1, priv->regs + PCIECCTLR);
173
174         /* Check for errors */
175         if (readl(priv->regs + PCIEERRFR) & UNSUPPORTED_REQUEST)
176                 return -ENODEV;
177
178         /* Check for master and target aborts */
179         if (rcar_read_conf(udev, RCONF(PCI_STATUS)) &
180                 (PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT))
181                 return -ENODEV;
182
183         if (access_type == RCAR_PCI_ACCESS_READ)
184                 *data = readl(priv->regs + PCIECDR);
185         else
186                 writel(*data, priv->regs + PCIECDR);
187
188         /* Disable the configuration access */
189         writel(0, priv->regs + PCIECCTLR);
190
191         return 0;
192 }
193
194 static int rcar_gen3_pcie_addr_valid(pci_dev_t d, uint where)
195 {
196         u32 slot;
197
198         if (PCI_FUNC(d))
199                 return -EINVAL;
200
201         slot = PCI_DEV(d);
202         if (slot != 1)
203                 return -EINVAL;
204
205         return 0;
206 }
207
208 static int rcar_gen3_pcie_read_config(const struct udevice *dev, pci_dev_t bdf,
209                                       uint where, ulong *val,
210                                       enum pci_size_t size)
211 {
212         ulong reg;
213         int ret;
214
215         ret = rcar_gen3_pcie_addr_valid(bdf, where);
216         if (ret) {
217                 *val = pci_get_ff(size);
218                 return 0;
219         }
220
221         ret = rcar_pcie_config_access(dev, RCAR_PCI_ACCESS_READ,
222                                       bdf, where, &reg);
223         if (ret != 0)
224                 reg = 0xffffffffUL;
225
226         *val = pci_conv_32_to_size(reg, where, size);
227
228         return ret;
229 }
230
231 static int rcar_gen3_pcie_write_config(struct udevice *dev, pci_dev_t bdf,
232                                        uint where, ulong val,
233                                        enum pci_size_t size)
234 {
235         ulong data;
236         int ret;
237
238         ret = rcar_gen3_pcie_addr_valid(bdf, where);
239         if (ret)
240                 return ret;
241
242         data = pci_conv_32_to_size(val, where, size);
243
244         ret = rcar_pcie_config_access(dev, RCAR_PCI_ACCESS_WRITE,
245                                       bdf, where, &data);
246
247         return ret;
248 }
249
250 static int rcar_gen3_pcie_wait_for_phyrdy(struct udevice *dev)
251 {
252         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
253
254         return wait_for_bit_le32((void *)priv->regs + PCIEPHYSR, PHYRDY,
255                                  true, 50, false);
256 }
257
258 static int rcar_gen3_pcie_wait_for_dl(struct udevice *dev)
259 {
260         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
261
262         return wait_for_bit_le32((void *)priv->regs + PCIETSTR,
263                                  DATA_LINK_ACTIVE, true, 50, false);
264 }
265
266 static int rcar_gen3_pcie_hw_init(struct udevice *dev)
267 {
268         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
269         int ret;
270
271         /* Begin initialization */
272         writel(0, priv->regs + PCIETCTLR);
273
274         /* Set mode */
275         writel(1, priv->regs + PCIEMSR);
276
277         ret = rcar_gen3_pcie_wait_for_phyrdy(dev);
278         if (ret)
279                 return ret;
280
281         /*
282          * Initial header for port config space is type 1, set the device
283          * class to match. Hardware takes care of propagating the IDSETR
284          * settings, so there is no need to bother with a quirk.
285          */
286         writel(PCI_CLASS_BRIDGE_PCI << 16, priv->regs + IDSETR1);
287
288         /*
289          * Setup Secondary Bus Number & Subordinate Bus Number, even though
290          * they aren't used, to avoid bridge being detected as broken.
291          */
292         rcar_rmw32(dev, RCONF(PCI_SECONDARY_BUS), 0xff, 1);
293         rcar_rmw32(dev, RCONF(PCI_SUBORDINATE_BUS), 0xff, 1);
294
295         /* Initialize default capabilities. */
296         rcar_rmw32(dev, REXPCAP(0), 0xff, PCI_CAP_ID_EXP);
297         rcar_rmw32(dev, REXPCAP(PCI_EXP_FLAGS),
298                    PCI_EXP_FLAGS_TYPE, PCI_EXP_TYPE_ROOT_PORT << 4);
299         rcar_rmw32(dev, RCONF(PCI_HEADER_TYPE), 0x7f,
300                    PCI_HEADER_TYPE_BRIDGE);
301
302         /* Enable data link layer active state reporting */
303         rcar_rmw32(dev, REXPCAP(PCI_EXP_LNKCAP),
304                    PCI_EXP_LNKCAP_DLLLARC, PCI_EXP_LNKCAP_DLLLARC);
305
306         /* Write out the physical slot number = 0 */
307         rcar_rmw32(dev, REXPCAP(PCI_EXP_SLTCAP),
308                    PCI_EXP_SLTCAP_PSN, 0);
309
310         /* Set the completion timer timeout to the maximum 50ms. */
311         rcar_rmw32(dev, TLCTLR + 1, 0x3f, 50);
312
313         /* Terminate list of capabilities (Next Capability Offset=0) */
314         rcar_rmw32(dev, RVCCAP(0), 0xfff00000, 0);
315
316         /* Finish initialization - establish a PCI Express link */
317         writel(CFINIT, priv->regs + PCIETCTLR);
318
319         return rcar_gen3_pcie_wait_for_dl(dev);
320 }
321
322 static int rcar_gen3_pcie_probe(struct udevice *dev)
323 {
324         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
325         struct pci_controller *hose = dev_get_uclass_priv(dev);
326         struct clk pci_clk;
327         u32 mask;
328         int i, cnt, ret;
329
330         ret = clk_get_by_index(dev, 0, &pci_clk);
331         if (ret)
332                 return ret;
333
334         ret = clk_enable(&pci_clk);
335         if (ret)
336                 return ret;
337
338         for (i = 0; i < hose->region_count; i++) {
339                 if (hose->regions[i].flags != PCI_REGION_SYS_MEMORY)
340                         continue;
341
342                 if (hose->regions[i].phys_start == 0)
343                         continue;
344
345                 mask = (hose->regions[i].size - 1) & ~0xf;
346                 mask |= LAR_ENABLE;
347                 writel(hose->regions[i].phys_start, priv->regs + PCIEPRAR(0));
348                 writel(hose->regions[i].phys_start, priv->regs + PCIELAR(0));
349                 writel(mask, priv->regs + PCIELAMR(0));
350                 break;
351         }
352
353         writel(0, priv->regs + PCIEPRAR(4));
354         writel(0, priv->regs + PCIELAR(4));
355         writel(0, priv->regs + PCIELAMR(4));
356
357         ret = rcar_gen3_pcie_hw_init(dev);
358         if (ret)
359                 return ret;
360
361         for (i = 0, cnt = 0; i < hose->region_count; i++) {
362                 if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY)
363                         continue;
364
365                 writel(0, priv->regs + PCIEPTCTLR(cnt));
366                 writel((hose->regions[i].size - 1) & ~0x7f,
367                        priv->regs + PCIEPAMR(cnt));
368                 writel(upper_32_bits(hose->regions[i].phys_start),
369                        priv->regs + PCIEPAUR(cnt));
370                 writel(lower_32_bits(hose->regions[i].phys_start),
371                        priv->regs + PCIEPALR(cnt));
372                 mask = PAR_ENABLE;
373                 if (hose->regions[i].flags == PCI_REGION_IO)
374                         mask |= IO_SPACE;
375                 writel(mask, priv->regs + PCIEPTCTLR(cnt));
376
377                 cnt++;
378         }
379
380         return 0;
381 }
382
383 static int rcar_gen3_pcie_ofdata_to_platdata(struct udevice *dev)
384 {
385         struct rcar_gen3_pcie_priv *priv = dev_get_platdata(dev);
386
387         priv->regs = devfdt_get_addr_index(dev, 0);
388         if (!priv->regs)
389                 return -EINVAL;
390
391         return 0;
392 }
393
394 static const struct dm_pci_ops rcar_gen3_pcie_ops = {
395         .read_config    = rcar_gen3_pcie_read_config,
396         .write_config   = rcar_gen3_pcie_write_config,
397 };
398
399 static const struct udevice_id rcar_gen3_pcie_ids[] = {
400         { .compatible = "renesas,pcie-rcar-gen3" },
401         { }
402 };
403
404 U_BOOT_DRIVER(rcar_gen3_pcie) = {
405         .name                   = "rcar_gen3_pcie",
406         .id                     = UCLASS_PCI,
407         .of_match               = rcar_gen3_pcie_ids,
408         .ops                    = &rcar_gen3_pcie_ops,
409         .probe                  = rcar_gen3_pcie_probe,
410         .ofdata_to_platdata     = rcar_gen3_pcie_ofdata_to_platdata,
411         .platdata_auto_alloc_size = sizeof(struct rcar_gen3_pcie_priv),
412 };