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