Debian Codegraph @

drivers/dahdi/wcb4xxp/base.c

raw symbols SCIP
1 /*
2 * WCB410P Quad-BRI PCI Driver
3 * Written by Andrew Kohlsmith <[email protected]>
4 *
5 * Copyright (C) 2009-2012 Digium, Inc.
6 * All rights reserved.
7 *
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 */
22
23 #include <linux/init.h>
24
25 #include <linux/kernel.h> /* printk() */
26 #include <linux/errno.h> /* error codes */
27 #include <linux/module.h>
28 #include <linux/types.h> /* size_t */
29 #include <linux/fcntl.h> /* O_ACCMODE */
30 #include <linux/fs.h>
31 #include <linux/cdev.h>
32 #include <linux/pci.h> /* for PCI structures */
33 #include <linux/delay.h>
34 #include <asm/io.h>
35 #include <linux/spinlock.h>
36 #include <linux/device.h> /* dev_err() */
37 #include <linux/interrupt.h>
38 #include <asm/uaccess.h> /* copy_*_user */
39 #include <linux/workqueue.h> /* work_struct */
40 #include <linux/timer.h> /* timer_struct */
41 #include <linux/moduleparam.h>
42 #include <linux/proc_fs.h>
43 #include <linux/slab.h>
44 #include <linux/ctype.h>
45
46 #include <dahdi/kernel.h>
47
48 #include "wcb4xxp.h"
49
50 #ifndef BIT /* added in 2.6.24 */
51 #define BIT(i) (1UL << (i))
52 #endif
53 #define BIT_SET(x, i) ((x) |= BIT(i))
54 #define BIT_CLR(x, i) ((x) &= ~BIT(i))
55 #define IS_SET(x, i) (((x) & BIT(i)) != 0)
56 #define BITMASK(i) (((u64)1 << (i)) - 1)
57
58
59 #if (DAHDI_CHUNKSIZE != 8)
60 #error Sorry, wcb4xxp does not support chunksize != 8
61 #endif
62
63 //#define SIMPLE_BCHAN_FIFO
64 //#define DEBUG_LOWLEVEL_REGS /* debug __pci_in/out, not b4xxp_setreg */
65
66 #define DEBUG_GENERAL (1 << 0) /* general debug messages */
67 #define DEBUG_DTMF (1 << 1) /* emit DTMF detector messages */
68 #define DEBUG_REGS (1 << 2) /* emit register read/write, but only if the kernel's DEBUG is defined */
69 #define DEBUG_FOPS (1 << 3) /* emit file operation messages */
70 #define DEBUG_ECHOCAN (1 << 4)
71 #define DEBUG_ST_STATE (1 << 5) /* S/T state machine */
72 #define DEBUG_HDLC (1 << 6) /* HDLC controller */
73 #define DEBUG_ALARM (1 << 7) /* alarm changes */
74
75 #define DBG (debug & DEBUG_GENERAL)
76 #define DBG_DTMF (debug & DEBUG_DTMF)
77 #define DBG_REGS (debug & DEBUG_REGS)
78 #define DBG_FOPS (debug & DEBUG_FOPS)
79 #define DBG_EC (debug & DEBUG_ECHOCAN)
80 #define DBG_ST (debug & DEBUG_ST_STATE)
81 #define DBG_HDLC (debug & DEBUG_HDLC)
82 #define DBG_ALARM (debug & DEBUG_ALARM)
83
84 #define DBG_SPANFILTER (BIT(bspan->port) & spanfilter)
85
86 static int debug = 0;
87 static int spanfilter = 0xFF; /* Bitmap for ports 1-8 */
88 #ifdef LOOPBACK_SUPPORTED
89 static int loopback = 0;
90 #endif
91 static int milliwatt = 0;
92 static int pedanticpci = 0;
93 static int teignorered = 0;
94 static int alarmdebounce = 500;
95 static int persistentlayer1 = 1;
96 static int vpmsupport = 1;
97 static int timer_1_ms = 2000;
98 static int timer_3_ms = 30000;
99 static char *companding = "alaw";
100
101 #if !defined(mmiowb)
102 #define mmiowb() barrier()
103 #endif
104
105 #define MAX_B4_CARDS 64
106 static struct b4xxp *cards[MAX_B4_CARDS];
107
108 static int led_fader_table[] = {
109 0, 0, 0, 1, 2, 3, 4, 6, 8, 9, 11, 13, 16, 18, 20, 22, 24,
110 25, 27, 28, 29, 30, 31, 31, 32, 31, 31, 30, 29, 28, 27, 25, 23, 22,
111 20, 18, 16, 13, 11, 9, 8, 6, 4, 3, 2, 1, 0, 0,
112 };
113
114 #undef CREATE_WCB4XXP_PROCFS_ENTRY
115 #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
116 #define PROCFS_NAME "wcb4xxp"
117 static struct proc_dir_entry *myproc;
118 #endif
119
120 /* Expansion; right now there's just one card and all of its idiosyncrasies. */
121
122 #define FLAG_yyy (1 << 0)
123 #define FLAG_zzz (1 << 1)
124
125 struct devtype {
126 char *desc;
127 unsigned int flags;
128 int ports; /* Number of ports the card has */
129 enum cards_ids card_type; /* Card type - Digium B410P, ... */
130 };
131
132 static struct devtype wcb41xp = {"Wildcard B410P", .ports = 4,
133 .card_type = B410P};
134 static struct devtype wcb43xp = {"Wildcard B430P", .ports = 4,
135 .card_type = B430P};
136 static struct devtype wcb23xp = {"Wildcard B230P", .ports = 2,
137 .card_type = B230P};
138
139 #define IS_B430P(card) ((card)->card_type == B430P)
140 #define IS_B230P(card) ((card)->card_type == B230P)
141 #define IS_GEN2(card) (IS_B430P(card) || IS_B230P(card))
142 #define IS_B410P(card) ((card)->card_type == B410P)
143 #define CARD_HAS_EC(card) (IS_B410P(card) || IS_B430P(card) || IS_B230P(card))
144
145 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
146 static void b4xxp_update_leds(struct b4xxp *b4);
147
148 static const struct dahdi_echocan_features my_ec_features = {
149 .NLP_automatic = 1,
150 .CED_tx_detect = 1,
151 .CED_rx_detect = 1,
152 };
153
154 static const struct dahdi_echocan_ops my_ec_ops = {
155 .echocan_free = echocan_free,
156 };
157
158 /*
159 * lowlevel PCI access functions
160 * These are simply wrappers for the normal PCI access functions that the kernel provides,
161 * except that they allow us to work around specific PCI quirks with module options.
162 * Currently the only option supported is pedanticpci, which injects a (min.) 3us delay
163 * after any PCI access to forcibly disable fast back-to-back transactions.
164 * In the case of a PCI write, pedanticpci will also read from the status register, which
165 * has the effect of flushing any pending PCI writes.
166 */
167
168 static inline unsigned char __pci_in8(struct b4xxp *b4, const unsigned int reg)
169 {
170 unsigned char ret = ioread8(b4->addr + reg);
171
172 #ifdef DEBUG_LOWLEVEL_REGS
173 if (unlikely(DBG_REGS)) {
174 drv_dbg(&b4->pdev->dev,
175 "read 0x%02x from 0x%p\n", ret, b4->addr + reg);
176 }
177 #endif
178 if (unlikely(pedanticpci)) {
179 udelay(3);
180 }
181
182 return ret;
183 }
184
185 static inline unsigned short __pci_in16(struct b4xxp *b4, const unsigned int reg)
186 {
187 unsigned short ret = ioread16(b4->addr + reg);
188
189 #ifdef DEBUG_LOWLEVEL_REGS
190 if (unlikely(DBG_REGS)) {
191 drv_dbg(&b4->pdev->dev,
192 "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
193 }
194 #endif
195 if (unlikely(pedanticpci)) {
196 udelay(3);
197 }
198
199 return ret;
200 }
201
202 static inline unsigned int __pci_in32(struct b4xxp *b4, const unsigned int reg)
203 {
204 unsigned int ret = ioread32(b4->addr + reg);
205
206 #ifdef DEBUG_LOWLEVEL_REGS
207 if (unlikely(DBG_REGS)) {
208 drv_dbg(&b4->pdev->dev,
209 "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
210 }
211 #endif
212 if (unlikely(pedanticpci)) {
213 udelay(3);
214 }
215
216 return ret;
217 }
218
219 static inline void __pci_out32(struct b4xxp *b4, const unsigned int reg, const unsigned int val)
220 {
221 #ifdef DEBUG_LOWLEVEL_REGS
222 if (unlikely(DBG_REGS)) {
223 drv_dbg(&b4->pdev->dev,
224 "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
225 }
226 #endif
227 iowrite32(val, b4->addr + reg);
228
229 if (unlikely(pedanticpci)) {
230 udelay(3);
231 (void)ioread8(b4->addr + R_STATUS);
232 }
233 }
234
235 static inline void __pci_out8(struct b4xxp *b4, const unsigned int reg, const unsigned char val)
236 {
237 #ifdef DEBUG_LOWLEVEL_REGS
238 if (unlikely(DBG_REGS)) {
239 drv_dbg(&b4->pdev->dev,
240 "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
241 }
242 #endif
243 iowrite8(val, b4->addr + reg);
244
245 if (unlikely(pedanticpci)) {
246 udelay(3);
247 (void)ioread8(b4->addr + R_STATUS);
248 }
249 }
250
251
252 /*
253 * Standard I/O access functions
254 * uses spinlocks to protect against multiple I/O accesses
255 * DOES NOT automatically memory barrier
256 */
257 static inline unsigned char b4xxp_getreg8(struct b4xxp *b4, const unsigned int reg)
258 {
259 unsigned int ret;
260 unsigned long irq_flags;
261
262 spin_lock_irqsave(&b4->reglock, irq_flags);
263 #undef RETRY_REGISTER_READS
264 #ifdef RETRY_REGISTER_READS
265 switch (reg) {
266 case A_Z1:
267 case A_Z1H:
268 case A_F2:
269 case R_IRQ_OVIEW:
270 case R_BERT_STA:
271 case A_ST_RD_STA:
272 case R_IRQ_FIFO_BL0:
273 case A_Z2:
274 case A_Z2H:
275 case A_F1:
276 case R_RAM_USE:
277 case R_F0_CNTL:
278 case A_ST_SQ_RD:
279 case R_IRQ_FIFO_BL7:
280 /* On pg 53 of the data sheet for the hfc, it states that we must
281 * retry certain registers until we get two consecutive reads that are
282 * the same. */
283 retry:
284 ret = __pci_in8(b4, reg);
285 if (ret != __pci_in8(b4, reg))
286 goto retry;
287 break;
288 default:
289 #endif
290 ret = __pci_in8(b4, reg);
291 #ifdef RETRY_REGISTER_READS
292 break;
293 }
294 #endif
295 spin_unlock_irqrestore(&b4->reglock, irq_flags);
296
297 #ifndef DEBUG_LOWLEVEL_REGS
298 if (unlikely(DBG_REGS)) {
299 dev_dbg(&b4->pdev->dev,
300 "read 0x%02x from 0x%p\n", ret, b4->addr + reg);
301 }
302 #endif
303 return ret;
304 }
305
306 static inline unsigned int b4xxp_getreg32(struct b4xxp *b4, const unsigned int reg)
307 {
308 unsigned int ret;
309 unsigned long irq_flags;
310
311 spin_lock_irqsave(&b4->reglock, irq_flags);
312 ret = __pci_in32(b4, reg);
313 spin_unlock_irqrestore(&b4->reglock, irq_flags);
314
315 #ifndef DEBUG_LOWLEVEL_REGS
316 if (unlikely(DBG_REGS)) {
317 dev_dbg(&b4->pdev->dev,
318 "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
319 }
320 #endif
321 return ret;
322 }
323
324 static inline unsigned short b4xxp_getreg16(struct b4xxp *b4, const unsigned int reg)
325 {
326 unsigned int ret;
327 unsigned long irq_flags;
328
329 spin_lock_irqsave(&b4->reglock, irq_flags);
330 ret = __pci_in16(b4, reg);
331 spin_unlock_irqrestore(&b4->reglock, irq_flags);
332
333 #ifndef DEBUG_LOWLEVEL_REGS
334 if (unlikely(DBG_REGS)) {
335 dev_dbg(&b4->pdev->dev,
336 "read 0x%04x from 0x%p\n", ret, b4->addr + reg);
337 }
338 #endif
339 return ret;
340 }
341
342 static inline void b4xxp_setreg32(struct b4xxp *b4, const unsigned int reg, const unsigned int val)
343 {
344 unsigned long irq_flags;
345
346 #ifndef DEBUG_LOWLEVEL_REGS
347 if (unlikely(DBG_REGS)) {
348 dev_dbg(&b4->pdev->dev,
349 "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
350 }
351 #endif
352 spin_lock_irqsave(&b4->reglock, irq_flags);
353 __pci_out32(b4, reg, val);
354 spin_unlock_irqrestore(&b4->reglock, irq_flags);
355 }
356
357 static inline void b4xxp_setreg8(struct b4xxp *b4, const unsigned int reg, const unsigned char val)
358 {
359 unsigned long irq_flags;
360
361 #ifndef DEBUG_LOWLEVEL_REGS
362 if (unlikely(DBG_REGS)) {
363 dev_dbg(&b4->pdev->dev,
364 "writing 0x%02x to 0x%p\n", val, b4->addr + reg);
365 }
366 #endif
367 spin_lock_irqsave(&b4->reglock, irq_flags);
368 __pci_out8(b4, reg, val);
369 spin_unlock_irqrestore(&b4->reglock, irq_flags);
370 }
371
372 /*
373 * A lot of the registers in the HFC are indexed.
374 * this function sets the index, and then writes to the indexed register in an ordered fashion.
375 * memory barriers are useless unless spinlocked, so that's what these wrapper functions do.
376 */
377 static void b4xxp_setreg_ra(struct b4xxp *b4, unsigned char r, unsigned char rd, unsigned char a, unsigned char ad)
378 {
379 unsigned long irq_flags;
380
381 spin_lock_irqsave(&b4->seqlock, irq_flags);
382
383 b4xxp_setreg8(b4, r, rd);
384 wmb();
385 b4xxp_setreg8(b4, a, ad);
386
387 mmiowb();
388 spin_unlock_irqrestore(&b4->seqlock, irq_flags);
389 }
390
391 static unsigned char b4xxp_getreg_ra(struct b4xxp *b4, unsigned char r, unsigned char rd, unsigned char a)
392 {
393 unsigned long irq_flags;
394 unsigned char val;
395
396 spin_lock_irqsave(&b4->seqlock, irq_flags);
397
398 b4xxp_setreg8(b4, r, rd);
399 wmb();
400 val = b4xxp_getreg8(b4, a);
401
402 mmiowb();
403 spin_unlock_irqrestore(&b4->seqlock, irq_flags);
404 return val;
405 }
406
407 /* This gpio register set wrapper protects bit 0 on b430 devices from being
408 * cleared, as it's used for reset */
409 static void hfc_gpio_set(struct b4xxp *b4, unsigned char bits)
410 {
411 if (IS_GEN2(b4)) {
412 b4xxp_setreg8(b4, R_GPIO_OUT1, bits | 0x01);
413 flush_pci();
414 } else {
415 b4xxp_setreg8(b4, R_GPIO_OUT1, bits);
416 }
417 }
418
419 /*
420 * HFC-4S GPIO routines
421 *
422 * the B410P uses the HFC-4S GPIO as follows:
423 * GPIO 8..10: output, CPLD register select
424 * GPIO12..15: output, 1 = enable power for port 1-4
425 * GPI16: input, 0 = echo can #1 interrupt
426 * GPI17: input, 0 = echo can #2 interrupt
427 * GPI23: input, 1 = NT power module installed
428 * GPI24..27: input, NT power module problem on port 1-4
429 * GPI28..31: input, 1 = port 1-4 in NT mode
430 */
431
432 /* initialize HFC-4S GPIO. Set up pin drivers before setting GPIO mode */
433 static void hfc_gpio_init(struct b4xxp *b4)
434 {
435 unsigned long irq_flags;
436
437 spin_lock_irqsave(&b4->seqlock, irq_flags);
438
439 flush_pci();
440 mb();
441
442 /* GPIO0..7 input */
443 b4xxp_setreg8(b4, R_GPIO_EN0, 0x00);
444
445 if (IS_GEN2(b4))
446 b4xxp_setreg8(b4, R_GPIO_EN1, 0xf1);
447 else
448 b4xxp_setreg8(b4, R_GPIO_EN1, 0xf7);
449
450 hfc_gpio_set(b4, 0x00);
451
452 mb();
453
454 switch (b4->card_type) {
455 case OCTOBRI: /* fall through */
456 case B800P_OV: /* fall through */
457 case BN8S0:
458 /* GPIO0..15 S/T - HFC-8S uses GPIO8-15 for S/T ports 5-8 */
459 b4xxp_setreg8(b4, R_GPIO_SEL, 0x00);
460 break;
461 default:
462 /* GPIO0..7 S/T, 8..15 GPIO */
463 b4xxp_setreg8(b4, R_GPIO_SEL, 0xf0);
464 break;
465 }
466
467 mb();
468
469 spin_unlock_irqrestore(&b4->seqlock, irq_flags);
470 }
471
472 /*
473 * HFC SRAM interface code.
474 * This came from mattf, I don't even pretend to understand it,
475 * It seems to be using undocumented features in the HFC.
476 * I just added the __pci_in8() to ensure the PCI writes made it
477 * to hardware by the time these functions return.
478 */
479 static inline void enablepcibridge(struct b4xxp *b4)
480 {
481 b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x03);
482 flush_pci();
483 }
484
485 static inline void disablepcibridge(struct b4xxp *b4)
486 {
487 b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x02);
488 flush_pci();
489 }
490
491 /* NOTE: read/writepcibridge do not use __pci_in/out because they are using b4->ioaddr not b4->addr */
492 static inline unsigned char readpcibridge(struct b4xxp *b4, unsigned char address)
493 {
494 unsigned short cipv;
495 unsigned char data;
496
497
498 /* slow down a PCI read access by 1 PCI clock cycle */
499 b4xxp_setreg8(b4, R_CTRL, 0x4);
500 wmb();
501
502 if (address == 0)
503 cipv=0x4000;
504 else
505 cipv=0x5800;
506
507 /* select local bridge port address by writing to CIP port */
508 iowrite16(cipv, b4->ioaddr + 4);
509 wmb();
510 data = ioread8(b4->ioaddr);
511
512 /* restore R_CTRL for normal PCI read cycle speed */
513 b4xxp_setreg8(b4, R_CTRL, 0x0);
514 wmb();
515 flush_pci();
516
517 return data;
518 }
519
520 static inline void writepcibridge(struct b4xxp *b4, unsigned char address, unsigned char data)
521 {
522 unsigned short cipv;
523 unsigned int datav;
524
525
526 if (address == 0)
527 cipv=0x4000;
528 else
529 cipv=0x5800;
530
531 /* select local bridge port address by writing to CIP port */
532 iowrite16(cipv, b4->ioaddr + 4);
533 wmb();
534
535 /* define a 32 bit dword with 4 identical bytes for write sequence */
536 datav = data | ( (__u32) data <<8) | ( (__u32) data <<16) | ( (__u32) data <<24);
537
538 /*
539 * write this 32 bit dword to the bridge data port
540 * this will initiate a write sequence of up to 4 writes to the same address on the local bus
541 * interface
542 * the number of write accesses is undefined but >=1 and depends on the next PCI transaction
543 * during write sequence on the local bus
544 */
545 iowrite32(datav, b4->ioaddr);
546 wmb();
547 flush_pci();
548 }
549
550 /* CPLD access code, more or less copied verbatim from code provided by mattf. */
551 static inline void cpld_select_reg(struct b4xxp *b4, unsigned char reg)
552 {
553 hfc_gpio_set(b4, reg);
554 flush_pci();
555 }
556
557 static inline void cpld_setreg(struct b4xxp *b4, unsigned char reg, unsigned char val)
558 {
559 cpld_select_reg(b4, reg);
560
561 enablepcibridge(b4);
562 writepcibridge(b4, 1, val);
563 disablepcibridge(b4);
564 }
565
566 static inline unsigned char cpld_getreg(struct b4xxp *b4, unsigned char reg)
567 {
568 unsigned char data;
569
570 cpld_select_reg(b4, reg);
571
572 enablepcibridge(b4);
573 data = readpcibridge(b4, 1);
574 disablepcibridge(b4);
575
576 return data;
577 }
578
579
580 /*
581 * echo canceller code, verbatim from mattf.
582 * I don't pretend to understand it.
583 */
584 static inline void ec_select_addr(struct b4xxp *b4, unsigned short addr)
585 {
586 cpld_setreg(b4, 0, 0xff & addr);
587 cpld_setreg(b4, 1, 0x01 & (addr >> 8));
588 }
589
590 static inline unsigned short ec_read_data(struct b4xxp *b4)
591 {
592 unsigned short addr;
593 unsigned short highbit;
594
595 addr = cpld_getreg(b4, 0);
596 highbit = cpld_getreg(b4, 1);
597
598 addr = addr | (highbit << 8);
599
600 return addr & 0x1ff;
601 }
602
603 static unsigned char hfc_sram_read(struct b4xxp *b4)
604 {
605 unsigned char data;
606
607 enablepcibridge(b4);
608 data = readpcibridge(b4, 1);
609 disablepcibridge(b4);
610 cpld_select_reg(b4, 0);
611
612 return data;
613 }
614
615 static void hfc_sram_write(struct b4xxp *b4, char data)
616 {
617 enablepcibridge(b4);
618 writepcibridge(b4, 1, data);
619 cpld_select_reg(b4, 0);
620 disablepcibridge(b4);
621 }
622
623 static void set_dsp_address(struct b4xxp *b4, unsigned short addr, bool dir)
624 {
625 hfc_gpio_set(b4, 0x30);
626 hfc_sram_write(b4, 0xff & addr);
627 hfc_gpio_set(b4, 0x40);
628 /* dir = 1 write */
629 if (dir)
630 hfc_sram_write(b4, 0x03 & (addr >> 8));
631 else
632 hfc_sram_write(b4, 0x80 | (0x03 & (addr >> 8)));
633 }
634
635 /* Read from zarlink echocan without locks */
636 static unsigned char __zl_read(struct b4xxp *b4, unsigned short addr)
637 {
638 unsigned char data;
639
640 set_dsp_address(b4, addr, 0);
641 hfc_gpio_set(b4, ((addr & 0x0400) >> 3) | 0x50);
642 data = hfc_sram_read(b4);
643 hfc_gpio_set(b4, 0x00);
644
645 return data;
646 }
647
648 /* Write to zarlink echocan unlocked */
649 static void __zl_write(struct b4xxp *b4, unsigned short addr,
650 unsigned char data)
651 {
652 unsigned char val;
653
654 hfc_gpio_set(b4, 0x00);
655 set_dsp_address(b4, addr, 1);
656 hfc_sram_write(b4, data);
657 val = ((addr & 0x0400) >> 3);
658 val = (val | 0x50);
659 hfc_gpio_set(b4, val);
660 hfc_gpio_set(b4, 0x00);
661 }
662
663 /* Write to zarlink echocan locked */
664 static void zl_write(struct b4xxp *b4, unsigned short addr, unsigned char data)
665 {
666 unsigned long flags;
667
668 spin_lock_irqsave(&b4->seqlock, flags);
669 __zl_write(b4, addr, data);
670 spin_unlock_irqrestore(&b4->seqlock, flags);
671 }
672
673 static inline unsigned char ec_read(struct b4xxp *b4, int which, unsigned short addr)
674 {
675 unsigned char data;
676 unsigned long flags;
677
678 spin_lock_irqsave(&b4->seqlock, flags);
679 ec_select_addr(b4, addr);
680
681 if (!which)
682 cpld_select_reg(b4, 2);
683 else
684 cpld_select_reg(b4, 3);
685
686 enablepcibridge(b4);
687 data = readpcibridge(b4, 1);
688 disablepcibridge(b4);
689
690 cpld_select_reg(b4, 0);
691 spin_unlock_irqrestore(&b4->seqlock, flags);
692
693 return data;
694 }
695
696 static inline void ec_write(struct b4xxp *b4, int which, unsigned short addr, unsigned char data)
697 {
698 unsigned char in;
699 unsigned long flags;
700
701 spin_lock_irqsave(&b4->seqlock, flags);
702
703 ec_select_addr(b4, addr);
704
705 enablepcibridge(b4);
706
707 if (!which)
708 cpld_select_reg(b4, 2);
709 else
710 cpld_select_reg(b4, 3);
711
712 writepcibridge(b4, 1, data);
713 cpld_select_reg(b4, 0);
714 disablepcibridge(b4);
715
716 spin_unlock_irqrestore(&b4->seqlock, flags);
717
718 in = ec_read(b4, which, addr);
719
720 if ((in != data) && printk_ratelimit()) {
721 dev_warn(&b4->pdev->dev,
722 "ec_write: Wrote 0x%02x to register 0x%02x "
723 "of VPM %d but got back 0x%02x\n",
724 data, addr, which, in);
725 }
726 }
727
728 #define NUM_EC 2
729 #define MAX_TDM_CHAN 32
730
731 static void zl_init(struct b4xxp *b4)
732 {
733 int i, offset;
734 int group_addr[4] = {0x00, 0x40, 0x80, 0xc0};
735
736 if (!b4->shutdown)
737 dev_info(&b4->pdev->dev, "Initializing Zarlink echocan\n");
738
739 /* There are 4 "groups" of echocans with two channels in each */
740 /* Main group control reg 0-3 */
741
742 /* Hardware Reset Sequence */
743 b4xxp_setreg8(b4, R_GPIO_OUT1, 0x00);
744 udelay(100);
745 b4xxp_setreg8(b4, R_GPIO_OUT1, 0x01);
746 udelay(500);
747
748 /* Software reset sequence */
749 zl_write(b4, 0x400, 0x41);
750 zl_write(b4, 0x401, 0x01);
751 zl_write(b4, 0x402, 0x01);
752 zl_write(b4, 0x403, 0x01);
753 udelay(250);
754 zl_write(b4, 0x400, 0x40);
755 zl_write(b4, 0x401, 0x00);
756 zl_write(b4, 0x402, 0x00);
757 zl_write(b4, 0x403, 0x00);
758 udelay(500);
759
760 /* Power up & Configure echo can gruops */
761 if (!strcasecmp(companding, "alaw")) {
762 zl_write(b4, 0x400, 0x47);
763 zl_write(b4, 0x401, 0x07);
764 zl_write(b4, 0x402, 0x07);
765 zl_write(b4, 0x403, 0x07);
766 } else {
767 zl_write(b4, 0x400, 0x45);
768 zl_write(b4, 0x401, 0x05);
769 zl_write(b4, 0x402, 0x05);
770 zl_write(b4, 0x403, 0x05);
771 }
772 udelay(250);
773
774 for (i = 0; i <= 3; i++) {
775 int group = group_addr[i];
776 /* Control reg 1, bank A & B, channel bypass mode */
777 zl_write(b4, group + 0x00, 0x08);
778 /* Control reg 1 on bank B must be written twicer as */
779 /* per the datasheet */
780 zl_write(b4, group + 0x20, 0x0a);
781 zl_write(b4, group + 0x20, 0x0a);
782
783 /* Two channels of the echocan must be set separately,
784 one at offset 0x00 and one at offset 0x20 */
785 for (offset = group; offset <= (group + 0x20); offset += 0x20) {
786 /* Control reg 2 */
787 zl_write(b4, offset + 0x01, 0x00);
788 /* Flat Delay */
789 zl_write(b4, offset + 0x04, 0x00);
790 /* Decay Step Size Reg */
791 zl_write(b4, offset + 0x06, 0x04);
792 /* Decay Step Number */
793 zl_write(b4, offset + 0x07, 0x00);
794 /* Control reg 3 */
795 zl_write(b4, offset + 0x08, 0xfb);
796 /* Control reg 4 */
797 zl_write(b4, offset + 0x09, 0x54);
798 /* Noise Scaling */
799 zl_write(b4, offset + 0x0a, 0x16);
800 /* Noise Control */
801 zl_write(b4, offset + 0x0b, 0x45);
802 /* DTDT Reg 1*/
803 zl_write(b4, offset + 0x14, 0x00);
804 /* DTDT Reg 2*/
805 zl_write(b4, offset + 0x15, 0x48);
806 /* NLPTHR reg 1*/
807 zl_write(b4, offset + 0x18, 0xe0);
808 /* NLPTHR reg 2*/
809 zl_write(b4, offset + 0x19, 0x0c);
810 /* Step Size, MU reg 1*/
811 zl_write(b4, offset + 0x1a, 0x00);
812 /* Step Size, MU reg 2*/
813 zl_write(b4, offset + 0x1b, 0x40);
814 /* Gains reg 1*/
815 zl_write(b4, offset + 0x1c, 0x44);
816 /* Gains reg 2*/
817 zl_write(b4, offset + 0x1d, 0x44);
818 }
819 }
820 }
821
822 static void ec_init(struct b4xxp *b4)
823 {
824 unsigned char b;
825 unsigned int i, j, mask;
826
827 if (!CARD_HAS_EC(b4))
828 return;
829
830 /* Short circuit to the new zarlink echocan logic */
831 if (IS_GEN2(b4)) {
832 zl_init(b4);
833 return;
834 }
835
836 /* Setup GPIO */
837 for (i=0; i < NUM_EC; i++) {
838 b = ec_read(b4, i, 0x1a0);
839
840 dev_info(&b4->pdev->dev,
841 "VPM %d/%d init: chip ver %02x\n", i, NUM_EC - 1, b);
842
843 for (j=0; j < b4->numspans; j++) {
844 ec_write(b4, i, 0x1a8 + j, 0x00); /* GPIO out */
845 ec_write(b4, i, 0x1ac + j, 0x00); /* GPIO dir */
846 ec_write(b4, i, 0x1b0 + j, 0x00); /* GPIO sel */
847 }
848
849 /* Setup TDM path - sets fsync and tdm_clk as inputs */
850 b = ec_read(b4, i, 0x1a3); /* misc_con */
851 ec_write(b4, i, 0x1a3, b & ~0x02);
852
853 /* Setup Echo length (512 taps) */
854 ec_write(b4, i, 0x022, 1);
855 ec_write(b4, i, 0x023, 0xff);
856
857 /* Setup timeslots */
858 ec_write(b4, i, 0x02f, 0x00);
859 mask = 0x02020202 << (i * 4);
860
861 /* Setup the tdm channel masks for all chips*/
862 for (j=0; j < 4; j++)
863 ec_write(b4, i, 0x33 - j, (mask >> (j << 3)) & 0xff);
864
865 /* Setup convergence rate */
866 b = ec_read(b4, i, 0x20);
867 b &= 0xe0;
868 b |= 0x12;
869 if (!strcasecmp(companding, "alaw")) {
870 if (DBG)
871 dev_info(&b4->pdev->dev, "Setting alaw mode\n");
872 b |= 0x01;
873 } else {
874 if (DBG)
875 dev_info(&b4->pdev->dev, "Setting ulaw mode");
876 }
877
878 ec_write(b4, i, 0x20, b);
879 if (DBG)
880 dev_info(&b4->pdev->dev, "reg 0x20 is 0x%02x\n", b);
881
882 if (DBG) {
883 dev_info(&b4->pdev->dev,
884 "NLP threshold is set to %d (0x%02x)\n", b, b);
885 }
886
887 /* Initialize echo cans */
888 for (j=0; j < MAX_TDM_CHAN; j++) {
889 if (mask & (0x00000001 << j))
890 ec_write(b4, i, j, 0x00);
891 }
892
893 mdelay(10);
894
895 /* Put in bypass mode */
896 for (j=0; j < MAX_TDM_CHAN; j++) {
897 if (mask & (0x00000001 << j)) {
898 ec_write(b4, i, j, 0x01);
899 }
900 }
901
902 /* Enable bypass */
903 for (j=0; j < MAX_TDM_CHAN; j++) {
904 if (mask & (0x00000001 << j))
905 ec_write(b4, i, 0x78 + j, 0x01);
906 }
907 }
908 }
909
910 /* performs a register write and then waits for the HFC "busy" bit to clear */
911 static void hfc_setreg_waitbusy(struct b4xxp *b4, const unsigned int reg, const unsigned int val)
912 {
913 /* V_BUSY is not supposed to take longer than 1us */
914 /* Since this func can be called with interrupts locked
915 * we should just a regular loop, as jiffies may not update
916 */
917 int TIMEOUT = 0.002 * 3000000;
918 int x = 0;
919
920 b4xxp_setreg8(b4, reg, val);
921
922 while (b4xxp_getreg8(b4, R_STATUS) & V_BUSY) {
923 if (x++ > TIMEOUT) {
924 if (printk_ratelimit()) {
925 dev_info(&b4->pdev->dev,
926 "hfc_setreg_waitbusy(write 0x%02x to 0x%02x) timed out waiting for busy flag to clear!\n",
927 val, reg);
928 }
929 return;
930 }
931 }
932 }
933
934 /*
935 * reads an 8-bit register over over and over until the same value is read twice, then returns that value.
936 */
937 static inline unsigned char hfc_readcounter8(struct b4xxp *b4, const unsigned int reg)
938 {
939 unsigned char r1, r2;
940 unsigned long maxwait = 1048576;
941
942 do {
943 r1 = b4xxp_getreg8(b4, reg);
944 r2 = b4xxp_getreg8(b4, reg);
945 } while ((r1 != r2) && maxwait--);
946
947 if (!maxwait && printk_ratelimit()) {
948 dev_warn(&b4->pdev->dev,
949 "hfc_readcounter8(reg 0x%02x) timed out waiting "
950 "for data to settle!\n", reg);
951 }
952
953 return r1;
954 }
955
956 /*
957 * reads a 16-bit register over over and over until the same value is read twice, then returns that value.
958 */
959 static inline unsigned short hfc_readcounter16(struct b4xxp *b4, const unsigned int reg)
960 {
961 unsigned short r1, r2;
962 unsigned long maxwait = 1048576;
963
964 do {
965 r1 = b4xxp_getreg16(b4, reg);
966 r2 = b4xxp_getreg16(b4, reg);
967 } while ((r1 != r2) && maxwait--);
968
969 if (!maxwait && printk_ratelimit()) {
970 dev_warn(&b4->pdev->dev,
971 "hfc_readcounter16(reg 0x%02x) timed out waiting "
972 "for data to settle!\n", reg);
973 }
974
975 return r1;
976 }
977
978 static inline unsigned int hfc_readcounter32(struct b4xxp *b4, const unsigned int reg)
979 {
980 unsigned int r1, r2;
981 unsigned long maxwait = 1048576;
982
983 do {
984 r1 = b4xxp_getreg32(b4, reg);
985 r2 = b4xxp_getreg32(b4, reg);
986 } while ((r1 != r2) && maxwait--);
987
988 if (!maxwait && printk_ratelimit()) {
989 dev_warn(&b4->pdev->dev,
990 "hfc_readcounter32(reg 0x%02x) timed out waiting "
991 "for data to settle!\n", reg);
992 }
993
994 return r1;
995 }
996
997 /* performs a soft-reset of the HFC-4S. This is as clean-slate as you can get to a hardware reset. */
998 static void hfc_reset(struct b4xxp *b4)
999 {
1000 int b, c;
1001
1002 /* all 32 FIFOs the same size (384 bytes), channel select data flow mode, sized for internal RAM */
1003 b4xxp_setreg8(b4, R_FIFO_MD, V_FIFO_MD_00 | V_DF_MD_CSM | V_FIFO_SZ_00);
1004 flush_pci();
1005
1006 /* reset everything, wait 500us, then bring everything BUT the PCM system out of reset */
1007 b4xxp_setreg8(b4, R_CIRM, HFC_FULL_RESET);
1008 flush_pci();
1009
1010 udelay(500);
1011
1012 b4xxp_setreg8(b4, R_CIRM, V_PCM_RES);
1013 flush_pci();
1014
1015 udelay(500);
1016
1017 /*
1018 * Now bring PCM out of reset and do a very basic setup of the PCM system to allow it to finish resetting correctly.
1019 * set F0IO as an output, and set up a 32-timeslot PCM bus
1020 * See Section 8.3 in the HFC-4S datasheet for more details.
1021 */
1022 b4xxp_setreg8(b4, R_CIRM, 0x00);
1023 b4xxp_setreg8(b4, R_PCM_MD0, V_PCM_MD | V_PCM_IDX_MD1);
1024 flush_pci();
1025
1026 if (IS_GEN2(b4))
1027 b4xxp_setreg8(b4, R_PCM_MD1, V_PLL_ADJ_00 | V_PCM_DR_4096);
1028 else
1029 b4xxp_setreg8(b4, R_PCM_MD1, V_PLL_ADJ_00 | V_PCM_DR_2048);
1030
1031 flush_pci();
1032
1033 /* now wait for R_F0_CNTL to reach at least 2 before continuing */
1034 c=10;
1035 while ((b = b4xxp_getreg8(b4, R_F0_CNTL)) < 2 && c) { udelay(100); c--; }
1036
1037 if (!c && b < 2) {
1038 dev_warn(&b4->pdev->dev, "hfc_reset() did not get the green light from the PCM system!\n");
1039 }
1040 }
1041
1042 static inline void hfc_enable_fifo_irqs(struct b4xxp *b4)
1043 {
1044 b4xxp_setreg8(b4, R_IRQ_CTRL, V_FIFO_IRQ | V_GLOB_IRQ_EN);
1045 flush_pci();
1046 }
1047
1048 static inline void hfc_disable_fifo_irqs(struct b4xxp *b4)
1049 {
1050 b4xxp_setreg8(b4, R_IRQ_CTRL, V_GLOB_IRQ_EN);
1051 flush_pci();
1052 }
1053
1054 static void hfc_enable_interrupts(struct b4xxp *b4)
1055 {
1056 b4->running = 1;
1057
1058 /* clear any pending interrupts */
1059 b4xxp_getreg8(b4, R_STATUS);
1060 b4xxp_getreg8(b4, R_IRQ_MISC);
1061 b4xxp_getreg8(b4, R_IRQ_FIFO_BL0);
1062 b4xxp_getreg8(b4, R_IRQ_FIFO_BL1);
1063 b4xxp_getreg8(b4, R_IRQ_FIFO_BL2);
1064 b4xxp_getreg8(b4, R_IRQ_FIFO_BL3);
1065 b4xxp_getreg8(b4, R_IRQ_FIFO_BL4);
1066 b4xxp_getreg8(b4, R_IRQ_FIFO_BL5);
1067 b4xxp_getreg8(b4, R_IRQ_FIFO_BL6);
1068 b4xxp_getreg8(b4, R_IRQ_FIFO_BL7);
1069
1070 b4xxp_setreg8(b4, R_IRQMSK_MISC, V_TI_IRQ);
1071 hfc_enable_fifo_irqs(b4);
1072 }
1073
1074 static void hfc_disable_interrupts(struct b4xxp *b4)
1075 {
1076 b4xxp_setreg8(b4, R_IRQMSK_MISC, 0);
1077 b4xxp_setreg8(b4, R_IRQ_CTRL, 0);
1078 flush_pci();
1079 b4->running = 0;
1080 }
1081
1082 /*
1083 * Connects an S/T port's B channel to a host-facing FIFO through the PCM busses.
1084 * This bchan flow plan should match up with the EC requirements.
1085 * TODO: Interrupts are only enabled on the host FIFO RX side, since everything is (should be) synchronous.
1086 * *** performs no error checking of parameters ***
1087 */
1088 static void hfc_assign_bchan_fifo_ec(struct b4xxp *b4, int port, int bchan)
1089 {
1090 int fifo, hfc_chan, ts;
1091 unsigned long irq_flags;
1092 static int first=1;
1093
1094 if (first) {
1095 first = 0;
1096 dev_info(&b4->pdev->dev, "Hardware echo cancellation enabled.\n");
1097 }
1098
1099 fifo = port * 2;
1100 hfc_chan = port * 4;
1101 ts = port * 8;
1102
1103 if (bchan) {
1104 fifo += 1;
1105 hfc_chan += 1;
1106 ts += 4;
1107 }
1108
1109 /* record the host's FIFO # in the span fifo array */
1110 b4->spans[port].fifos[bchan] = fifo;
1111 spin_lock_irqsave(&b4->fifolock, irq_flags);
1112
1113 if (DBG) {
1114 dev_info(&b4->pdev->dev,
1115 "port %d, B channel %d\n\tS/T -> PCM ts %d uses HFC "
1116 "chan %d via FIFO %d\n",
1117 port, bchan, ts + 1, hfc_chan, 16 + fifo);
1118 }
1119
1120 /* S/T RX -> PCM TX FIFO, transparent mode, no IRQ. */
1121 hfc_setreg_waitbusy(b4, R_FIFO, ((16 + fifo) << V_FIFO_NUM_SHIFT));
1122 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_110);
1123 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
1124 b4xxp_setreg8(b4, R_SLOT, ((ts + 1) << V_SL_NUM_SHIFT));
1125 b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_TX_STIO1 | (hfc_chan << V_CH_SNUM_SHIFT));
1126 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1127
1128 if (DBG)
1129 pr_info("\tPCM ts %d -> host uses HFC chan %d via FIFO %d\n", ts + 1, 16 + hfc_chan, fifo);
1130
1131 /* PCM RX -> Host TX FIFO, transparent mode, enable IRQ. */
1132 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1133 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_001);
1134 b4xxp_setreg8(b4, A_CHANNEL, ((16 + hfc_chan) << V_CH_FNUM_SHIFT) | V_CH_FDIR);
1135 b4xxp_setreg8(b4, R_SLOT, ((ts + 1) << V_SL_NUM_SHIFT) | 1);
1136 b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_RX_STIO2 | ((16 + hfc_chan) << V_CH_SNUM_SHIFT) | 1);
1137 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1138 // b4xxp_setreg8(b4, A_IRQ_MSK, V_IRQ);
1139
1140 if (DBG)
1141 pr_info("\thost -> PCM ts %d uses HFC chan %d via FIFO %d\n", ts, 16 + hfc_chan, fifo);
1142
1143 /* Host FIFO -> PCM TX */
1144 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1145 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_001);
1146 b4xxp_setreg8(b4, A_CHANNEL, ((16 + hfc_chan) << V_CH_FNUM_SHIFT));
1147 b4xxp_setreg8(b4, R_SLOT, (ts << V_SL_NUM_SHIFT));
1148 b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_RX_STIO2 | ((16 + hfc_chan) << V_CH_SNUM_SHIFT));
1149 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1150
1151 if (DBG)
1152 pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n", ts, hfc_chan, 16 + fifo);
1153
1154 /* PCM -> S/T */
1155 hfc_setreg_waitbusy(b4, R_FIFO, ((16 + fifo) << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1156 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_110);
1157 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
1158 b4xxp_setreg8(b4, R_SLOT, (ts << V_SL_NUM_SHIFT) | V_SL_DIR);
1159 b4xxp_setreg8(b4, A_SL_CFG, V_ROUT_TX_STIO2 | (hfc_chan << V_CH_SNUM_SHIFT) | V_CH_SDIR);
1160 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1161
1162 if (DBG)
1163 pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n", ts, hfc_chan, 16 + fifo);
1164
1165 flush_pci(); /* ensure all those writes actually hit hardware */
1166 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1167 }
1168
1169 static void hfc_assign_fifo_zl(struct b4xxp *b4, int port, int bchan)
1170 {
1171 int fifo, hfc_chan, ts;
1172 unsigned long irq_flags;
1173 static int first = 1;
1174
1175 if (first) {
1176 first = 0;
1177 dev_info(&b4->pdev->dev, "Zarlink echo cancellation enabled.\n");
1178 }
1179
1180 fifo = port * 2;
1181 hfc_chan = port * 4;
1182 ts = port * 8;
1183
1184 if (bchan) {
1185 fifo += 1;
1186 hfc_chan += 1;
1187 ts += 4;
1188 }
1189
1190 /* record the host's FIFO # in the span fifo array */
1191 if (IS_B230P(b4))
1192 b4->spans[2-port].fifos[bchan] = fifo;
1193 else
1194 b4->spans[3-port].fifos[bchan] = fifo;
1195 spin_lock_irqsave(&b4->fifolock, irq_flags);
1196
1197 if (DBG) {
1198 dev_info(&b4->pdev->dev,
1199 "port %d, B channel %d\n\tS/T -> PCM ts %d uses HFC chan %d via FIFO %d\n",
1200 port, bchan, ts + 1, hfc_chan, 16 + fifo);
1201 }
1202
1203 /* S/T RX -> PCM TX FIFO, transparent mode, no IRQ. */
1204 hfc_setreg_waitbusy(b4, R_FIFO, ((16 + fifo) << V_FIFO_NUM_SHIFT));
1205 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_110);
1206 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
1207 b4xxp_setreg8(b4, R_SLOT, (((fifo * 2)+1) << V_SL_NUM_SHIFT));
1208 b4xxp_setreg8(b4,
1209 A_SL_CFG, V_ROUT_TX_STIO1 | (hfc_chan << V_CH_SNUM_SHIFT));
1210 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1211
1212 if (DBG) {
1213 pr_info("\tPCM ts %d -> host uses HFC chan %d via FIFO %d\n",
1214 ts + 1, 16 + hfc_chan, fifo);
1215 }
1216
1217 /* PCM RX -> Host TX FIFO, transparent mode, enable IRQ. */
1218 hfc_setreg_waitbusy(b4, R_FIFO,
1219 (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1220 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_001);
1221 b4xxp_setreg8(b4, A_CHANNEL,
1222 ((16 + hfc_chan) << V_CH_FNUM_SHIFT) | V_CH_FDIR);
1223 b4xxp_setreg8(b4, R_SLOT,
1224 (((fifo * 2) + 4) << V_SL_NUM_SHIFT) | V_SL_DIR);
1225 b4xxp_setreg8(b4, A_SL_CFG,
1226 V_ROUT_RX_STIO2 | ((16 + hfc_chan) << V_CH_SNUM_SHIFT) |
1227 V_CH_SDIR);
1228 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1229
1230 if (DBG) {
1231 pr_info("\thost -> PCM ts %d uses HFC chan %d via FIFO %d\n",
1232 ts, 16 + hfc_chan, fifo);
1233 }
1234
1235 /* Host FIFO -> PCM TX */
1236 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1237 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_001);
1238 b4xxp_setreg8(b4, A_CHANNEL, ((16 + hfc_chan) << V_CH_FNUM_SHIFT));
1239 b4xxp_setreg8(b4, R_SLOT, ((fifo * 2) << V_SL_NUM_SHIFT));
1240 b4xxp_setreg8(b4, A_SL_CFG,
1241 V_ROUT_TX_STIO1 | ((16 + hfc_chan) << V_CH_SNUM_SHIFT));
1242 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1243
1244 if (DBG) {
1245 pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n",
1246 ts, hfc_chan, 16 + fifo);
1247 }
1248
1249 /* PCM -> S/T */
1250 hfc_setreg_waitbusy(b4, R_FIFO,
1251 ((16 + fifo) << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1252 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_110);
1253 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
1254 b4xxp_setreg8(b4, R_SLOT, (((fifo*2)+3) << V_SL_NUM_SHIFT) | V_SL_DIR);
1255 b4xxp_setreg8(b4, A_SL_CFG,
1256 V_ROUT_RX_STIO2 | (hfc_chan << V_CH_SNUM_SHIFT) | V_CH_SDIR);
1257 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1258
1259 if (DBG) {
1260 pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n",
1261 ts, hfc_chan, 16 + fifo);
1262 }
1263
1264 flush_pci();
1265 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1266 }
1267
1268 static void hfc_assign_bchan_fifo_noec(struct b4xxp *b4, int port, int bchan)
1269 {
1270 int fifo, hfc_chan, ts;
1271 unsigned long irq_flags;
1272 static int first=1;
1273
1274 if (first) {
1275 first = 0;
1276 dev_info(&b4->pdev->dev, "NOTE: hardware echo cancellation has been disabled\n");
1277 }
1278
1279 fifo = port * 2;
1280 hfc_chan = port * 4;
1281 ts = port * 8;
1282
1283 if (bchan) {
1284 fifo += 1;
1285 hfc_chan += 1;
1286 ts += 4;
1287 }
1288
1289 /* record the host's FIFO # in the span fifo array */
1290 b4->spans[port].fifos[bchan] = fifo;
1291 spin_lock_irqsave(&b4->fifolock, irq_flags);
1292
1293 if (DBG) {
1294 dev_info(&b4->pdev->dev,
1295 "port %d, B channel %d\n\thost -> S/T "
1296 "uses HFC chan %d via FIFO %d\n",
1297 port, bchan, hfc_chan, fifo);
1298 }
1299
1300 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1301 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_000);
1302 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
1303 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1304
1305 if (DBG)
1306 pr_info("\tS/T -> host uses HFC chan %d via FIFO %d\n", hfc_chan, fifo);
1307
1308 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1309 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_HDLC_TRP | V_DATA_FLOW_000);
1310 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
1311 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1312
1313 if (DBG)
1314 pr_info("\tPCM ts %d -> S/T uses HFC chan %d via FIFO %d\n", ts, hfc_chan, 16 + fifo);
1315
1316 flush_pci(); /* ensure all those writes actually hit hardware */
1317 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1318 }
1319
1320 /*
1321 * Connects an S/T port's D channel to a host-facing FIFO.
1322 * Both TX and RX interrupts are enabled!
1323 * *** performs no error checking of parameters ***
1324 */
1325 static void hfc_assign_dchan_fifo(struct b4xxp *b4, int port)
1326 {
1327 int fifo, hfc_chan;
1328 unsigned long irq_flags;
1329
1330 switch (b4->card_type) {
1331 case B800P_OV: /* fall through */
1332 case OCTOBRI: /* fall through */
1333 case BN8S0:
1334 /* In HFC-8S cards we can't use ports 8-11 for dchan FIFOs */
1335 fifo = port + 16;
1336 break;
1337 default:
1338 fifo = port + 8;
1339 break;
1340 }
1341
1342 hfc_chan = (port * 4) + 2;
1343
1344 /* record the host's FIFO # in the span fifo array */
1345 if (IS_B430P(b4))
1346 b4->spans[3-port].fifos[2] = fifo;
1347 else if (IS_B230P(b4))
1348 b4->spans[2-port].fifos[2] = fifo;
1349 else
1350 b4->spans[port].fifos[2] = fifo;
1351
1352 if (DBG) {
1353 dev_info(&b4->pdev->dev,
1354 "port %d, D channel\n\thost -> S/T uses HFC chan %d "
1355 "via FIFO %d\n", port, hfc_chan, fifo);
1356 }
1357
1358 spin_lock_irqsave(&b4->fifolock, irq_flags);
1359
1360 /* Host FIFO -> S/T TX, HDLC mode, no IRQ. */
1361 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1362 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_TRP_IRQ | V_DATA_FLOW_000);
1363 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT));
1364 b4xxp_setreg8(b4, A_SUBCH_CFG, 0x02);
1365 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1366
1367 if (DBG)
1368 pr_info("\tS/T -> host uses HFC chan %d via FIFO %d\n", hfc_chan, fifo);
1369
1370 /* S/T RX -> Host FIFO, HDLC mode, IRQ will be enabled when port opened. */
1371 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1372 b4xxp_setreg8(b4, A_CON_HDLC, V_IFF | V_TRP_IRQ | V_DATA_FLOW_000);
1373 b4xxp_setreg8(b4, A_CHANNEL, (hfc_chan << V_CH_FNUM_SHIFT) | V_CH_FDIR);
1374 b4xxp_setreg8(b4, A_SUBCH_CFG, 0x02);
1375 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1376
1377 if (DBG)
1378 pr_info("\n");
1379
1380 flush_pci(); /* ensure all those writes actually hit hardware */
1381 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1382 }
1383
1384 /* takes a read/write fifo pair and optionally resets it, optionally enabling the rx/tx interrupt */
1385 static void hfc_reset_fifo_pair(struct b4xxp *b4, int fifo, int reset, int force_no_irq)
1386 {
1387 unsigned long irq_flags;
1388
1389 spin_lock_irqsave(&b4->fifolock, irq_flags);
1390
1391 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1392 b4xxp_setreg8(b4, A_IRQ_MSK, (!force_no_irq && b4->fifo_en_txint & (1 << fifo)) ? V_IRQ : 0);
1393
1394 if (reset)
1395 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1396
1397 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1398 b4xxp_setreg8(b4, A_IRQ_MSK, (!force_no_irq && b4->fifo_en_rxint & (1 << fifo)) ? V_IRQ : 0);
1399
1400 if (reset)
1401 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_RES_FIFO);
1402
1403 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1404 }
1405
1406
1407 static void b4xxp_set_sync_src(struct b4xxp *b4, int port)
1408 {
1409 int b;
1410 struct b4xxp_span *bspan;
1411
1412 /* -1 = no timing selection */
1413 if (port == -1) {
1414 b = 0;
1415 } else {
1416 bspan = &b4->spans[port];
1417 b = (bspan->phy_port & V_SYNC_SEL_MASK) | V_MAN_SYNC;
1418 }
1419
1420 b4xxp_setreg8(b4, R_ST_SYNC, b);
1421 b4->syncspan = port;
1422 }
1423
1424 /*
1425 * Finds the highest-priority sync span that is not in alarm and returns it.
1426 * Note: the span #s in b4->spans[].sync are 1-based, and this returns
1427 * a 0-based span, or -1 if no spans are found.
1428 */
1429 static int b4xxp_find_sync(struct b4xxp *b4)
1430 {
1431 int i, psrc, src;
1432
1433 src = -1; /* default to automatic */
1434
1435 for (i=0; i < b4->numspans; i++) {
1436 if (DBG)
1437 dev_info(&b4->pdev->dev, "Checking sync pos %d, have span %d\n", i, b4->spans[i].sync);
1438 psrc = b4->spans[i].sync;
1439 if (psrc > 0 && !b4->spans[psrc - 1].span.alarms) {
1440 if (DBG)
1441 dev_info(&b4->pdev->dev, "chosen\n");
1442 src = psrc;
1443 break;
1444 }
1445 }
1446
1447 if (src >= 0)
1448 return src - 1;
1449 else
1450 return src;
1451 }
1452
1453 static ssize_t b4_timing_master_show(struct device *dev,
1454 struct device_attribute *attr,
1455 char *buf)
1456 {
1457 struct b4xxp *b4 = dev_get_drvdata(dev);
1458 return sprintf(buf, "%d\n", b4->syncspan);
1459 }
1460
1461 static DEVICE_ATTR(timing_master, 0400, b4_timing_master_show, NULL);
1462
1463 static void create_sysfs_files(struct b4xxp *b4)
1464 {
1465 int ret;
1466 ret = device_create_file(&b4->pdev->dev,
1467 &dev_attr_timing_master);
1468 if (ret) {
1469 dev_info(&b4->pdev->dev,
1470 "Failed to create device attributes.\n");
1471 }
1472 }
1473
1474 static void remove_sysfs_files(struct b4xxp *b4)
1475 {
1476 device_remove_file(&b4->pdev->dev,
1477 &dev_attr_timing_master);
1478 }
1479
1480 /*
1481 * allocates memory and pretty-prints a given S/T state engine state to it.
1482 * calling routine is responsible for freeing the pointer returned!
1483 * Performs no hardware access whatsoever, but does use GFP_KERNEL so do not call from IRQ context.
1484 * if full == 1, prints a "full" dump; otherwise just prints current state.
1485 */
1486 static char *hfc_decode_st_state(struct b4xxp *b4, struct b4xxp_span *span, unsigned char state, int full)
1487 {
1488 int nt, sta;
1489 char s[128], *str;
1490 const char *ststr[2][16] = { /* TE, NT */
1491 { "RESET", "?", "SENSING", "DEACT.", "AWAIT.SIG", "IDENT.INPUT", "SYNCD", "ACTIVATED",
1492 "LOSTFRAMING", "?", "?", "?", "?", "?", "?", "?" },
1493 { "RESET", "DEACT.", "PEND.ACT", "ACTIVE", "PEND.DEACT", "?", "?", "?",
1494 "?", "?", "?", "?", "?", "?", "?", "?" }
1495 };
1496
1497 str = kmalloc(256, GFP_ATOMIC);
1498 if (!str) {
1499 dev_warn(&b4->pdev->dev, "could not allocate mem for ST state decode string!\n");
1500 return NULL;
1501 }
1502
1503 nt = !span->te_mode;
1504 sta = (state & V_ST_STA_MASK);
1505
1506 sprintf(str, "P%d: %s state %c%d (%s)", span->port + 1, (nt ? "NT" : "TE"), (nt ? 'G' : 'F'), sta, ststr[nt][sta]);
1507
1508 if (full) {
1509 sprintf(s, " SYNC: %s, RX INFO0: %s", ((state & V_FR_SYNC) ? "yes" : "no"), ((state & V_INFO0) ? "yes" : "no"));
1510 strcat(str, s);
1511
1512 if (nt) {
1513 sprintf(s, ", T2 %s, auto G2->G3: %s", ((state & V_T2_EXP) ? "expired" : "OK"),
1514 ((state & V_G2_G3) ? "yes" : "no"));
1515 strcat(str, s);
1516 }
1517 }
1518
1519 return str;
1520 }
1521
1522 /*
1523 * sets an S/T port state machine to a given state.
1524 * if 'auto' is nonzero, will put the state machine back in auto mode after setting the state.
1525 */
1526 static void hfc_handle_state(struct b4xxp_span *s);
1527 static void hfc_force_st_state(struct b4xxp *b4, struct b4xxp_span *s, int state, int resume_auto)
1528 {
1529 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port,
1530 A_ST_RD_STA, state | V_ST_LD_STA);
1531
1532 udelay(6);
1533
1534 if (resume_auto) {
1535 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_RD_STA, state);
1536 }
1537
1538 if (DBG_ST) {
1539 char *x;
1540
1541 x = hfc_decode_st_state(b4, s, state, 1);
1542 dev_info(&b4->pdev->dev,
1543 "forced port %d to state %d (auto: %d), new decode: %s\n",
1544 s->port + 1, state, resume_auto, x);
1545 kfree(x);
1546 }
1547
1548 /* make sure that we activate any timers/etc needed by this state change */
1549 hfc_handle_state(s);
1550 }
1551
1552 static void hfc_stop_st(struct b4xxp_span *s);
1553 static void hfc_start_st(struct b4xxp_span *s);
1554
1555 /* figures out what to do when an S/T port's timer expires. */
1556 static void hfc_timer_expire(struct b4xxp_span *s, int t_no)
1557 {
1558 struct b4xxp *b4 = s->parent;
1559
1560 if (DBG_ST) {
1561 dev_info(&b4->pdev->dev,
1562 "%lu: hfc_timer_expire, Port %d T%d expired "
1563 "(value=%lu ena=%d)\n",
1564 b4->ticks, s->port + 1, t_no + 1, s->hfc_timers[t_no],
1565 s->hfc_timer_on[t_no]);
1566 }
1567 /*
1568 * There are four timers associated with every HFC S/T port:
1569 * T1 is used by the NT state machine, and is the maximum time the NT side
1570 * should wait for G3 (active) state.
1571 * T2 is not actually used in the driver, it is handled by the HFC-4S
1572 * internally.
1573 * T3 is used by the TE state machine; it is the maximum time the TE side should
1574 * wait for the INFO4 (activated) signal.
1575 * T4 is a special timer used for debug purposes for monitoring of L1 state
1576 * during activation attempt.
1577 */
1578
1579 /* First, disable the expired timer; hfc_force_st_state() may activate it again. */
1580 s->hfc_timer_on[t_no] = 0;
1581
1582 switch(t_no) {
1583 case HFC_T1: /* switch to G4 (pending deact.), resume auto mode */
1584 hfc_force_st_state(b4, s, 4, 1);
1585 break;
1586 case HFC_T2: /* switch to G1 (deactivated), resume auto mode */
1587 hfc_force_st_state(b4, s, 1, 1);
1588 break;
1589 case HFC_T3: /* switch to F3 (deactivated), resume auto mode */
1590 hfc_stop_st(s);
1591 if (persistentlayer1)
1592 hfc_start_st(s);
1593 break;
1594 case HFC_T4:
1595 hfc_handle_state(s);
1596 s->hfc_timers[HFC_T4] = b4->ticks + 1000;
1597 s->hfc_timer_on[HFC_T4] = 1;
1598 break;
1599 default:
1600 if (printk_ratelimit()) {
1601 dev_warn(&b4->pdev->dev,
1602 "hfc_timer_expire found an unknown expired "
1603 "timer (%d)??\n", t_no);
1604 }
1605 }
1606 }
1607
1608 /*
1609 * Run through the active timers on a card and deal with any expiries.
1610 * Also see if the alarm debounce time has expired and if it has, tell DAHDI.
1611 */
1612 static void hfc_update_st_timers(struct b4xxp *b4)
1613 {
1614 int i, j;
1615 struct b4xxp_span *s;
1616
1617 for (i=0; i < b4->numspans; i++) {
1618 s = &b4->spans[i];
1619
1620 for (j = HFC_T1; j < ARRAY_SIZE(s->hfc_timers); j++) {
1621 /* we don't really do timer2, it is expired by the
1622 * state change handler */
1623 if (j == HFC_T2)
1624 continue;
1625
1626 if (s->hfc_timer_on[j] && time_after_eq(b4->ticks, s->hfc_timers[j])) {
1627 hfc_timer_expire(s, j);
1628 }
1629 }
1630
1631 if (s->newalarm != s->span.alarms && time_after_eq(b4->ticks, s->alarmtimer)) {
1632 s->span.alarms = s->newalarm;
1633 if ((!s->newalarm && teignorered) || (!teignorered)) {
1634 dahdi_alarm_notify(&s->span);
1635 }
1636 b4xxp_set_sync_src(b4, b4xxp_find_sync(b4));
1637 if (DBG_ALARM) {
1638 dev_info(&b4->pdev->dev,
1639 "span %d: alarm %d "
1640 "debounced\n",
1641 i + 1, s->newalarm);
1642 }
1643 }
1644 }
1645 }
1646
1647 /* this is the driver-level state machine for an S/T port */
1648 static void hfc_handle_state(struct b4xxp_span *s)
1649 {
1650 struct b4xxp *b4;
1651 unsigned char state, sta;
1652 int nt, oldalarm;
1653 unsigned long oldtimer;
1654
1655 b4 = s->parent;
1656 nt = !s->te_mode;
1657
1658 state = b4xxp_getreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_RD_STA);
1659
1660 sta = (state & V_ST_STA_MASK);
1661
1662 if (DBG_ST) {
1663 char *x;
1664
1665 x = hfc_decode_st_state(b4, s, state, 1);
1666 dev_info(&b4->pdev->dev,
1667 "port %d phy_port %d A_ST_RD_STA old=0x%02x now=0x%02x, decoded: %s\n",
1668 s->port + 1, s->phy_port, s->oldstate, state, x);
1669 kfree(x);
1670 }
1671
1672 oldalarm = s->newalarm;
1673 oldtimer = s->alarmtimer;
1674
1675 if (nt) {
1676 switch(sta) {
1677 default: /* Invalid NT state */
1678 case 0x0: /* NT state G0: Reset */
1679 case 0x1: /* NT state G1: Deactivated */
1680 case 0x4: /* NT state G4: Pending Deactivation */
1681 s->newalarm = DAHDI_ALARM_RED;
1682 break;
1683 case 0x2: /* NT state G2: Pending Activation */
1684 s->newalarm = DAHDI_ALARM_YELLOW;
1685 break;
1686 case 0x3: /* NT state G3: Active */
1687 s->hfc_timer_on[HFC_T1] = 0;
1688 s->newalarm = 0;
1689 break;
1690 }
1691 } else {
1692 switch(sta) {
1693 default: /* Invalid TE state */
1694 case 0x0: /* TE state F0: Reset */
1695 case 0x2: /* TE state F2: Sensing */
1696 case 0x3: /* TE state F3: Deactivated */
1697 case 0x4: /* TE state F4: Awaiting Signal */
1698 case 0x8: /* TE state F8: Lost Framing */
1699 s->newalarm = DAHDI_ALARM_RED;
1700 break;
1701 case 0x5: /* TE state F5: Identifying Input */
1702 case 0x6: /* TE state F6: Synchronized */
1703 s->newalarm = DAHDI_ALARM_YELLOW;
1704 break;
1705 case 0x7: /* TE state F7: Activated */
1706 s->hfc_timer_on[HFC_T3] = 0;
1707 s->hfc_timer_on[HFC_T4] = 0;
1708 s->newalarm = 0;
1709 break;
1710 }
1711 }
1712
1713 s->alarmtimer = b4->ticks + alarmdebounce;
1714 s->oldstate = state;
1715
1716 if (DBG_ALARM) {
1717 dev_info(&b4->pdev->dev, "span %d: old alarm %d expires %ld, new alarm %d expires %ld\n",
1718 s->port + 1, oldalarm, oldtimer, s->newalarm, s->alarmtimer);
1719 }
1720
1721 /* we only care about T2 expiry in G4. */
1722 if (nt && (sta == 4) && (state & V_T2_EXP)) {
1723 if (s->hfc_timer_on[HFC_T2])
1724 hfc_timer_expire(s, HFC_T2); /* handle T2 expiry */
1725 }
1726
1727 /* If we're in F3 and receiving INFO0, start T3 and jump to F4 */
1728 if (!nt && (sta == 3) && (state & V_INFO0)) {
1729 if (persistentlayer1) {
1730 s->hfc_timers[HFC_T3] = b4->ticks + timer_3_ms;
1731 s->hfc_timer_on[HFC_T3] = 1;
1732 if (DBG_ST) {
1733 dev_info(&b4->pdev->dev,
1734 "port %d: receiving INFO0 in state 3, "
1735 "setting T3 and jumping to F4\n",
1736 s->port + 1);
1737 }
1738 hfc_start_st(s);
1739 }
1740 }
1741 }
1742
1743 static void hfc_stop_all_timers(struct b4xxp_span *s)
1744 {
1745 s->hfc_timer_on[HFC_T4] = 0;
1746 s->hfc_timer_on[HFC_T3] = 0;
1747 s->hfc_timer_on[HFC_T2] = 0;
1748 s->hfc_timer_on[HFC_T1] = 0;
1749 }
1750
1751 static void hfc_stop_st(struct b4xxp_span *s)
1752 {
1753 struct b4xxp *b4 = s->parent;
1754
1755 hfc_stop_all_timers(s);
1756
1757 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_WR_STA,
1758 V_ST_ACT_DEACTIVATE);
1759 }
1760
1761 /*
1762 * resets an S/T interface to a given NT/TE mode
1763 */
1764 static void hfc_reset_st(struct b4xxp_span *s)
1765 {
1766 int b;
1767 struct b4xxp *b4;
1768
1769 b4 = s->parent;
1770
1771 hfc_stop_st(s);
1772
1773 /* force state G0/F0 (reset), then force state 1/2 (deactivated/sensing) */
1774 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_WR_STA, V_ST_LD_STA);
1775 flush_pci(); /* make sure write hit hardware */
1776
1777 s->span.alarms = DAHDI_ALARM_RED;
1778 s->newalarm = DAHDI_ALARM_RED;
1779 dahdi_alarm_notify(&s->span);
1780
1781 udelay(10);
1782
1783 /* set up the clock control register. Must be done before we activate the interface. */
1784 if (s->te_mode)
1785 b = 0x0e;
1786 else
1787 b = 0x0c | (6 << V_ST_SMPL_SHIFT);
1788
1789 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_CLK_DLY, b);
1790
1791 /* set TE/NT mode, enable B and D channels. */
1792
1793 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_CTRL0,
1794 V_B1_EN | V_B2_EN | (s->te_mode ? 0 : V_ST_MD));
1795 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_CTRL1,
1796 V_G2_G3_EN | V_E_IGNO);
1797 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_CTRL2,
1798 V_B1_RX_EN | V_B2_RX_EN);
1799
1800 /* enable the state machine. */
1801 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_WR_STA, 0x00);
1802 flush_pci();
1803
1804 udelay(100);
1805 }
1806
1807 static void hfc_start_st(struct b4xxp_span *s)
1808 {
1809 struct b4xxp *b4 = s->parent;
1810
1811 b4xxp_setreg_ra(b4, R_ST_SEL, s->phy_port, A_ST_WR_STA,
1812 V_ST_ACT_ACTIVATE);
1813
1814 /* start T1 if in NT mode, T3 if in TE mode */
1815 if (s->te_mode) {
1816 s->hfc_timers[HFC_T3] = b4->ticks + timer_3_ms;
1817 s->hfc_timer_on[HFC_T3] = 1;
1818 s->hfc_timer_on[HFC_T1] = 0;
1819
1820 s->hfc_timers[HFC_T4] = b4->ticks + 1000;
1821 s->hfc_timer_on[HFC_T4] = 1;
1822
1823 if (DBG_ST) {
1824 dev_info(&b4->pdev->dev,
1825 "setting port %d t3 timer to %lu\n",
1826 s->port + 1, s->hfc_timers[HFC_T3]);
1827 }
1828 } else {
1829 s->hfc_timers[HFC_T1] = b4->ticks + timer_1_ms;
1830 s->hfc_timer_on[HFC_T1] = 1;
1831 s->hfc_timer_on[HFC_T3] = 0;
1832 if (DBG_ST) {
1833 dev_info(&b4->pdev->dev,
1834 "setting port %d t1 timer to %lu\n",
1835 s->port + 1, s->hfc_timers[HFC_T1]);
1836 }
1837 }
1838 }
1839
1840 /*
1841 * read in the HFC GPIO to determine each port's mode (TE or NT).
1842 * Then, reset and start the port.
1843 * the flow controller should be set up before this is called.
1844 */
1845 static void hfc_init_all_st(struct b4xxp *b4)
1846 {
1847 int gpio = 0;
1848 int i, nt;
1849 struct b4xxp_span *s;
1850
1851 /* All other cards supported by this driver read jumpers for modes */
1852 if (!IS_GEN2(b4))
1853 gpio = b4xxp_getreg8(b4, R_GPI_IN3);
1854
1855 for (i=0; i < b4->numspans; i++) {
1856 s = &b4->spans[i];
1857 s->parent = b4;
1858
1859 if (IS_B430P(b4)) {
1860 /* The physical ports are reversed on the b430 */
1861 /* Port 0-3 in b4->spans[] are physically ports 3-0 */
1862 s->phy_port = b4->numspans-1-i;
1863 s->port = i;
1864 } else if (IS_B230P(b4)) {
1865 /* The physical ports are reversed on the b230 */
1866 /* Port 0-1 in b4->spans[] are physically ports 2-1 */
1867 s->phy_port = b4->numspans - i;
1868 s->port = i;
1869 } else {
1870 s->phy_port = i;
1871 s->port = i;
1872 }
1873
1874 /* The way the Digium B410P card reads the NT/TE mode
1875 * jumper is the oposite of how other HFC-4S cards do:
1876 * - In B410P: GPIO=0: NT
1877 * - In Junghanns: GPIO=0: TE
1878 */
1879 if (IS_B410P(b4)) {
1880 nt = ((gpio & (1 << (i + 4))) == 0);
1881 } else if (IS_GEN2(b4)) {
1882 /* Read default digital lineconfig reg on GEN2 */
1883 int reg;
1884 unsigned long flags;
1885
1886 spin_lock_irqsave(&b4->seqlock, flags);
1887 hfc_gpio_set(b4, 0x20);
1888 reg = hfc_sram_read(b4);
1889 spin_unlock_irqrestore(&b4->seqlock, flags);
1890
1891 if (reg & (1 << (4 + s->phy_port)))
1892 nt = 1;
1893 else
1894 nt = 0;
1895 } else {
1896 nt = ((gpio & (1 << (i + 4))) != 0);
1897 }
1898
1899 s->te_mode = !nt;
1900
1901 dev_info(&b4->pdev->dev,
1902 "Port %d: %s mode\n", i + 1, (nt ? "NT" : "TE"));
1903
1904 }
1905 }
1906
1907 /*
1908 * Look at one B-channel FIFO and determine if we should exchange data with it.
1909 * It is assumed that the S/T port is active.
1910 * returns 1 if data was exchanged, 0 otherwise.
1911 */
1912 static int hfc_poll_one_bchan_fifo(struct b4xxp_span *span, int c)
1913 {
1914 int fifo, zlen, z1, z2, ret;
1915 unsigned long irq_flags;
1916 struct b4xxp *b4;
1917 struct dahdi_chan *chan;
1918
1919 ret = 0;
1920 b4 = span->parent;
1921 fifo = span->fifos[c];
1922 chan = span->chans[c];
1923
1924 spin_lock_irqsave(&b4->fifolock, irq_flags);
1925
1926 /* select RX FIFO */
1927 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR | V_REV);
1928
1929 get_Z(z1, z2, zlen);
1930
1931 /* TODO: error checking, full FIFO mostly */
1932
1933 if (zlen >= DAHDI_CHUNKSIZE) {
1934 *(unsigned int *)&chan->readchunk[0] = b4xxp_getreg32(b4, A_FIFO_DATA2);
1935 *(unsigned int *)&chan->readchunk[4] = b4xxp_getreg32(b4, A_FIFO_DATA2);
1936 /*
1937 * now TX FIFO
1938 *
1939 * Note that we won't write to the TX FIFO if there wasn't room in the RX FIFO.
1940 * The TX and RX sides should be kept pretty much lock-step.
1941 *
1942 * Write the last byte _NOINC so that if we don't get more data in time, we aren't leaking unknown data
1943 * (See HFC datasheet)
1944 */
1945
1946 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_REV);
1947
1948 b4xxp_setreg32(b4, A_FIFO_DATA2, *(unsigned int *) &chan->writechunk[0]);
1949 b4xxp_setreg32(b4, A_FIFO_DATA2, *(unsigned int *) &chan->writechunk[4]);
1950 ret = 1;
1951 }
1952
1953 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1954 return ret;
1955 }
1956
1957 /*
1958 * Run through all of the host-facing B-channel RX FIFOs, looking for at least 8 bytes available.
1959 * If a B channel RX fifo has enough data, perform the data transfer in both directions.
1960 * D channel is done in an interrupt handler.
1961 * The S/T port state must be active or we ignore the fifo.
1962 * Returns nonzero if there was at least DAHDI_CHUNKSIZE bytes in the FIFO
1963 */
1964 static int hfc_poll_fifos(struct b4xxp *b4)
1965 {
1966 int ret=0, span;
1967 unsigned long irq_flags;
1968
1969 for (span=0; span < b4->numspans; span++) {
1970
1971 /* Make sure DAHDI's got this span up */
1972 if (!(b4->spans[span].span.flags & DAHDI_FLAG_RUNNING))
1973 continue;
1974
1975 /* TODO: Make sure S/T port is in active state */
1976 ret = hfc_poll_one_bchan_fifo(&b4->spans[span], 0);
1977 ret |= hfc_poll_one_bchan_fifo(&b4->spans[span], 1);
1978 }
1979
1980 /* change the active FIFO one last time to make sure the last-changed FIFO updates its pointers (as per the datasheet) */
1981 spin_lock_irqsave(&b4->fifolock, irq_flags);
1982 hfc_setreg_waitbusy(b4, R_FIFO, (31 << V_FIFO_NUM_SHIFT));
1983 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
1984
1985 return ret;
1986 }
1987
1988 /* NOTE: assumes fifo lock is held */
1989 static inline void debug_fz(struct b4xxp *b4, int fifo, const char *prefix, char *buf)
1990 {
1991 int f1, f2, flen, z1, z2, zlen;
1992
1993 get_F(f1, f2, flen);
1994 get_Z(z1, z2, zlen);
1995
1996 sprintf(buf, "%s: (fifo %d): f1/f2/flen=%d/%d/%d, z1/z2/zlen=%d/%d/%d\n", prefix, fifo, f1, f2, flen, z1, z2, zlen);
1997 }
1998
1999 /* enable FIFO RX int and reset the FIFO */
2000 static int hdlc_start(struct b4xxp *b4, int fifo)
2001 {
2002 b4->fifo_en_txint |= (1 << fifo);
2003 b4->fifo_en_rxint |= (1 << fifo);
2004
2005 hfc_reset_fifo_pair(b4, fifo, 1, 0);
2006 return 0;
2007 }
2008
2009 /* disable FIFO ints and reset the FIFO */
2010 static void hdlc_stop(struct b4xxp *b4, int fifo)
2011 {
2012 b4->fifo_en_txint &= ~(1 << fifo);
2013 b4->fifo_en_rxint &= ~(1 << fifo);
2014
2015 hfc_reset_fifo_pair(b4, fifo, 1, 0);
2016 }
2017
2018 /*
2019 * Inner loop for D-channel receive function.
2020 * Retrieves a full HDLC frame from the hardware.
2021 * If the hardware indicates that the frame is complete,
2022 * we check the HDLC engine's STAT byte and update DAHDI as needed.
2023 *
2024 * Returns the number of HDLC frames left in the FIFO.
2025 */
2026 static int hdlc_rx_frame(struct b4xxp_span *bspan)
2027 {
2028 int fifo, i, j, zleft;
2029 int z1, z2, zlen, f1, f2, flen;
2030 unsigned char buf[WCB4XXP_HDLC_BUF_LEN];
2031 char debugbuf[256];
2032 unsigned long irq_flags;
2033 struct b4xxp *b4 = bspan->parent;
2034 unsigned char stat;
2035
2036 fifo = bspan->fifos[2];
2037
2038 spin_lock_irqsave(&b4->fifolock, irq_flags);
2039 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
2040 get_F(f1, f2, flen);
2041 get_Z(z1, z2, zlen);
2042 debug_fz(b4, fifo, "hdlc_rx_frame", debugbuf);
2043 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
2044
2045 if (DBG_HDLC && DBG_SPANFILTER) {
2046 pr_info("%s", debugbuf);
2047 }
2048
2049 /* first check to make sure we really do have HDLC frames available to retrieve */
2050 if (flen == 0) {
2051 if (DBG_HDLC && DBG_SPANFILTER) {
2052 dev_info(&b4->pdev->dev, "hdlc_rx_frame(span %d): no frames available?\n",
2053 bspan->port + 1);
2054 }
2055
2056 return flen;
2057 }
2058
2059 zleft = zlen + 1; /* include STAT byte that the HFC injects after FCS */
2060
2061 do {
2062 int truncated;
2063 if (zleft > WCB4XXP_HDLC_BUF_LEN) {
2064 truncated = 1;
2065 j = WCB4XXP_HDLC_BUF_LEN;
2066 } else {
2067 truncated = 0;
2068 j = zleft;
2069 }
2070
2071 spin_lock_irqsave(&b4->fifolock, irq_flags);
2072 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
2073 for (i=0; i < j; i++)
2074 buf[i] = b4xxp_getreg8(b4, A_FIFO_DATA0);
2075 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
2076
2077 /* don't send STAT byte to DAHDI */
2078 if ((bspan->sigchan) && (j > 1))
2079 dahdi_hdlc_putbuf(bspan->sigchan, buf, truncated ? j : j - 1);
2080
2081 zleft -= j;
2082 if (DBG_HDLC && DBG_SPANFILTER) {
2083 dev_info(&b4->pdev->dev, "hdlc_rx_frame(span %d): z1/z2/zlen=%d/%d/%d, zleft=%d\n",
2084 bspan->port + 1, z1, z2, zlen, zleft);
2085 for (i=0; i < j; i++) printk("%02x%c", buf[i], (i < ( j - 1)) ? ' ':'\n');
2086 }
2087 } while (zleft > 0);
2088 stat = buf[j - 1];
2089
2090 /* Frame received, increment F2 and get an updated count of frames left */
2091 spin_lock_irqsave(&b4->fifolock, irq_flags);
2092 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
2093 get_F(f1, f2, flen);
2094 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
2095
2096 /* If this channel is not configured with a signalling span we don't
2097 * need to notify the rest of dahdi about this frame. */
2098 if (!bspan->sigchan)
2099 return flen;
2100
2101 ++bspan->frames_in;
2102 if (zlen < 3) {
2103 if (DBG_HDLC && DBG_SPANFILTER)
2104 dev_notice(&b4->pdev->dev, "odd, zlen less then 3?\n");
2105 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
2106 } else {
2107
2108 /* if STAT != 0, indicates bad frame */
2109 if (stat != 0x00) {
2110 if (DBG_HDLC && DBG_SPANFILTER) {
2111 dev_info(&b4->pdev->dev,
2112 "(span %d) STAT=0x%02x indicates "
2113 "frame problem: ",
2114 bspan->port + 1, stat);
2115 }
2116 if (stat == 0xff) {
2117 if (DBG_HDLC && DBG_SPANFILTER)
2118 printk("HDLC Abort\n");
2119 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
2120 } else {
2121 if (DBG_HDLC && DBG_SPANFILTER)
2122 printk("Bad FCS\n");
2123 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_BADFCS);
2124 }
2125 /* STAT == 0, means frame was OK */
2126 } else {
2127 if (DBG_HDLC && DBG_SPANFILTER) {
2128 dev_info(&b4->pdev->dev,
2129 "(span %d) Frame %d is good!\n",
2130 bspan->port + 1, bspan->frames_in);
2131 }
2132 dahdi_hdlc_finish(bspan->sigchan);
2133 }
2134 }
2135
2136 return flen;
2137 }
2138
2139
2140 /*
2141 * Takes one blob of data from DAHDI and shoots it out to the hardware.
2142 * The blob may or may not be a complete HDLC frame.
2143 * If it isn't, the D-channel FIFO interrupt handler will take care of pulling the rest.
2144 * Returns nonzero if there is still data to send in the current HDLC frame.
2145 */
2146 static int hdlc_tx_frame(struct b4xxp_span *bspan)
2147 {
2148 struct b4xxp *b4 = bspan->parent;
2149 int res, i, fifo;
2150 int z1, z2, zlen;
2151 unsigned char buf[WCB4XXP_HDLC_BUF_LEN];
2152 unsigned int size = sizeof(buf) / sizeof(buf[0]);
2153 char debugbuf[256];
2154 unsigned long irq_flags;
2155
2156 /* if we're ignoring TE red alarms and we are in alarm, restart the
2157 * S/T state machine */
2158 if (bspan->te_mode && bspan->newalarm != 0)
2159 hfc_start_st(bspan);
2160
2161 fifo = bspan->fifos[2];
2162 res = dahdi_hdlc_getbuf(bspan->sigchan, buf, &size);
2163
2164 spin_lock_irqsave(&b4->fifolock, irq_flags);
2165 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
2166
2167 get_Z(z1, z2, zlen);
2168 debug_fz(b4, fifo, "hdlc_tx_frame", debugbuf);
2169
2170 /* TODO: check zlen, etc. */
2171
2172 if (size > 0) {
2173 bspan->sigactive = 1;
2174
2175 for (i=0; i < size; i++)
2176 b4xxp_setreg8(b4, A_FIFO_DATA0, buf[i]);
2177
2178 /*
2179 * If we got a full frame from DAHDI, increment F and decrement our HDLC pending counter.
2180 * Otherwise, select the FIFO again (to start transmission) and make sure the
2181 * TX IRQ is enabled so we will get called again to finish off the data
2182 */
2183 if (res != 0) {
2184 ++bspan->frames_out;
2185 bspan->sigactive = 0;
2186 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
2187 atomic_dec(&bspan->hdlc_pending);
2188 } else {
2189 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
2190 b4xxp_setreg8(b4, A_IRQ_MSK, V_IRQ);
2191 }
2192 }
2193
2194 /* if there are no more frames pending, disable the interrupt. */
2195 if (res == -1) {
2196 b4xxp_setreg8(b4, A_IRQ_MSK, 0);
2197 }
2198
2199 spin_unlock_irqrestore(&b4->fifolock, irq_flags);
2200
2201 if (DBG_HDLC && DBG_SPANFILTER) {
2202 dev_info(&b4->pdev->dev, "%s", debugbuf);
2203 dev_info(&b4->pdev->dev, "hdlc_tx_frame(span %d): DAHDI gave %d bytes for FIFO %d (res=%d)\n",
2204 bspan->port + 1, size, fifo, res);
2205 for (i=0; i < size; i++)
2206 printk("%02x%c", buf[i], (i < (size - 1)) ? ' ' : '\n');
2207
2208 if (size && res != 0) {
2209 pr_info("Transmitted frame %d on span %d\n",
2210 bspan->frames_out - 1, bspan->port + 1);
2211 }
2212 }
2213
2214 return(res == 0);
2215 }
2216
2217 /*
2218 * b4xxp lowlevel functions
2219 * These are functions which impact more than just the HFC controller.
2220 * (those are named hfc_xxx())
2221 */
2222
2223 /*
2224 * Performs a total reset of the card, reinitializes GPIO.
2225 * The card is initialized enough to have LEDs running, and that's about it.
2226 * Anything to do with audio and enabling any kind of processing is done in stage2.
2227 */
2228 static void b4xxp_init_stage1(struct b4xxp *b4)
2229 {
2230 int i;
2231
2232 hfc_reset(b4); /* total reset of controller */
2233 hfc_gpio_init(b4); /* initialize controller GPIO for CPLD access */
2234 ec_init(b4); /* initialize VPM and VPM GPIO */
2235
2236 b4xxp_setreg8(b4, R_IRQ_CTRL, 0x00); /* make sure interrupts are disabled */
2237 flush_pci(); /* make sure PCI write hits hardware */
2238
2239 /* disable all FIFO interrupts */
2240 for (i=0; i < HFC_NR_FIFOS; i++) {
2241 hfc_setreg_waitbusy(b4, R_FIFO, (i << V_FIFO_NUM_SHIFT));
2242 b4xxp_setreg8(b4, A_IRQ_MSK, 0x00); /* disable the interrupt */
2243 hfc_setreg_waitbusy(b4, R_FIFO, (i << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
2244 b4xxp_setreg8(b4, A_IRQ_MSK, 0x00); /* disable the interrupt */
2245 flush_pci();
2246 }
2247
2248 /* clear any pending FIFO interrupts */
2249 b4xxp_getreg8(b4, R_IRQ_FIFO_BL0);
2250 b4xxp_getreg8(b4, R_IRQ_FIFO_BL1);
2251 b4xxp_getreg8(b4, R_IRQ_FIFO_BL2);
2252 b4xxp_getreg8(b4, R_IRQ_FIFO_BL3);
2253 b4xxp_getreg8(b4, R_IRQ_FIFO_BL4);
2254 b4xxp_getreg8(b4, R_IRQ_FIFO_BL5);
2255 b4xxp_getreg8(b4, R_IRQ_FIFO_BL6);
2256 b4xxp_getreg8(b4, R_IRQ_FIFO_BL7);
2257
2258 b4xxp_setreg8(b4, R_SCI_MSK, 0x00); /* mask off all S/T interrupts */
2259 b4xxp_setreg8(b4, R_IRQMSK_MISC, 0x00); /* nothing else can generate an interrupt */
2260
2261 /*
2262 * set up the clock controller B410P & Cologne Eval Board have a
2263 * 24.576MHz crystal, so the PCM clock is 2x the incoming clock.
2264 * Other cards have a 49.152Mhz crystal, so the PCM clock equals
2265 * incoming clock.
2266 */
2267
2268 if (IS_B410P(b4) || IS_GEN2(b4) || (b4->card_type == QUADBRI_EVAL))
2269 b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x02);
2270 else
2271 b4xxp_setreg8(b4, R_BRG_PCM_CFG, V_PCM_CLK);
2272
2273 /* Reset LED state */
2274 b4->ledreg = 0;
2275 b4xxp_update_leds(b4);
2276
2277 flush_pci();
2278
2279 udelay(100); /* wait a bit for clock to settle */
2280 }
2281
2282 /*
2283 * Stage 2 hardware init.
2284 * Sets up the flow controller, PCM and FIFOs.
2285 * Initializes the echo cancellers.
2286 * S/T interfaces are not initialized here, that is done later, in hfc_init_all_st().
2287 * Interrupts are enabled and once the s/t interfaces are configured, chip should be pretty much operational.
2288 */
2289 static void b4xxp_init_stage2(struct b4xxp *b4)
2290 {
2291 int span;
2292
2293 /*
2294 * set up PCM bus.
2295 * HFC is PCM master.
2296 * C4IO, SYNC_I and SYNC_O unused.
2297 * 32 channels, frame signal positive polarity, active for 2 C4 clocks.
2298 * only the first two timeslots in each quad are active
2299 * STIO0 is transmit-only, STIO1 is receive-only.
2300 */
2301 b4xxp_setreg8(b4, R_PCM_MD0, V_PCM_MD | V_PCM_IDX_MD1);
2302 flush_pci();
2303
2304 if (IS_GEN2(b4)) {
2305 b4xxp_setreg8(b4, R_PCM_MD1, V_PLL_ADJ_00 | V_PCM_DR_4096);
2306 b4xxp_setreg8(b4, R_PWM_MD, 0x50);
2307 b4xxp_setreg8(b4, R_PWM0, 0x38);
2308 } else {
2309 b4xxp_setreg8(b4, R_PCM_MD1, V_PLL_ADJ_00 | V_PCM_DR_2048);
2310 b4xxp_setreg8(b4, R_PWM_MD, 0xa0);
2311 b4xxp_setreg8(b4, R_PWM0, 0x1b);
2312 }
2313
2314 /*
2315 * set up the flow controller.
2316 * B channel map: (4 ports cards with Hardware Echo Cancel present & active)
2317 * FIFO 0 connects Port 1 B0 using HFC channel 16 and PCM timeslots 0/1.
2318 * FIFO 1 connects Port 1 B1 using HFC channel 17 and PCM timeslots 4/5.
2319 * FIFO 2 connects Port 2 B0 using HFC channel 20 and PCM timeslots 8/9.
2320 * FIFO 3 connects Port 2 B1 using HFC channel 21 and PCM timeslots 12/13.
2321 * FIFO 4 connects Port 3 B0 using HFC channel 24 and PCM timeslots 16/17.
2322 * FIFO 5 connects Port 3 B1 using HFC channel 25 and PCM timeslots 20/21.
2323 * FIFO 6 connects Port 4 B0 using HFC channel 28 and PCM timeslots 24/25.
2324 * FIFO 7 connects Port 4 B1 using HFC channel 29 and PCM timeslots 28/29.
2325 *
2326 * All B channel FIFOs have their HDLC controller in transparent mode,
2327 * and only the FIFO for B0 on each port has its interrupt operational.
2328 *
2329 * D channels are handled by FIFOs 8-11.
2330 * FIFO 8 connects Port 1 D using HFC channel 3
2331 * FIFO 9 connects Port 2 D using HFC channel 7
2332 * FIFO 10 connects Port 3 D using HFC channel 11
2333 * FIFO 11 connects Port 4 D using HFC channel 15
2334 *
2335 * D channel FIFOs are operated in HDLC mode and interrupt on end of frame.
2336 *
2337 * B channel map: (8 ports cards without Hardware Echo Cancel)
2338 * FIFO 0 connects Port 1 B0 using HFC channel 0
2339 * FIFO 1 connects Port 1 B1 using HFC channel 1
2340 * FIFO 2 connects Port 2 B0 using HFC channel 4
2341 * FIFO 3 connects Port 2 B1 using HFC channel 5
2342 * .........................
2343 * FIFO 14 connects Port 8 B0 using HFC channel 28
2344 * FIFO 15 connects Port 8 B1 using HFC channel 29
2345 *
2346 * All B channel FIFOs have their HDLC controller in transparent mode,
2347 * and only the FIFO for B0 on each port has its interrupt operational.
2348 *
2349 * D channels are handled by FIFOs 16-23.
2350 * FIFO 16 connects Port 1 D using HFC channel 3
2351 * FIFO 17 connects Port 2 D using HFC channel 7
2352 * FIFO 18 connects Port 3 D using HFC channel 11
2353 * FIFO 19 connects Port 4 D using HFC channel 15
2354 * ................
2355 * FIFO 23 connects Port 8 D using HFC channel 31
2356 * D channel FIFOs are operated in HDLC mode and interrupt on end of frame.
2357 */
2358 for (span=0; span < b4->numspans; span++) {
2359 if (IS_B430P(b4)) {
2360 hfc_assign_fifo_zl(b4, span, 0);
2361 hfc_assign_fifo_zl(b4, span, 1);
2362 hfc_assign_dchan_fifo(b4, span);
2363 } else if (IS_B230P(b4)) {
2364 hfc_assign_fifo_zl(b4, span + 1, 0);
2365 hfc_assign_fifo_zl(b4, span + 1, 1);
2366 hfc_assign_dchan_fifo(b4, span + 1);
2367 } else {
2368 if ((vpmsupport) && (CARD_HAS_EC(b4))) {
2369 hfc_assign_bchan_fifo_ec(b4, span, 0);
2370 hfc_assign_bchan_fifo_ec(b4, span, 1);
2371 } else {
2372 hfc_assign_bchan_fifo_noec(b4, span, 0);
2373 hfc_assign_bchan_fifo_noec(b4, span, 1);
2374 }
2375 hfc_assign_dchan_fifo(b4, span);
2376 }
2377 }
2378
2379 /* set up the timer interrupt for 1ms intervals */
2380 b4xxp_setreg8(b4, R_TI_WD, (2 << V_EV_TS_SHIFT));
2381
2382 /*
2383 * At this point, everything's set up and ready to go.
2384 * Don't actually enable the global interrupt pin.
2385 * DAHDI still needs to start up the spans, and we don't know exactly when.
2386 */
2387 }
2388
2389 static void b4xxp_setleds(struct b4xxp *b4, unsigned char val)
2390 {
2391 ec_write(b4, 0, 0x1a8 + 3, val);
2392 }
2393
2394 static void b4xxp_update_leds_hfc_8s(struct b4xxp *b4)
2395 {
2396 unsigned long lled = 0; /* A bit set is a led OFF */
2397 unsigned long leddw;
2398 int j;
2399 struct b4xxp_span *bspan;
2400
2401 b4->blinktimer++;
2402 for (j = 7; j >= 0; j--) {
2403 bspan = &b4->spans[7 - j];
2404 if (!(bspan->span.flags & DAHDI_FLAG_RUNNING) ||
2405 bspan->span.alarms) {
2406 BIT_SET(lled, j);
2407 continue; /* Led OFF */
2408 }
2409
2410 if (bspan->span.mainttimer || bspan->span.maintstat) {
2411 /* Led Blinking in maint state */
2412 if (b4->blinktimer >= 0x7f)
2413 BIT_SET(lled, j);
2414 }
2415 /* Else: Led on */
2416 }
2417
2418 /* Write Leds...*/
2419 leddw = lled << 24 | lled << 16 | lled << 8 | lled;
2420 b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x21);
2421 iowrite16(0x4000, b4->ioaddr + 4);
2422 iowrite32(leddw, b4->ioaddr);
2423 b4xxp_setreg8(b4, R_BRG_PCM_CFG, 0x20);
2424
2425 if (b4->blinktimer == 0xff)
2426 b4->blinktimer = -1;
2427 }
2428
2429 /* So far only tested for OpenVox cards. Please test it for other hardware */
2430 static void b4xxp_update_leds_hfc(struct b4xxp *b4)
2431 {
2432 int i;
2433 int leds = 0, green_leds = 0; /* Default: off */
2434 struct b4xxp_span *bspan;
2435
2436 b4->blinktimer++;
2437 for (i=0; i < b4->numspans; i++) {
2438 bspan = &b4->spans[i];
2439
2440 if (!(bspan->span.flags & DAHDI_FLAG_RUNNING))
2441 continue; /* Leds are off */
2442
2443 if (bspan->span.alarms) {
2444 /* Red blinking -> Alarm */
2445 if (b4->blinktimer >= 0x7f)
2446 BIT_SET(leds, i);
2447 } else if (bspan->span.mainttimer || bspan->span.maintstat) {
2448 /* Green blinking -> Maint status */
2449 if (b4->blinktimer >= 0x7f)
2450 BIT_SET(green_leds, i);
2451 } else {
2452 /* Steady grean -> No Alarm */
2453 BIT_SET(green_leds, i);
2454 }
2455 }
2456
2457 /* Actually set them. for red: just set the bit in R_GPIO_EN1.
2458 For green: in both R_GPIO_EN1 and R_GPIO_OUT1. */
2459 leds |= green_leds;
2460 b4xxp_setreg8(b4, R_GPIO_EN1, leds);
2461 hfc_gpio_set(b4, green_leds);
2462
2463 if (b4->blinktimer == 0xff)
2464 b4->blinktimer = -1;
2465 }
2466
2467 static void b4xxp_set_span_led(struct b4xxp *b4, int span, unsigned char val)
2468 {
2469 if (IS_GEN2(b4)) {
2470 unsigned long flags;
2471
2472 if (IS_B230P(b4)) {
2473 b4->ledreg &= ~(0x03 << (span + 1)*2);
2474 b4->ledreg |= (val << (span + 1)*2);
2475 } else {
2476 b4->ledreg &= ~(0x03 << span*2);
2477 b4->ledreg |= (val << span*2);
2478 }
2479
2480 spin_lock_irqsave(&b4->seqlock, flags);
2481 /* Set multiplexer for led R/W */
2482 hfc_gpio_set(b4, 0x10);
2483 hfc_sram_write(b4, b4->ledreg);
2484 spin_unlock_irqrestore(&b4->seqlock, flags);
2485 } else {
2486 int shift, spanmask;
2487
2488 shift = span << 1;
2489 spanmask = ~(0x03 << shift);
2490
2491 b4->ledreg &= spanmask;
2492 b4->ledreg |= (val << shift);
2493 b4xxp_setleds(b4, b4->ledreg);
2494 }
2495 }
2496
2497 static void b4xxp_update_leds(struct b4xxp *b4)
2498 {
2499 int i;
2500 struct b4xxp_span *bspan;
2501
2502 if (b4->numspans == 8) {
2503 /* Use the alternative function for non-Digium HFC-8S cards */
2504 b4xxp_update_leds_hfc_8s(b4);
2505 return;
2506 }
2507
2508 if (!IS_B410P(b4) && !IS_GEN2(b4)) {
2509 /* Use the alternative function for non-Digium HFC-4S cards */
2510 b4xxp_update_leds_hfc(b4);
2511 return;
2512 }
2513
2514 b4->blinktimer++;
2515 for (i=0; i < b4->numspans; i++) {
2516 bspan = &b4->spans[i];
2517
2518 if (bspan->span.flags & DAHDI_FLAG_RUNNING) {
2519 if (bspan->span.alarms) {
2520 if (b4->blinktimer == (led_fader_table[b4->alarmpos] >> 1))
2521 b4xxp_set_span_led(b4, i, LED_RED);
2522 if (b4->blinktimer == 0xf)
2523 b4xxp_set_span_led(b4, i, LED_OFF);
2524 } else if (bspan->span.mainttimer || bspan->span.maintstat) {
2525 if (b4->blinktimer == (led_fader_table[b4->alarmpos] >> 1))
2526 b4xxp_set_span_led(b4, i, LED_GREEN);
2527 if (b4->blinktimer == 0xf)
2528 b4xxp_set_span_led(b4, i, LED_OFF);
2529 } else {
2530 /* No Alarm */
2531 b4xxp_set_span_led(b4, i, LED_GREEN);
2532 }
2533 } else
2534 b4xxp_set_span_led(b4, i, LED_OFF);
2535 }
2536
2537 if (b4->blinktimer == 0xf) {
2538 b4->blinktimer = -1;
2539 b4->alarmpos++;
2540 if (b4->alarmpos >= (sizeof(led_fader_table) / sizeof(led_fader_table[0])))
2541 b4->alarmpos = 0;
2542 }
2543 }
2544
2545 static const char *b4xxp_echocan_name(const struct dahdi_chan *chan)
2546 {
2547 struct b4xxp_span *bspan = container_of(chan->span, struct b4xxp_span,
2548 span);
2549 if (!vpmsupport)
2550 return NULL;
2551
2552 if (IS_B410P(bspan->parent))
2553 return "LASVEGAS2";
2554
2555 if (IS_GEN2(bspan->parent))
2556 return "ZARLINK";
2557
2558 return NULL;
2559 }
2560
2561 static int b4xxp_echocan_create(struct dahdi_chan *chan,
2562 struct dahdi_echocanparams *ecp,
2563 struct dahdi_echocanparam *p,
2564 struct dahdi_echocan_state **ec)
2565 {
2566 struct b4xxp_span *bspan = container_of(chan->span, struct b4xxp_span, span);
2567 int channel;
2568
2569 if (!vpmsupport || !CARD_HAS_EC(bspan->parent))
2570 return -ENODEV;
2571
2572 if (chan->chanpos == 3) {
2573 printk(KERN_WARNING "Cannot enable echo canceller on D channel of span %d; failing request\n", chan->span->offset);
2574 return -EINVAL;
2575 }
2576
2577 if (ecp->param_count > 0) {
2578 printk(KERN_WARNING "wcb4xxp echo canceller does not support parameters; failing request\n");
2579 return -EINVAL;
2580 }
2581
2582 *ec = &bspan->ec[chan->chanpos];
2583 (*ec)->ops = &my_ec_ops;
2584 (*ec)->features = my_ec_features;
2585
2586 if (DBG_EC)
2587 printk("Enabling echo cancellation on chan %d span %d\n", chan->chanpos, chan->span->offset);
2588
2589 if (IS_GEN2(bspan->parent)) {
2590 int group;
2591 int chan_offset = (chan->chanpos % 2) ? 0x00 : 0x20;
2592 int reg;
2593 unsigned long flags;
2594
2595 /* Zarlink has 4 groups of 2 channel echo cancelers */
2596 /* Each channel has it's own individual control reg */
2597 group = bspan->phy_port * 0x40;
2598
2599 spin_lock_irqsave(&bspan->parent->seqlock, flags);
2600 reg = __zl_read(bspan->parent, group + chan_offset);
2601 reg &= ~(1 << 3);
2602 __zl_write(bspan->parent, group + chan_offset, reg);
2603 spin_unlock_irqrestore(&bspan->parent->seqlock, flags);
2604 } else {
2605 channel = (chan->span->offset * 8) + ((chan->chanpos - 1) * 4) + 1;
2606 ec_write(bspan->parent, chan->chanpos - 1, channel, 0x7e);
2607 }
2608
2609 return 0;
2610 }
2611
2612 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
2613 {
2614 struct b4xxp_span *bspan = container_of(chan->span, struct b4xxp_span, span);
2615 int channel;
2616
2617 memset(ec, 0, sizeof(*ec));
2618
2619 if (DBG_EC)
2620 printk("Disabling echo cancellation on chan %d span %d\n", chan->chanpos, chan->span->offset);
2621
2622 if (IS_GEN2(bspan->parent)) {
2623 int group;
2624 int chan_offset = (chan->chanpos % 2) ? 0x00 : 0x20;
2625 int reg;
2626 unsigned long flags;
2627
2628 /* Zarlink has 4 groups of 2 channel echo cancelers */
2629 /* Each channel has it's own individual control reg */
2630 group = bspan->phy_port * 0x40;
2631
2632 spin_lock_irqsave(&bspan->parent->seqlock, flags);
2633 reg = __zl_read(bspan->parent, group + chan_offset);
2634 reg |= (1 << 3);
2635 __zl_write(bspan->parent, group + chan_offset, reg);
2636 spin_unlock_irqrestore(&bspan->parent->seqlock, flags);
2637 } else {
2638 channel = (chan->span->offset * 8) + ((chan->chanpos - 1) * 4) + 1;
2639 ec_write(bspan->parent, chan->chanpos - 1, channel, 0x01);
2640 }
2641 }
2642
2643 /*
2644 * Filesystem and DAHDI interfaces
2645 */
2646 static int b4xxp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
2647 {
2648 switch(cmd) {
2649 default:
2650 return -ENOTTY;
2651 }
2652
2653 return 0;
2654 }
2655
2656 static int b4xxp_startup(struct file *file, struct dahdi_span *span)
2657 {
2658 struct b4xxp_span *bspan = container_of(span, struct b4xxp_span, span);
2659 struct b4xxp *b4 = bspan->parent;
2660
2661 if (!b4->running)
2662 hfc_enable_interrupts(bspan->parent);
2663
2664 return 0;
2665 }
2666
2667 static int b4xxp_shutdown(struct dahdi_span *span)
2668 {
2669 struct b4xxp_span *bspan = container_of(span, struct b4xxp_span, span);
2670
2671 hfc_disable_interrupts(bspan->parent);
2672 return 0;
2673 }
2674
2675 /* resets all the FIFOs for a given span. Disables IRQs for the span FIFOs */
2676 static void b4xxp_reset_span(struct b4xxp_span *bspan)
2677 {
2678 int i;
2679 struct b4xxp *b4 = bspan->parent;
2680
2681 for (i=0; i < 3; i++) {
2682 hfc_reset_fifo_pair(b4, bspan->fifos[i], (i == 2) ? 1 : 0, 1);
2683 }
2684
2685 b4xxp_set_sync_src(b4, b4xxp_find_sync(b4));
2686 }
2687
2688 /* spanconfig for us means to set up the HFC FIFO and channel mapping */
2689 static int b4xxp_spanconfig(struct file *file, struct dahdi_span *span,
2690 struct dahdi_lineconfig *lc)
2691 {
2692 int i;
2693 struct b4xxp_span *bspan = container_of(span, struct b4xxp_span, span);
2694 struct b4xxp *b4 = bspan->parent;
2695
2696 if (DBG)
2697 dev_info(&b4->pdev->dev, "Configuring span %d offset %d to be sync %d\n", span->spanno, span->offset, lc->sync);
2698
2699 if (lc->sync < 0 || lc->sync > b4->numspans) {
2700 dev_info(&b4->pdev->dev,
2701 "Span %d has invalid sync priority (%d), removing "
2702 "from sync source list\n", span->spanno, lc->sync);
2703 lc->sync = 0;
2704 }
2705
2706 /* remove this span number from the current sync sources, if there */
2707 for (i = 0; i < b4->numspans; i++) {
2708 if (b4->spans[i].sync == (span->offset + 1)) {
2709 b4->spans[i].sync = 0;
2710 }
2711 }
2712
2713 if (lc->sync)
2714 b4->spans[lc->sync - 1].sync = (span->offset + 1);
2715
2716 /* B430 sets TE/NT and Termination resistance modes via dahdi_cfg */
2717 if (IS_GEN2(b4)) {
2718 int te_mode, term, reg;
2719 unsigned long flags;
2720
2721 te_mode = (lc->lineconfig & DAHDI_CONFIG_NTTE) ? 0 : 1;
2722 term = (lc->lineconfig & DAHDI_CONFIG_TERM) ? 1 : 0;
2723 dev_info(&b4->pdev->dev,
2724 "Configuring span %d in %s mode with termination resistance %s\n",
2725 bspan->port + 1, (te_mode) ? "TE" : "NT",
2726 (term) ? "ENABLED" : "DISABLED");
2727
2728 if (!te_mode && lc->sync) {
2729 dev_info(&b4->pdev->dev,
2730 "NT Spans cannot be timing sources. Changing priority to 0\n");
2731 lc->sync = 0;
2732 }
2733
2734 /* Setup NT/TE */
2735 /* Bits 7 downto 5 correspond to spans 4-1 */
2736 /* 1 sets NT mode, 0 sets TE mode */
2737 spin_lock_irqsave(&b4->seqlock, flags);
2738 hfc_gpio_set(b4, 0x20);
2739 reg = hfc_sram_read(b4);
2740 hfc_gpio_set(b4, 0x00);
2741
2742 if (te_mode)
2743 reg &= ~(1 << (7 - bspan->phy_port));
2744 else
2745 reg |= (1 << (7 - bspan->phy_port));
2746
2747 /* Setup Termination resistance */
2748 /* Bits 4 downto 0 correspond to spans 4-1 */
2749 /* 1 sets resistance mode, 0 sets no resistance */
2750 if (term)
2751 reg |= (1 << (3 - bspan->phy_port));
2752 else
2753 reg &= ~(1 << (3 - bspan->phy_port));
2754
2755 hfc_gpio_set(b4, 0x20);
2756 hfc_sram_write(b4, reg);
2757 hfc_gpio_set(b4, 0x00);
2758 spin_unlock_irqrestore(&b4->seqlock, flags);
2759
2760 bspan->te_mode = te_mode;
2761 bspan->span.spantype = (bspan->te_mode)
2762 ? SPANTYPE_DIGITAL_BRI_TE
2763 : SPANTYPE_DIGITAL_BRI_NT;
2764 }
2765
2766 hfc_reset_st(bspan);
2767 if (persistentlayer1)
2768 hfc_start_st(bspan);
2769
2770 b4xxp_reset_span(bspan);
2771
2772 /* call startup() manually here, because DAHDI won't call the startup function unless it receives an IOCTL to do so, and dahdi_cfg doesn't. */
2773 b4xxp_startup(file, &bspan->span);
2774
2775 span->flags |= DAHDI_FLAG_RUNNING;
2776
2777 return 0;
2778 }
2779
2780 /* chanconfig for us means to configure the HDLC controller, if appropriate */
2781 static int
2782 b4xxp_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
2783 {
2784 int alreadyrunning;
2785 struct b4xxp *b4 = chan->pvt;
2786 struct b4xxp_span *bspan = &b4->spans[chan->span->offset];
2787 int fifo = bspan->fifos[2];
2788
2789 alreadyrunning = bspan->span.flags & DAHDI_FLAG_RUNNING;
2790
2791 if (DBG_FOPS) {
2792 dev_info(&b4->pdev->dev, "%s channel %d (%s) sigtype %08x\n",
2793 alreadyrunning ? "Reconfigured" : "Configured", chan->channo, chan->name, sigtype);
2794 }
2795
2796 /* (re)configure signalling channel */
2797 if ((sigtype == DAHDI_SIG_HARDHDLC) || (bspan->sigchan == chan)) {
2798 if (DBG_FOPS)
2799 dev_info(&b4->pdev->dev, "%sonfiguring hardware HDLC on %s\n",
2800 ((sigtype == DAHDI_SIG_HARDHDLC) ? "C" : "Unc"), chan->name);
2801
2802 if (alreadyrunning && bspan->sigchan) {
2803 hdlc_stop(b4, fifo);
2804 atomic_set(&bspan->hdlc_pending, 0);
2805 bspan->sigactive = 0;
2806 smp_mb();
2807 bspan->sigchan = NULL;
2808 }
2809
2810 if (sigtype == DAHDI_SIG_HARDHDLC) {
2811 if (hdlc_start(b4, fifo)) {
2812 dev_warn(&b4->pdev->dev, "Error initializing signalling controller\n");
2813 return -1;
2814 }
2815 }
2816
2817 bspan->sigchan = (sigtype == DAHDI_SIG_HARDHDLC) ? chan : NULL;
2818 bspan->sigactive = 0;
2819 atomic_set(&bspan->hdlc_pending, 0);
2820 } else {
2821 /* FIXME: shouldn't I be returning an error? */
2822 }
2823
2824 return 0;
2825 }
2826
2827 static int _b4xxp_open(struct dahdi_chan *chan)
2828 {
2829 struct b4xxp *b4 = chan->pvt;
2830 struct b4xxp_span *bspan = &b4->spans[chan->span->offset];
2831
2832 if (DBG_FOPS && DBG_SPANFILTER) {
2833 dev_info(&b4->pdev->dev, "open() on chan %s (%i/%i)\n",
2834 chan->name, chan->channo, chan->chanpos);
2835 }
2836
2837 hfc_reset_fifo_pair(b4, bspan->fifos[chan->chanpos - 1], 0, 0);
2838 return 0;
2839 }
2840
2841 static int b4xxp_open(struct dahdi_chan *chan)
2842 {
2843 unsigned long flags;
2844 int res;
2845 spin_lock_irqsave(&chan->lock, flags);
2846 res = _b4xxp_open(chan);
2847 spin_unlock_irqrestore(&chan->lock, flags);
2848 return res;
2849 }
2850
2851 static int b4xxp_close(struct dahdi_chan *chan)
2852 {
2853 struct b4xxp *b4 = chan->pvt;
2854 struct b4xxp_span *bspan = &b4->spans[chan->span->offset];
2855
2856 if (DBG_FOPS && DBG_SPANFILTER) {
2857 dev_info(&b4->pdev->dev, "close() on chan %s (%i/%i)\n",
2858 chan->name, chan->channo, chan->chanpos);
2859 }
2860
2861 hfc_reset_fifo_pair(b4, bspan->fifos[chan->chanpos - 1], 1, 1);
2862 return 0;
2863 }
2864
2865 /* DAHDI calls this when it has data it wants to send to the HDLC controller */
2866 static void b4xxp_hdlc_hard_xmit(struct dahdi_chan *chan)
2867 {
2868 struct b4xxp *b4 = chan->pvt;
2869 int span = chan->span->offset;
2870 struct b4xxp_span *bspan = &b4->spans[span];
2871
2872 if ((DBG_FOPS || DBG_HDLC) && DBG_SPANFILTER)
2873 dev_info(&b4->pdev->dev, "hdlc_hard_xmit on chan %s (%i/%i), span=%i\n",
2874 chan->name, chan->channo, chan->chanpos, span + 1);
2875
2876 /*
2877 * increment the hdlc_pending counter and trigger the bottom-half so it
2878 * will be picked up and sent.
2879 */
2880 if (bspan->sigchan == chan) {
2881 atomic_inc(&bspan->hdlc_pending);
2882 }
2883 }
2884
2885 /* internal functions, not specific to the hardware or DAHDI */
2886
2887 static const struct dahdi_span_ops b4xxp_span_ops = {
2888 .owner = THIS_MODULE,
2889 .spanconfig = b4xxp_spanconfig,
2890 .chanconfig = b4xxp_chanconfig,
2891 .startup = b4xxp_startup,
2892 .shutdown = b4xxp_shutdown,
2893 .open = b4xxp_open,
2894 .close = b4xxp_close,
2895 .ioctl = b4xxp_ioctl,
2896 .hdlc_hard_xmit = b4xxp_hdlc_hard_xmit,
2897 .echocan_create = b4xxp_echocan_create,
2898 .echocan_name = b4xxp_echocan_name,
2899 };
2900
2901 /* initialize the span/chan structures. Doesn't touch hardware, although the callbacks might. */
2902 static void init_spans(struct b4xxp *b4)
2903 {
2904 int i, j;
2905 struct b4xxp_span *bspan;
2906 struct dahdi_chan *chan;
2907
2908 /* for each span on the card */
2909 for (i=0; i < b4->numspans; i++) {
2910 bspan = &b4->spans[i];
2911 bspan->parent = b4;
2912
2913 bspan->span.spantype = (bspan->te_mode)
2914 ? SPANTYPE_DIGITAL_BRI_TE
2915 : SPANTYPE_DIGITAL_BRI_NT;
2916 bspan->span.offset = i;
2917 bspan->span.channels = WCB4XXP_CHANNELS_PER_SPAN;
2918 bspan->span.flags = 0;
2919
2920 if (!strcasecmp(companding, "ulaw"))
2921 bspan->span.deflaw = DAHDI_LAW_MULAW;
2922 else
2923 bspan->span.deflaw = DAHDI_LAW_ALAW;
2924 /* For simplicty, we'll accept all line modes since BRI
2925 * ignores this setting anyway.*/
2926 bspan->span.linecompat = DAHDI_CONFIG_AMI |
2927 DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 |
2928 DAHDI_CONFIG_ESF | DAHDI_CONFIG_HDB3 |
2929 DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4 |
2930 DAHDI_CONFIG_NTTE | DAHDI_CONFIG_TERM;
2931
2932 sprintf(bspan->span.name, "B4/%d/%d", b4->cardno, i+1);
2933 sprintf(bspan->span.desc, "B4XXP (PCI) Card %d Span %d", b4->cardno, i+1);
2934
2935 bspan->span.ops = &b4xxp_span_ops;
2936 /* HDLC stuff */
2937 bspan->sigchan = NULL;
2938 bspan->sigactive = 0;
2939
2940 bspan->span.chans = bspan->chans;
2941
2942 /* now initialize each channel in the span */
2943 for (j=0; j < WCB4XXP_CHANNELS_PER_SPAN; j++) {
2944 bspan->chans[j] = &bspan->_chans[j];
2945 chan = bspan->chans[j];
2946 chan->pvt = b4;
2947
2948 sprintf(chan->name, "B4/%d/%d/%d", b4->cardno, i + 1, j + 1);
2949 /* The last channel in the span is the D-channel */
2950 if (j == WCB4XXP_CHANNELS_PER_SPAN - 1) {
2951 chan->sigcap = DAHDI_SIG_HARDHDLC;
2952 } else {
2953 chan->sigcap = DAHDI_SIG_CLEAR | DAHDI_SIG_DACS;
2954 }
2955 chan->chanpos = j + 1;
2956 chan->writechunk = (void *)(bspan->writechunk + j * DAHDI_CHUNKSIZE);
2957 chan->readchunk = (void *)(bspan->readchunk + j * DAHDI_CHUNKSIZE);
2958 }
2959 }
2960 }
2961
2962
2963 static void b4xxp_bottom_half(unsigned long data);
2964
2965 /* top-half interrupt handler */
2966 static irqreturn_t b4xxp_interrupt(int irq, void *dev_id)
2967 {
2968 struct b4xxp *b4 = dev_id;
2969 unsigned char status;
2970 unsigned long flags;
2971 int i;
2972
2973 /* Make sure it's really for us */
2974 status = __pci_in8(b4, R_STATUS);
2975 if (!(status & HFC_INTS))
2976 return IRQ_NONE;
2977
2978 /*
2979 * since the interrupt is for us, read in the FIFO and misc IRQ status registers.
2980 * Don't replace the struct copies; OR in the new bits instead.
2981 * That way if we get behind, we don't lose anything.
2982 * We don't actually do any processing here, we simply flag the bottom-half to do the heavy lifting.
2983 */
2984 local_irq_save(flags);
2985
2986 if (status & V_FR_IRQSTA) {
2987 b4->fifo_irqstatus[0] |= __pci_in8(b4, R_IRQ_FIFO_BL0);
2988 b4->fifo_irqstatus[1] |= __pci_in8(b4, R_IRQ_FIFO_BL1);
2989 b4->fifo_irqstatus[2] |= __pci_in8(b4, R_IRQ_FIFO_BL2);
2990 b4->fifo_irqstatus[3] |= __pci_in8(b4, R_IRQ_FIFO_BL3);
2991 b4->fifo_irqstatus[4] |= __pci_in8(b4, R_IRQ_FIFO_BL4);
2992 b4->fifo_irqstatus[5] |= __pci_in8(b4, R_IRQ_FIFO_BL5);
2993 b4->fifo_irqstatus[6] |= __pci_in8(b4, R_IRQ_FIFO_BL6);
2994 b4->fifo_irqstatus[7] |= __pci_in8(b4, R_IRQ_FIFO_BL7);
2995 }
2996
2997 if (status & V_MISC_IRQSTA) {
2998 b4->misc_irqstatus |= __pci_in8(b4, R_IRQ_MISC);
2999 }
3000
3001 /*
3002 * Well, that was the plan. It appears that I can't do this in the bottom half
3003 * or I start to see data corruption (too long a time between IRQ and tasklet??)
3004 * So, I do the B-channel stuff right here in interrupt context. yuck.
3005 */
3006 if (b4->misc_irqstatus & V_TI_IRQ) {
3007 hfc_poll_fifos(b4);
3008 for (i=0; i < b4->numspans; i++) {
3009 if (b4->spans[i].span.flags & DAHDI_FLAG_RUNNING) {
3010 dahdi_ec_span(&b4->spans[i].span);
3011 dahdi_receive(&b4->spans[i].span);
3012 dahdi_transmit(&b4->spans[i].span);
3013 }
3014 }
3015 }
3016
3017 /* kick off bottom-half handler */
3018 /* tasklet_hi_schedule(&b4->b4xxp_tlet); */
3019 b4xxp_bottom_half((unsigned long)b4);
3020
3021 local_irq_restore(flags);
3022
3023 return IRQ_RETVAL(1);
3024 }
3025
3026
3027 /*
3028 * The bottom half of course does all the heavy lifting for the interrupt.
3029 *
3030 * The original plan was to have the B channel RX FIFO interrupts enabled, and
3031 * to do the actual work here. Since that doesn't seem to work so well, we
3032 * poll the B channel FIFOs right in the interrupt handler and take care of the B
3033 * channel stuff there. The bottom half works for the timer interrupt and D
3034 * channel stuff.
3035 *
3036 * The HFC-4S timer interrupt is used to for several things:
3037 * - Update the S/T state machines, expire their timers, etc.
3038 * - Provide DAHDI's timing source, if so configured
3039 * - Update LEDs
3040 */
3041 static void b4xxp_bottom_half(unsigned long data)
3042 {
3043 struct b4xxp *b4 = (struct b4xxp *)data;
3044 int i, j, k, fifo, fifo_low, fifo_high;
3045 unsigned char b, b2;
3046
3047 if (b4->shutdown)
3048 return;
3049
3050 if (IS_GEN2(b4)) {
3051 unsigned long timeout = jiffies + (HZ/10);
3052 int i;
3053 struct b4xxp_span *bspan;
3054
3055 for (i = 0; i < b4->numspans; i++) {
3056 /* Map the ports from the virtual ports to the fifos */
3057 bspan = &b4->spans[i];
3058 b = (b4->fifo_irqstatus[2] >> (2 * bspan->phy_port));
3059
3060 if (b & V_IRQ_FIFOx_TX) {
3061 while (hdlc_tx_frame(&b4->spans[i])) {
3062 if (time_after(jiffies, timeout)) {
3063 dev_err(&b4->pdev->dev,
3064 "bottom_half timed out\n");
3065 break;
3066 }
3067 }
3068 }
3069
3070 if (b & V_IRQ_FIFOx_RX) {
3071 while (hdlc_rx_frame(&b4->spans[i])) {
3072 if (time_after(jiffies, timeout)) {
3073 dev_err(&b4->pdev->dev,
3074 "bottom_half timed out\n");
3075 break;
3076 }
3077 }
3078 }
3079 }
3080
3081 b4->fifo_irqstatus[2] = 0;
3082 } else {
3083
3084 /* HFC-4S d-chan fifos 8-11 *** HFC-8S d-chan fifos 16-23 */
3085 if (b4->numspans == 8) {
3086 fifo_low = 16;
3087 fifo_high = 23;
3088 } else {
3089 fifo_low = 8;
3090 fifo_high = 11;
3091 }
3092
3093 for (i = 0; i < 8; i++) {
3094 b = b2 = b4->fifo_irqstatus[i];
3095
3096 for (j = 0; j < b4->numspans; j++) {
3097 fifo = i*4 + j;
3098
3099 if (b & V_IRQ_FIFOx_TX) {
3100 if (fifo >= fifo_low && fifo <= fifo_high) {
3101 /* d-chan fifos */
3102 /*
3103 * WOW I don't like this.
3104 * It's bad enough that I have to send a fake frame to get an HDLC TX FIFO interrupt,
3105 * but now, I have to loop until the whole frame is read, or I get RX interrupts
3106 * (even though the chip says HDLC mode gives an IRQ when a *full frame* is received).
3107 * Yuck. It works well, but yuck.
3108 */
3109 do {
3110 k = hdlc_tx_frame(&b4->spans[fifo - fifo_low]);
3111 } while (k);
3112 } else {
3113 if (printk_ratelimit())
3114 dev_warn(&b4->pdev->dev, "Got FIFO TX int from non-d-chan FIFO %d??\n", fifo);
3115 }
3116 }
3117
3118 if (b & V_IRQ_FIFOx_RX) {
3119 if (fifo >= fifo_low && fifo <= fifo_high) { /* dchan fifos */
3120 /*
3121 * I have to loop here until hdlc_rx_frame says there are no more frames waiting.
3122 * for whatever reason, the HFC will not generate another interrupt if there are
3123 * still HDLC frames waiting to be received.
3124 * i.e. I get an int when F1 changes, not when F1 != F2.
3125 */
3126 do {
3127 k = hdlc_rx_frame(&b4->spans[fifo - fifo_low]);
3128 } while (k);
3129 } else {
3130 if (printk_ratelimit())
3131 dev_warn(&b4->pdev->dev, "Got FIFO RX int from non-d-chan FIFO %d??\n", fifo);
3132 }
3133 }
3134
3135 b >>= 2;
3136 }
3137
3138 /* zero the bits we just processed */
3139 b4->fifo_irqstatus[i] &= ~b2;
3140 }
3141 }
3142
3143 /*
3144 * timer interrupt
3145 * every tick (1ms), check the FIFOs and run through the S/T port timers.
3146 * every 100ms or so, look for S/T state machine changes.
3147 */
3148 if (b4->misc_irqstatus & V_TI_IRQ) {
3149
3150 /*
3151 * We should check the FIFOs here, but I'm seeing this tasklet getting scheduled FAR too late to be useful.
3152 * For now, we're handling that in the IRQ handler itself. (ICK!!)
3153 */
3154 b4->ticks++;
3155
3156 hfc_update_st_timers(b4);
3157
3158 b4xxp_update_leds(b4);
3159
3160 /* Poll interface state at 100ms interval */
3161 if (!(b4->ticks % 100)) {
3162 b = b4xxp_getreg8(b4, R_SCI);
3163 if (b) {
3164 for (i=0; i < b4->numspans; i++) {
3165 if (IS_B230P(b4)) {
3166 if (b & (1 << (i+1)))
3167 hfc_handle_state(&b4->spans[1-i]);
3168 } else {
3169 if (b & (1 << i)) {
3170 /* physical spans are reversed for b430 */
3171 if (IS_B430P(b4))
3172 hfc_handle_state(&b4->spans[b4->numspans-1-i]);
3173 else
3174 hfc_handle_state(&b4->spans[i]);
3175 }
3176 }
3177 }
3178 }
3179 }
3180
3181 /* We're supposed to kick DAHDI here, too, but again, seeing too much latency between the interrupt and the bottom-half. */
3182
3183 /* clear the timer interrupt flag. */
3184 b4->misc_irqstatus &= ~V_TI_IRQ;
3185 }
3186
3187 /*
3188 * Check for outgoing HDLC frame requests
3189 * The HFC does not generate TX interrupts when there is room to send, so
3190 * I use an atomic counter that is incremented every time DAHDI wants to send
3191 * a frame, and decremented every time I send a frame. It'd be better if I could
3192 * just use the interrupt handler, but the HFC seems to trigger a FIFO TX IRQ
3193 * only when it has finished sending a frame, not when one can be sent.
3194 */
3195 for (i=0; i < b4->numspans; i++) {
3196 struct b4xxp_span *bspan = &b4->spans[i];
3197
3198 if (atomic_read(&bspan->hdlc_pending)) {
3199 do {
3200 k = hdlc_tx_frame(bspan);
3201 } while (k);
3202 }
3203 }
3204 }
3205
3206
3207 /********************************************************************************* proc stuff *****/
3208
3209 #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
3210 static int b4xxp_proc_read_one(char *buf, struct b4xxp *b4)
3211 {
3212 struct dahdi_chan *chan;
3213 int len, i, j;
3214 char str[80], sBuf[4096];
3215
3216 *sBuf=0;
3217 sprintf(sBuf, "Card %d, PCI identifier %s, IRQ %d\n",
3218 b4->cardno + 1, b4->pdev->dev.bus_id, b4->irq);
3219
3220 strcat(sBuf,"Tx:\n");
3221 for (j=0; j<(b4->numspans * 2) ; j++) { /* B Channels */
3222 for (i=0; i<(b4->numspans * 3) ; i++) { /* All Channels */
3223 chan = b4->spans[i/3].chans[i%3];
3224 sprintf(str, "%02x ", chan->writechunk[j]);
3225 strcat(sBuf, str);
3226 }
3227
3228 strcat(sBuf, "\n");
3229 }
3230
3231 strcat(sBuf, "\nRx:\n");
3232 for (j=0; j < (b4->numspans * 2); j++) { /* B Channels */
3233 for (i=0; i < (b4->numspans * 3); i++) { /* All Channels */
3234 chan = b4->spans[i / 3].chans[i % 3];
3235 sprintf(str, "%02x%c", chan->readchunk[j], (i == 11) ? '\n' : ' ');
3236 strcat(sBuf, str);
3237 }
3238 }
3239
3240 strcat(sBuf, "\nPort states:\n");
3241 for (i=0; i < b4->numspans; i++) {
3242 int state;
3243 char *x;
3244 struct b4xxp_span *s = &b4->spans[i];
3245
3246 state = b4xxp_getreg_ra(b4, R_ST_SEL, s->port, A_ST_RD_STA);
3247 x = hfc_decode_st_state(b4, s, state, 0);
3248 sprintf(str, "%s\n", x);
3249 strcat(sBuf, str);
3250 kfree(x);
3251 }
3252
3253 len = sprintf(buf, "%s\n%s\nTicks: %ld\n", sBuf, str, b4->ticks);
3254 return len;
3255 }
3256
3257 static int b4xxp_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
3258 {
3259 struct b4xxp **b4_cards = data;
3260 char sBuf[256];
3261 int i, len;
3262
3263 len = sprintf(buf, "WCB4XXP Card Information\n");
3264 for (i=0; b4_cards[i] != NULL; i++) {
3265 if (i)
3266 len += sprintf(buf + len, "\n-----\n");
3267 len += b4xxp_proc_read_one(buf + len, b4_cards[i]);
3268 }
3269
3270 *sBuf = 0;
3271 strcat(sBuf, "\n-----\n\nAudio: ");
3272 #ifdef LOOPBACK_SUPPORTED
3273 if (loopback >= 3)
3274 strcat(sBuf, "DAHDI and S/T");
3275 else if (loopback == 2)
3276 strcat(sBuf, "DAHDI");
3277 else if (loopback == 1)
3278 strcat(sBuf, "S/T");
3279 else
3280 strcat(sBuf, "not");
3281 strcat(sBuf, " looped back");
3282 #else
3283 strcat(sBuf, "not looped back");
3284 #endif
3285
3286 if (milliwatt)
3287 strcat(sBuf, ", outgoing S/T replaced with mu-law milliwatt tone");
3288
3289 len += sprintf(buf + len, "%s\n", sBuf);
3290
3291 if (alarmdebounce)
3292 sprintf(sBuf, "Alarms: debounced (%dms)", alarmdebounce);
3293 else
3294 strcpy(sBuf, "Alarms: not debounced");
3295
3296 len += sprintf(buf + len, "%s\nT1 timer period %dms\nT3 timer period %dms\n", sBuf, timer_1_ms, timer_3_ms);
3297
3298 *eof = 1;
3299 return len;
3300 }
3301 #endif /* CREATE_WCB4XXP_PROCFS_ENTRY */
3302
3303 static int b4xxp_startdefaultspan(struct b4xxp *b4)
3304 {
3305 struct dahdi_lineconfig lc = {0,};
3306 return b4xxp_spanconfig(NULL, &b4->spans[0].span, &lc);
3307 }
3308
3309 static void set_ufm(struct b4xxp *b4, int signal)
3310 {
3311 int reg;
3312
3313 hfc_gpio_set(b4, 0x70);
3314 reg = hfc_sram_read(b4);
3315 hfc_gpio_set(b4, 0x00);
3316
3317 hfc_gpio_set(b4, 0x70);
3318 hfc_sram_write(b4, reg | signal);
3319 hfc_gpio_set(b4, 0x00);
3320 }
3321
3322 static void clr_ufm(struct b4xxp *b4, int signal)
3323 {
3324 int reg;
3325
3326 hfc_gpio_set(b4, 0x70);
3327 reg = hfc_sram_read(b4);
3328 hfc_gpio_set(b4, 0x00);
3329
3330 hfc_gpio_set(b4, 0x70);
3331 hfc_sram_write(b4, reg & ~signal);
3332 hfc_gpio_set(b4, 0x00);
3333 }
3334
3335 static unsigned char read_ufm_status(struct b4xxp *b4)
3336 {
3337 unsigned char ret;
3338
3339 hfc_gpio_set(b4, 0x00);
3340 ret = hfc_sram_read(b4);
3341 hfc_gpio_set(b4, 0x00);
3342
3343 return ret;
3344 }
3345
3346 /* Try to read the serial number from 2nd gen devices */
3347 static int read_serial(struct b4xxp *b4, char *serial)
3348 {
3349 unsigned long flags;
3350 int i, j;
3351 int ret = 0;
3352
3353 spin_lock_irqsave(&b4->seqlock, flags);
3354
3355 set_ufm(b4, UFM_ARSHIFT);
3356 set_ufm(b4, UFM_ARCLK);
3357 clr_ufm(b4, UFM_ARDIN);
3358 set_ufm(b4, UFM_DRSHIFT);
3359 set_ufm(b4, UFM_DRCLK);
3360 clr_ufm(b4, UFM_ERASE);
3361 clr_ufm(b4, UFM_PROGRAM);
3362
3363 for (i = 0; i < 255; i++) {
3364 char data = 0;
3365
3366 /* Bang out address */
3367 for (j = 0; j < 9; j++) {
3368 int mask = 0x100 >> j;
3369
3370 if ((i & mask) == 0)
3371 clr_ufm(b4, UFM_ARDIN);
3372 else
3373 set_ufm(b4, UFM_ARDIN);
3374
3375 clr_ufm(b4, UFM_ARCLK);
3376 set_ufm(b4, UFM_ARCLK);
3377 }
3378
3379 /* Latch the address */
3380 clr_ufm(b4, UFM_DRCLK);
3381 clr_ufm(b4, UFM_DRSHIFT);
3382 set_ufm(b4, UFM_DRCLK);
3383 set_ufm(b4, UFM_DRSHIFT);
3384
3385 /* Bang in data */
3386 for (j = 0; j < 8; j++) {
3387 int drdout = (read_ufm_status(b4) & 0x80) >> 7;
3388
3389 clr_ufm(b4, UFM_DRCLK);
3390 set_ufm(b4, UFM_DRCLK);
3391
3392 if (drdout == 1)
3393 data = data + (0x80 >> j);
3394 }
3395
3396 /* Bang out the data padding */
3397 for (j = 0; j < 8; j++) {
3398 clr_ufm(b4, UFM_DRCLK);
3399 set_ufm(b4, UFM_DRCLK);
3400 }
3401
3402 if ((data >= 0x20) && (data < 0x7f)) {
3403 serial[i] = data;
3404 } else if (!i) {
3405 ret = 1;
3406 break;
3407 } else {
3408 break;
3409 }
3410 }
3411
3412 spin_unlock_irqrestore(&b4->seqlock, flags);
3413
3414 return ret;
3415 }
3416
3417 static int __devinit b4xx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3418 {
3419 int x, ret;
3420 struct b4xxp *b4;
3421 struct devtype *dt;
3422
3423 dt = (struct devtype *)(ent->driver_data);
3424 dev_info(&pdev->dev, "probe called for b4xx...\n");
3425
3426 if ((ret = pci_enable_device(pdev)))
3427 goto err_out_disable_pdev;
3428
3429 if ((ret = pci_request_regions(pdev, dt->desc))) {
3430 dev_err(&pdev->dev, "Unable to request regions!\n");
3431 goto err_out_disable_pdev;
3432 }
3433
3434 if (!pdev->irq) { /* we better have an IRQ */
3435 dev_err(&pdev->dev, "Device has no associated IRQ?\n");
3436 ret = -EIO;
3437 goto err_out_release_regions;
3438 }
3439
3440 if (!(b4 = kzalloc(sizeof(struct b4xxp), GFP_KERNEL))) {
3441 dev_err(&pdev->dev, "Couldn't allocate memory for b4xxp structure!\n");
3442 ret = -ENOMEM;
3443 goto err_out_release_regions;
3444 }
3445
3446 /* card found, enabled and main struct allocated. Fill it out. */
3447 b4->variety = dt->desc;
3448 b4->card_type = dt->card_type;
3449 b4->pdev = pdev;
3450 pci_set_drvdata(pdev, b4);
3451
3452 b4->ioaddr = pci_iomap(pdev, 0, 0);
3453 b4->addr = pci_iomap(pdev, 1, 0);
3454 b4->irq = pdev->irq;
3455
3456 spin_lock_init(&b4->reglock);
3457 spin_lock_init(&b4->seqlock);
3458 spin_lock_init(&b4->fifolock);
3459
3460 x = b4xxp_getreg8(b4, R_CHIP_ID);
3461 if ((x != 0xc0) && (x != 0x80)) { /* wrong chip? */
3462 dev_err(&pdev->dev, "Unknown/unsupported controller detected (R_CHIP_ID = 0x%02x)\n", x);
3463 goto err_out_free_mem;
3464 }
3465
3466 /* future proofing */
3467 b4->chiprev = b4xxp_getreg8(b4, R_CHIP_RV);
3468
3469 /* check for various board-specific flags and modify init as necessary */
3470 /*
3471 if (dt->flags & FLAG_XXX)
3472 use_flag_somehow();
3473 */
3474
3475 b4->numspans = dt->ports;
3476 b4->syncspan = -1; /* sync span is unknown */
3477 if (b4->numspans > MAX_SPANS_PER_CARD) {
3478 dev_err(&b4->pdev->dev,
3479 "Driver does not know how to handle a %d span card!\n",
3480 b4->numspans);
3481 goto err_out_free_mem;
3482 }
3483
3484 dev_info(&b4->pdev->dev, "Identified %s (controller rev %d) at %p, IRQ %i\n",
3485 b4->variety, b4->chiprev, b4->ioaddr, b4->irq);
3486
3487 /* look for the next free card structure */
3488 for (x=0; x < MAX_B4_CARDS; x++) {
3489 if (!cards[x])
3490 break;
3491 }
3492
3493 if (x >= MAX_B4_CARDS) {
3494 dev_err(&pdev->dev, "Attempt to register more than %i cards, aborting!\n", MAX_B4_CARDS);
3495 goto err_out_free_mem;
3496 }
3497
3498 /* update the cards array, make sure the b4xxp struct knows where in the array it is */
3499 b4->cardno = x;
3500 cards[x] = b4;
3501
3502 b4xxp_init_stage1(b4);
3503
3504 if (IS_GEN2(b4)) {
3505 int version;
3506 unsigned long flags;
3507 char serial[255];
3508
3509 /* Read and print firmware version */
3510 spin_lock_irqsave(&b4->seqlock, flags);
3511 hfc_gpio_set(b4, 0x60);
3512 version = hfc_sram_read(b4);
3513 spin_unlock_irqrestore(&b4->seqlock, flags);
3514
3515 dev_info(&b4->pdev->dev, "CPLD ver: %x\n", version);
3516
3517 /* Read and print serial number */
3518 if (read_serial(b4, &serial[0]))
3519 dev_info(&b4->pdev->dev,
3520 "Unable to read serial number\n");
3521 else
3522 dev_info(&b4->pdev->dev, "serial: %s\n", serial);
3523 }
3524
3525 create_sysfs_files(b4);
3526
3527 if (request_irq(pdev->irq, b4xxp_interrupt, IRQF_SHARED, "b4xxp", b4)) {
3528 dev_err(&b4->pdev->dev, "Unable to request IRQ %d\n",
3529 pdev->irq);
3530 ret = -EIO;
3531 goto err_out_del_from_card_array;
3532 }
3533
3534 /* initialize the tasklet structure */
3535 /* TODO: perhaps only one tasklet for any number of cards in the system... don't need one per card I don't think. */
3536 tasklet_init(&b4->b4xxp_tlet, b4xxp_bottom_half, (unsigned long)b4);
3537
3538 /* interrupt allocated and tasklet initialized, it's now safe to finish initializing the hardware */
3539 b4xxp_init_stage2(b4);
3540 hfc_init_all_st(b4);
3541
3542 /* initialize the DAHDI structures, and let DAHDI know it has some new hardware to play with */
3543 b4->ddev = dahdi_create_device();
3544 init_spans(b4);
3545
3546 for (x=0; x < b4->numspans; x++) {
3547 struct dahdi_span *const s = &b4->spans[x].span;
3548 list_add_tail(&s->device_node, &b4->ddev->spans);
3549 }
3550
3551 b4->ddev->manufacturer = "Digium";
3552 b4->ddev->devicetype = b4->variety;
3553 b4->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
3554 b4->pdev->bus->number,
3555 PCI_SLOT(b4->pdev->devfn) + 1);
3556 if (!b4->ddev->location) {
3557 ret = -ENOMEM;
3558 goto err_out_del_from_card_array;
3559 }
3560
3561 if (dahdi_register_device(b4->ddev, &b4->pdev->dev)) {
3562 dev_err(&b4->pdev->dev, "Unable to register device.\n");
3563 goto err_out_unreg_spans;
3564 }
3565
3566 dev_info(&b4->pdev->dev, "Did not do the highestorder stuff\n");
3567
3568 ret = b4xxp_startdefaultspan(b4);
3569 if (ret)
3570 goto err_out_unreg_spans;
3571
3572 ret = 0;
3573 return ret;
3574
3575 /* 'x' will have the failing span #. (0-3). We need to unregister everything before it. */
3576 err_out_unreg_spans:
3577 dahdi_unregister_device(b4->ddev);
3578
3579 b4xxp_init_stage1(b4); /* full reset, re-init to "no-irq" state */
3580 free_irq(pdev->irq, b4);
3581
3582 err_out_del_from_card_array:
3583 for (x=0; x < MAX_B4_CARDS; x++) {
3584 if (cards[x] == b4) {
3585 b4->cardno = -1;
3586 cards[x] = NULL;
3587 break;
3588 }
3589 }
3590
3591 if (x >= MAX_B4_CARDS)
3592 dev_err(&pdev->dev, "b4 struct @ %p should be in cards array but isn't?!\n", b4);
3593
3594 err_out_free_mem:
3595 pci_set_drvdata(pdev, NULL);
3596 pci_iounmap(pdev, b4->ioaddr);
3597 pci_iounmap(pdev, b4->addr);
3598 kfree(b4->ddev->location);
3599 dahdi_free_device(b4->ddev);
3600 kfree(b4);
3601
3602 err_out_release_regions:
3603 pci_release_regions(pdev);
3604
3605 err_out_disable_pdev:
3606 pci_disable_device(pdev);
3607
3608 return ret;
3609 }
3610
3611 static void __devexit b4xxp_remove(struct pci_dev *pdev)
3612 {
3613 struct b4xxp *b4 = pci_get_drvdata(pdev);
3614
3615 if (b4) {
3616 b4->shutdown = 1;
3617
3618 dahdi_unregister_device(b4->ddev);
3619
3620 b4xxp_init_stage1(b4);
3621 remove_sysfs_files(b4);
3622 free_irq(pdev->irq, b4);
3623 pci_set_drvdata(pdev, NULL);
3624 pci_iounmap(pdev, b4->ioaddr);
3625 pci_iounmap(pdev, b4->addr);
3626 pci_release_regions(pdev);
3627 pci_disable_device(pdev);
3628
3629 b4->ioaddr = b4->addr = NULL;
3630
3631 tasklet_kill(&b4->b4xxp_tlet);
3632
3633 kfree(b4->ddev->location);
3634 dahdi_free_device(b4->ddev);
3635 kfree(b4);
3636 }
3637
3638 dev_info(&pdev->dev, "Driver unloaded.\n");
3639 return;
3640 }
3641
3642 static DEFINE_PCI_DEVICE_TABLE(b4xx_ids) =
3643 {
3644 {0xd161, 0xb410, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wcb41xp},
3645 {0xd161, 0x8014, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wcb43xp},
3646 {0xd161, 0x8015, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wcb43xp},
3647 {0xd161, 0x8016, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wcb23xp},
3648 {0xd161, 0x8017, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wcb23xp},
3649 {0, }
3650
3651 };
3652
3653 static int b4xx_suspend(struct pci_dev *pdev, pm_message_t state)
3654 {
3655 return -ENOSYS;
3656 }
3657
3658 static struct pci_driver b4xx_driver = {
3659 .name = "wcb4xxp",
3660 .probe = b4xx_probe,
3661 .remove = __devexit_p(b4xxp_remove),
3662 .id_table = b4xx_ids,
3663 .suspend = b4xx_suspend,
3664 };
3665
3666 static int __init b4xx_init(void)
3667 {
3668
3669 #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
3670 if (!(myproc = create_proc_read_entry(PROCFS_NAME, 0444, NULL,
3671 b4xxp_proc_read, cards))) {
3672 printk(KERN_ERR "%s: ERROR: Could not initialize /proc/%s\n",THIS_MODULE->name, PROCFS_NAME);
3673 }
3674 #endif
3675 if (pci_register_driver(&b4xx_driver))
3676 return -ENODEV;
3677
3678 return 0;
3679 }
3680
3681 static void __exit b4xx_exit(void)
3682 {
3683 #ifdef CREATE_WCB4XXP_PROCFS_ENTRY
3684 remove_proc_entry(PROCFS_NAME, NULL);
3685 #endif
3686 pci_unregister_driver(&b4xx_driver);
3687 }
3688
3689 module_param(debug, int, S_IRUGO | S_IWUSR);
3690 module_param(spanfilter, int, S_IRUGO | S_IWUSR);
3691 #ifdef LOOPBACK_SUPPORTED
3692 module_param(loopback, int, S_IRUGO | S_IWUSR);
3693 #endif
3694 module_param(milliwatt, int, S_IRUGO | S_IWUSR);
3695 module_param(pedanticpci, int, S_IRUGO);
3696 module_param(teignorered, int, S_IRUGO | S_IWUSR);
3697 module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
3698 module_param(vpmsupport, int, S_IRUGO);
3699 module_param(timer_1_ms, int, S_IRUGO | S_IWUSR);
3700 module_param(timer_3_ms, int, S_IRUGO | S_IWUSR);
3701 module_param(companding, charp, S_IRUGO);
3702 module_param(persistentlayer1, int, S_IRUGO | S_IWUSR);
3703
3704 MODULE_PARM_DESC(debug, "bitmap: 1=general 2=dtmf 4=regops 8=fops 16=ec 32=st state 64=hdlc 128=alarm");
3705 MODULE_PARM_DESC(spanfilter, "debug filter for spans. bitmap: 1=port 1, 2=port 2, 4=port 3, 8=port 4");
3706 #ifdef LOOPBACK_SUPPORTED
3707 MODULE_PARM_DESC(loopback, "TODO: bitmap: 1=loop back S/T port 2=loop back DAHDI");
3708 #endif
3709 MODULE_PARM_DESC(milliwatt, "1=replace outgoing S/T data with mu-law milliwatt");
3710 MODULE_PARM_DESC(pedanticpci, "1=disable PCI back-to-back transfers and flush all PCI writes immediately");
3711 MODULE_PARM_DESC(teignorered, "1=ignore (do not inform DAHDI) if a red alarm exists in TE mode");
3712 MODULE_PARM_DESC(alarmdebounce, "msec to wait before set/clear alarm condition");
3713 MODULE_PARM_DESC(vpmsupport, "1=enable hardware EC, 0=disable hardware EC");
3714 MODULE_PARM_DESC(timer_1_ms, "NT: msec to wait for link activation, TE: unused.");
3715 MODULE_PARM_DESC(timer_3_ms, "TE: msec to wait for link activation, NT: unused.");
3716 MODULE_PARM_DESC(companding, "Change the companding to \"alaw\" or \"ulaw\""\
3717 "(alaw by default)");
3718
3719 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
3720 MODULE_DESCRIPTION("B410P & Similars multi-port BRI module driver.");
3721 MODULE_LICENSE("GPL");
3722
3723 MODULE_DEVICE_TABLE(pci, b4xx_ids);
3724
3725 module_init(b4xx_init);
3726 module_exit(b4xx_exit);