1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Cadence
3 // Cadence PCIe controller driver.
4 // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
6 #include <linux/kernel.h>
8 #include "pcie-cadence.h"
10 void cdns_pcie_set_outbound_region(struct cdns_pcie
*pcie
, u8 fn
,
12 u64 cpu_addr
, u64 pci_addr
, size_t size
)
15 * roundup_pow_of_two() returns an unsigned long, which is not suited
18 u64 sz
= 1ULL << fls64(size
- 1);
19 int nbits
= ilog2(sz
);
20 u32 addr0
, addr1
, desc0
, desc1
;
25 /* Set the PCI address */
26 addr0
= CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits
) |
27 (lower_32_bits(pci_addr
) & GENMASK(31, 8));
28 addr1
= upper_32_bits(pci_addr
);
30 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r
), addr0
);
31 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r
), addr1
);
33 /* Set the PCIe header descriptor */
35 desc0
= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO
;
37 desc0
= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM
;
41 * Whatever Bit [23] is set or not inside DESC0 register of the outbound
42 * PCIe descriptor, the PCI function number must be set into
43 * Bits [26:24] of DESC0 anyway.
45 * In Root Complex mode, the function number is always 0 but in Endpoint
46 * mode, the PCIe controller may support more than one function. This
47 * function number needs to be set properly into the outbound PCIe
50 * Besides, setting Bit [23] is mandatory when in Root Complex mode:
51 * then the driver must provide the bus, resp. device, number in
52 * Bits [7:0] of DESC1, resp. Bits[31:27] of DESC0. Like the function
53 * number, the device number is always 0 in Root Complex mode.
55 * However when in Endpoint mode, we can clear Bit [23] of DESC0, hence
56 * the PCIe controller will use the captured values for the bus and
60 /* The device and function numbers are always 0. */
61 desc0
|= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID
|
62 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
63 desc1
|= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie
->bus
);
66 * Use captured values for bus and device numbers but still
67 * need to set the function number.
69 desc0
|= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn
);
72 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC0(r
), desc0
);
73 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC1(r
), desc1
);
75 /* Set the CPU address */
76 cpu_addr
-= pcie
->mem_res
->start
;
77 addr0
= CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits
) |
78 (lower_32_bits(cpu_addr
) & GENMASK(31, 8));
79 addr1
= upper_32_bits(cpu_addr
);
81 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r
), addr0
);
82 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r
), addr1
);
85 void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie
*pcie
, u8 fn
,
88 u32 addr0
, addr1
, desc0
, desc1
;
90 desc0
= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG
;
93 /* See cdns_pcie_set_outbound_region() comments above. */
95 desc0
|= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID
|
96 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
97 desc1
|= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie
->bus
);
99 desc0
|= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn
);
102 /* Set the CPU address */
103 cpu_addr
-= pcie
->mem_res
->start
;
104 addr0
= CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
105 (lower_32_bits(cpu_addr
) & GENMASK(31, 8));
106 addr1
= upper_32_bits(cpu_addr
);
108 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r
), 0);
109 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r
), 0);
110 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC0(r
), desc0
);
111 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC1(r
), desc1
);
112 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r
), addr0
);
113 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r
), addr1
);
116 void cdns_pcie_reset_outbound_region(struct cdns_pcie
*pcie
, u32 r
)
118 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r
), 0);
119 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r
), 0);
121 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC0(r
), 0);
122 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC1(r
), 0);
124 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r
), 0);
125 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r
), 0);