1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16 
17 #include <wlc_cfg.h>
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 
23 #include <bcmdefs.h>
24 #include <bcmutils.h>
25 #include <siutils.h>
26 #include <bcmdevs.h>
27 #include <sbhnddma.h>
28 #include <wlioctl.h>
29 
30 #include "d11.h"
31 #include "wlc_rate.h"
32 #include "wlc_key.h"
33 #include "wlc_scb.h"
34 #include "wlc_pub.h"
35 #include "wl_dbg.h"
36 #include "phy/wlc_phy_hal.h"
37 #include "wlc_bmac.h"
38 #include "wlc_channel.h"
39 #include "wlc_main.h"
40 #include "wl_export.h"
41 #include "wlc_phy_shim.h"
42 #include "wlc_antsel.h"
43 
44 /* useful macros */
45 #define WLC_ANTSEL_11N_0(ant)	((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
46 #define WLC_ANTSEL_11N_1(ant)	(((ant) & ANT_SELCFG_MASK) & 0xf)
47 #define WLC_ANTIDX_11N(ant)	(((WLC_ANTSEL_11N_0(ant)) << 2) + (WLC_ANTSEL_11N_1(ant)))
48 #define WLC_ANT_ISAUTO_11N(ant)	(((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
49 #define WLC_ANTSEL_11N(ant)	((ant) & ANT_SELCFG_MASK)
50 
51 /* antenna switch */
52 /* defines for no boardlevel antenna diversity */
53 #define ANT_SELCFG_DEF_2x2	0x01	/* default antenna configuration */
54 
55 /* 2x3 antdiv defines and tables for GPIO communication */
56 #define ANT_SELCFG_NUM_2x3	3
57 #define ANT_SELCFG_DEF_2x3	0x01	/* default antenna configuration */
58 
59 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
60 #define ANT_SELCFG_NUM_2x4	4
61 #define ANT_SELCFG_DEF_2x4	0x02	/* default antenna configuration */
62 
63 /* static functions */
64 static int wlc_antsel_cfgupd(struct antsel_info *asi, wlc_antselcfg_t *antsel);
65 static u8 wlc_antsel_id2antcfg(struct antsel_info *asi, u8 id);
66 static u16 wlc_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg);
67 static void wlc_antsel_init_cfg(struct antsel_info *asi,
68 				wlc_antselcfg_t *antsel,
69 				bool auto_sel);
70 
71 const u16 mimo_2x4_div_antselpat_tbl[] = {
72 	0, 0, 0x9, 0xa,		/* ant0: 0 ant1: 2,3 */
73 	0, 0, 0x5, 0x6,		/* ant0: 1 ant1: 2,3 */
74 	0, 0, 0, 0,		/* n.a.              */
75 	0, 0, 0, 0		/* n.a.              */
76 };
77 
78 const u8 mimo_2x4_div_antselid_tbl[16] = {
79 	0, 0, 0, 0, 0, 2, 3, 0,
80 	0, 0, 1, 0, 0, 0, 0, 0	/* pat to antselid */
81 };
82 
83 const u16 mimo_2x3_div_antselpat_tbl[] = {
84 	16, 0, 1, 16,		/* ant0: 0 ant1: 1,2 */
85 	16, 16, 16, 16,		/* n.a.              */
86 	16, 2, 16, 16,		/* ant0: 2 ant1: 1   */
87 	16, 16, 16, 16		/* n.a.              */
88 };
89 
90 const u8 mimo_2x3_div_antselid_tbl[16] = {
91 	0, 1, 2, 0, 0, 0, 0, 0,
92 	0, 0, 0, 0, 0, 0, 0, 0	/* pat to antselid */
93 };
94 
wlc_antsel_attach(struct wlc_info * wlc)95 struct antsel_info *wlc_antsel_attach(struct wlc_info *wlc)
96 {
97 	struct antsel_info *asi;
98 
99 	asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
100 	if (!asi) {
101 		WL_ERROR("wl%d: wlc_antsel_attach: out of mem\n",
102 			 wlc->pub->unit);
103 		return NULL;
104 	}
105 
106 	asi->wlc = wlc;
107 	asi->pub = wlc->pub;
108 	asi->antsel_type = ANTSEL_NA;
109 	asi->antsel_avail = false;
110 	asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
111 
112 	if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
113 		switch (asi->antsel_antswitch) {
114 		case ANTSWITCH_TYPE_1:
115 		case ANTSWITCH_TYPE_2:
116 		case ANTSWITCH_TYPE_3:
117 			/* 4321/2 board with 2x3 switch logic */
118 			asi->antsel_type = ANTSEL_2x3;
119 			/* Antenna selection availability */
120 			if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
121 			    ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
122 				asi->antsel_avail = true;
123 			} else
124 			    if (((u16) getintvar(asi->pub->vars, "aa2g") ==
125 				 3)
126 				|| ((u16) getintvar(asi->pub->vars, "aa5g")
127 				    == 3)) {
128 				asi->antsel_avail = false;
129 			} else {
130 				asi->antsel_avail = false;
131 				WL_ERROR("wlc_antsel_attach: 2o3 board cfg invalid\n");
132 				ASSERT(0);
133 			}
134 			break;
135 		default:
136 			break;
137 		}
138 	} else if ((asi->pub->sromrev == 4) &&
139 		   ((u16) getintvar(asi->pub->vars, "aa2g") == 7) &&
140 		   ((u16) getintvar(asi->pub->vars, "aa5g") == 0)) {
141 		/* hack to match old 4321CB2 cards with 2of3 antenna switch */
142 		asi->antsel_type = ANTSEL_2x3;
143 		asi->antsel_avail = true;
144 	} else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
145 		asi->antsel_type = ANTSEL_2x4;
146 		asi->antsel_avail = true;
147 	}
148 
149 	/* Set the antenna selection type for the low driver */
150 	wlc_bmac_antsel_type_set(wlc->hw, asi->antsel_type);
151 
152 	/* Init (auto/manual) antenna selection */
153 	wlc_antsel_init_cfg(asi, &asi->antcfg_11n, true);
154 	wlc_antsel_init_cfg(asi, &asi->antcfg_cur, true);
155 
156 	return asi;
157 }
158 
wlc_antsel_detach(struct antsel_info * asi)159 void wlc_antsel_detach(struct antsel_info *asi)
160 {
161 	kfree(asi);
162 }
163 
wlc_antsel_init(struct antsel_info * asi)164 void wlc_antsel_init(struct antsel_info *asi)
165 {
166 	if ((asi->antsel_type == ANTSEL_2x3) ||
167 	    (asi->antsel_type == ANTSEL_2x4))
168 		wlc_antsel_cfgupd(asi, &asi->antcfg_11n);
169 }
170 
171 /* boardlevel antenna selection: init antenna selection structure */
172 static void
wlc_antsel_init_cfg(struct antsel_info * asi,wlc_antselcfg_t * antsel,bool auto_sel)173 wlc_antsel_init_cfg(struct antsel_info *asi, wlc_antselcfg_t *antsel,
174 		    bool auto_sel)
175 {
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;
184 
185 	} else if (asi->antsel_type == ANTSEL_2x4) {
186 
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;
192 
193 	} else {		/* no antenna selection available */
194 
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;
200 	}
201 }
202 
203 void BCMFASTPATH
wlc_antsel_antcfg_get(struct antsel_info * asi,bool usedef,bool sel,u8 antselid,u8 fbantselid,u8 * antcfg,u8 * fbantcfg)204 wlc_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
205 		      u8 antselid, u8 fbantselid, u8 *antcfg,
206 		      u8 *fbantcfg)
207 {
208 	u8 ant;
209 
210 	/* if use default, assign it and return */
211 	if (usedef) {
212 		*antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
213 		*fbantcfg = *antcfg;
214 		return;
215 	}
216 
217 	if (!sel) {
218 		*antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
219 		*fbantcfg = *antcfg;
220 
221 	} else {
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);
226 		} else {
227 			*antcfg =
228 			    asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
229 			*fbantcfg = *antcfg;
230 		}
231 	}
232 	return;
233 }
234 
235 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
wlc_antsel_antsel2id(struct antsel_info * asi,u16 antsel)236 u8 wlc_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
237 {
238 	u8 antselid = 0;
239 
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)];
243 		return antselid;
244 
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)];
248 		return antselid;
249 	}
250 
251 	return antselid;
252 }
253 
254 /* boardlevel antenna selection: convert id to ant_cfg */
wlc_antsel_id2antcfg(struct antsel_info * asi,u8 id)255 static u8 wlc_antsel_id2antcfg(struct antsel_info *asi, u8 id)
256 {
257 	u8 antcfg = ANT_SELCFG_DEF_2x2;
258 
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));
262 		return antcfg;
263 
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));
267 		return antcfg;
268 	}
269 
270 	return antcfg;
271 }
272 
273 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
wlc_antsel_antcfg2antsel(struct antsel_info * asi,u8 ant_cfg)274 static u16 wlc_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
275 {
276 	u8 idx = WLC_ANTIDX_11N(WLC_ANTSEL_11N(ant_cfg));
277 	u16 mimo_antsel = 0;
278 
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);
282 		return mimo_antsel;
283 
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);
287 		return mimo_antsel;
288 	}
289 
290 	return mimo_antsel;
291 }
292 
293 /* boardlevel antenna selection: ucode interface control */
wlc_antsel_cfgupd(struct antsel_info * asi,wlc_antselcfg_t * antsel)294 static int wlc_antsel_cfgupd(struct antsel_info *asi, wlc_antselcfg_t *antsel)
295 {
296 	struct wlc_info *wlc = asi->wlc;
297 	u8 ant_cfg;
298 	u16 mimo_antsel;
299 
300 	ASSERT(asi->antsel_type != ANTSEL_NA);
301 
302 	/* 1) Update TX antconfig for all frames that are not unicast data
303 	 *    (aka default TX)
304 	 */
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;
310 
311 	/* 2) Update RX antconfig for all frames that are not unicast data
312 	 *    (aka default RX)
313 	 */
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;
319 
320 	return 0;
321 }
322