layer1: Disable FIQ as well as IRQ for locking between L1S and L1A
[osmocom-bb.git] / src / target / firmware / layer1 / sync.c
1 /* Synchronous part of GSM Layer 1 */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  * (C) 2010 by Dieter Spaar <spaar@mirider.augusta.de>
5  * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  */
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <defines.h>
31 #include <debug.h>
32 #include <memory.h>
33 #include <byteorder.h>
34 #include <asm/system.h>
35
36 #include <osmocore/gsm_utils.h>
37 #include <osmocore/msgb.h>
38 #include <calypso/dsp_api.h>
39 #include <calypso/irq.h>
40 #include <calypso/tpu.h>
41 #include <calypso/tsp.h>
42 #include <calypso/dsp.h>
43 #include <calypso/timer.h>
44 #include <comm/sercomm.h>
45
46 #include <abb/twl3025.h>
47
48 //#define DEBUG_EVERY_TDMA
49
50 #include <layer1/sync.h>
51 #include <layer1/afc.h>
52 #include <layer1/agc.h>
53 #include <layer1/tdma_sched.h>
54 #include <layer1/mframe_sched.h>
55 #include <layer1/sched_gsmtime.h>
56 #include <layer1/tpu_window.h>
57 #include <layer1/l23_api.h>
58
59 #include <l1a_l23_interface.h>
60
61 struct l1s_state l1s;
62
63 void l1s_time_inc(struct gsm_time *time, uint32_t delta_fn)
64 {
65         ADD_MODULO(time->fn, delta_fn, GSM_MAX_FN);
66
67         if (delta_fn == 1) {
68                 ADD_MODULO(time->t2, 1, 26);
69                 ADD_MODULO(time->t3, 1, 51);
70
71                 /* if the new frame number is a multiple of 51 */
72                 if (time->t3 == 0) {
73                         ADD_MODULO(time->tc, 1, 8);
74
75                         /* if new FN is multiple of 51 and 26 */
76                         if (time->t2 == 0)
77                                 ADD_MODULO(time->t1, 1, 2048);
78                 }
79         } else
80                 gsm_fn2gsmtime(time, time->fn);
81 }
82
83 void l1s_time_dump(const struct gsm_time *time)
84 {
85         printf("fn=%lu(%u/%2u/%2u)", time->fn, time->t1, time->t2, time->t3);
86 }
87
88 /* clip a signed 16bit value at a certain limit */
89 int16_t clip_int16(int16_t angle, int16_t clip_at)
90 {
91         if (angle > clip_at)
92                 angle = clip_at;
93         else if (angle < -clip_at)
94                 angle = -clip_at;
95
96         return angle;
97 }
98
99 int16_t l1s_snr_int(uint16_t snr)
100 {
101         return snr >> 10;
102 }
103
104 uint16_t l1s_snr_fract(uint16_t snr)
105 {
106         uint32_t fract = snr & 0x3ff;
107         fract = fract * 1000 / (2 << 10);
108
109         return fract & 0xffff;
110 }
111
112 #define AFC_MAX_ANGLE           328     /* 0.01 radian in fx1.15 */
113
114 /* synchronize the L1S to a new timebase (typically a new cell */
115 void synchronize_tdma(struct l1_cell_info *cinfo)
116 {
117         int32_t fn_offset;
118         uint32_t tpu_shift = cinfo->time_alignment;
119
120         /* NB detection only works if the TOA of the SB
121          * is within 0...8. We have to add 75 to get an SB TOA of 4. */
122         tpu_shift += 75;
123
124         tpu_shift = (l1s.tpu_offset + tpu_shift) % QBITS_PER_TDMA;
125
126         fn_offset = cinfo->fn_offset - 1;
127
128         /* if we're already very close to the end of the TPU frame, the
129          * next interrupt will basically occur now and we need to
130          * compensate */
131         if (tpu_shift < SWITCH_TIME)
132                 fn_offset++;
133
134 #if 0 /* probably wrong as we already added "offset" and "shift" above */
135         /* increment the TPU quarter-bit offset */
136         l1s.tpu_offset = (l1s.tpu_offset + tpu_shift) % TPU_RANGE;
137 #else
138         l1s.tpu_offset = tpu_shift;
139 #endif
140
141         puts("Synchronize_TDMA\n");
142         /* request the TPU to adjust the SYNCHRO and OFFSET registers */
143         tpu_enq_at(SWITCH_TIME);
144         tpu_enq_sync(l1s.tpu_offset);
145 #if 0
146         /* FIXME: properly end the TPU window at the emd of l1_sync() */
147         tpu_end_scenario();
148 #endif
149
150         /* Change the current time to reflect the new value */
151         l1s_time_inc(&l1s.current_time, fn_offset);
152         l1s.next_time = l1s.current_time;
153         l1s_time_inc(&l1s.next_time, 1);
154
155         /* The serving cell now no longer has a frame or bit offset */
156         cinfo->fn_offset = 0;
157         cinfo->time_alignment = 0;
158 }
159
160 void l1s_reset_hw(void)
161 {
162         dsp_api.w_page = 0;
163         dsp_api.r_page = 0;
164         dsp_api.r_page_used = 0;
165         dsp_api.db_w = (T_DB_MCU_TO_DSP *) BASE_API_W_PAGE_0;
166         dsp_api.db_r = (T_DB_DSP_TO_MCU *) BASE_API_R_PAGE_0;
167         dsp_api.ndb->d_dsp_page = 0;
168
169         /* we have to really reset the TPU, otherwise FB detection
170          * somtimes returns wrong TOA values. */
171         tpu_reset(1);
172         tpu_reset(0);
173         tpu_rewind();
174         tpu_enq_wait(5); /* really needed ? */
175         tpu_enq_offset(l1s.tpu_offset);
176         tpu_end_scenario();
177 }
178
179 /* Timer for detecting lost IRQ */
180 #define TIMER_TICKS_PER_TDMA    1875
181
182 static int last_timestamp;
183
184 static inline void check_lost_frame(void)
185 {
186         int diff, timestamp = hwtimer_read(1);
187
188         if (last_timestamp < timestamp)
189                 last_timestamp += (4*TIMER_TICKS_PER_TDMA);
190
191         diff = last_timestamp - timestamp;
192         if (diff != 1875)
193                 printf("LOST!\n");
194
195         last_timestamp = timestamp;
196 }
197
198 /* schedule a completion */
199 void l1s_compl_sched(enum l1_compl c)
200 {
201         unsigned long flags;
202
203         local_firq_save(flags);
204         l1s.scheduled_compl |= (1 << c);
205         local_irq_restore(flags);
206 }
207
208 /* main routine for synchronous part of layer 1, called by frame interrupt
209  * generated by TPU once every TDMA frame */
210 static void l1_sync(void)
211 {
212         putchart('+');
213
214         check_lost_frame();
215
216         /* Increment Time */
217         l1s.current_time = l1s.next_time;
218         l1s_time_inc(&l1s.next_time, 1);
219         //l1s_time_dump(&l1s.current_time); putchar(' ');
220
221         dsp_api.frame_ctr++;
222         dsp_api.r_page_used = 0;
223
224         /* Update pointers */
225         if (dsp_api.w_page == 0)
226                 dsp_api.db_w = (T_DB_MCU_TO_DSP *) BASE_API_W_PAGE_0;
227         else
228                 dsp_api.db_w = (T_DB_MCU_TO_DSP *) BASE_API_W_PAGE_1;
229
230         if (dsp_api.r_page == 0)
231                 dsp_api.db_r = (T_DB_DSP_TO_MCU *) BASE_API_R_PAGE_0;
232         else
233                 dsp_api.db_r = (T_DB_DSP_TO_MCU *) BASE_API_R_PAGE_1;
234
235         /* Reset MCU->DSP page */
236         dsp_api_memset((uint16_t *) dsp_api.db_w, sizeof(*dsp_api.db_w));
237
238         /* Update AFC */
239         afc_load_dsp();
240
241         if (dsp_api.ndb->d_error_status) {
242                 printf("DSP Error Status: %u\n", dsp_api.ndb->d_error_status);
243                 dsp_api.ndb->d_error_status = 0;
244         }
245
246         /* execute the sched_items that have been scheduled for this
247          * TDMA frame */
248         tdma_sched_execute();
249
250         if (dsp_api.r_page_used) {
251                 /* clear and switch the read page */
252                 dsp_api_memset((uint16_t *) dsp_api.db_r,
253                                 sizeof(*dsp_api.db_r));
254
255                 /* TSM30 does it (really needed ?):
256                  * Set crc result as "SB not found". */
257                 dsp_api.db_r->a_sch[0] = (1<<B_SCH_CRC);   /* B_SCH_CRC =1, BLUD =0 */
258
259                 dsp_api.r_page ^= 1;
260         }
261
262         //dsp_end_scenario();
263
264         /* schedule new / upcoming TDMA items */
265         mframe_schedule();
266         /* schedule new / upcoming one-shot events */
267         sched_gsmtime_execute(l1s.current_time.fn);
268
269         tdma_sched_advance();
270 }
271
272 /* ABORT command ********************************************************/
273
274 static int l1s_abort_cmd(__unused uint8_t p1, __unused uint8_t p2,
275                          __unused uint16_t p3)
276 {
277         putchart('A');
278
279         /* similar to l1s_reset_hw() without touching the TPU */
280
281         dsp_api.w_page = 0;
282         dsp_api.r_page = 0;
283         dsp_api.r_page_used = 0;
284         dsp_api.db_w = (T_DB_MCU_TO_DSP *) BASE_API_W_PAGE_0;
285         dsp_api.db_r = (T_DB_DSP_TO_MCU *) BASE_API_R_PAGE_0;
286
287         /* Reset task commands. */
288         dsp_api.db_w->d_task_d  = NO_DSP_TASK; /* Init. RX task to NO TASK */
289         dsp_api.db_w->d_task_u  = NO_DSP_TASK; /* Init. TX task to NO TASK */
290         dsp_api.db_w->d_task_ra = NO_DSP_TASK; /* Init. RA task to NO TASK */
291         dsp_api.db_w->d_task_md = NO_DSP_TASK; /* Init. MONITORING task to NO TASK */
292         dsp_api.ndb->d_dsp_page = 0;
293
294         /* Set "b_abort" to TRUE, dsp will reset current and pending tasks */
295         dsp_api.db_w->d_ctrl_system |= (1 << B_TASK_ABORT);
296         return 0;
297 }
298
299 void l1s_dsp_abort(void)
300 {
301         /* abort right now */
302         tdma_schedule(0, &l1s_abort_cmd, 0, 0, 0);
303 }
304
305 void l1s_tx_apc_helper(void)
306 {
307         int i;
308
309         /* Load the ApcOffset into the DSP */
310         #define  MY_OFFSET      4
311         dsp_api.ndb->d_apcoff = ABB_VAL(APCOFF, (1 << 6) | MY_OFFSET) | 1; /* 2x slope for the GTA-02 ramp */
312
313         /* Load the TX Power into the DSP */
314         /*
315            If the power is too low (below 0 dBm) the ramp is not OK,
316            especially for GSM-1800. However an MS does not send below
317            0dBm anyway.
318         */
319         dsp_api.db_w->d_power_ctl = ABB_VAL(AUXAPC, 0xC0); /* 2 dBm pulse with offset 4 (GSM-1800) */
320
321         /* Update the ramp according to the PCL */
322         for (i = 0; i < 16; i++)
323                 dsp_api.ndb->a_ramp[i] = ABB_VAL(APCRAM, twl3025_default_ramp[i]);
324
325         /* The Ramp Table is sent to ABB only once after RF init routine called */
326         dsp_api.db_w->d_ctrl_abb |= (1 << B_RAMP) | (1 << B_BULRAMPDEL);
327 }
328
329 /* Interrupt handler */
330 static void frame_irq(__unused enum irq_nr nr)
331 {
332         l1_sync();
333 }
334
335 /* reset the layer1 as part of synchronizing to a new cell */
336 void l1s_reset(void)
337 {
338         l1s.fb.mode = 0;
339         l1s.sb.synced = 0;
340         l1s.sb.count = 0;
341
342         /* reset scheduler and hardware */
343         mframe_reset();
344         tdma_sched_reset();
345         l1s_dsp_abort();
346 }
347
348 void l1s_init(void)
349 {
350         unsigned int i;
351
352         for (i = 0; i < ARRAY_SIZE(l1s.tx_queue); i++)
353                 INIT_LLIST_HEAD(&l1s.tx_queue[i]);
354
355         sched_gsmtime_init();
356
357         /* register FRAME interrupt as FIQ so it can interrupt normal IRQs */
358         irq_register_handler(IRQ_TPU_FRAME, &frame_irq);
359         irq_config(IRQ_TPU_FRAME, 1, 1, 0);
360         irq_enable(IRQ_TPU_FRAME);
361
362         /* configure timer 1 to be auto-reload and have a prescale of 12 (13MHz/12 == qbit clock) */
363         hwtimer_enable(1, 1);
364         hwtimer_load(1, (1875*4)-1);
365         hwtimer_config(1, 0, 1);
366         hwtimer_enable(1, 1);
367 }
368