fsl_mc_cmd.h 4.2 KB

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