fsl_mc_cmd.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /* Copyright 2013-2016 Freescale Semiconductor, Inc.
  3. * Copyright 2017 NXP
  4. */
  5. #ifndef __FSL_MC_CMD_H
  6. #define __FSL_MC_CMD_H
  7. #define MC_CMD_NUM_OF_PARAMS 7
  8. #define MAKE_UMASK64(_width) \
  9. ((uint64_t)((_width) < 64 ? ((uint64_t)1 << (_width)) - 1 : -1))
  10. static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
  11. {
  12. return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
  13. }
  14. static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
  15. {
  16. return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
  17. }
  18. struct mc_command {
  19. uint64_t header;
  20. uint64_t params[MC_CMD_NUM_OF_PARAMS];
  21. };
  22. struct mc_rsp_create {
  23. __le32 object_id;
  24. };
  25. struct mc_rsp_api_ver {
  26. __le16 major_ver;
  27. __le16 minor_ver;
  28. };
  29. enum mc_cmd_status {
  30. MC_CMD_STATUS_OK = 0x0, /*!< Completed successfully */
  31. MC_CMD_STATUS_READY = 0x1, /*!< Ready to be processed */
  32. MC_CMD_STATUS_AUTH_ERR = 0x3, /*!< Authentication error */
  33. MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /*!< No privilege */
  34. MC_CMD_STATUS_DMA_ERR = 0x5, /*!< DMA or I/O error */
  35. MC_CMD_STATUS_CONFIG_ERR = 0x6, /*!< Configuration error */
  36. MC_CMD_STATUS_TIMEOUT = 0x7, /*!< Operation timed out */
  37. MC_CMD_STATUS_NO_RESOURCE = 0x8, /*!< No resources */
  38. MC_CMD_STATUS_NO_MEMORY = 0x9, /*!< No memory available */
  39. MC_CMD_STATUS_BUSY = 0xA, /*!< Device is busy */
  40. MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /*!< Unsupported operation */
  41. MC_CMD_STATUS_INVALID_STATE = 0xC /*!< Invalid state */
  42. };
  43. /*
  44. * MC command flags
  45. */
  46. /* High priority flag */
  47. #define MC_CMD_FLAG_PRI 0x00008000
  48. /* No flags */
  49. #define MC_CMD_NO_FLAGS 0x00000000
  50. /* Command completion flag */
  51. #define MC_CMD_FLAG_INTR_DIS 0x01000000
  52. #define MC_CMD_HDR_CMDID_O 48 /* Command ID field offset */
  53. #define MC_CMD_HDR_CMDID_S 16 /* Command ID field size */
  54. #define MC_CMD_HDR_STATUS_O 16 /* Status field offset */
  55. #define MC_CMD_HDR_TOKEN_O 32 /* Token field offset */
  56. #define MC_CMD_HDR_TOKEN_S 16 /* Token field size */
  57. #define MC_CMD_HDR_STATUS_S 8 /* Status field size*/
  58. #define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */
  59. #define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/
  60. #define MC_CMD_HDR_FLAGS_MASK 0x0000FFFF /* Command flags mask */
  61. #define MC_CMD_HDR_READ_STATUS(_hdr) \
  62. ((enum mc_cmd_status)mc_dec((_hdr), \
  63. MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
  64. #define MC_CMD_HDR_READ_TOKEN(_hdr) \
  65. ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
  66. #define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
  67. ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
  68. #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
  69. (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
  70. #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
  71. ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
  72. #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
  73. (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
  74. /* cmd, param, offset, width, type, arg_name */
  75. #define MC_CMD_READ_OBJ_ID(cmd, obj_id) \
  76. MC_RSP_OP(cmd, 0, 0, 32, uint32_t, obj_id)
  77. /* cmd, param, offset, width, type, arg_name */
  78. #define CMD_DESTROY_SET_OBJ_ID_PARAM0(cmd, object_id) \
  79. MC_CMD_OP(cmd, 0, 0, 32, uint32_t, object_id)
  80. static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
  81. uint32_t cmd_flags,
  82. uint16_t token)
  83. {
  84. uint64_t hdr = 0;
  85. hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id);
  86. hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S,
  87. (cmd_flags & MC_CMD_HDR_FLAGS_MASK));
  88. hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token);
  89. hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S,
  90. MC_CMD_STATUS_READY);
  91. return hdr;
  92. }
  93. /**
  94. * mc_write_command - writes a command to a Management Complex (MC) portal
  95. *
  96. * @portal: pointer to an MC portal
  97. * @cmd: pointer to a filled command
  98. */
  99. static inline void mc_write_command(struct mc_command __iomem *portal,
  100. struct mc_command *cmd)
  101. {
  102. int i;
  103. /* copy command parameters into the portal */
  104. for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
  105. writeq(cmd->params[i], &portal->params[i]);
  106. /* submit the command by writing the header */
  107. writeq(cmd->header, &portal->header);
  108. }
  109. /**
  110. * mc_read_response - reads the response for the last MC command from a
  111. * Management Complex (MC) portal
  112. *
  113. * @portal: pointer to an MC portal
  114. * @resp: pointer to command response buffer
  115. *
  116. * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
  117. */
  118. static inline enum mc_cmd_status mc_read_response(
  119. struct mc_command __iomem *portal,
  120. struct mc_command *resp)
  121. {
  122. int i;
  123. enum mc_cmd_status status;
  124. /* Copy command response header from MC portal: */
  125. resp->header = readq(&portal->header);
  126. status = MC_CMD_HDR_READ_STATUS(resp->header);
  127. if (status != MC_CMD_STATUS_OK)
  128. return status;
  129. /* Copy command response data from MC portal: */
  130. for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
  131. resp->params[i] = readq(&portal->params[i]);
  132. return status;
  133. }
  134. /**
  135. * mc_read_version - read version of the given cmd
  136. *
  137. * @cmd: pointer to a filled command
  138. * @major_version: major version value for the given cmd
  139. * @minor_version: minor version value for the given cmd
  140. */
  141. static inline void mc_cmd_read_api_version(struct mc_command *cmd,
  142. u16 *major_ver,
  143. u16 *minor_ver)
  144. {
  145. struct mc_rsp_api_ver *rsp_params;
  146. rsp_params = (struct mc_rsp_api_ver *)cmd->params;
  147. *major_ver = le16_to_cpu(rsp_params->major_ver);
  148. *minor_ver = le16_to_cpu(rsp_params->minor_ver);
  149. }
  150. #endif /* __FSL_MC_CMD_H */