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