xref: /freebsd/sys/dev/ath/ath_hal/ar5312/ar5312_attach.c (revision ccb59683b98360afaf5b5bb641a68fea22c68d0b)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5  * Copyright (c) 2002-2008 Atheros Communications, Inc.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * $FreeBSD$
20  */
21 #include "opt_ah.h"
22 
23 #include "ah.h"
24 #include "ah_internal.h"
25 #include "ah_devid.h"
26 
27 #include "ar5312/ar5312.h"
28 #include "ar5312/ar5312reg.h"
29 #include "ar5312/ar5312phy.h"
30 
31 /* Add static register initialization vectors */
32 #define AH_5212_COMMON
33 #include "ar5212/ar5212.ini"
34 
35 static  HAL_BOOL ar5312GetMacAddr(struct ath_hal *ah);
36 
37 static void
38 ar5312AniSetup(struct ath_hal *ah)
39 {
40 	static const struct ar5212AniParams aniparams = {
41 		.maxNoiseImmunityLevel	= 4,	/* levels 0..4 */
42 		.totalSizeDesired	= { -41, -41, -48, -48, -48 },
43 		.coarseHigh		= { -18, -18, -16, -14, -12 },
44 		.coarseLow		= { -56, -56, -60, -60, -60 },
45 		.firpwr			= { -72, -72, -75, -78, -80 },
46 		.maxSpurImmunityLevel	= 2,
47 		.cycPwrThr1		= { 2, 4, 6 },
48 		.maxFirstepLevel	= 2,	/* levels 0..2 */
49 		.firstep		= { 0, 4, 8 },
50 		.ofdmTrigHigh		= 500,
51 		.ofdmTrigLow		= 200,
52 		.cckTrigHigh		= 200,
53 		.cckTrigLow		= 100,
54 		.rssiThrHigh		= 40,
55 		.rssiThrLow		= 7,
56 		.period			= 100,
57 	};
58 	ar5212AniAttach(ah, &aniparams, &aniparams, AH_TRUE);
59 }
60 
61 /*
62  * Attach for an AR5312 part.
63  */
64 static struct ath_hal *
65 ar5312Attach(uint16_t devid, HAL_SOFTC sc,
66 	HAL_BUS_TAG st, HAL_BUS_HANDLE sh, uint16_t *eepromdata,
67 	HAL_OPS_CONFIG *ah_config, HAL_STATUS *status)
68 {
69 	struct ath_hal_5212 *ahp = AH_NULL;
70 	struct ath_hal *ah;
71 	struct ath_hal_rf *rf;
72 	uint32_t val;
73 	uint16_t eeval;
74 	HAL_STATUS ecode;
75 
76 	HALDEBUG(AH_NULL, HAL_DEBUG_ATTACH, "%s: sc %p st %p sh %p\n",
77 		 __func__, sc, st, (void*) sh);
78 
79 	/* NB: memory is returned zero'd */
80 	ahp = ath_hal_malloc(sizeof (struct ath_hal_5212));
81 	if (ahp == AH_NULL) {
82 		HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
83 		    "%s: cannot allocate memory for state block\n", __func__);
84 		*status = HAL_ENOMEM;
85 		return AH_NULL;
86 	}
87 	ar5212InitState(ahp, devid, sc, st, sh, status);
88 	ah = &ahp->ah_priv.h;
89 
90 	/* override 5212 methods for our needs */
91 	ah->ah_reset			= ar5312Reset;
92 	ah->ah_phyDisable		= ar5312PhyDisable;
93 	ah->ah_setLedState		= ar5312SetLedState;
94 	ah->ah_detectCardPresent	= ar5312DetectCardPresent;
95 	ah->ah_setPowerMode		= ar5312SetPowerMode;
96 	ah->ah_getPowerMode		= ar5312GetPowerMode;
97 	ah->ah_isInterruptPending	= ar5312IsInterruptPending;
98 
99 	ahp->ah_priv.ah_eepromRead	= ar5312EepromRead;
100 #ifdef AH_SUPPORT_WRITE_EEPROM
101 	ahp->ah_priv.ah_eepromWrite	= ar5312EepromWrite;
102 #endif
103 #if ( AH_SUPPORT_2316 || AH_SUPPORT_2317)
104 	if (IS_5315(ah)) {
105 		ahp->ah_priv.ah_gpioCfgOutput	= ar5315GpioCfgOutput;
106 		ahp->ah_priv.ah_gpioCfgInput	= ar5315GpioCfgInput;
107 		ahp->ah_priv.ah_gpioGet		= ar5315GpioGet;
108 		ahp->ah_priv.ah_gpioSet		= ar5315GpioSet;
109 		ahp->ah_priv.ah_gpioSetIntr	= ar5315GpioSetIntr;
110 	} else
111 #endif
112 	{
113 		ahp->ah_priv.ah_gpioCfgOutput	= ar5312GpioCfgOutput;
114 		ahp->ah_priv.ah_gpioCfgInput	= ar5312GpioCfgInput;
115 		ahp->ah_priv.ah_gpioGet		= ar5312GpioGet;
116 		ahp->ah_priv.ah_gpioSet		= ar5312GpioSet;
117 		ahp->ah_priv.ah_gpioSetIntr	= ar5312GpioSetIntr;
118 	}
119 
120 	ah->ah_gpioCfgInput		= ahp->ah_priv.ah_gpioCfgInput;
121 	ah->ah_gpioCfgOutput		= ahp->ah_priv.ah_gpioCfgOutput;
122 	ah->ah_gpioGet			= ahp->ah_priv.ah_gpioGet;
123 	ah->ah_gpioSet			= ahp->ah_priv.ah_gpioSet;
124 	ah->ah_gpioSetIntr		= ahp->ah_priv.ah_gpioSetIntr;
125 
126 	/* setup common ini data; rf backends handle remainder */
127 	HAL_INI_INIT(&ahp->ah_ini_modes, ar5212Modes, 6);
128 	HAL_INI_INIT(&ahp->ah_ini_common, ar5212Common, 2);
129 
130 	if (!ar5312ChipReset(ah, AH_NULL)) {	/* reset chip */
131 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: chip reset failed\n", __func__);
132 		ecode = HAL_EIO;
133 		goto bad;
134 	}
135 
136 #if ( AH_SUPPORT_2316 || AH_SUPPORT_2317)
137 	if ((devid == AR5212_AR2315_REV6) ||
138 	    (devid == AR5212_AR2315_REV7) ||
139 	    (devid == AR5212_AR2317_REV1) ||
140 	    (devid == AR5212_AR2317_REV2) ) {
141 		val = ((OS_REG_READ(ah, (AR5315_RSTIMER_BASE -((uint32_t) sh)) + AR5315_WREV)) >> AR5315_WREV_S)
142 			& AR5315_WREV_ID;
143 		AH_PRIVATE(ah)->ah_macVersion = val >> AR5315_WREV_ID_S;
144 		AH_PRIVATE(ah)->ah_macRev = val & AR5315_WREV_REVISION;
145 		HALDEBUG(ah, HAL_DEBUG_ATTACH,
146 		    "%s: Mac Chip Rev 0x%02x.%x\n" , __func__,
147 		    AH_PRIVATE(ah)->ah_macVersion, AH_PRIVATE(ah)->ah_macRev);
148 	} else
149 #endif
150 	{
151 		val = OS_REG_READ(ah, (AR5312_RSTIMER_BASE - ((uint32_t) sh)) + 0x0020);
152 		val = OS_REG_READ(ah, (AR5312_RSTIMER_BASE - ((uint32_t) sh)) + 0x0080);
153 		/* Read Revisions from Chips */
154 		val = ((OS_REG_READ(ah, (AR5312_RSTIMER_BASE - ((uint32_t) sh)) + AR5312_WREV)) >> AR5312_WREV_S) & AR5312_WREV_ID;
155 		AH_PRIVATE(ah)->ah_macVersion = val >> AR5312_WREV_ID_S;
156 		AH_PRIVATE(ah)->ah_macRev = val & AR5312_WREV_REVISION;
157 	}
158 	/* XXX - THIS IS WRONG. NEEDS TO BE FIXED */
159 	if (((AH_PRIVATE(ah)->ah_macVersion != AR_SREV_VERSION_VENICE &&
160               AH_PRIVATE(ah)->ah_macVersion != AR_SREV_VERSION_VENICE) ||
161              AH_PRIVATE(ah)->ah_macRev < AR_SREV_D2PLUS) &&
162               AH_PRIVATE(ah)->ah_macVersion != AR_SREV_VERSION_COBRA) {
163 #ifdef AH_DEBUG
164 		ath_hal_printf(ah, "%s: Mac Chip Rev 0x%02x.%x is not supported by "
165                          "this driver\n", __func__,
166                          AH_PRIVATE(ah)->ah_macVersion,
167                          AH_PRIVATE(ah)->ah_macRev);
168 #endif
169 		ecode = HAL_ENOTSUPP;
170 		goto bad;
171 	}
172 
173 	AH_PRIVATE(ah)->ah_phyRev = OS_REG_READ(ah, AR_PHY_CHIP_ID);
174 
175 	if (!ar5212ChipTest(ah)) {
176 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: hardware self-test failed\n",
177 		    __func__);
178 		ecode = HAL_ESELFTEST;
179 		goto bad;
180 	}
181 
182 	/*
183 	 * Set correct Baseband to analog shift
184 	 * setting to access analog chips.
185 	 */
186 	OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
187 
188 	/* Read Radio Chip Rev Extract */
189 	AH_PRIVATE(ah)->ah_analog5GhzRev = ar5212GetRadioRev(ah);
190 
191 	rf = ath_hal_rfprobe(ah, &ecode);
192 	if (rf == AH_NULL)
193 		goto bad;
194 	if (IS_RAD5112(ah) && !IS_RADX112_REV2(ah)) {
195 #ifdef AH_DEBUG
196 		ath_hal_printf(ah, "%s: 5112 Rev 1 is not supported by this "
197                          "driver (analog5GhzRev 0x%x)\n", __func__,
198                          AH_PRIVATE(ah)->ah_analog5GhzRev);
199 #endif
200 		ecode = HAL_ENOTSUPP;
201 		goto bad;
202 	}
203 
204 	ecode = ath_hal_legacyEepromAttach(ah);
205 	if (ecode != HAL_OK) {
206 		goto bad;
207 	}
208 
209 	/*
210 	 * If Bmode and AR5212, verify 2.4 analog exists
211 	 */
212 	if (ath_hal_eepromGetFlag(ah, AR_EEP_BMODE) &&
213 	    (AH_PRIVATE(ah)->ah_analog5GhzRev & 0xF0) == AR_RAD5111_SREV_MAJOR) {
214 		/*
215 		 * Set correct Baseband to analog shift
216 		 * setting to access analog chips.
217 		 */
218 		OS_REG_WRITE(ah, AR_PHY(0), 0x00004007);
219 		OS_DELAY(2000);
220 		AH_PRIVATE(ah)->ah_analog2GhzRev = ar5212GetRadioRev(ah);
221 
222 		/* Set baseband for 5GHz chip */
223 		OS_REG_WRITE(ah, AR_PHY(0), 0x00000007);
224 		OS_DELAY(2000);
225 		if ((AH_PRIVATE(ah)->ah_analog2GhzRev & 0xF0) != AR_RAD2111_SREV_MAJOR) {
226 #ifdef AH_DEBUG
227 			ath_hal_printf(ah, "%s: 2G Radio Chip Rev 0x%02X is not "
228 				"supported by this driver\n", __func__,
229 				AH_PRIVATE(ah)->ah_analog2GhzRev);
230 #endif
231 			ecode = HAL_ENOTSUPP;
232 			goto bad;
233 		}
234 	}
235 
236 	ecode = ath_hal_eepromGet(ah, AR_EEP_REGDMN_0, &eeval);
237 	if (ecode != HAL_OK) {
238 		HALDEBUG(ah, HAL_DEBUG_ANY,
239 		    "%s: cannot read regulatory domain from EEPROM\n",
240 		    __func__);
241 		goto bad;
242         }
243 	AH_PRIVATE(ah)->ah_currentRD = eeval;
244 	/* XXX record serial number */
245 
246 	/* XXX other capabilities */
247 	/*
248 	 * Got everything we need now to setup the capabilities.
249 	 */
250 	if (!ar5212FillCapabilityInfo(ah)) {
251 		HALDEBUG(ah, HAL_DEBUG_ANY,
252 		    "%s: failed ar5212FillCapabilityInfo\n", __func__);
253 		ecode = HAL_EEREAD;
254 		goto bad;
255 	}
256 
257 	if (!rf->attach(ah, &ecode)) {
258 		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: RF setup failed, status %u\n",
259 		    __func__, ecode);
260 		goto bad;
261 	}
262 	/* arrange a direct call instead of thunking */
263 	AH_PRIVATE(ah)->ah_getNfAdjust = ahp->ah_rfHal->getNfAdjust;
264 
265 	/* Initialize gain ladder thermal calibration structure */
266 	ar5212InitializeGainValues(ah);
267 
268         /* BSP specific call for MAC address of this WMAC device */
269         if (!ar5312GetMacAddr(ah)) {
270                 ecode = HAL_EEBADMAC;
271                 goto bad;
272         }
273 
274 	ar5312AniSetup(ah);
275 	ar5212InitNfCalHistBuffer(ah);
276 
277 	/* XXX EAR stuff goes here */
278 	return ah;
279 
280 bad:
281 	if (ahp)
282 		ar5212Detach((struct ath_hal *) ahp);
283 	if (status)
284 		*status = ecode;
285 	return AH_NULL;
286 }
287 
288 static HAL_BOOL
289 ar5312GetMacAddr(struct ath_hal *ah)
290 {
291 	const struct ar531x_boarddata *board = AR5312_BOARDCONFIG(ah);
292         int wlanNum = AR5312_UNIT(ah);
293         const uint8_t *macAddr;
294 
295 	switch (wlanNum) {
296 	case 0:
297 		macAddr = board->wlan0Mac;
298 		break;
299 	case 1:
300 		macAddr = board->wlan1Mac;
301 		break;
302 	default:
303 #ifdef AH_DEBUG
304 		ath_hal_printf(ah, "Invalid WLAN wmac index (%d)\n",
305 			       wlanNum);
306 #endif
307 		return AH_FALSE;
308 	}
309 	OS_MEMCPY(AH5212(ah)->ah_macaddr, macAddr, 6);
310 	return AH_TRUE;
311 }
312 
313 static const char*
314 ar5312Probe(uint16_t vendorid, uint16_t devid)
315 {
316 	if (vendorid == ATHEROS_VENDOR_ID) {
317 		switch (devid) {
318 		case AR5212_AR5312_REV2:
319 		case AR5212_AR5312_REV7:
320 			return "Atheros 5312 WiSoC";
321 		case AR5212_AR2313_REV8:
322 			return "Atheros 2313 WiSoC";
323 		case AR5212_AR2315_REV6:
324 		case AR5212_AR2315_REV7:
325 			return "Atheros 2315 WiSoC";
326 		case AR5212_AR2317_REV1:
327 		case AR5212_AR2317_REV2:
328 			return "Atheros 2317 WiSoC";
329 		case AR5212_AR2413:
330 			return "Atheros 2413";
331 		case AR5212_AR2417:
332 			return "Atheros 2417";
333 		}
334 	}
335 	return AH_NULL;
336 }
337 AH_CHIP(AR5312, ar5312Probe, ar5312Attach);
338