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>
26 #define ANT_SELCFG_AUTO 0x80 /* bit indicates antenna sel AUTO */
27 #define ANT_SELCFG_MASK 0x33 /* antenna configuration mask */
28 #define ANT_SELCFG_TX_UNICAST 0 /* unicast tx antenna configuration */
29 #define ANT_SELCFG_RX_UNICAST 1 /* unicast rx antenna configuration */
30 #define ANT_SELCFG_TX_DEF 2 /* default tx antenna configuration */
31 #define ANT_SELCFG_RX_DEF 3 /* default rx antenna configuration */
34 #define BRCMS_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
35 #define BRCMS_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
36 #define BRCMS_ANTIDX_11N(ant) (((BRCMS_ANTSEL_11N_0(ant)) << 2) +\
37 (BRCMS_ANTSEL_11N_1(ant)))
38 #define BRCMS_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
39 #define BRCMS_ANTSEL_11N(ant) ((ant) & ANT_SELCFG_MASK)
42 /* defines for no boardlevel antenna diversity */
43 #define ANT_SELCFG_DEF_2x2 0x01 /* default antenna configuration */
45 /* 2x3 antdiv defines and tables for GPIO communication */
46 #define ANT_SELCFG_NUM_2x3 3
47 #define ANT_SELCFG_DEF_2x3 0x01 /* default antenna configuration */
49 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
50 #define ANT_SELCFG_NUM_2x4 4
51 #define ANT_SELCFG_DEF_2x4 0x02 /* default antenna configuration */
53 /* static functions */
54 static int brcms_c_antsel_cfgupd(struct antsel_info
*asi
,
55 struct brcms_antselcfg
*antsel
);
56 static u8
brcms_c_antsel_id2antcfg(struct antsel_info
*asi
, u8 id
);
57 static u16
brcms_c_antsel_antcfg2antsel(struct antsel_info
*asi
, u8 ant_cfg
);
58 static void brcms_c_antsel_init_cfg(struct antsel_info
*asi
,
59 struct brcms_antselcfg
*antsel
,
62 const u16 mimo_2x4_div_antselpat_tbl
[] = {
63 0, 0, 0x9, 0xa, /* ant0: 0 ant1: 2,3 */
64 0, 0, 0x5, 0x6, /* ant0: 1 ant1: 2,3 */
65 0, 0, 0, 0, /* n.a. */
69 const u8 mimo_2x4_div_antselid_tbl
[16] = {
70 0, 0, 0, 0, 0, 2, 3, 0,
71 0, 0, 1, 0, 0, 0, 0, 0 /* pat to antselid */
74 const u16 mimo_2x3_div_antselpat_tbl
[] = {
75 16, 0, 1, 16, /* ant0: 0 ant1: 1,2 */
76 16, 16, 16, 16, /* n.a. */
77 16, 2, 16, 16, /* ant0: 2 ant1: 1 */
78 16, 16, 16, 16 /* n.a. */
81 const u8 mimo_2x3_div_antselid_tbl
[16] = {
82 0, 1, 2, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0 /* pat to antselid */
86 struct antsel_info
*brcms_c_antsel_attach(struct brcms_c_info
*wlc
)
88 struct antsel_info
*asi
;
90 asi
= kzalloc(sizeof(struct antsel_info
), GFP_ATOMIC
);
92 wiphy_err(wlc
->wiphy
, "wl%d: brcms_c_antsel_attach: out of "
93 "mem\n", wlc
->pub
->unit
);
99 asi
->antsel_type
= ANTSEL_NA
;
100 asi
->antsel_avail
= false;
101 asi
->antsel_antswitch
= (u8
) getintvar(asi
->pub
->vars
, "antswitch");
103 if ((asi
->pub
->sromrev
>= 4) && (asi
->antsel_antswitch
!= 0)) {
104 switch (asi
->antsel_antswitch
) {
105 case ANTSWITCH_TYPE_1
:
106 case ANTSWITCH_TYPE_2
:
107 case ANTSWITCH_TYPE_3
:
108 /* 4321/2 board with 2x3 switch logic */
109 asi
->antsel_type
= ANTSEL_2x3
;
110 /* Antenna selection availability */
111 if (((u16
) getintvar(asi
->pub
->vars
, "aa2g") == 7) ||
112 ((u16
) getintvar(asi
->pub
->vars
, "aa5g") == 7)) {
113 asi
->antsel_avail
= true;
115 if (((u16
) getintvar(asi
->pub
->vars
, "aa2g") ==
117 || ((u16
) getintvar(asi
->pub
->vars
, "aa5g")
119 asi
->antsel_avail
= false;
121 asi
->antsel_avail
= false;
122 wiphy_err(wlc
->wiphy
, "antsel_attach: 2o3 "
123 "board cfg invalid\n");
129 } else if ((asi
->pub
->sromrev
== 4) &&
130 ((u16
) getintvar(asi
->pub
->vars
, "aa2g") == 7) &&
131 ((u16
) getintvar(asi
->pub
->vars
, "aa5g") == 0)) {
132 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
133 asi
->antsel_type
= ANTSEL_2x3
;
134 asi
->antsel_avail
= true;
135 } else if (asi
->pub
->boardflags2
& BFL2_2X4_DIV
) {
136 asi
->antsel_type
= ANTSEL_2x4
;
137 asi
->antsel_avail
= true;
140 /* Set the antenna selection type for the low driver */
141 brcms_b_antsel_type_set(wlc
->hw
, asi
->antsel_type
);
143 /* Init (auto/manual) antenna selection */
144 brcms_c_antsel_init_cfg(asi
, &asi
->antcfg_11n
, true);
145 brcms_c_antsel_init_cfg(asi
, &asi
->antcfg_cur
, true);
150 void brcms_c_antsel_detach(struct antsel_info
*asi
)
155 void brcms_c_antsel_init(struct antsel_info
*asi
)
157 if ((asi
->antsel_type
== ANTSEL_2x3
) ||
158 (asi
->antsel_type
== ANTSEL_2x4
))
159 brcms_c_antsel_cfgupd(asi
, &asi
->antcfg_11n
);
162 /* boardlevel antenna selection: init antenna selection structure */
164 brcms_c_antsel_init_cfg(struct antsel_info
*asi
, struct brcms_antselcfg
*antsel
,
167 if (asi
->antsel_type
== ANTSEL_2x3
) {
168 u8 antcfg_def
= ANT_SELCFG_DEF_2x3
|
169 ((asi
->antsel_avail
&& auto_sel
) ? ANT_SELCFG_AUTO
: 0);
170 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = antcfg_def
;
171 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = antcfg_def
;
172 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = antcfg_def
;
173 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = antcfg_def
;
174 antsel
->num_antcfg
= ANT_SELCFG_NUM_2x3
;
176 } else if (asi
->antsel_type
== ANTSEL_2x4
) {
178 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = ANT_SELCFG_DEF_2x4
;
179 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = ANT_SELCFG_DEF_2x4
;
180 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = ANT_SELCFG_DEF_2x4
;
181 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = ANT_SELCFG_DEF_2x4
;
182 antsel
->num_antcfg
= ANT_SELCFG_NUM_2x4
;
184 } else { /* no antenna selection available */
186 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = ANT_SELCFG_DEF_2x2
;
187 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = ANT_SELCFG_DEF_2x2
;
188 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = ANT_SELCFG_DEF_2x2
;
189 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = ANT_SELCFG_DEF_2x2
;
190 antsel
->num_antcfg
= 0;
195 brcms_c_antsel_antcfg_get(struct antsel_info
*asi
, bool usedef
, bool sel
,
196 u8 antselid
, u8 fbantselid
, u8
*antcfg
,
201 /* if use default, assign it and return */
203 *antcfg
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_DEF
];
209 *antcfg
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
213 ant
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
214 if ((ant
& ANT_SELCFG_AUTO
) == ANT_SELCFG_AUTO
) {
215 *antcfg
= brcms_c_antsel_id2antcfg(asi
, antselid
);
216 *fbantcfg
= brcms_c_antsel_id2antcfg(asi
, fbantselid
);
219 asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
226 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
227 u8
brcms_c_antsel_antsel2id(struct antsel_info
*asi
, u16 antsel
)
231 if (asi
->antsel_type
== ANTSEL_2x4
) {
232 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
233 antselid
= mimo_2x4_div_antselid_tbl
[(antsel
& 0xf)];
236 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
237 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
238 antselid
= mimo_2x3_div_antselid_tbl
[(antsel
& 0xf)];
245 /* boardlevel antenna selection: convert id to ant_cfg */
246 static u8
brcms_c_antsel_id2antcfg(struct antsel_info
*asi
, u8 id
)
248 u8 antcfg
= ANT_SELCFG_DEF_2x2
;
250 if (asi
->antsel_type
== ANTSEL_2x4
) {
251 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
252 antcfg
= (((id
& 0x2) << 3) | ((id
& 0x1) + 2));
255 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
256 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
257 antcfg
= (((id
& 0x02) << 4) | ((id
& 0x1) + 1));
264 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
265 static u16
brcms_c_antsel_antcfg2antsel(struct antsel_info
*asi
, u8 ant_cfg
)
267 u8 idx
= BRCMS_ANTIDX_11N(BRCMS_ANTSEL_11N(ant_cfg
));
270 if (asi
->antsel_type
== ANTSEL_2x4
) {
271 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
272 mimo_antsel
= (mimo_2x4_div_antselpat_tbl
[idx
] & 0xf);
275 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
276 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
277 mimo_antsel
= (mimo_2x3_div_antselpat_tbl
[idx
] & 0xf);
284 /* boardlevel antenna selection: ucode interface control */
285 static int brcms_c_antsel_cfgupd(struct antsel_info
*asi
,
286 struct brcms_antselcfg
*antsel
)
288 struct brcms_c_info
*wlc
= asi
->wlc
;
292 /* 1) Update TX antconfig for all frames that are not unicast data
295 ant_cfg
= antsel
->ant_config
[ANT_SELCFG_TX_DEF
];
296 mimo_antsel
= brcms_c_antsel_antcfg2antsel(asi
, ant_cfg
);
297 brcms_c_write_shm(wlc
, M_MIMO_ANTSEL_TXDFLT
, mimo_antsel
);
298 /* Update driver stats for currently selected default tx/rx antenna config */
299 asi
->antcfg_cur
.ant_config
[ANT_SELCFG_TX_DEF
] = ant_cfg
;
301 /* 2) Update RX antconfig for all frames that are not unicast data
304 ant_cfg
= antsel
->ant_config
[ANT_SELCFG_RX_DEF
];
305 mimo_antsel
= brcms_c_antsel_antcfg2antsel(asi
, ant_cfg
);
306 brcms_c_write_shm(wlc
, M_MIMO_ANTSEL_RXDFLT
, mimo_antsel
);
307 /* Update driver stats for currently selected default tx/rx antenna config */
308 asi
->antcfg_cur
.ant_config
[ANT_SELCFG_RX_DEF
] = ant_cfg
;