[WIP] Voice IND
[osmocom-bb.git] / src / target / firmware / calypso / tsp.c
1 /* Calypso DBB internal TSP (Time Serial Port) Driver */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdint.h>
24 #include <stdio.h>
25
26 #include <debug.h>
27 #include <memory.h>
28 #include <calypso/tpu.h>
29 #include <calypso/tsp.h>
30
31 static uint16_t tspact_state;
32
33 /* initiate a TSP write through the TPU */
34 void tsp_write(uint8_t dev_idx, uint8_t bitlen, uint32_t dout)
35 {
36         if (bitlen <= 8) {
37                 tpu_enq_move(TPUI_TX_1, dout & 0xff);
38         } else if (bitlen <= 16) {
39                 tpu_enq_move(TPUI_TX_1, (dout >> 8) & 0xff);
40                 tpu_enq_move(TPUI_TX_2, dout & 0xff);
41         } else if (bitlen <= 24) {
42                 tpu_enq_move(TPUI_TX_1, (dout >> 16) & 0xff);
43                 tpu_enq_move(TPUI_TX_2, (dout >> 8) & 0xff);
44                 tpu_enq_move(TPUI_TX_3, dout & 0xff);
45         } else {
46                 tpu_enq_move(TPUI_TX_1, (dout >> 24) & 0xff);
47                 tpu_enq_move(TPUI_TX_2, (dout >> 16) & 0xff);
48                 tpu_enq_move(TPUI_TX_3, (dout >> 8) & 0xff);
49                 tpu_enq_move(TPUI_TX_4, dout & 0xff);
50         }
51         tpu_enq_move(TPUI_TSP_CTRL1, (dev_idx << 5) | (bitlen - 1));
52         tpu_enq_move(TPUI_TSP_CTRL2, TPUI_CTRL2_WR);
53 }
54
55 /* Configure clock edge and chip enable polarity for a device */
56 void tsp_setup(uint8_t dev_idx, int clk_rising, int en_positive, int en_edge)
57 {
58         uint8_t reg = TPUI_TSP_SET1 + (dev_idx / 2);
59         uint8_t val = 0;
60         uint8_t shift;
61
62         if (dev_idx & 1)
63                 shift = 4;
64         else
65                 shift = 0;
66
67         if (clk_rising)
68                 val |= 1;
69         if (en_positive)
70                 val |= 2;
71         if (en_edge)
72                 val |= 4;
73
74         tpu_enq_move(reg, (val << shift));
75 }
76
77 /* Update the TSPACT state, including enable and disable */
78 void tsp_act_update(uint16_t new_act)
79 {
80         uint8_t low = new_act & 0xff;
81         uint8_t high = new_act >> 8;
82
83         if (low != (tspact_state & 0xff))
84                 tpu_enq_move(TPUI_TSP_ACT_L, low);
85         if (high != (tspact_state >> 8))
86                 tpu_enq_move(TPUI_TSP_ACT_U, high);
87
88         tspact_state = new_act;
89 }
90
91 /* Enable one or multiple TSPACT signals */
92 void tsp_act_enable(uint16_t bitmask)
93 {
94         uint16_t new_act = tspact_state | bitmask;
95         tsp_act_update(new_act);
96 }
97
98 /* Disable one or multiple TSPACT signals */
99 void tsp_act_disable(uint16_t bitmask)
100 {
101         uint16_t new_act = tspact_state & ~bitmask;
102         tsp_act_update(new_act);
103 }
104
105 /* Obtain the current tspact state */
106 uint16_t tsp_act_state(void)
107 {
108         return tspact_state;
109 }
110
111 /* Toggle one or multiple TSPACT signals */
112 void tsp_act_toggle(uint16_t bitmask)
113 {
114         uint16_t new_act = tspact_state ^ bitmask;
115         tsp_act_update(new_act);
116 }
117
118 void tsp_init(void)
119 {
120         tsp_act_update(0);
121 }