xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8188fu/hal/HalPwrSeqCmd.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright(c) 2007 - 2017 Realtek Corporation.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
6*4882a593Smuzhiyun  * under the terms of version 2 of the GNU General Public License as
7*4882a593Smuzhiyun  * published by the Free Software Foundation.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but WITHOUT
10*4882a593Smuzhiyun  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12*4882a593Smuzhiyun  * more details.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *****************************************************************************/
15*4882a593Smuzhiyun /*++
16*4882a593Smuzhiyun Copyright (c) Realtek Semiconductor Corp. All rights reserved.
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun Module Name:
19*4882a593Smuzhiyun 	HalPwrSeqCmd.c
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun Abstract:
22*4882a593Smuzhiyun 	Implement HW Power sequence configuration CMD handling routine for Realtek devices.
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun Major Change History:
25*4882a593Smuzhiyun 	When       Who               What
26*4882a593Smuzhiyun 	---------- ---------------   -------------------------------
27*4882a593Smuzhiyun 	2011-10-26 Lucas            Modify to be compatible with SD4-CE driver.
28*4882a593Smuzhiyun 	2011-07-07 Roger            Create.
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun --*/
31*4882a593Smuzhiyun #include <HalPwrSeqCmd.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  *	Description:
36*4882a593Smuzhiyun  *		This routine deal with the Power Configuration CMDs parsing for RTL8723/RTL8188E Series IC.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  *	Assumption:
39*4882a593Smuzhiyun  *		We should follow specific format which was released from HW SD.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  *	2011.07.07, added by Roger.
42*4882a593Smuzhiyun  *   */
HalPwrSeqCmdParsing(PADAPTER padapter,u8 CutVersion,u8 FabVersion,u8 InterfaceType,WLAN_PWR_CFG PwrSeqCmd[])43*4882a593Smuzhiyun u8 HalPwrSeqCmdParsing(
44*4882a593Smuzhiyun 	PADAPTER		padapter,
45*4882a593Smuzhiyun 	u8				CutVersion,
46*4882a593Smuzhiyun 	u8				FabVersion,
47*4882a593Smuzhiyun 	u8				InterfaceType,
48*4882a593Smuzhiyun 	WLAN_PWR_CFG	PwrSeqCmd[])
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	WLAN_PWR_CFG	PwrCfgCmd = {0};
51*4882a593Smuzhiyun 	u8				bPollingBit = _FALSE;
52*4882a593Smuzhiyun 	u8				bHWICSupport = _FALSE;
53*4882a593Smuzhiyun 	u32				AryIdx = 0;
54*4882a593Smuzhiyun 	u8				value = 0;
55*4882a593Smuzhiyun 	u32				offset = 0;
56*4882a593Smuzhiyun 	u8				flag = 0;
57*4882a593Smuzhiyun 	u32				pollingCount = 0; /* polling autoload done. */
58*4882a593Smuzhiyun 	u32				maxPollingCnt = 5000;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	do {
61*4882a593Smuzhiyun 		PwrCfgCmd = PwrSeqCmd[AryIdx];
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 		/* 2 Only Handle the command whose FAB, CUT, and Interface are matched */
65*4882a593Smuzhiyun 		if ((GET_PWR_CFG_FAB_MASK(PwrCfgCmd) & FabVersion) &&
66*4882a593Smuzhiyun 		    (GET_PWR_CFG_CUT_MASK(PwrCfgCmd) & CutVersion) &&
67*4882a593Smuzhiyun 		    (GET_PWR_CFG_INTF_MASK(PwrCfgCmd) & InterfaceType)) {
68*4882a593Smuzhiyun 			switch (GET_PWR_CFG_CMD(PwrCfgCmd)) {
69*4882a593Smuzhiyun 			case PWR_CMD_READ:
70*4882a593Smuzhiyun 				break;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 			case PWR_CMD_WRITE:
73*4882a593Smuzhiyun 				offset = GET_PWR_CFG_OFFSET(PwrCfgCmd);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #ifdef CONFIG_SDIO_HCI
76*4882a593Smuzhiyun 				/*  */
77*4882a593Smuzhiyun 				/* <Roger_Notes> We should deal with interface specific address mapping for some interfaces, e.g., SDIO interface */
78*4882a593Smuzhiyun 				/* 2011.07.07. */
79*4882a593Smuzhiyun 				/*  */
80*4882a593Smuzhiyun 				if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO) {
81*4882a593Smuzhiyun 					/* Read Back SDIO Local value */
82*4882a593Smuzhiyun 					value = SdioLocalCmd52Read1Byte(padapter, offset);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 					value &= ~(GET_PWR_CFG_MASK(PwrCfgCmd));
85*4882a593Smuzhiyun 					value |= (GET_PWR_CFG_VALUE(PwrCfgCmd) & GET_PWR_CFG_MASK(PwrCfgCmd));
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 					/* Write Back SDIO Local value */
88*4882a593Smuzhiyun 					SdioLocalCmd52Write1Byte(padapter, offset, value);
89*4882a593Smuzhiyun 				} else
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 				{
92*4882a593Smuzhiyun #ifdef CONFIG_GSPI_HCI
93*4882a593Smuzhiyun 					if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO)
94*4882a593Smuzhiyun 						offset = SPI_LOCAL_OFFSET | offset;
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun 					/* Read the value from system register */
97*4882a593Smuzhiyun 					value = rtw_read8(padapter, offset);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 					value = value & (~(GET_PWR_CFG_MASK(PwrCfgCmd)));
100*4882a593Smuzhiyun 					value = value | (GET_PWR_CFG_VALUE(PwrCfgCmd) & GET_PWR_CFG_MASK(PwrCfgCmd));
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 					/* Write the value back to sytem register */
103*4882a593Smuzhiyun 					rtw_write8(padapter, offset, value);
104*4882a593Smuzhiyun 				}
105*4882a593Smuzhiyun 				break;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 			case PWR_CMD_POLLING:
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 				bPollingBit = _FALSE;
110*4882a593Smuzhiyun 				offset = GET_PWR_CFG_OFFSET(PwrCfgCmd);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 				rtw_hal_get_hwreg(padapter, HW_VAR_PWR_CMD, &bHWICSupport);
113*4882a593Smuzhiyun 				if (bHWICSupport && offset == 0x06) {
114*4882a593Smuzhiyun 					flag = 0;
115*4882a593Smuzhiyun 					maxPollingCnt = 100000;
116*4882a593Smuzhiyun 				} else
117*4882a593Smuzhiyun 					maxPollingCnt = 5000;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun #ifdef CONFIG_GSPI_HCI
120*4882a593Smuzhiyun 				if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO)
121*4882a593Smuzhiyun 					offset = SPI_LOCAL_OFFSET | offset;
122*4882a593Smuzhiyun #endif
123*4882a593Smuzhiyun 				do {
124*4882a593Smuzhiyun #ifdef CONFIG_SDIO_HCI
125*4882a593Smuzhiyun 					if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO)
126*4882a593Smuzhiyun 						value = SdioLocalCmd52Read1Byte(padapter, offset);
127*4882a593Smuzhiyun 					else
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun 						value = rtw_read8(padapter, offset);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 					value = value & GET_PWR_CFG_MASK(PwrCfgCmd);
132*4882a593Smuzhiyun 					if (value == (GET_PWR_CFG_VALUE(PwrCfgCmd) & GET_PWR_CFG_MASK(PwrCfgCmd)))
133*4882a593Smuzhiyun 						bPollingBit = _TRUE;
134*4882a593Smuzhiyun 					else
135*4882a593Smuzhiyun 						rtw_udelay_os(10);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 					if (pollingCount++ > maxPollingCnt) {
138*4882a593Smuzhiyun 						RTW_ERR("HalPwrSeqCmdParsing: Fail to polling Offset[%#x]=%02x\n", offset, value);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 						/* For PCIE + USB package poll power bit timeout issue only modify 8821AE and 8723BE */
141*4882a593Smuzhiyun 						if (bHWICSupport && offset == 0x06  && flag == 0) {
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 							RTW_ERR("[WARNING] PCIE polling(0x%X) timeout(%d), Toggle 0x04[3] and try again.\n", offset, maxPollingCnt);
144*4882a593Smuzhiyun 							if (IS_HARDWARE_TYPE_8723DE(padapter))
145*4882a593Smuzhiyun 								PlatformEFIOWrite1Byte(padapter, 0x40, (PlatformEFIORead1Byte(padapter, 0x40)) & (~BIT3));
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 							PlatformEFIOWrite1Byte(padapter, 0x04, PlatformEFIORead1Byte(padapter, 0x04) | BIT3);
148*4882a593Smuzhiyun 							PlatformEFIOWrite1Byte(padapter, 0x04, PlatformEFIORead1Byte(padapter, 0x04) & ~BIT3);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 							if (IS_HARDWARE_TYPE_8723DE(padapter))
151*4882a593Smuzhiyun 								PlatformEFIOWrite1Byte(padapter, 0x40, PlatformEFIORead1Byte(padapter, 0x40)|BIT3);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 							/* Retry Polling Process one more time */
154*4882a593Smuzhiyun 							pollingCount = 0;
155*4882a593Smuzhiyun 							flag = 1;
156*4882a593Smuzhiyun 						} else {
157*4882a593Smuzhiyun 							return _FALSE;
158*4882a593Smuzhiyun 						}
159*4882a593Smuzhiyun 					}
160*4882a593Smuzhiyun 				} while (!bPollingBit);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 				break;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 			case PWR_CMD_DELAY:
165*4882a593Smuzhiyun 				if (GET_PWR_CFG_VALUE(PwrCfgCmd) == PWRSEQ_DELAY_US)
166*4882a593Smuzhiyun 					rtw_udelay_os(GET_PWR_CFG_OFFSET(PwrCfgCmd));
167*4882a593Smuzhiyun 				else
168*4882a593Smuzhiyun 					rtw_udelay_os(GET_PWR_CFG_OFFSET(PwrCfgCmd) * 1000);
169*4882a593Smuzhiyun 				break;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 			case PWR_CMD_END:
172*4882a593Smuzhiyun 				/* When this command is parsed, end the process */
173*4882a593Smuzhiyun 				return _TRUE;
174*4882a593Smuzhiyun 				break;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 			default:
177*4882a593Smuzhiyun 				break;
178*4882a593Smuzhiyun 			}
179*4882a593Smuzhiyun 		}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 		AryIdx++;/* Add Array Index */
182*4882a593Smuzhiyun 	} while (1);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	return _TRUE;
185*4882a593Smuzhiyun }
186