blob: b3a5533fe0b92b50fbc3b5555fe100f4cc424418 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05302/**
Bjorn Helgaas96291d52017-09-01 16:35:50 -05003 * Synopsys DesignWare PCIe Endpoint controller driver
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05304 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +05307 */
8
9#include <linux/of.h>
10
11#include "pcie-designware.h"
12#include <linux/pci-epc.h>
13#include <linux/pci-epf.h>
14
15void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
16{
17 struct pci_epc *epc = ep->epc;
18
19 pci_epc_linkup(epc);
20}
21
Niklas Cassel9e718112017-12-20 00:29:26 +010022void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053023{
24 u32 reg;
25
26 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
Niklas Cassel1cab8262017-12-20 00:29:24 +010027 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053028 dw_pcie_writel_dbi2(pci, reg, 0x0);
29 dw_pcie_writel_dbi(pci, reg, 0x0);
Niklas Cassel1cab8262017-12-20 00:29:24 +010030 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053031}
32
Cyrille Pitchen44947382018-01-30 21:56:56 +010033static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053034 struct pci_epf_header *hdr)
35{
36 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
37 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
38
Niklas Cassel1cab8262017-12-20 00:29:24 +010039 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053040 dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, hdr->vendorid);
41 dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, hdr->deviceid);
42 dw_pcie_writeb_dbi(pci, PCI_REVISION_ID, hdr->revid);
43 dw_pcie_writeb_dbi(pci, PCI_CLASS_PROG, hdr->progif_code);
44 dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE,
45 hdr->subclass_code | hdr->baseclass_code << 8);
46 dw_pcie_writeb_dbi(pci, PCI_CACHE_LINE_SIZE,
47 hdr->cache_line_size);
48 dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_VENDOR_ID,
49 hdr->subsys_vendor_id);
50 dw_pcie_writew_dbi(pci, PCI_SUBSYSTEM_ID, hdr->subsys_id);
51 dw_pcie_writeb_dbi(pci, PCI_INTERRUPT_PIN,
52 hdr->interrupt_pin);
Niklas Cassel1cab8262017-12-20 00:29:24 +010053 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053054
55 return 0;
56}
57
58static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, enum pci_barno bar,
59 dma_addr_t cpu_addr,
60 enum dw_pcie_as_type as_type)
61{
62 int ret;
63 u32 free_win;
64 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
65
Niklas Casselad4a5be2017-12-14 14:01:44 +010066 free_win = find_first_zero_bit(ep->ib_window_map, ep->num_ib_windows);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053067 if (free_win >= ep->num_ib_windows) {
68 dev_err(pci->dev, "no free inbound window\n");
69 return -EINVAL;
70 }
71
72 ret = dw_pcie_prog_inbound_atu(pci, free_win, bar, cpu_addr,
73 as_type);
74 if (ret < 0) {
75 dev_err(pci->dev, "Failed to program IB window\n");
76 return ret;
77 }
78
79 ep->bar_to_atu[bar] = free_win;
Niklas Casselad4a5be2017-12-14 14:01:44 +010080 set_bit(free_win, ep->ib_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053081
82 return 0;
83}
84
85static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, phys_addr_t phys_addr,
86 u64 pci_addr, size_t size)
87{
88 u32 free_win;
89 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
90
Niklas Casselad4a5be2017-12-14 14:01:44 +010091 free_win = find_first_zero_bit(ep->ob_window_map, ep->num_ob_windows);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +053092 if (free_win >= ep->num_ob_windows) {
93 dev_err(pci->dev, "no free outbound window\n");
94 return -EINVAL;
95 }
96
97 dw_pcie_prog_outbound_atu(pci, free_win, PCIE_ATU_TYPE_MEM,
98 phys_addr, pci_addr, size);
99
Niklas Casselad4a5be2017-12-14 14:01:44 +0100100 set_bit(free_win, ep->ob_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530101 ep->outbound_addr[free_win] = phys_addr;
102
103 return 0;
104}
105
Cyrille Pitchen44947382018-01-30 21:56:56 +0100106static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no,
107 enum pci_barno bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530108{
109 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
110 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
111 u32 atu_index = ep->bar_to_atu[bar];
112
113 dw_pcie_ep_reset_bar(pci, bar);
114
115 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_INBOUND);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100116 clear_bit(atu_index, ep->ib_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530117}
118
Cyrille Pitchen44947382018-01-30 21:56:56 +0100119static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no,
Niklas Casselbc4a4892018-03-28 13:50:07 +0200120 struct pci_epf_bar *epf_bar)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530121{
122 int ret;
123 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
124 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
Niklas Casselbc4a4892018-03-28 13:50:07 +0200125 enum pci_barno bar = epf_bar->barno;
126 size_t size = epf_bar->size;
127 int flags = epf_bar->flags;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530128 enum dw_pcie_as_type as_type;
129 u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
130
131 if (!(flags & PCI_BASE_ADDRESS_SPACE))
132 as_type = DW_PCIE_AS_MEM;
133 else
134 as_type = DW_PCIE_AS_IO;
135
Niklas Casselbc4a4892018-03-28 13:50:07 +0200136 ret = dw_pcie_ep_inbound_atu(ep, bar, epf_bar->phys_addr, as_type);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530137 if (ret)
138 return ret;
139
Niklas Cassel1cab8262017-12-20 00:29:24 +0100140 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530141 dw_pcie_writel_dbi2(pci, reg, size - 1);
142 dw_pcie_writel_dbi(pci, reg, flags);
Niklas Cassel1cab8262017-12-20 00:29:24 +0100143 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530144
145 return 0;
146}
147
148static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
149 u32 *atu_index)
150{
151 u32 index;
152
153 for (index = 0; index < ep->num_ob_windows; index++) {
154 if (ep->outbound_addr[index] != addr)
155 continue;
156 *atu_index = index;
157 return 0;
158 }
159
160 return -EINVAL;
161}
162
Cyrille Pitchen44947382018-01-30 21:56:56 +0100163static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no,
164 phys_addr_t addr)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530165{
166 int ret;
167 u32 atu_index;
168 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
169 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
170
171 ret = dw_pcie_find_index(ep, addr, &atu_index);
172 if (ret < 0)
173 return;
174
175 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100176 clear_bit(atu_index, ep->ob_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530177}
178
Cyrille Pitchen44947382018-01-30 21:56:56 +0100179static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no,
180 phys_addr_t addr,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530181 u64 pci_addr, size_t size)
182{
183 int ret;
184 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
185 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
186
187 ret = dw_pcie_ep_outbound_atu(ep, addr, pci_addr, size);
188 if (ret) {
189 dev_err(pci->dev, "failed to enable address\n");
190 return ret;
191 }
192
193 return 0;
194}
195
Cyrille Pitchen44947382018-01-30 21:56:56 +0100196static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530197{
198 int val;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530199 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
200 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
201
Kishon Vijay Abraham Ia134a452017-12-19 15:25:41 +0530202 val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
203 if (!(val & MSI_CAP_MSI_EN_MASK))
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530204 return -EINVAL;
205
Kishon Vijay Abraham Ia134a452017-12-19 15:25:41 +0530206 val = (val & MSI_CAP_MME_MASK) >> MSI_CAP_MME_SHIFT;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530207 return val;
208}
209
Cyrille Pitchen44947382018-01-30 21:56:56 +0100210static 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 +0530211{
212 int val;
213 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
214 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
215
Niklas Cassel099a95f2017-12-20 00:29:23 +0100216 val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
217 val &= ~MSI_CAP_MMC_MASK;
218 val |= (encode_int << MSI_CAP_MMC_SHIFT) & MSI_CAP_MMC_MASK;
Niklas Cassel1cab8262017-12-20 00:29:24 +0100219 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530220 dw_pcie_writew_dbi(pci, MSI_MESSAGE_CONTROL, val);
Niklas Cassel1cab8262017-12-20 00:29:24 +0100221 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530222
223 return 0;
224}
225
Cyrille Pitchen44947382018-01-30 21:56:56 +0100226static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530227 enum pci_epc_irq_type type, u8 interrupt_num)
228{
229 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
230
231 if (!ep->ops->raise_irq)
232 return -EINVAL;
233
Bjorn Helgaas16093362018-02-01 11:36:07 -0600234 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530235}
236
237static void dw_pcie_ep_stop(struct pci_epc *epc)
238{
239 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
240 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
241
242 if (!pci->ops->stop_link)
243 return;
244
245 pci->ops->stop_link(pci);
246}
247
248static int dw_pcie_ep_start(struct pci_epc *epc)
249{
250 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
251 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
252
253 if (!pci->ops->start_link)
254 return -EINVAL;
255
256 return pci->ops->start_link(pci);
257}
258
259static const struct pci_epc_ops epc_ops = {
260 .write_header = dw_pcie_ep_write_header,
261 .set_bar = dw_pcie_ep_set_bar,
262 .clear_bar = dw_pcie_ep_clear_bar,
263 .map_addr = dw_pcie_ep_map_addr,
264 .unmap_addr = dw_pcie_ep_unmap_addr,
265 .set_msi = dw_pcie_ep_set_msi,
266 .get_msi = dw_pcie_ep_get_msi,
267 .raise_irq = dw_pcie_ep_raise_irq,
268 .start = dw_pcie_ep_start,
269 .stop = dw_pcie_ep_stop,
270};
271
Bjorn Helgaas16093362018-02-01 11:36:07 -0600272int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100273 u8 interrupt_num)
274{
275 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
276 struct pci_epc *epc = ep->epc;
277 u16 msg_ctrl, msg_data;
278 u32 msg_addr_lower, msg_addr_upper;
279 u64 msg_addr;
280 bool has_upper;
281 int ret;
282
283 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
284 msg_ctrl = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
285 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
286 msg_addr_lower = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_L32);
287 if (has_upper) {
288 msg_addr_upper = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_U32);
289 msg_data = dw_pcie_readw_dbi(pci, MSI_MESSAGE_DATA_64);
290 } else {
291 msg_addr_upper = 0;
292 msg_data = dw_pcie_readw_dbi(pci, MSI_MESSAGE_DATA_32);
293 }
294 msg_addr = ((u64) msg_addr_upper) << 32 | msg_addr_lower;
Bjorn Helgaas16093362018-02-01 11:36:07 -0600295 ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr,
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100296 epc->mem->page_size);
297 if (ret)
298 return ret;
299
300 writel(msg_data | (interrupt_num - 1), ep->msi_mem);
301
Bjorn Helgaas16093362018-02-01 11:36:07 -0600302 dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100303
304 return 0;
305}
306
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530307void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
308{
309 struct pci_epc *epc = ep->epc;
310
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100311 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
312 epc->mem->page_size);
313
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530314 pci_epc_mem_exit(epc);
315}
316
317int dw_pcie_ep_init(struct dw_pcie_ep *ep)
318{
319 int ret;
320 void *addr;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530321 struct pci_epc *epc;
322 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
323 struct device *dev = pci->dev;
324 struct device_node *np = dev->of_node;
325
326 if (!pci->dbi_base || !pci->dbi_base2) {
327 dev_err(dev, "dbi_base/deb_base2 is not populated\n");
328 return -EINVAL;
329 }
330
331 ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
332 if (ret < 0) {
333 dev_err(dev, "unable to read *num-ib-windows* property\n");
334 return ret;
335 }
Niklas Casselad4a5be2017-12-14 14:01:44 +0100336 if (ep->num_ib_windows > MAX_IATU_IN) {
337 dev_err(dev, "invalid *num-ib-windows*\n");
338 return -EINVAL;
339 }
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530340
341 ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
342 if (ret < 0) {
343 dev_err(dev, "unable to read *num-ob-windows* property\n");
344 return ret;
345 }
Niklas Casselad4a5be2017-12-14 14:01:44 +0100346 if (ep->num_ob_windows > MAX_IATU_OUT) {
347 dev_err(dev, "invalid *num-ob-windows*\n");
348 return -EINVAL;
349 }
350
351 ep->ib_window_map = devm_kzalloc(dev, sizeof(long) *
352 BITS_TO_LONGS(ep->num_ib_windows),
353 GFP_KERNEL);
354 if (!ep->ib_window_map)
355 return -ENOMEM;
356
357 ep->ob_window_map = devm_kzalloc(dev, sizeof(long) *
358 BITS_TO_LONGS(ep->num_ob_windows),
359 GFP_KERNEL);
360 if (!ep->ob_window_map)
361 return -ENOMEM;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530362
363 addr = devm_kzalloc(dev, sizeof(phys_addr_t) * ep->num_ob_windows,
364 GFP_KERNEL);
365 if (!addr)
366 return -ENOMEM;
367 ep->outbound_addr = addr;
368
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530369 if (ep->ops->ep_init)
370 ep->ops->ep_init(ep);
371
372 epc = devm_pci_epc_create(dev, &epc_ops);
373 if (IS_ERR(epc)) {
374 dev_err(dev, "failed to create epc device\n");
375 return PTR_ERR(epc);
376 }
377
378 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
379 if (ret < 0)
380 epc->max_functions = 1;
381
Kishon Vijay Abraham Ia937fe02017-08-18 20:28:02 +0530382 ret = __pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
383 ep->page_size);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530384 if (ret < 0) {
385 dev_err(dev, "Failed to initialize address space\n");
386 return ret;
387 }
388
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100389 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
390 epc->mem->page_size);
391 if (!ep->msi_mem) {
392 dev_err(dev, "Failed to reserve memory for MSI\n");
393 return -ENOMEM;
394 }
395
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530396 ep->epc = epc;
397 epc_set_drvdata(epc, ep);
398 dw_pcie_setup(pci);
399
400 return 0;
401}