common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / pci / pcie_intel_fpga.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel FPGA PCIe host controller driver
4  *
5  * Copyright (C) 2013-2018 Intel Corporation. All rights reserved
6  *
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <pci.h>
12 #include <asm/io.h>
13 #include <dm/device_compat.h>
14 #include <linux/delay.h>
15
16 #define RP_TX_REG0                      0x2000
17 #define RP_TX_CNTRL                     0x2004
18 #define RP_TX_SOP                       BIT(0)
19 #define RP_TX_EOP                       BIT(1)
20 #define RP_RXCPL_STATUS                 0x200C
21 #define RP_RXCPL_SOP                    BIT(0)
22 #define RP_RXCPL_EOP                    BIT(1)
23 #define RP_RXCPL_REG                    0x2008
24 #define P2A_INT_STATUS                  0x3060
25 #define P2A_INT_STS_ALL                 0xf
26 #define P2A_INT_ENABLE                  0x3070
27 #define RP_CAP_OFFSET                   0x70
28
29 /* TLP configuration type 0 and 1 */
30 #define TLP_FMTTYPE_CFGRD0              0x04    /* Configuration Read Type 0 */
31 #define TLP_FMTTYPE_CFGWR0              0x44    /* Configuration Write Type 0 */
32 #define TLP_FMTTYPE_CFGRD1              0x05    /* Configuration Read Type 1 */
33 #define TLP_FMTTYPE_CFGWR1              0x45    /* Configuration Write Type 1 */
34 #define TLP_PAYLOAD_SIZE                0x01
35 #define TLP_READ_TAG                    0x1d
36 #define TLP_WRITE_TAG                   0x10
37 #define RP_DEVFN                        0
38
39 #define RP_CFG_ADDR(pcie, reg)                                          \
40                 ((pcie->hip_base) + (reg) + (1 << 20))
41 #define RP_SECONDARY(pcie)                                              \
42         readb(RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
43 #define TLP_REQ_ID(bus, devfn)          (((bus) << 8) | (devfn))
44
45 #define TLP_CFGRD_DW0(pcie, bus)                                        \
46         ((((bus > RP_SECONDARY(pcie)) ? TLP_FMTTYPE_CFGRD1              \
47                                       : TLP_FMTTYPE_CFGRD0) << 24) |    \
48                                         TLP_PAYLOAD_SIZE)
49
50 #define TLP_CFGWR_DW0(pcie, bus)                                        \
51         ((((bus > RP_SECONDARY(pcie)) ? TLP_FMTTYPE_CFGWR1              \
52                                       : TLP_FMTTYPE_CFGWR0) << 24) |    \
53                                         TLP_PAYLOAD_SIZE)
54
55 #define TLP_CFG_DW1(pcie, tag, be)                                      \
56         (((TLP_REQ_ID(pcie->first_busno,  RP_DEVFN)) << 16) | (tag << 8) | (be))
57 #define TLP_CFG_DW2(bus, dev, fn, offset)                               \
58         (((bus) << 24) | ((dev) << 19) | ((fn) << 16) | (offset))
59
60 #define TLP_COMP_STATUS(s)              (((s) >> 13) & 7)
61 #define TLP_BYTE_COUNT(s)               (((s) >> 0) & 0xfff)
62 #define TLP_HDR_SIZE                    3
63 #define TLP_LOOP                        20000
64 #define DWORD_MASK                      3
65
66 #define IS_ROOT_PORT(pcie, bdf)                         \
67                 ((PCI_BUS(bdf) == pcie->first_busno) ? true : false)
68
69 #define PCI_EXP_LNKSTA          18      /* Link Status */
70 #define PCI_EXP_LNKSTA_DLLLA    0x2000  /* Data Link Layer Link Active */
71
72 /**
73  * struct intel_fpga_pcie - Intel FPGA PCIe controller state
74  * @bus: Pointer to the PCI bus
75  * @cra_base: The base address of CRA register space
76  * @hip_base: The base address of Rootport configuration space
77  * @first_busno: This driver supports multiple PCIe controllers.
78  *               first_busno stores the bus number of the PCIe root-port
79  *               number which may vary depending on the PCIe setup.
80  */
81 struct intel_fpga_pcie {
82         struct udevice *bus;
83         void __iomem *cra_base;
84         void __iomem *hip_base;
85         int first_busno;
86 };
87
88 /**
89  * Intel FPGA PCIe port uses BAR0 of RC's configuration space as the
90  * translation from PCI bus to native BUS. Entire DDR region is mapped
91  * into PCIe space using these registers, so it can be reached by DMA from
92  * EP devices.
93  * The BAR0 of bridge should be hidden during enumeration to avoid the
94  * sizing and resource allocation by PCIe core.
95  */
96 static bool intel_fpga_pcie_hide_rc_bar(struct intel_fpga_pcie *pcie,
97                                         pci_dev_t bdf, int offset)
98 {
99         if (IS_ROOT_PORT(pcie, bdf) && PCI_DEV(bdf) == 0 &&
100             PCI_FUNC(bdf) == 0 && offset == PCI_BASE_ADDRESS_0)
101                 return true;
102
103         return false;
104 }
105
106 static inline void cra_writel(struct intel_fpga_pcie *pcie, const u32 value,
107                               const u32 reg)
108 {
109         writel(value, pcie->cra_base + reg);
110 }
111
112 static inline u32 cra_readl(struct intel_fpga_pcie *pcie, const u32 reg)
113 {
114         return readl(pcie->cra_base + reg);
115 }
116
117 static bool intel_fpga_pcie_link_up(struct intel_fpga_pcie *pcie)
118 {
119         return !!(readw(RP_CFG_ADDR(pcie, RP_CAP_OFFSET + PCI_EXP_LNKSTA))
120                         & PCI_EXP_LNKSTA_DLLLA);
121 }
122
123 static bool intel_fpga_pcie_addr_valid(struct intel_fpga_pcie *pcie,
124                                        pci_dev_t bdf)
125 {
126         /* If there is no link, then there is no device */
127         if (!IS_ROOT_PORT(pcie, bdf) && !intel_fpga_pcie_link_up(pcie))
128                 return false;
129
130         /* access only one slot on each root port */
131         if (IS_ROOT_PORT(pcie, bdf) && PCI_DEV(bdf) > 0)
132                 return false;
133
134         if ((PCI_BUS(bdf) == pcie->first_busno + 1) && PCI_DEV(bdf) > 0)
135                 return false;
136
137         return true;
138 }
139
140 static void tlp_write_tx(struct intel_fpga_pcie *pcie, u32 reg0, u32 ctrl)
141 {
142         cra_writel(pcie, reg0, RP_TX_REG0);
143         cra_writel(pcie, ctrl, RP_TX_CNTRL);
144 }
145
146 static int tlp_read_packet(struct intel_fpga_pcie *pcie, u32 *value)
147 {
148         int i;
149         u32 ctrl;
150         u32 comp_status;
151         u32 dw[4];
152         u32 count = 0;
153
154         for (i = 0; i < TLP_LOOP; i++) {
155                 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
156                 if (!(ctrl & RP_RXCPL_SOP))
157                         continue;
158
159                 /* read first DW */
160                 dw[count++] = cra_readl(pcie, RP_RXCPL_REG);
161
162                 /* Poll for EOP */
163                 for (i = 0; i < TLP_LOOP; i++) {
164                         ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
165                         dw[count++] = cra_readl(pcie, RP_RXCPL_REG);
166                         if (ctrl & RP_RXCPL_EOP) {
167                                 comp_status = TLP_COMP_STATUS(dw[1]);
168                                 if (comp_status) {
169                                         *value = pci_get_ff(PCI_SIZE_32);
170                                         return 0;
171                                 }
172
173                                 if (value &&
174                                     TLP_BYTE_COUNT(dw[1]) == sizeof(u32) &&
175                                     count >= 3)
176                                         *value = dw[3];
177
178                                 return 0;
179                         }
180                 }
181
182                 udelay(5);
183         }
184
185         dev_err(pcie->dev, "read TLP packet timed out\n");
186         return -ENODEV;
187 }
188
189 static void tlp_write_packet(struct intel_fpga_pcie *pcie, u32 *headers,
190                              u32 data)
191 {
192         tlp_write_tx(pcie, headers[0], RP_TX_SOP);
193
194         tlp_write_tx(pcie, headers[1], 0);
195
196         tlp_write_tx(pcie, headers[2], 0);
197
198         tlp_write_tx(pcie, data, RP_TX_EOP);
199 }
200
201 static int tlp_cfg_dword_read(struct intel_fpga_pcie *pcie, pci_dev_t bdf,
202                               int offset, u8 byte_en, u32 *value)
203 {
204         u32 headers[TLP_HDR_SIZE];
205         u8 busno = PCI_BUS(bdf);
206
207         headers[0] = TLP_CFGRD_DW0(pcie, busno);
208         headers[1] = TLP_CFG_DW1(pcie, TLP_READ_TAG, byte_en);
209         headers[2] = TLP_CFG_DW2(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
210
211         tlp_write_packet(pcie, headers, 0);
212
213         return tlp_read_packet(pcie, value);
214 }
215
216 static int tlp_cfg_dword_write(struct intel_fpga_pcie *pcie, pci_dev_t bdf,
217                                int offset, u8 byte_en, u32 value)
218 {
219         u32 headers[TLP_HDR_SIZE];
220         u8 busno = PCI_BUS(bdf);
221
222         headers[0] = TLP_CFGWR_DW0(pcie, busno);
223         headers[1] = TLP_CFG_DW1(pcie, TLP_WRITE_TAG, byte_en);
224         headers[2] = TLP_CFG_DW2(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
225
226         tlp_write_packet(pcie, headers, value);
227
228         return tlp_read_packet(pcie, NULL);
229 }
230
231 int intel_fpga_rp_conf_addr(const struct udevice *bus, pci_dev_t bdf,
232                             uint offset, void **paddress)
233 {
234         struct intel_fpga_pcie *pcie = dev_get_priv(bus);
235
236         *paddress = RP_CFG_ADDR(pcie, offset);
237
238         return 0;
239 }
240
241 static int intel_fpga_pcie_rp_rd_conf(struct udevice *bus, pci_dev_t bdf,
242                                       uint offset, ulong *valuep,
243                                       enum pci_size_t size)
244 {
245         return pci_generic_mmap_read_config(bus, intel_fpga_rp_conf_addr,
246                                             bdf, offset, valuep, size);
247 }
248
249 static int intel_fpga_pcie_rp_wr_conf(struct udevice *bus, pci_dev_t bdf,
250                                       uint offset, ulong value,
251                                       enum pci_size_t size)
252 {
253         int ret;
254         struct intel_fpga_pcie *pcie = dev_get_priv(bus);
255
256         ret = pci_generic_mmap_write_config(bus, intel_fpga_rp_conf_addr,
257                                             bdf, offset, value, size);
258         if (!ret) {
259                 /* Monitor changes to PCI_PRIMARY_BUS register on root port
260                  * and update local copy of root bus number accordingly.
261                  */
262                 if (offset == PCI_PRIMARY_BUS)
263                         pcie->first_busno = (u8)(value);
264         }
265
266         return ret;
267 }
268
269 static u8 pcie_get_byte_en(uint offset, enum pci_size_t size)
270 {
271         switch (size) {
272         case PCI_SIZE_8:
273                 return 1 << (offset & 3);
274         case PCI_SIZE_16:
275                 return 3 << (offset & 3);
276         default:
277                 return 0xf;
278         }
279 }
280
281 static int _pcie_intel_fpga_read_config(struct intel_fpga_pcie *pcie,
282                                         pci_dev_t bdf, uint offset,
283                                         ulong *valuep, enum pci_size_t size)
284 {
285         int ret;
286         u32 data;
287         u8 byte_en;
288
289         /* Uses memory mapped method to read rootport config registers */
290         if (IS_ROOT_PORT(pcie, bdf))
291                 return intel_fpga_pcie_rp_rd_conf(pcie->bus, bdf,
292                                        offset, valuep, size);
293
294         byte_en = pcie_get_byte_en(offset, size);
295         ret = tlp_cfg_dword_read(pcie, bdf, offset & ~DWORD_MASK,
296                                  byte_en, &data);
297         if (ret)
298                 return ret;
299
300         dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
301                 offset, size, data);
302         *valuep = pci_conv_32_to_size(data, offset, size);
303
304         return 0;
305 }
306
307 static int _pcie_intel_fpga_write_config(struct intel_fpga_pcie *pcie,
308                                          pci_dev_t bdf, uint offset,
309                                          ulong value, enum pci_size_t size)
310 {
311         u32 data;
312         u8 byte_en;
313
314         dev_dbg(pcie->dev, "PCIE CFG write: (b.d.f)=(%02d.%02d.%02d)\n",
315                 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
316         dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
317                 offset, size, value);
318
319         /* Uses memory mapped method to read rootport config registers */
320         if (IS_ROOT_PORT(pcie, bdf))
321                 return intel_fpga_pcie_rp_wr_conf(pcie->bus, bdf, offset,
322                                                   value, size);
323
324         byte_en = pcie_get_byte_en(offset, size);
325         data = pci_conv_size_to_32(0, value, offset, size);
326
327         return tlp_cfg_dword_write(pcie, bdf, offset & ~DWORD_MASK,
328                                    byte_en, data);
329 }
330
331 static int pcie_intel_fpga_read_config(const struct udevice *bus, pci_dev_t bdf,
332                                        uint offset, ulong *valuep,
333                                        enum pci_size_t size)
334 {
335         struct intel_fpga_pcie *pcie = dev_get_priv(bus);
336
337         dev_dbg(pcie->dev, "PCIE CFG read:  (b.d.f)=(%02d.%02d.%02d)\n",
338                 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
339
340         if (intel_fpga_pcie_hide_rc_bar(pcie, bdf, offset)) {
341                 *valuep = (u32)pci_get_ff(size);
342                 return 0;
343         }
344
345         if (!intel_fpga_pcie_addr_valid(pcie, bdf)) {
346                 *valuep = (u32)pci_get_ff(size);
347                 return 0;
348         }
349
350         return _pcie_intel_fpga_read_config(pcie, bdf, offset, valuep, size);
351 }
352
353 static int pcie_intel_fpga_write_config(struct udevice *bus, pci_dev_t bdf,
354                                         uint offset, ulong value,
355                                         enum pci_size_t size)
356 {
357         struct intel_fpga_pcie *pcie = dev_get_priv(bus);
358
359         if (intel_fpga_pcie_hide_rc_bar(pcie, bdf, offset))
360                 return 0;
361
362         if (!intel_fpga_pcie_addr_valid(pcie, bdf))
363                 return 0;
364
365         return _pcie_intel_fpga_write_config(pcie, bdf, offset, value,
366                                           size);
367 }
368
369 static int pcie_intel_fpga_probe(struct udevice *dev)
370 {
371         struct intel_fpga_pcie *pcie = dev_get_priv(dev);
372
373         pcie->bus = pci_get_controller(dev);
374         pcie->first_busno = dev->seq;
375
376         /* clear all interrupts */
377         cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
378         /* disable all interrupts */
379         cra_writel(pcie, 0, P2A_INT_ENABLE);
380
381         return 0;
382 }
383
384 static int pcie_intel_fpga_ofdata_to_platdata(struct udevice *dev)
385 {
386         struct intel_fpga_pcie *pcie = dev_get_priv(dev);
387         struct fdt_resource reg_res;
388         int node = dev_of_offset(dev);
389         int ret;
390
391         DECLARE_GLOBAL_DATA_PTR;
392
393         ret = fdt_get_named_resource(gd->fdt_blob, node, "reg", "reg-names",
394                                      "Cra", &reg_res);
395         if (ret) {
396                 dev_err(dev, "resource \"Cra\" not found\n");
397                 return ret;
398         }
399
400         pcie->cra_base = map_physmem(reg_res.start,
401                                      fdt_resource_size(&reg_res),
402                                      MAP_NOCACHE);
403
404         ret = fdt_get_named_resource(gd->fdt_blob, node, "reg", "reg-names",
405                                      "Hip", &reg_res);
406         if (ret) {
407                 dev_err(dev, "resource \"Hip\" not found\n");
408                 return ret;
409         }
410
411         pcie->hip_base = map_physmem(reg_res.start,
412                                      fdt_resource_size(&reg_res),
413                                      MAP_NOCACHE);
414
415         return 0;
416 }
417
418 static const struct dm_pci_ops pcie_intel_fpga_ops = {
419         .read_config    = pcie_intel_fpga_read_config,
420         .write_config   = pcie_intel_fpga_write_config,
421 };
422
423 static const struct udevice_id pcie_intel_fpga_ids[] = {
424         { .compatible = "altr,pcie-root-port-2.0" },
425         {},
426 };
427
428 U_BOOT_DRIVER(pcie_intel_fpga) = {
429         .name                   = "pcie_intel_fpga",
430         .id                     = UCLASS_PCI,
431         .of_match               = pcie_intel_fpga_ids,
432         .ops                    = &pcie_intel_fpga_ops,
433         .ofdata_to_platdata     = pcie_intel_fpga_ofdata_to_platdata,
434         .probe                  = pcie_intel_fpga_probe,
435         .priv_auto_alloc_size   = sizeof(struct intel_fpga_pcie),
436 };