1 // SPDX-License-Identifier: GPL-2.0-only 2 /** 3 * tpci200.c 4 * 5 * driver for the TEWS TPCI-200 device 6 * 7 * Copyright (C) 2009-2012 CERN (www.cern.ch) 8 * Author: Nicolas Serafini, EIC2 SA 9 * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com> 10 */ 11 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include "tpci200.h" 15 16 static const u16 tpci200_status_timeout[] = { 17 TPCI200_A_TIMEOUT, 18 TPCI200_B_TIMEOUT, 19 TPCI200_C_TIMEOUT, 20 TPCI200_D_TIMEOUT, 21 }; 22 23 static const u16 tpci200_status_error[] = { 24 TPCI200_A_ERROR, 25 TPCI200_B_ERROR, 26 TPCI200_C_ERROR, 27 TPCI200_D_ERROR, 28 }; 29 30 static const size_t tpci200_space_size[IPACK_SPACE_COUNT] = { 31 [IPACK_IO_SPACE] = TPCI200_IO_SPACE_SIZE, 32 [IPACK_ID_SPACE] = TPCI200_ID_SPACE_SIZE, 33 [IPACK_INT_SPACE] = TPCI200_INT_SPACE_SIZE, 34 [IPACK_MEM8_SPACE] = TPCI200_MEM8_SPACE_SIZE, 35 [IPACK_MEM16_SPACE] = TPCI200_MEM16_SPACE_SIZE, 36 }; 37 38 static const size_t tpci200_space_interval[IPACK_SPACE_COUNT] = { 39 [IPACK_IO_SPACE] = TPCI200_IO_SPACE_INTERVAL, 40 [IPACK_ID_SPACE] = TPCI200_ID_SPACE_INTERVAL, 41 [IPACK_INT_SPACE] = TPCI200_INT_SPACE_INTERVAL, 42 [IPACK_MEM8_SPACE] = TPCI200_MEM8_SPACE_INTERVAL, 43 [IPACK_MEM16_SPACE] = TPCI200_MEM16_SPACE_INTERVAL, 44 }; 45 46 static struct tpci200_board *check_slot(struct ipack_device *dev) 47 { 48 struct tpci200_board *tpci200; 49 50 if (dev == NULL) 51 return NULL; 52 53 54 tpci200 = dev_get_drvdata(dev->bus->parent); 55 56 if (tpci200 == NULL) { 57 dev_info(&dev->dev, "carrier board not found\n"); 58 return NULL; 59 } 60 61 if (dev->slot >= TPCI200_NB_SLOT) { 62 dev_info(&dev->dev, 63 "Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n", 64 dev->bus->bus_nr, dev->slot, TPCI200_NB_SLOT-1); 65 return NULL; 66 } 67 68 return tpci200; 69 } 70 71 static void tpci200_clear_mask(struct tpci200_board *tpci200, 72 __le16 __iomem *addr, u16 mask) 73 { 74 unsigned long flags; 75 spin_lock_irqsave(&tpci200->regs_lock, flags); 76 iowrite16(ioread16(addr) & (~mask), addr); 77 spin_unlock_irqrestore(&tpci200->regs_lock, flags); 78 } 79 80 static void tpci200_set_mask(struct tpci200_board *tpci200, 81 __le16 __iomem *addr, u16 mask) 82 { 83 unsigned long flags; 84 spin_lock_irqsave(&tpci200->regs_lock, flags); 85 iowrite16(ioread16(addr) | mask, addr); 86 spin_unlock_irqrestore(&tpci200->regs_lock, flags); 87 } 88 89 static void tpci200_unregister(struct tpci200_board *tpci200) 90 { 91 free_irq(tpci200->info->pdev->irq, (void *) tpci200); 92 93 pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs); 94 95 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR); 96 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR); 97 pci_release_region(tpci200->info->pdev, TPCI200_MEM16_SPACE_BAR); 98 pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR); 99 100 pci_disable_device(tpci200->info->pdev); 101 } 102 103 static void tpci200_enable_irq(struct tpci200_board *tpci200, 104 int islot) 105 { 106 tpci200_set_mask(tpci200, 107 &tpci200->info->interface_regs->control[islot], 108 TPCI200_INT0_EN | TPCI200_INT1_EN); 109 } 110 111 static void tpci200_disable_irq(struct tpci200_board *tpci200, 112 int islot) 113 { 114 tpci200_clear_mask(tpci200, 115 &tpci200->info->interface_regs->control[islot], 116 TPCI200_INT0_EN | TPCI200_INT1_EN); 117 } 118 119 static irqreturn_t tpci200_slot_irq(struct slot_irq *slot_irq) 120 { 121 irqreturn_t ret; 122 123 if (!slot_irq) 124 return -ENODEV; 125 ret = slot_irq->handler(slot_irq->arg); 126 127 return ret; 128 } 129 130 static irqreturn_t tpci200_interrupt(int irq, void *dev_id) 131 { 132 struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id; 133 struct slot_irq *slot_irq; 134 irqreturn_t ret; 135 u16 status_reg; 136 int i; 137 138 /* Read status register */ 139 status_reg = ioread16(&tpci200->info->interface_regs->status); 140 141 /* Did we cause the interrupt? */ 142 if (!(status_reg & TPCI200_SLOT_INT_MASK)) 143 return IRQ_NONE; 144 145 /* callback to the IRQ handler for the corresponding slot */ 146 rcu_read_lock(); 147 for (i = 0; i < TPCI200_NB_SLOT; i++) { 148 if (!(status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2 * i)))) 149 continue; 150 slot_irq = rcu_dereference(tpci200->slots[i].irq); 151 ret = tpci200_slot_irq(slot_irq); 152 if (ret == -ENODEV) { 153 dev_info(&tpci200->info->pdev->dev, 154 "No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n", 155 tpci200->number, i); 156 tpci200_disable_irq(tpci200, i); 157 } 158 } 159 rcu_read_unlock(); 160 161 return IRQ_HANDLED; 162 } 163 164 static int tpci200_free_irq(struct ipack_device *dev) 165 { 166 struct slot_irq *slot_irq; 167 struct tpci200_board *tpci200; 168 169 tpci200 = check_slot(dev); 170 if (tpci200 == NULL) 171 return -EINVAL; 172 173 if (mutex_lock_interruptible(&tpci200->mutex)) 174 return -ERESTARTSYS; 175 176 if (tpci200->slots[dev->slot].irq == NULL) { 177 mutex_unlock(&tpci200->mutex); 178 return -EINVAL; 179 } 180 181 tpci200_disable_irq(tpci200, dev->slot); 182 slot_irq = tpci200->slots[dev->slot].irq; 183 /* uninstall handler */ 184 RCU_INIT_POINTER(tpci200->slots[dev->slot].irq, NULL); 185 synchronize_rcu(); 186 kfree(slot_irq); 187 mutex_unlock(&tpci200->mutex); 188 return 0; 189 } 190 191 static int tpci200_request_irq(struct ipack_device *dev, 192 irqreturn_t (*handler)(void *), void *arg) 193 { 194 int res = 0; 195 struct slot_irq *slot_irq; 196 struct tpci200_board *tpci200; 197 198 tpci200 = check_slot(dev); 199 if (tpci200 == NULL) 200 return -EINVAL; 201 202 if (mutex_lock_interruptible(&tpci200->mutex)) 203 return -ERESTARTSYS; 204 205 if (tpci200->slots[dev->slot].irq != NULL) { 206 dev_err(&dev->dev, 207 "Slot [%d:%d] IRQ already registered !\n", 208 dev->bus->bus_nr, 209 dev->slot); 210 res = -EINVAL; 211 goto out_unlock; 212 } 213 214 slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL); 215 if (slot_irq == NULL) { 216 dev_err(&dev->dev, 217 "Slot [%d:%d] unable to allocate memory for IRQ !\n", 218 dev->bus->bus_nr, dev->slot); 219 res = -ENOMEM; 220 goto out_unlock; 221 } 222 223 /* 224 * WARNING: Setup Interrupt Vector in the IndustryPack device 225 * before an IRQ request. 226 * Read the User Manual of your IndustryPack device to know 227 * where to write the vector in memory. 228 */ 229 slot_irq->handler = handler; 230 slot_irq->arg = arg; 231 slot_irq->holder = dev; 232 233 rcu_assign_pointer(tpci200->slots[dev->slot].irq, slot_irq); 234 tpci200_enable_irq(tpci200, dev->slot); 235 236 out_unlock: 237 mutex_unlock(&tpci200->mutex); 238 return res; 239 } 240 241 static int tpci200_register(struct tpci200_board *tpci200) 242 { 243 int i; 244 int res; 245 phys_addr_t ioidint_base; 246 unsigned short slot_ctrl; 247 248 if (pci_enable_device(tpci200->info->pdev) < 0) 249 return -ENODEV; 250 251 /* Request IP interface register (Bar 2) */ 252 res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR, 253 "Carrier IP interface registers"); 254 if (res) { 255 dev_err(&tpci200->info->pdev->dev, 256 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !", 257 tpci200->info->pdev->bus->number, 258 tpci200->info->pdev->devfn); 259 goto out_disable_pci; 260 } 261 262 /* Request IO ID INT space (Bar 3) */ 263 res = pci_request_region(tpci200->info->pdev, 264 TPCI200_IO_ID_INT_SPACES_BAR, 265 "Carrier IO ID INT space"); 266 if (res) { 267 dev_err(&tpci200->info->pdev->dev, 268 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !", 269 tpci200->info->pdev->bus->number, 270 tpci200->info->pdev->devfn); 271 goto out_release_ip_space; 272 } 273 274 /* Request MEM8 space (Bar 5) */ 275 res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR, 276 "Carrier MEM8 space"); 277 if (res) { 278 dev_err(&tpci200->info->pdev->dev, 279 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 5!", 280 tpci200->info->pdev->bus->number, 281 tpci200->info->pdev->devfn); 282 goto out_release_ioid_int_space; 283 } 284 285 /* Request MEM16 space (Bar 4) */ 286 res = pci_request_region(tpci200->info->pdev, TPCI200_MEM16_SPACE_BAR, 287 "Carrier MEM16 space"); 288 if (res) { 289 dev_err(&tpci200->info->pdev->dev, 290 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!", 291 tpci200->info->pdev->bus->number, 292 tpci200->info->pdev->devfn); 293 goto out_release_mem8_space; 294 } 295 296 /* Map internal tpci200 driver user space */ 297 tpci200->info->interface_regs = 298 ioremap(pci_resource_start(tpci200->info->pdev, 299 TPCI200_IP_INTERFACE_BAR), 300 TPCI200_IFACE_SIZE); 301 if (!tpci200->info->interface_regs) { 302 dev_err(&tpci200->info->pdev->dev, 303 "(bn 0x%X, sn 0x%X) failed to map driver user space!", 304 tpci200->info->pdev->bus->number, 305 tpci200->info->pdev->devfn); 306 res = -ENOMEM; 307 goto out_release_mem8_space; 308 } 309 310 /* Initialize lock that protects interface_regs */ 311 spin_lock_init(&tpci200->regs_lock); 312 313 ioidint_base = pci_resource_start(tpci200->info->pdev, 314 TPCI200_IO_ID_INT_SPACES_BAR); 315 tpci200->mod_mem[IPACK_IO_SPACE] = ioidint_base + TPCI200_IO_SPACE_OFF; 316 tpci200->mod_mem[IPACK_ID_SPACE] = ioidint_base + TPCI200_ID_SPACE_OFF; 317 tpci200->mod_mem[IPACK_INT_SPACE] = 318 ioidint_base + TPCI200_INT_SPACE_OFF; 319 tpci200->mod_mem[IPACK_MEM8_SPACE] = 320 pci_resource_start(tpci200->info->pdev, 321 TPCI200_MEM8_SPACE_BAR); 322 tpci200->mod_mem[IPACK_MEM16_SPACE] = 323 pci_resource_start(tpci200->info->pdev, 324 TPCI200_MEM16_SPACE_BAR); 325 326 /* Set the default parameters of the slot 327 * INT0 disabled, level sensitive 328 * INT1 disabled, level sensitive 329 * error interrupt disabled 330 * timeout interrupt disabled 331 * recover time disabled 332 * clock rate 8 MHz 333 */ 334 slot_ctrl = 0; 335 for (i = 0; i < TPCI200_NB_SLOT; i++) 336 writew(slot_ctrl, &tpci200->info->interface_regs->control[i]); 337 338 res = request_irq(tpci200->info->pdev->irq, 339 tpci200_interrupt, IRQF_SHARED, 340 KBUILD_MODNAME, (void *) tpci200); 341 if (res) { 342 dev_err(&tpci200->info->pdev->dev, 343 "(bn 0x%X, sn 0x%X) unable to register IRQ !", 344 tpci200->info->pdev->bus->number, 345 tpci200->info->pdev->devfn); 346 goto out_release_ioid_int_space; 347 } 348 349 return 0; 350 351 out_release_mem8_space: 352 pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR); 353 out_release_ioid_int_space: 354 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR); 355 out_release_ip_space: 356 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR); 357 out_disable_pci: 358 pci_disable_device(tpci200->info->pdev); 359 return res; 360 } 361 362 static int tpci200_get_clockrate(struct ipack_device *dev) 363 { 364 struct tpci200_board *tpci200 = check_slot(dev); 365 __le16 __iomem *addr; 366 367 if (!tpci200) 368 return -ENODEV; 369 370 addr = &tpci200->info->interface_regs->control[dev->slot]; 371 return (ioread16(addr) & TPCI200_CLK32) ? 32 : 8; 372 } 373 374 static int tpci200_set_clockrate(struct ipack_device *dev, int mherz) 375 { 376 struct tpci200_board *tpci200 = check_slot(dev); 377 __le16 __iomem *addr; 378 379 if (!tpci200) 380 return -ENODEV; 381 382 addr = &tpci200->info->interface_regs->control[dev->slot]; 383 384 switch (mherz) { 385 case 8: 386 tpci200_clear_mask(tpci200, addr, TPCI200_CLK32); 387 break; 388 case 32: 389 tpci200_set_mask(tpci200, addr, TPCI200_CLK32); 390 break; 391 default: 392 return -EINVAL; 393 } 394 return 0; 395 } 396 397 static int tpci200_get_error(struct ipack_device *dev) 398 { 399 struct tpci200_board *tpci200 = check_slot(dev); 400 __le16 __iomem *addr; 401 u16 mask; 402 403 if (!tpci200) 404 return -ENODEV; 405 406 addr = &tpci200->info->interface_regs->status; 407 mask = tpci200_status_error[dev->slot]; 408 return (ioread16(addr) & mask) ? 1 : 0; 409 } 410 411 static int tpci200_get_timeout(struct ipack_device *dev) 412 { 413 struct tpci200_board *tpci200 = check_slot(dev); 414 __le16 __iomem *addr; 415 u16 mask; 416 417 if (!tpci200) 418 return -ENODEV; 419 420 addr = &tpci200->info->interface_regs->status; 421 mask = tpci200_status_timeout[dev->slot]; 422 423 return (ioread16(addr) & mask) ? 1 : 0; 424 } 425 426 static int tpci200_reset_timeout(struct ipack_device *dev) 427 { 428 struct tpci200_board *tpci200 = check_slot(dev); 429 __le16 __iomem *addr; 430 u16 mask; 431 432 if (!tpci200) 433 return -ENODEV; 434 435 addr = &tpci200->info->interface_regs->status; 436 mask = tpci200_status_timeout[dev->slot]; 437 438 iowrite16(mask, addr); 439 return 0; 440 } 441 442 static void tpci200_uninstall(struct tpci200_board *tpci200) 443 { 444 tpci200_unregister(tpci200); 445 kfree(tpci200->slots); 446 } 447 448 static const struct ipack_bus_ops tpci200_bus_ops = { 449 .request_irq = tpci200_request_irq, 450 .free_irq = tpci200_free_irq, 451 .get_clockrate = tpci200_get_clockrate, 452 .set_clockrate = tpci200_set_clockrate, 453 .get_error = tpci200_get_error, 454 .get_timeout = tpci200_get_timeout, 455 .reset_timeout = tpci200_reset_timeout, 456 }; 457 458 static int tpci200_install(struct tpci200_board *tpci200) 459 { 460 int res; 461 462 tpci200->slots = kcalloc(TPCI200_NB_SLOT, sizeof(struct tpci200_slot), 463 GFP_KERNEL); 464 if (tpci200->slots == NULL) 465 return -ENOMEM; 466 467 res = tpci200_register(tpci200); 468 if (res) { 469 kfree(tpci200->slots); 470 tpci200->slots = NULL; 471 return res; 472 } 473 474 mutex_init(&tpci200->mutex); 475 return 0; 476 } 477 478 static void tpci200_release_device(struct ipack_device *dev) 479 { 480 kfree(dev); 481 } 482 483 static int tpci200_create_device(struct tpci200_board *tpci200, int i) 484 { 485 int ret; 486 enum ipack_space space; 487 struct ipack_device *dev = 488 kzalloc(sizeof(struct ipack_device), GFP_KERNEL); 489 if (!dev) 490 return -ENOMEM; 491 dev->slot = i; 492 dev->bus = tpci200->info->ipack_bus; 493 dev->release = tpci200_release_device; 494 495 for (space = 0; space < IPACK_SPACE_COUNT; space++) { 496 dev->region[space].start = 497 tpci200->mod_mem[space] 498 + tpci200_space_interval[space] * i; 499 dev->region[space].size = tpci200_space_size[space]; 500 } 501 502 ret = ipack_device_init(dev); 503 if (ret < 0) { 504 ipack_put_device(dev); 505 return ret; 506 } 507 508 ret = ipack_device_add(dev); 509 if (ret < 0) 510 ipack_put_device(dev); 511 512 return ret; 513 } 514 515 static int tpci200_pci_probe(struct pci_dev *pdev, 516 const struct pci_device_id *id) 517 { 518 int ret, i; 519 struct tpci200_board *tpci200; 520 u32 reg32; 521 522 tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL); 523 if (!tpci200) 524 return -ENOMEM; 525 526 tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL); 527 if (!tpci200->info) { 528 ret = -ENOMEM; 529 goto err_tpci200; 530 } 531 532 pci_dev_get(pdev); 533 534 /* Obtain a mapping of the carrier's PCI configuration registers */ 535 ret = pci_request_region(pdev, TPCI200_CFG_MEM_BAR, 536 KBUILD_MODNAME " Configuration Memory"); 537 if (ret) { 538 dev_err(&pdev->dev, "Failed to allocate PCI Configuration Memory"); 539 ret = -EBUSY; 540 goto err_tpci200_info; 541 } 542 tpci200->info->cfg_regs = ioremap( 543 pci_resource_start(pdev, TPCI200_CFG_MEM_BAR), 544 pci_resource_len(pdev, TPCI200_CFG_MEM_BAR)); 545 if (!tpci200->info->cfg_regs) { 546 dev_err(&pdev->dev, "Failed to map PCI Configuration Memory"); 547 ret = -EFAULT; 548 goto err_request_region; 549 } 550 551 /* Disable byte swapping for 16 bit IP module access. This will ensure 552 * that the Industrypack big endian byte order is preserved by the 553 * carrier. */ 554 reg32 = ioread32(tpci200->info->cfg_regs + LAS1_DESC); 555 reg32 |= 1 << LAS_BIT_BIGENDIAN; 556 iowrite32(reg32, tpci200->info->cfg_regs + LAS1_DESC); 557 558 reg32 = ioread32(tpci200->info->cfg_regs + LAS2_DESC); 559 reg32 |= 1 << LAS_BIT_BIGENDIAN; 560 iowrite32(reg32, tpci200->info->cfg_regs + LAS2_DESC); 561 562 /* Save struct pci_dev pointer */ 563 tpci200->info->pdev = pdev; 564 tpci200->info->id_table = (struct pci_device_id *)id; 565 566 /* register the device and initialize it */ 567 ret = tpci200_install(tpci200); 568 if (ret) { 569 dev_err(&pdev->dev, "error during tpci200 install\n"); 570 ret = -ENODEV; 571 goto err_cfg_regs; 572 } 573 574 /* Register the carrier in the industry pack bus driver */ 575 tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev, 576 TPCI200_NB_SLOT, 577 &tpci200_bus_ops, 578 THIS_MODULE); 579 if (!tpci200->info->ipack_bus) { 580 dev_err(&pdev->dev, 581 "error registering the carrier on ipack driver\n"); 582 ret = -EFAULT; 583 goto err_tpci200_install; 584 } 585 586 /* save the bus number given by ipack to logging purpose */ 587 tpci200->number = tpci200->info->ipack_bus->bus_nr; 588 dev_set_drvdata(&pdev->dev, tpci200); 589 590 for (i = 0; i < TPCI200_NB_SLOT; i++) 591 tpci200_create_device(tpci200, i); 592 return 0; 593 594 err_tpci200_install: 595 tpci200_uninstall(tpci200); 596 err_cfg_regs: 597 pci_iounmap(tpci200->info->pdev, tpci200->info->cfg_regs); 598 err_request_region: 599 pci_release_region(pdev, TPCI200_CFG_MEM_BAR); 600 err_tpci200_info: 601 kfree(tpci200->info); 602 pci_dev_put(pdev); 603 err_tpci200: 604 kfree(tpci200); 605 return ret; 606 } 607 608 static void __tpci200_pci_remove(struct tpci200_board *tpci200) 609 { 610 ipack_bus_unregister(tpci200->info->ipack_bus); 611 tpci200_uninstall(tpci200); 612 613 pci_iounmap(tpci200->info->pdev, tpci200->info->cfg_regs); 614 615 pci_release_region(tpci200->info->pdev, TPCI200_CFG_MEM_BAR); 616 617 pci_dev_put(tpci200->info->pdev); 618 619 kfree(tpci200->info); 620 kfree(tpci200); 621 } 622 623 static void tpci200_pci_remove(struct pci_dev *dev) 624 { 625 struct tpci200_board *tpci200 = pci_get_drvdata(dev); 626 627 __tpci200_pci_remove(tpci200); 628 } 629 630 static const struct pci_device_id tpci200_idtable[] = { 631 { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID, 632 TPCI200_SUBDEVICE_ID }, 633 { 0, }, 634 }; 635 636 MODULE_DEVICE_TABLE(pci, tpci200_idtable); 637 638 static struct pci_driver tpci200_pci_drv = { 639 .name = "tpci200", 640 .id_table = tpci200_idtable, 641 .probe = tpci200_pci_probe, 642 .remove = tpci200_pci_remove, 643 }; 644 645 module_pci_driver(tpci200_pci_drv); 646 647 MODULE_DESCRIPTION("TEWS TPCI-200 device driver"); 648 MODULE_LICENSE("GPL"); 649