bmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * Copyright (c) 2004-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. #include "core.h"
  18. #include "hif-ops.h"
  19. #include "target.h"
  20. #include "debug.h"
  21. int ath6kl_bmi_done(struct ath6kl *ar)
  22. {
  23. int ret;
  24. u32 cid = BMI_DONE;
  25. if (ar->bmi.done_sent) {
  26. ath6kl_dbg(ATH6KL_DBG_BMI, "bmi done skipped\n");
  27. return 0;
  28. }
  29. ar->bmi.done_sent = true;
  30. ret = ath6kl_hif_bmi_write(ar, (u8 *)&cid, sizeof(cid));
  31. if (ret) {
  32. ath6kl_err("Unable to send bmi done: %d\n", ret);
  33. return ret;
  34. }
  35. return 0;
  36. }
  37. int ath6kl_bmi_get_target_info(struct ath6kl *ar,
  38. struct ath6kl_bmi_target_info *targ_info)
  39. {
  40. int ret;
  41. u32 cid = BMI_GET_TARGET_INFO;
  42. if (ar->bmi.done_sent) {
  43. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  44. return -EACCES;
  45. }
  46. ret = ath6kl_hif_bmi_write(ar, (u8 *)&cid, sizeof(cid));
  47. if (ret) {
  48. ath6kl_err("Unable to send get target info: %d\n", ret);
  49. return ret;
  50. }
  51. if (ar->hif_type == ATH6KL_HIF_TYPE_USB) {
  52. ret = ath6kl_hif_bmi_read(ar, (u8 *)targ_info,
  53. sizeof(*targ_info));
  54. } else {
  55. ret = ath6kl_hif_bmi_read(ar, (u8 *)&targ_info->version,
  56. sizeof(targ_info->version));
  57. }
  58. if (ret) {
  59. ath6kl_err("Unable to recv target info: %d\n", ret);
  60. return ret;
  61. }
  62. if (le32_to_cpu(targ_info->version) == TARGET_VERSION_SENTINAL) {
  63. /* Determine how many bytes are in the Target's targ_info */
  64. ret = ath6kl_hif_bmi_read(ar,
  65. (u8 *)&targ_info->byte_count,
  66. sizeof(targ_info->byte_count));
  67. if (ret) {
  68. ath6kl_err("unable to read target info byte count: %d\n",
  69. ret);
  70. return ret;
  71. }
  72. /*
  73. * The target's targ_info doesn't match the host's targ_info.
  74. * We need to do some backwards compatibility to make this work.
  75. */
  76. if (le32_to_cpu(targ_info->byte_count) != sizeof(*targ_info)) {
  77. WARN_ON(1);
  78. return -EINVAL;
  79. }
  80. /* Read the remainder of the targ_info */
  81. ret = ath6kl_hif_bmi_read(ar,
  82. ((u8 *)targ_info) +
  83. sizeof(targ_info->byte_count),
  84. sizeof(*targ_info) -
  85. sizeof(targ_info->byte_count));
  86. if (ret) {
  87. ath6kl_err("Unable to read target info (%d bytes): %d\n",
  88. targ_info->byte_count, ret);
  89. return ret;
  90. }
  91. }
  92. ath6kl_dbg(ATH6KL_DBG_BMI, "target info (ver: 0x%x type: 0x%x)\n",
  93. targ_info->version, targ_info->type);
  94. return 0;
  95. }
  96. int ath6kl_bmi_read(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
  97. {
  98. u32 cid = BMI_READ_MEMORY;
  99. int ret;
  100. u32 offset;
  101. u32 len_remain, rx_len;
  102. u16 size;
  103. if (ar->bmi.done_sent) {
  104. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  105. return -EACCES;
  106. }
  107. size = ar->bmi.max_data_size + sizeof(cid) + sizeof(addr) + sizeof(len);
  108. if (size > ar->bmi.max_cmd_size) {
  109. WARN_ON(1);
  110. return -EINVAL;
  111. }
  112. memset(ar->bmi.cmd_buf, 0, size);
  113. ath6kl_dbg(ATH6KL_DBG_BMI,
  114. "bmi read memory: device: addr: 0x%x, len: %d\n",
  115. addr, len);
  116. len_remain = len;
  117. while (len_remain) {
  118. rx_len = (len_remain < ar->bmi.max_data_size) ?
  119. len_remain : ar->bmi.max_data_size;
  120. offset = 0;
  121. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  122. offset += sizeof(cid);
  123. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  124. offset += sizeof(addr);
  125. memcpy(&(ar->bmi.cmd_buf[offset]), &rx_len, sizeof(rx_len));
  126. offset += sizeof(len);
  127. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  128. if (ret) {
  129. ath6kl_err("Unable to write to the device: %d\n",
  130. ret);
  131. return ret;
  132. }
  133. ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, rx_len);
  134. if (ret) {
  135. ath6kl_err("Unable to read from the device: %d\n",
  136. ret);
  137. return ret;
  138. }
  139. memcpy(&buf[len - len_remain], ar->bmi.cmd_buf, rx_len);
  140. len_remain -= rx_len; addr += rx_len;
  141. }
  142. return 0;
  143. }
  144. int ath6kl_bmi_write(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
  145. {
  146. u32 cid = BMI_WRITE_MEMORY;
  147. int ret;
  148. u32 offset;
  149. u32 len_remain, tx_len;
  150. const u32 header = sizeof(cid) + sizeof(addr) + sizeof(len);
  151. u8 aligned_buf[400];
  152. u8 *src;
  153. if (ar->bmi.done_sent) {
  154. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  155. return -EACCES;
  156. }
  157. if ((ar->bmi.max_data_size + header) > ar->bmi.max_cmd_size) {
  158. WARN_ON(1);
  159. return -EINVAL;
  160. }
  161. if (WARN_ON(ar->bmi.max_data_size > sizeof(aligned_buf)))
  162. return -E2BIG;
  163. memset(ar->bmi.cmd_buf, 0, ar->bmi.max_data_size + header);
  164. ath6kl_dbg(ATH6KL_DBG_BMI,
  165. "bmi write memory: addr: 0x%x, len: %d\n", addr, len);
  166. len_remain = len;
  167. while (len_remain) {
  168. src = &buf[len - len_remain];
  169. if (len_remain < (ar->bmi.max_data_size - header)) {
  170. if (len_remain & 3) {
  171. /* align it with 4 bytes */
  172. len_remain = len_remain +
  173. (4 - (len_remain & 3));
  174. memcpy(aligned_buf, src, len_remain);
  175. src = aligned_buf;
  176. }
  177. tx_len = len_remain;
  178. } else {
  179. tx_len = (ar->bmi.max_data_size - header);
  180. }
  181. offset = 0;
  182. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  183. offset += sizeof(cid);
  184. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  185. offset += sizeof(addr);
  186. memcpy(&(ar->bmi.cmd_buf[offset]), &tx_len, sizeof(tx_len));
  187. offset += sizeof(tx_len);
  188. memcpy(&(ar->bmi.cmd_buf[offset]), src, tx_len);
  189. offset += tx_len;
  190. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  191. if (ret) {
  192. ath6kl_err("Unable to write to the device: %d\n",
  193. ret);
  194. return ret;
  195. }
  196. len_remain -= tx_len; addr += tx_len;
  197. }
  198. return 0;
  199. }
  200. int ath6kl_bmi_execute(struct ath6kl *ar, u32 addr, u32 *param)
  201. {
  202. u32 cid = BMI_EXECUTE;
  203. int ret;
  204. u32 offset;
  205. u16 size;
  206. if (ar->bmi.done_sent) {
  207. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  208. return -EACCES;
  209. }
  210. size = sizeof(cid) + sizeof(addr) + sizeof(param);
  211. if (size > ar->bmi.max_cmd_size) {
  212. WARN_ON(1);
  213. return -EINVAL;
  214. }
  215. memset(ar->bmi.cmd_buf, 0, size);
  216. ath6kl_dbg(ATH6KL_DBG_BMI, "bmi execute: addr: 0x%x, param: %d)\n",
  217. addr, *param);
  218. offset = 0;
  219. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  220. offset += sizeof(cid);
  221. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  222. offset += sizeof(addr);
  223. memcpy(&(ar->bmi.cmd_buf[offset]), param, sizeof(*param));
  224. offset += sizeof(*param);
  225. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  226. if (ret) {
  227. ath6kl_err("Unable to write to the device: %d\n", ret);
  228. return ret;
  229. }
  230. ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, sizeof(*param));
  231. if (ret) {
  232. ath6kl_err("Unable to read from the device: %d\n", ret);
  233. return ret;
  234. }
  235. memcpy(param, ar->bmi.cmd_buf, sizeof(*param));
  236. return 0;
  237. }
  238. int ath6kl_bmi_set_app_start(struct ath6kl *ar, u32 addr)
  239. {
  240. u32 cid = BMI_SET_APP_START;
  241. int ret;
  242. u32 offset;
  243. u16 size;
  244. if (ar->bmi.done_sent) {
  245. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  246. return -EACCES;
  247. }
  248. size = sizeof(cid) + sizeof(addr);
  249. if (size > ar->bmi.max_cmd_size) {
  250. WARN_ON(1);
  251. return -EINVAL;
  252. }
  253. memset(ar->bmi.cmd_buf, 0, size);
  254. ath6kl_dbg(ATH6KL_DBG_BMI, "bmi set app start: addr: 0x%x\n", addr);
  255. offset = 0;
  256. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  257. offset += sizeof(cid);
  258. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  259. offset += sizeof(addr);
  260. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  261. if (ret) {
  262. ath6kl_err("Unable to write to the device: %d\n", ret);
  263. return ret;
  264. }
  265. return 0;
  266. }
  267. int ath6kl_bmi_reg_read(struct ath6kl *ar, u32 addr, u32 *param)
  268. {
  269. u32 cid = BMI_READ_SOC_REGISTER;
  270. int ret;
  271. u32 offset;
  272. u16 size;
  273. if (ar->bmi.done_sent) {
  274. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  275. return -EACCES;
  276. }
  277. size = sizeof(cid) + sizeof(addr);
  278. if (size > ar->bmi.max_cmd_size) {
  279. WARN_ON(1);
  280. return -EINVAL;
  281. }
  282. memset(ar->bmi.cmd_buf, 0, size);
  283. ath6kl_dbg(ATH6KL_DBG_BMI, "bmi read SOC reg: addr: 0x%x\n", addr);
  284. offset = 0;
  285. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  286. offset += sizeof(cid);
  287. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  288. offset += sizeof(addr);
  289. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  290. if (ret) {
  291. ath6kl_err("Unable to write to the device: %d\n", ret);
  292. return ret;
  293. }
  294. ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, sizeof(*param));
  295. if (ret) {
  296. ath6kl_err("Unable to read from the device: %d\n", ret);
  297. return ret;
  298. }
  299. memcpy(param, ar->bmi.cmd_buf, sizeof(*param));
  300. return 0;
  301. }
  302. int ath6kl_bmi_reg_write(struct ath6kl *ar, u32 addr, u32 param)
  303. {
  304. u32 cid = BMI_WRITE_SOC_REGISTER;
  305. int ret;
  306. u32 offset;
  307. u16 size;
  308. if (ar->bmi.done_sent) {
  309. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  310. return -EACCES;
  311. }
  312. size = sizeof(cid) + sizeof(addr) + sizeof(param);
  313. if (size > ar->bmi.max_cmd_size) {
  314. WARN_ON(1);
  315. return -EINVAL;
  316. }
  317. memset(ar->bmi.cmd_buf, 0, size);
  318. ath6kl_dbg(ATH6KL_DBG_BMI,
  319. "bmi write SOC reg: addr: 0x%x, param: %d\n",
  320. addr, param);
  321. offset = 0;
  322. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  323. offset += sizeof(cid);
  324. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  325. offset += sizeof(addr);
  326. memcpy(&(ar->bmi.cmd_buf[offset]), &param, sizeof(param));
  327. offset += sizeof(param);
  328. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  329. if (ret) {
  330. ath6kl_err("Unable to write to the device: %d\n", ret);
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. int ath6kl_bmi_lz_data(struct ath6kl *ar, u8 *buf, u32 len)
  336. {
  337. u32 cid = BMI_LZ_DATA;
  338. int ret;
  339. u32 offset;
  340. u32 len_remain, tx_len;
  341. const u32 header = sizeof(cid) + sizeof(len);
  342. u16 size;
  343. if (ar->bmi.done_sent) {
  344. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  345. return -EACCES;
  346. }
  347. size = ar->bmi.max_data_size + header;
  348. if (size > ar->bmi.max_cmd_size) {
  349. WARN_ON(1);
  350. return -EINVAL;
  351. }
  352. memset(ar->bmi.cmd_buf, 0, size);
  353. ath6kl_dbg(ATH6KL_DBG_BMI, "bmi send LZ data: len: %d)\n",
  354. len);
  355. len_remain = len;
  356. while (len_remain) {
  357. tx_len = (len_remain < (ar->bmi.max_data_size - header)) ?
  358. len_remain : (ar->bmi.max_data_size - header);
  359. offset = 0;
  360. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  361. offset += sizeof(cid);
  362. memcpy(&(ar->bmi.cmd_buf[offset]), &tx_len, sizeof(tx_len));
  363. offset += sizeof(tx_len);
  364. memcpy(&(ar->bmi.cmd_buf[offset]), &buf[len - len_remain],
  365. tx_len);
  366. offset += tx_len;
  367. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  368. if (ret) {
  369. ath6kl_err("Unable to write to the device: %d\n",
  370. ret);
  371. return ret;
  372. }
  373. len_remain -= tx_len;
  374. }
  375. return 0;
  376. }
  377. int ath6kl_bmi_lz_stream_start(struct ath6kl *ar, u32 addr)
  378. {
  379. u32 cid = BMI_LZ_STREAM_START;
  380. int ret;
  381. u32 offset;
  382. u16 size;
  383. if (ar->bmi.done_sent) {
  384. ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
  385. return -EACCES;
  386. }
  387. size = sizeof(cid) + sizeof(addr);
  388. if (size > ar->bmi.max_cmd_size) {
  389. WARN_ON(1);
  390. return -EINVAL;
  391. }
  392. memset(ar->bmi.cmd_buf, 0, size);
  393. ath6kl_dbg(ATH6KL_DBG_BMI,
  394. "bmi LZ stream start: addr: 0x%x)\n",
  395. addr);
  396. offset = 0;
  397. memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
  398. offset += sizeof(cid);
  399. memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
  400. offset += sizeof(addr);
  401. ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
  402. if (ret) {
  403. ath6kl_err("Unable to start LZ stream to the device: %d\n",
  404. ret);
  405. return ret;
  406. }
  407. return 0;
  408. }
  409. int ath6kl_bmi_fast_download(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
  410. {
  411. int ret;
  412. u32 last_word = 0;
  413. u32 last_word_offset = len & ~0x3;
  414. u32 unaligned_bytes = len & 0x3;
  415. ret = ath6kl_bmi_lz_stream_start(ar, addr);
  416. if (ret)
  417. return ret;
  418. if (unaligned_bytes) {
  419. /* copy the last word into a zero padded buffer */
  420. memcpy(&last_word, &buf[last_word_offset], unaligned_bytes);
  421. }
  422. ret = ath6kl_bmi_lz_data(ar, buf, last_word_offset);
  423. if (ret)
  424. return ret;
  425. if (unaligned_bytes)
  426. ret = ath6kl_bmi_lz_data(ar, (u8 *)&last_word, 4);
  427. if (!ret) {
  428. /* Close compressed stream and open a new (fake) one.
  429. * This serves mainly to flush Target caches. */
  430. ret = ath6kl_bmi_lz_stream_start(ar, 0x00);
  431. }
  432. return ret;
  433. }
  434. void ath6kl_bmi_reset(struct ath6kl *ar)
  435. {
  436. ar->bmi.done_sent = false;
  437. }
  438. int ath6kl_bmi_init(struct ath6kl *ar)
  439. {
  440. if (WARN_ON(ar->bmi.max_data_size == 0))
  441. return -EINVAL;
  442. /* cmd + addr + len + data_size */
  443. ar->bmi.max_cmd_size = ar->bmi.max_data_size + (sizeof(u32) * 3);
  444. ar->bmi.cmd_buf = kzalloc(ar->bmi.max_cmd_size, GFP_ATOMIC);
  445. if (!ar->bmi.cmd_buf)
  446. return -ENOMEM;
  447. return 0;
  448. }
  449. void ath6kl_bmi_cleanup(struct ath6kl *ar)
  450. {
  451. kfree(ar->bmi.cmd_buf);
  452. ar->bmi.cmd_buf = NULL;
  453. }