ar9285_diversity.c revision 221694
1/*
2 * Copyright (c) 2008-2010 Atheros Communications Inc.
3 * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/ath/ath_hal/ar9002/ar9285_diversity.c 221694 2011-05-09 15:19:49Z adrian $
27 */
28#include "opt_ah.h"
29
30#include "ah.h"
31#include "ah_desc.h"
32#include "ah_internal.h"
33#include "ah_eeprom_v4k.h"
34
35#include "ar9002/ar9280.h"
36#include "ar9002/ar9285_diversity.h"
37#include "ar9002/ar9285.h"
38#include "ar5416/ar5416reg.h"
39#include "ar5416/ar5416phy.h"
40#include "ar9002/ar9285phy.h"
41#include "ar9002/ar9285_phy.h"
42
43
44/* Linux compability macros */
45/*
46 * XXX these don't handle rounding, underflow, overflow, wrapping!
47 */
48#define	msecs_to_jiffies(a)		( (a) * hz / 1000 )
49#define	time_after(a, b)		( (long) (b) - (long) (a) < 0 )
50
51static HAL_BOOL
52ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta, int mindelta,
53    int main_rssi_avg, int alt_rssi_avg, int pkt_count)
54{
55	return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
56		(alt_rssi_avg > main_rssi_avg + maxdelta)) ||
57		(alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
58}
59
60static void
61ath_lnaconf_alt_good_scan(struct ar9285_ant_comb *antcomb,
62    struct ar9285_antcomb_conf ant_conf, int main_rssi_avg)
63{
64	antcomb->quick_scan_cnt = 0;
65
66	if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
67		antcomb->rssi_lna2 = main_rssi_avg;
68	else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
69		antcomb->rssi_lna1 = main_rssi_avg;
70
71	switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
72	case (0x10): /* LNA2 A-B */
73		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
74		antcomb->first_quick_scan_conf =
75			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
76		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
77		break;
78	case (0x20): /* LNA1 A-B */
79		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
80		antcomb->first_quick_scan_conf =
81			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
82		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
83		break;
84	case (0x21): /* LNA1 LNA2 */
85		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
86		antcomb->first_quick_scan_conf =
87			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
88		antcomb->second_quick_scan_conf =
89			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
90		break;
91	case (0x12): /* LNA2 LNA1 */
92		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
93		antcomb->first_quick_scan_conf =
94			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
95		antcomb->second_quick_scan_conf =
96			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
97		break;
98	case (0x13): /* LNA2 A+B */
99		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
100		antcomb->first_quick_scan_conf =
101			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
102		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
103		break;
104	case (0x23): /* LNA1 A+B */
105		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
106		antcomb->first_quick_scan_conf =
107			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
108		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
109		break;
110	default:
111		break;
112	}
113}
114
115static void
116ath_select_ant_div_from_quick_scan(struct ar9285_ant_comb *antcomb,
117    struct ar9285_antcomb_conf *div_ant_conf, int main_rssi_avg,
118    int alt_rssi_avg, int alt_ratio)
119{
120	/* alt_good */
121	switch (antcomb->quick_scan_cnt) {
122	case 0:
123		/* set alt to main, and alt to first conf */
124		div_ant_conf->main_lna_conf = antcomb->main_conf;
125		div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
126		break;
127	case 1:
128		/* set alt to main, and alt to first conf */
129		div_ant_conf->main_lna_conf = antcomb->main_conf;
130		div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
131		antcomb->rssi_first = main_rssi_avg;
132		antcomb->rssi_second = alt_rssi_avg;
133
134		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
135			/* main is LNA1 */
136			if (ath_is_alt_ant_ratio_better(alt_ratio,
137						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
138						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
139						main_rssi_avg, alt_rssi_avg,
140						antcomb->total_pkt_count))
141				antcomb->first_ratio = AH_TRUE;
142			else
143				antcomb->first_ratio = AH_FALSE;
144		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
145			if (ath_is_alt_ant_ratio_better(alt_ratio,
146						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
147						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
148						main_rssi_avg, alt_rssi_avg,
149						antcomb->total_pkt_count))
150				antcomb->first_ratio = AH_TRUE;
151			else
152				antcomb->first_ratio = AH_FALSE;
153		} else {
154			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
155			    (alt_rssi_avg > main_rssi_avg +
156			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
157			    (alt_rssi_avg > main_rssi_avg)) &&
158			    (antcomb->total_pkt_count > 50))
159				antcomb->first_ratio = AH_TRUE;
160			else
161				antcomb->first_ratio = AH_FALSE;
162		}
163		break;
164	case 2:
165		antcomb->alt_good = AH_FALSE;
166		antcomb->scan_not_start = AH_FALSE;
167		antcomb->scan = AH_FALSE;
168		antcomb->rssi_first = main_rssi_avg;
169		antcomb->rssi_third = alt_rssi_avg;
170
171		if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
172			antcomb->rssi_lna1 = alt_rssi_avg;
173		else if (antcomb->second_quick_scan_conf ==
174			 ATH_ANT_DIV_COMB_LNA2)
175			antcomb->rssi_lna2 = alt_rssi_avg;
176		else if (antcomb->second_quick_scan_conf ==
177			 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
178			if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
179				antcomb->rssi_lna2 = main_rssi_avg;
180			else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
181				antcomb->rssi_lna1 = main_rssi_avg;
182		}
183
184		if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
185		    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
186			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
187		else
188			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
189
190		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
191			if (ath_is_alt_ant_ratio_better(alt_ratio,
192						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
193						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
194						main_rssi_avg, alt_rssi_avg,
195						antcomb->total_pkt_count))
196				antcomb->second_ratio = AH_TRUE;
197			else
198				antcomb->second_ratio = AH_FALSE;
199		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
200			if (ath_is_alt_ant_ratio_better(alt_ratio,
201						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
202						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
203						main_rssi_avg, alt_rssi_avg,
204						antcomb->total_pkt_count))
205				antcomb->second_ratio = AH_TRUE;
206			else
207				antcomb->second_ratio = AH_FALSE;
208		} else {
209			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
210			    (alt_rssi_avg > main_rssi_avg +
211			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
212			    (alt_rssi_avg > main_rssi_avg)) &&
213			    (antcomb->total_pkt_count > 50))
214				antcomb->second_ratio = AH_TRUE;
215			else
216				antcomb->second_ratio = AH_FALSE;
217		}
218
219		/* set alt to the conf with maximun ratio */
220		if (antcomb->first_ratio && antcomb->second_ratio) {
221			if (antcomb->rssi_second > antcomb->rssi_third) {
222				/* first alt*/
223				if ((antcomb->first_quick_scan_conf ==
224				    ATH_ANT_DIV_COMB_LNA1) ||
225				    (antcomb->first_quick_scan_conf ==
226				    ATH_ANT_DIV_COMB_LNA2))
227					/* Set alt LNA1 or LNA2*/
228					if (div_ant_conf->main_lna_conf ==
229					    ATH_ANT_DIV_COMB_LNA2)
230						div_ant_conf->alt_lna_conf =
231							ATH_ANT_DIV_COMB_LNA1;
232					else
233						div_ant_conf->alt_lna_conf =
234							ATH_ANT_DIV_COMB_LNA2;
235				else
236					/* Set alt to A+B or A-B */
237					div_ant_conf->alt_lna_conf =
238						antcomb->first_quick_scan_conf;
239			} else if ((antcomb->second_quick_scan_conf ==
240				   ATH_ANT_DIV_COMB_LNA1) ||
241				   (antcomb->second_quick_scan_conf ==
242				   ATH_ANT_DIV_COMB_LNA2)) {
243				/* Set alt LNA1 or LNA2 */
244				if (div_ant_conf->main_lna_conf ==
245				    ATH_ANT_DIV_COMB_LNA2)
246					div_ant_conf->alt_lna_conf =
247						ATH_ANT_DIV_COMB_LNA1;
248				else
249					div_ant_conf->alt_lna_conf =
250						ATH_ANT_DIV_COMB_LNA2;
251			} else {
252				/* Set alt to A+B or A-B */
253				div_ant_conf->alt_lna_conf =
254					antcomb->second_quick_scan_conf;
255			}
256		} else if (antcomb->first_ratio) {
257			/* first alt */
258			if ((antcomb->first_quick_scan_conf ==
259			    ATH_ANT_DIV_COMB_LNA1) ||
260			    (antcomb->first_quick_scan_conf ==
261			    ATH_ANT_DIV_COMB_LNA2))
262					/* Set alt LNA1 or LNA2 */
263				if (div_ant_conf->main_lna_conf ==
264				    ATH_ANT_DIV_COMB_LNA2)
265					div_ant_conf->alt_lna_conf =
266							ATH_ANT_DIV_COMB_LNA1;
267				else
268					div_ant_conf->alt_lna_conf =
269							ATH_ANT_DIV_COMB_LNA2;
270			else
271				/* Set alt to A+B or A-B */
272				div_ant_conf->alt_lna_conf =
273						antcomb->first_quick_scan_conf;
274		} else if (antcomb->second_ratio) {
275				/* second alt */
276			if ((antcomb->second_quick_scan_conf ==
277			    ATH_ANT_DIV_COMB_LNA1) ||
278			    (antcomb->second_quick_scan_conf ==
279			    ATH_ANT_DIV_COMB_LNA2))
280				/* Set alt LNA1 or LNA2 */
281				if (div_ant_conf->main_lna_conf ==
282				    ATH_ANT_DIV_COMB_LNA2)
283					div_ant_conf->alt_lna_conf =
284						ATH_ANT_DIV_COMB_LNA1;
285				else
286					div_ant_conf->alt_lna_conf =
287						ATH_ANT_DIV_COMB_LNA2;
288			else
289				/* Set alt to A+B or A-B */
290				div_ant_conf->alt_lna_conf =
291						antcomb->second_quick_scan_conf;
292		} else {
293			/* main is largest */
294			if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
295			    (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
296				/* Set alt LNA1 or LNA2 */
297				if (div_ant_conf->main_lna_conf ==
298				    ATH_ANT_DIV_COMB_LNA2)
299					div_ant_conf->alt_lna_conf =
300							ATH_ANT_DIV_COMB_LNA1;
301				else
302					div_ant_conf->alt_lna_conf =
303							ATH_ANT_DIV_COMB_LNA2;
304			else
305				/* Set alt to A+B or A-B */
306				div_ant_conf->alt_lna_conf = antcomb->main_conf;
307		}
308		break;
309	default:
310		break;
311	}
312}
313
314static void
315ath_ant_div_conf_fast_divbias(struct ar9285_antcomb_conf *ant_conf)
316{
317	/* Adjust the fast_div_bias based on main and alt lna conf */
318	switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) {
319	case (0x01): /* A-B LNA2 */
320		ant_conf->fast_div_bias = 0x3b;
321		break;
322	case (0x02): /* A-B LNA1 */
323		ant_conf->fast_div_bias = 0x3d;
324		break;
325	case (0x03): /* A-B A+B */
326		ant_conf->fast_div_bias = 0x1;
327		break;
328	case (0x10): /* LNA2 A-B */
329		ant_conf->fast_div_bias = 0x7;
330		break;
331	case (0x12): /* LNA2 LNA1 */
332		ant_conf->fast_div_bias = 0x2;
333		break;
334	case (0x13): /* LNA2 A+B */
335		ant_conf->fast_div_bias = 0x7;
336		break;
337	case (0x20): /* LNA1 A-B */
338		ant_conf->fast_div_bias = 0x6;
339		break;
340	case (0x21): /* LNA1 LNA2 */
341		ant_conf->fast_div_bias = 0x0;
342		break;
343	case (0x23): /* LNA1 A+B */
344		ant_conf->fast_div_bias = 0x6;
345		break;
346	case (0x30): /* A+B A-B */
347		ant_conf->fast_div_bias = 0x1;
348		break;
349	case (0x31): /* A+B LNA2 */
350		ant_conf->fast_div_bias = 0x3b;
351		break;
352	case (0x32): /* A+B LNA1 */
353		ant_conf->fast_div_bias = 0x3d;
354		break;
355	default:
356		break;
357	}
358}
359
360/* Antenna diversity and combining */
361void
362ar9285_ant_comb_scan(struct ath_hal *ah, struct ath_rx_status *rs,
363    unsigned long ticks, int hz)
364{
365	struct ar9285_antcomb_conf div_ant_conf;
366	struct ar9285_ant_comb *antcomb = &AH9285(ah)->ant_comb;
367	int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
368	int curr_main_set, curr_bias;
369	int main_rssi = rs->rs_rssi_ctl[0];
370	int alt_rssi = rs->rs_rssi_ctl[1];
371	int rx_ant_conf, main_ant_conf;
372	HAL_BOOL short_scan = AH_FALSE;
373
374	if (! ar9285_check_div_comb(ah))
375		return;
376
377	if (AH5212(ah)->ah_diversity == AH_FALSE)
378		return;
379
380	rx_ant_conf = (rs->rs_rssi_ctl[2] >> ATH_ANT_RX_CURRENT_SHIFT) &
381		       ATH_ANT_RX_MASK;
382	main_ant_conf = (rs->rs_rssi_ctl[2] >> ATH_ANT_RX_MAIN_SHIFT) &
383			 ATH_ANT_RX_MASK;
384
385#if 0
386	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: main: %d, alt: %d, rx_ant_conf: %x, main_ant_conf: %x\n",
387	    __func__, main_rssi, alt_rssi, rx_ant_conf, main_ant_conf);
388#endif
389
390	/* Record packet only when alt_rssi is positive */
391	if (alt_rssi > 0) {
392		antcomb->total_pkt_count++;
393		antcomb->main_total_rssi += main_rssi;
394		antcomb->alt_total_rssi  += alt_rssi;
395		if (main_ant_conf == rx_ant_conf)
396			antcomb->main_recv_cnt++;
397		else
398			antcomb->alt_recv_cnt++;
399	}
400
401	/* Short scan check */
402	if (antcomb->scan && antcomb->alt_good) {
403		if (time_after(ticks, antcomb->scan_start_time +
404		    msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
405			short_scan = AH_TRUE;
406		else
407			if (antcomb->total_pkt_count ==
408			    ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
409				alt_ratio = ((antcomb->alt_recv_cnt * 100) /
410					    antcomb->total_pkt_count);
411				if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
412					short_scan = AH_TRUE;
413			}
414	}
415
416	if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
417	    rs->rs_moreaggr) && !short_scan)
418		return;
419
420	if (antcomb->total_pkt_count) {
421		alt_ratio = ((antcomb->alt_recv_cnt * 100) /
422			     antcomb->total_pkt_count);
423		main_rssi_avg = (antcomb->main_total_rssi /
424				 antcomb->total_pkt_count);
425		alt_rssi_avg = (antcomb->alt_total_rssi /
426				 antcomb->total_pkt_count);
427	}
428
429	ar9285_antdiv_comb_conf_get(ah, &div_ant_conf);
430	curr_alt_set = div_ant_conf.alt_lna_conf;
431	curr_main_set = div_ant_conf.main_lna_conf;
432	curr_bias = div_ant_conf.fast_div_bias;
433
434	antcomb->count++;
435
436	if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
437		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
438			ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
439						  main_rssi_avg);
440			antcomb->alt_good = AH_TRUE;
441		} else {
442			antcomb->alt_good = AH_FALSE;
443		}
444
445		antcomb->count = 0;
446		antcomb->scan = AH_TRUE;
447		antcomb->scan_not_start = AH_TRUE;
448	}
449
450	if (!antcomb->scan) {
451		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
452			if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
453				/* Switch main and alt LNA */
454				div_ant_conf.main_lna_conf =
455						ATH_ANT_DIV_COMB_LNA2;
456				div_ant_conf.alt_lna_conf  =
457						ATH_ANT_DIV_COMB_LNA1;
458			} else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
459				div_ant_conf.main_lna_conf =
460						ATH_ANT_DIV_COMB_LNA1;
461				div_ant_conf.alt_lna_conf  =
462						ATH_ANT_DIV_COMB_LNA2;
463			}
464
465			goto div_comb_done;
466		} else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
467			   (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
468			/* Set alt to another LNA */
469			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
470				div_ant_conf.alt_lna_conf =
471						ATH_ANT_DIV_COMB_LNA1;
472			else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
473				div_ant_conf.alt_lna_conf =
474						ATH_ANT_DIV_COMB_LNA2;
475
476			goto div_comb_done;
477		}
478
479		if ((alt_rssi_avg < (main_rssi_avg +
480		    ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA)))
481			goto div_comb_done;
482	}
483
484	if (!antcomb->scan_not_start) {
485		switch (curr_alt_set) {
486		case ATH_ANT_DIV_COMB_LNA2:
487			antcomb->rssi_lna2 = alt_rssi_avg;
488			antcomb->rssi_lna1 = main_rssi_avg;
489			antcomb->scan = AH_TRUE;
490			/* set to A+B */
491			div_ant_conf.main_lna_conf =
492				ATH_ANT_DIV_COMB_LNA1;
493			div_ant_conf.alt_lna_conf  =
494				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
495			break;
496		case ATH_ANT_DIV_COMB_LNA1:
497			antcomb->rssi_lna1 = alt_rssi_avg;
498			antcomb->rssi_lna2 = main_rssi_avg;
499			antcomb->scan = AH_TRUE;
500			/* set to A+B */
501			div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
502			div_ant_conf.alt_lna_conf  =
503				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
504			break;
505		case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
506			antcomb->rssi_add = alt_rssi_avg;
507			antcomb->scan = AH_TRUE;
508			/* set to A-B */
509			div_ant_conf.alt_lna_conf =
510				ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
511			break;
512		case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
513			antcomb->rssi_sub = alt_rssi_avg;
514			antcomb->scan = AH_FALSE;
515			if (antcomb->rssi_lna2 >
516			    (antcomb->rssi_lna1 +
517			    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
518				/* use LNA2 as main LNA */
519				if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
520				    (antcomb->rssi_add > antcomb->rssi_sub)) {
521					/* set to A+B */
522					div_ant_conf.main_lna_conf =
523						ATH_ANT_DIV_COMB_LNA2;
524					div_ant_conf.alt_lna_conf  =
525						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
526				} else if (antcomb->rssi_sub >
527					   antcomb->rssi_lna1) {
528					/* set to A-B */
529					div_ant_conf.main_lna_conf =
530						ATH_ANT_DIV_COMB_LNA2;
531					div_ant_conf.alt_lna_conf =
532						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
533				} else {
534					/* set to LNA1 */
535					div_ant_conf.main_lna_conf =
536						ATH_ANT_DIV_COMB_LNA2;
537					div_ant_conf.alt_lna_conf =
538						ATH_ANT_DIV_COMB_LNA1;
539				}
540			} else {
541				/* use LNA1 as main LNA */
542				if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
543				    (antcomb->rssi_add > antcomb->rssi_sub)) {
544					/* set to A+B */
545					div_ant_conf.main_lna_conf =
546						ATH_ANT_DIV_COMB_LNA1;
547					div_ant_conf.alt_lna_conf  =
548						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
549				} else if (antcomb->rssi_sub >
550					   antcomb->rssi_lna1) {
551					/* set to A-B */
552					div_ant_conf.main_lna_conf =
553						ATH_ANT_DIV_COMB_LNA1;
554					div_ant_conf.alt_lna_conf =
555						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
556				} else {
557					/* set to LNA2 */
558					div_ant_conf.main_lna_conf =
559						ATH_ANT_DIV_COMB_LNA1;
560					div_ant_conf.alt_lna_conf =
561						ATH_ANT_DIV_COMB_LNA2;
562				}
563			}
564			break;
565		default:
566			break;
567		}
568	} else {
569		if (!antcomb->alt_good) {
570			antcomb->scan_not_start = AH_FALSE;
571			/* Set alt to another LNA */
572			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
573				div_ant_conf.main_lna_conf =
574						ATH_ANT_DIV_COMB_LNA2;
575				div_ant_conf.alt_lna_conf =
576						ATH_ANT_DIV_COMB_LNA1;
577			} else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
578				div_ant_conf.main_lna_conf =
579						ATH_ANT_DIV_COMB_LNA1;
580				div_ant_conf.alt_lna_conf =
581						ATH_ANT_DIV_COMB_LNA2;
582			}
583			goto div_comb_done;
584		}
585	}
586
587	ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
588					   main_rssi_avg, alt_rssi_avg,
589					   alt_ratio);
590
591	antcomb->quick_scan_cnt++;
592
593div_comb_done:
594	ath_ant_div_conf_fast_divbias(&div_ant_conf);
595
596	ar9285_antdiv_comb_conf_set(ah, &div_ant_conf);
597
598	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: total_pkt_count=%d\n",
599	   __func__, antcomb->total_pkt_count);
600
601	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: main_total_rssi=%d\n",
602	   __func__, antcomb->main_total_rssi);
603	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: alt_total_rssi=%d\n",
604	   __func__, antcomb->alt_total_rssi);
605
606	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: main_rssi_avg=%d\n",
607	   __func__, main_rssi_avg);
608	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: alt_alt_rssi_avg=%d\n",
609	   __func__, alt_rssi_avg);
610
611	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: main_recv_cnt=%d\n",
612	   __func__, antcomb->main_recv_cnt);
613	HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: alt_recv_cnt=%d\n",
614	   __func__, antcomb->alt_recv_cnt);
615
616	if (curr_alt_set != div_ant_conf.alt_lna_conf)
617		HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: lna_conf: %x -> %x\n",
618		    __func__, curr_alt_set, div_ant_conf.alt_lna_conf);
619	if (curr_main_set != div_ant_conf.main_lna_conf)
620		HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: main_lna_conf: %x -> %x\n",
621		    __func__, curr_main_set, div_ant_conf.main_lna_conf);
622	if (curr_bias != div_ant_conf.fast_div_bias)
623		HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: fast_div_bias: %x -> %x\n",
624		    __func__, curr_bias, div_ant_conf.fast_div_bias);
625
626	antcomb->scan_start_time = ticks;
627	antcomb->total_pkt_count = 0;
628	antcomb->main_total_rssi = 0;
629	antcomb->alt_total_rssi = 0;
630	antcomb->main_recv_cnt = 0;
631	antcomb->alt_recv_cnt = 0;
632}
633
634/*
635 * Set the antenna switch to control RX antenna diversity.
636 *
637 * If a fixed configuration is used, the LNA and div bias
638 * settings are fixed and the antenna diversity scanning routine
639 * is disabled.
640 *
641 * If a variable configuration is used, a default is programmed
642 * in and sampling commences per RXed packet.
643 *
644 * Since this is called from ar9285SetBoardValues() to setup
645 * diversity, it means that after a reset or scan, any current
646 * software diversity combining settings will be lost and won't
647 * re-appear until after the first successful sample run.
648 * Please keep this in mind if you're seeing weird performance
649 * that happens to relate to scan/diversity timing.
650 */
651HAL_BOOL
652ar9285SetAntennaSwitch(struct ath_hal *ah, HAL_ANT_SETTING settings)
653{
654	int regVal;
655	const HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
656	const MODAL_EEP4K_HEADER *pModal = &ee->ee_base.modalHeader;
657	uint8_t ant_div_control1, ant_div_control2;
658
659	if (pModal->version < 3) {
660		HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: not supported\n",
661	    __func__);
662		return AH_FALSE;	/* Can't do diversity */
663	}
664
665	/* Store settings */
666	AH5212(ah)->ah_antControl = settings;
667	AH5212(ah)->ah_diversity = (settings == HAL_ANT_VARIABLE);
668
669	/* XXX don't fiddle if the PHY is in sleep mode or ! chan */
670
671	/* Begin setting the relevant registers */
672
673	ant_div_control1 = pModal->antdiv_ctl1;
674	ant_div_control2 = pModal->antdiv_ctl2;
675
676	regVal = OS_REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
677	regVal &= (~(AR_PHY_9285_ANT_DIV_CTL_ALL));
678
679	/* enable antenna diversity only if diversityControl == HAL_ANT_VARIABLE */
680	if (settings == HAL_ANT_VARIABLE)
681	    regVal |= SM(ant_div_control1, AR_PHY_9285_ANT_DIV_CTL);
682
683	if (settings == HAL_ANT_VARIABLE) {
684	    HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: HAL_ANT_VARIABLE\n",
685	      __func__);
686	    regVal |= SM(ant_div_control2, AR_PHY_9285_ANT_DIV_ALT_LNACONF);
687	    regVal |= SM((ant_div_control2 >> 2), AR_PHY_9285_ANT_DIV_MAIN_LNACONF);
688	    regVal |= SM((ant_div_control1 >> 1), AR_PHY_9285_ANT_DIV_ALT_GAINTB);
689	    regVal |= SM((ant_div_control1 >> 2), AR_PHY_9285_ANT_DIV_MAIN_GAINTB);
690	} else {
691	    if (settings == HAL_ANT_FIXED_A) {
692		/* Diversity disabled, RX = LNA1 */
693		HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: HAL_ANT_FIXED_A\n",
694		    __func__);
695		regVal |= SM(ATH_ANT_DIV_COMB_LNA2, AR_PHY_9285_ANT_DIV_ALT_LNACONF);
696		regVal |= SM(ATH_ANT_DIV_COMB_LNA1, AR_PHY_9285_ANT_DIV_MAIN_LNACONF);
697		regVal |= SM(AR_PHY_9285_ANT_DIV_GAINTB_0, AR_PHY_9285_ANT_DIV_ALT_GAINTB);
698		regVal |= SM(AR_PHY_9285_ANT_DIV_GAINTB_1, AR_PHY_9285_ANT_DIV_MAIN_GAINTB);
699	    }
700	    else if (settings == HAL_ANT_FIXED_B) {
701		/* Diversity disabled, RX = LNA2 */
702		HALDEBUG(ah, HAL_DEBUG_DIVERSITY, "%s: HAL_ANT_FIXED_B\n",
703		    __func__);
704		regVal |= SM(ATH_ANT_DIV_COMB_LNA1, AR_PHY_9285_ANT_DIV_ALT_LNACONF);
705		regVal |= SM(ATH_ANT_DIV_COMB_LNA2, AR_PHY_9285_ANT_DIV_MAIN_LNACONF);
706		regVal |= SM(AR_PHY_9285_ANT_DIV_GAINTB_1, AR_PHY_9285_ANT_DIV_ALT_GAINTB);
707		regVal |= SM(AR_PHY_9285_ANT_DIV_GAINTB_0, AR_PHY_9285_ANT_DIV_MAIN_GAINTB);
708	    }
709	}
710
711	OS_REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regVal);
712	regVal = OS_REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
713	regVal = OS_REG_READ(ah, AR_PHY_CCK_DETECT);
714	regVal &= (~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
715	if (settings == HAL_ANT_VARIABLE)
716	    regVal |= SM((ant_div_control1 >> 3), AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
717
718	OS_REG_WRITE(ah, AR_PHY_CCK_DETECT, regVal);
719	regVal = OS_REG_READ(ah, AR_PHY_CCK_DETECT);
720
721	/*
722	 * If Diversity combining is available and the diversity setting
723	 * is to allow variable diversity, enable it by default.
724	 *
725	 * This will be eventually overridden by the software antenna
726	 * diversity logic.
727	 *
728	 * Note that yes, this following section overrides the above
729	 * settings for the LNA configuration and fast-bias.
730	 */
731	if (ar9285_check_div_comb(ah) && AH5212(ah)->ah_diversity == AH_TRUE) {
732		// If support DivComb, set MAIN to LNA1 and ALT to LNA2 at the first beginning
733		HALDEBUG(ah, HAL_DEBUG_DIVERSITY,
734		    "%s: Enable initial settings for combined diversity\n",
735		    __func__);
736		regVal = OS_REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
737		regVal &= (~(AR_PHY_9285_ANT_DIV_MAIN_LNACONF | AR_PHY_9285_ANT_DIV_ALT_LNACONF));
738		regVal |= (ATH_ANT_DIV_COMB_LNA1 << AR_PHY_9285_ANT_DIV_MAIN_LNACONF_S);
739		regVal |= (ATH_ANT_DIV_COMB_LNA2 << AR_PHY_9285_ANT_DIV_ALT_LNACONF_S);
740		regVal &= (~(AR_PHY_9285_FAST_DIV_BIAS));
741		regVal |= (0 << AR_PHY_9285_FAST_DIV_BIAS_S);
742		OS_REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regVal);
743	}
744
745	return AH_TRUE;
746}
747