Linux-libre 4.4.228-gnu
[librecmc/linux-libre.git] / drivers / staging / vme / devices / vme_pio2_core.c
1 /*
2  * GE PIO2 6U VME I/O Driver
3  *
4  * Author: Martyn Welch <martyn.welch@ge.com>
5  * Copyright 2009 GE Intelligent Platforms Embedded Systems, Inc.
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/device.h>
20 #include <linux/ctype.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23 #include <linux/vme.h>
24
25 #include "vme_pio2.h"
26
27 static const char driver_name[] = "pio2";
28
29 static int bus[PIO2_CARDS_MAX];
30 static int bus_num;
31 static long base[PIO2_CARDS_MAX];
32 static int base_num;
33 static int vector[PIO2_CARDS_MAX];
34 static int vector_num;
35 static int level[PIO2_CARDS_MAX];
36 static int level_num;
37 static char *variant[PIO2_CARDS_MAX];
38 static int variant_num;
39
40 static bool loopback;
41
42 static int pio2_match(struct vme_dev *);
43 static int pio2_probe(struct vme_dev *);
44 static int pio2_remove(struct vme_dev *);
45
46 static int pio2_get_led(struct pio2_card *card)
47 {
48         /* Can't read hardware, state saved in structure */
49         return card->led;
50 }
51
52 static int pio2_set_led(struct pio2_card *card, int state)
53 {
54         u8 reg;
55         int retval;
56
57         reg = card->irq_level;
58
59         /* Register state inverse of led state */
60         if (!state)
61                 reg |= PIO2_LED;
62
63         if (loopback)
64                 reg |= PIO2_LOOP;
65
66         retval = vme_master_write(card->window, &reg, 1, PIO2_REGS_CTRL);
67         if (retval < 0)
68                 return retval;
69
70         card->led = state ? 1 : 0;
71
72         return 0;
73 }
74
75 static void pio2_int(int level, int vector, void *ptr)
76 {
77         int vec, i, channel, retval;
78         u8 reg;
79         struct pio2_card *card  = ptr;
80
81         vec = vector & ~PIO2_VME_VECTOR_MASK;
82
83         switch (vec) {
84         case 0:
85                 dev_warn(&card->vdev->dev, "Spurious Interrupt\n");
86                 break;
87         case 1:
88         case 2:
89         case 3:
90         case 4:
91                 /* Channels 0 to 7 */
92                 retval = vme_master_read(card->window, &reg, 1,
93                         PIO2_REGS_INT_STAT[vec - 1]);
94                 if (retval < 0) {
95                         dev_err(&card->vdev->dev,
96                                 "Unable to read IRQ status register\n");
97                         return;
98                 }
99                 for (i = 0; i < 8; i++) {
100                         channel = ((vec - 1) * 8) + i;
101                         if (reg & PIO2_CHANNEL_BIT[channel])
102                                 dev_info(&card->vdev->dev,
103                                         "Interrupt on I/O channel %d\n",
104                                         channel);
105                 }
106                 break;
107         case 5:
108         case 6:
109         case 7:
110         case 8:
111         case 9:
112         case 10:
113                 /* Counters are dealt with by their own handler */
114                 dev_err(&card->vdev->dev,
115                         "Counter interrupt\n");
116                 break;
117         }
118 }
119
120 /*
121  * We return whether this has been successful - this is used in the probe to
122  * ensure we have a valid card.
123  */
124 static int pio2_reset_card(struct pio2_card *card)
125 {
126         int retval = 0;
127         u8 data = 0;
128
129         /* Clear main register*/
130         retval = vme_master_write(card->window, &data, 1, PIO2_REGS_CTRL);
131         if (retval < 0)
132                 return retval;
133
134         /* Clear VME vector */
135         retval = vme_master_write(card->window, &data, 1, PIO2_REGS_VME_VECTOR);
136         if (retval < 0)
137                 return retval;
138
139         /* Reset GPIO */
140         retval = pio2_gpio_reset(card);
141         if (retval < 0)
142                 return retval;
143
144         /* Reset counters */
145         retval = pio2_cntr_reset(card);
146         if (retval < 0)
147                 return retval;
148
149         return 0;
150 }
151
152 static struct vme_driver pio2_driver = {
153         .name = driver_name,
154         .match = pio2_match,
155         .probe = pio2_probe,
156         .remove = pio2_remove,
157 };
158
159 static int __init pio2_init(void)
160 {
161         if (bus_num == 0) {
162                 pr_err("No cards, skipping registration\n");
163                 return -ENODEV;
164         }
165
166         if (bus_num > PIO2_CARDS_MAX) {
167                 pr_err("Driver only able to handle %d PIO2 Cards\n",
168                        PIO2_CARDS_MAX);
169                 bus_num = PIO2_CARDS_MAX;
170         }
171
172         /* Register the PIO2 driver */
173         return  vme_register_driver(&pio2_driver, bus_num);
174 }
175
176 static int pio2_match(struct vme_dev *vdev)
177 {
178         if (vdev->num >= bus_num) {
179                 dev_err(&vdev->dev,
180                         "The enumeration of the VMEbus to which the board is connected must be specified\n");
181                 return 0;
182         }
183
184         if (vdev->num >= base_num) {
185                 dev_err(&vdev->dev,
186                         "The VME address for the cards registers must be specified\n");
187                 return 0;
188         }
189
190         if (vdev->num >= vector_num) {
191                 dev_err(&vdev->dev,
192                         "The IRQ vector used by the card must be specified\n");
193                 return 0;
194         }
195
196         if (vdev->num >= level_num) {
197                 dev_err(&vdev->dev,
198                         "The IRQ level used by the card must be specified\n");
199                 return 0;
200         }
201
202         if (vdev->num >= variant_num) {
203                 dev_err(&vdev->dev, "The variant of the card must be specified\n");
204                 return 0;
205         }
206
207         return 1;
208 }
209
210 static int pio2_probe(struct vme_dev *vdev)
211 {
212         struct pio2_card *card;
213         int retval;
214         int i;
215         u8 reg;
216         int vec;
217
218         card = kzalloc(sizeof(struct pio2_card), GFP_KERNEL);
219         if (!card) {
220                 retval = -ENOMEM;
221                 goto err_struct;
222         }
223
224         card->id = vdev->num;
225         card->bus = bus[card->id];
226         card->base = base[card->id];
227         card->irq_vector = vector[card->id];
228         card->irq_level = level[card->id] & PIO2_VME_INT_MASK;
229         strncpy(card->variant, variant[card->id], PIO2_VARIANT_LENGTH);
230         card->vdev = vdev;
231
232         for (i = 0; i < PIO2_VARIANT_LENGTH; i++) {
233                 if (isdigit(card->variant[i]) == 0) {
234                         dev_err(&card->vdev->dev, "Variant invalid\n");
235                         retval = -EINVAL;
236                         goto err_variant;
237                 }
238         }
239
240         /*
241          * Bottom 4 bits of VME interrupt vector used to determine source,
242          * provided vector should only use upper 4 bits.
243          */
244         if (card->irq_vector & ~PIO2_VME_VECTOR_MASK) {
245                 dev_err(&card->vdev->dev,
246                         "Invalid VME IRQ Vector, vector must not use lower 4 bits\n");
247                 retval = -EINVAL;
248                 goto err_vector;
249         }
250
251         /*
252          * There is no way to determine the build variant or whether each bank
253          * is input, output or both at run time. The inputs are also inverted
254          * if configured as both.
255          *
256          * We pass in the board variant and use that to determine the
257          * configuration of the banks.
258          */
259         for (i = 1; i < PIO2_VARIANT_LENGTH; i++) {
260                 switch (card->variant[i]) {
261                 case '0':
262                         card->bank[i - 1].config = NOFIT;
263                         break;
264                 case '1':
265                 case '2':
266                 case '3':
267                 case '4':
268                         card->bank[i - 1].config = INPUT;
269                         break;
270                 case '5':
271                         card->bank[i - 1].config = OUTPUT;
272                         break;
273                 case '6':
274                 case '7':
275                 case '8':
276                 case '9':
277                         card->bank[i - 1].config = BOTH;
278                         break;
279                 }
280         }
281
282         /* Get a master window and position over regs */
283         card->window = vme_master_request(vdev, VME_A24, VME_SCT, VME_D16);
284         if (!card->window) {
285                 dev_err(&card->vdev->dev,
286                         "Unable to assign VME master resource\n");
287                 retval = -EIO;
288                 goto err_window;
289         }
290
291         retval = vme_master_set(card->window, 1, card->base, 0x10000, VME_A24,
292                 (VME_SCT | VME_USER | VME_DATA), VME_D16);
293         if (retval) {
294                 dev_err(&card->vdev->dev,
295                         "Unable to configure VME master resource\n");
296                 goto err_set;
297         }
298
299         /*
300          * There is also no obvious register which we can probe to determine
301          * whether the provided base is valid. If we can read the "ID Register"
302          * offset and the reset function doesn't error, assume we have a valid
303          * location.
304          */
305         retval = vme_master_read(card->window, &reg, 1, PIO2_REGS_ID);
306         if (retval < 0) {
307                 dev_err(&card->vdev->dev, "Unable to read from device\n");
308                 goto err_read;
309         }
310
311         dev_dbg(&card->vdev->dev, "ID Register:%x\n", reg);
312
313         /*
314          * Ensure all the I/O is cleared. We can't read back the states, so
315          * this is the only method we have to ensure that the I/O is in a known
316          * state.
317          */
318         retval = pio2_reset_card(card);
319         if (retval) {
320                 dev_err(&card->vdev->dev,
321                         "Failed to reset card, is location valid?\n");
322                 retval = -ENODEV;
323                 goto err_reset;
324         }
325
326         /* Configure VME Interrupts */
327         reg = card->irq_level;
328         if (pio2_get_led(card))
329                 reg |= PIO2_LED;
330         if (loopback)
331                 reg |= PIO2_LOOP;
332         retval = vme_master_write(card->window, &reg, 1, PIO2_REGS_CTRL);
333         if (retval < 0)
334                 return retval;
335
336         /* Set VME vector */
337         retval = vme_master_write(card->window, &card->irq_vector, 1,
338                 PIO2_REGS_VME_VECTOR);
339         if (retval < 0)
340                 return retval;
341
342         /* Attach spurious interrupt handler. */
343         vec = card->irq_vector | PIO2_VME_VECTOR_SPUR;
344
345         retval = vme_irq_request(vdev, card->irq_level, vec,
346                 &pio2_int, (void *)card);
347         if (retval < 0) {
348                 dev_err(&card->vdev->dev,
349                         "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
350                          vec, card->irq_level);
351                 goto err_irq;
352         }
353
354         /* Attach GPIO interrupt handlers. */
355         for (i = 0; i < 4; i++) {
356                 vec = card->irq_vector | PIO2_VECTOR_BANK[i];
357
358                 retval = vme_irq_request(vdev, card->irq_level, vec,
359                         &pio2_int, (void *)card);
360                 if (retval < 0) {
361                         dev_err(&card->vdev->dev,
362                                 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
363                                  vec, card->irq_level);
364                         goto err_gpio_irq;
365                 }
366         }
367
368         /* Attach counter interrupt handlers. */
369         for (i = 0; i < 6; i++) {
370                 vec = card->irq_vector | PIO2_VECTOR_CNTR[i];
371
372                 retval = vme_irq_request(vdev, card->irq_level, vec,
373                         &pio2_int, (void *)card);
374                 if (retval < 0) {
375                         dev_err(&card->vdev->dev,
376                                 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
377                                 vec, card->irq_level);
378                         goto err_cntr_irq;
379                 }
380         }
381
382         /* Register IO */
383         retval = pio2_gpio_init(card);
384         if (retval < 0) {
385                 dev_err(&card->vdev->dev,
386                         "Unable to register with GPIO framework\n");
387                 goto err_gpio;
388         }
389
390         /* Set LED - This also sets interrupt level */
391         retval = pio2_set_led(card, 0);
392         if (retval < 0) {
393                 dev_err(&card->vdev->dev, "Unable to set LED\n");
394                 goto err_led;
395         }
396
397         dev_set_drvdata(&card->vdev->dev, card);
398
399         dev_info(&card->vdev->dev,
400                 "PIO2 (variant %s) configured at 0x%lx\n", card->variant,
401                 card->base);
402
403         return 0;
404
405 err_led:
406         pio2_gpio_exit(card);
407 err_gpio:
408         i = 6;
409 err_cntr_irq:
410         while (i > 0) {
411                 i--;
412                 vec = card->irq_vector | PIO2_VECTOR_CNTR[i];
413                 vme_irq_free(vdev, card->irq_level, vec);
414         }
415
416         i = 4;
417 err_gpio_irq:
418         while (i > 0) {
419                 i--;
420                 vec = card->irq_vector | PIO2_VECTOR_BANK[i];
421                 vme_irq_free(vdev, card->irq_level, vec);
422         }
423
424         vec = (card->irq_vector & PIO2_VME_VECTOR_MASK) | PIO2_VME_VECTOR_SPUR;
425         vme_irq_free(vdev, card->irq_level, vec);
426 err_irq:
427          pio2_reset_card(card);
428 err_reset:
429 err_read:
430         vme_master_set(card->window, 0, 0, 0, VME_A16, 0, VME_D16);
431 err_set:
432         vme_master_free(card->window);
433 err_window:
434 err_vector:
435 err_variant:
436         kfree(card);
437 err_struct:
438         return retval;
439 }
440
441 static int pio2_remove(struct vme_dev *vdev)
442 {
443         int vec;
444         int i;
445
446         struct pio2_card *card = dev_get_drvdata(&vdev->dev);
447
448         pio2_gpio_exit(card);
449
450         for (i = 0; i < 6; i++) {
451                 vec = card->irq_vector | PIO2_VECTOR_CNTR[i];
452                 vme_irq_free(vdev, card->irq_level, vec);
453         }
454
455         for (i = 0; i < 4; i++) {
456                 vec = card->irq_vector | PIO2_VECTOR_BANK[i];
457                 vme_irq_free(vdev, card->irq_level, vec);
458         }
459
460         vec = (card->irq_vector & PIO2_VME_VECTOR_MASK) | PIO2_VME_VECTOR_SPUR;
461         vme_irq_free(vdev, card->irq_level, vec);
462
463         pio2_reset_card(card);
464
465         vme_master_set(card->window, 0, 0, 0, VME_A16, 0, VME_D16);
466
467         vme_master_free(card->window);
468
469         kfree(card);
470
471         return 0;
472 }
473
474 static void __exit pio2_exit(void)
475 {
476         vme_unregister_driver(&pio2_driver);
477 }
478
479 /* These are required for each board */
480 MODULE_PARM_DESC(bus, "Enumeration of VMEbus to which the board is connected");
481 module_param_array(bus, int, &bus_num, S_IRUGO);
482
483 MODULE_PARM_DESC(base, "Base VME address for PIO2 Registers");
484 module_param_array(base, long, &base_num, S_IRUGO);
485
486 MODULE_PARM_DESC(vector, "VME IRQ Vector (Lower 4 bits masked)");
487 module_param_array(vector, int, &vector_num, S_IRUGO);
488
489 MODULE_PARM_DESC(level, "VME IRQ Level");
490 module_param_array(level, int, &level_num, S_IRUGO);
491
492 MODULE_PARM_DESC(variant, "Last 4 characters of PIO2 board variant");
493 module_param_array(variant, charp, &variant_num, S_IRUGO);
494
495 /* This is for debugging */
496 MODULE_PARM_DESC(loopback, "Enable loopback mode on all cards");
497 module_param(loopback, bool, S_IRUGO);
498
499 MODULE_DESCRIPTION("GE PIO2 6U VME I/O Driver");
500 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
501 MODULE_LICENSE("GPL");
502
503 module_init(pio2_init);
504 module_exit(pio2_exit);
505