Debian Codegraph @

drivers/dahdi/wcte13xp-base.c

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