2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright 2013-2016 Freescale Semiconductor Inc.
8 * Copyright (c) 2016 NXP.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of the above-listed copyright holders nor the
18 * names of any contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") as published by the Free Software
25 * Foundation, either version 2 of that License or (at your option) any
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 #ifndef __FSL_MC_CMD_H
41 #define __FSL_MC_CMD_H
43 #define MC_CMD_NUM_OF_PARAMS 7
45 #define MAKE_UMASK64(_width) \
46 ((uint64_t)((_width) < 64 ? ((uint64_t)1 << (_width)) - 1 : \
49 static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
51 return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
54 static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
56 return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
61 uint64_t params[MC_CMD_NUM_OF_PARAMS];
65 * enum mc_cmd_status - indicates MC status at command response
66 * @MC_CMD_STATUS_OK: Completed successfully
67 * @MC_CMD_STATUS_READY: Ready to be processed
68 * @MC_CMD_STATUS_AUTH_ERR: Authentication error
69 * @MC_CMD_STATUS_NO_PRIVILEGE: No privilege
70 * @MC_CMD_STATUS_DMA_ERR: DMA or I/O error
71 * @MC_CMD_STATUS_CONFIG_ERR: Configuration error
72 * @MC_CMD_STATUS_TIMEOUT: Operation timed out
73 * @MC_CMD_STATUS_NO_RESOURCE: No resources
74 * @MC_CMD_STATUS_NO_MEMORY: No memory available
75 * @MC_CMD_STATUS_BUSY: Device is busy
76 * @MC_CMD_STATUS_UNSUPPORTED_OP: Unsupported operation
77 * @MC_CMD_STATUS_INVALID_STATE: Invalid state
80 MC_CMD_STATUS_OK = 0x0,
81 MC_CMD_STATUS_READY = 0x1,
82 MC_CMD_STATUS_AUTH_ERR = 0x3,
83 MC_CMD_STATUS_NO_PRIVILEGE = 0x4,
84 MC_CMD_STATUS_DMA_ERR = 0x5,
85 MC_CMD_STATUS_CONFIG_ERR = 0x6,
86 MC_CMD_STATUS_TIMEOUT = 0x7,
87 MC_CMD_STATUS_NO_RESOURCE = 0x8,
88 MC_CMD_STATUS_NO_MEMORY = 0x9,
89 MC_CMD_STATUS_BUSY = 0xA,
90 MC_CMD_STATUS_UNSUPPORTED_OP = 0xB,
91 MC_CMD_STATUS_INVALID_STATE = 0xC
94 /* MC command flags */
99 #define MC_CMD_FLAG_PRI 0x00008000
101 * Command completion flag
103 #define MC_CMD_FLAG_INTR_DIS 0x01000000
106 * Command ID field offset
108 #define MC_CMD_HDR_CMDID_O 48
110 * Command ID field size
112 #define MC_CMD_HDR_CMDID_S 16
116 #define MC_CMD_HDR_TOKEN_O 32
120 #define MC_CMD_HDR_TOKEN_S 16
122 * Status field offset
124 #define MC_CMD_HDR_STATUS_O 16
128 #define MC_CMD_HDR_STATUS_S 8
132 #define MC_CMD_HDR_FLAGS_O 0
136 #define MC_CMD_HDR_FLAGS_S 32
140 #define MC_CMD_HDR_FLAGS_MASK 0xFF00FF00
142 #define MC_CMD_HDR_READ_STATUS(_hdr) \
143 ((enum mc_cmd_status)mc_dec((_hdr), \
144 MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
146 #define MC_CMD_HDR_READ_TOKEN(_hdr) \
147 ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
149 #define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
150 ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
152 #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
153 (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
155 #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
156 ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
158 #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
159 (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
161 /* cmd, param, offset, width, type, arg_name */
162 #define CMD_CREATE_RSP_GET_OBJ_ID_PARAM0(cmd, object_id) \
163 MC_RSP_OP(cmd, 0, 0, 32, uint32_t, object_id)
165 /* cmd, param, offset, width, type, arg_name */
166 #define CMD_DESTROY_SET_OBJ_ID_PARAM0(cmd, object_id) \
167 MC_CMD_OP(cmd, 0, 0, 32, uint32_t, object_id)
169 static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
175 hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id);
176 hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S,
177 (cmd_flags & MC_CMD_HDR_FLAGS_MASK));
178 hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token);
179 hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S,
180 MC_CMD_STATUS_READY);
186 * mc_write_command - writes a command to a Management Complex (MC) portal
188 * @portal: pointer to an MC portal
189 * @cmd: pointer to a filled command
191 static inline void mc_write_command(struct mc_command __iomem *portal,
192 struct mc_command *cmd)
196 char *header = (char *)&portal->header;
198 /* copy command parameters into the portal */
199 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
200 iowrite64(cmd->params[i], &portal->params[i]);
202 /* submit the command by writing the header */
203 word = (uint32_t)mc_dec(cmd->header, 32, 32);
204 iowrite32(word, (((uint32_t *)header) + 1));
206 word = (uint32_t)mc_dec(cmd->header, 0, 32);
207 iowrite32(word, (uint32_t *)header);
211 * mc_read_response - reads the response for the last MC command from a
212 * Management Complex (MC) portal
214 * @portal: pointer to an MC portal
215 * @resp: pointer to command response buffer
217 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
219 static inline enum mc_cmd_status mc_read_response(
220 struct mc_command __iomem *portal,
221 struct mc_command *resp)
224 enum mc_cmd_status status;
226 /* Copy command response header from MC portal: */
227 resp->header = ioread64(&portal->header);
228 status = MC_CMD_HDR_READ_STATUS(resp->header);
229 if (status != MC_CMD_STATUS_OK)
232 /* Copy command response data from MC portal: */
233 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
234 resp->params[i] = ioread64(&portal->params[i]);
239 #endif /* __FSL_MC_CMD_H */