1
2
3
4
5
6
7
8
9
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
22int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
23{
24 struct mmc_command cmd = {0};
25 int i, err = 0;
26
27 BUG_ON(!host);
28
29 cmd.opcode = SD_IO_SEND_OP_COND;
30 cmd.arg = ocr;
31 cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
32
33 for (i = 100; i; i--) {
34 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
35 if (err)
36 break;
37
38
39 if (ocr == 0)
40 break;
41
42
43 if (mmc_host_is_spi(host)) {
44
45
46
47
48
49
50 if (cmd.resp[1] & MMC_CARD_BUSY)
51 break;
52 } else {
53 if (cmd.resp[0] & MMC_CARD_BUSY)
54 break;
55 }
56
57 err = -ETIMEDOUT;
58
59 mmc_delay(10);
60 }
61
62 if (rocr)
63 *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
64
65 return err;
66}
67
68static int mmc_io_rw_direct_host(struct mmc_host *host, int write, unsigned fn,
69 unsigned addr, u8 in, u8 *out)
70{
71 struct mmc_command cmd = {0};
72 int err;
73
74 BUG_ON(!host);
75 BUG_ON(fn > 7);
76
77
78 if (addr & ~0x1FFFF)
79 return -EINVAL;
80
81 cmd.opcode = SD_IO_RW_DIRECT;
82 cmd.arg = write ? 0x80000000 : 0x00000000;
83 cmd.arg |= fn << 28;
84 cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
85 cmd.arg |= addr << 9;
86 cmd.arg |= in;
87 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
88
89 err = mmc_wait_for_cmd(host, &cmd, 0);
90 if (err)
91 return err;
92
93 if (mmc_host_is_spi(host)) {
94
95 } else {
96 if (cmd.resp[0] & R5_ERROR)
97 return -EIO;
98 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
99 return -EINVAL;
100 if (cmd.resp[0] & R5_OUT_OF_RANGE)
101 return -ERANGE;
102 }
103
104 if (out) {
105 if (mmc_host_is_spi(host))
106 *out = (cmd.resp[0] >> 8) & 0xFF;
107 else
108 *out = cmd.resp[0] & 0xFF;
109 }
110
111 return 0;
112}
113
114int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
115 unsigned addr, u8 in, u8 *out)
116{
117 BUG_ON(!card);
118 return mmc_io_rw_direct_host(card->host, write, fn, addr, in, out);
119}
120
121int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
122 unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
123{
124 struct mmc_request mrq = {NULL};
125 struct mmc_command cmd = {0};
126 struct mmc_data data = {0};
127 struct scatterlist sg;
128
129 BUG_ON(!card);
130 BUG_ON(fn > 7);
131 BUG_ON(blocks == 1 && blksz > 512);
132 WARN_ON(blocks == 0);
133 WARN_ON(blksz == 0);
134
135
136 if (addr & ~0x1FFFF)
137 return -EINVAL;
138
139 mrq.cmd = &cmd;
140 mrq.data = &data;
141
142 cmd.opcode = SD_IO_RW_EXTENDED;
143 cmd.arg = write ? 0x80000000 : 0x00000000;
144 cmd.arg |= fn << 28;
145 cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
146 cmd.arg |= addr << 9;
147 if (blocks == 1 && blksz < 512)
148 cmd.arg |= blksz;
149 else if (blocks == 1 && blksz == 512 &&
150 !(mmc_card_broken_byte_mode_512(card)))
151 cmd.arg |= 0;
152 else
153 cmd.arg |= 0x08000000 | blocks;
154 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
155
156 data.blksz = blksz;
157 data.blocks = blocks;
158 data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
159 data.sg = &sg;
160 data.sg_len = 1;
161
162 sg_init_one(&sg, buf, blksz * blocks);
163
164 mmc_set_data_timeout(&data, card);
165
166 mmc_wait_for_req(card->host, &mrq);
167
168 if (cmd.error)
169 return cmd.error;
170 if (data.error)
171 return data.error;
172
173 if (mmc_host_is_spi(card->host)) {
174
175 } else {
176 if (cmd.resp[0] & R5_ERROR)
177 return -EIO;
178 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
179 return -EINVAL;
180 if (cmd.resp[0] & R5_OUT_OF_RANGE)
181 return -ERANGE;
182 }
183
184 return 0;
185}
186
187int sdio_reset(struct mmc_host *host)
188{
189 int ret;
190 u8 abort;
191
192
193
194 ret = mmc_io_rw_direct_host(host, 0, 0, SDIO_CCCR_ABORT, 0, &abort);
195 if (ret)
196 abort = 0x08;
197 else
198 abort |= 0x08;
199
200 ret = mmc_io_rw_direct_host(host, 1, 0, SDIO_CCCR_ABORT, abort, NULL);
201 return ret;
202}
203
204