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