1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * comedi/drivers/ni_labpc_pci.c
4  * Driver for National Instruments Lab-PC PCI-1200
5  * Copyright (C) 2001, 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net>
6  */
7 
8 /*
9  * Driver: ni_labpc_pci
10  * Description: National Instruments Lab-PC PCI-1200
11  * Devices: [National Instruments] PCI-1200 (ni_pci-1200)
12  * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
13  * Status: works
14  *
15  * This is the PCI-specific support split off from the ni_labpc driver.
16  *
17  * Configuration Options: not applicable, uses PCI auto config
18  *
19  * NI manuals:
20  * 340914a (pci-1200)
21  */
22 
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 
26 #include "../comedi_pci.h"
27 
28 #include "ni_labpc.h"
29 
30 enum labpc_pci_boardid {
31 	BOARD_NI_PCI1200,
32 };
33 
34 static const struct labpc_boardinfo labpc_pci_boards[] = {
35 	[BOARD_NI_PCI1200] = {
36 		.name			= "ni_pci-1200",
37 		.ai_speed		= 10000,
38 		.ai_scan_up		= 1,
39 		.has_ao			= 1,
40 		.is_labpc1200		= 1,
41 	},
42 };
43 
44 /* ripped from mite.h and mite_setup2() to avoid mite dependency */
45 #define MITE_IODWBSR	0xc0	/* IO Device Window Base Size Register */
46 #define WENAB		BIT(7)	/* window enable */
47 
48 static int labpc_pci_mite_init(struct pci_dev *pcidev)
49 {
50 	void __iomem *mite_base;
51 	u32 main_phys_addr;
52 
53 	/* ioremap the MITE registers (BAR 0) temporarily */
54 	mite_base = pci_ioremap_bar(pcidev, 0);
55 	if (!mite_base)
56 		return -ENOMEM;
57 
58 	/* set data window to main registers (BAR 1) */
59 	main_phys_addr = pci_resource_start(pcidev, 1);
60 	writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
61 
62 	/* finished with MITE registers */
63 	iounmap(mite_base);
64 	return 0;
65 }
66 
67 static int labpc_pci_auto_attach(struct comedi_device *dev,
68 				 unsigned long context)
69 {
70 	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
71 	const struct labpc_boardinfo *board = NULL;
72 	int ret;
73 
74 	if (context < ARRAY_SIZE(labpc_pci_boards))
75 		board = &labpc_pci_boards[context];
76 	if (!board)
77 		return -ENODEV;
78 	dev->board_ptr = board;
79 	dev->board_name = board->name;
80 
81 	ret = comedi_pci_enable(dev);
82 	if (ret)
83 		return ret;
84 
85 	ret = labpc_pci_mite_init(pcidev);
86 	if (ret)
87 		return ret;
88 
89 	dev->mmio = pci_ioremap_bar(pcidev, 1);
90 	if (!dev->mmio)
91 		return -ENOMEM;
92 
93 	return labpc_common_attach(dev, pcidev->irq, IRQF_SHARED);
94 }
95 
96 static void labpc_pci_detach(struct comedi_device *dev)
97 {
98 	labpc_common_detach(dev);
99 	comedi_pci_detach(dev);
100 }
101 
102 static struct comedi_driver labpc_pci_comedi_driver = {
103 	.driver_name	= "labpc_pci",
104 	.module		= THIS_MODULE,
105 	.auto_attach	= labpc_pci_auto_attach,
106 	.detach		= labpc_pci_detach,
107 };
108 
109 static const struct pci_device_id labpc_pci_table[] = {
110 	{ PCI_VDEVICE(NI, 0x161), BOARD_NI_PCI1200 },
111 	{ 0 }
112 };
113 MODULE_DEVICE_TABLE(pci, labpc_pci_table);
114 
115 static int labpc_pci_probe(struct pci_dev *dev,
116 			   const struct pci_device_id *id)
117 {
118 	return comedi_pci_auto_config(dev, &labpc_pci_comedi_driver,
119 				      id->driver_data);
120 }
121 
122 static struct pci_driver labpc_pci_driver = {
123 	.name		= "labpc_pci",
124 	.id_table	= labpc_pci_table,
125 	.probe		= labpc_pci_probe,
126 	.remove		= comedi_pci_auto_unconfig,
127 };
128 module_comedi_pci_driver(labpc_pci_comedi_driver, labpc_pci_driver);
129 
130 MODULE_DESCRIPTION("Comedi: National Instruments Lab-PC PCI-1200 driver");
131 MODULE_AUTHOR("Comedi https://www.comedi.org");
132 MODULE_LICENSE("GPL");
133