phy.c 10.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2008-2009 Atheros Communications Inc.
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"
18 19

void
20
ath9k_hw_write_regs(struct ath_hw *ah, u32 modesIndex, u32 freqIndex,
21 22
		    int regWrites)
{
23
	REG_WRITE_ARRAY(&ah->iniBB_RfGain, freqIndex, regWrites);
24 25 26
}

bool
27
ath9k_hw_set_channel(struct ath_hw *ah, struct ath9k_channel *chan)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
	u32 channelSel = 0;
	u32 bModeSynth = 0;
	u32 aModeRefSel = 0;
	u32 reg32 = 0;
	u16 freq;
	struct chan_centers centers;

	ath9k_hw_get_channel_centers(ah, chan, &centers);
	freq = centers.synth_center;

	if (freq < 4800) {
		u32 txctl;

		if (((freq - 2192) % 5) == 0) {
			channelSel = ((freq - 672) * 2 - 3040) / 10;
			bModeSynth = 0;
		} else if (((freq - 2224) % 5) == 0) {
			channelSel = ((freq - 704) * 2 - 3040) / 10;
			bModeSynth = 1;
		} else {
S
Sujith 已提交
49
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
50
				"Invalid channel %u MHz\n", freq);
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
			return false;
		}

		channelSel = (channelSel << 2) & 0xff;
		channelSel = ath9k_hw_reverse_bits(channelSel, 8);

		txctl = REG_READ(ah, AR_PHY_CCK_TX_CTRL);
		if (freq == 2484) {

			REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
				  txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
		} else {
			REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
				  txctl & ~AR_PHY_CCK_TX_CTRL_JAPAN);
		}

	} else if ((freq % 20) == 0 && freq >= 5120) {
		channelSel =
		    ath9k_hw_reverse_bits(((freq - 4800) / 20 << 2), 8);
		aModeRefSel = ath9k_hw_reverse_bits(1, 2);
	} else if ((freq % 10) == 0) {
		channelSel =
		    ath9k_hw_reverse_bits(((freq - 4800) / 10 << 1), 8);
		if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah))
			aModeRefSel = ath9k_hw_reverse_bits(2, 2);
		else
			aModeRefSel = ath9k_hw_reverse_bits(1, 2);
	} else if ((freq % 5) == 0) {
		channelSel = ath9k_hw_reverse_bits((freq - 4800) / 5, 8);
		aModeRefSel = ath9k_hw_reverse_bits(1, 2);
	} else {
S
Sujith 已提交
82
		DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
83
			"Invalid channel %u MHz\n", freq);
84 85 86 87 88 89 90 91 92
		return false;
	}

	reg32 =
	    (channelSel << 8) | (aModeRefSel << 2) | (bModeSynth << 1) |
	    (1 << 5) | 0x1;

	REG_WRITE(ah, AR_PHY(0x37), reg32);

93 94
	ah->curchan = chan;
	ah->curchan_rad_index = -1;
95 96 97 98

	return true;
}

99 100
void ath9k_hw_ar9280_set_channel(struct ath_hw *ah,
				 struct ath9k_channel *chan)
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
{
	u16 bMode, fracMode, aModeRefSel = 0;
	u32 freq, ndiv, channelSel = 0, channelFrac = 0, reg32 = 0;
	struct chan_centers centers;
	u32 refDivA = 24;

	ath9k_hw_get_channel_centers(ah, chan, &centers);
	freq = centers.synth_center;

	reg32 = REG_READ(ah, AR_PHY_SYNTH_CONTROL);
	reg32 &= 0xc0000000;

	if (freq < 4800) {
		u32 txctl;

		bMode = 1;
		fracMode = 1;
		aModeRefSel = 0;
		channelSel = (freq * 0x10000) / 15;

		txctl = REG_READ(ah, AR_PHY_CCK_TX_CTRL);
		if (freq == 2484) {

			REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
				  txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
		} else {
			REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
				  txctl & ~AR_PHY_CCK_TX_CTRL_JAPAN);
		}
	} else {
		bMode = 0;
		fracMode = 0;

S
Sujith 已提交
134 135 136 137 138 139 140 141 142 143 144
		switch(ah->eep_ops->get_eeprom(ah, EEP_FRAC_N_5G)) {
		case 0:
			if ((freq % 20) == 0) {
				aModeRefSel = 3;
			} else if ((freq % 10) == 0) {
				aModeRefSel = 2;
			}
			if (aModeRefSel)
				break;
		case 1:
		default:
145 146 147 148 149 150 151
			aModeRefSel = 0;
			fracMode = 1;
			refDivA = 1;
			channelSel = (freq * 0x8000) / 15;

			REG_RMW_FIELD(ah, AR_AN_SYNTH9,
				      AR_AN_SYNTH9_REFDIVA, refDivA);
S
Sujith 已提交
152

153
		}
S
Sujith 已提交
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168
		if (!fracMode) {
			ndiv = (freq * (refDivA >> aModeRefSel)) / 60;
			channelSel = ndiv & 0x1ff;
			channelFrac = (ndiv & 0xfffffe00) * 2;
			channelSel = (channelSel << 17) | channelFrac;
		}
	}

	reg32 = reg32 |
	    (bMode << 29) |
	    (fracMode << 28) | (aModeRefSel << 26) | (channelSel);

	REG_WRITE(ah, AR_PHY_SYNTH_CONTROL, reg32);

169 170
	ah->curchan = chan;
	ah->curchan_rad_index = -1;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
}

static void
ath9k_phy_modify_rx_buffer(u32 *rfBuf, u32 reg32,
			   u32 numBits, u32 firstBit,
			   u32 column)
{
	u32 tmp32, mask, arrayEntry, lastBit;
	int32_t bitPosition, bitsLeft;

	tmp32 = ath9k_hw_reverse_bits(reg32, numBits);
	arrayEntry = (firstBit - 1) / 8;
	bitPosition = (firstBit - 1) % 8;
	bitsLeft = numBits;
	while (bitsLeft > 0) {
		lastBit = (bitPosition + bitsLeft > 8) ?
		    8 : bitPosition + bitsLeft;
		mask = (((1 << lastBit) - 1) ^ ((1 << bitPosition) - 1)) <<
		    (column * 8);
		rfBuf[arrayEntry] &= ~mask;
		rfBuf[arrayEntry] |= ((tmp32 << bitPosition) <<
				      (column * 8)) & mask;
		bitsLeft -= 8 - bitPosition;
		tmp32 = tmp32 >> (8 - bitPosition);
		bitPosition = 0;
		arrayEntry++;
	}
}

bool
201
ath9k_hw_set_rf_regs(struct ath_hw *ah, struct ath9k_channel *chan,
202 203 204 205 206 207 208 209 210 211
		     u16 modesIndex)
{
	u32 eepMinorRev;
	u32 ob5GHz = 0, db5GHz = 0;
	u32 ob2GHz = 0, db2GHz = 0;
	int regWrites = 0;

	if (AR_SREV_9280_10_OR_LATER(ah))
		return true;

S
Sujith 已提交
212
	eepMinorRev = ah->eep_ops->get_eeprom(ah, EEP_MINOR_REV);
213

214
	RF_BANK_SETUP(ah->analogBank0Data, &ah->iniBank0, 1);
215

216
	RF_BANK_SETUP(ah->analogBank1Data, &ah->iniBank1, 1);
217

218
	RF_BANK_SETUP(ah->analogBank2Data, &ah->iniBank2, 1);
219

220
	RF_BANK_SETUP(ah->analogBank3Data, &ah->iniBank3,
221 222 223
		      modesIndex);
	{
		int i;
224 225 226
		for (i = 0; i < ah->iniBank6TPC.ia_rows; i++) {
			ah->analogBank6Data[i] =
			    INI_RA(&ah->iniBank6TPC, i, modesIndex);
227 228 229 230 231
		}
	}

	if (eepMinorRev >= 2) {
		if (IS_CHAN_2GHZ(chan)) {
S
Sujith 已提交
232 233
			ob2GHz = ah->eep_ops->get_eeprom(ah, EEP_OB_2);
			db2GHz = ah->eep_ops->get_eeprom(ah, EEP_DB_2);
234
			ath9k_phy_modify_rx_buffer(ah->analogBank6Data,
235
						   ob2GHz, 3, 197, 0);
236
			ath9k_phy_modify_rx_buffer(ah->analogBank6Data,
237 238
						   db2GHz, 3, 194, 0);
		} else {
S
Sujith 已提交
239 240
			ob5GHz = ah->eep_ops->get_eeprom(ah, EEP_OB_5);
			db5GHz = ah->eep_ops->get_eeprom(ah, EEP_DB_5);
241
			ath9k_phy_modify_rx_buffer(ah->analogBank6Data,
242
						   ob5GHz, 3, 203, 0);
243
			ath9k_phy_modify_rx_buffer(ah->analogBank6Data,
244 245 246 247
						   db5GHz, 3, 200, 0);
		}
	}

248
	RF_BANK_SETUP(ah->analogBank7Data, &ah->iniBank7, 1);
249

250
	REG_WRITE_RF_ARRAY(&ah->iniBank0, ah->analogBank0Data,
251
			   regWrites);
252
	REG_WRITE_RF_ARRAY(&ah->iniBank1, ah->analogBank1Data,
253
			   regWrites);
254
	REG_WRITE_RF_ARRAY(&ah->iniBank2, ah->analogBank2Data,
255
			   regWrites);
256
	REG_WRITE_RF_ARRAY(&ah->iniBank3, ah->analogBank3Data,
257
			   regWrites);
258
	REG_WRITE_RF_ARRAY(&ah->iniBank6TPC, ah->analogBank6Data,
259
			   regWrites);
260
	REG_WRITE_RF_ARRAY(&ah->iniBank7, ah->analogBank7Data,
261 262 263 264 265 266
			   regWrites);

	return true;
}

void
267
ath9k_hw_rf_free(struct ath_hw *ah)
268
{
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#define ATH_FREE_BANK(bank) do { \
		kfree(bank); \
		bank = NULL; \
	} while (0);

	ATH_FREE_BANK(ah->analogBank0Data);
	ATH_FREE_BANK(ah->analogBank1Data);
	ATH_FREE_BANK(ah->analogBank2Data);
	ATH_FREE_BANK(ah->analogBank3Data);
	ATH_FREE_BANK(ah->analogBank6Data);
	ATH_FREE_BANK(ah->analogBank6TPCData);
	ATH_FREE_BANK(ah->analogBank7Data);
	ATH_FREE_BANK(ah->addac5416_21);
	ATH_FREE_BANK(ah->bank6Temp);
#undef ATH_FREE_BANK
284 285
}

286
bool ath9k_hw_init_rf(struct ath_hw *ah, int *status)
287 288
{
	if (!AR_SREV_9280_10_OR_LATER(ah)) {
289
		ah->analogBank0Data =
290
		    kzalloc((sizeof(u32) *
291 292
			     ah->iniBank0.ia_rows), GFP_KERNEL);
		ah->analogBank1Data =
293
		    kzalloc((sizeof(u32) *
294 295
			     ah->iniBank1.ia_rows), GFP_KERNEL);
		ah->analogBank2Data =
296
		    kzalloc((sizeof(u32) *
297 298
			     ah->iniBank2.ia_rows), GFP_KERNEL);
		ah->analogBank3Data =
299
		    kzalloc((sizeof(u32) *
300 301
			     ah->iniBank3.ia_rows), GFP_KERNEL);
		ah->analogBank6Data =
302
		    kzalloc((sizeof(u32) *
303 304
			     ah->iniBank6.ia_rows), GFP_KERNEL);
		ah->analogBank6TPCData =
305
		    kzalloc((sizeof(u32) *
306 307
			     ah->iniBank6TPC.ia_rows), GFP_KERNEL);
		ah->analogBank7Data =
308
		    kzalloc((sizeof(u32) *
309 310 311 312 313 314 315 316 317
			     ah->iniBank7.ia_rows), GFP_KERNEL);

		if (ah->analogBank0Data == NULL
		    || ah->analogBank1Data == NULL
		    || ah->analogBank2Data == NULL
		    || ah->analogBank3Data == NULL
		    || ah->analogBank6Data == NULL
		    || ah->analogBank6TPCData == NULL
		    || ah->analogBank7Data == NULL) {
318
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
S
Sujith 已提交
319
				"Cannot allocate RF banks\n");
320 321 322 323
			*status = -ENOMEM;
			return false;
		}

324
		ah->addac5416_21 =
325
		    kzalloc((sizeof(u32) *
326 327 328
			     ah->iniAddac.ia_rows *
			     ah->iniAddac.ia_columns), GFP_KERNEL);
		if (ah->addac5416_21 == NULL) {
329
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
330
				"Cannot allocate addac5416_21\n");
331 332 333 334
			*status = -ENOMEM;
			return false;
		}

335
		ah->bank6Temp =
336
		    kzalloc((sizeof(u32) *
337 338
			     ah->iniBank6.ia_rows), GFP_KERNEL);
		if (ah->bank6Temp == NULL) {
339
			DPRINTF(ah->ah_sc, ATH_DBG_FATAL,
340
				"Cannot allocate bank6Temp\n");
341 342 343 344 345 346 347 348 349
			*status = -ENOMEM;
			return false;
		}
	}

	return true;
}

void
350
ath9k_hw_decrease_chain_power(struct ath_hw *ah, struct ath9k_channel *chan)
351 352 353
{
	int i, regWrites = 0;
	u32 bank6SelMask;
354
	u32 *bank6Temp = ah->bank6Temp;
355

S
Sujith 已提交
356
	switch (ah->config.diversity_control) {
357 358
	case ATH9K_ANT_FIXED_A:
		bank6SelMask =
S
Sujith 已提交
359 360
		    (ah->config.antenna_switch_swap & ANTSWAP_AB) ?
			REDUCE_CHAIN_0 : REDUCE_CHAIN_1;
361 362 363
		break;
	case ATH9K_ANT_FIXED_B:
		bank6SelMask =
S
Sujith 已提交
364 365
		    (ah->config.antenna_switch_swap & ANTSWAP_AB) ?
			REDUCE_CHAIN_1 : REDUCE_CHAIN_0;
366 367 368 369 370 371 372 373 374
		break;
	case ATH9K_ANT_VARIABLE:
		return;
		break;
	default:
		return;
		break;
	}

375 376
	for (i = 0; i < ah->iniBank6.ia_rows; i++)
		bank6Temp[i] = ah->analogBank6Data[i];
377 378 379 380 381 382 383 384 385 386 387 388 389

	REG_WRITE(ah, AR_PHY_BASE + 0xD8, bank6SelMask);

	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 189, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 190, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 191, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 192, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 193, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 222, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 245, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 246, 0);
	ath9k_phy_modify_rx_buffer(bank6Temp, 1, 1, 247, 0);

390
	REG_WRITE_RF_ARRAY(&ah->iniBank6, bank6Temp, regWrites);
391 392 393 394 395 396 397 398

	REG_WRITE(ah, AR_PHY_BASE + 0xD8, 0x00000053);
#ifdef ALTER_SWITCH
	REG_WRITE(ah, PHY_SWITCH_CHAIN_0,
		  (REG_READ(ah, PHY_SWITCH_CHAIN_0) & ~0x38)
		  | ((REG_READ(ah, PHY_SWITCH_CHAIN_0) >> 3) & 0x38));
#endif
}