xref: /linux/drivers/net/wireless/ath/ath6kl/init.c (revision e76ac2bf637defbe3b7fc644813be584b941ff0a)
1bdcd8170SKalle Valo 
2bdcd8170SKalle Valo /*
3bdcd8170SKalle Valo  * Copyright (c) 2011 Atheros Communications Inc.
41b2df407SVasanthakumar Thiagarajan  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
5bdcd8170SKalle Valo  *
6bdcd8170SKalle Valo  * Permission to use, copy, modify, and/or distribute this software for any
7bdcd8170SKalle Valo  * purpose with or without fee is hereby granted, provided that the above
8bdcd8170SKalle Valo  * copyright notice and this permission notice appear in all copies.
9bdcd8170SKalle Valo  *
10bdcd8170SKalle Valo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11bdcd8170SKalle Valo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12bdcd8170SKalle Valo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13bdcd8170SKalle Valo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14bdcd8170SKalle Valo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15bdcd8170SKalle Valo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16bdcd8170SKalle Valo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17bdcd8170SKalle Valo  */
18bdcd8170SKalle Valo 
19c6efe578SStephen Rothwell #include <linux/moduleparam.h>
20f7830202SSangwook Lee #include <linux/errno.h>
21d6a434d6SKalle Valo #include <linux/export.h>
2292ecbff4SSam Leffler #include <linux/of.h>
23bdcd8170SKalle Valo #include <linux/mmc/sdio_func.h>
24d6a434d6SKalle Valo 
25bdcd8170SKalle Valo #include "core.h"
26bdcd8170SKalle Valo #include "cfg80211.h"
27bdcd8170SKalle Valo #include "target.h"
28bdcd8170SKalle Valo #include "debug.h"
29bdcd8170SKalle Valo #include "hif-ops.h"
30*e76ac2bfSKalle Valo #include "htc-ops.h"
31bdcd8170SKalle Valo 
32856f4b31SKalle Valo static const struct ath6kl_hw hw_list[] = {
33856f4b31SKalle Valo 	{
340d0192baSKalle Valo 		.id				= AR6003_HW_2_0_VERSION,
35293badf4SKalle Valo 		.name				= "ar6003 hw 2.0",
36856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
37856f4b31SKalle Valo 		.app_load_addr			= 0x543180,
38856f4b31SKalle Valo 		.board_ext_data_addr		= 0x57e500,
39856f4b31SKalle Valo 		.reserved_ram_size		= 6912,
4039586bf2SRyan Hsu 		.refclk_hz			= 26000000,
4139586bf2SRyan Hsu 		.uarttx_pin			= 8,
42856f4b31SKalle Valo 
43856f4b31SKalle Valo 		/* hw2.0 needs override address hardcoded */
44856f4b31SKalle Valo 		.app_start_override_addr	= 0x944C00,
45d1a9421dSKalle Valo 
46c0038972SKalle Valo 		.fw = {
47c0038972SKalle Valo 			.dir		= AR6003_HW_2_0_FW_DIR,
48c0038972SKalle Valo 			.otp		= AR6003_HW_2_0_OTP_FILE,
49d1a9421dSKalle Valo 			.fw		= AR6003_HW_2_0_FIRMWARE_FILE,
50c0038972SKalle Valo 			.tcmd		= AR6003_HW_2_0_TCMD_FIRMWARE_FILE,
51c0038972SKalle Valo 			.patch		= AR6003_HW_2_0_PATCH_FILE,
52c0038972SKalle Valo 		},
53c0038972SKalle Valo 
54d1a9421dSKalle Valo 		.fw_board		= AR6003_HW_2_0_BOARD_DATA_FILE,
55d1a9421dSKalle Valo 		.fw_default_board	= AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE,
56856f4b31SKalle Valo 	},
57856f4b31SKalle Valo 	{
580d0192baSKalle Valo 		.id				= AR6003_HW_2_1_1_VERSION,
59293badf4SKalle Valo 		.name				= "ar6003 hw 2.1.1",
60856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57ff74,
61856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
62856f4b31SKalle Valo 		.board_ext_data_addr		= 0x542330,
63856f4b31SKalle Valo 		.reserved_ram_size		= 512,
6439586bf2SRyan Hsu 		.refclk_hz			= 26000000,
6539586bf2SRyan Hsu 		.uarttx_pin			= 8,
66cd23c1c9SAlex Yang 		.testscript_addr		= 0x57ef74,
67d1a9421dSKalle Valo 
68c0038972SKalle Valo 		.fw = {
69c0038972SKalle Valo 			.dir		= AR6003_HW_2_1_1_FW_DIR,
70c0038972SKalle Valo 			.otp		= AR6003_HW_2_1_1_OTP_FILE,
71d1a9421dSKalle Valo 			.fw		= AR6003_HW_2_1_1_FIRMWARE_FILE,
72c0038972SKalle Valo 			.tcmd		= AR6003_HW_2_1_1_TCMD_FIRMWARE_FILE,
73c0038972SKalle Valo 			.patch		= AR6003_HW_2_1_1_PATCH_FILE,
74cd23c1c9SAlex Yang 			.utf		= AR6003_HW_2_1_1_UTF_FIRMWARE_FILE,
75cd23c1c9SAlex Yang 			.testscript	= AR6003_HW_2_1_1_TESTSCRIPT_FILE,
76c0038972SKalle Valo 		},
77c0038972SKalle Valo 
78d1a9421dSKalle Valo 		.fw_board		= AR6003_HW_2_1_1_BOARD_DATA_FILE,
79d1a9421dSKalle Valo 		.fw_default_board = AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE,
80856f4b31SKalle Valo 	},
81856f4b31SKalle Valo 	{
820d0192baSKalle Valo 		.id				= AR6004_HW_1_0_VERSION,
83293badf4SKalle Valo 		.name				= "ar6004 hw 1.0",
84856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
85856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
86856f4b31SKalle Valo 		.board_ext_data_addr		= 0x437000,
87856f4b31SKalle Valo 		.reserved_ram_size		= 19456,
880d4d72bfSKalle Valo 		.board_addr			= 0x433900,
8939586bf2SRyan Hsu 		.refclk_hz			= 26000000,
9039586bf2SRyan Hsu 		.uarttx_pin			= 11,
91d1a9421dSKalle Valo 
92c0038972SKalle Valo 		.fw = {
93c0038972SKalle Valo 			.dir		= AR6004_HW_1_0_FW_DIR,
94d1a9421dSKalle Valo 			.fw		= AR6004_HW_1_0_FIRMWARE_FILE,
95c0038972SKalle Valo 		},
96c0038972SKalle Valo 
97d1a9421dSKalle Valo 		.fw_board		= AR6004_HW_1_0_BOARD_DATA_FILE,
98d1a9421dSKalle Valo 		.fw_default_board	= AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE,
99856f4b31SKalle Valo 	},
100856f4b31SKalle Valo 	{
1010d0192baSKalle Valo 		.id				= AR6004_HW_1_1_VERSION,
102293badf4SKalle Valo 		.name				= "ar6004 hw 1.1",
103856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
104856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
105856f4b31SKalle Valo 		.board_ext_data_addr		= 0x437000,
106856f4b31SKalle Valo 		.reserved_ram_size		= 11264,
1070d4d72bfSKalle Valo 		.board_addr			= 0x43d400,
10839586bf2SRyan Hsu 		.refclk_hz			= 40000000,
10939586bf2SRyan Hsu 		.uarttx_pin			= 11,
110d1a9421dSKalle Valo 
111c0038972SKalle Valo 		.fw = {
112c0038972SKalle Valo 			.dir		= AR6004_HW_1_1_FW_DIR,
113d1a9421dSKalle Valo 			.fw		= AR6004_HW_1_1_FIRMWARE_FILE,
114c0038972SKalle Valo 		},
115c0038972SKalle Valo 
116d1a9421dSKalle Valo 		.fw_board		= AR6004_HW_1_1_BOARD_DATA_FILE,
117d1a9421dSKalle Valo 		.fw_default_board	= AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE,
118856f4b31SKalle Valo 	},
119856f4b31SKalle Valo };
120856f4b31SKalle Valo 
121bdcd8170SKalle Valo /*
122bdcd8170SKalle Valo  * Include definitions here that can be used to tune the WLAN module
123bdcd8170SKalle Valo  * behavior. Different customers can tune the behavior as per their needs,
124bdcd8170SKalle Valo  * here.
125bdcd8170SKalle Valo  */
126bdcd8170SKalle Valo 
127bdcd8170SKalle Valo /*
128bdcd8170SKalle Valo  * This configuration item enable/disable keepalive support.
129bdcd8170SKalle Valo  * Keepalive support: In the absence of any data traffic to AP, null
130bdcd8170SKalle Valo  * frames will be sent to the AP at periodic interval, to keep the association
131bdcd8170SKalle Valo  * active. This configuration item defines the periodic interval.
132bdcd8170SKalle Valo  * Use value of zero to disable keepalive support
133bdcd8170SKalle Valo  * Default: 60 seconds
134bdcd8170SKalle Valo  */
135bdcd8170SKalle Valo #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
136bdcd8170SKalle Valo 
137bdcd8170SKalle Valo /*
138bdcd8170SKalle Valo  * This configuration item sets the value of disconnect timeout
139bdcd8170SKalle Valo  * Firmware delays sending the disconnec event to the host for this
140bdcd8170SKalle Valo  * timeout after is gets disconnected from the current AP.
141bdcd8170SKalle Valo  * If the firmware successly roams within the disconnect timeout
142bdcd8170SKalle Valo  * it sends a new connect event
143bdcd8170SKalle Valo  */
144bdcd8170SKalle Valo #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
145bdcd8170SKalle Valo 
146bdcd8170SKalle Valo 
147bdcd8170SKalle Valo #define ATH6KL_DATA_OFFSET    64
148bdcd8170SKalle Valo struct sk_buff *ath6kl_buf_alloc(int size)
149bdcd8170SKalle Valo {
150bdcd8170SKalle Valo 	struct sk_buff *skb;
151bdcd8170SKalle Valo 	u16 reserved;
152bdcd8170SKalle Valo 
153bdcd8170SKalle Valo 	/* Add chacheline space at front and back of buffer */
154bdcd8170SKalle Valo 	reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
1551df94a85SVasanthakumar Thiagarajan 		   sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
156bdcd8170SKalle Valo 	skb = dev_alloc_skb(size + reserved);
157bdcd8170SKalle Valo 
158bdcd8170SKalle Valo 	if (skb)
159bdcd8170SKalle Valo 		skb_reserve(skb, reserved - L1_CACHE_BYTES);
160bdcd8170SKalle Valo 	return skb;
161bdcd8170SKalle Valo }
162bdcd8170SKalle Valo 
163e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_profile_info(struct ath6kl_vif *vif)
164bdcd8170SKalle Valo {
1653450334fSVasanthakumar Thiagarajan 	vif->ssid_len = 0;
1663450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
1673450334fSVasanthakumar Thiagarajan 
1683450334fSVasanthakumar Thiagarajan 	vif->dot11_auth_mode = OPEN_AUTH;
1693450334fSVasanthakumar Thiagarajan 	vif->auth_mode = NONE_AUTH;
1703450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto = NONE_CRYPT;
1713450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto_len = 0;
1723450334fSVasanthakumar Thiagarajan 	vif->grp_crypto = NONE_CRYPT;
1733450334fSVasanthakumar Thiagarajan 	vif->grp_crypto_len = 0;
1746f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
1758c8b65e3SVasanthakumar Thiagarajan 	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
1768c8b65e3SVasanthakumar Thiagarajan 	memset(vif->bssid, 0, sizeof(vif->bssid));
177f74bac54SVasanthakumar Thiagarajan 	vif->bss_ch = 0;
178bdcd8170SKalle Valo }
179bdcd8170SKalle Valo 
180bdcd8170SKalle Valo static int ath6kl_set_host_app_area(struct ath6kl *ar)
181bdcd8170SKalle Valo {
182bdcd8170SKalle Valo 	u32 address, data;
183bdcd8170SKalle Valo 	struct host_app_area host_app_area;
184bdcd8170SKalle Valo 
185bdcd8170SKalle Valo 	/* Fetch the address of the host_app_area_s
186bdcd8170SKalle Valo 	 * instance in the host interest area */
187bdcd8170SKalle Valo 	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
18831024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, address);
189bdcd8170SKalle Valo 
190addb44beSKalle Valo 	if (ath6kl_diag_read32(ar, address, &data))
191bdcd8170SKalle Valo 		return -EIO;
192bdcd8170SKalle Valo 
19331024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, data);
194cbf49a6fSKalle Valo 	host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
195addb44beSKalle Valo 	if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
196addb44beSKalle Valo 			      sizeof(struct host_app_area)))
197bdcd8170SKalle Valo 		return -EIO;
198bdcd8170SKalle Valo 
199bdcd8170SKalle Valo 	return 0;
200bdcd8170SKalle Valo }
201bdcd8170SKalle Valo 
202bdcd8170SKalle Valo static inline void set_ac2_ep_map(struct ath6kl *ar,
203bdcd8170SKalle Valo 				  u8 ac,
204bdcd8170SKalle Valo 				  enum htc_endpoint_id ep)
205bdcd8170SKalle Valo {
206bdcd8170SKalle Valo 	ar->ac2ep_map[ac] = ep;
207bdcd8170SKalle Valo 	ar->ep2ac_map[ep] = ac;
208bdcd8170SKalle Valo }
209bdcd8170SKalle Valo 
210bdcd8170SKalle Valo /* connect to a service */
211bdcd8170SKalle Valo static int ath6kl_connectservice(struct ath6kl *ar,
212bdcd8170SKalle Valo 				 struct htc_service_connect_req  *con_req,
213bdcd8170SKalle Valo 				 char *desc)
214bdcd8170SKalle Valo {
215bdcd8170SKalle Valo 	int status;
216bdcd8170SKalle Valo 	struct htc_service_connect_resp response;
217bdcd8170SKalle Valo 
218bdcd8170SKalle Valo 	memset(&response, 0, sizeof(response));
219bdcd8170SKalle Valo 
220ad226ec2SKalle Valo 	status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
221bdcd8170SKalle Valo 	if (status) {
222bdcd8170SKalle Valo 		ath6kl_err("failed to connect to %s service status:%d\n",
223bdcd8170SKalle Valo 			   desc, status);
224bdcd8170SKalle Valo 		return status;
225bdcd8170SKalle Valo 	}
226bdcd8170SKalle Valo 
227bdcd8170SKalle Valo 	switch (con_req->svc_id) {
228bdcd8170SKalle Valo 	case WMI_CONTROL_SVC:
229bdcd8170SKalle Valo 		if (test_bit(WMI_ENABLED, &ar->flag))
230bdcd8170SKalle Valo 			ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
231bdcd8170SKalle Valo 		ar->ctrl_ep = response.endpoint;
232bdcd8170SKalle Valo 		break;
233bdcd8170SKalle Valo 	case WMI_DATA_BE_SVC:
234bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
235bdcd8170SKalle Valo 		break;
236bdcd8170SKalle Valo 	case WMI_DATA_BK_SVC:
237bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
238bdcd8170SKalle Valo 		break;
239bdcd8170SKalle Valo 	case WMI_DATA_VI_SVC:
240bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
241bdcd8170SKalle Valo 		break;
242bdcd8170SKalle Valo 	case WMI_DATA_VO_SVC:
243bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
244bdcd8170SKalle Valo 		break;
245bdcd8170SKalle Valo 	default:
246bdcd8170SKalle Valo 		ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
247bdcd8170SKalle Valo 		return -EINVAL;
248bdcd8170SKalle Valo 	}
249bdcd8170SKalle Valo 
250bdcd8170SKalle Valo 	return 0;
251bdcd8170SKalle Valo }
252bdcd8170SKalle Valo 
253bdcd8170SKalle Valo static int ath6kl_init_service_ep(struct ath6kl *ar)
254bdcd8170SKalle Valo {
255bdcd8170SKalle Valo 	struct htc_service_connect_req connect;
256bdcd8170SKalle Valo 
257bdcd8170SKalle Valo 	memset(&connect, 0, sizeof(connect));
258bdcd8170SKalle Valo 
259bdcd8170SKalle Valo 	/* these fields are the same for all service endpoints */
260900d6b3fSKalle Valo 	connect.ep_cb.tx_comp_multi = ath6kl_tx_complete;
261bdcd8170SKalle Valo 	connect.ep_cb.rx = ath6kl_rx;
262bdcd8170SKalle Valo 	connect.ep_cb.rx_refill = ath6kl_rx_refill;
263bdcd8170SKalle Valo 	connect.ep_cb.tx_full = ath6kl_tx_queue_full;
264bdcd8170SKalle Valo 
265bdcd8170SKalle Valo 	/*
266bdcd8170SKalle Valo 	 * Set the max queue depth so that our ath6kl_tx_queue_full handler
267bdcd8170SKalle Valo 	 * gets called.
268bdcd8170SKalle Valo 	*/
269bdcd8170SKalle Valo 	connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
270bdcd8170SKalle Valo 	connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
271bdcd8170SKalle Valo 	if (!connect.ep_cb.rx_refill_thresh)
272bdcd8170SKalle Valo 		connect.ep_cb.rx_refill_thresh++;
273bdcd8170SKalle Valo 
274bdcd8170SKalle Valo 	/* connect to control service */
275bdcd8170SKalle Valo 	connect.svc_id = WMI_CONTROL_SVC;
276bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
277bdcd8170SKalle Valo 		return -EIO;
278bdcd8170SKalle Valo 
279bdcd8170SKalle Valo 	connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
280bdcd8170SKalle Valo 
281bdcd8170SKalle Valo 	/*
282bdcd8170SKalle Valo 	 * Limit the HTC message size on the send path, although e can
283bdcd8170SKalle Valo 	 * receive A-MSDU frames of 4K, we will only send ethernet-sized
284bdcd8170SKalle Valo 	 * (802.3) frames on the send path.
285bdcd8170SKalle Valo 	 */
286bdcd8170SKalle Valo 	connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
287bdcd8170SKalle Valo 
288bdcd8170SKalle Valo 	/*
289bdcd8170SKalle Valo 	 * To reduce the amount of committed memory for larger A_MSDU
290bdcd8170SKalle Valo 	 * frames, use the recv-alloc threshold mechanism for larger
291bdcd8170SKalle Valo 	 * packets.
292bdcd8170SKalle Valo 	 */
293bdcd8170SKalle Valo 	connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
294bdcd8170SKalle Valo 	connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
295bdcd8170SKalle Valo 
296bdcd8170SKalle Valo 	/*
297bdcd8170SKalle Valo 	 * For the remaining data services set the connection flag to
298bdcd8170SKalle Valo 	 * reduce dribbling, if configured to do so.
299bdcd8170SKalle Valo 	 */
300bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
301bdcd8170SKalle Valo 	connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
302bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
303bdcd8170SKalle Valo 
304bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BE_SVC;
305bdcd8170SKalle Valo 
306bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
307bdcd8170SKalle Valo 		return -EIO;
308bdcd8170SKalle Valo 
309bdcd8170SKalle Valo 	/* connect to back-ground map this to WMI LOW_PRI */
310bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BK_SVC;
311bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
312bdcd8170SKalle Valo 		return -EIO;
313bdcd8170SKalle Valo 
314bdcd8170SKalle Valo 	/* connect to Video service, map this to to HI PRI */
315bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VI_SVC;
316bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
317bdcd8170SKalle Valo 		return -EIO;
318bdcd8170SKalle Valo 
319bdcd8170SKalle Valo 	/*
320bdcd8170SKalle Valo 	 * Connect to VO service, this is currently not mapped to a WMI
321bdcd8170SKalle Valo 	 * priority stream due to historical reasons. WMI originally
322bdcd8170SKalle Valo 	 * defined 3 priorities over 3 mailboxes We can change this when
323bdcd8170SKalle Valo 	 * WMI is reworked so that priorities are not dependent on
324bdcd8170SKalle Valo 	 * mailboxes.
325bdcd8170SKalle Valo 	 */
326bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VO_SVC;
327bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
328bdcd8170SKalle Valo 		return -EIO;
329bdcd8170SKalle Valo 
330bdcd8170SKalle Valo 	return 0;
331bdcd8170SKalle Valo }
332bdcd8170SKalle Valo 
333e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_control_info(struct ath6kl_vif *vif)
334bdcd8170SKalle Valo {
335e29f25f5SVasanthakumar Thiagarajan 	ath6kl_init_profile_info(vif);
3363450334fSVasanthakumar Thiagarajan 	vif->def_txkey_index = 0;
3376f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
338f74bac54SVasanthakumar Thiagarajan 	vif->ch_hint = 0;
339bdcd8170SKalle Valo }
340bdcd8170SKalle Valo 
341bdcd8170SKalle Valo /*
342bdcd8170SKalle Valo  * Set HTC/Mbox operational parameters, this can only be called when the
343bdcd8170SKalle Valo  * target is in the BMI phase.
344bdcd8170SKalle Valo  */
345bdcd8170SKalle Valo static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
346bdcd8170SKalle Valo 				 u8 htc_ctrl_buf)
347bdcd8170SKalle Valo {
348bdcd8170SKalle Valo 	int status;
349bdcd8170SKalle Valo 	u32 blk_size;
350bdcd8170SKalle Valo 
351bdcd8170SKalle Valo 	blk_size = ar->mbox_info.block_size;
352bdcd8170SKalle Valo 
353bdcd8170SKalle Valo 	if (htc_ctrl_buf)
354bdcd8170SKalle Valo 		blk_size |=  ((u32)htc_ctrl_buf) << 16;
355bdcd8170SKalle Valo 
356bdcd8170SKalle Valo 	/* set the host interest area for the block size */
35724fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_mbox_io_block_sz, blk_size);
358bdcd8170SKalle Valo 	if (status) {
359bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for IO block size failed\n");
360bdcd8170SKalle Valo 		goto out;
361bdcd8170SKalle Valo 	}
362bdcd8170SKalle Valo 
363bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
364bdcd8170SKalle Valo 		   blk_size,
365bdcd8170SKalle Valo 		   ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
366bdcd8170SKalle Valo 
367bdcd8170SKalle Valo 	if (mbox_isr_yield_val) {
368bdcd8170SKalle Valo 		/* set the host interest area for the mbox ISR yield limit */
36924fc32b3SKalle Valo 		status = ath6kl_bmi_write_hi32(ar, hi_mbox_isr_yield_limit,
37024fc32b3SKalle Valo 					       mbox_isr_yield_val);
371bdcd8170SKalle Valo 		if (status) {
372bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for yield limit failed\n");
373bdcd8170SKalle Valo 			goto out;
374bdcd8170SKalle Valo 		}
375bdcd8170SKalle Valo 	}
376bdcd8170SKalle Valo 
377bdcd8170SKalle Valo out:
378bdcd8170SKalle Valo 	return status;
379bdcd8170SKalle Valo }
380bdcd8170SKalle Valo 
3810ce59445SVasanthakumar Thiagarajan static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
382bdcd8170SKalle Valo {
3834dea08e0SJouni Malinen 	int ret;
384bdcd8170SKalle Valo 
385bdcd8170SKalle Valo 	/*
386bdcd8170SKalle Valo 	 * Configure the device for rx dot11 header rules. "0,0" are the
387bdcd8170SKalle Valo 	 * default values. Required if checksum offload is needed. Set
388bdcd8170SKalle Valo 	 * RxMetaVersion to 2.
389bdcd8170SKalle Valo 	 */
3901ca4d0b6SKalle Valo 	ret = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
3911ca4d0b6SKalle Valo 						 ar->rx_meta_ver, 0, 0);
3921ca4d0b6SKalle Valo 	if (ret) {
3931ca4d0b6SKalle Valo 		ath6kl_err("unable to set the rx frame format: %d\n", ret);
3941ca4d0b6SKalle Valo 		return ret;
395bdcd8170SKalle Valo 	}
396bdcd8170SKalle Valo 
3971ca4d0b6SKalle Valo 	if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN) {
3981ca4d0b6SKalle Valo 		ret = ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
39905aab177SKalle Valo 					      IGNORE_PS_FAIL_DURING_SCAN);
4001ca4d0b6SKalle Valo 		if (ret) {
4011ca4d0b6SKalle Valo 			ath6kl_err("unable to set power save fail event policy: %d\n",
4021ca4d0b6SKalle Valo 				   ret);
4031ca4d0b6SKalle Valo 			return ret;
4041ca4d0b6SKalle Valo 		}
405bdcd8170SKalle Valo 	}
406bdcd8170SKalle Valo 
4071ca4d0b6SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER)) {
4081ca4d0b6SKalle Valo 		ret = ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
40905aab177SKalle Valo 						   WMI_FOLLOW_BARKER_IN_ERP);
4101ca4d0b6SKalle Valo 		if (ret) {
4111ca4d0b6SKalle Valo 			ath6kl_err("unable to set barker preamble policy: %d\n",
4121ca4d0b6SKalle Valo 				   ret);
4131ca4d0b6SKalle Valo 			return ret;
4141ca4d0b6SKalle Valo 		}
415bdcd8170SKalle Valo 	}
416bdcd8170SKalle Valo 
4171ca4d0b6SKalle Valo 	ret = ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
4181ca4d0b6SKalle Valo 					   WLAN_CONFIG_KEEP_ALIVE_INTERVAL);
4191ca4d0b6SKalle Valo 	if (ret) {
4201ca4d0b6SKalle Valo 		ath6kl_err("unable to set keep alive interval: %d\n", ret);
4211ca4d0b6SKalle Valo 		return ret;
422bdcd8170SKalle Valo 	}
423bdcd8170SKalle Valo 
4241ca4d0b6SKalle Valo 	ret = ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
4251ca4d0b6SKalle Valo 					 WLAN_CONFIG_DISCONNECT_TIMEOUT);
4261ca4d0b6SKalle Valo 	if (ret) {
4271ca4d0b6SKalle Valo 		ath6kl_err("unable to set disconnect timeout: %d\n", ret);
4281ca4d0b6SKalle Valo 		return ret;
429bdcd8170SKalle Valo 	}
430bdcd8170SKalle Valo 
4311ca4d0b6SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST)) {
4321ca4d0b6SKalle Valo 		ret = ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED);
4331ca4d0b6SKalle Valo 		if (ret) {
4341ca4d0b6SKalle Valo 			ath6kl_err("unable to set txop bursting: %d\n", ret);
4351ca4d0b6SKalle Valo 			return ret;
4361ca4d0b6SKalle Valo 		}
437bdcd8170SKalle Valo 	}
438bdcd8170SKalle Valo 
439b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && (ar->vif_max == 1 || idx)) {
4400ce59445SVasanthakumar Thiagarajan 		ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
4416bbc7c35SJouni Malinen 					      P2P_FLAG_CAPABILITIES_REQ |
4424dea08e0SJouni Malinen 					      P2P_FLAG_MACADDR_REQ |
4434dea08e0SJouni Malinen 					      P2P_FLAG_HMODEL_REQ);
4444dea08e0SJouni Malinen 		if (ret) {
4454dea08e0SJouni Malinen 			ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
4466bbc7c35SJouni Malinen 				   "capabilities (%d) - assuming P2P not "
4476bbc7c35SJouni Malinen 				   "supported\n", ret);
4483db1cd5cSRusty Russell 			ar->p2p = false;
4496bbc7c35SJouni Malinen 		}
4506bbc7c35SJouni Malinen 	}
4516bbc7c35SJouni Malinen 
452b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && (ar->vif_max == 1 || idx)) {
4536bbc7c35SJouni Malinen 		/* Enable Probe Request reporting for P2P */
4540ce59445SVasanthakumar Thiagarajan 		ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
4556bbc7c35SJouni Malinen 		if (ret) {
4566bbc7c35SJouni Malinen 			ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
4576bbc7c35SJouni Malinen 				   "Request reporting (%d)\n", ret);
4586bbc7c35SJouni Malinen 		}
4594dea08e0SJouni Malinen 	}
4604dea08e0SJouni Malinen 
4611ca4d0b6SKalle Valo 	return ret;
462bdcd8170SKalle Valo }
463bdcd8170SKalle Valo 
464bdcd8170SKalle Valo int ath6kl_configure_target(struct ath6kl *ar)
465bdcd8170SKalle Valo {
466bdcd8170SKalle Valo 	u32 param, ram_reserved_size;
4673226f68aSVasanthakumar Thiagarajan 	u8 fw_iftype, fw_mode = 0, fw_submode = 0;
46839586bf2SRyan Hsu 	int i, status;
469bdcd8170SKalle Valo 
470f29af978SKalle Valo 	param = !!(ar->conf_flags & ATH6KL_CONF_UART_DEBUG);
47124fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_serial_enable, param)) {
472a10e2f2fSVasanthakumar Thiagarajan 		ath6kl_err("bmi_write_memory for uart debug failed\n");
473a10e2f2fSVasanthakumar Thiagarajan 		return -EIO;
474a10e2f2fSVasanthakumar Thiagarajan 	}
475a10e2f2fSVasanthakumar Thiagarajan 
4767b85832dSVasanthakumar Thiagarajan 	/*
4777b85832dSVasanthakumar Thiagarajan 	 * Note: Even though the firmware interface type is
4787b85832dSVasanthakumar Thiagarajan 	 * chosen as BSS_STA for all three interfaces, can
4797b85832dSVasanthakumar Thiagarajan 	 * be configured to IBSS/AP as long as the fw submode
4807b85832dSVasanthakumar Thiagarajan 	 * remains normal mode (0 - AP, STA and IBSS). But
4817b85832dSVasanthakumar Thiagarajan 	 * due to an target assert in firmware only one interface is
4827b85832dSVasanthakumar Thiagarajan 	 * configured for now.
4837b85832dSVasanthakumar Thiagarajan 	 */
484dd3751f7SVasanthakumar Thiagarajan 	fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
485bdcd8170SKalle Valo 
48671f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++)
4877b85832dSVasanthakumar Thiagarajan 		fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
4887b85832dSVasanthakumar Thiagarajan 
4897b85832dSVasanthakumar Thiagarajan 	/*
4903226f68aSVasanthakumar Thiagarajan 	 * By default, submodes :
4913226f68aSVasanthakumar Thiagarajan 	 *		vif[0] - AP/STA/IBSS
4927b85832dSVasanthakumar Thiagarajan 	 *		vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
4937b85832dSVasanthakumar Thiagarajan 	 *		vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
4947b85832dSVasanthakumar Thiagarajan 	 */
4953226f68aSVasanthakumar Thiagarajan 
4963226f68aSVasanthakumar Thiagarajan 	for (i = 0; i < ar->max_norm_iface; i++)
4973226f68aSVasanthakumar Thiagarajan 		fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
4983226f68aSVasanthakumar Thiagarajan 			      (i * HI_OPTION_FW_SUBMODE_BITS);
4993226f68aSVasanthakumar Thiagarajan 
50071f96ee6SKalle Valo 	for (i = ar->max_norm_iface; i < ar->vif_max; i++)
5013226f68aSVasanthakumar Thiagarajan 		fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
5023226f68aSVasanthakumar Thiagarajan 			      (i * HI_OPTION_FW_SUBMODE_BITS);
5037b85832dSVasanthakumar Thiagarajan 
504b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && ar->vif_max == 1)
5057b85832dSVasanthakumar Thiagarajan 		fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
5067b85832dSVasanthakumar Thiagarajan 
50724fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_app_host_interest,
50824fc32b3SKalle Valo 				  HTC_PROTOCOL_VERSION) != 0) {
509bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for htc version failed\n");
510bdcd8170SKalle Valo 		return -EIO;
511bdcd8170SKalle Valo 	}
512bdcd8170SKalle Valo 
513bdcd8170SKalle Valo 	/* set the firmware mode to STA/IBSS/AP */
514bdcd8170SKalle Valo 	param = 0;
515bdcd8170SKalle Valo 
51680fb2686SKalle Valo 	if (ath6kl_bmi_read_hi32(ar, hi_option_flag, &param) != 0) {
517bdcd8170SKalle Valo 		ath6kl_err("bmi_read_memory for setting fwmode failed\n");
518bdcd8170SKalle Valo 		return -EIO;
519bdcd8170SKalle Valo 	}
520bdcd8170SKalle Valo 
52171f96ee6SKalle Valo 	param |= (ar->vif_max << HI_OPTION_NUM_DEV_SHIFT);
5227b85832dSVasanthakumar Thiagarajan 	param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
5237b85832dSVasanthakumar Thiagarajan 	param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
5247b85832dSVasanthakumar Thiagarajan 
525bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
526bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
527bdcd8170SKalle Valo 
52824fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_option_flag, param) != 0) {
529bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for setting fwmode failed\n");
530bdcd8170SKalle Valo 		return -EIO;
531bdcd8170SKalle Valo 	}
532bdcd8170SKalle Valo 
533bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
534bdcd8170SKalle Valo 
535bdcd8170SKalle Valo 	/*
536bdcd8170SKalle Valo 	 * Hardcode the address use for the extended board data
537bdcd8170SKalle Valo 	 * Ideally this should be pre-allocate by the OS at boot time
538bdcd8170SKalle Valo 	 * But since it is a new feature and board data is loaded
539bdcd8170SKalle Valo 	 * at init time, we have to workaround this from host.
540bdcd8170SKalle Valo 	 * It is difficult to patch the firmware boot code,
541bdcd8170SKalle Valo 	 * but possible in theory.
542bdcd8170SKalle Valo 	 */
543bdcd8170SKalle Valo 
5446b42d308SKalle Valo 	if (ar->target_type == TARGET_TYPE_AR6003) {
545991b27eaSKalle Valo 		param = ar->hw.board_ext_data_addr;
546991b27eaSKalle Valo 		ram_reserved_size = ar->hw.reserved_ram_size;
547bdcd8170SKalle Valo 
54824fc32b3SKalle Valo 		if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
549bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
550bdcd8170SKalle Valo 			return -EIO;
551bdcd8170SKalle Valo 		}
552991b27eaSKalle Valo 
55324fc32b3SKalle Valo 		if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
55424fc32b3SKalle Valo 					  ram_reserved_size) != 0) {
555bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
556bdcd8170SKalle Valo 			return -EIO;
557bdcd8170SKalle Valo 		}
5586b42d308SKalle Valo 	}
559bdcd8170SKalle Valo 
560bdcd8170SKalle Valo 	/* set the block size for the target */
561bdcd8170SKalle Valo 	if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
562bdcd8170SKalle Valo 		/* use default number of control buffers */
563bdcd8170SKalle Valo 		return -EIO;
564bdcd8170SKalle Valo 
56539586bf2SRyan Hsu 	/* Configure GPIO AR600x UART */
56624fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
56724fc32b3SKalle Valo 				       ar->hw.uarttx_pin);
56839586bf2SRyan Hsu 	if (status)
56939586bf2SRyan Hsu 		return status;
57039586bf2SRyan Hsu 
57139586bf2SRyan Hsu 	/* Configure target refclk_hz */
57224fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz, ar->hw.refclk_hz);
57339586bf2SRyan Hsu 	if (status)
57439586bf2SRyan Hsu 		return status;
57539586bf2SRyan Hsu 
576bdcd8170SKalle Valo 	return 0;
577bdcd8170SKalle Valo }
578bdcd8170SKalle Valo 
579bdcd8170SKalle Valo /* firmware upload */
580bdcd8170SKalle Valo static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
581bdcd8170SKalle Valo 			 u8 **fw, size_t *fw_len)
582bdcd8170SKalle Valo {
583bdcd8170SKalle Valo 	const struct firmware *fw_entry;
584bdcd8170SKalle Valo 	int ret;
585bdcd8170SKalle Valo 
586bdcd8170SKalle Valo 	ret = request_firmware(&fw_entry, filename, ar->dev);
587bdcd8170SKalle Valo 	if (ret)
588bdcd8170SKalle Valo 		return ret;
589bdcd8170SKalle Valo 
590bdcd8170SKalle Valo 	*fw_len = fw_entry->size;
591bdcd8170SKalle Valo 	*fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
592bdcd8170SKalle Valo 
593bdcd8170SKalle Valo 	if (*fw == NULL)
594bdcd8170SKalle Valo 		ret = -ENOMEM;
595bdcd8170SKalle Valo 
596bdcd8170SKalle Valo 	release_firmware(fw_entry);
597bdcd8170SKalle Valo 
598bdcd8170SKalle Valo 	return ret;
599bdcd8170SKalle Valo }
600bdcd8170SKalle Valo 
60192ecbff4SSam Leffler #ifdef CONFIG_OF
60292ecbff4SSam Leffler /*
60392ecbff4SSam Leffler  * Check the device tree for a board-id and use it to construct
60492ecbff4SSam Leffler  * the pathname to the firmware file.  Used (for now) to find a
60592ecbff4SSam Leffler  * fallback to the "bdata.bin" file--typically a symlink to the
60692ecbff4SSam Leffler  * appropriate board-specific file.
60792ecbff4SSam Leffler  */
60892ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
60992ecbff4SSam Leffler {
61092ecbff4SSam Leffler 	static const char *board_id_prop = "atheros,board-id";
61192ecbff4SSam Leffler 	struct device_node *node;
61292ecbff4SSam Leffler 	char board_filename[64];
61392ecbff4SSam Leffler 	const char *board_id;
61492ecbff4SSam Leffler 	int ret;
61592ecbff4SSam Leffler 
61692ecbff4SSam Leffler 	for_each_compatible_node(node, NULL, "atheros,ath6kl") {
61792ecbff4SSam Leffler 		board_id = of_get_property(node, board_id_prop, NULL);
61892ecbff4SSam Leffler 		if (board_id == NULL) {
61992ecbff4SSam Leffler 			ath6kl_warn("No \"%s\" property on %s node.\n",
62092ecbff4SSam Leffler 				    board_id_prop, node->name);
62192ecbff4SSam Leffler 			continue;
62292ecbff4SSam Leffler 		}
62392ecbff4SSam Leffler 		snprintf(board_filename, sizeof(board_filename),
624c0038972SKalle Valo 			 "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
62592ecbff4SSam Leffler 
62692ecbff4SSam Leffler 		ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
62792ecbff4SSam Leffler 				    &ar->fw_board_len);
62892ecbff4SSam Leffler 		if (ret) {
62992ecbff4SSam Leffler 			ath6kl_err("Failed to get DT board file %s: %d\n",
63092ecbff4SSam Leffler 				   board_filename, ret);
63192ecbff4SSam Leffler 			continue;
63292ecbff4SSam Leffler 		}
63392ecbff4SSam Leffler 		return true;
63492ecbff4SSam Leffler 	}
63592ecbff4SSam Leffler 	return false;
63692ecbff4SSam Leffler }
63792ecbff4SSam Leffler #else
63892ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
63992ecbff4SSam Leffler {
64092ecbff4SSam Leffler 	return false;
64192ecbff4SSam Leffler }
64292ecbff4SSam Leffler #endif /* CONFIG_OF */
64392ecbff4SSam Leffler 
644bdcd8170SKalle Valo static int ath6kl_fetch_board_file(struct ath6kl *ar)
645bdcd8170SKalle Valo {
646bdcd8170SKalle Valo 	const char *filename;
647bdcd8170SKalle Valo 	int ret;
648bdcd8170SKalle Valo 
649772c31eeSKalle Valo 	if (ar->fw_board != NULL)
650772c31eeSKalle Valo 		return 0;
651772c31eeSKalle Valo 
652d1a9421dSKalle Valo 	if (WARN_ON(ar->hw.fw_board == NULL))
653d1a9421dSKalle Valo 		return -EINVAL;
654d1a9421dSKalle Valo 
655d1a9421dSKalle Valo 	filename = ar->hw.fw_board;
656bdcd8170SKalle Valo 
657bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
658bdcd8170SKalle Valo 			    &ar->fw_board_len);
659bdcd8170SKalle Valo 	if (ret == 0) {
660bdcd8170SKalle Valo 		/* managed to get proper board file */
661bdcd8170SKalle Valo 		return 0;
662bdcd8170SKalle Valo 	}
663bdcd8170SKalle Valo 
66492ecbff4SSam Leffler 	if (check_device_tree(ar)) {
66592ecbff4SSam Leffler 		/* got board file from device tree */
66692ecbff4SSam Leffler 		return 0;
66792ecbff4SSam Leffler 	}
66892ecbff4SSam Leffler 
669bdcd8170SKalle Valo 	/* there was no proper board file, try to use default instead */
670bdcd8170SKalle Valo 	ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
671bdcd8170SKalle Valo 		    filename, ret);
672bdcd8170SKalle Valo 
673d1a9421dSKalle Valo 	filename = ar->hw.fw_default_board;
674bdcd8170SKalle Valo 
675bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
676bdcd8170SKalle Valo 			    &ar->fw_board_len);
677bdcd8170SKalle Valo 	if (ret) {
678bdcd8170SKalle Valo 		ath6kl_err("Failed to get default board file %s: %d\n",
679bdcd8170SKalle Valo 			   filename, ret);
680bdcd8170SKalle Valo 		return ret;
681bdcd8170SKalle Valo 	}
682bdcd8170SKalle Valo 
683bdcd8170SKalle Valo 	ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
684bdcd8170SKalle Valo 	ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
685bdcd8170SKalle Valo 
686bdcd8170SKalle Valo 	return 0;
687bdcd8170SKalle Valo }
688bdcd8170SKalle Valo 
689772c31eeSKalle Valo static int ath6kl_fetch_otp_file(struct ath6kl *ar)
690772c31eeSKalle Valo {
691c0038972SKalle Valo 	char filename[100];
692772c31eeSKalle Valo 	int ret;
693772c31eeSKalle Valo 
694772c31eeSKalle Valo 	if (ar->fw_otp != NULL)
695772c31eeSKalle Valo 		return 0;
696772c31eeSKalle Valo 
697c0038972SKalle Valo 	if (ar->hw.fw.otp == NULL) {
698d1a9421dSKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
699d1a9421dSKalle Valo 			   "no OTP file configured for this hw\n");
700772c31eeSKalle Valo 		return 0;
701772c31eeSKalle Valo 	}
702772c31eeSKalle Valo 
703c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
704c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.otp);
705d1a9421dSKalle Valo 
706772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
707772c31eeSKalle Valo 			    &ar->fw_otp_len);
708772c31eeSKalle Valo 	if (ret) {
709772c31eeSKalle Valo 		ath6kl_err("Failed to get OTP file %s: %d\n",
710772c31eeSKalle Valo 			   filename, ret);
711772c31eeSKalle Valo 		return ret;
712772c31eeSKalle Valo 	}
713772c31eeSKalle Valo 
714772c31eeSKalle Valo 	return 0;
715772c31eeSKalle Valo }
716772c31eeSKalle Valo 
7175f1127ffSKalle Valo static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
718772c31eeSKalle Valo {
719c0038972SKalle Valo 	char filename[100];
720772c31eeSKalle Valo 	int ret;
721772c31eeSKalle Valo 
7225f1127ffSKalle Valo 	if (ar->testmode == 0)
723772c31eeSKalle Valo 		return 0;
724772c31eeSKalle Valo 
7255f1127ffSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
7265f1127ffSKalle Valo 
7275f1127ffSKalle Valo 	if (ar->testmode == 2) {
728cd23c1c9SAlex Yang 		if (ar->hw.fw.utf == NULL) {
729cd23c1c9SAlex Yang 			ath6kl_warn("testmode 2 not supported\n");
730cd23c1c9SAlex Yang 			return -EOPNOTSUPP;
731cd23c1c9SAlex Yang 		}
732cd23c1c9SAlex Yang 
733cd23c1c9SAlex Yang 		snprintf(filename, sizeof(filename), "%s/%s",
734cd23c1c9SAlex Yang 			 ar->hw.fw.dir, ar->hw.fw.utf);
735cd23c1c9SAlex Yang 	} else {
736c0038972SKalle Valo 		if (ar->hw.fw.tcmd == NULL) {
737cd23c1c9SAlex Yang 			ath6kl_warn("testmode 1 not supported\n");
738772c31eeSKalle Valo 			return -EOPNOTSUPP;
739772c31eeSKalle Valo 		}
740772c31eeSKalle Valo 
741c0038972SKalle Valo 		snprintf(filename, sizeof(filename), "%s/%s",
742c0038972SKalle Valo 			 ar->hw.fw.dir, ar->hw.fw.tcmd);
743cd23c1c9SAlex Yang 	}
7445f1127ffSKalle Valo 
745772c31eeSKalle Valo 	set_bit(TESTMODE, &ar->flag);
746772c31eeSKalle Valo 
7475f1127ffSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
7485f1127ffSKalle Valo 	if (ret) {
7495f1127ffSKalle Valo 		ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
7505f1127ffSKalle Valo 			   ar->testmode, filename, ret);
7515f1127ffSKalle Valo 		return ret;
752772c31eeSKalle Valo 	}
753772c31eeSKalle Valo 
7545f1127ffSKalle Valo 	return 0;
7555f1127ffSKalle Valo }
7565f1127ffSKalle Valo 
7575f1127ffSKalle Valo static int ath6kl_fetch_fw_file(struct ath6kl *ar)
7585f1127ffSKalle Valo {
7595f1127ffSKalle Valo 	char filename[100];
7605f1127ffSKalle Valo 	int ret;
7615f1127ffSKalle Valo 
7625f1127ffSKalle Valo 	if (ar->fw != NULL)
7635f1127ffSKalle Valo 		return 0;
7645f1127ffSKalle Valo 
765c0038972SKalle Valo 	/* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
766c0038972SKalle Valo 	if (WARN_ON(ar->hw.fw.fw == NULL))
767d1a9421dSKalle Valo 		return -EINVAL;
768d1a9421dSKalle Valo 
769c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
770c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.fw);
771772c31eeSKalle Valo 
772772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
773772c31eeSKalle Valo 	if (ret) {
774772c31eeSKalle Valo 		ath6kl_err("Failed to get firmware file %s: %d\n",
775772c31eeSKalle Valo 			   filename, ret);
776772c31eeSKalle Valo 		return ret;
777772c31eeSKalle Valo 	}
778772c31eeSKalle Valo 
779772c31eeSKalle Valo 	return 0;
780772c31eeSKalle Valo }
781772c31eeSKalle Valo 
782772c31eeSKalle Valo static int ath6kl_fetch_patch_file(struct ath6kl *ar)
783772c31eeSKalle Valo {
784c0038972SKalle Valo 	char filename[100];
785772c31eeSKalle Valo 	int ret;
786772c31eeSKalle Valo 
787d1a9421dSKalle Valo 	if (ar->fw_patch != NULL)
788772c31eeSKalle Valo 		return 0;
789772c31eeSKalle Valo 
790c0038972SKalle Valo 	if (ar->hw.fw.patch == NULL)
791d1a9421dSKalle Valo 		return 0;
792d1a9421dSKalle Valo 
793c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
794c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.patch);
795d1a9421dSKalle Valo 
796772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
797772c31eeSKalle Valo 			    &ar->fw_patch_len);
798772c31eeSKalle Valo 	if (ret) {
799772c31eeSKalle Valo 		ath6kl_err("Failed to get patch file %s: %d\n",
800772c31eeSKalle Valo 			   filename, ret);
801772c31eeSKalle Valo 		return ret;
802772c31eeSKalle Valo 	}
803772c31eeSKalle Valo 
804772c31eeSKalle Valo 	return 0;
805772c31eeSKalle Valo }
806772c31eeSKalle Valo 
807cd23c1c9SAlex Yang static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
808cd23c1c9SAlex Yang {
809cd23c1c9SAlex Yang 	char filename[100];
810cd23c1c9SAlex Yang 	int ret;
811cd23c1c9SAlex Yang 
8125f1127ffSKalle Valo 	if (ar->testmode != 2)
813cd23c1c9SAlex Yang 		return 0;
814cd23c1c9SAlex Yang 
815cd23c1c9SAlex Yang 	if (ar->fw_testscript != NULL)
816cd23c1c9SAlex Yang 		return 0;
817cd23c1c9SAlex Yang 
818cd23c1c9SAlex Yang 	if (ar->hw.fw.testscript == NULL)
819cd23c1c9SAlex Yang 		return 0;
820cd23c1c9SAlex Yang 
821cd23c1c9SAlex Yang 	snprintf(filename, sizeof(filename), "%s/%s",
822cd23c1c9SAlex Yang 		 ar->hw.fw.dir, ar->hw.fw.testscript);
823cd23c1c9SAlex Yang 
824cd23c1c9SAlex Yang 	ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
825cd23c1c9SAlex Yang 				&ar->fw_testscript_len);
826cd23c1c9SAlex Yang 	if (ret) {
827cd23c1c9SAlex Yang 		ath6kl_err("Failed to get testscript file %s: %d\n",
828cd23c1c9SAlex Yang 			   filename, ret);
829cd23c1c9SAlex Yang 		return ret;
830cd23c1c9SAlex Yang 	}
831cd23c1c9SAlex Yang 
832cd23c1c9SAlex Yang 	return 0;
833cd23c1c9SAlex Yang }
834cd23c1c9SAlex Yang 
83550d41234SKalle Valo static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
836772c31eeSKalle Valo {
837772c31eeSKalle Valo 	int ret;
838772c31eeSKalle Valo 
839772c31eeSKalle Valo 	ret = ath6kl_fetch_otp_file(ar);
840772c31eeSKalle Valo 	if (ret)
841772c31eeSKalle Valo 		return ret;
842772c31eeSKalle Valo 
843772c31eeSKalle Valo 	ret = ath6kl_fetch_fw_file(ar);
844772c31eeSKalle Valo 	if (ret)
845772c31eeSKalle Valo 		return ret;
846772c31eeSKalle Valo 
847772c31eeSKalle Valo 	ret = ath6kl_fetch_patch_file(ar);
848772c31eeSKalle Valo 	if (ret)
849772c31eeSKalle Valo 		return ret;
850772c31eeSKalle Valo 
851cd23c1c9SAlex Yang 	ret = ath6kl_fetch_testscript_file(ar);
852cd23c1c9SAlex Yang 	if (ret)
853cd23c1c9SAlex Yang 		return ret;
854cd23c1c9SAlex Yang 
855772c31eeSKalle Valo 	return 0;
856772c31eeSKalle Valo }
857bdcd8170SKalle Valo 
85865a8b4ccSKalle Valo static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
85950d41234SKalle Valo {
86050d41234SKalle Valo 	size_t magic_len, len, ie_len;
86150d41234SKalle Valo 	const struct firmware *fw;
86250d41234SKalle Valo 	struct ath6kl_fw_ie *hdr;
863c0038972SKalle Valo 	char filename[100];
86450d41234SKalle Valo 	const u8 *data;
86597e0496dSKalle Valo 	int ret, ie_id, i, index, bit;
8668a137480SKalle Valo 	__le32 *val;
86750d41234SKalle Valo 
86865a8b4ccSKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
86950d41234SKalle Valo 
87050d41234SKalle Valo 	ret = request_firmware(&fw, filename, ar->dev);
87150d41234SKalle Valo 	if (ret)
87250d41234SKalle Valo 		return ret;
87350d41234SKalle Valo 
87450d41234SKalle Valo 	data = fw->data;
87550d41234SKalle Valo 	len = fw->size;
87650d41234SKalle Valo 
87750d41234SKalle Valo 	/* magic also includes the null byte, check that as well */
87850d41234SKalle Valo 	magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
87950d41234SKalle Valo 
88050d41234SKalle Valo 	if (len < magic_len) {
88150d41234SKalle Valo 		ret = -EINVAL;
88250d41234SKalle Valo 		goto out;
88350d41234SKalle Valo 	}
88450d41234SKalle Valo 
88550d41234SKalle Valo 	if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
88650d41234SKalle Valo 		ret = -EINVAL;
88750d41234SKalle Valo 		goto out;
88850d41234SKalle Valo 	}
88950d41234SKalle Valo 
89050d41234SKalle Valo 	len -= magic_len;
89150d41234SKalle Valo 	data += magic_len;
89250d41234SKalle Valo 
89350d41234SKalle Valo 	/* loop elements */
89450d41234SKalle Valo 	while (len > sizeof(struct ath6kl_fw_ie)) {
89550d41234SKalle Valo 		/* hdr is unaligned! */
89650d41234SKalle Valo 		hdr = (struct ath6kl_fw_ie *) data;
89750d41234SKalle Valo 
89850d41234SKalle Valo 		ie_id = le32_to_cpup(&hdr->id);
89950d41234SKalle Valo 		ie_len = le32_to_cpup(&hdr->len);
90050d41234SKalle Valo 
90150d41234SKalle Valo 		len -= sizeof(*hdr);
90250d41234SKalle Valo 		data += sizeof(*hdr);
90350d41234SKalle Valo 
90450d41234SKalle Valo 		if (len < ie_len) {
90550d41234SKalle Valo 			ret = -EINVAL;
90650d41234SKalle Valo 			goto out;
90750d41234SKalle Valo 		}
90850d41234SKalle Valo 
90950d41234SKalle Valo 		switch (ie_id) {
91050d41234SKalle Valo 		case ATH6KL_FW_IE_OTP_IMAGE:
911ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
9126bc36431SKalle Valo 				   ie_len);
9136bc36431SKalle Valo 
91450d41234SKalle Valo 			ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
91550d41234SKalle Valo 
91650d41234SKalle Valo 			if (ar->fw_otp == NULL) {
91750d41234SKalle Valo 				ret = -ENOMEM;
91850d41234SKalle Valo 				goto out;
91950d41234SKalle Valo 			}
92050d41234SKalle Valo 
92150d41234SKalle Valo 			ar->fw_otp_len = ie_len;
92250d41234SKalle Valo 			break;
92350d41234SKalle Valo 		case ATH6KL_FW_IE_FW_IMAGE:
924ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
9256bc36431SKalle Valo 				   ie_len);
9266bc36431SKalle Valo 
9275f1127ffSKalle Valo 			/* in testmode we already might have a fw file */
9285f1127ffSKalle Valo 			if (ar->fw != NULL)
9295f1127ffSKalle Valo 				break;
9305f1127ffSKalle Valo 
93150d41234SKalle Valo 			ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
93250d41234SKalle Valo 
93350d41234SKalle Valo 			if (ar->fw == NULL) {
93450d41234SKalle Valo 				ret = -ENOMEM;
93550d41234SKalle Valo 				goto out;
93650d41234SKalle Valo 			}
93750d41234SKalle Valo 
93850d41234SKalle Valo 			ar->fw_len = ie_len;
93950d41234SKalle Valo 			break;
94050d41234SKalle Valo 		case ATH6KL_FW_IE_PATCH_IMAGE:
941ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
9426bc36431SKalle Valo 				   ie_len);
9436bc36431SKalle Valo 
94450d41234SKalle Valo 			ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
94550d41234SKalle Valo 
94650d41234SKalle Valo 			if (ar->fw_patch == NULL) {
94750d41234SKalle Valo 				ret = -ENOMEM;
94850d41234SKalle Valo 				goto out;
94950d41234SKalle Valo 			}
95050d41234SKalle Valo 
95150d41234SKalle Valo 			ar->fw_patch_len = ie_len;
95250d41234SKalle Valo 			break;
9538a137480SKalle Valo 		case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
9548a137480SKalle Valo 			val = (__le32 *) data;
9558a137480SKalle Valo 			ar->hw.reserved_ram_size = le32_to_cpup(val);
9566bc36431SKalle Valo 
9576bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
9586bc36431SKalle Valo 				   "found reserved ram size ie 0x%d\n",
9596bc36431SKalle Valo 				   ar->hw.reserved_ram_size);
9608a137480SKalle Valo 			break;
96197e0496dSKalle Valo 		case ATH6KL_FW_IE_CAPABILITIES:
962277d90f4SKalle Valo 			if (ie_len < DIV_ROUND_UP(ATH6KL_FW_CAPABILITY_MAX, 8))
963277d90f4SKalle Valo 				break;
964277d90f4SKalle Valo 
9656bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
966ef548626SKalle Valo 				   "found firmware capabilities ie (%zd B)\n",
9676bc36431SKalle Valo 				   ie_len);
9686bc36431SKalle Valo 
96997e0496dSKalle Valo 			for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
970277d90f4SKalle Valo 				index = i / 8;
97197e0496dSKalle Valo 				bit = i % 8;
97297e0496dSKalle Valo 
97397e0496dSKalle Valo 				if (data[index] & (1 << bit))
97497e0496dSKalle Valo 					__set_bit(i, ar->fw_capabilities);
97597e0496dSKalle Valo 			}
9766bc36431SKalle Valo 
9776bc36431SKalle Valo 			ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
9786bc36431SKalle Valo 					ar->fw_capabilities,
9796bc36431SKalle Valo 					sizeof(ar->fw_capabilities));
98097e0496dSKalle Valo 			break;
9811b4304daSKalle Valo 		case ATH6KL_FW_IE_PATCH_ADDR:
9821b4304daSKalle Valo 			if (ie_len != sizeof(*val))
9831b4304daSKalle Valo 				break;
9841b4304daSKalle Valo 
9851b4304daSKalle Valo 			val = (__le32 *) data;
9861b4304daSKalle Valo 			ar->hw.dataset_patch_addr = le32_to_cpup(val);
9876bc36431SKalle Valo 
9886bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
98903ef0250SKalle Valo 				   "found patch address ie 0x%x\n",
9906bc36431SKalle Valo 				   ar->hw.dataset_patch_addr);
9911b4304daSKalle Valo 			break;
99203ef0250SKalle Valo 		case ATH6KL_FW_IE_BOARD_ADDR:
99303ef0250SKalle Valo 			if (ie_len != sizeof(*val))
99403ef0250SKalle Valo 				break;
99503ef0250SKalle Valo 
99603ef0250SKalle Valo 			val = (__le32 *) data;
99703ef0250SKalle Valo 			ar->hw.board_addr = le32_to_cpup(val);
99803ef0250SKalle Valo 
99903ef0250SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
100003ef0250SKalle Valo 				   "found board address ie 0x%x\n",
100103ef0250SKalle Valo 				   ar->hw.board_addr);
100203ef0250SKalle Valo 			break;
1003368b1b0fSKalle Valo 		case ATH6KL_FW_IE_VIF_MAX:
1004368b1b0fSKalle Valo 			if (ie_len != sizeof(*val))
1005368b1b0fSKalle Valo 				break;
1006368b1b0fSKalle Valo 
1007368b1b0fSKalle Valo 			val = (__le32 *) data;
1008368b1b0fSKalle Valo 			ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
1009368b1b0fSKalle Valo 					    ATH6KL_VIF_MAX);
1010368b1b0fSKalle Valo 
1011f143379dSVasanthakumar Thiagarajan 			if (ar->vif_max > 1 && !ar->p2p)
1012f143379dSVasanthakumar Thiagarajan 				ar->max_norm_iface = 2;
1013f143379dSVasanthakumar Thiagarajan 
1014368b1b0fSKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1015368b1b0fSKalle Valo 				   "found vif max ie %d\n", ar->vif_max);
1016368b1b0fSKalle Valo 			break;
101750d41234SKalle Valo 		default:
10186bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
101950d41234SKalle Valo 				   le32_to_cpup(&hdr->id));
102050d41234SKalle Valo 			break;
102150d41234SKalle Valo 		}
102250d41234SKalle Valo 
102350d41234SKalle Valo 		len -= ie_len;
102450d41234SKalle Valo 		data += ie_len;
102550d41234SKalle Valo 	};
102650d41234SKalle Valo 
102750d41234SKalle Valo 	ret = 0;
102850d41234SKalle Valo out:
102950d41234SKalle Valo 	release_firmware(fw);
103050d41234SKalle Valo 
103150d41234SKalle Valo 	return ret;
103250d41234SKalle Valo }
103350d41234SKalle Valo 
103445eaa78fSKalle Valo int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
103550d41234SKalle Valo {
103650d41234SKalle Valo 	int ret;
103750d41234SKalle Valo 
103850d41234SKalle Valo 	ret = ath6kl_fetch_board_file(ar);
103950d41234SKalle Valo 	if (ret)
104050d41234SKalle Valo 		return ret;
104150d41234SKalle Valo 
10425f1127ffSKalle Valo 	ret = ath6kl_fetch_testmode_file(ar);
10435f1127ffSKalle Valo 	if (ret)
10445f1127ffSKalle Valo 		return ret;
10455f1127ffSKalle Valo 
104665a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
10476bc36431SKalle Valo 	if (ret == 0) {
104865a8b4ccSKalle Valo 		ar->fw_api = 3;
104965a8b4ccSKalle Valo 		goto out;
105065a8b4ccSKalle Valo 	}
105165a8b4ccSKalle Valo 
105265a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
105365a8b4ccSKalle Valo 	if (ret == 0) {
105465a8b4ccSKalle Valo 		ar->fw_api = 2;
105565a8b4ccSKalle Valo 		goto out;
10566bc36431SKalle Valo 	}
105750d41234SKalle Valo 
105850d41234SKalle Valo 	ret = ath6kl_fetch_fw_api1(ar);
105950d41234SKalle Valo 	if (ret)
106050d41234SKalle Valo 		return ret;
106150d41234SKalle Valo 
106265a8b4ccSKalle Valo 	ar->fw_api = 1;
106365a8b4ccSKalle Valo 
106465a8b4ccSKalle Valo out:
106565a8b4ccSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
10666bc36431SKalle Valo 
106750d41234SKalle Valo 	return 0;
106850d41234SKalle Valo }
106950d41234SKalle Valo 
1070bdcd8170SKalle Valo static int ath6kl_upload_board_file(struct ath6kl *ar)
1071bdcd8170SKalle Valo {
1072bdcd8170SKalle Valo 	u32 board_address, board_ext_address, param;
107331024d99SKevin Fang 	u32 board_data_size, board_ext_data_size;
1074bdcd8170SKalle Valo 	int ret;
1075bdcd8170SKalle Valo 
1076772c31eeSKalle Valo 	if (WARN_ON(ar->fw_board == NULL))
1077772c31eeSKalle Valo 		return -ENOENT;
1078bdcd8170SKalle Valo 
107931024d99SKevin Fang 	/*
108031024d99SKevin Fang 	 * Determine where in Target RAM to write Board Data.
108131024d99SKevin Fang 	 * For AR6004, host determine Target RAM address for
108231024d99SKevin Fang 	 * writing board data.
108331024d99SKevin Fang 	 */
10840d4d72bfSKalle Valo 	if (ar->hw.board_addr != 0) {
1085b0fc7c1aSKalle Valo 		board_address = ar->hw.board_addr;
108624fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_data,
1087b0fc7c1aSKalle Valo 				      board_address);
108831024d99SKevin Fang 	} else {
108980fb2686SKalle Valo 		ath6kl_bmi_read_hi32(ar, hi_board_data, &board_address);
109031024d99SKevin Fang 	}
109131024d99SKevin Fang 
1092bdcd8170SKalle Valo 	/* determine where in target ram to write extended board data */
109380fb2686SKalle Valo 	ath6kl_bmi_read_hi32(ar, hi_board_ext_data, &board_ext_address);
1094bdcd8170SKalle Valo 
109550e2740bSKalle Valo 	if (ar->target_type == TARGET_TYPE_AR6003 &&
109650e2740bSKalle Valo 	    board_ext_address == 0) {
1097bdcd8170SKalle Valo 		ath6kl_err("Failed to get board file target address.\n");
1098bdcd8170SKalle Valo 		return -EINVAL;
1099bdcd8170SKalle Valo 	}
1100bdcd8170SKalle Valo 
110131024d99SKevin Fang 	switch (ar->target_type) {
110231024d99SKevin Fang 	case TARGET_TYPE_AR6003:
110331024d99SKevin Fang 		board_data_size = AR6003_BOARD_DATA_SZ;
110431024d99SKevin Fang 		board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1105fb1ac2efSPrasanna Kumar 		if (ar->fw_board_len > (board_data_size + board_ext_data_size))
1106fb1ac2efSPrasanna Kumar 			board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
110731024d99SKevin Fang 		break;
110831024d99SKevin Fang 	case TARGET_TYPE_AR6004:
110931024d99SKevin Fang 		board_data_size = AR6004_BOARD_DATA_SZ;
111031024d99SKevin Fang 		board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
111131024d99SKevin Fang 		break;
111231024d99SKevin Fang 	default:
111331024d99SKevin Fang 		WARN_ON(1);
111431024d99SKevin Fang 		return -EINVAL;
111531024d99SKevin Fang 		break;
111631024d99SKevin Fang 	}
111731024d99SKevin Fang 
111850e2740bSKalle Valo 	if (board_ext_address &&
111950e2740bSKalle Valo 	    ar->fw_board_len == (board_data_size + board_ext_data_size)) {
112031024d99SKevin Fang 
1121bdcd8170SKalle Valo 		/* write extended board data */
11226bc36431SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
11236bc36431SKalle Valo 			   "writing extended board data to 0x%x (%d B)\n",
11246bc36431SKalle Valo 			   board_ext_address, board_ext_data_size);
11256bc36431SKalle Valo 
1126bdcd8170SKalle Valo 		ret = ath6kl_bmi_write(ar, board_ext_address,
112731024d99SKevin Fang 				       ar->fw_board + board_data_size,
112831024d99SKevin Fang 				       board_ext_data_size);
1129bdcd8170SKalle Valo 		if (ret) {
1130bdcd8170SKalle Valo 			ath6kl_err("Failed to write extended board data: %d\n",
1131bdcd8170SKalle Valo 				   ret);
1132bdcd8170SKalle Valo 			return ret;
1133bdcd8170SKalle Valo 		}
1134bdcd8170SKalle Valo 
1135bdcd8170SKalle Valo 		/* record that extended board data is initialized */
113631024d99SKevin Fang 		param = (board_ext_data_size << 16) | 1;
113731024d99SKevin Fang 
113824fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
1139bdcd8170SKalle Valo 	}
1140bdcd8170SKalle Valo 
114131024d99SKevin Fang 	if (ar->fw_board_len < board_data_size) {
1142bdcd8170SKalle Valo 		ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1143bdcd8170SKalle Valo 		ret = -EINVAL;
1144bdcd8170SKalle Valo 		return ret;
1145bdcd8170SKalle Valo 	}
1146bdcd8170SKalle Valo 
11476bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
11486bc36431SKalle Valo 		   board_address, board_data_size);
11496bc36431SKalle Valo 
1150bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
115131024d99SKevin Fang 			       board_data_size);
1152bdcd8170SKalle Valo 
1153bdcd8170SKalle Valo 	if (ret) {
1154bdcd8170SKalle Valo 		ath6kl_err("Board file bmi write failed: %d\n", ret);
1155bdcd8170SKalle Valo 		return ret;
1156bdcd8170SKalle Valo 	}
1157bdcd8170SKalle Valo 
1158bdcd8170SKalle Valo 	/* record the fact that Board Data IS initialized */
115924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, 1);
1160bdcd8170SKalle Valo 
1161bdcd8170SKalle Valo 	return ret;
1162bdcd8170SKalle Valo }
1163bdcd8170SKalle Valo 
1164bdcd8170SKalle Valo static int ath6kl_upload_otp(struct ath6kl *ar)
1165bdcd8170SKalle Valo {
1166bdcd8170SKalle Valo 	u32 address, param;
1167bef26a7fSKalle Valo 	bool from_hw = false;
1168bdcd8170SKalle Valo 	int ret;
1169bdcd8170SKalle Valo 
117050e2740bSKalle Valo 	if (ar->fw_otp == NULL)
117150e2740bSKalle Valo 		return 0;
1172bdcd8170SKalle Valo 
1173a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1174bdcd8170SKalle Valo 
1175ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
11766bc36431SKalle Valo 		   ar->fw_otp_len);
11776bc36431SKalle Valo 
1178bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1179bdcd8170SKalle Valo 				       ar->fw_otp_len);
1180bdcd8170SKalle Valo 	if (ret) {
1181bdcd8170SKalle Valo 		ath6kl_err("Failed to upload OTP file: %d\n", ret);
1182bdcd8170SKalle Valo 		return ret;
1183bdcd8170SKalle Valo 	}
1184bdcd8170SKalle Valo 
1185639d0b89SKalle Valo 	/* read firmware start address */
118680fb2686SKalle Valo 	ret = ath6kl_bmi_read_hi32(ar, hi_app_start, &address);
1187639d0b89SKalle Valo 
1188639d0b89SKalle Valo 	if (ret) {
1189639d0b89SKalle Valo 		ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1190639d0b89SKalle Valo 		return ret;
1191639d0b89SKalle Valo 	}
1192639d0b89SKalle Valo 
1193bef26a7fSKalle Valo 	if (ar->hw.app_start_override_addr == 0) {
1194639d0b89SKalle Valo 		ar->hw.app_start_override_addr = address;
1195bef26a7fSKalle Valo 		from_hw = true;
1196bef26a7fSKalle Valo 	}
1197639d0b89SKalle Valo 
1198bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1199bef26a7fSKalle Valo 		   from_hw ? " (from hw)" : "",
12006bc36431SKalle Valo 		   ar->hw.app_start_override_addr);
12016bc36431SKalle Valo 
1202bdcd8170SKalle Valo 	/* execute the OTP code */
1203bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1204bef26a7fSKalle Valo 		   ar->hw.app_start_override_addr);
1205bdcd8170SKalle Valo 	param = 0;
1206bef26a7fSKalle Valo 	ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
1207bdcd8170SKalle Valo 
1208bdcd8170SKalle Valo 	return ret;
1209bdcd8170SKalle Valo }
1210bdcd8170SKalle Valo 
1211bdcd8170SKalle Valo static int ath6kl_upload_firmware(struct ath6kl *ar)
1212bdcd8170SKalle Valo {
1213bdcd8170SKalle Valo 	u32 address;
1214bdcd8170SKalle Valo 	int ret;
1215bdcd8170SKalle Valo 
1216772c31eeSKalle Valo 	if (WARN_ON(ar->fw == NULL))
121750e2740bSKalle Valo 		return 0;
1218bdcd8170SKalle Valo 
1219a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1220bdcd8170SKalle Valo 
1221ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
12226bc36431SKalle Valo 		   address, ar->fw_len);
12236bc36431SKalle Valo 
1224bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1225bdcd8170SKalle Valo 
1226bdcd8170SKalle Valo 	if (ret) {
1227bdcd8170SKalle Valo 		ath6kl_err("Failed to write firmware: %d\n", ret);
1228bdcd8170SKalle Valo 		return ret;
1229bdcd8170SKalle Valo 	}
1230bdcd8170SKalle Valo 
123131024d99SKevin Fang 	/*
123231024d99SKevin Fang 	 * Set starting address for firmware
123331024d99SKevin Fang 	 * Don't need to setup app_start override addr on AR6004
123431024d99SKevin Fang 	 */
123531024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1236a01ac414SKalle Valo 		address = ar->hw.app_start_override_addr;
1237bdcd8170SKalle Valo 		ath6kl_bmi_set_app_start(ar, address);
123831024d99SKevin Fang 	}
1239bdcd8170SKalle Valo 	return ret;
1240bdcd8170SKalle Valo }
1241bdcd8170SKalle Valo 
1242bdcd8170SKalle Valo static int ath6kl_upload_patch(struct ath6kl *ar)
1243bdcd8170SKalle Valo {
124424fc32b3SKalle Valo 	u32 address;
1245bdcd8170SKalle Valo 	int ret;
1246bdcd8170SKalle Valo 
124750e2740bSKalle Valo 	if (ar->fw_patch == NULL)
124850e2740bSKalle Valo 		return 0;
1249bdcd8170SKalle Valo 
1250a01ac414SKalle Valo 	address = ar->hw.dataset_patch_addr;
1251bdcd8170SKalle Valo 
1252ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
12536bc36431SKalle Valo 		   address, ar->fw_patch_len);
12546bc36431SKalle Valo 
1255bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1256bdcd8170SKalle Valo 	if (ret) {
1257bdcd8170SKalle Valo 		ath6kl_err("Failed to write patch file: %d\n", ret);
1258bdcd8170SKalle Valo 		return ret;
1259bdcd8170SKalle Valo 	}
1260bdcd8170SKalle Valo 
126124fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
1262bdcd8170SKalle Valo 
1263bdcd8170SKalle Valo 	return 0;
1264bdcd8170SKalle Valo }
1265bdcd8170SKalle Valo 
1266cd23c1c9SAlex Yang static int ath6kl_upload_testscript(struct ath6kl *ar)
1267cd23c1c9SAlex Yang {
126824fc32b3SKalle Valo 	u32 address;
1269cd23c1c9SAlex Yang 	int ret;
1270cd23c1c9SAlex Yang 
12715f1127ffSKalle Valo 	if (ar->testmode != 2)
1272cd23c1c9SAlex Yang 		return 0;
1273cd23c1c9SAlex Yang 
1274cd23c1c9SAlex Yang 	if (ar->fw_testscript == NULL)
1275cd23c1c9SAlex Yang 		return 0;
1276cd23c1c9SAlex Yang 
1277cd23c1c9SAlex Yang 	address = ar->hw.testscript_addr;
1278cd23c1c9SAlex Yang 
1279cd23c1c9SAlex Yang 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
1280cd23c1c9SAlex Yang 		   address, ar->fw_testscript_len);
1281cd23c1c9SAlex Yang 
1282cd23c1c9SAlex Yang 	ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
1283cd23c1c9SAlex Yang 		ar->fw_testscript_len);
1284cd23c1c9SAlex Yang 	if (ret) {
1285cd23c1c9SAlex Yang 		ath6kl_err("Failed to write testscript file: %d\n", ret);
1286cd23c1c9SAlex Yang 		return ret;
1287cd23c1c9SAlex Yang 	}
1288cd23c1c9SAlex Yang 
128924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
129024fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
129124fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
1292cd23c1c9SAlex Yang 
1293cd23c1c9SAlex Yang 	return 0;
1294cd23c1c9SAlex Yang }
1295cd23c1c9SAlex Yang 
1296bdcd8170SKalle Valo static int ath6kl_init_upload(struct ath6kl *ar)
1297bdcd8170SKalle Valo {
1298bdcd8170SKalle Valo 	u32 param, options, sleep, address;
1299bdcd8170SKalle Valo 	int status = 0;
1300bdcd8170SKalle Valo 
130131024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6003 &&
130231024d99SKevin Fang 	    ar->target_type != TARGET_TYPE_AR6004)
1303bdcd8170SKalle Valo 		return -EINVAL;
1304bdcd8170SKalle Valo 
1305bdcd8170SKalle Valo 	/* temporarily disable system sleep */
1306bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1307bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1308bdcd8170SKalle Valo 	if (status)
1309bdcd8170SKalle Valo 		return status;
1310bdcd8170SKalle Valo 
1311bdcd8170SKalle Valo 	options = param;
1312bdcd8170SKalle Valo 
1313bdcd8170SKalle Valo 	param |= ATH6KL_OPTION_SLEEP_DISABLE;
1314bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1315bdcd8170SKalle Valo 	if (status)
1316bdcd8170SKalle Valo 		return status;
1317bdcd8170SKalle Valo 
1318bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1319bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1320bdcd8170SKalle Valo 	if (status)
1321bdcd8170SKalle Valo 		return status;
1322bdcd8170SKalle Valo 
1323bdcd8170SKalle Valo 	sleep = param;
1324bdcd8170SKalle Valo 
1325bdcd8170SKalle Valo 	param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1326bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1327bdcd8170SKalle Valo 	if (status)
1328bdcd8170SKalle Valo 		return status;
1329bdcd8170SKalle Valo 
1330bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1331bdcd8170SKalle Valo 		   options, sleep);
1332bdcd8170SKalle Valo 
1333bdcd8170SKalle Valo 	/* program analog PLL register */
133431024d99SKevin Fang 	/* no need to control 40/44MHz clock on AR6004 */
133531024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1336bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1337bdcd8170SKalle Valo 					      0xF9104001);
133831024d99SKevin Fang 
1339bdcd8170SKalle Valo 		if (status)
1340bdcd8170SKalle Valo 			return status;
1341bdcd8170SKalle Valo 
1342bdcd8170SKalle Valo 		/* Run at 80/88MHz by default */
1343bdcd8170SKalle Valo 		param = SM(CPU_CLOCK_STANDARD, 1);
1344bdcd8170SKalle Valo 
1345bdcd8170SKalle Valo 		address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1346bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1347bdcd8170SKalle Valo 		if (status)
1348bdcd8170SKalle Valo 			return status;
134931024d99SKevin Fang 	}
1350bdcd8170SKalle Valo 
1351bdcd8170SKalle Valo 	param = 0;
1352bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1353bdcd8170SKalle Valo 	param = SM(LPO_CAL_ENABLE, 1);
1354bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1355bdcd8170SKalle Valo 	if (status)
1356bdcd8170SKalle Valo 		return status;
1357bdcd8170SKalle Valo 
1358bdcd8170SKalle Valo 	/* WAR to avoid SDIO CRC err */
13594480bb59SRaja Mani 	if (ar->version.target_ver == AR6003_HW_2_0_VERSION ||
13604480bb59SRaja Mani 	    ar->version.target_ver == AR6003_HW_2_1_1_VERSION) {
1361bdcd8170SKalle Valo 		ath6kl_err("temporary war to avoid sdio crc error\n");
1362bdcd8170SKalle Valo 
1363bdcd8170SKalle Valo 		param = 0x20;
1364bdcd8170SKalle Valo 
1365bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1366bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1367bdcd8170SKalle Valo 		if (status)
1368bdcd8170SKalle Valo 			return status;
1369bdcd8170SKalle Valo 
1370bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1371bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1372bdcd8170SKalle Valo 		if (status)
1373bdcd8170SKalle Valo 			return status;
1374bdcd8170SKalle Valo 
1375bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1376bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1377bdcd8170SKalle Valo 		if (status)
1378bdcd8170SKalle Valo 			return status;
1379bdcd8170SKalle Valo 
1380bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1381bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1382bdcd8170SKalle Valo 		if (status)
1383bdcd8170SKalle Valo 			return status;
1384bdcd8170SKalle Valo 	}
1385bdcd8170SKalle Valo 
1386bdcd8170SKalle Valo 	/* write EEPROM data to Target RAM */
1387bdcd8170SKalle Valo 	status = ath6kl_upload_board_file(ar);
1388bdcd8170SKalle Valo 	if (status)
1389bdcd8170SKalle Valo 		return status;
1390bdcd8170SKalle Valo 
1391bdcd8170SKalle Valo 	/* transfer One time Programmable data */
1392bdcd8170SKalle Valo 	status = ath6kl_upload_otp(ar);
1393bdcd8170SKalle Valo 	if (status)
1394bdcd8170SKalle Valo 		return status;
1395bdcd8170SKalle Valo 
1396bdcd8170SKalle Valo 	/* Download Target firmware */
1397bdcd8170SKalle Valo 	status = ath6kl_upload_firmware(ar);
1398bdcd8170SKalle Valo 	if (status)
1399bdcd8170SKalle Valo 		return status;
1400bdcd8170SKalle Valo 
1401bdcd8170SKalle Valo 	status = ath6kl_upload_patch(ar);
1402bdcd8170SKalle Valo 	if (status)
1403bdcd8170SKalle Valo 		return status;
1404bdcd8170SKalle Valo 
1405cd23c1c9SAlex Yang 	/* Download the test script */
1406cd23c1c9SAlex Yang 	status = ath6kl_upload_testscript(ar);
1407cd23c1c9SAlex Yang 	if (status)
1408cd23c1c9SAlex Yang 		return status;
1409cd23c1c9SAlex Yang 
1410bdcd8170SKalle Valo 	/* Restore system sleep */
1411bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1412bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, sleep);
1413bdcd8170SKalle Valo 	if (status)
1414bdcd8170SKalle Valo 		return status;
1415bdcd8170SKalle Valo 
1416bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1417bdcd8170SKalle Valo 	param = options | 0x20;
1418bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1419bdcd8170SKalle Valo 	if (status)
1420bdcd8170SKalle Valo 		return status;
1421bdcd8170SKalle Valo 
1422bdcd8170SKalle Valo 	return status;
1423bdcd8170SKalle Valo }
1424bdcd8170SKalle Valo 
142545eaa78fSKalle Valo int ath6kl_init_hw_params(struct ath6kl *ar)
1426a01ac414SKalle Valo {
14271b46dc04SKalle Valo 	const struct ath6kl_hw *uninitialized_var(hw);
1428856f4b31SKalle Valo 	int i;
1429bef26a7fSKalle Valo 
1430856f4b31SKalle Valo 	for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1431856f4b31SKalle Valo 		hw = &hw_list[i];
1432bef26a7fSKalle Valo 
1433856f4b31SKalle Valo 		if (hw->id == ar->version.target_ver)
1434a01ac414SKalle Valo 			break;
1435856f4b31SKalle Valo 	}
1436856f4b31SKalle Valo 
1437856f4b31SKalle Valo 	if (i == ARRAY_SIZE(hw_list)) {
1438a01ac414SKalle Valo 		ath6kl_err("Unsupported hardware version: 0x%x\n",
1439a01ac414SKalle Valo 			   ar->version.target_ver);
1440a01ac414SKalle Valo 		return -EINVAL;
1441a01ac414SKalle Valo 	}
1442a01ac414SKalle Valo 
1443856f4b31SKalle Valo 	ar->hw = *hw;
1444856f4b31SKalle Valo 
14456bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
14466bc36431SKalle Valo 		   "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
14476bc36431SKalle Valo 		   ar->version.target_ver, ar->target_type,
14486bc36431SKalle Valo 		   ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
14496bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
14506bc36431SKalle Valo 		   "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
14516bc36431SKalle Valo 		   ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
14526bc36431SKalle Valo 		   ar->hw.reserved_ram_size);
145339586bf2SRyan Hsu 	ath6kl_dbg(ATH6KL_DBG_BOOT,
145439586bf2SRyan Hsu 		   "refclk_hz %d uarttx_pin %d",
145539586bf2SRyan Hsu 		   ar->hw.refclk_hz, ar->hw.uarttx_pin);
14566bc36431SKalle Valo 
1457a01ac414SKalle Valo 	return 0;
1458a01ac414SKalle Valo }
1459a01ac414SKalle Valo 
1460293badf4SKalle Valo static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
1461293badf4SKalle Valo {
1462293badf4SKalle Valo 	switch (type) {
1463293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_SDIO:
1464293badf4SKalle Valo 		return "sdio";
1465293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_USB:
1466293badf4SKalle Valo 		return "usb";
1467293badf4SKalle Valo 	}
1468293badf4SKalle Valo 
1469293badf4SKalle Valo 	return NULL;
1470293badf4SKalle Valo }
1471293badf4SKalle Valo 
14725fe4dffbSKalle Valo int ath6kl_init_hw_start(struct ath6kl *ar)
147320459ee2SKalle Valo {
147420459ee2SKalle Valo 	long timeleft;
147520459ee2SKalle Valo 	int ret, i;
147620459ee2SKalle Valo 
14775fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
14785fe4dffbSKalle Valo 
147920459ee2SKalle Valo 	ret = ath6kl_hif_power_on(ar);
148020459ee2SKalle Valo 	if (ret)
148120459ee2SKalle Valo 		return ret;
148220459ee2SKalle Valo 
148320459ee2SKalle Valo 	ret = ath6kl_configure_target(ar);
148420459ee2SKalle Valo 	if (ret)
148520459ee2SKalle Valo 		goto err_power_off;
148620459ee2SKalle Valo 
148720459ee2SKalle Valo 	ret = ath6kl_init_upload(ar);
148820459ee2SKalle Valo 	if (ret)
148920459ee2SKalle Valo 		goto err_power_off;
149020459ee2SKalle Valo 
149120459ee2SKalle Valo 	/* Do we need to finish the BMI phase */
149220459ee2SKalle Valo 	/* FIXME: return error from ath6kl_bmi_done() */
149320459ee2SKalle Valo 	if (ath6kl_bmi_done(ar)) {
149420459ee2SKalle Valo 		ret = -EIO;
149520459ee2SKalle Valo 		goto err_power_off;
149620459ee2SKalle Valo 	}
149720459ee2SKalle Valo 
149820459ee2SKalle Valo 	/*
149920459ee2SKalle Valo 	 * The reason we have to wait for the target here is that the
150020459ee2SKalle Valo 	 * driver layer has to init BMI in order to set the host block
150120459ee2SKalle Valo 	 * size.
150220459ee2SKalle Valo 	 */
150320459ee2SKalle Valo 	if (ath6kl_htc_wait_target(ar->htc_target)) {
150420459ee2SKalle Valo 		ret = -EIO;
150520459ee2SKalle Valo 		goto err_power_off;
150620459ee2SKalle Valo 	}
150720459ee2SKalle Valo 
150820459ee2SKalle Valo 	if (ath6kl_init_service_ep(ar)) {
150920459ee2SKalle Valo 		ret = -EIO;
151020459ee2SKalle Valo 		goto err_cleanup_scatter;
151120459ee2SKalle Valo 	}
151220459ee2SKalle Valo 
151320459ee2SKalle Valo 	/* setup credit distribution */
1514*e76ac2bfSKalle Valo 	ath6kl_htc_credit_setup(ar->htc_target, &ar->credit_state_info);
151520459ee2SKalle Valo 
151620459ee2SKalle Valo 	/* start HTC */
151720459ee2SKalle Valo 	ret = ath6kl_htc_start(ar->htc_target);
151820459ee2SKalle Valo 	if (ret) {
151920459ee2SKalle Valo 		/* FIXME: call this */
152020459ee2SKalle Valo 		ath6kl_cookie_cleanup(ar);
152120459ee2SKalle Valo 		goto err_cleanup_scatter;
152220459ee2SKalle Valo 	}
152320459ee2SKalle Valo 
152420459ee2SKalle Valo 	/* Wait for Wmi event to be ready */
152520459ee2SKalle Valo 	timeleft = wait_event_interruptible_timeout(ar->event_wq,
152620459ee2SKalle Valo 						    test_bit(WMI_READY,
152720459ee2SKalle Valo 							     &ar->flag),
152820459ee2SKalle Valo 						    WMI_TIMEOUT);
152920459ee2SKalle Valo 
153020459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
153120459ee2SKalle Valo 
1532293badf4SKalle Valo 
1533293badf4SKalle Valo 	if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
153465a8b4ccSKalle Valo 		ath6kl_info("%s %s fw %s api %d%s\n",
1535293badf4SKalle Valo 			    ar->hw.name,
1536293badf4SKalle Valo 			    ath6kl_init_get_hif_name(ar->hif_type),
1537293badf4SKalle Valo 			    ar->wiphy->fw_version,
153865a8b4ccSKalle Valo 			    ar->fw_api,
1539293badf4SKalle Valo 			    test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
1540293badf4SKalle Valo 	}
1541293badf4SKalle Valo 
154220459ee2SKalle Valo 	if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
154320459ee2SKalle Valo 		ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
154420459ee2SKalle Valo 			   ATH6KL_ABI_VERSION, ar->version.abi_ver);
154520459ee2SKalle Valo 		ret = -EIO;
154620459ee2SKalle Valo 		goto err_htc_stop;
154720459ee2SKalle Valo 	}
154820459ee2SKalle Valo 
154920459ee2SKalle Valo 	if (!timeleft || signal_pending(current)) {
155020459ee2SKalle Valo 		ath6kl_err("wmi is not ready or wait was interrupted\n");
155120459ee2SKalle Valo 		ret = -EIO;
155220459ee2SKalle Valo 		goto err_htc_stop;
155320459ee2SKalle Valo 	}
155420459ee2SKalle Valo 
155520459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
155620459ee2SKalle Valo 
155720459ee2SKalle Valo 	/* communicate the wmi protocol verision to the target */
155820459ee2SKalle Valo 	/* FIXME: return error */
155920459ee2SKalle Valo 	if ((ath6kl_set_host_app_area(ar)) != 0)
156020459ee2SKalle Valo 		ath6kl_err("unable to set the host app area\n");
156120459ee2SKalle Valo 
156271f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++) {
156320459ee2SKalle Valo 		ret = ath6kl_target_config_wlan_params(ar, i);
156420459ee2SKalle Valo 		if (ret)
156520459ee2SKalle Valo 			goto err_htc_stop;
156620459ee2SKalle Valo 	}
156720459ee2SKalle Valo 
156876a9fbe2SKalle Valo 	ar->state = ATH6KL_STATE_ON;
156976a9fbe2SKalle Valo 
157020459ee2SKalle Valo 	return 0;
157120459ee2SKalle Valo 
157220459ee2SKalle Valo err_htc_stop:
157320459ee2SKalle Valo 	ath6kl_htc_stop(ar->htc_target);
157420459ee2SKalle Valo err_cleanup_scatter:
157520459ee2SKalle Valo 	ath6kl_hif_cleanup_scatter(ar);
157620459ee2SKalle Valo err_power_off:
157720459ee2SKalle Valo 	ath6kl_hif_power_off(ar);
157820459ee2SKalle Valo 
157920459ee2SKalle Valo 	return ret;
158020459ee2SKalle Valo }
158120459ee2SKalle Valo 
15825fe4dffbSKalle Valo int ath6kl_init_hw_stop(struct ath6kl *ar)
15835fe4dffbSKalle Valo {
15845fe4dffbSKalle Valo 	int ret;
15855fe4dffbSKalle Valo 
15865fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
15875fe4dffbSKalle Valo 
15885fe4dffbSKalle Valo 	ath6kl_htc_stop(ar->htc_target);
15895fe4dffbSKalle Valo 
15905fe4dffbSKalle Valo 	ath6kl_hif_stop(ar);
15915fe4dffbSKalle Valo 
15925fe4dffbSKalle Valo 	ath6kl_bmi_reset(ar);
15935fe4dffbSKalle Valo 
15945fe4dffbSKalle Valo 	ret = ath6kl_hif_power_off(ar);
15955fe4dffbSKalle Valo 	if (ret)
15965fe4dffbSKalle Valo 		ath6kl_warn("failed to power off hif: %d\n", ret);
15975fe4dffbSKalle Valo 
159876a9fbe2SKalle Valo 	ar->state = ATH6KL_STATE_OFF;
159976a9fbe2SKalle Valo 
16005fe4dffbSKalle Valo 	return 0;
16015fe4dffbSKalle Valo }
16025fe4dffbSKalle Valo 
1603c25889e8SKalle Valo /* FIXME: move this to cfg80211.c and rename to ath6kl_cfg80211_vif_stop() */
160455055976SVasanthakumar Thiagarajan void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
16056db8fa53SVasanthakumar Thiagarajan {
16066db8fa53SVasanthakumar Thiagarajan 	static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
16076db8fa53SVasanthakumar Thiagarajan 	bool discon_issued;
16086db8fa53SVasanthakumar Thiagarajan 
16096db8fa53SVasanthakumar Thiagarajan 	netif_stop_queue(vif->ndev);
16106db8fa53SVasanthakumar Thiagarajan 
16116db8fa53SVasanthakumar Thiagarajan 	clear_bit(WLAN_ENABLED, &vif->flags);
16126db8fa53SVasanthakumar Thiagarajan 
16136db8fa53SVasanthakumar Thiagarajan 	if (wmi_ready) {
16146db8fa53SVasanthakumar Thiagarajan 		discon_issued = test_bit(CONNECTED, &vif->flags) ||
16156db8fa53SVasanthakumar Thiagarajan 				test_bit(CONNECT_PEND, &vif->flags);
16166db8fa53SVasanthakumar Thiagarajan 		ath6kl_disconnect(vif);
16176db8fa53SVasanthakumar Thiagarajan 		del_timer(&vif->disconnect_timer);
16186db8fa53SVasanthakumar Thiagarajan 
16196db8fa53SVasanthakumar Thiagarajan 		if (discon_issued)
16206db8fa53SVasanthakumar Thiagarajan 			ath6kl_disconnect_event(vif, DISCONNECT_CMD,
16216db8fa53SVasanthakumar Thiagarajan 						(vif->nw_type & AP_NETWORK) ?
16226db8fa53SVasanthakumar Thiagarajan 						bcast_mac : vif->bssid,
16236db8fa53SVasanthakumar Thiagarajan 						0, NULL, 0);
16246db8fa53SVasanthakumar Thiagarajan 	}
16256db8fa53SVasanthakumar Thiagarajan 
16266db8fa53SVasanthakumar Thiagarajan 	if (vif->scan_req) {
16276db8fa53SVasanthakumar Thiagarajan 		cfg80211_scan_done(vif->scan_req, true);
16286db8fa53SVasanthakumar Thiagarajan 		vif->scan_req = NULL;
16296db8fa53SVasanthakumar Thiagarajan 	}
16306db8fa53SVasanthakumar Thiagarajan }
16316db8fa53SVasanthakumar Thiagarajan 
1632bdcd8170SKalle Valo void ath6kl_stop_txrx(struct ath6kl *ar)
1633bdcd8170SKalle Valo {
1634990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif, *tmp_vif;
16351d2a4456SVasanthakumar Thiagarajan 	int i;
1636bdcd8170SKalle Valo 
1637bdcd8170SKalle Valo 	set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1638bdcd8170SKalle Valo 
1639bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
1640bdcd8170SKalle Valo 		ath6kl_err("down_interruptible failed\n");
1641bdcd8170SKalle Valo 		return;
1642bdcd8170SKalle Valo 	}
1643bdcd8170SKalle Valo 
16441d2a4456SVasanthakumar Thiagarajan 	for (i = 0; i < AP_MAX_NUM_STA; i++)
16451d2a4456SVasanthakumar Thiagarajan 		aggr_reset_state(ar->sta_list[i].aggr_conn);
16461d2a4456SVasanthakumar Thiagarajan 
164711f6e40dSVasanthakumar Thiagarajan 	spin_lock_bh(&ar->list_lock);
1648990bd915SVasanthakumar Thiagarajan 	list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1649990bd915SVasanthakumar Thiagarajan 		list_del(&vif->list);
165011f6e40dSVasanthakumar Thiagarajan 		spin_unlock_bh(&ar->list_lock);
1651990bd915SVasanthakumar Thiagarajan 		ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
165227929723SVasanthakumar Thiagarajan 		rtnl_lock();
1653c25889e8SKalle Valo 		ath6kl_cfg80211_vif_cleanup(vif);
165427929723SVasanthakumar Thiagarajan 		rtnl_unlock();
165511f6e40dSVasanthakumar Thiagarajan 		spin_lock_bh(&ar->list_lock);
1656990bd915SVasanthakumar Thiagarajan 	}
165711f6e40dSVasanthakumar Thiagarajan 	spin_unlock_bh(&ar->list_lock);
1658bdcd8170SKalle Valo 
16596db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_READY, &ar->flag);
16606db8fa53SVasanthakumar Thiagarajan 
16616db8fa53SVasanthakumar Thiagarajan 	/*
16626db8fa53SVasanthakumar Thiagarajan 	 * After wmi_shudown all WMI events will be dropped. We
16636db8fa53SVasanthakumar Thiagarajan 	 * need to cleanup the buffers allocated in AP mode and
16646db8fa53SVasanthakumar Thiagarajan 	 * give disconnect notification to stack, which usually
16656db8fa53SVasanthakumar Thiagarajan 	 * happens in the disconnect_event. Simulate the disconnect
16666db8fa53SVasanthakumar Thiagarajan 	 * event by calling the function directly. Sometimes
16676db8fa53SVasanthakumar Thiagarajan 	 * disconnect_event will be received when the debug logs
16686db8fa53SVasanthakumar Thiagarajan 	 * are collected.
16696db8fa53SVasanthakumar Thiagarajan 	 */
16706db8fa53SVasanthakumar Thiagarajan 	ath6kl_wmi_shutdown(ar->wmi);
16716db8fa53SVasanthakumar Thiagarajan 
16726db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_ENABLED, &ar->flag);
16736db8fa53SVasanthakumar Thiagarajan 	if (ar->htc_target) {
16746db8fa53SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
16756db8fa53SVasanthakumar Thiagarajan 		ath6kl_htc_stop(ar->htc_target);
1676bdcd8170SKalle Valo 	}
1677bdcd8170SKalle Valo 
1678bdcd8170SKalle Valo 	/*
16796db8fa53SVasanthakumar Thiagarajan 	 * Try to reset the device if we can. The driver may have been
16806db8fa53SVasanthakumar Thiagarajan 	 * configure NOT to reset the target during a debug session.
1681bdcd8170SKalle Valo 	 */
16826db8fa53SVasanthakumar Thiagarajan 	ath6kl_dbg(ATH6KL_DBG_TRC,
16836db8fa53SVasanthakumar Thiagarajan 		   "attempting to reset target on instance destroy\n");
16846db8fa53SVasanthakumar Thiagarajan 	ath6kl_reset_device(ar, ar->target_type, true, true);
1685bdcd8170SKalle Valo 
16866db8fa53SVasanthakumar Thiagarajan 	clear_bit(WLAN_ENABLED, &ar->flag);
1687e8ad9a06SVasanthakumar Thiagarajan 
1688e8ad9a06SVasanthakumar Thiagarajan 	up(&ar->sem);
1689bdcd8170SKalle Valo }
1690d6a434d6SKalle Valo EXPORT_SYMBOL(ath6kl_stop_txrx);
1691