1*a2a55e51SPrabhakar Kushwaha /* Copyright 2013-2015 Freescale Semiconductor Inc. 27b3bd9a7SJ. German Rivera * 37b3bd9a7SJ. German Rivera * SPDX-License-Identifier: GPL-2.0+ 47b3bd9a7SJ. German Rivera */ 57b3bd9a7SJ. German Rivera #ifndef __FSL_MC_CMD_H 67b3bd9a7SJ. German Rivera #define __FSL_MC_CMD_H 77b3bd9a7SJ. German Rivera 87b3bd9a7SJ. German Rivera #define MC_CMD_NUM_OF_PARAMS 7 97b3bd9a7SJ. German Rivera 107b3bd9a7SJ. German Rivera #define MAKE_UMASK64(_width) \ 117b3bd9a7SJ. German Rivera ((uint64_t)((_width) < 64 ? ((uint64_t)1 << (_width)) - 1 : -1)) 127b3bd9a7SJ. German Rivera 137b3bd9a7SJ. German Rivera static inline uint64_t u64_enc(int lsoffset, int width, uint64_t val) 147b3bd9a7SJ. German Rivera { 157b3bd9a7SJ. German Rivera return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset); 167b3bd9a7SJ. German Rivera } 177b3bd9a7SJ. German Rivera static inline uint64_t u64_dec(uint64_t val, int lsoffset, int width) 187b3bd9a7SJ. German Rivera { 197b3bd9a7SJ. German Rivera return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width)); 207b3bd9a7SJ. German Rivera } 217b3bd9a7SJ. German Rivera 227b3bd9a7SJ. German Rivera struct mc_command { 237b3bd9a7SJ. German Rivera uint64_t header; 247b3bd9a7SJ. German Rivera uint64_t params[MC_CMD_NUM_OF_PARAMS]; 257b3bd9a7SJ. German Rivera }; 267b3bd9a7SJ. German Rivera 277b3bd9a7SJ. German Rivera enum mc_cmd_status { 287b3bd9a7SJ. German Rivera MC_CMD_STATUS_OK = 0x0, /*!< Completed successfully */ 297b3bd9a7SJ. German Rivera MC_CMD_STATUS_READY = 0x1, /*!< Ready to be processed */ 307b3bd9a7SJ. German Rivera MC_CMD_STATUS_AUTH_ERR = 0x3, /*!< Authentication error */ 317b3bd9a7SJ. German Rivera MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /*!< No privilege */ 327b3bd9a7SJ. German Rivera MC_CMD_STATUS_DMA_ERR = 0x5, /*!< DMA or I/O error */ 337b3bd9a7SJ. German Rivera MC_CMD_STATUS_CONFIG_ERR = 0x6, /*!< Configuration error */ 347b3bd9a7SJ. German Rivera MC_CMD_STATUS_TIMEOUT = 0x7, /*!< Operation timed out */ 357b3bd9a7SJ. German Rivera MC_CMD_STATUS_NO_RESOURCE = 0x8, /*!< No resources */ 367b3bd9a7SJ. German Rivera MC_CMD_STATUS_NO_MEMORY = 0x9, /*!< No memory available */ 377b3bd9a7SJ. German Rivera MC_CMD_STATUS_BUSY = 0xA, /*!< Device is busy */ 387b3bd9a7SJ. German Rivera MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /*!< Unsupported operation */ 397b3bd9a7SJ. German Rivera MC_CMD_STATUS_INVALID_STATE = 0xC /*!< Invalid state */ 407b3bd9a7SJ. German Rivera }; 417b3bd9a7SJ. German Rivera 427b3bd9a7SJ. German Rivera #define MC_CMD_HDR_CMDID_O 52 /* Command ID field offset */ 437b3bd9a7SJ. German Rivera #define MC_CMD_HDR_CMDID_S 12 /* Command ID field size */ 447b3bd9a7SJ. German Rivera #define MC_CMD_HDR_STATUS_O 16 /* Status field offset */ 45*a2a55e51SPrabhakar Kushwaha #define MC_CMD_HDR_TOKEN_O 38 /* Token field offset */ 46*a2a55e51SPrabhakar Kushwaha #define MC_CMD_HDR_TOKEN_S 10 /* Token field size */ 477b3bd9a7SJ. German Rivera #define MC_CMD_HDR_STATUS_S 8 /* Status field size*/ 487b3bd9a7SJ. German Rivera #define MC_CMD_HDR_PRI_O 15 /* Priority field offset */ 497b3bd9a7SJ. German Rivera #define MC_CMD_HDR_PRI_S 1 /* Priority field size */ 507b3bd9a7SJ. German Rivera 517b3bd9a7SJ. German Rivera #define MC_CMD_HDR_READ_STATUS(_hdr) \ 527b3bd9a7SJ. German Rivera ((enum mc_cmd_status)u64_dec((_hdr), \ 537b3bd9a7SJ. German Rivera MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S)) 547b3bd9a7SJ. German Rivera 55*a2a55e51SPrabhakar Kushwaha #define MC_CMD_HDR_READ_TOKEN(_hdr) \ 56*a2a55e51SPrabhakar Kushwaha ((uint16_t)u64_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S)) 577b3bd9a7SJ. German Rivera 587b3bd9a7SJ. German Rivera #define MC_CMD_PRI_LOW 0 /*!< Low Priority command indication */ 597b3bd9a7SJ. German Rivera #define MC_CMD_PRI_HIGH 1 /*!< High Priority command indication */ 607b3bd9a7SJ. German Rivera 61*a2a55e51SPrabhakar Kushwaha #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \ 62*a2a55e51SPrabhakar Kushwaha ((_ext)[_param] |= u64_enc((_offset), (_width), _arg)) 63*a2a55e51SPrabhakar Kushwaha 647b3bd9a7SJ. German Rivera #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \ 657b3bd9a7SJ. German Rivera ((_cmd).params[_param] |= u64_enc((_offset), (_width), _arg)) 667b3bd9a7SJ. German Rivera 677b3bd9a7SJ. German Rivera #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \ 687b3bd9a7SJ. German Rivera (_arg = (_type)u64_dec(_cmd.params[_param], (_offset), (_width))) 697b3bd9a7SJ. German Rivera 707b3bd9a7SJ. German Rivera static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id, 717b3bd9a7SJ. German Rivera uint8_t priority, 72*a2a55e51SPrabhakar Kushwaha uint16_t token) 737b3bd9a7SJ. German Rivera { 747b3bd9a7SJ. German Rivera uint64_t hdr; 757b3bd9a7SJ. German Rivera 767b3bd9a7SJ. German Rivera hdr = u64_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id); 77*a2a55e51SPrabhakar Kushwaha hdr |= u64_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token); 787b3bd9a7SJ. German Rivera hdr |= u64_enc(MC_CMD_HDR_PRI_O, MC_CMD_HDR_PRI_S, priority); 797b3bd9a7SJ. German Rivera hdr |= u64_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S, 807b3bd9a7SJ. German Rivera MC_CMD_STATUS_READY); 817b3bd9a7SJ. German Rivera 827b3bd9a7SJ. German Rivera return hdr; 837b3bd9a7SJ. German Rivera } 847b3bd9a7SJ. German Rivera 857b3bd9a7SJ. German Rivera /** 867b3bd9a7SJ. German Rivera * mc_write_command - writes a command to a Management Complex (MC) portal 877b3bd9a7SJ. German Rivera * 887b3bd9a7SJ. German Rivera * @portal: pointer to an MC portal 897b3bd9a7SJ. German Rivera * @cmd: pointer to a filled command 907b3bd9a7SJ. German Rivera */ 917b3bd9a7SJ. German Rivera static inline void mc_write_command(struct mc_command __iomem *portal, 927b3bd9a7SJ. German Rivera struct mc_command *cmd) 937b3bd9a7SJ. German Rivera { 947b3bd9a7SJ. German Rivera int i; 957b3bd9a7SJ. German Rivera 967b3bd9a7SJ. German Rivera /* copy command parameters into the portal */ 977b3bd9a7SJ. German Rivera for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) 987b3bd9a7SJ. German Rivera writeq(cmd->params[i], &portal->params[i]); 997b3bd9a7SJ. German Rivera 1007b3bd9a7SJ. German Rivera /* submit the command by writing the header */ 1017b3bd9a7SJ. German Rivera writeq(cmd->header, &portal->header); 1027b3bd9a7SJ. German Rivera } 1037b3bd9a7SJ. German Rivera 1047b3bd9a7SJ. German Rivera /** 1057b3bd9a7SJ. German Rivera * mc_read_response - reads the response for the last MC command from a 1067b3bd9a7SJ. German Rivera * Management Complex (MC) portal 1077b3bd9a7SJ. German Rivera * 1087b3bd9a7SJ. German Rivera * @portal: pointer to an MC portal 1097b3bd9a7SJ. German Rivera * @resp: pointer to command response buffer 1107b3bd9a7SJ. German Rivera * 1117b3bd9a7SJ. German Rivera * Returns MC_CMD_STATUS_OK on Success; Error code otherwise. 1127b3bd9a7SJ. German Rivera */ 1137b3bd9a7SJ. German Rivera static inline enum mc_cmd_status mc_read_response( 1147b3bd9a7SJ. German Rivera struct mc_command __iomem *portal, 1157b3bd9a7SJ. German Rivera struct mc_command *resp) 1167b3bd9a7SJ. German Rivera { 1177b3bd9a7SJ. German Rivera int i; 1187b3bd9a7SJ. German Rivera enum mc_cmd_status status; 1197b3bd9a7SJ. German Rivera 1207b3bd9a7SJ. German Rivera /* Copy command response header from MC portal: */ 1217b3bd9a7SJ. German Rivera resp->header = readq(&portal->header); 1227b3bd9a7SJ. German Rivera status = MC_CMD_HDR_READ_STATUS(resp->header); 1237b3bd9a7SJ. German Rivera if (status != MC_CMD_STATUS_OK) 1247b3bd9a7SJ. German Rivera return status; 1257b3bd9a7SJ. German Rivera 1267b3bd9a7SJ. German Rivera /* Copy command response data from MC portal: */ 1277b3bd9a7SJ. German Rivera for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) 1287b3bd9a7SJ. German Rivera resp->params[i] = readq(&portal->params[i]); 1297b3bd9a7SJ. German Rivera 1307b3bd9a7SJ. German Rivera return status; 1317b3bd9a7SJ. German Rivera } 1327b3bd9a7SJ. German Rivera 1337b3bd9a7SJ. German Rivera int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd); 1347b3bd9a7SJ. German Rivera 1357b3bd9a7SJ. German Rivera #endif /* __FSL_MC_CMD_H */ 136