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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
19 static struct ipack_bus_ops tpci200_bus_ops
;
21 /* TPCI200 controls registers */
22 static int control_reg
[] = {
23 TPCI200_CONTROL_A_REG
,
24 TPCI200_CONTROL_B_REG
,
25 TPCI200_CONTROL_C_REG
,
29 /* Linked list to save the registered devices */
30 static LIST_HEAD(tpci200_list
);
32 static int tpci200_slot_unregister(struct ipack_device
*dev
);
34 static struct tpci200_board
*check_slot(struct ipack_device
*dev
)
36 struct tpci200_board
*tpci200
;
40 pr_info("Slot doesn't exist.\n");
44 list_for_each_entry(tpci200
, &tpci200_list
, list
) {
45 if (tpci200
->number
== dev
->bus_nr
) {
52 pr_err("Carrier not found\n");
56 if (dev
->slot
>= TPCI200_NB_SLOT
) {
57 pr_info("Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
58 dev
->bus_nr
, dev
->slot
, TPCI200_NB_SLOT
-1);
62 BUG_ON(tpci200
->slots
== NULL
);
63 if (tpci200
->slots
[dev
->slot
].dev
== NULL
) {
64 pr_info("Slot [%d:%d] is not registered !\n", dev
->bus_nr
,
72 static inline unsigned char __tpci200_read8(void __iomem
*address
,
75 return ioread8(address
+ (offset
^1));
78 static inline unsigned short __tpci200_read16(void __iomem
*address
,
81 return ioread16(address
+ offset
);
84 static inline unsigned int __tpci200_read32(void __iomem
*address
,
87 return swahw32(ioread32(address
+ offset
));
90 static inline void __tpci200_write8(unsigned char value
,
91 void __iomem
*address
, unsigned long offset
)
93 iowrite8(value
, address
+(offset
^1));
96 static inline void __tpci200_write16(unsigned short value
,
97 void __iomem
*address
,
100 iowrite16(value
, address
+offset
);
103 static inline void __tpci200_write32(unsigned int value
,
104 void __iomem
*address
,
105 unsigned long offset
)
107 iowrite32(swahw32(value
), address
+offset
);
110 static struct ipack_addr_space
*get_slot_address_space(struct ipack_device
*dev
,
113 struct ipack_addr_space
*addr
;
117 addr
= &dev
->io_space
;
120 addr
= &dev
->id_space
;
122 case IPACK_MEM_SPACE
:
123 addr
= &dev
->mem_space
;
126 pr_err("Slot [%d:%d] space number %d doesn't exist !\n",
127 dev
->bus_nr
, dev
->slot
, space
);
132 if ((addr
->size
== 0) || (addr
->address
== NULL
)) {
133 pr_err("Error, slot space not mapped !\n");
140 static int tpci200_read8(struct ipack_device
*dev
, int space
,
141 unsigned long offset
, unsigned char *value
)
143 struct ipack_addr_space
*addr
;
144 struct tpci200_board
*tpci200
;
146 tpci200
= check_slot(dev
);
150 addr
= get_slot_address_space(dev
, space
);
154 if (offset
>= addr
->size
) {
155 pr_err("Error, slot space offset error !\n");
159 *value
= __tpci200_read8(addr
->address
, offset
);
164 static int tpci200_read16(struct ipack_device
*dev
, int space
,
165 unsigned long offset
, unsigned short *value
)
167 struct ipack_addr_space
*addr
;
168 struct tpci200_board
*tpci200
;
170 tpci200
= check_slot(dev
);
174 addr
= get_slot_address_space(dev
, space
);
178 if ((offset
+2) >= addr
->size
) {
179 pr_err("Error, slot space offset error !\n");
182 *value
= __tpci200_read16(addr
->address
, offset
);
187 static int tpci200_read32(struct ipack_device
*dev
, int space
,
188 unsigned long offset
, unsigned int *value
)
190 struct ipack_addr_space
*addr
;
191 struct tpci200_board
*tpci200
;
193 tpci200
= check_slot(dev
);
197 addr
= get_slot_address_space(dev
, space
);
201 if ((offset
+4) >= addr
->size
) {
202 pr_err("Error, slot space offset error !\n");
206 *value
= __tpci200_read32(addr
->address
, offset
);
211 static int tpci200_write8(struct ipack_device
*dev
, int space
,
212 unsigned long offset
, unsigned char value
)
214 struct ipack_addr_space
*addr
;
215 struct tpci200_board
*tpci200
;
217 tpci200
= check_slot(dev
);
221 addr
= get_slot_address_space(dev
, space
);
225 if (offset
>= addr
->size
) {
226 pr_err("Error, slot space offset error !\n");
230 __tpci200_write8(value
, addr
->address
, offset
);
235 static int tpci200_write16(struct ipack_device
*dev
, int space
,
236 unsigned long offset
, unsigned short value
)
238 struct ipack_addr_space
*addr
;
239 struct tpci200_board
*tpci200
;
241 tpci200
= check_slot(dev
);
245 addr
= get_slot_address_space(dev
, space
);
249 if ((offset
+2) >= addr
->size
) {
250 pr_err("Error, slot space offset error !\n");
254 __tpci200_write16(value
, addr
->address
, offset
);
259 static int tpci200_write32(struct ipack_device
*dev
, int space
,
260 unsigned long offset
, unsigned int value
)
262 struct ipack_addr_space
*addr
;
263 struct tpci200_board
*tpci200
;
265 tpci200
= check_slot(dev
);
269 addr
= get_slot_address_space(dev
, space
);
273 if ((offset
+4) >= addr
->size
) {
274 pr_err("Error, slot space offset error !\n");
278 __tpci200_write32(value
, addr
->address
, offset
);
283 static void tpci200_unregister(struct tpci200_board
*tpci200
)
287 free_irq(tpci200
->info
->pdev
->irq
, (void *) tpci200
);
289 pci_iounmap(tpci200
->info
->pdev
, tpci200
->info
->interface_regs
);
290 pci_iounmap(tpci200
->info
->pdev
, tpci200
->info
->ioidint_space
);
291 pci_iounmap(tpci200
->info
->pdev
, tpci200
->info
->mem8_space
);
293 pci_release_region(tpci200
->info
->pdev
, TPCI200_IP_INTERFACE_BAR
);
294 pci_release_region(tpci200
->info
->pdev
, TPCI200_IO_ID_INT_SPACES_BAR
);
295 pci_release_region(tpci200
->info
->pdev
, TPCI200_MEM8_SPACE_BAR
);
297 pci_disable_device(tpci200
->info
->pdev
);
298 pci_dev_put(tpci200
->info
->pdev
);
300 kfree(tpci200
->info
);
302 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++) {
303 tpci200
->slots
[i
].io_phys
.address
= NULL
;
304 tpci200
->slots
[i
].io_phys
.size
= 0;
305 tpci200
->slots
[i
].id_phys
.address
= NULL
;
306 tpci200
->slots
[i
].id_phys
.size
= 0;
307 tpci200
->slots
[i
].mem_phys
.address
= NULL
;
308 tpci200
->slots
[i
].mem_phys
.size
= 0;
312 static irqreturn_t
tpci200_interrupt(int irq
, void *dev_id
)
314 struct tpci200_board
*tpci200
= (struct tpci200_board
*) dev_id
;
317 unsigned short status_reg
, reg_value
;
318 unsigned short unhandled_ints
= 0;
319 irqreturn_t ret
= IRQ_NONE
;
321 spin_lock_irqsave(&tpci200
->info
->access_lock
, flags
);
323 /* Read status register */
324 status_reg
= readw(tpci200
->info
->interface_regs
+
327 if (status_reg
& TPCI200_SLOT_INT_MASK
) {
328 unhandled_ints
= status_reg
& TPCI200_SLOT_INT_MASK
;
329 /* callback to the IRQ handler for the corresponding slot */
330 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++) {
331 if ((tpci200
->slots
[i
].irq
!= NULL
) &&
332 (status_reg
& ((TPCI200_A_INT0
| TPCI200_A_INT1
) << (2*i
)))) {
334 ret
= tpci200
->slots
[i
].irq
->handler(tpci200
->slots
[i
].irq
->arg
);
337 readw(tpci200
->slots
[i
].dev
->io_space
.address
+
339 readw(tpci200
->slots
[i
].dev
->io_space
.address
+
342 unhandled_ints
&= ~(((TPCI200_A_INT0
| TPCI200_A_INT1
) << (2*i
)));
346 /* Interrupt not handled are disabled */
347 if (unhandled_ints
) {
348 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++) {
349 if (unhandled_ints
& ((TPCI200_INT0_EN
| TPCI200_INT1_EN
) << (2*i
))) {
350 pr_info("No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
353 tpci200
->info
->interface_regs
+
356 ~(TPCI200_INT0_EN
| TPCI200_INT1_EN
);
358 (tpci200
->info
->interface_regs
+
364 spin_unlock_irqrestore(&tpci200
->info
->access_lock
, flags
);
370 static struct ipack_device
*tpci200_slot_register(unsigned int tpci200_number
,
371 unsigned int slot_position
)
374 struct ipack_device
*dev
;
375 struct tpci200_board
*tpci200
;
377 list_for_each_entry(tpci200
, &tpci200_list
, list
) {
378 if (tpci200
->number
== tpci200_number
) {
385 pr_err("carrier board not found for the device\n");
389 if (slot_position
>= TPCI200_NB_SLOT
) {
390 pr_info("Slot [%d:%d] doesn't exist!\n", tpci200_number
,
395 if (mutex_lock_interruptible(&tpci200
->mutex
))
398 if (tpci200
->slots
[slot_position
].dev
!= NULL
) {
399 pr_err("Slot [%d:%d] already installed !\n", tpci200_number
,
405 * Give the same IRQ number as the slot number.
406 * The TPCI200 has assigned his own two IRQ by PCI bus driver
408 dev
= ipack_device_register(tpci200
->info
->ipack_bus
,
409 slot_position
, slot_position
);
411 pr_info("Slot [%d:%d] Unable to register an ipack device\n",
412 tpci200_number
, slot_position
);
416 tpci200
->slots
[slot_position
].dev
= dev
;
417 mutex_unlock(&tpci200
->mutex
);
421 mutex_unlock(&tpci200
->mutex
);
425 static ssize_t
tpci200_store_board(struct device
*pdev
, const char *buf
,
426 size_t count
, int slot
)
428 struct tpci200_board
*card
= dev_get_drvdata(pdev
);
429 struct ipack_device
*dev
= card
->slots
[slot
].dev
;
434 dev
= tpci200_slot_register(card
->number
, slot
);
441 static ssize_t
tpci200_show_board(struct device
*pdev
, char *buf
, int slot
)
443 struct tpci200_board
*card
= dev_get_drvdata(pdev
);
444 struct ipack_device
*dev
= card
->slots
[slot
].dev
;
447 return snprintf(buf
, PAGE_SIZE
, "%s\n", dev_name(&dev
->dev
));
449 return snprintf(buf
, PAGE_SIZE
, "none\n");
452 static ssize_t
tpci200_show_description(struct device
*pdev
,
453 struct device_attribute
*attr
,
456 return snprintf(buf
, PAGE_SIZE
,
457 "TEWS tpci200 carrier PCI for Industry-pack mezzanines.\n");
460 static ssize_t
tpci200_show_board_slot0(struct device
*pdev
,
461 struct device_attribute
*attr
,
464 return tpci200_show_board(pdev
, buf
, 0);
467 static ssize_t
tpci200_store_board_slot0(struct device
*pdev
,
468 struct device_attribute
*attr
,
469 const char *buf
, size_t count
)
471 return tpci200_store_board(pdev
, buf
, count
, 0);
474 static ssize_t
tpci200_show_board_slot1(struct device
*pdev
,
475 struct device_attribute
*attr
,
478 return tpci200_show_board(pdev
, buf
, 1);
481 static ssize_t
tpci200_store_board_slot1(struct device
*pdev
,
482 struct device_attribute
*attr
,
483 const char *buf
, size_t count
)
485 return tpci200_store_board(pdev
, buf
, count
, 1);
488 static ssize_t
tpci200_show_board_slot2(struct device
*pdev
,
489 struct device_attribute
*attr
,
492 return tpci200_show_board(pdev
, buf
, 2);
495 static ssize_t
tpci200_store_board_slot2(struct device
*pdev
,
496 struct device_attribute
*attr
,
497 const char *buf
, size_t count
)
499 return tpci200_store_board(pdev
, buf
, count
, 2);
503 static ssize_t
tpci200_show_board_slot3(struct device
*pdev
,
504 struct device_attribute
*attr
,
507 return tpci200_show_board(pdev
, buf
, 3);
510 static ssize_t
tpci200_store_board_slot3(struct device
*pdev
,
511 struct device_attribute
*attr
,
512 const char *buf
, size_t count
)
514 return tpci200_store_board(pdev
, buf
, count
, 3);
517 /* Declaration of the device attributes for the TPCI200 */
518 static DEVICE_ATTR(description
, S_IRUGO
,
519 tpci200_show_description
, NULL
);
520 static DEVICE_ATTR(board_slot0
, S_IRUGO
| S_IWUSR
,
521 tpci200_show_board_slot0
, tpci200_store_board_slot0
);
522 static DEVICE_ATTR(board_slot1
, S_IRUGO
| S_IWUSR
,
523 tpci200_show_board_slot1
, tpci200_store_board_slot1
);
524 static DEVICE_ATTR(board_slot2
, S_IRUGO
| S_IWUSR
,
525 tpci200_show_board_slot2
, tpci200_store_board_slot2
);
526 static DEVICE_ATTR(board_slot3
, S_IRUGO
| S_IWUSR
,
527 tpci200_show_board_slot3
, tpci200_store_board_slot3
);
529 static struct attribute
*tpci200_attrs
[] = {
530 &dev_attr_description
.attr
,
531 &dev_attr_board_slot0
.attr
,
532 &dev_attr_board_slot1
.attr
,
533 &dev_attr_board_slot2
.attr
,
534 &dev_attr_board_slot3
.attr
,
538 static struct attribute_group tpci200_attr_group
= {
539 .attrs
= tpci200_attrs
,
542 static int tpci200_create_sysfs_files(struct tpci200_board
*card
)
544 return sysfs_create_group(&card
->info
->pdev
->dev
.kobj
,
545 &tpci200_attr_group
);
548 static void tpci200_remove_sysfs_files(struct tpci200_board
*card
)
550 sysfs_remove_group(&card
->info
->pdev
->dev
.kobj
, &tpci200_attr_group
);
555 static int tpci200_create_sysfs_files(struct tpci200_board
*card
)
560 static void tpci200_remove_sysfs_files(struct tpci200_board
*card
)
564 #endif /* CONFIG_SYSFS */
566 static int tpci200_register(struct tpci200_board
*tpci200
)
570 unsigned long ioidint_base
;
571 unsigned long mem_base
;
572 unsigned short slot_ctrl
;
574 if (pci_enable_device(tpci200
->info
->pdev
) < 0)
577 if (tpci200_create_sysfs_files(tpci200
) < 0) {
578 pr_err("failed creating sysfs files\n");
580 goto out_disable_pci
;
583 /* Request IP interface register (Bar 2) */
584 res
= pci_request_region(tpci200
->info
->pdev
, TPCI200_IP_INTERFACE_BAR
,
585 "Carrier IP interface registers");
587 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
588 tpci200
->info
->pdev
->bus
->number
,
589 tpci200
->info
->pdev
->devfn
);
590 goto out_remove_sysfs
;
593 /* Request IO ID INT space (Bar 3) */
594 res
= pci_request_region(tpci200
->info
->pdev
,
595 TPCI200_IO_ID_INT_SPACES_BAR
,
596 "Carrier IO ID INT space");
598 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
599 tpci200
->info
->pdev
->bus
->number
,
600 tpci200
->info
->pdev
->devfn
);
601 goto out_release_ip_space
;
604 /* Request MEM space (Bar 4) */
605 res
= pci_request_region(tpci200
->info
->pdev
, TPCI200_MEM8_SPACE_BAR
,
606 "Carrier MEM space");
608 pr_err("(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
609 tpci200
->info
->pdev
->bus
->number
,
610 tpci200
->info
->pdev
->devfn
);
611 goto out_release_ioid_int_space
;
614 /* Map internal tpci200 driver user space */
615 tpci200
->info
->interface_regs
=
616 ioremap(pci_resource_start(tpci200
->info
->pdev
,
617 TPCI200_IP_INTERFACE_BAR
),
619 tpci200
->info
->ioidint_space
=
620 ioremap(pci_resource_start(tpci200
->info
->pdev
,
621 TPCI200_IO_ID_INT_SPACES_BAR
),
622 TPCI200_IOIDINT_SIZE
);
623 tpci200
->info
->mem8_space
=
624 ioremap(pci_resource_start(tpci200
->info
->pdev
,
625 TPCI200_MEM8_SPACE_BAR
),
628 spin_lock_init(&tpci200
->info
->access_lock
);
629 ioidint_base
= pci_resource_start(tpci200
->info
->pdev
,
630 TPCI200_IO_ID_INT_SPACES_BAR
);
631 mem_base
= pci_resource_start(tpci200
->info
->pdev
,
632 TPCI200_MEM8_SPACE_BAR
);
634 /* Set the default parameters of the slot
635 * INT0 disabled, level sensitive
636 * INT1 disabled, level sensitive
637 * error interrupt disabled
638 * timeout interrupt disabled
639 * recover time disabled
644 /* Set all slot physical address space */
645 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++) {
646 tpci200
->slots
[i
].io_phys
.address
=
647 (void __iomem
*)ioidint_base
+
648 TPCI200_IO_SPACE_OFF
+ TPCI200_IO_SPACE_GAP
*i
;
649 tpci200
->slots
[i
].io_phys
.size
= TPCI200_IO_SPACE_SIZE
;
651 tpci200
->slots
[i
].id_phys
.address
=
652 (void __iomem
*)ioidint_base
+
653 TPCI200_ID_SPACE_OFF
+ TPCI200_ID_SPACE_GAP
*i
;
654 tpci200
->slots
[i
].id_phys
.size
= TPCI200_ID_SPACE_SIZE
;
656 tpci200
->slots
[i
].mem_phys
.address
=
657 (void __iomem
*)mem_base
+ TPCI200_MEM8_GAP
*i
;
658 tpci200
->slots
[i
].mem_phys
.size
= TPCI200_MEM8_SIZE
;
660 writew(slot_ctrl
, (tpci200
->info
->interface_regs
+
664 res
= request_irq(tpci200
->info
->pdev
->irq
,
665 tpci200_interrupt
, IRQF_SHARED
,
666 KBUILD_MODNAME
, (void *) tpci200
);
668 pr_err("(bn 0x%X, sn 0x%X) unable to register IRQ !",
669 tpci200
->info
->pdev
->bus
->number
,
670 tpci200
->info
->pdev
->devfn
);
671 tpci200_unregister(tpci200
);
677 out_release_ioid_int_space
:
678 pci_release_region(tpci200
->info
->pdev
, TPCI200_IO_ID_INT_SPACES_BAR
);
679 out_release_ip_space
:
680 pci_release_region(tpci200
->info
->pdev
, TPCI200_IP_INTERFACE_BAR
);
682 tpci200_remove_sysfs_files(tpci200
);
684 pci_disable_device(tpci200
->info
->pdev
);
689 static int __tpci200_request_irq(struct tpci200_board
*tpci200
,
690 struct ipack_device
*dev
)
692 unsigned short slot_ctrl
;
694 /* Set the default parameters of the slot
695 * INT0 enabled, level sensitive
696 * INT1 enabled, level sensitive
697 * error interrupt disabled
698 * timeout interrupt disabled
699 * recover time disabled
702 slot_ctrl
= TPCI200_INT0_EN
| TPCI200_INT1_EN
;
703 writew(slot_ctrl
, (tpci200
->info
->interface_regs
+
704 control_reg
[dev
->slot
]));
709 static void __tpci200_free_irq(struct tpci200_board
*tpci200
,
710 struct ipack_device
*dev
)
712 unsigned short slot_ctrl
;
714 /* Set the default parameters of the slot
715 * INT0 disabled, level sensitive
716 * INT1 disabled, level sensitive
717 * error interrupt disabled
718 * timeout interrupt disabled
719 * recover time disabled
723 writew(slot_ctrl
, (tpci200
->info
->interface_regs
+
724 control_reg
[dev
->slot
]));
727 static int tpci200_free_irq(struct ipack_device
*dev
)
730 struct slot_irq
*slot_irq
;
731 struct tpci200_board
*tpci200
;
733 tpci200
= check_slot(dev
);
734 if (tpci200
== NULL
) {
739 if (mutex_lock_interruptible(&tpci200
->mutex
)) {
744 if (tpci200
->slots
[dev
->slot
].irq
== NULL
) {
749 __tpci200_free_irq(tpci200
, dev
);
750 slot_irq
= tpci200
->slots
[dev
->slot
].irq
;
751 tpci200
->slots
[dev
->slot
].irq
= NULL
;
755 mutex_unlock(&tpci200
->mutex
);
760 static int tpci200_slot_unmap_space(struct ipack_device
*dev
, int space
)
763 struct ipack_addr_space
*virt_addr_space
;
764 struct tpci200_board
*tpci200
;
766 tpci200
= check_slot(dev
);
767 if (tpci200
== NULL
) {
772 if (mutex_lock_interruptible(&tpci200
->mutex
)) {
779 if (dev
->io_space
.address
== NULL
) {
780 pr_info("Slot [%d:%d] IO space not mapped !\n",
781 dev
->bus_nr
, dev
->slot
);
784 virt_addr_space
= &dev
->io_space
;
787 if (dev
->id_space
.address
== NULL
) {
788 pr_info("Slot [%d:%d] ID space not mapped !\n",
789 dev
->bus_nr
, dev
->slot
);
792 virt_addr_space
= &dev
->id_space
;
794 case IPACK_MEM_SPACE
:
795 if (dev
->mem_space
.address
== NULL
) {
796 pr_info("Slot [%d:%d] MEM space not mapped !\n",
797 dev
->bus_nr
, dev
->slot
);
800 virt_addr_space
= &dev
->mem_space
;
803 pr_err("Slot [%d:%d] space number %d doesn't exist !\n",
804 dev
->bus_nr
, dev
->slot
, space
);
810 iounmap(virt_addr_space
->address
);
812 virt_addr_space
->address
= NULL
;
813 virt_addr_space
->size
= 0;
815 mutex_unlock(&tpci200
->mutex
);
820 static int tpci200_slot_unregister(struct ipack_device
*dev
)
822 struct tpci200_board
*tpci200
;
827 tpci200
= check_slot(dev
);
831 tpci200_free_irq(dev
);
833 if (mutex_lock_interruptible(&tpci200
->mutex
))
836 ipack_device_unregister(dev
);
837 tpci200
->slots
[dev
->slot
].dev
= NULL
;
838 mutex_unlock(&tpci200
->mutex
);
843 static int tpci200_slot_map_space(struct ipack_device
*dev
,
844 unsigned int memory_size
, int space
)
847 unsigned int size_to_map
;
848 void __iomem
*phys_address
;
849 struct ipack_addr_space
*virt_addr_space
;
850 struct tpci200_board
*tpci200
;
852 tpci200
= check_slot(dev
);
853 if (tpci200
== NULL
) {
858 if (mutex_lock_interruptible(&tpci200
->mutex
)) {
865 if (dev
->io_space
.address
!= NULL
) {
866 pr_err("Slot [%d:%d] IO space already mapped !\n",
867 tpci200
->number
, dev
->slot
);
871 virt_addr_space
= &dev
->io_space
;
873 phys_address
= tpci200
->slots
[dev
->slot
].io_phys
.address
;
874 size_to_map
= tpci200
->slots
[dev
->slot
].io_phys
.size
;
877 if (dev
->id_space
.address
!= NULL
) {
878 pr_err("Slot [%d:%d] ID space already mapped !\n",
879 tpci200
->number
, dev
->slot
);
883 virt_addr_space
= &dev
->id_space
;
885 phys_address
= tpci200
->slots
[dev
->slot
].id_phys
.address
;
886 size_to_map
= tpci200
->slots
[dev
->slot
].id_phys
.size
;
888 case IPACK_MEM_SPACE
:
889 if (dev
->mem_space
.address
!= NULL
) {
890 pr_err("Slot [%d:%d] MEM space already mapped !\n",
891 tpci200
->number
, dev
->slot
);
895 virt_addr_space
= &dev
->mem_space
;
897 if (memory_size
> tpci200
->slots
[dev
->slot
].mem_phys
.size
) {
898 pr_err("Slot [%d:%d] request is 0x%X memory, only 0x%X available !\n",
899 dev
->bus_nr
, dev
->slot
, memory_size
,
900 tpci200
->slots
[dev
->slot
].mem_phys
.size
);
905 phys_address
= tpci200
->slots
[dev
->slot
].mem_phys
.address
;
906 size_to_map
= memory_size
;
909 pr_err("Slot [%d:%d] space %d doesn't exist !\n",
910 tpci200
->number
, dev
->slot
, space
);
916 virt_addr_space
->size
= size_to_map
;
917 virt_addr_space
->address
=
918 ioremap((unsigned long)phys_address
, size_to_map
);
921 mutex_unlock(&tpci200
->mutex
);
926 static int tpci200_request_irq(struct ipack_device
*dev
, int vector
,
927 int (*handler
)(void *), void *arg
)
930 struct slot_irq
*slot_irq
;
931 struct tpci200_board
*tpci200
;
933 tpci200
= check_slot(dev
);
934 if (tpci200
== NULL
) {
939 if (mutex_lock_interruptible(&tpci200
->mutex
)) {
944 if (tpci200
->slots
[dev
->slot
].irq
!= NULL
) {
945 pr_err("Slot [%d:%d] IRQ already registered !\n", dev
->bus_nr
,
951 slot_irq
= kzalloc(sizeof(struct slot_irq
), GFP_KERNEL
);
952 if (slot_irq
== NULL
) {
953 pr_err("Slot [%d:%d] unable to allocate memory for IRQ !\n",
954 dev
->bus_nr
, dev
->slot
);
960 * WARNING: Setup Interrupt Vector in the IndustryPack device
961 * before an IRQ request.
962 * Read the User Manual of your IndustryPack device to know
963 * where to write the vector in memory.
965 slot_irq
->vector
= vector
;
966 slot_irq
->handler
= handler
;
968 slot_irq
->name
= dev_name(&dev
->dev
);
970 tpci200
->slots
[dev
->slot
].irq
= slot_irq
;
971 res
= __tpci200_request_irq(tpci200
, dev
);
974 mutex_unlock(&tpci200
->mutex
);
979 static void tpci200_slot_remove(struct tpci200_slot
*slot
)
981 if ((slot
->dev
== NULL
) ||
982 (slot
->dev
->driver
->ops
->remove
== NULL
))
985 slot
->dev
->driver
->ops
->remove(slot
->dev
);
988 static void tpci200_uninstall(struct tpci200_board
*tpci200
)
992 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++)
993 tpci200_slot_remove(&tpci200
->slots
[i
]);
995 tpci200_unregister(tpci200
);
996 kfree(tpci200
->slots
);
999 static struct ipack_bus_ops tpci200_bus_ops
= {
1000 .map_space
= tpci200_slot_map_space
,
1001 .unmap_space
= tpci200_slot_unmap_space
,
1002 .request_irq
= tpci200_request_irq
,
1003 .free_irq
= tpci200_free_irq
,
1004 .read8
= tpci200_read8
,
1005 .read16
= tpci200_read16
,
1006 .read32
= tpci200_read32
,
1007 .write8
= tpci200_write8
,
1008 .write16
= tpci200_write16
,
1009 .write32
= tpci200_write32
,
1010 .remove_device
= tpci200_slot_unregister
,
1013 static int tpci200_install(struct tpci200_board
*tpci200
)
1017 tpci200
->slots
= kzalloc(
1018 TPCI200_NB_SLOT
* sizeof(struct tpci200_slot
), GFP_KERNEL
);
1019 if (tpci200
->slots
== NULL
) {
1024 res
= tpci200_register(tpci200
);
1028 mutex_init(&tpci200
->mutex
);
1032 kfree(tpci200
->slots
);
1033 tpci200
->slots
= NULL
;
1038 static int tpci200_pciprobe(struct pci_dev
*pdev
,
1039 const struct pci_device_id
*id
)
1042 struct tpci200_board
*tpci200
;
1044 tpci200
= kzalloc(sizeof(struct tpci200_board
), GFP_KERNEL
);
1048 tpci200
->info
= kzalloc(sizeof(struct tpci200_infos
), GFP_KERNEL
);
1049 if (!tpci200
->info
) {
1054 /* Save struct pci_dev pointer */
1055 tpci200
->info
->pdev
= pdev
;
1056 tpci200
->info
->id_table
= (struct pci_device_id
*)id
;
1058 /* register the device and initialize it */
1059 ret
= tpci200_install(tpci200
);
1061 pr_err("Error during tpci200 install !\n");
1062 kfree(tpci200
->info
);
1067 /* Register the carrier in the industry pack bus driver */
1068 tpci200
->info
->ipack_bus
= ipack_bus_register(&pdev
->dev
,
1071 if (!tpci200
->info
->ipack_bus
) {
1072 pr_err("error registering the carrier on ipack driver\n");
1073 tpci200_uninstall(tpci200
);
1074 kfree(tpci200
->info
);
1079 /* save the bus number given by ipack to logging purpose */
1080 tpci200
->number
= tpci200
->info
->ipack_bus
->bus_nr
;
1081 dev_set_drvdata(&pdev
->dev
, tpci200
);
1082 /* add the registered device in an internal linked list */
1083 list_add_tail(&tpci200
->list
, &tpci200_list
);
1087 static void __tpci200_pci_remove(struct tpci200_board
*tpci200
)
1089 tpci200_uninstall(tpci200
);
1090 tpci200_remove_sysfs_files(tpci200
);
1091 list_del(&tpci200
->list
);
1092 ipack_bus_unregister(tpci200
->info
->ipack_bus
);
1093 kfree(tpci200
->info
);
1097 static void __devexit
tpci200_pci_remove(struct pci_dev
*dev
)
1099 struct tpci200_board
*tpci200
, *next
;
1101 /* Search the registered device to uninstall it */
1102 list_for_each_entry_safe(tpci200
, next
, &tpci200_list
, list
) {
1103 if (tpci200
->info
->pdev
== dev
) {
1104 __tpci200_pci_remove(tpci200
);
1110 static struct pci_device_id tpci200_idtable
[2] = {
1111 { TPCI200_VENDOR_ID
, TPCI200_DEVICE_ID
, TPCI200_SUBVENDOR_ID
,
1112 TPCI200_SUBDEVICE_ID
},
1116 static struct pci_driver tpci200_pci_drv
= {
1118 .id_table
= tpci200_idtable
,
1119 .probe
= tpci200_pciprobe
,
1120 .remove
= __devexit_p(tpci200_pci_remove
),
1123 static int __init
tpci200_drvr_init_module(void)
1125 return pci_register_driver(&tpci200_pci_drv
);
1128 static void __exit
tpci200_drvr_exit_module(void)
1130 struct tpci200_board
*tpci200
, *next
;
1132 list_for_each_entry_safe(tpci200
, next
, &tpci200_list
, list
)
1133 __tpci200_pci_remove(tpci200
);
1135 pci_unregister_driver(&tpci200_pci_drv
);
1138 MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
1139 MODULE_LICENSE("GPL");
1140 module_init(tpci200_drvr_init_module
);
1141 module_exit(tpci200_drvr_exit_module
);