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  * $FreeBSD$
18  */
19 #include "opt_ah.h"
20 
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_devid.h"
24 
25 #include "ar5416/ar5416.h"
26 #include "ar5416/ar5416reg.h"
27 #include "ar5416/ar5416phy.h"
28 
29 #include "ar9001/ar9160.ini"
30 
31 static const HAL_PERCAL_DATA ar9160_iq_cal = {		/* multi sample */
32 	.calName = "IQ", .calType = IQ_MISMATCH_CAL,
33 	.calNumSamples	= MAX_CAL_SAMPLES,
34 	.calCountMax	= PER_MIN_LOG_COUNT,
35 	.calCollect	= ar5416IQCalCollect,
36 	.calPostProc	= ar5416IQCalibration
37 };
38 static const HAL_PERCAL_DATA ar9160_adc_gain_cal = {	/* multi sample */
39 	.calName = "ADC Gain", .calType = ADC_GAIN_CAL,
40 	.calNumSamples	= MAX_CAL_SAMPLES,
41 	.calCountMax	= PER_MIN_LOG_COUNT,
42 	.calCollect	= ar5416AdcGainCalCollect,
43 	.calPostProc	= ar5416AdcGainCalibration
44 };
45 static const HAL_PERCAL_DATA ar9160_adc_dc_cal = {	/* multi sample */
46 	.calName = "ADC DC", .calType = ADC_DC_CAL,
47 	.calNumSamples	= MAX_CAL_SAMPLES,
48 	.calCountMax	= PER_MIN_LOG_COUNT,
49 	.calCollect	= ar5416AdcDcCalCollect,
50 	.calPostProc	= ar5416AdcDcCalibration
51 };
52 static const HAL_PERCAL_DATA ar9160_adc_init_dc_cal = {
53 	.calName = "ADC Init DC", .calType = ADC_DC_INIT_CAL,
54 	.calNumSamples	= MIN_CAL_SAMPLES,
55 	.calCountMax	= INIT_LOG_COUNT,
56 	.calCollect	= ar5416AdcDcCalCollect,
57 	.calPostProc	= ar5416AdcDcCalibration
58 };
59 
60 static HAL_BOOL ar9160FillCapabilityInfo(struct ath_hal *ah);
61 
62 static void
63 ar9160AniSetup(struct ath_hal *ah)
64 {
65 	static const struct ar5212AniParams aniparams = {
66 		.maxNoiseImmunityLevel	= 4,	/* levels 0..4 */
67 		.totalSizeDesired	= { -55, -55, -55, -55, -62 },
68 		.coarseHigh		= { -14, -14, -14, -14, -12 },
69 		.coarseLow		= { -64, -64, -64, -64, -70 },
70 		.firpwr			= { -78, -78, -78, -78, -80 },
71 		.maxSpurImmunityLevel	= 7,
72 		.cycPwrThr1		= { 2, 4, 6, 8, 10, 12, 14, 16 },
73 		.maxFirstepLevel	= 2,	/* levels 0..2 */
74 		.firstep		= { 0, 4, 8 },
75 		.ofdmTrigHigh		= 500,
76 		.ofdmTrigLow		= 200,
77 		.cckTrigHigh		= 200,
78 		.cckTrigLow		= 100,
79 		.rssiThrHigh		= 40,
80 		.rssiThrLow		= 7,
81 		.period			= 100,
82 	};
83 
84 	/* NB: disable ANI noise immmunity for reliable RIFS rx */
85 	AH5416(ah)->ah_ani_function &= ~(1 << HAL_ANI_NOISE_IMMUNITY_LEVEL);
86 	ar5416AniAttach(ah, &aniparams, &aniparams, AH_TRUE);
87 }
88 
89 static void
90 ar9160InitPLL(struct ath_hal *ah, const struct ieee80211_channel *chan)
91 {
92 	uint32_t pll = SM(0x5, AR_RTC_SOWL_PLL_REFDIV);
93 	if (chan != AH_NULL) {
94 		if (IEEE80211_IS_CHAN_HALF(chan))
95 			pll |= SM(0x1, AR_RTC_SOWL_PLL_CLKSEL);
96 		else if (IEEE80211_IS_CHAN_QUARTER(chan))
97 			pll |= SM(0x2, AR_RTC_SOWL_PLL_CLKSEL);
98 
99 		if (IEEE80211_IS_CHAN_5GHZ(chan))
100 			pll |= SM(0x50, AR_RTC_SOWL_PLL_DIV);
101 		else
102 			pll |= SM(0x58, AR_RTC_SOWL_PLL_DIV);
103 	} else
104 		pll |= SM(0x58, AR_RTC_SOWL_PLL_DIV);
105 
106 	OS_REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
107 	OS_DELAY(RTC_PLL_SETTLE_DELAY);
108 	OS_REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_SLEEP_DERIVED_CLK);
109 }
110 
111 /*
112  * Attach for an AR9160 part.
113  */
114 static struct ath_hal *
115 ar9160Attach(uint16_t devid, HAL_SOFTC sc,
116 	HAL_BUS_TAG st, HAL_BUS_HANDLE sh, uint16_t *eepromdata,
117 	HAL_OPS_CONFIG *ah_config,
118 	HAL_STATUS *status)
119 {
120 	struct ath_hal_5416 *ahp5416;
121 	struct ath_hal_5212 *ahp;
122 	struct ath_hal *ah;
123 	uint32_t val;
124 	HAL_STATUS ecode;
125 	HAL_BOOL rfStatus;
126 
127 	HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
128 	    __func__, sc, (void*) st, (void*) sh);
129 
130 	/* NB: memory is returned zero'd */
131 	ahp5416 = ath_hal_malloc(sizeof (struct ath_hal_5416));
132 	if (ahp5416 == AH_NULL) {
133 		HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
134 		    "%s: cannot allocate memory for state block\n", __func__);
135 		*status = HAL_ENOMEM;
136 		return AH_NULL;
137 	}
138 	ar5416InitState(ahp5416, devid, sc, st, sh, status);
139 	ahp = &ahp5416->ah_5212;
140 	ah = &ahp->ah_priv.h;
141 
142 	/* XXX override with 9160 specific state */
143 	/* override 5416 methods for our needs */
144 	AH5416(ah)->ah_initPLL = ar9160InitPLL;
145 
146 	AH5416(ah)->ah_cal.iqCalData.calData = &ar9160_iq_cal;
147 	AH5416(ah)->ah_cal.adcGainCalData.calData = &ar9160_adc_gain_cal;
148 	AH5416(ah)->ah_cal.adcDcCalData.calData = &ar9160_adc_dc_cal;
149 	AH5416(ah)->ah_cal.adcDcCalInitData.calData = &ar9160_adc_init_dc_cal;
150 	AH5416(ah)->ah_cal.suppCals = ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
151 
152 	if (!ar5416SetResetReg(ah, HAL_RESET_POWER_ON)) {
153 		/* reset chip */
154 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't reset chip\n",
155 		    __func__);
156 		ecode = HAL_EIO;
157 		goto bad;
158 	}
159 
160 	if (!ar5416SetPowerMode(ah, HAL_PM_AWAKE, AH_TRUE)) {
161 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: couldn't wakeup chip\n",
162 		    __func__);
163 		ecode = HAL_EIO;
164 		goto bad;
165 	}
166 	/* Read Revisions from Chips before taking out of reset */
167 	val = OS_REG_READ(ah, AR_SREV);
168 	HALDEBUG(ah, HAL_DEBUG_ATTACH,
169 	    "%s: ID 0x%x VERSION 0x%x TYPE 0x%x REVISION 0x%x\n",
170 	    __func__, MS(val, AR_XSREV_ID), MS(val, AR_XSREV_VERSION),
171 	    MS(val, AR_XSREV_TYPE), MS(val, AR_XSREV_REVISION));
172 	/* NB: include chip type to differentiate from pre-Sowl versions */
173 	AH_PRIVATE(ah)->ah_macVersion =
174 	    (val & AR_XSREV_VERSION) >> AR_XSREV_TYPE_S;
175 	AH_PRIVATE(ah)->ah_macRev = MS(val, AR_XSREV_REVISION);
176 	AH_PRIVATE(ah)->ah_ispcie = (val & AR_XSREV_TYPE_HOST_MODE) == 0;
177 
178 	/* setup common ini data; rf backends handle remainder */
179 	HAL_INI_INIT(&ahp->ah_ini_modes, ar9160Modes, 6);
180 	HAL_INI_INIT(&ahp->ah_ini_common, ar9160Common, 2);
181 
182 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bb_rfgain, ar9160BB_RfGain, 3);
183 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank0, ar9160Bank0, 2);
184 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank1, ar9160Bank1, 2);
185 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank2, ar9160Bank2, 2);
186 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank3, ar9160Bank3, 3);
187 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank6, ar9160Bank6TPC, 3);
188 	HAL_INI_INIT(&AH5416(ah)->ah_ini_bank7, ar9160Bank7, 2);
189 	if (AR_SREV_SOWL_11(ah))
190 		HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar9160Addac_1_1, 2);
191 	else
192 		HAL_INI_INIT(&AH5416(ah)->ah_ini_addac, ar9160Addac, 2);
193 
194 	ecode = ath_hal_v14EepromAttach(ah);
195 	if (ecode != HAL_OK)
196 		goto bad;
197 
198 	HAL_INI_INIT(&AH5416(ah)->ah_ini_pcieserdes, ar9160PciePhy, 2);
199 	ar5416AttachPCIE(ah);
200 
201 	if (!ar5416ChipReset(ah, AH_NULL)) {	/* reset chip */
202 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
203 		ecode = HAL_EIO;
204 		goto bad;
205 	}
206 
207 	AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
208 
209 	if (!ar5212ChipTest(ah)) {
210 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
211 		    __func__);
212 		ecode = HAL_ESELFTEST;
213 		goto bad;
214 	}
215 
216 	/*
217 	 * Set correct Baseband to analog shift
218 	 * setting to access analog chips.
219 	 */
220 	OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
221 
222 	/* Read Radio Chip Rev Extract */
223 	AH_PRIVATE(ah)->ah_analog5GhzRev = ar5416GetRadioRev(ah);
224 	switch (AH_PRIVATE(ah)->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR) {
225         case AR_RAD2133_SREV_MAJOR:	/* Sowl: 2G/3x3 */
226 	case AR_RAD5133_SREV_MAJOR:	/* Sowl: 2+5G/3x3 */
227 		break;
228 	default:
229 		if (AH_PRIVATE(ah)->ah_analog5GhzRev == 0) {
230 			AH_PRIVATE(ah)->ah_analog5GhzRev =
231 				AR_RAD5133_SREV_MAJOR;
232 			break;
233 		}
234 #ifdef AH_DEBUG
235 		HALDEBUG(ah, HAL_DEBUG_ANY,
236 		    "%s: 5G Radio Chip Rev 0x%02X is not supported by "
237 		    "this driver\n", __func__,
238 		    AH_PRIVATE(ah)->ah_analog5GhzRev);
239 		ecode = HAL_ENOTSUPP;
240 		goto bad;
241 #endif
242 	}
243 	rfStatus = ar2133RfAttach(ah, &ecode);
244 	if (!rfStatus) {
245 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: RF setup failed, status %u\n",
246 		    __func__, ecode);
247 		goto bad;
248 	}
249 
250 	/*
251 	 * Got everything we need now to setup the capabilities.
252 	 */
253 	if (!ar9160FillCapabilityInfo(ah)) {
254 		ecode = HAL_EEREAD;
255 		goto bad;
256 	}
257 
258 	ecode = ath_hal_eepromGet(ah, AR_EEP_MACADDR, ahp->ah_macaddr);
259 	if (ecode != HAL_OK) {
260 		HALDEBUG(ah, HAL_DEBUG_ANY,
261 		    "%s: error getting mac address from EEPROM\n", __func__);
262 		goto bad;
263         }
264 	/* XXX How about the serial number ? */
265 	/* Read Reg Domain */
266 	AH_PRIVATE(ah)->ah_currentRD =
267 	    ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, AH_NULL);
268 	AH_PRIVATE(ah)->ah_currentRDext =
269 	    ath_hal_eepromGet(ah, AR_EEP_REGDMN_1, AH_NULL);
270 
271 	/*
272 	 * ah_miscMode is populated by ar5416FillCapabilityInfo()
273 	 * starting from griffin. Set here to make sure that
274 	 * AR_MISC_MODE_MIC_NEW_LOC_ENABLE is set before a GTK is
275 	 * placed into hardware.
276 	 */
277 	if (ahp->ah_miscMode != 0)
278 		OS_REG_WRITE(ah, AR_MISC_MODE, OS_REG_READ(ah, AR_MISC_MODE) | ahp->ah_miscMode);
279 
280 	ar9160AniSetup(ah);			/* Anti Noise Immunity */
281 
282 	/* This just uses the AR5416 NF values */
283 	AH5416(ah)->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_2GHZ;
284 	AH5416(ah)->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_2GHZ;
285 	AH5416(ah)->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_5416_2GHZ;
286 	AH5416(ah)->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_5GHZ;
287 	AH5416(ah)->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_5GHZ;
288 	AH5416(ah)->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_5416_5GHZ;
289 
290 	ar5416InitNfHistBuff(AH5416(ah)->ah_cal.nfCalHist);
291 
292 	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: return\n", __func__);
293 
294 	return ah;
295 bad:
296 	if (ahp)
297 		ar5416Detach((struct ath_hal *) ahp);
298 	if (status)
299 		*status = ecode;
300 	return AH_NULL;
301 }
302 
303 /*
304  * Fill all software cached or static hardware state information.
305  * Return failure if capabilities are to come from EEPROM and
306  * cannot be read.
307  */
308 static HAL_BOOL
309 ar9160FillCapabilityInfo(struct ath_hal *ah)
310 {
311 	HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
312 
313 	if (!ar5416FillCapabilityInfo(ah))
314 		return AH_FALSE;
315 	pCap->halCSTSupport = AH_TRUE;
316 	pCap->halRifsRxSupport = AH_TRUE;
317 	pCap->halRifsTxSupport = AH_TRUE;
318 	pCap->halRtsAggrLimit = 64*1024;	/* 802.11n max */
319 	pCap->halExtChanDfsSupport = AH_TRUE;
320 	pCap->halUseCombinedRadarRssi = AH_TRUE;
321 	pCap->halAutoSleepSupport = AH_FALSE;	/* XXX? */
322 	pCap->halMbssidAggrSupport = AH_TRUE;
323 	pCap->hal4AddrAggrSupport = AH_TRUE;
324 	/* BB Read WAR */
325 	pCap->halHasBBReadWar = AH_TRUE;
326 
327 	/* AR9160 is a 2x2 stream device */
328 	pCap->halTxStreams = 2;
329 	pCap->halRxStreams = 2;
330 
331 	return AH_TRUE;
332 }
333 
334 static const char*
335 ar9160Probe(uint16_t vendorid, uint16_t devid)
336 {
337 	if (vendorid == ATHEROS_VENDOR_ID && devid == AR9160_DEVID_PCI)
338 		return "Atheros 9160";
339 	return AH_NULL;
340 }
341 AH_CHIP(AR9160, ar9160Probe, ar9160Attach);
342