1
2
3
4
5
6#include <linux/kernel.h>
7
8#include "pcie-cadence.h"
9
10void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u8 fn,
11 u32 r, bool is_io,
12 u64 cpu_addr, u64 pci_addr, size_t size)
13{
14
15
16
17
18 u64 sz = 1ULL << fls64(size - 1);
19 int nbits = ilog2(sz);
20 u32 addr0, addr1, desc0, desc1;
21
22 if (nbits < 8)
23 nbits = 8;
24
25
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);
29
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);
32
33
34 if (is_io)
35 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
36 else
37 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
38 desc1 = 0;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 if (pcie->is_rc) {
60
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);
64 } else {
65
66
67
68
69 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
70 }
71
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);
74
75
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);
80
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);
83}
84
85void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie, u8 fn,
86 u32 r, u64 cpu_addr)
87{
88 u32 addr0, addr1, desc0, desc1;
89
90 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
91 desc1 = 0;
92
93
94 if (pcie->is_rc) {
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);
98 } else {
99 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
100 }
101
102
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);
107
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);
114}
115
116void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
117{
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);
120
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);
123
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);
126}
127
128void cdns_pcie_disable_phy(struct cdns_pcie *pcie)
129{
130 int i = pcie->phy_count;
131
132 while (i--) {
133 phy_power_off(pcie->phy[i]);
134 phy_exit(pcie->phy[i]);
135 }
136}
137
138int cdns_pcie_enable_phy(struct cdns_pcie *pcie)
139{
140 int ret;
141 int i;
142
143 for (i = 0; i < pcie->phy_count; i++) {
144 ret = phy_init(pcie->phy[i]);
145 if (ret < 0)
146 goto err_phy;
147
148 ret = phy_power_on(pcie->phy[i]);
149 if (ret < 0) {
150 phy_exit(pcie->phy[i]);
151 goto err_phy;
152 }
153 }
154
155 return 0;
156
157err_phy:
158 while (--i >= 0) {
159 phy_power_off(pcie->phy[i]);
160 phy_exit(pcie->phy[i]);
161 }
162
163 return ret;
164}
165
166int cdns_pcie_init_phy(struct device *dev, struct cdns_pcie *pcie)
167{
168 struct device_node *np = dev->of_node;
169 int phy_count;
170 struct phy **phy;
171 struct device_link **link;
172 int i;
173 int ret;
174 const char *name;
175
176 phy_count = of_property_count_strings(np, "phy-names");
177 if (phy_count < 1) {
178 dev_err(dev, "no phy-names. PHY will not be initialized\n");
179 pcie->phy_count = 0;
180 return 0;
181 }
182
183 phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
184 if (!phy)
185 return -ENOMEM;
186
187 link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
188 if (!link)
189 return -ENOMEM;
190
191 for (i = 0; i < phy_count; i++) {
192 of_property_read_string_index(np, "phy-names", i, &name);
193 phy[i] = devm_phy_optional_get(dev, name);
194 if (IS_ERR(phy))
195 return PTR_ERR(phy);
196
197 link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
198 if (!link[i]) {
199 ret = -EINVAL;
200 goto err_link;
201 }
202 }
203
204 pcie->phy_count = phy_count;
205 pcie->phy = phy;
206 pcie->link = link;
207
208 ret = cdns_pcie_enable_phy(pcie);
209 if (ret)
210 goto err_link;
211
212 return 0;
213
214err_link:
215 while (--i >= 0)
216 device_link_del(link[i]);
217
218 return ret;
219}
220
221#ifdef CONFIG_PM_SLEEP
222static int cdns_pcie_suspend_noirq(struct device *dev)
223{
224 struct cdns_pcie *pcie = dev_get_drvdata(dev);
225
226 cdns_pcie_disable_phy(pcie);
227
228 return 0;
229}
230
231static int cdns_pcie_resume_noirq(struct device *dev)
232{
233 struct cdns_pcie *pcie = dev_get_drvdata(dev);
234 int ret;
235
236 ret = cdns_pcie_enable_phy(pcie);
237 if (ret) {
238 dev_err(dev, "failed to enable phy\n");
239 return ret;
240 }
241
242 return 0;
243}
244#endif
245
246const struct dev_pm_ops cdns_pcie_pm_ops = {
247 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cdns_pcie_suspend_noirq,
248 cdns_pcie_resume_noirq)
249};
250