sd_ops.c 8.4 KB

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