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_LICENSE("GPL v2");
101 MODULE_VERSION(SPEEDFAX_REV);
103 module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
104 MODULE_PARM_DESC(debug, "Speedfax debug mask");
105 module_param(irqloops, uint, S_IRUGO | S_IWUSR);
106 MODULE_PARM_DESC(irqloops, "Speedfax maximal irqloops (default 4)");
108 IOFUNC_IND(ISAC, sfax_hw, p_isac)
109 IOFUNC_IND(ISAR, sfax_hw, p_isar)
112 speedfax_irq(int intno, void *dev_id)
114 struct sfax_hw *sf = dev_id;
118 spin_lock(&sf->lock);
119 val = inb(sf->cfg + TIGER_AUX_STATUS);
120 if (val & SFAX_TIGER_IRQ_BIT) { /* for us or shared ? */
121 spin_unlock(&sf->lock);
122 return IRQ_NONE; /* shared */
125 val = ReadISAR_IND(sf, ISAR_IRQBIT);
127 if (val & ISAR_IRQSTA)
128 mISDNisar_irq(&sf->isar);
129 val = ReadISAC_IND(sf, ISAC_ISTA);
131 mISDNisac_irq(&sf->isac, val);
132 val = ReadISAR_IND(sf, ISAR_IRQBIT);
133 if ((val & ISAR_IRQSTA) && cnt--)
136 pr_debug("%s: %d irqloops cpu%d\n", sf->name,
137 irqloops - cnt, smp_processor_id());
138 if (irqloops && !cnt)
139 pr_notice("%s: %d IRQ LOOP cpu%d\n", sf->name,
140 irqloops, smp_processor_id());
141 spin_unlock(&sf->lock);
146 enable_hwirq(struct sfax_hw *sf)
148 WriteISAC_IND(sf, ISAC_MASK, 0);
149 WriteISAR_IND(sf, ISAR_IRQBIT, ISAR_IRQMSK);
150 outb(SFAX_TIGER_IRQ_BIT, sf->cfg + TIGER_AUX_IRQMASK);
154 disable_hwirq(struct sfax_hw *sf)
156 WriteISAC_IND(sf, ISAC_MASK, 0xFF);
157 WriteISAR_IND(sf, ISAR_IRQBIT, 0);
158 outb(0, sf->cfg + TIGER_AUX_IRQMASK);
162 reset_speedfax(struct sfax_hw *sf)
165 pr_debug("%s: resetting card\n", sf->name);
166 outb(TIGER_EXTERN_RESET_ON, sf->cfg + TIGER_RESET_ADDR);
167 outb(SFAX_PCI_RESET_ON, sf->cfg + TIGER_AUX_DATA);
169 outb(TIGER_EXTERN_RESET_OFF, sf->cfg + TIGER_RESET_ADDR);
170 sf->aux_data = SFAX_PCI_RESET_OFF;
171 outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
176 sfax_ctrl(struct sfax_hw *sf, u32 cmd, u_long arg)
184 case HW_ACTIVATE_IND:
186 sf->aux_data &= ~SFAX_LED1_BIT;
188 sf->aux_data &= ~SFAX_LED2_BIT;
189 outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
193 sf->aux_data |= SFAX_LED1_BIT;
195 sf->aux_data |= SFAX_LED2_BIT;
196 outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
199 pr_info("%s: %s unknown command %x %lx\n",
200 sf->name, __func__, cmd, arg);
208 channel_ctrl(struct sfax_hw *sf, struct mISDN_ctrl_req *cq)
213 case MISDN_CTRL_GETOP:
214 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_L1_TIMER3;
216 case MISDN_CTRL_LOOP:
217 /* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */
218 if (cq->channel < 0 || cq->channel > 3) {
222 ret = sf->isac.ctrl(&sf->isac, HW_TESTLOOP, cq->channel);
224 case MISDN_CTRL_L1_TIMER3:
225 ret = sf->isac.ctrl(&sf->isac, HW_TIMER3_VALUE, cq->p1);
228 pr_info("%s: unknown Op %x\n", sf->name, cq->op);
236 sfax_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
238 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
239 struct dchannel *dch = container_of(dev, struct dchannel, dev);
240 struct sfax_hw *sf = dch->hw;
241 struct channel_req *rq;
244 pr_debug("%s: cmd:%x %p\n", sf->name, cmd, arg);
248 if (rq->protocol == ISDN_P_TE_S0)
249 err = sf->isac.open(&sf->isac, rq);
251 err = sf->isar.open(&sf->isar, rq);
254 if (!try_module_get(THIS_MODULE))
255 pr_info("%s: cannot get module\n", sf->name);
258 pr_debug("%s: dev(%d) close from %p\n", sf->name,
259 dch->dev.id, __builtin_return_address(0));
260 module_put(THIS_MODULE);
262 case CONTROL_CHANNEL:
263 err = channel_ctrl(sf, arg);
266 pr_debug("%s: unknown command %x\n", sf->name, cmd);
273 init_card(struct sfax_hw *sf)
278 ret = request_irq(sf->irq, speedfax_irq, IRQF_SHARED, sf->name, sf);
280 pr_info("%s: couldn't get interrupt %d\n", sf->name, sf->irq);
284 spin_lock_irqsave(&sf->lock, flags);
285 ret = sf->isac.init(&sf->isac);
287 spin_unlock_irqrestore(&sf->lock, flags);
288 pr_info("%s: ISAC init failed with %d\n",
293 /* RESET Receiver and Transmitter */
294 WriteISAC_IND(sf, ISAC_CMDR, 0x41);
295 spin_unlock_irqrestore(&sf->lock, flags);
296 msleep_interruptible(10);
297 if (debug & DEBUG_HW)
298 pr_notice("%s: IRQ %d count %d\n", sf->name,
299 sf->irq, sf->irqcnt);
301 pr_info("%s: IRQ(%d) got no requests during init %d\n",
302 sf->name, sf->irq, 3 - cnt);
306 free_irq(sf->irq, sf);
312 setup_speedfax(struct sfax_hw *sf)
316 if (!request_region(sf->cfg, 256, sf->name)) {
317 pr_info("mISDN: %s config port %x-%x already in use\n",
318 sf->name, sf->cfg, sf->cfg + 255);
323 outb(0xdd, sf->cfg + TIGER_AUX_CTRL);
324 outb(0, sf->cfg + TIGER_AUX_IRQMASK);
326 sf->isac.type = IPAC_TYPE_ISAC;
327 sf->p_isac.ale = sf->cfg + SFAX_PCI_ADDR;
328 sf->p_isac.port = sf->cfg + SFAX_PCI_ISAC;
329 sf->p_isar.ale = sf->cfg + SFAX_PCI_ADDR;
330 sf->p_isar.port = sf->cfg + SFAX_PCI_ISAR;
331 ASSIGN_FUNC(IND, ISAC, sf->isac);
332 ASSIGN_FUNC(IND, ISAR, sf->isar);
333 spin_lock_irqsave(&sf->lock, flags);
336 spin_unlock_irqrestore(&sf->lock, flags);
341 release_card(struct sfax_hw *card) {
344 spin_lock_irqsave(&card->lock, flags);
346 spin_unlock_irqrestore(&card->lock, flags);
347 card->isac.release(&card->isac);
348 free_irq(card->irq, card);
349 card->isar.release(&card->isar);
350 mISDN_unregister_device(&card->isac.dch.dev);
351 release_region(card->cfg, 256);
352 pci_disable_device(card->pdev);
353 pci_set_drvdata(card->pdev, NULL);
354 write_lock_irqsave(&card_lock, flags);
355 list_del(&card->list);
356 write_unlock_irqrestore(&card_lock, flags);
362 setup_instance(struct sfax_hw *card)
364 const struct firmware *firmware;
368 snprintf(card->name, MISDN_MAX_IDLEN - 1, "Speedfax.%d", sfax_cnt + 1);
369 write_lock_irqsave(&card_lock, flags);
370 list_add_tail(&card->list, &Cards);
371 write_unlock_irqrestore(&card_lock, flags);
373 spin_lock_init(&card->lock);
374 card->isac.hwlock = &card->lock;
375 card->isar.hwlock = &card->lock;
376 card->isar.ctrl = (void *)&sfax_ctrl;
377 card->isac.name = card->name;
378 card->isar.name = card->name;
379 card->isar.owner = THIS_MODULE;
381 err = reject_firmware(&firmware, "/*(DEBLOBBED)*/", &card->pdev->dev);
383 pr_info("%s: firmware request failed %d\n",
387 if (debug & DEBUG_HW)
388 pr_notice("%s: got firmware %zu bytes\n",
389 card->name, firmware->size);
391 mISDNisac_init(&card->isac, card);
393 card->isac.dch.dev.D.ctrl = sfax_dctrl;
394 card->isac.dch.dev.Bprotocols =
395 mISDNisar_init(&card->isar, card);
396 for (i = 0; i < 2; i++) {
397 set_channelmap(i + 1, card->isac.dch.dev.channelmap);
398 list_add(&card->isar.ch[i].bch.ch.list,
399 &card->isac.dch.dev.bchannels);
402 err = setup_speedfax(card);
405 err = card->isar.init(&card->isar);
408 err = mISDN_register_device(&card->isac.dch.dev,
409 &card->pdev->dev, card->name);
412 err = init_card(card);
415 err = card->isar.firmware(&card->isar, firmware->data, firmware->size);
417 release_firmware(firmware);
419 pr_notice("SpeedFax %d cards installed\n", sfax_cnt);
423 free_irq(card->irq, card);
425 mISDN_unregister_device(&card->isac.dch.dev);
427 release_region(card->cfg, 256);
429 card->isac.release(&card->isac);
430 card->isar.release(&card->isar);
431 release_firmware(firmware);
433 pci_disable_device(card->pdev);
434 write_lock_irqsave(&card_lock, flags);
435 list_del(&card->list);
436 write_unlock_irqrestore(&card_lock, flags);
442 sfaxpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
445 struct sfax_hw *card = kzalloc(sizeof(struct sfax_hw), GFP_KERNEL);
448 pr_info("No memory for Speedfax+ PCI\n");
452 err = pci_enable_device(pdev);
458 pr_notice("mISDN: Speedfax found adapter %s at %s\n",
459 (char *)ent->driver_data, pci_name(pdev));
461 card->cfg = pci_resource_start(pdev, 0);
462 card->irq = pdev->irq;
463 pci_set_drvdata(pdev, card);
464 err = setup_instance(card);
466 pci_set_drvdata(pdev, NULL);
471 sfax_remove_pci(struct pci_dev *pdev)
473 struct sfax_hw *card = pci_get_drvdata(pdev);
478 pr_debug("%s: drvdata already removed\n", __func__);
481 static struct pci_device_id sfaxpci_ids[] = {
482 { PCI_VENDOR_ID_TIGERJET, PCI_DEVICE_ID_TIGERJET_100,
483 PCI_SUBVENDOR_SPEEDFAX_PYRAMID, PCI_SUB_ID_SEDLBAUER,
484 0, 0, (unsigned long) "Pyramid Speedfax + PCI"
486 { PCI_VENDOR_ID_TIGERJET, PCI_DEVICE_ID_TIGERJET_100,
487 PCI_SUBVENDOR_SPEEDFAX_PCI, PCI_SUB_ID_SEDLBAUER,
488 0, 0, (unsigned long) "Sedlbauer Speedfax + PCI"
492 MODULE_DEVICE_TABLE(pci, sfaxpci_ids);
494 static struct pci_driver sfaxpci_driver = {
495 .name = "speedfax+ pci",
496 .probe = sfaxpci_probe,
497 .remove = sfax_remove_pci,
498 .id_table = sfaxpci_ids,
506 pr_notice("Sedlbauer Speedfax+ Driver Rev. %s\n",
508 err = pci_register_driver(&sfaxpci_driver);
513 Speedfax_cleanup(void)
515 pci_unregister_driver(&sfaxpci_driver);
518 module_init(Speedfax_init);
519 module_exit(Speedfax_cleanup);