bmi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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(ATH10K_DBG_CORE, "BMI started\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. if (ar->bmi.done_sent) {
  32. ath10k_dbg(ATH10K_DBG_CORE, "%s skipped\n", __func__);
  33. return 0;
  34. }
  35. ar->bmi.done_sent = true;
  36. cmd.id = __cpu_to_le32(BMI_DONE);
  37. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
  38. if (ret) {
  39. ath10k_warn("unable to write to the device: %d\n", ret);
  40. return ret;
  41. }
  42. ath10k_dbg(ATH10K_DBG_CORE, "BMI done\n");
  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. if (ar->bmi.done_sent) {
  54. ath10k_warn("BMI Get Target Info Command disallowed\n");
  55. return -EBUSY;
  56. }
  57. cmd.id = __cpu_to_le32(BMI_GET_TARGET_INFO);
  58. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
  59. if (ret) {
  60. ath10k_warn("unable to get target info from device\n");
  61. return ret;
  62. }
  63. if (resplen < sizeof(resp.get_target_info)) {
  64. ath10k_warn("invalid get_target_info response length (%d)\n",
  65. resplen);
  66. return -EIO;
  67. }
  68. target_info->version = __le32_to_cpu(resp.get_target_info.version);
  69. target_info->type = __le32_to_cpu(resp.get_target_info.type);
  70. return 0;
  71. }
  72. int ath10k_bmi_read_memory(struct ath10k *ar,
  73. u32 address, void *buffer, u32 length)
  74. {
  75. struct bmi_cmd cmd;
  76. union bmi_resp resp;
  77. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.read_mem);
  78. u32 rxlen;
  79. int ret;
  80. if (ar->bmi.done_sent) {
  81. ath10k_warn("command disallowed\n");
  82. return -EBUSY;
  83. }
  84. ath10k_dbg(ATH10K_DBG_CORE,
  85. "%s: (device: 0x%p, address: 0x%x, length: %d)\n",
  86. __func__, ar, address, length);
  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("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. if (ar->bmi.done_sent) {
  114. ath10k_warn("command disallowed\n");
  115. return -EBUSY;
  116. }
  117. ath10k_dbg(ATH10K_DBG_CORE,
  118. "%s: (device: 0x%p, address: 0x%x, length: %d)\n",
  119. __func__, ar, address, length);
  120. while (length) {
  121. txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
  122. /* copy before roundup to avoid reading beyond buffer*/
  123. memcpy(cmd.write_mem.payload, buffer, txlen);
  124. txlen = roundup(txlen, 4);
  125. cmd.id = __cpu_to_le32(BMI_WRITE_MEMORY);
  126. cmd.write_mem.addr = __cpu_to_le32(address);
  127. cmd.write_mem.len = __cpu_to_le32(txlen);
  128. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
  129. NULL, NULL);
  130. if (ret) {
  131. ath10k_warn("unable to write to the device (%d)\n",
  132. ret);
  133. return ret;
  134. }
  135. /* fixup roundup() so `length` zeroes out for last chunk */
  136. txlen = min(txlen, length);
  137. address += txlen;
  138. buffer += txlen;
  139. length -= txlen;
  140. }
  141. return 0;
  142. }
  143. int ath10k_bmi_execute(struct ath10k *ar, u32 address, u32 *param)
  144. {
  145. struct bmi_cmd cmd;
  146. union bmi_resp resp;
  147. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.execute);
  148. u32 resplen = sizeof(resp.execute);
  149. int ret;
  150. if (ar->bmi.done_sent) {
  151. ath10k_warn("command disallowed\n");
  152. return -EBUSY;
  153. }
  154. ath10k_dbg(ATH10K_DBG_CORE,
  155. "%s: (device: 0x%p, address: 0x%x, param: %d)\n",
  156. __func__, ar, address, *param);
  157. cmd.id = __cpu_to_le32(BMI_EXECUTE);
  158. cmd.execute.addr = __cpu_to_le32(address);
  159. cmd.execute.param = __cpu_to_le32(*param);
  160. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
  161. if (ret) {
  162. ath10k_warn("unable to read from the device\n");
  163. return ret;
  164. }
  165. if (resplen < sizeof(resp.execute)) {
  166. ath10k_warn("invalid execute response length (%d)\n",
  167. resplen);
  168. return ret;
  169. }
  170. *param = __le32_to_cpu(resp.execute.result);
  171. return 0;
  172. }
  173. int ath10k_bmi_lz_data(struct ath10k *ar, const void *buffer, u32 length)
  174. {
  175. struct bmi_cmd cmd;
  176. u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.lz_data);
  177. u32 txlen;
  178. int ret;
  179. if (ar->bmi.done_sent) {
  180. ath10k_warn("command disallowed\n");
  181. return -EBUSY;
  182. }
  183. while (length) {
  184. txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
  185. WARN_ON_ONCE(txlen & 3);
  186. cmd.id = __cpu_to_le32(BMI_LZ_DATA);
  187. cmd.lz_data.len = __cpu_to_le32(txlen);
  188. memcpy(cmd.lz_data.payload, buffer, txlen);
  189. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
  190. NULL, NULL);
  191. if (ret) {
  192. ath10k_warn("unable to write to the device\n");
  193. return ret;
  194. }
  195. buffer += txlen;
  196. length -= txlen;
  197. }
  198. return 0;
  199. }
  200. int ath10k_bmi_lz_stream_start(struct ath10k *ar, u32 address)
  201. {
  202. struct bmi_cmd cmd;
  203. u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.lz_start);
  204. int ret;
  205. if (ar->bmi.done_sent) {
  206. ath10k_warn("command disallowed\n");
  207. return -EBUSY;
  208. }
  209. cmd.id = __cpu_to_le32(BMI_LZ_STREAM_START);
  210. cmd.lz_start.addr = __cpu_to_le32(address);
  211. ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
  212. if (ret) {
  213. ath10k_warn("unable to Start LZ Stream to the device\n");
  214. return ret;
  215. }
  216. return 0;
  217. }
  218. int ath10k_bmi_fast_download(struct ath10k *ar,
  219. u32 address, const void *buffer, u32 length)
  220. {
  221. u8 trailer[4] = {};
  222. u32 head_len = rounddown(length, 4);
  223. u32 trailer_len = length - head_len;
  224. int ret;
  225. ret = ath10k_bmi_lz_stream_start(ar, address);
  226. if (ret)
  227. return ret;
  228. /* copy the last word into a zero padded buffer */
  229. if (trailer_len > 0)
  230. memcpy(trailer, buffer + head_len, trailer_len);
  231. ret = ath10k_bmi_lz_data(ar, buffer, head_len);
  232. if (ret)
  233. return ret;
  234. if (trailer_len > 0)
  235. ret = ath10k_bmi_lz_data(ar, trailer, 4);
  236. if (ret != 0)
  237. return ret;
  238. /*
  239. * Close compressed stream and open a new (fake) one.
  240. * This serves mainly to flush Target caches.
  241. */
  242. ret = ath10k_bmi_lz_stream_start(ar, 0x00);
  243. return ret;
  244. }