Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * Copyright (c) 2008-2011 Atheros Communications Inc.
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17#include <asm/unaligned.h>
  18#include "hw.h"
  19#include "ar9002_phy.h"
  20
  21static void ath9k_get_txgain_index(struct ath_hw *ah,
  22		struct ath9k_channel *chan,
  23		struct calDataPerFreqOpLoop *rawDatasetOpLoop,
  24		u8 *calChans,  u16 availPiers, u8 *pwr, u8 *pcdacIdx)
  25{
  26	u8 pcdac, i = 0;
  27	u16 idxL = 0, idxR = 0, numPiers;
  28	bool match;
  29	struct chan_centers centers;
  30
  31	ath9k_hw_get_channel_centers(ah, chan, &centers);
  32
  33	for (numPiers = 0; numPiers < availPiers; numPiers++)
  34		if (calChans[numPiers] == AR5416_BCHAN_UNUSED)
  35			break;
  36
  37	match = ath9k_hw_get_lower_upper_index(
  38			(u8)FREQ2FBIN(centers.synth_center, IS_CHAN_2GHZ(chan)),
  39			calChans, numPiers, &idxL, &idxR);
  40	if (match) {
  41		pcdac = rawDatasetOpLoop[idxL].pcdac[0][0];
  42		*pwr = rawDatasetOpLoop[idxL].pwrPdg[0][0];
  43	} else {
  44		pcdac = rawDatasetOpLoop[idxR].pcdac[0][0];
  45		*pwr = (rawDatasetOpLoop[idxL].pwrPdg[0][0] +
  46				rawDatasetOpLoop[idxR].pwrPdg[0][0])/2;
  47	}
  48
  49	while (pcdac > ah->originalGain[i] &&
  50			i < (AR9280_TX_GAIN_TABLE_SIZE - 1))
  51		i++;
  52
  53	*pcdacIdx = i;
  54}
  55
  56static void ath9k_olc_get_pdadcs(struct ath_hw *ah,
  57				u32 initTxGain,
  58				int txPower,
  59				u8 *pPDADCValues)
  60{
  61	u32 i;
  62	u32 offset;
  63
  64	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL6_0,
  65			AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
  66	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL6_1,
  67			AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
  68
  69	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL7,
  70			AR_PHY_TX_PWRCTRL_INIT_TX_GAIN, initTxGain);
  71
  72	offset = txPower;
  73	for (i = 0; i < AR5416_NUM_PDADC_VALUES; i++)
  74		if (i < offset)
  75			pPDADCValues[i] = 0x0;
  76		else
  77			pPDADCValues[i] = 0xFF;
  78}
  79
  80static int ath9k_hw_def_get_eeprom_ver(struct ath_hw *ah)
  81{
  82	return ((ah->eeprom.def.baseEepHeader.version >> 12) & 0xF);
  83}
  84
  85static int ath9k_hw_def_get_eeprom_rev(struct ath_hw *ah)
  86{
  87	return ((ah->eeprom.def.baseEepHeader.version) & 0xFFF);
  88}
  89
  90#define SIZE_EEPROM_DEF (sizeof(struct ar5416_eeprom_def) / sizeof(u16))
  91
  92static bool __ath9k_hw_def_fill_eeprom(struct ath_hw *ah)
  93{
  94	struct ath_common *common = ath9k_hw_common(ah);
  95	u16 *eep_data = (u16 *)&ah->eeprom.def;
  96	int addr, ar5416_eep_start_loc = 0x100;
  97
  98	for (addr = 0; addr < SIZE_EEPROM_DEF; addr++) {
  99		if (!ath9k_hw_nvram_read(common, addr + ar5416_eep_start_loc,
 100					 eep_data)) {
 101			ath_err(ath9k_hw_common(ah),
 102				"Unable to read eeprom region\n");
 103			return false;
 104		}
 105		eep_data++;
 106	}
 107	return true;
 108}
 109
 110static bool __ath9k_hw_usb_def_fill_eeprom(struct ath_hw *ah)
 111{
 112	u16 *eep_data = (u16 *)&ah->eeprom.def;
 113
 114	ath9k_hw_usb_gen_fill_eeprom(ah, eep_data,
 115				     0x100, SIZE_EEPROM_DEF);
 116	return true;
 117}
 118
 119static bool ath9k_hw_def_fill_eeprom(struct ath_hw *ah)
 120{
 121	struct ath_common *common = ath9k_hw_common(ah);
 122
 123	if (!ath9k_hw_use_flash(ah)) {
 124		ath_dbg(common, ATH_DBG_EEPROM,
 125			"Reading from EEPROM, not flash\n");
 126	}
 127
 128	if (common->bus_ops->ath_bus_type == ATH_USB)
 129		return __ath9k_hw_usb_def_fill_eeprom(ah);
 130	else
 131		return __ath9k_hw_def_fill_eeprom(ah);
 132}
 133
 134#undef SIZE_EEPROM_DEF
 135
 136static int ath9k_hw_def_check_eeprom(struct ath_hw *ah)
 137{
 138	struct ar5416_eeprom_def *eep =
 139		(struct ar5416_eeprom_def *) &ah->eeprom.def;
 140	struct ath_common *common = ath9k_hw_common(ah);
 141	u16 *eepdata, temp, magic, magic2;
 142	u32 sum = 0, el;
 143	bool need_swap = false;
 144	int i, addr, size;
 145
 146	if (!ath9k_hw_nvram_read(common, AR5416_EEPROM_MAGIC_OFFSET, &magic)) {
 147		ath_err(common, "Reading Magic # failed\n");
 148		return false;
 149	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 150
 151	if (!ath9k_hw_use_flash(ah)) {
 152		ath_dbg(common, ATH_DBG_EEPROM,
 153			"Read Magic = 0x%04X\n", magic);
 154
 155		if (magic != AR5416_EEPROM_MAGIC) {
 156			magic2 = swab16(magic);
 
 
 
 157
 158			if (magic2 == AR5416_EEPROM_MAGIC) {
 159				size = sizeof(struct ar5416_eeprom_def);
 160				need_swap = true;
 161				eepdata = (u16 *) (&ah->eeprom);
 162
 163				for (addr = 0; addr < size / sizeof(u16); addr++) {
 164					temp = swab16(*eepdata);
 165					*eepdata = temp;
 166					eepdata++;
 167				}
 168			} else {
 169				ath_err(common,
 170					"Invalid EEPROM Magic. Endianness mismatch.\n");
 171				return -EINVAL;
 172			}
 173		}
 174	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 175
 176	ath_dbg(common, ATH_DBG_EEPROM, "need_swap = %s.\n",
 177		need_swap ? "True" : "False");
 
 
 
 
 
 
 
 178
 179	if (need_swap)
 180		el = swab16(ah->eeprom.def.baseEepHeader.length);
 181	else
 182		el = ah->eeprom.def.baseEepHeader.length;
 
 
 
 
 
 
 
 183
 184	if (el > sizeof(struct ar5416_eeprom_def))
 185		el = sizeof(struct ar5416_eeprom_def) / sizeof(u16);
 186	else
 187		el = el / sizeof(u16);
 188
 189	eepdata = (u16 *)(&ah->eeprom);
 190
 191	for (i = 0; i < el; i++)
 192		sum ^= *eepdata++;
 
 193
 194	if (need_swap) {
 195		u32 integer, j;
 196		u16 word;
 197
 198		ath_dbg(common, ATH_DBG_EEPROM,
 199			"EEPROM Endianness is not native.. Changing.\n");
 200
 201		word = swab16(eep->baseEepHeader.length);
 202		eep->baseEepHeader.length = word;
 203
 204		word = swab16(eep->baseEepHeader.checksum);
 205		eep->baseEepHeader.checksum = word;
 206
 207		word = swab16(eep->baseEepHeader.version);
 208		eep->baseEepHeader.version = word;
 209
 210		word = swab16(eep->baseEepHeader.regDmn[0]);
 211		eep->baseEepHeader.regDmn[0] = word;
 212
 213		word = swab16(eep->baseEepHeader.regDmn[1]);
 214		eep->baseEepHeader.regDmn[1] = word;
 215
 216		word = swab16(eep->baseEepHeader.rfSilent);
 217		eep->baseEepHeader.rfSilent = word;
 218
 219		word = swab16(eep->baseEepHeader.blueToothOptions);
 220		eep->baseEepHeader.blueToothOptions = word;
 221
 222		word = swab16(eep->baseEepHeader.deviceCap);
 223		eep->baseEepHeader.deviceCap = word;
 224
 225		for (j = 0; j < ARRAY_SIZE(eep->modalHeader); j++) {
 226			struct modal_eep_header *pModal =
 227				&eep->modalHeader[j];
 228			integer = swab32(pModal->antCtrlCommon);
 229			pModal->antCtrlCommon = integer;
 230
 231			for (i = 0; i < AR5416_MAX_CHAINS; i++) {
 232				integer = swab32(pModal->antCtrlChain[i]);
 233				pModal->antCtrlChain[i] = integer;
 234			}
 235			for (i = 0; i < 3; i++) {
 236				word = swab16(pModal->xpaBiasLvlFreq[i]);
 237				pModal->xpaBiasLvlFreq[i] = word;
 238			}
 239
 240			for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
 241				word = swab16(pModal->spurChans[i].spurChan);
 242				pModal->spurChans[i].spurChan = word;
 243			}
 244		}
 245	}
 246
 247	if (sum != 0xffff || ah->eep_ops->get_eeprom_ver(ah) != AR5416_EEP_VER ||
 248	    ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_NO_BACK_VER) {
 249		ath_err(common, "Bad EEPROM checksum 0x%x or revision 0x%04x\n",
 250			sum, ah->eep_ops->get_eeprom_ver(ah));
 251		return -EINVAL;
 252	}
 253
 254	/* Enable fixup for AR_AN_TOP2 if necessary */
 255	if ((ah->hw_version.devid == AR9280_DEVID_PCI) &&
 256	    ((eep->baseEepHeader.version & 0xff) > 0x0a) &&
 257	    (eep->baseEepHeader.pwdclkind == 0))
 258		ah->need_an_top2_fixup = 1;
 259
 260	if ((common->bus_ops->ath_bus_type == ATH_USB) &&
 261	    (AR_SREV_9280(ah)))
 262		eep->modalHeader[0].xpaBiasLvl = 0;
 263
 264	return 0;
 265}
 266
 
 
 267static u32 ath9k_hw_def_get_eeprom(struct ath_hw *ah,
 268				   enum eeprom_param param)
 269{
 270	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 271	struct modal_eep_header *pModal = eep->modalHeader;
 272	struct base_eep_header *pBase = &eep->baseEepHeader;
 
 273
 274	switch (param) {
 275	case EEP_NFTHRESH_5:
 276		return pModal[0].noiseFloorThreshCh[0];
 277	case EEP_NFTHRESH_2:
 278		return pModal[1].noiseFloorThreshCh[0];
 279	case EEP_MAC_LSW:
 280		return get_unaligned_be16(pBase->macAddr);
 281	case EEP_MAC_MID:
 282		return get_unaligned_be16(pBase->macAddr + 2);
 283	case EEP_MAC_MSW:
 284		return get_unaligned_be16(pBase->macAddr + 4);
 285	case EEP_REG_0:
 286		return pBase->regDmn[0];
 287	case EEP_REG_1:
 288		return pBase->regDmn[1];
 289	case EEP_OP_CAP:
 290		return pBase->deviceCap;
 291	case EEP_OP_MODE:
 292		return pBase->opCapFlags;
 293	case EEP_RF_SILENT:
 294		return pBase->rfSilent;
 295	case EEP_OB_5:
 296		return pModal[0].ob;
 297	case EEP_DB_5:
 298		return pModal[0].db;
 299	case EEP_OB_2:
 300		return pModal[1].ob;
 301	case EEP_DB_2:
 302		return pModal[1].db;
 303	case EEP_MINOR_REV:
 304		return AR5416_VER_MASK;
 305	case EEP_TX_MASK:
 306		return pBase->txMask;
 307	case EEP_RX_MASK:
 308		return pBase->rxMask;
 309	case EEP_FSTCLK_5G:
 310		return pBase->fastClk5g;
 311	case EEP_RXGAIN_TYPE:
 312		return pBase->rxGainType;
 313	case EEP_TXGAIN_TYPE:
 314		return pBase->txGainType;
 315	case EEP_OL_PWRCTRL:
 316		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
 317			return pBase->openLoopPwrCntl ? true : false;
 318		else
 319			return false;
 320	case EEP_RC_CHAIN_MASK:
 321		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
 322			return pBase->rcChainMask;
 323		else
 324			return 0;
 325	case EEP_DAC_HPWR_5G:
 326		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_20)
 327			return pBase->dacHiPwrMode_5G;
 328		else
 329			return 0;
 330	case EEP_FRAC_N_5G:
 331		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_22)
 332			return pBase->frac_n_5g;
 333		else
 334			return 0;
 335	case EEP_PWR_TABLE_OFFSET:
 336		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_21)
 337			return pBase->pwr_table_offset;
 338		else
 339			return AR5416_PWR_TABLE_OFFSET_DB;
 
 
 
 
 
 
 
 
 340	default:
 341		return 0;
 342	}
 343}
 344
 345static void ath9k_hw_def_set_gain(struct ath_hw *ah,
 346				  struct modal_eep_header *pModal,
 347				  struct ar5416_eeprom_def *eep,
 348				  u8 txRxAttenLocal, int regChainOffset, int i)
 349{
 
 350	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_3) {
 351		txRxAttenLocal = pModal->txRxAttenCh[i];
 352
 353		if (AR_SREV_9280_20_OR_LATER(ah)) {
 354			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 355			      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
 356			      pModal->bswMargin[i]);
 357			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 358			      AR_PHY_GAIN_2GHZ_XATTEN1_DB,
 359			      pModal->bswAtten[i]);
 360			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 361			      AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
 362			      pModal->xatten2Margin[i]);
 363			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 364			      AR_PHY_GAIN_2GHZ_XATTEN2_DB,
 365			      pModal->xatten2Db[i]);
 366		} else {
 367			REG_WRITE(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 368			  (REG_READ(ah, AR_PHY_GAIN_2GHZ + regChainOffset) &
 369			   ~AR_PHY_GAIN_2GHZ_BSW_MARGIN)
 370			  | SM(pModal-> bswMargin[i],
 371			       AR_PHY_GAIN_2GHZ_BSW_MARGIN));
 372			REG_WRITE(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 373			  (REG_READ(ah, AR_PHY_GAIN_2GHZ + regChainOffset) &
 374			   ~AR_PHY_GAIN_2GHZ_BSW_ATTEN)
 375			  | SM(pModal->bswAtten[i],
 376			       AR_PHY_GAIN_2GHZ_BSW_ATTEN));
 377		}
 378	}
 379
 380	if (AR_SREV_9280_20_OR_LATER(ah)) {
 381		REG_RMW_FIELD(ah,
 382		      AR_PHY_RXGAIN + regChainOffset,
 383		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
 384		REG_RMW_FIELD(ah,
 385		      AR_PHY_RXGAIN + regChainOffset,
 386		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[i]);
 387	} else {
 388		REG_WRITE(ah,
 389			  AR_PHY_RXGAIN + regChainOffset,
 390			  (REG_READ(ah, AR_PHY_RXGAIN + regChainOffset) &
 391			   ~AR_PHY_RXGAIN_TXRX_ATTEN)
 392			  | SM(txRxAttenLocal, AR_PHY_RXGAIN_TXRX_ATTEN));
 393		REG_WRITE(ah,
 394			  AR_PHY_GAIN_2GHZ + regChainOffset,
 395			  (REG_READ(ah, AR_PHY_GAIN_2GHZ + regChainOffset) &
 396			   ~AR_PHY_GAIN_2GHZ_RXTX_MARGIN) |
 397			  SM(pModal->rxTxMarginCh[i], AR_PHY_GAIN_2GHZ_RXTX_MARGIN));
 398	}
 
 399}
 400
 401static void ath9k_hw_def_set_board_values(struct ath_hw *ah,
 402					  struct ath9k_channel *chan)
 403{
 404	struct modal_eep_header *pModal;
 405	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 406	int i, regChainOffset;
 407	u8 txRxAttenLocal;
 408
 409	pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
 410	txRxAttenLocal = IS_CHAN_2GHZ(chan) ? 23 : 44;
 411
 412	REG_WRITE(ah, AR_PHY_SWITCH_COM, pModal->antCtrlCommon & 0xffff);
 413
 414	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
 415		if (AR_SREV_9280(ah)) {
 416			if (i >= 2)
 417				break;
 418		}
 419
 420		if (AR_SREV_5416_20_OR_LATER(ah) &&
 421		    (ah->rxchainmask == 5 || ah->txchainmask == 5) && (i != 0))
 422			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
 423		else
 424			regChainOffset = i * 0x1000;
 425
 426		REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
 427			  pModal->antCtrlChain[i]);
 428
 429		REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
 430			  (REG_READ(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset) &
 431			   ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
 432			     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
 433			  SM(pModal->iqCalICh[i],
 434			     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
 435			  SM(pModal->iqCalQCh[i],
 436			     AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
 437
 438		if ((i == 0) || AR_SREV_5416_20_OR_LATER(ah))
 439			ath9k_hw_def_set_gain(ah, pModal, eep, txRxAttenLocal,
 440					      regChainOffset, i);
 441	}
 442
 443	if (AR_SREV_9280_20_OR_LATER(ah)) {
 444		if (IS_CHAN_2GHZ(chan)) {
 445			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
 446						  AR_AN_RF2G1_CH0_OB,
 447						  AR_AN_RF2G1_CH0_OB_S,
 448						  pModal->ob);
 449			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
 450						  AR_AN_RF2G1_CH0_DB,
 451						  AR_AN_RF2G1_CH0_DB_S,
 452						  pModal->db);
 453			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
 454						  AR_AN_RF2G1_CH1_OB,
 455						  AR_AN_RF2G1_CH1_OB_S,
 456						  pModal->ob_ch1);
 457			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
 458						  AR_AN_RF2G1_CH1_DB,
 459						  AR_AN_RF2G1_CH1_DB_S,
 460						  pModal->db_ch1);
 461		} else {
 462			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
 463						  AR_AN_RF5G1_CH0_OB5,
 464						  AR_AN_RF5G1_CH0_OB5_S,
 465						  pModal->ob);
 466			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
 467						  AR_AN_RF5G1_CH0_DB5,
 468						  AR_AN_RF5G1_CH0_DB5_S,
 469						  pModal->db);
 470			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
 471						  AR_AN_RF5G1_CH1_OB5,
 472						  AR_AN_RF5G1_CH1_OB5_S,
 473						  pModal->ob_ch1);
 474			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
 475						  AR_AN_RF5G1_CH1_DB5,
 476						  AR_AN_RF5G1_CH1_DB5_S,
 477						  pModal->db_ch1);
 478		}
 479		ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
 480					  AR_AN_TOP2_XPABIAS_LVL,
 481					  AR_AN_TOP2_XPABIAS_LVL_S,
 482					  pModal->xpaBiasLvl);
 483		ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
 484					  AR_AN_TOP2_LOCALBIAS,
 485					  AR_AN_TOP2_LOCALBIAS_S,
 486					  !!(pModal->lna_ctl &
 487					     LNA_CTL_LOCAL_BIAS));
 488		REG_RMW_FIELD(ah, AR_PHY_XPA_CFG, AR_PHY_FORCE_XPA_CFG,
 489			      !!(pModal->lna_ctl & LNA_CTL_FORCE_XPA));
 490	}
 491
 492	REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
 493		      pModal->switchSettling);
 494	REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
 495		      pModal->adcDesiredSize);
 496
 497	if (!AR_SREV_9280_20_OR_LATER(ah))
 498		REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
 499			      AR_PHY_DESIRED_SZ_PGA,
 500			      pModal->pgaDesiredSize);
 501
 502	REG_WRITE(ah, AR_PHY_RF_CTL4,
 503		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF)
 504		  | SM(pModal->txEndToXpaOff,
 505		       AR_PHY_RF_CTL4_TX_END_XPAB_OFF)
 506		  | SM(pModal->txFrameToXpaOn,
 507		       AR_PHY_RF_CTL4_FRAME_XPAA_ON)
 508		  | SM(pModal->txFrameToXpaOn,
 509		       AR_PHY_RF_CTL4_FRAME_XPAB_ON));
 510
 511	REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
 512		      pModal->txEndToRxOn);
 513
 514	if (AR_SREV_9280_20_OR_LATER(ah)) {
 515		REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
 516			      pModal->thresh62);
 517		REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0,
 518			      AR_PHY_EXT_CCA0_THRESH62,
 519			      pModal->thresh62);
 520	} else {
 521		REG_RMW_FIELD(ah, AR_PHY_CCA, AR_PHY_CCA_THRESH62,
 522			      pModal->thresh62);
 523		REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
 524			      AR_PHY_EXT_CCA_THRESH62,
 525			      pModal->thresh62);
 526	}
 527
 528	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_2) {
 529		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
 530			      AR_PHY_TX_END_DATA_START,
 531			      pModal->txFrameToDataStart);
 532		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
 533			      pModal->txFrameToPaOn);
 534	}
 535
 536	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_3) {
 537		if (IS_CHAN_HT40(chan))
 538			REG_RMW_FIELD(ah, AR_PHY_SETTLING,
 539				      AR_PHY_SETTLING_SWITCH,
 540				      pModal->swSettleHt40);
 541	}
 542
 543	if (AR_SREV_9280_20_OR_LATER(ah) &&
 544	    AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
 545		REG_RMW_FIELD(ah, AR_PHY_CCK_TX_CTRL,
 546			      AR_PHY_CCK_TX_CTRL_TX_DAC_SCALE_CCK,
 547			      pModal->miscBits);
 548
 549
 550	if (AR_SREV_9280_20(ah) && AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_20) {
 551		if (IS_CHAN_2GHZ(chan))
 552			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE,
 553					eep->baseEepHeader.dacLpMode);
 554		else if (eep->baseEepHeader.dacHiPwrMode_5G)
 555			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE, 0);
 556		else
 557			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE,
 558				      eep->baseEepHeader.dacLpMode);
 559
 560		udelay(100);
 561
 562		REG_RMW_FIELD(ah, AR_PHY_FRAME_CTL, AR_PHY_FRAME_CTL_TX_CLIP,
 563			      pModal->miscBits >> 2);
 564
 565		REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL9,
 566			      AR_PHY_TX_DESIRED_SCALE_CCK,
 567			      eep->baseEepHeader.desiredScaleCCK);
 568	}
 569}
 570
 571static void ath9k_hw_def_set_addac(struct ath_hw *ah,
 572				   struct ath9k_channel *chan)
 573{
 574#define XPA_LVL_FREQ(cnt) (pModal->xpaBiasLvlFreq[cnt])
 575	struct modal_eep_header *pModal;
 576	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 577	u8 biaslevel;
 578
 579	if (ah->hw_version.macVersion != AR_SREV_VERSION_9160)
 580		return;
 581
 582	if (ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_MINOR_VER_7)
 583		return;
 584
 585	pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
 586
 587	if (pModal->xpaBiasLvl != 0xff) {
 588		biaslevel = pModal->xpaBiasLvl;
 589	} else {
 590		u16 resetFreqBin, freqBin, freqCount = 0;
 591		struct chan_centers centers;
 592
 593		ath9k_hw_get_channel_centers(ah, chan, &centers);
 594
 595		resetFreqBin = FREQ2FBIN(centers.synth_center,
 596					 IS_CHAN_2GHZ(chan));
 597		freqBin = XPA_LVL_FREQ(0) & 0xff;
 598		biaslevel = (u8) (XPA_LVL_FREQ(0) >> 14);
 599
 600		freqCount++;
 601
 602		while (freqCount < 3) {
 603			if (XPA_LVL_FREQ(freqCount) == 0x0)
 604				break;
 605
 606			freqBin = XPA_LVL_FREQ(freqCount) & 0xff;
 607			if (resetFreqBin >= freqBin)
 608				biaslevel = (u8)(XPA_LVL_FREQ(freqCount) >> 14);
 609			else
 610				break;
 611			freqCount++;
 612		}
 613	}
 614
 615	if (IS_CHAN_2GHZ(chan)) {
 616		INI_RA(&ah->iniAddac, 7, 1) = (INI_RA(&ah->iniAddac,
 617					7, 1) & (~0x18)) | biaslevel << 3;
 618	} else {
 619		INI_RA(&ah->iniAddac, 6, 1) = (INI_RA(&ah->iniAddac,
 620					6, 1) & (~0xc0)) | biaslevel << 6;
 621	}
 622#undef XPA_LVL_FREQ
 623}
 624
 625static int16_t ath9k_change_gain_boundary_setting(struct ath_hw *ah,
 626				u16 *gb,
 627				u16 numXpdGain,
 628				u16 pdGainOverlap_t2,
 629				int8_t pwr_table_offset,
 630				int16_t *diff)
 631
 632{
 633	u16 k;
 634
 635	/* Prior to writing the boundaries or the pdadc vs. power table
 636	 * into the chip registers the default starting point on the pdadc
 637	 * vs. power table needs to be checked and the curve boundaries
 638	 * adjusted accordingly
 639	 */
 640	if (AR_SREV_9280_20_OR_LATER(ah)) {
 641		u16 gb_limit;
 642
 643		if (AR5416_PWR_TABLE_OFFSET_DB != pwr_table_offset) {
 644			/* get the difference in dB */
 645			*diff = (u16)(pwr_table_offset - AR5416_PWR_TABLE_OFFSET_DB);
 646			/* get the number of half dB steps */
 647			*diff *= 2;
 648			/* change the original gain boundary settings
 649			 * by the number of half dB steps
 650			 */
 651			for (k = 0; k < numXpdGain; k++)
 652				gb[k] = (u16)(gb[k] - *diff);
 653		}
 654		/* Because of a hardware limitation, ensure the gain boundary
 655		 * is not larger than (63 - overlap)
 656		 */
 657		gb_limit = (u16)(MAX_RATE_POWER - pdGainOverlap_t2);
 658
 659		for (k = 0; k < numXpdGain; k++)
 660			gb[k] = (u16)min(gb_limit, gb[k]);
 661	}
 662
 663	return *diff;
 664}
 665
 666static void ath9k_adjust_pdadc_values(struct ath_hw *ah,
 667				      int8_t pwr_table_offset,
 668				      int16_t diff,
 669				      u8 *pdadcValues)
 670{
 671#define NUM_PDADC(diff) (AR5416_NUM_PDADC_VALUES - diff)
 672	u16 k;
 673
 674	/* If this is a board that has a pwrTableOffset that differs from
 675	 * the default AR5416_PWR_TABLE_OFFSET_DB then the start of the
 676	 * pdadc vs pwr table needs to be adjusted prior to writing to the
 677	 * chip.
 678	 */
 679	if (AR_SREV_9280_20_OR_LATER(ah)) {
 680		if (AR5416_PWR_TABLE_OFFSET_DB != pwr_table_offset) {
 681			/* shift the table to start at the new offset */
 682			for (k = 0; k < (u16)NUM_PDADC(diff); k++ ) {
 683				pdadcValues[k] = pdadcValues[k + diff];
 684			}
 685
 686			/* fill the back of the table */
 687			for (k = (u16)NUM_PDADC(diff); k < NUM_PDADC(0); k++) {
 688				pdadcValues[k] = pdadcValues[NUM_PDADC(diff)];
 689			}
 690		}
 691	}
 692#undef NUM_PDADC
 693}
 694
 695static void ath9k_hw_set_def_power_cal_table(struct ath_hw *ah,
 696				  struct ath9k_channel *chan,
 697				  int16_t *pTxPowerIndexOffset)
 698{
 699#define SM_PD_GAIN(x) SM(0x38, AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_##x)
 700#define SM_PDGAIN_B(x, y) \
 701		SM((gainBoundaries[x]), AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_##y)
 702	struct ath_common *common = ath9k_hw_common(ah);
 703	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
 704	struct cal_data_per_freq *pRawDataset;
 705	u8 *pCalBChans = NULL;
 706	u16 pdGainOverlap_t2;
 707	static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
 708	u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
 709	u16 numPiers, i, j;
 710	int16_t diff = 0;
 711	u16 numXpdGain, xpdMask;
 712	u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
 713	u32 reg32, regOffset, regChainOffset;
 714	int16_t modalIdx;
 715	int8_t pwr_table_offset;
 716
 717	modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
 718	xpdMask = pEepData->modalHeader[modalIdx].xpdGain;
 719
 720	pwr_table_offset = ah->eep_ops->get_eeprom(ah, EEP_PWR_TABLE_OFFSET);
 721
 722	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
 723	    AR5416_EEP_MINOR_VER_2) {
 724		pdGainOverlap_t2 =
 725			pEepData->modalHeader[modalIdx].pdGainOverlap;
 726	} else {
 727		pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
 728					    AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
 729	}
 730
 731	if (IS_CHAN_2GHZ(chan)) {
 732		pCalBChans = pEepData->calFreqPier2G;
 733		numPiers = AR5416_NUM_2G_CAL_PIERS;
 734	} else {
 735		pCalBChans = pEepData->calFreqPier5G;
 736		numPiers = AR5416_NUM_5G_CAL_PIERS;
 737	}
 738
 739	if (OLC_FOR_AR9280_20_LATER && IS_CHAN_2GHZ(chan)) {
 740		pRawDataset = pEepData->calPierData2G[0];
 741		ah->initPDADC = ((struct calDataPerFreqOpLoop *)
 742				 pRawDataset)->vpdPdg[0][0];
 743	}
 744
 745	numXpdGain = 0;
 746
 747	for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
 748		if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
 749			if (numXpdGain >= AR5416_NUM_PD_GAINS)
 750				break;
 751			xpdGainValues[numXpdGain] =
 752				(u16)(AR5416_PD_GAINS_IN_MASK - i);
 753			numXpdGain++;
 754		}
 755	}
 756
 757	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
 758		      (numXpdGain - 1) & 0x3);
 759	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
 760		      xpdGainValues[0]);
 761	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
 762		      xpdGainValues[1]);
 763	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
 764		      xpdGainValues[2]);
 765
 766	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
 767		if (AR_SREV_5416_20_OR_LATER(ah) &&
 768		    (ah->rxchainmask == 5 || ah->txchainmask == 5) &&
 769		    (i != 0)) {
 770			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
 771		} else
 772			regChainOffset = i * 0x1000;
 773
 774		if (pEepData->baseEepHeader.txMask & (1 << i)) {
 775			if (IS_CHAN_2GHZ(chan))
 776				pRawDataset = pEepData->calPierData2G[i];
 777			else
 778				pRawDataset = pEepData->calPierData5G[i];
 779
 780
 781			if (OLC_FOR_AR9280_20_LATER) {
 782				u8 pcdacIdx;
 783				u8 txPower;
 784
 785				ath9k_get_txgain_index(ah, chan,
 786				(struct calDataPerFreqOpLoop *)pRawDataset,
 787				pCalBChans, numPiers, &txPower, &pcdacIdx);
 788				ath9k_olc_get_pdadcs(ah, pcdacIdx,
 789						     txPower/2, pdadcValues);
 790			} else {
 791				ath9k_hw_get_gain_boundaries_pdadcs(ah,
 792							chan, pRawDataset,
 793							pCalBChans, numPiers,
 794							pdGainOverlap_t2,
 795							gainBoundaries,
 796							pdadcValues,
 797							numXpdGain);
 798			}
 799
 800			diff = ath9k_change_gain_boundary_setting(ah,
 801							   gainBoundaries,
 802							   numXpdGain,
 803							   pdGainOverlap_t2,
 804							   pwr_table_offset,
 805							   &diff);
 806
 807			ENABLE_REGWRITE_BUFFER(ah);
 808
 809			if ((i == 0) || AR_SREV_5416_20_OR_LATER(ah)) {
 810				if (OLC_FOR_AR9280_20_LATER) {
 811					REG_WRITE(ah,
 812						AR_PHY_TPCRG5 + regChainOffset,
 813						SM(0x6,
 814						AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
 815						SM_PD_GAIN(1) | SM_PD_GAIN(2) |
 816						SM_PD_GAIN(3) | SM_PD_GAIN(4));
 817				} else {
 818					REG_WRITE(ah,
 819						AR_PHY_TPCRG5 + regChainOffset,
 820						SM(pdGainOverlap_t2,
 821						AR_PHY_TPCRG5_PD_GAIN_OVERLAP)|
 822						SM_PDGAIN_B(0, 1) |
 823						SM_PDGAIN_B(1, 2) |
 824						SM_PDGAIN_B(2, 3) |
 825						SM_PDGAIN_B(3, 4));
 826				}
 827			}
 828
 829
 830			ath9k_adjust_pdadc_values(ah, pwr_table_offset,
 831						  diff, pdadcValues);
 832
 833			regOffset = AR_PHY_BASE + (672 << 2) + regChainOffset;
 834			for (j = 0; j < 32; j++) {
 835				reg32 = get_unaligned_le32(&pdadcValues[4 * j]);
 836				REG_WRITE(ah, regOffset, reg32);
 837
 838				ath_dbg(common, ATH_DBG_EEPROM,
 839					"PDADC (%d,%4x): %4.4x %8.8x\n",
 840					i, regChainOffset, regOffset,
 841					reg32);
 842				ath_dbg(common, ATH_DBG_EEPROM,
 843					"PDADC: Chain %d | PDADC %3d "
 844					"Value %3d | PDADC %3d Value %3d | "
 845					"PDADC %3d Value %3d | PDADC %3d "
 846					"Value %3d |\n",
 847					i, 4 * j, pdadcValues[4 * j],
 848					4 * j + 1, pdadcValues[4 * j + 1],
 849					4 * j + 2, pdadcValues[4 * j + 2],
 850					4 * j + 3, pdadcValues[4 * j + 3]);
 851
 852				regOffset += 4;
 853			}
 854			REGWRITE_BUFFER_FLUSH(ah);
 855		}
 856	}
 857
 858	*pTxPowerIndexOffset = 0;
 859#undef SM_PD_GAIN
 860#undef SM_PDGAIN_B
 861}
 862
 863static void ath9k_hw_set_def_power_per_rate_table(struct ath_hw *ah,
 864						  struct ath9k_channel *chan,
 865						  int16_t *ratesArray,
 866						  u16 cfgCtl,
 867						  u16 AntennaReduction,
 868						  u16 twiceMaxRegulatoryPower,
 869						  u16 powerLimit)
 870{
 871#define REDUCE_SCALED_POWER_BY_TWO_CHAIN     6  /* 10*log10(2)*2 */
 872#define REDUCE_SCALED_POWER_BY_THREE_CHAIN   9 /* 10*log10(3)*2 */
 873
 874	struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
 875	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
 876	u16 twiceMaxEdgePower = MAX_RATE_POWER;
 877	static const u16 tpScaleReductionTable[5] =
 878		{ 0, 3, 6, 9, MAX_RATE_POWER };
 879
 880	int i;
 881	int16_t twiceLargestAntenna;
 882	struct cal_ctl_data *rep;
 883	struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
 884		0, { 0, 0, 0, 0}
 885	};
 886	struct cal_target_power_leg targetPowerOfdmExt = {
 887		0, { 0, 0, 0, 0} }, targetPowerCckExt = {
 888		0, { 0, 0, 0, 0 }
 889	};
 890	struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
 891		0, {0, 0, 0, 0}
 892	};
 893	u16 scaledPower = 0, minCtlPower, maxRegAllowedPower;
 894	static const u16 ctlModesFor11a[] = {
 895		CTL_11A, CTL_5GHT20, CTL_11A_EXT, CTL_5GHT40
 896	};
 897	static const u16 ctlModesFor11g[] = {
 898		CTL_11B, CTL_11G, CTL_2GHT20,
 899		CTL_11B_EXT, CTL_11G_EXT, CTL_2GHT40
 900	};
 901	u16 numCtlModes;
 902	const u16 *pCtlMode;
 903	u16 ctlMode, freq;
 904	struct chan_centers centers;
 905	int tx_chainmask;
 906	u16 twiceMinEdgePower;
 907
 908	tx_chainmask = ah->txchainmask;
 909
 910	ath9k_hw_get_channel_centers(ah, chan, &centers);
 911
 912	twiceLargestAntenna = max(
 913		pEepData->modalHeader
 914			[IS_CHAN_2GHZ(chan)].antennaGainCh[0],
 915		pEepData->modalHeader
 916			[IS_CHAN_2GHZ(chan)].antennaGainCh[1]);
 917
 918	twiceLargestAntenna = max((u8)twiceLargestAntenna,
 919				  pEepData->modalHeader
 920				  [IS_CHAN_2GHZ(chan)].antennaGainCh[2]);
 921
 922	twiceLargestAntenna = (int16_t)min(AntennaReduction -
 923					   twiceLargestAntenna, 0);
 924
 925	maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;
 926
 927	if (regulatory->tp_scale != ATH9K_TP_SCALE_MAX) {
 928		maxRegAllowedPower -=
 929			(tpScaleReductionTable[(regulatory->tp_scale)] * 2);
 930	}
 931
 932	scaledPower = min(powerLimit, maxRegAllowedPower);
 933
 934	switch (ar5416_get_ntxchains(tx_chainmask)) {
 935	case 1:
 936		break;
 937	case 2:
 938		if (scaledPower > REDUCE_SCALED_POWER_BY_TWO_CHAIN)
 939			scaledPower -= REDUCE_SCALED_POWER_BY_TWO_CHAIN;
 940		else
 941			scaledPower = 0;
 942		break;
 943	case 3:
 944		if (scaledPower > REDUCE_SCALED_POWER_BY_THREE_CHAIN)
 945			scaledPower -= REDUCE_SCALED_POWER_BY_THREE_CHAIN;
 946		else
 947			scaledPower = 0;
 948		break;
 949	}
 950
 951	if (IS_CHAN_2GHZ(chan)) {
 952		numCtlModes = ARRAY_SIZE(ctlModesFor11g) -
 953			SUB_NUM_CTL_MODES_AT_2G_40;
 954		pCtlMode = ctlModesFor11g;
 955
 956		ath9k_hw_get_legacy_target_powers(ah, chan,
 957			pEepData->calTargetPowerCck,
 958			AR5416_NUM_2G_CCK_TARGET_POWERS,
 959			&targetPowerCck, 4, false);
 960		ath9k_hw_get_legacy_target_powers(ah, chan,
 961			pEepData->calTargetPower2G,
 962			AR5416_NUM_2G_20_TARGET_POWERS,
 963			&targetPowerOfdm, 4, false);
 964		ath9k_hw_get_target_powers(ah, chan,
 965			pEepData->calTargetPower2GHT20,
 966			AR5416_NUM_2G_20_TARGET_POWERS,
 967			&targetPowerHt20, 8, false);
 968
 969		if (IS_CHAN_HT40(chan)) {
 970			numCtlModes = ARRAY_SIZE(ctlModesFor11g);
 971			ath9k_hw_get_target_powers(ah, chan,
 972				pEepData->calTargetPower2GHT40,
 973				AR5416_NUM_2G_40_TARGET_POWERS,
 974				&targetPowerHt40, 8, true);
 975			ath9k_hw_get_legacy_target_powers(ah, chan,
 976				pEepData->calTargetPowerCck,
 977				AR5416_NUM_2G_CCK_TARGET_POWERS,
 978				&targetPowerCckExt, 4, true);
 979			ath9k_hw_get_legacy_target_powers(ah, chan,
 980				pEepData->calTargetPower2G,
 981				AR5416_NUM_2G_20_TARGET_POWERS,
 982				&targetPowerOfdmExt, 4, true);
 983		}
 984	} else {
 985		numCtlModes = ARRAY_SIZE(ctlModesFor11a) -
 986			SUB_NUM_CTL_MODES_AT_5G_40;
 987		pCtlMode = ctlModesFor11a;
 988
 989		ath9k_hw_get_legacy_target_powers(ah, chan,
 990			pEepData->calTargetPower5G,
 991			AR5416_NUM_5G_20_TARGET_POWERS,
 992			&targetPowerOfdm, 4, false);
 993		ath9k_hw_get_target_powers(ah, chan,
 994			pEepData->calTargetPower5GHT20,
 995			AR5416_NUM_5G_20_TARGET_POWERS,
 996			&targetPowerHt20, 8, false);
 997
 998		if (IS_CHAN_HT40(chan)) {
 999			numCtlModes = ARRAY_SIZE(ctlModesFor11a);
1000			ath9k_hw_get_target_powers(ah, chan,
1001				pEepData->calTargetPower5GHT40,
1002				AR5416_NUM_5G_40_TARGET_POWERS,
1003				&targetPowerHt40, 8, true);
1004			ath9k_hw_get_legacy_target_powers(ah, chan,
1005				pEepData->calTargetPower5G,
1006				AR5416_NUM_5G_20_TARGET_POWERS,
1007				&targetPowerOfdmExt, 4, true);
1008		}
1009	}
1010
1011	for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
1012		bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
1013			(pCtlMode[ctlMode] == CTL_2GHT40);
1014		if (isHt40CtlMode)
1015			freq = centers.synth_center;
1016		else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
1017			freq = centers.ext_center;
1018		else
1019			freq = centers.ctl_center;
1020
1021		if (ah->eep_ops->get_eeprom_ver(ah) == 14 &&
1022		    ah->eep_ops->get_eeprom_rev(ah) <= 2)
1023			twiceMaxEdgePower = MAX_RATE_POWER;
1024
1025		for (i = 0; (i < AR5416_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {
1026			if ((((cfgCtl & ~CTL_MODE_M) |
1027			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
1028			     pEepData->ctlIndex[i]) ||
1029			    (((cfgCtl & ~CTL_MODE_M) |
1030			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
1031			     ((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
1032				rep = &(pEepData->ctlData[i]);
1033
1034				twiceMinEdgePower = ath9k_hw_get_max_edge_power(freq,
1035				rep->ctlEdges[ar5416_get_ntxchains(tx_chainmask) - 1],
1036				IS_CHAN_2GHZ(chan), AR5416_NUM_BAND_EDGES);
1037
1038				if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
1039					twiceMaxEdgePower = min(twiceMaxEdgePower,
1040								twiceMinEdgePower);
1041				} else {
1042					twiceMaxEdgePower = twiceMinEdgePower;
1043					break;
1044				}
1045			}
1046		}
1047
1048		minCtlPower = min(twiceMaxEdgePower, scaledPower);
1049
1050		switch (pCtlMode[ctlMode]) {
1051		case CTL_11B:
1052			for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x); i++) {
1053				targetPowerCck.tPow2x[i] =
1054					min((u16)targetPowerCck.tPow2x[i],
1055					    minCtlPower);
1056			}
1057			break;
1058		case CTL_11A:
1059		case CTL_11G:
1060			for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x); i++) {
1061				targetPowerOfdm.tPow2x[i] =
1062					min((u16)targetPowerOfdm.tPow2x[i],
1063					    minCtlPower);
1064			}
1065			break;
1066		case CTL_5GHT20:
1067		case CTL_2GHT20:
1068			for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++) {
1069				targetPowerHt20.tPow2x[i] =
1070					min((u16)targetPowerHt20.tPow2x[i],
1071					    minCtlPower);
1072			}
1073			break;
1074		case CTL_11B_EXT:
1075			targetPowerCckExt.tPow2x[0] = min((u16)
1076					targetPowerCckExt.tPow2x[0],
1077					minCtlPower);
1078			break;
1079		case CTL_11A_EXT:
1080		case CTL_11G_EXT:
1081			targetPowerOfdmExt.tPow2x[0] = min((u16)
1082					targetPowerOfdmExt.tPow2x[0],
1083					minCtlPower);
1084			break;
1085		case CTL_5GHT40:
1086		case CTL_2GHT40:
1087			for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
1088				targetPowerHt40.tPow2x[i] =
1089					min((u16)targetPowerHt40.tPow2x[i],
1090					    minCtlPower);
1091			}
1092			break;
1093		default:
1094			break;
1095		}
1096	}
1097
1098	ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
1099		ratesArray[rate18mb] = ratesArray[rate24mb] =
1100		targetPowerOfdm.tPow2x[0];
1101	ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
1102	ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
1103	ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
1104	ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
1105
1106	for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
1107		ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
1108
1109	if (IS_CHAN_2GHZ(chan)) {
1110		ratesArray[rate1l] = targetPowerCck.tPow2x[0];
1111		ratesArray[rate2s] = ratesArray[rate2l] =
1112			targetPowerCck.tPow2x[1];
1113		ratesArray[rate5_5s] = ratesArray[rate5_5l] =
1114			targetPowerCck.tPow2x[2];
1115		ratesArray[rate11s] = ratesArray[rate11l] =
1116			targetPowerCck.tPow2x[3];
1117	}
1118	if (IS_CHAN_HT40(chan)) {
1119		for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
1120			ratesArray[rateHt40_0 + i] =
1121				targetPowerHt40.tPow2x[i];
1122		}
1123		ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
1124		ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
1125		ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
1126		if (IS_CHAN_2GHZ(chan)) {
1127			ratesArray[rateExtCck] =
1128				targetPowerCckExt.tPow2x[0];
1129		}
1130	}
1131}
1132
1133static void ath9k_hw_def_set_txpower(struct ath_hw *ah,
1134				    struct ath9k_channel *chan,
1135				    u16 cfgCtl,
1136				    u8 twiceAntennaReduction,
1137				    u8 twiceMaxRegulatoryPower,
1138				    u8 powerLimit, bool test)
1139{
1140#define RT_AR_DELTA(x) (ratesArray[x] - cck_ofdm_delta)
1141	struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
1142	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
1143	struct modal_eep_header *pModal =
1144		&(pEepData->modalHeader[IS_CHAN_2GHZ(chan)]);
1145	int16_t ratesArray[Ar5416RateSize];
1146	int16_t txPowerIndexOffset = 0;
1147	u8 ht40PowerIncForPdadc = 2;
1148	int i, cck_ofdm_delta = 0;
1149
1150	memset(ratesArray, 0, sizeof(ratesArray));
1151
1152	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
1153	    AR5416_EEP_MINOR_VER_2) {
1154		ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
1155	}
1156
1157	ath9k_hw_set_def_power_per_rate_table(ah, chan,
1158					       &ratesArray[0], cfgCtl,
1159					       twiceAntennaReduction,
1160					       twiceMaxRegulatoryPower,
1161					       powerLimit);
1162
1163	ath9k_hw_set_def_power_cal_table(ah, chan, &txPowerIndexOffset);
1164
1165	regulatory->max_power_level = 0;
1166	for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
1167		ratesArray[i] =	(int16_t)(txPowerIndexOffset + ratesArray[i]);
1168		if (ratesArray[i] > MAX_RATE_POWER)
1169			ratesArray[i] = MAX_RATE_POWER;
1170		if (ratesArray[i] > regulatory->max_power_level)
1171			regulatory->max_power_level = ratesArray[i];
1172	}
1173
1174	if (!test) {
1175		i = rate6mb;
1176
1177		if (IS_CHAN_HT40(chan))
1178			i = rateHt40_0;
1179		else if (IS_CHAN_HT20(chan))
1180			i = rateHt20_0;
1181
1182		regulatory->max_power_level = ratesArray[i];
1183	}
1184
1185	switch(ar5416_get_ntxchains(ah->txchainmask)) {
1186	case 1:
1187		break;
1188	case 2:
1189		regulatory->max_power_level += INCREASE_MAXPOW_BY_TWO_CHAIN;
1190		break;
1191	case 3:
1192		regulatory->max_power_level += INCREASE_MAXPOW_BY_THREE_CHAIN;
1193		break;
1194	default:
1195		ath_dbg(ath9k_hw_common(ah), ATH_DBG_EEPROM,
1196			"Invalid chainmask configuration\n");
1197		break;
1198	}
1199
1200	if (test)
1201		return;
1202
1203	if (AR_SREV_9280_20_OR_LATER(ah)) {
1204		for (i = 0; i < Ar5416RateSize; i++) {
1205			int8_t pwr_table_offset;
1206
1207			pwr_table_offset = ah->eep_ops->get_eeprom(ah,
1208							EEP_PWR_TABLE_OFFSET);
1209			ratesArray[i] -= pwr_table_offset * 2;
1210		}
1211	}
1212
1213	ENABLE_REGWRITE_BUFFER(ah);
1214
1215	REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
1216		  ATH9K_POW_SM(ratesArray[rate18mb], 24)
1217		  | ATH9K_POW_SM(ratesArray[rate12mb], 16)
1218		  | ATH9K_POW_SM(ratesArray[rate9mb], 8)
1219		  | ATH9K_POW_SM(ratesArray[rate6mb], 0));
1220	REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
1221		  ATH9K_POW_SM(ratesArray[rate54mb], 24)
1222		  | ATH9K_POW_SM(ratesArray[rate48mb], 16)
1223		  | ATH9K_POW_SM(ratesArray[rate36mb], 8)
1224		  | ATH9K_POW_SM(ratesArray[rate24mb], 0));
1225
1226	if (IS_CHAN_2GHZ(chan)) {
1227		if (OLC_FOR_AR9280_20_LATER) {
1228			cck_ofdm_delta = 2;
1229			REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
1230				ATH9K_POW_SM(RT_AR_DELTA(rate2s), 24)
1231				| ATH9K_POW_SM(RT_AR_DELTA(rate2l), 16)
1232				| ATH9K_POW_SM(ratesArray[rateXr], 8)
1233				| ATH9K_POW_SM(RT_AR_DELTA(rate1l), 0));
1234			REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
1235				ATH9K_POW_SM(RT_AR_DELTA(rate11s), 24)
1236				| ATH9K_POW_SM(RT_AR_DELTA(rate11l), 16)
1237				| ATH9K_POW_SM(RT_AR_DELTA(rate5_5s), 8)
1238				| ATH9K_POW_SM(RT_AR_DELTA(rate5_5l), 0));
1239		} else {
1240			REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
1241				ATH9K_POW_SM(ratesArray[rate2s], 24)
1242				| ATH9K_POW_SM(ratesArray[rate2l], 16)
1243				| ATH9K_POW_SM(ratesArray[rateXr], 8)
1244				| ATH9K_POW_SM(ratesArray[rate1l], 0));
1245			REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
1246				ATH9K_POW_SM(ratesArray[rate11s], 24)
1247				| ATH9K_POW_SM(ratesArray[rate11l], 16)
1248				| ATH9K_POW_SM(ratesArray[rate5_5s], 8)
1249				| ATH9K_POW_SM(ratesArray[rate5_5l], 0));
1250		}
1251	}
1252
1253	REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
1254		  ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
1255		  | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
1256		  | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
1257		  | ATH9K_POW_SM(ratesArray[rateHt20_0], 0));
1258	REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
1259		  ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
1260		  | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
1261		  | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
1262		  | ATH9K_POW_SM(ratesArray[rateHt20_4], 0));
1263
1264	if (IS_CHAN_HT40(chan)) {
1265		REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
1266			  ATH9K_POW_SM(ratesArray[rateHt40_3] +
1267				       ht40PowerIncForPdadc, 24)
1268			  | ATH9K_POW_SM(ratesArray[rateHt40_2] +
1269					 ht40PowerIncForPdadc, 16)
1270			  | ATH9K_POW_SM(ratesArray[rateHt40_1] +
1271					 ht40PowerIncForPdadc, 8)
1272			  | ATH9K_POW_SM(ratesArray[rateHt40_0] +
1273					 ht40PowerIncForPdadc, 0));
1274		REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
1275			  ATH9K_POW_SM(ratesArray[rateHt40_7] +
1276				       ht40PowerIncForPdadc, 24)
1277			  | ATH9K_POW_SM(ratesArray[rateHt40_6] +
1278					 ht40PowerIncForPdadc, 16)
1279			  | ATH9K_POW_SM(ratesArray[rateHt40_5] +
1280					 ht40PowerIncForPdadc, 8)
1281			  | ATH9K_POW_SM(ratesArray[rateHt40_4] +
1282					 ht40PowerIncForPdadc, 0));
1283		if (OLC_FOR_AR9280_20_LATER) {
1284			REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
1285				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
1286				| ATH9K_POW_SM(RT_AR_DELTA(rateExtCck), 16)
1287				| ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
1288				| ATH9K_POW_SM(RT_AR_DELTA(rateDupCck), 0));
1289		} else {
1290			REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
1291				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
1292				| ATH9K_POW_SM(ratesArray[rateExtCck], 16)
1293				| ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
1294				| ATH9K_POW_SM(ratesArray[rateDupCck], 0));
1295		}
1296	}
1297
1298	REG_WRITE(ah, AR_PHY_POWER_TX_SUB,
1299		  ATH9K_POW_SM(pModal->pwrDecreaseFor3Chain, 6)
1300		  | ATH9K_POW_SM(pModal->pwrDecreaseFor2Chain, 0));
1301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1302	REGWRITE_BUFFER_FLUSH(ah);
1303}
1304
1305static u16 ath9k_hw_def_get_spur_channel(struct ath_hw *ah, u16 i, bool is2GHz)
1306{
1307#define EEP_DEF_SPURCHAN \
1308	(ah->eeprom.def.modalHeader[is2GHz].spurChans[i].spurChan)
1309	struct ath_common *common = ath9k_hw_common(ah);
1310
1311	u16 spur_val = AR_NO_SPUR;
1312
1313	ath_dbg(common, ATH_DBG_ANI,
1314		"Getting spur idx:%d is2Ghz:%d val:%x\n",
1315		i, is2GHz, ah->config.spurchans[i][is2GHz]);
1316
1317	switch (ah->config.spurmode) {
1318	case SPUR_DISABLE:
1319		break;
1320	case SPUR_ENABLE_IOCTL:
1321		spur_val = ah->config.spurchans[i][is2GHz];
1322		ath_dbg(common, ATH_DBG_ANI,
1323			"Getting spur val from new loc. %d\n", spur_val);
1324		break;
1325	case SPUR_ENABLE_EEPROM:
1326		spur_val = EEP_DEF_SPURCHAN;
1327		break;
1328	}
1329
1330	return spur_val;
1331
1332#undef EEP_DEF_SPURCHAN
1333}
1334
1335const struct eeprom_ops eep_def_ops = {
1336	.check_eeprom		= ath9k_hw_def_check_eeprom,
1337	.get_eeprom		= ath9k_hw_def_get_eeprom,
1338	.fill_eeprom		= ath9k_hw_def_fill_eeprom,
 
1339	.get_eeprom_ver		= ath9k_hw_def_get_eeprom_ver,
1340	.get_eeprom_rev		= ath9k_hw_def_get_eeprom_rev,
1341	.set_board_values	= ath9k_hw_def_set_board_values,
1342	.set_addac		= ath9k_hw_def_set_addac,
1343	.set_txpower		= ath9k_hw_def_set_txpower,
1344	.get_spur_channel	= ath9k_hw_def_get_spur_channel
1345};
v4.6
   1/*
   2 * Copyright (c) 2008-2011 Atheros Communications Inc.
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17#include <asm/unaligned.h>
  18#include "hw.h"
  19#include "ar9002_phy.h"
  20
  21static void ath9k_get_txgain_index(struct ath_hw *ah,
  22		struct ath9k_channel *chan,
  23		struct calDataPerFreqOpLoop *rawDatasetOpLoop,
  24		u8 *calChans,  u16 availPiers, u8 *pwr, u8 *pcdacIdx)
  25{
  26	u8 pcdac, i = 0;
  27	u16 idxL = 0, idxR = 0, numPiers;
  28	bool match;
  29	struct chan_centers centers;
  30
  31	ath9k_hw_get_channel_centers(ah, chan, &centers);
  32
  33	for (numPiers = 0; numPiers < availPiers; numPiers++)
  34		if (calChans[numPiers] == AR5416_BCHAN_UNUSED)
  35			break;
  36
  37	match = ath9k_hw_get_lower_upper_index(
  38			(u8)FREQ2FBIN(centers.synth_center, IS_CHAN_2GHZ(chan)),
  39			calChans, numPiers, &idxL, &idxR);
  40	if (match) {
  41		pcdac = rawDatasetOpLoop[idxL].pcdac[0][0];
  42		*pwr = rawDatasetOpLoop[idxL].pwrPdg[0][0];
  43	} else {
  44		pcdac = rawDatasetOpLoop[idxR].pcdac[0][0];
  45		*pwr = (rawDatasetOpLoop[idxL].pwrPdg[0][0] +
  46				rawDatasetOpLoop[idxR].pwrPdg[0][0])/2;
  47	}
  48
  49	while (pcdac > ah->originalGain[i] &&
  50			i < (AR9280_TX_GAIN_TABLE_SIZE - 1))
  51		i++;
  52
  53	*pcdacIdx = i;
  54}
  55
  56static void ath9k_olc_get_pdadcs(struct ath_hw *ah,
  57				u32 initTxGain,
  58				int txPower,
  59				u8 *pPDADCValues)
  60{
  61	u32 i;
  62	u32 offset;
  63
  64	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL6_0,
  65			AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
  66	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL6_1,
  67			AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
  68
  69	REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL7,
  70			AR_PHY_TX_PWRCTRL_INIT_TX_GAIN, initTxGain);
  71
  72	offset = txPower;
  73	for (i = 0; i < AR5416_NUM_PDADC_VALUES; i++)
  74		if (i < offset)
  75			pPDADCValues[i] = 0x0;
  76		else
  77			pPDADCValues[i] = 0xFF;
  78}
  79
  80static int ath9k_hw_def_get_eeprom_ver(struct ath_hw *ah)
  81{
  82	return ((ah->eeprom.def.baseEepHeader.version >> 12) & 0xF);
  83}
  84
  85static int ath9k_hw_def_get_eeprom_rev(struct ath_hw *ah)
  86{
  87	return ((ah->eeprom.def.baseEepHeader.version) & 0xFFF);
  88}
  89
  90#define SIZE_EEPROM_DEF (sizeof(struct ar5416_eeprom_def) / sizeof(u16))
  91
  92static bool __ath9k_hw_def_fill_eeprom(struct ath_hw *ah)
  93{
 
  94	u16 *eep_data = (u16 *)&ah->eeprom.def;
  95	int addr, ar5416_eep_start_loc = 0x100;
  96
  97	for (addr = 0; addr < SIZE_EEPROM_DEF; addr++) {
  98		if (!ath9k_hw_nvram_read(ah, addr + ar5416_eep_start_loc,
  99					 eep_data))
 
 
 100			return false;
 
 101		eep_data++;
 102	}
 103	return true;
 104}
 105
 106static bool __ath9k_hw_usb_def_fill_eeprom(struct ath_hw *ah)
 107{
 108	u16 *eep_data = (u16 *)&ah->eeprom.def;
 109
 110	ath9k_hw_usb_gen_fill_eeprom(ah, eep_data,
 111				     0x100, SIZE_EEPROM_DEF);
 112	return true;
 113}
 114
 115static bool ath9k_hw_def_fill_eeprom(struct ath_hw *ah)
 116{
 117	struct ath_common *common = ath9k_hw_common(ah);
 118
 119	if (!ath9k_hw_use_flash(ah)) {
 120		ath_dbg(common, EEPROM, "Reading from EEPROM, not flash\n");
 
 121	}
 122
 123	if (common->bus_ops->ath_bus_type == ATH_USB)
 124		return __ath9k_hw_usb_def_fill_eeprom(ah);
 125	else
 126		return __ath9k_hw_def_fill_eeprom(ah);
 127}
 128
 129#if defined(CONFIG_ATH9K_DEBUGFS) || defined(CONFIG_ATH9K_HTC_DEBUGFS)
 130static u32 ath9k_def_dump_modal_eeprom(char *buf, u32 len, u32 size,
 131				       struct modal_eep_header *modal_hdr)
 132{
 133	PR_EEP("Chain0 Ant. Control", modal_hdr->antCtrlChain[0]);
 134	PR_EEP("Chain1 Ant. Control", modal_hdr->antCtrlChain[1]);
 135	PR_EEP("Chain2 Ant. Control", modal_hdr->antCtrlChain[2]);
 136	PR_EEP("Ant. Common Control", modal_hdr->antCtrlCommon);
 137	PR_EEP("Chain0 Ant. Gain", modal_hdr->antennaGainCh[0]);
 138	PR_EEP("Chain1 Ant. Gain", modal_hdr->antennaGainCh[1]);
 139	PR_EEP("Chain2 Ant. Gain", modal_hdr->antennaGainCh[2]);
 140	PR_EEP("Switch Settle", modal_hdr->switchSettling);
 141	PR_EEP("Chain0 TxRxAtten", modal_hdr->txRxAttenCh[0]);
 142	PR_EEP("Chain1 TxRxAtten", modal_hdr->txRxAttenCh[1]);
 143	PR_EEP("Chain2 TxRxAtten", modal_hdr->txRxAttenCh[2]);
 144	PR_EEP("Chain0 RxTxMargin", modal_hdr->rxTxMarginCh[0]);
 145	PR_EEP("Chain1 RxTxMargin", modal_hdr->rxTxMarginCh[1]);
 146	PR_EEP("Chain2 RxTxMargin", modal_hdr->rxTxMarginCh[2]);
 147	PR_EEP("ADC Desired size", modal_hdr->adcDesiredSize);
 148	PR_EEP("PGA Desired size", modal_hdr->pgaDesiredSize);
 149	PR_EEP("Chain0 xlna Gain", modal_hdr->xlnaGainCh[0]);
 150	PR_EEP("Chain1 xlna Gain", modal_hdr->xlnaGainCh[1]);
 151	PR_EEP("Chain2 xlna Gain", modal_hdr->xlnaGainCh[2]);
 152	PR_EEP("txEndToXpaOff", modal_hdr->txEndToXpaOff);
 153	PR_EEP("txEndToRxOn", modal_hdr->txEndToRxOn);
 154	PR_EEP("txFrameToXpaOn", modal_hdr->txFrameToXpaOn);
 155	PR_EEP("CCA Threshold)", modal_hdr->thresh62);
 156	PR_EEP("Chain0 NF Threshold", modal_hdr->noiseFloorThreshCh[0]);
 157	PR_EEP("Chain1 NF Threshold", modal_hdr->noiseFloorThreshCh[1]);
 158	PR_EEP("Chain2 NF Threshold", modal_hdr->noiseFloorThreshCh[2]);
 159	PR_EEP("xpdGain", modal_hdr->xpdGain);
 160	PR_EEP("External PD", modal_hdr->xpd);
 161	PR_EEP("Chain0 I Coefficient", modal_hdr->iqCalICh[0]);
 162	PR_EEP("Chain1 I Coefficient", modal_hdr->iqCalICh[1]);
 163	PR_EEP("Chain2 I Coefficient", modal_hdr->iqCalICh[2]);
 164	PR_EEP("Chain0 Q Coefficient", modal_hdr->iqCalQCh[0]);
 165	PR_EEP("Chain1 Q Coefficient", modal_hdr->iqCalQCh[1]);
 166	PR_EEP("Chain2 Q Coefficient", modal_hdr->iqCalQCh[2]);
 167	PR_EEP("pdGainOverlap", modal_hdr->pdGainOverlap);
 168	PR_EEP("Chain0 OutputBias", modal_hdr->ob);
 169	PR_EEP("Chain0 DriverBias", modal_hdr->db);
 170	PR_EEP("xPA Bias Level", modal_hdr->xpaBiasLvl);
 171	PR_EEP("2chain pwr decrease", modal_hdr->pwrDecreaseFor2Chain);
 172	PR_EEP("3chain pwr decrease", modal_hdr->pwrDecreaseFor3Chain);
 173	PR_EEP("txFrameToDataStart", modal_hdr->txFrameToDataStart);
 174	PR_EEP("txFrameToPaOn", modal_hdr->txFrameToPaOn);
 175	PR_EEP("HT40 Power Inc.", modal_hdr->ht40PowerIncForPdadc);
 176	PR_EEP("Chain0 bswAtten", modal_hdr->bswAtten[0]);
 177	PR_EEP("Chain1 bswAtten", modal_hdr->bswAtten[1]);
 178	PR_EEP("Chain2 bswAtten", modal_hdr->bswAtten[2]);
 179	PR_EEP("Chain0 bswMargin", modal_hdr->bswMargin[0]);
 180	PR_EEP("Chain1 bswMargin", modal_hdr->bswMargin[1]);
 181	PR_EEP("Chain2 bswMargin", modal_hdr->bswMargin[2]);
 182	PR_EEP("HT40 Switch Settle", modal_hdr->swSettleHt40);
 183	PR_EEP("Chain0 xatten2Db", modal_hdr->xatten2Db[0]);
 184	PR_EEP("Chain1 xatten2Db", modal_hdr->xatten2Db[1]);
 185	PR_EEP("Chain2 xatten2Db", modal_hdr->xatten2Db[2]);
 186	PR_EEP("Chain0 xatten2Margin", modal_hdr->xatten2Margin[0]);
 187	PR_EEP("Chain1 xatten2Margin", modal_hdr->xatten2Margin[1]);
 188	PR_EEP("Chain2 xatten2Margin", modal_hdr->xatten2Margin[2]);
 189	PR_EEP("Chain1 OutputBias", modal_hdr->ob_ch1);
 190	PR_EEP("Chain1 DriverBias", modal_hdr->db_ch1);
 191	PR_EEP("LNA Control", modal_hdr->lna_ctl);
 192	PR_EEP("XPA Bias Freq0", modal_hdr->xpaBiasLvlFreq[0]);
 193	PR_EEP("XPA Bias Freq1", modal_hdr->xpaBiasLvlFreq[1]);
 194	PR_EEP("XPA Bias Freq2", modal_hdr->xpaBiasLvlFreq[2]);
 195
 196	return len;
 197}
 
 198
 199static u32 ath9k_hw_def_dump_eeprom(struct ath_hw *ah, bool dump_base_hdr,
 200				    u8 *buf, u32 len, u32 size)
 201{
 202	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 203	struct base_eep_header *pBase = &eep->baseEepHeader;
 204
 205	if (!dump_base_hdr) {
 206		len += scnprintf(buf + len, size - len,
 207				 "%20s :\n", "2GHz modal Header");
 208		len = ath9k_def_dump_modal_eeprom(buf, len, size,
 209						   &eep->modalHeader[0]);
 210		len += scnprintf(buf + len, size - len,
 211				 "%20s :\n", "5GHz modal Header");
 212		len = ath9k_def_dump_modal_eeprom(buf, len, size,
 213						   &eep->modalHeader[1]);
 214		goto out;
 215	}
 216
 217	PR_EEP("Major Version", pBase->version >> 12);
 218	PR_EEP("Minor Version", pBase->version & 0xFFF);
 219	PR_EEP("Checksum", pBase->checksum);
 220	PR_EEP("Length", pBase->length);
 221	PR_EEP("RegDomain1", pBase->regDmn[0]);
 222	PR_EEP("RegDomain2", pBase->regDmn[1]);
 223	PR_EEP("TX Mask", pBase->txMask);
 224	PR_EEP("RX Mask", pBase->rxMask);
 225	PR_EEP("Allow 5GHz", !!(pBase->opCapFlags & AR5416_OPFLAGS_11A));
 226	PR_EEP("Allow 2GHz", !!(pBase->opCapFlags & AR5416_OPFLAGS_11G));
 227	PR_EEP("Disable 2GHz HT20", !!(pBase->opCapFlags &
 228					AR5416_OPFLAGS_N_2G_HT20));
 229	PR_EEP("Disable 2GHz HT40", !!(pBase->opCapFlags &
 230					AR5416_OPFLAGS_N_2G_HT40));
 231	PR_EEP("Disable 5Ghz HT20", !!(pBase->opCapFlags &
 232					AR5416_OPFLAGS_N_5G_HT20));
 233	PR_EEP("Disable 5Ghz HT40", !!(pBase->opCapFlags &
 234					AR5416_OPFLAGS_N_5G_HT40));
 235	PR_EEP("Big Endian", !!(pBase->eepMisc & 0x01));
 236	PR_EEP("Cal Bin Major Ver", (pBase->binBuildNumber >> 24) & 0xFF);
 237	PR_EEP("Cal Bin Minor Ver", (pBase->binBuildNumber >> 16) & 0xFF);
 238	PR_EEP("Cal Bin Build", (pBase->binBuildNumber >> 8) & 0xFF);
 239	PR_EEP("OpenLoop Power Ctrl", pBase->openLoopPwrCntl);
 240
 241	len += scnprintf(buf + len, size - len, "%20s : %pM\n", "MacAddress",
 242			 pBase->macAddr);
 243
 244out:
 245	if (len > size)
 246		len = size;
 247
 248	return len;
 249}
 250#else
 251static u32 ath9k_hw_def_dump_eeprom(struct ath_hw *ah, bool dump_base_hdr,
 252				    u8 *buf, u32 len, u32 size)
 253{
 254	return 0;
 255}
 256#endif
 257
 258static int ath9k_hw_def_check_eeprom(struct ath_hw *ah)
 259{
 260	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 261	struct ath_common *common = ath9k_hw_common(ah);
 262	u32 el;
 263	bool need_swap;
 264	int i, err;
 265
 266	err = ath9k_hw_nvram_swap_data(ah, &need_swap, SIZE_EEPROM_DEF);
 267	if (err)
 268		return err;
 269
 270	if (need_swap)
 271		el = swab16(eep->baseEepHeader.length);
 272	else
 273		el = eep->baseEepHeader.length;
 
 
 274
 275	el = min(el / sizeof(u16), SIZE_EEPROM_DEF);
 276	if (!ath9k_hw_nvram_validate_checksum(ah, el))
 277		return -EINVAL;
 278
 279	if (need_swap) {
 280		u32 integer, j;
 281		u16 word;
 282
 
 
 
 283		word = swab16(eep->baseEepHeader.length);
 284		eep->baseEepHeader.length = word;
 285
 286		word = swab16(eep->baseEepHeader.checksum);
 287		eep->baseEepHeader.checksum = word;
 288
 289		word = swab16(eep->baseEepHeader.version);
 290		eep->baseEepHeader.version = word;
 291
 292		word = swab16(eep->baseEepHeader.regDmn[0]);
 293		eep->baseEepHeader.regDmn[0] = word;
 294
 295		word = swab16(eep->baseEepHeader.regDmn[1]);
 296		eep->baseEepHeader.regDmn[1] = word;
 297
 298		word = swab16(eep->baseEepHeader.rfSilent);
 299		eep->baseEepHeader.rfSilent = word;
 300
 301		word = swab16(eep->baseEepHeader.blueToothOptions);
 302		eep->baseEepHeader.blueToothOptions = word;
 303
 304		word = swab16(eep->baseEepHeader.deviceCap);
 305		eep->baseEepHeader.deviceCap = word;
 306
 307		for (j = 0; j < ARRAY_SIZE(eep->modalHeader); j++) {
 308			struct modal_eep_header *pModal =
 309				&eep->modalHeader[j];
 310			integer = swab32(pModal->antCtrlCommon);
 311			pModal->antCtrlCommon = integer;
 312
 313			for (i = 0; i < AR5416_MAX_CHAINS; i++) {
 314				integer = swab32(pModal->antCtrlChain[i]);
 315				pModal->antCtrlChain[i] = integer;
 316			}
 317			for (i = 0; i < 3; i++) {
 318				word = swab16(pModal->xpaBiasLvlFreq[i]);
 319				pModal->xpaBiasLvlFreq[i] = word;
 320			}
 321
 322			for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
 323				word = swab16(pModal->spurChans[i].spurChan);
 324				pModal->spurChans[i].spurChan = word;
 325			}
 326		}
 327	}
 328
 329	if (!ath9k_hw_nvram_check_version(ah, AR5416_EEP_VER,
 330	    AR5416_EEP_NO_BACK_VER))
 
 
 331		return -EINVAL;
 
 332
 333	/* Enable fixup for AR_AN_TOP2 if necessary */
 334	if ((ah->hw_version.devid == AR9280_DEVID_PCI) &&
 335	    ((eep->baseEepHeader.version & 0xff) > 0x0a) &&
 336	    (eep->baseEepHeader.pwdclkind == 0))
 337		ah->need_an_top2_fixup = true;
 338
 339	if ((common->bus_ops->ath_bus_type == ATH_USB) &&
 340	    (AR_SREV_9280(ah)))
 341		eep->modalHeader[0].xpaBiasLvl = 0;
 342
 343	return 0;
 344}
 345
 346#undef SIZE_EEPROM_DEF
 347
 348static u32 ath9k_hw_def_get_eeprom(struct ath_hw *ah,
 349				   enum eeprom_param param)
 350{
 351	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 352	struct modal_eep_header *pModal = eep->modalHeader;
 353	struct base_eep_header *pBase = &eep->baseEepHeader;
 354	int band = 0;
 355
 356	switch (param) {
 357	case EEP_NFTHRESH_5:
 358		return pModal[0].noiseFloorThreshCh[0];
 359	case EEP_NFTHRESH_2:
 360		return pModal[1].noiseFloorThreshCh[0];
 361	case EEP_MAC_LSW:
 362		return get_unaligned_be16(pBase->macAddr);
 363	case EEP_MAC_MID:
 364		return get_unaligned_be16(pBase->macAddr + 2);
 365	case EEP_MAC_MSW:
 366		return get_unaligned_be16(pBase->macAddr + 4);
 367	case EEP_REG_0:
 368		return pBase->regDmn[0];
 
 
 369	case EEP_OP_CAP:
 370		return pBase->deviceCap;
 371	case EEP_OP_MODE:
 372		return pBase->opCapFlags;
 373	case EEP_RF_SILENT:
 374		return pBase->rfSilent;
 375	case EEP_OB_5:
 376		return pModal[0].ob;
 377	case EEP_DB_5:
 378		return pModal[0].db;
 379	case EEP_OB_2:
 380		return pModal[1].ob;
 381	case EEP_DB_2:
 382		return pModal[1].db;
 383	case EEP_MINOR_REV:
 384		return AR5416_VER_MASK;
 385	case EEP_TX_MASK:
 386		return pBase->txMask;
 387	case EEP_RX_MASK:
 388		return pBase->rxMask;
 389	case EEP_FSTCLK_5G:
 390		return pBase->fastClk5g;
 391	case EEP_RXGAIN_TYPE:
 392		return pBase->rxGainType;
 393	case EEP_TXGAIN_TYPE:
 394		return pBase->txGainType;
 395	case EEP_OL_PWRCTRL:
 396		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
 397			return pBase->openLoopPwrCntl ? true : false;
 398		else
 399			return false;
 400	case EEP_RC_CHAIN_MASK:
 401		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
 402			return pBase->rcChainMask;
 403		else
 404			return 0;
 405	case EEP_DAC_HPWR_5G:
 406		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_20)
 407			return pBase->dacHiPwrMode_5G;
 408		else
 409			return 0;
 410	case EEP_FRAC_N_5G:
 411		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_22)
 412			return pBase->frac_n_5g;
 413		else
 414			return 0;
 415	case EEP_PWR_TABLE_OFFSET:
 416		if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_21)
 417			return pBase->pwr_table_offset;
 418		else
 419			return AR5416_PWR_TABLE_OFFSET_DB;
 420	case EEP_ANTENNA_GAIN_2G:
 421		band = 1;
 422		/* fall through */
 423	case EEP_ANTENNA_GAIN_5G:
 424		return max_t(u8, max_t(u8,
 425			pModal[band].antennaGainCh[0],
 426			pModal[band].antennaGainCh[1]),
 427			pModal[band].antennaGainCh[2]);
 428	default:
 429		return 0;
 430	}
 431}
 432
 433static void ath9k_hw_def_set_gain(struct ath_hw *ah,
 434				  struct modal_eep_header *pModal,
 435				  struct ar5416_eeprom_def *eep,
 436				  u8 txRxAttenLocal, int regChainOffset, int i)
 437{
 438	ENABLE_REG_RMW_BUFFER(ah);
 439	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_3) {
 440		txRxAttenLocal = pModal->txRxAttenCh[i];
 441
 442		if (AR_SREV_9280_20_OR_LATER(ah)) {
 443			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 444			      AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
 445			      pModal->bswMargin[i]);
 446			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 447			      AR_PHY_GAIN_2GHZ_XATTEN1_DB,
 448			      pModal->bswAtten[i]);
 449			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 450			      AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
 451			      pModal->xatten2Margin[i]);
 452			REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 453			      AR_PHY_GAIN_2GHZ_XATTEN2_DB,
 454			      pModal->xatten2Db[i]);
 455		} else {
 456			REG_RMW(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 457				SM(pModal-> bswMargin[i], AR_PHY_GAIN_2GHZ_BSW_MARGIN),
 458				AR_PHY_GAIN_2GHZ_BSW_MARGIN);
 459			REG_RMW(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 460				SM(pModal->bswAtten[i], AR_PHY_GAIN_2GHZ_BSW_ATTEN),
 461				AR_PHY_GAIN_2GHZ_BSW_ATTEN);
 
 
 
 
 462		}
 463	}
 464
 465	if (AR_SREV_9280_20_OR_LATER(ah)) {
 466		REG_RMW_FIELD(ah,
 467		      AR_PHY_RXGAIN + regChainOffset,
 468		      AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
 469		REG_RMW_FIELD(ah,
 470		      AR_PHY_RXGAIN + regChainOffset,
 471		      AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[i]);
 472	} else {
 473		REG_RMW(ah, AR_PHY_RXGAIN + regChainOffset,
 474			SM(txRxAttenLocal, AR_PHY_RXGAIN_TXRX_ATTEN),
 475			AR_PHY_RXGAIN_TXRX_ATTEN);
 476		REG_RMW(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
 477			SM(pModal->rxTxMarginCh[i], AR_PHY_GAIN_2GHZ_RXTX_MARGIN),
 478			AR_PHY_GAIN_2GHZ_RXTX_MARGIN);
 
 
 
 
 479	}
 480	REG_RMW_BUFFER_FLUSH(ah);
 481}
 482
 483static void ath9k_hw_def_set_board_values(struct ath_hw *ah,
 484					  struct ath9k_channel *chan)
 485{
 486	struct modal_eep_header *pModal;
 487	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 488	int i, regChainOffset;
 489	u8 txRxAttenLocal;
 490
 491	pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
 492	txRxAttenLocal = IS_CHAN_2GHZ(chan) ? 23 : 44;
 493
 494	REG_WRITE(ah, AR_PHY_SWITCH_COM, pModal->antCtrlCommon & 0xffff);
 495
 496	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
 497		if (AR_SREV_9280(ah)) {
 498			if (i >= 2)
 499				break;
 500		}
 501
 502		if ((ah->rxchainmask == 5 || ah->txchainmask == 5) && (i != 0))
 
 503			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
 504		else
 505			regChainOffset = i * 0x1000;
 506
 507		REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
 508			  pModal->antCtrlChain[i]);
 509
 510		REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
 511			  (REG_READ(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset) &
 512			   ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
 513			     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
 514			  SM(pModal->iqCalICh[i],
 515			     AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
 516			  SM(pModal->iqCalQCh[i],
 517			     AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
 518
 519		ath9k_hw_def_set_gain(ah, pModal, eep, txRxAttenLocal,
 520				      regChainOffset, i);
 
 521	}
 522
 523	if (AR_SREV_9280_20_OR_LATER(ah)) {
 524		if (IS_CHAN_2GHZ(chan)) {
 525			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
 526						  AR_AN_RF2G1_CH0_OB,
 527						  AR_AN_RF2G1_CH0_OB_S,
 528						  pModal->ob);
 529			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
 530						  AR_AN_RF2G1_CH0_DB,
 531						  AR_AN_RF2G1_CH0_DB_S,
 532						  pModal->db);
 533			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
 534						  AR_AN_RF2G1_CH1_OB,
 535						  AR_AN_RF2G1_CH1_OB_S,
 536						  pModal->ob_ch1);
 537			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
 538						  AR_AN_RF2G1_CH1_DB,
 539						  AR_AN_RF2G1_CH1_DB_S,
 540						  pModal->db_ch1);
 541		} else {
 542			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
 543						  AR_AN_RF5G1_CH0_OB5,
 544						  AR_AN_RF5G1_CH0_OB5_S,
 545						  pModal->ob);
 546			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
 547						  AR_AN_RF5G1_CH0_DB5,
 548						  AR_AN_RF5G1_CH0_DB5_S,
 549						  pModal->db);
 550			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
 551						  AR_AN_RF5G1_CH1_OB5,
 552						  AR_AN_RF5G1_CH1_OB5_S,
 553						  pModal->ob_ch1);
 554			ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
 555						  AR_AN_RF5G1_CH1_DB5,
 556						  AR_AN_RF5G1_CH1_DB5_S,
 557						  pModal->db_ch1);
 558		}
 559		ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
 560					  AR_AN_TOP2_XPABIAS_LVL,
 561					  AR_AN_TOP2_XPABIAS_LVL_S,
 562					  pModal->xpaBiasLvl);
 563		ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
 564					  AR_AN_TOP2_LOCALBIAS,
 565					  AR_AN_TOP2_LOCALBIAS_S,
 566					  !!(pModal->lna_ctl &
 567					     LNA_CTL_LOCAL_BIAS));
 568		REG_RMW_FIELD(ah, AR_PHY_XPA_CFG, AR_PHY_FORCE_XPA_CFG,
 569			      !!(pModal->lna_ctl & LNA_CTL_FORCE_XPA));
 570	}
 571
 572	REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
 573		      pModal->switchSettling);
 574	REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
 575		      pModal->adcDesiredSize);
 576
 577	if (!AR_SREV_9280_20_OR_LATER(ah))
 578		REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
 579			      AR_PHY_DESIRED_SZ_PGA,
 580			      pModal->pgaDesiredSize);
 581
 582	REG_WRITE(ah, AR_PHY_RF_CTL4,
 583		  SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF)
 584		  | SM(pModal->txEndToXpaOff,
 585		       AR_PHY_RF_CTL4_TX_END_XPAB_OFF)
 586		  | SM(pModal->txFrameToXpaOn,
 587		       AR_PHY_RF_CTL4_FRAME_XPAA_ON)
 588		  | SM(pModal->txFrameToXpaOn,
 589		       AR_PHY_RF_CTL4_FRAME_XPAB_ON));
 590
 591	REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
 592		      pModal->txEndToRxOn);
 593
 594	if (AR_SREV_9280_20_OR_LATER(ah)) {
 595		REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
 596			      pModal->thresh62);
 597		REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0,
 598			      AR_PHY_EXT_CCA0_THRESH62,
 599			      pModal->thresh62);
 600	} else {
 601		REG_RMW_FIELD(ah, AR_PHY_CCA, AR_PHY_CCA_THRESH62,
 602			      pModal->thresh62);
 603		REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
 604			      AR_PHY_EXT_CCA_THRESH62,
 605			      pModal->thresh62);
 606	}
 607
 608	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_2) {
 609		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
 610			      AR_PHY_TX_END_DATA_START,
 611			      pModal->txFrameToDataStart);
 612		REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
 613			      pModal->txFrameToPaOn);
 614	}
 615
 616	if (AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_3) {
 617		if (IS_CHAN_HT40(chan))
 618			REG_RMW_FIELD(ah, AR_PHY_SETTLING,
 619				      AR_PHY_SETTLING_SWITCH,
 620				      pModal->swSettleHt40);
 621	}
 622
 623	if (AR_SREV_9280_20_OR_LATER(ah) &&
 624	    AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_19)
 625		REG_RMW_FIELD(ah, AR_PHY_CCK_TX_CTRL,
 626			      AR_PHY_CCK_TX_CTRL_TX_DAC_SCALE_CCK,
 627			      pModal->miscBits);
 628
 629
 630	if (AR_SREV_9280_20(ah) && AR5416_VER_MASK >= AR5416_EEP_MINOR_VER_20) {
 631		if (IS_CHAN_2GHZ(chan))
 632			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE,
 633					eep->baseEepHeader.dacLpMode);
 634		else if (eep->baseEepHeader.dacHiPwrMode_5G)
 635			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE, 0);
 636		else
 637			REG_RMW_FIELD(ah, AR_AN_TOP1, AR_AN_TOP1_DACIPMODE,
 638				      eep->baseEepHeader.dacLpMode);
 639
 640		udelay(100);
 641
 642		REG_RMW_FIELD(ah, AR_PHY_FRAME_CTL, AR_PHY_FRAME_CTL_TX_CLIP,
 643			      pModal->miscBits >> 2);
 644
 645		REG_RMW_FIELD(ah, AR_PHY_TX_PWRCTRL9,
 646			      AR_PHY_TX_DESIRED_SCALE_CCK,
 647			      eep->baseEepHeader.desiredScaleCCK);
 648	}
 649}
 650
 651static void ath9k_hw_def_set_addac(struct ath_hw *ah,
 652				   struct ath9k_channel *chan)
 653{
 654#define XPA_LVL_FREQ(cnt) (pModal->xpaBiasLvlFreq[cnt])
 655	struct modal_eep_header *pModal;
 656	struct ar5416_eeprom_def *eep = &ah->eeprom.def;
 657	u8 biaslevel;
 658
 659	if (ah->hw_version.macVersion != AR_SREV_VERSION_9160)
 660		return;
 661
 662	if (ah->eep_ops->get_eeprom_rev(ah) < AR5416_EEP_MINOR_VER_7)
 663		return;
 664
 665	pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
 666
 667	if (pModal->xpaBiasLvl != 0xff) {
 668		biaslevel = pModal->xpaBiasLvl;
 669	} else {
 670		u16 resetFreqBin, freqBin, freqCount = 0;
 671		struct chan_centers centers;
 672
 673		ath9k_hw_get_channel_centers(ah, chan, &centers);
 674
 675		resetFreqBin = FREQ2FBIN(centers.synth_center,
 676					 IS_CHAN_2GHZ(chan));
 677		freqBin = XPA_LVL_FREQ(0) & 0xff;
 678		biaslevel = (u8) (XPA_LVL_FREQ(0) >> 14);
 679
 680		freqCount++;
 681
 682		while (freqCount < 3) {
 683			if (XPA_LVL_FREQ(freqCount) == 0x0)
 684				break;
 685
 686			freqBin = XPA_LVL_FREQ(freqCount) & 0xff;
 687			if (resetFreqBin >= freqBin)
 688				biaslevel = (u8)(XPA_LVL_FREQ(freqCount) >> 14);
 689			else
 690				break;
 691			freqCount++;
 692		}
 693	}
 694
 695	if (IS_CHAN_2GHZ(chan)) {
 696		INI_RA(&ah->iniAddac, 7, 1) = (INI_RA(&ah->iniAddac,
 697					7, 1) & (~0x18)) | biaslevel << 3;
 698	} else {
 699		INI_RA(&ah->iniAddac, 6, 1) = (INI_RA(&ah->iniAddac,
 700					6, 1) & (~0xc0)) | biaslevel << 6;
 701	}
 702#undef XPA_LVL_FREQ
 703}
 704
 705static int16_t ath9k_change_gain_boundary_setting(struct ath_hw *ah,
 706				u16 *gb,
 707				u16 numXpdGain,
 708				u16 pdGainOverlap_t2,
 709				int8_t pwr_table_offset,
 710				int16_t *diff)
 711
 712{
 713	u16 k;
 714
 715	/* Prior to writing the boundaries or the pdadc vs. power table
 716	 * into the chip registers the default starting point on the pdadc
 717	 * vs. power table needs to be checked and the curve boundaries
 718	 * adjusted accordingly
 719	 */
 720	if (AR_SREV_9280_20_OR_LATER(ah)) {
 721		u16 gb_limit;
 722
 723		if (AR5416_PWR_TABLE_OFFSET_DB != pwr_table_offset) {
 724			/* get the difference in dB */
 725			*diff = (u16)(pwr_table_offset - AR5416_PWR_TABLE_OFFSET_DB);
 726			/* get the number of half dB steps */
 727			*diff *= 2;
 728			/* change the original gain boundary settings
 729			 * by the number of half dB steps
 730			 */
 731			for (k = 0; k < numXpdGain; k++)
 732				gb[k] = (u16)(gb[k] - *diff);
 733		}
 734		/* Because of a hardware limitation, ensure the gain boundary
 735		 * is not larger than (63 - overlap)
 736		 */
 737		gb_limit = (u16)(MAX_RATE_POWER - pdGainOverlap_t2);
 738
 739		for (k = 0; k < numXpdGain; k++)
 740			gb[k] = (u16)min(gb_limit, gb[k]);
 741	}
 742
 743	return *diff;
 744}
 745
 746static void ath9k_adjust_pdadc_values(struct ath_hw *ah,
 747				      int8_t pwr_table_offset,
 748				      int16_t diff,
 749				      u8 *pdadcValues)
 750{
 751#define NUM_PDADC(diff) (AR5416_NUM_PDADC_VALUES - diff)
 752	u16 k;
 753
 754	/* If this is a board that has a pwrTableOffset that differs from
 755	 * the default AR5416_PWR_TABLE_OFFSET_DB then the start of the
 756	 * pdadc vs pwr table needs to be adjusted prior to writing to the
 757	 * chip.
 758	 */
 759	if (AR_SREV_9280_20_OR_LATER(ah)) {
 760		if (AR5416_PWR_TABLE_OFFSET_DB != pwr_table_offset) {
 761			/* shift the table to start at the new offset */
 762			for (k = 0; k < (u16)NUM_PDADC(diff); k++ ) {
 763				pdadcValues[k] = pdadcValues[k + diff];
 764			}
 765
 766			/* fill the back of the table */
 767			for (k = (u16)NUM_PDADC(diff); k < NUM_PDADC(0); k++) {
 768				pdadcValues[k] = pdadcValues[NUM_PDADC(diff)];
 769			}
 770		}
 771	}
 772#undef NUM_PDADC
 773}
 774
 775static void ath9k_hw_set_def_power_cal_table(struct ath_hw *ah,
 776				  struct ath9k_channel *chan)
 
 777{
 778#define SM_PD_GAIN(x) SM(0x38, AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_##x)
 779#define SM_PDGAIN_B(x, y) \
 780		SM((gainBoundaries[x]), AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_##y)
 781	struct ath_common *common = ath9k_hw_common(ah);
 782	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
 783	struct cal_data_per_freq *pRawDataset;
 784	u8 *pCalBChans = NULL;
 785	u16 pdGainOverlap_t2;
 786	static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
 787	u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
 788	u16 numPiers, i, j;
 789	int16_t diff = 0;
 790	u16 numXpdGain, xpdMask;
 791	u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
 792	u32 reg32, regOffset, regChainOffset;
 793	int16_t modalIdx;
 794	int8_t pwr_table_offset;
 795
 796	modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
 797	xpdMask = pEepData->modalHeader[modalIdx].xpdGain;
 798
 799	pwr_table_offset = ah->eep_ops->get_eeprom(ah, EEP_PWR_TABLE_OFFSET);
 800
 801	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
 802	    AR5416_EEP_MINOR_VER_2) {
 803		pdGainOverlap_t2 =
 804			pEepData->modalHeader[modalIdx].pdGainOverlap;
 805	} else {
 806		pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
 807					    AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
 808	}
 809
 810	if (IS_CHAN_2GHZ(chan)) {
 811		pCalBChans = pEepData->calFreqPier2G;
 812		numPiers = AR5416_NUM_2G_CAL_PIERS;
 813	} else {
 814		pCalBChans = pEepData->calFreqPier5G;
 815		numPiers = AR5416_NUM_5G_CAL_PIERS;
 816	}
 817
 818	if (OLC_FOR_AR9280_20_LATER && IS_CHAN_2GHZ(chan)) {
 819		pRawDataset = pEepData->calPierData2G[0];
 820		ah->initPDADC = ((struct calDataPerFreqOpLoop *)
 821				 pRawDataset)->vpdPdg[0][0];
 822	}
 823
 824	numXpdGain = 0;
 825
 826	for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
 827		if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
 828			if (numXpdGain >= AR5416_NUM_PD_GAINS)
 829				break;
 830			xpdGainValues[numXpdGain] =
 831				(u16)(AR5416_PD_GAINS_IN_MASK - i);
 832			numXpdGain++;
 833		}
 834	}
 835
 836	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
 837		      (numXpdGain - 1) & 0x3);
 838	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
 839		      xpdGainValues[0]);
 840	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
 841		      xpdGainValues[1]);
 842	REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
 843		      xpdGainValues[2]);
 844
 845	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
 846		if ((ah->rxchainmask == 5 || ah->txchainmask == 5) &&
 
 847		    (i != 0)) {
 848			regChainOffset = (i == 1) ? 0x2000 : 0x1000;
 849		} else
 850			regChainOffset = i * 0x1000;
 851
 852		if (pEepData->baseEepHeader.txMask & (1 << i)) {
 853			if (IS_CHAN_2GHZ(chan))
 854				pRawDataset = pEepData->calPierData2G[i];
 855			else
 856				pRawDataset = pEepData->calPierData5G[i];
 857
 858
 859			if (OLC_FOR_AR9280_20_LATER) {
 860				u8 pcdacIdx;
 861				u8 txPower;
 862
 863				ath9k_get_txgain_index(ah, chan,
 864				(struct calDataPerFreqOpLoop *)pRawDataset,
 865				pCalBChans, numPiers, &txPower, &pcdacIdx);
 866				ath9k_olc_get_pdadcs(ah, pcdacIdx,
 867						     txPower/2, pdadcValues);
 868			} else {
 869				ath9k_hw_get_gain_boundaries_pdadcs(ah,
 870							chan, pRawDataset,
 871							pCalBChans, numPiers,
 872							pdGainOverlap_t2,
 873							gainBoundaries,
 874							pdadcValues,
 875							numXpdGain);
 876			}
 877
 878			diff = ath9k_change_gain_boundary_setting(ah,
 879							   gainBoundaries,
 880							   numXpdGain,
 881							   pdGainOverlap_t2,
 882							   pwr_table_offset,
 883							   &diff);
 884
 885			ENABLE_REGWRITE_BUFFER(ah);
 886
 887			if (OLC_FOR_AR9280_20_LATER) {
 888				REG_WRITE(ah,
 889					AR_PHY_TPCRG5 + regChainOffset,
 890					SM(0x6,
 891					AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
 892					SM_PD_GAIN(1) | SM_PD_GAIN(2) |
 893					SM_PD_GAIN(3) | SM_PD_GAIN(4));
 894			} else {
 895				REG_WRITE(ah,
 896					AR_PHY_TPCRG5 + regChainOffset,
 897					SM(pdGainOverlap_t2,
 898					AR_PHY_TPCRG5_PD_GAIN_OVERLAP)|
 899					SM_PDGAIN_B(0, 1) |
 900					SM_PDGAIN_B(1, 2) |
 901					SM_PDGAIN_B(2, 3) |
 902					SM_PDGAIN_B(3, 4));
 
 
 903			}
 904
 
 905			ath9k_adjust_pdadc_values(ah, pwr_table_offset,
 906						  diff, pdadcValues);
 907
 908			regOffset = AR_PHY_BASE + (672 << 2) + regChainOffset;
 909			for (j = 0; j < 32; j++) {
 910				reg32 = get_unaligned_le32(&pdadcValues[4 * j]);
 911				REG_WRITE(ah, regOffset, reg32);
 912
 913				ath_dbg(common, EEPROM,
 914					"PDADC (%d,%4x): %4.4x %8.8x\n",
 915					i, regChainOffset, regOffset,
 916					reg32);
 917				ath_dbg(common, EEPROM,
 918					"PDADC: Chain %d | PDADC %3d Value %3d | PDADC %3d Value %3d | PDADC %3d Value %3d | PDADC %3d Value %3d |\n",
 
 
 
 919					i, 4 * j, pdadcValues[4 * j],
 920					4 * j + 1, pdadcValues[4 * j + 1],
 921					4 * j + 2, pdadcValues[4 * j + 2],
 922					4 * j + 3, pdadcValues[4 * j + 3]);
 923
 924				regOffset += 4;
 925			}
 926			REGWRITE_BUFFER_FLUSH(ah);
 927		}
 928	}
 929
 
 930#undef SM_PD_GAIN
 931#undef SM_PDGAIN_B
 932}
 933
 934static void ath9k_hw_set_def_power_per_rate_table(struct ath_hw *ah,
 935						  struct ath9k_channel *chan,
 936						  int16_t *ratesArray,
 937						  u16 cfgCtl,
 938						  u16 antenna_reduction,
 
 939						  u16 powerLimit)
 940{
 
 
 
 
 941	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
 942	u16 twiceMaxEdgePower;
 
 
 
 943	int i;
 
 944	struct cal_ctl_data *rep;
 945	struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
 946		0, { 0, 0, 0, 0}
 947	};
 948	struct cal_target_power_leg targetPowerOfdmExt = {
 949		0, { 0, 0, 0, 0} }, targetPowerCckExt = {
 950		0, { 0, 0, 0, 0 }
 951	};
 952	struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
 953		0, {0, 0, 0, 0}
 954	};
 955	u16 scaledPower = 0, minCtlPower;
 956	static const u16 ctlModesFor11a[] = {
 957		CTL_11A, CTL_5GHT20, CTL_11A_EXT, CTL_5GHT40
 958	};
 959	static const u16 ctlModesFor11g[] = {
 960		CTL_11B, CTL_11G, CTL_2GHT20,
 961		CTL_11B_EXT, CTL_11G_EXT, CTL_2GHT40
 962	};
 963	u16 numCtlModes;
 964	const u16 *pCtlMode;
 965	u16 ctlMode, freq;
 966	struct chan_centers centers;
 967	int tx_chainmask;
 968	u16 twiceMinEdgePower;
 969
 970	tx_chainmask = ah->txchainmask;
 971
 972	ath9k_hw_get_channel_centers(ah, chan, &centers);
 973
 974	scaledPower = ath9k_hw_get_scaled_power(ah, powerLimit,
 975						antenna_reduction);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 976
 977	if (IS_CHAN_2GHZ(chan)) {
 978		numCtlModes = ARRAY_SIZE(ctlModesFor11g) -
 979			SUB_NUM_CTL_MODES_AT_2G_40;
 980		pCtlMode = ctlModesFor11g;
 981
 982		ath9k_hw_get_legacy_target_powers(ah, chan,
 983			pEepData->calTargetPowerCck,
 984			AR5416_NUM_2G_CCK_TARGET_POWERS,
 985			&targetPowerCck, 4, false);
 986		ath9k_hw_get_legacy_target_powers(ah, chan,
 987			pEepData->calTargetPower2G,
 988			AR5416_NUM_2G_20_TARGET_POWERS,
 989			&targetPowerOfdm, 4, false);
 990		ath9k_hw_get_target_powers(ah, chan,
 991			pEepData->calTargetPower2GHT20,
 992			AR5416_NUM_2G_20_TARGET_POWERS,
 993			&targetPowerHt20, 8, false);
 994
 995		if (IS_CHAN_HT40(chan)) {
 996			numCtlModes = ARRAY_SIZE(ctlModesFor11g);
 997			ath9k_hw_get_target_powers(ah, chan,
 998				pEepData->calTargetPower2GHT40,
 999				AR5416_NUM_2G_40_TARGET_POWERS,
1000				&targetPowerHt40, 8, true);
1001			ath9k_hw_get_legacy_target_powers(ah, chan,
1002				pEepData->calTargetPowerCck,
1003				AR5416_NUM_2G_CCK_TARGET_POWERS,
1004				&targetPowerCckExt, 4, true);
1005			ath9k_hw_get_legacy_target_powers(ah, chan,
1006				pEepData->calTargetPower2G,
1007				AR5416_NUM_2G_20_TARGET_POWERS,
1008				&targetPowerOfdmExt, 4, true);
1009		}
1010	} else {
1011		numCtlModes = ARRAY_SIZE(ctlModesFor11a) -
1012			SUB_NUM_CTL_MODES_AT_5G_40;
1013		pCtlMode = ctlModesFor11a;
1014
1015		ath9k_hw_get_legacy_target_powers(ah, chan,
1016			pEepData->calTargetPower5G,
1017			AR5416_NUM_5G_20_TARGET_POWERS,
1018			&targetPowerOfdm, 4, false);
1019		ath9k_hw_get_target_powers(ah, chan,
1020			pEepData->calTargetPower5GHT20,
1021			AR5416_NUM_5G_20_TARGET_POWERS,
1022			&targetPowerHt20, 8, false);
1023
1024		if (IS_CHAN_HT40(chan)) {
1025			numCtlModes = ARRAY_SIZE(ctlModesFor11a);
1026			ath9k_hw_get_target_powers(ah, chan,
1027				pEepData->calTargetPower5GHT40,
1028				AR5416_NUM_5G_40_TARGET_POWERS,
1029				&targetPowerHt40, 8, true);
1030			ath9k_hw_get_legacy_target_powers(ah, chan,
1031				pEepData->calTargetPower5G,
1032				AR5416_NUM_5G_20_TARGET_POWERS,
1033				&targetPowerOfdmExt, 4, true);
1034		}
1035	}
1036
1037	for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
1038		bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
1039			(pCtlMode[ctlMode] == CTL_2GHT40);
1040		if (isHt40CtlMode)
1041			freq = centers.synth_center;
1042		else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
1043			freq = centers.ext_center;
1044		else
1045			freq = centers.ctl_center;
1046
1047		twiceMaxEdgePower = MAX_RATE_POWER;
 
 
1048
1049		for (i = 0; (i < AR5416_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {
1050			if ((((cfgCtl & ~CTL_MODE_M) |
1051			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
1052			     pEepData->ctlIndex[i]) ||
1053			    (((cfgCtl & ~CTL_MODE_M) |
1054			      (pCtlMode[ctlMode] & CTL_MODE_M)) ==
1055			     ((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
1056				rep = &(pEepData->ctlData[i]);
1057
1058				twiceMinEdgePower = ath9k_hw_get_max_edge_power(freq,
1059				rep->ctlEdges[ar5416_get_ntxchains(tx_chainmask) - 1],
1060				IS_CHAN_2GHZ(chan), AR5416_NUM_BAND_EDGES);
1061
1062				if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
1063					twiceMaxEdgePower = min(twiceMaxEdgePower,
1064								twiceMinEdgePower);
1065				} else {
1066					twiceMaxEdgePower = twiceMinEdgePower;
1067					break;
1068				}
1069			}
1070		}
1071
1072		minCtlPower = min(twiceMaxEdgePower, scaledPower);
1073
1074		switch (pCtlMode[ctlMode]) {
1075		case CTL_11B:
1076			for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x); i++) {
1077				targetPowerCck.tPow2x[i] =
1078					min((u16)targetPowerCck.tPow2x[i],
1079					    minCtlPower);
1080			}
1081			break;
1082		case CTL_11A:
1083		case CTL_11G:
1084			for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x); i++) {
1085				targetPowerOfdm.tPow2x[i] =
1086					min((u16)targetPowerOfdm.tPow2x[i],
1087					    minCtlPower);
1088			}
1089			break;
1090		case CTL_5GHT20:
1091		case CTL_2GHT20:
1092			for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++) {
1093				targetPowerHt20.tPow2x[i] =
1094					min((u16)targetPowerHt20.tPow2x[i],
1095					    minCtlPower);
1096			}
1097			break;
1098		case CTL_11B_EXT:
1099			targetPowerCckExt.tPow2x[0] = min((u16)
1100					targetPowerCckExt.tPow2x[0],
1101					minCtlPower);
1102			break;
1103		case CTL_11A_EXT:
1104		case CTL_11G_EXT:
1105			targetPowerOfdmExt.tPow2x[0] = min((u16)
1106					targetPowerOfdmExt.tPow2x[0],
1107					minCtlPower);
1108			break;
1109		case CTL_5GHT40:
1110		case CTL_2GHT40:
1111			for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
1112				targetPowerHt40.tPow2x[i] =
1113					min((u16)targetPowerHt40.tPow2x[i],
1114					    minCtlPower);
1115			}
1116			break;
1117		default:
1118			break;
1119		}
1120	}
1121
1122	ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
1123		ratesArray[rate18mb] = ratesArray[rate24mb] =
1124		targetPowerOfdm.tPow2x[0];
1125	ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
1126	ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
1127	ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
1128	ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
1129
1130	for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
1131		ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
1132
1133	if (IS_CHAN_2GHZ(chan)) {
1134		ratesArray[rate1l] = targetPowerCck.tPow2x[0];
1135		ratesArray[rate2s] = ratesArray[rate2l] =
1136			targetPowerCck.tPow2x[1];
1137		ratesArray[rate5_5s] = ratesArray[rate5_5l] =
1138			targetPowerCck.tPow2x[2];
1139		ratesArray[rate11s] = ratesArray[rate11l] =
1140			targetPowerCck.tPow2x[3];
1141	}
1142	if (IS_CHAN_HT40(chan)) {
1143		for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
1144			ratesArray[rateHt40_0 + i] =
1145				targetPowerHt40.tPow2x[i];
1146		}
1147		ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
1148		ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
1149		ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
1150		if (IS_CHAN_2GHZ(chan)) {
1151			ratesArray[rateExtCck] =
1152				targetPowerCckExt.tPow2x[0];
1153		}
1154	}
1155}
1156
1157static void ath9k_hw_def_set_txpower(struct ath_hw *ah,
1158				    struct ath9k_channel *chan,
1159				    u16 cfgCtl,
1160				    u8 twiceAntennaReduction,
 
1161				    u8 powerLimit, bool test)
1162{
1163#define RT_AR_DELTA(x) (ratesArray[x] - cck_ofdm_delta)
1164	struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
1165	struct ar5416_eeprom_def *pEepData = &ah->eeprom.def;
1166	struct modal_eep_header *pModal =
1167		&(pEepData->modalHeader[IS_CHAN_2GHZ(chan)]);
1168	int16_t ratesArray[Ar5416RateSize];
 
1169	u8 ht40PowerIncForPdadc = 2;
1170	int i, cck_ofdm_delta = 0;
1171
1172	memset(ratesArray, 0, sizeof(ratesArray));
1173
1174	if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
1175	    AR5416_EEP_MINOR_VER_2) {
1176		ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
1177	}
1178
1179	ath9k_hw_set_def_power_per_rate_table(ah, chan,
1180					       &ratesArray[0], cfgCtl,
1181					       twiceAntennaReduction,
 
1182					       powerLimit);
1183
1184	ath9k_hw_set_def_power_cal_table(ah, chan);
1185
1186	regulatory->max_power_level = 0;
1187	for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
 
1188		if (ratesArray[i] > MAX_RATE_POWER)
1189			ratesArray[i] = MAX_RATE_POWER;
1190		if (ratesArray[i] > regulatory->max_power_level)
1191			regulatory->max_power_level = ratesArray[i];
1192	}
1193
1194	ath9k_hw_update_regulatory_maxpower(ah);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1195
1196	if (test)
1197		return;
1198
1199	if (AR_SREV_9280_20_OR_LATER(ah)) {
1200		for (i = 0; i < Ar5416RateSize; i++) {
1201			int8_t pwr_table_offset;
1202
1203			pwr_table_offset = ah->eep_ops->get_eeprom(ah,
1204							EEP_PWR_TABLE_OFFSET);
1205			ratesArray[i] -= pwr_table_offset * 2;
1206		}
1207	}
1208
1209	ENABLE_REGWRITE_BUFFER(ah);
1210
1211	REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
1212		  ATH9K_POW_SM(ratesArray[rate18mb], 24)
1213		  | ATH9K_POW_SM(ratesArray[rate12mb], 16)
1214		  | ATH9K_POW_SM(ratesArray[rate9mb], 8)
1215		  | ATH9K_POW_SM(ratesArray[rate6mb], 0));
1216	REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
1217		  ATH9K_POW_SM(ratesArray[rate54mb], 24)
1218		  | ATH9K_POW_SM(ratesArray[rate48mb], 16)
1219		  | ATH9K_POW_SM(ratesArray[rate36mb], 8)
1220		  | ATH9K_POW_SM(ratesArray[rate24mb], 0));
1221
1222	if (IS_CHAN_2GHZ(chan)) {
1223		if (OLC_FOR_AR9280_20_LATER) {
1224			cck_ofdm_delta = 2;
1225			REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
1226				ATH9K_POW_SM(RT_AR_DELTA(rate2s), 24)
1227				| ATH9K_POW_SM(RT_AR_DELTA(rate2l), 16)
1228				| ATH9K_POW_SM(ratesArray[rateXr], 8)
1229				| ATH9K_POW_SM(RT_AR_DELTA(rate1l), 0));
1230			REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
1231				ATH9K_POW_SM(RT_AR_DELTA(rate11s), 24)
1232				| ATH9K_POW_SM(RT_AR_DELTA(rate11l), 16)
1233				| ATH9K_POW_SM(RT_AR_DELTA(rate5_5s), 8)
1234				| ATH9K_POW_SM(RT_AR_DELTA(rate5_5l), 0));
1235		} else {
1236			REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
1237				ATH9K_POW_SM(ratesArray[rate2s], 24)
1238				| ATH9K_POW_SM(ratesArray[rate2l], 16)
1239				| ATH9K_POW_SM(ratesArray[rateXr], 8)
1240				| ATH9K_POW_SM(ratesArray[rate1l], 0));
1241			REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
1242				ATH9K_POW_SM(ratesArray[rate11s], 24)
1243				| ATH9K_POW_SM(ratesArray[rate11l], 16)
1244				| ATH9K_POW_SM(ratesArray[rate5_5s], 8)
1245				| ATH9K_POW_SM(ratesArray[rate5_5l], 0));
1246		}
1247	}
1248
1249	REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
1250		  ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
1251		  | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
1252		  | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
1253		  | ATH9K_POW_SM(ratesArray[rateHt20_0], 0));
1254	REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
1255		  ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
1256		  | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
1257		  | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
1258		  | ATH9K_POW_SM(ratesArray[rateHt20_4], 0));
1259
1260	if (IS_CHAN_HT40(chan)) {
1261		REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
1262			  ATH9K_POW_SM(ratesArray[rateHt40_3] +
1263				       ht40PowerIncForPdadc, 24)
1264			  | ATH9K_POW_SM(ratesArray[rateHt40_2] +
1265					 ht40PowerIncForPdadc, 16)
1266			  | ATH9K_POW_SM(ratesArray[rateHt40_1] +
1267					 ht40PowerIncForPdadc, 8)
1268			  | ATH9K_POW_SM(ratesArray[rateHt40_0] +
1269					 ht40PowerIncForPdadc, 0));
1270		REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
1271			  ATH9K_POW_SM(ratesArray[rateHt40_7] +
1272				       ht40PowerIncForPdadc, 24)
1273			  | ATH9K_POW_SM(ratesArray[rateHt40_6] +
1274					 ht40PowerIncForPdadc, 16)
1275			  | ATH9K_POW_SM(ratesArray[rateHt40_5] +
1276					 ht40PowerIncForPdadc, 8)
1277			  | ATH9K_POW_SM(ratesArray[rateHt40_4] +
1278					 ht40PowerIncForPdadc, 0));
1279		if (OLC_FOR_AR9280_20_LATER) {
1280			REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
1281				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
1282				| ATH9K_POW_SM(RT_AR_DELTA(rateExtCck), 16)
1283				| ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
1284				| ATH9K_POW_SM(RT_AR_DELTA(rateDupCck), 0));
1285		} else {
1286			REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
1287				ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
1288				| ATH9K_POW_SM(ratesArray[rateExtCck], 16)
1289				| ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
1290				| ATH9K_POW_SM(ratesArray[rateDupCck], 0));
1291		}
1292	}
1293
1294	REG_WRITE(ah, AR_PHY_POWER_TX_SUB,
1295		  ATH9K_POW_SM(pModal->pwrDecreaseFor3Chain, 6)
1296		  | ATH9K_POW_SM(pModal->pwrDecreaseFor2Chain, 0));
1297
1298	/* TPC initializations */
1299	if (ah->tpc_enabled) {
1300		int ht40_delta;
1301
1302		ht40_delta = (IS_CHAN_HT40(chan)) ? ht40PowerIncForPdadc : 0;
1303		ar5008_hw_init_rate_txpower(ah, ratesArray, chan, ht40_delta);
1304		/* Enable TPC */
1305		REG_WRITE(ah, AR_PHY_POWER_TX_RATE_MAX,
1306			MAX_RATE_POWER | AR_PHY_POWER_TX_RATE_MAX_TPC_ENABLE);
1307	} else {
1308		/* Disable TPC */
1309		REG_WRITE(ah, AR_PHY_POWER_TX_RATE_MAX, MAX_RATE_POWER);
1310	}
1311
1312	REGWRITE_BUFFER_FLUSH(ah);
1313}
1314
1315static u16 ath9k_hw_def_get_spur_channel(struct ath_hw *ah, u16 i, bool is2GHz)
1316{
1317	return ah->eeprom.def.modalHeader[is2GHz].spurChans[i].spurChan;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1318}
1319
1320const struct eeprom_ops eep_def_ops = {
1321	.check_eeprom		= ath9k_hw_def_check_eeprom,
1322	.get_eeprom		= ath9k_hw_def_get_eeprom,
1323	.fill_eeprom		= ath9k_hw_def_fill_eeprom,
1324	.dump_eeprom		= ath9k_hw_def_dump_eeprom,
1325	.get_eeprom_ver		= ath9k_hw_def_get_eeprom_ver,
1326	.get_eeprom_rev		= ath9k_hw_def_get_eeprom_rev,
1327	.set_board_values	= ath9k_hw_def_set_board_values,
1328	.set_addac		= ath9k_hw_def_set_addac,
1329	.set_txpower		= ath9k_hw_def_set_txpower,
1330	.get_spur_channel	= ath9k_hw_def_get_spur_channel
1331};