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_0; 69 case AR_EEP_DB_2: 70 return pModal->db1_1; 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 pBase->txGainType; 79 case AR_EEP_OL_PWRCTRL: 80 HALASSERT(val == AH_NULL); 81 return HAL_EIO; 82 case AR_EEP_AMODE: 83 HALASSERT(val == AH_NULL); 84 return pBase->opCapFlags & AR5416_OPFLAGS_11A ? 85 HAL_OK : HAL_EIO; 86 case AR_EEP_BMODE: 87 case AR_EEP_GMODE: 88 HALASSERT(val == AH_NULL); 89 return pBase->opCapFlags & AR5416_OPFLAGS_11G ? 90 HAL_OK : HAL_EIO; 91 case AR_EEP_32KHZCRYSTAL: 92 case AR_EEP_COMPRESS: 93 case AR_EEP_FASTFRAME: /* XXX policy decision, h/w can do it */ 94 case AR_EEP_WRITEPROTECT: /* NB: no write protect bit */ 95 HALASSERT(val == AH_NULL); 96 /* fall thru... */ 97 case AR_EEP_MAXQCU: /* NB: not in opCapFlags */ 98 case AR_EEP_KCENTRIES: /* NB: not in opCapFlags */ 99 return HAL_EIO; 100 case AR_EEP_AES: 101 case AR_EEP_BURST: 102 case AR_EEP_RFKILL: 103 case AR_EEP_TURBO2DISABLE: 104 HALASSERT(val == AH_NULL); 105 return HAL_OK; 106 case AR_EEP_ANTGAINMAX_2: 107 *(int8_t *) val = ee->ee_antennaGainMax; 108 return HAL_OK; 109 default: 110 HALASSERT(0); 111 return HAL_EINVAL; 112 } 113 #undef IS_VERS 114 #undef CHAN_A_IDX 115 #undef CHAN_B_IDX 116 } 117 118 static HAL_STATUS 119 v4kEepromSet(struct ath_hal *ah, int param, int v) 120 { 121 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 122 123 switch (param) { 124 case AR_EEP_ANTGAINMAX_2: 125 ee->ee_antennaGainMax = (int8_t) v; 126 return HAL_OK; 127 } 128 return HAL_EINVAL; 129 } 130 131 static HAL_BOOL 132 v4kEepromDiag(struct ath_hal *ah, int request, 133 const void *args, uint32_t argsize, void **result, uint32_t *resultsize) 134 { 135 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 136 137 switch (request) { 138 case HAL_DIAG_EEPROM: 139 *result = ee; 140 *resultsize = sizeof(HAL_EEPROM_v4k); 141 return AH_TRUE; 142 } 143 return AH_FALSE; 144 } 145 146 /* Do structure specific swaps if Eeprom format is non native to host */ 147 static void 148 eepromSwap(struct ar5416eeprom_4k *ee) 149 { 150 uint32_t integer, i; 151 uint16_t word; 152 MODAL_EEP4K_HEADER *pModal; 153 154 /* convert Base Eep header */ 155 word = __bswap16(ee->baseEepHeader.length); 156 ee->baseEepHeader.length = word; 157 158 word = __bswap16(ee->baseEepHeader.checksum); 159 ee->baseEepHeader.checksum = word; 160 161 word = __bswap16(ee->baseEepHeader.version); 162 ee->baseEepHeader.version = word; 163 164 word = __bswap16(ee->baseEepHeader.regDmn[0]); 165 ee->baseEepHeader.regDmn[0] = word; 166 167 word = __bswap16(ee->baseEepHeader.regDmn[1]); 168 ee->baseEepHeader.regDmn[1] = word; 169 170 word = __bswap16(ee->baseEepHeader.rfSilent); 171 ee->baseEepHeader.rfSilent = word; 172 173 word = __bswap16(ee->baseEepHeader.blueToothOptions); 174 ee->baseEepHeader.blueToothOptions = word; 175 176 word = __bswap16(ee->baseEepHeader.deviceCap); 177 ee->baseEepHeader.deviceCap = word; 178 179 /* convert Modal Eep header */ 180 pModal = &ee->modalHeader; 181 182 /* XXX linux/ah_osdep.h only defines __bswap32 for BE */ 183 integer = __bswap32(pModal->antCtrlCommon); 184 pModal->antCtrlCommon = integer; 185 186 for (i = 0; i < AR5416_4K_MAX_CHAINS; i++) { 187 integer = __bswap32(pModal->antCtrlChain[i]); 188 pModal->antCtrlChain[i] = integer; 189 } 190 191 for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) { 192 word = __bswap16(pModal->spurChans[i].spurChan); 193 pModal->spurChans[i].spurChan = word; 194 } 195 } 196 197 static uint16_t 198 v4kEepromGetSpurChan(struct ath_hal *ah, int ix, HAL_BOOL is2GHz) 199 { 200 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 201 202 HALASSERT(0 <= ix && ix < AR5416_EEPROM_MODAL_SPURS); 203 HALASSERT(is2GHz); 204 return ee->ee_base.modalHeader.spurChans[ix].spurChan; 205 } 206 207 /************************************************************************** 208 * fbin2freq 209 * 210 * Get channel value from binary representation held in eeprom 211 * RETURNS: the frequency in MHz 212 */ 213 static uint16_t 214 fbin2freq(uint8_t fbin, HAL_BOOL is2GHz) 215 { 216 /* 217 * Reserved value 0xFF provides an empty definition both as 218 * an fbin and as a frequency - do not convert 219 */ 220 if (fbin == AR5416_BCHAN_UNUSED) 221 return fbin; 222 return (uint16_t)((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin)); 223 } 224 225 /* 226 * Copy EEPROM Conformance Testing Limits contents 227 * into the allocated space 228 */ 229 /* USE CTLS from chain zero */ 230 #define CTL_CHAIN 0 231 232 static void 233 v4kEepromReadCTLInfo(struct ath_hal *ah, HAL_EEPROM_v4k *ee) 234 { 235 RD_EDGES_POWER *rep = ee->ee_rdEdgesPower; 236 int i, j; 237 238 HALASSERT(AR5416_4K_NUM_CTLS <= sizeof(ee->ee_rdEdgesPower)/NUM_EDGES); 239 240 for (i = 0; ee->ee_base.ctlIndex[i] != 0 && i < AR5416_4K_NUM_CTLS; i++) { 241 for (j = 0; j < NUM_EDGES; j ++) { 242 /* XXX Confirm this is the right thing to do when an invalid channel is stored */ 243 if (ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel == AR5416_BCHAN_UNUSED) { 244 rep[j].rdEdge = 0; 245 rep[j].twice_rdEdgePower = 0; 246 rep[j].flag = 0; 247 } else { 248 rep[j].rdEdge = fbin2freq( 249 ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].bChannel, 250 (ee->ee_base.ctlIndex[i] & CTL_MODE_M) != CTL_11A); 251 rep[j].twice_rdEdgePower = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_POWER); 252 rep[j].flag = MS(ee->ee_base.ctlData[i].ctlEdges[CTL_CHAIN][j].tPowerFlag, CAL_CTL_EDGES_FLAG) != 0; 253 } 254 } 255 rep += NUM_EDGES; 256 } 257 ee->ee_numCtls = i; 258 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 259 "%s Numctls = %u\n",__func__,i); 260 } 261 262 /* 263 * Reclaim any EEPROM-related storage. 264 */ 265 static void 266 v4kEepromDetach(struct ath_hal *ah) 267 { 268 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 269 270 ath_hal_free(ee); 271 AH_PRIVATE(ah)->ah_eeprom = AH_NULL; 272 } 273 274 #define owl_get_eep_ver(_ee) \ 275 (((_ee)->ee_base.baseEepHeader.version >> 12) & 0xF) 276 #define owl_get_eep_rev(_ee) \ 277 (((_ee)->ee_base.baseEepHeader.version) & 0xFFF) 278 279 HAL_STATUS 280 ath_hal_v4kEepromAttach(struct ath_hal *ah) 281 { 282 #define NW(a) (sizeof(a) / sizeof(uint16_t)) 283 HAL_EEPROM_v4k *ee = AH_PRIVATE(ah)->ah_eeprom; 284 uint16_t *eep_data, magic; 285 HAL_BOOL need_swap; 286 u_int w, off, len; 287 uint32_t sum; 288 289 HALASSERT(ee == AH_NULL); 290 /* 291 * Don't check magic if we're supplied with an EEPROM block, 292 * typically this is from Howl but it may also be from later 293 * boards w/ an embedded WMAC. 294 */ 295 if (ah->ah_eepromdata == NULL) { 296 if (!ath_hal_eepromRead(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) { 297 HALDEBUG(ah, HAL_DEBUG_ANY, 298 "%s Error reading Eeprom MAGIC\n", __func__); 299 return HAL_EEREAD; 300 } 301 HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s Eeprom Magic = 0x%x\n", 302 __func__, magic); 303 if (magic != AR5416_EEPROM_MAGIC) { 304 HALDEBUG(ah, HAL_DEBUG_ANY, "Bad magic number\n"); 305 return HAL_EEMAGIC; 306 } 307 } 308 309 ee = ath_hal_malloc(sizeof(HAL_EEPROM_v4k)); 310 if (ee == AH_NULL) { 311 /* XXX message */ 312 return HAL_ENOMEM; 313 } 314 315 eep_data = (uint16_t *)&ee->ee_base; 316 for (w = 0; w < NW(struct ar5416eeprom_4k); w++) { 317 off = owl_eep_start_loc + w; /* NB: AP71 starts at 0 */ 318 if (!ath_hal_eepromRead(ah, off, &eep_data[w])) { 319 HALDEBUG(ah, HAL_DEBUG_ANY, 320 "%s eeprom read error at offset 0x%x\n", 321 __func__, off); 322 return HAL_EEREAD; 323 } 324 } 325 /* Convert to eeprom native eeprom endian format */ 326 /* 327 * XXX this is likely incorrect but will do for now 328 * XXX to get embedded boards working. 329 */ 330 if (ah->ah_eepromdata == NULL && isBigEndian()) { 331 for (w = 0; w < NW(struct ar5416eeprom_4k); w++) 332 eep_data[w] = __bswap16(eep_data[w]); 333 } 334 335 /* 336 * At this point, we're in the native eeprom endian format 337 * Now, determine the eeprom endian by looking at byte 26?? 338 */ 339 need_swap = ((ee->ee_base.baseEepHeader.eepMisc & AR5416_EEPMISC_BIG_ENDIAN) != 0) ^ isBigEndian(); 340 if (need_swap) { 341 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 342 "Byte swap EEPROM contents.\n"); 343 len = __bswap16(ee->ee_base.baseEepHeader.length); 344 } else { 345 len = ee->ee_base.baseEepHeader.length; 346 } 347 len = AH_MIN(len, sizeof(struct ar5416eeprom_4k)) / sizeof(uint16_t); 348 349 /* Apply the checksum, done in native eeprom format */ 350 /* XXX - Need to check to make sure checksum calculation is done 351 * in the correct endian format. Right now, it seems it would 352 * cast the raw data to host format and do the calculation, which may 353 * not be correct as the calculation may need to be done in the native 354 * eeprom format 355 */ 356 sum = 0; 357 for (w = 0; w < len; w++) { 358 sum ^= eep_data[w]; 359 } 360 /* Check CRC - Attach should fail on a bad checksum */ 361 if (sum != 0xffff) { 362 HALDEBUG(ah, HAL_DEBUG_ANY, 363 "Bad EEPROM checksum 0x%x (Len=%u)\n", sum, len); 364 return HAL_EEBADSUM; 365 } 366 367 if (need_swap) 368 eepromSwap(&ee->ee_base); /* byte swap multi-byte data */ 369 370 /* swap words 0+2 so version is at the front */ 371 magic = eep_data[0]; 372 eep_data[0] = eep_data[2]; 373 eep_data[2] = magic; 374 375 HALDEBUG(ah, HAL_DEBUG_ATTACH | HAL_DEBUG_EEPROM, 376 "%s Eeprom Version %u.%u\n", __func__, 377 owl_get_eep_ver(ee), owl_get_eep_rev(ee)); 378 379 /* NB: must be after all byte swapping */ 380 if (owl_get_eep_ver(ee) != AR5416_EEP_VER) { 381 HALDEBUG(ah, HAL_DEBUG_ANY, 382 "Bad EEPROM version 0x%x\n", owl_get_eep_ver(ee)); 383 return HAL_EEBADSUM; 384 } 385 386 v4kEepromReadCTLInfo(ah, ee); /* Get CTLs */ 387 388 AH_PRIVATE(ah)->ah_eeprom = ee; 389 AH_PRIVATE(ah)->ah_eeversion = ee->ee_base.baseEepHeader.version; 390 AH_PRIVATE(ah)->ah_eepromDetach = v4kEepromDetach; 391 AH_PRIVATE(ah)->ah_eepromGet = v4kEepromGet; 392 AH_PRIVATE(ah)->ah_eepromSet = v4kEepromSet; 393 AH_PRIVATE(ah)->ah_getSpurChan = v4kEepromGetSpurChan; 394 AH_PRIVATE(ah)->ah_eepromDiag = v4kEepromDiag; 395 return HAL_OK; 396 #undef NW 397 } 398