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
mc_enc(int lsoffset,int width,uint64_t val)1387457d11SPrabhakar 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 }
mc_dec(uint64_t val,int lsoffset,int width)1787457d11SPrabhakar 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
4287457d11SPrabhakar Kushwaha /*
4387457d11SPrabhakar Kushwaha * MC command flags
4487457d11SPrabhakar Kushwaha */
4587457d11SPrabhakar Kushwaha
4687457d11SPrabhakar Kushwaha /* High priority flag */
4787457d11SPrabhakar Kushwaha #define MC_CMD_FLAG_PRI 0x00008000
4887457d11SPrabhakar Kushwaha /* No flags */
4987457d11SPrabhakar Kushwaha #define MC_CMD_NO_FLAGS 0x00000000
5087457d11SPrabhakar Kushwaha /* Command completion flag */
5187457d11SPrabhakar Kushwaha #define MC_CMD_FLAG_INTR_DIS 0x01000000
5287457d11SPrabhakar Kushwaha
5387457d11SPrabhakar 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*/
6087457d11SPrabhakar Kushwaha #define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */
6187457d11SPrabhakar Kushwaha #define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/
6287457d11SPrabhakar Kushwaha #define MC_CMD_HDR_FLAGS_MASK 0xFF00FF00 /* Command flags mask */
637b3bd9a7SJ. German Rivera
647b3bd9a7SJ. German Rivera #define MC_CMD_HDR_READ_STATUS(_hdr) \
6587457d11SPrabhakar 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) \
6987457d11SPrabhakar Kushwaha ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
707b3bd9a7SJ. German Rivera
71*53e353fcSPrabhakar Kushwaha #define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
72*53e353fcSPrabhakar Kushwaha ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
73*53e353fcSPrabhakar Kushwaha
74a2a55e51SPrabhakar Kushwaha #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
75*53e353fcSPrabhakar Kushwaha (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
76a2a55e51SPrabhakar Kushwaha
777b3bd9a7SJ. German Rivera #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
7887457d11SPrabhakar Kushwaha ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
797b3bd9a7SJ. German Rivera
807b3bd9a7SJ. German Rivera #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
8187457d11SPrabhakar Kushwaha (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
827b3bd9a7SJ. German Rivera
mc_encode_cmd_header(uint16_t cmd_id,uint32_t cmd_flags,uint16_t token)837b3bd9a7SJ. German Rivera static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
8487457d11SPrabhakar Kushwaha uint32_t cmd_flags,
85a2a55e51SPrabhakar Kushwaha uint16_t token)
867b3bd9a7SJ. German Rivera {
877b3bd9a7SJ. German Rivera uint64_t hdr;
887b3bd9a7SJ. German Rivera
8987457d11SPrabhakar Kushwaha hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id);
9087457d11SPrabhakar Kushwaha hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S,
9187457d11SPrabhakar Kushwaha (cmd_flags & MC_CMD_HDR_FLAGS_MASK));
9287457d11SPrabhakar Kushwaha hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token);
9387457d11SPrabhakar Kushwaha hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S,
947b3bd9a7SJ. German Rivera MC_CMD_STATUS_READY);
957b3bd9a7SJ. German Rivera
967b3bd9a7SJ. German Rivera return hdr;
977b3bd9a7SJ. German Rivera }
987b3bd9a7SJ. German Rivera
997b3bd9a7SJ. German Rivera /**
1007b3bd9a7SJ. German Rivera * mc_write_command - writes a command to a Management Complex (MC) portal
1017b3bd9a7SJ. German Rivera *
1027b3bd9a7SJ. German Rivera * @portal: pointer to an MC portal
1037b3bd9a7SJ. German Rivera * @cmd: pointer to a filled command
1047b3bd9a7SJ. German Rivera */
mc_write_command(struct mc_command __iomem * portal,struct mc_command * cmd)1057b3bd9a7SJ. German Rivera static inline void mc_write_command(struct mc_command __iomem *portal,
1067b3bd9a7SJ. German Rivera struct mc_command *cmd)
1077b3bd9a7SJ. German Rivera {
1087b3bd9a7SJ. German Rivera int i;
1097b3bd9a7SJ. German Rivera
1107b3bd9a7SJ. German Rivera /* copy command parameters into the portal */
1117b3bd9a7SJ. German Rivera for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
1127b3bd9a7SJ. German Rivera writeq(cmd->params[i], &portal->params[i]);
1137b3bd9a7SJ. German Rivera
1147b3bd9a7SJ. German Rivera /* submit the command by writing the header */
1157b3bd9a7SJ. German Rivera writeq(cmd->header, &portal->header);
1167b3bd9a7SJ. German Rivera }
1177b3bd9a7SJ. German Rivera
1187b3bd9a7SJ. German Rivera /**
1197b3bd9a7SJ. German Rivera * mc_read_response - reads the response for the last MC command from a
1207b3bd9a7SJ. German Rivera * Management Complex (MC) portal
1217b3bd9a7SJ. German Rivera *
1227b3bd9a7SJ. German Rivera * @portal: pointer to an MC portal
1237b3bd9a7SJ. German Rivera * @resp: pointer to command response buffer
1247b3bd9a7SJ. German Rivera *
1257b3bd9a7SJ. German Rivera * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
1267b3bd9a7SJ. German Rivera */
mc_read_response(struct mc_command __iomem * portal,struct mc_command * resp)1277b3bd9a7SJ. German Rivera static inline enum mc_cmd_status mc_read_response(
1287b3bd9a7SJ. German Rivera struct mc_command __iomem *portal,
1297b3bd9a7SJ. German Rivera struct mc_command *resp)
1307b3bd9a7SJ. German Rivera {
1317b3bd9a7SJ. German Rivera int i;
1327b3bd9a7SJ. German Rivera enum mc_cmd_status status;
1337b3bd9a7SJ. German Rivera
1347b3bd9a7SJ. German Rivera /* Copy command response header from MC portal: */
1357b3bd9a7SJ. German Rivera resp->header = readq(&portal->header);
1367b3bd9a7SJ. German Rivera status = MC_CMD_HDR_READ_STATUS(resp->header);
1377b3bd9a7SJ. German Rivera if (status != MC_CMD_STATUS_OK)
1387b3bd9a7SJ. German Rivera return status;
1397b3bd9a7SJ. German Rivera
1407b3bd9a7SJ. German Rivera /* Copy command response data from MC portal: */
1417b3bd9a7SJ. German Rivera for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
1427b3bd9a7SJ. German Rivera resp->params[i] = readq(&portal->params[i]);
1437b3bd9a7SJ. German Rivera
1447b3bd9a7SJ. German Rivera return status;
1457b3bd9a7SJ. German Rivera }
1467b3bd9a7SJ. German Rivera
1477b3bd9a7SJ. German Rivera #endif /* __FSL_MC_CMD_H */
148