1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Cadence
3 // Cadence PCIe controller driver.
6 #include <linux/kernel.h>
9 #include "pcie-cadence.h"
11 void cdns_pcie_detect_quiet_min_delay_set(struct cdns_pcie *pcie)
14 u32 ltssm_control_cap;
17 * Set the LTSSM Detect Quiet state min. delay to 2ms.
19 ltssm_control_cap = cdns_pcie_readl(pcie, CDNS_PCIE_LTSSM_CONTROL_CAP);
20 ltssm_control_cap = ((ltssm_control_cap &
21 ~CDNS_PCIE_DETECT_QUIET_MIN_DELAY_MASK) |
22 CDNS_PCIE_DETECT_QUIET_MIN_DELAY(delay));
24 cdns_pcie_writel(pcie, CDNS_PCIE_LTSSM_CONTROL_CAP, ltssm_control_cap);
27 void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u8 busnr, u8 fn,
29 u64 cpu_addr, u64 pci_addr, size_t size)
32 * roundup_pow_of_two() returns an unsigned long, which is not suited
35 u64 sz = 1ULL << fls64(size - 1);
36 int nbits = ilog2(sz);
37 u32 addr0, addr1, desc0, desc1;
42 /* Set the PCI address */
43 addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) |
44 (lower_32_bits(pci_addr) & GENMASK(31, 8));
45 addr1 = upper_32_bits(pci_addr);
47 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), addr0);
48 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), addr1);
50 /* Set the PCIe header descriptor */
52 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
54 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
58 * Whatever Bit [23] is set or not inside DESC0 register of the outbound
59 * PCIe descriptor, the PCI function number must be set into
60 * Bits [26:24] of DESC0 anyway.
62 * In Root Complex mode, the function number is always 0 but in Endpoint
63 * mode, the PCIe controller may support more than one function. This
64 * function number needs to be set properly into the outbound PCIe
67 * Besides, setting Bit [23] is mandatory when in Root Complex mode:
68 * then the driver must provide the bus, resp. device, number in
69 * Bits [7:0] of DESC1, resp. Bits[31:27] of DESC0. Like the function
70 * number, the device number is always 0 in Root Complex mode.
72 * However when in Endpoint mode, we can clear Bit [23] of DESC0, hence
73 * the PCIe controller will use the captured values for the bus and
77 /* The device and function numbers are always 0. */
78 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
79 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
80 desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(busnr);
83 * Use captured values for bus and device numbers but still
84 * need to set the function number.
86 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
89 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
90 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
92 /* Set the CPU address */
93 if (pcie->ops->cpu_addr_fixup)
94 cpu_addr = pcie->ops->cpu_addr_fixup(pcie, cpu_addr);
96 addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) |
97 (lower_32_bits(cpu_addr) & GENMASK(31, 8));
98 addr1 = upper_32_bits(cpu_addr);
100 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
101 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
104 void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie,
108 u32 addr0, addr1, desc0, desc1;
110 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
113 /* See cdns_pcie_set_outbound_region() comments above. */
115 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
116 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
117 desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(busnr);
119 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
122 /* Set the CPU address */
123 if (pcie->ops->cpu_addr_fixup)
124 cpu_addr = pcie->ops->cpu_addr_fixup(pcie, cpu_addr);
126 addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
127 (lower_32_bits(cpu_addr) & GENMASK(31, 8));
128 addr1 = upper_32_bits(cpu_addr);
130 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
131 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
132 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
133 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
134 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
135 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
138 void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
140 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
141 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
143 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), 0);
144 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), 0);
146 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), 0);
147 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), 0);
150 void cdns_pcie_disable_phy(struct cdns_pcie *pcie)
152 int i = pcie->phy_count;
155 phy_power_off(pcie->phy[i]);
156 phy_exit(pcie->phy[i]);
160 int cdns_pcie_enable_phy(struct cdns_pcie *pcie)
165 for (i = 0; i < pcie->phy_count; i++) {
166 ret = phy_init(pcie->phy[i]);
170 ret = phy_power_on(pcie->phy[i]);
172 phy_exit(pcie->phy[i]);
181 phy_power_off(pcie->phy[i]);
182 phy_exit(pcie->phy[i]);
188 int cdns_pcie_init_phy(struct device *dev, struct cdns_pcie *pcie)
190 struct device_node *np = dev->of_node;
193 struct device_link **link;
198 phy_count = of_property_count_strings(np, "phy-names");
200 dev_err(dev, "no phy-names. PHY will not be initialized\n");
205 phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
209 link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
213 for (i = 0; i < phy_count; i++) {
214 of_property_read_string_index(np, "phy-names", i, &name);
215 phy[i] = devm_phy_get(dev, name);
216 if (IS_ERR(phy[i])) {
217 ret = PTR_ERR(phy[i]);
220 link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
222 devm_phy_put(dev, phy[i]);
228 pcie->phy_count = phy_count;
232 ret = cdns_pcie_enable_phy(pcie);
240 device_link_del(link[i]);
241 devm_phy_put(dev, phy[i]);
247 static int cdns_pcie_suspend_noirq(struct device *dev)
249 struct cdns_pcie *pcie = dev_get_drvdata(dev);
251 cdns_pcie_disable_phy(pcie);
256 static int cdns_pcie_resume_noirq(struct device *dev)
258 struct cdns_pcie *pcie = dev_get_drvdata(dev);
261 ret = cdns_pcie_enable_phy(pcie);
263 dev_err(dev, "failed to enable phy\n");
270 const struct dev_pm_ops cdns_pcie_pm_ops = {
271 NOIRQ_SYSTEM_SLEEP_PM_OPS(cdns_pcie_suspend_noirq,
272 cdns_pcie_resume_noirq)