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 static int ath6kl_get_bmi_cmd_credits(struct ath6kl
*ar
)
25 unsigned long timeout
;
28 ar
->bmi
.cmd_credits
= 0;
30 /* Read the counter register to get the command credits */
31 addr
= COUNT_DEC_ADDRESS
+ (HTC_MAILBOX_NUM_MAX
+ ENDPOINT1
) * 4;
33 timeout
= jiffies
+ msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT
);
34 while (time_before(jiffies
, timeout
) && !ar
->bmi
.cmd_credits
) {
37 * Hit the credit counter with a 4-byte access, the first byte
38 * read will hit the counter and cause a decrement, while the
39 * remaining 3 bytes has no effect. The rationale behind this
40 * is to make all HIF accesses 4-byte aligned.
42 ret
= hif_read_write_sync(ar
, addr
,
43 (u8
*)&ar
->bmi
.cmd_credits
, 4,
44 HIF_RD_SYNC_BYTE_INC
);
46 ath6kl_err("Unable to decrement the command credit count register: %d\n",
51 /* The counter is only 8 bits.
52 * Ignore anything in the upper 3 bytes
54 ar
->bmi
.cmd_credits
&= 0xFF;
57 if (!ar
->bmi
.cmd_credits
) {
58 ath6kl_err("bmi communication timeout\n");
65 static int ath6kl_bmi_get_rx_lkahd(struct ath6kl
*ar
)
67 unsigned long timeout
;
71 timeout
= jiffies
+ msecs_to_jiffies(BMI_COMMUNICATION_TIMEOUT
);
72 while (time_before(jiffies
, timeout
) && !rx_word
) {
73 ret
= hif_read_write_sync(ar
, RX_LOOKAHEAD_VALID_ADDRESS
,
74 (u8
*)&rx_word
, sizeof(rx_word
),
75 HIF_RD_SYNC_BYTE_INC
);
77 ath6kl_err("unable to read RX_LOOKAHEAD_VALID\n");
81 /* all we really want is one bit */
82 rx_word
&= (1 << ENDPOINT1
);
86 ath6kl_err("bmi_recv_buf FIFO empty\n");
93 static int ath6kl_bmi_send_buf(struct ath6kl
*ar
, u8
*buf
, u32 len
)
98 ret
= ath6kl_get_bmi_cmd_credits(ar
);
102 addr
= ar
->mbox_info
.htc_addr
;
104 ret
= hif_read_write_sync(ar
, addr
, buf
, len
,
105 HIF_WR_SYNC_BYTE_INC
);
107 ath6kl_err("unable to send the bmi data to the device\n");
112 static int ath6kl_bmi_recv_buf(struct ath6kl
*ar
, u8
*buf
, u32 len
)
118 * During normal bootup, small reads may be required.
119 * Rather than issue an HIF Read and then wait as the Target
120 * adds successive bytes to the FIFO, we wait here until
121 * we know that response data is available.
123 * This allows us to cleanly timeout on an unexpected
124 * Target failure rather than risk problems at the HIF level.
125 * In particular, this avoids SDIO timeouts and possibly garbage
126 * data on some host controllers. And on an interconnect
127 * such as Compact Flash (as well as some SDIO masters) which
128 * does not provide any indication on data timeout, it avoids
129 * a potential hang or garbage response.
131 * Synchronization is more difficult for reads larger than the
132 * size of the MBOX FIFO (128B), because the Target is unable
133 * to push the 129th byte of data until AFTER the Host posts an
134 * HIF Read and removes some FIFO data. So for large reads the
135 * Host proceeds to post an HIF Read BEFORE all the data is
136 * actually available to read. Fortunately, large BMI reads do
137 * not occur in practice -- they're supported for debug/development.
139 * So Host/Target BMI synchronization is divided into these cases:
143 * CASE 2: 4 <= length <= 128
144 * Wait for first 4 bytes to be in FIFO
145 * If CONSERVATIVE_BMI_READ is enabled, also wait for
146 * a BMI command credit, which indicates that the ENTIRE
147 * response is available in the the FIFO
149 * CASE 3: length > 128
150 * Wait for the first 4 bytes to be in FIFO
152 * For most uses, a small timeout should be sufficient and we will
153 * usually see a response quickly; but there may be some unusual
154 * (debug) cases of BMI_EXECUTE where we want an larger timeout.
155 * For now, we use an unbounded busy loop while waiting for
158 * If BMI_EXECUTE ever needs to support longer-latency execution,
159 * especially in production, this code needs to be enhanced to sleep
160 * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
161 * a function of Host processor speed.
163 if (len
>= 4) { /* NB: Currently, always true */
164 ret
= ath6kl_bmi_get_rx_lkahd(ar
);
169 addr
= ar
->mbox_info
.htc_addr
;
170 ret
= hif_read_write_sync(ar
, addr
, buf
, len
,
171 HIF_RD_SYNC_BYTE_INC
);
173 ath6kl_err("Unable to read the bmi data from the device: %d\n",
181 int ath6kl_bmi_done(struct ath6kl
*ar
)
186 if (ar
->bmi
.done_sent
) {
187 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi done skipped\n");
191 ar
->bmi
.done_sent
= true;
193 ret
= ath6kl_bmi_send_buf(ar
, (u8
*)&cid
, sizeof(cid
));
195 ath6kl_err("Unable to send bmi done: %d\n", ret
);
202 int ath6kl_bmi_get_target_info(struct ath6kl
*ar
,
203 struct ath6kl_bmi_target_info
*targ_info
)
206 u32 cid
= BMI_GET_TARGET_INFO
;
208 if (ar
->bmi
.done_sent
) {
209 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
213 ret
= ath6kl_bmi_send_buf(ar
, (u8
*)&cid
, sizeof(cid
));
215 ath6kl_err("Unable to send get target info: %d\n", ret
);
219 ret
= ath6kl_bmi_recv_buf(ar
, (u8
*)&targ_info
->version
,
220 sizeof(targ_info
->version
));
222 ath6kl_err("Unable to recv target info: %d\n", ret
);
226 if (le32_to_cpu(targ_info
->version
) == TARGET_VERSION_SENTINAL
) {
227 /* Determine how many bytes are in the Target's targ_info */
228 ret
= ath6kl_bmi_recv_buf(ar
,
229 (u8
*)&targ_info
->byte_count
,
230 sizeof(targ_info
->byte_count
));
232 ath6kl_err("unable to read target info byte count: %d\n",
238 * The target's targ_info doesn't match the host's targ_info.
239 * We need to do some backwards compatibility to make this work.
241 if (le32_to_cpu(targ_info
->byte_count
) != sizeof(*targ_info
)) {
246 /* Read the remainder of the targ_info */
247 ret
= ath6kl_bmi_recv_buf(ar
,
249 sizeof(targ_info
->byte_count
),
251 sizeof(targ_info
->byte_count
));
254 ath6kl_err("Unable to read target info (%d bytes): %d\n",
255 targ_info
->byte_count
, ret
);
260 ath6kl_dbg(ATH6KL_DBG_BMI
, "target info (ver: 0x%x type: 0x%x)\n",
261 targ_info
->version
, targ_info
->type
);
266 int ath6kl_bmi_read(struct ath6kl
*ar
, u32 addr
, u8
*buf
, u32 len
)
268 u32 cid
= BMI_READ_MEMORY
;
271 u32 len_remain
, rx_len
;
274 if (ar
->bmi
.done_sent
) {
275 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
279 size
= BMI_DATASZ_MAX
+ sizeof(cid
) + sizeof(addr
) + sizeof(len
);
280 if (size
> MAX_BMI_CMDBUF_SZ
) {
284 memset(ar
->bmi
.cmd_buf
, 0, size
);
286 ath6kl_dbg(ATH6KL_DBG_BMI
,
287 "bmi read memory: device: addr: 0x%x, len: %d\n",
293 rx_len
= (len_remain
< BMI_DATASZ_MAX
) ?
294 len_remain
: BMI_DATASZ_MAX
;
296 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
297 offset
+= sizeof(cid
);
298 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
299 offset
+= sizeof(addr
);
300 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &rx_len
, sizeof(rx_len
));
301 offset
+= sizeof(len
);
303 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
305 ath6kl_err("Unable to write to the device: %d\n",
309 ret
= ath6kl_bmi_recv_buf(ar
, ar
->bmi
.cmd_buf
, rx_len
);
311 ath6kl_err("Unable to read from the device: %d\n",
315 memcpy(&buf
[len
- len_remain
], ar
->bmi
.cmd_buf
, rx_len
);
316 len_remain
-= rx_len
; addr
+= rx_len
;
322 int ath6kl_bmi_write(struct ath6kl
*ar
, u32 addr
, u8
*buf
, u32 len
)
324 u32 cid
= BMI_WRITE_MEMORY
;
327 u32 len_remain
, tx_len
;
328 const u32 header
= sizeof(cid
) + sizeof(addr
) + sizeof(len
);
329 u8 aligned_buf
[BMI_DATASZ_MAX
];
332 if (ar
->bmi
.done_sent
) {
333 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
337 if ((BMI_DATASZ_MAX
+ header
) > MAX_BMI_CMDBUF_SZ
) {
342 memset(ar
->bmi
.cmd_buf
, 0, BMI_DATASZ_MAX
+ header
);
344 ath6kl_dbg(ATH6KL_DBG_BMI
,
345 "bmi write memory: addr: 0x%x, len: %d\n", addr
, len
);
349 src
= &buf
[len
- len_remain
];
351 if (len_remain
< (BMI_DATASZ_MAX
- header
)) {
352 if (len_remain
& 3) {
353 /* align it with 4 bytes */
354 len_remain
= len_remain
+
355 (4 - (len_remain
& 3));
356 memcpy(aligned_buf
, src
, len_remain
);
361 tx_len
= (BMI_DATASZ_MAX
- header
);
365 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
366 offset
+= sizeof(cid
);
367 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
368 offset
+= sizeof(addr
);
369 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &tx_len
, sizeof(tx_len
));
370 offset
+= sizeof(tx_len
);
371 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), src
, tx_len
);
374 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
376 ath6kl_err("Unable to write to the device: %d\n",
380 len_remain
-= tx_len
; addr
+= tx_len
;
386 int ath6kl_bmi_execute(struct ath6kl
*ar
, u32 addr
, u32
*param
)
388 u32 cid
= BMI_EXECUTE
;
393 if (ar
->bmi
.done_sent
) {
394 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
398 size
= sizeof(cid
) + sizeof(addr
) + sizeof(param
);
399 if (size
> MAX_BMI_CMDBUF_SZ
) {
403 memset(ar
->bmi
.cmd_buf
, 0, size
);
405 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi execute: addr: 0x%x, param: %d)\n",
409 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
410 offset
+= sizeof(cid
);
411 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
412 offset
+= sizeof(addr
);
413 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), param
, sizeof(*param
));
414 offset
+= sizeof(*param
);
416 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
418 ath6kl_err("Unable to write to the device: %d\n", ret
);
422 ret
= ath6kl_bmi_recv_buf(ar
, ar
->bmi
.cmd_buf
, sizeof(*param
));
424 ath6kl_err("Unable to read from the device: %d\n", ret
);
428 memcpy(param
, ar
->bmi
.cmd_buf
, sizeof(*param
));
433 int ath6kl_bmi_set_app_start(struct ath6kl
*ar
, u32 addr
)
435 u32 cid
= BMI_SET_APP_START
;
440 if (ar
->bmi
.done_sent
) {
441 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
445 size
= sizeof(cid
) + sizeof(addr
);
446 if (size
> MAX_BMI_CMDBUF_SZ
) {
450 memset(ar
->bmi
.cmd_buf
, 0, size
);
452 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi set app start: addr: 0x%x\n", addr
);
455 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
456 offset
+= sizeof(cid
);
457 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
458 offset
+= sizeof(addr
);
460 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
462 ath6kl_err("Unable to write to the device: %d\n", ret
);
469 int ath6kl_bmi_reg_read(struct ath6kl
*ar
, u32 addr
, u32
*param
)
471 u32 cid
= BMI_READ_SOC_REGISTER
;
476 if (ar
->bmi
.done_sent
) {
477 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
481 size
= sizeof(cid
) + sizeof(addr
);
482 if (size
> MAX_BMI_CMDBUF_SZ
) {
486 memset(ar
->bmi
.cmd_buf
, 0, size
);
488 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi read SOC reg: addr: 0x%x\n", addr
);
491 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
492 offset
+= sizeof(cid
);
493 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
494 offset
+= sizeof(addr
);
496 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
498 ath6kl_err("Unable to write to the device: %d\n", ret
);
502 ret
= ath6kl_bmi_recv_buf(ar
, ar
->bmi
.cmd_buf
, sizeof(*param
));
504 ath6kl_err("Unable to read from the device: %d\n", ret
);
507 memcpy(param
, ar
->bmi
.cmd_buf
, sizeof(*param
));
512 int ath6kl_bmi_reg_write(struct ath6kl
*ar
, u32 addr
, u32 param
)
514 u32 cid
= BMI_WRITE_SOC_REGISTER
;
519 if (ar
->bmi
.done_sent
) {
520 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
524 size
= sizeof(cid
) + sizeof(addr
) + sizeof(param
);
525 if (size
> MAX_BMI_CMDBUF_SZ
) {
529 memset(ar
->bmi
.cmd_buf
, 0, size
);
531 ath6kl_dbg(ATH6KL_DBG_BMI
,
532 "bmi write SOC reg: addr: 0x%x, param: %d\n",
536 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
537 offset
+= sizeof(cid
);
538 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
539 offset
+= sizeof(addr
);
540 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), ¶m
, sizeof(param
));
541 offset
+= sizeof(param
);
543 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
545 ath6kl_err("Unable to write to the device: %d\n", ret
);
552 int ath6kl_bmi_lz_data(struct ath6kl
*ar
, u8
*buf
, u32 len
)
554 u32 cid
= BMI_LZ_DATA
;
557 u32 len_remain
, tx_len
;
558 const u32 header
= sizeof(cid
) + sizeof(len
);
561 if (ar
->bmi
.done_sent
) {
562 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
566 size
= BMI_DATASZ_MAX
+ header
;
567 if (size
> MAX_BMI_CMDBUF_SZ
) {
571 memset(ar
->bmi
.cmd_buf
, 0, size
);
573 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi send LZ data: len: %d)\n",
578 tx_len
= (len_remain
< (BMI_DATASZ_MAX
- header
)) ?
579 len_remain
: (BMI_DATASZ_MAX
- header
);
582 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
583 offset
+= sizeof(cid
);
584 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &tx_len
, sizeof(tx_len
));
585 offset
+= sizeof(tx_len
);
586 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &buf
[len
- len_remain
],
590 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
592 ath6kl_err("Unable to write to the device: %d\n",
597 len_remain
-= tx_len
;
603 int ath6kl_bmi_lz_stream_start(struct ath6kl
*ar
, u32 addr
)
605 u32 cid
= BMI_LZ_STREAM_START
;
610 if (ar
->bmi
.done_sent
) {
611 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
615 size
= sizeof(cid
) + sizeof(addr
);
616 if (size
> MAX_BMI_CMDBUF_SZ
) {
620 memset(ar
->bmi
.cmd_buf
, 0, size
);
622 ath6kl_dbg(ATH6KL_DBG_BMI
,
623 "bmi LZ stream start: addr: 0x%x)\n",
627 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
628 offset
+= sizeof(cid
);
629 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
630 offset
+= sizeof(addr
);
632 ret
= ath6kl_bmi_send_buf(ar
, ar
->bmi
.cmd_buf
, offset
);
634 ath6kl_err("Unable to start LZ stream to the device: %d\n",
642 int ath6kl_bmi_fast_download(struct ath6kl
*ar
, u32 addr
, u8
*buf
, u32 len
)
646 u32 last_word_offset
= len
& ~0x3;
647 u32 unaligned_bytes
= len
& 0x3;
649 ret
= ath6kl_bmi_lz_stream_start(ar
, addr
);
653 if (unaligned_bytes
) {
654 /* copy the last word into a zero padded buffer */
655 memcpy(&last_word
, &buf
[last_word_offset
], unaligned_bytes
);
658 ret
= ath6kl_bmi_lz_data(ar
, buf
, last_word_offset
);
663 ret
= ath6kl_bmi_lz_data(ar
, (u8
*)&last_word
, 4);
666 /* Close compressed stream and open a new (fake) one.
667 * This serves mainly to flush Target caches. */
668 ret
= ath6kl_bmi_lz_stream_start(ar
, 0x00);
673 void ath6kl_bmi_reset(struct ath6kl
*ar
)
675 ar
->bmi
.done_sent
= false;
678 int ath6kl_bmi_init(struct ath6kl
*ar
)
680 ar
->bmi
.cmd_buf
= kzalloc(MAX_BMI_CMDBUF_SZ
, GFP_ATOMIC
);
682 if (!ar
->bmi
.cmd_buf
)
688 void ath6kl_bmi_cleanup(struct ath6kl
*ar
)
690 kfree(ar
->bmi
.cmd_buf
);
691 ar
->bmi
.cmd_buf
= NULL
;