1 /* 2 * Copyright (c) 2009 Rui Paulo <rpaulo@FreeBSD.org> 3 * Copyright (c) 2008 Sam Leffler, Errno Consulting 4 * Copyright (c) 2008 Atheros Communications, Inc. 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * $FreeBSD$ 19 */ 20 #include "opt_ah.h" 21 22 #include "ah.h" 23 #include "ah_internal.h" 24 #include "ah_eeprom_v14.h" 25 #include "ah_eeprom_v4k.h" 26 27 static HAL_STATUS 28 v4kEepromGet(struct ath_hal *ah, int param, void *val) 29 { 30 #define CHAN_A_IDX 0 31 #define CHAN_B_IDX 1 32 #define IS_VERS(op, v) ((pBase->version & AR5416_EEP_VER_MINOR_MASK) op (v)) 33 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 34 const MODAL_EEP4K_HEADER *pModal = &ee->ee_base.modalHeader; 35 const BASE_EEP4K_HEADER *pBase = &ee->ee_base.baseEepHeader; 36 uint32_t sum; 37 uint8_t *macaddr; 38 int i; 39 40 switch (param) { 41 case AR_EEP_NFTHRESH_2: 42 *(int16_t *)val = pModal->noiseFloorThreshCh[0]; 43 return HAL_OK; 44 case AR_EEP_MACADDR: /* Get MAC Address */ 45 sum = 0; 46 macaddr = val; 47 for (i = 0; i < 6; i++) { 48 macaddr[i] = pBase->macAddr[i]; 49 sum += pBase->macAddr[i]; 50 } 51 if (sum == 0 || sum == 0xffff*3) { 52 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: bad mac address %s\n", 53 __func__, ath_hal_ether_sprintf(macaddr)); 54 return HAL_EEBADMAC; 55 } 56 return HAL_OK; 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_2: 68 return pModal->ob; 69 case AR_EEP_DB_2: 70 return pModal->db; 71 case AR_EEP_TXMASK: 72 return pBase->txMask; 73 case AR_EEP_RXMASK: 74 return pBase->rxMask; 75 case AR_EEP_RXGAIN_TYPE: 76 return AR5416_EEP_RXGAIN_ORIG; 77 case AR_EEP_TXGAIN_TYPE: 78 return IS_VERS(>=, AR5416_EEP_MINOR_VER_19) ? 79 pBase->txGainType : AR5416_EEP_TXGAIN_ORIG; 80 case AR_EEP_OL_PWRCTRL: 81 HALASSERT(val == AH_NULL); 82 return HAL_EIO; 83 case AR_EEP_AMODE: 84 HALASSERT(val == AH_NULL); 85 return pBase->opCapFlags & AR5416_OPFLAGS_11A ? 86 HAL_OK : HAL_EIO; 87 case AR_EEP_BMODE: 88 case AR_EEP_GMODE: 89 HALASSERT(val == AH_NULL); 90 return pBase->opCapFlags & AR5416_OPFLAGS_11G ? 91 HAL_OK : HAL_EIO; 92 case AR_EEP_32KHZCRYSTAL: 93 case AR_EEP_COMPRESS: 94 case AR_EEP_FASTFRAME: /* XXX policy decision, h/w can do it */ 95 case AR_EEP_WRITEPROTECT: /* NB: no write protect bit */ 96 HALASSERT(val == AH_NULL); 97 /* fall thru... */ 98 case AR_EEP_MAXQCU: /* NB: not in opCapFlags */ 99 case AR_EEP_KCENTRIES: /* NB: not in opCapFlags */ 100 return HAL_EIO; 101 case AR_EEP_AES: 102 case AR_EEP_BURST: 103 case AR_EEP_RFKILL: 104 case AR_EEP_TURBO2DISABLE: 105 HALASSERT(val == AH_NULL); 106 return HAL_OK; 107 case AR_EEP_ANTGAINMAX_2: 108 *(int8_t *) val = ee->ee_antennaGainMax; 109 return HAL_OK; 110 default: 111 HALASSERT(0); 112 return HAL_EINVAL; 113 } 114 #undef IS_VERS 115 #undef CHAN_A_IDX 116 #undef CHAN_B_IDX 117 } 118 119 static HAL_BOOL 120 v4kEepromSet(struct ath_hal *ah, int param, int v) 121 { 122 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 123 124 switch (param) { 125 case AR_EEP_ANTGAINMAX_2: 126 ee->ee_antennaGainMax = (int8_t) v; 127 return HAL_OK; 128 } 129 return HAL_EINVAL; 130 } 131 132 static HAL_BOOL 133 v4kEepromDiag(struct ath_hal *ah, int request, 134 const void *args, uint32_t argsize, void **result, uint32_t *resultsize) 135 { 136 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 137 138 switch (request) { 139 case HAL_DIAG_EEPROM: 140 *result = &ee->ee_base; 141 *resultsize = sizeof(ee->ee_base); 142 return AH_TRUE; 143 } 144 return AH_FALSE; 145 } 146 147 /* Do structure specific swaps if Eeprom format is non native to host */ 148 static void 149 eepromSwap(struct ar5416eeprom_4k *ee) 150 { 151 uint32_t integer, i; 152 uint16_t word; 153 MODAL_EEP4K_HEADER *pModal; 154 155 /* convert Base Eep header */ 156 word = __bswap16(ee->baseEepHeader.length); 157 ee->baseEepHeader.length = word; 158 159 word = __bswap16(ee->baseEepHeader.checksum); 160 ee->baseEepHeader.checksum = word; 161 162 word = __bswap16(ee->baseEepHeader.version); 163 ee->baseEepHeader.version = word; 164 165 word = __bswap16(ee->baseEepHeader.regDmn[0]); 166 ee->baseEepHeader.regDmn[0] = word; 167 168 word = __bswap16(ee->baseEepHeader.regDmn[1]); 169 ee->baseEepHeader.regDmn[1] = word; 170 171 word = __bswap16(ee->baseEepHeader.rfSilent); 172 ee->baseEepHeader.rfSilent = word; 173 174 word = __bswap16(ee->baseEepHeader.blueToothOptions); 175 ee->baseEepHeader.blueToothOptions = word; 176 177 word = __bswap16(ee->baseEepHeader.deviceCap); 178 ee->baseEepHeader.deviceCap = word; 179 180 /* convert Modal Eep header */ 181 pModal = &ee->modalHeader; 182 183 /* XXX linux/ah_osdep.h only defines __bswap32 for BE */ 184 integer = __bswap32(pModal->antCtrlCommon); 185 pModal->antCtrlCommon = integer; 186 187 for (i = 0; i < AR5416_4K_MAX_CHAINS; i++) { 188 integer = __bswap32(pModal->antCtrlChain[i]); 189 pModal->antCtrlChain[i] = integer; 190 } 191 192 for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) { 193 word = __bswap16(pModal->spurChans[i].spurChan); 194 pModal->spurChans[i].spurChan = word; 195 } 196 } 197 198 static uint16_t 199 v4kEepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz) 200 { 201 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 202 203 HALASSERT(0 <= ix && ix < AR5416_EEPROM_MODAL_SPURS); 204 HALASSERT(is2GHz); 205 return ee->ee_base.modalHeader.spurChans[ix].spurChan; 206 } 207 208 /************************************************************************** 209 * fbin2freq 210 * 211 * Get channel value from binary representation held in eeprom 212 * RETURNS: the frequency in MHz 213 */ 214 static uint16_t 215 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz) 216 { 217 /* 218 * Reserved value 0xFF provides an empty definition both as 219 * an fbin and as a frequency - do not convert 220 */ 221 if (fbin == AR5416_BCHAN_UNUSED) 222 return fbin; 223 return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin)); 224 } 225 226 /* 227 * Copy EEPROM Conformance Testing Limits contents 228 * into the allocated space 229 */ 230 /* USE CTLS from chain zero */ 231 #define CTL_CHAIN 0 232 233 static void 234 v4kEepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v4k *ee) 235 { 236 RD_EDGES_POWER *rep = ee->ee_rdEdgesPower; 237 int i, j; 238 239 HALASSERT(AR5416_4K_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES); 240 241 for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_4K_NUM_CTLS; i++) { 242 for (j = 0; j < NUM_EDGES; j ++) { 243 /* XXX Confirm this is the right thing to do when an invalid channel is stored */ 244 if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) { 245 rep[j].rdEdge = 0; 246 rep[j].twice_rdEdgePower = 0; 247 rep[j].flag = 0; 248 } else { 249 rep[j].rdEdge = fbin2freq( 250 ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel, 251 (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A); 252 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER); 253 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0; 254 } 255 } 256 rep += NUM_EDGES; 257 } 258 ee->ee_numCtls = i; 259 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 260 "%s Numctls = %u\n",__func__,i); 261 } 262 263 /* 264 * Reclaim any EEPROM-related storage. 265 */ 266 static void 267 v4kEepromDetach(struct ath_hal *ah) 268 { 269 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 270 271 ath_hal_free(ee); 272 AH_PRIVATE(ah)->ah_eeprom = AH_NULL; 273 } 274 275 #define owl_get_eep_ver(_ee) \ 276 (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF) 277 #define owl_get_eep_rev(_ee) \ 278 (((_ee)->ee_base.baseEepHeader.version) & 0xFFF) 279 280 HAL_STATUS 281 ath_hal_v4kEepromAttach(struct ath_hal *ah) 282 { 283 #define NW(a) (sizeof(a) / sizeof(uint16_t)) 284 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 285 uint16_t *eep_data, magic; 286 HAL_BOOL need_swap; 287 u_int w, off, len; 288 uint32_t sum; 289 290 HALASSERT(ee == AH_NULL); 291 292 if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) { 293 HALDEBUG(ah, HAL_DEBUG_ANY, 294 "%s Error reading Eeprom MAGIC\n", __func__); 295 return HAL_EEREAD; 296 } 297 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n", 298 __func__, magic); 299 if (magic != AR5416_EEPROM_MAGIC) { 300 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n"); 301 return HAL_EEMAGIC; 302 } 303 304 ee = ath_hal_malloc(sizeof(HAL_EEPROM_v4k)); 305 if (ee == AH_NULL) { 306 /* XXX message */ 307 return HAL_ENOMEM; 308 } 309 310 eep_data = (uint16_t *)&ee->ee_base; 311 for (w = 0; w < NW(struct ar5416eeprom_4k); w++) { 312 off = owl_eep_start_loc + w; /* NB: AP71 starts at 0 */ 313 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) { 314 HALDEBUG(ah, HAL_DEBUG_ANY, 315 "%s eeprom read error at offset 0x%x\n", 316 __func__, off); 317 return HAL_EEREAD; 318 } 319 } 320 /* Convert to eeprom native eeprom endian format */ 321 if (isBigEndian()) { 322 for (w = 0; w < NW(struct ar5416eeprom_4k); w++) 323 eep_data[w] = __bswap16(eep_data[w]); 324 } 325 326 /* 327 * At this point, we're in the native eeprom endian format 328 * Now, determine the eeprom endian by looking at byte 26?? 329 */ 330 need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian(); 331 if (need_swap) { 332 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 333 "Byte swap EEPROM contents.\n"); 334 len = __bswap16(ee->ee_base.baseEepHeader.length); 335 } else { 336 len = ee->ee_base.baseEepHeader.length; 337 } 338 len = AH_MIN(len, sizeof(struct ar5416eeprom_4k)) / sizeof(uint16_t); 339 340 /* Apply the checksum, done in native eeprom format */ 341 /* XXX - Need to check to make sure checksum calculation is done 342 * in the correct endian format. Right now, it seems it would 343 * cast the raw data to host format and do the calculation, which may 344 * not be correct as the calculation may need to be done in the native 345 * eeprom format 346 */ 347 sum = 0; 348 for (w = 0; w < len; w++) { 349 sum ^= eep_data[w]; 350 } 351 /* Check CRC - Attach should fail on a bad checksum */ 352 if (sum != 0xffff) { 353 HALDEBUG(ah, HAL_DEBUG_ANY, 354 "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len); 355 return HAL_EEBADSUM; 356 } 357 358 if (need_swap) 359 eepromSwap(&ee->ee_base); /* byte swap multi-byte data */ 360 361 /* swap words 0+2 so version is at the front */ 362 magic = eep_data[0]; 363 eep_data[0] = eep_data[2]; 364 eep_data[2] = magic; 365 366 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 367 "%s Eeprom Version %u.%u\n", __func__, 368 owl_get_eep_ver(ee), owl_get_eep_rev(ee)); 369 370 /* NB: must be after all byte swapping */ 371 if (owl_get_eep_ver(ee) != AR5416_EEP_VER) { 372 HALDEBUG(ah, HAL_DEBUG_ANY, 373 "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee)); 374 return HAL_EEBADSUM; 375 } 376 377 v4kEepromReadCTLInfo(ah, ee); /* Get CTLs */ 378 379 AH_PRIVATE(ah)->ah_eeprom = ee; 380 AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version; 381 AH_PRIVATE(ah)->ah_eepromDetach = v4kEepromDetach; 382 AH_PRIVATE(ah)->ah_eepromGet = v4kEepromGet; 383 AH_PRIVATE(ah)->ah_eepromSet = v4kEepromSet; 384 AH_PRIVATE(ah)->ah_getSpurChan = v4kEepromGetSpurChan; 385 AH_PRIVATE(ah)->ah_eepromDiag = v4kEepromDiag; 386 return HAL_OK; 387 #undef NW 388 } 389