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