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 const u16 mimo_2x4_div_antselpat_tbl
[] = {
53 0, 0, 0x9, 0xa, /* ant0: 0 ant1: 2,3 */
54 0, 0, 0x5, 0x6, /* ant0: 1 ant1: 2,3 */
55 0, 0, 0, 0, /* n.a. */
59 static const u8 mimo_2x4_div_antselid_tbl
[16] = {
60 0, 0, 0, 0, 0, 2, 3, 0,
61 0, 0, 1, 0, 0, 0, 0, 0 /* pat to antselid */
64 static const u16 mimo_2x3_div_antselpat_tbl
[] = {
65 16, 0, 1, 16, /* ant0: 0 ant1: 1,2 */
66 16, 16, 16, 16, /* n.a. */
67 16, 2, 16, 16, /* ant0: 2 ant1: 1 */
68 16, 16, 16, 16 /* n.a. */
71 static const u8 mimo_2x3_div_antselid_tbl
[16] = {
72 0, 1, 2, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0 /* pat to antselid */
76 /* boardlevel antenna selection: init antenna selection structure */
78 brcms_c_antsel_init_cfg(struct antsel_info
*asi
, struct brcms_antselcfg
*antsel
,
81 if (asi
->antsel_type
== ANTSEL_2x3
) {
82 u8 antcfg_def
= ANT_SELCFG_DEF_2x3
|
83 ((asi
->antsel_avail
&& auto_sel
) ? ANT_SELCFG_AUTO
: 0);
84 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = antcfg_def
;
85 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = antcfg_def
;
86 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = antcfg_def
;
87 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = antcfg_def
;
88 antsel
->num_antcfg
= ANT_SELCFG_NUM_2x3
;
90 } else if (asi
->antsel_type
== ANTSEL_2x4
) {
92 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = ANT_SELCFG_DEF_2x4
;
93 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = ANT_SELCFG_DEF_2x4
;
94 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = ANT_SELCFG_DEF_2x4
;
95 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = ANT_SELCFG_DEF_2x4
;
96 antsel
->num_antcfg
= ANT_SELCFG_NUM_2x4
;
98 } else { /* no antenna selection available */
100 antsel
->ant_config
[ANT_SELCFG_TX_DEF
] = ANT_SELCFG_DEF_2x2
;
101 antsel
->ant_config
[ANT_SELCFG_TX_UNICAST
] = ANT_SELCFG_DEF_2x2
;
102 antsel
->ant_config
[ANT_SELCFG_RX_DEF
] = ANT_SELCFG_DEF_2x2
;
103 antsel
->ant_config
[ANT_SELCFG_RX_UNICAST
] = ANT_SELCFG_DEF_2x2
;
104 antsel
->num_antcfg
= 0;
108 struct antsel_info
*brcms_c_antsel_attach(struct brcms_c_info
*wlc
)
110 struct antsel_info
*asi
;
111 struct si_pub
*sih
= wlc
->hw
->sih
;
113 asi
= kzalloc(sizeof(struct antsel_info
), GFP_ATOMIC
);
119 asi
->antsel_type
= ANTSEL_NA
;
120 asi
->antsel_avail
= false;
121 asi
->antsel_antswitch
= (u8
) getintvar(sih
, BRCMS_SROM_ANTSWITCH
);
123 if ((asi
->pub
->sromrev
>= 4) && (asi
->antsel_antswitch
!= 0)) {
124 switch (asi
->antsel_antswitch
) {
125 case ANTSWITCH_TYPE_1
:
126 case ANTSWITCH_TYPE_2
:
127 case ANTSWITCH_TYPE_3
:
128 /* 4321/2 board with 2x3 switch logic */
129 asi
->antsel_type
= ANTSEL_2x3
;
130 /* Antenna selection availability */
131 if (((u16
) getintvar(sih
, BRCMS_SROM_AA2G
) == 7) ||
132 ((u16
) getintvar(sih
, BRCMS_SROM_AA5G
) == 7)) {
133 asi
->antsel_avail
= true;
135 (u16
) getintvar(sih
, BRCMS_SROM_AA2G
) == 3 ||
136 (u16
) getintvar(sih
, BRCMS_SROM_AA5G
) == 3) {
137 asi
->antsel_avail
= false;
139 asi
->antsel_avail
= false;
140 wiphy_err(wlc
->wiphy
, "antsel_attach: 2o3 "
141 "board cfg invalid\n");
148 } else if ((asi
->pub
->sromrev
== 4) &&
149 ((u16
) getintvar(sih
, BRCMS_SROM_AA2G
) == 7) &&
150 ((u16
) getintvar(sih
, BRCMS_SROM_AA5G
) == 0)) {
151 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
152 asi
->antsel_type
= ANTSEL_2x3
;
153 asi
->antsel_avail
= true;
154 } else if (asi
->pub
->boardflags2
& BFL2_2X4_DIV
) {
155 asi
->antsel_type
= ANTSEL_2x4
;
156 asi
->antsel_avail
= true;
159 /* Set the antenna selection type for the low driver */
160 brcms_b_antsel_type_set(wlc
->hw
, asi
->antsel_type
);
162 /* Init (auto/manual) antenna selection */
163 brcms_c_antsel_init_cfg(asi
, &asi
->antcfg_11n
, true);
164 brcms_c_antsel_init_cfg(asi
, &asi
->antcfg_cur
, true);
169 void brcms_c_antsel_detach(struct antsel_info
*asi
)
175 * boardlevel antenna selection:
176 * convert ant_cfg to mimo_antsel (ucode interface)
178 static u16
brcms_c_antsel_antcfg2antsel(struct antsel_info
*asi
, u8 ant_cfg
)
180 u8 idx
= BRCMS_ANTIDX_11N(BRCMS_ANTSEL_11N(ant_cfg
));
183 if (asi
->antsel_type
== ANTSEL_2x4
) {
184 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
185 mimo_antsel
= (mimo_2x4_div_antselpat_tbl
[idx
] & 0xf);
188 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
189 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
190 mimo_antsel
= (mimo_2x3_div_antselpat_tbl
[idx
] & 0xf);
197 /* boardlevel antenna selection: ucode interface control */
198 static int brcms_c_antsel_cfgupd(struct antsel_info
*asi
,
199 struct brcms_antselcfg
*antsel
)
201 struct brcms_c_info
*wlc
= asi
->wlc
;
205 /* 1) Update TX antconfig for all frames that are not unicast data
208 ant_cfg
= antsel
->ant_config
[ANT_SELCFG_TX_DEF
];
209 mimo_antsel
= brcms_c_antsel_antcfg2antsel(asi
, ant_cfg
);
210 brcms_b_write_shm(wlc
->hw
, M_MIMO_ANTSEL_TXDFLT
, mimo_antsel
);
212 * Update driver stats for currently selected
213 * default tx/rx antenna config
215 asi
->antcfg_cur
.ant_config
[ANT_SELCFG_TX_DEF
] = ant_cfg
;
217 /* 2) Update RX antconfig for all frames that are not unicast data
220 ant_cfg
= antsel
->ant_config
[ANT_SELCFG_RX_DEF
];
221 mimo_antsel
= brcms_c_antsel_antcfg2antsel(asi
, ant_cfg
);
222 brcms_b_write_shm(wlc
->hw
, M_MIMO_ANTSEL_RXDFLT
, mimo_antsel
);
224 * Update driver stats for currently selected
225 * default tx/rx antenna config
227 asi
->antcfg_cur
.ant_config
[ANT_SELCFG_RX_DEF
] = ant_cfg
;
232 void brcms_c_antsel_init(struct antsel_info
*asi
)
234 if ((asi
->antsel_type
== ANTSEL_2x3
) ||
235 (asi
->antsel_type
== ANTSEL_2x4
))
236 brcms_c_antsel_cfgupd(asi
, &asi
->antcfg_11n
);
239 /* boardlevel antenna selection: convert id to ant_cfg */
240 static u8
brcms_c_antsel_id2antcfg(struct antsel_info
*asi
, u8 id
)
242 u8 antcfg
= ANT_SELCFG_DEF_2x2
;
244 if (asi
->antsel_type
== ANTSEL_2x4
) {
245 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
246 antcfg
= (((id
& 0x2) << 3) | ((id
& 0x1) + 2));
249 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
250 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
251 antcfg
= (((id
& 0x02) << 4) | ((id
& 0x1) + 1));
259 brcms_c_antsel_antcfg_get(struct antsel_info
*asi
, bool usedef
, bool sel
,
260 u8 antselid
, u8 fbantselid
, u8
*antcfg
,
265 /* if use default, assign it and return */
267 *antcfg
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_DEF
];
273 *antcfg
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
277 ant
= asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
278 if ((ant
& ANT_SELCFG_AUTO
) == ANT_SELCFG_AUTO
) {
279 *antcfg
= brcms_c_antsel_id2antcfg(asi
, antselid
);
280 *fbantcfg
= brcms_c_antsel_id2antcfg(asi
, fbantselid
);
283 asi
->antcfg_11n
.ant_config
[ANT_SELCFG_TX_UNICAST
];
290 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
291 u8
brcms_c_antsel_antsel2id(struct antsel_info
*asi
, u16 antsel
)
295 if (asi
->antsel_type
== ANTSEL_2x4
) {
296 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
297 antselid
= mimo_2x4_div_antselid_tbl
[(antsel
& 0xf)];
300 } else if (asi
->antsel_type
== ANTSEL_2x3
) {
301 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
302 antselid
= mimo_2x3_div_antselid_tbl
[(antsel
& 0xf)];