1 // SPDX-License-Identifier: GPL-2.0
3 * ddbridge.c: Digital Devices PCIe bridge driver
5 * Copyright (C) 2010-2017 Digital Devices GmbH
6 * Ralph Metzler <rjkm@metzlerbros.de>
7 * Marcus Metzler <mocm@metzlerbros.de>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/poll.h>
19 #include <linux/pci.h>
20 #include <linux/pci_ids.h>
21 #include <linux/timer.h>
22 #include <linux/i2c.h>
23 #include <linux/swab.h>
24 #include <linux/vmalloc.h>
27 #include "ddbridge-i2c.h"
28 #include "ddbridge-regs.h"
29 #include "ddbridge-hw.h"
30 #include "ddbridge-io.h"
32 /****************************************************************************/
33 /* module parameters */
36 #ifdef CONFIG_DVB_DDBRIDGE_MSIENABLE
41 module_param(msi
, int, 0444);
42 #ifdef CONFIG_DVB_DDBRIDGE_MSIENABLE
43 MODULE_PARM_DESC(msi
, "Control MSI interrupts: 0-disable, 1-enable (default)");
45 MODULE_PARM_DESC(msi
, "Control MSI interrupts: 0-disable (default), 1-enable");
49 /****************************************************************************/
50 /****************************************************************************/
51 /****************************************************************************/
53 static void ddb_irq_disable(struct ddb
*dev
)
55 ddbwritel(dev
, 0, INTERRUPT_ENABLE
);
56 ddbwritel(dev
, 0, MSI1_ENABLE
);
59 static void ddb_msi_exit(struct ddb
*dev
)
63 pci_free_irq_vectors(dev
->pdev
);
67 static void ddb_irq_exit(struct ddb
*dev
)
71 free_irq(pci_irq_vector(dev
->pdev
, 1), dev
);
72 free_irq(pci_irq_vector(dev
->pdev
, 0), dev
);
75 static void ddb_remove(struct pci_dev
*pdev
)
77 struct ddb
*dev
= (struct ddb
*)pci_get_drvdata(pdev
);
79 ddb_device_destroy(dev
);
80 ddb_ports_detach(dev
);
85 ddb_ports_release(dev
);
86 ddb_buffers_free(dev
);
89 pci_set_drvdata(pdev
, NULL
);
90 pci_disable_device(pdev
);
94 static void ddb_irq_msi(struct ddb
*dev
, int nr
)
98 if (msi
&& pci_msi_enabled()) {
99 stat
= pci_alloc_irq_vectors(dev
->pdev
, 1, nr
,
100 PCI_IRQ_MSI
| PCI_IRQ_MSIX
);
103 dev_info(dev
->dev
, "using %d MSI interrupt(s)\n",
106 dev_info(dev
->dev
, "MSI not available.\n");
112 static int ddb_irq_init(struct ddb
*dev
)
115 int irq_flag
= IRQF_SHARED
;
117 ddbwritel(dev
, 0x00000000, INTERRUPT_ENABLE
);
118 ddbwritel(dev
, 0x00000000, MSI1_ENABLE
);
119 ddbwritel(dev
, 0x00000000, MSI2_ENABLE
);
120 ddbwritel(dev
, 0x00000000, MSI3_ENABLE
);
121 ddbwritel(dev
, 0x00000000, MSI4_ENABLE
);
122 ddbwritel(dev
, 0x00000000, MSI5_ENABLE
);
123 ddbwritel(dev
, 0x00000000, MSI6_ENABLE
);
124 ddbwritel(dev
, 0x00000000, MSI7_ENABLE
);
126 #ifdef CONFIG_PCI_MSI
132 stat
= request_irq(pci_irq_vector(dev
->pdev
, 0),
133 ddb_irq_handler0
, irq_flag
, "ddbridge",
137 stat
= request_irq(pci_irq_vector(dev
->pdev
, 1),
138 ddb_irq_handler1
, irq_flag
, "ddbridge",
141 free_irq(pci_irq_vector(dev
->pdev
, 0), dev
);
147 stat
= request_irq(pci_irq_vector(dev
->pdev
, 0),
148 ddb_irq_handler
, irq_flag
, "ddbridge",
154 ddbwritel(dev
, 0x0fffff00, INTERRUPT_ENABLE
);
155 ddbwritel(dev
, 0x0000000f, MSI1_ENABLE
);
157 ddbwritel(dev
, 0x0fffff0f, INTERRUPT_ENABLE
);
158 ddbwritel(dev
, 0x00000000, MSI1_ENABLE
);
163 static int ddb_probe(struct pci_dev
*pdev
,
164 const struct pci_device_id
*id
)
169 if (pci_enable_device(pdev
) < 0)
172 pci_set_master(pdev
);
174 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(64)))
175 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32)))
178 dev
= vzalloc(sizeof(*dev
));
182 mutex_init(&dev
->mutex
);
185 dev
->dev
= &pdev
->dev
;
186 pci_set_drvdata(pdev
, dev
);
188 dev
->link
[0].ids
.vendor
= id
->vendor
;
189 dev
->link
[0].ids
.device
= id
->device
;
190 dev
->link
[0].ids
.subvendor
= id
->subvendor
;
191 dev
->link
[0].ids
.subdevice
= pdev
->subsystem_device
;
192 dev
->link
[0].ids
.devid
= (id
->device
<< 16) | id
->vendor
;
194 dev
->link
[0].dev
= dev
;
195 dev
->link
[0].info
= get_ddb_info(id
->vendor
, id
->device
,
196 id
->subvendor
, pdev
->subsystem_device
);
198 dev_info(&pdev
->dev
, "detected %s\n", dev
->link
[0].info
->name
);
200 dev
->regs_len
= pci_resource_len(dev
->pdev
, 0);
201 dev
->regs
= ioremap(pci_resource_start(dev
->pdev
, 0),
202 pci_resource_len(dev
->pdev
, 0));
205 dev_err(&pdev
->dev
, "not enough memory for register map\n");
209 if (ddbreadl(dev
, 0) == 0xffffffff) {
210 dev_err(&pdev
->dev
, "cannot read registers\n");
215 dev
->link
[0].ids
.hwid
= ddbreadl(dev
, 0);
216 dev
->link
[0].ids
.regmapid
= ddbreadl(dev
, 4);
218 dev_info(&pdev
->dev
, "HW %08x REGMAP %08x\n",
219 dev
->link
[0].ids
.hwid
, dev
->link
[0].ids
.regmapid
);
221 ddbwritel(dev
, 0, DMA_BASE_READ
);
222 ddbwritel(dev
, 0, DMA_BASE_WRITE
);
224 stat
= ddb_irq_init(dev
);
228 if (ddb_init(dev
) == 0)
233 dev_err(&pdev
->dev
, "fail0\n");
236 dev_err(&pdev
->dev
, "fail\n");
239 pci_set_drvdata(pdev
, NULL
);
240 pci_disable_device(pdev
);
244 /****************************************************************************/
245 /****************************************************************************/
246 /****************************************************************************/
248 #define DDB_DEVICE_ANY(_device) \
249 { PCI_DEVICE_SUB(DDVID, _device, DDVID, PCI_ANY_ID) }
251 static const struct pci_device_id ddb_id_table
[] = {
252 DDB_DEVICE_ANY(0x0002),
253 DDB_DEVICE_ANY(0x0003),
254 DDB_DEVICE_ANY(0x0005),
255 DDB_DEVICE_ANY(0x0006),
256 DDB_DEVICE_ANY(0x0007),
257 DDB_DEVICE_ANY(0x0008),
258 DDB_DEVICE_ANY(0x0009),
259 DDB_DEVICE_ANY(0x0011),
260 DDB_DEVICE_ANY(0x0012),
261 DDB_DEVICE_ANY(0x0013),
262 DDB_DEVICE_ANY(0x0201),
263 DDB_DEVICE_ANY(0x0203),
264 DDB_DEVICE_ANY(0x0210),
265 DDB_DEVICE_ANY(0x0220),
266 DDB_DEVICE_ANY(0x0320),
267 DDB_DEVICE_ANY(0x0321),
268 DDB_DEVICE_ANY(0x0322),
269 DDB_DEVICE_ANY(0x0323),
270 DDB_DEVICE_ANY(0x0328),
271 DDB_DEVICE_ANY(0x0329),
275 MODULE_DEVICE_TABLE(pci
, ddb_id_table
);
277 static struct pci_driver ddb_pci_driver
= {
279 .id_table
= ddb_id_table
,
281 .remove
= ddb_remove
,
284 static __init
int module_init_ddbridge(void)
288 pr_info("Digital Devices PCIE bridge driver "
290 ", Copyright (C) 2010-17 Digital Devices GmbH\n");
291 stat
= ddb_init_ddbridge();
294 stat
= pci_register_driver(&ddb_pci_driver
);
296 ddb_exit_ddbridge(0, stat
);
301 static __exit
void module_exit_ddbridge(void)
303 pci_unregister_driver(&ddb_pci_driver
);
304 ddb_exit_ddbridge(0, 0);
307 module_init(module_init_ddbridge
);
308 module_exit(module_exit_ddbridge
);
310 MODULE_DESCRIPTION("Digital Devices PCIe Bridge");
311 MODULE_AUTHOR("Ralph and Marcus Metzler, Metzler Brothers Systementwicklung GbR");
312 MODULE_LICENSE("GPL v2");
313 MODULE_VERSION(DDBRIDGE_VERSION
);