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