Initial import of OsmocomBB into git repository
[osmocom-bb.git] / src / target / firmware / include / calypso / tpu.h
1 #ifndef _CALYPSO_TPU_H
2 #define _CALYPSO_TPU_H
3
4 /* Assert or de-assert TPU reset */
5 void tpu_reset(int active);
6 /* Enable or Disable a new scenario loaded into the TPU */
7 void tpu_enable(int active);
8 /* Enable or Disable the clock of teh TPU Module */
9 void tpu_clk_enable(int active);
10 /* Enable Frame Interrupt generation on next frame.  DSP will reset it */
11 void tpu_dsp_frameirq_enable(void);
12 /* Is a Frame interrupt still pending for the DSP ? */
13 int tpu_dsp_fameirq_pending(void);
14 /* Rewind the TPU, i.e. restart enqueueing instructions at the base addr */
15 void tpu_rewind(void);
16 /* Enqueue a raw TPU instruction */
17 void tpu_enqueue(uint16_t instr);
18 /* Initialize TPU and TPU driver */
19 void tpu_init(void);
20 /* (Busy)Wait until TPU is idle */
21 void tpu_wait_idle(void);
22 /* Enable FRAME interrupt generation */
23 void tpu_frame_irq_en(int mcu, int dsp);
24 /* Force the generation of a DSP interrupt */
25 void tpu_force_dsp_frame_irq(void);
26
27 /* Get the current TPU SYNCHRO register */
28 uint16_t tpu_get_synchro(void);
29 /* Get the current TPU OFFSET register */
30 uint16_t tpu_get_offset(void);
31
32 enum tpu_instr {
33         TPU_INSTR_AT            = (1 << 13),
34         TPU_INSTR_OFFSET        = (2 << 13),
35         TPU_INSTR_SYNCHRO       = (3 << 13),    /* Loading delta synchro value in TPU synchro register */
36         TPU_INSTR_WAIT          = (5 << 13),    /* Wait a certain period (in GSM qbits) */
37         TPU_INSTR_SLEEP         = (0 << 13),    /* Stop the sequencer by disabling TPU ENABLE bit in ctrl reg */
38         /* data processing */
39         TPU_INSTR_MOVE          = (4 << 13),
40 };
41
42 /* Addresses internal to the TPU, only accessible via MOVE */
43 enum tpu_reg_int {
44         TPUI_TSP_CTRL1  = 0x00,
45         TPUI_TSP_CTRL2  = 0x01,
46         TPUI_TX_1       = 0x04,
47         TPUI_TX_2       = 0x03,
48         TPUI_TX_3       = 0x03,
49         TPUI_TX_4       = 0x05,
50         TPUI_TSP_ACT_L  = 0x06,
51         TPUI_TSP_ACT_U  = 0x07,
52         TPUI_TSP_SET1   = 0x09,
53         TPUI_TSP_SET2   = 0x0a,
54         TPUI_TSP_SET3   = 0x0b,
55         TPUI_DSP_INT_PG = 0x10,
56         TPUI_GAUGING_EN = 0x11,
57 };
58
59 enum tpui_ctrl2_bits {
60         TPUI_CTRL2_RD           = (1 << 0),
61         TPUI_CTRL2_WR           = (1 << 1),
62 };
63
64 static inline uint16_t tpu_mod5000(int16_t time)
65 {
66         if (time < 0)
67                 return time + 5000;
68         if (time >= 5000)
69                 return time - 5000;
70         return time;
71 }
72
73 /* Enqueue a SLEEP operation (stop sequencer by disabling TPU ENABLE bit) */
74 static inline void tpu_enq_sleep(void)
75 {
76         tpu_enqueue(TPU_INSTR_SLEEP);
77 }
78
79 /* Enqueue a MOVE operation */
80 static inline void tpu_enq_move(uint8_t addr, uint8_t data)
81 {
82         tpu_enqueue(TPU_INSTR_MOVE | (data << 5) | (addr & 0x1f));
83 }
84
85 /* Enqueue an AT operation */
86 static inline void tpu_enq_at(int16_t time)
87 {
88         tpu_enqueue(TPU_INSTR_AT | tpu_mod5000(time));
89 }
90
91 /* Enqueue a SYNC operation */
92 static inline void tpu_enq_sync(int16_t time)
93 {
94         tpu_enqueue(TPU_INSTR_SYNCHRO | time);
95 }
96
97 /* Enqueue a WAIT operation */
98 static inline void tpu_enq_wait(int16_t time)
99 {
100         tpu_enqueue(TPU_INSTR_WAIT | time);
101 }
102
103 /* Enqueue an OFFSET operation */
104 static inline void tpu_enq_offset(int16_t time)
105 {
106         tpu_enqueue(TPU_INSTR_OFFSET | time);
107 }
108
109 static inline void tpu_enq_dsp_irq(void)
110 {
111         tpu_enq_move(TPUI_DSP_INT_PG, 0x0001);
112 }
113
114 /* add two numbers, modulo 5000, and ensure the result is positive */
115 uint16_t add_mod5000(uint16_t a, uint16_t b);
116
117 #endif /* _CALYPSO_TPU_H */