Debian Codegraph @

drivers/dahdi/wctdm24xxp/xhfc.c

raw symbols SCIP
1 /*
2 * B400M Quad-BRI module Driver
3 * Written by Andrew Kohlsmith <[email protected]>
4 *
5 * Copyright (C) 2010 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 #include <linux/kernel.h>
24 #include <linux/pci.h>
25 #include <linux/ppp_defs.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29
30 #define FAST_HDLC_NEED_TABLES
31 #include <dahdi/kernel.h>
32 #include <dahdi/fasthdlc.h>
33
34 #include "wctdm24xxp.h"
35 #include "xhfc.h"
36
37 #define HFC_NR_FIFOS 16
38 #define HFC_ZMIN 0x00 /* from datasheet */
39 #define HFC_ZMAX 0x7f
40 #define HFC_FMIN 0x00
41 #define HFC_FMAX 0x07
42
43 /*
44 * yuck. Any reg which is not mandated read/write or read-only is write-only.
45 * Also, there are dozens of registers with the same address. Additionally,
46 * there are array registers (A_) which have an index register These A_
47 * registers require an index register to be written to indicate WHICH in the
48 * array you want.
49 */
50
51 #define R_CIRM 0x00 /* WO */
52 #define R_CTRL 0x01 /* WO */
53 #define R_CLK_CFG 0x02 /* WO */
54 #define A_Z1 0x04 /* RO */
55 #define A_Z2 0x06 /* RO */
56 #define R_RAM_ADDR 0x08 /* WO */
57 #define R_RAM_CTRL 0x09 /* WO */
58 #define R_FIRST_FIFO 0x0b /* WO */
59 #define R_FIFO_THRES 0x0c /* WO */
60 #define A_F1 0x0c /* RO */
61 #define R_FIFO_MD 0x0d /* WO */
62 #define A_F2 0x0d /* RO */
63 #define A_INC_RES_FIFO 0x0e /* WO */
64 #define A_FIFO_STA 0x0e /* RO */
65 #define R_FSM_IDX 0x0f /* WO */
66 #define R_FIFO 0x0f /* WO */
67
68 #define R_SLOT 0x10 /* WO */
69 #define R_IRQ_OVIEW 0x10 /* RO */
70 #define R_MISC_IRQMSK 0x11 /* WO */
71 #define R_MISC_IRQ 0x11 /* RO */
72 #define R_SU_IRQMSK 0x12 /* WO */
73 #define R_SU_IRQ 0x12 /* RO */
74 #define R_IRQ_CTRL 0x13 /* WO */
75 #define R_AF0_OVIEW 0x13 /* RO */
76 #define R_PCM_MD0 0x14 /* WO */
77 #define A_USAGE 0x14 /* RO */
78 #define R_MSS0 0x15 /* WO */
79 #define R_MSS1 0x15 /* WO */
80 #define R_PCM_MD1 0x15 /* WO */
81 #define R_PCM_MD2 0x15 /* WO */
82 #define R_SH0H 0x15 /* WO */
83 #define R_SH1H 0x15 /* WO */
84 #define R_SH0L 0x15 /* WO */
85 #define R_SH1L 0x15 /* WO */
86 #define R_SL_SEL0 0x15 /* WO */
87 #define R_SL_SEL1 0x15 /* WO */
88 #define R_SL_SEL7 0x15 /* WO */
89 #define R_RAM_USE 0x15 /* RO */
90 #define R_SU_SEL 0x16 /* WO */
91 #define R_CHIP_ID 0x16 /* RO */
92 #define R_SU_SYNC 0x17 /* WO */
93 #define R_BERT_STA 0x17 /* RO */
94 #define R_F0_CNTL 0x18 /* RO */
95 #define R_F0_CNTH 0x19 /* RO */
96 #define R_TI_WD 0x1a /* WO */
97 #define R_BERT_ECL 0x1a /* RO */
98 #define R_BERT_WD_MD 0x1b /* WO */
99 #define R_BERT_ECH 0x1b /* RO */
100 #define R_STATUS 0x1c /* RO */
101 #define R_SL_MAX 0x1d /* RO */
102 #define R_PWM_CFG 0x1e /* WO */
103 #define R_CHIP_RV 0x1f /* RO */
104
105 #define R_FIFO_BL0_IRQ 0x20 /* RO */
106 #define R_FIFO_BL1_IRQ 0x21 /* RO */
107 #define R_FIFO_BL2_IRQ 0x22 /* RO */
108 #define R_FIFO_BL3_IRQ 0x23 /* RO */
109 #define R_FILL_BL0 0x24 /* RO */
110 #define R_FILL_BL1 0x25 /* RO */
111 #define R_FILL_BL2 0x26 /* RO */
112 #define R_FILL_BL3 0x27 /* RO */
113 #define R_CI_TX 0x28 /* WO */
114 #define R_CI_RX 0x28 /* RO */
115 #define R_CGI_CFG0 0x29 /* WO */
116 #define R_CGI_STA 0x29 /* RO */
117 #define R_CGI_CFG1 0x2a /* WO */
118 #define R_MON_RX 0x2a /* RO */
119 #define R_MON_TX 0x2b /* WO */
120
121 #define A_SU_WR_STA 0x30 /* WO */
122 #define A_SU_RD_STA 0x30 /* RO */
123 #define A_SU_CTRL0 0x31 /* WO */
124 #define A_SU_DLYL 0x31 /* RO */
125 #define A_SU_CTRL1 0x32 /* WO */
126 #define A_SU_DLYH 0x32 /* RO */
127 #define A_SU_CTRL2 0x33 /* WO */
128 #define A_MS_TX 0x34 /* WO */
129 #define A_MS_RX 0x34 /* RO */
130 #define A_ST_CTRL3 0x35 /* WO */
131 #define A_UP_CTRL3 0x35 /* WO */
132 #define A_SU_STA 0x35 /* RO */
133 #define A_MS_DF 0x36 /* WO */
134 #define A_SU_CLK_DLY 0x37 /* WO */
135 #define R_PWM0 0x38 /* WO */
136 #define R_PWM1 0x39 /* WO */
137 #define A_B1_TX 0x3c /* WO */
138 #define A_B1_RX 0x3c /* RO */
139 #define A_B2_TX 0x3d /* WO */
140 #define A_B2_RX 0x3d /* RO */
141 #define A_D_TX 0x3e /* WO */
142 #define A_D_RX 0x3e /* RO */
143 #define A_BAC_S_TX 0x3f /* WO */
144 #define A_E_RX 0x3f /* RO */
145
146 #define R_GPIO_OUT1 0x40 /* WO */
147 #define R_GPIO_IN1 0x40 /* RO */
148 #define R_GPIO_OUT3 0x41 /* WO */
149 #define R_GPIO_IN3 0x41 /* RO */
150 #define R_GPIO_EN1 0x42 /* WO */
151 #define R_GPIO_EN3 0x43 /* WO */
152 #define R_GPIO_SEL_BL 0x44 /* WO */
153 #define R_GPIO_OUT2 0x45 /* WO */
154 #define R_GPIO_IN2 0x45 /* RO */
155 #define R_PWM_MD 0x46 /* WO */
156 #define R_GPIO_EN2 0x47 /* WO */
157 #define R_GPIO_OUT0 0x48 /* WO */
158 #define R_GPIO_IN0 0x48 /* RO */
159 #define R_GPIO_EN0 0x4a /* WO */
160 #define R_GPIO_SEL 0x4c /* WO */
161
162 #define R_PLL_CTRL 0x50 /* WO */
163 #define R_PLL_STA 0x50 /* RO */
164 #define R_PLL_P 0x51 /* RW */
165 #define R_PLL_N 0x52 /* RW */
166 #define R_PLL_S 0x53 /* RW */
167
168 #define A_FIFO_DATA 0x80 /* RW */
169 #define A_FIFO_DATA_NOINC 0x84 /* RW */
170
171 #define R_INT_DATA 0x88 /* RO */
172
173 #define R_RAM_DATA 0xc0 /* RW */
174
175 #define A_SL_CFG 0xd0 /* RW */
176 #define A_CH_MSK 0xf4 /* RW */
177 #define A_CON_HDLC 0xfa /* RW */
178 #define A_SUBCH_CFG 0xfb /* RW */
179 #define A_CHANNEL 0xfc /* RW */
180 #define A_FIFO_SEQ 0xfd /* RW */
181 #define A_FIFO_CTRL 0xff /* RW */
182
183 /* R_CIRM bits */
184 #define V_CLK_OFF (1 << 0) /* 1=internal clocks disabled */
185 #define V_WAIT_PROC (1 << 1) /* 1=additional /WAIT after write access */
186 #define V_WAIT_REG (1 << 2) /* 1=additional /WAIT for internal BUSY phase */
187 #define V_SRES (1 << 3) /* soft reset (group 0) */
188 #define V_HFC_RES (1 << 4) /* HFC reset (group 1) */
189 #define V_PCM_RES (1 << 5) /* PCM reset (group 2) */
190 #define V_SU_RES (1 << 6) /* S/T reset (group 3) */
191 #define XHFC_FULL_RESET (V_SRES | V_HFC_RES | V_PCM_RES | V_SU_RES)
192
193 /* R_STATUS bits */
194 #define V_BUSY (1 << 0) /* 1=HFC busy, limited register access */
195 #define V_PROC (1 << 1) /* 1=HFC in processing phase */
196 #define V_LOST_STA (1 << 3) /* 1=frames have been lost */
197 #define V_PCM_INIT (1 << 4) /* 1=PCM init in progress */
198 #define V_WAK_STA (1 << 5) /* state of WAKEUP pin wien V_WAK_EN=1 */
199 #define V_MISC_IRQSTA (1 << 6) /* 1=misc interrupt has occurred */
200 #define V_FR_IRQSTA (1 << 7) /* 1=fifo interrupt has occured */
201 #define XHFC_INTS (V_MISC_IRQSTA | V_FR_IRQSTA)
202
203 /* R_FIFO_BLx_IRQ bits */
204 #define V_FIFOx_TX_IRQ (1 << 0) /* FIFO TX interrupt occurred */
205 #define V_FIFOx_RX_IRQ (1 << 1) /* FIFO RX interrupt occurred */
206 #define FIFOx_TXRX_IRQ (V_FIFOx_TX_IRQ | V_FIFOx_RX_IRQ)
207
208 /* R_FILL_BLx bits */
209 #define V_FILL_FIFOx_TX (1 << 0) /* TX FIFO reached V_THRES_TX level */
210 #define V_FILL_FIFOx_RX (1 << 1) /* RX FIFO reached V_THRES_RX level */
211 #define FILL_FIFOx_TXRX (V_FILL_FIFOx_TX | V_FILL_FIFOx_RX)
212
213 /* R_MISC_IRQ / R_MISC_IRQMSK bits */
214 #define V_SLP_IRQ (1 << 0) /* frame sync pulse flips */
215 #define V_TI_IRQ (1 << 1) /* timer elapsed */
216 #define V_PROC_IRQ (1 << 2) /* processing/non-processing transition */
217 #define V_CI_IRQ (1 << 4) /* indication bits changed */
218 #define V_WAK_IRQ (1 << 5) /* WAKEUP pin */
219 #define V_MON_TX_IRQ (1 << 6) /* monitor byte can be written */
220 #define V_MON_RX_IRQ (1 << 7) /* monitor byte received */
221
222 /* R_SU_IRQ/R_SU_IRQMSK bits */
223 #define V_SU0_IRQ (1 << 0) /* interrupt/mask port 1 */
224 #define V_SU1_IRQ (1 << 1) /* interrupt/mask port 2 */
225 #define V_SU2_IRQ (1 << 2) /* interrupt/mask port 3 */
226 #define V_SU3_IRQ (1 << 3) /* interrupt/mask port 4 */
227
228 /* R_IRQ_CTRL bits */
229 #define V_FIFO_IRQ_EN (1 << 0) /* enable any unmasked FIFO IRQs */
230 #define V_GLOB_IRQ_EN (1 << 3) /* enable any unmasked IRQs */
231 #define V_IRQ_POL (1 << 4) /* 1=IRQ active high */
232
233 /* R_BERT_WD_MD bits */
234 #define V_BERT_ERR (1 << 3) /* 1=generate an error bit in BERT stream */
235 #define V_AUTO_WD_RES (1 << 5) /* 1=automatically kick the watchdog */
236 #define V_WD_RES (1 << 7) /* 1=kick the watchdog (bit auto clears) */
237
238 /* R_TI_WD bits */
239 #define V_EV_TS_SHIFT (0)
240 #define V_EV_TS_MASK (0x0f)
241 #define V_WD_TS_SHIFT (4)
242 #define V_WD_TS_MASK (0xf0)
243
244 /* A_FIFO_CTRL bits */
245 #define V_FIFO_IRQMSK (1 << 0) /* 1=FIFO can generate interrupts */
246 #define V_BERT_EN (1 << 1) /* 1=BERT data replaces FIFO data */
247 #define V_MIX_IRQ (1 << 2) /* IRQ when 0=end of frame only, 1=also when Z1==Z2 */
248 #define V_FR_ABO (1 << 3) /* 1=generate frame abort/frame abort detected */
249 #define V_NO_CRC (1 << 4) /* 1=do not send CRC at end of frame */
250 #define V_NO_REP (1 << 5) /* 1=frame deleted after d-chan contention */
251
252 /* R_CLK_CFG bits */
253 #define V_CLK_PLL (1 << 0) /* Sysclk select 0=OSC_IN, 1=PLL output */
254 #define V_CLKO_HI (1 << 1) /* CLKOUT selection 0=PLL/8, 1=PLL */
255 #define V_CLKO_PLL (1 << 2) /* CLKOUT source 0=divider or PLL input, 1=PLL output */
256 #define V_PCM_CLK (1 << 5) /* 1=PCM clk = OSC, 0 = PCM clk is 2x OSC */
257 #define V_CLKO_OFF (1 << 6) /* CLKOUT enable 0=enabled */
258 #define V_CLK_F1 (1 << 7) /* PLL input pin 0=OSC_IN, 1=F1_1 */
259
260 /* R_PCM_MD0 bits */
261 #define V_PCM_MD (1 << 0) /* 1=PCM master */
262 #define V_C4_POL (1 << 1) /* 1=F0IO sampled on rising edge of C4IO */
263 #define V_F0_NEG (1 << 2) /* 1=negative polarity of F0IO */
264 #define V_F0_LEN (1 << 3) /* 1=F0IO active for 2 C4IO clocks */
265 #define V_PCM_IDX_SEL0 (0x0 << 4) /* reg15 = R_SL_SEL0 */
266 #define V_PCM_IDX_SEL1 (0x1 << 4) /* reg15 = R_SL_SEL1 */
267 #define V_PCM_IDX_SEL7 (0x7 << 4) /* reg15 = R_SL_SEL7 */
268 #define V_PCM_IDX_MSS0 (0x8 << 4) /* reg15 = R_MSS0 */
269 #define V_PCM_IDX_MD1 (0x9 << 4) /* reg15 = R_PCM_MD1 */
270 #define V_PCM_IDX_MD2 (0xa << 4) /* reg15 = R_PCM_MD2 */
271 #define V_PCM_IDX_MSS1 (0xb << 4) /* reg15 = R_MSS1 */
272 #define V_PCM_IDX_SH0L (0xc << 4) /* reg15 = R_SH0L */
273 #define V_PCM_IDX_SH0H (0xd << 4) /* reg15 = R_SH0H */
274 #define V_PCM_IDX_SH1L (0xe << 4) /* reg15 = R_SH1L */
275 #define V_PCM_IDX_SH1H (0xf << 4) /* reg15 = R_SH1H */
276 #define V_PCM_IDX_MASK (0xf0)
277
278 /* R_PCM_MD1 bits */
279 #define V_PLL_ADJ_00 (0x0 << 2) /* adj 4 times by 0.5 system clk cycles */
280 #define V_PLL_ADJ_01 (0x1 << 2) /* adj 3 times by 0.5 system clk cycles */
281 #define V_PLL_ADJ_10 (0x2 << 2) /* adj 2 times by 0.5 system clk cycles */
282 #define V_PLL_ADJ_11 (0x3 << 2) /* adj 1 time by 0.5 system clk cycles */
283 #define V_PCM_DR_2048 (0x0 << 4) /* 2.048Mbps, 32 timeslots */
284 #define V_PCM_DR_4096 (0x1 << 4) /* 4.096Mbps, 64 timeslots */
285 #define V_PCM_DR_8192 (0x2 << 4) /* 8.192Mbps, 128 timeslots */
286 #define V_PCM_DR_075 (0x3 << 4) /* 0.75Mbps, 12 timeslots */
287 #define V_PCM_LOOP (1 << 6) /* 1=internal loopback */
288 #define V_PCM_SMPL (1 << 7) /* 0=sample at middle of bit cell, 1=sample at 3/4 point */
289 #define V_PLL_ADJ_MASK (0x3 << 2)
290 #define V_PCM_DR_MASK (0x3 << 4)
291
292 /* R_PCM_MD2 bits */
293 #define V_SYNC_OUT1 (1 << 1) /* SYNC_O source 0=SYNC_I or FSX_RX, 1=512kHz from PLL or multiframe */
294 #define V_SYNC_SRC (1 << 2) /* 0=line interface, 1=SYNC_I */
295 #define V_SYNC_OUT2 (1 << 3) /* SYNC_O source 0=rx sync or FSC_RX 1=SYNC_I or received superframe */
296 #define V_C2O_EN (1 << 4) /* C2IO output enable (when V_C2I_EN=0) */
297 #define V_C2I_EN (1 << 5) /* PCM controller clock source 0=C4IO, 1=C2IO */
298 #define V_PLL_ICR (1 << 6) /* 0=reduce PCM frame time, 1=increase */
299 #define V_PLL_MAN (1 << 7) /* 0=auto, 1=manual */
300
301 /* A_SL_CFG bits */
302 #define V_CH_SDIR (1 << 0) /* 1=HFC channel receives data from PCM TS */
303 #define V_ROUT_TX_DIS (0x0 << 6) /* disabled, output disabled */
304 #define V_ROUT_TX_LOOP (0x1 << 6) /* internally looped, output disabled */
305 #define V_ROUT_TX_STIO1 (0x2 << 6) /* output data to STIO1 */
306 #define V_ROUT_TX_STIO2 (0x3 << 6) /* output data to STIO2 */
307 #define V_ROUT_RX_DIS (0x0 << 6) /* disabled, input data ignored */
308 #define V_ROUT_RX_LOOP (0x1 << 6) /* internally looped, input data ignored */
309 #define V_ROUT_RX_STIO2 (0x2 << 6) /* channel data comes from STIO1 */
310 #define V_ROUT_RX_STIO1 (0x3 << 6) /* channel data comes from STIO2 */
311 #define V_CH_SNUM_SHIFT (1)
312 #define V_CH_SNUM_MASK (31 << 1)
313
314 /* A_CON_HDLC bits */
315 #define V_IFF (1 << 0) /* Inter-Frame Fill: 0=0x7e, 1=0xff */
316 #define V_HDLC_TRP (1 << 1) /* 0=HDLC mode, 1=transparent */
317 #define V_TRP_DISABLED (0x0 << 2) /* FIFO disabled, no interrupt */
318 #define V_TRP_IRQ_64 (0x1 << 2) /* FIFO enabled, int @ 8 bytes */
319 #define V_TRP_IRQ_128 (0x2 << 2) /* FIFO enabled, int @ 16 bytes */
320 #define V_TRP_IRQ_256 (0x3 << 2) /* FIFO enabled, int @ 32 bytes */
321 #define V_TRP_IRQ_512 (0x4 << 2) /* FIFO enabled, int @ 64 bytes */
322 #define V_TRP_IRQ_1024 (0x5 << 2) /* FIFO enabled, int @ 128 bytes */
323 #define V_TRP_NO_IRQ (0x7 << 2) /* FIFO enabled, no interrupt */
324 #define V_HDLC_IRQ (0x3 << 2) /* HDLC: FIFO enabled, interrupt at end of frame or when FIFO > 16 byte boundary (Mixed IRQ) */
325 #define V_DATA_FLOW_000 (0x0 << 5) /* see A_CON_HDLC reg description in datasheet */
326 #define V_DATA_FLOW_001 (0x1 << 5) /* see A_CON_HDLC reg description in datasheet */
327 #define V_DATA_FLOW_010 (0x2 << 5) /* see A_CON_HDLC reg description in datasheet */
328 #define V_DATA_FLOW_011 (0x3 << 5) /* see A_CON_HDLC reg description in datasheet */
329 #define V_DATA_FLOW_100 (0x4 << 5) /* see A_CON_HDLC reg description in datasheet */
330 #define V_DATA_FLOW_101 (0x5 << 5) /* see A_CON_HDLC reg description in datasheet */
331 #define V_DATA_FLOW_110 (0x6 << 5) /* see A_CON_HDLC reg description in datasheet */
332 #define V_DATA_FLOW_111 (0x7 << 5) /* see A_CON_HDLC reg description in datasheet */
333
334 /* R_FIFO bits */
335 #define V_FIFO_DIR (1 << 0) /* 1=RX FIFO data */
336 #define V_REV (1 << 7) /* 1=MSB first */
337 #define V_FIFO_NUM_SHIFT (1)
338 #define V_FIFO_NUM_MASK (0x3e)
339
340 /* A_CHANNEL bits */
341 #define V_CH_FDIR (1 << 0) /* 1=HFC chan for RX data */
342 #define V_CH_FNUM_SHIFT (1)
343 #define V_CH_FNUM_MASK (0x3e)
344
345 /* R_SLOT bits */
346 #define V_SL_DIR (1 << 0) /* 1=timeslot will RX PCM data from bus */
347 #define V_SL_NUM_SHIFT (1)
348 #define V_SL_NUM_MASK (0xfe)
349
350 /* A_INC_RES_FIFO bits */
351 #define V_INC_F (1 << 0) /* 1=increment FIFO F-counter (bit auto-clears) */
352 #define V_RES_FIFO (1 << 1) /* 1=reset FIFO (bit auto-clears) */
353 #define V_RES_LOST (1 << 2) /* 1=reset LOST error (bit auto-clears) */
354 #define V_RES_FIFO_ERR (1 << 3) /* 1=reset FIFO error (bit auto-clears), check V_ABO_DONE before setting */
355
356 /* R_FIFO_MD bits */
357 #define V_FIFO_MD_00 (0x0 << 0) /* 16 FIFOs, 64 bytes TX/RX, 128 TX or RX if V_UNIDIR_RX */
358 #define V_FIFO_MD_01 (0x1 << 0) /* 8 FIFOs, 128 bytes TX/RX, 256 TX or RX if V_UNIDIR_RX */
359 #define V_FIFO_MD_10 (0x2 << 0) /* 4 FIFOs, 256 bytes TX/RX, invalid mode with V_UNIDIR_RX */
360 #define V_DF_MD_SM (0x0 << 2) /* simple data flow mode */
361 #define V_DF_MD_CSM (0x1 << 2) /* channel select mode */
362 #define V_DF_MD_FSM (0x3 << 2) /* FIFO sequence mode */
363 #define V_UNIDIR_MD (1 << 4) /* 1=unidirectional FIFO mode */
364 #define V_UNIDIR_RX (1 << 5) /* 1=unidirection FIFO is RX */
365
366 /* A_SUBCH_CFG bits */
367 #define V_BIT_CNT_8BIT (0) /* process 8 bits */
368 #define V_BIT_CNT_1BIT (1) /* process 1 bit */
369 #define V_BIT_CNT_2BIT (2) /* process 2 bits */
370 #define V_BIT_CNT_3BIT (3) /* process 3 bits */
371 #define V_BIT_CNT_4BIT (4) /* process 4 bits */
372 #define V_BIT_CNT_5BIT (5) /* process 5 bits */
373 #define V_BIT_CNT_6BIT (6) /* process 6 bits */
374 #define V_BIT_CNT_7BIT (7) /* process 7 bits */
375 #define V_LOOP_FIFO (1 << 6) /* loop FIFO data */
376 #define V_INV_DATA (1 << 7) /* invert FIFO data */
377 #define V_START_BIT_SHIFT (3)
378 #define V_START_BIT_MASK (0x38)
379
380 /* R_SU_SYNC bits */
381 #define V_SYNC_SEL_PORT0 (0x0 << 0) /* sync to TE port 0 */
382 #define V_SYNC_SEL_PORT1 (0x1 << 0) /* sync to TE port 1 */
383 #define V_SYNC_SEL_PORT2 (0x2 << 0) /* sync to TE port 2 */
384 #define V_SYNC_SEL_PORT3 (0x3 << 0) /* sync to TE port 3 */
385 #define V_SYNC_SEL_SYNCI (0x4 << 0) /* sync to SYNC_I */
386 #define V_MAN_SYNC (1 << 3) /* 1=manual sync mode */
387 #define V_AUTO_SYNCI (1 << 4) /* 1=SYNC_I used if FSC_RX not found */
388 #define V_D_MERGE_TX (1 << 5) /* 1=all 4 dchan taken from one byte in TX */
389 #define V_E_MERGE_RX (1 << 6) /* 1=all 4 echan combined in RX direction */
390 #define V_D_MERGE_RX (1 << 7) /* 1=all 4 dchan combined in RX direction */
391 #define V_SYNC_SEL_MASK (0x03)
392
393 /* A_SU_WR_STA bits */
394 #define V_SU_SET_STA_MASK (0x0f)
395 #define V_SU_LD_STA (1 << 4) /* 1=force SU_SET_STA mode, must be manually cleared 6us later */
396 #define V_SU_ACT_NOP (0x0 << 5) /* NOP */
397 #define V_SU_ACT_DEACTIVATE (0x2 << 5) /* start deactivation. auto-clears */
398 #define V_SU_ACT_ACTIVATE (0x3 << 5) /* start activation. auto-clears. */
399 #define V_SET_G2_G3 (1 << 7) /* 1=auto G2->G3 in NT mode. auto-clears after transition. */
400
401 /* A_SU_RD_STA */
402 #define V_SU_STA_MASK (0x0f)
403 #define V_SU_FR_SYNC (1 << 4) /* 1=synchronized */
404 #define V_SU_T2_EXP (1 << 5) /* 1=T2 expired (NT only) */
405 #define V_SU_INFO0 (1 << 6) /* 1=INFO0 */
406 #define V_G2_G3 (1 << 7) /* 1=allows G2->G3 (NT only, auto-clears) */
407
408 /* A_SU_CLK_DLY bits */
409 #define V_SU_DLY_MASK (0x0f)
410 #define V_SU_SMPL_MASK (0xf0)
411 #define V_SU_SMPL_SHIFT (4)
412
413 /* A_SU_CTRL0 bits */
414 #define V_B1_TX_EN (1 << 0) /* 1=B1-channel transmit */
415 #define V_B2_TX_EN (1 << 1) /* 1=B2-channel transmit */
416 #define V_SU_MD (1 << 2) /* 0=TE, 1=NT */
417 #define V_ST_D_LPRIO (1 << 3) /* D-Chan priority 0=high, 1=low */
418 #define V_ST_SQ_EN (1 << 4) /* S/Q bits transmit (1=enabled) */
419 #define V_SU_TST_SIG (1 << 5) /* 1=transmit test signal */
420 #define V_ST_PU_CTRL (1 << 6) /* 1=enable end of pulse control */
421 #define V_SU_STOP (1 << 7) /* 1=power down */
422
423 /* A_SU_CTRL1 bits */
424 #define V_G2_G3_EN (1 << 0) /* 1=G2->G3 allowed without V_SET_G2_G3 */
425 #define V_D_RES (1 << 2) /* 1=D-chan reset */
426 #define V_ST_E_IGNO (1 << 3) /* TE:1=ignore Echan, NT:should always be 1. */
427 #define V_ST_E_LO (1 << 4) /* NT only: 1=force Echan low */
428 #define V_BAC_D (1 << 6) /* 1=BAC bit controls Dchan TX */
429 #define V_B12_SWAP (1 << 7) /* 1=swap B1/B2 */
430
431 /* A_SU_CTRL2 bits */
432 #define V_B1_RX_EN (1 << 0) /* 1=enable B1 RX */
433 #define V_B2_RX_EN (1 << 1) /* 1=enable B2 RX */
434 #define V_MS_SSYNC2 (1 << 2) /* 0 normally, see datasheet */
435 #define V_BAC_S_SEL (1 << 3) /* see datasheet */
436 #define V_SU_SYNC_NT (1 << 4) /* 0=sync pulses generated only in TE, 1=in TE and NT */
437 #define V_SU_2KHZ (1 << 5) /* 0=96kHz test tone, 1=2kHz */
438 #define V_SU_TRI (1 << 6) /* 1=tristate output buffer */
439 #define V_SU_EXCHG (1 << 7) /* 1=invert output drivers */
440
441 /* R_IRQ_OVIEW bits */
442 #define V_FIFO_BL0_IRQ (1 << 0) /* FIFO 0-3 IRQ */
443 #define V_FIFO_BL1_IRQ (1 << 1) /* FIFO 4-7 IRQ */
444 #define V_FIFO_BL2_IRQ (1 << 2) /* FIFO 8-11 IRQ */
445 #define V_FIFO_BL3_IRQ (1 << 3) /* FIFO 12-15 IRQ */
446 #define V_MISC_IRQ (1 << 4) /* R_MISC_IRQ changed */
447 #define V_STUP_IRQ (1 << 5) /* R_SU_IRQ changed */
448 #define V_FIFO_BLx_IRQ (V_FIFO_BL0_IRQ | V_FIFO_BL1_IRQ | V_FIFO_BL2_IRQ | V_FIFO_BL3_IRQ)
449
450 /* R_FIRST_FIFO bits */
451 #define V_FIRST_FIFO_NUM_SHIFT (1)
452
453 /* A_FIFO_SEQ bits */
454 #define V_NEXT_FIFO_NUM_SHIFT (1)
455 #define V_SEQ_END (1 << 6)
456
457 #if (DAHDI_CHUNKSIZE != 8)
458 #error Sorry, the b400m does not support chunksize != 8
459 #endif
460
461 /* general debug messages */
462 #define DEBUG_GENERAL (1 << 0)
463 /* emit DTMF detector messages */
464 #define DEBUG_DTMF (1 << 1)
465 /* emit register read/write, but only if the kernel's DEBUG is defined */
466 #define DEBUG_REGS (1 << 2)
467 /* emit file operation messages */
468 #define DEBUG_FOPS (1 << 3)
469 #define DEBUG_ECHOCAN (1 << 4)
470 /* S/T state machine */
471 #define DEBUG_ST_STATE (1 << 5)
472 /* HDLC controller */
473 #define DEBUG_HDLC (1 << 6)
474 /* alarm changes */
475 #define DEBUG_ALARM (1 << 7)
476 /* Timing related changes */
477 #define DEBUG_TIMING (1 << 8)
478
479 #define DBG (bri_debug & DEBUG_GENERAL)
480 #define DBG_DTMF (bri_debug & DEBUG_DTMF)
481 #define DBG_REGS (bri_debug & DEBUG_REGS)
482 #define DBG_FOPS (bri_debug & DEBUG_FOPS)
483 #define DBG_EC (bri_debug & DEBUG_ECHOCAN)
484 #define DBG_ST (bri_debug & DEBUG_ST_STATE)
485 #define DBG_HDLC (bri_debug & DEBUG_HDLC)
486 #define DBG_ALARM (bri_debug & DEBUG_ALARM)
487 #define DBG_TIMING (bri_debug & DEBUG_TIMING)
488
489 #define DBG_SPANFILTER ((1 << bspan->port) & bri_spanfilter)
490
491 /* #define HARDHDLC_RX */
492
493 /* Any static variables not initialized by default should be set
494 * to 0 automatically */
495 int bri_debug;
496 int bri_spanfilter = 9;
497 int bri_teignorered = 1;
498 int bri_alarmdebounce;
499 int bri_persistentlayer1;
500 int timingcable;
501
502 static int synccard = -1;
503 static int syncspan = -1;
504
505 static const int TIMER_3_MS = 30000;
506
507 #define b4_info(b4, format, arg...) \
508 dev_info(&(b4)->wc->vb.pdev->dev , format , ## arg)
509
510 /* if defined, swaps ports 2 and 3 on the B400M module */
511 #define SWAP_PORTS
512
513 #define XHFC_T1 0
514 #define XHFC_T2 1
515 #define XHFC_T3 2
516 /* T4 - Special timer, used for debug purposes for monitoring of L1 state during activation attempt. */
517 #define XHFC_T4 3
518
519 #define B400M_CHANNELS_PER_SPAN 3 /* 2 B-channels and 1 D-Channel for each BRI span */
520 #define B400M_HDLC_BUF_LEN 128 /* arbitrary, just the max # of byts we will send to DAHDI per call */
521
522 #define get_F(f1, f2, flen) { \
523 f1 = hfc_readcounter8(b4, A_F1); \
524 f2 = hfc_readcounter8(b4, A_F2); \
525 flen = f1 - f2; \
526 \
527 if (flen < 0) \
528 flen += (HFC_FMAX - HFC_FMIN) + 1; \
529 }
530
531 #define get_Z(z1, z2, zlen) { \
532 z1 = hfc_readcounter8(b4, A_Z1); \
533 z2 = hfc_readcounter8(b4, A_Z2); \
534 zlen = z1 - z2; \
535 \
536 if (zlen < 0) \
537 zlen += (HFC_ZMAX - HFC_ZMIN) + 1; \
538 }
539
540 struct b400m_span {
541 struct b400m *parent;
542 unsigned int port; /* which S/T port this span belongs to */
543
544 int oldstate; /* old state machine state */
545 int newalarm; /* alarm to send to DAHDI once alarm timer expires */
546 unsigned long alarmtimer;
547
548 unsigned int te_mode:1; /* 1=TE, 0=NT */
549 unsigned int term_on:1; /* 1= 390 ohm termination enable, 0 = disabled */
550 unsigned long hfc_timers[B400M_CHANNELS_PER_SPAN+1]; /* T1, T2, T3 */
551 int hfc_timer_on[B400M_CHANNELS_PER_SPAN+1]; /* 1=timer active */
552 int fifos[B400M_CHANNELS_PER_SPAN]; /* B1, B2, D <--> host fifo numbers */
553
554 /* HDLC controller fields */
555 struct wctdm_span *wspan; /* pointer to the actual dahdi_span */
556 struct dahdi_chan *sigchan; /* pointer to the signalling channel for this span */
557 int sigactive; /* nonzero means we're in the middle of sending an HDLC frame */
558 atomic_t hdlc_pending; /* hdlc_hard_xmit() increments, hdlc_tx_frame() decrements */
559 unsigned int frames_out;
560 unsigned int frames_in;
561
562 struct fasthdlc_state rxhdlc;
563 int infcs;
564 int f_sz;
565 };
566
567 /* This structure exists one per module */
568 struct b400m {
569 char name[10];
570 int position; /* module position in carrier board */
571 int b400m_no; /* 0-based B400M number in system */
572
573 struct wctdm *wc; /* parent structure */
574
575 spinlock_t reglock; /* lock for all register accesses */
576
577 unsigned long ticks;
578
579 unsigned long fifo_en_rxint; /* each bit is the RX int enable for that FIFO */
580 unsigned long fifo_en_txint; /* each bit is the TX int enable for that FIFO */
581 unsigned char fifo_irqstatus; /* top-half ORs in new interrupts, bottom-half ANDs them out */
582
583 int setsyncspan; /* Span reported from HFC for sync on this card */
584 int reportedsyncspan; /* Span reported from HFC for sync on this card */
585
586 unsigned int running:1; /* interrupts are enabled */
587 unsigned int shutdown:1; /* 1=bottom half doesn't process anything, just returns */
588 unsigned int inited:1; /* FIXME: temporary */
589 unsigned int misc_irq_mask:1; /* 1= interrupt is valid */
590
591 struct b400m_span spans[4]; /* Individual spans */
592
593 struct workqueue_struct *xhfc_ws;
594 struct work_struct xhfc_wq;
595
596 unsigned char irq_oview; /* copy of r_irq_oview */
597 unsigned char fifo_fill; /* copy of R_FILL_BL0 */
598
599 struct semaphore regsem; /* lock for low-level register accesses */
600 struct semaphore fifosem; /* lock for fifo accesses */
601
602 unsigned char lastreg; /* last XHFC register accessed (used to speed up multiple address "hits" */
603 };
604
605 static void hfc_start_st(struct b400m_span *s);
606 static void hfc_stop_st(struct b400m_span *s);
607
608 void b400m_set_dahdi_span(struct b400m *b4, int spanno,
609 struct wctdm_span *wspan)
610 {
611 b4->spans[spanno].wspan = wspan;
612 wspan->bspan = &b4->spans[spanno];
613 }
614
615 static inline void flush_hw(void)
616 {
617 }
618
619 static int xhfc_getreg(struct wctdm *wc, struct wctdm_module *const mod,
620 int addr, u8 *lastreg)
621 {
622 int x;
623
624 if (*lastreg != (unsigned char)addr) {
625 wctdm_setreg(wc, mod, 0x60, addr);
626 *lastreg = (unsigned char)addr;
627 }
628 x = wctdm_getreg(wc, mod, 0x80);
629 return x;
630 }
631
632 static int xhfc_setreg(struct wctdm *wc, struct wctdm_module *const mod,
633 int addr, int val, u8 *lastreg)
634 {
635 if (*lastreg != (unsigned char)addr) {
636 wctdm_setreg(wc, mod, 0x60, addr);
637 *lastreg = (unsigned char)addr;
638 }
639 return wctdm_setreg(wc, mod, 0x00, val);
640 }
641
642 static inline struct wctdm_module *get_mod(struct b400m *b4)
643 {
644 return &b4->wc->mods[b4->position];
645 }
646
647 static int b400m_getreg(struct b400m *b4, int addr)
648 {
649 int x;
650
651 if (down_trylock(&b4->regsem)) {
652 if (down_interruptible(&b4->regsem)) {
653 b4_info(b4, "b400m_getreg(0x%02x) interrupted\n",
654 addr);
655 return -1;
656 }
657 }
658
659 x = xhfc_getreg(b4->wc, get_mod(b4), addr, &b4->lastreg);
660 up(&b4->regsem);
661
662 return x;
663 }
664
665 static int b400m_setreg(struct b400m *b4, const int addr, const int val)
666 {
667 int x;
668
669 if (down_trylock(&b4->regsem)) {
670 if (down_interruptible(&b4->regsem)) {
671 b4_info(b4, "b400m_setreg(0x%02x -> 0x%02x) "
672 "interrupted\n", val, addr);
673 return -1;
674 }
675 }
676
677 x = xhfc_setreg(b4->wc, get_mod(b4), addr, val, &b4->lastreg);
678 up(&b4->regsem);
679
680 return x;
681 }
682
683
684 /*
685 * A lot of the registers in the XHFC are indexed.
686 * this function sets the index, and then writes to the indexed register.
687 */
688 static void b400m_setreg_ra(struct b400m *b4, u8 r, u8 rd, u8 a, u8 ad)
689 {
690 if (down_trylock(&b4->regsem)) {
691 if (down_interruptible(&b4->regsem)) {
692 b4_info(b4, "b400m_setreg_ra(0x%02x -> 0x%02x) "
693 "interrupted\n", a, ad);
694 return;
695 }
696 }
697
698 xhfc_setreg(b4->wc, get_mod(b4), r, rd, &b4->lastreg);
699 xhfc_setreg(b4->wc, get_mod(b4), a, ad, &b4->lastreg);
700 up(&b4->regsem);
701 }
702
703 static u8 b400m_getreg_ra(struct b400m *b4, u8 r, u8 rd, u8 a)
704 {
705 unsigned char res;
706 if (down_trylock(&b4->regsem)) {
707 if (down_interruptible(&b4->regsem)) {
708 b4_info(b4, "b400m_getreg_ra(0x%02x) interrupted\n",
709 a);
710 return -1;
711 }
712 }
713
714 xhfc_setreg(b4->wc, get_mod(b4), r, rd, &b4->lastreg);
715 res = xhfc_getreg(b4->wc, get_mod(b4), a, &b4->lastreg);
716 up(&b4->regsem);
717 return res;
718 }
719
720
721 /*
722 * XHFC-4S GPIO routines
723 *
724 * the xhfc doesn't use its gpio for anything. :-)
725 */
726
727 /*
728 * initialize XHFC GPIO.
729 * GPIO 0-7 are output, low (unconnected, or used for their primary function).
730 */
731 static void hfc_gpio_init(struct b400m *b4)
732 {
733 /* GPIO0..3,7 are GPIO, 4,5,6 primary function */
734 b400m_setreg(b4, R_GPIO_SEL, 0x8f);
735 /* GPIO0..7 drivers set low */
736 b400m_setreg(b4, R_GPIO_OUT0, 0x00);
737 /* GPIO0..7 drivers enabled */
738 b400m_setreg(b4, R_GPIO_EN0, 0xff);
739 /* all other GPIO set to primary function */
740 b400m_setreg(b4, R_GPIO_SEL_BL, 0x00);
741
742 }
743
744
745 /* performs a register write and then waits for the HFC "busy" bit to clear
746 * NOTE: doesn't actually read status, since busy bit is 1us typically, and
747 * we're much, much slower than that. */
748 static void hfc_setreg_waitbusy(struct b400m *b4, const unsigned int reg,
749 const unsigned int val)
750 {
751 b400m_setreg(b4, reg, val);
752 }
753
754 /*
755 * reads an 8-bit register over over and over until the same value is read
756 * twice, then returns that value.
757 */
758 static unsigned char hfc_readcounter8(struct b400m *b4, const unsigned int reg)
759 {
760 unsigned char r1, r2;
761 unsigned long maxwait = 1048576;
762
763 do {
764 r1 = b400m_getreg(b4, reg);
765 r2 = b400m_getreg(b4, reg);
766 } while ((r1 != r2) && maxwait--);
767
768 if (!maxwait) {
769 if (printk_ratelimit()) {
770 dev_warn(&b4->wc->vb.pdev->dev,
771 "hfc_readcounter8(reg 0x%02x) timed out " \
772 "waiting for data to settle!\n", reg);
773 }
774 }
775
776 return r1;
777 }
778
779 /* performs a soft-reset of the HFC-4S. */
780 static void hfc_reset(struct b400m *b4)
781 {
782 unsigned long start;
783 int TIMEOUT = HZ; /* 1s */
784
785 /* Set the FIFOs to 8 128 bytes FIFOs, bidirectional, and set up the
786 * flow controller for channel select mode. */
787 /* Note, this reg has to be set *before* the SW reset */
788 b400m_setreg(b4, R_FIFO_MD, V_FIFO_MD_01 | V_DF_MD_FSM);
789
790 msleep(1); /* wait a bit for clock to settle */
791 /* reset everything, wait 100ms, then allow the XHFC to come out of reset */
792 b400m_setreg(b4, R_CIRM, V_SRES);
793 flush_hw();
794
795 msleep(100);
796
797 b400m_setreg(b4, R_CIRM, 0x00);
798 flush_hw();
799
800 /* wait for XHFC to come out of reset. */
801 start = jiffies;
802 while (b400m_getreg(b4, R_STATUS) & (V_BUSY | V_PCM_INIT)) {
803 if (time_after(jiffies, start + TIMEOUT)) {
804 b4_info(b4, "hfc_reset() Module won't come out of "
805 "reset... continuing.\n");
806 break;
807 }
808 };
809
810 /* Disable the output clock pin, and also the PLL (it's not needed) */
811 b400m_setreg(b4, R_CTRL, 0x00);
812 }
813
814 static void hfc_enable_fifo_irqs(struct b400m *b4)
815 {
816 b400m_setreg(b4, R_IRQ_CTRL, V_FIFO_IRQ_EN | V_GLOB_IRQ_EN);
817 flush_hw();
818 }
819
820 static void hfc_enable_interrupts(struct b400m *b4)
821 {
822 b4->running = 1;
823
824 /* mask all misc interrupts */
825 b4->misc_irq_mask = 0x01;
826 b400m_setreg(b4, R_MISC_IRQMSK, b4->misc_irq_mask);
827
828 /* clear any pending interrupts */
829 b400m_getreg(b4, R_STATUS);
830 b400m_getreg(b4, R_MISC_IRQ);
831 b400m_getreg(b4, R_FIFO_BL0_IRQ);
832 b400m_getreg(b4, R_FIFO_BL1_IRQ);
833 b400m_getreg(b4, R_FIFO_BL2_IRQ);
834 b400m_getreg(b4, R_FIFO_BL3_IRQ);
835
836 hfc_enable_fifo_irqs(b4);
837 }
838
839 static inline void hfc_reset_fifo(struct b400m *b4)
840 {
841 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO,
842 V_RES_FIFO | V_RES_LOST | V_RES_FIFO_ERR);
843 }
844
845 static void hfc_setup_fifo(struct b400m *b4, int fifo)
846 {
847 if (fifo < 4) {
848 /* TX */
849 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
850 b400m_setreg(b4, A_CON_HDLC,
851 V_HDLC_IRQ | V_DATA_FLOW_000 | V_IFF);
852 hfc_reset_fifo(b4);
853
854 /* RX */
855 hfc_setreg_waitbusy(b4, R_FIFO,
856 (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
857 b400m_setreg(b4, A_CON_HDLC,
858 V_HDLC_IRQ | V_DATA_FLOW_000 | V_IFF);
859 hfc_reset_fifo(b4);
860 } else {
861 /* TX */
862 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
863 b400m_setreg(b4, A_CON_HDLC,
864 V_HDLC_TRP | V_TRP_NO_IRQ | V_DATA_FLOW_110);
865 hfc_reset_fifo(b4);
866
867 /* RX */
868 hfc_setreg_waitbusy(b4, R_FIFO,
869 (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
870 b400m_setreg(b4, A_CON_HDLC,
871 V_HDLC_TRP | V_TRP_NO_IRQ | V_DATA_FLOW_110);
872 hfc_reset_fifo(b4);
873 }
874 }
875
876 static void hfc_setup_pcm(struct b400m *b4, int port)
877 {
878 int physport;
879 int offset;
880 int hfc_chan;
881 int ts;
882 #ifdef HARDHDLC_RX
883 const int MAX_OFFSET = 2;
884 #else
885 const int MAX_OFFSET = 3;
886 #endif
887
888 #ifdef SWAP_PORTS
889 /* swap the middle ports */
890 physport = (1 == port) ? 2 : (2 == port) ? 1 : port;
891 #else
892 physport = port;
893 #endif
894
895 for (offset = 0; offset < MAX_OFFSET; offset++) {
896 hfc_chan = (port * 4) + offset;
897 ts = (physport * 3) + offset;
898 ts += (b4->b400m_no * 12);
899 b400m_setreg(b4, R_SLOT, (ts << V_SL_NUM_SHIFT));
900 b400m_setreg(b4, A_SL_CFG,
901 (hfc_chan << V_CH_SNUM_SHIFT) |
902 V_ROUT_TX_STIO2);
903
904 if (offset < 2) {
905 b400m_setreg(b4, R_SLOT,
906 (ts << V_SL_NUM_SHIFT) |
907 V_SL_DIR);
908 b400m_setreg(b4, A_SL_CFG,
909 (hfc_chan << V_CH_SNUM_SHIFT) |
910 V_ROUT_RX_STIO1 | V_CH_SDIR);
911 }
912 }
913 }
914
915 #ifdef SWAP_PORTS
916 #ifdef HARDHDLC_RX
917 static const int fifos[24] = {0, 0, 2, 2, 1, 1, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
918 5, 5, 5, 5, 7, 7, 7, 7 };
919 #else
920 static const int fifos[24] = {0, 4, 2, 6, 1, 5, 3, 7, 4, 4, 4, 4, 6, 6, 6, 6,
921 5, 5, 5, 5, 7, 7, 7, 7 };
922 #endif
923 static const int hfc_chans[12] = {2, 10, 6, 14, 0, 1, 8, 9, 4, 5, 12, 13 };
924 #else
925 #ifdef HARDHDLC_RX
926 static const int fifos[24] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
927 6, 6, 6, 6, 7, 7, 7, 7 };
928 #else
929 static const int fifos[24] = {0, 4, 1, 5, 2, 6, 3, 7, 4, 4, 4, 4, 5, 5, 5, 5,
930 6, 6, 6, 6, 7, 7, 7, 7 };
931 #endif
932 static const int hfc_chans[12] = { 2, 6, 10, 14, 0, 1, 4, 5, 8, 9, 12, 13 };
933 #endif
934
935 static void hfc_setup_fifo_arrays(struct b400m *b4, int fifo)
936 {
937 int val;
938 if (!fifo) {
939 val = (fifos[fifo] << V_FIRST_FIFO_NUM_SHIFT) | (fifo & 1);
940 b400m_setreg(b4, R_FIRST_FIFO, val);
941 } else {
942 #ifdef HARDHDLC_RX
943 val = (fifos[fifo] << V_NEXT_FIFO_NUM_SHIFT) | (fifo & 1);
944 #else
945 val = (fifo < 8) ? (fifos[fifo] << V_NEXT_FIFO_NUM_SHIFT) :
946 (fifos[fifo] << V_NEXT_FIFO_NUM_SHIFT) |
947 (fifo&1);
948 #endif
949 b400m_setreg(b4, A_FIFO_SEQ, val);
950 }
951
952 b400m_setreg(b4, R_FSM_IDX, fifo);
953 val = (fifo < 8) ? (hfc_chans[fifo>>1] << V_CH_FNUM_SHIFT) :
954 (hfc_chans[fifo>>1] << V_CH_FNUM_SHIFT) |
955 (fifo & 1);
956
957 b400m_setreg(b4, A_CHANNEL, val);
958 b400m_setreg(b4, A_SUBCH_CFG, 0x02);
959 }
960
961 static void hfc_setup_fsm(struct b400m *b4)
962 {
963 int chan, fifo, port, offset;
964 #ifdef SWAP_PORTS
965 const int chan_to_fifo[12] = { 4, 4, 0, 6, 6, 2, 5, 5, 1, 7, 7, 3 };
966 #else
967 const int chan_to_fifo[12] = { 4, 4, 0, 5, 5, 1, 6, 6, 2, 7, 7, 3 };
968 #endif
969
970 for (port = 0; port < 4; port++) {
971 for (offset = 0; offset < 3; offset++) {
972 b4->spans[port].fifos[offset] =
973 chan_to_fifo[(port * 3) + offset];
974 }
975 }
976
977 for (chan = 0; chan < ARRAY_SIZE(fifos); chan++)
978 hfc_setup_fifo_arrays(b4, chan);
979
980 b400m_setreg(b4, A_FIFO_SEQ, V_SEQ_END);
981
982 for (fifo = 0; fifo < 8; fifo++)
983 hfc_setup_fifo(b4, fifo);
984
985 for (port = 0; port < 4; port++)
986 hfc_setup_pcm(b4, port);
987 }
988
989 /* takes a read/write fifo pair and optionally resets it, optionally enabling
990 * the rx/tx interrupt */
991 static void hfc_reset_fifo_pair(struct b400m *b4, int fifo,
992 int reset, int force_no_irq)
993 {
994 unsigned char b;
995
996 if (down_interruptible(&b4->fifosem)) {
997 b4_info(b4, "Unable to retrieve fifo sem\n");
998 return;
999 }
1000 b = (!force_no_irq && b4->fifo_en_txint & (1 << fifo)) ?
1001 V_FIFO_IRQMSK : 0;
1002 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1003
1004 if (fifo < 4)
1005 b |= V_MIX_IRQ;
1006
1007 b400m_setreg(b4, A_FIFO_CTRL, b);
1008
1009 if (reset)
1010 hfc_reset_fifo(b4);
1011
1012 b = (!force_no_irq && b4->fifo_en_rxint & (1 << fifo)) ?
1013 V_FIFO_IRQMSK : 0;
1014 hfc_setreg_waitbusy(b4, R_FIFO,
1015 (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1016
1017 if (fifo < 4)
1018 b |= V_MIX_IRQ;
1019
1020 b400m_setreg(b4, A_FIFO_CTRL, b);
1021
1022 if (reset)
1023 hfc_reset_fifo(b4);
1024
1025 up(&b4->fifosem);
1026 }
1027
1028 static void xhfc_set_sync_src(struct b400m *b4, int port)
1029 {
1030 int b;
1031
1032 /* -2 means we need to go back and try again later */
1033 if (port == -2)
1034 return;
1035
1036 if (port == b4->setsyncspan)
1037 return;
1038 else
1039 b4->setsyncspan = port;
1040
1041 b4_info(b4, "xhfc_set_sync_src - modpos %d: setting sync to "
1042 "be port %d\n", b4->position, port);
1043
1044 if (port == -1) /* automatic */
1045 b = 0;
1046 else {
1047 #ifdef SWAP_PORTS
1048 port = (1 == port) ? 2 : (2 == port) ? 1 : port;
1049 #endif
1050 b = (port & V_SYNC_SEL_MASK) | V_MAN_SYNC;
1051 }
1052
1053 b400m_setreg(b4, R_SU_SYNC, b);
1054 }
1055
1056 static void wctdm_change_card_sync_src(struct wctdm *wc, int newsrc, int master)
1057 {
1058 int newctlreg;
1059
1060 newctlreg = wc->ctlreg;
1061
1062 if (master)
1063 newctlreg |= (1 << 5);
1064 else
1065 newctlreg &= ~(1 << 5);
1066
1067 newctlreg &= 0xfc;
1068
1069 newctlreg |= newsrc;
1070
1071 if (DBG_TIMING) {
1072 dev_info(&wc->vb.pdev->dev,
1073 "Final ctlreg before swap: %02x\n", newctlreg);
1074 }
1075
1076 wc->ctlreg = newctlreg;
1077 wc->oldsync = newsrc;
1078
1079 msleep(10);
1080 }
1081
1082 static void wctdm_change_system_sync_src(int oldsync, int oldspan,
1083 int newsync, int newspan)
1084 {
1085 struct wctdm *wc;
1086 struct wctdm *oldsyncwc = NULL, *newsyncwc = NULL;
1087 int newspot;
1088 int i;
1089 int max_latency = 0;
1090
1091 if (oldsync > -1)
1092 oldsyncwc = ifaces[oldsync];
1093
1094 if (newsync > -1)
1095 newsyncwc = ifaces[newsync];
1096
1097 if (newsync == -1) {
1098 BUG_ON(!ifaces[0]);
1099
1100 newsyncwc = ifaces[0];
1101 newsync = 0;
1102 }
1103
1104 newspot = (-1 == newspan) ? 0 : 2 | (newspan >> 2);
1105
1106 if ((oldsync == newsync) && (oldspan == newspan)) {
1107 dev_info(&newsyncwc->vb.pdev->dev,
1108 "No need for timing change. All is same\n");
1109 return;
1110 }
1111
1112 /* First we set all sources to local timing */
1113 for (i = 0; i < WC_MAX_IFACES; i++) {
1114 wc = ifaces[i];
1115 if ((wc != oldsyncwc) && wc) {
1116 wctdm_change_card_sync_src(wc, 0, 0);
1117 if (voicebus_current_latency(&wc->vb) > max_latency)
1118 max_latency = voicebus_current_latency(&wc->vb);
1119 }
1120 }
1121 msleep(max_latency << 1);
1122
1123 /* Set the old sync source to local timing, not driving timing */
1124 if (oldsyncwc) {
1125 wctdm_change_card_sync_src(oldsyncwc, 0, 0);
1126 msleep(voicebus_current_latency(&oldsyncwc->vb) << 1);
1127 }
1128
1129 dev_info(&newsyncwc->vb.pdev->dev,
1130 "Setting new card %d now to be timing master\n", newsync);
1131 /* Finally, set the new sync source to broadcast master timing */
1132 wctdm_change_card_sync_src(newsyncwc, newspot, 1);
1133 msleep(voicebus_current_latency(&newsyncwc->vb) << 1);
1134
1135 /* Last we double verify and set all the remaining cards to be timing
1136 * slaves */
1137 for (i = 0; (i < WC_MAX_IFACES) && ifaces[i]; i++) {
1138 wc = ifaces[i];
1139 if (i == newsync)
1140 continue;
1141
1142 dev_info(&wc->vb.pdev->dev,
1143 "Setting card %d to be timing slave\n", i);
1144 wctdm_change_card_sync_src(wc, 1, 0);
1145 }
1146 msleep(max_latency << 1);
1147 synccard = newsync;
1148 syncspan = newspan;
1149 }
1150
1151 static int xhfc_find_sync_with_timingcable(struct b400m *b4)
1152 {
1153 struct wctdm *wc = b4->wc;
1154 int i, j, osrc, src = -1;
1155 int lowestprio = 10000;
1156 int lowestcard = -1;
1157
1158 if (down_trylock(&ifacelock)) {
1159 set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
1160 return -2;
1161 }
1162
1163 for (j = 0; j < WC_MAX_IFACES && ifaces[j]; j++) {
1164 if (is_initialized(ifaces[j])) {
1165 set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
1166 osrc = -2;
1167 goto out;
1168 } else {
1169 for (i = 0; i < (MAX_SPANS - 1); i++) {
1170 struct wctdm_span *wspan = ifaces[j]->spans[i];
1171 if (wspan &&
1172 wspan->timing_priority &&
1173 !wspan->span.alarms &&
1174 (wspan->timing_priority <
1175 lowestprio)) {
1176 src = i;
1177 lowestprio = wspan->timing_priority;
1178 lowestcard = j;
1179 }
1180 }
1181 }
1182 }
1183
1184 if (lowestcard != synccard) {
1185 b4_info(b4, "Found new timing master, card "
1186 "%d. Old is card %d\n", lowestcard, synccard);
1187 } else if (src != syncspan) {
1188 b4_info(b4, "Timing change, but only from %d to %d on "
1189 "card %d\n", syncspan, src, lowestcard);
1190 }
1191
1192 wctdm_change_system_sync_src(synccard, syncspan,
1193 lowestcard, src);
1194
1195 osrc = -1;
1196
1197 if (wc == ifaces[lowestcard]) {
1198 if (src < (b4->position + 4) && (src >= b4->position))
1199 osrc = src - b4->position;
1200 }
1201
1202 out:
1203 up(&ifacelock);
1204
1205 return osrc;
1206 }
1207
1208 static int xhfc_find_sync_without_timingcable(struct b400m *b4)
1209 {
1210 struct wctdm *wc = b4->wc;
1211 int i, osrc, src = -1;
1212 int lowestprio = 10000;
1213 int newctlregmux;
1214
1215 if (down_trylock(&wc->syncsem)) {
1216 set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
1217 return -2;
1218 }
1219
1220 /* Find lowest slave timing priority on digital spans */
1221 for (i = 0; i < (MAX_SPANS - 1); i++) {
1222 struct wctdm_span *const wspan = wc->spans[i];
1223 if (wspan && wspan->timing_priority &&
1224 !wspan->span.alarms &&
1225 (wspan->timing_priority < lowestprio)) {
1226 src = i;
1227 lowestprio = wspan->timing_priority;
1228 }
1229 }
1230
1231 if (src < 0) {
1232 if (DBG_TIMING)
1233 b4_info(b4, "Picked analog span\n");
1234 osrc = src;
1235 goto check_card_timing;
1236 } else {
1237 if (DBG_TIMING) {
1238 b4_info(b4, "Picked span offset %d to be timing "
1239 "source\n", src);
1240 }
1241 }
1242
1243 osrc = ((src < (b4->position + 4)) && (src >= b4->position)) ?
1244 src - b4->position : -1;
1245
1246 if (DBG_TIMING) {
1247 b4_info(b4, "For b4->position %d timing is %d\n",
1248 b4->position, osrc);
1249 }
1250
1251 check_card_timing:
1252
1253 if (src != -1)
1254 newctlregmux = 2 | (src >> 2);
1255 else
1256 newctlregmux = 0;
1257
1258 if ((newctlregmux & 3) != (wc->ctlreg & 3)) {
1259 if (DBG_TIMING) {
1260 b4_info(b4, "!!!Need to change timing "
1261 "on baseboard to spot %d!!!\n",
1262 src >> 2);
1263 }
1264
1265 wctdm_change_card_sync_src(wc, newctlregmux, 0);
1266 } else {
1267 if (DBG_TIMING) {
1268 dev_info(&b4->wc->vb.pdev->dev, "!!!No need to change timing " \
1269 "on baseboard to spot %d, already there!!!\n",
1270 src >> 2);
1271
1272 }
1273 }
1274
1275
1276 up(&wc->syncsem);
1277
1278 return osrc;
1279 }
1280
1281 /*
1282 * Finds the highest-priority sync span that is not in alarm and returns it.
1283 * Note: the span #s in b4->spans[].sync are 1-based, and this returns a
1284 * 0-based span, or -1 if no spans are found.
1285 */
1286 static inline int xhfc_find_sync(struct b400m *b4)
1287 {
1288 if (timingcable)
1289 return xhfc_find_sync_with_timingcable(b4);
1290 else
1291 return xhfc_find_sync_without_timingcable(b4);
1292 }
1293
1294 /*
1295 * allocates memory and pretty-prints a given S/T state engine state to it.
1296 * calling routine is responsible for freeing the pointer returned! Performs
1297 * no hardware access whatsoever, but does use GFP_KERNEL so do not call from
1298 * IRQ context. if full == 1, prints a "full" dump; otherwise just prints
1299 * current state.
1300 */
1301 static char *hfc_decode_st_state(struct b400m *b4, struct b400m_span *span,
1302 unsigned char state, int full)
1303 {
1304 int nt, sta;
1305 char s[128], *str;
1306 const char *ststr[2][16] = { /* TE, NT */
1307 { "RESET", "?", "SENSING", "DEACT.",
1308 "AWAIT.SIG", "IDENT.INPUT", "SYNCD", "ACTIVATED",
1309 "LOSTFRAMING", "?", "?", "?",
1310 "?", "?", "?", "?" },
1311 { "RESET", "DEACT.", "PEND.ACT", "ACTIVE",
1312 "PEND.DEACT", "?", "?", "?",
1313 "?", "?", "?", "?",
1314 "?", "?", "?", "?" }
1315 };
1316
1317 str = kmalloc(256, GFP_KERNEL);
1318 if (!str) {
1319 dev_warn(&b4->wc->vb.pdev->dev,
1320 "could not allocate mem for ST state decode " \
1321 "string!\n");
1322 return NULL;
1323 }
1324
1325 nt = (span->te_mode == 0);
1326 sta = (state & V_SU_STA_MASK);
1327
1328 sprintf(str, "P%d: %s state %c%d (%s)", span->port + 1,
1329 (nt ? "NT" : "TE"), (nt ? 'G' : 'F'), sta,
1330 ststr[nt][sta]);
1331
1332 if (full) {
1333 sprintf(s, " SYNC: %s, RX INFO0: %s",
1334 ((state & V_SU_FR_SYNC) ? "yes" : "no"),
1335 ((state & V_SU_INFO0) ? "yes" : "no"));
1336 strcat(str, s);
1337
1338 if (nt) {
1339 sprintf(s, ", T2 %s, auto G2->G3: %s",
1340 ((state & V_SU_T2_EXP) ? "expired" : "OK"),
1341 ((state & V_G2_G3) ? "yes" : "no"));
1342 strcat(str, s);
1343 }
1344 }
1345
1346 return str;
1347 }
1348
1349 /*
1350 * sets an S/T port state machine to a given state. if 'auto' is nonzero,
1351 * will put the state machine back in auto mode after setting the state.
1352 */
1353 static void hfc_handle_state(struct b400m_span *s);
1354 static void hfc_force_st_state(struct b400m *b4, struct b400m_span *s,
1355 int state, int resume_auto)
1356 {
1357 b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA,
1358 state | V_SU_LD_STA);
1359
1360 if (resume_auto)
1361 b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, state);
1362
1363 if (DBG_ST && ((1 << s->port) & bri_spanfilter)) {
1364 char *x;
1365
1366 x = hfc_decode_st_state(b4, s, state, 1);
1367 b4_info(b4, "forced port %d to state %d (auto: %d), "
1368 "new decode: %s\n", s->port + 1, state,
1369 resume_auto, x);
1370 kfree(x);
1371 }
1372
1373 /* make sure that we activate any timers/etc needed by this state
1374 * change */
1375 hfc_handle_state(s);
1376 }
1377
1378 /* figures out what to do when an S/T port's timer expires. */
1379 static void hfc_timer_expire(struct b400m_span *s, int t_no)
1380 {
1381 struct b400m *b4 = s->parent;
1382
1383 if (DBG_ST && ((1 << s->port) & bri_spanfilter)) {
1384 b4_info(b4, "%lu: hfc_timer_expire, Port %d T%d "
1385 "expired (value=%lu ena=%d)\n", b4->ticks,
1386 s->port + 1, t_no + 1, s->hfc_timers[t_no],
1387 s->hfc_timer_on[t_no]);
1388 }
1389 /*
1390 * there are three timers associated with every HFC S/T port.
1391 *
1392 * T1 is used by the NT state machine, and is the maximum time the NT
1393 * side should wait for G3 (active) state.
1394 *
1395 * T2 is not actually used in the driver, it is handled by the HFC-4S
1396 * internally.
1397 *
1398 * T3 is used by the TE state machine; it is the maximum time the TE
1399 * side should wait for the INFO4 (activated) signal.
1400 */
1401
1402 /* First, disable the expired timer; hfc_force_st_state() may activate
1403 * it again. */
1404 s->hfc_timer_on[t_no] = 0;
1405
1406 switch (t_no) {
1407 case XHFC_T1: /* switch to G4 (pending deact.), resume auto mode */
1408 hfc_force_st_state(b4, s, 4, 1);
1409 break;
1410 case XHFC_T2: /* switch to G1 (deactivated), resume auto mode */
1411 hfc_force_st_state(b4, s, 1, 1);
1412 break;
1413 case XHFC_T3: /* switch to F3 (deactivated), resume auto mode */
1414 hfc_stop_st(s);
1415 if (bri_persistentlayer1)
1416 hfc_start_st(s);
1417 break;
1418 case XHFC_T4: /* switch to F3 (deactivated), resume auto mode */
1419 hfc_handle_state(s);
1420 s->hfc_timers[XHFC_T4] = b4->ticks + 1000;
1421 s->hfc_timer_on[XHFC_T4] = 1;
1422 break;
1423 default:
1424 if (printk_ratelimit()) {
1425 dev_warn(&b4->wc->vb.pdev->dev,
1426 "hfc_timer_expire found an unknown expired "
1427 "timer (%d)??\n", t_no);
1428 }
1429 }
1430 }
1431
1432 /*
1433 * Run through the active timers on a card and deal with any expiries.
1434 * Also see if the alarm debounce time has expired and if it has, tell DAHDI.
1435 */
1436 static void hfc_update_st_timers(struct b400m *b4)
1437 {
1438 int i, j;
1439 struct b400m_span *s;
1440
1441 for (i = 0; i < 4; i++) {
1442 s = &b4->spans[i];
1443
1444 for (j = XHFC_T1; j <= XHFC_T4; j++) {
1445
1446 /* we don't really do timer2, it is expired by the
1447 * state change handler */
1448 if (j == XHFC_T2)
1449 continue;
1450
1451 if (s->hfc_timer_on[j] &&
1452 time_after_eq(b4->ticks, s->hfc_timers[j]))
1453 hfc_timer_expire(s, j);
1454 }
1455
1456 if (s->wspan && s->newalarm != s->wspan->span.alarms &&
1457 time_after_eq(b4->ticks, s->alarmtimer)) {
1458 s->wspan->span.alarms = s->newalarm;
1459 if ((!s->newalarm && bri_teignorered) || (!bri_teignorered))
1460 dahdi_alarm_notify(&s->wspan->span);
1461
1462 if (DBG_ALARM) {
1463 dev_info(&b4->wc->vb.pdev->dev, "span %d: alarm " \
1464 "%d debounced\n", i + 1,
1465 s->newalarm);
1466 }
1467 set_bit(WCTDM_CHECK_TIMING, &b4->wc->checkflag);
1468 }
1469 }
1470
1471 if (test_and_clear_bit(WCTDM_CHECK_TIMING, &b4->wc->checkflag))
1472 xhfc_set_sync_src(b4, xhfc_find_sync(b4));
1473 }
1474
1475 /* this is the driver-level state machine for an S/T port */
1476 static void hfc_handle_state(struct b400m_span *s)
1477 {
1478 struct b400m *b4;
1479 unsigned char state, sta;
1480 int nt, newsync, oldalarm;
1481 unsigned long oldtimer;
1482
1483 b4 = s->parent;
1484 nt = !s->te_mode;
1485
1486 state = b400m_getreg_ra(b4, R_SU_SEL, s->port, A_SU_RD_STA);
1487 sta = (state & V_SU_STA_MASK);
1488
1489 if (DBG_ST && ((1 << s->port) & bri_spanfilter)) {
1490 char *x;
1491
1492 x = hfc_decode_st_state(b4, s, state, 1);
1493 b4_info(b4, "port %d A_SU_RD_STA old=0x%02x "
1494 "now=0x%02x, decoded: %s\n", s->port + 1,
1495 s->oldstate, state, x);
1496 kfree(x);
1497 }
1498
1499 oldalarm = s->newalarm;
1500 oldtimer = s->alarmtimer;
1501
1502 if (nt) {
1503 switch (sta) {
1504 default: /* Invalid NT state */
1505 case 0x0: /* NT state G0: Reset */
1506 case 0x1: /* NT state G1: Deactivated */
1507 case 0x4: /* NT state G4: Pending Deactivation */
1508 s->newalarm = DAHDI_ALARM_RED;
1509 break;
1510 case 0x2: /* NT state G2: Pending Activation */
1511 s->newalarm = DAHDI_ALARM_YELLOW;
1512 break;
1513 case 0x3: /* NT state G3: Active */
1514 s->hfc_timer_on[XHFC_T1] = 0;
1515 s->newalarm = 0;
1516 break;
1517 }
1518 } else {
1519 switch (sta) {
1520 default: /* Invalid TE state */
1521 case 0x0: /* TE state F0: Reset */
1522 case 0x2: /* TE state F2: Sensing */
1523 case 0x3: /* TE state F3: Deactivated */
1524 case 0x4: /* TE state F4: Awaiting Signal */
1525 case 0x8: /* TE state F8: Lost Framing */
1526 s->newalarm = DAHDI_ALARM_RED;
1527 break;
1528 case 0x5: /* TE state F5: Identifying Input */
1529 case 0x6: /* TE state F6: Synchronized */
1530 s->newalarm = DAHDI_ALARM_YELLOW;
1531 break;
1532 case 0x7: /* TE state F7: Activated */
1533 s->hfc_timer_on[XHFC_T3] = 0;
1534 s->hfc_timer_on[XHFC_T4] = 0;
1535 s->newalarm = 0;
1536 break;
1537 }
1538 }
1539
1540 s->alarmtimer = b4->ticks + bri_alarmdebounce;
1541 s->oldstate = state;
1542
1543 if (DBG_ALARM) {
1544 b4_info(b4, "span %d: old alarm %d expires %ld, "
1545 "new alarm %d expires %ld\n", s->port + 1, oldalarm,
1546 oldtimer, s->newalarm, s->alarmtimer);
1547 }
1548
1549 /* we only care about T2 expiry in G4. */
1550 if (nt && (sta == 4) && (state & V_SU_T2_EXP)) {
1551 if (s->hfc_timer_on[XHFC_T2])
1552 hfc_timer_expire(s, XHFC_T2); /* handle T2 expiry */
1553 }
1554
1555 /* If we're in F3 and receiving INFO0, start T3 and jump to F4 */
1556 if (!nt && (sta == 3) && (state & V_SU_INFO0)) {
1557 if (bri_persistentlayer1) {
1558 s->hfc_timers[XHFC_T3] = b4->ticks + TIMER_3_MS;
1559 s->hfc_timer_on[XHFC_T3] = 1;
1560 if (DBG_ST) {
1561 b4_info(b4, "port %d: receiving "
1562 "INFO0 in state 3, setting T3 and "
1563 "jumping to F4\n", s->port + 1);
1564 }
1565 hfc_start_st(s);
1566 }
1567 }
1568
1569 /* read in R_BERT_STA to determine where our current sync source is */
1570 newsync = b400m_getreg(b4, R_BERT_STA) & 0x07;
1571 if (newsync != b4->reportedsyncspan) {
1572 if (DBG_TIMING) {
1573 if (newsync == 5) {
1574 b4_info(b4, "new card sync source: SYNC_I\n");
1575 } else {
1576 b4_info(b4, "Card position %d: new "
1577 "sync source: port %d\n",
1578 b4->position, newsync);
1579 }
1580 }
1581
1582 b4->reportedsyncspan = newsync;
1583 }
1584 }
1585
1586 static void hfc_stop_all_timers(struct b400m_span *s)
1587 {
1588 s->hfc_timer_on[XHFC_T4] = 0;
1589 s->hfc_timer_on[XHFC_T3] = 0;
1590 s->hfc_timer_on[XHFC_T2] = 0;
1591 s->hfc_timer_on[XHFC_T1] = 0;
1592 }
1593
1594 static void hfc_stop_st(struct b400m_span *s)
1595 {
1596 struct b400m *b4 = s->parent;
1597
1598 hfc_stop_all_timers(s);
1599
1600 b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, V_SU_ACT_DEACTIVATE);
1601 }
1602
1603 /*
1604 * resets an S/T interface to a given NT/TE mode
1605 */
1606 static void hfc_reset_st(struct b400m_span *s)
1607 {
1608 int b;
1609 struct b400m *b4;
1610
1611 b4 = s->parent;
1612
1613 hfc_stop_st(s);
1614
1615 /* force state G0/F0 (reset), then force state 1/2
1616 * (deactivated/sensing) */
1617 b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, V_SU_LD_STA);
1618 flush_hw(); /* make sure write hit hardware */
1619
1620 s->wspan->span.alarms = DAHDI_ALARM_RED;
1621 s->newalarm = DAHDI_ALARM_RED;
1622 dahdi_alarm_notify(&s->wspan->span);
1623
1624 /* set up the clock control register. Must be done before we activate
1625 * the interface. */
1626 if (s->te_mode)
1627 b = 0x0e;
1628 else
1629 b = 0x0c | (6 << V_SU_SMPL_SHIFT);
1630
1631 b400m_setreg(b4, A_SU_CLK_DLY, b);
1632
1633 /* set TE/NT mode, enable B and D channels. */
1634 b400m_setreg(b4, A_SU_CTRL0, V_B1_TX_EN | V_B2_TX_EN |
1635 (s->te_mode ? 0 : V_SU_MD) | V_ST_PU_CTRL);
1636 b400m_setreg(b4, A_SU_CTRL1, V_G2_G3_EN);
1637 b400m_setreg(b4, A_SU_CTRL2, V_B1_RX_EN | V_B2_RX_EN);
1638 b400m_setreg(b4, A_ST_CTRL3, (0x7c << 1));
1639
1640 /* enable the state machine. */
1641 b400m_setreg(b4, A_SU_WR_STA, 0x00);
1642 flush_hw();
1643 }
1644
1645 static void hfc_start_st(struct b400m_span *s)
1646 {
1647 struct b400m *b4 = s->parent;
1648
1649 b400m_setreg_ra(b4, R_SU_SEL, s->port, A_SU_WR_STA, V_SU_ACT_ACTIVATE);
1650
1651 /* start T1 if in NT mode, T3 if in TE mode */
1652 if (s->te_mode) {
1653 /* 500ms wait first time, TIMER_3_MS afterward. */
1654 s->hfc_timers[XHFC_T3] = b4->ticks + TIMER_3_MS;
1655 s->hfc_timer_on[XHFC_T3] = 1;
1656 s->hfc_timer_on[XHFC_T1] = 0;
1657
1658 s->hfc_timers[XHFC_T4] = b4->ticks + 1000;
1659 s->hfc_timer_on[XHFC_T4] = 1;
1660
1661 if (DBG_ST) {
1662 b4_info(b4, "setting port %d t3 timer to %lu\n",
1663 s->port + 1, s->hfc_timers[XHFC_T3]);
1664 }
1665 } else {
1666 static const int TIMER_1_MS = 2000;
1667 s->hfc_timers[XHFC_T1] = b4->ticks + TIMER_1_MS;
1668 s->hfc_timer_on[XHFC_T1] = 1;
1669 s->hfc_timer_on[XHFC_T3] = 0;
1670 if (DBG_ST) {
1671 b4_info(b4, "setting port %d t1 timer to %lu\n",
1672 s->port + 1, s->hfc_timers[XHFC_T1]);
1673 }
1674 }
1675 }
1676
1677 /*
1678 * read in the HFC GPIO to determine each port's mode (TE or NT).
1679 * Then, reset and start the port.
1680 * the flow controller should be set up before this is called.
1681 */
1682 static int hdlc_start(struct b400m *b4, int fifo);
1683 static void hfc_init_all_st(struct b400m *b4)
1684 {
1685 int i;
1686 struct b400m_span *s;
1687
1688 for (i = 0; i < 4; i++) {
1689 s = &b4->spans[i];
1690 s->parent = b4;
1691
1692 #ifdef SWAP_PORTS
1693 s->port = (1 == i) ? 2 : (2 == i) ? 1 : i;
1694 #else
1695 s->port = i;
1696 #endif
1697 s->te_mode = 1;
1698
1699 hdlc_start(b4, s->fifos[2]);
1700 }
1701
1702 }
1703
1704 /* NOTE: assumes fifo lock is held */
1705 #define debug_fz(b4, fifo, prefix, buf) \
1706 do { \
1707 sprintf(buf, "%s: (fifo %d): f1/f2/flen=%d/%d/%d, " \
1708 "z1/z2/zlen=%d/%d/%d\n", prefix, fifo, f1, f2, flen, z1, \
1709 z2, zlen); \
1710 } while (0)
1711
1712 /* enable FIFO RX int and reset the FIFO */
1713 static int hdlc_start(struct b400m *b4, int fifo)
1714 {
1715 b4->fifo_en_txint |= (1 << fifo);
1716 b4->fifo_en_rxint |= (1 << fifo);
1717
1718 hfc_reset_fifo_pair(b4, fifo, 1, 0);
1719 return 0;
1720 }
1721
1722 #ifdef HARDHDLC_RX
1723
1724 /**
1725 * hdlc_signal_complete() - Signal dahdi that we have a complete frame.
1726 *
1727 * @bpan: The span which received the frame.
1728 * @stat: The frame status from the XHFC controller.
1729 *
1730 */
1731 static void hdlc_signal_complete(struct b400m_span *bspan, u8 stat)
1732 {
1733 struct b400m *b4 = bspan->parent;
1734
1735 /* if STAT != 0, indicates bad frame */
1736 if (stat != 0x00) {
1737 if (DBG_HDLC && DBG_SPANFILTER) {
1738 b4_info(b4, "(span %d) STAT=0x%02x indicates " \
1739 "frame problem: %s\n", bspan->port + 1, stat,
1740 (0xff == stat) ? "HDLC Abort" : "Bad FCS");
1741 }
1742
1743 dahdi_hdlc_abort(bspan->sigchan, (0xff == stat) ?
1744 DAHDI_EVENT_ABORT : DAHDI_EVENT_BADFCS);
1745 /* STAT == 0, means frame was OK */
1746 } else {
1747 if (DBG_HDLC && DBG_SPANFILTER) {
1748 b4_info(b4, "(span %d) Frame %d is good!\n",
1749 bspan->port + 1, bspan->frames_in);
1750 }
1751 dahdi_hdlc_finish(bspan->sigchan);
1752 }
1753 }
1754
1755 /*
1756 * Inner loop for D-channel receive function. Retrieves HDLC data from the
1757 * hardware. If the hardware indicates that the frame is complete, we check
1758 * the HDLC engine's STAT byte and update DAHDI as needed.
1759 *
1760 * Returns the number of HDLC frames left in the FIFO, or -1 if we couldn't
1761 * get the lock.
1762 */
1763 static int hdlc_rx_frame(struct b400m_span *bspan)
1764 {
1765 int fifo, i, j, x, zleft;
1766 int z1, z2, zlen, f1, f2, flen, new_flen;
1767 unsigned char buf[B400M_HDLC_BUF_LEN];
1768 char debugbuf[256];
1769 struct b400m *b4 = bspan->parent;
1770
1771 fifo = bspan->fifos[2];
1772
1773 if (DBG_HDLC && DBG_SPANFILTER)
1774 b4_info(b4, "hdlc_rx_frame fifo %d: start\n", fifo);
1775
1776 if (down_trylock(&b4->fifosem) && DBG_HDLC && DBG_SPANFILTER) {
1777 b4_info(b4, "rx_frame: fifo %d 1: couldn't get lock\n",
1778 fifo);
1779 return -1;
1780 }
1781
1782 hfc_setreg_waitbusy(b4, R_FIFO,
1783 (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1784 get_F(f1, f2, flen);
1785 get_Z(z1, z2, zlen);
1786 debug_fz(b4, fifo, "hdlc_rx_frame", debugbuf);
1787 up(&b4->fifosem);
1788
1789 if (DBG_HDLC && DBG_SPANFILTER)
1790 pr_info("%s", debugbuf);
1791
1792 /* if we have at least one complete frame, increment zleft to include
1793 * status byte */
1794 zleft = zlen;
1795 if (flen)
1796 zleft++;
1797
1798 do {
1799 if (zleft > B400M_HDLC_BUF_LEN)
1800 j = B400M_HDLC_BUF_LEN;
1801 else
1802 j = zleft;
1803
1804 if (down_trylock(&b4->fifosem) && DBG_HDLC && DBG_SPANFILTER) {
1805 b4_info(b4,
1806 "rx_frame fifo %d 2: couldn't get lock\n",
1807 fifo);
1808 return -1;
1809 }
1810 hfc_setreg_waitbusy(b4, R_FIFO,
1811 (fifo << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
1812 for (i = 0; i < j; i++)
1813 buf[i] = b400m_getreg(b4, A_FIFO_DATA);
1814 up(&b4->fifosem);
1815
1816 /* don't send STAT byte to DAHDI */
1817 x = j;
1818 if (bspan->sigchan) {
1819 if ((j != B400M_HDLC_BUF_LEN) && flen)
1820 x--;
1821 if (x)
1822 dahdi_hdlc_putbuf(bspan->sigchan, buf, x);
1823 }
1824
1825 zleft -= j;
1826
1827 if (DBG_HDLC && DBG_SPANFILTER) {
1828 b4_info(b4, "transmitted %d bytes to dahdi, " \
1829 "zleft=%d\n", x, zleft);
1830 }
1831
1832 if (DBG_HDLC && DBG_SPANFILTER) {
1833 /* !!! */
1834 b4_info(b4, "hdlc_rx_frame(span %d): " \
1835 "z1/z2/zlen=%d/%d/%d, zleft=%d\n",
1836 bspan->port + 1, z1, z2, zlen, zleft);
1837 for (i = 0; i < j; i++) {
1838 b4_info(b4, "%02x%c", buf[i],
1839 (i < (j - 1)) ? ' ' : '\n');
1840 }
1841 }
1842 } while (zleft > 0);
1843
1844 /* Frame received, increment F2 and get an updated count of frames
1845 * left */
1846 if (down_trylock(&b4->fifosem) && DBG_HDLC && DBG_SPANFILTER) {
1847 b4_info(b4, "rx_frame fifo %d 3: couldn't get lock\n",
1848 fifo);
1849 return 0;
1850 }
1851
1852 /* go get the F count again, just in case another frame snuck in while
1853 * we weren't looking. */
1854 if (flen) {
1855 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
1856 ++bspan->frames_in;
1857 get_F(f1, f2, new_flen);
1858 } else
1859 new_flen = flen;
1860
1861 up(&b4->fifosem);
1862
1863 /* If this channel is not configured with a signalling span we don't
1864 * need to notify the rest of dahdi about this frame. */
1865 if (!bspan->sigchan) {
1866 if (DBG_HDLC && DBG_SPANFILTER) {
1867 b4_info(b4, "hdlc_rx_frame fifo %d: " \
1868 "new_flen %d, early end.\n", fifo, new_flen);
1869 }
1870 return new_flen;
1871 }
1872
1873 if (flen) {
1874 /* disable < 3 check for now */
1875 if (0 && zlen < 3) {
1876 if (DBG_HDLC && DBG_SPANFILTER)
1877 b4_info(b4, "odd, zlen less then 3?\n");
1878 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
1879 } else {
1880 hdlc_signal_complete(bspan, buf[i - 1]);
1881 }
1882 }
1883
1884 if (DBG_HDLC && DBG_SPANFILTER) {
1885 b4_info(b4, "hdlc_rx_frame fifo %d: new_flen=%d end.\n",
1886 fifo, new_flen);
1887 }
1888
1889 return new_flen;
1890 }
1891
1892 #endif /* HARDHDLC_RX */
1893
1894
1895 /*
1896 * Takes one blob of data from DAHDI and shoots it out to the hardware. The
1897 * blob may or may not be a complete HDLC frame. If it isn't, the D-channel
1898 * FIFO interrupt handler will take care of pulling the rest. Returns nonzero
1899 * if there is still data to send in the current HDLC frame.
1900 */
1901 static int hdlc_tx_frame(struct b400m_span *bspan)
1902 {
1903 struct b400m *b4 = bspan->parent;
1904 int res, i, fifo;
1905 int z1, z2, zlen;
1906 int f1 = -1, f2 = -1, flen = -1;
1907 unsigned char buf[B400M_HDLC_BUF_LEN];
1908 unsigned int size = ARRAY_SIZE(buf);
1909 char debugbuf[256];
1910
1911 /* if we're ignoring TE red alarms and we are in alarm, restart the
1912 * S/T state machine */
1913 if (bspan->te_mode && (bspan->newalarm != 0)) {
1914 hfc_start_st(bspan);
1915 }
1916
1917 fifo = bspan->fifos[2];
1918 res = dahdi_hdlc_getbuf(bspan->sigchan, buf, &size);
1919
1920 if (down_interruptible(&b4->fifosem)) {
1921 static int arg;
1922 b4_info(b4, "b400m: arg (%d), grabbed data from DAHDI " \
1923 "but couldn't grab the lock!\n", ++arg);
1924 /* TODO: Inform DAHDI that we have grabbed data and can't use
1925 * it */
1926 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_OVERRUN);
1927 return 1; /* return 1 so we keep trying */
1928 }
1929 hfc_setreg_waitbusy(b4, R_FIFO, (fifo << V_FIFO_NUM_SHIFT));
1930
1931 get_Z(z1, z2, zlen);
1932 debug_fz(b4, fifo, __func__, debugbuf);
1933
1934 /* TODO: check zlen, etc. */
1935 if ((HFC_ZMAX-zlen) < size) {
1936 static int arg;
1937 b4_info(b4, "b400m: arg (%d), zlen (%d) < what we " \
1938 "grabbed from DAHDI (%d)!\n", ++arg, zlen, size);
1939 size = zlen;
1940 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_OVERRUN);
1941 }
1942
1943 if (size > 0) {
1944 bspan->sigactive = 1;
1945
1946 for (i = 0; i < size; i++)
1947 b400m_setreg(b4, A_FIFO_DATA, buf[i]);
1948 /*
1949 * If we got a full frame from DAHDI, increment F and
1950 * decrement our HDLC pending counter. Otherwise, select the
1951 * FIFO again (to start transmission) and make sure the TX IRQ
1952 * is enabled so we will get called again to finish off the
1953 * data
1954 */
1955 if (res != 0) {
1956 ++bspan->frames_out;
1957 bspan->sigactive = 0;
1958 hfc_setreg_waitbusy(b4, A_INC_RES_FIFO, V_INC_F);
1959 atomic_dec(&bspan->hdlc_pending);
1960 } else {
1961 hfc_setreg_waitbusy(b4, R_FIFO,
1962 (fifo << V_FIFO_NUM_SHIFT));
1963 }
1964 }
1965
1966 up(&b4->fifosem);
1967
1968 if (0 && DBG_HDLC && DBG_SPANFILTER) {
1969 b4_info(b4, "%s", debugbuf);
1970
1971 b4_info(b4, "hdlc_tx_frame(span %d): DAHDI gave %d " \
1972 "bytes for FIFO %d (res = %d)\n",
1973 bspan->port + 1, size, fifo, res);
1974
1975 for (i = 0; i < size; i++)
1976 b4_info(b4,
1977 "%02x%c\n", buf[i],
1978 (i < (size - 1)) ? ' ' : '\n');
1979
1980 if (size && res != 0) {
1981 pr_info("Transmitted frame %d on span %d\n",
1982 bspan->frames_out - 1, bspan->port);
1983 }
1984 }
1985
1986 return (res == 0);
1987 }
1988
1989 /*
1990 * b400m lowlevel functions These are functions which impact more than just
1991 * the HFC controller. (those are named hfc_xxx())
1992 */
1993
1994 /*
1995 * Performs a total reset of the card, reinitializes GPIO. The card is
1996 * initialized enough to have LEDs running, and that's about it. Anything to
1997 * do with audio and enabling any kind of processing is done in stage2.
1998 */
1999 static void xhfc_init_stage1(struct b400m *b4)
2000 {
2001 int i;
2002
2003 hfc_reset(b4);
2004 hfc_gpio_init(b4);
2005
2006 /* make sure interrupts are disabled */
2007 b400m_setreg(b4, R_IRQ_CTRL, 0x00);
2008
2009 /* make sure write hits hardware */
2010 flush_hw();
2011
2012 /* disable all FIFO interrupts */
2013 for (i = 0; i < HFC_NR_FIFOS; i++) {
2014 hfc_setreg_waitbusy(b4, R_FIFO, (i << V_FIFO_NUM_SHIFT));
2015 /* disable the interrupt */
2016 b400m_setreg(b4, A_FIFO_CTRL, 0x00);
2017 hfc_setreg_waitbusy(b4, R_FIFO,
2018 (i << V_FIFO_NUM_SHIFT) | V_FIFO_DIR);
2019 /* disable the interrupt */
2020 b400m_setreg(b4, A_FIFO_CTRL, 0x00);
2021 flush_hw();
2022 }
2023
2024 /* set fill threshhold to 16 bytes */
2025 b400m_setreg(b4, R_FIFO_THRES, 0x11);
2026
2027 /* clear any pending FIFO interrupts */
2028 b400m_getreg(b4, R_FIFO_BL2_IRQ);
2029 b400m_getreg(b4, R_FIFO_BL3_IRQ);
2030
2031 b4->misc_irq_mask = 0x00;
2032 b400m_setreg(b4, R_MISC_IRQMSK, b4->misc_irq_mask);
2033 b400m_setreg(b4, R_IRQ_CTRL, 0);
2034 }
2035
2036 /*
2037 * Stage 2 hardware init. Sets up the flow controller, PCM and FIFOs.
2038 * Initializes the echo cancellers. S/T interfaces are not initialized here,
2039 * that is done later, in hfc_init_all_st(). Interrupts are enabled and once
2040 * the s/t interfaces are configured, chip should be pretty much operational.
2041 */
2042 static void xhfc_init_stage2(struct b400m *b4)
2043 {
2044 /*
2045 * set up PCM bus. XHFC is PCM slave C2IO is the clock, auto sync,
2046 * SYNC_O follows SYNC_I. 128 timeslots, long frame sync positive
2047 * polarity, sample on falling clock edge. STIO2 is transmit-only,
2048 * STIO1 is receive-only.
2049 */
2050 b400m_setreg(b4, R_PCM_MD0, V_PCM_IDX_MD1);
2051 b400m_setreg(b4, R_PCM_MD1, V_PCM_DR_8192 | (0x3 << 2));
2052 b400m_setreg(b4, R_PCM_MD0, V_PCM_IDX_MD2);
2053 b400m_setreg(b4, R_PCM_MD2, V_C2I_EN | V_SYNC_OUT1);
2054 b400m_setreg(b4, R_SU_SYNC, V_SYNC_SEL_PORT0);
2055
2056 /* Now set up the flow controller. */
2057 hfc_setup_fsm(b4);
2058
2059 /*
2060 * At this point, everything's set up and ready to go. Don't actually
2061 * enable the global interrupt pin. DAHDI still needs to start up the
2062 * spans, and we don't know exactly when.
2063 */
2064 }
2065
2066 static inline struct b400m_span *bspan_from_dspan(struct dahdi_span *span)
2067 {
2068 return container_of(span, struct wctdm_span, span)->bspan;
2069 }
2070
2071 static int xhfc_startup(struct dahdi_span *span)
2072 {
2073 struct b400m_span *bspan = bspan_from_dspan(span);
2074 struct b400m *b4 = bspan->parent;
2075 if (!b4->running)
2076 hfc_enable_interrupts(bspan->parent);
2077
2078 return 0;
2079 }
2080
2081 /* resets all the FIFOs for a given span. Disables IRQs for the span FIFOs */
2082 static void xhfc_reset_span(struct b400m_span *bspan)
2083 {
2084 int i;
2085 struct b400m *b4 = bspan->parent;
2086
2087 /* b4_info(b4, "xhfc_reset_span()\n"); */
2088 for (i = 0; i < 3; i++)
2089 hfc_reset_fifo_pair(b4, bspan->fifos[i], (i == 2) ? 1 : 0, 1);
2090 }
2091
2092 static void b400m_enable_workqueues(struct wctdm *wc)
2093 {
2094 struct b400m *b4s[2];
2095 int i, numb4s = 0;
2096 unsigned long flags;
2097
2098 spin_lock_irqsave(&wc->reglock, flags);
2099 for (i = 0; i < wc->mods_per_board; i += 4) {
2100 if (wc->mods[i].type == BRI)
2101 b4s[numb4s++] = wc->mods[i].mod.bri;
2102 }
2103 spin_unlock_irqrestore(&wc->reglock, flags);
2104
2105 for (i = 0; i < numb4s; i++) {
2106 if (b4s[i])
2107 b4s[i]->shutdown = 0;
2108 }
2109
2110 }
2111
2112 static void b400m_disable_workqueues(struct wctdm *wc)
2113 {
2114 struct b400m *b4s[2];
2115 int i, numb4s = 0;
2116 unsigned long flags;
2117
2118 spin_lock_irqsave(&wc->reglock, flags);
2119 for (i = 0; i < wc->mods_per_board; i += 4) {
2120 if (wc->mods[i].type == BRI)
2121 b4s[numb4s++] = wc->mods[i].mod.bri;
2122 }
2123 spin_unlock_irqrestore(&wc->reglock, flags);
2124
2125 for (i = 0; i < numb4s; i++) {
2126 if (b4s[i]) {
2127 down(&wc->syncsem);
2128 b4s[i]->shutdown = 1;
2129 up(&wc->syncsem);
2130 flush_workqueue(b4s[i]->xhfc_ws);
2131 }
2132 }
2133 }
2134 /*
2135 * Software selectable NT and TE mode settings on the B400M.
2136 *
2137 * mode - bitwise selection of NT vs TE mode
2138 * 1 = NT; 0 = TE;
2139 * bit 0 is port 0
2140 * bit 1 is port 1
2141 * ...
2142 * term - termination resistance
2143 * 0 = no termination resistance
2144 * 1 = 390 ohm termination resistance switched on
2145 */
2146 static int b400m_set_ntte(struct b400m_span *bspan, int te_mode, int term_on)
2147 {
2148 struct b400m *b4 = bspan->parent;
2149 unsigned char data;
2150 unsigned char addr;
2151 int all_modes = 0, all_terms = 0;
2152 int i;
2153
2154 bspan->wspan->span.spantype = (te_mode > 0)
2155 ? SPANTYPE_DIGITAL_BRI_TE
2156 : SPANTYPE_DIGITAL_BRI_NT;
2157
2158 bspan->te_mode = te_mode;
2159 bspan->term_on = term_on;
2160
2161 for (i = 0; i < 4; i++) {
2162 if (!b4->spans[i].te_mode)
2163 all_modes |= (1 << i);
2164 if (b4->spans[i].term_on)
2165 all_terms |= (1 << i);
2166 }
2167
2168 data = 0x10 | ((all_terms << 4) & 0xc0) | ((all_terms << 2) & 0x0c);
2169 addr = 0x10 | all_modes;
2170
2171 msleep(voicebus_current_latency(&b4->wc->vb) + 2);
2172 wctdm_setreg(b4->wc, get_mod(b4), addr, data);
2173
2174 b4->lastreg = 0xff;
2175 msleep(voicebus_current_latency(&b4->wc->vb) + 2);
2176
2177 hfc_reset_st(bspan);
2178
2179 if (bri_persistentlayer1)
2180 hfc_start_st(bspan);
2181
2182 return 0;
2183 }
2184
2185 /* spanconfig for us means ...? */
2186 int b400m_spanconfig(struct file *file, struct dahdi_span *span,
2187 struct dahdi_lineconfig *lc)
2188 {
2189 struct b400m_span *bspan;
2190 struct b400m *b4;
2191 struct wctdm *wc;
2192 int te_mode, term;
2193 int pos;
2194 int res;
2195
2196 bspan = bspan_from_dspan(span);
2197 b4 = bspan->parent;
2198 wc = b4->wc;
2199
2200 if ((file->f_flags & O_NONBLOCK) && !is_initialized(wc))
2201 return -EAGAIN;
2202
2203 res = wctdm_wait_for_ready(wc);
2204 if (res)
2205 return res;
2206
2207 b400m_disable_workqueues(b4->wc);
2208
2209 te_mode = (lc->lineconfig & DAHDI_CONFIG_NTTE) ? 0 : 1;
2210
2211 term = (lc->lineconfig & DAHDI_CONFIG_TERM) ? 1 : 0;
2212
2213 b4_info(b4, "xhfc: Configuring port %d span %d in %s " \
2214 "mode with termination resistance %s\n", bspan->port,
2215 span->spanno, (te_mode) ? "TE" : "NT",
2216 (term) ? "ENABLED" : "DISABLED");
2217
2218 b400m_set_ntte(bspan, te_mode, term);
2219 if (lc->sync < 0) {
2220 b4_info(b4, "Span %d has invalid sync priority (%d), " \
2221 "removing from sync source list\n", span->spanno,
2222 lc->sync);
2223 lc->sync = 0;
2224 }
2225
2226 if (span->offset >= 4) {
2227 pos = span->offset;
2228 } else {
2229 /* This is tricky. Have to figure out if we're slot 1 or slot
2230 * 2 */
2231 pos = span->offset + b4->position;
2232 }
2233
2234 if (!te_mode && lc->sync) {
2235 b4_info(b4, "NT Spans cannot be timing sources. " \
2236 "Span %d requested to be timing source of " \
2237 "priority %d. Changing priority to 0\n", pos,
2238 lc->sync);
2239 lc->sync = 0;
2240 }
2241
2242 wc->spans[pos]->timing_priority = lc->sync;
2243
2244 bspan->wspan = container_of(span, struct wctdm_span, span);
2245 xhfc_reset_span(bspan);
2246
2247 /* call startup() manually here, because DAHDI won't call the startup
2248 * function unless it receives an IOCTL to do so, and dahdi_cfg
2249 * doesn't. */
2250 xhfc_startup(span);
2251
2252 span->flags |= DAHDI_FLAG_RUNNING;
2253
2254 set_bit(WCTDM_CHECK_TIMING, &wc->checkflag);
2255
2256 b400m_enable_workqueues(b4->wc);
2257
2258 return 0;
2259 }
2260
2261 /* chanconfig for us means to configure the HDLC controller, if appropriate
2262 *
2263 * NOTE: apparently the DAHDI ioctl function calls us with a interrupts
2264 * disabled. This means we cannot actually touch the hardware, because all
2265 * register accesses are wrapped up in a mutex that can sleep.
2266 *
2267 * The solution to that is to simply increment the span's "restart" flag, and
2268 * the driver's workqueue will do the dirty work on our behalf.
2269 */
2270 int b400m_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
2271 {
2272 int alreadyrunning;
2273 struct b400m_span *bspan = bspan_from_dspan(chan->span);
2274 struct b400m *b4 = bspan->parent;
2275 int res;
2276
2277 if ((file->f_flags & O_NONBLOCK) && !is_initialized(b4->wc))
2278 return -EAGAIN;
2279
2280 res = wctdm_wait_for_ready(b4->wc);
2281 if (res)
2282 return res;
2283
2284 alreadyrunning = bspan->wspan->span.flags & DAHDI_FLAG_RUNNING;
2285
2286 if (DBG_FOPS) {
2287 b4_info(b4, "%s channel %d (%s) sigtype %08x\n",
2288 alreadyrunning ? "Reconfigured" : "Configured",
2289 chan->channo, chan->name, sigtype);
2290 }
2291
2292 switch (sigtype) {
2293 case DAHDI_SIG_HARDHDLC:
2294 if (DBG_FOPS) {
2295 b4_info(b4, "%sonfiguring hardware HDLC on %s\n",
2296 ((sigtype == DAHDI_SIG_HARDHDLC) ? "C" :
2297 "Unc"), chan->name);
2298 }
2299 bspan->sigchan = chan;
2300 bspan->sigactive = 0;
2301 atomic_set(&bspan->hdlc_pending, 0);
2302 res = 0;
2303 break;
2304 case DAHDI_SIG_HDLCFCS:
2305 case DAHDI_SIG_HDLCNET:
2306 case DAHDI_SIG_HDLCRAW:
2307 /* Only HARDHDLC is supported for the signalling channel on BRI
2308 * spans. */
2309 res = -EINVAL;
2310 break;
2311 default:
2312 res = 0;
2313 break;
2314 };
2315
2316 return res;
2317 }
2318
2319 int b400m_dchan(struct dahdi_span *span)
2320 {
2321 struct b400m_span *bspan;
2322 struct b400m *b4;
2323 unsigned char *rxb;
2324 int res;
2325 int i;
2326
2327 bspan = bspan_from_dspan(span);
2328 b4 = bspan->parent;
2329 #ifdef HARDHDLC_RX
2330 return 0;
2331 #else
2332 #endif
2333
2334 if (!bspan->sigchan)
2335 return 0;
2336
2337 rxb = bspan->sigchan->readchunk;
2338
2339 if (!rxb) {
2340 b4_info(b4, "No RXB!\n");
2341 return 0;
2342 }
2343
2344 for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
2345 fasthdlc_rx_load_nocheck(&bspan->rxhdlc, *(rxb++));
2346 res = fasthdlc_rx_run(&bspan->rxhdlc);
2347 /* If there is nothing there, continue */
2348 if (res & RETURN_EMPTY_FLAG)
2349 continue;
2350 else if (res & RETURN_COMPLETE_FLAG) {
2351
2352 if (!bspan->f_sz)
2353 continue;
2354
2355 /* Only count this if it's a non-empty frame */
2356 if (bspan->infcs != PPP_GOODFCS) {
2357 dahdi_hdlc_abort(bspan->sigchan,
2358 DAHDI_EVENT_BADFCS);
2359 } else {
2360 dahdi_hdlc_finish(bspan->sigchan);
2361 }
2362 bspan->infcs = PPP_INITFCS;
2363 bspan->f_sz = 0;
2364 continue;
2365 } else if (res & RETURN_DISCARD_FLAG) {
2366
2367 if (!bspan->f_sz)
2368 continue;
2369
2370 dahdi_hdlc_abort(bspan->sigchan, DAHDI_EVENT_ABORT);
2371 bspan->infcs = PPP_INITFCS;
2372 bspan->f_sz = 0;
2373 break;
2374 } else {
2375 unsigned char rxc = res;
2376 bspan->infcs = PPP_FCS(bspan->infcs, rxc);
2377 bspan->f_sz++;
2378 dahdi_hdlc_putbuf(bspan->sigchan, &rxc, 1);
2379 }
2380 }
2381
2382 return 0;
2383 }
2384
2385 /* internal functions, not specific to the hardware or DAHDI */
2386
2387 /*
2388 */
2389 static void xhfc_work(struct work_struct *work)
2390 {
2391 struct b400m *b4 = container_of(work, struct b400m, xhfc_wq);
2392 int i, j, k, fifo;
2393 unsigned char b, b2;
2394
2395 if (b4->shutdown || !is_initialized(b4->wc))
2396 return;
2397
2398 b4->irq_oview = b400m_getreg(b4, R_IRQ_OVIEW);
2399 b4->fifo_fill = b400m_getreg(b4, R_FILL_BL0);
2400
2401 if (b4->irq_oview & V_FIFO_BL0_IRQ) {
2402 b4->fifo_irqstatus |= b400m_getreg(b4, R_FIFO_BL0_IRQ);
2403 b4->irq_oview &= ~V_FIFO_BL0_IRQ;
2404 }
2405
2406 /* only look at BL0, we put all D channel FIFOs in the first block. */
2407 b = b2 = b4->fifo_irqstatus;
2408
2409 for (j = 0; j < 4; j++) {
2410 #ifdef SWAP_PORTS
2411 fifo = (1 == j) ? 2 : (2 == j) ? 1 : j;
2412 #else
2413 fifo = j;
2414 #endif
2415
2416 #ifdef HARDHDLC_RX
2417 if (b & V_FIFOx_RX_IRQ) {
2418 if (fifo < 4) { /* d-channel FIFO */
2419
2420 /*
2421 * I have to loop here until hdlc_rx_frame
2422 * says there are no more frames waiting. for
2423 * whatever reason, the HFC will not generate
2424 * another interrupt if there are still HDLC
2425 * frames waiting to be received. i.e. I get
2426 * an int when F1 changes, not when F1 != F2.
2427 *
2428 */
2429 do {
2430 k = hdlc_rx_frame(&b4->spans[fifo]);
2431 } while (k);
2432 }
2433 }
2434
2435 #endif
2436 b >>= 2;
2437 }
2438
2439 /* zero the bits we just processed */
2440 b4->fifo_irqstatus &= ~b2;
2441 b4->fifo_fill &= ~b2;
2442
2443
2444 #if 1
2445 /* All four D channel FIFOs are in BL0. */
2446 b = b2 = b4->fifo_fill;
2447
2448 for (j = 0; j < 4; j++) {
2449 #ifdef SWAP_PORTS
2450 fifo = (1 == j) ? 2 : (2 == j) ? 1 : j;
2451 #else
2452 fifo = j;
2453 #endif
2454 if (b4->spans[fifo].sigactive && (b & V_FIFOx_TX_IRQ))
2455 hdlc_tx_frame(&b4->spans[fifo]);
2456
2457 #ifdef HARDHDLC_RX
2458 if (b & V_FIFOx_RX_IRQ)
2459 hdlc_rx_frame(&b4->spans[fifo]);
2460 #endif
2461
2462 b >>= 2;
2463 }
2464 #endif
2465
2466 /* Check for outgoing HDLC frame requests The HFC does not generate TX
2467 * interrupts when there is room to send, so I use an atomic counter
2468 * that is incremented every time DAHDI wants to send a frame, and
2469 * decremented every time I send a frame. It'd be better if I could
2470 * just use the interrupt handler, but the HFC seems to trigger a FIFO
2471 * TX IRQ only when it has finished sending a frame, not when one can
2472 * be sent.
2473 */
2474 for (i = 0; i < ARRAY_SIZE(b4->spans); i++) {
2475 struct b400m_span *bspan = &b4->spans[i];
2476
2477 if (atomic_read(&bspan->hdlc_pending)) {
2478 do {
2479 k = hdlc_tx_frame(bspan);
2480 } while (k);
2481 }
2482 }
2483
2484 b = b400m_getreg(b4, R_SU_IRQ);
2485
2486 if (b) {
2487 for (i = 0; i < ARRAY_SIZE(b4->spans); i++) {
2488 int physport;
2489
2490 #ifdef SWAP_PORTS
2491 if (i == 1)
2492 physport = 2;
2493 else if (i == 2)
2494 physport = 1;
2495 else
2496 physport = i;
2497 #else
2498 physport = i;
2499 #endif
2500 if (b & (1 << i))
2501 hfc_handle_state(&b4->spans[physport]);
2502 }
2503 }
2504
2505 hfc_update_st_timers(b4);
2506 }
2507
2508 void wctdm_bri_checkisr(struct wctdm *wc, struct wctdm_module *const mod,
2509 int offset)
2510 {
2511 struct b400m *b4 = mod->mod.bri;
2512
2513 /* don't do anything for non-base card slots */
2514 if (mod->card & 0x03)
2515 return;
2516
2517 /* DEFINITELY don't do anything if our structures aren't ready! */
2518 if (!is_initialized(wc) || !b4 || !b4->inited)
2519 return;
2520
2521 if (offset == 0) {
2522 if (!b4->shutdown)
2523 queue_work(b4->xhfc_ws, &b4->xhfc_wq);
2524 b4->ticks++;
2525 }
2526 return;
2527 }
2528
2529 /* DAHDI calls this when it has data it wants to send to the HDLC controller */
2530 void wctdm_hdlc_hard_xmit(struct dahdi_chan *chan)
2531 {
2532 struct b400m *b4;
2533 struct b400m_span *bspan;
2534 struct dahdi_span *dspan;
2535 int span;
2536
2537 dspan = chan->span;
2538 bspan = bspan_from_dspan(dspan);
2539 b4 = bspan->parent;
2540 span = bspan->port;
2541
2542 if ((DBG_FOPS || DBG_HDLC) && DBG_SPANFILTER) {
2543 b4_info(b4, "hdlc_hard_xmit on chan %s (%i/%i), " \
2544 "span=%i (sigchan=%p, chan=%p)\n", chan->name,
2545 chan->channo, chan->chanpos, span + 1,
2546 bspan->sigchan, chan);
2547 }
2548
2549 /* Increment the hdlc_pending counter and trigger the bottom-half so
2550 * it will be picked up and sent. */
2551 if (bspan->sigchan == chan)
2552 atomic_inc(&bspan->hdlc_pending);
2553 }
2554
2555 static int b400m_probe(struct wctdm *wc, int modpos)
2556 {
2557 unsigned char id, x;
2558 struct b400m *b4;
2559 unsigned long flags;
2560 int chiprev;
2561
2562 wctdm_setreg(wc, &wc->mods[modpos], 0x10, 0x10);
2563 id = xhfc_getreg(wc, &wc->mods[modpos], R_CHIP_ID, &x);
2564
2565 /* chip ID high 7 bits must be 0x62, see datasheet */
2566 if ((id & 0xfe) != 0x62)
2567 return -2;
2568
2569 b4 = kzalloc(sizeof(struct b400m), GFP_KERNEL);
2570 if (!b4) {
2571 dev_err(&wc->vb.pdev->dev,
2572 "Couldn't allocate memory for b400m structure!\n");
2573 return -ENOMEM;
2574 }
2575
2576 /* card found, enabled and main struct allocated. Fill it out. */
2577 b4->wc = wc;
2578 b4->position = modpos;
2579
2580 /* which B400M in the system is this one? count all of them found so
2581 * far */
2582 for (x = 0; x < modpos; x += 4) {
2583 if (wc->mods[x].type == BRI)
2584 ++b4->b400m_no;
2585 }
2586
2587 spin_lock_init(&b4->reglock);
2588 sema_init(&b4->regsem, 1);
2589 sema_init(&b4->fifosem, 1);
2590
2591 for (x = 0; x < 4; x++) {
2592 fasthdlc_init(&b4->spans[x].rxhdlc, FASTHDLC_MODE_16);
2593 b4->spans[x].infcs = PPP_INITFCS;
2594 }
2595
2596 b4->lastreg = 0xff; /* a register we won't hit right off the bat */
2597
2598 chiprev = b400m_getreg(b4, R_CHIP_RV);
2599
2600 b4->setsyncspan = -1; /* sync span is unknown */
2601 b4->reportedsyncspan = -1; /* sync span is unknown */
2602
2603 if (DBG) {
2604 b4_info(b4, "Identified controller rev %d in module %d.\n",
2605 chiprev, b4->position);
2606 }
2607
2608 xhfc_init_stage1(b4);
2609
2610 xhfc_init_stage2(b4);
2611 hfc_init_all_st(b4);
2612
2613 hfc_enable_interrupts(b4);
2614
2615 spin_lock_irqsave(&wc->reglock, flags);
2616 wc->mods[modpos].mod.bri = (void *)b4;
2617 spin_unlock_irqrestore(&wc->reglock, flags);
2618
2619 return 0;
2620 }
2621
2622 void b400m_post_init(struct b400m *b4)
2623 {
2624 snprintf(b4->name, sizeof(b4->name) - 1, "b400m-%d",
2625 b4->b400m_no);
2626 b4->xhfc_ws = create_singlethread_workqueue(b4->name);
2627 INIT_WORK(&b4->xhfc_wq, xhfc_work);
2628 b4->inited = 1;
2629 }
2630
2631 /* functions called from the wctdm code */
2632 int wctdm_init_b400m(struct wctdm *wc, int card)
2633 {
2634 int ret = 0;
2635 unsigned long flags;
2636
2637 if (wc->mods[card & 0xfc].type == QRV)
2638 return -2;
2639
2640 if (!(card & 0x03)) { /* only init if at lowest port in module */
2641 spin_lock_irqsave(&wc->reglock, flags);
2642 wc->mods[card + 0].type = BRI;
2643 wc->mods[card + 0].mod.bri = NULL;
2644 wc->mods[card + 1].type = BRI;
2645 wc->mods[card + 1].mod.bri = NULL;
2646 wc->mods[card + 2].type = BRI;
2647 wc->mods[card + 2].mod.bri = NULL;
2648 wc->mods[card + 3].type = BRI;
2649 wc->mods[card + 3].mod.bri = NULL;
2650 spin_unlock_irqrestore(&wc->reglock, flags);
2651
2652 msleep(20);
2653
2654 if (b400m_probe(wc, card) != 0) {
2655 spin_lock_irqsave(&wc->reglock, flags);
2656 wc->mods[card + 0].type = NONE;
2657 wc->mods[card + 1].type = NONE;
2658 wc->mods[card + 2].type = NONE;
2659 wc->mods[card + 3].type = NONE;
2660 spin_unlock_irqrestore(&wc->reglock, flags);
2661 ret = -2;
2662 }
2663 } else { /* for the "sub-cards" */
2664 if (wc->mods[card & 0xfc].type == BRI) {
2665 spin_lock_irqsave(&wc->reglock, flags);
2666 wc->mods[card].type = BRI;
2667 wc->mods[card].mod.bri = wc->mods[card & 0xfc].mod.bri;
2668 spin_unlock_irqrestore(&wc->reglock, flags);
2669 } else {
2670 ret = -2;
2671 }
2672 }
2673
2674 return ret;
2675 }
2676
2677 void wctdm_unload_b400m(struct wctdm *wc, int card)
2678 {
2679 struct b400m *b4 = wc->mods[card].mod.bri;
2680 int i;
2681
2682 /* TODO: shutdown once won't work if just a single card is hotswapped
2683 * out. But since most of the time this is called because the entire
2684 * driver is in the process of unloading, I'll leave it here. */
2685 static int shutdown_once;
2686
2687
2688 /* only really unload with the 'base' card number. base+1/2/3 aren't
2689 * real. */
2690 if (card & 0x03)
2691 return;
2692
2693 if (timingcable && !shutdown_once) {
2694 b4_info(b4, "Disabling all workqueues for B400Ms\n");
2695 /* Gotta shut down timing change potential during this */
2696 for (i = 0; i < WC_MAX_IFACES; i++) {
2697 if (ifaces[i])
2698 b400m_disable_workqueues(ifaces[i]);
2699 }
2700 b4_info(b4, "Forcing sync to card 0\n");
2701 /* Put the timing configuration in a known state: card 0 is
2702 * master */
2703 wctdm_change_system_sync_src(synccard, syncspan, -1, -1);
2704 /* Change all other cards in the system to self time before
2705 * card 0 is removed */
2706 b4_info(b4, "Setting all cards to return to self sync\n");
2707 for (i = 1; i < WC_MAX_IFACES; i++) {
2708 if (ifaces[i])
2709 wctdm_change_card_sync_src(ifaces[i], 0, 0);
2710 }
2711
2712 b4_info(b4,
2713 "Finished preparing timing linked cards for "
2714 "shutdown\n");
2715
2716 shutdown_once = 1;
2717 }
2718
2719 if (b4) {
2720 b4->inited = 0;
2721
2722 msleep(100);
2723
2724 /* TODO: wait for tdm24xx driver to unregister the spans */
2725 /* do { ... } while(not_unregistered); */
2726
2727 /* Change sync source back to base board so we don't freeze up
2728 * when we reset the XHFC */
2729 b400m_disable_workqueues(wc);
2730
2731 for (i = 0; i < (MAX_SPANS - 1); i++) {
2732 if (wc->spans[i])
2733 wc->spans[i]->timing_priority = 0;
2734 }
2735
2736 for (i = 0; i < ARRAY_SIZE(b4->spans); i++)
2737 b4->spans[i].wspan->span.flags &= ~DAHDI_FLAG_RUNNING;
2738
2739 wctdm_change_card_sync_src(b4->wc, 0, 0);
2740
2741 xhfc_init_stage1(b4);
2742
2743 destroy_workqueue(b4->xhfc_ws);
2744
2745 /* Set these to NONE to ensure that our checkisr
2746 * routines are not entered */
2747 wc->mods[card].type = NONE;
2748 wc->mods[card + 1].type = NONE;
2749 wc->mods[card + 2].type = NONE;
2750 wc->mods[card + 3].type = NONE;
2751
2752 wc->mods[card].mod.bri = NULL;
2753 wc->mods[card + 1].mod.bri = NULL;
2754 wc->mods[card + 2].mod.bri = NULL;
2755 wc->mods[card + 3].mod.bri = NULL;
2756
2757 msleep(voicebus_current_latency(&wc->vb) << 1);
2758 b4_info(b4, "Driver unloaded.\n");
2759 kfree(b4);
2760 }
2761
2762 }
2763
2764 void b400m_module_init(void)
2765 {
2766 fasthdlc_precalc();
2767 }
2768
2769 void b400m_module_cleanup(void)
2770 {
2771 }