eeprom.c 114.4 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2008-2009 Atheros Communications Inc.
S
Sujith 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

S
Sujith 已提交
17
#include "ath9k.h"
S
Sujith 已提交
18

19
static void ath9k_hw_analog_shift_rmw(struct ath_hw *ah,
S
Sujith 已提交
20 21 22 23 24 25 26 27 28 29
				      u32 reg, u32 mask,
				      u32 shift, u32 val)
{
	u32 regVal;

	regVal = REG_READ(ah, reg) & ~mask;
	regVal |= (val << shift) & mask;

	REG_WRITE(ah, reg, regVal);

30
	if (ah->config.analog_shiftreg)
S
Sujith 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		udelay(100);

	return;
}

static inline u16 ath9k_hw_fbin2freq(u8 fbin, bool is2GHz)
{

	if (fbin == AR5416_BCHAN_UNUSED)
		return fbin;

	return (u16) ((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
}

static inline int16_t ath9k_hw_interpolate(u16 target,
					   u16 srcLeft, u16 srcRight,
					   int16_t targetLeft,
					   int16_t targetRight)
{
	int16_t rv;

	if (srcRight == srcLeft) {
		rv = targetLeft;
	} else {
		rv = (int16_t) (((target - srcLeft) * targetRight +
				 (srcRight - target) * targetLeft) /
				(srcRight - srcLeft));
	}
	return rv;
}

static inline bool ath9k_hw_get_lower_upper_index(u8 target, u8 *pList,
						  u16 listSize, u16 *indexL,
						  u16 *indexR)
{
	u16 i;

	if (target <= pList[0]) {
		*indexL = *indexR = 0;
		return true;
	}
	if (target >= pList[listSize - 1]) {
		*indexL = *indexR = (u16) (listSize - 1);
		return true;
	}

	for (i = 0; i < listSize - 1; i++) {
		if (pList[i] == target) {
			*indexL = *indexR = i;
			return true;
		}
		if (target < pList[i + 1]) {
			*indexL = i;
			*indexR = (u16) (i + 1);
			return false;
		}
	}
	return false;
}

91
static inline bool ath9k_hw_nvram_read(struct ath_hw *ah, u32 off, u16 *data)
S
Sujith 已提交
92
{
93 94 95
	struct ath_softc *sc = ah->ah_sc;

	return sc->bus_ops->eeprom_read(ah, off, data);
S
Sujith 已提交
96 97
}

S
Sujith 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static inline bool ath9k_hw_fill_vpd_table(u8 pwrMin, u8 pwrMax, u8 *pPwrList,
					   u8 *pVpdList, u16 numIntercepts,
					   u8 *pRetVpdList)
{
	u16 i, k;
	u8 currPwr = pwrMin;
	u16 idxL = 0, idxR = 0;

	for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
		ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
					       numIntercepts, &(idxL),
					       &(idxR));
		if (idxR < 1)
			idxR = 1;
		if (idxL == numIntercepts - 1)
			idxL = (u16) (numIntercepts - 2);
		if (pPwrList[idxL] == pPwrList[idxR])
			k = pVpdList[idxL];
		else
			k = (u16)(((currPwr - pPwrList[idxL]) * pVpdList[idxR] +
				   (pPwrList[idxR] - currPwr) * pVpdList[idxL]) /
				  (pPwrList[idxR] - pPwrList[idxL]));
		pRetVpdList[i] = (u8) k;
		currPwr += 2;
	}

	return true;
}

static void ath9k_hw_get_legacy_target_powers(struct ath_hw *ah,
				      struct ath9k_channel *chan,
				      struct cal_target_power_leg *powInfo,
				      u16 numChannels,
				      struct cal_target_power_leg *pNewPower,
				      u16 numRates, bool isExtTarget)
{
	struct chan_centers centers;
	u16 clo, chi;
	int i;
	int matchIndex = -1, lowIndex = -1;
	u16 freq;

	ath9k_hw_get_channel_centers(ah, chan, &centers);
	freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;

	if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
				       IS_CHAN_2GHZ(chan))) {
		matchIndex = 0;
	} else {
		for (i = 0; (i < numChannels) &&
			     (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
			if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
						       IS_CHAN_2GHZ(chan))) {
				matchIndex = i;
				break;
			} else if ((freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
						      IS_CHAN_2GHZ(chan))) &&
				   (freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
						      IS_CHAN_2GHZ(chan)))) {
				lowIndex = i - 1;
				break;
			}
		}
		if ((matchIndex == -1) && (lowIndex == -1))
			matchIndex = i - 1;
	}

	if (matchIndex != -1) {
		*pNewPower = powInfo[matchIndex];
	} else {
		clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
					 IS_CHAN_2GHZ(chan));
		chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
					 IS_CHAN_2GHZ(chan));

		for (i = 0; i < numRates; i++) {
			pNewPower->tPow2x[i] =
				(u8)ath9k_hw_interpolate(freq, clo, chi,
						powInfo[lowIndex].tPow2x[i],
						powInfo[lowIndex + 1].tPow2x[i]);
		}
	}
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static void ath9k_get_txgain_index(struct ath_hw *ah,
		struct ath9k_channel *chan,
		struct calDataPerFreqOpLoop *rawDatasetOpLoop,
		u8 *calChans,  u16 availPiers, u8 *pwr, u8 *pcdacIdx)
{
	u8 pcdac, i = 0;
	u16 idxL = 0, idxR = 0, numPiers;
	bool match;
	struct chan_centers centers;

	ath9k_hw_get_channel_centers(ah, chan, &centers);

	for (numPiers = 0; numPiers < availPiers; numPiers++)
		if (calChans[numPiers] == AR5416_BCHAN_UNUSED)
			break;

	match = ath9k_hw_get_lower_upper_index(
			(u8)FREQ2FBIN(centers.synth_center, IS_CHAN_2GHZ(chan)),
			calChans, numPiers, &idxL, &idxR);
	if (match) {
		pcdac = rawDatasetOpLoop[idxL].pcdac[0][0];
		*pwr = rawDatasetOpLoop[idxL].pwrPdg[0][0];
	} else {
		pcdac = rawDatasetOpLoop[idxR].pcdac[0][0];
		*pwr = (rawDatasetOpLoop[idxL].pwrPdg[0][0] +
				rawDatasetOpLoop[idxR].pwrPdg[0][0])/2;
	}

	while (pcdac > ah->originalGain[i] &&
			i < (AR9280_TX_GAIN_TABLE_SIZE - 1))
		i++;

	*pcdacIdx = i;
	return;
}

static void ath9k_olc_get_pdadcs(struct ath_hw *ah,
				u32 initTxGain,
				int txPower,
				u8 *pPDADCValues)
{
	u32 i;
	u32 offset;

	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL6_0,
			AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL6_1,
			AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);

	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL7,
			AR_PHY_TX_PWRCTRL_INIT_TX_GAIN, initTxGain);

	offset = txPower;
	for (i = 0; i < AR5416_NUM_PDADC_VALUES; i++)
		if (i < offset)
			pPDADCValues[i] = 0x0;
		else
			pPDADCValues[i] = 0xFF;
}




S
Sujith 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
static void ath9k_hw_get_target_powers(struct ath_hw *ah,
				       struct ath9k_channel *chan,
				       struct cal_target_power_ht *powInfo,
				       u16 numChannels,
				       struct cal_target_power_ht *pNewPower,
				       u16 numRates, bool isHt40Target)
{
	struct chan_centers centers;
	u16 clo, chi;
	int i;
	int matchIndex = -1, lowIndex = -1;
	u16 freq;

	ath9k_hw_get_channel_centers(ah, chan, &centers);
	freq = isHt40Target ? centers.synth_center : centers.ctl_center;

	if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
		matchIndex = 0;
	} else {
		for (i = 0; (i < numChannels) &&
			     (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
			if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
						       IS_CHAN_2GHZ(chan))) {
				matchIndex = i;
				break;
			} else
				if ((freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
						       IS_CHAN_2GHZ(chan))) &&
				    (freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
						       IS_CHAN_2GHZ(chan)))) {
					lowIndex = i - 1;
					break;
				}
		}
		if ((matchIndex == -1) && (lowIndex == -1))
			matchIndex = i - 1;
	}

	if (matchIndex != -1) {
		*pNewPower = powInfo[matchIndex];
	} else {
		clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
					 IS_CHAN_2GHZ(chan));
		chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
					 IS_CHAN_2GHZ(chan));

		for (i = 0; i < numRates; i++) {
			pNewPower->tPow2x[i] = (u8)ath9k_hw_interpolate(freq,
						clo, chi,
						powInfo[lowIndex].tPow2x[i],
						powInfo[lowIndex + 1].tPow2x[i]);
		}
	}
}

static u16 ath9k_hw_get_max_edge_power(u16 freq,
				       struct cal_ctl_edges *pRdEdgesPower,
				       bool is2GHz, int num_band_edges)
{
	u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
	int i;

	for (i = 0; (i < num_band_edges) &&
		     (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
		if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel, is2GHz)) {
			twiceMaxEdgePower = pRdEdgesPower[i].tPower;
			break;
		} else if ((i > 0) &&
			   (freq < ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
						      is2GHz))) {
			if (ath9k_hw_fbin2freq(pRdEdgesPower[i - 1].bChannel,
					       is2GHz) < freq &&
			    pRdEdgesPower[i - 1].flag) {
				twiceMaxEdgePower =
					pRdEdgesPower[i - 1].tPower;
			}
			break;
		}
	}

	return twiceMaxEdgePower;
}

/****************************************/
/* EEPROM Operations for 4K sized cards */
/****************************************/

static int ath9k_hw_4k_get_eeprom_ver(struct ath_hw *ah)
{
334
	return ((ah->eeprom.map4k.baseEepHeader.version >> 12) & 0xF);
S
Sujith 已提交
335 336 337 338
}

static int ath9k_hw_4k_get_eeprom_rev(struct ath_hw *ah)
{
339
	return ((ah->eeprom.map4k.baseEepHeader.version) & 0xFFF);
S
Sujith 已提交
340 341 342
}

static bool ath9k_hw_4k_fill_eeprom(struct ath_hw *ah)
S
Sujith 已提交
343
{
344
#define SIZE_EEPROM_4K (sizeof(struct ar5416_eeprom_4k) / sizeof(u16))
345
	u16 *eep_data = (u16 *)&ah->eeprom.map4k;
346 347 348
	int addr, eep_start_loc = 0;

	eep_start_loc = 64;
S
Sujith 已提交
349 350 351

	if (!ath9k_hw_use_flash(ah)) {
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
352
			"Reading from EEPROM, not flash\n");
S
Sujith 已提交
353 354
	}

355 356 357 358 359 360 361 362
	for (addr = 0; addr < SIZE_EEPROM_4K; addr++) {
		if (!ath9k_hw_nvram_read(ah, addr + eep_start_loc, eep_data)) {
			DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
			       "Unable to read eeprom region \n");
			return false;
		}
		eep_data++;
	}
363

364 365 366 367
	return true;
#undef SIZE_EEPROM_4K
}

S
Sujith 已提交
368
static int ath9k_hw_4k_check_eeprom(struct ath_hw *ah)
S
Sujith 已提交
369
{
S
Sujith 已提交
370 371
#define EEPROM_4K_SIZE (sizeof(struct ar5416_eeprom_4k) / sizeof(u16))
	struct ar5416_eeprom_4k *eep =
372
		(struct ar5416_eeprom_4k *) &ah->eeprom.map4k;
S
Sujith 已提交
373 374 375
	u16 *eepdata, temp, magic, magic2;
	u32 sum = 0, el;
	bool need_swap = false;
S
Sujith 已提交
376
	int i, addr;
S
Sujith 已提交
377

378

S
Sujith 已提交
379
	if (!ath9k_hw_use_flash(ah)) {
S
Sujith 已提交
380 381
		if (!ath9k_hw_nvram_read(ah, AR5416_EEPROM_MAGIC_OFFSET,
					 &magic)) {
382
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
383 384 385 386
				"Reading Magic # failed\n");
			return false;
		}

387
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
388
			"Read Magic = 0x%04X\n", magic);
S
Sujith 已提交
389 390 391 392 393 394

		if (magic != AR5416_EEPROM_MAGIC) {
			magic2 = swab16(magic);

			if (magic2 == AR5416_EEPROM_MAGIC) {
				need_swap = true;
395
				eepdata = (u16 *) (&ah->eeprom);
S
Sujith 已提交
396

S
Sujith 已提交
397
				for (addr = 0; addr < EEPROM_4K_SIZE; addr++) {
S
Sujith 已提交
398 399 400 401 402
					temp = swab16(*eepdata);
					*eepdata = temp;
					eepdata++;
				}
			} else {
403
				DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
404 405 406 407 408 409 410 411 412 413 414
					"Invalid EEPROM Magic. "
					"endianness mismatch.\n");
				return -EINVAL;
			}
		}
	}

	DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "need_swap = %s.\n",
		need_swap ? "True" : "False");

	if (need_swap)
415
		el = swab16(ah->eeprom.map4k.baseEepHeader.length);
S
Sujith 已提交
416
	else
417
		el = ah->eeprom.map4k.baseEepHeader.length;
S
Sujith 已提交
418

419
	if (el > sizeof(struct ar5416_eeprom_4k))
S
Sujith 已提交
420
		el = sizeof(struct ar5416_eeprom_4k) / sizeof(u16);
S
Sujith 已提交
421 422 423
	else
		el = el / sizeof(u16);

424
	eepdata = (u16 *)(&ah->eeprom);
S
Sujith 已提交
425 426 427 428 429

	for (i = 0; i < el; i++)
		sum ^= *eepdata++;

	if (need_swap) {
S
Sujith 已提交
430
		u32 integer;
S
Sujith 已提交
431 432 433
		u16 word;

		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
434
			"EEPROM Endianness is not native.. Changing\n");
S
Sujith 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

		word = swab16(eep->baseEepHeader.length);
		eep->baseEepHeader.length = word;

		word = swab16(eep->baseEepHeader.checksum);
		eep->baseEepHeader.checksum = word;

		word = swab16(eep->baseEepHeader.version);
		eep->baseEepHeader.version = word;

		word = swab16(eep->baseEepHeader.regDmn[0]);
		eep->baseEepHeader.regDmn[0] = word;

		word = swab16(eep->baseEepHeader.regDmn[1]);
		eep->baseEepHeader.regDmn[1] = word;

		word = swab16(eep->baseEepHeader.rfSilent);
		eep->baseEepHeader.rfSilent = word;

		word = swab16(eep->baseEepHeader.blueToothOptions);
		eep->baseEepHeader.blueToothOptions = word;

		word = swab16(eep->baseEepHeader.deviceCap);
		eep->baseEepHeader.deviceCap = word;

S
Sujith 已提交
460 461
		integer = swab32(eep->modalHeader.antCtrlCommon);
		eep->modalHeader.antCtrlCommon = integer;
S
Sujith 已提交
462

R
Roel Kluin 已提交
463
		for (i = 0; i < AR5416_EEP4K_MAX_CHAINS; i++) {
S
Sujith 已提交
464 465 466
			integer = swab32(eep->modalHeader.antCtrlChain[i]);
			eep->modalHeader.antCtrlChain[i] = integer;
		}
S
Sujith 已提交
467

S
Sujith 已提交
468 469 470
		for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
			word = swab16(eep->modalHeader.spurChans[i].spurChan);
			eep->modalHeader.spurChans[i].spurChan = word;
S
Sujith 已提交
471 472 473
		}
	}

S
Sujith 已提交
474 475
	if (sum != 0xffff || ah->eep_ops->get_eeprom_ver(ah) != AR5416_EEP_VER ||
	    ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_NO_BACK_VER) {
476
		DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
477
			"Bad EEPROM checksum 0x%x or revision 0x%04x\n",
S
Sujith 已提交
478
			sum, ah->eep_ops->get_eeprom_ver(ah));
S
Sujith 已提交
479 480 481 482
		return -EINVAL;
	}

	return 0;
S
Sujith 已提交
483
#undef EEPROM_4K_SIZE
S
Sujith 已提交
484 485
}

S
Sujith 已提交
486 487
static u32 ath9k_hw_4k_get_eeprom(struct ath_hw *ah,
				  enum eeprom_param param)
488
{
489
	struct ar5416_eeprom_4k *eep = &ah->eeprom.map4k;
S
Sujith 已提交
490 491
	struct modal_eep_4k_header *pModal = &eep->modalHeader;
	struct base_eep_header_4k *pBase = &eep->baseEepHeader;
S
Sujith 已提交
492

S
Sujith 已提交
493 494
	switch (param) {
	case EEP_NFTHRESH_2:
495
		return pModal->noiseFloorThreshCh[0];
S
Sujith 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	case AR_EEPROM_MAC(0):
		return pBase->macAddr[0] << 8 | pBase->macAddr[1];
	case AR_EEPROM_MAC(1):
		return pBase->macAddr[2] << 8 | pBase->macAddr[3];
	case AR_EEPROM_MAC(2):
		return pBase->macAddr[4] << 8 | pBase->macAddr[5];
	case EEP_REG_0:
		return pBase->regDmn[0];
	case EEP_REG_1:
		return pBase->regDmn[1];
	case EEP_OP_CAP:
		return pBase->deviceCap;
	case EEP_OP_MODE:
		return pBase->opCapFlags;
	case EEP_RF_SILENT:
		return pBase->rfSilent;
	case EEP_OB_2:
		return pModal->ob_01;
	case EEP_DB_2:
		return pModal->db1_01;
	case EEP_MINOR_REV:
		return pBase->version & AR5416_EEP_VER_MINOR_MASK;
	case EEP_TX_MASK:
		return pBase->txMask;
	case EEP_RX_MASK:
		return pBase->rxMask;
S
Sujith 已提交
522 523
	case EEP_FRAC_N_5G:
		return 0;
S
Sujith 已提交
524 525
	default:
		return 0;
S
Sujith 已提交
526 527 528
	}
}

529
static void ath9k_hw_get_4k_gain_boundaries_pdadcs(struct ath_hw *ah,
S
Sujith 已提交
530
				struct ath9k_channel *chan,
531
				struct cal_data_per_freq_4k *pRawDataSet,
S
Sujith 已提交
532 533 534 535 536
				u8 *bChans, u16 availPiers,
				u16 tPdGainOverlap, int16_t *pMinCalPower,
				u16 *pPdGainBoundaries, u8 *pPDADCValues,
				u16 numXpdGains)
{
537 538
#define TMP_VAL_VPD_TABLE \
	((vpdTableI[i][sizeCurrVpdTable - 1] + (ss - maxIndex + 1) * vpdStep));
S
Sujith 已提交
539 540 541
	int i, j, k;
	int16_t ss;
	u16 idxL = 0, idxR = 0, numPiers;
542
	static u8 vpdTableL[AR5416_EEP4K_NUM_PD_GAINS]
S
Sujith 已提交
543
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
544
	static u8 vpdTableR[AR5416_EEP4K_NUM_PD_GAINS]
S
Sujith 已提交
545
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
546
	static u8 vpdTableI[AR5416_EEP4K_NUM_PD_GAINS]
S
Sujith 已提交
547 548 549
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];

	u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
550 551
	u8 minPwrT4[AR5416_EEP4K_NUM_PD_GAINS];
	u8 maxPwrT4[AR5416_EEP4K_NUM_PD_GAINS];
S
Sujith 已提交
552 553 554 555 556 557
	int16_t vpdStep;
	int16_t tmpVal;
	u16 sizeCurrVpdTable, maxIndex, tgtIndex;
	bool match;
	int16_t minDelta = 0;
	struct chan_centers centers;
558
#define PD_GAIN_BOUNDARY_DEFAULT 58;
S
Sujith 已提交
559 560 561 562 563 564 565 566

	ath9k_hw_get_channel_centers(ah, chan, &centers);

	for (numPiers = 0; numPiers < availPiers; numPiers++) {
		if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
			break;
	}

567 568 569 570
	match = ath9k_hw_get_lower_upper_index(
					(u8)FREQ2FBIN(centers.synth_center,
					IS_CHAN_2GHZ(chan)), bChans, numPiers,
					&idxL, &idxR);
S
Sujith 已提交
571 572 573 574 575 576 577 578

	if (match) {
		for (i = 0; i < numXpdGains; i++) {
			minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
			maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
					pRawDataSet[idxL].pwrPdg[i],
					pRawDataSet[idxL].vpdPdg[i],
579
					AR5416_EEP4K_PD_GAIN_ICEPTS,
S
Sujith 已提交
580 581 582 583 584 585 586 587 588 589 590 591
					vpdTableI[i]);
		}
	} else {
		for (i = 0; i < numXpdGains; i++) {
			pVpdL = pRawDataSet[idxL].vpdPdg[i];
			pPwrL = pRawDataSet[idxL].pwrPdg[i];
			pVpdR = pRawDataSet[idxR].vpdPdg[i];
			pPwrR = pRawDataSet[idxR].pwrPdg[i];

			minPwrT4[i] = max(pPwrL[0], pPwrR[0]);

			maxPwrT4[i] =
592 593
				min(pPwrL[AR5416_EEP4K_PD_GAIN_ICEPTS - 1],
				    pPwrR[AR5416_EEP4K_PD_GAIN_ICEPTS - 1]);
S
Sujith 已提交
594 595 596 597


			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
						pPwrL, pVpdL,
598
						AR5416_EEP4K_PD_GAIN_ICEPTS,
S
Sujith 已提交
599 600 601
						vpdTableL[i]);
			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
						pPwrR, pVpdR,
602
						AR5416_EEP4K_PD_GAIN_ICEPTS,
S
Sujith 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
						vpdTableR[i]);

			for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
				vpdTableI[i][j] =
					(u8)(ath9k_hw_interpolate((u16)
					     FREQ2FBIN(centers.
						       synth_center,
						       IS_CHAN_2GHZ
						       (chan)),
					     bChans[idxL], bChans[idxR],
					     vpdTableL[i][j], vpdTableR[i][j]));
			}
		}
	}

	*pMinCalPower = (int16_t)(minPwrT4[0] / 2);

	k = 0;

	for (i = 0; i < numXpdGains; i++) {
		if (i == (numXpdGains - 1))
			pPdGainBoundaries[i] =
				(u16)(maxPwrT4[i] / 2);
		else
			pPdGainBoundaries[i] =
				(u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);

		pPdGainBoundaries[i] =
			min((u16)AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);

633
		if ((i == 0) && !AR_SREV_5416_20_OR_LATER(ah)) {
S
Sujith 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
			minDelta = pPdGainBoundaries[0] - 23;
			pPdGainBoundaries[0] = 23;
		} else {
			minDelta = 0;
		}

		if (i == 0) {
			if (AR_SREV_9280_10_OR_LATER(ah))
				ss = (int16_t)(0 - (minPwrT4[i] / 2));
			else
				ss = 0;
		} else {
			ss = (int16_t)((pPdGainBoundaries[i - 1] -
					(minPwrT4[i] / 2)) -
				       tPdGainOverlap + 1 + minDelta);
		}
		vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
		vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);

		while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
			tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
			pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
			ss++;
		}

		sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
		tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
				(minPwrT4[i] / 2));
		maxIndex = (tgtIndex < sizeCurrVpdTable) ?
			tgtIndex : sizeCurrVpdTable;

665
		while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1)))
S
Sujith 已提交
666 667 668 669 670 671
			pPDADCValues[k++] = vpdTableI[i][ss++];

		vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
				    vpdTableI[i][sizeCurrVpdTable - 2]);
		vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);

672
		if (tgtIndex >= maxIndex) {
S
Sujith 已提交
673 674
			while ((ss <= tgtIndex) &&
			       (k < (AR5416_NUM_PDADC_VALUES - 1))) {
675
				tmpVal = (int16_t) TMP_VAL_VPD_TABLE;
S
Sujith 已提交
676 677 678 679 680 681 682
				pPDADCValues[k++] = (u8)((tmpVal > 255) ?
							 255 : tmpVal);
				ss++;
			}
		}
	}

683 684
	while (i < AR5416_EEP4K_PD_GAINS_IN_MASK) {
		pPdGainBoundaries[i] = PD_GAIN_BOUNDARY_DEFAULT;
S
Sujith 已提交
685 686 687 688 689 690 691 692 693
		i++;
	}

	while (k < AR5416_NUM_PDADC_VALUES) {
		pPDADCValues[k] = pPDADCValues[k - 1];
		k++;
	}

	return;
694
#undef TMP_VAL_VPD_TABLE
S
Sujith 已提交
695 696
}

697
static void ath9k_hw_set_4k_power_cal_table(struct ath_hw *ah,
S
Sujith 已提交
698 699
				  struct ath9k_channel *chan,
				  int16_t *pTxPowerIndexOffset)
S
Sujith 已提交
700
{
701
	struct ar5416_eeprom_4k *pEepData = &ah->eeprom.map4k;
S
Sujith 已提交
702 703 704 705
	struct cal_data_per_freq_4k *pRawDataset;
	u8 *pCalBChans = NULL;
	u16 pdGainOverlap_t2;
	static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
706
	u16 gainBoundaries[AR5416_EEP4K_PD_GAINS_IN_MASK];
S
Sujith 已提交
707 708 709
	u16 numPiers, i, j;
	int16_t tMinCalPower;
	u16 numXpdGain, xpdMask;
710
	u16 xpdGainValues[AR5416_EEP4K_NUM_PD_GAINS] = { 0, 0 };
S
Sujith 已提交
711
	u32 reg32, regOffset, regChainOffset;
S
Sujith 已提交
712

S
Sujith 已提交
713
	xpdMask = pEepData->modalHeader.xpdGain;
S
Sujith 已提交
714

S
Sujith 已提交
715 716 717 718 719 720 721
	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
	    AR5416_EEP_MINOR_VER_2) {
		pdGainOverlap_t2 =
			pEepData->modalHeader.pdGainOverlap;
	} else {
		pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
					    AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
S
Sujith 已提交
722 723
	}

S
Sujith 已提交
724
	pCalBChans = pEepData->calFreqPier2G;
725
	numPiers = AR5416_EEP4K_NUM_2G_CAL_PIERS;
S
Sujith 已提交
726

S
Sujith 已提交
727
	numXpdGain = 0;
728

729 730 731
	for (i = 1; i <= AR5416_EEP4K_PD_GAINS_IN_MASK; i++) {
		if ((xpdMask >> (AR5416_EEP4K_PD_GAINS_IN_MASK - i)) & 1) {
			if (numXpdGain >= AR5416_EEP4K_NUM_PD_GAINS)
S
Sujith 已提交
732 733
				break;
			xpdGainValues[numXpdGain] =
734
				(u16)(AR5416_EEP4K_PD_GAINS_IN_MASK - i);
S
Sujith 已提交
735
			numXpdGain++;
736 737 738
		}
	}

S
Sujith 已提交
739 740 741 742 743 744
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
		      (numXpdGain - 1) & 0x3);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
		      xpdGainValues[0]);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
		      xpdGainValues[1]);
745
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3, 0);
746

747
	for (i = 0; i < AR5416_EEP4K_MAX_CHAINS; i++) {
748
		if (AR_SREV_5416_20_OR_LATER(ah) &&
749
		    (ah->rxchainmask == 5 || ah->txchainmask == 5) &&
S
Sujith 已提交
750 751 752 753
		    (i != 0)) {
			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
		} else
			regChainOffset = i * 0x1000;
754

S
Sujith 已提交
755 756
		if (pEepData->baseEepHeader.txMask & (1 << i)) {
			pRawDataset = pEepData->calPierData2G[i];
757

S
Sujith 已提交
758 759 760 761 762
			ath9k_hw_get_4k_gain_boundaries_pdadcs(ah, chan,
					    pRawDataset, pCalBChans,
					    numPiers, pdGainOverlap_t2,
					    &tMinCalPower, gainBoundaries,
					    pdadcValues, numXpdGain);
763

764
			if ((i == 0) || AR_SREV_5416_20_OR_LATER(ah)) {
S
Sujith 已提交
765 766 767 768 769 770 771 772 773 774 775 776
				REG_WRITE(ah, AR_PHY_TPCRG5 + regChainOffset,
					  SM(pdGainOverlap_t2,
					     AR_PHY_TPCRG5_PD_GAIN_OVERLAP)
					  | SM(gainBoundaries[0],
					       AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1)
					  | SM(gainBoundaries[1],
					       AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2)
					  | SM(gainBoundaries[2],
					       AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3)
					  | SM(gainBoundaries[3],
				       AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
			}
777

S
Sujith 已提交
778 779 780 781 782 783 784
			regOffset = AR_PHY_BASE + (672 << 2) + regChainOffset;
			for (j = 0; j < 32; j++) {
				reg32 = ((pdadcValues[4 * j + 0] & 0xFF) << 0) |
					((pdadcValues[4 * j + 1] & 0xFF) << 8) |
					((pdadcValues[4 * j + 2] & 0xFF) << 16)|
					((pdadcValues[4 * j + 3] & 0xFF) << 24);
				REG_WRITE(ah, regOffset, reg32);
785

S
Sujith 已提交
786
				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
787 788 789
					"PDADC (%d,%4x): %4.4x %8.8x\n",
					i, regChainOffset, regOffset,
					reg32);
S
Sujith 已提交
790
				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
791 792 793 794 795 796 797 798 799 800
					"PDADC: Chain %d | "
					"PDADC %3d Value %3d | "
					"PDADC %3d Value %3d | "
					"PDADC %3d Value %3d | "
					"PDADC %3d Value %3d |\n",
					i, 4 * j, pdadcValues[4 * j],
					4 * j + 1, pdadcValues[4 * j + 1],
					4 * j + 2, pdadcValues[4 * j + 2],
					4 * j + 3,
					pdadcValues[4 * j + 3]);
801

S
Sujith 已提交
802
				regOffset += 4;
803 804 805 806
			}
		}
	}

S
Sujith 已提交
807
	*pTxPowerIndexOffset = 0;
808 809
}

810
static void ath9k_hw_set_4k_power_per_rate_table(struct ath_hw *ah,
S
Sujith 已提交
811 812 813 814 815 816
						 struct ath9k_channel *chan,
						 int16_t *ratesArray,
						 u16 cfgCtl,
						 u16 AntennaReduction,
						 u16 twiceMaxRegulatoryPower,
						 u16 powerLimit)
817
{
818
	struct ar5416_eeprom_4k *pEepData = &ah->eeprom.map4k;
S
Sujith 已提交
819 820 821 822
	u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
	static const u16 tpScaleReductionTable[5] =
		{ 0, 3, 6, 9, AR5416_MAX_RATE_POWER };

823
	int i;
S
Sujith 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
	int16_t twiceLargestAntenna;
	struct cal_ctl_data_4k *rep;
	struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
		0, { 0, 0, 0, 0}
	};
	struct cal_target_power_leg targetPowerOfdmExt = {
		0, { 0, 0, 0, 0} }, targetPowerCckExt = {
		0, { 0, 0, 0, 0 }
	};
	struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
		0, {0, 0, 0, 0}
	};
	u16 scaledPower = 0, minCtlPower, maxRegAllowedPower;
	u16 ctlModesFor11g[] =
		{ CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT,
		  CTL_2GHT40
		};
	u16 numCtlModes, *pCtlMode, ctlMode, freq;
	struct chan_centers centers;
	int tx_chainmask;
	u16 twiceMinEdgePower;

846
	tx_chainmask = ah->txchainmask;
847 848 849

	ath9k_hw_get_channel_centers(ah, chan, &centers);

S
Sujith 已提交
850
	twiceLargestAntenna = pEepData->modalHeader.antennaGainCh[0];
851

S
Sujith 已提交
852 853
	twiceLargestAntenna = (int16_t)min(AntennaReduction -
					   twiceLargestAntenna, 0);
854

S
Sujith 已提交
855 856 857 858 859
	maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;

	if (ah->regulatory.tp_scale != ATH9K_TP_SCALE_MAX) {
		maxRegAllowedPower -=
			(tpScaleReductionTable[(ah->regulatory.tp_scale)] * 2);
860 861
	}

S
Sujith 已提交
862 863
	scaledPower = min(powerLimit, maxRegAllowedPower);
	scaledPower = max((u16)0, scaledPower);
S
Sujith 已提交
864

S
Sujith 已提交
865 866
	numCtlModes = ARRAY_SIZE(ctlModesFor11g) - SUB_NUM_CTL_MODES_AT_2G_40;
	pCtlMode = ctlModesFor11g;
S
Sujith 已提交
867

S
Sujith 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
	ath9k_hw_get_legacy_target_powers(ah, chan,
			pEepData->calTargetPowerCck,
			AR5416_NUM_2G_CCK_TARGET_POWERS,
			&targetPowerCck, 4, false);
	ath9k_hw_get_legacy_target_powers(ah, chan,
			pEepData->calTargetPower2G,
			AR5416_NUM_2G_20_TARGET_POWERS,
			&targetPowerOfdm, 4, false);
	ath9k_hw_get_target_powers(ah, chan,
			pEepData->calTargetPower2GHT20,
			AR5416_NUM_2G_20_TARGET_POWERS,
			&targetPowerHt20, 8, false);

	if (IS_CHAN_HT40(chan)) {
		numCtlModes = ARRAY_SIZE(ctlModesFor11g);
		ath9k_hw_get_target_powers(ah, chan,
				pEepData->calTargetPower2GHT40,
				AR5416_NUM_2G_40_TARGET_POWERS,
				&targetPowerHt40, 8, true);
		ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPowerCck,
				AR5416_NUM_2G_CCK_TARGET_POWERS,
				&targetPowerCckExt, 4, true);
		ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPower2G,
				AR5416_NUM_2G_20_TARGET_POWERS,
				&targetPowerOfdmExt, 4, true);
S
Sujith 已提交
895 896
	}

S
Sujith 已提交
897 898 899 900 901 902 903 904 905
	for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
		bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
			(pCtlMode[ctlMode] == CTL_2GHT40);
		if (isHt40CtlMode)
			freq = centers.synth_center;
		else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
			freq = centers.ext_center;
		else
			freq = centers.ctl_center;
S
Sujith 已提交
906

S
Sujith 已提交
907 908 909 910
		if (ah->eep_ops->get_eeprom_ver(ah) == 14 &&
		    ah->eep_ops->get_eeprom_rev(ah) <= 2)
			twiceMaxEdgePower = AR5416_MAX_RATE_POWER;

S
Sujith 已提交
911
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
912 913 914 915 916
			"LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d, "
			"EXT_ADDITIVE %d\n",
			ctlMode, numCtlModes, isHt40CtlMode,
			(pCtlMode[ctlMode] & EXT_ADDITIVE));

R
Roel Kluin 已提交
917
		for (i = 0; (i < AR5416_EEP4K_NUM_CTLS) &&
S
Sujith 已提交
918
				pEepData->ctlIndex[i]; i++) {
S
Sujith 已提交
919
			DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
				"  LOOP-Ctlidx %d: cfgCtl 0x%2.2x "
				"pCtlMode 0x%2.2x ctlIndex 0x%2.2x "
				"chan %d\n",
				i, cfgCtl, pCtlMode[ctlMode],
				pEepData->ctlIndex[i], chan->channel);

			if ((((cfgCtl & ~CTL_MODE_M) |
			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
			     pEepData->ctlIndex[i]) ||
			    (((cfgCtl & ~CTL_MODE_M) |
			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
			     ((pEepData->ctlIndex[i] & CTL_MODE_M) |
			      SD_NO_CTL))) {
				rep = &(pEepData->ctlData[i]);

				twiceMinEdgePower =
					ath9k_hw_get_max_edge_power(freq,
				rep->ctlEdges[ar5416_get_ntxchains
						(tx_chainmask) - 1],
				IS_CHAN_2GHZ(chan),
				AR5416_EEP4K_NUM_BAND_EDGES);

S
Sujith 已提交
942
				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
					"    MATCH-EE_IDX %d: ch %d is2 %d "
					"2xMinEdge %d chainmask %d chains %d\n",
					i, freq, IS_CHAN_2GHZ(chan),
					twiceMinEdgePower, tx_chainmask,
					ar5416_get_ntxchains
					(tx_chainmask));
				if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
					twiceMaxEdgePower =
						min(twiceMaxEdgePower,
						    twiceMinEdgePower);
				} else {
					twiceMaxEdgePower = twiceMinEdgePower;
					break;
				}
			}
S
Sujith 已提交
958 959
		}

S
Sujith 已提交
960
		minCtlPower = (u8)min(twiceMaxEdgePower, scaledPower);
S
Sujith 已提交
961

S
Sujith 已提交
962
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975
			"    SEL-Min ctlMode %d pCtlMode %d "
			"2xMaxEdge %d sP %d minCtlPwr %d\n",
			ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
			scaledPower, minCtlPower);

		switch (pCtlMode[ctlMode]) {
		case CTL_11B:
			for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x);
					i++) {
				targetPowerCck.tPow2x[i] =
					min((u16)targetPowerCck.tPow2x[i],
					    minCtlPower);
			}
S
Sujith 已提交
976
			break;
S
Sujith 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
		case CTL_11G:
			for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x);
					i++) {
				targetPowerOfdm.tPow2x[i] =
					min((u16)targetPowerOfdm.tPow2x[i],
					    minCtlPower);
			}
			break;
		case CTL_2GHT20:
			for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x);
					i++) {
				targetPowerHt20.tPow2x[i] =
					min((u16)targetPowerHt20.tPow2x[i],
					    minCtlPower);
			}
			break;
		case CTL_11B_EXT:
			targetPowerCckExt.tPow2x[0] = min((u16)
					targetPowerCckExt.tPow2x[0],
					minCtlPower);
			break;
		case CTL_11G_EXT:
			targetPowerOfdmExt.tPow2x[0] = min((u16)
					targetPowerOfdmExt.tPow2x[0],
					minCtlPower);
			break;
		case CTL_2GHT40:
			for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x);
					i++) {
				targetPowerHt40.tPow2x[i] =
					min((u16)targetPowerHt40.tPow2x[i],
					    minCtlPower);
S
Sujith 已提交
1009 1010
			}
			break;
S
Sujith 已提交
1011 1012
		default:
			break;
S
Sujith 已提交
1013 1014 1015
		}
	}

S
Sujith 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
		ratesArray[rate18mb] = ratesArray[rate24mb] =
		targetPowerOfdm.tPow2x[0];
	ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
	ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
	ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
	ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];

	for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
		ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];

	ratesArray[rate1l] = targetPowerCck.tPow2x[0];
	ratesArray[rate2s] = ratesArray[rate2l] = targetPowerCck.tPow2x[1];
	ratesArray[rate5_5s] = ratesArray[rate5_5l] = targetPowerCck.tPow2x[2];
	ratesArray[rate11s] = ratesArray[rate11l] = targetPowerCck.tPow2x[3];

	if (IS_CHAN_HT40(chan)) {
		for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
			ratesArray[rateHt40_0 + i] =
				targetPowerHt40.tPow2x[i];
		}
		ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
		ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
		ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
		ratesArray[rateExtCck] = targetPowerCckExt.tPow2x[0];
	}
S
Sujith 已提交
1042 1043
}

1044
static void ath9k_hw_4k_set_txpower(struct ath_hw *ah,
S
Sujith 已提交
1045 1046 1047 1048 1049
				   struct ath9k_channel *chan,
				   u16 cfgCtl,
				   u8 twiceAntennaReduction,
				   u8 twiceMaxRegulatoryPower,
				   u8 powerLimit)
S
Sujith 已提交
1050
{
1051
	struct ar5416_eeprom_4k *pEepData = &ah->eeprom.map4k;
S
Sujith 已提交
1052 1053 1054 1055 1056
	struct modal_eep_4k_header *pModal = &pEepData->modalHeader;
	int16_t ratesArray[Ar5416RateSize];
	int16_t txPowerIndexOffset = 0;
	u8 ht40PowerIncForPdadc = 2;
	int i;
S
Sujith 已提交
1057

S
Sujith 已提交
1058
	memset(ratesArray, 0, sizeof(ratesArray));
S
Sujith 已提交
1059 1060 1061

	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
	    AR5416_EEP_MINOR_VER_2) {
S
Sujith 已提交
1062
		ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
S
Sujith 已提交
1063 1064
	}

1065
	ath9k_hw_set_4k_power_per_rate_table(ah, chan,
S
Sujith 已提交
1066 1067 1068
					       &ratesArray[0], cfgCtl,
					       twiceAntennaReduction,
					       twiceMaxRegulatoryPower,
1069
					       powerLimit);
S
Sujith 已提交
1070

1071
	ath9k_hw_set_4k_power_cal_table(ah, chan, &txPowerIndexOffset);
S
Sujith 已提交
1072

S
Sujith 已提交
1073 1074 1075 1076
	for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
		ratesArray[i] =	(int16_t)(txPowerIndexOffset + ratesArray[i]);
		if (ratesArray[i] > AR5416_MAX_RATE_POWER)
			ratesArray[i] = AR5416_MAX_RATE_POWER;
S
Sujith 已提交
1077 1078
	}

S
Sujith 已提交
1079 1080 1081 1082
	if (AR_SREV_9280_10_OR_LATER(ah)) {
		for (i = 0; i < Ar5416RateSize; i++)
			ratesArray[i] -= AR5416_PWR_TABLE_OFFSET * 2;
	}
S
Sujith 已提交
1083

S
Sujith 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
		  ATH9K_POW_SM(ratesArray[rate18mb], 24)
		  | ATH9K_POW_SM(ratesArray[rate12mb], 16)
		  | ATH9K_POW_SM(ratesArray[rate9mb], 8)
		  | ATH9K_POW_SM(ratesArray[rate6mb], 0));
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
		  ATH9K_POW_SM(ratesArray[rate54mb], 24)
		  | ATH9K_POW_SM(ratesArray[rate48mb], 16)
		  | ATH9K_POW_SM(ratesArray[rate36mb], 8)
		  | ATH9K_POW_SM(ratesArray[rate24mb], 0));
S
Sujith 已提交
1094

S
Sujith 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	if (IS_CHAN_2GHZ(chan)) {
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
			  ATH9K_POW_SM(ratesArray[rate2s], 24)
			  | ATH9K_POW_SM(ratesArray[rate2l], 16)
			  | ATH9K_POW_SM(ratesArray[rateXr], 8)
			  | ATH9K_POW_SM(ratesArray[rate1l], 0));
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
			  ATH9K_POW_SM(ratesArray[rate11s], 24)
			  | ATH9K_POW_SM(ratesArray[rate11l], 16)
			  | ATH9K_POW_SM(ratesArray[rate5_5s], 8)
			  | ATH9K_POW_SM(ratesArray[rate5_5l], 0));
	}
S
Sujith 已提交
1107

S
Sujith 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
		  ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
		  | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
		  | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
		  | ATH9K_POW_SM(ratesArray[rateHt20_0], 0));
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
		  ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
		  | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
		  | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
		  | ATH9K_POW_SM(ratesArray[rateHt20_4], 0));
S
Sujith 已提交
1118

S
Sujith 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	if (IS_CHAN_HT40(chan)) {
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
			  ATH9K_POW_SM(ratesArray[rateHt40_3] +
				       ht40PowerIncForPdadc, 24)
			  | ATH9K_POW_SM(ratesArray[rateHt40_2] +
					 ht40PowerIncForPdadc, 16)
			  | ATH9K_POW_SM(ratesArray[rateHt40_1] +
					 ht40PowerIncForPdadc, 8)
			  | ATH9K_POW_SM(ratesArray[rateHt40_0] +
					 ht40PowerIncForPdadc, 0));
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
			  ATH9K_POW_SM(ratesArray[rateHt40_7] +
				       ht40PowerIncForPdadc, 24)
			  | ATH9K_POW_SM(ratesArray[rateHt40_6] +
					 ht40PowerIncForPdadc, 16)
			  | ATH9K_POW_SM(ratesArray[rateHt40_5] +
					 ht40PowerIncForPdadc, 8)
			  | ATH9K_POW_SM(ratesArray[rateHt40_4] +
					 ht40PowerIncForPdadc, 0));
S
Sujith 已提交
1138

S
Sujith 已提交
1139 1140 1141 1142 1143 1144
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
			  ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
			  | ATH9K_POW_SM(ratesArray[rateExtCck], 16)
			  | ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
			  | ATH9K_POW_SM(ratesArray[rateDupCck], 0));
	}
S
Sujith 已提交
1145

S
Sujith 已提交
1146
	i = rate6mb;
S
Sujith 已提交
1147

S
Sujith 已提交
1148 1149 1150 1151
	if (IS_CHAN_HT40(chan))
		i = rateHt40_0;
	else if (IS_CHAN_HT20(chan))
		i = rateHt20_0;
S
Sujith 已提交
1152

S
Sujith 已提交
1153 1154 1155 1156 1157
	if (AR_SREV_9280_10_OR_LATER(ah))
		ah->regulatory.max_power_level =
			ratesArray[i] + AR5416_PWR_TABLE_OFFSET * 2;
	else
		ah->regulatory.max_power_level = ratesArray[i];
S
Sujith 已提交
1158 1159 1160

}

S
Sujith 已提交
1161 1162
static void ath9k_hw_4k_set_addac(struct ath_hw *ah,
				  struct ath9k_channel *chan)
S
Sujith 已提交
1163
{
S
Sujith 已提交
1164
	struct modal_eep_4k_header *pModal;
1165
	struct ar5416_eeprom_4k *eep = &ah->eeprom.map4k;
S
Sujith 已提交
1166
	u8 biaslevel;
S
Sujith 已提交
1167

S
Sujith 已提交
1168 1169
	if (ah->hw_version.macVersion != AR_SREV_VERSION_9160)
		return;
S
Sujith 已提交
1170

S
Sujith 已提交
1171 1172
	if (ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_MINOR_VER_7)
		return;
S
Sujith 已提交
1173

S
Sujith 已提交
1174
	pModal = &eep->modalHeader;
S
Sujith 已提交
1175

S
Sujith 已提交
1176 1177
	if (pModal->xpaBiasLvl != 0xff) {
		biaslevel = pModal->xpaBiasLvl;
1178 1179
		INI_RA(&ah->iniAddac, 7, 1) =
		  (INI_RA(&ah->iniAddac, 7, 1) & (~0x18)) | biaslevel << 3;
1180
	}
S
Sujith 已提交
1181
}
1182

1183 1184 1185 1186
static void ath9k_hw_4k_set_gain(struct ath_hw *ah,
				 struct modal_eep_4k_header *pModal,
				 struct ar5416_eeprom_4k *eep,
				 u8 txRxAttenLocal, int regChainOffset)
S
Sujith 已提交
1187 1188 1189
{
	REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
		  pModal->antCtrlChain[0]);
1190

S
Sujith 已提交
1191
	REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
1192 1193 1194 1195 1196
		  (REG_READ(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset) &
		   ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
		     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
		  SM(pModal->iqCalICh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
		  SM(pModal->iqCalQCh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
1197

S
Sujith 已提交
1198
	if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
1199
	    AR5416_EEP_MINOR_VER_3) {
S
Sujith 已提交
1200
		txRxAttenLocal = pModal->txRxAttenCh[0];
1201

S
Sujith 已提交
1202
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
1203
			      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN, pModal->bswMargin[0]);
S
Sujith 已提交
1204
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
1205
			      AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
S
Sujith 已提交
1206
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
1207 1208
			      AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
			      pModal->xatten2Margin[0]);
S
Sujith 已提交
1209
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
1210
			      AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

		/* Set the block 1 value to block 0 value */
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
			      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
			      pModal->bswMargin[0]);
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
			      AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
			      AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
			      pModal->xatten2Margin[0]);
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + 0x1000,
			      AR_PHY_GAIN_2GHZ_XATTEN2_DB,
			      pModal->xatten2Db[0]);
S
Sujith 已提交
1224 1225
	}

S
Sujith 已提交
1226
	REG_RMW_FIELD(ah, AR_PHY_RXGAIN + regChainOffset,
1227
		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
S
Sujith 已提交
1228
	REG_RMW_FIELD(ah, AR_PHY_RXGAIN + regChainOffset,
1229
		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);
1230

1231 1232 1233 1234 1235
	REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,
		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
	REG_RMW_FIELD(ah, AR_PHY_RXGAIN + 0x1000,
		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);

S
Sujith 已提交
1236 1237
	if (AR_SREV_9285_11(ah))
		REG_WRITE(ah, AR9285_AN_TOP4, (AR9285_AN_TOP4_DEFAULT | 0x14));
1238 1239
}

1240
static void ath9k_hw_4k_set_board_values(struct ath_hw *ah,
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
					 struct ath9k_channel *chan)
{
	struct modal_eep_4k_header *pModal;
	struct ar5416_eeprom_4k *eep = &ah->eeprom.map4k;
	u8 txRxAttenLocal;
	u8 ob[5], db1[5], db2[5];
	u8 ant_div_control1, ant_div_control2;
	u32 regVal;

	pModal = &eep->modalHeader;
	txRxAttenLocal = 23;

	REG_WRITE(ah, AR_PHY_SWITCH_COM,
		  ah->eep_ops->get_eeprom_antenna_cfg(ah, chan));

	/* Single chain for 4K EEPROM*/
	ath9k_hw_4k_set_gain(ah, pModal, eep, txRxAttenLocal, 0);
S
Sujith 已提交
1258

S
Sujith 已提交
1259
	/* Initialize Ant Diversity settings from EEPROM */
1260
	if (pModal->version >= 3) {
S
Sujith 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		ant_div_control1 = ((pModal->ob_234 >> 12) & 0xf);
		ant_div_control2 = ((pModal->db1_234 >> 12) & 0xf);
		regVal = REG_READ(ah, 0x99ac);
		regVal &= (~(0x7f000000));
		regVal |= ((ant_div_control1 & 0x1) << 24);
		regVal |= (((ant_div_control1 >> 1) & 0x1) << 29);
		regVal |= (((ant_div_control1 >> 2) & 0x1) << 30);
		regVal |= ((ant_div_control2 & 0x3) << 25);
		regVal |= (((ant_div_control2 >> 2) & 0x3) << 27);
		REG_WRITE(ah, 0x99ac, regVal);
		regVal = REG_READ(ah, 0x99ac);
		regVal = REG_READ(ah, 0xa208);
		regVal &= (~(0x1 << 13));
		regVal |= (((ant_div_control1 >> 3) & 0x1) << 13);
		REG_WRITE(ah, 0xa208, regVal);
		regVal = REG_READ(ah, 0xa208);
	}
1278

S
Sujith 已提交
1279 1280 1281 1282 1283 1284
	if (pModal->version >= 2) {
		ob[0] = (pModal->ob_01 & 0xf);
		ob[1] = (pModal->ob_01 >> 4) & 0xf;
		ob[2] = (pModal->ob_234 & 0xf);
		ob[3] = ((pModal->ob_234 >> 4) & 0xf);
		ob[4] = ((pModal->ob_234 >> 8) & 0xf);
S
Sujith 已提交
1285

S
Sujith 已提交
1286 1287 1288 1289 1290
		db1[0] = (pModal->db1_01 & 0xf);
		db1[1] = ((pModal->db1_01 >> 4) & 0xf);
		db1[2] = (pModal->db1_234 & 0xf);
		db1[3] = ((pModal->db1_234 >> 4) & 0xf);
		db1[4] = ((pModal->db1_234 >> 8) & 0xf);
S
Sujith 已提交
1291

S
Sujith 已提交
1292 1293 1294 1295 1296
		db2[0] = (pModal->db2_01 & 0xf);
		db2[1] = ((pModal->db2_01 >> 4) & 0xf);
		db2[2] = (pModal->db2_234 & 0xf);
		db2[3] = ((pModal->db2_234 >> 4) & 0xf);
		db2[4] = ((pModal->db2_234 >> 8) & 0xf);
S
Sujith 已提交
1297

S
Sujith 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	} else if (pModal->version == 1) {
		ob[0] = (pModal->ob_01 & 0xf);
		ob[1] = ob[2] = ob[3] = ob[4] = (pModal->ob_01 >> 4) & 0xf;
		db1[0] = (pModal->db1_01 & 0xf);
		db1[1] = db1[2] = db1[3] =
			db1[4] = ((pModal->db1_01 >> 4) & 0xf);
		db2[0] = (pModal->db2_01 & 0xf);
		db2[1] = db2[2] = db2[3] =
			db2[4] = ((pModal->db2_01 >> 4) & 0xf);
	} else {
		int i;
		for (i = 0; i < 5; i++) {
			ob[i] = pModal->ob_01;
			db1[i] = pModal->db1_01;
			db2[i] = pModal->db1_01;
		}
	}
S
Sujith 已提交
1315

S
Sujith 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_OB_0, AR9285_AN_RF2G3_OB_0_S, ob[0]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_OB_1, AR9285_AN_RF2G3_OB_1_S, ob[1]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_OB_2, AR9285_AN_RF2G3_OB_2_S, ob[2]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_OB_3, AR9285_AN_RF2G3_OB_3_S, ob[3]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_OB_4, AR9285_AN_RF2G3_OB_4_S, ob[4]);
S
Sujith 已提交
1326

S
Sujith 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_DB1_0, AR9285_AN_RF2G3_DB1_0_S, db1[0]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_DB1_1, AR9285_AN_RF2G3_DB1_1_S, db1[1]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
			AR9285_AN_RF2G3_DB1_2, AR9285_AN_RF2G3_DB1_2_S, db1[2]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB1_3, AR9285_AN_RF2G4_DB1_3_S, db1[3]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB1_4, AR9285_AN_RF2G4_DB1_4_S, db1[4]);
S
Sujith 已提交
1337

S
Sujith 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB2_0, AR9285_AN_RF2G4_DB2_0_S, db2[0]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB2_1, AR9285_AN_RF2G4_DB2_1_S, db2[1]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB2_2, AR9285_AN_RF2G4_DB2_2_S, db2[2]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB2_3, AR9285_AN_RF2G4_DB2_3_S, db2[3]);
	ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
			AR9285_AN_RF2G4_DB2_4, AR9285_AN_RF2G4_DB2_4_S, db2[4]);
S
Sujith 已提交
1348 1349


S
Sujith 已提交
1350 1351
	if (AR_SREV_9285_11(ah))
		REG_WRITE(ah, AR9285_AN_TOP4, AR9285_AN_TOP4_DEFAULT);
S
Sujith 已提交
1352

S
Sujith 已提交
1353 1354 1355 1356
	REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
		      pModal->switchSettling);
	REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
		      pModal->adcDesiredSize);
S
Sujith 已提交
1357

S
Sujith 已提交
1358 1359 1360 1361 1362
	REG_WRITE(ah, AR_PHY_RF_CTL4,
		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF) |
		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAB_OFF) |
		  SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAA_ON)  |
		  SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAB_ON));
S
Sujith 已提交
1363

S
Sujith 已提交
1364 1365 1366 1367 1368 1369
	REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
		      pModal->txEndToRxOn);
	REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
		      pModal->thresh62);
	REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0, AR_PHY_EXT_CCA0_THRESH62,
		      pModal->thresh62);
S
Sujith 已提交
1370

S
Sujith 已提交
1371 1372 1373 1374 1375 1376 1377
	if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
						AR5416_EEP_MINOR_VER_2) {
		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_DATA_START,
			      pModal->txFrameToDataStart);
		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
			      pModal->txFrameToPaOn);
	}
S
Sujith 已提交
1378

S
Sujith 已提交
1379 1380 1381 1382 1383 1384 1385 1386
	if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
						AR5416_EEP_MINOR_VER_3) {
		if (IS_CHAN_HT40(chan))
			REG_RMW_FIELD(ah, AR_PHY_SETTLING,
				      AR_PHY_SETTLING_SWITCH,
				      pModal->swSettleHt40);
	}
}
S
Sujith 已提交
1387

S
Sujith 已提交
1388 1389 1390
static u16 ath9k_hw_4k_get_eeprom_antenna_cfg(struct ath_hw *ah,
					      struct ath9k_channel *chan)
{
1391
	struct ar5416_eeprom_4k *eep = &ah->eeprom.map4k;
S
Sujith 已提交
1392
	struct modal_eep_4k_header *pModal = &eep->modalHeader;
S
Sujith 已提交
1393

S
Sujith 已提交
1394 1395
	return pModal->antCtrlCommon & 0xFFFF;
}
S
Sujith 已提交
1396

S
Sujith 已提交
1397 1398 1399 1400 1401
static u8 ath9k_hw_4k_get_num_ant_config(struct ath_hw *ah,
					 enum ieee80211_band freq_band)
{
	return 1;
}
S
Sujith 已提交
1402

1403
static u16 ath9k_hw_4k_get_spur_channel(struct ath_hw *ah, u16 i, bool is2GHz)
S
Sujith 已提交
1404 1405
{
#define EEP_MAP4K_SPURCHAN \
1406
	(ah->eeprom.map4k.modalHeader.spurChans[i].spurChan)
S
Sujith 已提交
1407

S
Sujith 已提交
1408
	u16 spur_val = AR_NO_SPUR;
S
Sujith 已提交
1409

S
Sujith 已提交
1410 1411
	DPRINTF(ah->ah_sc, ATH_DBG_ANI,
		"Getting spur idx %d is2Ghz. %d val %x\n",
1412
		i, is2GHz, ah->config.spurchans[i][is2GHz]);
S
Sujith 已提交
1413

1414
	switch (ah->config.spurmode) {
S
Sujith 已提交
1415 1416 1417
	case SPUR_DISABLE:
		break;
	case SPUR_ENABLE_IOCTL:
1418
		spur_val = ah->config.spurchans[i][is2GHz];
S
Sujith 已提交
1419 1420 1421 1422 1423 1424 1425
		DPRINTF(ah->ah_sc, ATH_DBG_ANI,
			"Getting spur val from new loc. %d\n", spur_val);
		break;
	case SPUR_ENABLE_EEPROM:
		spur_val = EEP_MAP4K_SPURCHAN;
		break;
	}
S
Sujith 已提交
1426

S
Sujith 已提交
1427
	return spur_val;
S
Sujith 已提交
1428

S
Sujith 已提交
1429 1430
#undef EEP_MAP4K_SPURCHAN
}
S
Sujith 已提交
1431

1432
static struct eeprom_ops eep_4k_ops = {
S
Sujith 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	.check_eeprom		= ath9k_hw_4k_check_eeprom,
	.get_eeprom		= ath9k_hw_4k_get_eeprom,
	.fill_eeprom		= ath9k_hw_4k_fill_eeprom,
	.get_eeprom_ver		= ath9k_hw_4k_get_eeprom_ver,
	.get_eeprom_rev		= ath9k_hw_4k_get_eeprom_rev,
	.get_num_ant_config	= ath9k_hw_4k_get_num_ant_config,
	.get_eeprom_antenna_cfg	= ath9k_hw_4k_get_eeprom_antenna_cfg,
	.set_board_values	= ath9k_hw_4k_set_board_values,
	.set_addac		= ath9k_hw_4k_set_addac,
	.set_txpower		= ath9k_hw_4k_set_txpower,
	.get_spur_channel	= ath9k_hw_4k_get_spur_channel
};
S
Sujith 已提交
1445

S
Sujith 已提交
1446 1447 1448
/************************************************/
/* EEPROM Operations for non-4K (Default) cards */
/************************************************/
S
Sujith 已提交
1449

S
Sujith 已提交
1450 1451
static int ath9k_hw_def_get_eeprom_ver(struct ath_hw *ah)
{
1452
	return ((ah->eeprom.def.baseEepHeader.version >> 12) & 0xF);
S
Sujith 已提交
1453
}
S
Sujith 已提交
1454

S
Sujith 已提交
1455 1456
static int ath9k_hw_def_get_eeprom_rev(struct ath_hw *ah)
{
1457
	return ((ah->eeprom.def.baseEepHeader.version) & 0xFFF);
S
Sujith 已提交
1458
}
S
Sujith 已提交
1459

S
Sujith 已提交
1460 1461 1462
static bool ath9k_hw_def_fill_eeprom(struct ath_hw *ah)
{
#define SIZE_EEPROM_DEF (sizeof(struct ar5416_eeprom_def) / sizeof(u16))
1463
	u16 *eep_data = (u16 *)&ah->eeprom.def;
S
Sujith 已提交
1464 1465 1466 1467 1468
	int addr, ar5416_eep_start_loc = 0x100;

	for (addr = 0; addr < SIZE_EEPROM_DEF; addr++) {
		if (!ath9k_hw_nvram_read(ah, addr + ar5416_eep_start_loc,
					 eep_data)) {
1469
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
1470 1471
				"Unable to read eeprom region\n");
			return false;
S
Sujith 已提交
1472
		}
S
Sujith 已提交
1473
		eep_data++;
S
Sujith 已提交
1474 1475
	}
	return true;
S
Sujith 已提交
1476
#undef SIZE_EEPROM_DEF
S
Sujith 已提交
1477 1478
}

S
Sujith 已提交
1479
static int ath9k_hw_def_check_eeprom(struct ath_hw *ah)
S
Sujith 已提交
1480
{
S
Sujith 已提交
1481
	struct ar5416_eeprom_def *eep =
1482
		(struct ar5416_eeprom_def *) &ah->eeprom.def;
S
Sujith 已提交
1483 1484 1485 1486
	u16 *eepdata, temp, magic, magic2;
	u32 sum = 0, el;
	bool need_swap = false;
	int i, addr, size;
S
Sujith 已提交
1487

1488 1489
	if (!ath9k_hw_nvram_read(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
		DPRINTF(ah->ah_sc, ATH_DBG_FATAL, "Reading Magic # failed\n");
S
Sujith 已提交
1490 1491
		return false;
	}
1492

S
Sujith 已提交
1493 1494
	if (!ath9k_hw_use_flash(ah)) {
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
1495
			"Read Magic = 0x%04X\n", magic);
1496

S
Sujith 已提交
1497 1498
		if (magic != AR5416_EEPROM_MAGIC) {
			magic2 = swab16(magic);
1499

S
Sujith 已提交
1500 1501 1502
			if (magic2 == AR5416_EEPROM_MAGIC) {
				size = sizeof(struct ar5416_eeprom_def);
				need_swap = true;
1503
				eepdata = (u16 *) (&ah->eeprom);
1504

S
Sujith 已提交
1505 1506 1507 1508 1509 1510
				for (addr = 0; addr < size / sizeof(u16); addr++) {
					temp = swab16(*eepdata);
					*eepdata = temp;
					eepdata++;
				}
			} else {
1511
				DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
1512
					"Invalid EEPROM Magic. "
1513
					"Endianness mismatch.\n");
S
Sujith 已提交
1514 1515 1516
				return -EINVAL;
			}
		}
1517 1518
	}

S
Sujith 已提交
1519 1520
	DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "need_swap = %s.\n",
		need_swap ? "True" : "False");
1521

S
Sujith 已提交
1522
	if (need_swap)
1523
		el = swab16(ah->eeprom.def.baseEepHeader.length);
S
Sujith 已提交
1524
	else
1525
		el = ah->eeprom.def.baseEepHeader.length;
1526

S
Sujith 已提交
1527 1528 1529 1530
	if (el > sizeof(struct ar5416_eeprom_def))
		el = sizeof(struct ar5416_eeprom_def) / sizeof(u16);
	else
		el = el / sizeof(u16);
1531

1532
	eepdata = (u16 *)(&ah->eeprom);
1533

S
Sujith 已提交
1534 1535
	for (i = 0; i < el; i++)
		sum ^= *eepdata++;
1536

S
Sujith 已提交
1537 1538 1539
	if (need_swap) {
		u32 integer, j;
		u16 word;
1540

S
Sujith 已提交
1541
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
1542
			"EEPROM Endianness is not native.. Changing.\n");
1543

S
Sujith 已提交
1544 1545
		word = swab16(eep->baseEepHeader.length);
		eep->baseEepHeader.length = word;
1546

S
Sujith 已提交
1547 1548
		word = swab16(eep->baseEepHeader.checksum);
		eep->baseEepHeader.checksum = word;
1549

S
Sujith 已提交
1550 1551
		word = swab16(eep->baseEepHeader.version);
		eep->baseEepHeader.version = word;
1552

S
Sujith 已提交
1553 1554
		word = swab16(eep->baseEepHeader.regDmn[0]);
		eep->baseEepHeader.regDmn[0] = word;
1555

S
Sujith 已提交
1556 1557
		word = swab16(eep->baseEepHeader.regDmn[1]);
		eep->baseEepHeader.regDmn[1] = word;
1558

S
Sujith 已提交
1559 1560
		word = swab16(eep->baseEepHeader.rfSilent);
		eep->baseEepHeader.rfSilent = word;
1561

S
Sujith 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
		word = swab16(eep->baseEepHeader.blueToothOptions);
		eep->baseEepHeader.blueToothOptions = word;

		word = swab16(eep->baseEepHeader.deviceCap);
		eep->baseEepHeader.deviceCap = word;

		for (j = 0; j < ARRAY_SIZE(eep->modalHeader); j++) {
			struct modal_eep_header *pModal =
				&eep->modalHeader[j];
			integer = swab32(pModal->antCtrlCommon);
			pModal->antCtrlCommon = integer;

			for (i = 0; i < AR5416_MAX_CHAINS; i++) {
				integer = swab32(pModal->antCtrlChain[i]);
				pModal->antCtrlChain[i] = integer;
1577
			}
S
Sujith 已提交
1578 1579 1580 1581

			for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
				word = swab16(pModal->spurChans[i].spurChan);
				pModal->spurChans[i].spurChan = word;
1582 1583 1584 1585
			}
		}
	}

S
Sujith 已提交
1586 1587
	if (sum != 0xffff || ah->eep_ops->get_eeprom_ver(ah) != AR5416_EEP_VER ||
	    ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_NO_BACK_VER) {
1588
		DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
1589 1590 1591 1592
			"Bad EEPROM checksum 0x%x or revision 0x%04x\n",
			sum, ah->eep_ops->get_eeprom_ver(ah));
		return -EINVAL;
	}
1593

S
Sujith 已提交
1594 1595
	return 0;
}
1596

S
Sujith 已提交
1597 1598 1599
static u32 ath9k_hw_def_get_eeprom(struct ath_hw *ah,
				   enum eeprom_param param)
{
1600
	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
S
Sujith 已提交
1601 1602
	struct modal_eep_header *pModal = eep->modalHeader;
	struct base_eep_header *pBase = &eep->baseEepHeader;
1603

S
Sujith 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
	switch (param) {
	case EEP_NFTHRESH_5:
		return pModal[0].noiseFloorThreshCh[0];
	case EEP_NFTHRESH_2:
		return pModal[1].noiseFloorThreshCh[0];
	case AR_EEPROM_MAC(0):
		return pBase->macAddr[0] << 8 | pBase->macAddr[1];
	case AR_EEPROM_MAC(1):
		return pBase->macAddr[2] << 8 | pBase->macAddr[3];
	case AR_EEPROM_MAC(2):
		return pBase->macAddr[4] << 8 | pBase->macAddr[5];
	case EEP_REG_0:
		return pBase->regDmn[0];
	case EEP_REG_1:
		return pBase->regDmn[1];
	case EEP_OP_CAP:
		return pBase->deviceCap;
	case EEP_OP_MODE:
		return pBase->opCapFlags;
	case EEP_RF_SILENT:
		return pBase->rfSilent;
	case EEP_OB_5:
		return pModal[0].ob;
	case EEP_DB_5:
		return pModal[0].db;
	case EEP_OB_2:
		return pModal[1].ob;
	case EEP_DB_2:
		return pModal[1].db;
	case EEP_MINOR_REV:
		return AR5416_VER_MASK;
	case EEP_TX_MASK:
		return pBase->txMask;
	case EEP_RX_MASK:
		return pBase->rxMask;
	case EEP_RXGAIN_TYPE:
		return pBase->rxGainType;
	case EEP_TXGAIN_TYPE:
		return pBase->txGainType;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
	case EEP_OL_PWRCTRL:
		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
			return pBase->openLoopPwrCntl ? true : false;
		else
			return false;
	case EEP_RC_CHAIN_MASK:
		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
			return pBase->rcChainMask;
		else
			return 0;
S
Sujith 已提交
1653 1654 1655 1656 1657
	case EEP_DAC_HPWR_5G:
		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_20)
			return pBase->dacHiPwrMode_5G;
		else
			return 0;
S
Sujith 已提交
1658 1659 1660 1661 1662
	case EEP_FRAC_N_5G:
		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_22)
			return pBase->frac_n_5g;
		else
			return 0;
S
Sujith 已提交
1663 1664
	default:
		return 0;
1665 1666 1667
	}
}

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
static void ath9k_hw_def_set_gain(struct ath_hw *ah,
				  struct modal_eep_header *pModal,
				  struct ar5416_eeprom_def *eep,
				  u8 txRxAttenLocal, int regChainOffset, int i)
{
	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_3) {
		txRxAttenLocal = pModal->txRxAttenCh[i];

		if (AR_SREV_9280_10_OR_LATER(ah)) {
			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
			      pModal->bswMargin[i]);
			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			      AR_PHY_GAIN_2GHZ_XATTEN1_DB,
			      pModal->bswAtten[i]);
			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			      AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
			      pModal->xatten2Margin[i]);
			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			      AR_PHY_GAIN_2GHZ_XATTEN2_DB,
			      pModal->xatten2Db[i]);
		} else {
			REG_WRITE(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			  (REG_READ(ah, AR_PHY_GAIN_2GHZ + regChainOffset) &
			   ~AR_PHY_GAIN_2GHZ_BSW_MARGIN)
			  | SM(pModal-> bswMargin[i],
			       AR_PHY_GAIN_2GHZ_BSW_MARGIN));
			REG_WRITE(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			  (REG_READ(ah, AR_PHY_GAIN_2GHZ + regChainOffset) &
			   ~AR_PHY_GAIN_2GHZ_BSW_ATTEN)
			  | SM(pModal->bswAtten[i],
			       AR_PHY_GAIN_2GHZ_BSW_ATTEN));
		}
	}

	if (AR_SREV_9280_10_OR_LATER(ah)) {
		REG_RMW_FIELD(ah,
		      AR_PHY_RXGAIN + regChainOffset,
		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
		REG_RMW_FIELD(ah,
		      AR_PHY_RXGAIN + regChainOffset,
		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[i]);
	} else {
		REG_WRITE(ah,
			  AR_PHY_RXGAIN + regChainOffset,
			  (REG_READ(ah, AR_PHY_RXGAIN + regChainOffset) &
			   ~AR_PHY_RXGAIN_TXRX_ATTEN)
			  | SM(txRxAttenLocal, AR_PHY_RXGAIN_TXRX_ATTEN));
		REG_WRITE(ah,
			  AR_PHY_GAIN_2GHZ + regChainOffset,
			  (REG_READ(ah, AR_PHY_GAIN_2GHZ + regChainOffset) &
			   ~AR_PHY_GAIN_2GHZ_RXTX_MARGIN) |
			  SM(pModal->rxTxMarginCh[i], AR_PHY_GAIN_2GHZ_RXTX_MARGIN));
	}
}

1724
static void ath9k_hw_def_set_board_values(struct ath_hw *ah,
S
Sujith 已提交
1725
					  struct ath9k_channel *chan)
1726
{
S
Sujith 已提交
1727
	struct modal_eep_header *pModal;
1728
	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
S
Sujith 已提交
1729 1730
	int i, regChainOffset;
	u8 txRxAttenLocal;
1731

S
Sujith 已提交
1732 1733
	pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
	txRxAttenLocal = IS_CHAN_2GHZ(chan) ? 23 : 44;
1734

S
Sujith 已提交
1735 1736
	REG_WRITE(ah, AR_PHY_SWITCH_COM,
		  ah->eep_ops->get_eeprom_antenna_cfg(ah, chan));
1737

S
Sujith 已提交
1738 1739 1740 1741 1742
	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
		if (AR_SREV_9280(ah)) {
			if (i >= 2)
				break;
		}
1743

1744
		if (AR_SREV_5416_20_OR_LATER(ah) &&
1745
		    (ah->rxchainmask == 5 || ah->txchainmask == 5) && (i != 0))
S
Sujith 已提交
1746 1747 1748
			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
		else
			regChainOffset = i * 0x1000;
1749

S
Sujith 已提交
1750 1751
		REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
			  pModal->antCtrlChain[i]);
1752

S
Sujith 已提交
1753
		REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
1754
			  (REG_READ(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset) &
S
Sujith 已提交
1755 1756 1757 1758 1759 1760
			   ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
			     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
			  SM(pModal->iqCalICh[i],
			     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
			  SM(pModal->iqCalQCh[i],
			     AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
1761

1762 1763 1764
		if ((i == 0) || AR_SREV_5416_20_OR_LATER(ah))
			ath9k_hw_def_set_gain(ah, pModal, eep, txRxAttenLocal,
					      regChainOffset, i);
1765 1766 1767
	}

	if (AR_SREV_9280_10_OR_LATER(ah)) {
S
Sujith 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
		if (IS_CHAN_2GHZ(chan)) {
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
						  AR_AN_RF2G1_CH0_OB,
						  AR_AN_RF2G1_CH0_OB_S,
						  pModal->ob);
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
						  AR_AN_RF2G1_CH0_DB,
						  AR_AN_RF2G1_CH0_DB_S,
						  pModal->db);
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
						  AR_AN_RF2G1_CH1_OB,
						  AR_AN_RF2G1_CH1_OB_S,
						  pModal->ob_ch1);
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
						  AR_AN_RF2G1_CH1_DB,
						  AR_AN_RF2G1_CH1_DB_S,
						  pModal->db_ch1);
		} else {
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
						  AR_AN_RF5G1_CH0_OB5,
						  AR_AN_RF5G1_CH0_OB5_S,
						  pModal->ob);
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
						  AR_AN_RF5G1_CH0_DB5,
						  AR_AN_RF5G1_CH0_DB5_S,
						  pModal->db);
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
						  AR_AN_RF5G1_CH1_OB5,
						  AR_AN_RF5G1_CH1_OB5_S,
						  pModal->ob_ch1);
			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
						  AR_AN_RF5G1_CH1_DB5,
						  AR_AN_RF5G1_CH1_DB5_S,
						  pModal->db_ch1);
		}
		ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
					  AR_AN_TOP2_XPABIAS_LVL,
					  AR_AN_TOP2_XPABIAS_LVL_S,
					  pModal->xpaBiasLvl);
		ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
					  AR_AN_TOP2_LOCALBIAS,
					  AR_AN_TOP2_LOCALBIAS_S,
					  pModal->local_bias);
		REG_RMW_FIELD(ah, AR_PHY_XPA_CFG, AR_PHY_FORCE_XPA_CFG,
			      pModal->force_xpaon);
1813 1814
	}

S
Sujith 已提交
1815 1816 1817 1818
	REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
		      pModal->switchSettling);
	REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
		      pModal->adcDesiredSize);
1819

S
Sujith 已提交
1820 1821 1822 1823
	if (!AR_SREV_9280_10_OR_LATER(ah))
		REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
			      AR_PHY_DESIRED_SZ_PGA,
			      pModal->pgaDesiredSize);
1824

S
Sujith 已提交
1825 1826 1827 1828 1829 1830 1831 1832
	REG_WRITE(ah, AR_PHY_RF_CTL4,
		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF)
		  | SM(pModal->txEndToXpaOff,
		       AR_PHY_RF_CTL4_TX_END_XPAB_OFF)
		  | SM(pModal->txFrameToXpaOn,
		       AR_PHY_RF_CTL4_FRAME_XPAA_ON)
		  | SM(pModal->txFrameToXpaOn,
		       AR_PHY_RF_CTL4_FRAME_XPAB_ON));
1833

S
Sujith 已提交
1834 1835
	REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
		      pModal->txEndToRxOn);
1836

S
Sujith 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
	if (AR_SREV_9280_10_OR_LATER(ah)) {
		REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
			      pModal->thresh62);
		REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0,
			      AR_PHY_EXT_CCA0_THRESH62,
			      pModal->thresh62);
	} else {
		REG_RMW_FIELD(ah, AR_PHY_CCA, AR_PHY_CCA_THRESH62,
			      pModal->thresh62);
		REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
			      AR_PHY_EXT_CCA_THRESH62,
			      pModal->thresh62);
	}
1850

S
Sujith 已提交
1851 1852 1853 1854 1855 1856 1857
	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_2) {
		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
			      AR_PHY_TX_END_DATA_START,
			      pModal->txFrameToDataStart);
		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
			      pModal->txFrameToPaOn);
	}
1858

S
Sujith 已提交
1859 1860 1861 1862 1863 1864
	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_3) {
		if (IS_CHAN_HT40(chan))
			REG_RMW_FIELD(ah, AR_PHY_SETTLING,
				      AR_PHY_SETTLING_SWITCH,
				      pModal->swSettleHt40);
	}
1865

1866
	if (AR_SREV_9280_20_OR_LATER(ah) &&
1867
	    AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
1868
		REG_RMW_FIELD(ah, AR_PHY_CCK_TX_CTRL,
1869 1870
			      AR_PHY_CCK_TX_CTRL_TX_DAC_SCALE_CCK,
			      pModal->miscBits);
1871 1872


S
Sujith 已提交
1873
	if (AR_SREV_9280_20(ah) && AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_20) {
1874
		if (IS_CHAN_2GHZ(chan))
S
Sujith 已提交
1875 1876 1877 1878 1879 1880
			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE,
					eep->baseEepHeader.dacLpMode);
		else if (eep->baseEepHeader.dacHiPwrMode_5G)
			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE, 0);
		else
			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE,
1881
				      eep->baseEepHeader.dacLpMode);
1882

S
Sujith 已提交
1883
		REG_RMW_FIELD(ah, AR_PHY_FRAME_CTL, AR_PHY_FRAME_CTL_TX_CLIP,
1884
			      pModal->miscBits >> 2);
1885 1886

		REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL9,
1887 1888
			      AR_PHY_TX_DESIRED_SCALE_CCK,
			      eep->baseEepHeader.desiredScaleCCK);
S
Sujith 已提交
1889
	}
1890 1891
}

S
Sujith 已提交
1892
static void ath9k_hw_def_set_addac(struct ath_hw *ah,
1893 1894 1895 1896
				   struct ath9k_channel *chan)
{
#define XPA_LVL_FREQ(cnt) (pModal->xpaBiasLvlFreq[cnt])
	struct modal_eep_header *pModal;
1897
	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
1898 1899
	u8 biaslevel;

1900
	if (ah->hw_version.macVersion != AR_SREV_VERSION_9160)
1901 1902
		return;

S
Sujith 已提交
1903
	if (ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_MINOR_VER_7)
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
		return;

	pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);

	if (pModal->xpaBiasLvl != 0xff) {
		biaslevel = pModal->xpaBiasLvl;
	} else {
		u16 resetFreqBin, freqBin, freqCount = 0;
		struct chan_centers centers;

		ath9k_hw_get_channel_centers(ah, chan, &centers);
S
Sujith 已提交
1915

1916 1917 1918 1919
		resetFreqBin = FREQ2FBIN(centers.synth_center,
					 IS_CHAN_2GHZ(chan));
		freqBin = XPA_LVL_FREQ(0) & 0xff;
		biaslevel = (u8) (XPA_LVL_FREQ(0) >> 14);
S
Sujith 已提交
1920

1921
		freqCount++;
S
Sujith 已提交
1922

1923 1924
		while (freqCount < 3) {
			if (XPA_LVL_FREQ(freqCount) == 0x0)
S
Sujith 已提交
1925
				break;
1926 1927 1928 1929 1930 1931 1932

			freqBin = XPA_LVL_FREQ(freqCount) & 0xff;
			if (resetFreqBin >= freqBin)
				biaslevel = (u8)(XPA_LVL_FREQ(freqCount) >> 14);
			else
				break;
			freqCount++;
S
Sujith 已提交
1933 1934 1935
		}
	}

1936
	if (IS_CHAN_2GHZ(chan)) {
1937
		INI_RA(&ah->iniAddac, 7, 1) = (INI_RA(&ah->iniAddac,
1938 1939
					7, 1) & (~0x18)) | biaslevel << 3;
	} else {
1940
		INI_RA(&ah->iniAddac, 6, 1) = (INI_RA(&ah->iniAddac,
1941 1942 1943 1944
					6, 1) & (~0xc0)) | biaslevel << 6;
	}
#undef XPA_LVL_FREQ
}
S
Sujith 已提交
1945

S
Sujith 已提交
1946 1947 1948 1949 1950 1951 1952
static void ath9k_hw_get_def_gain_boundaries_pdadcs(struct ath_hw *ah,
				struct ath9k_channel *chan,
				struct cal_data_per_freq *pRawDataSet,
				u8 *bChans, u16 availPiers,
				u16 tPdGainOverlap, int16_t *pMinCalPower,
				u16 *pPdGainBoundaries, u8 *pPDADCValues,
				u16 numXpdGains)
1953
{
S
Sujith 已提交
1954 1955 1956 1957 1958 1959 1960 1961 1962
	int i, j, k;
	int16_t ss;
	u16 idxL = 0, idxR = 0, numPiers;
	static u8 vpdTableL[AR5416_NUM_PD_GAINS]
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
	static u8 vpdTableR[AR5416_NUM_PD_GAINS]
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
	static u8 vpdTableI[AR5416_NUM_PD_GAINS]
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
S
Sujith 已提交
1963

S
Sujith 已提交
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
	u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
	u8 minPwrT4[AR5416_NUM_PD_GAINS];
	u8 maxPwrT4[AR5416_NUM_PD_GAINS];
	int16_t vpdStep;
	int16_t tmpVal;
	u16 sizeCurrVpdTable, maxIndex, tgtIndex;
	bool match;
	int16_t minDelta = 0;
	struct chan_centers centers;

	ath9k_hw_get_channel_centers(ah, chan, &centers);

	for (numPiers = 0; numPiers < availPiers; numPiers++) {
		if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
			break;
	}

	match = ath9k_hw_get_lower_upper_index((u8)FREQ2FBIN(centers.synth_center,
							     IS_CHAN_2GHZ(chan)),
					       bChans, numPiers, &idxL, &idxR);

	if (match) {
		for (i = 0; i < numXpdGains; i++) {
			minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
			maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
					pRawDataSet[idxL].pwrPdg[i],
					pRawDataSet[idxL].vpdPdg[i],
					AR5416_PD_GAIN_ICEPTS,
					vpdTableI[i]);
		}
	} else {
		for (i = 0; i < numXpdGains; i++) {
			pVpdL = pRawDataSet[idxL].vpdPdg[i];
			pPwrL = pRawDataSet[idxL].pwrPdg[i];
			pVpdR = pRawDataSet[idxR].vpdPdg[i];
			pPwrR = pRawDataSet[idxR].pwrPdg[i];

			minPwrT4[i] = max(pPwrL[0], pPwrR[0]);

			maxPwrT4[i] =
				min(pPwrL[AR5416_PD_GAIN_ICEPTS - 1],
				    pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);


			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
						pPwrL, pVpdL,
						AR5416_PD_GAIN_ICEPTS,
						vpdTableL[i]);
			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
						pPwrR, pVpdR,
						AR5416_PD_GAIN_ICEPTS,
						vpdTableR[i]);

			for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
				vpdTableI[i][j] =
					(u8)(ath9k_hw_interpolate((u16)
					     FREQ2FBIN(centers.
						       synth_center,
						       IS_CHAN_2GHZ
						       (chan)),
					     bChans[idxL], bChans[idxR],
					     vpdTableL[i][j], vpdTableR[i][j]));
			}
		}
	}

	*pMinCalPower = (int16_t)(minPwrT4[0] / 2);

	k = 0;

	for (i = 0; i < numXpdGains; i++) {
		if (i == (numXpdGains - 1))
			pPdGainBoundaries[i] =
				(u16)(maxPwrT4[i] / 2);
		else
			pPdGainBoundaries[i] =
				(u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);

		pPdGainBoundaries[i] =
			min((u16)AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);

2046
		if ((i == 0) && !AR_SREV_5416_20_OR_LATER(ah)) {
S
Sujith 已提交
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
			minDelta = pPdGainBoundaries[0] - 23;
			pPdGainBoundaries[0] = 23;
		} else {
			minDelta = 0;
		}

		if (i == 0) {
			if (AR_SREV_9280_10_OR_LATER(ah))
				ss = (int16_t)(0 - (minPwrT4[i] / 2));
			else
				ss = 0;
		} else {
			ss = (int16_t)((pPdGainBoundaries[i - 1] -
					(minPwrT4[i] / 2)) -
				       tPdGainOverlap + 1 + minDelta);
		}
		vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
		vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);

		while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
			tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
			pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
			ss++;
		}

		sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
		tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
				(minPwrT4[i] / 2));
		maxIndex = (tgtIndex < sizeCurrVpdTable) ?
			tgtIndex : sizeCurrVpdTable;

		while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
			pPDADCValues[k++] = vpdTableI[i][ss++];
		}
S
Sujith 已提交
2081

S
Sujith 已提交
2082 2083 2084
		vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
				    vpdTableI[i][sizeCurrVpdTable - 2]);
		vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
S
Sujith 已提交
2085

S
Sujith 已提交
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
		if (tgtIndex > maxIndex) {
			while ((ss <= tgtIndex) &&
			       (k < (AR5416_NUM_PDADC_VALUES - 1))) {
				tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
						    (ss - maxIndex + 1) * vpdStep));
				pPDADCValues[k++] = (u8)((tmpVal > 255) ?
							 255 : tmpVal);
				ss++;
			}
		}
	}
S
Sujith 已提交
2097

S
Sujith 已提交
2098 2099 2100
	while (i < AR5416_PD_GAINS_IN_MASK) {
		pPdGainBoundaries[i] = pPdGainBoundaries[i - 1];
		i++;
S
Sujith 已提交
2101 2102
	}

S
Sujith 已提交
2103 2104 2105 2106
	while (k < AR5416_NUM_PDADC_VALUES) {
		pPDADCValues[k] = pPDADCValues[k - 1];
		k++;
	}
S
Sujith 已提交
2107

S
Sujith 已提交
2108
	return;
S
Sujith 已提交
2109 2110
}

2111
static void ath9k_hw_set_def_power_cal_table(struct ath_hw *ah,
S
Sujith 已提交
2112 2113
				  struct ath9k_channel *chan,
				  int16_t *pTxPowerIndexOffset)
S
Sujith 已提交
2114
{
2115 2116 2117 2118
#define SM_PD_GAIN(x) SM(0x38, AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_##x)
#define SM_PDGAIN_B(x, y) \
		SM((gainBoundaries[x]), AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_##y)

2119
	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
S
Sujith 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
	struct cal_data_per_freq *pRawDataset;
	u8 *pCalBChans = NULL;
	u16 pdGainOverlap_t2;
	static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
	u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
	u16 numPiers, i, j;
	int16_t tMinCalPower;
	u16 numXpdGain, xpdMask;
	u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
	u32 reg32, regOffset, regChainOffset;
	int16_t modalIdx;
S
Sujith 已提交
2131

S
Sujith 已提交
2132 2133
	modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
	xpdMask = pEepData->modalHeader[modalIdx].xpdGain;
S
Sujith 已提交
2134

S
Sujith 已提交
2135 2136 2137 2138 2139 2140 2141 2142
	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
	    AR5416_EEP_MINOR_VER_2) {
		pdGainOverlap_t2 =
			pEepData->modalHeader[modalIdx].pdGainOverlap;
	} else {
		pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
					    AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
	}
S
Sujith 已提交
2143

S
Sujith 已提交
2144 2145 2146 2147 2148 2149
	if (IS_CHAN_2GHZ(chan)) {
		pCalBChans = pEepData->calFreqPier2G;
		numPiers = AR5416_NUM_2G_CAL_PIERS;
	} else {
		pCalBChans = pEepData->calFreqPier5G;
		numPiers = AR5416_NUM_5G_CAL_PIERS;
S
Sujith 已提交
2150 2151
	}

2152 2153 2154 2155 2156 2157
	if (OLC_FOR_AR9280_20_LATER && IS_CHAN_2GHZ(chan)) {
		pRawDataset = pEepData->calPierData2G[0];
		ah->initPDADC = ((struct calDataPerFreqOpLoop *)
				 pRawDataset)->vpdPdg[0][0];
	}

S
Sujith 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166
	numXpdGain = 0;

	for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
		if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
			if (numXpdGain >= AR5416_NUM_PD_GAINS)
				break;
			xpdGainValues[numXpdGain] =
				(u16)(AR5416_PD_GAINS_IN_MASK - i);
			numXpdGain++;
S
Sujith 已提交
2167 2168 2169
		}
	}

S
Sujith 已提交
2170 2171 2172 2173 2174 2175 2176 2177
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
		      (numXpdGain - 1) & 0x3);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
		      xpdGainValues[0]);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
		      xpdGainValues[1]);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
		      xpdGainValues[2]);
S
Sujith 已提交
2178

S
Sujith 已提交
2179
	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
2180
		if (AR_SREV_5416_20_OR_LATER(ah) &&
2181
		    (ah->rxchainmask == 5 || ah->txchainmask == 5) &&
S
Sujith 已提交
2182 2183 2184 2185
		    (i != 0)) {
			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
		} else
			regChainOffset = i * 0x1000;
S
Sujith 已提交
2186

S
Sujith 已提交
2187 2188 2189 2190 2191
		if (pEepData->baseEepHeader.txMask & (1 << i)) {
			if (IS_CHAN_2GHZ(chan))
				pRawDataset = pEepData->calPierData2G[i];
			else
				pRawDataset = pEepData->calPierData5G[i];
S
Sujith 已提交
2192

2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212

			if (OLC_FOR_AR9280_20_LATER) {
				u8 pcdacIdx;
				u8 txPower;

				ath9k_get_txgain_index(ah, chan,
				(struct calDataPerFreqOpLoop *)pRawDataset,
				pCalBChans, numPiers, &txPower, &pcdacIdx);
				ath9k_olc_get_pdadcs(ah, pcdacIdx,
						     txPower/2, pdadcValues);
			} else {
				ath9k_hw_get_def_gain_boundaries_pdadcs(ah,
							chan, pRawDataset,
							pCalBChans, numPiers,
							pdGainOverlap_t2,
							&tMinCalPower,
							gainBoundaries,
							pdadcValues,
							numXpdGain);
			}
S
Sujith 已提交
2213

2214
			if ((i == 0) || AR_SREV_5416_20_OR_LATER(ah)) {
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
				if (OLC_FOR_AR9280_20_LATER) {
					REG_WRITE(ah,
						AR_PHY_TPCRG5 + regChainOffset,
						SM(0x6,
						AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
						SM_PD_GAIN(1) | SM_PD_GAIN(2) |
						SM_PD_GAIN(3) | SM_PD_GAIN(4));
				} else {
					REG_WRITE(ah,
						AR_PHY_TPCRG5 + regChainOffset,
						SM(pdGainOverlap_t2,
						AR_PHY_TPCRG5_PD_GAIN_OVERLAP)|
						SM_PDGAIN_B(0, 1) |
						SM_PDGAIN_B(1, 2) |
						SM_PDGAIN_B(2, 3) |
						SM_PDGAIN_B(3, 4));
				}
S
Sujith 已提交
2232
			}
S
Sujith 已提交
2233

S
Sujith 已提交
2234 2235 2236 2237 2238 2239 2240
			regOffset = AR_PHY_BASE + (672 << 2) + regChainOffset;
			for (j = 0; j < 32; j++) {
				reg32 = ((pdadcValues[4 * j + 0] & 0xFF) << 0) |
					((pdadcValues[4 * j + 1] & 0xFF) << 8) |
					((pdadcValues[4 * j + 2] & 0xFF) << 16)|
					((pdadcValues[4 * j + 3] & 0xFF) << 24);
				REG_WRITE(ah, regOffset, reg32);
S
Sujith 已提交
2241

S
Sujith 已提交
2242
				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
2243 2244 2245
					"PDADC (%d,%4x): %4.4x %8.8x\n",
					i, regChainOffset, regOffset,
					reg32);
S
Sujith 已提交
2246
				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
2247 2248 2249 2250 2251 2252 2253 2254 2255
					"PDADC: Chain %d | PDADC %3d "
					"Value %3d | PDADC %3d Value %3d | "
					"PDADC %3d Value %3d | PDADC %3d "
					"Value %3d |\n",
					i, 4 * j, pdadcValues[4 * j],
					4 * j + 1, pdadcValues[4 * j + 1],
					4 * j + 2, pdadcValues[4 * j + 2],
					4 * j + 3,
					pdadcValues[4 * j + 3]);
2256

S
Sujith 已提交
2257 2258 2259
				regOffset += 4;
			}
		}
2260 2261
	}

S
Sujith 已提交
2262
	*pTxPowerIndexOffset = 0;
2263 2264
#undef SM_PD_GAIN
#undef SM_PDGAIN_B
S
Sujith 已提交
2265 2266
}

2267
static void ath9k_hw_set_def_power_per_rate_table(struct ath_hw *ah,
S
Sujith 已提交
2268 2269 2270 2271 2272 2273
						  struct ath9k_channel *chan,
						  int16_t *ratesArray,
						  u16 cfgCtl,
						  u16 AntennaReduction,
						  u16 twiceMaxRegulatoryPower,
						  u16 powerLimit)
2274
{
S
Sujith 已提交
2275 2276
#define REDUCE_SCALED_POWER_BY_TWO_CHAIN     6  /* 10*log10(2)*2 */
#define REDUCE_SCALED_POWER_BY_THREE_CHAIN   10 /* 10*log10(3)*2 */
2277

2278
	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
S
Sujith 已提交
2279 2280 2281
	u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
	static const u16 tpScaleReductionTable[5] =
		{ 0, 3, 6, 9, AR5416_MAX_RATE_POWER };
2282

S
Sujith 已提交
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
	int i;
	int16_t twiceLargestAntenna;
	struct cal_ctl_data *rep;
	struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
		0, { 0, 0, 0, 0}
	};
	struct cal_target_power_leg targetPowerOfdmExt = {
		0, { 0, 0, 0, 0} }, targetPowerCckExt = {
		0, { 0, 0, 0, 0 }
	};
	struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
		0, {0, 0, 0, 0}
	};
	u16 scaledPower = 0, minCtlPower, maxRegAllowedPower;
	u16 ctlModesFor11a[] =
		{ CTL_11A, CTL_5GHT20, CTL_11A_EXT, CTL_5GHT40 };
	u16 ctlModesFor11g[] =
		{ CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT,
		  CTL_2GHT40
		};
	u16 numCtlModes, *pCtlMode, ctlMode, freq;
	struct chan_centers centers;
	int tx_chainmask;
	u16 twiceMinEdgePower;
2307

2308
	tx_chainmask = ah->txchainmask;
2309

S
Sujith 已提交
2310
	ath9k_hw_get_channel_centers(ah, chan, &centers);
2311

S
Sujith 已提交
2312 2313 2314 2315 2316
	twiceLargestAntenna = max(
		pEepData->modalHeader
			[IS_CHAN_2GHZ(chan)].antennaGainCh[0],
		pEepData->modalHeader
			[IS_CHAN_2GHZ(chan)].antennaGainCh[1]);
2317

S
Sujith 已提交
2318 2319 2320
	twiceLargestAntenna = max((u8)twiceLargestAntenna,
				  pEepData->modalHeader
				  [IS_CHAN_2GHZ(chan)].antennaGainCh[2]);
2321

S
Sujith 已提交
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
	twiceLargestAntenna = (int16_t)min(AntennaReduction -
					   twiceLargestAntenna, 0);

	maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;

	if (ah->regulatory.tp_scale != ATH9K_TP_SCALE_MAX) {
		maxRegAllowedPower -=
			(tpScaleReductionTable[(ah->regulatory.tp_scale)] * 2);
	}

	scaledPower = min(powerLimit, maxRegAllowedPower);

	switch (ar5416_get_ntxchains(tx_chainmask)) {
	case 1:
		break;
	case 2:
		scaledPower -= REDUCE_SCALED_POWER_BY_TWO_CHAIN;
		break;
	case 3:
		scaledPower -= REDUCE_SCALED_POWER_BY_THREE_CHAIN;
		break;
	}

	scaledPower = max((u16)0, scaledPower);

	if (IS_CHAN_2GHZ(chan)) {
		numCtlModes = ARRAY_SIZE(ctlModesFor11g) -
			SUB_NUM_CTL_MODES_AT_2G_40;
		pCtlMode = ctlModesFor11g;

		ath9k_hw_get_legacy_target_powers(ah, chan,
			pEepData->calTargetPowerCck,
			AR5416_NUM_2G_CCK_TARGET_POWERS,
			&targetPowerCck, 4, false);
		ath9k_hw_get_legacy_target_powers(ah, chan,
			pEepData->calTargetPower2G,
			AR5416_NUM_2G_20_TARGET_POWERS,
			&targetPowerOfdm, 4, false);
		ath9k_hw_get_target_powers(ah, chan,
			pEepData->calTargetPower2GHT20,
			AR5416_NUM_2G_20_TARGET_POWERS,
			&targetPowerHt20, 8, false);

		if (IS_CHAN_HT40(chan)) {
			numCtlModes = ARRAY_SIZE(ctlModesFor11g);
			ath9k_hw_get_target_powers(ah, chan,
				pEepData->calTargetPower2GHT40,
				AR5416_NUM_2G_40_TARGET_POWERS,
				&targetPowerHt40, 8, true);
			ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPowerCck,
				AR5416_NUM_2G_CCK_TARGET_POWERS,
				&targetPowerCckExt, 4, true);
			ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPower2G,
				AR5416_NUM_2G_20_TARGET_POWERS,
				&targetPowerOfdmExt, 4, true);
		}
	} else {
		numCtlModes = ARRAY_SIZE(ctlModesFor11a) -
			SUB_NUM_CTL_MODES_AT_5G_40;
		pCtlMode = ctlModesFor11a;

		ath9k_hw_get_legacy_target_powers(ah, chan,
			pEepData->calTargetPower5G,
			AR5416_NUM_5G_20_TARGET_POWERS,
			&targetPowerOfdm, 4, false);
		ath9k_hw_get_target_powers(ah, chan,
			pEepData->calTargetPower5GHT20,
			AR5416_NUM_5G_20_TARGET_POWERS,
			&targetPowerHt20, 8, false);

		if (IS_CHAN_HT40(chan)) {
			numCtlModes = ARRAY_SIZE(ctlModesFor11a);
			ath9k_hw_get_target_powers(ah, chan,
				pEepData->calTargetPower5GHT40,
				AR5416_NUM_5G_40_TARGET_POWERS,
				&targetPowerHt40, 8, true);
			ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPower5G,
				AR5416_NUM_5G_20_TARGET_POWERS,
				&targetPowerOfdmExt, 4, true);
		}
2405 2406
	}

S
Sujith 已提交
2407 2408 2409 2410 2411 2412 2413 2414 2415
	for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
		bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
			(pCtlMode[ctlMode] == CTL_2GHT40);
		if (isHt40CtlMode)
			freq = centers.synth_center;
		else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
			freq = centers.ext_center;
		else
			freq = centers.ctl_center;
2416

S
Sujith 已提交
2417 2418 2419
		if (ah->eep_ops->get_eeprom_ver(ah) == 14 &&
		    ah->eep_ops->get_eeprom_rev(ah) <= 2)
			twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
2420

S
Sujith 已提交
2421
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
2422 2423 2424 2425
			"LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d, "
			"EXT_ADDITIVE %d\n",
			ctlMode, numCtlModes, isHt40CtlMode,
			(pCtlMode[ctlMode] & EXT_ADDITIVE));
2426

S
Sujith 已提交
2427
		for (i = 0; (i < AR5416_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {
S
Sujith 已提交
2428
			DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
2429 2430 2431 2432 2433
				"  LOOP-Ctlidx %d: cfgCtl 0x%2.2x "
				"pCtlMode 0x%2.2x ctlIndex 0x%2.2x "
				"chan %d\n",
				i, cfgCtl, pCtlMode[ctlMode],
				pEepData->ctlIndex[i], chan->channel);
2434

S
Sujith 已提交
2435 2436 2437 2438 2439 2440 2441
			if ((((cfgCtl & ~CTL_MODE_M) |
			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
			     pEepData->ctlIndex[i]) ||
			    (((cfgCtl & ~CTL_MODE_M) |
			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
			     ((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
				rep = &(pEepData->ctlData[i]);
2442

S
Sujith 已提交
2443 2444 2445
				twiceMinEdgePower = ath9k_hw_get_max_edge_power(freq,
				rep->ctlEdges[ar5416_get_ntxchains(tx_chainmask) - 1],
				IS_CHAN_2GHZ(chan), AR5416_NUM_BAND_EDGES);
2446

S
Sujith 已提交
2447
				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
					"    MATCH-EE_IDX %d: ch %d is2 %d "
					"2xMinEdge %d chainmask %d chains %d\n",
					i, freq, IS_CHAN_2GHZ(chan),
					twiceMinEdgePower, tx_chainmask,
					ar5416_get_ntxchains
					(tx_chainmask));
				if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
					twiceMaxEdgePower = min(twiceMaxEdgePower,
								twiceMinEdgePower);
				} else {
					twiceMaxEdgePower = twiceMinEdgePower;
					break;
				}
			}
		}
2463

S
Sujith 已提交
2464 2465
		minCtlPower = min(twiceMaxEdgePower, scaledPower);

S
Sujith 已提交
2466
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
S
Sujith 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
			"    SEL-Min ctlMode %d pCtlMode %d "
			"2xMaxEdge %d sP %d minCtlPwr %d\n",
			ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
			scaledPower, minCtlPower);

		switch (pCtlMode[ctlMode]) {
		case CTL_11B:
			for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x); i++) {
				targetPowerCck.tPow2x[i] =
					min((u16)targetPowerCck.tPow2x[i],
					    minCtlPower);
			}
			break;
		case CTL_11A:
		case CTL_11G:
			for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x); i++) {
				targetPowerOfdm.tPow2x[i] =
					min((u16)targetPowerOfdm.tPow2x[i],
					    minCtlPower);
			}
			break;
		case CTL_5GHT20:
		case CTL_2GHT20:
			for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++) {
				targetPowerHt20.tPow2x[i] =
					min((u16)targetPowerHt20.tPow2x[i],
					    minCtlPower);
			}
			break;
		case CTL_11B_EXT:
			targetPowerCckExt.tPow2x[0] = min((u16)
					targetPowerCckExt.tPow2x[0],
					minCtlPower);
			break;
		case CTL_11A_EXT:
		case CTL_11G_EXT:
			targetPowerOfdmExt.tPow2x[0] = min((u16)
					targetPowerOfdmExt.tPow2x[0],
					minCtlPower);
			break;
		case CTL_5GHT40:
		case CTL_2GHT40:
			for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
				targetPowerHt40.tPow2x[i] =
					min((u16)targetPowerHt40.tPow2x[i],
					    minCtlPower);
			}
			break;
		default:
			break;
2517 2518 2519
		}
	}

S
Sujith 已提交
2520 2521 2522 2523 2524 2525 2526
	ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
		ratesArray[rate18mb] = ratesArray[rate24mb] =
		targetPowerOfdm.tPow2x[0];
	ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
	ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
	ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
	ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
2527

S
Sujith 已提交
2528 2529
	for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
		ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
2530

S
Sujith 已提交
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
	if (IS_CHAN_2GHZ(chan)) {
		ratesArray[rate1l] = targetPowerCck.tPow2x[0];
		ratesArray[rate2s] = ratesArray[rate2l] =
			targetPowerCck.tPow2x[1];
		ratesArray[rate5_5s] = ratesArray[rate5_5l] =
			targetPowerCck.tPow2x[2];
		ratesArray[rate11s] = ratesArray[rate11l] =
			targetPowerCck.tPow2x[3];
	}
	if (IS_CHAN_HT40(chan)) {
		for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
			ratesArray[rateHt40_0 + i] =
				targetPowerHt40.tPow2x[i];
		}
		ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
		ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
		ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
		if (IS_CHAN_2GHZ(chan)) {
			ratesArray[rateExtCck] =
				targetPowerCckExt.tPow2x[0];
		}
	}
}
2554

2555
static void ath9k_hw_def_set_txpower(struct ath_hw *ah,
S
Sujith 已提交
2556 2557 2558 2559 2560 2561
				    struct ath9k_channel *chan,
				    u16 cfgCtl,
				    u8 twiceAntennaReduction,
				    u8 twiceMaxRegulatoryPower,
				    u8 powerLimit)
{
2562
#define RT_AR_DELTA(x) (ratesArray[x] - cck_ofdm_delta)
2563
	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
S
Sujith 已提交
2564 2565 2566 2567 2568
	struct modal_eep_header *pModal =
		&(pEepData->modalHeader[IS_CHAN_2GHZ(chan)]);
	int16_t ratesArray[Ar5416RateSize];
	int16_t txPowerIndexOffset = 0;
	u8 ht40PowerIncForPdadc = 2;
2569
	int i, cck_ofdm_delta = 0;
2570

S
Sujith 已提交
2571
	memset(ratesArray, 0, sizeof(ratesArray));
2572

S
Sujith 已提交
2573 2574 2575 2576
	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
	    AR5416_EEP_MINOR_VER_2) {
		ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
	}
2577

2578
	ath9k_hw_set_def_power_per_rate_table(ah, chan,
S
Sujith 已提交
2579 2580 2581
					       &ratesArray[0], cfgCtl,
					       twiceAntennaReduction,
					       twiceMaxRegulatoryPower,
2582
					       powerLimit);
2583

2584
	ath9k_hw_set_def_power_cal_table(ah, chan, &txPowerIndexOffset);
2585

S
Sujith 已提交
2586 2587 2588 2589
	for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
		ratesArray[i] =	(int16_t)(txPowerIndexOffset + ratesArray[i]);
		if (ratesArray[i] > AR5416_MAX_RATE_POWER)
			ratesArray[i] = AR5416_MAX_RATE_POWER;
2590 2591
	}

S
Sujith 已提交
2592 2593 2594 2595
	if (AR_SREV_9280_10_OR_LATER(ah)) {
		for (i = 0; i < Ar5416RateSize; i++)
			ratesArray[i] -= AR5416_PWR_TABLE_OFFSET * 2;
	}
2596

S
Sujith 已提交
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
		  ATH9K_POW_SM(ratesArray[rate18mb], 24)
		  | ATH9K_POW_SM(ratesArray[rate12mb], 16)
		  | ATH9K_POW_SM(ratesArray[rate9mb], 8)
		  | ATH9K_POW_SM(ratesArray[rate6mb], 0));
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
		  ATH9K_POW_SM(ratesArray[rate54mb], 24)
		  | ATH9K_POW_SM(ratesArray[rate48mb], 16)
		  | ATH9K_POW_SM(ratesArray[rate36mb], 8)
		  | ATH9K_POW_SM(ratesArray[rate24mb], 0));
2607

S
Sujith 已提交
2608
	if (IS_CHAN_2GHZ(chan)) {
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
		if (OLC_FOR_AR9280_20_LATER) {
			cck_ofdm_delta = 2;
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
				ATH9K_POW_SM(RT_AR_DELTA(rate2s), 24)
				| ATH9K_POW_SM(RT_AR_DELTA(rate2l), 16)
				| ATH9K_POW_SM(ratesArray[rateXr], 8)
				| ATH9K_POW_SM(RT_AR_DELTA(rate1l), 0));
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
				ATH9K_POW_SM(RT_AR_DELTA(rate11s), 24)
				| ATH9K_POW_SM(RT_AR_DELTA(rate11l), 16)
				| ATH9K_POW_SM(RT_AR_DELTA(rate5_5s), 8)
				| ATH9K_POW_SM(RT_AR_DELTA(rate5_5l), 0));
		} else {
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
				ATH9K_POW_SM(ratesArray[rate2s], 24)
				| ATH9K_POW_SM(ratesArray[rate2l], 16)
				| ATH9K_POW_SM(ratesArray[rateXr], 8)
				| ATH9K_POW_SM(ratesArray[rate1l], 0));
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
				ATH9K_POW_SM(ratesArray[rate11s], 24)
				| ATH9K_POW_SM(ratesArray[rate11l], 16)
				| ATH9K_POW_SM(ratesArray[rate5_5s], 8)
				| ATH9K_POW_SM(ratesArray[rate5_5l], 0));
		}
S
Sujith 已提交
2633
	}
2634

S
Sujith 已提交
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
		  ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
		  | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
		  | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
		  | ATH9K_POW_SM(ratesArray[rateHt20_0], 0));
	REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
		  ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
		  | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
		  | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
		  | ATH9K_POW_SM(ratesArray[rateHt20_4], 0));
S
Sujith 已提交
2645

S
Sujith 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
	if (IS_CHAN_HT40(chan)) {
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
			  ATH9K_POW_SM(ratesArray[rateHt40_3] +
				       ht40PowerIncForPdadc, 24)
			  | ATH9K_POW_SM(ratesArray[rateHt40_2] +
					 ht40PowerIncForPdadc, 16)
			  | ATH9K_POW_SM(ratesArray[rateHt40_1] +
					 ht40PowerIncForPdadc, 8)
			  | ATH9K_POW_SM(ratesArray[rateHt40_0] +
					 ht40PowerIncForPdadc, 0));
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
			  ATH9K_POW_SM(ratesArray[rateHt40_7] +
				       ht40PowerIncForPdadc, 24)
			  | ATH9K_POW_SM(ratesArray[rateHt40_6] +
					 ht40PowerIncForPdadc, 16)
			  | ATH9K_POW_SM(ratesArray[rateHt40_5] +
					 ht40PowerIncForPdadc, 8)
			  | ATH9K_POW_SM(ratesArray[rateHt40_4] +
					 ht40PowerIncForPdadc, 0));
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
		if (OLC_FOR_AR9280_20_LATER) {
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
				| ATH9K_POW_SM(RT_AR_DELTA(rateExtCck), 16)
				| ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
				| ATH9K_POW_SM(RT_AR_DELTA(rateDupCck), 0));
		} else {
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
				| ATH9K_POW_SM(ratesArray[rateExtCck], 16)
				| ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
				| ATH9K_POW_SM(ratesArray[rateDupCck], 0));
		}
S
Sujith 已提交
2678
	}
2679

S
Sujith 已提交
2680 2681 2682
	REG_WRITE(ah, AR_PHY_POWER_TX_SUB,
		  ATH9K_POW_SM(pModal->pwrDecreaseFor3Chain, 6)
		  | ATH9K_POW_SM(pModal->pwrDecreaseFor2Chain, 0));
2683

S
Sujith 已提交
2684
	i = rate6mb;
2685

S
Sujith 已提交
2686 2687 2688 2689
	if (IS_CHAN_HT40(chan))
		i = rateHt40_0;
	else if (IS_CHAN_HT20(chan))
		i = rateHt20_0;
2690

S
Sujith 已提交
2691 2692 2693 2694 2695 2696
	if (AR_SREV_9280_10_OR_LATER(ah))
		ah->regulatory.max_power_level =
			ratesArray[i] + AR5416_PWR_TABLE_OFFSET * 2;
	else
		ah->regulatory.max_power_level = ratesArray[i];

2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
	switch(ar5416_get_ntxchains(ah->txchainmask)) {
	case 1:
		break;
	case 2:
		ah->regulatory.max_power_level += INCREASE_MAXPOW_BY_TWO_CHAIN;
		break;
	case 3:
		ah->regulatory.max_power_level += INCREASE_MAXPOW_BY_THREE_CHAIN;
		break;
	default:
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
			"Invalid chainmask configuration\n");
		break;
	}
2711 2712
}

S
Sujith 已提交
2713
static u8 ath9k_hw_def_get_num_ant_config(struct ath_hw *ah,
2714
					  enum ieee80211_band freq_band)
S
Sujith 已提交
2715
{
2716
	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
S
Sujith 已提交
2717
	struct modal_eep_header *pModal =
2718
		&(eep->modalHeader[ATH9K_HAL_FREQ_BAND_2GHZ == freq_band]);
S
Sujith 已提交
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
	struct base_eep_header *pBase = &eep->baseEepHeader;
	u8 num_ant_config;

	num_ant_config = 1;

	if (pBase->version >= 0x0E0D)
		if (pModal->useAnt1)
			num_ant_config += 1;

	return num_ant_config;
}

S
Sujith 已提交
2731 2732
static u16 ath9k_hw_def_get_eeprom_antenna_cfg(struct ath_hw *ah,
					       struct ath9k_channel *chan)
2733
{
2734
	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
S
Sujith 已提交
2735 2736 2737 2738
	struct modal_eep_header *pModal =
		&(eep->modalHeader[IS_CHAN_2GHZ(chan)]);

	return pModal->antCtrlCommon & 0xFFFF;
2739 2740
}

2741
static u16 ath9k_hw_def_get_spur_channel(struct ath_hw *ah, u16 i, bool is2GHz)
S
Sujith 已提交
2742
{
2743
#define EEP_DEF_SPURCHAN \
2744
	(ah->eeprom.def.modalHeader[is2GHz].spurChans[i].spurChan)
S
Sujith 已提交
2745

S
Sujith 已提交
2746 2747 2748 2749
	u16 spur_val = AR_NO_SPUR;

	DPRINTF(ah->ah_sc, ATH_DBG_ANI,
		"Getting spur idx %d is2Ghz. %d val %x\n",
2750
		i, is2GHz, ah->config.spurchans[i][is2GHz]);
S
Sujith 已提交
2751

2752
	switch (ah->config.spurmode) {
S
Sujith 已提交
2753 2754 2755
	case SPUR_DISABLE:
		break;
	case SPUR_ENABLE_IOCTL:
2756
		spur_val = ah->config.spurchans[i][is2GHz];
S
Sujith 已提交
2757 2758 2759 2760
		DPRINTF(ah->ah_sc, ATH_DBG_ANI,
			"Getting spur val from new loc. %d\n", spur_val);
		break;
	case SPUR_ENABLE_EEPROM:
S
Sujith 已提交
2761
		spur_val = EEP_DEF_SPURCHAN;
S
Sujith 已提交
2762 2763 2764 2765 2766
		break;
	}

	return spur_val;

S
Sujith 已提交
2767
#undef EEP_DEF_SPURCHAN
S
Sujith 已提交
2768 2769
}

2770
static struct eeprom_ops eep_def_ops = {
S
Sujith 已提交
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
	.check_eeprom		= ath9k_hw_def_check_eeprom,
	.get_eeprom		= ath9k_hw_def_get_eeprom,
	.fill_eeprom		= ath9k_hw_def_fill_eeprom,
	.get_eeprom_ver		= ath9k_hw_def_get_eeprom_ver,
	.get_eeprom_rev		= ath9k_hw_def_get_eeprom_rev,
	.get_num_ant_config	= ath9k_hw_def_get_num_ant_config,
	.get_eeprom_antenna_cfg	= ath9k_hw_def_get_eeprom_antenna_cfg,
	.set_board_values	= ath9k_hw_def_set_board_values,
	.set_addac		= ath9k_hw_def_set_addac,
	.set_txpower		= ath9k_hw_def_set_txpower,
	.get_spur_channel	= ath9k_hw_def_get_spur_channel
2782 2783
};

2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796

static int ath9k_hw_AR9287_get_eeprom_ver(struct ath_hw *ah)
{
	return (ah->eeprom.map9287.baseEepHeader.version >> 12) & 0xF;
}

static int ath9k_hw_AR9287_get_eeprom_rev(struct ath_hw *ah)
{
	return (ah->eeprom.map9287.baseEepHeader.version) & 0xFFF;
}

static bool ath9k_hw_AR9287_fill_eeprom(struct ath_hw *ah)
{
2797
	struct ar9287_eeprom *eep = &ah->eeprom.map9287;
2798 2799 2800 2801 2802 2803 2804 2805
	u16 *eep_data;
	int addr, eep_start_loc = AR9287_EEP_START_LOC;
	eep_data = (u16 *)eep;
	if (!ath9k_hw_use_flash(ah)) {
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
				"Reading from EEPROM, not flash\n");
	}

2806
	for (addr = 0; addr < sizeof(struct ar9287_eeprom) / sizeof(u16);
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
			addr++)	{
		if (!ath9k_hw_nvram_read(ah, addr + eep_start_loc, eep_data)) {
			DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
					"Unable to read eeprom region \n");
			return false;
		}
		eep_data++;
	}
	return true;
}
static int ath9k_hw_AR9287_check_eeprom(struct ath_hw *ah)
{
2819
#define SIZE_EEPROM_87 (sizeof(struct ar9287_eeprom) / sizeof(u16))
2820 2821 2822 2823
	u32 sum = 0, el, integer;
	u16 temp, word, magic, magic2, *eepdata;
	int i, addr;
	bool need_swap = false;
2824
	struct ar9287_eeprom *eep = &ah->eeprom.map9287;
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922

	if (!ath9k_hw_use_flash(ah)) {
		if (!ath9k_hw_nvram_read
				(ah, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
					"Reading Magic # failed\n");
			return false;
		}

		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
				"Read Magic = 0x%04X\n", magic);
		if (magic != AR5416_EEPROM_MAGIC) {


			magic2 = swab16(magic);

			if (magic2 == AR5416_EEPROM_MAGIC) {
				need_swap = true;
				eepdata = (u16 *)(&ah->eeprom);

				for (addr = 0; addr < SIZE_EEPROM_87; addr++) {
					temp = swab16(*eepdata);
					*eepdata = temp;
					eepdata++;
				}
			} else {
				DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
						"Invalid EEPROM Magic. "
						"endianness mismatch.\n");
				return -EINVAL;            }
		}
	}
	DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "need_swap = %s.\n", need_swap ?
					   "True" : "False");

	if (need_swap)
		el = swab16(ah->eeprom.map9287.baseEepHeader.length);
	else
		el = ah->eeprom.map9287.baseEepHeader.length;

	eepdata = (u16 *)(&ah->eeprom);
	for (i = 0; i < min(el, SIZE_EEPROM_87); i++)
		sum ^= *eepdata++;

	if (need_swap) {
		word = swab16(eep->baseEepHeader.length);
		eep->baseEepHeader.length = word;

		word = swab16(eep->baseEepHeader.checksum);
		eep->baseEepHeader.checksum = word;

		word = swab16(eep->baseEepHeader.version);
		eep->baseEepHeader.version = word;

		word = swab16(eep->baseEepHeader.regDmn[0]);
		eep->baseEepHeader.regDmn[0] = word;

		word = swab16(eep->baseEepHeader.regDmn[1]);
		eep->baseEepHeader.regDmn[1] = word;

		word = swab16(eep->baseEepHeader.rfSilent);
		eep->baseEepHeader.rfSilent = word;

		word = swab16(eep->baseEepHeader.blueToothOptions);
		eep->baseEepHeader.blueToothOptions = word;

		word = swab16(eep->baseEepHeader.deviceCap);
		eep->baseEepHeader.deviceCap = word;

		integer = swab32(eep->modalHeader.antCtrlCommon);
		eep->modalHeader.antCtrlCommon = integer;

		for (i = 0; i < AR9287_MAX_CHAINS; i++) {
			integer = swab32(eep->modalHeader.antCtrlChain[i]);
			eep->modalHeader.antCtrlChain[i] = integer;
		}

		for (i = 0; i < AR9287_EEPROM_MODAL_SPURS; i++) {
			word = swab16(eep->modalHeader.spurChans[i].spurChan);
			eep->modalHeader.spurChans[i].spurChan = word;
		}
	}

	if (sum != 0xffff || ah->eep_ops->get_eeprom_ver(ah) != AR9287_EEP_VER
	    || ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_NO_BACK_VER) {
		DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
			"Bad EEPROM checksum 0x%x or revision 0x%04x\n",
			 sum, ah->eep_ops->get_eeprom_ver(ah));
		return -EINVAL;
	}

	return 0;
#undef SIZE_EEPROM_87
}

static u32 ath9k_hw_AR9287_get_eeprom(struct ath_hw *ah,
		enum eeprom_param param)
{
2923
	struct ar9287_eeprom *eep = &ah->eeprom.map9287;
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
	struct modal_eep_ar9287_header *pModal = &eep->modalHeader;
	struct base_eep_ar9287_header *pBase = &eep->baseEepHeader;
	u16 ver_minor;

	ver_minor = pBase->version & AR9287_EEP_VER_MINOR_MASK;
	switch (param) {
	case EEP_NFTHRESH_2:
		return pModal->noiseFloorThreshCh[0];
	case AR_EEPROM_MAC(0):
		return pBase->macAddr[0] << 8 | pBase->macAddr[1];
	case AR_EEPROM_MAC(1):
		return pBase->macAddr[2] << 8 | pBase->macAddr[3];
	case AR_EEPROM_MAC(2):
		return pBase->macAddr[4] << 8 | pBase->macAddr[5];
	case EEP_REG_0:
		return pBase->regDmn[0];
	case EEP_REG_1:
		return pBase->regDmn[1];
	case EEP_OP_CAP:
		return pBase->deviceCap;
	case EEP_OP_MODE:
		return pBase->opCapFlags;
	case EEP_RF_SILENT:
		return pBase->rfSilent;
	case EEP_MINOR_REV:
		return ver_minor;
	case EEP_TX_MASK:
		return pBase->txMask;
	case EEP_RX_MASK:
		return pBase->rxMask;
	case EEP_DEV_TYPE:
		return pBase->deviceType;
	case EEP_OL_PWRCTRL:
		return pBase->openLoopPwrCntl;
	case EEP_TEMPSENSE_SLOPE:
		if (ver_minor >= AR9287_EEP_MINOR_VER_2)
			return pBase->tempSensSlope;
		else
			return 0;
	case EEP_TEMPSENSE_SLOPE_PAL_ON:
		if (ver_minor >= AR9287_EEP_MINOR_VER_3)
			return pBase->tempSensSlopePalOn;
		else
			return 0;
	default:
		return 0;
	}
}


static void ath9k_hw_get_AR9287_gain_boundaries_pdadcs(struct ath_hw *ah,
				   struct ath9k_channel *chan,
				   struct cal_data_per_freq_ar9287 *pRawDataSet,
				   u8 *bChans,  u16 availPiers,
				   u16 tPdGainOverlap, int16_t *pMinCalPower,
				   u16 *pPdGainBoundaries, u8 *pPDADCValues,
				   u16 numXpdGains)
{
#define TMP_VAL_VPD_TABLE \
	((vpdTableI[i][sizeCurrVpdTable - 1] + (ss - maxIndex + 1) * vpdStep));
	int       i, j, k;
	int16_t   ss;
	u16  idxL = 0, idxR = 0, numPiers;
	u8   *pVpdL, *pVpdR, *pPwrL, *pPwrR;
	u8   minPwrT4[AR9287_NUM_PD_GAINS];
	u8   maxPwrT4[AR9287_NUM_PD_GAINS];
	int16_t   vpdStep;
	int16_t   tmpVal;
	u16  sizeCurrVpdTable, maxIndex, tgtIndex;
	bool    match;
	int16_t  minDelta = 0;
	struct chan_centers centers;
	static u8 vpdTableL[AR5416_EEP4K_NUM_PD_GAINS]
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
	static u8 vpdTableR[AR5416_EEP4K_NUM_PD_GAINS]
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];
	static u8 vpdTableI[AR5416_EEP4K_NUM_PD_GAINS]
		[AR5416_MAX_PWR_RANGE_IN_HALF_DB];

	ath9k_hw_get_channel_centers(ah, chan, &centers);
	for (numPiers = 0; numPiers < availPiers; numPiers++) {
		if (bChans[numPiers] == AR9287_BCHAN_UNUSED)
			break;
	}

	match = ath9k_hw_get_lower_upper_index(
				   (u8)FREQ2FBIN(centers.synth_center,
				    IS_CHAN_2GHZ(chan)), bChans, numPiers,
				    &idxL, &idxR);

	if (match) {
		for (i = 0; i < numXpdGains; i++) {
			minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
			maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
					pRawDataSet[idxL].pwrPdg[i],
					pRawDataSet[idxL].vpdPdg[i],
					AR9287_PD_GAIN_ICEPTS, vpdTableI[i]);
		}
	} else {
		for (i = 0; i < numXpdGains; i++) {
			pVpdL = pRawDataSet[idxL].vpdPdg[i];
			pPwrL = pRawDataSet[idxL].pwrPdg[i];
			pVpdR = pRawDataSet[idxR].vpdPdg[i];
			pPwrR = pRawDataSet[idxR].pwrPdg[i];

			minPwrT4[i] = max(pPwrL[0], pPwrR[0]);

			maxPwrT4[i] =
				min(pPwrL[AR9287_PD_GAIN_ICEPTS - 1],
				    pPwrR[AR9287_PD_GAIN_ICEPTS - 1]);

			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
					pPwrL, pVpdL,
					AR9287_PD_GAIN_ICEPTS,
					vpdTableL[i]);
			ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
					pPwrR, pVpdR,
					AR9287_PD_GAIN_ICEPTS,
					vpdTableR[i]);

			for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
				vpdTableI[i][j] =
					(u8)(ath9k_hw_interpolate((u16)
					FREQ2FBIN(centers. synth_center,
					IS_CHAN_2GHZ(chan)),
					bChans[idxL], bChans[idxR],
					vpdTableL[i][j], vpdTableR[i][j]));
			}
		}
	}
	*pMinCalPower = (int16_t)(minPwrT4[0] / 2);

	k = 0;
	for (i = 0; i < numXpdGains; i++) {
		if (i == (numXpdGains - 1))
			pPdGainBoundaries[i] = (u16)(maxPwrT4[i] / 2);
		else
			pPdGainBoundaries[i] = (u16)((maxPwrT4[i] +
						      minPwrT4[i+1]) / 4);

		pPdGainBoundaries[i] = min((u16)AR5416_MAX_RATE_POWER,
					    pPdGainBoundaries[i]);


		if ((i == 0) && !AR_SREV_5416_20_OR_LATER(ah)) {
			minDelta = pPdGainBoundaries[0] - 23;
			pPdGainBoundaries[0] = 23;
		} else
			minDelta = 0;

		if (i == 0) {
			if (AR_SREV_9280_10_OR_LATER(ah))
				ss = (int16_t)(0 - (minPwrT4[i] / 2));
			else
				ss = 0;
		} else
			ss = (int16_t)((pPdGainBoundaries[i-1] -
				       (minPwrT4[i] / 2)) -
				       tPdGainOverlap + 1 + minDelta);

		vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
		vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
		while ((ss < 0) && (k < (AR9287_NUM_PDADC_VALUES - 1)))	{
			tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
			pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
			ss++;
		}

		sizeCurrVpdTable = (u8)((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
		tgtIndex = (u8)(pPdGainBoundaries[i] +
				tPdGainOverlap - (minPwrT4[i] / 2));
		maxIndex = (tgtIndex < sizeCurrVpdTable) ?
			    tgtIndex : sizeCurrVpdTable;

		while ((ss < maxIndex) && (k < (AR9287_NUM_PDADC_VALUES - 1)))
			pPDADCValues[k++] = vpdTableI[i][ss++];

		vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
				    vpdTableI[i][sizeCurrVpdTable - 2]);
		vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
		if (tgtIndex > maxIndex) {
			while ((ss <= tgtIndex) &&
				(k < (AR9287_NUM_PDADC_VALUES - 1))) {
				tmpVal = (int16_t) TMP_VAL_VPD_TABLE;
				pPDADCValues[k++] = (u8)((tmpVal > 255) ?
							  255 : tmpVal);
				ss++;
			}
		}
	}

	while (i < AR9287_PD_GAINS_IN_MASK) {
		pPdGainBoundaries[i] = pPdGainBoundaries[i-1];
		i++;
	}

	while (k < AR9287_NUM_PDADC_VALUES) {
		pPDADCValues[k] = pPDADCValues[k-1];
		k++;
	}

#undef TMP_VAL_VPD_TABLE
}

static void ar9287_eeprom_get_tx_gain_index(struct ath_hw *ah,
		struct ath9k_channel *chan,
		struct cal_data_op_loop_ar9287 *pRawDatasetOpLoop,
		u8 *pCalChans,  u16 availPiers,
		int8_t *pPwr)
{
	u8 pcdac, i = 0;
	u16  idxL = 0, idxR = 0, numPiers;
	bool match;
	struct chan_centers centers;
	ath9k_hw_get_channel_centers(ah, chan, &centers);
	for (numPiers = 0; numPiers < availPiers; numPiers++) {
		if (pCalChans[numPiers] == AR9287_BCHAN_UNUSED)
			break;
	}

	match = ath9k_hw_get_lower_upper_index(
			(u8)FREQ2FBIN(centers.synth_center, IS_CHAN_2GHZ(chan)),
			pCalChans, numPiers,
			&idxL, &idxR);

	if (match) {
		pcdac = pRawDatasetOpLoop[idxL].pcdac[0][0];
		*pPwr = pRawDatasetOpLoop[idxL].pwrPdg[0][0];
	} else {
		pcdac = pRawDatasetOpLoop[idxR].pcdac[0][0];
		*pPwr = (pRawDatasetOpLoop[idxL].pwrPdg[0][0] +
				pRawDatasetOpLoop[idxR].pwrPdg[0][0])/2;
	}

	while ((pcdac > ah->originalGain[i]) &&
			(i < (AR9280_TX_GAIN_TABLE_SIZE - 1)))
		i++;
}

static void ar9287_eeprom_olpc_set_pdadcs(struct ath_hw *ah,
					  int32_t txPower, u16 chain)
{
	u32 tmpVal;
	u32 a;

	tmpVal = REG_READ(ah, 0xa270);
	tmpVal = tmpVal & 0xFCFFFFFF;
	tmpVal = tmpVal | (0x3 << 24);
	REG_WRITE(ah, 0xa270, tmpVal);

	tmpVal = REG_READ(ah, 0xb270);
	tmpVal = tmpVal & 0xFCFFFFFF;
	tmpVal = tmpVal | (0x3 << 24);
	REG_WRITE(ah, 0xb270, tmpVal);

	if (chain == 0) {
		tmpVal = REG_READ(ah, 0xa398);
		tmpVal = tmpVal & 0xff00ffff;
		a = (txPower)&0xff;
		tmpVal = tmpVal | (a << 16);
		REG_WRITE(ah, 0xa398, tmpVal);
	}

	if (chain == 1) {
		tmpVal = REG_READ(ah, 0xb398);
		tmpVal = tmpVal & 0xff00ffff;
		a = (txPower)&0xff;
		tmpVal = tmpVal | (a << 16);
		REG_WRITE(ah, 0xb398, tmpVal);
	}
}


static void ath9k_hw_set_AR9287_power_cal_table(struct ath_hw *ah,
		struct ath9k_channel *chan, int16_t *pTxPowerIndexOffset)
{
	struct cal_data_per_freq_ar9287 *pRawDataset;
	struct cal_data_op_loop_ar9287 *pRawDatasetOpenLoop;
	u8  *pCalBChans = NULL;
	u16 pdGainOverlap_t2;
	u8  pdadcValues[AR9287_NUM_PDADC_VALUES];
	u16 gainBoundaries[AR9287_PD_GAINS_IN_MASK];
	u16 numPiers = 0, i, j;
	int16_t  tMinCalPower;
	u16 numXpdGain, xpdMask;
	u16 xpdGainValues[AR9287_NUM_PD_GAINS] = {0, 0, 0, 0};
	u32 reg32, regOffset, regChainOffset;
	int16_t   modalIdx, diff = 0;
3213
	struct ar9287_eeprom *pEepData = &ah->eeprom.map9287;
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
	modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
	xpdMask = pEepData->modalHeader.xpdGain;
	if ((pEepData->baseEepHeader.version & AR9287_EEP_VER_MINOR_MASK) >=
			AR9287_EEP_MINOR_VER_2)
		pdGainOverlap_t2 = pEepData->modalHeader.pdGainOverlap;
	else
		pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
					    AR_PHY_TPCRG5_PD_GAIN_OVERLAP));

	if (IS_CHAN_2GHZ(chan)) {
		pCalBChans = pEepData->calFreqPier2G;
		numPiers = AR9287_NUM_2G_CAL_PIERS;
		if (ath9k_hw_AR9287_get_eeprom(ah, EEP_OL_PWRCTRL)) {
			pRawDatasetOpenLoop =
				(struct cal_data_op_loop_ar9287 *)
				pEepData->calPierData2G[0];
			ah->initPDADC = pRawDatasetOpenLoop->vpdPdg[0][0];
		}
	}

	numXpdGain = 0;
	for (i = 1; i <= AR9287_PD_GAINS_IN_MASK; i++) {
		if ((xpdMask >> (AR9287_PD_GAINS_IN_MASK - i)) & 1) {
			if (numXpdGain >= AR9287_NUM_PD_GAINS)
				break;
			xpdGainValues[numXpdGain] =
				(u16)(AR9287_PD_GAINS_IN_MASK-i);
			numXpdGain++;
		}
	}

	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
		      (numXpdGain - 1) & 0x3);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
		      xpdGainValues[0]);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
		      xpdGainValues[1]);
	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
		      xpdGainValues[2]);

	for (i = 0; i < AR9287_MAX_CHAINS; i++)	{
		regChainOffset = i * 0x1000;
		if (pEepData->baseEepHeader.txMask & (1 << i)) {
			pRawDatasetOpenLoop = (struct cal_data_op_loop_ar9287 *)
					       pEepData->calPierData2G[i];
			if (ath9k_hw_AR9287_get_eeprom(ah, EEP_OL_PWRCTRL)) {
				int8_t txPower;
				ar9287_eeprom_get_tx_gain_index(ah, chan,
							  pRawDatasetOpenLoop,
							  pCalBChans, numPiers,
							  &txPower);
				ar9287_eeprom_olpc_set_pdadcs(ah, txPower, i);
			} else {
				pRawDataset =
					(struct cal_data_per_freq_ar9287 *)
					pEepData->calPierData2G[i];
				ath9k_hw_get_AR9287_gain_boundaries_pdadcs(
						  ah, chan, pRawDataset,
						  pCalBChans, numPiers,
						  pdGainOverlap_t2,
						  &tMinCalPower, gainBoundaries,
						  pdadcValues, numXpdGain);
			}

			if (i == 0) {
				if (!ath9k_hw_AR9287_get_eeprom(
							ah, EEP_OL_PWRCTRL)) {
					REG_WRITE(ah, AR_PHY_TPCRG5 +
					    regChainOffset,
					    SM(pdGainOverlap_t2,
					    AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
					    SM(gainBoundaries[0],
					     AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1)
					     | SM(gainBoundaries[1],
					     AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2)
					     | SM(gainBoundaries[2],
					     AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3)
					     | SM(gainBoundaries[3],
					     AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
				}
			}

			if ((int32_t)AR9287_PWR_TABLE_OFFSET_DB !=
				     pEepData->baseEepHeader.pwrTableOffset) {
				diff = (u16)
				       (pEepData->baseEepHeader.pwrTableOffset
					- (int32_t)AR9287_PWR_TABLE_OFFSET_DB);
				diff *= 2;

				for (j = 0;
				     j < ((u16)AR9287_NUM_PDADC_VALUES-diff);
				     j++)
					pdadcValues[j] = pdadcValues[j+diff];

				for (j = (u16)(AR9287_NUM_PDADC_VALUES-diff);
				     j < AR9287_NUM_PDADC_VALUES; j++)
					pdadcValues[j] =
					  pdadcValues[
					  AR9287_NUM_PDADC_VALUES-diff];
			}
			if (!ath9k_hw_AR9287_get_eeprom(ah, EEP_OL_PWRCTRL)) {
				regOffset = AR_PHY_BASE + (672 << 2) +
							   regChainOffset;
				for (j = 0; j < 32; j++) {
					reg32 = ((pdadcValues[4*j + 0]
						  & 0xFF) << 0)  |
						((pdadcValues[4*j + 1]
						  & 0xFF) << 8)  |
						((pdadcValues[4*j + 2]
						  & 0xFF) << 16) |
						((pdadcValues[4*j + 3]
						  & 0xFF) << 24) ;
					REG_WRITE(ah, regOffset, reg32);

					DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
						"PDADC (%d,%4x): %4.4x %8.8x\n",
						i, regChainOffset, regOffset,
						reg32);
					DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
						"PDADC: Chain %d | "
						"PDADC %3d Value %3d | "
						"PDADC %3d Value %3d | "
						"PDADC %3d Value %3d | "
						"PDADC %3d Value %3d |\n",
						i, 4 * j, pdadcValues[4 * j],
						4 * j + 1,
						pdadcValues[4 * j + 1],
						4 * j + 2,
						pdadcValues[4 * j + 2],
						4 * j + 3,
						pdadcValues[4 * j + 3]);

					regOffset += 4;
				}
			}
		}
	}

	*pTxPowerIndexOffset = 0;
}


static void ath9k_hw_set_AR9287_power_per_rate_table(struct ath_hw *ah,
		struct ath9k_channel *chan, int16_t *ratesArray, u16 cfgCtl,
		u16 AntennaReduction, u16 twiceMaxRegulatoryPower,
		u16 powerLimit)
{
#define REDUCE_SCALED_POWER_BY_TWO_CHAIN     6
#define REDUCE_SCALED_POWER_BY_THREE_CHAIN   10

	u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
	static const u16 tpScaleReductionTable[5] = { 0, 3, 6, 9,
						      AR5416_MAX_RATE_POWER };
	int i;
	int16_t  twiceLargestAntenna;
	struct cal_ctl_data_ar9287 *rep;
	struct cal_target_power_leg targetPowerOfdm = {0, {0, 0, 0, 0} },
				    targetPowerCck = {0, {0, 0, 0, 0} };
	struct cal_target_power_leg targetPowerOfdmExt = {0, {0, 0, 0, 0} },
				    targetPowerCckExt = {0, {0, 0, 0, 0} };
	struct cal_target_power_ht  targetPowerHt20,
				    targetPowerHt40 = {0, {0, 0, 0, 0} };
	u16 scaledPower = 0, minCtlPower, maxRegAllowedPower;
	u16 ctlModesFor11g[] = {CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT,
				CTL_11G_EXT, CTL_2GHT40};
	u16 numCtlModes = 0, *pCtlMode = NULL, ctlMode, freq;
	struct chan_centers centers;
	int tx_chainmask;
	u16 twiceMinEdgePower;
3383
	struct ar9287_eeprom *pEepData = &ah->eeprom.map9287;
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
	tx_chainmask = ah->txchainmask;

	ath9k_hw_get_channel_centers(ah, chan, &centers);

	twiceLargestAntenna = max(pEepData->modalHeader.antennaGainCh[0],
			pEepData->modalHeader.antennaGainCh[1]);

	twiceLargestAntenna =  (int16_t)min((AntennaReduction) -
					    twiceLargestAntenna, 0);

	maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;
	if (ah->regulatory.tp_scale != ATH9K_TP_SCALE_MAX)
		maxRegAllowedPower -=
			(tpScaleReductionTable[(ah->regulatory.tp_scale)] * 2);

	scaledPower = min(powerLimit, maxRegAllowedPower);

	switch (ar5416_get_ntxchains(tx_chainmask)) {
	case 1:
		break;
	case 2:
		scaledPower -= REDUCE_SCALED_POWER_BY_TWO_CHAIN;
		break;
	case 3:
		scaledPower -= REDUCE_SCALED_POWER_BY_THREE_CHAIN;
		break;
	}
	scaledPower = max((u16)0, scaledPower);

	if (IS_CHAN_2GHZ(chan))	{
		numCtlModes = ARRAY_SIZE(ctlModesFor11g) -
					 SUB_NUM_CTL_MODES_AT_2G_40;
		pCtlMode = ctlModesFor11g;

		ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPowerCck,
				AR9287_NUM_2G_CCK_TARGET_POWERS,
				&targetPowerCck, 4, false);
		ath9k_hw_get_legacy_target_powers(ah, chan,
				pEepData->calTargetPower2G,
				AR9287_NUM_2G_20_TARGET_POWERS,
				&targetPowerOfdm, 4, false);
		ath9k_hw_get_target_powers(ah, chan,
				pEepData->calTargetPower2GHT20,
				AR9287_NUM_2G_20_TARGET_POWERS,
				&targetPowerHt20, 8, false);

		if (IS_CHAN_HT40(chan))	{
			numCtlModes = ARRAY_SIZE(ctlModesFor11g);
			ath9k_hw_get_target_powers(ah, chan,
					pEepData->calTargetPower2GHT40,
					AR9287_NUM_2G_40_TARGET_POWERS,
					&targetPowerHt40, 8, true);
			ath9k_hw_get_legacy_target_powers(ah, chan,
					pEepData->calTargetPowerCck,
					AR9287_NUM_2G_CCK_TARGET_POWERS,
					&targetPowerCckExt, 4, true);
			ath9k_hw_get_legacy_target_powers(ah, chan,
					pEepData->calTargetPower2G,
					AR9287_NUM_2G_20_TARGET_POWERS,
					&targetPowerOfdmExt, 4, true);
		}
	}

	for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {

		bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
				     (pCtlMode[ctlMode] == CTL_2GHT40);
		if (isHt40CtlMode)
			freq = centers.synth_center;
		else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
			freq = centers.ext_center;
		else
			freq = centers.ctl_center;


		if (ah->eep_ops->get_eeprom_ver(ah) == 14 &&
				ah->eep_ops->get_eeprom_rev(ah) <= 2)
			twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
			"LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d,"
			 "EXT_ADDITIVE %d\n", ctlMode, numCtlModes,
			 isHt40CtlMode, (pCtlMode[ctlMode] & EXT_ADDITIVE));
		for (i = 0; (i < AR9287_NUM_CTLS)
			     && pEepData->ctlIndex[i]; i++) {
			DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
				"LOOP-Ctlidx %d: cfgCtl 0x%2.2x"
				 "pCtlMode 0x%2.2x ctlIndex 0x%2.2x"
				 "chan %d chanctl=xxxx\n",
				 i, cfgCtl, pCtlMode[ctlMode],
				 pEepData->ctlIndex[i],	chan->channel);

			if ((((cfgCtl & ~CTL_MODE_M) |
			    (pCtlMode[ctlMode] & CTL_MODE_M)) ==
			    pEepData->ctlIndex[i]) ||
			    (((cfgCtl & ~CTL_MODE_M) |
			    (pCtlMode[ctlMode] & CTL_MODE_M)) ==
			    ((pEepData->ctlIndex[i] &
			    CTL_MODE_M) | SD_NO_CTL))) {

				rep = &(pEepData->ctlData[i]);
				twiceMinEdgePower = ath9k_hw_get_max_edge_power(
				    freq,
				    rep->ctlEdges[ar5416_get_ntxchains(
				    tx_chainmask) - 1],
				    IS_CHAN_2GHZ(chan), AR5416_NUM_BAND_EDGES);

				DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
					"MATCH-EE_IDX %d: ch %d is2 %d"
					"2xMinEdge %d chainmask %d chains %d\n",
					 i, freq, IS_CHAN_2GHZ(chan),
					 twiceMinEdgePower, tx_chainmask,
					 ar5416_get_ntxchains(tx_chainmask));

				if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL)
					twiceMaxEdgePower = min(
							    twiceMaxEdgePower,
							    twiceMinEdgePower);
				else {
					twiceMaxEdgePower = twiceMinEdgePower;
					break;
				}
			}
		}

		minCtlPower = (u8)min(twiceMaxEdgePower, scaledPower);

		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
				"SEL-Min ctlMode %d pCtlMode %d 2xMaxEdge %d"
				 "sP %d minCtlPwr %d\n",
				 ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
				 scaledPower, minCtlPower);


		switch (pCtlMode[ctlMode]) {

		case CTL_11B:
			for (i = 0;
			     i < ARRAY_SIZE(targetPowerCck.tPow2x);
			     i++) {
				targetPowerCck.tPow2x[i] = (u8)min(
					(u16)targetPowerCck.tPow2x[i],
					minCtlPower);
			}
			break;
		case CTL_11A:
		case CTL_11G:
			for (i = 0;
			     i < ARRAY_SIZE(targetPowerOfdm.tPow2x);
			     i++) {
				targetPowerOfdm.tPow2x[i] = (u8)min(
					(u16)targetPowerOfdm.tPow2x[i],
					minCtlPower);
			}
			break;
		case CTL_5GHT20:
		case CTL_2GHT20:
			for (i = 0;
			     i < ARRAY_SIZE(targetPowerHt20.tPow2x);
			     i++) {
				targetPowerHt20.tPow2x[i] = (u8)min(
					(u16)targetPowerHt20.tPow2x[i],
					minCtlPower);
			}
			break;
		case CTL_11B_EXT:
			targetPowerCckExt.tPow2x[0] = (u8)min(
				    (u16)targetPowerCckExt.tPow2x[0],
				    minCtlPower);
			break;
		case CTL_11A_EXT:
		case CTL_11G_EXT:
			targetPowerOfdmExt.tPow2x[0] = (u8)min(
				    (u16)targetPowerOfdmExt.tPow2x[0],
				    minCtlPower);
			break;
		case CTL_5GHT40:
		case CTL_2GHT40:
			for (i = 0;
			     i < ARRAY_SIZE(targetPowerHt40.tPow2x);
			     i++) {
				targetPowerHt40.tPow2x[i] = (u8)min(
					(u16)targetPowerHt40.tPow2x[i],
					minCtlPower);
			}
			break;
		default:
			break;
		}
	}

	ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
		ratesArray[rate18mb] = ratesArray[rate24mb] =
		targetPowerOfdm.tPow2x[0];
	ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
	ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
	ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
	ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];

	for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
		ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];

	if (IS_CHAN_2GHZ(chan))	{
		ratesArray[rate1l]  = targetPowerCck.tPow2x[0];
		ratesArray[rate2s] = ratesArray[rate2l]  =
			targetPowerCck.tPow2x[1];
		ratesArray[rate5_5s] = ratesArray[rate5_5l] =
			targetPowerCck.tPow2x[2];
		ratesArray[rate11s] = ratesArray[rate11l] =
			targetPowerCck.tPow2x[3];
	}
	if (IS_CHAN_HT40(chan))	{
		for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++)
			ratesArray[rateHt40_0 + i] = targetPowerHt40.tPow2x[i];

		ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
		ratesArray[rateDupCck]  = targetPowerHt40.tPow2x[0];
		ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
		if (IS_CHAN_2GHZ(chan))
			ratesArray[rateExtCck]  = targetPowerCckExt.tPow2x[0];
	}
#undef REDUCE_SCALED_POWER_BY_TWO_CHAIN
#undef REDUCE_SCALED_POWER_BY_THREE_CHAIN
}

static void ath9k_hw_AR9287_set_txpower(struct ath_hw *ah,
		struct ath9k_channel *chan, u16 cfgCtl,
		u8 twiceAntennaReduction, u8 twiceMaxRegulatoryPower,
		u8 powerLimit)
{
#define INCREASE_MAXPOW_BY_TWO_CHAIN     6
#define INCREASE_MAXPOW_BY_THREE_CHAIN   10
3616
	struct ar9287_eeprom *pEepData = &ah->eeprom.map9287;
3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778
	struct modal_eep_ar9287_header *pModal = &pEepData->modalHeader;
	int16_t ratesArray[Ar5416RateSize];
	int16_t  txPowerIndexOffset = 0;
	u8 ht40PowerIncForPdadc = 2;
	int i;
	memset(ratesArray, 0, sizeof(ratesArray));

	if ((pEepData->baseEepHeader.version & AR9287_EEP_VER_MINOR_MASK) >=
			AR9287_EEP_MINOR_VER_2)
		ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;

	ath9k_hw_set_AR9287_power_per_rate_table(ah, chan,
			&ratesArray[0], cfgCtl,
			twiceAntennaReduction,
			twiceMaxRegulatoryPower,
			powerLimit);


	ath9k_hw_set_AR9287_power_cal_table(ah, chan, &txPowerIndexOffset);

	for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
		ratesArray[i] = (int16_t)(txPowerIndexOffset + ratesArray[i]);
		if (ratesArray[i] > AR9287_MAX_RATE_POWER)
			ratesArray[i] = AR9287_MAX_RATE_POWER;
	}

	if (AR_SREV_9280_10_OR_LATER(ah)) {
		for (i = 0; i < Ar5416RateSize; i++)
			ratesArray[i] -= AR9287_PWR_TABLE_OFFSET_DB * 2;
	}


	REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
			ATH9K_POW_SM(ratesArray[rate18mb], 24)
			| ATH9K_POW_SM(ratesArray[rate12mb], 16)
			| ATH9K_POW_SM(ratesArray[rate9mb],  8)
			| ATH9K_POW_SM(ratesArray[rate6mb],  0)
		 );

	REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
			ATH9K_POW_SM(ratesArray[rate54mb], 24)
			| ATH9K_POW_SM(ratesArray[rate48mb], 16)
			| ATH9K_POW_SM(ratesArray[rate36mb],  8)
			| ATH9K_POW_SM(ratesArray[rate24mb],  0)
		 );

	if (IS_CHAN_2GHZ(chan))	{
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
				ATH9K_POW_SM(ratesArray[rate2s], 24)
				| ATH9K_POW_SM(ratesArray[rate2l],  16)
				| ATH9K_POW_SM(ratesArray[rateXr],  8)
				| ATH9K_POW_SM(ratesArray[rate1l],   0)
			 );
		REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
				ATH9K_POW_SM(ratesArray[rate11s], 24)
				| ATH9K_POW_SM(ratesArray[rate11l], 16)
				| ATH9K_POW_SM(ratesArray[rate5_5s],  8)
				| ATH9K_POW_SM(ratesArray[rate5_5l],  0)
			 );
	}

	REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
			ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
			| ATH9K_POW_SM(ratesArray[rateHt20_2],  16)
			| ATH9K_POW_SM(ratesArray[rateHt20_1],  8)
			| ATH9K_POW_SM(ratesArray[rateHt20_0],   0)
		 );

	REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
			ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
			| ATH9K_POW_SM(ratesArray[rateHt20_6],  16)
			| ATH9K_POW_SM(ratesArray[rateHt20_5],  8)
			| ATH9K_POW_SM(ratesArray[rateHt20_4],   0)
		 );

	if (IS_CHAN_HT40(chan))	{
		if (ath9k_hw_AR9287_get_eeprom(ah, EEP_OL_PWRCTRL)) {
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
				  ATH9K_POW_SM(ratesArray[rateHt40_3], 24)
				  | ATH9K_POW_SM(ratesArray[rateHt40_2],  16)
				  | ATH9K_POW_SM(ratesArray[rateHt40_1],  8)
				  | ATH9K_POW_SM(ratesArray[rateHt40_0],   0)
				 );

			REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
				  ATH9K_POW_SM(ratesArray[rateHt40_7], 24)
				  | ATH9K_POW_SM(ratesArray[rateHt40_6],  16)
				  | ATH9K_POW_SM(ratesArray[rateHt40_5],  8)
				  | ATH9K_POW_SM(ratesArray[rateHt40_4],   0)
				 );
		} else {
			REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
				  ATH9K_POW_SM(ratesArray[rateHt40_3] +
					       ht40PowerIncForPdadc, 24)
				  | ATH9K_POW_SM(ratesArray[rateHt40_2] +
					       ht40PowerIncForPdadc,  16)
				  | ATH9K_POW_SM(ratesArray[rateHt40_1] +
					       ht40PowerIncForPdadc,  8)
				  | ATH9K_POW_SM(ratesArray[rateHt40_0] +
					       ht40PowerIncForPdadc,   0)
				 );

			REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
				  ATH9K_POW_SM(ratesArray[rateHt40_7] +
					       ht40PowerIncForPdadc, 24)
				  | ATH9K_POW_SM(ratesArray[rateHt40_6] +
					       ht40PowerIncForPdadc,  16)
				  | ATH9K_POW_SM(ratesArray[rateHt40_5] +
					       ht40PowerIncForPdadc,  8)
				  | ATH9K_POW_SM(ratesArray[rateHt40_4] +
					       ht40PowerIncForPdadc,   0)
				 );

		}

		REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
				| ATH9K_POW_SM(ratesArray[rateExtCck],  16)
				| ATH9K_POW_SM(ratesArray[rateDupOfdm],  8)
				| ATH9K_POW_SM(ratesArray[rateDupCck],   0)
			 );
	}


	if (IS_CHAN_2GHZ(chan))
		i = rate1l;
	else
		i = rate6mb;

	if (AR_SREV_9280_10_OR_LATER(ah))
		ah->regulatory.max_power_level =
			ratesArray[i] + AR9287_PWR_TABLE_OFFSET_DB * 2;
	else
		ah->regulatory.max_power_level = ratesArray[i];

	switch (ar5416_get_ntxchains(ah->txchainmask)) {
	case 1:
		break;
	case 2:
		ah->regulatory.max_power_level +=
			INCREASE_MAXPOW_BY_TWO_CHAIN;
		break;
	case 3:
		ah->regulatory.max_power_level +=
			INCREASE_MAXPOW_BY_THREE_CHAIN;
		break;
	default:
		DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
				"Invalid chainmask configuration\n");
		break;
	}
}

static void ath9k_hw_AR9287_set_addac(struct ath_hw *ah,
				      struct ath9k_channel *chan)
{
	return;
}

static void ath9k_hw_AR9287_set_board_values(struct ath_hw *ah,
					     struct ath9k_channel *chan)
{
3779
	struct ar9287_eeprom *eep = &ah->eeprom.map9287;
3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
	struct modal_eep_ar9287_header *pModal = &eep->modalHeader;

	u16 antWrites[AR9287_ANT_16S];
	u32 regChainOffset;
	u8 txRxAttenLocal;
	int i, j, offset_num;

	pModal = &eep->modalHeader;

	antWrites[0] = (u16)((pModal->antCtrlCommon >> 28) & 0xF);
	antWrites[1] = (u16)((pModal->antCtrlCommon >> 24) & 0xF);
	antWrites[2] = (u16)((pModal->antCtrlCommon >> 20) & 0xF);
	antWrites[3] = (u16)((pModal->antCtrlCommon >> 16) & 0xF);
	antWrites[4] = (u16)((pModal->antCtrlCommon >> 12) & 0xF);
	antWrites[5] = (u16)((pModal->antCtrlCommon >> 8) & 0xF);
	antWrites[6] = (u16)((pModal->antCtrlCommon >> 4)  & 0xF);
	antWrites[7] = (u16)(pModal->antCtrlCommon & 0xF);

	offset_num = 8;

	for (i = 0, j = offset_num; i < AR9287_MAX_CHAINS; i++) {
		antWrites[j++] = (u16)((pModal->antCtrlChain[i] >> 28) & 0xf);
		antWrites[j++] = (u16)((pModal->antCtrlChain[i] >> 10) & 0x3);
		antWrites[j++] = (u16)((pModal->antCtrlChain[i] >> 8) & 0x3);
		antWrites[j++] = 0;
		antWrites[j++] = (u16)((pModal->antCtrlChain[i] >> 6) & 0x3);
		antWrites[j++] = (u16)((pModal->antCtrlChain[i] >> 4) & 0x3);
		antWrites[j++] = (u16)((pModal->antCtrlChain[i] >> 2) & 0x3);
		antWrites[j++] = (u16)(pModal->antCtrlChain[i] & 0x3);
	}


	REG_WRITE(ah, AR_PHY_SWITCH_COM,
		  ah->eep_ops->get_eeprom_antenna_cfg(ah, chan));

	for (i = 0; i < AR9287_MAX_CHAINS; i++)	{
		regChainOffset = i * 0x1000;

		REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
			  pModal->antCtrlChain[i]);

		REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
			  (REG_READ(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset)
			   & ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
			   AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
			   SM(pModal->iqCalICh[i],
			      AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
			   SM(pModal->iqCalQCh[i],
			      AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));

		txRxAttenLocal = pModal->txRxAttenCh[i];

		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
			      pModal->bswMargin[i]);
		REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
			      AR_PHY_GAIN_2GHZ_XATTEN1_DB,
			      pModal->bswAtten[i]);
		REG_RMW_FIELD(ah, AR_PHY_RXGAIN + regChainOffset,
			      AR9280_PHY_RXGAIN_TXRX_ATTEN,
			      txRxAttenLocal);
		REG_RMW_FIELD(ah, AR_PHY_RXGAIN + regChainOffset,
			      AR9280_PHY_RXGAIN_TXRX_MARGIN,
			      pModal->rxTxMarginCh[i]);
	}


	if (IS_CHAN_HT40(chan))
		REG_RMW_FIELD(ah, AR_PHY_SETTLING,
			      AR_PHY_SETTLING_SWITCH, pModal->swSettleHt40);
	else
		REG_RMW_FIELD(ah, AR_PHY_SETTLING,
			      AR_PHY_SETTLING_SWITCH, pModal->switchSettling);

	REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
		      AR_PHY_DESIRED_SZ_ADC, pModal->adcDesiredSize);

	REG_WRITE(ah, AR_PHY_RF_CTL4,
		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF)
		  | SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAB_OFF)
		  | SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAA_ON)
		  | SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAB_ON));

	REG_RMW_FIELD(ah, AR_PHY_RF_CTL3,
		      AR_PHY_TX_END_TO_A2_RX_ON, pModal->txEndToRxOn);

	REG_RMW_FIELD(ah, AR_PHY_CCA,
		      AR9280_PHY_CCA_THRESH62, pModal->thresh62);
	REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0,
		      AR_PHY_EXT_CCA0_THRESH62, pModal->thresh62);

	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH0, AR9287_AN_RF2G3_DB1,
				  AR9287_AN_RF2G3_DB1_S, pModal->db1);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH0, AR9287_AN_RF2G3_DB2,
				  AR9287_AN_RF2G3_DB2_S, pModal->db2);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH0,
				  AR9287_AN_RF2G3_OB_CCK,
				  AR9287_AN_RF2G3_OB_CCK_S, pModal->ob_cck);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH0,
				  AR9287_AN_RF2G3_OB_PSK,
				  AR9287_AN_RF2G3_OB_PSK_S, pModal->ob_psk);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH0,
				  AR9287_AN_RF2G3_OB_QAM,
				  AR9287_AN_RF2G3_OB_QAM_S, pModal->ob_qam);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH0,
				  AR9287_AN_RF2G3_OB_PAL_OFF,
				  AR9287_AN_RF2G3_OB_PAL_OFF_S,
				  pModal->ob_pal_off);

	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH1,
				  AR9287_AN_RF2G3_DB1, AR9287_AN_RF2G3_DB1_S,
				  pModal->db1);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH1, AR9287_AN_RF2G3_DB2,
				  AR9287_AN_RF2G3_DB2_S, pModal->db2);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH1,
				  AR9287_AN_RF2G3_OB_CCK,
				  AR9287_AN_RF2G3_OB_CCK_S, pModal->ob_cck);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH1,
				  AR9287_AN_RF2G3_OB_PSK,
				  AR9287_AN_RF2G3_OB_PSK_S, pModal->ob_psk);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH1,
				  AR9287_AN_RF2G3_OB_QAM,
				  AR9287_AN_RF2G3_OB_QAM_S, pModal->ob_qam);
	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_RF2G3_CH1,
				  AR9287_AN_RF2G3_OB_PAL_OFF,
				  AR9287_AN_RF2G3_OB_PAL_OFF_S,
				  pModal->ob_pal_off);

	REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
		      AR_PHY_TX_END_DATA_START, pModal->txFrameToDataStart);
	REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
		      AR_PHY_TX_END_PA_ON, pModal->txFrameToPaOn);

	ath9k_hw_analog_shift_rmw(ah, AR9287_AN_TOP2,
				  AR9287_AN_TOP2_XPABIAS_LVL,
				  AR9287_AN_TOP2_XPABIAS_LVL_S,
				  pModal->xpaBiasLvl);
}

static u8 ath9k_hw_AR9287_get_num_ant_config(struct ath_hw *ah,
		enum ieee80211_band freq_band)
{
	return 1;
}




static u16 ath9k_hw_AR9287_get_eeprom_antenna_cfg(struct ath_hw *ah,
		struct ath9k_channel *chan)
{
3931
	struct ar9287_eeprom *eep = &ah->eeprom.map9287;
3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980
	struct modal_eep_ar9287_header *pModal = &eep->modalHeader;
	return pModal->antCtrlCommon & 0xFFFF;
}


static u16 ath9k_hw_AR9287_get_spur_channel(struct ath_hw *ah,
					    u16 i, bool is2GHz)
{
#define EEP_MAP9287_SPURCHAN \
	(ah->eeprom.map9287.modalHeader.spurChans[i].spurChan)
	u16 spur_val = AR_NO_SPUR;

	DPRINTF(ah->ah_sc, ATH_DBG_ANI,
			"Getting spur idx %d is2Ghz. %d val %x\n",
			i, is2GHz, ah->config.spurchans[i][is2GHz]);

	switch (ah->config.spurmode) {
	case SPUR_DISABLE:
		break;
	case SPUR_ENABLE_IOCTL:
		spur_val = ah->config.spurchans[i][is2GHz];
		DPRINTF(ah->ah_sc, ATH_DBG_ANI,
		       "Getting spur val from new loc. %d\n", spur_val);
		break;
	case SPUR_ENABLE_EEPROM:
		spur_val = EEP_MAP9287_SPURCHAN;
		break;
	}

	return spur_val;

#undef EEP_MAP9287_SPURCHAN
}

static struct eeprom_ops eep_AR9287_ops = {
	.check_eeprom		= ath9k_hw_AR9287_check_eeprom,
	.get_eeprom		= ath9k_hw_AR9287_get_eeprom,
	.fill_eeprom		= ath9k_hw_AR9287_fill_eeprom,
	.get_eeprom_ver		= ath9k_hw_AR9287_get_eeprom_ver,
	.get_eeprom_rev		= ath9k_hw_AR9287_get_eeprom_rev,
	.get_num_ant_config	= ath9k_hw_AR9287_get_num_ant_config,
	.get_eeprom_antenna_cfg	= ath9k_hw_AR9287_get_eeprom_antenna_cfg,
	.set_board_values	= ath9k_hw_AR9287_set_board_values,
	.set_addac		= ath9k_hw_AR9287_set_addac,
	.set_txpower		= ath9k_hw_AR9287_set_txpower,
	.get_spur_channel	= ath9k_hw_AR9287_get_spur_channel
};


3981
int ath9k_hw_eeprom_init(struct ath_hw *ah)
S
Sujith 已提交
3982 3983
{
	int status;
3984 3985 3986 3987
	if (AR_SREV_9287(ah)) {
		ah->eep_map = EEP_MAP_AR9287;
		ah->eep_ops = &eep_AR9287_ops;
	} else if (AR_SREV_9285(ah)) {
3988
		ah->eep_map = EEP_MAP_4KBITS;
S
Sujith 已提交
3989 3990
		ah->eep_ops = &eep_4k_ops;
	} else {
3991
		ah->eep_map = EEP_MAP_DEFAULT;
S
Sujith 已提交
3992 3993
		ah->eep_ops = &eep_def_ops;
	}
3994

S
Sujith 已提交
3995
	if (!ah->eep_ops->fill_eeprom(ah))
S
Sujith 已提交
3996 3997
		return -EIO;

S
Sujith 已提交
3998
	status = ah->eep_ops->check_eeprom(ah);
S
Sujith 已提交
3999 4000 4001

	return status;
}