2 * Copyright (c) 2004-2011 Atheros Communications Inc.
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.
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.
22 int ath6kl_bmi_done(struct ath6kl *ar)
27 if (ar->bmi.done_sent) {
28 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi done skipped\n");
32 ar->bmi.done_sent = true;
34 ret = ath6kl_hif_bmi_write(ar, (u8 *)&cid, sizeof(cid));
36 ath6kl_err("Unable to send bmi done: %d\n", ret);
43 int ath6kl_bmi_get_target_info(struct ath6kl *ar,
44 struct ath6kl_bmi_target_info *targ_info)
47 u32 cid = BMI_GET_TARGET_INFO;
49 if (ar->bmi.done_sent) {
50 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
54 ret = ath6kl_hif_bmi_write(ar, (u8 *)&cid, sizeof(cid));
56 ath6kl_err("Unable to send get target info: %d\n", ret);
60 if (ar->hif_type == ATH6KL_HIF_TYPE_USB) {
61 ret = ath6kl_hif_bmi_read(ar, (u8 *)targ_info,
64 ret = ath6kl_hif_bmi_read(ar, (u8 *)&targ_info->version,
65 sizeof(targ_info->version));
69 ath6kl_err("Unable to recv target info: %d\n", ret);
73 if (le32_to_cpu(targ_info->version) == TARGET_VERSION_SENTINAL) {
74 /* Determine how many bytes are in the Target's targ_info */
75 ret = ath6kl_hif_bmi_read(ar,
76 (u8 *)&targ_info->byte_count,
77 sizeof(targ_info->byte_count));
79 ath6kl_err("unable to read target info byte count: %d\n",
85 * The target's targ_info doesn't match the host's targ_info.
86 * We need to do some backwards compatibility to make this work.
88 if (le32_to_cpu(targ_info->byte_count) != sizeof(*targ_info)) {
93 /* Read the remainder of the targ_info */
94 ret = ath6kl_hif_bmi_read(ar,
96 sizeof(targ_info->byte_count),
98 sizeof(targ_info->byte_count));
101 ath6kl_err("Unable to read target info (%d bytes): %d\n",
102 targ_info->byte_count, ret);
107 ath6kl_dbg(ATH6KL_DBG_BMI, "target info (ver: 0x%x type: 0x%x)\n",
108 targ_info->version, targ_info->type);
113 int ath6kl_bmi_read(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
115 u32 cid = BMI_READ_MEMORY;
118 u32 len_remain, rx_len;
121 if (ar->bmi.done_sent) {
122 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
126 size = ar->bmi.max_data_size + sizeof(cid) + sizeof(addr) + sizeof(len);
127 if (size > ar->bmi.max_cmd_size) {
131 memset(ar->bmi.cmd_buf, 0, size);
133 ath6kl_dbg(ATH6KL_DBG_BMI,
134 "bmi read memory: device: addr: 0x%x, len: %d\n",
140 rx_len = (len_remain < ar->bmi.max_data_size) ?
141 len_remain : ar->bmi.max_data_size;
143 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
144 offset += sizeof(cid);
145 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
146 offset += sizeof(addr);
147 memcpy(&(ar->bmi.cmd_buf[offset]), &rx_len, sizeof(rx_len));
148 offset += sizeof(len);
150 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
152 ath6kl_err("Unable to write to the device: %d\n",
156 ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, rx_len);
158 ath6kl_err("Unable to read from the device: %d\n",
162 memcpy(&buf[len - len_remain], ar->bmi.cmd_buf, rx_len);
163 len_remain -= rx_len; addr += rx_len;
169 int ath6kl_bmi_write(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
171 u32 cid = BMI_WRITE_MEMORY;
174 u32 len_remain, tx_len;
175 const u32 header = sizeof(cid) + sizeof(addr) + sizeof(len);
179 if (ar->bmi.done_sent) {
180 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
184 if ((ar->bmi.max_data_size + header) > ar->bmi.max_cmd_size) {
189 if (WARN_ON(ar->bmi.max_data_size > sizeof(aligned_buf)))
192 memset(ar->bmi.cmd_buf, 0, ar->bmi.max_data_size + header);
194 ath6kl_dbg(ATH6KL_DBG_BMI,
195 "bmi write memory: addr: 0x%x, len: %d\n", addr, len);
199 src = &buf[len - len_remain];
201 if (len_remain < (ar->bmi.max_data_size - header)) {
202 if (len_remain & 3) {
203 /* align it with 4 bytes */
204 len_remain = len_remain +
205 (4 - (len_remain & 3));
206 memcpy(aligned_buf, src, len_remain);
211 tx_len = (ar->bmi.max_data_size - header);
215 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
216 offset += sizeof(cid);
217 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
218 offset += sizeof(addr);
219 memcpy(&(ar->bmi.cmd_buf[offset]), &tx_len, sizeof(tx_len));
220 offset += sizeof(tx_len);
221 memcpy(&(ar->bmi.cmd_buf[offset]), src, tx_len);
224 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
226 ath6kl_err("Unable to write to the device: %d\n",
230 len_remain -= tx_len; addr += tx_len;
236 int ath6kl_bmi_execute(struct ath6kl *ar, u32 addr, u32 *param)
238 u32 cid = BMI_EXECUTE;
243 if (ar->bmi.done_sent) {
244 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
248 size = sizeof(cid) + sizeof(addr) + sizeof(param);
249 if (size > ar->bmi.max_cmd_size) {
253 memset(ar->bmi.cmd_buf, 0, size);
255 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi execute: addr: 0x%x, param: %d)\n",
259 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
260 offset += sizeof(cid);
261 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
262 offset += sizeof(addr);
263 memcpy(&(ar->bmi.cmd_buf[offset]), param, sizeof(*param));
264 offset += sizeof(*param);
266 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
268 ath6kl_err("Unable to write to the device: %d\n", ret);
272 ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, sizeof(*param));
274 ath6kl_err("Unable to read from the device: %d\n", ret);
278 memcpy(param, ar->bmi.cmd_buf, sizeof(*param));
283 int ath6kl_bmi_set_app_start(struct ath6kl *ar, u32 addr)
285 u32 cid = BMI_SET_APP_START;
290 if (ar->bmi.done_sent) {
291 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
295 size = sizeof(cid) + sizeof(addr);
296 if (size > ar->bmi.max_cmd_size) {
300 memset(ar->bmi.cmd_buf, 0, size);
302 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi set app start: addr: 0x%x\n", addr);
305 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
306 offset += sizeof(cid);
307 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
308 offset += sizeof(addr);
310 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
312 ath6kl_err("Unable to write to the device: %d\n", ret);
319 int ath6kl_bmi_reg_read(struct ath6kl *ar, u32 addr, u32 *param)
321 u32 cid = BMI_READ_SOC_REGISTER;
326 if (ar->bmi.done_sent) {
327 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
331 size = sizeof(cid) + sizeof(addr);
332 if (size > ar->bmi.max_cmd_size) {
336 memset(ar->bmi.cmd_buf, 0, size);
338 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi read SOC reg: addr: 0x%x\n", addr);
341 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
342 offset += sizeof(cid);
343 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
344 offset += sizeof(addr);
346 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
348 ath6kl_err("Unable to write to the device: %d\n", ret);
352 ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, sizeof(*param));
354 ath6kl_err("Unable to read from the device: %d\n", ret);
357 memcpy(param, ar->bmi.cmd_buf, sizeof(*param));
362 int ath6kl_bmi_reg_write(struct ath6kl *ar, u32 addr, u32 param)
364 u32 cid = BMI_WRITE_SOC_REGISTER;
369 if (ar->bmi.done_sent) {
370 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
374 size = sizeof(cid) + sizeof(addr) + sizeof(param);
375 if (size > ar->bmi.max_cmd_size) {
379 memset(ar->bmi.cmd_buf, 0, size);
381 ath6kl_dbg(ATH6KL_DBG_BMI,
382 "bmi write SOC reg: addr: 0x%x, param: %d\n",
386 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
387 offset += sizeof(cid);
388 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
389 offset += sizeof(addr);
390 memcpy(&(ar->bmi.cmd_buf[offset]), ¶m, sizeof(param));
391 offset += sizeof(param);
393 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
395 ath6kl_err("Unable to write to the device: %d\n", ret);
402 int ath6kl_bmi_lz_data(struct ath6kl *ar, u8 *buf, u32 len)
404 u32 cid = BMI_LZ_DATA;
407 u32 len_remain, tx_len;
408 const u32 header = sizeof(cid) + sizeof(len);
411 if (ar->bmi.done_sent) {
412 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
416 size = ar->bmi.max_data_size + header;
417 if (size > ar->bmi.max_cmd_size) {
421 memset(ar->bmi.cmd_buf, 0, size);
423 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi send LZ data: len: %d)\n",
428 tx_len = (len_remain < (ar->bmi.max_data_size - header)) ?
429 len_remain : (ar->bmi.max_data_size - header);
432 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
433 offset += sizeof(cid);
434 memcpy(&(ar->bmi.cmd_buf[offset]), &tx_len, sizeof(tx_len));
435 offset += sizeof(tx_len);
436 memcpy(&(ar->bmi.cmd_buf[offset]), &buf[len - len_remain],
440 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
442 ath6kl_err("Unable to write to the device: %d\n",
447 len_remain -= tx_len;
453 int ath6kl_bmi_lz_stream_start(struct ath6kl *ar, u32 addr)
455 u32 cid = BMI_LZ_STREAM_START;
460 if (ar->bmi.done_sent) {
461 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
465 size = sizeof(cid) + sizeof(addr);
466 if (size > ar->bmi.max_cmd_size) {
470 memset(ar->bmi.cmd_buf, 0, size);
472 ath6kl_dbg(ATH6KL_DBG_BMI,
473 "bmi LZ stream start: addr: 0x%x)\n",
477 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
478 offset += sizeof(cid);
479 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
480 offset += sizeof(addr);
482 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
484 ath6kl_err("Unable to start LZ stream to the device: %d\n",
492 int ath6kl_bmi_fast_download(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
496 u32 last_word_offset = len & ~0x3;
497 u32 unaligned_bytes = len & 0x3;
499 ret = ath6kl_bmi_lz_stream_start(ar, addr);
503 if (unaligned_bytes) {
504 /* copy the last word into a zero padded buffer */
505 memcpy(&last_word, &buf[last_word_offset], unaligned_bytes);
508 ret = ath6kl_bmi_lz_data(ar, buf, last_word_offset);
513 ret = ath6kl_bmi_lz_data(ar, (u8 *)&last_word, 4);
516 /* Close compressed stream and open a new (fake) one.
517 * This serves mainly to flush Target caches. */
518 ret = ath6kl_bmi_lz_stream_start(ar, 0x00);
523 void ath6kl_bmi_reset(struct ath6kl *ar)
525 ar->bmi.done_sent = false;
528 int ath6kl_bmi_init(struct ath6kl *ar)
530 if (WARN_ON(ar->bmi.max_data_size == 0))
533 /* cmd + addr + len + data_size */
534 ar->bmi.max_cmd_size = ar->bmi.max_data_size + (sizeof(u32) * 3);
536 ar->bmi.cmd_buf = kzalloc(ar->bmi.max_cmd_size, GFP_ATOMIC);
537 if (!ar->bmi.cmd_buf)
543 void ath6kl_bmi_cleanup(struct ath6kl *ar)
545 kfree(ar->bmi.cmd_buf);
546 ar->bmi.cmd_buf = NULL;