sd_ops.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/mmc/core/sd_ops.h
  4. *
  5. * Copyright 2006-2007 Pierre Ossman
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/types.h>
  9. #include <linux/export.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/mmc/host.h>
  12. #include <linux/mmc/card.h>
  13. #include <linux/mmc/mmc.h>
  14. #include <linux/mmc/sd.h>
  15. #include "core.h"
  16. #include "sd_ops.h"
  17. #include "mmc_ops.h"
  18. /*
  19. * Extensive testing has shown that some specific SD cards
  20. * require an increased command timeout to be successfully
  21. * initialized.
  22. */
  23. #define SD_APP_OP_COND_PERIOD_US (10 * 1000) /* 10ms */
  24. #define SD_APP_OP_COND_TIMEOUT_MS 2000 /* 2s */
  25. struct sd_app_op_cond_busy_data {
  26. struct mmc_host *host;
  27. u32 ocr;
  28. struct mmc_command *cmd;
  29. };
  30. int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
  31. {
  32. int err;
  33. struct mmc_command cmd = {};
  34. if (WARN_ON(card && card->host != host))
  35. return -EINVAL;
  36. cmd.opcode = MMC_APP_CMD;
  37. if (card) {
  38. cmd.arg = card->rca << 16;
  39. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  40. } else {
  41. cmd.arg = 0;
  42. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
  43. }
  44. err = mmc_wait_for_cmd(host, &cmd, 0);
  45. if (err)
  46. return err;
  47. /* Check that card supported application commands */
  48. if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
  49. return -EOPNOTSUPP;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(mmc_app_cmd);
  53. static int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
  54. struct mmc_command *cmd)
  55. {
  56. struct mmc_request mrq = {};
  57. int i, err = -EIO;
  58. /*
  59. * We have to resend MMC_APP_CMD for each attempt so
  60. * we cannot use the retries field in mmc_command.
  61. */
  62. for (i = 0; i <= MMC_CMD_RETRIES; i++) {
  63. err = mmc_app_cmd(host, card);
  64. if (err) {
  65. /* no point in retrying; no APP commands allowed */
  66. if (mmc_host_is_spi(host)) {
  67. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  68. break;
  69. }
  70. continue;
  71. }
  72. memset(&mrq, 0, sizeof(struct mmc_request));
  73. memset(cmd->resp, 0, sizeof(cmd->resp));
  74. cmd->retries = 0;
  75. mrq.cmd = cmd;
  76. cmd->data = NULL;
  77. mmc_wait_for_req(host, &mrq);
  78. err = cmd->error;
  79. if (!cmd->error)
  80. break;
  81. /* no point in retrying illegal APP commands */
  82. if (mmc_host_is_spi(host)) {
  83. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  84. break;
  85. }
  86. }
  87. return err;
  88. }
  89. int mmc_app_set_bus_width(struct mmc_card *card, int width)
  90. {
  91. struct mmc_command cmd = {};
  92. cmd.opcode = SD_APP_SET_BUS_WIDTH;
  93. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  94. switch (width) {
  95. case MMC_BUS_WIDTH_1:
  96. cmd.arg = SD_BUS_WIDTH_1;
  97. break;
  98. case MMC_BUS_WIDTH_4:
  99. cmd.arg = SD_BUS_WIDTH_4;
  100. break;
  101. default:
  102. return -EINVAL;
  103. }
  104. return mmc_wait_for_app_cmd(card->host, card, &cmd);
  105. }
  106. static int sd_app_op_cond_cb(void *cb_data, bool *busy)
  107. {
  108. struct sd_app_op_cond_busy_data *data = cb_data;
  109. struct mmc_host *host = data->host;
  110. struct mmc_command *cmd = data->cmd;
  111. u32 ocr = data->ocr;
  112. int err;
  113. *busy = false;
  114. err = mmc_wait_for_app_cmd(host, NULL, cmd);
  115. if (err)
  116. return err;
  117. /* If we're just probing, do a single pass. */
  118. if (ocr == 0)
  119. return 0;
  120. /* Wait until reset completes. */
  121. if (mmc_host_is_spi(host)) {
  122. if (!(cmd->resp[0] & R1_SPI_IDLE))
  123. return 0;
  124. } else if (cmd->resp[0] & MMC_CARD_BUSY) {
  125. return 0;
  126. }
  127. *busy = true;
  128. return 0;
  129. }
  130. int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  131. {
  132. struct mmc_command cmd = {};
  133. struct sd_app_op_cond_busy_data cb_data = {
  134. .host = host,
  135. .ocr = ocr,
  136. .cmd = &cmd
  137. };
  138. int err;
  139. cmd.opcode = SD_APP_OP_COND;
  140. if (mmc_host_is_spi(host))
  141. cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
  142. else
  143. cmd.arg = ocr;
  144. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
  145. err = __mmc_poll_for_busy(host, SD_APP_OP_COND_PERIOD_US,
  146. SD_APP_OP_COND_TIMEOUT_MS, &sd_app_op_cond_cb,
  147. &cb_data);
  148. if (err)
  149. return err;
  150. if (rocr && !mmc_host_is_spi(host))
  151. *rocr = cmd.resp[0];
  152. return 0;
  153. }
  154. static int __mmc_send_if_cond(struct mmc_host *host, u32 ocr, u8 pcie_bits,
  155. u32 *resp)
  156. {
  157. struct mmc_command cmd = {};
  158. int err;
  159. static const u8 test_pattern = 0xAA;
  160. u8 result_pattern;
  161. /*
  162. * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
  163. * before SD_APP_OP_COND. This command will harmlessly fail for
  164. * SD 1.0 cards.
  165. */
  166. cmd.opcode = SD_SEND_IF_COND;
  167. cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | pcie_bits << 8 | test_pattern;
  168. cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
  169. err = mmc_wait_for_cmd(host, &cmd, 0);
  170. if (err)
  171. return err;
  172. if (mmc_host_is_spi(host))
  173. result_pattern = cmd.resp[1] & 0xFF;
  174. else
  175. result_pattern = cmd.resp[0] & 0xFF;
  176. if (result_pattern != test_pattern)
  177. return -EIO;
  178. if (resp)
  179. *resp = cmd.resp[0];
  180. return 0;
  181. }
  182. int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
  183. {
  184. return __mmc_send_if_cond(host, ocr, 0, NULL);
  185. }
  186. int mmc_send_if_cond_pcie(struct mmc_host *host, u32 ocr)
  187. {
  188. u32 resp = 0;
  189. u8 pcie_bits = 0;
  190. int ret;
  191. if (host->caps2 & MMC_CAP2_SD_EXP) {
  192. /* Probe card for SD express support via PCIe. */
  193. pcie_bits = 0x10;
  194. if (host->caps2 & MMC_CAP2_SD_EXP_1_2V)
  195. /* Probe also for 1.2V support. */
  196. pcie_bits = 0x30;
  197. }
  198. ret = __mmc_send_if_cond(host, ocr, pcie_bits, &resp);
  199. if (ret)
  200. return 0;
  201. /* Continue with the SD express init, if the card supports it. */
  202. resp &= 0x3000;
  203. if (pcie_bits && resp) {
  204. if (resp == 0x3000)
  205. host->ios.timing = MMC_TIMING_SD_EXP_1_2V;
  206. else
  207. host->ios.timing = MMC_TIMING_SD_EXP;
  208. /*
  209. * According to the spec the clock shall also be gated, but
  210. * let's leave this to the host driver for more flexibility.
  211. */
  212. return host->ops->init_sd_express(host, &host->ios);
  213. }
  214. return 0;
  215. }
  216. int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
  217. {
  218. int err;
  219. struct mmc_command cmd = {};
  220. cmd.opcode = SD_SEND_RELATIVE_ADDR;
  221. cmd.arg = 0;
  222. cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
  223. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  224. if (err)
  225. return err;
  226. *rca = cmd.resp[0] >> 16;
  227. return 0;
  228. }
  229. int mmc_app_send_scr(struct mmc_card *card)
  230. {
  231. int err;
  232. struct mmc_request mrq = {};
  233. struct mmc_command cmd = {};
  234. struct mmc_data data = {};
  235. struct scatterlist sg;
  236. __be32 *scr;
  237. /* NOTE: caller guarantees scr is heap-allocated */
  238. err = mmc_app_cmd(card->host, card);
  239. if (err)
  240. return err;
  241. /* dma onto stack is unsafe/nonportable, but callers to this
  242. * routine normally provide temporary on-stack buffers ...
  243. */
  244. scr = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
  245. if (!scr)
  246. return -ENOMEM;
  247. mrq.cmd = &cmd;
  248. mrq.data = &data;
  249. cmd.opcode = SD_APP_SEND_SCR;
  250. cmd.arg = 0;
  251. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  252. data.blksz = 8;
  253. data.blocks = 1;
  254. data.flags = MMC_DATA_READ;
  255. data.sg = &sg;
  256. data.sg_len = 1;
  257. sg_init_one(&sg, scr, 8);
  258. mmc_set_data_timeout(&data, card);
  259. mmc_wait_for_req(card->host, &mrq);
  260. card->raw_scr[0] = be32_to_cpu(scr[0]);
  261. card->raw_scr[1] = be32_to_cpu(scr[1]);
  262. kfree(scr);
  263. if (cmd.error)
  264. return cmd.error;
  265. if (data.error)
  266. return data.error;
  267. return 0;
  268. }
  269. int mmc_sd_switch(struct mmc_card *card, bool mode, int group,
  270. u8 value, u8 *resp)
  271. {
  272. u32 cmd_args;
  273. /* NOTE: caller guarantees resp is heap-allocated */
  274. value &= 0xF;
  275. cmd_args = mode << 31 | 0x00FFFFFF;
  276. cmd_args &= ~(0xF << (group * 4));
  277. cmd_args |= value << (group * 4);
  278. return mmc_send_adtc_data(card, card->host, SD_SWITCH, cmd_args, resp,
  279. 64);
  280. }
  281. EXPORT_SYMBOL_GPL(mmc_sd_switch);
  282. int mmc_app_sd_status(struct mmc_card *card, void *ssr)
  283. {
  284. int err;
  285. struct mmc_request mrq = {};
  286. struct mmc_command cmd = {};
  287. struct mmc_data data = {};
  288. struct scatterlist sg;
  289. /* NOTE: caller guarantees ssr is heap-allocated */
  290. err = mmc_app_cmd(card->host, card);
  291. if (err)
  292. return err;
  293. mrq.cmd = &cmd;
  294. mrq.data = &data;
  295. cmd.opcode = SD_APP_SD_STATUS;
  296. cmd.arg = 0;
  297. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
  298. data.blksz = 64;
  299. data.blocks = 1;
  300. data.flags = MMC_DATA_READ;
  301. data.sg = &sg;
  302. data.sg_len = 1;
  303. sg_init_one(&sg, ssr, 64);
  304. mmc_set_data_timeout(&data, card);
  305. mmc_wait_for_req(card->host, &mrq);
  306. if (cmd.error)
  307. return cmd.error;
  308. if (data.error)
  309. return data.error;
  310. return 0;
  311. }