1a2a55e51SPrabhakar 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 13*87457d11SPrabhakar Kushwaha static inline uint64_t mc_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 } 17*87457d11SPrabhakar Kushwaha static inline uint64_t mc_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 42*87457d11SPrabhakar Kushwaha /* 43*87457d11SPrabhakar Kushwaha * MC command flags 44*87457d11SPrabhakar Kushwaha */ 45*87457d11SPrabhakar Kushwaha 46*87457d11SPrabhakar Kushwaha /* High priority flag */ 47*87457d11SPrabhakar Kushwaha #define MC_CMD_FLAG_PRI 0x00008000 48*87457d11SPrabhakar Kushwaha /* No flags */ 49*87457d11SPrabhakar Kushwaha #define MC_CMD_NO_FLAGS 0x00000000 50*87457d11SPrabhakar Kushwaha /* Command completion flag */ 51*87457d11SPrabhakar Kushwaha #define MC_CMD_FLAG_INTR_DIS 0x01000000 52*87457d11SPrabhakar Kushwaha 53*87457d11SPrabhakar Kushwaha 547b3bd9a7SJ. German Rivera #define MC_CMD_HDR_CMDID_O 52 /* Command ID field offset */ 557b3bd9a7SJ. German Rivera #define MC_CMD_HDR_CMDID_S 12 /* Command ID field size */ 567b3bd9a7SJ. German Rivera #define MC_CMD_HDR_STATUS_O 16 /* Status field offset */ 57a2a55e51SPrabhakar Kushwaha #define MC_CMD_HDR_TOKEN_O 38 /* Token field offset */ 58a2a55e51SPrabhakar Kushwaha #define MC_CMD_HDR_TOKEN_S 10 /* Token field size */ 597b3bd9a7SJ. German Rivera #define MC_CMD_HDR_STATUS_S 8 /* Status field size*/ 60*87457d11SPrabhakar Kushwaha #define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */ 61*87457d11SPrabhakar Kushwaha #define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/ 62*87457d11SPrabhakar Kushwaha #define MC_CMD_HDR_FLAGS_MASK 0xFF00FF00 /* Command flags mask */ 637b3bd9a7SJ. German Rivera 647b3bd9a7SJ. German Rivera #define MC_CMD_HDR_READ_STATUS(_hdr) \ 65*87457d11SPrabhakar Kushwaha ((enum mc_cmd_status)mc_dec((_hdr), \ 667b3bd9a7SJ. German Rivera MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S)) 677b3bd9a7SJ. German Rivera 68a2a55e51SPrabhakar Kushwaha #define MC_CMD_HDR_READ_TOKEN(_hdr) \ 69*87457d11SPrabhakar Kushwaha ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S)) 707b3bd9a7SJ. German Rivera 71a2a55e51SPrabhakar Kushwaha #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \ 72*87457d11SPrabhakar Kushwaha ((_ext)[_param] |= mc_enc((_offset), (_width), _arg)) 73a2a55e51SPrabhakar Kushwaha 747b3bd9a7SJ. German Rivera #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \ 75*87457d11SPrabhakar Kushwaha ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg)) 767b3bd9a7SJ. German Rivera 777b3bd9a7SJ. German Rivera #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \ 78*87457d11SPrabhakar Kushwaha (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width))) 797b3bd9a7SJ. German Rivera 807b3bd9a7SJ. German Rivera static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id, 81*87457d11SPrabhakar Kushwaha uint32_t cmd_flags, 82a2a55e51SPrabhakar Kushwaha uint16_t token) 837b3bd9a7SJ. German Rivera { 847b3bd9a7SJ. German Rivera uint64_t hdr; 857b3bd9a7SJ. German Rivera 86*87457d11SPrabhakar Kushwaha hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id); 87*87457d11SPrabhakar Kushwaha hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S, 88*87457d11SPrabhakar Kushwaha (cmd_flags & MC_CMD_HDR_FLAGS_MASK)); 89*87457d11SPrabhakar Kushwaha hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token); 90*87457d11SPrabhakar Kushwaha hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S, 917b3bd9a7SJ. German Rivera MC_CMD_STATUS_READY); 927b3bd9a7SJ. German Rivera 937b3bd9a7SJ. German Rivera return hdr; 947b3bd9a7SJ. German Rivera } 957b3bd9a7SJ. German Rivera 967b3bd9a7SJ. German Rivera /** 977b3bd9a7SJ. German Rivera * mc_write_command - writes a command to a Management Complex (MC) portal 987b3bd9a7SJ. German Rivera * 997b3bd9a7SJ. German Rivera * @portal: pointer to an MC portal 1007b3bd9a7SJ. German Rivera * @cmd: pointer to a filled command 1017b3bd9a7SJ. German Rivera */ 1027b3bd9a7SJ. German Rivera static inline void mc_write_command(struct mc_command __iomem *portal, 1037b3bd9a7SJ. German Rivera struct mc_command *cmd) 1047b3bd9a7SJ. German Rivera { 1057b3bd9a7SJ. German Rivera int i; 1067b3bd9a7SJ. German Rivera 1077b3bd9a7SJ. German Rivera /* copy command parameters into the portal */ 1087b3bd9a7SJ. German Rivera for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) 1097b3bd9a7SJ. German Rivera writeq(cmd->params[i], &portal->params[i]); 1107b3bd9a7SJ. German Rivera 1117b3bd9a7SJ. German Rivera /* submit the command by writing the header */ 1127b3bd9a7SJ. German Rivera writeq(cmd->header, &portal->header); 1137b3bd9a7SJ. German Rivera } 1147b3bd9a7SJ. German Rivera 1157b3bd9a7SJ. German Rivera /** 1167b3bd9a7SJ. German Rivera * mc_read_response - reads the response for the last MC command from a 1177b3bd9a7SJ. German Rivera * Management Complex (MC) portal 1187b3bd9a7SJ. German Rivera * 1197b3bd9a7SJ. German Rivera * @portal: pointer to an MC portal 1207b3bd9a7SJ. German Rivera * @resp: pointer to command response buffer 1217b3bd9a7SJ. German Rivera * 1227b3bd9a7SJ. German Rivera * Returns MC_CMD_STATUS_OK on Success; Error code otherwise. 1237b3bd9a7SJ. German Rivera */ 1247b3bd9a7SJ. German Rivera static inline enum mc_cmd_status mc_read_response( 1257b3bd9a7SJ. German Rivera struct mc_command __iomem *portal, 1267b3bd9a7SJ. German Rivera struct mc_command *resp) 1277b3bd9a7SJ. German Rivera { 1287b3bd9a7SJ. German Rivera int i; 1297b3bd9a7SJ. German Rivera enum mc_cmd_status status; 1307b3bd9a7SJ. German Rivera 1317b3bd9a7SJ. German Rivera /* Copy command response header from MC portal: */ 1327b3bd9a7SJ. German Rivera resp->header = readq(&portal->header); 1337b3bd9a7SJ. German Rivera status = MC_CMD_HDR_READ_STATUS(resp->header); 1347b3bd9a7SJ. German Rivera if (status != MC_CMD_STATUS_OK) 1357b3bd9a7SJ. German Rivera return status; 1367b3bd9a7SJ. German Rivera 1377b3bd9a7SJ. German Rivera /* Copy command response data from MC portal: */ 1387b3bd9a7SJ. German Rivera for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) 1397b3bd9a7SJ. German Rivera resp->params[i] = readq(&portal->params[i]); 1407b3bd9a7SJ. German Rivera 1417b3bd9a7SJ. German Rivera return status; 1427b3bd9a7SJ. German Rivera } 1437b3bd9a7SJ. German Rivera 1447b3bd9a7SJ. German Rivera #endif /* __FSL_MC_CMD_H */ 145