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