Linux 3.11-rc3
[cris-mirror.git] / drivers / net / wireless / ath / ath10k / bmi.c
blob1a2ef51b69d91f4335beff63e5b781136cd4ff95
1 /*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
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.
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.
18 #include "bmi.h"
19 #include "hif.h"
20 #include "debug.h"
21 #include "htc.h"
23 int ath10k_bmi_done(struct ath10k *ar)
25 struct bmi_cmd cmd;
26 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.done);
27 int ret;
29 if (ar->bmi.done_sent) {
30 ath10k_dbg(ATH10K_DBG_CORE, "%s skipped\n", __func__);
31 return 0;
34 ar->bmi.done_sent = true;
35 cmd.id = __cpu_to_le32(BMI_DONE);
37 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
38 if (ret) {
39 ath10k_warn("unable to write to the device: %d\n", ret);
40 return ret;
43 ath10k_dbg(ATH10K_DBG_CORE, "BMI done\n");
44 return 0;
47 int ath10k_bmi_get_target_info(struct ath10k *ar,
48 struct bmi_target_info *target_info)
50 struct bmi_cmd cmd;
51 union bmi_resp resp;
52 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.get_target_info);
53 u32 resplen = sizeof(resp.get_target_info);
54 int ret;
56 if (ar->bmi.done_sent) {
57 ath10k_warn("BMI Get Target Info Command disallowed\n");
58 return -EBUSY;
61 cmd.id = __cpu_to_le32(BMI_GET_TARGET_INFO);
63 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
64 if (ret) {
65 ath10k_warn("unable to get target info from device\n");
66 return ret;
69 if (resplen < sizeof(resp.get_target_info)) {
70 ath10k_warn("invalid get_target_info response length (%d)\n",
71 resplen);
72 return -EIO;
75 target_info->version = __le32_to_cpu(resp.get_target_info.version);
76 target_info->type = __le32_to_cpu(resp.get_target_info.type);
77 return 0;
80 int ath10k_bmi_read_memory(struct ath10k *ar,
81 u32 address, void *buffer, u32 length)
83 struct bmi_cmd cmd;
84 union bmi_resp resp;
85 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.read_mem);
86 u32 rxlen;
87 int ret;
89 if (ar->bmi.done_sent) {
90 ath10k_warn("command disallowed\n");
91 return -EBUSY;
94 ath10k_dbg(ATH10K_DBG_CORE,
95 "%s: (device: 0x%p, address: 0x%x, length: %d)\n",
96 __func__, ar, address, length);
98 while (length) {
99 rxlen = min_t(u32, length, BMI_MAX_DATA_SIZE);
101 cmd.id = __cpu_to_le32(BMI_READ_MEMORY);
102 cmd.read_mem.addr = __cpu_to_le32(address);
103 cmd.read_mem.len = __cpu_to_le32(rxlen);
105 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen,
106 &resp, &rxlen);
107 if (ret) {
108 ath10k_warn("unable to read from the device\n");
109 return ret;
112 memcpy(buffer, resp.read_mem.payload, rxlen);
113 address += rxlen;
114 buffer += rxlen;
115 length -= rxlen;
118 return 0;
121 int ath10k_bmi_write_memory(struct ath10k *ar,
122 u32 address, const void *buffer, u32 length)
124 struct bmi_cmd cmd;
125 u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.write_mem);
126 u32 txlen;
127 int ret;
129 if (ar->bmi.done_sent) {
130 ath10k_warn("command disallowed\n");
131 return -EBUSY;
134 ath10k_dbg(ATH10K_DBG_CORE,
135 "%s: (device: 0x%p, address: 0x%x, length: %d)\n",
136 __func__, ar, address, length);
138 while (length) {
139 txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
141 /* copy before roundup to avoid reading beyond buffer*/
142 memcpy(cmd.write_mem.payload, buffer, txlen);
143 txlen = roundup(txlen, 4);
145 cmd.id = __cpu_to_le32(BMI_WRITE_MEMORY);
146 cmd.write_mem.addr = __cpu_to_le32(address);
147 cmd.write_mem.len = __cpu_to_le32(txlen);
149 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
150 NULL, NULL);
151 if (ret) {
152 ath10k_warn("unable to write to the device\n");
153 return ret;
156 /* fixup roundup() so `length` zeroes out for last chunk */
157 txlen = min(txlen, length);
159 address += txlen;
160 buffer += txlen;
161 length -= txlen;
164 return 0;
167 int ath10k_bmi_execute(struct ath10k *ar, u32 address, u32 *param)
169 struct bmi_cmd cmd;
170 union bmi_resp resp;
171 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.execute);
172 u32 resplen = sizeof(resp.execute);
173 int ret;
175 if (ar->bmi.done_sent) {
176 ath10k_warn("command disallowed\n");
177 return -EBUSY;
180 ath10k_dbg(ATH10K_DBG_CORE,
181 "%s: (device: 0x%p, address: 0x%x, param: %d)\n",
182 __func__, ar, address, *param);
184 cmd.id = __cpu_to_le32(BMI_EXECUTE);
185 cmd.execute.addr = __cpu_to_le32(address);
186 cmd.execute.param = __cpu_to_le32(*param);
188 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
189 if (ret) {
190 ath10k_warn("unable to read from the device\n");
191 return ret;
194 if (resplen < sizeof(resp.execute)) {
195 ath10k_warn("invalid execute response length (%d)\n",
196 resplen);
197 return ret;
200 *param = __le32_to_cpu(resp.execute.result);
201 return 0;
204 int ath10k_bmi_lz_data(struct ath10k *ar, const void *buffer, u32 length)
206 struct bmi_cmd cmd;
207 u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.lz_data);
208 u32 txlen;
209 int ret;
211 if (ar->bmi.done_sent) {
212 ath10k_warn("command disallowed\n");
213 return -EBUSY;
216 while (length) {
217 txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
219 WARN_ON_ONCE(txlen & 3);
221 cmd.id = __cpu_to_le32(BMI_LZ_DATA);
222 cmd.lz_data.len = __cpu_to_le32(txlen);
223 memcpy(cmd.lz_data.payload, buffer, txlen);
225 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
226 NULL, NULL);
227 if (ret) {
228 ath10k_warn("unable to write to the device\n");
229 return ret;
232 buffer += txlen;
233 length -= txlen;
236 return 0;
239 int ath10k_bmi_lz_stream_start(struct ath10k *ar, u32 address)
241 struct bmi_cmd cmd;
242 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.lz_start);
243 int ret;
245 if (ar->bmi.done_sent) {
246 ath10k_warn("command disallowed\n");
247 return -EBUSY;
250 cmd.id = __cpu_to_le32(BMI_LZ_STREAM_START);
251 cmd.lz_start.addr = __cpu_to_le32(address);
253 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
254 if (ret) {
255 ath10k_warn("unable to Start LZ Stream to the device\n");
256 return ret;
259 return 0;
262 int ath10k_bmi_fast_download(struct ath10k *ar,
263 u32 address, const void *buffer, u32 length)
265 u8 trailer[4] = {};
266 u32 head_len = rounddown(length, 4);
267 u32 trailer_len = length - head_len;
268 int ret;
270 ret = ath10k_bmi_lz_stream_start(ar, address);
271 if (ret)
272 return ret;
274 /* copy the last word into a zero padded buffer */
275 if (trailer_len > 0)
276 memcpy(trailer, buffer + head_len, trailer_len);
278 ret = ath10k_bmi_lz_data(ar, buffer, head_len);
279 if (ret)
280 return ret;
282 if (trailer_len > 0)
283 ret = ath10k_bmi_lz_data(ar, trailer, 4);
285 if (ret != 0)
286 return ret;
289 * Close compressed stream and open a new (fake) one.
290 * This serves mainly to flush Target caches.
292 ret = ath10k_bmi_lz_stream_start(ar, 0x00);
294 return ret;