Debian Codegraph @

drivers/dahdi/wct4xxp/base.c

raw symbols SCIP
1 /*
2 * TE410P Quad-T1/E1 PCI Driver version 0.1, 12/16/02
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 * Further modified, optimized, and maintained by
8 * Matthew Fredrickson <[email protected]> and
9 * Russ Meyerriecks <[email protected]>
10 *
11 * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
12 * Copyright (C) 2001-2012, Digium, Inc.
13 *
14 * All rights reserved.
15 *
16 */
17
18 /*
19 * See http://www.asterisk.org for more information about
20 * the Asterisk project. Please do not directly contact
21 * any of the maintainers of this project for assistance;
22 * the project provides a web site, mailing lists and IRC
23 * channels for your use.
24 *
25 * This program is free software, distributed under the terms of
26 * the GNU General Public License Version 2 as published by the
27 * Free Software Foundation. See the LICENSE file included with
28 * this program for more details.
29 */
30 #include <linux/kernel.h>
31 #include <linux/errno.h>
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/init.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/spinlock.h>
38 #include <asm/io.h>
39 #include <linux/version.h>
40 #include <linux/delay.h>
41 #include <linux/moduleparam.h>
42 #include <linux/crc32.h>
43 #include <linux/slab.h>
44
45 #include <stdbool.h>
46 #include <dahdi/kernel.h>
47
48 #include "wct4xxp.h"
49 #include "vpm450m.h"
50
51 /* Work queues are a way to better distribute load on SMP systems */
52 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
53 /*
54 * Work queues can significantly improve performance and scalability
55 * on multi-processor machines, but requires bypassing some kernel
56 * API's, so it's not guaranteed to be compatible with all kernels.
57 */
58 /* #define ENABLE_WORKQUEUES */
59 #endif
60
61 /* Support first generation cards? */
62 #define SUPPORT_GEN1
63
64 /* Define to get more attention-grabbing but slightly more I/O using
65 alarm status */
66 #undef FANCY_ALARM
67
68 /* Define to support Digium Voice Processing Module expansion card */
69 #define VPM_SUPPORT
70
71 #define DEBUG_MAIN (1 << 0)
72 #define DEBUG_DTMF (1 << 1)
73 #define DEBUG_REGS (1 << 2)
74 #define DEBUG_TSI (1 << 3)
75 #define DEBUG_ECHOCAN (1 << 4)
76 #define DEBUG_RBS (1 << 5)
77 #define DEBUG_FRAMER (1 << 6)
78
79 /* Maximum latency to be used with Gen 5 */
80 #define GEN5_MAX_LATENCY 127
81
82 #ifdef ENABLE_WORKQUEUES
83 #include <linux/cpu.h>
84
85 /* XXX UGLY!!!! XXX We have to access the direct structures of the workqueue which
86 are only defined within workqueue.c because they don't give us a routine to allow us
87 to nail a work to a particular thread of the CPU. Nailing to threads gives us substantially
88 higher scalability in multi-CPU environments though! */
89
90 /*
91 * The per-CPU workqueue (if single thread, we always use cpu 0's).
92 *
93 * The sequence counters are for flush_scheduled_work(). It wants to wait
94 * until until all currently-scheduled works are completed, but it doesn't
95 * want to be livelocked by new, incoming ones. So it waits until
96 * remove_sequence is >= the insert_sequence which pertained when
97 * flush_scheduled_work() was called.
98 */
99
100 struct cpu_workqueue_struct {
101
102 spinlock_t lock;
103
104 long remove_sequence; /* Least-recently added (next to run) */
105 long insert_sequence; /* Next to add */
106
107 struct list_head worklist;
108 wait_queue_head_t more_work;
109 wait_queue_head_t work_done;
110
111 struct workqueue_struct *wq;
112 task_t *thread;
113
114 int run_depth; /* Detect run_workqueue() recursion depth */
115 } ____cacheline_aligned;
116
117 /*
118 * The externally visible workqueue abstraction is an array of
119 * per-CPU workqueues:
120 */
121 struct workqueue_struct {
122 /* TODO: Find out exactly where the API changed */
123 struct cpu_workqueue_struct *cpu_wq;
124 const char *name;
125 struct list_head list; /* Empty if single thread */
126 };
127
128 /* Preempt must be disabled. */
129 static void __t4_queue_work(struct cpu_workqueue_struct *cwq,
130 struct work_struct *work)
131 {
132 unsigned long flags;
133
134 spin_lock_irqsave(&cwq->lock, flags);
135 work->wq_data = cwq;
136 list_add_tail(&work->entry, &cwq->worklist);
137 cwq->insert_sequence++;
138 wake_up(&cwq->more_work);
139 spin_unlock_irqrestore(&cwq->lock, flags);
140 }
141
142 /*
143 * Queue work on a workqueue. Return non-zero if it was successfully
144 * added.
145 *
146 * We queue the work to the CPU it was submitted, but there is no
147 * guarantee that it will be processed by that CPU.
148 */
149 static inline int t4_queue_work(struct workqueue_struct *wq, struct work_struct *work, int cpu)
150 {
151 int ret = 0;
152 get_cpu();
153 if (!test_and_set_bit(0, &work->pending)) {
154 BUG_ON(!list_empty(&work->entry));
155 __t4_queue_work(wq->cpu_wq + cpu, work);
156 ret = 1;
157 }
158 put_cpu();
159 return ret;
160 }
161
162 #endif
163
164 /*
165 * Define CONFIG_FORCE_EXTENDED_RESET to allow the qfalc framer extra time
166 * to reset itself upon hardware initialization. This exits for rare
167 * cases for customers who are seeing the qfalc returning unexpected
168 * information at initialization
169 */
170 /* #define CONFIG_FORCE_EXTENDED_RESET */
171 /* #define CONFIG_NOEXTENDED_RESET */
172
173 /*
174 * Uncomment the following definition in order to disable Active-State Power
175 * Management on the PCIe bridge for PCIe cards. This has been known to work
176 * around issues where the BIOS enables it on the cards even though the
177 * platform does not support it.
178 *
179 */
180 /* #define CONFIG_WCT4XXP_DISABLE_ASPM */
181
182 #if defined(CONFIG_FORCE_EXTENDED_RESET) && defined(CONFIG_NOEXTENDED_RESET)
183 #error "You cannot define both CONFIG_FORCE_EXTENDED_RESET and " \
184 "CONFIG_NOEXTENDED_RESET."
185 #endif
186
187 int debug = 0;
188 static int timingcable = 0;
189 static int t1e1override = -1; /* deprecated */
190 static char *default_linemode = "auto";
191 static int j1mode = 0;
192 static int sigmode = FRMR_MODE_NO_ADDR_CMP;
193 static int alarmdebounce = 2500; /* LOF/LFA def to 2.5s AT&T TR54016*/
194 static int losalarmdebounce = 2500;/* LOS def to 2.5s AT&T TR54016*/
195 static int aisalarmdebounce = 2500;/* AIS(blue) def to 2.5s AT&T TR54016*/
196 static int yelalarmdebounce = 500;/* RAI(yellow) def to 0.5s AT&T devguide */
197 static int max_latency = GEN5_MAX_LATENCY; /* Used to set a maximum latency (if you don't wish it to hard cap it at a certain value) in milliseconds */
198 #ifdef VPM_SUPPORT
199 static int vpmsupport = 1;
200 /* If set to auto, vpmdtmfsupport is enabled for VPM400M and disabled for VPM450M */
201 static int vpmdtmfsupport = -1; /* -1=auto, 0=disabled, 1=enabled*/
202 #endif /* VPM_SUPPORT */
203
204 /* Enabling bursting can more efficiently utilize PCI bus bandwidth, but
205 can also cause PCI bus starvation, especially in combination with other
206 aggressive cards. Please note that burst mode has no effect on CPU
207 utilization / max number of calls / etc. */
208 static int noburst;
209 /* For 56kbps links, set this module parameter to 0x7f */
210 static int hardhdlcmode = 0xff;
211
212 static int latency = 1;
213
214 static int ms_per_irq = 1;
215 static int ignore_rotary;
216
217 #ifdef FANCY_ALARM
218 static int altab[] = {
219 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,
220 };
221 #endif
222
223 #define MAX_SPANS 16
224
225 #define FLAG_STARTED (1 << 0)
226 #define FLAG_NMF (1 << 1)
227 #define FLAG_SENDINGYELLOW (1 << 2)
228
229 #define FLAG_2NDGEN (1 << 3)
230 #define FLAG_2PORT (1 << 4)
231 #define FLAG_VPM2GEN (1 << 5)
232 #define FLAG_OCTOPT (1 << 6)
233 #define FLAG_3RDGEN (1 << 7)
234 #define FLAG_BURST (1 << 8)
235 #define FLAG_EXPRESS (1 << 9)
236 #define FLAG_5THGEN (1 << 10)
237 #define FLAG_8PORT (1 << 11)
238
239 #define CANARY 0xc0de
240
241 /* names of available HWEC modules */
242 #ifdef VPM_SUPPORT
243 #define T4_VPM_PRESENT (1 << 28)
244 static const char *vpmoct064_name = "VPMOCT064";
245 static const char *vpmoct128_name = "VPMOCT128";
246 static const char *vpmoct256_name = "VPMOCT256";
247 #endif
248
249 struct devtype {
250 char *desc;
251 unsigned int flags;
252 };
253
254 static struct devtype wct820p5 = { "Wildcard TE820 (5th Gen)", FLAG_8PORT | FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_EXPRESS };
255 static struct devtype wct420p5 = { "Wildcard TE420 (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_EXPRESS };
256 static struct devtype wct410p5 = { "Wildcard TE410P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
257 static struct devtype wct405p5 = { "Wildcard TE405P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
258 static struct devtype wct220p5 = { "Wildcard TE220 (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT | FLAG_EXPRESS };
259 static struct devtype wct210p5 = { "Wildcard TE210P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
260 static struct devtype wct205p5 = { "Wildcard TE205P (5th Gen)", FLAG_5THGEN | FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
261
262 static struct devtype wct4xxp = { "Wildcard TE410P/TE405P (1st Gen)", 0 };
263 static struct devtype wct420p4 = { "Wildcard TE420 (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_EXPRESS };
264 static struct devtype wct410p4 = { "Wildcard TE410P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
265 static struct devtype wct410p3 = { "Wildcard TE410P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN };
266 static struct devtype wct405p4 = { "Wildcard TE405P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN };
267 static struct devtype wct405p3 = { "Wildcard TE405P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN };
268 static struct devtype wct410p2 = { "Wildcard TE410P (2nd Gen)", FLAG_2NDGEN };
269 static struct devtype wct405p2 = { "Wildcard TE405P (2nd Gen)", FLAG_2NDGEN };
270 static struct devtype wct220p4 = { "Wildcard TE220 (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT | FLAG_EXPRESS };
271 static struct devtype wct205p4 = { "Wildcard TE205P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
272 static struct devtype wct205p3 = { "Wildcard TE205P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
273 static struct devtype wct210p4 = { "Wildcard TE210P (4th Gen)", FLAG_BURST | FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
274 static struct devtype wct210p3 = { "Wildcard TE210P (3rd Gen)", FLAG_2NDGEN | FLAG_3RDGEN | FLAG_2PORT };
275 static struct devtype wct205 = { "Wildcard TE205P ", FLAG_2NDGEN | FLAG_2PORT };
276 static struct devtype wct210 = { "Wildcard TE210P ", FLAG_2NDGEN | FLAG_2PORT };
277
278
279 struct t4;
280
281 enum linemode {T1, E1, J1};
282
283 struct spi_state {
284 int wrreg;
285 int rdreg;
286 };
287
288 struct t4_span {
289 struct t4 *owner;
290 u32 *writechunk; /* Double-word aligned write memory */
291 u32 *readchunk; /* Double-word aligned read memory */
292 enum linemode linemode;
293 int sync;
294 int alarmtimer;
295 int notclear;
296 unsigned long alarm_time;
297 unsigned long losalarm_time;
298 unsigned long aisalarm_time;
299 unsigned long yelalarm_time;
300 unsigned long alarmcheck_time;
301 int spanflags;
302 int syncpos;
303
304 #ifdef SUPPORT_GEN1
305 int e1check; /* E1 check */
306 #endif
307 struct dahdi_span span;
308 unsigned char txsigs[16]; /* Transmit sigs */
309 int loopupcnt;
310 int loopdowncnt;
311 #ifdef SUPPORT_GEN1
312 unsigned char ec_chunk1[31][DAHDI_CHUNKSIZE]; /* first EC chunk buffer */
313 unsigned char ec_chunk2[31][DAHDI_CHUNKSIZE]; /* second EC chunk buffer */
314 #endif
315 /* HDLC controller fields */
316 struct dahdi_chan *sigchan;
317 unsigned char sigmode;
318 int sigactive;
319 int frames_out;
320 int frames_in;
321
322 #ifdef VPM_SUPPORT
323 unsigned long dtmfactive;
324 unsigned long dtmfmask;
325 unsigned long dtmfmutemask;
326 #endif
327 #ifdef ENABLE_WORKQUEUES
328 struct work_struct swork;
329 #endif
330 struct dahdi_chan *chans[32]; /* Individual channels */
331 struct dahdi_echocan_state *ec[32]; /* Echocan state for each channel */
332 };
333
334 struct t4 {
335 /* This structure exists one per card */
336 struct pci_dev *dev; /* Pointer to PCI device */
337 unsigned int intcount;
338 int num; /* Which card we are */
339 int syncsrc; /* active sync source */
340 struct dahdi_device *ddev;
341 struct t4_span *tspans[8]; /* Individual spans */
342 int numspans; /* Number of spans on the card */
343 int blinktimer;
344 #ifdef FANCY_ALARM
345 int alarmpos;
346 #endif
347 int irq; /* IRQ used by device */
348 int order; /* Order */
349 const struct devtype *devtype;
350 unsigned int reset_required:1; /* If reset needed in serial_setup */
351 unsigned int falc31:1; /* are we falc v3.1 (atomic not necessary) */
352 unsigned int t1e1:8; /* T1 / E1 select pins */
353 int ledreg; /* LED Register */
354 int ledreg2; /* LED Register2 */
355 unsigned int gpio;
356 unsigned int gpioctl;
357 int e1recover; /* E1 recovery timer */
358 spinlock_t reglock; /* lock register access */
359 int spansstarted; /* number of spans started */
360 u32 *writechunk; /* Double-word aligned write memory */
361 u32 *readchunk; /* Double-word aligned read memory */
362 #ifdef ENABLE_WORKQUEUES
363 atomic_t worklist;
364 struct workqueue_struct *workq;
365 #endif
366 int last0; /* for detecting double-missed IRQ */
367
368 /* DMA related fields */
369 unsigned int dmactrl;
370 dma_addr_t readdma;
371 dma_addr_t writedma;
372 void __iomem *membase; /* Base address of card */
373
374 #define T4_CHECK_VPM 0
375 #define T4_LOADING_FW 1
376 #define T4_STOP_DMA 2
377 #define T4_CHECK_TIMING 3
378 #define T4_CHANGE_LATENCY 4
379 #define T4_IGNORE_LATENCY 5
380 unsigned long checkflag;
381 struct work_struct bh_work;
382 /* Latency related additions */
383 unsigned char rxident;
384 unsigned char lastindex;
385 int numbufs;
386 int needed_latency;
387
388 #ifdef VPM_SUPPORT
389 struct vpm450m *vpm;
390 #endif
391 struct spi_state st;
392 };
393
394 static inline bool is_pcie(const struct t4 *wc)
395 {
396 return (wc->devtype->flags & FLAG_EXPRESS) > 0;
397 }
398
399 static inline bool has_e1_span(const struct t4 *wc)
400 {
401 return (wc->t1e1 > 0);
402 }
403
404 static inline bool is_octal(const struct t4 *wc)
405 {
406 return (wc->devtype->flags & FLAG_8PORT) > 0;
407 }
408
409 static inline int T4_BASE_SIZE(struct t4 *wc)
410 {
411 if (is_octal(wc))
412 return DAHDI_MAX_CHUNKSIZE * 32 * 8;
413 else
414 return DAHDI_MAX_CHUNKSIZE * 32 * 4;
415 }
416
417 /**
418 * ports_on_framer - The number of ports on the framers.
419 * @wc: Board to check.
420 *
421 * The framer ports could be different the the number of ports on the card
422 * since the dual spans have four ports internally but two ports extenally.
423 *
424 */
425 static inline unsigned int ports_on_framer(const struct t4 *wc)
426 {
427 return (is_octal(wc)) ? 8 : 4;
428 }
429
430 #ifdef VPM_SUPPORT
431 static void t4_vpm_init(struct t4 *wc);
432
433 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
434
435 static const struct dahdi_echocan_features vpm_ec_features = {
436 .NLP_automatic = 1,
437 .CED_tx_detect = 1,
438 .CED_rx_detect = 1,
439 };
440
441 static const struct dahdi_echocan_ops vpm_ec_ops = {
442 .echocan_free = echocan_free,
443 };
444 #endif
445
446 static void __set_clear(struct t4 *wc, int span);
447 static int _t4_startup(struct file *file, struct dahdi_span *span);
448 static int t4_startup(struct file *file, struct dahdi_span *span);
449 static int t4_shutdown(struct dahdi_span *span);
450 static int t4_rbsbits(struct dahdi_chan *chan, int bits);
451 static int t4_maint(struct dahdi_span *span, int cmd);
452 static int t4_clear_maint(struct dahdi_span *span);
453 static int t4_reset_counters(struct dahdi_span *span);
454 #ifdef SUPPORT_GEN1
455 static int t4_reset_dma(struct t4 *wc);
456 #endif
457 static void t4_hdlc_hard_xmit(struct dahdi_chan *chan);
458 static int t4_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data);
459 static void t4_tsi_assign(struct t4 *wc, int fromspan, int fromchan, int tospan, int tochan);
460 static void t4_tsi_unassign(struct t4 *wc, int tospan, int tochan);
461 static void __t4_set_rclk_src(struct t4 *wc, int span);
462 static void __t4_set_sclk_src(struct t4 *wc, int mode, int master, int slave);
463 static void t4_check_alarms(struct t4 *wc, int span);
464 static void t4_check_sigbits(struct t4 *wc, int span);
465
466 #define WC_RDADDR 0
467 #define WC_WRADDR 1
468 #define WC_COUNT 2
469 #define WC_DMACTRL 3
470 #define WC_INTR 4
471 /* #define WC_GPIO 5 */
472 #define WC_VERSION 6
473 #define WC_LEDS 7
474 #define WC_GPIOCTL 8
475 #define WC_GPIO 9
476 #define WC_LADDR 10
477 #define WC_LDATA 11
478 #define WC_LEDS2 12
479
480 #define WC_SET_AUTH (1 << 20)
481 #define WC_GET_AUTH (1 << 12)
482
483 #define WC_LFRMR_CS (1 << 10) /* Framer's ChipSelect signal */
484 #define WC_LCS (1 << 11)
485 #define WC_LCS2 (1 << 12)
486 #define WC_LALE (1 << 13)
487 #define WC_LFRMR_CS2 (1 << 14) /* Framer's ChipSelect signal 2 */
488 #define WC_LREAD (1 << 15)
489 #define WC_LWRITE (1 << 16)
490
491 #define WC_ACTIVATE (1 << 12)
492
493 #define WC_OFF (0)
494 #define WC_RED (1)
495 #define WC_GREEN (2)
496 #define WC_YELLOW (3)
497
498 #define WC_RECOVER 0
499 #define WC_SELF 1
500
501 #define LIM0_T 0x36 /* Line interface mode 0 register */
502 #define LIM0_LL (1 << 1) /* Local Loop */
503 #define LIM1_T 0x37 /* Line interface mode 1 register */
504 #define LIM1_RL (1 << 1) /* Remote Loop */
505
506 #define FMR0 0x1C /* Framer Mode Register 0 */
507 #define FMR0_SIM (1 << 0) /* Alarm Simulation */
508 #define FMR1_T 0x1D /* Framer Mode Register 1 */
509 #define FMR1_ECM (1 << 2) /* Error Counter 1sec Interrupt Enable */
510 #define FMR5 0x21 /* Framer Mode Register 5 */
511 #define FMR5_XLU (1 << 4) /* Transmit loopup code */
512 #define FMR5_XLD (1 << 5) /* Transmit loopdown code */
513 #define FMR5_EIBR (1 << 6) /* Internal Bit Robbing Access */
514 #define DEC_T 0x60 /* Diable Error Counter */
515 #define IERR_T 0x1B /* Single Bit Defect Insertion Register */
516 #define IBV (1 << 0) /* Bipolar violation */
517 #define IPE (1 << 1) /* PRBS defect */
518 #define ICASE (1 << 2) /* CAS defect */
519 #define ICRCE (1 << 3) /* CRC defect */
520 #define IMFE (1 << 4) /* Multiframe defect */
521 #define IFASE (1 << 5) /* FAS defect */
522 #define ISR3_SEC (1 << 6) /* Internal one-second interrupt bit mask */
523 #define ISR3_ES (1 << 7) /* Errored Second interrupt bit mask */
524 #define ESM 0x47 /* Errored Second mask register */
525
526 #define FMR2_T 0x1E /* Framer Mode Register 2 */
527 #define FMR2_PLB (1 << 2) /* Framer Mode Register 2 */
528
529 #define FECL_T 0x50 /* Framing Error Counter Lower Byte */
530 #define FECH_T 0x51 /* Framing Error Counter Higher Byte */
531 #define CVCL_T 0x52 /* Code Violation Counter Lower Byte */
532 #define CVCH_T 0x53 /* Code Violation Counter Higher Byte */
533 #define CEC1L_T 0x54 /* CRC Error Counter 1 Lower Byte */
534 #define CEC1H_T 0x55 /* CRC Error Counter 1 Higher Byte */
535 #define EBCL_T 0x56 /* E-Bit Error Counter Lower Byte */
536 #define EBCH_T 0x57 /* E-Bit Error Counter Higher Byte */
537 #define BECL_T 0x58 /* Bit Error Counter Lower Byte */
538 #define BECH_T 0x59 /* Bit Error Counter Higher Byte */
539 #define COEC_T 0x5A /* COFA Event Counter */
540 #define PRBSSTA_T 0xDA /* PRBS Status Register */
541
542 #define LCR1_T 0x3B /* Loop Code Register 1 */
543 #define EPRM (1 << 7) /* Enable PRBS rx */
544 #define XPRBS (1 << 6) /* Enable PRBS tx */
545 #define FLLB (1 << 1) /* Framed line loop/Invert */
546 #define LLBP (1 << 0) /* Line Loopback Pattern */
547 #define TPC0_T 0xA8 /* Test Pattern Control Register */
548 #define FRA (1 << 6) /* Framed/Unframed Selection */
549 #define PRBS23 (3 << 4) /* Pattern selection (23 poly) */
550 #define PRM (1 << 2) /* Non framed mode */
551 #define FRS1_T 0x4D /* Framer Receive Status Reg 1 */
552 #define LLBDD (1 << 4)
553 #define LLBAD (1 << 3)
554
555 #define MAX_T4_CARDS 64
556
557 static struct t4 *cards[MAX_T4_CARDS];
558
559 struct t8_firm_header {
560 u8 header[6];
561 __le32 chksum;
562 u8 pad[18];
563 __le32 version;
564 } __packed;
565
566 #define MAX_TDM_CHAN 32
567 #define MAX_DTMF_DET 16
568
569 #define HDLC_IMR0_MASK (FRMR_IMR0_RME | FRMR_IMR0_RPF)
570 #define HDLC_IMR1_MASK (FRMR_IMR1_XDU | FRMR_IMR1_XPR)
571
572 static inline unsigned int __t4_pci_in(struct t4 *wc, const unsigned int addr)
573 {
574 unsigned int res = readl(wc->membase + (addr * sizeof(u32)));
575 return res;
576 }
577
578 static inline void __t4_pci_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
579 {
580 #ifdef DEBUG
581 unsigned int tmp;
582 #endif
583 writel(value, wc->membase + (addr * sizeof(u32)));
584 #ifdef DEBUG
585 tmp = __t4_pci_in(wc, WC_VERSION);
586 if ((tmp & 0xffff0000) != 0xc01a0000)
587 dev_notice(&wc->dev->dev,
588 "Version Synchronization Error!\n");
589 #else
590 __t4_pci_in(wc, WC_VERSION);
591 #endif
592 }
593
594 static inline void __t4_gpio_set(struct t4 *wc, unsigned bits, unsigned int val)
595 {
596 unsigned int newgpio;
597 newgpio = wc->gpio & (~bits);
598 newgpio |= val;
599 if (newgpio != wc->gpio) {
600 wc->gpio = newgpio;
601 __t4_pci_out(wc, WC_GPIO, wc->gpio);
602 }
603 }
604
605 static inline void __t4_gpio_setdir(struct t4 *wc, unsigned int bits, unsigned int val)
606 {
607 unsigned int newgpioctl;
608 newgpioctl = wc->gpioctl & (~bits);
609 newgpioctl |= val;
610 if (newgpioctl != wc->gpioctl) {
611 wc->gpioctl = newgpioctl;
612 __t4_pci_out(wc, WC_GPIOCTL, wc->gpioctl);
613 }
614 }
615
616 static inline void t4_gpio_setdir(struct t4 *wc, unsigned int bits, unsigned int val)
617 {
618 unsigned long flags;
619 spin_lock_irqsave(&wc->reglock, flags);
620 __t4_gpio_setdir(wc, bits, val);
621 spin_unlock_irqrestore(&wc->reglock, flags);
622 }
623
624 static inline void t4_gpio_set(struct t4 *wc, unsigned int bits, unsigned int val)
625 {
626 unsigned long flags;
627 spin_lock_irqsave(&wc->reglock, flags);
628 __t4_gpio_set(wc, bits, val);
629 spin_unlock_irqrestore(&wc->reglock, flags);
630 }
631
632 static inline void t4_pci_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
633 {
634 unsigned long flags;
635 spin_lock_irqsave(&wc->reglock, flags);
636 __t4_pci_out(wc, addr, value);
637 spin_unlock_irqrestore(&wc->reglock, flags);
638 }
639
640 static inline void __t4_set_led(struct t4 *wc, int span, int color)
641 {
642 if (span <= 3) {
643 int oldreg = wc->ledreg;
644
645 wc->ledreg &= ~(0x3 << (span << 1));
646 wc->ledreg |= (color << (span << 1));
647 if (oldreg != wc->ledreg)
648 __t4_pci_out(wc, WC_LEDS, wc->ledreg);
649 } else {
650 int oldreg = wc->ledreg2;
651
652 span &= 3;
653 wc->ledreg2 &= ~(0x3 << (span << 1));
654 wc->ledreg2 |= (color << (span << 1));
655 if (oldreg != wc->ledreg2)
656 __t4_pci_out(wc, WC_LEDS2, wc->ledreg2);
657 }
658 }
659
660 static inline void t4_activate(struct t4 *wc)
661 {
662 wc->ledreg |= WC_ACTIVATE;
663 t4_pci_out(wc, WC_LEDS, wc->ledreg);
664 }
665
666 static inline unsigned int t4_pci_in(struct t4 *wc, const unsigned int addr)
667 {
668 unsigned int ret;
669 unsigned long flags;
670
671 spin_lock_irqsave(&wc->reglock, flags);
672 ret = __t4_pci_in(wc, addr);
673 spin_unlock_irqrestore(&wc->reglock, flags);
674 return ret;
675 }
676
677 static unsigned int __t4_framer_in(const struct t4 *wc, int unit,
678 const unsigned int addr)
679 {
680 unsigned int ret;
681 register u32 val;
682 void __iomem *const wc_laddr = wc->membase + (WC_LADDR*sizeof(u32));
683 void __iomem *const wc_version = wc->membase + (WC_VERSION*sizeof(u32));
684 void __iomem *const wc_ldata = wc->membase + (WC_LDATA*sizeof(u32));
685 int haddr = (((unit & 4) ? 0 : WC_LFRMR_CS2));
686 unit &= 0x3;
687
688 val = ((unit & 0x3) << 8) | (addr & 0xff) | haddr;
689 writel(val, wc_laddr);
690 readl(wc_version);
691 writel(val | WC_LFRMR_CS | WC_LREAD, wc_laddr);
692 readl(wc_version);
693 ret = readb(wc_ldata);
694 writel(val, wc_laddr);
695 readl(wc_version);
696 return ret;
697 }
698
699 static unsigned int
700 t4_framer_in(struct t4 *wc, int unit, const unsigned int addr)
701 {
702 unsigned long flags;
703 unsigned int ret;
704 spin_lock_irqsave(&wc->reglock, flags);
705 ret = __t4_framer_in(wc, unit, addr);
706 spin_unlock_irqrestore(&wc->reglock, flags);
707 return ret;
708 }
709
710 static void __t4_framer_out(const struct t4 *wc, int unit, const u8 addr,
711 const unsigned int value)
712 {
713 register u32 val;
714 void __iomem *const wc_laddr = wc->membase + (WC_LADDR*sizeof(u32));
715 void __iomem *const wc_version = wc->membase + (WC_VERSION*sizeof(u32));
716 void __iomem *const wc_ldata = wc->membase + (WC_LDATA*sizeof(u32));
717 int haddr = (((unit & 4) ? 0 : WC_LFRMR_CS2));
718
719 val = ((unit & 0x3) << 8) | (addr & 0xff) | haddr;
720 writel(val, wc_laddr);
721 readl(wc_version);
722 writel(value, wc_ldata);
723 readl(wc_version);
724 writel(val | WC_LFRMR_CS | WC_LWRITE, wc_laddr);
725 readl(wc_version);
726 writel(val, wc_laddr);
727 readl(wc_version);
728 }
729
730 static void t4_framer_out(struct t4 *wc, int unit,
731 const unsigned int addr,
732 const unsigned int value)
733 {
734 unsigned long flags;
735 spin_lock_irqsave(&wc->reglock, flags);
736 __t4_framer_out(wc, unit, addr, value);
737 spin_unlock_irqrestore(&wc->reglock, flags);
738 }
739
740 #ifdef VPM_SUPPORT
741
742 static inline void __t4_raw_oct_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
743 {
744 int octopt = wc->tspans[0]->spanflags & FLAG_OCTOPT;
745 if (!octopt)
746 __t4_gpio_set(wc, 0xff, (addr >> 8));
747 __t4_pci_out(wc, WC_LDATA, 0x10000 | (addr & 0xffff));
748 if (!octopt)
749 __t4_pci_out(wc, WC_LADDR, (WC_LWRITE));
750 __t4_pci_out(wc, WC_LADDR, (WC_LWRITE | WC_LALE));
751 if (!octopt)
752 __t4_gpio_set(wc, 0xff, (value >> 8));
753 __t4_pci_out(wc, WC_LDATA, (value & 0xffff));
754 __t4_pci_out(wc, WC_LADDR, (WC_LWRITE | WC_LALE | WC_LCS));
755 __t4_pci_out(wc, WC_LADDR, (0));
756 }
757
758 static inline unsigned int __t4_raw_oct_in(struct t4 *wc, const unsigned int addr)
759 {
760 unsigned int ret;
761 int octopt = wc->tspans[0]->spanflags & FLAG_OCTOPT;
762 if (!octopt)
763 __t4_gpio_set(wc, 0xff, (addr >> 8));
764 __t4_pci_out(wc, WC_LDATA, 0x10000 | (addr & 0xffff));
765 if (!octopt)
766 __t4_pci_out(wc, WC_LADDR, (WC_LWRITE));
767 __t4_pci_out(wc, WC_LADDR, (WC_LWRITE | WC_LALE));
768 __t4_pci_out(wc, WC_LADDR, (WC_LALE));
769 if (!octopt) {
770 __t4_gpio_setdir(wc, 0xff, 0x00);
771 __t4_gpio_set(wc, 0xff, 0x00);
772 }
773 __t4_pci_out(wc, WC_LADDR, (WC_LREAD | WC_LALE | WC_LCS));
774 if (octopt) {
775 ret = __t4_pci_in(wc, WC_LDATA) & 0xffff;
776 } else {
777 ret = __t4_pci_in(wc, WC_LDATA) & 0xff;
778 ret |= (__t4_pci_in(wc, WC_GPIO) & 0xff) << 8;
779 }
780 __t4_pci_out(wc, WC_LADDR, (0));
781 if (!octopt)
782 __t4_gpio_setdir(wc, 0xff, 0xff);
783 return ret & 0xffff;
784 }
785
786 static inline unsigned int __t4_oct_in(struct t4 *wc, unsigned int addr)
787 {
788 #ifdef PEDANTIC_OCTASIC_CHECKING
789 int count = 1000;
790 #endif
791 __t4_raw_oct_out(wc, 0x0008, (addr >> 20));
792 __t4_raw_oct_out(wc, 0x000a, (addr >> 4) & ((1 << 16) - 1));
793 __t4_raw_oct_out(wc, 0x0000, (((addr >> 1) & 0x7) << 9) | (1 << 8) | (1));
794 #ifdef PEDANTIC_OCTASIC_CHECKING
795 while((__t4_raw_oct_in(wc, 0x0000) & (1 << 8)) && --count);
796 if (count != 1000)
797 dev_notice(&wc->dev->dev, "Yah, read can be slow...\n");
798 if (!count)
799 dev_notice(&wc->dev->dev, "Read timed out!\n");
800 #endif
801 return __t4_raw_oct_in(wc, 0x0004);
802 }
803
804 static inline unsigned int t4_oct_in(struct t4 *wc, const unsigned int addr)
805 {
806 unsigned long flags;
807 unsigned int ret;
808
809 spin_lock_irqsave(&wc->reglock, flags);
810 ret = __t4_oct_in(wc, addr);
811 spin_unlock_irqrestore(&wc->reglock, flags);
812 return ret;
813 }
814
815 static inline void __t4_oct_out(struct t4 *wc, unsigned int addr, unsigned int value)
816 {
817 #ifdef PEDANTIC_OCTASIC_CHECKING
818 int count = 1000;
819 #endif
820 __t4_raw_oct_out(wc, 0x0008, (addr >> 20));
821 __t4_raw_oct_out(wc, 0x000a, (addr >> 4) & ((1 << 16) - 1));
822 __t4_raw_oct_out(wc, 0x0004, value);
823 __t4_raw_oct_out(wc, 0x0000, (((addr >> 1) & 0x7) << 9) | (1 << 8) | (3 << 12) | 1);
824 #ifdef PEDANTIC_OCTASIC_CHECKING
825 while((__t4_raw_oct_in(wc, 0x0000) & (1 << 8)) && --count);
826 if (count != 1000)
827 dev_notice(&wc->dev->dev, "Yah, write can be slow\n");
828 if (!count)
829 dev_notice(&wc->dev->dev, "Write timed out!\n");
830 #endif
831 }
832
833 static inline void t4_oct_out(struct t4 *wc, const unsigned int addr, const unsigned int value)
834 {
835 unsigned long flags;
836
837 spin_lock_irqsave(&wc->reglock, flags);
838 __t4_oct_out(wc, addr, value);
839 spin_unlock_irqrestore(&wc->reglock, flags);
840 }
841
842 static void t4_check_vpm(struct t4 *wc)
843 {
844 int channel, tone, start, span;
845
846 if (vpm450m_checkirq(wc->vpm)) {
847 while(vpm450m_getdtmf(wc->vpm, &channel, &tone, &start)) {
848 span = channel & 0x3;
849 channel >>= 2;
850 if (!has_e1_span(wc))
851 channel -= 5;
852 else
853 channel -= 1;
854 if (unlikely(debug))
855 dev_info(&wc->dev->dev, "Got tone %s of '%c' "
856 "on channel %d of span %d\n",
857 (start ? "START" : "STOP"),
858 tone, channel, span + 1);
859 if (test_bit(channel, &wc->tspans[span]->dtmfmask) && (tone != 'u')) {
860 if (start) {
861 /* The octasic is supposed to mute us, but... Yah, you
862 guessed it. */
863 if (test_bit(channel, &wc->tspans[span]->dtmfmutemask)) {
864 unsigned long flags;
865 struct dahdi_chan *chan = wc->tspans[span]->span.chans[channel];
866 int y;
867 spin_lock_irqsave(&chan->lock, flags);
868 for (y=0;y<chan->numbufs;y++) {
869 if ((chan->inreadbuf > -1) && (chan->readidx[y]))
870 memset(chan->readbuf[chan->inreadbuf], DAHDI_XLAW(0, chan), chan->readidx[y]);
871 }
872 spin_unlock_irqrestore(&chan->lock, flags);
873 }
874 set_bit(channel, &wc->tspans[span]->dtmfactive);
875 dahdi_qevent_lock(wc->tspans[span]->span.chans[channel], (DAHDI_EVENT_DTMFDOWN | tone));
876 } else {
877 clear_bit(channel, &wc->tspans[span]->dtmfactive);
878 dahdi_qevent_lock(wc->tspans[span]->span.chans[channel], (DAHDI_EVENT_DTMFUP | tone));
879 }
880 }
881 }
882 }
883 }
884
885 #endif /* VPM_SUPPORT */
886
887 static void hdlc_stop(struct t4 *wc, unsigned int span)
888 {
889 struct t4_span *t = wc->tspans[span];
890 unsigned char imr0, imr1, mode;
891 unsigned long flags;
892 int i = 0;
893
894 if (debug & DEBUG_FRAMER)
895 dev_notice(&wc->dev->dev, "Stopping HDLC controller on span "
896 "%d\n", span+1);
897
898 /* Clear receive and transmit timeslots */
899 for (i = 0; i < 4; i++) {
900 t4_framer_out(wc, span, FRMR_RTR_BASE + i, 0x00);
901 t4_framer_out(wc, span, FRMR_TTR_BASE + i, 0x00);
902 }
903
904 spin_lock_irqsave(&wc->reglock, flags);
905 imr0 = __t4_framer_in(wc, span, FRMR_IMR0);
906 imr1 = __t4_framer_in(wc, span, FRMR_IMR1);
907
908 /* Disable HDLC interrupts */
909 imr0 |= HDLC_IMR0_MASK;
910 __t4_framer_out(wc, span, FRMR_IMR0, imr0);
911
912 imr1 |= HDLC_IMR1_MASK;
913 __t4_framer_out(wc, span, FRMR_IMR1, imr1);
914
915 mode = __t4_framer_in(wc, span, FRMR_MODE);
916 mode &= ~FRMR_MODE_HRAC;
917 __t4_framer_out(wc, span, FRMR_MODE, mode);
918 spin_unlock_irqrestore(&wc->reglock, flags);
919
920 t->sigactive = 0;
921 }
922
923 static inline void __t4_framer_cmd(struct t4 *wc, unsigned int span, int cmd)
924 {
925 __t4_framer_out(wc, span, FRMR_CMDR, cmd);
926 }
927
928 static inline void t4_framer_cmd_wait(struct t4 *wc, unsigned int span, int cmd)
929 {
930 int sis;
931 int loops = 0;
932
933 /* XXX could be time consuming XXX */
934 for (;;) {
935 sis = t4_framer_in(wc, span, FRMR_SIS);
936 if (!(sis & 0x04))
937 break;
938 if (!loops++ && (debug & DEBUG_FRAMER)) {
939 dev_notice(&wc->dev->dev, "!!!SIS Waiting before cmd "
940 "%02x\n", cmd);
941 }
942 }
943 if (loops && (debug & DEBUG_FRAMER))
944 dev_notice(&wc->dev->dev, "!!!SIS waited %d loops\n", loops);
945
946 t4_framer_out(wc, span, FRMR_CMDR, cmd);
947 }
948
949 static int hdlc_start(struct t4 *wc, unsigned int span, struct dahdi_chan *chan, unsigned char mode)
950 {
951 struct t4_span *t = wc->tspans[span];
952 unsigned char imr0, imr1;
953 int offset = chan->chanpos;
954 unsigned long flags;
955
956 if (debug & DEBUG_FRAMER)
957 dev_info(&wc->dev->dev, "Starting HDLC controller for channel "
958 "%d span %d\n", offset, span+1);
959
960 if (mode != FRMR_MODE_NO_ADDR_CMP)
961 return -1;
962
963 mode |= FRMR_MODE_HRAC;
964
965 spin_lock_irqsave(&wc->reglock, flags);
966
967 /* Make sure we're in the right mode */
968 __t4_framer_out(wc, span, FRMR_MODE, mode);
969 __t4_framer_out(wc, span, FRMR_TSEO, 0x00);
970 __t4_framer_out(wc, span, FRMR_TSBS1, hardhdlcmode);
971
972 /* Set the interframe gaps, etc */
973 __t4_framer_out(wc, span, FRMR_CCR1, FRMR_CCR1_ITF|FRMR_CCR1_EITS);
974
975 __t4_framer_out(wc, span, FRMR_CCR2, FRMR_CCR2_RCRC);
976
977 /* Set up the time slot that we want to tx/rx on */
978 __t4_framer_out(wc, span, FRMR_TTR_BASE + (offset / 8), (0x80 >> (offset % 8)));
979 __t4_framer_out(wc, span, FRMR_RTR_BASE + (offset / 8), (0x80 >> (offset % 8)));
980
981 imr0 = __t4_framer_in(wc, span, FRMR_IMR0);
982 imr1 = __t4_framer_in(wc, span, FRMR_IMR1);
983
984 /* Enable our interrupts again */
985 imr0 &= ~HDLC_IMR0_MASK;
986 __t4_framer_out(wc, span, FRMR_IMR0, imr0);
987
988 imr1 &= ~HDLC_IMR1_MASK;
989 __t4_framer_out(wc, span, FRMR_IMR1, imr1);
990
991 spin_unlock_irqrestore(&wc->reglock, flags);
992
993 /* Reset the signaling controller */
994 t4_framer_cmd_wait(wc, span, FRMR_CMDR_SRES);
995
996 spin_lock_irqsave(&wc->reglock, flags);
997 t->sigchan = chan;
998 spin_unlock_irqrestore(&wc->reglock, flags);
999
1000 t->sigactive = 0;
1001
1002 return 0;
1003 }
1004
1005 static void __set_clear(struct t4 *wc, int span)
1006 {
1007 int i,j;
1008 int oldnotclear;
1009 unsigned short val=0;
1010 struct t4_span *ts = wc->tspans[span];
1011
1012 oldnotclear = ts->notclear;
1013 if (E1 != ts->linemode) {
1014 for (i=0;i<24;i++) {
1015 j = (i/8);
1016 if (ts->span.chans[i]->flags & DAHDI_FLAG_CLEAR) {
1017 val |= 1 << (7 - (i % 8));
1018 ts->notclear &= ~(1 << i);
1019 } else
1020 ts->notclear |= (1 << i);
1021 if ((i % 8)==7) {
1022 if (debug)
1023 dev_notice(&wc->dev->dev, "Putting %d "
1024 "in register %02x on span %d"
1025 "\n", val, 0x2f + j, span + 1);
1026 __t4_framer_out(wc, span, 0x2f + j, val);
1027 val = 0;
1028 }
1029 }
1030 } else {
1031 for (i=0;i<31;i++) {
1032 if (ts->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
1033 ts->notclear &= ~(1 << i);
1034 else
1035 ts->notclear |= (1 << i);
1036 }
1037 }
1038 if (ts->notclear != oldnotclear) {
1039 unsigned char reg;
1040 reg = __t4_framer_in(wc, span, FRMR_IMR0);
1041 if (ts->notclear)
1042 reg &= ~0x08;
1043 else
1044 reg |= 0x08;
1045 __t4_framer_out(wc, span, FRMR_IMR0, reg);
1046 }
1047 }
1048
1049 static int t4_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
1050 {
1051 struct t4 *wc;
1052 struct t4_span *ts;
1053 wc = dst->pvt;
1054 ts = wc->tspans[dst->span->offset];
1055 if (src && (src->pvt != dst->pvt)) {
1056 if (ts->spanflags & FLAG_2NDGEN)
1057 t4_tsi_unassign(wc, dst->span->offset, dst->chanpos);
1058 wc = src->pvt;
1059 if (ts->spanflags & FLAG_2NDGEN)
1060 t4_tsi_unassign(wc, src->span->offset, src->chanpos);
1061 if (debug)
1062 dev_notice(&wc->dev->dev, "Unassigning %d/%d by "
1063 "default and...\n", src->span->offset,
1064 src->chanpos);
1065 if (debug)
1066 dev_notice(&wc->dev->dev, "Unassigning %d/%d by "
1067 "default\n", dst->span->offset, dst->chanpos);
1068 return -1;
1069 }
1070 if (src) {
1071 t4_tsi_assign(wc, src->span->offset, src->chanpos, dst->span->offset, dst->chanpos);
1072 if (debug)
1073 dev_notice(&wc->dev->dev, "Assigning channel %d/%d -> "
1074 "%d/%d!\n", src->span->offset, src->chanpos,
1075 dst->span->offset, dst->chanpos);
1076 } else {
1077 t4_tsi_unassign(wc, dst->span->offset, dst->chanpos);
1078 if (debug)
1079 dev_notice(&wc->dev->dev, "Unassigning channel %d/%d!"
1080 "\n", dst->span->offset, dst->chanpos);
1081 }
1082 return 0;
1083 }
1084
1085 #ifdef VPM_SUPPORT
1086
1087 void oct_set_reg(void *data, unsigned int reg, unsigned int val)
1088 {
1089 struct t4 *wc = data;
1090 t4_oct_out(wc, reg, val);
1091 }
1092
1093 unsigned int oct_get_reg(void *data, unsigned int reg)
1094 {
1095 struct t4 *wc = data;
1096 unsigned int ret;
1097 ret = t4_oct_in(wc, reg);
1098 return ret;
1099 }
1100
1101 static const char *__t4_echocan_name(struct t4 *wc)
1102 {
1103 if (wc->vpm) {
1104 if (wc->numspans == 2)
1105 return vpmoct064_name;
1106 else if (wc->numspans == 4)
1107 return vpmoct128_name;
1108 else if (wc->numspans == 8)
1109 return vpmoct256_name;
1110 }
1111 return NULL;
1112 }
1113
1114 static const char *t4_echocan_name(const struct dahdi_chan *chan)
1115 {
1116 struct t4 *wc = chan->pvt;
1117 return __t4_echocan_name(wc);
1118 }
1119
1120 static int t4_echocan_create(struct dahdi_chan *chan,
1121 struct dahdi_echocanparams *ecp,
1122 struct dahdi_echocanparam *p,
1123 struct dahdi_echocan_state **ec)
1124 {
1125 struct t4 *wc = chan->pvt;
1126 struct t4_span *tspan = container_of(chan->span, struct t4_span, span);
1127 int channel;
1128 const bool alaw = (chan->span->deflaw == 2);
1129
1130 if (!vpmsupport || !wc->vpm)
1131 return -ENODEV;
1132
1133 if (ecp->param_count > 0) {
1134 dev_warn(&wc->dev->dev, "%s echo canceller does not support "
1135 "parameters; failing request\n",
1136 chan->ec_factory->get_name(chan));
1137 return -EINVAL;
1138 }
1139
1140 *ec = tspan->ec[chan->chanpos - 1];
1141 (*ec)->ops = &vpm_ec_ops;
1142 (*ec)->features = vpm_ec_features;
1143
1144 channel = has_e1_span(wc) ? chan->chanpos : chan->chanpos + 4;
1145
1146 if (is_octal(wc))
1147 channel = channel << 3;
1148 else
1149 channel = channel << 2;
1150 channel |= chan->span->offset;
1151 if (debug & DEBUG_ECHOCAN) {
1152 dev_notice(&wc->dev->dev,
1153 "echocan: Card is %d, Channel is %d, Span is %d, offset is %d length %d\n",
1154 wc->num, chan->chanpos, chan->span->offset,
1155 channel, ecp->tap_length);
1156 }
1157 vpm450m_set_alaw_companding(wc->vpm, channel, alaw);
1158 vpm450m_setec(wc->vpm, channel, ecp->tap_length);
1159 return 0;
1160 }
1161
1162 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
1163 {
1164 struct t4 *wc = chan->pvt;
1165 int channel;
1166
1167 if (!wc->vpm)
1168 return;
1169
1170 memset(ec, 0, sizeof(*ec));
1171 channel = has_e1_span(wc) ? chan->chanpos : chan->chanpos + 4;
1172
1173 if (is_octal(wc))
1174 channel = channel << 3;
1175 else
1176 channel = channel << 2;
1177 channel |= chan->span->offset;
1178 if (debug & DEBUG_ECHOCAN) {
1179 dev_notice(&wc->dev->dev,
1180 "echocan: Card is %d, Channel is %d, Span is %d, offset is %d length 0\n",
1181 wc->num, chan->chanpos, chan->span->offset, channel);
1182 }
1183 vpm450m_setec(wc->vpm, channel, 0);
1184 }
1185 #endif
1186
1187 static int t4_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
1188 {
1189 struct t4_regs regs;
1190 struct t4_reg reg;
1191 int x;
1192 struct t4 *wc = chan->pvt;
1193 #ifdef VPM_SUPPORT
1194 int j;
1195 int channel;
1196 struct t4_span *ts = wc->tspans[chan->span->offset];
1197 #endif
1198
1199 switch(cmd) {
1200 case WCT4_SET_REG:
1201 if (copy_from_user(&reg, (struct t4_reg __user *)data,
1202 sizeof(reg)))
1203 return -EFAULT;
1204 t4_pci_out(wc, reg.reg, reg.val);
1205 break;
1206 case WCT4_GET_REG:
1207 if (copy_from_user(&reg, (struct t4_reg __user *)data,
1208 sizeof(reg)))
1209 return -EFAULT;
1210 reg.val = t4_pci_in(wc, reg.reg);
1211 if (copy_to_user((struct t4_reg __user *)data,
1212 &reg, sizeof(reg)))
1213 return -EFAULT;
1214 break;
1215 case WCT4_GET_REGS:
1216 for (x=0;x<NUM_PCI;x++)
1217 regs.pci[x] = t4_pci_in(wc, x);
1218 for (x=0;x<NUM_REGS;x++)
1219 regs.regs[x] = t4_framer_in(wc, chan->span->offset, x);
1220 if (copy_to_user((void __user *) data,
1221 &regs, sizeof(regs)))
1222 return -EFAULT;
1223 break;
1224 #ifdef VPM_SUPPORT
1225 case DAHDI_TONEDETECT:
1226 if (get_user(j, (__user int *) data))
1227 return -EFAULT;
1228 if (!wc->vpm)
1229 return -ENOSYS;
1230 if (j && (vpmdtmfsupport == 0))
1231 return -ENOSYS;
1232 if (j & DAHDI_TONEDETECT_ON)
1233 set_bit(chan->chanpos - 1, &ts->dtmfmask);
1234 else
1235 clear_bit(chan->chanpos - 1, &ts->dtmfmask);
1236 if (j & DAHDI_TONEDETECT_MUTE)
1237 set_bit(chan->chanpos - 1, &ts->dtmfmutemask);
1238 else
1239 clear_bit(chan->chanpos - 1, &ts->dtmfmutemask);
1240
1241 channel = has_e1_span(wc) ? chan->chanpos : chan->chanpos + 4;
1242 if (is_octal(wc))
1243 channel = channel << 3;
1244 else
1245 channel = channel << 2;
1246 channel |= chan->span->offset;
1247 vpm450m_setdtmf(wc->vpm, channel, j & DAHDI_TONEDETECT_ON,
1248 j & DAHDI_TONEDETECT_MUTE);
1249 return 0;
1250 #endif
1251 default:
1252 return -ENOTTY;
1253 }
1254 return 0;
1255 }
1256
1257 static void inline t4_hdlc_xmit_fifo(struct t4 *wc, unsigned int span, struct t4_span *ts)
1258 {
1259 int res, i;
1260 unsigned int size = 32;
1261 unsigned char buf[32];
1262
1263 res = dahdi_hdlc_getbuf(ts->sigchan, buf, &size);
1264 if (debug & DEBUG_FRAMER)
1265 dev_notice(&wc->dev->dev, "Got buffer sized %d and res %d "
1266 "for %d\n", size, res, span);
1267 if (size > 0) {
1268 ts->sigactive = 1;
1269
1270 if (debug & DEBUG_FRAMER) {
1271 dev_notice(&wc->dev->dev, "TX(");
1272 for (i = 0; i < size; i++)
1273 dev_notice(&wc->dev->dev, "%s%02x",
1274 (i ? " " : ""), buf[i]);
1275 dev_notice(&wc->dev->dev, ")\n");
1276 }
1277
1278 for (i = 0; i < size; i++)
1279 t4_framer_out(wc, span, FRMR_TXFIFO, buf[i]);
1280
1281 if (res) /* End of message */ {
1282 if (debug & DEBUG_FRAMER)
1283 dev_notice(&wc->dev->dev,
1284 "transmiting XHF|XME\n");
1285 t4_framer_cmd_wait(wc, span, FRMR_CMDR_XHF | FRMR_CMDR_XME);
1286 ++ts->frames_out;
1287 if ((debug & DEBUG_FRAMER) && !(ts->frames_out & 0x0f))
1288 dev_notice(&wc->dev->dev, "Transmitted %d "
1289 "frames on span %d\n", ts->frames_out,
1290 span);
1291 } else { /* Still more to transmit */
1292 if (debug & DEBUG_FRAMER)
1293 dev_notice(&wc->dev->dev, "transmiting XHF\n");
1294 t4_framer_cmd_wait(wc, span, FRMR_CMDR_XHF);
1295 }
1296 }
1297 else if (res < 0)
1298 ts->sigactive = 0;
1299 }
1300
1301 static void t4_hdlc_hard_xmit(struct dahdi_chan *chan)
1302 {
1303 struct t4 *wc = chan->pvt;
1304 int span = chan->span->offset;
1305 struct t4_span *ts = wc->tspans[span];
1306 unsigned long flags;
1307
1308 spin_lock_irqsave(&wc->reglock, flags);
1309 if (!ts->sigchan) {
1310 dev_notice(&wc->dev->dev, "t4_hdlc_hard_xmit: Invalid (NULL) "
1311 "signalling channel\n");
1312 spin_unlock_irqrestore(&wc->reglock, flags);
1313 return;
1314 }
1315 spin_unlock_irqrestore(&wc->reglock, flags);
1316
1317 if (debug & DEBUG_FRAMER)
1318 dev_notice(&wc->dev->dev, "t4_hdlc_hard_xmit on channel %s "
1319 "(sigchan %s), sigactive=%d\n", chan->name,
1320 ts->sigchan->name, ts->sigactive);
1321
1322 if ((ts->sigchan == chan) && !ts->sigactive)
1323 t4_hdlc_xmit_fifo(wc, span, ts);
1324 }
1325
1326 /**
1327 * t4_set_framer_bits - Atomically set bits in a framer register.
1328 */
1329 static void t4_set_framer_bits(struct t4 *wc, unsigned int spanno,
1330 unsigned int const addr, u16 bits)
1331 {
1332 unsigned long flags;
1333 unsigned int reg;
1334
1335 spin_lock_irqsave(&wc->reglock, flags);
1336 reg = __t4_framer_in(wc, spanno, addr);
1337 __t4_framer_out(wc, spanno, addr, (reg | bits));
1338 spin_unlock_irqrestore(&wc->reglock, flags);
1339 }
1340
1341 static int t4_maint(struct dahdi_span *span, int cmd)
1342 {
1343 struct t4_span *ts = container_of(span, struct t4_span, span);
1344 struct t4 *wc = ts->owner;
1345 unsigned int reg;
1346 unsigned long flags;
1347
1348 if (E1 == ts->linemode) {
1349 switch(cmd) {
1350 case DAHDI_MAINT_NONE:
1351 dev_info(&wc->dev->dev, "Clearing all maint modes\n");
1352 t4_clear_maint(span);
1353 break;
1354 case DAHDI_MAINT_LOCALLOOP:
1355 dev_info(&wc->dev->dev,
1356 "Turning on local loopback\n");
1357 t4_clear_maint(span);
1358 t4_set_framer_bits(wc, span->offset, LIM0_T, LIM0_LL);
1359 break;
1360 case DAHDI_MAINT_NETWORKLINELOOP:
1361 dev_info(&wc->dev->dev,
1362 "Turning on network line loopback\n");
1363 t4_clear_maint(span);
1364 t4_set_framer_bits(wc, span->offset, LIM1_T, LIM1_RL);
1365 break;
1366 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
1367 dev_info(&wc->dev->dev,
1368 "Turning on network payload loopback\n");
1369 t4_clear_maint(span);
1370 t4_set_framer_bits(wc, span->offset, FMR2_T, FMR2_PLB);
1371 break;
1372 case DAHDI_MAINT_LOOPUP:
1373 case DAHDI_MAINT_LOOPDOWN:
1374 dev_info(&wc->dev->dev,
1375 "Loopup & loopdown not supported in E1 mode\n");
1376 return -ENOSYS;
1377 case DAHDI_MAINT_FAS_DEFECT:
1378 t4_framer_out(wc, span->offset, IERR_T, IFASE);
1379 break;
1380 case DAHDI_MAINT_MULTI_DEFECT:
1381 t4_framer_out(wc, span->offset, IERR_T, IMFE);
1382 break;
1383 case DAHDI_MAINT_CRC_DEFECT:
1384 t4_framer_out(wc, span->offset, IERR_T, ICRCE);
1385 break;
1386 case DAHDI_MAINT_CAS_DEFECT:
1387 t4_framer_out(wc, span->offset, IERR_T, ICASE);
1388 break;
1389 case DAHDI_MAINT_PRBS_DEFECT:
1390 t4_framer_out(wc, span->offset, IERR_T, IPE);
1391 break;
1392 case DAHDI_MAINT_BIPOLAR_DEFECT:
1393 t4_framer_out(wc, span->offset, IERR_T, IBV);
1394 break;
1395 case DAHDI_RESET_COUNTERS:
1396 t4_reset_counters(span);
1397 break;
1398 case DAHDI_MAINT_ALARM_SIM:
1399 dev_info(&wc->dev->dev, "Invoking alarm state");
1400 t4_set_framer_bits(wc, span->offset, FMR0, FMR0_SIM);
1401 break;
1402 default:
1403 dev_info(&wc->dev->dev,
1404 "Unknown E1 maint command: %d\n", cmd);
1405 return -ENOSYS;
1406 }
1407 } else {
1408 switch(cmd) {
1409 case DAHDI_MAINT_NONE:
1410 dev_info(&wc->dev->dev, "Clearing all maint modes\n");
1411 t4_clear_maint(span);
1412 break;
1413 case DAHDI_MAINT_LOCALLOOP:
1414 dev_info(&wc->dev->dev,
1415 "Turning on local loopback\n");
1416 t4_clear_maint(span);
1417 t4_set_framer_bits(wc, span->offset, LIM0_T, LIM0_LL);
1418 break;
1419 case DAHDI_MAINT_NETWORKLINELOOP:
1420 dev_info(&wc->dev->dev,
1421 "Turning on network line loopback\n");
1422 t4_clear_maint(span);
1423 t4_set_framer_bits(wc, span->offset, LIM1_T, LIM1_RL);
1424 break;
1425 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
1426 dev_info(&wc->dev->dev,
1427 "Turning on network payload loopback\n");
1428 t4_clear_maint(span);
1429 t4_set_framer_bits(wc, span->offset, FMR2_T, FMR2_PLB);
1430 break;
1431 case DAHDI_MAINT_LOOPUP:
1432 dev_info(&wc->dev->dev, "Transmitting loopup code\n");
1433 t4_clear_maint(span);
1434 t4_set_framer_bits(wc, span->offset, FMR5, FMR5_XLU);
1435 ts->span.maintstat = DAHDI_MAINT_REMOTELOOP;
1436 break;
1437 case DAHDI_MAINT_LOOPDOWN:
1438 dev_info(&wc->dev->dev, "Transmitting loopdown code\n");
1439 t4_clear_maint(span);
1440 t4_set_framer_bits(wc, span->offset, FMR5, FMR5_XLD);
1441 break;
1442 case DAHDI_MAINT_FAS_DEFECT:
1443 t4_framer_out(wc, span->offset, IERR_T, IFASE);
1444 break;
1445 case DAHDI_MAINT_MULTI_DEFECT:
1446 t4_framer_out(wc, span->offset, IERR_T, IMFE);
1447 break;
1448 case DAHDI_MAINT_CRC_DEFECT:
1449 t4_framer_out(wc, span->offset, IERR_T, ICRCE);
1450 break;
1451 case DAHDI_MAINT_CAS_DEFECT:
1452 t4_framer_out(wc, span->offset, IERR_T, ICASE);
1453 break;
1454 case DAHDI_MAINT_PRBS_DEFECT:
1455 t4_framer_out(wc, span->offset, IERR_T, IPE);
1456 break;
1457 case DAHDI_MAINT_BIPOLAR_DEFECT:
1458 t4_framer_out(wc, span->offset, IERR_T, IBV);
1459 break;
1460 case DAHDI_MAINT_PRBS:
1461 dev_info(&wc->dev->dev, "PRBS not supported\n");
1462 return -ENOSYS;
1463 case DAHDI_RESET_COUNTERS:
1464 t4_reset_counters(span);
1465 break;
1466 case DAHDI_MAINT_ALARM_SIM:
1467 spin_lock_irqsave(&wc->reglock, flags);
1468 reg = __t4_framer_in(wc, span->offset, FMR0);
1469
1470 /*
1471 * The alarm simulation state machine requires us to
1472 * bring this bit up and down for at least 1 clock cycle
1473 */
1474 __t4_framer_out(wc, span->offset,
1475 FMR0, (reg | FMR0_SIM));
1476 udelay(1);
1477 __t4_framer_out(wc, span->offset,
1478 FMR0, (reg & ~FMR0_SIM));
1479 udelay(1);
1480 spin_unlock_irqrestore(&wc->reglock, flags);
1481
1482 reg = t4_framer_in(wc, span->offset, 0x4e);
1483 if (debug & DEBUG_MAIN) {
1484 dev_info(&wc->dev->dev,
1485 "FRS2(alarm state): %d\n",
1486 ((reg & 0xe0) >> 5));
1487 }
1488 break;
1489 default:
1490 dev_info(&wc->dev->dev, "Unknown T1 maint command:%d\n",
1491 cmd);
1492 break;
1493 }
1494 }
1495 return 0;
1496 }
1497
1498 static int t4_clear_maint(struct dahdi_span *span)
1499 {
1500 struct t4_span *ts = container_of(span, struct t4_span, span);
1501 struct t4 *wc = ts->owner;
1502 unsigned int reg;
1503 unsigned long flags;
1504
1505 spin_lock_irqsave(&wc->reglock, flags);
1506
1507 /* Clear local loop */
1508 reg = __t4_framer_in(wc, span->offset, LIM0_T);
1509 __t4_framer_out(wc, span->offset, LIM0_T, (reg & ~LIM0_LL));
1510
1511 /* Clear Remote Loop */
1512 reg = __t4_framer_in(wc, span->offset, LIM1_T);
1513 __t4_framer_out(wc, span->offset, LIM1_T, (reg & ~LIM1_RL));
1514
1515 /* Clear Remote Payload Loop */
1516 reg = __t4_framer_in(wc, span->offset, FMR2_T);
1517 __t4_framer_out(wc, span->offset, FMR2_T, (reg & ~FMR2_PLB));
1518
1519 /* Clear PRBS */
1520 reg = __t4_framer_in(wc, span->offset, LCR1_T);
1521 __t4_framer_out(wc, span->offset, LCR1_T, (reg & ~(XPRBS | EPRM)));
1522
1523 /* Clear loopup/loopdown signals on the line */
1524 reg = __t4_framer_in(wc, span->offset, FMR5);
1525 __t4_framer_out(wc, span->offset, FMR5, (reg & ~(FMR5_XLU | FMR5_XLD)));
1526
1527 spin_unlock_irqrestore(&wc->reglock, flags);
1528 span->mainttimer = 0;
1529
1530 return 0;
1531 }
1532
1533 static int t4_reset_counters(struct dahdi_span *span)
1534 {
1535 struct t4_span *ts = container_of(span, struct t4_span, span);
1536 memset(&ts->span.count, 0, sizeof(ts->span.count));
1537 return 0;
1538 }
1539
1540 static int t4_rbsbits(struct dahdi_chan *chan, int bits)
1541 {
1542 u_char m,c;
1543 int k,n,b;
1544 struct t4 *wc = chan->pvt;
1545 struct t4_span *ts = wc->tspans[chan->span->offset];
1546 unsigned long flags;
1547
1548 if (debug & DEBUG_RBS)
1549 dev_notice(&wc->dev->dev, "Setting bits to %d on channel %s\n",
1550 bits, chan->name);
1551 spin_lock_irqsave(&wc->reglock, flags);
1552 k = chan->span->offset;
1553 if (E1 == ts->linemode) {
1554 if (chan->chanpos == 16) {
1555 spin_unlock_irqrestore(&wc->reglock, flags);
1556 return 0;
1557 }
1558 n = chan->chanpos - 1;
1559 if (chan->chanpos > 15) n--;
1560 b = (n % 15);
1561 c = ts->txsigs[b];
1562 m = (n / 15) << 2; /* nibble selector */
1563 c &= (0xf << m); /* keep the other nibble */
1564 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1565 ts->txsigs[b] = c;
1566 /* output them to the chip */
1567 __t4_framer_out(wc,k,0x71 + b,c);
1568 } else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
1569 n = chan->chanpos - 1;
1570 b = (n/4);
1571 c = ts->txsigs[b];
1572 m = ((3 - (n % 4)) << 1); /* nibble selector */
1573 c &= ~(0x3 << m); /* keep the other nibble */
1574 c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
1575 ts->txsigs[b] = c;
1576 /* output them to the chip */
1577 __t4_framer_out(wc,k,0x70 + b,c);
1578 __t4_framer_out(wc,k,0x70 + b + 6,c);
1579 } else if (ts->span.lineconfig & DAHDI_CONFIG_ESF) {
1580 n = chan->chanpos - 1;
1581 b = (n/2);
1582 c = ts->txsigs[b];
1583 m = ((n % 2) << 2); /* nibble selector */
1584 c &= (0xf << m); /* keep the other nibble */
1585 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1586 ts->txsigs[b] = c;
1587 /* output them to the chip */
1588 __t4_framer_out(wc,k,0x70 + b,c);
1589 }
1590 spin_unlock_irqrestore(&wc->reglock, flags);
1591 if (debug & DEBUG_RBS)
1592 dev_notice(&wc->dev->dev, "Finished setting RBS bits\n");
1593 return 0;
1594 }
1595
1596 static int t4_shutdown(struct dahdi_span *span)
1597 {
1598 int tspan;
1599 int wasrunning;
1600 unsigned long flags;
1601 struct t4_span *ts = container_of(span, struct t4_span, span);
1602 struct t4 *wc = ts->owner;
1603
1604 tspan = span->offset + 1;
1605 if (tspan < 0) {
1606 dev_notice(&wc->dev->dev, "T%dXXP: Span '%d' isn't us?\n",
1607 wc->numspans, span->spanno);
1608 return -1;
1609 }
1610
1611 if (debug & DEBUG_MAIN)
1612 dev_notice(&wc->dev->dev, "Shutting down span %d (%s)\n",
1613 span->spanno, span->name);
1614
1615 /* Stop HDLC controller if runned */
1616 if (ts->sigchan)
1617 hdlc_stop(wc, span->offset);
1618
1619 spin_lock_irqsave(&wc->reglock, flags);
1620 wasrunning = span->flags & DAHDI_FLAG_RUNNING;
1621
1622 span->flags &= ~DAHDI_FLAG_RUNNING;
1623 __t4_set_led(wc, span->offset, WC_OFF);
1624 if (((wc->numspans == 8) &&
1625 (!(wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)) &&
1626 (!(wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)) &&
1627 (!(wc->tspans[2]->span.flags & DAHDI_FLAG_RUNNING)) &&
1628 (!(wc->tspans[3]->span.flags & DAHDI_FLAG_RUNNING)) &&
1629 (!(wc->tspans[4]->span.flags & DAHDI_FLAG_RUNNING)) &&
1630 (!(wc->tspans[5]->span.flags & DAHDI_FLAG_RUNNING)) &&
1631 (!(wc->tspans[6]->span.flags & DAHDI_FLAG_RUNNING)) &&
1632 (!(wc->tspans[7]->span.flags & DAHDI_FLAG_RUNNING)))
1633 ||
1634 ((wc->numspans == 4) &&
1635 (!(wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)) &&
1636 (!(wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)) &&
1637 (!(wc->tspans[2]->span.flags & DAHDI_FLAG_RUNNING)) &&
1638 (!(wc->tspans[3]->span.flags & DAHDI_FLAG_RUNNING)))
1639 ||
1640 ((wc->numspans == 2) &&
1641 (!(wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)) &&
1642 (!(wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)))) {
1643 /* No longer in use, disable interrupts */
1644 dev_info(&wc->dev->dev, "TE%dXXP: Disabling interrupts since "
1645 "there are no active spans\n", wc->numspans);
1646 set_bit(T4_STOP_DMA, &wc->checkflag);
1647 } else
1648 set_bit(T4_CHECK_TIMING, &wc->checkflag);
1649
1650 spin_unlock_irqrestore(&wc->reglock, flags);
1651
1652 /* Wait for interrupt routine to shut itself down */
1653 msleep(10);
1654 if (wasrunning)
1655 wc->spansstarted--;
1656
1657 if (debug & DEBUG_MAIN)
1658 dev_notice(&wc->dev->dev, "Span %d (%s) shutdown\n",
1659 span->spanno, span->name);
1660 return 0;
1661 }
1662
1663 static void t4_chan_set_sigcap(struct dahdi_span *span, int x)
1664 {
1665 struct t4_span *wc = container_of(span, struct t4_span, span);
1666 struct dahdi_chan *chan = wc->chans[x];
1667 chan->sigcap = DAHDI_SIG_CLEAR;
1668 /* E&M variant supported depends on span type */
1669 if (E1 == wc->linemode) {
1670 /* E1 sigcap setup */
1671 if (span->lineconfig & DAHDI_CONFIG_CCS) {
1672 /* CCS setup */
1673 chan->sigcap |= DAHDI_SIG_MTP2 | DAHDI_SIG_SF |
1674 DAHDI_SIG_HARDHDLC;
1675 return;
1676 }
1677 /* clear out sig and sigcap for channel 16 on E1 CAS
1678 * lines, otherwise, set it correctly */
1679 if (x == 15) {
1680 /* CAS signaling channel setup */
1681 wc->chans[15]->sigcap = 0;
1682 wc->chans[15]->sig = 0;
1683 return;
1684 }
1685 /* normal CAS setup */
1686 chan->sigcap |= DAHDI_SIG_EM_E1 | DAHDI_SIG_FXSLS |
1687 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_SF |
1688 DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS |
1689 DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
1690 } else {
1691 /* T1 sigcap setup */
1692 chan->sigcap |= DAHDI_SIG_EM | DAHDI_SIG_FXSLS |
1693 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_MTP2 |
1694 DAHDI_SIG_SF | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
1695 DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS |
1696 DAHDI_SIG_HARDHDLC;
1697 }
1698 }
1699
1700 static int
1701 _t4_spanconfig(struct file *file, struct dahdi_span *span,
1702 struct dahdi_lineconfig *lc)
1703 {
1704 int i;
1705 struct t4_span *ts = container_of(span, struct t4_span, span);
1706 struct t4 *wc = ts->owner;
1707
1708 if (debug)
1709 dev_info(&wc->dev->dev, "About to enter spanconfig!\n");
1710 if (debug & DEBUG_MAIN)
1711 dev_notice(&wc->dev->dev, "TE%dXXP: Configuring span %d\n",
1712 wc->numspans, span->spanno);
1713
1714 if (lc->sync < 0)
1715 lc->sync = 0;
1716 if (lc->sync > wc->numspans) {
1717 dev_warn(&wc->dev->dev, "WARNING: Cannot set priority on span %d to %d. Please set to a number between 1 and %d\n",
1718 span->spanno, lc->sync, wc->numspans);
1719 lc->sync = 0;
1720 }
1721
1722 /* remove this span number from the current sync sources, if there */
1723 for(i = 0; i < wc->numspans; i++) {
1724 if (wc->tspans[i]->sync == span->spanno)
1725 wc->tspans[i]->sync = 0;
1726 }
1727 wc->tspans[span->offset]->syncpos = lc->sync;
1728 /* if a sync src, put it in proper place */
1729 if (lc->sync)
1730 wc->tspans[lc->sync - 1]->sync = span->spanno;
1731
1732 set_bit(T4_CHECK_TIMING, &wc->checkflag);
1733
1734 /* Make sure this is clear in case of multiple startup and shutdown
1735 * iterations */
1736 clear_bit(T4_STOP_DMA, &wc->checkflag);
1737
1738 /* make sure that sigcaps gets updated if necessary */
1739 for (i = 0; i < span->channels; i++)
1740 t4_chan_set_sigcap(span, i);
1741
1742 /* If we're already running, then go ahead and apply the changes */
1743 if (span->flags & DAHDI_FLAG_RUNNING)
1744 return _t4_startup(file, span);
1745
1746 if (debug)
1747 dev_info(&wc->dev->dev, "Done with spanconfig!\n");
1748 return 0;
1749 }
1750
1751 static int
1752 t4_spanconfig(struct file *file, struct dahdi_span *span,
1753 struct dahdi_lineconfig *lc)
1754 {
1755 int ret;
1756 struct dahdi_device *const ddev = span->parent;
1757 struct dahdi_span *s;
1758
1759 ret = _t4_spanconfig(file, span, lc);
1760
1761 /* Make sure all the spans have a basic configuration in case they are
1762 * not all specified in the configuration files. */
1763 lc->sync = 0;
1764 list_for_each_entry(s, &ddev->spans, device_node) {
1765 WARN_ON(!s->channels);
1766 if (!s->channels)
1767 continue;
1768 if (!s->chans[0]->sigcap)
1769 _t4_spanconfig(file, s, lc);
1770 }
1771 return ret;
1772 }
1773
1774 static int
1775 t4_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
1776 {
1777 int alreadyrunning;
1778 unsigned long flags;
1779 struct t4 *wc = chan->pvt;
1780 struct t4_span *ts = wc->tspans[chan->span->offset];
1781
1782 alreadyrunning = ts->span.flags & DAHDI_FLAG_RUNNING;
1783 if (debug & DEBUG_MAIN) {
1784 if (alreadyrunning)
1785 dev_notice(&wc->dev->dev, "TE%dXXP: Reconfigured "
1786 "channel %d (%s) sigtype %d\n", wc->numspans,
1787 chan->channo, chan->name, sigtype);
1788 else
1789 dev_notice(&wc->dev->dev, "TE%dXXP: Configured channel"
1790 " %d (%s) sigtype %d\n", wc->numspans,
1791 chan->channo, chan->name, sigtype);
1792 }
1793
1794 spin_lock_irqsave(&wc->reglock, flags);
1795
1796 if (alreadyrunning)
1797 __set_clear(wc, chan->span->offset);
1798
1799 spin_unlock_irqrestore(&wc->reglock, flags);
1800
1801 /* (re)configure signalling channel */
1802 if ((sigtype == DAHDI_SIG_HARDHDLC) || (ts->sigchan == chan)) {
1803 if (debug & DEBUG_FRAMER)
1804 dev_notice(&wc->dev->dev, "%sonfiguring hardware HDLC "
1805 "on %s\n",
1806 ((sigtype == DAHDI_SIG_HARDHDLC) ? "C" : "Unc"),
1807 chan->name);
1808 if (alreadyrunning) {
1809 if (ts->sigchan)
1810 hdlc_stop(wc, ts->sigchan->span->offset);
1811 if (sigtype == DAHDI_SIG_HARDHDLC) {
1812 if (hdlc_start(wc, chan->span->offset, chan, ts->sigmode)) {
1813 dev_notice(&wc->dev->dev, "Error "
1814 "initializing signalling "
1815 "controller\n");
1816 return -1;
1817 }
1818 } else {
1819 spin_lock_irqsave(&wc->reglock, flags);
1820 ts->sigchan = NULL;
1821 spin_unlock_irqrestore(&wc->reglock, flags);
1822 }
1823
1824 }
1825 else {
1826 spin_lock_irqsave(&wc->reglock, flags);
1827 ts->sigchan = (sigtype == DAHDI_SIG_HARDHDLC) ? chan : NULL;
1828 spin_unlock_irqrestore(&wc->reglock, flags);
1829 ts->sigactive = 0;
1830 }
1831 }
1832 return 0;
1833 }
1834
1835 static int set_span_devicetype(struct t4 *wc)
1836 {
1837 #ifdef VPM_SUPPORT
1838 const char *vpmstring = __t4_echocan_name(wc);
1839
1840 if (vpmstring) {
1841 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (%s)",
1842 wc->devtype->desc, vpmstring);
1843 } else {
1844 wc->ddev->devicetype = kasprintf(GFP_KERNEL, wc->devtype->desc);
1845 }
1846 #else
1847 wc->ddev->devicetype = kasprintf(GFP_KERNEL, wc->devtype->desc);
1848 #endif
1849
1850 if (!wc->ddev->devicetype)
1851 return -ENOMEM;
1852 return 0;
1853 }
1854
1855 /* The number of cards we have seen with each
1856 possible 'order' switch setting.
1857 */
1858 static unsigned int order_index[16];
1859
1860 static void setup_chunks(struct t4 *wc, int which)
1861 {
1862 struct t4_span *ts;
1863 int offset = 1;
1864 int x, y;
1865 int gen2;
1866 int basesize = T4_BASE_SIZE(wc) >> 2;
1867
1868 if (!has_e1_span(wc))
1869 offset += 4;
1870
1871 gen2 = (wc->tspans[0]->spanflags & FLAG_2NDGEN);
1872
1873 for (x = 0; x < wc->numspans; x++) {
1874 ts = wc->tspans[x];
1875 ts->writechunk = (void *)(wc->writechunk + (x * 32 * 2) + (which * (basesize)));
1876 ts->readchunk = (void *)(wc->readchunk + (x * 32 * 2) + (which * (basesize)));
1877 for (y=0;y<wc->tspans[x]->span.channels;y++) {
1878 struct dahdi_chan *mychans = ts->chans[y];
1879 if (gen2) {
1880 mychans->writechunk = (void *)(wc->writechunk + ((x * 32 + y + offset) * 2) + (which * (basesize)));
1881 mychans->readchunk = (void *)(wc->readchunk + ((x * 32 + y + offset) * 2) + (which * (basesize)));
1882 }
1883 }
1884 }
1885 }
1886
1887 static int __t4_hardware_init_1(struct t4 *wc, unsigned int cardflags,
1888 bool first_time);
1889 static int __t4_hardware_init_2(struct t4 *wc, bool first_time);
1890
1891 static int t4_hardware_stop(struct t4 *wc);
1892
1893 static void t4_framer_reset(struct t4 *wc)
1894 {
1895 const bool first_time = false;
1896 bool have_vpm = wc->vpm != NULL;
1897 if (have_vpm) {
1898 release_vpm450m(wc->vpm);
1899 wc->vpm = NULL;
1900 }
1901 t4_hardware_stop(wc);
1902 __t4_set_sclk_src(wc, WC_SELF, 0, 0);
1903 __t4_hardware_init_1(wc, wc->devtype->flags, first_time);
1904 __t4_hardware_init_2(wc, first_time);
1905 if (have_vpm) {
1906 t4_vpm_init(wc);
1907 wc->dmactrl |= (wc->vpm) ? T4_VPM_PRESENT : 0;
1908 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
1909 }
1910 setup_chunks(wc, 0);
1911 wc->lastindex = 0;
1912 }
1913
1914 /**
1915 * t4_serial_setup - Setup serial parameters and system interface.
1916 * @wc: The card to configure.
1917 *
1918 */
1919 static void t4_serial_setup(struct t4 *wc)
1920 {
1921 unsigned long flags;
1922 unsigned int unit;
1923 bool reset_required = false;
1924
1925 if (debug) {
1926 dev_info(&wc->dev->dev,
1927 "TE%dXXP: Setting up global serial parameters\n",
1928 wc->numspans);
1929 }
1930
1931 spin_lock_irqsave(&wc->reglock, flags);
1932 reset_required = wc->reset_required > 0;
1933 wc->reset_required = 0;
1934 spin_unlock_irqrestore(&wc->reglock, flags);
1935
1936 if (reset_required)
1937 t4_framer_reset(wc);
1938
1939 spin_lock_irqsave(&wc->reglock, flags);
1940 /* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from
1941 * channel 0 */
1942 __t4_framer_out(wc, 0, 0x85, 0xe0);
1943 if (is_octal(wc))
1944 __t4_framer_out(wc, 0, FRMR_GPC2, 0x00);
1945
1946 /* IPC: Interrupt push/pull active low */
1947 __t4_framer_out(wc, 0, 0x08, 0x01);
1948
1949 if (is_octal(wc)) {
1950 /* Global clocks (16.384 Mhz CLK) */
1951 __t4_framer_out(wc, 0, 0x92, 0x00); /* GCM1 */
1952 __t4_framer_out(wc, 0, 0x93, 0x18);
1953 __t4_framer_out(wc, 0, 0x94, 0xfb);
1954 __t4_framer_out(wc, 0, 0x95, 0x0b);
1955 __t4_framer_out(wc, 0, 0x96, 0x01);
1956 __t4_framer_out(wc, 0, 0x97, 0x0b);
1957 __t4_framer_out(wc, 0, 0x98, 0xdb);
1958 __t4_framer_out(wc, 0, 0x99, 0xdf);
1959 } else {
1960 /* Global clocks (8.192 Mhz CLK) */
1961 __t4_framer_out(wc, 0, 0x92, 0x00);
1962 __t4_framer_out(wc, 0, 0x93, 0x18);
1963 __t4_framer_out(wc, 0, 0x94, 0xfb);
1964 __t4_framer_out(wc, 0, 0x95, 0x0b);
1965 __t4_framer_out(wc, 0, 0x96, 0x00);
1966 __t4_framer_out(wc, 0, 0x97, 0x0b);
1967 __t4_framer_out(wc, 0, 0x98, 0xdb);
1968 __t4_framer_out(wc, 0, 0x99, 0xdf);
1969 }
1970 spin_unlock_irqrestore(&wc->reglock, flags);
1971
1972 for (unit = 0; unit < ports_on_framer(wc); ++unit) {
1973 spin_lock_irqsave(&wc->reglock, flags);
1974
1975 /* Configure interrupts */
1976 /* GCR: Interrupt on Activation/Deactivation of each */
1977 __t4_framer_out(wc, unit, FRMR_GCR, 0x00);
1978
1979 /* Configure system interface */
1980 if (is_octal(wc)) {
1981 /* SIC1: 16.384 Mhz clock/bus, double buffer receive /
1982 * transmit, byte interleaved */
1983 __t4_framer_out(wc, unit, FRMR_SIC1, 0xc2 | 0x08);
1984 } else {
1985 /* SIC1: 8.192 Mhz clock/bus, double buffer receive /
1986 * transmit, byte interleaved */
1987 __t4_framer_out(wc, unit, FRMR_SIC1, 0xc2);
1988 }
1989 /* SIC2: No FFS, no center receive eliastic buffer, phase */
1990 __t4_framer_out(wc, unit, FRMR_SIC2, 0x20 | (unit << 1));
1991 /* SIC3: Edges for capture */
1992 if (is_octal(wc)) {
1993 __t4_framer_out(wc, unit, FRMR_SIC3, 0x04 | (1 << 4));
1994 } else {
1995 __t4_framer_out(wc, unit, FRMR_SIC3, 0x04);
1996 }
1997 /* CMR2: We provide sync and clock for tx and rx. */
1998 __t4_framer_out(wc, unit, FRMR_CMR2, 0x00);
1999
2000 if (is_octal(wc)) {
2001 /* Set RCLK to 16 MHz */
2002 __t4_framer_out(wc, unit, FRMR_CMR4, 0x5);
2003
2004 if (!has_e1_span(wc)) { /* T1/J1 mode */
2005 __t4_framer_out(wc, unit, FRMR_XC0, 0x07);
2006 __t4_framer_out(wc, unit, FRMR_XC1, 0x04);
2007 if (wc->tspans[unit]->linemode == J1)
2008 __t4_framer_out(wc, unit, FRMR_RC0, 0x87);
2009 else
2010 __t4_framer_out(wc, unit, FRMR_RC0, 0x07);
2011 __t4_framer_out(wc, unit, FRMR_RC1, 0x04);
2012 } else { /* E1 mode */
2013 __t4_framer_out(wc, unit, FRMR_XC0, 0x00);
2014 __t4_framer_out(wc, unit, FRMR_XC1, 0x04);
2015 __t4_framer_out(wc, unit, FRMR_RC0, 0x00);
2016 __t4_framer_out(wc, unit, FRMR_RC1, 0x04);
2017 }
2018
2019 } else {
2020 if (!has_e1_span(wc)) { /* T1/J1 mode */
2021 __t4_framer_out(wc, unit, FRMR_XC0, 0x03);
2022 __t4_framer_out(wc, unit, FRMR_XC1, 0x84);
2023 if (J1 == wc->tspans[unit]->linemode)
2024 __t4_framer_out(wc, unit, FRMR_RC0, 0x83);
2025 else
2026 __t4_framer_out(wc, unit, FRMR_RC0, 0x03);
2027 __t4_framer_out(wc, unit, FRMR_RC1, 0x84);
2028 } else { /* E1 mode */
2029 __t4_framer_out(wc, unit, FRMR_XC0, 0x00);
2030 __t4_framer_out(wc, unit, FRMR_XC1, 0x04);
2031 __t4_framer_out(wc, unit, FRMR_RC0, 0x04);
2032 __t4_framer_out(wc, unit, FRMR_RC1, 0x04);
2033 }
2034 }
2035
2036 /* Configure ports */
2037
2038 /* PC1: SPYR/SPYX input on RPA/XPA */
2039 __t4_framer_out(wc, unit, 0x80, 0x00);
2040
2041 /* PC2: RMFB/XSIG output/input on RPB/XPB */
2042 /* PC3: Some unused stuff */
2043 /* PC4: Some more unused stuff */
2044 if (is_octal(wc)) {
2045 __t4_framer_out(wc, unit, 0x81, 0xBB);
2046 __t4_framer_out(wc, unit, 0x82, 0xf5);
2047 __t4_framer_out(wc, unit, 0x83, 0x35);
2048 } else if (wc->falc31) {
2049 __t4_framer_out(wc, unit, 0x81, 0xBB);
2050 __t4_framer_out(wc, unit, 0x82, 0xBB);
2051 __t4_framer_out(wc, unit, 0x83, 0xBB);
2052 } else {
2053 __t4_framer_out(wc, unit, 0x81, 0x22);
2054 __t4_framer_out(wc, unit, 0x82, 0x65);
2055 __t4_framer_out(wc, unit, 0x83, 0x35);
2056 }
2057
2058 /* PC5: XMFS active low, SCLKR is input, RCLK is output */
2059 __t4_framer_out(wc, unit, 0x84, 0x01);
2060
2061 if (debug & DEBUG_MAIN) {
2062 dev_notice(&wc->dev->dev,
2063 "Successfully initialized serial bus "
2064 "for unit %d\n", unit);
2065 }
2066
2067 spin_unlock_irqrestore(&wc->reglock, flags);
2068 }
2069 }
2070
2071 /**
2072 * t4_span_assigned - Called when the span is assigned by DAHDI.
2073 * @span: Span that has been assigned.
2074 *
2075 * When this function is called, the span has a valid spanno and all the
2076 * channels on the span have valid channel numbers assigned.
2077 *
2078 * This function is necessary because a device may be registered, and
2079 * then user space may then later decide to assign span numbers and the
2080 * channel numbers.
2081 *
2082 */
2083 static void t4_span_assigned(struct dahdi_span *span)
2084 {
2085 struct t4_span *tspan = container_of(span, struct t4_span, span);
2086 struct t4 *wc = tspan->owner;
2087 struct dahdi_span *pos;
2088 unsigned int unassigned_spans = 0;
2089 unsigned long flags;
2090
2091 /* We use this to make sure all the spans are assigned before
2092 * running the serial setup. */
2093 list_for_each_entry(pos, &wc->ddev->spans, device_node) {
2094 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &pos->flags))
2095 ++unassigned_spans;
2096 }
2097
2098 if (0 == unassigned_spans) {
2099 t4_serial_setup(wc);
2100
2101 set_bit(T4_CHECK_TIMING, &wc->checkflag);
2102 spin_lock_irqsave(&wc->reglock, flags);
2103 __t4_set_sclk_src(wc, WC_SELF, 0, 0);
2104 spin_unlock_irqrestore(&wc->reglock, flags);
2105 }
2106 }
2107
2108 static void free_wc(struct t4 *wc)
2109 {
2110 unsigned int x, y;
2111
2112 flush_scheduled_work();
2113
2114 for (x = 0; x < ARRAY_SIZE(wc->tspans); x++) {
2115 if (!wc->tspans[x])
2116 continue;
2117 for (y = 0; y < ARRAY_SIZE(wc->tspans[x]->chans); y++) {
2118 kfree(wc->tspans[x]->chans[y]);
2119 kfree(wc->tspans[x]->ec[y]);
2120 }
2121 kfree(wc->tspans[x]);
2122 }
2123
2124 kfree(wc->ddev->devicetype);
2125 kfree(wc->ddev->location);
2126 kfree(wc->ddev->hardware_id);
2127 dahdi_free_device(wc->ddev);
2128 kfree(wc);
2129 }
2130
2131 /**
2132 * t4_alloc_channels - Allocate the channels on a span.
2133 * @wc: The board we're allocating for.
2134 * @ts: The span we're allocating for.
2135 * @linemode: Which mode (T1/E1/J1) to use for this span.
2136 *
2137 * This function must only be called before the span is assigned it's
2138 * possible for user processes to have an open reference to the
2139 * channels.
2140 *
2141 */
2142 static int t4_alloc_channels(struct t4 *wc, struct t4_span *ts,
2143 enum linemode linemode)
2144 {
2145 int i;
2146
2147 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &ts->span.flags)) {
2148 dev_dbg(&wc->dev->dev,
2149 "Cannot allocate channels on a span that is already "
2150 "assigned.\n");
2151 return -EINVAL;
2152 }
2153
2154 /* Cleanup any previously allocated channels. */
2155 for (i = 0; i < ARRAY_SIZE(ts->chans); ++i) {
2156 kfree(ts->chans[i]);
2157 kfree(ts->ec[i]);
2158 ts->chans[i] = NULL;
2159 ts->ec[i] = NULL;
2160 }
2161
2162 ts->linemode = linemode;
2163 for (i = 0; i < ((E1 == ts->linemode) ? 31 : 24); i++) {
2164 struct dahdi_chan *chan;
2165 struct dahdi_echocan_state *ec;
2166
2167 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
2168 if (!chan) {
2169 free_wc(wc);
2170 return -ENOMEM;
2171 }
2172 ts->chans[i] = chan;
2173
2174 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
2175 if (!ec) {
2176 free_wc(wc);
2177 return -ENOMEM;
2178 }
2179 ts->ec[i] = ec;
2180 }
2181
2182 return 0;
2183 }
2184
2185 static void t4_init_one_span(struct t4 *wc, struct t4_span *ts)
2186 {
2187 unsigned long flags;
2188 unsigned int reg;
2189 int i;
2190
2191 snprintf(ts->span.name, sizeof(ts->span.name) - 1,
2192 "TE%d/%d/%d", wc->numspans, wc->num, ts->span.offset + 1);
2193 snprintf(ts->span.desc, sizeof(ts->span.desc) - 1,
2194 "T%dXXP (PCI) Card %d Span %d", wc->numspans, wc->num,
2195 ts->span.offset + 1);
2196
2197 switch (ts->linemode) {
2198 case T1:
2199 ts->span.spantype = SPANTYPE_DIGITAL_T1;
2200 break;
2201 case E1:
2202 ts->span.spantype = SPANTYPE_DIGITAL_E1;
2203 break;
2204 case J1:
2205 ts->span.spantype = SPANTYPE_DIGITAL_J1;
2206 break;
2207 }
2208
2209 /* HDLC Specific init */
2210 ts->sigchan = NULL;
2211 ts->sigmode = sigmode;
2212 ts->sigactive = 0;
2213
2214 if (E1 != ts->linemode) {
2215 ts->span.channels = 24;
2216 ts->span.deflaw = DAHDI_LAW_MULAW;
2217 ts->span.linecompat = DAHDI_CONFIG_AMI |
2218 DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 |
2219 DAHDI_CONFIG_ESF;
2220 } else {
2221 ts->span.channels = 31;
2222 ts->span.deflaw = DAHDI_LAW_ALAW;
2223 ts->span.linecompat = DAHDI_CONFIG_AMI |
2224 DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS |
2225 DAHDI_CONFIG_CRC4;
2226 }
2227 ts->span.chans = ts->chans;
2228 ts->span.flags = DAHDI_FLAG_RBS;
2229
2230 for (i = 0; i < ts->span.channels; i++) {
2231 struct dahdi_chan *const chan = ts->chans[i];
2232 chan->pvt = wc;
2233 snprintf(chan->name, sizeof(chan->name) - 1,
2234 "%s/%d", ts->span.name, i + 1);
2235 t4_chan_set_sigcap(&ts->span, i);
2236 chan->chanpos = i + 1;
2237 }
2238
2239 /* Enable 1sec timer interrupt */
2240 spin_lock_irqsave(&wc->reglock, flags);
2241 reg = __t4_framer_in(wc, ts->span.offset, FMR1_T);
2242 __t4_framer_out(wc, ts->span.offset, FMR1_T, (reg | FMR1_ECM));
2243
2244 /* Enable Errored Second interrupt */
2245 __t4_framer_out(wc, ts->span.offset, ESM, 0);
2246 spin_unlock_irqrestore(&wc->reglock, flags);
2247
2248 t4_reset_counters(&ts->span);
2249 }
2250
2251 /**
2252 * t4_set_linemode - Allows user space to change the linemode before spans are assigned.
2253 * @span: span on which to change the linemode.
2254 * @linemode: A value from enumerated spantypes
2255 *
2256 * This callback is used to override the E1/T1 mode jumper settings and set
2257 * the linemode on for each span. Called when the "spantype" attribute
2258 * is written in sysfs under the dahdi_device.
2259 *
2260 */
2261 static int t4_set_linemode(struct dahdi_span *span, enum spantypes linemode)
2262 {
2263 struct t4_span *ts = container_of(span, struct t4_span, span);
2264 struct t4 *wc = ts->owner;
2265 int res = 0;
2266 enum linemode mode;
2267 const char *old_name;
2268 static DEFINE_MUTEX(linemode_lock);
2269 unsigned long flags;
2270
2271 dev_dbg(&wc->dev->dev, "Setting '%s' to '%s'\n", span->name,
2272 dahdi_spantype2str(linemode));
2273
2274 if (span->spantype == linemode)
2275 return 0;
2276
2277 spin_lock_irqsave(&wc->reglock, flags);
2278 wc->reset_required = 1;
2279 spin_unlock_irqrestore(&wc->reglock, flags);
2280
2281 /* Do not allow the t1e1 member to be changed by multiple threads. */
2282 mutex_lock(&linemode_lock);
2283 old_name = dahdi_spantype2str(span->spantype);
2284 switch (linemode) {
2285 case SPANTYPE_DIGITAL_T1:
2286 dev_info(&wc->dev->dev,
2287 "Changing from %s to T1 line mode.\n", old_name);
2288 mode = T1;
2289 wc->t1e1 &= ~(1 << span->offset);
2290 break;
2291 case SPANTYPE_DIGITAL_E1:
2292 dev_info(&wc->dev->dev,
2293 "Changing from %s to E1 line mode.\n", old_name);
2294 mode = E1;
2295 wc->t1e1 |= (1 << span->offset);
2296 break;
2297 case SPANTYPE_DIGITAL_J1:
2298 dev_info(&wc->dev->dev,
2299 "Changing from %s to J1 line mode.\n", old_name);
2300 mode = J1;
2301 wc->t1e1 &= ~(1 << span->offset);
2302 break;
2303 default:
2304 dev_err(&wc->dev->dev,
2305 "Got invalid linemode %d from dahdi\n", linemode);
2306 res = -EINVAL;
2307 }
2308
2309 if (!res) {
2310 t4_alloc_channels(wc, ts, mode);
2311 t4_init_one_span(wc, ts);
2312 dahdi_init_span(span);
2313 }
2314
2315 mutex_unlock(&linemode_lock);
2316 return res;
2317 }
2318
2319 static const struct dahdi_span_ops t4_gen1_span_ops = {
2320 .owner = THIS_MODULE,
2321 .spanconfig = t4_spanconfig,
2322 .chanconfig = t4_chanconfig,
2323 .startup = t4_startup,
2324 .shutdown = t4_shutdown,
2325 .rbsbits = t4_rbsbits,
2326 .maint = t4_maint,
2327 .ioctl = t4_ioctl,
2328 .hdlc_hard_xmit = t4_hdlc_hard_xmit,
2329 .assigned = t4_span_assigned,
2330 .set_spantype = t4_set_linemode,
2331 };
2332
2333 static const struct dahdi_span_ops t4_gen2_span_ops = {
2334 .owner = THIS_MODULE,
2335 .spanconfig = t4_spanconfig,
2336 .chanconfig = t4_chanconfig,
2337 .startup = t4_startup,
2338 .shutdown = t4_shutdown,
2339 .rbsbits = t4_rbsbits,
2340 .maint = t4_maint,
2341 .ioctl = t4_ioctl,
2342 .hdlc_hard_xmit = t4_hdlc_hard_xmit,
2343 .dacs = t4_dacs,
2344 .assigned = t4_span_assigned,
2345 .set_spantype = t4_set_linemode,
2346 #ifdef VPM_SUPPORT
2347 .echocan_create = t4_echocan_create,
2348 .echocan_name = t4_echocan_name,
2349 #endif
2350 };
2351
2352 /**
2353 * init_spans - Do first initialization on all the spans
2354 * @wc: Card to initialize the spans on.
2355 *
2356 * This function is called *before* the dahdi_device is first registered
2357 * with the system. What happens in t4_init_one_span can happen between
2358 * when the device is registered and when the spans are assigned via
2359 * sysfs (or automatically).
2360 *
2361 */
2362 static void init_spans(struct t4 *wc)
2363 {
2364 int x, y;
2365 int gen2;
2366 struct t4_span *ts;
2367 unsigned int reg;
2368 unsigned long flags;
2369
2370 gen2 = (wc->tspans[0]->spanflags & FLAG_2NDGEN);
2371 for (x = 0; x < wc->numspans; x++) {
2372 ts = wc->tspans[x];
2373
2374 sprintf(ts->span.name, "TE%d/%d/%d", wc->numspans, wc->num, x + 1);
2375 snprintf(ts->span.desc, sizeof(ts->span.desc) - 1,
2376 "T%dXXP (PCI) Card %d Span %d", wc->numspans, wc->num, x+1);
2377 switch (ts->linemode) {
2378 case T1:
2379 ts->span.spantype = SPANTYPE_DIGITAL_T1;
2380 break;
2381 case E1:
2382 ts->span.spantype = SPANTYPE_DIGITAL_E1;
2383 break;
2384 case J1:
2385 ts->span.spantype = SPANTYPE_DIGITAL_J1;
2386 break;
2387 }
2388
2389 /* HDLC Specific init */
2390 ts->sigchan = NULL;
2391 ts->sigmode = sigmode;
2392 ts->sigactive = 0;
2393
2394 if (E1 != ts->linemode) {
2395 ts->span.channels = 24;
2396 ts->span.deflaw = DAHDI_LAW_MULAW;
2397 ts->span.linecompat = DAHDI_CONFIG_AMI |
2398 DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 |
2399 DAHDI_CONFIG_ESF;
2400 } else {
2401 ts->span.channels = 31;
2402 ts->span.deflaw = DAHDI_LAW_ALAW;
2403 ts->span.linecompat = DAHDI_CONFIG_AMI |
2404 DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS |
2405 DAHDI_CONFIG_CRC4;
2406 }
2407 ts->span.chans = ts->chans;
2408 ts->span.flags = DAHDI_FLAG_RBS;
2409
2410 ts->owner = wc;
2411 ts->span.offset = x;
2412 ts->writechunk = (void *)(wc->writechunk + x * 32 * 2);
2413 ts->readchunk = (void *)(wc->readchunk + x * 32 * 2);
2414
2415 if (gen2) {
2416 ts->span.ops = &t4_gen2_span_ops;
2417 } else {
2418 ts->span.ops = &t4_gen1_span_ops;
2419 }
2420
2421 for (y=0;y<wc->tspans[x]->span.channels;y++) {
2422 struct dahdi_chan *mychans = ts->chans[y];
2423 sprintf(mychans->name, "TE%d/%d/%d/%d", wc->numspans, wc->num, x + 1, y + 1);
2424 t4_chan_set_sigcap(&ts->span, x);
2425 mychans->pvt = wc;
2426 mychans->chanpos = y + 1;
2427 }
2428
2429 /* Start checking for alarms in 250 ms */
2430 ts->alarmcheck_time = jiffies + msecs_to_jiffies(250);
2431
2432 /* Enable 1sec timer interrupt */
2433 spin_lock_irqsave(&wc->reglock, flags);
2434 reg = __t4_framer_in(wc, x, FMR1_T);
2435 __t4_framer_out(wc, x, FMR1_T, (reg | FMR1_ECM));
2436
2437 /* Enable Errored Second interrupt */
2438 __t4_framer_out(wc, x, ESM, 0);
2439 spin_unlock_irqrestore(&wc->reglock, flags);
2440
2441 t4_reset_counters(&ts->span);
2442
2443 }
2444
2445 set_span_devicetype(wc);
2446 setup_chunks(wc, 0);
2447 wc->lastindex = 0;
2448 }
2449
2450 static int syncsrc = 0;
2451 static int syncnum = 0 /* -1 */;
2452 static int syncspan = 0;
2453 static DEFINE_SPINLOCK(synclock);
2454
2455 static void __t4_set_rclk_src(struct t4 *wc, int span)
2456 {
2457 if (is_octal(wc)) {
2458 int cmr5 = 0x00 | (span << 5);
2459 int cmr1 = 0x38; /* Clock Mode: RCLK sourced by DCO-R1
2460 by default, Disable Clock-Switching */
2461
2462 __t4_framer_out(wc, 0, 0x44, cmr1);
2463 __t4_framer_out(wc, 0, FRMR_CMR5, cmr5);
2464 } else {
2465 int cmr1 = 0x38; /* Clock Mode: RCLK sourced by DCO-R1
2466 by default, Disable Clock-Switching */
2467 cmr1 |= (span << 6);
2468 __t4_framer_out(wc, 0, 0x44, cmr1);
2469 }
2470
2471 dev_info(&wc->dev->dev, "RCLK source set to span %d\n", span+1);
2472 }
2473
2474 static void __t4_set_sclk_src(struct t4 *wc, int mode, int master, int slave)
2475 {
2476 if (slave) {
2477 wc->dmactrl |= (1 << 25);
2478 dev_info(&wc->dev->dev, "SCLK is slaved to timing cable\n");
2479 } else {
2480 wc->dmactrl &= ~(1 << 25);
2481 }
2482
2483 if (master) {
2484 wc->dmactrl |= (1 << 24);
2485 dev_info(&wc->dev->dev, "SCLK is master to timing cable\n");
2486 } else {
2487 wc->dmactrl &= ~(1 << 24);
2488 }
2489
2490 if (mode == WC_RECOVER)
2491 wc->dmactrl |= (1 << 29); /* Recover timing from RCLK */
2492
2493 if (mode == WC_SELF)
2494 wc->dmactrl &= ~(1 << 29);/* Provide timing from MCLK */
2495
2496 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
2497 }
2498
2499 static ssize_t t4_timing_master_show(struct device *dev,
2500 struct device_attribute *attr,
2501 char *buf)
2502 {
2503 struct t4 *wc = dev_get_drvdata(dev);
2504 if (wc->dmactrl & (1 << 29))
2505 return sprintf(buf, "%d\n", wc->syncsrc);
2506 else
2507 return sprintf(buf, "%d\n", -1);
2508 }
2509
2510 static DEVICE_ATTR(timing_master, 0400, t4_timing_master_show, NULL);
2511
2512 static void create_sysfs_files(struct t4 *wc)
2513 {
2514 int ret;
2515 ret = device_create_file(&wc->dev->dev,
2516 &dev_attr_timing_master);
2517 if (ret) {
2518 dev_info(&wc->dev->dev,
2519 "Failed to create device attributes.\n");
2520 }
2521 }
2522
2523 static void remove_sysfs_files(struct t4 *wc)
2524 {
2525 device_remove_file(&wc->dev->dev,
2526 &dev_attr_timing_master);
2527 }
2528
2529 static inline void __t4_update_timing(struct t4 *wc)
2530 {
2531 int i;
2532 /* update sync src info */
2533 if (wc->syncsrc != syncsrc) {
2534 dev_info(&wc->dev->dev, "Swapping card %d from %d to %d\n",
2535 wc->num, wc->syncsrc, syncsrc);
2536 wc->syncsrc = syncsrc;
2537 /* Update sync sources */
2538 for (i = 0; i < wc->numspans; i++) {
2539 wc->tspans[i]->span.syncsrc = wc->syncsrc;
2540 }
2541 if (syncnum == wc->num) {
2542 __t4_set_rclk_src(wc, syncspan-1);
2543 __t4_set_sclk_src(wc, WC_RECOVER, 1, 0);
2544 if (debug)
2545 dev_notice(&wc->dev->dev, "Card %d, using sync "
2546 "span %d, master\n", wc->num, syncspan);
2547 } else {
2548 __t4_set_sclk_src(wc, WC_RECOVER, 0, 1);
2549 if (debug)
2550 dev_notice(&wc->dev->dev, "Card %d, using "
2551 "Timing Bus, NOT master\n", wc->num);
2552 }
2553 }
2554 }
2555
2556 static int __t4_findsync(struct t4 *wc)
2557 {
2558 int i;
2559 int x;
2560 unsigned long flags;
2561 int p;
2562 int nonzero;
2563 int newsyncsrc = 0; /* DAHDI span number */
2564 int newsyncnum = 0; /* wct4xxp card number */
2565 int newsyncspan = 0; /* span on given wct4xxp card */
2566 spin_lock_irqsave(&synclock, flags);
2567 if (!wc->num) {
2568 /* If we're the first card, go through all the motions, up to 8 levels
2569 of sync source */
2570 p = 1;
2571 while (p < 8) {
2572 nonzero = 0;
2573 for (x=0;cards[x];x++) {
2574 for (i = 0; i < cards[x]->numspans; i++) {
2575 if (cards[x]->tspans[i]->syncpos) {
2576 nonzero = 1;
2577 if ((cards[x]->tspans[i]->syncpos == p) &&
2578 !(cards[x]->tspans[i]->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_LOOPBACK)) &&
2579 (cards[x]->tspans[i]->span.flags & DAHDI_FLAG_RUNNING)) {
2580 /* This makes a good sync source */
2581 newsyncsrc = cards[x]->tspans[i]->span.spanno;
2582 newsyncnum = x;
2583 newsyncspan = i + 1;
2584 /* Jump out */
2585 goto found;
2586 }
2587 }
2588 }
2589 }
2590 if (nonzero)
2591 p++;
2592 else
2593 break;
2594 }
2595 found:
2596 if ((syncnum != newsyncnum) || (syncsrc != newsyncsrc) || (newsyncspan != syncspan)) {
2597 if (debug)
2598 dev_notice(&wc->dev->dev, "New syncnum: %d "
2599 "(was %d), syncsrc: %d (was %d), "
2600 "syncspan: %d (was %d)\n", newsyncnum,
2601 syncnum, newsyncsrc, syncsrc,
2602 newsyncspan, syncspan);
2603 syncnum = newsyncnum;
2604 syncsrc = newsyncsrc;
2605 syncspan = newsyncspan;
2606 for (x=0;cards[x];x++) {
2607 __t4_update_timing(cards[x]);
2608 }
2609 }
2610 }
2611 __t4_update_timing(wc);
2612 spin_unlock_irqrestore(&synclock, flags);
2613 return 0;
2614 }
2615
2616 static void __t4_set_timing_source_auto(struct t4 *wc)
2617 {
2618 int x, i;
2619 int firstprio, secondprio;
2620 firstprio = secondprio = 4;
2621
2622 if (debug)
2623 dev_info(&wc->dev->dev, "timing source auto\n");
2624 clear_bit(T4_CHECK_TIMING, &wc->checkflag);
2625 if (timingcable) {
2626 __t4_findsync(wc);
2627 } else {
2628 if (debug)
2629 dev_info(&wc->dev->dev, "Evaluating spans for timing "
2630 "source\n");
2631 for (x=0;x<wc->numspans;x++) {
2632 if ((wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) &&
2633 !(wc->tspans[x]->span.alarms & (DAHDI_ALARM_RED |
2634 DAHDI_ALARM_BLUE))) {
2635 if (debug)
2636 dev_info(&wc->dev->dev, "span %d is "
2637 "green : syncpos %d\n", x+1,
2638 wc->tspans[x]->syncpos);
2639 if (wc->tspans[x]->syncpos) {
2640 /* Valid rsync source in recovered
2641 timing mode */
2642 if (firstprio == 4)
2643 firstprio = x;
2644 else if (wc->tspans[x]->syncpos <
2645 wc->tspans[firstprio]->syncpos)
2646 firstprio = x;
2647 } else {
2648 /* Valid rsync source in system timing
2649 mode */
2650 if (secondprio == 4)
2651 secondprio = x;
2652 }
2653 }
2654 }
2655 if (firstprio != 4) {
2656 wc->syncsrc = firstprio;
2657 __t4_set_rclk_src(wc, firstprio);
2658 __t4_set_sclk_src(wc, WC_RECOVER, 0, 0);
2659 dev_info(&wc->dev->dev, "Recovered timing mode, "\
2660 "RCLK set to span %d\n",
2661 firstprio+1);
2662 } else if (secondprio != 4) {
2663 wc->syncsrc = -1;
2664 __t4_set_rclk_src(wc, secondprio);
2665 __t4_set_sclk_src(wc, WC_SELF, 0, 0);
2666 dev_info(&wc->dev->dev, "System timing mode, "\
2667 "RCLK set to span %d\n",
2668 secondprio+1);
2669 } else {
2670 wc->syncsrc = -1;
2671 dev_info(&wc->dev->dev, "All spans in alarm : No valid"\
2672 "span to source RCLK from\n");
2673 /* Default rclk to lock with span 1 */
2674 __t4_set_rclk_src(wc, 0);
2675 __t4_set_sclk_src(wc, WC_SELF, 0, 0);
2676 }
2677
2678 /* Propagate sync selection to dahdi_span struct
2679 * this is read by dahdi_tool to display the span's
2680 * master/slave sync information */
2681 for (i = 0; i < wc->numspans; i++) {
2682 wc->tspans[i]->span.syncsrc = wc->syncsrc + 1;
2683 }
2684 }
2685 }
2686
2687 static void __t4_configure_t1(struct t4 *wc, int unit, int lineconfig, int txlevel)
2688 {
2689 unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
2690 char *framing, *line;
2691 int mytxlevel;
2692 if ((txlevel > 7) || (txlevel < 4))
2693 mytxlevel = 0;
2694 else
2695 mytxlevel = txlevel - 4;
2696
2697 if (is_octal(wc))
2698 fmr1 = 0x9c | 0x02; /* FMR1: Mode 1, T1 mode, CRC on for ESF, 8.192 Mhz system data rate, no XAIS */
2699 else
2700 fmr1 = 0x9c; /* FMR1: Mode 1, T1 mode, CRC on for ESF, 8.192 Mhz system data rate, no XAIS */
2701
2702 fmr2 = 0x20; /* FMR2: no payload loopback, don't auto yellow */
2703 fmr4 = 0x0c; /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
2704 lim2 = 0x21; /* LIM2: 50% peak is a "1", Advanced Loss recovery */
2705 lim2 |= (mytxlevel << 6); /* LIM2: Add line buildout */
2706 __t4_framer_out(wc, unit, 0x1d, fmr1);
2707 __t4_framer_out(wc, unit, 0x1e, fmr2);
2708
2709 /* Configure line interface */
2710 if (lineconfig & DAHDI_CONFIG_AMI) {
2711 line = "AMI";
2712 /* workaround for errata #2 in ES v3 09-10-16 */
2713 fmr0 = (is_octal(wc) || wc->falc31) ? 0xb0 : 0xa0;
2714 } else {
2715 line = "B8ZS";
2716 fmr0 = 0xf0;
2717 }
2718 if (lineconfig & DAHDI_CONFIG_D4) {
2719 framing = "D4";
2720 } else {
2721 framing = "ESF";
2722 fmr4 |= 0x2;
2723 fmr2 |= 0xc0;
2724 }
2725 __t4_framer_out(wc, unit, 0x1c, fmr0);
2726 __t4_framer_out(wc, unit, 0x20, fmr4);
2727 __t4_framer_out(wc, unit, FMR5, FMR5_EIBR); /* FMR5: Enable RBS mode */
2728
2729 __t4_framer_out(wc, unit, 0x37, 0xf0 ); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
2730 __t4_framer_out(wc, unit, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
2731
2732 __t4_framer_out(wc, unit, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
2733 __t4_framer_out(wc, unit, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
2734
2735 if (wc->falc31) {
2736 if (debug)
2737 dev_info(&wc->dev->dev, "card %d span %d: setting Rtx "
2738 "to 0ohm for T1\n", wc->num, unit);
2739 __t4_framer_out(wc, unit, 0x86, 0x00); /* PC6: set Rtx to 0ohm for T1 */
2740
2741 // Hitting the bugfix register to fix errata #3
2742 __t4_framer_out(wc, unit, 0xbd, 0x05);
2743 }
2744
2745 __t4_framer_out(wc, unit, 0x3a, lim2); /* LIM2: 50% peak amplitude is a "1" */
2746 __t4_framer_out(wc, unit, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
2747 __t4_framer_out(wc, unit, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
2748
2749 /* Generate pulse mask for T1 */
2750 switch(mytxlevel) {
2751 case 3:
2752 __t4_framer_out(wc, unit, 0x26, 0x07); /* XPM0 */
2753 __t4_framer_out(wc, unit, 0x27, 0x01); /* XPM1 */
2754 __t4_framer_out(wc, unit, 0x28, 0x00); /* XPM2 */
2755 break;
2756 case 2:
2757 __t4_framer_out(wc, unit, 0x26, 0x8c); /* XPM0 */
2758 __t4_framer_out(wc, unit, 0x27, 0x11); /* XPM1 */
2759 __t4_framer_out(wc, unit, 0x28, 0x01); /* XPM2 */
2760 break;
2761 case 1:
2762 __t4_framer_out(wc, unit, 0x26, 0x8c); /* XPM0 */
2763 __t4_framer_out(wc, unit, 0x27, 0x01); /* XPM1 */
2764 __t4_framer_out(wc, unit, 0x28, 0x00); /* XPM2 */
2765 break;
2766 case 0:
2767 default:
2768 __t4_framer_out(wc, unit, 0x26, 0xd7); /* XPM0 */
2769 __t4_framer_out(wc, unit, 0x27, 0x22); /* XPM1 */
2770 __t4_framer_out(wc, unit, 0x28, 0x01); /* XPM2 */
2771 break;
2772 }
2773
2774 /* Don't mask framer interrupts if hardware HDLC is in use */
2775 __t4_framer_out(wc, unit, FRMR_IMR0, 0xff & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR0_MASK : 0)); /* IMR0: We care about CAS changes, etc */
2776 __t4_framer_out(wc, unit, FRMR_IMR1, 0xff & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR1_MASK : 0)); /* IMR1: We care about nothing */
2777 __t4_framer_out(wc, unit, 0x16, 0x00); /* IMR2: All the alarm stuff! */
2778 __t4_framer_out(wc, unit, 0x17, 0x34); /* IMR3: AIS and friends */
2779 __t4_framer_out(wc, unit, 0x18, 0x3f); /* IMR4: Slips on transmit */
2780
2781 dev_info(&wc->dev->dev, "Span %d configured for %s/%s\n", unit + 1,
2782 framing, line);
2783 }
2784
2785 static void __t4_configure_e1(struct t4 *wc, int unit, int lineconfig)
2786 {
2787 unsigned int fmr2, fmr1, fmr0;
2788 unsigned int cas = 0;
2789 unsigned int imr3extra=0;
2790 char *crc4 = "";
2791 char *framing, *line;
2792 if (is_octal(wc)) {
2793 /* 16 MHz */
2794 fmr1 = 0x44 | 0x02; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
2795 } else {
2796 /* 8 MHz */
2797 fmr1 = 0x44; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
2798 }
2799 fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of multiframe recovery, no payload loopback */
2800 if (lineconfig & DAHDI_CONFIG_CRC4) {
2801 fmr1 |= 0x08; /* CRC4 transmit */
2802 fmr2 |= 0xc0; /* CRC4 receive */
2803 crc4 = "/CRC4";
2804 }
2805 __t4_framer_out(wc, unit, 0x1d, fmr1);
2806 __t4_framer_out(wc, unit, 0x1e, fmr2);
2807
2808 /* Configure line interface */
2809 if (lineconfig & DAHDI_CONFIG_AMI) {
2810 line = "AMI";
2811 /* workaround for errata #2 in ES v3 09-10-16 */
2812 fmr0 = (is_octal(wc) || wc->falc31) ? 0xb0 : 0xa0;
2813 } else {
2814 line = "HDB3";
2815 fmr0 = 0xf0;
2816 }
2817 if (lineconfig & DAHDI_CONFIG_CCS) {
2818 framing = "CCS";
2819 imr3extra = 0x28;
2820 } else {
2821 framing = "CAS";
2822 cas = 0x40;
2823 }
2824 __t4_framer_out(wc, unit, 0x1c, fmr0);
2825
2826 __t4_framer_out(wc, unit, 0x37, 0xf0 /*| 0x6 */ ); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
2827 __t4_framer_out(wc, unit, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
2828
2829 __t4_framer_out(wc, unit, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
2830 __t4_framer_out(wc, unit, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
2831
2832 if (wc->falc31) {
2833 if (debug)
2834 dev_info(&wc->dev->dev,
2835 "setting Rtx to 7.5ohm for E1\n");
2836 __t4_framer_out(wc, unit, 0x86, 0x40); /* PC6: turn on 7.5ohm Rtx for E1 */
2837 }
2838
2839 /* Condition receive line interface for E1 after reset */
2840 __t4_framer_out(wc, unit, 0xbb, 0x17);
2841 __t4_framer_out(wc, unit, 0xbc, 0x55);
2842 __t4_framer_out(wc, unit, 0xbb, 0x97);
2843 __t4_framer_out(wc, unit, 0xbb, 0x11);
2844 __t4_framer_out(wc, unit, 0xbc, 0xaa);
2845 __t4_framer_out(wc, unit, 0xbb, 0x91);
2846 __t4_framer_out(wc, unit, 0xbb, 0x12);
2847 __t4_framer_out(wc, unit, 0xbc, 0x55);
2848 __t4_framer_out(wc, unit, 0xbb, 0x92);
2849 __t4_framer_out(wc, unit, 0xbb, 0x0c);
2850 __t4_framer_out(wc, unit, 0xbb, 0x00);
2851 __t4_framer_out(wc, unit, 0xbb, 0x8c);
2852
2853 __t4_framer_out(wc, unit, 0x3a, 0x20); /* LIM2: 50% peak amplitude is a "1" */
2854 __t4_framer_out(wc, unit, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
2855 __t4_framer_out(wc, unit, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
2856
2857 __t4_framer_out(wc, unit, 0x20, 0x9f); /* XSW: Spare bits all to 1 */
2858 __t4_framer_out(wc, unit, 0x21, 0x1c|cas); /* XSP: E-bit set when async. AXS auto, XSIF to 1 */
2859
2860
2861 /* Generate pulse mask for E1 */
2862 __t4_framer_out(wc, unit, 0x26, 0x54); /* XPM0 */
2863 __t4_framer_out(wc, unit, 0x27, 0x02); /* XPM1 */
2864 __t4_framer_out(wc, unit, 0x28, 0x00); /* XPM2 */
2865
2866 /* Don't mask framer interrupts if hardware HDLC is in use */
2867 __t4_framer_out(wc, unit, FRMR_IMR0, 0xff & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR0_MASK : 0)); /* IMR0: We care about CRC errors, CAS changes, etc */
2868 __t4_framer_out(wc, unit, FRMR_IMR1, 0x3f & ~((wc->tspans[unit]->sigchan) ? HDLC_IMR1_MASK : 0)); /* IMR1: We care about loopup / loopdown */
2869 __t4_framer_out(wc, unit, 0x16, 0x00); /* IMR2: We care about all the alarm stuff! */
2870 __t4_framer_out(wc, unit, 0x17, 0x04 | imr3extra); /* IMR3: AIS */
2871 __t4_framer_out(wc, unit, 0x18, 0x3f); /* IMR4: We care about slips on transmit */
2872
2873 __t4_framer_out(wc, unit, 0x2f, 0x00);
2874 __t4_framer_out(wc, unit, 0x30, 0x00);
2875 __t4_framer_out(wc, unit, 0x31, 0x00);
2876
2877 dev_info(&wc->dev->dev, "TE%dXXP: Span %d configured for %s/%s%s\n",
2878 wc->numspans, unit + 1, framing, line, crc4);
2879 }
2880
2881 /**
2882 * t4_check_for_interrupts - Return 0 if the card is generating interrupts.
2883 * @wc: The card to check.
2884 *
2885 * If the card is not generating interrupts, this function will also place all
2886 * the spans on the card into red alarm.
2887 *
2888 */
2889 static int t4_check_for_interrupts(struct t4 *wc)
2890 {
2891 unsigned int starting_intcount = wc->intcount;
2892 unsigned long stop_time = jiffies + HZ*2;
2893 unsigned long flags;
2894 int x;
2895
2896 msleep(20);
2897 spin_lock_irqsave(&wc->reglock, flags);
2898 while (starting_intcount == wc->intcount) {
2899 spin_unlock_irqrestore(&wc->reglock, flags);
2900 if (time_after(jiffies, stop_time)) {
2901 for (x = 0; x < wc->numspans; x++)
2902 wc->tspans[x]->span.alarms = DAHDI_ALARM_RED;
2903 dev_err(&wc->dev->dev, "Interrupts not detected.\n");
2904 return -EIO;
2905 }
2906 msleep(100);
2907 spin_lock_irqsave(&wc->reglock, flags);
2908 }
2909 spin_unlock_irqrestore(&wc->reglock, flags);
2910
2911 return 0;
2912 }
2913
2914 static int _t4_startup(struct file *file, struct dahdi_span *span)
2915 {
2916 #ifdef SUPPORT_GEN1
2917 int i;
2918 #endif
2919 int tspan;
2920 unsigned long flags;
2921 int alreadyrunning;
2922 struct t4_span *ts = container_of(span, struct t4_span, span);
2923 struct t4 *wc = ts->owner;
2924
2925 set_bit(T4_IGNORE_LATENCY, &wc->checkflag);
2926 if (debug)
2927 dev_info(&wc->dev->dev, "About to enter startup!\n");
2928
2929 tspan = span->offset + 1;
2930 if (tspan < 0) {
2931 dev_info(&wc->dev->dev, "TE%dXXP: Span '%d' isn't us?\n",
2932 wc->numspans, span->spanno);
2933 return -1;
2934 }
2935
2936 spin_lock_irqsave(&wc->reglock, flags);
2937
2938 alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
2939
2940 #ifdef SUPPORT_GEN1
2941 /* initialize the start value for the entire chunk of last ec buffer */
2942 for(i = 0; i < span->channels; i++)
2943 {
2944 memset(ts->ec_chunk1[i],
2945 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
2946 memset(ts->ec_chunk2[i],
2947 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
2948 }
2949 #endif
2950 /* Force re-evaluation of timing source */
2951 wc->syncsrc = -1;
2952 set_bit(T4_CHECK_TIMING, &wc->checkflag);
2953
2954 if (E1 == ts->linemode)
2955 __t4_configure_e1(wc, span->offset, span->lineconfig);
2956 else
2957 __t4_configure_t1(wc, span->offset, span->lineconfig, span->txlevel);
2958
2959 /* Note clear channel status */
2960 wc->tspans[span->offset]->notclear = 0;
2961 __set_clear(wc, span->offset);
2962
2963 if (!alreadyrunning) {
2964 span->flags |= DAHDI_FLAG_RUNNING;
2965 wc->spansstarted++;
2966
2967 if (wc->devtype->flags & FLAG_5THGEN)
2968 __t4_pci_out(wc, 5, (ms_per_irq << 16) | wc->numbufs);
2969 else
2970 __t4_pci_out(wc, 5, (1 << 16) | 1);
2971 /* enable interrupts */
2972 /* Start DMA, enabling DMA interrupts on read only */
2973 wc->dmactrl |= (ts->spanflags & FLAG_2NDGEN) ? 0xc0000000 : 0xc0000003;
2974 #ifdef VPM_SUPPORT
2975 wc->dmactrl |= (wc->vpm) ? T4_VPM_PRESENT : 0;
2976 #endif
2977 /* Seed interrupt register */
2978 __t4_pci_out(wc, WC_INTR, 0x0c);
2979 if (noburst || !(ts->spanflags & FLAG_BURST))
2980 wc->dmactrl |= (1 << 26);
2981 else
2982 wc->dmactrl &= ~(1 << 26);
2983 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
2984
2985 /* Startup HDLC controller too */
2986 }
2987
2988 if (ts->sigchan) {
2989 struct dahdi_chan *sigchan = ts->sigchan;
2990
2991 spin_unlock_irqrestore(&wc->reglock, flags);
2992 if (hdlc_start(wc, span->offset, sigchan, ts->sigmode)) {
2993 dev_notice(&wc->dev->dev, "Error initializing "
2994 "signalling controller\n");
2995 return -1;
2996 }
2997 spin_lock_irqsave(&wc->reglock, flags);
2998 }
2999
3000 spin_unlock_irqrestore(&wc->reglock, flags);
3001
3002 local_irq_save(flags);
3003 t4_check_alarms(wc, span->offset);
3004 t4_check_sigbits(wc, span->offset);
3005 local_irq_restore(flags);
3006
3007 if (wc->tspans[0]->sync == span->spanno)
3008 dev_info(&wc->dev->dev, "SPAN %d: Primary Sync Source\n",
3009 span->spanno);
3010 if (wc->tspans[1]->sync == span->spanno)
3011 dev_info(&wc->dev->dev, "SPAN %d: Secondary Sync Source\n",
3012 span->spanno);
3013 if (wc->numspans >= 4) {
3014 if (wc->tspans[2]->sync == span->spanno)
3015 dev_info(&wc->dev->dev, "SPAN %d: Tertiary Sync Source"
3016 "\n", span->spanno);
3017 if (wc->tspans[3]->sync == span->spanno)
3018 dev_info(&wc->dev->dev, "SPAN %d: Quaternary Sync "
3019 "Source\n", span->spanno);
3020 }
3021 if (wc->numspans == 8) {
3022 if (wc->tspans[4]->sync == span->spanno)
3023 dev_info(&wc->dev->dev, "SPAN %d: Quinary Sync "
3024 "Source\n", span->spanno);
3025 if (wc->tspans[5]->sync == span->spanno)
3026 dev_info(&wc->dev->dev, "SPAN %d: Senary Sync "
3027 "Source\n", span->spanno);
3028 if (wc->tspans[6]->sync == span->spanno)
3029 dev_info(&wc->dev->dev, "SPAN %d: Septenary Sync "
3030 "Source\n", span->spanno);
3031 if (wc->tspans[7]->sync == span->spanno)
3032 dev_info(&wc->dev->dev, "SPAN %d: Octonary Sync "
3033 "Source\n", span->spanno);
3034 }
3035
3036 if (!alreadyrunning) {
3037 if (t4_check_for_interrupts(wc))
3038 return -EIO;
3039 }
3040
3041 if (debug)
3042 dev_info(&wc->dev->dev, "Completed startup!\n");
3043 clear_bit(T4_IGNORE_LATENCY, &wc->checkflag);
3044 return 0;
3045 }
3046
3047 static int t4_startup(struct file *file, struct dahdi_span *span)
3048 {
3049 int ret;
3050 struct dahdi_device *const ddev = span->parent;
3051 struct dahdi_span *s;
3052
3053 ret = _t4_startup(file, span);
3054 list_for_each_entry(s, &ddev->spans, device_node) {
3055 if (!test_bit(DAHDI_FLAGBIT_RUNNING, &s->flags)) {
3056 _t4_startup(file, s);
3057 }
3058 }
3059 return ret;
3060 }
3061
3062 #ifdef SUPPORT_GEN1
3063 static inline void e1_check(struct t4 *wc, int span, int val)
3064 {
3065 struct t4_span *ts = wc->tspans[span];
3066 if ((ts->span.channels > 24) &&
3067 (ts->span.flags & DAHDI_FLAG_RUNNING) &&
3068 !(ts->span.alarms) &&
3069 (!wc->e1recover)) {
3070 if (val != 0x1b) {
3071 ts->e1check++;
3072 } else
3073 ts->e1check = 0;
3074 if (ts->e1check > 100) {
3075 /* Wait 1000 ms */
3076 wc->e1recover = 1000 * 8;
3077 wc->tspans[0]->e1check = wc->tspans[1]->e1check = 0;
3078 if (wc->numspans == 4)
3079 wc->tspans[2]->e1check = wc->tspans[3]->e1check = 0;
3080 if (debug & DEBUG_MAIN)
3081 dev_notice(&wc->dev->dev, "Detected loss of "
3082 "E1 alignment on span %d!\n", span);
3083 t4_reset_dma(wc);
3084 }
3085 }
3086 }
3087
3088 static void t4_receiveprep(struct t4 *wc, int irq)
3089 {
3090 unsigned int *readchunk;
3091 int dbl = 0;
3092 int x,y,z;
3093 unsigned int tmp;
3094 int offset=0;
3095 if (!has_e1_span(wc))
3096 offset = 4;
3097 if (irq & 1) {
3098 /* First part */
3099 readchunk = wc->readchunk;
3100 if (!wc->last0)
3101 dbl = 1;
3102 wc->last0 = 0;
3103 } else {
3104 readchunk = wc->readchunk + DAHDI_CHUNKSIZE * 32;
3105 if (wc->last0)
3106 dbl = 1;
3107 wc->last0 = 1;
3108 }
3109 if (unlikely(dbl && (debug & DEBUG_MAIN)))
3110 dev_notice(&wc->dev->dev, "Double/missed interrupt detected\n");
3111
3112 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
3113 for (z=0;z<24;z++) {
3114 /* All T1/E1 channels */
3115 tmp = readchunk[z+1+offset];
3116 if (wc->numspans == 4) {
3117 wc->tspans[3]->span.chans[z]->readchunk[x] = tmp & 0xff;
3118 wc->tspans[2]->span.chans[z]->readchunk[x] = (tmp & 0xff00) >> 8;
3119 }
3120 wc->tspans[1]->span.chans[z]->readchunk[x] = (tmp & 0xff0000) >> 16;
3121 wc->tspans[0]->span.chans[z]->readchunk[x] = tmp >> 24;
3122 }
3123 if (has_e1_span(wc)) {
3124 if (wc->e1recover > 0)
3125 wc->e1recover--;
3126 tmp = readchunk[0];
3127 if (wc->numspans == 4) {
3128 e1_check(wc, 3, (tmp & 0x7f));
3129 e1_check(wc, 2, (tmp & 0x7f00) >> 8);
3130 }
3131 e1_check(wc, 1, (tmp & 0x7f0000) >> 16);
3132 e1_check(wc, 0, (tmp & 0x7f000000) >> 24);
3133 for (z=24;z<31;z++) {
3134 /* Only E1 channels now */
3135 tmp = readchunk[z+1];
3136 if (wc->numspans == 4) {
3137 if (wc->tspans[3]->span.channels > 24)
3138 wc->tspans[3]->span.chans[z]->readchunk[x] = tmp & 0xff;
3139 if (wc->tspans[2]->span.channels > 24)
3140 wc->tspans[2]->span.chans[z]->readchunk[x] = (tmp & 0xff00) >> 8;
3141 }
3142 if (wc->tspans[1]->span.channels > 24)
3143 wc->tspans[1]->span.chans[z]->readchunk[x] = (tmp & 0xff0000) >> 16;
3144 if (wc->tspans[0]->span.channels > 24)
3145 wc->tspans[0]->span.chans[z]->readchunk[x] = tmp >> 24;
3146 }
3147 }
3148 /* Advance pointer by 4 TDM frame lengths */
3149 readchunk += 32;
3150 }
3151 for (x=0;x<wc->numspans;x++) {
3152 if (wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) {
3153 for (y=0;y<wc->tspans[x]->span.channels;y++) {
3154 /* Echo cancel double buffered data */
3155 dahdi_ec_chunk(wc->tspans[x]->span.chans[y],
3156 wc->tspans[x]->span.chans[y]->readchunk,
3157 wc->tspans[x]->ec_chunk2[y]);
3158 memcpy(wc->tspans[x]->ec_chunk2[y],wc->tspans[x]->ec_chunk1[y],
3159 DAHDI_CHUNKSIZE);
3160 memcpy(wc->tspans[x]->ec_chunk1[y],
3161 wc->tspans[x]->span.chans[y]->writechunk,
3162 DAHDI_CHUNKSIZE);
3163 }
3164 _dahdi_receive(&wc->tspans[x]->span);
3165 }
3166 }
3167 }
3168 #endif
3169
3170 #if (DAHDI_CHUNKSIZE != 8)
3171 #error Sorry, nextgen does not support chunksize != 8
3172 #endif
3173
3174 static void __receive_span(struct t4_span *ts)
3175 {
3176 #ifdef VPM_SUPPORT
3177 int y;
3178 unsigned long merged;
3179 merged = ts->dtmfactive & ts->dtmfmutemask;
3180 if (merged) {
3181 for (y=0;y<ts->span.channels;y++) {
3182 /* Mute any DTMFs which are supposed to be muted */
3183 if (test_bit(y, &merged)) {
3184 memset(ts->span.chans[y]->readchunk, DAHDI_XLAW(0, ts->span.chans[y]), DAHDI_CHUNKSIZE);
3185 }
3186 }
3187 }
3188 #endif
3189 _dahdi_ec_span(&ts->span);
3190 _dahdi_receive(&ts->span);
3191 }
3192
3193 static inline void __transmit_span(struct t4_span *ts)
3194 {
3195 _dahdi_transmit(&ts->span);
3196 }
3197
3198 #ifdef ENABLE_WORKQUEUES
3199 static void workq_handlespan(void *data)
3200 {
3201 struct t4_span *ts = data;
3202 struct t4 *wc = ts->owner;
3203
3204 __receive_span(ts);
3205 __transmit_span(ts);
3206 atomic_dec(&wc->worklist);
3207 if (!atomic_read(&wc->worklist))
3208 t4_pci_out(wc, WC_INTR, 0);
3209 }
3210 #else
3211 static void t4_prep_gen2(struct t4 *wc)
3212 {
3213 int x;
3214 for (x=0;x<wc->numspans;x++) {
3215 if (wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) {
3216 __receive_span(wc->tspans[x]);
3217 __transmit_span(wc->tspans[x]);
3218 }
3219 }
3220 }
3221
3222 #endif
3223 #ifdef SUPPORT_GEN1
3224 static void t4_transmitprep(struct t4 *wc, int irq)
3225 {
3226 u32 *writechunk;
3227 int x, y, z;
3228 unsigned int tmp;
3229 int offset = 0;
3230 if (!has_e1_span(wc))
3231 offset = 4;
3232 if (irq & 1) {
3233 /* First part */
3234 writechunk = wc->writechunk + 1;
3235 } else {
3236 writechunk = wc->writechunk + DAHDI_CHUNKSIZE * 32 + 1;
3237 }
3238 for (y=0;y<wc->numspans;y++) {
3239 if (wc->tspans[y]->span.flags & DAHDI_FLAG_RUNNING)
3240 _dahdi_transmit(&wc->tspans[y]->span);
3241 }
3242
3243 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
3244 /* Once per chunk */
3245 for (z=0;z<24;z++) {
3246 /* All T1/E1 channels */
3247 tmp = (wc->tspans[3]->span.chans[z]->writechunk[x]) |
3248 (wc->tspans[2]->span.chans[z]->writechunk[x] << 8) |
3249 (wc->tspans[1]->span.chans[z]->writechunk[x] << 16) |
3250 (wc->tspans[0]->span.chans[z]->writechunk[x] << 24);
3251 writechunk[z+offset] = tmp;
3252 }
3253 if (has_e1_span(wc)) {
3254 for (z=24;z<31;z++) {
3255 /* Only E1 channels now */
3256 tmp = 0;
3257 if (wc->numspans == 4) {
3258 if (wc->tspans[3]->span.channels > 24)
3259 tmp |= wc->tspans[3]->span.chans[z]->writechunk[x];
3260 if (wc->tspans[2]->span.channels > 24)
3261 tmp |= (wc->tspans[2]->span.chans[z]->writechunk[x] << 8);
3262 }
3263 if (wc->tspans[1]->span.channels > 24)
3264 tmp |= (wc->tspans[1]->span.chans[z]->writechunk[x] << 16);
3265 if (wc->tspans[0]->span.channels > 24)
3266 tmp |= (wc->tspans[0]->span.chans[z]->writechunk[x] << 24);
3267 writechunk[z] = tmp;
3268 }
3269 }
3270 /* Advance pointer by 4 TDM frame lengths */
3271 writechunk += 32;
3272 }
3273
3274 }
3275 #endif
3276
3277 static void t4_dahdi_rbsbits(struct dahdi_chan *const chan, int rxs)
3278 {
3279 if ((debug & DEBUG_RBS) && printk_ratelimit()) {
3280 const struct t4_span *tspan = container_of(chan->span,
3281 struct t4_span,
3282 span);
3283 const struct t4 *const wc = tspan->owner;
3284 dev_notice(&wc->dev->dev, "Detected sigbits change on " \
3285 "channel %s to %04x\n", chan->name, rxs);
3286 }
3287 dahdi_rbsbits(chan, rxs);
3288 }
3289
3290 static void t4_check_sigbits(struct t4 *wc, int span)
3291 {
3292 int a,i,rxs;
3293 struct t4_span *ts = wc->tspans[span];
3294
3295 if (debug & DEBUG_RBS)
3296 dev_notice(&wc->dev->dev, "Checking sigbits on span %d\n",
3297 span + 1);
3298
3299 if (E1 == ts->linemode) {
3300 for (i = 0; i < 15; i++) {
3301 a = t4_framer_in(wc, span, 0x71 + i);
3302 /* Get high channel in low bits */
3303 rxs = (a & 0xf);
3304 if (!(ts->span.chans[i+16]->sig & DAHDI_SIG_CLEAR)) {
3305 if (ts->span.chans[i+16]->rxsig != rxs)
3306 t4_dahdi_rbsbits(ts->span.chans[i+16], rxs);
3307 }
3308 rxs = (a >> 4) & 0xf;
3309 if (!(ts->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
3310 if (ts->span.chans[i]->rxsig != rxs)
3311 t4_dahdi_rbsbits(ts->span.chans[i], rxs);
3312 }
3313 }
3314 } else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
3315 for (i = 0; i < 24; i+=4) {
3316 a = t4_framer_in(wc, span, 0x70 + (i>>2));
3317 /* Get high channel in low bits */
3318 rxs = (a & 0x3) << 2;
3319 if (!(ts->span.chans[i+3]->sig & DAHDI_SIG_CLEAR)) {
3320 if (ts->span.chans[i+3]->rxsig != rxs)
3321 t4_dahdi_rbsbits(ts->span.chans[i+3], rxs);
3322 }
3323 rxs = (a & 0xc);
3324 if (!(ts->span.chans[i+2]->sig & DAHDI_SIG_CLEAR)) {
3325 if (ts->span.chans[i+2]->rxsig != rxs)
3326 t4_dahdi_rbsbits(ts->span.chans[i+2], rxs);
3327 }
3328 rxs = (a >> 2) & 0xc;
3329 if (!(ts->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
3330 if (ts->span.chans[i+1]->rxsig != rxs)
3331 t4_dahdi_rbsbits(ts->span.chans[i+1], rxs);
3332 }
3333 rxs = (a >> 4) & 0xc;
3334 if (!(ts->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
3335 if (ts->span.chans[i]->rxsig != rxs)
3336 t4_dahdi_rbsbits(ts->span.chans[i], rxs);
3337 }
3338 }
3339 } else {
3340 for (i = 0; i < 24; i+=2) {
3341 a = t4_framer_in(wc, span, 0x70 + (i>>1));
3342 /* Get high channel in low bits */
3343 rxs = (a & 0xf);
3344 if (!(ts->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
3345 /* XXX Not really reset on every trans! XXX */
3346 if (ts->span.chans[i+1]->rxsig != rxs) {
3347 t4_dahdi_rbsbits(ts->span.chans[i+1], rxs);
3348 }
3349 }
3350 rxs = (a >> 4) & 0xf;
3351 if (!(ts->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
3352 /* XXX Not really reset on every trans! XXX */
3353 if (ts->span.chans[i]->rxsig != rxs) {
3354 t4_dahdi_rbsbits(ts->span.chans[i], rxs);
3355 }
3356 }
3357 }
3358 }
3359 }
3360
3361 /* Must be called from within hardirq context. */
3362 static void t4_check_alarms(struct t4 *wc, int span)
3363 {
3364 unsigned char c, d, e;
3365 int alarms;
3366 int x,j;
3367 struct t4_span *ts = wc->tspans[span];
3368
3369 if (time_before(jiffies, ts->alarmcheck_time))
3370 return;
3371
3372 if (!(ts->span.flags & DAHDI_FLAG_RUNNING))
3373 return;
3374
3375 spin_lock(&wc->reglock);
3376
3377 c = __t4_framer_in(wc, span, 0x4c);
3378 d = __t4_framer_in(wc, span, 0x4d);
3379
3380 /* Assume no alarms */
3381 alarms = 0;
3382
3383 /* And consider only carrier alarms */
3384 ts->span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
3385
3386 if (E1 == ts->linemode) {
3387 if (c & 0x04) {
3388 /* No multiframe found, force RAI high after 400ms only if
3389 we haven't found a multiframe since last loss
3390 of frame */
3391 if (!(ts->spanflags & FLAG_NMF)) {
3392 __t4_framer_out(wc, span, 0x20, 0x9f | 0x20); /* LIM0: Force RAI High */
3393 ts->spanflags |= FLAG_NMF;
3394 dev_notice(&wc->dev->dev,
3395 "Lost crc4-multiframe alignment\n");
3396 }
3397 __t4_framer_out(wc, span, 0x1e, 0xc3); /* Reset to CRC4 mode */
3398 __t4_framer_out(wc, span, 0x1c, 0xf2); /* Force Resync */
3399 __t4_framer_out(wc, span, 0x1c, 0xf0); /* Force Resync */
3400 } else if (!(c & 0x02)) {
3401 if ((ts->spanflags & FLAG_NMF)) {
3402 __t4_framer_out(wc, span, 0x20, 0x9f); /* LIM0: Clear forced RAI */
3403 ts->spanflags &= ~FLAG_NMF;
3404 dev_notice(&wc->dev->dev,
3405 "Obtained crc4-multiframe alignment\n");
3406 }
3407 }
3408 } else {
3409 /* Detect loopup code if we're not sending one */
3410 if ((!ts->span.mainttimer) && (d & 0x08)) {
3411 /* Loop-up code detected */
3412 if ((ts->loopupcnt++ > 80) && (ts->span.maintstat != DAHDI_MAINT_REMOTELOOP)) {
3413 dev_notice(&wc->dev->dev,
3414 "span %d: Loopup detected,"\
3415 " enabling remote loop\n",
3416 span+1);
3417 __t4_framer_out(wc, span, 0x36, 0x08); /* LIM0: Disable any local loop */
3418 __t4_framer_out(wc, span, 0x37, 0xf6 ); /* LIM1: Enable remote loop */
3419 ts->span.maintstat = DAHDI_MAINT_REMOTELOOP;
3420 }
3421 } else
3422 ts->loopupcnt = 0;
3423 /* Same for loopdown code */
3424 if ((!ts->span.mainttimer) && (d & 0x10)) {
3425 /* Loop-down code detected */
3426 if ((ts->loopdowncnt++ > 80) && (ts->span.maintstat == DAHDI_MAINT_REMOTELOOP)) {
3427 dev_notice(&wc->dev->dev,
3428 "span %d: Loopdown detected,"\
3429 " disabling remote loop\n",
3430 span+1);
3431 __t4_framer_out(wc, span, 0x36, 0x08); /* LIM0: Disable any local loop */
3432 __t4_framer_out(wc, span, 0x37, 0xf0 ); /* LIM1: Disable remote loop */
3433 ts->span.maintstat = DAHDI_MAINT_NONE;
3434 }
3435 } else
3436 ts->loopdowncnt = 0;
3437 }
3438
3439 if (ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
3440 for (x=0,j=0;x < ts->span.channels;x++)
3441 if ((ts->span.chans[x]->flags & DAHDI_FLAG_OPEN) ||
3442 dahdi_have_netdev(ts->span.chans[x]))
3443 j++;
3444 if (!j)
3445 alarms |= DAHDI_ALARM_NOTOPEN;
3446 }
3447
3448 /* Loss of Frame Alignment */
3449 if (c & 0x20) {
3450 if (!ts->alarm_time) {
3451 if (unlikely(debug)) {
3452 /* starting to debounce LOF/LFA */
3453 dev_info(&wc->dev->dev, "wct%dxxp: LOF/LFA "
3454 "detected on span %d but debouncing "
3455 "for %d ms\n", wc->numspans, span + 1,
3456 alarmdebounce);
3457 }
3458 ts->alarm_time = jiffies +
3459 msecs_to_jiffies(alarmdebounce);
3460 } else if (time_after(jiffies, ts->alarm_time)) {
3461 /* Disable Slip Interrupts */
3462 e = __t4_framer_in(wc, span, 0x17);
3463 __t4_framer_out(wc, span, 0x17, (e|0x03));
3464
3465 alarms |= DAHDI_ALARM_RED;
3466 }
3467 } else {
3468 ts->alarm_time = 0;
3469 }
3470
3471 /* Loss of Signal */
3472 if (c & 0x80) {
3473 if (!ts->losalarm_time) {
3474 if (unlikely(debug)) {
3475 /* starting to debounce LOS */
3476 dev_info(&wc->dev->dev, "wct%dxxp: LOS "
3477 "detected on span %d but debouncing "
3478 "for %d ms\n", wc->numspans,
3479 span + 1, losalarmdebounce);
3480 }
3481 ts->losalarm_time = jiffies +
3482 msecs_to_jiffies(losalarmdebounce);
3483 } else if (time_after(jiffies, ts->losalarm_time)) {
3484 /* Disable Slip Interrupts */
3485 e = __t4_framer_in(wc, span, 0x17);
3486 __t4_framer_out(wc, span, 0x17, (e|0x03));
3487
3488 alarms |= DAHDI_ALARM_RED;
3489 }
3490 } else {
3491 ts->losalarm_time = 0;
3492 }
3493
3494 /* Alarm Indication Signal */
3495 if (c & 0x40) {
3496 if (!ts->aisalarm_time) {
3497 if (unlikely(debug)) {
3498 /* starting to debounce AIS */
3499 dev_info(&wc->dev->dev, "wct%dxxp: AIS "
3500 "detected on span %d but debouncing "
3501 "for %d ms\n", wc->numspans,
3502 span + 1, aisalarmdebounce);
3503 }
3504 ts->aisalarm_time = jiffies +
3505 msecs_to_jiffies(aisalarmdebounce);
3506 } else if (time_after(jiffies, ts->aisalarm_time)) {
3507 alarms |= DAHDI_ALARM_BLUE;
3508 }
3509 } else {
3510 ts->aisalarm_time = 0;
3511 }
3512
3513 /* Add detailed alarm status information to a red alarm state */
3514 if (alarms & DAHDI_ALARM_RED) {
3515 if (c & FRS0_LOS)
3516 alarms |= DAHDI_ALARM_LOS;
3517 if (c & FRS0_LFA)
3518 alarms |= DAHDI_ALARM_LFA;
3519 if (c & FRS0_LMFA)
3520 alarms |= DAHDI_ALARM_LMFA;
3521 }
3522
3523 if (unlikely(debug)) {
3524 /* Check to ensure the xmit line isn't shorted */
3525 if (unlikely(d & FRS1_XLS)) {
3526 dev_info(&wc->dev->dev,
3527 "Detected a possible hardware malfunction"\
3528 " this card may need servicing\n");
3529 }
3530 }
3531
3532 if (((!ts->span.alarms) && alarms) ||
3533 (ts->span.alarms && (!alarms)))
3534 set_bit(T4_CHECK_TIMING, &wc->checkflag);
3535
3536 /* Keep track of recovering */
3537 if ((!alarms) && ts->span.alarms)
3538 ts->alarmtimer = DAHDI_ALARMSETTLE_TIME;
3539 if (ts->alarmtimer)
3540 alarms |= DAHDI_ALARM_RECOVER;
3541
3542 /* If receiving alarms, go into Yellow alarm state */
3543 if (alarms && !(ts->spanflags & FLAG_SENDINGYELLOW)) {
3544 /* We manually do yellow alarm to handle RECOVER and NOTOPEN, otherwise it's auto anyway */
3545 unsigned char fmr4;
3546 fmr4 = __t4_framer_in(wc, span, 0x20);
3547 __t4_framer_out(wc, span, 0x20, fmr4 | 0x20);
3548 dev_info(&wc->dev->dev, "Setting yellow alarm span %d\n",
3549 span+1);
3550 ts->spanflags |= FLAG_SENDINGYELLOW;
3551 } else if ((!alarms) && (ts->spanflags & FLAG_SENDINGYELLOW)) {
3552 unsigned char fmr4;
3553 /* We manually do yellow alarm to handle RECOVER */
3554 fmr4 = __t4_framer_in(wc, span, 0x20);
3555 __t4_framer_out(wc, span, 0x20, fmr4 & ~0x20);
3556 dev_info(&wc->dev->dev, "Clearing yellow alarm span %d\n",
3557 span+1);
3558
3559 /* Re-enable timing slip interrupts */
3560 e = __t4_framer_in(wc, span, 0x17);
3561
3562 __t4_framer_out(wc, span, 0x17, (e & ~(0x03)));
3563
3564 ts->spanflags &= ~FLAG_SENDINGYELLOW;
3565 }
3566
3567 /* Re-check the timing source when we enter/leave alarm, not withstanding
3568 yellow alarm */
3569 if (c & 0x10) { /* receiving yellow (RAI) */
3570 if (!ts->yelalarm_time) {
3571 if (unlikely(debug)) {
3572 /* starting to debounce AIS */
3573 dev_info(&wc->dev->dev, "wct%dxxp: yellow "
3574 "(RAI) detected on span %d but "
3575 "debouncing for %d ms\n",
3576 wc->numspans, span + 1,
3577 yelalarmdebounce);
3578 }
3579 ts->yelalarm_time = jiffies +
3580 msecs_to_jiffies(yelalarmdebounce);
3581 } else if (time_after(jiffies, ts->yelalarm_time)) {
3582 alarms |= DAHDI_ALARM_YELLOW;
3583 }
3584 } else {
3585 ts->yelalarm_time = 0;
3586 }
3587
3588 if (alarms)
3589 ts->alarmcheck_time = jiffies + msecs_to_jiffies(100);
3590 else
3591 ts->alarmcheck_time = jiffies + msecs_to_jiffies(50);
3592
3593 if (ts->span.mainttimer || ts->span.maintstat)
3594 alarms |= DAHDI_ALARM_LOOPBACK;
3595 ts->span.alarms = alarms;
3596
3597 spin_unlock(&wc->reglock);
3598 dahdi_alarm_notify(&ts->span);
3599 }
3600
3601 static void t4_do_counters(struct t4 *wc)
3602 {
3603 int span;
3604 for (span = 0; span < wc->numspans; span++) {
3605 struct t4_span *ts = wc->tspans[span];
3606
3607 spin_lock(&wc->reglock);
3608 if (ts->alarmtimer && (0 == (--ts->alarmtimer)))
3609 ts->span.alarms &= ~(DAHDI_ALARM_RECOVER);
3610 spin_unlock(&wc->reglock);
3611
3612 t4_check_alarms(wc, span);
3613 }
3614 }
3615
3616 static inline void __handle_leds(struct t4 *wc)
3617 {
3618 int x;
3619
3620 wc->blinktimer++;
3621 for (x=0;x<wc->numspans;x++) {
3622 struct t4_span *ts = wc->tspans[x];
3623 if (ts->span.flags & DAHDI_FLAG_RUNNING) {
3624 if ((ts->span.alarms & (DAHDI_ALARM_RED |
3625 DAHDI_ALARM_BLUE)) ||
3626 ts->losalarm_time) {
3627 #ifdef FANCY_ALARM
3628 if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
3629 __t4_set_led(wc, x, WC_RED);
3630 }
3631 if (wc->blinktimer == 0xf) {
3632 __t4_set_led(wc, x, WC_OFF);
3633 }
3634 #else
3635 if (wc->blinktimer == 160) {
3636 __t4_set_led(wc, x, WC_RED);
3637 } else if (wc->blinktimer == 480) {
3638 __t4_set_led(wc, x, WC_OFF);
3639 }
3640 #endif
3641 } else if (ts->span.alarms & DAHDI_ALARM_YELLOW) {
3642 /* Yellow Alarm */
3643 __t4_set_led(wc, x, WC_YELLOW);
3644 } else if (ts->span.mainttimer || ts->span.maintstat) {
3645 #ifdef FANCY_ALARM
3646 if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
3647 __t4_set_led(wc, x, WC_GREEN);
3648 }
3649 if (wc->blinktimer == 0xf) {
3650 __t4_set_led(wc, x, WC_OFF);
3651 }
3652 #else
3653 if (wc->blinktimer == 160) {
3654 __t4_set_led(wc, x, WC_GREEN);
3655 } else if (wc->blinktimer == 480) {
3656 __t4_set_led(wc, x, WC_OFF);
3657 }
3658 #endif
3659 } else {
3660 /* No Alarm */
3661 __t4_set_led(wc, x, WC_GREEN);
3662 }
3663 } else
3664 __t4_set_led(wc, x, WC_OFF);
3665
3666 }
3667 #ifdef FANCY_ALARM
3668 if (wc->blinktimer == 0xf) {
3669 wc->blinktimer = -1;
3670 wc->alarmpos++;
3671 if (wc->alarmpos >= ARRAY_SIZE(altab))
3672 wc->alarmpos = 0;
3673 }
3674 #else
3675 if (wc->blinktimer == 480)
3676 wc->blinktimer = 0;
3677 #endif
3678 }
3679
3680 static inline void t4_framer_interrupt(struct t4 *wc, int span)
3681 {
3682 /* Check interrupts for a given span */
3683 unsigned char gis, isr0, isr1, isr2, isr3, isr4;
3684 int readsize = -1;
3685 struct t4_span *ts = wc->tspans[span];
3686 struct dahdi_chan *sigchan;
3687 unsigned long flags;
3688 bool recheck_sigbits = false;
3689
3690
3691 /* 1st gen cards isn't used interrupts */
3692 spin_lock_irqsave(&wc->reglock, flags);
3693 gis = __t4_framer_in(wc, span, FRMR_GIS);
3694 isr0 = (gis & FRMR_GIS_ISR0) ? __t4_framer_in(wc, span, FRMR_ISR0) : 0;
3695 isr1 = (gis & FRMR_GIS_ISR1) ? __t4_framer_in(wc, span, FRMR_ISR1) : 0;
3696 isr2 = (gis & FRMR_GIS_ISR2) ? __t4_framer_in(wc, span, FRMR_ISR2) : 0;
3697 isr3 = (gis & FRMR_GIS_ISR3) ? __t4_framer_in(wc, span, FRMR_ISR3) : 0;
3698 isr4 = (gis & FRMR_GIS_ISR4) ? __t4_framer_in(wc, span, FRMR_ISR4) : 0;
3699
3700 if ((debug & DEBUG_FRAMER) && !(isr3 & ISR3_SEC)) {
3701 dev_info(&wc->dev->dev, "gis: %02x, isr0: %02x, isr1: %02x, "\
3702 "isr2: %02x, isr3: %08x, isr4: %02x, intcount=%u\n",
3703 gis, isr0, isr1, isr2, isr3, isr4, wc->intcount);
3704 }
3705
3706 /* Collect performance counters once per second */
3707 if (isr3 & ISR3_SEC) {
3708 ts->span.count.fe += __t4_framer_in(wc, span, FECL_T);
3709 ts->span.count.crc4 += __t4_framer_in(wc, span, CEC1L_T);
3710 ts->span.count.cv += __t4_framer_in(wc, span, CVCL_T);
3711 ts->span.count.ebit += __t4_framer_in(wc, span, EBCL_T);
3712 ts->span.count.be += __t4_framer_in(wc, span, BECL_T);
3713 ts->span.count.prbs = __t4_framer_in(wc, span, FRS1_T);
3714 if (DAHDI_RXSIG_INITIAL == ts->span.chans[0]->rxhooksig)
3715 recheck_sigbits = true;
3716 }
3717 spin_unlock_irqrestore(&wc->reglock, flags);
3718
3719 /* Collect errored second counter once per second */
3720 if (isr3 & ISR3_ES) {
3721 ts->span.count.errsec += 1;
3722 }
3723
3724 if (isr0 & 0x08 || recheck_sigbits)
3725 t4_check_sigbits(wc, span);
3726
3727 if (E1 == ts->linemode) {
3728 /* E1 checks */
3729 if ((isr3 & 0x38) || isr2 || isr1)
3730 t4_check_alarms(wc, span);
3731 } else {
3732 /* T1 checks */
3733 if (isr2 || (isr3 & 0x08))
3734 t4_check_alarms(wc, span);
3735 }
3736 if (!ts->span.alarms) {
3737 if ((isr3 & 0x3) || (isr4 & 0xc0))
3738 ts->span.count.timingslips++;
3739
3740 if (debug & DEBUG_MAIN) {
3741 if (isr3 & 0x02)
3742 dev_notice(&wc->dev->dev, "TE%d10P: RECEIVE "
3743 "slip NEGATIVE on span %d\n",
3744 wc->numspans, span + 1);
3745 if (isr3 & 0x01)
3746 dev_notice(&wc->dev->dev, "TE%d10P: RECEIVE "
3747 "slip POSITIVE on span %d\n",
3748 wc->numspans, span + 1);
3749 if (isr4 & 0x80)
3750 dev_notice(&wc->dev->dev, "TE%dXXP: TRANSMIT "
3751 "slip POSITIVE on span %d\n",
3752 wc->numspans, span + 1);
3753 if (isr4 & 0x40)
3754 dev_notice(&wc->dev->dev, "TE%d10P: TRANSMIT "
3755 "slip NEGATIVE on span %d\n",
3756 wc->numspans, span + 1);
3757 }
3758 } else
3759 ts->span.count.timingslips = 0;
3760
3761 spin_lock_irqsave(&wc->reglock, flags);
3762 /* HDLC controller checks - receive side */
3763 if (!ts->sigchan) {
3764 spin_unlock_irqrestore(&wc->reglock, flags);
3765 return;
3766 }
3767
3768 sigchan = ts->sigchan;
3769 spin_unlock_irqrestore(&wc->reglock, flags);
3770
3771 if (isr0 & FRMR_ISR0_RME) {
3772 readsize = (t4_framer_in(wc, span, FRMR_RBCH) << 8) | t4_framer_in(wc, span, FRMR_RBCL);
3773 if (debug & DEBUG_FRAMER)
3774 dev_notice(&wc->dev->dev, "Received data length is %d "
3775 "(%d)\n", readsize,
3776 readsize & FRMR_RBCL_MAX_SIZE);
3777 /* RPF isn't set on last part of frame */
3778 if ((readsize > 0) && ((readsize &= FRMR_RBCL_MAX_SIZE) == 0))
3779 readsize = FRMR_RBCL_MAX_SIZE + 1;
3780 } else if (isr0 & FRMR_ISR0_RPF)
3781 readsize = FRMR_RBCL_MAX_SIZE + 1;
3782
3783 if (readsize > 0) {
3784 int i;
3785 unsigned char readbuf[FRMR_RBCL_MAX_SIZE + 1];
3786
3787 if (debug & DEBUG_FRAMER)
3788 dev_notice(&wc->dev->dev, "Framer %d: Got RPF/RME! "
3789 "readsize is %d\n", sigchan->span->offset,
3790 readsize);
3791
3792 for (i = 0; i < readsize; i++)
3793 readbuf[i] = t4_framer_in(wc, span, FRMR_RXFIFO);
3794
3795 /* Tell the framer to clear the RFIFO */
3796 t4_framer_cmd_wait(wc, span, FRMR_CMDR_RMC);
3797
3798 if (debug & DEBUG_FRAMER) {
3799 dev_notice(&wc->dev->dev, "RX(");
3800 for (i = 0; i < readsize; i++)
3801 dev_notice(&wc->dev->dev, "%s%02x",
3802 (i ? " " : ""), readbuf[i]);
3803 dev_notice(&wc->dev->dev, ")\n");
3804 }
3805
3806 if (isr0 & FRMR_ISR0_RME) {
3807 /* Do checks for HDLC problems */
3808 unsigned char rsis = readbuf[readsize-1];
3809 unsigned char rsis_reg = t4_framer_in(wc, span, FRMR_RSIS);
3810
3811 ++ts->frames_in;
3812 if ((debug & DEBUG_FRAMER) && !(ts->frames_in & 0x0f))
3813 dev_notice(&wc->dev->dev, "Received %d frames "
3814 "on span %d\n", ts->frames_in, span);
3815 if (debug & DEBUG_FRAMER)
3816 dev_notice(&wc->dev->dev, "Received HDLC frame"
3817 " %d. RSIS = 0x%x (%x)\n",
3818 ts->frames_in, rsis, rsis_reg);
3819 if (!(rsis & FRMR_RSIS_CRC16)) {
3820 if (debug & DEBUG_FRAMER)
3821 dev_notice(&wc->dev->dev, "CRC check "
3822 "failed %d\n", span);
3823 dahdi_hdlc_abort(sigchan, DAHDI_EVENT_BADFCS);
3824 } else if (rsis & FRMR_RSIS_RAB) {
3825 if (debug & DEBUG_FRAMER)
3826 dev_notice(&wc->dev->dev, "ABORT of "
3827 "current frame due to "
3828 "overflow %d\n", span);
3829 dahdi_hdlc_abort(sigchan, DAHDI_EVENT_ABORT);
3830 } else if (rsis & FRMR_RSIS_RDO) {
3831 if (debug & DEBUG_FRAMER)
3832 dev_notice(&wc->dev->dev, "HDLC "
3833 "overflow occured %d\n",
3834 span);
3835 dahdi_hdlc_abort(sigchan, DAHDI_EVENT_OVERRUN);
3836 } else if (!(rsis & FRMR_RSIS_VFR)) {
3837 if (debug & DEBUG_FRAMER)
3838 dev_notice(&wc->dev->dev, "Valid Frame"
3839 " check failed on span %d\n",
3840 span);
3841 dahdi_hdlc_abort(sigchan, DAHDI_EVENT_ABORT);
3842 } else {
3843 dahdi_hdlc_putbuf(sigchan, readbuf, readsize - 1);
3844 dahdi_hdlc_finish(sigchan);
3845 if (debug & DEBUG_FRAMER)
3846 dev_notice(&wc->dev->dev, "Received "
3847 "valid HDLC frame on span %d"
3848 "\n", span);
3849 }
3850 } else if (isr0 & FRMR_ISR0_RPF)
3851 dahdi_hdlc_putbuf(sigchan, readbuf, readsize);
3852 }
3853
3854 /* Transmit side */
3855 if (isr1 & FRMR_ISR1_XDU) {
3856 if (debug & DEBUG_FRAMER)
3857 dev_notice(&wc->dev->dev, "XDU: Resetting signal "
3858 "controller!\n");
3859 t4_framer_cmd_wait(wc, span, FRMR_CMDR_SRES);
3860 } else if (isr1 & FRMR_ISR1_XPR) {
3861 if (debug & DEBUG_FRAMER)
3862 dev_notice(&wc->dev->dev, "Sigchan %d is %p\n",
3863 sigchan->chanpos, sigchan);
3864
3865 if (debug & DEBUG_FRAMER)
3866 dev_notice(&wc->dev->dev, "Framer %d: Got XPR!\n",
3867 sigchan->span->offset);
3868 t4_hdlc_xmit_fifo(wc, span, ts);
3869 }
3870
3871 if (isr1 & FRMR_ISR1_ALLS) {
3872 if (debug & DEBUG_FRAMER)
3873 dev_notice(&wc->dev->dev, "ALLS received\n");
3874 }
3875 }
3876
3877 #ifdef SUPPORT_GEN1
3878 static irqreturn_t _t4_interrupt(int irq, void *dev_id)
3879 {
3880 struct t4 *wc = dev_id;
3881 unsigned long flags;
3882 int x;
3883
3884 unsigned int status;
3885 unsigned int status2;
3886
3887 /* Make sure it's really for us */
3888 status = __t4_pci_in(wc, WC_INTR);
3889
3890 /* Process framer interrupts */
3891 status2 = t4_framer_in(wc, 0, FRMR_CIS);
3892 if (status2 & 0x0f) {
3893 for (x = 0; x < wc->numspans; ++x) {
3894 if (status2 & (1 << x))
3895 t4_framer_interrupt(wc, x);
3896 }
3897 }
3898
3899 /* Ignore if it's not for us */
3900 if (!status)
3901 return IRQ_NONE;
3902
3903 __t4_pci_out(wc, WC_INTR, 0);
3904
3905 if (!wc->spansstarted) {
3906 dev_notice(&wc->dev->dev, "Not prepped yet!\n");
3907 return IRQ_NONE;
3908 }
3909
3910 wc->intcount++;
3911
3912 if (status & 0x3) {
3913 t4_receiveprep(wc, status);
3914 t4_transmitprep(wc, status);
3915 }
3916
3917 t4_do_counters(wc);
3918
3919 x = wc->intcount & 15 /* 63 */;
3920 switch(x) {
3921 case 0:
3922 case 1:
3923 case 2:
3924 case 3:
3925 t4_check_sigbits(wc, x);
3926 break;
3927 case 4:
3928 case 5:
3929 case 6:
3930 case 7:
3931 t4_check_alarms(wc, x - 4);
3932 break;
3933 }
3934
3935 spin_lock_irqsave(&wc->reglock, flags);
3936
3937 __handle_leds(wc);
3938
3939 if (test_bit(T4_CHECK_TIMING, &wc->checkflag))
3940 __t4_set_timing_source_auto(wc);
3941
3942 spin_unlock_irqrestore(&wc->reglock, flags);
3943
3944 return IRQ_RETVAL(1);
3945 }
3946
3947 DAHDI_IRQ_HANDLER(t4_interrupt)
3948 {
3949 irqreturn_t ret;
3950 unsigned long flags;
3951 local_irq_save(flags);
3952 ret = _t4_interrupt(irq, dev_id);
3953 local_irq_restore(flags);
3954 return ret;
3955 }
3956 #endif
3957
3958 static int t4_allocate_buffers(struct t4 *wc, int numbufs,
3959 void **oldalloc, dma_addr_t *oldwritedma)
3960 {
3961 void *alloc;
3962 dma_addr_t writedma;
3963
3964 /* 32 channels, Double-buffer, Read/Write, 4 spans */
3965 alloc = pci_alloc_consistent(wc->dev, numbufs * T4_BASE_SIZE(wc) * 2,
3966 &writedma);
3967
3968 if (!alloc) {
3969 dev_notice(&wc->dev->dev, "wct%dxxp: Unable to allocate "
3970 "DMA-able memory\n", wc->numspans);
3971 return -ENOMEM;
3972 }
3973
3974 if (oldwritedma)
3975 *oldwritedma = wc->writedma;
3976 if (oldalloc)
3977 *oldalloc = wc->writechunk;
3978
3979 wc->writechunk = alloc;
3980 wc->writedma = writedma;
3981
3982 /* Read is after the whole write piece (in words) */
3983 wc->readchunk = wc->writechunk + (T4_BASE_SIZE(wc) * numbufs) / 4;
3984
3985 /* Same thing but in bytes... */
3986 wc->readdma = wc->writedma + (T4_BASE_SIZE(wc) * numbufs);
3987
3988 wc->numbufs = numbufs;
3989
3990 /* Initialize Write/Buffers to all blank data */
3991 memset(wc->writechunk, 0x00, T4_BASE_SIZE(wc) * numbufs);
3992 memset(wc->readchunk, 0xff, T4_BASE_SIZE(wc) * numbufs);
3993
3994 if (debug) {
3995 dev_notice(&wc->dev->dev, "DMA memory base of size %d at " \
3996 "%p. Read: %p and Write %p\n",
3997 numbufs * T4_BASE_SIZE(wc) * 2, wc->writechunk,
3998 wc->readchunk, wc->writechunk);
3999 }
4000
4001 return 0;
4002 }
4003
4004 static void t4_increase_latency(struct t4 *wc, int newlatency)
4005 {
4006 unsigned long flags;
4007 void *oldalloc;
4008 dma_addr_t oldaddr;
4009 int oldbufs;
4010
4011 spin_lock_irqsave(&wc->reglock, flags);
4012
4013 __t4_pci_out(wc, WC_DMACTRL, 0x00000000);
4014 /* Acknowledge any pending interrupts */
4015 __t4_pci_out(wc, WC_INTR, 0x00000000);
4016
4017 __t4_pci_in(wc, WC_VERSION);
4018
4019 oldbufs = wc->numbufs;
4020
4021 if (t4_allocate_buffers(wc, newlatency, &oldalloc, &oldaddr)) {
4022 dev_info(&wc->dev->dev, "Error allocating latency buffers for "
4023 "latency of %d\n", newlatency);
4024 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4025 spin_unlock_irqrestore(&wc->reglock, flags);
4026 return;
4027 }
4028
4029 __t4_pci_out(wc, WC_RDADDR, wc->readdma);
4030 __t4_pci_out(wc, WC_WRADDR, wc->writedma);
4031
4032 __t4_pci_in(wc, WC_VERSION);
4033
4034 __t4_pci_out(wc, 5, (ms_per_irq << 16) | newlatency);
4035 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4036
4037 __t4_pci_in(wc, WC_VERSION);
4038
4039 wc->rxident = 0;
4040 wc->lastindex = 0;
4041
4042 spin_unlock_irqrestore(&wc->reglock, flags);
4043
4044 pci_free_consistent(wc->dev, T4_BASE_SIZE(wc) * oldbufs * 2,
4045 oldalloc, oldaddr);
4046
4047 dev_info(&wc->dev->dev, "Increased latency to %d\n", newlatency);
4048
4049 }
4050
4051 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
4052 static void t4_work_func(void *data)
4053 {
4054 struct t4 *wc = data;
4055 #else
4056 static void t4_work_func(struct work_struct *work)
4057 {
4058 struct t4 *wc = container_of(work, struct t4, bh_work);
4059 #endif
4060
4061 if (test_bit(T4_CHANGE_LATENCY, &wc->checkflag)) {
4062 if (wc->needed_latency != wc->numbufs) {
4063 t4_increase_latency(wc, wc->needed_latency);
4064 clear_bit(T4_CHANGE_LATENCY, &wc->checkflag);
4065 }
4066 }
4067 #ifdef VPM_SUPPORT
4068 if (wc->vpm) {
4069 if (test_and_clear_bit(T4_CHECK_VPM, &wc->checkflag)) {
4070 /* How stupid is it that the octasic can't generate an
4071 * interrupt when there's a tone, in spite of what
4072 * their documentation says? */
4073 t4_check_vpm(wc);
4074 }
4075 }
4076 #endif
4077 }
4078
4079 static irqreturn_t _t4_interrupt_gen2(int irq, void *dev_id)
4080 {
4081 struct t4 *wc = dev_id;
4082 unsigned int status;
4083 unsigned char rxident, expected;
4084
4085 /* Check this first in case we get a spurious interrupt */
4086 if (unlikely(test_bit(T4_STOP_DMA, &wc->checkflag))) {
4087 /* Stop DMA cleanly if requested */
4088 wc->dmactrl = 0x0;
4089 t4_pci_out(wc, WC_DMACTRL, 0x00000000);
4090 /* Acknowledge any pending interrupts */
4091 t4_pci_out(wc, WC_INTR, 0x00000000);
4092 spin_lock(&wc->reglock);
4093 __t4_set_sclk_src(wc, WC_SELF, 0, 0);
4094 spin_unlock(&wc->reglock);
4095 return IRQ_RETVAL(1);
4096 }
4097
4098 /* Make sure it's really for us */
4099 status = __t4_pci_in(wc, WC_INTR);
4100
4101 /* Ignore if it's not for us */
4102 if (!(status & 0x7)) {
4103 return IRQ_NONE;
4104 }
4105
4106 #ifdef ENABLE_WORKQUEUES
4107 __t4_pci_out(wc, WC_INTR, status & 0x00000008);
4108 #endif
4109
4110 if (unlikely(!wc->spansstarted)) {
4111 dev_info(&wc->dev->dev, "Not prepped yet!\n");
4112 return IRQ_NONE;
4113 }
4114
4115 wc->intcount++;
4116 if ((wc->devtype->flags & FLAG_5THGEN) && (status & 0x2)) {
4117 rxident = (status >> 16) & 0x7f;
4118 expected = (wc->rxident + ms_per_irq) % 128;
4119
4120 if ((rxident != expected) && !test_bit(T4_IGNORE_LATENCY, &wc->checkflag)) {
4121 int needed_latency;
4122 int smallest_max;
4123
4124 if (debug & DEBUG_MAIN)
4125 dev_warn(&wc->dev->dev, "Missed interrupt. "
4126 "Expected ident of %d and got ident "
4127 "of %d\n", expected, rxident);
4128
4129 if (test_bit(T4_IGNORE_LATENCY, &wc->checkflag)) {
4130 dev_info(&wc->dev->dev,
4131 "Should have ignored latency\n");
4132 }
4133 if (rxident > wc->rxident) {
4134 needed_latency = rxident - wc->rxident;
4135 } else {
4136 needed_latency = (128 - wc->rxident) + rxident;
4137 }
4138
4139 needed_latency += 1;
4140
4141 smallest_max = (max_latency >= GEN5_MAX_LATENCY) ? GEN5_MAX_LATENCY : max_latency;
4142
4143 if (needed_latency > smallest_max) {
4144 dev_info(&wc->dev->dev, "Truncating latency "
4145 "request to %d instead of %d\n",
4146 smallest_max, needed_latency);
4147 needed_latency = smallest_max;
4148 }
4149
4150 if (needed_latency > wc->numbufs) {
4151 dev_info(&wc->dev->dev, "Need to increase "
4152 "latency. Estimated latency should "
4153 "be %d\n", needed_latency);
4154 wc->ddev->irqmisses++;
4155 wc->needed_latency = needed_latency;
4156 __t4_pci_out(wc, WC_DMACTRL, 0x00000000);
4157 set_bit(T4_CHANGE_LATENCY, &wc->checkflag);
4158 goto out;
4159 }
4160 }
4161
4162 wc->rxident = rxident;
4163 }
4164
4165 #ifdef DEBUG
4166 if (unlikely((wc->intcount < 20)))
4167 dev_dbg(&wc->dev->dev, "2G: Got interrupt, status = %08x, "
4168 "CIS = %04x\n", status, t4_framer_in(wc, 0, FRMR_CIS));
4169 #endif
4170
4171 if (likely(status & 0x2)) {
4172 #ifdef ENABLE_WORKQUEUES
4173 int cpus = num_online_cpus();
4174 atomic_set(&wc->worklist, wc->numspans);
4175 if (wc->tspans[0]->span.flags & DAHDI_FLAG_RUNNING)
4176 t4_queue_work(wc->workq, &wc->tspans[0]->swork, 0);
4177 else
4178 atomic_dec(&wc->worklist);
4179 if (wc->tspans[1]->span.flags & DAHDI_FLAG_RUNNING)
4180 t4_queue_work(wc->workq, &wc->tspans[1]->swork, 1 % cpus);
4181 else
4182 atomic_dec(&wc->worklist);
4183 if (wc->numspans == 4) {
4184 if (wc->tspans[2]->span.flags & DAHDI_FLAG_RUNNING)
4185 t4_queue_work(wc->workq, &wc->tspans[2]->swork, 2 % cpus);
4186 else
4187 atomic_dec(&wc->worklist);
4188 if (wc->tspans[3]->span.flags & DAHDI_FLAG_RUNNING)
4189 t4_queue_work(wc->workq, &wc->tspans[3]->swork, 3 % cpus);
4190 else
4191 atomic_dec(&wc->worklist);
4192 }
4193 #else
4194 unsigned int reg5 = __t4_pci_in(wc, 5);
4195
4196 #ifdef DEBUG
4197 if (wc->intcount < 20)
4198 dev_info(&wc->dev->dev, "Reg 5 is %08x\n", reg5);
4199 #endif
4200
4201 if (wc->devtype->flags & FLAG_5THGEN) {
4202 unsigned int current_index = (reg5 >> 8) & 0x7f;
4203
4204 while (((wc->lastindex + 1) % wc->numbufs) != current_index) {
4205 wc->lastindex = (wc->lastindex + 1) % wc->numbufs;
4206 setup_chunks(wc, wc->lastindex);
4207 t4_prep_gen2(wc);
4208 }
4209 } else {
4210 t4_prep_gen2(wc);
4211 }
4212
4213 #endif
4214 t4_do_counters(wc);
4215 spin_lock(&wc->reglock);
4216 __handle_leds(wc);
4217 spin_unlock(&wc->reglock);
4218
4219 }
4220
4221 if (unlikely(status & 0x1)) {
4222 unsigned char cis;
4223
4224 cis = t4_framer_in(wc, 0, FRMR_CIS);
4225 if (cis & FRMR_CIS_GIS1)
4226 t4_framer_interrupt(wc, 0);
4227 if (cis & FRMR_CIS_GIS2)
4228 t4_framer_interrupt(wc, 1);
4229 if (cis & FRMR_CIS_GIS3)
4230 t4_framer_interrupt(wc, 2);
4231 if (cis & FRMR_CIS_GIS4)
4232 t4_framer_interrupt(wc, 3);
4233
4234 if (is_octal(wc)) {
4235 if (cis & FRMR_CIS_GIS5)
4236 t4_framer_interrupt(wc, 4);
4237 if (cis & FRMR_CIS_GIS6)
4238 t4_framer_interrupt(wc, 5);
4239 if (cis & FRMR_CIS_GIS7)
4240 t4_framer_interrupt(wc, 6);
4241 if (cis & FRMR_CIS_GIS8)
4242 t4_framer_interrupt(wc, 7);
4243 }
4244 }
4245
4246 #ifdef VPM_SUPPORT
4247 if (wc->vpm && vpmdtmfsupport) {
4248 /* How stupid is it that the octasic can't generate an
4249 * interrupt when there's a tone, in spite of what their
4250 * documentation says? */
4251 if (!(wc->intcount & 0xf))
4252 set_bit(T4_CHECK_VPM, &wc->checkflag);
4253 }
4254 #endif
4255
4256 spin_lock(&wc->reglock);
4257
4258 if (unlikely(test_bit(T4_CHECK_TIMING, &wc->checkflag))) {
4259 __t4_set_timing_source_auto(wc);
4260 }
4261
4262 spin_unlock(&wc->reglock);
4263
4264 out:
4265 if (unlikely(test_bit(T4_CHANGE_LATENCY, &wc->checkflag) || test_bit(T4_CHECK_VPM, &wc->checkflag)))
4266 schedule_work(&wc->bh_work);
4267
4268 #ifndef ENABLE_WORKQUEUES
4269 __t4_pci_out(wc, WC_INTR, 0);
4270 #endif
4271
4272 return IRQ_RETVAL(1);
4273 }
4274
4275 DAHDI_IRQ_HANDLER(t4_interrupt_gen2)
4276 {
4277 irqreturn_t ret;
4278 unsigned long flags;
4279 local_irq_save(flags);
4280 ret = _t4_interrupt_gen2(irq, dev_id);
4281 local_irq_restore(flags);
4282 return ret;
4283 }
4284
4285 #ifdef SUPPORT_GEN1
4286 static int t4_reset_dma(struct t4 *wc)
4287 {
4288 /* Turn off DMA and such */
4289 wc->dmactrl = 0x0;
4290 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4291 t4_pci_out(wc, WC_COUNT, 0);
4292 t4_pci_out(wc, WC_RDADDR, 0);
4293 t4_pci_out(wc, WC_WRADDR, 0);
4294 t4_pci_out(wc, WC_INTR, 0);
4295 /* Turn it all back on */
4296 t4_pci_out(wc, WC_RDADDR, wc->readdma);
4297 t4_pci_out(wc, WC_WRADDR, wc->writedma);
4298 t4_pci_out(wc, WC_COUNT, ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 18) | ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 2));
4299 t4_pci_out(wc, WC_INTR, 0);
4300 #ifdef VPM_SUPPORT
4301 wc->dmactrl = 0xc0000000 | (1 << 29) |
4302 ((wc->vpm) ? T4_VPM_PRESENT : 0);
4303 #else
4304 wc->dmactrl = 0xc0000000 | (1 << 29);
4305 #endif
4306 if (noburst)
4307 wc->dmactrl |= (1 << 26);
4308 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4309 return 0;
4310 }
4311 #endif
4312
4313 #ifdef VPM_SUPPORT
4314 static void t4_vpm_init(struct t4 *wc)
4315 {
4316 int laws[8] = { 0, };
4317 int x;
4318 unsigned int vpm_capacity;
4319 struct firmware embedded_firmware;
4320 const struct firmware *firmware = &embedded_firmware;
4321 #if !defined(HOTPLUG_FIRMWARE)
4322 extern void _binary_dahdi_fw_oct6114_064_bin_size;
4323 extern void _binary_dahdi_fw_oct6114_128_bin_size;
4324 extern void _binary_dahdi_fw_oct6114_256_bin_size;
4325 extern u8 _binary_dahdi_fw_oct6114_064_bin_start[];
4326 extern u8 _binary_dahdi_fw_oct6114_128_bin_start[];
4327 extern u8 _binary_dahdi_fw_oct6114_256_bin_start[];
4328 #else
4329 static const char oct064_firmware[] = "dahdi-fw-oct6114-064.bin";
4330 static const char oct128_firmware[] = "dahdi-fw-oct6114-128.bin";
4331 static const char oct256_firmware[] = "dahdi-fw-oct6114-256.bin";
4332 #endif
4333
4334 if (!vpmsupport) {
4335 dev_info(&wc->dev->dev, "VPM450: Support Disabled\n");
4336 return;
4337 }
4338
4339 /* Turn on GPIO/DATA mux if supported */
4340 t4_gpio_setdir(wc, (1 << 24), (1 << 24));
4341 __t4_raw_oct_out(wc, 0x000a, 0x5678);
4342 __t4_raw_oct_out(wc, 0x0004, 0x1234);
4343 __t4_raw_oct_in(wc, 0x0004);
4344 __t4_raw_oct_in(wc, 0x000a);
4345 if (debug)
4346 dev_notice(&wc->dev->dev, "OCT Result: %04x/%04x\n",
4347 __t4_raw_oct_in(wc, 0x0004),
4348 __t4_raw_oct_in(wc, 0x000a));
4349 if (__t4_raw_oct_in(wc, 0x0004) != 0x1234) {
4350 dev_notice(&wc->dev->dev, "VPM450: Not Present\n");
4351 return;
4352 }
4353
4354 /* Setup alaw vs ulaw rules */
4355 for (x = 0;x < wc->numspans; x++) {
4356 if (wc->tspans[x]->span.channels > 24)
4357 laws[x] = 1;
4358 }
4359
4360 vpm_capacity = get_vpm450m_capacity(&wc->dev->dev);
4361 if (vpm_capacity != wc->numspans * 32) {
4362 dev_info(&wc->dev->dev, "Disabling VPMOCT%03d. TE%dXXP"\
4363 " requires a VPMOCT%03d", vpm_capacity,
4364 wc->numspans, wc->numspans*32);
4365 return;
4366 }
4367
4368 switch (vpm_capacity) {
4369 case 64:
4370 #if defined(HOTPLUG_FIRMWARE)
4371 if ((request_firmware(&firmware, oct064_firmware, &wc->dev->dev) != 0) ||
4372 !firmware) {
4373 dev_notice(&wc->dev->dev, "VPM450: firmware %s not "
4374 "available from userspace\n", oct064_firmware);
4375 return;
4376 }
4377 #else
4378 embedded_firmware.data = _binary_dahdi_fw_oct6114_064_bin_start;
4379 /* Yes... this is weird. objcopy gives us a symbol containing
4380 the size of the firmware, not a pointer a variable containing
4381 the size. The only way we can get the value of the symbol
4382 is to take its address, so we define it as a pointer and
4383 then cast that value to the proper type.
4384 */
4385 embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_064_bin_size;
4386 #endif
4387 break;
4388 case 128:
4389 #if defined(HOTPLUG_FIRMWARE)
4390 if ((request_firmware(&firmware, oct128_firmware, &wc->dev->dev) != 0) ||
4391 !firmware) {
4392 dev_notice(&wc->dev->dev, "VPM450: firmware %s not "
4393 "available from userspace\n", oct128_firmware);
4394 return;
4395 }
4396 #else
4397 embedded_firmware.data = _binary_dahdi_fw_oct6114_128_bin_start;
4398 /* Yes... this is weird. objcopy gives us a symbol containing
4399 the size of the firmware, not a pointer a variable containing
4400 the size. The only way we can get the value of the symbol
4401 is to take its address, so we define it as a pointer and
4402 then cast that value to the proper type.
4403 */
4404 embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_128_bin_size;
4405 #endif
4406 break;
4407 case 256:
4408 #if defined(HOTPLUG_FIRMWARE)
4409 if ((request_firmware(&firmware, oct256_firmware, &wc->dev->dev) != 0) ||
4410 !firmware) {
4411 dev_notice(&wc->dev->dev, "VPM450: firmware %s not "
4412 "available from userspace\n", oct256_firmware);
4413 return;
4414 }
4415 #else
4416 embedded_firmware.data = _binary_dahdi_fw_oct6114_256_bin_start;
4417 /* Yes... this is weird. objcopy gives us a symbol containing
4418 the size of the firmware, not a pointer a variable containing
4419 the size. The only way we can get the value of the symbol
4420 is to take its address, so we define it as a pointer and
4421 then cast that value to the proper type.
4422 */
4423 embedded_firmware.size = (size_t) &_binary_dahdi_fw_oct6114_256_bin_size;
4424 #endif
4425 break;
4426 default:
4427 dev_notice(&wc->dev->dev, "Unsupported channel capacity found "
4428 "on VPM module (%d).\n", vpm_capacity);
4429 return;
4430 }
4431
4432 wc->vpm = init_vpm450m(&wc->dev->dev, laws, wc->numspans, firmware);
4433 if (!wc->vpm) {
4434 dev_notice(&wc->dev->dev, "VPM450: Failed to initialize\n");
4435 if (firmware != &embedded_firmware)
4436 release_firmware(firmware);
4437 return;
4438 }
4439
4440 if (firmware != &embedded_firmware)
4441 release_firmware(firmware);
4442
4443 if (vpmdtmfsupport == -1) {
4444 dev_info(&wc->dev->dev, "VPM450: hardware DTMF disabled.\n");
4445 vpmdtmfsupport = 0;
4446 }
4447
4448 dev_info(&wc->dev->dev, "VPM450: Present and operational servicing %d "
4449 "span(s)\n", wc->numspans);
4450
4451 }
4452 #endif /* VPM_SUPPORT */
4453
4454 static void t4_tsi_reset(struct t4 *wc)
4455 {
4456 int x;
4457 if (is_octal(wc)) {
4458 for (x = 0; x < 256; x++) {
4459 wc->dmactrl &= ~0x0001ffff;
4460 wc->dmactrl |= (0x00004000 | ((x & 0x7f) << 7) | ((x >> 7) << 15));
4461 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4462 }
4463 wc->dmactrl &= ~0x0001ffff;
4464 } else {
4465 for (x = 0; x < 128; x++) {
4466 wc->dmactrl &= ~0x00007fff;
4467 wc->dmactrl |= (0x00004000 | (x << 7));
4468 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4469 }
4470 wc->dmactrl &= ~0x00007fff;
4471 }
4472 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4473 }
4474
4475 /* Note that channels here start from 1 */
4476 static void t4_tsi_assign(struct t4 *wc, int fromspan, int fromchan, int tospan, int tochan)
4477 {
4478 unsigned long flags;
4479 int fromts, tots;
4480
4481 fromts = (fromspan << 5) |(fromchan);
4482 tots = (tospan << 5) | (tochan);
4483
4484 if (!has_e1_span(wc)) {
4485 fromts += 4;
4486 tots += 4;
4487 }
4488 spin_lock_irqsave(&wc->reglock, flags);
4489 if (is_octal(wc)) {
4490 int fromts_b = fromts & 0x7f;
4491 int fromts_t = fromts >> 7;
4492 int tots_b = tots & 0x7f;
4493 int tots_t = tots >> 7;
4494
4495 wc->dmactrl &= ~0x0001ffff;
4496 wc->dmactrl |= ((fromts_t << 16) | (tots_t << 15) | 0x00004000 | (tots_b << 7) | (fromts_b));
4497 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4498 wc->dmactrl &= ~0x0001ffff;
4499 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4500 } else {
4501 wc->dmactrl &= ~0x00007fff;
4502 wc->dmactrl |= (0x00004000 | (tots << 7) | (fromts));
4503 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4504 wc->dmactrl &= ~0x00007fff;
4505 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4506 }
4507 spin_unlock_irqrestore(&wc->reglock, flags);
4508 }
4509
4510 static void t4_tsi_unassign(struct t4 *wc, int tospan, int tochan)
4511 {
4512 unsigned long flags;
4513 int tots;
4514
4515 tots = (tospan << 5) | (tochan);
4516
4517 if (!has_e1_span(wc))
4518 tots += 4;
4519 spin_lock_irqsave(&wc->reglock, flags);
4520 if (is_octal(wc)) {
4521 int tots_b = tots & 0x7f;
4522 int tots_t = tots >> 7;
4523
4524 wc->dmactrl &= ~0x0001ffff;
4525 wc->dmactrl |= ((tots_t << 15) | 0x00004000 | (tots_b << 7));
4526 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4527 if (debug & DEBUG_TSI)
4528 dev_notice(&wc->dev->dev, "Sending '%08x\n", wc->dmactrl);
4529 wc->dmactrl &= ~0x0001ffff;
4530 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4531 } else {
4532 wc->dmactrl &= ~0x00007fff;
4533 wc->dmactrl |= (0x00004000 | (tots << 7));
4534 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4535 if (debug & DEBUG_TSI)
4536 dev_notice(&wc->dev->dev, "Sending '%08x\n", wc->dmactrl);
4537 wc->dmactrl &= ~0x00007fff;
4538 __t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4539 }
4540 spin_unlock_irqrestore(&wc->reglock, flags);
4541 }
4542
4543 #ifndef CONFIG_NOEXTENDED_RESET
4544 static void t4_extended_reset(struct t4 *wc)
4545 {
4546 unsigned int oldreg = t4_pci_in(wc, 0x4);
4547
4548 udelay(1000);
4549
4550 t4_pci_out(wc, 0x4, 0x42000000);
4551 t4_pci_out(wc, 0xa, 0x42000000);
4552 t4_pci_out(wc, 0xa, 0x00080000);
4553 t4_pci_out(wc, 0xa, 0x00080000);
4554 t4_pci_out(wc, 0xa, 0x00080000);
4555 t4_pci_out(wc, 0xa, 0x00180000);
4556 t4_pci_out(wc, 0xa, 0x00080000);
4557 t4_pci_out(wc, 0xa, 0x00180000);
4558 t4_pci_out(wc, 0xa, 0x00080000);
4559 t4_pci_out(wc, 0xa, 0x00180000);
4560 t4_pci_out(wc, 0xa, 0x00080000);
4561 t4_pci_out(wc, 0xa, 0x00180000);
4562 t4_pci_out(wc, 0xa, 0x00080000);
4563 t4_pci_out(wc, 0xa, 0x00180000);
4564 t4_pci_out(wc, 0xa, 0x00080000);
4565 t4_pci_out(wc, 0xa, 0x00180000);
4566 t4_pci_out(wc, 0x4, oldreg);
4567
4568 udelay(1000);
4569 }
4570 #endif
4571
4572 #define SPI_CS (0)
4573 #define SPI_CLK (1)
4574 #define SPI_IO0 (2)
4575 #define SPI_IO1 (3)
4576 #define SPI_IO3 (4)
4577 #define SPI_IO2 (5)
4578 #define SPI_IN (0)
4579 #define SPI_OUT (1)
4580 #define ESPI_REG 13
4581
4582 static void t8_clear_bit(struct t4 *wc, int whichb)
4583 {
4584 wc->st.wrreg &= ~(1 << whichb);
4585 }
4586
4587 static void t8_set_bit(struct t4 *wc, int whichb, int val)
4588 {
4589 t8_clear_bit(wc, whichb);
4590 wc->st.wrreg |= (val << whichb);
4591 }
4592
4593 static int t8_get_bit(struct t4 *wc, int whichb)
4594 {
4595 return (wc->st.rdreg >> whichb) & 1;
4596 }
4597
4598 static void set_iodir(struct t4 *wc, int whichb, int dir)
4599 {
4600 whichb += 16;
4601 t8_clear_bit(wc, whichb);
4602 t8_set_bit(wc, whichb, dir);
4603 }
4604
4605 static void write_hwreg(struct t4 *wc)
4606 {
4607 t4_pci_out(wc, ESPI_REG, wc->st.wrreg);
4608 }
4609
4610 static void read_hwreg(struct t4 *wc)
4611 {
4612 wc->st.rdreg = t4_pci_in(wc, ESPI_REG);
4613 }
4614
4615 static void set_cs(struct t4 *wc, int state)
4616 {
4617 t8_set_bit(wc, SPI_CS, state);
4618 write_hwreg(wc);
4619 }
4620
4621 static void set_clk(struct t4 *wc, int clk)
4622 {
4623 t8_set_bit(wc, SPI_CLK, clk);
4624 write_hwreg(wc);
4625 }
4626
4627 static void clk_bit_out(struct t4 *wc, int val)
4628 {
4629 t8_set_bit(wc, SPI_IO0, val & 1);
4630 set_clk(wc, 0);
4631 set_clk(wc, 1);
4632 }
4633
4634 static void shift_out(struct t4 *wc, int val)
4635 {
4636 int i;
4637 for (i = 7; i >= 0; i--)
4638 clk_bit_out(wc, (val >> i) & 1);
4639 }
4640
4641 static int clk_bit_in(struct t4 *wc)
4642 {
4643 int ret;
4644 set_clk(wc, 0);
4645 read_hwreg(wc);
4646 ret = t8_get_bit(wc, SPI_IO1);
4647 set_clk(wc, 1);
4648 return ret;
4649 }
4650
4651 static int shift_in(struct t4 *wc)
4652 {
4653 int ret = 0;
4654 int i;
4655 int bit;
4656
4657 for (i = 7; i >= 0; i--) {
4658 bit = clk_bit_in(wc);
4659 ret |= ((bit & 1) << i);
4660 }
4661 return ret;
4662 }
4663
4664 static void write_enable(struct t4 *wc)
4665 {
4666 int cmd = 0x06;
4667 set_cs(wc, 0);
4668 shift_out(wc, cmd);
4669 set_cs(wc, 1);
4670 }
4671
4672 static int read_sr1(struct t4 *wc)
4673 {
4674 int cmd = 0x05;
4675 int ret;
4676 set_cs(wc, 0);
4677 shift_out(wc, cmd);
4678 ret = shift_in(wc);
4679 set_cs(wc, 1);
4680 return ret;
4681 }
4682
4683 static void clear_busy(struct t4 *wc)
4684 {
4685 static const int SR1_BUSY = (1 << 0);
4686 unsigned long stop;
4687
4688 stop = jiffies + 2*HZ;
4689 while (read_sr1(wc) & SR1_BUSY) {
4690 if (time_after(jiffies, stop)) {
4691 if (printk_ratelimit()) {
4692 dev_err(&wc->dev->dev,
4693 "Lockup in %s\n", __func__);
4694 }
4695 break;
4696 }
4697 cond_resched();
4698 }
4699 }
4700
4701 static void sector_erase(struct t4 *wc, uint32_t addr)
4702 {
4703 int cmd = 0x20;
4704 write_enable(wc);
4705 set_cs(wc, 0);
4706 shift_out(wc, cmd);
4707 shift_out(wc, (addr >> 16) & 0xff);
4708 shift_out(wc, (addr >> 8) & 0xff);
4709 shift_out(wc, (addr >> 0) & 0xff);
4710 set_cs(wc, 1);
4711 clear_busy(wc);
4712 }
4713
4714 static void erase_half(struct t4 *wc)
4715 {
4716 uint32_t addr = 0x00080000;
4717 uint32_t i;
4718
4719 dev_info(&wc->dev->dev, "Erasing octal firmware\n");
4720
4721 for (i = addr; i < (addr + 0x80000); i += 4096)
4722 sector_erase(wc, i);
4723 }
4724
4725
4726 #define T8_FLASH_PAGE_SIZE 256UL
4727
4728 static void t8_update_firmware_page(struct t4 *wc, u32 address,
4729 const u8 *page_data, size_t size)
4730 {
4731 int i;
4732
4733 write_enable(wc);
4734 set_cs(wc, 0);
4735 shift_out(wc, 0x02);
4736 shift_out(wc, (address >> 16) & 0xff);
4737 shift_out(wc, (address >> 8) & 0xff);
4738 shift_out(wc, (address >> 0) & 0xff);
4739
4740 for (i = 0; i < size; ++i)
4741 shift_out(wc, page_data[i]);
4742
4743 set_cs(wc, 1);
4744 clear_busy(wc);
4745 }
4746
4747 static int t8_update_firmware(struct t4 *wc, const struct firmware *fw,
4748 const char *t8_firmware)
4749 {
4750 int res;
4751 size_t offset = 0;
4752 const u32 BASE_ADDRESS = 0x00080000;
4753 const u8 *data, *end;
4754 size_t size = 0;
4755
4756 /* Erase flash */
4757 erase_half(wc);
4758
4759 dev_info(&wc->dev->dev,
4760 "Uploading %s. This can take up to 30 seconds.\n", t8_firmware);
4761
4762 data = &fw->data[sizeof(struct t8_firm_header)];
4763 end = &fw->data[fw->size];
4764
4765 while (data < end) {
4766 /* Calculate the tail end of data that's shorter than a page */
4767 size = min(T8_FLASH_PAGE_SIZE, (unsigned long)(end - data));
4768
4769 t8_update_firmware_page(wc, BASE_ADDRESS + offset,
4770 data, size);
4771 data += T8_FLASH_PAGE_SIZE;
4772 offset += T8_FLASH_PAGE_SIZE;
4773
4774 cond_resched();
4775 }
4776
4777 /* Reset te820 fpga after loading firmware */
4778 dev_info(&wc->dev->dev, "Firmware load complete. Reseting device.\n");
4779 res = pci_save_state(wc->dev);
4780 if (res)
4781 goto error_exit;
4782 /* Set the fpga reset bits and clobber the remainder of the
4783 * register, device will be reset anyway */
4784 t4_pci_out(wc, WC_LEDS, 0xe0000000);
4785 msleep(1000);
4786
4787 pci_restore_state(wc->dev);
4788
4789 /* Signal the driver to restart initialization.
4790 * This will back out all initialization so far and
4791 * restart the driver load process */
4792 return -EAGAIN;
4793
4794 error_exit:
4795 return res;
4796 }
4797
4798 static char read_flash_byte(struct t4 *wc, unsigned int addr)
4799 {
4800 int cmd = 0x03;
4801 char data;
4802
4803 set_cs(wc, 0);
4804
4805 shift_out(wc, cmd);
4806
4807 shift_out(wc, (addr >> 16) & 0xff);
4808 shift_out(wc, (addr >> 8) & 0xff);
4809 shift_out(wc, (addr >> 0) & 0xff);
4810
4811 data = shift_in(wc) & 0xff;
4812
4813 set_cs(wc, 1);
4814
4815 return data;
4816 }
4817
4818 /**
4819 * t8_read_serial - Returns the serial number of the board.
4820 * @wc: The board whos serial number we are reading.
4821 *
4822 * The buffer returned is dynamically allocated and must be kfree'd by the
4823 * caller. If memory could not be allocated, NULL is returned.
4824 *
4825 * Must be called in process context.
4826 *
4827 */
4828 static char *t8_read_serial(struct t4 *wc)
4829 {
4830 int i;
4831 static const int MAX_SERIAL = 14;
4832 static const u32 base_addr = 0x00080000-256;
4833 unsigned char c;
4834 unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
4835
4836 if (!serial)
4837 return NULL;
4838
4839 for (i = 0; i < MAX_SERIAL; ++i) {
4840 c = read_flash_byte(wc, base_addr+i);
4841 if ((c >= 'a' && c <= 'z') ||
4842 (c >= 'A' && c <= 'Z') ||
4843 (c >= '0' && c <= '9'))
4844 serial[i] = c;
4845 else
4846 break;
4847
4848 }
4849
4850 if (!i) {
4851 kfree(serial);
4852 serial = NULL;
4853 }
4854
4855 return serial;
4856 }
4857
4858 static void setup_spi(struct t4 *wc)
4859 {
4860 wc->st.rdreg = wc->st.wrreg = 0;
4861
4862 set_iodir(wc, SPI_IO0, SPI_OUT);
4863 set_iodir(wc, SPI_IO1, SPI_IN);
4864 set_iodir(wc, SPI_CS, SPI_OUT);
4865 set_iodir(wc, SPI_CLK, SPI_OUT);
4866
4867 t8_set_bit(wc, SPI_CS, 1);
4868 t8_set_bit(wc, SPI_CLK, 1);
4869
4870 write_hwreg(wc);
4871 }
4872
4873 static int t8_check_firmware(struct t4 *wc, unsigned int version)
4874 {
4875 const struct firmware *fw;
4876 static const char t8_firmware[] = "dahdi-fw-te820.bin";
4877 const struct t8_firm_header *header;
4878 int res = 0;
4879 u32 crc;
4880
4881 res = request_firmware(&fw, t8_firmware, &wc->dev->dev);
4882 if (res) {
4883 dev_info(&wc->dev->dev, "firmware %s not "
4884 "available from userspace\n", t8_firmware);
4885 goto cleanup;
4886 }
4887
4888 header = (const struct t8_firm_header *)fw->data;
4889
4890 /* Check the crc before anything else */
4891 crc = crc32(~0, &fw->data[10], fw->size - 10) ^ ~0;
4892 if (memcmp("DIGIUM", header->header, sizeof(header->header)) ||
4893 (le32_to_cpu(header->chksum) != crc)) {
4894 dev_info(&wc->dev->dev,
4895 "%s is invalid. Please reinstall.\n", t8_firmware);
4896 goto cleanup;
4897 }
4898
4899 /* Spi struct must be setup before any access to flash memory */
4900 setup_spi(wc);
4901
4902 /* Check the two firmware versions */
4903 if (le32_to_cpu(header->version) == version)
4904 goto cleanup;
4905
4906 dev_info(&wc->dev->dev, "%s Version: %08x available for flash\n",
4907 t8_firmware, header->version);
4908
4909 res = t8_update_firmware(wc, fw, t8_firmware);
4910 if (res && res != -EAGAIN) {
4911 dev_info(&wc->dev->dev, "Failed to load firmware %s\n",
4912 t8_firmware);
4913 }
4914
4915 cleanup:
4916 release_firmware(fw);
4917 return res;
4918 }
4919
4920 static int
4921 __t4_hardware_init_1(struct t4 *wc, unsigned int cardflags, bool first_time)
4922 {
4923 unsigned int version;
4924 int res;
4925
4926 version = t4_pci_in(wc, WC_VERSION);
4927 if (is_octal(wc) && first_time) {
4928 dev_info(&wc->dev->dev, "Firmware Version: %01x.%02x\n",
4929 (version & 0xf00) >> 8,
4930 version & 0xff);
4931 } else if (first_time) {
4932 dev_info(&wc->dev->dev, "Firmware Version: %08x\n", version);
4933 }
4934 if (debug) {
4935 dev_info(&wc->dev->dev, "Burst Mode: %s\n",
4936 (!(cardflags & FLAG_BURST) && noburst) ? "Off" : "On");
4937 #ifdef ENABLE_WORKQUEUES
4938 dev_info(&wc->dev->dev, "Work Queues: Enabled\n");
4939 #endif
4940 }
4941
4942 /* Check the field updatable firmware for the wcte820 */
4943 if (is_octal(wc)) {
4944 res = t8_check_firmware(wc, version);
4945 if (res)
4946 return res;
4947
4948 wc->ddev->hardware_id = t8_read_serial(wc);
4949 }
4950
4951 #if defined(CONFIG_FORCE_EXTENDED_RESET)
4952 t4_extended_reset(wc);
4953 #elif !defined(CONFIG_NOEXTENDED_RESET)
4954 if (wc->devtype->flags & FLAG_EXPRESS)
4955 t4_extended_reset(wc);
4956 #endif
4957
4958 /* Make sure DMA engine is not running and interrupts are acknowledged */
4959 wc->dmactrl = 0x0;
4960 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
4961 /* Reset Framer and friends */
4962 t4_pci_out(wc, WC_LEDS, 0x00000000);
4963
4964 /* Set DMA addresses */
4965 t4_pci_out(wc, WC_RDADDR, wc->readdma);
4966 t4_pci_out(wc, WC_WRADDR, wc->writedma);
4967
4968 /* Setup counters, interrupt flags (ignored in Gen2) */
4969 if (cardflags & FLAG_2NDGEN) {
4970 t4_tsi_reset(wc);
4971 } else {
4972 t4_pci_out(wc, WC_COUNT, ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 18) | ((DAHDI_MAX_CHUNKSIZE * 2 * 32 - 1) << 2));
4973 }
4974
4975 /* Reset pending interrupts */
4976 t4_pci_out(wc, WC_INTR, 0x00000000);
4977
4978 /* Read T1/E1 status */
4979 if (first_time) {
4980 if (!strcasecmp("auto", default_linemode)) {
4981 if (-1 == t1e1override) {
4982 wc->t1e1 = (((t4_pci_in(wc, WC_LEDS)) &
4983 0x0f00) >> 8);
4984 wc->t1e1 &= 0xf;
4985 if (is_octal(wc)) {
4986 wc->t1e1 |= ((t4_pci_in(wc, WC_LEDS2)) &
4987 0x0f00) >> 4;
4988 }
4989 } else {
4990 dev_warn(&wc->dev->dev,
4991 "'t1e1override' is deprecated. Please use 'default_linemode'.\n");
4992 wc->t1e1 = t1e1override & 0xf;
4993 }
4994 } else if (!strcasecmp("t1", default_linemode)) {
4995 wc->t1e1 = 0;
4996 } else if (!strcasecmp("e1", default_linemode)) {
4997 wc->t1e1 = 0xff;
4998 } else if (!strcasecmp("j1", default_linemode)) {
4999 wc->t1e1 = 0;
5000 j1mode = 1;
5001 } else {
5002 dev_err(&wc->dev->dev, "'%s' is an unknown linemode.\n",
5003 default_linemode);
5004 wc->t1e1 = 0;
5005 }
5006 }
5007
5008 wc->order = ((t4_pci_in(wc, WC_LEDS)) & 0xf0000000) >> 28;
5009 order_index[wc->order]++;
5010
5011 /* TE820 Auth Check */
5012 if (is_octal(wc)) {
5013 unsigned long stop = jiffies + HZ;
5014 uint32_t donebit;
5015
5016 t4_pci_out(wc, WC_LEDS2, WC_SET_AUTH);
5017 donebit = t4_pci_in(wc, WC_LEDS2);
5018 while (!(donebit & WC_GET_AUTH)) {
5019 if (time_after(jiffies, stop)) {
5020 /* Encryption check failed, stop operation */
5021 dev_info(&wc->dev->dev,
5022 "Failed encryption check. "
5023 "Unloading driver.\n");
5024 return -EIO;
5025 }
5026 msleep(20);
5027 donebit = t4_pci_in(wc, WC_LEDS2);
5028 }
5029 }
5030
5031 return 0;
5032 }
5033
5034 static int t4_hardware_init_1(struct t4 *wc, unsigned int cardflags)
5035 {
5036 return __t4_hardware_init_1(wc, cardflags, true);
5037 }
5038
5039 static int __t4_hardware_init_2(struct t4 *wc, bool first_time)
5040 {
5041 int x;
5042 unsigned int regval;
5043 unsigned long flags;
5044
5045 if (t4_pci_in(wc, WC_VERSION) >= 0xc01a0165) {
5046 wc->tspans[0]->spanflags |= FLAG_OCTOPT;
5047 }
5048 /* Setup LEDS, take out of reset */
5049 t4_pci_out(wc, WC_LEDS, 0x000000ff);
5050 udelay(100);
5051 t4_activate(wc);
5052 udelay(100);
5053
5054 /* In order to find out the QFALC framer version, we have to
5055 * temporarily turn off compat mode and take a peak at VSTR. We turn
5056 * compat back on when we are done.
5057 *
5058 */
5059 spin_lock_irqsave(&wc->reglock, flags);
5060 regval = __t4_framer_in(wc, 0, 0xd6);
5061 if (is_octal(wc))
5062 regval |= (1 << 4); /* SSI16 - For 16 MHz multiplex mode with comp = 1 */
5063 else
5064 regval |= (1 << 5); /* set COMP_DIS*/
5065
5066 __t4_framer_out(wc, 0, 0xd6, regval);
5067 spin_unlock_irqrestore(&wc->reglock, flags);
5068
5069 if (!is_octal(wc)) {
5070 regval = t4_framer_in(wc, 0, 0x4a);
5071 if (first_time && regval == 0x05) {
5072 dev_info(&wc->dev->dev, "FALC Framer Version: 2.1 or "
5073 "earlier\n");
5074 } else if (regval == 0x20) {
5075 if (first_time)
5076 dev_info(&wc->dev->dev, "FALC Framer Version: 3.1\n");
5077 wc->falc31 = 1;
5078 } else if (first_time) {
5079 dev_info(&wc->dev->dev, "FALC Framer Version: Unknown "
5080 "(VSTR = 0x%02x)\n", regval);
5081 }
5082 }
5083
5084 spin_lock_irqsave(&wc->reglock, flags);
5085 regval = __t4_framer_in(wc, 0, 0xd6);
5086 regval &= ~(1 << 5); /* clear COMP_DIS*/
5087 __t4_framer_out(wc, 0, 0xd6, regval);
5088 __t4_framer_out(wc, 0, 0x4a, 0xaa);
5089 spin_unlock_irqrestore(&wc->reglock, flags);
5090
5091 if (debug) {
5092 dev_info(&wc->dev->dev, "Board ID: %02x\n", wc->order);
5093 for (x = 0; x < 11; x++) {
5094 dev_info(&wc->dev->dev, "Reg %d: 0x%08x\n", x,
5095 t4_pci_in(wc, x));
5096 }
5097 }
5098
5099 wc->gpio = 0x00000000;
5100 t4_pci_out(wc, WC_GPIO, wc->gpio);
5101 t4_gpio_setdir(wc, (1 << 17), (1 << 17));
5102 t4_gpio_setdir(wc, (0xff), (0xff));
5103
5104 return 0;
5105 }
5106
5107 static int t4_hardware_init_2(struct t4 *wc)
5108 {
5109 return __t4_hardware_init_2(wc, true);
5110 }
5111
5112 static int __devinit t4_launch(struct t4 *wc)
5113 {
5114 int x;
5115 int res;
5116
5117 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &wc->tspans[0]->span.flags))
5118 return 0;
5119
5120 if (debug) {
5121 dev_info(&wc->dev->dev,
5122 "TE%dXXP: Launching card: %d\n", wc->numspans,
5123 wc->order);
5124 }
5125
5126 wc->ddev->manufacturer = "Digium";
5127 if (!ignore_rotary && (1 == order_index[wc->order])) {
5128 wc->ddev->location = kasprintf(GFP_KERNEL,
5129 "Board ID Switch %d", wc->order);
5130 } else {
5131 bool express = ((wc->tspans[0]->spanflags & FLAG_EXPRESS) > 0);
5132 wc->ddev->location = kasprintf(GFP_KERNEL,
5133 "PCI%s Bus %02d Slot %02d",
5134 (express) ? " Express" : "",
5135 wc->dev->bus->number,
5136 PCI_SLOT(wc->dev->devfn) + 1);
5137 }
5138
5139 if (!wc->ddev->location)
5140 return -ENOMEM;
5141
5142 for (x = 0; x < wc->numspans; ++x) {
5143 list_add_tail(&wc->tspans[x]->span.device_node,
5144 &wc->ddev->spans);
5145 }
5146
5147 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
5148 INIT_WORK(&wc->bh_work, t4_work_func, wc);
5149 #else
5150 INIT_WORK(&wc->bh_work, t4_work_func);
5151 #endif
5152
5153 res = dahdi_register_device(wc->ddev, &wc->dev->dev);
5154 if (res) {
5155 dev_err(&wc->dev->dev, "Failed to register with DAHDI.\n");
5156 return res;
5157 }
5158
5159 return 0;
5160 }
5161
5162 /**
5163 * wct4xxp_sort_cards - Sort the cards in card array by rotary switch settings.
5164 *
5165 */
5166 static void wct4xxp_sort_cards(void)
5167 {
5168 int x;
5169
5170 /* get the current number of probed cards and run a slice of a tail
5171 * insertion sort */
5172 for (x = 0; x < MAX_T4_CARDS; x++) {
5173 if (!cards[x+1])
5174 break;
5175 }
5176 for ( ; x > 0; x--) {
5177 if (cards[x]->order < cards[x-1]->order) {
5178 struct t4 *tmp = cards[x];
5179 cards[x] = cards[x-1];
5180 cards[x-1] = tmp;
5181 } else {
5182 /* if we're not moving it, we won't move any more
5183 * since all cards are sorted on addition */
5184 break;
5185 }
5186 }
5187 }
5188
5189 static int __devinit
5190 t4_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5191 {
5192 int res;
5193 struct t4 *wc;
5194 unsigned int x;
5195 int init_latency;
5196
5197 if (pci_enable_device(pdev)) {
5198 return -EIO;
5199 }
5200
5201 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
5202 if (!wc)
5203 return -ENOMEM;
5204
5205 wc->ddev = dahdi_create_device();
5206 if (!wc->ddev) {
5207 kfree(wc);
5208 return -ENOMEM;
5209 }
5210
5211 spin_lock_init(&wc->reglock);
5212 wc->devtype = (const struct devtype *)(ent->driver_data);
5213
5214 #ifdef CONFIG_WCT4XXP_DISABLE_ASPM
5215 if (is_pcie(wc)) {
5216 pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
5217 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5218 };
5219 #endif
5220
5221 if (is_octal(wc))
5222 wc->numspans = 8;
5223 else if (wc->devtype->flags & FLAG_2PORT)
5224 wc->numspans = 2;
5225 else
5226 wc->numspans = 4;
5227
5228 wc->membase = pci_iomap(pdev, 0, 0);
5229 /* This rids of the Double missed interrupt message after loading */
5230 wc->last0 = 1;
5231 if (pci_request_regions(pdev, wc->devtype->desc))
5232 dev_info(&pdev->dev, "wct%dxxp: Unable to request regions\n",
5233 wc->numspans);
5234
5235 if (debug)
5236 dev_info(&pdev->dev, "Found TE%dXXP\n", wc->numspans);
5237
5238 wc->dev = pdev;
5239
5240 /* Enable bus mastering */
5241 pci_set_master(pdev);
5242
5243 /* Keep track of which device we are */
5244 pci_set_drvdata(pdev, wc);
5245
5246 if (wc->devtype->flags & FLAG_5THGEN) {
5247 if ((ms_per_irq > 1) && (latency <= ((ms_per_irq) << 1))) {
5248 init_latency = ms_per_irq << 1;
5249 } else {
5250 if (latency > 2)
5251 init_latency = latency;
5252 else
5253 init_latency = 2;
5254 }
5255 dev_info(&wc->dev->dev, "5th gen card with initial latency of "
5256 "%d and %d ms per IRQ\n", init_latency, ms_per_irq);
5257 } else {
5258 if (wc->devtype->flags & FLAG_2NDGEN)
5259 init_latency = 1;
5260 else
5261 init_latency = 2;
5262 }
5263
5264 if (max_latency < init_latency) {
5265 printk(KERN_INFO "maxlatency must be set to something greater than %d ms, increasing it to %d\n", init_latency, init_latency);
5266 max_latency = init_latency;
5267 }
5268
5269 if (t4_allocate_buffers(wc, init_latency, NULL, NULL)) {
5270 return -ENOMEM;
5271 }
5272
5273 /* Initialize hardware */
5274 res = t4_hardware_init_1(wc, wc->devtype->flags);
5275 if (res) {
5276 /* If this function returns -EAGAIN, we expect
5277 * to attempt another driver load. Clean everything
5278 * up first */
5279 pci_iounmap(wc->dev, wc->membase);
5280 pci_release_regions(wc->dev);
5281 pci_free_consistent(wc->dev, T4_BASE_SIZE(wc) * wc->numbufs * 2,
5282 wc->writechunk, wc->writedma);
5283 pci_set_drvdata(wc->dev, NULL);
5284 free_wc(wc);
5285 return res;
5286 }
5287
5288 for(x = 0; x < MAX_T4_CARDS; x++) {
5289 if (!cards[x])
5290 break;
5291 }
5292
5293 if (x >= MAX_T4_CARDS) {
5294 dev_notice(&wc->dev->dev, "No cards[] slot available!!\n");
5295 kfree(wc);
5296 return -ENOMEM;
5297 }
5298
5299 wc->num = x;
5300 cards[x] = wc;
5301
5302 #ifdef ENABLE_WORKQUEUES
5303 if (wc->devtype->flags & FLAG_2NDGEN) {
5304 char tmp[20];
5305
5306 sprintf(tmp, "te%dxxp[%d]", wc->numspans, wc->num);
5307 wc->workq = create_workqueue(tmp);
5308 }
5309 #endif
5310
5311 /* Allocate pieces we need here */
5312 for (x = 0; x < ports_on_framer(wc); x++) {
5313 struct t4_span *ts;
5314 enum linemode linemode;
5315
5316 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
5317 if (!ts) {
5318 free_wc(wc);
5319 return -ENOMEM;
5320 }
5321 wc->tspans[x] = ts;
5322
5323 #ifdef ENABLE_WORKQUEUES
5324 INIT_WORK(&ts->swork, workq_handlespan, ts);
5325 #endif
5326 ts->spanflags |= wc->devtype->flags;
5327 linemode = (wc->t1e1 & (1 << x)) ? E1 : ((j1mode) ? J1 : T1);
5328 t4_alloc_channels(wc, wc->tspans[x], linemode);
5329 }
5330
5331 /* Continue hardware intiialization */
5332 t4_hardware_init_2(wc);
5333
5334 #ifdef SUPPORT_GEN1
5335 if (request_irq(pdev->irq, (wc->devtype->flags & FLAG_2NDGEN) ?
5336 t4_interrupt_gen2 : t4_interrupt,
5337 IRQF_SHARED,
5338 (wc->numspans == 8) ? "wct8xxp" :
5339 (wc->numspans == 2) ? "wct2xxp" :
5340 "wct4xxp",
5341 wc)) {
5342 #else
5343 if (!(wc->tspans[0]->spanflags & FLAG_2NDGEN)) {
5344 dev_notice(&wc->dev->dev, "This driver does not "
5345 "support 1st gen modules\n");
5346 free_wc(wc);
5347 return -ENODEV;
5348 }
5349
5350 if (request_irq(pdev->irq, t4_interrupt_gen2,
5351 IRQF_SHARED, "t4xxp", wc)) {
5352 #endif
5353 dev_notice(&wc->dev->dev, "Unable to request IRQ %d\n",
5354 pdev->irq);
5355 free_wc(wc);
5356 return -EIO;
5357 }
5358
5359 init_spans(wc);
5360
5361 if (!ignore_rotary)
5362 wct4xxp_sort_cards();
5363
5364 if (wc->ddev->hardware_id) {
5365 dev_info(&wc->dev->dev,
5366 "Found a Wildcard: %s (SN: %s)\n", wc->devtype->desc,
5367 wc->ddev->hardware_id);
5368 } else {
5369 dev_info(&wc->dev->dev,
5370 "Found a Wildcard: %s\n", wc->devtype->desc);
5371 }
5372
5373 #ifdef VPM_SUPPORT
5374 if (!wc->vpm) {
5375 t4_vpm_init(wc);
5376 wc->dmactrl |= (wc->vpm) ? T4_VPM_PRESENT : 0;
5377 t4_pci_out(wc, WC_DMACTRL, wc->dmactrl);
5378 if (wc->vpm)
5379 set_span_devicetype(wc);
5380 }
5381 #endif
5382
5383 create_sysfs_files(wc);
5384
5385 res = 0;
5386 if (ignore_rotary)
5387 res = t4_launch(wc);
5388
5389 return res;
5390 }
5391
5392 static int t4_hardware_stop(struct t4 *wc)
5393 {
5394
5395 /* Turn off DMA, leave interrupts enabled */
5396 set_bit(T4_STOP_DMA, &wc->checkflag);
5397
5398 /* Wait for interrupts to stop */
5399 msleep(25);
5400
5401 /* Turn off counter, address, etc */
5402 if (wc->tspans[0]->spanflags & FLAG_2NDGEN) {
5403 t4_tsi_reset(wc);
5404 } else {
5405 t4_pci_out(wc, WC_COUNT, 0x000000);
5406 }
5407 t4_pci_out(wc, WC_RDADDR, 0x0000000);
5408 t4_pci_out(wc, WC_WRADDR, 0x0000000);
5409 wc->gpio = 0x00000000;
5410 t4_pci_out(wc, WC_GPIO, wc->gpio);
5411 t4_pci_out(wc, WC_LEDS, 0x00000000);
5412
5413 if (debug) {
5414 dev_notice(&wc->dev->dev, "Stopped TE%dXXP, Turned off DMA\n",
5415 wc->numspans);
5416 }
5417 return 0;
5418 }
5419
5420 static int __devinit
5421 t4_init_one_retry(struct pci_dev *pdev, const struct pci_device_id *ent)
5422 {
5423 int res;
5424 res = t4_init_one(pdev, ent);
5425
5426 /* If the driver was reset by a firmware load,
5427 * try to load once again */
5428 if (-EAGAIN == res) {
5429 res = t4_init_one(pdev, ent);
5430 if (-EAGAIN == res) {
5431 dev_err(&pdev->dev, "Failed to update firmware.\n");
5432 res = -EIO;
5433 }
5434 }
5435
5436 return res;
5437 }
5438
5439 static void _t4_remove_one(struct t4 *wc)
5440 {
5441 int basesize;
5442
5443 if (!wc)
5444 return;
5445
5446 dahdi_unregister_device(wc->ddev);
5447
5448 remove_sysfs_files(wc);
5449
5450 /* Stop hardware */
5451 t4_hardware_stop(wc);
5452
5453 #ifdef VPM_SUPPORT
5454 /* Release vpm */
5455 if (wc->vpm)
5456 release_vpm450m(wc->vpm);
5457 wc->vpm = NULL;
5458 #endif
5459 /* Unregister spans */
5460
5461 basesize = DAHDI_MAX_CHUNKSIZE * 32 * 4;
5462 if (!(wc->tspans[0]->spanflags & FLAG_2NDGEN))
5463 basesize = basesize * 2;
5464
5465 #ifdef ENABLE_WORKQUEUES
5466 if (wc->workq) {
5467 flush_workqueue(wc->workq);
5468 destroy_workqueue(wc->workq);
5469 }
5470 #endif
5471
5472 free_irq(wc->dev->irq, wc);
5473
5474 if (wc->membase)
5475 pci_iounmap(wc->dev, wc->membase);
5476
5477 pci_release_regions(wc->dev);
5478
5479 /* Immediately free resources */
5480 pci_free_consistent(wc->dev, T4_BASE_SIZE(wc) * wc->numbufs * 2,
5481 wc->writechunk, wc->writedma);
5482
5483 order_index[wc->order]--;
5484
5485 cards[wc->num] = NULL;
5486 pci_set_drvdata(wc->dev, NULL);
5487 free_wc(wc);
5488 }
5489
5490 static void __devexit t4_remove_one(struct pci_dev *pdev)
5491 {
5492 struct t4 *wc = pci_get_drvdata(pdev);
5493 if (!wc)
5494 return;
5495
5496 _t4_remove_one(wc);
5497 }
5498
5499
5500 static DEFINE_PCI_DEVICE_TABLE(t4_pci_tbl) =
5501 {
5502 { 0x10ee, 0x0314, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct4xxp },
5503
5504 { 0xd161, 0x1820, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct820p5 },
5505 { 0xd161, 0x1420, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct420p5 },
5506 { 0xd161, 0x1410, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p5 },
5507 { 0xd161, 0x1405, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p5 },
5508 { 0xd161, 0x0420, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct420p4 },
5509 { 0xd161, 0x0410, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p4 },
5510 { 0xd161, 0x0405, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p4 },
5511 { 0xd161, 0x0410, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p3 },
5512 { 0xd161, 0x0405, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p3 },
5513 { 0xd161, 0x0410, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct410p2 },
5514 { 0xd161, 0x0405, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct405p2 },
5515
5516 { 0xd161, 0x1220, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct220p5 },
5517 { 0xd161, 0x1205, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct205p5 },
5518 { 0xd161, 0x1210, 0x0005, PCI_ANY_ID, 0, 0, (unsigned long)&wct210p5 },
5519 { 0xd161, 0x0220, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct220p4 },
5520 { 0xd161, 0x0205, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct205p4 },
5521 { 0xd161, 0x0210, 0x0004, PCI_ANY_ID, 0, 0, (unsigned long)&wct210p4 },
5522 { 0xd161, 0x0205, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct205p3 },
5523 { 0xd161, 0x0210, 0x0003, PCI_ANY_ID, 0, 0, (unsigned long)&wct210p3 },
5524 { 0xd161, 0x0205, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct205 },
5525 { 0xd161, 0x0210, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long)&wct210 },
5526 { 0, }
5527 };
5528
5529 static void _t4_shutdown(struct pci_dev *pdev)
5530 {
5531 struct t4 *wc = pci_get_drvdata(pdev);
5532 t4_hardware_stop(wc);
5533 }
5534
5535 static int t4_suspend(struct pci_dev *pdev, pm_message_t state)
5536 {
5537 return -ENOSYS;
5538 }
5539
5540 static struct pci_driver t4_driver = {
5541 .name = "wct4xxp",
5542 .probe = t4_init_one_retry,
5543 .remove = __devexit_p(t4_remove_one),
5544 .shutdown = _t4_shutdown,
5545 .suspend = t4_suspend,
5546 .id_table = t4_pci_tbl,
5547 };
5548
5549 static int __init t4_init(void)
5550 {
5551 int i;
5552 int res;
5553
5554 if (-1 != t1e1override) {
5555 pr_info("'t1e1override' module parameter is deprecated. "
5556 "Please use 'default_linemode' instead.\n");
5557 }
5558
5559 res = dahdi_pci_module(&t4_driver);
5560 if (res)
5561 return -ENODEV;
5562
5563 /* If we're ignoring the rotary switch settings, then we've already
5564 * registered in the context of .probe */
5565 if (!ignore_rotary) {
5566
5567 /* Initialize cards since we have all of them. Warn for
5568 * missing zero and duplicate numbers. */
5569
5570 if (cards[0] && cards[0]->order != 0) {
5571 printk(KERN_NOTICE "wct4xxp: Ident of first card is not zero (%d)\n",
5572 cards[0]->order);
5573 }
5574
5575 for (i = 0; cards[i]; i++) {
5576 /* warn the user of duplicate ident values it is
5577 * probably unintended */
5578 if (debug && res < 15 && cards[i+1] &&
5579 cards[res]->order == cards[i+1]->order) {
5580 printk(KERN_NOTICE "wct4xxp: Duplicate ident "
5581 "value found (%d)\n", cards[i]->order);
5582 }
5583 res = t4_launch(cards[i]);
5584 if (res) {
5585 int j;
5586 for (j = 0; j < i; ++j)
5587 _t4_remove_one(cards[j]);
5588 break;
5589 }
5590 }
5591 }
5592 return res;
5593 }
5594
5595 static void __exit t4_cleanup(void)
5596 {
5597 pci_unregister_driver(&t4_driver);
5598 }
5599
5600 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
5601 MODULE_DESCRIPTION("Wildcard Dual/Quad/Octal-port Digital Card Driver");
5602 MODULE_ALIAS("wct2xxp");
5603 MODULE_LICENSE("GPL v2");
5604
5605 module_param(debug, int, 0600);
5606 module_param(noburst, int, 0600);
5607 module_param(timingcable, int, 0600);
5608 module_param(t1e1override, int, 0400);
5609 module_param(default_linemode, charp, S_IRUGO);
5610 MODULE_PARM_DESC(default_linemode, "\"auto\"(default), \"e1\", \"t1\", "
5611 "or \"j1\". \"auto\" will use the value from any hardware "
5612 "jumpers.");
5613 module_param(alarmdebounce, int, 0600);
5614 module_param(losalarmdebounce, int, 0600);
5615 module_param(aisalarmdebounce, int, 0600);
5616 module_param(yelalarmdebounce, int, 0600);
5617 module_param(max_latency, int, 0600);
5618 module_param(j1mode, int, 0600);
5619 module_param(sigmode, int, 0600);
5620 module_param(latency, int, 0600);
5621 module_param(ms_per_irq, int, 0600);
5622 module_param(ignore_rotary, int, 0400);
5623 MODULE_PARM_DESC(ignore_rotary, "Set to > 0 to ignore the rotary switch when " \
5624 "registering with DAHDI.");
5625
5626 #ifdef VPM_SUPPORT
5627 module_param(vpmsupport, int, 0600);
5628 module_param(vpmdtmfsupport, int, 0600);
5629 #endif
5630
5631 MODULE_DEVICE_TABLE(pci, t4_pci_tbl);
5632
5633 module_init(t4_init);
5634 module_exit(t4_cleanup);