xref: /openbmc/linux/drivers/mmc/core/sdio_ops.c (revision 791d3ef2)
1 /*
2  *  linux/drivers/mmc/sdio_ops.c
3  *
4  *  Copyright 2006-2007 Pierre Ossman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  */
11 
12 #include <linux/scatterlist.h>
13 
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/mmc.h>
17 #include <linux/mmc/sdio.h>
18 
19 #include "core.h"
20 #include "sdio_ops.h"
21 
22 int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
23 {
24 	struct mmc_command cmd = {};
25 	int i, err = 0;
26 
27 	cmd.opcode = SD_IO_SEND_OP_COND;
28 	cmd.arg = ocr;
29 	cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
30 
31 	for (i = 100; i; i--) {
32 		err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
33 		if (err)
34 			break;
35 
36 		/* if we're just probing, do a single pass */
37 		if (ocr == 0)
38 			break;
39 
40 		/* otherwise wait until reset completes */
41 		if (mmc_host_is_spi(host)) {
42 			/*
43 			 * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
44 			 * an initialized card under SPI, but some cards
45 			 * (Marvell's) only behave when looking at this
46 			 * one.
47 			 */
48 			if (cmd.resp[1] & MMC_CARD_BUSY)
49 				break;
50 		} else {
51 			if (cmd.resp[0] & MMC_CARD_BUSY)
52 				break;
53 		}
54 
55 		err = -ETIMEDOUT;
56 
57 		mmc_delay(10);
58 	}
59 
60 	if (rocr)
61 		*rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
62 
63 	return err;
64 }
65 
66 static int mmc_io_rw_direct_host(struct mmc_host *host, int write, unsigned fn,
67 	unsigned addr, u8 in, u8 *out)
68 {
69 	struct mmc_command cmd = {};
70 	int err;
71 
72 	if (fn > 7)
73 		return -EINVAL;
74 
75 	/* sanity check */
76 	if (addr & ~0x1FFFF)
77 		return -EINVAL;
78 
79 	cmd.opcode = SD_IO_RW_DIRECT;
80 	cmd.arg = write ? 0x80000000 : 0x00000000;
81 	cmd.arg |= fn << 28;
82 	cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
83 	cmd.arg |= addr << 9;
84 	cmd.arg |= in;
85 	cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
86 
87 	err = mmc_wait_for_cmd(host, &cmd, 0);
88 	if (err)
89 		return err;
90 
91 	if (mmc_host_is_spi(host)) {
92 		/* host driver already reported errors */
93 	} else {
94 		if (cmd.resp[0] & R5_ERROR)
95 			return -EIO;
96 		if (cmd.resp[0] & R5_FUNCTION_NUMBER)
97 			return -EINVAL;
98 		if (cmd.resp[0] & R5_OUT_OF_RANGE)
99 			return -ERANGE;
100 	}
101 
102 	if (out) {
103 		if (mmc_host_is_spi(host))
104 			*out = (cmd.resp[0] >> 8) & 0xFF;
105 		else
106 			*out = cmd.resp[0] & 0xFF;
107 	}
108 
109 	return 0;
110 }
111 
112 int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
113 	unsigned addr, u8 in, u8 *out)
114 {
115 	return mmc_io_rw_direct_host(card->host, write, fn, addr, in, out);
116 }
117 
118 int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
119 	unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
120 {
121 	struct mmc_request mrq = {};
122 	struct mmc_command cmd = {};
123 	struct mmc_data data = {};
124 	struct scatterlist sg, *sg_ptr;
125 	struct sg_table sgtable;
126 	unsigned int nents, left_size, i;
127 	unsigned int seg_size = card->host->max_seg_size;
128 
129 	WARN_ON(blksz == 0);
130 
131 	/* sanity check */
132 	if (addr & ~0x1FFFF)
133 		return -EINVAL;
134 
135 	mrq.cmd = &cmd;
136 	mrq.data = &data;
137 
138 	cmd.opcode = SD_IO_RW_EXTENDED;
139 	cmd.arg = write ? 0x80000000 : 0x00000000;
140 	cmd.arg |= fn << 28;
141 	cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
142 	cmd.arg |= addr << 9;
143 	if (blocks == 0)
144 		cmd.arg |= (blksz == 512) ? 0 : blksz;	/* byte mode */
145 	else
146 		cmd.arg |= 0x08000000 | blocks;		/* block mode */
147 	cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
148 
149 	data.blksz = blksz;
150 	/* Code in host drivers/fwk assumes that "blocks" always is >=1 */
151 	data.blocks = blocks ? blocks : 1;
152 	data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
153 
154 	left_size = data.blksz * data.blocks;
155 	nents = DIV_ROUND_UP(left_size, seg_size);
156 	if (nents > 1) {
157 		if (sg_alloc_table(&sgtable, nents, GFP_KERNEL))
158 			return -ENOMEM;
159 
160 		data.sg = sgtable.sgl;
161 		data.sg_len = nents;
162 
163 		for_each_sg(data.sg, sg_ptr, data.sg_len, i) {
164 			sg_set_buf(sg_ptr, buf + i * seg_size,
165 				   min(seg_size, left_size));
166 			left_size -= seg_size;
167 		}
168 	} else {
169 		data.sg = &sg;
170 		data.sg_len = 1;
171 
172 		sg_init_one(&sg, buf, left_size);
173 	}
174 
175 	mmc_set_data_timeout(&data, card);
176 
177 	mmc_wait_for_req(card->host, &mrq);
178 
179 	if (nents > 1)
180 		sg_free_table(&sgtable);
181 
182 	if (cmd.error)
183 		return cmd.error;
184 	if (data.error)
185 		return data.error;
186 
187 	if (mmc_host_is_spi(card->host)) {
188 		/* host driver already reported errors */
189 	} else {
190 		if (cmd.resp[0] & R5_ERROR)
191 			return -EIO;
192 		if (cmd.resp[0] & R5_FUNCTION_NUMBER)
193 			return -EINVAL;
194 		if (cmd.resp[0] & R5_OUT_OF_RANGE)
195 			return -ERANGE;
196 	}
197 
198 	return 0;
199 }
200 
201 int sdio_reset(struct mmc_host *host)
202 {
203 	int ret;
204 	u8 abort;
205 
206 	/* SDIO Simplified Specification V2.0, 4.4 Reset for SDIO */
207 
208 	ret = mmc_io_rw_direct_host(host, 0, 0, SDIO_CCCR_ABORT, 0, &abort);
209 	if (ret)
210 		abort = 0x08;
211 	else
212 		abort |= 0x08;
213 
214 	return mmc_io_rw_direct_host(host, 1, 0, SDIO_CCCR_ABORT, abort, NULL);
215 }
216 
217