4 * driver for the TEWS TPCI-200 device
6 * Copyright (C) 2009-2012 CERN (www.cern.ch)
7 * Author: Nicolas Serafini, EIC2 SA
8 * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
15 #include <linux/module.h>
16 #include <linux/slab.h>
19 static const u16 tpci200_status_timeout
[] = {
26 static const u16 tpci200_status_error
[] = {
33 static const size_t tpci200_space_size
[IPACK_SPACE_COUNT
] = {
34 [IPACK_IO_SPACE
] = TPCI200_IO_SPACE_SIZE
,
35 [IPACK_ID_SPACE
] = TPCI200_ID_SPACE_SIZE
,
36 [IPACK_INT_SPACE
] = TPCI200_INT_SPACE_SIZE
,
37 [IPACK_MEM8_SPACE
] = TPCI200_MEM8_SPACE_SIZE
,
38 [IPACK_MEM16_SPACE
] = TPCI200_MEM16_SPACE_SIZE
,
41 static const size_t tpci200_space_interval
[IPACK_SPACE_COUNT
] = {
42 [IPACK_IO_SPACE
] = TPCI200_IO_SPACE_INTERVAL
,
43 [IPACK_ID_SPACE
] = TPCI200_ID_SPACE_INTERVAL
,
44 [IPACK_INT_SPACE
] = TPCI200_INT_SPACE_INTERVAL
,
45 [IPACK_MEM8_SPACE
] = TPCI200_MEM8_SPACE_INTERVAL
,
46 [IPACK_MEM16_SPACE
] = TPCI200_MEM16_SPACE_INTERVAL
,
49 static struct tpci200_board
*check_slot(struct ipack_device
*dev
)
51 struct tpci200_board
*tpci200
;
57 tpci200
= dev_get_drvdata(dev
->bus
->parent
);
59 if (tpci200
== NULL
) {
60 dev_info(&dev
->dev
, "carrier board not found\n");
64 if (dev
->slot
>= TPCI200_NB_SLOT
) {
66 "Slot [%d:%d] doesn't exist! Last tpci200 slot is %d.\n",
67 dev
->bus
->bus_nr
, dev
->slot
, TPCI200_NB_SLOT
-1);
74 static void tpci200_clear_mask(struct tpci200_board
*tpci200
,
75 __le16 __iomem
*addr
, u16 mask
)
78 spin_lock_irqsave(&tpci200
->regs_lock
, flags
);
79 iowrite16(ioread16(addr
) & (~mask
), addr
);
80 spin_unlock_irqrestore(&tpci200
->regs_lock
, flags
);
83 static void tpci200_set_mask(struct tpci200_board
*tpci200
,
84 __le16 __iomem
*addr
, u16 mask
)
87 spin_lock_irqsave(&tpci200
->regs_lock
, flags
);
88 iowrite16(ioread16(addr
) | mask
, addr
);
89 spin_unlock_irqrestore(&tpci200
->regs_lock
, flags
);
92 static void tpci200_unregister(struct tpci200_board
*tpci200
)
94 free_irq(tpci200
->info
->pdev
->irq
, (void *) tpci200
);
96 pci_iounmap(tpci200
->info
->pdev
, tpci200
->info
->interface_regs
);
97 pci_iounmap(tpci200
->info
->pdev
, tpci200
->info
->cfg_regs
);
99 pci_release_region(tpci200
->info
->pdev
, TPCI200_IP_INTERFACE_BAR
);
100 pci_release_region(tpci200
->info
->pdev
, TPCI200_IO_ID_INT_SPACES_BAR
);
101 pci_release_region(tpci200
->info
->pdev
, TPCI200_MEM16_SPACE_BAR
);
102 pci_release_region(tpci200
->info
->pdev
, TPCI200_MEM8_SPACE_BAR
);
103 pci_release_region(tpci200
->info
->pdev
, TPCI200_CFG_MEM_BAR
);
105 pci_disable_device(tpci200
->info
->pdev
);
106 pci_dev_put(tpci200
->info
->pdev
);
109 static void tpci200_enable_irq(struct tpci200_board
*tpci200
,
112 tpci200_set_mask(tpci200
,
113 &tpci200
->info
->interface_regs
->control
[islot
],
114 TPCI200_INT0_EN
| TPCI200_INT1_EN
);
117 static void tpci200_disable_irq(struct tpci200_board
*tpci200
,
120 tpci200_clear_mask(tpci200
,
121 &tpci200
->info
->interface_regs
->control
[islot
],
122 TPCI200_INT0_EN
| TPCI200_INT1_EN
);
125 static irqreturn_t
tpci200_slot_irq(struct slot_irq
*slot_irq
)
131 ret
= slot_irq
->handler(slot_irq
->arg
);
136 static irqreturn_t
tpci200_interrupt(int irq
, void *dev_id
)
138 struct tpci200_board
*tpci200
= (struct tpci200_board
*) dev_id
;
139 struct slot_irq
*slot_irq
;
144 /* Read status register */
145 status_reg
= ioread16(&tpci200
->info
->interface_regs
->status
);
147 /* Did we cause the interrupt? */
148 if (!(status_reg
& TPCI200_SLOT_INT_MASK
))
151 /* callback to the IRQ handler for the corresponding slot */
153 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++) {
154 if (!(status_reg
& ((TPCI200_A_INT0
| TPCI200_A_INT1
) << (2 * i
))))
156 slot_irq
= rcu_dereference(tpci200
->slots
[i
].irq
);
157 ret
= tpci200_slot_irq(slot_irq
);
158 if (ret
== -ENODEV
) {
159 dev_info(&tpci200
->info
->pdev
->dev
,
160 "No registered ISR for slot [%d:%d]!. IRQ will be disabled.\n",
162 tpci200_disable_irq(tpci200
, i
);
170 static int tpci200_free_irq(struct ipack_device
*dev
)
172 struct slot_irq
*slot_irq
;
173 struct tpci200_board
*tpci200
;
175 tpci200
= check_slot(dev
);
179 if (mutex_lock_interruptible(&tpci200
->mutex
))
182 if (tpci200
->slots
[dev
->slot
].irq
== NULL
) {
183 mutex_unlock(&tpci200
->mutex
);
187 tpci200_disable_irq(tpci200
, dev
->slot
);
188 slot_irq
= tpci200
->slots
[dev
->slot
].irq
;
189 /* uninstall handler */
190 RCU_INIT_POINTER(tpci200
->slots
[dev
->slot
].irq
, NULL
);
193 mutex_unlock(&tpci200
->mutex
);
197 static int tpci200_request_irq(struct ipack_device
*dev
,
198 irqreturn_t (*handler
)(void *), void *arg
)
201 struct slot_irq
*slot_irq
;
202 struct tpci200_board
*tpci200
;
204 tpci200
= check_slot(dev
);
208 if (mutex_lock_interruptible(&tpci200
->mutex
))
211 if (tpci200
->slots
[dev
->slot
].irq
!= NULL
) {
213 "Slot [%d:%d] IRQ already registered !\n",
220 slot_irq
= kzalloc(sizeof(struct slot_irq
), GFP_KERNEL
);
221 if (slot_irq
== NULL
) {
223 "Slot [%d:%d] unable to allocate memory for IRQ !\n",
224 dev
->bus
->bus_nr
, dev
->slot
);
230 * WARNING: Setup Interrupt Vector in the IndustryPack device
231 * before an IRQ request.
232 * Read the User Manual of your IndustryPack device to know
233 * where to write the vector in memory.
235 slot_irq
->handler
= handler
;
237 slot_irq
->holder
= dev
;
239 rcu_assign_pointer(tpci200
->slots
[dev
->slot
].irq
, slot_irq
);
240 tpci200_enable_irq(tpci200
, dev
->slot
);
243 mutex_unlock(&tpci200
->mutex
);
247 static int tpci200_register(struct tpci200_board
*tpci200
)
251 phys_addr_t ioidint_base
;
252 unsigned short slot_ctrl
;
254 if (pci_enable_device(tpci200
->info
->pdev
) < 0)
257 /* Request IP interface register (Bar 2) */
258 res
= pci_request_region(tpci200
->info
->pdev
, TPCI200_IP_INTERFACE_BAR
,
259 "Carrier IP interface registers");
261 dev_err(&tpci200
->info
->pdev
->dev
,
262 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 2 !",
263 tpci200
->info
->pdev
->bus
->number
,
264 tpci200
->info
->pdev
->devfn
);
265 goto out_disable_pci
;
268 /* Request IO ID INT space (Bar 3) */
269 res
= pci_request_region(tpci200
->info
->pdev
,
270 TPCI200_IO_ID_INT_SPACES_BAR
,
271 "Carrier IO ID INT space");
273 dev_err(&tpci200
->info
->pdev
->dev
,
274 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 3 !",
275 tpci200
->info
->pdev
->bus
->number
,
276 tpci200
->info
->pdev
->devfn
);
277 goto out_release_ip_space
;
280 /* Request MEM8 space (Bar 5) */
281 res
= pci_request_region(tpci200
->info
->pdev
, TPCI200_MEM8_SPACE_BAR
,
282 "Carrier MEM8 space");
284 dev_err(&tpci200
->info
->pdev
->dev
,
285 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 5!",
286 tpci200
->info
->pdev
->bus
->number
,
287 tpci200
->info
->pdev
->devfn
);
288 goto out_release_ioid_int_space
;
291 /* Request MEM16 space (Bar 4) */
292 res
= pci_request_region(tpci200
->info
->pdev
, TPCI200_MEM16_SPACE_BAR
,
293 "Carrier MEM16 space");
295 dev_err(&tpci200
->info
->pdev
->dev
,
296 "(bn 0x%X, sn 0x%X) failed to allocate PCI resource for BAR 4!",
297 tpci200
->info
->pdev
->bus
->number
,
298 tpci200
->info
->pdev
->devfn
);
299 goto out_release_mem8_space
;
302 /* Map internal tpci200 driver user space */
303 tpci200
->info
->interface_regs
=
304 ioremap_nocache(pci_resource_start(tpci200
->info
->pdev
,
305 TPCI200_IP_INTERFACE_BAR
),
307 if (!tpci200
->info
->interface_regs
) {
308 dev_err(&tpci200
->info
->pdev
->dev
,
309 "(bn 0x%X, sn 0x%X) failed to map driver user space!",
310 tpci200
->info
->pdev
->bus
->number
,
311 tpci200
->info
->pdev
->devfn
);
313 goto out_release_mem8_space
;
316 /* Initialize lock that protects interface_regs */
317 spin_lock_init(&tpci200
->regs_lock
);
319 ioidint_base
= pci_resource_start(tpci200
->info
->pdev
,
320 TPCI200_IO_ID_INT_SPACES_BAR
);
321 tpci200
->mod_mem
[IPACK_IO_SPACE
] = ioidint_base
+ TPCI200_IO_SPACE_OFF
;
322 tpci200
->mod_mem
[IPACK_ID_SPACE
] = ioidint_base
+ TPCI200_ID_SPACE_OFF
;
323 tpci200
->mod_mem
[IPACK_INT_SPACE
] =
324 ioidint_base
+ TPCI200_INT_SPACE_OFF
;
325 tpci200
->mod_mem
[IPACK_MEM8_SPACE
] =
326 pci_resource_start(tpci200
->info
->pdev
,
327 TPCI200_MEM8_SPACE_BAR
);
328 tpci200
->mod_mem
[IPACK_MEM16_SPACE
] =
329 pci_resource_start(tpci200
->info
->pdev
,
330 TPCI200_MEM16_SPACE_BAR
);
332 /* Set the default parameters of the slot
333 * INT0 disabled, level sensitive
334 * INT1 disabled, level sensitive
335 * error interrupt disabled
336 * timeout interrupt disabled
337 * recover time disabled
341 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++)
342 writew(slot_ctrl
, &tpci200
->info
->interface_regs
->control
[i
]);
344 res
= request_irq(tpci200
->info
->pdev
->irq
,
345 tpci200_interrupt
, IRQF_SHARED
,
346 KBUILD_MODNAME
, (void *) tpci200
);
348 dev_err(&tpci200
->info
->pdev
->dev
,
349 "(bn 0x%X, sn 0x%X) unable to register IRQ !",
350 tpci200
->info
->pdev
->bus
->number
,
351 tpci200
->info
->pdev
->devfn
);
352 goto out_release_ioid_int_space
;
357 out_release_mem8_space
:
358 pci_release_region(tpci200
->info
->pdev
, TPCI200_MEM8_SPACE_BAR
);
359 out_release_ioid_int_space
:
360 pci_release_region(tpci200
->info
->pdev
, TPCI200_IO_ID_INT_SPACES_BAR
);
361 out_release_ip_space
:
362 pci_release_region(tpci200
->info
->pdev
, TPCI200_IP_INTERFACE_BAR
);
364 pci_disable_device(tpci200
->info
->pdev
);
368 static int tpci200_get_clockrate(struct ipack_device
*dev
)
370 struct tpci200_board
*tpci200
= check_slot(dev
);
371 __le16 __iomem
*addr
;
376 addr
= &tpci200
->info
->interface_regs
->control
[dev
->slot
];
377 return (ioread16(addr
) & TPCI200_CLK32
) ? 32 : 8;
380 static int tpci200_set_clockrate(struct ipack_device
*dev
, int mherz
)
382 struct tpci200_board
*tpci200
= check_slot(dev
);
383 __le16 __iomem
*addr
;
388 addr
= &tpci200
->info
->interface_regs
->control
[dev
->slot
];
392 tpci200_clear_mask(tpci200
, addr
, TPCI200_CLK32
);
395 tpci200_set_mask(tpci200
, addr
, TPCI200_CLK32
);
403 static int tpci200_get_error(struct ipack_device
*dev
)
405 struct tpci200_board
*tpci200
= check_slot(dev
);
406 __le16 __iomem
*addr
;
412 addr
= &tpci200
->info
->interface_regs
->status
;
413 mask
= tpci200_status_error
[dev
->slot
];
414 return (ioread16(addr
) & mask
) ? 1 : 0;
417 static int tpci200_get_timeout(struct ipack_device
*dev
)
419 struct tpci200_board
*tpci200
= check_slot(dev
);
420 __le16 __iomem
*addr
;
426 addr
= &tpci200
->info
->interface_regs
->status
;
427 mask
= tpci200_status_timeout
[dev
->slot
];
429 return (ioread16(addr
) & mask
) ? 1 : 0;
432 static int tpci200_reset_timeout(struct ipack_device
*dev
)
434 struct tpci200_board
*tpci200
= check_slot(dev
);
435 __le16 __iomem
*addr
;
441 addr
= &tpci200
->info
->interface_regs
->status
;
442 mask
= tpci200_status_timeout
[dev
->slot
];
444 iowrite16(mask
, addr
);
448 static void tpci200_uninstall(struct tpci200_board
*tpci200
)
450 tpci200_unregister(tpci200
);
451 kfree(tpci200
->slots
);
454 static const struct ipack_bus_ops tpci200_bus_ops
= {
455 .request_irq
= tpci200_request_irq
,
456 .free_irq
= tpci200_free_irq
,
457 .get_clockrate
= tpci200_get_clockrate
,
458 .set_clockrate
= tpci200_set_clockrate
,
459 .get_error
= tpci200_get_error
,
460 .get_timeout
= tpci200_get_timeout
,
461 .reset_timeout
= tpci200_reset_timeout
,
464 static int tpci200_install(struct tpci200_board
*tpci200
)
468 tpci200
->slots
= kcalloc(TPCI200_NB_SLOT
, sizeof(struct tpci200_slot
),
470 if (tpci200
->slots
== NULL
)
473 res
= tpci200_register(tpci200
);
475 kfree(tpci200
->slots
);
476 tpci200
->slots
= NULL
;
480 mutex_init(&tpci200
->mutex
);
484 static void tpci200_release_device(struct ipack_device
*dev
)
489 static int tpci200_create_device(struct tpci200_board
*tpci200
, int i
)
492 enum ipack_space space
;
493 struct ipack_device
*dev
=
494 kzalloc(sizeof(struct ipack_device
), GFP_KERNEL
);
498 dev
->bus
= tpci200
->info
->ipack_bus
;
499 dev
->release
= tpci200_release_device
;
501 for (space
= 0; space
< IPACK_SPACE_COUNT
; space
++) {
502 dev
->region
[space
].start
=
503 tpci200
->mod_mem
[space
]
504 + tpci200_space_interval
[space
] * i
;
505 dev
->region
[space
].size
= tpci200_space_size
[space
];
508 ret
= ipack_device_init(dev
);
510 ipack_put_device(dev
);
514 ret
= ipack_device_add(dev
);
516 ipack_put_device(dev
);
521 static int tpci200_pci_probe(struct pci_dev
*pdev
,
522 const struct pci_device_id
*id
)
525 struct tpci200_board
*tpci200
;
528 tpci200
= kzalloc(sizeof(struct tpci200_board
), GFP_KERNEL
);
532 tpci200
->info
= kzalloc(sizeof(struct tpci200_infos
), GFP_KERNEL
);
533 if (!tpci200
->info
) {
540 /* Obtain a mapping of the carrier's PCI configuration registers */
541 ret
= pci_request_region(pdev
, TPCI200_CFG_MEM_BAR
,
542 KBUILD_MODNAME
" Configuration Memory");
544 dev_err(&pdev
->dev
, "Failed to allocate PCI Configuration Memory");
546 goto out_err_pci_request
;
548 tpci200
->info
->cfg_regs
= ioremap_nocache(
549 pci_resource_start(pdev
, TPCI200_CFG_MEM_BAR
),
550 pci_resource_len(pdev
, TPCI200_CFG_MEM_BAR
));
551 if (!tpci200
->info
->cfg_regs
) {
552 dev_err(&pdev
->dev
, "Failed to map PCI Configuration Memory");
554 goto out_err_ioremap
;
557 /* Disable byte swapping for 16 bit IP module access. This will ensure
558 * that the Industrypack big endian byte order is preserved by the
560 reg32
= ioread32(tpci200
->info
->cfg_regs
+ LAS1_DESC
);
561 reg32
|= 1 << LAS_BIT_BIGENDIAN
;
562 iowrite32(reg32
, tpci200
->info
->cfg_regs
+ LAS1_DESC
);
564 reg32
= ioread32(tpci200
->info
->cfg_regs
+ LAS2_DESC
);
565 reg32
|= 1 << LAS_BIT_BIGENDIAN
;
566 iowrite32(reg32
, tpci200
->info
->cfg_regs
+ LAS2_DESC
);
568 /* Save struct pci_dev pointer */
569 tpci200
->info
->pdev
= pdev
;
570 tpci200
->info
->id_table
= (struct pci_device_id
*)id
;
572 /* register the device and initialize it */
573 ret
= tpci200_install(tpci200
);
575 dev_err(&pdev
->dev
, "error during tpci200 install\n");
577 goto out_err_install
;
580 /* Register the carrier in the industry pack bus driver */
581 tpci200
->info
->ipack_bus
= ipack_bus_register(&pdev
->dev
,
585 if (!tpci200
->info
->ipack_bus
) {
587 "error registering the carrier on ipack driver\n");
589 goto out_err_bus_register
;
592 /* save the bus number given by ipack to logging purpose */
593 tpci200
->number
= tpci200
->info
->ipack_bus
->bus_nr
;
594 dev_set_drvdata(&pdev
->dev
, tpci200
);
596 for (i
= 0; i
< TPCI200_NB_SLOT
; i
++)
597 tpci200_create_device(tpci200
, i
);
600 out_err_bus_register
:
601 tpci200_uninstall(tpci200
);
603 iounmap(tpci200
->info
->cfg_regs
);
605 pci_release_region(pdev
, TPCI200_CFG_MEM_BAR
);
608 kfree(tpci200
->info
);
614 static void __tpci200_pci_remove(struct tpci200_board
*tpci200
)
616 ipack_bus_unregister(tpci200
->info
->ipack_bus
);
617 tpci200_uninstall(tpci200
);
619 kfree(tpci200
->info
);
623 static void tpci200_pci_remove(struct pci_dev
*dev
)
625 struct tpci200_board
*tpci200
= pci_get_drvdata(dev
);
627 __tpci200_pci_remove(tpci200
);
630 static const struct pci_device_id tpci200_idtable
[] = {
631 { TPCI200_VENDOR_ID
, TPCI200_DEVICE_ID
, TPCI200_SUBVENDOR_ID
,
632 TPCI200_SUBDEVICE_ID
},
636 MODULE_DEVICE_TABLE(pci
, tpci200_idtable
);
638 static struct pci_driver tpci200_pci_drv
= {
640 .id_table
= tpci200_idtable
,
641 .probe
= tpci200_pci_probe
,
642 .remove
= tpci200_pci_remove
,
645 module_pci_driver(tpci200_pci_drv
);
647 MODULE_DESCRIPTION("TEWS TPCI-200 device driver");
648 MODULE_LICENSE("GPL");