1 /* sercos3: UIO driver for the Automata Sercos III PCI card
3 Copyright (C) 2008 Linutronix GmbH
4 Author: John Ogness <john.ogness@linutronix.de>
6 This is a straight-forward UIO driver, where interrupts are disabled
7 by the interrupt handler and re-enabled via a write to the UIO device
10 The only part that may seem odd is the use of a logical OR when
11 storing and restoring enabled interrupts. This is done because the
12 userspace-part could directly modify the Interrupt Enable Register
13 at any time. To reduce possible conflicts, the kernel driver uses
14 a logical OR to make more controlled changes (rather than blindly
15 overwriting previous values).
17 Race conditions exist if the userspace-part directly modifies the
18 Interrupt Enable Register while in operation. The consequences are
19 that certain interrupts would fail to be enabled or disabled. For
20 this reason, the userspace-part should only directly modify the
21 Interrupt Enable Register at the beginning (to get things going).
22 The userspace-part can safely disable interrupts at any time using
23 a write to the UIO device.
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/uio_driver.h>
31 #include <linux/slab.h>
33 /* ID's for SERCOS III PCI card (PLX 9030) */
34 #define SERCOS_SUB_VENDOR_ID 0x1971
35 #define SERCOS_SUB_SYSID_3530 0x3530
36 #define SERCOS_SUB_SYSID_3535 0x3535
37 #define SERCOS_SUB_SYSID_3780 0x3780
39 /* Interrupt Enable Register */
40 #define IER0_OFFSET 0x08
42 /* Interrupt Status Register */
43 #define ISR0_OFFSET 0x18
47 spinlock_t ier0_cache_lock
;
50 /* this function assumes ier0_cache_lock is locked! */
51 static void sercos3_disable_interrupts(struct uio_info
*info
,
52 struct sercos3_priv
*priv
)
54 void __iomem
*ier0
= info
->mem
[3].internal_addr
+ IER0_OFFSET
;
56 /* add enabled interrupts to cache */
57 priv
->ier0_cache
|= ioread32(ier0
);
59 /* disable interrupts */
63 /* this function assumes ier0_cache_lock is locked! */
64 static void sercos3_enable_interrupts(struct uio_info
*info
,
65 struct sercos3_priv
*priv
)
67 void __iomem
*ier0
= info
->mem
[3].internal_addr
+ IER0_OFFSET
;
69 /* restore previously enabled interrupts */
70 iowrite32(ioread32(ier0
) | priv
->ier0_cache
, ier0
);
74 static irqreturn_t
sercos3_handler(int irq
, struct uio_info
*info
)
76 struct sercos3_priv
*priv
= info
->priv
;
77 void __iomem
*isr0
= info
->mem
[3].internal_addr
+ ISR0_OFFSET
;
78 void __iomem
*ier0
= info
->mem
[3].internal_addr
+ IER0_OFFSET
;
80 if (!(ioread32(isr0
) & ioread32(ier0
)))
83 spin_lock(&priv
->ier0_cache_lock
);
84 sercos3_disable_interrupts(info
, priv
);
85 spin_unlock(&priv
->ier0_cache_lock
);
90 static int sercos3_irqcontrol(struct uio_info
*info
, s32 irq_on
)
92 struct sercos3_priv
*priv
= info
->priv
;
94 spin_lock_irq(&priv
->ier0_cache_lock
);
96 sercos3_enable_interrupts(info
, priv
);
98 sercos3_disable_interrupts(info
, priv
);
99 spin_unlock_irq(&priv
->ier0_cache_lock
);
104 static int sercos3_setup_iomem(struct pci_dev
*dev
, struct uio_info
*info
,
107 info
->mem
[n
].addr
= pci_resource_start(dev
, pci_bar
);
108 if (!info
->mem
[n
].addr
)
110 info
->mem
[n
].internal_addr
= ioremap(pci_resource_start(dev
, pci_bar
),
111 pci_resource_len(dev
, pci_bar
));
112 if (!info
->mem
[n
].internal_addr
)
114 info
->mem
[n
].size
= pci_resource_len(dev
, pci_bar
);
115 info
->mem
[n
].memtype
= UIO_MEM_PHYS
;
119 static int sercos3_pci_probe(struct pci_dev
*dev
,
120 const struct pci_device_id
*id
)
122 struct uio_info
*info
;
123 struct sercos3_priv
*priv
;
126 info
= kzalloc(sizeof(struct uio_info
), GFP_KERNEL
);
130 priv
= kzalloc(sizeof(struct sercos3_priv
), GFP_KERNEL
);
134 if (pci_enable_device(dev
))
137 if (pci_request_regions(dev
, "sercos3"))
140 /* we only need PCI BAR's 0, 2, 3, 4, 5 */
141 if (sercos3_setup_iomem(dev
, info
, 0, 0))
143 if (sercos3_setup_iomem(dev
, info
, 1, 2))
145 if (sercos3_setup_iomem(dev
, info
, 2, 3))
147 if (sercos3_setup_iomem(dev
, info
, 3, 4))
149 if (sercos3_setup_iomem(dev
, info
, 4, 5))
152 spin_lock_init(&priv
->ier0_cache_lock
);
154 info
->name
= "Sercos_III_PCI";
155 info
->version
= "0.0.1";
156 info
->irq
= dev
->irq
;
157 info
->irq_flags
= IRQF_SHARED
;
158 info
->handler
= sercos3_handler
;
159 info
->irqcontrol
= sercos3_irqcontrol
;
161 pci_set_drvdata(dev
, info
);
163 if (uio_register_device(&dev
->dev
, info
))
169 for (i
= 0; i
< 5; i
++) {
170 if (info
->mem
[i
].internal_addr
)
171 iounmap(info
->mem
[i
].internal_addr
);
173 pci_release_regions(dev
);
175 pci_disable_device(dev
);
183 static void sercos3_pci_remove(struct pci_dev
*dev
)
185 struct uio_info
*info
= pci_get_drvdata(dev
);
188 uio_unregister_device(info
);
189 pci_release_regions(dev
);
190 pci_disable_device(dev
);
191 for (i
= 0; i
< 5; i
++) {
192 if (info
->mem
[i
].internal_addr
)
193 iounmap(info
->mem
[i
].internal_addr
);
199 static struct pci_device_id sercos3_pci_ids
[] = {
201 .vendor
= PCI_VENDOR_ID_PLX
,
202 .device
= PCI_DEVICE_ID_PLX_9030
,
203 .subvendor
= SERCOS_SUB_VENDOR_ID
,
204 .subdevice
= SERCOS_SUB_SYSID_3530
,
207 .vendor
= PCI_VENDOR_ID_PLX
,
208 .device
= PCI_DEVICE_ID_PLX_9030
,
209 .subvendor
= SERCOS_SUB_VENDOR_ID
,
210 .subdevice
= SERCOS_SUB_SYSID_3535
,
213 .vendor
= PCI_VENDOR_ID_PLX
,
214 .device
= PCI_DEVICE_ID_PLX_9030
,
215 .subvendor
= SERCOS_SUB_VENDOR_ID
,
216 .subdevice
= SERCOS_SUB_SYSID_3780
,
221 static struct pci_driver sercos3_pci_driver
= {
223 .id_table
= sercos3_pci_ids
,
224 .probe
= sercos3_pci_probe
,
225 .remove
= sercos3_pci_remove
,
228 module_pci_driver(sercos3_pci_driver
);
229 MODULE_DESCRIPTION("UIO driver for the Automata Sercos III PCI card");
230 MODULE_AUTHOR("John Ogness <john.ogness@linutronix.de>");
231 MODULE_LICENSE("GPL v2");