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 /* WARNING! must check i bound before indexing the array */ 252 for (i = 0; i < AR9287_NUM_CTLS && ee->ee_base.ctlIndex[i] != 0; i++) { 253 for (j = 0; j < AR9287_NUM_BAND_EDGES; j++) { 254 /* XXX Confirm this is the right thing to do when an invalid channel is stored */ 255 if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) { 256 rep[j].rdEdge = 0; 257 rep[j].twice_rdEdgePower = 0; 258 rep[j].flag = 0; 259 } else { 260 rep[j].rdEdge = fbin2freq( 261 ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel, 262 (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A); 263 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER); 264 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0; 265 } 266 } 267 rep += NUM_EDGES; 268 } 269 ee->ee_numCtls = i; 270 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 271 "%s Numctls = %u\n",__func__,i); 272 } 273 274 /* 275 * Reclaim any EEPROM-related storage. 276 */ 277 static void 278 v9287EepromDetach(struct ath_hal *ah) 279 { 280 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom; 281 282 ath_hal_free(ee); 283 AH_PRIVATE(ah)->ah_eeprom = AH_NULL; 284 } 285 286 #define owl_get_eep_ver(_ee) \ 287 (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF) 288 #define owl_get_eep_rev(_ee) \ 289 (((_ee)->ee_base.baseEepHeader.version) & 0xFFF) 290 291 HAL_STATUS 292 ath_hal_9287EepromAttach(struct ath_hal *ah) 293 { 294 #define NW(a) (sizeof(a) / sizeof(uint16_t)) 295 HAL_EEPROM_9287 *ee = AH_PRIVATE(ah)->ah_eeprom; 296 uint16_t *eep_data, magic; 297 HAL_BOOL need_swap; 298 u_int w, off, len; 299 uint32_t sum; 300 301 HALASSERT(ee == AH_NULL); 302 303 /* 304 * Don't check magic if we're supplied with an EEPROM block, 305 * typically this is from Howl but it may also be from later 306 * boards w/ an embedded WMAC. 307 */ 308 if (ah->ah_eepromdata == NULL) { 309 if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) { 310 HALDEBUG(ah, HAL_DEBUG_ANY, 311 "%s Error reading Eeprom MAGIC\n", __func__); 312 return HAL_EEREAD; 313 } 314 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n", 315 __func__, magic); 316 if (magic != AR5416_EEPROM_MAGIC) { 317 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n"); 318 return HAL_EEMAGIC; 319 } 320 } 321 322 ee = ath_hal_malloc(sizeof(HAL_EEPROM_9287)); 323 if (ee == AH_NULL) { 324 /* XXX message */ 325 return HAL_ENOMEM; 326 } 327 328 eep_data = (uint16_t *) ee; 329 for (w = 0; w < NW(struct ar9287_eeprom); w++) { 330 off = AR9287_EEP_START_LOC + w; 331 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) { 332 HALDEBUG(ah, HAL_DEBUG_ANY, 333 "%s eeprom read error at offset 0x%x\n", 334 __func__, off); 335 return HAL_EEREAD; 336 } 337 } 338 /* Convert to eeprom native eeprom endian format */ 339 /* 340 * XXX this is likely incorrect but will do for now 341 * XXX to get embedded boards working. 342 */ 343 if (ah->ah_eepromdata == NULL && isBigEndian()) { 344 for (w = 0; w < NW(HAL_EEPROM_9287); w++) 345 eep_data[w] = __bswap16(eep_data[w]); 346 } 347 348 /* 349 * At this point, we're in the native eeprom endian format 350 * Now, determine the eeprom endian by looking at byte 26?? 351 */ 352 need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian(); 353 if (need_swap) { 354 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 355 "Byte swap EEPROM contents.\n"); 356 len = __bswap16(ee->ee_base.baseEepHeader.length); 357 } else { 358 len = ee->ee_base.baseEepHeader.length; 359 } 360 len = AH_MIN(len, sizeof(HAL_EEPROM_9287)) / sizeof(uint16_t); 361 362 /* Apply the checksum, done in native eeprom format */ 363 /* XXX - Need to check to make sure checksum calculation is done 364 * in the correct endian format. Right now, it seems it would 365 * cast the raw data to host format and do the calculation, which may 366 * not be correct as the calculation may need to be done in the native 367 * eeprom format 368 */ 369 sum = 0; 370 for (w = 0; w < len; w++) 371 sum ^= eep_data[w]; 372 /* Check CRC - Attach should fail on a bad checksum */ 373 if (sum != 0xffff) { 374 HALDEBUG(ah, HAL_DEBUG_ANY, 375 "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len); 376 return HAL_EEBADSUM; 377 } 378 379 if (need_swap) 380 eepromSwap(ee); /* byte swap multi-byte data */ 381 382 /* swap words 0+2 so version is at the front */ 383 magic = eep_data[0]; 384 eep_data[0] = eep_data[2]; 385 eep_data[2] = magic; 386 387 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 388 "%s Eeprom Version %u.%u\n", __func__, 389 owl_get_eep_ver(ee), owl_get_eep_rev(ee)); 390 391 /* NB: must be after all byte swapping */ 392 if (owl_get_eep_ver(ee) != AR5416_EEP_VER) { 393 HALDEBUG(ah, HAL_DEBUG_ANY, 394 "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee)); 395 return HAL_EEBADSUM; 396 } 397 398 v9287EepromReadCTLInfo(ah, ee); /* Get CTLs */ 399 400 AH_PRIVATE(ah)->ah_eeprom = ee; 401 AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version; 402 AH_PRIVATE(ah)->ah_eepromDetach = v9287EepromDetach; 403 AH_PRIVATE(ah)->ah_eepromGet = v9287EepromGet; 404 AH_PRIVATE(ah)->ah_eepromSet = v9287EepromSet; 405 AH_PRIVATE(ah)->ah_getSpurChan = v9287EepromGetSpurChan; 406 AH_PRIVATE(ah)->ah_eepromDiag = v9287EepromDiag; 407 return HAL_OK; 408 #undef NW 409 } 410