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/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>
27 static const char driver_name
[] = "pio2";
29 static int bus
[PIO2_CARDS_MAX
];
31 static long base
[PIO2_CARDS_MAX
];
33 static int vector
[PIO2_CARDS_MAX
];
34 static int vector_num
;
35 static int level
[PIO2_CARDS_MAX
];
37 static char *variant
[PIO2_CARDS_MAX
];
38 static int variant_num
;
42 static int pio2_match(struct vme_dev
*);
43 static int pio2_probe(struct vme_dev
*);
44 static int pio2_remove(struct vme_dev
*);
46 static int pio2_get_led(struct pio2_card
*card
)
48 /* Can't read hardware, state saved in structure */
52 static int pio2_set_led(struct pio2_card
*card
, int state
)
57 reg
= card
->irq_level
;
59 /* Register state inverse of led state */
66 retval
= vme_master_write(card
->window
, ®
, 1, PIO2_REGS_CTRL
);
70 card
->led
= state
? 1 : 0;
75 static void pio2_int(int level
, int vector
, void *ptr
)
77 int vec
, i
, channel
, retval
;
79 struct pio2_card
*card
= ptr
;
81 vec
= vector
& ~PIO2_VME_VECTOR_MASK
;
85 dev_warn(&card
->vdev
->dev
, "Spurious Interrupt\n");
92 retval
= vme_master_read(card
->window
, ®
, 1,
93 PIO2_REGS_INT_STAT
[vec
- 1]);
95 dev_err(&card
->vdev
->dev
,
96 "Unable to read IRQ status register\n");
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",
113 /* Counters are dealt with by their own handler */
114 dev_err(&card
->vdev
->dev
,
115 "Counter interrupt\n");
121 * We return whether this has been successful - this is used in the probe to
122 * ensure we have a valid card.
124 static int pio2_reset_card(struct pio2_card
*card
)
129 /* Clear main register*/
130 retval
= vme_master_write(card
->window
, &data
, 1, PIO2_REGS_CTRL
);
134 /* Clear VME vector */
135 retval
= vme_master_write(card
->window
, &data
, 1, PIO2_REGS_VME_VECTOR
);
140 retval
= pio2_gpio_reset(card
);
145 retval
= pio2_cntr_reset(card
);
152 static struct vme_driver pio2_driver
= {
156 .remove
= pio2_remove
,
159 static int __init
pio2_init(void)
162 pr_err("No cards, skipping registration\n");
166 if (bus_num
> PIO2_CARDS_MAX
) {
167 pr_err("Driver only able to handle %d PIO2 Cards\n",
169 bus_num
= PIO2_CARDS_MAX
;
172 /* Register the PIO2 driver */
173 return vme_register_driver(&pio2_driver
, bus_num
);
176 static int pio2_match(struct vme_dev
*vdev
)
178 if (vdev
->num
>= bus_num
) {
180 "The enumeration of the VMEbus to which the board is connected must be specified\n");
184 if (vdev
->num
>= base_num
) {
186 "The VME address for the cards registers must be specified\n");
190 if (vdev
->num
>= vector_num
) {
192 "The IRQ vector used by the card must be specified\n");
196 if (vdev
->num
>= level_num
) {
198 "The IRQ level used by the card must be specified\n");
202 if (vdev
->num
>= variant_num
) {
203 dev_err(&vdev
->dev
, "The variant of the card must be specified\n");
210 static int pio2_probe(struct vme_dev
*vdev
)
212 struct pio2_card
*card
;
218 card
= devm_kzalloc(&vdev
->dev
, sizeof(*card
), GFP_KERNEL
);
222 card
->id
= vdev
->num
;
223 card
->bus
= bus
[card
->id
];
224 card
->base
= base
[card
->id
];
225 card
->irq_vector
= vector
[card
->id
];
226 card
->irq_level
= level
[card
->id
] & PIO2_VME_INT_MASK
;
227 strncpy(card
->variant
, variant
[card
->id
], PIO2_VARIANT_LENGTH
);
230 for (i
= 0; i
< PIO2_VARIANT_LENGTH
; i
++) {
231 if (!isdigit(card
->variant
[i
])) {
232 dev_err(&card
->vdev
->dev
, "Variant invalid\n");
238 * Bottom 4 bits of VME interrupt vector used to determine source,
239 * provided vector should only use upper 4 bits.
241 if (card
->irq_vector
& ~PIO2_VME_VECTOR_MASK
) {
242 dev_err(&card
->vdev
->dev
,
243 "Invalid VME IRQ Vector, vector must not use lower 4 bits\n");
248 * There is no way to determine the build variant or whether each bank
249 * is input, output or both at run time. The inputs are also inverted
250 * if configured as both.
252 * We pass in the board variant and use that to determine the
253 * configuration of the banks.
255 for (i
= 1; i
< PIO2_VARIANT_LENGTH
; i
++) {
256 switch (card
->variant
[i
]) {
258 card
->bank
[i
- 1].config
= NOFIT
;
264 card
->bank
[i
- 1].config
= INPUT
;
267 card
->bank
[i
- 1].config
= OUTPUT
;
273 card
->bank
[i
- 1].config
= BOTH
;
278 /* Get a master window and position over regs */
279 card
->window
= vme_master_request(vdev
, VME_A24
, VME_SCT
, VME_D16
);
281 dev_err(&card
->vdev
->dev
,
282 "Unable to assign VME master resource\n");
286 retval
= vme_master_set(card
->window
, 1, card
->base
, 0x10000, VME_A24
,
287 VME_SCT
| VME_USER
| VME_DATA
, VME_D16
);
289 dev_err(&card
->vdev
->dev
,
290 "Unable to configure VME master resource\n");
295 * There is also no obvious register which we can probe to determine
296 * whether the provided base is valid. If we can read the "ID Register"
297 * offset and the reset function doesn't error, assume we have a valid
300 retval
= vme_master_read(card
->window
, ®
, 1, PIO2_REGS_ID
);
302 dev_err(&card
->vdev
->dev
, "Unable to read from device\n");
306 dev_dbg(&card
->vdev
->dev
, "ID Register:%x\n", reg
);
309 * Ensure all the I/O is cleared. We can't read back the states, so
310 * this is the only method we have to ensure that the I/O is in a known
313 retval
= pio2_reset_card(card
);
315 dev_err(&card
->vdev
->dev
,
316 "Failed to reset card, is location valid?\n");
321 /* Configure VME Interrupts */
322 reg
= card
->irq_level
;
323 if (pio2_get_led(card
))
327 retval
= vme_master_write(card
->window
, ®
, 1, PIO2_REGS_CTRL
);
332 retval
= vme_master_write(card
->window
, &card
->irq_vector
, 1,
333 PIO2_REGS_VME_VECTOR
);
337 /* Attach spurious interrupt handler. */
338 vec
= card
->irq_vector
| PIO2_VME_VECTOR_SPUR
;
340 retval
= vme_irq_request(vdev
, card
->irq_level
, vec
,
343 dev_err(&card
->vdev
->dev
,
344 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
345 vec
, card
->irq_level
);
349 /* Attach GPIO interrupt handlers. */
350 for (i
= 0; i
< 4; i
++) {
351 vec
= card
->irq_vector
| PIO2_VECTOR_BANK
[i
];
353 retval
= vme_irq_request(vdev
, card
->irq_level
, vec
,
356 dev_err(&card
->vdev
->dev
,
357 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
358 vec
, card
->irq_level
);
363 /* Attach counter interrupt handlers. */
364 for (i
= 0; i
< 6; i
++) {
365 vec
= card
->irq_vector
| PIO2_VECTOR_CNTR
[i
];
367 retval
= vme_irq_request(vdev
, card
->irq_level
, vec
,
370 dev_err(&card
->vdev
->dev
,
371 "Unable to attach VME interrupt vector0x%x, level 0x%x\n",
372 vec
, card
->irq_level
);
378 retval
= pio2_gpio_init(card
);
380 dev_err(&card
->vdev
->dev
,
381 "Unable to register with GPIO framework\n");
385 /* Set LED - This also sets interrupt level */
386 retval
= pio2_set_led(card
, 0);
388 dev_err(&card
->vdev
->dev
, "Unable to set LED\n");
392 dev_set_drvdata(&card
->vdev
->dev
, card
);
394 dev_info(&card
->vdev
->dev
,
395 "PIO2 (variant %s) configured at 0x%lx\n", card
->variant
,
401 pio2_gpio_exit(card
);
407 vec
= card
->irq_vector
| PIO2_VECTOR_CNTR
[i
];
408 vme_irq_free(vdev
, card
->irq_level
, vec
);
415 vec
= card
->irq_vector
| PIO2_VECTOR_BANK
[i
];
416 vme_irq_free(vdev
, card
->irq_level
, vec
);
419 vec
= (card
->irq_vector
& PIO2_VME_VECTOR_MASK
) | PIO2_VME_VECTOR_SPUR
;
420 vme_irq_free(vdev
, card
->irq_level
, vec
);
422 pio2_reset_card(card
);
425 vme_master_set(card
->window
, 0, 0, 0, VME_A16
, 0, VME_D16
);
427 vme_master_free(card
->window
);
431 static int pio2_remove(struct vme_dev
*vdev
)
436 struct pio2_card
*card
= dev_get_drvdata(&vdev
->dev
);
438 pio2_gpio_exit(card
);
440 for (i
= 0; i
< 6; i
++) {
441 vec
= card
->irq_vector
| PIO2_VECTOR_CNTR
[i
];
442 vme_irq_free(vdev
, card
->irq_level
, vec
);
445 for (i
= 0; i
< 4; i
++) {
446 vec
= card
->irq_vector
| PIO2_VECTOR_BANK
[i
];
447 vme_irq_free(vdev
, card
->irq_level
, vec
);
450 vec
= (card
->irq_vector
& PIO2_VME_VECTOR_MASK
) | PIO2_VME_VECTOR_SPUR
;
451 vme_irq_free(vdev
, card
->irq_level
, vec
);
453 pio2_reset_card(card
);
455 vme_master_set(card
->window
, 0, 0, 0, VME_A16
, 0, VME_D16
);
457 vme_master_free(card
->window
);
462 static void __exit
pio2_exit(void)
464 vme_unregister_driver(&pio2_driver
);
467 /* These are required for each board */
468 MODULE_PARM_DESC(bus
, "Enumeration of VMEbus to which the board is connected");
469 module_param_array(bus
, int, &bus_num
, S_IRUGO
);
471 MODULE_PARM_DESC(base
, "Base VME address for PIO2 Registers");
472 module_param_array(base
, long, &base_num
, S_IRUGO
);
474 MODULE_PARM_DESC(vector
, "VME IRQ Vector (Lower 4 bits masked)");
475 module_param_array(vector
, int, &vector_num
, S_IRUGO
);
477 MODULE_PARM_DESC(level
, "VME IRQ Level");
478 module_param_array(level
, int, &level_num
, S_IRUGO
);
480 MODULE_PARM_DESC(variant
, "Last 4 characters of PIO2 board variant");
481 module_param_array(variant
, charp
, &variant_num
, S_IRUGO
);
483 /* This is for debugging */
484 MODULE_PARM_DESC(loopback
, "Enable loopback mode on all cards");
485 module_param(loopback
, bool, S_IRUGO
);
487 MODULE_DESCRIPTION("GE PIO2 6U VME I/O Driver");
488 MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
489 MODULE_LICENSE("GPL");
491 module_init(pio2_init
);
492 module_exit(pio2_exit
);
This page took 0.045642 seconds and 5 git commands to generate.