/*1* mmc_spi.c - Access SD/MMC cards through SPI master controllers2*3* (C) Copyright 2005, Intec Automation,4* Mike Lavender (mike@steroidmicros)5* (C) Copyright 2006-2007, David Brownell6* (C) Copyright 2007, Axis Communications,7* Hans-Peter Nilsson ([email protected])8* (C) Copyright 2007, ATRON electronic GmbH,9* Jan Nikitenko <[email protected]>10*11*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.25*/26#include <linux/sched.h>27#include <linux/delay.h>28#include <linux/slab.h>29#include <linux/bio.h>30#include <linux/dma-mapping.h>31#include <linux/crc7.h>32#include <linux/crc-itu-t.h>33#include <linux/scatterlist.h>3435#include <linux/mmc/host.h>36#include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */3738#include <linux/spi/spi.h>39#include <linux/spi/mmc_spi.h>4041#include <asm/unaligned.h>424344/* NOTES:45*46* - For now, we won't try to interoperate with a real mmc/sd/sdio47* controller, although some of them do have hardware support for48* SPI protocol. The main reason for such configs would be mmc-ish49* cards like DataFlash, which don't support that "native" protocol.50*51* We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to52* switch between driver stacks, and in any case if "native" mode53* is available, it will be faster and hence preferable.54*55* - MMC depends on a different chipselect management policy than the56* SPI interface currently supports for shared bus segments: it needs57* to issue multiple spi_message requests with the chipselect active,58* using the results of one message to decide the next one to issue.59*60* Pending updates to the programming interface, this driver expects61* that it not share the bus with other drivers (precluding conflicts).62*63* - We tell the controller to keep the chipselect active from the64* beginning of an mmc_host_ops.request until the end. So beware65* of SPI controller drivers that mis-handle the cs_change flag!66*67* However, many cards seem OK with chipselect flapping up/down68* during that time ... at least on unshared bus segments.69*/707172/*73* Local protocol constants, internal to data block protocols.74*/7576/* Response tokens used to ack each block written: */77#define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)78#define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)79#define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)80#define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)8182/* Read and write blocks start with these tokens and end with crc;83* on error, read tokens act like a subset of R2_SPI_* values.84*/85#define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */86#define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */87#define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */8889#define MMC_SPI_BLOCKSIZE 512909192/* These fixed timeouts come from the latest SD specs, which say to ignore93* the CSD values. The R1B value is for card erase (e.g. the "I forgot the94* card's password" scenario); it's mostly applied to STOP_TRANSMISSION after95* reads which takes nowhere near that long. Older cards may be able to use96* shorter timeouts ... but why bother?97*/98#define r1b_timeout (HZ * 3)99100/* One of the critical speed parameters is the amount of data which may101* be transferred in one command. If this value is too low, the SD card102* controller has to do multiple partial block writes (argggh!). With103* today (2008) SD cards there is little speed gain if we transfer more104* than 64 KBytes at a time. So use this value until there is any indication105* that we should do more here.106*/107#define MMC_SPI_BLOCKSATONCE 128108109/****************************************************************************/110111/*112* Local Data Structures113*/114115/* "scratch" is per-{command,block} data exchanged with the card */116struct scratch {117u8 status[29];118u8 data_token;119__be16 crc_val;120};121122struct mmc_spi_host {123struct mmc_host *mmc;124struct spi_device *spi;125126unsigned char power_mode;127u16 powerup_msecs;128129struct mmc_spi_platform_data *pdata;130131/* for bulk data transfers */132struct spi_transfer token, t, crc, early_status;133struct spi_message m;134135/* for status readback */136struct spi_transfer status;137struct spi_message readback;138139/* underlying DMA-aware controller, or null */140struct device *dma_dev;141142/* buffer used for commands and for message "overhead" */143struct scratch *data;144dma_addr_t data_dma;145146/* Specs say to write ones most of the time, even when the card147* has no need to read its input data; and many cards won't care.148* This is our source of those ones.149*/150void *ones;151dma_addr_t ones_dma;152};153154155/****************************************************************************/156157/*158* MMC-over-SPI protocol glue, used by the MMC stack interface159*/160161static inline int mmc_cs_off(struct mmc_spi_host *host)162{163/* chipselect will always be inactive after setup() */164return spi_setup(host->spi);165}166167static int168mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)169{170int status;171172if (len > sizeof(*host->data)) {173WARN_ON(1);174return -EIO;175}176177host->status.len = len;178179if (host->dma_dev)180dma_sync_single_for_device(host->dma_dev,181host->data_dma, sizeof(*host->data),182DMA_FROM_DEVICE);183184status = spi_sync_locked(host->spi, &host->readback);185186if (host->dma_dev)187dma_sync_single_for_cpu(host->dma_dev,188host->data_dma, sizeof(*host->data),189DMA_FROM_DEVICE);190191return status;192}193194static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,195unsigned n, u8 byte)196{197u8 *cp = host->data->status;198unsigned long start = jiffies;199200while (1) {201int status;202unsigned i;203204status = mmc_spi_readbytes(host, n);205if (status < 0)206return status;207208for (i = 0; i < n; i++) {209if (cp[i] != byte)210return cp[i];211}212213if (time_is_before_jiffies(start + timeout))214break;215216/* If we need long timeouts, we may release the CPU.217* We use jiffies here because we want to have a relation218* between elapsed time and the blocking of the scheduler.219*/220if (time_is_before_jiffies(start+1))221schedule();222}223return -ETIMEDOUT;224}225226static inline int227mmc_spi_wait_unbusy(struct mmc_spi_host *host, unsigned long timeout)228{229return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);230}231232static int mmc_spi_readtoken(struct mmc_spi_host *host, unsigned long timeout)233{234return mmc_spi_skip(host, timeout, 1, 0xff);235}236237238/*239* Note that for SPI, cmd->resp[0] is not the same data as "native" protocol240* hosts return! The low byte holds R1_SPI bits. The next byte may hold241* R2_SPI bits ... for SEND_STATUS, or after data read errors.242*243* cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on244* newer cards R7 (IF_COND).245*/246247static char *maptype(struct mmc_command *cmd)248{249switch (mmc_spi_resp_type(cmd)) {250case MMC_RSP_SPI_R1: return "R1";251case MMC_RSP_SPI_R1B: return "R1B";252case MMC_RSP_SPI_R2: return "R2/R5";253case MMC_RSP_SPI_R3: return "R3/R4/R7";254default: return "?";255}256}257258/* return zero, else negative errno after setting cmd->error */259static int mmc_spi_response_get(struct mmc_spi_host *host,260struct mmc_command *cmd, int cs_on)261{262u8 *cp = host->data->status;263u8 *end = cp + host->t.len;264int value = 0;265int bitshift;266u8 leftover = 0;267unsigned short rotator;268int i;269char tag[32];270271snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",272cmd->opcode, maptype(cmd));273274/* Except for data block reads, the whole response will already275* be stored in the scratch buffer. It's somewhere after the276* command and the first byte we read after it. We ignore that277* first byte. After STOP_TRANSMISSION command it may include278* two data bits, but otherwise it's all ones.279*/280cp += 8;281while (cp < end && *cp == 0xff)282cp++;283284/* Data block reads (R1 response types) may need more data... */285if (cp == end) {286cp = host->data->status;287end = cp+1;288289/* Card sends N(CR) (== 1..8) bytes of all-ones then one290* status byte ... and we already scanned 2 bytes.291*292* REVISIT block read paths use nasty byte-at-a-time I/O293* so it can always DMA directly into the target buffer.294* It'd probably be better to memcpy() the first chunk and295* avoid extra i/o calls...296*297* Note we check for more than 8 bytes, because in practice,298* some SD cards are slow...299*/300for (i = 2; i < 16; i++) {301value = mmc_spi_readbytes(host, 1);302if (value < 0)303goto done;304if (*cp != 0xff)305goto checkstatus;306}307value = -ETIMEDOUT;308goto done;309}310311checkstatus:312bitshift = 0;313if (*cp & 0x80) {314/* Houston, we have an ugly card with a bit-shifted response */315rotator = *cp++ << 8;316/* read the next byte */317if (cp == end) {318value = mmc_spi_readbytes(host, 1);319if (value < 0)320goto done;321cp = host->data->status;322end = cp+1;323}324rotator |= *cp++;325while (rotator & 0x8000) {326bitshift++;327rotator <<= 1;328}329cmd->resp[0] = rotator >> 8;330leftover = rotator;331} else {332cmd->resp[0] = *cp++;333}334cmd->error = 0;335336/* Status byte: the entire seven-bit R1 response. */337if (cmd->resp[0] != 0) {338if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS)339& cmd->resp[0])340value = -EFAULT; /* Bad address */341else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0])342value = -ENOSYS; /* Function not implemented */343else if (R1_SPI_COM_CRC & cmd->resp[0])344value = -EILSEQ; /* Illegal byte sequence */345else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)346& cmd->resp[0])347value = -EIO; /* I/O error */348/* else R1_SPI_IDLE, "it's resetting" */349}350351switch (mmc_spi_resp_type(cmd)) {352353/* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)354* and less-common stuff like various erase operations.355*/356case MMC_RSP_SPI_R1B:357/* maybe we read all the busy tokens already */358while (cp < end && *cp == 0)359cp++;360if (cp == end)361mmc_spi_wait_unbusy(host, r1b_timeout);362break;363364/* SPI R2 == R1 + second status byte; SEND_STATUS365* SPI R5 == R1 + data byte; IO_RW_DIRECT366*/367case MMC_RSP_SPI_R2:368/* read the next byte */369if (cp == end) {370value = mmc_spi_readbytes(host, 1);371if (value < 0)372goto done;373cp = host->data->status;374end = cp+1;375}376if (bitshift) {377rotator = leftover << 8;378rotator |= *cp << bitshift;379cmd->resp[0] |= (rotator & 0xFF00);380} else {381cmd->resp[0] |= *cp << 8;382}383break;384385/* SPI R3, R4, or R7 == R1 + 4 bytes */386case MMC_RSP_SPI_R3:387rotator = leftover << 8;388cmd->resp[1] = 0;389for (i = 0; i < 4; i++) {390cmd->resp[1] <<= 8;391/* read the next byte */392if (cp == end) {393value = mmc_spi_readbytes(host, 1);394if (value < 0)395goto done;396cp = host->data->status;397end = cp+1;398}399if (bitshift) {400rotator |= *cp++ << bitshift;401cmd->resp[1] |= (rotator >> 8);402rotator <<= 8;403} else {404cmd->resp[1] |= *cp++;405}406}407break;408409/* SPI R1 == just one status byte */410case MMC_RSP_SPI_R1:411break;412413default:414dev_dbg(&host->spi->dev, "bad response type %04x\n",415mmc_spi_resp_type(cmd));416if (value >= 0)417value = -EINVAL;418goto done;419}420421if (value < 0)422dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",423tag, cmd->resp[0], cmd->resp[1]);424425/* disable chipselect on errors and some success cases */426if (value >= 0 && cs_on)427return value;428done:429if (value < 0)430cmd->error = value;431mmc_cs_off(host);432return value;433}434435/* Issue command and read its response.436* Returns zero on success, negative for error.437*438* On error, caller must cope with mmc core retry mechanism. That439* means immediate low-level resubmit, which affects the bus lock...440*/441static int442mmc_spi_command_send(struct mmc_spi_host *host,443struct mmc_request *mrq,444struct mmc_command *cmd, int cs_on)445{446struct scratch *data = host->data;447u8 *cp = data->status;448u32 arg = cmd->arg;449int status;450struct spi_transfer *t;451452/* We can handle most commands (except block reads) in one full453* duplex I/O operation before either starting the next transfer454* (data block or command) or else deselecting the card.455*456* First, write 7 bytes:457* - an all-ones byte to ensure the card is ready458* - opcode byte (plus start and transmission bits)459* - four bytes of big-endian argument460* - crc7 (plus end bit) ... always computed, it's cheap461*462* We init the whole buffer to all-ones, which is what we need463* to write while we're reading (later) response data.464*/465memset(cp++, 0xff, sizeof(data->status));466467*cp++ = 0x40 | cmd->opcode;468*cp++ = (u8)(arg >> 24);469*cp++ = (u8)(arg >> 16);470*cp++ = (u8)(arg >> 8);471*cp++ = (u8)arg;472*cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;473474/* Then, read up to 13 bytes (while writing all-ones):475* - N(CR) (== 1..8) bytes of all-ones476* - status byte (for all response types)477* - the rest of the response, either:478* + nothing, for R1 or R1B responses479* + second status byte, for R2 responses480* + four data bytes, for R3 and R7 responses481*482* Finally, read some more bytes ... in the nice cases we know in483* advance how many, and reading 1 more is always OK:484* - N(EC) (== 0..N) bytes of all-ones, before deselect/finish485* - N(RC) (== 1..N) bytes of all-ones, before next command486* - N(WR) (== 1..N) bytes of all-ones, before data write487*488* So in those cases one full duplex I/O of at most 21 bytes will489* handle the whole command, leaving the card ready to receive a490* data block or new command. We do that whenever we can, shaving491* CPU and IRQ costs (especially when using DMA or FIFOs).492*493* There are two other cases, where it's not generally practical494* to rely on a single I/O:495*496* - R1B responses need at least N(EC) bytes of all-zeroes.497*498* In this case we can *try* to fit it into one I/O, then499* maybe read more data later.500*501* - Data block reads are more troublesome, since a variable502* number of padding bytes precede the token and data.503* + N(CX) (== 0..8) bytes of all-ones, before CSD or CID504* + N(AC) (== 1..many) bytes of all-ones505*506* In this case we currently only have minimal speedups here:507* when N(CR) == 1 we can avoid I/O in response_get().508*/509if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {510cp += 2; /* min(N(CR)) + status */511/* R1 */512} else {513cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */514if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */515cp++;516else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */517cp += 4;518else if (cmd->flags & MMC_RSP_BUSY) /* R1B */519cp = data->status + sizeof(data->status);520/* else: R1 (most commands) */521}522523dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",524cmd->opcode, maptype(cmd));525526/* send command, leaving chipselect active */527spi_message_init(&host->m);528529t = &host->t;530memset(t, 0, sizeof(*t));531t->tx_buf = t->rx_buf = data->status;532t->tx_dma = t->rx_dma = host->data_dma;533t->len = cp - data->status;534t->cs_change = 1;535spi_message_add_tail(t, &host->m);536537if (host->dma_dev) {538host->m.is_dma_mapped = 1;539dma_sync_single_for_device(host->dma_dev,540host->data_dma, sizeof(*host->data),541DMA_BIDIRECTIONAL);542}543status = spi_sync_locked(host->spi, &host->m);544545if (host->dma_dev)546dma_sync_single_for_cpu(host->dma_dev,547host->data_dma, sizeof(*host->data),548DMA_BIDIRECTIONAL);549if (status < 0) {550dev_dbg(&host->spi->dev, " ... write returned %d\n", status);551cmd->error = status;552return status;553}554555/* after no-data commands and STOP_TRANSMISSION, chipselect off */556return mmc_spi_response_get(host, cmd, cs_on);557}558559/* Build data message with up to four separate transfers. For TX, we560* start by writing the data token. And in most cases, we finish with561* a status transfer.562*563* We always provide TX data for data and CRC. The MMC/SD protocol564* requires us to write ones; but Linux defaults to writing zeroes;565* so we explicitly initialize it to all ones on RX paths.566*567* We also handle DMA mapping, so the underlying SPI controller does568* not need to (re)do it for each message.569*/570static void571mmc_spi_setup_data_message(572struct mmc_spi_host *host,573int multiple,574enum dma_data_direction direction)575{576struct spi_transfer *t;577struct scratch *scratch = host->data;578dma_addr_t dma = host->data_dma;579580spi_message_init(&host->m);581if (dma)582host->m.is_dma_mapped = 1;583584/* for reads, readblock() skips 0xff bytes before finding585* the token; for writes, this transfer issues that token.586*/587if (direction == DMA_TO_DEVICE) {588t = &host->token;589memset(t, 0, sizeof(*t));590t->len = 1;591if (multiple)592scratch->data_token = SPI_TOKEN_MULTI_WRITE;593else594scratch->data_token = SPI_TOKEN_SINGLE;595t->tx_buf = &scratch->data_token;596if (dma)597t->tx_dma = dma + offsetof(struct scratch, data_token);598spi_message_add_tail(t, &host->m);599}600601/* Body of transfer is buffer, then CRC ...602* either TX-only, or RX with TX-ones.603*/604t = &host->t;605memset(t, 0, sizeof(*t));606t->tx_buf = host->ones;607t->tx_dma = host->ones_dma;608/* length and actual buffer info are written later */609spi_message_add_tail(t, &host->m);610611t = &host->crc;612memset(t, 0, sizeof(*t));613t->len = 2;614if (direction == DMA_TO_DEVICE) {615/* the actual CRC may get written later */616t->tx_buf = &scratch->crc_val;617if (dma)618t->tx_dma = dma + offsetof(struct scratch, crc_val);619} else {620t->tx_buf = host->ones;621t->tx_dma = host->ones_dma;622t->rx_buf = &scratch->crc_val;623if (dma)624t->rx_dma = dma + offsetof(struct scratch, crc_val);625}626spi_message_add_tail(t, &host->m);627628/*629* A single block read is followed by N(EC) [0+] all-ones bytes630* before deselect ... don't bother.631*632* Multiblock reads are followed by N(AC) [1+] all-ones bytes before633* the next block is read, or a STOP_TRANSMISSION is issued. We'll634* collect that single byte, so readblock() doesn't need to.635*636* For a write, the one-byte data response follows immediately, then637* come zero or more busy bytes, then N(WR) [1+] all-ones bytes.638* Then single block reads may deselect, and multiblock ones issue639* the next token (next data block, or STOP_TRAN). We can try to640* minimize I/O ops by using a single read to collect end-of-busy.641*/642if (multiple || direction == DMA_TO_DEVICE) {643t = &host->early_status;644memset(t, 0, sizeof(*t));645t->len = (direction == DMA_TO_DEVICE)646? sizeof(scratch->status)647: 1;648t->tx_buf = host->ones;649t->tx_dma = host->ones_dma;650t->rx_buf = scratch->status;651if (dma)652t->rx_dma = dma + offsetof(struct scratch, status);653t->cs_change = 1;654spi_message_add_tail(t, &host->m);655}656}657658/*659* Write one block:660* - caller handled preceding N(WR) [1+] all-ones bytes661* - data block662* + token663* + data bytes664* + crc16665* - an all-ones byte ... card writes a data-response byte666* - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'667*668* Return negative errno, else success.669*/670static int671mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t,672unsigned long timeout)673{674struct spi_device *spi = host->spi;675int status, i;676struct scratch *scratch = host->data;677u32 pattern;678679if (host->mmc->use_spi_crc)680scratch->crc_val = cpu_to_be16(681crc_itu_t(0, t->tx_buf, t->len));682if (host->dma_dev)683dma_sync_single_for_device(host->dma_dev,684host->data_dma, sizeof(*scratch),685DMA_BIDIRECTIONAL);686687status = spi_sync_locked(spi, &host->m);688689if (status != 0) {690dev_dbg(&spi->dev, "write error (%d)\n", status);691return status;692}693694if (host->dma_dev)695dma_sync_single_for_cpu(host->dma_dev,696host->data_dma, sizeof(*scratch),697DMA_BIDIRECTIONAL);698699/*700* Get the transmission data-response reply. It must follow701* immediately after the data block we transferred. This reply702* doesn't necessarily tell whether the write operation succeeded;703* it just says if the transmission was ok and whether *earlier*704* writes succeeded; see the standard.705*706* In practice, there are (even modern SDHC-)cards which are late707* in sending the response, and miss the time frame by a few bits,708* so we have to cope with this situation and check the response709* bit-by-bit. Arggh!!!710*/711pattern = scratch->status[0] << 24;712pattern |= scratch->status[1] << 16;713pattern |= scratch->status[2] << 8;714pattern |= scratch->status[3];715716/* First 3 bit of pattern are undefined */717pattern |= 0xE0000000;718719/* left-adjust to leading 0 bit */720while (pattern & 0x80000000)721pattern <<= 1;722/* right-adjust for pattern matching. Code is in bit 4..0 now. */723pattern >>= 27;724725switch (pattern) {726case SPI_RESPONSE_ACCEPTED:727status = 0;728break;729case SPI_RESPONSE_CRC_ERR:730/* host shall then issue MMC_STOP_TRANSMISSION */731status = -EILSEQ;732break;733case SPI_RESPONSE_WRITE_ERR:734/* host shall then issue MMC_STOP_TRANSMISSION,735* and should MMC_SEND_STATUS to sort it out736*/737status = -EIO;738break;739default:740status = -EPROTO;741break;742}743if (status != 0) {744dev_dbg(&spi->dev, "write error %02x (%d)\n",745scratch->status[0], status);746return status;747}748749t->tx_buf += t->len;750if (host->dma_dev)751t->tx_dma += t->len;752753/* Return when not busy. If we didn't collect that status yet,754* we'll need some more I/O.755*/756for (i = 4; i < sizeof(scratch->status); i++) {757/* card is non-busy if the most recent bit is 1 */758if (scratch->status[i] & 0x01)759return 0;760}761return mmc_spi_wait_unbusy(host, timeout);762}763764/*765* Read one block:766* - skip leading all-ones bytes ... either767* + N(AC) [1..f(clock,CSD)] usually, else768* + N(CX) [0..8] when reading CSD or CID769* - data block770* + token ... if error token, no data or crc771* + data bytes772* + crc16773*774* After single block reads, we're done; N(EC) [0+] all-ones bytes follow775* before dropping chipselect.776*777* For multiblock reads, caller either reads the next block or issues a778* STOP_TRANSMISSION command.779*/780static int781mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t,782unsigned long timeout)783{784struct spi_device *spi = host->spi;785int status;786struct scratch *scratch = host->data;787unsigned int bitshift;788u8 leftover;789790/* At least one SD card sends an all-zeroes byte when N(CX)791* applies, before the all-ones bytes ... just cope with that.792*/793status = mmc_spi_readbytes(host, 1);794if (status < 0)795return status;796status = scratch->status[0];797if (status == 0xff || status == 0)798status = mmc_spi_readtoken(host, timeout);799800if (status < 0) {801dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);802return status;803}804805/* The token may be bit-shifted...806* the first 0-bit precedes the data stream.807*/808bitshift = 7;809while (status & 0x80) {810status <<= 1;811bitshift--;812}813leftover = status << 1;814815if (host->dma_dev) {816dma_sync_single_for_device(host->dma_dev,817host->data_dma, sizeof(*scratch),818DMA_BIDIRECTIONAL);819dma_sync_single_for_device(host->dma_dev,820t->rx_dma, t->len,821DMA_FROM_DEVICE);822}823824status = spi_sync_locked(spi, &host->m);825826if (host->dma_dev) {827dma_sync_single_for_cpu(host->dma_dev,828host->data_dma, sizeof(*scratch),829DMA_BIDIRECTIONAL);830dma_sync_single_for_cpu(host->dma_dev,831t->rx_dma, t->len,832DMA_FROM_DEVICE);833}834835if (bitshift) {836/* Walk through the data and the crc and do837* all the magic to get byte-aligned data.838*/839u8 *cp = t->rx_buf;840unsigned int len;841unsigned int bitright = 8 - bitshift;842u8 temp;843for (len = t->len; len; len--) {844temp = *cp;845*cp++ = leftover | (temp >> bitshift);846leftover = temp << bitright;847}848cp = (u8 *) &scratch->crc_val;849temp = *cp;850*cp++ = leftover | (temp >> bitshift);851leftover = temp << bitright;852temp = *cp;853*cp = leftover | (temp >> bitshift);854}855856if (host->mmc->use_spi_crc) {857u16 crc = crc_itu_t(0, t->rx_buf, t->len);858859be16_to_cpus(&scratch->crc_val);860if (scratch->crc_val != crc) {861dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "862"computed=0x%04x len=%d\n",863scratch->crc_val, crc, t->len);864return -EILSEQ;865}866}867868t->rx_buf += t->len;869if (host->dma_dev)870t->rx_dma += t->len;871872return 0;873}874875/*876* An MMC/SD data stage includes one or more blocks, optional CRCs,877* and inline handshaking. That handhaking makes it unlike most878* other SPI protocol stacks.879*/880static void881mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,882struct mmc_data *data, u32 blk_size)883{884struct spi_device *spi = host->spi;885struct device *dma_dev = host->dma_dev;886struct spi_transfer *t;887enum dma_data_direction direction;888struct scatterlist *sg;889unsigned n_sg;890int multiple = (data->blocks > 1);891u32 clock_rate;892unsigned long timeout;893894if (data->flags & MMC_DATA_READ)895direction = DMA_FROM_DEVICE;896else897direction = DMA_TO_DEVICE;898mmc_spi_setup_data_message(host, multiple, direction);899t = &host->t;900901if (t->speed_hz)902clock_rate = t->speed_hz;903else904clock_rate = spi->max_speed_hz;905906timeout = data->timeout_ns +907data->timeout_clks * 1000000 / clock_rate;908timeout = usecs_to_jiffies((unsigned int)(timeout / 1000)) + 1;909910/* Handle scatterlist segments one at a time, with synch for911* each 512-byte block912*/913for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {914int status = 0;915dma_addr_t dma_addr = 0;916void *kmap_addr;917unsigned length = sg->length;918enum dma_data_direction dir = direction;919920/* set up dma mapping for controller drivers that might921* use DMA ... though they may fall back to PIO922*/923if (dma_dev) {924/* never invalidate whole *shared* pages ... */925if ((sg->offset != 0 || length != PAGE_SIZE)926&& dir == DMA_FROM_DEVICE)927dir = DMA_BIDIRECTIONAL;928929dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,930PAGE_SIZE, dir);931if (direction == DMA_TO_DEVICE)932t->tx_dma = dma_addr + sg->offset;933else934t->rx_dma = dma_addr + sg->offset;935}936937/* allow pio too; we don't allow highmem */938kmap_addr = kmap(sg_page(sg));939if (direction == DMA_TO_DEVICE)940t->tx_buf = kmap_addr + sg->offset;941else942t->rx_buf = kmap_addr + sg->offset;943944/* transfer each block, and update request status */945while (length) {946t->len = min(length, blk_size);947948dev_dbg(&host->spi->dev,949" mmc_spi: %s block, %d bytes\n",950(direction == DMA_TO_DEVICE)951? "write"952: "read",953t->len);954955if (direction == DMA_TO_DEVICE)956status = mmc_spi_writeblock(host, t, timeout);957else958status = mmc_spi_readblock(host, t, timeout);959if (status < 0)960break;961962data->bytes_xfered += t->len;963length -= t->len;964965if (!multiple)966break;967}968969/* discard mappings */970if (direction == DMA_FROM_DEVICE)971flush_kernel_dcache_page(sg_page(sg));972kunmap(sg_page(sg));973if (dma_dev)974dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);975976if (status < 0) {977data->error = status;978dev_dbg(&spi->dev, "%s status %d\n",979(direction == DMA_TO_DEVICE)980? "write" : "read",981status);982break;983}984}985986/* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that987* can be issued before multiblock writes. Unlike its more widely988* documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),989* that can affect the STOP_TRAN logic. Complete (and current)990* MMC specs should sort that out before Linux starts using CMD23.991*/992if (direction == DMA_TO_DEVICE && multiple) {993struct scratch *scratch = host->data;994int tmp;995const unsigned statlen = sizeof(scratch->status);996997dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");998999/* Tweak the per-block message we set up earlier by morphing1000* it to hold single buffer with the token followed by some1001* all-ones bytes ... skip N(BR) (0..1), scan the rest for1002* "not busy any longer" status, and leave chip selected.1003*/1004INIT_LIST_HEAD(&host->m.transfers);1005list_add(&host->early_status.transfer_list,1006&host->m.transfers);10071008memset(scratch->status, 0xff, statlen);1009scratch->status[0] = SPI_TOKEN_STOP_TRAN;10101011host->early_status.tx_buf = host->early_status.rx_buf;1012host->early_status.tx_dma = host->early_status.rx_dma;1013host->early_status.len = statlen;10141015if (host->dma_dev)1016dma_sync_single_for_device(host->dma_dev,1017host->data_dma, sizeof(*scratch),1018DMA_BIDIRECTIONAL);10191020tmp = spi_sync_locked(spi, &host->m);10211022if (host->dma_dev)1023dma_sync_single_for_cpu(host->dma_dev,1024host->data_dma, sizeof(*scratch),1025DMA_BIDIRECTIONAL);10261027if (tmp < 0) {1028if (!data->error)1029data->error = tmp;1030return;1031}10321033/* Ideally we collected "not busy" status with one I/O,1034* avoiding wasteful byte-at-a-time scanning... but more1035* I/O is often needed.1036*/1037for (tmp = 2; tmp < statlen; tmp++) {1038if (scratch->status[tmp] != 0)1039return;1040}1041tmp = mmc_spi_wait_unbusy(host, timeout);1042if (tmp < 0 && !data->error)1043data->error = tmp;1044}1045}10461047/****************************************************************************/10481049/*1050* MMC driver implementation -- the interface to the MMC stack1051*/10521053static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)1054{1055struct mmc_spi_host *host = mmc_priv(mmc);1056int status = -EINVAL;1057int crc_retry = 5;1058struct mmc_command stop;10591060#ifdef DEBUG1061/* MMC core and layered drivers *MUST* issue SPI-aware commands */1062{1063struct mmc_command *cmd;1064int invalid = 0;10651066cmd = mrq->cmd;1067if (!mmc_spi_resp_type(cmd)) {1068dev_dbg(&host->spi->dev, "bogus command\n");1069cmd->error = -EINVAL;1070invalid = 1;1071}10721073cmd = mrq->stop;1074if (cmd && !mmc_spi_resp_type(cmd)) {1075dev_dbg(&host->spi->dev, "bogus STOP command\n");1076cmd->error = -EINVAL;1077invalid = 1;1078}10791080if (invalid) {1081dump_stack();1082mmc_request_done(host->mmc, mrq);1083return;1084}1085}1086#endif10871088/* request exclusive bus access */1089spi_bus_lock(host->spi->master);10901091crc_recover:1092/* issue command; then optionally data and stop */1093status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);1094if (status == 0 && mrq->data) {1095mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);10961097/*1098* The SPI bus is not always reliable for large data transfers.1099* If an occasional crc error is reported by the SD device with1100* data read/write over SPI, it may be recovered by repeating1101* the last SD command again. The retry count is set to 5 to1102* ensure the driver passes stress tests.1103*/1104if (mrq->data->error == -EILSEQ && crc_retry) {1105stop.opcode = MMC_STOP_TRANSMISSION;1106stop.arg = 0;1107stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;1108status = mmc_spi_command_send(host, mrq, &stop, 0);1109crc_retry--;1110mrq->data->error = 0;1111goto crc_recover;1112}11131114if (mrq->stop)1115status = mmc_spi_command_send(host, mrq, mrq->stop, 0);1116else1117mmc_cs_off(host);1118}11191120/* release the bus */1121spi_bus_unlock(host->spi->master);11221123mmc_request_done(host->mmc, mrq);1124}11251126/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"1127*1128* NOTE that here we can't know that the card has just been powered up;1129* not all MMC/SD sockets support power switching.1130*1131* FIXME when the card is still in SPI mode, e.g. from a previous kernel,1132* this doesn't seem to do the right thing at all...1133*/1134static void mmc_spi_initsequence(struct mmc_spi_host *host)1135{1136/* Try to be very sure any previous command has completed;1137* wait till not-busy, skip debris from any old commands.1138*/1139mmc_spi_wait_unbusy(host, r1b_timeout);1140mmc_spi_readbytes(host, 10);11411142/*1143* Do a burst with chipselect active-high. We need to do this to1144* meet the requirement of 74 clock cycles with both chipselect1145* and CMD (MOSI) high before CMD0 ... after the card has been1146* powered up to Vdd(min), and so is ready to take commands.1147*1148* Some cards are particularly needy of this (e.g. Viking "SD256")1149* while most others don't seem to care.1150*1151* Note that this is one of the places MMC/SD plays games with the1152* SPI protocol. Another is that when chipselect is released while1153* the card returns BUSY status, the clock must issue several cycles1154* with chipselect high before the card will stop driving its output.1155*/1156host->spi->mode |= SPI_CS_HIGH;1157if (spi_setup(host->spi) != 0) {1158/* Just warn; most cards work without it. */1159dev_warn(&host->spi->dev,1160"can't change chip-select polarity\n");1161host->spi->mode &= ~SPI_CS_HIGH;1162} else {1163mmc_spi_readbytes(host, 18);11641165host->spi->mode &= ~SPI_CS_HIGH;1166if (spi_setup(host->spi) != 0) {1167/* Wot, we can't get the same setup we had before? */1168dev_err(&host->spi->dev,1169"can't restore chip-select polarity\n");1170}1171}1172}11731174static char *mmc_powerstring(u8 power_mode)1175{1176switch (power_mode) {1177case MMC_POWER_OFF: return "off";1178case MMC_POWER_UP: return "up";1179case MMC_POWER_ON: return "on";1180}1181return "?";1182}11831184static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)1185{1186struct mmc_spi_host *host = mmc_priv(mmc);11871188if (host->power_mode != ios->power_mode) {1189int canpower;11901191canpower = host->pdata && host->pdata->setpower;11921193dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",1194mmc_powerstring(ios->power_mode),1195ios->vdd,1196canpower ? ", can switch" : "");11971198/* switch power on/off if possible, accounting for1199* max 250msec powerup time if needed.1200*/1201if (canpower) {1202switch (ios->power_mode) {1203case MMC_POWER_OFF:1204case MMC_POWER_UP:1205host->pdata->setpower(&host->spi->dev,1206ios->vdd);1207if (ios->power_mode == MMC_POWER_UP)1208msleep(host->powerup_msecs);1209}1210}12111212/* See 6.4.1 in the simplified SD card physical spec 2.0 */1213if (ios->power_mode == MMC_POWER_ON)1214mmc_spi_initsequence(host);12151216/* If powering down, ground all card inputs to avoid power1217* delivery from data lines! On a shared SPI bus, this1218* will probably be temporary; 6.4.2 of the simplified SD1219* spec says this must last at least 1msec.1220*1221* - Clock low means CPOL 0, e.g. mode 01222* - MOSI low comes from writing zero1223* - Chipselect is usually active low...1224*/1225if (canpower && ios->power_mode == MMC_POWER_OFF) {1226int mres;1227u8 nullbyte = 0;12281229host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);1230mres = spi_setup(host->spi);1231if (mres < 0)1232dev_dbg(&host->spi->dev,1233"switch to SPI mode 0 failed\n");12341235if (spi_write(host->spi, &nullbyte, 1) < 0)1236dev_dbg(&host->spi->dev,1237"put spi signals to low failed\n");12381239/*1240* Now clock should be low due to spi mode 0;1241* MOSI should be low because of written 0x00;1242* chipselect should be low (it is active low)1243* power supply is off, so now MMC is off too!1244*1245* FIXME no, chipselect can be high since the1246* device is inactive and SPI_CS_HIGH is clear...1247*/1248msleep(10);1249if (mres == 0) {1250host->spi->mode |= (SPI_CPOL|SPI_CPHA);1251mres = spi_setup(host->spi);1252if (mres < 0)1253dev_dbg(&host->spi->dev,1254"switch back to SPI mode 3"1255" failed\n");1256}1257}12581259host->power_mode = ios->power_mode;1260}12611262if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {1263int status;12641265host->spi->max_speed_hz = ios->clock;1266status = spi_setup(host->spi);1267dev_dbg(&host->spi->dev,1268"mmc_spi: clock to %d Hz, %d\n",1269host->spi->max_speed_hz, status);1270}1271}12721273static int mmc_spi_get_ro(struct mmc_host *mmc)1274{1275struct mmc_spi_host *host = mmc_priv(mmc);12761277if (host->pdata && host->pdata->get_ro)1278return !!host->pdata->get_ro(mmc->parent);1279/*1280* Board doesn't support read only detection; let the mmc core1281* decide what to do.1282*/1283return -ENOSYS;1284}12851286static int mmc_spi_get_cd(struct mmc_host *mmc)1287{1288struct mmc_spi_host *host = mmc_priv(mmc);12891290if (host->pdata && host->pdata->get_cd)1291return !!host->pdata->get_cd(mmc->parent);1292return -ENOSYS;1293}12941295static const struct mmc_host_ops mmc_spi_ops = {1296.request = mmc_spi_request,1297.set_ios = mmc_spi_set_ios,1298.get_ro = mmc_spi_get_ro,1299.get_cd = mmc_spi_get_cd,1300};130113021303/****************************************************************************/13041305/*1306* SPI driver implementation1307*/13081309static irqreturn_t1310mmc_spi_detect_irq(int irq, void *mmc)1311{1312struct mmc_spi_host *host = mmc_priv(mmc);1313u16 delay_msec = max(host->pdata->detect_delay, (u16)100);13141315mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));1316return IRQ_HANDLED;1317}13181319static int mmc_spi_probe(struct spi_device *spi)1320{1321void *ones;1322struct mmc_host *mmc;1323struct mmc_spi_host *host;1324int status;13251326/* We rely on full duplex transfers, mostly to reduce1327* per-transfer overheads (by making fewer transfers).1328*/1329if (spi->master->flags & SPI_MASTER_HALF_DUPLEX)1330return -EINVAL;13311332/* MMC and SD specs only seem to care that sampling is on the1333* rising edge ... meaning SPI modes 0 or 3. So either SPI mode1334* should be legit. We'll use mode 0 since the steady state is 0,1335* which is appropriate for hotplugging, unless the platform data1336* specify mode 3 (if hardware is not compatible to mode 0).1337*/1338if (spi->mode != SPI_MODE_3)1339spi->mode = SPI_MODE_0;1340spi->bits_per_word = 8;13411342status = spi_setup(spi);1343if (status < 0) {1344dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",1345spi->mode, spi->max_speed_hz / 1000,1346status);1347return status;1348}13491350/* We need a supply of ones to transmit. This is the only time1351* the CPU touches these, so cache coherency isn't a concern.1352*1353* NOTE if many systems use more than one MMC-over-SPI connector1354* it'd save some memory to share this. That's evidently rare.1355*/1356status = -ENOMEM;1357ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);1358if (!ones)1359goto nomem;1360memset(ones, 0xff, MMC_SPI_BLOCKSIZE);13611362mmc = mmc_alloc_host(sizeof(*host), &spi->dev);1363if (!mmc)1364goto nomem;13651366mmc->ops = &mmc_spi_ops;1367mmc->max_blk_size = MMC_SPI_BLOCKSIZE;1368mmc->max_segs = MMC_SPI_BLOCKSATONCE;1369mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE;1370mmc->max_blk_count = MMC_SPI_BLOCKSATONCE;13711372mmc->caps = MMC_CAP_SPI;13731374/* SPI doesn't need the lowspeed device identification thing for1375* MMC or SD cards, since it never comes up in open drain mode.1376* That's good; some SPI masters can't handle very low speeds!1377*1378* However, low speed SDIO cards need not handle over 400 KHz;1379* that's the only reason not to use a few MHz for f_min (until1380* the upper layer reads the target frequency from the CSD).1381*/1382mmc->f_min = 400000;1383mmc->f_max = spi->max_speed_hz;13841385host = mmc_priv(mmc);1386host->mmc = mmc;1387host->spi = spi;13881389host->ones = ones;13901391/* Platform data is used to hook up things like card sensing1392* and power switching gpios.1393*/1394host->pdata = mmc_spi_get_pdata(spi);1395if (host->pdata)1396mmc->ocr_avail = host->pdata->ocr_mask;1397if (!mmc->ocr_avail) {1398dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");1399mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;1400}1401if (host->pdata && host->pdata->setpower) {1402host->powerup_msecs = host->pdata->powerup_msecs;1403if (!host->powerup_msecs || host->powerup_msecs > 250)1404host->powerup_msecs = 250;1405}14061407dev_set_drvdata(&spi->dev, mmc);14081409/* preallocate dma buffers */1410host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);1411if (!host->data)1412goto fail_nobuf1;14131414if (spi->master->dev.parent->dma_mask) {1415struct device *dev = spi->master->dev.parent;14161417host->dma_dev = dev;1418host->ones_dma = dma_map_single(dev, ones,1419MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);1420host->data_dma = dma_map_single(dev, host->data,1421sizeof(*host->data), DMA_BIDIRECTIONAL);14221423/* REVISIT in theory those map operations can fail... */14241425dma_sync_single_for_cpu(host->dma_dev,1426host->data_dma, sizeof(*host->data),1427DMA_BIDIRECTIONAL);1428}14291430/* setup message for status/busy readback */1431spi_message_init(&host->readback);1432host->readback.is_dma_mapped = (host->dma_dev != NULL);14331434spi_message_add_tail(&host->status, &host->readback);1435host->status.tx_buf = host->ones;1436host->status.tx_dma = host->ones_dma;1437host->status.rx_buf = &host->data->status;1438host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);1439host->status.cs_change = 1;14401441/* register card detect irq */1442if (host->pdata && host->pdata->init) {1443status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);1444if (status != 0)1445goto fail_glue_init;1446}14471448/* pass platform capabilities, if any */1449if (host->pdata)1450mmc->caps |= host->pdata->caps;14511452status = mmc_add_host(mmc);1453if (status != 0)1454goto fail_add_host;14551456dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",1457dev_name(&mmc->class_dev),1458host->dma_dev ? "" : ", no DMA",1459(host->pdata && host->pdata->get_ro)1460? "" : ", no WP",1461(host->pdata && host->pdata->setpower)1462? "" : ", no poweroff",1463(mmc->caps & MMC_CAP_NEEDS_POLL)1464? ", cd polling" : "");1465return 0;14661467fail_add_host:1468mmc_remove_host (mmc);1469fail_glue_init:1470if (host->dma_dev)1471dma_unmap_single(host->dma_dev, host->data_dma,1472sizeof(*host->data), DMA_BIDIRECTIONAL);1473kfree(host->data);14741475fail_nobuf1:1476mmc_free_host(mmc);1477mmc_spi_put_pdata(spi);1478dev_set_drvdata(&spi->dev, NULL);14791480nomem:1481kfree(ones);1482return status;1483}148414851486static int __devexit mmc_spi_remove(struct spi_device *spi)1487{1488struct mmc_host *mmc = dev_get_drvdata(&spi->dev);1489struct mmc_spi_host *host;14901491if (mmc) {1492host = mmc_priv(mmc);14931494/* prevent new mmc_detect_change() calls */1495if (host->pdata && host->pdata->exit)1496host->pdata->exit(&spi->dev, mmc);14971498mmc_remove_host(mmc);14991500if (host->dma_dev) {1501dma_unmap_single(host->dma_dev, host->ones_dma,1502MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);1503dma_unmap_single(host->dma_dev, host->data_dma,1504sizeof(*host->data), DMA_BIDIRECTIONAL);1505}15061507kfree(host->data);1508kfree(host->ones);15091510spi->max_speed_hz = mmc->f_max;1511mmc_free_host(mmc);1512mmc_spi_put_pdata(spi);1513dev_set_drvdata(&spi->dev, NULL);1514}1515return 0;1516}15171518static struct of_device_id mmc_spi_of_match_table[] __devinitdata = {1519{ .compatible = "mmc-spi-slot", },1520{},1521};15221523static struct spi_driver mmc_spi_driver = {1524.driver = {1525.name = "mmc_spi",1526.bus = &spi_bus_type,1527.owner = THIS_MODULE,1528.of_match_table = mmc_spi_of_match_table,1529},1530.probe = mmc_spi_probe,1531.remove = __devexit_p(mmc_spi_remove),1532};153315341535static int __init mmc_spi_init(void)1536{1537return spi_register_driver(&mmc_spi_driver);1538}1539module_init(mmc_spi_init);154015411542static void __exit mmc_spi_exit(void)1543{1544spi_unregister_driver(&mmc_spi_driver);1545}1546module_exit(mmc_spi_exit);154715481549MODULE_AUTHOR("Mike Lavender, David Brownell, "1550"Hans-Peter Nilsson, Jan Nikitenko");1551MODULE_DESCRIPTION("SPI SD/MMC host driver");1552MODULE_LICENSE("GPL");1553MODULE_ALIAS("spi:mmc_spi");155415551556