phy.c 119.7 KB
Newer Older
1 2
/******************************************************************************
 *
L
Larry Finger 已提交
3
 * Copyright(c) 2009-2012  Realtek Corporation.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 199 200 201 202 203 204 205 206
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
 * wlanfae <wlanfae@realtek.com>
 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
 * Hsinchu 300, Taiwan.
 *
 * Larry Finger <Larry.Finger@lwfinger.net>
 *
 *****************************************************************************/

#include "../wifi.h"
#include "../pci.h"
#include "../ps.h"
#include "reg.h"
#include "def.h"
#include "phy.h"
#include "rf.h"
#include "dm.h"
#include "table.h"
#include "sw.h"
#include "hw.h"

#define MAX_RF_IMR_INDEX			12
#define MAX_RF_IMR_INDEX_NORMAL			13
#define RF_REG_NUM_FOR_C_CUT_5G			6
#define RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA	7
#define RF_REG_NUM_FOR_C_CUT_2G			5
#define RF_CHNL_NUM_5G				19
#define RF_CHNL_NUM_5G_40M			17
#define TARGET_CHNL_NUM_5G			221
#define TARGET_CHNL_NUM_2G			14
#define CV_CURVE_CNT				64

static u32 rf_reg_for_5g_swchnl_normal[MAX_RF_IMR_INDEX_NORMAL] = {
	0, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x0
};

static u8 rf_reg_for_c_cut_5g[RF_REG_NUM_FOR_C_CUT_5G] = {
	RF_SYN_G1, RF_SYN_G2, RF_SYN_G3, RF_SYN_G4, RF_SYN_G5, RF_SYN_G6
};

static u8 rf_reg_for_c_cut_2g[RF_REG_NUM_FOR_C_CUT_2G] = {
	RF_SYN_G1, RF_SYN_G2, RF_SYN_G3, RF_SYN_G7, RF_SYN_G8
};

static u8 rf_for_c_cut_5g_internal_pa[RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA] = {
	0x0B, 0x48, 0x49, 0x4B, 0x03, 0x04, 0x0E
};

static u32 rf_reg_mask_for_c_cut_2g[RF_REG_NUM_FOR_C_CUT_2G] = {
	BIT(19) | BIT(18) | BIT(17) | BIT(14) | BIT(1),
	BIT(10) | BIT(9),
	BIT(18) | BIT(17) | BIT(16) | BIT(1),
	BIT(2) | BIT(1),
	BIT(15) | BIT(14) | BIT(13) | BIT(12) | BIT(11)
};

static u8 rf_chnl_5g[RF_CHNL_NUM_5G] = {
	36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108,
	112, 116, 120, 124, 128, 132, 136, 140
};

static u8 rf_chnl_5g_40m[RF_CHNL_NUM_5G_40M] = {
	38, 42, 46, 50, 54, 58, 62, 102, 106, 110, 114,
	118, 122, 126, 130, 134, 138
};
static u32 rf_reg_pram_c_5g[5][RF_REG_NUM_FOR_C_CUT_5G] = {
	{0xE43BE, 0xFC638, 0x77C0A, 0xDE471, 0xd7110, 0x8EB04},
	{0xE43BE, 0xFC078, 0xF7C1A, 0xE0C71, 0xD7550, 0xAEB04},
	{0xE43BF, 0xFF038, 0xF7C0A, 0xDE471, 0xE5550, 0xAEB04},
	{0xE43BF, 0xFF079, 0xF7C1A, 0xDE471, 0xE5550, 0xAEB04},
	{0xE43BF, 0xFF038, 0xF7C1A, 0xDE471, 0xd7550, 0xAEB04}
};

static u32 rf_reg_param_for_c_cut_2g[3][RF_REG_NUM_FOR_C_CUT_2G] = {
	{0x643BC, 0xFC038, 0x77C1A, 0x41289, 0x01840},
	{0x643BC, 0xFC038, 0x07C1A, 0x41289, 0x01840},
	{0x243BC, 0xFC438, 0x07C1A, 0x4128B, 0x0FC41}
};

static u32 rf_syn_g4_for_c_cut_2g = 0xD1C31 & 0x7FF;

static u32 rf_pram_c_5g_int_pa[3][RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA] = {
	{0x01a00, 0x40443, 0x00eb5, 0x89bec, 0x94a12, 0x94a12, 0x94a12},
	{0x01800, 0xc0443, 0x00730, 0x896ee, 0x94a52, 0x94a52, 0x94a52},
	{0x01800, 0xc0443, 0x00730, 0x896ee, 0x94a12, 0x94a12, 0x94a12}
};

/* [mode][patha+b][reg] */
static u32 rf_imr_param_normal[1][3][MAX_RF_IMR_INDEX_NORMAL] = {
	{
		/* channel 1-14. */
		{
			0x70000, 0x00ff0, 0x4400f, 0x00ff0, 0x0, 0x0, 0x0,
			0x0, 0x0, 0x64888, 0xe266c, 0x00090, 0x22fff
		},
		/* path 36-64 */
		{
			0x70000, 0x22880, 0x4470f, 0x55880, 0x00070, 0x88000,
			0x0, 0x88080, 0x70000, 0x64a82, 0xe466c, 0x00090,
			0x32c9a
		},
		/* 100 -165 */
		{
			0x70000, 0x44880, 0x4477f, 0x77880, 0x00070, 0x88000,
			0x0, 0x880b0, 0x0, 0x64b82, 0xe466c, 0x00090, 0x32c9a
		}
	}
};

static u32 curveindex_5g[TARGET_CHNL_NUM_5G] = {0};

static u32 curveindex_2g[TARGET_CHNL_NUM_2G] = {0};

static u32 targetchnl_5g[TARGET_CHNL_NUM_5G] = {
	25141, 25116, 25091, 25066, 25041,
	25016, 24991, 24966, 24941, 24917,
	24892, 24867, 24843, 24818, 24794,
	24770, 24765, 24721, 24697, 24672,
	24648, 24624, 24600, 24576, 24552,
	24528, 24504, 24480, 24457, 24433,
	24409, 24385, 24362, 24338, 24315,
	24291, 24268, 24245, 24221, 24198,
	24175, 24151, 24128, 24105, 24082,
	24059, 24036, 24013, 23990, 23967,
	23945, 23922, 23899, 23876, 23854,
	23831, 23809, 23786, 23764, 23741,
	23719, 23697, 23674, 23652, 23630,
	23608, 23586, 23564, 23541, 23519,
	23498, 23476, 23454, 23432, 23410,
	23388, 23367, 23345, 23323, 23302,
	23280, 23259, 23237, 23216, 23194,
	23173, 23152, 23130, 23109, 23088,
	23067, 23046, 23025, 23003, 22982,
	22962, 22941, 22920, 22899, 22878,
	22857, 22837, 22816, 22795, 22775,
	22754, 22733, 22713, 22692, 22672,
	22652, 22631, 22611, 22591, 22570,
	22550, 22530, 22510, 22490, 22469,
	22449, 22429, 22409, 22390, 22370,
	22350, 22336, 22310, 22290, 22271,
	22251, 22231, 22212, 22192, 22173,
	22153, 22134, 22114, 22095, 22075,
	22056, 22037, 22017, 21998, 21979,
	21960, 21941, 21921, 21902, 21883,
	21864, 21845, 21826, 21807, 21789,
	21770, 21751, 21732, 21713, 21695,
	21676, 21657, 21639, 21620, 21602,
	21583, 21565, 21546, 21528, 21509,
	21491, 21473, 21454, 21436, 21418,
	21400, 21381, 21363, 21345, 21327,
	21309, 21291, 21273, 21255, 21237,
	21219, 21201, 21183, 21166, 21148,
	21130, 21112, 21095, 21077, 21059,
	21042, 21024, 21007, 20989, 20972,
	25679, 25653, 25627, 25601, 25575,
	25549, 25523, 25497, 25471, 25446,
	25420, 25394, 25369, 25343, 25318,
	25292, 25267, 25242, 25216, 25191,
	25166
};

/* channel 1~14 */
static u32 targetchnl_2g[TARGET_CHNL_NUM_2G] = {
	26084, 26030, 25976, 25923, 25869, 25816, 25764,
	25711, 25658, 25606, 25554, 25502, 25451, 25328
};

static u32 _rtl92d_phy_calculate_bit_shift(u32 bitmask)
{
	u32 i;

	for (i = 0; i <= 31; i++) {
		if (((bitmask >> i) & 0x1) == 1)
			break;
	}

	return i;
}

u32 rtl92d_phy_query_bb_reg(struct ieee80211_hw *hw, u32 regaddr, u32 bitmask)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
	u32 returnvalue, originalvalue, bitshift;
	u8 dbi_direct;

207 208
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, "regaddr(%#x), bitmask(%#x)\n",
		 regaddr, bitmask);
209 210 211 212 213 214 215 216 217 218 219 220 221 222
	if (rtlhal->during_mac1init_radioa || rtlhal->during_mac0init_radiob) {
		/* mac1 use phy0 read radio_b. */
		/* mac0 use phy1 read radio_b. */
		if (rtlhal->during_mac1init_radioa)
			dbi_direct = BIT(3);
		else if (rtlhal->during_mac0init_radiob)
			dbi_direct = BIT(3) | BIT(2);
		originalvalue = rtl92de_read_dword_dbi(hw, (u16)regaddr,
			dbi_direct);
	} else {
		originalvalue = rtl_read_dword(rtlpriv, regaddr);
	}
	bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
	returnvalue = (originalvalue & bitmask) >> bitshift;
223 224 225
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
		 "BBR MASK=0x%x Addr[0x%x]=0x%x\n",
		 bitmask, regaddr, originalvalue);
226 227 228 229 230 231 232 233 234 235 236
	return returnvalue;
}

void rtl92d_phy_set_bb_reg(struct ieee80211_hw *hw,
			   u32 regaddr, u32 bitmask, u32 data)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
	u8 dbi_direct = 0;
	u32 originalvalue, bitshift;

237 238 239
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
		 "regaddr(%#x), bitmask(%#x), data(%#x)\n",
		 regaddr, bitmask, data);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	if (rtlhal->during_mac1init_radioa)
		dbi_direct = BIT(3);
	else if (rtlhal->during_mac0init_radiob)
		/* mac0 use phy1 write radio_b. */
		dbi_direct = BIT(3) | BIT(2);
	if (bitmask != BMASKDWORD) {
		if (rtlhal->during_mac1init_radioa ||
		    rtlhal->during_mac0init_radiob)
			originalvalue = rtl92de_read_dword_dbi(hw,
					(u16) regaddr,
					dbi_direct);
		else
			originalvalue = rtl_read_dword(rtlpriv, regaddr);
		bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
		data = ((originalvalue & (~bitmask)) | (data << bitshift));
	}
	if (rtlhal->during_mac1init_radioa || rtlhal->during_mac0init_radiob)
		rtl92de_write_dword_dbi(hw, (u16) regaddr, data, dbi_direct);
	else
		rtl_write_dword(rtlpriv, regaddr, data);
260 261 262
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
		 "regaddr(%#x), bitmask(%#x), data(%#x)\n",
		 regaddr, bitmask, data);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

static u32 _rtl92d_phy_rf_serial_read(struct ieee80211_hw *hw,
				      enum radio_path rfpath, u32 offset)
{

	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
	u32 newoffset;
	u32 tmplong, tmplong2;
	u8 rfpi_enable = 0;
	u32 retvalue;

	newoffset = offset;
	tmplong = rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, BMASKDWORD);
	if (rfpath == RF90_PATH_A)
		tmplong2 = tmplong;
	else
		tmplong2 = rtl_get_bbreg(hw, pphyreg->rfhssi_para2, BMASKDWORD);
	tmplong2 = (tmplong2 & (~BLSSIREADADDRESS)) |
		(newoffset << 23) | BLSSIREADEDGE;
	rtl_set_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, BMASKDWORD,
		tmplong & (~BLSSIREADEDGE));
	udelay(10);
	rtl_set_bbreg(hw, pphyreg->rfhssi_para2, BMASKDWORD, tmplong2);
	udelay(50);
	udelay(50);
	rtl_set_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, BMASKDWORD,
		tmplong | BLSSIREADEDGE);
	udelay(10);
	if (rfpath == RF90_PATH_A)
		rfpi_enable = (u8) rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER1,
			      BIT(8));
	else if (rfpath == RF90_PATH_B)
		rfpi_enable = (u8) rtl_get_bbreg(hw, RFPGA0_XB_HSSIPARAMETER1,
			      BIT(8));
	if (rfpi_enable)
		retvalue = rtl_get_bbreg(hw, pphyreg->rflssi_readbackpi,
			BLSSIREADBACKDATA);
	else
		retvalue = rtl_get_bbreg(hw, pphyreg->rflssi_readback,
			BLSSIREADBACKDATA);
306 307
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, "RFR-%d Addr[0x%x] = 0x%x\n",
		 rfpath, pphyreg->rflssi_readback, retvalue);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	return retvalue;
}

static void _rtl92d_phy_rf_serial_write(struct ieee80211_hw *hw,
					enum radio_path rfpath,
					u32 offset, u32 data)
{
	u32 data_and_addr;
	u32 newoffset;
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];

	newoffset = offset;
	/* T65 RF */
	data_and_addr = ((newoffset << 20) | (data & 0x000fffff)) & 0x0fffffff;
	rtl_set_bbreg(hw, pphyreg->rf3wire_offset, BMASKDWORD, data_and_addr);
325 326
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, "RFW-%d Addr[0x%x]=0x%x\n",
		 rfpath, pphyreg->rf3wire_offset, data_and_addr);
327 328 329 330 331 332 333 334 335
}

u32 rtl92d_phy_query_rf_reg(struct ieee80211_hw *hw,
			    enum radio_path rfpath, u32 regaddr, u32 bitmask)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 original_value, readback_value, bitshift;
	unsigned long flags;

336 337 338
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
		 "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
		 regaddr, rfpath, bitmask);
339 340 341 342 343
	spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
	original_value = _rtl92d_phy_rf_serial_read(hw, rfpath, regaddr);
	bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
	readback_value = (original_value & bitmask) >> bitshift;
	spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
344 345 346
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
		 "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
		 regaddr, rfpath, bitmask, original_value);
347 348 349 350 351 352 353 354 355 356 357 358
	return readback_value;
}

void rtl92d_phy_set_rf_reg(struct ieee80211_hw *hw, enum radio_path rfpath,
	u32 regaddr, u32 bitmask, u32 data)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	u32 original_value, bitshift;
	unsigned long flags;

	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
359 360
		 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
		 regaddr, bitmask, data, rfpath);
361 362 363 364 365 366 367 368 369 370 371 372 373 374
	if (bitmask == 0)
		return;
	spin_lock_irqsave(&rtlpriv->locks.rf_lock, flags);
	if (rtlphy->rf_mode != RF_OP_BY_FW) {
		if (bitmask != BRFREGOFFSETMASK) {
			original_value = _rtl92d_phy_rf_serial_read(hw,
				rfpath, regaddr);
			bitshift = _rtl92d_phy_calculate_bit_shift(bitmask);
			data = ((original_value & (~bitmask)) |
				(data << bitshift));
		}
		_rtl92d_phy_rf_serial_write(hw, rfpath, regaddr, data);
	}
	spin_unlock_irqrestore(&rtlpriv->locks.rf_lock, flags);
375 376 377
	RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
		 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
		 regaddr, bitmask, data, rfpath);
378 379 380 381 382 383 384 385 386
}

bool rtl92d_phy_mac_config(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 i;
	u32 arraylength;
	u32 *ptrarray;

387
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Read Rtl819XMACPHY_Array\n");
388 389
	arraylength = MAC_2T_ARRAYLENGTH;
	ptrarray = rtl8192de_mac_2tarray;
390
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Img:Rtl819XMAC_Array\n");
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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 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 560 561 562 563 564 565 566
	for (i = 0; i < arraylength; i = i + 2)
		rtl_write_byte(rtlpriv, ptrarray[i], (u8) ptrarray[i + 1]);
	if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY) {
		/* improve 2-stream TX EVM */
		/* rtl_write_byte(rtlpriv, 0x14,0x71); */
		/* AMPDU aggregation number 9 */
		/* rtl_write_word(rtlpriv, REG_MAX_AGGR_NUM, MAX_AGGR_NUM); */
		rtl_write_byte(rtlpriv, REG_MAX_AGGR_NUM, 0x0B);
	} else {
		/* 92D need to test to decide the num. */
		rtl_write_byte(rtlpriv, REG_MAX_AGGR_NUM, 0x07);
	}
	return true;
}

static void _rtl92d_phy_init_bb_rf_register_definition(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);

	/* RF Interface Sowrtware Control */
	/* 16 LSBs if read 32-bit from 0x870 */
	rtlphy->phyreg_def[RF90_PATH_A].rfintfs = RFPGA0_XAB_RFINTERFACESW;
	/* 16 MSBs if read 32-bit from 0x870 (16-bit for 0x872) */
	rtlphy->phyreg_def[RF90_PATH_B].rfintfs = RFPGA0_XAB_RFINTERFACESW;
	/* 16 LSBs if read 32-bit from 0x874 */
	rtlphy->phyreg_def[RF90_PATH_C].rfintfs = RFPGA0_XCD_RFINTERFACESW;
	/* 16 MSBs if read 32-bit from 0x874 (16-bit for 0x876) */

	rtlphy->phyreg_def[RF90_PATH_D].rfintfs = RFPGA0_XCD_RFINTERFACESW;
	/* RF Interface Readback Value */
	/* 16 LSBs if read 32-bit from 0x8E0 */
	rtlphy->phyreg_def[RF90_PATH_A].rfintfi = RFPGA0_XAB_RFINTERFACERB;
	/* 16 MSBs if read 32-bit from 0x8E0 (16-bit for 0x8E2) */
	rtlphy->phyreg_def[RF90_PATH_B].rfintfi = RFPGA0_XAB_RFINTERFACERB;
	/* 16 LSBs if read 32-bit from 0x8E4 */
	rtlphy->phyreg_def[RF90_PATH_C].rfintfi = RFPGA0_XCD_RFINTERFACERB;
	/* 16 MSBs if read 32-bit from 0x8E4 (16-bit for 0x8E6) */
	rtlphy->phyreg_def[RF90_PATH_D].rfintfi = RFPGA0_XCD_RFINTERFACERB;

	/* RF Interface Output (and Enable) */
	/* 16 LSBs if read 32-bit from 0x860 */
	rtlphy->phyreg_def[RF90_PATH_A].rfintfo = RFPGA0_XA_RFINTERFACEOE;
	/* 16 LSBs if read 32-bit from 0x864 */
	rtlphy->phyreg_def[RF90_PATH_B].rfintfo = RFPGA0_XB_RFINTERFACEOE;

	/* RF Interface (Output and)  Enable */
	/* 16 MSBs if read 32-bit from 0x860 (16-bit for 0x862) */
	rtlphy->phyreg_def[RF90_PATH_A].rfintfe = RFPGA0_XA_RFINTERFACEOE;
	/* 16 MSBs if read 32-bit from 0x864 (16-bit for 0x866) */
	rtlphy->phyreg_def[RF90_PATH_B].rfintfe = RFPGA0_XB_RFINTERFACEOE;

	/* Addr of LSSI. Wirte RF register by driver */
	/* LSSI Parameter */
	rtlphy->phyreg_def[RF90_PATH_A].rf3wire_offset =
				 RFPGA0_XA_LSSIPARAMETER;
	rtlphy->phyreg_def[RF90_PATH_B].rf3wire_offset =
				 RFPGA0_XB_LSSIPARAMETER;

	/* RF parameter */
	/* BB Band Select */
	rtlphy->phyreg_def[RF90_PATH_A].rflssi_select = RFPGA0_XAB_RFPARAMETER;
	rtlphy->phyreg_def[RF90_PATH_B].rflssi_select = RFPGA0_XAB_RFPARAMETER;
	rtlphy->phyreg_def[RF90_PATH_C].rflssi_select = RFPGA0_XCD_RFPARAMETER;
	rtlphy->phyreg_def[RF90_PATH_D].rflssi_select = RFPGA0_XCD_RFPARAMETER;

	/* Tx AGC Gain Stage (same for all path. Should we remove this?) */
	/* Tx gain stage */
	rtlphy->phyreg_def[RF90_PATH_A].rftxgain_stage = RFPGA0_TXGAINSTAGE;
	/* Tx gain stage */
	rtlphy->phyreg_def[RF90_PATH_B].rftxgain_stage = RFPGA0_TXGAINSTAGE;
	/* Tx gain stage */
	rtlphy->phyreg_def[RF90_PATH_C].rftxgain_stage = RFPGA0_TXGAINSTAGE;
	/* Tx gain stage */
	rtlphy->phyreg_def[RF90_PATH_D].rftxgain_stage = RFPGA0_TXGAINSTAGE;

	/* Tranceiver A~D HSSI Parameter-1 */
	/* wire control parameter1 */
	rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para1 = RFPGA0_XA_HSSIPARAMETER1;
	/* wire control parameter1 */
	rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para1 = RFPGA0_XB_HSSIPARAMETER1;

	/* Tranceiver A~D HSSI Parameter-2 */
	/* wire control parameter2 */
	rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para2 = RFPGA0_XA_HSSIPARAMETER2;
	/* wire control parameter2 */
	rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para2 = RFPGA0_XB_HSSIPARAMETER2;

	/* RF switch Control */
	/* TR/Ant switch control */
	rtlphy->phyreg_def[RF90_PATH_A].rfswitch_control =
		RFPGA0_XAB_SWITCHCONTROL;
	rtlphy->phyreg_def[RF90_PATH_B].rfswitch_control =
	    RFPGA0_XAB_SWITCHCONTROL;
	rtlphy->phyreg_def[RF90_PATH_C].rfswitch_control =
	    RFPGA0_XCD_SWITCHCONTROL;
	rtlphy->phyreg_def[RF90_PATH_D].rfswitch_control =
	    RFPGA0_XCD_SWITCHCONTROL;

	/* AGC control 1 */
	rtlphy->phyreg_def[RF90_PATH_A].rfagc_control1 = ROFDM0_XAAGCCORE1;
	rtlphy->phyreg_def[RF90_PATH_B].rfagc_control1 = ROFDM0_XBAGCCORE1;
	rtlphy->phyreg_def[RF90_PATH_C].rfagc_control1 = ROFDM0_XCAGCCORE1;
	rtlphy->phyreg_def[RF90_PATH_D].rfagc_control1 = ROFDM0_XDAGCCORE1;

	/* AGC control 2  */
	rtlphy->phyreg_def[RF90_PATH_A].rfagc_control2 = ROFDM0_XAAGCCORE2;
	rtlphy->phyreg_def[RF90_PATH_B].rfagc_control2 = ROFDM0_XBAGCCORE2;
	rtlphy->phyreg_def[RF90_PATH_C].rfagc_control2 = ROFDM0_XCAGCCORE2;
	rtlphy->phyreg_def[RF90_PATH_D].rfagc_control2 = ROFDM0_XDAGCCORE2;

	/* RX AFE control 1 */
	rtlphy->phyreg_def[RF90_PATH_A].rfrxiq_imbalance =
	    ROFDM0_XARXIQIMBALANCE;
	rtlphy->phyreg_def[RF90_PATH_B].rfrxiq_imbalance =
	    ROFDM0_XBRXIQIMBALANCE;
	rtlphy->phyreg_def[RF90_PATH_C].rfrxiq_imbalance =
	    ROFDM0_XCRXIQIMBALANCE;
	rtlphy->phyreg_def[RF90_PATH_D].rfrxiq_imbalance =
	    ROFDM0_XDRXIQIMBALANCE;

	/*RX AFE control 1 */
	rtlphy->phyreg_def[RF90_PATH_A].rfrx_afe = ROFDM0_XARXAFE;
	rtlphy->phyreg_def[RF90_PATH_B].rfrx_afe = ROFDM0_XBRXAFE;
	rtlphy->phyreg_def[RF90_PATH_C].rfrx_afe = ROFDM0_XCRXAFE;
	rtlphy->phyreg_def[RF90_PATH_D].rfrx_afe = ROFDM0_XDRXAFE;

	/* Tx AFE control 1 */
	rtlphy->phyreg_def[RF90_PATH_A].rftxiq_imbalance =
	    ROFDM0_XATxIQIMBALANCE;
	rtlphy->phyreg_def[RF90_PATH_B].rftxiq_imbalance =
	    ROFDM0_XBTxIQIMBALANCE;
	rtlphy->phyreg_def[RF90_PATH_C].rftxiq_imbalance =
	    ROFDM0_XCTxIQIMBALANCE;
	rtlphy->phyreg_def[RF90_PATH_D].rftxiq_imbalance =
	    ROFDM0_XDTxIQIMBALANCE;

	/* Tx AFE control 2 */
	rtlphy->phyreg_def[RF90_PATH_A].rftx_afe = ROFDM0_XATxAFE;
	rtlphy->phyreg_def[RF90_PATH_B].rftx_afe = ROFDM0_XBTxAFE;
	rtlphy->phyreg_def[RF90_PATH_C].rftx_afe = ROFDM0_XCTxAFE;
	rtlphy->phyreg_def[RF90_PATH_D].rftx_afe = ROFDM0_XDTxAFE;

	/* Tranceiver LSSI Readback SI mode */
	rtlphy->phyreg_def[RF90_PATH_A].rflssi_readback =
	    RFPGA0_XA_LSSIREADBACK;
	rtlphy->phyreg_def[RF90_PATH_B].rflssi_readback =
	    RFPGA0_XB_LSSIREADBACK;
	rtlphy->phyreg_def[RF90_PATH_C].rflssi_readback =
	    RFPGA0_XC_LSSIREADBACK;
	rtlphy->phyreg_def[RF90_PATH_D].rflssi_readback =
	    RFPGA0_XD_LSSIREADBACK;

	/* Tranceiver LSSI Readback PI mode */
	rtlphy->phyreg_def[RF90_PATH_A].rflssi_readbackpi =
	    TRANSCEIVERA_HSPI_READBACK;
	rtlphy->phyreg_def[RF90_PATH_B].rflssi_readbackpi =
	    TRANSCEIVERB_HSPI_READBACK;
}

static bool _rtl92d_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
	u8 configtype)
{
	int i;
	u32 *phy_regarray_table;
	u32 *agctab_array_table = NULL;
	u32 *agctab_5garray_table;
	u16 phy_reg_arraylen, agctab_arraylen = 0, agctab_5garraylen;
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));

	/* Normal chip,Mac0 use AGC_TAB.txt for 2G and 5G band. */
	if (rtlhal->interfaceindex == 0) {
		agctab_arraylen = AGCTAB_ARRAYLENGTH;
		agctab_array_table = rtl8192de_agctab_array;
		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
567
			 " ===> phy:MAC0, Rtl819XAGCTAB_Array\n");
568 569 570 571 572
	} else {
		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
			agctab_arraylen = AGCTAB_2G_ARRAYLENGTH;
			agctab_array_table = rtl8192de_agctab_2garray;
			RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
573
				 " ===> phy:MAC1, Rtl819XAGCTAB_2GArray\n");
574 575 576 577
		} else {
			agctab_5garraylen = AGCTAB_5G_ARRAYLENGTH;
			agctab_5garray_table = rtl8192de_agctab_5garray;
			RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
578
				 " ===> phy:MAC1, Rtl819XAGCTAB_5GArray\n");
579 580 581 582 583 584

		}
	}
	phy_reg_arraylen = PHY_REG_2T_ARRAYLENGTH;
	phy_regarray_table = rtl8192de_phy_reg_2tarray;
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
585
		 " ===> phy:Rtl819XPHY_REG_Array_PG\n");
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	if (configtype == BASEBAND_CONFIG_PHY_REG) {
		for (i = 0; i < phy_reg_arraylen; i = i + 2) {
			if (phy_regarray_table[i] == 0xfe)
				mdelay(50);
			else if (phy_regarray_table[i] == 0xfd)
				mdelay(5);
			else if (phy_regarray_table[i] == 0xfc)
				mdelay(1);
			else if (phy_regarray_table[i] == 0xfb)
				udelay(50);
			else if (phy_regarray_table[i] == 0xfa)
				udelay(5);
			else if (phy_regarray_table[i] == 0xf9)
				udelay(1);
			rtl_set_bbreg(hw, phy_regarray_table[i], BMASKDWORD,
				      phy_regarray_table[i + 1]);
			udelay(1);
			RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
604 605 606
				 "The phy_regarray_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
				 phy_regarray_table[i],
				 phy_regarray_table[i + 1]);
607 608 609 610 611 612 613 614 615 616 617
		}
	} else if (configtype == BASEBAND_CONFIG_AGC_TAB) {
		if (rtlhal->interfaceindex == 0) {
			for (i = 0; i < agctab_arraylen; i = i + 2) {
				rtl_set_bbreg(hw, agctab_array_table[i],
					BMASKDWORD,
					agctab_array_table[i + 1]);
				/* Add 1us delay between BB/RF register
				 * setting. */
				udelay(1);
				RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
618
					 "The Rtl819XAGCTAB_Array_Table[0] is %ul Rtl819XPHY_REGArray[1] is %ul\n",
619
					 agctab_array_table[i],
620
					 agctab_array_table[i + 1]);
621 622
			}
			RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
623
				 "Normal Chip, MAC0, load Rtl819XAGCTAB_Array\n");
624 625 626 627 628 629 630 631 632 633
		} else {
			if (rtlhal->current_bandtype == BAND_ON_2_4G) {
				for (i = 0; i < agctab_arraylen; i = i + 2) {
					rtl_set_bbreg(hw, agctab_array_table[i],
						BMASKDWORD,
						agctab_array_table[i + 1]);
					/* Add 1us delay between BB/RF register
					 * setting. */
					udelay(1);
					RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
634
						 "The Rtl819XAGCTAB_Array_Table[0] is %ul Rtl819XPHY_REGArray[1] is %ul\n",
635
						 agctab_array_table[i],
636
						 agctab_array_table[i + 1]);
637 638
				}
				RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
639
					 "Load Rtl819XAGCTAB_2GArray\n");
640 641 642 643 644 645 646 647 648 649
			} else {
				for (i = 0; i < agctab_5garraylen; i = i + 2) {
					rtl_set_bbreg(hw,
						agctab_5garray_table[i],
						BMASKDWORD,
						agctab_5garray_table[i + 1]);
					/* Add 1us delay between BB/RF registeri
					 * setting. */
					udelay(1);
					RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
650
						 "The Rtl819XAGCTAB_5GArray_Table[0] is %ul Rtl819XPHY_REGArray[1] is %ul\n",
651
						 agctab_5garray_table[i],
652
						 agctab_5garray_table[i + 1]);
653 654
				}
				RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
655
					 "Load Rtl819XAGCTAB_5GArray\n");
656 657 658 659 660 661 662 663 664 665 666 667
			}
		}
	}
	return true;
}

static void _rtl92d_store_pwrindex_diffrate_offset(struct ieee80211_hw *hw,
						   u32 regaddr, u32 bitmask,
						   u32 data)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
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 703
	int index;

	if (regaddr == RTXAGC_A_RATE18_06)
		index = 0;
	else if (regaddr == RTXAGC_A_RATE54_24)
		index = 1;
	else if (regaddr == RTXAGC_A_CCK1_MCS32)
		index = 6;
	else if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0xffffff00)
		index = 7;
	else if (regaddr == RTXAGC_A_MCS03_MCS00)
		index = 2;
	else if (regaddr == RTXAGC_A_MCS07_MCS04)
		index = 3;
	else if (regaddr == RTXAGC_A_MCS11_MCS08)
		index = 4;
	else if (regaddr == RTXAGC_A_MCS15_MCS12)
		index = 5;
	else if (regaddr == RTXAGC_B_RATE18_06)
		index = 8;
	else if (regaddr == RTXAGC_B_RATE54_24)
		index = 9;
	else if (regaddr == RTXAGC_B_CCK1_55_MCS32)
		index = 14;
	else if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0x000000ff)
		index = 15;
	else if (regaddr == RTXAGC_B_MCS03_MCS00)
		index = 10;
	else if (regaddr == RTXAGC_B_MCS07_MCS04)
		index = 11;
	else if (regaddr == RTXAGC_B_MCS11_MCS08)
		index = 12;
	else if (regaddr == RTXAGC_B_MCS15_MCS12)
		index = 13;
	else
		return;
704

705 706 707 708 709 710 711
	rtlphy->mcs_txpwrlevel_origoffset[rtlphy->pwrgroup_cnt][index] = data;
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
		 "MCSTxPowerLevelOriginalOffset[%d][%d] = 0x%ulx\n",
		 rtlphy->pwrgroup_cnt, index,
		 rtlphy->mcs_txpwrlevel_origoffset
		 [rtlphy->pwrgroup_cnt][index]);
	if (index == 13)
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
		rtlphy->pwrgroup_cnt++;
}

static bool _rtl92d_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
	u8 configtype)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	int i;
	u32 *phy_regarray_table_pg;
	u16 phy_regarray_pg_len;

	phy_regarray_pg_len = PHY_REG_ARRAY_PG_LENGTH;
	phy_regarray_table_pg = rtl8192de_phy_reg_array_pg;
	if (configtype == BASEBAND_CONFIG_PHY_REG) {
		for (i = 0; i < phy_regarray_pg_len; i = i + 3) {
			if (phy_regarray_table_pg[i] == 0xfe)
				mdelay(50);
			else if (phy_regarray_table_pg[i] == 0xfd)
				mdelay(5);
			else if (phy_regarray_table_pg[i] == 0xfc)
				mdelay(1);
			else if (phy_regarray_table_pg[i] == 0xfb)
				udelay(50);
			else if (phy_regarray_table_pg[i] == 0xfa)
				udelay(5);
			else if (phy_regarray_table_pg[i] == 0xf9)
				udelay(1);
			_rtl92d_store_pwrindex_diffrate_offset(hw,
				phy_regarray_table_pg[i],
				phy_regarray_table_pg[i + 1],
				phy_regarray_table_pg[i + 2]);
		}
	} else {
		RT_TRACE(rtlpriv, COMP_SEND, DBG_TRACE,
746
			 "configtype != BaseBand_Config_PHY_REG\n");
747 748 749 750 751 752 753 754 755 756 757
	}
	return true;
}

static bool _rtl92d_phy_bb_config(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	bool rtstatus = true;

758
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "==>\n");
759 760
	rtstatus = _rtl92d_phy_config_bb_with_headerfile(hw,
		BASEBAND_CONFIG_PHY_REG);
761
	if (!rtstatus) {
762
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "Write BB Reg Fail!!\n");
763 764 765 766 767
		return false;
	}

	/* if (rtlphy->rf_type == RF_1T2R) {
	 *      _rtl92c_phy_bb_config_1t(hw);
768
	 *     RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Config to 1T!!\n");
769 770 771 772 773 774 775
	 *} */

	if (rtlefuse->autoload_failflag == false) {
		rtlphy->pwrgroup_cnt = 0;
		rtstatus = _rtl92d_phy_config_bb_with_pgheaderfile(hw,
			BASEBAND_CONFIG_PHY_REG);
	}
776
	if (!rtstatus) {
777
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "BB_PG Reg Fail!!\n");
778 779 780 781
		return false;
	}
	rtstatus = _rtl92d_phy_config_bb_with_headerfile(hw,
		BASEBAND_CONFIG_AGC_TAB);
782
	if (!rtstatus) {
783
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, "AGC Table Fail\n");
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
		return false;
	}
	rtlphy->cck_high_power = (bool) (rtl_get_bbreg(hw,
		RFPGA0_XA_HSSIPARAMETER2, 0x200));

	return true;
}

bool rtl92d_phy_bb_config(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u16 regval;
	u32 regvaldw;
	u8 value;

	_rtl92d_phy_init_bb_rf_register_definition(hw);
	regval = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
	rtl_write_word(rtlpriv, REG_SYS_FUNC_EN,
		       regval | BIT(13) | BIT(0) | BIT(1));
	rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL, 0x83);
	rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL + 1, 0xdb);
	/* 0x1f bit7 bit6 represent for mac0/mac1 driver ready */
	value = rtl_read_byte(rtlpriv, REG_RF_CTRL);
	rtl_write_byte(rtlpriv, REG_RF_CTRL, value | RF_EN | RF_RSTB |
		RF_SDMRSTB);
	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, FEN_PPLL | FEN_PCIEA |
		FEN_DIO_PCIE | FEN_BB_GLB_RSTn | FEN_BBRSTB);
	rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL + 1, 0x80);
	if (!(IS_92D_SINGLEPHY(rtlpriv->rtlhal.version))) {
		regvaldw = rtl_read_dword(rtlpriv, REG_LEDCFG0);
		rtl_write_dword(rtlpriv, REG_LEDCFG0, regvaldw | BIT(23));
	}

	return _rtl92d_phy_bb_config(hw);
}

bool rtl92d_phy_rf_config(struct ieee80211_hw *hw)
{
	return rtl92d_phy_rf6052_config(hw);
}

bool rtl92d_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
					  enum rf_content content,
					  enum radio_path rfpath)
{
829
	int i;
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	u32 *radioa_array_table;
	u32 *radiob_array_table;
	u16 radioa_arraylen, radiob_arraylen;
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	radioa_arraylen = RADIOA_2T_ARRAYLENGTH;
	radioa_array_table = rtl8192de_radioa_2tarray;
	radiob_arraylen = RADIOB_2T_ARRAYLENGTH;
	radiob_array_table = rtl8192de_radiob_2tarray;
	if (rtlpriv->efuse.internal_pa_5g[0]) {
		radioa_arraylen = RADIOA_2T_INT_PA_ARRAYLENGTH;
		radioa_array_table = rtl8192de_radioa_2t_int_paarray;
	}
	if (rtlpriv->efuse.internal_pa_5g[1]) {
		radiob_arraylen = RADIOB_2T_INT_PA_ARRAYLENGTH;
		radiob_array_table = rtl8192de_radiob_2t_int_paarray;
	}
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
848
		 "PHY_ConfigRFWithHeaderFile() Radio_A:Rtl819XRadioA_1TArray\n");
849
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
850 851
		 "PHY_ConfigRFWithHeaderFile() Radio_B:Rtl819XRadioB_1TArray\n");
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "Radio No %x\n", rfpath);
852 853 854 855 856 857

	/* this only happens when DMDP, mac0 start on 2.4G,
	 * mac1 start on 5G, mac 0 has to set phy0&phy1
	 * pathA or mac1 has to set phy0&phy1 pathA */
	if ((content == radiob_txt) && (rfpath == RF90_PATH_A)) {
		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
858
			 " ===> althougth Path A, we load radiob.txt\n");
859 860 861 862 863 864 865 866 867 868
		radioa_arraylen = radiob_arraylen;
		radioa_array_table = radiob_array_table;
	}
	switch (rfpath) {
	case RF90_PATH_A:
		for (i = 0; i < radioa_arraylen; i = i + 2) {
			if (radioa_array_table[i] == 0xfe) {
				mdelay(50);
			} else if (radioa_array_table[i] == 0xfd) {
				/* delay_ms(5); */
869
				mdelay(5);
870 871
			} else if (radioa_array_table[i] == 0xfc) {
				/* delay_ms(1); */
872
				mdelay(1);
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
			} else if (radioa_array_table[i] == 0xfb) {
				udelay(50);
			} else if (radioa_array_table[i] == 0xfa) {
				udelay(5);
			} else if (radioa_array_table[i] == 0xf9) {
				udelay(1);
			} else {
				rtl_set_rfreg(hw, rfpath, radioa_array_table[i],
					      BRFREGOFFSETMASK,
					      radioa_array_table[i + 1]);
				/*  Add 1us delay between BB/RF register set. */
				udelay(1);
			}
		}
		break;
	case RF90_PATH_B:
		for (i = 0; i < radiob_arraylen; i = i + 2) {
			if (radiob_array_table[i] == 0xfe) {
				/* Delay specific ms. Only RF configuration
				 * requires delay. */
				mdelay(50);
			} else if (radiob_array_table[i] == 0xfd) {
				/* delay_ms(5); */
896
				mdelay(5);
897 898
			} else if (radiob_array_table[i] == 0xfc) {
				/* delay_ms(1); */
899
				mdelay(1);
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
			} else if (radiob_array_table[i] == 0xfb) {
				udelay(50);
			} else if (radiob_array_table[i] == 0xfa) {
				udelay(5);
			} else if (radiob_array_table[i] == 0xf9) {
				udelay(1);
			} else {
				rtl_set_rfreg(hw, rfpath, radiob_array_table[i],
					      BRFREGOFFSETMASK,
					      radiob_array_table[i + 1]);
				/*  Add 1us delay between BB/RF register set. */
				udelay(1);
			}
		}
		break;
	case RF90_PATH_C:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
917
			 "switch case not processed\n");
918 919 920
		break;
	case RF90_PATH_D:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
921
			 "switch case not processed\n");
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
		break;
	}
	return true;
}

void rtl92d_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);

	rtlphy->default_initialgain[0] =
	    (u8) rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, BMASKBYTE0);
	rtlphy->default_initialgain[1] =
	    (u8) rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, BMASKBYTE0);
	rtlphy->default_initialgain[2] =
	    (u8) rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, BMASKBYTE0);
	rtlphy->default_initialgain[3] =
	    (u8) rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, BMASKBYTE0);
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
941 942 943 944 945
		 "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
		 rtlphy->default_initialgain[0],
		 rtlphy->default_initialgain[1],
		 rtlphy->default_initialgain[2],
		 rtlphy->default_initialgain[3]);
946 947 948 949 950
	rtlphy->framesync = (u8)rtl_get_bbreg(hw, ROFDM0_RXDETECTOR3,
					      BMASKBYTE0);
	rtlphy->framesync_c34 = rtl_get_bbreg(hw, ROFDM0_RXDETECTOR2,
					      BMASKDWORD);
	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
951 952
		 "Default framesync (0x%x) = 0x%x\n",
		 ROFDM0_RXDETECTOR3, rtlphy->framesync);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 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 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
}

static void _rtl92d_get_txpower_index(struct ieee80211_hw *hw, u8 channel,
	u8 *cckpowerlevel, u8 *ofdmpowerlevel)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	u8 index = (channel - 1);

	/* 1. CCK */
	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
		/* RF-A */
		cckpowerlevel[RF90_PATH_A] =
				 rtlefuse->txpwrlevel_cck[RF90_PATH_A][index];
		/* RF-B */
		cckpowerlevel[RF90_PATH_B] =
				 rtlefuse->txpwrlevel_cck[RF90_PATH_B][index];
	} else {
		cckpowerlevel[RF90_PATH_A] = 0;
		cckpowerlevel[RF90_PATH_B] = 0;
	}
	/* 2. OFDM for 1S or 2S */
	if (rtlphy->rf_type == RF_1T2R || rtlphy->rf_type == RF_1T1R) {
		/*  Read HT 40 OFDM TX power */
		ofdmpowerlevel[RF90_PATH_A] =
		    rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_A][index];
		ofdmpowerlevel[RF90_PATH_B] =
		    rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_B][index];
	} else if (rtlphy->rf_type == RF_2T2R) {
		/* Read HT 40 OFDM TX power */
		ofdmpowerlevel[RF90_PATH_A] =
		    rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_A][index];
		ofdmpowerlevel[RF90_PATH_B] =
		    rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_B][index];
	}
}

static void _rtl92d_ccxpower_index_check(struct ieee80211_hw *hw,
	u8 channel, u8 *cckpowerlevel, u8 *ofdmpowerlevel)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);

	rtlphy->cur_cck_txpwridx = cckpowerlevel[0];
	rtlphy->cur_ofdm24g_txpwridx = ofdmpowerlevel[0];
}

static u8 _rtl92c_phy_get_rightchnlplace(u8 chnl)
{
	u8 channel_5g[59] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58,
		60, 62, 64, 100, 102, 104, 106, 108, 110, 112,
		114, 116, 118, 120, 122, 124, 126, 128,
		130, 132, 134, 136, 138, 140, 149, 151,
		153, 155, 157, 159, 161, 163, 165
	};
	u8 place = chnl;

	if (chnl > 14) {
		for (place = 14; place < sizeof(channel_5g); place++) {
			if (channel_5g[place] == chnl) {
				place++;
				break;
			}
		}
	}
	return place;
}

void rtl92d_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
{
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 cckpowerlevel[2], ofdmpowerlevel[2];

1031
	if (!rtlefuse->txpwr_fromeprom)
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 1062 1063 1064 1065
		return;
	channel = _rtl92c_phy_get_rightchnlplace(channel);
	_rtl92d_get_txpower_index(hw, channel, &cckpowerlevel[0],
		&ofdmpowerlevel[0]);
	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
		_rtl92d_ccxpower_index_check(hw, channel, &cckpowerlevel[0],
				&ofdmpowerlevel[0]);
	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
		rtl92d_phy_rf6052_set_cck_txpower(hw, &cckpowerlevel[0]);
	rtl92d_phy_rf6052_set_ofdm_txpower(hw, &ofdmpowerlevel[0], channel);
}

void rtl92d_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	enum io_type iotype;

	if (!is_hal_stop(rtlhal)) {
		switch (operation) {
		case SCAN_OPT_BACKUP:
			rtlhal->current_bandtypebackup =
						 rtlhal->current_bandtype;
			iotype = IO_CMD_PAUSE_DM_BY_SCAN;
			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
						      (u8 *)&iotype);
			break;
		case SCAN_OPT_RESTORE:
			iotype = IO_CMD_RESUME_DM_BY_SCAN;
			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
						      (u8 *)&iotype);
			break;
		default:
			RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1066
				 "Unknown Scan Backup operation\n");
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
			break;
		}
	}
}

void rtl92d_phy_set_bw_mode(struct ieee80211_hw *hw,
			    enum nl80211_channel_type ch_type)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	unsigned long flag = 0;
	u8 reg_prsr_rsc;
	u8 reg_bw_opmode;

	if (rtlphy->set_bwmode_inprogress)
		return;
	if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1087
			 "FALSE driver sleep or unload\n");
1088 1089 1090
		return;
	}
	rtlphy->set_bwmode_inprogress = true;
1091 1092 1093
	RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "Switch to %s bandwidth\n",
		 rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
		 "20MHz" : "40MHz");
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
	reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
	reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
	switch (rtlphy->current_chan_bw) {
	case HT_CHANNEL_WIDTH_20:
		reg_bw_opmode |= BW_OPMODE_20MHZ;
		rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
		break;
	case HT_CHANNEL_WIDTH_20_40:
		reg_bw_opmode &= ~BW_OPMODE_20MHZ;
		rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);

		reg_prsr_rsc = (reg_prsr_rsc & 0x90) |
			(mac->cur_40_prime_sc << 5);
		rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
		break;
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1111
			 "unknown bandwidth: %#X\n", rtlphy->current_chan_bw);
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
		break;
	}
	switch (rtlphy->current_chan_bw) {
	case HT_CHANNEL_WIDTH_20:
		rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
		rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
		/* SET BIT10 BIT11  for receive cck */
		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10) |
			      BIT(11), 3);
		break;
	case HT_CHANNEL_WIDTH_20_40:
		rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
		rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
		/* Set Control channel to upper or lower.
		 * These settings are required only for 40MHz */
		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
			rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
			rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCKSIDEBAND,
				(mac->cur_40_prime_sc >> 1));
			rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
		}
		rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
		/* SET BIT10 BIT11  for receive cck */
		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10) |
			      BIT(11), 0);
		rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
			(mac->cur_40_prime_sc ==
			HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
		break;
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1143
			 "unknown bandwidth: %#X\n", rtlphy->current_chan_bw);
1144 1145 1146 1147 1148
		break;

	}
	rtl92d_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
	rtlphy->set_bwmode_inprogress = false;
1149
	RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "<==\n");
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
}

static void _rtl92d_phy_stop_trx_before_changeband(struct ieee80211_hw *hw)
{
	rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0);
	rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0);
	rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, BMASKBYTE0, 0x00);
	rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x0);
}

static void rtl92d_phy_switch_wirelessband(struct ieee80211_hw *hw, u8 band)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1164
	u8 value8;
1165

1166
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "==>\n");
1167 1168 1169 1170 1171 1172 1173 1174 1175
	rtlhal->bandset = band;
	rtlhal->current_bandtype = band;
	if (IS_92D_SINGLEPHY(rtlhal->version))
		rtlhal->bandset = BAND_ON_BOTH;
	/* stop RX/Tx */
	_rtl92d_phy_stop_trx_before_changeband(hw);
	/* reconfig BB/RF according to wireless mode */
	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
		/* BB & RF Config */
1176
		RT_TRACE(rtlpriv, COMP_CMD, DBG_DMESG, "====>2.4G\n");
1177 1178 1179 1180 1181
		if (rtlhal->interfaceindex == 1)
			_rtl92d_phy_config_bb_with_headerfile(hw,
				BASEBAND_CONFIG_AGC_TAB);
	} else {
		/* 5G band */
1182
		RT_TRACE(rtlpriv, COMP_CMD, DBG_DMESG, "====>5G\n");
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
		if (rtlhal->interfaceindex == 1)
			_rtl92d_phy_config_bb_with_headerfile(hw,
				BASEBAND_CONFIG_AGC_TAB);
	}
	rtl92d_update_bbrf_configuration(hw);
	if (rtlhal->current_bandtype == BAND_ON_2_4G)
		rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0x1);
	rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0x1);

	/* 20M BW. */
	/* rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1); */
	rtlhal->reloadtxpowerindex = true;
	/* notice fw know band status  0x81[1]/0x53[1] = 0: 5G, 1: 2G */
	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
		value8 = rtl_read_byte(rtlpriv,	(rtlhal->interfaceindex ==
			0 ? REG_MAC0 : REG_MAC1));
		value8 |= BIT(1);
		rtl_write_byte(rtlpriv, (rtlhal->interfaceindex ==
			0 ? REG_MAC0 : REG_MAC1), value8);
	} else {
		value8 = rtl_read_byte(rtlpriv, (rtlhal->interfaceindex ==
			0 ? REG_MAC0 : REG_MAC1));
		value8 &= (~BIT(1));
		rtl_write_byte(rtlpriv, (rtlhal->interfaceindex ==
			0 ? REG_MAC0 : REG_MAC1), value8);
	}
1209
	mdelay(1);
1210
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "<==Switch Band OK\n");
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
}

static void _rtl92d_phy_reload_imr_setting(struct ieee80211_hw *hw,
	u8 channel, u8 rfpath)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 imr_num = MAX_RF_IMR_INDEX;
	u32 rfmask = BRFREGOFFSETMASK;
	u8 group, i;
	unsigned long flag = 0;

1222
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>path %d\n", rfpath);
1223
	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G) {
1224
		RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>5G\n");
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
		rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(25) | BIT(24), 0);
		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0x00f00000, 0xf);
		/* fc area 0xd2c */
		if (channel > 99)
			rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(13) |
				      BIT(14), 2);
		else
			rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(13) |
				      BIT(14), 1);
		/* leave 0 for channel1-14. */
		group = channel <= 64 ? 1 : 2;
		imr_num = MAX_RF_IMR_INDEX_NORMAL;
		for (i = 0; i < imr_num; i++)
			rtl_set_rfreg(hw, (enum radio_path)rfpath,
				      rf_reg_for_5g_swchnl_normal[i], rfmask,
				      rf_imr_param_normal[0][group][i]);
		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0x00f00000, 0);
		rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 1);
	} else {
		/* G band. */
		RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1246 1247 1248
			 "Load RF IMR parameters for G band. IMR already setting %d\n",
			 rtlpriv->rtlhal.load_imrandiqk_setting_for2g);
		RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>2.4G\n");
1249 1250
		if (!rtlpriv->rtlhal.load_imrandiqk_setting_for2g) {
			RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1251 1252
				 "Load RF IMR parameters for G band. %d\n",
				 rfpath);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
			rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
			rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(25) | BIT(24), 0);
			rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4,
				      0x00f00000, 0xf);
			imr_num = MAX_RF_IMR_INDEX_NORMAL;
			for (i = 0; i < imr_num; i++) {
				rtl_set_rfreg(hw, (enum radio_path)rfpath,
					      rf_reg_for_5g_swchnl_normal[i],
					      BRFREGOFFSETMASK,
					      rf_imr_param_normal[0][0][i]);
			}
			rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4,
				      0x00f00000, 0);
			rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN | BCCKEN, 3);
			rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
		}
	}
1270
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
1271 1272 1273 1274 1275 1276 1277 1278 1279
}

static void _rtl92d_phy_enable_rf_env(struct ieee80211_hw *hw,
	u8 rfpath, u32 *pu4_regval)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];

1280
	RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "====>\n");
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	/*----Store original RFENV control type----*/
	switch (rfpath) {
	case RF90_PATH_A:
	case RF90_PATH_C:
		*pu4_regval = rtl_get_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV);
		break;
	case RF90_PATH_B:
	case RF90_PATH_D:
		*pu4_regval =
		    rtl_get_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16);
		break;
	}
	/*----Set RF_ENV enable----*/
	rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
	udelay(1);
	/*----Set RF_ENV output high----*/
	rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
	udelay(1);
	/* Set bit number of Address and Data for RF register */
	/* Set 1 to 4 bits for 8255 */
	rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREADDRESSLENGTH, 0x0);
	udelay(1);
	/*Set 0 to 12 bits for 8255 */
	rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
	udelay(1);
1306
	RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "<====\n");
1307 1308 1309 1310 1311 1312 1313 1314 1315
}

static void _rtl92d_phy_restore_rf_env(struct ieee80211_hw *hw, u8 rfpath,
				       u32 *pu4_regval)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];

1316
	RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "=====>\n");
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	/*----Restore RFENV control type----*/ ;
	switch (rfpath) {
	case RF90_PATH_A:
	case RF90_PATH_C:
		rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV, *pu4_regval);
		break;
	case RF90_PATH_B:
	case RF90_PATH_D:
		rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16,
			      *pu4_regval);
		break;
	}
1329
	RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "<=====\n");
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
}

static void _rtl92d_phy_switch_rf_setting(struct ieee80211_hw *hw, u8 channel)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u8 path = rtlhal->current_bandtype ==
	    BAND_ON_5G ? RF90_PATH_A : RF90_PATH_B;
	u8 index = 0, i = 0, rfpath = RF90_PATH_A;
	bool need_pwr_down = false, internal_pa = false;
	u32 u4regvalue, mask = 0x1C000, value = 0, u4tmp, u4tmp2;

1343
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>\n");
1344 1345
	/* config path A for 5G */
	if (rtlhal->current_bandtype == BAND_ON_5G) {
1346
		RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>5G\n");
1347
		u4tmp = curveindex_5g[channel - 1];
1348 1349
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
			"ver 1 set RF-A, 5G, 0x28 = 0x%x !!\n", u4tmp);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 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
		for (i = 0; i < RF_CHNL_NUM_5G; i++) {
			if (channel == rf_chnl_5g[i] && channel <= 140)
				index = 0;
		}
		for (i = 0; i < RF_CHNL_NUM_5G_40M; i++) {
			if (channel == rf_chnl_5g_40m[i] && channel <= 140)
				index = 1;
		}
		if (channel == 149 || channel == 155 || channel == 161)
			index = 2;
		else if (channel == 151 || channel == 153 || channel == 163
			 || channel == 165)
			index = 3;
		else if (channel == 157 || channel == 159)
			index = 4;

		if (rtlhal->macphymode == DUALMAC_DUALPHY
		    && rtlhal->interfaceindex == 1) {
			need_pwr_down = rtl92d_phy_enable_anotherphy(hw, false);
			rtlhal->during_mac1init_radioa = true;
			/* asume no this case */
			if (need_pwr_down)
				_rtl92d_phy_enable_rf_env(hw, path,
							  &u4regvalue);
		}
		for (i = 0; i < RF_REG_NUM_FOR_C_CUT_5G; i++) {
			if (i == 0 && (rtlhal->macphymode == DUALMAC_DUALPHY)) {
				rtl_set_rfreg(hw, (enum radio_path)path,
					      rf_reg_for_c_cut_5g[i],
					      BRFREGOFFSETMASK, 0xE439D);
			} else if (rf_reg_for_c_cut_5g[i] == RF_SYN_G4) {
				u4tmp2 = (rf_reg_pram_c_5g[index][i] &
				     0x7FF) | (u4tmp << 11);
				if (channel == 36)
					u4tmp2 &= ~(BIT(7) | BIT(6));
				rtl_set_rfreg(hw, (enum radio_path)path,
					      rf_reg_for_c_cut_5g[i],
					      BRFREGOFFSETMASK, u4tmp2);
			} else {
				rtl_set_rfreg(hw, (enum radio_path)path,
					      rf_reg_for_c_cut_5g[i],
					      BRFREGOFFSETMASK,
					      rf_reg_pram_c_5g[index][i]);
			}
			RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
1395 1396 1397 1398 1399 1400 1401
				 "offset 0x%x value 0x%x path %d index %d readback 0x%x\n",
				 rf_reg_for_c_cut_5g[i],
				 rf_reg_pram_c_5g[index][i],
				 path, index,
				 rtl_get_rfreg(hw, (enum radio_path)path,
					       rf_reg_for_c_cut_5g[i],
					       BRFREGOFFSETMASK));
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
		}
		if (need_pwr_down)
			_rtl92d_phy_restore_rf_env(hw, path, &u4regvalue);
		if (rtlhal->during_mac1init_radioa)
			rtl92d_phy_powerdown_anotherphy(hw, false);
		if (channel < 149)
			value = 0x07;
		else if (channel >= 149)
			value = 0x02;
		if (channel >= 36 && channel <= 64)
			index = 0;
		else if (channel >= 100 && channel <= 140)
			index = 1;
		else
			index = 2;
		for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
			rfpath++) {
			if (rtlhal->macphymode == DUALMAC_DUALPHY &&
				rtlhal->interfaceindex == 1)	/* MAC 1 5G */
				internal_pa = rtlpriv->efuse.internal_pa_5g[1];
			else
				internal_pa =
					 rtlpriv->efuse.internal_pa_5g[rfpath];
			if (internal_pa) {
				for (i = 0;
				     i < RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA;
				     i++) {
					rtl_set_rfreg(hw, rfpath,
						rf_for_c_cut_5g_internal_pa[i],
						BRFREGOFFSETMASK,
						rf_pram_c_5g_int_pa[index][i]);
					RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD,
1434
						 "offset 0x%x value 0x%x path %d index %d\n",
1435 1436
						 rf_for_c_cut_5g_internal_pa[i],
						 rf_pram_c_5g_int_pa[index][i],
1437
						 rfpath, index);
1438 1439 1440 1441 1442 1443 1444
				}
			} else {
				rtl_set_rfreg(hw, (enum radio_path)rfpath, 0x0B,
					      mask, value);
			}
		}
	} else if (rtlhal->current_bandtype == BAND_ON_2_4G) {
1445
		RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "====>2.4G\n");
1446
		u4tmp = curveindex_2g[channel - 1];
1447 1448
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
			"ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n", u4tmp);
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
		if (channel == 1 || channel == 2 || channel == 4 || channel == 9
		    || channel == 10 || channel == 11 || channel == 12)
			index = 0;
		else if (channel == 3 || channel == 13 || channel == 14)
			index = 1;
		else if (channel >= 5 && channel <= 8)
			index = 2;
		if (rtlhal->macphymode == DUALMAC_DUALPHY) {
			path = RF90_PATH_A;
			if (rtlhal->interfaceindex == 0) {
				need_pwr_down =
					 rtl92d_phy_enable_anotherphy(hw, true);
				rtlhal->during_mac0init_radiob = true;

				if (need_pwr_down)
					_rtl92d_phy_enable_rf_env(hw, path,
								  &u4regvalue);
			}
		}
		for (i = 0; i < RF_REG_NUM_FOR_C_CUT_2G; i++) {
			if (rf_reg_for_c_cut_2g[i] == RF_SYN_G7)
				rtl_set_rfreg(hw, (enum radio_path)path,
					rf_reg_for_c_cut_2g[i],
					BRFREGOFFSETMASK,
					(rf_reg_param_for_c_cut_2g[index][i] |
					BIT(17)));
			else
				rtl_set_rfreg(hw, (enum radio_path)path,
					      rf_reg_for_c_cut_2g[i],
					      BRFREGOFFSETMASK,
					      rf_reg_param_for_c_cut_2g
					      [index][i]);
			RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
1482 1483 1484 1485 1486 1487 1488
				 "offset 0x%x value 0x%x mak 0x%x path %d index %d readback 0x%x\n",
				 rf_reg_for_c_cut_2g[i],
				 rf_reg_param_for_c_cut_2g[index][i],
				 rf_reg_mask_for_c_cut_2g[i], path, index,
				 rtl_get_rfreg(hw, (enum radio_path)path,
					       rf_reg_for_c_cut_2g[i],
					       BRFREGOFFSETMASK));
1489 1490
		}
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
1491 1492
			"cosa ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n",
			rf_syn_g4_for_c_cut_2g | (u4tmp << 11));
1493 1494 1495 1496 1497 1498 1499 1500 1501

		rtl_set_rfreg(hw, (enum radio_path)path, RF_SYN_G4,
			      BRFREGOFFSETMASK,
			      rf_syn_g4_for_c_cut_2g | (u4tmp << 11));
		if (need_pwr_down)
			_rtl92d_phy_restore_rf_env(hw, path, &u4regvalue);
		if (rtlhal->during_mac0init_radiob)
			rtl92d_phy_powerdown_anotherphy(hw, true);
	}
1502
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
}

u8 rtl92d_get_rightchnlplace_for_iqk(u8 chnl)
{
	u8 channel_all[59] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58,
		60, 62, 64, 100, 102, 104, 106, 108, 110, 112,
		114, 116, 118, 120, 122, 124, 126, 128,	130,
		132, 134, 136, 138, 140, 149, 151, 153, 155,
		157, 159, 161, 163, 165
	};
	u8 place = chnl;

	if (chnl > 14) {
		for (place = 14; place < sizeof(channel_all); place++) {
			if (channel_all[place] == chnl)
				return place - 13;
		}
	}

	return 0;
}

#define MAX_TOLERANCE		5
#define IQK_DELAY_TIME		1	/* ms */
#define MAX_TOLERANCE_92D	3

/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
static u8 _rtl92d_phy_patha_iqk(struct ieee80211_hw *hw, bool configpathb)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	u32 regeac, rege94, rege9c, regea4;
	u8 result = 0;

1539
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK!\n");
1540
	/* path-A IQK setting */
1541
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A IQK setting!\n");
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	if (rtlhal->interfaceindex == 0) {
		rtl_set_bbreg(hw, 0xe30, BMASKDWORD, 0x10008c1f);
		rtl_set_bbreg(hw, 0xe34, BMASKDWORD, 0x10008c1f);
	} else {
		rtl_set_bbreg(hw, 0xe30, BMASKDWORD, 0x10008c22);
		rtl_set_bbreg(hw, 0xe34, BMASKDWORD, 0x10008c22);
	}
	rtl_set_bbreg(hw, 0xe38, BMASKDWORD, 0x82140102);
	rtl_set_bbreg(hw, 0xe3c, BMASKDWORD, 0x28160206);
	/* path-B IQK setting */
	if (configpathb) {
		rtl_set_bbreg(hw, 0xe50, BMASKDWORD, 0x10008c22);
		rtl_set_bbreg(hw, 0xe54, BMASKDWORD, 0x10008c22);
		rtl_set_bbreg(hw, 0xe58, BMASKDWORD, 0x82140102);
		rtl_set_bbreg(hw, 0xe5c, BMASKDWORD, 0x28160206);
	}
	/* LO calibration setting */
1559
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LO calibration setting!\n");
1560 1561
	rtl_set_bbreg(hw, 0xe4c, BMASKDWORD, 0x00462911);
	/* One shot, path A LOK & IQK */
1562
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "One shot, path A LOK & IQK!\n");
1563 1564 1565 1566
	rtl_set_bbreg(hw, 0xe48, BMASKDWORD, 0xf9000000);
	rtl_set_bbreg(hw, 0xe48, BMASKDWORD, 0xf8000000);
	/* delay x ms */
	RTPRINT(rtlpriv, FINIT, INIT_IQK,
1567 1568
		"Delay %d ms for One shot, path A LOK & IQK\n",
		IQK_DELAY_TIME);
1569
	mdelay(IQK_DELAY_TIME);
1570 1571
	/* Check failed */
	regeac = rtl_get_bbreg(hw, 0xeac, BMASKDWORD);
1572
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
1573
	rege94 = rtl_get_bbreg(hw, 0xe94, BMASKDWORD);
1574
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe94 = 0x%x\n", rege94);
1575
	rege9c = rtl_get_bbreg(hw, 0xe9c, BMASKDWORD);
1576
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe9c = 0x%x\n", rege9c);
1577
	regea4 = rtl_get_bbreg(hw, 0xea4, BMASKDWORD);
1578
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xea4 = 0x%x\n", regea4);
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	if (!(regeac & BIT(28)) && (((rege94 & 0x03FF0000) >> 16) != 0x142) &&
	    (((rege9c & 0x03FF0000) >> 16) != 0x42))
		result |= 0x01;
	else			/* if Tx not OK, ignore Rx */
		return result;
	/* if Tx is OK, check whether Rx is OK */
	if (!(regeac & BIT(27)) && (((regea4 & 0x03FF0000) >> 16) != 0x132) &&
	    (((regeac & 0x03FF0000) >> 16) != 0x36))
		result |= 0x02;
	else
1589
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A Rx IQK fail!!\n");
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
	return result;
}

/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
static u8 _rtl92d_phy_patha_iqk_5g_normal(struct ieee80211_hw *hw,
					  bool configpathb)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	u32 regeac, rege94, rege9c, regea4;
	u8 result = 0;
	u8 i;
	u8 retrycount = 2;
	u32 TxOKBit = BIT(28), RxOKBit = BIT(27);

	if (rtlhal->interfaceindex == 1) {	/* PHY1 */
		TxOKBit = BIT(31);
		RxOKBit = BIT(30);
	}
1610
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK!\n");
1611
	/* path-A IQK setting */
1612
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A IQK setting!\n");
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
	rtl_set_bbreg(hw, 0xe30, BMASKDWORD, 0x18008c1f);
	rtl_set_bbreg(hw, 0xe34, BMASKDWORD, 0x18008c1f);
	rtl_set_bbreg(hw, 0xe38, BMASKDWORD, 0x82140307);
	rtl_set_bbreg(hw, 0xe3c, BMASKDWORD, 0x68160960);
	/* path-B IQK setting */
	if (configpathb) {
		rtl_set_bbreg(hw, 0xe50, BMASKDWORD, 0x18008c2f);
		rtl_set_bbreg(hw, 0xe54, BMASKDWORD, 0x18008c2f);
		rtl_set_bbreg(hw, 0xe58, BMASKDWORD, 0x82110000);
		rtl_set_bbreg(hw, 0xe5c, BMASKDWORD, 0x68110000);
	}
	/* LO calibration setting */
1625
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LO calibration setting!\n");
1626 1627 1628 1629 1630 1631 1632
	rtl_set_bbreg(hw, 0xe4c, BMASKDWORD, 0x00462911);
	/* path-A PA on */
	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, BMASKDWORD, 0x07000f60);
	rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BMASKDWORD, 0x66e60e30);
	for (i = 0; i < retrycount; i++) {
		/* One shot, path A LOK & IQK */
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
1633
			"One shot, path A LOK & IQK!\n");
1634 1635 1636 1637
		rtl_set_bbreg(hw, 0xe48, BMASKDWORD, 0xf9000000);
		rtl_set_bbreg(hw, 0xe48, BMASKDWORD, 0xf8000000);
		/* delay x ms */
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
1638 1639
			"Delay %d ms for One shot, path A LOK & IQK.\n",
			IQK_DELAY_TIME);
1640
		mdelay(IQK_DELAY_TIME * 10);
1641 1642
		/* Check failed */
		regeac = rtl_get_bbreg(hw, 0xeac, BMASKDWORD);
1643
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
1644
		rege94 = rtl_get_bbreg(hw, 0xe94, BMASKDWORD);
1645
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe94 = 0x%x\n", rege94);
1646
		rege9c = rtl_get_bbreg(hw, 0xe9c, BMASKDWORD);
1647
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe9c = 0x%x\n", rege9c);
1648
		regea4 = rtl_get_bbreg(hw, 0xea4, BMASKDWORD);
1649
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xea4 = 0x%x\n", regea4);
1650 1651 1652 1653 1654
		if (!(regeac & TxOKBit) &&
		     (((rege94 & 0x03FF0000) >> 16) != 0x142)) {
			result |= 0x01;
		} else { /* if Tx not OK, ignore Rx */
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
1655
				"Path A Tx IQK fail!!\n");
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
			continue;
		}

		/* if Tx is OK, check whether Rx is OK */
		if (!(regeac & RxOKBit) &&
		    (((regea4 & 0x03FF0000) >> 16) != 0x132)) {
			result |= 0x02;
			break;
		} else {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
1666
				"Path A Rx IQK fail!!\n");
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
		}
	}
	/* path A PA off */
	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, BMASKDWORD,
		      rtlphy->iqk_bb_backup[0]);
	rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BMASKDWORD,
		      rtlphy->iqk_bb_backup[1]);
	return result;
}

/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
static u8 _rtl92d_phy_pathb_iqk(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 regeac, regeb4, regebc, regec4, regecc;
	u8 result = 0;

1684
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path B IQK!\n");
1685
	/* One shot, path B LOK & IQK */
1686
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "One shot, path A LOK & IQK!\n");
1687 1688 1689 1690
	rtl_set_bbreg(hw, 0xe60, BMASKDWORD, 0x00000002);
	rtl_set_bbreg(hw, 0xe60, BMASKDWORD, 0x00000000);
	/* delay x ms  */
	RTPRINT(rtlpriv, FINIT, INIT_IQK,
1691
		"Delay %d ms for One shot, path B LOK & IQK\n", IQK_DELAY_TIME);
1692
	mdelay(IQK_DELAY_TIME);
1693 1694
	/* Check failed */
	regeac = rtl_get_bbreg(hw, 0xeac, BMASKDWORD);
1695
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
1696
	regeb4 = rtl_get_bbreg(hw, 0xeb4, BMASKDWORD);
1697
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeb4 = 0x%x\n", regeb4);
1698
	regebc = rtl_get_bbreg(hw, 0xebc, BMASKDWORD);
1699
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xebc = 0x%x\n", regebc);
1700
	regec4 = rtl_get_bbreg(hw, 0xec4, BMASKDWORD);
1701
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xec4 = 0x%x\n", regec4);
1702
	regecc = rtl_get_bbreg(hw, 0xecc, BMASKDWORD);
1703
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xecc = 0x%x\n", regecc);
1704 1705 1706 1707 1708 1709 1710 1711 1712
	if (!(regeac & BIT(31)) && (((regeb4 & 0x03FF0000) >> 16) != 0x142) &&
	    (((regebc & 0x03FF0000) >> 16) != 0x42))
		result |= 0x01;
	else
		return result;
	if (!(regeac & BIT(30)) && (((regec4 & 0x03FF0000) >> 16) != 0x132) &&
	    (((regecc & 0x03FF0000) >> 16) != 0x36))
		result |= 0x02;
	else
1713
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path B Rx IQK fail!!\n");
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
	return result;
}

/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
static u8 _rtl92d_phy_pathb_iqk_5g_normal(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	u32 regeac, regeb4, regebc, regec4, regecc;
	u8 result = 0;
	u8 i;
	u8 retrycount = 2;

1727
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path B IQK!\n");
1728
	/* path-A IQK setting */
1729
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A IQK setting!\n");
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
	rtl_set_bbreg(hw, 0xe30, BMASKDWORD, 0x18008c1f);
	rtl_set_bbreg(hw, 0xe34, BMASKDWORD, 0x18008c1f);
	rtl_set_bbreg(hw, 0xe38, BMASKDWORD, 0x82110000);
	rtl_set_bbreg(hw, 0xe3c, BMASKDWORD, 0x68110000);

	/* path-B IQK setting */
	rtl_set_bbreg(hw, 0xe50, BMASKDWORD, 0x18008c2f);
	rtl_set_bbreg(hw, 0xe54, BMASKDWORD, 0x18008c2f);
	rtl_set_bbreg(hw, 0xe58, BMASKDWORD, 0x82140307);
	rtl_set_bbreg(hw, 0xe5c, BMASKDWORD, 0x68160960);

	/* LO calibration setting */
1742
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LO calibration setting!\n");
1743 1744 1745 1746 1747 1748 1749 1750 1751
	rtl_set_bbreg(hw, 0xe4c, BMASKDWORD, 0x00462911);

	/* path-B PA on */
	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, BMASKDWORD, 0x0f600700);
	rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, BMASKDWORD, 0x061f0d30);

	for (i = 0; i < retrycount; i++) {
		/* One shot, path B LOK & IQK */
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
1752
			"One shot, path A LOK & IQK!\n");
1753 1754 1755 1756 1757
		rtl_set_bbreg(hw, 0xe48, BMASKDWORD, 0xfa000000);
		rtl_set_bbreg(hw, 0xe48, BMASKDWORD, 0xf8000000);

		/* delay x ms */
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
1758
			"Delay %d ms for One shot, path B LOK & IQK.\n", 10);
1759
		mdelay(IQK_DELAY_TIME * 10);
1760 1761 1762

		/* Check failed */
		regeac = rtl_get_bbreg(hw, 0xeac, BMASKDWORD);
1763
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
1764
		regeb4 = rtl_get_bbreg(hw, 0xeb4, BMASKDWORD);
1765
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeb4 = 0x%x\n", regeb4);
1766
		regebc = rtl_get_bbreg(hw, 0xebc, BMASKDWORD);
1767
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xebc = 0x%x\n", regebc);
1768
		regec4 = rtl_get_bbreg(hw, 0xec4, BMASKDWORD);
1769
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xec4 = 0x%x\n", regec4);
1770
		regecc = rtl_get_bbreg(hw, 0xecc, BMASKDWORD);
1771
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xecc = 0x%x\n", regecc);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
		if (!(regeac & BIT(31)) &&
		    (((regeb4 & 0x03FF0000) >> 16) != 0x142))
			result |= 0x01;
		else
			continue;
		if (!(regeac & BIT(30)) &&
		    (((regec4 & 0x03FF0000) >> 16) != 0x132)) {
			result |= 0x02;
			break;
		} else {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
1783
				"Path B Rx IQK fail!!\n");
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
		}
	}

	/* path B PA off */
	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, BMASKDWORD,
		      rtlphy->iqk_bb_backup[0]);
	rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, BMASKDWORD,
		      rtlphy->iqk_bb_backup[2]);
	return result;
}

static void _rtl92d_phy_save_adda_registers(struct ieee80211_hw *hw,
					    u32 *adda_reg, u32 *adda_backup,
					    u32 regnum)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 i;

1802
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Save ADDA parameters.\n");
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
	for (i = 0; i < regnum; i++)
		adda_backup[i] = rtl_get_bbreg(hw, adda_reg[i], BMASKDWORD);
}

static void _rtl92d_phy_save_mac_registers(struct ieee80211_hw *hw,
	u32 *macreg, u32 *macbackup)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 i;

1813
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Save MAC parameters.\n");
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
	for (i = 0; i < (IQK_MAC_REG_NUM - 1); i++)
		macbackup[i] = rtl_read_byte(rtlpriv, macreg[i]);
	macbackup[i] = rtl_read_dword(rtlpriv, macreg[i]);
}

static void _rtl92d_phy_reload_adda_registers(struct ieee80211_hw *hw,
					      u32 *adda_reg, u32 *adda_backup,
					      u32 regnum)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 i;

	RTPRINT(rtlpriv, FINIT, INIT_IQK,
1827
		"Reload ADDA power saving parameters !\n");
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
	for (i = 0; i < regnum; i++)
		rtl_set_bbreg(hw, adda_reg[i], BMASKDWORD, adda_backup[i]);
}

static void _rtl92d_phy_reload_mac_registers(struct ieee80211_hw *hw,
					     u32 *macreg, u32 *macbackup)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 i;

1838
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Reload MAC parameters !\n");
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
	for (i = 0; i < (IQK_MAC_REG_NUM - 1); i++)
		rtl_write_byte(rtlpriv, macreg[i], (u8) macbackup[i]);
	rtl_write_byte(rtlpriv, macreg[i], macbackup[i]);
}

static void _rtl92d_phy_path_adda_on(struct ieee80211_hw *hw,
		u32 *adda_reg, bool patha_on, bool is2t)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 pathon;
	u32 i;

1851
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "ADDA ON.\n");
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
	pathon = patha_on ? 0x04db25a4 : 0x0b1b25a4;
	if (patha_on)
		pathon = rtlpriv->rtlhal.interfaceindex == 0 ?
		    0x04db25a4 : 0x0b1b25a4;
	for (i = 0; i < IQK_ADDA_REG_NUM; i++)
		rtl_set_bbreg(hw, adda_reg[i], BMASKDWORD, pathon);
}

static void _rtl92d_phy_mac_setting_calibration(struct ieee80211_hw *hw,
						u32 *macreg, u32 *macbackup)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 i;

1866
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "MAC settings for Calibration.\n");
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
	rtl_write_byte(rtlpriv, macreg[0], 0x3F);

	for (i = 1; i < (IQK_MAC_REG_NUM - 1); i++)
		rtl_write_byte(rtlpriv, macreg[i], (u8)(macbackup[i] &
			       (~BIT(3))));
	rtl_write_byte(rtlpriv, macreg[i], (u8) (macbackup[i] & (~BIT(5))));
}

static void _rtl92d_phy_patha_standby(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
1878
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A standby mode!\n");
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

	rtl_set_bbreg(hw, 0xe28, BMASKDWORD, 0x0);
	rtl_set_bbreg(hw, RFPGA0_XA_LSSIPARAMETER, BMASKDWORD, 0x00010000);
	rtl_set_bbreg(hw, 0xe28, BMASKDWORD, 0x80800000);
}

static void _rtl92d_phy_pimode_switch(struct ieee80211_hw *hw, bool pi_mode)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 mode;

	RTPRINT(rtlpriv, FINIT, INIT_IQK,
1891
		"BB Switch to %s mode!\n", pi_mode ? "PI" : "SI");
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
	mode = pi_mode ? 0x01000100 : 0x01000000;
	rtl_set_bbreg(hw, 0x820, BMASKDWORD, mode);
	rtl_set_bbreg(hw, 0x828, BMASKDWORD, mode);
}

static void _rtl92d_phy_iq_calibrate(struct ieee80211_hw *hw, long result[][8],
				     u8 t, bool is2t)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	u32 i;
	u8 patha_ok, pathb_ok;
	static u32 adda_reg[IQK_ADDA_REG_NUM] = {
		RFPGA0_XCD_SWITCHCONTROL, 0xe6c, 0xe70, 0xe74,
		0xe78, 0xe7c, 0xe80, 0xe84,
		0xe88, 0xe8c, 0xed0, 0xed4,
		0xed8, 0xedc, 0xee0, 0xeec
	};
	static u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
		0x522, 0x550, 0x551, 0x040
	};
	static u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
		RFPGA0_XAB_RFINTERFACESW, RFPGA0_XA_RFINTERFACEOE,
		RFPGA0_XB_RFINTERFACEOE, ROFDM0_TRMUXPAR,
		RFPGA0_XCD_RFINTERFACESW, ROFDM0_TRXPATHENABLE,
		RFPGA0_RFMOD, RFPGA0_ANALOGPARAMETER4,
		ROFDM0_XAAGCCORE1, ROFDM0_XBAGCCORE1
	};
	const u32 retrycount = 2;
	u32 bbvalue;

1923
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK for 2.4G :Start!!!\n");
1924 1925
	if (t == 0) {
		bbvalue = rtl_get_bbreg(hw, RFPGA0_RFMOD, BMASKDWORD);
1926 1927 1928
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "==>0x%08x\n", bbvalue);
		RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQ Calibration for %s\n",
			is2t ? "2T2R" : "1T1R");
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965

		/*  Save ADDA parameters, turn Path A ADDA on */
		_rtl92d_phy_save_adda_registers(hw, adda_reg,
			rtlphy->adda_backup, IQK_ADDA_REG_NUM);
		_rtl92d_phy_save_mac_registers(hw, iqk_mac_reg,
			rtlphy->iqk_mac_backup);
		_rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
			rtlphy->iqk_bb_backup, IQK_BB_REG_NUM);
	}
	_rtl92d_phy_path_adda_on(hw, adda_reg, true, is2t);
	if (t == 0)
		rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
				RFPGA0_XA_HSSIPARAMETER1, BIT(8));

	/*  Switch BB to PI mode to do IQ Calibration. */
	if (!rtlphy->rfpi_enable)
		_rtl92d_phy_pimode_switch(hw, true);

	rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(24), 0x00);
	rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, BMASKDWORD, 0x03a05600);
	rtl_set_bbreg(hw, ROFDM0_TRMUXPAR, BMASKDWORD, 0x000800e4);
	rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW, BMASKDWORD, 0x22204000);
	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xf00000, 0x0f);
	if (is2t) {
		rtl_set_bbreg(hw, RFPGA0_XA_LSSIPARAMETER, BMASKDWORD,
			      0x00010000);
		rtl_set_bbreg(hw, RFPGA0_XB_LSSIPARAMETER, BMASKDWORD,
			      0x00010000);
	}
	/* MAC settings */
	_rtl92d_phy_mac_setting_calibration(hw, iqk_mac_reg,
					    rtlphy->iqk_mac_backup);
	/* Page B init */
	rtl_set_bbreg(hw, 0xb68, BMASKDWORD, 0x0f600000);
	if (is2t)
		rtl_set_bbreg(hw, 0xb6c, BMASKDWORD, 0x0f600000);
	/* IQ calibration setting */
1966
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK setting!\n");
1967 1968 1969 1970 1971 1972 1973
	rtl_set_bbreg(hw, 0xe28, BMASKDWORD, 0x80800000);
	rtl_set_bbreg(hw, 0xe40, BMASKDWORD, 0x01007c00);
	rtl_set_bbreg(hw, 0xe44, BMASKDWORD, 0x01004800);
	for (i = 0; i < retrycount; i++) {
		patha_ok = _rtl92d_phy_patha_iqk(hw, is2t);
		if (patha_ok == 0x03) {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
1974
				"Path A IQK Success!!\n");
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
			result[t][0] = (rtl_get_bbreg(hw, 0xe94, BMASKDWORD) &
					0x3FF0000) >> 16;
			result[t][1] = (rtl_get_bbreg(hw, 0xe9c, BMASKDWORD) &
					0x3FF0000) >> 16;
			result[t][2] = (rtl_get_bbreg(hw, 0xea4, BMASKDWORD) &
					0x3FF0000) >> 16;
			result[t][3] = (rtl_get_bbreg(hw, 0xeac, BMASKDWORD) &
					0x3FF0000) >> 16;
			break;
		} else if (i == (retrycount - 1) && patha_ok == 0x01) {
			/* Tx IQK OK */
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
1987
				"Path A IQK Only  Tx Success!!\n");
1988 1989 1990 1991 1992 1993 1994 1995

			result[t][0] = (rtl_get_bbreg(hw, 0xe94, BMASKDWORD) &
					0x3FF0000) >> 16;
			result[t][1] = (rtl_get_bbreg(hw, 0xe9c, BMASKDWORD) &
					0x3FF0000) >> 16;
		}
	}
	if (0x00 == patha_ok)
1996
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK failed!!\n");
1997 1998 1999 2000 2001 2002 2003 2004
	if (is2t) {
		_rtl92d_phy_patha_standby(hw);
		/* Turn Path B ADDA on */
		_rtl92d_phy_path_adda_on(hw, adda_reg, false, is2t);
		for (i = 0; i < retrycount; i++) {
			pathb_ok = _rtl92d_phy_pathb_iqk(hw);
			if (pathb_ok == 0x03) {
				RTPRINT(rtlpriv, FINIT, INIT_IQK,
2005
					"Path B IQK Success!!\n");
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
				result[t][4] = (rtl_get_bbreg(hw, 0xeb4,
					       BMASKDWORD) & 0x3FF0000) >> 16;
				result[t][5] = (rtl_get_bbreg(hw, 0xebc,
					       BMASKDWORD) & 0x3FF0000) >> 16;
				result[t][6] = (rtl_get_bbreg(hw, 0xec4,
					       BMASKDWORD) & 0x3FF0000) >> 16;
				result[t][7] = (rtl_get_bbreg(hw, 0xecc,
					       BMASKDWORD) & 0x3FF0000) >> 16;
				break;
			} else if (i == (retrycount - 1) && pathb_ok == 0x01) {
				/* Tx IQK OK */
				RTPRINT(rtlpriv, FINIT, INIT_IQK,
2018
					"Path B Only Tx IQK Success!!\n");
2019 2020 2021 2022 2023 2024 2025 2026
				result[t][4] = (rtl_get_bbreg(hw, 0xeb4,
					       BMASKDWORD) & 0x3FF0000) >> 16;
				result[t][5] = (rtl_get_bbreg(hw, 0xebc,
					       BMASKDWORD) & 0x3FF0000) >> 16;
			}
		}
		if (0x00 == pathb_ok)
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
2027
				"Path B IQK failed!!\n");
2028 2029 2030 2031
	}

	/* Back to BB mode, load original value */
	RTPRINT(rtlpriv, FINIT, INIT_IQK,
2032
		"IQK:Back to BB mode, load original value!\n");
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056

	rtl_set_bbreg(hw, 0xe28, BMASKDWORD, 0);
	if (t != 0) {
		/* Switch back BB to SI mode after finish IQ Calibration. */
		if (!rtlphy->rfpi_enable)
			_rtl92d_phy_pimode_switch(hw, false);
		/* Reload ADDA power saving parameters */
		_rtl92d_phy_reload_adda_registers(hw, adda_reg,
				rtlphy->adda_backup, IQK_ADDA_REG_NUM);
		/* Reload MAC parameters */
		_rtl92d_phy_reload_mac_registers(hw, iqk_mac_reg,
					rtlphy->iqk_mac_backup);
		if (is2t)
			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
							  rtlphy->iqk_bb_backup,
							  IQK_BB_REG_NUM);
		else
			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
							  rtlphy->iqk_bb_backup,
							  IQK_BB_REG_NUM - 1);
		/* load 0xe30 IQC default value */
		rtl_set_bbreg(hw, 0xe30, BMASKDWORD, 0x01008c00);
		rtl_set_bbreg(hw, 0xe34, BMASKDWORD, 0x01008c00);
	}
2057
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "<==\n");
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
}

static void _rtl92d_phy_iq_calibrate_5g_normal(struct ieee80211_hw *hw,
					       long result[][8], u8 t)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u8 patha_ok, pathb_ok;
	static u32 adda_reg[IQK_ADDA_REG_NUM] = {
		RFPGA0_XCD_SWITCHCONTROL, 0xe6c, 0xe70, 0xe74,
		0xe78, 0xe7c, 0xe80, 0xe84,
		0xe88, 0xe8c, 0xed0, 0xed4,
		0xed8, 0xedc, 0xee0, 0xeec
	};
	static u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
		0x522, 0x550, 0x551, 0x040
	};
	static u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
		RFPGA0_XAB_RFINTERFACESW, RFPGA0_XA_RFINTERFACEOE,
		RFPGA0_XB_RFINTERFACEOE, ROFDM0_TRMUXPAR,
		RFPGA0_XCD_RFINTERFACESW, ROFDM0_TRXPATHENABLE,
		RFPGA0_RFMOD, RFPGA0_ANALOGPARAMETER4,
		ROFDM0_XAAGCCORE1, ROFDM0_XBAGCCORE1
	};
	u32 bbvalue;
	bool is2t = IS_92D_SINGLEPHY(rtlhal->version);

	/* Note: IQ calibration must be performed after loading
	 * PHY_REG.txt , and radio_a, radio_b.txt */

2089
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK for 5G NORMAL:Start!!!\n");
2090
	mdelay(IQK_DELAY_TIME * 20);
2091 2092
	if (t == 0) {
		bbvalue = rtl_get_bbreg(hw, RFPGA0_RFMOD, BMASKDWORD);
2093 2094 2095
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "==>0x%08x\n", bbvalue);
		RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQ Calibration for %s\n",
			is2t ? "2T2R" : "1T1R");
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
		/* Save ADDA parameters, turn Path A ADDA on */
		_rtl92d_phy_save_adda_registers(hw, adda_reg,
						rtlphy->adda_backup,
						IQK_ADDA_REG_NUM);
		_rtl92d_phy_save_mac_registers(hw, iqk_mac_reg,
					       rtlphy->iqk_mac_backup);
		if (is2t)
			_rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
							rtlphy->iqk_bb_backup,
							IQK_BB_REG_NUM);
		else
			_rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
							rtlphy->iqk_bb_backup,
							IQK_BB_REG_NUM - 1);
	}
	_rtl92d_phy_path_adda_on(hw, adda_reg, true, is2t);
	/* MAC settings */
	_rtl92d_phy_mac_setting_calibration(hw, iqk_mac_reg,
			rtlphy->iqk_mac_backup);
	if (t == 0)
		rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
			RFPGA0_XA_HSSIPARAMETER1, BIT(8));
	/*  Switch BB to PI mode to do IQ Calibration. */
	if (!rtlphy->rfpi_enable)
		_rtl92d_phy_pimode_switch(hw, true);
	rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(24), 0x00);
	rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, BMASKDWORD, 0x03a05600);
	rtl_set_bbreg(hw, ROFDM0_TRMUXPAR, BMASKDWORD, 0x000800e4);
	rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW, BMASKDWORD, 0x22208000);
	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xf00000, 0x0f);

	/* Page B init */
	rtl_set_bbreg(hw, 0xb68, BMASKDWORD, 0x0f600000);
	if (is2t)
		rtl_set_bbreg(hw, 0xb6c, BMASKDWORD, 0x0f600000);
	/* IQ calibration setting  */
2132
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK setting!\n");
2133 2134 2135 2136 2137
	rtl_set_bbreg(hw, 0xe28, BMASKDWORD, 0x80800000);
	rtl_set_bbreg(hw, 0xe40, BMASKDWORD, 0x10007c00);
	rtl_set_bbreg(hw, 0xe44, BMASKDWORD, 0x01004800);
	patha_ok = _rtl92d_phy_patha_iqk_5g_normal(hw, is2t);
	if (patha_ok == 0x03) {
2138
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK Success!!\n");
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
		result[t][0] = (rtl_get_bbreg(hw, 0xe94, BMASKDWORD) &
				0x3FF0000) >> 16;
		result[t][1] = (rtl_get_bbreg(hw, 0xe9c, BMASKDWORD) &
				0x3FF0000) >> 16;
		result[t][2] = (rtl_get_bbreg(hw, 0xea4, BMASKDWORD) &
				0x3FF0000) >> 16;
		result[t][3] = (rtl_get_bbreg(hw, 0xeac, BMASKDWORD) &
				0x3FF0000) >> 16;
	} else if (patha_ok == 0x01) {	/* Tx IQK OK */
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2149
			"Path A IQK Only  Tx Success!!\n");
2150 2151 2152 2153 2154 2155

		result[t][0] = (rtl_get_bbreg(hw, 0xe94, BMASKDWORD) &
				0x3FF0000) >> 16;
		result[t][1] = (rtl_get_bbreg(hw, 0xe9c, BMASKDWORD) &
				0x3FF0000) >> 16;
	} else {
2156
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK Fail!!\n");
2157 2158 2159 2160 2161 2162 2163 2164
	}
	if (is2t) {
		/* _rtl92d_phy_patha_standby(hw); */
		/* Turn Path B ADDA on  */
		_rtl92d_phy_path_adda_on(hw, adda_reg, false, is2t);
		pathb_ok = _rtl92d_phy_pathb_iqk_5g_normal(hw);
		if (pathb_ok == 0x03) {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
2165
				"Path B IQK Success!!\n");
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
			result[t][4] = (rtl_get_bbreg(hw, 0xeb4, BMASKDWORD) &
			     0x3FF0000) >> 16;
			result[t][5] = (rtl_get_bbreg(hw, 0xebc, BMASKDWORD) &
			     0x3FF0000) >> 16;
			result[t][6] = (rtl_get_bbreg(hw, 0xec4, BMASKDWORD) &
			     0x3FF0000) >> 16;
			result[t][7] = (rtl_get_bbreg(hw, 0xecc, BMASKDWORD) &
			     0x3FF0000) >> 16;
		} else if (pathb_ok == 0x01) { /* Tx IQK OK */
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
2176
				"Path B Only Tx IQK Success!!\n");
2177 2178 2179 2180 2181 2182
			result[t][4] = (rtl_get_bbreg(hw, 0xeb4, BMASKDWORD) &
			     0x3FF0000) >> 16;
			result[t][5] = (rtl_get_bbreg(hw, 0xebc, BMASKDWORD) &
			     0x3FF0000) >> 16;
		} else {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
2183
				"Path B IQK failed!!\n");
2184 2185 2186 2187 2188
		}
	}

	/* Back to BB mode, load original value */
	RTPRINT(rtlpriv, FINIT, INIT_IQK,
2189
		"IQK:Back to BB mode, load original value!\n");
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
	rtl_set_bbreg(hw, 0xe28, BMASKDWORD, 0);
	if (t != 0) {
		if (is2t)
			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
							  rtlphy->iqk_bb_backup,
							  IQK_BB_REG_NUM);
		else
			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
							  rtlphy->iqk_bb_backup,
							  IQK_BB_REG_NUM - 1);
		/* Reload MAC parameters */
		_rtl92d_phy_reload_mac_registers(hw, iqk_mac_reg,
				rtlphy->iqk_mac_backup);
		/*  Switch back BB to SI mode after finish IQ Calibration. */
		if (!rtlphy->rfpi_enable)
			_rtl92d_phy_pimode_switch(hw, false);
		/* Reload ADDA power saving parameters */
		_rtl92d_phy_reload_adda_registers(hw, adda_reg,
						  rtlphy->adda_backup,
						  IQK_ADDA_REG_NUM);
	}
2211
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "<==\n");
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
}

static bool _rtl92d_phy_simularity_compare(struct ieee80211_hw *hw,
	long result[][8], u8 c1, u8 c2)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u32 i, j, diff, sim_bitmap, bound;
	u8 final_candidate[2] = {0xFF, 0xFF};	/* for path A and path B */
	bool bresult = true;
	bool is2t = IS_92D_SINGLEPHY(rtlhal->version);

	if (is2t)
		bound = 8;
	else
		bound = 4;
	sim_bitmap = 0;
	for (i = 0; i < bound; i++) {
		diff = (result[c1][i] > result[c2][i]) ? (result[c1][i] -
		       result[c2][i]) : (result[c2][i] - result[c1][i]);
		if (diff > MAX_TOLERANCE_92D) {
			if ((i == 2 || i == 6) && !sim_bitmap) {
				if (result[c1][i] + result[c1][i + 1] == 0)
					final_candidate[(i / 4)] = c2;
				else if (result[c2][i] + result[c2][i + 1] == 0)
					final_candidate[(i / 4)] = c1;
				else
					sim_bitmap = sim_bitmap | (1 << i);
			} else {
				sim_bitmap = sim_bitmap | (1 << i);
			}
		}
	}
	if (sim_bitmap == 0) {
		for (i = 0; i < (bound / 4); i++) {
			if (final_candidate[i] != 0xFF) {
				for (j = i * 4; j < (i + 1) * 4 - 2; j++)
					result[3][j] =
						 result[final_candidate[i]][j];
				bresult = false;
			}
		}
		return bresult;
	}
	if (!(sim_bitmap & 0x0F)) { /* path A OK */
		for (i = 0; i < 4; i++)
			result[3][i] = result[c1][i];
	} else if (!(sim_bitmap & 0x03)) { /* path A, Tx OK */
		for (i = 0; i < 2; i++)
			result[3][i] = result[c1][i];
	}
	if (!(sim_bitmap & 0xF0) && is2t) { /* path B OK */
		for (i = 4; i < 8; i++)
			result[3][i] = result[c1][i];
	} else if (!(sim_bitmap & 0x30)) { /* path B, Tx OK */
		for (i = 4; i < 6; i++)
			result[3][i] = result[c1][i];
	}
	return false;
}

static void _rtl92d_phy_patha_fill_iqk_matrix(struct ieee80211_hw *hw,
					      bool iqk_ok, long result[][8],
					      u8 final_candidate, bool txonly)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u32 oldval_0, val_x, tx0_a, reg;
	long val_y, tx0_c;
	bool is2t = IS_92D_SINGLEPHY(rtlhal->version) ||
	    rtlhal->macphymode == DUALMAC_DUALPHY;

	RTPRINT(rtlpriv, FINIT, INIT_IQK,
2285
		"Path A IQ Calibration %s !\n", iqk_ok ? "Success" : "Failed");
2286 2287 2288 2289 2290 2291 2292 2293 2294
	if (final_candidate == 0xFF) {
		return;
	} else if (iqk_ok) {
		oldval_0 = (rtl_get_bbreg(hw, ROFDM0_XATxIQIMBALANCE,
			BMASKDWORD) >> 22) & 0x3FF;	/* OFDM0_D */
		val_x = result[final_candidate][0];
		if ((val_x & 0x00000200) != 0)
			val_x = val_x | 0xFFFFFC00;
		tx0_a = (val_x * oldval_0) >> 8;
2295 2296 2297
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
			"X = 0x%x, tx0_a = 0x%x, oldval_0 0x%x\n",
			val_x, tx0_a, oldval_0);
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
		rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, 0x3FF, tx0_a);
		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(24),
			      ((val_x * oldval_0 >> 7) & 0x1));
		val_y = result[final_candidate][1];
		if ((val_y & 0x00000200) != 0)
			val_y = val_y | 0xFFFFFC00;
		/* path B IQK result + 3 */
		if (rtlhal->interfaceindex == 1 &&
			rtlhal->current_bandtype == BAND_ON_5G)
			val_y += 3;
		tx0_c = (val_y * oldval_0) >> 8;
2309 2310 2311
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
			"Y = 0x%lx, tx0_c = 0x%lx\n",
			val_y, tx0_c);
2312 2313 2314 2315 2316 2317 2318
		rtl_set_bbreg(hw, ROFDM0_XCTxAFE, 0xF0000000,
			      ((tx0_c & 0x3C0) >> 6));
		rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, 0x003F0000,
			      (tx0_c & 0x3F));
		if (is2t)
			rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(26),
				      ((val_y * oldval_0 >> 7) & 0x1));
2319 2320 2321
		RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xC80 = 0x%x\n",
			rtl_get_bbreg(hw, ROFDM0_XATxIQIMBALANCE,
				      BMASKDWORD));
2322
		if (txonly) {
2323
			RTPRINT(rtlpriv, FINIT, INIT_IQK,  "only Tx OK\n");
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
			return;
		}
		reg = result[final_candidate][2];
		rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, 0x3FF, reg);
		reg = result[final_candidate][3] & 0x3F;
		rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, 0xFC00, reg);
		reg = (result[final_candidate][3] >> 6) & 0xF;
		rtl_set_bbreg(hw, 0xca0, 0xF0000000, reg);
	}
}

static void _rtl92d_phy_pathb_fill_iqk_matrix(struct ieee80211_hw *hw,
	bool iqk_ok, long result[][8], u8 final_candidate, bool txonly)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u32 oldval_1, val_x, tx1_a, reg;
	long val_y, tx1_c;

2343 2344
	RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path B IQ Calibration %s !\n",
		iqk_ok ? "Success" : "Failed");
2345 2346 2347 2348 2349 2350 2351 2352 2353
	if (final_candidate == 0xFF) {
		return;
	} else if (iqk_ok) {
		oldval_1 = (rtl_get_bbreg(hw, ROFDM0_XBTxIQIMBALANCE,
					  BMASKDWORD) >> 22) & 0x3FF;
		val_x = result[final_candidate][4];
		if ((val_x & 0x00000200) != 0)
			val_x = val_x | 0xFFFFFC00;
		tx1_a = (val_x * oldval_1) >> 8;
2354 2355
		RTPRINT(rtlpriv, FINIT, INIT_IQK, "X = 0x%x, tx1_a = 0x%x\n",
			val_x, tx1_a);
2356 2357 2358 2359 2360 2361 2362 2363 2364
		rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, 0x3FF, tx1_a);
		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(28),
			      ((val_x * oldval_1 >> 7) & 0x1));
		val_y = result[final_candidate][5];
		if ((val_y & 0x00000200) != 0)
			val_y = val_y | 0xFFFFFC00;
		if (rtlhal->current_bandtype == BAND_ON_5G)
			val_y += 3;
		tx1_c = (val_y * oldval_1) >> 8;
2365 2366
		RTPRINT(rtlpriv, FINIT, INIT_IQK, "Y = 0x%lx, tx1_c = 0x%lx\n",
			val_y, tx1_c);
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
		rtl_set_bbreg(hw, ROFDM0_XDTxAFE, 0xF0000000,
			      ((tx1_c & 0x3C0) >> 6));
		rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, 0x003F0000,
			      (tx1_c & 0x3F));
		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(30),
			      ((val_y * oldval_1 >> 7) & 0x1));
		if (txonly)
			return;
		reg = result[final_candidate][6];
		rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0x3FF, reg);
		reg = result[final_candidate][7] & 0x3F;
		rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0xFC00, reg);
		reg = (result[final_candidate][7] >> 6) & 0xF;
		rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, 0x0000F000, reg);
	}
}

void rtl92d_phy_iq_calibrate(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	long result[4][8];
	u8 i, final_candidate, indexforchannel;
	bool patha_ok, pathb_ok;
	long rege94, rege9c, regea4, regeac, regeb4;
	long regebc, regec4, regecc, regtmp = 0;
	bool is12simular, is13simular, is23simular;
	unsigned long flag = 0;

	RTPRINT(rtlpriv, FINIT, INIT_IQK,
2398
		"IQK:Start!!!channel %d\n", rtlphy->current_channel);
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
	for (i = 0; i < 8; i++) {
		result[0][i] = 0;
		result[1][i] = 0;
		result[2][i] = 0;
		result[3][i] = 0;
	}
	final_candidate = 0xff;
	patha_ok = false;
	pathb_ok = false;
	is12simular = false;
	is23simular = false;
	is13simular = false;
	RTPRINT(rtlpriv, FINIT, INIT_IQK,
2412
		"IQK !!!currentband %d\n", rtlhal->current_bandtype);
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
	rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
	for (i = 0; i < 3; i++) {
		if (rtlhal->current_bandtype == BAND_ON_5G) {
			_rtl92d_phy_iq_calibrate_5g_normal(hw, result, i);
		} else if (rtlhal->current_bandtype == BAND_ON_2_4G) {
			if (IS_92D_SINGLEPHY(rtlhal->version))
				_rtl92d_phy_iq_calibrate(hw, result, i, true);
			else
				_rtl92d_phy_iq_calibrate(hw, result, i, false);
		}
		if (i == 1) {
			is12simular = _rtl92d_phy_simularity_compare(hw, result,
								     0, 1);
			if (is12simular) {
				final_candidate = 0;
				break;
			}
		}
		if (i == 2) {
			is13simular = _rtl92d_phy_simularity_compare(hw, result,
								     0, 2);
			if (is13simular) {
				final_candidate = 0;
				break;
			}
			is23simular = _rtl92d_phy_simularity_compare(hw, result,
								     1, 2);
			if (is23simular) {
				final_candidate = 1;
			} else {
				for (i = 0; i < 8; i++)
					regtmp += result[3][i];

				if (regtmp != 0)
					final_candidate = 3;
				else
					final_candidate = 0xFF;
			}
		}
	}
	rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
	for (i = 0; i < 4; i++) {
		rege94 = result[i][0];
		rege9c = result[i][1];
		regea4 = result[i][2];
		regeac = result[i][3];
		regeb4 = result[i][4];
		regebc = result[i][5];
		regec4 = result[i][6];
		regecc = result[i][7];
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2464
			"IQK: rege94=%lx rege9c=%lx regea4=%lx regeac=%lx regeb4=%lx regebc=%lx regec4=%lx regecc=%lx\n",
2465
			rege94, rege9c, regea4, regeac, regeb4, regebc, regec4,
2466
			regecc);
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
	}
	if (final_candidate != 0xff) {
		rtlphy->reg_e94 = rege94 = result[final_candidate][0];
		rtlphy->reg_e9c = rege9c = result[final_candidate][1];
		regea4 = result[final_candidate][2];
		regeac = result[final_candidate][3];
		rtlphy->reg_eb4 = regeb4 = result[final_candidate][4];
		rtlphy->reg_ebc = regebc = result[final_candidate][5];
		regec4 = result[final_candidate][6];
		regecc = result[final_candidate][7];
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2478
			"IQK: final_candidate is %x\n", final_candidate);
2479
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2480
			"IQK: rege94=%lx rege9c=%lx regea4=%lx regeac=%lx regeb4=%lx regebc=%lx regec4=%lx regecc=%lx\n",
2481
			rege94, rege9c, regea4, regeac, regeb4, regebc, regec4,
2482
			regecc);
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
		patha_ok = pathb_ok = true;
	} else {
		rtlphy->reg_e94 = rtlphy->reg_eb4 = 0x100; /* X default value */
		rtlphy->reg_e9c = rtlphy->reg_ebc = 0x0;   /* Y default value */
	}
	if ((rege94 != 0) /*&&(regea4 != 0) */)
		_rtl92d_phy_patha_fill_iqk_matrix(hw, patha_ok, result,
				final_candidate, (regea4 == 0));
	if (IS_92D_SINGLEPHY(rtlhal->version)) {
		if ((regeb4 != 0) /*&&(regec4 != 0) */)
			_rtl92d_phy_pathb_fill_iqk_matrix(hw, pathb_ok, result,
						final_candidate, (regec4 == 0));
	}
	if (final_candidate != 0xFF) {
		indexforchannel = rtl92d_get_rightchnlplace_for_iqk(
				  rtlphy->current_channel);

		for (i = 0; i < IQK_MATRIX_REG_NUM; i++)
			rtlphy->iqk_matrix_regsetting[indexforchannel].
				value[0][i] = result[final_candidate][i];
		rtlphy->iqk_matrix_regsetting[indexforchannel].iqk_done =
			true;

		RT_TRACE(rtlpriv, COMP_SCAN | COMP_MLME, DBG_LOUD,
2507
			 "IQK OK indexforchannel %d\n", indexforchannel);
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
	}
}

void rtl92d_phy_reload_iqk_setting(struct ieee80211_hw *hw, u8 channel)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u8 indexforchannel;

2518
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "channel %d\n", channel);
2519 2520
	/*------Do IQK for normal chip and test chip 5G band------- */
	indexforchannel = rtl92d_get_rightchnlplace_for_iqk(channel);
2521 2522 2523
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "indexforchannel %d done %d\n",
		 indexforchannel,
		 rtlphy->iqk_matrix_regsetting[indexforchannel].iqk_done);
2524 2525 2526 2527
	if (0 && !rtlphy->iqk_matrix_regsetting[indexforchannel].iqk_done &&
		rtlphy->need_iqk) {
		/* Re Do IQK. */
		RT_TRACE(rtlpriv, COMP_SCAN | COMP_INIT, DBG_LOUD,
2528
			 "Do IQK Matrix reg for channel:%d....\n", channel);
2529 2530 2531 2532 2533 2534 2535
		rtl92d_phy_iq_calibrate(hw);
	} else {
		/* Just load the value. */
		/* 2G band just load once. */
		if (((!rtlhal->load_imrandiqk_setting_for2g) &&
		    indexforchannel == 0) || indexforchannel > 0) {
			RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
2536 2537
				 "Just Read IQK Matrix reg for channel:%d....\n",
				 channel);
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560
			if ((rtlphy->iqk_matrix_regsetting[indexforchannel].
			     value[0] != NULL)
				/*&&(regea4 != 0) */)
				_rtl92d_phy_patha_fill_iqk_matrix(hw, true,
					rtlphy->iqk_matrix_regsetting[
					indexforchannel].value,	0,
					(rtlphy->iqk_matrix_regsetting[
					indexforchannel].value[0][2] == 0));
			if (IS_92D_SINGLEPHY(rtlhal->version)) {
				if ((rtlphy->iqk_matrix_regsetting[
					indexforchannel].value[0][4] != 0)
					/*&&(regec4 != 0) */)
					_rtl92d_phy_pathb_fill_iqk_matrix(hw,
						true,
						rtlphy->iqk_matrix_regsetting[
						indexforchannel].value, 0,
						(rtlphy->iqk_matrix_regsetting[
						indexforchannel].value[0][6]
						== 0));
			}
		}
	}
	rtlphy->need_iqk = false;
2561
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
}

static u32 _rtl92d_phy_get_abs(u32 val1, u32 val2)
{
	u32 ret;

	if (val1 >= val2)
		ret = val1 - val2;
	else
		ret = val2 - val1;
	return ret;
}

static bool _rtl92d_is_legal_5g_channel(struct ieee80211_hw *hw, u8 channel)
{

	int i;
	u8 channel_5g[45] = {
		36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58,
		60, 62, 64, 100, 102, 104, 106, 108, 110, 112,
		114, 116, 118, 120, 122, 124, 126, 128, 130, 132,
		134, 136, 138, 140, 149, 151, 153, 155, 157, 159,
		161, 163, 165
	};

	for (i = 0; i < sizeof(channel_5g); i++)
		if (channel == channel_5g[i])
			return true;
	return false;
}

static void _rtl92d_phy_calc_curvindex(struct ieee80211_hw *hw,
				       u32 *targetchnl, u32 * curvecount_val,
				       bool is5g, u32 *curveindex)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 smallest_abs_val = 0xffffffff, u4tmp;
	u8 i, j;
	u8 chnl_num = is5g ? TARGET_CHNL_NUM_5G : TARGET_CHNL_NUM_2G;

	for (i = 0; i < chnl_num; i++) {
		if (is5g && !_rtl92d_is_legal_5g_channel(hw, i + 1))
			continue;
		curveindex[i] = 0;
		for (j = 0; j < (CV_CURVE_CNT * 2); j++) {
			u4tmp = _rtl92d_phy_get_abs(targetchnl[i],
				curvecount_val[j]);

			if (u4tmp < smallest_abs_val) {
				curveindex[i] = j;
				smallest_abs_val = u4tmp;
			}
		}
		smallest_abs_val = 0xffffffff;
2616 2617
		RTPRINT(rtlpriv, FINIT, INIT_IQK, "curveindex[%d] = %x\n",
			i, curveindex[i]);
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
	}
}

static void _rtl92d_phy_reload_lck_setting(struct ieee80211_hw *hw,
		u8 channel)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u8 erfpath = rtlpriv->rtlhal.current_bandtype ==
		BAND_ON_5G ? RF90_PATH_A :
		IS_92D_SINGLEPHY(rtlpriv->rtlhal.version) ?
		RF90_PATH_B : RF90_PATH_A;
	u32 u4tmp = 0, u4regvalue = 0;
	bool bneed_powerdown_radio = false;

2632
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "path %d\n", erfpath);
2633 2634 2635
	RTPRINT(rtlpriv, FINIT, INIT_IQK, "band type = %d\n",
		rtlpriv->rtlhal.current_bandtype);
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "channel = %d\n", channel);
2636 2637 2638
	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G) {/* Path-A for 5G */
		u4tmp = curveindex_5g[channel-1];
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2639
			"ver 1 set RF-A, 5G,	0x28 = 0x%ulx !!\n", u4tmp);
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
		if (rtlpriv->rtlhal.macphymode == DUALMAC_DUALPHY &&
			rtlpriv->rtlhal.interfaceindex == 1) {
			bneed_powerdown_radio =
				rtl92d_phy_enable_anotherphy(hw, false);
			rtlpriv->rtlhal.during_mac1init_radioa = true;
			/* asume no this case */
			if (bneed_powerdown_radio)
				_rtl92d_phy_enable_rf_env(hw, erfpath,
							  &u4regvalue);
		}
		rtl_set_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800, u4tmp);
		if (bneed_powerdown_radio)
			_rtl92d_phy_restore_rf_env(hw, erfpath, &u4regvalue);
		if (rtlpriv->rtlhal.during_mac1init_radioa)
			rtl92d_phy_powerdown_anotherphy(hw, false);
	} else if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G) {
		u4tmp = curveindex_2g[channel-1];
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2658
			"ver 3 set RF-B, 2G, 0x28 = 0x%ulx !!\n", u4tmp);
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
		if (rtlpriv->rtlhal.macphymode == DUALMAC_DUALPHY &&
			rtlpriv->rtlhal.interfaceindex == 0) {
			bneed_powerdown_radio =
				rtl92d_phy_enable_anotherphy(hw, true);
			rtlpriv->rtlhal.during_mac0init_radiob = true;
			if (bneed_powerdown_radio)
				_rtl92d_phy_enable_rf_env(hw, erfpath,
							  &u4regvalue);
		}
		rtl_set_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800, u4tmp);
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2670 2671
			"ver 3 set RF-B, 2G, 0x28 = 0x%ulx !!\n",
			rtl_get_rfreg(hw,  erfpath, RF_SYN_G4, 0x3f800));
2672 2673 2674 2675 2676
		if (bneed_powerdown_radio)
			_rtl92d_phy_restore_rf_env(hw, erfpath, &u4regvalue);
		if (rtlpriv->rtlhal.during_mac0init_radiob)
			rtl92d_phy_powerdown_anotherphy(hw, true);
	}
2677
	RT_TRACE(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
}

static void _rtl92d_phy_lc_calibrate_sw(struct ieee80211_hw *hw, bool is2t)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
	u8 tmpreg, index, rf_mode[2];
	u8 path = is2t ? 2 : 1;
	u8 i;
	u32 u4tmp, offset;
	u32 curvecount_val[CV_CURVE_CNT * 2] = {0};
	u16 timeout = 800, timecount = 0;

	/* Check continuous TX and Packet TX */
	tmpreg = rtl_read_byte(rtlpriv, 0xd03);
	/* if Deal with contisuous TX case, disable all continuous TX */
	/* if Deal with Packet TX case, block all queues */
	if ((tmpreg & 0x70) != 0)
		rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
	else
		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xF00000, 0x0F);
	for (index = 0; index < path; index++) {
		/* 1. Read original RF mode */
		offset = index == 0 ? ROFDM0_XAAGCCORE1 : ROFDM0_XBAGCCORE1;
		rf_mode[index] = rtl_read_byte(rtlpriv, offset);
		/* 2. Set RF mode = standby mode */
		rtl_set_rfreg(hw, (enum radio_path)index, RF_AC,
			      BRFREGOFFSETMASK, 0x010000);
		if (rtlpci->init_ready) {
			/* switch CV-curve control by LC-calibration */
			rtl_set_rfreg(hw, (enum radio_path)index, RF_SYN_G7,
				      BIT(17), 0x0);
			/* 4. Set LC calibration begin */
			rtl_set_rfreg(hw, (enum radio_path)index, RF_CHNLBW,
				      0x08000, 0x01);
		}
		u4tmp = rtl_get_rfreg(hw, (enum radio_path)index, RF_SYN_G6,
				  BRFREGOFFSETMASK);
		while ((!(u4tmp & BIT(11))) && timecount <= timeout) {
			mdelay(50);
			timecount += 50;
			u4tmp = rtl_get_rfreg(hw, (enum radio_path)index,
					      RF_SYN_G6, BRFREGOFFSETMASK);
		}
		RTPRINT(rtlpriv, FINIT, INIT_IQK,
2725
			"PHY_LCK finish delay for %d ms=2\n", timecount);
2726 2727 2728
		u4tmp = rtl_get_rfreg(hw, index, RF_SYN_G4, BRFREGOFFSETMASK);
		if (index == 0 && rtlhal->interfaceindex == 0) {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
2729
				"path-A / 5G LCK\n");
2730 2731
		} else {
			RTPRINT(rtlpriv, FINIT, INIT_IQK,
2732
				"path-B / 2.4G LCK\n");
2733 2734 2735 2736 2737
		}
		memset(&curvecount_val[0], 0, CV_CURVE_CNT * 2);
		/* Set LC calibration off */
		rtl_set_rfreg(hw, (enum radio_path)index, RF_CHNLBW,
			      0x08000, 0x0);
2738
		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "set RF 0x18[15] = 0\n");
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
		/* save Curve-counting number */
		for (i = 0; i < CV_CURVE_CNT; i++) {
			u32 readval = 0, readval2 = 0;
			rtl_set_rfreg(hw, (enum radio_path)index, 0x3F,
				      0x7f, i);

			rtl_set_rfreg(hw, (enum radio_path)index, 0x4D,
				BRFREGOFFSETMASK, 0x0);
			readval = rtl_get_rfreg(hw, (enum radio_path)index,
					  0x4F, BRFREGOFFSETMASK);
			curvecount_val[2 * i + 1] = (readval & 0xfffe0) >> 5;
			/* reg 0x4f [4:0] */
			/* reg 0x50 [19:10] */
			readval2 = rtl_get_rfreg(hw, (enum radio_path)index,
						 0x50, 0xffc00);
			curvecount_val[2 * i] = (((readval & 0x1F) << 10) |
						 readval2);
		}
		if (index == 0 && rtlhal->interfaceindex == 0)
			_rtl92d_phy_calc_curvindex(hw, targetchnl_5g,
						   curvecount_val,
						   true, curveindex_5g);
		else
			_rtl92d_phy_calc_curvindex(hw, targetchnl_2g,
						   curvecount_val,
						   false, curveindex_2g);
		/* switch CV-curve control mode */
		rtl_set_rfreg(hw, (enum radio_path)index, RF_SYN_G7,
			      BIT(17), 0x1);
	}

	/* Restore original situation  */
	for (index = 0; index < path; index++) {
		offset = index == 0 ? ROFDM0_XAAGCCORE1 : ROFDM0_XBAGCCORE1;
		rtl_write_byte(rtlpriv, offset, 0x50);
		rtl_write_byte(rtlpriv, offset, rf_mode[index]);
	}
	if ((tmpreg & 0x70) != 0)
		rtl_write_byte(rtlpriv, 0xd03, tmpreg);
	else /*Deal with Packet TX case */
		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xF00000, 0x00);
	_rtl92d_phy_reload_lck_setting(hw, rtlpriv->phy.current_channel);
}

static void _rtl92d_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);

2788
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "cosa PHY_LCK ver=2\n");
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
	_rtl92d_phy_lc_calibrate_sw(hw, is2t);
}

void rtl92d_phy_lc_calibrate(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
	u32 timeout = 2000, timecount = 0;

	while (rtlpriv->mac80211.act_scanning && timecount < timeout) {
		udelay(50);
		timecount += 50;
	}

	rtlphy->lck_inprogress = true;
	RTPRINT(rtlpriv, FINIT, INIT_IQK,
2806 2807
		"LCK:Start!!! currentband %x delay %d ms\n",
		rtlhal->current_bandtype, timecount);
2808 2809 2810 2811 2812 2813 2814
	if (IS_92D_SINGLEPHY(rtlhal->version)) {
		_rtl92d_phy_lc_calibrate(hw, true);
	} else {
		/* For 1T1R */
		_rtl92d_phy_lc_calibrate(hw, false);
	}
	rtlphy->lck_inprogress = false;
2815
	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LCK:Finish!!!\n");
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
}

void rtl92d_phy_ap_calibrate(struct ieee80211_hw *hw, char delta)
{
	return;
}

static bool _rtl92d_phy_set_sw_chnl_cmdarray(struct swchnlcmd *cmdtable,
		u32 cmdtableidx, u32 cmdtablesz, enum swchnlcmd_id cmdid,
		u32 para1, u32 para2, u32 msdelay)
{
	struct swchnlcmd *pcmd;

	if (cmdtable == NULL) {
2830
		RT_ASSERT(false, "cmdtable cannot be NULL\n");
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
		return false;
	}
	if (cmdtableidx >= cmdtablesz)
		return false;

	pcmd = cmdtable + cmdtableidx;
	pcmd->cmdid = cmdid;
	pcmd->para1 = para1;
	pcmd->para2 = para2;
	pcmd->msdelay = msdelay;
	return true;
}

void rtl92d_phy_reset_iqk_result(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	u8 i;

	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2851 2852 2853 2854
		 "settings regs %d default regs %d\n",
		 (int)(sizeof(rtlphy->iqk_matrix_regsetting) /
		       sizeof(struct iqk_matrix_regs)),
		 IQK_MATRIX_REG_NUM);
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
	/* 0xe94, 0xe9c, 0xea4, 0xeac, 0xeb4, 0xebc, 0xec4, 0xecc */
	for (i = 0; i < IQK_MATRIX_SETTINGS_NUM; i++) {
		rtlphy->iqk_matrix_regsetting[i].value[0][0] = 0x100;
		rtlphy->iqk_matrix_regsetting[i].value[0][2] = 0x100;
		rtlphy->iqk_matrix_regsetting[i].value[0][4] = 0x100;
		rtlphy->iqk_matrix_regsetting[i].value[0][6] = 0x100;
		rtlphy->iqk_matrix_regsetting[i].value[0][1] = 0x0;
		rtlphy->iqk_matrix_regsetting[i].value[0][3] = 0x0;
		rtlphy->iqk_matrix_regsetting[i].value[0][5] = 0x0;
		rtlphy->iqk_matrix_regsetting[i].value[0][7] = 0x0;
		rtlphy->iqk_matrix_regsetting[i].iqk_done = false;
	}
}

static bool _rtl92d_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
					     u8 channel, u8 *stage, u8 *step,
					     u32 *delay)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
	u32 precommoncmdcnt;
	struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
	u32 postcommoncmdcnt;
	struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
	u32 rfdependcmdcnt;
	struct swchnlcmd *currentcmd = NULL;
	u8 rfpath;
	u8 num_total_rfpath = rtlphy->num_total_rfpath;

	precommoncmdcnt = 0;
	_rtl92d_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
					 MAX_PRECMD_CNT,
					 CMDID_SET_TXPOWEROWER_LEVEL, 0, 0, 0);
	_rtl92d_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
					 MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
	postcommoncmdcnt = 0;
	_rtl92d_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
					 MAX_POSTCMD_CNT, CMDID_END, 0, 0, 0);
	rfdependcmdcnt = 0;
	_rtl92d_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
					 MAX_RFDEPENDCMD_CNT, CMDID_RF_WRITEREG,
					 RF_CHNLBW, channel, 0);
	_rtl92d_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
					 MAX_RFDEPENDCMD_CNT, CMDID_END,
					 0, 0, 0);

	do {
		switch (*stage) {
		case 0:
			currentcmd = &precommoncmd[*step];
			break;
		case 1:
			currentcmd = &rfdependcmd[*step];
			break;
		case 2:
			currentcmd = &postcommoncmd[*step];
			break;
		}
		if (currentcmd->cmdid == CMDID_END) {
			if ((*stage) == 2) {
				return true;
			} else {
				(*stage)++;
				(*step) = 0;
				continue;
			}
		}
		switch (currentcmd->cmdid) {
		case CMDID_SET_TXPOWEROWER_LEVEL:
			rtl92d_phy_set_txpower_level(hw, channel);
			break;
		case CMDID_WRITEPORT_ULONG:
			rtl_write_dword(rtlpriv, currentcmd->para1,
					currentcmd->para2);
			break;
		case CMDID_WRITEPORT_USHORT:
			rtl_write_word(rtlpriv, currentcmd->para1,
				       (u16)currentcmd->para2);
			break;
		case CMDID_WRITEPORT_UCHAR:
			rtl_write_byte(rtlpriv, currentcmd->para1,
				       (u8)currentcmd->para2);
			break;
		case CMDID_RF_WRITEREG:
			for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
				rtlphy->rfreg_chnlval[rfpath] =
					((rtlphy->rfreg_chnlval[rfpath] &
					0xffffff00) | currentcmd->para2);
				if (rtlpriv->rtlhal.current_bandtype ==
				    BAND_ON_5G) {
					if (currentcmd->para2 > 99)
						rtlphy->rfreg_chnlval[rfpath] =
						    rtlphy->rfreg_chnlval
						    [rfpath] | (BIT(18));
					else
						rtlphy->rfreg_chnlval[rfpath] =
						    rtlphy->rfreg_chnlval
						    [rfpath] & (~BIT(18));
					rtlphy->rfreg_chnlval[rfpath] |=
						 (BIT(16) | BIT(8));
				} else {
					rtlphy->rfreg_chnlval[rfpath] &=
						~(BIT(8) | BIT(16) | BIT(18));
				}
				rtl_set_rfreg(hw, (enum radio_path)rfpath,
					      currentcmd->para1,
					      BRFREGOFFSETMASK,
					      rtlphy->rfreg_chnlval[rfpath]);
				_rtl92d_phy_reload_imr_setting(hw, channel,
							       rfpath);
			}
			_rtl92d_phy_switch_rf_setting(hw, channel);
			/* do IQK when all parameters are ready */
			rtl92d_phy_reload_iqk_setting(hw, channel);
			break;
		default:
			RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
2973
				 "switch case not processed\n");
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
			break;
		}
		break;
	} while (true);
	(*delay) = currentcmd->msdelay;
	(*step)++;
	return false;
}

u8 rtl92d_phy_sw_chnl(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	u32 delay;
	u32 timeout = 1000, timecount = 0;
	u8 channel = rtlphy->current_channel;
	u32 ret_value;

	if (rtlphy->sw_chnl_inprogress)
		return 0;
	if (rtlphy->set_bwmode_inprogress)
		return 0;

	if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
		RT_TRACE(rtlpriv, COMP_CHAN, DBG_LOUD,
3000
			 "sw_chnl_inprogress false driver sleep or unload\n");
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021
		return 0;
	}
	while (rtlphy->lck_inprogress && timecount < timeout) {
		mdelay(50);
		timecount += 50;
	}
	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
	    rtlhal->bandset == BAND_ON_BOTH) {
		ret_value = rtl_get_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
					  BMASKDWORD);
		if (rtlphy->current_channel > 14 && !(ret_value & BIT(0)))
			rtl92d_phy_switch_wirelessband(hw, BAND_ON_5G);
		else if (rtlphy->current_channel <= 14 && (ret_value & BIT(0)))
			rtl92d_phy_switch_wirelessband(hw, BAND_ON_2_4G);
	}
	switch (rtlhal->current_bandtype) {
	case BAND_ON_5G:
		/* Get first channel error when change between
		 * 5G and 2.4G band. */
		if (channel <= 14)
			return 0;
3022
		RT_ASSERT((channel > 14), "5G but channel<=14\n");
3023 3024 3025 3026 3027 3028
		break;
	case BAND_ON_2_4G:
		/* Get first channel error when change between
		 * 5G and 2.4G band. */
		if (channel > 14)
			return 0;
3029
		RT_ASSERT((channel <= 14), "2G but channel>14\n");
3030 3031
		break;
	default:
3032 3033
		RT_ASSERT(false, "Invalid WirelessMode(%#x)!!\n",
			  rtlpriv->mac80211.mode);
3034 3035 3036 3037 3038 3039 3040 3041
		break;
	}
	rtlphy->sw_chnl_inprogress = true;
	if (channel == 0)
		channel = 1;
	rtlphy->sw_chnl_stage = 0;
	rtlphy->sw_chnl_step = 0;
	RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE,
3042
		 "switch to channel%d\n", rtlphy->current_channel);
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058

	do {
		if (!rtlphy->sw_chnl_inprogress)
			break;
		if (!_rtl92d_phy_sw_chnl_step_by_step(hw,
						      rtlphy->current_channel,
		    &rtlphy->sw_chnl_stage, &rtlphy->sw_chnl_step, &delay)) {
			if (delay > 0)
				mdelay(delay);
			else
				continue;
		} else {
			rtlphy->sw_chnl_inprogress = false;
		}
		break;
	} while (true);
3059
	RT_TRACE(rtlpriv, COMP_SCAN, DBG_TRACE, "<==\n");
3060 3061 3062 3063 3064 3065 3066
	rtlphy->sw_chnl_inprogress = false;
	return 1;
}

static void rtl92d_phy_set_io(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
3067
	struct dig_t *de_digtable = &rtlpriv->dm_digtable;
3068 3069 3070
	struct rtl_phy *rtlphy = &(rtlpriv->phy);

	RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
3071 3072
		 "--->Cmd(%#x), set_io_inprogress(%d)\n",
		 rtlphy->current_io_type, rtlphy->set_io_inprogress);
3073 3074
	switch (rtlphy->current_io_type) {
	case IO_CMD_RESUME_DM_BY_SCAN:
3075
		de_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
3076 3077 3078 3079
		rtl92d_dm_write_dig(hw);
		rtl92d_phy_set_txpower_level(hw, rtlphy->current_channel);
		break;
	case IO_CMD_PAUSE_DM_BY_SCAN:
3080 3081
		rtlphy->initgain_backup.xaagccore1 = de_digtable->cur_igvalue;
		de_digtable->cur_igvalue = 0x37;
3082 3083 3084 3085
		rtl92d_dm_write_dig(hw);
		break;
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
3086
			 "switch case not processed\n");
3087 3088 3089
		break;
	}
	rtlphy->set_io_inprogress = false;
3090 3091
	RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE, "<---(%#x)\n",
		 rtlphy->current_io_type);
3092 3093 3094 3095 3096 3097 3098 3099 3100
}

bool rtl92d_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	bool postprocessing = false;

	RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
3101 3102
		 "-->IO Cmd(%#x), set_io_inprogress(%d)\n",
		 iotype, rtlphy->set_io_inprogress);
3103 3104 3105 3106
	do {
		switch (iotype) {
		case IO_CMD_RESUME_DM_BY_SCAN:
			RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
3107
				 "[IO CMD] Resume DM after scan\n");
3108 3109 3110 3111
			postprocessing = true;
			break;
		case IO_CMD_PAUSE_DM_BY_SCAN:
			RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE,
3112
				 "[IO CMD] Pause DM before scan\n");
3113 3114 3115 3116
			postprocessing = true;
			break;
		default:
			RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
3117
				 "switch case not processed\n");
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
			break;
		}
	} while (false);
	if (postprocessing && !rtlphy->set_io_inprogress) {
		rtlphy->set_io_inprogress = true;
		rtlphy->current_io_type = iotype;
	} else {
		return false;
	}
	rtl92d_phy_set_io(hw);
3128
	RT_TRACE(rtlpriv, COMP_CMD, DBG_TRACE, "<--IO Type(%#x)\n", iotype);
3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
	return true;
}

static void _rtl92d_phy_set_rfon(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	/* a.  SYS_CLKR 0x08[11] = 1  restore MAC clock */
	/* b.  SPS_CTRL 0x11[7:0] = 0x2b */
	if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY)
		rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
	/* c.  For PCIE: SYS_FUNC_EN 0x02[7:0] = 0xE3 enable BB TRX function */
	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
	/* RF_ON_EXCEP(d~g): */
	/* d.  APSD_CTRL 0x600[7:0] = 0x00 */
	rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
	/* e.  SYS_FUNC_EN 0x02[7:0] = 0xE2  reset BB TRX function again */
	/* f.  SYS_FUNC_EN 0x02[7:0] = 0xE3  enable BB TRX function*/
	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
	/* g.   txpause 0x522[7:0] = 0x00  enable mac tx queue */
	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
}

static void _rtl92d_phy_set_rfsleep(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	u32 u4btmp;
	u8 delay = 5;

	/* a.   TXPAUSE 0x522[7:0] = 0xFF  Pause MAC TX queue  */
	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
	/* b.   RF path 0 offset 0x00 = 0x00  disable RF  */
	rtl_set_rfreg(hw, RF90_PATH_A, 0x00, BRFREGOFFSETMASK, 0x00);
	/* c.   APSD_CTRL 0x600[7:0] = 0x40 */
	rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
	/* d. APSD_CTRL 0x600[7:0] = 0x00
	 * APSD_CTRL 0x600[7:0] = 0x00
	 * RF path 0 offset 0x00 = 0x00
	 * APSD_CTRL 0x600[7:0] = 0x40
	 * */
	u4btmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, BRFREGOFFSETMASK);
	while (u4btmp != 0 && delay > 0) {
		rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x0);
		rtl_set_rfreg(hw, RF90_PATH_A, 0x00, BRFREGOFFSETMASK, 0x00);
		rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
		u4btmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, BRFREGOFFSETMASK);
		delay--;
	}
	if (delay == 0) {
		/* Jump out the LPS turn off sequence to RF_ON_EXCEP */
		rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);

		rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
		rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
3186
			 "Fail !!! Switch RF timeout\n");
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
		return;
	}
	/* e.   For PCIE: SYS_FUNC_EN 0x02[7:0] = 0xE2 reset BB TRX function */
	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
	/* f.   SPS_CTRL 0x11[7:0] = 0x22 */
	if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY)
		rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
	/* g.    SYS_CLKR 0x08[11] = 0  gated MAC clock */
}

bool rtl92d_phy_set_rf_power_state(struct ieee80211_hw *hw,
				   enum rf_pwrstate rfpwr_state)
{

	bool bresult = true;
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
	u8 i, queue_id;
	struct rtl8192_tx_ring *ring = NULL;

	if (rfpwr_state == ppsc->rfpwr_state)
		return false;
	switch (rfpwr_state) {
	case ERFON:
		if ((ppsc->rfpwr_state == ERFOFF) &&
		    RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
			bool rtstatus;
			u32 InitializeCount = 0;
			do {
				InitializeCount++;
				RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
3221
					 "IPS Set eRf nic enable\n");
3222
				rtstatus = rtl_ps_enable_nic(hw);
3223
			} while (!rtstatus && (InitializeCount < 10));
3224 3225 3226 3227 3228

			RT_CLEAR_PS_LEVEL(ppsc,
					  RT_RF_OFF_LEVL_HALT_NIC);
		} else {
			RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
3229
				 "awake, sleeped:%d ms state_inap:%x\n",
3230
				 jiffies_to_msecs(jiffies -
3231 3232
						  ppsc->last_sleep_jiffies),
				 rtlpriv->psc.state_inap);
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246
			ppsc->last_awake_jiffies = jiffies;
			_rtl92d_phy_set_rfon(hw);
		}

		if (mac->link_state == MAC80211_LINKED)
			rtlpriv->cfg->ops->led_control(hw,
					 LED_CTL_LINK);
		else
			rtlpriv->cfg->ops->led_control(hw,
					 LED_CTL_NO_LINK);
		break;
	case ERFOFF:
		if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
			RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
3247
				 "IPS Set eRf nic disable\n");
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260
			rtl_ps_disable_nic(hw);
			RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
		} else {
			if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS)
				rtlpriv->cfg->ops->led_control(hw,
						 LED_CTL_NO_LINK);
			else
				rtlpriv->cfg->ops->led_control(hw,
						 LED_CTL_POWER_OFF);
		}
		break;
	case ERFSLEEP:
		if (ppsc->rfpwr_state == ERFOFF)
3261
			return false;
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271

		for (queue_id = 0, i = 0;
		     queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
			ring = &pcipriv->dev.tx_ring[queue_id];
			if (skb_queue_len(&ring->queue) == 0 ||
			    queue_id == BEACON_QUEUE) {
				queue_id++;
				continue;
			} else if (rtlpci->pdev->current_state != PCI_D0) {
				RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
3272 3273
					 "eRf Off/Sleep: %d times TcbBusyQueue[%d] !=0 but lower power state!\n",
					 i + 1, queue_id);
3274 3275 3276
				break;
			} else {
				RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
3277 3278 3279
					 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
					 i + 1, queue_id,
					 skb_queue_len(&ring->queue));
3280 3281 3282 3283 3284 3285
				udelay(10);
				i++;
			}

			if (i >= MAX_DOZE_WAITING_TIMES_9x) {
				RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
3286 3287 3288
					 "ERFOFF: %d times TcbBusyQueue[%d] = %d !\n",
					 MAX_DOZE_WAITING_TIMES_9x, queue_id,
					 skb_queue_len(&ring->queue));
3289 3290 3291 3292
				break;
			}
		}
		RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
3293 3294 3295 3296 3297 3298 3299
			 "Set rfsleep awaked:%d ms\n",
			 jiffies_to_msecs(jiffies - ppsc->last_awake_jiffies));
		RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
			 "sleep awaked:%d ms state_inap:%x\n",
			 jiffies_to_msecs(jiffies -
					  ppsc->last_awake_jiffies),
			 rtlpriv->psc.state_inap);
3300 3301 3302 3303 3304
		ppsc->last_sleep_jiffies = jiffies;
		_rtl92d_phy_set_rfsleep(hw);
		break;
	default:
		RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
3305
			 "switch case not processed\n");
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322
		bresult = false;
		break;
	}
	if (bresult)
		ppsc->rfpwr_state = rfpwr_state;
	return bresult;
}

void rtl92d_phy_config_macphymode(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	u8 offset = REG_MAC_PHY_CTRL_NORMAL;

	switch (rtlhal->macphymode) {
	case DUALMAC_DUALPHY:
		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3323
			 "MacPhyMode: DUALMAC_DUALPHY\n");
3324 3325 3326 3327
		rtl_write_byte(rtlpriv, offset, 0xF3);
		break;
	case SINGLEMAC_SINGLEPHY:
		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3328
			 "MacPhyMode: SINGLEMAC_SINGLEPHY\n");
3329 3330 3331 3332
		rtl_write_byte(rtlpriv, offset, 0xF4);
		break;
	case DUALMAC_SINGLEPHY:
		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3333
			 "MacPhyMode: DUALMAC_SINGLEPHY\n");
3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
		rtl_write_byte(rtlpriv, offset, 0xF1);
		break;
	}
}

void rtl92d_phy_config_macphymode_info(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_phy *rtlphy = &(rtlpriv->phy);

	switch (rtlhal->macphymode) {
	case DUALMAC_SINGLEPHY:
		rtlphy->rf_type = RF_2T2R;
3348
		rtlhal->version |= RF_TYPE_2T2R;
3349 3350 3351 3352 3353 3354
		rtlhal->bandset = BAND_ON_BOTH;
		rtlhal->current_bandtype = BAND_ON_2_4G;
		break;

	case SINGLEMAC_SINGLEPHY:
		rtlphy->rf_type = RF_2T2R;
3355
		rtlhal->version |= RF_TYPE_2T2R;
3356 3357 3358 3359 3360 3361
		rtlhal->bandset = BAND_ON_BOTH;
		rtlhal->current_bandtype = BAND_ON_2_4G;
		break;

	case DUALMAC_DUALPHY:
		rtlphy->rf_type = RF_1T1R;
3362
		rtlhal->version &= RF_TYPE_1T1R;
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
		/* Now we let MAC0 run on 5G band. */
		if (rtlhal->interfaceindex == 0) {
			rtlhal->bandset = BAND_ON_5G;
			rtlhal->current_bandtype = BAND_ON_5G;
		} else {
			rtlhal->bandset = BAND_ON_2_4G;
			rtlhal->current_bandtype = BAND_ON_2_4G;
		}
		break;
	default:
		break;
	}
}

u8 rtl92d_get_chnlgroup_fromarray(u8 chnl)
{
	u8 group;
	u8 channel_info[59] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56,
		58, 60, 62, 64, 100, 102, 104, 106, 108,
		110, 112, 114, 116, 118, 120, 122, 124,
		126, 128, 130, 132, 134, 136, 138, 140,
		149, 151, 153, 155, 157, 159, 161, 163,
		165
	};

	if (channel_info[chnl] <= 3)
		group = 0;
	else if (channel_info[chnl] <= 9)
		group = 1;
	else if (channel_info[chnl] <= 14)
		group = 2;
	else if (channel_info[chnl] <= 44)
		group = 3;
	else if (channel_info[chnl] <= 54)
		group = 4;
	else if (channel_info[chnl] <= 64)
		group = 5;
	else if (channel_info[chnl] <= 112)
		group = 6;
	else if (channel_info[chnl] <= 126)
		group = 7;
	else if (channel_info[chnl] <= 140)
		group = 8;
	else if (channel_info[chnl] <= 153)
		group = 9;
	else if (channel_info[chnl] <= 159)
		group = 10;
	else
		group = 11;
	return group;
}

void rtl92d_phy_set_poweron(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	unsigned long flags;
	u8 value8;
	u16 i;
	u32 mac_reg = (rtlhal->interfaceindex == 0 ? REG_MAC0 : REG_MAC1);

	/* notice fw know band status  0x81[1]/0x53[1] = 0: 5G, 1: 2G */
	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
		value8 = rtl_read_byte(rtlpriv, mac_reg);
		value8 |= BIT(1);
		rtl_write_byte(rtlpriv, mac_reg, value8);
	} else {
		value8 = rtl_read_byte(rtlpriv, mac_reg);
		value8 &= (~BIT(1));
		rtl_write_byte(rtlpriv, mac_reg, value8);
	}

	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY) {
		value8 = rtl_read_byte(rtlpriv, REG_MAC0);
		rtl_write_byte(rtlpriv, REG_MAC0, value8 | MAC0_ON);
	} else {
		spin_lock_irqsave(&globalmutex_power, flags);
		if (rtlhal->interfaceindex == 0) {
			value8 = rtl_read_byte(rtlpriv, REG_MAC0);
			rtl_write_byte(rtlpriv, REG_MAC0, value8 | MAC0_ON);
		} else {
			value8 = rtl_read_byte(rtlpriv, REG_MAC1);
			rtl_write_byte(rtlpriv, REG_MAC1, value8 | MAC1_ON);
		}
		value8 = rtl_read_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS);
		spin_unlock_irqrestore(&globalmutex_power, flags);
		for (i = 0; i < 200; i++) {
			if ((value8 & BIT(7)) == 0) {
				break;
			} else {
				udelay(500);
				spin_lock_irqsave(&globalmutex_power, flags);
				value8 = rtl_read_byte(rtlpriv,
						    REG_POWER_OFF_IN_PROCESS);
				spin_unlock_irqrestore(&globalmutex_power,
						       flags);
			}
		}
		if (i == 200)
3464
			RT_ASSERT(false, "Another mac power off over time\n");
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
	}
}

void rtl92d_phy_config_maccoexist_rfpage(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);

	switch (rtlpriv->rtlhal.macphymode) {
	case DUALMAC_DUALPHY:
		rtl_write_byte(rtlpriv, REG_DMC, 0x0);
		rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x08);
		rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x13ff);
		break;
	case DUALMAC_SINGLEPHY:
		rtl_write_byte(rtlpriv, REG_DMC, 0xf8);
		rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x08);
		rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x13ff);
		break;
	case SINGLEMAC_SINGLEPHY:
		rtl_write_byte(rtlpriv, REG_DMC, 0x0);
		rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x10);
		rtl_write_word(rtlpriv, (REG_TRXFF_BNDY + 2), 0x27FF);
		break;
	default:
		break;
	}
}

void rtl92d_update_bbrf_configuration(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	struct rtl_phy *rtlphy = &(rtlpriv->phy);
	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
	u8 rfpath, i;

3501
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "==>\n");
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
	/* r_select_5G for path_A/B 0 for 2.4G, 1 for 5G */
	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
		/* r_select_5G for path_A/B,0x878 */
		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(0), 0x0);
		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0x0);
		if (rtlhal->macphymode != DUALMAC_DUALPHY) {
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(16), 0x0);
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(31), 0x0);
		}
		/* rssi_table_select:index 0 for 2.4G.1~3 for 5G,0xc78 */
		rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, BIT(6) | BIT(7), 0x0);
		/* fc_area  0xd2c */
		rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(14) | BIT(13), 0x0);
		/* 5G LAN ON */
		rtl_set_bbreg(hw, 0xB30, 0x00F00000, 0xa);
		/* TX BB gain shift*1,Just for testchip,0xc80,0xc88 */
		rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, BMASKDWORD,
			      0x40000100);
		rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, BMASKDWORD,
			      0x40000100);
		if (rtlhal->macphymode == DUALMAC_DUALPHY) {
			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
				      BIT(10) | BIT(6) | BIT(5),
				      ((rtlefuse->eeprom_c9 & BIT(3)) >> 3) |
				      (rtlefuse->eeprom_c9 & BIT(1)) |
				      ((rtlefuse->eeprom_cc & BIT(1)) << 4));
			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE,
				      BIT(10) | BIT(6) | BIT(5),
				      ((rtlefuse->eeprom_c9 & BIT(2)) >> 2) |
				      ((rtlefuse->eeprom_c9 & BIT(0)) << 1) |
				      ((rtlefuse->eeprom_cc & BIT(0)) << 5));
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0);
		} else {
			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
				      BIT(26) | BIT(22) | BIT(21) | BIT(10) |
				      BIT(6) | BIT(5),
				      ((rtlefuse->eeprom_c9 & BIT(3)) >> 3) |
				      (rtlefuse->eeprom_c9 & BIT(1)) |
				      ((rtlefuse->eeprom_cc & BIT(1)) << 4) |
				      ((rtlefuse->eeprom_c9 & BIT(7)) << 9) |
				      ((rtlefuse->eeprom_c9 & BIT(5)) << 12) |
				      ((rtlefuse->eeprom_cc & BIT(3)) << 18));
			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE,
				      BIT(10) | BIT(6) | BIT(5),
				      ((rtlefuse->eeprom_c9 & BIT(2)) >> 2) |
				      ((rtlefuse->eeprom_c9 & BIT(0)) << 1) |
				      ((rtlefuse->eeprom_cc & BIT(0)) << 5));
			rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE,
				      BIT(10) | BIT(6) | BIT(5),
				      ((rtlefuse->eeprom_c9 & BIT(6)) >> 6) |
				      ((rtlefuse->eeprom_c9 & BIT(4)) >> 3) |
				      ((rtlefuse->eeprom_cc & BIT(2)) << 3));
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
				      BIT(31) | BIT(15), 0);
		}
		/* 1.5V_LDO */
	} else {
		/* r_select_5G for path_A/B */
		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(0), 0x1);
		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0x1);
		if (rtlhal->macphymode != DUALMAC_DUALPHY) {
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(16), 0x1);
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(31), 0x1);
		}
		/* rssi_table_select:index 0 for 2.4G.1~3 for 5G */
		rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, BIT(6) | BIT(7), 0x1);
		/* fc_area */
		rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(14) | BIT(13), 0x1);
		/* 5G LAN ON */
		rtl_set_bbreg(hw, 0xB30, 0x00F00000, 0x0);
		/* TX BB gain shift,Just for testchip,0xc80,0xc88 */
		if (rtlefuse->internal_pa_5g[0])
			rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, BMASKDWORD,
				      0x2d4000b5);
		else
			rtl_set_bbreg(hw, ROFDM0_XATxIQIMBALANCE, BMASKDWORD,
				      0x20000080);
		if (rtlefuse->internal_pa_5g[1])
			rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, BMASKDWORD,
				      0x2d4000b5);
		else
			rtl_set_bbreg(hw, ROFDM0_XBTxIQIMBALANCE, BMASKDWORD,
				      0x20000080);
		if (rtlhal->macphymode == DUALMAC_DUALPHY) {
			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
				      BIT(10) | BIT(6) | BIT(5),
				      (rtlefuse->eeprom_cc & BIT(5)));
			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BIT(10),
				      ((rtlefuse->eeprom_cc & BIT(4)) >> 4));
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15),
				      (rtlefuse->eeprom_cc & BIT(4)) >> 4);
		} else {
			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
				      BIT(26) | BIT(22) | BIT(21) | BIT(10) |
				      BIT(6) | BIT(5),
				      (rtlefuse->eeprom_cc & BIT(5)) |
				      ((rtlefuse->eeprom_cc & BIT(7)) << 14));
			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BIT(10),
				      ((rtlefuse->eeprom_cc & BIT(4)) >> 4));
			rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, BIT(10),
				      ((rtlefuse->eeprom_cc & BIT(6)) >> 6));
			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
				      BIT(31) | BIT(15),
				      ((rtlefuse->eeprom_cc & BIT(4)) >> 4) |
				      ((rtlefuse->eeprom_cc & BIT(6)) << 10));
		}
	}
	/* update IQK related settings */
	rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, BMASKDWORD, 0x40000100);
	rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, BMASKDWORD, 0x40000100);
	rtl_set_bbreg(hw, ROFDM0_XCTxAFE, 0xF0000000, 0x00);
	rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(30) | BIT(28) |
		      BIT(26) | BIT(24), 0x00);
	rtl_set_bbreg(hw, ROFDM0_XDTxAFE, 0xF0000000, 0x00);
	rtl_set_bbreg(hw, 0xca0, 0xF0000000, 0x00);
	rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, 0x0000F000, 0x00);

	/* Update RF */
	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
	     rfpath++) {
		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
			/* MOD_AG for RF paht_A 0x18 BIT8,BIT16 */
			rtl_set_rfreg(hw, rfpath, RF_CHNLBW, BIT(8) | BIT(16) |
				      BIT(18), 0);
			/* RF0x0b[16:14] =3b'111 */
			rtl_set_rfreg(hw, (enum radio_path)rfpath, 0x0B,
				      0x1c000, 0x07);
		} else {
			/* MOD_AG for RF paht_A 0x18 BIT8,BIT16 */
			rtl_set_rfreg(hw, rfpath, RF_CHNLBW, BIT(8) |
				      BIT(16) | BIT(18),
				      (BIT(16) | BIT(8)) >> 8);
		}
	}
	/* Update for all band. */
	/* DMDP */
	if (rtlphy->rf_type == RF_1T1R) {
		/* Use antenna 0,0xc04,0xd04 */
		rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, BMASKBYTE0, 0x11);
		rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x1);

		/* enable ad/da clock1 for dual-phy reg0x888 */
		if (rtlhal->interfaceindex == 0) {
			rtl_set_bbreg(hw, RFPGA0_ADDALLOCKEN, BIT(12) |
				      BIT(13), 0x3);
		} else {
			rtl92d_phy_enable_anotherphy(hw, false);
			RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3650
				 "MAC1 use DBI to update 0x888\n");
3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674
			/* 0x888 */
			rtl92de_write_dword_dbi(hw, RFPGA0_ADDALLOCKEN,
						rtl92de_read_dword_dbi(hw,
						RFPGA0_ADDALLOCKEN,
						BIT(3)) | BIT(12) | BIT(13),
						BIT(3));
			rtl92d_phy_powerdown_anotherphy(hw, false);
		}
	} else {
		/* Single PHY */
		/* Use antenna 0 & 1,0xc04,0xd04 */
		rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, BMASKBYTE0, 0x33);
		rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x3);
		/* disable ad/da clock1,0x888 */
		rtl_set_bbreg(hw, RFPGA0_ADDALLOCKEN, BIT(12) | BIT(13), 0);
	}
	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
	     rfpath++) {
		rtlphy->rfreg_chnlval[rfpath] = rtl_get_rfreg(hw, rfpath,
						RF_CHNLBW, BRFREGOFFSETMASK);
		rtlphy->reg_rf3c[rfpath] = rtl_get_rfreg(hw, rfpath, 0x3C,
			BRFREGOFFSETMASK);
	}
	for (i = 0; i < 2; i++)
3675 3676 3677
		RT_TRACE(rtlpriv, COMP_RF, DBG_LOUD, "RF 0x18 = 0x%x\n",
			 rtlphy->rfreg_chnlval[i]);
	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "<==\n");
3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714

}

bool rtl92d_phy_check_poweroff(struct ieee80211_hw *hw)
{
	struct rtl_priv *rtlpriv = rtl_priv(hw);
	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
	u8 u1btmp;
	unsigned long flags;

	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY) {
		u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
		rtl_write_byte(rtlpriv, REG_MAC0, u1btmp & (~MAC0_ON));
		return true;
	}
	spin_lock_irqsave(&globalmutex_power, flags);
	if (rtlhal->interfaceindex == 0) {
		u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
		rtl_write_byte(rtlpriv, REG_MAC0, u1btmp & (~MAC0_ON));
		u1btmp = rtl_read_byte(rtlpriv, REG_MAC1);
		u1btmp &= MAC1_ON;
	} else {
		u1btmp = rtl_read_byte(rtlpriv, REG_MAC1);
		rtl_write_byte(rtlpriv, REG_MAC1, u1btmp & (~MAC1_ON));
		u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
		u1btmp &= MAC0_ON;
	}
	if (u1btmp) {
		spin_unlock_irqrestore(&globalmutex_power, flags);
		return false;
	}
	u1btmp = rtl_read_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS);
	u1btmp |= BIT(7);
	rtl_write_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS, u1btmp);
	spin_unlock_irqrestore(&globalmutex_power, flags);
	return true;
}