xref: /OK3568_Linux_fs/kernel/drivers/mmc/host/mmc_spi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Access SD/MMC cards through SPI master controllers
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright 2005, Intec Automation,
6*4882a593Smuzhiyun  *		Mike Lavender (mike@steroidmicros)
7*4882a593Smuzhiyun  * (C) Copyright 2006-2007, David Brownell
8*4882a593Smuzhiyun  * (C) Copyright 2007, Axis Communications,
9*4882a593Smuzhiyun  *		Hans-Peter Nilsson (hp@axis.com)
10*4882a593Smuzhiyun  * (C) Copyright 2007, ATRON electronic GmbH,
11*4882a593Smuzhiyun  *		Jan Nikitenko <jan.nikitenko@gmail.com>
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/delay.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/bio.h>
18*4882a593Smuzhiyun #include <linux/dma-mapping.h>
19*4882a593Smuzhiyun #include <linux/crc7.h>
20*4882a593Smuzhiyun #include <linux/crc-itu-t.h>
21*4882a593Smuzhiyun #include <linux/scatterlist.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/mmc/host.h>
24*4882a593Smuzhiyun #include <linux/mmc/mmc.h>		/* for R1_SPI_* bit values */
25*4882a593Smuzhiyun #include <linux/mmc/slot-gpio.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <linux/spi/spi.h>
28*4882a593Smuzhiyun #include <linux/spi/mmc_spi.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include <asm/unaligned.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* NOTES:
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * - For now, we won't try to interoperate with a real mmc/sd/sdio
36*4882a593Smuzhiyun  *   controller, although some of them do have hardware support for
37*4882a593Smuzhiyun  *   SPI protocol.  The main reason for such configs would be mmc-ish
38*4882a593Smuzhiyun  *   cards like DataFlash, which don't support that "native" protocol.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  *   We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
41*4882a593Smuzhiyun  *   switch between driver stacks, and in any case if "native" mode
42*4882a593Smuzhiyun  *   is available, it will be faster and hence preferable.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * - MMC depends on a different chipselect management policy than the
45*4882a593Smuzhiyun  *   SPI interface currently supports for shared bus segments:  it needs
46*4882a593Smuzhiyun  *   to issue multiple spi_message requests with the chipselect active,
47*4882a593Smuzhiyun  *   using the results of one message to decide the next one to issue.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  *   Pending updates to the programming interface, this driver expects
50*4882a593Smuzhiyun  *   that it not share the bus with other drivers (precluding conflicts).
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * - We tell the controller to keep the chipselect active from the
53*4882a593Smuzhiyun  *   beginning of an mmc_host_ops.request until the end.  So beware
54*4882a593Smuzhiyun  *   of SPI controller drivers that mis-handle the cs_change flag!
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *   However, many cards seem OK with chipselect flapping up/down
57*4882a593Smuzhiyun  *   during that time ... at least on unshared bus segments.
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * Local protocol constants, internal to data block protocols.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Response tokens used to ack each block written: */
66*4882a593Smuzhiyun #define SPI_MMC_RESPONSE_CODE(x)	((x) & 0x1f)
67*4882a593Smuzhiyun #define SPI_RESPONSE_ACCEPTED		((2 << 1)|1)
68*4882a593Smuzhiyun #define SPI_RESPONSE_CRC_ERR		((5 << 1)|1)
69*4882a593Smuzhiyun #define SPI_RESPONSE_WRITE_ERR		((6 << 1)|1)
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /* Read and write blocks start with these tokens and end with crc;
72*4882a593Smuzhiyun  * on error, read tokens act like a subset of R2_SPI_* values.
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun #define SPI_TOKEN_SINGLE	0xfe	/* single block r/w, multiblock read */
75*4882a593Smuzhiyun #define SPI_TOKEN_MULTI_WRITE	0xfc	/* multiblock write */
76*4882a593Smuzhiyun #define SPI_TOKEN_STOP_TRAN	0xfd	/* terminate multiblock write */
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #define MMC_SPI_BLOCKSIZE	512
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun #define MMC_SPI_R1B_TIMEOUT_MS	3000
81*4882a593Smuzhiyun #define MMC_SPI_INIT_TIMEOUT_MS	3000
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /* One of the critical speed parameters is the amount of data which may
84*4882a593Smuzhiyun  * be transferred in one command. If this value is too low, the SD card
85*4882a593Smuzhiyun  * controller has to do multiple partial block writes (argggh!). With
86*4882a593Smuzhiyun  * today (2008) SD cards there is little speed gain if we transfer more
87*4882a593Smuzhiyun  * than 64 KBytes at a time. So use this value until there is any indication
88*4882a593Smuzhiyun  * that we should do more here.
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun #define MMC_SPI_BLOCKSATONCE	128
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /****************************************************************************/
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun  * Local Data Structures
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /* "scratch" is per-{command,block} data exchanged with the card */
99*4882a593Smuzhiyun struct scratch {
100*4882a593Smuzhiyun 	u8			status[29];
101*4882a593Smuzhiyun 	u8			data_token;
102*4882a593Smuzhiyun 	__be16			crc_val;
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun struct mmc_spi_host {
106*4882a593Smuzhiyun 	struct mmc_host		*mmc;
107*4882a593Smuzhiyun 	struct spi_device	*spi;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	unsigned char		power_mode;
110*4882a593Smuzhiyun 	u16			powerup_msecs;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	struct mmc_spi_platform_data	*pdata;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/* for bulk data transfers */
115*4882a593Smuzhiyun 	struct spi_transfer	token, t, crc, early_status;
116*4882a593Smuzhiyun 	struct spi_message	m;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* for status readback */
119*4882a593Smuzhiyun 	struct spi_transfer	status;
120*4882a593Smuzhiyun 	struct spi_message	readback;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	/* underlying DMA-aware controller, or null */
123*4882a593Smuzhiyun 	struct device		*dma_dev;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* buffer used for commands and for message "overhead" */
126*4882a593Smuzhiyun 	struct scratch		*data;
127*4882a593Smuzhiyun 	dma_addr_t		data_dma;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	/* Specs say to write ones most of the time, even when the card
130*4882a593Smuzhiyun 	 * has no need to read its input data; and many cards won't care.
131*4882a593Smuzhiyun 	 * This is our source of those ones.
132*4882a593Smuzhiyun 	 */
133*4882a593Smuzhiyun 	void			*ones;
134*4882a593Smuzhiyun 	dma_addr_t		ones_dma;
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /****************************************************************************/
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun  * MMC-over-SPI protocol glue, used by the MMC stack interface
142*4882a593Smuzhiyun  */
143*4882a593Smuzhiyun 
mmc_cs_off(struct mmc_spi_host * host)144*4882a593Smuzhiyun static inline int mmc_cs_off(struct mmc_spi_host *host)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	/* chipselect will always be inactive after setup() */
147*4882a593Smuzhiyun 	return spi_setup(host->spi);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static int
mmc_spi_readbytes(struct mmc_spi_host * host,unsigned len)151*4882a593Smuzhiyun mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	int status;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (len > sizeof(*host->data)) {
156*4882a593Smuzhiyun 		WARN_ON(1);
157*4882a593Smuzhiyun 		return -EIO;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	host->status.len = len;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (host->dma_dev)
163*4882a593Smuzhiyun 		dma_sync_single_for_device(host->dma_dev,
164*4882a593Smuzhiyun 				host->data_dma, sizeof(*host->data),
165*4882a593Smuzhiyun 				DMA_FROM_DEVICE);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	status = spi_sync_locked(host->spi, &host->readback);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (host->dma_dev)
170*4882a593Smuzhiyun 		dma_sync_single_for_cpu(host->dma_dev,
171*4882a593Smuzhiyun 				host->data_dma, sizeof(*host->data),
172*4882a593Smuzhiyun 				DMA_FROM_DEVICE);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	return status;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
mmc_spi_skip(struct mmc_spi_host * host,unsigned long timeout,unsigned n,u8 byte)177*4882a593Smuzhiyun static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
178*4882a593Smuzhiyun 			unsigned n, u8 byte)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	u8 *cp = host->data->status;
181*4882a593Smuzhiyun 	unsigned long start = jiffies;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	while (1) {
184*4882a593Smuzhiyun 		int		status;
185*4882a593Smuzhiyun 		unsigned	i;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 		status = mmc_spi_readbytes(host, n);
188*4882a593Smuzhiyun 		if (status < 0)
189*4882a593Smuzhiyun 			return status;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		for (i = 0; i < n; i++) {
192*4882a593Smuzhiyun 			if (cp[i] != byte)
193*4882a593Smuzhiyun 				return cp[i];
194*4882a593Smuzhiyun 		}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		if (time_is_before_jiffies(start + timeout))
197*4882a593Smuzhiyun 			break;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 		/* If we need long timeouts, we may release the CPU.
200*4882a593Smuzhiyun 		 * We use jiffies here because we want to have a relation
201*4882a593Smuzhiyun 		 * between elapsed time and the blocking of the scheduler.
202*4882a593Smuzhiyun 		 */
203*4882a593Smuzhiyun 		if (time_is_before_jiffies(start + 1))
204*4882a593Smuzhiyun 			schedule();
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 	return -ETIMEDOUT;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun static inline int
mmc_spi_wait_unbusy(struct mmc_spi_host * host,unsigned long timeout)210*4882a593Smuzhiyun mmc_spi_wait_unbusy(struct mmc_spi_host *host, unsigned long timeout)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
mmc_spi_readtoken(struct mmc_spi_host * host,unsigned long timeout)215*4882a593Smuzhiyun static int mmc_spi_readtoken(struct mmc_spi_host *host, unsigned long timeout)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	return mmc_spi_skip(host, timeout, 1, 0xff);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun  * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
223*4882a593Smuzhiyun  * hosts return!  The low byte holds R1_SPI bits.  The next byte may hold
224*4882a593Smuzhiyun  * R2_SPI bits ... for SEND_STATUS, or after data read errors.
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
227*4882a593Smuzhiyun  * newer cards R7 (IF_COND).
228*4882a593Smuzhiyun  */
229*4882a593Smuzhiyun 
maptype(struct mmc_command * cmd)230*4882a593Smuzhiyun static char *maptype(struct mmc_command *cmd)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	switch (mmc_spi_resp_type(cmd)) {
233*4882a593Smuzhiyun 	case MMC_RSP_SPI_R1:	return "R1";
234*4882a593Smuzhiyun 	case MMC_RSP_SPI_R1B:	return "R1B";
235*4882a593Smuzhiyun 	case MMC_RSP_SPI_R2:	return "R2/R5";
236*4882a593Smuzhiyun 	case MMC_RSP_SPI_R3:	return "R3/R4/R7";
237*4882a593Smuzhiyun 	default:		return "?";
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /* return zero, else negative errno after setting cmd->error */
mmc_spi_response_get(struct mmc_spi_host * host,struct mmc_command * cmd,int cs_on)242*4882a593Smuzhiyun static int mmc_spi_response_get(struct mmc_spi_host *host,
243*4882a593Smuzhiyun 		struct mmc_command *cmd, int cs_on)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	unsigned long timeout_ms;
246*4882a593Smuzhiyun 	u8	*cp = host->data->status;
247*4882a593Smuzhiyun 	u8	*end = cp + host->t.len;
248*4882a593Smuzhiyun 	int	value = 0;
249*4882a593Smuzhiyun 	int	bitshift;
250*4882a593Smuzhiyun 	u8 	leftover = 0;
251*4882a593Smuzhiyun 	unsigned short rotator;
252*4882a593Smuzhiyun 	int 	i;
253*4882a593Smuzhiyun 	char	tag[32];
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
256*4882a593Smuzhiyun 		cmd->opcode, maptype(cmd));
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* Except for data block reads, the whole response will already
259*4882a593Smuzhiyun 	 * be stored in the scratch buffer.  It's somewhere after the
260*4882a593Smuzhiyun 	 * command and the first byte we read after it.  We ignore that
261*4882a593Smuzhiyun 	 * first byte.  After STOP_TRANSMISSION command it may include
262*4882a593Smuzhiyun 	 * two data bits, but otherwise it's all ones.
263*4882a593Smuzhiyun 	 */
264*4882a593Smuzhiyun 	cp += 8;
265*4882a593Smuzhiyun 	while (cp < end && *cp == 0xff)
266*4882a593Smuzhiyun 		cp++;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/* Data block reads (R1 response types) may need more data... */
269*4882a593Smuzhiyun 	if (cp == end) {
270*4882a593Smuzhiyun 		cp = host->data->status;
271*4882a593Smuzhiyun 		end = cp+1;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		/* Card sends N(CR) (== 1..8) bytes of all-ones then one
274*4882a593Smuzhiyun 		 * status byte ... and we already scanned 2 bytes.
275*4882a593Smuzhiyun 		 *
276*4882a593Smuzhiyun 		 * REVISIT block read paths use nasty byte-at-a-time I/O
277*4882a593Smuzhiyun 		 * so it can always DMA directly into the target buffer.
278*4882a593Smuzhiyun 		 * It'd probably be better to memcpy() the first chunk and
279*4882a593Smuzhiyun 		 * avoid extra i/o calls...
280*4882a593Smuzhiyun 		 *
281*4882a593Smuzhiyun 		 * Note we check for more than 8 bytes, because in practice,
282*4882a593Smuzhiyun 		 * some SD cards are slow...
283*4882a593Smuzhiyun 		 */
284*4882a593Smuzhiyun 		for (i = 2; i < 16; i++) {
285*4882a593Smuzhiyun 			value = mmc_spi_readbytes(host, 1);
286*4882a593Smuzhiyun 			if (value < 0)
287*4882a593Smuzhiyun 				goto done;
288*4882a593Smuzhiyun 			if (*cp != 0xff)
289*4882a593Smuzhiyun 				goto checkstatus;
290*4882a593Smuzhiyun 		}
291*4882a593Smuzhiyun 		value = -ETIMEDOUT;
292*4882a593Smuzhiyun 		goto done;
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun checkstatus:
296*4882a593Smuzhiyun 	bitshift = 0;
297*4882a593Smuzhiyun 	if (*cp & 0x80)	{
298*4882a593Smuzhiyun 		/* Houston, we have an ugly card with a bit-shifted response */
299*4882a593Smuzhiyun 		rotator = *cp++ << 8;
300*4882a593Smuzhiyun 		/* read the next byte */
301*4882a593Smuzhiyun 		if (cp == end) {
302*4882a593Smuzhiyun 			value = mmc_spi_readbytes(host, 1);
303*4882a593Smuzhiyun 			if (value < 0)
304*4882a593Smuzhiyun 				goto done;
305*4882a593Smuzhiyun 			cp = host->data->status;
306*4882a593Smuzhiyun 			end = cp+1;
307*4882a593Smuzhiyun 		}
308*4882a593Smuzhiyun 		rotator |= *cp++;
309*4882a593Smuzhiyun 		while (rotator & 0x8000) {
310*4882a593Smuzhiyun 			bitshift++;
311*4882a593Smuzhiyun 			rotator <<= 1;
312*4882a593Smuzhiyun 		}
313*4882a593Smuzhiyun 		cmd->resp[0] = rotator >> 8;
314*4882a593Smuzhiyun 		leftover = rotator;
315*4882a593Smuzhiyun 	} else {
316*4882a593Smuzhiyun 		cmd->resp[0] = *cp++;
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 	cmd->error = 0;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* Status byte: the entire seven-bit R1 response.  */
321*4882a593Smuzhiyun 	if (cmd->resp[0] != 0) {
322*4882a593Smuzhiyun 		if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS)
323*4882a593Smuzhiyun 				& cmd->resp[0])
324*4882a593Smuzhiyun 			value = -EFAULT; /* Bad address */
325*4882a593Smuzhiyun 		else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0])
326*4882a593Smuzhiyun 			value = -ENOSYS; /* Function not implemented */
327*4882a593Smuzhiyun 		else if (R1_SPI_COM_CRC & cmd->resp[0])
328*4882a593Smuzhiyun 			value = -EILSEQ; /* Illegal byte sequence */
329*4882a593Smuzhiyun 		else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
330*4882a593Smuzhiyun 				& cmd->resp[0])
331*4882a593Smuzhiyun 			value = -EIO;    /* I/O error */
332*4882a593Smuzhiyun 		/* else R1_SPI_IDLE, "it's resetting" */
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	switch (mmc_spi_resp_type(cmd)) {
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
338*4882a593Smuzhiyun 	 * and less-common stuff like various erase operations.
339*4882a593Smuzhiyun 	 */
340*4882a593Smuzhiyun 	case MMC_RSP_SPI_R1B:
341*4882a593Smuzhiyun 		/* maybe we read all the busy tokens already */
342*4882a593Smuzhiyun 		while (cp < end && *cp == 0)
343*4882a593Smuzhiyun 			cp++;
344*4882a593Smuzhiyun 		if (cp == end) {
345*4882a593Smuzhiyun 			timeout_ms = cmd->busy_timeout ? cmd->busy_timeout :
346*4882a593Smuzhiyun 				MMC_SPI_R1B_TIMEOUT_MS;
347*4882a593Smuzhiyun 			mmc_spi_wait_unbusy(host, msecs_to_jiffies(timeout_ms));
348*4882a593Smuzhiyun 		}
349*4882a593Smuzhiyun 		break;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/* SPI R2 == R1 + second status byte; SEND_STATUS
352*4882a593Smuzhiyun 	 * SPI R5 == R1 + data byte; IO_RW_DIRECT
353*4882a593Smuzhiyun 	 */
354*4882a593Smuzhiyun 	case MMC_RSP_SPI_R2:
355*4882a593Smuzhiyun 		/* read the next byte */
356*4882a593Smuzhiyun 		if (cp == end) {
357*4882a593Smuzhiyun 			value = mmc_spi_readbytes(host, 1);
358*4882a593Smuzhiyun 			if (value < 0)
359*4882a593Smuzhiyun 				goto done;
360*4882a593Smuzhiyun 			cp = host->data->status;
361*4882a593Smuzhiyun 			end = cp+1;
362*4882a593Smuzhiyun 		}
363*4882a593Smuzhiyun 		if (bitshift) {
364*4882a593Smuzhiyun 			rotator = leftover << 8;
365*4882a593Smuzhiyun 			rotator |= *cp << bitshift;
366*4882a593Smuzhiyun 			cmd->resp[0] |= (rotator & 0xFF00);
367*4882a593Smuzhiyun 		} else {
368*4882a593Smuzhiyun 			cmd->resp[0] |= *cp << 8;
369*4882a593Smuzhiyun 		}
370*4882a593Smuzhiyun 		break;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/* SPI R3, R4, or R7 == R1 + 4 bytes */
373*4882a593Smuzhiyun 	case MMC_RSP_SPI_R3:
374*4882a593Smuzhiyun 		rotator = leftover << 8;
375*4882a593Smuzhiyun 		cmd->resp[1] = 0;
376*4882a593Smuzhiyun 		for (i = 0; i < 4; i++) {
377*4882a593Smuzhiyun 			cmd->resp[1] <<= 8;
378*4882a593Smuzhiyun 			/* read the next byte */
379*4882a593Smuzhiyun 			if (cp == end) {
380*4882a593Smuzhiyun 				value = mmc_spi_readbytes(host, 1);
381*4882a593Smuzhiyun 				if (value < 0)
382*4882a593Smuzhiyun 					goto done;
383*4882a593Smuzhiyun 				cp = host->data->status;
384*4882a593Smuzhiyun 				end = cp+1;
385*4882a593Smuzhiyun 			}
386*4882a593Smuzhiyun 			if (bitshift) {
387*4882a593Smuzhiyun 				rotator |= *cp++ << bitshift;
388*4882a593Smuzhiyun 				cmd->resp[1] |= (rotator >> 8);
389*4882a593Smuzhiyun 				rotator <<= 8;
390*4882a593Smuzhiyun 			} else {
391*4882a593Smuzhiyun 				cmd->resp[1] |= *cp++;
392*4882a593Smuzhiyun 			}
393*4882a593Smuzhiyun 		}
394*4882a593Smuzhiyun 		break;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	/* SPI R1 == just one status byte */
397*4882a593Smuzhiyun 	case MMC_RSP_SPI_R1:
398*4882a593Smuzhiyun 		break;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	default:
401*4882a593Smuzhiyun 		dev_dbg(&host->spi->dev, "bad response type %04x\n",
402*4882a593Smuzhiyun 			mmc_spi_resp_type(cmd));
403*4882a593Smuzhiyun 		if (value >= 0)
404*4882a593Smuzhiyun 			value = -EINVAL;
405*4882a593Smuzhiyun 		goto done;
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	if (value < 0)
409*4882a593Smuzhiyun 		dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
410*4882a593Smuzhiyun 			tag, cmd->resp[0], cmd->resp[1]);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* disable chipselect on errors and some success cases */
413*4882a593Smuzhiyun 	if (value >= 0 && cs_on)
414*4882a593Smuzhiyun 		return value;
415*4882a593Smuzhiyun done:
416*4882a593Smuzhiyun 	if (value < 0)
417*4882a593Smuzhiyun 		cmd->error = value;
418*4882a593Smuzhiyun 	mmc_cs_off(host);
419*4882a593Smuzhiyun 	return value;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /* Issue command and read its response.
423*4882a593Smuzhiyun  * Returns zero on success, negative for error.
424*4882a593Smuzhiyun  *
425*4882a593Smuzhiyun  * On error, caller must cope with mmc core retry mechanism.  That
426*4882a593Smuzhiyun  * means immediate low-level resubmit, which affects the bus lock...
427*4882a593Smuzhiyun  */
428*4882a593Smuzhiyun static int
mmc_spi_command_send(struct mmc_spi_host * host,struct mmc_request * mrq,struct mmc_command * cmd,int cs_on)429*4882a593Smuzhiyun mmc_spi_command_send(struct mmc_spi_host *host,
430*4882a593Smuzhiyun 		struct mmc_request *mrq,
431*4882a593Smuzhiyun 		struct mmc_command *cmd, int cs_on)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun 	struct scratch		*data = host->data;
434*4882a593Smuzhiyun 	u8			*cp = data->status;
435*4882a593Smuzhiyun 	int			status;
436*4882a593Smuzhiyun 	struct spi_transfer	*t;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	/* We can handle most commands (except block reads) in one full
439*4882a593Smuzhiyun 	 * duplex I/O operation before either starting the next transfer
440*4882a593Smuzhiyun 	 * (data block or command) or else deselecting the card.
441*4882a593Smuzhiyun 	 *
442*4882a593Smuzhiyun 	 * First, write 7 bytes:
443*4882a593Smuzhiyun 	 *  - an all-ones byte to ensure the card is ready
444*4882a593Smuzhiyun 	 *  - opcode byte (plus start and transmission bits)
445*4882a593Smuzhiyun 	 *  - four bytes of big-endian argument
446*4882a593Smuzhiyun 	 *  - crc7 (plus end bit) ... always computed, it's cheap
447*4882a593Smuzhiyun 	 *
448*4882a593Smuzhiyun 	 * We init the whole buffer to all-ones, which is what we need
449*4882a593Smuzhiyun 	 * to write while we're reading (later) response data.
450*4882a593Smuzhiyun 	 */
451*4882a593Smuzhiyun 	memset(cp, 0xff, sizeof(data->status));
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	cp[1] = 0x40 | cmd->opcode;
454*4882a593Smuzhiyun 	put_unaligned_be32(cmd->arg, cp + 2);
455*4882a593Smuzhiyun 	cp[6] = crc7_be(0, cp + 1, 5) | 0x01;
456*4882a593Smuzhiyun 	cp += 7;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* Then, read up to 13 bytes (while writing all-ones):
459*4882a593Smuzhiyun 	 *  - N(CR) (== 1..8) bytes of all-ones
460*4882a593Smuzhiyun 	 *  - status byte (for all response types)
461*4882a593Smuzhiyun 	 *  - the rest of the response, either:
462*4882a593Smuzhiyun 	 *      + nothing, for R1 or R1B responses
463*4882a593Smuzhiyun 	 *	+ second status byte, for R2 responses
464*4882a593Smuzhiyun 	 *	+ four data bytes, for R3 and R7 responses
465*4882a593Smuzhiyun 	 *
466*4882a593Smuzhiyun 	 * Finally, read some more bytes ... in the nice cases we know in
467*4882a593Smuzhiyun 	 * advance how many, and reading 1 more is always OK:
468*4882a593Smuzhiyun 	 *  - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
469*4882a593Smuzhiyun 	 *  - N(RC) (== 1..N) bytes of all-ones, before next command
470*4882a593Smuzhiyun 	 *  - N(WR) (== 1..N) bytes of all-ones, before data write
471*4882a593Smuzhiyun 	 *
472*4882a593Smuzhiyun 	 * So in those cases one full duplex I/O of at most 21 bytes will
473*4882a593Smuzhiyun 	 * handle the whole command, leaving the card ready to receive a
474*4882a593Smuzhiyun 	 * data block or new command.  We do that whenever we can, shaving
475*4882a593Smuzhiyun 	 * CPU and IRQ costs (especially when using DMA or FIFOs).
476*4882a593Smuzhiyun 	 *
477*4882a593Smuzhiyun 	 * There are two other cases, where it's not generally practical
478*4882a593Smuzhiyun 	 * to rely on a single I/O:
479*4882a593Smuzhiyun 	 *
480*4882a593Smuzhiyun 	 *  - R1B responses need at least N(EC) bytes of all-zeroes.
481*4882a593Smuzhiyun 	 *
482*4882a593Smuzhiyun 	 *    In this case we can *try* to fit it into one I/O, then
483*4882a593Smuzhiyun 	 *    maybe read more data later.
484*4882a593Smuzhiyun 	 *
485*4882a593Smuzhiyun 	 *  - Data block reads are more troublesome, since a variable
486*4882a593Smuzhiyun 	 *    number of padding bytes precede the token and data.
487*4882a593Smuzhiyun 	 *      + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
488*4882a593Smuzhiyun 	 *      + N(AC) (== 1..many) bytes of all-ones
489*4882a593Smuzhiyun 	 *
490*4882a593Smuzhiyun 	 *    In this case we currently only have minimal speedups here:
491*4882a593Smuzhiyun 	 *    when N(CR) == 1 we can avoid I/O in response_get().
492*4882a593Smuzhiyun 	 */
493*4882a593Smuzhiyun 	if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
494*4882a593Smuzhiyun 		cp += 2;	/* min(N(CR)) + status */
495*4882a593Smuzhiyun 		/* R1 */
496*4882a593Smuzhiyun 	} else {
497*4882a593Smuzhiyun 		cp += 10;	/* max(N(CR)) + status + min(N(RC),N(WR)) */
498*4882a593Smuzhiyun 		if (cmd->flags & MMC_RSP_SPI_S2)	/* R2/R5 */
499*4882a593Smuzhiyun 			cp++;
500*4882a593Smuzhiyun 		else if (cmd->flags & MMC_RSP_SPI_B4)	/* R3/R4/R7 */
501*4882a593Smuzhiyun 			cp += 4;
502*4882a593Smuzhiyun 		else if (cmd->flags & MMC_RSP_BUSY)	/* R1B */
503*4882a593Smuzhiyun 			cp = data->status + sizeof(data->status);
504*4882a593Smuzhiyun 		/* else:  R1 (most commands) */
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	dev_dbg(&host->spi->dev, "  mmc_spi: CMD%d, resp %s\n",
508*4882a593Smuzhiyun 		cmd->opcode, maptype(cmd));
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	/* send command, leaving chipselect active */
511*4882a593Smuzhiyun 	spi_message_init(&host->m);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	t = &host->t;
514*4882a593Smuzhiyun 	memset(t, 0, sizeof(*t));
515*4882a593Smuzhiyun 	t->tx_buf = t->rx_buf = data->status;
516*4882a593Smuzhiyun 	t->tx_dma = t->rx_dma = host->data_dma;
517*4882a593Smuzhiyun 	t->len = cp - data->status;
518*4882a593Smuzhiyun 	t->cs_change = 1;
519*4882a593Smuzhiyun 	spi_message_add_tail(t, &host->m);
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	if (host->dma_dev) {
522*4882a593Smuzhiyun 		host->m.is_dma_mapped = 1;
523*4882a593Smuzhiyun 		dma_sync_single_for_device(host->dma_dev,
524*4882a593Smuzhiyun 				host->data_dma, sizeof(*host->data),
525*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 	status = spi_sync_locked(host->spi, &host->m);
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	if (host->dma_dev)
530*4882a593Smuzhiyun 		dma_sync_single_for_cpu(host->dma_dev,
531*4882a593Smuzhiyun 				host->data_dma, sizeof(*host->data),
532*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
533*4882a593Smuzhiyun 	if (status < 0) {
534*4882a593Smuzhiyun 		dev_dbg(&host->spi->dev, "  ... write returned %d\n", status);
535*4882a593Smuzhiyun 		cmd->error = status;
536*4882a593Smuzhiyun 		return status;
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	/* after no-data commands and STOP_TRANSMISSION, chipselect off */
540*4882a593Smuzhiyun 	return mmc_spi_response_get(host, cmd, cs_on);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /* Build data message with up to four separate transfers.  For TX, we
544*4882a593Smuzhiyun  * start by writing the data token.  And in most cases, we finish with
545*4882a593Smuzhiyun  * a status transfer.
546*4882a593Smuzhiyun  *
547*4882a593Smuzhiyun  * We always provide TX data for data and CRC.  The MMC/SD protocol
548*4882a593Smuzhiyun  * requires us to write ones; but Linux defaults to writing zeroes;
549*4882a593Smuzhiyun  * so we explicitly initialize it to all ones on RX paths.
550*4882a593Smuzhiyun  *
551*4882a593Smuzhiyun  * We also handle DMA mapping, so the underlying SPI controller does
552*4882a593Smuzhiyun  * not need to (re)do it for each message.
553*4882a593Smuzhiyun  */
554*4882a593Smuzhiyun static void
mmc_spi_setup_data_message(struct mmc_spi_host * host,int multiple,enum dma_data_direction direction)555*4882a593Smuzhiyun mmc_spi_setup_data_message(
556*4882a593Smuzhiyun 	struct mmc_spi_host	*host,
557*4882a593Smuzhiyun 	int			multiple,
558*4882a593Smuzhiyun 	enum dma_data_direction	direction)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	struct spi_transfer	*t;
561*4882a593Smuzhiyun 	struct scratch		*scratch = host->data;
562*4882a593Smuzhiyun 	dma_addr_t		dma = host->data_dma;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	spi_message_init(&host->m);
565*4882a593Smuzhiyun 	if (dma)
566*4882a593Smuzhiyun 		host->m.is_dma_mapped = 1;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	/* for reads, readblock() skips 0xff bytes before finding
569*4882a593Smuzhiyun 	 * the token; for writes, this transfer issues that token.
570*4882a593Smuzhiyun 	 */
571*4882a593Smuzhiyun 	if (direction == DMA_TO_DEVICE) {
572*4882a593Smuzhiyun 		t = &host->token;
573*4882a593Smuzhiyun 		memset(t, 0, sizeof(*t));
574*4882a593Smuzhiyun 		t->len = 1;
575*4882a593Smuzhiyun 		if (multiple)
576*4882a593Smuzhiyun 			scratch->data_token = SPI_TOKEN_MULTI_WRITE;
577*4882a593Smuzhiyun 		else
578*4882a593Smuzhiyun 			scratch->data_token = SPI_TOKEN_SINGLE;
579*4882a593Smuzhiyun 		t->tx_buf = &scratch->data_token;
580*4882a593Smuzhiyun 		if (dma)
581*4882a593Smuzhiyun 			t->tx_dma = dma + offsetof(struct scratch, data_token);
582*4882a593Smuzhiyun 		spi_message_add_tail(t, &host->m);
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	/* Body of transfer is buffer, then CRC ...
586*4882a593Smuzhiyun 	 * either TX-only, or RX with TX-ones.
587*4882a593Smuzhiyun 	 */
588*4882a593Smuzhiyun 	t = &host->t;
589*4882a593Smuzhiyun 	memset(t, 0, sizeof(*t));
590*4882a593Smuzhiyun 	t->tx_buf = host->ones;
591*4882a593Smuzhiyun 	t->tx_dma = host->ones_dma;
592*4882a593Smuzhiyun 	/* length and actual buffer info are written later */
593*4882a593Smuzhiyun 	spi_message_add_tail(t, &host->m);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	t = &host->crc;
596*4882a593Smuzhiyun 	memset(t, 0, sizeof(*t));
597*4882a593Smuzhiyun 	t->len = 2;
598*4882a593Smuzhiyun 	if (direction == DMA_TO_DEVICE) {
599*4882a593Smuzhiyun 		/* the actual CRC may get written later */
600*4882a593Smuzhiyun 		t->tx_buf = &scratch->crc_val;
601*4882a593Smuzhiyun 		if (dma)
602*4882a593Smuzhiyun 			t->tx_dma = dma + offsetof(struct scratch, crc_val);
603*4882a593Smuzhiyun 	} else {
604*4882a593Smuzhiyun 		t->tx_buf = host->ones;
605*4882a593Smuzhiyun 		t->tx_dma = host->ones_dma;
606*4882a593Smuzhiyun 		t->rx_buf = &scratch->crc_val;
607*4882a593Smuzhiyun 		if (dma)
608*4882a593Smuzhiyun 			t->rx_dma = dma + offsetof(struct scratch, crc_val);
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun 	spi_message_add_tail(t, &host->m);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	/*
613*4882a593Smuzhiyun 	 * A single block read is followed by N(EC) [0+] all-ones bytes
614*4882a593Smuzhiyun 	 * before deselect ... don't bother.
615*4882a593Smuzhiyun 	 *
616*4882a593Smuzhiyun 	 * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
617*4882a593Smuzhiyun 	 * the next block is read, or a STOP_TRANSMISSION is issued.  We'll
618*4882a593Smuzhiyun 	 * collect that single byte, so readblock() doesn't need to.
619*4882a593Smuzhiyun 	 *
620*4882a593Smuzhiyun 	 * For a write, the one-byte data response follows immediately, then
621*4882a593Smuzhiyun 	 * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
622*4882a593Smuzhiyun 	 * Then single block reads may deselect, and multiblock ones issue
623*4882a593Smuzhiyun 	 * the next token (next data block, or STOP_TRAN).  We can try to
624*4882a593Smuzhiyun 	 * minimize I/O ops by using a single read to collect end-of-busy.
625*4882a593Smuzhiyun 	 */
626*4882a593Smuzhiyun 	if (multiple || direction == DMA_TO_DEVICE) {
627*4882a593Smuzhiyun 		t = &host->early_status;
628*4882a593Smuzhiyun 		memset(t, 0, sizeof(*t));
629*4882a593Smuzhiyun 		t->len = (direction == DMA_TO_DEVICE) ? sizeof(scratch->status) : 1;
630*4882a593Smuzhiyun 		t->tx_buf = host->ones;
631*4882a593Smuzhiyun 		t->tx_dma = host->ones_dma;
632*4882a593Smuzhiyun 		t->rx_buf = scratch->status;
633*4882a593Smuzhiyun 		if (dma)
634*4882a593Smuzhiyun 			t->rx_dma = dma + offsetof(struct scratch, status);
635*4882a593Smuzhiyun 		t->cs_change = 1;
636*4882a593Smuzhiyun 		spi_message_add_tail(t, &host->m);
637*4882a593Smuzhiyun 	}
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun /*
641*4882a593Smuzhiyun  * Write one block:
642*4882a593Smuzhiyun  *  - caller handled preceding N(WR) [1+] all-ones bytes
643*4882a593Smuzhiyun  *  - data block
644*4882a593Smuzhiyun  *	+ token
645*4882a593Smuzhiyun  *	+ data bytes
646*4882a593Smuzhiyun  *	+ crc16
647*4882a593Smuzhiyun  *  - an all-ones byte ... card writes a data-response byte
648*4882a593Smuzhiyun  *  - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
649*4882a593Smuzhiyun  *
650*4882a593Smuzhiyun  * Return negative errno, else success.
651*4882a593Smuzhiyun  */
652*4882a593Smuzhiyun static int
mmc_spi_writeblock(struct mmc_spi_host * host,struct spi_transfer * t,unsigned long timeout)653*4882a593Smuzhiyun mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t,
654*4882a593Smuzhiyun 	unsigned long timeout)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	struct spi_device	*spi = host->spi;
657*4882a593Smuzhiyun 	int			status, i;
658*4882a593Smuzhiyun 	struct scratch		*scratch = host->data;
659*4882a593Smuzhiyun 	u32			pattern;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	if (host->mmc->use_spi_crc)
662*4882a593Smuzhiyun 		scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len));
663*4882a593Smuzhiyun 	if (host->dma_dev)
664*4882a593Smuzhiyun 		dma_sync_single_for_device(host->dma_dev,
665*4882a593Smuzhiyun 				host->data_dma, sizeof(*scratch),
666*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	status = spi_sync_locked(spi, &host->m);
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	if (status != 0) {
671*4882a593Smuzhiyun 		dev_dbg(&spi->dev, "write error (%d)\n", status);
672*4882a593Smuzhiyun 		return status;
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	if (host->dma_dev)
676*4882a593Smuzhiyun 		dma_sync_single_for_cpu(host->dma_dev,
677*4882a593Smuzhiyun 				host->data_dma, sizeof(*scratch),
678*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	/*
681*4882a593Smuzhiyun 	 * Get the transmission data-response reply.  It must follow
682*4882a593Smuzhiyun 	 * immediately after the data block we transferred.  This reply
683*4882a593Smuzhiyun 	 * doesn't necessarily tell whether the write operation succeeded;
684*4882a593Smuzhiyun 	 * it just says if the transmission was ok and whether *earlier*
685*4882a593Smuzhiyun 	 * writes succeeded; see the standard.
686*4882a593Smuzhiyun 	 *
687*4882a593Smuzhiyun 	 * In practice, there are (even modern SDHC-)cards which are late
688*4882a593Smuzhiyun 	 * in sending the response, and miss the time frame by a few bits,
689*4882a593Smuzhiyun 	 * so we have to cope with this situation and check the response
690*4882a593Smuzhiyun 	 * bit-by-bit. Arggh!!!
691*4882a593Smuzhiyun 	 */
692*4882a593Smuzhiyun 	pattern = get_unaligned_be32(scratch->status);
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	/* First 3 bit of pattern are undefined */
695*4882a593Smuzhiyun 	pattern |= 0xE0000000;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	/* left-adjust to leading 0 bit */
698*4882a593Smuzhiyun 	while (pattern & 0x80000000)
699*4882a593Smuzhiyun 		pattern <<= 1;
700*4882a593Smuzhiyun 	/* right-adjust for pattern matching. Code is in bit 4..0 now. */
701*4882a593Smuzhiyun 	pattern >>= 27;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	switch (pattern) {
704*4882a593Smuzhiyun 	case SPI_RESPONSE_ACCEPTED:
705*4882a593Smuzhiyun 		status = 0;
706*4882a593Smuzhiyun 		break;
707*4882a593Smuzhiyun 	case SPI_RESPONSE_CRC_ERR:
708*4882a593Smuzhiyun 		/* host shall then issue MMC_STOP_TRANSMISSION */
709*4882a593Smuzhiyun 		status = -EILSEQ;
710*4882a593Smuzhiyun 		break;
711*4882a593Smuzhiyun 	case SPI_RESPONSE_WRITE_ERR:
712*4882a593Smuzhiyun 		/* host shall then issue MMC_STOP_TRANSMISSION,
713*4882a593Smuzhiyun 		 * and should MMC_SEND_STATUS to sort it out
714*4882a593Smuzhiyun 		 */
715*4882a593Smuzhiyun 		status = -EIO;
716*4882a593Smuzhiyun 		break;
717*4882a593Smuzhiyun 	default:
718*4882a593Smuzhiyun 		status = -EPROTO;
719*4882a593Smuzhiyun 		break;
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun 	if (status != 0) {
722*4882a593Smuzhiyun 		dev_dbg(&spi->dev, "write error %02x (%d)\n",
723*4882a593Smuzhiyun 			scratch->status[0], status);
724*4882a593Smuzhiyun 		return status;
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	t->tx_buf += t->len;
728*4882a593Smuzhiyun 	if (host->dma_dev)
729*4882a593Smuzhiyun 		t->tx_dma += t->len;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	/* Return when not busy.  If we didn't collect that status yet,
732*4882a593Smuzhiyun 	 * we'll need some more I/O.
733*4882a593Smuzhiyun 	 */
734*4882a593Smuzhiyun 	for (i = 4; i < sizeof(scratch->status); i++) {
735*4882a593Smuzhiyun 		/* card is non-busy if the most recent bit is 1 */
736*4882a593Smuzhiyun 		if (scratch->status[i] & 0x01)
737*4882a593Smuzhiyun 			return 0;
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun 	return mmc_spi_wait_unbusy(host, timeout);
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun /*
743*4882a593Smuzhiyun  * Read one block:
744*4882a593Smuzhiyun  *  - skip leading all-ones bytes ... either
745*4882a593Smuzhiyun  *      + N(AC) [1..f(clock,CSD)] usually, else
746*4882a593Smuzhiyun  *      + N(CX) [0..8] when reading CSD or CID
747*4882a593Smuzhiyun  *  - data block
748*4882a593Smuzhiyun  *	+ token ... if error token, no data or crc
749*4882a593Smuzhiyun  *	+ data bytes
750*4882a593Smuzhiyun  *	+ crc16
751*4882a593Smuzhiyun  *
752*4882a593Smuzhiyun  * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
753*4882a593Smuzhiyun  * before dropping chipselect.
754*4882a593Smuzhiyun  *
755*4882a593Smuzhiyun  * For multiblock reads, caller either reads the next block or issues a
756*4882a593Smuzhiyun  * STOP_TRANSMISSION command.
757*4882a593Smuzhiyun  */
758*4882a593Smuzhiyun static int
mmc_spi_readblock(struct mmc_spi_host * host,struct spi_transfer * t,unsigned long timeout)759*4882a593Smuzhiyun mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t,
760*4882a593Smuzhiyun 	unsigned long timeout)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun 	struct spi_device	*spi = host->spi;
763*4882a593Smuzhiyun 	int			status;
764*4882a593Smuzhiyun 	struct scratch		*scratch = host->data;
765*4882a593Smuzhiyun 	unsigned int 		bitshift;
766*4882a593Smuzhiyun 	u8			leftover;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	/* At least one SD card sends an all-zeroes byte when N(CX)
769*4882a593Smuzhiyun 	 * applies, before the all-ones bytes ... just cope with that.
770*4882a593Smuzhiyun 	 */
771*4882a593Smuzhiyun 	status = mmc_spi_readbytes(host, 1);
772*4882a593Smuzhiyun 	if (status < 0)
773*4882a593Smuzhiyun 		return status;
774*4882a593Smuzhiyun 	status = scratch->status[0];
775*4882a593Smuzhiyun 	if (status == 0xff || status == 0)
776*4882a593Smuzhiyun 		status = mmc_spi_readtoken(host, timeout);
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	if (status < 0) {
779*4882a593Smuzhiyun 		dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
780*4882a593Smuzhiyun 		return status;
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* The token may be bit-shifted...
784*4882a593Smuzhiyun 	 * the first 0-bit precedes the data stream.
785*4882a593Smuzhiyun 	 */
786*4882a593Smuzhiyun 	bitshift = 7;
787*4882a593Smuzhiyun 	while (status & 0x80) {
788*4882a593Smuzhiyun 		status <<= 1;
789*4882a593Smuzhiyun 		bitshift--;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 	leftover = status << 1;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	if (host->dma_dev) {
794*4882a593Smuzhiyun 		dma_sync_single_for_device(host->dma_dev,
795*4882a593Smuzhiyun 				host->data_dma, sizeof(*scratch),
796*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
797*4882a593Smuzhiyun 		dma_sync_single_for_device(host->dma_dev,
798*4882a593Smuzhiyun 				t->rx_dma, t->len,
799*4882a593Smuzhiyun 				DMA_FROM_DEVICE);
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	status = spi_sync_locked(spi, &host->m);
803*4882a593Smuzhiyun 	if (status < 0) {
804*4882a593Smuzhiyun 		dev_dbg(&spi->dev, "read error %d\n", status);
805*4882a593Smuzhiyun 		return status;
806*4882a593Smuzhiyun 	}
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	if (host->dma_dev) {
809*4882a593Smuzhiyun 		dma_sync_single_for_cpu(host->dma_dev,
810*4882a593Smuzhiyun 				host->data_dma, sizeof(*scratch),
811*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
812*4882a593Smuzhiyun 		dma_sync_single_for_cpu(host->dma_dev,
813*4882a593Smuzhiyun 				t->rx_dma, t->len,
814*4882a593Smuzhiyun 				DMA_FROM_DEVICE);
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	if (bitshift) {
818*4882a593Smuzhiyun 		/* Walk through the data and the crc and do
819*4882a593Smuzhiyun 		 * all the magic to get byte-aligned data.
820*4882a593Smuzhiyun 		 */
821*4882a593Smuzhiyun 		u8 *cp = t->rx_buf;
822*4882a593Smuzhiyun 		unsigned int len;
823*4882a593Smuzhiyun 		unsigned int bitright = 8 - bitshift;
824*4882a593Smuzhiyun 		u8 temp;
825*4882a593Smuzhiyun 		for (len = t->len; len; len--) {
826*4882a593Smuzhiyun 			temp = *cp;
827*4882a593Smuzhiyun 			*cp++ = leftover | (temp >> bitshift);
828*4882a593Smuzhiyun 			leftover = temp << bitright;
829*4882a593Smuzhiyun 		}
830*4882a593Smuzhiyun 		cp = (u8 *) &scratch->crc_val;
831*4882a593Smuzhiyun 		temp = *cp;
832*4882a593Smuzhiyun 		*cp++ = leftover | (temp >> bitshift);
833*4882a593Smuzhiyun 		leftover = temp << bitright;
834*4882a593Smuzhiyun 		temp = *cp;
835*4882a593Smuzhiyun 		*cp = leftover | (temp >> bitshift);
836*4882a593Smuzhiyun 	}
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	if (host->mmc->use_spi_crc) {
839*4882a593Smuzhiyun 		u16 crc = crc_itu_t(0, t->rx_buf, t->len);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 		be16_to_cpus(&scratch->crc_val);
842*4882a593Smuzhiyun 		if (scratch->crc_val != crc) {
843*4882a593Smuzhiyun 			dev_dbg(&spi->dev,
844*4882a593Smuzhiyun 				"read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n",
845*4882a593Smuzhiyun 				scratch->crc_val, crc, t->len);
846*4882a593Smuzhiyun 			return -EILSEQ;
847*4882a593Smuzhiyun 		}
848*4882a593Smuzhiyun 	}
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	t->rx_buf += t->len;
851*4882a593Smuzhiyun 	if (host->dma_dev)
852*4882a593Smuzhiyun 		t->rx_dma += t->len;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	return 0;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun /*
858*4882a593Smuzhiyun  * An MMC/SD data stage includes one or more blocks, optional CRCs,
859*4882a593Smuzhiyun  * and inline handshaking.  That handhaking makes it unlike most
860*4882a593Smuzhiyun  * other SPI protocol stacks.
861*4882a593Smuzhiyun  */
862*4882a593Smuzhiyun static void
mmc_spi_data_do(struct mmc_spi_host * host,struct mmc_command * cmd,struct mmc_data * data,u32 blk_size)863*4882a593Smuzhiyun mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
864*4882a593Smuzhiyun 		struct mmc_data *data, u32 blk_size)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	struct spi_device	*spi = host->spi;
867*4882a593Smuzhiyun 	struct device		*dma_dev = host->dma_dev;
868*4882a593Smuzhiyun 	struct spi_transfer	*t;
869*4882a593Smuzhiyun 	enum dma_data_direction	direction;
870*4882a593Smuzhiyun 	struct scatterlist	*sg;
871*4882a593Smuzhiyun 	unsigned		n_sg;
872*4882a593Smuzhiyun 	int			multiple = (data->blocks > 1);
873*4882a593Smuzhiyun 	u32			clock_rate;
874*4882a593Smuzhiyun 	unsigned long		timeout;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	direction = mmc_get_dma_dir(data);
877*4882a593Smuzhiyun 	mmc_spi_setup_data_message(host, multiple, direction);
878*4882a593Smuzhiyun 	t = &host->t;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	if (t->speed_hz)
881*4882a593Smuzhiyun 		clock_rate = t->speed_hz;
882*4882a593Smuzhiyun 	else
883*4882a593Smuzhiyun 		clock_rate = spi->max_speed_hz;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	timeout = data->timeout_ns / 1000 +
886*4882a593Smuzhiyun 		  data->timeout_clks * 1000000 / clock_rate;
887*4882a593Smuzhiyun 	timeout = usecs_to_jiffies((unsigned int)timeout) + 1;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/* Handle scatterlist segments one at a time, with synch for
890*4882a593Smuzhiyun 	 * each 512-byte block
891*4882a593Smuzhiyun 	 */
892*4882a593Smuzhiyun 	for_each_sg(data->sg, sg, data->sg_len, n_sg) {
893*4882a593Smuzhiyun 		int			status = 0;
894*4882a593Smuzhiyun 		dma_addr_t		dma_addr = 0;
895*4882a593Smuzhiyun 		void			*kmap_addr;
896*4882a593Smuzhiyun 		unsigned		length = sg->length;
897*4882a593Smuzhiyun 		enum dma_data_direction	dir = direction;
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 		/* set up dma mapping for controller drivers that might
900*4882a593Smuzhiyun 		 * use DMA ... though they may fall back to PIO
901*4882a593Smuzhiyun 		 */
902*4882a593Smuzhiyun 		if (dma_dev) {
903*4882a593Smuzhiyun 			/* never invalidate whole *shared* pages ... */
904*4882a593Smuzhiyun 			if ((sg->offset != 0 || length != PAGE_SIZE)
905*4882a593Smuzhiyun 					&& dir == DMA_FROM_DEVICE)
906*4882a593Smuzhiyun 				dir = DMA_BIDIRECTIONAL;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 			dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
909*4882a593Smuzhiyun 						PAGE_SIZE, dir);
910*4882a593Smuzhiyun 			if (dma_mapping_error(dma_dev, dma_addr)) {
911*4882a593Smuzhiyun 				data->error = -EFAULT;
912*4882a593Smuzhiyun 				break;
913*4882a593Smuzhiyun 			}
914*4882a593Smuzhiyun 			if (direction == DMA_TO_DEVICE)
915*4882a593Smuzhiyun 				t->tx_dma = dma_addr + sg->offset;
916*4882a593Smuzhiyun 			else
917*4882a593Smuzhiyun 				t->rx_dma = dma_addr + sg->offset;
918*4882a593Smuzhiyun 		}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 		/* allow pio too; we don't allow highmem */
921*4882a593Smuzhiyun 		kmap_addr = kmap(sg_page(sg));
922*4882a593Smuzhiyun 		if (direction == DMA_TO_DEVICE)
923*4882a593Smuzhiyun 			t->tx_buf = kmap_addr + sg->offset;
924*4882a593Smuzhiyun 		else
925*4882a593Smuzhiyun 			t->rx_buf = kmap_addr + sg->offset;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 		/* transfer each block, and update request status */
928*4882a593Smuzhiyun 		while (length) {
929*4882a593Smuzhiyun 			t->len = min(length, blk_size);
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 			dev_dbg(&host->spi->dev,
932*4882a593Smuzhiyun 				"    mmc_spi: %s block, %d bytes\n",
933*4882a593Smuzhiyun 				(direction == DMA_TO_DEVICE) ? "write" : "read",
934*4882a593Smuzhiyun 				t->len);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 			if (direction == DMA_TO_DEVICE)
937*4882a593Smuzhiyun 				status = mmc_spi_writeblock(host, t, timeout);
938*4882a593Smuzhiyun 			else
939*4882a593Smuzhiyun 				status = mmc_spi_readblock(host, t, timeout);
940*4882a593Smuzhiyun 			if (status < 0)
941*4882a593Smuzhiyun 				break;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 			data->bytes_xfered += t->len;
944*4882a593Smuzhiyun 			length -= t->len;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 			if (!multiple)
947*4882a593Smuzhiyun 				break;
948*4882a593Smuzhiyun 		}
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 		/* discard mappings */
951*4882a593Smuzhiyun 		if (direction == DMA_FROM_DEVICE)
952*4882a593Smuzhiyun 			flush_kernel_dcache_page(sg_page(sg));
953*4882a593Smuzhiyun 		kunmap(sg_page(sg));
954*4882a593Smuzhiyun 		if (dma_dev)
955*4882a593Smuzhiyun 			dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 		if (status < 0) {
958*4882a593Smuzhiyun 			data->error = status;
959*4882a593Smuzhiyun 			dev_dbg(&spi->dev, "%s status %d\n",
960*4882a593Smuzhiyun 				(direction == DMA_TO_DEVICE) ? "write" : "read",
961*4882a593Smuzhiyun 				status);
962*4882a593Smuzhiyun 			break;
963*4882a593Smuzhiyun 		}
964*4882a593Smuzhiyun 	}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	/* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
967*4882a593Smuzhiyun 	 * can be issued before multiblock writes.  Unlike its more widely
968*4882a593Smuzhiyun 	 * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
969*4882a593Smuzhiyun 	 * that can affect the STOP_TRAN logic.   Complete (and current)
970*4882a593Smuzhiyun 	 * MMC specs should sort that out before Linux starts using CMD23.
971*4882a593Smuzhiyun 	 */
972*4882a593Smuzhiyun 	if (direction == DMA_TO_DEVICE && multiple) {
973*4882a593Smuzhiyun 		struct scratch	*scratch = host->data;
974*4882a593Smuzhiyun 		int		tmp;
975*4882a593Smuzhiyun 		const unsigned	statlen = sizeof(scratch->status);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 		dev_dbg(&spi->dev, "    mmc_spi: STOP_TRAN\n");
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 		/* Tweak the per-block message we set up earlier by morphing
980*4882a593Smuzhiyun 		 * it to hold single buffer with the token followed by some
981*4882a593Smuzhiyun 		 * all-ones bytes ... skip N(BR) (0..1), scan the rest for
982*4882a593Smuzhiyun 		 * "not busy any longer" status, and leave chip selected.
983*4882a593Smuzhiyun 		 */
984*4882a593Smuzhiyun 		INIT_LIST_HEAD(&host->m.transfers);
985*4882a593Smuzhiyun 		list_add(&host->early_status.transfer_list,
986*4882a593Smuzhiyun 				&host->m.transfers);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 		memset(scratch->status, 0xff, statlen);
989*4882a593Smuzhiyun 		scratch->status[0] = SPI_TOKEN_STOP_TRAN;
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 		host->early_status.tx_buf = host->early_status.rx_buf;
992*4882a593Smuzhiyun 		host->early_status.tx_dma = host->early_status.rx_dma;
993*4882a593Smuzhiyun 		host->early_status.len = statlen;
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 		if (host->dma_dev)
996*4882a593Smuzhiyun 			dma_sync_single_for_device(host->dma_dev,
997*4882a593Smuzhiyun 					host->data_dma, sizeof(*scratch),
998*4882a593Smuzhiyun 					DMA_BIDIRECTIONAL);
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 		tmp = spi_sync_locked(spi, &host->m);
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 		if (host->dma_dev)
1003*4882a593Smuzhiyun 			dma_sync_single_for_cpu(host->dma_dev,
1004*4882a593Smuzhiyun 					host->data_dma, sizeof(*scratch),
1005*4882a593Smuzhiyun 					DMA_BIDIRECTIONAL);
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 		if (tmp < 0) {
1008*4882a593Smuzhiyun 			if (!data->error)
1009*4882a593Smuzhiyun 				data->error = tmp;
1010*4882a593Smuzhiyun 			return;
1011*4882a593Smuzhiyun 		}
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 		/* Ideally we collected "not busy" status with one I/O,
1014*4882a593Smuzhiyun 		 * avoiding wasteful byte-at-a-time scanning... but more
1015*4882a593Smuzhiyun 		 * I/O is often needed.
1016*4882a593Smuzhiyun 		 */
1017*4882a593Smuzhiyun 		for (tmp = 2; tmp < statlen; tmp++) {
1018*4882a593Smuzhiyun 			if (scratch->status[tmp] != 0)
1019*4882a593Smuzhiyun 				return;
1020*4882a593Smuzhiyun 		}
1021*4882a593Smuzhiyun 		tmp = mmc_spi_wait_unbusy(host, timeout);
1022*4882a593Smuzhiyun 		if (tmp < 0 && !data->error)
1023*4882a593Smuzhiyun 			data->error = tmp;
1024*4882a593Smuzhiyun 	}
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun /****************************************************************************/
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun /*
1030*4882a593Smuzhiyun  * MMC driver implementation -- the interface to the MMC stack
1031*4882a593Smuzhiyun  */
1032*4882a593Smuzhiyun 
mmc_spi_request(struct mmc_host * mmc,struct mmc_request * mrq)1033*4882a593Smuzhiyun static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	struct mmc_spi_host	*host = mmc_priv(mmc);
1036*4882a593Smuzhiyun 	int			status = -EINVAL;
1037*4882a593Smuzhiyun 	int			crc_retry = 5;
1038*4882a593Smuzhiyun 	struct mmc_command	stop;
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun #ifdef DEBUG
1041*4882a593Smuzhiyun 	/* MMC core and layered drivers *MUST* issue SPI-aware commands */
1042*4882a593Smuzhiyun 	{
1043*4882a593Smuzhiyun 		struct mmc_command	*cmd;
1044*4882a593Smuzhiyun 		int			invalid = 0;
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 		cmd = mrq->cmd;
1047*4882a593Smuzhiyun 		if (!mmc_spi_resp_type(cmd)) {
1048*4882a593Smuzhiyun 			dev_dbg(&host->spi->dev, "bogus command\n");
1049*4882a593Smuzhiyun 			cmd->error = -EINVAL;
1050*4882a593Smuzhiyun 			invalid = 1;
1051*4882a593Smuzhiyun 		}
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 		cmd = mrq->stop;
1054*4882a593Smuzhiyun 		if (cmd && !mmc_spi_resp_type(cmd)) {
1055*4882a593Smuzhiyun 			dev_dbg(&host->spi->dev, "bogus STOP command\n");
1056*4882a593Smuzhiyun 			cmd->error = -EINVAL;
1057*4882a593Smuzhiyun 			invalid = 1;
1058*4882a593Smuzhiyun 		}
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 		if (invalid) {
1061*4882a593Smuzhiyun 			dump_stack();
1062*4882a593Smuzhiyun 			mmc_request_done(host->mmc, mrq);
1063*4882a593Smuzhiyun 			return;
1064*4882a593Smuzhiyun 		}
1065*4882a593Smuzhiyun 	}
1066*4882a593Smuzhiyun #endif
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	/* request exclusive bus access */
1069*4882a593Smuzhiyun 	spi_bus_lock(host->spi->master);
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun crc_recover:
1072*4882a593Smuzhiyun 	/* issue command; then optionally data and stop */
1073*4882a593Smuzhiyun 	status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
1074*4882a593Smuzhiyun 	if (status == 0 && mrq->data) {
1075*4882a593Smuzhiyun 		mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 		/*
1078*4882a593Smuzhiyun 		 * The SPI bus is not always reliable for large data transfers.
1079*4882a593Smuzhiyun 		 * If an occasional crc error is reported by the SD device with
1080*4882a593Smuzhiyun 		 * data read/write over SPI, it may be recovered by repeating
1081*4882a593Smuzhiyun 		 * the last SD command again. The retry count is set to 5 to
1082*4882a593Smuzhiyun 		 * ensure the driver passes stress tests.
1083*4882a593Smuzhiyun 		 */
1084*4882a593Smuzhiyun 		if (mrq->data->error == -EILSEQ && crc_retry) {
1085*4882a593Smuzhiyun 			stop.opcode = MMC_STOP_TRANSMISSION;
1086*4882a593Smuzhiyun 			stop.arg = 0;
1087*4882a593Smuzhiyun 			stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1088*4882a593Smuzhiyun 			status = mmc_spi_command_send(host, mrq, &stop, 0);
1089*4882a593Smuzhiyun 			crc_retry--;
1090*4882a593Smuzhiyun 			mrq->data->error = 0;
1091*4882a593Smuzhiyun 			goto crc_recover;
1092*4882a593Smuzhiyun 		}
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 		if (mrq->stop)
1095*4882a593Smuzhiyun 			status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
1096*4882a593Smuzhiyun 		else
1097*4882a593Smuzhiyun 			mmc_cs_off(host);
1098*4882a593Smuzhiyun 	}
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	/* release the bus */
1101*4882a593Smuzhiyun 	spi_bus_unlock(host->spi->master);
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	mmc_request_done(host->mmc, mrq);
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun /* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
1107*4882a593Smuzhiyun  *
1108*4882a593Smuzhiyun  * NOTE that here we can't know that the card has just been powered up;
1109*4882a593Smuzhiyun  * not all MMC/SD sockets support power switching.
1110*4882a593Smuzhiyun  *
1111*4882a593Smuzhiyun  * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
1112*4882a593Smuzhiyun  * this doesn't seem to do the right thing at all...
1113*4882a593Smuzhiyun  */
mmc_spi_initsequence(struct mmc_spi_host * host)1114*4882a593Smuzhiyun static void mmc_spi_initsequence(struct mmc_spi_host *host)
1115*4882a593Smuzhiyun {
1116*4882a593Smuzhiyun 	/* Try to be very sure any previous command has completed;
1117*4882a593Smuzhiyun 	 * wait till not-busy, skip debris from any old commands.
1118*4882a593Smuzhiyun 	 */
1119*4882a593Smuzhiyun 	mmc_spi_wait_unbusy(host, msecs_to_jiffies(MMC_SPI_INIT_TIMEOUT_MS));
1120*4882a593Smuzhiyun 	mmc_spi_readbytes(host, 10);
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	/*
1123*4882a593Smuzhiyun 	 * Do a burst with chipselect active-high.  We need to do this to
1124*4882a593Smuzhiyun 	 * meet the requirement of 74 clock cycles with both chipselect
1125*4882a593Smuzhiyun 	 * and CMD (MOSI) high before CMD0 ... after the card has been
1126*4882a593Smuzhiyun 	 * powered up to Vdd(min), and so is ready to take commands.
1127*4882a593Smuzhiyun 	 *
1128*4882a593Smuzhiyun 	 * Some cards are particularly needy of this (e.g. Viking "SD256")
1129*4882a593Smuzhiyun 	 * while most others don't seem to care.
1130*4882a593Smuzhiyun 	 *
1131*4882a593Smuzhiyun 	 * Note that this is one of the places MMC/SD plays games with the
1132*4882a593Smuzhiyun 	 * SPI protocol.  Another is that when chipselect is released while
1133*4882a593Smuzhiyun 	 * the card returns BUSY status, the clock must issue several cycles
1134*4882a593Smuzhiyun 	 * with chipselect high before the card will stop driving its output.
1135*4882a593Smuzhiyun 	 *
1136*4882a593Smuzhiyun 	 * SPI_CS_HIGH means "asserted" here. In some cases like when using
1137*4882a593Smuzhiyun 	 * GPIOs for chip select, SPI_CS_HIGH is set but this will be logically
1138*4882a593Smuzhiyun 	 * inverted by gpiolib, so if we want to ascertain to drive it high
1139*4882a593Smuzhiyun 	 * we should toggle the default with an XOR as we do here.
1140*4882a593Smuzhiyun 	 */
1141*4882a593Smuzhiyun 	host->spi->mode ^= SPI_CS_HIGH;
1142*4882a593Smuzhiyun 	if (spi_setup(host->spi) != 0) {
1143*4882a593Smuzhiyun 		/* Just warn; most cards work without it. */
1144*4882a593Smuzhiyun 		dev_warn(&host->spi->dev,
1145*4882a593Smuzhiyun 				"can't change chip-select polarity\n");
1146*4882a593Smuzhiyun 		host->spi->mode ^= SPI_CS_HIGH;
1147*4882a593Smuzhiyun 	} else {
1148*4882a593Smuzhiyun 		mmc_spi_readbytes(host, 18);
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 		host->spi->mode ^= SPI_CS_HIGH;
1151*4882a593Smuzhiyun 		if (spi_setup(host->spi) != 0) {
1152*4882a593Smuzhiyun 			/* Wot, we can't get the same setup we had before? */
1153*4882a593Smuzhiyun 			dev_err(&host->spi->dev,
1154*4882a593Smuzhiyun 					"can't restore chip-select polarity\n");
1155*4882a593Smuzhiyun 		}
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
mmc_powerstring(u8 power_mode)1159*4882a593Smuzhiyun static char *mmc_powerstring(u8 power_mode)
1160*4882a593Smuzhiyun {
1161*4882a593Smuzhiyun 	switch (power_mode) {
1162*4882a593Smuzhiyun 	case MMC_POWER_OFF: return "off";
1163*4882a593Smuzhiyun 	case MMC_POWER_UP:  return "up";
1164*4882a593Smuzhiyun 	case MMC_POWER_ON:  return "on";
1165*4882a593Smuzhiyun 	}
1166*4882a593Smuzhiyun 	return "?";
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun 
mmc_spi_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)1169*4882a593Smuzhiyun static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun 	struct mmc_spi_host *host = mmc_priv(mmc);
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	if (host->power_mode != ios->power_mode) {
1174*4882a593Smuzhiyun 		int		canpower;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 		canpower = host->pdata && host->pdata->setpower;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 		dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1179*4882a593Smuzhiyun 				mmc_powerstring(ios->power_mode),
1180*4882a593Smuzhiyun 				ios->vdd,
1181*4882a593Smuzhiyun 				canpower ? ", can switch" : "");
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 		/* switch power on/off if possible, accounting for
1184*4882a593Smuzhiyun 		 * max 250msec powerup time if needed.
1185*4882a593Smuzhiyun 		 */
1186*4882a593Smuzhiyun 		if (canpower) {
1187*4882a593Smuzhiyun 			switch (ios->power_mode) {
1188*4882a593Smuzhiyun 			case MMC_POWER_OFF:
1189*4882a593Smuzhiyun 			case MMC_POWER_UP:
1190*4882a593Smuzhiyun 				host->pdata->setpower(&host->spi->dev,
1191*4882a593Smuzhiyun 						ios->vdd);
1192*4882a593Smuzhiyun 				if (ios->power_mode == MMC_POWER_UP)
1193*4882a593Smuzhiyun 					msleep(host->powerup_msecs);
1194*4882a593Smuzhiyun 			}
1195*4882a593Smuzhiyun 		}
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 		/* See 6.4.1 in the simplified SD card physical spec 2.0 */
1198*4882a593Smuzhiyun 		if (ios->power_mode == MMC_POWER_ON)
1199*4882a593Smuzhiyun 			mmc_spi_initsequence(host);
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 		/* If powering down, ground all card inputs to avoid power
1202*4882a593Smuzhiyun 		 * delivery from data lines!  On a shared SPI bus, this
1203*4882a593Smuzhiyun 		 * will probably be temporary; 6.4.2 of the simplified SD
1204*4882a593Smuzhiyun 		 * spec says this must last at least 1msec.
1205*4882a593Smuzhiyun 		 *
1206*4882a593Smuzhiyun 		 *   - Clock low means CPOL 0, e.g. mode 0
1207*4882a593Smuzhiyun 		 *   - MOSI low comes from writing zero
1208*4882a593Smuzhiyun 		 *   - Chipselect is usually active low...
1209*4882a593Smuzhiyun 		 */
1210*4882a593Smuzhiyun 		if (canpower && ios->power_mode == MMC_POWER_OFF) {
1211*4882a593Smuzhiyun 			int mres;
1212*4882a593Smuzhiyun 			u8 nullbyte = 0;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 			host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1215*4882a593Smuzhiyun 			mres = spi_setup(host->spi);
1216*4882a593Smuzhiyun 			if (mres < 0)
1217*4882a593Smuzhiyun 				dev_dbg(&host->spi->dev,
1218*4882a593Smuzhiyun 					"switch to SPI mode 0 failed\n");
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 			if (spi_write(host->spi, &nullbyte, 1) < 0)
1221*4882a593Smuzhiyun 				dev_dbg(&host->spi->dev,
1222*4882a593Smuzhiyun 					"put spi signals to low failed\n");
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 			/*
1225*4882a593Smuzhiyun 			 * Now clock should be low due to spi mode 0;
1226*4882a593Smuzhiyun 			 * MOSI should be low because of written 0x00;
1227*4882a593Smuzhiyun 			 * chipselect should be low (it is active low)
1228*4882a593Smuzhiyun 			 * power supply is off, so now MMC is off too!
1229*4882a593Smuzhiyun 			 *
1230*4882a593Smuzhiyun 			 * FIXME no, chipselect can be high since the
1231*4882a593Smuzhiyun 			 * device is inactive and SPI_CS_HIGH is clear...
1232*4882a593Smuzhiyun 			 */
1233*4882a593Smuzhiyun 			msleep(10);
1234*4882a593Smuzhiyun 			if (mres == 0) {
1235*4882a593Smuzhiyun 				host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1236*4882a593Smuzhiyun 				mres = spi_setup(host->spi);
1237*4882a593Smuzhiyun 				if (mres < 0)
1238*4882a593Smuzhiyun 					dev_dbg(&host->spi->dev,
1239*4882a593Smuzhiyun 						"switch back to SPI mode 3 failed\n");
1240*4882a593Smuzhiyun 			}
1241*4882a593Smuzhiyun 		}
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 		host->power_mode = ios->power_mode;
1244*4882a593Smuzhiyun 	}
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1247*4882a593Smuzhiyun 		int		status;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 		host->spi->max_speed_hz = ios->clock;
1250*4882a593Smuzhiyun 		status = spi_setup(host->spi);
1251*4882a593Smuzhiyun 		dev_dbg(&host->spi->dev,
1252*4882a593Smuzhiyun 			"mmc_spi:  clock to %d Hz, %d\n",
1253*4882a593Smuzhiyun 			host->spi->max_speed_hz, status);
1254*4882a593Smuzhiyun 	}
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun static const struct mmc_host_ops mmc_spi_ops = {
1258*4882a593Smuzhiyun 	.request	= mmc_spi_request,
1259*4882a593Smuzhiyun 	.set_ios	= mmc_spi_set_ios,
1260*4882a593Smuzhiyun 	.get_ro		= mmc_gpio_get_ro,
1261*4882a593Smuzhiyun 	.get_cd		= mmc_gpio_get_cd,
1262*4882a593Smuzhiyun };
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun /****************************************************************************/
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun /*
1268*4882a593Smuzhiyun  * SPI driver implementation
1269*4882a593Smuzhiyun  */
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun static irqreturn_t
mmc_spi_detect_irq(int irq,void * mmc)1272*4882a593Smuzhiyun mmc_spi_detect_irq(int irq, void *mmc)
1273*4882a593Smuzhiyun {
1274*4882a593Smuzhiyun 	struct mmc_spi_host *host = mmc_priv(mmc);
1275*4882a593Smuzhiyun 	u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1278*4882a593Smuzhiyun 	return IRQ_HANDLED;
1279*4882a593Smuzhiyun }
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun #ifdef CONFIG_HAS_DMA
mmc_spi_dma_alloc(struct mmc_spi_host * host)1282*4882a593Smuzhiyun static int mmc_spi_dma_alloc(struct mmc_spi_host *host)
1283*4882a593Smuzhiyun {
1284*4882a593Smuzhiyun 	struct spi_device *spi = host->spi;
1285*4882a593Smuzhiyun 	struct device *dev;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	if (!spi->master->dev.parent->dma_mask)
1288*4882a593Smuzhiyun 		return 0;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	dev = spi->master->dev.parent;
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	host->ones_dma = dma_map_single(dev, host->ones, MMC_SPI_BLOCKSIZE,
1293*4882a593Smuzhiyun 					DMA_TO_DEVICE);
1294*4882a593Smuzhiyun 	if (dma_mapping_error(dev, host->ones_dma))
1295*4882a593Smuzhiyun 		return -ENOMEM;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	host->data_dma = dma_map_single(dev, host->data, sizeof(*host->data),
1298*4882a593Smuzhiyun 					DMA_BIDIRECTIONAL);
1299*4882a593Smuzhiyun 	if (dma_mapping_error(dev, host->data_dma)) {
1300*4882a593Smuzhiyun 		dma_unmap_single(dev, host->ones_dma, MMC_SPI_BLOCKSIZE,
1301*4882a593Smuzhiyun 				 DMA_TO_DEVICE);
1302*4882a593Smuzhiyun 		return -ENOMEM;
1303*4882a593Smuzhiyun 	}
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	dma_sync_single_for_cpu(dev, host->data_dma, sizeof(*host->data),
1306*4882a593Smuzhiyun 				DMA_BIDIRECTIONAL);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	host->dma_dev = dev;
1309*4882a593Smuzhiyun 	return 0;
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun 
mmc_spi_dma_free(struct mmc_spi_host * host)1312*4882a593Smuzhiyun static void mmc_spi_dma_free(struct mmc_spi_host *host)
1313*4882a593Smuzhiyun {
1314*4882a593Smuzhiyun 	if (!host->dma_dev)
1315*4882a593Smuzhiyun 		return;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	dma_unmap_single(host->dma_dev, host->ones_dma, MMC_SPI_BLOCKSIZE,
1318*4882a593Smuzhiyun 			 DMA_TO_DEVICE);
1319*4882a593Smuzhiyun 	dma_unmap_single(host->dma_dev, host->data_dma,	sizeof(*host->data),
1320*4882a593Smuzhiyun 			 DMA_BIDIRECTIONAL);
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun #else
mmc_spi_dma_alloc(struct mmc_spi_host * host)1323*4882a593Smuzhiyun static inline int mmc_spi_dma_alloc(struct mmc_spi_host *host) { return 0; }
mmc_spi_dma_free(struct mmc_spi_host * host)1324*4882a593Smuzhiyun static inline void mmc_spi_dma_free(struct mmc_spi_host *host) {}
1325*4882a593Smuzhiyun #endif
1326*4882a593Smuzhiyun 
mmc_spi_probe(struct spi_device * spi)1327*4882a593Smuzhiyun static int mmc_spi_probe(struct spi_device *spi)
1328*4882a593Smuzhiyun {
1329*4882a593Smuzhiyun 	void			*ones;
1330*4882a593Smuzhiyun 	struct mmc_host		*mmc;
1331*4882a593Smuzhiyun 	struct mmc_spi_host	*host;
1332*4882a593Smuzhiyun 	int			status;
1333*4882a593Smuzhiyun 	bool			has_ro = false;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	/* We rely on full duplex transfers, mostly to reduce
1336*4882a593Smuzhiyun 	 * per-transfer overheads (by making fewer transfers).
1337*4882a593Smuzhiyun 	 */
1338*4882a593Smuzhiyun 	if (spi->master->flags & SPI_MASTER_HALF_DUPLEX)
1339*4882a593Smuzhiyun 		return -EINVAL;
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	/* MMC and SD specs only seem to care that sampling is on the
1342*4882a593Smuzhiyun 	 * rising edge ... meaning SPI modes 0 or 3.  So either SPI mode
1343*4882a593Smuzhiyun 	 * should be legit.  We'll use mode 0 since the steady state is 0,
1344*4882a593Smuzhiyun 	 * which is appropriate for hotplugging, unless the platform data
1345*4882a593Smuzhiyun 	 * specify mode 3 (if hardware is not compatible to mode 0).
1346*4882a593Smuzhiyun 	 */
1347*4882a593Smuzhiyun 	if (spi->mode != SPI_MODE_3)
1348*4882a593Smuzhiyun 		spi->mode = SPI_MODE_0;
1349*4882a593Smuzhiyun 	spi->bits_per_word = 8;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	status = spi_setup(spi);
1352*4882a593Smuzhiyun 	if (status < 0) {
1353*4882a593Smuzhiyun 		dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1354*4882a593Smuzhiyun 				spi->mode, spi->max_speed_hz / 1000,
1355*4882a593Smuzhiyun 				status);
1356*4882a593Smuzhiyun 		return status;
1357*4882a593Smuzhiyun 	}
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	/* We need a supply of ones to transmit.  This is the only time
1360*4882a593Smuzhiyun 	 * the CPU touches these, so cache coherency isn't a concern.
1361*4882a593Smuzhiyun 	 *
1362*4882a593Smuzhiyun 	 * NOTE if many systems use more than one MMC-over-SPI connector
1363*4882a593Smuzhiyun 	 * it'd save some memory to share this.  That's evidently rare.
1364*4882a593Smuzhiyun 	 */
1365*4882a593Smuzhiyun 	status = -ENOMEM;
1366*4882a593Smuzhiyun 	ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1367*4882a593Smuzhiyun 	if (!ones)
1368*4882a593Smuzhiyun 		goto nomem;
1369*4882a593Smuzhiyun 	memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 	mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1372*4882a593Smuzhiyun 	if (!mmc)
1373*4882a593Smuzhiyun 		goto nomem;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	mmc->ops = &mmc_spi_ops;
1376*4882a593Smuzhiyun 	mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1377*4882a593Smuzhiyun 	mmc->max_segs = MMC_SPI_BLOCKSATONCE;
1378*4882a593Smuzhiyun 	mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE;
1379*4882a593Smuzhiyun 	mmc->max_blk_count = MMC_SPI_BLOCKSATONCE;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	mmc->caps = MMC_CAP_SPI;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	/* SPI doesn't need the lowspeed device identification thing for
1384*4882a593Smuzhiyun 	 * MMC or SD cards, since it never comes up in open drain mode.
1385*4882a593Smuzhiyun 	 * That's good; some SPI masters can't handle very low speeds!
1386*4882a593Smuzhiyun 	 *
1387*4882a593Smuzhiyun 	 * However, low speed SDIO cards need not handle over 400 KHz;
1388*4882a593Smuzhiyun 	 * that's the only reason not to use a few MHz for f_min (until
1389*4882a593Smuzhiyun 	 * the upper layer reads the target frequency from the CSD).
1390*4882a593Smuzhiyun 	 */
1391*4882a593Smuzhiyun 	mmc->f_min = 400000;
1392*4882a593Smuzhiyun 	mmc->f_max = spi->max_speed_hz;
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	host = mmc_priv(mmc);
1395*4882a593Smuzhiyun 	host->mmc = mmc;
1396*4882a593Smuzhiyun 	host->spi = spi;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	host->ones = ones;
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	/* Platform data is used to hook up things like card sensing
1401*4882a593Smuzhiyun 	 * and power switching gpios.
1402*4882a593Smuzhiyun 	 */
1403*4882a593Smuzhiyun 	host->pdata = mmc_spi_get_pdata(spi);
1404*4882a593Smuzhiyun 	if (host->pdata)
1405*4882a593Smuzhiyun 		mmc->ocr_avail = host->pdata->ocr_mask;
1406*4882a593Smuzhiyun 	if (!mmc->ocr_avail) {
1407*4882a593Smuzhiyun 		dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
1408*4882a593Smuzhiyun 		mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1409*4882a593Smuzhiyun 	}
1410*4882a593Smuzhiyun 	if (host->pdata && host->pdata->setpower) {
1411*4882a593Smuzhiyun 		host->powerup_msecs = host->pdata->powerup_msecs;
1412*4882a593Smuzhiyun 		if (!host->powerup_msecs || host->powerup_msecs > 250)
1413*4882a593Smuzhiyun 			host->powerup_msecs = 250;
1414*4882a593Smuzhiyun 	}
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	dev_set_drvdata(&spi->dev, mmc);
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	/* preallocate dma buffers */
1419*4882a593Smuzhiyun 	host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1420*4882a593Smuzhiyun 	if (!host->data)
1421*4882a593Smuzhiyun 		goto fail_nobuf1;
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	status = mmc_spi_dma_alloc(host);
1424*4882a593Smuzhiyun 	if (status)
1425*4882a593Smuzhiyun 		goto fail_dma;
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 	/* setup message for status/busy readback */
1428*4882a593Smuzhiyun 	spi_message_init(&host->readback);
1429*4882a593Smuzhiyun 	host->readback.is_dma_mapped = (host->dma_dev != NULL);
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	spi_message_add_tail(&host->status, &host->readback);
1432*4882a593Smuzhiyun 	host->status.tx_buf = host->ones;
1433*4882a593Smuzhiyun 	host->status.tx_dma = host->ones_dma;
1434*4882a593Smuzhiyun 	host->status.rx_buf = &host->data->status;
1435*4882a593Smuzhiyun 	host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
1436*4882a593Smuzhiyun 	host->status.cs_change = 1;
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	/* register card detect irq */
1439*4882a593Smuzhiyun 	if (host->pdata && host->pdata->init) {
1440*4882a593Smuzhiyun 		status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1441*4882a593Smuzhiyun 		if (status != 0)
1442*4882a593Smuzhiyun 			goto fail_glue_init;
1443*4882a593Smuzhiyun 	}
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	/* pass platform capabilities, if any */
1446*4882a593Smuzhiyun 	if (host->pdata) {
1447*4882a593Smuzhiyun 		mmc->caps |= host->pdata->caps;
1448*4882a593Smuzhiyun 		mmc->caps2 |= host->pdata->caps2;
1449*4882a593Smuzhiyun 	}
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 	status = mmc_add_host(mmc);
1452*4882a593Smuzhiyun 	if (status != 0)
1453*4882a593Smuzhiyun 		goto fail_add_host;
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	/*
1456*4882a593Smuzhiyun 	 * Index 0 is card detect
1457*4882a593Smuzhiyun 	 * Old boardfiles were specifying 1 ms as debounce
1458*4882a593Smuzhiyun 	 */
1459*4882a593Smuzhiyun 	status = mmc_gpiod_request_cd(mmc, NULL, 0, false, 1000);
1460*4882a593Smuzhiyun 	if (status == -EPROBE_DEFER)
1461*4882a593Smuzhiyun 		goto fail_add_host;
1462*4882a593Smuzhiyun 	if (!status) {
1463*4882a593Smuzhiyun 		/*
1464*4882a593Smuzhiyun 		 * The platform has a CD GPIO signal that may support
1465*4882a593Smuzhiyun 		 * interrupts, so let mmc_gpiod_request_cd_irq() decide
1466*4882a593Smuzhiyun 		 * if polling is needed or not.
1467*4882a593Smuzhiyun 		 */
1468*4882a593Smuzhiyun 		mmc->caps &= ~MMC_CAP_NEEDS_POLL;
1469*4882a593Smuzhiyun 		mmc_gpiod_request_cd_irq(mmc);
1470*4882a593Smuzhiyun 	}
1471*4882a593Smuzhiyun 	mmc_detect_change(mmc, 0);
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 	/* Index 1 is write protect/read only */
1474*4882a593Smuzhiyun 	status = mmc_gpiod_request_ro(mmc, NULL, 1, 0);
1475*4882a593Smuzhiyun 	if (status == -EPROBE_DEFER)
1476*4882a593Smuzhiyun 		goto fail_add_host;
1477*4882a593Smuzhiyun 	if (!status)
1478*4882a593Smuzhiyun 		has_ro = true;
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",
1481*4882a593Smuzhiyun 			dev_name(&mmc->class_dev),
1482*4882a593Smuzhiyun 			host->dma_dev ? "" : ", no DMA",
1483*4882a593Smuzhiyun 			has_ro ? "" : ", no WP",
1484*4882a593Smuzhiyun 			(host->pdata && host->pdata->setpower)
1485*4882a593Smuzhiyun 				? "" : ", no poweroff",
1486*4882a593Smuzhiyun 			(mmc->caps & MMC_CAP_NEEDS_POLL)
1487*4882a593Smuzhiyun 				? ", cd polling" : "");
1488*4882a593Smuzhiyun 	return 0;
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun fail_add_host:
1491*4882a593Smuzhiyun 	mmc_remove_host(mmc);
1492*4882a593Smuzhiyun fail_glue_init:
1493*4882a593Smuzhiyun 	mmc_spi_dma_free(host);
1494*4882a593Smuzhiyun fail_dma:
1495*4882a593Smuzhiyun 	kfree(host->data);
1496*4882a593Smuzhiyun fail_nobuf1:
1497*4882a593Smuzhiyun 	mmc_free_host(mmc);
1498*4882a593Smuzhiyun 	mmc_spi_put_pdata(spi);
1499*4882a593Smuzhiyun nomem:
1500*4882a593Smuzhiyun 	kfree(ones);
1501*4882a593Smuzhiyun 	return status;
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 
mmc_spi_remove(struct spi_device * spi)1505*4882a593Smuzhiyun static int mmc_spi_remove(struct spi_device *spi)
1506*4882a593Smuzhiyun {
1507*4882a593Smuzhiyun 	struct mmc_host		*mmc = dev_get_drvdata(&spi->dev);
1508*4882a593Smuzhiyun 	struct mmc_spi_host	*host = mmc_priv(mmc);
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	/* prevent new mmc_detect_change() calls */
1511*4882a593Smuzhiyun 	if (host->pdata && host->pdata->exit)
1512*4882a593Smuzhiyun 		host->pdata->exit(&spi->dev, mmc);
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	mmc_remove_host(mmc);
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	mmc_spi_dma_free(host);
1517*4882a593Smuzhiyun 	kfree(host->data);
1518*4882a593Smuzhiyun 	kfree(host->ones);
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	spi->max_speed_hz = mmc->f_max;
1521*4882a593Smuzhiyun 	mmc_free_host(mmc);
1522*4882a593Smuzhiyun 	mmc_spi_put_pdata(spi);
1523*4882a593Smuzhiyun 	return 0;
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun static const struct of_device_id mmc_spi_of_match_table[] = {
1527*4882a593Smuzhiyun 	{ .compatible = "mmc-spi-slot", },
1528*4882a593Smuzhiyun 	{},
1529*4882a593Smuzhiyun };
1530*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mmc_spi_of_match_table);
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun static struct spi_driver mmc_spi_driver = {
1533*4882a593Smuzhiyun 	.driver = {
1534*4882a593Smuzhiyun 		.name =		"mmc_spi",
1535*4882a593Smuzhiyun 		.of_match_table = mmc_spi_of_match_table,
1536*4882a593Smuzhiyun 	},
1537*4882a593Smuzhiyun 	.probe =	mmc_spi_probe,
1538*4882a593Smuzhiyun 	.remove =	mmc_spi_remove,
1539*4882a593Smuzhiyun };
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun module_spi_driver(mmc_spi_driver);
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");
1544*4882a593Smuzhiyun MODULE_DESCRIPTION("SPI SD/MMC host driver");
1545*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1546*4882a593Smuzhiyun MODULE_ALIAS("spi:mmc_spi");
1547