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