Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uio_sercos3.c
Go to the documentation of this file.
1 /* sercos3: UIO driver for the Automata Sercos III PCI card
2 
3  Copyright (C) 2008 Linutronix GmbH
4  Author: John Ogness <[email protected]>
5 
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
8  by the userspace-part.
9 
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).
16 
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.
24 */
25 
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/uio_driver.h>
30 #include <linux/io.h>
31 #include <linux/slab.h>
32 
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
38 
39 /* Interrupt Enable Register */
40 #define IER0_OFFSET 0x08
41 
42 /* Interrupt Status Register */
43 #define ISR0_OFFSET 0x18
44 
45 struct sercos3_priv {
48 };
49 
50 /* this function assumes ier0_cache_lock is locked! */
51 static void sercos3_disable_interrupts(struct uio_info *info,
52  struct sercos3_priv *priv)
53 {
54  void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
55 
56  /* add enabled interrupts to cache */
57  priv->ier0_cache |= ioread32(ier0);
58 
59  /* disable interrupts */
60  iowrite32(0, ier0);
61 }
62 
63 /* this function assumes ier0_cache_lock is locked! */
64 static void sercos3_enable_interrupts(struct uio_info *info,
65  struct sercos3_priv *priv)
66 {
67  void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
68 
69  /* restore previously enabled interrupts */
70  iowrite32(ioread32(ier0) | priv->ier0_cache, ier0);
71  priv->ier0_cache = 0;
72 }
73 
74 static irqreturn_t sercos3_handler(int irq, struct uio_info *info)
75 {
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;
79 
80  if (!(ioread32(isr0) & ioread32(ier0)))
81  return IRQ_NONE;
82 
83  spin_lock(&priv->ier0_cache_lock);
84  sercos3_disable_interrupts(info, priv);
85  spin_unlock(&priv->ier0_cache_lock);
86 
87  return IRQ_HANDLED;
88 }
89 
90 static int sercos3_irqcontrol(struct uio_info *info, s32 irq_on)
91 {
92  struct sercos3_priv *priv = info->priv;
93 
94  spin_lock_irq(&priv->ier0_cache_lock);
95  if (irq_on)
96  sercos3_enable_interrupts(info, priv);
97  else
98  sercos3_disable_interrupts(info, priv);
99  spin_unlock_irq(&priv->ier0_cache_lock);
100 
101  return 0;
102 }
103 
104 static int sercos3_setup_iomem(struct pci_dev *dev, struct uio_info *info,
105  int n, int pci_bar)
106 {
107  info->mem[n].addr = pci_resource_start(dev, pci_bar);
108  if (!info->mem[n].addr)
109  return -1;
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)
113  return -1;
114  info->mem[n].size = pci_resource_len(dev, pci_bar);
115  info->mem[n].memtype = UIO_MEM_PHYS;
116  return 0;
117 }
118 
119 static int __devinit sercos3_pci_probe(struct pci_dev *dev,
120  const struct pci_device_id *id)
121 {
122  struct uio_info *info;
123  struct sercos3_priv *priv;
124  int i;
125 
126  info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
127  if (!info)
128  return -ENOMEM;
129 
130  priv = kzalloc(sizeof(struct sercos3_priv), GFP_KERNEL);
131  if (!priv)
132  goto out_free;
133 
134  if (pci_enable_device(dev))
135  goto out_free_priv;
136 
137  if (pci_request_regions(dev, "sercos3"))
138  goto out_disable;
139 
140  /* we only need PCI BAR's 0, 2, 3, 4, 5 */
141  if (sercos3_setup_iomem(dev, info, 0, 0))
142  goto out_unmap;
143  if (sercos3_setup_iomem(dev, info, 1, 2))
144  goto out_unmap;
145  if (sercos3_setup_iomem(dev, info, 2, 3))
146  goto out_unmap;
147  if (sercos3_setup_iomem(dev, info, 3, 4))
148  goto out_unmap;
149  if (sercos3_setup_iomem(dev, info, 4, 5))
150  goto out_unmap;
151 
153  info->priv = priv;
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;
160 
161  pci_set_drvdata(dev, info);
162 
163  if (uio_register_device(&dev->dev, info))
164  goto out_unmap;
165 
166  return 0;
167 
168 out_unmap:
169  for (i = 0; i < 5; i++) {
170  if (info->mem[i].internal_addr)
171  iounmap(info->mem[i].internal_addr);
172  }
173  pci_release_regions(dev);
174 out_disable:
175  pci_disable_device(dev);
176 out_free_priv:
177  kfree(priv);
178 out_free:
179  kfree(info);
180  return -ENODEV;
181 }
182 
183 static void sercos3_pci_remove(struct pci_dev *dev)
184 {
185  struct uio_info *info = pci_get_drvdata(dev);
186  int i;
187 
188  uio_unregister_device(info);
189  pci_release_regions(dev);
190  pci_disable_device(dev);
191  pci_set_drvdata(dev, NULL);
192  for (i = 0; i < 5; i++) {
193  if (info->mem[i].internal_addr)
194  iounmap(info->mem[i].internal_addr);
195  }
196  kfree(info->priv);
197  kfree(info);
198 }
199 
200 static struct pci_device_id sercos3_pci_ids[] __devinitdata = {
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_3530,
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_3535,
212  },
213  {
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,
218  },
219  { 0, }
220 };
221 
222 static struct pci_driver sercos3_pci_driver = {
223  .name = "sercos3",
224  .id_table = sercos3_pci_ids,
225  .probe = sercos3_pci_probe,
226  .remove = sercos3_pci_remove,
227 };
228 
229 static int __init sercos3_init_module(void)
230 {
231  return pci_register_driver(&sercos3_pci_driver);
232 }
233 
234 static void __exit sercos3_exit_module(void)
235 {
236  pci_unregister_driver(&sercos3_pci_driver);
237 }
238 
239 module_init(sercos3_init_module);
240 module_exit(sercos3_exit_module);
241 
242 MODULE_DESCRIPTION("UIO driver for the Automata Sercos III PCI card");
243 MODULE_AUTHOR("John Ogness <[email protected]>");
244 MODULE_LICENSE("GPL v2");