ar9160_attach.c revision 185380
1/*
2 * Copyright (c) 2008 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 * $Id: ar9160_attach.c,v 1.14 2008/11/27 22:30:08 sam Exp $
18 */
19#include "opt_ah.h"
20
21#ifdef AH_SUPPORT_AR9160
22
23#if !defined(AH_SUPPORT_AR5416)
24#error "No 5416 support defined"
25#endif
26#if !defined(AH_SUPPORT_2133)
27#error "No 2133 RF support defined"
28#endif
29
30#include "ah.h"
31#include "ah_internal.h"
32#include "ah_devid.h"
33
34#include "ar5416/ar5416.h"
35#include "ar5416/ar5416reg.h"
36#include "ar5416/ar5416phy.h"
37
38#include "ar5416/ar9160.ini"
39
40static const HAL_PERCAL_DATA ar9160_iq_cal = {		/* multi sample */
41	.calName = "IQ", .calType = IQ_MISMATCH_CAL,
42	.calNumSamples	= MAX_CAL_SAMPLES,
43	.calCountMax	= PER_MIN_LOG_COUNT,
44	.calCollect	= ar5416IQCalCollect,
45	.calPostProc	= ar5416IQCalibration
46};
47static const HAL_PERCAL_DATA ar9160_adc_gain_cal = {	/* multi sample */
48	.calName = "ADC Gain", .calType = ADC_GAIN_CAL,
49	.calNumSamples	= MAX_CAL_SAMPLES,
50	.calCountMax	= PER_MIN_LOG_COUNT,
51	.calCollect	= ar5416AdcGainCalCollect,
52	.calPostProc	= ar5416AdcGainCalibration
53};
54static const HAL_PERCAL_DATA ar9160_adc_dc_cal = {	/* multi sample */
55	.calName = "ADC DC", .calType = ADC_DC_CAL,
56	.calNumSamples	= MAX_CAL_SAMPLES,
57	.calCountMax	= PER_MIN_LOG_COUNT,
58	.calCollect	= ar5416AdcDcCalCollect,
59	.calPostProc	= ar5416AdcDcCalibration
60};
61static const HAL_PERCAL_DATA ar9160_adc_init_dc_cal = {
62	.calName = "ADC Init DC", .calType = ADC_DC_INIT_CAL,
63	.calNumSamples	= MIN_CAL_SAMPLES,
64	.calCountMax	= INIT_LOG_COUNT,
65	.calCollect	= ar5416AdcDcCalCollect,
66	.calPostProc	= ar5416AdcDcCalibration
67};
68
69struct ath_hal *ar9160Attach(uint16_t devid, HAL_SOFTC sc,
70	HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status);
71static void ar9160Detach(struct ath_hal *);
72static HAL_BOOL ar9160FillCapabilityInfo(struct ath_hal *ah);
73
74static void
75ar9160AniSetup(struct ath_hal *ah)
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: ANI is not enabled yet */
96	ar5212AniAttach(ah, &aniparams, &aniparams, AH_FALSE);
97}
98
99/*
100 * Attach for an AR9160 part.
101 */
102struct ath_hal *
103ar9160Attach(uint16_t devid, HAL_SOFTC sc,
104	HAL_BUS_TAG st, HAL_BUS_HANDLE sh, HAL_STATUS *status)
105{
106	struct ath_hal_5416 *ahp5416;
107	struct ath_hal_5212 *ahp;
108	struct ath_hal *ah;
109	uint32_t val;
110	HAL_STATUS ecode;
111	HAL_BOOL rfStatus;
112
113	HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
114	    __func__, sc, (void*) st, (void*) sh);
115
116	/* NB: memory is returned zero'd */
117	ahp5416 = ath_hal_malloc(sizeof (struct ath_hal_5416));
118	if (ahp5416 == AH_NULL) {
119		HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
120		    "%s: cannot allocate memory for state block\n", __func__);
121		*status = HAL_ENOMEM;
122		return AH_NULL;
123	}
124	ar5416InitState(ahp5416, devid, sc, st, sh, status);
125	ahp = &ahp5416->ah_5212;
126	ah = &ahp->ah_priv.h;
127
128	/* XXX override with 9160 specific state */
129	/* override 5416 methods for our needs */
130	ah->ah_detach			= ar9160Detach;
131
132	AH5416(ah)->ah_cal.iqCalData.calData = &ar9160_iq_cal;
133	AH5416(ah)->ah_cal.adcGainCalData.calData = &ar9160_adc_gain_cal;
134	AH5416(ah)->ah_cal.adcDcCalData.calData = &ar9160_adc_dc_cal;
135	AH5416(ah)->ah_cal.adcDcCalInitData.calData = &ar9160_adc_init_dc_cal;
136	AH5416(ah)->ah_cal.suppCals = ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
137
138	if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON)) {
139		/* reset chip */
140		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't reset chip\n",
141		    __func__);
142		ecode = HAL_EIO;
143		goto bad;
144	}
145
146	if (!ar5416SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE)) {
147		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't wakeup chip\n",
148		    __func__);
149		ecode = HAL_EIO;
150		goto bad;
151	}
152	/* Read Revisions from Chips before taking out of reset */
153	val = OS_REG_READ(ah, AR_SREV);
154	HALDEBUG(ah, HAL_DEBUG_ATTACH,
155	    "%s: ID 0x%x VERSION 0x%x TYPE 0x%x REVISION 0x%x\n",
156	    __func__, MS(val, AR_XSREV_ID), MS(val, AR_XSREV_VERSION),
157	    MS(val, AR_XSREV_TYPE), MS(val, AR_XSREV_REVISION));
158	/* NB: include chip type to differentiate from pre-Sowl versions */
159	AH_PRIVATE(ah)->ah_macVersion =
160	    (val & AR_XSREV_VERSION) >> AR_XSREV_TYPE_S;
161	AH_PRIVATE(ah)->ah_macRev = MS(val, AR_XSREV_REVISION);
162	/* XXX extract pcie info */
163
164	/* setup common ini data; rf backends handle remainder */
165	HAL_INI_INIT(&ahp->ah_ini_modes, ar9160Modes, 6);
166	HAL_INI_INIT(&ahp->ah_ini_common, ar9160Common, 2);
167
168	HAL_INI_INIT(&AH5416(ah)->ah_ini_bb_rfgain, ar9160BB_RfGain, 3);
169	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank0, ar9160Bank0, 2);
170	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank1, ar9160Bank1, 2);
171	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank2, ar9160Bank2, 2);
172	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank3, ar9160Bank3, 3);
173	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank6, ar9160Bank6, 3);
174	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank7, ar9160Bank7, 2);
175	if (AR_SREV_SOWL_11(ah))
176		HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar9160Addac_1_1, 2);
177	else
178		HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar9160Addac, 2);
179
180	if (!ar5416ChipReset(ah, AH_NULL)) {	/* reset chip */
181		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
182		ecode = HAL_EIO;
183		goto bad;
184	}
185
186	AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
187
188	if (!ar5212ChipTest(ah)) {
189		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
190		    __func__);
191		ecode = HAL_ESELFTEST;
192		goto bad;
193	}
194
195	/*
196	 * Set correct Baseband to analog shift
197	 * setting to access analog chips.
198	 */
199	OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
200
201	/* Read Radio Chip Rev Extract */
202	AH_PRIVATE(ah)->ah_analog5GhzRev = ar5212GetRadioRev(ah);
203	switch (AH_PRIVATE(ah)->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR) {
204        case AR_RAD2133_SREV_MAJOR:	/* Sowl: 2G/3x3 */
205	case AR_RAD5133_SREV_MAJOR:	/* Sowl: 2+5G/3x3 */
206		break;
207	default:
208		if (AH_PRIVATE(ah)->ah_analog5GhzRev == 0) {
209			AH_PRIVATE(ah)->ah_analog5GhzRev =
210				AR_RAD5133_SREV_MAJOR;
211			break;
212		}
213#ifdef AH_DEBUG
214		HALDEBUG(ah, HAL_DEBUG_ANY,
215		    "%s: 5G Radio Chip Rev 0x%02X is not supported by "
216		    "this driver\n", __func__,
217		    AH_PRIVATE(ah)->ah_analog5GhzRev);
218		ecode = HAL_ENOTSUPP;
219		goto bad;
220#endif
221	}
222	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: Attaching AR2133 radio\n",
223	    __func__);
224	rfStatus = ar2133RfAttach(ah, &ecode);
225	if (!rfStatus) {
226		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: RF setup failed, status %u\n",
227		    __func__, ecode);
228		goto bad;
229	}
230
231	ecode = ath_hal_v14EepromAttach(ah);
232	if (ecode != HAL_OK)
233		goto bad;
234
235	/*
236	 * Got everything we need now to setup the capabilities.
237	 */
238	if (!ar9160FillCapabilityInfo(ah)) {
239		ecode = HAL_EEREAD;
240		goto bad;
241	}
242
243	ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
244	if (ecode != HAL_OK) {
245		HALDEBUG(ah, HAL_DEBUG_ANY,
246		    "%s: error getting mac address from EEPROM\n", __func__);
247		goto bad;
248        }
249	/* XXX How about the serial number ? */
250	/* Read Reg Domain */
251	AH_PRIVATE(ah)->ah_currentRD =
252	    ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, AH_NULL);
253
254	/*
255	 * ah_miscMode is populated by ar5416FillCapabilityInfo()
256	 * starting from griffin. Set here to make sure that
257	 * AR_MISC_MODE_MIC_NEW_LOC_ENABLE is set before a GTK is
258	 * placed into hardware.
259	 */
260	if (ahp->ah_miscMode != 0)
261		OS_REG_WRITE(ah, AR_MISC_MODE, ahp->ah_miscMode);
262
263	ar9160AniSetup(ah);			/* Anti Noise Immunity */
264	ar5416InitNfHistBuff(AH5416(ah)->ah_cal.nfCalHist);
265
266	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
267
268	return ah;
269bad:
270	if (ahp)
271		ar9160Detach((struct ath_hal *) ahp);
272	if (status)
273		*status = ecode;
274	return AH_NULL;
275}
276
277void
278ar9160Detach(struct ath_hal *ah)
279{
280	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s:\n", __func__);
281
282	HALASSERT(ah != AH_NULL);
283	HALASSERT(ah->ah_magic == AR5416_MAGIC);
284
285	ar5416Detach(ah);
286}
287
288/*
289 * Fill all software cached or static hardware state information.
290 * Return failure if capabilities are to come from EEPROM and
291 * cannot be read.
292 */
293static HAL_BOOL
294ar9160FillCapabilityInfo(struct ath_hal *ah)
295{
296	HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
297
298	if (!ar5416FillCapabilityInfo(ah))
299		return AH_FALSE;
300	pCap->halCSTSupport = AH_TRUE;
301	pCap->halRifsRxSupport = AH_TRUE;
302	pCap->halRifsTxSupport = AH_TRUE;
303	pCap->halRtsAggrLimit = 64*1024;	/* 802.11n max */
304	pCap->halExtChanDfsSupport = AH_TRUE;
305	pCap->halAutoSleepSupport = AH_FALSE;	/* XXX? */
306	return AH_TRUE;
307}
308#endif /* AH_SUPPORT_AR9160 */
309