4 * driver for the TEWS TPCI-200 device
5 * Copyright (c) 2009 Nicolas Serafini, EIC2 SA
6 * Copyright (c) 2010,2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
7 * Copyright (c) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; version 2 of the License.
14 #include <linux/module.h>
17 static struct ipack_bus_ops tpci200_bus_ops;
19 /* TPCI200 controls registers */
20 static int control_reg[] = {
21 TPCI200_CONTROL_A_REG,
22 TPCI200_CONTROL_B_REG,
23 TPCI200_CONTROL_C_REG,
27 static int tpci200_slot_unregister(struct ipack_device *dev);
29 static struct tpci200_board *check_slot(struct ipack_device *dev)
31 struct tpci200_board *tpci200;
37 tpci200 = dev_get_drvdata(dev->bus->parent);
39 if (tpci200 == NULL) {
40 dev_info(&dev->dev, "carrier board not found\n");
44 if (dev->slot >= TPCI200_NB_SLOT) {
46 "Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
47 dev->bus_nr, dev->slot, TPCI200_NB_SLOT-1);
54 static void tpci200_unregister(struct tpci200_board *tpci200)
58 free_irq(tpci200->info->pdev->irq, (void *) tpci200);
60 pci_iounmap(tpci200->info->pdev, tpci200->info->interface_regs);
61 pci_iounmap(tpci200->info->pdev, tpci200->info->ioidint_space);
62 pci_iounmap(tpci200->info->pdev, tpci200->info->mem8_space);
64 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
65 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
66 pci_release_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR);
68 pci_disable_device(tpci200->info->pdev);
69 pci_dev_put(tpci200->info->pdev);
71 for (i = 0; i < TPCI200_NB_SLOT; i++) {
72 tpci200->slots[i].io_phys.address = NULL;
73 tpci200->slots[i].io_phys.size = 0;
74 tpci200->slots[i].id_phys.address = NULL;
75 tpci200->slots[i].id_phys.size = 0;
76 tpci200->slots[i].mem_phys.address = NULL;
77 tpci200->slots[i].mem_phys.size = 0;
81 static irqreturn_t tpci200_interrupt(int irq, void *dev_id)
83 struct tpci200_board *tpci200 = (struct tpci200_board *) dev_id;
85 unsigned short status_reg, reg_value;
86 unsigned short unhandled_ints = 0;
87 irqreturn_t ret = IRQ_NONE;
89 /* Read status register */
90 status_reg = readw(tpci200->info->interface_regs +
93 if (status_reg & TPCI200_SLOT_INT_MASK) {
94 unhandled_ints = status_reg & TPCI200_SLOT_INT_MASK;
95 /* callback to the IRQ handler for the corresponding slot */
96 for (i = 0; i < TPCI200_NB_SLOT; i++) {
97 if ((tpci200->slots[i].irq != NULL) &&
98 (status_reg & ((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)))) {
100 ret = tpci200->slots[i].irq->handler(tpci200->slots[i].irq->arg);
103 readw(tpci200->slots[i].dev->io_space.address +
105 readw(tpci200->slots[i].dev->io_space.address +
108 unhandled_ints &= ~(((TPCI200_A_INT0 | TPCI200_A_INT1) << (2*i)));
112 /* Interrupt not handled are disabled */
113 if (unhandled_ints) {
114 for (i = 0; i < TPCI200_NB_SLOT; i++) {
115 if (unhandled_ints & ((TPCI200_INT0_EN | TPCI200_INT1_EN) << (2*i))) {
116 dev_info(&tpci200->slots[i].dev->dev,
117 "No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
120 tpci200->info->interface_regs +
123 ~(TPCI200_INT0_EN | TPCI200_INT1_EN);
125 (tpci200->info->interface_regs +
134 static int tpci200_register(struct tpci200_board *tpci200)
138 unsigned long ioidint_base;
139 unsigned long mem_base;
140 unsigned short slot_ctrl;
142 if (pci_enable_device(tpci200->info->pdev) < 0)
145 /* Request IP interface register (Bar 2) */
146 res = pci_request_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR,
147 "Carrier IP interface registers");
149 dev_err(&tpci200->info->pdev->dev,
150 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
151 tpci200->info->pdev->bus->number,
152 tpci200->info->pdev->devfn);
153 goto out_disable_pci;
156 /* Request IO ID INT space (Bar 3) */
157 res = pci_request_region(tpci200->info->pdev,
158 TPCI200_IO_ID_INT_SPACES_BAR,
159 "Carrier IO ID INT space");
161 dev_err(&tpci200->info->pdev->dev,
162 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
163 tpci200->info->pdev->bus->number,
164 tpci200->info->pdev->devfn);
165 goto out_release_ip_space;
168 /* Request MEM space (Bar 4) */
169 res = pci_request_region(tpci200->info->pdev, TPCI200_MEM8_SPACE_BAR,
170 "Carrier MEM space");
172 dev_err(&tpci200->info->pdev->dev,
173 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
174 tpci200->info->pdev->bus->number,
175 tpci200->info->pdev->devfn);
176 goto out_release_ioid_int_space;
179 /* Map internal tpci200 driver user space */
180 tpci200->info->interface_regs =
181 ioremap_nocache(pci_resource_start(tpci200->info->pdev,
182 TPCI200_IP_INTERFACE_BAR),
184 tpci200->info->ioidint_space =
185 ioremap_nocache(pci_resource_start(tpci200->info->pdev,
186 TPCI200_IO_ID_INT_SPACES_BAR),
187 TPCI200_IOIDINT_SIZE);
188 tpci200->info->mem8_space =
189 ioremap_nocache(pci_resource_start(tpci200->info->pdev,
190 TPCI200_MEM8_SPACE_BAR),
193 ioidint_base = pci_resource_start(tpci200->info->pdev,
194 TPCI200_IO_ID_INT_SPACES_BAR);
195 mem_base = pci_resource_start(tpci200->info->pdev,
196 TPCI200_MEM8_SPACE_BAR);
198 /* Set the default parameters of the slot
199 * INT0 disabled, level sensitive
200 * INT1 disabled, level sensitive
201 * error interrupt disabled
202 * timeout interrupt disabled
203 * recover time disabled
208 /* Set all slot physical address space */
209 for (i = 0; i < TPCI200_NB_SLOT; i++) {
210 tpci200->slots[i].io_phys.address =
211 (void __iomem *)ioidint_base +
212 TPCI200_IO_SPACE_OFF + TPCI200_IO_SPACE_GAP*i;
213 tpci200->slots[i].io_phys.size = TPCI200_IO_SPACE_SIZE;
215 tpci200->slots[i].id_phys.address =
216 (void __iomem *)ioidint_base +
217 TPCI200_ID_SPACE_OFF + TPCI200_ID_SPACE_GAP*i;
218 tpci200->slots[i].id_phys.size = TPCI200_ID_SPACE_SIZE;
220 tpci200->slots[i].mem_phys.address =
221 (void __iomem *)mem_base + TPCI200_MEM8_GAP*i;
222 tpci200->slots[i].mem_phys.size = TPCI200_MEM8_SIZE;
224 writew(slot_ctrl, (tpci200->info->interface_regs +
228 res = request_irq(tpci200->info->pdev->irq,
229 tpci200_interrupt, IRQF_SHARED,
230 KBUILD_MODNAME, (void *) tpci200);
232 dev_err(&tpci200->info->pdev->dev,
233 "(bn 0x%X, sn 0x%X) unable to register IRQ !",
234 tpci200->info->pdev->bus->number,
235 tpci200->info->pdev->devfn);
236 goto out_release_ioid_int_space;
241 out_release_ioid_int_space:
242 pci_release_region(tpci200->info->pdev, TPCI200_IO_ID_INT_SPACES_BAR);
243 out_release_ip_space:
244 pci_release_region(tpci200->info->pdev, TPCI200_IP_INTERFACE_BAR);
246 pci_disable_device(tpci200->info->pdev);
250 static int __tpci200_request_irq(struct tpci200_board *tpci200,
251 struct ipack_device *dev)
253 unsigned short slot_ctrl;
255 /* Set the default parameters of the slot
256 * INT0 enabled, level sensitive
257 * INT1 enabled, level sensitive
258 * error interrupt disabled
259 * timeout interrupt disabled
260 * recover time disabled
263 slot_ctrl = TPCI200_INT0_EN | TPCI200_INT1_EN;
264 writew(slot_ctrl, (tpci200->info->interface_regs +
265 control_reg[dev->slot]));
270 static void __tpci200_free_irq(struct tpci200_board *tpci200,
271 struct ipack_device *dev)
273 unsigned short slot_ctrl;
275 /* Set the default parameters of the slot
276 * INT0 disabled, level sensitive
277 * INT1 disabled, level sensitive
278 * error interrupt disabled
279 * timeout interrupt disabled
280 * recover time disabled
284 writew(slot_ctrl, (tpci200->info->interface_regs +
285 control_reg[dev->slot]));
288 static int tpci200_free_irq(struct ipack_device *dev)
290 struct slot_irq *slot_irq;
291 struct tpci200_board *tpci200;
293 tpci200 = check_slot(dev);
297 if (mutex_lock_interruptible(&tpci200->mutex))
300 if (tpci200->slots[dev->slot].irq == NULL) {
301 mutex_unlock(&tpci200->mutex);
305 __tpci200_free_irq(tpci200, dev);
306 slot_irq = tpci200->slots[dev->slot].irq;
307 tpci200->slots[dev->slot].irq = NULL;
310 mutex_unlock(&tpci200->mutex);
314 static int tpci200_slot_unmap_space(struct ipack_device *dev, int space)
316 struct ipack_addr_space *virt_addr_space;
317 struct tpci200_board *tpci200;
319 tpci200 = check_slot(dev);
323 if (mutex_lock_interruptible(&tpci200->mutex))
328 if (dev->io_space.address == NULL) {
330 "Slot [%d:%d] IO space not mapped !\n",
331 dev->bus_nr, dev->slot);
334 virt_addr_space = &dev->io_space;
337 if (dev->id_space.address == NULL) {
339 "Slot [%d:%d] ID space not mapped !\n",
340 dev->bus_nr, dev->slot);
343 virt_addr_space = &dev->id_space;
345 case IPACK_MEM_SPACE:
346 if (dev->mem_space.address == NULL) {
348 "Slot [%d:%d] MEM space not mapped !\n",
349 dev->bus_nr, dev->slot);
352 virt_addr_space = &dev->mem_space;
356 "Slot [%d:%d] space number %d doesn't exist !\n",
357 dev->bus_nr, dev->slot, space);
358 mutex_unlock(&tpci200->mutex);
362 iounmap(virt_addr_space->address);
364 virt_addr_space->address = NULL;
365 virt_addr_space->size = 0;
367 mutex_unlock(&tpci200->mutex);
371 static int tpci200_slot_unregister(struct ipack_device *dev)
373 struct tpci200_board *tpci200;
378 tpci200 = check_slot(dev);
382 tpci200_free_irq(dev);
384 if (mutex_lock_interruptible(&tpci200->mutex))
387 tpci200->slots[dev->slot].dev = NULL;
388 ipack_device_unregister(dev);
389 mutex_unlock(&tpci200->mutex);
394 static int tpci200_slot_map_space(struct ipack_device *dev,
395 unsigned int memory_size, int space)
398 unsigned int size_to_map;
399 void __iomem *phys_address;
400 struct ipack_addr_space *virt_addr_space;
401 struct tpci200_board *tpci200;
403 tpci200 = check_slot(dev);
407 if (mutex_lock_interruptible(&tpci200->mutex))
412 if (dev->io_space.address != NULL) {
414 "Slot [%d:%d] IO space already mapped !\n",
415 tpci200->number, dev->slot);
419 virt_addr_space = &dev->io_space;
421 phys_address = tpci200->slots[dev->slot].io_phys.address;
422 size_to_map = tpci200->slots[dev->slot].io_phys.size;
425 if (dev->id_space.address != NULL) {
427 "Slot [%d:%d] ID space already mapped !\n",
428 tpci200->number, dev->slot);
432 virt_addr_space = &dev->id_space;
434 phys_address = tpci200->slots[dev->slot].id_phys.address;
435 size_to_map = tpci200->slots[dev->slot].id_phys.size;
437 case IPACK_MEM_SPACE:
438 if (dev->mem_space.address != NULL) {
440 "Slot [%d:%d] MEM space already mapped !\n",
441 tpci200->number, dev->slot);
445 virt_addr_space = &dev->mem_space;
447 if (memory_size > tpci200->slots[dev->slot].mem_phys.size) {
449 "Slot [%d:%d] request is 0x%X memory, only 0x%X available !\n",
450 dev->bus_nr, dev->slot, memory_size,
451 tpci200->slots[dev->slot].mem_phys.size);
456 phys_address = tpci200->slots[dev->slot].mem_phys.address;
457 size_to_map = memory_size;
460 dev_err(&dev->dev, "Slot [%d:%d] space %d doesn't exist !\n",
461 tpci200->number, dev->slot, space);
466 virt_addr_space->size = size_to_map;
467 virt_addr_space->address =
468 ioremap_nocache((unsigned long)phys_address, size_to_map);
471 mutex_unlock(&tpci200->mutex);
475 static int tpci200_request_irq(struct ipack_device *dev, int vector,
476 int (*handler)(void *), void *arg)
479 struct slot_irq *slot_irq;
480 struct tpci200_board *tpci200;
482 tpci200 = check_slot(dev);
486 if (mutex_lock_interruptible(&tpci200->mutex))
489 if (tpci200->slots[dev->slot].irq != NULL) {
491 "Slot [%d:%d] IRQ already registered !\n", dev->bus_nr,
497 slot_irq = kzalloc(sizeof(struct slot_irq), GFP_KERNEL);
498 if (slot_irq == NULL) {
500 "Slot [%d:%d] unable to allocate memory for IRQ !\n",
501 dev->bus_nr, dev->slot);
507 * WARNING: Setup Interrupt Vector in the IndustryPack device
508 * before an IRQ request.
509 * Read the User Manual of your IndustryPack device to know
510 * where to write the vector in memory.
512 slot_irq->vector = vector;
513 slot_irq->handler = handler;
516 tpci200->slots[dev->slot].irq = slot_irq;
517 res = __tpci200_request_irq(tpci200, dev);
520 mutex_unlock(&tpci200->mutex);
524 static void tpci200_uninstall(struct tpci200_board *tpci200)
528 for (i = 0; i < TPCI200_NB_SLOT; i++)
529 tpci200_slot_unregister(tpci200->slots[i].dev);
531 tpci200_unregister(tpci200);
532 kfree(tpci200->slots);
535 static struct ipack_bus_ops tpci200_bus_ops = {
536 .map_space = tpci200_slot_map_space,
537 .unmap_space = tpci200_slot_unmap_space,
538 .request_irq = tpci200_request_irq,
539 .free_irq = tpci200_free_irq,
540 .remove_device = tpci200_slot_unregister,
543 static int tpci200_install(struct tpci200_board *tpci200)
547 tpci200->slots = kzalloc(
548 TPCI200_NB_SLOT * sizeof(struct tpci200_slot), GFP_KERNEL);
549 if (tpci200->slots == NULL)
552 res = tpci200_register(tpci200);
554 kfree(tpci200->slots);
555 tpci200->slots = NULL;
559 mutex_init(&tpci200->mutex);
563 static int tpci200_pci_probe(struct pci_dev *pdev,
564 const struct pci_device_id *id)
567 struct tpci200_board *tpci200;
570 tpci200 = kzalloc(sizeof(struct tpci200_board), GFP_KERNEL);
574 tpci200->info = kzalloc(sizeof(struct tpci200_infos), GFP_KERNEL);
575 if (!tpci200->info) {
580 /* Obtain a mapping of the carrier's PCI configuration registers */
581 ret = pci_request_region(pdev, TPCI200_CFG_MEM_BAR,
582 KBUILD_MODNAME " Configuration Memory");
584 dev_err(&pdev->dev, "Failed to allocate PCI Configuration Memory");
586 goto out_err_pci_request;
588 tpci200->info->cfg_regs = ioremap_nocache(
589 pci_resource_start(pdev, TPCI200_CFG_MEM_BAR),
590 pci_resource_len(pdev, TPCI200_CFG_MEM_BAR));
591 if (!tpci200->info->cfg_regs) {
592 dev_err(&pdev->dev, "Failed to map PCI Configuration Memory");
594 goto out_err_ioremap;
597 /* Disable byte swapping for 16 bit IP module access. This will ensure
598 * that the Industrypack big endian byte order is preserved by the
600 reg32 = ioread32(tpci200->info->cfg_regs + LAS1_DESC);
601 reg32 |= 1 << LAS_BIT_BIGENDIAN;
602 iowrite32(reg32, tpci200->info->cfg_regs + LAS1_DESC);
604 reg32 = ioread32(tpci200->info->cfg_regs + LAS2_DESC);
605 reg32 |= 1 << LAS_BIT_BIGENDIAN;
606 iowrite32(reg32, tpci200->info->cfg_regs + LAS2_DESC);
608 /* Save struct pci_dev pointer */
609 tpci200->info->pdev = pdev;
610 tpci200->info->id_table = (struct pci_device_id *)id;
612 /* register the device and initialize it */
613 ret = tpci200_install(tpci200);
615 dev_err(&pdev->dev, "error during tpci200 install\n");
617 goto out_err_install;
620 /* Register the carrier in the industry pack bus driver */
621 tpci200->info->ipack_bus = ipack_bus_register(&pdev->dev,
624 if (!tpci200->info->ipack_bus) {
626 "error registering the carrier on ipack driver\n");
628 goto out_err_bus_register;
631 /* save the bus number given by ipack to logging purpose */
632 tpci200->number = tpci200->info->ipack_bus->bus_nr;
633 dev_set_drvdata(&pdev->dev, tpci200);
636 * Give the same IRQ number as the slot number.
637 * The TPCI200 has assigned his own two IRQ by PCI bus driver
639 for (i = 0; i < TPCI200_NB_SLOT; i++)
640 tpci200->slots[i].dev =
641 ipack_device_register(tpci200->info->ipack_bus, i, i);
644 out_err_bus_register:
645 tpci200_uninstall(tpci200);
647 iounmap(tpci200->info->cfg_regs);
649 pci_release_region(pdev, TPCI200_CFG_MEM_BAR);
651 kfree(tpci200->info);
657 static void __tpci200_pci_remove(struct tpci200_board *tpci200)
659 tpci200_uninstall(tpci200);
660 ipack_bus_unregister(tpci200->info->ipack_bus);
662 iounmap(tpci200->info->cfg_regs);
663 pci_release_region(tpci200->info->pdev, TPCI200_CFG_MEM_BAR);
665 kfree(tpci200->info);
669 static void __devexit tpci200_pci_remove(struct pci_dev *dev)
671 struct tpci200_board *tpci200 = pci_get_drvdata(dev);
673 __tpci200_pci_remove(tpci200);
676 static DEFINE_PCI_DEVICE_TABLE(tpci200_idtable) = {
677 { TPCI200_VENDOR_ID, TPCI200_DEVICE_ID, TPCI200_SUBVENDOR_ID,
678 TPCI200_SUBDEVICE_ID },
682 MODULE_DEVICE_TABLE(pci, tpci200_idtable);
684 static struct pci_driver tpci200_pci_drv = {
686 .id_table = tpci200_idtable,
687 .probe = tpci200_pci_probe,
688 .remove = __devexit_p(tpci200_pci_remove),
691 static int __init tpci200_drvr_init_module(void)
693 return pci_register_driver(&tpci200_pci_drv);
696 static void __exit tpci200_drvr_exit_module(void)
698 pci_unregister_driver(&tpci200_pci_drv);
701 MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
702 MODULE_LICENSE("GPL");
703 module_init(tpci200_drvr_init_module);
704 module_exit(tpci200_drvr_exit_module);