xref: /rk3399_ARM-atf/drivers/arm/css/scp/css_sds.c (revision 2d4135e08fb11989a4bbd6ebf9f3c1b324493237)
1 /*
2  * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdint.h>
9 
10 #include <arch_helpers.h>
11 #include <common/debug.h>
12 #include <drivers/arm/css/css_scp.h>
13 #include <drivers/delay_timer.h>
14 #include <plat/common/platform.h>
15 #include <platform_def.h>
16 
17 #include "../sds/sds.h"
18 
19 int css_scp_boot_image_xfer(void *image, unsigned int image_size)
20 {
21 	int ret;
22 	unsigned int image_offset, image_flags;
23 
24 	ret = sds_init();
25 	if (ret != SDS_OK) {
26 		ERROR("SCP SDS initialization failed\n");
27 		panic();
28 	}
29 
30 	VERBOSE("Writing SCP image metadata\n");
31 	image_offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE;
32 	ret = sds_struct_write(SDS_SCP_IMG_STRUCT_ID, SDS_SCP_IMG_ADDR_OFFSET,
33 			&image_offset, SDS_SCP_IMG_ADDR_SIZE,
34 			SDS_ACCESS_MODE_NON_CACHED);
35 	if (ret != SDS_OK)
36 		goto sds_fail;
37 
38 	ret = sds_struct_write(SDS_SCP_IMG_STRUCT_ID, SDS_SCP_IMG_SIZE_OFFSET,
39 			&image_size, SDS_SCP_IMG_SIZE_SIZE,
40 			SDS_ACCESS_MODE_NON_CACHED);
41 	if (ret != SDS_OK)
42 		goto sds_fail;
43 
44 	VERBOSE("Marking SCP image metadata as valid\n");
45 	image_flags = SDS_SCP_IMG_VALID_FLAG_BIT;
46 	ret = sds_struct_write(SDS_SCP_IMG_STRUCT_ID, SDS_SCP_IMG_FLAG_OFFSET,
47 			&image_flags, SDS_SCP_IMG_FLAG_SIZE,
48 			SDS_ACCESS_MODE_NON_CACHED);
49 	if (ret != SDS_OK)
50 		goto sds_fail;
51 
52 	return 0;
53 sds_fail:
54 	ERROR("SCP SDS write to SCP IMG struct failed\n");
55 	panic();
56 }
57 
58 /*
59  * API to wait for SCP to signal till it's ready after booting the transferred
60  * image.
61  */
62 int css_scp_boot_ready(void)
63 {
64 	uint32_t scp_feature_availability_flags;
65 	int ret, retry = CSS_SCP_READY_10US_RETRIES;
66 
67 
68 	VERBOSE("Waiting for SCP RAM to complete its initialization process\n");
69 
70 	/* Wait for the SCP RAM Firmware to complete its initialization process */
71 	while (retry > 0) {
72 		ret = sds_struct_read(SDS_FEATURE_AVAIL_STRUCT_ID, 0,
73 				&scp_feature_availability_flags,
74 				SDS_FEATURE_AVAIL_SIZE,
75 				SDS_ACCESS_MODE_NON_CACHED);
76 		if (ret == SDS_ERR_STRUCT_NOT_FINALIZED)
77 			continue;
78 
79 		if (ret != SDS_OK) {
80 			ERROR(" sds_struct_read failed\n");
81 			panic();
82 		}
83 
84 		if (scp_feature_availability_flags &
85 				SDS_FEATURE_AVAIL_SCP_RAM_READY_BIT)
86 			return 0;
87 
88 		udelay(10);
89 		retry--;
90 	}
91 
92 	ERROR("Timeout of %d ms expired waiting for SCP RAM Ready flag\n",
93 			CSS_SCP_READY_10US_RETRIES/100);
94 
95 	plat_panic_handler();
96 }
97