Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / uio / uio_sercos3.c
blobb93a5f8f4cba2e065925cc004c1d38fb3525756e
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
9 by the userspace-part.
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>
31 #include <linux/io.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
46 struct sercos3_priv {
47 u32 ier0_cache;
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 */
61 iowrite32(0, ier0);
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);
72 priv->ier0_cache = 0;
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)))
82 return IRQ_NONE;
84 spin_lock(&priv->ier0_cache_lock);
85 sercos3_disable_interrupts(info, priv);
86 spin_unlock(&priv->ier0_cache_lock);
88 return IRQ_HANDLED;
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);
96 if (irq_on)
97 sercos3_enable_interrupts(info, priv);
98 else
99 sercos3_disable_interrupts(info, priv);
100 spin_unlock_irq(&priv->ier0_cache_lock);
102 return 0;
105 static int sercos3_setup_iomem(struct pci_dev *dev, struct uio_info *info,
106 int n, int pci_bar)
108 info->mem[n].addr = pci_resource_start(dev, pci_bar);
109 if (!info->mem[n].addr)
110 return -1;
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)
114 return -1;
115 info->mem[n].size = pci_resource_len(dev, pci_bar);
116 info->mem[n].memtype = UIO_MEM_PHYS;
117 return 0;
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;
125 int i;
127 info = devm_kzalloc(&dev->dev, sizeof(struct uio_info), GFP_KERNEL);
128 if (!info)
129 return -ENOMEM;
131 priv = devm_kzalloc(&dev->dev, sizeof(struct sercos3_priv), GFP_KERNEL);
132 if (!priv)
133 return -ENOMEM;
135 if (pci_enable_device(dev))
136 return -ENODEV;
138 if (pci_request_regions(dev, "sercos3"))
139 goto out_disable;
141 /* we only need PCI BAR's 0, 2, 3, 4, 5 */
142 if (sercos3_setup_iomem(dev, info, 0, 0))
143 goto out_unmap;
144 if (sercos3_setup_iomem(dev, info, 1, 2))
145 goto out_unmap;
146 if (sercos3_setup_iomem(dev, info, 2, 3))
147 goto out_unmap;
148 if (sercos3_setup_iomem(dev, info, 3, 4))
149 goto out_unmap;
150 if (sercos3_setup_iomem(dev, info, 4, 5))
151 goto out_unmap;
153 spin_lock_init(&priv->ier0_cache_lock);
154 info->priv = priv;
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))
165 goto out_unmap;
167 return 0;
169 out_unmap:
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);
175 out_disable:
176 pci_disable_device(dev);
177 return -ENODEV;
180 static void sercos3_pci_remove(struct pci_dev *dev)
182 struct uio_info *info = pci_get_drvdata(dev);
183 int i;
185 uio_unregister_device(info);
186 pci_release_regions(dev);
187 pci_disable_device(dev);
188 for (i = 0; i < 5; i++) {
189 if (info->mem[i].internal_addr)
190 iounmap(info->mem[i].internal_addr);
194 static struct pci_device_id sercos3_pci_ids[] = {
196 .vendor = PCI_VENDOR_ID_PLX,
197 .device = PCI_DEVICE_ID_PLX_9030,
198 .subvendor = SERCOS_SUB_VENDOR_ID,
199 .subdevice = SERCOS_SUB_SYSID_3530,
202 .vendor = PCI_VENDOR_ID_PLX,
203 .device = PCI_DEVICE_ID_PLX_9030,
204 .subvendor = SERCOS_SUB_VENDOR_ID,
205 .subdevice = SERCOS_SUB_SYSID_3535,
208 .vendor = PCI_VENDOR_ID_PLX,
209 .device = PCI_DEVICE_ID_PLX_9030,
210 .subvendor = SERCOS_SUB_VENDOR_ID,
211 .subdevice = SERCOS_SUB_SYSID_3780,
213 { 0, }
216 static struct pci_driver sercos3_pci_driver = {
217 .name = "sercos3",
218 .id_table = sercos3_pci_ids,
219 .probe = sercos3_pci_probe,
220 .remove = sercos3_pci_remove,
223 module_pci_driver(sercos3_pci_driver);
224 MODULE_DESCRIPTION("UIO driver for the Automata Sercos III PCI card");
225 MODULE_AUTHOR("John Ogness <john.ogness@linutronix.de>");
226 MODULE_LICENSE("GPL v2");