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