init.c 40 KB

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