1 // SPDX-License-Identifier: GPL-2.0-only
3 * speedfax.c low level stuff for Sedlbauer Speedfax+ cards
4 * based on the ISAR DSP
5 * Thanks to Sedlbauer AG for informations and HW
7 * Author Karsten Keil <keil@isdn4linux.de>
9 * Copyright 2009 by Karsten Keil <keil@isdn4linux.de>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/pci.h>
16 #include <linux/delay.h>
17 #include <linux/mISDNhw.h>
18 #include <linux/firmware.h>
22 #define SPEEDFAX_REV "2.0"
24 #define PCI_SUBVENDOR_SPEEDFAX_PYRAMID 0x51
25 #define PCI_SUBVENDOR_SPEEDFAX_PCI 0x54
26 #define PCI_SUB_ID_SEDLBAUER 0x01
28 #define SFAX_PCI_ADDR 0xc8
29 #define SFAX_PCI_ISAC 0xd0
30 #define SFAX_PCI_ISAR 0xe0
32 /* TIGER 100 Registers */
34 #define TIGER_RESET_ADDR 0x00
35 #define TIGER_EXTERN_RESET_ON 0x01
36 #define TIGER_EXTERN_RESET_OFF 0x00
37 #define TIGER_AUX_CTRL 0x02
38 #define TIGER_AUX_DATA 0x03
39 #define TIGER_AUX_IRQMASK 0x05
40 #define TIGER_AUX_STATUS 0x07
43 #define SFAX_AUX_IOMASK 0xdd /* 1 and 5 are inputs */
44 #define SFAX_ISAR_RESET_BIT_OFF 0x00
45 #define SFAX_ISAR_RESET_BIT_ON 0x01
46 #define SFAX_TIGER_IRQ_BIT 0x02
47 #define SFAX_LED1_BIT 0x08
48 #define SFAX_LED2_BIT 0x10
50 #define SFAX_PCI_RESET_ON (SFAX_ISAR_RESET_BIT_ON)
51 #define SFAX_PCI_RESET_OFF (SFAX_LED1_BIT | SFAX_LED2_BIT)
55 static u32 irqloops
= 4;
58 struct list_head list
;
60 char name
[MISDN_MAX_IDLEN
];
64 struct _ioport p_isac
;
65 struct _ioport p_isar
;
67 spinlock_t lock
; /* HW access lock */
72 static LIST_HEAD(Cards
);
73 static DEFINE_RWLOCK(card_lock
); /* protect Cards */
76 _set_debug(struct sfax_hw
*card
)
78 card
->isac
.dch
.debug
= debug
;
79 card
->isar
.ch
[0].bch
.debug
= debug
;
80 card
->isar
.ch
[1].bch
.debug
= debug
;
84 set_debug(const char *val
, const struct kernel_param
*kp
)
89 ret
= param_set_uint(val
, kp
);
91 read_lock(&card_lock
);
92 list_for_each_entry(card
, &Cards
, list
)
94 read_unlock(&card_lock
);
99 MODULE_AUTHOR("Karsten Keil");
100 MODULE_DESCRIPTION("mISDN driver for Sedlbauer Speedfax+ cards");
101 MODULE_LICENSE("GPL v2");
102 MODULE_VERSION(SPEEDFAX_REV
);
103 MODULE_FIRMWARE("isdn/ISAR.BIN");
104 module_param_call(debug
, set_debug
, param_get_uint
, &debug
, S_IRUGO
| S_IWUSR
);
105 MODULE_PARM_DESC(debug
, "Speedfax debug mask");
106 module_param(irqloops
, uint
, S_IRUGO
| S_IWUSR
);
107 MODULE_PARM_DESC(irqloops
, "Speedfax maximal irqloops (default 4)");
109 IOFUNC_IND(ISAC
, sfax_hw
, p_isac
)
110 IOFUNC_IND(ISAR
, sfax_hw
, p_isar
)
113 speedfax_irq(int intno
, void *dev_id
)
115 struct sfax_hw
*sf
= dev_id
;
119 spin_lock(&sf
->lock
);
120 val
= inb(sf
->cfg
+ TIGER_AUX_STATUS
);
121 if (val
& SFAX_TIGER_IRQ_BIT
) { /* for us or shared ? */
122 spin_unlock(&sf
->lock
);
123 return IRQ_NONE
; /* shared */
126 val
= ReadISAR_IND(sf
, ISAR_IRQBIT
);
128 if (val
& ISAR_IRQSTA
)
129 mISDNisar_irq(&sf
->isar
);
130 val
= ReadISAC_IND(sf
, ISAC_ISTA
);
132 mISDNisac_irq(&sf
->isac
, val
);
133 val
= ReadISAR_IND(sf
, ISAR_IRQBIT
);
134 if ((val
& ISAR_IRQSTA
) && cnt
--)
137 pr_debug("%s: %d irqloops cpu%d\n", sf
->name
,
138 irqloops
- cnt
, smp_processor_id());
139 if (irqloops
&& !cnt
)
140 pr_notice("%s: %d IRQ LOOP cpu%d\n", sf
->name
,
141 irqloops
, smp_processor_id());
142 spin_unlock(&sf
->lock
);
147 enable_hwirq(struct sfax_hw
*sf
)
149 WriteISAC_IND(sf
, ISAC_MASK
, 0);
150 WriteISAR_IND(sf
, ISAR_IRQBIT
, ISAR_IRQMSK
);
151 outb(SFAX_TIGER_IRQ_BIT
, sf
->cfg
+ TIGER_AUX_IRQMASK
);
155 disable_hwirq(struct sfax_hw
*sf
)
157 WriteISAC_IND(sf
, ISAC_MASK
, 0xFF);
158 WriteISAR_IND(sf
, ISAR_IRQBIT
, 0);
159 outb(0, sf
->cfg
+ TIGER_AUX_IRQMASK
);
163 reset_speedfax(struct sfax_hw
*sf
)
166 pr_debug("%s: resetting card\n", sf
->name
);
167 outb(TIGER_EXTERN_RESET_ON
, sf
->cfg
+ TIGER_RESET_ADDR
);
168 outb(SFAX_PCI_RESET_ON
, sf
->cfg
+ TIGER_AUX_DATA
);
170 outb(TIGER_EXTERN_RESET_OFF
, sf
->cfg
+ TIGER_RESET_ADDR
);
171 sf
->aux_data
= SFAX_PCI_RESET_OFF
;
172 outb(sf
->aux_data
, sf
->cfg
+ TIGER_AUX_DATA
);
177 sfax_ctrl(struct sfax_hw
*sf
, u32 cmd
, u_long arg
)
185 case HW_ACTIVATE_IND
:
187 sf
->aux_data
&= ~SFAX_LED1_BIT
;
189 sf
->aux_data
&= ~SFAX_LED2_BIT
;
190 outb(sf
->aux_data
, sf
->cfg
+ TIGER_AUX_DATA
);
194 sf
->aux_data
|= SFAX_LED1_BIT
;
196 sf
->aux_data
|= SFAX_LED2_BIT
;
197 outb(sf
->aux_data
, sf
->cfg
+ TIGER_AUX_DATA
);
200 pr_info("%s: %s unknown command %x %lx\n",
201 sf
->name
, __func__
, cmd
, arg
);
209 channel_ctrl(struct sfax_hw
*sf
, struct mISDN_ctrl_req
*cq
)
214 case MISDN_CTRL_GETOP
:
215 cq
->op
= MISDN_CTRL_LOOP
| MISDN_CTRL_L1_TIMER3
;
217 case MISDN_CTRL_LOOP
:
218 /* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */
219 if (cq
->channel
< 0 || cq
->channel
> 3) {
223 ret
= sf
->isac
.ctrl(&sf
->isac
, HW_TESTLOOP
, cq
->channel
);
225 case MISDN_CTRL_L1_TIMER3
:
226 ret
= sf
->isac
.ctrl(&sf
->isac
, HW_TIMER3_VALUE
, cq
->p1
);
229 pr_info("%s: unknown Op %x\n", sf
->name
, cq
->op
);
237 sfax_dctrl(struct mISDNchannel
*ch
, u32 cmd
, void *arg
)
239 struct mISDNdevice
*dev
= container_of(ch
, struct mISDNdevice
, D
);
240 struct dchannel
*dch
= container_of(dev
, struct dchannel
, dev
);
241 struct sfax_hw
*sf
= dch
->hw
;
242 struct channel_req
*rq
;
245 pr_debug("%s: cmd:%x %p\n", sf
->name
, cmd
, arg
);
249 if (rq
->protocol
== ISDN_P_TE_S0
)
250 err
= sf
->isac
.open(&sf
->isac
, rq
);
252 err
= sf
->isar
.open(&sf
->isar
, rq
);
255 if (!try_module_get(THIS_MODULE
))
256 pr_info("%s: cannot get module\n", sf
->name
);
259 pr_debug("%s: dev(%d) close from %p\n", sf
->name
,
260 dch
->dev
.id
, __builtin_return_address(0));
261 module_put(THIS_MODULE
);
263 case CONTROL_CHANNEL
:
264 err
= channel_ctrl(sf
, arg
);
267 pr_debug("%s: unknown command %x\n", sf
->name
, cmd
);
274 init_card(struct sfax_hw
*sf
)
279 ret
= request_irq(sf
->irq
, speedfax_irq
, IRQF_SHARED
, sf
->name
, sf
);
281 pr_info("%s: couldn't get interrupt %d\n", sf
->name
, sf
->irq
);
285 spin_lock_irqsave(&sf
->lock
, flags
);
286 ret
= sf
->isac
.init(&sf
->isac
);
288 spin_unlock_irqrestore(&sf
->lock
, flags
);
289 pr_info("%s: ISAC init failed with %d\n",
294 /* RESET Receiver and Transmitter */
295 WriteISAC_IND(sf
, ISAC_CMDR
, 0x41);
296 spin_unlock_irqrestore(&sf
->lock
, flags
);
297 msleep_interruptible(10);
298 if (debug
& DEBUG_HW
)
299 pr_notice("%s: IRQ %d count %d\n", sf
->name
,
300 sf
->irq
, sf
->irqcnt
);
302 pr_info("%s: IRQ(%d) got no requests during init %d\n",
303 sf
->name
, sf
->irq
, 3 - cnt
);
307 free_irq(sf
->irq
, sf
);
313 setup_speedfax(struct sfax_hw
*sf
)
317 if (!request_region(sf
->cfg
, 256, sf
->name
)) {
318 pr_info("mISDN: %s config port %x-%x already in use\n",
319 sf
->name
, sf
->cfg
, sf
->cfg
+ 255);
324 outb(0xdd, sf
->cfg
+ TIGER_AUX_CTRL
);
325 outb(0, sf
->cfg
+ TIGER_AUX_IRQMASK
);
327 sf
->isac
.type
= IPAC_TYPE_ISAC
;
328 sf
->p_isac
.ale
= sf
->cfg
+ SFAX_PCI_ADDR
;
329 sf
->p_isac
.port
= sf
->cfg
+ SFAX_PCI_ISAC
;
330 sf
->p_isar
.ale
= sf
->cfg
+ SFAX_PCI_ADDR
;
331 sf
->p_isar
.port
= sf
->cfg
+ SFAX_PCI_ISAR
;
332 ASSIGN_FUNC(IND
, ISAC
, sf
->isac
);
333 ASSIGN_FUNC(IND
, ISAR
, sf
->isar
);
334 spin_lock_irqsave(&sf
->lock
, flags
);
337 spin_unlock_irqrestore(&sf
->lock
, flags
);
342 release_card(struct sfax_hw
*card
) {
345 spin_lock_irqsave(&card
->lock
, flags
);
347 spin_unlock_irqrestore(&card
->lock
, flags
);
348 card
->isac
.release(&card
->isac
);
349 free_irq(card
->irq
, card
);
350 card
->isar
.release(&card
->isar
);
351 mISDN_unregister_device(&card
->isac
.dch
.dev
);
352 release_region(card
->cfg
, 256);
353 pci_disable_device(card
->pdev
);
354 pci_set_drvdata(card
->pdev
, NULL
);
355 write_lock_irqsave(&card_lock
, flags
);
356 list_del(&card
->list
);
357 write_unlock_irqrestore(&card_lock
, flags
);
363 setup_instance(struct sfax_hw
*card
)
365 const struct firmware
*firmware
;
369 snprintf(card
->name
, MISDN_MAX_IDLEN
- 1, "Speedfax.%d", sfax_cnt
+ 1);
370 write_lock_irqsave(&card_lock
, flags
);
371 list_add_tail(&card
->list
, &Cards
);
372 write_unlock_irqrestore(&card_lock
, flags
);
374 spin_lock_init(&card
->lock
);
375 card
->isac
.hwlock
= &card
->lock
;
376 card
->isar
.hwlock
= &card
->lock
;
377 card
->isar
.ctrl
= (void *)&sfax_ctrl
;
378 card
->isac
.name
= card
->name
;
379 card
->isar
.name
= card
->name
;
380 card
->isar
.owner
= THIS_MODULE
;
382 err
= request_firmware(&firmware
, "isdn/ISAR.BIN", &card
->pdev
->dev
);
384 pr_info("%s: firmware request failed %d\n",
388 if (debug
& DEBUG_HW
)
389 pr_notice("%s: got firmware %zu bytes\n",
390 card
->name
, firmware
->size
);
392 mISDNisac_init(&card
->isac
, card
);
394 card
->isac
.dch
.dev
.D
.ctrl
= sfax_dctrl
;
395 card
->isac
.dch
.dev
.Bprotocols
=
396 mISDNisar_init(&card
->isar
, card
);
397 for (i
= 0; i
< 2; i
++) {
398 set_channelmap(i
+ 1, card
->isac
.dch
.dev
.channelmap
);
399 list_add(&card
->isar
.ch
[i
].bch
.ch
.list
,
400 &card
->isac
.dch
.dev
.bchannels
);
403 err
= setup_speedfax(card
);
406 err
= card
->isar
.init(&card
->isar
);
409 err
= mISDN_register_device(&card
->isac
.dch
.dev
,
410 &card
->pdev
->dev
, card
->name
);
413 err
= init_card(card
);
416 err
= card
->isar
.firmware(&card
->isar
, firmware
->data
, firmware
->size
);
418 release_firmware(firmware
);
420 pr_notice("SpeedFax %d cards installed\n", sfax_cnt
);
424 free_irq(card
->irq
, card
);
426 mISDN_unregister_device(&card
->isac
.dch
.dev
);
428 release_region(card
->cfg
, 256);
430 card
->isac
.release(&card
->isac
);
431 card
->isar
.release(&card
->isar
);
432 release_firmware(firmware
);
434 pci_disable_device(card
->pdev
);
435 write_lock_irqsave(&card_lock
, flags
);
436 list_del(&card
->list
);
437 write_unlock_irqrestore(&card_lock
, flags
);
443 sfaxpci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
446 struct sfax_hw
*card
= kzalloc(sizeof(struct sfax_hw
), GFP_KERNEL
);
449 pr_info("No memory for Speedfax+ PCI\n");
453 err
= pci_enable_device(pdev
);
459 pr_notice("mISDN: Speedfax found adapter %s at %s\n",
460 (char *)ent
->driver_data
, pci_name(pdev
));
462 card
->cfg
= pci_resource_start(pdev
, 0);
463 card
->irq
= pdev
->irq
;
464 pci_set_drvdata(pdev
, card
);
465 err
= setup_instance(card
);
467 pci_set_drvdata(pdev
, NULL
);
472 sfax_remove_pci(struct pci_dev
*pdev
)
474 struct sfax_hw
*card
= pci_get_drvdata(pdev
);
479 pr_debug("%s: drvdata already removed\n", __func__
);
482 static struct pci_device_id sfaxpci_ids
[] = {
483 { PCI_VENDOR_ID_TIGERJET
, PCI_DEVICE_ID_TIGERJET_100
,
484 PCI_SUBVENDOR_SPEEDFAX_PYRAMID
, PCI_SUB_ID_SEDLBAUER
,
485 0, 0, (unsigned long) "Pyramid Speedfax + PCI"
487 { PCI_VENDOR_ID_TIGERJET
, PCI_DEVICE_ID_TIGERJET_100
,
488 PCI_SUBVENDOR_SPEEDFAX_PCI
, PCI_SUB_ID_SEDLBAUER
,
489 0, 0, (unsigned long) "Sedlbauer Speedfax + PCI"
493 MODULE_DEVICE_TABLE(pci
, sfaxpci_ids
);
495 static struct pci_driver sfaxpci_driver
= {
496 .name
= "speedfax+ pci",
497 .probe
= sfaxpci_probe
,
498 .remove
= sfax_remove_pci
,
499 .id_table
= sfaxpci_ids
,
507 pr_notice("Sedlbauer Speedfax+ Driver Rev. %s\n",
509 err
= pci_register_driver(&sfaxpci_driver
);
514 Speedfax_cleanup(void)
516 pci_unregister_driver(&sfaxpci_driver
);
519 module_init(Speedfax_init
);
520 module_exit(Speedfax_cleanup
);