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.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
38 #include <wlc_mac80211.h>
40 #include <wlc_phy_hal.h>
41 #include <wl_export.h>
42 #include <wlc_antsel.h>
43 #include <wlc_phy_shim.h>
46 #define WLC_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
47 #define WLC_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
48 #define WLC_ANTIDX_11N(ant) (((WLC_ANTSEL_11N_0(ant)) << 2) + (WLC_ANTSEL_11N_1(ant)))
49 #define WLC_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
50 #define WLC_ANTSEL_11N(ant) ((ant) & ANT_SELCFG_MASK)
53 /* defines for no boardlevel antenna diversity */
54 #define ANT_SELCFG_DEF_2x2 0x01 /* default antenna configuration */
56 /* 2x3 antdiv defines and tables for GPIO communication */
57 #define ANT_SELCFG_NUM_2x3 3
58 #define ANT_SELCFG_DEF_2x3 0x01 /* default antenna configuration */
60 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
61 #define ANT_SELCFG_NUM_2x4 4
62 #define ANT_SELCFG_DEF_2x4 0x02 /* default antenna configuration */
64 /* static functions */
65 static int wlc_antsel_cfgupd(struct antsel_info
*asi
, wlc_antselcfg_t
*antsel
);
66 static u8
wlc_antsel_id2antcfg(struct antsel_info
*asi
, u8 id
);
67 static u16
wlc_antsel_antcfg2antsel(struct antsel_info
*asi
, u8 ant_cfg
);
68 static void wlc_antsel_init_cfg(struct antsel_info
*asi
,
69 wlc_antselcfg_t
*antsel
,
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. */
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 */
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. */
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 */
96 struct antsel_info
*wlc_antsel_attach(struct wlc_info
*wlc
,
99 struct wlc_hw_info
*wlc_hw
) {
100 struct antsel_info
*asi
;
102 asi
= kzalloc(sizeof(struct antsel_info
), GFP_ATOMIC
);
104 WL_ERROR("wl%d: wlc_antsel_attach: out of mem\n", pub
->unit
);
110 asi
->antsel_type
= ANTSEL_NA
;
111 asi
->antsel_avail
= false;
112 asi
->antsel_antswitch
= (u8
) getintvar(asi
->pub
->vars
, "antswitch");
114 if ((asi
->pub
->sromrev
>= 4) && (asi
->antsel_antswitch
!= 0)) {
115 switch (asi
->antsel_antswitch
) {
116 case ANTSWITCH_TYPE_1
:
117 case ANTSWITCH_TYPE_2
:
118 case ANTSWITCH_TYPE_3
:
119 /* 4321/2 board with 2x3 switch logic */
120 asi
->antsel_type
= ANTSEL_2x3
;
121 /* Antenna selection availability */
122 if (((u16
) getintvar(asi
->pub
->vars
, "aa2g") == 7) ||
123 ((u16
) getintvar(asi
->pub
->vars
, "aa5g") == 7)) {
124 asi
->antsel_avail
= true;
126 if (((u16
) getintvar(asi
->pub
->vars
, "aa2g") ==
128 || ((u16
) getintvar(asi
->pub
->vars
, "aa5g")
130 asi
->antsel_avail
= false;
132 asi
->antsel_avail
= false;
133 WL_ERROR("wlc_antsel_attach: 2o3 board cfg invalid\n");
140 } else if ((asi
->pub
->sromrev
== 4) &&
141 ((u16
) getintvar(asi
->pub
->vars
, "aa2g") == 7) &&
142 ((u16
) getintvar(asi
->pub
->vars
, "aa5g") == 0)) {
143 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
144 asi
->antsel_type
= ANTSEL_2x3
;
145 asi
->antsel_avail
= true;
146 } else if (asi
->pub
->boardflags2
& BFL2_2X4_DIV
) {
147 asi
->antsel_type
= ANTSEL_2x4
;
148 asi
->antsel_avail
= true;
151 /* Set the antenna selection type for the low driver */
152 wlc_bmac_antsel_type_set(wlc_hw
, asi
->antsel_type
);
154 /* Init (auto/manual) antenna selection */
155 wlc_antsel_init_cfg(asi
, &asi
->antcfg_11n
, true);
156 wlc_antsel_init_cfg(asi
, &asi
->antcfg_cur
, true);
161 void wlc_antsel_detach(struct antsel_info
*asi
)
169 void wlc_antsel_init(struct antsel_info
*asi
)
171 if ((asi
->antsel_type
== ANTSEL_2x3
) ||
172 (asi
->antsel_type
== ANTSEL_2x4
))
173 wlc_antsel_cfgupd(asi
, &asi
->antcfg_11n
);
176 /* boardlevel antenna selection: init antenna selection structure */
178 wlc_antsel_init_cfg(struct antsel_info
*asi
, wlc_antselcfg_t
*antsel
,
181 if (asi
->antsel_type
== ANTSEL_2x3
) {
182 u8 antcfg_def
= ANT_SELCFG_DEF_2x3
|
183 ((asi
->antsel_avail
&& auto_sel
) ? ANT_SELCFG_AUTO
: 0);
184 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = antcfg_def
;
185 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = antcfg_def
;
186 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = antcfg_def
;
187 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = antcfg_def
;
188 antsel
->num_antcfg
= ANT_SELCFG_NUM_2x3
;
190 } else if (asi
->antsel_type
== ANTSEL_2x4
) {
192 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = ANT_SELCFG_DEF_2x4
;
193 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = ANT_SELCFG_DEF_2x4
;
194 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = ANT_SELCFG_DEF_2x4
;
195 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = ANT_SELCFG_DEF_2x4
;
196 antsel
->num_antcfg
= ANT_SELCFG_NUM_2x4
;
198 } else { /* no antenna selection available */
200 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = ANT_SELCFG_DEF_2x2
;
201 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = ANT_SELCFG_DEF_2x2
;
202 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = ANT_SELCFG_DEF_2x2
;
203 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = ANT_SELCFG_DEF_2x2
;
204 antsel
->num_antcfg
= 0;
209 wlc_antsel_antcfg_get(struct antsel_info
*asi
, bool usedef
, bool sel
,
210 u8 antselid
, u8 fbantselid
, u8
*antcfg
,
215 /* if use default, assign it and return */
217 *antcfg
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_DEF
];
223 *antcfg
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
227 ant
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
228 if ((ant
& ANT_SELCFG_AUTO
) == ANT_SELCFG_AUTO
) {
229 *antcfg
= wlc_antsel_id2antcfg(asi
, antselid
);
230 *fbantcfg
= wlc_antsel_id2antcfg(asi
, fbantselid
);
233 asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
240 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
241 u8
wlc_antsel_antsel2id(struct antsel_info
*asi
, u16 antsel
)
245 if (asi
->antsel_type
== ANTSEL_2x4
) {
246 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
247 antselid
= mimo_2x4_div_antselid_tbl
[(antsel
& 0xf)];
250 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
251 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
252 antselid
= mimo_2x3_div_antselid_tbl
[(antsel
& 0xf)];
259 /* boardlevel antenna selection: convert id to ant_cfg */
260 static u8
wlc_antsel_id2antcfg(struct antsel_info
*asi
, u8 id
)
262 u8 antcfg
= ANT_SELCFG_DEF_2x2
;
264 if (asi
->antsel_type
== ANTSEL_2x4
) {
265 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
266 antcfg
= (((id
& 0x2) << 3) | ((id
& 0x1) + 2));
269 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
270 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
271 antcfg
= (((id
& 0x02) << 4) | ((id
& 0x1) + 1));
278 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
279 static u16
wlc_antsel_antcfg2antsel(struct antsel_info
*asi
, u8 ant_cfg
)
281 u8 idx
= WLC_ANTIDX_11N(WLC_ANTSEL_11N(ant_cfg
));
284 if (asi
->antsel_type
== ANTSEL_2x4
) {
285 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
286 mimo_antsel
= (mimo_2x4_div_antselpat_tbl
[idx
] & 0xf);
289 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
290 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
291 mimo_antsel
= (mimo_2x3_div_antselpat_tbl
[idx
] & 0xf);
298 /* boardlevel antenna selection: ucode interface control */
299 static int wlc_antsel_cfgupd(struct antsel_info
*asi
, wlc_antselcfg_t
*antsel
)
301 struct wlc_info
*wlc
= asi
->wlc
;
305 ASSERT(asi
->antsel_type
!= ANTSEL_NA
);
307 /* 1) Update TX antconfig for all frames that are not unicast data
310 ant_cfg
= antsel
->ant_config
[ANT_SELCFG_TX_DEF
];
311 mimo_antsel
= wlc_antsel_antcfg2antsel(asi
, ant_cfg
);
312 wlc_write_shm(wlc
, M_MIMO_ANTSEL_TXDFLT
, mimo_antsel
);
313 /* Update driver stats for currently selected default tx/rx antenna config */
314 asi
->antcfg_cur
.ant_config
[ANT_SELCFG_TX_DEF
] = ant_cfg
;
316 /* 2) Update RX antconfig for all frames that are not unicast data
319 ant_cfg
= antsel
->ant_config
[ANT_SELCFG_RX_DEF
];
320 mimo_antsel
= wlc_antsel_antcfg2antsel(asi
, ant_cfg
);
321 wlc_write_shm(wlc
, M_MIMO_ANTSEL_RXDFLT
, mimo_antsel
);
322 /* Update driver stats for currently selected default tx/rx antenna config */
323 asi
->antcfg_cur
.ant_config
[ANT_SELCFG_RX_DEF
] = ant_cfg
;
328 #endif /* WLANTSEL */