1 /*
2  * Copyright (c) 2020-2022, Intel Corporation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <lib/mmio.h>
8 #include <common/debug.h>
9 #include <drivers/delay_timer.h>
10 
11 #include "socfpga_mailbox.h"
12 #include "socfpga_sip_svc.h"
13 
14 static mailbox_payload_t mailbox_resp_payload;
15 static mailbox_container_t mailbox_resp_ctr = {0, 0, &mailbox_resp_payload};
16 
is_mailbox_cmdbuf_full(uint32_t cin)17 static bool is_mailbox_cmdbuf_full(uint32_t cin)
18 {
19 	uint32_t cout = mmio_read_32(MBOX_OFFSET + MBOX_COUT);
20 
21 	return (((cin + 1U) % MBOX_CMD_BUFFER_SIZE) == cout);
22 }
23 
is_mailbox_cmdbuf_empty(uint32_t cin)24 static bool is_mailbox_cmdbuf_empty(uint32_t cin)
25 {
26 	uint32_t cout = mmio_read_32(MBOX_OFFSET + MBOX_COUT);
27 
28 	return (((cout + 1U) % MBOX_CMD_BUFFER_SIZE) == cin);
29 }
30 
wait_for_mailbox_cmdbuf_empty(uint32_t cin)31 static int wait_for_mailbox_cmdbuf_empty(uint32_t cin)
32 {
33 	unsigned int timeout = 200U;
34 
35 	do {
36 		if (is_mailbox_cmdbuf_empty(cin)) {
37 			break;
38 		}
39 		mdelay(10U);
40 	} while (--timeout != 0U);
41 
42 	if (timeout == 0U) {
43 		return MBOX_TIMEOUT;
44 	}
45 
46 	return MBOX_RET_OK;
47 }
48 
write_mailbox_cmd_buffer(uint32_t * cin,uint32_t cout,uint32_t data,bool * is_doorbell_triggered)49 static int write_mailbox_cmd_buffer(uint32_t *cin, uint32_t cout,
50 				    uint32_t data,
51 				    bool *is_doorbell_triggered)
52 {
53 	unsigned int timeout = 100U;
54 
55 	do {
56 		if (is_mailbox_cmdbuf_full(*cin)) {
57 			if (!(*is_doorbell_triggered)) {
58 				mmio_write_32(MBOX_OFFSET +
59 					      MBOX_DOORBELL_TO_SDM, 1U);
60 				*is_doorbell_triggered = true;
61 			}
62 			mdelay(10U);
63 		} else {
64 			mmio_write_32(MBOX_ENTRY_TO_ADDR(CMD, (*cin)++), data);
65 			*cin %= MBOX_CMD_BUFFER_SIZE;
66 			mmio_write_32(MBOX_OFFSET + MBOX_CIN, *cin);
67 			break;
68 		}
69 	} while (--timeout != 0U);
70 
71 	if (timeout == 0U) {
72 		return MBOX_TIMEOUT;
73 	}
74 
75 	if (*is_doorbell_triggered) {
76 		int ret = wait_for_mailbox_cmdbuf_empty(*cin);
77 		return ret;
78 	}
79 
80 	return MBOX_RET_OK;
81 }
82 
fill_mailbox_circular_buffer(uint32_t header_cmd,uint32_t * args,unsigned int len)83 static int fill_mailbox_circular_buffer(uint32_t header_cmd, uint32_t *args,
84 					unsigned int len)
85 {
86 	uint32_t sdm_read_offset, cmd_free_offset;
87 	unsigned int i;
88 	int ret;
89 	bool is_doorbell_triggered = false;
90 
91 	cmd_free_offset = mmio_read_32(MBOX_OFFSET + MBOX_CIN);
92 	sdm_read_offset = mmio_read_32(MBOX_OFFSET + MBOX_COUT);
93 
94 	ret = write_mailbox_cmd_buffer(&cmd_free_offset, sdm_read_offset,
95 				       header_cmd, &is_doorbell_triggered);
96 	if (ret != 0) {
97 		goto restart_mailbox;
98 	}
99 
100 	for (i = 0U; i < len; i++) {
101 		is_doorbell_triggered = false;
102 		ret = write_mailbox_cmd_buffer(&cmd_free_offset,
103 					       sdm_read_offset, args[i],
104 					       &is_doorbell_triggered);
105 		if (ret != 0) {
106 			goto restart_mailbox;
107 		}
108 	}
109 
110 	mmio_write_32(MBOX_OFFSET + MBOX_DOORBELL_TO_SDM, 1U);
111 
112 	return MBOX_RET_OK;
113 
114 restart_mailbox:
115 	/*
116 	 * Attempt to restart mailbox if the driver not able to write
117 	 * into mailbox command buffer
118 	 */
119 	if (MBOX_CMD_MASK(header_cmd) != MBOX_CMD_RESTART) {
120 		INFO("Mailbox timed out: Attempting mailbox reset\n");
121 		ret = mailbox_init();
122 
123 		if (ret == MBOX_TIMEOUT) {
124 			INFO("Error: Mailbox fail to restart\n");
125 		}
126 	}
127 
128 	return MBOX_TIMEOUT;
129 }
130 
mailbox_read_response(unsigned int * job_id,uint32_t * response,unsigned int * resp_len)131 int mailbox_read_response(unsigned int *job_id, uint32_t *response,
132 				unsigned int *resp_len)
133 {
134 	uint32_t rin;
135 	uint32_t rout;
136 	uint32_t resp_data;
137 	unsigned int ret_resp_len;
138 
139 	if (mmio_read_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM) == 1U) {
140 		mmio_write_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM, 0U);
141 	}
142 
143 	rin = mmio_read_32(MBOX_OFFSET + MBOX_RIN);
144 	rout = mmio_read_32(MBOX_OFFSET + MBOX_ROUT);
145 
146 	if (rout != rin) {
147 		resp_data = mmio_read_32(MBOX_ENTRY_TO_ADDR(RESP, (rout)++));
148 
149 		rout %= MBOX_RESP_BUFFER_SIZE;
150 		mmio_write_32(MBOX_OFFSET + MBOX_ROUT, rout);
151 
152 
153 		if (MBOX_RESP_CLIENT_ID(resp_data) != MBOX_ATF_CLIENT_ID) {
154 			return MBOX_WRONG_ID;
155 		}
156 
157 		*job_id = MBOX_RESP_JOB_ID(resp_data);
158 
159 		ret_resp_len = MBOX_RESP_LEN(resp_data);
160 
161 		if (iterate_resp(ret_resp_len, response, resp_len)
162 			!= MBOX_RET_OK) {
163 			return MBOX_TIMEOUT;
164 		}
165 
166 		if (MBOX_RESP_ERR(resp_data) > 0U) {
167 			INFO("Error in response: %x\n", resp_data);
168 			return -MBOX_RESP_ERR(resp_data);
169 		}
170 
171 		return MBOX_RET_OK;
172 	}
173 	return MBOX_NO_RESPONSE;
174 }
175 
mailbox_read_response_async(unsigned int * job_id,uint32_t * header,uint32_t * response,unsigned int * resp_len,uint8_t ignore_client_id)176 int mailbox_read_response_async(unsigned int *job_id, uint32_t *header,
177 				uint32_t *response, unsigned int *resp_len,
178 				uint8_t ignore_client_id)
179 {
180 	uint32_t rin;
181 	uint32_t rout;
182 	uint32_t resp_data;
183 	uint32_t ret_resp_len = 0;
184 	uint8_t is_done = 0;
185 
186 	if ((mailbox_resp_ctr.flag & MBOX_PAYLOAD_FLAG_BUSY) != 0) {
187 		ret_resp_len = MBOX_RESP_LEN(
188 				mailbox_resp_ctr.payload->header) -
189 				mailbox_resp_ctr.index;
190 	}
191 
192 	if (mmio_read_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM) == 1U) {
193 		mmio_write_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM, 0U);
194 	}
195 
196 	rin = mmio_read_32(MBOX_OFFSET + MBOX_RIN);
197 	rout = mmio_read_32(MBOX_OFFSET + MBOX_ROUT);
198 
199 	while (rout != rin && !is_done) {
200 
201 		resp_data = mmio_read_32(MBOX_ENTRY_TO_ADDR(RESP, (rout)++));
202 
203 		rout %= MBOX_RESP_BUFFER_SIZE;
204 		mmio_write_32(MBOX_OFFSET + MBOX_ROUT, rout);
205 		rin = mmio_read_32(MBOX_OFFSET + MBOX_RIN);
206 
207 		if ((mailbox_resp_ctr.flag & MBOX_PAYLOAD_FLAG_BUSY) != 0) {
208 			mailbox_resp_ctr.payload->data[mailbox_resp_ctr.index] = resp_data;
209 			mailbox_resp_ctr.index++;
210 			ret_resp_len--;
211 		} else {
212 			if (!ignore_client_id) {
213 				if (MBOX_RESP_CLIENT_ID(resp_data) != MBOX_ATF_CLIENT_ID) {
214 					*resp_len = 0;
215 					return MBOX_WRONG_ID;
216 				}
217 			}
218 
219 			*job_id = MBOX_RESP_JOB_ID(resp_data);
220 			ret_resp_len = MBOX_RESP_LEN(resp_data);
221 			mailbox_resp_ctr.payload->header = resp_data;
222 			mailbox_resp_ctr.flag |= MBOX_PAYLOAD_FLAG_BUSY;
223 		}
224 
225 		if (ret_resp_len == 0) {
226 			is_done = 1;
227 		}
228 	}
229 
230 	if (is_done != 0) {
231 
232 		/* copy header data to input address if applicable */
233 		if (header != 0) {
234 			*header = mailbox_resp_ctr.payload->header;
235 		}
236 
237 		/* copy response data to input buffer if applicable */
238 		ret_resp_len = MBOX_RESP_LEN(mailbox_resp_ctr.payload->header);
239 		if ((ret_resp_len > 0) && (response == NULL) && resp_len) {
240 			if (*resp_len > ret_resp_len) {
241 				*resp_len = ret_resp_len;
242 			}
243 
244 			memcpy((uint8_t *) response,
245 				(uint8_t *) mailbox_resp_ctr.payload->data,
246 				*resp_len * MBOX_WORD_BYTE);
247 		}
248 
249 		/* reset async response param */
250 		mailbox_resp_ctr.index = 0;
251 		mailbox_resp_ctr.flag = 0;
252 
253 		if (MBOX_RESP_ERR(mailbox_resp_ctr.payload->header) > 0U) {
254 			INFO("Error in async response: %x\n",
255 				mailbox_resp_ctr.payload->header);
256 			return -MBOX_RESP_ERR(mailbox_resp_ctr.payload->header);
257 		}
258 
259 		return MBOX_RET_OK;
260 	}
261 
262 	*resp_len = 0;
263 	return (mailbox_resp_ctr.flag & MBOX_PAYLOAD_FLAG_BUSY) ? MBOX_BUSY : MBOX_NO_RESPONSE;
264 }
265 
mailbox_poll_response(uint32_t job_id,uint32_t urgent,uint32_t * response,unsigned int * resp_len)266 int mailbox_poll_response(uint32_t job_id, uint32_t urgent, uint32_t *response,
267 				unsigned int *resp_len)
268 {
269 	unsigned int timeout = 40U;
270 	unsigned int sdm_loop = 255U;
271 	unsigned int ret_resp_len;
272 	uint32_t rin;
273 	uint32_t rout;
274 	uint32_t resp_data;
275 
276 	while (sdm_loop != 0U) {
277 
278 		do {
279 			if (mmio_read_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM)
280 				== 1U) {
281 				break;
282 			}
283 			mdelay(10U);
284 		} while (--timeout != 0U);
285 
286 		if (timeout == 0U) {
287 			break;
288 		}
289 
290 		mmio_write_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM, 0U);
291 
292 		if ((urgent & 1U) != 0U) {
293 			mdelay(5U);
294 			if ((mmio_read_32(MBOX_OFFSET + MBOX_STATUS) &
295 				MBOX_STATUS_UA_MASK) ^
296 				(urgent & MBOX_STATUS_UA_MASK)) {
297 				mmio_write_32(MBOX_OFFSET + MBOX_URG, 0U);
298 				return MBOX_RET_OK;
299 			}
300 
301 			mmio_write_32(MBOX_OFFSET + MBOX_URG, 0U);
302 			INFO("Error: Mailbox did not get UA");
303 			return MBOX_RET_ERROR;
304 		}
305 
306 		rin = mmio_read_32(MBOX_OFFSET + MBOX_RIN);
307 		rout = mmio_read_32(MBOX_OFFSET + MBOX_ROUT);
308 
309 		while (rout != rin) {
310 			resp_data = mmio_read_32(MBOX_ENTRY_TO_ADDR(RESP,
311 								(rout)++));
312 
313 			rout %= MBOX_RESP_BUFFER_SIZE;
314 			mmio_write_32(MBOX_OFFSET + MBOX_ROUT, rout);
315 
316 			if (MBOX_RESP_CLIENT_ID(resp_data) != MBOX_ATF_CLIENT_ID
317 				|| MBOX_RESP_JOB_ID(resp_data) != job_id) {
318 				continue;
319 			}
320 
321 			ret_resp_len = MBOX_RESP_LEN(resp_data);
322 
323 			if (iterate_resp(ret_resp_len, response, resp_len)
324 				!= MBOX_RET_OK) {
325 				return MBOX_TIMEOUT;
326 			}
327 
328 			if (MBOX_RESP_ERR(resp_data) > 0U) {
329 				INFO("Error in response: %x\n", resp_data);
330 				return -MBOX_RESP_ERR(resp_data);
331 			}
332 
333 			return MBOX_RET_OK;
334 		}
335 
336 	sdm_loop--;
337 	}
338 
339 	INFO("Timed out waiting for SDM\n");
340 	return MBOX_TIMEOUT;
341 }
342 
iterate_resp(uint32_t mbox_resp_len,uint32_t * resp_buf,unsigned int * resp_len)343 int iterate_resp(uint32_t mbox_resp_len, uint32_t *resp_buf,
344 			unsigned int *resp_len)
345 {
346 	unsigned int timeout, total_resp_len = 0U;
347 	uint32_t resp_data;
348 	uint32_t rin = mmio_read_32(MBOX_OFFSET + MBOX_RIN);
349 	uint32_t rout = mmio_read_32(MBOX_OFFSET + MBOX_ROUT);
350 
351 	while (mbox_resp_len > 0U) {
352 		timeout = 100U;
353 		mbox_resp_len--;
354 		resp_data = mmio_read_32(MBOX_ENTRY_TO_ADDR(RESP, (rout)++));
355 
356 		if ((resp_buf != NULL) && (resp_len != NULL)
357 			&& (*resp_len != 0U)) {
358 			*(resp_buf + total_resp_len)
359 					= resp_data;
360 			*resp_len = *resp_len - 1;
361 			total_resp_len++;
362 		}
363 		rout %= MBOX_RESP_BUFFER_SIZE;
364 		mmio_write_32(MBOX_OFFSET + MBOX_ROUT, rout);
365 
366 		do {
367 			rin = mmio_read_32(MBOX_OFFSET + MBOX_RIN);
368 			if (rout == rin) {
369 				mdelay(10U);
370 			} else {
371 				break;
372 			}
373 			timeout--;
374 		} while ((mbox_resp_len > 0U) && (timeout != 0U));
375 
376 		if (timeout == 0U) {
377 			INFO("Timed out waiting for SDM\n");
378 			return MBOX_TIMEOUT;
379 		}
380 	}
381 
382 	if (resp_len)
383 		*resp_len = total_resp_len;
384 
385 	return MBOX_RET_OK;
386 }
387 
mailbox_send_cmd_async_ext(uint32_t header_cmd,uint32_t * args,unsigned int len)388 int mailbox_send_cmd_async_ext(uint32_t header_cmd, uint32_t *args,
389 			unsigned int len)
390 {
391 	return fill_mailbox_circular_buffer(header_cmd, args, len);
392 }
393 
mailbox_send_cmd_async(uint32_t * job_id,uint32_t cmd,uint32_t * args,unsigned int len,unsigned int indirect)394 int mailbox_send_cmd_async(uint32_t *job_id, uint32_t cmd, uint32_t *args,
395 			  unsigned int len, unsigned int indirect)
396 {
397 	int status;
398 
399 	status = fill_mailbox_circular_buffer(
400 				MBOX_CLIENT_ID_CMD(MBOX_ATF_CLIENT_ID) |
401 				MBOX_JOB_ID_CMD(*job_id) |
402 				MBOX_CMD_LEN_CMD(len) |
403 				MBOX_INDIRECT(indirect) |
404 				cmd, args, len);
405 	if (status < 0) {
406 		return status;
407 	}
408 
409 	*job_id = (*job_id + 1U) % MBOX_MAX_IND_JOB_ID;
410 
411 	return MBOX_RET_OK;
412 }
413 
mailbox_send_cmd(uint32_t job_id,uint32_t cmd,uint32_t * args,unsigned int len,uint32_t urgent,uint32_t * response,unsigned int * resp_len)414 int mailbox_send_cmd(uint32_t job_id, uint32_t cmd, uint32_t *args,
415 			unsigned int len, uint32_t urgent, uint32_t *response,
416 			unsigned int *resp_len)
417 {
418 	int status = 0;
419 
420 	if (urgent != 0U) {
421 		urgent |= mmio_read_32(MBOX_OFFSET + MBOX_STATUS) &
422 					MBOX_STATUS_UA_MASK;
423 		mmio_write_32(MBOX_OFFSET + MBOX_URG, cmd);
424 		mmio_write_32(MBOX_OFFSET + MBOX_DOORBELL_TO_SDM, 1U);
425 	}
426 
427 	else {
428 		status = fill_mailbox_circular_buffer(
429 			MBOX_CLIENT_ID_CMD(MBOX_ATF_CLIENT_ID) |
430 			MBOX_JOB_ID_CMD(job_id) |
431 			MBOX_CMD_LEN_CMD(len) |
432 			cmd, args, len);
433 	}
434 
435 	if (status != 0) {
436 		return status;
437 	}
438 
439 	status = mailbox_poll_response(job_id, urgent, response, resp_len);
440 
441 	return status;
442 }
443 
mailbox_clear_response(void)444 void mailbox_clear_response(void)
445 {
446 	mmio_write_32(MBOX_OFFSET + MBOX_ROUT,
447 		mmio_read_32(MBOX_OFFSET + MBOX_RIN));
448 }
449 
mailbox_set_int(uint32_t interrupt)450 void mailbox_set_int(uint32_t interrupt)
451 {
452 
453 	mmio_write_32(MBOX_OFFSET+MBOX_INT, MBOX_COE_BIT(interrupt) |
454 			MBOX_UAE_BIT(interrupt));
455 }
456 
457 
mailbox_set_qspi_open(void)458 void mailbox_set_qspi_open(void)
459 {
460 	mailbox_set_int(MBOX_INT_FLAG_COE | MBOX_INT_FLAG_RIE);
461 	mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_QSPI_OPEN, NULL, 0U,
462 				CMD_CASUAL, NULL, NULL);
463 }
464 
mailbox_set_qspi_direct(void)465 void mailbox_set_qspi_direct(void)
466 {
467 	mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_QSPI_DIRECT, NULL, 0U,
468 				CMD_CASUAL, NULL, NULL);
469 }
470 
mailbox_set_qspi_close(void)471 void mailbox_set_qspi_close(void)
472 {
473 	mailbox_set_int(MBOX_INT_FLAG_COE | MBOX_INT_FLAG_RIE);
474 	mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_QSPI_CLOSE, NULL, 0U,
475 				CMD_CASUAL, NULL, NULL);
476 }
477 
mailbox_qspi_set_cs(uint32_t device_select)478 void mailbox_qspi_set_cs(uint32_t device_select)
479 {
480 	uint32_t cs_setting;
481 
482 	/* QSPI device select settings at 31:28 */
483 	cs_setting = (device_select << 28);
484 	mailbox_set_int(MBOX_INT_FLAG_COE | MBOX_INT_FLAG_RIE);
485 	mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_QSPI_SET_CS, &cs_setting,
486 				1U, CMD_CASUAL, NULL, NULL);
487 }
488 
mailbox_hps_qspi_enable(void)489 void mailbox_hps_qspi_enable(void)
490 {
491 	mailbox_set_qspi_open();
492 	mailbox_set_qspi_direct();
493 }
494 
mailbox_reset_cold(void)495 void mailbox_reset_cold(void)
496 {
497 	mailbox_set_int(MBOX_INT_FLAG_COE | MBOX_INT_FLAG_RIE);
498 	mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_REBOOT_HPS, NULL, 0U,
499 				CMD_CASUAL, NULL, NULL);
500 }
501 
mailbox_rsu_get_spt_offset(uint32_t * resp_buf,unsigned int resp_buf_len)502 int mailbox_rsu_get_spt_offset(uint32_t *resp_buf, unsigned int resp_buf_len)
503 {
504 	return mailbox_send_cmd(MBOX_JOB_ID, MBOX_GET_SUBPARTITION_TABLE,
505 				NULL, 0U, CMD_CASUAL, resp_buf,
506 				&resp_buf_len);
507 }
508 
509 struct rsu_status_info {
510 	uint64_t current_image;
511 	uint64_t fail_image;
512 	uint32_t state;
513 	uint32_t version;
514 	uint32_t error_location;
515 	uint32_t error_details;
516 	uint32_t retry_counter;
517 };
518 
mailbox_rsu_status(uint32_t * resp_buf,unsigned int resp_buf_len)519 int mailbox_rsu_status(uint32_t *resp_buf, unsigned int resp_buf_len)
520 {
521 	int ret;
522 	struct rsu_status_info *info = (struct rsu_status_info *)resp_buf;
523 
524 	info->retry_counter = ~0U;
525 
526 	ret = mailbox_send_cmd(MBOX_JOB_ID, MBOX_RSU_STATUS, NULL, 0U,
527 				CMD_CASUAL, resp_buf,
528 				&resp_buf_len);
529 
530 	if (ret < 0) {
531 		return ret;
532 	}
533 
534 	if (info->retry_counter != ~0U) {
535 		if ((info->version & RSU_VERSION_ACMF_MASK) == 0U) {
536 			info->version |= RSU_VERSION_ACMF;
537 		}
538 	}
539 
540 	return ret;
541 }
542 
mailbox_rsu_update(uint32_t * flash_offset)543 int mailbox_rsu_update(uint32_t *flash_offset)
544 {
545 	return mailbox_send_cmd(MBOX_JOB_ID, MBOX_RSU_UPDATE,
546 				flash_offset, 2U,
547 				CMD_CASUAL, NULL, NULL);
548 }
549 
mailbox_hps_stage_notify(uint32_t execution_stage)550 int mailbox_hps_stage_notify(uint32_t execution_stage)
551 {
552 	return mailbox_send_cmd(MBOX_JOB_ID, MBOX_HPS_STAGE_NOTIFY,
553 				&execution_stage, 1U, CMD_CASUAL,
554 				NULL, NULL);
555 }
556 
mailbox_init(void)557 int mailbox_init(void)
558 {
559 	int status;
560 
561 	mailbox_set_int(MBOX_INT_FLAG_COE | MBOX_INT_FLAG_RIE |
562 			MBOX_INT_FLAG_UAE);
563 	mmio_write_32(MBOX_OFFSET + MBOX_URG, 0U);
564 	mmio_write_32(MBOX_OFFSET + MBOX_DOORBELL_FROM_SDM, 0U);
565 
566 	status = mailbox_send_cmd(0U, MBOX_CMD_RESTART, NULL, 0U,
567 					CMD_URGENT, NULL, NULL);
568 
569 	if (status != 0) {
570 		return status;
571 	}
572 
573 	mailbox_set_int(MBOX_INT_FLAG_COE | MBOX_INT_FLAG_RIE |
574 			MBOX_INT_FLAG_UAE);
575 
576 	return MBOX_RET_OK;
577 }
578 
intel_mailbox_get_config_status(uint32_t cmd,bool init_done)579 int intel_mailbox_get_config_status(uint32_t cmd, bool init_done)
580 {
581 	int status;
582 	uint32_t res, response[6];
583 	unsigned int resp_len = ARRAY_SIZE(response);
584 
585 	status = mailbox_send_cmd(MBOX_JOB_ID, cmd, NULL, 0U, CMD_CASUAL,
586 				response, &resp_len);
587 
588 	if (status < 0) {
589 		return status;
590 	}
591 
592 	res = response[RECONFIG_STATUS_STATE];
593 	if ((res != 0U) && (res != MBOX_CFGSTAT_STATE_CONFIG)) {
594 		return res;
595 	}
596 
597 	res = response[RECONFIG_STATUS_PIN_STATUS];
598 	if ((res & PIN_STATUS_NSTATUS) == 0U) {
599 		return MBOX_CFGSTAT_STATE_ERROR_HARDWARE;
600 	}
601 
602 	res = response[RECONFIG_STATUS_SOFTFUNC_STATUS];
603 	if ((res & SOFTFUNC_STATUS_SEU_ERROR) != 0U) {
604 		return MBOX_CFGSTAT_STATE_ERROR_HARDWARE;
605 	}
606 
607 	if ((res & SOFTFUNC_STATUS_CONF_DONE) == 0U) {
608 		return MBOX_CFGSTAT_STATE_CONFIG;
609 	}
610 
611 	if (init_done && (res & SOFTFUNC_STATUS_INIT_DONE) == 0U) {
612 		return MBOX_CFGSTAT_STATE_CONFIG;
613 	}
614 
615 	return MBOX_RET_OK;
616 }
617 
intel_mailbox_is_fpga_not_ready(void)618 int intel_mailbox_is_fpga_not_ready(void)
619 {
620 	int ret = intel_mailbox_get_config_status(MBOX_RECONFIG_STATUS, true);
621 
622 	if ((ret != MBOX_RET_OK) && (ret != MBOX_CFGSTAT_STATE_CONFIG)) {
623 		ret = intel_mailbox_get_config_status(MBOX_CONFIG_STATUS,
624 							false);
625 	}
626 
627 	return ret;
628 }
629 
mailbox_hwmon_readtemp(uint32_t chan,uint32_t * resp_buf)630 int mailbox_hwmon_readtemp(uint32_t chan, uint32_t *resp_buf)
631 {
632 	unsigned int resp_len = sizeof(resp_buf);
633 
634 	return mailbox_send_cmd(MBOX_JOB_ID, MBOX_HWMON_READTEMP, &chan, 1U,
635 				CMD_CASUAL, resp_buf,
636 				&resp_len);
637 
638 }
639 
mailbox_hwmon_readvolt(uint32_t chan,uint32_t * resp_buf)640 int mailbox_hwmon_readvolt(uint32_t chan, uint32_t *resp_buf)
641 {
642 	unsigned int resp_len = sizeof(resp_buf);
643 
644 	return mailbox_send_cmd(MBOX_JOB_ID, MBOX_HWMON_READVOLT, &chan, 1U,
645 				CMD_CASUAL, resp_buf,
646 				&resp_len);
647 }
648