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.
17 #include <linux/slab.h>
18 #include <net/mac80211.h>
25 #define ANT_SELCFG_AUTO 0x80 /* bit indicates antenna sel AUTO */
26 #define ANT_SELCFG_MASK 0x33 /* antenna configuration mask */
27 #define ANT_SELCFG_TX_UNICAST 0 /* unicast tx antenna configuration */
28 #define ANT_SELCFG_RX_UNICAST 1 /* unicast rx antenna configuration */
29 #define ANT_SELCFG_TX_DEF 2 /* default tx antenna configuration */
30 #define ANT_SELCFG_RX_DEF 3 /* default rx antenna configuration */
33 #define BRCMS_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
34 #define BRCMS_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
35 #define BRCMS_ANTIDX_11N(ant) (((BRCMS_ANTSEL_11N_0(ant)) << 2) +\
36 (BRCMS_ANTSEL_11N_1(ant)))
37 #define BRCMS_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
38 #define BRCMS_ANTSEL_11N(ant) ((ant) & ANT_SELCFG_MASK)
41 /* defines for no boardlevel antenna diversity */
42 #define ANT_SELCFG_DEF_2x2 0x01 /* default antenna configuration */
44 /* 2x3 antdiv defines and tables for GPIO communication */
45 #define ANT_SELCFG_NUM_2x3 3
46 #define ANT_SELCFG_DEF_2x3 0x01 /* default antenna configuration */
48 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
49 #define ANT_SELCFG_NUM_2x4 4
50 #define ANT_SELCFG_DEF_2x4 0x02 /* default antenna configuration */
52 /* static functions */
53 static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
54 struct brcms_antselcfg *antsel);
55 static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id);
56 static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg);
57 static void brcms_c_antsel_init_cfg(struct antsel_info *asi,
58 struct brcms_antselcfg *antsel,
61 const u16 mimo_2x4_div_antselpat_tbl[] = {
62 0, 0, 0x9, 0xa, /* ant0: 0 ant1: 2,3 */
63 0, 0, 0x5, 0x6, /* ant0: 1 ant1: 2,3 */
64 0, 0, 0, 0, /* n.a. */
68 const u8 mimo_2x4_div_antselid_tbl[16] = {
69 0, 0, 0, 0, 0, 2, 3, 0,
70 0, 0, 1, 0, 0, 0, 0, 0 /* pat to antselid */
73 const u16 mimo_2x3_div_antselpat_tbl[] = {
74 16, 0, 1, 16, /* ant0: 0 ant1: 1,2 */
75 16, 16, 16, 16, /* n.a. */
76 16, 2, 16, 16, /* ant0: 2 ant1: 1 */
77 16, 16, 16, 16 /* n.a. */
80 const u8 mimo_2x3_div_antselid_tbl[16] = {
81 0, 1, 2, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0 /* pat to antselid */
85 struct antsel_info *brcms_c_antsel_attach(struct brcms_c_info *wlc)
87 struct antsel_info *asi;
89 asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
91 wiphy_err(wlc->wiphy, "wl%d: brcms_c_antsel_attach: out of "
92 "mem\n", wlc->pub->unit);
98 asi->antsel_type = ANTSEL_NA;
99 asi->antsel_avail = false;
100 asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
102 if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
103 switch (asi->antsel_antswitch) {
104 case ANTSWITCH_TYPE_1:
105 case ANTSWITCH_TYPE_2:
106 case ANTSWITCH_TYPE_3:
107 /* 4321/2 board with 2x3 switch logic */
108 asi->antsel_type = ANTSEL_2x3;
109 /* Antenna selection availability */
110 if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
111 ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
112 asi->antsel_avail = true;
114 (u16) getintvar(asi->pub->vars, "aa2g") == 3 ||
115 (u16) getintvar(asi->pub->vars, "aa5g") == 3) {
116 asi->antsel_avail = false;
118 asi->antsel_avail = false;
119 wiphy_err(wlc->wiphy, "antsel_attach: 2o3 "
120 "board cfg invalid\n");
127 } else if ((asi->pub->sromrev == 4) &&
128 ((u16) getintvar(asi->pub->vars, "aa2g") == 7) &&
129 ((u16) getintvar(asi->pub->vars, "aa5g") == 0)) {
130 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
131 asi->antsel_type = ANTSEL_2x3;
132 asi->antsel_avail = true;
133 } else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
134 asi->antsel_type = ANTSEL_2x4;
135 asi->antsel_avail = true;
138 /* Set the antenna selection type for the low driver */
139 brcms_b_antsel_type_set(wlc->hw, asi->antsel_type);
141 /* Init (auto/manual) antenna selection */
142 brcms_c_antsel_init_cfg(asi, &asi->antcfg_11n, true);
143 brcms_c_antsel_init_cfg(asi, &asi->antcfg_cur, true);
148 void brcms_c_antsel_detach(struct antsel_info *asi)
153 void brcms_c_antsel_init(struct antsel_info *asi)
155 if ((asi->antsel_type == ANTSEL_2x3) ||
156 (asi->antsel_type == ANTSEL_2x4))
157 brcms_c_antsel_cfgupd(asi, &asi->antcfg_11n);
160 /* boardlevel antenna selection: init antenna selection structure */
162 brcms_c_antsel_init_cfg(struct antsel_info *asi, struct brcms_antselcfg *antsel,
165 if (asi->antsel_type == ANTSEL_2x3) {
166 u8 antcfg_def = ANT_SELCFG_DEF_2x3 |
167 ((asi->antsel_avail && auto_sel) ? ANT_SELCFG_AUTO : 0);
168 antsel->ant_config[ANT_SELCFG_TX_DEF] = antcfg_def;
169 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = antcfg_def;
170 antsel->ant_config[ANT_SELCFG_RX_DEF] = antcfg_def;
171 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = antcfg_def;
172 antsel->num_antcfg = ANT_SELCFG_NUM_2x3;
174 } else if (asi->antsel_type == ANTSEL_2x4) {
176 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x4;
177 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x4;
178 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x4;
179 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x4;
180 antsel->num_antcfg = ANT_SELCFG_NUM_2x4;
182 } else { /* no antenna selection available */
184 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x2;
185 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x2;
186 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x2;
187 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x2;
188 antsel->num_antcfg = 0;
193 brcms_c_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
194 u8 antselid, u8 fbantselid, u8 *antcfg,
199 /* if use default, assign it and return */
201 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
207 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
211 ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
212 if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
213 *antcfg = brcms_c_antsel_id2antcfg(asi, antselid);
214 *fbantcfg = brcms_c_antsel_id2antcfg(asi, fbantselid);
217 asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
224 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
225 u8 brcms_c_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
229 if (asi->antsel_type == ANTSEL_2x4) {
230 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
231 antselid = mimo_2x4_div_antselid_tbl[(antsel & 0xf)];
234 } else if (asi->antsel_type == ANTSEL_2x3) {
235 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
236 antselid = mimo_2x3_div_antselid_tbl[(antsel & 0xf)];
243 /* boardlevel antenna selection: convert id to ant_cfg */
244 static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id)
246 u8 antcfg = ANT_SELCFG_DEF_2x2;
248 if (asi->antsel_type == ANTSEL_2x4) {
249 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
250 antcfg = (((id & 0x2) << 3) | ((id & 0x1) + 2));
253 } else if (asi->antsel_type == ANTSEL_2x3) {
254 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
255 antcfg = (((id & 0x02) << 4) | ((id & 0x1) + 1));
263 * boardlevel antenna selection:
264 * convert ant_cfg to mimo_antsel (ucode interface)
266 static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
268 u8 idx = BRCMS_ANTIDX_11N(BRCMS_ANTSEL_11N(ant_cfg));
271 if (asi->antsel_type == ANTSEL_2x4) {
272 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
273 mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
276 } else if (asi->antsel_type == ANTSEL_2x3) {
277 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
278 mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
285 /* boardlevel antenna selection: ucode interface control */
286 static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
287 struct brcms_antselcfg *antsel)
289 struct brcms_c_info *wlc = asi->wlc;
293 /* 1) Update TX antconfig for all frames that are not unicast data
296 ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
297 mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
298 brcms_c_write_shm(wlc, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
300 * Update driver stats for currently selected
301 * default tx/rx antenna config
303 asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
305 /* 2) Update RX antconfig for all frames that are not unicast data
308 ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
309 mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
310 brcms_c_write_shm(wlc, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
312 * Update driver stats for currently selected
313 * default tx/rx antenna config
315 asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;