1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2019, Broadcom
4  */
5 
6 #include <drivers/bcm_sotp.h>
7 #include <io.h>
8 #include <kernel/misc.h>
9 #include <kernel/pseudo_ta.h>
10 
11 #define SOTP_SERVICE_UUID \
12 		{0x6272636D, 0x2018, 0x1101,  \
13 		{0x42, 0x43, 0x4D, 0x5F, 0x53, 0x4F, 0x54, 0x50} }
14 
15 enum pta_bcm_sotp_cmd {
16 	PTA_BCM_SOTP_CMD_READ = 0,
17 	PTA_BCM_SOTP_CMD_WRITE,
18 };
19 
20 #define SOTP_TA_NAME		"pta_bcm_sotp.ta"
21 
pta_sotp_read(uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])22 static TEE_Result pta_sotp_read(uint32_t param_types,
23 				TEE_Param params[TEE_NUM_PARAMS])
24 {
25 	uint64_t sotp_row_value = 0;
26 	uint32_t val = 0;
27 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
28 						   TEE_PARAM_TYPE_VALUE_OUTPUT,
29 						   TEE_PARAM_TYPE_NONE,
30 						   TEE_PARAM_TYPE_NONE);
31 	if (exp_param_types != param_types) {
32 		EMSG("Invalid Param types");
33 		return TEE_ERROR_BAD_PARAMETERS;
34 	}
35 
36 	val = params[0].value.a;
37 
38 	bcm_iproc_sotp_mem_read(val, 1, &sotp_row_value);
39 	reg_pair_from_64(sotp_row_value, &params[1].value.a,
40 			 &params[1].value.b);
41 
42 	return TEE_SUCCESS;
43 }
44 
pta_sotp_write(uint32_t param_types __unused,TEE_Param params[TEE_NUM_PARAMS]__unused)45 static TEE_Result pta_sotp_write(uint32_t param_types __unused,
46 				 TEE_Param params[TEE_NUM_PARAMS] __unused)
47 {
48 	/* Nothing as of now */
49 	return TEE_ERROR_NOT_IMPLEMENTED;
50 }
51 
invoke_command(void * session_context __unused,uint32_t cmd_id,uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])52 static TEE_Result invoke_command(void *session_context __unused,
53 				 uint32_t cmd_id,
54 				 uint32_t param_types,
55 				 TEE_Param params[TEE_NUM_PARAMS])
56 {
57 	TEE_Result res = TEE_SUCCESS;
58 
59 	DMSG("command entry point[%d] for \"%s\"", cmd_id, SOTP_TA_NAME);
60 
61 	switch (cmd_id) {
62 	case PTA_BCM_SOTP_CMD_READ:
63 		res = pta_sotp_read(param_types, params);
64 		break;
65 	case PTA_BCM_SOTP_CMD_WRITE:
66 		res = pta_sotp_write(param_types, params);
67 		break;
68 	default:
69 		EMSG("cmd %d Not supported %s", cmd_id, SOTP_TA_NAME);
70 		res = TEE_ERROR_NOT_SUPPORTED;
71 		break;
72 	}
73 
74 	return res;
75 }
76 
77 pseudo_ta_register(.uuid = SOTP_SERVICE_UUID,
78 		   .name = SOTP_TA_NAME,
79 		   .flags = PTA_DEFAULT_FLAGS,
80 		   .invoke_command_entry_point = invoke_command);
81