1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018-2019 Synopsys, Inc. and/or its affiliates.
4 * Synopsys DesignWare eDMA PCIe driver
6 * Author: Gustavo Pimentel <gustavo.pimentel@synopsys.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/device.h>
13 #include <linux/dma/edma.h>
14 #include <linux/pci-epf.h>
15 #include <linux/msi.h>
17 #include "dw-edma-core.h"
19 struct dw_edma_pcie_data
{
20 /* eDMA registers location */
21 enum pci_barno rg_bar
;
24 /* eDMA memory linked list location */
25 enum pci_barno ll_bar
;
28 /* eDMA memory data location */
29 enum pci_barno dt_bar
;
34 enum dw_edma_mode mode
;
38 static const struct dw_edma_pcie_data snps_edda_data
= {
39 /* eDMA registers location */
41 .rg_off
= 0x00001000, /* 4 Kbytes */
42 .rg_sz
= 0x00002000, /* 8 Kbytes */
43 /* eDMA memory linked list location */
45 .ll_off
= 0x00000000, /* 0 Kbytes */
46 .ll_sz
= 0x00800000, /* 8 Mbytes */
47 /* eDMA memory data location */
49 .dt_off
= 0x00800000, /* 8 Mbytes */
50 .dt_sz
= 0x03800000, /* 56 Mbytes */
53 .mode
= EDMA_MODE_UNROLL
,
57 static int dw_edma_pcie_irq_vector(struct device
*dev
, unsigned int nr
)
59 return pci_irq_vector(to_pci_dev(dev
), nr
);
62 static const struct dw_edma_core_ops dw_edma_pcie_core_ops
= {
63 .irq_vector
= dw_edma_pcie_irq_vector
,
66 static int dw_edma_pcie_probe(struct pci_dev
*pdev
,
67 const struct pci_device_id
*pid
)
69 const struct dw_edma_pcie_data
*pdata
= (void *)pid
->driver_data
;
70 struct device
*dev
= &pdev
->dev
;
71 struct dw_edma_chip
*chip
;
75 /* Enable PCI device */
76 err
= pcim_enable_device(pdev
);
78 pci_err(pdev
, "enabling device failed\n");
82 /* Mapping PCI BAR regions */
83 err
= pcim_iomap_regions(pdev
, BIT(pdata
->rg_bar
) |
88 pci_err(pdev
, "eDMA BAR I/O remapping failed\n");
94 /* DMA configuration */
95 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
97 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
99 pci_err(pdev
, "consistent DMA mask 64 set failed\n");
103 pci_err(pdev
, "DMA mask 64 set failed\n");
105 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
107 pci_err(pdev
, "DMA mask 32 set failed\n");
111 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
113 pci_err(pdev
, "consistent DMA mask 32 set failed\n");
118 /* Data structure allocation */
119 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
123 dw
= devm_kzalloc(dev
, sizeof(*dw
), GFP_KERNEL
);
127 /* IRQs allocation */
128 nr_irqs
= pci_alloc_irq_vectors(pdev
, 1, pdata
->irqs
,
129 PCI_IRQ_MSI
| PCI_IRQ_MSIX
);
131 pci_err(pdev
, "fail to alloc IRQ vector (number of IRQs=%u)\n",
136 /* Data structure initialization */
139 chip
->id
= pdev
->devfn
;
140 chip
->irq
= pdev
->irq
;
142 dw
->rg_region
.vaddr
= pcim_iomap_table(pdev
)[pdata
->rg_bar
];
143 dw
->rg_region
.vaddr
+= pdata
->rg_off
;
144 dw
->rg_region
.paddr
= pdev
->resource
[pdata
->rg_bar
].start
;
145 dw
->rg_region
.paddr
+= pdata
->rg_off
;
146 dw
->rg_region
.sz
= pdata
->rg_sz
;
148 dw
->ll_region
.vaddr
= pcim_iomap_table(pdev
)[pdata
->ll_bar
];
149 dw
->ll_region
.vaddr
+= pdata
->ll_off
;
150 dw
->ll_region
.paddr
= pdev
->resource
[pdata
->ll_bar
].start
;
151 dw
->ll_region
.paddr
+= pdata
->ll_off
;
152 dw
->ll_region
.sz
= pdata
->ll_sz
;
154 dw
->dt_region
.vaddr
= pcim_iomap_table(pdev
)[pdata
->dt_bar
];
155 dw
->dt_region
.vaddr
+= pdata
->dt_off
;
156 dw
->dt_region
.paddr
= pdev
->resource
[pdata
->dt_bar
].start
;
157 dw
->dt_region
.paddr
+= pdata
->dt_off
;
158 dw
->dt_region
.sz
= pdata
->dt_sz
;
160 dw
->version
= pdata
->version
;
161 dw
->mode
= pdata
->mode
;
162 dw
->nr_irqs
= nr_irqs
;
163 dw
->ops
= &dw_edma_pcie_core_ops
;
166 pci_dbg(pdev
, "Version:\t%u\n", dw
->version
);
168 pci_dbg(pdev
, "Mode:\t%s\n",
169 dw
->mode
== EDMA_MODE_LEGACY
? "Legacy" : "Unroll");
171 pci_dbg(pdev
, "Registers:\tBAR=%u, off=0x%.8lx, sz=0x%zx bytes, addr(v=%p, p=%pa)\n",
172 pdata
->rg_bar
, pdata
->rg_off
, pdata
->rg_sz
,
173 dw
->rg_region
.vaddr
, &dw
->rg_region
.paddr
);
175 pci_dbg(pdev
, "L. List:\tBAR=%u, off=0x%.8lx, sz=0x%zx bytes, addr(v=%p, p=%pa)\n",
176 pdata
->ll_bar
, pdata
->ll_off
, pdata
->ll_sz
,
177 dw
->ll_region
.vaddr
, &dw
->ll_region
.paddr
);
179 pci_dbg(pdev
, "Data:\tBAR=%u, off=0x%.8lx, sz=0x%zx bytes, addr(v=%p, p=%pa)\n",
180 pdata
->dt_bar
, pdata
->dt_off
, pdata
->dt_sz
,
181 dw
->dt_region
.vaddr
, &dw
->dt_region
.paddr
);
183 pci_dbg(pdev
, "Nr. IRQs:\t%u\n", dw
->nr_irqs
);
185 /* Validating if PCI interrupts were enabled */
186 if (!pci_dev_msi_enabled(pdev
)) {
187 pci_err(pdev
, "enable interrupt failed\n");
191 dw
->irq
= devm_kcalloc(dev
, nr_irqs
, sizeof(*dw
->irq
), GFP_KERNEL
);
195 /* Starting eDMA driver */
196 err
= dw_edma_probe(chip
);
198 pci_err(pdev
, "eDMA probe failed\n");
202 /* Saving data structure reference */
203 pci_set_drvdata(pdev
, chip
);
208 static void dw_edma_pcie_remove(struct pci_dev
*pdev
)
210 struct dw_edma_chip
*chip
= pci_get_drvdata(pdev
);
213 /* Stopping eDMA driver */
214 err
= dw_edma_remove(chip
);
216 pci_warn(pdev
, "can't remove device properly: %d\n", err
);
219 pci_free_irq_vectors(pdev
);
222 static const struct pci_device_id dw_edma_pcie_id_table
[] = {
223 { PCI_DEVICE_DATA(SYNOPSYS
, EDDA
, &snps_edda_data
) },
226 MODULE_DEVICE_TABLE(pci
, dw_edma_pcie_id_table
);
228 static struct pci_driver dw_edma_pcie_driver
= {
229 .name
= "dw-edma-pcie",
230 .id_table
= dw_edma_pcie_id_table
,
231 .probe
= dw_edma_pcie_probe
,
232 .remove
= dw_edma_pcie_remove
,
235 module_pci_driver(dw_edma_pcie_driver
);
237 MODULE_LICENSE("GPL v2");
238 MODULE_DESCRIPTION("Synopsys DesignWare eDMA PCIe driver");
239 MODULE_AUTHOR("Gustavo Pimentel <gustavo.pimentel@synopsys.com>");