init.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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 ret;
  325. /*
  326. * Configure the device for rx dot11 header rules. "0,0" are the
  327. * default values. Required if checksum offload is needed. Set
  328. * RxMetaVersion to 2.
  329. */
  330. ret = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
  331. ar->rx_meta_ver, 0, 0);
  332. if (ret) {
  333. ath6kl_err("unable to set the rx frame format: %d\n", ret);
  334. return ret;
  335. }
  336. if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN) {
  337. ret = ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
  338. IGNORE_PS_FAIL_DURING_SCAN);
  339. if (ret) {
  340. ath6kl_err("unable to set power save fail event policy: %d\n",
  341. ret);
  342. return ret;
  343. }
  344. }
  345. if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER)) {
  346. ret = ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
  347. WMI_FOLLOW_BARKER_IN_ERP);
  348. if (ret) {
  349. ath6kl_err("unable to set barker preamble policy: %d\n",
  350. ret);
  351. return ret;
  352. }
  353. }
  354. ret = ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
  355. WLAN_CONFIG_KEEP_ALIVE_INTERVAL);
  356. if (ret) {
  357. ath6kl_err("unable to set keep alive interval: %d\n", ret);
  358. return ret;
  359. }
  360. ret = ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
  361. WLAN_CONFIG_DISCONNECT_TIMEOUT);
  362. if (ret) {
  363. ath6kl_err("unable to set disconnect timeout: %d\n", ret);
  364. return ret;
  365. }
  366. if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST)) {
  367. ret = ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED);
  368. if (ret) {
  369. ath6kl_err("unable to set txop bursting: %d\n", ret);
  370. return ret;
  371. }
  372. }
  373. if (ar->p2p && (ar->vif_max == 1 || idx)) {
  374. ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
  375. P2P_FLAG_CAPABILITIES_REQ |
  376. P2P_FLAG_MACADDR_REQ |
  377. P2P_FLAG_HMODEL_REQ);
  378. if (ret) {
  379. ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
  380. "capabilities (%d) - assuming P2P not "
  381. "supported\n", ret);
  382. ar->p2p = false;
  383. }
  384. }
  385. if (ar->p2p && (ar->vif_max == 1 || idx)) {
  386. /* Enable Probe Request reporting for P2P */
  387. ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
  388. if (ret) {
  389. ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
  390. "Request reporting (%d)\n", ret);
  391. }
  392. }
  393. return ret;
  394. }
  395. int ath6kl_configure_target(struct ath6kl *ar)
  396. {
  397. u32 param, ram_reserved_size;
  398. u8 fw_iftype, fw_mode = 0, fw_submode = 0;
  399. int i, status;
  400. param = !!(ar->conf_flags & ATH6KL_CONF_UART_DEBUG);
  401. if (ath6kl_bmi_write_hi32(ar, hi_serial_enable, param)) {
  402. ath6kl_err("bmi_write_memory for uart debug failed\n");
  403. return -EIO;
  404. }
  405. /*
  406. * Note: Even though the firmware interface type is
  407. * chosen as BSS_STA for all three interfaces, can
  408. * be configured to IBSS/AP as long as the fw submode
  409. * remains normal mode (0 - AP, STA and IBSS). But
  410. * due to an target assert in firmware only one interface is
  411. * configured for now.
  412. */
  413. fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
  414. for (i = 0; i < ar->vif_max; i++)
  415. fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
  416. /*
  417. * By default, submodes :
  418. * vif[0] - AP/STA/IBSS
  419. * vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
  420. * vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
  421. */
  422. for (i = 0; i < ar->max_norm_iface; i++)
  423. fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
  424. (i * HI_OPTION_FW_SUBMODE_BITS);
  425. for (i = ar->max_norm_iface; i < ar->vif_max; i++)
  426. fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
  427. (i * HI_OPTION_FW_SUBMODE_BITS);
  428. if (ar->p2p && ar->vif_max == 1)
  429. fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
  430. if (ath6kl_bmi_write_hi32(ar, hi_app_host_interest,
  431. HTC_PROTOCOL_VERSION) != 0) {
  432. ath6kl_err("bmi_write_memory for htc version failed\n");
  433. return -EIO;
  434. }
  435. /* set the firmware mode to STA/IBSS/AP */
  436. param = 0;
  437. if (ath6kl_bmi_read_hi32(ar, hi_option_flag, &param) != 0) {
  438. ath6kl_err("bmi_read_memory for setting fwmode failed\n");
  439. return -EIO;
  440. }
  441. param |= (ar->vif_max << HI_OPTION_NUM_DEV_SHIFT);
  442. param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
  443. param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
  444. param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
  445. param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
  446. if (ath6kl_bmi_write_hi32(ar, hi_option_flag, param) != 0) {
  447. ath6kl_err("bmi_write_memory for setting fwmode failed\n");
  448. return -EIO;
  449. }
  450. ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
  451. /*
  452. * Hardcode the address use for the extended board data
  453. * Ideally this should be pre-allocate by the OS at boot time
  454. * But since it is a new feature and board data is loaded
  455. * at init time, we have to workaround this from host.
  456. * It is difficult to patch the firmware boot code,
  457. * but possible in theory.
  458. */
  459. param = ar->hw.board_ext_data_addr;
  460. ram_reserved_size = ar->hw.reserved_ram_size;
  461. if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
  462. ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
  463. return -EIO;
  464. }
  465. if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
  466. ram_reserved_size) != 0) {
  467. ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
  468. return -EIO;
  469. }
  470. /* set the block size for the target */
  471. if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
  472. /* use default number of control buffers */
  473. return -EIO;
  474. /* Configure GPIO AR600x UART */
  475. status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
  476. ar->hw.uarttx_pin);
  477. if (status)
  478. return status;
  479. /* Configure target refclk_hz */
  480. status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz, ar->hw.refclk_hz);
  481. if (status)
  482. return status;
  483. return 0;
  484. }
  485. /* firmware upload */
  486. static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
  487. u8 **fw, size_t *fw_len)
  488. {
  489. const struct firmware *fw_entry;
  490. int ret;
  491. ret = request_firmware(&fw_entry, filename, ar->dev);
  492. if (ret)
  493. return ret;
  494. *fw_len = fw_entry->size;
  495. *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
  496. if (*fw == NULL)
  497. ret = -ENOMEM;
  498. release_firmware(fw_entry);
  499. return ret;
  500. }
  501. #ifdef CONFIG_OF
  502. /*
  503. * Check the device tree for a board-id and use it to construct
  504. * the pathname to the firmware file. Used (for now) to find a
  505. * fallback to the "bdata.bin" file--typically a symlink to the
  506. * appropriate board-specific file.
  507. */
  508. static bool check_device_tree(struct ath6kl *ar)
  509. {
  510. static const char *board_id_prop = "atheros,board-id";
  511. struct device_node *node;
  512. char board_filename[64];
  513. const char *board_id;
  514. int ret;
  515. for_each_compatible_node(node, NULL, "atheros,ath6kl") {
  516. board_id = of_get_property(node, board_id_prop, NULL);
  517. if (board_id == NULL) {
  518. ath6kl_warn("No \"%s\" property on %s node.\n",
  519. board_id_prop, node->name);
  520. continue;
  521. }
  522. snprintf(board_filename, sizeof(board_filename),
  523. "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
  524. ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
  525. &ar->fw_board_len);
  526. if (ret) {
  527. ath6kl_err("Failed to get DT board file %s: %d\n",
  528. board_filename, ret);
  529. continue;
  530. }
  531. return true;
  532. }
  533. return false;
  534. }
  535. #else
  536. static bool check_device_tree(struct ath6kl *ar)
  537. {
  538. return false;
  539. }
  540. #endif /* CONFIG_OF */
  541. static int ath6kl_fetch_board_file(struct ath6kl *ar)
  542. {
  543. const char *filename;
  544. int ret;
  545. if (ar->fw_board != NULL)
  546. return 0;
  547. if (WARN_ON(ar->hw.fw_board == NULL))
  548. return -EINVAL;
  549. filename = ar->hw.fw_board;
  550. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  551. &ar->fw_board_len);
  552. if (ret == 0) {
  553. /* managed to get proper board file */
  554. return 0;
  555. }
  556. if (check_device_tree(ar)) {
  557. /* got board file from device tree */
  558. return 0;
  559. }
  560. /* there was no proper board file, try to use default instead */
  561. ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
  562. filename, ret);
  563. filename = ar->hw.fw_default_board;
  564. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  565. &ar->fw_board_len);
  566. if (ret) {
  567. ath6kl_err("Failed to get default board file %s: %d\n",
  568. filename, ret);
  569. return ret;
  570. }
  571. ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
  572. ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
  573. return 0;
  574. }
  575. static int ath6kl_fetch_otp_file(struct ath6kl *ar)
  576. {
  577. char filename[100];
  578. int ret;
  579. if (ar->fw_otp != NULL)
  580. return 0;
  581. if (ar->hw.fw.otp == NULL) {
  582. ath6kl_dbg(ATH6KL_DBG_BOOT,
  583. "no OTP file configured for this hw\n");
  584. return 0;
  585. }
  586. snprintf(filename, sizeof(filename), "%s/%s",
  587. ar->hw.fw.dir, ar->hw.fw.otp);
  588. ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
  589. &ar->fw_otp_len);
  590. if (ret) {
  591. ath6kl_err("Failed to get OTP file %s: %d\n",
  592. filename, ret);
  593. return ret;
  594. }
  595. return 0;
  596. }
  597. static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
  598. {
  599. char filename[100];
  600. int ret;
  601. if (ar->testmode == 0)
  602. return 0;
  603. ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
  604. if (ar->testmode == 2) {
  605. if (ar->hw.fw.utf == NULL) {
  606. ath6kl_warn("testmode 2 not supported\n");
  607. return -EOPNOTSUPP;
  608. }
  609. snprintf(filename, sizeof(filename), "%s/%s",
  610. ar->hw.fw.dir, ar->hw.fw.utf);
  611. } else {
  612. if (ar->hw.fw.tcmd == NULL) {
  613. ath6kl_warn("testmode 1 not supported\n");
  614. return -EOPNOTSUPP;
  615. }
  616. snprintf(filename, sizeof(filename), "%s/%s",
  617. ar->hw.fw.dir, ar->hw.fw.tcmd);
  618. }
  619. set_bit(TESTMODE, &ar->flag);
  620. ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
  621. if (ret) {
  622. ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
  623. ar->testmode, filename, ret);
  624. return ret;
  625. }
  626. return 0;
  627. }
  628. static int ath6kl_fetch_fw_file(struct ath6kl *ar)
  629. {
  630. char filename[100];
  631. int ret;
  632. if (ar->fw != NULL)
  633. return 0;
  634. /* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
  635. if (WARN_ON(ar->hw.fw.fw == NULL))
  636. return -EINVAL;
  637. snprintf(filename, sizeof(filename), "%s/%s",
  638. ar->hw.fw.dir, ar->hw.fw.fw);
  639. ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
  640. if (ret) {
  641. ath6kl_err("Failed to get firmware file %s: %d\n",
  642. filename, ret);
  643. return ret;
  644. }
  645. return 0;
  646. }
  647. static int ath6kl_fetch_patch_file(struct ath6kl *ar)
  648. {
  649. char filename[100];
  650. int ret;
  651. if (ar->fw_patch != NULL)
  652. return 0;
  653. if (ar->hw.fw.patch == NULL)
  654. return 0;
  655. snprintf(filename, sizeof(filename), "%s/%s",
  656. ar->hw.fw.dir, ar->hw.fw.patch);
  657. ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
  658. &ar->fw_patch_len);
  659. if (ret) {
  660. ath6kl_err("Failed to get patch file %s: %d\n",
  661. filename, ret);
  662. return ret;
  663. }
  664. return 0;
  665. }
  666. static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
  667. {
  668. char filename[100];
  669. int ret;
  670. if (ar->testmode != 2)
  671. return 0;
  672. if (ar->fw_testscript != NULL)
  673. return 0;
  674. if (ar->hw.fw.testscript == NULL)
  675. return 0;
  676. snprintf(filename, sizeof(filename), "%s/%s",
  677. ar->hw.fw.dir, ar->hw.fw.testscript);
  678. ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
  679. &ar->fw_testscript_len);
  680. if (ret) {
  681. ath6kl_err("Failed to get testscript file %s: %d\n",
  682. filename, ret);
  683. return ret;
  684. }
  685. return 0;
  686. }
  687. static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
  688. {
  689. int ret;
  690. ret = ath6kl_fetch_otp_file(ar);
  691. if (ret)
  692. return ret;
  693. ret = ath6kl_fetch_fw_file(ar);
  694. if (ret)
  695. return ret;
  696. ret = ath6kl_fetch_patch_file(ar);
  697. if (ret)
  698. return ret;
  699. ret = ath6kl_fetch_testscript_file(ar);
  700. if (ret)
  701. return ret;
  702. return 0;
  703. }
  704. static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
  705. {
  706. size_t magic_len, len, ie_len;
  707. const struct firmware *fw;
  708. struct ath6kl_fw_ie *hdr;
  709. char filename[100];
  710. const u8 *data;
  711. int ret, ie_id, i, index, bit;
  712. __le32 *val;
  713. snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
  714. ret = request_firmware(&fw, filename, ar->dev);
  715. if (ret)
  716. return ret;
  717. data = fw->data;
  718. len = fw->size;
  719. /* magic also includes the null byte, check that as well */
  720. magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
  721. if (len < magic_len) {
  722. ret = -EINVAL;
  723. goto out;
  724. }
  725. if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
  726. ret = -EINVAL;
  727. goto out;
  728. }
  729. len -= magic_len;
  730. data += magic_len;
  731. /* loop elements */
  732. while (len > sizeof(struct ath6kl_fw_ie)) {
  733. /* hdr is unaligned! */
  734. hdr = (struct ath6kl_fw_ie *) data;
  735. ie_id = le32_to_cpup(&hdr->id);
  736. ie_len = le32_to_cpup(&hdr->len);
  737. len -= sizeof(*hdr);
  738. data += sizeof(*hdr);
  739. if (len < ie_len) {
  740. ret = -EINVAL;
  741. goto out;
  742. }
  743. switch (ie_id) {
  744. case ATH6KL_FW_IE_OTP_IMAGE:
  745. ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
  746. ie_len);
  747. ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
  748. if (ar->fw_otp == NULL) {
  749. ret = -ENOMEM;
  750. goto out;
  751. }
  752. ar->fw_otp_len = ie_len;
  753. break;
  754. case ATH6KL_FW_IE_FW_IMAGE:
  755. ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
  756. ie_len);
  757. /* in testmode we already might have a fw file */
  758. if (ar->fw != NULL)
  759. break;
  760. ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
  761. if (ar->fw == NULL) {
  762. ret = -ENOMEM;
  763. goto out;
  764. }
  765. ar->fw_len = ie_len;
  766. break;
  767. case ATH6KL_FW_IE_PATCH_IMAGE:
  768. ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
  769. ie_len);
  770. ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
  771. if (ar->fw_patch == NULL) {
  772. ret = -ENOMEM;
  773. goto out;
  774. }
  775. ar->fw_patch_len = ie_len;
  776. break;
  777. case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
  778. val = (__le32 *) data;
  779. ar->hw.reserved_ram_size = le32_to_cpup(val);
  780. ath6kl_dbg(ATH6KL_DBG_BOOT,
  781. "found reserved ram size ie 0x%d\n",
  782. ar->hw.reserved_ram_size);
  783. break;
  784. case ATH6KL_FW_IE_CAPABILITIES:
  785. if (ie_len < DIV_ROUND_UP(ATH6KL_FW_CAPABILITY_MAX, 8))
  786. break;
  787. ath6kl_dbg(ATH6KL_DBG_BOOT,
  788. "found firmware capabilities ie (%zd B)\n",
  789. ie_len);
  790. for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
  791. index = i / 8;
  792. bit = i % 8;
  793. if (data[index] & (1 << bit))
  794. __set_bit(i, ar->fw_capabilities);
  795. }
  796. ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
  797. ar->fw_capabilities,
  798. sizeof(ar->fw_capabilities));
  799. break;
  800. case ATH6KL_FW_IE_PATCH_ADDR:
  801. if (ie_len != sizeof(*val))
  802. break;
  803. val = (__le32 *) data;
  804. ar->hw.dataset_patch_addr = le32_to_cpup(val);
  805. ath6kl_dbg(ATH6KL_DBG_BOOT,
  806. "found patch address ie 0x%x\n",
  807. ar->hw.dataset_patch_addr);
  808. break;
  809. case ATH6KL_FW_IE_BOARD_ADDR:
  810. if (ie_len != sizeof(*val))
  811. break;
  812. val = (__le32 *) data;
  813. ar->hw.board_addr = le32_to_cpup(val);
  814. ath6kl_dbg(ATH6KL_DBG_BOOT,
  815. "found board address ie 0x%x\n",
  816. ar->hw.board_addr);
  817. break;
  818. case ATH6KL_FW_IE_VIF_MAX:
  819. if (ie_len != sizeof(*val))
  820. break;
  821. val = (__le32 *) data;
  822. ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
  823. ATH6KL_VIF_MAX);
  824. if (ar->vif_max > 1 && !ar->p2p)
  825. ar->max_norm_iface = 2;
  826. ath6kl_dbg(ATH6KL_DBG_BOOT,
  827. "found vif max ie %d\n", ar->vif_max);
  828. break;
  829. default:
  830. ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
  831. le32_to_cpup(&hdr->id));
  832. break;
  833. }
  834. len -= ie_len;
  835. data += ie_len;
  836. };
  837. ret = 0;
  838. out:
  839. release_firmware(fw);
  840. return ret;
  841. }
  842. int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
  843. {
  844. int ret;
  845. ret = ath6kl_fetch_board_file(ar);
  846. if (ret)
  847. return ret;
  848. ret = ath6kl_fetch_testmode_file(ar);
  849. if (ret)
  850. return ret;
  851. ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
  852. if (ret == 0) {
  853. ar->fw_api = 3;
  854. goto out;
  855. }
  856. ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
  857. if (ret == 0) {
  858. ar->fw_api = 2;
  859. goto out;
  860. }
  861. ret = ath6kl_fetch_fw_api1(ar);
  862. if (ret)
  863. return ret;
  864. ar->fw_api = 1;
  865. out:
  866. ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
  867. return 0;
  868. }
  869. static int ath6kl_upload_board_file(struct ath6kl *ar)
  870. {
  871. u32 board_address, board_ext_address, param;
  872. u32 board_data_size, board_ext_data_size;
  873. int ret;
  874. if (WARN_ON(ar->fw_board == NULL))
  875. return -ENOENT;
  876. /*
  877. * Determine where in Target RAM to write Board Data.
  878. * For AR6004, host determine Target RAM address for
  879. * writing board data.
  880. */
  881. if (ar->hw.board_addr != 0) {
  882. board_address = ar->hw.board_addr;
  883. ath6kl_bmi_write_hi32(ar, hi_board_data,
  884. board_address);
  885. } else {
  886. ath6kl_bmi_read_hi32(ar, hi_board_data, &board_address);
  887. }
  888. /* determine where in target ram to write extended board data */
  889. ath6kl_bmi_read_hi32(ar, hi_board_ext_data, &board_ext_address);
  890. if (ar->target_type == TARGET_TYPE_AR6003 &&
  891. board_ext_address == 0) {
  892. ath6kl_err("Failed to get board file target address.\n");
  893. return -EINVAL;
  894. }
  895. switch (ar->target_type) {
  896. case TARGET_TYPE_AR6003:
  897. board_data_size = AR6003_BOARD_DATA_SZ;
  898. board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
  899. if (ar->fw_board_len > (board_data_size + board_ext_data_size))
  900. board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
  901. break;
  902. case TARGET_TYPE_AR6004:
  903. board_data_size = AR6004_BOARD_DATA_SZ;
  904. board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
  905. break;
  906. default:
  907. WARN_ON(1);
  908. return -EINVAL;
  909. break;
  910. }
  911. if (board_ext_address &&
  912. ar->fw_board_len == (board_data_size + board_ext_data_size)) {
  913. /* write extended board data */
  914. ath6kl_dbg(ATH6KL_DBG_BOOT,
  915. "writing extended board data to 0x%x (%d B)\n",
  916. board_ext_address, board_ext_data_size);
  917. ret = ath6kl_bmi_write(ar, board_ext_address,
  918. ar->fw_board + board_data_size,
  919. board_ext_data_size);
  920. if (ret) {
  921. ath6kl_err("Failed to write extended board data: %d\n",
  922. ret);
  923. return ret;
  924. }
  925. /* record that extended board data is initialized */
  926. param = (board_ext_data_size << 16) | 1;
  927. ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
  928. }
  929. if (ar->fw_board_len < board_data_size) {
  930. ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
  931. ret = -EINVAL;
  932. return ret;
  933. }
  934. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
  935. board_address, board_data_size);
  936. ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
  937. board_data_size);
  938. if (ret) {
  939. ath6kl_err("Board file bmi write failed: %d\n", ret);
  940. return ret;
  941. }
  942. /* record the fact that Board Data IS initialized */
  943. ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, 1);
  944. return ret;
  945. }
  946. static int ath6kl_upload_otp(struct ath6kl *ar)
  947. {
  948. u32 address, param;
  949. bool from_hw = false;
  950. int ret;
  951. if (ar->fw_otp == NULL)
  952. return 0;
  953. address = ar->hw.app_load_addr;
  954. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
  955. ar->fw_otp_len);
  956. ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
  957. ar->fw_otp_len);
  958. if (ret) {
  959. ath6kl_err("Failed to upload OTP file: %d\n", ret);
  960. return ret;
  961. }
  962. /* read firmware start address */
  963. ret = ath6kl_bmi_read_hi32(ar, hi_app_start, &address);
  964. if (ret) {
  965. ath6kl_err("Failed to read hi_app_start: %d\n", ret);
  966. return ret;
  967. }
  968. if (ar->hw.app_start_override_addr == 0) {
  969. ar->hw.app_start_override_addr = address;
  970. from_hw = true;
  971. }
  972. ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
  973. from_hw ? " (from hw)" : "",
  974. ar->hw.app_start_override_addr);
  975. /* execute the OTP code */
  976. ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
  977. ar->hw.app_start_override_addr);
  978. param = 0;
  979. ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
  980. return ret;
  981. }
  982. static int ath6kl_upload_firmware(struct ath6kl *ar)
  983. {
  984. u32 address;
  985. int ret;
  986. if (WARN_ON(ar->fw == NULL))
  987. return 0;
  988. address = ar->hw.app_load_addr;
  989. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
  990. address, ar->fw_len);
  991. ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
  992. if (ret) {
  993. ath6kl_err("Failed to write firmware: %d\n", ret);
  994. return ret;
  995. }
  996. /*
  997. * Set starting address for firmware
  998. * Don't need to setup app_start override addr on AR6004
  999. */
  1000. if (ar->target_type != TARGET_TYPE_AR6004) {
  1001. address = ar->hw.app_start_override_addr;
  1002. ath6kl_bmi_set_app_start(ar, address);
  1003. }
  1004. return ret;
  1005. }
  1006. static int ath6kl_upload_patch(struct ath6kl *ar)
  1007. {
  1008. u32 address;
  1009. int ret;
  1010. if (ar->fw_patch == NULL)
  1011. return 0;
  1012. address = ar->hw.dataset_patch_addr;
  1013. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
  1014. address, ar->fw_patch_len);
  1015. ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
  1016. if (ret) {
  1017. ath6kl_err("Failed to write patch file: %d\n", ret);
  1018. return ret;
  1019. }
  1020. ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
  1021. return 0;
  1022. }
  1023. static int ath6kl_upload_testscript(struct ath6kl *ar)
  1024. {
  1025. u32 address;
  1026. int ret;
  1027. if (ar->testmode != 2)
  1028. return 0;
  1029. if (ar->fw_testscript == NULL)
  1030. return 0;
  1031. address = ar->hw.testscript_addr;
  1032. ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
  1033. address, ar->fw_testscript_len);
  1034. ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
  1035. ar->fw_testscript_len);
  1036. if (ret) {
  1037. ath6kl_err("Failed to write testscript file: %d\n", ret);
  1038. return ret;
  1039. }
  1040. ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
  1041. ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
  1042. ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
  1043. return 0;
  1044. }
  1045. static int ath6kl_init_upload(struct ath6kl *ar)
  1046. {
  1047. u32 param, options, sleep, address;
  1048. int status = 0;
  1049. if (ar->target_type != TARGET_TYPE_AR6003 &&
  1050. ar->target_type != TARGET_TYPE_AR6004)
  1051. return -EINVAL;
  1052. /* temporarily disable system sleep */
  1053. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1054. status = ath6kl_bmi_reg_read(ar, address, &param);
  1055. if (status)
  1056. return status;
  1057. options = param;
  1058. param |= ATH6KL_OPTION_SLEEP_DISABLE;
  1059. status = ath6kl_bmi_reg_write(ar, address, param);
  1060. if (status)
  1061. return status;
  1062. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1063. status = ath6kl_bmi_reg_read(ar, address, &param);
  1064. if (status)
  1065. return status;
  1066. sleep = param;
  1067. param |= SM(SYSTEM_SLEEP_DISABLE, 1);
  1068. status = ath6kl_bmi_reg_write(ar, address, param);
  1069. if (status)
  1070. return status;
  1071. ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
  1072. options, sleep);
  1073. /* program analog PLL register */
  1074. /* no need to control 40/44MHz clock on AR6004 */
  1075. if (ar->target_type != TARGET_TYPE_AR6004) {
  1076. status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
  1077. 0xF9104001);
  1078. if (status)
  1079. return status;
  1080. /* Run at 80/88MHz by default */
  1081. param = SM(CPU_CLOCK_STANDARD, 1);
  1082. address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
  1083. status = ath6kl_bmi_reg_write(ar, address, param);
  1084. if (status)
  1085. return status;
  1086. }
  1087. param = 0;
  1088. address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
  1089. param = SM(LPO_CAL_ENABLE, 1);
  1090. status = ath6kl_bmi_reg_write(ar, address, param);
  1091. if (status)
  1092. return status;
  1093. /* WAR to avoid SDIO CRC err */
  1094. if (ar->version.target_ver == AR6003_HW_2_0_VERSION ||
  1095. ar->version.target_ver == AR6003_HW_2_1_1_VERSION) {
  1096. ath6kl_err("temporary war to avoid sdio crc error\n");
  1097. param = 0x20;
  1098. address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
  1099. status = ath6kl_bmi_reg_write(ar, address, param);
  1100. if (status)
  1101. return status;
  1102. address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
  1103. status = ath6kl_bmi_reg_write(ar, address, param);
  1104. if (status)
  1105. return status;
  1106. address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
  1107. status = ath6kl_bmi_reg_write(ar, address, param);
  1108. if (status)
  1109. return status;
  1110. address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
  1111. status = ath6kl_bmi_reg_write(ar, address, param);
  1112. if (status)
  1113. return status;
  1114. }
  1115. /* write EEPROM data to Target RAM */
  1116. status = ath6kl_upload_board_file(ar);
  1117. if (status)
  1118. return status;
  1119. /* transfer One time Programmable data */
  1120. status = ath6kl_upload_otp(ar);
  1121. if (status)
  1122. return status;
  1123. /* Download Target firmware */
  1124. status = ath6kl_upload_firmware(ar);
  1125. if (status)
  1126. return status;
  1127. status = ath6kl_upload_patch(ar);
  1128. if (status)
  1129. return status;
  1130. /* Download the test script */
  1131. status = ath6kl_upload_testscript(ar);
  1132. if (status)
  1133. return status;
  1134. /* Restore system sleep */
  1135. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1136. status = ath6kl_bmi_reg_write(ar, address, sleep);
  1137. if (status)
  1138. return status;
  1139. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1140. param = options | 0x20;
  1141. status = ath6kl_bmi_reg_write(ar, address, param);
  1142. if (status)
  1143. return status;
  1144. return status;
  1145. }
  1146. int ath6kl_init_hw_params(struct ath6kl *ar)
  1147. {
  1148. const struct ath6kl_hw *uninitialized_var(hw);
  1149. int i;
  1150. for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
  1151. hw = &hw_list[i];
  1152. if (hw->id == ar->version.target_ver)
  1153. break;
  1154. }
  1155. if (i == ARRAY_SIZE(hw_list)) {
  1156. ath6kl_err("Unsupported hardware version: 0x%x\n",
  1157. ar->version.target_ver);
  1158. return -EINVAL;
  1159. }
  1160. ar->hw = *hw;
  1161. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1162. "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
  1163. ar->version.target_ver, ar->target_type,
  1164. ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
  1165. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1166. "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
  1167. ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
  1168. ar->hw.reserved_ram_size);
  1169. ath6kl_dbg(ATH6KL_DBG_BOOT,
  1170. "refclk_hz %d uarttx_pin %d",
  1171. ar->hw.refclk_hz, ar->hw.uarttx_pin);
  1172. return 0;
  1173. }
  1174. static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
  1175. {
  1176. switch (type) {
  1177. case ATH6KL_HIF_TYPE_SDIO:
  1178. return "sdio";
  1179. case ATH6KL_HIF_TYPE_USB:
  1180. return "usb";
  1181. }
  1182. return NULL;
  1183. }
  1184. int ath6kl_init_hw_start(struct ath6kl *ar)
  1185. {
  1186. long timeleft;
  1187. int ret, i;
  1188. ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
  1189. ret = ath6kl_hif_power_on(ar);
  1190. if (ret)
  1191. return ret;
  1192. ret = ath6kl_configure_target(ar);
  1193. if (ret)
  1194. goto err_power_off;
  1195. ret = ath6kl_init_upload(ar);
  1196. if (ret)
  1197. goto err_power_off;
  1198. /* Do we need to finish the BMI phase */
  1199. /* FIXME: return error from ath6kl_bmi_done() */
  1200. if (ath6kl_bmi_done(ar)) {
  1201. ret = -EIO;
  1202. goto err_power_off;
  1203. }
  1204. /*
  1205. * The reason we have to wait for the target here is that the
  1206. * driver layer has to init BMI in order to set the host block
  1207. * size.
  1208. */
  1209. if (ath6kl_htc_wait_target(ar->htc_target)) {
  1210. ret = -EIO;
  1211. goto err_power_off;
  1212. }
  1213. if (ath6kl_init_service_ep(ar)) {
  1214. ret = -EIO;
  1215. goto err_cleanup_scatter;
  1216. }
  1217. /* setup credit distribution */
  1218. ath6kl_credit_setup(ar->htc_target, &ar->credit_state_info);
  1219. /* start HTC */
  1220. ret = ath6kl_htc_start(ar->htc_target);
  1221. if (ret) {
  1222. /* FIXME: call this */
  1223. ath6kl_cookie_cleanup(ar);
  1224. goto err_cleanup_scatter;
  1225. }
  1226. /* Wait for Wmi event to be ready */
  1227. timeleft = wait_event_interruptible_timeout(ar->event_wq,
  1228. test_bit(WMI_READY,
  1229. &ar->flag),
  1230. WMI_TIMEOUT);
  1231. ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
  1232. if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
  1233. ath6kl_info("%s %s fw %s api %d%s\n",
  1234. ar->hw.name,
  1235. ath6kl_init_get_hif_name(ar->hif_type),
  1236. ar->wiphy->fw_version,
  1237. ar->fw_api,
  1238. test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
  1239. }
  1240. if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
  1241. ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
  1242. ATH6KL_ABI_VERSION, ar->version.abi_ver);
  1243. ret = -EIO;
  1244. goto err_htc_stop;
  1245. }
  1246. if (!timeleft || signal_pending(current)) {
  1247. ath6kl_err("wmi is not ready or wait was interrupted\n");
  1248. ret = -EIO;
  1249. goto err_htc_stop;
  1250. }
  1251. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
  1252. /* communicate the wmi protocol verision to the target */
  1253. /* FIXME: return error */
  1254. if ((ath6kl_set_host_app_area(ar)) != 0)
  1255. ath6kl_err("unable to set the host app area\n");
  1256. for (i = 0; i < ar->vif_max; i++) {
  1257. ret = ath6kl_target_config_wlan_params(ar, i);
  1258. if (ret)
  1259. goto err_htc_stop;
  1260. }
  1261. ar->state = ATH6KL_STATE_ON;
  1262. return 0;
  1263. err_htc_stop:
  1264. ath6kl_htc_stop(ar->htc_target);
  1265. err_cleanup_scatter:
  1266. ath6kl_hif_cleanup_scatter(ar);
  1267. err_power_off:
  1268. ath6kl_hif_power_off(ar);
  1269. return ret;
  1270. }
  1271. int ath6kl_init_hw_stop(struct ath6kl *ar)
  1272. {
  1273. int ret;
  1274. ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
  1275. ath6kl_htc_stop(ar->htc_target);
  1276. ath6kl_hif_stop(ar);
  1277. ath6kl_bmi_reset(ar);
  1278. ret = ath6kl_hif_power_off(ar);
  1279. if (ret)
  1280. ath6kl_warn("failed to power off hif: %d\n", ret);
  1281. ar->state = ATH6KL_STATE_OFF;
  1282. return 0;
  1283. }
  1284. /* FIXME: move this to cfg80211.c and rename to ath6kl_cfg80211_vif_stop() */
  1285. void ath6kl_cleanup_vif(struct ath6kl_vif *vif, bool wmi_ready)
  1286. {
  1287. static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  1288. bool discon_issued;
  1289. netif_stop_queue(vif->ndev);
  1290. clear_bit(WLAN_ENABLED, &vif->flags);
  1291. if (wmi_ready) {
  1292. discon_issued = test_bit(CONNECTED, &vif->flags) ||
  1293. test_bit(CONNECT_PEND, &vif->flags);
  1294. ath6kl_disconnect(vif);
  1295. del_timer(&vif->disconnect_timer);
  1296. if (discon_issued)
  1297. ath6kl_disconnect_event(vif, DISCONNECT_CMD,
  1298. (vif->nw_type & AP_NETWORK) ?
  1299. bcast_mac : vif->bssid,
  1300. 0, NULL, 0);
  1301. }
  1302. if (vif->scan_req) {
  1303. cfg80211_scan_done(vif->scan_req, true);
  1304. vif->scan_req = NULL;
  1305. }
  1306. }
  1307. void ath6kl_stop_txrx(struct ath6kl *ar)
  1308. {
  1309. struct ath6kl_vif *vif, *tmp_vif;
  1310. int i;
  1311. set_bit(DESTROY_IN_PROGRESS, &ar->flag);
  1312. if (down_interruptible(&ar->sem)) {
  1313. ath6kl_err("down_interruptible failed\n");
  1314. return;
  1315. }
  1316. for (i = 0; i < AP_MAX_NUM_STA; i++)
  1317. aggr_reset_state(ar->sta_list[i].aggr_conn);
  1318. spin_lock_bh(&ar->list_lock);
  1319. list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
  1320. list_del(&vif->list);
  1321. spin_unlock_bh(&ar->list_lock);
  1322. ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
  1323. rtnl_lock();
  1324. ath6kl_cfg80211_vif_cleanup(vif);
  1325. rtnl_unlock();
  1326. spin_lock_bh(&ar->list_lock);
  1327. }
  1328. spin_unlock_bh(&ar->list_lock);
  1329. clear_bit(WMI_READY, &ar->flag);
  1330. /*
  1331. * After wmi_shudown all WMI events will be dropped. We
  1332. * need to cleanup the buffers allocated in AP mode and
  1333. * give disconnect notification to stack, which usually
  1334. * happens in the disconnect_event. Simulate the disconnect
  1335. * event by calling the function directly. Sometimes
  1336. * disconnect_event will be received when the debug logs
  1337. * are collected.
  1338. */
  1339. ath6kl_wmi_shutdown(ar->wmi);
  1340. clear_bit(WMI_ENABLED, &ar->flag);
  1341. if (ar->htc_target) {
  1342. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
  1343. ath6kl_htc_stop(ar->htc_target);
  1344. }
  1345. /*
  1346. * Try to reset the device if we can. The driver may have been
  1347. * configure NOT to reset the target during a debug session.
  1348. */
  1349. ath6kl_dbg(ATH6KL_DBG_TRC,
  1350. "attempting to reset target on instance destroy\n");
  1351. ath6kl_reset_device(ar, ar->target_type, true, true);
  1352. clear_bit(WLAN_ENABLED, &ar->flag);
  1353. up(&ar->sem);
  1354. }
  1355. EXPORT_SYMBOL(ath6kl_stop_txrx);