xref: /freebsd/sys/dev/ath/ath_hal/ah_eeprom_v4k.c (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
1 /*
2  * Copyright (c) 2009 Rui Paulo <rpaulo@FreeBSD.org>
3  * Copyright (c) 2008 Sam Leffler, Errno Consulting
4  * Copyright (c) 2008 Atheros Communications, Inc.
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * $FreeBSD$
19  */
20 #include "opt_ah.h"
21 
22 #include "ah.h"
23 #include "ah_internal.h"
24 #include "ah_eeprom_v14.h"
25 #include "ah_eeprom_v4k.h"
26 
27 static HAL_STATUS
28 v4kEepromGet(struct ath_hal *ah, int param, void *val)
29 {
30 #define	CHAN_A_IDX	0
31 #define	CHAN_B_IDX	1
32 #define	IS_VERS(op, v)	((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v))
33 	HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
34 	const MODAL_EEP4K_HEADER *pModal = &ee->ee_base.modalHeader;
35 	const BASE_EEP4K_HEADER  *pBase  = &ee->ee_base.baseEepHeader;
36 	uint32_t sum;
37 	uint8_t *macaddr;
38 	int i;
39 
40 	switch (param) {
41         case AR_EEP_NFTHRESH_5:
42 		*(int16_t *)val = pModal[0].noiseFloorThreshCh[0];
43 		return HAL_OK;
44         case AR_EEP_NFTHRESH_2:
45 		*(int16_t *)val = pModal[1].noiseFloorThreshCh[0];
46 		return HAL_OK;
47         case AR_EEP_MACADDR:		/* Get MAC Address */
48 		sum = 0;
49 		macaddr = val;
50 		for (i = 0; i < 6; i++) {
51 			macaddr[i] = pBase->macAddr[i];
52 			sum += pBase->macAddr[i];
53 		}
54 		if (sum == 0 || sum == 0xffff*3) {
55 			HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
56 			    __func__, ath_hal_ether_sprintf(macaddr));
57 			return HAL_EEBADMAC;
58 		}
59 		return HAL_OK;
60         case AR_EEP_REGDMN_0:
61 		return pBase->regDmn[0];
62         case AR_EEP_REGDMN_1:
63 		return pBase->regDmn[1];
64         case AR_EEP_OPCAP:
65 		return pBase->deviceCap;
66         case AR_EEP_OPMODE:
67 		return pBase->opCapFlags;
68         case AR_EEP_RFSILENT:
69 		return pBase->rfSilent;
70 	case AR_EEP_OB_5:
71 		return pModal[CHAN_A_IDX].ob;
72     	case AR_EEP_DB_5:
73 		return pModal[CHAN_A_IDX].db;
74     	case AR_EEP_OB_2:
75 		return pModal[CHAN_B_IDX].ob;
76     	case AR_EEP_DB_2:
77 		return pModal[CHAN_B_IDX].db;
78 	case AR_EEP_TXMASK:
79 		return pBase->txMask;
80 	case AR_EEP_RXMASK:
81 		return pBase->rxMask;
82 	case AR_EEP_RXGAIN_TYPE:
83 		return AR5416_EEP_RXGAIN_ORIG;
84 	case AR_EEP_TXGAIN_TYPE:
85 		return IS_VERS(>=, AR5416_EEP_MINOR_VER_19) ?
86 		    pBase->txGainType : AR5416_EEP_TXGAIN_ORIG;
87 #if 0
88 	case AR_EEP_OL_PWRCTRL:
89 		HALASSERT(val == AH_NULL);
90 		return pBase->openLoopPwrCntl ?  HAL_OK : HAL_EIO;
91 #endif
92 	case AR_EEP_AMODE:
93 		HALASSERT(val == AH_NULL);
94 		return pBase->opCapFlags & AR5416_OPFLAGS_11A ?
95 		    HAL_OK : HAL_EIO;
96 	case AR_EEP_BMODE:
97 	case AR_EEP_GMODE:
98 		HALASSERT(val == AH_NULL);
99 		return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
100 		    HAL_OK : HAL_EIO;
101 	case AR_EEP_32KHZCRYSTAL:
102 	case AR_EEP_COMPRESS:
103 	case AR_EEP_FASTFRAME:		/* XXX policy decision, h/w can do it */
104 	case AR_EEP_WRITEPROTECT:	/* NB: no write protect bit */
105 		HALASSERT(val == AH_NULL);
106 		/* fall thru... */
107 	case AR_EEP_MAXQCU:		/* NB: not in opCapFlags */
108 	case AR_EEP_KCENTRIES:		/* NB: not in opCapFlags */
109 		return HAL_EIO;
110 	case AR_EEP_AES:
111 	case AR_EEP_BURST:
112         case AR_EEP_RFKILL:
113 	case AR_EEP_TURBO5DISABLE:
114 	case AR_EEP_TURBO2DISABLE:
115 		HALASSERT(val == AH_NULL);
116 		return HAL_OK;
117 	case AR_EEP_ANTGAINMAX_2:
118 		*(int8_t *) val = ee->ee_antennaGainMax[1];
119 		return HAL_OK;
120 	case AR_EEP_ANTGAINMAX_5:
121 		*(int8_t *) val = ee->ee_antennaGainMax[0];
122 		return HAL_OK;
123         default:
124 		HALASSERT(0);
125 		return HAL_EINVAL;
126 	}
127 #undef IS_VERS
128 #undef CHAN_A_IDX
129 #undef CHAN_B_IDX
130 }
131 
132 static HAL_BOOL
133 v4kEepromSet(struct ath_hal *ah, int param, int v)
134 {
135 	HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
136 
137 	switch (param) {
138 	case AR_EEP_ANTGAINMAX_2:
139 		ee->ee_antennaGainMax[1] = (int8_t) v;
140 		return HAL_OK;
141 	case AR_EEP_ANTGAINMAX_5:
142 		ee->ee_antennaGainMax[0] = (int8_t) v;
143 		return HAL_OK;
144 	}
145 	return HAL_EINVAL;
146 }
147 
148 static HAL_BOOL
149 v4kEepromDiag(struct ath_hal *ah, int request,
150      const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
151 {
152 	HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
153 
154 	switch (request) {
155 	case HAL_DIAG_EEPROM:
156 		*result = &ee->ee_base;
157 		*resultsize = sizeof(ee->ee_base);
158 		return AH_TRUE;
159 	}
160 	return AH_FALSE;
161 }
162 
163 /* Do structure specific swaps if Eeprom format is non native to host */
164 static void
165 eepromSwap(struct ar5416eeprom_4k *ee)
166 {
167 	uint32_t integer, i;
168 	uint16_t word;
169 	MODAL_EEP4K_HEADER *pModal;
170 
171 	/* convert Base Eep header */
172 	word = __bswap16(ee->baseEepHeader.length);
173 	ee->baseEepHeader.length = word;
174 
175 	word = __bswap16(ee->baseEepHeader.checksum);
176 	ee->baseEepHeader.checksum = word;
177 
178 	word = __bswap16(ee->baseEepHeader.version);
179 	ee->baseEepHeader.version = word;
180 
181 	word = __bswap16(ee->baseEepHeader.regDmn[0]);
182 	ee->baseEepHeader.regDmn[0] = word;
183 
184 	word = __bswap16(ee->baseEepHeader.regDmn[1]);
185 	ee->baseEepHeader.regDmn[1] = word;
186 
187 	word = __bswap16(ee->baseEepHeader.rfSilent);
188 	ee->baseEepHeader.rfSilent = word;
189 
190 	word = __bswap16(ee->baseEepHeader.blueToothOptions);
191 	ee->baseEepHeader.blueToothOptions = word;
192 
193 	word = __bswap16(ee->baseEepHeader.deviceCap);
194 	ee->baseEepHeader.deviceCap = word;
195 
196 	/* convert Modal Eep header */
197 	pModal = &ee->modalHeader;
198 
199 	/* XXX linux/ah_osdep.h only defines __bswap32 for BE */
200 	integer = __bswap32(pModal->antCtrlCommon);
201 	pModal->antCtrlCommon = integer;
202 
203 	for (i = 0; i < AR5416_4K_MAX_CHAINS; i++) {
204 		integer = __bswap32(pModal->antCtrlChain[i]);
205 		pModal->antCtrlChain[i] = integer;
206 	}
207 
208 	for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
209 		word = __bswap16(pModal->spurChans[i].spurChan);
210 		pModal->spurChans[i].spurChan = word;
211 	}
212 }
213 
214 static uint16_t
215 v4kEepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
216 {
217 	HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
218 
219 	HALASSERT(0 <= ix && ix <  AR5416_EEPROM_MODAL_SPURS);
220 	HALASSERT(is2GHz);
221 	return ee->ee_base.modalHeader.spurChans[ix].spurChan;
222 }
223 
224 /**************************************************************************
225  * fbin2freq
226  *
227  * Get channel value from binary representation held in eeprom
228  * RETURNS: the frequency in MHz
229  */
230 static uint16_t
231 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
232 {
233 	/*
234 	 * Reserved value 0xFF provides an empty definition both as
235 	 * an fbin and as a frequency - do not convert
236 	 */
237 	if (fbin == AR5416_BCHAN_UNUSED)
238 		return fbin;
239 	return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
240 }
241 
242 /*
243  * Copy EEPROM Conformance Testing Limits contents
244  * into the allocated space
245  */
246 /* USE CTLS from chain zero */
247 #define CTL_CHAIN	0
248 
249 static void
250 v4kEepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v4k *ee)
251 {
252 	RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
253 	int i, j;
254 
255 	HALASSERT(AR5416_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
256 
257 	for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_4K_NUM_CTLS; i++) {
258 		for (j = 0; j < NUM_EDGES; j ++) {
259 			/* XXX Confirm this is the right thing to do when an invalid channel is stored */
260 			if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
261 				rep[j].rdEdge = 0;
262 				rep[j].twice_rdEdgePower = 0;
263 				rep[j].flag = 0;
264 			} else {
265 				rep[j].rdEdge = fbin2freq(
266 				    ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
267 				    (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
268 				rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
269 				rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
270 			}
271 		}
272 		rep += NUM_EDGES;
273 	}
274 	ee->ee_numCtls = i;
275 	HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
276 	    "%s Numctls = %u\n",__func__,i);
277 }
278 
279 /*
280  * Reclaim any EEPROM-related storage.
281  */
282 static void
283 v4kEepromDetach(struct ath_hal *ah)
284 {
285 	HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
286 
287 	ath_hal_free(ee);
288 	AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
289 }
290 
291 #define owl_get_eep_ver(_ee)   \
292     (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
293 #define owl_get_eep_rev(_ee)   \
294     (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
295 
296 HAL_STATUS
297 ath_hal_v4kEepromAttach(struct ath_hal *ah)
298 {
299 #define	NW(a)	(sizeof(a) / sizeof(uint16_t))
300 	HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
301 	uint16_t *eep_data, magic;
302 	HAL_BOOL need_swap;
303 	u_int w, off, len;
304 	uint32_t sum;
305 
306 	HALASSERT(ee == AH_NULL);
307 
308 	if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
309 		HALDEBUG(ah, HAL_DEBUG_ANY,
310 		    "%s Error reading Eeprom MAGIC\n", __func__);
311 		return HAL_EEREAD;
312 	}
313 	HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
314 	    __func__, magic);
315 	if (magic != AR5416_EEPROM_MAGIC) {
316 		HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
317 		return HAL_EEMAGIC;
318 	}
319 
320 	ee = ath_hal_malloc(sizeof(HAL_EEPROM_v4k));
321 	if (ee == AH_NULL) {
322 		/* XXX message */
323 		return HAL_ENOMEM;
324 	}
325 
326 	eep_data = (uint16_t *)&ee->ee_base;
327 	for (w = 0; w < NW(struct ar5416eeprom_4k); w++) {
328 		off = owl_eep_start_loc + w;	/* NB: AP71 starts at 0 */
329 		if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
330 			HALDEBUG(ah, HAL_DEBUG_ANY,
331 			    "%s eeprom read error at offset 0x%x\n",
332 			    __func__, off);
333 			return HAL_EEREAD;
334 		}
335 	}
336 	/* Convert to eeprom native eeprom endian format */
337 	if (isBigEndian()) {
338 		for (w = 0; w < NW(struct ar5416eeprom_4k); w++)
339 			eep_data[w] = __bswap16(eep_data[w]);
340 	}
341 
342 	/*
343 	 * At this point, we're in the native eeprom endian format
344 	 * Now, determine the eeprom endian by looking at byte 26??
345 	 */
346 	need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
347 	if (need_swap) {
348 		HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
349 		    "Byte swap EEPROM contents.\n");
350 		len = __bswap16(ee->ee_base.baseEepHeader.length);
351 	} else {
352 		len = ee->ee_base.baseEepHeader.length;
353 	}
354 	len = AH_MIN(len, sizeof(struct ar5416eeprom_4k)) / sizeof(uint16_t);
355 
356 	/* Apply the checksum, done in native eeprom format */
357 	/* XXX - Need to check to make sure checksum calculation is done
358 	 * in the correct endian format.  Right now, it seems it would
359 	 * cast the raw data to host format and do the calculation, which may
360 	 * not be correct as the calculation may need to be done in the native
361 	 * eeprom format
362 	 */
363 	sum = 0;
364 	for (w = 0; w < len; w++) {
365 		sum ^= eep_data[w];
366 	}
367 	/* Check CRC - Attach should fail on a bad checksum */
368 	if (sum != 0xffff) {
369 		HALDEBUG(ah, HAL_DEBUG_ANY,
370 		    "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
371 		return HAL_EEBADSUM;
372 	}
373 
374 	if (need_swap)
375 		eepromSwap(&ee->ee_base);	/* byte swap multi-byte data */
376 
377 	/* swap words 0+2 so version is at the front */
378 	magic = eep_data[0];
379 	eep_data[0] = eep_data[2];
380 	eep_data[2] = magic;
381 
382 	HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
383 	    "%s Eeprom Version %u.%u\n", __func__,
384 	    owl_get_eep_ver(ee), owl_get_eep_rev(ee));
385 
386 	/* NB: must be after all byte swapping */
387 	if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
388 		HALDEBUG(ah, HAL_DEBUG_ANY,
389 		    "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
390 		return HAL_EEBADSUM;
391 	}
392 
393 	v4kEepromReadCTLInfo(ah, ee);		/* Get CTLs */
394 
395 	AH_PRIVATE(ah)->ah_eeprom = ee;
396 	AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
397 	AH_PRIVATE(ah)->ah_eepromDetach = v4kEepromDetach;
398 	AH_PRIVATE(ah)->ah_eepromGet = v4kEepromGet;
399 	AH_PRIVATE(ah)->ah_eepromSet = v4kEepromSet;
400 	AH_PRIVATE(ah)->ah_getSpurChan = v4kEepromGetSpurChan;
401 	AH_PRIVATE(ah)->ah_eepromDiag = v4kEepromDiag;
402 	return HAL_OK;
403 #undef NW
404 }
405