blob: 7a573d8bb62d2fc3735f223a2971a2af6d49fc14 [file] [log] [blame]
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05301/**
Bjorn Helgaas96291d52017-09-01 16:35:50 -05002 * Synopsys DesignWare PCIe Endpoint controller driver
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05303 *
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_writel_dbi2(pci, reg, 0x0);
39 dw_pcie_writel_dbi(pci, reg, 0x0);
40}
41
Cyrille Pitchen44947382018-01-30 21:56:56 +010042static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053043 struct pci_epf_header *hdr)
44{
45 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
46 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
47
48 dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, hdr->vendorid);
49 dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, hdr->deviceid);
50 dw_pcie_writeb_dbi(pci, PCI_REVISION_ID, hdr->revid);
51 dw_pcie_writeb_dbi(pci, PCI_CLASS_PROG, hdr->progif_code);
52 dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE,
53 hdr->subclass_code | hdr->baseclass_code << 8);
54 dw_pcie_writeb_dbi(pci, PCI_CACHE_LINE_SIZE,
55 hdr->cache_line_size);
56 dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_VENDOR_ID,
57 hdr->subsys_vendor_id);
58 dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_ID, hdr->subsys_id);
59 dw_pcie_writeb_dbi(pci, PCI_INTERRUPT_PIN,
60 hdr->interrupt_pin);
61
62 return 0;
63}
64
65static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, enum pci_barno bar,
66 dma_addr_t cpu_addr,
67 enum dw_pcie_as_type as_type)
68{
69 int ret;
70 u32 free_win;
71 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
72
73 free_win = find_first_zero_bit(&ep->ib_window_map,
74 sizeof(ep->ib_window_map));
75 if (free_win >= ep->num_ib_windows) {
76 dev_err(pci->dev, "no free inbound window\n");
77 return -EINVAL;
78 }
79
80 ret = dw_pcie_prog_inbound_atu(pci, free_win, bar, cpu_addr,
81 as_type);
82 if (ret < 0) {
83 dev_err(pci->dev, "Failed to program IB window\n");
84 return ret;
85 }
86
87 ep->bar_to_atu[bar] = free_win;
88 set_bit(free_win, &ep->ib_window_map);
89
90 return 0;
91}
92
93static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, phys_addr_t phys_addr,
94 u64 pci_addr, size_t size)
95{
96 u32 free_win;
97 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
98
99 free_win = find_first_zero_bit(&ep->ob_window_map,
100 sizeof(ep->ob_window_map));
101 if (free_win >= ep->num_ob_windows) {
102 dev_err(pci->dev, "no free outbound window\n");
103 return -EINVAL;
104 }
105
106 dw_pcie_prog_outbound_atu(pci, free_win, PCIE_ATU_TYPE_MEM,
107 phys_addr, pci_addr, size);
108
109 set_bit(free_win, &ep->ob_window_map);
110 ep->outbound_addr[free_win] = phys_addr;
111
112 return 0;
113}
114
Cyrille Pitchen44947382018-01-30 21:56:56 +0100115static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no,
116 enum pci_barno bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530117{
118 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
119 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
120 u32 atu_index = ep->bar_to_atu[bar];
121
122 dw_pcie_ep_reset_bar(pci, bar);
123
124 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
125 clear_bit(atu_index, &ep->ib_window_map);
126}
127
Cyrille Pitchen44947382018-01-30 21:56:56 +0100128static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
129 enum pci_barno bar,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530130 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_writel_dbi2(pci, reg, size - 1);
148 dw_pcie_writel_dbi(pci, reg, flags);
149
150 return 0;
151}
152
153static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
154 u32 *atu_index)
155{
156 u32 index;
157
158 for (index = 0; index < ep->num_ob_windows; index++) {
159 if (ep->outbound_addr[index] != addr)
160 continue;
161 *atu_index = index;
162 return 0;
163 }
164
165 return -EINVAL;
166}
167
Cyrille Pitchen44947382018-01-30 21:56:56 +0100168static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no,
169 phys_addr_t addr)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530170{
171 int ret;
172 u32 atu_index;
173 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
174 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
175
176 ret = dw_pcie_find_index(ep, addr, &atu_index);
177 if (ret < 0)
178 return;
179
180 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
181 clear_bit(atu_index, &ep->ob_window_map);
182}
183
Cyrille Pitchen44947382018-01-30 21:56:56 +0100184static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no,
185 phys_addr_t addr,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530186 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
Cyrille Pitchen44947382018-01-30 21:56:56 +0100201static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530202{
203 int val;
204 u32 lower_addr;
205 u32 upper_addr;
206 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
207 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
208
209 val = dw_pcie_readb_dbi(pci, MSI_MESSAGE_CONTROL);
210 val = (val & MSI_CAP_MME_MASK) >> MSI_CAP_MME_SHIFT;
211
212 lower_addr = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_L32);
213 upper_addr = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_U32);
214
215 if (!(lower_addr || upper_addr))
216 return -EINVAL;
217
218 return val;
219}
220
Cyrille Pitchen44947382018-01-30 21:56:56 +0100221static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 encode_int)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530222{
223 int val;
224 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
225 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
226
227 val = (encode_int << MSI_CAP_MMC_SHIFT);
228 dw_pcie_writew_dbi(pci, MSI_MESSAGE_CONTROL, val);
229
230 return 0;
231}
232
Cyrille Pitchen44947382018-01-30 21:56:56 +0100233static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530234 enum pci_epc_irq_type type, u8 interrupt_num)
235{
236 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
237
238 if (!ep->ops->raise_irq)
239 return -EINVAL;
240
241 return ep->ops->raise_irq(ep, type, interrupt_num);
242}
243
244static void dw_pcie_ep_stop(struct pci_epc *epc)
245{
246 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
247 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
248
249 if (!pci->ops->stop_link)
250 return;
251
252 pci->ops->stop_link(pci);
253}
254
255static int dw_pcie_ep_start(struct pci_epc *epc)
256{
257 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
258 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
259
260 if (!pci->ops->start_link)
261 return -EINVAL;
262
263 return pci->ops->start_link(pci);
264}
265
266static const struct pci_epc_ops epc_ops = {
267 .write_header = dw_pcie_ep_write_header,
268 .set_bar = dw_pcie_ep_set_bar,
269 .clear_bar = dw_pcie_ep_clear_bar,
270 .map_addr = dw_pcie_ep_map_addr,
271 .unmap_addr = dw_pcie_ep_unmap_addr,
272 .set_msi = dw_pcie_ep_set_msi,
273 .get_msi = dw_pcie_ep_get_msi,
274 .raise_irq = dw_pcie_ep_raise_irq,
275 .start = dw_pcie_ep_start,
276 .stop = dw_pcie_ep_stop,
277};
278
279void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
280{
281 struct pci_epc *epc = ep->epc;
282
283 pci_epc_mem_exit(epc);
284}
285
286int dw_pcie_ep_init(struct dw_pcie_ep *ep)
287{
288 int ret;
289 void *addr;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530290 struct pci_epc *epc;
291 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
292 struct device *dev = pci->dev;
293 struct device_node *np = dev->of_node;
294
295 if (!pci->dbi_base || !pci->dbi_base2) {
296 dev_err(dev, "dbi_base/deb_base2 is not populated\n");
297 return -EINVAL;
298 }
299
300 ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
301 if (ret < 0) {
302 dev_err(dev, "unable to read *num-ib-windows* property\n");
303 return ret;
304 }
305
306 ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
307 if (ret < 0) {
308 dev_err(dev, "unable to read *num-ob-windows* property\n");
309 return ret;
310 }
311
312 addr = devm_kzalloc(dev, sizeof(phys_addr_t) * ep->num_ob_windows,
313 GFP_KERNEL);
314 if (!addr)
315 return -ENOMEM;
316 ep->outbound_addr = addr;
317
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530318 if (ep->ops->ep_init)
319 ep->ops->ep_init(ep);
320
321 epc = devm_pci_epc_create(dev, &epc_ops);
322 if (IS_ERR(epc)) {
323 dev_err(dev, "failed to create epc device\n");
324 return PTR_ERR(epc);
325 }
326
327 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
328 if (ret < 0)
329 epc->max_functions = 1;
330
Kishon Vijay Abraham Ia937fe02017-08-18 20:28:02 +0530331 ret = __pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
332 ep->page_size);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530333 if (ret < 0) {
334 dev_err(dev, "Failed to initialize address space\n");
335 return ret;
336 }
337
338 ep->epc = epc;
339 epc_set_drvdata(epc, ep);
340 dw_pcie_setup(pci);
341
342 return 0;
343}