xref: /openbmc/u-boot/include/fsl-mc/fsl_mc_cmd.h (revision 9d466f2f)
1 /* Copyright 2013-2016 Freescale Semiconductor Inc.
2  * Copyright 2017 NXP
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 #ifndef __FSL_MC_CMD_H
7 #define __FSL_MC_CMD_H
8 
9 #define MC_CMD_NUM_OF_PARAMS	7
10 
11 #define MAKE_UMASK64(_width) \
12 	((uint64_t)((_width) < 64 ? ((uint64_t)1 << (_width)) - 1 : -1))
13 
14 static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
15 {
16 	return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
17 }
18 static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
19 {
20 	return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
21 }
22 
23 struct mc_command {
24 	uint64_t header;
25 	uint64_t params[MC_CMD_NUM_OF_PARAMS];
26 };
27 
28 struct mc_rsp_create {
29 	__le32 object_id;
30 };
31 
32 struct mc_rsp_api_ver {
33 	__le16 major_ver;
34 	__le16 minor_ver;
35 };
36 
37 enum mc_cmd_status {
38 	MC_CMD_STATUS_OK = 0x0, /*!< Completed successfully */
39 	MC_CMD_STATUS_READY = 0x1, /*!< Ready to be processed */
40 	MC_CMD_STATUS_AUTH_ERR = 0x3, /*!< Authentication error */
41 	MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /*!< No privilege */
42 	MC_CMD_STATUS_DMA_ERR = 0x5, /*!< DMA or I/O error */
43 	MC_CMD_STATUS_CONFIG_ERR = 0x6, /*!< Configuration error */
44 	MC_CMD_STATUS_TIMEOUT = 0x7, /*!< Operation timed out */
45 	MC_CMD_STATUS_NO_RESOURCE = 0x8, /*!< No resources */
46 	MC_CMD_STATUS_NO_MEMORY = 0x9, /*!< No memory available */
47 	MC_CMD_STATUS_BUSY = 0xA, /*!< Device is busy */
48 	MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /*!< Unsupported operation */
49 	MC_CMD_STATUS_INVALID_STATE = 0xC /*!< Invalid state */
50 };
51 
52 /*
53  * MC command flags
54  */
55 
56 /* High priority flag */
57 #define MC_CMD_FLAG_PRI		0x00008000
58 /* No flags */
59 #define MC_CMD_NO_FLAGS		0x00000000
60 /* Command completion flag */
61 #define MC_CMD_FLAG_INTR_DIS	0x01000000
62 
63 
64 #define MC_CMD_HDR_CMDID_O	48	/* Command ID field offset */
65 #define MC_CMD_HDR_CMDID_S	16	/* Command ID field size */
66 #define MC_CMD_HDR_STATUS_O	16	/* Status field offset */
67 #define MC_CMD_HDR_TOKEN_O	32	/* Token field offset */
68 #define MC_CMD_HDR_TOKEN_S	16	/* Token field size */
69 #define MC_CMD_HDR_STATUS_S	8	/* Status field size*/
70 #define MC_CMD_HDR_FLAGS_O	0	/* Flags field offset */
71 #define MC_CMD_HDR_FLAGS_S	32	/* Flags field size*/
72 #define MC_CMD_HDR_FLAGS_MASK	0x0000FFFF /* Command flags mask */
73 
74 #define MC_CMD_HDR_READ_STATUS(_hdr) \
75 	((enum mc_cmd_status)mc_dec((_hdr), \
76 		MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
77 
78 #define MC_CMD_HDR_READ_TOKEN(_hdr) \
79 	((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
80 
81 #define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
82 	((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
83 
84 #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
85 	(_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
86 
87 #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
88 	((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
89 
90 #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
91 	(_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
92 
93 /*                cmd, param, offset, width, type, arg_name */
94 #define MC_CMD_READ_OBJ_ID(cmd, obj_id) \
95 	MC_RSP_OP(cmd, 0, 0,  32,  uint32_t,	    obj_id)
96 
97 /* cmd, param, offset, width, type, arg_name */
98 #define CMD_DESTROY_SET_OBJ_ID_PARAM0(cmd, object_id) \
99 	MC_CMD_OP(cmd, 0, 0,  32,  uint32_t,  object_id)
100 
101 static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
102 					    uint32_t cmd_flags,
103 					    uint16_t token)
104 {
105 	uint64_t hdr = 0;
106 
107 	hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id);
108 	hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S,
109 		       (cmd_flags & MC_CMD_HDR_FLAGS_MASK));
110 	hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token);
111 	hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S,
112 		       MC_CMD_STATUS_READY);
113 
114 	return hdr;
115 }
116 
117 /**
118  * mc_write_command - writes a command to a Management Complex (MC) portal
119  *
120  * @portal: pointer to an MC portal
121  * @cmd: pointer to a filled command
122  */
123 static inline void mc_write_command(struct mc_command __iomem *portal,
124 				    struct mc_command *cmd)
125 {
126 	int i;
127 
128 	/* copy command parameters into the portal */
129 	for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
130 		writeq(cmd->params[i], &portal->params[i]);
131 
132 	/* submit the command by writing the header */
133 	writeq(cmd->header, &portal->header);
134 }
135 
136 /**
137  * mc_read_response - reads the response for the last MC command from a
138  * Management Complex (MC) portal
139  *
140  * @portal: pointer to an MC portal
141  * @resp: pointer to command response buffer
142  *
143  * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
144  */
145 static inline enum mc_cmd_status mc_read_response(
146 					struct mc_command __iomem *portal,
147 					struct mc_command *resp)
148 {
149 	int i;
150 	enum mc_cmd_status status;
151 
152 	/* Copy command response header from MC portal: */
153 	resp->header = readq(&portal->header);
154 	status = MC_CMD_HDR_READ_STATUS(resp->header);
155 	if (status != MC_CMD_STATUS_OK)
156 		return status;
157 
158 	/* Copy command response data from MC portal: */
159 	for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
160 		resp->params[i] = readq(&portal->params[i]);
161 
162 	return status;
163 }
164 
165 /**
166  * mc_read_version - read version of the given cmd
167  *
168  * @cmd: pointer to a filled command
169  * @major_version: major version value for the given cmd
170  * @minor_version: minor version value for the given cmd
171  */
172 static inline void mc_cmd_read_api_version(struct mc_command *cmd,
173 					   u16 *major_ver,
174 					   u16 *minor_ver)
175 {
176 	struct mc_rsp_api_ver *rsp_params;
177 
178 	rsp_params = (struct mc_rsp_api_ver *)cmd->params;
179 	*major_ver = le16_to_cpu(rsp_params->major_ver);
180 	*minor_ver = le16_to_cpu(rsp_params->minor_ver);
181 }
182 
183 #endif /* __FSL_MC_CMD_H */
184