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