]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/vme/devices/vme_pio2_core.c
fd19c257f53320a779d0ec3713c89c342b713cae
2 * GE PIO2 6U VME I/O Driver
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2009 GE Intelligent Platforms Embedded Systems, Inc.
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.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/device.h>
21 #include <linux/ctype.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
24 #include <linux/vme.h>
29 static const char driver_name
[] = "pio2";
31 static int bus
[PIO2_CARDS_MAX
];
33 static long base
[PIO2_CARDS_MAX
];
35 static int vector
[PIO2_CARDS_MAX
];
36 static int vector_num
;
37 static int level
[PIO2_CARDS_MAX
];
39 static char *variant
[PIO2_CARDS_MAX
];
40 static int variant_num
;
44 static int pio2_match(struct vme_dev
*);
45 static int pio2_probe(struct vme_dev
*);
46 static int pio2_remove(struct vme_dev
*);
48 static int pio2_get_led(struct pio2_card
*card
)
50 /* Can't read hardware, state saved in structure */
54 static int pio2_set_led(struct pio2_card
*card
, int state
)
59 reg
= card
->irq_level
;
61 /* Register state inverse of led state */
68 retval
= vme_master_write(card
->window
, ®
, 1, PIO2_REGS_CTRL
);
72 card
->led
= state
? 1 : 0;
77 static void pio2_int(int level
, int vector
, void *ptr
)
79 int vec
, i
, channel
, retval
;
81 struct pio2_card
*card
= ptr
;
83 vec
= vector
& ~PIO2_VME_VECTOR_MASK
;
87 dev_warn(&card
->vdev
->dev
, "Spurious Interrupt\n");
94 retval
= vme_master_read(card
->window
, ®
, 1,
95 PIO2_REGS_INT_STAT
[vec
- 1]);
97 dev_err(&card
->vdev
->dev
,
98 "Unable to read IRQ status register\n");
101 for (i
= 0; i
< 8; i
++) {
102 channel
= ((vec
- 1) * 8) + i
;
103 if (reg
& PIO2_CHANNEL_BIT
[channel
])
104 dev_info(&card
->vdev
->dev
,
105 "Interrupt on I/O channel %d\n",
115 /* Counters are dealt with by their own handler */
116 dev_err(&card
->vdev
->dev
,
117 "Counter interrupt\n");
124 * We return whether this has been successful - this is used in the probe to
125 * ensure we have a valid card.
127 static int pio2_reset_card(struct pio2_card
*card
)
132 /* Clear main register*/
133 retval
= vme_master_write(card
->window
, &data
, 1, PIO2_REGS_CTRL
);
137 /* Clear VME vector */
138 retval
= vme_master_write(card
->window
, &data
, 1, PIO2_REGS_VME_VECTOR
);
143 retval
= pio2_gpio_reset(card
);
148 retval
= pio2_cntr_reset(card
);
155 static struct vme_driver pio2_driver
= {
159 .remove
= pio2_remove
,
163 static int __init
pio2_init(void)
166 pr_err("No cards, skipping registration\n");
170 if (bus_num
> PIO2_CARDS_MAX
) {
171 pr_err("Driver only able to handle %d PIO2 Cards\n",
173 bus_num
= PIO2_CARDS_MAX
;
176 /* Register the PIO2 driver */
177 return vme_register_driver(&pio2_driver
, bus_num
);
180 static int pio2_match(struct vme_dev
*vdev
)
183 if (vdev
->num
>= bus_num
) {
185 "The enumeration of the VMEbus to which the board is connected must be specified");
189 if (vdev
->num
>= base_num
) {
191 "The VME address for the cards registers must be specified");
195 if (vdev
->num
>= vector_num
) {
197 "The IRQ vector used by the card must be specified");
201 if (vdev
->num
>= level_num
) {
203 "The IRQ level used by the card must be specified");
207 if (vdev
->num
>= variant_num
) {
208 dev_err(&vdev
->dev
, "The variant of the card must be specified");
215 static int pio2_probe(struct vme_dev
*vdev
)
217 struct pio2_card
*card
;
223 card
= kzalloc(sizeof(struct pio2_card
), GFP_KERNEL
);
229 card
->id
= vdev
->num
;
230 card
->bus
= bus
[card
->id
];
231 card
->base
= base
[card
->id
];
232 card
->irq_vector
= vector
[card
->id
];
233 card
->irq_level
= level
[card
->id
] & PIO2_VME_INT_MASK
;
234 strncpy(card
->variant
, variant
[card
->id
], PIO2_VARIANT_LENGTH
);
237 for (i
= 0; i
< PIO2_VARIANT_LENGTH
; i
++) {
239 if (isdigit(card
->variant
[i
]) == 0) {
240 dev_err(&card
->vdev
->dev
, "Variant invalid\n");
247 * Bottom 4 bits of VME interrupt vector used to determine source,
248 * provided vector should only use upper 4 bits.
250 if (card
->irq_vector
& ~PIO2_VME_VECTOR_MASK
) {
251 dev_err(&card
->vdev
->dev
,
252 "Invalid VME IRQ Vector, vector must not use lower 4 bits\n");
258 * There is no way to determine the build variant or whether each bank
259 * is input, output or both at run time. The inputs are also inverted
260 * if configured as both.
262 * We pass in the board variant and use that to determine the
263 * configuration of the banks.
265 for (i
= 1; i
< PIO2_VARIANT_LENGTH
; i
++) {
266 switch (card
->variant
[i
]) {
268 card
->bank
[i
-1].config
= NOFIT
;
274 card
->bank
[i
-1].config
= INPUT
;
277 card
->bank
[i
-1].config
= OUTPUT
;
283 card
->bank
[i
-1].config
= BOTH
;
288 /* Get a master window and position over regs */
289 card
->window
= vme_master_request(vdev
, VME_A24
, VME_SCT
, VME_D16
);
290 if (card
->window
== NULL
) {
291 dev_err(&card
->vdev
->dev
,
292 "Unable to assign VME master resource\n");
297 retval
= vme_master_set(card
->window
, 1, card
->base
, 0x10000, VME_A24
,
298 (VME_SCT
| VME_USER
| VME_DATA
), VME_D16
);
300 dev_err(&card
->vdev
->dev
,
301 "Unable to configure VME master resource\n");
306 * There is also no obvious register which we can probe to determine
307 * whether the provided base is valid. If we can read the "ID Register"
308 * offset and the reset function doesn't error, assume we have a valid
311 retval
= vme_master_read(card
->window
, ®
, 1, PIO2_REGS_ID
);
313 dev_err(&card
->vdev
->dev
, "Unable to read from device\n");
317 dev_dbg(&card
->vdev
->dev
, "ID Register:%x\n", reg
);
320 * Ensure all the I/O is cleared. We can't read back the states, so
321 * this is the only method we have to ensure that the I/O is in a known
324 retval
= pio2_reset_card(card
);
326 dev_err(&card
->vdev
->dev
,
327 "Failed to reset card, is location valid?");
332 /* Configure VME Interrupts */
333 reg
= card
->irq_level
;
334 if (pio2_get_led(card
))
338 retval
= vme_master_write(card
->window
, ®
, 1, PIO2_REGS_CTRL
);
343 retval
= vme_master_write(card
->window
, &card
->irq_vector
, 1,
344 PIO2_REGS_VME_VECTOR
);
348 /* Attach spurious interrupt handler. */
349 vec
= card
->irq_vector
| PIO2_VME_VECTOR_SPUR
;
351 retval
= vme_irq_request(vdev
, card
->irq_level
, vec
,
352 &pio2_int
, (void *)card
);
354 dev_err(&card
->vdev
->dev
,
355 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
356 vec
, card
->irq_level
);
360 /* Attach GPIO interrupt handlers. */
361 for (i
= 0; i
< 4; i
++) {
362 vec
= card
->irq_vector
| PIO2_VECTOR_BANK
[i
];
364 retval
= vme_irq_request(vdev
, card
->irq_level
, vec
,
365 &pio2_int
, (void *)card
);
367 dev_err(&card
->vdev
->dev
,
368 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
369 vec
, card
->irq_level
);
374 /* Attach counter interrupt handlers. */
375 for (i
= 0; i
< 6; i
++) {
376 vec
= card
->irq_vector
| PIO2_VECTOR_CNTR
[i
];
378 retval
= vme_irq_request(vdev
, card
->irq_level
, vec
,
379 &pio2_int
, (void *)card
);
381 dev_err(&card
->vdev
->dev
,
382 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
383 vec
, card
->irq_level
);
389 retval
= pio2_gpio_init(card
);
391 dev_err(&card
->vdev
->dev
,
392 "Unable to register with GPIO framework\n");
396 /* Set LED - This also sets interrupt level */
397 retval
= pio2_set_led(card
, 0);
399 dev_err(&card
->vdev
->dev
, "Unable to set LED\n");
403 dev_set_drvdata(&card
->vdev
->dev
, card
);
405 dev_info(&card
->vdev
->dev
,
406 "PIO2 (variant %s) configured at 0x%lx\n", card
->variant
,
412 pio2_gpio_exit(card
);
418 vec
= card
->irq_vector
| PIO2_VECTOR_CNTR
[i
];
419 vme_irq_free(vdev
, card
->irq_level
, vec
);
426 vec
= card
->irq_vector
| PIO2_VECTOR_BANK
[i
];
427 vme_irq_free(vdev
, card
->irq_level
, vec
);
430 vec
= (card
->irq_vector
& PIO2_VME_VECTOR_MASK
) | PIO2_VME_VECTOR_SPUR
;
431 vme_irq_free(vdev
, card
->irq_level
, vec
);
433 pio2_reset_card(card
);
436 vme_master_set(card
->window
, 0, 0, 0, VME_A16
, 0, VME_D16
);
438 vme_master_free(card
->window
);
447 static int pio2_remove(struct vme_dev
*vdev
)
452 struct pio2_card
*card
= dev_get_drvdata(&vdev
->dev
);
454 pio2_gpio_exit(card
);
456 for (i
= 0; i
< 6; i
++) {
457 vec
= card
->irq_vector
| PIO2_VECTOR_CNTR
[i
];
458 vme_irq_free(vdev
, card
->irq_level
, vec
);
461 for (i
= 0; i
< 4; i
++) {
462 vec
= card
->irq_vector
| PIO2_VECTOR_BANK
[i
];
463 vme_irq_free(vdev
, card
->irq_level
, vec
);
466 vec
= (card
->irq_vector
& PIO2_VME_VECTOR_MASK
) | PIO2_VME_VECTOR_SPUR
;
467 vme_irq_free(vdev
, card
->irq_level
, vec
);
469 pio2_reset_card(card
);
471 vme_master_set(card
->window
, 0, 0, 0, VME_A16
, 0, VME_D16
);
473 vme_master_free(card
->window
);
480 static void __exit
pio2_exit(void)
482 vme_unregister_driver(&pio2_driver
);
486 /* These are required for each board */
487 MODULE_PARM_DESC(bus
, "Enumeration of VMEbus to which the board is connected");
488 module_param_array(bus
, int, &bus_num
, S_IRUGO
);
490 MODULE_PARM_DESC(base
, "Base VME address for PIO2 Registers");
491 module_param_array(base
, long, &base_num
, S_IRUGO
);
493 MODULE_PARM_DESC(vector
, "VME IRQ Vector (Lower 4 bits masked)");
494 module_param_array(vector
, int, &vector_num
, S_IRUGO
);
496 MODULE_PARM_DESC(level
, "VME IRQ Level");
497 module_param_array(level
, int, &level_num
, S_IRUGO
);
499 MODULE_PARM_DESC(variant
, "Last 4 characters of PIO2 board variant");
500 module_param_array(variant
, charp
, &variant_num
, S_IRUGO
);
502 /* This is for debugging */
503 MODULE_PARM_DESC(loopback
, "Enable loopback mode on all cards");
504 module_param(loopback
, bool, S_IRUGO
);
506 MODULE_DESCRIPTION("GE PIO2 6U VME I/O Driver");
507 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
508 MODULE_LICENSE("GPL");
510 module_init(pio2_init
);
511 module_exit(pio2_exit
);