blob: 70c8c1eedb426563d8f73032f2227e8b60262258 [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);
Niklas Casseld28810b2018-03-28 13:50:11 +0200141
142 dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530143 dw_pcie_writel_dbi(pci, reg, flags);
Niklas Casseld28810b2018-03-28 13:50:11 +0200144
145 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
146 dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
147 dw_pcie_writel_dbi(pci, reg + 4, 0);
148 }
149
Niklas Cassel1cab8262017-12-20 00:29:24 +0100150 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530151
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
Cyrille Pitchen44947382018-01-30 21:56:56 +0100170static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no,
171 phys_addr_t addr)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530172{
173 int ret;
174 u32 atu_index;
175 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
176 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
177
178 ret = dw_pcie_find_index(ep, addr, &atu_index);
179 if (ret < 0)
180 return;
181
182 dw_pcie_disable_atu(pci, atu_index, DW_PCIE_REGION_OUTBOUND);
Niklas Casselad4a5be2017-12-14 14:01:44 +0100183 clear_bit(atu_index, ep->ob_window_map);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530184}
185
Cyrille Pitchen44947382018-01-30 21:56:56 +0100186static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no,
187 phys_addr_t addr,
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530188 u64 pci_addr, size_t size)
189{
190 int ret;
191 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
192 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
193
194 ret = dw_pcie_ep_outbound_atu(ep, addr, pci_addr, size);
195 if (ret) {
196 dev_err(pci->dev, "failed to enable address\n");
197 return ret;
198 }
199
200 return 0;
201}
202
Cyrille Pitchen44947382018-01-30 21:56:56 +0100203static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no)
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530204{
205 int val;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530206 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
207 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
208
Kishon Vijay Abraham Ia134a452017-12-19 15:25:41 +0530209 val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
210 if (!(val & MSI_CAP_MSI_EN_MASK))
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530211 return -EINVAL;
212
Kishon Vijay Abraham Ia134a452017-12-19 15:25:41 +0530213 val = (val & MSI_CAP_MME_MASK) >> MSI_CAP_MME_SHIFT;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530214 return val;
215}
216
Cyrille Pitchen44947382018-01-30 21:56:56 +0100217static 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 +0530218{
219 int val;
220 struct dw_pcie_ep *ep = epc_get_drvdata(epc);
221 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
222
Niklas Cassel099a95f2017-12-20 00:29:23 +0100223 val = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
224 val &= ~MSI_CAP_MMC_MASK;
225 val |= (encode_int << MSI_CAP_MMC_SHIFT) & MSI_CAP_MMC_MASK;
Niklas Cassel1cab8262017-12-20 00:29:24 +0100226 dw_pcie_dbi_ro_wr_en(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530227 dw_pcie_writew_dbi(pci, MSI_MESSAGE_CONTROL, val);
Niklas Cassel1cab8262017-12-20 00:29:24 +0100228 dw_pcie_dbi_ro_wr_dis(pci);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530229
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
Bjorn Helgaas16093362018-02-01 11:36:07 -0600241 return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530242}
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
Bjorn Helgaas16093362018-02-01 11:36:07 -0600279int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100280 u8 interrupt_num)
281{
282 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
283 struct pci_epc *epc = ep->epc;
284 u16 msg_ctrl, msg_data;
285 u32 msg_addr_lower, msg_addr_upper;
286 u64 msg_addr;
287 bool has_upper;
288 int ret;
289
290 /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
291 msg_ctrl = dw_pcie_readw_dbi(pci, MSI_MESSAGE_CONTROL);
292 has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
293 msg_addr_lower = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_L32);
294 if (has_upper) {
295 msg_addr_upper = dw_pcie_readl_dbi(pci, MSI_MESSAGE_ADDR_U32);
296 msg_data = dw_pcie_readw_dbi(pci, MSI_MESSAGE_DATA_64);
297 } else {
298 msg_addr_upper = 0;
299 msg_data = dw_pcie_readw_dbi(pci, MSI_MESSAGE_DATA_32);
300 }
301 msg_addr = ((u64) msg_addr_upper) << 32 | msg_addr_lower;
Bjorn Helgaas16093362018-02-01 11:36:07 -0600302 ret = dw_pcie_ep_map_addr(epc, func_no, ep->msi_mem_phys, msg_addr,
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100303 epc->mem->page_size);
304 if (ret)
305 return ret;
306
307 writel(msg_data | (interrupt_num - 1), ep->msi_mem);
308
Bjorn Helgaas16093362018-02-01 11:36:07 -0600309 dw_pcie_ep_unmap_addr(epc, func_no, ep->msi_mem_phys);
Niklas Cassel6f6d7872017-12-20 00:29:27 +0100310
311 return 0;
312}
313
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530314void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
315{
316 struct pci_epc *epc = ep->epc;
317
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100318 pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
319 epc->mem->page_size);
320
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530321 pci_epc_mem_exit(epc);
322}
323
324int dw_pcie_ep_init(struct dw_pcie_ep *ep)
325{
326 int ret;
327 void *addr;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530328 struct pci_epc *epc;
329 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
330 struct device *dev = pci->dev;
331 struct device_node *np = dev->of_node;
332
333 if (!pci->dbi_base || !pci->dbi_base2) {
334 dev_err(dev, "dbi_base/deb_base2 is not populated\n");
335 return -EINVAL;
336 }
337
338 ret = of_property_read_u32(np, "num-ib-windows", &ep->num_ib_windows);
339 if (ret < 0) {
340 dev_err(dev, "unable to read *num-ib-windows* property\n");
341 return ret;
342 }
Niklas Casselad4a5be2017-12-14 14:01:44 +0100343 if (ep->num_ib_windows > MAX_IATU_IN) {
344 dev_err(dev, "invalid *num-ib-windows*\n");
345 return -EINVAL;
346 }
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530347
348 ret = of_property_read_u32(np, "num-ob-windows", &ep->num_ob_windows);
349 if (ret < 0) {
350 dev_err(dev, "unable to read *num-ob-windows* property\n");
351 return ret;
352 }
Niklas Casselad4a5be2017-12-14 14:01:44 +0100353 if (ep->num_ob_windows > MAX_IATU_OUT) {
354 dev_err(dev, "invalid *num-ob-windows*\n");
355 return -EINVAL;
356 }
357
358 ep->ib_window_map = devm_kzalloc(dev, sizeof(long) *
359 BITS_TO_LONGS(ep->num_ib_windows),
360 GFP_KERNEL);
361 if (!ep->ib_window_map)
362 return -ENOMEM;
363
364 ep->ob_window_map = devm_kzalloc(dev, sizeof(long) *
365 BITS_TO_LONGS(ep->num_ob_windows),
366 GFP_KERNEL);
367 if (!ep->ob_window_map)
368 return -ENOMEM;
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530369
370 addr = devm_kzalloc(dev, sizeof(phys_addr_t) * ep->num_ob_windows,
371 GFP_KERNEL);
372 if (!addr)
373 return -ENOMEM;
374 ep->outbound_addr = addr;
375
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530376 if (ep->ops->ep_init)
377 ep->ops->ep_init(ep);
378
379 epc = devm_pci_epc_create(dev, &epc_ops);
380 if (IS_ERR(epc)) {
381 dev_err(dev, "failed to create epc device\n");
382 return PTR_ERR(epc);
383 }
384
385 ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
386 if (ret < 0)
387 epc->max_functions = 1;
388
Kishon Vijay Abraham Ia937fe02017-08-18 20:28:02 +0530389 ret = __pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
390 ep->page_size);
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530391 if (ret < 0) {
392 dev_err(dev, "Failed to initialize address space\n");
393 return ret;
394 }
395
Niklas Cassel2fd0c9d2017-12-20 00:29:25 +0100396 ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
397 epc->mem->page_size);
398 if (!ep->msi_mem) {
399 dev_err(dev, "Failed to reserve memory for MSI\n");
400 return -ENOMEM;
401 }
402
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530403 ep->epc = epc;
404 epc_set_drvdata(epc, ep);
405 dw_pcie_setup(pci);
406
407 return 0;
408}