xref: /linux/drivers/net/wireless/ath/ath6kl/init.c (revision 516304b0f45614fb8967dc86ff681499204cdbb1)
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 
19*516304b0SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20*516304b0SJoe Perches 
21c6efe578SStephen Rothwell #include <linux/moduleparam.h>
22f7830202SSangwook Lee #include <linux/errno.h>
23d6a434d6SKalle Valo #include <linux/export.h>
2492ecbff4SSam Leffler #include <linux/of.h>
25bdcd8170SKalle Valo #include <linux/mmc/sdio_func.h>
26d6a434d6SKalle Valo 
27bdcd8170SKalle Valo #include "core.h"
28bdcd8170SKalle Valo #include "cfg80211.h"
29bdcd8170SKalle Valo #include "target.h"
30bdcd8170SKalle Valo #include "debug.h"
31bdcd8170SKalle Valo #include "hif-ops.h"
32bdcd8170SKalle Valo 
33856f4b31SKalle Valo static const struct ath6kl_hw hw_list[] = {
34856f4b31SKalle Valo 	{
350d0192baSKalle Valo 		.id				= AR6003_HW_2_0_VERSION,
36293badf4SKalle Valo 		.name				= "ar6003 hw 2.0",
37856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
38856f4b31SKalle Valo 		.app_load_addr			= 0x543180,
39856f4b31SKalle Valo 		.board_ext_data_addr		= 0x57e500,
40856f4b31SKalle Valo 		.reserved_ram_size		= 6912,
4139586bf2SRyan Hsu 		.refclk_hz			= 26000000,
4239586bf2SRyan Hsu 		.uarttx_pin			= 8,
43856f4b31SKalle Valo 
44856f4b31SKalle Valo 		/* hw2.0 needs override address hardcoded */
45856f4b31SKalle Valo 		.app_start_override_addr	= 0x944C00,
46d1a9421dSKalle Valo 
47c0038972SKalle Valo 		.fw = {
48c0038972SKalle Valo 			.dir		= AR6003_HW_2_0_FW_DIR,
49c0038972SKalle Valo 			.otp		= AR6003_HW_2_0_OTP_FILE,
50d1a9421dSKalle Valo 			.fw		= AR6003_HW_2_0_FIRMWARE_FILE,
51c0038972SKalle Valo 			.tcmd		= AR6003_HW_2_0_TCMD_FIRMWARE_FILE,
52c0038972SKalle Valo 			.patch		= AR6003_HW_2_0_PATCH_FILE,
53c0038972SKalle Valo 		},
54c0038972SKalle Valo 
55d1a9421dSKalle Valo 		.fw_board		= AR6003_HW_2_0_BOARD_DATA_FILE,
56d1a9421dSKalle Valo 		.fw_default_board	= AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE,
57856f4b31SKalle Valo 	},
58856f4b31SKalle Valo 	{
590d0192baSKalle Valo 		.id				= AR6003_HW_2_1_1_VERSION,
60293badf4SKalle Valo 		.name				= "ar6003 hw 2.1.1",
61856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57ff74,
62856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
63856f4b31SKalle Valo 		.board_ext_data_addr		= 0x542330,
64856f4b31SKalle Valo 		.reserved_ram_size		= 512,
6539586bf2SRyan Hsu 		.refclk_hz			= 26000000,
6639586bf2SRyan Hsu 		.uarttx_pin			= 8,
67cd23c1c9SAlex Yang 		.testscript_addr		= 0x57ef74,
68d1a9421dSKalle Valo 
69c0038972SKalle Valo 		.fw = {
70c0038972SKalle Valo 			.dir		= AR6003_HW_2_1_1_FW_DIR,
71c0038972SKalle Valo 			.otp		= AR6003_HW_2_1_1_OTP_FILE,
72d1a9421dSKalle Valo 			.fw		= AR6003_HW_2_1_1_FIRMWARE_FILE,
73c0038972SKalle Valo 			.tcmd		= AR6003_HW_2_1_1_TCMD_FIRMWARE_FILE,
74c0038972SKalle Valo 			.patch		= AR6003_HW_2_1_1_PATCH_FILE,
75cd23c1c9SAlex Yang 			.utf		= AR6003_HW_2_1_1_UTF_FIRMWARE_FILE,
76cd23c1c9SAlex Yang 			.testscript	= AR6003_HW_2_1_1_TESTSCRIPT_FILE,
77c0038972SKalle Valo 		},
78c0038972SKalle Valo 
79d1a9421dSKalle Valo 		.fw_board		= AR6003_HW_2_1_1_BOARD_DATA_FILE,
80d1a9421dSKalle Valo 		.fw_default_board = AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE,
81856f4b31SKalle Valo 	},
82856f4b31SKalle Valo 	{
830d0192baSKalle Valo 		.id				= AR6004_HW_1_0_VERSION,
84293badf4SKalle Valo 		.name				= "ar6004 hw 1.0",
85856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
86856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
87856f4b31SKalle Valo 		.board_ext_data_addr		= 0x437000,
88856f4b31SKalle Valo 		.reserved_ram_size		= 19456,
890d4d72bfSKalle Valo 		.board_addr			= 0x433900,
9039586bf2SRyan Hsu 		.refclk_hz			= 26000000,
9139586bf2SRyan Hsu 		.uarttx_pin			= 11,
92d1a9421dSKalle Valo 
93c0038972SKalle Valo 		.fw = {
94c0038972SKalle Valo 			.dir		= AR6004_HW_1_0_FW_DIR,
95d1a9421dSKalle Valo 			.fw		= AR6004_HW_1_0_FIRMWARE_FILE,
96c0038972SKalle Valo 		},
97c0038972SKalle Valo 
98d1a9421dSKalle Valo 		.fw_board		= AR6004_HW_1_0_BOARD_DATA_FILE,
99d1a9421dSKalle Valo 		.fw_default_board	= AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE,
100856f4b31SKalle Valo 	},
101856f4b31SKalle Valo 	{
1020d0192baSKalle Valo 		.id				= AR6004_HW_1_1_VERSION,
103293badf4SKalle Valo 		.name				= "ar6004 hw 1.1",
104856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
105856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
106856f4b31SKalle Valo 		.board_ext_data_addr		= 0x437000,
107856f4b31SKalle Valo 		.reserved_ram_size		= 11264,
1080d4d72bfSKalle Valo 		.board_addr			= 0x43d400,
10939586bf2SRyan Hsu 		.refclk_hz			= 40000000,
11039586bf2SRyan Hsu 		.uarttx_pin			= 11,
111d1a9421dSKalle Valo 
112c0038972SKalle Valo 		.fw = {
113c0038972SKalle Valo 			.dir		= AR6004_HW_1_1_FW_DIR,
114d1a9421dSKalle Valo 			.fw		= AR6004_HW_1_1_FIRMWARE_FILE,
115c0038972SKalle Valo 		},
116c0038972SKalle Valo 
117d1a9421dSKalle Valo 		.fw_board		= AR6004_HW_1_1_BOARD_DATA_FILE,
118d1a9421dSKalle Valo 		.fw_default_board	= AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE,
119856f4b31SKalle Valo 	},
120856f4b31SKalle Valo };
121856f4b31SKalle Valo 
122bdcd8170SKalle Valo /*
123bdcd8170SKalle Valo  * Include definitions here that can be used to tune the WLAN module
124bdcd8170SKalle Valo  * behavior. Different customers can tune the behavior as per their needs,
125bdcd8170SKalle Valo  * here.
126bdcd8170SKalle Valo  */
127bdcd8170SKalle Valo 
128bdcd8170SKalle Valo /*
129bdcd8170SKalle Valo  * This configuration item enable/disable keepalive support.
130bdcd8170SKalle Valo  * Keepalive support: In the absence of any data traffic to AP, null
131bdcd8170SKalle Valo  * frames will be sent to the AP at periodic interval, to keep the association
132bdcd8170SKalle Valo  * active. This configuration item defines the periodic interval.
133bdcd8170SKalle Valo  * Use value of zero to disable keepalive support
134bdcd8170SKalle Valo  * Default: 60 seconds
135bdcd8170SKalle Valo  */
136bdcd8170SKalle Valo #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
137bdcd8170SKalle Valo 
138bdcd8170SKalle Valo /*
139bdcd8170SKalle Valo  * This configuration item sets the value of disconnect timeout
140bdcd8170SKalle Valo  * Firmware delays sending the disconnec event to the host for this
141bdcd8170SKalle Valo  * timeout after is gets disconnected from the current AP.
142bdcd8170SKalle Valo  * If the firmware successly roams within the disconnect timeout
143bdcd8170SKalle Valo  * it sends a new connect event
144bdcd8170SKalle Valo  */
145bdcd8170SKalle Valo #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
146bdcd8170SKalle Valo 
147bdcd8170SKalle Valo 
148bdcd8170SKalle Valo #define ATH6KL_DATA_OFFSET    64
149bdcd8170SKalle Valo struct sk_buff *ath6kl_buf_alloc(int size)
150bdcd8170SKalle Valo {
151bdcd8170SKalle Valo 	struct sk_buff *skb;
152bdcd8170SKalle Valo 	u16 reserved;
153bdcd8170SKalle Valo 
154bdcd8170SKalle Valo 	/* Add chacheline space at front and back of buffer */
155bdcd8170SKalle Valo 	reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
1561df94a85SVasanthakumar Thiagarajan 		   sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
157bdcd8170SKalle Valo 	skb = dev_alloc_skb(size + reserved);
158bdcd8170SKalle Valo 
159bdcd8170SKalle Valo 	if (skb)
160bdcd8170SKalle Valo 		skb_reserve(skb, reserved - L1_CACHE_BYTES);
161bdcd8170SKalle Valo 	return skb;
162bdcd8170SKalle Valo }
163bdcd8170SKalle Valo 
164e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_profile_info(struct ath6kl_vif *vif)
165bdcd8170SKalle Valo {
1663450334fSVasanthakumar Thiagarajan 	vif->ssid_len = 0;
1673450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
1683450334fSVasanthakumar Thiagarajan 
1693450334fSVasanthakumar Thiagarajan 	vif->dot11_auth_mode = OPEN_AUTH;
1703450334fSVasanthakumar Thiagarajan 	vif->auth_mode = NONE_AUTH;
1713450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto = NONE_CRYPT;
1723450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto_len = 0;
1733450334fSVasanthakumar Thiagarajan 	vif->grp_crypto = NONE_CRYPT;
1743450334fSVasanthakumar Thiagarajan 	vif->grp_crypto_len = 0;
1756f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
1768c8b65e3SVasanthakumar Thiagarajan 	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
1778c8b65e3SVasanthakumar Thiagarajan 	memset(vif->bssid, 0, sizeof(vif->bssid));
178f74bac54SVasanthakumar Thiagarajan 	vif->bss_ch = 0;
179bdcd8170SKalle Valo }
180bdcd8170SKalle Valo 
181bdcd8170SKalle Valo static int ath6kl_set_host_app_area(struct ath6kl *ar)
182bdcd8170SKalle Valo {
183bdcd8170SKalle Valo 	u32 address, data;
184bdcd8170SKalle Valo 	struct host_app_area host_app_area;
185bdcd8170SKalle Valo 
186bdcd8170SKalle Valo 	/* Fetch the address of the host_app_area_s
187bdcd8170SKalle Valo 	 * instance in the host interest area */
188bdcd8170SKalle Valo 	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
18931024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, address);
190bdcd8170SKalle Valo 
191addb44beSKalle Valo 	if (ath6kl_diag_read32(ar, address, &data))
192bdcd8170SKalle Valo 		return -EIO;
193bdcd8170SKalle Valo 
19431024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, data);
195cbf49a6fSKalle Valo 	host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
196addb44beSKalle Valo 	if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
197addb44beSKalle Valo 			      sizeof(struct host_app_area)))
198bdcd8170SKalle Valo 		return -EIO;
199bdcd8170SKalle Valo 
200bdcd8170SKalle Valo 	return 0;
201bdcd8170SKalle Valo }
202bdcd8170SKalle Valo 
203bdcd8170SKalle Valo static inline void set_ac2_ep_map(struct ath6kl *ar,
204bdcd8170SKalle Valo 				  u8 ac,
205bdcd8170SKalle Valo 				  enum htc_endpoint_id ep)
206bdcd8170SKalle Valo {
207bdcd8170SKalle Valo 	ar->ac2ep_map[ac] = ep;
208bdcd8170SKalle Valo 	ar->ep2ac_map[ep] = ac;
209bdcd8170SKalle Valo }
210bdcd8170SKalle Valo 
211bdcd8170SKalle Valo /* connect to a service */
212bdcd8170SKalle Valo static int ath6kl_connectservice(struct ath6kl *ar,
213bdcd8170SKalle Valo 				 struct htc_service_connect_req  *con_req,
214bdcd8170SKalle Valo 				 char *desc)
215bdcd8170SKalle Valo {
216bdcd8170SKalle Valo 	int status;
217bdcd8170SKalle Valo 	struct htc_service_connect_resp response;
218bdcd8170SKalle Valo 
219bdcd8170SKalle Valo 	memset(&response, 0, sizeof(response));
220bdcd8170SKalle Valo 
221ad226ec2SKalle Valo 	status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
222bdcd8170SKalle Valo 	if (status) {
223bdcd8170SKalle Valo 		ath6kl_err("failed to connect to %s service status:%d\n",
224bdcd8170SKalle Valo 			   desc, status);
225bdcd8170SKalle Valo 		return status;
226bdcd8170SKalle Valo 	}
227bdcd8170SKalle Valo 
228bdcd8170SKalle Valo 	switch (con_req->svc_id) {
229bdcd8170SKalle Valo 	case WMI_CONTROL_SVC:
230bdcd8170SKalle Valo 		if (test_bit(WMI_ENABLED, &ar->flag))
231bdcd8170SKalle Valo 			ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
232bdcd8170SKalle Valo 		ar->ctrl_ep = response.endpoint;
233bdcd8170SKalle Valo 		break;
234bdcd8170SKalle Valo 	case WMI_DATA_BE_SVC:
235bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
236bdcd8170SKalle Valo 		break;
237bdcd8170SKalle Valo 	case WMI_DATA_BK_SVC:
238bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
239bdcd8170SKalle Valo 		break;
240bdcd8170SKalle Valo 	case WMI_DATA_VI_SVC:
241bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
242bdcd8170SKalle Valo 		break;
243bdcd8170SKalle Valo 	case WMI_DATA_VO_SVC:
244bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
245bdcd8170SKalle Valo 		break;
246bdcd8170SKalle Valo 	default:
247bdcd8170SKalle Valo 		ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
248bdcd8170SKalle Valo 		return -EINVAL;
249bdcd8170SKalle Valo 	}
250bdcd8170SKalle Valo 
251bdcd8170SKalle Valo 	return 0;
252bdcd8170SKalle Valo }
253bdcd8170SKalle Valo 
254bdcd8170SKalle Valo static int ath6kl_init_service_ep(struct ath6kl *ar)
255bdcd8170SKalle Valo {
256bdcd8170SKalle Valo 	struct htc_service_connect_req connect;
257bdcd8170SKalle Valo 
258bdcd8170SKalle Valo 	memset(&connect, 0, sizeof(connect));
259bdcd8170SKalle Valo 
260bdcd8170SKalle Valo 	/* these fields are the same for all service endpoints */
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 
544991b27eaSKalle Valo 	param = ar->hw.board_ext_data_addr;
545991b27eaSKalle Valo 	ram_reserved_size = ar->hw.reserved_ram_size;
546bdcd8170SKalle Valo 
54724fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
548bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
549bdcd8170SKalle Valo 		return -EIO;
550bdcd8170SKalle Valo 	}
551991b27eaSKalle Valo 
55224fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
55324fc32b3SKalle Valo 				  ram_reserved_size) != 0) {
554bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
555bdcd8170SKalle Valo 		return -EIO;
556bdcd8170SKalle Valo 	}
557bdcd8170SKalle Valo 
558bdcd8170SKalle Valo 	/* set the block size for the target */
559bdcd8170SKalle Valo 	if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
560bdcd8170SKalle Valo 		/* use default number of control buffers */
561bdcd8170SKalle Valo 		return -EIO;
562bdcd8170SKalle Valo 
56339586bf2SRyan Hsu 	/* Configure GPIO AR600x UART */
56424fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
56524fc32b3SKalle Valo 				       ar->hw.uarttx_pin);
56639586bf2SRyan Hsu 	if (status)
56739586bf2SRyan Hsu 		return status;
56839586bf2SRyan Hsu 
56939586bf2SRyan Hsu 	/* Configure target refclk_hz */
57024fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz, ar->hw.refclk_hz);
57139586bf2SRyan Hsu 	if (status)
57239586bf2SRyan Hsu 		return status;
57339586bf2SRyan Hsu 
574bdcd8170SKalle Valo 	return 0;
575bdcd8170SKalle Valo }
576bdcd8170SKalle Valo 
577bdcd8170SKalle Valo /* firmware upload */
578bdcd8170SKalle Valo static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
579bdcd8170SKalle Valo 			 u8 **fw, size_t *fw_len)
580bdcd8170SKalle Valo {
581bdcd8170SKalle Valo 	const struct firmware *fw_entry;
582bdcd8170SKalle Valo 	int ret;
583bdcd8170SKalle Valo 
584bdcd8170SKalle Valo 	ret = request_firmware(&fw_entry, filename, ar->dev);
585bdcd8170SKalle Valo 	if (ret)
586bdcd8170SKalle Valo 		return ret;
587bdcd8170SKalle Valo 
588bdcd8170SKalle Valo 	*fw_len = fw_entry->size;
589bdcd8170SKalle Valo 	*fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
590bdcd8170SKalle Valo 
591bdcd8170SKalle Valo 	if (*fw == NULL)
592bdcd8170SKalle Valo 		ret = -ENOMEM;
593bdcd8170SKalle Valo 
594bdcd8170SKalle Valo 	release_firmware(fw_entry);
595bdcd8170SKalle Valo 
596bdcd8170SKalle Valo 	return ret;
597bdcd8170SKalle Valo }
598bdcd8170SKalle Valo 
59992ecbff4SSam Leffler #ifdef CONFIG_OF
60092ecbff4SSam Leffler /*
60192ecbff4SSam Leffler  * Check the device tree for a board-id and use it to construct
60292ecbff4SSam Leffler  * the pathname to the firmware file.  Used (for now) to find a
60392ecbff4SSam Leffler  * fallback to the "bdata.bin" file--typically a symlink to the
60492ecbff4SSam Leffler  * appropriate board-specific file.
60592ecbff4SSam Leffler  */
60692ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
60792ecbff4SSam Leffler {
60892ecbff4SSam Leffler 	static const char *board_id_prop = "atheros,board-id";
60992ecbff4SSam Leffler 	struct device_node *node;
61092ecbff4SSam Leffler 	char board_filename[64];
61192ecbff4SSam Leffler 	const char *board_id;
61292ecbff4SSam Leffler 	int ret;
61392ecbff4SSam Leffler 
61492ecbff4SSam Leffler 	for_each_compatible_node(node, NULL, "atheros,ath6kl") {
61592ecbff4SSam Leffler 		board_id = of_get_property(node, board_id_prop, NULL);
61692ecbff4SSam Leffler 		if (board_id == NULL) {
61792ecbff4SSam Leffler 			ath6kl_warn("No \"%s\" property on %s node.\n",
61892ecbff4SSam Leffler 				    board_id_prop, node->name);
61992ecbff4SSam Leffler 			continue;
62092ecbff4SSam Leffler 		}
62192ecbff4SSam Leffler 		snprintf(board_filename, sizeof(board_filename),
622c0038972SKalle Valo 			 "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
62392ecbff4SSam Leffler 
62492ecbff4SSam Leffler 		ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
62592ecbff4SSam Leffler 				    &ar->fw_board_len);
62692ecbff4SSam Leffler 		if (ret) {
62792ecbff4SSam Leffler 			ath6kl_err("Failed to get DT board file %s: %d\n",
62892ecbff4SSam Leffler 				   board_filename, ret);
62992ecbff4SSam Leffler 			continue;
63092ecbff4SSam Leffler 		}
63192ecbff4SSam Leffler 		return true;
63292ecbff4SSam Leffler 	}
63392ecbff4SSam Leffler 	return false;
63492ecbff4SSam Leffler }
63592ecbff4SSam Leffler #else
63692ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
63792ecbff4SSam Leffler {
63892ecbff4SSam Leffler 	return false;
63992ecbff4SSam Leffler }
64092ecbff4SSam Leffler #endif /* CONFIG_OF */
64192ecbff4SSam Leffler 
642bdcd8170SKalle Valo static int ath6kl_fetch_board_file(struct ath6kl *ar)
643bdcd8170SKalle Valo {
644bdcd8170SKalle Valo 	const char *filename;
645bdcd8170SKalle Valo 	int ret;
646bdcd8170SKalle Valo 
647772c31eeSKalle Valo 	if (ar->fw_board != NULL)
648772c31eeSKalle Valo 		return 0;
649772c31eeSKalle Valo 
650d1a9421dSKalle Valo 	if (WARN_ON(ar->hw.fw_board == NULL))
651d1a9421dSKalle Valo 		return -EINVAL;
652d1a9421dSKalle Valo 
653d1a9421dSKalle Valo 	filename = ar->hw.fw_board;
654bdcd8170SKalle Valo 
655bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
656bdcd8170SKalle Valo 			    &ar->fw_board_len);
657bdcd8170SKalle Valo 	if (ret == 0) {
658bdcd8170SKalle Valo 		/* managed to get proper board file */
659bdcd8170SKalle Valo 		return 0;
660bdcd8170SKalle Valo 	}
661bdcd8170SKalle Valo 
66292ecbff4SSam Leffler 	if (check_device_tree(ar)) {
66392ecbff4SSam Leffler 		/* got board file from device tree */
66492ecbff4SSam Leffler 		return 0;
66592ecbff4SSam Leffler 	}
66692ecbff4SSam Leffler 
667bdcd8170SKalle Valo 	/* there was no proper board file, try to use default instead */
668bdcd8170SKalle Valo 	ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
669bdcd8170SKalle Valo 		    filename, ret);
670bdcd8170SKalle Valo 
671d1a9421dSKalle Valo 	filename = ar->hw.fw_default_board;
672bdcd8170SKalle Valo 
673bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
674bdcd8170SKalle Valo 			    &ar->fw_board_len);
675bdcd8170SKalle Valo 	if (ret) {
676bdcd8170SKalle Valo 		ath6kl_err("Failed to get default board file %s: %d\n",
677bdcd8170SKalle Valo 			   filename, ret);
678bdcd8170SKalle Valo 		return ret;
679bdcd8170SKalle Valo 	}
680bdcd8170SKalle Valo 
681bdcd8170SKalle Valo 	ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
682bdcd8170SKalle Valo 	ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
683bdcd8170SKalle Valo 
684bdcd8170SKalle Valo 	return 0;
685bdcd8170SKalle Valo }
686bdcd8170SKalle Valo 
687772c31eeSKalle Valo static int ath6kl_fetch_otp_file(struct ath6kl *ar)
688772c31eeSKalle Valo {
689c0038972SKalle Valo 	char filename[100];
690772c31eeSKalle Valo 	int ret;
691772c31eeSKalle Valo 
692772c31eeSKalle Valo 	if (ar->fw_otp != NULL)
693772c31eeSKalle Valo 		return 0;
694772c31eeSKalle Valo 
695c0038972SKalle Valo 	if (ar->hw.fw.otp == NULL) {
696d1a9421dSKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
697d1a9421dSKalle Valo 			   "no OTP file configured for this hw\n");
698772c31eeSKalle Valo 		return 0;
699772c31eeSKalle Valo 	}
700772c31eeSKalle Valo 
701c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
702c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.otp);
703d1a9421dSKalle Valo 
704772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
705772c31eeSKalle Valo 			    &ar->fw_otp_len);
706772c31eeSKalle Valo 	if (ret) {
707772c31eeSKalle Valo 		ath6kl_err("Failed to get OTP file %s: %d\n",
708772c31eeSKalle Valo 			   filename, ret);
709772c31eeSKalle Valo 		return ret;
710772c31eeSKalle Valo 	}
711772c31eeSKalle Valo 
712772c31eeSKalle Valo 	return 0;
713772c31eeSKalle Valo }
714772c31eeSKalle Valo 
7155f1127ffSKalle Valo static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
716772c31eeSKalle Valo {
717c0038972SKalle Valo 	char filename[100];
718772c31eeSKalle Valo 	int ret;
719772c31eeSKalle Valo 
7205f1127ffSKalle Valo 	if (ar->testmode == 0)
721772c31eeSKalle Valo 		return 0;
722772c31eeSKalle Valo 
7235f1127ffSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
7245f1127ffSKalle Valo 
7255f1127ffSKalle Valo 	if (ar->testmode == 2) {
726cd23c1c9SAlex Yang 		if (ar->hw.fw.utf == NULL) {
727cd23c1c9SAlex Yang 			ath6kl_warn("testmode 2 not supported\n");
728cd23c1c9SAlex Yang 			return -EOPNOTSUPP;
729cd23c1c9SAlex Yang 		}
730cd23c1c9SAlex Yang 
731cd23c1c9SAlex Yang 		snprintf(filename, sizeof(filename), "%s/%s",
732cd23c1c9SAlex Yang 			 ar->hw.fw.dir, ar->hw.fw.utf);
733cd23c1c9SAlex Yang 	} else {
734c0038972SKalle Valo 		if (ar->hw.fw.tcmd == NULL) {
735cd23c1c9SAlex Yang 			ath6kl_warn("testmode 1 not supported\n");
736772c31eeSKalle Valo 			return -EOPNOTSUPP;
737772c31eeSKalle Valo 		}
738772c31eeSKalle Valo 
739c0038972SKalle Valo 		snprintf(filename, sizeof(filename), "%s/%s",
740c0038972SKalle Valo 			 ar->hw.fw.dir, ar->hw.fw.tcmd);
741cd23c1c9SAlex Yang 	}
7425f1127ffSKalle Valo 
743772c31eeSKalle Valo 	set_bit(TESTMODE, &ar->flag);
744772c31eeSKalle Valo 
7455f1127ffSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
7465f1127ffSKalle Valo 	if (ret) {
7475f1127ffSKalle Valo 		ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
7485f1127ffSKalle Valo 			   ar->testmode, filename, ret);
7495f1127ffSKalle Valo 		return ret;
750772c31eeSKalle Valo 	}
751772c31eeSKalle Valo 
7525f1127ffSKalle Valo 	return 0;
7535f1127ffSKalle Valo }
7545f1127ffSKalle Valo 
7555f1127ffSKalle Valo static int ath6kl_fetch_fw_file(struct ath6kl *ar)
7565f1127ffSKalle Valo {
7575f1127ffSKalle Valo 	char filename[100];
7585f1127ffSKalle Valo 	int ret;
7595f1127ffSKalle Valo 
7605f1127ffSKalle Valo 	if (ar->fw != NULL)
7615f1127ffSKalle Valo 		return 0;
7625f1127ffSKalle Valo 
763c0038972SKalle Valo 	/* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
764c0038972SKalle Valo 	if (WARN_ON(ar->hw.fw.fw == NULL))
765d1a9421dSKalle Valo 		return -EINVAL;
766d1a9421dSKalle Valo 
767c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
768c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.fw);
769772c31eeSKalle Valo 
770772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
771772c31eeSKalle Valo 	if (ret) {
772772c31eeSKalle Valo 		ath6kl_err("Failed to get firmware file %s: %d\n",
773772c31eeSKalle Valo 			   filename, ret);
774772c31eeSKalle Valo 		return ret;
775772c31eeSKalle Valo 	}
776772c31eeSKalle Valo 
777772c31eeSKalle Valo 	return 0;
778772c31eeSKalle Valo }
779772c31eeSKalle Valo 
780772c31eeSKalle Valo static int ath6kl_fetch_patch_file(struct ath6kl *ar)
781772c31eeSKalle Valo {
782c0038972SKalle Valo 	char filename[100];
783772c31eeSKalle Valo 	int ret;
784772c31eeSKalle Valo 
785d1a9421dSKalle Valo 	if (ar->fw_patch != NULL)
786772c31eeSKalle Valo 		return 0;
787772c31eeSKalle Valo 
788c0038972SKalle Valo 	if (ar->hw.fw.patch == NULL)
789d1a9421dSKalle Valo 		return 0;
790d1a9421dSKalle Valo 
791c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
792c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.patch);
793d1a9421dSKalle Valo 
794772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
795772c31eeSKalle Valo 			    &ar->fw_patch_len);
796772c31eeSKalle Valo 	if (ret) {
797772c31eeSKalle Valo 		ath6kl_err("Failed to get patch file %s: %d\n",
798772c31eeSKalle Valo 			   filename, ret);
799772c31eeSKalle Valo 		return ret;
800772c31eeSKalle Valo 	}
801772c31eeSKalle Valo 
802772c31eeSKalle Valo 	return 0;
803772c31eeSKalle Valo }
804772c31eeSKalle Valo 
805cd23c1c9SAlex Yang static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
806cd23c1c9SAlex Yang {
807cd23c1c9SAlex Yang 	char filename[100];
808cd23c1c9SAlex Yang 	int ret;
809cd23c1c9SAlex Yang 
8105f1127ffSKalle Valo 	if (ar->testmode != 2)
811cd23c1c9SAlex Yang 		return 0;
812cd23c1c9SAlex Yang 
813cd23c1c9SAlex Yang 	if (ar->fw_testscript != NULL)
814cd23c1c9SAlex Yang 		return 0;
815cd23c1c9SAlex Yang 
816cd23c1c9SAlex Yang 	if (ar->hw.fw.testscript == NULL)
817cd23c1c9SAlex Yang 		return 0;
818cd23c1c9SAlex Yang 
819cd23c1c9SAlex Yang 	snprintf(filename, sizeof(filename), "%s/%s",
820cd23c1c9SAlex Yang 		 ar->hw.fw.dir, ar->hw.fw.testscript);
821cd23c1c9SAlex Yang 
822cd23c1c9SAlex Yang 	ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
823cd23c1c9SAlex Yang 				&ar->fw_testscript_len);
824cd23c1c9SAlex Yang 	if (ret) {
825cd23c1c9SAlex Yang 		ath6kl_err("Failed to get testscript file %s: %d\n",
826cd23c1c9SAlex Yang 			   filename, ret);
827cd23c1c9SAlex Yang 		return ret;
828cd23c1c9SAlex Yang 	}
829cd23c1c9SAlex Yang 
830cd23c1c9SAlex Yang 	return 0;
831cd23c1c9SAlex Yang }
832cd23c1c9SAlex Yang 
83350d41234SKalle Valo static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
834772c31eeSKalle Valo {
835772c31eeSKalle Valo 	int ret;
836772c31eeSKalle Valo 
837772c31eeSKalle Valo 	ret = ath6kl_fetch_otp_file(ar);
838772c31eeSKalle Valo 	if (ret)
839772c31eeSKalle Valo 		return ret;
840772c31eeSKalle Valo 
841772c31eeSKalle Valo 	ret = ath6kl_fetch_fw_file(ar);
842772c31eeSKalle Valo 	if (ret)
843772c31eeSKalle Valo 		return ret;
844772c31eeSKalle Valo 
845772c31eeSKalle Valo 	ret = ath6kl_fetch_patch_file(ar);
846772c31eeSKalle Valo 	if (ret)
847772c31eeSKalle Valo 		return ret;
848772c31eeSKalle Valo 
849cd23c1c9SAlex Yang 	ret = ath6kl_fetch_testscript_file(ar);
850cd23c1c9SAlex Yang 	if (ret)
851cd23c1c9SAlex Yang 		return ret;
852cd23c1c9SAlex Yang 
853772c31eeSKalle Valo 	return 0;
854772c31eeSKalle Valo }
855bdcd8170SKalle Valo 
85665a8b4ccSKalle Valo static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
85750d41234SKalle Valo {
85850d41234SKalle Valo 	size_t magic_len, len, ie_len;
85950d41234SKalle Valo 	const struct firmware *fw;
86050d41234SKalle Valo 	struct ath6kl_fw_ie *hdr;
861c0038972SKalle Valo 	char filename[100];
86250d41234SKalle Valo 	const u8 *data;
86397e0496dSKalle Valo 	int ret, ie_id, i, index, bit;
8648a137480SKalle Valo 	__le32 *val;
86550d41234SKalle Valo 
86665a8b4ccSKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
86750d41234SKalle Valo 
86850d41234SKalle Valo 	ret = request_firmware(&fw, filename, ar->dev);
86950d41234SKalle Valo 	if (ret)
87050d41234SKalle Valo 		return ret;
87150d41234SKalle Valo 
87250d41234SKalle Valo 	data = fw->data;
87350d41234SKalle Valo 	len = fw->size;
87450d41234SKalle Valo 
87550d41234SKalle Valo 	/* magic also includes the null byte, check that as well */
87650d41234SKalle Valo 	magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
87750d41234SKalle Valo 
87850d41234SKalle Valo 	if (len < magic_len) {
87950d41234SKalle Valo 		ret = -EINVAL;
88050d41234SKalle Valo 		goto out;
88150d41234SKalle Valo 	}
88250d41234SKalle Valo 
88350d41234SKalle Valo 	if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
88450d41234SKalle Valo 		ret = -EINVAL;
88550d41234SKalle Valo 		goto out;
88650d41234SKalle Valo 	}
88750d41234SKalle Valo 
88850d41234SKalle Valo 	len -= magic_len;
88950d41234SKalle Valo 	data += magic_len;
89050d41234SKalle Valo 
89150d41234SKalle Valo 	/* loop elements */
89250d41234SKalle Valo 	while (len > sizeof(struct ath6kl_fw_ie)) {
89350d41234SKalle Valo 		/* hdr is unaligned! */
89450d41234SKalle Valo 		hdr = (struct ath6kl_fw_ie *) data;
89550d41234SKalle Valo 
89650d41234SKalle Valo 		ie_id = le32_to_cpup(&hdr->id);
89750d41234SKalle Valo 		ie_len = le32_to_cpup(&hdr->len);
89850d41234SKalle Valo 
89950d41234SKalle Valo 		len -= sizeof(*hdr);
90050d41234SKalle Valo 		data += sizeof(*hdr);
90150d41234SKalle Valo 
90250d41234SKalle Valo 		if (len < ie_len) {
90350d41234SKalle Valo 			ret = -EINVAL;
90450d41234SKalle Valo 			goto out;
90550d41234SKalle Valo 		}
90650d41234SKalle Valo 
90750d41234SKalle Valo 		switch (ie_id) {
90850d41234SKalle Valo 		case ATH6KL_FW_IE_OTP_IMAGE:
909ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
9106bc36431SKalle Valo 				   ie_len);
9116bc36431SKalle Valo 
91250d41234SKalle Valo 			ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
91350d41234SKalle Valo 
91450d41234SKalle Valo 			if (ar->fw_otp == NULL) {
91550d41234SKalle Valo 				ret = -ENOMEM;
91650d41234SKalle Valo 				goto out;
91750d41234SKalle Valo 			}
91850d41234SKalle Valo 
91950d41234SKalle Valo 			ar->fw_otp_len = ie_len;
92050d41234SKalle Valo 			break;
92150d41234SKalle Valo 		case ATH6KL_FW_IE_FW_IMAGE:
922ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
9236bc36431SKalle Valo 				   ie_len);
9246bc36431SKalle Valo 
9255f1127ffSKalle Valo 			/* in testmode we already might have a fw file */
9265f1127ffSKalle Valo 			if (ar->fw != NULL)
9275f1127ffSKalle Valo 				break;
9285f1127ffSKalle Valo 
92950d41234SKalle Valo 			ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
93050d41234SKalle Valo 
93150d41234SKalle Valo 			if (ar->fw == NULL) {
93250d41234SKalle Valo 				ret = -ENOMEM;
93350d41234SKalle Valo 				goto out;
93450d41234SKalle Valo 			}
93550d41234SKalle Valo 
93650d41234SKalle Valo 			ar->fw_len = ie_len;
93750d41234SKalle Valo 			break;
93850d41234SKalle Valo 		case ATH6KL_FW_IE_PATCH_IMAGE:
939ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
9406bc36431SKalle Valo 				   ie_len);
9416bc36431SKalle Valo 
94250d41234SKalle Valo 			ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
94350d41234SKalle Valo 
94450d41234SKalle Valo 			if (ar->fw_patch == NULL) {
94550d41234SKalle Valo 				ret = -ENOMEM;
94650d41234SKalle Valo 				goto out;
94750d41234SKalle Valo 			}
94850d41234SKalle Valo 
94950d41234SKalle Valo 			ar->fw_patch_len = ie_len;
95050d41234SKalle Valo 			break;
9518a137480SKalle Valo 		case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
9528a137480SKalle Valo 			val = (__le32 *) data;
9538a137480SKalle Valo 			ar->hw.reserved_ram_size = le32_to_cpup(val);
9546bc36431SKalle Valo 
9556bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
9566bc36431SKalle Valo 				   "found reserved ram size ie 0x%d\n",
9576bc36431SKalle Valo 				   ar->hw.reserved_ram_size);
9588a137480SKalle Valo 			break;
95997e0496dSKalle Valo 		case ATH6KL_FW_IE_CAPABILITIES:
960277d90f4SKalle Valo 			if (ie_len < DIV_ROUND_UP(ATH6KL_FW_CAPABILITY_MAX, 8))
961277d90f4SKalle Valo 				break;
962277d90f4SKalle Valo 
9636bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
964ef548626SKalle Valo 				   "found firmware capabilities ie (%zd B)\n",
9656bc36431SKalle Valo 				   ie_len);
9666bc36431SKalle Valo 
96797e0496dSKalle Valo 			for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
968277d90f4SKalle Valo 				index = i / 8;
96997e0496dSKalle Valo 				bit = i % 8;
97097e0496dSKalle Valo 
97197e0496dSKalle Valo 				if (data[index] & (1 << bit))
97297e0496dSKalle Valo 					__set_bit(i, ar->fw_capabilities);
97397e0496dSKalle Valo 			}
9746bc36431SKalle Valo 
9756bc36431SKalle Valo 			ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
9766bc36431SKalle Valo 					ar->fw_capabilities,
9776bc36431SKalle Valo 					sizeof(ar->fw_capabilities));
97897e0496dSKalle Valo 			break;
9791b4304daSKalle Valo 		case ATH6KL_FW_IE_PATCH_ADDR:
9801b4304daSKalle Valo 			if (ie_len != sizeof(*val))
9811b4304daSKalle Valo 				break;
9821b4304daSKalle Valo 
9831b4304daSKalle Valo 			val = (__le32 *) data;
9841b4304daSKalle Valo 			ar->hw.dataset_patch_addr = le32_to_cpup(val);
9856bc36431SKalle Valo 
9866bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
98703ef0250SKalle Valo 				   "found patch address ie 0x%x\n",
9886bc36431SKalle Valo 				   ar->hw.dataset_patch_addr);
9891b4304daSKalle Valo 			break;
99003ef0250SKalle Valo 		case ATH6KL_FW_IE_BOARD_ADDR:
99103ef0250SKalle Valo 			if (ie_len != sizeof(*val))
99203ef0250SKalle Valo 				break;
99303ef0250SKalle Valo 
99403ef0250SKalle Valo 			val = (__le32 *) data;
99503ef0250SKalle Valo 			ar->hw.board_addr = le32_to_cpup(val);
99603ef0250SKalle Valo 
99703ef0250SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
99803ef0250SKalle Valo 				   "found board address ie 0x%x\n",
99903ef0250SKalle Valo 				   ar->hw.board_addr);
100003ef0250SKalle Valo 			break;
1001368b1b0fSKalle Valo 		case ATH6KL_FW_IE_VIF_MAX:
1002368b1b0fSKalle Valo 			if (ie_len != sizeof(*val))
1003368b1b0fSKalle Valo 				break;
1004368b1b0fSKalle Valo 
1005368b1b0fSKalle Valo 			val = (__le32 *) data;
1006368b1b0fSKalle Valo 			ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
1007368b1b0fSKalle Valo 					    ATH6KL_VIF_MAX);
1008368b1b0fSKalle Valo 
1009f143379dSVasanthakumar Thiagarajan 			if (ar->vif_max > 1 && !ar->p2p)
1010f143379dSVasanthakumar Thiagarajan 				ar->max_norm_iface = 2;
1011f143379dSVasanthakumar Thiagarajan 
1012368b1b0fSKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1013368b1b0fSKalle Valo 				   "found vif max ie %d\n", ar->vif_max);
1014368b1b0fSKalle Valo 			break;
101550d41234SKalle Valo 		default:
10166bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
101750d41234SKalle Valo 				   le32_to_cpup(&hdr->id));
101850d41234SKalle Valo 			break;
101950d41234SKalle Valo 		}
102050d41234SKalle Valo 
102150d41234SKalle Valo 		len -= ie_len;
102250d41234SKalle Valo 		data += ie_len;
102350d41234SKalle Valo 	};
102450d41234SKalle Valo 
102550d41234SKalle Valo 	ret = 0;
102650d41234SKalle Valo out:
102750d41234SKalle Valo 	release_firmware(fw);
102850d41234SKalle Valo 
102950d41234SKalle Valo 	return ret;
103050d41234SKalle Valo }
103150d41234SKalle Valo 
103245eaa78fSKalle Valo int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
103350d41234SKalle Valo {
103450d41234SKalle Valo 	int ret;
103550d41234SKalle Valo 
103650d41234SKalle Valo 	ret = ath6kl_fetch_board_file(ar);
103750d41234SKalle Valo 	if (ret)
103850d41234SKalle Valo 		return ret;
103950d41234SKalle Valo 
10405f1127ffSKalle Valo 	ret = ath6kl_fetch_testmode_file(ar);
10415f1127ffSKalle Valo 	if (ret)
10425f1127ffSKalle Valo 		return ret;
10435f1127ffSKalle Valo 
104465a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
10456bc36431SKalle Valo 	if (ret == 0) {
104665a8b4ccSKalle Valo 		ar->fw_api = 3;
104765a8b4ccSKalle Valo 		goto out;
104865a8b4ccSKalle Valo 	}
104965a8b4ccSKalle Valo 
105065a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
105165a8b4ccSKalle Valo 	if (ret == 0) {
105265a8b4ccSKalle Valo 		ar->fw_api = 2;
105365a8b4ccSKalle Valo 		goto out;
10546bc36431SKalle Valo 	}
105550d41234SKalle Valo 
105650d41234SKalle Valo 	ret = ath6kl_fetch_fw_api1(ar);
105750d41234SKalle Valo 	if (ret)
105850d41234SKalle Valo 		return ret;
105950d41234SKalle Valo 
106065a8b4ccSKalle Valo 	ar->fw_api = 1;
106165a8b4ccSKalle Valo 
106265a8b4ccSKalle Valo out:
106365a8b4ccSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
10646bc36431SKalle Valo 
106550d41234SKalle Valo 	return 0;
106650d41234SKalle Valo }
106750d41234SKalle Valo 
1068bdcd8170SKalle Valo static int ath6kl_upload_board_file(struct ath6kl *ar)
1069bdcd8170SKalle Valo {
1070bdcd8170SKalle Valo 	u32 board_address, board_ext_address, param;
107131024d99SKevin Fang 	u32 board_data_size, board_ext_data_size;
1072bdcd8170SKalle Valo 	int ret;
1073bdcd8170SKalle Valo 
1074772c31eeSKalle Valo 	if (WARN_ON(ar->fw_board == NULL))
1075772c31eeSKalle Valo 		return -ENOENT;
1076bdcd8170SKalle Valo 
107731024d99SKevin Fang 	/*
107831024d99SKevin Fang 	 * Determine where in Target RAM to write Board Data.
107931024d99SKevin Fang 	 * For AR6004, host determine Target RAM address for
108031024d99SKevin Fang 	 * writing board data.
108131024d99SKevin Fang 	 */
10820d4d72bfSKalle Valo 	if (ar->hw.board_addr != 0) {
1083b0fc7c1aSKalle Valo 		board_address = ar->hw.board_addr;
108424fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_data,
1085b0fc7c1aSKalle Valo 				      board_address);
108631024d99SKevin Fang 	} else {
108780fb2686SKalle Valo 		ath6kl_bmi_read_hi32(ar, hi_board_data, &board_address);
108831024d99SKevin Fang 	}
108931024d99SKevin Fang 
1090bdcd8170SKalle Valo 	/* determine where in target ram to write extended board data */
109180fb2686SKalle Valo 	ath6kl_bmi_read_hi32(ar, hi_board_ext_data, &board_ext_address);
1092bdcd8170SKalle Valo 
109350e2740bSKalle Valo 	if (ar->target_type == TARGET_TYPE_AR6003 &&
109450e2740bSKalle Valo 	    board_ext_address == 0) {
1095bdcd8170SKalle Valo 		ath6kl_err("Failed to get board file target address.\n");
1096bdcd8170SKalle Valo 		return -EINVAL;
1097bdcd8170SKalle Valo 	}
1098bdcd8170SKalle Valo 
109931024d99SKevin Fang 	switch (ar->target_type) {
110031024d99SKevin Fang 	case TARGET_TYPE_AR6003:
110131024d99SKevin Fang 		board_data_size = AR6003_BOARD_DATA_SZ;
110231024d99SKevin Fang 		board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1103fb1ac2efSPrasanna Kumar 		if (ar->fw_board_len > (board_data_size + board_ext_data_size))
1104fb1ac2efSPrasanna Kumar 			board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
110531024d99SKevin Fang 		break;
110631024d99SKevin Fang 	case TARGET_TYPE_AR6004:
110731024d99SKevin Fang 		board_data_size = AR6004_BOARD_DATA_SZ;
110831024d99SKevin Fang 		board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
110931024d99SKevin Fang 		break;
111031024d99SKevin Fang 	default:
111131024d99SKevin Fang 		WARN_ON(1);
111231024d99SKevin Fang 		return -EINVAL;
111331024d99SKevin Fang 		break;
111431024d99SKevin Fang 	}
111531024d99SKevin Fang 
111650e2740bSKalle Valo 	if (board_ext_address &&
111750e2740bSKalle Valo 	    ar->fw_board_len == (board_data_size + board_ext_data_size)) {
111831024d99SKevin Fang 
1119bdcd8170SKalle Valo 		/* write extended board data */
11206bc36431SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
11216bc36431SKalle Valo 			   "writing extended board data to 0x%x (%d B)\n",
11226bc36431SKalle Valo 			   board_ext_address, board_ext_data_size);
11236bc36431SKalle Valo 
1124bdcd8170SKalle Valo 		ret = ath6kl_bmi_write(ar, board_ext_address,
112531024d99SKevin Fang 				       ar->fw_board + board_data_size,
112631024d99SKevin Fang 				       board_ext_data_size);
1127bdcd8170SKalle Valo 		if (ret) {
1128bdcd8170SKalle Valo 			ath6kl_err("Failed to write extended board data: %d\n",
1129bdcd8170SKalle Valo 				   ret);
1130bdcd8170SKalle Valo 			return ret;
1131bdcd8170SKalle Valo 		}
1132bdcd8170SKalle Valo 
1133bdcd8170SKalle Valo 		/* record that extended board data is initialized */
113431024d99SKevin Fang 		param = (board_ext_data_size << 16) | 1;
113531024d99SKevin Fang 
113624fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
1137bdcd8170SKalle Valo 	}
1138bdcd8170SKalle Valo 
113931024d99SKevin Fang 	if (ar->fw_board_len < board_data_size) {
1140bdcd8170SKalle Valo 		ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1141bdcd8170SKalle Valo 		ret = -EINVAL;
1142bdcd8170SKalle Valo 		return ret;
1143bdcd8170SKalle Valo 	}
1144bdcd8170SKalle Valo 
11456bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
11466bc36431SKalle Valo 		   board_address, board_data_size);
11476bc36431SKalle Valo 
1148bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
114931024d99SKevin Fang 			       board_data_size);
1150bdcd8170SKalle Valo 
1151bdcd8170SKalle Valo 	if (ret) {
1152bdcd8170SKalle Valo 		ath6kl_err("Board file bmi write failed: %d\n", ret);
1153bdcd8170SKalle Valo 		return ret;
1154bdcd8170SKalle Valo 	}
1155bdcd8170SKalle Valo 
1156bdcd8170SKalle Valo 	/* record the fact that Board Data IS initialized */
115724fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, 1);
1158bdcd8170SKalle Valo 
1159bdcd8170SKalle Valo 	return ret;
1160bdcd8170SKalle Valo }
1161bdcd8170SKalle Valo 
1162bdcd8170SKalle Valo static int ath6kl_upload_otp(struct ath6kl *ar)
1163bdcd8170SKalle Valo {
1164bdcd8170SKalle Valo 	u32 address, param;
1165bef26a7fSKalle Valo 	bool from_hw = false;
1166bdcd8170SKalle Valo 	int ret;
1167bdcd8170SKalle Valo 
116850e2740bSKalle Valo 	if (ar->fw_otp == NULL)
116950e2740bSKalle Valo 		return 0;
1170bdcd8170SKalle Valo 
1171a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1172bdcd8170SKalle Valo 
1173ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
11746bc36431SKalle Valo 		   ar->fw_otp_len);
11756bc36431SKalle Valo 
1176bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1177bdcd8170SKalle Valo 				       ar->fw_otp_len);
1178bdcd8170SKalle Valo 	if (ret) {
1179bdcd8170SKalle Valo 		ath6kl_err("Failed to upload OTP file: %d\n", ret);
1180bdcd8170SKalle Valo 		return ret;
1181bdcd8170SKalle Valo 	}
1182bdcd8170SKalle Valo 
1183639d0b89SKalle Valo 	/* read firmware start address */
118480fb2686SKalle Valo 	ret = ath6kl_bmi_read_hi32(ar, hi_app_start, &address);
1185639d0b89SKalle Valo 
1186639d0b89SKalle Valo 	if (ret) {
1187639d0b89SKalle Valo 		ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1188639d0b89SKalle Valo 		return ret;
1189639d0b89SKalle Valo 	}
1190639d0b89SKalle Valo 
1191bef26a7fSKalle Valo 	if (ar->hw.app_start_override_addr == 0) {
1192639d0b89SKalle Valo 		ar->hw.app_start_override_addr = address;
1193bef26a7fSKalle Valo 		from_hw = true;
1194bef26a7fSKalle Valo 	}
1195639d0b89SKalle Valo 
1196bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1197bef26a7fSKalle Valo 		   from_hw ? " (from hw)" : "",
11986bc36431SKalle Valo 		   ar->hw.app_start_override_addr);
11996bc36431SKalle Valo 
1200bdcd8170SKalle Valo 	/* execute the OTP code */
1201bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1202bef26a7fSKalle Valo 		   ar->hw.app_start_override_addr);
1203bdcd8170SKalle Valo 	param = 0;
1204bef26a7fSKalle Valo 	ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
1205bdcd8170SKalle Valo 
1206bdcd8170SKalle Valo 	return ret;
1207bdcd8170SKalle Valo }
1208bdcd8170SKalle Valo 
1209bdcd8170SKalle Valo static int ath6kl_upload_firmware(struct ath6kl *ar)
1210bdcd8170SKalle Valo {
1211bdcd8170SKalle Valo 	u32 address;
1212bdcd8170SKalle Valo 	int ret;
1213bdcd8170SKalle Valo 
1214772c31eeSKalle Valo 	if (WARN_ON(ar->fw == NULL))
121550e2740bSKalle Valo 		return 0;
1216bdcd8170SKalle Valo 
1217a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1218bdcd8170SKalle Valo 
1219ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
12206bc36431SKalle Valo 		   address, ar->fw_len);
12216bc36431SKalle Valo 
1222bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1223bdcd8170SKalle Valo 
1224bdcd8170SKalle Valo 	if (ret) {
1225bdcd8170SKalle Valo 		ath6kl_err("Failed to write firmware: %d\n", ret);
1226bdcd8170SKalle Valo 		return ret;
1227bdcd8170SKalle Valo 	}
1228bdcd8170SKalle Valo 
122931024d99SKevin Fang 	/*
123031024d99SKevin Fang 	 * Set starting address for firmware
123131024d99SKevin Fang 	 * Don't need to setup app_start override addr on AR6004
123231024d99SKevin Fang 	 */
123331024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1234a01ac414SKalle Valo 		address = ar->hw.app_start_override_addr;
1235bdcd8170SKalle Valo 		ath6kl_bmi_set_app_start(ar, address);
123631024d99SKevin Fang 	}
1237bdcd8170SKalle Valo 	return ret;
1238bdcd8170SKalle Valo }
1239bdcd8170SKalle Valo 
1240bdcd8170SKalle Valo static int ath6kl_upload_patch(struct ath6kl *ar)
1241bdcd8170SKalle Valo {
124224fc32b3SKalle Valo 	u32 address;
1243bdcd8170SKalle Valo 	int ret;
1244bdcd8170SKalle Valo 
124550e2740bSKalle Valo 	if (ar->fw_patch == NULL)
124650e2740bSKalle Valo 		return 0;
1247bdcd8170SKalle Valo 
1248a01ac414SKalle Valo 	address = ar->hw.dataset_patch_addr;
1249bdcd8170SKalle Valo 
1250ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
12516bc36431SKalle Valo 		   address, ar->fw_patch_len);
12526bc36431SKalle Valo 
1253bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1254bdcd8170SKalle Valo 	if (ret) {
1255bdcd8170SKalle Valo 		ath6kl_err("Failed to write patch file: %d\n", ret);
1256bdcd8170SKalle Valo 		return ret;
1257bdcd8170SKalle Valo 	}
1258bdcd8170SKalle Valo 
125924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
1260bdcd8170SKalle Valo 
1261bdcd8170SKalle Valo 	return 0;
1262bdcd8170SKalle Valo }
1263bdcd8170SKalle Valo 
1264cd23c1c9SAlex Yang static int ath6kl_upload_testscript(struct ath6kl *ar)
1265cd23c1c9SAlex Yang {
126624fc32b3SKalle Valo 	u32 address;
1267cd23c1c9SAlex Yang 	int ret;
1268cd23c1c9SAlex Yang 
12695f1127ffSKalle Valo 	if (ar->testmode != 2)
1270cd23c1c9SAlex Yang 		return 0;
1271cd23c1c9SAlex Yang 
1272cd23c1c9SAlex Yang 	if (ar->fw_testscript == NULL)
1273cd23c1c9SAlex Yang 		return 0;
1274cd23c1c9SAlex Yang 
1275cd23c1c9SAlex Yang 	address = ar->hw.testscript_addr;
1276cd23c1c9SAlex Yang 
1277cd23c1c9SAlex Yang 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
1278cd23c1c9SAlex Yang 		   address, ar->fw_testscript_len);
1279cd23c1c9SAlex Yang 
1280cd23c1c9SAlex Yang 	ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
1281cd23c1c9SAlex Yang 		ar->fw_testscript_len);
1282cd23c1c9SAlex Yang 	if (ret) {
1283cd23c1c9SAlex Yang 		ath6kl_err("Failed to write testscript file: %d\n", ret);
1284cd23c1c9SAlex Yang 		return ret;
1285cd23c1c9SAlex Yang 	}
1286cd23c1c9SAlex Yang 
128724fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
128824fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
128924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
1290cd23c1c9SAlex Yang 
1291cd23c1c9SAlex Yang 	return 0;
1292cd23c1c9SAlex Yang }
1293cd23c1c9SAlex Yang 
1294bdcd8170SKalle Valo static int ath6kl_init_upload(struct ath6kl *ar)
1295bdcd8170SKalle Valo {
1296bdcd8170SKalle Valo 	u32 param, options, sleep, address;
1297bdcd8170SKalle Valo 	int status = 0;
1298bdcd8170SKalle Valo 
129931024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6003 &&
130031024d99SKevin Fang 	    ar->target_type != TARGET_TYPE_AR6004)
1301bdcd8170SKalle Valo 		return -EINVAL;
1302bdcd8170SKalle Valo 
1303bdcd8170SKalle Valo 	/* temporarily disable system sleep */
1304bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1305bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1306bdcd8170SKalle Valo 	if (status)
1307bdcd8170SKalle Valo 		return status;
1308bdcd8170SKalle Valo 
1309bdcd8170SKalle Valo 	options = param;
1310bdcd8170SKalle Valo 
1311bdcd8170SKalle Valo 	param |= ATH6KL_OPTION_SLEEP_DISABLE;
1312bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1313bdcd8170SKalle Valo 	if (status)
1314bdcd8170SKalle Valo 		return status;
1315bdcd8170SKalle Valo 
1316bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1317bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1318bdcd8170SKalle Valo 	if (status)
1319bdcd8170SKalle Valo 		return status;
1320bdcd8170SKalle Valo 
1321bdcd8170SKalle Valo 	sleep = param;
1322bdcd8170SKalle Valo 
1323bdcd8170SKalle Valo 	param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1324bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1325bdcd8170SKalle Valo 	if (status)
1326bdcd8170SKalle Valo 		return status;
1327bdcd8170SKalle Valo 
1328bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1329bdcd8170SKalle Valo 		   options, sleep);
1330bdcd8170SKalle Valo 
1331bdcd8170SKalle Valo 	/* program analog PLL register */
133231024d99SKevin Fang 	/* no need to control 40/44MHz clock on AR6004 */
133331024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1334bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1335bdcd8170SKalle Valo 					      0xF9104001);
133631024d99SKevin Fang 
1337bdcd8170SKalle Valo 		if (status)
1338bdcd8170SKalle Valo 			return status;
1339bdcd8170SKalle Valo 
1340bdcd8170SKalle Valo 		/* Run at 80/88MHz by default */
1341bdcd8170SKalle Valo 		param = SM(CPU_CLOCK_STANDARD, 1);
1342bdcd8170SKalle Valo 
1343bdcd8170SKalle Valo 		address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1344bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1345bdcd8170SKalle Valo 		if (status)
1346bdcd8170SKalle Valo 			return status;
134731024d99SKevin Fang 	}
1348bdcd8170SKalle Valo 
1349bdcd8170SKalle Valo 	param = 0;
1350bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1351bdcd8170SKalle Valo 	param = SM(LPO_CAL_ENABLE, 1);
1352bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1353bdcd8170SKalle Valo 	if (status)
1354bdcd8170SKalle Valo 		return status;
1355bdcd8170SKalle Valo 
1356bdcd8170SKalle Valo 	/* WAR to avoid SDIO CRC err */
13574480bb59SRaja Mani 	if (ar->version.target_ver == AR6003_HW_2_0_VERSION ||
13584480bb59SRaja Mani 	    ar->version.target_ver == AR6003_HW_2_1_1_VERSION) {
1359bdcd8170SKalle Valo 		ath6kl_err("temporary war to avoid sdio crc error\n");
1360bdcd8170SKalle Valo 
1361bdcd8170SKalle Valo 		param = 0x20;
1362bdcd8170SKalle Valo 
1363bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1364bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1365bdcd8170SKalle Valo 		if (status)
1366bdcd8170SKalle Valo 			return status;
1367bdcd8170SKalle Valo 
1368bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1369bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1370bdcd8170SKalle Valo 		if (status)
1371bdcd8170SKalle Valo 			return status;
1372bdcd8170SKalle Valo 
1373bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1374bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1375bdcd8170SKalle Valo 		if (status)
1376bdcd8170SKalle Valo 			return status;
1377bdcd8170SKalle Valo 
1378bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1379bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1380bdcd8170SKalle Valo 		if (status)
1381bdcd8170SKalle Valo 			return status;
1382bdcd8170SKalle Valo 	}
1383bdcd8170SKalle Valo 
1384bdcd8170SKalle Valo 	/* write EEPROM data to Target RAM */
1385bdcd8170SKalle Valo 	status = ath6kl_upload_board_file(ar);
1386bdcd8170SKalle Valo 	if (status)
1387bdcd8170SKalle Valo 		return status;
1388bdcd8170SKalle Valo 
1389bdcd8170SKalle Valo 	/* transfer One time Programmable data */
1390bdcd8170SKalle Valo 	status = ath6kl_upload_otp(ar);
1391bdcd8170SKalle Valo 	if (status)
1392bdcd8170SKalle Valo 		return status;
1393bdcd8170SKalle Valo 
1394bdcd8170SKalle Valo 	/* Download Target firmware */
1395bdcd8170SKalle Valo 	status = ath6kl_upload_firmware(ar);
1396bdcd8170SKalle Valo 	if (status)
1397bdcd8170SKalle Valo 		return status;
1398bdcd8170SKalle Valo 
1399bdcd8170SKalle Valo 	status = ath6kl_upload_patch(ar);
1400bdcd8170SKalle Valo 	if (status)
1401bdcd8170SKalle Valo 		return status;
1402bdcd8170SKalle Valo 
1403cd23c1c9SAlex Yang 	/* Download the test script */
1404cd23c1c9SAlex Yang 	status = ath6kl_upload_testscript(ar);
1405cd23c1c9SAlex Yang 	if (status)
1406cd23c1c9SAlex Yang 		return status;
1407cd23c1c9SAlex Yang 
1408bdcd8170SKalle Valo 	/* Restore system sleep */
1409bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1410bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, sleep);
1411bdcd8170SKalle Valo 	if (status)
1412bdcd8170SKalle Valo 		return status;
1413bdcd8170SKalle Valo 
1414bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1415bdcd8170SKalle Valo 	param = options | 0x20;
1416bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1417bdcd8170SKalle Valo 	if (status)
1418bdcd8170SKalle Valo 		return status;
1419bdcd8170SKalle Valo 
1420bdcd8170SKalle Valo 	return status;
1421bdcd8170SKalle Valo }
1422bdcd8170SKalle Valo 
142345eaa78fSKalle Valo int ath6kl_init_hw_params(struct ath6kl *ar)
1424a01ac414SKalle Valo {
14251b46dc04SKalle Valo 	const struct ath6kl_hw *uninitialized_var(hw);
1426856f4b31SKalle Valo 	int i;
1427bef26a7fSKalle Valo 
1428856f4b31SKalle Valo 	for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1429856f4b31SKalle Valo 		hw = &hw_list[i];
1430bef26a7fSKalle Valo 
1431856f4b31SKalle Valo 		if (hw->id == ar->version.target_ver)
1432a01ac414SKalle Valo 			break;
1433856f4b31SKalle Valo 	}
1434856f4b31SKalle Valo 
1435856f4b31SKalle Valo 	if (i == ARRAY_SIZE(hw_list)) {
1436a01ac414SKalle Valo 		ath6kl_err("Unsupported hardware version: 0x%x\n",
1437a01ac414SKalle Valo 			   ar->version.target_ver);
1438a01ac414SKalle Valo 		return -EINVAL;
1439a01ac414SKalle Valo 	}
1440a01ac414SKalle Valo 
1441856f4b31SKalle Valo 	ar->hw = *hw;
1442856f4b31SKalle Valo 
14436bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
14446bc36431SKalle Valo 		   "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
14456bc36431SKalle Valo 		   ar->version.target_ver, ar->target_type,
14466bc36431SKalle Valo 		   ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
14476bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
14486bc36431SKalle Valo 		   "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
14496bc36431SKalle Valo 		   ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
14506bc36431SKalle Valo 		   ar->hw.reserved_ram_size);
145139586bf2SRyan Hsu 	ath6kl_dbg(ATH6KL_DBG_BOOT,
145239586bf2SRyan Hsu 		   "refclk_hz %d uarttx_pin %d",
145339586bf2SRyan Hsu 		   ar->hw.refclk_hz, ar->hw.uarttx_pin);
14546bc36431SKalle Valo 
1455a01ac414SKalle Valo 	return 0;
1456a01ac414SKalle Valo }
1457a01ac414SKalle Valo 
1458293badf4SKalle Valo static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
1459293badf4SKalle Valo {
1460293badf4SKalle Valo 	switch (type) {
1461293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_SDIO:
1462293badf4SKalle Valo 		return "sdio";
1463293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_USB:
1464293badf4SKalle Valo 		return "usb";
1465293badf4SKalle Valo 	}
1466293badf4SKalle Valo 
1467293badf4SKalle Valo 	return NULL;
1468293badf4SKalle Valo }
1469293badf4SKalle Valo 
14705fe4dffbSKalle Valo int ath6kl_init_hw_start(struct ath6kl *ar)
147120459ee2SKalle Valo {
147220459ee2SKalle Valo 	long timeleft;
147320459ee2SKalle Valo 	int ret, i;
147420459ee2SKalle Valo 
14755fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
14765fe4dffbSKalle Valo 
147720459ee2SKalle Valo 	ret = ath6kl_hif_power_on(ar);
147820459ee2SKalle Valo 	if (ret)
147920459ee2SKalle Valo 		return ret;
148020459ee2SKalle Valo 
148120459ee2SKalle Valo 	ret = ath6kl_configure_target(ar);
148220459ee2SKalle Valo 	if (ret)
148320459ee2SKalle Valo 		goto err_power_off;
148420459ee2SKalle Valo 
148520459ee2SKalle Valo 	ret = ath6kl_init_upload(ar);
148620459ee2SKalle Valo 	if (ret)
148720459ee2SKalle Valo 		goto err_power_off;
148820459ee2SKalle Valo 
148920459ee2SKalle Valo 	/* Do we need to finish the BMI phase */
149020459ee2SKalle Valo 	/* FIXME: return error from ath6kl_bmi_done() */
149120459ee2SKalle Valo 	if (ath6kl_bmi_done(ar)) {
149220459ee2SKalle Valo 		ret = -EIO;
149320459ee2SKalle Valo 		goto err_power_off;
149420459ee2SKalle Valo 	}
149520459ee2SKalle Valo 
149620459ee2SKalle Valo 	/*
149720459ee2SKalle Valo 	 * The reason we have to wait for the target here is that the
149820459ee2SKalle Valo 	 * driver layer has to init BMI in order to set the host block
149920459ee2SKalle Valo 	 * size.
150020459ee2SKalle Valo 	 */
150120459ee2SKalle Valo 	if (ath6kl_htc_wait_target(ar->htc_target)) {
150220459ee2SKalle Valo 		ret = -EIO;
150320459ee2SKalle Valo 		goto err_power_off;
150420459ee2SKalle Valo 	}
150520459ee2SKalle Valo 
150620459ee2SKalle Valo 	if (ath6kl_init_service_ep(ar)) {
150720459ee2SKalle Valo 		ret = -EIO;
150820459ee2SKalle Valo 		goto err_cleanup_scatter;
150920459ee2SKalle Valo 	}
151020459ee2SKalle Valo 
151120459ee2SKalle Valo 	/* setup credit distribution */
151220459ee2SKalle Valo 	ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
151320459ee2SKalle Valo 
151420459ee2SKalle Valo 	/* start HTC */
151520459ee2SKalle Valo 	ret = ath6kl_htc_start(ar->htc_target);
151620459ee2SKalle Valo 	if (ret) {
151720459ee2SKalle Valo 		/* FIXME: call this */
151820459ee2SKalle Valo 		ath6kl_cookie_cleanup(ar);
151920459ee2SKalle Valo 		goto err_cleanup_scatter;
152020459ee2SKalle Valo 	}
152120459ee2SKalle Valo 
152220459ee2SKalle Valo 	/* Wait for Wmi event to be ready */
152320459ee2SKalle Valo 	timeleft = wait_event_interruptible_timeout(ar->event_wq,
152420459ee2SKalle Valo 						    test_bit(WMI_READY,
152520459ee2SKalle Valo 							     &ar->flag),
152620459ee2SKalle Valo 						    WMI_TIMEOUT);
152720459ee2SKalle Valo 
152820459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
152920459ee2SKalle Valo 
1530293badf4SKalle Valo 
1531293badf4SKalle Valo 	if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
153265a8b4ccSKalle Valo 		ath6kl_info("%s %s fw %s api %d%s\n",
1533293badf4SKalle Valo 			    ar->hw.name,
1534293badf4SKalle Valo 			    ath6kl_init_get_hif_name(ar->hif_type),
1535293badf4SKalle Valo 			    ar->wiphy->fw_version,
153665a8b4ccSKalle Valo 			    ar->fw_api,
1537293badf4SKalle Valo 			    test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
1538293badf4SKalle Valo 	}
1539293badf4SKalle Valo 
154020459ee2SKalle Valo 	if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
154120459ee2SKalle Valo 		ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
154220459ee2SKalle Valo 			   ATH6KL_ABI_VERSION, ar->version.abi_ver);
154320459ee2SKalle Valo 		ret = -EIO;
154420459ee2SKalle Valo 		goto err_htc_stop;
154520459ee2SKalle Valo 	}
154620459ee2SKalle Valo 
154720459ee2SKalle Valo 	if (!timeleft || signal_pending(current)) {
154820459ee2SKalle Valo 		ath6kl_err("wmi is not ready or wait was interrupted\n");
154920459ee2SKalle Valo 		ret = -EIO;
155020459ee2SKalle Valo 		goto err_htc_stop;
155120459ee2SKalle Valo 	}
155220459ee2SKalle Valo 
155320459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
155420459ee2SKalle Valo 
155520459ee2SKalle Valo 	/* communicate the wmi protocol verision to the target */
155620459ee2SKalle Valo 	/* FIXME: return error */
155720459ee2SKalle Valo 	if ((ath6kl_set_host_app_area(ar)) != 0)
155820459ee2SKalle Valo 		ath6kl_err("unable to set the host app area\n");
155920459ee2SKalle Valo 
156071f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++) {
156120459ee2SKalle Valo 		ret = ath6kl_target_config_wlan_params(ar, i);
156220459ee2SKalle Valo 		if (ret)
156320459ee2SKalle Valo 			goto err_htc_stop;
156420459ee2SKalle Valo 	}
156520459ee2SKalle Valo 
156676a9fbe2SKalle Valo 	ar->state = ATH6KL_STATE_ON;
156776a9fbe2SKalle Valo 
156820459ee2SKalle Valo 	return 0;
156920459ee2SKalle Valo 
157020459ee2SKalle Valo err_htc_stop:
157120459ee2SKalle Valo 	ath6kl_htc_stop(ar->htc_target);
157220459ee2SKalle Valo err_cleanup_scatter:
157320459ee2SKalle Valo 	ath6kl_hif_cleanup_scatter(ar);
157420459ee2SKalle Valo err_power_off:
157520459ee2SKalle Valo 	ath6kl_hif_power_off(ar);
157620459ee2SKalle Valo 
157720459ee2SKalle Valo 	return ret;
157820459ee2SKalle Valo }
157920459ee2SKalle Valo 
15805fe4dffbSKalle Valo int ath6kl_init_hw_stop(struct ath6kl *ar)
15815fe4dffbSKalle Valo {
15825fe4dffbSKalle Valo 	int ret;
15835fe4dffbSKalle Valo 
15845fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
15855fe4dffbSKalle Valo 
15865fe4dffbSKalle Valo 	ath6kl_htc_stop(ar->htc_target);
15875fe4dffbSKalle Valo 
15885fe4dffbSKalle Valo 	ath6kl_hif_stop(ar);
15895fe4dffbSKalle Valo 
15905fe4dffbSKalle Valo 	ath6kl_bmi_reset(ar);
15915fe4dffbSKalle Valo 
15925fe4dffbSKalle Valo 	ret = ath6kl_hif_power_off(ar);
15935fe4dffbSKalle Valo 	if (ret)
15945fe4dffbSKalle Valo 		ath6kl_warn("failed to power off hif: %d\n", ret);
15955fe4dffbSKalle Valo 
159676a9fbe2SKalle Valo 	ar->state = ATH6KL_STATE_OFF;
159776a9fbe2SKalle Valo 
15985fe4dffbSKalle Valo 	return 0;
15995fe4dffbSKalle Valo }
16005fe4dffbSKalle Valo 
1601c25889e8SKalle Valo /* FIXME: move this to cfg80211.c and rename to ath6kl_cfg80211_vif_stop() */
160255055976SVasanthakumar Thiagarajan void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
16036db8fa53SVasanthakumar Thiagarajan {
16046db8fa53SVasanthakumar Thiagarajan 	static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
16056db8fa53SVasanthakumar Thiagarajan 	bool discon_issued;
16066db8fa53SVasanthakumar Thiagarajan 
16076db8fa53SVasanthakumar Thiagarajan 	netif_stop_queue(vif->ndev);
16086db8fa53SVasanthakumar Thiagarajan 
16096db8fa53SVasanthakumar Thiagarajan 	clear_bit(WLAN_ENABLED, &vif->flags);
16106db8fa53SVasanthakumar Thiagarajan 
16116db8fa53SVasanthakumar Thiagarajan 	if (wmi_ready) {
16126db8fa53SVasanthakumar Thiagarajan 		discon_issued = test_bit(CONNECTED, &vif->flags) ||
16136db8fa53SVasanthakumar Thiagarajan 				test_bit(CONNECT_PEND, &vif->flags);
16146db8fa53SVasanthakumar Thiagarajan 		ath6kl_disconnect(vif);
16156db8fa53SVasanthakumar Thiagarajan 		del_timer(&vif->disconnect_timer);
16166db8fa53SVasanthakumar Thiagarajan 
16176db8fa53SVasanthakumar Thiagarajan 		if (discon_issued)
16186db8fa53SVasanthakumar Thiagarajan 			ath6kl_disconnect_event(vif, DISCONNECT_CMD,
16196db8fa53SVasanthakumar Thiagarajan 						(vif->nw_type & AP_NETWORK) ?
16206db8fa53SVasanthakumar Thiagarajan 						bcast_mac : vif->bssid,
16216db8fa53SVasanthakumar Thiagarajan 						0, NULL, 0);
16226db8fa53SVasanthakumar Thiagarajan 	}
16236db8fa53SVasanthakumar Thiagarajan 
16246db8fa53SVasanthakumar Thiagarajan 	if (vif->scan_req) {
16256db8fa53SVasanthakumar Thiagarajan 		cfg80211_scan_done(vif->scan_req, true);
16266db8fa53SVasanthakumar Thiagarajan 		vif->scan_req = NULL;
16276db8fa53SVasanthakumar Thiagarajan 	}
16286db8fa53SVasanthakumar Thiagarajan }
16296db8fa53SVasanthakumar Thiagarajan 
1630bdcd8170SKalle Valo void ath6kl_stop_txrx(struct ath6kl *ar)
1631bdcd8170SKalle Valo {
1632990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif, *tmp_vif;
16331d2a4456SVasanthakumar Thiagarajan 	int i;
1634bdcd8170SKalle Valo 
1635bdcd8170SKalle Valo 	set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1636bdcd8170SKalle Valo 
1637bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
1638bdcd8170SKalle Valo 		ath6kl_err("down_interruptible failed\n");
1639bdcd8170SKalle Valo 		return;
1640bdcd8170SKalle Valo 	}
1641bdcd8170SKalle Valo 
16421d2a4456SVasanthakumar Thiagarajan 	for (i = 0; i < AP_MAX_NUM_STA; i++)
16431d2a4456SVasanthakumar Thiagarajan 		aggr_reset_state(ar->sta_list[i].aggr_conn);
16441d2a4456SVasanthakumar Thiagarajan 
164511f6e40dSVasanthakumar Thiagarajan 	spin_lock_bh(&ar->list_lock);
1646990bd915SVasanthakumar Thiagarajan 	list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1647990bd915SVasanthakumar Thiagarajan 		list_del(&vif->list);
164811f6e40dSVasanthakumar Thiagarajan 		spin_unlock_bh(&ar->list_lock);
1649990bd915SVasanthakumar Thiagarajan 		ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
165027929723SVasanthakumar Thiagarajan 		rtnl_lock();
1651c25889e8SKalle Valo 		ath6kl_cfg80211_vif_cleanup(vif);
165227929723SVasanthakumar Thiagarajan 		rtnl_unlock();
165311f6e40dSVasanthakumar Thiagarajan 		spin_lock_bh(&ar->list_lock);
1654990bd915SVasanthakumar Thiagarajan 	}
165511f6e40dSVasanthakumar Thiagarajan 	spin_unlock_bh(&ar->list_lock);
1656bdcd8170SKalle Valo 
16576db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_READY, &ar->flag);
16586db8fa53SVasanthakumar Thiagarajan 
16596db8fa53SVasanthakumar Thiagarajan 	/*
16606db8fa53SVasanthakumar Thiagarajan 	 * After wmi_shudown all WMI events will be dropped. We
16616db8fa53SVasanthakumar Thiagarajan 	 * need to cleanup the buffers allocated in AP mode and
16626db8fa53SVasanthakumar Thiagarajan 	 * give disconnect notification to stack, which usually
16636db8fa53SVasanthakumar Thiagarajan 	 * happens in the disconnect_event. Simulate the disconnect
16646db8fa53SVasanthakumar Thiagarajan 	 * event by calling the function directly. Sometimes
16656db8fa53SVasanthakumar Thiagarajan 	 * disconnect_event will be received when the debug logs
16666db8fa53SVasanthakumar Thiagarajan 	 * are collected.
16676db8fa53SVasanthakumar Thiagarajan 	 */
16686db8fa53SVasanthakumar Thiagarajan 	ath6kl_wmi_shutdown(ar->wmi);
16696db8fa53SVasanthakumar Thiagarajan 
16706db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_ENABLED, &ar->flag);
16716db8fa53SVasanthakumar Thiagarajan 	if (ar->htc_target) {
16726db8fa53SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
16736db8fa53SVasanthakumar Thiagarajan 		ath6kl_htc_stop(ar->htc_target);
1674bdcd8170SKalle Valo 	}
1675bdcd8170SKalle Valo 
1676bdcd8170SKalle Valo 	/*
16776db8fa53SVasanthakumar Thiagarajan 	 * Try to reset the device if we can. The driver may have been
16786db8fa53SVasanthakumar Thiagarajan 	 * configure NOT to reset the target during a debug session.
1679bdcd8170SKalle Valo 	 */
16806db8fa53SVasanthakumar Thiagarajan 	ath6kl_dbg(ATH6KL_DBG_TRC,
16816db8fa53SVasanthakumar Thiagarajan 		   "attempting to reset target on instance destroy\n");
16826db8fa53SVasanthakumar Thiagarajan 	ath6kl_reset_device(ar, ar->target_type, true, true);
1683bdcd8170SKalle Valo 
16846db8fa53SVasanthakumar Thiagarajan 	clear_bit(WLAN_ENABLED, &ar->flag);
1685e8ad9a06SVasanthakumar Thiagarajan 
1686e8ad9a06SVasanthakumar Thiagarajan 	up(&ar->sem);
1687bdcd8170SKalle Valo }
1688d6a434d6SKalle Valo EXPORT_SYMBOL(ath6kl_stop_txrx);
1689