1 // SPDX-License-Identifier: GPL-2.0
2 /* sercos3: UIO driver for the Automata Sercos III PCI card
4 Copyright (C) 2008 Linutronix GmbH
5 Author: John Ogness <john.ogness@linutronix.de>
7 This is a straight-forward UIO driver, where interrupts are disabled
8 by the interrupt handler and re-enabled via a write to the UIO device
11 The only part that may seem odd is the use of a logical OR when
12 storing and restoring enabled interrupts. This is done because the
13 userspace-part could directly modify the Interrupt Enable Register
14 at any time. To reduce possible conflicts, the kernel driver uses
15 a logical OR to make more controlled changes (rather than blindly
16 overwriting previous values).
18 Race conditions exist if the userspace-part directly modifies the
19 Interrupt Enable Register while in operation. The consequences are
20 that certain interrupts would fail to be enabled or disabled. For
21 this reason, the userspace-part should only directly modify the
22 Interrupt Enable Register at the beginning (to get things going).
23 The userspace-part can safely disable interrupts at any time using
24 a write to the UIO device.
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/uio_driver.h>
32 #include <linux/slab.h>
34 /* ID's for SERCOS III PCI card (PLX 9030) */
35 #define SERCOS_SUB_VENDOR_ID 0x1971
36 #define SERCOS_SUB_SYSID_3530 0x3530
37 #define SERCOS_SUB_SYSID_3535 0x3535
38 #define SERCOS_SUB_SYSID_3780 0x3780
40 /* Interrupt Enable Register */
41 #define IER0_OFFSET 0x08
43 /* Interrupt Status Register */
44 #define ISR0_OFFSET 0x18
48 spinlock_t ier0_cache_lock
;
51 /* this function assumes ier0_cache_lock is locked! */
52 static void sercos3_disable_interrupts(struct uio_info
*info
,
53 struct sercos3_priv
*priv
)
55 void __iomem
*ier0
= info
->mem
[3].internal_addr
+ IER0_OFFSET
;
57 /* add enabled interrupts to cache */
58 priv
->ier0_cache
|= ioread32(ier0
);
60 /* disable interrupts */
64 /* this function assumes ier0_cache_lock is locked! */
65 static void sercos3_enable_interrupts(struct uio_info
*info
,
66 struct sercos3_priv
*priv
)
68 void __iomem
*ier0
= info
->mem
[3].internal_addr
+ IER0_OFFSET
;
70 /* restore previously enabled interrupts */
71 iowrite32(ioread32(ier0
) | priv
->ier0_cache
, ier0
);
75 static irqreturn_t
sercos3_handler(int irq
, struct uio_info
*info
)
77 struct sercos3_priv
*priv
= info
->priv
;
78 void __iomem
*isr0
= info
->mem
[3].internal_addr
+ ISR0_OFFSET
;
79 void __iomem
*ier0
= info
->mem
[3].internal_addr
+ IER0_OFFSET
;
81 if (!(ioread32(isr0
) & ioread32(ier0
)))
84 spin_lock(&priv
->ier0_cache_lock
);
85 sercos3_disable_interrupts(info
, priv
);
86 spin_unlock(&priv
->ier0_cache_lock
);
91 static int sercos3_irqcontrol(struct uio_info
*info
, s32 irq_on
)
93 struct sercos3_priv
*priv
= info
->priv
;
95 spin_lock_irq(&priv
->ier0_cache_lock
);
97 sercos3_enable_interrupts(info
, priv
);
99 sercos3_disable_interrupts(info
, priv
);
100 spin_unlock_irq(&priv
->ier0_cache_lock
);
105 static int sercos3_setup_iomem(struct pci_dev
*dev
, struct uio_info
*info
,
108 info
->mem
[n
].addr
= pci_resource_start(dev
, pci_bar
);
109 if (!info
->mem
[n
].addr
)
111 info
->mem
[n
].internal_addr
= ioremap(pci_resource_start(dev
, pci_bar
),
112 pci_resource_len(dev
, pci_bar
));
113 if (!info
->mem
[n
].internal_addr
)
115 info
->mem
[n
].size
= pci_resource_len(dev
, pci_bar
);
116 info
->mem
[n
].memtype
= UIO_MEM_PHYS
;
120 static int sercos3_pci_probe(struct pci_dev
*dev
,
121 const struct pci_device_id
*id
)
123 struct uio_info
*info
;
124 struct sercos3_priv
*priv
;
127 info
= kzalloc(sizeof(struct uio_info
), GFP_KERNEL
);
131 priv
= kzalloc(sizeof(struct sercos3_priv
), GFP_KERNEL
);
135 if (pci_enable_device(dev
))
138 if (pci_request_regions(dev
, "sercos3"))
141 /* we only need PCI BAR's 0, 2, 3, 4, 5 */
142 if (sercos3_setup_iomem(dev
, info
, 0, 0))
144 if (sercos3_setup_iomem(dev
, info
, 1, 2))
146 if (sercos3_setup_iomem(dev
, info
, 2, 3))
148 if (sercos3_setup_iomem(dev
, info
, 3, 4))
150 if (sercos3_setup_iomem(dev
, info
, 4, 5))
153 spin_lock_init(&priv
->ier0_cache_lock
);
155 info
->name
= "Sercos_III_PCI";
156 info
->version
= "0.0.1";
157 info
->irq
= dev
->irq
;
158 info
->irq_flags
= IRQF_SHARED
;
159 info
->handler
= sercos3_handler
;
160 info
->irqcontrol
= sercos3_irqcontrol
;
162 pci_set_drvdata(dev
, info
);
164 if (uio_register_device(&dev
->dev
, info
))
170 for (i
= 0; i
< 5; i
++) {
171 if (info
->mem
[i
].internal_addr
)
172 iounmap(info
->mem
[i
].internal_addr
);
174 pci_release_regions(dev
);
176 pci_disable_device(dev
);
184 static void sercos3_pci_remove(struct pci_dev
*dev
)
186 struct uio_info
*info
= pci_get_drvdata(dev
);
189 uio_unregister_device(info
);
190 pci_release_regions(dev
);
191 pci_disable_device(dev
);
192 for (i
= 0; i
< 5; i
++) {
193 if (info
->mem
[i
].internal_addr
)
194 iounmap(info
->mem
[i
].internal_addr
);
200 static struct pci_device_id sercos3_pci_ids
[] = {
202 .vendor
= PCI_VENDOR_ID_PLX
,
203 .device
= PCI_DEVICE_ID_PLX_9030
,
204 .subvendor
= SERCOS_SUB_VENDOR_ID
,
205 .subdevice
= SERCOS_SUB_SYSID_3530
,
208 .vendor
= PCI_VENDOR_ID_PLX
,
209 .device
= PCI_DEVICE_ID_PLX_9030
,
210 .subvendor
= SERCOS_SUB_VENDOR_ID
,
211 .subdevice
= SERCOS_SUB_SYSID_3535
,
214 .vendor
= PCI_VENDOR_ID_PLX
,
215 .device
= PCI_DEVICE_ID_PLX_9030
,
216 .subvendor
= SERCOS_SUB_VENDOR_ID
,
217 .subdevice
= SERCOS_SUB_SYSID_3780
,
222 static struct pci_driver sercos3_pci_driver
= {
224 .id_table
= sercos3_pci_ids
,
225 .probe
= sercos3_pci_probe
,
226 .remove
= sercos3_pci_remove
,
229 module_pci_driver(sercos3_pci_driver
);
230 MODULE_DESCRIPTION("UIO driver for the Automata Sercos III PCI card");
231 MODULE_AUTHOR("John Ogness <john.ogness@linutronix.de>");
232 MODULE_LICENSE("GPL v2");