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