1 /* Layer 1 - FCCH and SCH burst handling */
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.
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.
32 #include <byteorder.h>
33 #include <osmocom/gsm/gsm_utils.h>
34 #include <osmocom/core/msgb.h>
35 #include <calypso/dsp_api.h>
36 #include <calypso/irq.h>
37 #include <calypso/tpu.h>
38 #include <calypso/tsp.h>
39 #include <calypso/dsp.h>
40 #include <calypso/timer.h>
41 #include <comm/sercomm.h>
43 #include <layer1/sync.h>
44 #include <layer1/afc.h>
45 #include <layer1/toa.h>
46 #include <layer1/tdma_sched.h>
47 #include <layer1/mframe_sched.h>
48 #include <layer1/tpu_window.h>
49 #include <layer1/l23_api.h>
51 #include <l1ctl_proto.h>
53 #define FB0_RETRY_COUNT 3
54 #define AFC_RETRY_COUNT 30
56 extern uint16_t rf_arfcn; // TODO
59 uint32_t fnr_report; /* frame number when DSP reported it */
60 int attempt; /* which attempt was this ? */
77 struct l1ctl_fbsb_req req;
78 int16_t initial_freq_err;
83 static struct l1a_fb_state fbs;
84 static struct mon_state *last_fb = &fbs.mon;
86 static void dump_mon_state(struct mon_state *fb)
89 printf("(%u:%u): TOA=%5u, Power=%4ddBm, Angle=%5dHz, "
90 "SNR=%04x(%d.%u) OFFSET=%u SYNCHRO=%u\n",
91 fb->fnr_report, fb->attempt, fb->toa,
92 agc_inp_dbm8_by_pm(fb->pm)/8, ANGLE_TO_FREQ(fb->angle),
93 fb->snr, l1s_snr_int(fb->snr), l1s_snr_fract(fb->snr),
94 tpu_get_offset(), tpu_get_synchro());
96 printf("(%u:%u): TOA=%5u, Power=%4ddBm, Angle=%5dHz\n",
97 fb->fnr_report, fb->attempt, fb->toa,
98 agc_inp_dbm8_by_pm(fb->pm)/8, ANGLE_TO_FREQ(fb->angle));
102 static int l1ctl_fbsb_resp(uint8_t res)
105 struct l1ctl_fbsb_conf *resp;
107 msg = l1_create_l2_msg(L1CTL_FBSB_CONF, fbs.mon.time.fn,
108 l1s_snr_int(fbs.mon.snr),
113 resp = (struct l1ctl_fbsb_conf *) msgb_put(msg, sizeof(*resp));
114 resp->initial_freq_err = htons(fbs.initial_freq_err);
116 resp->bsic = fbs.mon.bsic;
118 /* no need to set BSIC, as it is never used here */
119 l1_queue_for_l2(msg);
124 /* SCH Burst Detection ********************************************************/
126 /* determine the GSM time and BSIC from a Sync Burst */
127 static uint8_t l1s_decode_sb(struct gsm_time *time, uint32_t sb)
129 uint8_t bsic = (sb >> 2) & 0x3f;
132 memset(time, 0, sizeof(*time));
134 /* TS 05.02 Chapter 3.3.2.2.1 SCH Frame Numbers */
135 time->t1 = ((sb >> 23) & 1) | ((sb >> 7) & 0x1fe) | ((sb << 9) & 0x600);
136 time->t2 = (sb >> 18) & 0x1f;
137 t3p = ((sb >> 24) & 1) | ((sb >> 15) & 6);
138 time->t3 = t3p*10 + 1;
140 /* TS 05.02 Chapter 4.3.3 TDMA frame number */
141 time->fn = gsm_gsmtime2fn(time);
143 time->tc = (time->fn / 51) % 8;
148 static void read_sb_result(struct mon_state *st, int attempt)
150 st->toa = dsp_api.db_r->a_serv_demod[D_TOA];
151 st->pm = dsp_api.db_r->a_serv_demod[D_PM]>>3;
152 st->angle = dsp_api.db_r->a_serv_demod[D_ANGLE];
153 st->snr = dsp_api.db_r->a_serv_demod[D_SNR];
155 st->freq_diff = ANGLE_TO_FREQ(st->angle);
156 st->fnr_report = l1s.current_time.fn;
157 st->attempt = attempt;
161 if (st->snr > AFC_SNR_THRESHOLD)
162 afc_input(st->freq_diff, rf_arfcn, 1);
164 afc_input(st->freq_diff, rf_arfcn, 0);
166 dsp_api.r_page_used = 1;
169 /* Note: When we get the SB response, it is 2 TDMA frames after the SB
170 * actually happened, as it is a "C W W R" task */
171 #define SB2_LATENCY 2
173 static int l1s_sbdet_resp(__unused uint8_t p1, uint8_t attempt,
174 __unused uint16_t p3)
177 int qbits, fn_offset;
178 struct l1_cell_info *cinfo = &l1s.serving_cell;
179 int fnr_delta, bits_delta;
180 struct l1ctl_sync_new_ccch_resp *l1;
185 if (dsp_api.db_r->a_sch[0] & (1<<B_SCH_CRC)) {
186 /* mark READ page as being used */
187 dsp_api.r_page_used = 1;
189 /* after 2nd attempt, we failed */
191 last_fb->attempt = 13;
192 l1s_compl_sched(L1_COMPL_FB);
195 /* after 1st attempt, we simply wait for 2nd */
199 printf("SB%d ", attempt);
200 read_sb_result(last_fb, attempt);
202 sb = dsp_api.db_r->a_sch[3] | dsp_api.db_r->a_sch[4] << 16;
203 fbs.mon.bsic = l1s_decode_sb(&fbs.mon.time, sb);
204 printf("=> SB 0x%08x: BSIC=%u ", sb, fbs.mon.bsic);
205 l1s_time_dump(&fbs.mon.time);
207 l1s.serving_cell.bsic = fbs.mon.bsic;
209 /* calculate synchronisation value (TODO: only complete for qbits) */
211 qbits = last_fb->toa * 4;
212 fn_offset = l1s.current_time.fn; // TODO
214 if (qbits > QBITS_PER_TDMA) {
215 qbits -= QBITS_PER_TDMA;
217 } else if (qbits < 0) {
218 qbits += QBITS_PER_TDMA;
222 fnr_delta = last_fb->fnr_report - attempt;
223 bits_delta = fnr_delta * BITS_PER_TDMA;
225 cinfo->fn_offset = fnr_delta;
226 cinfo->time_alignment = qbits;
227 cinfo->arfcn = rf_arfcn;
229 if (last_fb->toa > bits_delta)
230 printf("=> DSP reports SB in bit that is %d bits in the "
231 "future?!?\n", last_fb->toa - bits_delta);
233 printf(" qbits=%u\n", qbits);
235 synchronize_tdma(&l1s.serving_cell);
237 /* if we have recived a SYNC burst, update our local GSM time */
238 gsm_fn2gsmtime(&l1s.current_time, fbs.mon.time.fn + SB2_LATENCY);
239 /* compute next time from new current time */
240 l1s.next_time = l1s.current_time;
241 l1s_time_inc(&l1s.next_time, 1);
243 /* If we call tdma_sched_reset(), which is only needed if there
244 * are further l1s_sbdet_resp() scheduled, we will bring
245 * dsp_api.db_r and dsp_api.db_w out of sync because we changed
246 * dsp_api.db_w for l1s_sbdet_cmd() and canceled
247 * l1s_sbdet_resp() which would change dsp_api.db_r. The DSP
248 * however expects dsp_api.db_w and dsp_api.db_r to be in sync
249 * (either "0 - 0" or "1 - 1"). So we have to bring dsp_api.db_w
250 * and dsp_api.db_r into sync again, otherwise NB reading will
251 * complain. We probably don't need the Abort command and could
252 * just bring dsp_api.db_w and dsp_api.db_r into sync. */
259 /* enable the MF Task for BCCH reading */
260 mframe_enable(MF_TASK_BCCH_NORM);
261 if (l1s.serving_cell.ccch_mode == CCCH_MODE_COMBINED)
262 mframe_enable(MF_TASK_CCCH_COMB);
263 else if (l1s.serving_cell.ccch_mode == CCCH_MODE_NON_COMBINED)
264 mframe_enable(MF_TASK_CCCH);
266 l1s_compl_sched(L1_COMPL_FB);
271 static int l1s_sbdet_cmd(__unused uint8_t p1, __unused uint8_t p2,
272 __unused uint16_t p3)
279 dsp_api.db_w->d_task_md = SB_DSP_TASK;
280 dsp_api.ndb->d_fb_mode = 0; /* wideband search */
283 l1s_rx_win_ctrl(rf_arfcn, L1_RXWIN_SB, 0);
288 /* This is how it is done by the TSM30 */
289 static const struct tdma_sched_item sb_sched_set[] = {
290 SCHED_ITEM_DT(l1s_sbdet_cmd, 0, 0, 1), SCHED_END_FRAME(),
291 SCHED_ITEM_DT(l1s_sbdet_cmd, 0, 0, 2), SCHED_END_FRAME(),
293 SCHED_ITEM(l1s_sbdet_resp, -4, 0, 1), SCHED_END_FRAME(),
294 SCHED_ITEM(l1s_sbdet_resp, -4, 0, 2), SCHED_END_FRAME(),
298 void l1s_sb_test(uint8_t base_fn)
300 tdma_schedule_set(base_fn, sb_sched_set, 0);
302 /* FCCH Burst *****************************************************************/
304 static int read_fb_result(struct mon_state *st, int attempt)
306 st->toa = dsp_api.ndb->a_sync_demod[D_TOA];
307 st->pm = dsp_api.ndb->a_sync_demod[D_PM]>>3;
308 st->angle = dsp_api.ndb->a_sync_demod[D_ANGLE];
309 st->snr = dsp_api.ndb->a_sync_demod[D_SNR];
311 //last_fb->angle = clip_int16(last_fb->angle, AFC_MAX_ANGLE);
312 st->freq_diff = ANGLE_TO_FREQ(last_fb->angle);
313 st->fnr_report = l1s.current_time.fn;
314 st->attempt = attempt;
318 dsp_api.ndb->d_fb_det = 0;
319 dsp_api.ndb->a_sync_demod[D_TOA] = 0; /* TSM30 does it (really needed ?) */
321 /* Update AFC with current frequency offset */
322 afc_correct(st->freq_diff, rf_arfcn);
324 //tpu_dsp_frameirq_enable();
328 static void fbinfo2cellinfo(struct l1_cell_info *cinfo,
329 const struct mon_state *mon)
331 int ntdma, qbits, fn_offset, fnr_delta, bits_delta;
333 /* FIXME: where did this magic 23 come from? */
336 if (last_fb->toa < 0) {
337 qbits = (last_fb->toa + BITS_PER_TDMA) * 4;
340 ntdma = (last_fb->toa) / BITS_PER_TDMA;
341 qbits = (last_fb->toa - ntdma * BITS_PER_TDMA) * 4;
344 fn_offset = l1s.current_time.fn - last_fb->attempt + ntdma;
345 fnr_delta = last_fb->fnr_report - last_fb->attempt;
346 bits_delta = fnr_delta * BITS_PER_TDMA;
348 cinfo->fn_offset = fnr_delta;
349 cinfo->time_alignment = qbits;
350 cinfo->arfcn = rf_arfcn;
352 if (last_fb->toa > bits_delta)
353 printf("=> DSP reports FB in bit that is %d bits in "
354 "the future?!?\n", last_fb->toa - bits_delta);
356 int fb_fnr = (last_fb->fnr_report - last_fb->attempt)
357 + last_fb->toa/BITS_PER_TDMA;
358 printf("=>FB @ FNR %u fn_offset=%d qbits=%u\n",
359 fb_fnr, fn_offset, qbits);
363 /* scheduler callback to issue a FB detection task to the DSP */
364 static int l1s_fbdet_cmd(__unused uint8_t p1, __unused uint8_t p2,
373 l1s.fb.mode = fb_mode;
375 /* Tell the RF frontend to set the gain appropriately */
376 rffe_compute_gain(-85, CAL_DSP_TGT_BB_LVL);
379 dsp_api.db_w->d_task_md = FB_DSP_TASK; /* maybe with I/Q swap? */
380 dsp_api.ndb->d_fb_mode = fb_mode;
383 l1s_rx_win_ctrl(fbs.req.band_arfcn, L1_RXWIN_FB, 0);
389 #define FB0_SNR_THRESH 2000
390 #define FB1_SNR_THRESH 3000
392 #define FB0_SNR_THRESH 0
393 #define FB1_SNR_THRESH 0
396 static const struct tdma_sched_item fb_sched_set[];
398 /* scheduler callback to check for a FB detection response */
399 static int l1s_fbdet_resp(__unused uint8_t p1, uint8_t attempt,
404 if (!dsp_api.ndb->d_fb_det) {
405 /* we did not detect a FB */
407 /* attempt < 12, do nothing */
411 /* attempt >= 12, we simply don't find one */
413 /* If we don't reset here, we get DSP DMA errors */
416 if (fbs.fb_retries < FB0_RETRY_COUNT) {
417 /* retry once more */
418 tdma_schedule_set(1, fb_sched_set, 0);
421 last_fb->attempt = 13;
422 l1s_compl_sched(L1_COMPL_FB);
428 /* We found a frequency burst, reset everything */
431 printf("FB%u ", dsp_api.ndb->d_fb_mode);
432 read_fb_result(last_fb, attempt);
434 /* if this is the first success, save freq err */
435 if (!fbs.initial_freq_err)
436 fbs.initial_freq_err = last_fb->freq_diff;
438 /* If we don't reset here, we get DSP DMA errors */
441 /* Immediately schedule further TDMA tasklets, if requested. Doing
442 * this directly from L1S means we can do this quickly without any
443 * additional delays */
445 if (fbs.req.flags & L1CTL_FBSB_F_FB1) {
446 /* If we don't reset here, we get DSP DMA errors */
448 /* FIXME: don't only use the last but an average */
449 if (abs(last_fb->freq_diff) < fbs.req.freq_err_thresh1 &&
450 last_fb->snr > FB0_SNR_THRESH) {
451 /* continue with FB1 task in DSP */
452 tdma_schedule_set(1, fb_sched_set, 1);
454 if (fbs.afc_retries < AFC_RETRY_COUNT) {
455 tdma_schedule_set(1, fb_sched_set, 0);
459 last_fb->attempt = 13;
460 l1s_compl_sched(L1_COMPL_FB);
464 l1s_compl_sched(L1_COMPL_FB);
465 } else if (fb_mode == 1) {
466 if (fbs.req.flags & L1CTL_FBSB_F_SB) {
469 /* FIXME: where did this magic 23 come from? */
472 if (last_fb->toa < 0) {
473 qbits = (last_fb->toa + BITS_PER_TDMA) * 4;
476 ntdma = (last_fb->toa) / BITS_PER_TDMA;
477 qbits = (last_fb->toa - ntdma * BITS_PER_TDMA) * 4;
481 int fn_offset = l1s.current_time.fn - last_fb->attempt + ntdma;
482 int delay = fn_offset + 11 - l1s.current_time.fn - 1;
483 printf(" fn_offset=%d (fn=%u + attempt=%u + ntdma = %d)\m",
484 fn_offset, l1s.current_time.fn, last_fb->attempt, ntdma);
485 printf(" delay=%d (fn_offset=%d + 11 - fn=%u - 1\n", delay,
486 fn_offset, l1s.current_time.fn);
487 printf(" scheduling next FB/SB detection task with delay %u\n", delay);
488 if (abs(last_fb->freq_diff) < fbs.req.freq_err_thresh2 &&
489 last_fb->snr > FB1_SNR_THRESH) {
490 /* synchronize before reading SB */
491 fbinfo2cellinfo(&l1s.serving_cell, last_fb);
492 synchronize_tdma(&l1s.serving_cell);
493 tdma_schedule_set(delay, sb_sched_set, 0);
495 tdma_schedule_set(delay, fb_sched_set, 1);
497 l1s_compl_sched(L1_COMPL_FB);
504 static const struct tdma_sched_item fb_sched_set[] = {
505 SCHED_ITEM_DT(l1s_fbdet_cmd, 0, 0, 0), SCHED_END_FRAME(),
507 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 1), SCHED_END_FRAME(),
508 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 2), SCHED_END_FRAME(),
509 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 3), SCHED_END_FRAME(),
510 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 4), SCHED_END_FRAME(),
511 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 5), SCHED_END_FRAME(),
512 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 6), SCHED_END_FRAME(),
513 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 7), SCHED_END_FRAME(),
514 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 8), SCHED_END_FRAME(),
515 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 9), SCHED_END_FRAME(),
516 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 10), SCHED_END_FRAME(),
517 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 11), SCHED_END_FRAME(),
518 SCHED_ITEM(l1s_fbdet_resp, -4, 0, 12), SCHED_END_FRAME(),
522 /* Asynchronous completion handler for FB detection */
523 static void l1a_fb_compl(__unused enum l1_compl c)
525 struct l1_cell_info *cinfo = &l1s.serving_cell;
527 if (last_fb->attempt >= 13) {
528 /* FB detection failed, signal this via L1CTL */
529 return l1ctl_fbsb_resp(255);
532 /* FIME: use l1s.neigh_cell[fbs.cinfo_idx] */
533 fbinfo2cellinfo(&l1s.serving_cell, last_fb);
535 /* send FBSB_CONF success message via L1CTL */
539 void l1s_fbsb_req(uint8_t base_fn, struct l1ctl_fbsb_req *req)
541 /* copy + endian convert request data */
542 fbs.req.band_arfcn = ntohs(req->band_arfcn);
543 fbs.req.timeout = ntohs(req->timeout);
544 fbs.req.freq_err_thresh1 = ntohs(req->freq_err_thresh1);
545 fbs.req.freq_err_thresh2 = ntohs(req->freq_err_thresh2);
546 fbs.req.num_freqerr_avg = req->num_freqerr_avg;
547 fbs.req.flags = req->flags;
548 fbs.req.sync_info_idx = req->sync_info_idx;
550 /* clear initial frequency error */
551 fbs.initial_freq_err = 0;
555 /* Make sure we start at a 'center' AFCDAC output value */
558 /* Reset the TOA loop counters */
561 if (fbs.req.flags & L1CTL_FBSB_F_FB0)
562 tdma_schedule_set(base_fn, fb_sched_set, 0);
563 else if (fbs.req.flags & L1CTL_FBSB_F_FB1)
564 tdma_schedule_set(base_fn, fb_sched_set, 0);
565 else if (fbs.req.flags & L1CTL_FBSB_F_SB)
566 tdma_schedule_set(base_fn, sb_sched_set, 0);
570 static __attribute__ ((constructor)) void l1s_prim_fbsb_init(void)
572 l1s.completion[L1_COMPL_FB] = &l1a_fb_compl;