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 ret
= ath6kl_hif_bmi_read(ar
, (u8
*)&targ_info
->version
,
61 sizeof(targ_info
->version
));
63 ath6kl_err("Unable to recv target info: %d\n", ret
);
67 if (le32_to_cpu(targ_info
->version
) == TARGET_VERSION_SENTINAL
) {
68 /* Determine how many bytes are in the Target's targ_info */
69 ret
= ath6kl_hif_bmi_read(ar
,
70 (u8
*)&targ_info
->byte_count
,
71 sizeof(targ_info
->byte_count
));
73 ath6kl_err("unable to read target info byte count: %d\n",
79 * The target's targ_info doesn't match the host's targ_info.
80 * We need to do some backwards compatibility to make this work.
82 if (le32_to_cpu(targ_info
->byte_count
) != sizeof(*targ_info
)) {
87 /* Read the remainder of the targ_info */
88 ret
= ath6kl_hif_bmi_read(ar
,
90 sizeof(targ_info
->byte_count
),
92 sizeof(targ_info
->byte_count
));
95 ath6kl_err("Unable to read target info (%d bytes): %d\n",
96 targ_info
->byte_count
, ret
);
101 ath6kl_dbg(ATH6KL_DBG_BMI
, "target info (ver: 0x%x type: 0x%x)\n",
102 targ_info
->version
, targ_info
->type
);
107 int ath6kl_bmi_read(struct ath6kl
*ar
, u32 addr
, u8
*buf
, u32 len
)
109 u32 cid
= BMI_READ_MEMORY
;
112 u32 len_remain
, rx_len
;
115 if (ar
->bmi
.done_sent
) {
116 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
120 size
= ar
->bmi
.max_data_size
+ sizeof(cid
) + sizeof(addr
) + sizeof(len
);
121 if (size
> ar
->bmi
.max_cmd_size
) {
125 memset(ar
->bmi
.cmd_buf
, 0, size
);
127 ath6kl_dbg(ATH6KL_DBG_BMI
,
128 "bmi read memory: device: addr: 0x%x, len: %d\n",
134 rx_len
= (len_remain
< ar
->bmi
.max_data_size
) ?
135 len_remain
: ar
->bmi
.max_data_size
;
137 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
138 offset
+= sizeof(cid
);
139 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
140 offset
+= sizeof(addr
);
141 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &rx_len
, sizeof(rx_len
));
142 offset
+= sizeof(len
);
144 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
146 ath6kl_err("Unable to write to the device: %d\n",
150 ret
= ath6kl_hif_bmi_read(ar
, ar
->bmi
.cmd_buf
, rx_len
);
152 ath6kl_err("Unable to read from the device: %d\n",
156 memcpy(&buf
[len
- len_remain
], ar
->bmi
.cmd_buf
, rx_len
);
157 len_remain
-= rx_len
; addr
+= rx_len
;
163 int ath6kl_bmi_write(struct ath6kl
*ar
, u32 addr
, u8
*buf
, u32 len
)
165 u32 cid
= BMI_WRITE_MEMORY
;
168 u32 len_remain
, tx_len
;
169 const u32 header
= sizeof(cid
) + sizeof(addr
) + sizeof(len
);
173 if (ar
->bmi
.done_sent
) {
174 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
178 if ((ar
->bmi
.max_data_size
+ header
) > ar
->bmi
.max_cmd_size
) {
183 if (WARN_ON(ar
->bmi
.max_data_size
> sizeof(aligned_buf
)))
186 memset(ar
->bmi
.cmd_buf
, 0, ar
->bmi
.max_data_size
+ header
);
188 ath6kl_dbg(ATH6KL_DBG_BMI
,
189 "bmi write memory: addr: 0x%x, len: %d\n", addr
, len
);
193 src
= &buf
[len
- len_remain
];
195 if (len_remain
< (ar
->bmi
.max_data_size
- header
)) {
196 if (len_remain
& 3) {
197 /* align it with 4 bytes */
198 len_remain
= len_remain
+
199 (4 - (len_remain
& 3));
200 memcpy(aligned_buf
, src
, len_remain
);
205 tx_len
= (ar
->bmi
.max_data_size
- header
);
209 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
210 offset
+= sizeof(cid
);
211 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
212 offset
+= sizeof(addr
);
213 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &tx_len
, sizeof(tx_len
));
214 offset
+= sizeof(tx_len
);
215 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), src
, tx_len
);
218 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
220 ath6kl_err("Unable to write to the device: %d\n",
224 len_remain
-= tx_len
; addr
+= tx_len
;
230 int ath6kl_bmi_execute(struct ath6kl
*ar
, u32 addr
, u32
*param
)
232 u32 cid
= BMI_EXECUTE
;
237 if (ar
->bmi
.done_sent
) {
238 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
242 size
= sizeof(cid
) + sizeof(addr
) + sizeof(param
);
243 if (size
> ar
->bmi
.max_cmd_size
) {
247 memset(ar
->bmi
.cmd_buf
, 0, size
);
249 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi execute: addr: 0x%x, param: %d)\n",
253 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
254 offset
+= sizeof(cid
);
255 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
256 offset
+= sizeof(addr
);
257 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), param
, sizeof(*param
));
258 offset
+= sizeof(*param
);
260 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
262 ath6kl_err("Unable to write to the device: %d\n", ret
);
266 ret
= ath6kl_hif_bmi_read(ar
, ar
->bmi
.cmd_buf
, sizeof(*param
));
268 ath6kl_err("Unable to read from the device: %d\n", ret
);
272 memcpy(param
, ar
->bmi
.cmd_buf
, sizeof(*param
));
277 int ath6kl_bmi_set_app_start(struct ath6kl
*ar
, u32 addr
)
279 u32 cid
= BMI_SET_APP_START
;
284 if (ar
->bmi
.done_sent
) {
285 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
289 size
= sizeof(cid
) + sizeof(addr
);
290 if (size
> ar
->bmi
.max_cmd_size
) {
294 memset(ar
->bmi
.cmd_buf
, 0, size
);
296 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi set app start: addr: 0x%x\n", addr
);
299 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
300 offset
+= sizeof(cid
);
301 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
302 offset
+= sizeof(addr
);
304 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
306 ath6kl_err("Unable to write to the device: %d\n", ret
);
313 int ath6kl_bmi_reg_read(struct ath6kl
*ar
, u32 addr
, u32
*param
)
315 u32 cid
= BMI_READ_SOC_REGISTER
;
320 if (ar
->bmi
.done_sent
) {
321 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
325 size
= sizeof(cid
) + sizeof(addr
);
326 if (size
> ar
->bmi
.max_cmd_size
) {
330 memset(ar
->bmi
.cmd_buf
, 0, size
);
332 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi read SOC reg: addr: 0x%x\n", addr
);
335 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
336 offset
+= sizeof(cid
);
337 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
338 offset
+= sizeof(addr
);
340 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
342 ath6kl_err("Unable to write to the device: %d\n", ret
);
346 ret
= ath6kl_hif_bmi_read(ar
, ar
->bmi
.cmd_buf
, sizeof(*param
));
348 ath6kl_err("Unable to read from the device: %d\n", ret
);
351 memcpy(param
, ar
->bmi
.cmd_buf
, sizeof(*param
));
356 int ath6kl_bmi_reg_write(struct ath6kl
*ar
, u32 addr
, u32 param
)
358 u32 cid
= BMI_WRITE_SOC_REGISTER
;
363 if (ar
->bmi
.done_sent
) {
364 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
368 size
= sizeof(cid
) + sizeof(addr
) + sizeof(param
);
369 if (size
> ar
->bmi
.max_cmd_size
) {
373 memset(ar
->bmi
.cmd_buf
, 0, size
);
375 ath6kl_dbg(ATH6KL_DBG_BMI
,
376 "bmi write SOC reg: addr: 0x%x, param: %d\n",
380 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
381 offset
+= sizeof(cid
);
382 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
383 offset
+= sizeof(addr
);
384 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), ¶m
, sizeof(param
));
385 offset
+= sizeof(param
);
387 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
389 ath6kl_err("Unable to write to the device: %d\n", ret
);
396 int ath6kl_bmi_lz_data(struct ath6kl
*ar
, u8
*buf
, u32 len
)
398 u32 cid
= BMI_LZ_DATA
;
401 u32 len_remain
, tx_len
;
402 const u32 header
= sizeof(cid
) + sizeof(len
);
405 if (ar
->bmi
.done_sent
) {
406 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
410 size
= ar
->bmi
.max_data_size
+ header
;
411 if (size
> ar
->bmi
.max_cmd_size
) {
415 memset(ar
->bmi
.cmd_buf
, 0, size
);
417 ath6kl_dbg(ATH6KL_DBG_BMI
, "bmi send LZ data: len: %d)\n",
422 tx_len
= (len_remain
< (ar
->bmi
.max_data_size
- header
)) ?
423 len_remain
: (ar
->bmi
.max_data_size
- header
);
426 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
427 offset
+= sizeof(cid
);
428 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &tx_len
, sizeof(tx_len
));
429 offset
+= sizeof(tx_len
);
430 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &buf
[len
- len_remain
],
434 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
436 ath6kl_err("Unable to write to the device: %d\n",
441 len_remain
-= tx_len
;
447 int ath6kl_bmi_lz_stream_start(struct ath6kl
*ar
, u32 addr
)
449 u32 cid
= BMI_LZ_STREAM_START
;
454 if (ar
->bmi
.done_sent
) {
455 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid
);
459 size
= sizeof(cid
) + sizeof(addr
);
460 if (size
> ar
->bmi
.max_cmd_size
) {
464 memset(ar
->bmi
.cmd_buf
, 0, size
);
466 ath6kl_dbg(ATH6KL_DBG_BMI
,
467 "bmi LZ stream start: addr: 0x%x)\n",
471 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &cid
, sizeof(cid
));
472 offset
+= sizeof(cid
);
473 memcpy(&(ar
->bmi
.cmd_buf
[offset
]), &addr
, sizeof(addr
));
474 offset
+= sizeof(addr
);
476 ret
= ath6kl_hif_bmi_write(ar
, ar
->bmi
.cmd_buf
, offset
);
478 ath6kl_err("Unable to start LZ stream to the device: %d\n",
486 int ath6kl_bmi_fast_download(struct ath6kl
*ar
, u32 addr
, u8
*buf
, u32 len
)
490 u32 last_word_offset
= len
& ~0x3;
491 u32 unaligned_bytes
= len
& 0x3;
493 ret
= ath6kl_bmi_lz_stream_start(ar
, addr
);
497 if (unaligned_bytes
) {
498 /* copy the last word into a zero padded buffer */
499 memcpy(&last_word
, &buf
[last_word_offset
], unaligned_bytes
);
502 ret
= ath6kl_bmi_lz_data(ar
, buf
, last_word_offset
);
507 ret
= ath6kl_bmi_lz_data(ar
, (u8
*)&last_word
, 4);
510 /* Close compressed stream and open a new (fake) one.
511 * This serves mainly to flush Target caches. */
512 ret
= ath6kl_bmi_lz_stream_start(ar
, 0x00);
517 void ath6kl_bmi_reset(struct ath6kl
*ar
)
519 ar
->bmi
.done_sent
= false;
522 int ath6kl_bmi_init(struct ath6kl
*ar
)
524 if (WARN_ON(ar
->bmi
.max_data_size
== 0))
527 /* cmd + addr + len + data_size */
528 ar
->bmi
.max_cmd_size
= ar
->bmi
.max_data_size
+ (sizeof(u32
) * 3);
530 ar
->bmi
.cmd_buf
= kzalloc(ar
->bmi
.max_cmd_size
, GFP_ATOMIC
);
531 if (!ar
->bmi
.cmd_buf
)
537 void ath6kl_bmi_cleanup(struct ath6kl
*ar
)
539 kfree(ar
->bmi
.cmd_buf
);
540 ar
->bmi
.cmd_buf
= NULL
;