fsl_mc_cmd.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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. 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. /*
  37. * MC command flags
  38. */
  39. /* High priority flag */
  40. #define MC_CMD_FLAG_PRI 0x00008000
  41. /* No flags */
  42. #define MC_CMD_NO_FLAGS 0x00000000
  43. /* Command completion flag */
  44. #define MC_CMD_FLAG_INTR_DIS 0x01000000
  45. #define MC_CMD_HDR_CMDID_O 52 /* Command ID field offset */
  46. #define MC_CMD_HDR_CMDID_S 12 /* Command ID field size */
  47. #define MC_CMD_HDR_STATUS_O 16 /* Status field offset */
  48. #define MC_CMD_HDR_TOKEN_O 38 /* Token field offset */
  49. #define MC_CMD_HDR_TOKEN_S 10 /* Token field size */
  50. #define MC_CMD_HDR_STATUS_S 8 /* Status field size*/
  51. #define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */
  52. #define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/
  53. #define MC_CMD_HDR_FLAGS_MASK 0xFF00FF00 /* Command flags mask */
  54. #define MC_CMD_HDR_READ_STATUS(_hdr) \
  55. ((enum mc_cmd_status)mc_dec((_hdr), \
  56. MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
  57. #define MC_CMD_HDR_READ_TOKEN(_hdr) \
  58. ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
  59. #define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
  60. ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
  61. #define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
  62. (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
  63. #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
  64. ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
  65. #define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
  66. (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
  67. static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
  68. uint32_t cmd_flags,
  69. uint16_t token)
  70. {
  71. uint64_t hdr;
  72. hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id);
  73. hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S,
  74. (cmd_flags & MC_CMD_HDR_FLAGS_MASK));
  75. hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token);
  76. hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S,
  77. MC_CMD_STATUS_READY);
  78. return hdr;
  79. }
  80. /**
  81. * mc_write_command - writes a command to a Management Complex (MC) portal
  82. *
  83. * @portal: pointer to an MC portal
  84. * @cmd: pointer to a filled command
  85. */
  86. static inline void mc_write_command(struct mc_command __iomem *portal,
  87. struct mc_command *cmd)
  88. {
  89. int i;
  90. /* copy command parameters into the portal */
  91. for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
  92. writeq(cmd->params[i], &portal->params[i]);
  93. /* submit the command by writing the header */
  94. writeq(cmd->header, &portal->header);
  95. }
  96. /**
  97. * mc_read_response - reads the response for the last MC command from a
  98. * Management Complex (MC) portal
  99. *
  100. * @portal: pointer to an MC portal
  101. * @resp: pointer to command response buffer
  102. *
  103. * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
  104. */
  105. static inline enum mc_cmd_status mc_read_response(
  106. struct mc_command __iomem *portal,
  107. struct mc_command *resp)
  108. {
  109. int i;
  110. enum mc_cmd_status status;
  111. /* Copy command response header from MC portal: */
  112. resp->header = readq(&portal->header);
  113. status = MC_CMD_HDR_READ_STATUS(resp->header);
  114. if (status != MC_CMD_STATUS_OK)
  115. return status;
  116. /* Copy command response data from MC portal: */
  117. for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
  118. resp->params[i] = readq(&portal->params[i]);
  119. return status;
  120. }
  121. #endif /* __FSL_MC_CMD_H */