blob: 591cda9685155094d60604fc11c3f3d7c1a5f3d9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Yogesh Gaura6f2a6e2018-05-09 10:52:17 +05302/* Copyright 2013-2016 Freescale Semiconductor, Inc.
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05303 * Copyright 2017 NXP
J. German Rivera7b3bd9a2015-01-06 13:19:02 -08004 */
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
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053013static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080014{
15 return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
16}
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053017static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080018{
19 return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
20}
21
22struct mc_command {
23 uint64_t header;
24 uint64_t params[MC_CMD_NUM_OF_PARAMS];
25};
26
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053027struct mc_rsp_create {
28 __le32 object_id;
29};
30
31struct mc_rsp_api_ver {
32 __le16 major_ver;
33 __le16 minor_ver;
34};
35
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080036enum 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
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053051/*
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
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053063#define MC_CMD_HDR_CMDID_O 48 /* Command ID field offset */
64#define MC_CMD_HDR_CMDID_S 16 /* Command ID field size */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080065#define MC_CMD_HDR_STATUS_O 16 /* Status field offset */
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053066#define MC_CMD_HDR_TOKEN_O 32 /* Token field offset */
67#define MC_CMD_HDR_TOKEN_S 16 /* Token field size */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080068#define MC_CMD_HDR_STATUS_S 8 /* Status field size*/
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053069#define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */
70#define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053071#define MC_CMD_HDR_FLAGS_MASK 0x0000FFFF /* Command flags mask */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080072
73#define MC_CMD_HDR_READ_STATUS(_hdr) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053074 ((enum mc_cmd_status)mc_dec((_hdr), \
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080075 MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
76
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070077#define MC_CMD_HDR_READ_TOKEN(_hdr) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053078 ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080079
Prabhakar Kushwaha53e353f2015-12-24 15:32:49 +053080#define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
81 ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
82
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070083#define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha53e353f2015-12-24 15:32:49 +053084 (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070085
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080086#define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053087 ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080088
89#define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +053090 (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080091
Yogesh Gaur2557c5a2017-11-15 11:59:31 +053092/* 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
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800100static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530101 uint32_t cmd_flags,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700102 uint16_t token)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800103{
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530104 uint64_t hdr = 0;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800105
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530106 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,
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800111 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 */
122static 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 */
144static 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
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530164/**
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 */
171static 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
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800182#endif /* __FSL_MC_CMD_H */