Debian Codegraph @

drivers/dahdi/wcte11xp.c

raw
1 /*
2 * Digium, Inc. Wildcard TE110P T1/PRI card Driver
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Matthew Fredrickson <[email protected]>
6 * William Meadows <[email protected]>
7 *
8 * Copyright (C) 2004, Digium, Inc.
9 *
10 * All rights reserved.
11 */
12
13 /*
14 * See http://www.asterisk.org for more information about
15 * the Asterisk project. Please do not directly contact
16 * any of the maintainers of this project for assistance;
17 * the project provides a web site, mailing lists and IRC
18 * channels for your use.
19 *
20 * This program is free software, distributed under the terms of
21 * the GNU General Public License Version 2 as published by the
22 * Free Software Foundation. See the LICENSE file included with
23 * this program for more details.
24 */
25
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/usb.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33 #include <linux/moduleparam.h>
34 #include <linux/slab.h>
35
36 #include <dahdi/kernel.h>
37
38 /* XXX: fix this */
39 #include "wct4xxp/wct4xxp.h" /* For certain definitions */
40
41 #define WC_MAX_CARDS 32
42
43 /*
44 #define TEST_REGS
45 */
46
47 /* Define to get more attention-grabbing but slightly more I/O using
48 alarm status */
49 #define FANCY_ALARM
50
51 /* Define to enable the V2.1 errata register settings */
52 #if 0
53 #define TRUST_INFINEON_ERRATA
54 #endif
55
56 #define DELAY 0x0 /* 30 = 15 cycles, 10 = 8 cycles, 0 = 3 cycles */
57
58 #define WC_CNTL 0x00
59 #define WC_OPER 0x01
60 #define WC_AUXC 0x02
61 #define WC_AUXD 0x03
62 #define WC_MASK0 0x04
63 #define WC_MASK1 0x05
64 #define WC_INTSTAT 0x06
65
66 #define WC_DMAWS 0x08
67 #define WC_DMAWI 0x0c
68 #define WC_DMAWE 0x10
69 #define WC_DMARS 0x18
70 #define WC_DMARI 0x1c
71 #define WC_DMARE 0x20
72 #define WC_CURPOS 0x24
73
74 #define WC_SERC 0x2d
75 #define WC_FSCDELAY 0x2f
76
77 #define WC_USERREG 0xc0
78
79 #define WC_CLOCK 0x0
80 #define WC_LEDTEST 0x1
81 #define WC_VERSION 0x2
82
83 /* Offset between transmit and receive */
84 #define WC_OFFSET 4
85
86 #define BIT_CS (1 << 7)
87 #define BIT_ADDR (0xf << 3)
88
89 #define BIT_LED1 (1 << 0)
90 #define BIT_LED0 (1 << 1)
91 #define BIT_TEST (1 << 2)
92
93 #define FLAG_STARTED (1 << 0)
94 #define FLAG_NMF (1 << 1)
95 #define FLAG_SENDINGYELLOW (1 << 2)
96 #define FLAG_FALC12 (1 << 3)
97
98 #define TYPE_T1 1 /* is a T1 card */
99 #define TYPE_E1 2 /* is an E1 card */
100
101 static int chanmap_t1[] =
102 { 2,1,0,
103 6,5,4,
104 10,9,8,
105 14,13,12,
106 18,17,16,
107 22,21,20,
108 26,25,24,
109 30,29,28 };
110
111 static int chanmap_e1[] =
112 { 2,1,0,
113 7,6,5,4,
114 11,10,9,8,
115 15,14,13,12,
116 19,18,17,16,
117 23,22,21,20,
118 27,26,25,24,
119 31,30,29,28 };
120
121 static int chanmap_e1uc[] =
122 { 3,2,1,0,
123 7,6,5,4,
124 11,10,9,8,
125 15,14,13,12,
126 19,18,17,16,
127 23,22,21,20,
128 27,26,25,24,
129 31,30,29,28 };
130
131
132 #ifdef FANCY_ALARM
133 static int altab[] = {
134 0, 0, 0, 1, 2, 3, 4, 6, 8, 9, 11, 13, 16, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 31, 32, 31, 31, 30, 29, 28, 27, 25, 23, 22, 20, 18, 16, 13, 11, 9, 8, 6, 4, 3, 2, 1, 0, 0,
135 };
136 #endif
137
138 struct t1 {
139 struct pci_dev *dev;
140 spinlock_t lock;
141 int spantype;
142 int spanflags; /* Span flags */
143 unsigned char txsigs[16]; /* Copy of tx sig registers */
144 int num;
145 int alarmcount; /* How much red alarm we've seen */
146 int alarmdebounce;
147 /* Our offset for finding channel 1 */
148 int offset;
149 char *variety;
150 unsigned int intcount;
151 int usecount;
152 int clocktimeout;
153 int sync;
154 int dead;
155 int blinktimer;
156 int alarmtimer;
157 int checktiming; /* Set >0 to cause the timing source to be checked */
158 int loopupcnt;
159 int loopdowncnt;
160 int miss;
161 int misslast;
162 int *chanmap;
163 #ifdef FANCY_ALARM
164 int alarmpos;
165 #endif
166 unsigned char ledtestreg;
167 unsigned char outbyte;
168 unsigned long ioaddr;
169 unsigned short canary;
170 /* T1 signalling */
171 dma_addr_t readdma;
172 dma_addr_t writedma;
173 volatile unsigned char *writechunk; /* Double-word aligned write memory */
174 volatile unsigned char *readchunk; /* Double-word aligned read memory */
175 unsigned char ec_chunk1[32][DAHDI_CHUNKSIZE];
176 unsigned char ec_chunk2[32][DAHDI_CHUNKSIZE];
177 unsigned char tempo[33];
178 struct dahdi_device *ddev;
179 struct dahdi_span span; /* Span */
180 struct dahdi_chan *chans[32]; /* Channels */
181 };
182
183 #define CANARY 0xca1e
184
185 static int debug = 0; /* doesnt do anything */
186 static int j1mode = 0;
187 static int alarmdebounce = 0;
188 static int loopback = 0;
189 static int clockextra = 0;
190 static int t1e1override = -1;
191 static int unchannelized = 0;
192
193 static struct t1 *cards[WC_MAX_CARDS];
194
195 static inline void start_alarm(struct t1 *wc)
196 {
197 #ifdef FANCY_ALARM
198 wc->alarmpos = 0;
199 #endif
200 wc->blinktimer = 0;
201 }
202
203 static inline void stop_alarm(struct t1 *wc)
204 {
205 #ifdef FANCY_ALARM
206 wc->alarmpos = 0;
207 #endif
208 wc->blinktimer = 0;
209 }
210
211 static inline void __select_framer(struct t1 *wc, int reg)
212 {
213 /* Top four bits of address from AUX 6-3 */
214 wc->outbyte &= ~BIT_CS;
215 wc->outbyte &= ~BIT_ADDR;
216 wc->outbyte |= (reg & 0xf0) >> 1;
217 outb(wc->outbyte, wc->ioaddr + WC_AUXD);
218 }
219
220 static inline void __select_control(struct t1 *wc)
221 {
222 if (!(wc->outbyte & BIT_CS)) {
223 wc->outbyte |= BIT_CS;
224 outb(wc->outbyte, wc->ioaddr + WC_AUXD);
225 }
226 }
227
228 static int _t1xxp_open(struct dahdi_chan *chan)
229 {
230 struct t1 *wc = chan->pvt;
231 if (wc->dead)
232 return -ENODEV;
233 wc->usecount++;
234
235 return 0;
236 }
237
238 static int t1xxp_open(struct dahdi_chan *chan)
239 {
240 unsigned long flags;
241 int res;
242 spin_lock_irqsave(&chan->lock, flags);
243 res = _t1xxp_open(chan);
244 spin_unlock_irqrestore(&chan->lock, flags);
245 return res;
246 }
247
248 static int __control_set_reg(struct t1 *wc, int reg, unsigned char val)
249 {
250 __select_control(wc);
251 outb(val, wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
252 return 0;
253 }
254
255 static int control_set_reg(struct t1 *wc, int reg, unsigned char val)
256 {
257 unsigned long flags;
258 int res;
259 spin_lock_irqsave(&wc->lock, flags);
260 res = __control_set_reg(wc, reg, val);
261 spin_unlock_irqrestore(&wc->lock, flags);
262 return res;
263 }
264
265 static int __control_get_reg(struct t1 *wc, int reg)
266 {
267 unsigned char res;
268 /* The following makes UTTERLY no sense, but what was happening
269 was that reads in some cases were not actually happening
270 on the physical bus. Why, we dunno. But in debugging, we found
271 that writing before reading (in this case to an unused position)
272 seems to get rid of the problem */
273 __control_set_reg(wc,3,0x69); /* do magic here */
274 /* now get the read byte from the Xilinx part */
275 res = inb(wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
276 return res;
277 }
278
279 static int control_get_reg(struct t1 *wc, int reg)
280 {
281 unsigned long flags;
282 int res;
283 spin_lock_irqsave(&wc->lock, flags);
284 res = __control_get_reg(wc, reg);
285 spin_unlock_irqrestore(&wc->lock, flags);
286 return res;
287 }
288
289 static inline unsigned int __t1_framer_in(struct t1 *wc, const unsigned int reg)
290 {
291 unsigned char res;
292 __select_framer(wc, reg);
293 /* Get value */
294 res = inb(wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
295 return res;
296 #if 0
297 unsigned int ret;
298 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff));
299 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff) | ( 1 << 10) | WC_LREAD);
300 ret = __t1_pci_in(wc, WC_LDATA);
301 __t1_pci_out(wc, WC_LADDR, 0);
302 return ret & 0xff;
303 #endif
304 }
305
306 static inline unsigned int t1_framer_in(struct t1 *wc, const unsigned int addr)
307 {
308 unsigned long flags;
309 unsigned int ret;
310 spin_lock_irqsave(&wc->lock, flags);
311 ret = __t1_framer_in(wc, addr);
312 spin_unlock_irqrestore(&wc->lock, flags);
313 return ret;
314
315 }
316
317 static inline void __t1_framer_out(struct t1 *wc, const unsigned int reg, const unsigned int val)
318 {
319 if (debug > 1)
320 printk(KERN_DEBUG "Writing %02x to address %02x\n", val, reg);
321 __select_framer(wc, reg);
322 /* Send address */
323 outb(val, wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
324 #if 0
325 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff));
326 __t1_pci_out(wc, WC_LDATA, value);
327 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff) | (1 << 10));
328 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff) | (1 << 10) | WC_LWRITE);
329 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff) | (1 << 10));
330 __t1_pci_out(wc, WC_LADDR, (unit << 8) | (addr & 0xff));
331 __t1_pci_out(wc, WC_LADDR, 0);
332 if (debug) printk(KERN_DEBUG "Write complete\n");
333 #endif
334 #if 0
335 { unsigned int tmp;
336 tmp = t1_framer_in(wc, unit, addr);
337 if (tmp != value) {
338 printk(KERN_DEBUG "Expected %d from unit %d register %d but got %d instead\n", value, unit, addr, tmp);
339 } }
340 #endif
341 }
342
343 static inline void t1_framer_out(struct t1 *wc, const unsigned int addr, const unsigned int value)
344 {
345 unsigned long flags;
346 spin_lock_irqsave(&wc->lock, flags);
347 __t1_framer_out(wc, addr, value);
348 spin_unlock_irqrestore(&wc->lock, flags);
349 }
350
351 static void t1xxp_release(struct t1 *wc)
352 {
353 unsigned int x;
354
355 dahdi_unregister_device(wc->ddev);
356 for (x = 0; x < (wc->spantype == TYPE_E1 ? 31 : 24); x++) {
357 kfree(wc->chans[x]);
358 }
359 kfree(wc->ddev->location);
360 dahdi_free_device(wc->ddev);
361 kfree(wc);
362 printk(KERN_INFO "Freed a Wildcard\n");
363 }
364
365 static int t1xxp_close(struct dahdi_chan *chan)
366 {
367 struct t1 *wc = chan->pvt;
368
369 wc->usecount--;
370 /* If we're dead, release us now */
371 if (!wc->usecount && wc->dead)
372 t1xxp_release(wc);
373 return 0;
374 }
375
376 static void t1xxp_enable_interrupts(struct t1 *wc)
377 {
378 /* Clear interrupts */
379 outb(0xff, wc->ioaddr + WC_INTSTAT);
380 /* Enable interrupts (we care about all of them) */
381 outb(0x3c /* 0x3f */, wc->ioaddr + WC_MASK0);
382 /* No external interrupts */
383 outb(0x00, wc->ioaddr + WC_MASK1);
384 if (debug) printk(KERN_DEBUG "Enabled interrupts!\n");
385 }
386
387 static void t1xxp_start_dma(struct t1 *wc)
388 {
389 /* Reset Master and TDM */
390 outb(DELAY | 0x0f, wc->ioaddr + WC_CNTL);
391 set_current_state(TASK_INTERRUPTIBLE);
392 schedule_timeout(1);
393 outb(DELAY | 0x01, wc->ioaddr + WC_CNTL);
394 outb(0x01, wc->ioaddr + WC_OPER);
395 if (debug) printk(KERN_DEBUG "Started DMA\n");
396 outb(0x03, wc->ioaddr + WC_OPER);
397 outb(0x01, wc->ioaddr + WC_OPER);
398 }
399
400 static void __t1xxp_stop_dma(struct t1 *wc)
401 {
402 outb(0x00, wc->ioaddr + WC_OPER);
403 }
404
405 static void __t1xxp_disable_interrupts(struct t1 *wc)
406 {
407 outb(0x00, wc->ioaddr + WC_MASK0);
408 outb(0x00, wc->ioaddr + WC_MASK1);
409 }
410
411 static void __t1xxp_set_clear(struct t1 *wc)
412 {
413 int i,j;
414 unsigned short val=0;
415 for (i=0;i<24;i++) {
416 j = (i/8);
417 if (wc->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
418 val |= 1 << (7 - (i % 8));
419 if ((i % 8)==7) {
420 if (debug > 1)
421 printk(KERN_DEBUG "Putting %d in register %02x\n",
422 val, 0x2f + j);
423 __t1_framer_out(wc, 0x2f + j, val);
424 val = 0;
425 }
426 }
427 }
428
429 static int t1xxp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
430 {
431 struct t4_regs regs;
432 int x;
433 struct t1 *wc;
434 switch(cmd) {
435 case WCT4_GET_REGS:
436 wc = chan->pvt;
437 for (x=0;x<NUM_PCI;x++)
438 #if 1
439 regs.pci[x] = (inb(wc->ioaddr + (x << 2))) |
440 (inb(wc->ioaddr + (x << 2) + 1) << 8) |
441 (inb(wc->ioaddr + (x << 2) + 2) << 16) |
442 (inb(wc->ioaddr + (x << 2) + 3) << 24);
443 #else
444 regs.pci[x] = (inb(wc->ioaddr + x));
445 #endif
446
447 for (x=0;x<NUM_REGS;x++)
448 regs.regs[x] = t1_framer_in(wc, x);
449 if (copy_to_user((__user void *)data, &regs, sizeof(regs)))
450 return -EFAULT;
451 break;
452 default:
453 return -ENOTTY;
454 }
455 return 0;
456 }
457
458 static inline struct t1 *t1_from_span(struct dahdi_span *span)
459 {
460 return container_of(span, struct t1, span);
461 }
462
463 static int t1xxp_maint(struct dahdi_span *span, int cmd)
464 {
465 struct t1 *wc = t1_from_span(span);
466
467 if (wc->spantype == TYPE_E1) {
468 switch(cmd) {
469 case DAHDI_MAINT_NONE:
470 printk(KERN_INFO "XXX Turn off local and remote loops E1 XXX\n");
471 break;
472 case DAHDI_MAINT_LOCALLOOP:
473 printk(KERN_INFO "XXX Turn on local loopback E1 XXX\n");
474 break;
475 case DAHDI_MAINT_REMOTELOOP:
476 printk(KERN_INFO "XXX Turn on remote loopback E1 XXX\n");
477 break;
478 case DAHDI_MAINT_LOOPUP:
479 printk(KERN_INFO "XXX Send loopup code E1 XXX\n");
480 break;
481 case DAHDI_MAINT_LOOPDOWN:
482 printk(KERN_INFO "XXX Send loopdown code E1 XXX\n");
483 break;
484 default:
485 printk(KERN_NOTICE "TE110P: Unknown E1 maint command: %d\n", cmd);
486 break;
487 }
488 } else {
489 switch(cmd) {
490 case DAHDI_MAINT_NONE:
491 printk(KERN_INFO "XXX Turn off local and remote loops T1 XXX\n");
492 t1_framer_out(wc, 0x21, 0x40);
493 break;
494 case DAHDI_MAINT_LOCALLOOP:
495 printk(KERN_INFO "XXX Turn on local loop and no remote loop XXX\n");
496 break;
497 case DAHDI_MAINT_REMOTELOOP:
498 printk(KERN_INFO "XXX Turn on remote loopup XXX\n");
499 break;
500 case DAHDI_MAINT_LOOPUP:
501 t1_framer_out(wc, 0x21, 0x50); /* FMR5: Nothing but RBS mode */
502 break;
503 case DAHDI_MAINT_LOOPDOWN:
504 t1_framer_out(wc, 0x21, 0x60); /* FMR5: Nothing but RBS mode */
505 break;
506 default:
507 printk(KERN_NOTICE "TE110P: Unknown T1 maint command: %d\n", cmd);
508 break;
509 }
510 }
511 return 0;
512 }
513
514 static int t1xxp_rbsbits(struct dahdi_chan *chan, int bits)
515 {
516 u_char m,c;
517 int n,b;
518 struct t1 *wc = chan->pvt;
519 unsigned long flags;
520
521 if(debug > 1) printk(KERN_DEBUG "Setting bits to %d on channel %s\n", bits, chan->name);
522 spin_lock_irqsave(&wc->lock, flags);
523 if (wc->spantype == TYPE_E1) { /* do it E1 way */
524 if (chan->chanpos == 16) {
525 spin_unlock_irqrestore(&wc->lock, flags);
526 return 0;
527 }
528 n = chan->chanpos - 1;
529 if (chan->chanpos > 15) n--;
530 b = (n % 15);
531 c = wc->txsigs[b];
532 m = (n / 15) << 2; /* nibble selector */
533 c &= (0xf << m); /* keep the other nibble */
534 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
535 wc->txsigs[b] = c;
536 /* output them to the chip */
537 __t1_framer_out(wc,0x71 + b,c);
538 } else if (wc->span.lineconfig & DAHDI_CONFIG_D4) {
539 n = chan->chanpos - 1;
540 b = (n/4);
541 c = wc->txsigs[b];
542 m = ((3 - (n % 4)) << 1); /* nibble selector */
543 c &= ~(0x3 << m); /* keep the other nibble */
544 c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
545 wc->txsigs[b] = c;
546 /* output them to the chip */
547 __t1_framer_out(wc,0x70 + b,c);
548 __t1_framer_out(wc,0x70 + b + 6,c);
549 } else if (wc->span.lineconfig & DAHDI_CONFIG_ESF) {
550 n = chan->chanpos - 1;
551 b = (n/2);
552 c = wc->txsigs[b];
553 m = ((n % 2) << 2); /* nibble selector */
554 c &= (0xf << m); /* keep the other nibble */
555 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
556 wc->txsigs[b] = c;
557 /* output them to the chip */
558 __t1_framer_out(wc,0x70 + b,c);
559 }
560 spin_unlock_irqrestore(&wc->lock, flags);
561 if (debug > 1)
562 printk(KERN_DEBUG "Finished setting RBS bits\n");
563 return 0;
564 }
565
566 static void t1_check_sigbits(struct t1 *wc)
567 {
568 int a,i,rxs;
569 unsigned long flags;
570
571 if (!(wc->span.flags & DAHDI_FLAG_RUNNING))
572 return;
573
574 spin_lock_irqsave(&wc->lock, flags);
575
576 if (wc->spantype == TYPE_E1) {
577 for (i = 0; i < 15; i++) {
578 a = __t1_framer_in(wc, 0x71 + i);
579 /* Get high channel in low bits */
580 rxs = (a & 0xf);
581 if (!(wc->span.chans[i+16]->sig & DAHDI_SIG_CLEAR)) {
582 if (wc->span.chans[i+16]->rxsig != rxs) {
583 spin_unlock_irqrestore(&wc->lock, flags);
584 dahdi_rbsbits(wc->span.chans[i+16], rxs);
585 spin_lock_irqsave(&wc->lock, flags);
586 }
587 }
588 rxs = (a >> 4) & 0xf;
589 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
590 if (wc->span.chans[i]->rxsig != rxs) {
591 spin_unlock_irqrestore(&wc->lock, flags);
592 dahdi_rbsbits(wc->span.chans[i], rxs);
593 spin_lock_irqsave(&wc->lock, flags);
594 }
595 }
596 }
597 } else if (wc->span.lineconfig & DAHDI_CONFIG_D4) {
598 for (i = 0; i < 24; i+=4) {
599 a = __t1_framer_in(wc, 0x70 + (i>>2));
600 /* Get high channel in low bits */
601 rxs = (a & 0x3) << 2;
602 if (!(wc->span.chans[i+3]->sig & DAHDI_SIG_CLEAR)) {
603 if (wc->span.chans[i+3]->rxsig != rxs) {
604 spin_unlock_irqrestore(&wc->lock, flags);
605 dahdi_rbsbits(wc->span.chans[i+3], rxs);
606 spin_lock_irqsave(&wc->lock, flags);
607 }
608 }
609 rxs = (a & 0xc);
610 if (!(wc->span.chans[i+2]->sig & DAHDI_SIG_CLEAR)) {
611 if (wc->span.chans[i+2]->rxsig != rxs) {
612 spin_unlock_irqrestore(&wc->lock, flags);
613 dahdi_rbsbits(wc->span.chans[i+2], rxs);
614 spin_lock_irqsave(&wc->lock, flags);
615 }
616 }
617 rxs = (a >> 2) & 0xc;
618 if (!(wc->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
619 if (wc->span.chans[i+1]->rxsig != rxs) {
620 spin_unlock_irqrestore(&wc->lock, flags);
621 dahdi_rbsbits(wc->span.chans[i+1], rxs);
622 spin_lock_irqsave(&wc->lock, flags);
623 }
624 }
625 rxs = (a >> 4) & 0xc;
626 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
627 if (wc->span.chans[i]->rxsig != rxs) {
628 spin_unlock_irqrestore(&wc->lock, flags);
629 dahdi_rbsbits(wc->span.chans[i], rxs);
630 spin_lock_irqsave(&wc->lock, flags);
631 }
632 }
633 }
634 } else {
635 for (i = 0; i < 24; i+=2) {
636 a = __t1_framer_in(wc, 0x70 + (i>>1));
637 /* Get high channel in low bits */
638 rxs = (a & 0xf);
639 if (!(wc->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
640 if (wc->span.chans[i+1]->rxsig != rxs) {
641 spin_unlock_irqrestore(&wc->lock, flags);
642 dahdi_rbsbits(wc->span.chans[i+1], rxs);
643 spin_lock_irqsave(&wc->lock, flags);
644 }
645 }
646 rxs = (a >> 4) & 0xf;
647 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
648 if (wc->span.chans[i]->rxsig != rxs) {
649 spin_unlock_irqrestore(&wc->lock, flags);
650 dahdi_rbsbits(wc->span.chans[i], rxs);
651 spin_lock_irqsave(&wc->lock, flags);
652 }
653 }
654 }
655 }
656 spin_unlock_irqrestore(&wc->lock, flags);
657 }
658
659 static void t4_serial_setup(struct t1 *wc)
660 {
661 printk(KERN_INFO "TE110P: Setting up global serial parameters for %s %s\n",
662 wc->spantype == TYPE_E1 ? (unchannelized ? "Unchannelized E1" : "E1") : "T1",
663 wc->spanflags & FLAG_FALC12 ? "FALC V1.2" : "FALC V2.2");
664 t1_framer_out(wc, 0x85, 0xe0); /* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from channel 0 */
665 t1_framer_out(wc, 0x08, 0x05); /* IPC: Interrupt push/pull active low */
666 if (wc->spanflags & FLAG_FALC12) {
667 t1_framer_out(wc, 0x92, 0x00);
668 t1_framer_out(wc, 0x93, 0x58);
669 t1_framer_out(wc, 0x94, 0xd2);
670 t1_framer_out(wc, 0x95, 0xc2);
671 t1_framer_out(wc, 0x96, 0x03);
672 t1_framer_out(wc, 0x97, 0x10);
673 } else {
674 /* Global clocks (8.192 Mhz CLK) */
675 t1_framer_out(wc, 0x92, 0x00);
676 t1_framer_out(wc, 0x93, 0x18);
677 t1_framer_out(wc, 0x94, 0xfb);
678 t1_framer_out(wc, 0x95, 0x0b);
679 t1_framer_out(wc, 0x96, 0x00);
680 t1_framer_out(wc, 0x97, 0x0b);
681 t1_framer_out(wc, 0x98, 0xdb);
682 t1_framer_out(wc, 0x99, 0xdf);
683 }
684 /* Configure interrupts */
685 t1_framer_out(wc, 0x46, 0x40); /* GCR: Interrupt on Activation/Deactivation of AIX, LOS */
686
687 /* Configure system interface */
688 t1_framer_out(wc, 0x3e, 0x02); /* SIC1: 4.096 Mhz clock/bus, double buffer receive / transmit, byte interleaved */
689 t1_framer_out(wc, 0x3f, 0x00); /* SIC2: No FFS, no center receive eliastic buffer, phase 0 */
690 t1_framer_out(wc, 0x40, 0x04); /* SIC3: Edges for capture */
691 t1_framer_out(wc, 0x44, 0x30); /* CMR1: RCLK is at 8.192 Mhz dejittered */
692 t1_framer_out(wc, 0x45, 0x00); /* CMR2: We provide sync and clock for tx and rx. */
693 t1_framer_out(wc, 0x22, 0x00); /* XC0: Normal operation of Sa-bits */
694 t1_framer_out(wc, 0x23, 0x04); /* XC1: 0 offset */
695 t1_framer_out(wc, 0x24, 0x07); /* RC0: Just shy of 255 */
696 if (wc->spanflags & FLAG_FALC12)
697 t1_framer_out(wc, 0x25, 0x04); /* RC1: The rest of RC0 */
698 else
699 t1_framer_out(wc, 0x25, 0x05); /* RC1: The rest of RC0 */
700
701 /* Configure ports */
702 t1_framer_out(wc, 0x80, 0x00); /* PC1: SPYR/SPYX input on RPA/XPA */
703 t1_framer_out(wc, 0x81, 0x22); /* PC2: RMFB/XSIG output/input on RPB/XPB */
704 t1_framer_out(wc, 0x82, 0x65); /* PC3: Some unused stuff */
705 t1_framer_out(wc, 0x83, 0x35); /* PC4: Some more unused stuff */
706 t1_framer_out(wc, 0x84, 0x31); /* PC5: XMFS active low, SCLKR is input, RCLK is output */
707 t1_framer_out(wc, 0x86, 0x03); /* PC6: CLK1 is Tx Clock output, CLK2 is 8.192 Mhz from DCO-R */
708 t1_framer_out(wc, 0x3b, 0x00); /* Clear LCR1 */
709 printk(KERN_INFO "TE110P: Successfully initialized serial bus for card\n");
710 }
711
712 static void __t1_configure_t1(struct t1 *wc, int lineconfig, int txlevel)
713 {
714 unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
715 char *framing, *line;
716 int mytxlevel;
717 if ((txlevel > 7) || (txlevel < 4))
718 mytxlevel = 0;
719 else
720 mytxlevel = txlevel - 4;
721 fmr1 = 0x1c; /* FMR1: Mode 0, T1 mode, CRC on for ESF, 2.048 Mhz system data rate, no XAIS */
722 fmr2 = 0x22; /* FMR2: no payload loopback, auto send yellow alarm */
723 if (loopback)
724 fmr2 |= 0x4;
725
726 if (j1mode)
727 fmr4 = 0x1c;
728 else
729 fmr4 = 0x0c; /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
730
731
732 lim2 = 0x21; /* LIM2: 50% peak is a "1", Advanced Loss recovery */
733 lim2 |= (mytxlevel << 6); /* LIM2: Add line buildout */
734 __t1_framer_out(wc, 0x1d, fmr1);
735 __t1_framer_out(wc, 0x1e, fmr2);
736
737 /* Configure line interface */
738 if (lineconfig & DAHDI_CONFIG_AMI) {
739 line = "AMI";
740 fmr0 = 0xa0;
741 } else {
742 line = "B8ZS";
743 fmr0 = 0xf0;
744 }
745 if (lineconfig & DAHDI_CONFIG_D4) {
746 framing = "D4";
747 } else {
748 framing = "ESF";
749 fmr4 |= 0x2;
750 fmr2 |= 0xc0;
751 }
752 __t1_framer_out(wc, 0x1c, fmr0);
753
754 __t1_framer_out(wc, 0x20, fmr4);
755 __t1_framer_out(wc, 0x21, 0x40); /* FMR5: Enable RBS mode */
756
757 __t1_framer_out(wc, 0x37, 0xf8); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
758 __t1_framer_out(wc, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
759
760 __t1_framer_out(wc, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
761 __t1_framer_out(wc, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
762
763 __t1_framer_out(wc, 0x3a, lim2); /* LIM2: 50% peak amplitude is a "1" */
764 __t1_framer_out(wc, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
765 __t1_framer_out(wc, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
766
767 if (j1mode)
768 __t1_framer_out(wc, 0x24, 0x80); /* J1 overide */
769
770 /* Generate pulse mask for T1 */
771 switch(mytxlevel) {
772 case 3:
773 __t1_framer_out(wc, 0x26, 0x07); /* XPM0 */
774 __t1_framer_out(wc, 0x27, 0x01); /* XPM1 */
775 __t1_framer_out(wc, 0x28, 0x00); /* XPM2 */
776 break;
777 case 2:
778 __t1_framer_out(wc, 0x26, 0x8c); /* XPM0 */
779 __t1_framer_out(wc, 0x27, 0x11); /* XPM1 */
780 __t1_framer_out(wc, 0x28, 0x01); /* XPM2 */
781 break;
782 case 1:
783 __t1_framer_out(wc, 0x26, 0x8c); /* XPM0 */
784 __t1_framer_out(wc, 0x27, 0x01); /* XPM1 */
785 __t1_framer_out(wc, 0x28, 0x00); /* XPM2 */
786 break;
787 case 0:
788 default:
789 __t1_framer_out(wc, 0x26, 0xd7); /* XPM0 */
790 __t1_framer_out(wc, 0x27, 0x22); /* XPM1 */
791 __t1_framer_out(wc, 0x28, 0x01); /* XPM2 */
792 break;
793 }
794 printk(KERN_INFO "TE110P: Span configured for %s/%s\n", framing, line);
795 }
796
797 static void __t1_configure_e1(struct t1 *wc, int lineconfig)
798 {
799 unsigned int fmr2, fmr1, fmr0;
800 unsigned int cas = 0;
801 char *crc4 = "";
802 char *framing, *line;
803 fmr1 = 0x44; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
804 fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of multiframe recovery, no payload loopback */
805 if (unchannelized)
806 fmr2 |= 0x30;
807 if (loopback)
808 fmr2 |= 0x4;
809 if (lineconfig & DAHDI_CONFIG_CRC4) {
810 fmr1 |= 0x08; /* CRC4 transmit */
811 fmr2 |= 0xc0; /* CRC4 receive */
812 crc4 = "/CRC4";
813 }
814 __t1_framer_out(wc, 0x1d, fmr1);
815 __t1_framer_out(wc, 0x1e, fmr2);
816
817 /* Configure line interface */
818 if (lineconfig & DAHDI_CONFIG_AMI) {
819 line = "AMI";
820 fmr0 = 0xa0;
821 } else {
822 line = "HDB3";
823 fmr0 = 0xf0;
824 }
825 if (lineconfig & DAHDI_CONFIG_CCS) {
826 framing = "CCS";
827 } else {
828 framing = "CAS";
829 cas = 0x40;
830 }
831 __t1_framer_out(wc, 0x1c, fmr0);
832
833 if (unchannelized)
834 __t1_framer_out(wc, 0x1f, 0x40);
835
836 __t1_framer_out(wc, 0x37, 0xf0 /*| 0x6 */ ); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
837 __t1_framer_out(wc, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
838
839 __t1_framer_out(wc, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
840 __t1_framer_out(wc, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
841
842 /* Condition receive line interface for E1 after reset */
843 __t1_framer_out(wc, 0xbb, 0x17);
844 __t1_framer_out(wc, 0xbc, 0x55);
845 __t1_framer_out(wc, 0xbb, 0x97);
846 __t1_framer_out(wc, 0xbb, 0x11);
847 __t1_framer_out(wc, 0xbc, 0xaa);
848 __t1_framer_out(wc, 0xbb, 0x91);
849 __t1_framer_out(wc, 0xbb, 0x12);
850 __t1_framer_out(wc, 0xbc, 0x55);
851 __t1_framer_out(wc, 0xbb, 0x92);
852 __t1_framer_out(wc, 0xbb, 0x0c);
853 __t1_framer_out(wc, 0xbb, 0x00);
854 __t1_framer_out(wc, 0xbb, 0x8c);
855
856 __t1_framer_out(wc, 0x3a, 0x20); /* LIM2: 50% peak amplitude is a "1" */
857 __t1_framer_out(wc, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
858 __t1_framer_out(wc, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
859
860 __t1_framer_out(wc, 0x20, 0x9f); /* XSW: Spare bits all to 1 */
861 if (unchannelized)
862 __t1_framer_out(wc, 0x21, 0x3c);
863 else
864 __t1_framer_out(wc, 0x21, 0x1c|cas); /* XSP: E-bit set when async. AXS auto, XSIF to 1 */
865
866
867 /* Generate pulse mask for E1 */
868 __t1_framer_out(wc, 0x26, 0x54); /* XPM0 */
869 __t1_framer_out(wc, 0x27, 0x02); /* XPM1 */
870 __t1_framer_out(wc, 0x28, 0x00); /* XPM2 */
871 printk(KERN_INFO "TE110P: Span configured for %s/%s%s\n", framing, line, crc4);
872 }
873
874 static void t1xxp_framer_start(struct t1 *wc, struct dahdi_span *span)
875 {
876 int alreadyrunning = wc->span.flags & DAHDI_FLAG_RUNNING;
877 unsigned long flags;
878
879 spin_lock_irqsave(&wc->lock, flags);
880
881 if (wc->spantype == TYPE_E1) { /* if this is an E1 card */
882 __t1_configure_e1(wc, span->lineconfig);
883 } else { /* is a T1 card */
884 __t1_configure_t1(wc, span->lineconfig, span->txlevel);
885 __t1xxp_set_clear(wc);
886 }
887
888 if (!alreadyrunning)
889 wc->span.flags |= DAHDI_FLAG_RUNNING;
890
891 spin_unlock_irqrestore(&wc->lock, flags);
892 }
893
894
895 static int t1xxp_startup(struct file *file, struct dahdi_span *span)
896 {
897 struct t1 *wc = t1_from_span(span);
898
899 int i,alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
900
901 /* initialize the start value for the entire chunk of last ec buffer */
902 for(i = 0; i < span->channels; i++)
903 {
904 memset(wc->ec_chunk1[i],
905 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
906 memset(wc->ec_chunk2[i],
907 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
908 }
909
910 /* Reset framer with proper parameters and start */
911 t1xxp_framer_start(wc, span);
912 printk(KERN_INFO "Calling startup (flags is %lu)\n", span->flags);
913
914 if (!alreadyrunning) {
915 /* Only if we're not already going */
916 t1xxp_enable_interrupts(wc);
917 t1xxp_start_dma(wc);
918 span->flags |= DAHDI_FLAG_RUNNING;
919 }
920 return 0;
921 }
922
923 static int t1xxp_shutdown(struct dahdi_span *span)
924 {
925 struct t1 *wc = t1_from_span(span);
926 unsigned long flags;
927
928 spin_lock_irqsave(&wc->lock, flags);
929 __t1xxp_stop_dma(wc);
930 __t1xxp_disable_interrupts(wc);
931 span->flags &= ~DAHDI_FLAG_RUNNING;
932 spin_unlock_irqrestore(&wc->lock, flags);
933 return 0;
934 }
935
936
937 static int
938 t1xxp_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
939 {
940 struct t1 *wc = chan->pvt;
941 unsigned long flags;
942 int alreadyrunning = chan->span->flags & DAHDI_FLAG_RUNNING;
943
944 spin_lock_irqsave(&wc->lock, flags);
945
946 if (alreadyrunning && (wc->spantype != TYPE_E1))
947 __t1xxp_set_clear(wc);
948
949 spin_unlock_irqrestore(&wc->lock, flags);
950 return 0;
951 }
952
953 static int
954 t1xxp_spanconfig(struct file *file, struct dahdi_span *span,
955 struct dahdi_lineconfig *lc)
956 {
957 struct t1 *wc = t1_from_span(span);
958
959 /* Do we want to SYNC on receive or not */
960 wc->sync = (lc->sync) ? 1 : 0;
961 /* If already running, apply changes immediately */
962 if (span->flags & DAHDI_FLAG_RUNNING)
963 return t1xxp_startup(file, span);
964
965 return 0;
966 }
967
968 static const struct dahdi_span_ops t1xxp_span_ops = {
969 .owner = THIS_MODULE,
970 .startup = t1xxp_startup,
971 .shutdown = t1xxp_shutdown,
972 .rbsbits = t1xxp_rbsbits,
973 .maint = t1xxp_maint,
974 .open = t1xxp_open,
975 .close = t1xxp_close,
976 .spanconfig = t1xxp_spanconfig,
977 .chanconfig = t1xxp_chanconfig,
978 .ioctl = t1xxp_ioctl,
979 };
980
981 static int t1xxp_software_init(struct t1 *wc)
982 {
983 int x;
984 /* Find position */
985 for (x=0;x<WC_MAX_CARDS;x++) {
986 if (!cards[x]) {
987 cards[x] = wc;
988 break;
989 }
990 }
991 if (x >= WC_MAX_CARDS)
992 return -1;
993
994 wc->ddev = dahdi_create_device();
995
996 t4_serial_setup(wc);
997 wc->num = x;
998 sprintf(wc->span.name, "WCT1/%d", wc->num);
999 snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Card %d", wc->variety, wc->num);
1000 wc->ddev->manufacturer = "Digium";
1001 wc->ddev->devicetype = wc->variety;
1002 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
1003 wc->dev->bus->number,
1004 PCI_SLOT(wc->dev->devfn) + 1);
1005 if (!wc->ddev->location)
1006 return -ENOMEM;
1007
1008 if (wc->spantype == TYPE_E1) {
1009 if (unchannelized)
1010 wc->span.channels = 32;
1011 else
1012 wc->span.channels = 31;
1013 wc->span.deflaw = DAHDI_LAW_ALAW;
1014 wc->span.spantype = SPANTYPE_DIGITAL_E1;
1015 wc->span.linecompat = DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
1016 } else {
1017 wc->span.channels = 24;
1018 wc->span.deflaw = DAHDI_LAW_MULAW;
1019 wc->span.spantype = SPANTYPE_DIGITAL_T1;
1020 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
1021 }
1022 wc->span.chans = wc->chans;
1023 wc->span.flags = DAHDI_FLAG_RBS;
1024 for (x=0;x<wc->span.channels;x++) {
1025 sprintf(wc->chans[x]->name, "WCT1/%d/%d", wc->num, x + 1);
1026 wc->chans[x]->sigcap = DAHDI_SIG_EM | DAHDI_SIG_CLEAR | DAHDI_SIG_EM_E1 |
1027 DAHDI_SIG_FXSLS | DAHDI_SIG_FXSGS | DAHDI_SIG_MTP2 |
1028 DAHDI_SIG_FXSKS | DAHDI_SIG_FXOLS | DAHDI_SIG_DACS_RBS |
1029 DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_SF;
1030 wc->chans[x]->pvt = wc;
1031 wc->chans[x]->chanpos = x + 1;
1032 }
1033 wc->span.ops = &t1xxp_span_ops;
1034 list_add_tail(&wc->span.device_node, &wc->ddev->spans);
1035 if (dahdi_register_device(wc->ddev, &wc->dev->dev)) {
1036 printk(KERN_NOTICE "Unable to register span with DAHDI\n");
1037 return -1;
1038 }
1039 return 0;
1040 }
1041
1042 static inline void __handle_leds(struct t1 *wc)
1043 {
1044 int oldreg;
1045
1046 if (wc->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE)) {
1047 /* Red/Blue alarm */
1048 wc->blinktimer++;
1049 #ifdef FANCY_ALARM
1050 if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
1051 wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
1052 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
1053 }
1054 if (wc->blinktimer >= 0xf) {
1055 wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
1056 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
1057 wc->blinktimer = -1;
1058 wc->alarmpos++;
1059 if (wc->alarmpos >= (sizeof(altab) / sizeof(altab[0])))
1060 wc->alarmpos = 0;
1061 }
1062 #else
1063 if (wc->blinktimer == 160) {
1064 wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
1065 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
1066 } else if (wc->blinktimer == 480) {
1067 wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
1068 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
1069 wc->blinktimer = 0;
1070 }
1071 #endif
1072 } else if (wc->span.alarms & DAHDI_ALARM_YELLOW) {
1073 /* Yellow Alarm */
1074 if (!(wc->blinktimer % 2))
1075 wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
1076 else
1077 wc->ledtestreg = (wc->ledtestreg | BIT_LED0) & ~BIT_LED1;
1078 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
1079 } else {
1080 /* No Alarm */
1081 oldreg = wc->ledtestreg;
1082 if (wc->span.maintstat != DAHDI_MAINT_NONE)
1083 wc->ledtestreg |= BIT_TEST;
1084 else
1085 wc->ledtestreg &= ~BIT_TEST;
1086 if (wc->span.flags & DAHDI_FLAG_RUNNING)
1087 wc->ledtestreg = (wc->ledtestreg | BIT_LED0) & ~BIT_LED1;
1088 else
1089 wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
1090 if (oldreg != wc->ledtestreg)
1091 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
1092 }
1093 }
1094
1095 static void t1xxp_transmitprep(struct t1 *wc, int ints)
1096 {
1097 volatile unsigned char *txbuf;
1098 int x,y;
1099 int pos;
1100 if (ints & 0x04 /* 0x01 */) {
1101 /* We just finished sending the first buffer, start filling it
1102 now */
1103 txbuf = wc->writechunk;
1104 } else {
1105 /* Just finished sending second buffer, fill it now */
1106 txbuf = wc->writechunk + 32 * DAHDI_CHUNKSIZE;
1107 }
1108 dahdi_transmit(&wc->span);
1109 for (x=0;x<wc->offset;x++)
1110 txbuf[x] = wc->tempo[x];
1111 for (y=0;y<DAHDI_CHUNKSIZE;y++) {
1112 for (x=0;x<wc->span.channels;x++) {
1113 pos = y * 32 + wc->chanmap[x] + wc->offset;
1114 /* Put channel number as outgoing data */
1115 if (pos < 32 * DAHDI_CHUNKSIZE)
1116 txbuf[pos] = wc->chans[x]->writechunk[y];
1117 else
1118 wc->tempo[pos - 32 * DAHDI_CHUNKSIZE] = wc->chans[x]->writechunk[y];
1119 }
1120 }
1121 }
1122
1123 static void t1xxp_receiveprep(struct t1 *wc, int ints)
1124 {
1125 volatile unsigned char *rxbuf;
1126 volatile unsigned int *canary;
1127 int x;
1128 int y;
1129 unsigned int oldcan;
1130 if (ints & 0x04) {
1131 /* Just received first buffer */
1132 rxbuf = wc->readchunk;
1133 canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 64 - 4);
1134 } else {
1135 rxbuf = wc->readchunk + DAHDI_CHUNKSIZE * 32;
1136 canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 32 - 4);
1137 }
1138 oldcan = *canary;
1139 if (((oldcan & 0xffff0000) >> 16) != CANARY) {
1140 /* Check top part */
1141 if (debug) printk(KERN_DEBUG "Expecting top %04x, got %04x\n", CANARY, (oldcan & 0xffff0000) >> 16);
1142 wc->ddev->irqmisses++;
1143 } else if ((oldcan & 0xffff) != ((wc->canary - 1) & 0xffff)) {
1144 if (debug) printk(KERN_DEBUG "Expecting bottom %d, got %d\n", wc->canary - 1, oldcan & 0xffff);
1145 wc->ddev->irqmisses++;
1146 }
1147 for (y=0;y<DAHDI_CHUNKSIZE;y++) {
1148 for (x=0;x<wc->span.channels;x++) {
1149 /* XXX Optimize, remove * and + XXX */
1150 /* Must map received channels into appropriate data */
1151 wc->chans[x]->readchunk[y] =
1152 rxbuf[32 * y + ((wc->chanmap[x] + WC_OFFSET + wc->offset) & 0x1f)];
1153 }
1154 if (wc->spantype != TYPE_E1) {
1155 for (x=3;x<32;x+=4) {
1156 if (rxbuf[32 * y + ((x + WC_OFFSET) & 0x1f)] == 0x7f) {
1157 if (wc->offset != (x-3)) {
1158 /* Resync */
1159 control_set_reg(wc, WC_CLOCK, 0x06 | wc->sync | clockextra);
1160 wc->clocktimeout = 100;
1161 #if 1
1162 if (debug) printk(KERN_DEBUG "T1: Lost our place, resyncing\n");
1163 #endif
1164 }
1165 }
1166 }
1167 } else if (!unchannelized) {
1168 if (!wc->clocktimeout && !wc->span.alarms) {
1169 if ((rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)] & 0x7f) != 0x1b) {
1170 if (wc->miss) {
1171 if (debug) printk(KERN_DEBUG "Double miss (%d, %d)...\n", wc->misslast, rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)]);
1172 control_set_reg(wc, WC_CLOCK, 0x06 | wc->sync | clockextra);
1173 wc->clocktimeout = 100;
1174 } else {
1175 wc->miss = 1;
1176 wc->misslast = rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)];
1177 }
1178 } else {
1179 wc->miss = 0;
1180 }
1181 } else {
1182 wc->miss = 0;
1183 }
1184 }
1185 }
1186 /* Store the next canary */
1187 canary = (unsigned int *)(rxbuf + DAHDI_CHUNKSIZE * 32 - 4);
1188 *canary = (wc->canary++) | (CANARY << 16);
1189 for (x=0;x<wc->span.channels;x++) {
1190 dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->ec_chunk2[x]);
1191 memcpy(wc->ec_chunk2[x],wc->ec_chunk1[x],DAHDI_CHUNKSIZE);
1192 memcpy(wc->ec_chunk1[x],wc->chans[x]->writechunk,DAHDI_CHUNKSIZE);
1193 }
1194 dahdi_receive(&wc->span);
1195 }
1196
1197 static void t1_check_alarms(struct t1 *wc)
1198 {
1199 unsigned char c,d;
1200 int alarms;
1201 int x,j;
1202 unsigned long flags;
1203
1204 if (!(wc->span.flags & DAHDI_FLAG_RUNNING))
1205 return;
1206
1207 spin_lock_irqsave(&wc->lock, flags);
1208
1209 c = __t1_framer_in(wc, 0x4c);
1210 if (wc->spanflags & FLAG_FALC12)
1211 d = __t1_framer_in(wc, 0x4f);
1212 else
1213 d = __t1_framer_in(wc, 0x4d);
1214
1215 /* Assume no alarms */
1216 alarms = 0;
1217
1218 /* And consider only carrier alarms */
1219 wc->span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
1220
1221 if (wc->spantype == TYPE_E1) {
1222 if (c & 0x04) {
1223 /* No multiframe found, force RAI high after 400ms only if
1224 we haven't found a multiframe since last loss
1225 of frame */
1226 if (!(wc->spanflags & FLAG_NMF)) {
1227 __t1_framer_out(wc, 0x20, 0x9f | 0x20); /* LIM0: Force RAI High */
1228 wc->spanflags |= FLAG_NMF;
1229 printk(KERN_DEBUG "NMF workaround on!\n");
1230 }
1231 __t1_framer_out(wc, 0x1e, 0xc3); /* Reset to CRC4 mode */
1232 __t1_framer_out(wc, 0x1c, 0xf2); /* Force Resync */
1233 __t1_framer_out(wc, 0x1c, 0xf0); /* Force Resync */
1234 } else if (!(c & 0x02)) {
1235 if ((wc->spanflags & FLAG_NMF)) {
1236 __t1_framer_out(wc, 0x20, 0x9f); /* LIM0: Clear forced RAI */
1237 wc->spanflags &= ~FLAG_NMF;
1238 printk(KERN_DEBUG "NMF workaround off!\n");
1239 }
1240 }
1241 } else {
1242 /* Detect loopup code if we're not sending one */
1243 if ((!wc->span.mainttimer) && (d & 0x08)) {
1244 /* Loop-up code detected */
1245 if ((wc->loopupcnt++ > 80) && (wc->span.maintstat != DAHDI_MAINT_REMOTELOOP)) {
1246 __t1_framer_out(wc, 0x36, 0x08); /* LIM0: Disable any local loop */
1247 __t1_framer_out(wc, 0x37, 0xf6 ); /* LIM1: Enable remote loop */
1248 wc->span.maintstat = DAHDI_MAINT_REMOTELOOP;
1249 }
1250 } else
1251 wc->loopupcnt = 0;
1252 /* Same for loopdown code */
1253 if ((!wc->span.mainttimer) && (d & 0x10)) {
1254 /* Loop-down code detected */
1255 if ((wc->loopdowncnt++ > 80) && (wc->span.maintstat == DAHDI_MAINT_REMOTELOOP)) {
1256 __t1_framer_out(wc, 0x36, 0x08); /* LIM0: Disable any local loop */
1257 __t1_framer_out(wc, 0x37, 0xf0 ); /* LIM1: Disable remote loop */
1258 wc->span.maintstat = DAHDI_MAINT_NONE;
1259 }
1260 } else
1261 wc->loopdowncnt = 0;
1262 }
1263
1264 if (wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
1265 for (x=0,j=0;x < wc->span.channels;x++)
1266 if ((wc->span.chans[x]->flags & DAHDI_FLAG_OPEN) ||
1267 dahdi_have_netdev(wc->span.chans[x]))
1268 j++;
1269 if (!j)
1270 alarms |= DAHDI_ALARM_NOTOPEN;
1271 }
1272
1273 if (c & 0xa0) {
1274 if (wc->alarmcount >= alarmdebounce) {
1275 if (!unchannelized)
1276 alarms |= DAHDI_ALARM_RED;
1277 } else
1278 wc->alarmcount++;
1279 } else
1280 wc->alarmcount = 0;
1281 if (c & 0x4)
1282 alarms |= DAHDI_ALARM_BLUE;
1283
1284 if (((!wc->span.alarms) && alarms) ||
1285 (wc->span.alarms && (!alarms)))
1286 wc->checktiming = 1;
1287
1288 /* Keep track of recovering */
1289 if ((!alarms) && wc->span.alarms)
1290 wc->alarmtimer = DAHDI_ALARMSETTLE_TIME;
1291 if (wc->alarmtimer)
1292 alarms |= DAHDI_ALARM_RECOVER;
1293
1294 /* If receiving alarms, go into Yellow alarm state */
1295 if (alarms && !(wc->spanflags & FLAG_SENDINGYELLOW)) {
1296 unsigned char fmr4;
1297 #if 1
1298 printk(KERN_INFO "wcte1xxp: Setting yellow alarm\n");
1299 #endif
1300 /* We manually do yellow alarm to handle RECOVER and NOTOPEN, otherwise it's auto anyway */
1301 fmr4 = __t1_framer_in(wc, 0x20);
1302 __t1_framer_out(wc, 0x20, fmr4 | 0x20);
1303 wc->spanflags |= FLAG_SENDINGYELLOW;
1304 } else if ((!alarms) && (wc->spanflags & FLAG_SENDINGYELLOW)) {
1305 unsigned char fmr4;
1306 #if 1
1307 printk(KERN_INFO "wcte1xxp: Clearing yellow alarm\n");
1308 #endif
1309 /* We manually do yellow alarm to handle RECOVER */
1310 fmr4 = __t1_framer_in(wc, 0x20);
1311 __t1_framer_out(wc, 0x20, fmr4 & ~0x20);
1312 wc->spanflags &= ~FLAG_SENDINGYELLOW;
1313 }
1314
1315 /* Re-check the timing source when we enter/leave alarm, not withstanding
1316 yellow alarm */
1317 if ((c & 0x10) && !unchannelized)
1318 alarms |= DAHDI_ALARM_YELLOW;
1319 if (wc->span.mainttimer || wc->span.maintstat)
1320 alarms |= DAHDI_ALARM_LOOPBACK;
1321 wc->span.alarms = alarms;
1322 spin_unlock_irqrestore(&wc->lock, flags);
1323 dahdi_alarm_notify(&wc->span);
1324 }
1325
1326
1327 static void t1_do_counters(struct t1 *wc)
1328 {
1329 unsigned long flags;
1330
1331 spin_lock_irqsave(&wc->lock, flags);
1332 if (wc->alarmtimer) {
1333 if (!--wc->alarmtimer) {
1334 wc->span.alarms &= ~(DAHDI_ALARM_RECOVER);
1335 spin_unlock_irqrestore(&wc->lock, flags);
1336 dahdi_alarm_notify(&wc->span);
1337 spin_lock_irqsave(&wc->lock, flags);
1338 }
1339 }
1340 spin_unlock_irqrestore(&wc->lock, flags);
1341 }
1342
1343 DAHDI_IRQ_HANDLER(t1xxp_interrupt)
1344 {
1345 struct t1 *wc = dev_id;
1346 unsigned char ints;
1347 unsigned long flags;
1348 int x;
1349
1350 ints = inb(wc->ioaddr + WC_INTSTAT);
1351 if (!ints)
1352 return IRQ_NONE;
1353
1354 local_irq_save(flags);
1355
1356 outb(ints, wc->ioaddr + WC_INTSTAT);
1357
1358 if (!wc->intcount) {
1359 if (debug) printk(KERN_DEBUG "Got interrupt: 0x%04x\n", ints);
1360 }
1361 wc->intcount++;
1362
1363 if (wc->clocktimeout && !--wc->clocktimeout)
1364 control_set_reg(wc, WC_CLOCK, 0x04 | wc->sync | clockextra);
1365
1366 if (ints & 0x0f) {
1367 t1xxp_receiveprep(wc, ints);
1368 t1xxp_transmitprep(wc, ints);
1369 }
1370 spin_lock(&wc->lock);
1371
1372 #if 1
1373 __handle_leds(wc);
1374 #endif
1375
1376 spin_unlock(&wc->lock);
1377
1378 /* Count down timers */
1379 t1_do_counters(wc);
1380
1381 /* Do some things that we don't have to do very often */
1382 x = wc->intcount & 15 /* 63 */;
1383 switch(x) {
1384 case 0:
1385 case 1:
1386 break;
1387 case 2:
1388 t1_check_sigbits(wc);
1389 break;
1390 case 4:
1391 /* Check alarms 1/4 as frequently */
1392 if (!(wc->intcount & 0x30))
1393 t1_check_alarms(wc);
1394 break;
1395 }
1396
1397 if (ints & 0x10)
1398 printk(KERN_NOTICE "PCI Master abort\n");
1399
1400 if (ints & 0x20)
1401 printk(KERN_NOTICE "PCI Target abort\n");
1402
1403 local_irq_restore(flags);
1404 return IRQ_RETVAL(1);
1405 }
1406
1407 static int t1xxp_hardware_init(struct t1 *wc)
1408 {
1409 unsigned int falcver;
1410 unsigned int x;
1411 /* Hardware PCI stuff */
1412 /* Reset chip and registers */
1413 outb(DELAY | 0x0e, wc->ioaddr + WC_CNTL);
1414 /* Set all outputs to 0 */
1415 outb(0x00, wc->ioaddr + WC_AUXD);
1416 /* Set all to outputs except AUX1 (TDO). */
1417 outb(0xfd, wc->ioaddr + WC_AUXC);
1418 /* Configure the serial port: double clock, 20ns width, no inversion,
1419 MSB first */
1420 outb(0xc8, wc->ioaddr + WC_SERC);
1421
1422 /* Internally delay FSC by one */
1423 outb(0x01, wc->ioaddr + WC_FSCDELAY);
1424
1425 /* Back to normal, with automatic DMA wrap around */
1426 outb(DELAY | 0x01, wc->ioaddr + WC_CNTL);
1427
1428 /* Make sure serial port and DMA are out of reset */
1429 outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, WC_CNTL);
1430
1431 /* Setup DMA Addresses */
1432 /* Start at writedma */
1433 outl(wc->writedma, wc->ioaddr + WC_DMAWS); /* Write start */
1434 /* First frame */
1435 outl(wc->writedma + DAHDI_CHUNKSIZE * 32 - 4, wc->ioaddr + WC_DMAWI); /* Middle (interrupt) */
1436 /* Second frame */
1437 outl(wc->writedma + DAHDI_CHUNKSIZE * 32 * 2 - 4, wc->ioaddr + WC_DMAWE); /* End */
1438
1439 outl(wc->readdma, wc->ioaddr + WC_DMARS); /* Read start */
1440 /* First frame */
1441 outl(wc->readdma + DAHDI_CHUNKSIZE * 32 - 4, wc->ioaddr + WC_DMARI); /* Middle (interrupt) */
1442 /* Second frame */
1443 outl(wc->readdma + DAHDI_CHUNKSIZE * 32 * 2 - 4, wc->ioaddr + WC_DMARE); /* End */
1444
1445 if (debug) printk(KERN_DEBUG "Setting up DMA (write/read = %08lx/%08lx)\n", (long)wc->writedma, (long)wc->readdma);
1446
1447 if (t1e1override > -1) {
1448 if (t1e1override)
1449 wc->spantype = TYPE_E1;
1450 else
1451 wc->spantype = TYPE_T1;
1452 } else {
1453 if (control_get_reg(wc, WC_CLOCK) & 0x20)
1454 wc->spantype = TYPE_T1;
1455 else
1456 wc->spantype = TYPE_E1;
1457 }
1458
1459 /* Check out the controller */
1460 if (debug) printk(KERN_DEBUG "Controller version: %02x\n", control_get_reg(wc, WC_VERSION));
1461
1462
1463 control_set_reg(wc, WC_LEDTEST, 0x00);
1464
1465 if (wc->spantype == TYPE_E1) {
1466 if (unchannelized)
1467 wc->chanmap = chanmap_e1uc;
1468 else
1469 wc->chanmap = chanmap_e1;
1470 } else
1471 wc->chanmap = chanmap_t1;
1472 /* Setup clock appropriately */
1473 control_set_reg(wc, WC_CLOCK, 0x06 | wc->sync | clockextra);
1474 wc->clocktimeout = 100;
1475
1476 /* Perform register test on FALC */
1477 for (x=0;x<256;x++) {
1478 t1_framer_out(wc, 0x14, x);
1479 if ((falcver = t1_framer_in(wc, 0x14)) != x)
1480 printk(KERN_DEBUG "Wrote '%x' but read '%x'\n", x, falcver);
1481 }
1482
1483 t1_framer_out(wc, 0x4a, 0xaa);
1484 falcver = t1_framer_in(wc ,0x4a);
1485 printk(KERN_INFO "FALC version: %08x\n", falcver);
1486 if (!falcver)
1487 wc->spanflags |= FLAG_FALC12;
1488
1489
1490 start_alarm(wc);
1491 return 0;
1492
1493 }
1494
1495 static int __devinit t1xxp_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1496 {
1497 struct t1 *wc;
1498 unsigned int *canary;
1499 unsigned int x;
1500
1501 if (pci_enable_device(pdev)) {
1502 return -EIO;
1503 }
1504 if (!(wc = kmalloc(sizeof(*wc), GFP_KERNEL))) {
1505 return -ENOMEM;
1506 }
1507
1508 memset(wc, 0x0, sizeof(*wc));
1509 spin_lock_init(&wc->lock);
1510 wc->ioaddr = pci_resource_start(pdev, 0);
1511 wc->dev = pdev;
1512 wc->offset = 28; /* And you thought 42 was the answer */
1513
1514 wc->writechunk =
1515 /* 32 channels, Double-buffer, Read/Write */
1516 (unsigned char *)pci_alloc_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 32 * 2 * 2, &wc->writedma);
1517 if (!wc->writechunk) {
1518 printk(KERN_NOTICE "wcte11xp: Unable to allocate DMA-able memory\n");
1519 return -ENOMEM;
1520 }
1521
1522 /* Read is after the whole write piece (in bytes) */
1523 wc->readchunk = wc->writechunk + DAHDI_CHUNKSIZE * 32 * 2;
1524
1525 /* Same thing... */
1526 wc->readdma = wc->writedma + DAHDI_CHUNKSIZE * 32 * 2;
1527
1528 /* Initialize Write/Buffers to all blank data */
1529 memset((void *)wc->writechunk,0x00,DAHDI_MAX_CHUNKSIZE * 2 * 2 * 32);
1530 /* Initialize canary */
1531 canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 64 - 4);
1532 *canary = (CANARY << 16) | (0xffff);
1533
1534 /* Enable bus mastering */
1535 pci_set_master(pdev);
1536
1537 /* Keep track of which device we are */
1538 pci_set_drvdata(pdev, wc);
1539
1540 if (request_irq(pdev->irq, t1xxp_interrupt,
1541 IRQF_SHARED, "wcte11xp", wc)) {
1542 printk(KERN_NOTICE "wcte11xp: Unable to request IRQ %d\n", pdev->irq);
1543 kfree(wc);
1544 return -EIO;
1545 }
1546 /* Initialize hardware */
1547 t1xxp_hardware_init(wc);
1548
1549 /* We now know which version of card we have */
1550 wc->variety = "Digium Wildcard TE110P T1/E1";
1551
1552 for (x = 0; x < (wc->spantype == TYPE_E1 ? 31 : 24); x++) {
1553 if (!(wc->chans[x] = kmalloc(sizeof(*wc->chans[x]), GFP_KERNEL))) {
1554 while (x) {
1555 kfree(wc->chans[--x]);
1556 }
1557
1558 kfree(wc);
1559 return -ENOMEM;
1560 }
1561 memset(wc->chans[x], 0, sizeof(*wc->chans[x]));
1562 }
1563
1564 /* Misc. software stuff */
1565 t1xxp_software_init(wc);
1566
1567 printk(KERN_INFO "Found a Wildcard: %s\n", wc->variety);
1568
1569 return 0;
1570 }
1571
1572 static void t1xxp_stop_stuff(struct t1 *wc)
1573 {
1574 /* Kill clock */
1575 control_set_reg(wc, WC_CLOCK, 0);
1576
1577 /* Turn off LED's */
1578 control_set_reg(wc, WC_LEDTEST, 0);
1579
1580 }
1581
1582 static void __devexit t1xxp_remove_one(struct pci_dev *pdev)
1583 {
1584 struct t1 *wc = pci_get_drvdata(pdev);
1585 if (wc) {
1586
1587 /* Stop any DMA */
1588 __t1xxp_stop_dma(wc);
1589
1590 /* In case hardware is still there */
1591 __t1xxp_disable_interrupts(wc);
1592
1593 t1xxp_stop_stuff(wc);
1594
1595 /* Immediately free resources */
1596 pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 2 * 2 * 32 * 4, (void *)wc->writechunk, wc->writedma);
1597 free_irq(pdev->irq, wc);
1598
1599 /* Reset PCI chip and registers */
1600 outb(DELAY | 0x0e, wc->ioaddr + WC_CNTL);
1601
1602 /* Release span, possibly delayed */
1603 if (!wc->usecount)
1604 t1xxp_release(wc);
1605 else
1606 wc->dead = 1;
1607 }
1608 }
1609
1610 static DEFINE_PCI_DEVICE_TABLE(t1xxp_pci_tbl) = {
1611 { 0xe159, 0x0001, 0x71fe, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard TE110P T1/E1 Board" },
1612 { 0xe159, 0x0001, 0x79fe, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard TE110P T1/E1 Board" },
1613 { 0xe159, 0x0001, 0x795e, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard TE110P T1/E1 Board" },
1614 { 0xe159, 0x0001, 0x79de, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard TE110P T1/E1 Board" },
1615 { 0xe159, 0x0001, 0x797e, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard TE110P T1/E1 Board" },
1616 { 0 }
1617 };
1618
1619 MODULE_DEVICE_TABLE(pci,t1xxp_pci_tbl);
1620
1621 static struct pci_driver t1xxp_driver = {
1622 .name = "wcte11xp",
1623 .probe = t1xxp_init_one,
1624 .remove = __devexit_p(t1xxp_remove_one),
1625 .suspend = NULL,
1626 .resume = NULL,
1627 .id_table = t1xxp_pci_tbl,
1628 };
1629
1630 static int __init t1xxp_init(void)
1631 {
1632 int res;
1633 res = dahdi_pci_module(&t1xxp_driver);
1634 if (res)
1635 return -ENODEV;
1636 return 0;
1637 }
1638
1639 static void __exit t1xxp_cleanup(void)
1640 {
1641 pci_unregister_driver(&t1xxp_driver);
1642 }
1643
1644 module_param(alarmdebounce, int, 0600);
1645 module_param(loopback, int, 0600);
1646 module_param(t1e1override, int, 0600);
1647 module_param(unchannelized, int, 0600);
1648 module_param(clockextra, int, 0600);
1649 module_param(debug, int, 0600);
1650 module_param(j1mode, int, 0600);
1651
1652 MODULE_DESCRIPTION("Wildcard TE110P Driver");
1653 MODULE_AUTHOR("Mark Spencer <[email protected]>");
1654 MODULE_LICENSE("GPL v2");
1655
1656 module_init(t1xxp_init);
1657 module_exit(t1xxp_cleanup);