Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / networks / atheros5000 / hal / ah_eeprom_v14.c
blobc01c66d850ae00fcb6f85b172984d34f13909d43
1 /*
2 * Copyright (c) 2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2008 Atheros Communications, Inc.
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.
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.
17 * $Id$
19 #include "opt_ah.h"
21 #include "ah.h"
22 #include "ah_internal.h"
23 #include "ah_eeprom_v14.h"
25 static HAL_STATUS
26 v14EepromGet(struct ath_hal *ah, int param, void *val)
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;
38 switch (param) {
39 case AR_EEP_NFTHRESH_5:
40 *(int8_t *)val = pModal[0].noiseFloorThreshCh[0];
41 return HAL_OK;
42 case AR_EEP_NFTHRESH_2:
43 *(int8_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];
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;
57 case AR_EEP_REGDMN_0:
58 return pBase->regDmn[0];
59 case AR_EEP_REGDMN_1:
60 return pBase->regDmn[1];
61 case AR_EEP_OPCAP:
62 return pBase->deviceCap;
63 case AR_EEP_OPMODE:
64 return pBase->opCapFlags;
65 case AR_EEP_RFSILENT:
66 return pBase->rfSilent;
67 case AR_EEP_OB_5:
68 return pModal[CHAN_A_IDX].ob;
69 case AR_EEP_DB_5:
70 return pModal[CHAN_A_IDX].db;
71 case AR_EEP_OB_2:
72 return pModal[CHAN_B_IDX].ob;
73 case AR_EEP_DB_2:
74 return pModal[CHAN_B_IDX].db;
75 case AR_EEP_TXMASK:
76 return pBase->txMask;
77 case AR_EEP_RXMASK:
78 return pBase->rxMask;
79 case AR_EEP_RXGAIN_TYPE:
80 return IS_VERS(>=, AR5416_EEP_MINOR_VER_17) ?
81 pBase->rxGainType : AR5416_EEP_RXGAIN_ORIG;
82 case AR_EEP_TXGAIN_TYPE:
83 return IS_VERS(>=, AR5416_EEP_MINOR_VER_19) ?
84 pBase->txGainType : AR5416_EEP_TXGAIN_ORIG;
85 case AR_EEP_FSTCLK_5G:
86 return IS_VERS(>, AR5416_EEP_MINOR_VER_16) ?
87 pBase->fastClk5g : AH_TRUE;
88 case AR_EEP_OL_PWRCTRL:
89 HALASSERT(val == AH_NULL);
90 return pBase->openLoopPwrCntl ? HAL_OK : HAL_EIO;
91 case AR_EEP_AMODE:
92 HALASSERT(val == AH_NULL);
93 return pBase->opCapFlags & AR5416_OPFLAGS_11A ?
94 HAL_OK : HAL_EIO;
95 case AR_EEP_BMODE:
96 case AR_EEP_GMODE:
97 HALASSERT(val == AH_NULL);
98 return pBase->opCapFlags & AR5416_OPFLAGS_11G ?
99 HAL_OK : HAL_EIO;
100 case AR_EEP_32KHZCRYSTAL:
101 case AR_EEP_COMPRESS:
102 case AR_EEP_FASTFRAME: /* XXX policy decision, h/w can do it */
103 case AR_EEP_WRITEPROTECT: /* NB: no write protect bit */
104 HALASSERT(val == AH_NULL);
105 /* fall thru... */
106 case AR_EEP_MAXQCU: /* NB: not in opCapFlags */
107 case AR_EEP_KCENTRIES: /* NB: not in opCapFlags */
108 return HAL_EIO;
109 case AR_EEP_AES:
110 case AR_EEP_BURST:
111 case AR_EEP_RFKILL:
112 case AR_EEP_TURBO5DISABLE:
113 case AR_EEP_TURBO2DISABLE:
114 HALASSERT(val == AH_NULL);
115 return HAL_OK;
116 case AR_EEP_ANTGAINMAX_2:
117 *(int8_t *) val = ee->ee_antennaGainMax[1];
118 return HAL_OK;
119 case AR_EEP_ANTGAINMAX_5:
120 *(int8_t *) val = ee->ee_antennaGainMax[0];
121 return HAL_OK;
122 default:
123 HALASSERT(0);
124 return HAL_EINVAL;
126 #undef IS_VERS
127 #undef CHAN_A_IDX
128 #undef CHAN_B_IDX
131 static HAL_BOOL
132 v14EepromSet(struct ath_hal *ah, int param, int v)
134 HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
136 switch (param) {
137 case AR_EEP_ANTGAINMAX_2:
138 ee->ee_antennaGainMax[1] = (int8_t) v;
139 return HAL_OK;
140 case AR_EEP_ANTGAINMAX_5:
141 ee->ee_antennaGainMax[0] = (int8_t) v;
142 return HAL_OK;
144 return HAL_EINVAL;
147 static HAL_BOOL
148 v14EepromDiag(struct ath_hal *ah, int request,
149 const void *args, uint32_t argsize, void **result, uint32_t *resultsize)
151 HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
153 switch (request) {
154 case HAL_DIAG_EEPROM:
155 *result = &ee->ee_base;
156 *resultsize = sizeof(ee->ee_base);
157 return AH_TRUE;
159 return AH_FALSE;
162 /* XXX conditionalize by target byte order */
163 #ifndef bswap16
164 static __inline__ uint16_t
165 __bswap16(uint16_t _x)
167 return ((uint16_t)(
168 (((const uint8_t *)(&_x))[0] ) |
169 (((const uint8_t *)(&_x))[1]<< 8))
172 #endif
174 /* Do structure specific swaps if Eeprom format is non native to host */
175 static void
176 eepromSwap(struct ar5416eeprom *ee)
178 uint32_t integer, i, j;
179 uint16_t word;
180 MODAL_EEP_HEADER *pModal;
182 /* convert Base Eep header */
183 word = __bswap16(ee->baseEepHeader.length);
184 ee->baseEepHeader.length = word;
186 word = __bswap16(ee->baseEepHeader.checksum);
187 ee->baseEepHeader.checksum = word;
189 word = __bswap16(ee->baseEepHeader.version);
190 ee->baseEepHeader.version = word;
192 word = __bswap16(ee->baseEepHeader.regDmn[0]);
193 ee->baseEepHeader.regDmn[0] = word;
195 word = __bswap16(ee->baseEepHeader.regDmn[1]);
196 ee->baseEepHeader.regDmn[1] = word;
198 word = __bswap16(ee->baseEepHeader.rfSilent);
199 ee->baseEepHeader.rfSilent = word;
201 word = __bswap16(ee->baseEepHeader.blueToothOptions);
202 ee->baseEepHeader.blueToothOptions = word;
204 word = __bswap16(ee->baseEepHeader.deviceCap);
205 ee->baseEepHeader.deviceCap = word;
207 /* convert Modal Eep header */
208 for (j = 0; j < 2; j++) {
209 pModal = &ee->modalHeader[j];
211 /* XXX linux/ah_osdep.h only defines __bswap32 for BE */
212 integer = __bswap32(pModal->antCtrlCommon);
213 pModal->antCtrlCommon = integer;
215 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
216 integer = __bswap32(pModal->antCtrlChain[i]);
217 pModal->antCtrlChain[i] = integer;
220 for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
221 word = __bswap16(pModal->spurChans[i].spurChan);
222 pModal->spurChans[i].spurChan = word;
227 static uint16_t
228 v14EepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz)
230 HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
232 HALASSERT(0 <= ix && ix < AR5416_EEPROM_MODAL_SPURS);
233 return ee->ee_base.modalHeader[is2GHz].spurChans[ix].spurChan;
236 /**************************************************************************
237 * fbin2freq
239 * Get channel value from binary representation held in eeprom
240 * RETURNS: the frequency in MHz
242 static uint16_t
243 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz)
246 * Reserved value 0xFF provides an empty definition both as
247 * an fbin and as a frequency - do not convert
249 if (fbin == AR5416_BCHAN_UNUSED)
250 return fbin;
251 return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
255 * Copy EEPROM Conformance Testing Limits contents
256 * into the allocated space
258 /* USE CTLS from chain zero */
259 #define CTL_CHAIN 0
261 static void
262 v14EepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v14 *ee)
264 RD_EDGES_POWER *rep = ee->ee_rdEdgesPower;
265 int i, j;
267 HALASSERT(AR5416_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES);
269 for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_NUM_CTLS; i++) {
270 for (j = 0; j < NUM_EDGES; j ++) {
271 /* XXX Confirm this is the right thing to do when an invalid channel is stored */
272 if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) {
273 rep[j].rdEdge = 0;
274 rep[j].twice_rdEdgePower = 0;
275 rep[j].flag = 0;
276 } else {
277 rep[j].rdEdge = fbin2freq(
278 ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel,
279 (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A);
280 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER);
281 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0;
284 rep += NUM_EDGES;
286 ee->ee_numCtls = i;
287 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
288 "%s Numctls = %u\n",__func__,i);
292 * Reclaim any EEPROM-related storage.
294 static void
295 v14EepromDetach(struct ath_hal *ah)
297 HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
299 ath_hal_free(ee);
300 AH_PRIVATE(ah)->ah_eeprom = AH_NULL;
303 #define owl_get_eep_ver(_ee) \
304 (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF)
305 #define owl_get_eep_rev(_ee) \
306 (((_ee)->ee_base.baseEepHeader.version) & 0xFFF)
308 HAL_STATUS
309 ath_hal_v14EepromAttach(struct ath_hal *ah)
311 #define NW(a) (sizeof(a) / sizeof(uint16_t))
312 HAL_EEPROM_v14 *ee = AH_PRIVATE(ah)->ah_eeprom;
313 uint16_t *eep_data, magic;
314 HAL_BOOL need_swap;
315 u_int w, off, len;
316 uint32_t sum;
318 HALASSERT(ee == AH_NULL);
320 if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
321 HALDEBUG(ah, HAL_DEBUG_ANY,
322 "%s Error reading Eeprom MAGIC\n", __func__);
323 return HAL_EEREAD;
325 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n",
326 __func__, magic);
327 if (magic != AR5416_EEPROM_MAGIC) {
328 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n");
329 return HAL_EEMAGIC;
332 ee = ath_hal_malloc(sizeof(HAL_EEPROM_v14));
333 if (ee == AH_NULL) {
334 /* XXX message */
335 return HAL_ENOMEM;
338 eep_data = (uint16_t *)&ee->ee_base;
339 for (w = 0; w < NW(struct ar5416eeprom); w++) {
340 off = owl_eep_start_loc + w; /* NB: AP71 starts at 0 */
341 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) {
342 HALDEBUG(ah, HAL_DEBUG_ANY,
343 "%s eeprom read error at offset 0x%x\n",
344 __func__, off);
345 return HAL_EEREAD;
348 /* Convert to eeprom native eeprom endian format */
349 if (isBigEndian()) {
350 for (w = 0; w < NW(struct ar5416eeprom); w++)
351 eep_data[w] = __bswap16(eep_data[w]);
355 * At this point, we're in the native eeprom endian format
356 * Now, determine the eeprom endian by looking at byte 26??
358 need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian();
359 if (need_swap) {
360 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
361 "Byte swap EEPROM contents.\n");
362 len = __bswap16(ee->ee_base.baseEepHeader.length);
363 } else {
364 len = ee->ee_base.baseEepHeader.length;
366 len = AH_MIN(len, sizeof(struct ar5416eeprom)) / sizeof(uint16_t);
368 /* Apply the checksum, done in native eeprom format */
369 /* XXX - Need to check to make sure checksum calculation is done
370 * in the correct endian format. Right now, it seems it would
371 * cast the raw data to host format and do the calculation, which may
372 * not be correct as the calculation may need to be done in the native
373 * eeprom format
375 sum = 0;
376 for (w = 0; w < len; w++)
377 sum ^= eep_data[w];
378 /* Check CRC - Attach should fail on a bad checksum */
379 if (sum != 0xffff) {
380 HALDEBUG(ah, HAL_DEBUG_ANY,
381 "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len);
382 return HAL_EEBADSUM;
385 if (need_swap)
386 eepromSwap(&ee->ee_base); /* byte swap multi-byte data */
388 /* swap words 0+2 so version is at the front */
389 magic = eep_data[0];
390 eep_data[0] = eep_data[2];
391 eep_data[2] = magic;
393 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM,
394 "%s Eeprom Version %u.%u\n", __func__,
395 owl_get_eep_ver(ee), owl_get_eep_rev(ee));
397 /* NB: must be after all byte swapping */
398 if (owl_get_eep_ver(ee) != AR5416_EEP_VER) {
399 HALDEBUG(ah, HAL_DEBUG_ANY,
400 "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee));
401 return HAL_EEBADSUM;
404 v14EepromReadCTLInfo(ah, ee); /* Get CTLs */
406 AH_PRIVATE(ah)->ah_eeprom = ee;
407 AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version;
408 AH_PRIVATE(ah)->ah_eepromDetach = v14EepromDetach;
409 AH_PRIVATE(ah)->ah_eepromGet = v14EepromGet;
410 AH_PRIVATE(ah)->ah_eepromSet = v14EepromSet;
411 AH_PRIVATE(ah)->ah_getSpurChan = v14EepromGetSpurChan;
412 AH_PRIVATE(ah)->ah_eepromDiag = v14EepromDiag;
413 return HAL_OK;
414 #undef NW