bmi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "bmi.h"
  18. #include "hif.h"
  19. #include "debug.h"
  20. #include "htc.h"
  21. void ath10k_bmi_start(struct ath10k *ar)
  22. {
  23. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi start\n");
  24. ar->bmi.done_sent = false;
  25. }
  26. int ath10k_bmi_done(struct ath10k *ar)
  27. {
  28. struct bmi_cmd cmd;
  29. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.done);
  30. int ret;
  31. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi done\n");
  32. if (ar->bmi.done_sent) {
  33. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi skipped\n");
  34. return 0;
  35. }
  36. ar->bmi.done_sent = true;
  37. cmd.id = __cpu_to_le32(BMI_DONE);
  38. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
  39. if (ret) {
  40. ath10k_warn(ar, "unable to write to the device: %d\n", ret);
  41. return ret;
  42. }
  43. return 0;
  44. }
  45. int ath10k_bmi_get_target_info(struct ath10k *ar,
  46. struct bmi_target_info *target_info)
  47. {
  48. struct bmi_cmd cmd;
  49. union bmi_resp resp;
  50. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.get_target_info);
  51. u32 resplen = sizeof(resp.get_target_info);
  52. int ret;
  53. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi get target info\n");
  54. if (ar->bmi.done_sent) {
  55. ath10k_warn(ar, "BMI Get Target Info Command disallowed\n");
  56. return -EBUSY;
  57. }
  58. cmd.id = __cpu_to_le32(BMI_GET_TARGET_INFO);
  59. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
  60. if (ret) {
  61. ath10k_warn(ar, "unable to get target info from device\n");
  62. return ret;
  63. }
  64. if (resplen < sizeof(resp.get_target_info)) {
  65. ath10k_warn(ar, "invalid get_target_info response length (%d)\n",
  66. resplen);
  67. return -EIO;
  68. }
  69. target_info->version = __le32_to_cpu(resp.get_target_info.version);
  70. target_info->type = __le32_to_cpu(resp.get_target_info.type);
  71. return 0;
  72. }
  73. int ath10k_bmi_read_memory(struct ath10k *ar,
  74. u32 address, void *buffer, u32 length)
  75. {
  76. struct bmi_cmd cmd;
  77. union bmi_resp resp;
  78. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.read_mem);
  79. u32 rxlen;
  80. int ret;
  81. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi read address 0x%x length %d\n",
  82. address, length);
  83. if (ar->bmi.done_sent) {
  84. ath10k_warn(ar, "command disallowed\n");
  85. return -EBUSY;
  86. }
  87. while (length) {
  88. rxlen = min_t(u32, length, BMI_MAX_DATA_SIZE);
  89. cmd.id = __cpu_to_le32(BMI_READ_MEMORY);
  90. cmd.read_mem.addr = __cpu_to_le32(address);
  91. cmd.read_mem.len = __cpu_to_le32(rxlen);
  92. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen,
  93. &resp, &rxlen);
  94. if (ret) {
  95. ath10k_warn(ar, "unable to read from the device (%d)\n",
  96. ret);
  97. return ret;
  98. }
  99. memcpy(buffer, resp.read_mem.payload, rxlen);
  100. address += rxlen;
  101. buffer += rxlen;
  102. length -= rxlen;
  103. }
  104. return 0;
  105. }
  106. int ath10k_bmi_write_memory(struct ath10k *ar,
  107. u32 address, const void *buffer, u32 length)
  108. {
  109. struct bmi_cmd cmd;
  110. u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.write_mem);
  111. u32 txlen;
  112. int ret;
  113. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi write address 0x%x length %d\n",
  114. address, length);
  115. if (ar->bmi.done_sent) {
  116. ath10k_warn(ar, "command disallowed\n");
  117. return -EBUSY;
  118. }
  119. while (length) {
  120. txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
  121. /* copy before roundup to avoid reading beyond buffer*/
  122. memcpy(cmd.write_mem.payload, buffer, txlen);
  123. txlen = roundup(txlen, 4);
  124. cmd.id = __cpu_to_le32(BMI_WRITE_MEMORY);
  125. cmd.write_mem.addr = __cpu_to_le32(address);
  126. cmd.write_mem.len = __cpu_to_le32(txlen);
  127. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
  128. NULL, NULL);
  129. if (ret) {
  130. ath10k_warn(ar, "unable to write to the device (%d)\n",
  131. ret);
  132. return ret;
  133. }
  134. /* fixup roundup() so `length` zeroes out for last chunk */
  135. txlen = min(txlen, length);
  136. address += txlen;
  137. buffer += txlen;
  138. length -= txlen;
  139. }
  140. return 0;
  141. }
  142. int ath10k_bmi_execute(struct ath10k *ar, u32 address, u32 param, u32 *result)
  143. {
  144. struct bmi_cmd cmd;
  145. union bmi_resp resp;
  146. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.execute);
  147. u32 resplen = sizeof(resp.execute);
  148. int ret;
  149. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi execute address 0x%x param 0x%x\n",
  150. address, param);
  151. if (ar->bmi.done_sent) {
  152. ath10k_warn(ar, "command disallowed\n");
  153. return -EBUSY;
  154. }
  155. cmd.id = __cpu_to_le32(BMI_EXECUTE);
  156. cmd.execute.addr = __cpu_to_le32(address);
  157. cmd.execute.param = __cpu_to_le32(param);
  158. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
  159. if (ret) {
  160. ath10k_warn(ar, "unable to read from the device\n");
  161. return ret;
  162. }
  163. if (resplen < sizeof(resp.execute)) {
  164. ath10k_warn(ar, "invalid execute response length (%d)\n",
  165. resplen);
  166. return -EIO;
  167. }
  168. *result = __le32_to_cpu(resp.execute.result);
  169. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi execute result 0x%x\n", *result);
  170. return 0;
  171. }
  172. int ath10k_bmi_lz_data(struct ath10k *ar, const void *buffer, u32 length)
  173. {
  174. struct bmi_cmd cmd;
  175. u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.lz_data);
  176. u32 txlen;
  177. int ret;
  178. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi lz data buffer 0x%p length %d\n",
  179. buffer, length);
  180. if (ar->bmi.done_sent) {
  181. ath10k_warn(ar, "command disallowed\n");
  182. return -EBUSY;
  183. }
  184. while (length) {
  185. txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
  186. WARN_ON_ONCE(txlen & 3);
  187. cmd.id = __cpu_to_le32(BMI_LZ_DATA);
  188. cmd.lz_data.len = __cpu_to_le32(txlen);
  189. memcpy(cmd.lz_data.payload, buffer, txlen);
  190. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
  191. NULL, NULL);
  192. if (ret) {
  193. ath10k_warn(ar, "unable to write to the device\n");
  194. return ret;
  195. }
  196. buffer += txlen;
  197. length -= txlen;
  198. }
  199. return 0;
  200. }
  201. int ath10k_bmi_lz_stream_start(struct ath10k *ar, u32 address)
  202. {
  203. struct bmi_cmd cmd;
  204. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.lz_start);
  205. int ret;
  206. ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi lz stream start address 0x%x\n",
  207. address);
  208. if (ar->bmi.done_sent) {
  209. ath10k_warn(ar, "command disallowed\n");
  210. return -EBUSY;
  211. }
  212. cmd.id = __cpu_to_le32(BMI_LZ_STREAM_START);
  213. cmd.lz_start.addr = __cpu_to_le32(address);
  214. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
  215. if (ret) {
  216. ath10k_warn(ar, "unable to Start LZ Stream to the device\n");
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. int ath10k_bmi_fast_download(struct ath10k *ar,
  222. u32 address, const void *buffer, u32 length)
  223. {
  224. u8 trailer[4] = {};
  225. u32 head_len = rounddown(length, 4);
  226. u32 trailer_len = length - head_len;
  227. int ret;
  228. ath10k_dbg(ar, ATH10K_DBG_BMI,
  229. "bmi fast download address 0x%x buffer 0x%p length %d\n",
  230. address, buffer, length);
  231. ret = ath10k_bmi_lz_stream_start(ar, address);
  232. if (ret)
  233. return ret;
  234. /* copy the last word into a zero padded buffer */
  235. if (trailer_len > 0)
  236. memcpy(trailer, buffer + head_len, trailer_len);
  237. ret = ath10k_bmi_lz_data(ar, buffer, head_len);
  238. if (ret)
  239. return ret;
  240. if (trailer_len > 0)
  241. ret = ath10k_bmi_lz_data(ar, trailer, 4);
  242. if (ret != 0)
  243. return ret;
  244. /*
  245. * Close compressed stream and open a new (fake) one.
  246. * This serves mainly to flush Target caches.
  247. */
  248. ret = ath10k_bmi_lz_stream_start(ar, 0x00);
  249. return ret;
  250. }