ar5416_misc.c revision 221878
1/*
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * $FreeBSD: head/sys/dev/ath/ath_hal/ar5416/ar5416_misc.c 221878 2011-05-14 05:43:33Z adrian $
18 */
19#include "opt_ah.h"
20
21#include "ah.h"
22#include "ah_internal.h"
23#include "ah_devid.h"
24#ifdef AH_DEBUG
25#include "ah_desc.h"                    /* NB: for HAL_PHYERR* */
26#endif
27
28#include "ar5416/ar5416.h"
29#include "ar5416/ar5416reg.h"
30#include "ar5416/ar5416phy.h"
31
32/*
33 * Return the wireless modes (a,b,g,n,t) supported by hardware.
34 *
35 * This value is what is actually supported by the hardware
36 * and is unaffected by regulatory/country code settings.
37 *
38 */
39u_int
40ar5416GetWirelessModes(struct ath_hal *ah)
41{
42	u_int mode;
43	struct ath_hal_private *ahpriv = AH_PRIVATE(ah);
44	HAL_CAPABILITIES *pCap = &ahpriv->ah_caps;
45
46	mode = ar5212GetWirelessModes(ah);
47
48	/* Only enable HT modes if the NIC supports HT */
49	if (pCap->halHTSupport == AH_TRUE && (mode & HAL_MODE_11A))
50		mode |= HAL_MODE_11NA_HT20
51		     |  HAL_MODE_11NA_HT40PLUS
52		     |  HAL_MODE_11NA_HT40MINUS
53		     ;
54	if (pCap->halHTSupport == AH_TRUE && (mode & HAL_MODE_11G))
55		mode |= HAL_MODE_11NG_HT20
56		     |  HAL_MODE_11NG_HT40PLUS
57		     |  HAL_MODE_11NG_HT40MINUS
58		     ;
59	return mode;
60}
61
62/*
63 * Change the LED blinking pattern to correspond to the connectivity
64 */
65void
66ar5416SetLedState(struct ath_hal *ah, HAL_LED_STATE state)
67{
68	static const uint32_t ledbits[8] = {
69		AR_MAC_LED_ASSOC_NONE,		/* HAL_LED_INIT */
70		AR_MAC_LED_ASSOC_PEND,		/* HAL_LED_SCAN */
71		AR_MAC_LED_ASSOC_PEND,		/* HAL_LED_AUTH */
72		AR_MAC_LED_ASSOC_ACTIVE,	/* HAL_LED_ASSOC*/
73		AR_MAC_LED_ASSOC_ACTIVE,	/* HAL_LED_RUN */
74		AR_MAC_LED_ASSOC_NONE,
75		AR_MAC_LED_ASSOC_NONE,
76		AR_MAC_LED_ASSOC_NONE,
77	};
78	uint32_t bits;
79
80	if (AR_SREV_HOWL(ah))
81		return;
82
83	bits = OS_REG_READ(ah, AR_MAC_LED);
84	bits = (bits &~ AR_MAC_LED_MODE)
85	     | SM(AR_MAC_LED_MODE_POWON, AR_MAC_LED_MODE)
86#if 1
87	     | SM(AR_MAC_LED_MODE_NETON, AR_MAC_LED_MODE)
88#endif
89	     ;
90	bits = (bits &~ AR_MAC_LED_ASSOC)
91	     | SM(ledbits[state & 0x7], AR_MAC_LED_ASSOC);
92	OS_REG_WRITE(ah, AR_MAC_LED, bits);
93}
94
95/*
96 * Reset the current hardware tsf for stamlme.
97 */
98void
99ar5416ResetTsf(struct ath_hal *ah)
100{
101	uint32_t v;
102	int i;
103
104	for (i = 0; i < 10; i++) {
105		v = OS_REG_READ(ah, AR_SLP32_MODE);
106		if ((v & AR_SLP32_TSF_WRITE_STATUS) == 0)
107			break;
108		OS_DELAY(10);
109	}
110	OS_REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
111}
112
113HAL_BOOL
114ar5416SetAntennaSwitch(struct ath_hal *ah, HAL_ANT_SETTING settings)
115{
116	return AH_TRUE;
117}
118
119/* Setup decompression for given key index */
120HAL_BOOL
121ar5416SetDecompMask(struct ath_hal *ah, uint16_t keyidx, int en)
122{
123	return HAL_OK;
124}
125
126/* Setup coverage class */
127void
128ar5416SetCoverageClass(struct ath_hal *ah, uint8_t coverageclass, int now)
129{
130	AH_PRIVATE(ah)->ah_coverageClass = coverageclass;
131}
132
133/*
134 * Return approximation of extension channel busy over an time interval
135 * 0% (clear) -> 100% (busy)
136 *
137 */
138uint32_t
139ar5416Get11nExtBusy(struct ath_hal *ah)
140{
141    struct ath_hal_5416 *ahp = AH5416(ah);
142    uint32_t busy; /* percentage */
143    uint32_t cycleCount, ctlBusy, extBusy;
144
145    ctlBusy = OS_REG_READ(ah, AR_RCCNT);
146    extBusy = OS_REG_READ(ah, AR_EXTRCCNT);
147    cycleCount = OS_REG_READ(ah, AR_CCCNT);
148
149    if (ahp->ah_cycleCount == 0 || ahp->ah_cycleCount > cycleCount) {
150        /*
151         * Cycle counter wrap (or initial call); it's not possible
152         * to accurately calculate a value because the registers
153         * right shift rather than wrap--so punt and return 0.
154         */
155        busy = 0;
156        HALDEBUG(ah, HAL_DEBUG_ANY, "%s: cycle counter wrap. ExtBusy = 0\n",
157	    __func__);
158
159    } else {
160        uint32_t cycleDelta = cycleCount - ahp->ah_cycleCount;
161        uint32_t ctlBusyDelta = ctlBusy - ahp->ah_ctlBusy;
162        uint32_t extBusyDelta = extBusy - ahp->ah_extBusy;
163        uint32_t ctlClearDelta = 0;
164
165        /* Compute control channel rxclear.
166         * The cycle delta may be less than the control channel delta.
167         * This could be solved by freezing the timers (or an atomic read,
168         * if one was available). Checking for the condition should be
169         * sufficient.
170         */
171        if (cycleDelta > ctlBusyDelta) {
172            ctlClearDelta = cycleDelta - ctlBusyDelta;
173        }
174
175        /* Compute ratio of extension channel busy to control channel clear
176         * as an approximation to extension channel cleanliness.
177         *
178         * According to the hardware folks, ext rxclear is undefined
179         * if the ctrl rxclear is de-asserted (i.e. busy)
180         */
181        if (ctlClearDelta) {
182            busy = (extBusyDelta * 100) / ctlClearDelta;
183        } else {
184            busy = 100;
185        }
186        if (busy > 100) {
187            busy = 100;
188        }
189#if 0
190        HALDEBUG(ah, HAL_DEBUG_ANY, "%s: cycleDelta 0x%x, ctlBusyDelta 0x%x, "
191             "extBusyDelta 0x%x, ctlClearDelta 0x%x, "
192             "busy %d\n",
193              __func__, cycleDelta, ctlBusyDelta, extBusyDelta, ctlClearDelta, busy);
194#endif
195    }
196
197    ahp->ah_cycleCount = cycleCount;
198    ahp->ah_ctlBusy = ctlBusy;
199    ahp->ah_extBusy = extBusy;
200
201    return busy;
202}
203
204/*
205 * Configure 20/40 operation
206 *
207 * 20/40 = joint rx clear (control and extension)
208 * 20    = rx clear (control)
209 *
210 * - NOTE: must stop MAC (tx) and requeue 40 MHz packets as 20 MHz when changing
211 *         from 20/40 => 20 only
212 */
213void
214ar5416Set11nMac2040(struct ath_hal *ah, HAL_HT_MACMODE mode)
215{
216    uint32_t macmode;
217
218    /* Configure MAC for 20/40 operation */
219    if (mode == HAL_HT_MACMODE_2040) {
220        macmode = AR_2040_JOINED_RX_CLEAR;
221    } else {
222        macmode = 0;
223    }
224    OS_REG_WRITE(ah, AR_2040_MODE, macmode);
225}
226
227/*
228 * Get Rx clear (control/extension channel)
229 *
230 * Returns active low (busy) for ctrl/ext channel
231 * Owl 2.0
232 */
233HAL_HT_RXCLEAR
234ar5416Get11nRxClear(struct ath_hal *ah)
235{
236    HAL_HT_RXCLEAR rxclear = 0;
237    uint32_t val;
238
239    val = OS_REG_READ(ah, AR_DIAG_SW);
240
241    /* control channel */
242    if (val & AR_DIAG_RXCLEAR_CTL_LOW) {
243        rxclear |= HAL_RX_CLEAR_CTL_LOW;
244    }
245    /* extension channel */
246    if (val & AR_DIAG_RXCLEAR_CTL_LOW) {
247        rxclear |= HAL_RX_CLEAR_EXT_LOW;
248    }
249    return rxclear;
250}
251
252/*
253 * Set Rx clear (control/extension channel)
254 *
255 * Useful for forcing the channel to appear busy for
256 * debugging/diagnostics
257 * Owl 2.0
258 */
259void
260ar5416Set11nRxClear(struct ath_hal *ah, HAL_HT_RXCLEAR rxclear)
261{
262    /* control channel */
263    if (rxclear & HAL_RX_CLEAR_CTL_LOW) {
264        OS_REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RXCLEAR_CTL_LOW);
265    } else {
266        OS_REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_RXCLEAR_CTL_LOW);
267    }
268    /* extension channel */
269    if (rxclear & HAL_RX_CLEAR_EXT_LOW) {
270        OS_REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RXCLEAR_EXT_LOW);
271    } else {
272        OS_REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_RXCLEAR_EXT_LOW);
273    }
274}
275
276HAL_STATUS
277ar5416GetCapability(struct ath_hal *ah, HAL_CAPABILITY_TYPE type,
278        uint32_t capability, uint32_t *result)
279{
280	switch (type) {
281	case HAL_CAP_BB_HANG:
282		switch (capability) {
283		case HAL_BB_HANG_RIFS:
284			return (AR_SREV_HOWL(ah) || AR_SREV_SOWL(ah)) ? HAL_OK : HAL_ENOTSUPP;
285		case HAL_BB_HANG_DFS:
286			return (AR_SREV_HOWL(ah) || AR_SREV_SOWL(ah)) ? HAL_OK : HAL_ENOTSUPP;
287		case HAL_BB_HANG_RX_CLEAR:
288			return AR_SREV_MERLIN(ah) ? HAL_OK : HAL_ENOTSUPP;
289		}
290		break;
291	case HAL_CAP_MAC_HANG:
292		return ((ah->ah_macVersion == AR_XSREV_VERSION_OWL_PCI) ||
293		    (ah->ah_macVersion == AR_XSREV_VERSION_OWL_PCIE) ||
294		    AR_SREV_HOWL(ah) || AR_SREV_SOWL(ah)) ?
295			HAL_OK : HAL_ENOTSUPP;
296	case HAL_CAP_DIVERSITY:		/* disable classic fast diversity */
297		return HAL_ENXIO;
298	default:
299		break;
300	}
301	return ar5212GetCapability(ah, type, capability, result);
302}
303
304static int ar5416DetectMacHang(struct ath_hal *ah);
305static int ar5416DetectBBHang(struct ath_hal *ah);
306
307HAL_BOOL
308ar5416GetDiagState(struct ath_hal *ah, int request,
309	const void *args, uint32_t argsize,
310	void **result, uint32_t *resultsize)
311{
312	struct ath_hal_5416 *ahp = AH5416(ah);
313	int hangs;
314
315	if (ath_hal_getdiagstate(ah, request, args, argsize, result, resultsize))
316		return AH_TRUE;
317	switch (request) {
318	case HAL_DIAG_EEPROM:
319		return ath_hal_eepromDiag(ah, request,
320		    args, argsize, result, resultsize);
321	case HAL_DIAG_CHECK_HANGS:
322		if (argsize != sizeof(int))
323			return AH_FALSE;
324		hangs = *(const int *) args;
325		ahp->ah_hangs = 0;
326		if (hangs & HAL_BB_HANGS)
327			ahp->ah_hangs |= ar5416DetectBBHang(ah);
328		/* NB: if BB is hung MAC will be hung too so skip check */
329		if (ahp->ah_hangs == 0 && (hangs & HAL_MAC_HANGS))
330			ahp->ah_hangs |= ar5416DetectMacHang(ah);
331		*result = &ahp->ah_hangs;
332		*resultsize = sizeof(ahp->ah_hangs);
333		return AH_TRUE;
334	}
335	return ar5212GetDiagState(ah, request,
336	    args, argsize, result, resultsize);
337}
338
339typedef struct {
340	uint32_t dma_dbg_3;
341	uint32_t dma_dbg_4;
342	uint32_t dma_dbg_5;
343	uint32_t dma_dbg_6;
344} mac_dbg_regs_t;
345
346typedef enum {
347	dcu_chain_state		= 0x1,
348	dcu_complete_state	= 0x2,
349	qcu_state		= 0x4,
350	qcu_fsp_ok		= 0x8,
351	qcu_fsp_state		= 0x10,
352	qcu_stitch_state	= 0x20,
353	qcu_fetch_state		= 0x40,
354	qcu_complete_state	= 0x80
355} hal_mac_hangs_t;
356
357typedef struct {
358	int states;
359	uint8_t dcu_chain_state;
360	uint8_t dcu_complete_state;
361	uint8_t qcu_state;
362	uint8_t qcu_fsp_ok;
363	uint8_t qcu_fsp_state;
364	uint8_t qcu_stitch_state;
365	uint8_t qcu_fetch_state;
366	uint8_t qcu_complete_state;
367} hal_mac_hang_check_t;
368
369HAL_BOOL
370ar5416SetRifsDelay(struct ath_hal *ah, const struct ieee80211_channel *chan,
371    HAL_BOOL enable)
372{
373	uint32_t val;
374	HAL_BOOL is_chan_2g = AH_FALSE;
375	HAL_BOOL is_ht40 = AH_FALSE;
376
377	if (chan)
378		is_chan_2g = IEEE80211_IS_CHAN_2GHZ(chan);
379
380	if (chan)
381		is_ht40 = IEEE80211_IS_CHAN_HT40(chan);
382
383	/* Only support disabling RIFS delay for now */
384	HALASSERT(enable == AH_FALSE);
385
386	if (enable == AH_TRUE)
387		return AH_FALSE;
388
389	/* Change RIFS init delay to 0 */
390	val = OS_REG_READ(ah, AR_PHY_HEAVY_CLIP_FACTOR_RIFS);
391	val &= ~AR_PHY_RIFS_INIT_DELAY;
392	OS_REG_WRITE(ah, AR_PHY_HEAVY_CLIP_FACTOR_RIFS, val);
393
394	/*
395	 * For Owl, RIFS RX parameters are controlled differently;
396	 * it isn't enabled in the inivals by default.
397	 *
398	 * For Sowl/Howl, RIFS RX is enabled in the inivals by default;
399	 * the following code sets them back to non-RIFS values.
400	 *
401	 * For > Sowl/Howl, RIFS RX can be left on by default and so
402	 * this function shouldn't be called.
403	 */
404	if ((! AR_SREV_SOWL(ah)) && (! AR_SREV_HOWL(ah)))
405		return AH_TRUE;
406
407	/* Reset search delay to default values */
408	if (is_chan_2g)
409		if (is_ht40)
410			OS_REG_WRITE(ah, AR_PHY_SEARCH_START_DELAY, 0x268);
411		else
412			OS_REG_WRITE(ah, AR_PHY_SEARCH_START_DELAY, 0x134);
413	else
414		if (is_ht40)
415			OS_REG_WRITE(ah, AR_PHY_SEARCH_START_DELAY, 0x370);
416		else
417			OS_REG_WRITE(ah, AR_PHY_SEARCH_START_DELAY, 0x1b8);
418
419	return AH_TRUE;
420}
421
422static HAL_BOOL
423ar5416CompareDbgHang(struct ath_hal *ah, const mac_dbg_regs_t *regs,
424    const hal_mac_hang_check_t *check)
425{
426	int found_states;
427
428	found_states = 0;
429	if (check->states & dcu_chain_state) {
430		int i;
431
432		for (i = 0; i < 6; i++) {
433			if (((regs->dma_dbg_4 >> (5*i)) & 0x1f) ==
434			    check->dcu_chain_state)
435				found_states |= dcu_chain_state;
436		}
437		for (i = 0; i < 4; i++) {
438			if (((regs->dma_dbg_5 >> (5*i)) & 0x1f) ==
439			    check->dcu_chain_state)
440				found_states |= dcu_chain_state;
441		}
442	}
443	if (check->states & dcu_complete_state) {
444		if ((regs->dma_dbg_6 & 0x3) == check->dcu_complete_state)
445			found_states |= dcu_complete_state;
446	}
447	if (check->states & qcu_stitch_state) {
448		if (((regs->dma_dbg_3 >> 18) & 0xf) == check->qcu_stitch_state)
449			found_states |= qcu_stitch_state;
450	}
451	if (check->states & qcu_fetch_state) {
452		if (((regs->dma_dbg_3 >> 22) & 0xf) == check->qcu_fetch_state)
453			found_states |= qcu_fetch_state;
454	}
455	if (check->states & qcu_complete_state) {
456		if (((regs->dma_dbg_3 >> 26) & 0x7) == check->qcu_complete_state)
457			found_states |= qcu_complete_state;
458	}
459	return (found_states == check->states);
460}
461
462#define NUM_STATUS_READS 50
463
464static int
465ar5416DetectMacHang(struct ath_hal *ah)
466{
467	static const hal_mac_hang_check_t hang_sig1 = {
468		.dcu_chain_state	= 0x6,
469		.dcu_complete_state	= 0x1,
470		.states			= dcu_chain_state
471					| dcu_complete_state,
472	};
473	static const hal_mac_hang_check_t hang_sig2 = {
474		.qcu_stitch_state	= 0x9,
475		.qcu_fetch_state	= 0x8,
476		.qcu_complete_state	= 0x4,
477		.states			= qcu_stitch_state
478					| qcu_fetch_state
479					| qcu_complete_state,
480        };
481	mac_dbg_regs_t mac_dbg;
482	int i;
483
484	mac_dbg.dma_dbg_3 = OS_REG_READ(ah, AR_DMADBG_3);
485	mac_dbg.dma_dbg_4 = OS_REG_READ(ah, AR_DMADBG_4);
486	mac_dbg.dma_dbg_5 = OS_REG_READ(ah, AR_DMADBG_5);
487	mac_dbg.dma_dbg_6 = OS_REG_READ(ah, AR_DMADBG_6);
488	for (i = 1; i <= NUM_STATUS_READS; i++) {
489		if (mac_dbg.dma_dbg_3 != OS_REG_READ(ah, AR_DMADBG_3) ||
490		    mac_dbg.dma_dbg_4 != OS_REG_READ(ah, AR_DMADBG_4) ||
491		    mac_dbg.dma_dbg_5 != OS_REG_READ(ah, AR_DMADBG_5) ||
492		    mac_dbg.dma_dbg_6 != OS_REG_READ(ah, AR_DMADBG_6))
493			return 0;
494	}
495
496	if (ar5416CompareDbgHang(ah, &mac_dbg, &hang_sig1))
497		return HAL_MAC_HANG_SIG1;
498	if (ar5416CompareDbgHang(ah, &mac_dbg, &hang_sig2))
499		return HAL_MAC_HANG_SIG2;
500
501	HALDEBUG(ah, HAL_DEBUG_HANG, "%s Found an unknown MAC hang signature "
502	    "DMADBG_3=0x%x DMADBG_4=0x%x DMADBG_5=0x%x DMADBG_6=0x%x\n",
503	    __func__, mac_dbg.dma_dbg_3, mac_dbg.dma_dbg_4, mac_dbg.dma_dbg_5,
504	    mac_dbg.dma_dbg_6);
505
506	return 0;
507}
508
509/*
510 * Determine if the baseband using the Observation Bus Register
511 */
512static int
513ar5416DetectBBHang(struct ath_hal *ah)
514{
515#define N(a) (sizeof(a)/sizeof(a[0]))
516	/*
517	 * Check the PCU Observation Bus 1 register (0x806c)
518	 * NUM_STATUS_READS times
519	 *
520	 * 4 known BB hang signatures -
521	 * [1] bits 8,9,11 are 0. State machine state (bits 25-31) is 0x1E
522	 * [2] bits 8,9 are 1, bit 11 is 0. State machine state
523	 *     (bits 25-31) is 0x52
524	 * [3] bits 8,9 are 1, bit 11 is 0. State machine state
525	 *     (bits 25-31) is 0x18
526	 * [4] bit 10 is 1, bit 11 is 0. WEP state (bits 12-17) is 0x2,
527	 *     Rx State (bits 20-24) is 0x7.
528	 */
529	static const struct {
530		uint32_t val;
531		uint32_t mask;
532		int code;
533	} hang_list[] = {
534		/* Reg Value   Reg Mask    Hang Code XXX */
535		{ 0x1E000000, 0x7E000B00, HAL_BB_HANG_DFS },
536		{ 0x52000B00, 0x7E000B00, HAL_BB_HANG_RIFS },
537		{ 0x18000B00, 0x7E000B00, HAL_BB_HANG_RX_CLEAR },
538		{ 0x00702400, 0x7E7FFFEF, HAL_BB_HANG_RX_CLEAR }
539	};
540	uint32_t hang_sig;
541	int i;
542
543	hang_sig = OS_REG_READ(ah, AR_OBSERV_1);
544	for (i = 1; i <= NUM_STATUS_READS; i++) {
545		if (hang_sig != OS_REG_READ(ah, AR_OBSERV_1))
546			return 0;
547	}
548	for (i = 0; i < N(hang_list); i++)
549		if ((hang_sig & hang_list[i].mask) == hang_list[i].val) {
550			HALDEBUG(ah, HAL_DEBUG_HANG,
551			    "%s BB hang, signature 0x%x, code 0x%x\n",
552			    __func__, hang_sig, hang_list[i].code);
553			return hang_list[i].code;
554		}
555
556	HALDEBUG(ah, HAL_DEBUG_HANG, "%s Found an unknown BB hang signature! "
557	    "<0x806c>=0x%x\n", __func__, hang_sig);
558
559	return 0;
560#undef N
561}
562#undef NUM_STATUS_READS
563