drm: bridge: adv7511: remove s32 format from i2s capabilities
[drm/drm-misc.git] / drivers / net / wireless / ath / ath10k / bmi.c
blob9a4f8e815412cba207f3a788013d362a36a02ac3
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (c) 2005-2011 Atheros Communications Inc.
4 * Copyright (c) 2011-2014,2016-2017 Qualcomm Atheros, Inc.
5 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
6 */
8 #include "bmi.h"
9 #include "hif.h"
10 #include "debug.h"
11 #include "htc.h"
12 #include "hw.h"
14 void ath10k_bmi_start(struct ath10k *ar)
16 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi start\n");
18 ar->bmi.done_sent = false;
20 EXPORT_SYMBOL(ath10k_bmi_start);
22 int ath10k_bmi_done(struct ath10k *ar)
24 struct bmi_cmd cmd;
25 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.done);
26 int ret;
28 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi done\n");
30 if (ar->bmi.done_sent) {
31 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi skipped\n");
32 return 0;
35 ar->bmi.done_sent = true;
36 cmd.id = __cpu_to_le32(BMI_DONE);
38 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
39 if (ret) {
40 ath10k_warn(ar, "unable to write to the device: %d\n", ret);
41 return ret;
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 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi get target info\n");
58 if (ar->bmi.done_sent) {
59 ath10k_warn(ar, "BMI Get Target Info Command disallowed\n");
60 return -EBUSY;
63 cmd.id = __cpu_to_le32(BMI_GET_TARGET_INFO);
65 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
66 if (ret) {
67 ath10k_warn(ar, "unable to get target info from device\n");
68 return ret;
71 if (resplen < sizeof(resp.get_target_info)) {
72 ath10k_warn(ar, "invalid get_target_info response length (%d)\n",
73 resplen);
74 return -EIO;
77 target_info->version = __le32_to_cpu(resp.get_target_info.version);
78 target_info->type = __le32_to_cpu(resp.get_target_info.type);
80 return 0;
83 #define TARGET_VERSION_SENTINAL 0xffffffffu
85 int ath10k_bmi_get_target_info_sdio(struct ath10k *ar,
86 struct bmi_target_info *target_info)
88 struct bmi_cmd cmd;
89 union bmi_resp resp;
90 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.get_target_info);
91 u32 resplen, ver_len;
92 __le32 tmp;
93 int ret;
95 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi get target info SDIO\n");
97 if (ar->bmi.done_sent) {
98 ath10k_warn(ar, "BMI Get Target Info Command disallowed\n");
99 return -EBUSY;
102 cmd.id = __cpu_to_le32(BMI_GET_TARGET_INFO);
104 /* Step 1: Read 4 bytes of the target info and check if it is
105 * the special sentinel version word or the first word in the
106 * version response.
108 resplen = sizeof(u32);
109 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &tmp, &resplen);
110 if (ret) {
111 ath10k_warn(ar, "unable to read from device\n");
112 return ret;
115 /* Some SDIO boards have a special sentinel byte before the real
116 * version response.
118 if (__le32_to_cpu(tmp) == TARGET_VERSION_SENTINAL) {
119 /* Step 1b: Read the version length */
120 resplen = sizeof(u32);
121 ret = ath10k_hif_exchange_bmi_msg(ar, NULL, 0, &tmp,
122 &resplen);
123 if (ret) {
124 ath10k_warn(ar, "unable to read from device\n");
125 return ret;
129 ver_len = __le32_to_cpu(tmp);
131 /* Step 2: Check the target info length */
132 if (ver_len != sizeof(resp.get_target_info)) {
133 ath10k_warn(ar, "Unexpected target info len: %u. Expected: %zu\n",
134 ver_len, sizeof(resp.get_target_info));
135 return -EINVAL;
138 /* Step 3: Read the rest of the version response */
139 resplen = sizeof(resp.get_target_info) - sizeof(u32);
140 ret = ath10k_hif_exchange_bmi_msg(ar, NULL, 0,
141 &resp.get_target_info.version,
142 &resplen);
143 if (ret) {
144 ath10k_warn(ar, "unable to read from device\n");
145 return ret;
148 target_info->version = __le32_to_cpu(resp.get_target_info.version);
149 target_info->type = __le32_to_cpu(resp.get_target_info.type);
151 return 0;
154 int ath10k_bmi_read_memory(struct ath10k *ar,
155 u32 address, void *buffer, u32 length)
157 struct bmi_cmd cmd;
158 union bmi_resp resp;
159 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.read_mem);
160 u32 rxlen;
161 int ret;
163 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi read address 0x%x length %d\n",
164 address, length);
166 if (ar->bmi.done_sent) {
167 ath10k_warn(ar, "command disallowed\n");
168 return -EBUSY;
171 while (length) {
172 rxlen = min_t(u32, length, BMI_MAX_DATA_SIZE);
174 cmd.id = __cpu_to_le32(BMI_READ_MEMORY);
175 cmd.read_mem.addr = __cpu_to_le32(address);
176 cmd.read_mem.len = __cpu_to_le32(rxlen);
178 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen,
179 &resp, &rxlen);
180 if (ret) {
181 ath10k_warn(ar, "unable to read from the device (%d)\n",
182 ret);
183 return ret;
186 memcpy(buffer, resp.read_mem.payload, rxlen);
187 address += rxlen;
188 buffer += rxlen;
189 length -= rxlen;
192 return 0;
194 EXPORT_SYMBOL(ath10k_bmi_read_memory);
196 int ath10k_bmi_write_soc_reg(struct ath10k *ar, u32 address, u32 reg_val)
198 struct bmi_cmd cmd;
199 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.write_soc_reg);
200 int ret;
202 ath10k_dbg(ar, ATH10K_DBG_BMI,
203 "bmi write soc register 0x%08x val 0x%08x\n",
204 address, reg_val);
206 if (ar->bmi.done_sent) {
207 ath10k_warn(ar, "bmi write soc register command in progress\n");
208 return -EBUSY;
211 cmd.id = __cpu_to_le32(BMI_WRITE_SOC_REGISTER);
212 cmd.write_soc_reg.addr = __cpu_to_le32(address);
213 cmd.write_soc_reg.value = __cpu_to_le32(reg_val);
215 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
216 if (ret) {
217 ath10k_warn(ar, "Unable to write soc register to device: %d\n",
218 ret);
219 return ret;
222 return 0;
225 int ath10k_bmi_read_soc_reg(struct ath10k *ar, u32 address, u32 *reg_val)
227 struct bmi_cmd cmd;
228 union bmi_resp resp;
229 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.read_soc_reg);
230 u32 resplen = sizeof(resp.read_soc_reg);
231 int ret;
233 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi read soc register 0x%08x\n",
234 address);
236 if (ar->bmi.done_sent) {
237 ath10k_warn(ar, "bmi read soc register command in progress\n");
238 return -EBUSY;
241 cmd.id = __cpu_to_le32(BMI_READ_SOC_REGISTER);
242 cmd.read_soc_reg.addr = __cpu_to_le32(address);
244 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
245 if (ret) {
246 ath10k_warn(ar, "Unable to read soc register from device: %d\n",
247 ret);
248 return ret;
251 *reg_val = __le32_to_cpu(resp.read_soc_reg.value);
253 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi read soc register value 0x%08x\n",
254 *reg_val);
256 return 0;
259 int ath10k_bmi_write_memory(struct ath10k *ar,
260 u32 address, const void *buffer, u32 length)
262 struct bmi_cmd cmd;
263 u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.write_mem);
264 u32 txlen;
265 int ret;
267 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi write address 0x%x length %d\n",
268 address, length);
270 if (ar->bmi.done_sent) {
271 ath10k_warn(ar, "command disallowed\n");
272 return -EBUSY;
275 while (length) {
276 txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
278 /* copy before roundup to avoid reading beyond buffer*/
279 memcpy(cmd.write_mem.payload, buffer, txlen);
280 txlen = roundup(txlen, 4);
282 cmd.id = __cpu_to_le32(BMI_WRITE_MEMORY);
283 cmd.write_mem.addr = __cpu_to_le32(address);
284 cmd.write_mem.len = __cpu_to_le32(txlen);
286 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
287 NULL, NULL);
288 if (ret) {
289 ath10k_warn(ar, "unable to write to the device (%d)\n",
290 ret);
291 return ret;
294 /* fixup roundup() so `length` zeroes out for last chunk */
295 txlen = min(txlen, length);
297 address += txlen;
298 buffer += txlen;
299 length -= txlen;
302 return 0;
305 int ath10k_bmi_execute(struct ath10k *ar, u32 address, u32 param, u32 *result)
307 struct bmi_cmd cmd;
308 union bmi_resp resp;
309 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.execute);
310 u32 resplen = sizeof(resp.execute);
311 int ret;
313 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi execute address 0x%x param 0x%x\n",
314 address, param);
316 if (ar->bmi.done_sent) {
317 ath10k_warn(ar, "command disallowed\n");
318 return -EBUSY;
321 cmd.id = __cpu_to_le32(BMI_EXECUTE);
322 cmd.execute.addr = __cpu_to_le32(address);
323 cmd.execute.param = __cpu_to_le32(param);
325 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, &resp, &resplen);
326 if (ret) {
327 ath10k_warn(ar, "unable to read from the device\n");
328 return ret;
331 if (resplen < sizeof(resp.execute)) {
332 ath10k_warn(ar, "invalid execute response length (%d)\n",
333 resplen);
334 return -EIO;
337 *result = __le32_to_cpu(resp.execute.result);
339 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi execute result 0x%x\n", *result);
341 return 0;
344 static int ath10k_bmi_lz_data_large(struct ath10k *ar, const void *buffer, u32 length)
346 struct bmi_cmd *cmd;
347 u32 hdrlen = sizeof(cmd->id) + sizeof(cmd->lz_data);
348 u32 txlen;
349 int ret;
350 size_t buf_len;
352 ath10k_dbg(ar, ATH10K_DBG_BMI, "large bmi lz data buffer 0x%pK length %d\n",
353 buffer, length);
355 if (ar->bmi.done_sent) {
356 ath10k_warn(ar, "command disallowed\n");
357 return -EBUSY;
360 buf_len = sizeof(*cmd) + BMI_MAX_LARGE_DATA_SIZE - BMI_MAX_DATA_SIZE;
361 cmd = kzalloc(buf_len, GFP_KERNEL);
362 if (!cmd)
363 return -ENOMEM;
365 while (length) {
366 txlen = min(length, BMI_MAX_LARGE_DATA_SIZE - hdrlen);
368 WARN_ON_ONCE(txlen & 3);
370 cmd->id = __cpu_to_le32(BMI_LZ_DATA);
371 cmd->lz_data.len = __cpu_to_le32(txlen);
372 memcpy(cmd->lz_data.payload, buffer, txlen);
374 ret = ath10k_hif_exchange_bmi_msg(ar, cmd, hdrlen + txlen,
375 NULL, NULL);
376 if (ret) {
377 ath10k_warn(ar, "unable to write to the device\n");
378 kfree(cmd);
379 return ret;
382 buffer += txlen;
383 length -= txlen;
386 kfree(cmd);
388 return 0;
391 int ath10k_bmi_lz_data(struct ath10k *ar, const void *buffer, u32 length)
393 struct bmi_cmd cmd;
394 u32 hdrlen = sizeof(cmd.id) + sizeof(cmd.lz_data);
395 u32 txlen;
396 int ret;
398 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi lz data buffer 0x%pK length %d\n",
399 buffer, length);
401 if (ar->bmi.done_sent) {
402 ath10k_warn(ar, "command disallowed\n");
403 return -EBUSY;
406 while (length) {
407 txlen = min(length, BMI_MAX_DATA_SIZE - hdrlen);
409 WARN_ON_ONCE(txlen & 3);
411 cmd.id = __cpu_to_le32(BMI_LZ_DATA);
412 cmd.lz_data.len = __cpu_to_le32(txlen);
413 memcpy(cmd.lz_data.payload, buffer, txlen);
415 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, hdrlen + txlen,
416 NULL, NULL);
417 if (ret) {
418 ath10k_warn(ar, "unable to write to the device\n");
419 return ret;
422 buffer += txlen;
423 length -= txlen;
426 return 0;
429 int ath10k_bmi_lz_stream_start(struct ath10k *ar, u32 address)
431 struct bmi_cmd cmd;
432 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.lz_start);
433 int ret;
435 ath10k_dbg(ar, ATH10K_DBG_BMI, "bmi lz stream start address 0x%x\n",
436 address);
438 if (ar->bmi.done_sent) {
439 ath10k_warn(ar, "command disallowed\n");
440 return -EBUSY;
443 cmd.id = __cpu_to_le32(BMI_LZ_STREAM_START);
444 cmd.lz_start.addr = __cpu_to_le32(address);
446 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
447 if (ret) {
448 ath10k_warn(ar, "unable to Start LZ Stream to the device\n");
449 return ret;
452 return 0;
455 int ath10k_bmi_fast_download(struct ath10k *ar,
456 u32 address, const void *buffer, u32 length)
458 u8 trailer[4] = {};
459 u32 head_len = rounddown(length, 4);
460 u32 trailer_len = length - head_len;
461 int ret;
463 ath10k_dbg(ar, ATH10K_DBG_BMI,
464 "bmi fast download address 0x%x buffer 0x%pK length %d\n",
465 address, buffer, length);
467 ret = ath10k_bmi_lz_stream_start(ar, address);
468 if (ret)
469 return ret;
471 /* copy the last word into a zero padded buffer */
472 if (trailer_len > 0)
473 memcpy(trailer, buffer + head_len, trailer_len);
475 if (ar->hw_params.bmi_large_size_download)
476 ret = ath10k_bmi_lz_data_large(ar, buffer, head_len);
477 else
478 ret = ath10k_bmi_lz_data(ar, buffer, head_len);
480 if (ret)
481 return ret;
483 if (trailer_len > 0)
484 ret = ath10k_bmi_lz_data(ar, trailer, 4);
486 if (ret != 0)
487 return ret;
490 * Close compressed stream and open a new (fake) one.
491 * This serves mainly to flush Target caches.
493 ret = ath10k_bmi_lz_stream_start(ar, 0x00);
495 return ret;
498 int ath10k_bmi_set_start(struct ath10k *ar, u32 address)
500 struct bmi_cmd cmd;
501 u32 cmdlen = sizeof(cmd.id) + sizeof(cmd.set_app_start);
502 int ret;
504 if (ar->bmi.done_sent) {
505 ath10k_warn(ar, "bmi set start command disallowed\n");
506 return -EBUSY;
509 cmd.id = __cpu_to_le32(BMI_SET_APP_START);
510 cmd.set_app_start.addr = __cpu_to_le32(address);
512 ret = ath10k_hif_exchange_bmi_msg(ar, &cmd, cmdlen, NULL, NULL);
513 if (ret) {
514 ath10k_warn(ar, "unable to set start to the device:%d\n", ret);
515 return ret;
518 return 0;