ar9280_attach.c revision 234508
1/*
2 * Copyright (c) 2008-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 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/ar9002/ar9280_attach.c 234508 2012-04-20 21:56:13Z adrian $
18 */
19#include "opt_ah.h"
20
21#include "ah.h"
22#include "ah_internal.h"
23#include "ah_devid.h"
24
25#include "ah_eeprom_v14.h"		/* XXX for tx/rx gain */
26
27#include "ar9002/ar9280.h"
28#include "ar5416/ar5416reg.h"
29#include "ar5416/ar5416phy.h"
30
31#include "ar9002/ar9280v1.ini"
32#include "ar9002/ar9280v2.ini"
33#include "ar9002/ar9280_olc.h"
34
35static const HAL_PERCAL_DATA ar9280_iq_cal = {		/* single sample */
36	.calName = "IQ", .calType = IQ_MISMATCH_CAL,
37	.calNumSamples	= MIN_CAL_SAMPLES,
38	.calCountMax	= PER_MAX_LOG_COUNT,
39	.calCollect	= ar5416IQCalCollect,
40	.calPostProc	= ar5416IQCalibration
41};
42static const HAL_PERCAL_DATA ar9280_adc_gain_cal = {	/* single sample */
43	.calName = "ADC Gain", .calType = ADC_GAIN_CAL,
44	.calNumSamples	= MIN_CAL_SAMPLES,
45	.calCountMax	= PER_MAX_LOG_COUNT,
46	.calCollect	= ar5416AdcGainCalCollect,
47	.calPostProc	= ar5416AdcGainCalibration
48};
49static const HAL_PERCAL_DATA ar9280_adc_dc_cal = {	/* single sample */
50	.calName = "ADC DC", .calType = ADC_DC_CAL,
51	.calNumSamples	= MIN_CAL_SAMPLES,
52	.calCountMax	= PER_MAX_LOG_COUNT,
53	.calCollect	= ar5416AdcDcCalCollect,
54	.calPostProc	= ar5416AdcDcCalibration
55};
56static const HAL_PERCAL_DATA ar9280_adc_init_dc_cal = {
57	.calName = "ADC Init DC", .calType = ADC_DC_INIT_CAL,
58	.calNumSamples	= MIN_CAL_SAMPLES,
59	.calCountMax	= INIT_LOG_COUNT,
60	.calCollect	= ar5416AdcDcCalCollect,
61	.calPostProc	= ar5416AdcDcCalibration
62};
63
64static void ar9280ConfigPCIE(struct ath_hal *ah, HAL_BOOL restore);
65static HAL_BOOL ar9280FillCapabilityInfo(struct ath_hal *ah);
66static void ar9280WriteIni(struct ath_hal *ah,
67	const struct ieee80211_channel *chan);
68
69static void
70ar9280AniSetup(struct ath_hal *ah)
71{
72	/*
73	 * These are the parameters from the AR5416 ANI code;
74	 * they likely need quite a bit of adjustment for the
75	 * AR9280.
76	 */
77        static const struct ar5212AniParams aniparams = {
78                .maxNoiseImmunityLevel  = 4,    /* levels 0..4 */
79                .totalSizeDesired       = { -55, -55, -55, -55, -62 },
80                .coarseHigh             = { -14, -14, -14, -14, -12 },
81                .coarseLow              = { -64, -64, -64, -64, -70 },
82                .firpwr                 = { -78, -78, -78, -78, -80 },
83                .maxSpurImmunityLevel   = 2,
84                .cycPwrThr1             = { 2, 4, 6 },
85                .maxFirstepLevel        = 2,    /* levels 0..2 */
86                .firstep                = { 0, 4, 8 },
87                .ofdmTrigHigh           = 500,
88                .ofdmTrigLow            = 200,
89                .cckTrigHigh            = 200,
90                .cckTrigLow             = 100,
91                .rssiThrHigh            = 40,
92                .rssiThrLow             = 7,
93                .period                 = 100,
94        };
95	/* NB: disable ANI noise immmunity for reliable RIFS rx */
96	AH5416(ah)->ah_ani_function &= ~(1 << HAL_ANI_NOISE_IMMUNITY_LEVEL);
97
98        /* NB: ANI is not enabled yet */
99        ar5416AniAttach(ah, &aniparams, &aniparams, AH_TRUE);
100}
101
102void
103ar9280InitPLL(struct ath_hal *ah, const struct ieee80211_channel *chan)
104{
105	uint32_t pll = SM(0x5, AR_RTC_SOWL_PLL_REFDIV);
106
107	if (AR_SREV_MERLIN_20(ah) &&
108	    chan != AH_NULL && IEEE80211_IS_CHAN_5GHZ(chan)) {
109		/*
110		 * PLL WAR for Merlin 2.0/2.1
111		 * When doing fast clock, set PLL to 0x142c
112		 * Else, set PLL to 0x2850 to prevent reset-to-reset variation
113		 */
114		pll = IS_5GHZ_FAST_CLOCK_EN(ah, chan) ? 0x142c : 0x2850;
115	} else if (AR_SREV_MERLIN_10_OR_LATER(ah)) {
116		pll = SM(0x5, AR_RTC_SOWL_PLL_REFDIV);
117		if (chan != AH_NULL) {
118			if (IEEE80211_IS_CHAN_HALF(chan))
119				pll |= SM(0x1, AR_RTC_SOWL_PLL_CLKSEL);
120			else if (IEEE80211_IS_CHAN_QUARTER(chan))
121				pll |= SM(0x2, AR_RTC_SOWL_PLL_CLKSEL);
122			if (IEEE80211_IS_CHAN_5GHZ(chan))
123				pll |= SM(0x28, AR_RTC_SOWL_PLL_DIV);
124			else
125				pll |= SM(0x2c, AR_RTC_SOWL_PLL_DIV);
126		} else
127			pll |= SM(0x2c, AR_RTC_SOWL_PLL_DIV);
128	}
129
130	OS_REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
131	OS_DELAY(RTC_PLL_SETTLE_DELAY);
132	OS_REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_SLEEP_DERIVED_CLK);
133}
134
135/* XXX shouldn't be here! */
136#define	EEP_MINOR(_ah) \
137	(AH_PRIVATE(_ah)->ah_eeversion & AR5416_EEP_VER_MINOR_MASK)
138
139/*
140 * Attach for an AR9280 part.
141 */
142static struct ath_hal *
143ar9280Attach(uint16_t devid, HAL_SOFTC sc,
144	HAL_BUS_TAG st, HAL_BUS_HANDLE sh, uint16_t *eepromdata,
145	HAL_STATUS *status)
146{
147	struct ath_hal_9280 *ahp9280;
148	struct ath_hal_5212 *ahp;
149	struct ath_hal *ah;
150	uint32_t val;
151	HAL_STATUS ecode;
152	HAL_BOOL rfStatus;
153	int8_t pwr_table_offset;
154	uint8_t pwr;
155
156	HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
157	    __func__, sc, (void*) st, (void*) sh);
158
159	/* NB: memory is returned zero'd */
160	ahp9280 = ath_hal_malloc(sizeof (struct ath_hal_9280));
161	if (ahp9280 == AH_NULL) {
162		HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
163		    "%s: cannot allocate memory for state block\n", __func__);
164		*status = HAL_ENOMEM;
165		return AH_NULL;
166	}
167	ahp = AH5212(ahp9280);
168	ah = &ahp->ah_priv.h;
169
170	ar5416InitState(AH5416(ah), devid, sc, st, sh, status);
171
172	/*
173	 * Use the "local" EEPROM data given to us by the higher layers.
174	 * This is a private copy out of system flash. The Linux ath9k
175	 * commit for the initial AR9130 support mentions MMIO flash
176	 * access is "unreliable." -adrian
177	 */
178	if (eepromdata != AH_NULL) {
179		AH_PRIVATE((ah))->ah_eepromRead = ath_hal_EepromDataRead;
180		AH_PRIVATE((ah))->ah_eepromWrite = NULL;
181		ah->ah_eepromdata = eepromdata;
182
183	/* XXX override with 9280 specific state */
184	/* override 5416 methods for our needs */
185	AH5416(ah)->ah_initPLL = ar9280InitPLL;
186
187	ah->ah_setAntennaSwitch		= ar9280SetAntennaSwitch;
188	ah->ah_configPCIE		= ar9280ConfigPCIE;
189
190	AH5416(ah)->ah_cal.iqCalData.calData = &ar9280_iq_cal;
191	AH5416(ah)->ah_cal.adcGainCalData.calData = &ar9280_adc_gain_cal;
192	AH5416(ah)->ah_cal.adcDcCalData.calData = &ar9280_adc_dc_cal;
193	AH5416(ah)->ah_cal.adcDcCalInitData.calData = &ar9280_adc_init_dc_cal;
194	AH5416(ah)->ah_cal.suppCals = ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
195
196	AH5416(ah)->ah_spurMitigate	= ar9280SpurMitigate;
197	AH5416(ah)->ah_writeIni		= ar9280WriteIni;
198	AH5416(ah)->ah_olcInit		= ar9280olcInit;
199	AH5416(ah)->ah_olcTempCompensation = ar9280olcTemperatureCompensation;
200	AH5416(ah)->ah_setPowerCalTable	= ar9280SetPowerCalTable;
201
202	AH5416(ah)->ah_rx_chainmask	= AR9280_DEFAULT_RXCHAINMASK;
203	AH5416(ah)->ah_tx_chainmask	= AR9280_DEFAULT_TXCHAINMASK;
204
205	if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON)) {
206		/* reset chip */
207		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't reset chip\n",
208		    __func__);
209		ecode = HAL_EIO;
210		goto bad;
211	}
212
213	if (!ar5416SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE)) {
214		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't wakeup chip\n",
215		    __func__);
216		ecode = HAL_EIO;
217		goto bad;
218	}
219	/* Read Revisions from Chips before taking out of reset */
220	val = OS_REG_READ(ah, AR_SREV);
221	HALDEBUG(ah, HAL_DEBUG_ATTACH,
222	    "%s: ID 0x%x VERSION 0x%x TYPE 0x%x REVISION 0x%x\n",
223	    __func__, MS(val, AR_XSREV_ID), MS(val, AR_XSREV_VERSION),
224	    MS(val, AR_XSREV_TYPE), MS(val, AR_XSREV_REVISION));
225	/* NB: include chip type to differentiate from pre-Sowl versions */
226	AH_PRIVATE(ah)->ah_macVersion =
227	    (val & AR_XSREV_VERSION) >> AR_XSREV_TYPE_S;
228	AH_PRIVATE(ah)->ah_macRev = MS(val, AR_XSREV_REVISION);
229	AH_PRIVATE(ah)->ah_ispcie = (val & AR_XSREV_TYPE_HOST_MODE) == 0;
230
231	/* setup common ini data; rf backends handle remainder */
232	if (AR_SREV_MERLIN_20_OR_LATER(ah)) {
233		HAL_INI_INIT(&ahp->ah_ini_modes, ar9280Modes_v2, 6);
234		HAL_INI_INIT(&ahp->ah_ini_common, ar9280Common_v2, 2);
235		HAL_INI_INIT(&AH5416(ah)->ah_ini_pcieserdes,
236		    ar9280PciePhy_clkreq_always_on_L1_v2, 2);
237		HAL_INI_INIT(&ahp9280->ah_ini_xmodes,
238		    ar9280Modes_fast_clock_v2, 3);
239	} else {
240		HAL_INI_INIT(&ahp->ah_ini_modes, ar9280Modes_v1, 6);
241		HAL_INI_INIT(&ahp->ah_ini_common, ar9280Common_v1, 2);
242		HAL_INI_INIT(&AH5416(ah)->ah_ini_pcieserdes,
243		    ar9280PciePhy_v1, 2);
244	}
245	ar5416AttachPCIE(ah);
246
247	ecode = ath_hal_v14EepromAttach(ah);
248	if (ecode != HAL_OK)
249		goto bad;
250
251	if (!ar5416ChipReset(ah, AH_NULL)) {	/* reset chip */
252		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
253		ecode = HAL_EIO;
254		goto bad;
255	}
256
257	AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
258
259	if (!ar5212ChipTest(ah)) {
260		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
261		    __func__);
262		ecode = HAL_ESELFTEST;
263		goto bad;
264	}
265
266	/*
267	 * Set correct Baseband to analog shift
268	 * setting to access analog chips.
269	 */
270	OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
271
272	/* Read Radio Chip Rev Extract */
273	AH_PRIVATE(ah)->ah_analog5GhzRev = ar5416GetRadioRev(ah);
274	switch (AH_PRIVATE(ah)->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR) {
275        case AR_RAD2133_SREV_MAJOR:	/* Sowl: 2G/3x3 */
276	case AR_RAD5133_SREV_MAJOR:	/* Sowl: 2+5G/3x3 */
277		break;
278	default:
279		if (AH_PRIVATE(ah)->ah_analog5GhzRev == 0) {
280			AH_PRIVATE(ah)->ah_analog5GhzRev =
281				AR_RAD5133_SREV_MAJOR;
282			break;
283		}
284#ifdef AH_DEBUG
285		HALDEBUG(ah, HAL_DEBUG_ANY,
286		    "%s: 5G Radio Chip Rev 0x%02X is not supported by "
287		    "this driver\n", __func__,
288		    AH_PRIVATE(ah)->ah_analog5GhzRev);
289		ecode = HAL_ENOTSUPP;
290		goto bad;
291#endif
292	}
293	rfStatus = ar9280RfAttach(ah, &ecode);
294	if (!rfStatus) {
295		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: RF setup failed, status %u\n",
296		    __func__, ecode);
297		goto bad;
298	}
299
300	/* Enable fixup for AR_AN_TOP2 if necessary */
301	/*
302	 * The v14 EEPROM layer returns HAL_EIO if PWDCLKIND isn't supported
303	 * by the EEPROM version.
304	 *
305	 * ath9k checks the EEPROM minor version is >= 0x0a here, instead of
306	 * the abstracted EEPROM access layer.
307	 */
308	ecode = ath_hal_eepromGet(ah, AR_EEP_PWDCLKIND, &pwr);
309	if (AR_SREV_MERLIN_20_OR_LATER(ah) && ecode == HAL_OK && pwr == 0) {
310		printf("[ath] enabling AN_TOP2_FIXUP\n");
311		AH5416(ah)->ah_need_an_top2_fixup = 1;
312	}
313
314        /*
315         * Check whether the power table offset isn't the default.
316         * This can occur with eeprom minor V21 or greater on Merlin.
317         */
318	(void) ath_hal_eepromGet(ah, AR_EEP_PWR_TABLE_OFFSET, &pwr_table_offset);
319	if (pwr_table_offset != AR5416_PWR_TABLE_OFFSET_DB)
320		ath_hal_printf(ah, "[ath]: default pwr offset: %d dBm != EEPROM pwr offset: %d dBm; curves will be adjusted.\n",
321		    AR5416_PWR_TABLE_OFFSET_DB, (int) pwr_table_offset);
322
323	/* XXX check for >= minor ver 17 */
324	if (AR_SREV_MERLIN_20(ah)) {
325		/* setup rxgain table */
326		switch (ath_hal_eepromGet(ah, AR_EEP_RXGAIN_TYPE, AH_NULL)) {
327		case AR5416_EEP_RXGAIN_13dB_BACKOFF:
328			HAL_INI_INIT(&ahp9280->ah_ini_rxgain,
329			    ar9280Modes_backoff_13db_rxgain_v2, 6);
330			break;
331		case AR5416_EEP_RXGAIN_23dB_BACKOFF:
332			HAL_INI_INIT(&ahp9280->ah_ini_rxgain,
333			    ar9280Modes_backoff_23db_rxgain_v2, 6);
334			break;
335		case AR5416_EEP_RXGAIN_ORIG:
336			HAL_INI_INIT(&ahp9280->ah_ini_rxgain,
337			    ar9280Modes_original_rxgain_v2, 6);
338			break;
339		default:
340			HALASSERT(AH_FALSE);
341			goto bad;		/* XXX ? try to continue */
342		}
343	}
344
345	/* XXX check for >= minor ver 19 */
346	if (AR_SREV_MERLIN_20(ah)) {
347		/* setp txgain table */
348		switch (ath_hal_eepromGet(ah, AR_EEP_TXGAIN_TYPE, AH_NULL)) {
349		case AR5416_EEP_TXGAIN_HIGH_POWER:
350			HAL_INI_INIT(&ahp9280->ah_ini_txgain,
351			    ar9280Modes_high_power_tx_gain_v2, 6);
352			break;
353		case AR5416_EEP_TXGAIN_ORIG:
354			HAL_INI_INIT(&ahp9280->ah_ini_txgain,
355			    ar9280Modes_original_tx_gain_v2, 6);
356			break;
357		default:
358			HALASSERT(AH_FALSE);
359			goto bad;		/* XXX ? try to continue */
360		}
361	}
362
363	/*
364	 * Got everything we need now to setup the capabilities.
365	 */
366	if (!ar9280FillCapabilityInfo(ah)) {
367		ecode = HAL_EEREAD;
368		goto bad;
369	}
370
371	ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
372	if (ecode != HAL_OK) {
373		HALDEBUG(ah, HAL_DEBUG_ANY,
374		    "%s: error getting mac address from EEPROM\n", __func__);
375		goto bad;
376        }
377	/* XXX How about the serial number ? */
378	/* Read Reg Domain */
379	AH_PRIVATE(ah)->ah_currentRD =
380	    ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, AH_NULL);
381	AH_PRIVATE(ah)->ah_currentRDext =
382	    ath_hal_eepromGet(ah, AR_EEP_REGDMN_1, AH_NULL);
383
384	/*
385	 * ah_miscMode is populated by ar5416FillCapabilityInfo()
386	 * starting from griffin. Set here to make sure that
387	 * AR_MISC_MODE_MIC_NEW_LOC_ENABLE is set before a GTK is
388	 * placed into hardware.
389	 */
390	if (ahp->ah_miscMode != 0)
391		OS_REG_WRITE(ah, AR_MISC_MODE, OS_REG_READ(ah, AR_MISC_MODE) | ahp->ah_miscMode);
392
393	ar9280AniSetup(ah);			/* Anti Noise Immunity */
394
395	/* Setup noise floor min/max/nominal values */
396	AH5416(ah)->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9280_2GHZ;
397	AH5416(ah)->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9280_2GHZ;
398	AH5416(ah)->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9280_2GHZ;
399	AH5416(ah)->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_9280_5GHZ;
400	AH5416(ah)->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_9280_5GHZ;
401	AH5416(ah)->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_9280_5GHZ;
402
403	ar5416InitNfHistBuff(AH5416(ah)->ah_cal.nfCalHist);
404
405	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
406
407	return ah;
408bad:
409	if (ah != AH_NULL)
410		ah->ah_detach(ah);
411	if (status)
412		*status = ecode;
413	return AH_NULL;
414}
415
416static void
417ar9280ConfigPCIE(struct ath_hal *ah, HAL_BOOL restore)
418{
419	if (AH_PRIVATE(ah)->ah_ispcie && !restore) {
420		ath_hal_ini_write(ah, &AH5416(ah)->ah_ini_pcieserdes, 1, 0);
421		OS_DELAY(1000);
422		OS_REG_SET_BIT(ah, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA);
423		OS_REG_WRITE(ah, AR_WA, AR9280_WA_DEFAULT);
424	}
425}
426
427static void
428ar9280WriteIni(struct ath_hal *ah, const struct ieee80211_channel *chan)
429{
430	u_int modesIndex, freqIndex;
431	int regWrites = 0;
432	int i;
433	const HAL_INI_ARRAY *ia;
434
435	/* Setup the indices for the next set of register array writes */
436	/* XXX Ignore 11n dynamic mode on the AR5416 for the moment */
437	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
438		freqIndex = 2;
439		if (IEEE80211_IS_CHAN_HT40(chan))
440			modesIndex = 3;
441		else if (IEEE80211_IS_CHAN_108G(chan))
442			modesIndex = 5;
443		else
444			modesIndex = 4;
445	} else {
446		freqIndex = 1;
447		if (IEEE80211_IS_CHAN_HT40(chan) ||
448		    IEEE80211_IS_CHAN_TURBO(chan))
449			modesIndex = 2;
450		else
451			modesIndex = 1;
452	}
453
454	/* Set correct Baseband to analog shift setting to access analog chips. */
455	OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
456	OS_REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
457
458	/*
459	 * This is unwound because at the moment, there's a requirement
460	 * for Merlin (and later, perhaps) to have a specific bit fixed
461	 * in the AR_AN_TOP2 register before writing it.
462	 */
463	ia = &AH5212(ah)->ah_ini_modes;
464#if 0
465	regWrites = ath_hal_ini_write(ah, &AH5212(ah)->ah_ini_modes,
466	    modesIndex, regWrites);
467#endif
468	HALASSERT(modesIndex < ia->cols);
469	for (i = 0; i < ia->rows; i++) {
470		uint32_t reg = HAL_INI_VAL(ia, i, 0);
471		uint32_t val = HAL_INI_VAL(ia, i, modesIndex);
472
473		if (reg == AR_AN_TOP2 && AH5416(ah)->ah_need_an_top2_fixup)
474			val &= ~AR_AN_TOP2_PWDCLKIND;
475
476		OS_REG_WRITE(ah, reg, val);
477
478		/* Analog shift register delay seems needed for Merlin - PR kern/154220 */
479		if (reg >= 0x7800 && reg < 0x7900)
480			OS_DELAY(100);
481
482		DMA_YIELD(regWrites);
483	}
484
485	if (AR_SREV_MERLIN_20_OR_LATER(ah)) {
486		regWrites = ath_hal_ini_write(ah, &AH9280(ah)->ah_ini_rxgain,
487		    modesIndex, regWrites);
488		regWrites = ath_hal_ini_write(ah, &AH9280(ah)->ah_ini_txgain,
489		    modesIndex, regWrites);
490	}
491	/* XXX Merlin 100us delay for shift registers */
492	regWrites = ath_hal_ini_write(ah, &AH5212(ah)->ah_ini_common,
493	    1, regWrites);
494
495	if (AR_SREV_MERLIN_20(ah) && IS_5GHZ_FAST_CLOCK_EN(ah, chan)) {
496		/* 5GHz channels w/ Fast Clock use different modal values */
497		regWrites = ath_hal_ini_write(ah, &AH9280(ah)->ah_ini_xmodes,
498		    modesIndex, regWrites);
499	}
500}
501
502#define	AR_BASE_FREQ_2GHZ	2300
503#define	AR_BASE_FREQ_5GHZ	4900
504#define	AR_SPUR_FEEQ_BOUND_HT40	19
505#define	AR_SPUR_FEEQ_BOUND_HT20	10
506
507void
508ar9280SpurMitigate(struct ath_hal *ah, const struct ieee80211_channel *chan)
509{
510    static const int pilot_mask_reg[4] = { AR_PHY_TIMING7, AR_PHY_TIMING8,
511                AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60 };
512    static const int chan_mask_reg[4] = { AR_PHY_TIMING9, AR_PHY_TIMING10,
513                AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60 };
514    static int inc[4] = { 0, 100, 0, 0 };
515
516    int bb_spur = AR_NO_SPUR;
517    int freq;
518    int bin, cur_bin;
519    int bb_spur_off, spur_subchannel_sd;
520    int spur_freq_sd;
521    int spur_delta_phase;
522    int denominator;
523    int upper, lower, cur_vit_mask;
524    int tmp, newVal;
525    int i;
526    CHAN_CENTERS centers;
527
528    int8_t mask_m[123];
529    int8_t mask_p[123];
530    int8_t mask_amt;
531    int tmp_mask;
532    int cur_bb_spur;
533    HAL_BOOL is2GHz = IEEE80211_IS_CHAN_2GHZ(chan);
534
535    OS_MEMZERO(&mask_m, sizeof(int8_t) * 123);
536    OS_MEMZERO(&mask_p, sizeof(int8_t) * 123);
537
538    ar5416GetChannelCenters(ah, chan, &centers);
539    freq = centers.synth_center;
540
541    /*
542     * Need to verify range +/- 9.38 for static ht20 and +/- 18.75 for ht40,
543     * otherwise spur is out-of-band and can be ignored.
544     */
545    for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
546        cur_bb_spur = ath_hal_getSpurChan(ah, i, is2GHz);
547        /* Get actual spur freq in MHz from EEPROM read value */
548        if (is2GHz) {
549            cur_bb_spur =  (cur_bb_spur / 10) + AR_BASE_FREQ_2GHZ;
550        } else {
551            cur_bb_spur =  (cur_bb_spur / 10) + AR_BASE_FREQ_5GHZ;
552        }
553
554        if (AR_NO_SPUR == cur_bb_spur)
555            break;
556        cur_bb_spur = cur_bb_spur - freq;
557
558        if (IEEE80211_IS_CHAN_HT40(chan)) {
559            if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT40) &&
560                (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT40)) {
561                bb_spur = cur_bb_spur;
562                break;
563            }
564        } else if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT20) &&
565                   (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT20)) {
566            bb_spur = cur_bb_spur;
567            break;
568        }
569    }
570
571    if (AR_NO_SPUR == bb_spur) {
572#if 1
573        /*
574         * MRC CCK can interfere with beacon detection and cause deaf/mute.
575         * Disable MRC CCK for now.
576         */
577        OS_REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK, AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
578#else
579        /* Enable MRC CCK if no spur is found in this channel. */
580        OS_REG_SET_BIT(ah, AR_PHY_FORCE_CLKEN_CCK, AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
581#endif
582        return;
583    } else {
584        /*
585         * For Merlin, spur can break CCK MRC algorithm. Disable CCK MRC if spur
586         * is found in this channel.
587         */
588        OS_REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK, AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
589    }
590
591    bin = bb_spur * 320;
592
593    tmp = OS_REG_READ(ah, AR_PHY_TIMING_CTRL4_CHAIN(0));
594
595    newVal = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
596        AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
597        AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
598        AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
599    OS_REG_WRITE(ah, AR_PHY_TIMING_CTRL4_CHAIN(0), newVal);
600
601    newVal = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
602        AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
603        AR_PHY_SPUR_REG_MASK_RATE_SELECT |
604        AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
605        SM(AR5416_SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
606    OS_REG_WRITE(ah, AR_PHY_SPUR_REG, newVal);
607
608    /* Pick control or extn channel to cancel the spur */
609    if (IEEE80211_IS_CHAN_HT40(chan)) {
610        if (bb_spur < 0) {
611            spur_subchannel_sd = 1;
612            bb_spur_off = bb_spur + 10;
613        } else {
614            spur_subchannel_sd = 0;
615            bb_spur_off = bb_spur - 10;
616        }
617    } else {
618        spur_subchannel_sd = 0;
619        bb_spur_off = bb_spur;
620    }
621
622    /*
623     * spur_delta_phase = bb_spur/40 * 2**21 for static ht20,
624     * /80 for dyn2040.
625     */
626    if (IEEE80211_IS_CHAN_HT40(chan))
627        spur_delta_phase = ((bb_spur * 262144) / 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
628    else
629        spur_delta_phase = ((bb_spur * 524288) / 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
630
631    /*
632     * in 11A mode the denominator of spur_freq_sd should be 40 and
633     * it should be 44 in 11G
634     */
635    denominator = IEEE80211_IS_CHAN_2GHZ(chan) ? 44 : 40;
636    spur_freq_sd = ((bb_spur_off * 2048) / denominator) & 0x3ff;
637
638    newVal = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
639        SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
640        SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
641    OS_REG_WRITE(ah, AR_PHY_TIMING11, newVal);
642
643    /* Choose to cancel between control and extension channels */
644    newVal = spur_subchannel_sd << AR_PHY_SFCORR_SPUR_SUBCHNL_SD_S;
645    OS_REG_WRITE(ah, AR_PHY_SFCORR_EXT, newVal);
646
647    /*
648     * ============================================
649     * Set Pilot and Channel Masks
650     *
651     * pilot mask 1 [31:0] = +6..-26, no 0 bin
652     * pilot mask 2 [19:0] = +26..+7
653     *
654     * channel mask 1 [31:0] = +6..-26, no 0 bin
655     * channel mask 2 [19:0] = +26..+7
656     */
657    cur_bin = -6000;
658    upper = bin + 100;
659    lower = bin - 100;
660
661    for (i = 0; i < 4; i++) {
662        int pilot_mask = 0;
663        int chan_mask  = 0;
664        int bp         = 0;
665        for (bp = 0; bp < 30; bp++) {
666            if ((cur_bin > lower) && (cur_bin < upper)) {
667                pilot_mask = pilot_mask | 0x1 << bp;
668                chan_mask  = chan_mask | 0x1 << bp;
669            }
670            cur_bin += 100;
671        }
672        cur_bin += inc[i];
673        OS_REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
674        OS_REG_WRITE(ah, chan_mask_reg[i], chan_mask);
675    }
676
677    /* =================================================
678     * viterbi mask 1 based on channel magnitude
679     * four levels 0-3
680     *  - mask (-27 to 27) (reg 64,0x9900 to 67,0x990c)
681     *      [1 2 2 1] for -9.6 or [1 2 1] for +16
682     *  - enable_mask_ppm, all bins move with freq
683     *
684     *  - mask_select,    8 bits for rates (reg 67,0x990c)
685     *  - mask_rate_cntl, 8 bits for rates (reg 67,0x990c)
686     *      choose which mask to use mask or mask2
687     */
688
689    /*
690     * viterbi mask 2  2nd set for per data rate puncturing
691     * four levels 0-3
692     *  - mask_select, 8 bits for rates (reg 67)
693     *  - mask (-27 to 27) (reg 98,0x9988 to 101,0x9994)
694     *      [1 2 2 1] for -9.6 or [1 2 1] for +16
695     */
696    cur_vit_mask = 6100;
697    upper        = bin + 120;
698    lower        = bin - 120;
699
700    for (i = 0; i < 123; i++) {
701        if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
702            if ((abs(cur_vit_mask - bin)) < 75) {
703                mask_amt = 1;
704            } else {
705                mask_amt = 0;
706            }
707            if (cur_vit_mask < 0) {
708                mask_m[abs(cur_vit_mask / 100)] = mask_amt;
709            } else {
710                mask_p[cur_vit_mask / 100] = mask_amt;
711            }
712        }
713        cur_vit_mask -= 100;
714    }
715
716    tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
717          | (mask_m[48] << 26) | (mask_m[49] << 24)
718          | (mask_m[50] << 22) | (mask_m[51] << 20)
719          | (mask_m[52] << 18) | (mask_m[53] << 16)
720          | (mask_m[54] << 14) | (mask_m[55] << 12)
721          | (mask_m[56] << 10) | (mask_m[57] <<  8)
722          | (mask_m[58] <<  6) | (mask_m[59] <<  4)
723          | (mask_m[60] <<  2) | (mask_m[61] <<  0);
724    OS_REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
725    OS_REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
726
727    tmp_mask =             (mask_m[31] << 28)
728          | (mask_m[32] << 26) | (mask_m[33] << 24)
729          | (mask_m[34] << 22) | (mask_m[35] << 20)
730          | (mask_m[36] << 18) | (mask_m[37] << 16)
731          | (mask_m[48] << 14) | (mask_m[39] << 12)
732          | (mask_m[40] << 10) | (mask_m[41] <<  8)
733          | (mask_m[42] <<  6) | (mask_m[43] <<  4)
734          | (mask_m[44] <<  2) | (mask_m[45] <<  0);
735    OS_REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
736    OS_REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
737
738    tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
739          | (mask_m[18] << 26) | (mask_m[18] << 24)
740          | (mask_m[20] << 22) | (mask_m[20] << 20)
741          | (mask_m[22] << 18) | (mask_m[22] << 16)
742          | (mask_m[24] << 14) | (mask_m[24] << 12)
743          | (mask_m[25] << 10) | (mask_m[26] <<  8)
744          | (mask_m[27] <<  6) | (mask_m[28] <<  4)
745          | (mask_m[29] <<  2) | (mask_m[30] <<  0);
746    OS_REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
747    OS_REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
748
749    tmp_mask = (mask_m[ 0] << 30) | (mask_m[ 1] << 28)
750          | (mask_m[ 2] << 26) | (mask_m[ 3] << 24)
751          | (mask_m[ 4] << 22) | (mask_m[ 5] << 20)
752          | (mask_m[ 6] << 18) | (mask_m[ 7] << 16)
753          | (mask_m[ 8] << 14) | (mask_m[ 9] << 12)
754          | (mask_m[10] << 10) | (mask_m[11] <<  8)
755          | (mask_m[12] <<  6) | (mask_m[13] <<  4)
756          | (mask_m[14] <<  2) | (mask_m[15] <<  0);
757    OS_REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
758    OS_REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
759
760    tmp_mask =             (mask_p[15] << 28)
761          | (mask_p[14] << 26) | (mask_p[13] << 24)
762          | (mask_p[12] << 22) | (mask_p[11] << 20)
763          | (mask_p[10] << 18) | (mask_p[ 9] << 16)
764          | (mask_p[ 8] << 14) | (mask_p[ 7] << 12)
765          | (mask_p[ 6] << 10) | (mask_p[ 5] <<  8)
766          | (mask_p[ 4] <<  6) | (mask_p[ 3] <<  4)
767          | (mask_p[ 2] <<  2) | (mask_p[ 1] <<  0);
768    OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
769    OS_REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
770
771    tmp_mask =             (mask_p[30] << 28)
772          | (mask_p[29] << 26) | (mask_p[28] << 24)
773          | (mask_p[27] << 22) | (mask_p[26] << 20)
774          | (mask_p[25] << 18) | (mask_p[24] << 16)
775          | (mask_p[23] << 14) | (mask_p[22] << 12)
776          | (mask_p[21] << 10) | (mask_p[20] <<  8)
777          | (mask_p[19] <<  6) | (mask_p[18] <<  4)
778          | (mask_p[17] <<  2) | (mask_p[16] <<  0);
779    OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
780    OS_REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
781
782    tmp_mask =             (mask_p[45] << 28)
783          | (mask_p[44] << 26) | (mask_p[43] << 24)
784          | (mask_p[42] << 22) | (mask_p[41] << 20)
785          | (mask_p[40] << 18) | (mask_p[39] << 16)
786          | (mask_p[38] << 14) | (mask_p[37] << 12)
787          | (mask_p[36] << 10) | (mask_p[35] <<  8)
788          | (mask_p[34] <<  6) | (mask_p[33] <<  4)
789          | (mask_p[32] <<  2) | (mask_p[31] <<  0);
790    OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
791    OS_REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
792
793    tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
794          | (mask_p[59] << 26) | (mask_p[58] << 24)
795          | (mask_p[57] << 22) | (mask_p[56] << 20)
796          | (mask_p[55] << 18) | (mask_p[54] << 16)
797          | (mask_p[53] << 14) | (mask_p[52] << 12)
798          | (mask_p[51] << 10) | (mask_p[50] <<  8)
799          | (mask_p[49] <<  6) | (mask_p[48] <<  4)
800          | (mask_p[47] <<  2) | (mask_p[46] <<  0);
801    OS_REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
802    OS_REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
803}
804
805/*
806 * Fill all software cached or static hardware state information.
807 * Return failure if capabilities are to come from EEPROM and
808 * cannot be read.
809 */
810static HAL_BOOL
811ar9280FillCapabilityInfo(struct ath_hal *ah)
812{
813	HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
814
815	if (!ar5416FillCapabilityInfo(ah))
816		return AH_FALSE;
817	pCap->halNumGpioPins = 10;
818	pCap->halWowSupport = AH_TRUE;
819	pCap->halWowMatchPatternExact = AH_TRUE;
820#if 0
821	pCap->halWowMatchPatternDword = AH_TRUE;
822#endif
823	pCap->halCSTSupport = AH_TRUE;
824	pCap->halRifsRxSupport = AH_TRUE;
825	pCap->halRifsTxSupport = AH_TRUE;
826	pCap->halRtsAggrLimit = 64*1024;	/* 802.11n max */
827	pCap->halExtChanDfsSupport = AH_TRUE;
828	pCap->halUseCombinedRadarRssi = AH_TRUE;
829#if 0
830	/* XXX bluetooth */
831	pCap->halBtCoexSupport = AH_TRUE;
832#endif
833	pCap->halAutoSleepSupport = AH_FALSE;	/* XXX? */
834	pCap->hal4kbSplitTransSupport = AH_FALSE;
835	/* Disable this so Block-ACK works correctly */
836	pCap->halHasRxSelfLinkedTail = AH_FALSE;
837	pCap->halMbssidAggrSupport = AH_TRUE;
838	pCap->hal4AddrAggrSupport = AH_TRUE;
839
840	if (AR_SREV_MERLIN_20(ah)) {
841		pCap->halPSPollBroken = AH_FALSE;
842		/*
843		 * This just enables the support; it doesn't
844		 * state 5ghz fast clock will always be used.
845		 */
846		pCap->halSupportsFastClock5GHz = AH_TRUE;
847	}
848	pCap->halRxStbcSupport = 1;
849	pCap->halTxStbcSupport = 1;
850	pCap->halEnhancedDfsSupport = AH_TRUE;
851
852	return AH_TRUE;
853}
854
855/*
856 * This has been disabled - having the HAL flip chainmasks on/off
857 * when attempting to implement 11n disrupts things. For now, just
858 * leave this flipped off and worry about implementing TX diversity
859 * for legacy and MCS0-7 when 11n is fully functioning.
860 */
861HAL_BOOL
862ar9280SetAntennaSwitch(struct ath_hal *ah, HAL_ANT_SETTING settings)
863{
864#define ANTENNA0_CHAINMASK    0x1
865#define ANTENNA1_CHAINMASK    0x2
866#if 0
867	struct ath_hal_5416 *ahp = AH5416(ah);
868
869	/* Antenna selection is done by setting the tx/rx chainmasks approp. */
870	switch (settings) {
871	case HAL_ANT_FIXED_A:
872		/* Enable first antenna only */
873		ahp->ah_tx_chainmask = ANTENNA0_CHAINMASK;
874		ahp->ah_rx_chainmask = ANTENNA0_CHAINMASK;
875		break;
876	case HAL_ANT_FIXED_B:
877		/* Enable second antenna only, after checking capability */
878		if (AH_PRIVATE(ah)->ah_caps.halTxChainMask > ANTENNA1_CHAINMASK)
879			ahp->ah_tx_chainmask = ANTENNA1_CHAINMASK;
880		ahp->ah_rx_chainmask = ANTENNA1_CHAINMASK;
881		break;
882	case HAL_ANT_VARIABLE:
883		/* Restore original chainmask settings */
884		/* XXX */
885		ahp->ah_tx_chainmask = AR9280_DEFAULT_TXCHAINMASK;
886		ahp->ah_rx_chainmask = AR9280_DEFAULT_RXCHAINMASK;
887		break;
888	}
889
890	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: settings=%d, tx/rx chainmask=%d/%d\n",
891	    __func__, settings, ahp->ah_tx_chainmask, ahp->ah_rx_chainmask);
892
893#endif
894	return AH_TRUE;
895#undef ANTENNA0_CHAINMASK
896#undef ANTENNA1_CHAINMASK
897}
898
899static const char*
900ar9280Probe(uint16_t vendorid, uint16_t devid)
901{
902	if (vendorid == ATHEROS_VENDOR_ID) {
903		if (devid == AR9280_DEVID_PCI)
904			return "Atheros 9220";
905		if (devid == AR9280_DEVID_PCIE)
906			return "Atheros 9280";
907	}
908	return AH_NULL;
909}
910AH_CHIP(AR9280, ar9280Probe, ar9280Attach);
911