0ca4345c6a8c3748f7a6c4c6f9862a193167c8ea
[deb_dpdk.git] / drivers / bus / fslmc / mc / fsl_mc_cmd.h
1 /*-
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.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2013-2016 Freescale Semiconductor Inc.
8  * Copyright 2016 NXP.
9  *
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.
20  *
21  *   GPL LICENSE SUMMARY
22  *
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
26  * later version.
27  *
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.
39  */
40 #ifndef __FSL_MC_CMD_H
41 #define __FSL_MC_CMD_H
42
43 #define MC_CMD_NUM_OF_PARAMS    7
44
45 #define MAKE_UMASK64(_width) \
46         ((uint64_t)((_width) < 64 ? ((uint64_t)1 << (_width)) - 1 : \
47                        (uint64_t)-1))
48
49 static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
50 {
51         return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
52 }
53
54 static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
55 {
56         return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
57 }
58
59 struct mc_command {
60         uint64_t header;
61         uint64_t params[MC_CMD_NUM_OF_PARAMS];
62 };
63
64 /**
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
78  */
79 enum mc_cmd_status {
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
92 };
93
94 /*  MC command flags */
95
96 /**
97  * High priority flag
98  */
99 #define MC_CMD_FLAG_PRI         0x00008000
100 /**
101  * Command completion flag
102  */
103 #define MC_CMD_FLAG_INTR_DIS    0x01000000
104
105 /**
106  * Command ID field offset
107  */
108 #define MC_CMD_HDR_CMDID_O      48
109 /**
110  * Command ID field size
111  */
112 #define MC_CMD_HDR_CMDID_S      16
113 /**
114  * Token field offset
115  */
116 #define MC_CMD_HDR_TOKEN_O      32
117 /**
118  * Token field size
119  */
120 #define MC_CMD_HDR_TOKEN_S      16
121 /**
122  * Status field offset
123  */
124 #define MC_CMD_HDR_STATUS_O     16
125 /**
126  * Status field size
127  */
128 #define MC_CMD_HDR_STATUS_S     8
129 /**
130  * Flags field offset
131  */
132 #define MC_CMD_HDR_FLAGS_O      0
133 /**
134  * Flags field size
135  */
136 #define MC_CMD_HDR_FLAGS_S      32
137 /**
138  *  Command flags mask
139  */
140 #define MC_CMD_HDR_FLAGS_MASK   0xFF00FF00
141
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))
145
146 #define MC_CMD_HDR_READ_TOKEN(_hdr) \
147         ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
148
149 #define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
150         ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
151
152 #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
153         (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
154
155 #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
156         ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
157
158 #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
159         (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
160
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)
164
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)
168
169 static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
170                                             uint32_t cmd_flags,
171                                             uint16_t token)
172 {
173         uint64_t hdr;
174
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);
181
182         return hdr;
183 }
184
185 /**
186  * mc_write_command - writes a command to a Management Complex (MC) portal
187  *
188  * @portal: pointer to an MC portal
189  * @cmd: pointer to a filled command
190  */
191 static inline void mc_write_command(struct mc_command __iomem *portal,
192                                     struct mc_command *cmd)
193 {
194         int i;
195         uint32_t word;
196         char *header = (char *)&portal->header;
197
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]);
201
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));
205
206         word = (uint32_t)mc_dec(cmd->header, 0, 32);
207         iowrite32(word, (uint32_t *)header);
208 }
209
210 /**
211  * mc_read_response - reads the response for the last MC command from a
212  * Management Complex (MC) portal
213  *
214  * @portal: pointer to an MC portal
215  * @resp: pointer to command response buffer
216  *
217  * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
218  */
219 static inline enum mc_cmd_status mc_read_response(
220                                         struct mc_command __iomem *portal,
221                                         struct mc_command *resp)
222 {
223         int i;
224         enum mc_cmd_status status;
225
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)
230                 return status;
231
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]);
235
236         return status;
237 }
238
239 #endif /* __FSL_MC_CMD_H */