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 busnr
, 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(busnr
);
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 if (pcie
->ops
->cpu_addr_fixup
)
77 cpu_addr
= pcie
->ops
->cpu_addr_fixup(pcie
, cpu_addr
);
79 addr0
= CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits
) |
80 (lower_32_bits(cpu_addr
) & GENMASK(31, 8));
81 addr1
= upper_32_bits(cpu_addr
);
83 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r
), addr0
);
84 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r
), addr1
);
87 void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie
*pcie
,
91 u32 addr0
, addr1
, desc0
, desc1
;
93 desc0
= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG
;
96 /* See cdns_pcie_set_outbound_region() comments above. */
98 desc0
|= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID
|
99 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
100 desc1
|= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(busnr
);
102 desc0
|= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn
);
105 /* Set the CPU address */
106 if (pcie
->ops
->cpu_addr_fixup
)
107 cpu_addr
= pcie
->ops
->cpu_addr_fixup(pcie
, cpu_addr
);
109 addr0
= CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
110 (lower_32_bits(cpu_addr
) & GENMASK(31, 8));
111 addr1
= upper_32_bits(cpu_addr
);
113 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r
), 0);
114 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r
), 0);
115 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC0(r
), desc0
);
116 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC1(r
), desc1
);
117 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r
), addr0
);
118 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r
), addr1
);
121 void cdns_pcie_reset_outbound_region(struct cdns_pcie
*pcie
, u32 r
)
123 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r
), 0);
124 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r
), 0);
126 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC0(r
), 0);
127 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_DESC1(r
), 0);
129 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r
), 0);
130 cdns_pcie_writel(pcie
, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r
), 0);
133 void cdns_pcie_disable_phy(struct cdns_pcie
*pcie
)
135 int i
= pcie
->phy_count
;
138 phy_power_off(pcie
->phy
[i
]);
139 phy_exit(pcie
->phy
[i
]);
143 int cdns_pcie_enable_phy(struct cdns_pcie
*pcie
)
148 for (i
= 0; i
< pcie
->phy_count
; i
++) {
149 ret
= phy_init(pcie
->phy
[i
]);
153 ret
= phy_power_on(pcie
->phy
[i
]);
155 phy_exit(pcie
->phy
[i
]);
164 phy_power_off(pcie
->phy
[i
]);
165 phy_exit(pcie
->phy
[i
]);
171 int cdns_pcie_init_phy(struct device
*dev
, struct cdns_pcie
*pcie
)
173 struct device_node
*np
= dev
->of_node
;
176 struct device_link
**link
;
181 phy_count
= of_property_count_strings(np
, "phy-names");
183 dev_err(dev
, "no phy-names. PHY will not be initialized\n");
188 phy
= devm_kcalloc(dev
, phy_count
, sizeof(*phy
), GFP_KERNEL
);
192 link
= devm_kcalloc(dev
, phy_count
, sizeof(*link
), GFP_KERNEL
);
196 for (i
= 0; i
< phy_count
; i
++) {
197 of_property_read_string_index(np
, "phy-names", i
, &name
);
198 phy
[i
] = devm_phy_get(dev
, name
);
199 if (IS_ERR(phy
[i
])) {
200 ret
= PTR_ERR(phy
[i
]);
203 link
[i
] = device_link_add(dev
, &phy
[i
]->dev
, DL_FLAG_STATELESS
);
205 devm_phy_put(dev
, phy
[i
]);
211 pcie
->phy_count
= phy_count
;
215 ret
= cdns_pcie_enable_phy(pcie
);
223 device_link_del(link
[i
]);
224 devm_phy_put(dev
, phy
[i
]);
230 #ifdef CONFIG_PM_SLEEP
231 static int cdns_pcie_suspend_noirq(struct device
*dev
)
233 struct cdns_pcie
*pcie
= dev_get_drvdata(dev
);
235 cdns_pcie_disable_phy(pcie
);
240 static int cdns_pcie_resume_noirq(struct device
*dev
)
242 struct cdns_pcie
*pcie
= dev_get_drvdata(dev
);
245 ret
= cdns_pcie_enable_phy(pcie
);
247 dev_err(dev
, "failed to enable phy\n");
255 const struct dev_pm_ops cdns_pcie_pm_ops
= {
256 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cdns_pcie_suspend_noirq
,
257 cdns_pcie_resume_noirq
)