Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * generic mmc spi driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 5 | * Copyright 2019 Bhargav Shah <bhargavshah1988@gmail.com> |
| 6 | * |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 7 | * Licensed under the GPL-2 or later. |
| 8 | */ |
| 9 | #include <common.h> |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <part.h> |
| 14 | #include <mmc.h> |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Philipp Tomsich | 36b26d1 | 2018-11-25 19:22:18 +0100 | [diff] [blame] | 17 | #include <u-boot/crc.h> |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 18 | #include <linux/crc7.h> |
Yoshinori Sato | 923c207 | 2015-06-01 15:22:37 +0900 | [diff] [blame] | 19 | #include <asm/byteorder.h> |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 20 | #include <dm.h> |
| 21 | #include <spi.h> |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 22 | |
| 23 | /* MMC/SD in SPI mode reports R1 status always */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 24 | #define R1_SPI_IDLE BIT(0) |
| 25 | #define R1_SPI_ERASE_RESET BIT(1) |
| 26 | #define R1_SPI_ILLEGAL_COMMAND BIT(2) |
| 27 | #define R1_SPI_COM_CRC BIT(3) |
| 28 | #define R1_SPI_ERASE_SEQ BIT(4) |
| 29 | #define R1_SPI_ADDRESS BIT(5) |
| 30 | #define R1_SPI_PARAMETER BIT(6) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 31 | /* R1 bit 7 is always zero, reuse this bit for error */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 32 | #define R1_SPI_ERROR BIT(7) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 33 | |
| 34 | /* Response tokens used to ack each block written: */ |
| 35 | #define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f) |
| 36 | #define SPI_RESPONSE_ACCEPTED ((2 << 1)|1) |
| 37 | #define SPI_RESPONSE_CRC_ERR ((5 << 1)|1) |
| 38 | #define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1) |
| 39 | |
| 40 | /* Read and write blocks start with these tokens and end with crc; |
| 41 | * on error, read tokens act like a subset of R2_SPI_* values. |
| 42 | */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 43 | /* 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 Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 49 | |
| 50 | /* MMC SPI commands start with a start bit "0" and a transmit bit "1" */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 51 | #define MMC_SPI_CMD(x) (0x40 | (x)) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 52 | |
| 53 | /* bus capability */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 54 | #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 Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 57 | |
| 58 | /* timeout value */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 59 | #define CMD_TIMEOUT 8 |
| 60 | #define READ_TIMEOUT 3000000 /* 1 sec */ |
| 61 | #define WRITE_TIMEOUT 3000000 /* 1 sec */ |
Pragnesh Patel | 0f26cf1 | 2020-06-29 15:17:29 +0530 | [diff] [blame] | 62 | #define R1B_TIMEOUT 3000000 /* 1 sec */ |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 63 | |
Bin Meng | f726032 | 2019-08-30 21:15:33 -0700 | [diff] [blame] | 64 | struct mmc_spi_plat { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 65 | struct mmc_config cfg; |
| 66 | struct mmc mmc; |
| 67 | }; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 68 | |
Bin Meng | f726032 | 2019-08-30 21:15:33 -0700 | [diff] [blame] | 69 | struct mmc_spi_priv { |
| 70 | struct spi_slave *spi; |
| 71 | }; |
| 72 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 73 | static int mmc_spi_sendcmd(struct udevice *dev, |
| 74 | ushort cmdidx, u32 cmdarg, u32 resp_type, |
| 75 | u8 *resp, u32 resp_size, |
Pragnesh Patel | 0f26cf1 | 2020-06-29 15:17:29 +0530 | [diff] [blame] | 76 | bool resp_match, u8 resp_match_value, bool r1b) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 77 | { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 78 | int i, rpos = 0, ret = 0; |
| 79 | u8 cmdo[7], r; |
| 80 | |
| 81 | debug("%s: cmd%d cmdarg=0x%x resp_type=0x%x " |
| 82 | "resp_size=%d resp_match=%d resp_match_value=0x%x\n", |
| 83 | __func__, cmdidx, cmdarg, resp_type, |
| 84 | resp_size, resp_match, resp_match_value); |
| 85 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 86 | cmdo[0] = 0xff; |
| 87 | cmdo[1] = MMC_SPI_CMD(cmdidx); |
| 88 | cmdo[2] = cmdarg >> 24; |
| 89 | cmdo[3] = cmdarg >> 16; |
| 90 | cmdo[4] = cmdarg >> 8; |
| 91 | cmdo[5] = cmdarg; |
| 92 | cmdo[6] = (crc7(0, &cmdo[1], 5) << 1) | 0x01; |
Anup Patel | d2c68c0 | 2019-07-17 04:23:38 +0000 | [diff] [blame] | 93 | ret = dm_spi_xfer(dev, sizeof(cmdo) * 8, cmdo, NULL, SPI_XFER_BEGIN); |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 94 | if (ret) |
| 95 | return ret; |
| 96 | |
| 97 | ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | if (!resp || !resp_size) |
| 102 | return 0; |
| 103 | |
| 104 | debug("%s: cmd%d", __func__, cmdidx); |
| 105 | |
| 106 | if (resp_match) { |
| 107 | r = ~resp_match_value; |
| 108 | i = CMD_TIMEOUT; |
Pragnesh Patel | 32ca52e | 2020-06-29 15:17:24 +0530 | [diff] [blame] | 109 | while (i) { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 110 | ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0); |
| 111 | if (ret) |
| 112 | return ret; |
| 113 | debug(" resp%d=0x%x", rpos, r); |
| 114 | rpos++; |
Pragnesh Patel | 32ca52e | 2020-06-29 15:17:24 +0530 | [diff] [blame] | 115 | i--; |
| 116 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 117 | if (r == resp_match_value) |
| 118 | break; |
| 119 | } |
| 120 | if (!i && (r != resp_match_value)) |
| 121 | return -ETIMEDOUT; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 122 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 123 | |
| 124 | for (i = 0; i < resp_size; i++) { |
| 125 | if (i == 0 && resp_match) { |
| 126 | resp[i] = resp_match_value; |
| 127 | continue; |
| 128 | } |
| 129 | ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | debug(" resp%d=0x%x", rpos, r); |
| 133 | rpos++; |
| 134 | resp[i] = r; |
| 135 | } |
| 136 | |
Pragnesh Patel | 0f26cf1 | 2020-06-29 15:17:29 +0530 | [diff] [blame] | 137 | if (r1b == true) { |
| 138 | i = R1B_TIMEOUT; |
| 139 | while (i) { |
| 140 | ret = dm_spi_xfer(dev, 1 * 8, NULL, &r, 0); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | debug(" resp%d=0x%x", rpos, r); |
| 145 | rpos++; |
| 146 | i--; |
| 147 | |
| 148 | if (r) |
| 149 | break; |
| 150 | } |
| 151 | if (!i) |
| 152 | return -ETIMEDOUT; |
| 153 | } |
| 154 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 155 | debug("\n"); |
| 156 | |
| 157 | return 0; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 160 | static int mmc_spi_readdata(struct udevice *dev, |
| 161 | void *xbuf, u32 bcnt, u32 bsize) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 162 | { |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 163 | u16 crc; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 164 | u8 *buf = xbuf, r1; |
| 165 | int i, ret = 0; |
| 166 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 167 | while (bcnt--) { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 168 | for (i = 0; i < READ_TIMEOUT; i++) { |
| 169 | ret = dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | if (r1 == SPI_TOKEN_SINGLE) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 173 | break; |
| 174 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 175 | debug("%s: data tok%d 0x%x\n", __func__, i, r1); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 176 | if (r1 == SPI_TOKEN_SINGLE) { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 177 | ret = dm_spi_xfer(dev, bsize * 8, NULL, buf, 0); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | ret = dm_spi_xfer(dev, 2 * 8, NULL, &crc, 0); |
| 181 | if (ret) |
| 182 | return ret; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 183 | #ifdef CONFIG_MMC_SPI_CRC_ON |
Bin Meng | ce387d9 | 2021-02-02 10:32:48 +0800 | [diff] [blame^] | 184 | u16 crc_ok = be16_to_cpu(crc16_ccitt(0, buf, bsize)); |
| 185 | if (crc_ok != crc) { |
| 186 | debug("%s: data crc error, expected %04x got %04x\n", |
| 187 | __func__, crc_ok, crc); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 188 | r1 = R1_SPI_COM_CRC; |
| 189 | break; |
| 190 | } |
| 191 | #endif |
| 192 | r1 = 0; |
| 193 | } else { |
| 194 | r1 = R1_SPI_ERROR; |
| 195 | break; |
| 196 | } |
| 197 | buf += bsize; |
| 198 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 199 | |
| 200 | if (r1 & R1_SPI_COM_CRC) |
| 201 | ret = -ECOMM; |
| 202 | else if (r1) /* other errors */ |
| 203 | ret = -ETIMEDOUT; |
| 204 | |
| 205 | return ret; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 208 | static int mmc_spi_writedata(struct udevice *dev, const void *xbuf, |
| 209 | u32 bcnt, u32 bsize, int multi) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 210 | { |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 211 | const u8 *buf = xbuf; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 212 | u8 r1, tok[2]; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 213 | u16 crc; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 214 | int i, ret = 0; |
| 215 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 216 | tok[0] = 0xff; |
| 217 | tok[1] = multi ? SPI_TOKEN_MULTI_WRITE : SPI_TOKEN_SINGLE; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 218 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 219 | while (bcnt--) { |
| 220 | #ifdef CONFIG_MMC_SPI_CRC_ON |
Stefan Roese | 084ff1e | 2016-03-03 09:34:12 +0100 | [diff] [blame] | 221 | crc = cpu_to_be16(crc16_ccitt(0, (u8 *)buf, bsize)); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 222 | #endif |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 223 | dm_spi_xfer(dev, 2 * 8, tok, NULL, 0); |
| 224 | dm_spi_xfer(dev, bsize * 8, buf, NULL, 0); |
| 225 | dm_spi_xfer(dev, 2 * 8, &crc, NULL, 0); |
| 226 | for (i = 0; i < CMD_TIMEOUT; i++) { |
| 227 | dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 228 | if ((r1 & 0x10) == 0) /* response token */ |
| 229 | break; |
| 230 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 231 | debug("%s: data tok%d 0x%x\n", __func__, i, r1); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 232 | if (SPI_MMC_RESPONSE_CODE(r1) == SPI_RESPONSE_ACCEPTED) { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 233 | debug("%s: data accepted\n", __func__); |
| 234 | for (i = 0; i < WRITE_TIMEOUT; i++) { /* wait busy */ |
| 235 | dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 236 | if (i && r1 == 0xff) { |
| 237 | r1 = 0; |
| 238 | break; |
| 239 | } |
| 240 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 241 | if (i == WRITE_TIMEOUT) { |
| 242 | debug("%s: data write timeout 0x%x\n", |
| 243 | __func__, r1); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 244 | r1 = R1_SPI_ERROR; |
| 245 | break; |
| 246 | } |
| 247 | } else { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 248 | debug("%s: data error 0x%x\n", __func__, r1); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 249 | r1 = R1_SPI_COM_CRC; |
| 250 | break; |
| 251 | } |
| 252 | buf += bsize; |
| 253 | } |
| 254 | if (multi && bcnt == -1) { /* stop multi write */ |
| 255 | tok[1] = SPI_TOKEN_STOP_TRAN; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 256 | dm_spi_xfer(dev, 2 * 8, tok, NULL, 0); |
| 257 | for (i = 0; i < WRITE_TIMEOUT; i++) { /* wait busy */ |
| 258 | dm_spi_xfer(dev, 1 * 8, NULL, &r1, 0); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 259 | if (i && r1 == 0xff) { |
| 260 | r1 = 0; |
| 261 | break; |
| 262 | } |
| 263 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 264 | if (i == WRITE_TIMEOUT) { |
| 265 | debug("%s: data write timeout 0x%x\n", __func__, r1); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 266 | r1 = R1_SPI_ERROR; |
| 267 | } |
| 268 | } |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 269 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 270 | if (r1 & R1_SPI_COM_CRC) |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 271 | ret = -ECOMM; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 272 | else if (r1) /* other errors */ |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 273 | ret = -ETIMEDOUT; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int dm_mmc_spi_set_ios(struct udevice *dev) |
| 279 | { |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int dm_mmc_spi_request(struct udevice *dev, struct mmc_cmd *cmd, |
| 284 | struct mmc_data *data) |
| 285 | { |
| 286 | int i, multi, ret = 0; |
| 287 | u8 *resp = NULL; |
| 288 | u32 resp_size = 0; |
Pragnesh Patel | 0f26cf1 | 2020-06-29 15:17:29 +0530 | [diff] [blame] | 289 | bool resp_match = false, r1b = false; |
Pragnesh Patel | 68fbc9d | 2020-06-29 15:17:27 +0530 | [diff] [blame] | 290 | u8 resp8 = 0, resp16[2] = { 0 }, resp40[5] = { 0 }, resp_match_value = 0; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 291 | |
| 292 | dm_spi_claim_bus(dev); |
| 293 | |
| 294 | for (i = 0; i < 4; i++) |
| 295 | cmd->response[i] = 0; |
| 296 | |
| 297 | switch (cmd->cmdidx) { |
| 298 | case SD_CMD_APP_SEND_OP_COND: |
| 299 | case MMC_CMD_SEND_OP_COND: |
| 300 | resp = &resp8; |
| 301 | resp_size = sizeof(resp8); |
| 302 | cmd->cmdarg = 0x40000000; |
| 303 | break; |
| 304 | case SD_CMD_SEND_IF_COND: |
| 305 | resp = (u8 *)&resp40[0]; |
| 306 | resp_size = sizeof(resp40); |
| 307 | resp_match = true; |
| 308 | resp_match_value = R1_SPI_IDLE; |
| 309 | break; |
| 310 | case MMC_CMD_SPI_READ_OCR: |
| 311 | resp = (u8 *)&resp40[0]; |
| 312 | resp_size = sizeof(resp40); |
| 313 | break; |
| 314 | case MMC_CMD_SEND_STATUS: |
Pragnesh Patel | 68fbc9d | 2020-06-29 15:17:27 +0530 | [diff] [blame] | 315 | resp = (u8 *)&resp16[0]; |
| 316 | resp_size = sizeof(resp16); |
| 317 | break; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 318 | case MMC_CMD_SET_BLOCKLEN: |
| 319 | case MMC_CMD_SPI_CRC_ON_OFF: |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 320 | resp = &resp8; |
| 321 | resp_size = sizeof(resp8); |
| 322 | resp_match = true; |
| 323 | resp_match_value = 0x0; |
| 324 | break; |
Pragnesh Patel | 0f26cf1 | 2020-06-29 15:17:29 +0530 | [diff] [blame] | 325 | case MMC_CMD_STOP_TRANSMISSION: |
| 326 | case MMC_CMD_ERASE: |
| 327 | resp = &resp8; |
| 328 | resp_size = sizeof(resp8); |
| 329 | r1b = true; |
| 330 | break; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 331 | case MMC_CMD_SEND_CSD: |
| 332 | case MMC_CMD_SEND_CID: |
| 333 | case MMC_CMD_READ_SINGLE_BLOCK: |
| 334 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
| 335 | case MMC_CMD_WRITE_SINGLE_BLOCK: |
| 336 | case MMC_CMD_WRITE_MULTIPLE_BLOCK: |
Pragnesh Patel | a01f57e | 2020-06-29 15:17:26 +0530 | [diff] [blame] | 337 | case MMC_CMD_APP_CMD: |
Pragnesh Patel | 536b456 | 2020-06-29 15:17:28 +0530 | [diff] [blame] | 338 | case SD_CMD_ERASE_WR_BLK_START: |
| 339 | case SD_CMD_ERASE_WR_BLK_END: |
Pragnesh Patel | 049dc5f | 2020-06-29 15:17:25 +0530 | [diff] [blame] | 340 | resp = &resp8; |
| 341 | resp_size = sizeof(resp8); |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 342 | break; |
| 343 | default: |
| 344 | resp = &resp8; |
| 345 | resp_size = sizeof(resp8); |
| 346 | resp_match = true; |
| 347 | resp_match_value = R1_SPI_IDLE; |
| 348 | break; |
| 349 | }; |
| 350 | |
| 351 | ret = mmc_spi_sendcmd(dev, cmd->cmdidx, cmd->cmdarg, cmd->resp_type, |
Pragnesh Patel | 0f26cf1 | 2020-06-29 15:17:29 +0530 | [diff] [blame] | 352 | resp, resp_size, resp_match, resp_match_value, r1b); |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 353 | if (ret) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 354 | goto done; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 355 | |
| 356 | switch (cmd->cmdidx) { |
| 357 | case SD_CMD_APP_SEND_OP_COND: |
| 358 | case MMC_CMD_SEND_OP_COND: |
| 359 | cmd->response[0] = (resp8 & R1_SPI_IDLE) ? 0 : OCR_BUSY; |
| 360 | break; |
| 361 | case SD_CMD_SEND_IF_COND: |
| 362 | case MMC_CMD_SPI_READ_OCR: |
| 363 | cmd->response[0] = resp40[4]; |
| 364 | cmd->response[0] |= (uint)resp40[3] << 8; |
| 365 | cmd->response[0] |= (uint)resp40[2] << 16; |
| 366 | cmd->response[0] |= (uint)resp40[1] << 24; |
| 367 | break; |
| 368 | case MMC_CMD_SEND_STATUS: |
Pragnesh Patel | 68fbc9d | 2020-06-29 15:17:27 +0530 | [diff] [blame] | 369 | if (resp16[0] || resp16[1]) |
| 370 | cmd->response[0] = MMC_STATUS_ERROR; |
| 371 | else |
| 372 | cmd->response[0] = MMC_STATUS_RDY_FOR_DATA; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 373 | break; |
| 374 | case MMC_CMD_SEND_CID: |
| 375 | case MMC_CMD_SEND_CSD: |
| 376 | ret = mmc_spi_readdata(dev, cmd->response, 1, 16); |
| 377 | if (ret) |
| 378 | return ret; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 379 | for (i = 0; i < 4; i++) |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 380 | cmd->response[i] = |
| 381 | cpu_to_be32(cmd->response[i]); |
| 382 | break; |
| 383 | default: |
| 384 | cmd->response[0] = resp8; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | debug("%s: cmd%d resp0=0x%x resp1=0x%x resp2=0x%x resp3=0x%x\n", |
| 389 | __func__, cmd->cmdidx, cmd->response[0], cmd->response[1], |
| 390 | cmd->response[2], cmd->response[3]); |
| 391 | |
| 392 | if (data) { |
| 393 | debug("%s: data flags=0x%x blocks=%d block_size=%d\n", |
| 394 | __func__, data->flags, data->blocks, data->blocksize); |
| 395 | multi = (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 396 | if (data->flags == MMC_DATA_READ) |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 397 | ret = mmc_spi_readdata(dev, data->dest, |
| 398 | data->blocks, data->blocksize); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 399 | else if (data->flags == MMC_DATA_WRITE) |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 400 | ret = mmc_spi_writedata(dev, data->src, |
| 401 | data->blocks, data->blocksize, |
| 402 | multi); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 403 | } |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 404 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 405 | done: |
Anup Patel | d2c68c0 | 2019-07-17 04:23:38 +0000 | [diff] [blame] | 406 | dm_spi_xfer(dev, 0, NULL, NULL, SPI_XFER_END); |
| 407 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 408 | dm_spi_release_bus(dev); |
| 409 | |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 410 | return ret; |
| 411 | } |
| 412 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 413 | static int mmc_spi_probe(struct udevice *dev) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 414 | { |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 415 | struct mmc_spi_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 416 | struct mmc_spi_plat *plat = dev_get_plat(dev); |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 417 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 418 | char *name; |
| 419 | |
| 420 | priv->spi = dev_get_parent_priv(dev); |
| 421 | if (!priv->spi->max_hz) |
| 422 | priv->spi->max_hz = MMC_SPI_MAX_CLOCK; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 423 | priv->spi->mode = SPI_MODE_0; |
| 424 | priv->spi->wordlen = 8; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 425 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 426 | name = malloc(strlen(dev->parent->name) + strlen(dev->name) + 4); |
| 427 | if (!name) |
| 428 | return -ENOMEM; |
| 429 | sprintf(name, "%s:%s", dev->parent->name, dev->name); |
| 430 | |
Bin Meng | f726032 | 2019-08-30 21:15:33 -0700 | [diff] [blame] | 431 | plat->cfg.name = name; |
| 432 | plat->cfg.host_caps = MMC_MODE_SPI; |
| 433 | plat->cfg.voltages = MMC_SPI_VOLTAGE; |
| 434 | plat->cfg.f_min = MMC_SPI_MIN_CLOCK; |
| 435 | plat->cfg.f_max = priv->spi->max_hz; |
| 436 | plat->cfg.part_type = PART_TYPE_DOS; |
| 437 | plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 438 | |
Bin Meng | f726032 | 2019-08-30 21:15:33 -0700 | [diff] [blame] | 439 | plat->mmc.cfg = &plat->cfg; |
| 440 | plat->mmc.priv = priv; |
| 441 | plat->mmc.dev = dev; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 442 | |
Bin Meng | f726032 | 2019-08-30 21:15:33 -0700 | [diff] [blame] | 443 | upriv->mmc = &plat->mmc; |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 444 | |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 445 | return 0; |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 448 | static int mmc_spi_bind(struct udevice *dev) |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 449 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 450 | struct mmc_spi_plat *plat = dev_get_plat(dev); |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 451 | |
Bin Meng | f726032 | 2019-08-30 21:15:33 -0700 | [diff] [blame] | 452 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
Thomas Chou | 1254c3d | 2010-12-24 13:12:21 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 455 | static const struct dm_mmc_ops mmc_spi_ops = { |
| 456 | .send_cmd = dm_mmc_spi_request, |
| 457 | .set_ios = dm_mmc_spi_set_ios, |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 458 | }; |
| 459 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 460 | static const struct udevice_id dm_mmc_spi_match[] = { |
| 461 | { .compatible = "mmc-spi-slot" }, |
| 462 | { /* sentinel */ } |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 463 | }; |
| 464 | |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 465 | U_BOOT_DRIVER(mmc_spi) = { |
| 466 | .name = "mmc_spi", |
| 467 | .id = UCLASS_MMC, |
| 468 | .of_match = dm_mmc_spi_match, |
| 469 | .ops = &mmc_spi_ops, |
| 470 | .probe = mmc_spi_probe, |
| 471 | .bind = mmc_spi_bind, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 472 | .plat_auto = sizeof(struct mmc_spi_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 473 | .priv_auto = sizeof(struct mmc_spi_priv), |
Bhargav Shah | a1afe25 | 2019-07-08 04:10:48 +0000 | [diff] [blame] | 474 | }; |