xref: /openbmc/linux/drivers/uio/uio_sercos3.c (revision e2ad626f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* sercos3: UIO driver for the Automata Sercos III PCI card
3 
4    Copyright (C) 2008 Linutronix GmbH
5      Author: John Ogness <john.ogness@linutronix.de>
6 
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.
10 
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).
17 
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.
25 */
26 
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>
33 
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
39 
40 /* Interrupt Enable Register */
41 #define IER0_OFFSET 0x08
42 
43 /* Interrupt Status Register */
44 #define ISR0_OFFSET 0x18
45 
46 struct sercos3_priv {
47 	u32 ier0_cache;
48 	spinlock_t ier0_cache_lock;
49 };
50 
51 /* this function assumes ier0_cache_lock is locked! */
52 static void sercos3_disable_interrupts(struct uio_info *info,
53 				       struct sercos3_priv *priv)
54 {
55 	void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
56 
57 	/* add enabled interrupts to cache */
58 	priv->ier0_cache |= ioread32(ier0);
59 
60 	/* disable interrupts */
61 	iowrite32(0, ier0);
62 }
63 
64 /* this function assumes ier0_cache_lock is locked! */
65 static void sercos3_enable_interrupts(struct uio_info *info,
66 				      struct sercos3_priv *priv)
67 {
68 	void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
69 
70 	/* restore previously enabled interrupts */
71 	iowrite32(ioread32(ier0) | priv->ier0_cache, ier0);
72 	priv->ier0_cache = 0;
73 }
74 
75 static irqreturn_t sercos3_handler(int irq, struct uio_info *info)
76 {
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;
80 
81 	if (!(ioread32(isr0) & ioread32(ier0)))
82 		return IRQ_NONE;
83 
84 	spin_lock(&priv->ier0_cache_lock);
85 	sercos3_disable_interrupts(info, priv);
86 	spin_unlock(&priv->ier0_cache_lock);
87 
88 	return IRQ_HANDLED;
89 }
90 
91 static int sercos3_irqcontrol(struct uio_info *info, s32 irq_on)
92 {
93 	struct sercos3_priv *priv = info->priv;
94 
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);
101 
102 	return 0;
103 }
104 
105 static int sercos3_setup_iomem(struct pci_dev *dev, struct uio_info *info,
106 			       int n, int pci_bar)
107 {
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;
118 }
119 
120 static int sercos3_pci_probe(struct pci_dev *dev,
121 				       const struct pci_device_id *id)
122 {
123 	struct uio_info *info;
124 	struct sercos3_priv *priv;
125 	int i;
126 
127 	info = devm_kzalloc(&dev->dev, sizeof(struct uio_info), GFP_KERNEL);
128 	if (!info)
129 		return -ENOMEM;
130 
131 	priv = devm_kzalloc(&dev->dev, sizeof(struct sercos3_priv), GFP_KERNEL);
132 	if (!priv)
133 		return -ENOMEM;
134 
135 	if (pci_enable_device(dev))
136 		return -ENODEV;
137 
138 	if (pci_request_regions(dev, "sercos3"))
139 		goto out_disable;
140 
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;
152 
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;
161 
162 	pci_set_drvdata(dev, info);
163 
164 	if (uio_register_device(&dev->dev, info))
165 		goto out_unmap;
166 
167 	return 0;
168 
169 out_unmap:
170 	for (i = 0; i < 5; i++) {
171 		if (info->mem[i].internal_addr)
172 			iounmap(info->mem[i].internal_addr);
173 	}
174 	pci_release_regions(dev);
175 out_disable:
176 	pci_disable_device(dev);
177 	return -ENODEV;
178 }
179 
180 static void sercos3_pci_remove(struct pci_dev *dev)
181 {
182 	struct uio_info *info = pci_get_drvdata(dev);
183 	int i;
184 
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);
191 	}
192 }
193 
194 static struct pci_device_id sercos3_pci_ids[] = {
195 	{
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,
200 	},
201 	{
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,
206 	},
207 	{
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,
212 	},
213 	{ 0, }
214 };
215 
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,
221 };
222 
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");
227