1 /* Calypso DBB internal TPU (Time Processing Unit) Driver */
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
25 #include <calypso/tpu.h>
26 #include <calypso/tsp.h>
28 /* Using TPU_DEBUG you will send special HLDC messages to the host PC
29 * containing the full TPU RAM content at the time you call tpu_enable() */
32 #define BASE_ADDR_TPU 0xffff1000
33 #define TPU_REG(x) (BASE_ADDR_TPU+(x))
35 #define BASE_ADDR_TPU_RAM 0xffff9000
36 #define TPU_RAM_END 0xffff97ff
39 TPU_CTRL
= 0x0, /* Control & Status Register */
40 INT_CTRL
= 0x2, /* Interrupt Control Register */
41 INT_STAT
= 0x4, /* Interrupt Status Register */
42 TPU_OFFSET
= 0xC, /* Offset operand value register */
43 TPU_SYNCHRO
= 0xE, /* synchro operand value register */
48 TPU_CTRL_RESET
= (1 << 0),
49 TPU_CTRL_PAGE
= (1 << 1),
50 TPU_CTRL_EN
= (1 << 2),
52 TPU_CTRL_DSP_EN
= (1 << 4),
54 TPU_CTRL_MCU_RAM_ACC
= (1 << 6),
55 TPU_CTRL_TSP_RESET
= (1 << 7),
56 TPU_CTRL_IDLE
= (1 << 8),
57 TPU_CTRL_WAIT
= (1 << 9),
58 TPU_CTRL_CK_ENABLE
= (1 << 10),
59 TPU_CTRL_FULL_WRITE
= (1 << 11),
62 enum tpu_int_ctrl_bits
{
63 ICTRL_MCU_FRAME
= (1 << 0),
64 ICTRL_MCU_PAGE
= (1 << 1),
65 ICTRL_DSP_FRAME
= (1 << 2),
66 ICTRL_DSP_FRAME_FORCE
= (1 << 3),
69 static uint16_t *tpu_ptr
= (uint16_t *)BASE_ADDR_TPU_RAM
;
72 #include <comm/sercomm.h>
73 #include <layer1/sync.h>
74 static struct msgb
*tpu_debug_msg
= NULL
;
76 static void tpu_debug_alloc(void)
78 tpu_debug_msg
= sercomm_alloc_msgb(sizeof(uint32_t) + 64*2);
80 printf("UNABLE TO ALLOC TPU DBG\n");
82 static void tpu_debug_flush(void)
85 sercomm_sendmsg(SC_DLCI_DEBUG
, tpu_debug_msg
);
90 static void tpu_debug_enqueue(uint16_t instr
)
96 if (tpu_ptr
== (uint16_t *) BASE_ADDR_TPU_RAM
) {
97 /* prepend tpu memory dump with frame number */
98 uint32_t *fn
= (uint32_t *) msgb_put(tpu_debug_msg
, sizeof(fn
));
99 *fn
= l1s
.current_time
.fn
;
101 if (msgb_tailroom(tpu_debug_msg
) >= sizeof(instr
)) {
102 /* cannot use msgb_put_u16 as host program expects little endian */
103 u16_out
= (uint16_t *) msgb_put(tpu_debug_msg
, sizeof(instr
));
108 static void tpu_debug_alloc(void) { }
109 static void tpu_debug_flush(void) { }
110 static void tpu_debug_enqueue(uint16_t instr
) { }
116 /* wait for a certain control bit to be set */
117 static int tpu_wait_ctrl_bit(uint16_t bit
, int set
)
119 int timeout
= 10*1000;
122 uint16_t reg
= readw(TPU_REG(TPU_CTRL
));
132 puts("Timeout while waiting for TPU ctrl bit!\n");
140 /* assert or de-assert TPU reset */
141 void tpu_reset(int active
)
145 printd("tpu_reset(%u)\n", active
);
146 reg
= readw(TPU_REG(TPU_CTRL
));
148 reg
|= (TPU_CTRL_RESET
|TPU_CTRL_TSP_RESET
);
149 writew(reg
, TPU_REG(TPU_CTRL
));
150 tpu_wait_ctrl_bit(TPU_CTRL_RESET
, BIT_SET
);
152 reg
&= ~(TPU_CTRL_RESET
|TPU_CTRL_TSP_RESET
);
153 writew(reg
, TPU_REG(TPU_CTRL
));
154 tpu_wait_ctrl_bit(TPU_CTRL_RESET
, BIT_CLEAR
);
158 /* Enable or Disable a new scenario loaded into the TPU */
159 void tpu_enable(int active
)
161 uint16_t reg
= readw(TPU_REG(TPU_CTRL
));
163 printd("tpu_enable(%u)\n", active
);
169 writew(reg
, TPU_REG(TPU_CTRL
));
173 /* After the new scenario is loaded, TPU switches the MCU-visible memory
174 * page, i.e. we can write without any danger */
181 for (i
= 0; i
< 100000; i
++) {
182 reg
= readw(TPU_REG(TPU_CTRL
));
183 if (i
== 0 || oldreg
!= reg
) {
184 printd("%d TPU state: 0x%04x\n", i
, reg
);
192 /* Enable or Disable the clock of the TPU Module */
193 void tpu_clk_enable(int active
)
195 uint16_t reg
= readw(TPU_REG(TPU_CTRL
));
197 printd("tpu_clk_enable(%u)\n", active
);
199 reg
|= TPU_CTRL_CK_ENABLE
;
200 writew(reg
, TPU_REG(TPU_CTRL
));
201 tpu_wait_ctrl_bit(TPU_CTRL_CK_ENABLE
, BIT_SET
);
203 reg
&= ~TPU_CTRL_CK_ENABLE
;
204 writew(reg
, TPU_REG(TPU_CTRL
));
205 tpu_wait_ctrl_bit(TPU_CTRL_CK_ENABLE
, BIT_CLEAR
);
209 /* Enable Frame Interrupt generation on next frame. DSP will reset it */
210 void tpu_dsp_frameirq_enable(void)
212 uint16_t reg
= readw(TPU_REG(TPU_CTRL
));
213 reg
|= TPU_CTRL_DSP_EN
;
214 writew(reg
, TPU_REG(TPU_CTRL
));
216 tpu_wait_ctrl_bit(TPU_CTRL_DSP_EN
, BIT_SET
);
219 /* Is a Frame interrupt still pending for the DSP ? */
220 int tpu_dsp_fameirq_pending(void)
222 uint16_t reg
= readw(TPU_REG(TPU_CTRL
));
224 if (reg
& TPU_CTRL_DSP_EN
)
230 void tpu_rewind(void)
232 dputs("tpu_rewind()\n");
233 tpu_ptr
= (uint16_t *) BASE_ADDR_TPU_RAM
;
236 void tpu_enqueue(uint16_t instr
)
238 printd("tpu_enqueue(tpu_ptr=%p, instr=0x%04x)\n", tpu_ptr
, instr
);
239 tpu_debug_enqueue(instr
);
241 if (tpu_ptr
> (uint16_t *) TPU_RAM_END
)
242 puts("TPU enqueue beyond end of TPU memory\n");
249 /* Put TPU into Reset and enable clock */
253 /* set all TPU RAM to zero */
254 for (ptr
= (uint16_t *) BASE_ADDR_TPU_RAM
; ptr
< (uint16_t *) TPU_RAM_END
; ptr
++)
257 /* Get TPU out of reset */
259 /* Disable all interrupts */
260 writeb(0x7, TPU_REG(INT_CTRL
));
271 /* program a sequence of TSPACT events into the TPU */
272 for (i
= 0; i
< 10; i
++) {
273 puts("TSP ACT enable: ");
274 tsp_act_enable(0x0001);
276 puts("TSP ACT disable: ");
277 tsp_act_disable(0x0001);
282 /* tell the chip to execute the scenario */
286 void tpu_wait_idle(void)
288 dputs("Waiting for TPU Idle ");
289 /* Wait until TPU is doing something */
291 /* Wait until TPU is idle */
292 while (readw(TPU_REG(TPU_CTRL
)) & TPU_CTRL_IDLE
)
297 void tpu_frame_irq_en(int mcu
, int dsp
)
299 uint8_t reg
= readb(TPU_REG(INT_CTRL
));
301 reg
&= ~ICTRL_MCU_FRAME
;
303 reg
|= ICTRL_MCU_FRAME
;
306 reg
&= ~ICTRL_DSP_FRAME
;
308 reg
|= ICTRL_DSP_FRAME
;
310 writeb(reg
, TPU_REG(INT_CTRL
));
313 void tpu_force_dsp_frame_irq(void)
315 uint8_t reg
= readb(TPU_REG(INT_CTRL
));
316 reg
|= ICTRL_DSP_FRAME_FORCE
;
317 writeb(reg
, TPU_REG(INT_CTRL
));
320 uint16_t tpu_get_offset(void)
322 return readw(TPU_REG(TPU_OFFSET
));
325 uint16_t tpu_get_synchro(void)
327 return readw(TPU_REG(TPU_SYNCHRO
));
330 /* add two numbers, modulo 5000, and ensure the result is positive */
331 uint16_t add_mod5000(int16_t a
, int16_t b
)
333 int32_t sum
= (int32_t)a
+ (int32_t)b
;
337 /* wrap around zero */