diff options
Diffstat (limited to 'drivers/net/wireless/ath/ath5k/phy.c')
| -rw-r--r-- | drivers/net/wireless/ath/ath5k/phy.c | 1791 | 
1 files changed, 1323 insertions, 468 deletions
diff --git a/drivers/net/wireless/ath/ath5k/phy.c b/drivers/net/wireless/ath/ath5k/phy.c index 6b43f535ff5..0fce1c76638 100644 --- a/drivers/net/wireless/ath/ath5k/phy.c +++ b/drivers/net/wireless/ath/ath5k/phy.c @@ -1,6 +1,4 @@  /* - * PHY functions - *   * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>   * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>   * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com> @@ -20,20 +18,179 @@   *   */ +/***********************\ +* PHY related functions * +\***********************/ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +  #include <linux/delay.h>  #include <linux/slab.h> +#include <asm/unaligned.h>  #include "ath5k.h"  #include "reg.h" -#include "base.h"  #include "rfbuffer.h"  #include "rfgain.h" +#include "../regd.h" -/* - * Used to modify RF Banks before writing them to AR5K_RF_BUFFER + +/** + * DOC: PHY related functions + * + * Here we handle the low-level functions related to baseband + * and analog frontend (RF) parts. This is by far the most complex + * part of the hw code so make sure you know what you are doing. + * + * Here is a list of what this is all about: + * + * - Channel setting/switching + * + * - Automatic Gain Control (AGC) calibration + * + * - Noise Floor calibration + * + * - I/Q imbalance calibration (QAM correction) + * + * - Calibration due to thermal changes (gain_F) + * + * - Spur noise mitigation + * + * - RF/PHY initialization for the various operating modes and bwmodes + * + * - Antenna control + * + * - TX power control per channel/rate/packet type + * + * Also have in mind we never got documentation for most of these + * functions, what we have comes mostly from Atheros's code, reverse + * engineering and patent docs/presentations etc. + */ + + +/******************\ +* Helper functions * +\******************/ + +/** + * ath5k_hw_radio_revision() - Get the PHY Chip revision + * @ah: The &struct ath5k_hw + * @band: One of enum ieee80211_band + * + * Returns the revision number of a 2GHz, 5GHz or single chip + * radio. + */ +u16 +ath5k_hw_radio_revision(struct ath5k_hw *ah, enum ieee80211_band band) +{ +	unsigned int i; +	u32 srev; +	u16 ret; + +	/* +	 * Set the radio chip access register +	 */ +	switch (band) { +	case IEEE80211_BAND_2GHZ: +		ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0)); +		break; +	case IEEE80211_BAND_5GHZ: +		ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0)); +		break; +	default: +		return 0; +	} + +	usleep_range(2000, 2500); + +	/* ...wait until PHY is ready and read the selected radio revision */ +	ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34)); + +	for (i = 0; i < 8; i++) +		ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20)); + +	if (ah->ah_version == AR5K_AR5210) { +		srev = (ath5k_hw_reg_read(ah, AR5K_PHY(256)) >> 28) & 0xf; +		ret = (u16)ath5k_hw_bitswap(srev, 4) + 1; +	} else { +		srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff; +		ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) | +				((srev & 0x0f) << 4), 8); +	} + +	/* Reset to the 5GHz mode */ +	ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0)); + +	return ret; +} + +/** + * ath5k_channel_ok() - Check if a channel is supported by the hw + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * Note: We don't do any regulatory domain checks here, it's just + * a sanity check.   */ -static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah, -					const struct ath5k_rf_reg *rf_regs, +bool +ath5k_channel_ok(struct ath5k_hw *ah, struct ieee80211_channel *channel) +{ +	u16 freq = channel->center_freq; + +	/* Check if the channel is in our supported range */ +	if (channel->band == IEEE80211_BAND_2GHZ) { +		if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) && +		    (freq <= ah->ah_capabilities.cap_range.range_2ghz_max)) +			return true; +	} else if (channel->band == IEEE80211_BAND_5GHZ) +		if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) && +		    (freq <= ah->ah_capabilities.cap_range.range_5ghz_max)) +			return true; + +	return false; +} + +/** + * ath5k_hw_chan_has_spur_noise() - Check if channel is sensitive to spur noise + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + */ +bool +ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah, +				struct ieee80211_channel *channel) +{ +	u8 refclk_freq; + +	if ((ah->ah_radio == AR5K_RF5112) || +	(ah->ah_radio == AR5K_RF5413) || +	(ah->ah_radio == AR5K_RF2413) || +	(ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) +		refclk_freq = 40; +	else +		refclk_freq = 32; + +	if ((channel->center_freq % refclk_freq != 0) && +	((channel->center_freq % refclk_freq < 10) || +	(channel->center_freq % refclk_freq > 22))) +		return true; +	else +		return false; +} + +/** + * ath5k_hw_rfb_op() - Perform an operation on the given RF Buffer + * @ah: The &struct ath5k_hw + * @rf_regs: The struct ath5k_rf_reg + * @val: New value + * @reg_id: RF register ID + * @set: Indicate we need to swap data + * + * This is an internal function used to modify RF Banks before + * writing them to AR5K_RF_BUFFER. Check out rfbuffer.h for more + * infos. + */ +static unsigned int +ath5k_hw_rfb_op(struct ath5k_hw *ah, const struct ath5k_rf_reg *rf_regs,  					u32 val, u8 reg_id, bool set)  {  	const struct ath5k_rf_reg *rfreg = NULL; @@ -84,7 +241,7 @@ static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,  		data = ath5k_hw_bitswap(val, num_bits);  	for (bits_shifted = 0, bits_left = num_bits; bits_left > 0; -	position = 0, entry++) { +	     position = 0, entry++) {  		last_bit = (position + bits_left > 8) ? 8 :  					position + bits_left; @@ -110,11 +267,132 @@ static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,  	return data;  } +/** + * ath5k_hw_write_ofdm_timings() - set OFDM timings on AR5212 + * @ah: the &struct ath5k_hw + * @channel: the currently set channel upon reset + * + * Write the delta slope coefficient (used on pilot tracking ?) for OFDM + * operation on the AR5212 upon reset. This is a helper for ath5k_hw_phy_init. + * + * Since delta slope is floating point we split it on its exponent and + * mantissa and provide these values on hw. + * + * For more infos i think this patent is related + * "http://www.freepatentsonline.com/7184495.html" + */ +static inline int +ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah, +				struct ieee80211_channel *channel) +{ +	/* Get exponent and mantissa and set it */ +	u32 coef_scaled, coef_exp, coef_man, +		ds_coef_exp, ds_coef_man, clock; + +	BUG_ON(!(ah->ah_version == AR5K_AR5212) || +		(channel->hw_value == AR5K_MODE_11B)); + +	/* Get coefficient +	 * ALGO: coef = (5 * clock / carrier_freq) / 2 +	 * we scale coef by shifting clock value by 24 for +	 * better precision since we use integers */ +	switch (ah->ah_bwmode) { +	case AR5K_BWMODE_40MHZ: +		clock = 40 * 2; +		break; +	case AR5K_BWMODE_10MHZ: +		clock = 40 / 2; +		break; +	case AR5K_BWMODE_5MHZ: +		clock = 40 / 4; +		break; +	default: +		clock = 40; +		break; +	} +	coef_scaled = ((5 * (clock << 24)) / 2) / channel->center_freq; + +	/* Get exponent +	 * ALGO: coef_exp = 14 - highest set bit position */ +	coef_exp = ilog2(coef_scaled); + +	/* Doesn't make sense if it's zero*/ +	if (!coef_scaled || !coef_exp) +		return -EINVAL; + +	/* Note: we've shifted coef_scaled by 24 */ +	coef_exp = 14 - (coef_exp - 24); + + +	/* Get mantissa (significant digits) +	 * ALGO: coef_mant = floor(coef_scaled* 2^coef_exp+0.5) */ +	coef_man = coef_scaled + +		(1 << (24 - coef_exp - 1)); + +	/* Calculate delta slope coefficient exponent +	 * and mantissa (remove scaling) and set them on hw */ +	ds_coef_man = coef_man >> (24 - coef_exp); +	ds_coef_exp = coef_exp - 16; + +	AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3, +		AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man); +	AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3, +		AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp); + +	return 0; +} + +/** + * ath5k_hw_phy_disable() - Disable PHY + * @ah: The &struct ath5k_hw + */ +int ath5k_hw_phy_disable(struct ath5k_hw *ah) +{ +	/*Just a try M.F.*/ +	ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT); + +	return 0; +} + +/** + * ath5k_hw_wait_for_synth() - Wait for synth to settle + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + */ +static void +ath5k_hw_wait_for_synth(struct ath5k_hw *ah, +			struct ieee80211_channel *channel) +{ +	/* +	 * On 5211+ read activation -> rx delay +	 * and use it (100ns steps). +	 */ +	if (ah->ah_version != AR5K_AR5210) { +		u32 delay; +		delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) & +			AR5K_PHY_RX_DELAY_M; +		delay = (channel->hw_value == AR5K_MODE_11B) ? +			((delay << 2) / 22) : (delay / 10); +		if (ah->ah_bwmode == AR5K_BWMODE_10MHZ) +			delay = delay << 1; +		if (ah->ah_bwmode == AR5K_BWMODE_5MHZ) +			delay = delay << 2; +		/* XXX: /2 on turbo ? Let's be safe +		 * for now */ +		usleep_range(100 + delay, 100 + (2 * delay)); +	} else { +		usleep_range(1000, 1500); +	} +} + +  /**********************\  * RF Gain optimization *  \**********************/ -/* +/** + * DOC: RF Gain optimization + *   * This code is used to optimize RF gain on different environments   * (temperature mostly) based on feedback from a power detector.   * @@ -123,22 +401,22 @@ static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,   * no gain optimization ladder-.   *   * For more infos check out this patent doc - * http://www.freepatentsonline.com/7400691.html + * "http://www.freepatentsonline.com/7400691.html"   *   * This paper describes power drops as seen on the receiver due to   * probe packets - * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues - * %20of%20Power%20Control.pdf + * "http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues + * %20of%20Power%20Control.pdf"   *   * And this is the MadWiFi bug entry related to the above - * http://madwifi-project.org/ticket/1659 + * "http://madwifi-project.org/ticket/1659"   * with various measurements and diagrams - * - * TODO: Deal with power drops due to probes by setting an apropriate - * tx power on the probe packets ! Make this part of the calibration process.   */ -/* Initialize ah_gain durring attach */ +/** + * ath5k_hw_rfgain_opt_init() - Initialize ah_gain during attach + * @ah: The &struct ath5k_hw + */  int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)  {  	/* Initialize the gain optimization values */ @@ -162,17 +440,21 @@ int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)  	return 0;  } -/* Schedule a gain probe check on the next transmited packet. +/** + * ath5k_hw_request_rfgain_probe() - Request a PAPD probe packet + * @ah: The &struct ath5k_hw + * + * Schedules a gain probe check on the next transmitted packet.   * That means our next packet is going to be sent with lower   * tx power and a Peak to Average Power Detector (PAPD) will try   * to measure the gain.   * - * XXX:  How about forcing a tx packet (bypassing PCU arbitrator etc) + * TODO: Force a tx packet (bypassing PCU arbitrator etc)   * just after we enable the probe so that we don't mess with - * standard traffic ? Maybe it's time to use sw interrupts and - * a probe tasklet !!! + * standard traffic.   */ -static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah) +static void +ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)  {  	/* Skip if gain calibration is inactive or @@ -190,9 +472,15 @@ static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)  } -/* Calculate gain_F measurement correction - * based on the current step for RF5112 rev. 2 */ -static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah) +/** + * ath5k_hw_rf_gainf_corr() - Calculate Gain_F measurement correction + * @ah: The &struct ath5k_hw + * + * Calculate Gain_F measurement correction + * based on the current step for RF5112 rev. 2 + */ +static u32 +ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)  {  	u32 mix, step;  	u32 *rf; @@ -245,11 +533,19 @@ static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)  	return ah->ah_gain.g_f_corr;  } -/* Check if current gain_F measurement is in the range of our +/** + * ath5k_hw_rf_check_gainf_readback() - Validate Gain_F feedback from detector + * @ah: The &struct ath5k_hw + * + * Check if current gain_F measurement is in the range of our   * power detector windows. If we get a measurement outside range   * we know it's not accurate (detectors can't measure anything outside - * their detection window) so we must ignore it */ -static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah) + * their detection window) so we must ignore it. + * + * Returns true if readback was O.K. or false on failure + */ +static bool +ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)  {  	const struct ath5k_rf_reg *rf_regs;  	u32 step, mix_ovr, level[4]; @@ -271,7 +567,7 @@ static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)  		level[0] = 0;  		level[1] = (step == 63) ? 50 : step + 4;  		level[2] = (step != 63) ? 64 : level[0]; -		level[3] = level[2] + 50 ; +		level[3] = level[2] + 50;  		ah->ah_gain.g_high = level[3] -  			(step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5); @@ -301,9 +597,15 @@ static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)  			ah->ah_gain.g_current <= level[3]);  } -/* Perform gain_F adjustment by choosing the right set - * of parameters from RF gain optimization ladder */ -static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah) +/** + * ath5k_hw_rf_gainf_adjust() - Perform Gain_F adjustment + * @ah: The &struct ath5k_hw + * + * Choose the right target gain based on current gain + * and RF gain optimization ladder + */ +static s8 +ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)  {  	const struct ath5k_gain_opt *go;  	const struct ath5k_gain_opt_step *g_step; @@ -348,7 +650,7 @@ static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)  		for (ah->ah_gain.g_target = ah->ah_gain.g_current;  				ah->ah_gain.g_target <= ah->ah_gain.g_low && -				ah->ah_gain.g_step_idx < go->go_steps_count-1; +				ah->ah_gain.g_step_idx < go->go_steps_count - 1;  				g_step = &go->go_step[ah->ah_gain.g_step_idx])  			ah->ah_gain.g_target -= 2 *  			    (go->go_step[++ah->ah_gain.g_step_idx].gos_gain - @@ -359,7 +661,7 @@ static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)  	}  done: -	ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +	ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,  		"ret %d, gain step %u, current gain %u, target gain %u\n",  		ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,  		ah->ah_gain.g_target); @@ -367,13 +669,18 @@ done:  	return ret;  } -/* Main callback for thermal RF gain calibration engine +/** + * ath5k_hw_gainf_calibrate() - Do a gain_F calibration + * @ah: The &struct ath5k_hw + * + * Main callback for thermal RF gain calibration engine   * Check for a new gain reading and schedule an adjustment   * if needed.   * - * TODO: Use sw interrupt to schedule reset if gain_F needs - * adjustment */ -enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah) + * Returns one of enum ath5k_rfgain codes + */ +enum ath5k_rfgain +ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)  {  	u32 data, type;  	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom; @@ -413,13 +720,13 @@ enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)  			ath5k_hw_rf_gainf_corr(ah);  			ah->ah_gain.g_current =  				ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ? -				(ah->ah_gain.g_current-ah->ah_gain.g_f_corr) : +				(ah->ah_gain.g_current - ah->ah_gain.g_f_corr) :  				0;  		}  		/* Check if measurement is ok and if we need  		 * to adjust gain, schedule a gain adjustment, -		 * else switch back to the acive state */ +		 * else switch back to the active state */  		if (ath5k_hw_rf_check_gainf_readback(ah) &&  		AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&  		ath5k_hw_rf_gainf_adjust(ah)) { @@ -433,13 +740,21 @@ done:  	return ah->ah_gain.g_state;  } -/* Write initial RF gain table to set the RF sensitivity - * this one works on all RF chips and has nothing to do - * with gain_F calibration */ -int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq) +/** + * ath5k_hw_rfgain_init() - Write initial RF gain settings to hw + * @ah: The &struct ath5k_hw + * @band: One of enum ieee80211_band + * + * Write initial RF gain table to set the RF sensitivity. + * + * NOTE: This one works on all RF chips and has nothing to do + * with Gain_F calibration + */ +static int +ath5k_hw_rfgain_init(struct ath5k_hw *ah, enum ieee80211_band band)  {  	const struct ath5k_ini_rfgain *ath5k_rfg; -	unsigned int i, size; +	unsigned int i, size, index;  	switch (ah->ah_radio) {  	case AR5K_RF5111: @@ -471,17 +786,11 @@ int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)  		return -EINVAL;  	} -	switch (freq) { -	case AR5K_INI_RFGAIN_2GHZ: -	case AR5K_INI_RFGAIN_5GHZ: -		break; -	default: -		return -EINVAL; -	} +	index = (band == IEEE80211_BAND_2GHZ) ? 1 : 0;  	for (i = 0; i < size; i++) {  		AR5K_REG_WAIT(i); -		ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq], +		ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[index],  			(u32)ath5k_rfg[i].rfg_register);  	} @@ -489,17 +798,23 @@ int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)  } -  /********************\  * RF Registers setup *  \********************/ - -/* - * Setup RF registers by writing RF buffer on hw +/** + * ath5k_hw_rfregs_init() - Initialize RF register settings + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * @mode: One of enum ath5k_driver_mode + * + * Setup RF registers by writing RF buffer on hw. For + * more infos on this, check out rfbuffer.h   */ -int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel, -		unsigned int mode) +static int +ath5k_hw_rfregs_init(struct ath5k_hw *ah, +			struct ieee80211_channel *channel, +			unsigned int mode)  {  	const struct ath5k_rf_reg *rf_regs;  	const struct ath5k_ini_rfbuffer *ini_rfb; @@ -578,7 +893,7 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  		ah->ah_rf_banks = kmalloc(sizeof(u32) * ah->ah_rf_banks_size,  								GFP_KERNEL);  		if (ah->ah_rf_banks == NULL) { -			ATH5K_ERR(ah->ah_sc, "out of memory\n"); +			ATH5K_ERR(ah, "out of memory\n");  			return -ENOMEM;  		}  	} @@ -588,7 +903,7 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  	for (i = 0; i < ah->ah_rf_banks_size; i++) {  		if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) { -			ATH5K_ERR(ah->ah_sc, "invalid bank\n"); +			ATH5K_ERR(ah, "invalid bank\n");  			return -EINVAL;  		} @@ -602,9 +917,9 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  	}  	/* Set Output and Driver bias current (OB/DB) */ -	if (channel->hw_value & CHANNEL_2GHZ) { +	if (channel->band == IEEE80211_BAND_2GHZ) { -		if (channel->hw_value & CHANNEL_CCK) +		if (channel->hw_value == AR5K_MODE_11B)  			ee_mode = AR5K_EEPROM_MODE_11B;  		else  			ee_mode = AR5K_EEPROM_MODE_11G; @@ -613,7 +928,7 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  		 * use b_OB and b_DB parameters stored  		 * in eeprom on ee->ee_ob[ee_mode][0]  		 * -		 * For all other chips we use OB/DB for 2Ghz +		 * For all other chips we use OB/DB for 2GHz  		 * stored in the b/g modal section just like  		 * 802.11a on ee->ee_ob[ee_mode][1] */  		if ((ah->ah_radio == AR5K_RF5111) || @@ -629,7 +944,7 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  						AR5K_RF_DB_2GHZ, true);  	/* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */ -	} else if ((channel->hw_value & CHANNEL_5GHZ) || +	} else if ((channel->band == IEEE80211_BAND_5GHZ) ||  			(ah->ah_radio == AR5K_RF5111)) {  		/* For 11a, Turbo and XR we need to choose @@ -652,11 +967,16 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  	g_step = &go->go_step[ah->ah_gain.g_step_idx]; +	/* Set turbo mode (N/A on RF5413) */ +	if ((ah->ah_bwmode == AR5K_BWMODE_40MHZ) && +	(ah->ah_radio != AR5K_RF5413)) +		ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_TURBO, false); +  	/* Bank Modifications (chip-specific) */  	if (ah->ah_radio == AR5K_RF5111) {  		/* Set gain_F settings according to current step */ -		if (channel->hw_value & CHANNEL_OFDM) { +		if (channel->hw_value != AR5K_MODE_11B) {  			AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,  					AR5K_PHY_FRAME_CTL_TX_CLIP, @@ -691,13 +1011,29 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  		ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],  						AR5K_RF_PLO_SEL, true); -		/* TODO: Half/quarter channel support */ +		/* Tweak power detectors for half/quarter rate support */ +		if (ah->ah_bwmode == AR5K_BWMODE_5MHZ || +		ah->ah_bwmode == AR5K_BWMODE_10MHZ) { +			u8 wait_i; + +			ath5k_hw_rfb_op(ah, rf_regs, 0x1f, +						AR5K_RF_WAIT_S, true); + +			wait_i = (ah->ah_bwmode == AR5K_BWMODE_5MHZ) ? +							0x1f : 0x10; + +			ath5k_hw_rfb_op(ah, rf_regs, wait_i, +						AR5K_RF_WAIT_I, true); +			ath5k_hw_rfb_op(ah, rf_regs, 3, +						AR5K_RF_MAX_TIME, true); + +		}  	}  	if (ah->ah_radio == AR5K_RF5112) {  		/* Set gain_F settings according to current step */ -		if (channel->hw_value & CHANNEL_OFDM) { +		if (channel->hw_value != AR5K_MODE_11B) {  			ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],  						AR5K_RF_MIXGAIN_OVR, true); @@ -755,17 +1091,20 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  			}  			/* Lower synth voltage on Rev 2 */ -			ath5k_hw_rfb_op(ah, rf_regs, 2, -					AR5K_RF_HIGH_VC_CP, true); +			if (ah->ah_radio == AR5K_RF5112 && +			    (ah->ah_radio_5ghz_revision & AR5K_SREV_REV) > 0) { +				ath5k_hw_rfb_op(ah, rf_regs, 2, +						AR5K_RF_HIGH_VC_CP, true); -			ath5k_hw_rfb_op(ah, rf_regs, 2, -					AR5K_RF_MID_VC_CP, true); +				ath5k_hw_rfb_op(ah, rf_regs, 2, +						AR5K_RF_MID_VC_CP, true); -			ath5k_hw_rfb_op(ah, rf_regs, 2, -					AR5K_RF_LOW_VC_CP, true); +				ath5k_hw_rfb_op(ah, rf_regs, 2, +						AR5K_RF_LOW_VC_CP, true); -			ath5k_hw_rfb_op(ah, rf_regs, 2, -					AR5K_RF_PUSH_UP, true); +				ath5k_hw_rfb_op(ah, rf_regs, 2, +						AR5K_RF_PUSH_UP, true); +			}  			/* Decrease power consumption on 5213+ BaseBand */  			if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) { @@ -789,12 +1128,24 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,  		ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],  						AR5K_RF_GAIN_I, true); -		/* TODO: Half/quarter channel support */ +		/* Tweak power detector for half/quarter rates */ +		if (ah->ah_bwmode == AR5K_BWMODE_5MHZ || +		ah->ah_bwmode == AR5K_BWMODE_10MHZ) { +			u8 pd_delay; + +			pd_delay = (ah->ah_bwmode == AR5K_BWMODE_5MHZ) ? +							0xf : 0x8; + +			ath5k_hw_rfb_op(ah, rf_regs, pd_delay, +						AR5K_RF_PD_PERIOD_A, true); +			ath5k_hw_rfb_op(ah, rf_regs, 0xf, +						AR5K_RF_PD_DELAY_A, true); +		}  	}  	if (ah->ah_radio == AR5K_RF5413 && -	channel->hw_value & CHANNEL_2GHZ) { +	channel->band == IEEE80211_BAND_2GHZ) {  		ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,  									true); @@ -821,37 +1172,18 @@ int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,    PHY/RF channel functions  \**************************/ -/* - * Check if a channel is supported - */ -bool ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags) -{ -	/* Check if the channel is in our supported range */ -	if (flags & CHANNEL_2GHZ) { -		if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) && -		    (freq <= ah->ah_capabilities.cap_range.range_2ghz_max)) -			return true; -	} else if (flags & CHANNEL_5GHZ) -		if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) && -		    (freq <= ah->ah_capabilities.cap_range.range_5ghz_max)) -			return true; - -	return false; -} - -/* - * Convertion needed for RF5110 +/** + * ath5k_hw_rf5110_chan2athchan() - Convert channel freq on RF5110 + * @channel: The &struct ieee80211_channel + * + * Map channel frequency to IEEE channel number and convert it + * to an internal channel value used by the RF5110 chipset.   */ -static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel) +static u32 +ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)  {  	u32 athchan; -	/* -	 * Convert IEEE channel/MHz to an internal channel value used -	 * by the AR5210 chipset. This has not been verified with -	 * newer chipsets like the AR5212A who have a completely -	 * different RF/PHY part. -	 */  	athchan = (ath5k_hw_bitswap(  			(ieee80211_frequency_to_channel(  				channel->center_freq) - 24) / 2, 5) @@ -859,10 +1191,13 @@ static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)  	return athchan;  } -/* - * Set channel on RF5110 +/** + * ath5k_hw_rf5110_channel() - Set channel frequency on RF5110 + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel   */ -static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah, +static int +ath5k_hw_rf5110_channel(struct ath5k_hw *ah,  		struct ieee80211_channel *channel)  {  	u32 data; @@ -873,15 +1208,23 @@ static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,  	data = ath5k_hw_rf5110_chan2athchan(channel);  	ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);  	ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0); -	mdelay(1); +	usleep_range(1000, 1500);  	return 0;  } -/* - * Convertion needed for 5111 +/** + * ath5k_hw_rf5111_chan2athchan() - Handle 2GHz channels on RF5111/2111 + * @ieee: IEEE channel number + * @athchan: The &struct ath5k_athchan_2ghz + * + * In order to enable the RF2111 frequency converter on RF5111/2111 setups + * we need to add some offsets and extra flags to the data values we pass + * on to the PHY. So for every 2GHz channel this function gets called + * to do the conversion.   */ -static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee, +static int +ath5k_hw_rf5111_chan2athchan(unsigned int ieee,  		struct ath5k_athchan_2ghz *athchan)  {  	int channel; @@ -907,10 +1250,13 @@ static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,  	return 0;  } -/* - * Set channel on 5111 +/** + * ath5k_hw_rf5111_channel() - Set channel frequency on RF5111/2111 + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel   */ -static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah, +static int +ath5k_hw_rf5111_channel(struct ath5k_hw *ah,  		struct ieee80211_channel *channel)  {  	struct ath5k_athchan_2ghz ath5k_channel_2ghz; @@ -924,7 +1270,7 @@ static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,  	 */  	data0 = data1 = 0; -	if (channel->hw_value & CHANNEL_2GHZ) { +	if (channel->band == IEEE80211_BAND_2GHZ) {  		/* Map 2GHz channel to 5GHz Atheros channel ID */  		ret = ath5k_hw_rf5111_chan2athchan(  			ieee80211_frequency_to_channel(channel->center_freq), @@ -955,10 +1301,20 @@ static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,  	return 0;  } -/* - * Set channel on 5112 and newer +/** + * ath5k_hw_rf5112_channel() - Set channel frequency on 5112 and newer + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * On RF5112/2112 and newer we don't need to do any conversion. + * We pass the frequency value after a few modifications to the + * chip directly. + * + * NOTE: Make sure channel frequency given is within our range or else + * we might damage the chip ! Use ath5k_channel_ok before calling this one.   */ -static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah, +static int +ath5k_hw_rf5112_channel(struct ath5k_hw *ah,  		struct ieee80211_channel *channel)  {  	u32 data, data0, data1, data2; @@ -967,17 +1323,37 @@ static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,  	data = data0 = data1 = data2 = 0;  	c = channel->center_freq; +	/* My guess based on code: +	 * 2GHz RF has 2 synth modes, one with a Local Oscillator +	 * at 2224Hz and one with a LO at 2192Hz. IF is 1520Hz +	 * (3040/2). data0 is used to set the PLL divider and data1 +	 * selects synth mode. */  	if (c < 4800) { +		/* Channel 14 and all frequencies with 2Hz spacing +		 * below/above (non-standard channels) */  		if (!((c - 2224) % 5)) { +			/* Same as (c - 2224) / 5 */  			data0 = ((2 * (c - 704)) - 3040) / 10;  			data1 = 1; +		/* Channel 1 and all frequencies with 5Hz spacing +		 * below/above (standard channels without channel 14) */  		} else if (!((c - 2192) % 5)) { +			/* Same as (c - 2192) / 5 */  			data0 = ((2 * (c - 672)) - 3040) / 10;  			data1 = 0;  		} else  			return -EINVAL;  		data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8); +	/* This is more complex, we have a single synthesizer with +	 * 4 reference clock settings (?) based on frequency spacing +	 * and set using data2. LO is at 4800Hz and data0 is again used +	 * to set some divider. +	 * +	 * NOTE: There is an old atheros presentation at Stanford +	 * that mentions a method called dual direct conversion +	 * with 1GHz sliding IF for RF5110. Maybe that's what we +	 * have here, or an updated version. */  	} else if ((c % 5) != 2 || c > 5435) {  		if (!(c % 20) && c >= 5120) {  			data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8); @@ -1003,10 +1379,16 @@ static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,  	return 0;  } -/* - * Set the channel on the RF2425 +/** + * ath5k_hw_rf2425_channel() - Set channel frequency on RF2425 + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * AR2425/2417 have a different 2GHz RF so code changes + * a little bit from RF5112.   */ -static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah, +static int +ath5k_hw_rf2425_channel(struct ath5k_hw *ah,  		struct ieee80211_channel *channel)  {  	u32 data, data0, data2; @@ -1042,19 +1424,25 @@ static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,  	return 0;  } -/* - * Set a channel on the radio chip +/** + * ath5k_hw_channel() - Set a channel on the radio chip + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * This is the main function called to set a channel on the + * radio chip based on the radio chip version.   */ -int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel) +static int +ath5k_hw_channel(struct ath5k_hw *ah, +		struct ieee80211_channel *channel)  {  	int ret;  	/* -	 * Check bounds supported by the PHY (we don't care about regultory -	 * restrictions at this point). Note: hw_value already has the band -	 * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok() -	 * of the band by that */ -	if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) { -		ATH5K_ERR(ah->ah_sc, +	 * Check bounds supported by the PHY (we don't care about regulatory +	 * restrictions at this point). +	 */ +	if (!ath5k_channel_ok(ah, channel)) { +		ATH5K_ERR(ah,  			"channel frequency (%u MHz) out of supported "  			"band range\n",  			channel->center_freq); @@ -1071,6 +1459,7 @@ int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)  	case AR5K_RF5111:  		ret = ath5k_hw_rf5111_channel(ah, channel);  		break; +	case AR5K_RF2317:  	case AR5K_RF2425:  		ret = ath5k_hw_rf2425_channel(ah, channel);  		break; @@ -1092,17 +1481,50 @@ int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)  	}  	ah->ah_current_channel = channel; -	ah->ah_turbo = channel->hw_value == CHANNEL_T ? true : false; -	ath5k_hw_set_clockrate(ah);  	return 0;  } +  /*****************\    PHY calibration  \*****************/ -static s32 ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah) +/** + * DOC: PHY Calibration routines + * + * Noise floor calibration: When we tell the hardware to + * perform a noise floor calibration by setting the + * AR5K_PHY_AGCCTL_NF bit on AR5K_PHY_AGCCTL, it will periodically + * sample-and-hold the minimum noise level seen at the antennas. + * This value is then stored in a ring buffer of recently measured + * noise floor values so we have a moving window of the last few + * samples. The median of the values in the history is then loaded + * into the hardware for its own use for RSSI and CCA measurements. + * This type of calibration doesn't interfere with traffic. + * + * AGC calibration: When we tell the hardware to perform + * an AGC (Automatic Gain Control) calibration by setting the + * AR5K_PHY_AGCCTL_CAL, hw disconnects the antennas and does + * a calibration on the DC offsets of ADCs. During this period + * rx/tx gets disabled so we have to deal with it on the driver + * part. + * + * I/Q calibration: When we tell the hardware to perform + * an I/Q calibration, it tries to correct I/Q imbalance and + * fix QAM constellation by sampling data from rxed frames. + * It doesn't interfere with traffic. + * + * For more infos on AGC and I/Q calibration check out patent doc + * #03/094463. + */ + +/** + * ath5k_hw_read_measured_noise_floor() - Read measured NF from hw + * @ah: The &struct ath5k_hw + */ +static s32 +ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)  {  	s32 val; @@ -1110,7 +1532,12 @@ static s32 ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)  	return sign_extend32(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 8);  } -void ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah) +/** + * ath5k_hw_init_nfcal_hist() - Initialize NF calibration history buffer + * @ah: The &struct ath5k_hw + */ +void +ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)  {  	int i; @@ -1119,14 +1546,24 @@ void ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)  		ah->ah_nfcal_hist.nfval[i] = AR5K_TUNE_CCA_MAX_GOOD_VALUE;  } +/** + * ath5k_hw_update_nfcal_hist() - Update NF calibration history buffer + * @ah: The &struct ath5k_hw + * @noise_floor: The NF we got from hw + */  static void ath5k_hw_update_nfcal_hist(struct ath5k_hw *ah, s16 noise_floor)  {  	struct ath5k_nfcal_hist *hist = &ah->ah_nfcal_hist; -	hist->index = (hist->index + 1) & (ATH5K_NF_CAL_HIST_MAX-1); +	hist->index = (hist->index + 1) & (ATH5K_NF_CAL_HIST_MAX - 1);  	hist->nfval[hist->index] = noise_floor;  } -static s16 ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah) +/** + * ath5k_hw_get_median_noise_floor() - Get median NF from history buffer + * @ah: The &struct ath5k_hw + */ +static s16 +ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah)  {  	s16 sort[ATH5K_NF_CAL_HIST_MAX];  	s16 tmp; @@ -1135,32 +1572,30 @@ static s16 ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah)  	memcpy(sort, ah->ah_nfcal_hist.nfval, sizeof(sort));  	for (i = 0; i < ATH5K_NF_CAL_HIST_MAX - 1; i++) {  		for (j = 1; j < ATH5K_NF_CAL_HIST_MAX - i; j++) { -			if (sort[j] > sort[j-1]) { +			if (sort[j] > sort[j - 1]) {  				tmp = sort[j]; -				sort[j] = sort[j-1]; -				sort[j-1] = tmp; +				sort[j] = sort[j - 1]; +				sort[j - 1] = tmp;  			}  		}  	}  	for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++) { -		ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +		ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,  			"cal %d:%d\n", i, sort[i]);  	} -	return sort[(ATH5K_NF_CAL_HIST_MAX-1) / 2]; +	return sort[(ATH5K_NF_CAL_HIST_MAX - 1) / 2];  } -/* - * When we tell the hardware to perform a noise floor calibration - * by setting the AR5K_PHY_AGCCTL_NF bit, it will periodically - * sample-and-hold the minimum noise level seen at the antennas. - * This value is then stored in a ring buffer of recently measured - * noise floor values so we have a moving window of the last few - * samples. +/** + * ath5k_hw_update_noise_floor() - Update NF on hardware + * @ah: The &struct ath5k_hw   * - * The median of the values in the history is then loaded into the - * hardware for its own use for RSSI and CCA measurements. + * This is the main function we call to perform a NF calibration, + * it reads NF from hardware, calculates the median and updates + * NF on hw.   */ -void ath5k_hw_update_noise_floor(struct ath5k_hw *ah) +void +ath5k_hw_update_noise_floor(struct ath5k_hw *ah)  {  	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;  	u32 val; @@ -1169,35 +1604,22 @@ void ath5k_hw_update_noise_floor(struct ath5k_hw *ah)  	/* keep last value if calibration hasn't completed */  	if (ath5k_hw_reg_read(ah, AR5K_PHY_AGCCTL) & AR5K_PHY_AGCCTL_NF) { -		ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +		ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,  			"NF did not complete in calibration window\n");  		return;  	} -	switch (ah->ah_current_channel->hw_value & CHANNEL_MODES) { -	case CHANNEL_A: -	case CHANNEL_T: -	case CHANNEL_XR: -		ee_mode = AR5K_EEPROM_MODE_11A; -		break; -	case CHANNEL_G: -	case CHANNEL_TG: -		ee_mode = AR5K_EEPROM_MODE_11G; -		break; -	default: -	case CHANNEL_B: -		ee_mode = AR5K_EEPROM_MODE_11B; -		break; -	} +	ah->ah_cal_mask |= AR5K_CALIBRATION_NF; +	ee_mode = ath5k_eeprom_mode_from_channel(ah, ah->ah_current_channel);  	/* completed NF calibration, test threshold */  	nf = ath5k_hw_read_measured_noise_floor(ah);  	threshold = ee->ee_noise_floor_thr[ee_mode];  	if (nf > threshold) { -		ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +		ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,  			"noise floor failure detected; "  			"read %d, threshold %d\n",  			nf, threshold); @@ -1234,20 +1656,29 @@ void ath5k_hw_update_noise_floor(struct ath5k_hw *ah)  	ah->ah_noise_floor = nf; -	ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +	ah->ah_cal_mask &= ~AR5K_CALIBRATION_NF; + +	ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,  		"noise floor calibrated: %d\n", nf);  } -/* - * Perform a PHY calibration on RF5110 - * -Fix BPSK/QAM Constellation (I/Q correction) +/** + * ath5k_hw_rf5110_calibrate() - Perform a PHY calibration on RF5110 + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * Do a complete PHY calibration (AGC + NF + I/Q) on RF5110   */ -static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah, +static int +ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,  		struct ieee80211_channel *channel)  {  	u32 phy_sig, phy_agc, phy_sat, beacon;  	int ret; +	if (!(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) +		return 0; +  	/*  	 * Disable beacons and RX/TX queues, wait  	 */ @@ -1256,7 +1687,7 @@ static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,  	beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);  	ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210); -	mdelay(2); +	usleep_range(2000, 2500);  	/*  	 * Set the channel (with AGC turned off) @@ -1269,7 +1700,7 @@ static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,  	 * Activate PHY and wait  	 */  	ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT); -	mdelay(1); +	usleep_range(1000, 1500);  	AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE); @@ -1306,7 +1737,7 @@ static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,  	ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);  	AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE); -	mdelay(1); +	usleep_range(1000, 1500);  	/*  	 * Enable calibration and wait until completion @@ -1322,7 +1753,7 @@ static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,  	ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);  	if (ret) { -		ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n", +		ATH5K_ERR(ah, "calibration timeout (%uMHz)\n",  				channel->center_freq);  		return ret;  	} @@ -1337,8 +1768,9 @@ static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,  	return 0;  } -/* - * Perform I/Q calibration on RF5111/5112 and newer chips +/** + * ath5k_hw_rf511x_iq_calibrate() - Perform I/Q calibration on RF5111 and newer + * @ah: The &struct ath5k_hw   */  static int  ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah) @@ -1347,17 +1779,24 @@ ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)  	s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;  	int i; -	if (!ah->ah_calibration || -		ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN) -		return 0; +	/* Skip if I/Q calibration is not needed or if it's still running */ +	if (!ah->ah_iq_cal_needed) +		return -EINVAL; +	else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN) { +		ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE, +				"I/Q calibration still running"); +		return -EBUSY; +	}  	/* Calibration has finished, get the results and re-run */ -	/* work around empty results which can apparently happen on 5212 */ + +	/* Work around for empty results which can apparently happen on 5212: +	 * Read registers up to 10 times until we get both i_pr and q_pwr */  	for (i = 0; i <= 10; i++) {  		iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);  		i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);  		q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q); -		ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +		ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,  			"iq_corr:%x i_pwr:%x q_pwr:%x", iq_corr, i_pwr, q_pwr);  		if (i_pwr && q_pwr)  			break; @@ -1370,9 +1809,13 @@ ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)  	else  		q_coffd = q_pwr >> 7; -	/* protect against divide by 0 and loss of sign bits */ +	/* In case i_coffd became zero, cancel calibration +	 * not only it's too small, it'll also result a divide +	 * by zero later on. */  	if (i_coffd == 0 || q_coffd < 2) -		return 0; +		return -ECANCELED; + +	/* Protect against loss of sign bits */  	i_coff = (-iq_corr) / i_coffd;  	i_coff = clamp(i_coff, -32, 31); /* signed 6 bit */ @@ -1383,7 +1826,7 @@ ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)  		q_coff = (i_pwr / q_coffd) - 128;  	q_coff = clamp(q_coff, -16, 15); /* signed 5 bit */ -	ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE, +	ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,  			"new I:%d Q:%d (i_coffd:%x q_coffd:%x)",  			i_coff, q_coff, i_coffd, q_coffd); @@ -1401,49 +1844,66 @@ ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)  	return 0;  } -/* - * Perform a PHY calibration +/** + * ath5k_hw_phy_calibrate() - Perform a PHY calibration + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * The main function we call from above to perform + * a short or full PHY calibration based on RF chip + * and current channel   */ -int ath5k_hw_phy_calibrate(struct ath5k_hw *ah, +int +ath5k_hw_phy_calibrate(struct ath5k_hw *ah,  		struct ieee80211_channel *channel)  {  	int ret;  	if (ah->ah_radio == AR5K_RF5110) -		ret = ath5k_hw_rf5110_calibrate(ah, channel); -	else { -		ret = ath5k_hw_rf511x_iq_calibrate(ah); -		ath5k_hw_request_rfgain_probe(ah); +		return ath5k_hw_rf5110_calibrate(ah, channel); + +	ret = ath5k_hw_rf511x_iq_calibrate(ah); +	if (ret) { +		ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE, +			"No I/Q correction performed (%uMHz)\n", +			channel->center_freq); + +		/* Happens all the time if there is not much +		 * traffic, consider it normal behaviour. */ +		ret = 0;  	} +	/* On full calibration request a PAPD probe for +	 * gainf calibration if needed */ +	if ((ah->ah_cal_mask & AR5K_CALIBRATION_FULL) && +	    (ah->ah_radio == AR5K_RF5111 || +	     ah->ah_radio == AR5K_RF5112) && +	    channel->hw_value != AR5K_MODE_11B) +		ath5k_hw_request_rfgain_probe(ah); + +	/* Update noise floor */ +	if (!(ah->ah_cal_mask & AR5K_CALIBRATION_NF)) +		ath5k_hw_update_noise_floor(ah); +  	return ret;  } +  /***************************\  * Spur mitigation functions *  \***************************/ -bool ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah, -				struct ieee80211_channel *channel) -{ -	u8 refclk_freq; - -	if ((ah->ah_radio == AR5K_RF5112) || -	(ah->ah_radio == AR5K_RF5413) || -	(ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4))) -		refclk_freq = 40; -	else -		refclk_freq = 32; - -	if ((channel->center_freq % refclk_freq != 0) && -	((channel->center_freq % refclk_freq < 10) || -	(channel->center_freq % refclk_freq > 22))) -		return true; -	else -		return false; -} - -void +/** + * ath5k_hw_set_spur_mitigation_filter() - Configure SPUR filter + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * + * This function gets called during PHY initialization to + * configure the spur filter for the given channel. Spur is noise + * generated due to "reflection" effects, for more information on this + * method check out patent US7643810 + */ +static void  ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,  				struct ieee80211_channel *channel)  { @@ -1459,7 +1919,7 @@ ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,  	/* Convert current frequency to fbin value (the same way channels  	 * are stored on EEPROM, check out ath5k_eeprom_bin2freq) and scale  	 * up by 2 so we can compare it later */ -	if (channel->hw_value & CHANNEL_2GHZ) { +	if (channel->band == IEEE80211_BAND_2GHZ) {  		chan_fbin = (channel->center_freq - 2300) * 10;  		freq_band = AR5K_EEPROM_BAND_2GHZ;  	} else { @@ -1472,7 +1932,7 @@ ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,  	spur_chan_fbin = AR5K_EEPROM_NO_SPUR;  	spur_detection_window = AR5K_SPUR_CHAN_WIDTH;  	/* XXX: Half/Quarter channels ?*/ -	if (channel->hw_value & CHANNEL_TURBO) +	if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)  		spur_detection_window *= 2;  	for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) { @@ -1501,32 +1961,45 @@ ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,  		 * Calculate deltas:  		 * spur_freq_sigma_delta -> spur_offset / sample_freq << 21  		 * spur_delta_phase -> spur_offset / chip_freq << 11 -		 * Note: Both values have 100KHz resolution +		 * Note: Both values have 100Hz resolution  		 */ -		/* XXX: Half/Quarter rate channels ? */ -		switch (channel->hw_value) { -		case CHANNEL_A: -			/* Both sample_freq and chip_freq are 40MHz */ -			spur_delta_phase = (spur_offset << 17) / 25; +		switch (ah->ah_bwmode) { +		case AR5K_BWMODE_40MHZ: +			/* Both sample_freq and chip_freq are 80MHz */ +			spur_delta_phase = (spur_offset << 16) / 25;  			spur_freq_sigma_delta = (spur_delta_phase >> 10); -			symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz; +			symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz * 2;  			break; -		case CHANNEL_G: -			/* sample_freq -> 40MHz chip_freq -> 44MHz -			 * (for b compatibility) */ -			spur_freq_sigma_delta = (spur_offset << 8) / 55; -			spur_delta_phase = (spur_offset << 17) / 25; -			symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz; +		case AR5K_BWMODE_10MHZ: +			/* Both sample_freq and chip_freq are 20MHz (?) */ +			spur_delta_phase = (spur_offset << 18) / 25; +			spur_freq_sigma_delta = (spur_delta_phase >> 10); +			symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz / 2;  			break; -		case CHANNEL_T: -		case CHANNEL_TG: -			/* Both sample_freq and chip_freq are 80MHz */ -			spur_delta_phase = (spur_offset << 16) / 25; +		case AR5K_BWMODE_5MHZ: +			/* Both sample_freq and chip_freq are 10MHz (?) */ +			spur_delta_phase = (spur_offset << 19) / 25;  			spur_freq_sigma_delta = (spur_delta_phase >> 10); -			symbol_width = AR5K_SPUR_SYMBOL_WIDTH_TURBO_100Hz; +			symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz / 4;  			break;  		default: -			return; +			if (channel->band == IEEE80211_BAND_5GHZ) { +				/* Both sample_freq and chip_freq are 40MHz */ +				spur_delta_phase = (spur_offset << 17) / 25; +				spur_freq_sigma_delta = +						(spur_delta_phase >> 10); +				symbol_width = +					AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz; +			} else { +				/* sample_freq -> 40MHz chip_freq -> 44MHz +				 * (for b compatibility) */ +				spur_delta_phase = (spur_offset << 17) / 25; +				spur_freq_sigma_delta = +						(spur_offset << 8) / 55; +				symbol_width = +					AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz; +			} +			break;  		}  		/* Calculate pilot and magnitude masks */ @@ -1629,7 +2102,7 @@ ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,  	} else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) &  	AR5K_PHY_IQ_SPUR_FILT_EN) { -		/* Clean up spur mitigation settings and disable fliter */ +		/* Clean up spur mitigation settings and disable filter */  		AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,  					AR5K_PHY_BIN_MASK_CTL_RATE, 0);  		AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_IQ, @@ -1666,77 +2139,78 @@ ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,  	}  } -/********************\ -  Misc PHY functions -\********************/ - -int ath5k_hw_phy_disable(struct ath5k_hw *ah) -{ -	/*Just a try M.F.*/ -	ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT); - -	return 0; -} - -/* - * Get the PHY Chip revision - */ -u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan) -{ -	unsigned int i; -	u32 srev; -	u16 ret; - -	/* -	 * Set the radio chip access register -	 */ -	switch (chan) { -	case CHANNEL_2GHZ: -		ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0)); -		break; -	case CHANNEL_5GHZ: -		ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0)); -		break; -	default: -		return 0; -	} - -	mdelay(2); - -	/* ...wait until PHY is ready and read the selected radio revision */ -	ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34)); - -	for (i = 0; i < 8; i++) -		ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20)); - -	if (ah->ah_version == AR5K_AR5210) { -		srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf; -		ret = (u16)ath5k_hw_bitswap(srev, 4) + 1; -	} else { -		srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff; -		ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) | -				((srev & 0x0f) << 4), 8); -	} - -	/* Reset to the 5GHz mode */ -	ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0)); - -	return ret; -}  /*****************\  * Antenna control *  \*****************/ -static void /*TODO:Boundary check*/ +/** + * DOC: Antenna control + * + * Hw supports up to 14 antennas ! I haven't found any card that implements + * that. The maximum number of antennas I've seen is up to 4 (2 for 2GHz and 2 + * for 5GHz). Antenna 1 (MAIN) should be omnidirectional, 2 (AUX) + * omnidirectional or sectorial and antennas 3-14 sectorial (or directional). + * + * We can have a single antenna for RX and multiple antennas for TX. + * RX antenna is our "default" antenna (usually antenna 1) set on + * DEFAULT_ANTENNA register and TX antenna is set on each TX control descriptor + * (0 for automatic selection, 1 - 14 antenna number). + * + * We can let hw do all the work doing fast antenna diversity for both + * tx and rx or we can do things manually. Here are the options we have + * (all are bits of STA_ID1 register): + * + * AR5K_STA_ID1_DEFAULT_ANTENNA -> When 0 is set as the TX antenna on TX + * control descriptor, use the default antenna to transmit or else use the last + * antenna on which we received an ACK. + * + * AR5K_STA_ID1_DESC_ANTENNA -> Update default antenna after each TX frame to + * the antenna on which we got the ACK for that frame. + * + * AR5K_STA_ID1_RTS_DEF_ANTENNA -> Use default antenna for RTS or else use the + * one on the TX descriptor. + * + * AR5K_STA_ID1_SELFGEN_DEF_ANT -> Use default antenna for self generated frames + * (ACKs etc), or else use current antenna (the one we just used for TX). + * + * Using the above we support the following scenarios: + * + * AR5K_ANTMODE_DEFAULT -> Hw handles antenna diversity etc automatically + * + * AR5K_ANTMODE_FIXED_A	-> Only antenna A (MAIN) is present + * + * AR5K_ANTMODE_FIXED_B	-> Only antenna B (AUX) is present + * + * AR5K_ANTMODE_SINGLE_AP -> Sta locked on a single ap + * + * AR5K_ANTMODE_SECTOR_AP -> AP with tx antenna set on tx desc + * + * AR5K_ANTMODE_SECTOR_STA -> STA with tx antenna set on tx desc + * + * AR5K_ANTMODE_DEBUG Debug mode -A -> Rx, B-> Tx- + * + * Also note that when setting antenna to F on tx descriptor card inverts + * current tx antenna. + */ + +/** + * ath5k_hw_set_def_antenna() - Set default rx antenna on AR5211/5212 and newer + * @ah: The &struct ath5k_hw + * @ant: Antenna number + */ +static void  ath5k_hw_set_def_antenna(struct ath5k_hw *ah, u8 ant)  {  	if (ah->ah_version != AR5K_AR5210)  		ath5k_hw_reg_write(ah, ant & 0x7, AR5K_DEFAULT_ANTENNA);  } -/* - * Enable/disable fast rx antenna diversity +/** + * ath5k_hw_set_fast_div() -  Enable/disable fast rx antenna diversity + * @ah: The &struct ath5k_hw + * @ee_mode: One of enum ath5k_driver_mode + * @enable: True to enable, false to disable   */  static void  ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable) @@ -1776,6 +2250,14 @@ ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable)  	}  } +/** + * ath5k_hw_set_antenna_switch() - Set up antenna switch table + * @ah: The &struct ath5k_hw + * @ee_mode: One of enum ath5k_driver_mode + * + * Switch table comes from EEPROM and includes information on controlling + * the 2 antenna RX attenuators + */  void  ath5k_hw_set_antenna_switch(struct ath5k_hw *ah, u8 ee_mode)  { @@ -1807,8 +2289,10 @@ ath5k_hw_set_antenna_switch(struct ath5k_hw *ah, u8 ee_mode)  		AR5K_PHY_ANT_SWITCH_TABLE_1);  } -/* - * Set antenna operating mode +/** + * ath5k_hw_set_antenna_mode() -  Set antenna operating mode + * @ah: The &struct ath5k_hw + * @ant_mode: One of enum ath5k_ant_mode   */  void  ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode) @@ -1816,7 +2300,8 @@ ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)  	struct ieee80211_channel *channel = ah->ah_current_channel;  	bool use_def_for_tx, update_def_on_tx, use_def_for_rts, fast_div;  	bool use_def_for_sg; -	u8 def_ant, tx_ant, ee_mode; +	int ee_mode; +	u8 def_ant, tx_ant;  	u32 sta_id1 = 0;  	/* if channel is not initialized yet we can't set the antennas @@ -1828,24 +2313,7 @@ ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)  	def_ant = ah->ah_def_ant; -	switch (channel->hw_value & CHANNEL_MODES) { -	case CHANNEL_A: -	case CHANNEL_T: -	case CHANNEL_XR: -		ee_mode = AR5K_EEPROM_MODE_11A; -		break; -	case CHANNEL_G: -	case CHANNEL_TG: -		ee_mode = AR5K_EEPROM_MODE_11G; -		break; -	case CHANNEL_B: -		ee_mode = AR5K_EEPROM_MODE_11B; -		break; -	default: -		ATH5K_ERR(ah->ah_sc, -			"invalid channel: %d\n", channel->center_freq); -		return; -	} +	ee_mode = ath5k_eeprom_mode_from_channel(ah, channel);  	switch (ant_mode) {  	case AR5K_ANTMODE_DEFAULT: @@ -1942,8 +2410,13 @@ ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)   * Helper functions   */ -/* - * Do linear interpolation between two given (x, y) points +/** + * ath5k_get_interpolated_value() - Get interpolated Y val between two points + * @target: X value of the middle point + * @x_left: X value of the left point + * @x_right: X value of the right point + * @y_left: Y value of the left point + * @y_right: Y value of the right point   */  static s16  ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right, @@ -1962,7 +2435,7 @@ ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,  	 * always 1 instead of 1.25, 1.75 etc). We scale up by 100  	 * to have some accuracy both for 0.5 and 0.25 steps.  	 */ -	ratio = ((100 * y_right - 100 * y_left)/(x_right - x_left)); +	ratio = ((100 * y_right - 100 * y_left) / (x_right - x_left));  	/* Now scale down to be in range */  	result = y_left + (ratio * (target - x_left) / 100); @@ -1970,13 +2443,18 @@ ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,  	return result;  } -/* - * Find vertical boundary (min pwr) for the linear PCDAC curve. +/** + * ath5k_get_linear_pcdac_min() - Find vertical boundary (min pwr) for the + * linear PCDAC curve + * @stepL: Left array with y values (pcdac steps) + * @stepR: Right array with y values (pcdac steps) + * @pwrL: Left array with x values (power steps) + * @pwrR: Right array with x values (power steps)   *   * Since we have the top of the curve and we draw the line below   * until we reach 1 (1 pcdac step) we need to know which point - * (x value) that is so that we don't go below y axis and have negative - * pcdac values when creating the curve, or fill the table with zeroes. + * (x value) that is so that we don't go below x axis and have negative + * pcdac values when creating the curve, or fill the table with zeros.   */  static s16  ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR, @@ -2022,7 +2500,16 @@ ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,  	return max(min_pwrL, min_pwrR);  } -/* +/** + * ath5k_create_power_curve() - Create a Power to PDADC or PCDAC curve + * @pmin: Minimum power value (xmin) + * @pmax: Maximum power value (xmax) + * @pwr: Array of power steps (x values) + * @vpd: Array of matching PCDAC/PDADC steps (y values) + * @num_points: Number of provided points + * @vpd_table: Array to fill with the full PCDAC/PDADC values (y values) + * @type: One of enum ath5k_powertable_type (eeprom.h) + *   * Interpolate (pwr,vpd) points to create a Power to PDADC or a   * Power to PCDAC curve.   * @@ -2041,7 +2528,7 @@ ath5k_create_power_curve(s16 pmin, s16 pmax,  			u8 *vpd_table, u8 type)  {  	u8 idx[2] = { 0, 1 }; -	s16 pwr_i = 2*pmin; +	s16 pwr_i = 2 * pmin;  	int i;  	if (num_points < 2) @@ -2080,10 +2567,17 @@ ath5k_create_power_curve(s16 pmin, s16 pmax,  	}  } -/* +/** + * ath5k_get_chan_pcal_surrounding_piers() - Get surrounding calibration piers + * for a given channel. + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * @pcinfo_l: The &struct ath5k_chan_pcal_info to put the left cal. pier + * @pcinfo_r: The &struct ath5k_chan_pcal_info to put the right cal. pier + *   * Get the surrounding per-channel power calibration piers   * for a given frequency so that we can interpolate between - * them and come up with an apropriate dataset for our current + * them and come up with an appropriate dataset for our current   * channel.   */  static void @@ -2101,15 +2595,20 @@ ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,  	idx_l = 0;  	idx_r = 0; -	if (!(channel->hw_value & CHANNEL_OFDM)) { +	switch (channel->hw_value) { +	case AR5K_EEPROM_MODE_11A: +		pcinfo = ee->ee_pwr_cal_a; +		mode = AR5K_EEPROM_MODE_11A; +		break; +	case AR5K_EEPROM_MODE_11B:  		pcinfo = ee->ee_pwr_cal_b;  		mode = AR5K_EEPROM_MODE_11B; -	} else if (channel->hw_value & CHANNEL_2GHZ) { +		break; +	case AR5K_EEPROM_MODE_11G: +	default:  		pcinfo = ee->ee_pwr_cal_g;  		mode = AR5K_EEPROM_MODE_11G; -	} else { -		pcinfo = ee->ee_pwr_cal_a; -		mode = AR5K_EEPROM_MODE_11A; +		break;  	}  	max = ee->ee_n_piers[mode] - 1; @@ -2158,11 +2657,17 @@ done:  	*pcinfo_r = &pcinfo[idx_r];  } -/* +/** + * ath5k_get_rate_pcal_data() - Get the interpolated per-rate power + * calibration data + * @ah: The &struct ath5k_hw *ah, + * @channel: The &struct ieee80211_channel + * @rates: The &struct ath5k_rate_pcal_info to fill + *   * Get the surrounding per-rate power calibration data   * for a given frequency and interpolate between power   * values to set max target power supported by hw for - * each rate. + * each rate on this frequency.   */  static void  ath5k_get_rate_pcal_data(struct ath5k_hw *ah, @@ -2178,15 +2683,20 @@ ath5k_get_rate_pcal_data(struct ath5k_hw *ah,  	idx_l = 0;  	idx_r = 0; -	if (!(channel->hw_value & CHANNEL_OFDM)) { +	switch (channel->hw_value) { +	case AR5K_MODE_11A: +		rpinfo = ee->ee_rate_tpwr_a; +		mode = AR5K_EEPROM_MODE_11A; +		break; +	case AR5K_MODE_11B:  		rpinfo = ee->ee_rate_tpwr_b;  		mode = AR5K_EEPROM_MODE_11B; -	} else if (channel->hw_value & CHANNEL_2GHZ) { +		break; +	case AR5K_MODE_11G: +	default:  		rpinfo = ee->ee_rate_tpwr_g;  		mode = AR5K_EEPROM_MODE_11G; -	} else { -		rpinfo = ee->ee_rate_tpwr_a; -		mode = AR5K_EEPROM_MODE_11A; +		break;  	}  	max = ee->ee_rate_target_pwr_num[mode] - 1; @@ -2245,7 +2755,11 @@ done:  					rpinfo[idx_r].target_power_54);  } -/* +/** + * ath5k_get_max_ctl_power() - Get max edge power for a given frequency + * @ah: the &struct ath5k_hw + * @channel: The &struct ieee80211_channel + *   * Get the max edge power for this channel if   * we have such data from EEPROM's Conformance Test   * Limits (CTL), and limit max power if needed. @@ -2267,24 +2781,22 @@ ath5k_get_max_ctl_power(struct ath5k_hw *ah,  	ctl_mode = ath_regd_get_band_ctl(regulatory, channel->band); -	switch (channel->hw_value & CHANNEL_MODES) { -	case CHANNEL_A: -		ctl_mode |= AR5K_CTL_11A; +	switch (channel->hw_value) { +	case AR5K_MODE_11A: +		if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) +			ctl_mode |= AR5K_CTL_TURBO; +		else +			ctl_mode |= AR5K_CTL_11A;  		break; -	case CHANNEL_G: -		ctl_mode |= AR5K_CTL_11G; +	case AR5K_MODE_11G: +		if (ah->ah_bwmode == AR5K_BWMODE_40MHZ) +			ctl_mode |= AR5K_CTL_TURBOG; +		else +			ctl_mode |= AR5K_CTL_11G;  		break; -	case CHANNEL_B: +	case AR5K_MODE_11B:  		ctl_mode |= AR5K_CTL_11B;  		break; -	case CHANNEL_T: -		ctl_mode |= AR5K_CTL_TURBO; -		break; -	case CHANNEL_TG: -		ctl_mode |= AR5K_CTL_TURBOG; -		break; -	case CHANNEL_XR: -		/* Fall through */  	default:  		return;  	} @@ -2319,7 +2831,7 @@ ath5k_get_max_ctl_power(struct ath5k_hw *ah,  	}  	if (edge_pwr) -		ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr); +		ah->ah_txpower.txp_max_pwr = 4 * min(edge_pwr, max_chan_pwr);  } @@ -2327,8 +2839,39 @@ ath5k_get_max_ctl_power(struct ath5k_hw *ah,   * Power to PCDAC table functions   */ -/* - * Fill Power to PCDAC table on RF5111 +/** + * DOC: Power to PCDAC table functions + * + * For RF5111 we have an XPD -eXternal Power Detector- curve + * for each calibrated channel. Each curve has 0,5dB Power steps + * on x axis and PCDAC steps (offsets) on y axis and looks like an + * exponential function. To recreate the curve we read 11 points + * from eeprom (eeprom.c) and interpolate here. + * + * For RF5112 we have 4 XPD -eXternal Power Detector- curves + * for each calibrated channel on 0, -6, -12 and -18dBm but we only + * use the higher (3) and the lower (0) curves. Each curve again has 0.5dB + * power steps on x axis and PCDAC steps on y axis and looks like a + * linear function. To recreate the curve and pass the power values + * on hw, we get 4 points for xpd 0 (lower gain -> max power) + * and 3 points for xpd 3 (higher gain -> lower power) from eeprom (eeprom.c) + * and interpolate here. + * + * For a given channel we get the calibrated points (piers) for it or + * -if we don't have calibration data for this specific channel- from the + * available surrounding channels we have calibration data for, after we do a + * linear interpolation between them. Then since we have our calibrated points + * for this channel, we do again a linear interpolation between them to get the + * whole curve. + * + * We finally write the Y values of the curve(s) (the PCDAC values) on hw + */ + +/** + * ath5k_fill_pwr_to_pcdac_table() - Fill Power to PCDAC table on RF5111 + * @ah: The &struct ath5k_hw + * @table_min: Minimum power (x min) + * @table_max: Maximum power (x max)   *   * No further processing is needed for RF5111, the only thing we have to   * do is fill the values below and above calibration range since eeprom data @@ -2338,7 +2881,7 @@ static void  ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,  							s16 *table_max)  { -	u8 	*pcdac_out = ah->ah_txpower.txp_pd_table; +	u8	*pcdac_out = ah->ah_txpower.txp_pd_table;  	u8	*pcdac_tmp = ah->ah_txpower.tmpL[0];  	u8	pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;  	s16	min_pwr, max_pwr; @@ -2357,8 +2900,8 @@ ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,  	/* Copy values from pcdac_tmp */  	pwr_idx = min_pwr; -	for (i = 0 ; pwr_idx <= max_pwr && -	pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) { +	for (i = 0; pwr_idx <= max_pwr && +		    pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {  		pcdac_out[pcdac_i++] = pcdac_tmp[i];  		pwr_idx++;  	} @@ -2369,10 +2912,14 @@ ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,  } -/* - * Combine available XPD Curves and fill Linear Power to PCDAC table - * on RF5112 +/** + * ath5k_combine_linear_pcdac_curves() - Combine available PCDAC Curves + * @ah: The &struct ath5k_hw + * @table_min: Minimum power (x min) + * @table_max: Maximum power (x max) + * @pdcurves: Number of pd curves   * + * Combine available XPD Curves and fill Linear Power to PCDAC table on RF5112   * RFX112 can have up to 2 curves (one for low txpower range and one for   * higher txpower range). We need to put them both on pcdac_out and place   * them in the correct location. In case we only have one curve available @@ -2384,7 +2931,7 @@ static void  ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,  						s16 *table_max, u8 pdcurves)  { -	u8 	*pcdac_out = ah->ah_txpower.txp_pd_table; +	u8	*pcdac_out = ah->ah_txpower.txp_pd_table;  	u8	*pcdac_low_pwr;  	u8	*pcdac_high_pwr;  	u8	*pcdac_tmp; @@ -2392,8 +2939,8 @@ ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,  	s16	max_pwr_idx;  	s16	min_pwr_idx;  	s16	mid_pwr_idx = 0; -	/* Edge flag turs on the 7nth bit on the PCDAC -	 * to delcare the higher power curve (force values +	/* Edge flag turns on the 7nth bit on the PCDAC +	 * to declare the higher power curve (force values  	 * to be greater than 64). If we only have one curve  	 * we don't need to set this, if we have 2 curves and  	 * fill the table backwards this can also be used to @@ -2434,7 +2981,7 @@ ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,  	}  	/* This is used when setting tx power*/ -	ah->ah_txpower.txp_min_idx = min_pwr_idx/2; +	ah->ah_txpower.txp_min_idx = min_pwr_idx / 2;  	/* Fill Power to PCDAC table backwards */  	pwr = max_pwr_idx; @@ -2443,14 +2990,14 @@ ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,  		 * edge flag and set pcdac_tmp to lower  		 * power curve.*/  		if (edge_flag == 0x40 && -		(2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) { +		(2 * pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {  			edge_flag = 0x00;  			pcdac_tmp = pcdac_low_pwr; -			pwr = mid_pwr_idx/2; +			pwr = mid_pwr_idx / 2;  		}  		/* Don't go below 1, extrapolate below if we have -		 * already swithced to the lower power curve -or +		 * already switched to the lower power curve -or  		 * we only have one curve and edge_flag is zero  		 * anyway */  		if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) { @@ -2474,11 +3021,14 @@ ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,  	}  } -/* Write PCDAC values on hw */ +/** + * ath5k_write_pcdac_table() - Write the PCDAC values on hw + * @ah: The &struct ath5k_hw + */  static void -ath5k_setup_pcdac_table(struct ath5k_hw *ah) +ath5k_write_pcdac_table(struct ath5k_hw *ah)  { -	u8 	*pcdac_out = ah->ah_txpower.txp_pd_table; +	u8	*pcdac_out = ah->ah_txpower.txp_pd_table;  	int	i;  	/* @@ -2486,8 +3036,8 @@ ath5k_setup_pcdac_table(struct ath5k_hw *ah)  	 */  	for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {  		ath5k_hw_reg_write(ah, -			(((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) | -			(((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16), +			(((pcdac_out[2 * i + 0] << 8 | 0xff) & 0xffff) << 0) | +			(((pcdac_out[2 * i + 1] << 8 | 0xff) & 0xffff) << 16),  			AR5K_PHY_PCDAC_TXPOWER(i));  	}  } @@ -2497,10 +3047,33 @@ ath5k_setup_pcdac_table(struct ath5k_hw *ah)   * Power to PDADC table functions   */ -/* - * Set the gain boundaries and create final Power to PDADC table +/** + * DOC: Power to PDADC table functions + * + * For RF2413 and later we have a Power to PDADC table (Power Detector) + * instead of a PCDAC (Power Control) and 4 pd gain curves for each + * calibrated channel. Each curve has power on x axis in 0.5 db steps and + * PDADC steps on y axis and looks like an exponential function like the + * RF5111 curve. + * + * To recreate the curves we read the points from eeprom (eeprom.c) + * and interpolate here. Note that in most cases only 2 (higher and lower) + * curves are used (like RF5112) but vendors have the opportunity to include + * all 4 curves on eeprom. The final curve (higher power) has an extra + * point for better accuracy like RF5112.   * - * We can have up to 4 pd curves, we need to do a simmilar process + * The process is similar to what we do above for RF5111/5112 + */ + +/** + * ath5k_combine_pwr_to_pdadc_curves() - Combine the various PDADC curves + * @ah: The &struct ath5k_hw + * @pwr_min: Minimum power (x min) + * @pwr_max: Maximum power (x max) + * @pdcurves: Number of available curves + * + * Combine the various pd curves and create the final Power to PDADC table + * We can have up to 4 pd curves, we need to do a similar process   * as we do for RF5112. This time we don't have an edge_flag but we   * set the gain boundaries on a separate register.   */ @@ -2623,12 +3196,18 @@ ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,  } -/* Write PDADC values on hw */ +/** + * ath5k_write_pwr_to_pdadc_table() - Write the PDADC values on hw + * @ah: The &struct ath5k_hw + * @ee_mode: One of enum ath5k_driver_mode + */  static void -ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah, -			u8 pdcurves, u8 *pdg_to_idx) +ath5k_write_pwr_to_pdadc_table(struct ath5k_hw *ah, u8 ee_mode)  { +	struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;  	u8 *pdadc_out = ah->ah_txpower.txp_pd_table; +	u8 *pdg_to_idx = ee->ee_pdc_to_idx[ee_mode]; +	u8 pdcurves = ee->ee_pd_gains[ee_mode];  	u32 reg;  	u8 i; @@ -2668,12 +3247,8 @@ ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,  	 * Write TX power values  	 */  	for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) { -		ath5k_hw_reg_write(ah, -			((pdadc_out[4*i + 0] & 0xff) << 0) | -			((pdadc_out[4*i + 1] & 0xff) << 8) | -			((pdadc_out[4*i + 2] & 0xff) << 16) | -			((pdadc_out[4*i + 3] & 0xff) << 24), -			AR5K_PHY_PDADC_TXPOWER(i)); +		u32 val = get_unaligned_le32(&pdadc_out[4 * i]); +		ath5k_hw_reg_write(ah, val, AR5K_PHY_PDADC_TXPOWER(i));  	}  } @@ -2682,10 +3257,16 @@ ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,   * Common code for PCDAC/PDADC tables   */ -/* +/** + * ath5k_setup_channel_powertable() - Set up power table for this channel + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * @ee_mode: One of enum ath5k_driver_mode + * @type: One of enum ath5k_powertable_type (eeprom.h) + *   * This is the main function that uses all of the above   * to set PCDAC/PDADC table on hw for the current channel. - * This table is used for tx power calibration on the basband, + * This table is used for tx power calibration on the baseband,   * without it we get weird tx power levels and in some cases   * distorted spectral mask   */ @@ -2706,13 +3287,13 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  	u32 target = channel->center_freq;  	int pdg, i; -	/* Get surounding freq piers for this channel */ +	/* Get surrounding freq piers for this channel */  	ath5k_get_chan_pcal_surrounding_piers(ah, channel,  						&pcinfo_L,  						&pcinfo_R);  	/* Loop over pd gain curves on -	 * surounding freq piers by index */ +	 * surrounding freq piers by index */  	for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {  		/* Fill curves in reverse order @@ -2803,7 +3384,7 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  		}  		/* Interpolate between curves -		 * of surounding freq piers to +		 * of surrounding freq piers to  		 * get the final curve for this  		 * pd gain. Re-use tmpL for interpolation  		 * output */ @@ -2827,7 +3408,7 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  	/* Fill min and max power levels for this  	 * channel by interpolating the values on -	 * surounding channels to complete the dataset */ +	 * surrounding channels to complete the dataset */  	ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,  					(s16) pcinfo_L->freq,  					(s16) pcinfo_R->freq, @@ -2838,8 +3419,7 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  					(s16) pcinfo_R->freq,  					pcinfo_L->max_pwr, pcinfo_R->max_pwr); -	/* We are ready to go, fill PCDAC/PDADC -	 * table and write settings on hardware */ +	/* Fill PCDAC/PDADC table */  	switch (type) {  	case AR5K_PWRTABLE_LINEAR_PCDAC:  		/* For RF5112 we can have one or two curves @@ -2852,9 +3432,6 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  		 * match max power value with max  		 * table index */  		ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2); - -		/* Write settings on hw */ -		ath5k_setup_pcdac_table(ah);  		break;  	case AR5K_PWRTABLE_PWR_TO_PCDAC:  		/* We are done for RF5111 since it has only @@ -2864,9 +3441,6 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  		/* No rate powertable adjustment for RF5111 */  		ah->ah_txpower.txp_min_idx = 0;  		ah->ah_txpower.txp_offset = 0; - -		/* Write settings on hw */ -		ath5k_setup_pcdac_table(ah);  		break;  	case AR5K_PWRTABLE_PWR_TO_PDADC:  		/* Set PDADC boundaries and fill @@ -2874,9 +3448,6 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  		ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,  						ee->ee_pd_gains[ee_mode]); -		/* Write settings on hw */ -		ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx); -  		/* Set txp.offset, note that table_min  		 * can be negative */  		ah->ah_txpower.txp_offset = table_min[0]; @@ -2885,32 +3456,56 @@ ath5k_setup_channel_powertable(struct ath5k_hw *ah,  		return -EINVAL;  	} +	ah->ah_txpower.txp_setup = true; +  	return 0;  } +/** + * ath5k_write_channel_powertable() - Set power table for current channel on hw + * @ah: The &struct ath5k_hw + * @ee_mode: One of enum ath5k_driver_mode + * @type: One of enum ath5k_powertable_type (eeprom.h) + */ +static void +ath5k_write_channel_powertable(struct ath5k_hw *ah, u8 ee_mode, u8 type) +{ +	if (type == AR5K_PWRTABLE_PWR_TO_PDADC) +		ath5k_write_pwr_to_pdadc_table(ah, ee_mode); +	else +		ath5k_write_pcdac_table(ah); +} + -/* - * Per-rate tx power setting +/** + * DOC: Per-rate tx power setting   * - * This is the code that sets the desired tx power (below + * This is the code that sets the desired tx power limit (below   * maximum) on hw for each rate (we also have TPC that sets - * power per packet). We do that by providing an index on the - * PCDAC/PDADC table we set up. - */ - -/* - * Set rate power table + * power per packet type). We do that by providing an index on the + * PCDAC/PDADC table we set up above, for each rate.   *   * For now we only limit txpower based on maximum tx power - * supported by hw (what's inside rate_info). We need to limit - * this even more, based on regulatory domain etc. + * supported by hw (what's inside rate_info) + conformance test + * limits. We need to limit this even more, based on regulatory domain + * etc to be safe. Normally this is done from above so we don't care + * here, all we care is that the tx power we set will be O.K. + * for the hw (e.g. won't create noise on PA etc).   * - * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps) - * and is indexed as follows: + * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps - + * x values) and is indexed as follows:   * rates[0] - rates[7] -> OFDM rates   * rates[8] - rates[14] -> CCK rates   * rates[15] -> XR rates (they all have the same power)   */ + +/** + * ath5k_setup_rate_powertable() - Set up rate power table for a given tx power + * @ah: The &struct ath5k_hw + * @max_pwr: The maximum tx power requested in 0.5dB steps + * @rate_info: The &struct ath5k_rate_pcal_info to fill + * @ee_mode: One of enum ath5k_driver_mode + */  static void  ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,  			struct ath5k_rate_pcal_info *rate_info, @@ -2918,6 +3513,7 @@ ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,  {  	unsigned int i;  	u16 *rates; +	s16 rate_idx_scaled = 0;  	/* max_pwr is power level we got from driver/user in 0.5dB  	 * units, switch to 0.25dB units so we can compare */ @@ -2964,47 +3560,66 @@ ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,  		for (i = 8; i <= 15; i++)  			rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta; +	/* Save min/max and current tx power for this channel +	 * in 0.25dB units. +	 * +	 * Note: We use rates[0] for current tx power because +	 * it covers most of the rates, in most cases. It's our +	 * tx power limit and what the user expects to see. */ +	ah->ah_txpower.txp_min_pwr = 2 * rates[7]; +	ah->ah_txpower.txp_cur_pwr = 2 * rates[0]; + +	/* Set max txpower for correct OFDM operation on all rates +	 * -that is the txpower for 54Mbit-, it's used for the PAPD +	 * gain probe and it's in 0.5dB units */ +	ah->ah_txpower.txp_ofdm = rates[7]; +  	/* Now that we have all rates setup use table offset to  	 * match the power range set by user with the power indices  	 * on PCDAC/PDADC table */  	for (i = 0; i < 16; i++) { -		rates[i] += ah->ah_txpower.txp_offset; +		rate_idx_scaled = rates[i] + ah->ah_txpower.txp_offset;  		/* Don't get out of bounds */ -		if (rates[i] > 63) -			rates[i] = 63; +		if (rate_idx_scaled > 63) +			rate_idx_scaled = 63; +		if (rate_idx_scaled < 0) +			rate_idx_scaled = 0; +		rates[i] = rate_idx_scaled;  	} - -	/* Min/max in 0.25dB units */ -	ah->ah_txpower.txp_min_pwr = 2 * rates[7]; -	ah->ah_txpower.txp_max_pwr = 2 * rates[0]; -	ah->ah_txpower.txp_ofdm = rates[7];  } -/* - * Set transmission power +/** + * ath5k_hw_txpower() - Set transmission power limit for a given channel + * @ah: The &struct ath5k_hw + * @channel: The &struct ieee80211_channel + * @txpower: Requested tx power in 0.5dB steps + * + * Combines all of the above to set the requested tx power limit + * on hw.   */ -int +static int  ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel, -		u8 ee_mode, u8 txpower) +		 u8 txpower)  {  	struct ath5k_rate_pcal_info rate_info; +	struct ieee80211_channel *curr_channel = ah->ah_current_channel; +	int ee_mode;  	u8 type;  	int ret;  	if (txpower > AR5K_TUNE_MAX_TXPOWER) { -		ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower); +		ATH5K_ERR(ah, "invalid tx power: %u\n", txpower);  		return -EINVAL;  	} -	/* Reset TX power values */ -	memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower)); -	ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER; -	ah->ah_txpower.txp_min_pwr = 0; -	ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER; +	ee_mode = ath5k_eeprom_mode_from_channel(ah, channel);  	/* Initialize TX power table */  	switch (ah->ah_radio) { +	case AR5K_RF5110: +		/* TODO */ +		return 0;  	case AR5K_RF5111:  		type = AR5K_PWRTABLE_PWR_TO_PCDAC;  		break; @@ -3022,10 +3637,33 @@ ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,  		return -EINVAL;  	} -	/* FIXME: Only on channel/mode change */ -	ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type); -	if (ret) -		return ret; +	/* +	 * If we don't change channel/mode skip tx powertable calculation +	 * and use the cached one. +	 */ +	if (!ah->ah_txpower.txp_setup || +	    (channel->hw_value != curr_channel->hw_value) || +	    (channel->center_freq != curr_channel->center_freq)) { +		/* Reset TX power values but preserve requested +		 * tx power from above */ +		int requested_txpower = ah->ah_txpower.txp_requested; + +		memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower)); + +		/* Restore TPC setting and requested tx power */ +		ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER; + +		ah->ah_txpower.txp_requested = requested_txpower; + +		/* Calculate the powertable */ +		ret = ath5k_setup_channel_powertable(ah, channel, +							ee_mode, type); +		if (ret) +			return ret; +	} + +	/* Write table on hw */ +	ath5k_write_channel_powertable(ah, ee_mode, type);  	/* Limit max power if we have a CTL available */  	ath5k_get_max_ctl_power(ah, channel); @@ -3036,7 +3674,7 @@ ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,  	/* FIXME: TPC scale reduction */ -	/* Get surounding channels for per-rate power table +	/* Get surrounding channels for per-rate power table  	 * calibration */  	ath5k_get_rate_pcal_data(ah, channel, &rate_info); @@ -3071,40 +3709,257 @@ ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,  			AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),  			AR5K_TPC);  	} else { -		ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX | -			AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX); +		ath5k_hw_reg_write(ah, AR5K_TUNE_MAX_TXPOWER, +			AR5K_PHY_TXPOWER_RATE_MAX);  	}  	return 0;  } -int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower) +/** + * ath5k_hw_set_txpower_limit() - Set txpower limit for the current channel + * @ah: The &struct ath5k_hw + * @txpower: The requested tx power limit in 0.5dB steps + * + * This function provides access to ath5k_hw_txpower to the driver in + * case user or an application changes it while PHY is running. + */ +int +ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower)  { -	/*Just a try M.F.*/ -	struct ieee80211_channel *channel = ah->ah_current_channel; -	u8 ee_mode; +	ATH5K_DBG(ah, ATH5K_DEBUG_TXPOWER, +		"changing txpower to %d\n", txpower); -	switch (channel->hw_value & CHANNEL_MODES) { -	case CHANNEL_A: -	case CHANNEL_T: -	case CHANNEL_XR: -		ee_mode = AR5K_EEPROM_MODE_11A; -		break; -	case CHANNEL_G: -	case CHANNEL_TG: -		ee_mode = AR5K_EEPROM_MODE_11G; -		break; -	case CHANNEL_B: -		ee_mode = AR5K_EEPROM_MODE_11B; -		break; -	default: -		ATH5K_ERR(ah->ah_sc, -			"invalid channel: %d\n", channel->center_freq); +	return ath5k_hw_txpower(ah, ah->ah_current_channel, txpower); +} + + +/*************\ + Init function +\*************/ + +/** + * ath5k_hw_phy_init() - Initialize PHY + * @ah: The &struct ath5k_hw + * @channel: The @struct ieee80211_channel + * @mode: One of enum ath5k_driver_mode + * @fast: Try a fast channel switch instead + * + * This is the main function used during reset to initialize PHY + * or do a fast channel change if possible. + * + * NOTE: Do not call this one from the driver, it assumes PHY is in a + * warm reset state ! + */ +int +ath5k_hw_phy_init(struct ath5k_hw *ah, struct ieee80211_channel *channel, +		      u8 mode, bool fast) +{ +	struct ieee80211_channel *curr_channel; +	int ret, i; +	u32 phy_tst1; +	ret = 0; + +	/* +	 * Sanity check for fast flag +	 * Don't try fast channel change when changing modulation +	 * mode/band. We check for chip compatibility on +	 * ath5k_hw_reset. +	 */ +	curr_channel = ah->ah_current_channel; +	if (fast && (channel->hw_value != curr_channel->hw_value))  		return -EINVAL; + +	/* +	 * On fast channel change we only set the synth parameters +	 * while PHY is running, enable calibration and skip the rest. +	 */ +	if (fast) { +		AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_RFBUS_REQ, +				    AR5K_PHY_RFBUS_REQ_REQUEST); +		for (i = 0; i < 100; i++) { +			if (ath5k_hw_reg_read(ah, AR5K_PHY_RFBUS_GRANT)) +				break; +			udelay(5); +		} +		/* Failed */ +		if (i >= 100) +			return -EIO; + +		/* Set channel and wait for synth */ +		ret = ath5k_hw_channel(ah, channel); +		if (ret) +			return ret; + +		ath5k_hw_wait_for_synth(ah, channel);  	} -	ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER, -		"changing txpower to %d\n", txpower); +	/* +	 * Set TX power +	 * +	 * Note: We need to do that before we set +	 * RF buffer settings on 5211/5212+ so that we +	 * properly set curve indices. +	 */ +	ret = ath5k_hw_txpower(ah, channel, ah->ah_txpower.txp_requested ? +					ah->ah_txpower.txp_requested * 2 : +					AR5K_TUNE_MAX_TXPOWER); +	if (ret) +		return ret; + +	/* Write OFDM timings on 5212*/ +	if (ah->ah_version == AR5K_AR5212 && +		channel->hw_value != AR5K_MODE_11B) { -	return ath5k_hw_txpower(ah, channel, ee_mode, txpower); +		ret = ath5k_hw_write_ofdm_timings(ah, channel); +		if (ret) +			return ret; + +		/* Spur info is available only from EEPROM versions +		 * greater than 5.3, but the EEPROM routines will use +		 * static values for older versions */ +		if (ah->ah_mac_srev >= AR5K_SREV_AR5424) +			ath5k_hw_set_spur_mitigation_filter(ah, +							    channel); +	} + +	/* If we used fast channel switching +	 * we are done, release RF bus and +	 * fire up NF calibration. +	 * +	 * Note: Only NF calibration due to +	 * channel change, not AGC calibration +	 * since AGC is still running ! +	 */ +	if (fast) { +		/* +		 * Release RF Bus grant +		 */ +		AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_RFBUS_REQ, +				    AR5K_PHY_RFBUS_REQ_REQUEST); + +		/* +		 * Start NF calibration +		 */ +		AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, +					AR5K_PHY_AGCCTL_NF); + +		return ret; +	} + +	/* +	 * For 5210 we do all initialization using +	 * initvals, so we don't have to modify +	 * any settings (5210 also only supports +	 * a/aturbo modes) +	 */ +	if (ah->ah_version != AR5K_AR5210) { + +		/* +		 * Write initial RF gain settings +		 * This should work for both 5111/5112 +		 */ +		ret = ath5k_hw_rfgain_init(ah, channel->band); +		if (ret) +			return ret; + +		usleep_range(1000, 1500); + +		/* +		 * Write RF buffer +		 */ +		ret = ath5k_hw_rfregs_init(ah, channel, mode); +		if (ret) +			return ret; + +		/*Enable/disable 802.11b mode on 5111 +		(enable 2111 frequency converter + CCK)*/ +		if (ah->ah_radio == AR5K_RF5111) { +			if (mode == AR5K_MODE_11B) +				AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, +				    AR5K_TXCFG_B_MODE); +			else +				AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG, +				    AR5K_TXCFG_B_MODE); +		} + +	} else if (ah->ah_version == AR5K_AR5210) { +		usleep_range(1000, 1500); +		/* Disable phy and wait */ +		ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT); +		usleep_range(1000, 1500); +	} + +	/* Set channel on PHY */ +	ret = ath5k_hw_channel(ah, channel); +	if (ret) +		return ret; + +	/* +	 * Enable the PHY and wait until completion +	 * This includes BaseBand and Synthesizer +	 * activation. +	 */ +	ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT); + +	ath5k_hw_wait_for_synth(ah, channel); + +	/* +	 * Perform ADC test to see if baseband is ready +	 * Set tx hold and check adc test register +	 */ +	phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1); +	ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1); +	for (i = 0; i <= 20; i++) { +		if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10)) +			break; +		usleep_range(200, 250); +	} +	ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1); + +	/* +	 * Start automatic gain control calibration +	 * +	 * During AGC calibration RX path is re-routed to +	 * a power detector so we don't receive anything. +	 * +	 * This method is used to calibrate some static offsets +	 * used together with on-the fly I/Q calibration (the +	 * one performed via ath5k_hw_phy_calibrate), which doesn't +	 * interrupt rx path. +	 * +	 * While rx path is re-routed to the power detector we also +	 * start a noise floor calibration to measure the +	 * card's noise floor (the noise we measure when we are not +	 * transmitting or receiving anything). +	 * +	 * If we are in a noisy environment, AGC calibration may time +	 * out and/or noise floor calibration might timeout. +	 */ +	AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, +				AR5K_PHY_AGCCTL_CAL | AR5K_PHY_AGCCTL_NF); + +	/* At the same time start I/Q calibration for QAM constellation +	 * -no need for CCK- */ +	ah->ah_iq_cal_needed = false; +	if (!(mode == AR5K_MODE_11B)) { +		ah->ah_iq_cal_needed = true; +		AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, +				AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15); +		AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, +				AR5K_PHY_IQ_RUN); +	} + +	/* Wait for gain calibration to finish (we check for I/Q calibration +	 * during ath5k_phy_calibrate) */ +	if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL, +			AR5K_PHY_AGCCTL_CAL, 0, false)) { +		ATH5K_ERR(ah, "gain calibration timeout (%uMHz)\n", +			channel->center_freq); +	} + +	/* Restore antenna mode */ +	ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode); + +	return ret;  }  | 
