pcu.c 17.2 KB
Newer Older
N
Nick Kossifidis 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
 * Copyright (c) 2007-2008 Matthew W. S. Bell  <mentor@madwifi.org>
 * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
 *
 * Permission to use, copy, modify, and 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.
 *
 */

/*********************************\
* Protocol Control Unit Functions *
\*********************************/

27 28
#include <asm/unaligned.h>

N
Nick Kossifidis 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
#include "ath5k.h"
#include "reg.h"
#include "debug.h"
#include "base.h"

/*******************\
* Generic functions *
\*******************/

/**
 * ath5k_hw_set_opmode - Set PCU operating mode
 *
 * @ah: The &struct ath5k_hw
42
 * @op_mode: &enum nl80211_iftype operating mode
N
Nick Kossifidis 已提交
43 44 45
 *
 * Initialize PCU for the various operating modes (AP/STA etc)
 */
46
int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
N
Nick Kossifidis 已提交
47
{
48
	struct ath_common *common = ath5k_hw_common(ah);
N
Nick Kossifidis 已提交
49 50
	u32 pcu_reg, beacon_reg, low_id, high_id;

51
	ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
N
Nick Kossifidis 已提交
52 53 54 55 56 57 58 59

	/* Preserve rest settings */
	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
	pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
			| AR5K_STA_ID1_KEYSRCH_MODE
			| (ah->ah_version == AR5K_AR5210 ?
			(AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));

N
Nick Kossifidis 已提交
60 61
	beacon_reg = 0;

62
	switch (op_mode) {
63
	case NL80211_IFTYPE_ADHOC:
N
Nick Kossifidis 已提交
64
		pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
N
Nick Kossifidis 已提交
65
		beacon_reg |= AR5K_BCR_ADHOC;
N
Nick Kossifidis 已提交
66 67 68
		if (ah->ah_version == AR5K_AR5210)
			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
		else
69
			AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
N
Nick Kossifidis 已提交
70 71
		break;

72 73
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_MESH_POINT:
N
Nick Kossifidis 已提交
74
		pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
N
Nick Kossifidis 已提交
75
		beacon_reg |= AR5K_BCR_AP;
N
Nick Kossifidis 已提交
76 77 78
		if (ah->ah_version == AR5K_AR5210)
			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
		else
79
			AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
N
Nick Kossifidis 已提交
80 81
		break;

82
	case NL80211_IFTYPE_STATION:
N
Nick Kossifidis 已提交
83 84
		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
			| (ah->ah_version == AR5K_AR5210 ?
N
Nick Kossifidis 已提交
85
				AR5K_STA_ID1_PWR_SV : 0);
86
	case NL80211_IFTYPE_MONITOR:
N
Nick Kossifidis 已提交
87 88
		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
			| (ah->ah_version == AR5K_AR5210 ?
N
Nick Kossifidis 已提交
89 90 91 92 93 94 95 96 97 98
				AR5K_STA_ID1_NO_PSPOLL : 0);
		break;

	default:
		return -EINVAL;
	}

	/*
	 * Set PCU registers
	 */
99 100
	low_id = get_unaligned_le32(common->macaddr);
	high_id = get_unaligned_le16(common->macaddr + 4);
N
Nick Kossifidis 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);

	/*
	 * Set Beacon Control Register on 5210
	 */
	if (ah->ah_version == AR5K_AR5210)
		ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);

	return 0;
}

/**
B
Bruno Randolf 已提交
114
 * ath5k_hw_update - Update MIB counters (mac layer statistics)
N
Nick Kossifidis 已提交
115 116 117
 *
 * @ah: The &struct ath5k_hw
 *
B
Bruno Randolf 已提交
118 119 120 121 122
 * Reads MIB counters from PCU and updates sw statistics. Is called after a
 * MIB interrupt, because one of these counters might have reached their maximum
 * and triggered the MIB interrupt, to let us read and clear the counter.
 *
 * Is called in interrupt context!
N
Nick Kossifidis 已提交
123
 */
B
Bruno Randolf 已提交
124
void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
N
Nick Kossifidis 已提交
125
{
B
Bruno Randolf 已提交
126
	struct ath5k_statistics *stats = &ah->ah_sc->stats;
N
Nick Kossifidis 已提交
127 128

	/* Read-And-Clear */
B
Bruno Randolf 已提交
129 130 131 132 133
	stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
	stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
	stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
	stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
	stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
N
Nick Kossifidis 已提交
134 135 136 137 138 139
}

/**
 * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
 *
 * @ah: The &struct ath5k_hw
B
Bob Copeland 已提交
140
 * @high: Flag to determine if we want to use high transmission rate
N
Nick Kossifidis 已提交
141 142 143
 * for ACKs or not
 *
 * If high flag is set, we tell hw to use a set of control rates based on
B
Bob Copeland 已提交
144
 * the current transmission rate (check out control_rates array inside reset.c).
N
Nick Kossifidis 已提交
145 146 147 148 149 150 151 152 153 154 155
 * If not hw just uses the lowest rate available for the current modulation
 * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
 */
void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
{
	if (ah->ah_version != AR5K_AR5212)
		return;
	else {
		u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
		if (high)
			AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
156 157
		else
			AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
N
Nick Kossifidis 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171
	}
}


/******************\
* ACK/CTS Timeouts *
\******************/

/**
 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
 *
 * @ah: The &struct ath5k_hw
 * @timeout: Timeout in usec
 */
172
static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
N
Nick Kossifidis 已提交
173
{
174 175
	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
			<= timeout)
N
Nick Kossifidis 已提交
176 177 178
		return -EINVAL;

	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
179
		ath5k_hw_htoclock(ah, timeout));
N
Nick Kossifidis 已提交
180 181 182 183 184 185 186 187 188 189

	return 0;
}

/**
 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
 *
 * @ah: The &struct ath5k_hw
 * @timeout: Timeout in usec
 */
190
static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
N
Nick Kossifidis 已提交
191
{
192 193
	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
			<= timeout)
N
Nick Kossifidis 已提交
194 195 196
		return -EINVAL;

	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
197
			ath5k_hw_htoclock(ah, timeout));
N
Nick Kossifidis 已提交
198 199 200 201

	return 0;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/**
 * ath5k_hw_htoclock - Translate usec to hw clock units
 *
 * @ah: The &struct ath5k_hw
 * @usec: value in microseconds
 */
unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
{
	return usec * ath5k_hw_get_clockrate(ah);
}

/**
 * ath5k_hw_clocktoh - Translate hw clock units to usec
 * @clock: value in hw clock units
 */
unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
{
	return clock / ath5k_hw_get_clockrate(ah);
}

/**
 * ath5k_hw_get_clockrate - Get the clock rate for current mode
 *
 * @ah: The &struct ath5k_hw
 */
unsigned int ath5k_hw_get_clockrate(struct ath5k_hw *ah)
{
	struct ieee80211_channel *channel = ah->ah_current_channel;
	int clock;

	if (channel->hw_value & CHANNEL_5GHZ)
		clock = 40; /* 802.11a */
	else if (channel->hw_value & CHANNEL_CCK)
		clock = 22; /* 802.11b */
	else
		clock = 44; /* 802.11g */

	/* Clock rate in turbo modes is twice the normal rate */
	if (channel->hw_value & CHANNEL_TURBO)
		clock *= 2;

	return clock;
}

246 247 248 249 250
/**
 * ath5k_hw_get_default_slottime - Get the default slot time for current mode
 *
 * @ah: The &struct ath5k_hw
 */
251
static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
{
	struct ieee80211_channel *channel = ah->ah_current_channel;

	if (channel->hw_value & CHANNEL_TURBO)
		return 6; /* both turbo modes */

	if (channel->hw_value & CHANNEL_CCK)
		return 20; /* 802.11b */

	return 9; /* 802.11 a/g */
}

/**
 * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
 *
 * @ah: The &struct ath5k_hw
 */
269
static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
270 271 272 273 274 275 276 277 278 279 280 281
{
	struct ieee80211_channel *channel = ah->ah_current_channel;

	if (channel->hw_value & CHANNEL_TURBO)
		return 8; /* both turbo modes */

	if (channel->hw_value & CHANNEL_5GHZ)
		return 16; /* 802.11a */

	return 10; /* 802.11 b/g */
}

N
Nick Kossifidis 已提交
282 283 284 285 286 287 288 289 290 291
/**
 * ath5k_hw_set_lladdr - Set station id
 *
 * @ah: The &struct ath5k_hw
 * @mac: The card's mac address
 *
 * Set station id on hw using the provided mac address
 */
int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
{
292
	struct ath_common *common = ath5k_hw_common(ah);
N
Nick Kossifidis 已提交
293
	u32 low_id, high_id;
294
	u32 pcu_reg;
N
Nick Kossifidis 已提交
295 296

	/* Set new station ID */
297
	memcpy(common->macaddr, mac, ETH_ALEN);
N
Nick Kossifidis 已提交
298

299 300
	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;

301 302
	low_id = get_unaligned_le32(mac);
	high_id = get_unaligned_le16(mac + 4);
N
Nick Kossifidis 已提交
303 304

	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
305
	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
N
Nick Kossifidis 已提交
306 307 308 309 310

	return 0;
}

/**
311
 * ath5k_hw_set_bssid - Set current BSSID on hw
N
Nick Kossifidis 已提交
312 313 314
 *
 * @ah: The &struct ath5k_hw
 *
315 316
 * Sets the current BSSID and BSSID mask we have from the
 * common struct into the hardware
N
Nick Kossifidis 已提交
317
 */
318
void ath5k_hw_set_bssid(struct ath5k_hw *ah)
N
Nick Kossifidis 已提交
319
{
320
	struct ath_common *common = ath5k_hw_common(ah);
N
Nick Kossifidis 已提交
321 322 323
	u16 tim_offset = 0;

	/*
324
	 * Set BSSID mask on 5212
N
Nick Kossifidis 已提交
325
	 */
326 327
	if (ah->ah_version == AR5K_AR5212)
		ath_hw_setbssidmask(common);
N
Nick Kossifidis 已提交
328 329

	/*
330
	 * Set BSSID
N
Nick Kossifidis 已提交
331
	 */
332 333
	ath5k_hw_reg_write(ah,
			   get_unaligned_le32(common->curbssid),
334
			   AR5K_BSS_ID0);
335 336 337
	ath5k_hw_reg_write(ah,
			   get_unaligned_le16(common->curbssid + 4) |
			   ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
338
			   AR5K_BSS_ID1);
N
Nick Kossifidis 已提交
339

340
	if (common->curaid == 0) {
N
Nick Kossifidis 已提交
341 342 343 344 345
		ath5k_hw_disable_pspoll(ah);
		return;
	}

	AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
346
			    tim_offset ? tim_offset + 4 : 0);
N
Nick Kossifidis 已提交
347 348 349 350

	ath5k_hw_enable_pspoll(ah, NULL, 0);
}

351
void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
N
Nick Kossifidis 已提交
352
{
353
	struct ath_common *common = ath5k_hw_common(ah);
N
Nick Kossifidis 已提交
354

N
Nick Kossifidis 已提交
355 356
	/* Cache bssid mask so that we can restore it
	 * on reset */
357
	memcpy(common->bssidmask, mask, ETH_ALEN);
358 359
	if (ah->ah_version == AR5K_AR5212)
		ath_hw_setbssidmask(common);
N
Nick Kossifidis 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
}

/************\
* RX Control *
\************/

/**
 * ath5k_hw_start_rx_pcu - Start RX engine
 *
 * @ah: The &struct ath5k_hw
 *
 * Starts RX engine on PCU so that hw can process RXed frames
 * (ACK etc).
 *
 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
 */
void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
{
	AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
}

/**
 * at5k_hw_stop_rx_pcu - Stop RX engine
 *
 * @ah: The &struct ath5k_hw
 *
 * Stops RX engine on PCU
 *
 * TODO: Detach ANI here
 */
void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
{
	AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
}

/*
 * Set multicast filter
 */
void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
{
	ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
	ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
}

/**
 * ath5k_hw_get_rx_filter - Get current rx filter
 *
 * @ah: The &struct ath5k_hw
 *
 * Returns the RX filter by reading rx filter and
 * phy error filter registers. RX filter is used
 * to set the allowed frame types that PCU will accept
 * and pass to the driver. For a list of frame types
 * check out reg.h.
 */
u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
{
	u32 data, filter = 0;

	filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);

	/*Radar detection for 5212*/
	if (ah->ah_version == AR5K_AR5212) {
		data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);

		if (data & AR5K_PHY_ERR_FIL_RADAR)
			filter |= AR5K_RX_FILTER_RADARERR;
		if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
			filter |= AR5K_RX_FILTER_PHYERR;
	}

	return filter;
}

/**
 * ath5k_hw_set_rx_filter - Set rx filter
 *
 * @ah: The &struct ath5k_hw
 * @filter: RX filter mask (see reg.h)
 *
 * Sets RX filter register and also handles PHY error filter
 * register on 5212 and newer chips so that we have proper PHY
 * error reporting.
 */
void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
{
	u32 data = 0;

	/* Set PHY error filter register on 5212*/
	if (ah->ah_version == AR5K_AR5212) {
		if (filter & AR5K_RX_FILTER_RADARERR)
			data |= AR5K_PHY_ERR_FIL_RADAR;
		if (filter & AR5K_RX_FILTER_PHYERR)
			data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
	}

	/*
	 * The AR5210 uses promiscous mode to detect radar activity
	 */
	if (ah->ah_version == AR5K_AR5210 &&
			(filter & AR5K_RX_FILTER_RADARERR)) {
		filter &= ~AR5K_RX_FILTER_RADARERR;
		filter |= AR5K_RX_FILTER_PROM;
	}

N
Nick Kossifidis 已提交
465
	/*Zero length DMA (phy error reporting) */
N
Nick Kossifidis 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	if (data)
		AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
	else
		AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);

	/*Write RX Filter register*/
	ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);

	/*Write PHY error filter register on 5212*/
	if (ah->ah_version == AR5K_AR5212)
		ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);

}


/****************\
* Beacon control *
\****************/

485 486
#define ATH5K_MAX_TSF_READ 10

N
Nick Kossifidis 已提交
487 488 489 490 491 492 493 494 495
/**
 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
 *
 * @ah: The &struct ath5k_hw
 *
 * Returns the current TSF
 */
u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
{
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	u32 tsf_lower, tsf_upper1, tsf_upper2;
	int i;

	/*
	 * While reading TSF upper and then lower part, the clock is still
	 * counting (or jumping in case of IBSS merge) so we might get
	 * inconsistent values. To avoid this, we read the upper part again
	 * and check it has not been changed. We make the hypothesis that a
	 * maximum of 3 changes can happens in a row (we use 10 as a safe
	 * value).
	 *
	 * Impact on performance is pretty small, since in most cases, only
	 * 3 register reads are needed.
	 */

	tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
	for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
		tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
		tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
		if (tsf_upper2 == tsf_upper1)
			break;
		tsf_upper1 = tsf_upper2;
	}

	WARN_ON( i == ATH5K_MAX_TSF_READ );

	return (((u64)tsf_upper1 << 32) | tsf_lower);
N
Nick Kossifidis 已提交
523 524
}

525 526 527 528 529 530 531 532 533 534 535
/**
 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
 *
 * @ah: The &struct ath5k_hw
 * @tsf64: The new 64bit TSF
 *
 * Sets the new TSF
 */
void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
{
	ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
A
Alina Friedrichsen 已提交
536
	ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
537 538
}

N
Nick Kossifidis 已提交
539 540 541 542 543 544 545 546 547
/**
 * ath5k_hw_reset_tsf - Force a TSF reset
 *
 * @ah: The &struct ath5k_hw
 *
 * Forces a TSF reset on PCU
 */
void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
{
548 549 550 551 552 553 554 555 556 557 558 559
	u32 val;

	val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;

	/*
	 * Each write to the RESET_TSF bit toggles a hardware internal
	 * signal to reset TSF, but if left high it will cause a TSF reset
	 * on the next chip reset as well.  Thus we always write the value
	 * twice to clear the signal.
	 */
	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
N
Nick Kossifidis 已提交
560 561 562 563 564 565 566 567 568 569 570 571
}

/*
 * Initialize beacon timers
 */
void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
{
	u32 timer1, timer2, timer3;

	/*
	 * Set the additional timers by mode
	 */
572
	switch (ah->ah_sc->opmode) {
N
Nick Kossifidis 已提交
573
	case NL80211_IFTYPE_MONITOR:
574
	case NL80211_IFTYPE_STATION:
N
Nick Kossifidis 已提交
575 576 577 578
		/* In STA mode timer1 is used as next wakeup
		 * timer and timer2 as next CFP duration start
		 * timer. Both in 1/8TUs. */
		/* TODO: PCF handling */
N
Nick Kossifidis 已提交
579 580 581 582 583 584 585
		if (ah->ah_version == AR5K_AR5210) {
			timer1 = 0xffffffff;
			timer2 = 0xffffffff;
		} else {
			timer1 = 0x0000ffff;
			timer2 = 0x0007ffff;
		}
N
Nick Kossifidis 已提交
586 587
		/* Mark associated AP as PCF incapable for now */
		AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
N
Nick Kossifidis 已提交
588
		break;
N
Nick Kossifidis 已提交
589 590
	case NL80211_IFTYPE_ADHOC:
		AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
N
Nick Kossifidis 已提交
591
	default:
N
Nick Kossifidis 已提交
592 593 594
		/* On non-STA modes timer1 is used as next DMA
		 * beacon alert (DBA) timer and timer2 as next
		 * software beacon alert. Both in 1/8TUs. */
N
Nick Kossifidis 已提交
595 596
		timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
		timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
N
Nick Kossifidis 已提交
597
		break;
N
Nick Kossifidis 已提交
598 599
	}

N
Nick Kossifidis 已提交
600 601 602
	/* Timer3 marks the end of our ATIM window
	 * a zero length window is not allowed because
	 * we 'll get no beacons */
603
	timer3 = next_beacon + 1;
N
Nick Kossifidis 已提交
604 605 606 607

	/*
	 * Set the beacon register and enable all timers.
	 */
N
Nick Kossifidis 已提交
608
	/* When in AP or Mesh Point mode zero timer0 to start TSF */
609 610
	if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
	    ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
N
Nick Kossifidis 已提交
611
		ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
N
Nick Kossifidis 已提交
612 613

	ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
N
Nick Kossifidis 已提交
614 615 616 617
	ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
	ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
	ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);

N
Nick Kossifidis 已提交
618 619 620 621
	/* Force a TSF reset if requested and enable beacons */
	if (interval & AR5K_BEACON_RESET_TSF)
		ath5k_hw_reset_tsf(ah);

N
Nick Kossifidis 已提交
622
	ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
N
Nick Kossifidis 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
					AR5K_BEACON_ENABLE),
						AR5K_BEACON);

	/* Flush any pending BMISS interrupts on ISR by
	 * performing a clear-on-write operation on PISR
	 * register for the BMISS bit (writing a bit on
	 * ISR togles a reset for that bit and leaves
	 * the rest bits intact) */
	if (ah->ah_version == AR5K_AR5210)
		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
	else
		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);

	/* TODO: Set enchanced sleep registers on AR5212
	 * based on vif->bss_conf params, until then
	 * disable power save reporting.*/
	AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);

N
Nick Kossifidis 已提交
641 642
}

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/**
 * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
 *
 * @ah: The &struct ath5k_hw
 * @coverage_class: IEEE 802.11 coverage class number
 *
 * Sets slot time, ACK timeout and CTS timeout for given coverage class.
 */
void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
{
	/* As defined by IEEE 802.11-2007 17.3.8.6 */
	int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
	int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
	int cts_timeout = ack_timeout;

	ath5k_hw_set_slot_time(ah, slot_time);
	ath5k_hw_set_ack_timeout(ah, ack_timeout);
	ath5k_hw_set_cts_timeout(ah, cts_timeout);

	ah->ah_coverage_class = coverage_class;
}