init.c 41 KB

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