init.c 40 KB

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