blob: b93a5f8f4cba2e065925cc004c1d38fb3525756e [file] [log] [blame]
Stephen Hemmingerbce5c2e2018-07-21 06:31:39 -07001// SPDX-License-Identifier: GPL-2.0
John Ognessa6030fcc2008-09-18 11:57:15 +02002/* 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
John Ognessa6030fcc2008-09-18 11:57:15 +020033
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
46struct sercos3_priv {
47 u32 ier0_cache;
48 spinlock_t ier0_cache_lock;
49};
50
51/* this function assumes ier0_cache_lock is locked! */
52static 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! */
65static 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
75static 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
91static 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
105static 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
Bill Pembertonb17b75b2012-11-19 13:21:49 -0500120static int sercos3_pci_probe(struct pci_dev *dev,
John Ognessa6030fcc2008-09-18 11:57:15 +0200121 const struct pci_device_id *id)
122{
123 struct uio_info *info;
124 struct sercos3_priv *priv;
125 int i;
126
Alexandru Ardelean023c9c62020-11-20 10:42:05 +0200127 info = devm_kzalloc(&dev->dev, sizeof(struct uio_info), GFP_KERNEL);
John Ognessa6030fcc2008-09-18 11:57:15 +0200128 if (!info)
129 return -ENOMEM;
130
Alexandru Ardelean023c9c62020-11-20 10:42:05 +0200131 priv = devm_kzalloc(&dev->dev, sizeof(struct sercos3_priv), GFP_KERNEL);
John Ognessa6030fcc2008-09-18 11:57:15 +0200132 if (!priv)
Alexandru Ardelean023c9c62020-11-20 10:42:05 +0200133 return -ENOMEM;
John Ognessa6030fcc2008-09-18 11:57:15 +0200134
135 if (pci_enable_device(dev))
Alexandru Ardelean023c9c62020-11-20 10:42:05 +0200136 return -ENODEV;
John Ognessa6030fcc2008-09-18 11:57:15 +0200137
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;
Hans J. Koch62c86772010-06-10 01:15:49 +0200158 info->irq_flags = IRQF_SHARED;
John Ognessa6030fcc2008-09-18 11:57:15 +0200159 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
169out_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);
175out_disable:
176 pci_disable_device(dev);
John Ognessa6030fcc2008-09-18 11:57:15 +0200177 return -ENODEV;
178}
179
180static 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);
John Ognessa6030fcc2008-09-18 11:57:15 +0200188 for (i = 0; i < 5; i++) {
189 if (info->mem[i].internal_addr)
190 iounmap(info->mem[i].internal_addr);
191 }
John Ognessa6030fcc2008-09-18 11:57:15 +0200192}
193
Bill Pembertond46f7432012-11-19 13:24:33 -0500194static struct pci_device_id sercos3_pci_ids[] = {
John Ognessa6030fcc2008-09-18 11:57:15 +0200195 {
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
216static 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
Peter Huewe8bcaec42013-05-20 22:47:44 +0200223module_pci_driver(sercos3_pci_driver);
John Ognessa6030fcc2008-09-18 11:57:15 +0200224MODULE_DESCRIPTION("UIO driver for the Automata Sercos III PCI card");
225MODULE_AUTHOR("John Ogness <john.ogness@linutronix.de>");
226MODULE_LICENSE("GPL v2");