ath6kl: cut power during suspend
[deliverable/linux.git] / drivers / net / wireless / ath / ath6kl / init.c
CommitLineData
bdcd8170
KV
1
2/*
3 * Copyright (c) 2011 Atheros Communications Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
c6efe578 18#include <linux/moduleparam.h>
f7830202 19#include <linux/errno.h>
92ecbff4 20#include <linux/of.h>
bdcd8170
KV
21#include <linux/mmc/sdio_func.h>
22#include "core.h"
23#include "cfg80211.h"
24#include "target.h"
25#include "debug.h"
26#include "hif-ops.h"
27
28unsigned int debug_mask;
003353b0 29static unsigned int testmode;
bdcd8170
KV
30
31module_param(debug_mask, uint, 0644);
003353b0 32module_param(testmode, uint, 0644);
bdcd8170
KV
33
34/*
35 * Include definitions here that can be used to tune the WLAN module
36 * behavior. Different customers can tune the behavior as per their needs,
37 * here.
38 */
39
40/*
41 * This configuration item enable/disable keepalive support.
42 * Keepalive support: In the absence of any data traffic to AP, null
43 * frames will be sent to the AP at periodic interval, to keep the association
44 * active. This configuration item defines the periodic interval.
45 * Use value of zero to disable keepalive support
46 * Default: 60 seconds
47 */
48#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
49
50/*
51 * This configuration item sets the value of disconnect timeout
52 * Firmware delays sending the disconnec event to the host for this
53 * timeout after is gets disconnected from the current AP.
54 * If the firmware successly roams within the disconnect timeout
55 * it sends a new connect event
56 */
57#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
58
59#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
60
bdcd8170
KV
61#define ATH6KL_DATA_OFFSET 64
62struct sk_buff *ath6kl_buf_alloc(int size)
63{
64 struct sk_buff *skb;
65 u16 reserved;
66
67 /* Add chacheline space at front and back of buffer */
68 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
1df94a85 69 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
bdcd8170
KV
70 skb = dev_alloc_skb(size + reserved);
71
72 if (skb)
73 skb_reserve(skb, reserved - L1_CACHE_BYTES);
74 return skb;
75}
76
e29f25f5 77void ath6kl_init_profile_info(struct ath6kl_vif *vif)
bdcd8170 78{
3450334f
VT
79 vif->ssid_len = 0;
80 memset(vif->ssid, 0, sizeof(vif->ssid));
81
82 vif->dot11_auth_mode = OPEN_AUTH;
83 vif->auth_mode = NONE_AUTH;
84 vif->prwise_crypto = NONE_CRYPT;
85 vif->prwise_crypto_len = 0;
86 vif->grp_crypto = NONE_CRYPT;
87 vif->grp_crypto_len = 0;
6f2a73f9 88 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
8c8b65e3
VT
89 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
90 memset(vif->bssid, 0, sizeof(vif->bssid));
f74bac54 91 vif->bss_ch = 0;
bdcd8170
KV
92}
93
bdcd8170
KV
94static int ath6kl_set_host_app_area(struct ath6kl *ar)
95{
96 u32 address, data;
97 struct host_app_area host_app_area;
98
99 /* Fetch the address of the host_app_area_s
100 * instance in the host interest area */
101 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
31024d99 102 address = TARG_VTOP(ar->target_type, address);
bdcd8170 103
addb44be 104 if (ath6kl_diag_read32(ar, address, &data))
bdcd8170
KV
105 return -EIO;
106
31024d99 107 address = TARG_VTOP(ar->target_type, data);
cbf49a6f 108 host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
addb44be
KV
109 if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
110 sizeof(struct host_app_area)))
bdcd8170
KV
111 return -EIO;
112
113 return 0;
114}
115
116static inline void set_ac2_ep_map(struct ath6kl *ar,
117 u8 ac,
118 enum htc_endpoint_id ep)
119{
120 ar->ac2ep_map[ac] = ep;
121 ar->ep2ac_map[ep] = ac;
122}
123
124/* connect to a service */
125static int ath6kl_connectservice(struct ath6kl *ar,
126 struct htc_service_connect_req *con_req,
127 char *desc)
128{
129 int status;
130 struct htc_service_connect_resp response;
131
132 memset(&response, 0, sizeof(response));
133
ad226ec2 134 status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
bdcd8170
KV
135 if (status) {
136 ath6kl_err("failed to connect to %s service status:%d\n",
137 desc, status);
138 return status;
139 }
140
141 switch (con_req->svc_id) {
142 case WMI_CONTROL_SVC:
143 if (test_bit(WMI_ENABLED, &ar->flag))
144 ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
145 ar->ctrl_ep = response.endpoint;
146 break;
147 case WMI_DATA_BE_SVC:
148 set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
149 break;
150 case WMI_DATA_BK_SVC:
151 set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
152 break;
153 case WMI_DATA_VI_SVC:
154 set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
155 break;
156 case WMI_DATA_VO_SVC:
157 set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
158 break;
159 default:
160 ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
161 return -EINVAL;
162 }
163
164 return 0;
165}
166
167static int ath6kl_init_service_ep(struct ath6kl *ar)
168{
169 struct htc_service_connect_req connect;
170
171 memset(&connect, 0, sizeof(connect));
172
173 /* these fields are the same for all service endpoints */
174 connect.ep_cb.rx = ath6kl_rx;
175 connect.ep_cb.rx_refill = ath6kl_rx_refill;
176 connect.ep_cb.tx_full = ath6kl_tx_queue_full;
177
178 /*
179 * Set the max queue depth so that our ath6kl_tx_queue_full handler
180 * gets called.
181 */
182 connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
183 connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
184 if (!connect.ep_cb.rx_refill_thresh)
185 connect.ep_cb.rx_refill_thresh++;
186
187 /* connect to control service */
188 connect.svc_id = WMI_CONTROL_SVC;
189 if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
190 return -EIO;
191
192 connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
193
194 /*
195 * Limit the HTC message size on the send path, although e can
196 * receive A-MSDU frames of 4K, we will only send ethernet-sized
197 * (802.3) frames on the send path.
198 */
199 connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
200
201 /*
202 * To reduce the amount of committed memory for larger A_MSDU
203 * frames, use the recv-alloc threshold mechanism for larger
204 * packets.
205 */
206 connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
207 connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
208
209 /*
210 * For the remaining data services set the connection flag to
211 * reduce dribbling, if configured to do so.
212 */
213 connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
214 connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
215 connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
216
217 connect.svc_id = WMI_DATA_BE_SVC;
218
219 if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
220 return -EIO;
221
222 /* connect to back-ground map this to WMI LOW_PRI */
223 connect.svc_id = WMI_DATA_BK_SVC;
224 if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
225 return -EIO;
226
227 /* connect to Video service, map this to to HI PRI */
228 connect.svc_id = WMI_DATA_VI_SVC;
229 if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
230 return -EIO;
231
232 /*
233 * Connect to VO service, this is currently not mapped to a WMI
234 * priority stream due to historical reasons. WMI originally
235 * defined 3 priorities over 3 mailboxes We can change this when
236 * WMI is reworked so that priorities are not dependent on
237 * mailboxes.
238 */
239 connect.svc_id = WMI_DATA_VO_SVC;
240 if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
241 return -EIO;
242
243 return 0;
244}
245
e29f25f5 246void ath6kl_init_control_info(struct ath6kl_vif *vif)
bdcd8170 247{
e29f25f5 248 ath6kl_init_profile_info(vif);
3450334f 249 vif->def_txkey_index = 0;
6f2a73f9 250 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
f74bac54 251 vif->ch_hint = 0;
bdcd8170
KV
252}
253
254/*
255 * Set HTC/Mbox operational parameters, this can only be called when the
256 * target is in the BMI phase.
257 */
258static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
259 u8 htc_ctrl_buf)
260{
261 int status;
262 u32 blk_size;
263
264 blk_size = ar->mbox_info.block_size;
265
266 if (htc_ctrl_buf)
267 blk_size |= ((u32)htc_ctrl_buf) << 16;
268
269 /* set the host interest area for the block size */
270 status = ath6kl_bmi_write(ar,
271 ath6kl_get_hi_item_addr(ar,
272 HI_ITEM(hi_mbox_io_block_sz)),
273 (u8 *)&blk_size,
274 4);
275 if (status) {
276 ath6kl_err("bmi_write_memory for IO block size failed\n");
277 goto out;
278 }
279
280 ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
281 blk_size,
282 ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
283
284 if (mbox_isr_yield_val) {
285 /* set the host interest area for the mbox ISR yield limit */
286 status = ath6kl_bmi_write(ar,
287 ath6kl_get_hi_item_addr(ar,
288 HI_ITEM(hi_mbox_isr_yield_limit)),
289 (u8 *)&mbox_isr_yield_val,
290 4);
291 if (status) {
292 ath6kl_err("bmi_write_memory for yield limit failed\n");
293 goto out;
294 }
295 }
296
297out:
298 return status;
299}
300
0ce59445 301static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
bdcd8170
KV
302{
303 int status = 0;
4dea08e0 304 int ret;
bdcd8170
KV
305
306 /*
307 * Configure the device for rx dot11 header rules. "0,0" are the
308 * default values. Required if checksum offload is needed. Set
309 * RxMetaVersion to 2.
310 */
0ce59445 311 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
bdcd8170
KV
312 ar->rx_meta_ver, 0, 0)) {
313 ath6kl_err("unable to set the rx frame format\n");
314 status = -EIO;
315 }
316
317 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
0ce59445 318 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
bdcd8170
KV
319 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
320 ath6kl_err("unable to set power save fail event policy\n");
321 status = -EIO;
322 }
323
324 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
0ce59445 325 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
bdcd8170
KV
326 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
327 ath6kl_err("unable to set barker preamble policy\n");
328 status = -EIO;
329 }
330
0ce59445 331 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
bdcd8170
KV
332 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
333 ath6kl_err("unable to set keep alive interval\n");
334 status = -EIO;
335 }
336
0ce59445 337 if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
bdcd8170
KV
338 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
339 ath6kl_err("unable to set disconnect timeout\n");
340 status = -EIO;
341 }
342
343 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
0ce59445 344 if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
bdcd8170
KV
345 ath6kl_err("unable to set txop bursting\n");
346 status = -EIO;
347 }
348
0ce59445
VT
349 /*
350 * FIXME: Make sure p2p configurations are not applied to
351 * non-p2p capable interfaces when multivif support is enabled.
352 */
6bbc7c35 353 if (ar->p2p) {
0ce59445 354 ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
6bbc7c35
JM
355 P2P_FLAG_CAPABILITIES_REQ |
356 P2P_FLAG_MACADDR_REQ |
357 P2P_FLAG_HMODEL_REQ);
358 if (ret) {
359 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
360 "capabilities (%d) - assuming P2P not "
361 "supported\n", ret);
362 ar->p2p = 0;
363 }
364 }
365
0ce59445
VT
366 /*
367 * FIXME: Make sure p2p configurations are not applied to
368 * non-p2p capable interfaces when multivif support is enabled.
369 */
6bbc7c35
JM
370 if (ar->p2p) {
371 /* Enable Probe Request reporting for P2P */
0ce59445 372 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
6bbc7c35
JM
373 if (ret) {
374 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
375 "Request reporting (%d)\n", ret);
376 }
4dea08e0
JM
377 }
378
bdcd8170
KV
379 return status;
380}
381
382int ath6kl_configure_target(struct ath6kl *ar)
383{
384 u32 param, ram_reserved_size;
3226f68a 385 u8 fw_iftype, fw_mode = 0, fw_submode = 0;
7b85832d 386 int i;
bdcd8170 387
7b85832d
VT
388 /*
389 * Note: Even though the firmware interface type is
390 * chosen as BSS_STA for all three interfaces, can
391 * be configured to IBSS/AP as long as the fw submode
392 * remains normal mode (0 - AP, STA and IBSS). But
393 * due to an target assert in firmware only one interface is
394 * configured for now.
395 */
dd3751f7 396 fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
bdcd8170 397
7b85832d
VT
398 for (i = 0; i < MAX_NUM_VIF; i++)
399 fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
400
401 /*
3226f68a
VT
402 * By default, submodes :
403 * vif[0] - AP/STA/IBSS
404 * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
405 * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
7b85832d 406 */
3226f68a
VT
407
408 for (i = 0; i < ar->max_norm_iface; i++)
409 fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
410 (i * HI_OPTION_FW_SUBMODE_BITS);
411
412 for (i = ar->max_norm_iface; i < MAX_NUM_VIF; i++)
413 fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
414 (i * HI_OPTION_FW_SUBMODE_BITS);
7b85832d
VT
415
416 /*
417 * FIXME: This needs to be removed once the multivif
418 * support is enabled.
419 */
420 if (ar->p2p)
421 fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
7b85832d 422
bdcd8170
KV
423 param = HTC_PROTOCOL_VERSION;
424 if (ath6kl_bmi_write(ar,
425 ath6kl_get_hi_item_addr(ar,
426 HI_ITEM(hi_app_host_interest)),
427 (u8 *)&param, 4) != 0) {
428 ath6kl_err("bmi_write_memory for htc version failed\n");
429 return -EIO;
430 }
431
432 /* set the firmware mode to STA/IBSS/AP */
433 param = 0;
434
435 if (ath6kl_bmi_read(ar,
436 ath6kl_get_hi_item_addr(ar,
437 HI_ITEM(hi_option_flag)),
438 (u8 *)&param, 4) != 0) {
439 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
440 return -EIO;
441 }
442
7b85832d
VT
443 param |= (MAX_NUM_VIF << HI_OPTION_NUM_DEV_SHIFT);
444 param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
445 param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
446
bdcd8170
KV
447 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
448 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
449
450 if (ath6kl_bmi_write(ar,
451 ath6kl_get_hi_item_addr(ar,
452 HI_ITEM(hi_option_flag)),
453 (u8 *)&param,
454 4) != 0) {
455 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
456 return -EIO;
457 }
458
459 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
460
461 /*
462 * Hardcode the address use for the extended board data
463 * Ideally this should be pre-allocate by the OS at boot time
464 * But since it is a new feature and board data is loaded
465 * at init time, we have to workaround this from host.
466 * It is difficult to patch the firmware boot code,
467 * but possible in theory.
468 */
469
991b27ea
KV
470 param = ar->hw.board_ext_data_addr;
471 ram_reserved_size = ar->hw.reserved_ram_size;
bdcd8170 472
991b27ea
KV
473 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
474 HI_ITEM(hi_board_ext_data)),
475 (u8 *)&param, 4) != 0) {
476 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
477 return -EIO;
478 }
479
480 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
481 HI_ITEM(hi_end_ram_reserve_sz)),
482 (u8 *)&ram_reserved_size, 4) != 0) {
483 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
484 return -EIO;
bdcd8170
KV
485 }
486
487 /* set the block size for the target */
488 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
489 /* use default number of control buffers */
490 return -EIO;
491
492 return 0;
493}
494
8dafb70e 495void ath6kl_core_free(struct ath6kl *ar)
bdcd8170 496{
8dafb70e 497 wiphy_free(ar->wiphy);
bdcd8170
KV
498}
499
6db8fa53 500void ath6kl_core_cleanup(struct ath6kl *ar)
bdcd8170 501{
b2e75698
KV
502 ath6kl_hif_power_off(ar);
503
6db8fa53 504 destroy_workqueue(ar->ath6kl_wq);
bdcd8170 505
6db8fa53
VT
506 if (ar->htc_target)
507 ath6kl_htc_cleanup(ar->htc_target);
508
509 ath6kl_cookie_cleanup(ar);
510
511 ath6kl_cleanup_amsdu_rxbufs(ar);
512
513 ath6kl_bmi_cleanup(ar);
514
515 ath6kl_debug_cleanup(ar);
516
517 kfree(ar->fw_board);
518 kfree(ar->fw_otp);
519 kfree(ar->fw);
520 kfree(ar->fw_patch);
521
522 ath6kl_deinit_ieee80211_hw(ar);
bdcd8170
KV
523}
524
525/* firmware upload */
bdcd8170
KV
526static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
527 u8 **fw, size_t *fw_len)
528{
529 const struct firmware *fw_entry;
530 int ret;
531
532 ret = request_firmware(&fw_entry, filename, ar->dev);
533 if (ret)
534 return ret;
535
536 *fw_len = fw_entry->size;
537 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
538
539 if (*fw == NULL)
540 ret = -ENOMEM;
541
542 release_firmware(fw_entry);
543
544 return ret;
545}
546
92ecbff4
SL
547#ifdef CONFIG_OF
548static const char *get_target_ver_dir(const struct ath6kl *ar)
549{
550 switch (ar->version.target_ver) {
551 case AR6003_REV1_VERSION:
552 return "ath6k/AR6003/hw1.0";
553 case AR6003_REV2_VERSION:
554 return "ath6k/AR6003/hw2.0";
555 case AR6003_REV3_VERSION:
556 return "ath6k/AR6003/hw2.1.1";
557 }
558 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
559 ar->version.target_ver);
560 return NULL;
561}
562
563/*
564 * Check the device tree for a board-id and use it to construct
565 * the pathname to the firmware file. Used (for now) to find a
566 * fallback to the "bdata.bin" file--typically a symlink to the
567 * appropriate board-specific file.
568 */
569static bool check_device_tree(struct ath6kl *ar)
570{
571 static const char *board_id_prop = "atheros,board-id";
572 struct device_node *node;
573 char board_filename[64];
574 const char *board_id;
575 int ret;
576
577 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
578 board_id = of_get_property(node, board_id_prop, NULL);
579 if (board_id == NULL) {
580 ath6kl_warn("No \"%s\" property on %s node.\n",
581 board_id_prop, node->name);
582 continue;
583 }
584 snprintf(board_filename, sizeof(board_filename),
585 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
586
587 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
588 &ar->fw_board_len);
589 if (ret) {
590 ath6kl_err("Failed to get DT board file %s: %d\n",
591 board_filename, ret);
592 continue;
593 }
594 return true;
595 }
596 return false;
597}
598#else
599static bool check_device_tree(struct ath6kl *ar)
600{
601 return false;
602}
603#endif /* CONFIG_OF */
604
bdcd8170
KV
605static int ath6kl_fetch_board_file(struct ath6kl *ar)
606{
607 const char *filename;
608 int ret;
609
772c31ee
KV
610 if (ar->fw_board != NULL)
611 return 0;
612
bdcd8170
KV
613 switch (ar->version.target_ver) {
614 case AR6003_REV2_VERSION:
615 filename = AR6003_REV2_BOARD_DATA_FILE;
616 break;
31024d99
KF
617 case AR6004_REV1_VERSION:
618 filename = AR6004_REV1_BOARD_DATA_FILE;
619 break;
bdcd8170
KV
620 default:
621 filename = AR6003_REV3_BOARD_DATA_FILE;
622 break;
623 }
624
625 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
626 &ar->fw_board_len);
627 if (ret == 0) {
628 /* managed to get proper board file */
629 return 0;
630 }
631
92ecbff4
SL
632 if (check_device_tree(ar)) {
633 /* got board file from device tree */
634 return 0;
635 }
636
bdcd8170
KV
637 /* there was no proper board file, try to use default instead */
638 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
639 filename, ret);
640
641 switch (ar->version.target_ver) {
642 case AR6003_REV2_VERSION:
643 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
644 break;
31024d99
KF
645 case AR6004_REV1_VERSION:
646 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
647 break;
bdcd8170
KV
648 default:
649 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
650 break;
651 }
652
653 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
654 &ar->fw_board_len);
655 if (ret) {
656 ath6kl_err("Failed to get default board file %s: %d\n",
657 filename, ret);
658 return ret;
659 }
660
661 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
662 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
663
664 return 0;
665}
666
772c31ee
KV
667static int ath6kl_fetch_otp_file(struct ath6kl *ar)
668{
669 const char *filename;
670 int ret;
671
672 if (ar->fw_otp != NULL)
673 return 0;
674
675 switch (ar->version.target_ver) {
676 case AR6003_REV2_VERSION:
677 filename = AR6003_REV2_OTP_FILE;
678 break;
679 case AR6004_REV1_VERSION:
680 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
681 return 0;
682 break;
683 default:
684 filename = AR6003_REV3_OTP_FILE;
685 break;
686 }
687
688 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
689 &ar->fw_otp_len);
690 if (ret) {
691 ath6kl_err("Failed to get OTP file %s: %d\n",
692 filename, ret);
693 return ret;
694 }
695
696 return 0;
697}
698
699static int ath6kl_fetch_fw_file(struct ath6kl *ar)
700{
701 const char *filename;
702 int ret;
703
704 if (ar->fw != NULL)
705 return 0;
706
707 if (testmode) {
708 switch (ar->version.target_ver) {
709 case AR6003_REV2_VERSION:
710 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
711 break;
712 case AR6003_REV3_VERSION:
713 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
714 break;
715 case AR6004_REV1_VERSION:
716 ath6kl_warn("testmode not supported with ar6004\n");
717 return -EOPNOTSUPP;
718 default:
719 ath6kl_warn("unknown target version: 0x%x\n",
720 ar->version.target_ver);
721 return -EINVAL;
722 }
723
724 set_bit(TESTMODE, &ar->flag);
725
726 goto get_fw;
727 }
728
729 switch (ar->version.target_ver) {
730 case AR6003_REV2_VERSION:
731 filename = AR6003_REV2_FIRMWARE_FILE;
732 break;
733 case AR6004_REV1_VERSION:
734 filename = AR6004_REV1_FIRMWARE_FILE;
735 break;
736 default:
737 filename = AR6003_REV3_FIRMWARE_FILE;
738 break;
739 }
740
741get_fw:
742 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
743 if (ret) {
744 ath6kl_err("Failed to get firmware file %s: %d\n",
745 filename, ret);
746 return ret;
747 }
748
749 return 0;
750}
751
752static int ath6kl_fetch_patch_file(struct ath6kl *ar)
753{
754 const char *filename;
755 int ret;
756
757 switch (ar->version.target_ver) {
758 case AR6003_REV2_VERSION:
759 filename = AR6003_REV2_PATCH_FILE;
760 break;
761 case AR6004_REV1_VERSION:
762 /* FIXME: implement for AR6004 */
763 return 0;
764 break;
765 default:
766 filename = AR6003_REV3_PATCH_FILE;
767 break;
768 }
769
770 if (ar->fw_patch == NULL) {
771 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
772 &ar->fw_patch_len);
773 if (ret) {
774 ath6kl_err("Failed to get patch file %s: %d\n",
775 filename, ret);
776 return ret;
777 }
778 }
779
780 return 0;
781}
782
50d41234 783static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
772c31ee
KV
784{
785 int ret;
786
772c31ee
KV
787 ret = ath6kl_fetch_otp_file(ar);
788 if (ret)
789 return ret;
790
791 ret = ath6kl_fetch_fw_file(ar);
792 if (ret)
793 return ret;
794
795 ret = ath6kl_fetch_patch_file(ar);
796 if (ret)
797 return ret;
798
799 return 0;
800}
bdcd8170 801
50d41234
KV
802static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
803{
804 size_t magic_len, len, ie_len;
805 const struct firmware *fw;
806 struct ath6kl_fw_ie *hdr;
807 const char *filename;
808 const u8 *data;
97e0496d 809 int ret, ie_id, i, index, bit;
8a137480 810 __le32 *val;
50d41234
KV
811
812 switch (ar->version.target_ver) {
813 case AR6003_REV2_VERSION:
814 filename = AR6003_REV2_FIRMWARE_2_FILE;
815 break;
816 case AR6003_REV3_VERSION:
817 filename = AR6003_REV3_FIRMWARE_2_FILE;
818 break;
819 case AR6004_REV1_VERSION:
820 filename = AR6004_REV1_FIRMWARE_2_FILE;
821 break;
822 default:
823 return -EOPNOTSUPP;
824 }
825
826 ret = request_firmware(&fw, filename, ar->dev);
827 if (ret)
828 return ret;
829
830 data = fw->data;
831 len = fw->size;
832
833 /* magic also includes the null byte, check that as well */
834 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
835
836 if (len < magic_len) {
837 ret = -EINVAL;
838 goto out;
839 }
840
841 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
842 ret = -EINVAL;
843 goto out;
844 }
845
846 len -= magic_len;
847 data += magic_len;
848
849 /* loop elements */
850 while (len > sizeof(struct ath6kl_fw_ie)) {
851 /* hdr is unaligned! */
852 hdr = (struct ath6kl_fw_ie *) data;
853
854 ie_id = le32_to_cpup(&hdr->id);
855 ie_len = le32_to_cpup(&hdr->len);
856
857 len -= sizeof(*hdr);
858 data += sizeof(*hdr);
859
860 if (len < ie_len) {
861 ret = -EINVAL;
862 goto out;
863 }
864
865 switch (ie_id) {
866 case ATH6KL_FW_IE_OTP_IMAGE:
ef548626 867 ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
6bc36431
KV
868 ie_len);
869
50d41234
KV
870 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
871
872 if (ar->fw_otp == NULL) {
873 ret = -ENOMEM;
874 goto out;
875 }
876
877 ar->fw_otp_len = ie_len;
878 break;
879 case ATH6KL_FW_IE_FW_IMAGE:
ef548626 880 ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
6bc36431
KV
881 ie_len);
882
50d41234
KV
883 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
884
885 if (ar->fw == NULL) {
886 ret = -ENOMEM;
887 goto out;
888 }
889
890 ar->fw_len = ie_len;
891 break;
892 case ATH6KL_FW_IE_PATCH_IMAGE:
ef548626 893 ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
6bc36431
KV
894 ie_len);
895
50d41234
KV
896 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
897
898 if (ar->fw_patch == NULL) {
899 ret = -ENOMEM;
900 goto out;
901 }
902
903 ar->fw_patch_len = ie_len;
904 break;
8a137480
KV
905 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
906 val = (__le32 *) data;
907 ar->hw.reserved_ram_size = le32_to_cpup(val);
6bc36431
KV
908
909 ath6kl_dbg(ATH6KL_DBG_BOOT,
910 "found reserved ram size ie 0x%d\n",
911 ar->hw.reserved_ram_size);
8a137480 912 break;
97e0496d 913 case ATH6KL_FW_IE_CAPABILITIES:
6bc36431 914 ath6kl_dbg(ATH6KL_DBG_BOOT,
ef548626 915 "found firmware capabilities ie (%zd B)\n",
6bc36431
KV
916 ie_len);
917
97e0496d
KV
918 for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
919 index = ALIGN(i, 8) / 8;
920 bit = i % 8;
921
922 if (data[index] & (1 << bit))
923 __set_bit(i, ar->fw_capabilities);
924 }
6bc36431
KV
925
926 ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
927 ar->fw_capabilities,
928 sizeof(ar->fw_capabilities));
97e0496d 929 break;
1b4304da
KV
930 case ATH6KL_FW_IE_PATCH_ADDR:
931 if (ie_len != sizeof(*val))
932 break;
933
934 val = (__le32 *) data;
935 ar->hw.dataset_patch_addr = le32_to_cpup(val);
6bc36431
KV
936
937 ath6kl_dbg(ATH6KL_DBG_BOOT,
938 "found patch address ie 0x%d\n",
939 ar->hw.dataset_patch_addr);
1b4304da 940 break;
50d41234 941 default:
6bc36431 942 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
50d41234
KV
943 le32_to_cpup(&hdr->id));
944 break;
945 }
946
947 len -= ie_len;
948 data += ie_len;
949 };
950
951 ret = 0;
952out:
953 release_firmware(fw);
954
955 return ret;
956}
957
958static int ath6kl_fetch_firmwares(struct ath6kl *ar)
959{
960 int ret;
961
962 ret = ath6kl_fetch_board_file(ar);
963 if (ret)
964 return ret;
965
966 ret = ath6kl_fetch_fw_api2(ar);
6bc36431
KV
967 if (ret == 0) {
968 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
50d41234 969 return 0;
6bc36431 970 }
50d41234
KV
971
972 ret = ath6kl_fetch_fw_api1(ar);
973 if (ret)
974 return ret;
975
6bc36431
KV
976 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
977
50d41234
KV
978 return 0;
979}
980
bdcd8170
KV
981static int ath6kl_upload_board_file(struct ath6kl *ar)
982{
983 u32 board_address, board_ext_address, param;
31024d99 984 u32 board_data_size, board_ext_data_size;
bdcd8170
KV
985 int ret;
986
772c31ee
KV
987 if (WARN_ON(ar->fw_board == NULL))
988 return -ENOENT;
bdcd8170 989
31024d99
KF
990 /*
991 * Determine where in Target RAM to write Board Data.
992 * For AR6004, host determine Target RAM address for
993 * writing board data.
994 */
995 if (ar->target_type == TARGET_TYPE_AR6004) {
996 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
997 ath6kl_bmi_write(ar,
998 ath6kl_get_hi_item_addr(ar,
999 HI_ITEM(hi_board_data)),
1000 (u8 *) &board_address, 4);
1001 } else {
1002 ath6kl_bmi_read(ar,
1003 ath6kl_get_hi_item_addr(ar,
1004 HI_ITEM(hi_board_data)),
1005 (u8 *) &board_address, 4);
1006 }
1007
bdcd8170
KV
1008 /* determine where in target ram to write extended board data */
1009 ath6kl_bmi_read(ar,
1010 ath6kl_get_hi_item_addr(ar,
1011 HI_ITEM(hi_board_ext_data)),
1012 (u8 *) &board_ext_address, 4);
1013
bdcd8170
KV
1014 if (board_ext_address == 0) {
1015 ath6kl_err("Failed to get board file target address.\n");
1016 return -EINVAL;
1017 }
1018
31024d99
KF
1019 switch (ar->target_type) {
1020 case TARGET_TYPE_AR6003:
1021 board_data_size = AR6003_BOARD_DATA_SZ;
1022 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1023 break;
1024 case TARGET_TYPE_AR6004:
1025 board_data_size = AR6004_BOARD_DATA_SZ;
1026 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1027 break;
1028 default:
1029 WARN_ON(1);
1030 return -EINVAL;
1031 break;
1032 }
1033
1034 if (ar->fw_board_len == (board_data_size +
1035 board_ext_data_size)) {
1036
bdcd8170 1037 /* write extended board data */
6bc36431
KV
1038 ath6kl_dbg(ATH6KL_DBG_BOOT,
1039 "writing extended board data to 0x%x (%d B)\n",
1040 board_ext_address, board_ext_data_size);
1041
bdcd8170 1042 ret = ath6kl_bmi_write(ar, board_ext_address,
31024d99
KF
1043 ar->fw_board + board_data_size,
1044 board_ext_data_size);
bdcd8170
KV
1045 if (ret) {
1046 ath6kl_err("Failed to write extended board data: %d\n",
1047 ret);
1048 return ret;
1049 }
1050
1051 /* record that extended board data is initialized */
31024d99
KF
1052 param = (board_ext_data_size << 16) | 1;
1053
bdcd8170
KV
1054 ath6kl_bmi_write(ar,
1055 ath6kl_get_hi_item_addr(ar,
1056 HI_ITEM(hi_board_ext_data_config)),
1057 (unsigned char *) &param, 4);
1058 }
1059
31024d99 1060 if (ar->fw_board_len < board_data_size) {
bdcd8170
KV
1061 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1062 ret = -EINVAL;
1063 return ret;
1064 }
1065
6bc36431
KV
1066 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1067 board_address, board_data_size);
1068
bdcd8170 1069 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
31024d99 1070 board_data_size);
bdcd8170
KV
1071
1072 if (ret) {
1073 ath6kl_err("Board file bmi write failed: %d\n", ret);
1074 return ret;
1075 }
1076
1077 /* record the fact that Board Data IS initialized */
1078 param = 1;
1079 ath6kl_bmi_write(ar,
1080 ath6kl_get_hi_item_addr(ar,
1081 HI_ITEM(hi_board_data_initialized)),
1082 (u8 *)&param, 4);
1083
1084 return ret;
1085}
1086
1087static int ath6kl_upload_otp(struct ath6kl *ar)
1088{
bdcd8170 1089 u32 address, param;
bef26a7f 1090 bool from_hw = false;
bdcd8170
KV
1091 int ret;
1092
772c31ee
KV
1093 if (WARN_ON(ar->fw_otp == NULL))
1094 return -ENOENT;
bdcd8170 1095
a01ac414 1096 address = ar->hw.app_load_addr;
bdcd8170 1097
ef548626 1098 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
6bc36431
KV
1099 ar->fw_otp_len);
1100
bdcd8170
KV
1101 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1102 ar->fw_otp_len);
1103 if (ret) {
1104 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1105 return ret;
1106 }
1107
639d0b89
KV
1108 /* read firmware start address */
1109 ret = ath6kl_bmi_read(ar,
1110 ath6kl_get_hi_item_addr(ar,
1111 HI_ITEM(hi_app_start)),
1112 (u8 *) &address, sizeof(address));
1113
1114 if (ret) {
1115 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1116 return ret;
1117 }
1118
bef26a7f
KV
1119 if (ar->hw.app_start_override_addr == 0) {
1120 ar->hw.app_start_override_addr = address;
1121 from_hw = true;
1122 }
639d0b89 1123
bef26a7f
KV
1124 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1125 from_hw ? " (from hw)" : "",
6bc36431
KV
1126 ar->hw.app_start_override_addr);
1127
bdcd8170 1128 /* execute the OTP code */
bef26a7f
KV
1129 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1130 ar->hw.app_start_override_addr);
bdcd8170 1131 param = 0;
bef26a7f 1132 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
bdcd8170
KV
1133
1134 return ret;
1135}
1136
1137static int ath6kl_upload_firmware(struct ath6kl *ar)
1138{
bdcd8170
KV
1139 u32 address;
1140 int ret;
1141
772c31ee
KV
1142 if (WARN_ON(ar->fw == NULL))
1143 return -ENOENT;
bdcd8170 1144
a01ac414 1145 address = ar->hw.app_load_addr;
bdcd8170 1146
ef548626 1147 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
6bc36431
KV
1148 address, ar->fw_len);
1149
bdcd8170
KV
1150 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1151
1152 if (ret) {
1153 ath6kl_err("Failed to write firmware: %d\n", ret);
1154 return ret;
1155 }
1156
31024d99
KF
1157 /*
1158 * Set starting address for firmware
1159 * Don't need to setup app_start override addr on AR6004
1160 */
1161 if (ar->target_type != TARGET_TYPE_AR6004) {
a01ac414 1162 address = ar->hw.app_start_override_addr;
31024d99
KF
1163 ath6kl_bmi_set_app_start(ar, address);
1164 }
bdcd8170
KV
1165 return ret;
1166}
1167
1168static int ath6kl_upload_patch(struct ath6kl *ar)
1169{
bdcd8170
KV
1170 u32 address, param;
1171 int ret;
1172
772c31ee
KV
1173 if (WARN_ON(ar->fw_patch == NULL))
1174 return -ENOENT;
bdcd8170 1175
a01ac414 1176 address = ar->hw.dataset_patch_addr;
bdcd8170 1177
ef548626 1178 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
6bc36431
KV
1179 address, ar->fw_patch_len);
1180
bdcd8170
KV
1181 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1182 if (ret) {
1183 ath6kl_err("Failed to write patch file: %d\n", ret);
1184 return ret;
1185 }
1186
1187 param = address;
1188 ath6kl_bmi_write(ar,
1189 ath6kl_get_hi_item_addr(ar,
1190 HI_ITEM(hi_dset_list_head)),
1191 (unsigned char *) &param, 4);
1192
1193 return 0;
1194}
1195
1196static int ath6kl_init_upload(struct ath6kl *ar)
1197{
1198 u32 param, options, sleep, address;
1199 int status = 0;
1200
31024d99
KF
1201 if (ar->target_type != TARGET_TYPE_AR6003 &&
1202 ar->target_type != TARGET_TYPE_AR6004)
bdcd8170
KV
1203 return -EINVAL;
1204
1205 /* temporarily disable system sleep */
1206 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1207 status = ath6kl_bmi_reg_read(ar, address, &param);
1208 if (status)
1209 return status;
1210
1211 options = param;
1212
1213 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1214 status = ath6kl_bmi_reg_write(ar, address, param);
1215 if (status)
1216 return status;
1217
1218 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1219 status = ath6kl_bmi_reg_read(ar, address, &param);
1220 if (status)
1221 return status;
1222
1223 sleep = param;
1224
1225 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1226 status = ath6kl_bmi_reg_write(ar, address, param);
1227 if (status)
1228 return status;
1229
1230 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1231 options, sleep);
1232
1233 /* program analog PLL register */
31024d99
KF
1234 /* no need to control 40/44MHz clock on AR6004 */
1235 if (ar->target_type != TARGET_TYPE_AR6004) {
1236 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1237 0xF9104001);
bdcd8170 1238
31024d99
KF
1239 if (status)
1240 return status;
bdcd8170 1241
31024d99
KF
1242 /* Run at 80/88MHz by default */
1243 param = SM(CPU_CLOCK_STANDARD, 1);
1244
1245 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1246 status = ath6kl_bmi_reg_write(ar, address, param);
1247 if (status)
1248 return status;
1249 }
bdcd8170
KV
1250
1251 param = 0;
1252 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1253 param = SM(LPO_CAL_ENABLE, 1);
1254 status = ath6kl_bmi_reg_write(ar, address, param);
1255 if (status)
1256 return status;
1257
1258 /* WAR to avoid SDIO CRC err */
1259 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1260 ath6kl_err("temporary war to avoid sdio crc error\n");
1261
1262 param = 0x20;
1263
1264 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1265 status = ath6kl_bmi_reg_write(ar, address, param);
1266 if (status)
1267 return status;
1268
1269 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1270 status = ath6kl_bmi_reg_write(ar, address, param);
1271 if (status)
1272 return status;
1273
1274 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1275 status = ath6kl_bmi_reg_write(ar, address, param);
1276 if (status)
1277 return status;
1278
1279 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1280 status = ath6kl_bmi_reg_write(ar, address, param);
1281 if (status)
1282 return status;
1283 }
1284
1285 /* write EEPROM data to Target RAM */
1286 status = ath6kl_upload_board_file(ar);
1287 if (status)
1288 return status;
1289
1290 /* transfer One time Programmable data */
1291 status = ath6kl_upload_otp(ar);
1292 if (status)
1293 return status;
1294
1295 /* Download Target firmware */
1296 status = ath6kl_upload_firmware(ar);
1297 if (status)
1298 return status;
1299
1300 status = ath6kl_upload_patch(ar);
1301 if (status)
1302 return status;
1303
1304 /* Restore system sleep */
1305 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1306 status = ath6kl_bmi_reg_write(ar, address, sleep);
1307 if (status)
1308 return status;
1309
1310 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1311 param = options | 0x20;
1312 status = ath6kl_bmi_reg_write(ar, address, param);
1313 if (status)
1314 return status;
1315
1316 /* Configure GPIO AR6003 UART */
1317 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1318 status = ath6kl_bmi_write(ar,
1319 ath6kl_get_hi_item_addr(ar,
1320 HI_ITEM(hi_dbg_uart_txpin)),
1321 (u8 *)&param, 4);
1322
1323 return status;
1324}
1325
a01ac414
KV
1326static int ath6kl_init_hw_params(struct ath6kl *ar)
1327{
1328 switch (ar->version.target_ver) {
1329 case AR6003_REV2_VERSION:
1330 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1331 ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
991b27ea
KV
1332 ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
1333 ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
bef26a7f
KV
1334
1335 /* hw2.0 needs override address hardcoded */
1336 ar->hw.app_start_override_addr = 0x944C00;
1337
a01ac414
KV
1338 break;
1339 case AR6003_REV3_VERSION:
1340 ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
1341 ar->hw.app_load_addr = 0x1234;
991b27ea
KV
1342 ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
1343 ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
a01ac414
KV
1344 break;
1345 case AR6004_REV1_VERSION:
1346 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1347 ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
991b27ea
KV
1348 ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
1349 ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
a01ac414
KV
1350 break;
1351 default:
1352 ath6kl_err("Unsupported hardware version: 0x%x\n",
1353 ar->version.target_ver);
1354 return -EINVAL;
1355 }
1356
6bc36431
KV
1357 ath6kl_dbg(ATH6KL_DBG_BOOT,
1358 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1359 ar->version.target_ver, ar->target_type,
1360 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1361 ath6kl_dbg(ATH6KL_DBG_BOOT,
1362 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1363 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1364 ar->hw.reserved_ram_size);
1365
a01ac414
KV
1366 return 0;
1367}
1368
5fe4dffb 1369int ath6kl_init_hw_start(struct ath6kl *ar)
20459ee2
KV
1370{
1371 long timeleft;
1372 int ret, i;
1373
5fe4dffb
KV
1374 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
1375
20459ee2
KV
1376 ret = ath6kl_hif_power_on(ar);
1377 if (ret)
1378 return ret;
1379
1380 ret = ath6kl_configure_target(ar);
1381 if (ret)
1382 goto err_power_off;
1383
1384 ret = ath6kl_init_upload(ar);
1385 if (ret)
1386 goto err_power_off;
1387
1388 /* Do we need to finish the BMI phase */
1389 /* FIXME: return error from ath6kl_bmi_done() */
1390 if (ath6kl_bmi_done(ar)) {
1391 ret = -EIO;
1392 goto err_power_off;
1393 }
1394
1395 /*
1396 * The reason we have to wait for the target here is that the
1397 * driver layer has to init BMI in order to set the host block
1398 * size.
1399 */
1400 if (ath6kl_htc_wait_target(ar->htc_target)) {
1401 ret = -EIO;
1402 goto err_power_off;
1403 }
1404
1405 if (ath6kl_init_service_ep(ar)) {
1406 ret = -EIO;
1407 goto err_cleanup_scatter;
1408 }
1409
1410 /* setup credit distribution */
1411 ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
1412
1413 /* start HTC */
1414 ret = ath6kl_htc_start(ar->htc_target);
1415 if (ret) {
1416 /* FIXME: call this */
1417 ath6kl_cookie_cleanup(ar);
1418 goto err_cleanup_scatter;
1419 }
1420
1421 /* Wait for Wmi event to be ready */
1422 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1423 test_bit(WMI_READY,
1424 &ar->flag),
1425 WMI_TIMEOUT);
1426
1427 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1428
1429 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1430 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1431 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1432 ret = -EIO;
1433 goto err_htc_stop;
1434 }
1435
1436 if (!timeleft || signal_pending(current)) {
1437 ath6kl_err("wmi is not ready or wait was interrupted\n");
1438 ret = -EIO;
1439 goto err_htc_stop;
1440 }
1441
1442 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1443
1444 /* communicate the wmi protocol verision to the target */
1445 /* FIXME: return error */
1446 if ((ath6kl_set_host_app_area(ar)) != 0)
1447 ath6kl_err("unable to set the host app area\n");
1448
1449 for (i = 0; i < MAX_NUM_VIF; i++) {
1450 ret = ath6kl_target_config_wlan_params(ar, i);
1451 if (ret)
1452 goto err_htc_stop;
1453 }
1454
76a9fbe2
KV
1455 ar->state = ATH6KL_STATE_ON;
1456
20459ee2
KV
1457 return 0;
1458
1459err_htc_stop:
1460 ath6kl_htc_stop(ar->htc_target);
1461err_cleanup_scatter:
1462 ath6kl_hif_cleanup_scatter(ar);
1463err_power_off:
1464 ath6kl_hif_power_off(ar);
1465
1466 return ret;
1467}
1468
5fe4dffb
KV
1469int ath6kl_init_hw_stop(struct ath6kl *ar)
1470{
1471 int ret;
1472
1473 ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
1474
1475 ath6kl_htc_stop(ar->htc_target);
1476
1477 ath6kl_hif_stop(ar);
1478
1479 ath6kl_bmi_reset(ar);
1480
1481 ret = ath6kl_hif_power_off(ar);
1482 if (ret)
1483 ath6kl_warn("failed to power off hif: %d\n", ret);
1484
76a9fbe2
KV
1485 ar->state = ATH6KL_STATE_OFF;
1486
5fe4dffb
KV
1487 return 0;
1488}
1489
61448a93 1490int ath6kl_core_init(struct ath6kl *ar)
bdcd8170 1491{
61448a93 1492 struct ath6kl_bmi_target_info targ_info;
8dafb70e 1493 struct net_device *ndev;
20459ee2 1494 int ret = 0, i;
bdcd8170 1495
61448a93
KV
1496 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1497 if (!ar->ath6kl_wq)
1498 return -ENOMEM;
1499
1500 ret = ath6kl_bmi_init(ar);
1501 if (ret)
1502 goto err_wq;
1503
20459ee2
KV
1504 /*
1505 * Turn on power to get hardware (target) version and leave power
1506 * on delibrately as we will boot the hardware anyway within few
1507 * seconds.
1508 */
61448a93
KV
1509 ret = ath6kl_hif_power_on(ar);
1510 if (ret)
1511 goto err_bmi_cleanup;
1512
1513 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1514 if (ret)
1515 goto err_power_off;
1516
1517 ar->version.target_ver = le32_to_cpu(targ_info.version);
1518 ar->target_type = le32_to_cpu(targ_info.type);
1519 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
1520
1521 ret = ath6kl_init_hw_params(ar);
1522 if (ret)
1523 goto err_power_off;
1524
61448a93
KV
1525 ar->htc_target = ath6kl_htc_create(ar);
1526
1527 if (!ar->htc_target) {
1528 ret = -ENOMEM;
1529 goto err_power_off;
1530 }
1531
1532 ret = ath6kl_fetch_firmwares(ar);
1533 if (ret)
1534 goto err_htc_cleanup;
1535
1536 /* FIXME: we should free all firmwares in the error cases below */
1537
bdcd8170
KV
1538 /* Indicate that WMI is enabled (although not ready yet) */
1539 set_bit(WMI_ENABLED, &ar->flag);
2865785e 1540 ar->wmi = ath6kl_wmi_init(ar);
bdcd8170
KV
1541 if (!ar->wmi) {
1542 ath6kl_err("failed to initialize wmi\n");
61448a93
KV
1543 ret = -EIO;
1544 goto err_htc_cleanup;
bdcd8170
KV
1545 }
1546
1547 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1548
61448a93
KV
1549 ret = ath6kl_register_ieee80211_hw(ar);
1550 if (ret)
8dafb70e
VT
1551 goto err_node_cleanup;
1552
61448a93
KV
1553 ret = ath6kl_debug_init(ar);
1554 if (ret) {
8dafb70e
VT
1555 wiphy_unregister(ar->wiphy);
1556 goto err_node_cleanup;
1557 }
1558
55055976
VT
1559 for (i = 0; i < MAX_NUM_VIF; i++)
1560 ar->avail_idx_map |= BIT(i);
1561
27929723
VT
1562 rtnl_lock();
1563
8dafb70e 1564 /* Add an initial station interface */
55055976
VT
1565 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0,
1566 INFRA_NETWORK);
27929723
VT
1567
1568 rtnl_unlock();
1569
8dafb70e
VT
1570 if (!ndev) {
1571 ath6kl_err("Failed to instantiate a network device\n");
61448a93 1572 ret = -ENOMEM;
8dafb70e
VT
1573 wiphy_unregister(ar->wiphy);
1574 goto err_debug_init;
1575 }
1576
1577
1578 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
28ae58dd 1579 __func__, ndev->name, ndev, ar);
8dafb70e 1580
bdcd8170
KV
1581 /* setup access class priority mappings */
1582 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1583 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1584 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1585 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1586
1587 /* give our connected endpoints some buffers */
1588 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1589 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1590
1591 /* allocate some buffers that handle larger AMSDU frames */
1592 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1593
bdcd8170
KV
1594 ath6kl_cookie_init(ar);
1595
bdcd8170
KV
1596 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1597 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1598
be98e3a4
VT
1599 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1600 WIPHY_FLAG_HAVE_AP_SME;
011a36e1 1601
5fe4dffb
KV
1602 set_bit(FIRST_BOOT, &ar->flag);
1603
1604 ret = ath6kl_init_hw_start(ar);
20459ee2 1605 if (ret) {
5fe4dffb 1606 ath6kl_err("Failed to start hardware: %d\n", ret);
20459ee2 1607 goto err_rxbuf_cleanup;
0ce59445 1608 }
d66ea4f9
VT
1609
1610 /*
1611 * Set mac address which is received in ready event
1612 * FIXME: Move to ath6kl_interface_add()
1613 */
1614 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
1615
5fe4dffb
KV
1616 ret = ath6kl_init_hw_stop(ar);
1617 if (ret) {
1618 ath6kl_err("Failed to stop hardware: %d\n", ret);
1619 goto err_htc_cleanup;
1620 }
1621
61448a93 1622 return ret;
bdcd8170 1623
bdcd8170 1624err_rxbuf_cleanup:
ad226ec2 1625 ath6kl_htc_flush_rx_buf(ar->htc_target);
bdcd8170 1626 ath6kl_cleanup_amsdu_rxbufs(ar);
27929723 1627 rtnl_lock();
108438bc 1628 ath6kl_deinit_if_data(netdev_priv(ndev));
27929723 1629 rtnl_unlock();
8dafb70e
VT
1630 wiphy_unregister(ar->wiphy);
1631err_debug_init:
1632 ath6kl_debug_cleanup(ar);
852bd9d9 1633err_node_cleanup:
bdcd8170
KV
1634 ath6kl_wmi_shutdown(ar->wmi);
1635 clear_bit(WMI_ENABLED, &ar->flag);
1636 ar->wmi = NULL;
bdcd8170 1637err_htc_cleanup:
ad226ec2 1638 ath6kl_htc_cleanup(ar->htc_target);
b2e75698
KV
1639err_power_off:
1640 ath6kl_hif_power_off(ar);
bdcd8170
KV
1641err_bmi_cleanup:
1642 ath6kl_bmi_cleanup(ar);
1643err_wq:
1644 destroy_workqueue(ar->ath6kl_wq);
8dafb70e 1645
bdcd8170
KV
1646 return ret;
1647}
1648
55055976 1649void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
6db8fa53
VT
1650{
1651 static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1652 bool discon_issued;
1653
1654 netif_stop_queue(vif->ndev);
1655
1656 clear_bit(WLAN_ENABLED, &vif->flags);
1657
1658 if (wmi_ready) {
1659 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1660 test_bit(CONNECT_PEND, &vif->flags);
1661 ath6kl_disconnect(vif);
1662 del_timer(&vif->disconnect_timer);
1663
1664 if (discon_issued)
1665 ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1666 (vif->nw_type & AP_NETWORK) ?
1667 bcast_mac : vif->bssid,
1668 0, NULL, 0);
1669 }
1670
1671 if (vif->scan_req) {
1672 cfg80211_scan_done(vif->scan_req, true);
1673 vif->scan_req = NULL;
1674 }
6db8fa53
VT
1675}
1676
bdcd8170
KV
1677void ath6kl_stop_txrx(struct ath6kl *ar)
1678{
990bd915 1679 struct ath6kl_vif *vif, *tmp_vif;
bdcd8170
KV
1680
1681 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1682
1683 if (down_interruptible(&ar->sem)) {
1684 ath6kl_err("down_interruptible failed\n");
1685 return;
1686 }
1687
990bd915
VT
1688 spin_lock(&ar->list_lock);
1689 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1690 list_del(&vif->list);
1691 spin_unlock(&ar->list_lock);
1692 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
27929723
VT
1693 rtnl_lock();
1694 ath6kl_deinit_if_data(vif);
1695 rtnl_unlock();
990bd915
VT
1696 spin_lock(&ar->list_lock);
1697 }
1698 spin_unlock(&ar->list_lock);
bdcd8170 1699
6db8fa53 1700 clear_bit(WMI_READY, &ar->flag);
bdcd8170 1701
6db8fa53
VT
1702 /*
1703 * After wmi_shudown all WMI events will be dropped. We
1704 * need to cleanup the buffers allocated in AP mode and
1705 * give disconnect notification to stack, which usually
1706 * happens in the disconnect_event. Simulate the disconnect
1707 * event by calling the function directly. Sometimes
1708 * disconnect_event will be received when the debug logs
1709 * are collected.
1710 */
1711 ath6kl_wmi_shutdown(ar->wmi);
bdcd8170 1712
6db8fa53
VT
1713 clear_bit(WMI_ENABLED, &ar->flag);
1714 if (ar->htc_target) {
1715 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1716 ath6kl_htc_stop(ar->htc_target);
bdcd8170
KV
1717 }
1718
6db8fa53
VT
1719 /*
1720 * Try to reset the device if we can. The driver may have been
1721 * configure NOT to reset the target during a debug session.
1722 */
1723 ath6kl_dbg(ATH6KL_DBG_TRC,
1724 "attempting to reset target on instance destroy\n");
1725 ath6kl_reset_device(ar, ar->target_type, true, true);
19703573 1726
6db8fa53 1727 clear_bit(WLAN_ENABLED, &ar->flag);
bdcd8170 1728}
This page took 0.159568 seconds and 5 git commands to generate.