blob: 71795db41261c33f11ed0d04822c1d7f7e01144d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/**
2 * Synopsys DesignWare PCIe Endpoint controller driver
3 *
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/of.h>
21
22#include "pcie-designware.h"
23#include <linux/pci-epc.h>
24#include <linux/pci-epf.h>
25
26void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
27{
28 struct pci_epc *epc = ep->epc;
29
30 pci_epc_linkup(epc);
31}
32
33static void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
34{
35 u32 reg;
36
37 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
38 dw_pcie_dbi_ro_wr_en(pci);
39 dw_pcie_writel_dbi2(pci, reg, 0x0);
40 dw_pcie_writel_dbi(pci, reg, 0x0);
41 dw_pcie_dbi_ro_wr_dis(pci);
42}
43
44static int dw_pcie_ep_write_header(struct pci_epc *epc,
45 struct pci_epf_header *hdr)
46{
47 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
48 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
49
50 dw_pcie_dbi_ro_wr_en(pci);
51 dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, hdr->vendorid);
52 dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, hdr->deviceid);
53 dw_pcie_writeb_dbi(pci, PCI_REVISION_ID, hdr->revid);
54 dw_pcie_writeb_dbi(pci, PCI_CLASS_PROG, hdr->progif_code);
55 dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE,
56 hdr->subclass_code | hdr->baseclass_code << 8);
57 dw_pcie_writeb_dbi(pci, PCI_CACHE_LINE_SIZE,
58 hdr->cache_line_size);
59 dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_VENDOR_ID,
60 hdr->subsys_vendor_id);
61 dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_ID, hdr->subsys_id);
62 dw_pcie_writeb_dbi(pci, PCI_INTERRUPT_PIN,
63 hdr->interrupt_pin);
64 dw_pcie_dbi_ro_wr_dis(pci);
65
66 return 0;
67}
68
69static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, enum pci_barno bar,
70 dma_addr_t cpu_addr,
71 enum dw_pcie_as_type as_type)
72{
73 int ret;
74 u32 free_win;
75 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
76
77 free_win = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows);
78 if (free_win >= ep->num_ib_windows) {
79 dev_err(pci->dev, "no free inbound window\n");
80 return -EINVAL;
81 }
82
83 ret = dw_pcie_prog_inbound_atu(pci, free_win, bar, cpu_addr,
84 as_type);
85 if (ret < 0) {
86 dev_err(pci->dev, "Failed to program IB window\n");
87 return ret;
88 }
89
90 ep->bar_to_atu[bar] = free_win;
91 set_bit(free_win, ep->ib_window_map);
92
93 return 0;
94}
95
96static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, phys_addr_t phys_addr,
97 u64 pci_addr, size_t size)
98{
99 u32 free_win;
100 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
101
102 free_win = find_first_zero_bit(ep->ob_window_map, ep->num_ob_windows);
103 if (free_win >= ep->num_ob_windows) {
104 dev_err(pci->dev, "no free outbound window\n");
105 return -EINVAL;
106 }
107
108 dw_pcie_prog_outbound_atu(pci, free_win, PCIE_ATU_TYPE_MEM,
109 phys_addr, pci_addr, size);
110
111 set_bit(free_win, ep->ob_window_map);
112 ep->outbound_addr[free_win] = phys_addr;
113
114 return 0;
115}
116
117static void dw_pcie_ep_clear_bar(struct pci_epc *epc, enum pci_barno bar)
118{
119 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
120 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
121 u32 atu_index = ep->bar_to_atu[bar];
122
123 dw_pcie_ep_reset_bar(pci, bar);
124
125 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
126 clear_bit(atu_index, ep->ib_window_map);
127}
128
129static int dw_pcie_ep_set_bar(struct pci_epc *epc, enum pci_barno bar,
130 dma_addr_t bar_phys, size_t size, int flags)
131{
132 int ret;
133 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
134 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
135 enum dw_pcie_as_type as_type;
136 u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
137
138 if (!(flags & PCI_BASE_ADDRESS_SPACE))
139 as_type = DW_PCIE_AS_MEM;
140 else
141 as_type = DW_PCIE_AS_IO;
142
143 ret = dw_pcie_ep_inbound_atu(ep, bar, bar_phys, as_type);
144 if (ret)
145 return ret;
146
147 dw_pcie_dbi_ro_wr_en(pci);
148 dw_pcie_writel_dbi2(pci, reg, size - 1);
149 dw_pcie_writel_dbi(pci, reg, flags);
150 dw_pcie_dbi_ro_wr_dis(pci);
151
152 return 0;
153}
154
155static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
156 u32 *atu_index)
157{
158 u32 index;
159
160 for (index = 0; index < ep->num_ob_windows; index++) {
161 if (ep->outbound_addr[index] != addr)
162 continue;
163 *atu_index = index;
164 return 0;
165 }
166
167 return -EINVAL;
168}
169
170static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, phys_addr_t addr)
171{
172 int ret;
173 u32 atu_index;
174 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
175 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
176
177 ret = dw_pcie_find_index(ep, addr, &atu_index);
178 if (ret < 0)
179 return;
180
181 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
182 clear_bit(atu_index, ep->ob_window_map);
183}
184
185static int dw_pcie_ep_map_addr(struct pci_epc *epc, phys_addr_t addr,
186 u64 pci_addr, size_t size)
187{
188 int ret;
189 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
190 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
191
192 ret = dw_pcie_ep_outbound_atu(ep, addr, pci_addr, size);
193 if (ret) {
194 dev_err(pci->dev, "failed to enable address\n");
195 return ret;
196 }
197
198 return 0;
199}
200
201static int dw_pcie_ep_get_msi(struct pci_epc *epc)
202{
203 int val;
204 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
205 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
206
207 val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
208 if (!(val & MSI_CAP_MSI_EN_MASK))
209 return -EINVAL;
210
211 val = (val & MSI_CAP_MME_MASK) >> MSI_CAP_MME_SHIFT;
212 return val;
213}
214
215static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 encode_int)
216{
217 int val;
218 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
219 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
220
221 val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
222 val &= ~MSI_CAP_MMC_MASK;
223 val |= (encode_int << MSI_CAP_MMC_SHIFT) & MSI_CAP_MMC_MASK;
224 dw_pcie_dbi_ro_wr_en(pci);
225 dw_pcie_writew_dbi(pci, MSI_MESSAGE_CONTROL, val);
226 dw_pcie_dbi_ro_wr_dis(pci);
227
228 return 0;
229}
230
231static int dw_pcie_ep_raise_irq(struct pci_epc *epc,
232 enum pci_epc_irq_type type, u8 interrupt_num)
233{
234 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
235
236 if (!ep->ops->raise_irq)
237 return -EINVAL;
238
239 return ep->ops->raise_irq(ep, type, interrupt_num);
240}
241
242static void dw_pcie_ep_stop(struct pci_epc *epc)
243{
244 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
245 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
246
247 if (!pci->ops->stop_link)
248 return;
249
250 pci->ops->stop_link(pci);
251}
252
253static int dw_pcie_ep_start(struct pci_epc *epc)
254{
255 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
256 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
257
258 if (!pci->ops->start_link)
259 return -EINVAL;
260
261 return pci->ops->start_link(pci);
262}
263
264static const struct pci_epc_ops epc_ops = {
265 .write_header = dw_pcie_ep_write_header,
266 .set_bar = dw_pcie_ep_set_bar,
267 .clear_bar = dw_pcie_ep_clear_bar,
268 .map_addr = dw_pcie_ep_map_addr,
269 .unmap_addr = dw_pcie_ep_unmap_addr,
270 .set_msi = dw_pcie_ep_set_msi,
271 .get_msi = dw_pcie_ep_get_msi,
272 .raise_irq = dw_pcie_ep_raise_irq,
273 .start = dw_pcie_ep_start,
274 .stop = dw_pcie_ep_stop,
275};
276
277void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
278{
279 struct pci_epc *epc = ep->epc;
280
281 pci_epc_mem_exit(epc);
282}
283
284int dw_pcie_ep_init(struct dw_pcie_ep *ep)
285{
286 int ret;
287 void *addr;
288 struct pci_epc *epc;
289 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
290 struct device *dev = pci->dev;
291 struct device_node *np = dev->of_node;
292
293 if (!pci->dbi_base || !pci->dbi_base2) {
294 dev_err(dev, "dbi_base/deb_base2 is not populated\n");
295 return -EINVAL;
296 }
297
298 ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
299 if (ret < 0) {
300 dev_err(dev, "unable to read *num-ib-windows* property\n");
301 return ret;
302 }
303 if (ep->num_ib_windows > MAX_IATU_IN) {
304 dev_err(dev, "invalid *num-ib-windows*\n");
305 return -EINVAL;
306 }
307
308 ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
309 if (ret < 0) {
310 dev_err(dev, "unable to read *num-ob-windows* property\n");
311 return ret;
312 }
313 if (ep->num_ob_windows > MAX_IATU_OUT) {
314 dev_err(dev, "invalid *num-ob-windows*\n");
315 return -EINVAL;
316 }
317
318 ep->ib_window_map = devm_kzalloc(dev, sizeof(long) *
319 BITS_TO_LONGS(ep->num_ib_windows),
320 GFP_KERNEL);
321 if (!ep->ib_window_map)
322 return -ENOMEM;
323
324 ep->ob_window_map = devm_kzalloc(dev, sizeof(long) *
325 BITS_TO_LONGS(ep->num_ob_windows),
326 GFP_KERNEL);
327 if (!ep->ob_window_map)
328 return -ENOMEM;
329
330 addr = devm_kzalloc(dev, sizeof(phys_addr_t) * ep->num_ob_windows,
331 GFP_KERNEL);
332 if (!addr)
333 return -ENOMEM;
334 ep->outbound_addr = addr;
335
336 if (ep->ops->ep_init)
337 ep->ops->ep_init(ep);
338
339 epc = devm_pci_epc_create(dev, &epc_ops);
340 if (IS_ERR(epc)) {
341 dev_err(dev, "failed to create epc device\n");
342 return PTR_ERR(epc);
343 }
344
345 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
346 if (ret < 0)
347 epc->max_functions = 1;
348
349 ret = __pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
350 ep->page_size);
351 if (ret < 0) {
352 dev_err(dev, "Failed to initialize address space\n");
353 return ret;
354 }
355
356 ep->epc = epc;
357 epc_set_drvdata(epc, ep);
358 dw_pcie_setup(pci);
359
360 return 0;
361}