1572ff6f6SMatthew Dillon /*
2572ff6f6SMatthew Dillon  * Copyright (c) 2008 Sam Leffler, Errno Consulting
3572ff6f6SMatthew Dillon  * Copyright (c) 2008 Atheros Communications, Inc.
4572ff6f6SMatthew Dillon  *
5572ff6f6SMatthew Dillon  * Permission to use, copy, modify, and/or distribute this software for any
6572ff6f6SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
7572ff6f6SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
8572ff6f6SMatthew Dillon  *
9572ff6f6SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10572ff6f6SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11572ff6f6SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12572ff6f6SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13572ff6f6SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14572ff6f6SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15572ff6f6SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16572ff6f6SMatthew Dillon  *
17572ff6f6SMatthew Dillon  * $FreeBSD$
18572ff6f6SMatthew Dillon  */
19572ff6f6SMatthew Dillon #include "opt_ah.h"
20572ff6f6SMatthew Dillon 
21572ff6f6SMatthew Dillon #include "ah.h"
22572ff6f6SMatthew Dillon #include "ah_internal.h"
23572ff6f6SMatthew Dillon #include "ah_eeprom_v14.h"
24572ff6f6SMatthew Dillon 
25572ff6f6SMatthew Dillon static HAL_STATUS
v14EepromGet(struct ath_hal * ah,int param,void * val)26572ff6f6SMatthew Dillon v14EepromGet(struct ath_hal *ah, int param, void *val)
27572ff6f6SMatthew Dillon {
28572ff6f6SMatthew Dillon #define	CHAN_A_IDX	0
29572ff6f6SMatthew Dillon #define	CHAN_B_IDX	1
30572ff6f6SMatthew Dillon #define	IS_VERS(op, v)	((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v))
31572ff6f6SMatthew Dillon 	HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
32572ff6f6SMatthew Dillon 	const MODAL_EEP_HEADER *pModal = ee->ee_base.modalHeader;
33572ff6f6SMatthew Dillon 	const BASE_EEP_HEADER  *pBase  = &ee->ee_base.baseEepHeader;
34572ff6f6SMatthew Dillon 	uint32_t sum;
35572ff6f6SMatthew Dillon 	uint8_t *macaddr;
36572ff6f6SMatthew Dillon 	int i;
37572ff6f6SMatthew Dillon 
38572ff6f6SMatthew Dillon 	switch (param) {
39572ff6f6SMatthew Dillon         case AR_EEP_NFTHRESH_5:
40572ff6f6SMatthew Dillon 		*(int16_t *)val = pModal[0].noiseFloorThreshCh[0];
41572ff6f6SMatthew Dillon 		return HAL_OK;
42572ff6f6SMatthew Dillon         case AR_EEP_NFTHRESH_2:
43572ff6f6SMatthew Dillon 		*(int16_t *)val = pModal[1].noiseFloorThreshCh[0];
44572ff6f6SMatthew Dillon 		return HAL_OK;
45572ff6f6SMatthew Dillon         case AR_EEP_MACADDR:		/* Get MAC Address */
46572ff6f6SMatthew Dillon 		sum = 0;
47572ff6f6SMatthew Dillon 		macaddr = val;
48572ff6f6SMatthew Dillon 		for (i = 0; i < 6; i++) {
49572ff6f6SMatthew Dillon 			macaddr[i] = pBase->macAddr[i];
50572ff6f6SMatthew Dillon 			sum += pBase->macAddr[i];
51572ff6f6SMatthew Dillon 		}
52572ff6f6SMatthew Dillon 		if (sum == 0 || sum == 0xffff*3) {
53572ff6f6SMatthew Dillon 			HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
54572ff6f6SMatthew Dillon 			    __func__, ath_hal_ether_sprintf(macaddr));
55572ff6f6SMatthew Dillon 			return HAL_EEBADMAC;
56572ff6f6SMatthew Dillon 		}
57572ff6f6SMatthew Dillon 		return HAL_OK;
58572ff6f6SMatthew Dillon         case AR_EEP_REGDMN_0:
59572ff6f6SMatthew Dillon 		return pBase->regDmn[0];
60572ff6f6SMatthew Dillon         case AR_EEP_REGDMN_1:
61572ff6f6SMatthew Dillon 		return pBase->regDmn[1];
62572ff6f6SMatthew Dillon         case AR_EEP_OPCAP:
63572ff6f6SMatthew Dillon 		return pBase->deviceCap;
64572ff6f6SMatthew Dillon         case AR_EEP_OPMODE:
65572ff6f6SMatthew Dillon 		return pBase->opCapFlags;
66572ff6f6SMatthew Dillon         case AR_EEP_RFSILENT:
67572ff6f6SMatthew Dillon 		return pBase->rfSilent;
68572ff6f6SMatthew Dillon 	case AR_EEP_OB_5:
69572ff6f6SMatthew Dillon 		return pModal[CHAN_A_IDX].ob;
70572ff6f6SMatthew Dillon     	case AR_EEP_DB_5:
71572ff6f6SMatthew Dillon 		return pModal[CHAN_A_IDX].db;
72572ff6f6SMatthew Dillon     	case AR_EEP_OB_2:
73572ff6f6SMatthew Dillon 		return pModal[CHAN_B_IDX].ob;
74572ff6f6SMatthew Dillon     	case AR_EEP_DB_2:
75572ff6f6SMatthew Dillon 		return pModal[CHAN_B_IDX].db;
76572ff6f6SMatthew Dillon 	case AR_EEP_TXMASK:
77572ff6f6SMatthew Dillon 		return pBase->txMask;
78572ff6f6SMatthew Dillon 	case AR_EEP_RXMASK:
79572ff6f6SMatthew Dillon 		return pBase->rxMask;
80572ff6f6SMatthew Dillon 	case AR_EEP_RXGAIN_TYPE:
81572ff6f6SMatthew Dillon 		return IS_VERS(>=, AR5416_EEP_MINOR_VER_17) ?
82572ff6f6SMatthew Dillon 		    pBase->rxGainType : AR5416_EEP_RXGAIN_ORIG;
83572ff6f6SMatthew Dillon 	case AR_EEP_TXGAIN_TYPE:
84572ff6f6SMatthew Dillon 		return IS_VERS(>=, AR5416_EEP_MINOR_VER_19) ?
85572ff6f6SMatthew Dillon 		    pBase->txGainType : AR5416_EEP_TXGAIN_ORIG;
86572ff6f6SMatthew Dillon 	case AR_EEP_FSTCLK_5G:
87572ff6f6SMatthew Dillon 		/* 5ghz fastclock is always enabled for Merlin minor <= 16 */
88572ff6f6SMatthew Dillon 		if (IS_VERS(<=, AR5416_EEP_MINOR_VER_16))
89572ff6f6SMatthew Dillon 			return HAL_OK;
90572ff6f6SMatthew Dillon 		return pBase->fastClk5g ? HAL_OK : HAL_EIO;
91572ff6f6SMatthew Dillon 	case AR_EEP_OL_PWRCTRL:
92572ff6f6SMatthew Dillon 		HALASSERT(val == AH_NULL);
93572ff6f6SMatthew Dillon 		return pBase->openLoopPwrCntl ?  HAL_OK : HAL_EIO;
94572ff6f6SMatthew Dillon 	case AR_EEP_DAC_HPWR_5G:
95572ff6f6SMatthew Dillon 		if (IS_VERS(>=, AR5416_EEP_MINOR_VER_20)) {
96572ff6f6SMatthew Dillon 			*(uint8_t *) val = pBase->dacHiPwrMode_5G;
97572ff6f6SMatthew Dillon 			return HAL_OK;
98572ff6f6SMatthew Dillon 		} else
99572ff6f6SMatthew Dillon 			return HAL_EIO;
100572ff6f6SMatthew Dillon 	case AR_EEP_FRAC_N_5G:
101572ff6f6SMatthew Dillon 		if (IS_VERS(>=, AR5416_EEP_MINOR_VER_22)) {
102572ff6f6SMatthew Dillon 			*(uint8_t *) val = pBase->frac_n_5g;
103572ff6f6SMatthew Dillon 		} else
104572ff6f6SMatthew Dillon 			*(uint8_t *) val = 0;
105572ff6f6SMatthew Dillon 		return HAL_OK;
106572ff6f6SMatthew Dillon 	case AR_EEP_AMODE:
107572ff6f6SMatthew Dillon 		HALASSERT(val == AH_NULL);
108572ff6f6SMatthew Dillon 		return pBase->opCapFlags & AR5416_OPFLAGS_11A ?
109572ff6f6SMatthew Dillon 		    HAL_OK : HAL_EIO;
110572ff6f6SMatthew Dillon 	case AR_EEP_BMODE:
111572ff6f6SMatthew Dillon 	case AR_EEP_GMODE:
112572ff6f6SMatthew Dillon 		HALASSERT(val == AH_NULL);
113572ff6f6SMatthew Dillon 		return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
114572ff6f6SMatthew Dillon 		    HAL_OK : HAL_EIO;
115572ff6f6SMatthew Dillon 	case AR_EEP_32KHZCRYSTAL:
116572ff6f6SMatthew Dillon 	case AR_EEP_COMPRESS:
117572ff6f6SMatthew Dillon 	case AR_EEP_FASTFRAME:		/* XXX policy decision, h/w can do it */
118572ff6f6SMatthew Dillon 	case AR_EEP_WRITEPROTECT:	/* NB: no write protect bit */
119572ff6f6SMatthew Dillon 		HALASSERT(val == AH_NULL);
120572ff6f6SMatthew Dillon 		/* fall thru... */
121572ff6f6SMatthew Dillon 	case AR_EEP_MAXQCU:		/* NB: not in opCapFlags */
122572ff6f6SMatthew Dillon 	case AR_EEP_KCENTRIES:		/* NB: not in opCapFlags */
123572ff6f6SMatthew Dillon 		return HAL_EIO;
124572ff6f6SMatthew Dillon 	case AR_EEP_AES:
125572ff6f6SMatthew Dillon 	case AR_EEP_BURST:
126572ff6f6SMatthew Dillon         case AR_EEP_RFKILL:
127572ff6f6SMatthew Dillon 	case AR_EEP_TURBO5DISABLE:
128572ff6f6SMatthew Dillon 	case AR_EEP_TURBO2DISABLE:
129572ff6f6SMatthew Dillon 		HALASSERT(val == AH_NULL);
130572ff6f6SMatthew Dillon 		return HAL_OK;
131572ff6f6SMatthew Dillon 	case AR_EEP_ANTGAINMAX_2:
132572ff6f6SMatthew Dillon 		*(int8_t *) val = ee->ee_antennaGainMax[1];
133572ff6f6SMatthew Dillon 		return HAL_OK;
134572ff6f6SMatthew Dillon 	case AR_EEP_ANTGAINMAX_5:
135572ff6f6SMatthew Dillon 		*(int8_t *) val = ee->ee_antennaGainMax[0];
136572ff6f6SMatthew Dillon 		return HAL_OK;
137572ff6f6SMatthew Dillon 	case AR_EEP_PWR_TABLE_OFFSET:
138572ff6f6SMatthew Dillon 		if (IS_VERS(>=, AR5416_EEP_MINOR_VER_21))
139572ff6f6SMatthew Dillon 			*(int8_t *) val = pBase->pwr_table_offset;
140572ff6f6SMatthew Dillon 		else
141572ff6f6SMatthew Dillon 			*(int8_t *) val = AR5416_PWR_TABLE_OFFSET_DB;
142572ff6f6SMatthew Dillon 		return HAL_OK;
143572ff6f6SMatthew Dillon 	case AR_EEP_PWDCLKIND:
144572ff6f6SMatthew Dillon 		if (IS_VERS(>=, AR5416_EEP_MINOR_VER_10)) {
145572ff6f6SMatthew Dillon 			*(uint8_t *) val = pBase->pwdclkind;
146572ff6f6SMatthew Dillon 			return HAL_OK;
147572ff6f6SMatthew Dillon 		}
148572ff6f6SMatthew Dillon 		return HAL_EIO;
149572ff6f6SMatthew Dillon 
150572ff6f6SMatthew Dillon         default:
151572ff6f6SMatthew Dillon 		HALASSERT(0);
152572ff6f6SMatthew Dillon 		return HAL_EINVAL;
153572ff6f6SMatthew Dillon 	}
154572ff6f6SMatthew Dillon #undef IS_VERS
155572ff6f6SMatthew Dillon #undef CHAN_A_IDX
156572ff6f6SMatthew Dillon #undef CHAN_B_IDX
157572ff6f6SMatthew Dillon }
158572ff6f6SMatthew Dillon 
159572ff6f6SMatthew Dillon static HAL_STATUS
v14EepromSet(struct ath_hal * ah,int param,int v)160572ff6f6SMatthew Dillon v14EepromSet(struct ath_hal *ah, int param, int v)
161572ff6f6SMatthew Dillon {
162572ff6f6SMatthew Dillon 	HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
163572ff6f6SMatthew Dillon 
164572ff6f6SMatthew Dillon 	switch (param) {
165572ff6f6SMatthew Dillon 	case AR_EEP_ANTGAINMAX_2:
166572ff6f6SMatthew Dillon 		ee->ee_antennaGainMax[1] = (int8_t) v;
167572ff6f6SMatthew Dillon 		return HAL_OK;
168572ff6f6SMatthew Dillon 	case AR_EEP_ANTGAINMAX_5:
169572ff6f6SMatthew Dillon 		ee->ee_antennaGainMax[0] = (int8_t) v;
170572ff6f6SMatthew Dillon 		return HAL_OK;
171572ff6f6SMatthew Dillon 	}
172572ff6f6SMatthew Dillon 	return HAL_EINVAL;
173572ff6f6SMatthew Dillon }
174572ff6f6SMatthew Dillon 
175572ff6f6SMatthew Dillon static HAL_BOOL
v14EepromDiag(struct ath_hal * ah,int request,const void * args,uint32_t argsize,void ** result,uint32_t * resultsize)176572ff6f6SMatthew Dillon v14EepromDiag(struct ath_hal *ah, int request,
177572ff6f6SMatthew Dillon      const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
178572ff6f6SMatthew Dillon {
179572ff6f6SMatthew Dillon 	HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
180572ff6f6SMatthew Dillon 
181572ff6f6SMatthew Dillon 	switch (request) {
182572ff6f6SMatthew Dillon 	case HAL_DIAG_EEPROM:
183572ff6f6SMatthew Dillon 		*result = ee;
184572ff6f6SMatthew Dillon 		*resultsize = sizeof(HAL_EEPROM_v14);
185572ff6f6SMatthew Dillon 		return AH_TRUE;
186572ff6f6SMatthew Dillon 	}
187572ff6f6SMatthew Dillon 	return AH_FALSE;
188572ff6f6SMatthew Dillon }
189572ff6f6SMatthew Dillon 
190572ff6f6SMatthew Dillon /* Do structure specific swaps if Eeprom format is non native to host */
191572ff6f6SMatthew Dillon static void
eepromSwap(struct ar5416eeprom * ee)192572ff6f6SMatthew Dillon eepromSwap(struct ar5416eeprom *ee)
193572ff6f6SMatthew Dillon {
194572ff6f6SMatthew Dillon 	uint32_t integer, i, j;
195572ff6f6SMatthew Dillon 	uint16_t word;
196572ff6f6SMatthew Dillon 	MODAL_EEP_HEADER *pModal;
197572ff6f6SMatthew Dillon 
198572ff6f6SMatthew Dillon 	/* convert Base Eep header */
199572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.length);
200572ff6f6SMatthew Dillon 	ee->baseEepHeader.length = word;
201572ff6f6SMatthew Dillon 
202572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.checksum);
203572ff6f6SMatthew Dillon 	ee->baseEepHeader.checksum = word;
204572ff6f6SMatthew Dillon 
205572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.version);
206572ff6f6SMatthew Dillon 	ee->baseEepHeader.version = word;
207572ff6f6SMatthew Dillon 
208572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.regDmn[0]);
209572ff6f6SMatthew Dillon 	ee->baseEepHeader.regDmn[0] = word;
210572ff6f6SMatthew Dillon 
211572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.regDmn[1]);
212572ff6f6SMatthew Dillon 	ee->baseEepHeader.regDmn[1] = word;
213572ff6f6SMatthew Dillon 
214572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.rfSilent);
215572ff6f6SMatthew Dillon 	ee->baseEepHeader.rfSilent = word;
216572ff6f6SMatthew Dillon 
217572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.blueToothOptions);
218572ff6f6SMatthew Dillon 	ee->baseEepHeader.blueToothOptions = word;
219572ff6f6SMatthew Dillon 
220572ff6f6SMatthew Dillon 	word = __bswap16(ee->baseEepHeader.deviceCap);
221572ff6f6SMatthew Dillon 	ee->baseEepHeader.deviceCap = word;
222572ff6f6SMatthew Dillon 
223572ff6f6SMatthew Dillon 	/* convert Modal Eep header */
224572ff6f6SMatthew Dillon 	for (j = 0; j < 2; j++) {
225572ff6f6SMatthew Dillon 		pModal = &ee->modalHeader[j];
226572ff6f6SMatthew Dillon 
227572ff6f6SMatthew Dillon 		/* XXX linux/ah_osdep.h only defines __bswap32 for BE */
228572ff6f6SMatthew Dillon 		integer = __bswap32(pModal->antCtrlCommon);
229572ff6f6SMatthew Dillon 		pModal->antCtrlCommon = integer;
230572ff6f6SMatthew Dillon 
231572ff6f6SMatthew Dillon 		for (i = 0; i < AR5416_MAX_CHAINS; i++) {
232572ff6f6SMatthew Dillon 			integer = __bswap32(pModal->antCtrlChain[i]);
233572ff6f6SMatthew Dillon 			pModal->antCtrlChain[i] = integer;
234572ff6f6SMatthew Dillon 		}
235572ff6f6SMatthew Dillon 		for (i = 0; i < 3; i++) {
236572ff6f6SMatthew Dillon 			word = __bswap16(pModal->xpaBiasLvlFreq[i]);
237572ff6f6SMatthew Dillon 			pModal->xpaBiasLvlFreq[i] = word;
238572ff6f6SMatthew Dillon 		}
239572ff6f6SMatthew Dillon 		for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
240572ff6f6SMatthew Dillon 			word = __bswap16(pModal->spurChans[i].spurChan);
241572ff6f6SMatthew Dillon 			pModal->spurChans[i].spurChan = word;
242572ff6f6SMatthew Dillon 		}
243572ff6f6SMatthew Dillon 	}
244572ff6f6SMatthew Dillon }
245572ff6f6SMatthew Dillon 
246572ff6f6SMatthew Dillon static uint16_t
v14EepromGetSpurChan(struct ath_hal * ah,int ix,HAL_BOOL is2GHz)247572ff6f6SMatthew Dillon v14EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
248572ff6f6SMatthew Dillon {
249572ff6f6SMatthew Dillon 	HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
250572ff6f6SMatthew Dillon 
251572ff6f6SMatthew Dillon 	HALASSERT(0 <= ix && ix <  AR5416_EEPROM_MODAL_SPURS);
252572ff6f6SMatthew Dillon 	return ee->ee_base.modalHeader[is2GHz].spurChans[ix].spurChan;
253572ff6f6SMatthew Dillon }
254572ff6f6SMatthew Dillon 
255572ff6f6SMatthew Dillon /**************************************************************************
256572ff6f6SMatthew Dillon  * fbin2freq
257572ff6f6SMatthew Dillon  *
258572ff6f6SMatthew Dillon  * Get channel value from binary representation held in eeprom
259572ff6f6SMatthew Dillon  * RETURNS: the frequency in MHz
260572ff6f6SMatthew Dillon  */
261572ff6f6SMatthew Dillon static uint16_t
fbin2freq(uint8_t fbin,HAL_BOOL is2GHz)262572ff6f6SMatthew Dillon fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
263572ff6f6SMatthew Dillon {
264572ff6f6SMatthew Dillon 	/*
265572ff6f6SMatthew Dillon 	 * Reserved value 0xFF provides an empty definition both as
266572ff6f6SMatthew Dillon 	 * an fbin and as a frequency - do not convert
267572ff6f6SMatthew Dillon 	 */
268572ff6f6SMatthew Dillon 	if (fbin == AR5416_BCHAN_UNUSED)
269572ff6f6SMatthew Dillon 		return fbin;
270572ff6f6SMatthew Dillon 	return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
271572ff6f6SMatthew Dillon }
272572ff6f6SMatthew Dillon 
273572ff6f6SMatthew Dillon /*
274572ff6f6SMatthew Dillon  * Copy EEPROM Conformance Testing Limits contents
275572ff6f6SMatthew Dillon  * into the allocated space
276572ff6f6SMatthew Dillon  */
277572ff6f6SMatthew Dillon /* USE CTLS from chain zero */
278572ff6f6SMatthew Dillon #define CTL_CHAIN	0
279572ff6f6SMatthew Dillon 
280572ff6f6SMatthew Dillon static void
v14EepromReadCTLInfo(struct ath_hal * ah,HAL_EEPROM_v14 * ee)281572ff6f6SMatthew Dillon v14EepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v14 *ee)
282572ff6f6SMatthew Dillon {
283572ff6f6SMatthew Dillon 	RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
284572ff6f6SMatthew Dillon 	int i, j;
285572ff6f6SMatthew Dillon 
286572ff6f6SMatthew Dillon 	HALASSERT(AR5416_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
287572ff6f6SMatthew Dillon 
288*93d72667SSascha Wildner 	for (i = 0; i < AR5416_NUM_CTLS && ee->ee_base.ctlIndex[i] != 0; i++) {
289572ff6f6SMatthew Dillon 		for (j = 0; j < NUM_EDGES; j ++) {
290572ff6f6SMatthew Dillon 			/* XXX Confirm this is the right thing to do when an invalid channel is stored */
291572ff6f6SMatthew Dillon 			if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
292572ff6f6SMatthew Dillon 				rep[j].rdEdge = 0;
293572ff6f6SMatthew Dillon 				rep[j].twice_rdEdgePower = 0;
294572ff6f6SMatthew Dillon 				rep[j].flag = 0;
295572ff6f6SMatthew Dillon 			} else {
296572ff6f6SMatthew Dillon 				rep[j].rdEdge = fbin2freq(
297572ff6f6SMatthew Dillon 				    ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
298572ff6f6SMatthew Dillon 				    (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
299572ff6f6SMatthew Dillon 				rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
300572ff6f6SMatthew Dillon 				rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
301572ff6f6SMatthew Dillon 			}
302572ff6f6SMatthew Dillon 		}
303572ff6f6SMatthew Dillon 		rep += NUM_EDGES;
304572ff6f6SMatthew Dillon 	}
305572ff6f6SMatthew Dillon 	ee->ee_numCtls = i;
306572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
307572ff6f6SMatthew Dillon 	    "%s Numctls = %u\n",__func__,i);
308572ff6f6SMatthew Dillon }
309572ff6f6SMatthew Dillon 
310572ff6f6SMatthew Dillon /*
311572ff6f6SMatthew Dillon  * Reclaim any EEPROM-related storage.
312572ff6f6SMatthew Dillon  */
313572ff6f6SMatthew Dillon static void
v14EepromDetach(struct ath_hal * ah)314572ff6f6SMatthew Dillon v14EepromDetach(struct ath_hal *ah)
315572ff6f6SMatthew Dillon {
316572ff6f6SMatthew Dillon 	HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
317572ff6f6SMatthew Dillon 
318572ff6f6SMatthew Dillon 	ath_hal_free(ee);
319572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
320572ff6f6SMatthew Dillon }
321572ff6f6SMatthew Dillon 
322572ff6f6SMatthew Dillon #define owl_get_eep_ver(_ee)   \
323572ff6f6SMatthew Dillon     (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
324572ff6f6SMatthew Dillon #define owl_get_eep_rev(_ee)   \
325572ff6f6SMatthew Dillon     (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
326572ff6f6SMatthew Dillon 
327572ff6f6SMatthew Dillon /*
328572ff6f6SMatthew Dillon  * Howl is (hopefully) a special case where the endian-ness of the EEPROM
329572ff6f6SMatthew Dillon  * matches the native endian-ness; and that supplied EEPROMs don't have
330572ff6f6SMatthew Dillon  * a magic value to check.
331572ff6f6SMatthew Dillon  */
332572ff6f6SMatthew Dillon HAL_STATUS
ath_hal_v14EepromAttach(struct ath_hal * ah)333572ff6f6SMatthew Dillon ath_hal_v14EepromAttach(struct ath_hal *ah)
334572ff6f6SMatthew Dillon {
335572ff6f6SMatthew Dillon #define	NW(a)	(sizeof(a) / sizeof(uint16_t))
336572ff6f6SMatthew Dillon 	HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
337572ff6f6SMatthew Dillon 	uint16_t *eep_data, magic;
338572ff6f6SMatthew Dillon 	HAL_BOOL need_swap;
339572ff6f6SMatthew Dillon 	u_int w, off, len;
340572ff6f6SMatthew Dillon 	uint32_t sum;
341572ff6f6SMatthew Dillon 
342572ff6f6SMatthew Dillon 	HALASSERT(ee == AH_NULL);
343572ff6f6SMatthew Dillon 
344572ff6f6SMatthew Dillon 	/*
345572ff6f6SMatthew Dillon 	 * Don't check magic if we're supplied with an EEPROM block,
346572ff6f6SMatthew Dillon 	 * typically this is from Howl but it may also be from later
347572ff6f6SMatthew Dillon 	 * boards w/ an embedded Merlin.
348572ff6f6SMatthew Dillon 	 */
349572ff6f6SMatthew Dillon 	if (ah->ah_eepromdata == NULL) {
350572ff6f6SMatthew Dillon 		if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
351572ff6f6SMatthew Dillon 			HALDEBUG(ah, HAL_DEBUG_ANY,
352572ff6f6SMatthew Dillon 			    "%s Error reading Eeprom MAGIC\n", __func__);
353572ff6f6SMatthew Dillon 			return HAL_EEREAD;
354572ff6f6SMatthew Dillon 		}
355572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
356572ff6f6SMatthew Dillon 		    __func__, magic);
357572ff6f6SMatthew Dillon 		if (magic != AR5416_EEPROM_MAGIC) {
358572ff6f6SMatthew Dillon 			HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
359572ff6f6SMatthew Dillon 			return HAL_EEMAGIC;
360572ff6f6SMatthew Dillon 		}
361572ff6f6SMatthew Dillon 	}
362572ff6f6SMatthew Dillon 
363572ff6f6SMatthew Dillon 	ee = ath_hal_malloc(sizeof(HAL_EEPROM_v14));
364572ff6f6SMatthew Dillon 	if (ee == AH_NULL) {
365572ff6f6SMatthew Dillon 		/* XXX message */
366572ff6f6SMatthew Dillon 		return HAL_ENOMEM;
367572ff6f6SMatthew Dillon 	}
368572ff6f6SMatthew Dillon 
369572ff6f6SMatthew Dillon 	eep_data = (uint16_t *)&ee->ee_base;
370572ff6f6SMatthew Dillon 	for (w = 0; w < NW(struct ar5416eeprom); w++) {
371572ff6f6SMatthew Dillon 		off = owl_eep_start_loc + w;	/* NB: AP71 starts at 0 */
372572ff6f6SMatthew Dillon 		if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
373572ff6f6SMatthew Dillon 			HALDEBUG(ah, HAL_DEBUG_ANY,
374572ff6f6SMatthew Dillon 			    "%s eeprom read error at offset 0x%x\n",
375572ff6f6SMatthew Dillon 			    __func__, off);
376572ff6f6SMatthew Dillon 			return HAL_EEREAD;
377572ff6f6SMatthew Dillon 		}
378572ff6f6SMatthew Dillon 	}
379572ff6f6SMatthew Dillon 	/* Convert to eeprom native eeprom endian format */
380572ff6f6SMatthew Dillon 	/* XXX this is likely incorrect but will do for now to get howl/ap83 working. */
381572ff6f6SMatthew Dillon 	if (ah->ah_eepromdata == NULL && isBigEndian()) {
382572ff6f6SMatthew Dillon 		for (w = 0; w < NW(struct ar5416eeprom); w++)
383572ff6f6SMatthew Dillon 			eep_data[w] = __bswap16(eep_data[w]);
384572ff6f6SMatthew Dillon 	}
385572ff6f6SMatthew Dillon 
386572ff6f6SMatthew Dillon 	/*
387572ff6f6SMatthew Dillon 	 * At this point, we're in the native eeprom endian format
388572ff6f6SMatthew Dillon 	 * Now, determine the eeprom endian by looking at byte 26??
389572ff6f6SMatthew Dillon 	 */
390572ff6f6SMatthew Dillon 	need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
391572ff6f6SMatthew Dillon 	if (need_swap) {
392572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
393572ff6f6SMatthew Dillon 		    "Byte swap EEPROM contents.\n");
394572ff6f6SMatthew Dillon 		len = __bswap16(ee->ee_base.baseEepHeader.length);
395572ff6f6SMatthew Dillon 	} else {
396572ff6f6SMatthew Dillon 		len = ee->ee_base.baseEepHeader.length;
397572ff6f6SMatthew Dillon 	}
398572ff6f6SMatthew Dillon 	len = AH_MIN(len, sizeof(struct ar5416eeprom)) / sizeof(uint16_t);
399572ff6f6SMatthew Dillon 
400572ff6f6SMatthew Dillon 	/* Apply the checksum, done in native eeprom format */
401572ff6f6SMatthew Dillon 	/* XXX - Need to check to make sure checksum calculation is done
402572ff6f6SMatthew Dillon 	 * in the correct endian format.  Right now, it seems it would
403572ff6f6SMatthew Dillon 	 * cast the raw data to host format and do the calculation, which may
404572ff6f6SMatthew Dillon 	 * not be correct as the calculation may need to be done in the native
405572ff6f6SMatthew Dillon 	 * eeprom format
406572ff6f6SMatthew Dillon 	 */
407572ff6f6SMatthew Dillon 	sum = 0;
408572ff6f6SMatthew Dillon 	for (w = 0; w < len; w++)
409572ff6f6SMatthew Dillon 		sum ^= eep_data[w];
410572ff6f6SMatthew Dillon 	/* Check CRC - Attach should fail on a bad checksum */
411572ff6f6SMatthew Dillon 	if (sum != 0xffff) {
412572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY,
413572ff6f6SMatthew Dillon 		    "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
414572ff6f6SMatthew Dillon 		return HAL_EEBADSUM;
415572ff6f6SMatthew Dillon 	}
416572ff6f6SMatthew Dillon 
417572ff6f6SMatthew Dillon 	if (need_swap)
418572ff6f6SMatthew Dillon 		eepromSwap(&ee->ee_base);	/* byte swap multi-byte data */
419572ff6f6SMatthew Dillon 
420572ff6f6SMatthew Dillon 	/* swap words 0+2 so version is at the front */
421572ff6f6SMatthew Dillon 	magic = eep_data[0];
422572ff6f6SMatthew Dillon 	eep_data[0] = eep_data[2];
423572ff6f6SMatthew Dillon 	eep_data[2] = magic;
424572ff6f6SMatthew Dillon 
425572ff6f6SMatthew Dillon 	HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
426572ff6f6SMatthew Dillon 	    "%s Eeprom Version %u.%u\n", __func__,
427572ff6f6SMatthew Dillon 	    owl_get_eep_ver(ee), owl_get_eep_rev(ee));
428572ff6f6SMatthew Dillon 
429572ff6f6SMatthew Dillon 	/* NB: must be after all byte swapping */
430572ff6f6SMatthew Dillon 	if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
431572ff6f6SMatthew Dillon 		HALDEBUG(ah, HAL_DEBUG_ANY,
432572ff6f6SMatthew Dillon 		    "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
433572ff6f6SMatthew Dillon 		return HAL_EEBADSUM;
434572ff6f6SMatthew Dillon 	}
435572ff6f6SMatthew Dillon 
436572ff6f6SMatthew Dillon 	v14EepromReadCTLInfo(ah, ee);		/* Get CTLs */
437572ff6f6SMatthew Dillon 
438572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eeprom = ee;
439572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
440572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eepromDetach = v14EepromDetach;
441572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eepromGet = v14EepromGet;
442572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eepromSet = v14EepromSet;
443572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_getSpurChan = v14EepromGetSpurChan;
444572ff6f6SMatthew Dillon 	AH_PRIVATE(ah)->ah_eepromDiag = v14EepromDiag;
445572ff6f6SMatthew Dillon 	return HAL_OK;
446572ff6f6SMatthew Dillon #undef NW
447572ff6f6SMatthew Dillon }
448