27 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
43 #include <asm/unaligned.h>
79 #define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
80 #define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
81 #define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
82 #define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
87 #define SPI_TOKEN_SINGLE 0xfe
88 #define SPI_TOKEN_MULTI_WRITE 0xfc
89 #define SPI_TOKEN_STOP_TRAN 0xfd
91 #define MMC_SPI_BLOCKSIZE 512
100 #define r1b_timeout (HZ * 3)
109 #define MMC_SPI_BLOCKSATONCE 128
174 if (len >
sizeof(*host->
data)) {
196 static int mmc_spi_skip(
struct mmc_spi_host *host,
unsigned long timeout,
206 status = mmc_spi_readbytes(host, n);
210 for (i = 0; i <
n; i++) {
229 mmc_spi_wait_unbusy(
struct mmc_spi_host *host,
unsigned long timeout)
231 return mmc_spi_skip(host, timeout,
sizeof(host->
data->status), 0);
234 static int mmc_spi_readtoken(
struct mmc_spi_host *host,
unsigned long timeout)
236 return mmc_spi_skip(host, timeout, 1, 0xff);
261 static int mmc_spi_response_get(
struct mmc_spi_host *host,
264 u8 *cp = host->
data->status;
265 u8 *
end = cp + host->t.len;
269 unsigned short rotator;
273 snprintf(tag,
sizeof(tag),
" ... CMD%d response SPI_%s",
274 cmd->
opcode, maptype(cmd));
283 while (cp < end && *cp == 0xff)
288 cp = host->
data->status;
302 for (i = 2; i < 16; i++) {
303 value = mmc_spi_readbytes(host, 1);
317 rotator = *cp++ << 8;
320 value = mmc_spi_readbytes(host, 1);
323 cp = host->
data->status;
327 while (rotator & 0x8000) {
331 cmd->
resp[0] = rotator >> 8;
334 cmd->
resp[0] = *cp++;
339 if (cmd->
resp[0] != 0) {
360 while (cp < end && *cp == 0)
372 value = mmc_spi_readbytes(host, 1);
375 cp = host->
data->status;
379 rotator = leftover << 8;
380 rotator |= *cp << bitshift;
381 cmd->
resp[0] |= (rotator & 0xFF00);
383 cmd->
resp[0] |= *cp << 8;
389 rotator = leftover << 8;
391 for (i = 0; i < 4; i++) {
395 value = mmc_spi_readbytes(host, 1);
398 cp = host->
data->status;
402 rotator |= *cp++ << bitshift;
403 cmd->
resp[1] |= (rotator >> 8);
406 cmd->
resp[1] |= *cp++;
416 dev_dbg(&host->
spi->dev,
"bad response type %04x\n",
424 dev_dbg(&host->
spi->dev,
"%s: resp %04x %08x\n",
428 if (value >= 0 && cs_on)
469 *cp++ = 0x40 | cmd->
opcode;
470 *cp++ = (
u8)(arg >> 24);
471 *cp++ = (
u8)(arg >> 16);
472 *cp++ = (
u8)(arg >> 8);
474 *cp++ = (
crc7(0, &data->
status[1], 5) << 1) | 0x01;
525 dev_dbg(&host->
spi->dev,
" mmc_spi: CMD%d, resp %s\n",
526 cmd->
opcode, maptype(cmd));
529 spi_message_init(&host->
m);
537 spi_message_add_tail(t, &host->
m);
540 host->
m.is_dma_mapped = 1;
552 dev_dbg(&host->
spi->dev,
" ... write returned %d\n", status);
558 return mmc_spi_response_get(host, cmd, cs_on);
573 mmc_spi_setup_data_message(
582 spi_message_init(&host->
m);
584 host->
m.is_dma_mapped = 1;
600 spi_message_add_tail(t, &host->
m);
611 spi_message_add_tail(t, &host->
m);
628 spi_message_add_tail(t, &host->
m);
656 spi_message_add_tail(t, &host->
m);
674 unsigned long timeout)
678 struct scratch *scratch = host->
data;
681 if (host->
mmc->use_spi_crc)
692 dev_dbg(&spi->
dev,
"write error (%d)\n", status);
713 pattern = scratch->
status[0] << 24;
714 pattern |= scratch->
status[1] << 16;
715 pattern |= scratch->
status[2] << 8;
716 pattern |= scratch->
status[3];
719 pattern |= 0xE0000000;
722 while (pattern & 0x80000000)
747 scratch->
status[0], status);
758 for (i = 4; i <
sizeof(scratch->
status); i++) {
760 if (scratch->
status[i] & 0x01)
763 return mmc_spi_wait_unbusy(host, timeout);
784 unsigned long timeout)
788 struct scratch *scratch = host->
data;
789 unsigned int bitshift;
795 status = mmc_spi_readbytes(host, 1);
798 status = scratch->
status[0];
799 if (status == 0xff || status == 0)
800 status = mmc_spi_readtoken(host, timeout);
803 dev_dbg(&spi->
dev,
"read error %02x (%d)\n", status, status);
811 while (status & 0x80) {
815 leftover = status << 1;
843 unsigned int bitright = 8 - bitshift;
845 for (len = t->
len; len; len--) {
847 *cp++ = leftover | (temp >> bitshift);
848 leftover = temp << bitright;
852 *cp++ = leftover | (temp >> bitshift);
853 leftover = temp << bitright;
855 *cp = leftover | (temp >> bitshift);
858 if (host->
mmc->use_spi_crc) {
863 dev_dbg(&spi->
dev,
"read - crc error: crc_val=0x%04x, "
864 "computed=0x%04x len=%d\n",
892 int multiple = (data->
blocks > 1);
894 unsigned long timeout;
900 mmc_spi_setup_data_message(host, multiple, direction);
915 for (sg = data->
sg, n_sg = data->
sg_len; n_sg; n_sg--, sg++) {
940 kmap_addr =
kmap(sg_page(sg));
948 t->
len =
min(length, blk_size);
951 " mmc_spi: %s block, %d bytes\n",
958 status = mmc_spi_writeblock(host, t, timeout);
960 status = mmc_spi_readblock(host, t, timeout);
973 flush_kernel_dcache_page(sg_page(sg));
995 struct scratch *scratch = host->
data;
997 const unsigned statlen =
sizeof(scratch->
status);
1006 INIT_LIST_HEAD(&host->
m.transfers);
1008 &host->
m.transfers);
1039 for (tmp = 2; tmp < statlen; tmp++) {
1040 if (scratch->
status[tmp] != 0)
1043 tmp = mmc_spi_wait_unbusy(host, timeout);
1044 if (tmp < 0 && !data->
error)
1077 dev_dbg(&host->
spi->dev,
"bogus STOP command\n");
1095 status = mmc_spi_command_send(host, mrq, mrq->
cmd, mrq->
data !=
NULL);
1096 if (status == 0 && mrq->
data) {
1097 mmc_spi_data_do(host, mrq->
cmd, mrq->
data, mrq->
data->blksz);
1106 if (mrq->
data->error == -
EILSEQ && crc_retry) {
1110 status = mmc_spi_command_send(host, mrq, &
stop, 0);
1112 mrq->
data->error = 0;
1117 status = mmc_spi_command_send(host, mrq, mrq->
stop, 0);
1136 static void mmc_spi_initsequence(
struct mmc_spi_host *host)
1142 mmc_spi_readbytes(host, 10);
1162 "can't change chip-select polarity\n");
1165 mmc_spi_readbytes(host, 18);
1171 "can't restore chip-select polarity\n");
1176 static char *mmc_powerstring(
u8 power_mode)
1178 switch (power_mode) {
1186 static void mmc_spi_set_ios(
struct mmc_host *mmc,
struct mmc_ios *ios)
1193 canpower = host->
pdata && host->
pdata->setpower;
1195 dev_dbg(&host->
spi->dev,
"mmc_spi: power %s (%d)%s\n",
1198 canpower ?
", can switch" :
"");
1207 host->
pdata->setpower(&host->
spi->dev,
1216 mmc_spi_initsequence(host);
1235 "switch to SPI mode 0 failed\n");
1237 if (spi_write(host->
spi, &nullbyte, 1) < 0)
1239 "put spi signals to low failed\n");
1256 "switch back to SPI mode 3"
1264 if (host->
spi->max_speed_hz != ios->
clock && ios->
clock != 0) {
1267 host->
spi->max_speed_hz = ios->
clock;
1270 "mmc_spi: clock to %d Hz, %d\n",
1271 host->
spi->max_speed_hz, status);
1275 static int mmc_spi_get_ro(
struct mmc_host *mmc)
1288 static int mmc_spi_get_cd(
struct mmc_host *mmc)
1298 .request = mmc_spi_request,
1299 .set_ios = mmc_spi_set_ios,
1300 .get_ro = mmc_spi_get_ro,
1301 .get_cd = mmc_spi_get_cd,
1312 mmc_spi_detect_irq(
int irq,
void *mmc)
1321 static int mmc_spi_probe(
struct spi_device *spi)
1346 dev_dbg(&spi->
dev,
"needs SPI mode %02x, %d KHz; %d\n",
1368 mmc->
ops = &mmc_spi_ops;
1384 mmc->
f_min = 400000;
1387 host = mmc_priv(mmc);
1400 dev_warn(&spi->
dev,
"ASSUMING 3.2-3.4 V slot power\n");
1416 if (spi->
master->dev.parent->dma_mask) {
1441 host->
status.cs_change = 1;
1445 status = host->
pdata->init(&spi->
dev, mmc_spi_detect_irq, mmc);
1447 goto fail_glue_init;
1460 host->
dma_dev ?
"" :
", no DMA",
1464 ?
"" :
", no poweroff",
1466 ?
", cd polling" :
"");
1494 host = mmc_priv(mmc);
1521 { .compatible =
"mmc-spi-slot", },
1529 .of_match_table = mmc_spi_of_match_table,
1531 .probe = mmc_spi_probe,
1538 "Hans-Peter Nilsson, Jan Nikitenko");