staging: brcm80211: remove include file sbhndpio.h
[firefly-linux-kernel-4.4.55.git] / drivers / staging / brcm80211 / brcmsmac / wlc_antsel.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <wlc_cfg.h>
18
19 #ifdef WLANTSEL
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24
25 #include <osl.h>
26 #include <bcmdefs.h>
27 #include <bcmutils.h>
28 #include <siutils.h>
29 #include <bcmdevs.h>
30 #include <sbhnddma.h>
31 #include <wlioctl.h>
32
33 #include "d11.h"
34 #include "wlc_rate.h"
35 #include "wlc_key.h"
36 #include "wlc_scb.h"
37 #include "wlc_pub.h"
38 #include "wl_dbg.h"
39 #include "phy/wlc_phy_hal.h"
40 #include "wlc_bmac.h"
41 #include "wlc_channel.h"
42 #include "wlc_bsscfg.h"
43 #include "wlc_mac80211.h"
44 #include "wl_export.h"
45 #include "wlc_phy_shim.h"
46 #include "wlc_antsel.h"
47
48 /* useful macros */
49 #define WLC_ANTSEL_11N_0(ant)   ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
50 #define WLC_ANTSEL_11N_1(ant)   (((ant) & ANT_SELCFG_MASK) & 0xf)
51 #define WLC_ANTIDX_11N(ant)     (((WLC_ANTSEL_11N_0(ant)) << 2) + (WLC_ANTSEL_11N_1(ant)))
52 #define WLC_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
53 #define WLC_ANTSEL_11N(ant)     ((ant) & ANT_SELCFG_MASK)
54
55 /* antenna switch */
56 /* defines for no boardlevel antenna diversity */
57 #define ANT_SELCFG_DEF_2x2      0x01    /* default antenna configuration */
58
59 /* 2x3 antdiv defines and tables for GPIO communication */
60 #define ANT_SELCFG_NUM_2x3      3
61 #define ANT_SELCFG_DEF_2x3      0x01    /* default antenna configuration */
62
63 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
64 #define ANT_SELCFG_NUM_2x4      4
65 #define ANT_SELCFG_DEF_2x4      0x02    /* default antenna configuration */
66
67 /* static functions */
68 static int wlc_antsel_cfgupd(struct antsel_info *asi, wlc_antselcfg_t *antsel);
69 static u8 wlc_antsel_id2antcfg(struct antsel_info *asi, u8 id);
70 static u16 wlc_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg);
71 static void wlc_antsel_init_cfg(struct antsel_info *asi,
72                                 wlc_antselcfg_t *antsel,
73                                 bool auto_sel);
74
75 const u16 mimo_2x4_div_antselpat_tbl[] = {
76         0, 0, 0x9, 0xa,         /* ant0: 0 ant1: 2,3 */
77         0, 0, 0x5, 0x6,         /* ant0: 1 ant1: 2,3 */
78         0, 0, 0, 0,             /* n.a.              */
79         0, 0, 0, 0              /* n.a.              */
80 };
81
82 const u8 mimo_2x4_div_antselid_tbl[16] = {
83         0, 0, 0, 0, 0, 2, 3, 0,
84         0, 0, 1, 0, 0, 0, 0, 0  /* pat to antselid */
85 };
86
87 const u16 mimo_2x3_div_antselpat_tbl[] = {
88         16, 0, 1, 16,           /* ant0: 0 ant1: 1,2 */
89         16, 16, 16, 16,         /* n.a.              */
90         16, 2, 16, 16,          /* ant0: 2 ant1: 1   */
91         16, 16, 16, 16          /* n.a.              */
92 };
93
94 const u8 mimo_2x3_div_antselid_tbl[16] = {
95         0, 1, 2, 0, 0, 0, 0, 0,
96         0, 0, 0, 0, 0, 0, 0, 0  /* pat to antselid */
97 };
98
99 struct antsel_info *wlc_antsel_attach(struct wlc_info *wlc,
100                                       struct osl_info *osh,
101                                       struct wlc_pub *pub,
102                                       struct wlc_hw_info *wlc_hw) {
103         struct antsel_info *asi;
104
105         asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
106         if (!asi) {
107                 WL_ERROR("wl%d: wlc_antsel_attach: out of mem\n", pub->unit);
108                 return NULL;
109         }
110
111         asi->wlc = wlc;
112         asi->pub = pub;
113         asi->antsel_type = ANTSEL_NA;
114         asi->antsel_avail = false;
115         asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
116
117         if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
118                 switch (asi->antsel_antswitch) {
119                 case ANTSWITCH_TYPE_1:
120                 case ANTSWITCH_TYPE_2:
121                 case ANTSWITCH_TYPE_3:
122                         /* 4321/2 board with 2x3 switch logic */
123                         asi->antsel_type = ANTSEL_2x3;
124                         /* Antenna selection availability */
125                         if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
126                             ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
127                                 asi->antsel_avail = true;
128                         } else
129                             if (((u16) getintvar(asi->pub->vars, "aa2g") ==
130                                  3)
131                                 || ((u16) getintvar(asi->pub->vars, "aa5g")
132                                     == 3)) {
133                                 asi->antsel_avail = false;
134                         } else {
135                                 asi->antsel_avail = false;
136                                 WL_ERROR("wlc_antsel_attach: 2o3 board cfg invalid\n");
137                                 ASSERT(0);
138                         }
139                         break;
140                 default:
141                         break;
142                 }
143         } else if ((asi->pub->sromrev == 4) &&
144                    ((u16) getintvar(asi->pub->vars, "aa2g") == 7) &&
145                    ((u16) getintvar(asi->pub->vars, "aa5g") == 0)) {
146                 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
147                 asi->antsel_type = ANTSEL_2x3;
148                 asi->antsel_avail = true;
149         } else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
150                 asi->antsel_type = ANTSEL_2x4;
151                 asi->antsel_avail = true;
152         }
153
154         /* Set the antenna selection type for the low driver */
155         wlc_bmac_antsel_type_set(wlc_hw, asi->antsel_type);
156
157         /* Init (auto/manual) antenna selection */
158         wlc_antsel_init_cfg(asi, &asi->antcfg_11n, true);
159         wlc_antsel_init_cfg(asi, &asi->antcfg_cur, true);
160
161         return asi;
162 }
163
164 void wlc_antsel_detach(struct antsel_info *asi)
165 {
166         if (!asi)
167                 return;
168
169         kfree(asi);
170 }
171
172 void wlc_antsel_init(struct antsel_info *asi)
173 {
174         if ((asi->antsel_type == ANTSEL_2x3) ||
175             (asi->antsel_type == ANTSEL_2x4))
176                 wlc_antsel_cfgupd(asi, &asi->antcfg_11n);
177 }
178
179 /* boardlevel antenna selection: init antenna selection structure */
180 static void
181 wlc_antsel_init_cfg(struct antsel_info *asi, wlc_antselcfg_t *antsel,
182                     bool auto_sel)
183 {
184         if (asi->antsel_type == ANTSEL_2x3) {
185                 u8 antcfg_def = ANT_SELCFG_DEF_2x3 |
186                     ((asi->antsel_avail && auto_sel) ? ANT_SELCFG_AUTO : 0);
187                 antsel->ant_config[ANT_SELCFG_TX_DEF] = antcfg_def;
188                 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = antcfg_def;
189                 antsel->ant_config[ANT_SELCFG_RX_DEF] = antcfg_def;
190                 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = antcfg_def;
191                 antsel->num_antcfg = ANT_SELCFG_NUM_2x3;
192
193         } else if (asi->antsel_type == ANTSEL_2x4) {
194
195                 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x4;
196                 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x4;
197                 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x4;
198                 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x4;
199                 antsel->num_antcfg = ANT_SELCFG_NUM_2x4;
200
201         } else {                /* no antenna selection available */
202
203                 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x2;
204                 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x2;
205                 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x2;
206                 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x2;
207                 antsel->num_antcfg = 0;
208         }
209 }
210
211 void BCMFASTPATH
212 wlc_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
213                       u8 antselid, u8 fbantselid, u8 *antcfg,
214                       u8 *fbantcfg)
215 {
216         u8 ant;
217
218         /* if use default, assign it and return */
219         if (usedef) {
220                 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
221                 *fbantcfg = *antcfg;
222                 return;
223         }
224
225         if (!sel) {
226                 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
227                 *fbantcfg = *antcfg;
228
229         } else {
230                 ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
231                 if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
232                         *antcfg = wlc_antsel_id2antcfg(asi, antselid);
233                         *fbantcfg = wlc_antsel_id2antcfg(asi, fbantselid);
234                 } else {
235                         *antcfg =
236                             asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
237                         *fbantcfg = *antcfg;
238                 }
239         }
240         return;
241 }
242
243 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
244 u8 wlc_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
245 {
246         u8 antselid = 0;
247
248         if (asi->antsel_type == ANTSEL_2x4) {
249                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
250                 antselid = mimo_2x4_div_antselid_tbl[(antsel & 0xf)];
251                 return antselid;
252
253         } else if (asi->antsel_type == ANTSEL_2x3) {
254                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
255                 antselid = mimo_2x3_div_antselid_tbl[(antsel & 0xf)];
256                 return antselid;
257         }
258
259         return antselid;
260 }
261
262 /* boardlevel antenna selection: convert id to ant_cfg */
263 static u8 wlc_antsel_id2antcfg(struct antsel_info *asi, u8 id)
264 {
265         u8 antcfg = ANT_SELCFG_DEF_2x2;
266
267         if (asi->antsel_type == ANTSEL_2x4) {
268                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
269                 antcfg = (((id & 0x2) << 3) | ((id & 0x1) + 2));
270                 return antcfg;
271
272         } else if (asi->antsel_type == ANTSEL_2x3) {
273                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
274                 antcfg = (((id & 0x02) << 4) | ((id & 0x1) + 1));
275                 return antcfg;
276         }
277
278         return antcfg;
279 }
280
281 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
282 static u16 wlc_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
283 {
284         u8 idx = WLC_ANTIDX_11N(WLC_ANTSEL_11N(ant_cfg));
285         u16 mimo_antsel = 0;
286
287         if (asi->antsel_type == ANTSEL_2x4) {
288                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
289                 mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
290                 return mimo_antsel;
291
292         } else if (asi->antsel_type == ANTSEL_2x3) {
293                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
294                 mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
295                 return mimo_antsel;
296         }
297
298         return mimo_antsel;
299 }
300
301 /* boardlevel antenna selection: ucode interface control */
302 static int wlc_antsel_cfgupd(struct antsel_info *asi, wlc_antselcfg_t *antsel)
303 {
304         struct wlc_info *wlc = asi->wlc;
305         u8 ant_cfg;
306         u16 mimo_antsel;
307
308         ASSERT(asi->antsel_type != ANTSEL_NA);
309
310         /* 1) Update TX antconfig for all frames that are not unicast data
311          *    (aka default TX)
312          */
313         ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
314         mimo_antsel = wlc_antsel_antcfg2antsel(asi, ant_cfg);
315         wlc_write_shm(wlc, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
316         /* Update driver stats for currently selected default tx/rx antenna config */
317         asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
318
319         /* 2) Update RX antconfig for all frames that are not unicast data
320          *    (aka default RX)
321          */
322         ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
323         mimo_antsel = wlc_antsel_antcfg2antsel(asi, ant_cfg);
324         wlc_write_shm(wlc, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
325         /* Update driver stats for currently selected default tx/rx antenna config */
326         asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;
327
328         return 0;
329 }
330
331 #endif                          /* WLANTSEL */