2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2009 Rui Paulo <rpaulo@FreeBSD.org>
5 * Copyright (c) 2008 Sam Leffler, Errno Consulting
6 * Copyright (c) 2008 Atheros Communications, Inc.
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include "ah_internal.h"
26 #include "ah_eeprom_v14.h"
27 #include "ah_eeprom_v4k.h"
30 v4kEepromGet(struct ath_hal *ah, int param, void *val)
34 #define IS_VERS(op, v) ((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v))
35 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
36 const MODAL_EEP4K_HEADER *pModal = &ee->ee_base.modalHeader;
37 const BASE_EEP4K_HEADER *pBase = &ee->ee_base.baseEepHeader;
43 case AR_EEP_NFTHRESH_2:
44 *(int16_t *)val = pModal->noiseFloorThreshCh[0];
46 case AR_EEP_MACADDR: /* Get MAC Address */
49 for (i = 0; i < 6; i++) {
50 macaddr[i] = pBase->macAddr[i];
51 sum += pBase->macAddr[i];
53 if (sum == 0 || sum == 0xffff*3) {
54 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n",
55 __func__, ath_hal_ether_sprintf(macaddr));
60 return pBase->regDmn[0];
62 return pBase->regDmn[1];
64 return pBase->deviceCap;
66 return pBase->opCapFlags;
68 return pBase->rfSilent;
77 case AR_EEP_RXGAIN_TYPE:
78 return AR5416_EEP_RXGAIN_ORIG;
79 case AR_EEP_TXGAIN_TYPE:
80 return pBase->txGainType;
81 case AR_EEP_OL_PWRCTRL:
82 HALASSERT(val == AH_NULL);
85 HALASSERT(val == AH_NULL);
86 return pBase->opCapFlags & AR5416_OPFLAGS_11A ?
90 HALASSERT(val == AH_NULL);
91 return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
93 case AR_EEP_32KHZCRYSTAL:
95 case AR_EEP_FASTFRAME: /* XXX policy decision, h/w can do it */
96 case AR_EEP_WRITEPROTECT: /* NB: no write protect bit */
97 HALASSERT(val == AH_NULL);
99 case AR_EEP_MAXQCU: /* NB: not in opCapFlags */
100 case AR_EEP_KCENTRIES: /* NB: not in opCapFlags */
105 case AR_EEP_TURBO2DISABLE:
106 HALASSERT(val == AH_NULL);
108 case AR_EEP_ANTGAINMAX_2:
109 *(int8_t *) val = ee->ee_antennaGainMax;
121 v4kEepromSet(struct ath_hal *ah, int param, int v)
123 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
126 case AR_EEP_ANTGAINMAX_2:
127 ee->ee_antennaGainMax = (int8_t) v;
134 v4kEepromDiag(struct ath_hal *ah, int request,
135 const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
137 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
140 case HAL_DIAG_EEPROM:
142 *resultsize = sizeof(HAL_EEPROM_v4k);
148 /* Do structure specific swaps if Eeprom format is non native to host */
150 eepromSwap(struct ar5416eeprom_4k *ee)
154 MODAL_EEP4K_HEADER *pModal;
156 /* convert Base Eep header */
157 word = __bswap16(ee->baseEepHeader.length);
158 ee->baseEepHeader.length = word;
160 word = __bswap16(ee->baseEepHeader.checksum);
161 ee->baseEepHeader.checksum = word;
163 word = __bswap16(ee->baseEepHeader.version);
164 ee->baseEepHeader.version = word;
166 word = __bswap16(ee->baseEepHeader.regDmn[0]);
167 ee->baseEepHeader.regDmn[0] = word;
169 word = __bswap16(ee->baseEepHeader.regDmn[1]);
170 ee->baseEepHeader.regDmn[1] = word;
172 word = __bswap16(ee->baseEepHeader.rfSilent);
173 ee->baseEepHeader.rfSilent = word;
175 word = __bswap16(ee->baseEepHeader.blueToothOptions);
176 ee->baseEepHeader.blueToothOptions = word;
178 word = __bswap16(ee->baseEepHeader.deviceCap);
179 ee->baseEepHeader.deviceCap = word;
181 /* convert Modal Eep header */
182 pModal = &ee->modalHeader;
184 /* XXX linux/ah_osdep.h only defines __bswap32 for BE */
185 integer = __bswap32(pModal->antCtrlCommon);
186 pModal->antCtrlCommon = integer;
188 for (i = 0; i < AR5416_4K_MAX_CHAINS; i++) {
189 integer = __bswap32(pModal->antCtrlChain[i]);
190 pModal->antCtrlChain[i] = integer;
193 for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
194 word = __bswap16(pModal->spurChans[i].spurChan);
195 pModal->spurChans[i].spurChan = word;
200 v4kEepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
202 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
204 HALASSERT(0 <= ix && ix < AR5416_EEPROM_MODAL_SPURS);
206 return ee->ee_base.modalHeader.spurChans[ix].spurChan;
209 /**************************************************************************
212 * Get channel value from binary representation held in eeprom
213 * RETURNS: the frequency in MHz
216 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
219 * Reserved value 0xFF provides an empty definition both as
220 * an fbin and as a frequency - do not convert
222 if (fbin == AR5416_BCHAN_UNUSED)
224 return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
228 * Copy EEPROM Conformance Testing Limits contents
229 * into the allocated space
231 /* USE CTLS from chain zero */
235 v4kEepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v4k *ee)
237 RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
240 HALASSERT(AR5416_4K_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
242 for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_4K_NUM_CTLS; i++) {
243 for (j = 0; j < NUM_EDGES; j ++) {
244 /* XXX Confirm this is the right thing to do when an invalid channel is stored */
245 if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
247 rep[j].twice_rdEdgePower = 0;
250 rep[j].rdEdge = fbin2freq(
251 ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
252 (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
253 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
254 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
260 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
261 "%s Numctls = %u\n",__func__,i);
265 * Reclaim any EEPROM-related storage.
268 v4kEepromDetach(struct ath_hal *ah)
270 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
273 AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
276 #define owl_get_eep_ver(_ee) \
277 (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
278 #define owl_get_eep_rev(_ee) \
279 (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
282 ath_hal_v4kEepromAttach(struct ath_hal *ah)
284 #define NW(a) (sizeof(a) / sizeof(uint16_t))
285 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom;
286 uint16_t *eep_data, magic;
291 HALASSERT(ee == AH_NULL);
293 * Don't check magic if we're supplied with an EEPROM block,
294 * typically this is from Howl but it may also be from later
295 * boards w/ an embedded WMAC.
297 if (ah->ah_eepromdata == NULL) {
298 if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
299 HALDEBUG(ah, HAL_DEBUG_ANY,
300 "%s Error reading Eeprom MAGIC\n", __func__);
303 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
305 if (magic != AR5416_EEPROM_MAGIC) {
306 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
311 ee = ath_hal_malloc(sizeof(HAL_EEPROM_v4k));
317 eep_data = (uint16_t *)&ee->ee_base;
318 for (w = 0; w < NW(struct ar5416eeprom_4k); w++) {
319 off = owl_eep_start_loc + w; /* NB: AP71 starts at 0 */
320 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
321 HALDEBUG(ah, HAL_DEBUG_ANY,
322 "%s eeprom read error at offset 0x%x\n",
327 /* Convert to eeprom native eeprom endian format */
329 * XXX this is likely incorrect but will do for now
330 * XXX to get embedded boards working.
332 if (ah->ah_eepromdata == NULL && isBigEndian()) {
333 for (w = 0; w < NW(struct ar5416eeprom_4k); w++)
334 eep_data[w] = __bswap16(eep_data[w]);
338 * At this point, we're in the native eeprom endian format
339 * Now, determine the eeprom endian by looking at byte 26??
341 need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
343 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
344 "Byte swap EEPROM contents.\n");
345 len = __bswap16(ee->ee_base.baseEepHeader.length);
347 len = ee->ee_base.baseEepHeader.length;
349 len = AH_MIN(len, sizeof(struct ar5416eeprom_4k)) / sizeof(uint16_t);
351 /* Apply the checksum, done in native eeprom format */
352 /* XXX - Need to check to make sure checksum calculation is done
353 * in the correct endian format. Right now, it seems it would
354 * cast the raw data to host format and do the calculation, which may
355 * not be correct as the calculation may need to be done in the native
359 for (w = 0; w < len; w++) {
362 /* Check CRC - Attach should fail on a bad checksum */
364 HALDEBUG(ah, HAL_DEBUG_ANY,
365 "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
370 eepromSwap(&ee->ee_base); /* byte swap multi-byte data */
372 /* swap words 0+2 so version is at the front */
374 eep_data[0] = eep_data[2];
377 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
378 "%s Eeprom Version %u.%u\n", __func__,
379 owl_get_eep_ver(ee), owl_get_eep_rev(ee));
381 /* NB: must be after all byte swapping */
382 if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
383 HALDEBUG(ah, HAL_DEBUG_ANY,
384 "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
388 v4kEepromReadCTLInfo(ah, ee); /* Get CTLs */
390 AH_PRIVATE(ah)->ah_eeprom = ee;
391 AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
392 AH_PRIVATE(ah)->ah_eepromDetach = v4kEepromDetach;
393 AH_PRIVATE(ah)->ah_eepromGet = v4kEepromGet;
394 AH_PRIVATE(ah)->ah_eepromSet = v4kEepromSet;
395 AH_PRIVATE(ah)->ah_getSpurChan = v4kEepromGetSpurChan;
396 AH_PRIVATE(ah)->ah_eepromDiag = v4kEepromDiag;