2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
22 #include <linux/kernel.h>
35 #include <wlc_mac80211.h>
37 #include <wlc_phy_hal.h>
38 #include <wl_export.h>
39 #include <wlc_antsel.h>
40 #include <wlc_phy_shim.h>
43 #define WLC_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
44 #define WLC_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
45 #define WLC_ANTIDX_11N(ant) (((WLC_ANTSEL_11N_0(ant)) << 2) + (WLC_ANTSEL_11N_1(ant)))
46 #define WLC_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
47 #define WLC_ANTSEL_11N(ant) ((ant) & ANT_SELCFG_MASK)
50 /* defines for no boardlevel antenna diversity */
51 #define ANT_SELCFG_DEF_2x2 0x01 /* default antenna configuration */
53 /* 2x3 antdiv defines and tables for GPIO communication */
54 #define ANT_SELCFG_NUM_2x3 3
55 #define ANT_SELCFG_DEF_2x3 0x01 /* default antenna configuration */
57 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
58 #define ANT_SELCFG_NUM_2x4 4
59 #define ANT_SELCFG_DEF_2x4 0x02 /* default antenna configuration */
61 /* static functions */
62 static int wlc_antsel_cfgupd(antsel_info_t *asi, wlc_antselcfg_t *antsel);
63 static u8 wlc_antsel_id2antcfg(antsel_info_t *asi, u8 id);
64 static u16 wlc_antsel_antcfg2antsel(antsel_info_t *asi, u8 ant_cfg);
65 static void wlc_antsel_init_cfg(antsel_info_t *asi, wlc_antselcfg_t *antsel,
68 const u16 mimo_2x4_div_antselpat_tbl[] = {
69 0, 0, 0x9, 0xa, /* ant0: 0 ant1: 2,3 */
70 0, 0, 0x5, 0x6, /* ant0: 1 ant1: 2,3 */
71 0, 0, 0, 0, /* n.a. */
75 const u8 mimo_2x4_div_antselid_tbl[16] = {
76 0, 0, 0, 0, 0, 2, 3, 0,
77 0, 0, 1, 0, 0, 0, 0, 0 /* pat to antselid */
80 const u16 mimo_2x3_div_antselpat_tbl[] = {
81 16, 0, 1, 16, /* ant0: 0 ant1: 1,2 */
82 16, 16, 16, 16, /* n.a. */
83 16, 2, 16, 16, /* ant0: 2 ant1: 1 */
84 16, 16, 16, 16 /* n.a. */
87 const u8 mimo_2x3_div_antselid_tbl[16] = {
88 0, 1, 2, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0 /* pat to antselid */
92 antsel_info_t *wlc_antsel_attach(wlc_info_t *wlc, osl_t *osh,
94 wlc_hw_info_t *wlc_hw) {
97 asi = kzalloc(sizeof(antsel_info_t), GFP_ATOMIC);
99 WL_ERROR(("wl%d: wlc_antsel_attach: out of mem\n", pub->unit));
105 asi->antsel_type = ANTSEL_NA;
106 asi->antsel_avail = false;
107 asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
109 if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
110 switch (asi->antsel_antswitch) {
111 case ANTSWITCH_TYPE_1:
112 case ANTSWITCH_TYPE_2:
113 case ANTSWITCH_TYPE_3:
114 /* 4321/2 board with 2x3 switch logic */
115 asi->antsel_type = ANTSEL_2x3;
116 /* Antenna selection availability */
117 if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
118 ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
119 asi->antsel_avail = true;
121 if (((u16) getintvar(asi->pub->vars, "aa2g") ==
123 || ((u16) getintvar(asi->pub->vars, "aa5g")
125 asi->antsel_avail = false;
127 asi->antsel_avail = false;
128 WL_ERROR(("wlc_antsel_attach: 2o3 board cfg invalid\n"));
135 } else if ((asi->pub->sromrev == 4) &&
136 ((u16) getintvar(asi->pub->vars, "aa2g") == 7) &&
137 ((u16) getintvar(asi->pub->vars, "aa5g") == 0)) {
138 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
139 asi->antsel_type = ANTSEL_2x3;
140 asi->antsel_avail = true;
141 } else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
142 asi->antsel_type = ANTSEL_2x4;
143 asi->antsel_avail = true;
146 /* Set the antenna selection type for the low driver */
147 wlc_bmac_antsel_type_set(wlc_hw, asi->antsel_type);
149 /* Init (auto/manual) antenna selection */
150 wlc_antsel_init_cfg(asi, &asi->antcfg_11n, true);
151 wlc_antsel_init_cfg(asi, &asi->antcfg_cur, true);
156 void wlc_antsel_detach(antsel_info_t *asi)
161 MFREE(asi->pub->osh, asi, sizeof(antsel_info_t));
164 void wlc_antsel_init(antsel_info_t *asi)
166 if ((asi->antsel_type == ANTSEL_2x3) ||
167 (asi->antsel_type == ANTSEL_2x4))
168 wlc_antsel_cfgupd(asi, &asi->antcfg_11n);
171 /* boardlevel antenna selection: init antenna selection structure */
173 wlc_antsel_init_cfg(antsel_info_t *asi, wlc_antselcfg_t *antsel,
176 if (asi->antsel_type == ANTSEL_2x3) {
177 u8 antcfg_def = ANT_SELCFG_DEF_2x3 |
178 ((asi->antsel_avail && auto_sel) ? ANT_SELCFG_AUTO : 0);
179 antsel->ant_config[ANT_SELCFG_TX_DEF] = antcfg_def;
180 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = antcfg_def;
181 antsel->ant_config[ANT_SELCFG_RX_DEF] = antcfg_def;
182 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = antcfg_def;
183 antsel->num_antcfg = ANT_SELCFG_NUM_2x3;
185 } else if (asi->antsel_type == ANTSEL_2x4) {
187 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x4;
188 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x4;
189 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x4;
190 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x4;
191 antsel->num_antcfg = ANT_SELCFG_NUM_2x4;
193 } else { /* no antenna selection available */
195 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x2;
196 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x2;
197 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x2;
198 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x2;
199 antsel->num_antcfg = 0;
204 wlc_antsel_antcfg_get(antsel_info_t *asi, bool usedef, bool sel,
205 u8 antselid, u8 fbantselid, u8 *antcfg,
210 /* if use default, assign it and return */
212 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
218 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
222 ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
223 if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
224 *antcfg = wlc_antsel_id2antcfg(asi, antselid);
225 *fbantcfg = wlc_antsel_id2antcfg(asi, fbantselid);
228 asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
235 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
236 u8 wlc_antsel_antsel2id(antsel_info_t *asi, u16 antsel)
240 if (asi->antsel_type == ANTSEL_2x4) {
241 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
242 antselid = mimo_2x4_div_antselid_tbl[(antsel & 0xf)];
245 } else if (asi->antsel_type == ANTSEL_2x3) {
246 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
247 antselid = mimo_2x3_div_antselid_tbl[(antsel & 0xf)];
254 /* boardlevel antenna selection: convert id to ant_cfg */
255 static u8 wlc_antsel_id2antcfg(antsel_info_t *asi, u8 id)
257 u8 antcfg = ANT_SELCFG_DEF_2x2;
259 if (asi->antsel_type == ANTSEL_2x4) {
260 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
261 antcfg = (((id & 0x2) << 3) | ((id & 0x1) + 2));
264 } else if (asi->antsel_type == ANTSEL_2x3) {
265 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
266 antcfg = (((id & 0x02) << 4) | ((id & 0x1) + 1));
273 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
274 static u16 wlc_antsel_antcfg2antsel(antsel_info_t *asi, u8 ant_cfg)
276 u8 idx = WLC_ANTIDX_11N(WLC_ANTSEL_11N(ant_cfg));
279 if (asi->antsel_type == ANTSEL_2x4) {
280 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
281 mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
284 } else if (asi->antsel_type == ANTSEL_2x3) {
285 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
286 mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
293 /* boardlevel antenna selection: ucode interface control */
294 static int wlc_antsel_cfgupd(antsel_info_t *asi, wlc_antselcfg_t *antsel)
296 wlc_info_t *wlc = asi->wlc;
300 ASSERT(asi->antsel_type != ANTSEL_NA);
302 /* 1) Update TX antconfig for all frames that are not unicast data
305 ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
306 mimo_antsel = wlc_antsel_antcfg2antsel(asi, ant_cfg);
307 wlc_write_shm(wlc, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
308 /* Update driver stats for currently selected default tx/rx antenna config */
309 asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
311 /* 2) Update RX antconfig for all frames that are not unicast data
314 ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
315 mimo_antsel = wlc_antsel_antcfg2antsel(asi, ant_cfg);
316 wlc_write_shm(wlc, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
317 /* Update driver stats for currently selected default tx/rx antenna config */
318 asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;
323 #endif /* WLANTSEL */