c3465dad18ff49a43c8997189d44835f0086ec52
[osmocom-bb.git] / src / target / firmware / layer1 / prim_rach.c
1 /* Layer 1 Random Access Channel Burst */
2
3 /* (C) 2010 by Dieter Spaar <spaar@mirider.augusta.de>
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <defines.h>
30 #include <debug.h>
31 #include <memory.h>
32 #include <byteorder.h>
33 #include <osmocore/gsm_utils.h>
34 #include <osmocore/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>
42
43 #include <layer1/sync.h>
44 #include <layer1/async.h>
45 #include <layer1/tdma_sched.h>
46 #include <layer1/tpu_window.h>
47 #include <layer1/l23_api.h>
48
49 #include <l1a_l23_interface.h>
50
51 struct {
52         uint32_t fn;
53         uint16_t band_arfcn;
54 } last_rach;
55
56 /* p1: type of operation (0: one NB, 1: one RACH burst, 2: four NB */
57 static int l1s_tx_rach_cmd(__unused uint8_t p1, __unused uint8_t p2, __unused uint16_t p3)
58 {
59         int i;
60         uint16_t  *info_ptr;
61         uint8_t data[2];
62
63         putchart('T');
64
65         l1s_tx_apc_helper();
66
67         data[0] = l1s.serving_cell.bsic << 2;
68         data[1] = l1s.rach.ra;
69
70         info_ptr = &dsp_api.ndb->d_rach;
71         info_ptr[0] = ((uint16_t)(data[0])) | ((uint16_t)(data[1])<<8);
72
73         dsp_api.db_w->d_task_ra = RACH_DSP_TASK;
74         dsp_end_scenario();
75
76         l1s_tx_win_ctrl(l1s.serving_cell.arfcn, L1_TXWIN_AB, 0);
77         tpu_end_scenario();
78
79         return 0;
80 }
81
82 /* p1: type of operation (0: one NB, 1: one RACH burst, 2: four NB */
83 static int l1s_tx_rach_resp(__unused uint8_t p1, __unused uint8_t burst_id,
84                             __unused uint16_t p3)
85 {
86         putchart('t');
87
88         dsp_api.r_page_used = 1;
89
90         /* schedule a confirmation back indicating the GSM time at which
91          * the RACH burst was transmitted to the BTS */
92         last_rach.fn = l1s.current_time.fn - 1;
93         last_rach.band_arfcn = l1s.serving_cell.arfcn;
94         l1s_compl_sched(L1_COMPL_FB);
95
96         return 0;
97 }
98
99 /* sched sets for uplink */
100 const struct tdma_sched_item rach_sched_set_ul[] = {
101         SCHED_ITEM(l1s_tx_rach_cmd, 1, 0),      SCHED_END_FRAME(),
102                                                 SCHED_END_FRAME(),
103         SCHED_ITEM(l1s_tx_rach_resp, 1, 0),     SCHED_END_FRAME(),
104         SCHED_END_SET()
105 };
106
107 /* Asynchronous completion handler for FB detection */
108 static void l1a_rach_compl(__unused enum l1_compl c)
109 {
110         struct msgb *msg;
111
112         msg = l1_create_l2_msg(L1CTL_RACH_RESP, last_rach.fn, 0,
113                                 last_rach.band_arfcn);
114         l1_queue_for_l2(msg);
115 }
116
117 /* request a RACH request at the next multiframe T3 = fn51 */
118 void l1a_rach_req(uint8_t fn51, uint8_t ra)
119 {
120         uint32_t fn_sched;
121
122         l1a_lock_sync();
123         l1s.rach.ra = ra;
124         /* TODO: can we wrap here? I don't think so */
125         fn_sched = l1s.current_time.fn - l1s.current_time.t3;
126         fn_sched += fn51;
127         sched_gsmtime(rach_sched_set_ul, fn_sched, 0);
128         l1a_unlock_sync();
129
130         memset(&last_rach, 0, sizeof(last_rach));
131         l1s.completion[L1_COMPL_FB] = &l1a_rach_compl;
132 }