1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2013 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include <linux/firmware.h>
9 #include <linux/slab.h>
10 #include <drv_types.h>
11 #include <rtl8723b_hal.h>
12 #include "hal_com_h2c.h"
13 
_FWDownloadEnable(struct adapter * padapter,bool enable)14 static void _FWDownloadEnable(struct adapter *padapter, bool enable)
15 {
16 	u8 tmp, count = 0;
17 
18 	if (enable) {
19 		/*  8051 enable */
20 		tmp = rtw_read8(padapter, REG_SYS_FUNC_EN+1);
21 		rtw_write8(padapter, REG_SYS_FUNC_EN+1, tmp|0x04);
22 
23 		tmp = rtw_read8(padapter, REG_MCUFWDL);
24 		rtw_write8(padapter, REG_MCUFWDL, tmp|0x01);
25 
26 		do {
27 			tmp = rtw_read8(padapter, REG_MCUFWDL);
28 			if (tmp & 0x01)
29 				break;
30 			rtw_write8(padapter, REG_MCUFWDL, tmp|0x01);
31 			msleep(1);
32 		} while (count++ < 100);
33 
34 		/*  8051 reset */
35 		tmp = rtw_read8(padapter, REG_MCUFWDL+2);
36 		rtw_write8(padapter, REG_MCUFWDL+2, tmp&0xf7);
37 	} else {
38 		/*  MCU firmware download disable. */
39 		tmp = rtw_read8(padapter, REG_MCUFWDL);
40 		rtw_write8(padapter, REG_MCUFWDL, tmp&0xfe);
41 	}
42 }
43 
_BlockWrite(struct adapter * padapter,void * buffer,u32 buffSize)44 static int _BlockWrite(struct adapter *padapter, void *buffer, u32 buffSize)
45 {
46 	int ret = _SUCCESS;
47 
48 	u32 blockSize_p1 = 4; /*  (Default) Phase #1 : PCI muse use 4-byte write to download FW */
49 	u32 blockSize_p2 = 8; /*  Phase #2 : Use 8-byte, if Phase#1 use big size to write FW. */
50 	u32 blockSize_p3 = 1; /*  Phase #3 : Use 1-byte, the remnant of FW image. */
51 	u32 blockCount_p1 = 0, blockCount_p2 = 0, blockCount_p3 = 0;
52 	u32 remainSize_p1 = 0, remainSize_p2 = 0;
53 	u8 *bufferPtr = buffer;
54 	u32 i = 0, offset = 0;
55 
56 /* 	printk("====>%s %d\n", __func__, __LINE__); */
57 
58 	/* 3 Phase #1 */
59 	blockCount_p1 = buffSize / blockSize_p1;
60 	remainSize_p1 = buffSize % blockSize_p1;
61 
62 	for (i = 0; i < blockCount_p1; i++) {
63 		ret = rtw_write32(padapter, (FW_8723B_START_ADDRESS + i * blockSize_p1), *((u32 *)(bufferPtr + i * blockSize_p1)));
64 		if (ret == _FAIL) {
65 			printk("====>%s %d i:%d\n", __func__, __LINE__, i);
66 			goto exit;
67 		}
68 	}
69 
70 	/* 3 Phase #2 */
71 	if (remainSize_p1) {
72 		offset = blockCount_p1 * blockSize_p1;
73 
74 		blockCount_p2 = remainSize_p1/blockSize_p2;
75 		remainSize_p2 = remainSize_p1%blockSize_p2;
76 	}
77 
78 	/* 3 Phase #3 */
79 	if (remainSize_p2) {
80 		offset = (blockCount_p1 * blockSize_p1) + (blockCount_p2 * blockSize_p2);
81 
82 		blockCount_p3 = remainSize_p2 / blockSize_p3;
83 
84 		for (i = 0; i < blockCount_p3; i++) {
85 			ret = rtw_write8(padapter, (FW_8723B_START_ADDRESS + offset + i), *(bufferPtr + offset + i));
86 
87 			if (ret == _FAIL) {
88 				printk("====>%s %d i:%d\n", __func__, __LINE__, i);
89 				goto exit;
90 			}
91 		}
92 	}
93 exit:
94 	return ret;
95 }
96 
_PageWrite(struct adapter * padapter,u32 page,void * buffer,u32 size)97 static int _PageWrite(
98 	struct adapter *padapter,
99 	u32 page,
100 	void *buffer,
101 	u32 size
102 )
103 {
104 	u8 value8;
105 	u8 u8Page = (u8) (page & 0x07);
106 
107 	value8 = (rtw_read8(padapter, REG_MCUFWDL+2) & 0xF8) | u8Page;
108 	rtw_write8(padapter, REG_MCUFWDL+2, value8);
109 
110 	return _BlockWrite(padapter, buffer, size);
111 }
112 
_WriteFW(struct adapter * padapter,void * buffer,u32 size)113 static int _WriteFW(struct adapter *padapter, void *buffer, u32 size)
114 {
115 	/*  Since we need dynamic decide method of dwonload fw, so we call this function to get chip version. */
116 	/*  We can remove _ReadChipVersion from ReadpadapterInfo8192C later. */
117 	int ret = _SUCCESS;
118 	u32 pageNums, remainSize;
119 	u32 page, offset;
120 	u8 *bufferPtr = buffer;
121 
122 	pageNums = size / MAX_DLFW_PAGE_SIZE;
123 	remainSize = size % MAX_DLFW_PAGE_SIZE;
124 
125 	for (page = 0; page < pageNums; page++) {
126 		offset = page * MAX_DLFW_PAGE_SIZE;
127 		ret = _PageWrite(padapter, page, bufferPtr+offset, MAX_DLFW_PAGE_SIZE);
128 
129 		if (ret == _FAIL) {
130 			printk("====>%s %d\n", __func__, __LINE__);
131 			goto exit;
132 		}
133 	}
134 
135 	if (remainSize) {
136 		offset = pageNums * MAX_DLFW_PAGE_SIZE;
137 		page = pageNums;
138 		ret = _PageWrite(padapter, page, bufferPtr+offset, remainSize);
139 
140 		if (ret == _FAIL) {
141 			printk("====>%s %d\n", __func__, __LINE__);
142 			goto exit;
143 		}
144 	}
145 
146 exit:
147 	return ret;
148 }
149 
_8051Reset8723(struct adapter * padapter)150 void _8051Reset8723(struct adapter *padapter)
151 {
152 	u8 cpu_rst;
153 	u8 io_rst;
154 
155 
156 	/*  Reset 8051(WLMCU) IO wrapper */
157 	/*  0x1c[8] = 0 */
158 	/*  Suggested by Isaac@SD1 and Gimmy@SD1, coding by Lucas@20130624 */
159 	io_rst = rtw_read8(padapter, REG_RSV_CTRL+1);
160 	io_rst &= ~BIT(0);
161 	rtw_write8(padapter, REG_RSV_CTRL+1, io_rst);
162 
163 	cpu_rst = rtw_read8(padapter, REG_SYS_FUNC_EN+1);
164 	cpu_rst &= ~BIT(2);
165 	rtw_write8(padapter, REG_SYS_FUNC_EN+1, cpu_rst);
166 
167 	/*  Enable 8051 IO wrapper */
168 	/*  0x1c[8] = 1 */
169 	io_rst = rtw_read8(padapter, REG_RSV_CTRL+1);
170 	io_rst |= BIT(0);
171 	rtw_write8(padapter, REG_RSV_CTRL+1, io_rst);
172 
173 	cpu_rst = rtw_read8(padapter, REG_SYS_FUNC_EN+1);
174 	cpu_rst |= BIT(2);
175 	rtw_write8(padapter, REG_SYS_FUNC_EN+1, cpu_rst);
176 }
177 
178 u8 g_fwdl_chksum_fail;
179 
polling_fwdl_chksum(struct adapter * adapter,u32 min_cnt,u32 timeout_ms)180 static s32 polling_fwdl_chksum(
181 	struct adapter *adapter, u32 min_cnt, u32 timeout_ms
182 )
183 {
184 	s32 ret = _FAIL;
185 	u32 value32;
186 	unsigned long start = jiffies;
187 	u32 cnt = 0;
188 
189 	/* polling CheckSum report */
190 	do {
191 		cnt++;
192 		value32 = rtw_read32(adapter, REG_MCUFWDL);
193 		if (value32 & FWDL_ChkSum_rpt || adapter->bSurpriseRemoved || adapter->bDriverStopped)
194 			break;
195 		yield();
196 	} while (jiffies_to_msecs(jiffies-start) < timeout_ms || cnt < min_cnt);
197 
198 	if (!(value32 & FWDL_ChkSum_rpt)) {
199 		goto exit;
200 	}
201 
202 	if (g_fwdl_chksum_fail) {
203 		g_fwdl_chksum_fail--;
204 		goto exit;
205 	}
206 
207 	ret = _SUCCESS;
208 
209 exit:
210 
211 	return ret;
212 }
213 
214 u8 g_fwdl_wintint_rdy_fail;
215 
_FWFreeToGo(struct adapter * adapter,u32 min_cnt,u32 timeout_ms)216 static s32 _FWFreeToGo(struct adapter *adapter, u32 min_cnt, u32 timeout_ms)
217 {
218 	s32 ret = _FAIL;
219 	u32 value32;
220 	unsigned long start = jiffies;
221 	u32 cnt = 0;
222 
223 	value32 = rtw_read32(adapter, REG_MCUFWDL);
224 	value32 |= MCUFWDL_RDY;
225 	value32 &= ~WINTINI_RDY;
226 	rtw_write32(adapter, REG_MCUFWDL, value32);
227 
228 	_8051Reset8723(adapter);
229 
230 	/*  polling for FW ready */
231 	do {
232 		cnt++;
233 		value32 = rtw_read32(adapter, REG_MCUFWDL);
234 		if (value32 & WINTINI_RDY || adapter->bSurpriseRemoved || adapter->bDriverStopped)
235 			break;
236 		yield();
237 	} while (jiffies_to_msecs(jiffies - start) < timeout_ms || cnt < min_cnt);
238 
239 	if (!(value32 & WINTINI_RDY)) {
240 		goto exit;
241 	}
242 
243 	if (g_fwdl_wintint_rdy_fail) {
244 		g_fwdl_wintint_rdy_fail--;
245 		goto exit;
246 	}
247 
248 	ret = _SUCCESS;
249 
250 exit:
251 
252 	return ret;
253 }
254 
255 #define IS_FW_81xxC(padapter)	(((GET_HAL_DATA(padapter))->FirmwareSignature & 0xFFF0) == 0x88C0)
256 
rtl8723b_FirmwareSelfReset(struct adapter * padapter)257 void rtl8723b_FirmwareSelfReset(struct adapter *padapter)
258 {
259 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
260 	u8 u1bTmp;
261 	u8 Delay = 100;
262 
263 	if (
264 		!(IS_FW_81xxC(padapter) && ((pHalData->FirmwareVersion < 0x21) || (pHalData->FirmwareVersion == 0x21 && pHalData->FirmwareSubVersion < 0x01)))
265 	) { /*  after 88C Fw v33.1 */
266 		/* 0x1cf = 0x20. Inform 8051 to reset. 2009.12.25. tynli_test */
267 		rtw_write8(padapter, REG_HMETFR+3, 0x20);
268 
269 		u1bTmp = rtw_read8(padapter, REG_SYS_FUNC_EN+1);
270 		while (u1bTmp & BIT2) {
271 			Delay--;
272 			if (Delay == 0)
273 				break;
274 			udelay(50);
275 			u1bTmp = rtw_read8(padapter, REG_SYS_FUNC_EN+1);
276 		}
277 
278 		if (Delay == 0) {
279 			/* force firmware reset */
280 			u1bTmp = rtw_read8(padapter, REG_SYS_FUNC_EN+1);
281 			rtw_write8(padapter, REG_SYS_FUNC_EN+1, u1bTmp&(~BIT2));
282 		}
283 	}
284 }
285 
286 /*  */
287 /* 	Description: */
288 /* 		Download 8192C firmware code. */
289 /*  */
290 /*  */
rtl8723b_FirmwareDownload(struct adapter * padapter,bool bUsedWoWLANFw)291 s32 rtl8723b_FirmwareDownload(struct adapter *padapter, bool  bUsedWoWLANFw)
292 {
293 	s32 rtStatus = _SUCCESS;
294 	u8 write_fw = 0;
295 	unsigned long fwdl_start_time;
296 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
297 	struct rt_firmware *pFirmware;
298 	struct rt_firmware *pBTFirmware;
299 	struct rt_firmware_hdr *pFwHdr = NULL;
300 	u8 *pFirmwareBuf;
301 	u32 FirmwareLen;
302 	const struct firmware *fw;
303 	struct device *device = dvobj_to_dev(padapter->dvobj);
304 	u8 *fwfilepath;
305 	struct dvobj_priv *psdpriv = padapter->dvobj;
306 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
307 	u8 tmp_ps;
308 
309 	pFirmware = kzalloc(sizeof(struct rt_firmware), GFP_KERNEL);
310 	if (!pFirmware)
311 		return _FAIL;
312 	pBTFirmware = kzalloc(sizeof(struct rt_firmware), GFP_KERNEL);
313 	if (!pBTFirmware) {
314 		kfree(pFirmware);
315 		return _FAIL;
316 	}
317 	tmp_ps = rtw_read8(padapter, 0xa3);
318 	tmp_ps &= 0xf8;
319 	tmp_ps |= 0x02;
320 	/* 1. write 0xA3[:2:0] = 3b'010 */
321 	rtw_write8(padapter, 0xa3, tmp_ps);
322 	/* 2. read power_state = 0xA0[1:0] */
323 	tmp_ps = rtw_read8(padapter, 0xa0);
324 	tmp_ps &= 0x03;
325 	if (tmp_ps != 0x01)
326 		pdbgpriv->dbg_downloadfw_pwr_state_cnt++;
327 
328 	fwfilepath = "rtlwifi/rtl8723bs_nic.bin";
329 
330 	pr_info("rtl8723bs: acquire FW from file:%s\n", fwfilepath);
331 
332 	rtStatus = request_firmware(&fw, fwfilepath, device);
333 	if (rtStatus) {
334 		pr_err("Request firmware failed with error 0x%x\n", rtStatus);
335 		rtStatus = _FAIL;
336 		goto exit;
337 	}
338 
339 	if (!fw) {
340 		pr_err("Firmware %s not available\n", fwfilepath);
341 		rtStatus = _FAIL;
342 		goto exit;
343 	}
344 
345 	if (fw->size > FW_8723B_SIZE) {
346 		rtStatus = _FAIL;
347 		goto exit;
348 	}
349 
350 	pFirmware->fw_buffer_sz = kmemdup(fw->data, fw->size, GFP_KERNEL);
351 	if (!pFirmware->fw_buffer_sz) {
352 		rtStatus = _FAIL;
353 		goto exit;
354 	}
355 
356 	pFirmware->fw_length = fw->size;
357 	release_firmware(fw);
358 	if (pFirmware->fw_length > FW_8723B_SIZE) {
359 		rtStatus = _FAIL;
360 		netdev_emerg(padapter->pnetdev,
361 			     "Firmware size:%u exceed %u\n",
362 			     pFirmware->fw_length, FW_8723B_SIZE);
363 		goto release_fw1;
364 	}
365 
366 	pFirmwareBuf = pFirmware->fw_buffer_sz;
367 	FirmwareLen = pFirmware->fw_length;
368 
369 	/*  To Check Fw header. Added by tynli. 2009.12.04. */
370 	pFwHdr = (struct rt_firmware_hdr *)pFirmwareBuf;
371 
372 	pHalData->FirmwareVersion =  le16_to_cpu(pFwHdr->version);
373 	pHalData->FirmwareSubVersion = le16_to_cpu(pFwHdr->subversion);
374 	pHalData->FirmwareSignature = le16_to_cpu(pFwHdr->signature);
375 
376 	if (IS_FW_HEADER_EXIST_8723B(pFwHdr)) {
377 		/*  Shift 32 bytes for FW header */
378 		pFirmwareBuf = pFirmwareBuf + 32;
379 		FirmwareLen = FirmwareLen - 32;
380 	}
381 
382 	/*  Suggested by Filen. If 8051 is running in RAM code, driver should inform Fw to reset by itself, */
383 	/*  or it will cause download Fw fail. 2010.02.01. by tynli. */
384 	if (rtw_read8(padapter, REG_MCUFWDL) & RAM_DL_SEL) { /* 8051 RAM code */
385 		rtw_write8(padapter, REG_MCUFWDL, 0x00);
386 		rtl8723b_FirmwareSelfReset(padapter);
387 	}
388 
389 	_FWDownloadEnable(padapter, true);
390 	fwdl_start_time = jiffies;
391 	while (
392 		!padapter->bDriverStopped &&
393 		!padapter->bSurpriseRemoved &&
394 		(write_fw++ < 3 || jiffies_to_msecs(jiffies - fwdl_start_time) < 500)
395 	) {
396 		/* reset FWDL chksum */
397 		rtw_write8(padapter, REG_MCUFWDL, rtw_read8(padapter, REG_MCUFWDL)|FWDL_ChkSum_rpt);
398 
399 		rtStatus = _WriteFW(padapter, pFirmwareBuf, FirmwareLen);
400 		if (rtStatus != _SUCCESS)
401 			continue;
402 
403 		rtStatus = polling_fwdl_chksum(padapter, 5, 50);
404 		if (rtStatus == _SUCCESS)
405 			break;
406 	}
407 	_FWDownloadEnable(padapter, false);
408 	if (_SUCCESS != rtStatus)
409 		goto fwdl_stat;
410 
411 	rtStatus = _FWFreeToGo(padapter, 10, 200);
412 	if (_SUCCESS != rtStatus)
413 		goto fwdl_stat;
414 
415 fwdl_stat:
416 
417 exit:
418 	kfree(pFirmware->fw_buffer_sz);
419 	kfree(pFirmware);
420 release_fw1:
421 	kfree(pBTFirmware);
422 	return rtStatus;
423 }
424 
rtl8723b_InitializeFirmwareVars(struct adapter * padapter)425 void rtl8723b_InitializeFirmwareVars(struct adapter *padapter)
426 {
427 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
428 
429 	/*  Init Fw LPS related. */
430 	adapter_to_pwrctl(padapter)->fw_current_in_ps_mode = false;
431 
432 	/* Init H2C cmd. */
433 	rtw_write8(padapter, REG_HMETFR, 0x0f);
434 
435 	/*  Init H2C counter. by tynli. 2009.12.09. */
436 	pHalData->LastHMEBoxNum = 0;
437 /* pHalData->H2CQueueHead = 0; */
438 /* pHalData->H2CQueueTail = 0; */
439 /* pHalData->H2CStopInsertQueue = false; */
440 }
441 
rtl8723b_free_hal_data(struct adapter * padapter)442 static void rtl8723b_free_hal_data(struct adapter *padapter)
443 {
444 }
445 
446 /*  */
447 /* 				Efuse related code */
448 /*  */
hal_EfuseSwitchToBank(struct adapter * padapter,u8 bank,bool bPseudoTest)449 static u8 hal_EfuseSwitchToBank(
450 	struct adapter *padapter, u8 bank, bool bPseudoTest
451 )
452 {
453 	u8 bRet = false;
454 	u32 value32 = 0;
455 #ifdef HAL_EFUSE_MEMORY
456 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
457 	struct efuse_hal *pEfuseHal = &pHalData->EfuseHal;
458 #endif
459 
460 
461 	if (bPseudoTest) {
462 #ifdef HAL_EFUSE_MEMORY
463 		pEfuseHal->fakeEfuseBank = bank;
464 #else
465 		fakeEfuseBank = bank;
466 #endif
467 		bRet = true;
468 	} else {
469 		value32 = rtw_read32(padapter, EFUSE_TEST);
470 		bRet = true;
471 		switch (bank) {
472 		case 0:
473 			value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_WIFI_SEL_0);
474 			break;
475 		case 1:
476 			value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_BT_SEL_0);
477 			break;
478 		case 2:
479 			value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_BT_SEL_1);
480 			break;
481 		case 3:
482 			value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_BT_SEL_2);
483 			break;
484 		default:
485 			value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_WIFI_SEL_0);
486 			bRet = false;
487 			break;
488 		}
489 		rtw_write32(padapter, EFUSE_TEST, value32);
490 	}
491 
492 	return bRet;
493 }
494 
Hal_GetEfuseDefinition(struct adapter * padapter,u8 efuseType,u8 type,void * pOut,bool bPseudoTest)495 static void Hal_GetEfuseDefinition(
496 	struct adapter *padapter,
497 	u8 efuseType,
498 	u8 type,
499 	void *pOut,
500 	bool bPseudoTest
501 )
502 {
503 	switch (type) {
504 	case TYPE_EFUSE_MAX_SECTION:
505 		{
506 			u8 *pMax_section;
507 			pMax_section = pOut;
508 
509 			if (efuseType == EFUSE_WIFI)
510 				*pMax_section = EFUSE_MAX_SECTION_8723B;
511 			else
512 				*pMax_section = EFUSE_BT_MAX_SECTION;
513 		}
514 		break;
515 
516 	case TYPE_EFUSE_REAL_CONTENT_LEN:
517 		{
518 			u16 *pu2Tmp;
519 			pu2Tmp = pOut;
520 
521 			if (efuseType == EFUSE_WIFI)
522 				*pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723B;
523 			else
524 				*pu2Tmp = EFUSE_BT_REAL_CONTENT_LEN;
525 		}
526 		break;
527 
528 	case TYPE_AVAILABLE_EFUSE_BYTES_BANK:
529 		{
530 			u16 *pu2Tmp;
531 			pu2Tmp = pOut;
532 
533 			if (efuseType == EFUSE_WIFI)
534 				*pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723B-EFUSE_OOB_PROTECT_BYTES);
535 			else
536 				*pu2Tmp = (EFUSE_BT_REAL_BANK_CONTENT_LEN-EFUSE_PROTECT_BYTES_BANK);
537 		}
538 		break;
539 
540 	case TYPE_AVAILABLE_EFUSE_BYTES_TOTAL:
541 		{
542 			u16 *pu2Tmp;
543 			pu2Tmp = pOut;
544 
545 			if (efuseType == EFUSE_WIFI)
546 				*pu2Tmp = (EFUSE_REAL_CONTENT_LEN_8723B-EFUSE_OOB_PROTECT_BYTES);
547 			else
548 				*pu2Tmp = (EFUSE_BT_REAL_CONTENT_LEN-(EFUSE_PROTECT_BYTES_BANK*3));
549 		}
550 		break;
551 
552 	case TYPE_EFUSE_MAP_LEN:
553 		{
554 			u16 *pu2Tmp;
555 			pu2Tmp = pOut;
556 
557 			if (efuseType == EFUSE_WIFI)
558 				*pu2Tmp = EFUSE_MAX_MAP_LEN;
559 			else
560 				*pu2Tmp = EFUSE_BT_MAP_LEN;
561 		}
562 		break;
563 
564 	case TYPE_EFUSE_PROTECT_BYTES_BANK:
565 		{
566 			u8 *pu1Tmp;
567 			pu1Tmp = pOut;
568 
569 			if (efuseType == EFUSE_WIFI)
570 				*pu1Tmp = EFUSE_OOB_PROTECT_BYTES;
571 			else
572 				*pu1Tmp = EFUSE_PROTECT_BYTES_BANK;
573 		}
574 		break;
575 
576 	case TYPE_EFUSE_CONTENT_LEN_BANK:
577 		{
578 			u16 *pu2Tmp;
579 			pu2Tmp = pOut;
580 
581 			if (efuseType == EFUSE_WIFI)
582 				*pu2Tmp = EFUSE_REAL_CONTENT_LEN_8723B;
583 			else
584 				*pu2Tmp = EFUSE_BT_REAL_BANK_CONTENT_LEN;
585 		}
586 		break;
587 
588 	default:
589 		{
590 			u8 *pu1Tmp;
591 			pu1Tmp = pOut;
592 			*pu1Tmp = 0;
593 		}
594 		break;
595 	}
596 }
597 
598 #define VOLTAGE_V25		0x03
599 
600 /*  */
601 /* 	The following is for compile ok */
602 /* 	That should be merged with the original in the future */
603 /*  */
604 #define EFUSE_ACCESS_ON_8723			0x69	/*  For RTL8723 only. */
605 #define REG_EFUSE_ACCESS_8723			0x00CF	/*  Efuse access protection for RTL8723 */
606 
607 /*  */
Hal_BT_EfusePowerSwitch(struct adapter * padapter,u8 bWrite,u8 PwrState)608 static void Hal_BT_EfusePowerSwitch(
609 	struct adapter *padapter, u8 bWrite, u8 PwrState
610 )
611 {
612 	u8 tempval;
613 	if (PwrState) {
614 		/*  enable BT power cut */
615 		/*  0x6A[14] = 1 */
616 		tempval = rtw_read8(padapter, 0x6B);
617 		tempval |= BIT(6);
618 		rtw_write8(padapter, 0x6B, tempval);
619 
620 		/*  Attention!! Between 0x6A[14] and 0x6A[15] setting need 100us delay */
621 		/*  So don't write 0x6A[14]= 1 and 0x6A[15]= 0 together! */
622 		msleep(1);
623 		/*  disable BT output isolation */
624 		/*  0x6A[15] = 0 */
625 		tempval = rtw_read8(padapter, 0x6B);
626 		tempval &= ~BIT(7);
627 		rtw_write8(padapter, 0x6B, tempval);
628 	} else {
629 		/*  enable BT output isolation */
630 		/*  0x6A[15] = 1 */
631 		tempval = rtw_read8(padapter, 0x6B);
632 		tempval |= BIT(7);
633 		rtw_write8(padapter, 0x6B, tempval);
634 
635 		/*  Attention!! Between 0x6A[14] and 0x6A[15] setting need 100us delay */
636 		/*  So don't write 0x6A[14]= 1 and 0x6A[15]= 0 together! */
637 
638 		/*  disable BT power cut */
639 		/*  0x6A[14] = 1 */
640 		tempval = rtw_read8(padapter, 0x6B);
641 		tempval &= ~BIT(6);
642 		rtw_write8(padapter, 0x6B, tempval);
643 	}
644 
645 }
Hal_EfusePowerSwitch(struct adapter * padapter,u8 bWrite,u8 PwrState)646 static void Hal_EfusePowerSwitch(
647 	struct adapter *padapter, u8 bWrite, u8 PwrState
648 )
649 {
650 	u8 tempval;
651 	u16 tmpV16;
652 
653 
654 	if (PwrState) {
655 		/*  To avoid cannot access efuse registers after disable/enable several times during DTM test. */
656 		/*  Suggested by SD1 IsaacHsu. 2013.07.08, added by tynli. */
657 		tempval = rtw_read8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HSUS_CTRL);
658 		if (tempval & BIT(0)) { /*  SDIO local register is suspend */
659 			u8 count = 0;
660 
661 
662 			tempval &= ~BIT(0);
663 			rtw_write8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HSUS_CTRL, tempval);
664 
665 			/*  check 0x86[1:0]= 10'2h, wait power state to leave suspend */
666 			do {
667 				tempval = rtw_read8(padapter, SDIO_LOCAL_BASE|SDIO_REG_HSUS_CTRL);
668 				tempval &= 0x3;
669 				if (tempval == 0x02)
670 					break;
671 
672 				count++;
673 				if (count >= 100)
674 					break;
675 
676 				mdelay(10);
677 			} while (1);
678 		}
679 
680 		rtw_write8(padapter, REG_EFUSE_ACCESS_8723, EFUSE_ACCESS_ON_8723);
681 
682 		/*  Reset: 0x0000h[28], default valid */
683 		tmpV16 =  rtw_read16(padapter, REG_SYS_FUNC_EN);
684 		if (!(tmpV16 & FEN_ELDR)) {
685 			tmpV16 |= FEN_ELDR;
686 			rtw_write16(padapter, REG_SYS_FUNC_EN, tmpV16);
687 		}
688 
689 		/*  Clock: Gated(0x0008h[5]) 8M(0x0008h[1]) clock from ANA, default valid */
690 		tmpV16 = rtw_read16(padapter, REG_SYS_CLKR);
691 		if ((!(tmpV16 & LOADER_CLK_EN))  || (!(tmpV16 & ANA8M))) {
692 			tmpV16 |= (LOADER_CLK_EN | ANA8M);
693 			rtw_write16(padapter, REG_SYS_CLKR, tmpV16);
694 		}
695 
696 		if (bWrite) {
697 			/*  Enable LDO 2.5V before read/write action */
698 			tempval = rtw_read8(padapter, EFUSE_TEST+3);
699 			tempval &= 0x0F;
700 			tempval |= (VOLTAGE_V25 << 4);
701 			rtw_write8(padapter, EFUSE_TEST+3, (tempval | 0x80));
702 
703 			/* rtw_write8(padapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_ON); */
704 		}
705 	} else {
706 		rtw_write8(padapter, REG_EFUSE_ACCESS, EFUSE_ACCESS_OFF);
707 
708 		if (bWrite) {
709 			/*  Disable LDO 2.5V after read/write action */
710 			tempval = rtw_read8(padapter, EFUSE_TEST+3);
711 			rtw_write8(padapter, EFUSE_TEST+3, (tempval & 0x7F));
712 		}
713 
714 	}
715 }
716 
hal_ReadEFuse_WiFi(struct adapter * padapter,u16 _offset,u16 _size_byte,u8 * pbuf,bool bPseudoTest)717 static void hal_ReadEFuse_WiFi(
718 	struct adapter *padapter,
719 	u16 _offset,
720 	u16 _size_byte,
721 	u8 *pbuf,
722 	bool bPseudoTest
723 )
724 {
725 #ifdef HAL_EFUSE_MEMORY
726 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
727 	struct efuse_hal *pEfuseHal = &pHalData->EfuseHal;
728 #endif
729 	u8 *efuseTbl = NULL;
730 	u16 eFuse_Addr = 0;
731 	u8 offset, wden;
732 	u8 efuseHeader, efuseExtHdr, efuseData;
733 	u16 i, total, used;
734 	u8 efuse_usage = 0;
735 
736 	/*  */
737 	/*  Do NOT excess total size of EFuse table. Added by Roger, 2008.11.10. */
738 	/*  */
739 	if ((_offset + _size_byte) > EFUSE_MAX_MAP_LEN)
740 		return;
741 
742 	efuseTbl = rtw_malloc(EFUSE_MAX_MAP_LEN);
743 	if (!efuseTbl)
744 		return;
745 
746 	/*  0xff will be efuse default value instead of 0x00. */
747 	memset(efuseTbl, 0xFF, EFUSE_MAX_MAP_LEN);
748 
749 	/*  switch bank back to bank 0 for later BT and wifi use. */
750 	hal_EfuseSwitchToBank(padapter, 0, bPseudoTest);
751 
752 	while (AVAILABLE_EFUSE_ADDR(eFuse_Addr)) {
753 		efuse_OneByteRead(padapter, eFuse_Addr++, &efuseHeader, bPseudoTest);
754 		if (efuseHeader == 0xFF)
755 			break;
756 
757 		/*  Check PG header for section num. */
758 		if (EXT_HEADER(efuseHeader)) { /* extended header */
759 			offset = GET_HDR_OFFSET_2_0(efuseHeader);
760 
761 			efuse_OneByteRead(padapter, eFuse_Addr++, &efuseExtHdr, bPseudoTest);
762 			if (ALL_WORDS_DISABLED(efuseExtHdr))
763 				continue;
764 
765 			offset |= ((efuseExtHdr & 0xF0) >> 1);
766 			wden = (efuseExtHdr & 0x0F);
767 		} else {
768 			offset = ((efuseHeader >> 4) & 0x0f);
769 			wden = (efuseHeader & 0x0f);
770 		}
771 
772 		if (offset < EFUSE_MAX_SECTION_8723B) {
773 			u16 addr;
774 			/*  Get word enable value from PG header */
775 
776 			addr = offset * PGPKT_DATA_SIZE;
777 			for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
778 				/*  Check word enable condition in the section */
779 				if (!(wden & (0x01<<i))) {
780 					efuse_OneByteRead(padapter, eFuse_Addr++, &efuseData, bPseudoTest);
781 					efuseTbl[addr] = efuseData;
782 
783 					efuse_OneByteRead(padapter, eFuse_Addr++, &efuseData, bPseudoTest);
784 					efuseTbl[addr+1] = efuseData;
785 				}
786 				addr += 2;
787 			}
788 		} else {
789 			eFuse_Addr += Efuse_CalculateWordCnts(wden)*2;
790 		}
791 	}
792 
793 	/*  Copy from Efuse map to output pointer memory!!! */
794 	for (i = 0; i < _size_byte; i++)
795 		pbuf[i] = efuseTbl[_offset+i];
796 
797 	/*  Calculate Efuse utilization */
798 	EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, &total, bPseudoTest);
799 	used = eFuse_Addr - 1;
800 	efuse_usage = (u8)((used*100)/total);
801 	if (bPseudoTest) {
802 #ifdef HAL_EFUSE_MEMORY
803 		pEfuseHal->fakeEfuseUsedBytes = used;
804 #else
805 		fakeEfuseUsedBytes = used;
806 #endif
807 	} else {
808 		rtw_hal_set_hwreg(padapter, HW_VAR_EFUSE_BYTES, (u8 *)&used);
809 		rtw_hal_set_hwreg(padapter, HW_VAR_EFUSE_USAGE, (u8 *)&efuse_usage);
810 	}
811 
812 	kfree(efuseTbl);
813 }
814 
hal_ReadEFuse_BT(struct adapter * padapter,u16 _offset,u16 _size_byte,u8 * pbuf,bool bPseudoTest)815 static void hal_ReadEFuse_BT(
816 	struct adapter *padapter,
817 	u16 _offset,
818 	u16 _size_byte,
819 	u8 *pbuf,
820 	bool bPseudoTest
821 )
822 {
823 #ifdef HAL_EFUSE_MEMORY
824 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
825 	struct efuse_hal *pEfuseHal = &pHalData->EfuseHal;
826 #endif
827 	u8 *efuseTbl;
828 	u8 bank;
829 	u16 eFuse_Addr;
830 	u8 efuseHeader, efuseExtHdr, efuseData;
831 	u8 offset, wden;
832 	u16 i, total, used;
833 	u8 efuse_usage;
834 
835 
836 	/*  */
837 	/*  Do NOT excess total size of EFuse table. Added by Roger, 2008.11.10. */
838 	/*  */
839 	if ((_offset + _size_byte) > EFUSE_BT_MAP_LEN)
840 		return;
841 
842 	efuseTbl = rtw_malloc(EFUSE_BT_MAP_LEN);
843 	if (!efuseTbl)
844 		return;
845 
846 	/*  0xff will be efuse default value instead of 0x00. */
847 	memset(efuseTbl, 0xFF, EFUSE_BT_MAP_LEN);
848 
849 	EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_AVAILABLE_EFUSE_BYTES_BANK, &total, bPseudoTest);
850 
851 	for (bank = 1; bank < 3; bank++) { /*  8723b Max bake 0~2 */
852 		if (hal_EfuseSwitchToBank(padapter, bank, bPseudoTest) == false)
853 			goto exit;
854 
855 		eFuse_Addr = 0;
856 
857 		while (AVAILABLE_EFUSE_ADDR(eFuse_Addr)) {
858 			efuse_OneByteRead(padapter, eFuse_Addr++, &efuseHeader, bPseudoTest);
859 			if (efuseHeader == 0xFF)
860 				break;
861 
862 			/*  Check PG header for section num. */
863 			if (EXT_HEADER(efuseHeader)) { /* extended header */
864 				offset = GET_HDR_OFFSET_2_0(efuseHeader);
865 
866 				efuse_OneByteRead(padapter, eFuse_Addr++, &efuseExtHdr, bPseudoTest);
867 				if (ALL_WORDS_DISABLED(efuseExtHdr))
868 					continue;
869 
870 
871 				offset |= ((efuseExtHdr & 0xF0) >> 1);
872 				wden = (efuseExtHdr & 0x0F);
873 			} else {
874 				offset = ((efuseHeader >> 4) & 0x0f);
875 				wden = (efuseHeader & 0x0f);
876 			}
877 
878 			if (offset < EFUSE_BT_MAX_SECTION) {
879 				u16 addr;
880 
881 				addr = offset * PGPKT_DATA_SIZE;
882 				for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
883 					/*  Check word enable condition in the section */
884 					if (!(wden & (0x01<<i))) {
885 						efuse_OneByteRead(padapter, eFuse_Addr++, &efuseData, bPseudoTest);
886 						efuseTbl[addr] = efuseData;
887 
888 						efuse_OneByteRead(padapter, eFuse_Addr++, &efuseData, bPseudoTest);
889 						efuseTbl[addr+1] = efuseData;
890 					}
891 					addr += 2;
892 				}
893 			} else {
894 				eFuse_Addr += Efuse_CalculateWordCnts(wden)*2;
895 			}
896 		}
897 
898 		if ((eFuse_Addr - 1) < total)
899 			break;
900 
901 	}
902 
903 	/*  switch bank back to bank 0 for later BT and wifi use. */
904 	hal_EfuseSwitchToBank(padapter, 0, bPseudoTest);
905 
906 	/*  Copy from Efuse map to output pointer memory!!! */
907 	for (i = 0; i < _size_byte; i++)
908 		pbuf[i] = efuseTbl[_offset+i];
909 
910 	/*  */
911 	/*  Calculate Efuse utilization. */
912 	/*  */
913 	EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, &total, bPseudoTest);
914 	used = (EFUSE_BT_REAL_BANK_CONTENT_LEN*(bank-1)) + eFuse_Addr - 1;
915 	efuse_usage = (u8)((used*100)/total);
916 	if (bPseudoTest) {
917 #ifdef HAL_EFUSE_MEMORY
918 		pEfuseHal->fakeBTEfuseUsedBytes = used;
919 #else
920 		fakeBTEfuseUsedBytes = used;
921 #endif
922 	} else {
923 		rtw_hal_set_hwreg(padapter, HW_VAR_EFUSE_BT_BYTES, (u8 *)&used);
924 		rtw_hal_set_hwreg(padapter, HW_VAR_EFUSE_BT_USAGE, (u8 *)&efuse_usage);
925 	}
926 
927 exit:
928 	kfree(efuseTbl);
929 }
930 
Hal_ReadEFuse(struct adapter * padapter,u8 efuseType,u16 _offset,u16 _size_byte,u8 * pbuf,bool bPseudoTest)931 static void Hal_ReadEFuse(
932 	struct adapter *padapter,
933 	u8 efuseType,
934 	u16 _offset,
935 	u16 _size_byte,
936 	u8 *pbuf,
937 	bool bPseudoTest
938 )
939 {
940 	if (efuseType == EFUSE_WIFI)
941 		hal_ReadEFuse_WiFi(padapter, _offset, _size_byte, pbuf, bPseudoTest);
942 	else
943 		hal_ReadEFuse_BT(padapter, _offset, _size_byte, pbuf, bPseudoTest);
944 }
945 
hal_EfuseGetCurrentSize_WiFi(struct adapter * padapter,bool bPseudoTest)946 static u16 hal_EfuseGetCurrentSize_WiFi(
947 	struct adapter *padapter, bool bPseudoTest
948 )
949 {
950 #ifdef HAL_EFUSE_MEMORY
951 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
952 	struct efuse_hal *pEfuseHal = &pHalData->EfuseHal;
953 #endif
954 	u16 efuse_addr = 0;
955 	u16 start_addr = 0; /*  for debug */
956 	u8 hworden = 0;
957 	u8 efuse_data, word_cnts = 0;
958 	u32 count = 0; /*  for debug */
959 
960 
961 	if (bPseudoTest) {
962 #ifdef HAL_EFUSE_MEMORY
963 		efuse_addr = (u16)pEfuseHal->fakeEfuseUsedBytes;
964 #else
965 		efuse_addr = (u16)fakeEfuseUsedBytes;
966 #endif
967 	} else
968 		rtw_hal_get_hwreg(padapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
969 
970 	start_addr = efuse_addr;
971 
972 	/*  switch bank back to bank 0 for later BT and wifi use. */
973 	hal_EfuseSwitchToBank(padapter, 0, bPseudoTest);
974 
975 	count = 0;
976 	while (AVAILABLE_EFUSE_ADDR(efuse_addr)) {
977 		if (efuse_OneByteRead(padapter, efuse_addr, &efuse_data, bPseudoTest) == false)
978 			goto error;
979 
980 		if (efuse_data == 0xFF)
981 			break;
982 
983 		if ((start_addr != 0) && (efuse_addr == start_addr)) {
984 			count++;
985 
986 			efuse_data = 0xFF;
987 			if (count < 4) {
988 				/*  try again! */
989 
990 				if (count > 2) {
991 					/*  try again form address 0 */
992 					efuse_addr = 0;
993 					start_addr = 0;
994 				}
995 
996 				continue;
997 			}
998 
999 			goto error;
1000 		}
1001 
1002 		if (EXT_HEADER(efuse_data)) {
1003 			efuse_addr++;
1004 			efuse_OneByteRead(padapter, efuse_addr, &efuse_data, bPseudoTest);
1005 			if (ALL_WORDS_DISABLED(efuse_data))
1006 				continue;
1007 
1008 			hworden = efuse_data & 0x0F;
1009 		} else {
1010 			hworden = efuse_data & 0x0F;
1011 		}
1012 
1013 		word_cnts = Efuse_CalculateWordCnts(hworden);
1014 		efuse_addr += (word_cnts*2)+1;
1015 	}
1016 
1017 	if (bPseudoTest) {
1018 #ifdef HAL_EFUSE_MEMORY
1019 		pEfuseHal->fakeEfuseUsedBytes = efuse_addr;
1020 #else
1021 		fakeEfuseUsedBytes = efuse_addr;
1022 #endif
1023 	} else
1024 		rtw_hal_set_hwreg(padapter, HW_VAR_EFUSE_BYTES, (u8 *)&efuse_addr);
1025 
1026 	goto exit;
1027 
1028 error:
1029 	/*  report max size to prevent write efuse */
1030 	EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, &efuse_addr, bPseudoTest);
1031 
1032 exit:
1033 
1034 	return efuse_addr;
1035 }
1036 
hal_EfuseGetCurrentSize_BT(struct adapter * padapter,u8 bPseudoTest)1037 static u16 hal_EfuseGetCurrentSize_BT(struct adapter *padapter, u8 bPseudoTest)
1038 {
1039 #ifdef HAL_EFUSE_MEMORY
1040 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
1041 	struct efuse_hal *pEfuseHal = &pHalData->EfuseHal;
1042 #endif
1043 	u16 btusedbytes;
1044 	u16 efuse_addr;
1045 	u8 bank, startBank;
1046 	u8 hworden = 0;
1047 	u8 efuse_data, word_cnts = 0;
1048 	u16 retU2 = 0;
1049 
1050 	if (bPseudoTest) {
1051 #ifdef HAL_EFUSE_MEMORY
1052 		btusedbytes = pEfuseHal->fakeBTEfuseUsedBytes;
1053 #else
1054 		btusedbytes = fakeBTEfuseUsedBytes;
1055 #endif
1056 	} else
1057 		rtw_hal_get_hwreg(padapter, HW_VAR_EFUSE_BT_BYTES, (u8 *)&btusedbytes);
1058 
1059 	efuse_addr = (u16)((btusedbytes%EFUSE_BT_REAL_BANK_CONTENT_LEN));
1060 	startBank = (u8)(1+(btusedbytes/EFUSE_BT_REAL_BANK_CONTENT_LEN));
1061 
1062 	EFUSE_GetEfuseDefinition(padapter, EFUSE_BT, TYPE_AVAILABLE_EFUSE_BYTES_BANK, &retU2, bPseudoTest);
1063 
1064 	for (bank = startBank; bank < 3; bank++) {
1065 		if (hal_EfuseSwitchToBank(padapter, bank, bPseudoTest) == false)
1066 			/* bank = EFUSE_MAX_BANK; */
1067 			break;
1068 
1069 		/*  only when bank is switched we have to reset the efuse_addr. */
1070 		if (bank != startBank)
1071 			efuse_addr = 0;
1072 #if 1
1073 
1074 		while (AVAILABLE_EFUSE_ADDR(efuse_addr)) {
1075 			if (efuse_OneByteRead(padapter, efuse_addr,
1076 					      &efuse_data, bPseudoTest) == false)
1077 				/* bank = EFUSE_MAX_BANK; */
1078 				break;
1079 
1080 			if (efuse_data == 0xFF)
1081 				break;
1082 
1083 			if (EXT_HEADER(efuse_data)) {
1084 				efuse_addr++;
1085 				efuse_OneByteRead(padapter, efuse_addr, &efuse_data, bPseudoTest);
1086 
1087 				if (ALL_WORDS_DISABLED(efuse_data)) {
1088 					efuse_addr++;
1089 					continue;
1090 				}
1091 
1092 				hworden = efuse_data & 0x0F;
1093 			} else {
1094 				hworden =  efuse_data & 0x0F;
1095 			}
1096 
1097 			word_cnts = Efuse_CalculateWordCnts(hworden);
1098 			/* read next header */
1099 			efuse_addr += (word_cnts*2)+1;
1100 		}
1101 #else
1102 	while (
1103 		bContinual &&
1104 		efuse_OneByteRead(padapter, efuse_addr, &efuse_data, bPseudoTest) &&
1105 		AVAILABLE_EFUSE_ADDR(efuse_addr)
1106 	) {
1107 			if (efuse_data != 0xFF) {
1108 				if ((efuse_data&0x1F) == 0x0F) { /* extended header */
1109 					efuse_addr++;
1110 					efuse_OneByteRead(padapter, efuse_addr, &efuse_data, bPseudoTest);
1111 					if ((efuse_data & 0x0F) == 0x0F) {
1112 						efuse_addr++;
1113 						continue;
1114 					} else {
1115 						hworden = efuse_data & 0x0F;
1116 					}
1117 				} else {
1118 					hworden =  efuse_data & 0x0F;
1119 				}
1120 				word_cnts = Efuse_CalculateWordCnts(hworden);
1121 				/* read next header */
1122 				efuse_addr = efuse_addr + (word_cnts*2)+1;
1123 			} else
1124 				bContinual = false;
1125 		}
1126 #endif
1127 
1128 
1129 		/*  Check if we need to check next bank efuse */
1130 		if (efuse_addr < retU2)
1131 			break; /*  don't need to check next bank. */
1132 	}
1133 
1134 	retU2 = ((bank-1)*EFUSE_BT_REAL_BANK_CONTENT_LEN)+efuse_addr;
1135 	if (bPseudoTest) {
1136 		pEfuseHal->fakeBTEfuseUsedBytes = retU2;
1137 	} else {
1138 		pEfuseHal->BTEfuseUsedBytes = retU2;
1139 	}
1140 
1141 	return retU2;
1142 }
1143 
Hal_EfuseGetCurrentSize(struct adapter * padapter,u8 efuseType,bool bPseudoTest)1144 static u16 Hal_EfuseGetCurrentSize(
1145 	struct adapter *padapter, u8 efuseType, bool bPseudoTest
1146 )
1147 {
1148 	u16 ret = 0;
1149 
1150 	if (efuseType == EFUSE_WIFI)
1151 		ret = hal_EfuseGetCurrentSize_WiFi(padapter, bPseudoTest);
1152 	else
1153 		ret = hal_EfuseGetCurrentSize_BT(padapter, bPseudoTest);
1154 
1155 	return ret;
1156 }
1157 
Hal_EfuseWordEnableDataWrite(struct adapter * padapter,u16 efuse_addr,u8 word_en,u8 * data,bool bPseudoTest)1158 static u8 Hal_EfuseWordEnableDataWrite(
1159 	struct adapter *padapter,
1160 	u16 efuse_addr,
1161 	u8 word_en,
1162 	u8 *data,
1163 	bool bPseudoTest
1164 )
1165 {
1166 	u16 tmpaddr = 0;
1167 	u16 start_addr = efuse_addr;
1168 	u8 badworden = 0x0F;
1169 	u8 tmpdata[PGPKT_DATA_SIZE];
1170 
1171 	memset(tmpdata, 0xFF, PGPKT_DATA_SIZE);
1172 
1173 	if (!(word_en & BIT(0))) {
1174 		tmpaddr = start_addr;
1175 		efuse_OneByteWrite(padapter, start_addr++, data[0], bPseudoTest);
1176 		efuse_OneByteWrite(padapter, start_addr++, data[1], bPseudoTest);
1177 
1178 		efuse_OneByteRead(padapter, tmpaddr, &tmpdata[0], bPseudoTest);
1179 		efuse_OneByteRead(padapter, tmpaddr+1, &tmpdata[1], bPseudoTest);
1180 		if ((data[0] != tmpdata[0]) || (data[1] != tmpdata[1])) {
1181 			badworden &= (~BIT(0));
1182 		}
1183 	}
1184 	if (!(word_en & BIT(1))) {
1185 		tmpaddr = start_addr;
1186 		efuse_OneByteWrite(padapter, start_addr++, data[2], bPseudoTest);
1187 		efuse_OneByteWrite(padapter, start_addr++, data[3], bPseudoTest);
1188 
1189 		efuse_OneByteRead(padapter, tmpaddr, &tmpdata[2], bPseudoTest);
1190 		efuse_OneByteRead(padapter, tmpaddr+1, &tmpdata[3], bPseudoTest);
1191 		if ((data[2] != tmpdata[2]) || (data[3] != tmpdata[3])) {
1192 			badworden &= (~BIT(1));
1193 		}
1194 	}
1195 
1196 	if (!(word_en & BIT(2))) {
1197 		tmpaddr = start_addr;
1198 		efuse_OneByteWrite(padapter, start_addr++, data[4], bPseudoTest);
1199 		efuse_OneByteWrite(padapter, start_addr++, data[5], bPseudoTest);
1200 
1201 		efuse_OneByteRead(padapter, tmpaddr, &tmpdata[4], bPseudoTest);
1202 		efuse_OneByteRead(padapter, tmpaddr+1, &tmpdata[5], bPseudoTest);
1203 		if ((data[4] != tmpdata[4]) || (data[5] != tmpdata[5])) {
1204 			badworden &= (~BIT(2));
1205 		}
1206 	}
1207 
1208 	if (!(word_en & BIT(3))) {
1209 		tmpaddr = start_addr;
1210 		efuse_OneByteWrite(padapter, start_addr++, data[6], bPseudoTest);
1211 		efuse_OneByteWrite(padapter, start_addr++, data[7], bPseudoTest);
1212 
1213 		efuse_OneByteRead(padapter, tmpaddr, &tmpdata[6], bPseudoTest);
1214 		efuse_OneByteRead(padapter, tmpaddr+1, &tmpdata[7], bPseudoTest);
1215 		if ((data[6] != tmpdata[6]) || (data[7] != tmpdata[7])) {
1216 			badworden &= (~BIT(3));
1217 		}
1218 	}
1219 
1220 	return badworden;
1221 }
1222 
Hal_EfusePgPacketRead(struct adapter * padapter,u8 offset,u8 * data,bool bPseudoTest)1223 static s32 Hal_EfusePgPacketRead(
1224 	struct adapter *padapter,
1225 	u8 offset,
1226 	u8 *data,
1227 	bool bPseudoTest
1228 )
1229 {
1230 	u8 efuse_data, word_cnts = 0;
1231 	u16 efuse_addr = 0;
1232 	u8 hoffset = 0, hworden = 0;
1233 	u8 i;
1234 	u8 max_section = 0;
1235 	s32	ret;
1236 
1237 
1238 	if (!data)
1239 		return false;
1240 
1241 	EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAX_SECTION, &max_section, bPseudoTest);
1242 	if (offset > max_section)
1243 		return false;
1244 
1245 	memset(data, 0xFF, PGPKT_DATA_SIZE);
1246 	ret = true;
1247 
1248 	/*  */
1249 	/*  <Roger_TODO> Efuse has been pre-programmed dummy 5Bytes at the end of Efuse by CP. */
1250 	/*  Skip dummy parts to prevent unexpected data read from Efuse. */
1251 	/*  By pass right now. 2009.02.19. */
1252 	/*  */
1253 	while (AVAILABLE_EFUSE_ADDR(efuse_addr)) {
1254 		if (efuse_OneByteRead(padapter, efuse_addr++, &efuse_data, bPseudoTest) == false) {
1255 			ret = false;
1256 			break;
1257 		}
1258 
1259 		if (efuse_data == 0xFF)
1260 			break;
1261 
1262 		if (EXT_HEADER(efuse_data)) {
1263 			hoffset = GET_HDR_OFFSET_2_0(efuse_data);
1264 			efuse_OneByteRead(padapter, efuse_addr++, &efuse_data, bPseudoTest);
1265 			if (ALL_WORDS_DISABLED(efuse_data))
1266 				continue;
1267 
1268 			hoffset |= ((efuse_data & 0xF0) >> 1);
1269 			hworden = efuse_data & 0x0F;
1270 		} else {
1271 			hoffset = (efuse_data>>4) & 0x0F;
1272 			hworden =  efuse_data & 0x0F;
1273 		}
1274 
1275 		if (hoffset == offset) {
1276 			for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
1277 				/*  Check word enable condition in the section */
1278 				if (!(hworden & (0x01<<i))) {
1279 					efuse_OneByteRead(padapter, efuse_addr++, &efuse_data, bPseudoTest);
1280 					data[i*2] = efuse_data;
1281 
1282 					efuse_OneByteRead(padapter, efuse_addr++, &efuse_data, bPseudoTest);
1283 					data[(i*2)+1] = efuse_data;
1284 				}
1285 			}
1286 		} else {
1287 			word_cnts = Efuse_CalculateWordCnts(hworden);
1288 			efuse_addr += word_cnts*2;
1289 		}
1290 	}
1291 
1292 	return ret;
1293 }
1294 
hal_EfusePgCheckAvailableAddr(struct adapter * padapter,u8 efuseType,u8 bPseudoTest)1295 static u8 hal_EfusePgCheckAvailableAddr(
1296 	struct adapter *padapter, u8 efuseType, u8 bPseudoTest
1297 )
1298 {
1299 	u16 max_available = 0;
1300 	u16 current_size;
1301 
1302 
1303 	EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, &max_available, bPseudoTest);
1304 
1305 	current_size = Efuse_GetCurrentSize(padapter, efuseType, bPseudoTest);
1306 	if (current_size >= max_available)
1307 		return false;
1308 
1309 	return true;
1310 }
1311 
hal_EfuseConstructPGPkt(u8 offset,u8 word_en,u8 * pData,struct pgpkt_struct * pTargetPkt)1312 static void hal_EfuseConstructPGPkt(
1313 	u8 offset,
1314 	u8 word_en,
1315 	u8 *pData,
1316 	struct pgpkt_struct *pTargetPkt
1317 )
1318 {
1319 	memset(pTargetPkt->data, 0xFF, PGPKT_DATA_SIZE);
1320 	pTargetPkt->offset = offset;
1321 	pTargetPkt->word_en = word_en;
1322 	efuse_WordEnableDataRead(word_en, pData, pTargetPkt->data);
1323 	pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
1324 }
1325 
hal_EfusePartialWriteCheck(struct adapter * padapter,u8 efuseType,u16 * pAddr,struct pgpkt_struct * pTargetPkt,u8 bPseudoTest)1326 static u8 hal_EfusePartialWriteCheck(
1327 	struct adapter *padapter,
1328 	u8 efuseType,
1329 	u16 *pAddr,
1330 	struct pgpkt_struct *pTargetPkt,
1331 	u8 bPseudoTest
1332 )
1333 {
1334 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
1335 	struct efuse_hal *pEfuseHal = &pHalData->EfuseHal;
1336 	u8 bRet = false;
1337 	u16 startAddr = 0, efuse_max_available_len = 0, efuse_max = 0;
1338 	u8 efuse_data = 0;
1339 
1340 	EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_AVAILABLE_EFUSE_BYTES_TOTAL, &efuse_max_available_len, bPseudoTest);
1341 	EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_EFUSE_CONTENT_LEN_BANK, &efuse_max, bPseudoTest);
1342 
1343 	if (efuseType == EFUSE_WIFI) {
1344 		if (bPseudoTest) {
1345 #ifdef HAL_EFUSE_MEMORY
1346 			startAddr = (u16)pEfuseHal->fakeEfuseUsedBytes;
1347 #else
1348 			startAddr = (u16)fakeEfuseUsedBytes;
1349 #endif
1350 		} else
1351 			rtw_hal_get_hwreg(padapter, HW_VAR_EFUSE_BYTES, (u8 *)&startAddr);
1352 	} else {
1353 		if (bPseudoTest) {
1354 #ifdef HAL_EFUSE_MEMORY
1355 			startAddr = (u16)pEfuseHal->fakeBTEfuseUsedBytes;
1356 #else
1357 			startAddr = (u16)fakeBTEfuseUsedBytes;
1358 #endif
1359 		} else
1360 			rtw_hal_get_hwreg(padapter, HW_VAR_EFUSE_BT_BYTES, (u8 *)&startAddr);
1361 	}
1362 	startAddr %= efuse_max;
1363 
1364 	while (1) {
1365 		if (startAddr >= efuse_max_available_len) {
1366 			bRet = false;
1367 			break;
1368 		}
1369 
1370 		if (efuse_OneByteRead(padapter, startAddr, &efuse_data, bPseudoTest) && (efuse_data != 0xFF)) {
1371 #if 1
1372 			bRet = false;
1373 			break;
1374 #else
1375 			if (EXT_HEADER(efuse_data)) {
1376 				cur_header = efuse_data;
1377 				startAddr++;
1378 				efuse_OneByteRead(padapter, startAddr, &efuse_data, bPseudoTest);
1379 				if (ALL_WORDS_DISABLED(efuse_data)) {
1380 					bRet = false;
1381 					break;
1382 				} else {
1383 					curPkt.offset = ((cur_header & 0xE0) >> 5) | ((efuse_data & 0xF0) >> 1);
1384 					curPkt.word_en = efuse_data & 0x0F;
1385 				}
1386 			} else {
1387 				cur_header  =  efuse_data;
1388 				curPkt.offset = (cur_header>>4) & 0x0F;
1389 				curPkt.word_en = cur_header & 0x0F;
1390 			}
1391 
1392 			curPkt.word_cnts = Efuse_CalculateWordCnts(curPkt.word_en);
1393 			/*  if same header is found but no data followed */
1394 			/*  write some part of data followed by the header. */
1395 			if (
1396 				(curPkt.offset == pTargetPkt->offset) &&
1397 				(hal_EfuseCheckIfDatafollowed(padapter, curPkt.word_cnts, startAddr+1, bPseudoTest) == false) &&
1398 				wordEnMatched(pTargetPkt, &curPkt, &matched_wden) == true
1399 			) {
1400 				/*  Here to write partial data */
1401 				badworden = Efuse_WordEnableDataWrite(padapter, startAddr+1, matched_wden, pTargetPkt->data, bPseudoTest);
1402 				if (badworden != 0x0F) {
1403 					u32 PgWriteSuccess = 0;
1404 					/*  if write fail on some words, write these bad words again */
1405 					if (efuseType == EFUSE_WIFI)
1406 						PgWriteSuccess = Efuse_PgPacketWrite(padapter, pTargetPkt->offset, badworden, pTargetPkt->data, bPseudoTest);
1407 					else
1408 						PgWriteSuccess = Efuse_PgPacketWrite_BT(padapter, pTargetPkt->offset, badworden, pTargetPkt->data, bPseudoTest);
1409 
1410 					if (!PgWriteSuccess) {
1411 						bRet = false;	/*  write fail, return */
1412 						break;
1413 					}
1414 				}
1415 				/*  partial write ok, update the target packet for later use */
1416 				for (i = 0; i < 4; i++) {
1417 					if ((matched_wden & (0x1<<i)) == 0) { /*  this word has been written */
1418 						pTargetPkt->word_en |= (0x1<<i);	/*  disable the word */
1419 					}
1420 				}
1421 				pTargetPkt->word_cnts = Efuse_CalculateWordCnts(pTargetPkt->word_en);
1422 			}
1423 			/*  read from next header */
1424 			startAddr = startAddr + (curPkt.word_cnts*2) + 1;
1425 #endif
1426 		} else {
1427 			/*  not used header, 0xff */
1428 			*pAddr = startAddr;
1429 			bRet = true;
1430 			break;
1431 		}
1432 	}
1433 
1434 	return bRet;
1435 }
1436 
hal_EfusePgPacketWrite1ByteHeader(struct adapter * padapter,u8 efuseType,u16 * pAddr,struct pgpkt_struct * pTargetPkt,u8 bPseudoTest)1437 static u8 hal_EfusePgPacketWrite1ByteHeader(
1438 	struct adapter *padapter,
1439 	u8 efuseType,
1440 	u16 *pAddr,
1441 	struct pgpkt_struct *pTargetPkt,
1442 	u8 bPseudoTest
1443 )
1444 {
1445 	u8 pg_header = 0, tmp_header = 0;
1446 	u16 efuse_addr = *pAddr;
1447 	u8 repeatcnt = 0;
1448 
1449 	pg_header = ((pTargetPkt->offset << 4) & 0xf0) | pTargetPkt->word_en;
1450 
1451 	do {
1452 		efuse_OneByteWrite(padapter, efuse_addr, pg_header, bPseudoTest);
1453 		efuse_OneByteRead(padapter, efuse_addr, &tmp_header, bPseudoTest);
1454 		if (tmp_header != 0xFF)
1455 			break;
1456 		if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
1457 			return false;
1458 
1459 	} while (1);
1460 
1461 	if (tmp_header != pg_header)
1462 		return false;
1463 
1464 	*pAddr = efuse_addr;
1465 
1466 	return true;
1467 }
1468 
hal_EfusePgPacketWrite2ByteHeader(struct adapter * padapter,u8 efuseType,u16 * pAddr,struct pgpkt_struct * pTargetPkt,u8 bPseudoTest)1469 static u8 hal_EfusePgPacketWrite2ByteHeader(
1470 	struct adapter *padapter,
1471 	u8 efuseType,
1472 	u16 *pAddr,
1473 	struct pgpkt_struct *pTargetPkt,
1474 	u8 bPseudoTest
1475 )
1476 {
1477 	u16 efuse_addr, efuse_max_available_len = 0;
1478 	u8 pg_header = 0, tmp_header = 0;
1479 	u8 repeatcnt = 0;
1480 
1481 	EFUSE_GetEfuseDefinition(padapter, efuseType, TYPE_AVAILABLE_EFUSE_BYTES_BANK, &efuse_max_available_len, bPseudoTest);
1482 
1483 	efuse_addr = *pAddr;
1484 	if (efuse_addr >= efuse_max_available_len)
1485 		return false;
1486 
1487 	pg_header = ((pTargetPkt->offset & 0x07) << 5) | 0x0F;
1488 
1489 	do {
1490 		efuse_OneByteWrite(padapter, efuse_addr, pg_header, bPseudoTest);
1491 		efuse_OneByteRead(padapter, efuse_addr, &tmp_header, bPseudoTest);
1492 		if (tmp_header != 0xFF)
1493 			break;
1494 		if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
1495 			return false;
1496 
1497 	} while (1);
1498 
1499 	if (tmp_header != pg_header)
1500 		return false;
1501 
1502 	/*  to write ext_header */
1503 	efuse_addr++;
1504 	pg_header = ((pTargetPkt->offset & 0x78) << 1) | pTargetPkt->word_en;
1505 
1506 	do {
1507 		efuse_OneByteWrite(padapter, efuse_addr, pg_header, bPseudoTest);
1508 		efuse_OneByteRead(padapter, efuse_addr, &tmp_header, bPseudoTest);
1509 		if (tmp_header != 0xFF)
1510 			break;
1511 		if (repeatcnt++ > EFUSE_REPEAT_THRESHOLD_)
1512 			return false;
1513 
1514 	} while (1);
1515 
1516 	if (tmp_header != pg_header) /* offset PG fail */
1517 		return false;
1518 
1519 	*pAddr = efuse_addr;
1520 
1521 	return true;
1522 }
1523 
hal_EfusePgPacketWriteHeader(struct adapter * padapter,u8 efuseType,u16 * pAddr,struct pgpkt_struct * pTargetPkt,u8 bPseudoTest)1524 static u8 hal_EfusePgPacketWriteHeader(
1525 	struct adapter *padapter,
1526 	u8 efuseType,
1527 	u16 *pAddr,
1528 	struct pgpkt_struct *pTargetPkt,
1529 	u8 bPseudoTest
1530 )
1531 {
1532 	u8 bRet = false;
1533 
1534 	if (pTargetPkt->offset >= EFUSE_MAX_SECTION_BASE)
1535 		bRet = hal_EfusePgPacketWrite2ByteHeader(padapter, efuseType, pAddr, pTargetPkt, bPseudoTest);
1536 	else
1537 		bRet = hal_EfusePgPacketWrite1ByteHeader(padapter, efuseType, pAddr, pTargetPkt, bPseudoTest);
1538 
1539 	return bRet;
1540 }
1541 
hal_EfusePgPacketWriteData(struct adapter * padapter,u8 efuseType,u16 * pAddr,struct pgpkt_struct * pTargetPkt,u8 bPseudoTest)1542 static u8 hal_EfusePgPacketWriteData(
1543 	struct adapter *padapter,
1544 	u8 efuseType,
1545 	u16 *pAddr,
1546 	struct pgpkt_struct *pTargetPkt,
1547 	u8 bPseudoTest
1548 )
1549 {
1550 	u16 efuse_addr;
1551 	u8 badworden;
1552 
1553 
1554 	efuse_addr = *pAddr;
1555 	badworden = Efuse_WordEnableDataWrite(padapter, efuse_addr+1, pTargetPkt->word_en, pTargetPkt->data, bPseudoTest);
1556 	if (badworden != 0x0F)
1557 		return false;
1558 
1559 	return true;
1560 }
1561 
Hal_EfusePgPacketWrite(struct adapter * padapter,u8 offset,u8 word_en,u8 * pData,bool bPseudoTest)1562 static s32 Hal_EfusePgPacketWrite(
1563 	struct adapter *padapter,
1564 	u8 offset,
1565 	u8 word_en,
1566 	u8 *pData,
1567 	bool bPseudoTest
1568 )
1569 {
1570 	struct pgpkt_struct targetPkt;
1571 	u16 startAddr = 0;
1572 	u8 efuseType = EFUSE_WIFI;
1573 
1574 	if (!hal_EfusePgCheckAvailableAddr(padapter, efuseType, bPseudoTest))
1575 		return false;
1576 
1577 	hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
1578 
1579 	if (!hal_EfusePartialWriteCheck(padapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
1580 		return false;
1581 
1582 	if (!hal_EfusePgPacketWriteHeader(padapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
1583 		return false;
1584 
1585 	if (!hal_EfusePgPacketWriteData(padapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
1586 		return false;
1587 
1588 	return true;
1589 }
1590 
Hal_EfusePgPacketWrite_BT(struct adapter * padapter,u8 offset,u8 word_en,u8 * pData,bool bPseudoTest)1591 static bool Hal_EfusePgPacketWrite_BT(
1592 	struct adapter *padapter,
1593 	u8 offset,
1594 	u8 word_en,
1595 	u8 *pData,
1596 	bool bPseudoTest
1597 )
1598 {
1599 	struct pgpkt_struct targetPkt;
1600 	u16 startAddr = 0;
1601 	u8 efuseType = EFUSE_BT;
1602 
1603 	if (!hal_EfusePgCheckAvailableAddr(padapter, efuseType, bPseudoTest))
1604 		return false;
1605 
1606 	hal_EfuseConstructPGPkt(offset, word_en, pData, &targetPkt);
1607 
1608 	if (!hal_EfusePartialWriteCheck(padapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
1609 		return false;
1610 
1611 	if (!hal_EfusePgPacketWriteHeader(padapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
1612 		return false;
1613 
1614 	if (!hal_EfusePgPacketWriteData(padapter, efuseType, &startAddr, &targetPkt, bPseudoTest))
1615 		return false;
1616 
1617 	return true;
1618 }
1619 
ReadChipVersion8723B(struct adapter * padapter)1620 static struct hal_version ReadChipVersion8723B(struct adapter *padapter)
1621 {
1622 	u32 value32;
1623 	struct hal_version ChipVersion;
1624 	struct hal_com_data *pHalData;
1625 
1626 /* YJ, TODO, move read chip type here */
1627 	pHalData = GET_HAL_DATA(padapter);
1628 
1629 	value32 = rtw_read32(padapter, REG_SYS_CFG);
1630 	ChipVersion.ICType = CHIP_8723B;
1631 	ChipVersion.ChipType = ((value32 & RTL_ID) ? TEST_CHIP : NORMAL_CHIP);
1632 	ChipVersion.VendorType = ((value32 & VENDOR_ID) ? CHIP_VENDOR_UMC : CHIP_VENDOR_TSMC);
1633 	ChipVersion.CUTVersion = (value32 & CHIP_VER_RTL_MASK)>>CHIP_VER_RTL_SHIFT; /*  IC version (CUT) */
1634 
1635 	/*  For regulator mode. by tynli. 2011.01.14 */
1636 	pHalData->RegulatorMode = ((value32 & SPS_SEL) ? RT_LDO_REGULATOR : RT_SWITCHING_REGULATOR);
1637 
1638 	value32 = rtw_read32(padapter, REG_GPIO_OUTSTS);
1639 	ChipVersion.ROMVer = ((value32 & RF_RL_ID) >> 20);	/*  ROM code version. */
1640 
1641 	/*  For multi-function consideration. Added by Roger, 2010.10.06. */
1642 	pHalData->MultiFunc = RT_MULTI_FUNC_NONE;
1643 	value32 = rtw_read32(padapter, REG_MULTI_FUNC_CTRL);
1644 	pHalData->MultiFunc |= ((value32 & WL_FUNC_EN) ? RT_MULTI_FUNC_WIFI : 0);
1645 	pHalData->MultiFunc |= ((value32 & BT_FUNC_EN) ? RT_MULTI_FUNC_BT : 0);
1646 	pHalData->MultiFunc |= ((value32 & GPS_FUNC_EN) ? RT_MULTI_FUNC_GPS : 0);
1647 	pHalData->PolarityCtl = ((value32 & WL_HWPDN_SL) ? RT_POLARITY_HIGH_ACT : RT_POLARITY_LOW_ACT);
1648 #if 1
1649 	dump_chip_info(ChipVersion);
1650 #endif
1651 	pHalData->VersionID = ChipVersion;
1652 
1653 	return ChipVersion;
1654 }
1655 
rtl8723b_read_chip_version(struct adapter * padapter)1656 static void rtl8723b_read_chip_version(struct adapter *padapter)
1657 {
1658 	ReadChipVersion8723B(padapter);
1659 }
1660 
rtl8723b_InitBeaconParameters(struct adapter * padapter)1661 void rtl8723b_InitBeaconParameters(struct adapter *padapter)
1662 {
1663 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
1664 	u16 val16;
1665 	u8 val8;
1666 
1667 
1668 	val8 = DIS_TSF_UDT;
1669 	val16 = val8 | (val8 << 8); /*  port0 and port1 */
1670 
1671 	/*  Enable prot0 beacon function for PSTDMA */
1672 	val16 |= EN_BCN_FUNCTION;
1673 
1674 	rtw_write16(padapter, REG_BCN_CTRL, val16);
1675 
1676 	/*  TODO: Remove these magic number */
1677 	rtw_write16(padapter, REG_TBTT_PROHIBIT, 0x6404);/*  ms */
1678 	/*  Firmware will control REG_DRVERLYINT when power saving is enable, */
1679 	/*  so don't set this register on STA mode. */
1680 	if (check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE) == false)
1681 		rtw_write8(padapter, REG_DRVERLYINT, DRIVER_EARLY_INT_TIME_8723B); /*  5ms */
1682 	rtw_write8(padapter, REG_BCNDMATIM, BCN_DMA_ATIME_INT_TIME_8723B); /*  2ms */
1683 
1684 	/*  Suggested by designer timchen. Change beacon AIFS to the largest number */
1685 	/*  because test chip does not contension before sending beacon. by tynli. 2009.11.03 */
1686 	rtw_write16(padapter, REG_BCNTCFG, 0x660F);
1687 
1688 	pHalData->RegBcnCtrlVal = rtw_read8(padapter, REG_BCN_CTRL);
1689 	pHalData->RegTxPause = rtw_read8(padapter, REG_TXPAUSE);
1690 	pHalData->RegFwHwTxQCtrl = rtw_read8(padapter, REG_FWHW_TXQ_CTRL+2);
1691 	pHalData->RegReg542 = rtw_read8(padapter, REG_TBTT_PROHIBIT+2);
1692 	pHalData->RegCR_1 = rtw_read8(padapter, REG_CR+1);
1693 }
1694 
_InitBurstPktLen_8723BS(struct adapter * Adapter)1695 void _InitBurstPktLen_8723BS(struct adapter *Adapter)
1696 {
1697 	struct hal_com_data *pHalData = GET_HAL_DATA(Adapter);
1698 
1699 	rtw_write8(Adapter, 0x4c7, rtw_read8(Adapter, 0x4c7)|BIT(7)); /* enable single pkt ampdu */
1700 	rtw_write8(Adapter, REG_RX_PKT_LIMIT_8723B, 0x18);		/* for VHT packet length 11K */
1701 	rtw_write8(Adapter, REG_MAX_AGGR_NUM_8723B, 0x1F);
1702 	rtw_write8(Adapter, REG_PIFS_8723B, 0x00);
1703 	rtw_write8(Adapter, REG_FWHW_TXQ_CTRL_8723B, rtw_read8(Adapter, REG_FWHW_TXQ_CTRL)&(~BIT(7)));
1704 	if (pHalData->AMPDUBurstMode)
1705 		rtw_write8(Adapter, REG_AMPDU_BURST_MODE_8723B,  0x5F);
1706 	rtw_write8(Adapter, REG_AMPDU_MAX_TIME_8723B, 0x70);
1707 
1708 	/*  ARFB table 9 for 11ac 5G 2SS */
1709 	rtw_write32(Adapter, REG_ARFR0_8723B, 0x00000010);
1710 	if (IS_NORMAL_CHIP(pHalData->VersionID))
1711 		rtw_write32(Adapter, REG_ARFR0_8723B+4, 0xfffff000);
1712 	else
1713 		rtw_write32(Adapter, REG_ARFR0_8723B+4, 0x3e0ff000);
1714 
1715 	/*  ARFB table 10 for 11ac 5G 1SS */
1716 	rtw_write32(Adapter, REG_ARFR1_8723B, 0x00000010);
1717 	rtw_write32(Adapter, REG_ARFR1_8723B+4, 0x003ff000);
1718 }
1719 
ResumeTxBeacon(struct adapter * padapter)1720 static void ResumeTxBeacon(struct adapter *padapter)
1721 {
1722 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
1723 
1724 	pHalData->RegFwHwTxQCtrl |= BIT(6);
1725 	rtw_write8(padapter, REG_FWHW_TXQ_CTRL+2, pHalData->RegFwHwTxQCtrl);
1726 	rtw_write8(padapter, REG_TBTT_PROHIBIT+1, 0xff);
1727 	pHalData->RegReg542 |= BIT(0);
1728 	rtw_write8(padapter, REG_TBTT_PROHIBIT+2, pHalData->RegReg542);
1729 }
1730 
StopTxBeacon(struct adapter * padapter)1731 static void StopTxBeacon(struct adapter *padapter)
1732 {
1733 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
1734 
1735 	pHalData->RegFwHwTxQCtrl &= ~BIT(6);
1736 	rtw_write8(padapter, REG_FWHW_TXQ_CTRL+2, pHalData->RegFwHwTxQCtrl);
1737 	rtw_write8(padapter, REG_TBTT_PROHIBIT+1, 0x64);
1738 	pHalData->RegReg542 &= ~BIT(0);
1739 	rtw_write8(padapter, REG_TBTT_PROHIBIT+2, pHalData->RegReg542);
1740 
1741 	CheckFwRsvdPageContent(padapter);  /*  2010.06.23. Added by tynli. */
1742 }
1743 
_BeaconFunctionEnable(struct adapter * padapter,u8 Enable,u8 Linked)1744 static void _BeaconFunctionEnable(struct adapter *padapter, u8 Enable, u8 Linked)
1745 {
1746 	rtw_write8(padapter, REG_BCN_CTRL, DIS_TSF_UDT | EN_BCN_FUNCTION | DIS_BCNQ_SUB);
1747 	rtw_write8(padapter, REG_RD_CTRL+1, 0x6F);
1748 }
1749 
rtl8723b_SetBeaconRelatedRegisters(struct adapter * padapter)1750 static void rtl8723b_SetBeaconRelatedRegisters(struct adapter *padapter)
1751 {
1752 	u8 val8;
1753 	u32 value32;
1754 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1755 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
1756 	u32 bcn_ctrl_reg;
1757 
1758 	/* reset TSF, enable update TSF, correcting TSF On Beacon */
1759 
1760 	/* REG_BCN_INTERVAL */
1761 	/* REG_BCNDMATIM */
1762 	/* REG_ATIMWND */
1763 	/* REG_TBTT_PROHIBIT */
1764 	/* REG_DRVERLYINT */
1765 	/* REG_BCN_MAX_ERR */
1766 	/* REG_BCNTCFG (0x510) */
1767 	/* REG_DUAL_TSF_RST */
1768 	/* REG_BCN_CTRL (0x550) */
1769 
1770 
1771 	bcn_ctrl_reg = REG_BCN_CTRL;
1772 
1773 	/*  */
1774 	/*  ATIM window */
1775 	/*  */
1776 	rtw_write16(padapter, REG_ATIMWND, 2);
1777 
1778 	/*  */
1779 	/*  Beacon interval (in unit of TU). */
1780 	/*  */
1781 	rtw_write16(padapter, REG_BCN_INTERVAL, pmlmeinfo->bcn_interval);
1782 
1783 	rtl8723b_InitBeaconParameters(padapter);
1784 
1785 	rtw_write8(padapter, REG_SLOT, 0x09);
1786 
1787 	/*  */
1788 	/*  Reset TSF Timer to zero, added by Roger. 2008.06.24 */
1789 	/*  */
1790 	value32 = rtw_read32(padapter, REG_TCR);
1791 	value32 &= ~TSFRST;
1792 	rtw_write32(padapter, REG_TCR, value32);
1793 
1794 	value32 |= TSFRST;
1795 	rtw_write32(padapter, REG_TCR, value32);
1796 
1797 	/*  NOTE: Fix test chip's bug (about contention windows's randomness) */
1798 	if (check_fwstate(&padapter->mlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE) == true) {
1799 		rtw_write8(padapter, REG_RXTSF_OFFSET_CCK, 0x50);
1800 		rtw_write8(padapter, REG_RXTSF_OFFSET_OFDM, 0x50);
1801 	}
1802 
1803 	_BeaconFunctionEnable(padapter, true, true);
1804 
1805 	ResumeTxBeacon(padapter);
1806 	val8 = rtw_read8(padapter, bcn_ctrl_reg);
1807 	val8 |= DIS_BCNQ_SUB;
1808 	rtw_write8(padapter, bcn_ctrl_reg, val8);
1809 }
1810 
rtl8723b_GetHalODMVar(struct adapter * Adapter,enum hal_odm_variable eVariable,void * pValue1,void * pValue2)1811 static void rtl8723b_GetHalODMVar(
1812 	struct adapter *Adapter,
1813 	enum hal_odm_variable eVariable,
1814 	void *pValue1,
1815 	void *pValue2
1816 )
1817 {
1818 	GetHalODMVar(Adapter, eVariable, pValue1, pValue2);
1819 }
1820 
rtl8723b_SetHalODMVar(struct adapter * Adapter,enum hal_odm_variable eVariable,void * pValue1,bool bSet)1821 static void rtl8723b_SetHalODMVar(
1822 	struct adapter *Adapter,
1823 	enum hal_odm_variable eVariable,
1824 	void *pValue1,
1825 	bool bSet
1826 )
1827 {
1828 	SetHalODMVar(Adapter, eVariable, pValue1, bSet);
1829 }
1830 
hal_notch_filter_8723b(struct adapter * adapter,bool enable)1831 static void hal_notch_filter_8723b(struct adapter *adapter, bool enable)
1832 {
1833 	if (enable)
1834 		rtw_write8(adapter, rOFDM0_RxDSP+1, rtw_read8(adapter, rOFDM0_RxDSP+1) | BIT1);
1835 	else
1836 		rtw_write8(adapter, rOFDM0_RxDSP+1, rtw_read8(adapter, rOFDM0_RxDSP+1) & ~BIT1);
1837 }
1838 
UpdateHalRAMask8723B(struct adapter * padapter,u32 mac_id,u8 rssi_level)1839 static void UpdateHalRAMask8723B(struct adapter *padapter, u32 mac_id, u8 rssi_level)
1840 {
1841 	u32 mask, rate_bitmap;
1842 	u8 shortGIrate = false;
1843 	struct sta_info *psta;
1844 	struct hal_com_data	*pHalData = GET_HAL_DATA(padapter);
1845 	struct dm_priv *pdmpriv = &pHalData->dmpriv;
1846 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1847 	struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1848 
1849 	if (mac_id >= NUM_STA) /* CAM_SIZE */
1850 		return;
1851 
1852 	psta = pmlmeinfo->FW_sta_info[mac_id].psta;
1853 	if (!psta)
1854 		return;
1855 
1856 	shortGIrate = query_ra_short_GI(psta);
1857 
1858 	mask = psta->ra_mask;
1859 
1860 	rate_bitmap = 0xffffffff;
1861 	rate_bitmap = ODM_Get_Rate_Bitmap(&pHalData->odmpriv, mac_id, mask, rssi_level);
1862 
1863 	mask &= rate_bitmap;
1864 
1865 	rate_bitmap = hal_btcoex_GetRaMask(padapter);
1866 	mask &= ~rate_bitmap;
1867 
1868 	if (pHalData->fw_ractrl) {
1869 		rtl8723b_set_FwMacIdConfig_cmd(padapter, mac_id, psta->raid, psta->bw_mode, shortGIrate, mask);
1870 	}
1871 
1872 	/* set correct initial date rate for each mac_id */
1873 	pdmpriv->INIDATA_RATE[mac_id] = psta->init_rate;
1874 }
1875 
1876 
rtl8723b_set_hal_ops(struct hal_ops * pHalFunc)1877 void rtl8723b_set_hal_ops(struct hal_ops *pHalFunc)
1878 {
1879 	pHalFunc->free_hal_data = &rtl8723b_free_hal_data;
1880 
1881 	pHalFunc->dm_init = &rtl8723b_init_dm_priv;
1882 
1883 	pHalFunc->read_chip_version = &rtl8723b_read_chip_version;
1884 
1885 	pHalFunc->UpdateRAMaskHandler = &UpdateHalRAMask8723B;
1886 
1887 	pHalFunc->set_bwmode_handler = &PHY_SetBWMode8723B;
1888 	pHalFunc->set_channel_handler = &PHY_SwChnl8723B;
1889 	pHalFunc->set_chnl_bw_handler = &PHY_SetSwChnlBWMode8723B;
1890 
1891 	pHalFunc->set_tx_power_level_handler = &PHY_SetTxPowerLevel8723B;
1892 	pHalFunc->get_tx_power_level_handler = &PHY_GetTxPowerLevel8723B;
1893 
1894 	pHalFunc->hal_dm_watchdog = &rtl8723b_HalDmWatchDog;
1895 	pHalFunc->hal_dm_watchdog_in_lps = &rtl8723b_HalDmWatchDog_in_LPS;
1896 
1897 
1898 	pHalFunc->SetBeaconRelatedRegistersHandler = &rtl8723b_SetBeaconRelatedRegisters;
1899 
1900 	pHalFunc->Add_RateATid = &rtl8723b_Add_RateATid;
1901 
1902 	pHalFunc->run_thread = &rtl8723b_start_thread;
1903 	pHalFunc->cancel_thread = &rtl8723b_stop_thread;
1904 
1905 	pHalFunc->read_bbreg = &PHY_QueryBBReg_8723B;
1906 	pHalFunc->write_bbreg = &PHY_SetBBReg_8723B;
1907 	pHalFunc->read_rfreg = &PHY_QueryRFReg_8723B;
1908 	pHalFunc->write_rfreg = &PHY_SetRFReg_8723B;
1909 
1910 	/*  Efuse related function */
1911 	pHalFunc->BTEfusePowerSwitch = &Hal_BT_EfusePowerSwitch;
1912 	pHalFunc->EfusePowerSwitch = &Hal_EfusePowerSwitch;
1913 	pHalFunc->ReadEFuse = &Hal_ReadEFuse;
1914 	pHalFunc->EFUSEGetEfuseDefinition = &Hal_GetEfuseDefinition;
1915 	pHalFunc->EfuseGetCurrentSize = &Hal_EfuseGetCurrentSize;
1916 	pHalFunc->Efuse_PgPacketRead = &Hal_EfusePgPacketRead;
1917 	pHalFunc->Efuse_PgPacketWrite = &Hal_EfusePgPacketWrite;
1918 	pHalFunc->Efuse_WordEnableDataWrite = &Hal_EfuseWordEnableDataWrite;
1919 	pHalFunc->Efuse_PgPacketWrite_BT = &Hal_EfusePgPacketWrite_BT;
1920 
1921 	pHalFunc->GetHalODMVarHandler = &rtl8723b_GetHalODMVar;
1922 	pHalFunc->SetHalODMVarHandler = &rtl8723b_SetHalODMVar;
1923 
1924 	pHalFunc->xmit_thread_handler = &hal_xmit_handler;
1925 	pHalFunc->hal_notch_filter = &hal_notch_filter_8723b;
1926 
1927 	pHalFunc->c2h_handler = c2h_handler_8723b;
1928 	pHalFunc->c2h_id_filter_ccx = c2h_id_filter_ccx_8723b;
1929 
1930 	pHalFunc->fill_h2c_cmd = &FillH2CCmd8723B;
1931 }
1932 
rtl8723b_InitAntenna_Selection(struct adapter * padapter)1933 void rtl8723b_InitAntenna_Selection(struct adapter *padapter)
1934 {
1935 	u8 val;
1936 
1937 	val = rtw_read8(padapter, REG_LEDCFG2);
1938 	/*  Let 8051 take control antenna setting */
1939 	val |= BIT(7); /*  DPDT_SEL_EN, 0x4C[23] */
1940 	rtw_write8(padapter, REG_LEDCFG2, val);
1941 }
1942 
rtl8723b_init_default_value(struct adapter * padapter)1943 void rtl8723b_init_default_value(struct adapter *padapter)
1944 {
1945 	struct hal_com_data *pHalData;
1946 	struct dm_priv *pdmpriv;
1947 	u8 i;
1948 
1949 
1950 	pHalData = GET_HAL_DATA(padapter);
1951 	pdmpriv = &pHalData->dmpriv;
1952 
1953 	padapter->registrypriv.wireless_mode = WIRELESS_11BG_24N;
1954 
1955 	/*  init default value */
1956 	pHalData->fw_ractrl = false;
1957 	pHalData->bIQKInitialized = false;
1958 	if (!adapter_to_pwrctl(padapter)->bkeepfwalive)
1959 		pHalData->LastHMEBoxNum = 0;
1960 
1961 	pHalData->bIQKInitialized = false;
1962 
1963 	/*  init dm default value */
1964 	pdmpriv->TM_Trigger = 0;/* for IQK */
1965 /* 	pdmpriv->binitialized = false; */
1966 /* 	pdmpriv->prv_traffic_idx = 3; */
1967 /* 	pdmpriv->initialize = 0; */
1968 
1969 	pdmpriv->ThermalValue_HP_index = 0;
1970 	for (i = 0; i < HP_THERMAL_NUM; i++)
1971 		pdmpriv->ThermalValue_HP[i] = 0;
1972 
1973 	/*  init Efuse variables */
1974 	pHalData->EfuseUsedBytes = 0;
1975 	pHalData->EfuseUsedPercentage = 0;
1976 #ifdef HAL_EFUSE_MEMORY
1977 	pHalData->EfuseHal.fakeEfuseBank = 0;
1978 	pHalData->EfuseHal.fakeEfuseUsedBytes = 0;
1979 	memset(pHalData->EfuseHal.fakeEfuseContent, 0xFF, EFUSE_MAX_HW_SIZE);
1980 	memset(pHalData->EfuseHal.fakeEfuseInitMap, 0xFF, EFUSE_MAX_MAP_LEN);
1981 	memset(pHalData->EfuseHal.fakeEfuseModifiedMap, 0xFF, EFUSE_MAX_MAP_LEN);
1982 	pHalData->EfuseHal.BTEfuseUsedBytes = 0;
1983 	pHalData->EfuseHal.BTEfuseUsedPercentage = 0;
1984 	memset(pHalData->EfuseHal.BTEfuseContent, 0xFF, EFUSE_MAX_BT_BANK*EFUSE_MAX_HW_SIZE);
1985 	memset(pHalData->EfuseHal.BTEfuseInitMap, 0xFF, EFUSE_BT_MAX_MAP_LEN);
1986 	memset(pHalData->EfuseHal.BTEfuseModifiedMap, 0xFF, EFUSE_BT_MAX_MAP_LEN);
1987 	pHalData->EfuseHal.fakeBTEfuseUsedBytes = 0;
1988 	memset(pHalData->EfuseHal.fakeBTEfuseContent, 0xFF, EFUSE_MAX_BT_BANK*EFUSE_MAX_HW_SIZE);
1989 	memset(pHalData->EfuseHal.fakeBTEfuseInitMap, 0xFF, EFUSE_BT_MAX_MAP_LEN);
1990 	memset(pHalData->EfuseHal.fakeBTEfuseModifiedMap, 0xFF, EFUSE_BT_MAX_MAP_LEN);
1991 #endif
1992 }
1993 
GetEEPROMSize8723B(struct adapter * padapter)1994 u8 GetEEPROMSize8723B(struct adapter *padapter)
1995 {
1996 	u8 size = 0;
1997 	u32 cr;
1998 
1999 	cr = rtw_read16(padapter, REG_9346CR);
2000 	/*  6: EEPROM used is 93C46, 4: boot from E-Fuse. */
2001 	size = (cr & BOOT_FROM_EEPROM) ? 6 : 4;
2002 
2003 	return size;
2004 }
2005 
2006 /*  */
2007 /*  */
2008 /*  LLT R/W/Init function */
2009 /*  */
2010 /*  */
rtl8723b_InitLLTTable(struct adapter * padapter)2011 s32 rtl8723b_InitLLTTable(struct adapter *padapter)
2012 {
2013 	unsigned long start, passing_time;
2014 	u32 val32;
2015 	s32 ret;
2016 
2017 
2018 	ret = _FAIL;
2019 
2020 	val32 = rtw_read32(padapter, REG_AUTO_LLT);
2021 	val32 |= BIT_AUTO_INIT_LLT;
2022 	rtw_write32(padapter, REG_AUTO_LLT, val32);
2023 
2024 	start = jiffies;
2025 
2026 	do {
2027 		val32 = rtw_read32(padapter, REG_AUTO_LLT);
2028 		if (!(val32 & BIT_AUTO_INIT_LLT)) {
2029 			ret = _SUCCESS;
2030 			break;
2031 		}
2032 
2033 		passing_time = jiffies_to_msecs(jiffies - start);
2034 		if (passing_time > 1000)
2035 			break;
2036 
2037 		msleep(1);
2038 	} while (1);
2039 
2040 	return ret;
2041 }
2042 
hal_get_chnl_group_8723b(u8 channel,u8 * group)2043 static void hal_get_chnl_group_8723b(u8 channel, u8 *group)
2044 {
2045 	if (1  <= channel && channel <= 2)
2046 		*group = 0;
2047 	else if (3  <= channel && channel <= 5)
2048 		*group = 1;
2049 	else if (6  <= channel && channel <= 8)
2050 		*group = 2;
2051 	else if (9  <= channel && channel <= 11)
2052 		*group = 3;
2053 	else if (12 <= channel && channel <= 14)
2054 		*group = 4;
2055 }
2056 
Hal_InitPGData(struct adapter * padapter,u8 * PROMContent)2057 void Hal_InitPGData(struct adapter *padapter, u8 *PROMContent)
2058 {
2059 	struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
2060 
2061 	if (!pEEPROM->bautoload_fail_flag) { /*  autoload OK. */
2062 		if (!pEEPROM->EepromOrEfuse) {
2063 			/*  Read EFUSE real map to shadow. */
2064 			EFUSE_ShadowMapUpdate(padapter, EFUSE_WIFI, false);
2065 			memcpy((void *)PROMContent, (void *)pEEPROM->efuse_eeprom_data, HWSET_MAX_SIZE_8723B);
2066 		}
2067 	} else {/* autoload fail */
2068 		if (!pEEPROM->EepromOrEfuse)
2069 			EFUSE_ShadowMapUpdate(padapter, EFUSE_WIFI, false);
2070 		memcpy((void *)PROMContent, (void *)pEEPROM->efuse_eeprom_data, HWSET_MAX_SIZE_8723B);
2071 	}
2072 }
2073 
Hal_EfuseParseIDCode(struct adapter * padapter,u8 * hwinfo)2074 void Hal_EfuseParseIDCode(struct adapter *padapter, u8 *hwinfo)
2075 {
2076 	struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
2077 /* 	struct hal_com_data	*pHalData = GET_HAL_DATA(padapter); */
2078 	u16 EEPROMId;
2079 
2080 
2081 	/*  Check 0x8129 again for making sure autoload status!! */
2082 	EEPROMId = le16_to_cpu(*((__le16 *)hwinfo));
2083 	if (EEPROMId != RTL_EEPROM_ID) {
2084 		pEEPROM->bautoload_fail_flag = true;
2085 	} else
2086 		pEEPROM->bautoload_fail_flag = false;
2087 }
2088 
Hal_ReadPowerValueFromPROM_8723B(struct adapter * Adapter,struct TxPowerInfo24G * pwrInfo24G,u8 * PROMContent,bool AutoLoadFail)2089 static void Hal_ReadPowerValueFromPROM_8723B(
2090 	struct adapter *Adapter,
2091 	struct TxPowerInfo24G *pwrInfo24G,
2092 	u8 *PROMContent,
2093 	bool AutoLoadFail
2094 )
2095 {
2096 	struct hal_com_data *pHalData = GET_HAL_DATA(Adapter);
2097 	u32 rfPath, eeAddr = EEPROM_TX_PWR_INX_8723B, group, TxCount = 0;
2098 
2099 	memset(pwrInfo24G, 0, sizeof(struct TxPowerInfo24G));
2100 
2101 	if (0xFF == PROMContent[eeAddr+1])
2102 		AutoLoadFail = true;
2103 
2104 	if (AutoLoadFail) {
2105 		for (rfPath = 0; rfPath < MAX_RF_PATH; rfPath++) {
2106 			/* 2.4G default value */
2107 			for (group = 0; group < MAX_CHNL_GROUP_24G; group++) {
2108 				pwrInfo24G->IndexCCK_Base[rfPath][group] = EEPROM_DEFAULT_24G_INDEX;
2109 				pwrInfo24G->IndexBW40_Base[rfPath][group] = EEPROM_DEFAULT_24G_INDEX;
2110 			}
2111 
2112 			for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2113 				if (TxCount == 0) {
2114 					pwrInfo24G->BW20_Diff[rfPath][0] = EEPROM_DEFAULT_24G_HT20_DIFF;
2115 					pwrInfo24G->OFDM_Diff[rfPath][0] = EEPROM_DEFAULT_24G_OFDM_DIFF;
2116 				} else {
2117 					pwrInfo24G->BW20_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2118 					pwrInfo24G->BW40_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2119 					pwrInfo24G->CCK_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2120 					pwrInfo24G->OFDM_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2121 				}
2122 			}
2123 		}
2124 
2125 		return;
2126 	}
2127 
2128 	pHalData->bTXPowerDataReadFromEEPORM = true;		/* YJ, move, 120316 */
2129 
2130 	for (rfPath = 0; rfPath < MAX_RF_PATH; rfPath++) {
2131 		/* 2 2.4G default value */
2132 		for (group = 0; group < MAX_CHNL_GROUP_24G; group++) {
2133 			pwrInfo24G->IndexCCK_Base[rfPath][group] =	PROMContent[eeAddr++];
2134 			if (pwrInfo24G->IndexCCK_Base[rfPath][group] == 0xFF)
2135 				pwrInfo24G->IndexCCK_Base[rfPath][group] = EEPROM_DEFAULT_24G_INDEX;
2136 		}
2137 
2138 		for (group = 0; group < MAX_CHNL_GROUP_24G-1; group++) {
2139 			pwrInfo24G->IndexBW40_Base[rfPath][group] =	PROMContent[eeAddr++];
2140 			if (pwrInfo24G->IndexBW40_Base[rfPath][group] == 0xFF)
2141 				pwrInfo24G->IndexBW40_Base[rfPath][group] =	EEPROM_DEFAULT_24G_INDEX;
2142 		}
2143 
2144 		for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2145 			if (TxCount == 0) {
2146 				pwrInfo24G->BW40_Diff[rfPath][TxCount] = 0;
2147 				if (PROMContent[eeAddr] == 0xFF)
2148 					pwrInfo24G->BW20_Diff[rfPath][TxCount] =	EEPROM_DEFAULT_24G_HT20_DIFF;
2149 				else {
2150 					pwrInfo24G->BW20_Diff[rfPath][TxCount] =	(PROMContent[eeAddr]&0xf0)>>4;
2151 					if (pwrInfo24G->BW20_Diff[rfPath][TxCount] & BIT3)		/* 4bit sign number to 8 bit sign number */
2152 						pwrInfo24G->BW20_Diff[rfPath][TxCount] |= 0xF0;
2153 				}
2154 
2155 				if (PROMContent[eeAddr] == 0xFF)
2156 					pwrInfo24G->OFDM_Diff[rfPath][TxCount] = EEPROM_DEFAULT_24G_OFDM_DIFF;
2157 				else {
2158 					pwrInfo24G->OFDM_Diff[rfPath][TxCount] = (PROMContent[eeAddr]&0x0f);
2159 					if (pwrInfo24G->OFDM_Diff[rfPath][TxCount] & BIT3)		/* 4bit sign number to 8 bit sign number */
2160 						pwrInfo24G->OFDM_Diff[rfPath][TxCount] |= 0xF0;
2161 				}
2162 				pwrInfo24G->CCK_Diff[rfPath][TxCount] = 0;
2163 				eeAddr++;
2164 			} else {
2165 				if (PROMContent[eeAddr] == 0xFF)
2166 					pwrInfo24G->BW40_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2167 				else {
2168 					pwrInfo24G->BW40_Diff[rfPath][TxCount] = (PROMContent[eeAddr]&0xf0)>>4;
2169 					if (pwrInfo24G->BW40_Diff[rfPath][TxCount] & BIT3)		/* 4bit sign number to 8 bit sign number */
2170 						pwrInfo24G->BW40_Diff[rfPath][TxCount] |= 0xF0;
2171 				}
2172 
2173 				if (PROMContent[eeAddr] == 0xFF)
2174 					pwrInfo24G->BW20_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2175 				else {
2176 					pwrInfo24G->BW20_Diff[rfPath][TxCount] = (PROMContent[eeAddr]&0x0f);
2177 					if (pwrInfo24G->BW20_Diff[rfPath][TxCount] & BIT3)		/* 4bit sign number to 8 bit sign number */
2178 						pwrInfo24G->BW20_Diff[rfPath][TxCount] |= 0xF0;
2179 				}
2180 				eeAddr++;
2181 
2182 				if (PROMContent[eeAddr] == 0xFF)
2183 					pwrInfo24G->OFDM_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2184 				else {
2185 					pwrInfo24G->OFDM_Diff[rfPath][TxCount] = (PROMContent[eeAddr]&0xf0)>>4;
2186 					if (pwrInfo24G->OFDM_Diff[rfPath][TxCount] & BIT3)		/* 4bit sign number to 8 bit sign number */
2187 						pwrInfo24G->OFDM_Diff[rfPath][TxCount] |= 0xF0;
2188 				}
2189 
2190 				if (PROMContent[eeAddr] == 0xFF)
2191 					pwrInfo24G->CCK_Diff[rfPath][TxCount] = EEPROM_DEFAULT_DIFF;
2192 				else {
2193 					pwrInfo24G->CCK_Diff[rfPath][TxCount] = (PROMContent[eeAddr]&0x0f);
2194 					if (pwrInfo24G->CCK_Diff[rfPath][TxCount] & BIT3)		/* 4bit sign number to 8 bit sign number */
2195 						pwrInfo24G->CCK_Diff[rfPath][TxCount] |= 0xF0;
2196 				}
2197 				eeAddr++;
2198 			}
2199 		}
2200 	}
2201 }
2202 
2203 
Hal_EfuseParseTxPowerInfo_8723B(struct adapter * padapter,u8 * PROMContent,bool AutoLoadFail)2204 void Hal_EfuseParseTxPowerInfo_8723B(
2205 	struct adapter *padapter, u8 *PROMContent, bool AutoLoadFail
2206 )
2207 {
2208 	struct hal_com_data	*pHalData = GET_HAL_DATA(padapter);
2209 	struct TxPowerInfo24G	pwrInfo24G;
2210 	u8 	rfPath, ch, TxCount = 1;
2211 
2212 	Hal_ReadPowerValueFromPROM_8723B(padapter, &pwrInfo24G, PROMContent, AutoLoadFail);
2213 	for (rfPath = 0 ; rfPath < MAX_RF_PATH ; rfPath++) {
2214 		for (ch = 0 ; ch < CHANNEL_MAX_NUMBER; ch++) {
2215 			u8 group = 0;
2216 
2217 			hal_get_chnl_group_8723b(ch + 1, &group);
2218 
2219 			if (ch == 14-1) {
2220 				pHalData->Index24G_CCK_Base[rfPath][ch] = pwrInfo24G.IndexCCK_Base[rfPath][5];
2221 				pHalData->Index24G_BW40_Base[rfPath][ch] = pwrInfo24G.IndexBW40_Base[rfPath][group];
2222 			} else {
2223 				pHalData->Index24G_CCK_Base[rfPath][ch] = pwrInfo24G.IndexCCK_Base[rfPath][group];
2224 				pHalData->Index24G_BW40_Base[rfPath][ch] = pwrInfo24G.IndexBW40_Base[rfPath][group];
2225 			}
2226 		}
2227 
2228 		for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2229 			pHalData->CCK_24G_Diff[rfPath][TxCount] = pwrInfo24G.CCK_Diff[rfPath][TxCount];
2230 			pHalData->OFDM_24G_Diff[rfPath][TxCount] = pwrInfo24G.OFDM_Diff[rfPath][TxCount];
2231 			pHalData->BW20_24G_Diff[rfPath][TxCount] = pwrInfo24G.BW20_Diff[rfPath][TxCount];
2232 			pHalData->BW40_24G_Diff[rfPath][TxCount] = pwrInfo24G.BW40_Diff[rfPath][TxCount];
2233 		}
2234 	}
2235 
2236 	/*  2010/10/19 MH Add Regulator recognize for CU. */
2237 	if (!AutoLoadFail) {
2238 		pHalData->EEPROMRegulatory = (PROMContent[EEPROM_RF_BOARD_OPTION_8723B]&0x7);	/* bit0~2 */
2239 		if (PROMContent[EEPROM_RF_BOARD_OPTION_8723B] == 0xFF)
2240 			pHalData->EEPROMRegulatory = (EEPROM_DEFAULT_BOARD_OPTION&0x7);	/* bit0~2 */
2241 	} else
2242 		pHalData->EEPROMRegulatory = 0;
2243 }
2244 
Hal_EfuseParseBTCoexistInfo_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2245 void Hal_EfuseParseBTCoexistInfo_8723B(
2246 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2247 )
2248 {
2249 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
2250 	u8 tempval;
2251 	u32 tmpu4;
2252 
2253 	if (!AutoLoadFail) {
2254 		tmpu4 = rtw_read32(padapter, REG_MULTI_FUNC_CTRL);
2255 		if (tmpu4 & BT_FUNC_EN)
2256 			pHalData->EEPROMBluetoothCoexist = true;
2257 		else
2258 			pHalData->EEPROMBluetoothCoexist = false;
2259 
2260 		pHalData->EEPROMBluetoothType = BT_RTL8723B;
2261 
2262 		tempval = hwinfo[EEPROM_RF_BT_SETTING_8723B];
2263 		if (tempval != 0xFF) {
2264 			pHalData->EEPROMBluetoothAntNum = tempval & BIT(0);
2265 			/*  EFUSE_0xC3[6] == 0, S1(Main)-RF_PATH_A; */
2266 			/*  EFUSE_0xC3[6] == 1, S0(Aux)-RF_PATH_B */
2267 			if (tempval & BIT(6))
2268 				pHalData->ant_path = RF_PATH_B;
2269 			else
2270 				pHalData->ant_path = RF_PATH_A;
2271 		} else {
2272 			pHalData->EEPROMBluetoothAntNum = Ant_x1;
2273 			if (pHalData->PackageType == PACKAGE_QFN68)
2274 				pHalData->ant_path = RF_PATH_B;
2275 			else
2276 				pHalData->ant_path = RF_PATH_A;
2277 		}
2278 	} else {
2279 		pHalData->EEPROMBluetoothCoexist = false;
2280 		pHalData->EEPROMBluetoothType = BT_RTL8723B;
2281 		pHalData->EEPROMBluetoothAntNum = Ant_x1;
2282 		pHalData->ant_path = RF_PATH_A;
2283 	}
2284 
2285 	if (padapter->registrypriv.ant_num > 0) {
2286 		switch (padapter->registrypriv.ant_num) {
2287 		case 1:
2288 			pHalData->EEPROMBluetoothAntNum = Ant_x1;
2289 			break;
2290 		case 2:
2291 			pHalData->EEPROMBluetoothAntNum = Ant_x2;
2292 			break;
2293 		default:
2294 			break;
2295 		}
2296 	}
2297 
2298 	hal_btcoex_SetBTCoexist(padapter, pHalData->EEPROMBluetoothCoexist);
2299 	hal_btcoex_SetPgAntNum(padapter, pHalData->EEPROMBluetoothAntNum == Ant_x2 ? 2 : 1);
2300 	if (pHalData->EEPROMBluetoothAntNum == Ant_x1)
2301 		hal_btcoex_SetSingleAntPath(padapter, pHalData->ant_path);
2302 }
2303 
Hal_EfuseParseEEPROMVer_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2304 void Hal_EfuseParseEEPROMVer_8723B(
2305 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2306 )
2307 {
2308 	struct hal_com_data	*pHalData = GET_HAL_DATA(padapter);
2309 
2310 	if (!AutoLoadFail)
2311 		pHalData->EEPROMVersion = hwinfo[EEPROM_VERSION_8723B];
2312 	else
2313 		pHalData->EEPROMVersion = 1;
2314 }
2315 
2316 
2317 
Hal_EfuseParsePackageType_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2318 void Hal_EfuseParsePackageType_8723B(
2319 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2320 )
2321 {
2322 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
2323 	u8 package;
2324 	u8 efuseContent;
2325 
2326 	Efuse_PowerSwitch(padapter, false, true);
2327 	efuse_OneByteRead(padapter, 0x1FB, &efuseContent, false);
2328 	Efuse_PowerSwitch(padapter, false, false);
2329 
2330 	package = efuseContent & 0x7;
2331 	switch (package) {
2332 	case 0x4:
2333 		pHalData->PackageType = PACKAGE_TFBGA79;
2334 		break;
2335 	case 0x5:
2336 		pHalData->PackageType = PACKAGE_TFBGA90;
2337 		break;
2338 	case 0x6:
2339 		pHalData->PackageType = PACKAGE_QFN68;
2340 		break;
2341 	case 0x7:
2342 		pHalData->PackageType = PACKAGE_TFBGA80;
2343 		break;
2344 
2345 	default:
2346 		pHalData->PackageType = PACKAGE_DEFAULT;
2347 		break;
2348 	}
2349 }
2350 
2351 
Hal_EfuseParseVoltage_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2352 void Hal_EfuseParseVoltage_8723B(
2353 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2354 )
2355 {
2356 	struct eeprom_priv *pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
2357 
2358 	/* memcpy(pEEPROM->adjuseVoltageVal, &hwinfo[EEPROM_Voltage_ADDR_8723B], 1); */
2359 	pEEPROM->adjuseVoltageVal = (hwinfo[EEPROM_Voltage_ADDR_8723B] & 0xf0) >> 4;
2360 }
2361 
Hal_EfuseParseChnlPlan_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2362 void Hal_EfuseParseChnlPlan_8723B(
2363 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2364 )
2365 {
2366 	padapter->mlmepriv.ChannelPlan = hal_com_config_channel_plan(
2367 		padapter,
2368 		hwinfo ? hwinfo[EEPROM_ChannelPlan_8723B] : 0xFF,
2369 		padapter->registrypriv.channel_plan,
2370 		RT_CHANNEL_DOMAIN_WORLD_NULL,
2371 		AutoLoadFail
2372 	);
2373 
2374 	Hal_ChannelPlanToRegulation(padapter, padapter->mlmepriv.ChannelPlan);
2375 }
2376 
Hal_EfuseParseCustomerID_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2377 void Hal_EfuseParseCustomerID_8723B(
2378 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2379 )
2380 {
2381 	struct hal_com_data	*pHalData = GET_HAL_DATA(padapter);
2382 
2383 	if (!AutoLoadFail)
2384 		pHalData->EEPROMCustomerID = hwinfo[EEPROM_CustomID_8723B];
2385 	else
2386 		pHalData->EEPROMCustomerID = 0;
2387 }
2388 
Hal_EfuseParseAntennaDiversity_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2389 void Hal_EfuseParseAntennaDiversity_8723B(
2390 	struct adapter *padapter,
2391 	u8 *hwinfo,
2392 	bool AutoLoadFail
2393 )
2394 {
2395 }
2396 
Hal_EfuseParseXtal_8723B(struct adapter * padapter,u8 * hwinfo,bool AutoLoadFail)2397 void Hal_EfuseParseXtal_8723B(
2398 	struct adapter *padapter, u8 *hwinfo, bool AutoLoadFail
2399 )
2400 {
2401 	struct hal_com_data	*pHalData = GET_HAL_DATA(padapter);
2402 
2403 	if (!AutoLoadFail) {
2404 		pHalData->CrystalCap = hwinfo[EEPROM_XTAL_8723B];
2405 		if (pHalData->CrystalCap == 0xFF)
2406 			pHalData->CrystalCap = EEPROM_Default_CrystalCap_8723B;	   /* what value should 8812 set? */
2407 	} else
2408 		pHalData->CrystalCap = EEPROM_Default_CrystalCap_8723B;
2409 }
2410 
2411 
Hal_EfuseParseThermalMeter_8723B(struct adapter * padapter,u8 * PROMContent,u8 AutoLoadFail)2412 void Hal_EfuseParseThermalMeter_8723B(
2413 	struct adapter *padapter, u8 *PROMContent, u8 AutoLoadFail
2414 )
2415 {
2416 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
2417 
2418 	/*  */
2419 	/*  ThermalMeter from EEPROM */
2420 	/*  */
2421 	if (!AutoLoadFail)
2422 		pHalData->EEPROMThermalMeter = PROMContent[EEPROM_THERMAL_METER_8723B];
2423 	else
2424 		pHalData->EEPROMThermalMeter = EEPROM_Default_ThermalMeter_8723B;
2425 
2426 	if ((pHalData->EEPROMThermalMeter == 0xff) || AutoLoadFail) {
2427 		pHalData->bAPKThermalMeterIgnore = true;
2428 		pHalData->EEPROMThermalMeter = EEPROM_Default_ThermalMeter_8723B;
2429 	}
2430 }
2431 
2432 
Hal_ReadRFGainOffset(struct adapter * Adapter,u8 * PROMContent,bool AutoloadFail)2433 void Hal_ReadRFGainOffset(
2434 	struct adapter *Adapter, u8 *PROMContent, bool AutoloadFail
2435 )
2436 {
2437 	/*  */
2438 	/*  BB_RF Gain Offset from EEPROM */
2439 	/*  */
2440 
2441 	if (!AutoloadFail) {
2442 		Adapter->eeprompriv.EEPROMRFGainOffset = PROMContent[EEPROM_RF_GAIN_OFFSET];
2443 		Adapter->eeprompriv.EEPROMRFGainVal = EFUSE_Read1Byte(Adapter, EEPROM_RF_GAIN_VAL);
2444 	} else {
2445 		Adapter->eeprompriv.EEPROMRFGainOffset = 0;
2446 		Adapter->eeprompriv.EEPROMRFGainVal = 0xFF;
2447 	}
2448 }
2449 
BWMapping_8723B(struct adapter * Adapter,struct pkt_attrib * pattrib)2450 u8 BWMapping_8723B(struct adapter *Adapter, struct pkt_attrib *pattrib)
2451 {
2452 	u8 BWSettingOfDesc = 0;
2453 	struct hal_com_data *pHalData = GET_HAL_DATA(Adapter);
2454 
2455 	if (pHalData->CurrentChannelBW == CHANNEL_WIDTH_40) {
2456 		if (pattrib->bwmode == CHANNEL_WIDTH_40)
2457 			BWSettingOfDesc = 1;
2458 		else
2459 			BWSettingOfDesc = 0;
2460 	} else
2461 		BWSettingOfDesc = 0;
2462 
2463 	/* if (pTcb->bBTTxPacket) */
2464 	/* 	BWSettingOfDesc = 0; */
2465 
2466 	return BWSettingOfDesc;
2467 }
2468 
SCMapping_8723B(struct adapter * Adapter,struct pkt_attrib * pattrib)2469 u8 SCMapping_8723B(struct adapter *Adapter, struct pkt_attrib *pattrib)
2470 {
2471 	u8 SCSettingOfDesc = 0;
2472 	struct hal_com_data *pHalData = GET_HAL_DATA(Adapter);
2473 
2474 	if (pHalData->CurrentChannelBW == CHANNEL_WIDTH_40) {
2475 		if (pattrib->bwmode == CHANNEL_WIDTH_40) {
2476 			SCSettingOfDesc = HT_DATA_SC_DONOT_CARE;
2477 		} else if (pattrib->bwmode == CHANNEL_WIDTH_20) {
2478 			if (pHalData->nCur40MhzPrimeSC == HAL_PRIME_CHNL_OFFSET_UPPER) {
2479 				SCSettingOfDesc = HT_DATA_SC_20_UPPER_OF_40MHZ;
2480 			} else if (pHalData->nCur40MhzPrimeSC == HAL_PRIME_CHNL_OFFSET_LOWER) {
2481 				SCSettingOfDesc = HT_DATA_SC_20_LOWER_OF_40MHZ;
2482 			} else {
2483 				SCSettingOfDesc = HT_DATA_SC_DONOT_CARE;
2484 			}
2485 		}
2486 	} else {
2487 		SCSettingOfDesc = HT_DATA_SC_DONOT_CARE;
2488 	}
2489 
2490 	return SCSettingOfDesc;
2491 }
2492 
rtl8723b_cal_txdesc_chksum(struct tx_desc * ptxdesc)2493 static void rtl8723b_cal_txdesc_chksum(struct tx_desc *ptxdesc)
2494 {
2495 	u16 *usPtr = (u16 *)ptxdesc;
2496 	u32 count;
2497 	u32 index;
2498 	u16 checksum = 0;
2499 
2500 
2501 	/*  Clear first */
2502 	ptxdesc->txdw7 &= cpu_to_le32(0xffff0000);
2503 
2504 	/*  checksum is always calculated by first 32 bytes, */
2505 	/*  and it doesn't depend on TX DESC length. */
2506 	/*  Thomas, Lucas@SD4, 20130515 */
2507 	count = 16;
2508 
2509 	for (index = 0; index < count; index++) {
2510 		checksum |= le16_to_cpu(*(__le16 *)(usPtr + index));
2511 	}
2512 
2513 	ptxdesc->txdw7 |= cpu_to_le32(checksum & 0x0000ffff);
2514 }
2515 
fill_txdesc_sectype(struct pkt_attrib * pattrib)2516 static u8 fill_txdesc_sectype(struct pkt_attrib *pattrib)
2517 {
2518 	u8 sectype = 0;
2519 	if ((pattrib->encrypt > 0) && !pattrib->bswenc) {
2520 		switch (pattrib->encrypt) {
2521 		/*  SEC_TYPE */
2522 		case _WEP40_:
2523 		case _WEP104_:
2524 		case _TKIP_:
2525 		case _TKIP_WTMIC_:
2526 			sectype = 1;
2527 			break;
2528 
2529 		case _AES_:
2530 			sectype = 3;
2531 			break;
2532 
2533 		case _NO_PRIVACY_:
2534 		default:
2535 			break;
2536 		}
2537 	}
2538 	return sectype;
2539 }
2540 
fill_txdesc_vcs_8723b(struct adapter * padapter,struct pkt_attrib * pattrib,struct txdesc_8723b * ptxdesc)2541 static void fill_txdesc_vcs_8723b(struct adapter *padapter, struct pkt_attrib *pattrib, struct txdesc_8723b *ptxdesc)
2542 {
2543 	if (pattrib->vcs_mode) {
2544 		switch (pattrib->vcs_mode) {
2545 		case RTS_CTS:
2546 			ptxdesc->rtsen = 1;
2547 			/*  ENABLE HW RTS */
2548 			ptxdesc->hw_rts_en = 1;
2549 			break;
2550 
2551 		case CTS_TO_SELF:
2552 			ptxdesc->cts2self = 1;
2553 			break;
2554 
2555 		case NONE_VCS:
2556 		default:
2557 			break;
2558 		}
2559 
2560 		ptxdesc->rtsrate = 8; /*  RTS Rate =24M */
2561 		ptxdesc->rts_ratefb_lmt = 0xF;
2562 
2563 		if (padapter->mlmeextpriv.mlmext_info.preamble_mode == PREAMBLE_SHORT)
2564 			ptxdesc->rts_short = 1;
2565 
2566 		/*  Set RTS BW */
2567 		if (pattrib->ht_en)
2568 			ptxdesc->rts_sc = SCMapping_8723B(padapter, pattrib);
2569 	}
2570 }
2571 
fill_txdesc_phy_8723b(struct adapter * padapter,struct pkt_attrib * pattrib,struct txdesc_8723b * ptxdesc)2572 static void fill_txdesc_phy_8723b(struct adapter *padapter, struct pkt_attrib *pattrib, struct txdesc_8723b *ptxdesc)
2573 {
2574 	if (pattrib->ht_en) {
2575 		ptxdesc->data_bw = BWMapping_8723B(padapter, pattrib);
2576 
2577 		ptxdesc->data_sc = SCMapping_8723B(padapter, pattrib);
2578 	}
2579 }
2580 
rtl8723b_fill_default_txdesc(struct xmit_frame * pxmitframe,u8 * pbuf)2581 static void rtl8723b_fill_default_txdesc(
2582 	struct xmit_frame *pxmitframe, u8 *pbuf
2583 )
2584 {
2585 	struct adapter *padapter;
2586 	struct hal_com_data *pHalData;
2587 	struct mlme_ext_priv *pmlmeext;
2588 	struct mlme_ext_info *pmlmeinfo;
2589 	struct pkt_attrib *pattrib;
2590 	struct txdesc_8723b *ptxdesc;
2591 	s32 bmcst;
2592 
2593 	memset(pbuf, 0, TXDESC_SIZE);
2594 
2595 	padapter = pxmitframe->padapter;
2596 	pHalData = GET_HAL_DATA(padapter);
2597 	pmlmeext = &padapter->mlmeextpriv;
2598 	pmlmeinfo = &(pmlmeext->mlmext_info);
2599 
2600 	pattrib = &pxmitframe->attrib;
2601 	bmcst = is_multicast_ether_addr(pattrib->ra);
2602 
2603 	ptxdesc = (struct txdesc_8723b *)pbuf;
2604 
2605 	if (pxmitframe->frame_tag == DATA_FRAMETAG) {
2606 		u8 drv_userate = 0;
2607 
2608 		ptxdesc->macid = pattrib->mac_id; /*  CAM_ID(MAC_ID) */
2609 		ptxdesc->rate_id = pattrib->raid;
2610 		ptxdesc->qsel = pattrib->qsel;
2611 		ptxdesc->seq = pattrib->seqnum;
2612 
2613 		ptxdesc->sectype = fill_txdesc_sectype(pattrib);
2614 		fill_txdesc_vcs_8723b(padapter, pattrib, ptxdesc);
2615 
2616 		if (pattrib->icmp_pkt == 1 && padapter->registrypriv.wifi_spec == 1)
2617 			drv_userate = 1;
2618 
2619 		if (
2620 			(pattrib->ether_type != 0x888e) &&
2621 			(pattrib->ether_type != 0x0806) &&
2622 			(pattrib->ether_type != 0x88B4) &&
2623 			(pattrib->dhcp_pkt != 1) &&
2624 			(drv_userate != 1)
2625 		) {
2626 			/*  Non EAP & ARP & DHCP type data packet */
2627 
2628 			if (pattrib->ampdu_en) {
2629 				ptxdesc->agg_en = 1; /*  AGG EN */
2630 				ptxdesc->max_agg_num = 0x1f;
2631 				ptxdesc->ampdu_density = pattrib->ampdu_spacing;
2632 			} else
2633 				ptxdesc->bk = 1; /*  AGG BK */
2634 
2635 			fill_txdesc_phy_8723b(padapter, pattrib, ptxdesc);
2636 
2637 			ptxdesc->data_ratefb_lmt = 0x1F;
2638 
2639 			if (!pHalData->fw_ractrl) {
2640 				ptxdesc->userate = 1;
2641 
2642 				if (pHalData->dmpriv.INIDATA_RATE[pattrib->mac_id] & BIT(7))
2643 					ptxdesc->data_short = 1;
2644 
2645 				ptxdesc->datarate = pHalData->dmpriv.INIDATA_RATE[pattrib->mac_id] & 0x7F;
2646 			}
2647 
2648 			if (padapter->fix_rate != 0xFF) { /*  modify data rate by iwpriv */
2649 				ptxdesc->userate = 1;
2650 				if (padapter->fix_rate & BIT(7))
2651 					ptxdesc->data_short = 1;
2652 
2653 				ptxdesc->datarate = (padapter->fix_rate & 0x7F);
2654 				ptxdesc->disdatafb = 1;
2655 			}
2656 
2657 			if (pattrib->ldpc)
2658 				ptxdesc->data_ldpc = 1;
2659 			if (pattrib->stbc)
2660 				ptxdesc->data_stbc = 1;
2661 		} else {
2662 			/*  EAP data packet and ARP packet. */
2663 			/*  Use the 1M data rate to send the EAP/ARP packet. */
2664 			/*  This will maybe make the handshake smooth. */
2665 
2666 			ptxdesc->bk = 1; /*  AGG BK */
2667 			ptxdesc->userate = 1; /*  driver uses rate */
2668 			if (pmlmeinfo->preamble_mode == PREAMBLE_SHORT)
2669 				ptxdesc->data_short = 1;/*  DATA_SHORT */
2670 			ptxdesc->datarate = MRateToHwRate(pmlmeext->tx_rate);
2671 		}
2672 
2673 		ptxdesc->usb_txagg_num = pxmitframe->agg_num;
2674 	} else if (pxmitframe->frame_tag == MGNT_FRAMETAG) {
2675 		ptxdesc->macid = pattrib->mac_id; /*  CAM_ID(MAC_ID) */
2676 		ptxdesc->qsel = pattrib->qsel;
2677 		ptxdesc->rate_id = pattrib->raid; /*  Rate ID */
2678 		ptxdesc->seq = pattrib->seqnum;
2679 		ptxdesc->userate = 1; /*  driver uses rate, 1M */
2680 
2681 		ptxdesc->mbssid = pattrib->mbssid & 0xF;
2682 
2683 		ptxdesc->rty_lmt_en = 1; /*  retry limit enable */
2684 		if (pattrib->retry_ctrl) {
2685 			ptxdesc->data_rt_lmt = 6;
2686 		} else {
2687 			ptxdesc->data_rt_lmt = 12;
2688 		}
2689 
2690 		ptxdesc->datarate = MRateToHwRate(pmlmeext->tx_rate);
2691 
2692 		/*  CCX-TXRPT ack for xmit mgmt frames. */
2693 		if (pxmitframe->ack_report) {
2694 			ptxdesc->spe_rpt = 1;
2695 			ptxdesc->sw_define = (u8)(GET_PRIMARY_ADAPTER(padapter)->xmitpriv.seq_no);
2696 		}
2697 	} else {
2698 		ptxdesc->macid = pattrib->mac_id; /*  CAM_ID(MAC_ID) */
2699 		ptxdesc->rate_id = pattrib->raid; /*  Rate ID */
2700 		ptxdesc->qsel = pattrib->qsel;
2701 		ptxdesc->seq = pattrib->seqnum;
2702 		ptxdesc->userate = 1; /*  driver uses rate */
2703 		ptxdesc->datarate = MRateToHwRate(pmlmeext->tx_rate);
2704 	}
2705 
2706 	ptxdesc->pktlen = pattrib->last_txcmdsz;
2707 	ptxdesc->offset = TXDESC_SIZE + OFFSET_SZ;
2708 
2709 	if (bmcst)
2710 		ptxdesc->bmc = 1;
2711 
2712 	/* 2009.11.05. tynli_test. Suggested by SD4 Filen for FW LPS.
2713 	 * (1) The sequence number of each non-Qos frame / broadcast /
2714 	 * multicast / mgnt frame should be controlled by Hw because Fw
2715 	 * will also send null data which we cannot control when Fw LPS
2716 	 * enable.
2717 	 * --> default enable non-Qos data sequence number. 2010.06.23.
2718 	 * by tynli.
2719 	 * (2) Enable HW SEQ control for beacon packet, because we use
2720 	 * Hw beacon.
2721 	 * (3) Use HW Qos SEQ to control the seq num of Ext port non-Qos
2722 	 * packets.
2723 	 * 2010.06.23. Added by tynli.
2724 	 */
2725 	if (!pattrib->qos_en) /*  Hw set sequence number */
2726 		ptxdesc->en_hwseq = 1; /*  HWSEQ_EN */
2727 }
2728 
2729 /* Description:
2730  *
2731  * Parameters:
2732  *	pxmitframe	xmitframe
2733  *	pbuf		where to fill tx desc
2734  */
rtl8723b_update_txdesc(struct xmit_frame * pxmitframe,u8 * pbuf)2735 void rtl8723b_update_txdesc(struct xmit_frame *pxmitframe, u8 *pbuf)
2736 {
2737 	struct tx_desc *pdesc;
2738 
2739 	rtl8723b_fill_default_txdesc(pxmitframe, pbuf);
2740 	pdesc = (struct tx_desc *)pbuf;
2741 	rtl8723b_cal_txdesc_chksum(pdesc);
2742 }
2743 
2744 /*  */
2745 /*  Description: In normal chip, we should send some packet to Hw which will be used by Fw */
2746 /* 			in FW LPS mode. The function is to fill the Tx descriptor of this packets, then */
2747 /* 			Fw can tell Hw to send these packet derectly. */
2748 /*  Added by tynli. 2009.10.15. */
2749 /*  */
2750 /* type1:pspoll, type2:null */
rtl8723b_fill_fake_txdesc(struct adapter * padapter,u8 * pDesc,u32 BufferLen,u8 IsPsPoll,u8 IsBTQosNull,u8 bDataFrame)2751 void rtl8723b_fill_fake_txdesc(
2752 	struct adapter *padapter,
2753 	u8 *pDesc,
2754 	u32 BufferLen,
2755 	u8 IsPsPoll,
2756 	u8 IsBTQosNull,
2757 	u8 bDataFrame
2758 )
2759 {
2760 	/*  Clear all status */
2761 	memset(pDesc, 0, TXDESC_SIZE);
2762 
2763 	SET_TX_DESC_FIRST_SEG_8723B(pDesc, 1); /* bFirstSeg; */
2764 	SET_TX_DESC_LAST_SEG_8723B(pDesc, 1); /* bLastSeg; */
2765 
2766 	SET_TX_DESC_OFFSET_8723B(pDesc, 0x28); /*  Offset = 32 */
2767 
2768 	SET_TX_DESC_PKT_SIZE_8723B(pDesc, BufferLen); /*  Buffer size + command header */
2769 	SET_TX_DESC_QUEUE_SEL_8723B(pDesc, QSLT_MGNT); /*  Fixed queue of Mgnt queue */
2770 
2771 	/*  Set NAVUSEHDR to prevent Ps-poll AId filed to be changed to error value by Hw. */
2772 	if (IsPsPoll) {
2773 		SET_TX_DESC_NAV_USE_HDR_8723B(pDesc, 1);
2774 	} else {
2775 		SET_TX_DESC_HWSEQ_EN_8723B(pDesc, 1); /*  Hw set sequence number */
2776 		SET_TX_DESC_HWSEQ_SEL_8723B(pDesc, 0);
2777 	}
2778 
2779 	if (IsBTQosNull) {
2780 		SET_TX_DESC_BT_INT_8723B(pDesc, 1);
2781 	}
2782 
2783 	SET_TX_DESC_USE_RATE_8723B(pDesc, 1); /*  use data rate which is set by Sw */
2784 	SET_TX_DESC_OWN_8723B((u8 *)pDesc, 1);
2785 
2786 	SET_TX_DESC_TX_RATE_8723B(pDesc, DESC8723B_RATE1M);
2787 
2788 	/*  */
2789 	/*  Encrypt the data frame if under security mode excepct null data. Suggested by CCW. */
2790 	/*  */
2791 	if (bDataFrame) {
2792 		u32 EncAlg;
2793 
2794 		EncAlg = padapter->securitypriv.dot11PrivacyAlgrthm;
2795 		switch (EncAlg) {
2796 		case _NO_PRIVACY_:
2797 			SET_TX_DESC_SEC_TYPE_8723B(pDesc, 0x0);
2798 			break;
2799 		case _WEP40_:
2800 		case _WEP104_:
2801 		case _TKIP_:
2802 			SET_TX_DESC_SEC_TYPE_8723B(pDesc, 0x1);
2803 			break;
2804 		case _SMS4_:
2805 			SET_TX_DESC_SEC_TYPE_8723B(pDesc, 0x2);
2806 			break;
2807 		case _AES_:
2808 			SET_TX_DESC_SEC_TYPE_8723B(pDesc, 0x3);
2809 			break;
2810 		default:
2811 			SET_TX_DESC_SEC_TYPE_8723B(pDesc, 0x0);
2812 			break;
2813 		}
2814 	}
2815 
2816 	/*  USB interface drop packet if the checksum of descriptor isn't correct. */
2817 	/*  Using this checksum can let hardware recovery from packet bulk out error (e.g. Cancel URC, Bulk out error.). */
2818 	rtl8723b_cal_txdesc_chksum((struct tx_desc *)pDesc);
2819 }
2820 
hw_var_set_opmode(struct adapter * padapter,u8 variable,u8 * val)2821 static void hw_var_set_opmode(struct adapter *padapter, u8 variable, u8 *val)
2822 {
2823 	u8 val8;
2824 	u8 mode = *((u8 *)val);
2825 
2826 	{
2827 		/*  disable Port0 TSF update */
2828 		val8 = rtw_read8(padapter, REG_BCN_CTRL);
2829 		val8 |= DIS_TSF_UDT;
2830 		rtw_write8(padapter, REG_BCN_CTRL, val8);
2831 
2832 		/*  set net_type */
2833 		Set_MSR(padapter, mode);
2834 
2835 		if ((mode == _HW_STATE_STATION_) || (mode == _HW_STATE_NOLINK_)) {
2836 			{
2837 				StopTxBeacon(padapter);
2838 			}
2839 
2840 			/*  disable atim wnd */
2841 			rtw_write8(padapter, REG_BCN_CTRL, DIS_TSF_UDT|EN_BCN_FUNCTION|DIS_ATIM);
2842 			/* rtw_write8(padapter, REG_BCN_CTRL, 0x18); */
2843 		} else if (mode == _HW_STATE_ADHOC_) {
2844 			ResumeTxBeacon(padapter);
2845 			rtw_write8(padapter, REG_BCN_CTRL, DIS_TSF_UDT|EN_BCN_FUNCTION|DIS_BCNQ_SUB);
2846 		} else if (mode == _HW_STATE_AP_) {
2847 
2848 			ResumeTxBeacon(padapter);
2849 
2850 			rtw_write8(padapter, REG_BCN_CTRL, DIS_TSF_UDT|DIS_BCNQ_SUB);
2851 
2852 			/* Set RCR */
2853 			rtw_write32(padapter, REG_RCR, 0x7000208e);/* CBSSID_DATA must set to 0, reject ICV_ERR packet */
2854 			/* enable to rx data frame */
2855 			rtw_write16(padapter, REG_RXFLTMAP2, 0xFFFF);
2856 			/* enable to rx ps-poll */
2857 			rtw_write16(padapter, REG_RXFLTMAP1, 0x0400);
2858 
2859 			/* Beacon Control related register for first time */
2860 			rtw_write8(padapter, REG_BCNDMATIM, 0x02); /*  2ms */
2861 
2862 			/* rtw_write8(padapter, REG_BCN_MAX_ERR, 0xFF); */
2863 			rtw_write8(padapter, REG_ATIMWND, 0x0a); /*  10ms */
2864 			rtw_write16(padapter, REG_BCNTCFG, 0x00);
2865 			rtw_write16(padapter, REG_TBTT_PROHIBIT, 0xff04);
2866 			rtw_write16(padapter, REG_TSFTR_SYN_OFFSET, 0x7fff);/*  +32767 (~32ms) */
2867 
2868 			/* reset TSF */
2869 			rtw_write8(padapter, REG_DUAL_TSF_RST, BIT(0));
2870 
2871 			/* enable BCN0 Function for if1 */
2872 			/* don't enable update TSF0 for if1 (due to TSF update when beacon/probe rsp are received) */
2873 			rtw_write8(padapter, REG_BCN_CTRL, (DIS_TSF_UDT|EN_BCN_FUNCTION|EN_TXBCN_RPT|DIS_BCNQ_SUB));
2874 
2875 			/* SW_BCN_SEL - Port0 */
2876 			/* rtw_write8(Adapter, REG_DWBCN1_CTRL_8192E+2, rtw_read8(Adapter, REG_DWBCN1_CTRL_8192E+2) & ~BIT4); */
2877 			rtw_hal_set_hwreg(padapter, HW_VAR_DL_BCN_SEL, NULL);
2878 
2879 			/*  select BCN on port 0 */
2880 			rtw_write8(
2881 				padapter,
2882 				REG_CCK_CHECK_8723B,
2883 				(rtw_read8(padapter, REG_CCK_CHECK_8723B)&~BIT_BCN_PORT_SEL)
2884 			);
2885 
2886 			/*  dis BCN1 ATIM  WND if if2 is station */
2887 			val8 = rtw_read8(padapter, REG_BCN_CTRL_1);
2888 			val8 |= DIS_ATIM;
2889 			rtw_write8(padapter, REG_BCN_CTRL_1, val8);
2890 		}
2891 	}
2892 }
2893 
hw_var_set_macaddr(struct adapter * padapter,u8 variable,u8 * val)2894 static void hw_var_set_macaddr(struct adapter *padapter, u8 variable, u8 *val)
2895 {
2896 	u8 idx = 0;
2897 	u32 reg_macid;
2898 
2899 	reg_macid = REG_MACID;
2900 
2901 	for (idx = 0 ; idx < 6; idx++)
2902 		rtw_write8(GET_PRIMARY_ADAPTER(padapter), (reg_macid+idx), val[idx]);
2903 }
2904 
hw_var_set_bssid(struct adapter * padapter,u8 variable,u8 * val)2905 static void hw_var_set_bssid(struct adapter *padapter, u8 variable, u8 *val)
2906 {
2907 	u8 idx = 0;
2908 	u32 reg_bssid;
2909 
2910 	reg_bssid = REG_BSSID;
2911 
2912 	for (idx = 0 ; idx < 6; idx++)
2913 		rtw_write8(padapter, (reg_bssid+idx), val[idx]);
2914 }
2915 
hw_var_set_bcn_func(struct adapter * padapter,u8 variable,u8 * val)2916 static void hw_var_set_bcn_func(struct adapter *padapter, u8 variable, u8 *val)
2917 {
2918 	u32 bcn_ctrl_reg;
2919 
2920 	bcn_ctrl_reg = REG_BCN_CTRL;
2921 
2922 	if (*(u8 *)val)
2923 		rtw_write8(padapter, bcn_ctrl_reg, (EN_BCN_FUNCTION | EN_TXBCN_RPT));
2924 	else {
2925 		u8 val8;
2926 		val8 = rtw_read8(padapter, bcn_ctrl_reg);
2927 		val8 &= ~(EN_BCN_FUNCTION | EN_TXBCN_RPT);
2928 
2929 		/*  Always enable port0 beacon function for PSTDMA */
2930 		if (REG_BCN_CTRL == bcn_ctrl_reg)
2931 			val8 |= EN_BCN_FUNCTION;
2932 
2933 		rtw_write8(padapter, bcn_ctrl_reg, val8);
2934 	}
2935 }
2936 
hw_var_set_correct_tsf(struct adapter * padapter,u8 variable,u8 * val)2937 static void hw_var_set_correct_tsf(struct adapter *padapter, u8 variable, u8 *val)
2938 {
2939 	u8 val8;
2940 	u64 tsf;
2941 	struct mlme_ext_priv *pmlmeext;
2942 	struct mlme_ext_info *pmlmeinfo;
2943 
2944 
2945 	pmlmeext = &padapter->mlmeextpriv;
2946 	pmlmeinfo = &pmlmeext->mlmext_info;
2947 
2948 	tsf = pmlmeext->TSFValue-do_div(pmlmeext->TSFValue, (pmlmeinfo->bcn_interval*1024))-1024; /* us */
2949 
2950 	if (
2951 		((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) ||
2952 		((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
2953 	)
2954 		StopTxBeacon(padapter);
2955 
2956 	{
2957 		/*  disable related TSF function */
2958 		val8 = rtw_read8(padapter, REG_BCN_CTRL);
2959 		val8 &= ~EN_BCN_FUNCTION;
2960 		rtw_write8(padapter, REG_BCN_CTRL, val8);
2961 
2962 		rtw_write32(padapter, REG_TSFTR, tsf);
2963 		rtw_write32(padapter, REG_TSFTR+4, tsf>>32);
2964 
2965 		/*  enable related TSF function */
2966 		val8 = rtw_read8(padapter, REG_BCN_CTRL);
2967 		val8 |= EN_BCN_FUNCTION;
2968 		rtw_write8(padapter, REG_BCN_CTRL, val8);
2969 	}
2970 
2971 	if (
2972 		((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) ||
2973 		((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
2974 	)
2975 		ResumeTxBeacon(padapter);
2976 }
2977 
hw_var_set_mlme_disconnect(struct adapter * padapter,u8 variable,u8 * val)2978 static void hw_var_set_mlme_disconnect(struct adapter *padapter, u8 variable, u8 *val)
2979 {
2980 	u8 val8;
2981 
2982 	/*  Set RCR to not to receive data frame when NO LINK state */
2983 	/* rtw_write32(padapter, REG_RCR, rtw_read32(padapter, REG_RCR) & ~RCR_ADF); */
2984 	/*  reject all data frames */
2985 	rtw_write16(padapter, REG_RXFLTMAP2, 0);
2986 
2987 	/*  reset TSF */
2988 	rtw_write8(padapter, REG_DUAL_TSF_RST, BIT(0));
2989 
2990 	/*  disable update TSF */
2991 	val8 = rtw_read8(padapter, REG_BCN_CTRL);
2992 	val8 |= DIS_TSF_UDT;
2993 	rtw_write8(padapter, REG_BCN_CTRL, val8);
2994 }
2995 
hw_var_set_mlme_sitesurvey(struct adapter * padapter,u8 variable,u8 * val)2996 static void hw_var_set_mlme_sitesurvey(struct adapter *padapter, u8 variable, u8 *val)
2997 {
2998 	u32 value_rcr, rcr_clear_bit, reg_bcn_ctl;
2999 	u16 value_rxfltmap2;
3000 	u8 val8;
3001 	struct hal_com_data *pHalData;
3002 	struct mlme_priv *pmlmepriv;
3003 
3004 
3005 	pHalData = GET_HAL_DATA(padapter);
3006 	pmlmepriv = &padapter->mlmepriv;
3007 
3008 	reg_bcn_ctl = REG_BCN_CTRL;
3009 
3010 	rcr_clear_bit = RCR_CBSSID_BCN;
3011 
3012 	/*  config RCR to receive different BSSID & not to receive data frame */
3013 	value_rxfltmap2 = 0;
3014 
3015 	if ((check_fwstate(pmlmepriv, WIFI_AP_STATE) == true))
3016 		rcr_clear_bit = RCR_CBSSID_BCN;
3017 
3018 	value_rcr = rtw_read32(padapter, REG_RCR);
3019 
3020 	if (*((u8 *)val)) {
3021 		/*  under sitesurvey */
3022 		value_rcr &= ~(rcr_clear_bit);
3023 		rtw_write32(padapter, REG_RCR, value_rcr);
3024 
3025 		rtw_write16(padapter, REG_RXFLTMAP2, value_rxfltmap2);
3026 
3027 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) {
3028 			/*  disable update TSF */
3029 			val8 = rtw_read8(padapter, reg_bcn_ctl);
3030 			val8 |= DIS_TSF_UDT;
3031 			rtw_write8(padapter, reg_bcn_ctl, val8);
3032 		}
3033 
3034 		/*  Save original RRSR setting. */
3035 		pHalData->RegRRSR = rtw_read16(padapter, REG_RRSR);
3036 	} else {
3037 		/*  sitesurvey done */
3038 		if (check_fwstate(pmlmepriv, (_FW_LINKED|WIFI_AP_STATE)))
3039 			/*  enable to rx data frame */
3040 			rtw_write16(padapter, REG_RXFLTMAP2, 0xFFFF);
3041 
3042 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) {
3043 			/*  enable update TSF */
3044 			val8 = rtw_read8(padapter, reg_bcn_ctl);
3045 			val8 &= ~DIS_TSF_UDT;
3046 			rtw_write8(padapter, reg_bcn_ctl, val8);
3047 		}
3048 
3049 		value_rcr |= rcr_clear_bit;
3050 		rtw_write32(padapter, REG_RCR, value_rcr);
3051 
3052 		/*  Restore original RRSR setting. */
3053 		rtw_write16(padapter, REG_RRSR, pHalData->RegRRSR);
3054 	}
3055 }
3056 
hw_var_set_mlme_join(struct adapter * padapter,u8 variable,u8 * val)3057 static void hw_var_set_mlme_join(struct adapter *padapter, u8 variable, u8 *val)
3058 {
3059 	u8 val8;
3060 	u16 val16;
3061 	u32 val32;
3062 	u8 RetryLimit;
3063 	u8 type;
3064 	struct mlme_priv *pmlmepriv;
3065 	struct eeprom_priv *pEEPROM;
3066 
3067 
3068 	RetryLimit = 0x30;
3069 	type = *(u8 *)val;
3070 	pmlmepriv = &padapter->mlmepriv;
3071 	pEEPROM = GET_EEPROM_EFUSE_PRIV(padapter);
3072 
3073 	if (type == 0) { /*  prepare to join */
3074 		/* enable to rx data frame.Accept all data frame */
3075 		/* rtw_write32(padapter, REG_RCR, rtw_read32(padapter, REG_RCR)|RCR_ADF); */
3076 		rtw_write16(padapter, REG_RXFLTMAP2, 0xFFFF);
3077 
3078 		val32 = rtw_read32(padapter, REG_RCR);
3079 		if (padapter->in_cta_test)
3080 			val32 &= ~(RCR_CBSSID_DATA | RCR_CBSSID_BCN);/*  RCR_ADF */
3081 		else
3082 			val32 |= RCR_CBSSID_DATA|RCR_CBSSID_BCN;
3083 		rtw_write32(padapter, REG_RCR, val32);
3084 
3085 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true)
3086 			RetryLimit = (pEEPROM->CustomerID == RT_CID_CCX) ? 7 : 48;
3087 		else /*  Ad-hoc Mode */
3088 			RetryLimit = 0x7;
3089 	} else if (type == 1) /* joinbss_event call back when join res < 0 */
3090 		rtw_write16(padapter, REG_RXFLTMAP2, 0x00);
3091 	else if (type == 2) { /* sta add event call back */
3092 		/* enable update TSF */
3093 		val8 = rtw_read8(padapter, REG_BCN_CTRL);
3094 		val8 &= ~DIS_TSF_UDT;
3095 		rtw_write8(padapter, REG_BCN_CTRL, val8);
3096 
3097 		if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE))
3098 			RetryLimit = 0x7;
3099 	}
3100 
3101 	val16 = (RetryLimit << RETRY_LIMIT_SHORT_SHIFT) | (RetryLimit << RETRY_LIMIT_LONG_SHIFT);
3102 	rtw_write16(padapter, REG_RL, val16);
3103 }
3104 
CCX_FwC2HTxRpt_8723b(struct adapter * padapter,u8 * pdata,u8 len)3105 void CCX_FwC2HTxRpt_8723b(struct adapter *padapter, u8 *pdata, u8 len)
3106 {
3107 
3108 #define	GET_8723B_C2H_TX_RPT_LIFE_TIME_OVER(_Header)	LE_BITS_TO_1BYTE((_Header + 0), 6, 1)
3109 #define	GET_8723B_C2H_TX_RPT_RETRY_OVER(_Header)	LE_BITS_TO_1BYTE((_Header + 0), 7, 1)
3110 
3111 	if (GET_8723B_C2H_TX_RPT_RETRY_OVER(pdata) | GET_8723B_C2H_TX_RPT_LIFE_TIME_OVER(pdata)) {
3112 		rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_CCX_PKT_FAIL);
3113 	}
3114 /*
3115 	else if (seq_no != padapter->xmitpriv.seq_no) {
3116 		rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_CCX_PKT_FAIL);
3117 	}
3118 */
3119 	else
3120 		rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_SUCCESS);
3121 }
3122 
c2h_id_filter_ccx_8723b(u8 * buf)3123 s32 c2h_id_filter_ccx_8723b(u8 *buf)
3124 {
3125 	struct c2h_evt_hdr_88xx *c2h_evt = (struct c2h_evt_hdr_88xx *)buf;
3126 	s32 ret = false;
3127 	if (c2h_evt->id == C2H_CCX_TX_RPT)
3128 		ret = true;
3129 
3130 	return ret;
3131 }
3132 
3133 
c2h_handler_8723b(struct adapter * padapter,u8 * buf)3134 s32 c2h_handler_8723b(struct adapter *padapter, u8 *buf)
3135 {
3136 	struct c2h_evt_hdr_88xx *pC2hEvent = (struct c2h_evt_hdr_88xx *)buf;
3137 	s32 ret = _SUCCESS;
3138 
3139 	if (!pC2hEvent) {
3140 		ret = _FAIL;
3141 		goto exit;
3142 	}
3143 
3144 	switch (pC2hEvent->id) {
3145 	case C2H_AP_RPT_RSP:
3146 		break;
3147 	case C2H_DBG:
3148 		{
3149 		}
3150 		break;
3151 
3152 	case C2H_CCX_TX_RPT:
3153 /* 			CCX_FwC2HTxRpt(padapter, QueueID, pC2hEvent->payload); */
3154 		break;
3155 
3156 	case C2H_EXT_RA_RPT:
3157 /* 			C2HExtRaRptHandler(padapter, pC2hEvent->payload, C2hEvent.CmdLen); */
3158 		break;
3159 
3160 	case C2H_HW_INFO_EXCH:
3161 		break;
3162 
3163 	case C2H_8723B_BT_INFO:
3164 		hal_btcoex_BtInfoNotify(padapter, pC2hEvent->plen, pC2hEvent->payload);
3165 		break;
3166 
3167 	default:
3168 		break;
3169 	}
3170 
3171 	/*  Clear event to notify FW we have read the command. */
3172 	/*  Note: */
3173 	/* 	If this field isn't clear, the FW won't update the next command message. */
3174 /* 	rtw_write8(padapter, REG_C2HEVT_CLEAR, C2H_EVT_HOST_CLOSE); */
3175 exit:
3176 	return ret;
3177 }
3178 
process_c2h_event(struct adapter * padapter,struct c2h_evt_hdr_t * pC2hEvent,u8 * c2hBuf)3179 static void process_c2h_event(struct adapter *padapter, struct c2h_evt_hdr_t *pC2hEvent, u8 *c2hBuf)
3180 {
3181 	if (!c2hBuf)
3182 		return;
3183 
3184 	switch (pC2hEvent->CmdID) {
3185 	case C2H_AP_RPT_RSP:
3186 		break;
3187 	case C2H_DBG:
3188 		{
3189 		}
3190 		break;
3191 
3192 	case C2H_CCX_TX_RPT:
3193 /* 			CCX_FwC2HTxRpt(padapter, QueueID, tmpBuf); */
3194 		break;
3195 
3196 	case C2H_EXT_RA_RPT:
3197 /* 			C2HExtRaRptHandler(padapter, tmpBuf, C2hEvent.CmdLen); */
3198 		break;
3199 
3200 	case C2H_HW_INFO_EXCH:
3201 		break;
3202 
3203 	case C2H_8723B_BT_INFO:
3204 		hal_btcoex_BtInfoNotify(padapter, pC2hEvent->CmdLen, c2hBuf);
3205 		break;
3206 
3207 	default:
3208 		break;
3209 	}
3210 }
3211 
C2HPacketHandler_8723B(struct adapter * padapter,u8 * pbuffer,u16 length)3212 void C2HPacketHandler_8723B(struct adapter *padapter, u8 *pbuffer, u16 length)
3213 {
3214 	struct c2h_evt_hdr_t	C2hEvent;
3215 	u8 *tmpBuf = NULL;
3216 	C2hEvent.CmdID = pbuffer[0];
3217 	C2hEvent.CmdSeq = pbuffer[1];
3218 	C2hEvent.CmdLen = length-2;
3219 	tmpBuf = pbuffer+2;
3220 
3221 	process_c2h_event(padapter, &C2hEvent, tmpBuf);
3222 	/* c2h_handler_8723b(padapter,&C2hEvent); */
3223 }
3224 
SetHwReg8723B(struct adapter * padapter,u8 variable,u8 * val)3225 void SetHwReg8723B(struct adapter *padapter, u8 variable, u8 *val)
3226 {
3227 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
3228 	u8 val8;
3229 	u32 val32;
3230 
3231 	switch (variable) {
3232 	case HW_VAR_MEDIA_STATUS:
3233 		val8 = rtw_read8(padapter, MSR) & 0x0c;
3234 		val8 |= *val;
3235 		rtw_write8(padapter, MSR, val8);
3236 		break;
3237 
3238 	case HW_VAR_MEDIA_STATUS1:
3239 		val8 = rtw_read8(padapter, MSR) & 0x03;
3240 		val8 |= *val << 2;
3241 		rtw_write8(padapter, MSR, val8);
3242 		break;
3243 
3244 	case HW_VAR_SET_OPMODE:
3245 		hw_var_set_opmode(padapter, variable, val);
3246 		break;
3247 
3248 	case HW_VAR_MAC_ADDR:
3249 		hw_var_set_macaddr(padapter, variable, val);
3250 		break;
3251 
3252 	case HW_VAR_BSSID:
3253 		hw_var_set_bssid(padapter, variable, val);
3254 		break;
3255 
3256 	case HW_VAR_BASIC_RATE:
3257 	{
3258 		struct mlme_ext_info *mlmext_info = &padapter->mlmeextpriv.mlmext_info;
3259 		u16 BrateCfg = 0;
3260 		u16 rrsr_2g_force_mask = (RRSR_11M|RRSR_5_5M|RRSR_1M);
3261 		u16 rrsr_2g_allow_mask = (RRSR_24M|RRSR_12M|RRSR_6M|RRSR_CCK_RATES);
3262 
3263 		HalSetBrateCfg(padapter, val, &BrateCfg);
3264 
3265 		/* apply force and allow mask */
3266 		BrateCfg |= rrsr_2g_force_mask;
3267 		BrateCfg &= rrsr_2g_allow_mask;
3268 
3269 		/* IOT consideration */
3270 		if (mlmext_info->assoc_AP_vendor == HT_IOT_PEER_CISCO) {
3271 			/* if peer is cisco and didn't use ofdm rate, we enable 6M ack */
3272 			if ((BrateCfg & (RRSR_24M|RRSR_12M|RRSR_6M)) == 0)
3273 				BrateCfg |= RRSR_6M;
3274 		}
3275 
3276 		pHalData->BasicRateSet = BrateCfg;
3277 
3278 		/*  Set RRSR rate table. */
3279 		rtw_write16(padapter, REG_RRSR, BrateCfg);
3280 		rtw_write8(padapter, REG_RRSR+2, rtw_read8(padapter, REG_RRSR+2)&0xf0);
3281 	}
3282 		break;
3283 
3284 	case HW_VAR_TXPAUSE:
3285 		rtw_write8(padapter, REG_TXPAUSE, *val);
3286 		break;
3287 
3288 	case HW_VAR_BCN_FUNC:
3289 		hw_var_set_bcn_func(padapter, variable, val);
3290 		break;
3291 
3292 	case HW_VAR_CORRECT_TSF:
3293 		hw_var_set_correct_tsf(padapter, variable, val);
3294 		break;
3295 
3296 	case HW_VAR_CHECK_BSSID:
3297 		{
3298 			u32 val32;
3299 			val32 = rtw_read32(padapter, REG_RCR);
3300 			if (*val)
3301 				val32 |= RCR_CBSSID_DATA|RCR_CBSSID_BCN;
3302 			else
3303 				val32 &= ~(RCR_CBSSID_DATA|RCR_CBSSID_BCN);
3304 			rtw_write32(padapter, REG_RCR, val32);
3305 		}
3306 		break;
3307 
3308 	case HW_VAR_MLME_DISCONNECT:
3309 		hw_var_set_mlme_disconnect(padapter, variable, val);
3310 		break;
3311 
3312 	case HW_VAR_MLME_SITESURVEY:
3313 		hw_var_set_mlme_sitesurvey(padapter, variable,  val);
3314 
3315 		hal_btcoex_ScanNotify(padapter, *val?true:false);
3316 		break;
3317 
3318 	case HW_VAR_MLME_JOIN:
3319 		hw_var_set_mlme_join(padapter, variable, val);
3320 
3321 		switch (*val) {
3322 		case 0:
3323 			/*  prepare to join */
3324 			hal_btcoex_ConnectNotify(padapter, true);
3325 			break;
3326 		case 1:
3327 			/*  joinbss_event callback when join res < 0 */
3328 			hal_btcoex_ConnectNotify(padapter, false);
3329 			break;
3330 		case 2:
3331 			/*  sta add event callback */
3332 /* 				rtw_btcoex_MediaStatusNotify(padapter, RT_MEDIA_CONNECT); */
3333 			break;
3334 		}
3335 		break;
3336 
3337 	case HW_VAR_ON_RCR_AM:
3338 		val32 = rtw_read32(padapter, REG_RCR);
3339 		val32 |= RCR_AM;
3340 		rtw_write32(padapter, REG_RCR, val32);
3341 		break;
3342 
3343 	case HW_VAR_OFF_RCR_AM:
3344 		val32 = rtw_read32(padapter, REG_RCR);
3345 		val32 &= ~RCR_AM;
3346 		rtw_write32(padapter, REG_RCR, val32);
3347 		break;
3348 
3349 	case HW_VAR_BEACON_INTERVAL:
3350 		rtw_write16(padapter, REG_BCN_INTERVAL, *((u16 *)val));
3351 		break;
3352 
3353 	case HW_VAR_SLOT_TIME:
3354 		rtw_write8(padapter, REG_SLOT, *val);
3355 		break;
3356 
3357 	case HW_VAR_RESP_SIFS:
3358 		/* SIFS_Timer = 0x0a0a0808; */
3359 		/* RESP_SIFS for CCK */
3360 		rtw_write8(padapter, REG_RESP_SIFS_CCK, val[0]); /*  SIFS_T2T_CCK (0x08) */
3361 		rtw_write8(padapter, REG_RESP_SIFS_CCK+1, val[1]); /* SIFS_R2T_CCK(0x08) */
3362 		/* RESP_SIFS for OFDM */
3363 		rtw_write8(padapter, REG_RESP_SIFS_OFDM, val[2]); /* SIFS_T2T_OFDM (0x0a) */
3364 		rtw_write8(padapter, REG_RESP_SIFS_OFDM+1, val[3]); /* SIFS_R2T_OFDM(0x0a) */
3365 		break;
3366 
3367 	case HW_VAR_ACK_PREAMBLE:
3368 		{
3369 			u8 regTmp;
3370 			u8 bShortPreamble = *val;
3371 
3372 			/*  Joseph marked out for Netgear 3500 TKIP channel 7 issue.(Temporarily) */
3373 			/* regTmp = (pHalData->nCur40MhzPrimeSC)<<5; */
3374 			regTmp = 0;
3375 			if (bShortPreamble)
3376 				regTmp |= 0x80;
3377 			rtw_write8(padapter, REG_RRSR+2, regTmp);
3378 		}
3379 		break;
3380 
3381 	case HW_VAR_CAM_EMPTY_ENTRY:
3382 		{
3383 			u8 ucIndex = *val;
3384 			u8 i;
3385 			u32 ulCommand = 0;
3386 			u32 ulContent = 0;
3387 			u32 ulEncAlgo = CAM_AES;
3388 
3389 			for (i = 0; i < CAM_CONTENT_COUNT; i++) {
3390 				/*  filled id in CAM config 2 byte */
3391 				if (i == 0) {
3392 					ulContent |= (ucIndex & 0x03) | ((u16)(ulEncAlgo)<<2);
3393 					/* ulContent |= CAM_VALID; */
3394 				} else
3395 					ulContent = 0;
3396 
3397 				/*  polling bit, and No Write enable, and address */
3398 				ulCommand = CAM_CONTENT_COUNT*ucIndex+i;
3399 				ulCommand = ulCommand | CAM_POLLINIG | CAM_WRITE;
3400 				/*  write content 0 is equal to mark as invalid */
3401 				rtw_write32(padapter, WCAMI, ulContent);  /* mdelay(40); */
3402 				rtw_write32(padapter, RWCAM, ulCommand);  /* mdelay(40); */
3403 			}
3404 		}
3405 		break;
3406 
3407 	case HW_VAR_CAM_INVALID_ALL:
3408 		rtw_write32(padapter, RWCAM, BIT(31)|BIT(30));
3409 		break;
3410 
3411 	case HW_VAR_CAM_WRITE:
3412 		{
3413 			u32 cmd;
3414 			u32 *cam_val = (u32 *)val;
3415 
3416 			rtw_write32(padapter, WCAMI, cam_val[0]);
3417 
3418 			cmd = CAM_POLLINIG | CAM_WRITE | cam_val[1];
3419 			rtw_write32(padapter, RWCAM, cmd);
3420 		}
3421 		break;
3422 
3423 	case HW_VAR_AC_PARAM_VO:
3424 		rtw_write32(padapter, REG_EDCA_VO_PARAM, *((u32 *)val));
3425 		break;
3426 
3427 	case HW_VAR_AC_PARAM_VI:
3428 		rtw_write32(padapter, REG_EDCA_VI_PARAM, *((u32 *)val));
3429 		break;
3430 
3431 	case HW_VAR_AC_PARAM_BE:
3432 		pHalData->AcParam_BE = ((u32 *)(val))[0];
3433 		rtw_write32(padapter, REG_EDCA_BE_PARAM, *((u32 *)val));
3434 		break;
3435 
3436 	case HW_VAR_AC_PARAM_BK:
3437 		rtw_write32(padapter, REG_EDCA_BK_PARAM, *((u32 *)val));
3438 		break;
3439 
3440 	case HW_VAR_ACM_CTRL:
3441 		{
3442 			u8 ctrl = *((u8 *)val);
3443 			u8 hwctrl = 0;
3444 
3445 			if (ctrl != 0) {
3446 				hwctrl |= AcmHw_HwEn;
3447 
3448 				if (ctrl & BIT(1)) /*  BE */
3449 					hwctrl |= AcmHw_BeqEn;
3450 
3451 				if (ctrl & BIT(2)) /*  VI */
3452 					hwctrl |= AcmHw_ViqEn;
3453 
3454 				if (ctrl & BIT(3)) /*  VO */
3455 					hwctrl |= AcmHw_VoqEn;
3456 			}
3457 
3458 			rtw_write8(padapter, REG_ACMHWCTRL, hwctrl);
3459 		}
3460 		break;
3461 
3462 	case HW_VAR_AMPDU_FACTOR:
3463 		{
3464 			u32 AMPDULen =  (*((u8 *)val));
3465 
3466 			if (AMPDULen < HT_AGG_SIZE_32K)
3467 				AMPDULen = (0x2000 << (*((u8 *)val)))-1;
3468 			else
3469 				AMPDULen = 0x7fff;
3470 
3471 			rtw_write32(padapter, REG_AMPDU_MAX_LENGTH_8723B, AMPDULen);
3472 		}
3473 		break;
3474 
3475 	case HW_VAR_H2C_FW_PWRMODE:
3476 		{
3477 			u8 psmode = *val;
3478 
3479 			/*  Forece leave RF low power mode for 1T1R to prevent conficting setting in Fw power */
3480 			/*  saving sequence. 2010.06.07. Added by tynli. Suggested by SD3 yschang. */
3481 			if (psmode != PS_MODE_ACTIVE) {
3482 				ODM_RF_Saving(&pHalData->odmpriv, true);
3483 			}
3484 
3485 			/* if (psmode != PS_MODE_ACTIVE)	{ */
3486 			/* 	rtl8723b_set_lowpwr_lps_cmd(padapter, true); */
3487 			/*  else { */
3488 			/* 	rtl8723b_set_lowpwr_lps_cmd(padapter, false); */
3489 			/*  */
3490 			rtl8723b_set_FwPwrMode_cmd(padapter, psmode);
3491 		}
3492 		break;
3493 	case HW_VAR_H2C_PS_TUNE_PARAM:
3494 		rtl8723b_set_FwPsTuneParam_cmd(padapter);
3495 		break;
3496 
3497 	case HW_VAR_H2C_FW_JOINBSSRPT:
3498 		rtl8723b_set_FwJoinBssRpt_cmd(padapter, *val);
3499 		break;
3500 
3501 	case HW_VAR_INITIAL_GAIN:
3502 		{
3503 			struct dig_t *pDigTable = &pHalData->odmpriv.DM_DigTable;
3504 			u32 rx_gain = *(u32 *)val;
3505 
3506 			if (rx_gain == 0xff) {/* restore rx gain */
3507 				ODM_Write_DIG(&pHalData->odmpriv, pDigTable->BackupIGValue);
3508 			} else {
3509 				pDigTable->BackupIGValue = pDigTable->CurIGValue;
3510 				ODM_Write_DIG(&pHalData->odmpriv, rx_gain);
3511 			}
3512 		}
3513 		break;
3514 
3515 	case HW_VAR_EFUSE_USAGE:
3516 		pHalData->EfuseUsedPercentage = *val;
3517 		break;
3518 
3519 	case HW_VAR_EFUSE_BYTES:
3520 		pHalData->EfuseUsedBytes = *((u16 *)val);
3521 		break;
3522 
3523 	case HW_VAR_EFUSE_BT_USAGE:
3524 #ifdef HAL_EFUSE_MEMORY
3525 		pHalData->EfuseHal.BTEfuseUsedPercentage = *val;
3526 #endif
3527 		break;
3528 
3529 	case HW_VAR_EFUSE_BT_BYTES:
3530 #ifdef HAL_EFUSE_MEMORY
3531 		pHalData->EfuseHal.BTEfuseUsedBytes = *((u16 *)val);
3532 #else
3533 		BTEfuseUsedBytes = *((u16 *)val);
3534 #endif
3535 		break;
3536 
3537 	case HW_VAR_FIFO_CLEARN_UP:
3538 		{
3539 			#define RW_RELEASE_EN		BIT(18)
3540 			#define RXDMA_IDLE			BIT(17)
3541 
3542 			struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
3543 			u8 trycnt = 100;
3544 
3545 			/*  pause tx */
3546 			rtw_write8(padapter, REG_TXPAUSE, 0xff);
3547 
3548 			/*  keep sn */
3549 			padapter->xmitpriv.nqos_ssn = rtw_read16(padapter, REG_NQOS_SEQ);
3550 
3551 			if (!pwrpriv->bkeepfwalive) {
3552 				/* RX DMA stop */
3553 				val32 = rtw_read32(padapter, REG_RXPKT_NUM);
3554 				val32 |= RW_RELEASE_EN;
3555 				rtw_write32(padapter, REG_RXPKT_NUM, val32);
3556 				do {
3557 					val32 = rtw_read32(padapter, REG_RXPKT_NUM);
3558 					val32 &= RXDMA_IDLE;
3559 					if (val32)
3560 						break;
3561 				} while (--trycnt);
3562 
3563 				/*  RQPN Load 0 */
3564 				rtw_write16(padapter, REG_RQPN_NPQ, 0);
3565 				rtw_write32(padapter, REG_RQPN, 0x80000000);
3566 				mdelay(2);
3567 			}
3568 		}
3569 		break;
3570 
3571 	case HW_VAR_APFM_ON_MAC:
3572 		pHalData->bMacPwrCtrlOn = *val;
3573 		break;
3574 
3575 	case HW_VAR_NAV_UPPER:
3576 		{
3577 			u32 usNavUpper = *((u32 *)val);
3578 
3579 			if (usNavUpper > HAL_NAV_UPPER_UNIT_8723B * 0xFF)
3580 				break;
3581 
3582 			usNavUpper = DIV_ROUND_UP(usNavUpper,
3583 						  HAL_NAV_UPPER_UNIT_8723B);
3584 			rtw_write8(padapter, REG_NAV_UPPER, (u8)usNavUpper);
3585 		}
3586 		break;
3587 
3588 	case HW_VAR_H2C_MEDIA_STATUS_RPT:
3589 		{
3590 			u16 mstatus_rpt = (*(u16 *)val);
3591 			u8 mstatus, macId;
3592 
3593 			mstatus = (u8) (mstatus_rpt & 0xFF);
3594 			macId = (u8)(mstatus_rpt >> 8);
3595 			rtl8723b_set_FwMediaStatusRpt_cmd(padapter, mstatus, macId);
3596 		}
3597 		break;
3598 	case HW_VAR_BCN_VALID:
3599 		{
3600 			/*  BCN_VALID, BIT16 of REG_TDECTRL = BIT0 of REG_TDECTRL+2, write 1 to clear, Clear by sw */
3601 			val8 = rtw_read8(padapter, REG_TDECTRL+2);
3602 			val8 |= BIT(0);
3603 			rtw_write8(padapter, REG_TDECTRL+2, val8);
3604 		}
3605 		break;
3606 
3607 	case HW_VAR_DL_BCN_SEL:
3608 		{
3609 			/*  SW_BCN_SEL - Port0 */
3610 			val8 = rtw_read8(padapter, REG_DWBCN1_CTRL_8723B+2);
3611 			val8 &= ~BIT(4);
3612 			rtw_write8(padapter, REG_DWBCN1_CTRL_8723B+2, val8);
3613 		}
3614 		break;
3615 
3616 	case HW_VAR_DO_IQK:
3617 		pHalData->bNeedIQK = true;
3618 		break;
3619 
3620 	case HW_VAR_DL_RSVD_PAGE:
3621 		if (check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == true)
3622 			rtl8723b_download_BTCoex_AP_mode_rsvd_page(padapter);
3623 		else
3624 			rtl8723b_download_rsvd_page(padapter, RT_MEDIA_CONNECT);
3625 		break;
3626 
3627 	case HW_VAR_MACID_SLEEP:
3628 		/*  Input is MACID */
3629 		val32 = *(u32 *)val;
3630 		if (val32 > 31)
3631 			break;
3632 
3633 		val8 = (u8)val32; /*  macid is between 0~31 */
3634 
3635 		val32 = rtw_read32(padapter, REG_MACID_SLEEP);
3636 		if (val32 & BIT(val8))
3637 			break;
3638 		val32 |= BIT(val8);
3639 		rtw_write32(padapter, REG_MACID_SLEEP, val32);
3640 		break;
3641 
3642 	case HW_VAR_MACID_WAKEUP:
3643 		/*  Input is MACID */
3644 		val32 = *(u32 *)val;
3645 		if (val32 > 31)
3646 			break;
3647 
3648 		val8 = (u8)val32; /*  macid is between 0~31 */
3649 
3650 		val32 = rtw_read32(padapter, REG_MACID_SLEEP);
3651 		if (!(val32 & BIT(val8)))
3652 			break;
3653 		val32 &= ~BIT(val8);
3654 		rtw_write32(padapter, REG_MACID_SLEEP, val32);
3655 		break;
3656 
3657 	default:
3658 		SetHwReg(padapter, variable, val);
3659 		break;
3660 	}
3661 }
3662 
GetHwReg8723B(struct adapter * padapter,u8 variable,u8 * val)3663 void GetHwReg8723B(struct adapter *padapter, u8 variable, u8 *val)
3664 {
3665 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
3666 	u8 val8;
3667 	u16 val16;
3668 
3669 	switch (variable) {
3670 	case HW_VAR_TXPAUSE:
3671 		*val = rtw_read8(padapter, REG_TXPAUSE);
3672 		break;
3673 
3674 	case HW_VAR_BCN_VALID:
3675 		{
3676 			/*  BCN_VALID, BIT16 of REG_TDECTRL = BIT0 of REG_TDECTRL+2 */
3677 			val8 = rtw_read8(padapter, REG_TDECTRL+2);
3678 			*val = (BIT(0) & val8) ? true : false;
3679 		}
3680 		break;
3681 
3682 	case HW_VAR_FWLPS_RF_ON:
3683 		{
3684 			/*  When we halt NIC, we should check if FW LPS is leave. */
3685 			u32 valRCR;
3686 
3687 			if (
3688 				padapter->bSurpriseRemoved  ||
3689 				(adapter_to_pwrctl(padapter)->rf_pwrstate == rf_off)
3690 			) {
3691 				/*  If it is in HW/SW Radio OFF or IPS state, we do not check Fw LPS Leave, */
3692 				/*  because Fw is unload. */
3693 				*val = true;
3694 			} else {
3695 				valRCR = rtw_read32(padapter, REG_RCR);
3696 				valRCR &= 0x00070000;
3697 				if (valRCR)
3698 					*val = false;
3699 				else
3700 					*val = true;
3701 			}
3702 		}
3703 		break;
3704 
3705 	case HW_VAR_EFUSE_USAGE:
3706 		*val = pHalData->EfuseUsedPercentage;
3707 		break;
3708 
3709 	case HW_VAR_EFUSE_BYTES:
3710 		*((u16 *)val) = pHalData->EfuseUsedBytes;
3711 		break;
3712 
3713 	case HW_VAR_EFUSE_BT_USAGE:
3714 #ifdef HAL_EFUSE_MEMORY
3715 		*val = pHalData->EfuseHal.BTEfuseUsedPercentage;
3716 #endif
3717 		break;
3718 
3719 	case HW_VAR_EFUSE_BT_BYTES:
3720 #ifdef HAL_EFUSE_MEMORY
3721 		*((u16 *)val) = pHalData->EfuseHal.BTEfuseUsedBytes;
3722 #else
3723 		*((u16 *)val) = BTEfuseUsedBytes;
3724 #endif
3725 		break;
3726 
3727 	case HW_VAR_APFM_ON_MAC:
3728 		*val = pHalData->bMacPwrCtrlOn;
3729 		break;
3730 	case HW_VAR_CHK_HI_QUEUE_EMPTY:
3731 		val16 = rtw_read16(padapter, REG_TXPKT_EMPTY);
3732 		*val = (val16 & BIT(10)) ? true:false;
3733 		break;
3734 	default:
3735 		GetHwReg(padapter, variable, val);
3736 		break;
3737 	}
3738 }
3739 
3740 /* Description:
3741  *	Change default setting of specified variable.
3742  */
SetHalDefVar8723B(struct adapter * padapter,enum hal_def_variable variable,void * pval)3743 u8 SetHalDefVar8723B(struct adapter *padapter, enum hal_def_variable variable, void *pval)
3744 {
3745 	u8 bResult;
3746 
3747 	bResult = _SUCCESS;
3748 
3749 	switch (variable) {
3750 	default:
3751 		bResult = SetHalDefVar(padapter, variable, pval);
3752 		break;
3753 	}
3754 
3755 	return bResult;
3756 }
3757 
3758 /* Description:
3759  *	Query setting of specified variable.
3760  */
GetHalDefVar8723B(struct adapter * padapter,enum hal_def_variable variable,void * pval)3761 u8 GetHalDefVar8723B(struct adapter *padapter, enum hal_def_variable variable, void *pval)
3762 {
3763 	u8 bResult;
3764 
3765 	bResult = _SUCCESS;
3766 
3767 	switch (variable) {
3768 	case HAL_DEF_MAX_RECVBUF_SZ:
3769 		*((u32 *)pval) = MAX_RECVBUF_SZ;
3770 		break;
3771 
3772 	case HAL_DEF_RX_PACKET_OFFSET:
3773 		*((u32 *)pval) = RXDESC_SIZE + DRVINFO_SZ*8;
3774 		break;
3775 
3776 	case HW_VAR_MAX_RX_AMPDU_FACTOR:
3777 		/*  Stanley@BB.SD3 suggests 16K can get stable performance */
3778 		/*  The experiment was done on SDIO interface */
3779 		/*  coding by Lucas@20130730 */
3780 		*(u32 *)pval = IEEE80211_HT_MAX_AMPDU_16K;
3781 		break;
3782 	case HAL_DEF_TX_LDPC:
3783 	case HAL_DEF_RX_LDPC:
3784 		*((u8 *)pval) = false;
3785 		break;
3786 	case HAL_DEF_TX_STBC:
3787 		*((u8 *)pval) = 0;
3788 		break;
3789 	case HAL_DEF_RX_STBC:
3790 		*((u8 *)pval) = 1;
3791 		break;
3792 	case HAL_DEF_EXPLICIT_BEAMFORMER:
3793 	case HAL_DEF_EXPLICIT_BEAMFORMEE:
3794 		*((u8 *)pval) = false;
3795 		break;
3796 
3797 	case HW_DEF_RA_INFO_DUMP:
3798 		{
3799 			u8 mac_id = *(u8 *)pval;
3800 			u32 cmd;
3801 
3802 			cmd = 0x40000100 | mac_id;
3803 			rtw_write32(padapter, REG_HMEBOX_DBG_2_8723B, cmd);
3804 			msleep(10);
3805 			rtw_read32(padapter, 0x2F0);	// info 1
3806 
3807 			cmd = 0x40000400 | mac_id;
3808 			rtw_write32(padapter, REG_HMEBOX_DBG_2_8723B, cmd);
3809 			msleep(10);
3810 			rtw_read32(padapter, 0x2F0);	// info 1
3811 			rtw_read32(padapter, 0x2F4);	// info 2
3812 			rtw_read32(padapter, 0x2F8);	// rate mask 1
3813 			rtw_read32(padapter, 0x2FC);	// rate mask 2
3814 		}
3815 		break;
3816 
3817 	case HAL_DEF_TX_PAGE_BOUNDARY:
3818 		if (!padapter->registrypriv.wifi_spec) {
3819 			*(u8 *)pval = TX_PAGE_BOUNDARY_8723B;
3820 		} else {
3821 			*(u8 *)pval = WMM_NORMAL_TX_PAGE_BOUNDARY_8723B;
3822 		}
3823 		break;
3824 
3825 	case HAL_DEF_MACID_SLEEP:
3826 		*(u8 *)pval = true; /*  support macid sleep */
3827 		break;
3828 
3829 	default:
3830 		bResult = GetHalDefVar(padapter, variable, pval);
3831 		break;
3832 	}
3833 
3834 	return bResult;
3835 }
3836 
rtl8723b_start_thread(struct adapter * padapter)3837 void rtl8723b_start_thread(struct adapter *padapter)
3838 {
3839 	struct xmit_priv *xmitpriv = &padapter->xmitpriv;
3840 
3841 	xmitpriv->SdioXmitThread = kthread_run(rtl8723bs_xmit_thread, padapter, "RTWHALXT");
3842 }
3843 
rtl8723b_stop_thread(struct adapter * padapter)3844 void rtl8723b_stop_thread(struct adapter *padapter)
3845 {
3846 	struct xmit_priv *xmitpriv = &padapter->xmitpriv;
3847 
3848 	/*  stop xmit_buf_thread */
3849 	if (xmitpriv->SdioXmitThread) {
3850 		complete(&xmitpriv->SdioXmitStart);
3851 		wait_for_completion(&xmitpriv->SdioXmitTerminate);
3852 		xmitpriv->SdioXmitThread = NULL;
3853 	}
3854 }
3855