init.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. /*
  2. * Copyright (c) 2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2012 Qualcomm Atheros, 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. #include <linux/moduleparam.h>
  18. #include <linux/errno.h>
  19. #include <linux/export.h>
  20. #include <linux/of.h>
  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. static const struct ath6kl_hw hw_list[] = {
  28. {
  29. .id = AR6003_HW_2_0_VERSION,
  30. .name = "ar6003 hw 2.0",
  31. .dataset_patch_addr = 0x57e884,
  32. .app_load_addr = 0x543180,
  33. .board_ext_data_addr = 0x57e500,
  34. .reserved_ram_size = 6912,
  35. .refclk_hz = 26000000,
  36. .uarttx_pin = 8,
  37. /* hw2.0 needs override address hardcoded */
  38. .app_start_override_addr = 0x944C00,
  39. .fw = {
  40. .dir = AR6003_HW_2_0_FW_DIR,
  41. .otp = AR6003_HW_2_0_OTP_FILE,
  42. .fw = AR6003_HW_2_0_FIRMWARE_FILE,
  43. .tcmd = AR6003_HW_2_0_TCMD_FIRMWARE_FILE,
  44. .patch = AR6003_HW_2_0_PATCH_FILE,
  45. },
  46. .fw_board = AR6003_HW_2_0_BOARD_DATA_FILE,
  47. .fw_default_board = AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE,
  48. },
  49. {
  50. .id = AR6003_HW_2_1_1_VERSION,
  51. .name = "ar6003 hw 2.1.1",
  52. .dataset_patch_addr = 0x57ff74,
  53. .app_load_addr = 0x1234,
  54. .board_ext_data_addr = 0x542330,
  55. .reserved_ram_size = 512,
  56. .refclk_hz = 26000000,
  57. .uarttx_pin = 8,
  58. .testscript_addr = 0x57ef74,
  59. .fw = {
  60. .dir = AR6003_HW_2_1_1_FW_DIR,
  61. .otp = AR6003_HW_2_1_1_OTP_FILE,
  62. .fw = AR6003_HW_2_1_1_FIRMWARE_FILE,
  63. .tcmd = AR6003_HW_2_1_1_TCMD_FIRMWARE_FILE,
  64. .patch = AR6003_HW_2_1_1_PATCH_FILE,
  65. .utf = AR6003_HW_2_1_1_UTF_FIRMWARE_FILE,
  66. .testscript = AR6003_HW_2_1_1_TESTSCRIPT_FILE,
  67. },
  68. .fw_board = AR6003_HW_2_1_1_BOARD_DATA_FILE,
  69. .fw_default_board = AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE,
  70. },
  71. {
  72. .id = AR6004_HW_1_0_VERSION,
  73. .name = "ar6004 hw 1.0",
  74. .dataset_patch_addr = 0x57e884,
  75. .app_load_addr = 0x1234,
  76. .board_ext_data_addr = 0x437000,
  77. .reserved_ram_size = 19456,
  78. .board_addr = 0x433900,
  79. .refclk_hz = 26000000,
  80. .uarttx_pin = 11,
  81. .fw = {
  82. .dir = AR6004_HW_1_0_FW_DIR,
  83. .fw = AR6004_HW_1_0_FIRMWARE_FILE,
  84. },
  85. .fw_board = AR6004_HW_1_0_BOARD_DATA_FILE,
  86. .fw_default_board = AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE,
  87. },
  88. {
  89. .id = AR6004_HW_1_1_VERSION,
  90. .name = "ar6004 hw 1.1",
  91. .dataset_patch_addr = 0x57e884,
  92. .app_load_addr = 0x1234,
  93. .board_ext_data_addr = 0x437000,
  94. .reserved_ram_size = 11264,
  95. .board_addr = 0x43d400,
  96. .refclk_hz = 40000000,
  97. .uarttx_pin = 11,
  98. .fw = {
  99. .dir = AR6004_HW_1_1_FW_DIR,
  100. .fw = AR6004_HW_1_1_FIRMWARE_FILE,
  101. },
  102. .fw_board = AR6004_HW_1_1_BOARD_DATA_FILE,
  103. .fw_default_board = AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE,
  104. },
  105. };
  106. /*
  107. * Include definitions here that can be used to tune the WLAN module
  108. * behavior. Different customers can tune the behavior as per their needs,
  109. * here.
  110. */
  111. /*
  112. * This configuration item enable/disable keepalive support.
  113. * Keepalive support: In the absence of any data traffic to AP, null
  114. * frames will be sent to the AP at periodic interval, to keep the association
  115. * active. This configuration item defines the periodic interval.
  116. * Use value of zero to disable keepalive support
  117. * Default: 60 seconds
  118. */
  119. #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
  120. /*
  121. * This configuration item sets the value of disconnect timeout
  122. * Firmware delays sending the disconnec event to the host for this
  123. * timeout after is gets disconnected from the current AP.
  124. * If the firmware successly roams within the disconnect timeout
  125. * it sends a new connect event
  126. */
  127. #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
  128. #define ATH6KL_DATA_OFFSET 64
  129. struct sk_buff *ath6kl_buf_alloc(int size)
  130. {
  131. struct sk_buff *skb;
  132. u16 reserved;
  133. /* Add chacheline space at front and back of buffer */
  134. reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
  135. sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
  136. skb = dev_alloc_skb(size + reserved);
  137. if (skb)
  138. skb_reserve(skb, reserved - L1_CACHE_BYTES);
  139. return skb;
  140. }
  141. void ath6kl_init_profile_info(struct ath6kl_vif *vif)
  142. {
  143. vif->ssid_len = 0;
  144. memset(vif->ssid, 0, sizeof(vif->ssid));
  145. vif->dot11_auth_mode = OPEN_AUTH;
  146. vif->auth_mode = NONE_AUTH;
  147. vif->prwise_crypto = NONE_CRYPT;
  148. vif->prwise_crypto_len = 0;
  149. vif->grp_crypto = NONE_CRYPT;
  150. vif->grp_crypto_len = 0;
  151. memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
  152. memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
  153. memset(vif->bssid, 0, sizeof(vif->bssid));
  154. vif->bss_ch = 0;
  155. }
  156. static int ath6kl_set_host_app_area(struct ath6kl *ar)
  157. {
  158. u32 address, data;
  159. struct host_app_area host_app_area;
  160. /* Fetch the address of the host_app_area_s
  161. * instance in the host interest area */
  162. address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
  163. address = TARG_VTOP(ar->target_type, address);
  164. if (ath6kl_diag_read32(ar, address, &data))
  165. return -EIO;
  166. address = TARG_VTOP(ar->target_type, data);
  167. host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
  168. if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
  169. sizeof(struct host_app_area)))
  170. return -EIO;
  171. return 0;
  172. }
  173. static inline void set_ac2_ep_map(struct ath6kl *ar,
  174. u8 ac,
  175. enum htc_endpoint_id ep)
  176. {
  177. ar->ac2ep_map[ac] = ep;
  178. ar->ep2ac_map[ep] = ac;
  179. }
  180. /* connect to a service */
  181. static int ath6kl_connectservice(struct ath6kl *ar,
  182. struct htc_service_connect_req *con_req,
  183. char *desc)
  184. {
  185. int status;
  186. struct htc_service_connect_resp response;
  187. memset(&response, 0, sizeof(response));
  188. status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
  189. if (status) {
  190. ath6kl_err("failed to connect to %s service status:%d\n",
  191. desc, status);
  192. return status;
  193. }
  194. switch (con_req->svc_id) {
  195. case WMI_CONTROL_SVC:
  196. if (test_bit(WMI_ENABLED, &ar->flag))
  197. ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
  198. ar->ctrl_ep = response.endpoint;
  199. break;
  200. case WMI_DATA_BE_SVC:
  201. set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
  202. break;
  203. case WMI_DATA_BK_SVC:
  204. set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
  205. break;
  206. case WMI_DATA_VI_SVC:
  207. set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
  208. break;
  209. case WMI_DATA_VO_SVC:
  210. set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
  211. break;
  212. default:
  213. ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
  214. return -EINVAL;
  215. }
  216. return 0;
  217. }
  218. static int ath6kl_init_service_ep(struct ath6kl *ar)
  219. {
  220. struct htc_service_connect_req connect;
  221. memset(&connect, 0, sizeof(connect));
  222. /* these fields are the same for all service endpoints */
  223. connect.ep_cb.rx = ath6kl_rx;
  224. connect.ep_cb.rx_refill = ath6kl_rx_refill;
  225. connect.ep_cb.tx_full = ath6kl_tx_queue_full;
  226. /*
  227. * Set the max queue depth so that our ath6kl_tx_queue_full handler
  228. * gets called.
  229. */
  230. connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
  231. connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
  232. if (!connect.ep_cb.rx_refill_thresh)
  233. connect.ep_cb.rx_refill_thresh++;
  234. /* connect to control service */
  235. connect.svc_id = WMI_CONTROL_SVC;
  236. if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
  237. return -EIO;
  238. connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
  239. /*
  240. * Limit the HTC message size on the send path, although e can
  241. * receive A-MSDU frames of 4K, we will only send ethernet-sized
  242. * (802.3) frames on the send path.
  243. */
  244. connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
  245. /*
  246. * To reduce the amount of committed memory for larger A_MSDU
  247. * frames, use the recv-alloc threshold mechanism for larger
  248. * packets.
  249. */
  250. connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
  251. connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
  252. /*
  253. * For the remaining data services set the connection flag to
  254. * reduce dribbling, if configured to do so.
  255. */
  256. connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
  257. connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
  258. connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
  259. connect.svc_id = WMI_DATA_BE_SVC;
  260. if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
  261. return -EIO;
  262. /* connect to back-ground map this to WMI LOW_PRI */
  263. connect.svc_id = WMI_DATA_BK_SVC;
  264. if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
  265. return -EIO;
  266. /* connect to Video service, map this to to HI PRI */
  267. connect.svc_id = WMI_DATA_VI_SVC;
  268. if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
  269. return -EIO;
  270. /*
  271. * Connect to VO service, this is currently not mapped to a WMI
  272. * priority stream due to historical reasons. WMI originally
  273. * defined 3 priorities over 3 mailboxes We can change this when
  274. * WMI is reworked so that priorities are not dependent on
  275. * mailboxes.
  276. */
  277. connect.svc_id = WMI_DATA_VO_SVC;
  278. if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
  279. return -EIO;
  280. return 0;
  281. }
  282. void ath6kl_init_control_info(struct ath6kl_vif *vif)
  283. {
  284. ath6kl_init_profile_info(vif);
  285. vif->def_txkey_index = 0;
  286. memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
  287. vif->ch_hint = 0;
  288. }
  289. /*
  290. * Set HTC/Mbox operational parameters, this can only be called when the
  291. * target is in the BMI phase.
  292. */
  293. static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
  294. u8 htc_ctrl_buf)
  295. {
  296. int status;
  297. u32 blk_size;
  298. blk_size = ar->mbox_info.block_size;
  299. if (htc_ctrl_buf)
  300. blk_size |= ((u32)htc_ctrl_buf) << 16;
  301. /* set the host interest area for the block size */
  302. status = ath6kl_bmi_write_hi32(ar, hi_mbox_io_block_sz, blk_size);
  303. if (status) {
  304. ath6kl_err("bmi_write_memory for IO block size failed\n");
  305. goto out;
  306. }
  307. ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
  308. blk_size,
  309. ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
  310. if (mbox_isr_yield_val) {
  311. /* set the host interest area for the mbox ISR yield limit */
  312. status = ath6kl_bmi_write_hi32(ar, hi_mbox_isr_yield_limit,
  313. mbox_isr_yield_val);
  314. if (status) {
  315. ath6kl_err("bmi_write_memory for yield limit failed\n");
  316. goto out;
  317. }
  318. }
  319. out:
  320. return status;
  321. }
  322. static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
  323. {
  324. int status = 0;
  325. int ret;
  326. /*
  327. * Configure the device for rx dot11 header rules. "0,0" are the
  328. * default values. Required if checksum offload is needed. Set
  329. * RxMetaVersion to 2.
  330. */
  331. if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
  332. ar->rx_meta_ver, 0, 0)) {
  333. ath6kl_err("unable to set the rx frame format\n");
  334. status = -EIO;
  335. }
  336. if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
  337. if ((ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
  338. IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
  339. ath6kl_err("unable to set power save fail event policy\n");
  340. status = -EIO;
  341. }
  342. if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
  343. if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
  344. WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
  345. ath6kl_err("unable to set barker preamble policy\n");
  346. status = -EIO;
  347. }
  348. if (ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
  349. WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
  350. ath6kl_err("unable to set keep alive interval\n");
  351. status = -EIO;
  352. }
  353. if (ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
  354. WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
  355. ath6kl_err("unable to set disconnect timeout\n");
  356. status = -EIO;
  357. }
  358. if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
  359. if (ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED)) {
  360. ath6kl_err("unable to set txop bursting\n");
  361. status = -EIO;
  362. }
  363. if (ar->p2p && (ar->vif_max == 1 || idx)) {
  364. ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
  365. P2P_FLAG_CAPABILITIES_REQ |
  366. P2P_FLAG_MACADDR_REQ |
  367. P2P_FLAG_HMODEL_REQ);
  368. if (ret) {
  369. ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
  370. "capabilities (%d) - assuming P2P not "
  371. "supported\n", ret);
  372. ar->p2p = false;
  373. }
  374. }
  375. if (ar->p2p && (ar->vif_max == 1 || idx)) {
  376. /* Enable Probe Request reporting for P2P */
  377. ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
  378. if (ret) {
  379. ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
  380. "Request reporting (%d)\n", ret);
  381. }
  382. }
  383. return status;
  384. }
  385. int ath6kl_configure_target(struct ath6kl *ar)
  386. {
  387. u32 param, ram_reserved_size;
  388. u8 fw_iftype, fw_mode = 0, fw_submode = 0;
  389. int i, status;
  390. param = !!(ar->conf_flags & ATH6KL_CONF_UART_DEBUG);
  391. if (ath6kl_bmi_write_hi32(ar, hi_serial_enable, param)) {
  392. ath6kl_err("bmi_write_memory for uart debug failed\n");
  393. return -EIO;
  394. }
  395. /*
  396. * Note: Even though the firmware interface type is
  397. * chosen as BSS_STA for all three interfaces, can
  398. * be configured to IBSS/AP as long as the fw submode
  399. * remains normal mode (0 - AP, STA and IBSS). But
  400. * due to an target assert in firmware only one interface is
  401. * configured for now.
  402. */
  403. fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
  404. for (i = 0; i < ar->vif_max; i++)
  405. fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
  406. /*
  407. * By default, submodes :
  408. * vif[0] - AP/STA/IBSS
  409. * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
  410. * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
  411. */
  412. for (i = 0; i < ar->max_norm_iface; i++)
  413. fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
  414. (i * HI_OPTION_FW_SUBMODE_BITS);
  415. for (i = ar->max_norm_iface; i < ar->vif_max; i++)
  416. fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
  417. (i * HI_OPTION_FW_SUBMODE_BITS);
  418. if (ar->p2p && ar->vif_max == 1)
  419. fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
  420. if (ath6kl_bmi_write_hi32(ar, hi_app_host_interest,
  421. HTC_PROTOCOL_VERSION) != 0) {
  422. ath6kl_err("bmi_write_memory for htc version failed\n");
  423. return -EIO;
  424. }
  425. /* set the firmware mode to STA/IBSS/AP */
  426. param = 0;
  427. if (ath6kl_bmi_read(ar,
  428. ath6kl_get_hi_item_addr(ar,
  429. HI_ITEM(hi_option_flag)),
  430. (u8 *)&param, 4) != 0) {
  431. ath6kl_err("bmi_read_memory for setting fwmode failed\n");
  432. return -EIO;
  433. }
  434. param |= (ar->vif_max << HI_OPTION_NUM_DEV_SHIFT);
  435. param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
  436. param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
  437. param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
  438. param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
  439. if (ath6kl_bmi_write_hi32(ar, hi_option_flag, param) != 0) {
  440. ath6kl_err("bmi_write_memory for setting fwmode failed\n");
  441. return -EIO;
  442. }
  443. ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
  444. /*
  445. * Hardcode the address use for the extended board data
  446. * Ideally this should be pre-allocate by the OS at boot time
  447. * But since it is a new feature and board data is loaded
  448. * at init time, we have to workaround this from host.
  449. * It is difficult to patch the firmware boot code,
  450. * but possible in theory.
  451. */
  452. param = ar->hw.board_ext_data_addr;
  453. ram_reserved_size = ar->hw.reserved_ram_size;
  454. if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
  455. ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
  456. return -EIO;
  457. }
  458. if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
  459. ram_reserved_size) != 0) {
  460. ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
  461. return -EIO;
  462. }
  463. /* set the block size for the target */
  464. if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
  465. /* use default number of control buffers */
  466. return -EIO;
  467. /* Configure GPIO AR600x UART */
  468. status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
  469. ar->hw.uarttx_pin);
  470. if (status)
  471. return status;
  472. /* Configure target refclk_hz */
  473. status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz, ar->hw.refclk_hz);
  474. if (status)
  475. return status;
  476. return 0;
  477. }
  478. /* firmware upload */
  479. static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
  480. u8 **fw, size_t *fw_len)
  481. {
  482. const struct firmware *fw_entry;
  483. int ret;
  484. ret = request_firmware(&fw_entry, filename, ar->dev);
  485. if (ret)
  486. return ret;
  487. *fw_len = fw_entry->size;
  488. *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
  489. if (*fw == NULL)
  490. ret = -ENOMEM;
  491. release_firmware(fw_entry);
  492. return ret;
  493. }
  494. #ifdef CONFIG_OF
  495. /*
  496. * Check the device tree for a board-id and use it to construct
  497. * the pathname to the firmware file. Used (for now) to find a
  498. * fallback to the "bdata.bin" file--typically a symlink to the
  499. * appropriate board-specific file.
  500. */
  501. static bool check_device_tree(struct ath6kl *ar)
  502. {
  503. static const char *board_id_prop = "atheros,board-id";
  504. struct device_node *node;
  505. char board_filename[64];
  506. const char *board_id;
  507. int ret;
  508. for_each_compatible_node(node, NULL, "atheros,ath6kl") {
  509. board_id = of_get_property(node, board_id_prop, NULL);
  510. if (board_id == NULL) {
  511. ath6kl_warn("No \"%s\" property on %s node.\n",
  512. board_id_prop, node->name);
  513. continue;
  514. }
  515. snprintf(board_filename, sizeof(board_filename),
  516. "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
  517. ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
  518. &ar->fw_board_len);
  519. if (ret) {
  520. ath6kl_err("Failed to get DT board file %s: %d\n",
  521. board_filename, ret);
  522. continue;
  523. }
  524. return true;
  525. }
  526. return false;
  527. }
  528. #else
  529. static bool check_device_tree(struct ath6kl *ar)
  530. {
  531. return false;
  532. }
  533. #endif /* CONFIG_OF */
  534. static int ath6kl_fetch_board_file(struct ath6kl *ar)
  535. {
  536. const char *filename;
  537. int ret;
  538. if (ar->fw_board != NULL)
  539. return 0;
  540. if (WARN_ON(ar->hw.fw_board == NULL))
  541. return -EINVAL;
  542. filename = ar->hw.fw_board;
  543. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  544. &ar->fw_board_len);
  545. if (ret == 0) {
  546. /* managed to get proper board file */
  547. return 0;
  548. }
  549. if (check_device_tree(ar)) {
  550. /* got board file from device tree */
  551. return 0;
  552. }
  553. /* there was no proper board file, try to use default instead */
  554. ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
  555. filename, ret);
  556. filename = ar->hw.fw_default_board;
  557. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  558. &ar->fw_board_len);
  559. if (ret) {
  560. ath6kl_err("Failed to get default board file %s: %d\n",
  561. filename, ret);
  562. return ret;
  563. }
  564. ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
  565. ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
  566. return 0;
  567. }
  568. static int ath6kl_fetch_otp_file(struct ath6kl *ar)
  569. {
  570. char filename[100];
  571. int ret;
  572. if (ar->fw_otp != NULL)
  573. return 0;
  574. if (ar->hw.fw.otp == NULL) {
  575. ath6kl_dbg(ATH6KL_DBG_BOOT,
  576. "no OTP file configured for this hw\n");
  577. return 0;
  578. }
  579. snprintf(filename, sizeof(filename), "%s/%s",
  580. ar->hw.fw.dir, ar->hw.fw.otp);
  581. ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
  582. &ar->fw_otp_len);
  583. if (ret) {
  584. ath6kl_err("Failed to get OTP file %s: %d\n",
  585. filename, ret);
  586. return ret;
  587. }
  588. return 0;
  589. }
  590. static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
  591. {
  592. char filename[100];
  593. int ret;
  594. if (ar->testmode == 0)
  595. return 0;
  596. ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
  597. if (ar->testmode == 2) {
  598. if (ar->hw.fw.utf == NULL) {
  599. ath6kl_warn("testmode 2 not supported\n");
  600. return -EOPNOTSUPP;
  601. }
  602. snprintf(filename, sizeof(filename), "%s/%s",
  603. ar->hw.fw.dir, ar->hw.fw.utf);
  604. } else {
  605. if (ar->hw.fw.tcmd == NULL) {
  606. ath6kl_warn("testmode 1 not supported\n");
  607. return -EOPNOTSUPP;
  608. }
  609. snprintf(filename, sizeof(filename), "%s/%s",
  610. ar->hw.fw.dir, ar->hw.fw.tcmd);
  611. }
  612. set_bit(TESTMODE, &ar->flag);
  613. ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
  614. if (ret) {
  615. ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
  616. ar->testmode, filename, ret);
  617. return ret;
  618. }
  619. return 0;
  620. }
  621. static int ath6kl_fetch_fw_file(struct ath6kl *ar)
  622. {
  623. char filename[100];
  624. int ret;
  625. if (ar->fw != NULL)
  626. return 0;
  627. /* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
  628. if (WARN_ON(ar->hw.fw.fw == NULL))
  629. return -EINVAL;
  630. snprintf(filename, sizeof(filename), "%s/%s",
  631. ar->hw.fw.dir, ar->hw.fw.fw);
  632. ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
  633. if (ret) {
  634. ath6kl_err("Failed to get firmware file %s: %d\n",
  635. filename, ret);
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. static int ath6kl_fetch_patch_file(struct ath6kl *ar)
  641. {
  642. char filename[100];
  643. int ret;
  644. if (ar->fw_patch != NULL)
  645. return 0;
  646. if (ar->hw.fw.patch == NULL)
  647. return 0;
  648. snprintf(filename, sizeof(filename), "%s/%s",
  649. ar->hw.fw.dir, ar->hw.fw.patch);
  650. ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
  651. &ar->fw_patch_len);
  652. if (ret) {
  653. ath6kl_err("Failed to get patch file %s: %d\n",
  654. filename, ret);
  655. return ret;
  656. }
  657. return 0;
  658. }
  659. static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
  660. {
  661. char filename[100];
  662. int ret;
  663. if (ar->testmode != 2)
  664. return 0;
  665. if (ar->fw_testscript != NULL)
  666. return 0;
  667. if (ar->hw.fw.testscript == NULL)
  668. return 0;
  669. snprintf(filename, sizeof(filename), "%s/%s",
  670. ar->hw.fw.dir, ar->hw.fw.testscript);
  671. ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
  672. &ar->fw_testscript_len);
  673. if (ret) {
  674. ath6kl_err("Failed to get testscript file %s: %d\n",
  675. filename, ret);
  676. return ret;
  677. }
  678. return 0;
  679. }
  680. static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
  681. {
  682. int ret;
  683. ret = ath6kl_fetch_otp_file(ar);
  684. if (ret)
  685. return ret;
  686. ret = ath6kl_fetch_fw_file(ar);
  687. if (ret)
  688. return ret;
  689. ret = ath6kl_fetch_patch_file(ar);
  690. if (ret)
  691. return ret;
  692. ret = ath6kl_fetch_testscript_file(ar);
  693. if (ret)
  694. return ret;
  695. return 0;
  696. }
  697. static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
  698. {
  699. size_t magic_len, len, ie_len;
  700. const struct firmware *fw;
  701. struct ath6kl_fw_ie *hdr;
  702. char filename[100];
  703. const u8 *data;
  704. int ret, ie_id, i, index, bit;
  705. __le32 *val;
  706. snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
  707. ret = request_firmware(&fw, filename, ar->dev);
  708. if (ret)
  709. return ret;
  710. data = fw->data;
  711. len = fw->size;
  712. /* magic also includes the null byte, check that as well */
  713. magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
  714. if (len < magic_len) {
  715. ret = -EINVAL;
  716. goto out;
  717. }
  718. if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
  719. ret = -EINVAL;
  720. goto out;
  721. }
  722. len -= magic_len;
  723. data += magic_len;
  724. /* loop elements */
  725. while (len > sizeof(struct ath6kl_fw_ie)) {
  726. /* hdr is unaligned! */
  727. hdr = (struct ath6kl_fw_ie *) data;
  728. ie_id = le32_to_cpup(&hdr->id);
  729. ie_len = le32_to_cpup(&hdr->len);
  730. len -= sizeof(*hdr);
  731. data += sizeof(*hdr);
  732. if (len < ie_len) {
  733. ret = -EINVAL;
  734. goto out;
  735. }
  736. switch (ie_id) {
  737. case ATH6KL_FW_IE_OTP_IMAGE:
  738. ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
  739. ie_len);
  740. ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
  741. if (ar->fw_otp == NULL) {
  742. ret = -ENOMEM;
  743. goto out;
  744. }
  745. ar->fw_otp_len = ie_len;
  746. break;
  747. case ATH6KL_FW_IE_FW_IMAGE:
  748. ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
  749. ie_len);
  750. /* in testmode we already might have a fw file */
  751. if (ar->fw != NULL)
  752. break;
  753. ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
  754. if (ar->fw == NULL) {
  755. ret = -ENOMEM;
  756. goto out;
  757. }
  758. ar->fw_len = ie_len;
  759. break;
  760. case ATH6KL_FW_IE_PATCH_IMAGE:
  761. ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
  762. ie_len);
  763. ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
  764. if (ar->fw_patch == NULL) {
  765. ret = -ENOMEM;
  766. goto out;
  767. }
  768. ar->fw_patch_len = ie_len;
  769. break;
  770. case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
  771. val = (__le32 *) data;
  772. ar->hw.reserved_ram_size = le32_to_cpup(val);
  773. ath6kl_dbg(ATH6KL_DBG_BOOT,
  774. "found reserved ram size ie 0x%d\n",
  775. ar->hw.reserved_ram_size);
  776. break;
  777. case ATH6KL_FW_IE_CAPABILITIES:
  778. if (ie_len < DIV_ROUND_UP(ATH6KL_FW_CAPABILITY_MAX, 8))
  779. break;
  780. ath6kl_dbg(ATH6KL_DBG_BOOT,
  781. "found firmware capabilities ie (%zd B)\n",
  782. ie_len);
  783. for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
  784. index = i / 8;
  785. bit = i % 8;
  786. if (data[index] & (1 << bit))
  787. __set_bit(i, ar->fw_capabilities);
  788. }
  789. ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
  790. ar->fw_capabilities,
  791. sizeof(ar->fw_capabilities));
  792. break;
  793. case ATH6KL_FW_IE_PATCH_ADDR:
  794. if (ie_len != sizeof(*val))
  795. break;
  796. val = (__le32 *) data;
  797. ar->hw.dataset_patch_addr = le32_to_cpup(val);
  798. ath6kl_dbg(ATH6KL_DBG_BOOT,
  799. "found patch address ie 0x%x\n",
  800. ar->hw.dataset_patch_addr);
  801. break;
  802. case ATH6KL_FW_IE_BOARD_ADDR:
  803. if (ie_len != sizeof(*val))
  804. break;
  805. val = (__le32 *) data;
  806. ar->hw.board_addr = le32_to_cpup(val);
  807. ath6kl_dbg(ATH6KL_DBG_BOOT,
  808. "found board address ie 0x%x\n",
  809. ar->hw.board_addr);
  810. break;
  811. case ATH6KL_FW_IE_VIF_MAX:
  812. if (ie_len != sizeof(*val))
  813. break;
  814. val = (__le32 *) data;
  815. ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
  816. ATH6KL_VIF_MAX);
  817. if (ar->vif_max > 1 && !ar->p2p)
  818. ar->max_norm_iface = 2;
  819. ath6kl_dbg(ATH6KL_DBG_BOOT,
  820. "found vif max ie %d\n", ar->vif_max);
  821. break;
  822. default:
  823. ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
  824. le32_to_cpup(&hdr->id));
  825. break;
  826. }
  827. len -= ie_len;
  828. data += ie_len;
  829. };
  830. ret = 0;
  831. out:
  832. release_firmware(fw);
  833. return ret;
  834. }
  835. int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
  836. {
  837. int ret;
  838. ret = ath6kl_fetch_board_file(ar);
  839. if (ret)
  840. return ret;
  841. ret = ath6kl_fetch_testmode_file(ar);
  842. if (ret)
  843. return ret;
  844. ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
  845. if (ret == 0) {
  846. ar->fw_api = 3;
  847. goto out;
  848. }
  849. ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
  850. if (ret == 0) {
  851. ar->fw_api = 2;
  852. goto out;
  853. }
  854. ret = ath6kl_fetch_fw_api1(ar);
  855. if (ret)
  856. return ret;
  857. ar->fw_api = 1;
  858. out:
  859. ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
  860. return 0;
  861. }
  862. static int ath6kl_upload_board_file(struct ath6kl *ar)
  863. {
  864. u32 board_address, board_ext_address, param;
  865. u32 board_data_size, board_ext_data_size;
  866. int ret;
  867. if (WARN_ON(ar->fw_board == NULL))
  868. return -ENOENT;
  869. /*
  870. * Determine where in Target RAM to write Board Data.
  871. * For AR6004, host determine Target RAM address for
  872. * writing board data.
  873. */
  874. if (ar->hw.board_addr != 0) {
  875. ath6kl_bmi_write_hi32(ar, hi_board_data,
  876. ar->hw.board_addr);
  877. } else {
  878. ath6kl_bmi_read(ar,
  879. ath6kl_get_hi_item_addr(ar,
  880. HI_ITEM(hi_board_data)),
  881. (u8 *) &board_address, 4);
  882. }
  883. /* determine where in target ram to write extended board data */
  884. ath6kl_bmi_read(ar,
  885. ath6kl_get_hi_item_addr(ar,
  886. HI_ITEM(hi_board_ext_data)),
  887. (u8 *) &board_ext_address, 4);
  888. if (ar->target_type == TARGET_TYPE_AR6003 &&
  889. board_ext_address == 0) {
  890. ath6kl_err("Failed to get board file target address.\n");
  891. return -EINVAL;
  892. }
  893. switch (ar->target_type) {
  894. case TARGET_TYPE_AR6003:
  895. board_data_size = AR6003_BOARD_DATA_SZ;
  896. board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
  897. if (ar->fw_board_len > (board_data_size + board_ext_data_size))
  898. board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
  899. break;
  900. case TARGET_TYPE_AR6004:
  901. board_data_size = AR6004_BOARD_DATA_SZ;
  902. board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
  903. break;
  904. default:
  905. WARN_ON(1);
  906. return -EINVAL;
  907. break;
  908. }
  909. if (board_ext_address &&
  910. ar->fw_board_len == (board_data_size + board_ext_data_size)) {
  911. /* write extended board data */
  912. ath6kl_dbg(ATH6KL_DBG_BOOT,
  913. "writing extended board data to 0x%x (%d B)\n",
  914. board_ext_address, board_ext_data_size);
  915. ret = ath6kl_bmi_write(ar, board_ext_address,
  916. ar->fw_board + board_data_size,
  917. board_ext_data_size);
  918. if (ret) {
  919. ath6kl_err("Failed to write extended board data: %d\n",
  920. ret);
  921. return ret;
  922. }
  923. /* record that extended board data is initialized */
  924. param = (board_ext_data_size << 16) | 1;
  925. ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
  926. }
  927. if (ar->fw_board_len < board_data_size) {
  928. ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
  929. ret = -EINVAL;
  930. return ret;
  931. }
  932. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
  933. board_address, board_data_size);
  934. ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
  935. board_data_size);
  936. if (ret) {
  937. ath6kl_err("Board file bmi write failed: %d\n", ret);
  938. return ret;
  939. }
  940. /* record the fact that Board Data IS initialized */
  941. ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, 1);
  942. return ret;
  943. }
  944. static int ath6kl_upload_otp(struct ath6kl *ar)
  945. {
  946. u32 address, param;
  947. bool from_hw = false;
  948. int ret;
  949. if (ar->fw_otp == NULL)
  950. return 0;
  951. address = ar->hw.app_load_addr;
  952. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
  953. ar->fw_otp_len);
  954. ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
  955. ar->fw_otp_len);
  956. if (ret) {
  957. ath6kl_err("Failed to upload OTP file: %d\n", ret);
  958. return ret;
  959. }
  960. /* read firmware start address */
  961. ret = ath6kl_bmi_read(ar,
  962. ath6kl_get_hi_item_addr(ar,
  963. HI_ITEM(hi_app_start)),
  964. (u8 *) &address, sizeof(address));
  965. if (ret) {
  966. ath6kl_err("Failed to read hi_app_start: %d\n", ret);
  967. return ret;
  968. }
  969. if (ar->hw.app_start_override_addr == 0) {
  970. ar->hw.app_start_override_addr = address;
  971. from_hw = true;
  972. }
  973. ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
  974. from_hw ? " (from hw)" : "",
  975. ar->hw.app_start_override_addr);
  976. /* execute the OTP code */
  977. ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
  978. ar->hw.app_start_override_addr);
  979. param = 0;
  980. ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
  981. return ret;
  982. }
  983. static int ath6kl_upload_firmware(struct ath6kl *ar)
  984. {
  985. u32 address;
  986. int ret;
  987. if (WARN_ON(ar->fw == NULL))
  988. return 0;
  989. address = ar->hw.app_load_addr;
  990. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
  991. address, ar->fw_len);
  992. ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
  993. if (ret) {
  994. ath6kl_err("Failed to write firmware: %d\n", ret);
  995. return ret;
  996. }
  997. /*
  998. * Set starting address for firmware
  999. * Don't need to setup app_start override addr on AR6004
  1000. */
  1001. if (ar->target_type != TARGET_TYPE_AR6004) {
  1002. address = ar->hw.app_start_override_addr;
  1003. ath6kl_bmi_set_app_start(ar, address);
  1004. }
  1005. return ret;
  1006. }
  1007. static int ath6kl_upload_patch(struct ath6kl *ar)
  1008. {
  1009. u32 address;
  1010. int ret;
  1011. if (ar->fw_patch == NULL)
  1012. return 0;
  1013. address = ar->hw.dataset_patch_addr;
  1014. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
  1015. address, ar->fw_patch_len);
  1016. ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
  1017. if (ret) {
  1018. ath6kl_err("Failed to write patch file: %d\n", ret);
  1019. return ret;
  1020. }
  1021. ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
  1022. return 0;
  1023. }
  1024. static int ath6kl_upload_testscript(struct ath6kl *ar)
  1025. {
  1026. u32 address;
  1027. int ret;
  1028. if (ar->testmode != 2)
  1029. return 0;
  1030. if (ar->fw_testscript == NULL)
  1031. return 0;
  1032. address = ar->hw.testscript_addr;
  1033. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
  1034. address, ar->fw_testscript_len);
  1035. ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
  1036. ar->fw_testscript_len);
  1037. if (ret) {
  1038. ath6kl_err("Failed to write testscript file: %d\n", ret);
  1039. return ret;
  1040. }
  1041. ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
  1042. ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
  1043. ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
  1044. return 0;
  1045. }
  1046. static int ath6kl_init_upload(struct ath6kl *ar)
  1047. {
  1048. u32 param, options, sleep, address;
  1049. int status = 0;
  1050. if (ar->target_type != TARGET_TYPE_AR6003 &&
  1051. ar->target_type != TARGET_TYPE_AR6004)
  1052. return -EINVAL;
  1053. /* temporarily disable system sleep */
  1054. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1055. status = ath6kl_bmi_reg_read(ar, address, &param);
  1056. if (status)
  1057. return status;
  1058. options = param;
  1059. param |= ATH6KL_OPTION_SLEEP_DISABLE;
  1060. status = ath6kl_bmi_reg_write(ar, address, param);
  1061. if (status)
  1062. return status;
  1063. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1064. status = ath6kl_bmi_reg_read(ar, address, &param);
  1065. if (status)
  1066. return status;
  1067. sleep = param;
  1068. param |= SM(SYSTEM_SLEEP_DISABLE, 1);
  1069. status = ath6kl_bmi_reg_write(ar, address, param);
  1070. if (status)
  1071. return status;
  1072. ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
  1073. options, sleep);
  1074. /* program analog PLL register */
  1075. /* no need to control 40/44MHz clock on AR6004 */
  1076. if (ar->target_type != TARGET_TYPE_AR6004) {
  1077. status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
  1078. 0xF9104001);
  1079. if (status)
  1080. return status;
  1081. /* Run at 80/88MHz by default */
  1082. param = SM(CPU_CLOCK_STANDARD, 1);
  1083. address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
  1084. status = ath6kl_bmi_reg_write(ar, address, param);
  1085. if (status)
  1086. return status;
  1087. }
  1088. param = 0;
  1089. address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
  1090. param = SM(LPO_CAL_ENABLE, 1);
  1091. status = ath6kl_bmi_reg_write(ar, address, param);
  1092. if (status)
  1093. return status;
  1094. /* WAR to avoid SDIO CRC err */
  1095. if (ar->version.target_ver == AR6003_HW_2_0_VERSION ||
  1096. ar->version.target_ver == AR6003_HW_2_1_1_VERSION) {
  1097. ath6kl_err("temporary war to avoid sdio crc error\n");
  1098. param = 0x20;
  1099. address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
  1100. status = ath6kl_bmi_reg_write(ar, address, param);
  1101. if (status)
  1102. return status;
  1103. address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
  1104. status = ath6kl_bmi_reg_write(ar, address, param);
  1105. if (status)
  1106. return status;
  1107. address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
  1108. status = ath6kl_bmi_reg_write(ar, address, param);
  1109. if (status)
  1110. return status;
  1111. address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
  1112. status = ath6kl_bmi_reg_write(ar, address, param);
  1113. if (status)
  1114. return status;
  1115. }
  1116. /* write EEPROM data to Target RAM */
  1117. status = ath6kl_upload_board_file(ar);
  1118. if (status)
  1119. return status;
  1120. /* transfer One time Programmable data */
  1121. status = ath6kl_upload_otp(ar);
  1122. if (status)
  1123. return status;
  1124. /* Download Target firmware */
  1125. status = ath6kl_upload_firmware(ar);
  1126. if (status)
  1127. return status;
  1128. status = ath6kl_upload_patch(ar);
  1129. if (status)
  1130. return status;
  1131. /* Download the test script */
  1132. status = ath6kl_upload_testscript(ar);
  1133. if (status)
  1134. return status;
  1135. /* Restore system sleep */
  1136. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1137. status = ath6kl_bmi_reg_write(ar, address, sleep);
  1138. if (status)
  1139. return status;
  1140. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1141. param = options | 0x20;
  1142. status = ath6kl_bmi_reg_write(ar, address, param);
  1143. if (status)
  1144. return status;
  1145. return status;
  1146. }
  1147. int ath6kl_init_hw_params(struct ath6kl *ar)
  1148. {
  1149. const struct ath6kl_hw *uninitialized_var(hw);
  1150. int i;
  1151. for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
  1152. hw = &hw_list[i];
  1153. if (hw->id == ar->version.target_ver)
  1154. break;
  1155. }
  1156. if (i == ARRAY_SIZE(hw_list)) {
  1157. ath6kl_err("Unsupported hardware version: 0x%x\n",
  1158. ar->version.target_ver);
  1159. return -EINVAL;
  1160. }
  1161. ar->hw = *hw;
  1162. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1163. "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
  1164. ar->version.target_ver, ar->target_type,
  1165. ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
  1166. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1167. "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
  1168. ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
  1169. ar->hw.reserved_ram_size);
  1170. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1171. "refclk_hz %d uarttx_pin %d",
  1172. ar->hw.refclk_hz, ar->hw.uarttx_pin);
  1173. return 0;
  1174. }
  1175. static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
  1176. {
  1177. switch (type) {
  1178. case ATH6KL_HIF_TYPE_SDIO:
  1179. return "sdio";
  1180. case ATH6KL_HIF_TYPE_USB:
  1181. return "usb";
  1182. }
  1183. return NULL;
  1184. }
  1185. int ath6kl_init_hw_start(struct ath6kl *ar)
  1186. {
  1187. long timeleft;
  1188. int ret, i;
  1189. ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
  1190. ret = ath6kl_hif_power_on(ar);
  1191. if (ret)
  1192. return ret;
  1193. ret = ath6kl_configure_target(ar);
  1194. if (ret)
  1195. goto err_power_off;
  1196. ret = ath6kl_init_upload(ar);
  1197. if (ret)
  1198. goto err_power_off;
  1199. /* Do we need to finish the BMI phase */
  1200. /* FIXME: return error from ath6kl_bmi_done() */
  1201. if (ath6kl_bmi_done(ar)) {
  1202. ret = -EIO;
  1203. goto err_power_off;
  1204. }
  1205. /*
  1206. * The reason we have to wait for the target here is that the
  1207. * driver layer has to init BMI in order to set the host block
  1208. * size.
  1209. */
  1210. if (ath6kl_htc_wait_target(ar->htc_target)) {
  1211. ret = -EIO;
  1212. goto err_power_off;
  1213. }
  1214. if (ath6kl_init_service_ep(ar)) {
  1215. ret = -EIO;
  1216. goto err_cleanup_scatter;
  1217. }
  1218. /* setup credit distribution */
  1219. ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
  1220. /* start HTC */
  1221. ret = ath6kl_htc_start(ar->htc_target);
  1222. if (ret) {
  1223. /* FIXME: call this */
  1224. ath6kl_cookie_cleanup(ar);
  1225. goto err_cleanup_scatter;
  1226. }
  1227. /* Wait for Wmi event to be ready */
  1228. timeleft = wait_event_interruptible_timeout(ar->event_wq,
  1229. test_bit(WMI_READY,
  1230. &ar->flag),
  1231. WMI_TIMEOUT);
  1232. ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
  1233. if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
  1234. ath6kl_info("%s %s fw %s api %d%s\n",
  1235. ar->hw.name,
  1236. ath6kl_init_get_hif_name(ar->hif_type),
  1237. ar->wiphy->fw_version,
  1238. ar->fw_api,
  1239. test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
  1240. }
  1241. if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
  1242. ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
  1243. ATH6KL_ABI_VERSION, ar->version.abi_ver);
  1244. ret = -EIO;
  1245. goto err_htc_stop;
  1246. }
  1247. if (!timeleft || signal_pending(current)) {
  1248. ath6kl_err("wmi is not ready or wait was interrupted\n");
  1249. ret = -EIO;
  1250. goto err_htc_stop;
  1251. }
  1252. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
  1253. /* communicate the wmi protocol verision to the target */
  1254. /* FIXME: return error */
  1255. if ((ath6kl_set_host_app_area(ar)) != 0)
  1256. ath6kl_err("unable to set the host app area\n");
  1257. for (i = 0; i < ar->vif_max; i++) {
  1258. ret = ath6kl_target_config_wlan_params(ar, i);
  1259. if (ret)
  1260. goto err_htc_stop;
  1261. }
  1262. ar->state = ATH6KL_STATE_ON;
  1263. return 0;
  1264. err_htc_stop:
  1265. ath6kl_htc_stop(ar->htc_target);
  1266. err_cleanup_scatter:
  1267. ath6kl_hif_cleanup_scatter(ar);
  1268. err_power_off:
  1269. ath6kl_hif_power_off(ar);
  1270. return ret;
  1271. }
  1272. int ath6kl_init_hw_stop(struct ath6kl *ar)
  1273. {
  1274. int ret;
  1275. ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
  1276. ath6kl_htc_stop(ar->htc_target);
  1277. ath6kl_hif_stop(ar);
  1278. ath6kl_bmi_reset(ar);
  1279. ret = ath6kl_hif_power_off(ar);
  1280. if (ret)
  1281. ath6kl_warn("failed to power off hif: %d\n", ret);
  1282. ar->state = ATH6KL_STATE_OFF;
  1283. return 0;
  1284. }
  1285. /* FIXME: move this to cfg80211.c and rename to ath6kl_cfg80211_vif_stop() */
  1286. void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
  1287. {
  1288. static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  1289. bool discon_issued;
  1290. netif_stop_queue(vif->ndev);
  1291. clear_bit(WLAN_ENABLED, &vif->flags);
  1292. if (wmi_ready) {
  1293. discon_issued = test_bit(CONNECTED, &vif->flags) ||
  1294. test_bit(CONNECT_PEND, &vif->flags);
  1295. ath6kl_disconnect(vif);
  1296. del_timer(&vif->disconnect_timer);
  1297. if (discon_issued)
  1298. ath6kl_disconnect_event(vif, DISCONNECT_CMD,
  1299. (vif->nw_type & AP_NETWORK) ?
  1300. bcast_mac : vif->bssid,
  1301. 0, NULL, 0);
  1302. }
  1303. if (vif->scan_req) {
  1304. cfg80211_scan_done(vif->scan_req, true);
  1305. vif->scan_req = NULL;
  1306. }
  1307. }
  1308. void ath6kl_stop_txrx(struct ath6kl *ar)
  1309. {
  1310. struct ath6kl_vif *vif, *tmp_vif;
  1311. int i;
  1312. set_bit(DESTROY_IN_PROGRESS, &ar->flag);
  1313. if (down_interruptible(&ar->sem)) {
  1314. ath6kl_err("down_interruptible failed\n");
  1315. return;
  1316. }
  1317. for (i = 0; i < AP_MAX_NUM_STA; i++)
  1318. aggr_reset_state(ar->sta_list[i].aggr_conn);
  1319. spin_lock_bh(&ar->list_lock);
  1320. list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
  1321. list_del(&vif->list);
  1322. spin_unlock_bh(&ar->list_lock);
  1323. ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
  1324. rtnl_lock();
  1325. ath6kl_cfg80211_vif_cleanup(vif);
  1326. rtnl_unlock();
  1327. spin_lock_bh(&ar->list_lock);
  1328. }
  1329. spin_unlock_bh(&ar->list_lock);
  1330. clear_bit(WMI_READY, &ar->flag);
  1331. /*
  1332. * After wmi_shudown all WMI events will be dropped. We
  1333. * need to cleanup the buffers allocated in AP mode and
  1334. * give disconnect notification to stack, which usually
  1335. * happens in the disconnect_event. Simulate the disconnect
  1336. * event by calling the function directly. Sometimes
  1337. * disconnect_event will be received when the debug logs
  1338. * are collected.
  1339. */
  1340. ath6kl_wmi_shutdown(ar->wmi);
  1341. clear_bit(WMI_ENABLED, &ar->flag);
  1342. if (ar->htc_target) {
  1343. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
  1344. ath6kl_htc_stop(ar->htc_target);
  1345. }
  1346. /*
  1347. * Try to reset the device if we can. The driver may have been
  1348. * configure NOT to reset the target during a debug session.
  1349. */
  1350. ath6kl_dbg(ATH6KL_DBG_TRC,
  1351. "attempting to reset target on instance destroy\n");
  1352. ath6kl_reset_device(ar, ar->target_type, true, true);
  1353. clear_bit(WLAN_ENABLED, &ar->flag);
  1354. up(&ar->sem);
  1355. }
  1356. EXPORT_SYMBOL(ath6kl_stop_txrx);