ath6kl: Maintain virtual interface in a list
[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>
92ecbff4 19#include <linux/of.h>
bdcd8170
KV
20#include <linux/mmc/sdio_func.h>
21#include "core.h"
22#include "cfg80211.h"
23#include "target.h"
24#include "debug.h"
25#include "hif-ops.h"
26
27unsigned int debug_mask;
003353b0 28static unsigned int testmode;
bdcd8170
KV
29
30module_param(debug_mask, uint, 0644);
003353b0 31module_param(testmode, uint, 0644);
bdcd8170
KV
32
33/*
34 * Include definitions here that can be used to tune the WLAN module
35 * behavior. Different customers can tune the behavior as per their needs,
36 * here.
37 */
38
39/*
40 * This configuration item enable/disable keepalive support.
41 * Keepalive support: In the absence of any data traffic to AP, null
42 * frames will be sent to the AP at periodic interval, to keep the association
43 * active. This configuration item defines the periodic interval.
44 * Use value of zero to disable keepalive support
45 * Default: 60 seconds
46 */
47#define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
48
49/*
50 * This configuration item sets the value of disconnect timeout
51 * Firmware delays sending the disconnec event to the host for this
52 * timeout after is gets disconnected from the current AP.
53 * If the firmware successly roams within the disconnect timeout
54 * it sends a new connect event
55 */
56#define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
57
58#define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
59
bdcd8170
KV
60#define ATH6KL_DATA_OFFSET 64
61struct sk_buff *ath6kl_buf_alloc(int size)
62{
63 struct sk_buff *skb;
64 u16 reserved;
65
66 /* Add chacheline space at front and back of buffer */
67 reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
1df94a85 68 sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
bdcd8170
KV
69 skb = dev_alloc_skb(size + reserved);
70
71 if (skb)
72 skb_reserve(skb, reserved - L1_CACHE_BYTES);
73 return skb;
74}
75
e29f25f5 76void ath6kl_init_profile_info(struct ath6kl_vif *vif)
bdcd8170 77{
3450334f
VT
78 vif->ssid_len = 0;
79 memset(vif->ssid, 0, sizeof(vif->ssid));
80
81 vif->dot11_auth_mode = OPEN_AUTH;
82 vif->auth_mode = NONE_AUTH;
83 vif->prwise_crypto = NONE_CRYPT;
84 vif->prwise_crypto_len = 0;
85 vif->grp_crypto = NONE_CRYPT;
86 vif->grp_crypto_len = 0;
6f2a73f9 87 memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
8c8b65e3
VT
88 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
89 memset(vif->bssid, 0, sizeof(vif->bssid));
f74bac54 90 vif->bss_ch = 0;
f5938f24 91 vif->nw_type = vif->next_mode = INFRA_NETWORK;
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
301#define REG_DUMP_COUNT_AR6003 60
302#define REGISTER_DUMP_LEN_MAX 60
303
304static void ath6kl_dump_target_assert_info(struct ath6kl *ar)
305{
306 u32 address;
307 u32 regdump_loc = 0;
308 int status;
309 u32 regdump_val[REGISTER_DUMP_LEN_MAX];
310 u32 i;
311
312 if (ar->target_type != TARGET_TYPE_AR6003)
313 return;
314
315 /* the reg dump pointer is copied to the host interest area */
316 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_failure_state));
31024d99 317 address = TARG_VTOP(ar->target_type, address);
bdcd8170
KV
318
319 /* read RAM location through diagnostic window */
addb44be 320 status = ath6kl_diag_read32(ar, address, &regdump_loc);
bdcd8170
KV
321
322 if (status || !regdump_loc) {
323 ath6kl_err("failed to get ptr to register dump area\n");
324 return;
325 }
326
327 ath6kl_dbg(ATH6KL_DBG_TRC, "location of register dump data: 0x%X\n",
328 regdump_loc);
31024d99 329 regdump_loc = TARG_VTOP(ar->target_type, regdump_loc);
bdcd8170
KV
330
331 /* fetch register dump data */
addb44be
KV
332 status = ath6kl_diag_read(ar, regdump_loc, (u8 *)&regdump_val[0],
333 REG_DUMP_COUNT_AR6003 * (sizeof(u32)));
bdcd8170
KV
334
335 if (status) {
336 ath6kl_err("failed to get register dump\n");
337 return;
338 }
339 ath6kl_dbg(ATH6KL_DBG_TRC, "Register Dump:\n");
340
341 for (i = 0; i < REG_DUMP_COUNT_AR6003; i++)
342 ath6kl_dbg(ATH6KL_DBG_TRC, " %d : 0x%8.8X\n",
343 i, regdump_val[i]);
344
345}
346
347void ath6kl_target_failure(struct ath6kl *ar)
348{
349 ath6kl_err("target asserted\n");
350
351 /* try dumping target assertion information (if any) */
352 ath6kl_dump_target_assert_info(ar);
353
354}
355
356static int ath6kl_target_config_wlan_params(struct ath6kl *ar)
357{
358 int status = 0;
4dea08e0 359 int ret;
bdcd8170
KV
360
361 /*
362 * Configure the device for rx dot11 header rules. "0,0" are the
363 * default values. Required if checksum offload is needed. Set
364 * RxMetaVersion to 2.
365 */
366 if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
367 ar->rx_meta_ver, 0, 0)) {
368 ath6kl_err("unable to set the rx frame format\n");
369 status = -EIO;
370 }
371
372 if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
373 if ((ath6kl_wmi_pmparams_cmd(ar->wmi, 0, 1, 0, 0, 1,
374 IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
375 ath6kl_err("unable to set power save fail event policy\n");
376 status = -EIO;
377 }
378
379 if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
380 if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, 0,
381 WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
382 ath6kl_err("unable to set barker preamble policy\n");
383 status = -EIO;
384 }
385
386 if (ath6kl_wmi_set_keepalive_cmd(ar->wmi,
387 WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
388 ath6kl_err("unable to set keep alive interval\n");
389 status = -EIO;
390 }
391
392 if (ath6kl_wmi_disctimeout_cmd(ar->wmi,
393 WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
394 ath6kl_err("unable to set disconnect timeout\n");
395 status = -EIO;
396 }
397
398 if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
399 if (ath6kl_wmi_set_wmm_txop(ar->wmi, WMI_TXOP_DISABLED)) {
400 ath6kl_err("unable to set txop bursting\n");
401 status = -EIO;
402 }
403
6bbc7c35
JM
404 if (ar->p2p) {
405 ret = ath6kl_wmi_info_req_cmd(ar->wmi,
406 P2P_FLAG_CAPABILITIES_REQ |
407 P2P_FLAG_MACADDR_REQ |
408 P2P_FLAG_HMODEL_REQ);
409 if (ret) {
410 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
411 "capabilities (%d) - assuming P2P not "
412 "supported\n", ret);
413 ar->p2p = 0;
414 }
415 }
416
417 if (ar->p2p) {
418 /* Enable Probe Request reporting for P2P */
419 ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, true);
420 if (ret) {
421 ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
422 "Request reporting (%d)\n", ret);
423 }
4dea08e0
JM
424 }
425
bdcd8170
KV
426 return status;
427}
428
429int ath6kl_configure_target(struct ath6kl *ar)
430{
431 u32 param, ram_reserved_size;
432 u8 fw_iftype;
433
dd3751f7 434 fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
bdcd8170
KV
435
436 /* Tell target which HTC version it is used*/
437 param = HTC_PROTOCOL_VERSION;
438 if (ath6kl_bmi_write(ar,
439 ath6kl_get_hi_item_addr(ar,
440 HI_ITEM(hi_app_host_interest)),
441 (u8 *)&param, 4) != 0) {
442 ath6kl_err("bmi_write_memory for htc version failed\n");
443 return -EIO;
444 }
445
446 /* set the firmware mode to STA/IBSS/AP */
447 param = 0;
448
449 if (ath6kl_bmi_read(ar,
450 ath6kl_get_hi_item_addr(ar,
451 HI_ITEM(hi_option_flag)),
452 (u8 *)&param, 4) != 0) {
453 ath6kl_err("bmi_read_memory for setting fwmode failed\n");
454 return -EIO;
455 }
456
457 param |= (1 << HI_OPTION_NUM_DEV_SHIFT);
458 param |= (fw_iftype << HI_OPTION_FW_MODE_SHIFT);
6bbc7c35
JM
459 if (ar->p2p && fw_iftype == HI_OPTION_FW_MODE_BSS_STA) {
460 param |= HI_OPTION_FW_SUBMODE_P2PDEV <<
461 HI_OPTION_FW_SUBMODE_SHIFT;
462 }
bdcd8170
KV
463 param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
464 param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
465
466 if (ath6kl_bmi_write(ar,
467 ath6kl_get_hi_item_addr(ar,
468 HI_ITEM(hi_option_flag)),
469 (u8 *)&param,
470 4) != 0) {
471 ath6kl_err("bmi_write_memory for setting fwmode failed\n");
472 return -EIO;
473 }
474
475 ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
476
477 /*
478 * Hardcode the address use for the extended board data
479 * Ideally this should be pre-allocate by the OS at boot time
480 * But since it is a new feature and board data is loaded
481 * at init time, we have to workaround this from host.
482 * It is difficult to patch the firmware boot code,
483 * but possible in theory.
484 */
485
991b27ea
KV
486 param = ar->hw.board_ext_data_addr;
487 ram_reserved_size = ar->hw.reserved_ram_size;
bdcd8170 488
991b27ea
KV
489 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
490 HI_ITEM(hi_board_ext_data)),
491 (u8 *)&param, 4) != 0) {
492 ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
493 return -EIO;
494 }
495
496 if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
497 HI_ITEM(hi_end_ram_reserve_sz)),
498 (u8 *)&ram_reserved_size, 4) != 0) {
499 ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
500 return -EIO;
bdcd8170
KV
501 }
502
503 /* set the block size for the target */
504 if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
505 /* use default number of control buffers */
506 return -EIO;
507
508 return 0;
509}
510
8dafb70e 511void ath6kl_core_free(struct ath6kl *ar)
bdcd8170 512{
8dafb70e 513 wiphy_free(ar->wiphy);
bdcd8170
KV
514}
515
6db8fa53 516void ath6kl_core_cleanup(struct ath6kl *ar)
bdcd8170 517{
6db8fa53 518 destroy_workqueue(ar->ath6kl_wq);
bdcd8170 519
6db8fa53
VT
520 if (ar->htc_target)
521 ath6kl_htc_cleanup(ar->htc_target);
522
523 ath6kl_cookie_cleanup(ar);
524
525 ath6kl_cleanup_amsdu_rxbufs(ar);
526
527 ath6kl_bmi_cleanup(ar);
528
529 ath6kl_debug_cleanup(ar);
530
531 kfree(ar->fw_board);
532 kfree(ar->fw_otp);
533 kfree(ar->fw);
534 kfree(ar->fw_patch);
535
536 ath6kl_deinit_ieee80211_hw(ar);
bdcd8170
KV
537}
538
539/* firmware upload */
bdcd8170
KV
540static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
541 u8 **fw, size_t *fw_len)
542{
543 const struct firmware *fw_entry;
544 int ret;
545
546 ret = request_firmware(&fw_entry, filename, ar->dev);
547 if (ret)
548 return ret;
549
550 *fw_len = fw_entry->size;
551 *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
552
553 if (*fw == NULL)
554 ret = -ENOMEM;
555
556 release_firmware(fw_entry);
557
558 return ret;
559}
560
92ecbff4
SL
561#ifdef CONFIG_OF
562static const char *get_target_ver_dir(const struct ath6kl *ar)
563{
564 switch (ar->version.target_ver) {
565 case AR6003_REV1_VERSION:
566 return "ath6k/AR6003/hw1.0";
567 case AR6003_REV2_VERSION:
568 return "ath6k/AR6003/hw2.0";
569 case AR6003_REV3_VERSION:
570 return "ath6k/AR6003/hw2.1.1";
571 }
572 ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
573 ar->version.target_ver);
574 return NULL;
575}
576
577/*
578 * Check the device tree for a board-id and use it to construct
579 * the pathname to the firmware file. Used (for now) to find a
580 * fallback to the "bdata.bin" file--typically a symlink to the
581 * appropriate board-specific file.
582 */
583static bool check_device_tree(struct ath6kl *ar)
584{
585 static const char *board_id_prop = "atheros,board-id";
586 struct device_node *node;
587 char board_filename[64];
588 const char *board_id;
589 int ret;
590
591 for_each_compatible_node(node, NULL, "atheros,ath6kl") {
592 board_id = of_get_property(node, board_id_prop, NULL);
593 if (board_id == NULL) {
594 ath6kl_warn("No \"%s\" property on %s node.\n",
595 board_id_prop, node->name);
596 continue;
597 }
598 snprintf(board_filename, sizeof(board_filename),
599 "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
600
601 ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
602 &ar->fw_board_len);
603 if (ret) {
604 ath6kl_err("Failed to get DT board file %s: %d\n",
605 board_filename, ret);
606 continue;
607 }
608 return true;
609 }
610 return false;
611}
612#else
613static bool check_device_tree(struct ath6kl *ar)
614{
615 return false;
616}
617#endif /* CONFIG_OF */
618
bdcd8170
KV
619static int ath6kl_fetch_board_file(struct ath6kl *ar)
620{
621 const char *filename;
622 int ret;
623
772c31ee
KV
624 if (ar->fw_board != NULL)
625 return 0;
626
bdcd8170
KV
627 switch (ar->version.target_ver) {
628 case AR6003_REV2_VERSION:
629 filename = AR6003_REV2_BOARD_DATA_FILE;
630 break;
31024d99
KF
631 case AR6004_REV1_VERSION:
632 filename = AR6004_REV1_BOARD_DATA_FILE;
633 break;
bdcd8170
KV
634 default:
635 filename = AR6003_REV3_BOARD_DATA_FILE;
636 break;
637 }
638
639 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
640 &ar->fw_board_len);
641 if (ret == 0) {
642 /* managed to get proper board file */
643 return 0;
644 }
645
92ecbff4
SL
646 if (check_device_tree(ar)) {
647 /* got board file from device tree */
648 return 0;
649 }
650
bdcd8170
KV
651 /* there was no proper board file, try to use default instead */
652 ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
653 filename, ret);
654
655 switch (ar->version.target_ver) {
656 case AR6003_REV2_VERSION:
657 filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
658 break;
31024d99
KF
659 case AR6004_REV1_VERSION:
660 filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
661 break;
bdcd8170
KV
662 default:
663 filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
664 break;
665 }
666
667 ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
668 &ar->fw_board_len);
669 if (ret) {
670 ath6kl_err("Failed to get default board file %s: %d\n",
671 filename, ret);
672 return ret;
673 }
674
675 ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
676 ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
677
678 return 0;
679}
680
772c31ee
KV
681static int ath6kl_fetch_otp_file(struct ath6kl *ar)
682{
683 const char *filename;
684 int ret;
685
686 if (ar->fw_otp != NULL)
687 return 0;
688
689 switch (ar->version.target_ver) {
690 case AR6003_REV2_VERSION:
691 filename = AR6003_REV2_OTP_FILE;
692 break;
693 case AR6004_REV1_VERSION:
694 ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
695 return 0;
696 break;
697 default:
698 filename = AR6003_REV3_OTP_FILE;
699 break;
700 }
701
702 ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
703 &ar->fw_otp_len);
704 if (ret) {
705 ath6kl_err("Failed to get OTP file %s: %d\n",
706 filename, ret);
707 return ret;
708 }
709
710 return 0;
711}
712
713static int ath6kl_fetch_fw_file(struct ath6kl *ar)
714{
715 const char *filename;
716 int ret;
717
718 if (ar->fw != NULL)
719 return 0;
720
721 if (testmode) {
722 switch (ar->version.target_ver) {
723 case AR6003_REV2_VERSION:
724 filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
725 break;
726 case AR6003_REV3_VERSION:
727 filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
728 break;
729 case AR6004_REV1_VERSION:
730 ath6kl_warn("testmode not supported with ar6004\n");
731 return -EOPNOTSUPP;
732 default:
733 ath6kl_warn("unknown target version: 0x%x\n",
734 ar->version.target_ver);
735 return -EINVAL;
736 }
737
738 set_bit(TESTMODE, &ar->flag);
739
740 goto get_fw;
741 }
742
743 switch (ar->version.target_ver) {
744 case AR6003_REV2_VERSION:
745 filename = AR6003_REV2_FIRMWARE_FILE;
746 break;
747 case AR6004_REV1_VERSION:
748 filename = AR6004_REV1_FIRMWARE_FILE;
749 break;
750 default:
751 filename = AR6003_REV3_FIRMWARE_FILE;
752 break;
753 }
754
755get_fw:
756 ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
757 if (ret) {
758 ath6kl_err("Failed to get firmware file %s: %d\n",
759 filename, ret);
760 return ret;
761 }
762
763 return 0;
764}
765
766static int ath6kl_fetch_patch_file(struct ath6kl *ar)
767{
768 const char *filename;
769 int ret;
770
771 switch (ar->version.target_ver) {
772 case AR6003_REV2_VERSION:
773 filename = AR6003_REV2_PATCH_FILE;
774 break;
775 case AR6004_REV1_VERSION:
776 /* FIXME: implement for AR6004 */
777 return 0;
778 break;
779 default:
780 filename = AR6003_REV3_PATCH_FILE;
781 break;
782 }
783
784 if (ar->fw_patch == NULL) {
785 ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
786 &ar->fw_patch_len);
787 if (ret) {
788 ath6kl_err("Failed to get patch file %s: %d\n",
789 filename, ret);
790 return ret;
791 }
792 }
793
794 return 0;
795}
796
50d41234 797static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
772c31ee
KV
798{
799 int ret;
800
772c31ee
KV
801 ret = ath6kl_fetch_otp_file(ar);
802 if (ret)
803 return ret;
804
805 ret = ath6kl_fetch_fw_file(ar);
806 if (ret)
807 return ret;
808
809 ret = ath6kl_fetch_patch_file(ar);
810 if (ret)
811 return ret;
812
813 return 0;
814}
bdcd8170 815
50d41234
KV
816static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
817{
818 size_t magic_len, len, ie_len;
819 const struct firmware *fw;
820 struct ath6kl_fw_ie *hdr;
821 const char *filename;
822 const u8 *data;
97e0496d 823 int ret, ie_id, i, index, bit;
8a137480 824 __le32 *val;
50d41234
KV
825
826 switch (ar->version.target_ver) {
827 case AR6003_REV2_VERSION:
828 filename = AR6003_REV2_FIRMWARE_2_FILE;
829 break;
830 case AR6003_REV3_VERSION:
831 filename = AR6003_REV3_FIRMWARE_2_FILE;
832 break;
833 case AR6004_REV1_VERSION:
834 filename = AR6004_REV1_FIRMWARE_2_FILE;
835 break;
836 default:
837 return -EOPNOTSUPP;
838 }
839
840 ret = request_firmware(&fw, filename, ar->dev);
841 if (ret)
842 return ret;
843
844 data = fw->data;
845 len = fw->size;
846
847 /* magic also includes the null byte, check that as well */
848 magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
849
850 if (len < magic_len) {
851 ret = -EINVAL;
852 goto out;
853 }
854
855 if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
856 ret = -EINVAL;
857 goto out;
858 }
859
860 len -= magic_len;
861 data += magic_len;
862
863 /* loop elements */
864 while (len > sizeof(struct ath6kl_fw_ie)) {
865 /* hdr is unaligned! */
866 hdr = (struct ath6kl_fw_ie *) data;
867
868 ie_id = le32_to_cpup(&hdr->id);
869 ie_len = le32_to_cpup(&hdr->len);
870
871 len -= sizeof(*hdr);
872 data += sizeof(*hdr);
873
874 if (len < ie_len) {
875 ret = -EINVAL;
876 goto out;
877 }
878
879 switch (ie_id) {
880 case ATH6KL_FW_IE_OTP_IMAGE:
ef548626 881 ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
6bc36431
KV
882 ie_len);
883
50d41234
KV
884 ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
885
886 if (ar->fw_otp == NULL) {
887 ret = -ENOMEM;
888 goto out;
889 }
890
891 ar->fw_otp_len = ie_len;
892 break;
893 case ATH6KL_FW_IE_FW_IMAGE:
ef548626 894 ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
6bc36431
KV
895 ie_len);
896
50d41234
KV
897 ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
898
899 if (ar->fw == NULL) {
900 ret = -ENOMEM;
901 goto out;
902 }
903
904 ar->fw_len = ie_len;
905 break;
906 case ATH6KL_FW_IE_PATCH_IMAGE:
ef548626 907 ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
6bc36431
KV
908 ie_len);
909
50d41234
KV
910 ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
911
912 if (ar->fw_patch == NULL) {
913 ret = -ENOMEM;
914 goto out;
915 }
916
917 ar->fw_patch_len = ie_len;
918 break;
8a137480
KV
919 case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
920 val = (__le32 *) data;
921 ar->hw.reserved_ram_size = le32_to_cpup(val);
6bc36431
KV
922
923 ath6kl_dbg(ATH6KL_DBG_BOOT,
924 "found reserved ram size ie 0x%d\n",
925 ar->hw.reserved_ram_size);
8a137480 926 break;
97e0496d 927 case ATH6KL_FW_IE_CAPABILITIES:
6bc36431 928 ath6kl_dbg(ATH6KL_DBG_BOOT,
ef548626 929 "found firmware capabilities ie (%zd B)\n",
6bc36431
KV
930 ie_len);
931
97e0496d
KV
932 for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
933 index = ALIGN(i, 8) / 8;
934 bit = i % 8;
935
936 if (data[index] & (1 << bit))
937 __set_bit(i, ar->fw_capabilities);
938 }
6bc36431
KV
939
940 ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
941 ar->fw_capabilities,
942 sizeof(ar->fw_capabilities));
97e0496d 943 break;
1b4304da
KV
944 case ATH6KL_FW_IE_PATCH_ADDR:
945 if (ie_len != sizeof(*val))
946 break;
947
948 val = (__le32 *) data;
949 ar->hw.dataset_patch_addr = le32_to_cpup(val);
6bc36431
KV
950
951 ath6kl_dbg(ATH6KL_DBG_BOOT,
952 "found patch address ie 0x%d\n",
953 ar->hw.dataset_patch_addr);
1b4304da 954 break;
50d41234 955 default:
6bc36431 956 ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
50d41234
KV
957 le32_to_cpup(&hdr->id));
958 break;
959 }
960
961 len -= ie_len;
962 data += ie_len;
963 };
964
965 ret = 0;
966out:
967 release_firmware(fw);
968
969 return ret;
970}
971
972static int ath6kl_fetch_firmwares(struct ath6kl *ar)
973{
974 int ret;
975
976 ret = ath6kl_fetch_board_file(ar);
977 if (ret)
978 return ret;
979
980 ret = ath6kl_fetch_fw_api2(ar);
6bc36431
KV
981 if (ret == 0) {
982 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 2\n");
50d41234 983 return 0;
6bc36431 984 }
50d41234
KV
985
986 ret = ath6kl_fetch_fw_api1(ar);
987 if (ret)
988 return ret;
989
6bc36431
KV
990 ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api 1\n");
991
50d41234
KV
992 return 0;
993}
994
bdcd8170
KV
995static int ath6kl_upload_board_file(struct ath6kl *ar)
996{
997 u32 board_address, board_ext_address, param;
31024d99 998 u32 board_data_size, board_ext_data_size;
bdcd8170
KV
999 int ret;
1000
772c31ee
KV
1001 if (WARN_ON(ar->fw_board == NULL))
1002 return -ENOENT;
bdcd8170 1003
31024d99
KF
1004 /*
1005 * Determine where in Target RAM to write Board Data.
1006 * For AR6004, host determine Target RAM address for
1007 * writing board data.
1008 */
1009 if (ar->target_type == TARGET_TYPE_AR6004) {
1010 board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
1011 ath6kl_bmi_write(ar,
1012 ath6kl_get_hi_item_addr(ar,
1013 HI_ITEM(hi_board_data)),
1014 (u8 *) &board_address, 4);
1015 } else {
1016 ath6kl_bmi_read(ar,
1017 ath6kl_get_hi_item_addr(ar,
1018 HI_ITEM(hi_board_data)),
1019 (u8 *) &board_address, 4);
1020 }
1021
bdcd8170
KV
1022 /* determine where in target ram to write extended board data */
1023 ath6kl_bmi_read(ar,
1024 ath6kl_get_hi_item_addr(ar,
1025 HI_ITEM(hi_board_ext_data)),
1026 (u8 *) &board_ext_address, 4);
1027
bdcd8170
KV
1028 if (board_ext_address == 0) {
1029 ath6kl_err("Failed to get board file target address.\n");
1030 return -EINVAL;
1031 }
1032
31024d99
KF
1033 switch (ar->target_type) {
1034 case TARGET_TYPE_AR6003:
1035 board_data_size = AR6003_BOARD_DATA_SZ;
1036 board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1037 break;
1038 case TARGET_TYPE_AR6004:
1039 board_data_size = AR6004_BOARD_DATA_SZ;
1040 board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
1041 break;
1042 default:
1043 WARN_ON(1);
1044 return -EINVAL;
1045 break;
1046 }
1047
1048 if (ar->fw_board_len == (board_data_size +
1049 board_ext_data_size)) {
1050
bdcd8170 1051 /* write extended board data */
6bc36431
KV
1052 ath6kl_dbg(ATH6KL_DBG_BOOT,
1053 "writing extended board data to 0x%x (%d B)\n",
1054 board_ext_address, board_ext_data_size);
1055
bdcd8170 1056 ret = ath6kl_bmi_write(ar, board_ext_address,
31024d99
KF
1057 ar->fw_board + board_data_size,
1058 board_ext_data_size);
bdcd8170
KV
1059 if (ret) {
1060 ath6kl_err("Failed to write extended board data: %d\n",
1061 ret);
1062 return ret;
1063 }
1064
1065 /* record that extended board data is initialized */
31024d99
KF
1066 param = (board_ext_data_size << 16) | 1;
1067
bdcd8170
KV
1068 ath6kl_bmi_write(ar,
1069 ath6kl_get_hi_item_addr(ar,
1070 HI_ITEM(hi_board_ext_data_config)),
1071 (unsigned char *) &param, 4);
1072 }
1073
31024d99 1074 if (ar->fw_board_len < board_data_size) {
bdcd8170
KV
1075 ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1076 ret = -EINVAL;
1077 return ret;
1078 }
1079
6bc36431
KV
1080 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
1081 board_address, board_data_size);
1082
bdcd8170 1083 ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
31024d99 1084 board_data_size);
bdcd8170
KV
1085
1086 if (ret) {
1087 ath6kl_err("Board file bmi write failed: %d\n", ret);
1088 return ret;
1089 }
1090
1091 /* record the fact that Board Data IS initialized */
1092 param = 1;
1093 ath6kl_bmi_write(ar,
1094 ath6kl_get_hi_item_addr(ar,
1095 HI_ITEM(hi_board_data_initialized)),
1096 (u8 *)&param, 4);
1097
1098 return ret;
1099}
1100
1101static int ath6kl_upload_otp(struct ath6kl *ar)
1102{
bdcd8170 1103 u32 address, param;
bef26a7f 1104 bool from_hw = false;
bdcd8170
KV
1105 int ret;
1106
772c31ee
KV
1107 if (WARN_ON(ar->fw_otp == NULL))
1108 return -ENOENT;
bdcd8170 1109
a01ac414 1110 address = ar->hw.app_load_addr;
bdcd8170 1111
ef548626 1112 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
6bc36431
KV
1113 ar->fw_otp_len);
1114
bdcd8170
KV
1115 ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1116 ar->fw_otp_len);
1117 if (ret) {
1118 ath6kl_err("Failed to upload OTP file: %d\n", ret);
1119 return ret;
1120 }
1121
639d0b89
KV
1122 /* read firmware start address */
1123 ret = ath6kl_bmi_read(ar,
1124 ath6kl_get_hi_item_addr(ar,
1125 HI_ITEM(hi_app_start)),
1126 (u8 *) &address, sizeof(address));
1127
1128 if (ret) {
1129 ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1130 return ret;
1131 }
1132
bef26a7f
KV
1133 if (ar->hw.app_start_override_addr == 0) {
1134 ar->hw.app_start_override_addr = address;
1135 from_hw = true;
1136 }
639d0b89 1137
bef26a7f
KV
1138 ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1139 from_hw ? " (from hw)" : "",
6bc36431
KV
1140 ar->hw.app_start_override_addr);
1141
bdcd8170 1142 /* execute the OTP code */
bef26a7f
KV
1143 ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1144 ar->hw.app_start_override_addr);
bdcd8170 1145 param = 0;
bef26a7f 1146 ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
bdcd8170
KV
1147
1148 return ret;
1149}
1150
1151static int ath6kl_upload_firmware(struct ath6kl *ar)
1152{
bdcd8170
KV
1153 u32 address;
1154 int ret;
1155
772c31ee
KV
1156 if (WARN_ON(ar->fw == NULL))
1157 return -ENOENT;
bdcd8170 1158
a01ac414 1159 address = ar->hw.app_load_addr;
bdcd8170 1160
ef548626 1161 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
6bc36431
KV
1162 address, ar->fw_len);
1163
bdcd8170
KV
1164 ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1165
1166 if (ret) {
1167 ath6kl_err("Failed to write firmware: %d\n", ret);
1168 return ret;
1169 }
1170
31024d99
KF
1171 /*
1172 * Set starting address for firmware
1173 * Don't need to setup app_start override addr on AR6004
1174 */
1175 if (ar->target_type != TARGET_TYPE_AR6004) {
a01ac414 1176 address = ar->hw.app_start_override_addr;
31024d99
KF
1177 ath6kl_bmi_set_app_start(ar, address);
1178 }
bdcd8170
KV
1179 return ret;
1180}
1181
1182static int ath6kl_upload_patch(struct ath6kl *ar)
1183{
bdcd8170
KV
1184 u32 address, param;
1185 int ret;
1186
772c31ee
KV
1187 if (WARN_ON(ar->fw_patch == NULL))
1188 return -ENOENT;
bdcd8170 1189
a01ac414 1190 address = ar->hw.dataset_patch_addr;
bdcd8170 1191
ef548626 1192 ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
6bc36431
KV
1193 address, ar->fw_patch_len);
1194
bdcd8170
KV
1195 ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1196 if (ret) {
1197 ath6kl_err("Failed to write patch file: %d\n", ret);
1198 return ret;
1199 }
1200
1201 param = address;
1202 ath6kl_bmi_write(ar,
1203 ath6kl_get_hi_item_addr(ar,
1204 HI_ITEM(hi_dset_list_head)),
1205 (unsigned char *) &param, 4);
1206
1207 return 0;
1208}
1209
1210static int ath6kl_init_upload(struct ath6kl *ar)
1211{
1212 u32 param, options, sleep, address;
1213 int status = 0;
1214
31024d99
KF
1215 if (ar->target_type != TARGET_TYPE_AR6003 &&
1216 ar->target_type != TARGET_TYPE_AR6004)
bdcd8170
KV
1217 return -EINVAL;
1218
1219 /* temporarily disable system sleep */
1220 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1221 status = ath6kl_bmi_reg_read(ar, address, &param);
1222 if (status)
1223 return status;
1224
1225 options = param;
1226
1227 param |= ATH6KL_OPTION_SLEEP_DISABLE;
1228 status = ath6kl_bmi_reg_write(ar, address, param);
1229 if (status)
1230 return status;
1231
1232 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1233 status = ath6kl_bmi_reg_read(ar, address, &param);
1234 if (status)
1235 return status;
1236
1237 sleep = param;
1238
1239 param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1240 status = ath6kl_bmi_reg_write(ar, address, param);
1241 if (status)
1242 return status;
1243
1244 ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1245 options, sleep);
1246
1247 /* program analog PLL register */
31024d99
KF
1248 /* no need to control 40/44MHz clock on AR6004 */
1249 if (ar->target_type != TARGET_TYPE_AR6004) {
1250 status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1251 0xF9104001);
bdcd8170 1252
31024d99
KF
1253 if (status)
1254 return status;
bdcd8170 1255
31024d99
KF
1256 /* Run at 80/88MHz by default */
1257 param = SM(CPU_CLOCK_STANDARD, 1);
1258
1259 address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1260 status = ath6kl_bmi_reg_write(ar, address, param);
1261 if (status)
1262 return status;
1263 }
bdcd8170
KV
1264
1265 param = 0;
1266 address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1267 param = SM(LPO_CAL_ENABLE, 1);
1268 status = ath6kl_bmi_reg_write(ar, address, param);
1269 if (status)
1270 return status;
1271
1272 /* WAR to avoid SDIO CRC err */
1273 if (ar->version.target_ver == AR6003_REV2_VERSION) {
1274 ath6kl_err("temporary war to avoid sdio crc error\n");
1275
1276 param = 0x20;
1277
1278 address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1279 status = ath6kl_bmi_reg_write(ar, address, param);
1280 if (status)
1281 return status;
1282
1283 address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1284 status = ath6kl_bmi_reg_write(ar, address, param);
1285 if (status)
1286 return status;
1287
1288 address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1289 status = ath6kl_bmi_reg_write(ar, address, param);
1290 if (status)
1291 return status;
1292
1293 address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1294 status = ath6kl_bmi_reg_write(ar, address, param);
1295 if (status)
1296 return status;
1297 }
1298
1299 /* write EEPROM data to Target RAM */
1300 status = ath6kl_upload_board_file(ar);
1301 if (status)
1302 return status;
1303
1304 /* transfer One time Programmable data */
1305 status = ath6kl_upload_otp(ar);
1306 if (status)
1307 return status;
1308
1309 /* Download Target firmware */
1310 status = ath6kl_upload_firmware(ar);
1311 if (status)
1312 return status;
1313
1314 status = ath6kl_upload_patch(ar);
1315 if (status)
1316 return status;
1317
1318 /* Restore system sleep */
1319 address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1320 status = ath6kl_bmi_reg_write(ar, address, sleep);
1321 if (status)
1322 return status;
1323
1324 address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1325 param = options | 0x20;
1326 status = ath6kl_bmi_reg_write(ar, address, param);
1327 if (status)
1328 return status;
1329
1330 /* Configure GPIO AR6003 UART */
1331 param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1332 status = ath6kl_bmi_write(ar,
1333 ath6kl_get_hi_item_addr(ar,
1334 HI_ITEM(hi_dbg_uart_txpin)),
1335 (u8 *)&param, 4);
1336
1337 return status;
1338}
1339
a01ac414
KV
1340static int ath6kl_init_hw_params(struct ath6kl *ar)
1341{
1342 switch (ar->version.target_ver) {
1343 case AR6003_REV2_VERSION:
1344 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1345 ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
991b27ea
KV
1346 ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
1347 ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
bef26a7f
KV
1348
1349 /* hw2.0 needs override address hardcoded */
1350 ar->hw.app_start_override_addr = 0x944C00;
1351
a01ac414
KV
1352 break;
1353 case AR6003_REV3_VERSION:
1354 ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
1355 ar->hw.app_load_addr = 0x1234;
991b27ea
KV
1356 ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
1357 ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
a01ac414
KV
1358 break;
1359 case AR6004_REV1_VERSION:
1360 ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
1361 ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
991b27ea
KV
1362 ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
1363 ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
a01ac414
KV
1364 break;
1365 default:
1366 ath6kl_err("Unsupported hardware version: 0x%x\n",
1367 ar->version.target_ver);
1368 return -EINVAL;
1369 }
1370
6bc36431
KV
1371 ath6kl_dbg(ATH6KL_DBG_BOOT,
1372 "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
1373 ar->version.target_ver, ar->target_type,
1374 ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
1375 ath6kl_dbg(ATH6KL_DBG_BOOT,
1376 "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
1377 ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
1378 ar->hw.reserved_ram_size);
1379
a01ac414
KV
1380 return 0;
1381}
1382
521dffcc 1383static int ath6kl_init(struct ath6kl *ar)
bdcd8170 1384{
bdcd8170
KV
1385 int status = 0;
1386 s32 timeleft;
8dafb70e 1387 struct net_device *ndev;
bdcd8170
KV
1388
1389 if (!ar)
1390 return -EIO;
1391
1392 /* Do we need to finish the BMI phase */
1393 if (ath6kl_bmi_done(ar)) {
1394 status = -EIO;
1395 goto ath6kl_init_done;
1396 }
1397
1398 /* Indicate that WMI is enabled (although not ready yet) */
1399 set_bit(WMI_ENABLED, &ar->flag);
2865785e 1400 ar->wmi = ath6kl_wmi_init(ar);
bdcd8170
KV
1401 if (!ar->wmi) {
1402 ath6kl_err("failed to initialize wmi\n");
1403 status = -EIO;
1404 goto ath6kl_init_done;
1405 }
1406
1407 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1408
8dafb70e
VT
1409 status = ath6kl_register_ieee80211_hw(ar);
1410 if (status)
1411 goto err_node_cleanup;
1412
1413 status = ath6kl_debug_init(ar);
1414 if (status) {
1415 wiphy_unregister(ar->wiphy);
1416 goto err_node_cleanup;
1417 }
1418
1419 /* Add an initial station interface */
334234b5 1420 ndev = ath6kl_interface_add(ar, "wlan%d", NL80211_IFTYPE_STATION, 0);
8dafb70e
VT
1421 if (!ndev) {
1422 ath6kl_err("Failed to instantiate a network device\n");
1423 status = -ENOMEM;
1424 wiphy_unregister(ar->wiphy);
1425 goto err_debug_init;
1426 }
1427
1428
1429 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
28ae58dd 1430 __func__, ndev->name, ndev, ar);
8dafb70e 1431
bdcd8170
KV
1432 /*
1433 * The reason we have to wait for the target here is that the
1434 * driver layer has to init BMI in order to set the host block
1435 * size.
1436 */
ad226ec2 1437 if (ath6kl_htc_wait_target(ar->htc_target)) {
bdcd8170 1438 status = -EIO;
8dafb70e 1439 goto err_if_deinit;
bdcd8170
KV
1440 }
1441
1442 if (ath6kl_init_service_ep(ar)) {
1443 status = -EIO;
1444 goto err_cleanup_scatter;
1445 }
1446
1447 /* setup access class priority mappings */
1448 ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
1449 ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1450 ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1451 ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1452
1453 /* give our connected endpoints some buffers */
1454 ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1455 ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1456
1457 /* allocate some buffers that handle larger AMSDU frames */
1458 ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1459
1460 /* setup credit distribution */
1461 ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
1462
1463 ath6kl_cookie_init(ar);
1464
1465 /* start HTC */
ad226ec2 1466 status = ath6kl_htc_start(ar->htc_target);
bdcd8170
KV
1467
1468 if (status) {
1469 ath6kl_cookie_cleanup(ar);
1470 goto err_rxbuf_cleanup;
1471 }
1472
1473 /* Wait for Wmi event to be ready */
1474 timeleft = wait_event_interruptible_timeout(ar->event_wq,
1475 test_bit(WMI_READY,
1476 &ar->flag),
1477 WMI_TIMEOUT);
1478
6bc36431
KV
1479 ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
1480
bdcd8170
KV
1481 if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1482 ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1483 ATH6KL_ABI_VERSION, ar->version.abi_ver);
1484 status = -EIO;
1485 goto err_htc_stop;
1486 }
1487
1488 if (!timeleft || signal_pending(current)) {
1489 ath6kl_err("wmi is not ready or wait was interrupted\n");
1490 status = -EIO;
1491 goto err_htc_stop;
1492 }
1493
1494 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1495
1496 /* communicate the wmi protocol verision to the target */
1497 if ((ath6kl_set_host_app_area(ar)) != 0)
1498 ath6kl_err("unable to set the host app area\n");
1499
1500 ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1501 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1502
be98e3a4
VT
1503 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
1504 WIPHY_FLAG_HAVE_AP_SME;
011a36e1 1505
bdcd8170 1506 status = ath6kl_target_config_wlan_params(ar);
d66ea4f9
VT
1507 if (status)
1508 goto err_htc_stop;
1509
1510 /*
1511 * Set mac address which is received in ready event
1512 * FIXME: Move to ath6kl_interface_add()
1513 */
1514 memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
1515
1516 return status;
bdcd8170
KV
1517
1518err_htc_stop:
ad226ec2 1519 ath6kl_htc_stop(ar->htc_target);
bdcd8170 1520err_rxbuf_cleanup:
ad226ec2 1521 ath6kl_htc_flush_rx_buf(ar->htc_target);
bdcd8170
KV
1522 ath6kl_cleanup_amsdu_rxbufs(ar);
1523err_cleanup_scatter:
1524 ath6kl_hif_cleanup_scatter(ar);
8dafb70e 1525err_if_deinit:
108438bc 1526 ath6kl_deinit_if_data(netdev_priv(ndev));
8dafb70e
VT
1527 wiphy_unregister(ar->wiphy);
1528err_debug_init:
1529 ath6kl_debug_cleanup(ar);
852bd9d9 1530err_node_cleanup:
bdcd8170
KV
1531 ath6kl_wmi_shutdown(ar->wmi);
1532 clear_bit(WMI_ENABLED, &ar->flag);
1533 ar->wmi = NULL;
1534
1535ath6kl_init_done:
1536 return status;
1537}
1538
1539int ath6kl_core_init(struct ath6kl *ar)
1540{
1541 int ret = 0;
1542 struct ath6kl_bmi_target_info targ_info;
1543
1544 ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1545 if (!ar->ath6kl_wq)
1546 return -ENOMEM;
1547
1548 ret = ath6kl_bmi_init(ar);
1549 if (ret)
1550 goto err_wq;
1551
1552 ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1553 if (ret)
1554 goto err_bmi_cleanup;
1555
1556 ar->version.target_ver = le32_to_cpu(targ_info.version);
1557 ar->target_type = le32_to_cpu(targ_info.type);
be98e3a4 1558 ar->wiphy->hw_version = le32_to_cpu(targ_info.version);
bdcd8170 1559
a01ac414
KV
1560 ret = ath6kl_init_hw_params(ar);
1561 if (ret)
1562 goto err_bmi_cleanup;
1563
bdcd8170
KV
1564 ret = ath6kl_configure_target(ar);
1565 if (ret)
1566 goto err_bmi_cleanup;
1567
ad226ec2 1568 ar->htc_target = ath6kl_htc_create(ar);
bdcd8170
KV
1569
1570 if (!ar->htc_target) {
1571 ret = -ENOMEM;
1572 goto err_bmi_cleanup;
1573 }
1574
772c31ee
KV
1575 ret = ath6kl_fetch_firmwares(ar);
1576 if (ret)
1577 goto err_htc_cleanup;
1578
bdcd8170
KV
1579 ret = ath6kl_init_upload(ar);
1580 if (ret)
1581 goto err_htc_cleanup;
1582
521dffcc 1583 ret = ath6kl_init(ar);
bdcd8170
KV
1584 if (ret)
1585 goto err_htc_cleanup;
1586
bdcd8170
KV
1587 return ret;
1588
1589err_htc_cleanup:
ad226ec2 1590 ath6kl_htc_cleanup(ar->htc_target);
bdcd8170
KV
1591err_bmi_cleanup:
1592 ath6kl_bmi_cleanup(ar);
1593err_wq:
1594 destroy_workqueue(ar->ath6kl_wq);
8dafb70e 1595
bdcd8170
KV
1596 return ret;
1597}
1598
6db8fa53
VT
1599static void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
1600{
1601 static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1602 bool discon_issued;
1603
1604 netif_stop_queue(vif->ndev);
1605
1606 clear_bit(WLAN_ENABLED, &vif->flags);
1607
1608 if (wmi_ready) {
1609 discon_issued = test_bit(CONNECTED, &vif->flags) ||
1610 test_bit(CONNECT_PEND, &vif->flags);
1611 ath6kl_disconnect(vif);
1612 del_timer(&vif->disconnect_timer);
1613
1614 if (discon_issued)
1615 ath6kl_disconnect_event(vif, DISCONNECT_CMD,
1616 (vif->nw_type & AP_NETWORK) ?
1617 bcast_mac : vif->bssid,
1618 0, NULL, 0);
1619 }
1620
1621 if (vif->scan_req) {
1622 cfg80211_scan_done(vif->scan_req, true);
1623 vif->scan_req = NULL;
1624 }
1625
1626 ath6kl_deinit_if_data(vif);
1627}
1628
bdcd8170
KV
1629void ath6kl_stop_txrx(struct ath6kl *ar)
1630{
990bd915 1631 struct ath6kl_vif *vif, *tmp_vif;
bdcd8170
KV
1632
1633 set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1634
1635 if (down_interruptible(&ar->sem)) {
1636 ath6kl_err("down_interruptible failed\n");
1637 return;
1638 }
1639
990bd915
VT
1640 spin_lock(&ar->list_lock);
1641 list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1642 list_del(&vif->list);
1643 spin_unlock(&ar->list_lock);
1644 ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
1645 spin_lock(&ar->list_lock);
1646 }
1647 spin_unlock(&ar->list_lock);
bdcd8170 1648
6db8fa53 1649 clear_bit(WMI_READY, &ar->flag);
bdcd8170 1650
6db8fa53
VT
1651 /*
1652 * After wmi_shudown all WMI events will be dropped. We
1653 * need to cleanup the buffers allocated in AP mode and
1654 * give disconnect notification to stack, which usually
1655 * happens in the disconnect_event. Simulate the disconnect
1656 * event by calling the function directly. Sometimes
1657 * disconnect_event will be received when the debug logs
1658 * are collected.
1659 */
1660 ath6kl_wmi_shutdown(ar->wmi);
bdcd8170 1661
6db8fa53
VT
1662 clear_bit(WMI_ENABLED, &ar->flag);
1663 if (ar->htc_target) {
1664 ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
1665 ath6kl_htc_stop(ar->htc_target);
bdcd8170
KV
1666 }
1667
6db8fa53
VT
1668 /*
1669 * Try to reset the device if we can. The driver may have been
1670 * configure NOT to reset the target during a debug session.
1671 */
1672 ath6kl_dbg(ATH6KL_DBG_TRC,
1673 "attempting to reset target on instance destroy\n");
1674 ath6kl_reset_device(ar, ar->target_type, true, true);
19703573 1675
6db8fa53 1676 clear_bit(WLAN_ENABLED, &ar->flag);
bdcd8170 1677}
This page took 0.128679 seconds and 5 git commands to generate.