blob: 1ec67b549164f4a7a48969d8eb9f004228252891 [file] [log] [blame]
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301/* Copyright 2013-2016 Freescale Semiconductor Inc.
2 * Copyright 2017 NXP
J. German Rivera7b3bd9a2015-01-06 13:19:02 -08003 *
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
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053014static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080015{
16 return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
17}
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053018static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080019{
20 return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
21}
22
23struct mc_command {
24 uint64_t header;
25 uint64_t params[MC_CMD_NUM_OF_PARAMS];
26};
27
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053028struct mc_rsp_create {
29 __le32 object_id;
30};
31
32struct mc_rsp_api_ver {
33 __le16 major_ver;
34 __le16 minor_ver;
35};
36
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080037enum 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
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053052/*
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
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053064#define MC_CMD_HDR_CMDID_O 48 /* Command ID field offset */
65#define MC_CMD_HDR_CMDID_S 16 /* Command ID field size */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080066#define MC_CMD_HDR_STATUS_O 16 /* Status field offset */
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053067#define MC_CMD_HDR_TOKEN_O 32 /* Token field offset */
68#define MC_CMD_HDR_TOKEN_S 16 /* Token field size */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080069#define MC_CMD_HDR_STATUS_S 8 /* Status field size*/
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053070#define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */
71#define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053072#define MC_CMD_HDR_FLAGS_MASK 0x0000FFFF /* Command flags mask */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080073
74#define MC_CMD_HDR_READ_STATUS(_hdr) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053075 ((enum mc_cmd_status)mc_dec((_hdr), \
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080076 MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
77
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070078#define MC_CMD_HDR_READ_TOKEN(_hdr) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053079 ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080080
Prabhakar Kushwaha53e353f2015-12-24 15:32:49 +053081#define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
82 ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
83
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070084#define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha53e353f2015-12-24 15:32:49 +053085 (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070086
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080087#define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053088 ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080089
90#define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053091 (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080092
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053093/* 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
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800101static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530102 uint32_t cmd_flags,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700103 uint16_t token)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800104{
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530105 uint64_t hdr = 0;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800106
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530107 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,
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800112 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 */
123static 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 */
145static 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
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530165/**
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 */
172static 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
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800183#endif /* __FSL_MC_CMD_H */