ath6kl: move bmi calls to hif driver
[linux-2.6.git] / drivers / net / wireless / ath / ath6kl / bmi.c
blob12f5b57d2dc23b4b2d81d6364b476c4c427d1f9f
1 /*
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.
17 #include "core.h"
18 #include "hif-ops.h"
19 #include "target.h"
20 #include "debug.h"
22 int ath6kl_bmi_done(struct ath6kl *ar)
24 int ret;
25 u32 cid = BMI_DONE;
27 if (ar->bmi.done_sent) {
28 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi done skipped\n");
29 return 0;
32 ar->bmi.done_sent = true;
34 ret = ath6kl_hif_bmi_write(ar, (u8 *)&cid, sizeof(cid));
35 if (ret) {
36 ath6kl_err("Unable to send bmi done: %d\n", ret);
37 return ret;
40 return 0;
43 int ath6kl_bmi_get_target_info(struct ath6kl *ar,
44 struct ath6kl_bmi_target_info *targ_info)
46 int ret;
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);
51 return -EACCES;
54 ret = ath6kl_hif_bmi_write(ar, (u8 *)&cid, sizeof(cid));
55 if (ret) {
56 ath6kl_err("Unable to send get target info: %d\n", ret);
57 return ret;
60 ret = ath6kl_hif_bmi_read(ar, (u8 *)&targ_info->version,
61 sizeof(targ_info->version));
62 if (ret) {
63 ath6kl_err("Unable to recv target info: %d\n", ret);
64 return 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));
72 if (ret) {
73 ath6kl_err("unable to read target info byte count: %d\n",
74 ret);
75 return ret;
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)) {
83 WARN_ON(1);
84 return -EINVAL;
87 /* Read the remainder of the targ_info */
88 ret = ath6kl_hif_bmi_read(ar,
89 ((u8 *)targ_info) +
90 sizeof(targ_info->byte_count),
91 sizeof(*targ_info) -
92 sizeof(targ_info->byte_count));
94 if (ret) {
95 ath6kl_err("Unable to read target info (%d bytes): %d\n",
96 targ_info->byte_count, ret);
97 return ret;
101 ath6kl_dbg(ATH6KL_DBG_BMI, "target info (ver: 0x%x type: 0x%x)\n",
102 targ_info->version, targ_info->type);
104 return 0;
107 int ath6kl_bmi_read(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
109 u32 cid = BMI_READ_MEMORY;
110 int ret;
111 u32 offset;
112 u32 len_remain, rx_len;
113 u16 size;
115 if (ar->bmi.done_sent) {
116 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
117 return -EACCES;
120 size = BMI_DATASZ_MAX + sizeof(cid) + sizeof(addr) + sizeof(len);
121 if (size > MAX_BMI_CMDBUF_SZ) {
122 WARN_ON(1);
123 return -EINVAL;
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",
129 addr, len);
131 len_remain = len;
133 while (len_remain) {
134 rx_len = (len_remain < BMI_DATASZ_MAX) ?
135 len_remain : BMI_DATASZ_MAX;
136 offset = 0;
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);
145 if (ret) {
146 ath6kl_err("Unable to write to the device: %d\n",
147 ret);
148 return ret;
150 ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, rx_len);
151 if (ret) {
152 ath6kl_err("Unable to read from the device: %d\n",
153 ret);
154 return ret;
156 memcpy(&buf[len - len_remain], ar->bmi.cmd_buf, rx_len);
157 len_remain -= rx_len; addr += rx_len;
160 return 0;
163 int ath6kl_bmi_write(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
165 u32 cid = BMI_WRITE_MEMORY;
166 int ret;
167 u32 offset;
168 u32 len_remain, tx_len;
169 const u32 header = sizeof(cid) + sizeof(addr) + sizeof(len);
170 u8 aligned_buf[BMI_DATASZ_MAX];
171 u8 *src;
173 if (ar->bmi.done_sent) {
174 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
175 return -EACCES;
178 if ((BMI_DATASZ_MAX + header) > MAX_BMI_CMDBUF_SZ) {
179 WARN_ON(1);
180 return -EINVAL;
183 memset(ar->bmi.cmd_buf, 0, BMI_DATASZ_MAX + header);
185 ath6kl_dbg(ATH6KL_DBG_BMI,
186 "bmi write memory: addr: 0x%x, len: %d\n", addr, len);
188 len_remain = len;
189 while (len_remain) {
190 src = &buf[len - len_remain];
192 if (len_remain < (BMI_DATASZ_MAX - header)) {
193 if (len_remain & 3) {
194 /* align it with 4 bytes */
195 len_remain = len_remain +
196 (4 - (len_remain & 3));
197 memcpy(aligned_buf, src, len_remain);
198 src = aligned_buf;
200 tx_len = len_remain;
201 } else {
202 tx_len = (BMI_DATASZ_MAX - header);
205 offset = 0;
206 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
207 offset += sizeof(cid);
208 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
209 offset += sizeof(addr);
210 memcpy(&(ar->bmi.cmd_buf[offset]), &tx_len, sizeof(tx_len));
211 offset += sizeof(tx_len);
212 memcpy(&(ar->bmi.cmd_buf[offset]), src, tx_len);
213 offset += tx_len;
215 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
216 if (ret) {
217 ath6kl_err("Unable to write to the device: %d\n",
218 ret);
219 return ret;
221 len_remain -= tx_len; addr += tx_len;
224 return 0;
227 int ath6kl_bmi_execute(struct ath6kl *ar, u32 addr, u32 *param)
229 u32 cid = BMI_EXECUTE;
230 int ret;
231 u32 offset;
232 u16 size;
234 if (ar->bmi.done_sent) {
235 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
236 return -EACCES;
239 size = sizeof(cid) + sizeof(addr) + sizeof(param);
240 if (size > MAX_BMI_CMDBUF_SZ) {
241 WARN_ON(1);
242 return -EINVAL;
244 memset(ar->bmi.cmd_buf, 0, size);
246 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi execute: addr: 0x%x, param: %d)\n",
247 addr, *param);
249 offset = 0;
250 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
251 offset += sizeof(cid);
252 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
253 offset += sizeof(addr);
254 memcpy(&(ar->bmi.cmd_buf[offset]), param, sizeof(*param));
255 offset += sizeof(*param);
257 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
258 if (ret) {
259 ath6kl_err("Unable to write to the device: %d\n", ret);
260 return ret;
263 ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, sizeof(*param));
264 if (ret) {
265 ath6kl_err("Unable to read from the device: %d\n", ret);
266 return ret;
269 memcpy(param, ar->bmi.cmd_buf, sizeof(*param));
271 return 0;
274 int ath6kl_bmi_set_app_start(struct ath6kl *ar, u32 addr)
276 u32 cid = BMI_SET_APP_START;
277 int ret;
278 u32 offset;
279 u16 size;
281 if (ar->bmi.done_sent) {
282 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
283 return -EACCES;
286 size = sizeof(cid) + sizeof(addr);
287 if (size > MAX_BMI_CMDBUF_SZ) {
288 WARN_ON(1);
289 return -EINVAL;
291 memset(ar->bmi.cmd_buf, 0, size);
293 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi set app start: addr: 0x%x\n", addr);
295 offset = 0;
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);
301 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
302 if (ret) {
303 ath6kl_err("Unable to write to the device: %d\n", ret);
304 return ret;
307 return 0;
310 int ath6kl_bmi_reg_read(struct ath6kl *ar, u32 addr, u32 *param)
312 u32 cid = BMI_READ_SOC_REGISTER;
313 int ret;
314 u32 offset;
315 u16 size;
317 if (ar->bmi.done_sent) {
318 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
319 return -EACCES;
322 size = sizeof(cid) + sizeof(addr);
323 if (size > MAX_BMI_CMDBUF_SZ) {
324 WARN_ON(1);
325 return -EINVAL;
327 memset(ar->bmi.cmd_buf, 0, size);
329 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi read SOC reg: addr: 0x%x\n", addr);
331 offset = 0;
332 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
333 offset += sizeof(cid);
334 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
335 offset += sizeof(addr);
337 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
338 if (ret) {
339 ath6kl_err("Unable to write to the device: %d\n", ret);
340 return ret;
343 ret = ath6kl_hif_bmi_read(ar, ar->bmi.cmd_buf, sizeof(*param));
344 if (ret) {
345 ath6kl_err("Unable to read from the device: %d\n", ret);
346 return ret;
348 memcpy(param, ar->bmi.cmd_buf, sizeof(*param));
350 return 0;
353 int ath6kl_bmi_reg_write(struct ath6kl *ar, u32 addr, u32 param)
355 u32 cid = BMI_WRITE_SOC_REGISTER;
356 int ret;
357 u32 offset;
358 u16 size;
360 if (ar->bmi.done_sent) {
361 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
362 return -EACCES;
365 size = sizeof(cid) + sizeof(addr) + sizeof(param);
366 if (size > MAX_BMI_CMDBUF_SZ) {
367 WARN_ON(1);
368 return -EINVAL;
370 memset(ar->bmi.cmd_buf, 0, size);
372 ath6kl_dbg(ATH6KL_DBG_BMI,
373 "bmi write SOC reg: addr: 0x%x, param: %d\n",
374 addr, param);
376 offset = 0;
377 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
378 offset += sizeof(cid);
379 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
380 offset += sizeof(addr);
381 memcpy(&(ar->bmi.cmd_buf[offset]), &param, sizeof(param));
382 offset += sizeof(param);
384 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
385 if (ret) {
386 ath6kl_err("Unable to write to the device: %d\n", ret);
387 return ret;
390 return 0;
393 int ath6kl_bmi_lz_data(struct ath6kl *ar, u8 *buf, u32 len)
395 u32 cid = BMI_LZ_DATA;
396 int ret;
397 u32 offset;
398 u32 len_remain, tx_len;
399 const u32 header = sizeof(cid) + sizeof(len);
400 u16 size;
402 if (ar->bmi.done_sent) {
403 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
404 return -EACCES;
407 size = BMI_DATASZ_MAX + header;
408 if (size > MAX_BMI_CMDBUF_SZ) {
409 WARN_ON(1);
410 return -EINVAL;
412 memset(ar->bmi.cmd_buf, 0, size);
414 ath6kl_dbg(ATH6KL_DBG_BMI, "bmi send LZ data: len: %d)\n",
415 len);
417 len_remain = len;
418 while (len_remain) {
419 tx_len = (len_remain < (BMI_DATASZ_MAX - header)) ?
420 len_remain : (BMI_DATASZ_MAX - header);
422 offset = 0;
423 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
424 offset += sizeof(cid);
425 memcpy(&(ar->bmi.cmd_buf[offset]), &tx_len, sizeof(tx_len));
426 offset += sizeof(tx_len);
427 memcpy(&(ar->bmi.cmd_buf[offset]), &buf[len - len_remain],
428 tx_len);
429 offset += tx_len;
431 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
432 if (ret) {
433 ath6kl_err("Unable to write to the device: %d\n",
434 ret);
435 return ret;
438 len_remain -= tx_len;
441 return 0;
444 int ath6kl_bmi_lz_stream_start(struct ath6kl *ar, u32 addr)
446 u32 cid = BMI_LZ_STREAM_START;
447 int ret;
448 u32 offset;
449 u16 size;
451 if (ar->bmi.done_sent) {
452 ath6kl_err("bmi done sent already, cmd %d disallowed\n", cid);
453 return -EACCES;
456 size = sizeof(cid) + sizeof(addr);
457 if (size > MAX_BMI_CMDBUF_SZ) {
458 WARN_ON(1);
459 return -EINVAL;
461 memset(ar->bmi.cmd_buf, 0, size);
463 ath6kl_dbg(ATH6KL_DBG_BMI,
464 "bmi LZ stream start: addr: 0x%x)\n",
465 addr);
467 offset = 0;
468 memcpy(&(ar->bmi.cmd_buf[offset]), &cid, sizeof(cid));
469 offset += sizeof(cid);
470 memcpy(&(ar->bmi.cmd_buf[offset]), &addr, sizeof(addr));
471 offset += sizeof(addr);
473 ret = ath6kl_hif_bmi_write(ar, ar->bmi.cmd_buf, offset);
474 if (ret) {
475 ath6kl_err("Unable to start LZ stream to the device: %d\n",
476 ret);
477 return ret;
480 return 0;
483 int ath6kl_bmi_fast_download(struct ath6kl *ar, u32 addr, u8 *buf, u32 len)
485 int ret;
486 u32 last_word = 0;
487 u32 last_word_offset = len & ~0x3;
488 u32 unaligned_bytes = len & 0x3;
490 ret = ath6kl_bmi_lz_stream_start(ar, addr);
491 if (ret)
492 return ret;
494 if (unaligned_bytes) {
495 /* copy the last word into a zero padded buffer */
496 memcpy(&last_word, &buf[last_word_offset], unaligned_bytes);
499 ret = ath6kl_bmi_lz_data(ar, buf, last_word_offset);
500 if (ret)
501 return ret;
503 if (unaligned_bytes)
504 ret = ath6kl_bmi_lz_data(ar, (u8 *)&last_word, 4);
506 if (!ret) {
507 /* Close compressed stream and open a new (fake) one.
508 * This serves mainly to flush Target caches. */
509 ret = ath6kl_bmi_lz_stream_start(ar, 0x00);
511 return ret;
514 void ath6kl_bmi_reset(struct ath6kl *ar)
516 ar->bmi.done_sent = false;
519 int ath6kl_bmi_init(struct ath6kl *ar)
521 ar->bmi.cmd_buf = kzalloc(MAX_BMI_CMDBUF_SZ, GFP_ATOMIC);
523 if (!ar->bmi.cmd_buf)
524 return -ENOMEM;
526 return 0;
529 void ath6kl_bmi_cleanup(struct ath6kl *ar)
531 kfree(ar->bmi.cmd_buf);
532 ar->bmi.cmd_buf = NULL;