• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2016 - 2019 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 
16 #include "halmac_type.h"
17 #include "halmac_api.h"
18 
19 #if (HALMAC_PLATFORM_WINDOWS)
20 
21 #if HALMAC_8822B_SUPPORT
22 #include "halmac_88xx/halmac_init_win8822b.h"
23 #endif
24 
25 #if HALMAC_8821C_SUPPORT
26 #include "halmac_88xx/halmac_init_win8821c.h"
27 #endif
28 
29 #if HALMAC_8814B_SUPPORT
30 #include "halmac_88xx_v1/halmac_init_win8814b_v1.h"
31 #endif
32 
33 #if HALMAC_8822C_SUPPORT
34 #include "halmac_88xx/halmac_init_win8822c.h"
35 #endif
36 
37 #if HALMAC_8812F_SUPPORT
38 #include "halmac_88xx/halmac_init_win8812f.h"
39 #endif
40 
41 #else
42 
43 #if HALMAC_88XX_SUPPORT
44 #include "halmac_88xx/halmac_init_88xx.h"
45 #endif
46 #if HALMAC_88XX_V1_SUPPORT
47 #include "halmac_88xx_v1/halmac_init_88xx_v1.h"
48 #if defined(HALMAC_DATA_CPU_EN)
49 #include "halmac_88xxd_v1/halmac_init_88xxd_v1.h"
50 #endif
51 #endif
52 
53 #endif
54 
55 enum chip_id_hw_def {
56 	CHIP_ID_HW_DEF_8723A = 0x01,
57 	CHIP_ID_HW_DEF_8188E = 0x02,
58 	CHIP_ID_HW_DEF_8881A = 0x03,
59 	CHIP_ID_HW_DEF_8812A = 0x04,
60 	CHIP_ID_HW_DEF_8821A = 0x05,
61 	CHIP_ID_HW_DEF_8723B = 0x06,
62 	CHIP_ID_HW_DEF_8192E = 0x07,
63 	CHIP_ID_HW_DEF_8814A = 0x08,
64 	CHIP_ID_HW_DEF_8821C = 0x09,
65 	CHIP_ID_HW_DEF_8822B = 0x0A,
66 	CHIP_ID_HW_DEF_8703B = 0x0B,
67 	CHIP_ID_HW_DEF_8188F = 0x0C,
68 	CHIP_ID_HW_DEF_8192F = 0x0D,
69 	CHIP_ID_HW_DEF_8197F = 0x0E,
70 	CHIP_ID_HW_DEF_8723D = 0x0F,
71 	CHIP_ID_HW_DEF_8814B = 0x11,
72 	CHIP_ID_HW_DEF_8822C = 0x13,
73 	CHIP_ID_HW_DEF_8812F = 0x14,
74 	CHIP_ID_HW_DEF_UNDEFINE = 0x7F,
75 	CHIP_ID_HW_DEF_PS = 0xEA,
76 };
77 
78 static enum halmac_ret_status
79 chk_pltfm_api(void *drv_adapter, enum halmac_interface intf,
80 	      struct halmac_platform_api *pltfm_api);
81 
82 static enum halmac_ret_status
83 get_chip_info(void *drv_adapter, struct halmac_platform_api *pltfm_api,
84 	      enum halmac_interface intf, struct halmac_adapter *adapter);
85 
86 static u8
87 pltfm_reg_r8_sdio(void *drv_adapter, struct halmac_platform_api *pltfm_api,
88 		  u32 offset);
89 
90 static enum halmac_ret_status
91 pltfm_reg_w8_sdio(void *drv_adapter, struct halmac_platform_api *pltfm_api,
92 		  u32 offset, u8 data);
93 
94 static u8
95 pltfm_reg_r_indir_sdio(void *drv_adapter, struct halmac_platform_api *pltfm_api,
96 		       u32 offset);
97 
98 static enum halmac_ret_status
99 cnv_to_sdio_bus_offset(u32 *offset);
100 
101 /**
102  * halmac_init_adapter() - init halmac_adapter
103  * @drv_adapter : the adapter of caller
104  * @pltfm_api : the platform APIs which is used in halmac
105  * @intf : bus interface
106  * @halmac_adapter : the adapter of halmac
107  * @halmac_api : the function pointer of APIs
108  * Author : KaiYuan Chang / Ivan Lin
109  * Return : enum halmac_ret_status
110  * More details of status code can be found in prototype document
111  */
112 enum halmac_ret_status
halmac_init_adapter(void * drv_adapter,struct halmac_platform_api * pltfm_api,enum halmac_interface intf,struct halmac_adapter ** halmac_adapter,struct halmac_api ** halmac_api)113 halmac_init_adapter(void *drv_adapter, struct halmac_platform_api *pltfm_api,
114 		    enum halmac_interface intf,
115 		    struct halmac_adapter **halmac_adapter,
116 		    struct halmac_api **halmac_api)
117 {
118 	struct halmac_adapter *adapter = NULL;
119 	enum halmac_ret_status status = HALMAC_RET_SUCCESS;
120 	u8 *buf = NULL;
121 
122 	union {
123 		u32 i;
124 		u8 x[4];
125 	} ENDIAN_CHECK = { 0x01000000 };
126 
127 	status = chk_pltfm_api(drv_adapter, intf, pltfm_api);
128 	if (status != HALMAC_RET_SUCCESS)
129 		return status;
130 
131 	pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT, HALMAC_DBG_ALWAYS,
132 			     HALMAC_SVN_VER "\n"
133 			     "HALMAC_MAJOR_VER = %d\n"
134 			     "HALMAC_PROTOTYPE_VER = %d\n"
135 			     "HALMAC_MINOR_VER = %d\n"
136 			     "HALMAC_PATCH_VER = %d\n",
137 			     HALMAC_MAJOR_VER, HALMAC_PROTOTYPE_VER,
138 			     HALMAC_MINOR_VER, HALMAC_PATCH_VER);
139 
140 	if (ENDIAN_CHECK.x[0] == HALMAC_SYSTEM_ENDIAN) {
141 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
142 				     HALMAC_DBG_ERR,
143 				     "[ERR]Endian setting err!!\n");
144 		return HALMAC_RET_ENDIAN_ERR;
145 	}
146 
147 	buf = (u8 *)pltfm_api->RTL_MALLOC(drv_adapter, sizeof(*adapter));
148 
149 	if (!buf) {
150 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
151 				     HALMAC_DBG_ERR,
152 				     "[ERR]Malloc HAL adapter err!!\n");
153 		return HALMAC_RET_MALLOC_FAIL;
154 	}
155 	pltfm_api->RTL_MEMSET(drv_adapter, buf, 0x00, sizeof(*adapter));
156 	adapter = (struct halmac_adapter *)buf;
157 
158 	*halmac_adapter = adapter;
159 
160 	adapter->pltfm_api = pltfm_api;
161 	adapter->drv_adapter = drv_adapter;
162 	intf = (intf == HALMAC_INTERFACE_AXI) ? HALMAC_INTERFACE_PCIE : intf;
163 	adapter->intf = intf;
164 
165 	if (get_chip_info(drv_adapter, pltfm_api, intf, adapter)
166 	    != HALMAC_RET_SUCCESS) {
167 		PLTFM_FREE(*halmac_adapter, sizeof(**halmac_adapter));
168 		*halmac_adapter = NULL;
169 		return HALMAC_RET_CHIP_NOT_SUPPORT;
170 	}
171 
172 	PLTFM_MUTEX_INIT(&adapter->efuse_mutex);
173 	PLTFM_MUTEX_INIT(&adapter->h2c_seq_mutex);
174 	PLTFM_MUTEX_INIT(&adapter->sdio_indir_mutex);
175 
176 #if (HALMAC_PLATFORM_WINDOWS == 0)
177 
178 #if HALMAC_88XX_SUPPORT
179 	if (adapter->chip_id == HALMAC_CHIP_ID_8822B ||
180 	    adapter->chip_id == HALMAC_CHIP_ID_8821C ||
181 	    adapter->chip_id == HALMAC_CHIP_ID_8822C ||
182 	    adapter->chip_id == HALMAC_CHIP_ID_8812F) {
183 		init_adapter_param_88xx(adapter);
184 		status = mount_api_88xx(adapter);
185 	}
186 #endif
187 
188 #if HALMAC_88XX_V1_SUPPORT
189 	if (adapter->chip_id == HALMAC_CHIP_ID_8814B) {
190 		init_adapter_param_88xx_v1(adapter);
191 		status = mount_api_88xx_v1(adapter);
192 	}
193 #if defined(HALMAC_DATA_CPU_EN)
194 	if (adapter->chip_id == HALMAC_CHIP_ID_8814B) {
195 		init_adapter_param_88xxd_v1(adapter);
196 		status = mount_api_88xxd_v1(adapter);
197 	}
198 #endif
199 #endif
200 
201 #else
202 
203 #if HALMAC_8822B_SUPPORT
204 	if (adapter->chip_id == HALMAC_CHIP_ID_8822B) {
205 		init_adapter_param_win8822b(adapter);
206 		status = mount_api_win8822b(adapter);
207 	}
208 #endif
209 
210 #if HALMAC_8821C_SUPPORT
211 	if (adapter->chip_id == HALMAC_CHIP_ID_8821C) {
212 		init_adapter_param_win8821c(adapter);
213 		status = mount_api_win8821c(adapter);
214 	}
215 #endif
216 
217 #if HALMAC_8814B_SUPPORT
218 	if (adapter->chip_id == HALMAC_CHIP_ID_8814B) {
219 		init_adapter_param_win8814b_v1(adapter);
220 		status = mount_api_win8814b_v1(adapter);
221 	}
222 #endif
223 
224 #if HALMAC_8822C_SUPPORT
225 	if (adapter->chip_id == HALMAC_CHIP_ID_8822C) {
226 		init_adapter_param_win8822c(adapter);
227 		status = mount_api_win8822c(adapter);
228 	}
229 #endif
230 
231 #if HALMAC_8812F_SUPPORT
232 	if (adapter->chip_id == HALMAC_CHIP_ID_8812F) {
233 		init_adapter_param_win8812f(adapter);
234 		status = mount_api_win8812f(adapter);
235 	}
236 #endif
237 
238 #endif
239 	*halmac_api = (struct halmac_api *)adapter->halmac_api;
240 
241 #if HALMAC_DBG_MONITOR_IO
242 	mount_api_dbg(adapter);
243 #endif
244 	PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
245 
246 	return status;
247 }
248 
249 /**
250  * halmac_halt_api() - stop halmac_api action
251  * @adapter : the adapter of halmac
252  * Author : Ivan Lin
253  * Return : enum halmac_ret_status
254  * More details of status code can be found in prototype document
255  */
256 enum halmac_ret_status
halmac_halt_api(struct halmac_adapter * adapter)257 halmac_halt_api(struct halmac_adapter *adapter)
258 {
259 	PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
260 
261 	adapter->halmac_state.api_state = HALMAC_API_STATE_HALT;
262 
263 	PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
264 
265 	return HALMAC_RET_SUCCESS;
266 }
267 
268 /**
269  * halmac_deinit_adapter() - deinit halmac adapter
270  * @adapter : the adapter of halmac
271  * Author : KaiYuan Chang / Ivan Lin
272  * Return : enum halmac_ret_status
273  * More details of status code can be found in prototype document
274  */
275 enum halmac_ret_status
halmac_deinit_adapter(struct halmac_adapter * adapter)276 halmac_deinit_adapter(struct halmac_adapter *adapter)
277 {
278 	PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
279 
280 	PLTFM_MUTEX_DEINIT(&adapter->efuse_mutex);
281 	PLTFM_MUTEX_DEINIT(&adapter->h2c_seq_mutex);
282 	PLTFM_MUTEX_DEINIT(&adapter->sdio_indir_mutex);
283 
284 	if (adapter->efuse_map) {
285 		PLTFM_FREE(adapter->efuse_map, adapter->hw_cfg_info.efuse_size);
286 		adapter->efuse_map = (u8 *)NULL;
287 	}
288 
289 	if (adapter->sdio_fs.macid_map) {
290 		PLTFM_FREE(adapter->sdio_fs.macid_map,
291 			   adapter->sdio_fs.macid_map_size);
292 		adapter->sdio_fs.macid_map = (u8 *)NULL;
293 	}
294 
295 	if (adapter->halmac_state.psd_state.data) {
296 		PLTFM_FREE(adapter->halmac_state.psd_state.data,
297 			   adapter->halmac_state.psd_state.data_size);
298 		adapter->halmac_state.psd_state.data = (u8 *)NULL;
299 	}
300 
301 	if (adapter->halmac_api) {
302 		PLTFM_FREE(adapter->halmac_api, sizeof(struct halmac_api));
303 		adapter->halmac_api = NULL;
304 	}
305 
306 	PLTFM_FREE(adapter, sizeof(*adapter));
307 
308 	return HALMAC_RET_SUCCESS;
309 }
310 
311 static enum halmac_ret_status
chk_pltfm_api(void * drv_adapter,enum halmac_interface intf,struct halmac_platform_api * pltfm_api)312 chk_pltfm_api(void *drv_adapter, enum halmac_interface intf,
313 	      struct halmac_platform_api *pltfm_api)
314 {
315 	if (!pltfm_api)
316 		return HALMAC_RET_PLATFORM_API_NULL;
317 
318 	if (!pltfm_api->MSG_PRINT)
319 		return HALMAC_RET_PLATFORM_API_NULL;
320 
321 	if (intf == HALMAC_INTERFACE_SDIO) {
322 		if (!pltfm_api->SDIO_CMD52_READ) {
323 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
324 					     HALMAC_DBG_ERR, "[ERR]sdio-r\n");
325 			return HALMAC_RET_PLATFORM_API_NULL;
326 		}
327 		if (!pltfm_api->SDIO_CMD53_READ_8) {
328 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
329 					     HALMAC_DBG_ERR, "[ERR]sdio-r8\n");
330 			return HALMAC_RET_PLATFORM_API_NULL;
331 		}
332 		if (!pltfm_api->SDIO_CMD53_READ_16) {
333 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
334 					     HALMAC_DBG_ERR, "[ERR]sdio-r16\n");
335 			return HALMAC_RET_PLATFORM_API_NULL;
336 		}
337 		if (!pltfm_api->SDIO_CMD53_READ_32) {
338 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
339 					     HALMAC_DBG_ERR, "[ERR]sdio-r32\n");
340 			return HALMAC_RET_PLATFORM_API_NULL;
341 		}
342 		if (!pltfm_api->SDIO_CMD53_READ_N) {
343 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
344 					     HALMAC_DBG_ERR, "[ERR]sdio-rn\n");
345 			return HALMAC_RET_PLATFORM_API_NULL;
346 		}
347 		if (!pltfm_api->SDIO_CMD52_WRITE) {
348 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
349 					     HALMAC_DBG_ERR, "[ERR]sdio-w\n");
350 			return HALMAC_RET_PLATFORM_API_NULL;
351 		}
352 		if (!pltfm_api->SDIO_CMD53_WRITE_8) {
353 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
354 					     HALMAC_DBG_ERR, "[ERR]sdio-w8\n");
355 			return HALMAC_RET_PLATFORM_API_NULL;
356 		}
357 		if (!pltfm_api->SDIO_CMD53_WRITE_16) {
358 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
359 					     HALMAC_DBG_ERR, "[ERR]sdio-w16\n");
360 			return HALMAC_RET_PLATFORM_API_NULL;
361 		}
362 		if (!pltfm_api->SDIO_CMD53_WRITE_32) {
363 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
364 					     HALMAC_DBG_ERR, "[ERR]sdio-w32\n");
365 			return HALMAC_RET_PLATFORM_API_NULL;
366 		}
367 		if (!pltfm_api->SDIO_CMD52_CIA_READ) {
368 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
369 					     HALMAC_DBG_ERR, "[ERR]sdio-cia\n");
370 			return HALMAC_RET_PLATFORM_API_NULL;
371 		}
372 	}
373 
374 	if (intf == HALMAC_INTERFACE_USB || intf == HALMAC_INTERFACE_PCIE) {
375 		if (!pltfm_api->REG_READ_8) {
376 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
377 					     HALMAC_DBG_ERR, "[ERR]reg-r8\n");
378 			return HALMAC_RET_PLATFORM_API_NULL;
379 		}
380 		if (!pltfm_api->REG_READ_16) {
381 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
382 					     HALMAC_DBG_ERR, "[ERR]reg-r16\n");
383 			return HALMAC_RET_PLATFORM_API_NULL;
384 		}
385 		if (!pltfm_api->REG_READ_32) {
386 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
387 					     HALMAC_DBG_ERR, "[ERR]reg-r32\n");
388 			return HALMAC_RET_PLATFORM_API_NULL;
389 		}
390 		if (!pltfm_api->REG_WRITE_8) {
391 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
392 					     HALMAC_DBG_ERR, "[ERR]reg-w8\n");
393 			return HALMAC_RET_PLATFORM_API_NULL;
394 		}
395 		if (!pltfm_api->REG_WRITE_16) {
396 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
397 					     HALMAC_DBG_ERR, "[ERR]reg-w16\n");
398 			return HALMAC_RET_PLATFORM_API_NULL;
399 		}
400 		if (!pltfm_api->REG_WRITE_32) {
401 			pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
402 					     HALMAC_DBG_ERR, "[ERR]reg-w32\n");
403 			return HALMAC_RET_PLATFORM_API_NULL;
404 		}
405 	}
406 
407 	if (!pltfm_api->RTL_FREE) {
408 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
409 				     HALMAC_DBG_ERR, "[ERR]mem-free\n");
410 		return HALMAC_RET_PLATFORM_API_NULL;
411 	}
412 
413 	if (!pltfm_api->RTL_MALLOC) {
414 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
415 				     HALMAC_DBG_ERR, "[ERR]mem-malloc\n");
416 		return HALMAC_RET_PLATFORM_API_NULL;
417 	}
418 	if (!pltfm_api->RTL_MEMCPY) {
419 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
420 				     HALMAC_DBG_ERR, "[ERR]mem-cpy\n");
421 		return HALMAC_RET_PLATFORM_API_NULL;
422 	}
423 	if (!pltfm_api->RTL_MEMSET) {
424 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
425 				     HALMAC_DBG_ERR, "[ERR]mem-set\n");
426 		return HALMAC_RET_PLATFORM_API_NULL;
427 	}
428 	if (!pltfm_api->RTL_DELAY_US) {
429 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
430 				     HALMAC_DBG_ERR, "[ERR]time-delay\n");
431 		return HALMAC_RET_PLATFORM_API_NULL;
432 	}
433 
434 	if (!pltfm_api->MUTEX_INIT) {
435 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
436 				     HALMAC_DBG_ERR, "[ERR]mutex-init\n");
437 		return HALMAC_RET_PLATFORM_API_NULL;
438 	}
439 	if (!pltfm_api->MUTEX_DEINIT) {
440 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
441 				     HALMAC_DBG_ERR, "[ERR]mutex-deinit\n");
442 		return HALMAC_RET_PLATFORM_API_NULL;
443 	}
444 	if (!pltfm_api->MUTEX_LOCK) {
445 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
446 				     HALMAC_DBG_ERR, "[ERR]mutex-lock\n");
447 		return HALMAC_RET_PLATFORM_API_NULL;
448 	}
449 	if (!pltfm_api->MUTEX_UNLOCK) {
450 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
451 				     HALMAC_DBG_ERR, "[ERR]mutex-unlock\n");
452 		return HALMAC_RET_PLATFORM_API_NULL;
453 	}
454 	if (!pltfm_api->EVENT_INDICATION) {
455 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
456 				     HALMAC_DBG_ERR, "[ERR]event-indication\n");
457 		return HALMAC_RET_PLATFORM_API_NULL;
458 	}
459 #if HALMAC_DBG_MONITOR_IO
460 	if (!pltfm_api->READ_MONITOR) {
461 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
462 				     HALMAC_DBG_ERR, "[ERR]read-monitor\n");
463 		return HALMAC_RET_PLATFORM_API_NULL;
464 	}
465 	if (!pltfm_api->WRITE_MONITOR) {
466 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
467 				     HALMAC_DBG_ERR, "[ERR]write-monitor\n");
468 		return HALMAC_RET_PLATFORM_API_NULL;
469 	}
470 #endif
471 	return HALMAC_RET_SUCCESS;
472 }
473 
474 /**
475  * halmac_get_version() - get HALMAC version
476  * @version : return version of major, prototype, minor and patch information
477  * Author : KaiYuan Chang / Ivan Lin
478  * Return : enum halmac_ret_status
479  * More details of status code can be found in prototype document
480  */
481 enum halmac_ret_status
halmac_get_version(struct halmac_ver * version)482 halmac_get_version(struct halmac_ver *version)
483 {
484 	version->major_ver = (u8)HALMAC_MAJOR_VER;
485 	version->prototype_ver = (u8)HALMAC_PROTOTYPE_VER;
486 	version->minor_ver = (u8)HALMAC_MINOR_VER;
487 	version->patch_ver = (u8)HALMAC_PATCH_VER;
488 
489 	return HALMAC_RET_SUCCESS;
490 }
491 
492 static enum halmac_ret_status
get_chip_info(void * drv_adapter,struct halmac_platform_api * pltfm_api,enum halmac_interface intf,struct halmac_adapter * adapter)493 get_chip_info(void *drv_adapter, struct halmac_platform_api *pltfm_api,
494 	      enum halmac_interface intf, struct halmac_adapter *adapter)
495 {
496 	u8 chip_id;
497 	u8 chip_ver;
498 	u32 cnt;
499 
500 	if (adapter->intf == HALMAC_INTERFACE_SDIO) {
501 		pltfm_reg_w8_sdio(drv_adapter, pltfm_api, REG_SDIO_HSUS_CTRL,
502 				  pltfm_reg_r8_sdio(drv_adapter, pltfm_api,
503 						    REG_SDIO_HSUS_CTRL) &
504 						    ~(BIT(0)));
505 
506 		cnt = 10000;
507 		while (!(pltfm_reg_r8_sdio(drv_adapter, pltfm_api,
508 					   REG_SDIO_HSUS_CTRL) & BIT(1))) {
509 			cnt--;
510 			if (cnt == 0)
511 				return HALMAC_RET_SDIO_LEAVE_SUSPEND_FAIL;
512 		}
513 
514 		chip_id = pltfm_reg_r_indir_sdio(drv_adapter, pltfm_api,
515 						 REG_SYS_CFG2);
516 		chip_ver = pltfm_reg_r_indir_sdio(drv_adapter, pltfm_api,
517 						  REG_SYS_CFG1 + 1) >> 4;
518 	} else {
519 		chip_id = pltfm_api->REG_READ_8(drv_adapter, REG_SYS_CFG2);
520 		chip_ver = pltfm_api->REG_READ_8(drv_adapter,
521 						 REG_SYS_CFG1 + 1) >> 4;
522 	}
523 
524 	adapter->chip_ver = (enum halmac_chip_ver)chip_ver;
525 
526 	if (chip_id == CHIP_ID_HW_DEF_8822B) {
527 		adapter->chip_id = HALMAC_CHIP_ID_8822B;
528 	} else if (chip_id == CHIP_ID_HW_DEF_8821C) {
529 		adapter->chip_id = HALMAC_CHIP_ID_8821C;
530 	} else if (chip_id == CHIP_ID_HW_DEF_8814B) {
531 		adapter->chip_id = HALMAC_CHIP_ID_8814B;
532 	} else if (chip_id == CHIP_ID_HW_DEF_8197F) {
533 		adapter->chip_id = HALMAC_CHIP_ID_8197F;
534 	} else if (chip_id == CHIP_ID_HW_DEF_8822C) {
535 		adapter->chip_id = HALMAC_CHIP_ID_8822C;
536 	} else if (chip_id == CHIP_ID_HW_DEF_8812F) {
537 		adapter->chip_id = HALMAC_CHIP_ID_8812F;
538 	} else {
539 		adapter->chip_id = HALMAC_CHIP_ID_UNDEFINE;
540 		PLTFM_MSG_ERR("[ERR]Chip id is undefined\n");
541 		return HALMAC_RET_CHIP_NOT_SUPPORT;
542 	}
543 
544 	return HALMAC_RET_SUCCESS;
545 }
546 
547 static u8
pltfm_reg_r8_sdio(void * drv_adapter,struct halmac_platform_api * pltfm_api,u32 offset)548 pltfm_reg_r8_sdio(void *drv_adapter, struct halmac_platform_api *pltfm_api,
549 		  u32 offset)
550 {
551 	u8 value8;
552 	enum halmac_ret_status status = HALMAC_RET_SUCCESS;
553 
554 	if (0 == (offset & 0xFFFF0000))
555 		offset |= WLAN_IOREG_OFFSET;
556 
557 	status = cnv_to_sdio_bus_offset(&offset);
558 	if (status != HALMAC_RET_SUCCESS)
559 		return status;
560 
561 	value8 = pltfm_api->SDIO_CMD52_READ(drv_adapter, offset);
562 
563 	return value8;
564 }
565 
566 static enum halmac_ret_status
pltfm_reg_w8_sdio(void * drv_adapter,struct halmac_platform_api * pltfm_api,u32 offset,u8 data)567 pltfm_reg_w8_sdio(void *drv_adapter, struct halmac_platform_api *pltfm_api,
568 		  u32 offset, u8 data)
569 {
570 	enum halmac_ret_status status = HALMAC_RET_SUCCESS;
571 
572 	if (0 == (offset & 0xFFFF0000))
573 		offset |= WLAN_IOREG_OFFSET;
574 
575 	status = cnv_to_sdio_bus_offset(&offset);
576 
577 	if (status != HALMAC_RET_SUCCESS)
578 		return status;
579 
580 	pltfm_api->SDIO_CMD52_WRITE(drv_adapter, offset, data);
581 
582 	return HALMAC_RET_SUCCESS;
583 }
584 
585 static u8
pltfm_reg_r_indir_sdio(void * drv_adapter,struct halmac_platform_api * pltfm_api,u32 offset)586 pltfm_reg_r_indir_sdio(void *drv_adapter, struct halmac_platform_api *pltfm_api,
587 		       u32 offset)
588 {
589 	u8 value8, tmp, cnt = 50;
590 	u32 reg_cfg = REG_SDIO_INDIRECT_REG_CFG;
591 	u32 reg_data = REG_SDIO_INDIRECT_REG_DATA;
592 	enum halmac_ret_status status = HALMAC_RET_SUCCESS;
593 
594 	status = cnv_to_sdio_bus_offset(&reg_cfg);
595 	if (status != HALMAC_RET_SUCCESS)
596 		return status;
597 	status = cnv_to_sdio_bus_offset(&reg_data);
598 	if (status != HALMAC_RET_SUCCESS)
599 		return status;
600 
601 	pltfm_api->SDIO_CMD52_WRITE(drv_adapter, reg_cfg, (u8)offset);
602 	pltfm_api->SDIO_CMD52_WRITE(drv_adapter, reg_cfg + 1,
603 				    (u8)(offset >> 8));
604 	pltfm_api->SDIO_CMD52_WRITE(drv_adapter, reg_cfg + 2,
605 				    (u8)(BIT(3) | BIT(4)));
606 
607 	do {
608 		tmp = pltfm_api->SDIO_CMD52_READ(drv_adapter, reg_cfg + 2);
609 		cnt--;
610 	} while (((tmp & BIT(4)) == 0) && (cnt > 0));
611 
612 	if (((cnt & BIT(4)) == 0) && cnt == 0)
613 		pltfm_api->MSG_PRINT(drv_adapter, HALMAC_MSG_INIT,
614 				     HALMAC_DBG_ERR, "[ERR]sdio indir read\n");
615 
616 	value8 = pltfm_api->SDIO_CMD52_READ(drv_adapter, reg_data);
617 
618 	return value8;
619 }
620 
621 /*Note: copy from cnv_to_sdio_bus_offset_88xx*/
622 static enum halmac_ret_status
cnv_to_sdio_bus_offset(u32 * offset)623 cnv_to_sdio_bus_offset(u32 *offset)
624 {
625 	switch ((*offset) & 0xFFFF0000) {
626 	case WLAN_IOREG_OFFSET:
627 		*offset &= HALMAC_WLAN_MAC_REG_MSK;
628 		*offset |= HALMAC_SDIO_CMD_ADDR_MAC_REG << 13;
629 		break;
630 	case SDIO_LOCAL_OFFSET:
631 		*offset &= HALMAC_SDIO_LOCAL_MSK;
632 		*offset |= HALMAC_SDIO_CMD_ADDR_SDIO_REG << 13;
633 		break;
634 	default:
635 		*offset = 0xFFFFFFFF;
636 		return HALMAC_RET_CONVERT_SDIO_OFFSET_FAIL;
637 	}
638 
639 	return HALMAC_RET_SUCCESS;
640 }
641 
642