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