xref: /freebsd/sys/dev/ath/ath_hal/ah_eeprom_9287.c (revision 81ad6265)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2008 Sam Leffler, Errno Consulting
5  * Copyright (c) 2010 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_eeprom_v14.h"
26 #include "ah_eeprom_9287.h"
27 
28 static HAL_STATUS
29 v9287EepromGet(struct ath_hal *ah, int param, void *val)
30 {
31 #define	CHAN_A_IDX	0
32 #define	CHAN_B_IDX	1
33 #define	IS_VERS(op, v)	((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v))
34 	HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
35 	const MODAL_EEP_9287_HEADER *pModal = &ee->ee_base.modalHeader;
36 	const BASE_EEP_9287_HEADER *pBase = &ee->ee_base.baseEepHeader;
37 	uint32_t sum;
38 	uint8_t *macaddr;
39 	int i;
40 
41 	switch (param) {
42         case AR_EEP_NFTHRESH_2:
43 		*(int16_t *)val = pModal->noiseFloorThreshCh[0];
44 		return HAL_OK;
45         case AR_EEP_MACADDR:		/* Get MAC Address */
46 		sum = 0;
47 		macaddr = val;
48 		for (i = 0; i < 6; i++) {
49 			macaddr[i] = pBase->macAddr[i];
50 			sum += pBase->macAddr[i];
51 		}
52 		if (sum == 0 || sum == 0xffff*3) {
53 			HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
54 			    __func__, ath_hal_ether_sprintf(macaddr));
55 			return HAL_EEBADMAC;
56 		}
57 		return HAL_OK;
58         case AR_EEP_REGDMN_0:
59 		return pBase->regDmn[0];
60         case AR_EEP_REGDMN_1:
61 		return pBase->regDmn[1];
62         case AR_EEP_OPCAP:
63 		return pBase->deviceCap;
64         case AR_EEP_OPMODE:
65 		return pBase->opCapFlags;
66         case AR_EEP_RFSILENT:
67 		return pBase->rfSilent;
68 	case AR_EEP_TXMASK:
69 		return pBase->txMask;
70 	case AR_EEP_RXMASK:
71 		return pBase->rxMask;
72 	case AR_EEP_OL_PWRCTRL:
73 		HALASSERT(val == AH_NULL);
74 		return pBase->openLoopPwrCntl ?  HAL_OK : HAL_EIO;
75 	case AR_EEP_AMODE:
76 		return HAL_EIO;		/* no 5GHz for Kiwi */
77 	case AR_EEP_BMODE:
78 	case AR_EEP_GMODE:
79 		HALASSERT(val == AH_NULL);
80 		return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
81 		    HAL_OK : HAL_EIO;
82 	case AR_EEP_32KHZCRYSTAL:
83 	case AR_EEP_COMPRESS:
84 	case AR_EEP_FASTFRAME:		/* XXX policy decision, h/w can do it */
85 	case AR_EEP_WRITEPROTECT:	/* NB: no write protect bit */
86 		HALASSERT(val == AH_NULL);
87 		/* fall thru... */
88 	case AR_EEP_MAXQCU:		/* NB: not in opCapFlags */
89 	case AR_EEP_KCENTRIES:		/* NB: not in opCapFlags */
90 		return HAL_EIO;
91 	case AR_EEP_AES:
92 	case AR_EEP_BURST:
93         case AR_EEP_RFKILL:
94 	case AR_EEP_TURBO5DISABLE:
95 	case AR_EEP_TURBO2DISABLE:
96 		HALASSERT(val == AH_NULL);
97 		return HAL_OK;
98 	case AR_EEP_ANTGAINMAX_2:
99 		*(int8_t *) val = ee->ee_antennaGainMax[1];
100 		return HAL_OK;
101 	case AR_EEP_PWR_TABLE_OFFSET:
102 		*(int8_t *) val = pBase->pwrTableOffset;
103 		return HAL_OK;
104 	case AR_EEP_TEMPSENSE_SLOPE:
105 		if (IS_VERS(>=,  AR9287_EEP_MINOR_VER_2))
106 			*(int8_t *)val = pBase->tempSensSlope;
107 		else
108 			*(int8_t *)val = 0;
109 		return HAL_OK;
110 	case AR_EEP_TEMPSENSE_SLOPE_PAL_ON:
111 		if (IS_VERS(>=,  AR9287_EEP_MINOR_VER_3))
112 			*(int8_t *)val = pBase->tempSensSlopePalOn;
113 		else
114 			*(int8_t *)val = 0;
115 		return HAL_OK;
116         default:
117 		HALASSERT(0);
118 		return HAL_EINVAL;
119 	}
120 #undef IS_VERS
121 #undef CHAN_A_IDX
122 #undef CHAN_B_IDX
123 }
124 
125 static HAL_STATUS
126 v9287EepromSet(struct ath_hal *ah, int param, int v)
127 {
128 	HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
129 
130 	switch (param) {
131 		case AR_EEP_ANTGAINMAX_2:
132 			ee->ee_antennaGainMax[1] = (int8_t) v;
133 			return HAL_OK;
134 		default:
135 			return HAL_EINVAL;
136 	}
137 }
138 
139 static HAL_BOOL
140 v9287EepromDiag(struct ath_hal *ah, int request,
141      const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
142 {
143 	HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
144 
145 	switch (request) {
146 	case HAL_DIAG_EEPROM:
147 		*result = ee;
148 		*resultsize = sizeof(HAL_EEPROM_9287);
149 		return AH_TRUE;
150 	}
151 	return AH_FALSE;
152 }
153 
154 /* Do structure specific swaps if Eeprom format is non native to host */
155 static void
156 eepromSwap(HAL_EEPROM_9287 *ee)
157 {
158 	uint32_t integer, i;
159 	uint16_t word;
160 	MODAL_EEP_9287_HEADER *pModal;
161 
162 	/* convert Base Eep header */
163 	word = __bswap16(ee->ee_base.baseEepHeader.length);
164 	ee->ee_base.baseEepHeader.length = word;
165 
166 	word = __bswap16(ee->ee_base.baseEepHeader.checksum);
167 	ee->ee_base.baseEepHeader.checksum = word;
168 
169 	word = __bswap16(ee->ee_base.baseEepHeader.version);
170 	ee->ee_base.baseEepHeader.version = word;
171 
172 	word = __bswap16(ee->ee_base.baseEepHeader.regDmn[0]);
173 	ee->ee_base.baseEepHeader.regDmn[0] = word;
174 
175 	word = __bswap16(ee->ee_base.baseEepHeader.regDmn[1]);
176 	ee->ee_base.baseEepHeader.regDmn[1] = word;
177 
178 	word = __bswap16(ee->ee_base.baseEepHeader.rfSilent);
179 	ee->ee_base.baseEepHeader.rfSilent = word;
180 
181 	word = __bswap16(ee->ee_base.baseEepHeader.blueToothOptions);
182 	ee->ee_base.baseEepHeader.blueToothOptions = word;
183 
184 	word = __bswap16(ee->ee_base.baseEepHeader.deviceCap);
185 	ee->ee_base.baseEepHeader.deviceCap = word;
186 
187 	/* convert Modal Eep header */
188 
189 	/* only 2.4ghz here; so only one modal header entry */
190 	pModal = &ee->ee_base.modalHeader;
191 
192 	/* XXX linux/ah_osdep.h only defines __bswap32 for BE */
193 	integer = __bswap32(pModal->antCtrlCommon);
194 	pModal->antCtrlCommon = integer;
195 
196 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
197 		integer = __bswap32(pModal->antCtrlChain[i]);
198 		pModal->antCtrlChain[i] = integer;
199 	}
200 	for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
201 		word = __bswap16(pModal->spurChans[i].spurChan);
202 		pModal->spurChans[i].spurChan = word;
203 	}
204 }
205 
206 static uint16_t
207 v9287EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
208 {
209 	HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
210 
211 	HALASSERT(is2GHz == AH_TRUE);
212 	if (is2GHz != AH_TRUE)
213 		return 0;	/* XXX ? */
214 
215 	HALASSERT(0 <= ix && ix <  AR5416_EEPROM_MODAL_SPURS);
216 	return ee->ee_base.modalHeader.spurChans[ix].spurChan;
217 }
218 
219 /**************************************************************************
220  * fbin2freq
221  *
222  * Get channel value from binary representation held in eeprom
223  * RETURNS: the frequency in MHz
224  */
225 static uint16_t
226 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
227 {
228 	/*
229 	 * Reserved value 0xFF provides an empty definition both as
230 	 * an fbin and as a frequency - do not convert
231 	*/
232 	if (fbin == AR5416_BCHAN_UNUSED)
233 		return fbin;
234 	return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
235 }
236 
237 /*
238  * Copy EEPROM Conformance Testing Limits contents
239  * into the allocated space
240  */
241 /* USE CTLS from chain zero */
242 #define CTL_CHAIN	0
243 
244 static void
245 v9287EepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_9287 *ee)
246 {
247 	RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
248 	int i, j;
249 
250 	HALASSERT(AR9287_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
251 
252 	for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR9287_NUM_CTLS; i++) {
253 		for (j = 0; j < NUM_EDGES; j ++) {
254 			/* XXX Confirm this is the right thing to do when an invalid channel is stored */
255 			if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
256 				rep[j].rdEdge = 0;
257 				rep[j].twice_rdEdgePower = 0;
258 				rep[j].flag = 0;
259 			} else {
260 				rep[j].rdEdge = fbin2freq(
261 				    ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
262 				    (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
263 				rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
264 				rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
265 			}
266 		}
267 		rep += NUM_EDGES;
268 	}
269 	ee->ee_numCtls = i;
270 	HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
271 	    "%s Numctls = %u\n",__func__,i);
272 }
273 
274 /*
275  * Reclaim any EEPROM-related storage.
276  */
277 static void
278 v9287EepromDetach(struct ath_hal *ah)
279 {
280 	HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
281 
282 	ath_hal_free(ee);
283 	AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
284 }
285 
286 #define owl_get_eep_ver(_ee)   \
287     (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
288 #define owl_get_eep_rev(_ee)   \
289     (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
290 
291 HAL_STATUS
292 ath_hal_9287EepromAttach(struct ath_hal *ah)
293 {
294 #define	NW(a)	(sizeof(a) / sizeof(uint16_t))
295 	HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom;
296 	uint16_t *eep_data, magic;
297 	HAL_BOOL need_swap;
298 	u_int w, off, len;
299 	uint32_t sum;
300 
301 	HALASSERT(ee == AH_NULL);
302 
303 	/*
304 	 * Don't check magic if we're supplied with an EEPROM block,
305 	 * typically this is from Howl but it may also be from later
306 	 * boards w/ an embedded WMAC.
307 	 */
308 	if (ah->ah_eepromdata == NULL) {
309 		if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
310 			HALDEBUG(ah, HAL_DEBUG_ANY,
311 			    "%s Error reading Eeprom MAGIC\n", __func__);
312 			return HAL_EEREAD;
313 		}
314 		HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
315 		    __func__, magic);
316 		if (magic != AR5416_EEPROM_MAGIC) {
317 			HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
318 			return HAL_EEMAGIC;
319 		}
320 	}
321 
322 	ee = ath_hal_malloc(sizeof(HAL_EEPROM_9287));
323 	if (ee == AH_NULL) {
324 		/* XXX message */
325 		return HAL_ENOMEM;
326 	}
327 
328 	eep_data = (uint16_t *) ee;
329 	for (w = 0; w < NW(struct ar9287_eeprom); w++) {
330 		off = AR9287_EEP_START_LOC + w;
331 		if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
332 			HALDEBUG(ah, HAL_DEBUG_ANY,
333 			    "%s eeprom read error at offset 0x%x\n",
334 			    __func__, off);
335 			return HAL_EEREAD;
336 		}
337 	}
338 	/* Convert to eeprom native eeprom endian format */
339 	/*
340 	 * XXX this is likely incorrect but will do for now
341 	 * XXX to get embedded boards working.
342 	 */
343 	if (ah->ah_eepromdata == NULL && isBigEndian()) {
344 		for (w = 0; w < NW(HAL_EEPROM_9287); w++)
345 			eep_data[w] = __bswap16(eep_data[w]);
346 	}
347 
348 	/*
349 	 * At this point, we're in the native eeprom endian format
350 	 * Now, determine the eeprom endian by looking at byte 26??
351 	 */
352 	need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
353 	if (need_swap) {
354 		HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
355 		    "Byte swap EEPROM contents.\n");
356 		len = __bswap16(ee->ee_base.baseEepHeader.length);
357 	} else {
358 		len = ee->ee_base.baseEepHeader.length;
359 	}
360 	len = AH_MIN(len, sizeof(HAL_EEPROM_9287)) / sizeof(uint16_t);
361 
362 	/* Apply the checksum, done in native eeprom format */
363 	/* XXX - Need to check to make sure checksum calculation is done
364 	 * in the correct endian format.  Right now, it seems it would
365 	 * cast the raw data to host format and do the calculation, which may
366 	 * not be correct as the calculation may need to be done in the native
367 	 * eeprom format
368 	 */
369 	sum = 0;
370 	for (w = 0; w < len; w++)
371 		sum ^= eep_data[w];
372 	/* Check CRC - Attach should fail on a bad checksum */
373 	if (sum != 0xffff) {
374 		HALDEBUG(ah, HAL_DEBUG_ANY,
375 		    "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
376 		return HAL_EEBADSUM;
377 	}
378 
379 	if (need_swap)
380 		eepromSwap(ee);	/* byte swap multi-byte data */
381 
382 	/* swap words 0+2 so version is at the front */
383 	magic = eep_data[0];
384 	eep_data[0] = eep_data[2];
385 	eep_data[2] = magic;
386 
387 	HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
388 	    "%s Eeprom Version %u.%u\n", __func__,
389 	    owl_get_eep_ver(ee), owl_get_eep_rev(ee));
390 
391 	/* NB: must be after all byte swapping */
392 	if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
393 		HALDEBUG(ah, HAL_DEBUG_ANY,
394 		    "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
395 		return HAL_EEBADSUM;
396 	}
397 
398 	v9287EepromReadCTLInfo(ah, ee);		/* Get CTLs */
399 
400 	AH_PRIVATE(ah)->ah_eeprom = ee;
401 	AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
402 	AH_PRIVATE(ah)->ah_eepromDetach = v9287EepromDetach;
403 	AH_PRIVATE(ah)->ah_eepromGet = v9287EepromGet;
404 	AH_PRIVATE(ah)->ah_eepromSet = v9287EepromSet;
405 	AH_PRIVATE(ah)->ah_getSpurChan = v9287EepromGetSpurChan;
406 	AH_PRIVATE(ah)->ah_eepromDiag = v9287EepromDiag;
407 	return HAL_OK;
408 #undef NW
409 }
410