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