blob: 675e642efd085f6c79fa58e9d4e20a7f60f405da [file] [log] [blame]
Thomas Choud52ebf12010-12-24 13:12:21 +00001/*
2 * generic mmc spi driver
3 *
4 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
Bhargav Shah05e35d42019-07-08 04:10:48 +00005 * Copyright 2019 Bhargav Shah <bhargavshah1988@gmail.com>
6 *
Thomas Choud52ebf12010-12-24 13:12:21 +00007 * Licensed under the GPL-2 or later.
8 */
Jaehoon Chung915ffa52016-07-19 16:33:36 +09009#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Thomas Choud52ebf12010-12-24 13:12:21 +000011#include <malloc.h>
12#include <part.h>
13#include <mmc.h>
Bhargav Shah05e35d42019-07-08 04:10:48 +000014#include <stdlib.h>
Simon Glasscd93d622020-05-10 11:40:13 -060015#include <linux/bitops.h>
Philipp Tomsicha740ee92018-11-25 19:22:18 +010016#include <u-boot/crc.h>
Thomas Choud52ebf12010-12-24 13:12:21 +000017#include <linux/crc7.h>
Yoshinori Sato6f67b692015-06-01 15:22:37 +090018#include <asm/byteorder.h>
Bhargav Shah05e35d42019-07-08 04:10:48 +000019#include <dm.h>
20#include <spi.h>
Thomas Choud52ebf12010-12-24 13:12:21 +000021
22/* MMC/SD in SPI mode reports R1 status always */
Bhargav Shah05e35d42019-07-08 04:10:48 +000023#define R1_SPI_IDLE BIT(0)
24#define R1_SPI_ERASE_RESET BIT(1)
25#define R1_SPI_ILLEGAL_COMMAND BIT(2)
26#define R1_SPI_COM_CRC BIT(3)
27#define R1_SPI_ERASE_SEQ BIT(4)
28#define R1_SPI_ADDRESS BIT(5)
29#define R1_SPI_PARAMETER BIT(6)
Thomas Choud52ebf12010-12-24 13:12:21 +000030/* R1 bit 7 is always zero, reuse this bit for error */
Bhargav Shah05e35d42019-07-08 04:10:48 +000031#define R1_SPI_ERROR BIT(7)
Thomas Choud52ebf12010-12-24 13:12:21 +000032
33/* Response tokens used to ack each block written: */
34#define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
35#define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
36#define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
37#define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
38
Bin Meng46938ab2021-02-02 10:48:48 +080039/*
40 * Read and write blocks start with these tokens and end with crc;
Thomas Choud52ebf12010-12-24 13:12:21 +000041 * on error, read tokens act like a subset of R2_SPI_* values.
42 */
Bhargav Shah05e35d42019-07-08 04:10:48 +000043/* single block write multiblock read */
44#define SPI_TOKEN_SINGLE 0xfe
45/* multiblock write */
46#define SPI_TOKEN_MULTI_WRITE 0xfc
47/* terminate multiblock write */
48#define SPI_TOKEN_STOP_TRAN 0xfd
Thomas Choud52ebf12010-12-24 13:12:21 +000049
50/* MMC SPI commands start with a start bit "0" and a transmit bit "1" */
Bhargav Shah05e35d42019-07-08 04:10:48 +000051#define MMC_SPI_CMD(x) (0x40 | (x))
Thomas Choud52ebf12010-12-24 13:12:21 +000052
53/* bus capability */
Bhargav Shah05e35d42019-07-08 04:10:48 +000054#define MMC_SPI_VOLTAGE (MMC_VDD_32_33 | MMC_VDD_33_34)
55#define MMC_SPI_MIN_CLOCK 400000 /* 400KHz to meet MMC spec */
56#define MMC_SPI_MAX_CLOCK 25000000 /* SD/MMC legacy speed */
Thomas Choud52ebf12010-12-24 13:12:21 +000057
58/* timeout value */
Bhargav Shah05e35d42019-07-08 04:10:48 +000059#define CMD_TIMEOUT 8
60#define READ_TIMEOUT 3000000 /* 1 sec */
61#define WRITE_TIMEOUT 3000000 /* 1 sec */
Pragnesh Pateled4a11c2020-06-29 15:17:29 +053062#define R1B_TIMEOUT 3000000 /* 1 sec */
Thomas Choud52ebf12010-12-24 13:12:21 +000063
Bin Mengd3302392019-08-30 21:15:33 -070064struct mmc_spi_plat {
Bhargav Shah05e35d42019-07-08 04:10:48 +000065 struct mmc_config cfg;
66 struct mmc mmc;
67};
68
Bin Mengd3302392019-08-30 21:15:33 -070069struct mmc_spi_priv {
70 struct spi_slave *spi;
71};
72
Bin Meng46938ab2021-02-02 10:48:48 +080073/**
74 * mmc_spi_sendcmd() - send a command to the SD card
75 *
76 * @dev: mmc_spi device
77 * @cmdidx: command index
78 * @cmdarg: command argument
79 * @resp_type: card response type
80 * @resp: buffer to store the card response
81 * @resp_size: size of the card response
82 * @resp_match: if true, compare each of received bytes with @resp_match_value
83 * @resp_match_value: a value to be compared with each of received bytes
84 * @r1b: if true, receive additional bytes for busy signal token
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010085 * Return: 0 if OK, -ETIMEDOUT if no card response is received, -ve on error
Bin Meng46938ab2021-02-02 10:48:48 +080086 */
Bhargav Shah05e35d42019-07-08 04:10:48 +000087static int mmc_spi_sendcmd(struct udevice *dev,
88 ushort cmdidx, u32 cmdarg, u32 resp_type,
89 u8 *resp, u32 resp_size,
Pragnesh Pateled4a11c2020-06-29 15:17:29 +053090 bool resp_match, u8 resp_match_value, bool r1b)
Thomas Choud52ebf12010-12-24 13:12:21 +000091{
Bhargav Shah05e35d42019-07-08 04:10:48 +000092 int i, rpos = 0, ret = 0;
93 u8 cmdo[7], r;
94
Bin Meng781aad02021-02-02 10:48:46 +080095 if (!resp || !resp_size)
96 return 0;
97
Bhargav Shah05e35d42019-07-08 04:10:48 +000098 debug("%s: cmd%d cmdarg=0x%x resp_type=0x%x "
99 "resp_size=%d resp_match=%d resp_match_value=0x%x\n",
100 __func__, cmdidx, cmdarg, resp_type,
101 resp_size, resp_match, resp_match_value);
102
Thomas Choud52ebf12010-12-24 13:12:21 +0000103 cmdo[0] = 0xff;
104 cmdo[1] = MMC_SPI_CMD(cmdidx);
105 cmdo[2] = cmdarg >> 24;
106 cmdo[3] = cmdarg >> 16;
107 cmdo[4] = cmdarg >> 8;
108 cmdo[5] = cmdarg;
109 cmdo[6] = (crc7(0, &cmdo[1], 5) << 1) | 0x01;
Anup Patela7060292019-07-17 04:23:38 +0000110 ret = dm_spi_xfer(dev, sizeof(cmdo) * 8, cmdo, NULL, SPI_XFER_BEGIN);
Bhargav Shah05e35d42019-07-08 04:10:48 +0000111 if (ret)
112 return ret;
Thomas Choud52ebf12010-12-24 13:12:21 +0000113
Bhargav Shah05e35d42019-07-08 04:10:48 +0000114 ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0);
115 if (ret)
116 return ret;
117
Bhargav Shah05e35d42019-07-08 04:10:48 +0000118 debug("%s: cmd%d", __func__, cmdidx);
119
Bin Meng2f22cb42021-02-02 10:48:47 +0800120 if (resp_match)
Bhargav Shah05e35d42019-07-08 04:10:48 +0000121 r = ~resp_match_value;
Bin Meng2f22cb42021-02-02 10:48:47 +0800122 i = CMD_TIMEOUT;
123 while (i) {
124 ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0);
125 if (ret)
126 return ret;
127 debug(" resp%d=0x%x", rpos, r);
128 rpos++;
129 i--;
Pragnesh Patel3ba1d532020-06-29 15:17:24 +0530130
Bin Meng2f22cb42021-02-02 10:48:47 +0800131 if (resp_match) {
Bhargav Shah05e35d42019-07-08 04:10:48 +0000132 if (r == resp_match_value)
Thomas Choud52ebf12010-12-24 13:12:21 +0000133 break;
Bin Meng2f22cb42021-02-02 10:48:47 +0800134 } else {
135 if (!(r & 0x80))
136 break;
Thomas Choud52ebf12010-12-24 13:12:21 +0000137 }
Bin Meng2f22cb42021-02-02 10:48:47 +0800138
139 if (!i)
Bhargav Shah05e35d42019-07-08 04:10:48 +0000140 return -ETIMEDOUT;
141 }
142
Bin Meng2f22cb42021-02-02 10:48:47 +0800143 resp[0] = r;
144 for (i = 1; i < resp_size; i++) {
Bhargav Shah05e35d42019-07-08 04:10:48 +0000145 ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0);
146 if (ret)
147 return ret;
148 debug(" resp%d=0x%x", rpos, r);
149 rpos++;
150 resp[i] = r;
151 }
152
Pragnesh Pateled4a11c2020-06-29 15:17:29 +0530153 if (r1b == true) {
154 i = R1B_TIMEOUT;
155 while (i) {
156 ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0);
157 if (ret)
158 return ret;
159
160 debug(" resp%d=0x%x", rpos, r);
161 rpos++;
162 i--;
163
164 if (r)
165 break;
166 }
167 if (!i)
168 return -ETIMEDOUT;
169 }
170
Bhargav Shah05e35d42019-07-08 04:10:48 +0000171 debug("\n");
172
173 return 0;
174}
175
Bin Meng46938ab2021-02-02 10:48:48 +0800176/**
177 * mmc_spi_readdata() - read data block(s) from the SD card
178 *
179 * @dev: mmc_spi device
180 * @xbuf: buffer of the actual data (excluding token and crc) to read
181 * @bcnt: number of data blocks to transfer
182 * @bsize: size of the actual data (excluding token and crc) in bytes
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100183 * Return: 0 if OK, -ECOMM if crc error, -ETIMEDOUT on other errors
Bin Meng46938ab2021-02-02 10:48:48 +0800184 */
Bhargav Shah05e35d42019-07-08 04:10:48 +0000185static int mmc_spi_readdata(struct udevice *dev,
186 void *xbuf, u32 bcnt, u32 bsize)
187{
188 u16 crc;
189 u8 *buf = xbuf, r1;
190 int i, ret = 0;
191
192 while (bcnt--) {
193 for (i = 0; i < READ_TIMEOUT; i++) {
194 ret = dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0);
195 if (ret)
196 return ret;
197 if (r1 == SPI_TOKEN_SINGLE)
198 break;
199 }
200 debug("%s: data tok%d 0x%x\n", __func__, i, r1);
Thomas Choud52ebf12010-12-24 13:12:21 +0000201 if (r1 == SPI_TOKEN_SINGLE) {
Bhargav Shah05e35d42019-07-08 04:10:48 +0000202 ret = dm_spi_xfer(dev, bsize * 8, NULL, buf, 0);
203 if (ret)
204 return ret;
205 ret = dm_spi_xfer(dev, 2 * 8, NULL, &crc, 0);
206 if (ret)
207 return ret;
Thomas Choud52ebf12010-12-24 13:12:21 +0000208#ifdef CONFIG_MMC_SPI_CRC_ON
Bin Meng01962f82021-02-02 10:32:48 +0800209 u16 crc_ok = be16_to_cpu(crc16_ccitt(0, buf, bsize));
210 if (crc_ok != crc) {
211 debug("%s: data crc error, expected %04x got %04x\n",
212 __func__, crc_ok, crc);
Thomas Choud52ebf12010-12-24 13:12:21 +0000213 r1 = R1_SPI_COM_CRC;
214 break;
215 }
216#endif
217 r1 = 0;
218 } else {
219 r1 = R1_SPI_ERROR;
220 break;
221 }
222 buf += bsize;
223 }
Bhargav Shah05e35d42019-07-08 04:10:48 +0000224
225 if (r1 & R1_SPI_COM_CRC)
226 ret = -ECOMM;
227 else if (r1) /* other errors */
228 ret = -ETIMEDOUT;
229
230 return ret;
Thomas Choud52ebf12010-12-24 13:12:21 +0000231}
232
Bin Meng46938ab2021-02-02 10:48:48 +0800233/**
234 * mmc_spi_writedata() - write data block(s) to the SD card
235 *
236 * @dev: mmc_spi device
237 * @xbuf: buffer of the actual data (excluding token and crc) to write
238 * @bcnt: number of data blocks to transfer
239 * @bsize: size of actual data (excluding token and crc) in bytes
240 * @multi: indicate a transfer by multiple block write command (CMD25)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100241 * Return: 0 if OK, -ECOMM if crc error, -ETIMEDOUT on other errors
Bin Meng46938ab2021-02-02 10:48:48 +0800242 */
Bhargav Shah05e35d42019-07-08 04:10:48 +0000243static int mmc_spi_writedata(struct udevice *dev, const void *xbuf,
244 u32 bcnt, u32 bsize, int multi)
Thomas Choud52ebf12010-12-24 13:12:21 +0000245{
Thomas Choud52ebf12010-12-24 13:12:21 +0000246 const u8 *buf = xbuf;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000247 u8 r1, tok[2];
Thomas Choud52ebf12010-12-24 13:12:21 +0000248 u16 crc;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000249 int i, ret = 0;
250
Thomas Choud52ebf12010-12-24 13:12:21 +0000251 tok[0] = 0xff;
252 tok[1] = multi ? SPI_TOKEN_MULTI_WRITE : SPI_TOKEN_SINGLE;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000253
Thomas Choud52ebf12010-12-24 13:12:21 +0000254 while (bcnt--) {
255#ifdef CONFIG_MMC_SPI_CRC_ON
Stefan Roeseecb57f62016-03-03 09:34:12 +0100256 crc = cpu_to_be16(crc16_ccitt(0, (u8 *)buf, bsize));
Thomas Choud52ebf12010-12-24 13:12:21 +0000257#endif
Bhargav Shah05e35d42019-07-08 04:10:48 +0000258 dm_spi_xfer(dev, 2 * 8, tok, NULL, 0);
259 dm_spi_xfer(dev, bsize * 8, buf, NULL, 0);
260 dm_spi_xfer(dev, 2 * 8, &crc, NULL, 0);
261 for (i = 0; i < CMD_TIMEOUT; i++) {
262 dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0);
Thomas Choud52ebf12010-12-24 13:12:21 +0000263 if ((r1 & 0x10) == 0) /* response token */
264 break;
265 }
Bhargav Shah05e35d42019-07-08 04:10:48 +0000266 debug("%s: data tok%d 0x%x\n", __func__, i, r1);
Thomas Choud52ebf12010-12-24 13:12:21 +0000267 if (SPI_MMC_RESPONSE_CODE(r1) == SPI_RESPONSE_ACCEPTED) {
Bhargav Shah05e35d42019-07-08 04:10:48 +0000268 debug("%s: data accepted\n", __func__);
269 for (i = 0; i < WRITE_TIMEOUT; i++) { /* wait busy */
270 dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0);
Thomas Choud52ebf12010-12-24 13:12:21 +0000271 if (i && r1 == 0xff) {
272 r1 = 0;
273 break;
274 }
275 }
Bhargav Shah05e35d42019-07-08 04:10:48 +0000276 if (i == WRITE_TIMEOUT) {
277 debug("%s: data write timeout 0x%x\n",
278 __func__, r1);
Thomas Choud52ebf12010-12-24 13:12:21 +0000279 r1 = R1_SPI_ERROR;
280 break;
281 }
282 } else {
Bhargav Shah05e35d42019-07-08 04:10:48 +0000283 debug("%s: data error 0x%x\n", __func__, r1);
Thomas Choud52ebf12010-12-24 13:12:21 +0000284 r1 = R1_SPI_COM_CRC;
285 break;
286 }
287 buf += bsize;
288 }
289 if (multi && bcnt == -1) { /* stop multi write */
290 tok[1] = SPI_TOKEN_STOP_TRAN;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000291 dm_spi_xfer(dev, 2 * 8, tok, NULL, 0);
292 for (i = 0; i < WRITE_TIMEOUT; i++) { /* wait busy */
293 dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0);
Thomas Choud52ebf12010-12-24 13:12:21 +0000294 if (i && r1 == 0xff) {
295 r1 = 0;
296 break;
297 }
298 }
Bhargav Shah05e35d42019-07-08 04:10:48 +0000299 if (i == WRITE_TIMEOUT) {
300 debug("%s: data write timeout 0x%x\n", __func__, r1);
Thomas Choud52ebf12010-12-24 13:12:21 +0000301 r1 = R1_SPI_ERROR;
302 }
303 }
Thomas Choud52ebf12010-12-24 13:12:21 +0000304
Bhargav Shah05e35d42019-07-08 04:10:48 +0000305 if (r1 & R1_SPI_COM_CRC)
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900306 ret = -ECOMM;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000307 else if (r1) /* other errors */
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900308 ret = -ETIMEDOUT;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000309
Thomas Choud52ebf12010-12-24 13:12:21 +0000310 return ret;
311}
312
Bhargav Shah05e35d42019-07-08 04:10:48 +0000313static int dm_mmc_spi_set_ios(struct udevice *dev)
Thomas Choud52ebf12010-12-24 13:12:21 +0000314{
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900315 return 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000316}
317
Bhargav Shah05e35d42019-07-08 04:10:48 +0000318static int dm_mmc_spi_request(struct udevice *dev, struct mmc_cmd *cmd,
319 struct mmc_data *data)
Thomas Choud52ebf12010-12-24 13:12:21 +0000320{
Bhargav Shah05e35d42019-07-08 04:10:48 +0000321 int i, multi, ret = 0;
322 u8 *resp = NULL;
323 u32 resp_size = 0;
Pragnesh Pateled4a11c2020-06-29 15:17:29 +0530324 bool resp_match = false, r1b = false;
Pragnesh Patel70f176a2020-06-29 15:17:27 +0530325 u8 resp8 = 0, resp16[2] = { 0 }, resp40[5] = { 0 }, resp_match_value = 0;
Thomas Choud52ebf12010-12-24 13:12:21 +0000326
Bhargav Shah05e35d42019-07-08 04:10:48 +0000327 dm_spi_claim_bus(dev);
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200328
Bhargav Shah05e35d42019-07-08 04:10:48 +0000329 for (i = 0; i < 4; i++)
330 cmd->response[i] = 0;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200331
Bhargav Shah05e35d42019-07-08 04:10:48 +0000332 switch (cmd->cmdidx) {
333 case SD_CMD_APP_SEND_OP_COND:
334 case MMC_CMD_SEND_OP_COND:
335 resp = &resp8;
336 resp_size = sizeof(resp8);
337 cmd->cmdarg = 0x40000000;
338 break;
339 case SD_CMD_SEND_IF_COND:
340 resp = (u8 *)&resp40[0];
341 resp_size = sizeof(resp40);
342 resp_match = true;
343 resp_match_value = R1_SPI_IDLE;
344 break;
345 case MMC_CMD_SPI_READ_OCR:
346 resp = (u8 *)&resp40[0];
347 resp_size = sizeof(resp40);
348 break;
349 case MMC_CMD_SEND_STATUS:
Pragnesh Patel70f176a2020-06-29 15:17:27 +0530350 resp = (u8 *)&resp16[0];
351 resp_size = sizeof(resp16);
352 break;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000353 case MMC_CMD_SET_BLOCKLEN:
354 case MMC_CMD_SPI_CRC_ON_OFF:
Bhargav Shah05e35d42019-07-08 04:10:48 +0000355 resp = &resp8;
356 resp_size = sizeof(resp8);
357 resp_match = true;
358 resp_match_value = 0x0;
359 break;
Pragnesh Pateled4a11c2020-06-29 15:17:29 +0530360 case MMC_CMD_STOP_TRANSMISSION:
361 case MMC_CMD_ERASE:
362 resp = &resp8;
363 resp_size = sizeof(resp8);
364 r1b = true;
365 break;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000366 case MMC_CMD_SEND_CSD:
367 case MMC_CMD_SEND_CID:
368 case MMC_CMD_READ_SINGLE_BLOCK:
369 case MMC_CMD_READ_MULTIPLE_BLOCK:
370 case MMC_CMD_WRITE_SINGLE_BLOCK:
371 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
Pragnesh Patel810bc132020-06-29 15:17:26 +0530372 case MMC_CMD_APP_CMD:
Pragnesh Patel6f4555a2020-06-29 15:17:28 +0530373 case SD_CMD_ERASE_WR_BLK_START:
374 case SD_CMD_ERASE_WR_BLK_END:
Pragnesh Patela236d832020-06-29 15:17:25 +0530375 resp = &resp8;
376 resp_size = sizeof(resp8);
Bhargav Shah05e35d42019-07-08 04:10:48 +0000377 break;
378 default:
379 resp = &resp8;
380 resp_size = sizeof(resp8);
381 resp_match = true;
382 resp_match_value = R1_SPI_IDLE;
383 break;
384 };
Thomas Choud52ebf12010-12-24 13:12:21 +0000385
Bhargav Shah05e35d42019-07-08 04:10:48 +0000386 ret = mmc_spi_sendcmd(dev, cmd->cmdidx, cmd->cmdarg, cmd->resp_type,
Pragnesh Pateled4a11c2020-06-29 15:17:29 +0530387 resp, resp_size, resp_match, resp_match_value, r1b);
Bhargav Shah05e35d42019-07-08 04:10:48 +0000388 if (ret)
389 goto done;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200390
Bhargav Shah05e35d42019-07-08 04:10:48 +0000391 switch (cmd->cmdidx) {
392 case SD_CMD_APP_SEND_OP_COND:
393 case MMC_CMD_SEND_OP_COND:
394 cmd->response[0] = (resp8 & R1_SPI_IDLE) ? 0 : OCR_BUSY;
395 break;
396 case SD_CMD_SEND_IF_COND:
397 case MMC_CMD_SPI_READ_OCR:
398 cmd->response[0] = resp40[4];
399 cmd->response[0] |= (uint)resp40[3] << 8;
400 cmd->response[0] |= (uint)resp40[2] << 16;
401 cmd->response[0] |= (uint)resp40[1] << 24;
402 break;
403 case MMC_CMD_SEND_STATUS:
Pragnesh Patel70f176a2020-06-29 15:17:27 +0530404 if (resp16[0] || resp16[1])
405 cmd->response[0] = MMC_STATUS_ERROR;
406 else
407 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000408 break;
409 case MMC_CMD_SEND_CID:
410 case MMC_CMD_SEND_CSD:
411 ret = mmc_spi_readdata(dev, cmd->response, 1, 16);
412 if (ret)
413 return ret;
414 for (i = 0; i < 4; i++)
415 cmd->response[i] =
416 cpu_to_be32(cmd->response[i]);
417 break;
418 default:
419 cmd->response[0] = resp8;
420 break;
Thomas Choud52ebf12010-12-24 13:12:21 +0000421 }
Bhargav Shah05e35d42019-07-08 04:10:48 +0000422
423 debug("%s: cmd%d resp0=0x%x resp1=0x%x resp2=0x%x resp3=0x%x\n",
424 __func__, cmd->cmdidx, cmd->response[0], cmd->response[1],
425 cmd->response[2], cmd->response[3]);
426
427 if (data) {
428 debug("%s: data flags=0x%x blocks=%d block_size=%d\n",
429 __func__, data->flags, data->blocks, data->blocksize);
430 multi = (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK);
431 if (data->flags == MMC_DATA_READ)
432 ret = mmc_spi_readdata(dev, data->dest,
433 data->blocks, data->blocksize);
434 else if (data->flags == MMC_DATA_WRITE)
435 ret = mmc_spi_writedata(dev, data->src,
436 data->blocks, data->blocksize,
437 multi);
438 }
439
440done:
Anup Patela7060292019-07-17 04:23:38 +0000441 dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END);
442
Bhargav Shah05e35d42019-07-08 04:10:48 +0000443 dm_spi_release_bus(dev);
444
445 return ret;
Thomas Choud52ebf12010-12-24 13:12:21 +0000446}
Bhargav Shah05e35d42019-07-08 04:10:48 +0000447
448static int mmc_spi_probe(struct udevice *dev)
449{
450 struct mmc_spi_priv *priv = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700451 struct mmc_spi_plat *plat = dev_get_plat(dev);
Bhargav Shah05e35d42019-07-08 04:10:48 +0000452 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
453 char *name;
454
455 priv->spi = dev_get_parent_priv(dev);
456 if (!priv->spi->max_hz)
457 priv->spi->max_hz = MMC_SPI_MAX_CLOCK;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000458 priv->spi->mode = SPI_MODE_0;
459 priv->spi->wordlen = 8;
460
461 name = malloc(strlen(dev->parent->name) + strlen(dev->name) + 4);
462 if (!name)
463 return -ENOMEM;
464 sprintf(name, "%s:%s", dev->parent->name, dev->name);
465
Bin Mengd3302392019-08-30 21:15:33 -0700466 plat->cfg.name = name;
467 plat->cfg.host_caps = MMC_MODE_SPI;
468 plat->cfg.voltages = MMC_SPI_VOLTAGE;
469 plat->cfg.f_min = MMC_SPI_MIN_CLOCK;
470 plat->cfg.f_max = priv->spi->max_hz;
471 plat->cfg.part_type = PART_TYPE_DOS;
472 plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000473
Bin Mengd3302392019-08-30 21:15:33 -0700474 plat->mmc.cfg = &plat->cfg;
475 plat->mmc.priv = priv;
476 plat->mmc.dev = dev;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000477
Bin Mengd3302392019-08-30 21:15:33 -0700478 upriv->mmc = &plat->mmc;
Bhargav Shah05e35d42019-07-08 04:10:48 +0000479
480 return 0;
481}
482
483static int mmc_spi_bind(struct udevice *dev)
484{
Simon Glassc69cda22020-12-03 16:55:20 -0700485 struct mmc_spi_plat *plat = dev_get_plat(dev);
Bhargav Shah05e35d42019-07-08 04:10:48 +0000486
Bin Mengd3302392019-08-30 21:15:33 -0700487 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Bhargav Shah05e35d42019-07-08 04:10:48 +0000488}
489
490static const struct dm_mmc_ops mmc_spi_ops = {
491 .send_cmd = dm_mmc_spi_request,
492 .set_ios = dm_mmc_spi_set_ios,
493};
494
495static const struct udevice_id dm_mmc_spi_match[] = {
496 { .compatible = "mmc-spi-slot" },
497 { /* sentinel */ }
498};
499
500U_BOOT_DRIVER(mmc_spi) = {
501 .name = "mmc_spi",
502 .id = UCLASS_MMC,
503 .of_match = dm_mmc_spi_match,
504 .ops = &mmc_spi_ops,
505 .probe = mmc_spi_probe,
506 .bind = mmc_spi_bind,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700507 .plat_auto = sizeof(struct mmc_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700508 .priv_auto = sizeof(struct mmc_spi_priv),
Bhargav Shah05e35d42019-07-08 04:10:48 +0000509};