ar9003_mci.c 40.5 KB
Newer Older
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
 * Copyright (c) 2008-2011 Atheros Communications Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/export.h>
#include "hw.h"
#include "ar9003_phy.h"
#include "ar9003_mci.h"

static void ar9003_mci_reset_req_wakeup(struct ath_hw *ah)
{
	if (!AR_SREV_9462_20(ah))
		return;

	REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
		      AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 1);
	udelay(1);
	REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
		      AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 0);
}

static int ar9003_mci_wait_for_interrupt(struct ath_hw *ah, u32 address,
					u32 bit_position, int time_out)
{
	struct ath_common *common = ath9k_hw_common(ah);

	while (time_out) {

		if (REG_READ(ah, address) & bit_position) {

			REG_WRITE(ah, address, bit_position);

			if (address == AR_MCI_INTERRUPT_RX_MSG_RAW) {

				if (bit_position &
				    AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE)
					ar9003_mci_reset_req_wakeup(ah);

				if (bit_position &
				    (AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING |
				     AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING))
					REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
					AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);

				REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
					  AR_MCI_INTERRUPT_RX_MSG);
			}
			break;
		}

		udelay(10);
		time_out -= 10;

		if (time_out < 0)
			break;
	}

	if (time_out <= 0) {
71 72
		ath_dbg(common, MCI,
			"MCI Wait for Reg 0x%08x = 0x%08x timeout\n",
73
			address, bit_position);
74 75
		ath_dbg(common, MCI,
			"MCI INT_RAW = 0x%08x, RX_MSG_RAW = 0x%08x\n",
76 77 78 79 80 81 82 83 84 85 86 87
			REG_READ(ah, AR_MCI_INTERRUPT_RAW),
			REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
		time_out = 0;
	}

	return time_out;
}

void ar9003_mci_remote_reset(struct ath_hw *ah, bool wait_done)
{
	u32 payload[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffff00};

88 89 90
	if (!ATH9K_HW_CAP_MCI)
		return;

91 92 93 94 95 96 97 98 99
	ar9003_mci_send_message(ah, MCI_REMOTE_RESET, 0, payload, 16,
				wait_done, false);
	udelay(5);
}

void ar9003_mci_send_lna_transfer(struct ath_hw *ah, bool wait_done)
{
	u32 payload = 0x00000000;

100 101 102
	if (!ATH9K_HW_CAP_MCI)
		return;

103 104 105 106 107 108 109 110 111 112 113 114 115
	ar9003_mci_send_message(ah, MCI_LNA_TRANS, 0, &payload, 1,
				wait_done, false);
}

static void ar9003_mci_send_req_wake(struct ath_hw *ah, bool wait_done)
{
	ar9003_mci_send_message(ah, MCI_REQ_WAKE, MCI_FLAG_DISABLE_TIMESTAMP,
				NULL, 0, wait_done, false);
	udelay(5);
}

void ar9003_mci_send_sys_waking(struct ath_hw *ah, bool wait_done)
{
116 117 118
	if (!ATH9K_HW_CAP_MCI)
		return;

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	ar9003_mci_send_message(ah, MCI_SYS_WAKING, MCI_FLAG_DISABLE_TIMESTAMP,
				NULL, 0, wait_done, false);
}

static void ar9003_mci_send_lna_take(struct ath_hw *ah, bool wait_done)
{
	u32 payload = 0x70000000;

	ar9003_mci_send_message(ah, MCI_LNA_TAKE, 0, &payload, 1,
				wait_done, false);
}

static void ar9003_mci_send_sys_sleeping(struct ath_hw *ah, bool wait_done)
{
	ar9003_mci_send_message(ah, MCI_SYS_SLEEPING,
				MCI_FLAG_DISABLE_TIMESTAMP,
				NULL, 0, wait_done, false);
}

static void ar9003_mci_send_coex_version_query(struct ath_hw *ah,
					       bool wait_done)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 payload[4] = {0, 0, 0, 0};

	if (!mci->bt_version_known &&
			(mci->bt_state != MCI_BT_SLEEP)) {
147
		ath_dbg(common, MCI, "MCI Send Coex version query\n");
148 149 150 151 152 153 154 155 156 157 158 159 160 161
		MCI_GPM_SET_TYPE_OPCODE(payload,
				MCI_GPM_COEX_AGENT, MCI_GPM_COEX_VERSION_QUERY);
		ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
				wait_done, true);
	}
}

static void ar9003_mci_send_coex_version_response(struct ath_hw *ah,
						     bool wait_done)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 payload[4] = {0, 0, 0, 0};

162
	ath_dbg(common, MCI, "MCI Send Coex version response\n");
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
			MCI_GPM_COEX_VERSION_RESPONSE);
	*(((u8 *)payload) + MCI_GPM_COEX_B_MAJOR_VERSION) =
		mci->wlan_ver_major;
	*(((u8 *)payload) + MCI_GPM_COEX_B_MINOR_VERSION) =
		mci->wlan_ver_minor;
	ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
}

static void ar9003_mci_send_coex_wlan_channels(struct ath_hw *ah,
						  bool wait_done)
{
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 *payload = &mci->wlan_channels[0];

	if ((mci->wlan_channels_update == true) &&
			(mci->bt_state != MCI_BT_SLEEP)) {
		MCI_GPM_SET_TYPE_OPCODE(payload,
		MCI_GPM_COEX_AGENT, MCI_GPM_COEX_WLAN_CHANNELS);
		ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
					wait_done, true);
		MCI_GPM_SET_TYPE_OPCODE(payload, 0xff, 0xff);
	}
}

static void ar9003_mci_send_coex_bt_status_query(struct ath_hw *ah,
						bool wait_done, u8 query_type)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 payload[4] = {0, 0, 0, 0};
	bool query_btinfo = !!(query_type & (MCI_GPM_COEX_QUERY_BT_ALL_INFO |
					     MCI_GPM_COEX_QUERY_BT_TOPOLOGY));

	if (mci->bt_state != MCI_BT_SLEEP) {

199 200
		ath_dbg(common, MCI, "MCI Send Coex BT Status Query 0x%02X\n",
			query_type);
201 202 203 204 205 206 207 208 209 210 211 212 213 214

		MCI_GPM_SET_TYPE_OPCODE(payload,
				MCI_GPM_COEX_AGENT, MCI_GPM_COEX_STATUS_QUERY);

		*(((u8 *)payload) + MCI_GPM_COEX_B_BT_BITMAP) = query_type;
		/*
		 * If bt_status_query message is  not sent successfully,
		 * then need_flush_btinfo should be set again.
		 */
		if (!ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
					     wait_done, true)) {
			if (query_btinfo) {
				mci->need_flush_btinfo = true;

215 216
				ath_dbg(common, MCI,
					"MCI send bt_status_query fail, set flush flag again\n");
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
			}
		}

		if (query_btinfo)
			mci->query_bt = false;
	}
}

void ar9003_mci_send_coex_halt_bt_gpm(struct ath_hw *ah, bool halt,
				      bool wait_done)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 payload[4] = {0, 0, 0, 0};

232 233 234
	if (!ATH9K_HW_CAP_MCI)
		return;

235
	ath_dbg(common, MCI, "MCI Send Coex %s BT GPM\n",
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		(halt) ? "halt" : "unhalt");

	MCI_GPM_SET_TYPE_OPCODE(payload,
				MCI_GPM_COEX_AGENT, MCI_GPM_COEX_HALT_BT_GPM);

	if (halt) {
		mci->query_bt = true;
		/* Send next unhalt no matter halt sent or not */
		mci->unhalt_bt_gpm = true;
		mci->need_flush_btinfo = true;
		*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
			MCI_GPM_COEX_BT_GPM_HALT;
	} else
		*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
			MCI_GPM_COEX_BT_GPM_UNHALT;

	ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
}


static void ar9003_mci_prep_interface(struct ath_hw *ah)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 saved_mci_int_en;
	u32 mci_timeout = 150;

	mci->bt_state = MCI_BT_SLEEP;
	saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);

	REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
	REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
		  REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
	REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
		  REG_READ(ah, AR_MCI_INTERRUPT_RAW));

	/* Remote Reset */
273 274
	ath_dbg(common, MCI, "MCI Reset sequence start\n");
	ath_dbg(common, MCI, "MCI send REMOTE_RESET\n");
275 276
	ar9003_mci_remote_reset(ah, true);

277
	ath_dbg(common, MCI, "MCI Send REQ_WAKE to remoter(BT)\n");
278 279 280 281 282
	ar9003_mci_send_req_wake(ah, true);

	if (ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
				AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING, 500)) {

283
		ath_dbg(common, MCI, "MCI SYS_WAKING from remote(BT)\n");
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		mci->bt_state = MCI_BT_AWAKE;

		/*
		 * we don't need to send more remote_reset at this moment.
		 * If BT receive first remote_reset, then BT HW will
		 * be cleaned up and will be able to receive req_wake
		 * and BT HW will respond sys_waking.
		 * In this case, WLAN will receive BT's HW sys_waking.
		 * Otherwise, if BT SW missed initial remote_reset,
		 * that remote_reset will still clean up BT MCI RX,
		 * and the req_wake will wake BT up,
		 * and BT SW will respond this req_wake with a remote_reset and
		 * sys_waking. In this case, WLAN will receive BT's SW
		 * sys_waking. In either case, BT's RX is cleaned up. So we
		 * don't need to reply BT's remote_reset now, if any.
		 * Similarly, if in any case, WLAN can receive BT's sys_waking,
		 * that means WLAN's RX is also fine.
		 */

		/* Send SYS_WAKING to BT */

305
		ath_dbg(common, MCI, "MCI send SW SYS_WAKING to remote BT\n");
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

		ar9003_mci_send_sys_waking(ah, true);
		udelay(10);

		/*
		 * Set BT priority interrupt value to be 0xff to
		 * avoid having too many BT PRIORITY interrupts.
		 */

		REG_WRITE(ah, AR_MCI_BT_PRI0, 0xFFFFFFFF);
		REG_WRITE(ah, AR_MCI_BT_PRI1, 0xFFFFFFFF);
		REG_WRITE(ah, AR_MCI_BT_PRI2, 0xFFFFFFFF);
		REG_WRITE(ah, AR_MCI_BT_PRI3, 0xFFFFFFFF);
		REG_WRITE(ah, AR_MCI_BT_PRI, 0X000000FF);

		/*
		 * A contention reset will be received after send out
		 * sys_waking. Also BT priority interrupt bits will be set.
		 * Clear those bits before the next step.
		 */

		REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
			  AR_MCI_INTERRUPT_RX_MSG_CONT_RST);
		REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
			  AR_MCI_INTERRUPT_BT_PRI);

332
		if (mci->is_2g) {
333
			/* Send LNA_TRANS */
334
			ath_dbg(common, MCI, "MCI send LNA_TRANS to BT\n");
335 336 337 338
			ar9003_mci_send_lna_transfer(ah, true);
			udelay(5);
		}

339
		if ((mci->is_2g && !mci->update_2g5g)) {
340 341 342 343
			if (ar9003_mci_wait_for_interrupt(ah,
				AR_MCI_INTERRUPT_RX_MSG_RAW,
				AR_MCI_INTERRUPT_RX_MSG_LNA_INFO,
				mci_timeout))
344 345
				ath_dbg(common, MCI,
					"MCI WLAN has control over the LNA & BT obeys it\n");
346
			else
347 348
				ath_dbg(common, MCI,
					"MCI BT didn't respond to LNA_TRANS\n");
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
		}
	}

	/* Clear the extra redundant SYS_WAKING from BT */
	if ((mci->bt_state == MCI_BT_AWAKE) &&
		(REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
				AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING)) &&
		(REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
				AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) == 0)) {

			REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
				  AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING);
			REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
				  AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
	}

	REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
}

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
void ar9003_mci_set_full_sleep(struct ath_hw *ah)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;

	if (ar9003_mci_state(ah, MCI_STATE_ENABLE, NULL) &&
	    (mci->bt_state != MCI_BT_SLEEP) &&
	    !mci->halted_bt_gpm) {
		ath_dbg(common, MCI,
			"MCI halt BT GPM (full_sleep)\n");
		ar9003_mci_send_coex_halt_bt_gpm(ah, true, true);
	}

	mci->ready = false;
	REG_WRITE(ah, AR_RTC_KEEP_AWAKE, 0x2);
}

385 386
void ar9003_mci_disable_interrupt(struct ath_hw *ah)
{
387 388 389
	if (!ATH9K_HW_CAP_MCI)
		return;

390 391 392 393 394 395
	REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
	REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
}

void ar9003_mci_enable_interrupt(struct ath_hw *ah)
{
396 397
	if (!ATH9K_HW_CAP_MCI)
		return;
398 399 400 401 402 403 404 405 406 407

	REG_WRITE(ah, AR_MCI_INTERRUPT_EN, AR_MCI_INTERRUPT_DEFAULT);
	REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN,
		  AR_MCI_INTERRUPT_RX_MSG_DEFAULT);
}

bool ar9003_mci_check_int(struct ath_hw *ah, u32 ints)
{
	u32 intr;

408 409 410
	if (!ATH9K_HW_CAP_MCI)
		return false;

411 412 413 414 415 416 417 418
	intr = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
	return ((intr & ints) == ints);
}

void ar9003_mci_get_interrupt(struct ath_hw *ah, u32 *raw_intr,
			      u32 *rx_msg_intr)
{
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
419 420 421 422

	if (!ATH9K_HW_CAP_MCI)
		return;

423 424 425 426 427 428 429 430 431
	*raw_intr = mci->raw_intr;
	*rx_msg_intr = mci->rx_msg_intr;

	/* Clean int bits after the values are read. */
	mci->raw_intr = 0;
	mci->rx_msg_intr = 0;
}
EXPORT_SYMBOL(ar9003_mci_get_interrupt);

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
void ar9003_mci_get_isr(struct ath_hw *ah, enum ath9k_int *masked)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 raw_intr, rx_msg_intr;

	rx_msg_intr = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
	raw_intr = REG_READ(ah, AR_MCI_INTERRUPT_RAW);

	if ((raw_intr == 0xdeadbeef) || (rx_msg_intr == 0xdeadbeef)) {
		ath_dbg(common, MCI,
			"MCI gets 0xdeadbeef during int processing\n");
	} else {
		mci->rx_msg_intr |= rx_msg_intr;
		mci->raw_intr |= raw_intr;
		*masked |= ATH9K_INT_MCI;

		if (rx_msg_intr & AR_MCI_INTERRUPT_RX_MSG_CONT_INFO)
			mci->cont_status = REG_READ(ah, AR_MCI_CONT_STATUS);

		REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW, rx_msg_intr);
		REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, raw_intr);
	}
}

457 458 459 460
void ar9003_mci_2g5g_changed(struct ath_hw *ah, bool is_2g)
{
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;

461 462 463
	if (!ATH9K_HW_CAP_MCI)
		return;

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	if (!mci->update_2g5g &&
	    (mci->is_2g != is_2g))
		mci->update_2g5g = true;

	mci->is_2g = is_2g;
}

static bool ar9003_mci_is_gpm_valid(struct ath_hw *ah, u32 msg_index)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 *payload;
	u32 recv_type, offset;

	if (msg_index == MCI_GPM_INVALID)
		return false;

	offset = msg_index << 4;

	payload = (u32 *)(mci->gpm_buf + offset);
	recv_type = MCI_GPM_TYPE(payload);

	if (recv_type == MCI_GPM_RSVD_PATTERN) {
487
		ath_dbg(common, MCI, "MCI Skip RSVD GPM\n");
488 489 490 491 492 493 494 495 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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
		return false;
	}

	return true;
}

static void ar9003_mci_observation_set_up(struct ath_hw *ah)
{
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	if (mci->config & ATH_MCI_CONFIG_MCI_OBS_MCI) {

		ath9k_hw_cfg_output(ah, 3,
					AR_GPIO_OUTPUT_MUX_AS_MCI_WLAN_DATA);
		ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_MCI_WLAN_CLK);
		ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
		ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);

	} else if (mci->config & ATH_MCI_CONFIG_MCI_OBS_TXRX) {

		ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_WL_IN_TX);
		ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_WL_IN_RX);
		ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
		ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
		ath9k_hw_cfg_output(ah, 5, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);

	} else if (mci->config & ATH_MCI_CONFIG_MCI_OBS_BT) {

		ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
		ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
		ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
		ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);

	} else
		return;

	REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);

	if (AR_SREV_9462_20_OR_LATER(ah)) {
		REG_RMW_FIELD(ah, AR_PHY_GLB_CONTROL,
			      AR_GLB_DS_JTAG_DISABLE, 1);
		REG_RMW_FIELD(ah, AR_PHY_GLB_CONTROL,
			      AR_GLB_WLAN_UART_INTF_EN, 0);
		REG_SET_BIT(ah, AR_GLB_GPIO_CONTROL,
			    ATH_MCI_CONFIG_MCI_OBS_GPIO);
	}

	REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_GPIO_OBS_SEL, 0);
	REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_MAC_BB_OBS_SEL, 1);
	REG_WRITE(ah, AR_OBS, 0x4b);
	REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL1, 0x03);
	REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL2, 0x01);
	REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_LSB, 0x02);
	REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_MSB, 0x03);
	REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS,
		      AR_PHY_TEST_CTL_DEBUGPORT_SEL, 0x07);
}

static bool ar9003_mci_send_coex_bt_flags(struct ath_hw *ah, bool wait_done,
						u8 opcode, u32 bt_flags)
{
	struct ath_common *common = ath9k_hw_common(ah);
	u32 pld[4] = {0, 0, 0, 0};

	MCI_GPM_SET_TYPE_OPCODE(pld,
			MCI_GPM_COEX_AGENT, MCI_GPM_COEX_BT_UPDATE_FLAGS);

	*(((u8 *)pld) + MCI_GPM_COEX_B_BT_FLAGS_OP)  = opcode;
	*(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 0) = bt_flags & 0xFF;
	*(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 1) = (bt_flags >> 8) & 0xFF;
	*(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 2) = (bt_flags >> 16) & 0xFF;
	*(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 3) = (bt_flags >> 24) & 0xFF;

560
	ath_dbg(common, MCI,
561
		"MCI BT_MCI_FLAGS: Send Coex BT Update Flags %s 0x%08x\n",
562 563 564
		opcode == MCI_GPM_COEX_BT_FLAGS_READ ? "READ" :
		opcode == MCI_GPM_COEX_BT_FLAGS_SET ? "SET" : "CLEAR",
		bt_flags);
565 566 567 568 569 570 571 572 573 574 575 576

	return ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16,
							wait_done, true);
}

void ar9003_mci_reset(struct ath_hw *ah, bool en_int, bool is_2g,
		      bool is_full_sleep)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 regval, thresh;

577 578 579
	if (!ATH9K_HW_CAP_MCI)
		return;

580
	ath_dbg(common, MCI, "MCI full_sleep = %d, is_2g = %d\n",
581 582 583 584 585 586 587
		is_full_sleep, is_2g);

	/*
	 * GPM buffer and scheduling message buffer are not allocated
	 */

	if (!mci->gpm_addr && !mci->sched_addr) {
588 589
		ath_dbg(common, MCI,
			"MCI GPM and schedule buffers are not allocated\n");
590 591 592 593
		return;
	}

	if (REG_READ(ah, AR_BTCOEX_CTRL) == 0xdeadbeef) {
594
		ath_dbg(common, MCI, "MCI it's deadbeef, quit mci_reset\n");
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		return;
	}

	/* Program MCI DMA related registers */
	REG_WRITE(ah, AR_MCI_GPM_0, mci->gpm_addr);
	REG_WRITE(ah, AR_MCI_GPM_1, mci->gpm_len);
	REG_WRITE(ah, AR_MCI_SCHD_TABLE_0, mci->sched_addr);

	/*
	* To avoid MCI state machine be affected by incoming remote MCI msgs,
	* MCI mode will be enabled later, right before reset the MCI TX and RX.
	*/

	regval = SM(1, AR_BTCOEX_CTRL_AR9462_MODE) |
		 SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
		 SM(1, AR_BTCOEX_CTRL_PA_SHARED) |
		 SM(1, AR_BTCOEX_CTRL_LNA_SHARED) |
		 SM(2, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
		 SM(3, AR_BTCOEX_CTRL_RX_CHAIN_MASK) |
		 SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
		 SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
		 SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);

	if (is_2g && (AR_SREV_9462_20(ah)) &&
		!(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA)) {

		regval |= SM(1, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
622
		ath_dbg(common, MCI, "MCI sched one step look ahead\n");
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639

		if (!(mci->config &
		      ATH_MCI_CONFIG_DISABLE_AGGR_THRESH)) {

			thresh = MS(mci->config,
				    ATH_MCI_CONFIG_AGGR_THRESH);
			thresh &= 7;
			regval |= SM(1,
				     AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN);
			regval |= SM(thresh, AR_BTCOEX_CTRL_AGGR_THRESH);

			REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2,
				      AR_MCI_SCHD_TABLE_2_HW_BASED, 1);
			REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2,
				      AR_MCI_SCHD_TABLE_2_MEM_BASED, 1);

		} else
640
			ath_dbg(common, MCI, "MCI sched aggr thresh: off\n");
641
	} else
642
		ath_dbg(common, MCI, "MCI SCHED one step look ahead off\n");
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

	REG_WRITE(ah, AR_BTCOEX_CTRL, regval);

	if (AR_SREV_9462_20(ah)) {
		REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
			    AR_BTCOEX_CTRL_SPDT_ENABLE);
		REG_RMW_FIELD(ah, AR_BTCOEX_CTRL3,
			      AR_BTCOEX_CTRL3_CONT_INFO_TIMEOUT, 20);
	}

	REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_RX_DEWEIGHT, 1);
	REG_RMW_FIELD(ah, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX, 0);

	thresh = MS(mci->config, ATH_MCI_CONFIG_CLK_DIV);
	REG_RMW_FIELD(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_CLK_DIV, thresh);
	REG_SET_BIT(ah, AR_BTCOEX_CTRL, AR_BTCOEX_CTRL_MCI_MODE_EN);

	/* Resetting the Rx and Tx paths of MCI */
	regval = REG_READ(ah, AR_MCI_COMMAND2);
	regval |= SM(1, AR_MCI_COMMAND2_RESET_TX);
	REG_WRITE(ah, AR_MCI_COMMAND2, regval);

	udelay(1);

	regval &= ~SM(1, AR_MCI_COMMAND2_RESET_TX);
	REG_WRITE(ah, AR_MCI_COMMAND2, regval);

	if (is_full_sleep) {
		ar9003_mci_mute_bt(ah);
		udelay(100);
	}

	regval |= SM(1, AR_MCI_COMMAND2_RESET_RX);
	REG_WRITE(ah, AR_MCI_COMMAND2, regval);
	udelay(1);
	regval &= ~SM(1, AR_MCI_COMMAND2_RESET_RX);
	REG_WRITE(ah, AR_MCI_COMMAND2, regval);

	ar9003_mci_state(ah, MCI_STATE_INIT_GPM_OFFSET, NULL);
	REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE,
		  (SM(0xe801, AR_MCI_MSG_ATTRIBUTES_TABLE_INVALID_HDR) |
		   SM(0x0000, AR_MCI_MSG_ATTRIBUTES_TABLE_CHECKSUM)));

	REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
			AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);

	if (AR_SREV_9462_20_OR_LATER(ah))
		ar9003_mci_observation_set_up(ah);

	mci->ready = true;
	ar9003_mci_prep_interface(ah);

	if (en_int)
		ar9003_mci_enable_interrupt(ah);
}

void ar9003_mci_mute_bt(struct ath_hw *ah)
{
	struct ath_common *common = ath9k_hw_common(ah);

703 704 705
	if (!ATH9K_HW_CAP_MCI)
		return;

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	/* disable all MCI messages */
	REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE, 0xffff0000);
	REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS0, 0xffffffff);
	REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS1, 0xffffffff);
	REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS2, 0xffffffff);
	REG_WRITE(ah, AR_BTCOEX_WL_WEIGHTS3, 0xffffffff);
	REG_SET_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);

	/* wait pending HW messages to flush out */
	udelay(10);

	/*
	 * Send LNA_TAKE and SYS_SLEEPING when
	 * 1. reset not after resuming from full sleep
	 * 2. before reset MCI RX, to quiet BT and avoid MCI RX misalignment
	 */

723
	ath_dbg(common, MCI, "MCI Send LNA take\n");
724 725 726 727
	ar9003_mci_send_lna_take(ah, true);

	udelay(5);

728
	ath_dbg(common, MCI, "MCI Send sys sleeping\n");
729 730 731 732 733 734 735 736 737
	ar9003_mci_send_sys_sleeping(ah, true);
}

void ar9003_mci_sync_bt_state(struct ath_hw *ah)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 cur_bt_state;

738 739 740
	if (!ATH9K_HW_CAP_MCI)
		return;

741 742 743
	cur_bt_state = ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP, NULL);

	if (mci->bt_state != cur_bt_state) {
744
		ath_dbg(common, MCI,
745 746 747 748 749 750 751 752 753 754 755
			"MCI BT state mismatches. old: %d, new: %d\n",
			mci->bt_state, cur_bt_state);
		mci->bt_state = cur_bt_state;
	}

	if (mci->bt_state != MCI_BT_SLEEP) {

		ar9003_mci_send_coex_version_query(ah, true);
		ar9003_mci_send_coex_wlan_channels(ah, true);

		if (mci->unhalt_bt_gpm == true) {
756
			ath_dbg(common, MCI, "MCI unhalt BT GPM\n");
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
			ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
		}
	}
}

static void ar9003_mci_send_2g5g_status(struct ath_hw *ah, bool wait_done)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 new_flags, to_set, to_clear;

	if (AR_SREV_9462_20(ah) &&
	    mci->update_2g5g &&
	    (mci->bt_state != MCI_BT_SLEEP)) {

		if (mci->is_2g) {
			new_flags = MCI_2G_FLAGS;
			to_clear = MCI_2G_FLAGS_CLEAR_MASK;
			to_set = MCI_2G_FLAGS_SET_MASK;
		} else {
			new_flags = MCI_5G_FLAGS;
			to_clear = MCI_5G_FLAGS_CLEAR_MASK;
			to_set = MCI_5G_FLAGS_SET_MASK;
		}

782
		ath_dbg(common, MCI,
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
			"MCI BT_MCI_FLAGS: %s 0x%08x clr=0x%08x, set=0x%08x\n",
		mci->is_2g ? "2G" : "5G", new_flags, to_clear, to_set);

		if (to_clear)
			ar9003_mci_send_coex_bt_flags(ah, wait_done,
					MCI_GPM_COEX_BT_FLAGS_CLEAR, to_clear);

		if (to_set)
			ar9003_mci_send_coex_bt_flags(ah, wait_done,
					MCI_GPM_COEX_BT_FLAGS_SET, to_set);
	}
}

static void ar9003_mci_queue_unsent_gpm(struct ath_hw *ah, u8 header,
					u32 *payload, bool queue)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u8 type, opcode;

	if (queue) {

		if (payload)
806
			ath_dbg(common, MCI,
807 808 809 810 811 812
				"MCI ERROR: Send fail: %02x: %02x %02x %02x\n",
				header,
				*(((u8 *)payload) + 4),
				*(((u8 *)payload) + 5),
				*(((u8 *)payload) + 6));
		else
813 814
			ath_dbg(common, MCI, "MCI ERROR: Send fail: %02x\n",
				header);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	}

	/* check if the message is to be queued */
	if (header != MCI_GPM)
		return;

	type = MCI_GPM_TYPE(payload);
	opcode = MCI_GPM_OPCODE(payload);

	if (type != MCI_GPM_COEX_AGENT)
		return;

	switch (opcode) {
	case MCI_GPM_COEX_BT_UPDATE_FLAGS:

		if (*(((u8 *)payload) + MCI_GPM_COEX_B_BT_FLAGS_OP) ==
831
		    MCI_GPM_COEX_BT_FLAGS_READ)
832 833 834 835 836
			break;

		mci->update_2g5g = queue;

		if (queue)
837 838
			ath_dbg(common, MCI,
				"MCI BT_MCI_FLAGS: 2G5G status <queued> %s\n",
839 840
				mci->is_2g ? "2G" : "5G");
		else
841 842
			ath_dbg(common, MCI,
				"MCI BT_MCI_FLAGS: 2G5G status <sent> %s\n",
843 844 845 846 847 848 849 850
				mci->is_2g ? "2G" : "5G");

		break;

	case MCI_GPM_COEX_WLAN_CHANNELS:

		mci->wlan_channels_update = queue;
		if (queue)
851
			ath_dbg(common, MCI, "MCI WLAN channel map <queued>\n");
852
		else
853
			ath_dbg(common, MCI, "MCI WLAN channel map <sent>\n");
854 855 856 857 858 859 860 861 862 863
		break;

	case MCI_GPM_COEX_HALT_BT_GPM:

		if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
				MCI_GPM_COEX_BT_GPM_UNHALT) {

			mci->unhalt_bt_gpm = queue;

			if (queue)
864
				ath_dbg(common, MCI,
865 866 867
					"MCI UNHALT BT GPM <queued>\n");
			else {
				mci->halted_bt_gpm = false;
868
				ath_dbg(common, MCI,
869 870 871 872 873 874 875 876 877 878
					"MCI UNHALT BT GPM <sent>\n");
			}
		}

		if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
				MCI_GPM_COEX_BT_GPM_HALT) {

			mci->halted_bt_gpm = !queue;

			if (queue)
879
				ath_dbg(common, MCI,
880 881
					"MCI HALT BT GPM <not sent>\n");
			else
882
				ath_dbg(common, MCI,
883 884 885 886 887 888 889 890 891 892 893 894 895 896
					"MCI UNHALT BT GPM <sent>\n");
		}

		break;
	default:
		break;
	}
}

void ar9003_mci_2g5g_switch(struct ath_hw *ah, bool wait_done)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;

897 898 899
	if (!ATH9K_HW_CAP_MCI)
		return;

900 901 902 903
	if (mci->update_2g5g) {
		if (mci->is_2g) {

			ar9003_mci_send_2g5g_status(ah, true);
904
			ath_dbg(common, MCI, "MCI Send LNA trans\n");
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
			ar9003_mci_send_lna_transfer(ah, true);
			udelay(5);

			REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
				    AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);

			if (AR_SREV_9462_20(ah)) {
				REG_CLR_BIT(ah, AR_PHY_GLB_CONTROL,
					    AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
				if (!(mci->config &
				      ATH_MCI_CONFIG_DISABLE_OSLA)) {
					REG_SET_BIT(ah, AR_BTCOEX_CTRL,
					AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
				}
			}
		} else {
921
			ath_dbg(common, MCI, "MCI Send LNA take\n");
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
			ar9003_mci_send_lna_take(ah, true);
			udelay(5);

			REG_SET_BIT(ah, AR_MCI_TX_CTRL,
				    AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);

			if (AR_SREV_9462_20(ah)) {
				REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
					    AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
				REG_CLR_BIT(ah, AR_BTCOEX_CTRL,
					AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
			}

			ar9003_mci_send_2g5g_status(ah, true);
		}
	}
}

bool ar9003_mci_send_message(struct ath_hw *ah, u8 header, u32 flag,
			     u32 *payload, u8 len, bool wait_done,
			     bool check_bt)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	bool msg_sent = false;
	u32 regval;
	u32 saved_mci_int_en;
	int i;

951 952 953
	if (!ATH9K_HW_CAP_MCI)
		return false;

954 955 956 957 958
	saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);
	regval = REG_READ(ah, AR_BTCOEX_CTRL);

	if ((regval == 0xdeadbeef) || !(regval & AR_BTCOEX_CTRL_MCI_MODE_EN)) {

959 960 961
		ath_dbg(common, MCI,
			"MCI Not sending 0x%x. MCI is not enabled. full_sleep = %d\n",
			header,
962 963 964 965 966 967 968
			(ah->power_mode == ATH9K_PM_FULL_SLEEP) ? 1 : 0);

		ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
		return false;

	} else if (check_bt && (mci->bt_state == MCI_BT_SLEEP)) {

969 970 971
		ath_dbg(common, MCI,
			"MCI Don't send message 0x%x. BT is in sleep state\n",
			header);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

		ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
		return false;
	}

	if (wait_done)
		REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);

	/* Need to clear SW_MSG_DONE raw bit before wait */

	REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
		  (AR_MCI_INTERRUPT_SW_MSG_DONE |
		   AR_MCI_INTERRUPT_MSG_FAIL_MASK));

	if (payload) {
		for (i = 0; (i * 4) < len; i++)
			REG_WRITE(ah, (AR_MCI_TX_PAYLOAD0 + i * 4),
				  *(payload + i));
	}

	REG_WRITE(ah, AR_MCI_COMMAND0,
		  (SM((flag & MCI_FLAG_DISABLE_TIMESTAMP),
		      AR_MCI_COMMAND0_DISABLE_TIMESTAMP) |
		   SM(len, AR_MCI_COMMAND0_LEN) |
		   SM(header, AR_MCI_COMMAND0_HEADER)));

	if (wait_done &&
	    !(ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RAW,
					AR_MCI_INTERRUPT_SW_MSG_DONE, 500)))
		ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
	else {
		ar9003_mci_queue_unsent_gpm(ah, header, payload, false);
		msg_sent = true;
	}

	if (wait_done)
		REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);

	return msg_sent;
}
EXPORT_SYMBOL(ar9003_mci_send_message);

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
void ar9003_mci_init_cal_req(struct ath_hw *ah, bool *is_reusable)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
	u32 pld[4] = {0, 0, 0, 0};

	if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
	    (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
		return;

	/* send CAL_REQ only when BT is AWAKE. */
	ath_dbg(common, MCI, "MCI send WLAN_CAL_REQ 0x%x\n",
		mci_hw->wlan_cal_seq);

	MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_REQ);
	pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_seq++;

	ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);

	/* Wait BT_CAL_GRANT for 50ms */
	ath_dbg(common, MCI, "MCI wait for BT_CAL_GRANT\n");

	if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_GRANT, 0, 50000)) {
		ath_dbg(common, MCI, "MCI got BT_CAL_GRANT\n");
	} else {
		is_reusable = false;
		ath_dbg(common, MCI, "MCI BT is not responding\n");
	}
}

void ar9003_mci_init_cal_done(struct ath_hw *ah)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
	u32 pld[4] = {0, 0, 0, 0};

	if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
	    (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
		return;

	ath_dbg(common, MCI, "MCI Send WLAN_CAL_DONE 0x%x\n",
		mci_hw->wlan_cal_done);

	MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_DONE);
	pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_done++;
	ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
}

1062 1063 1064 1065 1066
void ar9003_mci_setup(struct ath_hw *ah, u32 gpm_addr, void *gpm_buf,
		      u16 len, u32 sched_addr)
{
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;

1067 1068 1069
	if (!ATH9K_HW_CAP_MCI)
		return;

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	mci->gpm_addr = gpm_addr;
	mci->gpm_buf = gpm_buf;
	mci->gpm_len = len;
	mci->sched_addr = sched_addr;

	ar9003_mci_reset(ah, true, true, true);
}
EXPORT_SYMBOL(ar9003_mci_setup);

void ar9003_mci_cleanup(struct ath_hw *ah)
{
1081 1082 1083
	if (!ATH9K_HW_CAP_MCI)
		return;

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	/* Turn off MCI and Jupiter mode. */
	REG_WRITE(ah, AR_BTCOEX_CTRL, 0x00);
	ar9003_mci_disable_interrupt(ah);
}
EXPORT_SYMBOL(ar9003_mci_cleanup);

static void ar9003_mci_process_gpm_extra(struct ath_hw *ah, u8 gpm_type,
					 u8 gpm_opcode, u32 *p_gpm)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u8 *p_data = (u8 *) p_gpm;

	if (gpm_type != MCI_GPM_COEX_AGENT)
		return;

	switch (gpm_opcode) {
	case MCI_GPM_COEX_VERSION_QUERY:
1102
		ath_dbg(common, MCI, "MCI Recv GPM COEX Version Query\n");
1103 1104 1105
		ar9003_mci_send_coex_version_response(ah, true);
		break;
	case MCI_GPM_COEX_VERSION_RESPONSE:
1106
		ath_dbg(common, MCI, "MCI Recv GPM COEX Version Response\n");
1107 1108 1109 1110 1111
		mci->bt_ver_major =
			*(p_data + MCI_GPM_COEX_B_MAJOR_VERSION);
		mci->bt_ver_minor =
			*(p_data + MCI_GPM_COEX_B_MINOR_VERSION);
		mci->bt_version_known = true;
1112 1113
		ath_dbg(common, MCI, "MCI BT Coex version: %d.%d\n",
			mci->bt_ver_major, mci->bt_ver_minor);
1114 1115
		break;
	case MCI_GPM_COEX_STATUS_QUERY:
1116 1117
		ath_dbg(common, MCI,
			"MCI Recv GPM COEX Status Query = 0x%02X\n",
1118 1119 1120 1121 1122 1123
			*(p_data + MCI_GPM_COEX_B_WLAN_BITMAP));
		mci->wlan_channels_update = true;
		ar9003_mci_send_coex_wlan_channels(ah, true);
		break;
	case MCI_GPM_COEX_BT_PROFILE_INFO:
		mci->query_bt = true;
1124
		ath_dbg(common, MCI, "MCI Recv GPM COEX BT_Profile_Info\n");
1125 1126 1127
		break;
	case MCI_GPM_COEX_BT_STATUS_UPDATE:
		mci->query_bt = true;
1128 1129 1130
		ath_dbg(common, MCI,
			"MCI Recv GPM COEX BT_Status_Update SEQ=%d (drop&query)\n",
			*(p_gpm + 3));
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
		break;
	default:
		break;
	}
}

u32 ar9003_mci_wait_for_gpm(struct ath_hw *ah, u8 gpm_type,
			    u8 gpm_opcode, int time_out)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 *p_gpm = NULL, mismatch = 0, more_data;
	u32 offset;
	u8 recv_type = 0, recv_opcode = 0;
	bool b_is_bt_cal_done = (gpm_type == MCI_GPM_BT_CAL_DONE);

1147 1148 1149
	if (!ATH9K_HW_CAP_MCI)
		return 0;

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	more_data = time_out ? MCI_GPM_NOMORE : MCI_GPM_MORE;

	while (time_out > 0) {
		if (p_gpm) {
			MCI_GPM_RECYCLE(p_gpm);
			p_gpm = NULL;
		}

		if (more_data != MCI_GPM_MORE)
			time_out = ar9003_mci_wait_for_interrupt(ah,
					AR_MCI_INTERRUPT_RX_MSG_RAW,
					AR_MCI_INTERRUPT_RX_MSG_GPM,
					time_out);

		if (!time_out)
			break;

		offset = ar9003_mci_state(ah,
				MCI_STATE_NEXT_GPM_OFFSET, &more_data);

		if (offset == MCI_GPM_INVALID)
			continue;

		p_gpm = (u32 *) (mci->gpm_buf + offset);
		recv_type = MCI_GPM_TYPE(p_gpm);
		recv_opcode = MCI_GPM_OPCODE(p_gpm);

		if (MCI_GPM_IS_CAL_TYPE(recv_type)) {

			if (recv_type == gpm_type) {

				if ((gpm_type == MCI_GPM_BT_CAL_DONE) &&
				    !b_is_bt_cal_done) {
					gpm_type = MCI_GPM_BT_CAL_GRANT;
1184 1185
					ath_dbg(common, MCI,
						"MCI Recv BT_CAL_DONE wait BT_CAL_GRANT\n");
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
					continue;
				}

				break;
			}
		} else if ((recv_type == gpm_type) &&
			   (recv_opcode == gpm_opcode))
			break;

		/* not expected message */

		/*
		 * check if it's cal_grant
		 *
		 * When we're waiting for cal_grant in reset routine,
		 * it's possible that BT sends out cal_request at the
		 * same time. Since BT's calibration doesn't happen
		 * that often, we'll let BT completes calibration then
		 * we continue to wait for cal_grant from BT.
		 * Orginal: Wait BT_CAL_GRANT.
		 * New: Receive BT_CAL_REQ -> send WLAN_CAL_GRANT->wait
		 * BT_CAL_DONE -> Wait BT_CAL_GRANT.
		 */

		if ((gpm_type == MCI_GPM_BT_CAL_GRANT) &&
		    (recv_type == MCI_GPM_BT_CAL_REQ)) {

			u32 payload[4] = {0, 0, 0, 0};

			gpm_type = MCI_GPM_BT_CAL_DONE;
1216
			ath_dbg(common, MCI,
1217 1218 1219 1220 1221 1222 1223 1224
				"MCI Rcv BT_CAL_REQ, send WLAN_CAL_GRANT\n");

			MCI_GPM_SET_CAL_TYPE(payload,
					MCI_GPM_WLAN_CAL_GRANT);

			ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
						false, false);

1225
			ath_dbg(common, MCI, "MCI now wait for BT_CAL_DONE\n");
1226 1227 1228

			continue;
		} else {
1229 1230
			ath_dbg(common, MCI, "MCI GPM subtype not match 0x%x\n",
				*(p_gpm + 1));
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
			mismatch++;
			ar9003_mci_process_gpm_extra(ah, recv_type,
					recv_opcode, p_gpm);
		}
	}
	if (p_gpm) {
		MCI_GPM_RECYCLE(p_gpm);
		p_gpm = NULL;
	}

	if (time_out <= 0) {
		time_out = 0;
1243
		ath_dbg(common, MCI,
1244 1245
			"MCI GPM received timeout, mismatch = %d\n", mismatch);
	} else
1246
		ath_dbg(common, MCI, "MCI Receive GPM type=0x%x, code=0x%x\n",
1247 1248 1249 1250
			gpm_type, gpm_opcode);

	while (more_data == MCI_GPM_MORE) {

1251
		ath_dbg(common, MCI, "MCI discard remaining GPM\n");
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
		offset = ar9003_mci_state(ah, MCI_STATE_NEXT_GPM_OFFSET,
					  &more_data);

		if (offset == MCI_GPM_INVALID)
			break;

		p_gpm = (u32 *) (mci->gpm_buf + offset);
		recv_type = MCI_GPM_TYPE(p_gpm);
		recv_opcode = MCI_GPM_OPCODE(p_gpm);

		if (!MCI_GPM_IS_CAL_TYPE(recv_type))
			ar9003_mci_process_gpm_extra(ah, recv_type,
						     recv_opcode, p_gpm);

		MCI_GPM_RECYCLE(p_gpm);
	}

	return time_out;
}

u32 ar9003_mci_state(struct ath_hw *ah, u32 state_type, u32 *p_data)
{
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
	u32 value = 0, more_gpm = 0, gpm_ptr;
	u8 query_type;

1279 1280 1281
	if (!ATH9K_HW_CAP_MCI)
		return 0;

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	switch (state_type) {
	case MCI_STATE_ENABLE:
		if (mci->ready) {

			value = REG_READ(ah, AR_BTCOEX_CTRL);

			if ((value == 0xdeadbeef) || (value == 0xffffffff))
				value = 0;
		}
		value &= AR_BTCOEX_CTRL_MCI_MODE_EN;
		break;
	case MCI_STATE_INIT_GPM_OFFSET:
		value = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
1295
		ath_dbg(common, MCI, "MCI GPM initial WRITE_PTR=%d\n", value);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
		mci->gpm_idx = value;
		break;
	case MCI_STATE_NEXT_GPM_OFFSET:
	case MCI_STATE_LAST_GPM_OFFSET:
		/*
		* This could be useful to avoid new GPM message interrupt which
		* may lead to spurious interrupt after power sleep, or multiple
		* entry of ath_mci_intr().
		* Adding empty GPM check by returning HAL_MCI_GPM_INVALID can
		* alleviate this effect, but clearing GPM RX interrupt bit is
		* safe, because whether this is called from hw or driver code
		* there must be an interrupt bit set/triggered initially
		*/
		REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
			  AR_MCI_INTERRUPT_RX_MSG_GPM);

		gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
		value = gpm_ptr;

		if (value == 0)
			value = mci->gpm_len - 1;
		else if (value >= mci->gpm_len) {
			if (value != 0xFFFF) {
				value = 0;
1320 1321
				ath_dbg(common, MCI,
					"MCI GPM offset out of range\n");
1322 1323 1324 1325 1326 1327 1328
			}
		} else
			value--;

		if (value == 0xFFFF) {
			value = MCI_GPM_INVALID;
			more_gpm = MCI_GPM_NOMORE;
1329 1330
			ath_dbg(common, MCI,
				"MCI GPM ptr invalid @ptr=%d, offset=%d, more=GPM_NOMORE\n",
1331 1332 1333 1334 1335 1336 1337
				gpm_ptr, value);
		} else if (state_type == MCI_STATE_NEXT_GPM_OFFSET) {

			if (gpm_ptr == mci->gpm_idx) {
				value = MCI_GPM_INVALID;
				more_gpm = MCI_GPM_NOMORE;

1338 1339 1340
				ath_dbg(common, MCI,
					"MCI GPM message not available @ptr=%d, @offset=%d, more=GPM_NOMORE\n",
					gpm_ptr, value);
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
			} else {
				for (;;) {

					u32 temp_index;

					/* skip reserved GPM if any */

					if (value != mci->gpm_idx)
						more_gpm = MCI_GPM_MORE;
					else
						more_gpm = MCI_GPM_NOMORE;

					temp_index = mci->gpm_idx;
					mci->gpm_idx++;

					if (mci->gpm_idx >=
					    mci->gpm_len)
						mci->gpm_idx = 0;

1360 1361
					ath_dbg(common, MCI,
						"MCI GPM message got ptr=%d, @offset=%d, more=%d\n",
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
						gpm_ptr, temp_index,
						(more_gpm == MCI_GPM_MORE));

					if (ar9003_mci_is_gpm_valid(ah,
								temp_index)) {
						value = temp_index;
						break;
					}

					if (more_gpm == MCI_GPM_NOMORE) {
						value = MCI_GPM_INVALID;
						break;
					}
				}
			}
			if (p_data)
				*p_data = more_gpm;
			}

			if (value != MCI_GPM_INVALID)
				value <<= 4;

			break;
	case MCI_STATE_LAST_SCHD_MSG_OFFSET:
		value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
				    AR_MCI_RX_LAST_SCHD_MSG_INDEX);
		/* Make it in bytes */
		value <<= 4;
		break;

	case MCI_STATE_REMOTE_SLEEP:
		value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
			   AR_MCI_RX_REMOTE_SLEEP) ?
			MCI_BT_SLEEP : MCI_BT_AWAKE;
		break;

	case MCI_STATE_CONT_RSSI_POWER:
		value = MS(mci->cont_status, AR_MCI_CONT_RSSI_POWER);
			break;

	case MCI_STATE_CONT_PRIORITY:
		value = MS(mci->cont_status, AR_MCI_CONT_RRIORITY);
		break;

	case MCI_STATE_CONT_TXRX:
		value = MS(mci->cont_status, AR_MCI_CONT_TXRX);
		break;

	case MCI_STATE_BT:
		value = mci->bt_state;
		break;

	case MCI_STATE_SET_BT_SLEEP:
		mci->bt_state = MCI_BT_SLEEP;
		break;

	case MCI_STATE_SET_BT_AWAKE:
		mci->bt_state = MCI_BT_AWAKE;
		ar9003_mci_send_coex_version_query(ah, true);
		ar9003_mci_send_coex_wlan_channels(ah, true);

		if (mci->unhalt_bt_gpm) {

1425
			ath_dbg(common, MCI, "MCI unhalt BT GPM\n");
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
			ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
		}

		ar9003_mci_2g5g_switch(ah, true);
		break;

	case MCI_STATE_SET_BT_CAL_START:
		mci->bt_state = MCI_BT_CAL_START;
		break;

	case MCI_STATE_SET_BT_CAL:
		mci->bt_state = MCI_BT_CAL;
		break;

	case MCI_STATE_RESET_REQ_WAKE:
		ar9003_mci_reset_req_wakeup(ah);
		mci->update_2g5g = true;

		if ((AR_SREV_9462_20_OR_LATER(ah)) &&
		    (mci->config & ATH_MCI_CONFIG_MCI_OBS_MASK)) {
			/* Check if we still have control of the GPIOs */
			if ((REG_READ(ah, AR_GLB_GPIO_CONTROL) &
				      ATH_MCI_CONFIG_MCI_OBS_GPIO) !=
					ATH_MCI_CONFIG_MCI_OBS_GPIO) {

1451 1452
				ath_dbg(common, MCI,
					"MCI reconfigure observation\n");
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
				ar9003_mci_observation_set_up(ah);
			}
		}
		break;

	case MCI_STATE_SEND_WLAN_COEX_VERSION:
		ar9003_mci_send_coex_version_response(ah, true);
		break;

	case MCI_STATE_SET_BT_COEX_VERSION:

		if (!p_data)
1465
			ath_dbg(common, MCI,
1466 1467 1468 1469 1470
				"MCI Set BT Coex version with NULL data!!\n");
		else {
			mci->bt_ver_major = (*p_data >> 8) & 0xff;
			mci->bt_ver_minor = (*p_data) & 0xff;
			mci->bt_version_known = true;
1471 1472
			ath_dbg(common, MCI, "MCI BT version set: %d.%d\n",
				mci->bt_ver_major, mci->bt_ver_minor);
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
		}
		break;

	case MCI_STATE_SEND_WLAN_CHANNELS:
		if (p_data) {
			if (((mci->wlan_channels[1] & 0xffff0000) ==
			     (*(p_data + 1) & 0xffff0000)) &&
			    (mci->wlan_channels[2] == *(p_data + 2)) &&
			    (mci->wlan_channels[3] == *(p_data + 3)))
				break;

			mci->wlan_channels[0] = *p_data++;
			mci->wlan_channels[1] = *p_data++;
			mci->wlan_channels[2] = *p_data++;
			mci->wlan_channels[3] = *p_data++;
		}
		mci->wlan_channels_update = true;
		ar9003_mci_send_coex_wlan_channels(ah, true);
		break;

	case MCI_STATE_SEND_VERSION_QUERY:
		ar9003_mci_send_coex_version_query(ah, true);
		break;

	case MCI_STATE_SEND_STATUS_QUERY:
1498
		query_type = MCI_GPM_COEX_QUERY_BT_TOPOLOGY;
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524

		ar9003_mci_send_coex_bt_status_query(ah, true, query_type);
		break;

	case MCI_STATE_NEED_FLUSH_BT_INFO:
			/*
			 * btcoex_hw.mci.unhalt_bt_gpm means whether it's
			 * needed to send UNHALT message. It's set whenever
			 * there's a request to send HALT message.
			 * mci_halted_bt_gpm means whether HALT message is sent
			 * out successfully.
			 *
			 * Checking (mci_unhalt_bt_gpm == false) instead of
			 * checking (ah->mci_halted_bt_gpm == false) will make
			 * sure currently is in UNHALT-ed mode and BT can
			 * respond to status query.
			 */
			value = (!mci->unhalt_bt_gpm &&
				 mci->need_flush_btinfo) ? 1 : 0;
			if (p_data)
				mci->need_flush_btinfo =
					(*p_data != 0) ? true : false;
			break;

	case MCI_STATE_RECOVER_RX:

1525
		ath_dbg(common, MCI, "MCI hw RECOVER_RX\n");
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
		ar9003_mci_prep_interface(ah);
		mci->query_bt = true;
		mci->need_flush_btinfo = true;
		ar9003_mci_send_coex_wlan_channels(ah, true);
		ar9003_mci_2g5g_switch(ah, true);
		break;

	case MCI_STATE_NEED_FTP_STOMP:
		value = !(mci->config & ATH_MCI_CONFIG_DISABLE_FTP_STOMP);
		break;

	case MCI_STATE_NEED_TUNING:
		value = !(mci->config & ATH_MCI_CONFIG_DISABLE_TUNING);
		break;

	default:
		break;

	}

	return value;
}
EXPORT_SYMBOL(ar9003_mci_state);