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