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