mmc_ops.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2018 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <device.h>
  8. #include <string.h>
  9. #include <drivers/mmc/mmc.h>
  10. #include <drivers/mmc/sd.h>
  11. #include "mmc_ops.h"
  12. int mmc_select_card(const struct device *mmc_dev, struct mmc_cmd *cmd, int rca)
  13. {
  14. int err;
  15. memset(cmd, 0, sizeof(struct mmc_cmd));
  16. cmd->opcode = MMC_SELECT_CARD;
  17. if (rca) {
  18. cmd->arg = rca << 16;
  19. cmd->flags = MMC_RSP_R1B | MMC_CMD_AC;
  20. } else {
  21. cmd->arg = 0;
  22. cmd->flags = MMC_RSP_NONE | MMC_CMD_AC;
  23. }
  24. err = mmc_send_cmd(mmc_dev, cmd);
  25. if (err)
  26. return err;
  27. return 0;
  28. }
  29. int mmc_go_idle(const struct device *mmc_dev, struct mmc_cmd *cmd)
  30. {
  31. int err;
  32. memset(cmd, 0, sizeof(struct mmc_cmd));
  33. cmd->opcode = MMC_GO_IDLE_STATE;
  34. cmd->arg = 0;
  35. cmd->flags = MMC_RSP_NONE | MMC_CMD_BC;
  36. err = mmc_send_cmd(mmc_dev, cmd);
  37. k_sleep(K_MSEC(1));
  38. return err;
  39. }
  40. int mmc_send_status(const struct device *mmc_dev, struct mmc_cmd *cmd, int rca, u32_t *status)
  41. {
  42. int err;
  43. memset(cmd, 0, sizeof(struct mmc_cmd));
  44. cmd->opcode = MMC_SEND_STATUS;
  45. cmd->arg = rca << 16;
  46. cmd->flags = MMC_RSP_R1 | MMC_CMD_AC;
  47. err = mmc_send_cmd(mmc_dev, cmd);
  48. if (err)
  49. return err;
  50. if (status)
  51. *status = cmd->resp[0];
  52. return 0;
  53. }
  54. int mmc_app_cmd(const struct device *mmc_dev, struct mmc_cmd *cmd, int rca)
  55. {
  56. int err;
  57. memset(cmd, 0, sizeof(struct mmc_cmd));
  58. cmd->opcode = MMC_APP_CMD;
  59. if (rca) {
  60. cmd->arg = rca << 16;
  61. cmd->flags = MMC_RSP_R1 | MMC_CMD_AC;
  62. } else {
  63. cmd->arg = 0;
  64. cmd->flags = MMC_RSP_R1 | MMC_CMD_BCR;
  65. }
  66. err = mmc_send_cmd(mmc_dev, cmd);
  67. if (err)
  68. return err;
  69. /* Check that card supported application commands */
  70. if (!(cmd->resp[0] & R1_APP_CMD))
  71. return -EOPNOTSUPP;
  72. return 0;
  73. }
  74. int mmc_send_app_cmd(const struct device *mmc_dev, int rca, struct mmc_cmd *cmd,
  75. int retries)
  76. {
  77. int i, err = 0;
  78. struct mmc_cmd app_cmd;
  79. for (i = 0; i <= retries; i++) {
  80. err = mmc_app_cmd(mmc_dev, &app_cmd, rca);
  81. if (err)
  82. continue;
  83. err = mmc_send_cmd(mmc_dev, cmd);
  84. if (!err)
  85. break;
  86. }
  87. return err;
  88. }
  89. int mmc_all_send_cid(const struct device *mmc_dev, struct mmc_cmd *cmd, u32_t *cid)
  90. {
  91. int err;
  92. memset(cmd, 0, sizeof(struct mmc_cmd));
  93. cmd->opcode = MMC_ALL_SEND_CID;
  94. cmd->arg = 0;
  95. cmd->flags = MMC_RSP_R2 | MMC_CMD_BCR;
  96. err = mmc_send_cmd(mmc_dev, cmd);
  97. if (err)
  98. return err;
  99. memcpy(cid, cmd->resp, sizeof(u32_t) * 4);
  100. return 0;
  101. }
  102. int mmc_send_csd(const struct device *mmc_dev, struct mmc_cmd *cmd, int rca, u32_t *csd)
  103. {
  104. int err;
  105. memset(cmd, 0, sizeof(struct mmc_cmd));
  106. cmd->opcode = MMC_SEND_CSD;
  107. cmd->arg = rca << 16;
  108. cmd->flags = MMC_RSP_R2 | MMC_CMD_AC;
  109. err = mmc_send_cmd(mmc_dev, cmd);
  110. if (err)
  111. return err;
  112. memcpy(csd, cmd->resp, sizeof(u32_t) * 4);
  113. return 0;
  114. }
  115. int mmc_send_ext_csd(const struct device *mmc_dev, struct mmc_cmd *cmd, u8_t *ext_csd)
  116. {
  117. int err;
  118. memset(cmd, 0, sizeof(struct mmc_cmd));
  119. cmd->opcode = MMC_SEND_EXT_CSD;
  120. cmd->arg = 0;
  121. cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC | MMC_DATA_READ;
  122. cmd->blk_size = 512;
  123. cmd->blk_num = 1;
  124. cmd->buf = ext_csd;
  125. err = mmc_send_cmd(mmc_dev, cmd);
  126. if (err)
  127. return err;
  128. return 0;
  129. }
  130. int mmc_set_blockcount(const struct device *mmc_dev, struct mmc_cmd *cmd, unsigned int blockcount,
  131. bool is_rel_write)
  132. {
  133. memset(cmd, 0, sizeof(struct mmc_cmd));
  134. cmd->opcode = MMC_SET_BLOCK_COUNT;
  135. cmd->flags = MMC_RSP_R1 | MMC_CMD_AC;
  136. cmd->arg = blockcount & 0x0000FFFF;
  137. if (is_rel_write)
  138. cmd->arg |= 1 << 31;
  139. return mmc_send_cmd(mmc_dev, cmd);
  140. }
  141. int mmc_stop_block_transmission(const struct device *mmc_dev, struct mmc_cmd *cmd)
  142. {
  143. memset(cmd, 0, sizeof(struct mmc_cmd));
  144. cmd->opcode = MMC_STOP_TRANSMISSION;
  145. cmd->flags = MMC_RSP_R1 | MMC_CMD_AC;
  146. cmd->arg = 0;
  147. return mmc_send_cmd(mmc_dev, cmd);
  148. }
  149. int mmc_send_relative_addr(const struct device *mmc_dev, struct mmc_cmd *cmd, unsigned int *rca)
  150. {
  151. int err;
  152. memset(cmd, 0, sizeof(struct mmc_cmd));
  153. cmd->opcode = SD_SEND_RELATIVE_ADDR;
  154. cmd->arg = 0;
  155. cmd->flags = MMC_RSP_R6 | MMC_CMD_BCR;
  156. err = mmc_send_cmd(mmc_dev, cmd);
  157. if (err)
  158. return err;
  159. *rca = cmd->resp[0] >> 16;
  160. return 0;
  161. }
  162. int emmc_send_relative_addr(const struct device *mmc_dev, struct mmc_cmd *cmd, unsigned int *rca)
  163. {
  164. int err;
  165. memset(cmd, 0, sizeof(struct mmc_cmd));
  166. cmd->opcode = SD_SEND_RELATIVE_ADDR;
  167. cmd->arg = *rca << 16;
  168. cmd->flags = MMC_RSP_R1 | MMC_CMD_AC;
  169. err = mmc_send_cmd(mmc_dev, cmd);
  170. if (err)
  171. return err;
  172. return 0;
  173. }