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