Debian Codegraph @

drivers/dahdi/tor2.c

raw
1 /*
2 * Tormenta 2 Quad-T1 PCI Driver
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Based on previous works, designs, and archetectures conceived and
6 * written by Jim Dixon <[email protected]>.
7 *
8 * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
9 * Copyright (C) 2001-2008, Digium, Inc.
10 *
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/pci.h>
30 #include <linux/init.h>
31 #include <linux/sched.h>
32 #include <linux/interrupt.h>
33 #include <linux/moduleparam.h>
34 #include <linux/slab.h>
35
36 #include <dahdi/kernel.h>
37 #define NEED_PCI_IDS
38 #include "tor2-hw.h"
39 #include "tor2fw.h"
40
41 /*
42 * Tasklets provide better system interactive response at the cost of the
43 * possibility of losing a frame of data at very infrequent intervals. If
44 * you are more concerned with the performance of your machine, enable the
45 * tasklets. If you are strict about absolutely no drops, then do not enable
46 * tasklets.
47 */
48
49 /* #define ENABLE_TASKLETS */
50
51 /* this stuff needs to work for 64 bit systems, however using the macro causes
52 it to take twice as long */
53 /* #define FIXTHISFOR64 */ /* as of now, un-comment for 32 bit only system */
54
55 #define SPANS_PER_CARD 4
56 #define MAX_SPANS 16
57
58 #define FLAG_STARTED (1 << 0)
59
60 #define TYPE_T1 1 /* is a T1 card */
61 #define TYPE_E1 2 /* is an E1 card */
62
63 struct tor2_chan {
64 /* Private pointer for channel. We want to know our
65 channel and span */
66 struct tor2 *tor;
67 int span; /* Index from 0 */
68 };
69
70 struct tor2_span {
71 /* Private pointer for span. We want to know our
72 span number and pointer to the tor device */
73 struct tor2 *tor;
74 int span; /* Index from 0 */
75 struct dahdi_span dahdi_span;
76 };
77
78 struct tor2 {
79 /* This structure exists one per card */
80 struct pci_dev *pci; /* Pointer to PCI device */
81 int num; /* Which card we are */
82 int syncsrc; /* active sync source */
83 int syncs[SPANS_PER_CARD]; /* sync sources */
84 int psyncs[SPANS_PER_CARD]; /* span-relative sync sources */
85 int alarmtimer[SPANS_PER_CARD]; /* Alarm timer */
86 char *type; /* Type of tormenta 2 card */
87 int irq; /* IRQ used by device */
88 int order; /* Order */
89 int flags; /* Device flags */
90 int syncpos[SPANS_PER_CARD]; /* span-relative sync sources */
91 int master; /* Are we master */
92 unsigned long plx_region; /* phy addr of PCI9030 registers */
93 unsigned long plx_len; /* length of PLX window */
94 __iomem volatile unsigned short *plx; /* Virtual representation of local space */
95 unsigned long xilinx32_region; /* 32 bit Region allocated to Xilinx */
96 unsigned long xilinx32_len; /* Length of 32 bit Xilinx region */
97 __iomem volatile unsigned int *mem32; /* Virtual representation of 32 bit Xilinx memory area */
98 unsigned long xilinx8_region; /* 8 bit Region allocated to Xilinx */
99 unsigned long xilinx8_len; /* Length of 8 bit Xilinx region */
100 __iomem volatile unsigned char *mem8; /* Virtual representation of 8 bit Xilinx memory area */
101 struct dahdi_device *ddev;
102 struct tor2_span tspans[SPANS_PER_CARD]; /* Span data */
103 struct dahdi_chan **chans[SPANS_PER_CARD]; /* Pointers to card channels */
104 struct tor2_chan tchans[32 * SPANS_PER_CARD]; /* Channel user data */
105 unsigned char txsigs[SPANS_PER_CARD][16]; /* Copy of tx sig registers */
106 int loopupcnt[SPANS_PER_CARD]; /* loop up code counter */
107 int loopdowncnt[SPANS_PER_CARD];/* loop down code counter */
108 int spansstarted; /* number of spans started */
109 spinlock_t lock; /* lock context */
110 unsigned char leds; /* copy of LED register */
111 unsigned char ec_chunk1[SPANS_PER_CARD][32][DAHDI_CHUNKSIZE]; /* first EC chunk buffer */
112 unsigned char ec_chunk2[SPANS_PER_CARD][32][DAHDI_CHUNKSIZE]; /* second EC chunk buffer */
113 #ifdef ENABLE_TASKLETS
114 int taskletrun;
115 int taskletsched;
116 int taskletpending;
117 int taskletexec;
118 int txerrors;
119 struct tasklet_struct tor2_tlet;
120 #endif
121 int cardtype; /* card type, T1 or E1 */
122 unsigned int *datxlt; /* pointer to datxlt structure */
123 unsigned int passno; /* number of interrupt passes */
124 };
125
126 #define t1out(tor, span, reg, val) \
127 writeb(val, &tor->mem8[((span - 1) * 0x100) + reg])
128 #define t1in(tor, span, reg) readb(&tor->mem8[((span - 1) * 0x100) + reg])
129
130 #ifdef ENABLE_TASKLETS
131 static void tor2_tasklet(unsigned long data);
132 #endif
133
134 #define GPIOC (PLX_LOC_GPIOC >> 1) /* word-oriented address for PLX GPIOC reg. (32 bit reg.) */
135 #define LAS2BRD (0x30 >> 1)
136 #define LAS3BRD (0x34 >> 1)
137 #define INTCSR (0x4c >> 1) /* word-oriented address for PLX INTCSR reg. */
138 #define PLX_INTENA 0x43 /* enable, hi-going, level trigger */
139
140 #define SYNCREG 0x400
141 #define CTLREG 0x401
142 #define LEDREG 0x402
143 #define STATREG 0x400
144 #define SWREG 0x401
145 #define CTLREG1 0x404
146
147 #define INTENA (1 + ((loopback & 3) << 5))
148 #define OUTBIT (2 + ((loopback & 3) << 5))
149 #define E1DIV 0x10
150 #define INTACK (0x80 + ((loopback & 3) << 5))
151 #define INTACTIVE 2
152 #define MASTER (1 << 3)
153
154 /* un-define this if you dont want NON-REV A hardware support */
155 /* #define NONREVA 1 */
156
157 #define SYNCSELF 0
158 #define SYNC1 1
159 #define SYNC2 2
160 #define SYNC3 3
161 #define SYNC4 4
162 #define SYNCEXTERN 5
163
164 #define LEDRED 2
165 #define LEDGREEN 1
166
167 #define MAX_TOR_CARDS 64
168
169 static struct tor2 *cards[MAX_TOR_CARDS];
170
171 /* signalling bits */
172 #define TOR_ABIT 8
173 #define TOR_BBIT 4
174
175 static int debug;
176 static int japan;
177 static int loopback;
178 static int highestorder;
179 static int timingcable;
180
181 static void set_clear(struct tor2 *tor);
182 static int tor2_startup(struct file *file, struct dahdi_span *span);
183 static int tor2_shutdown(struct dahdi_span *span);
184 static int tor2_rbsbits(struct dahdi_chan *chan, int bits);
185 static int tor2_maint(struct dahdi_span *span, int cmd);
186 static int tor2_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data);
187 DAHDI_IRQ_HANDLER(tor2_intr);
188
189 /* translations of data channels for 24 channels in a 32 bit PCM highway */
190 static unsigned datxlt_t1[] = {
191 1 ,2 ,3 ,5 ,6 ,7 ,9 ,10,11,13,14,15,17,18,19,21,22,23,25,26,27,29,30,31 };
192
193 /* translations of data channels for 30/31 channels in a 32 bit PCM highway */
194 static unsigned datxlt_e1[] = {
195 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
196 25,26,27,28,29,30,31 };
197
198 static int tor2_spanconfig(struct file *file, struct dahdi_span *span,
199 struct dahdi_lineconfig *lc)
200 {
201 int i;
202 struct tor2_span *p = container_of(span, struct tor2_span, dahdi_span);
203
204 if (debug)
205 printk(KERN_INFO "Tor2: Configuring span %d\n", span->spanno);
206
207 if ((lc->sync < 0) || (lc->sync > SPANS_PER_CARD)) {
208 printk(KERN_WARNING "%s %d: invalid span timing value %d.\n",
209 THIS_MODULE->name, span->spanno, lc->sync);
210 return -EINVAL;
211 }
212 span->syncsrc = p->tor->syncsrc;
213
214 /* remove this span number from the current sync sources, if there */
215 for (i = 0; i < SPANS_PER_CARD; i++) {
216 if (p->tor->syncs[i] == span->spanno) {
217 p->tor->syncs[i] = 0;
218 p->tor->psyncs[i] = 0;
219 }
220 }
221 p->tor->syncpos[p->span] = lc->sync;
222 /* if a sync src, put it in the proper place */
223 if (lc->sync) {
224 p->tor->syncs[lc->sync - 1] = span->spanno;
225 p->tor->psyncs[lc->sync - 1] = p->span + 1;
226 }
227 /* If we're already running, then go ahead and apply the changes */
228 if (span->flags & DAHDI_FLAG_RUNNING)
229 return tor2_startup(file, span);
230
231 return 0;
232 }
233
234 static int tor2_chanconfig(struct file *file,
235 struct dahdi_chan *chan, int sigtype)
236 {
237 int alreadyrunning;
238 unsigned long flags;
239 struct tor2_chan *p = chan->pvt;
240
241 alreadyrunning = chan->span->flags & DAHDI_FLAG_RUNNING;
242 if (debug) {
243 if (alreadyrunning)
244 printk(KERN_INFO "Tor2: Reconfigured channel %d (%s) sigtype %d\n", chan->channo, chan->name, sigtype);
245 else
246 printk(KERN_INFO "Tor2: Configured channel %d (%s) sigtype %d\n", chan->channo, chan->name, sigtype);
247 }
248 /* nothing more to do if an E1 */
249 if (p->tor->cardtype == TYPE_E1) return 0;
250 spin_lock_irqsave(&p->tor->lock, flags);
251 if (alreadyrunning)
252 set_clear(p->tor);
253 spin_unlock_irqrestore(&p->tor->lock, flags);
254 return 0;
255 }
256
257 static const struct dahdi_span_ops tor2_span_ops = {
258 .owner = THIS_MODULE,
259 .spanconfig = tor2_spanconfig,
260 .chanconfig = tor2_chanconfig,
261 .startup = tor2_startup,
262 .shutdown = tor2_shutdown,
263 .rbsbits = tor2_rbsbits,
264 .maint = tor2_maint,
265 .ioctl = tor2_ioctl,
266 };
267
268 static void init_spans(struct tor2 *tor)
269 {
270 int x, y, c;
271 /* TODO: a debug printk macro */
272 for (x = 0; x < SPANS_PER_CARD; x++) {
273 struct dahdi_span *const s = &tor->tspans[x].dahdi_span;
274 sprintf(s->name, "Tor2/%d/%d", tor->num, x + 1);
275 snprintf(s->desc, sizeof(s->desc) - 1,
276 "Tormenta 2 (PCI) Quad %s Card %d Span %d",
277 (tor->cardtype == TYPE_T1) ? "T1" : "E1", tor->num, x + 1);
278 if (tor->cardtype == TYPE_T1) {
279 s->channels = 24;
280 s->deflaw = DAHDI_LAW_MULAW;
281 s->linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
282 s->spantype = SPANTYPE_DIGITAL_T1;
283 } else {
284 s->channels = 31;
285 s->deflaw = DAHDI_LAW_ALAW;
286 s->linecompat = DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
287 s->spantype = SPANTYPE_DIGITAL_E1;
288 }
289 s->chans = tor->chans[x];
290 s->flags = DAHDI_FLAG_RBS;
291 s->ops = &tor2_span_ops;
292
293 tor->tspans[x].tor = tor;
294 tor->tspans[x].span = x;
295 for (y = 0; y < s->channels; y++) {
296 struct dahdi_chan *mychans = tor->chans[x][y];
297 sprintf(mychans->name, "Tor2/%d/%d/%d", tor->num, x + 1, y + 1);
298 mychans->sigcap = DAHDI_SIG_EM | DAHDI_SIG_CLEAR | DAHDI_SIG_FXSLS | DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS |
299 DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_SF | DAHDI_SIG_EM_E1;
300 c = (x * s->channels) + y;
301 mychans->pvt = &tor->tchans[c];
302 mychans->chanpos = y + 1;
303 tor->tchans[c].span = x;
304 tor->tchans[c].tor = tor;
305 }
306 }
307 }
308
309 static int __devinit tor2_launch(struct tor2 *tor)
310 {
311 int res;
312 struct dahdi_span *s;
313 int i;
314
315 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &tor->tspans[0].dahdi_span.flags))
316 return 0;
317
318 tor->ddev = dahdi_create_device();
319 tor->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
320 tor->pci->bus->number,
321 PCI_SLOT(tor->pci->devfn) + 1);
322
323 if (!tor->ddev->location)
324 return -ENOMEM;
325
326 printk(KERN_INFO "Tor2: Launching card: %d\n", tor->order);
327 for (i = 0; i < SPANS_PER_CARD; ++i) {
328 s = &tor->tspans[i].dahdi_span;
329 list_add_tail(&s->device_node, &tor->ddev->spans);
330 }
331
332 res = dahdi_register_device(tor->ddev, &tor->pci->dev);
333 if (res) {
334 dev_err(&tor->pci->dev, "Unable to register with DAHDI.\n");
335 return res;
336 }
337 writew(PLX_INTENA, &tor->plx[INTCSR]); /* enable PLX interrupt */
338
339 #ifdef ENABLE_TASKLETS
340 tasklet_init(&tor->tor2_tlet, tor2_tasklet, (unsigned long)tor);
341 #endif
342 return 0;
343 }
344
345 static void free_tor(struct tor2 *tor)
346 {
347 unsigned int x, f;
348
349 for (x = 0; x < SPANS_PER_CARD; x++) {
350 for (f = 0; f < (tor->cardtype == TYPE_E1 ? 31 : 24); f++) {
351 if (tor->chans[x][f]) {
352 kfree(tor->chans[x][f]);
353 }
354 }
355 if (tor->chans[x])
356 kfree(tor->chans[x]);
357 }
358 kfree(tor->ddev->location);
359 dahdi_free_device(tor->ddev);
360 kfree(tor);
361 }
362
363 static int __devinit tor2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
364 {
365 int res,x,f;
366 int ret = -ENODEV;
367 struct tor2 *tor;
368 unsigned long endjif;
369 __iomem volatile unsigned long *gpdata_io, *lasdata_io;
370 unsigned long gpdata,lasdata;
371
372 res = pci_enable_device(pdev);
373 if (res)
374 return res;
375 tor = kmalloc(sizeof(struct tor2), GFP_KERNEL);
376 if (!tor)
377 return -ENOMEM;
378 memset(tor,0,sizeof(struct tor2));
379 spin_lock_init(&tor->lock);
380 /* Load the resources */
381 tor->pci = pdev;
382 tor->irq = pdev->irq;
383 if (tor->irq < 1) {
384 printk(KERN_ERR "No IRQ allocated for device\n");
385 goto err_out_free_tor;
386 }
387 tor->plx_region = pci_resource_start(pdev, 0);
388 tor->plx_len = pci_resource_len(pdev, 0);
389 tor->plx = ioremap(tor->plx_region, tor->plx_len);
390 /* We don't use the I/O space, so we dont do anything with section 1 */
391 tor->xilinx32_region = pci_resource_start(pdev, 2);
392 tor->xilinx32_len = pci_resource_len(pdev, 2);
393 tor->mem32 = ioremap(tor->xilinx32_region, tor->xilinx32_len);
394 tor->xilinx8_region = pci_resource_start(pdev, 3);
395 tor->xilinx8_len = pci_resource_len(pdev, 3);
396 tor->mem8 = ioremap(tor->xilinx8_region, tor->xilinx8_len);
397 /* Record what type */
398 tor->type = (char *)ent->driver_data;
399 /* Verify existence and accuracy of resources */
400 if (!tor->plx_region || !tor->plx ||
401 (pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
402 printk(KERN_ERR "Invalid PLX 9030 Base resource\n");
403 goto err_out_free_tor;
404 }
405 if (!tor->xilinx32_region || !tor->mem32 ||
406 (pci_resource_flags(pdev, 2) & IORESOURCE_IO)) {
407 printk(KERN_ERR "Invalid Xilinx 32 bit Base resource\n");
408 goto err_out_free_tor;
409 }
410 if (!tor->xilinx8_region || !tor->mem8 ||
411 (pci_resource_flags(pdev, 3) & IORESOURCE_IO)) {
412 printk(KERN_ERR "Invalid Xilinx 8 bit Base resource\n");
413 goto err_out_free_tor;
414 }
415 /* Request regions */
416 if (!request_mem_region(tor->plx_region, tor->plx_len, tor->type)) {
417 printk(KERN_ERR "Unable to reserve PLX memory %08lx window at %08lx\n",
418 tor->plx_len, tor->plx_region);
419 goto err_out_free_tor;
420 }
421 if (!request_mem_region(tor->xilinx32_region, tor->xilinx32_len, tor->type)) {
422 printk(KERN_ERR "Unable to reserve Xilinx 32 bit memory %08lx window at %08lx\n",
423 tor->xilinx32_len, tor->xilinx32_region);
424 goto err_out_release_plx_region;
425 }
426 if (!request_mem_region(tor->xilinx8_region, tor->xilinx8_len, tor->type)) {
427 printk(KERN_ERR "Unable to reserve Xilinx memory %08lx window at %08lx\n",
428 tor->xilinx8_len, tor->xilinx8_region);
429 goto err_out_release_plx_region;
430 }
431 pci_set_drvdata(pdev, tor);
432 printk(KERN_INFO "Detected %s at 0x%lx/0x%lx irq %d\n", tor->type,
433 tor->xilinx32_region, tor->xilinx8_region,tor->irq);
434
435 for (x = 0; x < MAX_TOR_CARDS; x++) {
436 if (!cards[x]) break;
437 }
438 if (x >= MAX_TOR_CARDS) {
439 printk(KERN_ERR "No cards[] slot available!!\n");
440 goto err_out_release_all;
441 }
442 tor->num = x;
443 cards[x] = tor;
444
445 /* start programming mode */
446 gpdata_io = (__iomem unsigned long *) &tor->plx[GPIOC];
447 gpdata = readl(gpdata_io);
448
449 gpdata |= GPIO_WRITE; /* make sure WRITE is not asserted */
450 writel(gpdata, gpdata_io);
451
452 gpdata &= ~GPIO_PROGRAM; /* activate the PROGRAM signal */
453 writel(gpdata, gpdata_io);
454
455 /* wait for INIT and DONE to go low */
456 endjif = jiffies + 10;
457 while (readl(gpdata_io) & (GPIO_INIT | GPIO_DONE) &&
458 (jiffies <= endjif)) {
459 ;
460 }
461
462 if (endjif < jiffies) {
463 printk(KERN_ERR "Timeout waiting for INIT and DONE to go low\n");
464 goto err_out_release_all;
465 }
466 if (debug) printk(KERN_ERR "fwload: Init and done gone to low\n");
467 gpdata |= GPIO_PROGRAM;
468 writel(gpdata, gpdata_io); /* de-activate the PROGRAM signal */
469 /* wait for INIT to go high (clearing done */
470 endjif = jiffies + 10;
471 while (!(readl(gpdata_io) & GPIO_INIT) && (jiffies <= endjif))
472 ;
473 if (endjif < jiffies) {
474 printk(KERN_ERR "Timeout waiting for INIT to go high\n");
475 goto err_out_release_all;
476 }
477
478 if (debug) printk(KERN_ERR "fwload: Init went high (clearing done)\nNow loading...\n");
479 /* assert WRITE signal */
480 gpdata &= ~GPIO_WRITE;
481 writel(gpdata, gpdata_io);
482 for (x = 0; x < sizeof(tor2fw); x++)
483 {
484 /* write the byte */
485 writeb(tor2fw[x], tor->mem8);
486 /* if DONE signal, we're done, exit */
487 if (readl(gpdata_io) & GPIO_DONE)
488 break;
489 /* if INIT drops, we're screwed, exit */
490 if (!(readl(gpdata_io) & GPIO_INIT))
491 break;
492 }
493 if (debug) printk(KERN_DEBUG "fwload: Transferred %d bytes into chip\n",x);
494 /* Wait for FIFO to clear */
495 endjif = jiffies + 2;
496 while (jiffies < endjif); /* wait */
497 /* de-assert write signal */
498 gpdata |= GPIO_WRITE;
499 writel(gpdata, gpdata_io);
500 if (debug) printk(KERN_DEBUG "fwload: Loading done!\n");
501
502 /* Wait for FIFO to clear */
503 endjif = jiffies + 2;
504 while (jiffies < endjif); /* wait */
505 if (!(readl(gpdata_io) & GPIO_INIT))
506 {
507 printk(KERN_ERR "Drove Init low!! CRC Error!!!\n");
508 goto err_out_release_all;
509 }
510 if (!(readl(gpdata_io) & GPIO_DONE))
511 {
512 printk(KERN_ERR "Did not get DONE signal. Short file maybe??\n");
513 goto err_out_release_all;
514 }
515 printk(KERN_INFO "Xilinx Chip successfully loaded, configured and started!!\n");
516
517 writeb(0, &tor->mem8[SYNCREG]);
518 writeb(0, &tor->mem8[CTLREG]);
519 writeb(0, &tor->mem8[CTLREG1]);
520 writeb(0, &tor->mem8[LEDREG]);
521
522 /* set the LA2BRD register so that we enable block transfer, read
523 pre-fetch, and set to maximum read pre-fetch size */
524 lasdata_io = (__iomem unsigned long *) &tor->plx[LAS2BRD];
525 lasdata = readl(lasdata_io);
526 lasdata |= 0x39;
527 writel(lasdata, lasdata_io);
528
529 /* set the LA3BRD register so that we enable block transfer */
530 lasdata_io = (__iomem unsigned long *) &tor->plx[LAS3BRD];
531 lasdata = readl(lasdata_io);
532 lasdata |= 1;
533 writel(lasdata, lasdata_io);
534
535 /* check part revision data */
536 x = t1in(tor,1,0xf) & 15;
537 #ifdef NONREVA
538 if (x > 3)
539 {
540 tor->mem8[CTLREG1] = NONREVA;
541 }
542 #endif
543 for (x = 0; x < 256; x++)
544 writel(0x7f7f7f7f, &tor->mem32[x]);
545
546 if (request_irq(tor->irq, tor2_intr, IRQF_SHARED, "tor2", tor)) {
547 printk(KERN_ERR "Unable to request tormenta IRQ %d\n", tor->irq);
548 goto err_out_release_all;
549 }
550
551 if (t1in(tor,1,0xf) & 0x80) {
552 printk(KERN_INFO "Tormenta 2 Quad E1/PRA Card\n");
553 tor->cardtype = TYPE_E1;
554 tor->datxlt = datxlt_e1;
555 } else {
556 printk(KERN_INFO "Tormenta 2 Quad T1/PRI Card\n");
557 tor->cardtype = TYPE_T1;
558 tor->datxlt = datxlt_t1;
559 }
560
561 for (x = 0; x < SPANS_PER_CARD; x++) {
562 int num_chans = tor->cardtype == TYPE_E1 ? 31 : 24;
563
564 if (!(tor->chans[x] = kmalloc(num_chans * sizeof(*tor->chans[x]), GFP_KERNEL))) {
565 printk(KERN_ERR "tor2: Not enough memory for chans[%d]\n", x);
566 ret = -ENOMEM;
567 goto err_out_release_all;
568 }
569 for (f = 0; f < (num_chans); f++) {
570 if (!(tor->chans[x][f] = kmalloc(sizeof(*tor->chans[x][f]), GFP_KERNEL))) {
571 printk(KERN_ERR "tor2: Not enough memory for chans[%d][%d]\n",
572 x, f);
573 ret = -ENOMEM;
574 goto err_out_release_all;
575 }
576 memset(tor->chans[x][f], 0, sizeof(*tor->chans[x][f]));
577 }
578 }
579
580 init_spans(tor);
581
582 tor->order = readb(&tor->mem8[SWREG]);
583 printk(KERN_INFO "Detected Card number: %d\n", tor->order);
584
585 /* Launch cards as appropriate */
586 x = 0;
587 for (;;) {
588 /* Find a card to activate */
589 f = 0;
590 for (x=0;cards[x];x++) {
591 if (cards[x]->order <= highestorder) {
592 tor2_launch(cards[x]);
593 if (cards[x]->order == highestorder)
594 f = 1;
595 }
596 }
597 /* If we found at least one, increment the highest order and search again, otherwise stop */
598 if (f)
599 highestorder++;
600 else
601 break;
602 }
603
604 return 0;
605
606 err_out_release_all:
607 release_mem_region(tor->xilinx32_region, tor->xilinx32_len);
608 release_mem_region(tor->xilinx8_region, tor->xilinx8_len);
609 err_out_release_plx_region:
610 release_mem_region(tor->plx_region, tor->plx_len);
611 err_out_free_tor:
612 if (tor->plx) iounmap(tor->plx);
613 if (tor->mem8) iounmap(tor->mem8);
614 if (tor->mem32) iounmap(tor->mem32);
615 if (tor) {
616 free_tor(tor);
617 }
618 return ret;
619 }
620
621 static struct pci_driver tor2_driver;
622
623 static void __devexit tor2_remove(struct pci_dev *pdev)
624 {
625 struct tor2 *tor;
626
627 tor = pci_get_drvdata(pdev);
628 if (!tor)
629 BUG();
630 writeb(0, &tor->mem8[SYNCREG]);
631 writeb(0, &tor->mem8[CTLREG]);
632 writeb(0, &tor->mem8[LEDREG]);
633 writew(0, &tor->plx[INTCSR]);
634 free_irq(tor->irq, tor);
635 dahdi_unregister_device(tor->ddev);
636 release_mem_region(tor->plx_region, tor->plx_len);
637 release_mem_region(tor->xilinx32_region, tor->xilinx32_len);
638 release_mem_region(tor->xilinx8_region, tor->xilinx8_len);
639 if (tor->plx) iounmap(tor->plx);
640 if (tor->mem8) iounmap(tor->mem8);
641 if (tor->mem32) iounmap(tor->mem32);
642
643 cards[tor->num] = NULL;
644 pci_set_drvdata(pdev, NULL);
645 free_tor(tor);
646 }
647
648 static struct pci_driver tor2_driver = {
649 .name = "tormenta2",
650 .probe = tor2_probe,
651 .remove = __devexit_p(tor2_remove),
652 .id_table = tor2_pci_ids,
653 };
654
655 static int __init tor2_init(void) {
656 int res;
657 res = dahdi_pci_module(&tor2_driver);
658 printk(KERN_INFO "Registered Tormenta2 PCI\n");
659 return res;
660 }
661
662 static void __exit tor2_cleanup(void) {
663 pci_unregister_driver(&tor2_driver);
664 printk(KERN_INFO "Unregistered Tormenta2\n");
665 }
666
667 static void set_clear(struct tor2 *tor)
668 {
669 int i,j,s;
670 unsigned short val=0;
671 for (s = 0; s < SPANS_PER_CARD; s++) {
672 struct dahdi_span *span = &tor->tspans[s].dahdi_span;
673 for (i = 0; i < 24; i++) {
674 j = (i/8);
675 if (span->chans[i]->flags & DAHDI_FLAG_CLEAR)
676 val |= 1 << (i % 8);
677
678 if ((i % 8)==7) {
679 #if 0
680 printk(KERN_DEBUG "Putting %d in register %02x on span %d\n",
681 val, 0x39 + j, 1 + s);
682 #endif
683 t1out(tor,1 + s, 0x39 + j, val);
684 val = 0;
685 }
686 }
687 }
688
689 }
690
691
692 static int tor2_rbsbits(struct dahdi_chan *chan, int bits)
693 {
694 u_char m,c;
695 int k,n,b;
696 struct tor2_chan *p = chan->pvt;
697 unsigned long flags;
698 #if 0
699 printk(KERN_DEBUG "Setting bits to %d on channel %s\n", bits, chan->name);
700 #endif
701 if (p->tor->cardtype == TYPE_E1) { /* do it E1 way */
702 if (chan->chanpos == 16) return 0;
703 n = chan->chanpos - 1;
704 if (chan->chanpos > 16) n--;
705 k = p->span;
706 b = (n % 15) + 1;
707 c = p->tor->txsigs[k][b];
708 m = (n / 15) * 4; /* nibble selector */
709 c &= (15 << m); /* keep the other nibble */
710 c |= (bits & 15) << (4 - m); /* put our new nibble here */
711 p->tor->txsigs[k][b] = c;
712 /* output them to the chip */
713 t1out(p->tor,k + 1,0x40 + b,c);
714 return 0;
715 }
716 n = chan->chanpos - 1;
717 k = p->span;
718 b = (n / 8); /* get byte number */
719 m = 1 << (n & 7); /* get mask */
720 c = p->tor->txsigs[k][b];
721 c &= ~m; /* clear mask bit */
722 /* set mask bit, if bit is to be set */
723 if (bits & DAHDI_ABIT) c |= m;
724 p->tor->txsigs[k][b] = c;
725 spin_lock_irqsave(&p->tor->lock, flags);
726 t1out(p->tor,k + 1,0x70 + b,c);
727 b += 3; /* now points to b bit stuff */
728 /* get current signalling values */
729 c = p->tor->txsigs[k][b];
730 c &= ~m; /* clear mask bit */
731 /* set mask bit, if bit is to be set */
732 if (bits & DAHDI_BBIT) c |= m;
733 /* save new signalling values */
734 p->tor->txsigs[k][b] = c;
735 /* output them into the chip */
736 t1out(p->tor,k + 1,0x70 + b,c);
737 b += 3; /* now points to c bit stuff */
738 /* get current signalling values */
739 c = p->tor->txsigs[k][b];
740 c &= ~m; /* clear mask bit */
741 /* set mask bit, if bit is to be set */
742 if (bits & DAHDI_CBIT) c |= m;
743 /* save new signalling values */
744 p->tor->txsigs[k][b] = c;
745 /* output them into the chip */
746 t1out(p->tor,k + 1,0x70 + b,c);
747 b += 3; /* now points to d bit stuff */
748 /* get current signalling values */
749 c = p->tor->txsigs[k][b];
750 c &= ~m; /* clear mask bit */
751 /* set mask bit, if bit is to be set */
752 if (bits & DAHDI_DBIT) c |= m;
753 /* save new signalling values */
754 p->tor->txsigs[k][b] = c;
755 /* output them into the chip */
756 t1out(p->tor,k + 1,0x70 + b,c);
757 spin_unlock_irqrestore(&p->tor->lock, flags);
758 return 0;
759 }
760
761 static int tor2_shutdown(struct dahdi_span *span)
762 {
763 int i;
764 int tspan;
765 int wasrunning;
766 unsigned long flags;
767 struct tor2_span *const p = container_of(span, struct tor2_span, dahdi_span);
768 struct tor2 *const tor = p->tor;
769
770 tspan = p->span + 1;
771 if (tspan < 0) {
772 printk(KERN_DEBUG "Tor2: Span '%d' isn't us?\n", span->spanno);
773 return -1;
774 }
775
776 spin_lock_irqsave(&tor->lock, flags);
777 wasrunning = span->flags & DAHDI_FLAG_RUNNING;
778
779 span->flags &= ~DAHDI_FLAG_RUNNING;
780 /* Zero out all registers */
781 if (tor->cardtype == TYPE_E1) {
782 for (i = 0; i < 192; i++)
783 t1out(tor, tspan, i, 0);
784 } else {
785 for (i = 0; i < 160; i++)
786 t1out(tor, tspan, i, 0);
787 }
788 if (wasrunning)
789 tor->spansstarted--;
790 spin_unlock_irqrestore(&tor->lock, flags);
791 if (!(tor->tspans[0].dahdi_span.flags & DAHDI_FLAG_RUNNING) &&
792 !(tor->tspans[1].dahdi_span.flags & DAHDI_FLAG_RUNNING) &&
793 !(tor->tspans[2].dahdi_span.flags & DAHDI_FLAG_RUNNING) &&
794 !(tor->tspans[3].dahdi_span.flags & DAHDI_FLAG_RUNNING))
795 /* No longer in use, disable interrupts */
796 writeb(0, &tor->mem8[CTLREG]);
797 if (debug)
798 printk(KERN_DEBUG"Span %d (%s) shutdown\n", span->spanno, span->name);
799 return 0;
800 }
801
802
803 static int tor2_startup(struct file *file, struct dahdi_span *span)
804 {
805 unsigned long endjif;
806 int i;
807 int tspan;
808 unsigned long flags;
809 char *coding;
810 char *framing;
811 char *crcing;
812 int alreadyrunning;
813 struct tor2_span *p = container_of(span, struct tor2_span, dahdi_span);
814
815 tspan = p->span + 1;
816 if (tspan < 0) {
817 printk(KERN_DEBUG "Tor2: Span '%d' isn't us?\n", span->spanno);
818 return -1;
819 }
820
821 spin_lock_irqsave(&p->tor->lock, flags);
822
823 alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
824
825 /* initialize the start value for the entire chunk of last ec buffer */
826 for (i = 0; i < span->channels; i++)
827 {
828 memset(p->tor->ec_chunk1[p->span][i],
829 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
830 memset(p->tor->ec_chunk2[p->span][i],
831 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
832 }
833 /* Force re-evaluation of the timing source */
834 if (timingcable)
835 p->tor->syncsrc = -1;
836
837 if (p->tor->cardtype == TYPE_E1) { /* if this is an E1 card */
838 unsigned char tcr1,ccr1,tcr2;
839 if (!alreadyrunning) {
840 writeb(SYNCSELF, &p->tor->mem8[SYNCREG]);
841 writeb(E1DIV, &p->tor->mem8[CTLREG]);
842 writeb(0, &p->tor->mem8[LEDREG]);
843 /* Force re-evaluation of sync src */
844 /* Zero out all registers */
845 for (i = 0; i < 192; i++)
846 t1out(p->tor,tspan, i, 0);
847
848 /* Set up for Interleaved Serial Bus operation in byte mode */
849 /* Set up all the spans every time, so we are sure they are
850 in a consistent state. If we don't, a card without all
851 its spans configured misbehaves in strange ways. */
852 t1out(p->tor,1,0xb5,9);
853 t1out(p->tor,2,0xb5,8);
854 t1out(p->tor,3,0xb5,8);
855 t1out(p->tor,4,0xb5,8);
856
857 t1out(p->tor,tspan,0x1a,4); /* CCR2: set LOTCMC */
858 for (i = 0; i <= 8; i++) t1out(p->tor,tspan,i,0);
859 for (i = 0x10; i <= 0x4f; i++) if (i != 0x1a) t1out(p->tor,tspan,i,0);
860 t1out(p->tor,tspan,0x10,0x20); /* RCR1: Rsync as input */
861 t1out(p->tor,tspan,0x11,6); /* RCR2: Sysclk=2.048 Mhz */
862 t1out(p->tor,tspan,0x12,9); /* TCR1: TSiS mode */
863 }
864 ccr1 = 0;
865 crcing = "";
866 tcr1 = 9; /* base TCR1 value: TSis mode */
867 tcr2 = 0;
868 if (span->lineconfig & DAHDI_CONFIG_CCS) {
869 ccr1 |= 8; /* CCR1: Rx Sig mode: CCS */
870 coding = "CCS";
871 } else {
872 tcr1 |= 0x20;
873 coding = "CAS";
874 }
875 if (span->lineconfig & DAHDI_CONFIG_HDB3) {
876 ccr1 |= 0x44; /* CCR1: TX and RX HDB3 */
877 framing = "HDB3";
878 } else framing = "AMI";
879 if (span->lineconfig & DAHDI_CONFIG_CRC4) {
880 ccr1 |= 0x11; /* CCR1: TX and TX CRC4 */
881 tcr2 |= 0x02; /* TCR2: CRC4 bit auto */
882 crcing = "/CRC4";
883 }
884 t1out(p->tor,tspan,0x12,tcr1);
885 t1out(p->tor,tspan,0x13,tcr2);
886 t1out(p->tor,tspan,0x14,ccr1);
887 t1out(p->tor,tspan, 0x18, 0x20); /* 120 Ohm, normal */
888
889 if (!alreadyrunning) {
890 t1out(p->tor,tspan,0x1b,0x8a); /* CCR3: LIRST & TSCLKM */
891 t1out(p->tor,tspan,0x20,0x1b); /* TAFR */
892 t1out(p->tor,tspan,0x21,0x5f); /* TNAFR */
893 t1out(p->tor,tspan,0x40,0xb); /* TSR1 */
894 for (i = 0x41; i <= 0x4f; i++) t1out(p->tor,tspan,i,0x55);
895 for (i = 0x22; i <= 0x25; i++) t1out(p->tor,tspan,i,0xff);
896 /* Wait 100 ms */
897 endjif = jiffies + 10;
898 spin_unlock_irqrestore(&p->tor->lock, flags);
899 while (jiffies < endjif); /* wait 100 ms */
900 spin_lock_irqsave(&p->tor->lock, flags);
901 t1out(p->tor,tspan,0x1b,0x9a); /* CCR3: set also ESR */
902 t1out(p->tor,tspan,0x1b,0x82); /* CCR3: TSCLKM only now */
903
904 span->flags |= DAHDI_FLAG_RUNNING;
905 p->tor->spansstarted++;
906
907 /* enable interrupts */
908 writeb(INTENA | E1DIV, &p->tor->mem8[CTLREG]);
909 }
910
911 spin_unlock_irqrestore(&p->tor->lock, flags);
912
913 if (debug) {
914 if (alreadyrunning)
915 printk(KERN_INFO "Tor2: Reconfigured span %d (%s/%s%s) 120 Ohms\n", span->spanno, coding, framing, crcing);
916 else
917 printk(KERN_INFO "Tor2: Startup span %d (%s/%s%s) 120 Ohms\n", span->spanno, coding, framing, crcing);
918 }
919 } else { /* is a T1 card */
920
921 if (!alreadyrunning) {
922 writeb(SYNCSELF, &p->tor->mem8[SYNCREG]);
923 writeb(0, &p->tor->mem8[CTLREG]);
924 writeb(0, &p->tor->mem8[LEDREG]);
925 /* Zero out all registers */
926 for (i = 0; i < 160; i++)
927 t1out(p->tor,tspan, i, 0);
928
929 /* Set up for Interleaved Serial Bus operation in byte mode */
930 /* Set up all the spans every time, so we are sure they are
931 in a consistent state. If we don't, a card without all
932 its spans configured misbehaves in strange ways. */
933 t1out(p->tor,1,0x94,9);
934 t1out(p->tor,2,0x94,8);
935 t1out(p->tor,3,0x94,8);
936 t1out(p->tor,4,0x94,8);
937 /* Full-on Sync required (RCR1) */
938 t1out(p->tor,tspan, 0x2b, 8);
939 /* RSYNC is an input (RCR2) */
940 t1out(p->tor,tspan, 0x2c, 8);
941 /* RBS enable (TCR1) */
942 t1out(p->tor,tspan, 0x35, 0x10);
943 /* TSYNC to be output (TCR2) */
944 t1out(p->tor,tspan, 0x36, 4);
945 /* Tx & Rx Elastic store, sysclk(s) = 2.048 mhz, loopback controls (CCR1) */
946 t1out(p->tor,tspan, 0x37, 0x9c);
947 /* Set up received loopup and loopdown codes */
948 t1out(p->tor,tspan, 0x12, 0x22);
949 t1out(p->tor,tspan, 0x14, 0x80);
950 t1out(p->tor,tspan, 0x15, 0x80);
951 /* Setup japanese mode if appropriate */
952 t1out(p->tor,tspan,0x19,(japan ? 0x80 : 0x00)); /* no local loop */
953 t1out(p->tor,tspan,0x1e,(japan ? 0x80 : 0x00)); /* no local loop */
954 }
955 /* Enable F bits pattern */
956 i = 0x20;
957 if (span->lineconfig & DAHDI_CONFIG_ESF)
958 i = 0x88;
959 if (span->lineconfig & DAHDI_CONFIG_B8ZS)
960 i |= 0x44;
961 t1out(p->tor,tspan, 0x38, i);
962 if (i & 0x80)
963 coding = "ESF";
964 else
965 coding = "SF";
966 if (i & 0x40)
967 framing = "B8ZS";
968 else {
969 framing = "AMI";
970 t1out(p->tor,tspan,0x7e,0x1c); /* F bits pattern (0x1c) into FDL register */
971 }
972 t1out(p->tor,tspan, 0x7c, span->txlevel << 5);
973
974 if (!alreadyrunning) {
975 /* LIRST to reset line interface */
976 t1out(p->tor,tspan, 0x0a, 0x80);
977
978 /* Wait 100 ms */
979 endjif = jiffies + 10;
980
981 spin_unlock_irqrestore(&p->tor->lock, flags);
982
983 while (jiffies < endjif); /* wait 100 ms */
984
985 spin_lock_irqsave(&p->tor->lock, flags);
986
987 t1out(p->tor,tspan,0x0a,0x30); /* LIRST back to normal, Resetting elastic stores */
988
989 span->flags |= DAHDI_FLAG_RUNNING;
990 p->tor->spansstarted++;
991
992 /* enable interrupts */
993 writeb(INTENA, &p->tor->mem8[CTLREG]);
994 }
995
996 set_clear(p->tor);
997
998 spin_unlock_irqrestore(&p->tor->lock, flags);
999
1000 if (debug) {
1001 if (alreadyrunning)
1002 printk(KERN_INFO "Tor2: Reconfigured span %d (%s/%s) LBO: %s\n", span->spanno, coding, framing, dahdi_lboname(span->txlevel));
1003 else
1004 printk(KERN_INFO "Tor2: Startup span %d (%s/%s) LBO: %s\n", span->spanno, coding, framing, dahdi_lboname(span->txlevel));
1005 }
1006 }
1007 if (p->tor->syncs[0] == span->spanno) printk(KERN_INFO "SPAN %d: Primary Sync Source\n",span->spanno);
1008 if (p->tor->syncs[1] == span->spanno) printk(KERN_INFO "SPAN %d: Secondary Sync Source\n",span->spanno);
1009 if (p->tor->syncs[2] == span->spanno) printk(KERN_INFO "SPAN %d: Tertiary Sync Source\n",span->spanno);
1010 if (p->tor->syncs[3] == span->spanno) printk(KERN_INFO "SPAN %d: Quaternary Sync Source\n",span->spanno);
1011 return 0;
1012 }
1013
1014 static int tor2_maint(struct dahdi_span *span, int cmd)
1015 {
1016 struct tor2_span *p = container_of(span, struct tor2_span, dahdi_span);
1017
1018 int tspan = p->span + 1;
1019
1020 if (p->tor->cardtype == TYPE_E1)
1021 {
1022 switch(cmd) {
1023 case DAHDI_MAINT_NONE:
1024 t1out(p->tor,tspan,0xa8,0); /* no loops */
1025 break;
1026 case DAHDI_MAINT_LOCALLOOP:
1027 t1out(p->tor,tspan,0xa8,0x40); /* local loop */
1028 break;
1029 case DAHDI_MAINT_REMOTELOOP:
1030 t1out(p->tor,tspan,0xa8,0x80); /* remote loop */
1031 break;
1032 case DAHDI_MAINT_LOOPUP:
1033 case DAHDI_MAINT_LOOPDOWN:
1034 return -ENOSYS;
1035 default:
1036 printk(KERN_NOTICE "Tor2: Unknown maint command: %d\n", cmd);
1037 break;
1038 }
1039 return 0;
1040 }
1041 switch(cmd) {
1042 case DAHDI_MAINT_NONE:
1043 t1out(p->tor,tspan,0x19,(japan ? 0x80 : 0x00)); /* no local loop */
1044 t1out(p->tor,tspan,0x0a,0); /* no remote loop */
1045 t1out(p->tor, tspan, 0x30, 0); /* stop sending loopup code */
1046 break;
1047 case DAHDI_MAINT_LOCALLOOP:
1048 t1out(p->tor,tspan,0x19,0x40 | (japan ? 0x80 : 0x00)); /* local loop */
1049 t1out(p->tor,tspan,0x0a,0); /* no remote loop */
1050 break;
1051 case DAHDI_MAINT_REMOTELOOP:
1052 t1out(p->tor,tspan,0x1e,(japan ? 0x80 : 0x00)); /* no local loop */
1053 t1out(p->tor,tspan,0x0a,0x40); /* remote loop */
1054 break;
1055 case DAHDI_MAINT_LOOPUP:
1056 t1out(p->tor,tspan,0x30,2); /* send loopup code */
1057 t1out(p->tor,tspan,0x12,0x22); /* send loopup code */
1058 t1out(p->tor,tspan,0x13,0x80); /* send loopup code */
1059 break;
1060 case DAHDI_MAINT_LOOPDOWN:
1061 t1out(p->tor,tspan,0x30,2); /* send loopdown code */
1062 t1out(p->tor,tspan,0x12,0x62); /* send loopdown code */
1063 t1out(p->tor,tspan,0x13,0x90); /* send loopdown code */
1064 break;
1065 default:
1066 printk(KERN_NOTICE "Tor2: Unknown maint command: %d\n", cmd);
1067 break;
1068 }
1069 return 0;
1070 }
1071
1072 static inline void tor2_run(struct tor2 *tor)
1073 {
1074 int x,y;
1075 for (x = 0; x < SPANS_PER_CARD; x++) {
1076 struct dahdi_span *const s = &tor->tspans[x].dahdi_span;
1077 if (s->flags & DAHDI_FLAG_RUNNING) {
1078 /* since the Tormenta 2 PCI is double-buffered, you
1079 need to delay the transmit data 2 entire chunks so
1080 that the transmit will be in sync with the receive */
1081 for (y = 0; y < s->channels; y++) {
1082 dahdi_ec_chunk(s->chans[y],
1083 s->chans[y]->readchunk,
1084 tor->ec_chunk2[x][y]);
1085 memcpy(tor->ec_chunk2[x][y],tor->ec_chunk1[x][y],
1086 DAHDI_CHUNKSIZE);
1087 memcpy(tor->ec_chunk1[x][y],
1088 s->chans[y]->writechunk,
1089 DAHDI_CHUNKSIZE);
1090 }
1091 dahdi_receive(s);
1092 }
1093 }
1094 for (x = 0; x < SPANS_PER_CARD; x++) {
1095 struct dahdi_span *const s = &tor->tspans[x].dahdi_span;
1096 if (s->flags & DAHDI_FLAG_RUNNING)
1097 dahdi_transmit(s);
1098 }
1099 }
1100
1101 #ifdef ENABLE_TASKLETS
1102 static void tor2_tasklet(unsigned long data)
1103 {
1104 struct tor2 *tor = (struct tor2 *)data;
1105 tor->taskletrun++;
1106 if (tor->taskletpending) {
1107 tor->taskletexec++;
1108 tor2_run(tor);
1109 }
1110 tor->taskletpending = 0;
1111 }
1112 #endif
1113
1114 static int syncsrc = 0;
1115 static int syncnum = 0 /* -1 */;
1116 static int syncspan = 0;
1117 static DEFINE_SPINLOCK(synclock);
1118
1119 static int tor2_findsync(struct tor2 *tor)
1120 {
1121 int i;
1122 int x;
1123 unsigned long flags;
1124 int p;
1125 int nonzero;
1126 int newsyncsrc = 0; /* DAHDI span number */
1127 int newsyncnum = 0; /* tor2 card number */
1128 int newsyncspan = 0; /* span on given tor2 card */
1129 spin_lock_irqsave(&synclock, flags);
1130 #if 1
1131 if (!tor->num) {
1132 /* If we're the first card, go through all the motions, up to 8 levels
1133 of sync source */
1134 p = 1;
1135 while (p < 8) {
1136 nonzero = 0;
1137 for (x=0;cards[x];x++) {
1138 for (i = 0; i < SPANS_PER_CARD; i++) {
1139 if (cards[x]->syncpos[i]) {
1140 nonzero = 1;
1141 if ((cards[x]->syncpos[i] == p) &&
1142 !(cards[x]->tspans[i].dahdi_span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_LOOPBACK)) &&
1143 (cards[x]->tspans[i].dahdi_span.flags & DAHDI_FLAG_RUNNING)) {
1144 /* This makes a good sync source */
1145 newsyncsrc = cards[x]->tspans[i].dahdi_span.spanno;
1146 newsyncnum = x;
1147 newsyncspan = i + 1;
1148 /* Jump out */
1149 goto found;
1150 }
1151 }
1152 }
1153 }
1154 if (nonzero)
1155 p++;
1156 else
1157 break;
1158 }
1159 found:
1160 if ((syncnum != newsyncnum) || (syncsrc != newsyncsrc) || (newsyncspan != syncspan)) {
1161 syncnum = newsyncnum;
1162 syncsrc = newsyncsrc;
1163 syncspan = newsyncspan;
1164 if (debug) printk(KERN_DEBUG "New syncnum: %d, syncsrc: %d, syncspan: %d\n", syncnum, syncsrc, syncspan);
1165 }
1166 }
1167 #endif
1168 /* update sync src info */
1169 if (tor->syncsrc != syncsrc) {
1170 tor->syncsrc = syncsrc;
1171 /* Update sync sources */
1172 for (i = 0; i < SPANS_PER_CARD; i++) {
1173 tor->tspans[i].dahdi_span.syncsrc = tor->syncsrc;
1174 }
1175 if (syncnum == tor->num) {
1176 #if 1
1177 /* actually set the sync register */
1178 writeb(syncspan, &tor->mem8[SYNCREG]);
1179 #endif
1180 if (debug) printk(KERN_DEBUG "Card %d, using sync span %d, master\n", tor->num, syncspan);
1181 tor->master = MASTER;
1182 } else {
1183 #if 1
1184 /* time from the timing cable */
1185 writeb(SYNCEXTERN, &tor->mem8[SYNCREG]);
1186 #endif
1187 tor->master = 0;
1188 if (debug) printk(KERN_DEBUG "Card %d, using Timing Bus, NOT master\n", tor->num);
1189 }
1190 }
1191 spin_unlock_irqrestore(&synclock, flags);
1192 return 0;
1193 }
1194
1195 DAHDI_IRQ_HANDLER(tor2_intr)
1196 {
1197 int n, i, j, k, newsyncsrc;
1198 unsigned int rxword,txword;
1199
1200 unsigned char c, rxc;
1201 unsigned char abits, bbits;
1202 struct tor2 *tor = (struct tor2 *) dev_id;
1203 unsigned long flags;
1204
1205 /* make sure its a real interrupt for us */
1206 if (!(readb(&tor->mem8[STATREG]) & INTACTIVE)) /* if not, just return */
1207 {
1208 return IRQ_NONE;
1209 }
1210
1211 local_irq_save(flags);
1212
1213 if (tor->cardtype == TYPE_E1) {
1214 /* set outbit, interrupt enable, and ack interrupt */
1215 writeb(OUTBIT | INTENA | INTACK | E1DIV | tor->master,
1216 &tor->mem8[CTLREG]);
1217 } else {
1218 /* set outbit, interrupt enable, and ack interrupt */
1219 writeb(OUTBIT | INTENA | INTACK | tor->master,
1220 &tor->mem8[CTLREG]);
1221 }
1222
1223 #if 0
1224 if (!tor->passno)
1225 printk(KERN_DEBUG "Interrupt handler\n");
1226 #endif
1227
1228 /* do the transmit output */
1229 for (n = 0; n < tor->tspans[0].dahdi_span.channels; n++) {
1230 for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
1231 /* span 1 */
1232 txword = tor->tspans[0].dahdi_span.chans[n]->writechunk[i] << 24;
1233 /* span 2 */
1234 txword |= tor->tspans[1].dahdi_span.chans[n]->writechunk[i] << 16;
1235 /* span 3 */
1236 txword |= tor->tspans[2].dahdi_span.chans[n]->writechunk[i] << 8;
1237 /* span 4 */
1238 txword |= tor->tspans[3].dahdi_span.chans[n]->writechunk[i];
1239 /* write to part */
1240 #ifdef FIXTHISFOR64
1241 tor->mem32[tor->datxlt[n] + (32 * i)] = txword;
1242 #else
1243 writel(txword, &tor->mem32[tor->datxlt[n] + (32 * i)]);
1244 #endif
1245 }
1246 }
1247
1248 /* Do the receive input */
1249 for (n = 0; n < tor->tspans[0].dahdi_span.channels; n++) {
1250 for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
1251 /* read from */
1252 #ifdef FIXTHISFOR64
1253 rxword = tor->mem32[tor->datxlt[n] + (32 * i)];
1254 #else
1255 rxword = readl(&tor->mem32[tor->datxlt[n] + (32 * i)]);
1256 #endif
1257 /* span 1 */
1258 tor->tspans[0].dahdi_span.chans[n]->readchunk[i] = rxword >> 24;
1259 /* span 2 */
1260 tor->tspans[1].dahdi_span.chans[n]->readchunk[i] = (rxword & 0xff0000) >> 16;
1261 /* span 3 */
1262 tor->tspans[2].dahdi_span.chans[n]->readchunk[i] = (rxword & 0xff00) >> 8;
1263 /* span 4 */
1264 tor->tspans[3].dahdi_span.chans[n]->readchunk[i] = rxword & 0xff;
1265 }
1266 }
1267
1268 i = tor->passno & 15;
1269 /* if an E1 card, do rx signalling for it */
1270 if ((i < 3) && (tor->cardtype == TYPE_E1)) { /* if an E1 card */
1271 for (j = (i * 5); j < (i * 5) + 5; j++) {
1272 for (k = 1; k <= SPANS_PER_CARD; k++) {
1273 c = t1in(tor,k,0x31 + j);
1274 rxc = c & 15;
1275 if (rxc != tor->tspans[k - 1].dahdi_span.chans[j + 16]->rxsig) {
1276 /* Check for changes in received bits */
1277 if (!(tor->tspans[k - 1].dahdi_span.chans[j + 16]->sig & DAHDI_SIG_CLEAR))
1278 dahdi_rbsbits(tor->tspans[k - 1].dahdi_span.chans[j + 16], rxc);
1279 }
1280 rxc = c >> 4;
1281 if (rxc != tor->tspans[k - 1].dahdi_span.chans[j]->rxsig) {
1282 /* Check for changes in received bits */
1283 if (!(tor->tspans[k - 1].dahdi_span.chans[j]->sig & DAHDI_SIG_CLEAR))
1284 dahdi_rbsbits(tor->tspans[k - 1].dahdi_span.chans[j], rxc);
1285 }
1286 }
1287 }
1288 }
1289
1290 /* if a T1, do the signalling */
1291 if ((i < 12) && (tor->cardtype == TYPE_T1)) {
1292 k = (i / 3); /* get span */
1293 n = (i % 3); /* get base */
1294 abits = t1in(tor,k + 1, 0x60 + n);
1295 bbits = t1in(tor,k + 1, 0x63 + n);
1296 for (j=0; j< 8; j++) {
1297 /* Get channel number */
1298 i = (n * 8) + j;
1299 rxc = 0;
1300 if (abits & (1 << j)) rxc |= DAHDI_ABIT;
1301 if (bbits & (1 << j)) rxc |= DAHDI_BBIT;
1302 if (tor->tspans[k].dahdi_span.chans[i]->rxsig != rxc) {
1303 /* Check for changes in received bits */
1304 if (!(tor->tspans[k].dahdi_span.chans[i]->sig & DAHDI_SIG_CLEAR))
1305 dahdi_rbsbits(tor->tspans[k].dahdi_span.chans[i], rxc);
1306 }
1307 }
1308 }
1309
1310 for (i = 0; i < SPANS_PER_CARD; i++) { /* Go thru all the spans */
1311 /* if alarm timer, and it's timed out */
1312 if (tor->alarmtimer[i]) {
1313 if (!--tor->alarmtimer[i]) {
1314 /* clear recover status */
1315 tor->tspans[i].dahdi_span.alarms &= ~DAHDI_ALARM_RECOVER;
1316 if (tor->cardtype == TYPE_E1)
1317 t1out(tor,i + 1,0x21,0x5f); /* turn off yel */
1318 else
1319 t1out(tor,i + 1,0x35,0x10); /* turn off yel */
1320 dahdi_alarm_notify(&tor->tspans[i].dahdi_span); /* let them know */
1321 }
1322 }
1323 }
1324
1325 i = tor->passno & 15;
1326 if ((i >= 10) && (i <= 13) && !(tor->passno & 0x30))
1327 {
1328 j = 0; /* clear this alarm status */
1329 i -= 10;
1330 if (tor->cardtype == TYPE_T1) {
1331 c = t1in(tor,i + 1,0x31); /* get RIR2 */
1332 tor->tspans[i].dahdi_span.rxlevel = c >> 6; /* get rx level */
1333 t1out(tor,i + 1,0x20,0xff);
1334 c = t1in(tor,i + 1,0x20); /* get the status */
1335 /* detect the code, only if we are not sending one */
1336 if ((!tor->tspans[i].dahdi_span.mainttimer) && (c & 0x80)) /* if loop-up code detected */
1337 {
1338 /* set into remote loop, if not there already */
1339 if ((tor->loopupcnt[i]++ > 80) &&
1340 (tor->tspans[i].dahdi_span.maintstat != DAHDI_MAINT_REMOTELOOP))
1341 {
1342 t1out(tor,i + 1,0x1e,(japan ? 0x80 : 0x00)); /* no local loop */
1343 t1out(tor,i + 1,0x0a,0x40); /* remote loop */
1344 tor->tspans[i].dahdi_span.maintstat = DAHDI_MAINT_REMOTELOOP;
1345 }
1346 } else tor->loopupcnt[i] = 0;
1347 /* detect the code, only if we are not sending one */
1348 if ((!tor->tspans[i].dahdi_span.mainttimer) && (c & 0x40)) /* if loop-down code detected */
1349 {
1350 /* if in remote loop, get out of it */
1351 if ((tor->loopdowncnt[i]++ > 80) &&
1352 (tor->tspans[i].dahdi_span.maintstat == DAHDI_MAINT_REMOTELOOP))
1353 {
1354 t1out(tor,i + 1,0x1e,(japan ? 0x80 : 0x00)); /* no local loop */
1355 t1out(tor,i + 1,0x0a,0); /* no remote loop */
1356 tor->tspans[i].dahdi_span.maintstat = DAHDI_MAINT_NONE;
1357 }
1358 } else tor->loopdowncnt[i] = 0;
1359 if (c & 3) /* if red alarm */
1360 {
1361 j |= DAHDI_ALARM_RED;
1362 }
1363 if (c & 8) /* if blue alarm */
1364 {
1365 j |= DAHDI_ALARM_BLUE;
1366 }
1367 } else { /* its an E1 card */
1368 t1out(tor,i + 1,6,0xff);
1369 c = t1in(tor,i + 1,6); /* get the status */
1370 if (c & 9) /* if red alarm */
1371 {
1372 j |= DAHDI_ALARM_RED;
1373 }
1374 if (c & 2) /* if blue alarm */
1375 {
1376 j |= DAHDI_ALARM_BLUE;
1377 }
1378 }
1379 /* only consider previous carrier alarm state */
1380 tor->tspans[i].dahdi_span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
1381 n = 1; /* set to 1 so will not be in yellow alarm if we dont
1382 care about open channels */
1383 /* if to have yellow alarm if nothing open */
1384 if (tor->tspans[i].dahdi_span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
1385 /* go thru all chans, and count # open */
1386 for (n = 0, k = 0; k < tor->tspans[i].dahdi_span.channels; k++) {
1387 if (((tor->chans[i][k])->flags & DAHDI_FLAG_OPEN) ||
1388 dahdi_have_netdev(tor->chans[i][k]))
1389 n++;
1390 }
1391 /* if none open, set alarm condition */
1392 if (!n)
1393 j |= DAHDI_ALARM_NOTOPEN;
1394 }
1395 /* if no more alarms, and we had some */
1396 if ((!j) && tor->tspans[i].dahdi_span.alarms)
1397 tor->alarmtimer[i] = DAHDI_ALARMSETTLE_TIME;
1398 if (tor->alarmtimer[i]) j |= DAHDI_ALARM_RECOVER;
1399 /* if going into alarm state, set yellow alarm */
1400 if ((j) && (!tor->tspans[i].dahdi_span.alarms)) {
1401 if (tor->cardtype == TYPE_E1)
1402 t1out(tor,i + 1,0x21,0x7f);
1403 else
1404 t1out(tor,i + 1,0x35,0x11);
1405 }
1406 if (c & 4) /* if yellow alarm */
1407 j |= DAHDI_ALARM_YELLOW;
1408 if (tor->tspans[i].dahdi_span.maintstat || tor->tspans[i].dahdi_span.mainttimer)
1409 j |= DAHDI_ALARM_LOOPBACK;
1410 tor->tspans[i].dahdi_span.alarms = j;
1411 c = (LEDRED | LEDGREEN) << (2 * i);
1412 tor->leds &= ~c; /* mask out bits for this span */
1413 /* light LED's if span configured and running */
1414 if (tor->tspans[i].dahdi_span.flags & DAHDI_FLAG_RUNNING) {
1415 if (j & DAHDI_ALARM_RED) tor->leds |= LEDRED << (2 * i);
1416 else if (j & DAHDI_ALARM_YELLOW) tor->leds |= (LEDRED | LEDGREEN) << (2 * i);
1417 else tor->leds |= LEDGREEN << (2 * i);
1418 }
1419 writeb(tor->leds, &tor->mem8[LEDREG]);
1420 dahdi_alarm_notify(&tor->tspans[i].dahdi_span);
1421 }
1422 if (!(tor->passno % 1000)) /* even second boundary */
1423 {
1424 /* do all spans */
1425 for (i = 1; i <= SPANS_PER_CARD; i++)
1426 {
1427 if (tor->cardtype == TYPE_E1)
1428 {
1429 /* add this second's BPV count to total one */
1430 tor->tspans[i - 1].dahdi_span.count.bpv +=
1431 t1in(tor, i, 1) + (t1in(tor, i, 0)<<8);
1432
1433 if (tor->tspans[i - 1].dahdi_span.lineconfig & DAHDI_CONFIG_CRC4)
1434 {
1435 tor->tspans[i - 1].dahdi_span.count.crc4 +=
1436 t1in(tor, i, 3) +
1437 ((t1in(tor, i, 2) & 3) << 8);
1438 tor->tspans[i - 1].dahdi_span.count.ebit +=
1439 t1in(tor, i, 5) +
1440 ((t1in(tor, i, 4) & 3) << 8);
1441 }
1442 tor->tspans[i - 1].dahdi_span.count.fas +=
1443 (t1in(tor, i, 4) >> 2) +
1444 ((t1in(tor, i, 2) & 0x3F) << 6);
1445 }
1446 else
1447 {
1448 /* add this second's BPV count to total one */
1449 tor->tspans[i - 1].dahdi_span.count.bpv +=
1450 t1in(tor, i, 0x24) +
1451 (t1in(tor, i, 0x23) << 8);
1452 }
1453 }
1454 }
1455 if (!timingcable) {
1456 /* re-evaluate active sync src (no cable version) */
1457 tor->syncsrc = 0;
1458 newsyncsrc = 0;
1459 /* if primary sync specified, see if we can use it */
1460 if (tor->psyncs[0])
1461 {
1462 /* if no alarms, use it */
1463 if (!(tor->tspans[tor->psyncs[0] - 1].dahdi_span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE |
1464 DAHDI_ALARM_LOOPBACK))) {
1465 tor->syncsrc = tor->psyncs[0];
1466 newsyncsrc = tor->syncs[0];
1467 }
1468 }
1469 /* if any others specified, see if we can use them */
1470 for (i = 1; i < SPANS_PER_CARD; i++) {
1471 /* if we dont have one yet, and there is one specified at this level, see if we can use it */
1472 if ((!tor->syncsrc) && (tor->psyncs[i])) {
1473 /* if no alarms, use it */
1474 if (!(tor->tspans[tor->psyncs[i] - 1].dahdi_span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE |
1475 DAHDI_ALARM_LOOPBACK))) {
1476 tor->syncsrc = tor->psyncs[i];
1477 newsyncsrc = tor->syncs[i];
1478 }
1479 }
1480 }
1481 /* update sync src info */
1482 for (i = 0; i < SPANS_PER_CARD; i++)
1483 tor->tspans[i].dahdi_span.syncsrc = newsyncsrc;
1484
1485 /* actually set the sync register */
1486 writeb(tor->syncsrc, &tor->mem8[SYNCREG]);
1487 } else /* Timing cable version */
1488 tor2_findsync(tor);
1489
1490 tor->passno++;
1491
1492 #ifdef ENABLE_TASKLETS
1493 if (!tor->taskletpending) {
1494 tor->taskletpending = 1;
1495 tor->taskletsched++;
1496 tasklet_hi_schedule(&tor->tor2_tlet);
1497 } else {
1498 tor->txerrors++;
1499 }
1500 #else
1501 tor2_run(tor);
1502 #endif
1503 /* We are not the timing bus master */
1504 if (tor->cardtype == TYPE_E1)
1505 /* clear OUTBIT and enable interrupts */
1506 writeb(INTENA | E1DIV | tor->master, &tor->mem8[CTLREG]);
1507 else
1508 /* clear OUTBIT and enable interrupts */
1509 writeb(INTENA | tor->master, &tor->mem8[CTLREG]);
1510
1511 local_irq_restore(flags);
1512 return IRQ_RETVAL(1);
1513 }
1514
1515
1516 static int tor2_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
1517 {
1518 switch(cmd) {
1519 default:
1520 return -ENOTTY;
1521 }
1522 return 0;
1523 }
1524
1525 MODULE_AUTHOR("Mark Spencer");
1526 MODULE_DESCRIPTION("Tormenta 2 PCI Quad T1 or E1 DAHDI Driver");
1527 MODULE_LICENSE("GPL v2");
1528
1529 module_param(debug, int, 0600);
1530 module_param(loopback, int, 0600);
1531 module_param(timingcable, int, 0600);
1532 module_param(japan, int, 0600);
1533
1534 MODULE_DEVICE_TABLE(pci, tor2_pci_ids);
1535
1536 module_init(tor2_init);
1537 module_exit(tor2_cleanup);