Debian Codegraph @

drivers/dahdi/wcte13xp-base.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Digium, Inc. Wildcard te13xp T1/E1 card Driver
3 *
4 * Written by Russ Meyerriecks <[email protected]>
5 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * All rights reserved.
7 *
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/proc_fs.h>
31 #include <linux/moduleparam.h>
32 #include <linux/interrupt.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <linux/sched.h>
36 #include <linux/crc32.h>
37
38 #include <dahdi/kernel.h>
39
40 #include "wct4xxp/wct4xxp.h" /* For certain definitions */
41 #include "wcxb.h"
42 #include "wcxb_spi.h"
43 #include "wcxb_flash.h"
44
45 static const char *TE133_FW_FILENAME = "dahdi-fw-te133.bin";
46 static const char *TE134_FW_FILENAME = "dahdi-fw-te134.bin";
47 static const u32 TE133_FW_VERSION = 0x7a001e;
48 static const u32 TE134_FW_VERSION = 0x780017;
49
50 #define WC_MAX_IFACES 8
51
52 enum linemode {
53 T1 = 1,
54 E1,
55 J1,
56 };
57
58 /* FPGA Status definitions */
59 #define OCT_CPU_RESET (1 << 0)
60 #define OCT_CPU_DRAM_CKE (1 << 1)
61 #define STATUS_LED_GREEN (1 << 9)
62 #define STATUS_LED_RED (1 << 10)
63 #define FALC_CPU_RESET (1 << 11)
64
65 /* Descriptor ring definitions */
66 #define DRING_SIZE (1 << 7) /* Must be in multiples of 2 */
67 #define DMA_CHAN_SIZE 128
68
69 /* Interrupt definitions */
70 #define INTERRUPT_CONTROL 0x300
71 #define IER (INTERRUPT_CONTROL + 0x8)
72 #define FALC_INT (1<<3)
73
74 struct t13x {
75 spinlock_t reglock;
76
77 u8 latency;
78
79 const struct t13x_desc *devtype;
80 struct {
81 unsigned int nmf:1;
82 unsigned int sendingyellow:1;
83 } flags;
84 unsigned char txsigs[16]; /* Copy of tx sig registers */
85 unsigned long lofalarmtimer;
86 unsigned long losalarmtimer;
87 unsigned long aisalarmtimer;
88 unsigned long yelalarmtimer;
89 unsigned long recoverytimer;
90 unsigned long blinktimer;
91 unsigned long loopuptimer;
92 unsigned long loopdntimer;
93 const char *name;
94 #define INITIALIZED 1
95 #define SHUTDOWN 2
96 #define READY 3
97 #define HAVE_OPEN_CHANNELS 4
98 unsigned long bit_flags;
99 u32 ledstate;
100 struct dahdi_device *ddev;
101 struct dahdi_span span; /* Span */
102 struct dahdi_chan *chans[32]; /* Channels */
103 struct dahdi_echocan_state *ec[32]; /* Echocan state for channels */
104
105 /* protected by t1.reglock */
106 struct timer_list timer;
107 struct work_struct timer_work;
108 struct workqueue_struct *wq;
109 struct vpm450m *vpm;
110 struct mutex lock;
111 struct wcxb xb;
112 u32 lineconfig_fingerprint;
113 };
114
115 static void te13x_handle_transmit(struct wcxb *xb, void *vfp);
116 static void te13x_handle_receive(struct wcxb *xb, void *vfp);
117 static void te13x_handle_interrupt(struct wcxb *xb, u32 pending);
118
119 static void te13x_handle_error(struct wcxb *xb)
120 {
121 struct t13x *wc = container_of(xb, struct t13x, xb);
122 wc->ddev->irqmisses++;
123 }
124
125 static struct wcxb_operations xb_ops = {
126 .handle_receive = te13x_handle_receive,
127 .handle_transmit = te13x_handle_transmit,
128 .handle_interrupt = te13x_handle_interrupt,
129 .handle_error = te13x_handle_error,
130 };
131
132 /* Maintenance Mode Registers */
133 #define LIM0 0x36
134 #define LIM0_LL (1<<1)
135 #define LIM1 0x37
136 #define LIM1_RL (1<<1)
137 #define LIM1_JATT (1<<2)
138 #define FMR3_T 0x21 /* T1 Framer 3 register */
139 #define FMR3_E 0x31 /* E1 Framer 3 register */
140 #define LOOPUP (1<<4) /* Transmit loopup code */
141 #define LOOPDOWN (1<<5) /* Transmit loopdown code */
142
143 /* Clear Channel Registers */
144 #define CCB1 0x2f
145 #define CCB2 0x30
146 #define CCB3 0x31
147
148 #define FECL_T 0x50 /* Framing Error Counter Lower Byte */
149 #define FECH_T 0x51 /* Framing Error Counter Higher Byte */
150 #define CVCL_T 0x52 /* Code Violation Counter Lower Byte */
151 #define CVCH_T 0x53 /* Code Violation Counter Higher Byte */
152 #define CEC1L_T 0x54 /* CRC Error Counter 1 Lower Byte */
153 #define CEC1H_T 0x55 /* CRC Error Counter 1 Higher Byte */
154 #define EBCL_T 0x56 /* E-Bit Error Counter Lower Byte */
155 #define EBCH_T 0x57 /* E-Bit Error Counter Higher Byte */
156 #define BECL_T 0x58 /* Bit Error Counter Lower Byte */
157 #define BECH_T 0x59 /* Bit Error Counter Higher Byte */
158 #define COEC_T 0x5A /* COFA Event Counter */
159 #define PRBSSTA_T 0xDA /* PRBS Status Register */
160 #define FRS1_T 0x4D /* Framer Receive Status Reg 1 */
161
162 #define ISR3_SEC (1 << 6) /* Internal one-second interrupt bit mask */
163 #define ISR3_ES (1 << 7) /* Errored Second interrupt bit mask */
164 #define IERR_T 0x1B /* Single Bit Defect Insertion Register */
165 #define IBV (1 << 0) /* Bipolar violation */
166 #define IPE (1 << 1) /* PRBS defect */
167 #define ICASE (1 << 2) /* CAS defect */
168 #define ICRCE (1 << 3) /* CRC defect */
169 #define IMFE (1 << 4) /* Multiframe defect */
170 #define IFASE (1 << 5) /* FAS defect */
171
172 #define IMR0 0x14
173 #define CCR1 0x09
174
175 /* pci memory map offsets */
176 #define FRAMER_BASE 0x00000800 /* framer's address space */
177
178 static int debug;
179 static int alarmdebounce = 2500; /* LOF/LFA def to 2.5s AT&T TR54016*/
180 static int losalarmdebounce = 2500; /* LOS def to 2.5s AT&T TR54016*/
181 static int aisalarmdebounce = 2500; /* AIS(blue) def to 2.5s AT&T TR54016*/
182 static int yelalarmdebounce = 500; /* RAI(yellow) def to 0.5s AT&T devguide */
183 static char *default_linemode = "t1"; /* 'e1', 't1', or 'j1' */
184 static int force_firmware;
185 static int latency = WCXB_DEFAULT_LATENCY;
186 static unsigned int max_latency = WCXB_DEFAULT_MAXLATENCY;
187
188 struct t13x_firm_header {
189 u8 header[6];
190 __le32 chksum;
191 u8 pad[18];
192 __le32 version;
193 } __packed;
194
195
196 static void t13x_check_alarms(struct t13x *wc);
197 static void t13x_check_sigbits(struct t13x *wc);
198
199 #include <linux/slab.h>
200 #include <linux/vmalloc.h>
201 #include <linux/string.h>
202 #include <linux/time.h>
203 #include <linux/version.h>
204 #include <linux/firmware.h>
205
206 #include <oct612x.h>
207
208 #define ECHOCAN_NUM_CHANS 32
209
210 #define FLAGBIT_DTMF 1
211 #define FLAGBIT_MUTE 2
212 #define FLAGBIT_ECHO 3
213 #define FLAGBIT_ALAW 4
214
215 #define OCT_CHIP_ID 0
216 #define OCT_MAX_TDM_STREAMS 4
217 #define OCT_TONEEVENT_BUFFER_SIZE 128
218 #define SOUT_STREAM 1
219 #define RIN_STREAM 0
220
221 #define SIN_STREAM 2
222 #define OCT_OFFSET (wc->xb.membase + 0x10000)
223 #define OCT_CONTROL_REG (OCT_OFFSET + 0)
224 #define OCT_DATA_REG (OCT_OFFSET + 0x4)
225 #define OCT_ADDRESS_HIGH_REG (OCT_OFFSET + 0x8)
226 #define OCT_ADDRESS_LOW_REG (OCT_OFFSET + 0xa)
227 #define OCT_DIRECT_WRITE_MASK 0x3001
228 #define OCT_INDIRECT_READ_MASK 0x0101
229 #define OCT_INDIRECT_WRITE_MASK 0x3101
230
231
232 static int vpmsupport = 1;
233 static const char *vpm_name = "VPMOCT032";
234 static void t13x_vpm_init(struct t13x *wc);
235 static void echocan_free(struct dahdi_chan *chan,
236 struct dahdi_echocan_state *ec);
237 static const struct dahdi_echocan_features vpm_ec_features = {
238 .NLP_automatic = 1,
239 .CED_tx_detect = 1,
240 .CED_rx_detect = 1,
241 };
242 static const struct dahdi_echocan_ops vpm_ec_ops = {
243 .echocan_free = echocan_free,
244 };
245
246 struct vpm450m {
247 tPOCT6100_INSTANCE_API pApiInstance;
248 struct oct612x_context context;
249 UINT32 aulEchoChanHndl[ECHOCAN_NUM_CHANS];
250 int ecmode[ECHOCAN_NUM_CHANS];
251 unsigned long chanflags[ECHOCAN_NUM_CHANS];
252 };
253
254 static void oct_reset(struct t13x *wc)
255 {
256 wcxb_gpio_clear(&wc->xb, OCT_CPU_RESET);
257 msleep_interruptible(1);
258 wcxb_gpio_set(&wc->xb, OCT_CPU_RESET);
259
260 dev_info(&wc->xb.pdev->dev, "Reset octasic\n");
261 }
262
263 static void oct_enable_dram(struct t13x *wc)
264 {
265 wcxb_gpio_set(&wc->xb, OCT_CPU_DRAM_CKE);
266 }
267
268 static unsigned int oct_get_reg_indirect(void *data, uint32_t address)
269 {
270 struct t13x *wc = data;
271 uint16_t highaddress = ((address >> 20) & 0xfff);
272 uint16_t lowaddress = ((address >> 4) & 0xfffff);
273 unsigned long stop = jiffies + HZ/10;
274 unsigned long flags;
275 uint16_t ret;
276
277 spin_lock_irqsave(&wc->reglock, flags);
278 iowrite16be(highaddress, OCT_ADDRESS_HIGH_REG);
279 iowrite16be(lowaddress, OCT_ADDRESS_LOW_REG);
280
281 iowrite16be(OCT_INDIRECT_READ_MASK | ((address & 0xe) << 8),
282 OCT_CONTROL_REG);
283 do {
284 ret = ioread16be(OCT_CONTROL_REG);
285 } while ((ret & (1<<8)) && time_before(jiffies, stop));
286
287 WARN_ON_ONCE(time_after_eq(jiffies, stop));
288
289 ret = ioread16be(OCT_DATA_REG);
290 spin_unlock_irqrestore(&wc->reglock, flags);
291
292 return ret;
293 }
294
295 static void oct_set_reg_indirect(void *data, uint32_t address, uint16_t val)
296 {
297 struct t13x *wc = data;
298 unsigned long flags;
299 uint16_t ret;
300 uint16_t highaddress = ((address >> 20) & 0xfff);
301 uint16_t lowaddress = ((address >> 4) & 0xffff);
302 unsigned long stop = jiffies + HZ/10;
303
304 spin_lock_irqsave(&wc->reglock, flags);
305 iowrite16be(highaddress, OCT_ADDRESS_HIGH_REG);
306 iowrite16be(lowaddress, OCT_ADDRESS_LOW_REG);
307
308 iowrite16be(val, OCT_DATA_REG);
309 iowrite16be(OCT_INDIRECT_WRITE_MASK | ((address & 0xe) << 8),
310 OCT_CONTROL_REG);
311
312 /* No write should take longer than 100ms */
313 do {
314 ret = ioread16be(OCT_CONTROL_REG);
315 } while ((ret & (1<<8)) && time_before(jiffies, stop));
316 spin_unlock_irqrestore(&wc->reglock, flags);
317
318 WARN_ON_ONCE(time_after_eq(jiffies, stop));
319 }
320
321 static int t13x_oct612x_write(struct oct612x_context *context,
322 u32 address, u16 value)
323 {
324 oct_set_reg_indirect(dev_get_drvdata(context->dev), address, value);
325 return 0;
326 }
327
328 static int t13x_oct612x_read(struct oct612x_context *context, u32 address,
329 u16 *value)
330 {
331 *value = oct_get_reg_indirect(dev_get_drvdata(context->dev), address);
332 return 0;
333 }
334
335 static int t13x_oct612x_write_smear(struct oct612x_context *context,
336 u32 address, u16 value, size_t count)
337 {
338 unsigned int i;
339 struct t13x *wc = dev_get_drvdata(context->dev);
340 for (i = 0; i < count; ++i)
341 oct_set_reg_indirect(wc, address + (i << 1), value);
342 return 0;
343 }
344
345 static int t13x_oct612x_write_burst(struct oct612x_context *context,
346 u32 address, const u16 *buffer,
347 size_t count)
348 {
349 unsigned int i;
350 struct t13x *wc = dev_get_drvdata(context->dev);
351 for (i = 0; i < count; ++i)
352 oct_set_reg_indirect(wc, address + (i << 1), buffer[i]);
353 return 0;
354 }
355
356 static int t13x_oct612x_read_burst(struct oct612x_context *context,
357 u32 address, u16 *buffer, size_t count)
358 {
359 unsigned int i;
360 struct t13x *wc = dev_get_drvdata(context->dev);
361 for (i = 0; i < count; ++i)
362 buffer[i] = oct_get_reg_indirect(wc, address + (i << 1));
363 return 0;
364 }
365
366 static const struct oct612x_ops t13x_oct612x_ops = {
367 .write = t13x_oct612x_write,
368 .read = t13x_oct612x_read,
369 .write_smear = t13x_oct612x_write_smear,
370 .write_burst = t13x_oct612x_write_burst,
371 .read_burst = t13x_oct612x_read_burst,
372 };
373
374 static void vpm450m_setecmode(struct vpm450m *vpm450m, int channel, int mode)
375 {
376 tOCT6100_CHANNEL_MODIFY *modify;
377 UINT32 ulResult;
378
379 if (vpm450m->ecmode[channel] == mode)
380 return;
381 modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
382 if (!modify) {
383 pr_notice("Unable to allocate memory for setec!\n");
384 return;
385 }
386 Oct6100ChannelModifyDef(modify);
387 modify->ulEchoOperationMode = mode;
388 modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
389 ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
390 if (ulResult != GENERIC_OK) {
391 pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
392 vpm450m->aulEchoChanHndl[channel], channel, ulResult);
393 } else {
394 #ifdef OCTASIC_DEBUG
395 pr_debug("Echo can on channel %d set to %d\n", channel, mode);
396 #endif
397 vpm450m->ecmode[channel] = mode;
398 }
399 kfree(modify);
400 }
401
402 static void vpm450m_set_alaw_companding(struct vpm450m *vpm450m, int channel,
403 bool alaw)
404 {
405 tOCT6100_CHANNEL_MODIFY *modify;
406 UINT32 ulResult;
407 UINT32 law_to_use = (alaw) ? cOCT6100_PCM_A_LAW :
408 cOCT6100_PCM_U_LAW;
409
410 /* If we're already in this companding mode, no need to do anything. */
411 if (alaw == (test_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]) > 0))
412 return;
413
414 modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
415 if (!modify) {
416 pr_notice("Unable to allocate memory for setec!\n");
417 return;
418 }
419
420 Oct6100ChannelModifyDef(modify);
421 modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
422 modify->fTdmConfigModified = TRUE;
423 modify->TdmConfig.ulSinPcmLaw = law_to_use;
424 modify->TdmConfig.ulRinPcmLaw = law_to_use;
425 modify->TdmConfig.ulSoutPcmLaw = law_to_use;
426 modify->TdmConfig.ulRoutPcmLaw = law_to_use;
427 ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
428 if (ulResult != GENERIC_OK) {
429 pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
430 vpm450m->aulEchoChanHndl[channel], channel, ulResult);
431 } else {
432 pr_info("Changed companding on channel %d to %s.\n", channel,
433 (alaw) ? "alaw" : "ulaw");
434 if (alaw)
435 set_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]);
436 else
437 clear_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]);
438 }
439 kfree(modify);
440 }
441
442 static void vpm450m_setec(struct vpm450m *vpm450m, int channel, int eclen)
443 {
444 if (eclen) {
445 set_bit(FLAGBIT_ECHO, &vpm450m->chanflags[channel]);
446 vpm450m_setecmode(vpm450m, channel,
447 cOCT6100_ECHO_OP_MODE_NORMAL);
448 } else {
449 unsigned long *flags = &vpm450m->chanflags[channel];
450 clear_bit(FLAGBIT_ECHO, &vpm450m->chanflags[channel]);
451 if (test_bit(FLAGBIT_DTMF, flags) ||
452 test_bit(FLAGBIT_MUTE, flags)) {
453 vpm450m_setecmode(vpm450m, channel,
454 cOCT6100_ECHO_OP_MODE_HT_RESET);
455 } else {
456 vpm450m_setecmode(vpm450m, channel,
457 cOCT6100_ECHO_OP_MODE_POWER_DOWN);
458 }
459 }
460 }
461
462 static UINT32 tdmmode_chan_to_slot_map(int channel)
463 {
464 /* Four phases on the tdm bus, skip three of them per channel */
465 /* Due to a bug in the octasic, we had to move the data onto phase 2 */
466 return 1+(channel*4);
467 }
468
469 static int echocan_initialize_channel(
470 struct vpm450m *vpm, int channel, int mode)
471 {
472 tOCT6100_CHANNEL_OPEN ChannelOpen;
473 UINT32 law_to_use = (mode) ? cOCT6100_PCM_A_LAW :
474 cOCT6100_PCM_U_LAW;
475 UINT32 tdmslot_setting;
476 UINT32 ulResult;
477
478 if (0 > channel || ECHOCAN_NUM_CHANS <= channel)
479 return -1;
480
481 tdmslot_setting = tdmmode_chan_to_slot_map(channel);
482
483 /* Fill Open channel structure with defaults */
484 Oct6100ChannelOpenDef(&ChannelOpen);
485
486 /* Assign the handle memory.*/
487 ChannelOpen.pulChannelHndl = &vpm->aulEchoChanHndl[channel];
488 ChannelOpen.ulUserChanId = channel;
489 /* Enable Tone disabling for Fax and Modems */
490 ChannelOpen.fEnableToneDisabler = TRUE;
491
492 /* Passthrough TDM data by default, no echocan */
493 ChannelOpen.ulEchoOperationMode = cOCT6100_ECHO_OP_MODE_POWER_DOWN;
494
495 /* Configure the TDM settings.*/
496 /* Input from the framer */
497 ChannelOpen.TdmConfig.ulSinStream = SIN_STREAM;
498 ChannelOpen.TdmConfig.ulSinTimeslot = tdmslot_setting;
499 ChannelOpen.TdmConfig.ulSinPcmLaw = law_to_use;
500
501 /* Input from the Host (pre-framer) */
502 ChannelOpen.TdmConfig.ulRinStream = RIN_STREAM;
503 ChannelOpen.TdmConfig.ulRinTimeslot = tdmslot_setting;
504 ChannelOpen.TdmConfig.ulRinPcmLaw = law_to_use;
505
506 /* Output to the Host */
507 ChannelOpen.TdmConfig.ulSoutStream = SOUT_STREAM;
508 ChannelOpen.TdmConfig.ulSoutTimeslot = tdmslot_setting;
509 ChannelOpen.TdmConfig.ulSoutPcmLaw = law_to_use;
510
511 /* From asterisk after echo-cancellation - goes nowhere */
512 ChannelOpen.TdmConfig.ulRoutStream = cOCT6100_UNASSIGNED;
513 ChannelOpen.TdmConfig.ulRoutTimeslot = cOCT6100_UNASSIGNED;
514 ChannelOpen.TdmConfig.ulRoutPcmLaw = law_to_use;
515
516 /* Set the desired VQE features.*/
517 ChannelOpen.VqeConfig.fEnableNlp = TRUE;
518 ChannelOpen.VqeConfig.fRinDcOffsetRemoval = TRUE;
519 ChannelOpen.VqeConfig.fSinDcOffsetRemoval = TRUE;
520 ChannelOpen.VqeConfig.ulComfortNoiseMode =
521 cOCT6100_COMFORT_NOISE_NORMAL;
522
523 /* Open the channel.*/
524 ulResult = Oct6100ChannelOpen(vpm->pApiInstance, &ChannelOpen);
525
526 return ulResult;
527 }
528
529 static struct vpm450m *init_vpm450m(struct t13x *wc, int isalaw,
530 const struct firmware *firmware)
531 {
532 tOCT6100_CHIP_OPEN *ChipOpen;
533 tOCT6100_GET_INSTANCE_SIZE InstanceSize;
534 tOCT6100_CHANNEL_OPEN *ChannelOpen;
535 UINT32 ulResult;
536 struct vpm450m *vpm450m;
537 int x, i;
538
539 vpm450m = kzalloc(sizeof(struct vpm450m), GFP_KERNEL);
540 if (!vpm450m) {
541 dev_info(&wc->xb.pdev->dev, "Unable to allocate vpm450m struct\n");
542 return NULL;
543 }
544
545 vpm450m->context.dev = &wc->xb.pdev->dev;
546 vpm450m->context.ops = &t13x_oct612x_ops;
547
548 ChipOpen = kzalloc(sizeof(tOCT6100_CHIP_OPEN), GFP_KERNEL);
549 if (!ChipOpen) {
550 dev_info(&wc->xb.pdev->dev, "Unable to allocate ChipOpen\n");
551 kfree(vpm450m);
552 return NULL;
553 }
554
555 ChannelOpen = kzalloc(sizeof(tOCT6100_CHANNEL_OPEN), GFP_KERNEL);
556 if (!ChannelOpen) {
557 dev_info(&wc->xb.pdev->dev, "Unable to allocate ChannelOpen\n");
558 kfree(vpm450m);
559 kfree(ChipOpen);
560 return NULL;
561 }
562
563 for (x = 0; x < ARRAY_SIZE(vpm450m->ecmode); x++)
564 vpm450m->ecmode[x] = -1;
565
566 dev_info(&wc->xb.pdev->dev, "Echo cancellation for %d channels\n",
567 ECHOCAN_NUM_CHANS);
568
569 Oct6100ChipOpenDef(ChipOpen);
570 ChipOpen->pProcessContext = &vpm450m->context;
571
572 /* Change default parameters as needed */
573 /* upclk oscillator is at 33.33 Mhz */
574 ChipOpen->ulUpclkFreq = cOCT6100_UPCLK_FREQ_33_33_MHZ;
575
576 /* mclk will be generated by internal PLL at 133 Mhz */
577 ChipOpen->fEnableMemClkOut = TRUE;
578 ChipOpen->ulMemClkFreq = cOCT6100_MCLK_FREQ_133_MHZ;
579
580 /* User defined Chip ID.*/
581 ChipOpen->ulUserChipId = OCT_CHIP_ID;
582
583 /* Set the maximums that the chip needs to support */
584 ChipOpen->ulMaxChannels = ECHOCAN_NUM_CHANS;
585 ChipOpen->ulMaxTdmStreams = OCT_MAX_TDM_STREAMS;
586
587 /* External Memory Settings */
588 /* Use DDR memory.*/
589 ChipOpen->ulMemoryType = cOCT6100_MEM_TYPE_DDR;
590 ChipOpen->ulNumMemoryChips = 1;
591 ChipOpen->ulMemoryChipSize = cOCT6100_MEMORY_CHIP_SIZE_32MB;
592
593 ChipOpen->pbyImageFile = (PUINT8) firmware->data;
594 ChipOpen->ulImageSize = firmware->size;
595
596 /* Set TDM data stream frequency */
597 for (i = 0; i < ChipOpen->ulMaxTdmStreams; i++)
598 ChipOpen->aulTdmStreamFreqs[i] = cOCT6100_TDM_STREAM_FREQ_8MHZ;
599
600 /* Configure TDM sampling */
601 ChipOpen->ulTdmSampling = cOCT6100_TDM_SAMPLE_AT_FALLING_EDGE;
602 /* Disable to save RAM footprint space */
603 ChipOpen->fEnableChannelRecording = FALSE;
604
605 /* In this example we will maintain the API using polling so
606 interrupts must be disabled */
607 ChipOpen->InterruptConfig.ulErrorH100Config =
608 cOCT6100_INTERRUPT_DISABLE;
609 ChipOpen->InterruptConfig.ulErrorMemoryConfig =
610 cOCT6100_INTERRUPT_DISABLE;
611 ChipOpen->InterruptConfig.ulFatalGeneralConfig =
612 cOCT6100_INTERRUPT_DISABLE;
613 ChipOpen->InterruptConfig.ulFatalMemoryConfig =
614 cOCT6100_INTERRUPT_DISABLE;
615
616 ChipOpen->ulSoftToneEventsBufSize = OCT_TONEEVENT_BUFFER_SIZE;
617
618 /* Inserting default values into tOCT6100_GET_INSTANCE_SIZE
619 structure parameters. */
620 Oct6100GetInstanceSizeDef(&InstanceSize);
621
622 /* Reset octasic device */
623 oct_reset(wc);
624
625 /* Get the size of the OCT6100 instance structure. */
626 ulResult = Oct6100GetInstanceSize(ChipOpen, &InstanceSize);
627 if (ulResult != cOCT6100_ERR_OK) {
628 dev_info(&wc->xb.pdev->dev, "Unable to get instance size: %x\n",
629 ulResult);
630 return NULL;
631 }
632
633 vpm450m->pApiInstance = vmalloc(InstanceSize.ulApiInstanceSize);
634 if (!vpm450m->pApiInstance) {
635 dev_info(&wc->xb.pdev->dev,
636 "Out of memory (can't allocate %d bytes)!\n",
637 InstanceSize.ulApiInstanceSize);
638 return NULL;
639 }
640
641 /* Perform the actual configuration of the chip. */
642 oct_enable_dram(wc);
643 ulResult = Oct6100ChipOpen(vpm450m->pApiInstance, ChipOpen);
644 if (ulResult != cOCT6100_ERR_OK) {
645 dev_info(&wc->xb.pdev->dev, "Unable to Oct6100ChipOpen: %x\n",
646 ulResult);
647 return NULL;
648 }
649
650 /* OCT6100 is now booted and channels can be opened */
651 /* Open all channels */
652 for (i = 0; i < ECHOCAN_NUM_CHANS; i++) {
653 ulResult = echocan_initialize_channel(vpm450m, i, isalaw);
654 if (0 != ulResult) {
655 dev_info(&wc->xb.pdev->dev,
656 "Unable to echocan_initialize_channel: %x\n",
657 ulResult);
658 return NULL;
659 } else if (isalaw) {
660 set_bit(FLAGBIT_ALAW, &vpm450m->chanflags[i]);
661 }
662 }
663
664 if (vpmsupport)
665 wcxb_enable_echocan(&wc->xb);
666
667 kfree(ChipOpen);
668 kfree(ChannelOpen);
669 return vpm450m;
670 }
671
672 static void release_vpm450m(struct vpm450m *vpm450m)
673 {
674 UINT32 ulResult;
675 tOCT6100_CHIP_CLOSE ChipClose;
676
677 Oct6100ChipCloseDef(&ChipClose);
678 ulResult = Oct6100ChipClose(vpm450m->pApiInstance, &ChipClose);
679 if (ulResult != cOCT6100_ERR_OK)
680 pr_notice("Failed to close chip, code %08x!\n", ulResult);
681 vfree(vpm450m->pApiInstance);
682 kfree(vpm450m);
683 }
684
685 static const char *__t13x_echocan_name(struct t13x *wc)
686 {
687 if (wc->vpm)
688 return vpm_name;
689 else
690 return NULL;
691 }
692
693 static const char *t13x_echocan_name(const struct dahdi_chan *chan)
694 {
695 struct t13x *wc = chan->pvt;
696 return __t13x_echocan_name(wc);
697 }
698
699 static int t13x_echocan_create(struct dahdi_chan *chan,
700 struct dahdi_echocanparams *ecp,
701 struct dahdi_echocanparam *p,
702 struct dahdi_echocan_state **ec)
703 {
704 struct t13x *wc = chan->pvt;
705 const int channel = chan->chanpos - 1;
706 const struct dahdi_echocan_ops *ops;
707 const struct dahdi_echocan_features *features;
708 const bool alaw = (chan->span->deflaw == 2);
709
710 if (!vpmsupport || !wc->vpm)
711 return -ENODEV;
712
713 ops = &vpm_ec_ops;
714 features = &vpm_ec_features;
715
716 if (ecp->param_count > 0) {
717 dev_warn(&wc->xb.pdev->dev, "%s echo canceller does not support parameters; failing request\n",
718 chan->ec_factory->get_name(chan));
719 return -EINVAL;
720 }
721
722 *ec = wc->ec[channel];
723 (*ec)->ops = ops;
724 (*ec)->features = *features;
725
726 vpm450m_set_alaw_companding(wc->vpm, channel, alaw);
727 vpm450m_setec(wc->vpm, channel, ecp->tap_length);
728 return 0;
729 }
730
731 static void echocan_free(struct dahdi_chan *chan,
732 struct dahdi_echocan_state *ec)
733 {
734 struct t13x *wc = chan->pvt;
735 const int channel = chan->chanpos - 1;
736 if (!wc->vpm)
737 return;
738 memset(ec, 0, sizeof(*ec));
739 vpm450m_setec(wc->vpm, channel, 0);
740 }
741
742 static void t13x_vpm_init(struct t13x *wc)
743 {
744 int companding = 0;
745 struct firmware embedded_firmware;
746 const struct firmware *firmware = &embedded_firmware;
747 #if !defined(HOTPLUG_FIRMWARE)
748 extern void _binary_dahdi_fw_oct6114_032_bin_size;
749 extern u8 _binary_dahdi_fw_oct6114_032_bin_start[];
750 #else
751 static const char oct032_firmware[] = "dahdi-fw-oct6114-032.bin";
752 #endif
753
754 if (!vpmsupport) {
755 dev_info(&wc->xb.pdev->dev, "VPM450: Support Disabled\n");
756 return;
757 }
758
759 #if defined(HOTPLUG_FIRMWARE)
760 if ((request_firmware(&firmware, oct032_firmware,
761 &wc->xb.pdev->dev) != 0) || !firmware) {
762 dev_notice(&wc->xb.pdev->dev, "VPM450: firmware %s not available from userspace\n",
763 oct032_firmware);
764 return;
765 }
766 #else
767 embedded_firmware.data = _binary_dahdi_fw_oct6114_032_bin_start;
768 /* Yes... this is weird. objcopy gives us a symbol containing
769 the size of the firmware, not a pointer a variable containing
770 the size. The only way we can get the value of the symbol
771 is to take its address, so we define it as a pointer and
772 then cast that value to the proper type.
773 */
774 embedded_firmware.size = (size_t)&_binary_dahdi_fw_oct6114_032_bin_size;
775 #endif
776
777 companding = dahdi_is_e1_span(&wc->span);
778
779 wc->vpm = init_vpm450m(wc, companding, firmware);
780 if (!wc->vpm) {
781 dev_notice(&wc->xb.pdev->dev, "VPM450: Failed to initialize\n");
782 if (firmware != &embedded_firmware)
783 release_firmware(firmware);
784 return;
785 }
786
787 if (firmware != &embedded_firmware)
788 release_firmware(firmware);
789
790 dev_info(&wc->xb.pdev->dev,
791 "VPM450: Present and operational servicing %d span\n", 1);
792
793 }
794
795 static int t13x_clear_maint(struct dahdi_span *span);
796
797 static struct t13x *ifaces[WC_MAX_IFACES];
798
799 struct t13x_desc {
800 const char *name;
801 };
802
803 static const struct t13x_desc te133 = {"Wildcard TE131/TE133"}; /* pci express*/
804 static const struct t13x_desc te134 = {"Wildcard TE132/TE134"}; /* legacy pci */
805
806 static inline bool is_pcie(const struct t13x *t1)
807 {
808 return t1->devtype == &te133;
809 }
810
811 static int __t13x_pci_get(struct t13x *wc, unsigned int addr)
812 {
813 unsigned int res = ioread8(wc->xb.membase + addr);
814 return res;
815 }
816
817 static inline int __t13x_pci_set(struct t13x *wc, unsigned int addr, int val)
818 {
819 iowrite8(val, wc->xb.membase + addr);
820 __t13x_pci_get(wc, 0);
821 return 0;
822 }
823
824 static inline int t13x_pci_get(struct t13x *wc, int addr)
825 {
826 unsigned int ret;
827 unsigned long flags;
828
829 spin_lock_irqsave(&wc->reglock, flags);
830 ret = __t13x_pci_get(wc, addr);
831 spin_unlock_irqrestore(&wc->reglock, flags);
832 return ret;
833 }
834
835 static inline int t13x_pci_set(struct t13x *wc, int addr, int val)
836 {
837 unsigned long flags;
838 unsigned int ret;
839 spin_lock_irqsave(&wc->reglock, flags);
840 ret = __t13x_pci_set(wc, addr, val);
841 spin_unlock_irqrestore(&wc->reglock, flags);
842 return ret;
843 }
844
845 static inline int __t13x_framer_set(struct t13x *wc, int addr, int val)
846 {
847 return __t13x_pci_set(wc, FRAMER_BASE + addr, val);
848 }
849
850 static inline int t13x_framer_set(struct t13x *wc, int addr, int val)
851 {
852 return t13x_pci_set(wc, FRAMER_BASE + addr, val);
853 }
854
855 static inline int __t13x_framer_get(struct t13x *wc, int addr)
856 {
857 return __t13x_pci_get(wc, FRAMER_BASE + addr);
858 }
859
860 static inline int t13x_framer_get(struct t13x *wc, int addr)
861 {
862 return t13x_pci_get(wc, FRAMER_BASE + addr);
863 }
864
865 static void t13x_framer_reset(struct t13x *wc)
866 {
867 /*
868 * When the framer is reset, RCLK will stop. The FPGA must be switched
869 * to it's internal clock when this happens, but it's only safe to
870 * switch the clock source on the FPGA when the DMA engine is stopped.
871 *
872 */
873 wcxb_stop_dma(&wc->xb);
874 wcxb_wait_for_stop(&wc->xb, 50);
875 wcxb_set_clksrc(&wc->xb, WCXB_CLOCK_SELF);
876 wcxb_gpio_clear(&wc->xb, FALC_CPU_RESET);
877 msleep_interruptible(100);
878 wcxb_gpio_set(&wc->xb, FALC_CPU_RESET);
879 }
880
881 static void t13x_setleds(struct t13x *wc, u32 leds)
882 {
883 static const u32 LED_MASK = 0x600;
884 wcxb_gpio_set(&wc->xb, leds & LED_MASK);
885 wcxb_gpio_clear(&wc->xb, ~leds & LED_MASK);
886 }
887
888 static void __t13x_set_clear(struct t13x *wc)
889 {
890 int i, offset;
891 int reg;
892 unsigned long flags;
893 bool span_has_cas_channel = false;
894
895 if (dahdi_is_e1_span(&wc->span)) {
896 span_has_cas_channel = !(wc->span.lineconfig&DAHDI_CONFIG_CCS);
897 } else {
898 unsigned char ccb[3] = {0, 0, 0};
899 /* Sort out channels that use CAS signalling */
900 for (i = 0; i < wc->span.channels; i++) {
901 offset = i/8;
902 if (offset >= ARRAY_SIZE(ccb)) {
903 WARN_ON(1);
904 break;
905 }
906 if (wc->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
907 ccb[offset] |= 1 << (7 - (i % 8));
908 else
909 ccb[offset] &= ~(1 << (7 - (i % 8)));
910 }
911
912 spin_lock_irqsave(&wc->reglock, flags);
913 __t13x_framer_set(wc, CCB1, ccb[0]);
914 __t13x_framer_set(wc, CCB2, ccb[1]);
915 __t13x_framer_set(wc, CCB3, ccb[2]);
916 spin_unlock_irqrestore(&wc->reglock, flags);
917
918 if ((~ccb[0]) | (~ccb[1]) | (~ccb[2]))
919 span_has_cas_channel = true;
920 }
921
922 /* Unmask CAS RX interrupt if any single channel is in CAS mode */
923 /* This interrupt is called RSC in T1 and CASC in E1 */
924 spin_lock_irqsave(&wc->reglock, flags);
925 reg = __t13x_framer_get(wc, IMR0);
926 if (span_has_cas_channel)
927 __t13x_framer_set(wc, IMR0, reg & ~0x08);
928 else
929 __t13x_framer_set(wc, IMR0, reg | 0x08);
930 spin_unlock_irqrestore(&wc->reglock, flags);
931 }
932
933 /**
934 * _t13x_free_channels - Free the memory allocated for the channels.
935 *
936 * Must be called with wc->reglock held.
937 *
938 */
939 static void _t13x_free_channels(struct t13x *wc)
940 {
941 int x;
942 for (x = 0; x < ARRAY_SIZE(wc->chans); x++) {
943 kfree(wc->chans[x]);
944 kfree(wc->ec[x]);
945 wc->chans[x] = NULL;
946 wc->ec[x] = NULL;
947 }
948 }
949
950 static void free_wc(struct t13x *wc)
951 {
952 unsigned long flags;
953 LIST_HEAD(list);
954
955 spin_lock_irqsave(&wc->reglock, flags);
956 _t13x_free_channels(wc);
957 spin_unlock_irqrestore(&wc->reglock, flags);
958
959 if (wc->wq)
960 destroy_workqueue(wc->wq);
961
962 kfree(wc->ddev->location);
963 kfree(wc->ddev->devicetype);
964 kfree(wc->ddev->hardware_id);
965 if (wc->ddev)
966 dahdi_free_device(wc->ddev);
967 kfree(wc->name);
968 kfree(wc);
969 }
970
971 static void t13x_serial_setup(struct t13x *wc)
972 {
973 unsigned long flags;
974
975 spin_lock_irqsave(&wc->reglock, flags);
976 __t13x_framer_set(wc, 0x85, 0xe0); /* GPC1: Multiplex mode
977 enabled, FSC is output,
978 active low, RCLK from
979 channel 0 */
980 __t13x_framer_set(wc, 0x08, 0x05); /* IPC: Interrupt push/pull
981 active low */
982
983 /* Global clocks (8.192 Mhz CLK) */
984 __t13x_framer_set(wc, 0x92, 0x00);
985 __t13x_framer_set(wc, 0x93, 0x18);
986 __t13x_framer_set(wc, 0x94, 0xfb);
987 __t13x_framer_set(wc, 0x95, 0x0b);
988 __t13x_framer_set(wc, 0x96, 0x00);
989 __t13x_framer_set(wc, 0x97, 0x0b);
990 __t13x_framer_set(wc, 0x98, 0xdb);
991 __t13x_framer_set(wc, 0x99, 0xdf);
992
993 /* Configure interrupts */
994 __t13x_framer_set(wc, 0x46, 0xc0); /* GCR: Interrupt on
995 Activation/Deactivation of
996 AIX, LOS */
997
998 /* Configure system interface */
999 __t13x_framer_set(wc, 0x3e, 0xc2); /* SIC1: 8.192 Mhz clock/bus,
1000 double buffer receive /
1001 transmit, byte interleaved
1002 */
1003 __t13x_framer_set(wc, 0x3f, 0x02); /* SIC2: No FFS, no center
1004 receive eliastic buffer,
1005 phase 1 */
1006 __t13x_framer_set(wc, 0x40, 0x04); /* SIC3: Edges for capture */
1007 __t13x_framer_set(wc, 0x44, 0x30); /* CMR1: RCLK is at 8.192 Mhz
1008 dejittered */
1009 __t13x_framer_set(wc, 0x45, 0x00); /* CMR2: We provide sync and
1010 clock for tx and rx. */
1011 __t13x_framer_set(wc, 0x22, 0x00); /* XC0: Normal operation of
1012 Sa-bits */
1013 __t13x_framer_set(wc, 0x23, 0x02); /* XC1: 0 offset */
1014 __t13x_framer_set(wc, 0x24, 0x00); /* RC0: Just shy of 255 */
1015 __t13x_framer_set(wc, 0x25, 0x03); /* RC1: The rest of RC0 */
1016
1017 /* Configure ports */
1018 __t13x_framer_set(wc, 0x80, 0x00); /* PC1: SPYR/SPYX input on
1019 RPA/XPA */
1020 __t13x_framer_set(wc, 0x81, 0x22); /* PC2: RMFB/XSIG output/input
1021 on RPB/XPB */
1022 __t13x_framer_set(wc, 0x82, 0x65); /* PC3: Unused stuff */
1023 __t13x_framer_set(wc, 0x83, 0x35); /* PC4: Unused stuff */
1024 __t13x_framer_set(wc, 0x84, 0x31); /* PC5: XMFS active low, SCLKR
1025 is input, RCLK is output */
1026 __t13x_framer_set(wc, 0x86, 0x03); /* PC6: CLK1 is Tx Clock
1027 output, CLK2 is 8.192 Mhz
1028 from DCO-R */
1029 __t13x_framer_set(wc, 0x3b, 0x00); /* Clear LCR1 */
1030 spin_unlock_irqrestore(&wc->reglock, flags);
1031 }
1032
1033 static void t13x_configure_t1(struct t13x *wc, int lineconfig, int txlevel)
1034 {
1035 unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
1036 char *framing, *line;
1037 int mytxlevel;
1038 unsigned long flags;
1039
1040 if ((txlevel > 7) || (txlevel < 4))
1041 mytxlevel = 0;
1042 else
1043 mytxlevel = txlevel - 4;
1044 fmr1 = 0x9c; /* FMR1: Mode 0, T1 mode, CRC on for ESF, 2.048 Mhz system
1045 data rate, no XAIS */
1046 fmr2 = 0x20; /* FMR2: no payload loopback, don't auto yellow alarm */
1047
1048
1049 if (SPANTYPE_DIGITAL_J1 == wc->span.spantype)
1050 fmr4 = 0x1c;
1051 else
1052 fmr4 = 0x0c; /* FMR4: Lose sync on 2 out of 5 framing bits,
1053 auto resync */
1054
1055 lim2 = 0x21; /* LIM2: 50% peak is a "1", Advanced Loss recovery */
1056 lim2 |= (mytxlevel << 6); /* LIM2: Add line buildout */
1057
1058 spin_lock_irqsave(&wc->reglock, flags);
1059
1060 __t13x_framer_set(wc, 0x1d, fmr1);
1061 __t13x_framer_set(wc, 0x1e, fmr2);
1062
1063 /* Configure line interface */
1064 if (lineconfig & DAHDI_CONFIG_AMI) {
1065 line = "AMI";
1066 fmr0 = 0xa0;
1067 } else {
1068 line = "B8ZS";
1069 fmr0 = 0xf0;
1070 }
1071 if (lineconfig & DAHDI_CONFIG_D4) {
1072 framing = "D4";
1073 } else {
1074 framing = "ESF";
1075 fmr4 |= 0x2;
1076 fmr2 |= 0xc0;
1077 }
1078 __t13x_framer_set(wc, 0x1c, fmr0);
1079
1080 __t13x_framer_set(wc, 0x20, fmr4);
1081 __t13x_framer_set(wc, FMR3_T, 0x40); /* FMR5: Enable RBS mode */
1082
1083 __t13x_framer_set(wc, 0x37, 0xf8); /* LIM1: Clear data in case of
1084 LOS, Set receiver threshold
1085 (0.5V), No remote loop, no
1086 DRS */
1087 __t13x_framer_set(wc, 0x36, 0x08); /* LIM0: Enable auto long haul
1088 mode, no local loop (must be
1089 after LIM1) */
1090
1091 __t13x_framer_set(wc, 0x02, 0x50); /* CMDR: Reset the receiver and
1092 transmitter line interface
1093 */
1094 __t13x_framer_set(wc, 0x02, 0x00); /* CMDR: Reset the receiver and
1095 transmitter line interface
1096 */
1097
1098 __t13x_framer_set(wc, 0x3a, lim2); /* LIM2: 50% peak amplitude is
1099 a "1" */
1100 __t13x_framer_set(wc, 0x38, 0x0a); /* PCD: LOS after 176
1101 consecutive "zeros" */
1102 __t13x_framer_set(wc, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
1103
1104 if (SPANTYPE_DIGITAL_J1 == wc->span.spantype)
1105 __t13x_framer_set(wc, 0x24, 0x80); /* J1 overide */
1106
1107 /* Generate pulse mask for T1 */
1108 switch (mytxlevel) {
1109 case 3:
1110 __t13x_framer_set(wc, 0x26, 0x07); /* XPM0 */
1111 __t13x_framer_set(wc, 0x27, 0x01); /* XPM1 */
1112 __t13x_framer_set(wc, 0x28, 0x00); /* XPM2 */
1113 break;
1114 case 2:
1115 __t13x_framer_set(wc, 0x26, 0x8c); /* XPM0 */
1116 __t13x_framer_set(wc, 0x27, 0x11); /* XPM1 */
1117 __t13x_framer_set(wc, 0x28, 0x01); /* XPM2 */
1118 break;
1119 case 1:
1120 __t13x_framer_set(wc, 0x26, 0x8c); /* XPM0 */
1121 __t13x_framer_set(wc, 0x27, 0x01); /* XPM1 */
1122 __t13x_framer_set(wc, 0x28, 0x00); /* XPM2 */
1123 break;
1124 case 0:
1125 default:
1126 __t13x_framer_set(wc, 0x26, 0x1a); /* XPM0 */
1127 __t13x_framer_set(wc, 0x27, 0x1f); /* XPM1 */
1128 __t13x_framer_set(wc, 0x28, 0x01); /* XPM2 */
1129 break;
1130 }
1131
1132 __t13x_framer_set(wc, 0x14, 0xff); /* IMR0 */
1133 __t13x_framer_set(wc, 0x15, 0xff); /* IMR1 */
1134
1135 __t13x_framer_set(wc, 0x16, 0x00); /* IMR2: All the alarms */
1136 __t13x_framer_set(wc, 0x17, 0x34); /* IMR3:
1137 ES, SEC, LLBSC, rx slips */
1138 __t13x_framer_set(wc, 0x18, 0x3f); /* IMR4: Slips on transmit */
1139
1140 spin_unlock_irqrestore(&wc->reglock, flags);
1141 dev_info(&wc->xb.pdev->dev, "Span configured for %s/%s\n", framing, line);
1142 }
1143
1144 static void t13x_configure_e1(struct t13x *wc, int lineconfig)
1145 {
1146 unsigned int fmr2, fmr1, fmr0;
1147 unsigned int cas = 0;
1148 unsigned int imr3extra = 0;
1149 char *crc4 = "";
1150 char *framing, *line;
1151 unsigned long flags;
1152
1153 fmr1 = 0x44; /* FMR1: E1 mode, Automatic force resync, PCM30 mode,
1154 8.192 Mhz backplane, no XAIS */
1155 fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of
1156 multiframe recovery, no payload loopback */
1157
1158 if (lineconfig & DAHDI_CONFIG_CRC4) {
1159 fmr1 |= 0x08; /* CRC4 transmit */
1160 fmr2 |= 0xc0; /* CRC4 receive */
1161 crc4 = "/CRC4";
1162 }
1163
1164 spin_lock_irqsave(&wc->reglock, flags);
1165
1166 __t13x_framer_set(wc, 0x1d, fmr1);
1167 __t13x_framer_set(wc, 0x1e, fmr2);
1168
1169 /* Configure line interface */
1170 if (lineconfig & DAHDI_CONFIG_AMI) {
1171 line = "AMI";
1172 fmr0 = 0xa0;
1173 } else {
1174 line = "HDB3";
1175 fmr0 = 0xf0;
1176 }
1177 if (lineconfig & DAHDI_CONFIG_CCS) {
1178 framing = "CCS";
1179 imr3extra = 0x28;
1180 } else {
1181 framing = "CAS";
1182 cas = 0x40;
1183 }
1184 __t13x_framer_set(wc, 0x1c, fmr0);
1185
1186 __t13x_framer_set(wc, 0x37, 0xf0); /* LIM1: Clear data in case of
1187 LOS, Set receiver threshold
1188 (0.5V), No remote loop, no
1189 DRS */
1190 __t13x_framer_set(wc, 0x36, 0x08); /* LIM0: Enable auto long haul
1191 mode, no local loop (must be
1192 after LIM1) */
1193
1194 __t13x_framer_set(wc, 0x02, 0x50); /* CMDR: Reset the receiver and
1195 transmitter line interface
1196 */
1197 __t13x_framer_set(wc, 0x02, 0x00); /* CMDR: Reset the receiver and
1198 transmitter line interface
1199 */
1200
1201 /* Condition receive line interface for E1 after reset */
1202 __t13x_framer_set(wc, 0xbb, 0x17);
1203 __t13x_framer_set(wc, 0xbc, 0x55);
1204 __t13x_framer_set(wc, 0xbb, 0x97);
1205 __t13x_framer_set(wc, 0xbb, 0x11);
1206 __t13x_framer_set(wc, 0xbc, 0xaa);
1207 __t13x_framer_set(wc, 0xbb, 0x91);
1208 __t13x_framer_set(wc, 0xbb, 0x12);
1209 __t13x_framer_set(wc, 0xbc, 0x55);
1210 __t13x_framer_set(wc, 0xbb, 0x92);
1211 __t13x_framer_set(wc, 0xbb, 0x0c);
1212 __t13x_framer_set(wc, 0xbb, 0x00);
1213 __t13x_framer_set(wc, 0xbb, 0x8c);
1214
1215 __t13x_framer_set(wc, 0x3a, 0x20); /* LIM2: 50% peak amplitude is
1216 a "1" */
1217 __t13x_framer_set(wc, 0x38, 0x0a); /* PCD: LOS after 176
1218 consecutive "zeros" */
1219 __t13x_framer_set(wc, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
1220
1221 __t13x_framer_set(wc, 0x20, 0x9f); /* XSW: Spare bits all to 1 */
1222 __t13x_framer_set(wc, 0x21, 0x1c|cas); /* XSP: E-bit set when async.
1223 AXS auto, XSIF to 1 */
1224
1225 /* Generate pulse mask for E1 */
1226 __t13x_framer_set(wc, 0x26, 0x74); /* XPM0 */
1227 __t13x_framer_set(wc, 0x27, 0x02); /* XPM1 */
1228 __t13x_framer_set(wc, 0x28, 0x00); /* XPM2 */
1229
1230 __t13x_framer_set(wc, 0x14, 0xff); /* IMR0 */
1231 __t13x_framer_set(wc, 0x15, 0xff); /* IMR1 */
1232
1233 __t13x_framer_set(wc, 0x16, 0x00); /* IMR2: all the
1234 alarm stuff! */
1235 __t13x_framer_set(wc, 0x17, 0x04 | imr3extra); /* IMR3: AIS */
1236 __t13x_framer_set(wc, 0x18, 0x3f); /* IMR4: slips on
1237 transmit */
1238
1239 spin_unlock_irqrestore(&wc->reglock, flags);
1240 dev_info(&wc->xb.pdev->dev,
1241 "Span configured for %s/%s%s\n", framing, line, crc4);
1242 }
1243
1244 static void t13x_framer_start(struct t13x *wc)
1245 {
1246 if (dahdi_is_e1_span(&wc->span)) {
1247 t13x_configure_e1(wc, wc->span.lineconfig);
1248 } else { /* is a T1 card */
1249 t13x_configure_t1(wc, wc->span.lineconfig, wc->span.txlevel);
1250 }
1251 __t13x_set_clear(wc);
1252
1253 /* Give RCLK a short bit of time to settle */
1254 udelay(1);
1255 set_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags);
1256 }
1257
1258 /**
1259 * te13xp_check_for_interrupts - Return 0 if the card is generating interrupts.
1260 * @wc: The card to check.
1261 *
1262 * If the card is not generating interrupts, this function will also place all
1263 * the spans on the card into red alarm.
1264 *
1265 */
1266 static int te13xp_check_for_interrupts(struct t13x *wc)
1267 {
1268 unsigned int starting_framecount = wc->xb.framecount;
1269 unsigned long stop_time = jiffies + HZ*2;
1270 unsigned long flags;
1271
1272 msleep(20);
1273 spin_lock_irqsave(&wc->reglock, flags);
1274 while (starting_framecount == wc->xb.framecount) {
1275 spin_unlock_irqrestore(&wc->reglock, flags);
1276 if (time_after(jiffies, stop_time)) {
1277 wc->span.alarms = DAHDI_ALARM_RED;
1278 dev_err(&wc->xb.pdev->dev, "Interrupts not detected.\n");
1279 return -EIO;
1280 }
1281 msleep(100);
1282 spin_lock_irqsave(&wc->reglock, flags);
1283 }
1284 spin_unlock_irqrestore(&wc->reglock, flags);
1285
1286 return 0;
1287 }
1288
1289 static bool have_open_channels(const struct t13x *wc)
1290 {
1291 int x, j;
1292 for (x = 0, j = 0; x < wc->span.channels; x++) {
1293 const struct dahdi_chan *chan = wc->span.chans[x];
1294 if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags) ||
1295 dahdi_have_netdev(chan))
1296 return true;
1297 }
1298 return false;
1299 }
1300
1301 static int t13x_startup(struct file *file, struct dahdi_span *span)
1302 {
1303 struct t13x *wc = container_of(span, struct t13x, span);
1304 unsigned long flags;
1305 int ret;
1306
1307 /* Stop the DMA since the clock source may have changed. */
1308 wcxb_stop_dma(&wc->xb);
1309 ret = wcxb_wait_for_stop(&wc->xb, 50);
1310 if (ret) {
1311 dev_err(&wc->xb.pdev->dev, "Timeout waiting for DMA to stop.\n");
1312 return ret;
1313 }
1314
1315 /* Reset framer with proper parameters and start */
1316 t13x_framer_start(wc);
1317
1318 /* Reset span alarm state to RED to prevent false
1319 * temporary GREEN state on span bringup */
1320 span->alarms |= DAHDI_ALARM_RED;
1321
1322 /* Do we want to SYNC on receive or not. This must always happen after
1323 * the framer is fully reset. */
1324 wcxb_set_clksrc(&wc->xb,
1325 (span->syncsrc) ? WCXB_CLOCK_RECOVER : WCXB_CLOCK_SELF);
1326
1327 wcxb_start(&wc->xb);
1328 ret = te13xp_check_for_interrupts(wc);
1329 if (ret)
1330 return ret;
1331
1332 dev_info(&wc->xb.pdev->dev,
1333 "Calling startup (flags is %lu)\n", span->flags);
1334
1335 /* Check for "open channels" here in case some channels have netdev. */
1336 if (have_open_channels(wc))
1337 clear_bit(HAVE_OPEN_CHANNELS, &wc->bit_flags);
1338 else
1339 set_bit(HAVE_OPEN_CHANNELS, &wc->bit_flags);
1340
1341 /* Get this party started */
1342 local_irq_save(flags);
1343 t13x_check_alarms(wc);
1344 t13x_check_sigbits(wc);
1345 local_irq_restore(flags);
1346
1347 return 0;
1348 }
1349
1350 static int t13x_chanconfig(struct file *file,
1351 struct dahdi_chan *chan, int sigtype)
1352 {
1353 struct t13x *wc = chan->pvt;
1354
1355 if (file->f_flags & O_NONBLOCK)
1356 return -EAGAIN;
1357
1358 if (test_bit(DAHDI_FLAGBIT_RUNNING, &chan->span->flags) &&
1359 dahdi_is_t1_span(&wc->span)) {
1360 __t13x_set_clear(wc);
1361 }
1362 return 0;
1363 }
1364
1365 static int t13x_rbsbits(struct dahdi_chan *chan, int bits)
1366 {
1367 u_char m, c;
1368 int n, b;
1369 struct t13x *wc = chan->pvt;
1370 unsigned long flags;
1371
1372 if (dahdi_is_e1_span(&wc->span)) { /* do it E1 way */
1373 if (chan->chanpos == 16)
1374 return 0;
1375
1376 n = chan->chanpos - 1;
1377 if (chan->chanpos > 15)
1378 n--;
1379 b = (n % 15);
1380 spin_lock_irqsave(&wc->reglock, flags);
1381 c = wc->txsigs[b];
1382 m = (n / 15) << 2; /* nibble selector */
1383 c &= (0xf << m); /* keep the other nibble */
1384 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1385 wc->txsigs[b] = c;
1386 /* output them to the chip */
1387 __t13x_framer_set(wc, 0x71 + b, c);
1388 spin_unlock_irqrestore(&wc->reglock, flags);
1389 } else if (wc->span.lineconfig & DAHDI_CONFIG_D4) {
1390 n = chan->chanpos - 1;
1391 b = (n / 4);
1392 spin_lock_irqsave(&wc->reglock, flags);
1393 c = wc->txsigs[b];
1394 m = ((3 - (n % 4)) << 1); /* nibble selector */
1395 c &= ~(0x3 << m); /* keep the other nibble */
1396 c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
1397 wc->txsigs[b] = c;
1398 /* output them to the chip */
1399 __t13x_framer_set(wc, 0x70 + b, c);
1400 __t13x_framer_set(wc, 0x70 + b + 6, c);
1401 spin_unlock_irqrestore(&wc->reglock, flags);
1402 } else if (wc->span.lineconfig & DAHDI_CONFIG_ESF) {
1403 n = chan->chanpos - 1;
1404 b = (n / 2);
1405 spin_lock_irqsave(&wc->reglock, flags);
1406 c = wc->txsigs[b];
1407 m = ((n % 2) << 2); /* nibble selector */
1408 c &= (0xf << m); /* keep the other nibble */
1409 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1410 wc->txsigs[b] = c;
1411 /* output them to the chip */
1412 __t13x_framer_set(wc, 0x70 + b, c);
1413 spin_unlock_irqrestore(&wc->reglock, flags);
1414 }
1415
1416 return 0;
1417 }
1418
1419 static void t13x_check_sigbits(struct t13x *wc)
1420 {
1421 int a, i, rxs;
1422
1423 if (!(test_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags)))
1424 return;
1425 if (dahdi_is_e1_span(&wc->span)) {
1426 for (i = 0; i < 15; i++) {
1427 a = t13x_framer_get(wc, 0x71 + i);
1428 if (a > -1) {
1429 /* Get high channel in low bits */
1430 rxs = (a & 0xf);
1431 if (!(wc->span.chans[i+16]->sig & DAHDI_SIG_CLEAR))
1432 if (wc->span.chans[i+16]->rxsig != rxs)
1433 dahdi_rbsbits(wc->span.chans[i+16], rxs);
1434
1435 rxs = (a >> 4) & 0xf;
1436 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR))
1437 if (wc->span.chans[i]->rxsig != rxs)
1438 dahdi_rbsbits(wc->span.chans[i], rxs);
1439 }
1440 }
1441 } else if (wc->span.lineconfig & DAHDI_CONFIG_D4) {
1442 for (i = 0; i < 24; i += 4) {
1443 a = t13x_framer_get(wc, 0x70 + (i>>2));
1444 if (a > -1) {
1445 /* Get high channel in low bits */
1446 rxs = (a & 0x3) << 2;
1447 if (!(wc->span.chans[i+3]->sig & DAHDI_SIG_CLEAR))
1448 if (wc->span.chans[i+3]->rxsig != rxs)
1449 dahdi_rbsbits(wc->span.chans[i+3], rxs);
1450
1451 rxs = (a & 0xc);
1452 if (!(wc->span.chans[i+2]->sig & DAHDI_SIG_CLEAR))
1453 if (wc->span.chans[i+2]->rxsig != rxs)
1454 dahdi_rbsbits(wc->span.chans[i+2], rxs);
1455
1456 rxs = (a >> 2) & 0xc;
1457 if (!(wc->span.chans[i+1]->sig & DAHDI_SIG_CLEAR))
1458 if (wc->span.chans[i+1]->rxsig != rxs)
1459 dahdi_rbsbits(wc->span.chans[i+1], rxs);
1460
1461 rxs = (a >> 4) & 0xc;
1462 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR))
1463 if (wc->span.chans[i]->rxsig != rxs)
1464 dahdi_rbsbits(wc->span.chans[i], rxs);
1465 }
1466 }
1467 } else {
1468 for (i = 0; i < 24; i += 2) {
1469 a = t13x_framer_get(wc, 0x70 + (i>>1));
1470 if (a > -1) {
1471 /* Get high channel in low bits */
1472 rxs = (a & 0xf);
1473 if (!(wc->span.chans[i+1]->sig & DAHDI_SIG_CLEAR))
1474 if (wc->span.chans[i+1]->rxsig != rxs)
1475 dahdi_rbsbits(wc->span.chans[i+1], rxs);
1476
1477 rxs = (a >> 4) & 0xf;
1478 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR))
1479 if (wc->span.chans[i]->rxsig != rxs)
1480 dahdi_rbsbits(wc->span.chans[i], rxs);
1481 }
1482 }
1483 }
1484 }
1485
1486 static void t13x_reset_counters(struct dahdi_span *span)
1487 {
1488 memset(&span->count, 0, sizeof(span->count));
1489 }
1490
1491 static int t13x_maint(struct dahdi_span *span, int cmd)
1492 {
1493 struct t13x *wc = container_of(span, struct t13x, span);
1494 int reg = 0;
1495 unsigned long flags;
1496
1497 switch (cmd) {
1498 case DAHDI_MAINT_NONE:
1499 dev_info(&wc->xb.pdev->dev, "Clearing all maint modes\n");
1500 t13x_clear_maint(span);
1501 break;
1502 case DAHDI_MAINT_LOCALLOOP:
1503 dev_info(&wc->xb.pdev->dev, "Turning on local loopback\n");
1504 t13x_clear_maint(span);
1505 spin_lock_irqsave(&wc->reglock, flags);
1506 reg = __t13x_framer_get(wc, LIM0);
1507 __t13x_framer_set(wc, LIM0, reg | LIM0_LL);
1508 spin_unlock_irqrestore(&wc->reglock, flags);
1509 break;
1510 case DAHDI_MAINT_NETWORKLINELOOP:
1511 dev_info(&wc->xb.pdev->dev,
1512 "Turning on network line loopback\n");
1513 t13x_clear_maint(span);
1514 spin_lock_irqsave(&wc->reglock, flags);
1515 reg = __t13x_framer_get(wc, LIM1);
1516 __t13x_framer_set(wc, LIM1, reg | LIM1_RL);
1517 spin_unlock_irqrestore(&wc->reglock, flags);
1518 break;
1519 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
1520 dev_info(&wc->xb.pdev->dev,
1521 "Turning on network payload loopback\n");
1522 t13x_clear_maint(span);
1523 spin_lock_irqsave(&wc->reglock, flags);
1524 reg = __t13x_framer_get(wc, LIM1);
1525 __t13x_framer_set(wc, LIM1, reg | (LIM1_RL | LIM1_JATT));
1526 spin_unlock_irqrestore(&wc->reglock, flags);
1527 break;
1528 case DAHDI_MAINT_LOOPUP:
1529 dev_info(&wc->xb.pdev->dev, "Transmitting loopup code\n");
1530 t13x_clear_maint(span);
1531 if (dahdi_is_e1_span(&wc->span)) {
1532 spin_lock_irqsave(&wc->reglock, flags);
1533 reg = __t13x_framer_get(wc, FMR3_E);
1534 __t13x_framer_set(wc, FMR3_E, reg | LOOPUP);
1535 spin_unlock_irqrestore(&wc->reglock, flags);
1536 } else {
1537 spin_lock_irqsave(&wc->reglock, flags);
1538 reg = __t13x_framer_get(wc, FMR3_T);
1539 __t13x_framer_set(wc, FMR3_T, reg | LOOPUP);
1540 spin_unlock_irqrestore(&wc->reglock, flags);
1541 }
1542 break;
1543 case DAHDI_MAINT_LOOPDOWN:
1544 dev_info(&wc->xb.pdev->dev, "Transmitting loopdown code\n");
1545 t13x_clear_maint(span);
1546 if (dahdi_is_e1_span(&wc->span)) {
1547 spin_lock_irqsave(&wc->reglock, flags);
1548 reg = __t13x_framer_get(wc, FMR3_E);
1549 __t13x_framer_set(wc, FMR3_E, reg | LOOPDOWN);
1550 spin_unlock_irqrestore(&wc->reglock, flags);
1551 } else {
1552 spin_lock_irqsave(&wc->reglock, flags);
1553 reg = __t13x_framer_get(wc, FMR3_T);
1554 __t13x_framer_set(wc, FMR3_T, reg | LOOPDOWN);
1555 spin_unlock_irqrestore(&wc->reglock, flags);
1556 }
1557 break;
1558 case DAHDI_MAINT_FAS_DEFECT:
1559 t13x_framer_set(wc, IERR_T, IFASE);
1560 break;
1561 case DAHDI_MAINT_MULTI_DEFECT:
1562 t13x_framer_set(wc, IERR_T, IMFE);
1563 break;
1564 case DAHDI_MAINT_CRC_DEFECT:
1565 t13x_framer_set(wc, IERR_T, ICRCE);
1566 break;
1567 case DAHDI_MAINT_CAS_DEFECT:
1568 t13x_framer_set(wc, IERR_T, ICASE);
1569 break;
1570 case DAHDI_MAINT_PRBS_DEFECT:
1571 t13x_framer_set(wc, IERR_T, IPE);
1572 break;
1573 case DAHDI_MAINT_BIPOLAR_DEFECT:
1574 t13x_framer_set(wc, IERR_T, IBV);
1575 break;
1576 case DAHDI_RESET_COUNTERS:
1577 t13x_reset_counters(span);
1578 break;
1579 default:
1580 dev_info(&wc->xb.pdev->dev,
1581 "Unknown maint command: %d\n", cmd);
1582 return -ENOSYS;
1583 }
1584
1585 /* update DAHDI_ALARM_LOOPBACK status bit and check timing source */
1586 spin_lock_irqsave(&wc->reglock, flags);
1587 if (!span->maintstat)
1588 span->alarms &= ~DAHDI_ALARM_LOOPBACK;
1589 dahdi_alarm_notify(span);
1590 spin_unlock_irqrestore(&wc->reglock, flags);
1591
1592 return 0;
1593 }
1594
1595 static int t13x_clear_maint(struct dahdi_span *span)
1596 {
1597 struct t13x *wc = container_of(span, struct t13x, span);
1598 int reg = 0;
1599 unsigned long flags;
1600
1601 /* Turn off local loop */
1602 spin_lock_irqsave(&wc->reglock, flags);
1603 reg = __t13x_framer_get(wc, LIM0);
1604 __t13x_framer_set(wc, LIM0, reg & ~LIM0_LL);
1605
1606 /* Turn off remote loop & jitter attenuator */
1607 reg = __t13x_framer_get(wc, LIM1);
1608 __t13x_framer_set(wc, LIM1, reg & ~(LIM1_RL | LIM1_JATT));
1609
1610 /* Clear loopup/loopdown signals on the line */
1611 if (dahdi_is_e1_span(&wc->span)) {
1612 reg = __t13x_framer_get(wc, FMR3_E);
1613 __t13x_framer_set(wc, FMR3_E, reg & ~(LOOPDOWN | LOOPUP));
1614 } else {
1615 reg = __t13x_framer_get(wc, FMR3_T);
1616 __t13x_framer_set(wc, FMR3_T, reg & ~(LOOPDOWN | LOOPUP));
1617 }
1618
1619 spin_unlock_irqrestore(&wc->reglock, flags);
1620
1621 return 0;
1622 }
1623
1624
1625 static int t13x_ioctl(struct dahdi_chan *chan, unsigned int cmd,
1626 unsigned long data)
1627 {
1628 struct t4_regs regs;
1629 unsigned int x;
1630 struct t13x *wc;
1631
1632 switch (cmd) {
1633 case WCT4_GET_REGS:
1634 wc = chan->pvt;
1635 for (x = 0; x < sizeof(regs.regs) / sizeof(regs.regs[0]); x++)
1636 regs.regs[x] = t13x_framer_get(wc, x);
1637
1638 if (copy_to_user((void __user *) data, &regs, sizeof(regs)))
1639 return -EFAULT;
1640 break;
1641 default:
1642 return -ENOTTY;
1643 }
1644 return 0;
1645 }
1646
1647 static void t13x_chan_set_sigcap(struct dahdi_span *span, int x)
1648 {
1649 struct t13x *wc = container_of(span, struct t13x, span);
1650 struct dahdi_chan *chan = wc->chans[x];
1651 chan->sigcap = DAHDI_SIG_CLEAR;
1652 /* E&M variant supported depends on span type */
1653 if (dahdi_is_e1_span(&wc->span)) {
1654 /* E1 sigcap setup */
1655 if (span->lineconfig & DAHDI_CONFIG_CCS) {
1656 /* CCS setup */
1657 chan->sigcap |= DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
1658 return;
1659 }
1660 /* clear out sig and sigcap for channel 16 on E1 CAS
1661 * lines, otherwise, set it correctly */
1662 if (x == 15) {
1663 /* CAS signaling channel setup */
1664 wc->chans[15]->sigcap = 0;
1665 wc->chans[15]->sig = 0;
1666 return;
1667 }
1668 /* normal CAS setup */
1669 chan->sigcap |= DAHDI_SIG_EM_E1 | DAHDI_SIG_FXSLS |
1670 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_SF |
1671 DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS |
1672 DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
1673 } else {
1674 /* T1 sigcap setup */
1675 chan->sigcap |= DAHDI_SIG_EM | DAHDI_SIG_FXSLS |
1676 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_MTP2 |
1677 DAHDI_SIG_SF | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
1678 DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
1679 }
1680 }
1681
1682 static bool t13x_lineconfig_changed(struct t13x *wc,
1683 const struct dahdi_lineconfig *lc)
1684 {
1685 unsigned long flags;
1686 bool result;
1687 u32 crc = crc32(~0, lc, sizeof(*lc));
1688
1689 spin_lock_irqsave(&wc->reglock, flags);
1690 result = (crc != wc->lineconfig_fingerprint);
1691 spin_unlock_irqrestore(&wc->reglock, flags);
1692 return result;
1693 }
1694
1695 static void t13x_save_lineconfig(struct t13x *wc,
1696 const struct dahdi_lineconfig *lc)
1697 {
1698 unsigned long flags;
1699 u32 crc = crc32(~0, lc, sizeof(*lc));
1700
1701 spin_lock_irqsave(&wc->reglock, flags);
1702 wc->lineconfig_fingerprint = crc;
1703 spin_unlock_irqrestore(&wc->reglock, flags);
1704 }
1705
1706 static int
1707 t13x_spanconfig(struct file *file, struct dahdi_span *span,
1708 struct dahdi_lineconfig *lc)
1709 {
1710 struct t13x *wc = container_of(span, struct t13x, span);
1711 int i;
1712 int res = 0;
1713
1714 if (file->f_flags & O_NONBLOCK)
1715 return -EAGAIN;
1716
1717 /* Set recover timing mode */
1718 span->syncsrc = lc->sync;
1719
1720 /* make sure that sigcaps gets updated if necessary */
1721 for (i = 0; i < wc->span.channels; i++)
1722 t13x_chan_set_sigcap(span, i);
1723
1724 /* If already running, apply changes immediately */
1725 if (test_bit(DAHDI_FLAGBIT_RUNNING, &span->flags) &&
1726 t13x_lineconfig_changed(wc, lc)) {
1727 res = t13x_startup(file, span);
1728 if (!res)
1729 t13x_save_lineconfig(wc, lc);
1730 }
1731 return res;
1732 }
1733
1734 /**
1735 * t13x_software_init - Initialize the board for the given type.
1736 * @wc: The board to initialize.
1737 * @type: The type of board we are, T1 / E1
1738 *
1739 * This function is called at startup and when the type of the span is changed
1740 * via the dahdi_device before the span is assigned a number.
1741 *
1742 */
1743 static int t13x_software_init(struct t13x *wc, enum linemode type)
1744 {
1745 int x;
1746 struct dahdi_chan *chans[32] = {NULL,};
1747 struct dahdi_echocan_state *ec[32] = {NULL,};
1748 unsigned long flags;
1749 int res = 0;
1750
1751 /* We may already be setup properly. */
1752 if (wc->span.channels == ((E1 == type) ? 31 : 24))
1753 return 0;
1754
1755 for (x = 0; x < ((E1 == type) ? 31 : 24); x++) {
1756 chans[x] = kzalloc(sizeof(*chans[x]), GFP_KERNEL);
1757 ec[x] = kzalloc(sizeof(*ec[x]), GFP_KERNEL);
1758 if (!chans[x] || !ec[x])
1759 goto error_exit;
1760 }
1761
1762 /* Because the interrupt handler is running, we need to atomically
1763 * swap the channel arrays. */
1764 spin_lock_irqsave(&wc->reglock, flags);
1765 _t13x_free_channels(wc);
1766 memcpy(wc->chans, chans, sizeof(wc->chans));
1767 memcpy(wc->ec, ec, sizeof(wc->ec));
1768 memset(chans, 0, sizeof(chans));
1769 memset(ec, 0, sizeof(ec));
1770
1771 switch (type) {
1772 case E1:
1773 wc->span.channels = 31;
1774 wc->span.spantype = SPANTYPE_DIGITAL_E1;
1775 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3 |
1776 DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
1777 wc->span.deflaw = DAHDI_LAW_ALAW;
1778 break;
1779 case T1:
1780 wc->span.channels = 24;
1781 wc->span.spantype = SPANTYPE_DIGITAL_T1;
1782 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
1783 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
1784 wc->span.deflaw = DAHDI_LAW_MULAW;
1785 break;
1786 case J1:
1787 wc->span.channels = 24;
1788 wc->span.spantype = SPANTYPE_DIGITAL_J1;
1789 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
1790 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
1791 wc->span.deflaw = DAHDI_LAW_MULAW;
1792 break;
1793 default:
1794 spin_unlock_irqrestore(&wc->reglock, flags);
1795 res = -EINVAL;
1796 goto error_exit;
1797 }
1798
1799 spin_unlock_irqrestore(&wc->reglock, flags);
1800
1801 dev_info(&wc->xb.pdev->dev, "Setting up global serial parameters for %s\n",
1802 dahdi_spantype2str(wc->span.spantype));
1803
1804 t13x_serial_setup(wc);
1805 set_bit(DAHDI_FLAGBIT_RBS, &wc->span.flags);
1806 for (x = 0; x < wc->span.channels; x++) {
1807 snprintf(wc->chans[x]->name, sizeof(wc->chans[x]->name),
1808 "%s/%d", wc->span.name, x + 1);
1809 t13x_chan_set_sigcap(&wc->span, x);
1810 wc->chans[x]->pvt = wc;
1811 wc->chans[x]->chanpos = x + 1;
1812 }
1813
1814 return 0;
1815
1816 error_exit:
1817 for (x = 0; x < ARRAY_SIZE(chans); ++x) {
1818 kfree(chans[x]);
1819 kfree(ec[x]);
1820 }
1821 return res;
1822 }
1823
1824 /**
1825 * t13x_set_linemode - Change the type of span before assignment.
1826 * @span: The span to change.
1827 * @linemode: Text string for the line mode.
1828 *
1829 * This function may be called after the dahdi_device is registered but
1830 * before the spans are assigned numbers (and are visible to the rest of
1831 * DAHDI).
1832 *
1833 */
1834 static int t13x_set_linemode(struct dahdi_span *span, enum spantypes linemode)
1835 {
1836 int res;
1837 struct t13x *wc = container_of(span, struct t13x, span);
1838
1839 /* We may already be set to the requested type. */
1840 if (span->spantype == linemode) {
1841 span->alarms = DAHDI_ALARM_NONE;
1842 return 0;
1843 }
1844
1845 mutex_lock(&wc->lock);
1846
1847 /* Stop the processing of the channels since we're going to change
1848 * them. */
1849 clear_bit(INITIALIZED, &wc->bit_flags);
1850 disable_irq(wc->xb.pdev->irq);
1851
1852 smp_mb__after_atomic();
•1853 timer_delete_sync(&wc->timer);
1854 flush_workqueue(wc->wq);
1855
1856 t13x_framer_reset(wc);
1857 t13x_serial_setup(wc);
1858
1859 switch (linemode) {
1860 case SPANTYPE_DIGITAL_T1:
1861 dev_info(&wc->xb.pdev->dev,
1862 "Changing from %s to T1 line mode.\n",
1863 dahdi_spantype2str(wc->span.spantype));
1864 res = t13x_software_init(wc, T1);
1865 break;
1866 case SPANTYPE_DIGITAL_E1:
1867 dev_info(&wc->xb.pdev->dev,
1868 "Changing from %s to E1 line mode.\n",
1869 dahdi_spantype2str(wc->span.spantype));
1870 res = t13x_software_init(wc, E1);
1871 break;
1872 case SPANTYPE_DIGITAL_J1:
1873 dev_info(&wc->xb.pdev->dev,
1874 "Changing from %s to E1 line mode.\n",
1875 dahdi_spantype2str(wc->span.spantype));
1876 res = t13x_software_init(wc, J1);
1877 break;
1878 default:
1879 dev_err(&wc->xb.pdev->dev,
1880 "Got invalid linemode '%s' from dahdi\n",
1881 dahdi_spantype2str(linemode));
1882 res = -EINVAL;
1883 }
1884
1885 /* Since we probably reallocated the channels we need to make
1886 * sure they are configured before setting INITIALIZED again. */
1887 if (!res) {
1888 dahdi_init_span(span);
1889 set_bit(INITIALIZED, &wc->bit_flags);
1890 mod_timer(&wc->timer, jiffies + HZ/5);
1891 }
1892 wcxb_lock_latency(&wc->xb);
1893 enable_irq(wc->xb.pdev->irq);
1894 mutex_unlock(&wc->lock);
1895 msleep(10);
1896 wcxb_unlock_latency(&wc->xb);
1897 return res;
1898 }
1899
1900 static int t13x_hardware_post_init(struct t13x *wc, enum linemode *type)
1901 {
1902 int reg;
1903 int x;
1904
1905 if (!strcasecmp(default_linemode, "e1")) {
1906 *type = E1;
1907 } else if (!strcasecmp(default_linemode, "t1")) {
1908 *type = T1;
1909 } else if (!strcasecmp(default_linemode, "j1")) {
1910 *type = J1;
1911 } else {
1912 dev_warn(&wc->xb.pdev->dev,
1913 "'%s' is an unknown linemode. Defaulting to 't1'\n",
1914 default_linemode);
1915 *type = T1;
1916 }
1917
1918 if (debug) {
1919 dev_info(&wc->xb.pdev->dev, "linemode: %s\n",
1920 (*type == T1) ? "T1" : ((J1 == *type) ? "J1" : "E1"));
1921 }
1922
1923 /* what version of the FALC are we using? */
1924 wcxb_gpio_set(&wc->xb, FALC_CPU_RESET);
1925 reg = t13x_framer_get(wc, 0x4a);
1926 if (reg < 0) {
1927 dev_info(&wc->xb.pdev->dev,
1928 "Failed to read FALC version (%x)\n", reg);
1929 return -EIO;
1930 }
1931 dev_info(&wc->xb.pdev->dev, "FALC version: %1x\n", reg);
1932
1933 /* make sure reads and writes work */
1934 for (x = 0; x < 256; x++) {
1935 t13x_framer_set(wc, 0x14, x);
1936 reg = t13x_framer_get(wc, 0x14);
1937 if (reg < 0) {
1938 dev_info(&wc->xb.pdev->dev,
1939 "Failed register read (%d)\n", reg);
1940 return -EIO;
1941 }
1942 if (reg != x) {
1943 dev_info(&wc->xb.pdev->dev,
1944 "Register test failed. Wrote '%x' but read '%x'\n",
1945 x, reg);
1946 return -EIO;
1947 }
1948 }
1949
1950 /* Enable all the GPIO outputs. */
1951 t13x_setleds(wc, wc->ledstate);
1952
1953 return 0;
1954 }
1955
1956 static void t13x_check_alarms(struct t13x *wc)
1957 {
1958 unsigned char c, d;
1959 int alarms;
1960
1961 if (!(test_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags)))
1962 return;
1963
1964 spin_lock(&wc->reglock);
1965
1966 c = __t13x_framer_get(wc, 0x4c);
1967 d = __t13x_framer_get(wc, 0x4d);
1968
1969 /* start with existing span alarms */
1970 alarms = wc->span.alarms;
1971
1972 if (dahdi_is_e1_span(&wc->span)) {
1973 if (c & 0x04) {
1974 /* No multiframe found, force RAI high after 400ms only
1975 * if we haven't found a multiframe since last loss of
1976 * frame */
1977 if (!wc->flags.nmf) {
1978 /* LIM0: Force RAI High */
1979 __t13x_framer_set(wc, 0x20, 0x9f | 0x20);
1980 wc->flags.nmf = 1;
1981 dev_info(&wc->xb.pdev->dev, "NMF workaround on!\n");
1982 }
1983 __t13x_framer_set(wc, 0x1e, 0xc3); /* Reset to CRC4 mode */
1984 __t13x_framer_set(wc, 0x1c, 0xf2); /* Force Resync */
1985 __t13x_framer_set(wc, 0x1c, 0xf0); /* Force Resync */
1986 } else if (!(c & 0x02)) {
1987 if (wc->flags.nmf) {
1988 /* LIM0: Clear forced RAI */
1989 __t13x_framer_set(wc, 0x20, 0x9f);
1990 wc->flags.nmf = 0;
1991 dev_info(&wc->xb.pdev->dev,
1992 "NMF workaround off!\n");
1993 }
1994 }
1995 }
1996
1997 if (wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
1998 if (!test_bit(HAVE_OPEN_CHANNELS, &wc->bit_flags))
1999 alarms |= DAHDI_ALARM_NOTOPEN;
2000 else
2001 alarms &= ~DAHDI_ALARM_NOTOPEN;
2002 }
2003
2004 if (c & 0x20) { /* LOF/LFA */
2005 if (!(alarms & DAHDI_ALARM_RED) && (0 == wc->lofalarmtimer))
2006 wc->lofalarmtimer = (jiffies + msecs_to_jiffies(alarmdebounce)) ?: 1;
2007 } else {
2008 wc->lofalarmtimer = 0;
2009 }
2010
2011 if (c & 0x80) { /* LOS */
2012 if (!(alarms & DAHDI_ALARM_RED) && (0 == wc->losalarmtimer))
2013 wc->losalarmtimer = (jiffies + msecs_to_jiffies(losalarmdebounce)) ?: 1;
2014 } else {
2015 wc->losalarmtimer = 0;
2016 }
2017
2018 if (!(c & (0x80|0x20)))
2019 alarms &= ~DAHDI_ALARM_RED;
2020
2021 if (c & 0x40) { /* AIS */
2022 if (!(alarms & DAHDI_ALARM_BLUE) && (0 == wc->aisalarmtimer))
2023 wc->aisalarmtimer = (jiffies + msecs_to_jiffies(aisalarmdebounce)) ?: 1;
2024 } else {
2025 wc->aisalarmtimer = 0;
2026 alarms &= ~DAHDI_ALARM_BLUE;
2027 }
2028
2029 /* Keep track of recovering */
2030 if (alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE|DAHDI_ALARM_NOTOPEN)) {
2031 wc->recoverytimer = 0;
2032 alarms &= ~DAHDI_ALARM_RECOVER;
2033 } else if (wc->span.alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE)) {
2034 if (0 == wc->recoverytimer) {
2035 wc->recoverytimer = (jiffies + 5*HZ) ?: 1;
2036 alarms |= DAHDI_ALARM_RECOVER;
2037 }
2038 }
2039
2040 if (c & 0x10) { /* receiving yellow (RAI) */
2041 if (!(alarms & DAHDI_ALARM_YELLOW) && (0 == wc->yelalarmtimer))
2042 wc->yelalarmtimer = (jiffies + msecs_to_jiffies(yelalarmdebounce)) ?: 1;
2043 } else {
2044 wc->yelalarmtimer = 0;
2045 alarms &= ~DAHDI_ALARM_YELLOW;
2046 }
2047
2048 if (wc->span.alarms != alarms) {
2049 wc->span.alarms = alarms;
2050 spin_unlock(&wc->reglock);
2051 dahdi_alarm_notify(&wc->span);
2052 } else {
2053 spin_unlock(&wc->reglock);
2054 }
2055 }
2056
2057 static void t13x_check_loopcodes(struct t13x *wc)
2058 {
2059 unsigned char frs1;
2060
2061 frs1 = t13x_framer_get(wc, 0x4d);
2062
2063 /* Detect loopup code if we're not sending one */
2064 if ((wc->span.maintstat != DAHDI_MAINT_LOOPUP) && (frs1 & 0x08)) {
2065 /* Loop-up code detected */
2066 if ((wc->span.maintstat != DAHDI_MAINT_REMOTELOOP) &&
2067 (0 == wc->loopuptimer))
2068 wc->loopuptimer = (jiffies + msecs_to_jiffies(800)) ?: 1;
2069 } else {
2070 wc->loopuptimer = 0;
2071 }
2072
2073 /* Same for loopdown code */
2074 if ((wc->span.maintstat != DAHDI_MAINT_LOOPDOWN) && (frs1 & 0x10)) {
2075 /* Loop-down code detected */
2076 if ((wc->span.maintstat == DAHDI_MAINT_REMOTELOOP) &&
2077 (0 == wc->loopdntimer))
2078 wc->loopdntimer = (jiffies + msecs_to_jiffies(800)) ?: 1;
2079 } else {
2080 wc->loopdntimer = 0;
2081 }
2082 }
2083
2084 static void t13x_debounce_alarms(struct t13x *wc)
2085 {
2086 int alarms;
2087 unsigned long flags;
2088 unsigned int fmr4;
2089
2090 spin_lock_irqsave(&wc->reglock, flags);
2091 alarms = wc->span.alarms;
2092
2093 if (wc->lofalarmtimer && time_after(jiffies, wc->lofalarmtimer)) {
2094 alarms |= DAHDI_ALARM_RED;
2095 wc->lofalarmtimer = 0;
2096 dev_info(&wc->xb.pdev->dev, "LOF alarm detected\n");
2097 }
2098
2099 if (wc->losalarmtimer && time_after(jiffies, wc->losalarmtimer)) {
2100 alarms |= DAHDI_ALARM_RED;
2101 wc->losalarmtimer = 0;
2102 dev_info(&wc->xb.pdev->dev, "LOS alarm detected\n");
2103 }
2104
2105 if (wc->aisalarmtimer && time_after(jiffies, wc->aisalarmtimer)) {
2106 alarms |= DAHDI_ALARM_BLUE;
2107 wc->aisalarmtimer = 0;
2108 dev_info(&wc->xb.pdev->dev, "AIS alarm detected\n");
2109 }
2110
2111 if (wc->yelalarmtimer && time_after(jiffies, wc->yelalarmtimer)) {
2112 alarms |= DAHDI_ALARM_YELLOW;
2113 wc->yelalarmtimer = 0;
2114 dev_info(&wc->xb.pdev->dev, "YEL alarm detected\n");
2115 }
2116
2117 if (wc->recoverytimer && time_after(jiffies, wc->recoverytimer)) {
2118 alarms &= ~(DAHDI_ALARM_RECOVER);
2119 wc->recoverytimer = 0;
2120 dev_info(&wc->xb.pdev->dev, "Alarms cleared\n");
2121 }
2122
2123 if (alarms != wc->span.alarms) {
2124 wc->span.alarms = alarms;
2125 spin_unlock_irqrestore(&wc->reglock, flags);
2126 dahdi_alarm_notify(&wc->span);
2127 spin_lock_irqsave(&wc->reglock, flags);
2128 }
2129
2130 /* If receiving alarms (except Yellow), go into Yellow alarm state */
2131 if (alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE|
2132 DAHDI_ALARM_NOTOPEN|DAHDI_ALARM_RECOVER)) {
2133 if (!wc->flags.sendingyellow) {
2134 dev_info(&wc->xb.pdev->dev, "Setting yellow alarm\n");
2135 /* We manually do yellow alarm to handle RECOVER
2136 * and NOTOPEN, otherwise it's auto anyway */
2137 fmr4 = __t13x_framer_get(wc, 0x20);
2138 __t13x_framer_set(wc, 0x20, fmr4 | 0x20);
2139 wc->flags.sendingyellow = 1;
2140 }
2141 } else {
2142 if (wc->flags.sendingyellow) {
2143 dev_info(&wc->xb.pdev->dev, "Clearing yellow alarm\n");
2144 /* We manually do yellow alarm to handle RECOVER */
2145 fmr4 = __t13x_framer_get(wc, 0x20);
2146 __t13x_framer_set(wc, 0x20, fmr4 & ~0x20);
2147 wc->flags.sendingyellow = 0;
2148 }
2149 }
2150
2151 spin_unlock_irqrestore(&wc->reglock, flags);
2152 }
2153
2154 static void t13x_debounce_loopcodes(struct t13x *wc)
2155 {
2156 unsigned long flags;
2157
2158 spin_lock_irqsave(&wc->reglock, flags);
2159 if (wc->loopuptimer && time_after(jiffies, wc->loopuptimer)) {
2160 /* Loop-up code debounced */
2161 dev_info(&wc->xb.pdev->dev, "Loopup detected, enabling remote loop\n");
2162 __t13x_framer_set(wc, 0x36, 0x08); /* LIM0: Disable
2163 any local loop */
2164 __t13x_framer_set(wc, 0x37, 0xf6); /* LIM1: Enable
2165 remote loop */
2166 wc->span.maintstat = DAHDI_MAINT_REMOTELOOP;
2167 wc->loopuptimer = 0;
2168 spin_unlock_irqrestore(&wc->reglock, flags);
2169 dahdi_alarm_notify(&wc->span);
2170 spin_lock_irqsave(&wc->reglock, flags);
2171 }
2172
2173 if (wc->loopdntimer && time_after(jiffies, wc->loopdntimer)) {
2174 /* Loop-down code debounced */
2175 dev_info(&wc->xb.pdev->dev, "Loopdown detected, disabling remote loop\n");
2176 __t13x_framer_set(wc, 0x36, 0x08); /* LIM0: Disable
2177 any local loop */
2178 __t13x_framer_set(wc, 0x37, 0xf0); /* LIM1: Disable
2179 remote loop */
2180 wc->span.maintstat = DAHDI_MAINT_NONE;
2181 wc->loopdntimer = 0;
2182 spin_unlock_irqrestore(&wc->reglock, flags);
2183 dahdi_alarm_notify(&wc->span);
2184 spin_lock_irqsave(&wc->reglock, flags);
2185 }
2186 spin_unlock_irqrestore(&wc->reglock, flags);
2187 }
2188
2189 static void handle_leds(struct t13x *wc)
2190 {
2191 u32 led;
2192 unsigned long flags;
2193 #define SET_LED_RED(a) ((a | STATUS_LED_RED) & ~STATUS_LED_GREEN)
2194 #define SET_LED_GREEN(a) ((a | STATUS_LED_GREEN) & ~STATUS_LED_RED)
2195 #define UNSET_LED_REDGREEN(a) (a & ~(STATUS_LED_RED | STATUS_LED_GREEN))
2196 #define SET_LED_YELLOW(a) (a | STATUS_LED_RED | STATUS_LED_GREEN)
2197
2198 led = wc->ledstate;
2199
2200 if ((wc->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE)) ||
2201 wc->losalarmtimer) {
2202 /* When we're in red alarm, blink the led once a second. */
2203 if (time_after(jiffies, wc->blinktimer)) {
2204 led = (led & STATUS_LED_RED) ? UNSET_LED_REDGREEN(led) :
2205 SET_LED_RED(led);
2206 }
2207 } else if (wc->span.alarms & DAHDI_ALARM_YELLOW) {
2208 led = SET_LED_YELLOW(led);
2209 } else {
2210 if (test_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags))
2211 led = SET_LED_GREEN(led);
2212 else
2213 led = UNSET_LED_REDGREEN(led);
2214 }
2215
2216 if (led != wc->ledstate) {
2217 wc->blinktimer = jiffies + HZ/2;
2218 /* TODO: set ledstate in t13x_setleds() */
2219 spin_lock_irqsave(&wc->reglock, flags);
2220 wc->ledstate = led;
2221 spin_unlock_irqrestore(&wc->reglock, flags);
2222 t13x_setleds(wc, led);
2223 }
2224 }
2225
2226 static void te13x_handle_receive(struct wcxb *xb, void *vfp)
2227 {
2228 int i, j;
2229 u_char *frame = (u_char *) vfp;
2230 struct t13x *wc = container_of(xb, struct t13x, xb);
2231
2232 for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
2233 for (i = 0; i < wc->span.channels; i++) {
2234 wc->chans[i]->readchunk[j] =
2235 frame[j*DMA_CHAN_SIZE+(1+i*4)];
2236 }
2237 }
2238
2239 if (!vpmsupport || !wc->vpm) {
2240 for (i = 0; i < wc->span.channels; i++) {
2241 struct dahdi_chan *const c = wc->span.chans[i];
2242 __dahdi_ec_chunk(c, c->readchunk, c->readchunk,
2243 c->writechunk);
2244 }
2245 }
2246
2247 _dahdi_receive(&wc->span);
2248 }
2249
2250 static void te13x_handle_transmit(struct wcxb *xb, void *vfp)
2251 {
2252 int i, j;
2253 u_char *frame = (u_char *) vfp;
2254 struct t13x *wc = container_of(xb, struct t13x, xb);
2255
2256 _dahdi_transmit(&wc->span);
2257
2258 for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
2259 for (i = 0; i < wc->span.channels; i++) {
2260 frame[j*DMA_CHAN_SIZE+(1+i*4)] =
2261 wc->chans[i]->writechunk[j];
2262 }
2263 }
2264 }
2265
2266 #define SPAN_DEBOUNCE \
2267 (wc->lofalarmtimer || wc->losalarmtimer || \
2268 wc->aisalarmtimer || wc->yelalarmtimer || \
2269 wc->recoverytimer || \
2270 wc->loopuptimer || wc->loopdntimer)
2271
2272 #define SPAN_ALARMS \
2273 (wc->span.alarms & ~DAHDI_ALARM_NOTOPEN)
2274
2275 static void timer_work_func(struct work_struct *work)
2276 {
2277 struct t13x *wc = container_of(work, struct t13x, timer_work);
2278 static int work_count;
2279
2280 if (debug)
2281 dev_notice(&wc->xb.pdev->dev, "Timer work: %d!\n",
2282 ++work_count);
2283
2284 t13x_debounce_alarms(wc);
2285 if (!dahdi_is_e1_span(&wc->span))
2286 t13x_debounce_loopcodes(wc);
2287 handle_leds(wc);
2288 if (test_bit(INITIALIZED, &wc->bit_flags)) {
2289 if (SPAN_DEBOUNCE || SPAN_ALARMS)
2290 mod_timer(&wc->timer, jiffies + HZ/10);
2291 }
2292 }
2293
2294 static void handle_falc_int(struct t13x *wc)
2295 {
2296 unsigned char gis, isr0, isr1, isr2, isr3, isr4;
2297 unsigned long flags;
2298 static int intcount;
2299 bool start_timer;
2300 bool recheck_sigbits = false;
2301
2302 intcount++;
2303 start_timer = FALSE;
2304
2305 spin_lock_irqsave(&wc->reglock, flags);
2306 gis = __t13x_framer_get(wc, FRMR_GIS);
2307 isr0 = (gis & FRMR_GIS_ISR0) ? __t13x_framer_get(wc, FRMR_ISR0) : 0;
2308 isr1 = (gis & FRMR_GIS_ISR1) ? __t13x_framer_get(wc, FRMR_ISR1) : 0;
2309 isr2 = (gis & FRMR_GIS_ISR2) ? __t13x_framer_get(wc, FRMR_ISR2) : 0;
2310 isr3 = (gis & FRMR_GIS_ISR3) ? __t13x_framer_get(wc, FRMR_ISR3) : 0;
2311 isr4 = (gis & FRMR_GIS_ISR4) ? __t13x_framer_get(wc, FRMR_ISR4) : 0;
2312
2313 if ((debug) && !(isr3 & ISR3_SEC)) {
2314 dev_info(&wc->xb.pdev->dev, "gis: %02x, isr0: %02x, isr1: %02x, "\
2315 "isr2: %02x, isr3: %02x, isr4: %02x, intcount=%u\n",
2316 gis, isr0, isr1, isr2, isr3, isr4, intcount);
2317 }
2318
2319 /* Collect performance counters once per second */
2320 if (isr3 & ISR3_SEC) {
2321 wc->span.count.fe += __t13x_framer_get(wc, FECL_T);
2322 wc->span.count.crc4 += __t13x_framer_get(wc, CEC1L_T);
2323 wc->span.count.cv += __t13x_framer_get(wc, CVCL_T);
2324 wc->span.count.ebit += __t13x_framer_get(wc, EBCL_T);
2325 wc->span.count.be += __t13x_framer_get(wc, BECL_T);
2326 wc->span.count.prbs = __t13x_framer_get(wc, FRS1_T);
2327
2328 if (DAHDI_RXSIG_INITIAL == wc->span.chans[0]->rxhooksig)
2329 recheck_sigbits = true;
2330 }
2331 spin_unlock_irqrestore(&wc->reglock, flags);
2332
2333 /* Collect errored second counter once per second */
2334 if (isr3 & ISR3_ES)
2335 wc->span.count.errsec += 1;
2336
2337 if ((isr0 & 0x08) || recheck_sigbits)
2338 t13x_check_sigbits(wc);
2339
2340 if (dahdi_is_e1_span(&wc->span)) {
2341 /* E1 checks */
2342 if ((isr3 & 0x68) || isr2 || (isr1 & 0x7f)) {
2343 t13x_check_alarms(wc);
2344 start_timer = TRUE;
2345 }
2346 } else {
2347 /* T1 checks */
2348 if (isr2) {
2349 t13x_check_alarms(wc);
2350 start_timer = TRUE;
2351 }
2352 if (isr3 & 0x08) { /* T1 LLBSC */
2353 t13x_check_loopcodes(wc);
2354 start_timer = TRUE;
2355 }
2356 }
2357
2358 if (!wc->span.alarms) {
2359 if ((isr3 & 0x3) || (isr4 & 0xc0))
2360 wc->span.count.timingslips++;
2361 } else
2362 wc->span.count.timingslips = 0;
2363
2364 if (start_timer && !timer_pending(&wc->timer) &&
2365 test_bit(INITIALIZED, &wc->bit_flags)) {
2366 mod_timer(&wc->timer, jiffies + HZ/10);
2367 }
2368 }
2369
2370 static void te13x_handle_interrupt(struct wcxb *xb, u32 pending)
2371 {
2372 struct t13x *wc = container_of(xb, struct t13x, xb);
2373
2374 if (pending & FALC_INT) {
2375 handle_falc_int(wc);
2376 }
2377 }
2378
2379 static void te13xp_timer(TIMER_DATA_TYPE timer)
2380 {
•2381 struct t13x *wc = timer_container_of(wc, timer, timer);
2382
2383 if (unlikely(!test_bit(INITIALIZED, &wc->bit_flags)))
2384 return;
2385
2386 queue_work(wc->wq, &wc->timer_work);
2387 return;
2388 }
2389
2390 static inline void create_sysfs_files(struct t13x *wc) { return; }
2391 static inline void remove_sysfs_files(struct t13x *wc) { return; }
2392
2393 static int t13x_open(struct dahdi_chan *chan)
2394 {
2395 struct t13x *wc = chan->pvt;
2396 unsigned long flags;
2397
2398 if (!(wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN))
2399 return 0;
2400
2401 if (!test_and_set_bit(HAVE_OPEN_CHANNELS, &wc->bit_flags)) {
2402 local_irq_save(flags);
2403 t13x_check_alarms(wc);
2404 local_irq_restore(flags);
2405 }
2406
2407 return 0;
2408 }
2409
2410 static int t13x_close(struct dahdi_chan *chan)
2411 {
2412 struct t13x *wc = chan->pvt;
2413 unsigned long flags;
2414
2415 if (!(wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN))
2416 return 0;
2417
2418 if (!have_open_channels(wc)) {
2419 if (test_and_clear_bit(HAVE_OPEN_CHANNELS, &wc->bit_flags)) {
2420 local_irq_save(flags);
2421 t13x_check_alarms(wc);
2422 local_irq_restore(flags);
2423 }
2424 }
2425 return 0;
2426 }
2427
2428 static const struct dahdi_span_ops t13x_span_ops = {
2429 .owner = THIS_MODULE,
2430 .spanconfig = t13x_spanconfig,
2431 .chanconfig = t13x_chanconfig,
2432 .startup = t13x_startup,
2433 .rbsbits = t13x_rbsbits,
2434 .maint = t13x_maint,
2435 .ioctl = t13x_ioctl,
2436 .set_spantype = t13x_set_linemode,
2437 .echocan_create = t13x_echocan_create,
2438 .echocan_name = t13x_echocan_name,
2439 .open = t13x_open,
2440 .close = t13x_close,
2441 };
2442
2443 #define SPI_BASE 0x200
2444 #define SPISRR (SPI_BASE + 0x40)
2445 #define SPICR (SPI_BASE + 0x60)
2446 #define SPISR (SPI_BASE + 0x64)
2447 #define SPIDTR (SPI_BASE + 0x68)
2448 #define SPIDRR (SPI_BASE + 0x6c)
2449 #define SPISSR (SPI_BASE + 0x70)
2450
2451 /**
2452 * t13x_read_serial - Returns the serial number of the board.
2453 * @wc: The board whos serial number we are reading.
2454 *
2455 * The buffer returned is dynamically allocated and must be kfree'd by the
2456 * caller. If memory could not be allocated, NULL is returned.
2457 *
2458 * Must be called in process context.
2459 *
2460 * TODO: Move this up into wcxb.c
2461 */
2462 static char *t13x_read_serial(struct t13x *wc)
2463 {
2464 int i;
2465 static const int MAX_SERIAL = 20*5;
2466 const unsigned int SERIAL_ADDRESS = 0x1f0000;
2467 unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
2468 struct wcxb const *xb = &wc->xb;
2469 struct wcxb_spi_master *flash_spi_master = NULL;
2470 struct wcxb_spi_device *flash_spi_device = NULL;
2471
2472 if (!serial)
2473 return NULL;
2474
2475 flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
2476 xb->membase + SPI_BASE,
2477 false);
2478 if (!flash_spi_master)
2479 return NULL;
2480
2481 flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
2482 if (!flash_spi_device)
2483 goto error_exit;
2484
2485 wcxb_flash_read(flash_spi_device, SERIAL_ADDRESS,
2486 serial, MAX_SERIAL);
2487
2488 for (i = 0; i < MAX_SERIAL; ++i) {
2489 if ((serial[i] < 0x20) || (serial[i] > 0x7e)) {
2490 serial[i] = '\0';
2491 break;
2492 }
2493 }
2494
2495 if (!i) {
2496 kfree(serial);
2497 serial = NULL;
2498 } else {
2499 /* Limit the size of the buffer to just what is needed to
2500 * actually hold the serial number. */
2501 unsigned char *new_serial;
2502 new_serial = kasprintf(GFP_KERNEL, "%s", serial);
2503 kfree(serial);
2504 serial = new_serial;
2505 }
2506
2507 error_exit:
2508 wcxb_spi_device_destroy(flash_spi_device);
2509 wcxb_spi_master_destroy(flash_spi_master);
2510 return serial;
2511 }
2512
2513 static int te13xp_check_firmware(struct t13x *wc)
2514 {
2515 const char *filename;
2516 u32 expected_version;
2517 enum wcxb_reset_option reset;
2518
2519 if (is_pcie(wc)) {
2520 filename = TE133_FW_FILENAME;
2521 expected_version = TE133_FW_VERSION;
2522 } else {
2523 filename = TE134_FW_FILENAME;
2524 expected_version = TE134_FW_VERSION;
2525 }
2526
2527 /* Specific firmware requires power cycle to properly reset */
2528 if (0x6f0017 == wcxb_get_firmware_version(&wc->xb))
2529 reset = WCXB_RESET_LATER;
2530 else
2531 reset = WCXB_RESET_NOW;
2532
2533 return wcxb_check_firmware(&wc->xb, expected_version, filename,
2534 force_firmware, reset);
2535 }
2536
2537 static int __devinit te13xp_init_one(struct pci_dev *pdev,
2538 const struct pci_device_id *ent)
2539 {
2540 struct t13x *wc;
2541 const struct t13x_desc *d = (struct t13x_desc *) ent->driver_data;
2542 unsigned int x;
2543 int res;
2544 unsigned int index = -1;
2545 enum linemode type;
2546 bool vpmcapable = false;
2547
2548 for (x = 0; x < ARRAY_SIZE(ifaces); x++) {
2549 if (!ifaces[x]) {
2550 index = x;
2551 break;
2552 }
2553 }
2554
2555 if (-1 == index) {
2556 pr_info("Too many interfaces\n");
2557 pci_disable_device(pdev);
2558 return -EIO;
2559 }
2560
2561 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
2562 if (!wc) {
2563 return -ENOMEM;
2564 }
2565
2566 wc->devtype = d;
2567 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
2568
2569 /* Set the performance counters to 0 */
2570 t13x_reset_counters(&wc->span);
2571
2572 ifaces[index] = wc;
2573
2574 sprintf(wc->span.name, "WCT13x/%d", index);
2575 snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Card %d",
2576 d->name, index);
2577 wc->ledstate = -1;
2578 spin_lock_init(&wc->reglock);
2579 mutex_init(&wc->lock);
2580 timer_setup(&wc->timer, te13xp_timer, 0);
2581
2582 INIT_WORK(&wc->timer_work, timer_work_func);
2583
2584 wc->ddev = dahdi_create_device();
2585 if (!wc->ddev) {
2586 res = -ENOMEM;
2587 goto fail_exit;
2588 }
2589 wc->ddev->manufacturer = "Digium";
2590 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
2591 pdev->bus->number,
2592 PCI_SLOT(pdev->devfn) + 1);
2593 if (!wc->ddev->location) {
2594 res = -ENOMEM;
2595 goto fail_exit;
2596 }
2597
2598 wc->name = kasprintf(GFP_KERNEL, "wcte13xp%d", index);
2599 if (!wc->name) {
2600 res = -ENOMEM;
2601 goto fail_exit;
2602 }
2603
2604 pci_set_drvdata(pdev, wc);
2605
2606 /* Setup wcxb library */
2607 wc->xb.pdev = pdev;
2608 wc->xb.ops = &xb_ops;
2609 wc->xb.debug = &debug;
2610 res = wcxb_init(&wc->xb, wc->name, 0);
2611 if (res)
2612 goto fail_exit;
2613
2614 res = te13xp_check_firmware(wc);
2615 if (res)
2616 goto fail_exit;
2617
2618 wc->ddev->hardware_id = t13x_read_serial(wc);
2619 if (!wc->ddev->hardware_id) {
2620 dev_info(&wc->xb.pdev->dev, "No serial number found.\n");
2621 res = -EIO;
2622 goto fail_exit;
2623 }
2624
2625 /* Check for hardware echo cancel support */
2626 if (!strncmp("TE133", wc->ddev->hardware_id+1, 5) ||
2627 !strncmp("TE134", wc->ddev->hardware_id+1, 5)) {
2628 vpmcapable = true;
2629 }
2630
2631 wc->wq = create_singlethread_workqueue(wc->name);
2632 if (!wc->wq) {
2633 res = -ENOMEM;
2634 goto fail_exit;
2635 }
2636
2637 /* Initial buffer latency size,
2638 adjustable on load by modparam "latency" */
2639 if (latency > 0 && latency < DRING_SIZE) {
2640 wcxb_set_minlatency(&wc->xb, latency);
2641 if (WCXB_DEFAULT_LATENCY != latency)
2642 dev_info(&wc->xb.pdev->dev,
2643 "latency manually overridden to %d\n",
2644 latency);
2645 } else {
2646 dev_info(&wc->xb.pdev->dev,
2647 "latency module parameter must be between 1 and %d\n",
2648 DRING_SIZE);
2649 res = -EPERM;
2650 goto fail_exit;
2651 }
2652
2653 wcxb_set_minlatency(&wc->xb, latency);
2654 wcxb_set_maxlatency(&wc->xb, max_latency);
2655
2656 create_sysfs_files(wc);
2657
2658 res = t13x_hardware_post_init(wc, &type);
2659 if (res)
2660 goto fail_exit;
2661
2662 wc->span.chans = wc->chans;
2663
2664 res = t13x_software_init(wc, type);
2665 if (res)
2666 goto fail_exit;
2667
2668 if (!wc->vpm && vpmsupport && vpmcapable)
2669 t13x_vpm_init(wc);
2670
2671 if (wc->vpm)
2672 wc->ddev->devicetype = kasprintf(GFP_KERNEL,
2673 "%s (VPMOCT032)", wc->devtype->name);
2674 else
2675 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s",
2676 wc->devtype->name);
2677
2678 wc->span.ops = &t13x_span_ops;
2679 list_add_tail(&wc->span.device_node, &wc->ddev->spans);
2680
2681 /* Span is in red alarm by default */
2682 wc->span.alarms = DAHDI_ALARM_NONE;
2683
2684 res = dahdi_register_device(wc->ddev, &wc->xb.pdev->dev);
2685 if (res) {
2686 dev_info(&wc->xb.pdev->dev, "Unable to register with DAHDI\n");
2687 goto fail_exit;
2688 }
2689
2690 set_bit(INITIALIZED, &wc->bit_flags);
2691 mod_timer(&wc->timer, jiffies + HZ/5);
2692
2693 if (wc->ddev->hardware_id) {
2694 dev_info(&wc->xb.pdev->dev, "Found a %s (SN: %s)\n",
2695 wc->devtype->name, wc->ddev->hardware_id);
2696 } else {
2697 dev_info(&wc->xb.pdev->dev, "Found a %s\n",
2698 wc->devtype->name);
2699 }
2700
2701 return 0;
2702
2703 fail_exit:
2704 wcxb_release(&wc->xb);
2705
2706 free_wc(wc);
2707 return res;
2708 }
2709
2710 static void __devexit te13xp_remove_one(struct pci_dev *pdev)
2711 {
2712 struct t13x *wc = pci_get_drvdata(pdev);
2713 dev_info(&wc->xb.pdev->dev, "Removing a Wildcard TE13xP.\n");
2714 if (!wc)
2715 return;
2716
2717 clear_bit(INITIALIZED, &wc->bit_flags);
2718 smp_mb__after_atomic();
2719
2720 /* Quiesce DMA engine interrupts */
2721 wcxb_stop(&wc->xb);
2722
2723 /* Leave framer in reset so it no longer transmits */
2724 wcxb_gpio_clear(&wc->xb, FALC_CPU_RESET);
2725
•2726 timer_delete_sync(&wc->timer);
2727 flush_workqueue(wc->wq);
•2728 timer_delete_sync(&wc->timer);
2729
2730 /* Turn off status LED */
2731 t13x_setleds(wc, 0);
2732
2733 if (wc->vpm)
2734 release_vpm450m(wc->vpm);
2735 wc->vpm = NULL;
2736
2737 dahdi_unregister_device(wc->ddev);
2738
2739 remove_sysfs_files(wc);
2740
2741 wcxb_release(&wc->xb);
2742 free_wc(wc);
2743 }
2744
2745 static DEFINE_PCI_DEVICE_TABLE(te13xp_pci_tbl) = {
2746 { 0xd161, 0x800a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te133},
2747 { 0xd161, 0x800b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te134},
2748 { 0 }
2749 };
2750
2751 static void te13xp_shutdown(struct pci_dev *pdev)
2752 {
2753 struct t13x *wc = pci_get_drvdata(pdev);
2754 dev_info(&wc->xb.pdev->dev, "Quiescing a Wildcard TE13xP.\n");
2755 if (!wc)
2756 return;
2757
2758 /* Quiesce and mask DMA engine interrupts */
2759 wcxb_stop(&wc->xb);
2760 }
2761
2762 static int te13xp_suspend(struct pci_dev *pdev, pm_message_t state)
2763 {
2764 return -ENOSYS;
2765 }
2766
2767 MODULE_DEVICE_TABLE(pci, te13xp_pci_tbl);
2768
2769 static struct pci_driver te13xp_driver = {
2770 .name = "wcte13xp",
2771 .probe = te13xp_init_one,
2772 .remove = __devexit_p(te13xp_remove_one),
2773 .shutdown = te13xp_shutdown,
2774 .suspend = te13xp_suspend,
2775 .id_table = te13xp_pci_tbl,
2776 };
2777
2778 static int __init te13xp_init(void)
2779 {
2780 int res;
2781
2782 if (strcasecmp(default_linemode, "t1") &&
2783 strcasecmp(default_linemode, "j1") &&
2784 strcasecmp(default_linemode, "e1")) {
2785 pr_err("'%s' is an unknown span type.\n", default_linemode);
2786 default_linemode = "t1";
2787 return -EINVAL;
2788 }
2789
2790 res = pci_register_driver(&te13xp_driver);
2791 if (res)
2792 return -ENODEV;
2793
2794 return 0;
2795 }
2796
2797
2798 static void __exit te13xp_cleanup(void)
2799 {
2800 pci_unregister_driver(&te13xp_driver);
2801 }
2802
2803 module_param(debug, int, S_IRUGO | S_IWUSR);
2804 module_param(default_linemode, charp, S_IRUGO);
2805 MODULE_PARM_DESC(default_linemode, "\"t1\"(default), \"e1\", or \"j1\".");
2806 module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
2807 module_param(losalarmdebounce, int, S_IRUGO | S_IWUSR);
2808 module_param(aisalarmdebounce, int, S_IRUGO | S_IWUSR);
2809 module_param(yelalarmdebounce, int, S_IRUGO | S_IWUSR);
2810 module_param(vpmsupport, int, 0600);
2811 module_param(force_firmware, int, S_IRUGO);
2812 module_param(latency, int, S_IRUGO);
2813 MODULE_PARM_DESC(latency, "How many milliseconds of audio to buffer between card and host (3ms default). This number will increase during runtime, dynamically, if dahdi detects that it is too small. This is commonly refered to as a \"latency bump\"");
2814 module_param(max_latency, int, 0600);
2815 MODULE_PARM_DESC(max_latency, "The maximum amount of latency that the driver will permit.");
2816
2817 MODULE_DESCRIPTION("Wildcard Digital Card Driver");
2818 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
2819 MODULE_LICENSE("GPL v2");
2820
2821 module_init(te13xp_init);
2822 module_exit(te13xp_cleanup);