1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2005-2011 Atheros Communications Inc.
4 * Copyright (c) 2011-2014,2016-2017 Qualcomm Atheros, Inc.
13 void ath10k_bmi_start(struct ath10k
*ar
)
15 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi start\n");
17 ar
->bmi
.done_sent
= false;
19 EXPORT_SYMBOL(ath10k_bmi_start
);
21 int ath10k_bmi_done(struct ath10k
*ar
)
24 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.done
);
27 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi done\n");
29 if (ar
->bmi
.done_sent
) {
30 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi skipped\n");
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
);
39 ath10k_warn(ar
, "unable to write to the device: %d\n", ret
);
46 int ath10k_bmi_get_target_info(struct ath10k
*ar
,
47 struct bmi_target_info
*target_info
)
51 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.get_target_info
);
52 u32 resplen
= sizeof(resp
.get_target_info
);
55 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi get target info\n");
57 if (ar
->bmi
.done_sent
) {
58 ath10k_warn(ar
, "BMI Get Target Info Command disallowed\n");
62 cmd
.id
= __cpu_to_le32(BMI_GET_TARGET_INFO
);
64 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, &resp
, &resplen
);
66 ath10k_warn(ar
, "unable to get target info from device\n");
70 if (resplen
< sizeof(resp
.get_target_info
)) {
71 ath10k_warn(ar
, "invalid get_target_info response length (%d)\n",
76 target_info
->version
= __le32_to_cpu(resp
.get_target_info
.version
);
77 target_info
->type
= __le32_to_cpu(resp
.get_target_info
.type
);
82 #define TARGET_VERSION_SENTINAL 0xffffffffu
84 int ath10k_bmi_get_target_info_sdio(struct ath10k
*ar
,
85 struct bmi_target_info
*target_info
)
89 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.get_target_info
);
94 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi get target info SDIO\n");
96 if (ar
->bmi
.done_sent
) {
97 ath10k_warn(ar
, "BMI Get Target Info Command disallowed\n");
101 cmd
.id
= __cpu_to_le32(BMI_GET_TARGET_INFO
);
103 /* Step 1: Read 4 bytes of the target info and check if it is
104 * the special sentinal version word or the first word in the
107 resplen
= sizeof(u32
);
108 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, &tmp
, &resplen
);
110 ath10k_warn(ar
, "unable to read from device\n");
114 /* Some SDIO boards have a special sentinal byte before the real
117 if (__le32_to_cpu(tmp
) == TARGET_VERSION_SENTINAL
) {
118 /* Step 1b: Read the version length */
119 resplen
= sizeof(u32
);
120 ret
= ath10k_hif_exchange_bmi_msg(ar
, NULL
, 0, &tmp
,
123 ath10k_warn(ar
, "unable to read from device\n");
128 ver_len
= __le32_to_cpu(tmp
);
130 /* Step 2: Check the target info length */
131 if (ver_len
!= sizeof(resp
.get_target_info
)) {
132 ath10k_warn(ar
, "Unexpected target info len: %u. Expected: %zu\n",
133 ver_len
, sizeof(resp
.get_target_info
));
137 /* Step 3: Read the rest of the version response */
138 resplen
= sizeof(resp
.get_target_info
) - sizeof(u32
);
139 ret
= ath10k_hif_exchange_bmi_msg(ar
, NULL
, 0,
140 &resp
.get_target_info
.version
,
143 ath10k_warn(ar
, "unable to read from device\n");
147 target_info
->version
= __le32_to_cpu(resp
.get_target_info
.version
);
148 target_info
->type
= __le32_to_cpu(resp
.get_target_info
.type
);
153 int ath10k_bmi_read_memory(struct ath10k
*ar
,
154 u32 address
, void *buffer
, u32 length
)
158 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.read_mem
);
162 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi read address 0x%x length %d\n",
165 if (ar
->bmi
.done_sent
) {
166 ath10k_warn(ar
, "command disallowed\n");
171 rxlen
= min_t(u32
, length
, BMI_MAX_DATA_SIZE
);
173 cmd
.id
= __cpu_to_le32(BMI_READ_MEMORY
);
174 cmd
.read_mem
.addr
= __cpu_to_le32(address
);
175 cmd
.read_mem
.len
= __cpu_to_le32(rxlen
);
177 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
,
180 ath10k_warn(ar
, "unable to read from the device (%d)\n",
185 memcpy(buffer
, resp
.read_mem
.payload
, rxlen
);
193 EXPORT_SYMBOL(ath10k_bmi_read_memory
);
195 int ath10k_bmi_write_soc_reg(struct ath10k
*ar
, u32 address
, u32 reg_val
)
198 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.write_soc_reg
);
201 ath10k_dbg(ar
, ATH10K_DBG_BMI
,
202 "bmi write soc register 0x%08x val 0x%08x\n",
205 if (ar
->bmi
.done_sent
) {
206 ath10k_warn(ar
, "bmi write soc register command in progress\n");
210 cmd
.id
= __cpu_to_le32(BMI_WRITE_SOC_REGISTER
);
211 cmd
.write_soc_reg
.addr
= __cpu_to_le32(address
);
212 cmd
.write_soc_reg
.value
= __cpu_to_le32(reg_val
);
214 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, NULL
, NULL
);
216 ath10k_warn(ar
, "Unable to write soc register to device: %d\n",
224 int ath10k_bmi_read_soc_reg(struct ath10k
*ar
, u32 address
, u32
*reg_val
)
228 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.read_soc_reg
);
229 u32 resplen
= sizeof(resp
.read_soc_reg
);
232 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi read soc register 0x%08x\n",
235 if (ar
->bmi
.done_sent
) {
236 ath10k_warn(ar
, "bmi read soc register command in progress\n");
240 cmd
.id
= __cpu_to_le32(BMI_READ_SOC_REGISTER
);
241 cmd
.read_soc_reg
.addr
= __cpu_to_le32(address
);
243 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, &resp
, &resplen
);
245 ath10k_warn(ar
, "Unable to read soc register from device: %d\n",
250 *reg_val
= __le32_to_cpu(resp
.read_soc_reg
.value
);
252 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi read soc register value 0x%08x\n",
258 int ath10k_bmi_write_memory(struct ath10k
*ar
,
259 u32 address
, const void *buffer
, u32 length
)
262 u32 hdrlen
= sizeof(cmd
.id
) + sizeof(cmd
.write_mem
);
266 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi write address 0x%x length %d\n",
269 if (ar
->bmi
.done_sent
) {
270 ath10k_warn(ar
, "command disallowed\n");
275 txlen
= min(length
, BMI_MAX_DATA_SIZE
- hdrlen
);
277 /* copy before roundup to avoid reading beyond buffer*/
278 memcpy(cmd
.write_mem
.payload
, buffer
, txlen
);
279 txlen
= roundup(txlen
, 4);
281 cmd
.id
= __cpu_to_le32(BMI_WRITE_MEMORY
);
282 cmd
.write_mem
.addr
= __cpu_to_le32(address
);
283 cmd
.write_mem
.len
= __cpu_to_le32(txlen
);
285 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, hdrlen
+ txlen
,
288 ath10k_warn(ar
, "unable to write to the device (%d)\n",
293 /* fixup roundup() so `length` zeroes out for last chunk */
294 txlen
= min(txlen
, length
);
304 int ath10k_bmi_execute(struct ath10k
*ar
, u32 address
, u32 param
, u32
*result
)
308 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.execute
);
309 u32 resplen
= sizeof(resp
.execute
);
312 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi execute address 0x%x param 0x%x\n",
315 if (ar
->bmi
.done_sent
) {
316 ath10k_warn(ar
, "command disallowed\n");
320 cmd
.id
= __cpu_to_le32(BMI_EXECUTE
);
321 cmd
.execute
.addr
= __cpu_to_le32(address
);
322 cmd
.execute
.param
= __cpu_to_le32(param
);
324 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, &resp
, &resplen
);
326 ath10k_warn(ar
, "unable to read from the device\n");
330 if (resplen
< sizeof(resp
.execute
)) {
331 ath10k_warn(ar
, "invalid execute response length (%d)\n",
336 *result
= __le32_to_cpu(resp
.execute
.result
);
338 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi execute result 0x%x\n", *result
);
343 static int ath10k_bmi_lz_data_large(struct ath10k
*ar
, const void *buffer
, u32 length
)
346 u32 hdrlen
= sizeof(cmd
->id
) + sizeof(cmd
->lz_data
);
351 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "large bmi lz data buffer 0x%pK length %d\n",
354 if (ar
->bmi
.done_sent
) {
355 ath10k_warn(ar
, "command disallowed\n");
359 buf_len
= sizeof(*cmd
) + BMI_MAX_LARGE_DATA_SIZE
- BMI_MAX_DATA_SIZE
;
360 cmd
= kzalloc(buf_len
, GFP_KERNEL
);
365 txlen
= min(length
, BMI_MAX_LARGE_DATA_SIZE
- hdrlen
);
367 WARN_ON_ONCE(txlen
& 3);
369 cmd
->id
= __cpu_to_le32(BMI_LZ_DATA
);
370 cmd
->lz_data
.len
= __cpu_to_le32(txlen
);
371 memcpy(cmd
->lz_data
.payload
, buffer
, txlen
);
373 ret
= ath10k_hif_exchange_bmi_msg(ar
, cmd
, hdrlen
+ txlen
,
376 ath10k_warn(ar
, "unable to write to the device\n");
390 int ath10k_bmi_lz_data(struct ath10k
*ar
, const void *buffer
, u32 length
)
393 u32 hdrlen
= sizeof(cmd
.id
) + sizeof(cmd
.lz_data
);
397 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi lz data buffer 0x%pK length %d\n",
400 if (ar
->bmi
.done_sent
) {
401 ath10k_warn(ar
, "command disallowed\n");
406 txlen
= min(length
, BMI_MAX_DATA_SIZE
- hdrlen
);
408 WARN_ON_ONCE(txlen
& 3);
410 cmd
.id
= __cpu_to_le32(BMI_LZ_DATA
);
411 cmd
.lz_data
.len
= __cpu_to_le32(txlen
);
412 memcpy(cmd
.lz_data
.payload
, buffer
, txlen
);
414 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, hdrlen
+ txlen
,
417 ath10k_warn(ar
, "unable to write to the device\n");
428 int ath10k_bmi_lz_stream_start(struct ath10k
*ar
, u32 address
)
431 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.lz_start
);
434 ath10k_dbg(ar
, ATH10K_DBG_BMI
, "bmi lz stream start address 0x%x\n",
437 if (ar
->bmi
.done_sent
) {
438 ath10k_warn(ar
, "command disallowed\n");
442 cmd
.id
= __cpu_to_le32(BMI_LZ_STREAM_START
);
443 cmd
.lz_start
.addr
= __cpu_to_le32(address
);
445 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, NULL
, NULL
);
447 ath10k_warn(ar
, "unable to Start LZ Stream to the device\n");
454 int ath10k_bmi_fast_download(struct ath10k
*ar
,
455 u32 address
, const void *buffer
, u32 length
)
458 u32 head_len
= rounddown(length
, 4);
459 u32 trailer_len
= length
- head_len
;
462 ath10k_dbg(ar
, ATH10K_DBG_BMI
,
463 "bmi fast download address 0x%x buffer 0x%pK length %d\n",
464 address
, buffer
, length
);
466 ret
= ath10k_bmi_lz_stream_start(ar
, address
);
470 /* copy the last word into a zero padded buffer */
472 memcpy(trailer
, buffer
+ head_len
, trailer_len
);
474 if (ar
->hw_params
.bmi_large_size_download
)
475 ret
= ath10k_bmi_lz_data_large(ar
, buffer
, head_len
);
477 ret
= ath10k_bmi_lz_data(ar
, buffer
, head_len
);
483 ret
= ath10k_bmi_lz_data(ar
, trailer
, 4);
489 * Close compressed stream and open a new (fake) one.
490 * This serves mainly to flush Target caches.
492 ret
= ath10k_bmi_lz_stream_start(ar
, 0x00);
497 int ath10k_bmi_set_start(struct ath10k
*ar
, u32 address
)
500 u32 cmdlen
= sizeof(cmd
.id
) + sizeof(cmd
.set_app_start
);
503 if (ar
->bmi
.done_sent
) {
504 ath10k_warn(ar
, "bmi set start command disallowed\n");
508 cmd
.id
= __cpu_to_le32(BMI_SET_APP_START
);
509 cmd
.set_app_start
.addr
= __cpu_to_le32(address
);
511 ret
= ath10k_hif_exchange_bmi_msg(ar
, &cmd
, cmdlen
, NULL
, NULL
);
513 ath10k_warn(ar
, "unable to set start to the device:%d\n", ret
);