Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 1 | /* Copyright 2013-2015 Freescale Semiconductor Inc. |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 2 | * |
| 3 | * SPDX-License-Identifier: GPL-2.0+ |
| 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 | |
| 13 | static inline uint64_t u64_enc(int lsoffset, int width, uint64_t val) |
| 14 | { |
| 15 | return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset); |
| 16 | } |
| 17 | static inline uint64_t u64_dec(uint64_t val, int lsoffset, int width) |
| 18 | { |
| 19 | return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width)); |
| 20 | } |
| 21 | |
| 22 | struct mc_command { |
| 23 | uint64_t header; |
| 24 | uint64_t params[MC_CMD_NUM_OF_PARAMS]; |
| 25 | }; |
| 26 | |
| 27 | enum mc_cmd_status { |
| 28 | MC_CMD_STATUS_OK = 0x0, /*!< Completed successfully */ |
| 29 | MC_CMD_STATUS_READY = 0x1, /*!< Ready to be processed */ |
| 30 | MC_CMD_STATUS_AUTH_ERR = 0x3, /*!< Authentication error */ |
| 31 | MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /*!< No privilege */ |
| 32 | MC_CMD_STATUS_DMA_ERR = 0x5, /*!< DMA or I/O error */ |
| 33 | MC_CMD_STATUS_CONFIG_ERR = 0x6, /*!< Configuration error */ |
| 34 | MC_CMD_STATUS_TIMEOUT = 0x7, /*!< Operation timed out */ |
| 35 | MC_CMD_STATUS_NO_RESOURCE = 0x8, /*!< No resources */ |
| 36 | MC_CMD_STATUS_NO_MEMORY = 0x9, /*!< No memory available */ |
| 37 | MC_CMD_STATUS_BUSY = 0xA, /*!< Device is busy */ |
| 38 | MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /*!< Unsupported operation */ |
| 39 | MC_CMD_STATUS_INVALID_STATE = 0xC /*!< Invalid state */ |
| 40 | }; |
| 41 | |
| 42 | #define MC_CMD_HDR_CMDID_O 52 /* Command ID field offset */ |
| 43 | #define MC_CMD_HDR_CMDID_S 12 /* Command ID field size */ |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 44 | #define MC_CMD_HDR_STATUS_O 16 /* Status field offset */ |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 45 | #define MC_CMD_HDR_TOKEN_O 38 /* Token field offset */ |
| 46 | #define MC_CMD_HDR_TOKEN_S 10 /* Token field size */ |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 47 | #define MC_CMD_HDR_STATUS_S 8 /* Status field size*/ |
| 48 | #define MC_CMD_HDR_PRI_O 15 /* Priority field offset */ |
| 49 | #define MC_CMD_HDR_PRI_S 1 /* Priority field size */ |
| 50 | |
| 51 | #define MC_CMD_HDR_READ_STATUS(_hdr) \ |
| 52 | ((enum mc_cmd_status)u64_dec((_hdr), \ |
| 53 | MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S)) |
| 54 | |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 55 | #define MC_CMD_HDR_READ_TOKEN(_hdr) \ |
| 56 | ((uint16_t)u64_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S)) |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 57 | |
| 58 | #define MC_CMD_PRI_LOW 0 /*!< Low Priority command indication */ |
| 59 | #define MC_CMD_PRI_HIGH 1 /*!< High Priority command indication */ |
| 60 | |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 61 | #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \ |
| 62 | ((_ext)[_param] |= u64_enc((_offset), (_width), _arg)) |
| 63 | |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 64 | #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \ |
| 65 | ((_cmd).params[_param] |= u64_enc((_offset), (_width), _arg)) |
| 66 | |
| 67 | #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \ |
| 68 | (_arg = (_type)u64_dec(_cmd.params[_param], (_offset), (_width))) |
| 69 | |
| 70 | static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id, |
| 71 | uint8_t priority, |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 72 | uint16_t token) |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 73 | { |
| 74 | uint64_t hdr; |
| 75 | |
| 76 | hdr = u64_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id); |
Prabhakar Kushwaha | cfd9fbf | 2015-03-19 09:20:45 -0700 | [diff] [blame] | 77 | hdr |= u64_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token); |
J. German Rivera | 43e4ae3 | 2015-01-06 13:19:02 -0800 | [diff] [blame] | 78 | hdr |= u64_enc(MC_CMD_HDR_PRI_O, MC_CMD_HDR_PRI_S, priority); |
| 79 | hdr |= u64_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S, |
| 80 | MC_CMD_STATUS_READY); |
| 81 | |
| 82 | return hdr; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * mc_write_command - writes a command to a Management Complex (MC) portal |
| 87 | * |
| 88 | * @portal: pointer to an MC portal |
| 89 | * @cmd: pointer to a filled command |
| 90 | */ |
| 91 | static inline void mc_write_command(struct mc_command __iomem *portal, |
| 92 | struct mc_command *cmd) |
| 93 | { |
| 94 | int i; |
| 95 | |
| 96 | /* copy command parameters into the portal */ |
| 97 | for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) |
| 98 | writeq(cmd->params[i], &portal->params[i]); |
| 99 | |
| 100 | /* submit the command by writing the header */ |
| 101 | writeq(cmd->header, &portal->header); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * mc_read_response - reads the response for the last MC command from a |
| 106 | * Management Complex (MC) portal |
| 107 | * |
| 108 | * @portal: pointer to an MC portal |
| 109 | * @resp: pointer to command response buffer |
| 110 | * |
| 111 | * Returns MC_CMD_STATUS_OK on Success; Error code otherwise. |
| 112 | */ |
| 113 | static inline enum mc_cmd_status mc_read_response( |
| 114 | struct mc_command __iomem *portal, |
| 115 | struct mc_command *resp) |
| 116 | { |
| 117 | int i; |
| 118 | enum mc_cmd_status status; |
| 119 | |
| 120 | /* Copy command response header from MC portal: */ |
| 121 | resp->header = readq(&portal->header); |
| 122 | status = MC_CMD_HDR_READ_STATUS(resp->header); |
| 123 | if (status != MC_CMD_STATUS_OK) |
| 124 | return status; |
| 125 | |
| 126 | /* Copy command response data from MC portal: */ |
| 127 | for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) |
| 128 | resp->params[i] = readq(&portal->params[i]); |
| 129 | |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd); |
| 134 | |
| 135 | #endif /* __FSL_MC_CMD_H */ |