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