1 // SPDX-License-Identifier: GPL-2.0
3 * Intel FPGA PCIe host controller driver
5 * Copyright (C) 2013-2018 Intel Corporation. All rights reserved
13 #include <dm/device_compat.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
17 #define RP_TX_REG0 0x2000
18 #define RP_TX_CNTRL 0x2004
19 #define RP_TX_SOP BIT(0)
20 #define RP_TX_EOP BIT(1)
21 #define RP_RXCPL_STATUS 0x200C
22 #define RP_RXCPL_SOP BIT(0)
23 #define RP_RXCPL_EOP BIT(1)
24 #define RP_RXCPL_REG 0x2008
25 #define P2A_INT_STATUS 0x3060
26 #define P2A_INT_STS_ALL 0xf
27 #define P2A_INT_ENABLE 0x3070
28 #define RP_CAP_OFFSET 0x70
30 /* TLP configuration type 0 and 1 */
31 #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */
32 #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */
33 #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */
34 #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */
35 #define TLP_PAYLOAD_SIZE 0x01
36 #define TLP_READ_TAG 0x1d
37 #define TLP_WRITE_TAG 0x10
40 #define RP_CFG_ADDR(pcie, reg) \
41 ((pcie->hip_base) + (reg) + (1 << 20))
42 #define RP_SECONDARY(pcie) \
43 readb(RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
44 #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn))
46 #define TLP_CFGRD_DW0(pcie, bus) \
47 ((((bus > RP_SECONDARY(pcie)) ? TLP_FMTTYPE_CFGRD1 \
48 : TLP_FMTTYPE_CFGRD0) << 24) | \
51 #define TLP_CFGWR_DW0(pcie, bus) \
52 ((((bus > RP_SECONDARY(pcie)) ? TLP_FMTTYPE_CFGWR1 \
53 : TLP_FMTTYPE_CFGWR0) << 24) | \
56 #define TLP_CFG_DW1(pcie, tag, be) \
57 (((TLP_REQ_ID(pcie->first_busno, RP_DEVFN)) << 16) | (tag << 8) | (be))
58 #define TLP_CFG_DW2(bus, dev, fn, offset) \
59 (((bus) << 24) | ((dev) << 19) | ((fn) << 16) | (offset))
61 #define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
62 #define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff)
63 #define TLP_HDR_SIZE 3
64 #define TLP_LOOP 20000
67 #define IS_ROOT_PORT(pcie, bdf) \
68 ((PCI_BUS(bdf) == pcie->first_busno) ? true : false)
70 #define PCI_EXP_LNKSTA 18 /* Link Status */
71 #define PCI_EXP_LNKSTA_DLLLA 0x2000 /* Data Link Layer Link Active */
74 * struct intel_fpga_pcie - Intel FPGA PCIe controller state
75 * @bus: Pointer to the PCI bus
76 * @cra_base: The base address of CRA register space
77 * @hip_base: The base address of Rootport configuration space
78 * @first_busno: This driver supports multiple PCIe controllers.
79 * first_busno stores the bus number of the PCIe root-port
80 * number which may vary depending on the PCIe setup.
82 struct intel_fpga_pcie {
84 void __iomem *cra_base;
85 void __iomem *hip_base;
90 * Intel FPGA PCIe port uses BAR0 of RC's configuration space as the
91 * translation from PCI bus to native BUS. Entire DDR region is mapped
92 * into PCIe space using these registers, so it can be reached by DMA from
94 * The BAR0 of bridge should be hidden during enumeration to avoid the
95 * sizing and resource allocation by PCIe core.
97 static bool intel_fpga_pcie_hide_rc_bar(struct intel_fpga_pcie *pcie,
98 pci_dev_t bdf, int offset)
100 if (IS_ROOT_PORT(pcie, bdf) && PCI_DEV(bdf) == 0 &&
101 PCI_FUNC(bdf) == 0 && offset == PCI_BASE_ADDRESS_0)
107 static inline void cra_writel(struct intel_fpga_pcie *pcie, const u32 value,
110 writel(value, pcie->cra_base + reg);
113 static inline u32 cra_readl(struct intel_fpga_pcie *pcie, const u32 reg)
115 return readl(pcie->cra_base + reg);
118 static bool intel_fpga_pcie_link_up(struct intel_fpga_pcie *pcie)
120 return !!(readw(RP_CFG_ADDR(pcie, RP_CAP_OFFSET + PCI_EXP_LNKSTA))
121 & PCI_EXP_LNKSTA_DLLLA);
124 static bool intel_fpga_pcie_addr_valid(struct intel_fpga_pcie *pcie,
127 /* If there is no link, then there is no device */
128 if (!IS_ROOT_PORT(pcie, bdf) && !intel_fpga_pcie_link_up(pcie))
131 /* access only one slot on each root port */
132 if (IS_ROOT_PORT(pcie, bdf) && PCI_DEV(bdf) > 0)
135 if ((PCI_BUS(bdf) == pcie->first_busno + 1) && PCI_DEV(bdf) > 0)
141 static void tlp_write_tx(struct intel_fpga_pcie *pcie, u32 reg0, u32 ctrl)
143 cra_writel(pcie, reg0, RP_TX_REG0);
144 cra_writel(pcie, ctrl, RP_TX_CNTRL);
147 static int tlp_read_packet(struct intel_fpga_pcie *pcie, u32 *value)
155 for (i = 0; i < TLP_LOOP; i++) {
156 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
157 if (!(ctrl & RP_RXCPL_SOP))
161 dw[count++] = cra_readl(pcie, RP_RXCPL_REG);
164 for (i = 0; i < TLP_LOOP; i++) {
165 ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
166 dw[count++] = cra_readl(pcie, RP_RXCPL_REG);
167 if (ctrl & RP_RXCPL_EOP) {
168 comp_status = TLP_COMP_STATUS(dw[1]);
170 *value = pci_get_ff(PCI_SIZE_32);
175 TLP_BYTE_COUNT(dw[1]) == sizeof(u32) &&
186 dev_err(pcie->dev, "read TLP packet timed out\n");
190 static void tlp_write_packet(struct intel_fpga_pcie *pcie, u32 *headers,
193 tlp_write_tx(pcie, headers[0], RP_TX_SOP);
195 tlp_write_tx(pcie, headers[1], 0);
197 tlp_write_tx(pcie, headers[2], 0);
199 tlp_write_tx(pcie, data, RP_TX_EOP);
202 static int tlp_cfg_dword_read(struct intel_fpga_pcie *pcie, pci_dev_t bdf,
203 int offset, u8 byte_en, u32 *value)
205 u32 headers[TLP_HDR_SIZE];
206 u8 busno = PCI_BUS(bdf);
208 headers[0] = TLP_CFGRD_DW0(pcie, busno);
209 headers[1] = TLP_CFG_DW1(pcie, TLP_READ_TAG, byte_en);
210 headers[2] = TLP_CFG_DW2(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
212 tlp_write_packet(pcie, headers, 0);
214 return tlp_read_packet(pcie, value);
217 static int tlp_cfg_dword_write(struct intel_fpga_pcie *pcie, pci_dev_t bdf,
218 int offset, u8 byte_en, u32 value)
220 u32 headers[TLP_HDR_SIZE];
221 u8 busno = PCI_BUS(bdf);
223 headers[0] = TLP_CFGWR_DW0(pcie, busno);
224 headers[1] = TLP_CFG_DW1(pcie, TLP_WRITE_TAG, byte_en);
225 headers[2] = TLP_CFG_DW2(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
227 tlp_write_packet(pcie, headers, value);
229 return tlp_read_packet(pcie, NULL);
232 int intel_fpga_rp_conf_addr(const struct udevice *bus, pci_dev_t bdf,
233 uint offset, void **paddress)
235 struct intel_fpga_pcie *pcie = dev_get_priv(bus);
237 *paddress = RP_CFG_ADDR(pcie, offset);
242 static int intel_fpga_pcie_rp_rd_conf(struct udevice *bus, pci_dev_t bdf,
243 uint offset, ulong *valuep,
244 enum pci_size_t size)
246 return pci_generic_mmap_read_config(bus, intel_fpga_rp_conf_addr,
247 bdf, offset, valuep, size);
250 static int intel_fpga_pcie_rp_wr_conf(struct udevice *bus, pci_dev_t bdf,
251 uint offset, ulong value,
252 enum pci_size_t size)
255 struct intel_fpga_pcie *pcie = dev_get_priv(bus);
257 ret = pci_generic_mmap_write_config(bus, intel_fpga_rp_conf_addr,
258 bdf, offset, value, size);
260 /* Monitor changes to PCI_PRIMARY_BUS register on root port
261 * and update local copy of root bus number accordingly.
263 if (offset == PCI_PRIMARY_BUS)
264 pcie->first_busno = (u8)(value);
270 static u8 pcie_get_byte_en(uint offset, enum pci_size_t size)
274 return 1 << (offset & 3);
276 return 3 << (offset & 3);
282 static int _pcie_intel_fpga_read_config(struct intel_fpga_pcie *pcie,
283 pci_dev_t bdf, uint offset,
284 ulong *valuep, enum pci_size_t size)
290 /* Uses memory mapped method to read rootport config registers */
291 if (IS_ROOT_PORT(pcie, bdf))
292 return intel_fpga_pcie_rp_rd_conf(pcie->bus, bdf,
293 offset, valuep, size);
295 byte_en = pcie_get_byte_en(offset, size);
296 ret = tlp_cfg_dword_read(pcie, bdf, offset & ~DWORD_MASK,
301 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08x)\n",
303 *valuep = pci_conv_32_to_size(data, offset, size);
308 static int _pcie_intel_fpga_write_config(struct intel_fpga_pcie *pcie,
309 pci_dev_t bdf, uint offset,
310 ulong value, enum pci_size_t size)
315 dev_dbg(pcie->dev, "PCIE CFG write: (b.d.f)=(%02d.%02d.%02d)\n",
316 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
317 dev_dbg(pcie->dev, "(addr,size,val)=(0x%04x, %d, 0x%08lx)\n",
318 offset, size, value);
320 /* Uses memory mapped method to read rootport config registers */
321 if (IS_ROOT_PORT(pcie, bdf))
322 return intel_fpga_pcie_rp_wr_conf(pcie->bus, bdf, offset,
325 byte_en = pcie_get_byte_en(offset, size);
326 data = pci_conv_size_to_32(0, value, offset, size);
328 return tlp_cfg_dword_write(pcie, bdf, offset & ~DWORD_MASK,
332 static int pcie_intel_fpga_read_config(const struct udevice *bus, pci_dev_t bdf,
333 uint offset, ulong *valuep,
334 enum pci_size_t size)
336 struct intel_fpga_pcie *pcie = dev_get_priv(bus);
338 dev_dbg(pcie->dev, "PCIE CFG read: (b.d.f)=(%02d.%02d.%02d)\n",
339 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
341 if (intel_fpga_pcie_hide_rc_bar(pcie, bdf, offset)) {
342 *valuep = (u32)pci_get_ff(size);
346 if (!intel_fpga_pcie_addr_valid(pcie, bdf)) {
347 *valuep = (u32)pci_get_ff(size);
351 return _pcie_intel_fpga_read_config(pcie, bdf, offset, valuep, size);
354 static int pcie_intel_fpga_write_config(struct udevice *bus, pci_dev_t bdf,
355 uint offset, ulong value,
356 enum pci_size_t size)
358 struct intel_fpga_pcie *pcie = dev_get_priv(bus);
360 if (intel_fpga_pcie_hide_rc_bar(pcie, bdf, offset))
363 if (!intel_fpga_pcie_addr_valid(pcie, bdf))
366 return _pcie_intel_fpga_write_config(pcie, bdf, offset, value,
370 static int pcie_intel_fpga_probe(struct udevice *dev)
372 struct intel_fpga_pcie *pcie = dev_get_priv(dev);
374 pcie->bus = pci_get_controller(dev);
375 pcie->first_busno = dev->seq;
377 /* clear all interrupts */
378 cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
379 /* disable all interrupts */
380 cra_writel(pcie, 0, P2A_INT_ENABLE);
385 static int pcie_intel_fpga_ofdata_to_platdata(struct udevice *dev)
387 struct intel_fpga_pcie *pcie = dev_get_priv(dev);
388 struct fdt_resource reg_res;
389 int node = dev_of_offset(dev);
392 DECLARE_GLOBAL_DATA_PTR;
394 ret = fdt_get_named_resource(gd->fdt_blob, node, "reg", "reg-names",
397 dev_err(dev, "resource \"Cra\" not found\n");
401 pcie->cra_base = map_physmem(reg_res.start,
402 fdt_resource_size(®_res),
405 ret = fdt_get_named_resource(gd->fdt_blob, node, "reg", "reg-names",
408 dev_err(dev, "resource \"Hip\" not found\n");
412 pcie->hip_base = map_physmem(reg_res.start,
413 fdt_resource_size(®_res),
419 static const struct dm_pci_ops pcie_intel_fpga_ops = {
420 .read_config = pcie_intel_fpga_read_config,
421 .write_config = pcie_intel_fpga_write_config,
424 static const struct udevice_id pcie_intel_fpga_ids[] = {
425 { .compatible = "altr,pcie-root-port-2.0" },
429 U_BOOT_DRIVER(pcie_intel_fpga) = {
430 .name = "pcie_intel_fpga",
432 .of_match = pcie_intel_fpga_ids,
433 .ops = &pcie_intel_fpga_ops,
434 .ofdata_to_platdata = pcie_intel_fpga_ofdata_to_platdata,
435 .probe = pcie_intel_fpga_probe,
436 .priv_auto_alloc_size = sizeof(struct intel_fpga_pcie),