Commit da7fbe58d2d347e95af699ddf04d885be6362bbe

Authored by Pierre Ossman
1 parent aaac1b470b

mmc: Separate out protocol ops

Move protocol operations and definitions into their own files
in an effort to separate protocol handling and bus
arbitration more clearly.

Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>

Showing 14 changed files with 1099 additions and 772 deletions Inline Diff

drivers/mmc/card/block.c
1 /* 1 /*
2 * Block driver for media (i.e., flash cards) 2 * Block driver for media (i.e., flash cards)
3 * 3 *
4 * Copyright 2002 Hewlett-Packard Company 4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2007 Pierre Ossman 5 * Copyright 2005-2007 Pierre Ossman
6 * 6 *
7 * Use consistent with the GNU GPL is permitted, 7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is 8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works. 9 * preserved in its entirety in all copies and derived works.
10 * 10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, 11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS 12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE. 13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 * 14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet! 15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 * 16 *
17 * Author: Andrew Christian 17 * Author: Andrew Christian
18 * 28 May 2002 18 * 28 May 2002
19 */ 19 */
20 #include <linux/moduleparam.h> 20 #include <linux/moduleparam.h>
21 #include <linux/module.h> 21 #include <linux/module.h>
22 #include <linux/init.h> 22 #include <linux/init.h>
23 23
24 #include <linux/kernel.h> 24 #include <linux/kernel.h>
25 #include <linux/fs.h> 25 #include <linux/fs.h>
26 #include <linux/errno.h> 26 #include <linux/errno.h>
27 #include <linux/hdreg.h> 27 #include <linux/hdreg.h>
28 #include <linux/kdev_t.h> 28 #include <linux/kdev_t.h>
29 #include <linux/blkdev.h> 29 #include <linux/blkdev.h>
30 #include <linux/mutex.h> 30 #include <linux/mutex.h>
31 #include <linux/scatterlist.h> 31 #include <linux/scatterlist.h>
32 32
33 #include <linux/mmc/card.h> 33 #include <linux/mmc/card.h>
34 #include <linux/mmc/host.h> 34 #include <linux/mmc/host.h>
35 #include <linux/mmc/protocol.h> 35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/host.h> 36 #include <linux/mmc/sd.h>
37 37
38 #include <asm/system.h> 38 #include <asm/system.h>
39 #include <asm/uaccess.h> 39 #include <asm/uaccess.h>
40 40
41 #include "queue.h" 41 #include "queue.h"
42 42
43 /* 43 /*
44 * max 8 partitions per card 44 * max 8 partitions per card
45 */ 45 */
46 #define MMC_SHIFT 3 46 #define MMC_SHIFT 3
47 47
48 static int major; 48 static int major;
49 49
50 /* 50 /*
51 * There is one mmc_blk_data per slot. 51 * There is one mmc_blk_data per slot.
52 */ 52 */
53 struct mmc_blk_data { 53 struct mmc_blk_data {
54 spinlock_t lock; 54 spinlock_t lock;
55 struct gendisk *disk; 55 struct gendisk *disk;
56 struct mmc_queue queue; 56 struct mmc_queue queue;
57 57
58 unsigned int usage; 58 unsigned int usage;
59 unsigned int block_bits; 59 unsigned int block_bits;
60 unsigned int read_only; 60 unsigned int read_only;
61 }; 61 };
62 62
63 static DEFINE_MUTEX(open_lock); 63 static DEFINE_MUTEX(open_lock);
64 64
65 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) 65 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
66 { 66 {
67 struct mmc_blk_data *md; 67 struct mmc_blk_data *md;
68 68
69 mutex_lock(&open_lock); 69 mutex_lock(&open_lock);
70 md = disk->private_data; 70 md = disk->private_data;
71 if (md && md->usage == 0) 71 if (md && md->usage == 0)
72 md = NULL; 72 md = NULL;
73 if (md) 73 if (md)
74 md->usage++; 74 md->usage++;
75 mutex_unlock(&open_lock); 75 mutex_unlock(&open_lock);
76 76
77 return md; 77 return md;
78 } 78 }
79 79
80 static void mmc_blk_put(struct mmc_blk_data *md) 80 static void mmc_blk_put(struct mmc_blk_data *md)
81 { 81 {
82 mutex_lock(&open_lock); 82 mutex_lock(&open_lock);
83 md->usage--; 83 md->usage--;
84 if (md->usage == 0) { 84 if (md->usage == 0) {
85 put_disk(md->disk); 85 put_disk(md->disk);
86 kfree(md); 86 kfree(md);
87 } 87 }
88 mutex_unlock(&open_lock); 88 mutex_unlock(&open_lock);
89 } 89 }
90 90
91 static int mmc_blk_open(struct inode *inode, struct file *filp) 91 static int mmc_blk_open(struct inode *inode, struct file *filp)
92 { 92 {
93 struct mmc_blk_data *md; 93 struct mmc_blk_data *md;
94 int ret = -ENXIO; 94 int ret = -ENXIO;
95 95
96 md = mmc_blk_get(inode->i_bdev->bd_disk); 96 md = mmc_blk_get(inode->i_bdev->bd_disk);
97 if (md) { 97 if (md) {
98 if (md->usage == 2) 98 if (md->usage == 2)
99 check_disk_change(inode->i_bdev); 99 check_disk_change(inode->i_bdev);
100 ret = 0; 100 ret = 0;
101 101
102 if ((filp->f_mode & FMODE_WRITE) && md->read_only) 102 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
103 ret = -EROFS; 103 ret = -EROFS;
104 } 104 }
105 105
106 return ret; 106 return ret;
107 } 107 }
108 108
109 static int mmc_blk_release(struct inode *inode, struct file *filp) 109 static int mmc_blk_release(struct inode *inode, struct file *filp)
110 { 110 {
111 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data; 111 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
112 112
113 mmc_blk_put(md); 113 mmc_blk_put(md);
114 return 0; 114 return 0;
115 } 115 }
116 116
117 static int 117 static int
118 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 118 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
119 { 119 {
120 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); 120 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
121 geo->heads = 4; 121 geo->heads = 4;
122 geo->sectors = 16; 122 geo->sectors = 16;
123 return 0; 123 return 0;
124 } 124 }
125 125
126 static struct block_device_operations mmc_bdops = { 126 static struct block_device_operations mmc_bdops = {
127 .open = mmc_blk_open, 127 .open = mmc_blk_open,
128 .release = mmc_blk_release, 128 .release = mmc_blk_release,
129 .getgeo = mmc_blk_getgeo, 129 .getgeo = mmc_blk_getgeo,
130 .owner = THIS_MODULE, 130 .owner = THIS_MODULE,
131 }; 131 };
132 132
133 struct mmc_blk_request { 133 struct mmc_blk_request {
134 struct mmc_request mrq; 134 struct mmc_request mrq;
135 struct mmc_command cmd; 135 struct mmc_command cmd;
136 struct mmc_command stop; 136 struct mmc_command stop;
137 struct mmc_data data; 137 struct mmc_data data;
138 }; 138 };
139 139
140 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req) 140 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
141 { 141 {
142 struct mmc_blk_data *md = mq->data; 142 struct mmc_blk_data *md = mq->data;
143 int stat = BLKPREP_OK; 143 int stat = BLKPREP_OK;
144 144
145 /* 145 /*
146 * If we have no device, we haven't finished initialising. 146 * If we have no device, we haven't finished initialising.
147 */ 147 */
148 if (!md || !mq->card) { 148 if (!md || !mq->card) {
149 printk(KERN_ERR "%s: killing request - no device/host\n", 149 printk(KERN_ERR "%s: killing request - no device/host\n",
150 req->rq_disk->disk_name); 150 req->rq_disk->disk_name);
151 stat = BLKPREP_KILL; 151 stat = BLKPREP_KILL;
152 } 152 }
153 153
154 return stat; 154 return stat;
155 } 155 }
156 156
157 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) 157 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
158 { 158 {
159 int err; 159 int err;
160 u32 blocks; 160 u32 blocks;
161 161
162 struct mmc_request mrq; 162 struct mmc_request mrq;
163 struct mmc_command cmd; 163 struct mmc_command cmd;
164 struct mmc_data data; 164 struct mmc_data data;
165 unsigned int timeout_us; 165 unsigned int timeout_us;
166 166
167 struct scatterlist sg; 167 struct scatterlist sg;
168 168
169 memset(&cmd, 0, sizeof(struct mmc_command)); 169 memset(&cmd, 0, sizeof(struct mmc_command));
170 170
171 cmd.opcode = MMC_APP_CMD; 171 cmd.opcode = MMC_APP_CMD;
172 cmd.arg = card->rca << 16; 172 cmd.arg = card->rca << 16;
173 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 173 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
174 174
175 err = mmc_wait_for_cmd(card->host, &cmd, 0); 175 err = mmc_wait_for_cmd(card->host, &cmd, 0);
176 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) 176 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
177 return (u32)-1; 177 return (u32)-1;
178 178
179 memset(&cmd, 0, sizeof(struct mmc_command)); 179 memset(&cmd, 0, sizeof(struct mmc_command));
180 180
181 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; 181 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
182 cmd.arg = 0; 182 cmd.arg = 0;
183 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 183 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
184 184
185 memset(&data, 0, sizeof(struct mmc_data)); 185 memset(&data, 0, sizeof(struct mmc_data));
186 186
187 data.timeout_ns = card->csd.tacc_ns * 100; 187 data.timeout_ns = card->csd.tacc_ns * 100;
188 data.timeout_clks = card->csd.tacc_clks * 100; 188 data.timeout_clks = card->csd.tacc_clks * 100;
189 189
190 timeout_us = data.timeout_ns / 1000; 190 timeout_us = data.timeout_ns / 1000;
191 timeout_us += data.timeout_clks * 1000 / 191 timeout_us += data.timeout_clks * 1000 /
192 (card->host->ios.clock / 1000); 192 (card->host->ios.clock / 1000);
193 193
194 if (timeout_us > 100000) { 194 if (timeout_us > 100000) {
195 data.timeout_ns = 100000000; 195 data.timeout_ns = 100000000;
196 data.timeout_clks = 0; 196 data.timeout_clks = 0;
197 } 197 }
198 198
199 data.blksz = 4; 199 data.blksz = 4;
200 data.blocks = 1; 200 data.blocks = 1;
201 data.flags = MMC_DATA_READ; 201 data.flags = MMC_DATA_READ;
202 data.sg = &sg; 202 data.sg = &sg;
203 data.sg_len = 1; 203 data.sg_len = 1;
204 204
205 memset(&mrq, 0, sizeof(struct mmc_request)); 205 memset(&mrq, 0, sizeof(struct mmc_request));
206 206
207 mrq.cmd = &cmd; 207 mrq.cmd = &cmd;
208 mrq.data = &data; 208 mrq.data = &data;
209 209
210 sg_init_one(&sg, &blocks, 4); 210 sg_init_one(&sg, &blocks, 4);
211 211
212 mmc_wait_for_req(card->host, &mrq); 212 mmc_wait_for_req(card->host, &mrq);
213 213
214 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) 214 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
215 return (u32)-1; 215 return (u32)-1;
216 216
217 blocks = ntohl(blocks); 217 blocks = ntohl(blocks);
218 218
219 return blocks; 219 return blocks;
220 } 220 }
221 221
222 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) 222 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
223 { 223 {
224 struct mmc_blk_data *md = mq->data; 224 struct mmc_blk_data *md = mq->data;
225 struct mmc_card *card = md->queue.card; 225 struct mmc_card *card = md->queue.card;
226 struct mmc_blk_request brq; 226 struct mmc_blk_request brq;
227 int ret = 1, sg_pos, data_size; 227 int ret = 1, sg_pos, data_size;
228 228
229 mmc_claim_host(card->host); 229 mmc_claim_host(card->host);
230 230
231 do { 231 do {
232 struct mmc_command cmd; 232 struct mmc_command cmd;
233 u32 readcmd, writecmd; 233 u32 readcmd, writecmd;
234 234
235 memset(&brq, 0, sizeof(struct mmc_blk_request)); 235 memset(&brq, 0, sizeof(struct mmc_blk_request));
236 brq.mrq.cmd = &brq.cmd; 236 brq.mrq.cmd = &brq.cmd;
237 brq.mrq.data = &brq.data; 237 brq.mrq.data = &brq.data;
238 238
239 brq.cmd.arg = req->sector; 239 brq.cmd.arg = req->sector;
240 if (!mmc_card_blockaddr(card)) 240 if (!mmc_card_blockaddr(card))
241 brq.cmd.arg <<= 9; 241 brq.cmd.arg <<= 9;
242 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 242 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
243 brq.data.blksz = 1 << md->block_bits; 243 brq.data.blksz = 1 << md->block_bits;
244 brq.stop.opcode = MMC_STOP_TRANSMISSION; 244 brq.stop.opcode = MMC_STOP_TRANSMISSION;
245 brq.stop.arg = 0; 245 brq.stop.arg = 0;
246 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 246 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
247 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9); 247 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
248 if (brq.data.blocks > card->host->max_blk_count) 248 if (brq.data.blocks > card->host->max_blk_count)
249 brq.data.blocks = card->host->max_blk_count; 249 brq.data.blocks = card->host->max_blk_count;
250 250
251 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ); 251 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
252 252
253 /* 253 /*
254 * If the host doesn't support multiple block writes, force 254 * If the host doesn't support multiple block writes, force
255 * block writes to single block. SD cards are excepted from 255 * block writes to single block. SD cards are excepted from
256 * this rule as they support querying the number of 256 * this rule as they support querying the number of
257 * successfully written sectors. 257 * successfully written sectors.
258 */ 258 */
259 if (rq_data_dir(req) != READ && 259 if (rq_data_dir(req) != READ &&
260 !(card->host->caps & MMC_CAP_MULTIWRITE) && 260 !(card->host->caps & MMC_CAP_MULTIWRITE) &&
261 !mmc_card_sd(card)) 261 !mmc_card_sd(card))
262 brq.data.blocks = 1; 262 brq.data.blocks = 1;
263 263
264 if (brq.data.blocks > 1) { 264 if (brq.data.blocks > 1) {
265 brq.data.flags |= MMC_DATA_MULTI; 265 brq.data.flags |= MMC_DATA_MULTI;
266 brq.mrq.stop = &brq.stop; 266 brq.mrq.stop = &brq.stop;
267 readcmd = MMC_READ_MULTIPLE_BLOCK; 267 readcmd = MMC_READ_MULTIPLE_BLOCK;
268 writecmd = MMC_WRITE_MULTIPLE_BLOCK; 268 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
269 } else { 269 } else {
270 brq.mrq.stop = NULL; 270 brq.mrq.stop = NULL;
271 readcmd = MMC_READ_SINGLE_BLOCK; 271 readcmd = MMC_READ_SINGLE_BLOCK;
272 writecmd = MMC_WRITE_BLOCK; 272 writecmd = MMC_WRITE_BLOCK;
273 } 273 }
274 274
275 if (rq_data_dir(req) == READ) { 275 if (rq_data_dir(req) == READ) {
276 brq.cmd.opcode = readcmd; 276 brq.cmd.opcode = readcmd;
277 brq.data.flags |= MMC_DATA_READ; 277 brq.data.flags |= MMC_DATA_READ;
278 } else { 278 } else {
279 brq.cmd.opcode = writecmd; 279 brq.cmd.opcode = writecmd;
280 brq.data.flags |= MMC_DATA_WRITE; 280 brq.data.flags |= MMC_DATA_WRITE;
281 } 281 }
282 282
283 brq.data.sg = mq->sg; 283 brq.data.sg = mq->sg;
284 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg); 284 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
285 285
286 if (brq.data.blocks != 286 if (brq.data.blocks !=
287 (req->nr_sectors >> (md->block_bits - 9))) { 287 (req->nr_sectors >> (md->block_bits - 9))) {
288 data_size = brq.data.blocks * brq.data.blksz; 288 data_size = brq.data.blocks * brq.data.blksz;
289 for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) { 289 for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
290 data_size -= mq->sg[sg_pos].length; 290 data_size -= mq->sg[sg_pos].length;
291 if (data_size <= 0) { 291 if (data_size <= 0) {
292 mq->sg[sg_pos].length += data_size; 292 mq->sg[sg_pos].length += data_size;
293 sg_pos++; 293 sg_pos++;
294 break; 294 break;
295 } 295 }
296 } 296 }
297 brq.data.sg_len = sg_pos; 297 brq.data.sg_len = sg_pos;
298 } 298 }
299 299
300 mmc_wait_for_req(card->host, &brq.mrq); 300 mmc_wait_for_req(card->host, &brq.mrq);
301 if (brq.cmd.error) { 301 if (brq.cmd.error) {
302 printk(KERN_ERR "%s: error %d sending read/write command\n", 302 printk(KERN_ERR "%s: error %d sending read/write command\n",
303 req->rq_disk->disk_name, brq.cmd.error); 303 req->rq_disk->disk_name, brq.cmd.error);
304 goto cmd_err; 304 goto cmd_err;
305 } 305 }
306 306
307 if (brq.data.error) { 307 if (brq.data.error) {
308 printk(KERN_ERR "%s: error %d transferring data\n", 308 printk(KERN_ERR "%s: error %d transferring data\n",
309 req->rq_disk->disk_name, brq.data.error); 309 req->rq_disk->disk_name, brq.data.error);
310 goto cmd_err; 310 goto cmd_err;
311 } 311 }
312 312
313 if (brq.stop.error) { 313 if (brq.stop.error) {
314 printk(KERN_ERR "%s: error %d sending stop command\n", 314 printk(KERN_ERR "%s: error %d sending stop command\n",
315 req->rq_disk->disk_name, brq.stop.error); 315 req->rq_disk->disk_name, brq.stop.error);
316 goto cmd_err; 316 goto cmd_err;
317 } 317 }
318 318
319 if (rq_data_dir(req) != READ) { 319 if (rq_data_dir(req) != READ) {
320 do { 320 do {
321 int err; 321 int err;
322 322
323 cmd.opcode = MMC_SEND_STATUS; 323 cmd.opcode = MMC_SEND_STATUS;
324 cmd.arg = card->rca << 16; 324 cmd.arg = card->rca << 16;
325 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 325 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
326 err = mmc_wait_for_cmd(card->host, &cmd, 5); 326 err = mmc_wait_for_cmd(card->host, &cmd, 5);
327 if (err) { 327 if (err) {
328 printk(KERN_ERR "%s: error %d requesting status\n", 328 printk(KERN_ERR "%s: error %d requesting status\n",
329 req->rq_disk->disk_name, err); 329 req->rq_disk->disk_name, err);
330 goto cmd_err; 330 goto cmd_err;
331 } 331 }
332 } while (!(cmd.resp[0] & R1_READY_FOR_DATA)); 332 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
333 333
334 #if 0 334 #if 0
335 if (cmd.resp[0] & ~0x00000900) 335 if (cmd.resp[0] & ~0x00000900)
336 printk(KERN_ERR "%s: status = %08x\n", 336 printk(KERN_ERR "%s: status = %08x\n",
337 req->rq_disk->disk_name, cmd.resp[0]); 337 req->rq_disk->disk_name, cmd.resp[0]);
338 if (mmc_decode_status(cmd.resp)) 338 if (mmc_decode_status(cmd.resp))
339 goto cmd_err; 339 goto cmd_err;
340 #endif 340 #endif
341 } 341 }
342 342
343 /* 343 /*
344 * A block was successfully transferred. 344 * A block was successfully transferred.
345 */ 345 */
346 spin_lock_irq(&md->lock); 346 spin_lock_irq(&md->lock);
347 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered); 347 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
348 if (!ret) { 348 if (!ret) {
349 /* 349 /*
350 * The whole request completed successfully. 350 * The whole request completed successfully.
351 */ 351 */
352 add_disk_randomness(req->rq_disk); 352 add_disk_randomness(req->rq_disk);
353 blkdev_dequeue_request(req); 353 blkdev_dequeue_request(req);
354 end_that_request_last(req, 1); 354 end_that_request_last(req, 1);
355 } 355 }
356 spin_unlock_irq(&md->lock); 356 spin_unlock_irq(&md->lock);
357 } while (ret); 357 } while (ret);
358 358
359 mmc_release_host(card->host); 359 mmc_release_host(card->host);
360 360
361 return 1; 361 return 1;
362 362
363 cmd_err: 363 cmd_err:
364 /* 364 /*
365 * If this is an SD card and we're writing, we can first 365 * If this is an SD card and we're writing, we can first
366 * mark the known good sectors as ok. 366 * mark the known good sectors as ok.
367 * 367 *
368 * If the card is not SD, we can still ok written sectors 368 * If the card is not SD, we can still ok written sectors
369 * if the controller can do proper error reporting. 369 * if the controller can do proper error reporting.
370 * 370 *
371 * For reads we just fail the entire chunk as that should 371 * For reads we just fail the entire chunk as that should
372 * be safe in all cases. 372 * be safe in all cases.
373 */ 373 */
374 if (rq_data_dir(req) != READ && mmc_card_sd(card)) { 374 if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
375 u32 blocks; 375 u32 blocks;
376 unsigned int bytes; 376 unsigned int bytes;
377 377
378 blocks = mmc_sd_num_wr_blocks(card); 378 blocks = mmc_sd_num_wr_blocks(card);
379 if (blocks != (u32)-1) { 379 if (blocks != (u32)-1) {
380 if (card->csd.write_partial) 380 if (card->csd.write_partial)
381 bytes = blocks << md->block_bits; 381 bytes = blocks << md->block_bits;
382 else 382 else
383 bytes = blocks << 9; 383 bytes = blocks << 9;
384 spin_lock_irq(&md->lock); 384 spin_lock_irq(&md->lock);
385 ret = end_that_request_chunk(req, 1, bytes); 385 ret = end_that_request_chunk(req, 1, bytes);
386 spin_unlock_irq(&md->lock); 386 spin_unlock_irq(&md->lock);
387 } 387 }
388 } else if (rq_data_dir(req) != READ && 388 } else if (rq_data_dir(req) != READ &&
389 (card->host->caps & MMC_CAP_MULTIWRITE)) { 389 (card->host->caps & MMC_CAP_MULTIWRITE)) {
390 spin_lock_irq(&md->lock); 390 spin_lock_irq(&md->lock);
391 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered); 391 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
392 spin_unlock_irq(&md->lock); 392 spin_unlock_irq(&md->lock);
393 } 393 }
394 394
395 mmc_release_host(card->host); 395 mmc_release_host(card->host);
396 396
397 spin_lock_irq(&md->lock); 397 spin_lock_irq(&md->lock);
398 while (ret) { 398 while (ret) {
399 ret = end_that_request_chunk(req, 0, 399 ret = end_that_request_chunk(req, 0,
400 req->current_nr_sectors << 9); 400 req->current_nr_sectors << 9);
401 } 401 }
402 402
403 add_disk_randomness(req->rq_disk); 403 add_disk_randomness(req->rq_disk);
404 blkdev_dequeue_request(req); 404 blkdev_dequeue_request(req);
405 end_that_request_last(req, 0); 405 end_that_request_last(req, 0);
406 spin_unlock_irq(&md->lock); 406 spin_unlock_irq(&md->lock);
407 407
408 return 0; 408 return 0;
409 } 409 }
410 410
411 #define MMC_NUM_MINORS (256 >> MMC_SHIFT) 411 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
412 412
413 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))]; 413 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
414 414
415 static inline int mmc_blk_readonly(struct mmc_card *card) 415 static inline int mmc_blk_readonly(struct mmc_card *card)
416 { 416 {
417 return mmc_card_readonly(card) || 417 return mmc_card_readonly(card) ||
418 !(card->csd.cmdclass & CCC_BLOCK_WRITE); 418 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
419 } 419 }
420 420
421 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) 421 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
422 { 422 {
423 struct mmc_blk_data *md; 423 struct mmc_blk_data *md;
424 int devidx, ret; 424 int devidx, ret;
425 425
426 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS); 426 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
427 if (devidx >= MMC_NUM_MINORS) 427 if (devidx >= MMC_NUM_MINORS)
428 return ERR_PTR(-ENOSPC); 428 return ERR_PTR(-ENOSPC);
429 __set_bit(devidx, dev_use); 429 __set_bit(devidx, dev_use);
430 430
431 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); 431 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
432 if (!md) { 432 if (!md) {
433 ret = -ENOMEM; 433 ret = -ENOMEM;
434 goto out; 434 goto out;
435 } 435 }
436 436
437 memset(md, 0, sizeof(struct mmc_blk_data)); 437 memset(md, 0, sizeof(struct mmc_blk_data));
438 438
439 /* 439 /*
440 * Set the read-only status based on the supported commands 440 * Set the read-only status based on the supported commands
441 * and the write protect switch. 441 * and the write protect switch.
442 */ 442 */
443 md->read_only = mmc_blk_readonly(card); 443 md->read_only = mmc_blk_readonly(card);
444 444
445 /* 445 /*
446 * Both SD and MMC specifications state (although a bit 446 * Both SD and MMC specifications state (although a bit
447 * unclearly in the MMC case) that a block size of 512 447 * unclearly in the MMC case) that a block size of 512
448 * bytes must always be supported by the card. 448 * bytes must always be supported by the card.
449 */ 449 */
450 md->block_bits = 9; 450 md->block_bits = 9;
451 451
452 md->disk = alloc_disk(1 << MMC_SHIFT); 452 md->disk = alloc_disk(1 << MMC_SHIFT);
453 if (md->disk == NULL) { 453 if (md->disk == NULL) {
454 ret = -ENOMEM; 454 ret = -ENOMEM;
455 goto err_kfree; 455 goto err_kfree;
456 } 456 }
457 457
458 spin_lock_init(&md->lock); 458 spin_lock_init(&md->lock);
459 md->usage = 1; 459 md->usage = 1;
460 460
461 ret = mmc_init_queue(&md->queue, card, &md->lock); 461 ret = mmc_init_queue(&md->queue, card, &md->lock);
462 if (ret) 462 if (ret)
463 goto err_putdisk; 463 goto err_putdisk;
464 464
465 md->queue.prep_fn = mmc_blk_prep_rq; 465 md->queue.prep_fn = mmc_blk_prep_rq;
466 md->queue.issue_fn = mmc_blk_issue_rq; 466 md->queue.issue_fn = mmc_blk_issue_rq;
467 md->queue.data = md; 467 md->queue.data = md;
468 468
469 md->disk->major = major; 469 md->disk->major = major;
470 md->disk->first_minor = devidx << MMC_SHIFT; 470 md->disk->first_minor = devidx << MMC_SHIFT;
471 md->disk->fops = &mmc_bdops; 471 md->disk->fops = &mmc_bdops;
472 md->disk->private_data = md; 472 md->disk->private_data = md;
473 md->disk->queue = md->queue.queue; 473 md->disk->queue = md->queue.queue;
474 md->disk->driverfs_dev = &card->dev; 474 md->disk->driverfs_dev = &card->dev;
475 475
476 /* 476 /*
477 * As discussed on lkml, GENHD_FL_REMOVABLE should: 477 * As discussed on lkml, GENHD_FL_REMOVABLE should:
478 * 478 *
479 * - be set for removable media with permanent block devices 479 * - be set for removable media with permanent block devices
480 * - be unset for removable block devices with permanent media 480 * - be unset for removable block devices with permanent media
481 * 481 *
482 * Since MMC block devices clearly fall under the second 482 * Since MMC block devices clearly fall under the second
483 * case, we do not set GENHD_FL_REMOVABLE. Userspace 483 * case, we do not set GENHD_FL_REMOVABLE. Userspace
484 * should use the block device creation/destruction hotplug 484 * should use the block device creation/destruction hotplug
485 * messages to tell when the card is present. 485 * messages to tell when the card is present.
486 */ 486 */
487 487
488 sprintf(md->disk->disk_name, "mmcblk%d", devidx); 488 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
489 489
490 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits); 490 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
491 491
492 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { 492 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
493 /* 493 /*
494 * The EXT_CSD sector count is in number or 512 byte 494 * The EXT_CSD sector count is in number or 512 byte
495 * sectors. 495 * sectors.
496 */ 496 */
497 set_capacity(md->disk, card->ext_csd.sectors); 497 set_capacity(md->disk, card->ext_csd.sectors);
498 } else { 498 } else {
499 /* 499 /*
500 * The CSD capacity field is in units of read_blkbits. 500 * The CSD capacity field is in units of read_blkbits.
501 * set_capacity takes units of 512 bytes. 501 * set_capacity takes units of 512 bytes.
502 */ 502 */
503 set_capacity(md->disk, 503 set_capacity(md->disk,
504 card->csd.capacity << (card->csd.read_blkbits - 9)); 504 card->csd.capacity << (card->csd.read_blkbits - 9));
505 } 505 }
506 return md; 506 return md;
507 507
508 err_putdisk: 508 err_putdisk:
509 put_disk(md->disk); 509 put_disk(md->disk);
510 err_kfree: 510 err_kfree:
511 kfree(md); 511 kfree(md);
512 out: 512 out:
513 return ERR_PTR(ret); 513 return ERR_PTR(ret);
514 } 514 }
515 515
516 static int 516 static int
517 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) 517 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
518 { 518 {
519 struct mmc_command cmd; 519 struct mmc_command cmd;
520 int err; 520 int err;
521 521
522 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */ 522 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
523 if (mmc_card_blockaddr(card)) 523 if (mmc_card_blockaddr(card))
524 return 0; 524 return 0;
525 525
526 mmc_claim_host(card->host); 526 mmc_claim_host(card->host);
527 cmd.opcode = MMC_SET_BLOCKLEN; 527 cmd.opcode = MMC_SET_BLOCKLEN;
528 cmd.arg = 1 << md->block_bits; 528 cmd.arg = 1 << md->block_bits;
529 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 529 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
530 err = mmc_wait_for_cmd(card->host, &cmd, 5); 530 err = mmc_wait_for_cmd(card->host, &cmd, 5);
531 mmc_release_host(card->host); 531 mmc_release_host(card->host);
532 532
533 if (err) { 533 if (err) {
534 printk(KERN_ERR "%s: unable to set block size to %d: %d\n", 534 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
535 md->disk->disk_name, cmd.arg, err); 535 md->disk->disk_name, cmd.arg, err);
536 return -EINVAL; 536 return -EINVAL;
537 } 537 }
538 538
539 return 0; 539 return 0;
540 } 540 }
541 541
542 static int mmc_blk_probe(struct mmc_card *card) 542 static int mmc_blk_probe(struct mmc_card *card)
543 { 543 {
544 struct mmc_blk_data *md; 544 struct mmc_blk_data *md;
545 int err; 545 int err;
546 546
547 /* 547 /*
548 * Check that the card supports the command class(es) we need. 548 * Check that the card supports the command class(es) we need.
549 */ 549 */
550 if (!(card->csd.cmdclass & CCC_BLOCK_READ)) 550 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
551 return -ENODEV; 551 return -ENODEV;
552 552
553 md = mmc_blk_alloc(card); 553 md = mmc_blk_alloc(card);
554 if (IS_ERR(md)) 554 if (IS_ERR(md))
555 return PTR_ERR(md); 555 return PTR_ERR(md);
556 556
557 err = mmc_blk_set_blksize(md, card); 557 err = mmc_blk_set_blksize(md, card);
558 if (err) 558 if (err)
559 goto out; 559 goto out;
560 560
561 printk(KERN_INFO "%s: %s %s %lluKiB %s\n", 561 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
562 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), 562 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
563 (unsigned long long)(get_capacity(md->disk) >> 1), 563 (unsigned long long)(get_capacity(md->disk) >> 1),
564 md->read_only ? "(ro)" : ""); 564 md->read_only ? "(ro)" : "");
565 565
566 mmc_set_drvdata(card, md); 566 mmc_set_drvdata(card, md);
567 add_disk(md->disk); 567 add_disk(md->disk);
568 return 0; 568 return 0;
569 569
570 out: 570 out:
571 mmc_blk_put(md); 571 mmc_blk_put(md);
572 572
573 return err; 573 return err;
574 } 574 }
575 575
576 static void mmc_blk_remove(struct mmc_card *card) 576 static void mmc_blk_remove(struct mmc_card *card)
577 { 577 {
578 struct mmc_blk_data *md = mmc_get_drvdata(card); 578 struct mmc_blk_data *md = mmc_get_drvdata(card);
579 579
580 if (md) { 580 if (md) {
581 int devidx; 581 int devidx;
582 582
583 /* Stop new requests from getting into the queue */ 583 /* Stop new requests from getting into the queue */
584 del_gendisk(md->disk); 584 del_gendisk(md->disk);
585 585
586 /* Then flush out any already in there */ 586 /* Then flush out any already in there */
587 mmc_cleanup_queue(&md->queue); 587 mmc_cleanup_queue(&md->queue);
588 588
589 devidx = md->disk->first_minor >> MMC_SHIFT; 589 devidx = md->disk->first_minor >> MMC_SHIFT;
590 __clear_bit(devidx, dev_use); 590 __clear_bit(devidx, dev_use);
591 591
592 mmc_blk_put(md); 592 mmc_blk_put(md);
593 } 593 }
594 mmc_set_drvdata(card, NULL); 594 mmc_set_drvdata(card, NULL);
595 } 595 }
596 596
597 #ifdef CONFIG_PM 597 #ifdef CONFIG_PM
598 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state) 598 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
599 { 599 {
600 struct mmc_blk_data *md = mmc_get_drvdata(card); 600 struct mmc_blk_data *md = mmc_get_drvdata(card);
601 601
602 if (md) { 602 if (md) {
603 mmc_queue_suspend(&md->queue); 603 mmc_queue_suspend(&md->queue);
604 } 604 }
605 return 0; 605 return 0;
606 } 606 }
607 607
608 static int mmc_blk_resume(struct mmc_card *card) 608 static int mmc_blk_resume(struct mmc_card *card)
609 { 609 {
610 struct mmc_blk_data *md = mmc_get_drvdata(card); 610 struct mmc_blk_data *md = mmc_get_drvdata(card);
611 611
612 if (md) { 612 if (md) {
613 mmc_blk_set_blksize(md, card); 613 mmc_blk_set_blksize(md, card);
614 mmc_queue_resume(&md->queue); 614 mmc_queue_resume(&md->queue);
615 } 615 }
616 return 0; 616 return 0;
617 } 617 }
618 #else 618 #else
619 #define mmc_blk_suspend NULL 619 #define mmc_blk_suspend NULL
620 #define mmc_blk_resume NULL 620 #define mmc_blk_resume NULL
621 #endif 621 #endif
622 622
623 static struct mmc_driver mmc_driver = { 623 static struct mmc_driver mmc_driver = {
624 .drv = { 624 .drv = {
625 .name = "mmcblk", 625 .name = "mmcblk",
626 }, 626 },
627 .probe = mmc_blk_probe, 627 .probe = mmc_blk_probe,
628 .remove = mmc_blk_remove, 628 .remove = mmc_blk_remove,
629 .suspend = mmc_blk_suspend, 629 .suspend = mmc_blk_suspend,
630 .resume = mmc_blk_resume, 630 .resume = mmc_blk_resume,
631 }; 631 };
632 632
633 static int __init mmc_blk_init(void) 633 static int __init mmc_blk_init(void)
634 { 634 {
635 int res = -ENOMEM; 635 int res = -ENOMEM;
636 636
637 res = register_blkdev(major, "mmc"); 637 res = register_blkdev(major, "mmc");
638 if (res < 0) { 638 if (res < 0) {
639 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n", 639 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
640 major, res); 640 major, res);
641 goto out; 641 goto out;
642 } 642 }
643 if (major == 0) 643 if (major == 0)
644 major = res; 644 major = res;
645 645
646 return mmc_register_driver(&mmc_driver); 646 return mmc_register_driver(&mmc_driver);
647 647
648 out: 648 out:
649 return res; 649 return res;
650 } 650 }
651 651
652 static void __exit mmc_blk_exit(void) 652 static void __exit mmc_blk_exit(void)
653 { 653 {
654 mmc_unregister_driver(&mmc_driver); 654 mmc_unregister_driver(&mmc_driver);
655 unregister_blkdev(major, "mmc"); 655 unregister_blkdev(major, "mmc");
656 } 656 }
657 657
658 module_init(mmc_blk_init); 658 module_init(mmc_blk_init);
659 module_exit(mmc_blk_exit); 659 module_exit(mmc_blk_exit);
660 660
661 MODULE_LICENSE("GPL"); 661 MODULE_LICENSE("GPL");
662 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); 662 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
663 663
664 module_param(major, int, 0444); 664 module_param(major, int, 0444);
665 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver"); 665 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");
666 666
drivers/mmc/core/Makefile
1 # 1 #
2 # Makefile for the kernel mmc core. 2 # Makefile for the kernel mmc core.
3 # 3 #
4 4
5 ifeq ($(CONFIG_MMC_DEBUG),y) 5 ifeq ($(CONFIG_MMC_DEBUG),y)
6 EXTRA_CFLAGS += -DDEBUG 6 EXTRA_CFLAGS += -DDEBUG
7 endif 7 endif
8 8
9 obj-$(CONFIG_MMC) += mmc_core.o 9 obj-$(CONFIG_MMC) += mmc_core.o
10 mmc_core-y := core.o sysfs.o 10 mmc_core-y := core.o sysfs.o mmc_ops.o sd_ops.o
11 11
12 12
drivers/mmc/core/core.c
1 /* 1 /*
2 * linux/drivers/mmc/core/core.c 2 * linux/drivers/mmc/core/core.c
3 * 3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as 10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. 11 * published by the Free Software Foundation.
12 */ 12 */
13 #include <linux/module.h> 13 #include <linux/module.h>
14 #include <linux/init.h> 14 #include <linux/init.h>
15 #include <linux/interrupt.h> 15 #include <linux/interrupt.h>
16 #include <linux/completion.h> 16 #include <linux/completion.h>
17 #include <linux/device.h> 17 #include <linux/device.h>
18 #include <linux/delay.h> 18 #include <linux/delay.h>
19 #include <linux/pagemap.h> 19 #include <linux/pagemap.h>
20 #include <linux/err.h> 20 #include <linux/err.h>
21 #include <asm/scatterlist.h> 21 #include <asm/scatterlist.h>
22 #include <linux/scatterlist.h> 22 #include <linux/scatterlist.h>
23 23
24 #include <linux/mmc/card.h> 24 #include <linux/mmc/card.h>
25 #include <linux/mmc/host.h> 25 #include <linux/mmc/host.h>
26 #include <linux/mmc/protocol.h> 26 #include <linux/mmc/mmc.h>
27 #include <linux/mmc/sd.h>
27 28
28 #include "core.h" 29 #include "core.h"
30 #include "sysfs.h"
29 31
32 #include "mmc_ops.h"
33 #include "sd_ops.h"
34
30 #define CMD_RETRIES 3 35 #define CMD_RETRIES 3
31 36
32 /* 37 /*
33 * OCR Bit positions to 10s of Vdd mV. 38 * OCR Bit positions to 10s of Vdd mV.
34 */ 39 */
35 static const unsigned short mmc_ocr_bit_to_vdd[] = { 40 static const unsigned short mmc_ocr_bit_to_vdd[] = {
36 150, 155, 160, 165, 170, 180, 190, 200, 41 150, 155, 160, 165, 170, 180, 190, 200,
37 210, 220, 230, 240, 250, 260, 270, 280, 42 210, 220, 230, 240, 250, 260, 270, 280,
38 290, 300, 310, 320, 330, 340, 350, 360 43 290, 300, 310, 320, 330, 340, 350, 360
39 }; 44 };
40 45
41 static const unsigned int tran_exp[] = { 46 static const unsigned int tran_exp[] = {
42 10000, 100000, 1000000, 10000000, 47 10000, 100000, 1000000, 10000000,
43 0, 0, 0, 0 48 0, 0, 0, 0
44 }; 49 };
45 50
46 static const unsigned char tran_mant[] = { 51 static const unsigned char tran_mant[] = {
47 0, 10, 12, 13, 15, 20, 25, 30, 52 0, 10, 12, 13, 15, 20, 25, 30,
48 35, 40, 45, 50, 55, 60, 70, 80, 53 35, 40, 45, 50, 55, 60, 70, 80,
49 }; 54 };
50 55
51 static const unsigned int tacc_exp[] = { 56 static const unsigned int tacc_exp[] = {
52 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 57 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
53 }; 58 };
54 59
55 static const unsigned int tacc_mant[] = { 60 static const unsigned int tacc_mant[] = {
56 0, 10, 12, 13, 15, 20, 25, 30, 61 0, 10, 12, 13, 15, 20, 25, 30,
57 35, 40, 45, 50, 55, 60, 70, 80, 62 35, 40, 45, 50, 55, 60, 70, 80,
58 }; 63 };
59 64
60 65
61 /** 66 /**
62 * mmc_request_done - finish processing an MMC request 67 * mmc_request_done - finish processing an MMC request
63 * @host: MMC host which completed request 68 * @host: MMC host which completed request
64 * @mrq: MMC request which request 69 * @mrq: MMC request which request
65 * 70 *
66 * MMC drivers should call this function when they have completed 71 * MMC drivers should call this function when they have completed
67 * their processing of a request. 72 * their processing of a request.
68 */ 73 */
69 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) 74 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
70 { 75 {
71 struct mmc_command *cmd = mrq->cmd; 76 struct mmc_command *cmd = mrq->cmd;
72 int err = cmd->error; 77 int err = cmd->error;
73 78
74 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n", 79 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
75 mmc_hostname(host), cmd->opcode, err, 80 mmc_hostname(host), cmd->opcode, err,
76 mrq->data ? mrq->data->error : 0, 81 mrq->data ? mrq->data->error : 0,
77 mrq->stop ? mrq->stop->error : 0, 82 mrq->stop ? mrq->stop->error : 0,
78 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); 83 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
79 84
80 if (err && cmd->retries) { 85 if (err && cmd->retries) {
81 cmd->retries--; 86 cmd->retries--;
82 cmd->error = 0; 87 cmd->error = 0;
83 host->ops->request(host, mrq); 88 host->ops->request(host, mrq);
84 } else if (mrq->done) { 89 } else if (mrq->done) {
85 mrq->done(mrq); 90 mrq->done(mrq);
86 } 91 }
87 } 92 }
88 93
89 EXPORT_SYMBOL(mmc_request_done); 94 EXPORT_SYMBOL(mmc_request_done);
90 95
91 /** 96 /**
92 * mmc_start_request - start a command on a host 97 * mmc_start_request - start a command on a host
93 * @host: MMC host to start command on 98 * @host: MMC host to start command on
94 * @mrq: MMC request to start 99 * @mrq: MMC request to start
95 * 100 *
96 * Queue a command on the specified host. We expect the 101 * Queue a command on the specified host. We expect the
97 * caller to be holding the host lock with interrupts disabled. 102 * caller to be holding the host lock with interrupts disabled.
98 */ 103 */
99 void 104 void
100 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) 105 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
101 { 106 {
102 #ifdef CONFIG_MMC_DEBUG 107 #ifdef CONFIG_MMC_DEBUG
103 unsigned int i, sz; 108 unsigned int i, sz;
104 #endif 109 #endif
105 110
106 pr_debug("%s: starting CMD%u arg %08x flags %08x\n", 111 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
107 mmc_hostname(host), mrq->cmd->opcode, 112 mmc_hostname(host), mrq->cmd->opcode,
108 mrq->cmd->arg, mrq->cmd->flags); 113 mrq->cmd->arg, mrq->cmd->flags);
109 114
110 WARN_ON(!host->claimed); 115 WARN_ON(!host->claimed);
111 116
112 mrq->cmd->error = 0; 117 mrq->cmd->error = 0;
113 mrq->cmd->mrq = mrq; 118 mrq->cmd->mrq = mrq;
114 if (mrq->data) { 119 if (mrq->data) {
115 BUG_ON(mrq->data->blksz > host->max_blk_size); 120 BUG_ON(mrq->data->blksz > host->max_blk_size);
116 BUG_ON(mrq->data->blocks > host->max_blk_count); 121 BUG_ON(mrq->data->blocks > host->max_blk_count);
117 BUG_ON(mrq->data->blocks * mrq->data->blksz > 122 BUG_ON(mrq->data->blocks * mrq->data->blksz >
118 host->max_req_size); 123 host->max_req_size);
119 124
120 #ifdef CONFIG_MMC_DEBUG 125 #ifdef CONFIG_MMC_DEBUG
121 sz = 0; 126 sz = 0;
122 for (i = 0;i < mrq->data->sg_len;i++) 127 for (i = 0;i < mrq->data->sg_len;i++)
123 sz += mrq->data->sg[i].length; 128 sz += mrq->data->sg[i].length;
124 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); 129 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
125 #endif 130 #endif
126 131
127 mrq->cmd->data = mrq->data; 132 mrq->cmd->data = mrq->data;
128 mrq->data->error = 0; 133 mrq->data->error = 0;
129 mrq->data->mrq = mrq; 134 mrq->data->mrq = mrq;
130 if (mrq->stop) { 135 if (mrq->stop) {
131 mrq->data->stop = mrq->stop; 136 mrq->data->stop = mrq->stop;
132 mrq->stop->error = 0; 137 mrq->stop->error = 0;
133 mrq->stop->mrq = mrq; 138 mrq->stop->mrq = mrq;
134 } 139 }
135 } 140 }
136 host->ops->request(host, mrq); 141 host->ops->request(host, mrq);
137 } 142 }
138 143
139 EXPORT_SYMBOL(mmc_start_request); 144 EXPORT_SYMBOL(mmc_start_request);
140 145
141 static void mmc_wait_done(struct mmc_request *mrq) 146 static void mmc_wait_done(struct mmc_request *mrq)
142 { 147 {
143 complete(mrq->done_data); 148 complete(mrq->done_data);
144 } 149 }
145 150
146 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) 151 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
147 { 152 {
148 DECLARE_COMPLETION_ONSTACK(complete); 153 DECLARE_COMPLETION_ONSTACK(complete);
149 154
150 mrq->done_data = &complete; 155 mrq->done_data = &complete;
151 mrq->done = mmc_wait_done; 156 mrq->done = mmc_wait_done;
152 157
153 mmc_start_request(host, mrq); 158 mmc_start_request(host, mrq);
154 159
155 wait_for_completion(&complete); 160 wait_for_completion(&complete);
156 161
157 return 0; 162 return 0;
158 } 163 }
159 164
160 EXPORT_SYMBOL(mmc_wait_for_req); 165 EXPORT_SYMBOL(mmc_wait_for_req);
161 166
162 /** 167 /**
163 * mmc_wait_for_cmd - start a command and wait for completion 168 * mmc_wait_for_cmd - start a command and wait for completion
164 * @host: MMC host to start command 169 * @host: MMC host to start command
165 * @cmd: MMC command to start 170 * @cmd: MMC command to start
166 * @retries: maximum number of retries 171 * @retries: maximum number of retries
167 * 172 *
168 * Start a new MMC command for a host, and wait for the command 173 * Start a new MMC command for a host, and wait for the command
169 * to complete. Return any error that occurred while the command 174 * to complete. Return any error that occurred while the command
170 * was executing. Do not attempt to parse the response. 175 * was executing. Do not attempt to parse the response.
171 */ 176 */
172 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) 177 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
173 { 178 {
174 struct mmc_request mrq; 179 struct mmc_request mrq;
175 180
176 BUG_ON(!host->claimed); 181 BUG_ON(!host->claimed);
177 182
178 memset(&mrq, 0, sizeof(struct mmc_request)); 183 memset(&mrq, 0, sizeof(struct mmc_request));
179 184
180 memset(cmd->resp, 0, sizeof(cmd->resp)); 185 memset(cmd->resp, 0, sizeof(cmd->resp));
181 cmd->retries = retries; 186 cmd->retries = retries;
182 187
183 mrq.cmd = cmd; 188 mrq.cmd = cmd;
184 cmd->data = NULL; 189 cmd->data = NULL;
185 190
186 mmc_wait_for_req(host, &mrq); 191 mmc_wait_for_req(host, &mrq);
187 192
188 return cmd->error; 193 return cmd->error;
189 } 194 }
190 195
191 EXPORT_SYMBOL(mmc_wait_for_cmd); 196 EXPORT_SYMBOL(mmc_wait_for_cmd);
192 197
193 /** 198 /**
194 * mmc_wait_for_app_cmd - start an application command and wait for
195 completion
196 * @host: MMC host to start command
197 * @rca: RCA to send MMC_APP_CMD to
198 * @cmd: MMC command to start
199 * @retries: maximum number of retries
200 *
201 * Sends a MMC_APP_CMD, checks the card response, sends the command
202 * in the parameter and waits for it to complete. Return any error
203 * that occurred while the command was executing. Do not attempt to
204 * parse the response.
205 */
206 int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
207 struct mmc_command *cmd, int retries)
208 {
209 struct mmc_request mrq;
210 struct mmc_command appcmd;
211
212 int i, err;
213
214 BUG_ON(!host->claimed);
215 BUG_ON(retries < 0);
216
217 err = MMC_ERR_INVALID;
218
219 /*
220 * We have to resend MMC_APP_CMD for each attempt so
221 * we cannot use the retries field in mmc_command.
222 */
223 for (i = 0;i <= retries;i++) {
224 memset(&mrq, 0, sizeof(struct mmc_request));
225
226 appcmd.opcode = MMC_APP_CMD;
227 appcmd.arg = rca << 16;
228 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
229 appcmd.retries = 0;
230 memset(appcmd.resp, 0, sizeof(appcmd.resp));
231 appcmd.data = NULL;
232
233 mrq.cmd = &appcmd;
234 appcmd.data = NULL;
235
236 mmc_wait_for_req(host, &mrq);
237
238 if (appcmd.error) {
239 err = appcmd.error;
240 continue;
241 }
242
243 /* Check that card supported application commands */
244 if (!(appcmd.resp[0] & R1_APP_CMD))
245 return MMC_ERR_FAILED;
246
247 memset(&mrq, 0, sizeof(struct mmc_request));
248
249 memset(cmd->resp, 0, sizeof(cmd->resp));
250 cmd->retries = 0;
251
252 mrq.cmd = cmd;
253 cmd->data = NULL;
254
255 mmc_wait_for_req(host, &mrq);
256
257 err = cmd->error;
258 if (cmd->error == MMC_ERR_NONE)
259 break;
260 }
261
262 return err;
263 }
264
265 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
266
267 /**
268 * mmc_set_data_timeout - set the timeout for a data command 199 * mmc_set_data_timeout - set the timeout for a data command
269 * @data: data phase for command 200 * @data: data phase for command
270 * @card: the MMC card associated with the data transfer 201 * @card: the MMC card associated with the data transfer
271 * @write: flag to differentiate reads from writes 202 * @write: flag to differentiate reads from writes
272 */ 203 */
273 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card, 204 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
274 int write) 205 int write)
275 { 206 {
276 unsigned int mult; 207 unsigned int mult;
277 208
278 /* 209 /*
279 * SD cards use a 100 multiplier rather than 10 210 * SD cards use a 100 multiplier rather than 10
280 */ 211 */
281 mult = mmc_card_sd(card) ? 100 : 10; 212 mult = mmc_card_sd(card) ? 100 : 10;
282 213
283 /* 214 /*
284 * Scale up the multiplier (and therefore the timeout) by 215 * Scale up the multiplier (and therefore the timeout) by
285 * the r2w factor for writes. 216 * the r2w factor for writes.
286 */ 217 */
287 if (write) 218 if (write)
288 mult <<= card->csd.r2w_factor; 219 mult <<= card->csd.r2w_factor;
289 220
290 data->timeout_ns = card->csd.tacc_ns * mult; 221 data->timeout_ns = card->csd.tacc_ns * mult;
291 data->timeout_clks = card->csd.tacc_clks * mult; 222 data->timeout_clks = card->csd.tacc_clks * mult;
292 223
293 /* 224 /*
294 * SD cards also have an upper limit on the timeout. 225 * SD cards also have an upper limit on the timeout.
295 */ 226 */
296 if (mmc_card_sd(card)) { 227 if (mmc_card_sd(card)) {
297 unsigned int timeout_us, limit_us; 228 unsigned int timeout_us, limit_us;
298 229
299 timeout_us = data->timeout_ns / 1000; 230 timeout_us = data->timeout_ns / 1000;
300 timeout_us += data->timeout_clks * 1000 / 231 timeout_us += data->timeout_clks * 1000 /
301 (card->host->ios.clock / 1000); 232 (card->host->ios.clock / 1000);
302 233
303 if (write) 234 if (write)
304 limit_us = 250000; 235 limit_us = 250000;
305 else 236 else
306 limit_us = 100000; 237 limit_us = 100000;
307 238
308 /* 239 /*
309 * SDHC cards always use these fixed values. 240 * SDHC cards always use these fixed values.
310 */ 241 */
311 if (timeout_us > limit_us || mmc_card_blockaddr(card)) { 242 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
312 data->timeout_ns = limit_us * 1000; 243 data->timeout_ns = limit_us * 1000;
313 data->timeout_clks = 0; 244 data->timeout_clks = 0;
314 } 245 }
315 } 246 }
316 } 247 }
317 EXPORT_SYMBOL(mmc_set_data_timeout); 248 EXPORT_SYMBOL(mmc_set_data_timeout);
318 249
319 /** 250 /**
320 * __mmc_claim_host - exclusively claim a host 251 * __mmc_claim_host - exclusively claim a host
321 * @host: mmc host to claim 252 * @host: mmc host to claim
322 * @card: mmc card to claim host for 253 * @card: mmc card to claim host for
323 * 254 *
324 * Claim a host for a set of operations. If a valid card 255 * Claim a host for a set of operations. If a valid card
325 * is passed and this wasn't the last card selected, select 256 * is passed and this wasn't the last card selected, select
326 * the card before returning. 257 * the card before returning.
327 * 258 *
328 * Note: you should use mmc_card_claim_host or mmc_claim_host. 259 * Note: you should use mmc_card_claim_host or mmc_claim_host.
329 */ 260 */
330 void mmc_claim_host(struct mmc_host *host) 261 void mmc_claim_host(struct mmc_host *host)
331 { 262 {
332 DECLARE_WAITQUEUE(wait, current); 263 DECLARE_WAITQUEUE(wait, current);
333 unsigned long flags; 264 unsigned long flags;
334 265
335 add_wait_queue(&host->wq, &wait); 266 add_wait_queue(&host->wq, &wait);
336 spin_lock_irqsave(&host->lock, flags); 267 spin_lock_irqsave(&host->lock, flags);
337 while (1) { 268 while (1) {
338 set_current_state(TASK_UNINTERRUPTIBLE); 269 set_current_state(TASK_UNINTERRUPTIBLE);
339 if (!host->claimed) 270 if (!host->claimed)
340 break; 271 break;
341 spin_unlock_irqrestore(&host->lock, flags); 272 spin_unlock_irqrestore(&host->lock, flags);
342 schedule(); 273 schedule();
343 spin_lock_irqsave(&host->lock, flags); 274 spin_lock_irqsave(&host->lock, flags);
344 } 275 }
345 set_current_state(TASK_RUNNING); 276 set_current_state(TASK_RUNNING);
346 host->claimed = 1; 277 host->claimed = 1;
347 spin_unlock_irqrestore(&host->lock, flags); 278 spin_unlock_irqrestore(&host->lock, flags);
348 remove_wait_queue(&host->wq, &wait); 279 remove_wait_queue(&host->wq, &wait);
349 } 280 }
350 281
351 EXPORT_SYMBOL(mmc_claim_host); 282 EXPORT_SYMBOL(mmc_claim_host);
352 283
353 /** 284 /**
354 * mmc_release_host - release a host 285 * mmc_release_host - release a host
355 * @host: mmc host to release 286 * @host: mmc host to release
356 * 287 *
357 * Release a MMC host, allowing others to claim the host 288 * Release a MMC host, allowing others to claim the host
358 * for their operations. 289 * for their operations.
359 */ 290 */
360 void mmc_release_host(struct mmc_host *host) 291 void mmc_release_host(struct mmc_host *host)
361 { 292 {
362 unsigned long flags; 293 unsigned long flags;
363 294
364 BUG_ON(!host->claimed); 295 BUG_ON(!host->claimed);
365 296
366 spin_lock_irqsave(&host->lock, flags); 297 spin_lock_irqsave(&host->lock, flags);
367 host->claimed = 0; 298 host->claimed = 0;
368 spin_unlock_irqrestore(&host->lock, flags); 299 spin_unlock_irqrestore(&host->lock, flags);
369 300
370 wake_up(&host->wq); 301 wake_up(&host->wq);
371 } 302 }
372 303
373 EXPORT_SYMBOL(mmc_release_host); 304 EXPORT_SYMBOL(mmc_release_host);
374 305
375 static inline void mmc_set_ios(struct mmc_host *host) 306 static inline void mmc_set_ios(struct mmc_host *host)
376 { 307 {
377 struct mmc_ios *ios = &host->ios; 308 struct mmc_ios *ios = &host->ios;
378 309
379 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " 310 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
380 "width %u timing %u\n", 311 "width %u timing %u\n",
381 mmc_hostname(host), ios->clock, ios->bus_mode, 312 mmc_hostname(host), ios->clock, ios->bus_mode,
382 ios->power_mode, ios->chip_select, ios->vdd, 313 ios->power_mode, ios->chip_select, ios->vdd,
383 ios->bus_width, ios->timing); 314 ios->bus_width, ios->timing);
384 315
385 host->ops->set_ios(host, ios); 316 host->ops->set_ios(host, ios);
386 } 317 }
387 318
388 static int mmc_select_card(struct mmc_card *card) 319 void mmc_set_chip_select(struct mmc_host *host, int mode)
389 { 320 {
390 int err; 321 host->ios.chip_select = mode;
391 struct mmc_command cmd; 322 mmc_set_ios(host);
392
393 BUG_ON(!card->host->claimed);
394
395 cmd.opcode = MMC_SELECT_CARD;
396 cmd.arg = card->rca << 16;
397 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
398
399 err = mmc_wait_for_cmd(card->host, &cmd, CMD_RETRIES);
400 if (err != MMC_ERR_NONE)
401 return err;
402
403 /*
404 * We can only change the bus width of SD cards when
405 * they are selected so we have to put the handling
406 * here.
407 *
408 * The card is in 1 bit mode by default so
409 * we only need to change if it supports the
410 * wider version.
411 */
412 if (mmc_card_sd(card) &&
413 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
414 (card->host->caps & MMC_CAP_4_BIT_DATA)) {
415
416 struct mmc_command cmd;
417 cmd.opcode = SD_APP_SET_BUS_WIDTH;
418 cmd.arg = SD_BUS_WIDTH_4;
419 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
420
421 err = mmc_wait_for_app_cmd(card->host, card->rca,
422 &cmd, CMD_RETRIES);
423 if (err != MMC_ERR_NONE)
424 return err;
425
426 card->host->ios.bus_width = MMC_BUS_WIDTH_4;
427 mmc_set_ios(card->host);
428 }
429
430 return MMC_ERR_NONE;
431 } 323 }
432 324
433
434 static inline void mmc_delay(unsigned int ms)
435 {
436 if (ms < 1000 / HZ) {
437 cond_resched();
438 mdelay(ms);
439 } else {
440 msleep(ms);
441 }
442 }
443
444 /* 325 /*
445 * Mask off any voltages we don't support and select 326 * Mask off any voltages we don't support and select
446 * the lowest voltage 327 * the lowest voltage
447 */ 328 */
448 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) 329 static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
449 { 330 {
450 int bit; 331 int bit;
451 332
452 ocr &= host->ocr_avail; 333 ocr &= host->ocr_avail;
453 334
454 bit = ffs(ocr); 335 bit = ffs(ocr);
455 if (bit) { 336 if (bit) {
456 bit -= 1; 337 bit -= 1;
457 338
458 ocr &= 3 << bit; 339 ocr &= 3 << bit;
459 340
460 host->ios.vdd = bit; 341 host->ios.vdd = bit;
461 mmc_set_ios(host); 342 mmc_set_ios(host);
462 } else { 343 } else {
463 ocr = 0; 344 ocr = 0;
464 } 345 }
465 346
466 return ocr; 347 return ocr;
467 } 348 }
468 349
469 #define UNSTUFF_BITS(resp,start,size) \ 350 #define UNSTUFF_BITS(resp,start,size) \
470 ({ \ 351 ({ \
471 const int __size = size; \ 352 const int __size = size; \
472 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ 353 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
473 const int __off = 3 - ((start) / 32); \ 354 const int __off = 3 - ((start) / 32); \
474 const int __shft = (start) & 31; \ 355 const int __shft = (start) & 31; \
475 u32 __res; \ 356 u32 __res; \
476 \ 357 \
477 __res = resp[__off] >> __shft; \ 358 __res = resp[__off] >> __shft; \
478 if (__size + __shft > 32) \ 359 if (__size + __shft > 32) \
479 __res |= resp[__off-1] << ((32 - __shft) % 32); \ 360 __res |= resp[__off-1] << ((32 - __shft) % 32); \
480 __res & __mask; \ 361 __res & __mask; \
481 }) 362 })
482 363
483 /* 364 /*
484 * Given the decoded CSD structure, decode the raw CID to our CID structure. 365 * Given the decoded CSD structure, decode the raw CID to our CID structure.
485 */ 366 */
486 static void mmc_decode_cid(struct mmc_card *card) 367 static void mmc_decode_cid(struct mmc_card *card)
487 { 368 {
488 u32 *resp = card->raw_cid; 369 u32 *resp = card->raw_cid;
489 370
490 memset(&card->cid, 0, sizeof(struct mmc_cid)); 371 memset(&card->cid, 0, sizeof(struct mmc_cid));
491 372
492 if (mmc_card_sd(card)) { 373 if (mmc_card_sd(card)) {
493 /* 374 /*
494 * SD doesn't currently have a version field so we will 375 * SD doesn't currently have a version field so we will
495 * have to assume we can parse this. 376 * have to assume we can parse this.
496 */ 377 */
497 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 378 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
498 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 379 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
499 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 380 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
500 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 381 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
501 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 382 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
502 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 383 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
503 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 384 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
504 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); 385 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
505 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); 386 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
506 card->cid.serial = UNSTUFF_BITS(resp, 24, 32); 387 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
507 card->cid.year = UNSTUFF_BITS(resp, 12, 8); 388 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
508 card->cid.month = UNSTUFF_BITS(resp, 8, 4); 389 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
509 390
510 card->cid.year += 2000; /* SD cards year offset */ 391 card->cid.year += 2000; /* SD cards year offset */
511 } else { 392 } else {
512 /* 393 /*
513 * The selection of the format here is based upon published 394 * The selection of the format here is based upon published
514 * specs from sandisk and from what people have reported. 395 * specs from sandisk and from what people have reported.
515 */ 396 */
516 switch (card->csd.mmca_vsn) { 397 switch (card->csd.mmca_vsn) {
517 case 0: /* MMC v1.0 - v1.2 */ 398 case 0: /* MMC v1.0 - v1.2 */
518 case 1: /* MMC v1.4 */ 399 case 1: /* MMC v1.4 */
519 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); 400 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
520 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 401 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
521 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 402 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
522 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 403 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
523 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 404 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
524 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 405 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
525 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 406 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
526 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); 407 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
527 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); 408 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
528 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); 409 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
529 card->cid.serial = UNSTUFF_BITS(resp, 16, 24); 410 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
530 card->cid.month = UNSTUFF_BITS(resp, 12, 4); 411 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
531 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 412 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
532 break; 413 break;
533 414
534 case 2: /* MMC v2.0 - v2.2 */ 415 case 2: /* MMC v2.0 - v2.2 */
535 case 3: /* MMC v3.1 - v3.3 */ 416 case 3: /* MMC v3.1 - v3.3 */
536 case 4: /* MMC v4 */ 417 case 4: /* MMC v4 */
537 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); 418 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
538 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); 419 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
539 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); 420 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
540 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); 421 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
541 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); 422 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
542 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); 423 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
543 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); 424 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
544 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); 425 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
545 card->cid.serial = UNSTUFF_BITS(resp, 16, 32); 426 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
546 card->cid.month = UNSTUFF_BITS(resp, 12, 4); 427 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
547 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; 428 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
548 break; 429 break;
549 430
550 default: 431 default:
551 printk("%s: card has unknown MMCA version %d\n", 432 printk("%s: card has unknown MMCA version %d\n",
552 mmc_hostname(card->host), card->csd.mmca_vsn); 433 mmc_hostname(card->host), card->csd.mmca_vsn);
553 mmc_card_set_bad(card); 434 mmc_card_set_bad(card);
554 break; 435 break;
555 } 436 }
556 } 437 }
557 } 438 }
558 439
559 /* 440 /*
560 * Given a 128-bit response, decode to our card CSD structure. 441 * Given a 128-bit response, decode to our card CSD structure.
561 */ 442 */
562 static void mmc_decode_csd(struct mmc_card *card) 443 static void mmc_decode_csd(struct mmc_card *card)
563 { 444 {
564 struct mmc_csd *csd = &card->csd; 445 struct mmc_csd *csd = &card->csd;
565 unsigned int e, m, csd_struct; 446 unsigned int e, m, csd_struct;
566 u32 *resp = card->raw_csd; 447 u32 *resp = card->raw_csd;
567 448
568 if (mmc_card_sd(card)) { 449 if (mmc_card_sd(card)) {
569 csd_struct = UNSTUFF_BITS(resp, 126, 2); 450 csd_struct = UNSTUFF_BITS(resp, 126, 2);
570 451
571 switch (csd_struct) { 452 switch (csd_struct) {
572 case 0: 453 case 0:
573 m = UNSTUFF_BITS(resp, 115, 4); 454 m = UNSTUFF_BITS(resp, 115, 4);
574 e = UNSTUFF_BITS(resp, 112, 3); 455 e = UNSTUFF_BITS(resp, 112, 3);
575 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 456 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
576 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 457 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
577 458
578 m = UNSTUFF_BITS(resp, 99, 4); 459 m = UNSTUFF_BITS(resp, 99, 4);
579 e = UNSTUFF_BITS(resp, 96, 3); 460 e = UNSTUFF_BITS(resp, 96, 3);
580 csd->max_dtr = tran_exp[e] * tran_mant[m]; 461 csd->max_dtr = tran_exp[e] * tran_mant[m];
581 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 462 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
582 463
583 e = UNSTUFF_BITS(resp, 47, 3); 464 e = UNSTUFF_BITS(resp, 47, 3);
584 m = UNSTUFF_BITS(resp, 62, 12); 465 m = UNSTUFF_BITS(resp, 62, 12);
585 csd->capacity = (1 + m) << (e + 2); 466 csd->capacity = (1 + m) << (e + 2);
586 467
587 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 468 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
588 csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 469 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
589 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 470 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
590 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 471 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
591 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 472 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
592 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 473 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
593 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 474 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
594 break; 475 break;
595 case 1: 476 case 1:
596 /* 477 /*
597 * This is a block-addressed SDHC card. Most 478 * This is a block-addressed SDHC card. Most
598 * interesting fields are unused and have fixed 479 * interesting fields are unused and have fixed
599 * values. To avoid getting tripped by buggy cards, 480 * values. To avoid getting tripped by buggy cards,
600 * we assume those fixed values ourselves. 481 * we assume those fixed values ourselves.
601 */ 482 */
602 mmc_card_set_blockaddr(card); 483 mmc_card_set_blockaddr(card);
603 484
604 csd->tacc_ns = 0; /* Unused */ 485 csd->tacc_ns = 0; /* Unused */
605 csd->tacc_clks = 0; /* Unused */ 486 csd->tacc_clks = 0; /* Unused */
606 487
607 m = UNSTUFF_BITS(resp, 99, 4); 488 m = UNSTUFF_BITS(resp, 99, 4);
608 e = UNSTUFF_BITS(resp, 96, 3); 489 e = UNSTUFF_BITS(resp, 96, 3);
609 csd->max_dtr = tran_exp[e] * tran_mant[m]; 490 csd->max_dtr = tran_exp[e] * tran_mant[m];
610 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 491 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
611 492
612 m = UNSTUFF_BITS(resp, 48, 22); 493 m = UNSTUFF_BITS(resp, 48, 22);
613 csd->capacity = (1 + m) << 10; 494 csd->capacity = (1 + m) << 10;
614 495
615 csd->read_blkbits = 9; 496 csd->read_blkbits = 9;
616 csd->read_partial = 0; 497 csd->read_partial = 0;
617 csd->write_misalign = 0; 498 csd->write_misalign = 0;
618 csd->read_misalign = 0; 499 csd->read_misalign = 0;
619 csd->r2w_factor = 4; /* Unused */ 500 csd->r2w_factor = 4; /* Unused */
620 csd->write_blkbits = 9; 501 csd->write_blkbits = 9;
621 csd->write_partial = 0; 502 csd->write_partial = 0;
622 break; 503 break;
623 default: 504 default:
624 printk("%s: unrecognised CSD structure version %d\n", 505 printk("%s: unrecognised CSD structure version %d\n",
625 mmc_hostname(card->host), csd_struct); 506 mmc_hostname(card->host), csd_struct);
626 mmc_card_set_bad(card); 507 mmc_card_set_bad(card);
627 return; 508 return;
628 } 509 }
629 } else { 510 } else {
630 /* 511 /*
631 * We only understand CSD structure v1.1 and v1.2. 512 * We only understand CSD structure v1.1 and v1.2.
632 * v1.2 has extra information in bits 15, 11 and 10. 513 * v1.2 has extra information in bits 15, 11 and 10.
633 */ 514 */
634 csd_struct = UNSTUFF_BITS(resp, 126, 2); 515 csd_struct = UNSTUFF_BITS(resp, 126, 2);
635 if (csd_struct != 1 && csd_struct != 2) { 516 if (csd_struct != 1 && csd_struct != 2) {
636 printk("%s: unrecognised CSD structure version %d\n", 517 printk("%s: unrecognised CSD structure version %d\n",
637 mmc_hostname(card->host), csd_struct); 518 mmc_hostname(card->host), csd_struct);
638 mmc_card_set_bad(card); 519 mmc_card_set_bad(card);
639 return; 520 return;
640 } 521 }
641 522
642 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); 523 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
643 m = UNSTUFF_BITS(resp, 115, 4); 524 m = UNSTUFF_BITS(resp, 115, 4);
644 e = UNSTUFF_BITS(resp, 112, 3); 525 e = UNSTUFF_BITS(resp, 112, 3);
645 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; 526 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
646 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 527 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
647 528
648 m = UNSTUFF_BITS(resp, 99, 4); 529 m = UNSTUFF_BITS(resp, 99, 4);
649 e = UNSTUFF_BITS(resp, 96, 3); 530 e = UNSTUFF_BITS(resp, 96, 3);
650 csd->max_dtr = tran_exp[e] * tran_mant[m]; 531 csd->max_dtr = tran_exp[e] * tran_mant[m];
651 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 532 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
652 533
653 e = UNSTUFF_BITS(resp, 47, 3); 534 e = UNSTUFF_BITS(resp, 47, 3);
654 m = UNSTUFF_BITS(resp, 62, 12); 535 m = UNSTUFF_BITS(resp, 62, 12);
655 csd->capacity = (1 + m) << (e + 2); 536 csd->capacity = (1 + m) << (e + 2);
656 537
657 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); 538 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
658 csd->read_partial = UNSTUFF_BITS(resp, 79, 1); 539 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
659 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); 540 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
660 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); 541 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
661 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 542 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
662 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 543 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
663 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 544 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
664 } 545 }
665 } 546 }
666 547
667 /* 548 /*
668 * Given a 64-bit response, decode to our card SCR structure. 549 * Given a 64-bit response, decode to our card SCR structure.
669 */ 550 */
670 static void mmc_decode_scr(struct mmc_card *card) 551 static void mmc_decode_scr(struct mmc_card *card)
671 { 552 {
672 struct sd_scr *scr = &card->scr; 553 struct sd_scr *scr = &card->scr;
673 unsigned int scr_struct; 554 unsigned int scr_struct;
674 u32 resp[4]; 555 u32 resp[4];
675 556
676 BUG_ON(!mmc_card_sd(card)); 557 BUG_ON(!mmc_card_sd(card));
677 558
678 resp[3] = card->raw_scr[1]; 559 resp[3] = card->raw_scr[1];
679 resp[2] = card->raw_scr[0]; 560 resp[2] = card->raw_scr[0];
680 561
681 scr_struct = UNSTUFF_BITS(resp, 60, 4); 562 scr_struct = UNSTUFF_BITS(resp, 60, 4);
682 if (scr_struct != 0) { 563 if (scr_struct != 0) {
683 printk("%s: unrecognised SCR structure version %d\n", 564 printk("%s: unrecognised SCR structure version %d\n",
684 mmc_hostname(card->host), scr_struct); 565 mmc_hostname(card->host), scr_struct);
685 mmc_card_set_bad(card); 566 mmc_card_set_bad(card);
686 return; 567 return;
687 } 568 }
688 569
689 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 570 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
690 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 571 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
691 } 572 }
692 573
693 /* 574 /*
694 * Allocate a new MMC card 575 * Allocate a new MMC card
695 */ 576 */
696 static struct mmc_card * 577 static struct mmc_card *
697 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid) 578 mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
698 { 579 {
699 struct mmc_card *card; 580 struct mmc_card *card;
700 581
701 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); 582 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
702 if (!card) 583 if (!card)
703 return ERR_PTR(-ENOMEM); 584 return ERR_PTR(-ENOMEM);
704 585
705 mmc_init_card(card, host); 586 mmc_init_card(card, host);
706 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); 587 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
707 588
708 return card; 589 return card;
709 } 590 }
710 591
711 /* 592 /*
712 * Tell attached cards to go to IDLE state
713 */
714 static void mmc_idle_cards(struct mmc_host *host)
715 {
716 struct mmc_command cmd;
717
718 host->ios.chip_select = MMC_CS_HIGH;
719 mmc_set_ios(host);
720
721 mmc_delay(1);
722
723 cmd.opcode = MMC_GO_IDLE_STATE;
724 cmd.arg = 0;
725 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
726
727 mmc_wait_for_cmd(host, &cmd, 0);
728
729 mmc_delay(1);
730
731 host->ios.chip_select = MMC_CS_DONTCARE;
732 mmc_set_ios(host);
733
734 mmc_delay(1);
735 }
736
737 /*
738 * Apply power to the MMC stack. This is a two-stage process. 593 * Apply power to the MMC stack. This is a two-stage process.
739 * First, we enable power to the card without the clock running. 594 * First, we enable power to the card without the clock running.
740 * We then wait a bit for the power to stabilise. Finally, 595 * We then wait a bit for the power to stabilise. Finally,
741 * enable the bus drivers and clock to the card. 596 * enable the bus drivers and clock to the card.
742 * 597 *
743 * We must _NOT_ enable the clock prior to power stablising. 598 * We must _NOT_ enable the clock prior to power stablising.
744 * 599 *
745 * If a host does all the power sequencing itself, ignore the 600 * If a host does all the power sequencing itself, ignore the
746 * initial MMC_POWER_UP stage. 601 * initial MMC_POWER_UP stage.
747 */ 602 */
748 static void mmc_power_up(struct mmc_host *host) 603 static void mmc_power_up(struct mmc_host *host)
749 { 604 {
750 int bit = fls(host->ocr_avail) - 1; 605 int bit = fls(host->ocr_avail) - 1;
751 606
752 host->ios.vdd = bit; 607 host->ios.vdd = bit;
753 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 608 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
754 host->ios.chip_select = MMC_CS_DONTCARE; 609 host->ios.chip_select = MMC_CS_DONTCARE;
755 host->ios.power_mode = MMC_POWER_UP; 610 host->ios.power_mode = MMC_POWER_UP;
756 host->ios.bus_width = MMC_BUS_WIDTH_1; 611 host->ios.bus_width = MMC_BUS_WIDTH_1;
757 host->ios.timing = MMC_TIMING_LEGACY; 612 host->ios.timing = MMC_TIMING_LEGACY;
758 mmc_set_ios(host); 613 mmc_set_ios(host);
759 614
760 mmc_delay(1); 615 mmc_delay(1);
761 616
762 host->ios.clock = host->f_min; 617 host->ios.clock = host->f_min;
763 host->ios.power_mode = MMC_POWER_ON; 618 host->ios.power_mode = MMC_POWER_ON;
764 mmc_set_ios(host); 619 mmc_set_ios(host);
765 620
766 mmc_delay(2); 621 mmc_delay(2);
767 } 622 }
768 623
769 static void mmc_power_off(struct mmc_host *host) 624 static void mmc_power_off(struct mmc_host *host)
770 { 625 {
771 host->ios.clock = 0; 626 host->ios.clock = 0;
772 host->ios.vdd = 0; 627 host->ios.vdd = 0;
773 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 628 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
774 host->ios.chip_select = MMC_CS_DONTCARE; 629 host->ios.chip_select = MMC_CS_DONTCARE;
775 host->ios.power_mode = MMC_POWER_OFF; 630 host->ios.power_mode = MMC_POWER_OFF;
776 host->ios.bus_width = MMC_BUS_WIDTH_1; 631 host->ios.bus_width = MMC_BUS_WIDTH_1;
777 host->ios.timing = MMC_TIMING_LEGACY; 632 host->ios.timing = MMC_TIMING_LEGACY;
778 mmc_set_ios(host); 633 mmc_set_ios(host);
779 } 634 }
780 635
781 static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
782 {
783 struct mmc_command cmd;
784 int i, err = 0;
785
786 cmd.opcode = MMC_SEND_OP_COND;
787 cmd.arg = ocr;
788 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
789
790 for (i = 100; i; i--) {
791 err = mmc_wait_for_cmd(host, &cmd, 0);
792 if (err != MMC_ERR_NONE)
793 break;
794
795 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
796 break;
797
798 err = MMC_ERR_TIMEOUT;
799
800 mmc_delay(10);
801 }
802
803 if (rocr)
804 *rocr = cmd.resp[0];
805
806 return err;
807 }
808
809 static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
810 {
811 struct mmc_command cmd;
812 int i, err = 0;
813
814 cmd.opcode = SD_APP_OP_COND;
815 cmd.arg = ocr;
816 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
817
818 for (i = 100; i; i--) {
819 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
820 if (err != MMC_ERR_NONE)
821 break;
822
823 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
824 break;
825
826 err = MMC_ERR_TIMEOUT;
827
828 mmc_delay(10);
829 }
830
831 if (rocr)
832 *rocr = cmd.resp[0];
833
834 return err;
835 }
836
837 static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
838 {
839 struct mmc_command cmd;
840 int err, sd2;
841 static const u8 test_pattern = 0xAA;
842
843 /*
844 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
845 * before SD_APP_OP_COND. This command will harmlessly fail for
846 * SD 1.0 cards.
847 */
848 cmd.opcode = SD_SEND_IF_COND;
849 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
850 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
851
852 err = mmc_wait_for_cmd(host, &cmd, 0);
853 if (err == MMC_ERR_NONE) {
854 if ((cmd.resp[0] & 0xFF) == test_pattern) {
855 sd2 = 1;
856 } else {
857 sd2 = 0;
858 err = MMC_ERR_FAILED;
859 }
860 } else {
861 /*
862 * Treat errors as SD 1.0 card.
863 */
864 sd2 = 0;
865 err = MMC_ERR_NONE;
866 }
867 if (rsd2)
868 *rsd2 = sd2;
869 return err;
870 }
871
872 /* 636 /*
873 * Discover the card by requesting its CID. 637 * Discover the card by requesting its CID.
874 * 638 *
875 * Create a mmc_card entry for the discovered card, assigning 639 * Create a mmc_card entry for the discovered card, assigning
876 * it an RCA, and save the raw CID for decoding later. 640 * it an RCA, and save the raw CID for decoding later.
877 */ 641 */
878 static void mmc_discover_card(struct mmc_host *host) 642 static void mmc_discover_card(struct mmc_host *host)
879 { 643 {
880 unsigned int err; 644 unsigned int err;
645 u32 cid[4];
881 646
882 struct mmc_command cmd;
883
884 BUG_ON(host->card); 647 BUG_ON(host->card);
885 648
886 cmd.opcode = MMC_ALL_SEND_CID; 649 err = mmc_all_send_cid(host, cid);
887 cmd.arg = 0;
888 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
889
890 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
891 if (err == MMC_ERR_TIMEOUT) {
892 err = MMC_ERR_NONE;
893 return;
894 }
895 if (err != MMC_ERR_NONE) { 650 if (err != MMC_ERR_NONE) {
896 printk(KERN_ERR "%s: error requesting CID: %d\n", 651 printk(KERN_ERR "%s: error requesting CID: %d\n",
897 mmc_hostname(host), err); 652 mmc_hostname(host), err);
898 return; 653 return;
899 } 654 }
900 655
901 host->card = mmc_alloc_card(host, cmd.resp); 656 host->card = mmc_alloc_card(host, cid);
902 if (IS_ERR(host->card)) { 657 if (IS_ERR(host->card)) {
903 err = PTR_ERR(host->card); 658 err = PTR_ERR(host->card);
904 host->card = NULL; 659 host->card = NULL;
905 return; 660 return;
906 } 661 }
907 662
908 if (host->mode == MMC_MODE_SD) { 663 if (host->mode == MMC_MODE_SD) {
909 host->card->type = MMC_TYPE_SD; 664 host->card->type = MMC_TYPE_SD;
910 665
911 cmd.opcode = SD_SEND_RELATIVE_ADDR; 666 err = mmc_send_relative_addr(host, &host->card->rca);
912 cmd.arg = 0;
913 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
914
915 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
916 if (err != MMC_ERR_NONE) 667 if (err != MMC_ERR_NONE)
917 mmc_card_set_dead(host->card); 668 mmc_card_set_dead(host->card);
918 else { 669 else {
919 host->card->rca = cmd.resp[0] >> 16;
920
921 if (!host->ops->get_ro) { 670 if (!host->ops->get_ro) {
922 printk(KERN_WARNING "%s: host does not " 671 printk(KERN_WARNING "%s: host does not "
923 "support reading read-only " 672 "support reading read-only "
924 "switch. assuming write-enable.\n", 673 "switch. assuming write-enable.\n",
925 mmc_hostname(host)); 674 mmc_hostname(host));
926 } else { 675 } else {
927 if (host->ops->get_ro(host)) 676 if (host->ops->get_ro(host))
928 mmc_card_set_readonly(host->card); 677 mmc_card_set_readonly(host->card);
929 } 678 }
930 } 679 }
931 } else { 680 } else {
932 host->card->type = MMC_TYPE_MMC; 681 host->card->type = MMC_TYPE_MMC;
933 host->card->rca = 1; 682 host->card->rca = 1;
934 683
935 cmd.opcode = MMC_SET_RELATIVE_ADDR; 684 err = mmc_set_relative_addr(host->card);
936 cmd.arg = host->card->rca << 16;
937 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
938
939 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
940 if (err != MMC_ERR_NONE) 685 if (err != MMC_ERR_NONE)
941 mmc_card_set_dead(host->card); 686 mmc_card_set_dead(host->card);
942 } 687 }
943 } 688 }
944 689
945 static void mmc_read_csd(struct mmc_host *host) 690 static void mmc_read_csd(struct mmc_host *host)
946 { 691 {
947 struct mmc_command cmd;
948 int err; 692 int err;
949 693
950 if (!host->card) 694 if (!host->card)
951 return; 695 return;
952 if (mmc_card_dead(host->card)) 696 if (mmc_card_dead(host->card))
953 return; 697 return;
954 698
955 cmd.opcode = MMC_SEND_CSD; 699 err = mmc_send_csd(host->card, host->card->raw_csd);
956 cmd.arg = host->card->rca << 16;
957 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
958
959 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
960 if (err != MMC_ERR_NONE) { 700 if (err != MMC_ERR_NONE) {
961 mmc_card_set_dead(host->card); 701 mmc_card_set_dead(host->card);
962 return; 702 return;
963 } 703 }
964 704
965 memcpy(host->card->raw_csd, cmd.resp, sizeof(host->card->raw_csd));
966
967 mmc_decode_csd(host->card); 705 mmc_decode_csd(host->card);
968 mmc_decode_cid(host->card); 706 mmc_decode_cid(host->card);
969 } 707 }
970 708
971 static void mmc_process_ext_csd(struct mmc_host *host) 709 static void mmc_process_ext_csd(struct mmc_host *host)
972 { 710 {
973 int err; 711 int err;
974
975 struct mmc_request mrq;
976 struct mmc_command cmd;
977 struct mmc_data data;
978
979 u8 *ext_csd; 712 u8 *ext_csd;
980 struct scatterlist sg;
981 713
982 if (!host->card) 714 if (!host->card)
983 return; 715 return;
984 if (mmc_card_dead(host->card)) 716 if (mmc_card_dead(host->card))
985 return; 717 return;
986 if (mmc_card_sd(host->card)) 718 if (mmc_card_sd(host->card))
987 return; 719 return;
988 if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4) 720 if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
989 return; 721 return;
990 722
991 /* 723 /*
992 * As the ext_csd is so large and mostly unused, we don't store the 724 * As the ext_csd is so large and mostly unused, we don't store the
993 * raw block in mmc_card. 725 * raw block in mmc_card.
994 */ 726 */
995 ext_csd = kmalloc(512, GFP_KERNEL); 727 ext_csd = kmalloc(512, GFP_KERNEL);
996 if (!ext_csd) { 728 if (!ext_csd) {
997 printk("%s: could not allocate a buffer to receive the ext_csd." 729 printk("%s: could not allocate a buffer to receive the ext_csd."
998 "mmc v4 cards will be treated as v3.\n", 730 "mmc v4 cards will be treated as v3.\n",
999 mmc_hostname(host)); 731 mmc_hostname(host));
1000 return; 732 return;
1001 } 733 }
1002 734
1003 memset(&cmd, 0, sizeof(struct mmc_command)); 735 err = mmc_send_ext_csd(host->card, ext_csd);
1004 736 if (err != MMC_ERR_NONE) {
1005 cmd.opcode = MMC_SEND_EXT_CSD;
1006 cmd.arg = 0;
1007 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1008
1009 memset(&data, 0, sizeof(struct mmc_data));
1010
1011 mmc_set_data_timeout(&data, host->card, 0);
1012
1013 data.blksz = 512;
1014 data.blocks = 1;
1015 data.flags = MMC_DATA_READ;
1016 data.sg = &sg;
1017 data.sg_len = 1;
1018
1019 memset(&mrq, 0, sizeof(struct mmc_request));
1020
1021 mrq.cmd = &cmd;
1022 mrq.data = &data;
1023
1024 sg_init_one(&sg, ext_csd, 512);
1025
1026 mmc_wait_for_req(host, &mrq);
1027
1028 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1029 if (host->card->csd.capacity == (4096 * 512)) { 737 if (host->card->csd.capacity == (4096 * 512)) {
1030 printk(KERN_ERR "%s: unable to read EXT_CSD " 738 printk(KERN_ERR "%s: unable to read EXT_CSD "
1031 "on a possible high capacity card. " 739 "on a possible high capacity card. "
1032 "Card will be ignored.\n", 740 "Card will be ignored.\n",
1033 mmc_hostname(host)); 741 mmc_hostname(host));
1034 mmc_card_set_dead(host->card); 742 mmc_card_set_dead(host->card);
1035 } else { 743 } else {
1036 printk(KERN_WARNING "%s: unable to read " 744 printk(KERN_WARNING "%s: unable to read "
1037 "EXT_CSD, performance might " 745 "EXT_CSD, performance might "
1038 "suffer.\n", 746 "suffer.\n",
1039 mmc_hostname(host)); 747 mmc_hostname(host));
1040 } 748 }
1041 goto out; 749 goto out;
1042 } 750 }
1043 751
1044 host->card->ext_csd.sectors = 752 host->card->ext_csd.sectors =
1045 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | 753 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
1046 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 754 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
1047 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 755 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
1048 ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 756 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1049 if (host->card->ext_csd.sectors) 757 if (host->card->ext_csd.sectors)
1050 mmc_card_set_blockaddr(host->card); 758 mmc_card_set_blockaddr(host->card);
1051 759
1052 switch (ext_csd[EXT_CSD_CARD_TYPE]) { 760 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
1053 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: 761 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
1054 host->card->ext_csd.hs_max_dtr = 52000000; 762 host->card->ext_csd.hs_max_dtr = 52000000;
1055 break; 763 break;
1056 case EXT_CSD_CARD_TYPE_26: 764 case EXT_CSD_CARD_TYPE_26:
1057 host->card->ext_csd.hs_max_dtr = 26000000; 765 host->card->ext_csd.hs_max_dtr = 26000000;
1058 break; 766 break;
1059 default: 767 default:
1060 /* MMC v4 spec says this cannot happen */ 768 /* MMC v4 spec says this cannot happen */
1061 printk("%s: card is mmc v4 but doesn't support " 769 printk("%s: card is mmc v4 but doesn't support "
1062 "any high-speed modes.\n", 770 "any high-speed modes.\n",
1063 mmc_hostname(host)); 771 mmc_hostname(host));
1064 goto out; 772 goto out;
1065 } 773 }
1066 774
1067 if (host->caps & MMC_CAP_MMC_HIGHSPEED) { 775 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
1068 /* Activate highspeed support. */ 776 /* Activate highspeed support. */
1069 cmd.opcode = MMC_SWITCH; 777 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
1070 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 778 EXT_CSD_HS_TIMING, 1);
1071 (EXT_CSD_HS_TIMING << 16) |
1072 (1 << 8) |
1073 EXT_CSD_CMD_SET_NORMAL;
1074 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1075
1076 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1077 if (err != MMC_ERR_NONE) { 779 if (err != MMC_ERR_NONE) {
1078 printk("%s: failed to switch card to mmc v4 " 780 printk("%s: failed to switch card to mmc v4 "
1079 "high-speed mode.\n", 781 "high-speed mode.\n",
1080 mmc_hostname(host)); 782 mmc_hostname(host));
1081 goto out; 783 goto out;
1082 } 784 }
1083 785
1084 mmc_card_set_highspeed(host->card); 786 mmc_card_set_highspeed(host->card);
1085 787
1086 host->ios.timing = MMC_TIMING_MMC_HS; 788 host->ios.timing = MMC_TIMING_MMC_HS;
1087 mmc_set_ios(host); 789 mmc_set_ios(host);
1088 } 790 }
1089 791
1090 /* Check for host support for wide-bus modes. */ 792 /* Check for host support for wide-bus modes. */
1091 if (host->caps & MMC_CAP_4_BIT_DATA) { 793 if (host->caps & MMC_CAP_4_BIT_DATA) {
1092 /* Activate 4-bit support. */ 794 /* Activate 4-bit support. */
1093 cmd.opcode = MMC_SWITCH; 795 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
1094 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 796 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |
1095 (EXT_CSD_BUS_WIDTH << 16) | 797 EXT_CSD_CMD_SET_NORMAL);
1096 (EXT_CSD_BUS_WIDTH_4 << 8) |
1097 EXT_CSD_CMD_SET_NORMAL;
1098 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1099
1100 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1101 if (err != MMC_ERR_NONE) { 798 if (err != MMC_ERR_NONE) {
1102 printk("%s: failed to switch card to " 799 printk("%s: failed to switch card to "
1103 "mmc v4 4-bit bus mode.\n", 800 "mmc v4 4-bit bus mode.\n",
1104 mmc_hostname(host)); 801 mmc_hostname(host));
1105 goto out; 802 goto out;
1106 } 803 }
1107 804
1108 host->ios.bus_width = MMC_BUS_WIDTH_4; 805 host->ios.bus_width = MMC_BUS_WIDTH_4;
1109 mmc_set_ios(host); 806 mmc_set_ios(host);
1110 } 807 }
1111 808
1112 out: 809 out:
1113 kfree(ext_csd); 810 kfree(ext_csd);
1114 } 811 }
1115 812
1116 static void mmc_read_scr(struct mmc_host *host) 813 static void mmc_read_scr(struct mmc_host *host)
1117 { 814 {
1118 int err; 815 int err;
1119 struct mmc_request mrq;
1120 struct mmc_command cmd;
1121 struct mmc_data data;
1122 struct scatterlist sg;
1123 816
1124 if (!host->card) 817 if (!host->card)
1125 return; 818 return;
1126 if (mmc_card_dead(host->card)) 819 if (mmc_card_dead(host->card))
1127 return; 820 return;
1128 if (!mmc_card_sd(host->card)) 821 if (!mmc_card_sd(host->card))
1129 return; 822 return;
1130 823
1131 memset(&cmd, 0, sizeof(struct mmc_command)); 824 err = mmc_app_send_scr(host->card, host->card->raw_scr);
1132 825 if (err != MMC_ERR_NONE) {
1133 cmd.opcode = MMC_APP_CMD;
1134 cmd.arg = host->card->rca << 16;
1135 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1136
1137 err = mmc_wait_for_cmd(host, &cmd, 0);
1138 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1139 mmc_card_set_dead(host->card); 826 mmc_card_set_dead(host->card);
1140 return; 827 return;
1141 } 828 }
1142 829
1143 memset(&cmd, 0, sizeof(struct mmc_command));
1144
1145 cmd.opcode = SD_APP_SEND_SCR;
1146 cmd.arg = 0;
1147 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1148
1149 memset(&data, 0, sizeof(struct mmc_data));
1150
1151 mmc_set_data_timeout(&data, host->card, 0);
1152
1153 data.blksz = 1 << 3;
1154 data.blocks = 1;
1155 data.flags = MMC_DATA_READ;
1156 data.sg = &sg;
1157 data.sg_len = 1;
1158
1159 memset(&mrq, 0, sizeof(struct mmc_request));
1160
1161 mrq.cmd = &cmd;
1162 mrq.data = &data;
1163
1164 sg_init_one(&sg, (u8*)host->card->raw_scr, 8);
1165
1166 mmc_wait_for_req(host, &mrq);
1167
1168 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1169 mmc_card_set_dead(host->card);
1170 return;
1171 }
1172
1173 host->card->raw_scr[0] = ntohl(host->card->raw_scr[0]);
1174 host->card->raw_scr[1] = ntohl(host->card->raw_scr[1]);
1175
1176 mmc_decode_scr(host->card); 830 mmc_decode_scr(host->card);
1177 } 831 }
1178 832
1179 static void mmc_read_switch_caps(struct mmc_host *host) 833 static void mmc_read_switch_caps(struct mmc_host *host)
1180 { 834 {
1181 struct mmc_request mrq; 835 int err;
1182 struct mmc_command cmd;
1183 struct mmc_data data;
1184 unsigned char *status; 836 unsigned char *status;
1185 struct scatterlist sg;
1186 837
1187 if (!(host->caps & MMC_CAP_SD_HIGHSPEED)) 838 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
1188 return; 839 return;
1189 840
1190 if (!host->card) 841 if (!host->card)
1191 return; 842 return;
1192 if (mmc_card_dead(host->card)) 843 if (mmc_card_dead(host->card))
1193 return; 844 return;
1194 if (!mmc_card_sd(host->card)) 845 if (!mmc_card_sd(host->card))
1195 return; 846 return;
1196 if (host->card->scr.sda_vsn < SCR_SPEC_VER_1) 847 if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
1197 return; 848 return;
1198 849
1199 status = kmalloc(64, GFP_KERNEL); 850 status = kmalloc(64, GFP_KERNEL);
1200 if (!status) { 851 if (!status) {
1201 printk(KERN_WARNING "%s: Unable to allocate buffer for " 852 printk(KERN_WARNING "%s: Unable to allocate buffer for "
1202 "reading switch capabilities.\n", 853 "reading switch capabilities.\n",
1203 mmc_hostname(host)); 854 mmc_hostname(host));
1204 return; 855 return;
1205 } 856 }
1206 857
1207 memset(&cmd, 0, sizeof(struct mmc_command)); 858 err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,
1208 859 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
1209 cmd.opcode = SD_SWITCH; 860 if (err != MMC_ERR_NONE) {
1210 cmd.arg = 0x00FFFFF1;
1211 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1212
1213 memset(&data, 0, sizeof(struct mmc_data));
1214
1215 mmc_set_data_timeout(&data, host->card, 0);
1216
1217 data.blksz = 64;
1218 data.blocks = 1;
1219 data.flags = MMC_DATA_READ;
1220 data.sg = &sg;
1221 data.sg_len = 1;
1222
1223 memset(&mrq, 0, sizeof(struct mmc_request));
1224
1225 mrq.cmd = &cmd;
1226 mrq.data = &data;
1227
1228 sg_init_one(&sg, status, 64);
1229
1230 mmc_wait_for_req(host, &mrq);
1231
1232 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
1233 printk("%s: unable to read switch capabilities, " 861 printk("%s: unable to read switch capabilities, "
1234 "performance might suffer.\n", 862 "performance might suffer.\n",
1235 mmc_hostname(host)); 863 mmc_hostname(host));
1236 goto out; 864 goto out;
1237 } 865 }
1238 866
1239 if (status[13] & 0x02) 867 if (status[13] & 0x02)
1240 host->card->sw_caps.hs_max_dtr = 50000000; 868 host->card->sw_caps.hs_max_dtr = 50000000;
1241 869
1242 memset(&cmd, 0, sizeof(struct mmc_command)); 870 err = mmc_sd_switch(host->card, SD_SWITCH_SET,
1243 871 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
1244 cmd.opcode = SD_SWITCH; 872 if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {
1245 cmd.arg = 0x80FFFFF1;
1246 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1247
1248 memset(&data, 0, sizeof(struct mmc_data));
1249
1250 mmc_set_data_timeout(&data, host->card, 0);
1251
1252 data.blksz = 64;
1253 data.blocks = 1;
1254 data.flags = MMC_DATA_READ;
1255 data.sg = &sg;
1256 data.sg_len = 1;
1257
1258 memset(&mrq, 0, sizeof(struct mmc_request));
1259
1260 mrq.cmd = &cmd;
1261 mrq.data = &data;
1262
1263 sg_init_one(&sg, status, 64);
1264
1265 mmc_wait_for_req(host, &mrq);
1266
1267 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE ||
1268 (status[16] & 0xF) != 1) {
1269 printk(KERN_WARNING "%s: Problem switching card " 873 printk(KERN_WARNING "%s: Problem switching card "
1270 "into high-speed mode!\n", 874 "into high-speed mode!\n",
1271 mmc_hostname(host)); 875 mmc_hostname(host));
1272 goto out; 876 goto out;
1273 } 877 }
1274 878
1275 mmc_card_set_highspeed(host->card); 879 mmc_card_set_highspeed(host->card);
1276 880
1277 host->ios.timing = MMC_TIMING_SD_HS; 881 host->ios.timing = MMC_TIMING_SD_HS;
1278 mmc_set_ios(host); 882 mmc_set_ios(host);
1279 883
1280 out: 884 out:
1281 kfree(status); 885 kfree(status);
1282 } 886 }
1283 887
1284 static unsigned int mmc_calculate_clock(struct mmc_host *host) 888 static unsigned int mmc_calculate_clock(struct mmc_host *host)
1285 { 889 {
1286 unsigned int max_dtr = host->f_max; 890 unsigned int max_dtr = host->f_max;
1287 891
1288 if (host->card && !mmc_card_dead(host->card)) { 892 if (host->card && !mmc_card_dead(host->card)) {
1289 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) { 893 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
1290 if (max_dtr > host->card->sw_caps.hs_max_dtr) 894 if (max_dtr > host->card->sw_caps.hs_max_dtr)
1291 max_dtr = host->card->sw_caps.hs_max_dtr; 895 max_dtr = host->card->sw_caps.hs_max_dtr;
1292 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) { 896 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
1293 if (max_dtr > host->card->ext_csd.hs_max_dtr) 897 if (max_dtr > host->card->ext_csd.hs_max_dtr)
1294 max_dtr = host->card->ext_csd.hs_max_dtr; 898 max_dtr = host->card->ext_csd.hs_max_dtr;
1295 } else if (max_dtr > host->card->csd.max_dtr) { 899 } else if (max_dtr > host->card->csd.max_dtr) {
1296 max_dtr = host->card->csd.max_dtr; 900 max_dtr = host->card->csd.max_dtr;
1297 } 901 }
1298 } 902 }
1299 903
1300 pr_debug("%s: selected %d.%03dMHz transfer rate\n", 904 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1301 mmc_hostname(host), 905 mmc_hostname(host),
1302 max_dtr / 1000000, (max_dtr / 1000) % 1000); 906 max_dtr / 1000000, (max_dtr / 1000) % 1000);
1303 907
1304 return max_dtr; 908 return max_dtr;
1305 } 909 }
1306 910
1307 /* 911 /*
1308 * Check whether cards we already know about are still present. 912 * Check whether cards we already know about are still present.
1309 * We do this by requesting status, and checking whether a card 913 * We do this by requesting status, and checking whether a card
1310 * responds. 914 * responds.
1311 * 915 *
1312 * A request for status does not cause a state change in data 916 * A request for status does not cause a state change in data
1313 * transfer mode. 917 * transfer mode.
1314 */ 918 */
1315 static void mmc_check_card(struct mmc_card *card) 919 static void mmc_check_card(struct mmc_card *card)
1316 { 920 {
1317 struct mmc_command cmd;
1318 int err; 921 int err;
1319 922
1320 BUG_ON(!card); 923 BUG_ON(!card);
1321 924
1322 cmd.opcode = MMC_SEND_STATUS; 925 err = mmc_send_status(card, NULL);
1323 cmd.arg = card->rca << 16;
1324 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1325
1326 err = mmc_wait_for_cmd(card->host, &cmd, CMD_RETRIES);
1327 if (err == MMC_ERR_NONE) 926 if (err == MMC_ERR_NONE)
1328 return; 927 return;
1329 928
1330 mmc_card_set_dead(card); 929 mmc_card_set_dead(card);
1331 } 930 }
1332 931
1333 static void mmc_setup(struct mmc_host *host) 932 static void mmc_setup(struct mmc_host *host)
1334 { 933 {
1335 int err; 934 int err;
1336 u32 ocr; 935 u32 ocr;
1337 936
1338 host->mode = MMC_MODE_SD; 937 host->mode = MMC_MODE_SD;
1339 938
1340 mmc_power_up(host); 939 mmc_power_up(host);
1341 mmc_idle_cards(host); 940 mmc_go_idle(host);
1342 941
1343 err = mmc_send_if_cond(host, host->ocr_avail, NULL); 942 err = mmc_send_if_cond(host, host->ocr_avail);
1344 if (err != MMC_ERR_NONE) { 943 if (err != MMC_ERR_NONE) {
1345 return; 944 return;
1346 } 945 }
1347 err = mmc_send_app_op_cond(host, 0, &ocr); 946 err = mmc_send_app_op_cond(host, 0, &ocr);
1348 947
1349 /* 948 /*
1350 * If we fail to detect any SD cards then try 949 * If we fail to detect any SD cards then try
1351 * searching for MMC cards. 950 * searching for MMC cards.
1352 */ 951 */
1353 if (err != MMC_ERR_NONE) { 952 if (err != MMC_ERR_NONE) {
1354 host->mode = MMC_MODE_MMC; 953 host->mode = MMC_MODE_MMC;
1355 954
1356 err = mmc_send_op_cond(host, 0, &ocr); 955 err = mmc_send_op_cond(host, 0, &ocr);
1357 if (err != MMC_ERR_NONE) 956 if (err != MMC_ERR_NONE)
1358 return; 957 return;
1359 } 958 }
1360 959
1361 host->ocr = mmc_select_voltage(host, ocr); 960 host->ocr = mmc_select_voltage(host, ocr);
1362 961
1363 if (host->ocr == 0) 962 if (host->ocr == 0)
1364 return; 963 return;
1365 964
1366 /* 965 /*
1367 * Since we're changing the OCR value, we seem to 966 * Since we're changing the OCR value, we seem to
1368 * need to tell some cards to go back to the idle 967 * need to tell some cards to go back to the idle
1369 * state. We wait 1ms to give cards time to 968 * state. We wait 1ms to give cards time to
1370 * respond. 969 * respond.
1371 */ 970 */
1372 mmc_idle_cards(host); 971 mmc_go_idle(host);
1373 972
1374 /* 973 /*
1375 * Send the selected OCR multiple times... until the cards 974 * Send the selected OCR multiple times... until the cards
1376 * all get the idea that they should be ready for CMD2. 975 * all get the idea that they should be ready for CMD2.
1377 * (My SanDisk card seems to need this.) 976 * (My SanDisk card seems to need this.)
1378 */ 977 */
1379 if (host->mode == MMC_MODE_SD) { 978 if (host->mode == MMC_MODE_SD) {
1380 int err, sd2; 979 /*
1381 err = mmc_send_if_cond(host, host->ocr, &sd2); 980 * If SD_SEND_IF_COND indicates an SD 2.0
1382 if (err == MMC_ERR_NONE) { 981 * compliant card and we should set bit 30
1383 /* 982 * of the ocr to indicate that we can handle
1384 * If SD_SEND_IF_COND indicates an SD 2.0 983 * block-addressed SDHC cards.
1385 * compliant card and we should set bit 30 984 */
1386 * of the ocr to indicate that we can handle 985 err = mmc_send_if_cond(host, host->ocr);
1387 * block-addressed SDHC cards. 986 if (err == MMC_ERR_NONE)
1388 */ 987 ocr = host->ocr | (1 << 30);
1389 mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL); 988
1390 } 989 mmc_send_app_op_cond(host, ocr, NULL);
1391 } else { 990 } else {
1392 /* The extra bit indicates that we support high capacity */ 991 /* The extra bit indicates that we support high capacity */
1393 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL); 992 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
1394 } 993 }
1395 994
1396 mmc_discover_card(host); 995 mmc_discover_card(host);
1397 996
1398 /* 997 /*
1399 * Ok, now switch to push-pull mode. 998 * Ok, now switch to push-pull mode.
1400 */ 999 */
1401 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 1000 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1402 mmc_set_ios(host); 1001 mmc_set_ios(host);
1403 1002
1404 mmc_read_csd(host); 1003 mmc_read_csd(host);
1405 1004
1406 if (host->card && !mmc_card_dead(host->card)) { 1005 if (host->card && !mmc_card_dead(host->card)) {
1407 err = mmc_select_card(host->card); 1006 err = mmc_select_card(host->card);
1408 if (err != MMC_ERR_NONE) 1007 if (err != MMC_ERR_NONE)
1409 mmc_card_set_dead(host->card); 1008 mmc_card_set_dead(host->card);
1009 }
1010
1011 /*
1012 * The card is in 1 bit mode by default so
1013 * we only need to change if it supports the
1014 * wider version.
1015 */
1016 if (host->card && !mmc_card_dead(host->card) &&
1017 mmc_card_sd(host->card) &&
1018 (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
1019 (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {
1020 err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);
1021 if (err != MMC_ERR_NONE)
1022 mmc_card_set_dead(host->card);
1023 else {
1024 host->ios.bus_width = MMC_BUS_WIDTH_4;
1025 mmc_set_ios(host);
1026 }
1410 } 1027 }
1411 1028
1412 if (host->mode == MMC_MODE_SD) { 1029 if (host->mode == MMC_MODE_SD) {
1413 mmc_read_scr(host); 1030 mmc_read_scr(host);
1414 mmc_read_switch_caps(host); 1031 mmc_read_switch_caps(host);
1415 } else 1032 } else
1416 mmc_process_ext_csd(host); 1033 mmc_process_ext_csd(host);
1417 } 1034 }
1418 1035
1419 1036
1420 /** 1037 /**
1421 * mmc_detect_change - process change of state on a MMC socket 1038 * mmc_detect_change - process change of state on a MMC socket
1422 * @host: host which changed state. 1039 * @host: host which changed state.
1423 * @delay: optional delay to wait before detection (jiffies) 1040 * @delay: optional delay to wait before detection (jiffies)
1424 * 1041 *
1425 * All we know is that card(s) have been inserted or removed 1042 * All we know is that card(s) have been inserted or removed
1426 * from the socket(s). We don't know which socket or cards. 1043 * from the socket(s). We don't know which socket or cards.
1427 */ 1044 */
1428 void mmc_detect_change(struct mmc_host *host, unsigned long delay) 1045 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1429 { 1046 {
1430 #ifdef CONFIG_MMC_DEBUG 1047 #ifdef CONFIG_MMC_DEBUG
1431 mmc_claim_host(host); 1048 mmc_claim_host(host);
1432 BUG_ON(host->removed); 1049 BUG_ON(host->removed);
1433 mmc_release_host(host); 1050 mmc_release_host(host);
1434 #endif 1051 #endif
1435 1052
1436 mmc_schedule_delayed_work(&host->detect, delay); 1053 mmc_schedule_delayed_work(&host->detect, delay);
1437 } 1054 }
1438 1055
1439 EXPORT_SYMBOL(mmc_detect_change); 1056 EXPORT_SYMBOL(mmc_detect_change);
1440 1057
1441 1058
1442 static void mmc_rescan(struct work_struct *work) 1059 static void mmc_rescan(struct work_struct *work)
1443 { 1060 {
1444 struct mmc_host *host = 1061 struct mmc_host *host =
1445 container_of(work, struct mmc_host, detect.work); 1062 container_of(work, struct mmc_host, detect.work);
1446 1063
1447 mmc_claim_host(host); 1064 mmc_claim_host(host);
1448 1065
1449 /* 1066 /*
1450 * Check for removed card and newly inserted ones. We check for 1067 * Check for removed card and newly inserted ones. We check for
1451 * removed cards first so we can intelligently re-select the VDD. 1068 * removed cards first so we can intelligently re-select the VDD.
1452 */ 1069 */
1453 if (host->card) { 1070 if (host->card) {
1454 mmc_check_card(host->card); 1071 mmc_check_card(host->card);
1455 1072
1456 mmc_release_host(host); 1073 mmc_release_host(host);
1457 1074
1458 if (mmc_card_dead(host->card)) { 1075 if (mmc_card_dead(host->card)) {
1459 mmc_remove_card(host->card); 1076 mmc_remove_card(host->card);
1460 host->card = NULL; 1077 host->card = NULL;
1461 } 1078 }
1462 1079
1463 goto out; 1080 goto out;
1464 } 1081 }
1465 1082
1466 mmc_setup(host); 1083 mmc_setup(host);
1467 1084
1468 if (host->card && !mmc_card_dead(host->card)) { 1085 if (host->card && !mmc_card_dead(host->card)) {
1469 /* 1086 /*
1470 * (Re-)calculate the fastest clock rate which the 1087 * (Re-)calculate the fastest clock rate which the
1471 * attached cards and the host support. 1088 * attached cards and the host support.
1472 */ 1089 */
1473 host->ios.clock = mmc_calculate_clock(host); 1090 host->ios.clock = mmc_calculate_clock(host);
1474 mmc_set_ios(host); 1091 mmc_set_ios(host);
1475 } 1092 }
1476 1093
1477 mmc_release_host(host); 1094 mmc_release_host(host);
1478 1095
1479 /* 1096 /*
1480 * If this is a new and good card, register it. 1097 * If this is a new and good card, register it.
1481 */ 1098 */
1482 if (host->card && !mmc_card_dead(host->card)) { 1099 if (host->card && !mmc_card_dead(host->card)) {
1483 if (mmc_register_card(host->card)) 1100 if (mmc_register_card(host->card))
1484 mmc_card_set_dead(host->card); 1101 mmc_card_set_dead(host->card);
1485 } 1102 }
1486 1103
1487 /* 1104 /*
1488 * If this card is dead, destroy it. 1105 * If this card is dead, destroy it.
1489 */ 1106 */
1490 if (host->card && mmc_card_dead(host->card)) { 1107 if (host->card && mmc_card_dead(host->card)) {
1491 mmc_remove_card(host->card); 1108 mmc_remove_card(host->card);
1492 host->card = NULL; 1109 host->card = NULL;
1493 } 1110 }
1494 1111
1495 out: 1112 out:
1496 /* 1113 /*
1497 * If we discover that there are no cards on the 1114 * If we discover that there are no cards on the
1498 * bus, turn off the clock and power down. 1115 * bus, turn off the clock and power down.
1499 */ 1116 */
1500 if (!host->card) 1117 if (!host->card)
1501 mmc_power_off(host); 1118 mmc_power_off(host);
1502 } 1119 }
1503 1120
1504 1121
1505 /** 1122 /**
1506 * mmc_alloc_host - initialise the per-host structure. 1123 * mmc_alloc_host - initialise the per-host structure.
1507 * @extra: sizeof private data structure 1124 * @extra: sizeof private data structure
1508 * @dev: pointer to host device model structure 1125 * @dev: pointer to host device model structure
1509 * 1126 *
1510 * Initialise the per-host structure. 1127 * Initialise the per-host structure.
1511 */ 1128 */
1512 struct mmc_host *mmc_alloc_host(int extra, struct device *dev) 1129 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1513 { 1130 {
1514 struct mmc_host *host; 1131 struct mmc_host *host;
1515 1132
1516 host = mmc_alloc_host_sysfs(extra, dev); 1133 host = mmc_alloc_host_sysfs(extra, dev);
1517 if (host) { 1134 if (host) {
1518 spin_lock_init(&host->lock); 1135 spin_lock_init(&host->lock);
1519 init_waitqueue_head(&host->wq); 1136 init_waitqueue_head(&host->wq);
1520 INIT_DELAYED_WORK(&host->detect, mmc_rescan); 1137 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
1521 1138
1522 /* 1139 /*
1523 * By default, hosts do not support SGIO or large requests. 1140 * By default, hosts do not support SGIO or large requests.
1524 * They have to set these according to their abilities. 1141 * They have to set these according to their abilities.
1525 */ 1142 */
1526 host->max_hw_segs = 1; 1143 host->max_hw_segs = 1;
1527 host->max_phys_segs = 1; 1144 host->max_phys_segs = 1;
1528 host->max_seg_size = PAGE_CACHE_SIZE; 1145 host->max_seg_size = PAGE_CACHE_SIZE;
1529 1146
1530 host->max_req_size = PAGE_CACHE_SIZE; 1147 host->max_req_size = PAGE_CACHE_SIZE;
1531 host->max_blk_size = 512; 1148 host->max_blk_size = 512;
1532 host->max_blk_count = PAGE_CACHE_SIZE / 512; 1149 host->max_blk_count = PAGE_CACHE_SIZE / 512;
1533 } 1150 }
1534 1151
1535 return host; 1152 return host;
1536 } 1153 }
1537 1154
1538 EXPORT_SYMBOL(mmc_alloc_host); 1155 EXPORT_SYMBOL(mmc_alloc_host);
1539 1156
1540 /** 1157 /**
1541 * mmc_add_host - initialise host hardware 1158 * mmc_add_host - initialise host hardware
1542 * @host: mmc host 1159 * @host: mmc host
1543 */ 1160 */
1544 int mmc_add_host(struct mmc_host *host) 1161 int mmc_add_host(struct mmc_host *host)
1545 { 1162 {
1546 int ret; 1163 int ret;
1547 1164
1548 ret = mmc_add_host_sysfs(host); 1165 ret = mmc_add_host_sysfs(host);
1549 if (ret == 0) { 1166 if (ret == 0) {
1550 mmc_power_off(host); 1167 mmc_power_off(host);
1551 mmc_detect_change(host, 0); 1168 mmc_detect_change(host, 0);
1552 } 1169 }
1553 1170
1554 return ret; 1171 return ret;
1555 } 1172 }
1556 1173
1557 EXPORT_SYMBOL(mmc_add_host); 1174 EXPORT_SYMBOL(mmc_add_host);
1558 1175
1559 /** 1176 /**
1560 * mmc_remove_host - remove host hardware 1177 * mmc_remove_host - remove host hardware
1561 * @host: mmc host 1178 * @host: mmc host
1562 * 1179 *
1563 * Unregister and remove all cards associated with this host, 1180 * Unregister and remove all cards associated with this host,
1564 * and power down the MMC bus. 1181 * and power down the MMC bus.
1565 */ 1182 */
1566 void mmc_remove_host(struct mmc_host *host) 1183 void mmc_remove_host(struct mmc_host *host)
1567 { 1184 {
1568 #ifdef CONFIG_MMC_DEBUG 1185 #ifdef CONFIG_MMC_DEBUG
1569 mmc_claim_host(host); 1186 mmc_claim_host(host);
1570 host->removed = 1; 1187 host->removed = 1;
1571 mmc_release_host(host); 1188 mmc_release_host(host);
1572 #endif 1189 #endif
1573 1190
1574 mmc_flush_scheduled_work(); 1191 mmc_flush_scheduled_work();
1575 1192
1576 if (host->card) { 1193 if (host->card) {
1577 mmc_remove_card(host->card); 1194 mmc_remove_card(host->card);
1578 host->card = NULL; 1195 host->card = NULL;
1579 } 1196 }
1580 1197
1581 mmc_power_off(host); 1198 mmc_power_off(host);
1582 mmc_remove_host_sysfs(host); 1199 mmc_remove_host_sysfs(host);
1583 } 1200 }
1584 1201
1585 EXPORT_SYMBOL(mmc_remove_host); 1202 EXPORT_SYMBOL(mmc_remove_host);
1586 1203
1587 /** 1204 /**
1588 * mmc_free_host - free the host structure 1205 * mmc_free_host - free the host structure
1589 * @host: mmc host 1206 * @host: mmc host
1590 * 1207 *
1591 * Free the host once all references to it have been dropped. 1208 * Free the host once all references to it have been dropped.
1592 */ 1209 */
1593 void mmc_free_host(struct mmc_host *host) 1210 void mmc_free_host(struct mmc_host *host)
1594 { 1211 {
1595 mmc_free_host_sysfs(host); 1212 mmc_free_host_sysfs(host);
1596 } 1213 }
1597 1214
1598 EXPORT_SYMBOL(mmc_free_host); 1215 EXPORT_SYMBOL(mmc_free_host);
1599 1216
1600 #ifdef CONFIG_PM 1217 #ifdef CONFIG_PM
1601 1218
1602 /** 1219 /**
1603 * mmc_suspend_host - suspend a host 1220 * mmc_suspend_host - suspend a host
1604 * @host: mmc host 1221 * @host: mmc host
1605 * @state: suspend mode (PM_SUSPEND_xxx) 1222 * @state: suspend mode (PM_SUSPEND_xxx)
1606 */ 1223 */
1607 int mmc_suspend_host(struct mmc_host *host, pm_message_t state) 1224 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1608 { 1225 {
1609 mmc_flush_scheduled_work(); 1226 mmc_flush_scheduled_work();
1610 1227
1611 if (host->card) { 1228 if (host->card) {
1612 mmc_remove_card(host->card); 1229 mmc_remove_card(host->card);
1613 host->card = NULL; 1230 host->card = NULL;
1614 } 1231 }
1615 1232
drivers/mmc/core/core.h
1 /* 1 /*
2 * linux/drivers/mmc/core/core.h 2 * linux/drivers/mmc/core/core.h
3 * 3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved. 4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright 2007 Pierre Ossman
5 * 6 *
6 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as 8 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
9 */ 10 */
10 #ifndef _MMC_CORE_H 11 #ifndef _MMC_CORE_CORE_H
11 #define _MMC_CORE_H 12 #define _MMC_CORE_CORE_H
12 /* core-internal functions */
13 void mmc_init_card(struct mmc_card *card, struct mmc_host *host);
14 int mmc_register_card(struct mmc_card *card);
15 void mmc_remove_card(struct mmc_card *card);
16 13
17 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev); 14 #include <linux/delay.h>
18 int mmc_add_host_sysfs(struct mmc_host *host);
19 void mmc_remove_host_sysfs(struct mmc_host *host);
20 void mmc_free_host_sysfs(struct mmc_host *host);
21 15
22 int mmc_schedule_work(struct work_struct *work); 16 #define MMC_CMD_RETRIES 3
23 int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay); 17
24 void mmc_flush_scheduled_work(void); 18 void mmc_set_chip_select(struct mmc_host *host, int mode);
19
20 static inline void mmc_delay(unsigned int ms)
21 {
22 if (ms < 1000 / HZ) {
23 cond_resched();
24 mdelay(ms);
25 } else {
drivers/mmc/core/mmc_ops.c
File was created 1 /*
2 * linux/drivers/mmc/mmc_ops.h
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/types.h>
13 #include <asm/scatterlist.h>
14 #include <linux/scatterlist.h>
15
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19
20 #include "core.h"
21 #include "mmc_ops.h"
22
23 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card)
24 {
25 int err;
26 struct mmc_command cmd;
27
28 BUG_ON(!host);
29
30 memset(&cmd, 0, sizeof(struct mmc_command));
31
32 cmd.opcode = MMC_SELECT_CARD;
33
34 if (card) {
35 cmd.arg = card->rca << 16;
36 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
37 } else {
38 cmd.arg = 0;
39 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
40 }
41
42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
43 if (err != MMC_ERR_NONE)
44 return err;
45
46 return MMC_ERR_NONE;
47 }
48
49 int mmc_select_card(struct mmc_card *card)
50 {
51 BUG_ON(!card);
52
53 return _mmc_select_card(card->host, card);
54 }
55
56 int mmc_deselect_cards(struct mmc_host *host)
57 {
58 return _mmc_select_card(host, NULL);
59 }
60
61 int mmc_go_idle(struct mmc_host *host)
62 {
63 int err;
64 struct mmc_command cmd;
65
66 mmc_set_chip_select(host, MMC_CS_HIGH);
67
68 mmc_delay(1);
69
70 memset(&cmd, 0, sizeof(struct mmc_command));
71
72 cmd.opcode = MMC_GO_IDLE_STATE;
73 cmd.arg = 0;
74 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
75
76 err = mmc_wait_for_cmd(host, &cmd, 0);
77
78 mmc_delay(1);
79
80 mmc_set_chip_select(host, MMC_CS_DONTCARE);
81
82 mmc_delay(1);
83
84 return err;
85 }
86
87 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
88 {
89 struct mmc_command cmd;
90 int i, err = 0;
91
92 BUG_ON(!host);
93
94 memset(&cmd, 0, sizeof(struct mmc_command));
95
96 cmd.opcode = MMC_SEND_OP_COND;
97 cmd.arg = ocr;
98 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
99
100 for (i = 100; i; i--) {
101 err = mmc_wait_for_cmd(host, &cmd, 0);
102 if (err != MMC_ERR_NONE)
103 break;
104
105 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
106 break;
107
108 err = MMC_ERR_TIMEOUT;
109
110 mmc_delay(10);
111 }
112
113 if (rocr)
114 *rocr = cmd.resp[0];
115
116 return err;
117 }
118
119 int mmc_all_send_cid(struct mmc_host *host, u32 *cid)
120 {
121 int err;
122 struct mmc_command cmd;
123
124 BUG_ON(!host);
125 BUG_ON(!cid);
126
127 memset(&cmd, 0, sizeof(struct mmc_command));
128
129 cmd.opcode = MMC_ALL_SEND_CID;
130 cmd.arg = 0;
131 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
132
133 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
134 if (err != MMC_ERR_NONE)
135 return err;
136
137 memcpy(cid, cmd.resp, sizeof(u32) * 4);
138
139 return MMC_ERR_NONE;
140 }
141
142 int mmc_set_relative_addr(struct mmc_card *card)
143 {
144 int err;
145 struct mmc_command cmd;
146
147 BUG_ON(!card);
148 BUG_ON(!card->host);
149
150 memset(&cmd, 0, sizeof(struct mmc_command));
151
152 cmd.opcode = MMC_SET_RELATIVE_ADDR;
153 cmd.arg = card->rca << 16;
154 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
155
156 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
157 if (err != MMC_ERR_NONE)
158 return err;
159
160 return MMC_ERR_NONE;
161 }
162
163 int mmc_send_csd(struct mmc_card *card, u32 *csd)
164 {
165 int err;
166 struct mmc_command cmd;
167
168 BUG_ON(!card);
169 BUG_ON(!card->host);
170 BUG_ON(!csd);
171
172 memset(&cmd, 0, sizeof(struct mmc_command));
173
174 cmd.opcode = MMC_SEND_CSD;
175 cmd.arg = card->rca << 16;
176 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
177
178 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
179 if (err != MMC_ERR_NONE)
180 return err;
181
182 memcpy(csd, cmd.resp, sizeof(u32) * 4);
183
184 return MMC_ERR_NONE;
185 }
186
187 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
188 {
189 struct mmc_request mrq;
190 struct mmc_command cmd;
191 struct mmc_data data;
192 struct scatterlist sg;
193
194 BUG_ON(!card);
195 BUG_ON(!card->host);
196 BUG_ON(!ext_csd);
197
198 memset(&mrq, 0, sizeof(struct mmc_request));
199 memset(&cmd, 0, sizeof(struct mmc_command));
200 memset(&data, 0, sizeof(struct mmc_data));
201
202 mrq.cmd = &cmd;
203 mrq.data = &data;
204
205 cmd.opcode = MMC_SEND_EXT_CSD;
206 cmd.arg = 0;
207 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
208
209 data.blksz = 512;
210 data.blocks = 1;
211 data.flags = MMC_DATA_READ;
212 data.sg = &sg;
213 data.sg_len = 1;
214
215 sg_init_one(&sg, ext_csd, 512);
216
217 mmc_set_data_timeout(&data, card, 0);
218
219 mmc_wait_for_req(card->host, &mrq);
220
221 if (cmd.error != MMC_ERR_NONE)
222 return cmd.error;
223 if (data.error != MMC_ERR_NONE)
224 return data.error;
225
226 return MMC_ERR_NONE;
227 }
228
229 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
230 {
231 int err;
232 struct mmc_command cmd;
233
234 BUG_ON(!card);
235 BUG_ON(!card->host);
236
237 memset(&cmd, 0, sizeof(struct mmc_command));
238
239 cmd.opcode = MMC_SWITCH;
240 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
241 (index << 16) |
242 (value << 8) |
243 set;
244 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
245
246 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
247 if (err != MMC_ERR_NONE)
248 return err;
249
250 return MMC_ERR_NONE;
251 }
252
253 int mmc_send_status(struct mmc_card *card, u32 *status)
254 {
255 int err;
256 struct mmc_command cmd;
257
258 BUG_ON(!card);
259 BUG_ON(!card->host);
260
261 memset(&cmd, 0, sizeof(struct mmc_command));
262
263 cmd.opcode = MMC_SEND_STATUS;
264 cmd.arg = card->rca << 16;
265 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
266
267 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
268 if (err != MMC_ERR_NONE)
269 return err;
270
271 if (status)
272 *status = cmd.resp[0];
273
274 return MMC_ERR_NONE;
275 }
276
277
drivers/mmc/core/mmc_ops.h
File was created 1 /*
2 * linux/drivers/mmc/mmc_ops.h
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 #ifndef _MMC_MMC_OPS_H
13 #define _MMC_MMC_OPS_H
14
15 int mmc_select_card(struct mmc_card *card);
16 int mmc_deselect_cards(struct mmc_host *host);
17 int mmc_go_idle(struct mmc_host *host);
18 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
19 int mmc_all_send_cid(struct mmc_host *host, u32 *cid);
20 int mmc_set_relative_addr(struct mmc_card *card);
21 int mmc_send_csd(struct mmc_card *card, u32 *csd);
22 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
23 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
24 int mmc_send_status(struct mmc_card *card, u32 *status);
25
26 #endif
27
28
drivers/mmc/core/sd_ops.c
File was created 1 /*
2 * linux/drivers/mmc/sd_ops.h
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/types.h>
13 #include <asm/scatterlist.h>
14 #include <linux/scatterlist.h>
15
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
20
21 #include "core.h"
22 #include "sd_ops.h"
23
24 /**
25 * mmc_wait_for_app_cmd - start an application command and wait for
26 completion
27 * @host: MMC host to start command
28 * @rca: RCA to send MMC_APP_CMD to
29 * @cmd: MMC command to start
30 * @retries: maximum number of retries
31 *
32 * Sends a MMC_APP_CMD, checks the card response, sends the command
33 * in the parameter and waits for it to complete. Return any error
34 * that occurred while the command was executing. Do not attempt to
35 * parse the response.
36 */
37 int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
38 struct mmc_command *cmd, int retries)
39 {
40 struct mmc_request mrq;
41
42 int i, err;
43
44 BUG_ON(!cmd);
45 BUG_ON(retries < 0);
46
47 err = MMC_ERR_INVALID;
48
49 /*
50 * We have to resend MMC_APP_CMD for each attempt so
51 * we cannot use the retries field in mmc_command.
52 */
53 for (i = 0;i <= retries;i++) {
54 memset(&mrq, 0, sizeof(struct mmc_request));
55
56 err = mmc_app_cmd(host, card);
57 if (err != MMC_ERR_NONE)
58 continue;
59
60 memset(&mrq, 0, sizeof(struct mmc_request));
61
62 memset(cmd->resp, 0, sizeof(cmd->resp));
63 cmd->retries = 0;
64
65 mrq.cmd = cmd;
66 cmd->data = NULL;
67
68 mmc_wait_for_req(host, &mrq);
69
70 err = cmd->error;
71 if (cmd->error == MMC_ERR_NONE)
72 break;
73 }
74
75 return err;
76 }
77
78 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
79
80 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
81 {
82 int err;
83 struct mmc_command cmd;
84
85 BUG_ON(!host);
86 BUG_ON(card && (card->host != host));
87
88 cmd.opcode = MMC_APP_CMD;
89
90 if (card) {
91 cmd.arg = card->rca << 16;
92 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
93 } else {
94 cmd.arg = 0;
95 cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
96 }
97
98 err = mmc_wait_for_cmd(host, &cmd, 0);
99 if (err != MMC_ERR_NONE)
100 return err;
101
102 /* Check that card supported application commands */
103 if (!(cmd.resp[0] & R1_APP_CMD))
104 return MMC_ERR_FAILED;
105
106 return MMC_ERR_NONE;
107 }
108
109 int mmc_app_set_bus_width(struct mmc_card *card, int width)
110 {
111 int err;
112 struct mmc_command cmd;
113
114 BUG_ON(!card);
115 BUG_ON(!card->host);
116
117 memset(&cmd, 0, sizeof(struct mmc_command));
118
119 cmd.opcode = SD_APP_SET_BUS_WIDTH;
120 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
121
122 switch (width) {
123 case MMC_BUS_WIDTH_1:
124 cmd.arg = SD_BUS_WIDTH_1;
125 break;
126 case MMC_BUS_WIDTH_4:
127 cmd.arg = SD_BUS_WIDTH_4;
128 break;
129 default:
130 return MMC_ERR_INVALID;
131 }
132
133 err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
134 if (err != MMC_ERR_NONE)
135 return err;
136
137 return MMC_ERR_NONE;
138 }
139
140 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
141 {
142 struct mmc_command cmd;
143 int i, err = 0;
144
145 BUG_ON(!host);
146
147 memset(&cmd, 0, sizeof(struct mmc_command));
148
149 cmd.opcode = SD_APP_OP_COND;
150 cmd.arg = ocr;
151 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
152
153 for (i = 100; i; i--) {
154 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
155 if (err != MMC_ERR_NONE)
156 break;
157
158 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
159 break;
160
161 err = MMC_ERR_TIMEOUT;
162
163 mmc_delay(10);
164 }
165
166 if (rocr)
167 *rocr = cmd.resp[0];
168
169 return err;
170 }
171
172 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
173 {
174 struct mmc_command cmd;
175 int err;
176 static const u8 test_pattern = 0xAA;
177
178 /*
179 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
180 * before SD_APP_OP_COND. This command will harmlessly fail for
181 * SD 1.0 cards.
182 */
183 cmd.opcode = SD_SEND_IF_COND;
184 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
185 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
186
187 err = mmc_wait_for_cmd(host, &cmd, 0);
188 if (err != MMC_ERR_NONE)
189 return err;
190
191 if ((cmd.resp[0] & 0xFF) != test_pattern)
192 return MMC_ERR_FAILED;
193
194 return MMC_ERR_NONE;
195 }
196
197 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
198 {
199 int err;
200 struct mmc_command cmd;
201
202 BUG_ON(!host);
203 BUG_ON(!rca);
204
205 memset(&cmd, 0, sizeof(struct mmc_command));
206
207 cmd.opcode = SD_SEND_RELATIVE_ADDR;
208 cmd.arg = 0;
209 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
210
211 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
212 if (err != MMC_ERR_NONE)
213 return err;
214
215 *rca = cmd.resp[0] >> 16;
216
217 return MMC_ERR_NONE;
218 }
219
220 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
221 {
222 int err;
223 struct mmc_request mrq;
224 struct mmc_command cmd;
225 struct mmc_data data;
226 struct scatterlist sg;
227
228 BUG_ON(!card);
229 BUG_ON(!card->host);
230 BUG_ON(!scr);
231
232 err = mmc_app_cmd(card->host, card);
233 if (err != MMC_ERR_NONE)
234 return err;
235
236 memset(&mrq, 0, sizeof(struct mmc_request));
237 memset(&cmd, 0, sizeof(struct mmc_command));
238 memset(&data, 0, sizeof(struct mmc_data));
239
240 mrq.cmd = &cmd;
241 mrq.data = &data;
242
243 cmd.opcode = SD_APP_SEND_SCR;
244 cmd.arg = 0;
245 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
246
247 data.blksz = 8;
248 data.blocks = 1;
249 data.flags = MMC_DATA_READ;
250 data.sg = &sg;
251 data.sg_len = 1;
252
253 sg_init_one(&sg, scr, 8);
254
255 mmc_set_data_timeout(&data, card, 0);
256
257 mmc_wait_for_req(card->host, &mrq);
258
259 if (cmd.error != MMC_ERR_NONE)
260 return cmd.error;
261 if (data.error != MMC_ERR_NONE)
262 return data.error;
263
264 scr[0] = ntohl(scr[0]);
265 scr[1] = ntohl(scr[1]);
266
267 return MMC_ERR_NONE;
268 }
269
270 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
271 u8 value, u8 *resp)
272 {
273 struct mmc_request mrq;
274 struct mmc_command cmd;
275 struct mmc_data data;
276 struct scatterlist sg;
277
278 BUG_ON(!card);
279 BUG_ON(!card->host);
280
281 mode = !!mode;
282 value &= 0xF;
283
284 memset(&mrq, 0, sizeof(struct mmc_request));
285 memset(&cmd, 0, sizeof(struct mmc_command));
286 memset(&data, 0, sizeof(struct mmc_data));
287
288 mrq.cmd = &cmd;
289 mrq.data = &data;
290
291 cmd.opcode = SD_SWITCH;
292 cmd.arg = mode << 31 | 0x00FFFFFF;
293 cmd.arg &= ~(0xF << (group * 4));
294 cmd.arg |= value << (group * 4);
295 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
296
297 data.blksz = 64;
298 data.blocks = 1;
299 data.flags = MMC_DATA_READ;
300 data.sg = &sg;
301 data.sg_len = 1;
302
303 sg_init_one(&sg, resp, 64);
304
305 mmc_set_data_timeout(&data, card, 0);
306
307 mmc_wait_for_req(card->host, &mrq);
308
309 if (cmd.error != MMC_ERR_NONE)
310 return cmd.error;
311 if (data.error != MMC_ERR_NONE)
312 return data.error;
313
314 return MMC_ERR_NONE;
315 }
316
317
drivers/mmc/core/sd_ops.h
File was created 1 /*
2 * linux/drivers/mmc/sd_ops.h
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 #ifndef _MMC_SD_OPS_H
13 #define _MMC_SD_OPS_H
14
15 int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card);
16 int mmc_app_set_bus_width(struct mmc_card *card, int width);
17 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
18 int mmc_send_if_cond(struct mmc_host *host, u32 ocr);
19 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca);
20 int mmc_app_send_scr(struct mmc_card *card, u32 *scr);
21 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
22 u8 value, u8 *resp);
23
24 #endif
25
26
drivers/mmc/core/sysfs.c
1 /* 1 /*
2 * linux/drivers/mmc/core/sysfs.c 2 * linux/drivers/mmc/core/sysfs.c
3 * 3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved. 4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 * 9 *
10 * MMC sysfs/driver model support. 10 * MMC sysfs/driver model support.
11 */ 11 */
12 #include <linux/module.h> 12 #include <linux/module.h>
13 #include <linux/init.h> 13 #include <linux/init.h>
14 #include <linux/device.h> 14 #include <linux/device.h>
15 #include <linux/idr.h> 15 #include <linux/idr.h>
16 #include <linux/workqueue.h> 16 #include <linux/workqueue.h>
17 17
18 #include <linux/mmc/card.h> 18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h> 19 #include <linux/mmc/host.h>
20 20
21 #include "core.h" 21 #include "sysfs.h"
22 22
23 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev) 23 #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
24 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv) 24 #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
25 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev) 25 #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
26 26
27 #define MMC_ATTR(name, fmt, args...) \ 27 #define MMC_ATTR(name, fmt, args...) \
28 static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \ 28 static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
29 { \ 29 { \
30 struct mmc_card *card = dev_to_mmc_card(dev); \ 30 struct mmc_card *card = dev_to_mmc_card(dev); \
31 return sprintf(buf, fmt, args); \ 31 return sprintf(buf, fmt, args); \
32 } 32 }
33 33
34 MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], 34 MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
35 card->raw_cid[2], card->raw_cid[3]); 35 card->raw_cid[2], card->raw_cid[3]);
36 MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 36 MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
37 card->raw_csd[2], card->raw_csd[3]); 37 card->raw_csd[2], card->raw_csd[3]);
38 MMC_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 38 MMC_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
39 MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 39 MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
40 MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 40 MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
41 MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 41 MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
42 MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid); 42 MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
43 MMC_ATTR(name, "%s\n", card->cid.prod_name); 43 MMC_ATTR(name, "%s\n", card->cid.prod_name);
44 MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid); 44 MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
45 MMC_ATTR(serial, "0x%08x\n", card->cid.serial); 45 MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
46 46
47 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL) 47 #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
48 48
49 static struct device_attribute mmc_dev_attrs[] = { 49 static struct device_attribute mmc_dev_attrs[] = {
50 MMC_ATTR_RO(cid), 50 MMC_ATTR_RO(cid),
51 MMC_ATTR_RO(csd), 51 MMC_ATTR_RO(csd),
52 MMC_ATTR_RO(date), 52 MMC_ATTR_RO(date),
53 MMC_ATTR_RO(fwrev), 53 MMC_ATTR_RO(fwrev),
54 MMC_ATTR_RO(hwrev), 54 MMC_ATTR_RO(hwrev),
55 MMC_ATTR_RO(manfid), 55 MMC_ATTR_RO(manfid),
56 MMC_ATTR_RO(name), 56 MMC_ATTR_RO(name),
57 MMC_ATTR_RO(oemid), 57 MMC_ATTR_RO(oemid),
58 MMC_ATTR_RO(serial), 58 MMC_ATTR_RO(serial),
59 __ATTR_NULL 59 __ATTR_NULL
60 }; 60 };
61 61
62 static struct device_attribute mmc_dev_attr_scr = MMC_ATTR_RO(scr); 62 static struct device_attribute mmc_dev_attr_scr = MMC_ATTR_RO(scr);
63 63
64 64
65 static void mmc_release_card(struct device *dev) 65 static void mmc_release_card(struct device *dev)
66 { 66 {
67 struct mmc_card *card = dev_to_mmc_card(dev); 67 struct mmc_card *card = dev_to_mmc_card(dev);
68 68
69 kfree(card); 69 kfree(card);
70 } 70 }
71 71
72 /* 72 /*
73 * This currently matches any MMC driver to any MMC card - drivers 73 * This currently matches any MMC driver to any MMC card - drivers
74 * themselves make the decision whether to drive this card in their 74 * themselves make the decision whether to drive this card in their
75 * probe method. However, we force "bad" cards to fail. 75 * probe method. However, we force "bad" cards to fail.
76 */ 76 */
77 static int mmc_bus_match(struct device *dev, struct device_driver *drv) 77 static int mmc_bus_match(struct device *dev, struct device_driver *drv)
78 { 78 {
79 struct mmc_card *card = dev_to_mmc_card(dev); 79 struct mmc_card *card = dev_to_mmc_card(dev);
80 return !mmc_card_bad(card); 80 return !mmc_card_bad(card);
81 } 81 }
82 82
83 static int 83 static int
84 mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf, 84 mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
85 int buf_size) 85 int buf_size)
86 { 86 {
87 struct mmc_card *card = dev_to_mmc_card(dev); 87 struct mmc_card *card = dev_to_mmc_card(dev);
88 char ccc[13]; 88 char ccc[13];
89 int retval = 0, i = 0, length = 0; 89 int retval = 0, i = 0, length = 0;
90 90
91 #define add_env(fmt,val) do { \ 91 #define add_env(fmt,val) do { \
92 retval = add_uevent_var(envp, num_envp, &i, \ 92 retval = add_uevent_var(envp, num_envp, &i, \
93 buf, buf_size, &length, \ 93 buf, buf_size, &length, \
94 fmt, val); \ 94 fmt, val); \
95 if (retval) \ 95 if (retval) \
96 return retval; \ 96 return retval; \
97 } while (0); 97 } while (0);
98 98
99 for (i = 0; i < 12; i++) 99 for (i = 0; i < 12; i++)
100 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0'; 100 ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
101 ccc[12] = '\0'; 101 ccc[12] = '\0';
102 102
103 add_env("MMC_CCC=%s", ccc); 103 add_env("MMC_CCC=%s", ccc);
104 add_env("MMC_MANFID=%06x", card->cid.manfid); 104 add_env("MMC_MANFID=%06x", card->cid.manfid);
105 add_env("MMC_NAME=%s", mmc_card_name(card)); 105 add_env("MMC_NAME=%s", mmc_card_name(card));
106 add_env("MMC_OEMID=%04x", card->cid.oemid); 106 add_env("MMC_OEMID=%04x", card->cid.oemid);
107 #undef add_env 107 #undef add_env
108 envp[i] = NULL; 108 envp[i] = NULL;
109 109
110 return 0; 110 return 0;
111 } 111 }
112 112
113 static int mmc_bus_suspend(struct device *dev, pm_message_t state) 113 static int mmc_bus_suspend(struct device *dev, pm_message_t state)
114 { 114 {
115 struct mmc_driver *drv = to_mmc_driver(dev->driver); 115 struct mmc_driver *drv = to_mmc_driver(dev->driver);
116 struct mmc_card *card = dev_to_mmc_card(dev); 116 struct mmc_card *card = dev_to_mmc_card(dev);
117 int ret = 0; 117 int ret = 0;
118 118
119 if (dev->driver && drv->suspend) 119 if (dev->driver && drv->suspend)
120 ret = drv->suspend(card, state); 120 ret = drv->suspend(card, state);
121 return ret; 121 return ret;
122 } 122 }
123 123
124 static int mmc_bus_resume(struct device *dev) 124 static int mmc_bus_resume(struct device *dev)
125 { 125 {
126 struct mmc_driver *drv = to_mmc_driver(dev->driver); 126 struct mmc_driver *drv = to_mmc_driver(dev->driver);
127 struct mmc_card *card = dev_to_mmc_card(dev); 127 struct mmc_card *card = dev_to_mmc_card(dev);
128 int ret = 0; 128 int ret = 0;
129 129
130 if (dev->driver && drv->resume) 130 if (dev->driver && drv->resume)
131 ret = drv->resume(card); 131 ret = drv->resume(card);
132 return ret; 132 return ret;
133 } 133 }
134 134
135 static int mmc_bus_probe(struct device *dev) 135 static int mmc_bus_probe(struct device *dev)
136 { 136 {
137 struct mmc_driver *drv = to_mmc_driver(dev->driver); 137 struct mmc_driver *drv = to_mmc_driver(dev->driver);
138 struct mmc_card *card = dev_to_mmc_card(dev); 138 struct mmc_card *card = dev_to_mmc_card(dev);
139 139
140 return drv->probe(card); 140 return drv->probe(card);
141 } 141 }
142 142
143 static int mmc_bus_remove(struct device *dev) 143 static int mmc_bus_remove(struct device *dev)
144 { 144 {
145 struct mmc_driver *drv = to_mmc_driver(dev->driver); 145 struct mmc_driver *drv = to_mmc_driver(dev->driver);
146 struct mmc_card *card = dev_to_mmc_card(dev); 146 struct mmc_card *card = dev_to_mmc_card(dev);
147 147
148 drv->remove(card); 148 drv->remove(card);
149 149
150 return 0; 150 return 0;
151 } 151 }
152 152
153 static struct bus_type mmc_bus_type = { 153 static struct bus_type mmc_bus_type = {
154 .name = "mmc", 154 .name = "mmc",
155 .dev_attrs = mmc_dev_attrs, 155 .dev_attrs = mmc_dev_attrs,
156 .match = mmc_bus_match, 156 .match = mmc_bus_match,
157 .uevent = mmc_bus_uevent, 157 .uevent = mmc_bus_uevent,
158 .probe = mmc_bus_probe, 158 .probe = mmc_bus_probe,
159 .remove = mmc_bus_remove, 159 .remove = mmc_bus_remove,
160 .suspend = mmc_bus_suspend, 160 .suspend = mmc_bus_suspend,
161 .resume = mmc_bus_resume, 161 .resume = mmc_bus_resume,
162 }; 162 };
163 163
164 /** 164 /**
165 * mmc_register_driver - register a media driver 165 * mmc_register_driver - register a media driver
166 * @drv: MMC media driver 166 * @drv: MMC media driver
167 */ 167 */
168 int mmc_register_driver(struct mmc_driver *drv) 168 int mmc_register_driver(struct mmc_driver *drv)
169 { 169 {
170 drv->drv.bus = &mmc_bus_type; 170 drv->drv.bus = &mmc_bus_type;
171 return driver_register(&drv->drv); 171 return driver_register(&drv->drv);
172 } 172 }
173 173
174 EXPORT_SYMBOL(mmc_register_driver); 174 EXPORT_SYMBOL(mmc_register_driver);
175 175
176 /** 176 /**
177 * mmc_unregister_driver - unregister a media driver 177 * mmc_unregister_driver - unregister a media driver
178 * @drv: MMC media driver 178 * @drv: MMC media driver
179 */ 179 */
180 void mmc_unregister_driver(struct mmc_driver *drv) 180 void mmc_unregister_driver(struct mmc_driver *drv)
181 { 181 {
182 drv->drv.bus = &mmc_bus_type; 182 drv->drv.bus = &mmc_bus_type;
183 driver_unregister(&drv->drv); 183 driver_unregister(&drv->drv);
184 } 184 }
185 185
186 EXPORT_SYMBOL(mmc_unregister_driver); 186 EXPORT_SYMBOL(mmc_unregister_driver);
187 187
188 188
189 /* 189 /*
190 * Internal function. Initialise a MMC card structure. 190 * Internal function. Initialise a MMC card structure.
191 */ 191 */
192 void mmc_init_card(struct mmc_card *card, struct mmc_host *host) 192 void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
193 { 193 {
194 memset(card, 0, sizeof(struct mmc_card)); 194 memset(card, 0, sizeof(struct mmc_card));
195 card->host = host; 195 card->host = host;
196 device_initialize(&card->dev); 196 device_initialize(&card->dev);
197 card->dev.parent = mmc_classdev(host); 197 card->dev.parent = mmc_classdev(host);
198 card->dev.bus = &mmc_bus_type; 198 card->dev.bus = &mmc_bus_type;
199 card->dev.release = mmc_release_card; 199 card->dev.release = mmc_release_card;
200 } 200 }
201 201
202 /* 202 /*
203 * Internal function. Register a new MMC card with the driver model. 203 * Internal function. Register a new MMC card with the driver model.
204 */ 204 */
205 int mmc_register_card(struct mmc_card *card) 205 int mmc_register_card(struct mmc_card *card)
206 { 206 {
207 int ret; 207 int ret;
208 208
209 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id), 209 snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
210 "%s:%04x", mmc_hostname(card->host), card->rca); 210 "%s:%04x", mmc_hostname(card->host), card->rca);
211 211
212 ret = device_add(&card->dev); 212 ret = device_add(&card->dev);
213 if (ret == 0) { 213 if (ret == 0) {
214 if (mmc_card_sd(card)) { 214 if (mmc_card_sd(card)) {
215 ret = device_create_file(&card->dev, &mmc_dev_attr_scr); 215 ret = device_create_file(&card->dev, &mmc_dev_attr_scr);
216 if (ret) 216 if (ret)
217 device_del(&card->dev); 217 device_del(&card->dev);
218 } 218 }
219 } 219 }
220 if (ret == 0) 220 if (ret == 0)
221 mmc_card_set_present(card); 221 mmc_card_set_present(card);
222 return ret; 222 return ret;
223 } 223 }
224 224
225 /* 225 /*
226 * Internal function. Unregister a new MMC card with the 226 * Internal function. Unregister a new MMC card with the
227 * driver model, and (eventually) free it. 227 * driver model, and (eventually) free it.
228 */ 228 */
229 void mmc_remove_card(struct mmc_card *card) 229 void mmc_remove_card(struct mmc_card *card)
230 { 230 {
231 if (mmc_card_present(card)) { 231 if (mmc_card_present(card)) {
232 if (mmc_card_sd(card)) 232 if (mmc_card_sd(card))
233 device_remove_file(&card->dev, &mmc_dev_attr_scr); 233 device_remove_file(&card->dev, &mmc_dev_attr_scr);
234 234
235 device_del(&card->dev); 235 device_del(&card->dev);
236 } 236 }
237 237
238 put_device(&card->dev); 238 put_device(&card->dev);
239 } 239 }
240 240
241 241
242 static void mmc_host_classdev_release(struct device *dev) 242 static void mmc_host_classdev_release(struct device *dev)
243 { 243 {
244 struct mmc_host *host = cls_dev_to_mmc_host(dev); 244 struct mmc_host *host = cls_dev_to_mmc_host(dev);
245 kfree(host); 245 kfree(host);
246 } 246 }
247 247
248 static struct class mmc_host_class = { 248 static struct class mmc_host_class = {
249 .name = "mmc_host", 249 .name = "mmc_host",
250 .dev_release = mmc_host_classdev_release, 250 .dev_release = mmc_host_classdev_release,
251 }; 251 };
252 252
253 static DEFINE_IDR(mmc_host_idr); 253 static DEFINE_IDR(mmc_host_idr);
254 static DEFINE_SPINLOCK(mmc_host_lock); 254 static DEFINE_SPINLOCK(mmc_host_lock);
255 255
256 /* 256 /*
257 * Internal function. Allocate a new MMC host. 257 * Internal function. Allocate a new MMC host.
258 */ 258 */
259 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev) 259 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
260 { 260 {
261 struct mmc_host *host; 261 struct mmc_host *host;
262 262
263 host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); 263 host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
264 if (host) { 264 if (host) {
265 memset(host, 0, sizeof(struct mmc_host) + extra); 265 memset(host, 0, sizeof(struct mmc_host) + extra);
266 266
267 host->parent = dev; 267 host->parent = dev;
268 host->class_dev.parent = dev; 268 host->class_dev.parent = dev;
269 host->class_dev.class = &mmc_host_class; 269 host->class_dev.class = &mmc_host_class;
270 device_initialize(&host->class_dev); 270 device_initialize(&host->class_dev);
271 } 271 }
272 272
273 return host; 273 return host;
274 } 274 }
275 275
276 /* 276 /*
277 * Internal function. Register a new MMC host with the MMC class. 277 * Internal function. Register a new MMC host with the MMC class.
278 */ 278 */
279 int mmc_add_host_sysfs(struct mmc_host *host) 279 int mmc_add_host_sysfs(struct mmc_host *host)
280 { 280 {
281 int err; 281 int err;
282 282
283 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL)) 283 if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
284 return -ENOMEM; 284 return -ENOMEM;
285 285
286 spin_lock(&mmc_host_lock); 286 spin_lock(&mmc_host_lock);
287 err = idr_get_new(&mmc_host_idr, host, &host->index); 287 err = idr_get_new(&mmc_host_idr, host, &host->index);
288 spin_unlock(&mmc_host_lock); 288 spin_unlock(&mmc_host_lock);
289 if (err) 289 if (err)
290 return err; 290 return err;
291 291
292 snprintf(host->class_dev.bus_id, BUS_ID_SIZE, 292 snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
293 "mmc%d", host->index); 293 "mmc%d", host->index);
294 294
295 return device_add(&host->class_dev); 295 return device_add(&host->class_dev);
296 } 296 }
297 297
298 /* 298 /*
299 * Internal function. Unregister a MMC host with the MMC class. 299 * Internal function. Unregister a MMC host with the MMC class.
300 */ 300 */
301 void mmc_remove_host_sysfs(struct mmc_host *host) 301 void mmc_remove_host_sysfs(struct mmc_host *host)
302 { 302 {
303 device_del(&host->class_dev); 303 device_del(&host->class_dev);
304 304
305 spin_lock(&mmc_host_lock); 305 spin_lock(&mmc_host_lock);
306 idr_remove(&mmc_host_idr, host->index); 306 idr_remove(&mmc_host_idr, host->index);
307 spin_unlock(&mmc_host_lock); 307 spin_unlock(&mmc_host_lock);
308 } 308 }
309 309
310 /* 310 /*
311 * Internal function. Free a MMC host. 311 * Internal function. Free a MMC host.
312 */ 312 */
313 void mmc_free_host_sysfs(struct mmc_host *host) 313 void mmc_free_host_sysfs(struct mmc_host *host)
314 { 314 {
315 put_device(&host->class_dev); 315 put_device(&host->class_dev);
316 } 316 }
317 317
318 static struct workqueue_struct *workqueue; 318 static struct workqueue_struct *workqueue;
319 319
320 /* 320 /*
321 * Internal function. Schedule delayed work in the MMC work queue. 321 * Internal function. Schedule delayed work in the MMC work queue.
322 */ 322 */
323 int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay) 323 int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay)
324 { 324 {
325 return queue_delayed_work(workqueue, work, delay); 325 return queue_delayed_work(workqueue, work, delay);
326 } 326 }
327 327
328 /* 328 /*
329 * Internal function. Flush all scheduled work from the MMC work queue. 329 * Internal function. Flush all scheduled work from the MMC work queue.
330 */ 330 */
331 void mmc_flush_scheduled_work(void) 331 void mmc_flush_scheduled_work(void)
332 { 332 {
333 flush_workqueue(workqueue); 333 flush_workqueue(workqueue);
334 } 334 }
335 335
336 static int __init mmc_init(void) 336 static int __init mmc_init(void)
337 { 337 {
338 int ret; 338 int ret;
339 339
340 workqueue = create_singlethread_workqueue("kmmcd"); 340 workqueue = create_singlethread_workqueue("kmmcd");
341 if (!workqueue) 341 if (!workqueue)
342 return -ENOMEM; 342 return -ENOMEM;
343 343
344 ret = bus_register(&mmc_bus_type); 344 ret = bus_register(&mmc_bus_type);
345 if (ret == 0) { 345 if (ret == 0) {
346 ret = class_register(&mmc_host_class); 346 ret = class_register(&mmc_host_class);
347 if (ret) 347 if (ret)
348 bus_unregister(&mmc_bus_type); 348 bus_unregister(&mmc_bus_type);
349 } 349 }
350 return ret; 350 return ret;
351 } 351 }
352 352
353 static void __exit mmc_exit(void) 353 static void __exit mmc_exit(void)
354 { 354 {
355 class_unregister(&mmc_host_class); 355 class_unregister(&mmc_host_class);
356 bus_unregister(&mmc_bus_type); 356 bus_unregister(&mmc_bus_type);
357 destroy_workqueue(workqueue); 357 destroy_workqueue(workqueue);
358 } 358 }
359 359
360 module_init(mmc_init); 360 module_init(mmc_init);
361 module_exit(mmc_exit); 361 module_exit(mmc_exit);
362 362
drivers/mmc/core/sysfs.h
File was created 1 /*
2 * linux/drivers/mmc/core/sysfs.h
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright 2007 Pierre Ossman
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #ifndef _MMC_CORE_SYSFS_H
12 #define _MMC_CORE_SYSFS_H
13
14 void mmc_init_card(struct mmc_card *card, struct mmc_host *host);
15 int mmc_register_card(struct mmc_card *card);
16 void mmc_remove_card(struct mmc_card *card);
17
18 struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev);
19 int mmc_add_host_sysfs(struct mmc_host *host);
20 void mmc_remove_host_sysfs(struct mmc_host *host);
21 void mmc_free_host_sysfs(struct mmc_host *host);
22
23 int mmc_schedule_work(struct work_struct *work);
24 int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay);
25 void mmc_flush_scheduled_work(void);
26
27 #endif
28
include/linux/mmc/core.h
1 /* 1 /*
2 * linux/include/linux/mmc/core.h 2 * linux/include/linux/mmc/core.h
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as 5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation. 6 * published by the Free Software Foundation.
7 */ 7 */
8 #ifndef LINUX_MMC_CORE_H 8 #ifndef LINUX_MMC_CORE_H
9 #define LINUX_MMC_CORE_H 9 #define LINUX_MMC_CORE_H
10 10
11 #include <linux/interrupt.h> 11 #include <linux/interrupt.h>
12 #include <linux/device.h> 12 #include <linux/device.h>
13 13
14 struct request; 14 struct request;
15 struct mmc_data; 15 struct mmc_data;
16 struct mmc_request; 16 struct mmc_request;
17 17
18 struct mmc_command { 18 struct mmc_command {
19 u32 opcode; 19 u32 opcode;
20 u32 arg; 20 u32 arg;
21 u32 resp[4]; 21 u32 resp[4];
22 unsigned int flags; /* expected response type */ 22 unsigned int flags; /* expected response type */
23 #define MMC_RSP_PRESENT (1 << 0) 23 #define MMC_RSP_PRESENT (1 << 0)
24 #define MMC_RSP_136 (1 << 1) /* 136 bit response */ 24 #define MMC_RSP_136 (1 << 1) /* 136 bit response */
25 #define MMC_RSP_CRC (1 << 2) /* expect valid crc */ 25 #define MMC_RSP_CRC (1 << 2) /* expect valid crc */
26 #define MMC_RSP_BUSY (1 << 3) /* card may send busy */ 26 #define MMC_RSP_BUSY (1 << 3) /* card may send busy */
27 #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */ 27 #define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
28 #define MMC_CMD_MASK (3 << 5) /* command type */ 28 #define MMC_CMD_MASK (3 << 5) /* command type */
29 #define MMC_CMD_AC (0 << 5) 29 #define MMC_CMD_AC (0 << 5)
30 #define MMC_CMD_ADTC (1 << 5) 30 #define MMC_CMD_ADTC (1 << 5)
31 #define MMC_CMD_BC (2 << 5) 31 #define MMC_CMD_BC (2 << 5)
32 #define MMC_CMD_BCR (3 << 5) 32 #define MMC_CMD_BCR (3 << 5)
33 33
34 /* 34 /*
35 * These are the response types, and correspond to valid bit 35 * These are the response types, and correspond to valid bit
36 * patterns of the above flags. One additional valid pattern 36 * patterns of the above flags. One additional valid pattern
37 * is all zeros, which means we don't expect a response. 37 * is all zeros, which means we don't expect a response.
38 */ 38 */
39 #define MMC_RSP_NONE (0) 39 #define MMC_RSP_NONE (0)
40 #define MMC_RSP_R1 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE) 40 #define MMC_RSP_R1 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
41 #define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY) 41 #define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
42 #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC) 42 #define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
43 #define MMC_RSP_R3 (MMC_RSP_PRESENT) 43 #define MMC_RSP_R3 (MMC_RSP_PRESENT)
44 #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE) 44 #define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
45 #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE) 45 #define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
46 46
47 #define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE)) 47 #define mmc_resp_type(cmd) ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
48 48
49 /* 49 /*
50 * These are the command types. 50 * These are the command types.
51 */ 51 */
52 #define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK) 52 #define mmc_cmd_type(cmd) ((cmd)->flags & MMC_CMD_MASK)
53 53
54 unsigned int retries; /* max number of retries */ 54 unsigned int retries; /* max number of retries */
55 unsigned int error; /* command error */ 55 unsigned int error; /* command error */
56 56
57 #define MMC_ERR_NONE 0 57 #define MMC_ERR_NONE 0
58 #define MMC_ERR_TIMEOUT 1 58 #define MMC_ERR_TIMEOUT 1
59 #define MMC_ERR_BADCRC 2 59 #define MMC_ERR_BADCRC 2
60 #define MMC_ERR_FIFO 3 60 #define MMC_ERR_FIFO 3
61 #define MMC_ERR_FAILED 4 61 #define MMC_ERR_FAILED 4
62 #define MMC_ERR_INVALID 5 62 #define MMC_ERR_INVALID 5
63 63
64 struct mmc_data *data; /* data segment associated with cmd */ 64 struct mmc_data *data; /* data segment associated with cmd */
65 struct mmc_request *mrq; /* associated request */ 65 struct mmc_request *mrq; /* associated request */
66 }; 66 };
67 67
68 struct mmc_data { 68 struct mmc_data {
69 unsigned int timeout_ns; /* data timeout (in ns, max 80ms) */ 69 unsigned int timeout_ns; /* data timeout (in ns, max 80ms) */
70 unsigned int timeout_clks; /* data timeout (in clocks) */ 70 unsigned int timeout_clks; /* data timeout (in clocks) */
71 unsigned int blksz; /* data block size */ 71 unsigned int blksz; /* data block size */
72 unsigned int blocks; /* number of blocks */ 72 unsigned int blocks; /* number of blocks */
73 unsigned int error; /* data error */ 73 unsigned int error; /* data error */
74 unsigned int flags; 74 unsigned int flags;
75 75
76 #define MMC_DATA_WRITE (1 << 8) 76 #define MMC_DATA_WRITE (1 << 8)
77 #define MMC_DATA_READ (1 << 9) 77 #define MMC_DATA_READ (1 << 9)
78 #define MMC_DATA_STREAM (1 << 10) 78 #define MMC_DATA_STREAM (1 << 10)
79 #define MMC_DATA_MULTI (1 << 11) 79 #define MMC_DATA_MULTI (1 << 11)
80 80
81 unsigned int bytes_xfered; 81 unsigned int bytes_xfered;
82 82
83 struct mmc_command *stop; /* stop command */ 83 struct mmc_command *stop; /* stop command */
84 struct mmc_request *mrq; /* associated request */ 84 struct mmc_request *mrq; /* associated request */
85 85
86 unsigned int sg_len; /* size of scatter list */ 86 unsigned int sg_len; /* size of scatter list */
87 struct scatterlist *sg; /* I/O scatter list */ 87 struct scatterlist *sg; /* I/O scatter list */
88 }; 88 };
89 89
90 struct mmc_request { 90 struct mmc_request {
91 struct mmc_command *cmd; 91 struct mmc_command *cmd;
92 struct mmc_data *data; 92 struct mmc_data *data;
93 struct mmc_command *stop; 93 struct mmc_command *stop;
94 94
95 void *done_data; /* completion data */ 95 void *done_data; /* completion data */
96 void (*done)(struct mmc_request *);/* completion function */ 96 void (*done)(struct mmc_request *);/* completion function */
97 }; 97 };
98 98
99 struct mmc_host; 99 struct mmc_host;
100 struct mmc_card; 100 struct mmc_card;
101 101
102 extern int mmc_wait_for_req(struct mmc_host *, struct mmc_request *); 102 extern int mmc_wait_for_req(struct mmc_host *, struct mmc_request *);
103 extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int); 103 extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int);
104 extern int mmc_wait_for_app_cmd(struct mmc_host *, unsigned int, 104 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
105 struct mmc_command *, int); 105 struct mmc_command *, int);
106 106
107 extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int); 107 extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int);
108 108
109 extern void mmc_claim_host(struct mmc_host *host); 109 extern void mmc_claim_host(struct mmc_host *host);
110 extern void mmc_release_host(struct mmc_host *host); 110 extern void mmc_release_host(struct mmc_host *host);
111 111
112 #endif 112 #endif
113 113
include/linux/mmc/mmc.h
File was created 1 /*
2 * Header for MultiMediaCard (MMC)
3 *
4 * Copyright 2002 Hewlett-Packard Company
5 *
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
9 *
10 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
11 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
12 * FITNESS FOR ANY PARTICULAR PURPOSE.
13 *
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
15 *
16 * Based strongly on code by:
17 *
18 * Author: Yong-iL Joh <tolkien@mizi.com>
19 * Date : $Date: 2002/06/18 12:37:30 $
20 *
21 * Author: Andrew Christian
22 * 15 May 2002
23 */
24
25 #ifndef MMC_MMC_H
26 #define MMC_MMC_H
27
28 /* Standard MMC commands (4.1) type argument response */
29 /* class 1 */
30 #define MMC_GO_IDLE_STATE 0 /* bc */
31 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
32 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
33 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
34 #define MMC_SET_DSR 4 /* bc [31:16] RCA */
35 #define MMC_SWITCH 6 /* ac [31:0] See below R1b */
36 #define MMC_SELECT_CARD 7 /* ac [31:16] RCA R1 */
37 #define MMC_SEND_EXT_CSD 8 /* adtc R1 */
38 #define MMC_SEND_CSD 9 /* ac [31:16] RCA R2 */
39 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
40 #define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
41 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
42 #define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
43 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
44
45 /* class 2 */
46 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
47 #define MMC_READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */
48 #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
49
50 /* class 3 */
51 #define MMC_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */
52
53 /* class 4 */
54 #define MMC_SET_BLOCK_COUNT 23 /* adtc [31:0] data addr R1 */
55 #define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
56 #define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
57 #define MMC_PROGRAM_CID 26 /* adtc R1 */
58 #define MMC_PROGRAM_CSD 27 /* adtc R1 */
59
60 /* class 6 */
61 #define MMC_SET_WRITE_PROT 28 /* ac [31:0] data addr R1b */
62 #define MMC_CLR_WRITE_PROT 29 /* ac [31:0] data addr R1b */
63 #define MMC_SEND_WRITE_PROT 30 /* adtc [31:0] wpdata addr R1 */
64
65 /* class 5 */
66 #define MMC_ERASE_GROUP_START 35 /* ac [31:0] data addr R1 */
67 #define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr R1 */
68 #define MMC_ERASE 38 /* ac R1b */
69
70 /* class 9 */
71 #define MMC_FAST_IO 39 /* ac <Complex> R4 */
72 #define MMC_GO_IRQ_STATE 40 /* bcr R5 */
73
74 /* class 7 */
75 #define MMC_LOCK_UNLOCK 42 /* adtc R1b */
76
77 /* class 8 */
78 #define MMC_APP_CMD 55 /* ac [31:16] RCA R1 */
79 #define MMC_GEN_CMD 56 /* adtc [0] RD/WR R1 */
80
81 /*
82 * MMC_SWITCH argument format:
83 *
84 * [31:26] Always 0
85 * [25:24] Access Mode
86 * [23:16] Location of target Byte in EXT_CSD
87 * [15:08] Value Byte
88 * [07:03] Always 0
89 * [02:00] Command Set
90 */
91
92 /*
93 MMC status in R1
94 Type
95 e : error bit
96 s : status bit
97 r : detected and set for the actual command response
98 x : detected and set during command execution. the host must poll
99 the card by sending status command in order to read these bits.
100 Clear condition
101 a : according to the card state
102 b : always related to the previous command. Reception of
103 a valid command will clear it (with a delay of one command)
104 c : clear by read
105 */
106
107 #define R1_OUT_OF_RANGE (1 << 31) /* er, c */
108 #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
109 #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
110 #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
111 #define R1_ERASE_PARAM (1 << 27) /* ex, c */
112 #define R1_WP_VIOLATION (1 << 26) /* erx, c */
113 #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
114 #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
115 #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
116 #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
117 #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
118 #define R1_CC_ERROR (1 << 20) /* erx, c */
119 #define R1_ERROR (1 << 19) /* erx, c */
120 #define R1_UNDERRUN (1 << 18) /* ex, c */
121 #define R1_OVERRUN (1 << 17) /* ex, c */
122 #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
123 #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
124 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
125 #define R1_ERASE_RESET (1 << 13) /* sr, c */
126 #define R1_STATUS(x) (x & 0xFFFFE000)
127 #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
128 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
129 #define R1_APP_CMD (1 << 5) /* sr, c */
130
131 /* These are unpacked versions of the actual responses */
132
133 struct _mmc_csd {
134 u8 csd_structure;
135 u8 spec_vers;
136 u8 taac;
137 u8 nsac;
138 u8 tran_speed;
139 u16 ccc;
140 u8 read_bl_len;
141 u8 read_bl_partial;
142 u8 write_blk_misalign;
143 u8 read_blk_misalign;
144 u8 dsr_imp;
145 u16 c_size;
146 u8 vdd_r_curr_min;
147 u8 vdd_r_curr_max;
148 u8 vdd_w_curr_min;
149 u8 vdd_w_curr_max;
150 u8 c_size_mult;
151 union {
152 struct { /* MMC system specification version 3.1 */
153 u8 erase_grp_size;
154 u8 erase_grp_mult;
155 } v31;
156 struct { /* MMC system specification version 2.2 */
157 u8 sector_size;
158 u8 erase_grp_size;
159 } v22;
160 } erase;
161 u8 wp_grp_size;
162 u8 wp_grp_enable;
163 u8 default_ecc;
164 u8 r2w_factor;
165 u8 write_bl_len;
166 u8 write_bl_partial;
167 u8 file_format_grp;
168 u8 copy;
169 u8 perm_write_protect;
170 u8 tmp_write_protect;
171 u8 file_format;
172 u8 ecc;
173 };
174
175 /*
176 * OCR bits are mostly in host.h
177 */
178 #define MMC_CARD_BUSY 0x80000000 /* Card Power up status bit */
179
180 /*
181 * Card Command Classes (CCC)
182 */
183 #define CCC_BASIC (1<<0) /* (0) Basic protocol functions */
184 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
185 #define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */
186 /* (CMD11) */
187 #define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */
188 /* (CMD16,17,18) */
189 #define CCC_STREAM_WRITE (1<<3) /* (3) Stream write commands */
190 /* (CMD20) */
191 #define CCC_BLOCK_WRITE (1<<4) /* (4) Block write commands */
192 /* (CMD16,24,25,26,27) */
193 #define CCC_ERASE (1<<5) /* (5) Ability to erase blocks */
194 /* (CMD32,33,34,35,36,37,38,39) */
195 #define CCC_WRITE_PROT (1<<6) /* (6) Able to write protect blocks */
196 /* (CMD28,29,30) */
197 #define CCC_LOCK_CARD (1<<7) /* (7) Able to lock down card */
198 /* (CMD16,CMD42) */
199 #define CCC_APP_SPEC (1<<8) /* (8) Application specific */
200 /* (CMD55,56,57,ACMD*) */
201 #define CCC_IO_MODE (1<<9) /* (9) I/O mode */
202 /* (CMD5,39,40,52,53) */
203 #define CCC_SWITCH (1<<10) /* (10) High speed switch */
204 /* (CMD6,34,35,36,37,50) */
205 /* (11) Reserved */
206 /* (CMD?) */
207
208 /*
209 * CSD field definitions
210 */
211
212 #define CSD_STRUCT_VER_1_0 0 /* Valid for system specification 1.0 - 1.2 */
213 #define CSD_STRUCT_VER_1_1 1 /* Valid for system specification 1.4 - 2.2 */
214 #define CSD_STRUCT_VER_1_2 2 /* Valid for system specification 3.1 - 3.2 - 3.31 - 4.0 - 4.1 */
215 #define CSD_STRUCT_EXT_CSD 3 /* Version is coded in CSD_STRUCTURE in EXT_CSD */
216
217 #define CSD_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.2 */
218 #define CSD_SPEC_VER_1 1 /* Implements system specification 1.4 */
219 #define CSD_SPEC_VER_2 2 /* Implements system specification 2.0 - 2.2 */
220 #define CSD_SPEC_VER_3 3 /* Implements system specification 3.1 - 3.2 - 3.31 */
221 #define CSD_SPEC_VER_4 4 /* Implements system specification 4.0 - 4.1 */
222
223 /*
224 * EXT_CSD fields
225 */
226
227 #define EXT_CSD_BUS_WIDTH 183 /* R/W */
228 #define EXT_CSD_HS_TIMING 185 /* R/W */
229 #define EXT_CSD_CARD_TYPE 196 /* RO */
230 #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
231
232 /*
233 * EXT_CSD field definitions
234 */
235
236 #define EXT_CSD_CMD_SET_NORMAL (1<<0)
237 #define EXT_CSD_CMD_SET_SECURE (1<<1)
238 #define EXT_CSD_CMD_SET_CPSECURE (1<<2)
239
240 #define EXT_CSD_CARD_TYPE_26 (1<<0) /* Card can run at 26MHz */
241 #define EXT_CSD_CARD_TYPE_52 (1<<1) /* Card can run at 52MHz */
242
243 #define EXT_CSD_BUS_WIDTH_1 0 /* Card is in 1 bit mode */
244 #define EXT_CSD_BUS_WIDTH_4 1 /* Card is in 4 bit mode */
245 #define EXT_CSD_BUS_WIDTH_8 2 /* Card is in 8 bit mode */
246
247 /*
248 * MMC_SWITCH access modes
249 */
250
251 #define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
252 #define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits which are 1 in value */
253 #define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits which are 1 in value */
254 #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
255
256 #endif /* MMC_MMC_PROTOCOL_H */
257
258
include/linux/mmc/protocol.h
1 /* File was deleted
2 * Header for MultiMediaCard (MMC)
3 *
4 * Copyright 2002 Hewlett-Packard Company
5 *
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
9 *
10 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
11 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
12 * FITNESS FOR ANY PARTICULAR PURPOSE.
13 *
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
15 *
16 * Based strongly on code by:
17 *
18 * Author: Yong-iL Joh <tolkien@mizi.com>
19 * Date : $Date: 2002/06/18 12:37:30 $
20 *
21 * Author: Andrew Christian
22 * 15 May 2002
23 */
24
25 #ifndef MMC_MMC_PROTOCOL_H
26 #define MMC_MMC_PROTOCOL_H
27
28 /* Standard MMC commands (4.1) type argument response */
29 /* class 1 */
30 #define MMC_GO_IDLE_STATE 0 /* bc */
31 #define MMC_SEND_OP_COND 1 /* bcr [31:0] OCR R3 */
32 #define MMC_ALL_SEND_CID 2 /* bcr R2 */
33 #define MMC_SET_RELATIVE_ADDR 3 /* ac [31:16] RCA R1 */
34 #define MMC_SET_DSR 4 /* bc [31:16] RCA */
35 #define MMC_SWITCH 6 /* ac [31:0] See below R1b */
36 #define MMC_SELECT_CARD 7 /* ac [31:16] RCA R1 */
37 #define MMC_SEND_EXT_CSD 8 /* adtc R1 */
38 #define MMC_SEND_CSD 9 /* ac [31:16] RCA R2 */
39 #define MMC_SEND_CID 10 /* ac [31:16] RCA R2 */
40 #define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
41 #define MMC_STOP_TRANSMISSION 12 /* ac R1b */
42 #define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
43 #define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
44
45 /* class 2 */
46 #define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
47 #define MMC_READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */
48 #define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
49
50 /* class 3 */
51 #define MMC_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */
52
53 /* class 4 */
54 #define MMC_SET_BLOCK_COUNT 23 /* adtc [31:0] data addr R1 */
55 #define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
56 #define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
57 #define MMC_PROGRAM_CID 26 /* adtc R1 */
58 #define MMC_PROGRAM_CSD 27 /* adtc R1 */
59
60 /* class 6 */
61 #define MMC_SET_WRITE_PROT 28 /* ac [31:0] data addr R1b */
62 #define MMC_CLR_WRITE_PROT 29 /* ac [31:0] data addr R1b */
63 #define MMC_SEND_WRITE_PROT 30 /* adtc [31:0] wpdata addr R1 */
64
65 /* class 5 */
66 #define MMC_ERASE_GROUP_START 35 /* ac [31:0] data addr R1 */
67 #define MMC_ERASE_GROUP_END 36 /* ac [31:0] data addr R1 */
68 #define MMC_ERASE 38 /* ac R1b */
69
70 /* class 9 */
71 #define MMC_FAST_IO 39 /* ac <Complex> R4 */
72 #define MMC_GO_IRQ_STATE 40 /* bcr R5 */
73
74 /* class 7 */
75 #define MMC_LOCK_UNLOCK 42 /* adtc R1b */
76
77 /* class 8 */
78 #define MMC_APP_CMD 55 /* ac [31:16] RCA R1 */
79 #define MMC_GEN_CMD 56 /* adtc [0] RD/WR R1 */
80
81 /* SD commands type argument response */
82 /* class 0 */
83 /* This is basically the same command as for MMC with some quirks. */
84 #define SD_SEND_RELATIVE_ADDR 3 /* bcr R6 */
85 #define SD_SEND_IF_COND 8 /* bcr [11:0] See below R7 */
86
87 /* class 10 */
88 #define SD_SWITCH 6 /* adtc [31:0] See below R1 */
89
90 /* Application commands */
91 #define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
92 #define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */
93 #define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */
94 #define SD_APP_SEND_SCR 51 /* adtc R1 */
95
96 /*
97 * MMC_SWITCH argument format:
98 *
99 * [31:26] Always 0
100 * [25:24] Access Mode
101 * [23:16] Location of target Byte in EXT_CSD
102 * [15:08] Value Byte
103 * [07:03] Always 0
104 * [02:00] Command Set
105 */
106
107 /*
108 * SD_SWITCH argument format:
109 *
110 * [31] Check (0) or switch (1)
111 * [30:24] Reserved (0)
112 * [23:20] Function group 6
113 * [19:16] Function group 5
114 * [15:12] Function group 4
115 * [11:8] Function group 3
116 * [7:4] Function group 2
117 * [3:0] Function group 1
118 */
119
120 /*
121 * SD_SEND_IF_COND argument format:
122 *
123 * [31:12] Reserved (0)
124 * [11:8] Host Voltage Supply Flags
125 * [7:0] Check Pattern (0xAA)
126 */
127
128 /*
129 MMC status in R1
130 Type
131 e : error bit
132 s : status bit
133 r : detected and set for the actual command response
134 x : detected and set during command execution. the host must poll
135 the card by sending status command in order to read these bits.
136 Clear condition
137 a : according to the card state
138 b : always related to the previous command. Reception of
139 a valid command will clear it (with a delay of one command)
140 c : clear by read
141 */
142
143 #define R1_OUT_OF_RANGE (1 << 31) /* er, c */
144 #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
145 #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
146 #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
147 #define R1_ERASE_PARAM (1 << 27) /* ex, c */
148 #define R1_WP_VIOLATION (1 << 26) /* erx, c */
149 #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
150 #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
151 #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
152 #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
153 #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
154 #define R1_CC_ERROR (1 << 20) /* erx, c */
155 #define R1_ERROR (1 << 19) /* erx, c */
156 #define R1_UNDERRUN (1 << 18) /* ex, c */
157 #define R1_OVERRUN (1 << 17) /* ex, c */
158 #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
159 #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
160 #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
161 #define R1_ERASE_RESET (1 << 13) /* sr, c */
162 #define R1_STATUS(x) (x & 0xFFFFE000)
163 #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
164 #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
165 #define R1_APP_CMD (1 << 5) /* sr, c */
166
167 /* These are unpacked versions of the actual responses */
168
169 struct _mmc_csd {
170 u8 csd_structure;
171 u8 spec_vers;
172 u8 taac;
173 u8 nsac;
174 u8 tran_speed;
175 u16 ccc;
176 u8 read_bl_len;
177 u8 read_bl_partial;
178 u8 write_blk_misalign;
179 u8 read_blk_misalign;
180 u8 dsr_imp;
181 u16 c_size;
182 u8 vdd_r_curr_min;
183 u8 vdd_r_curr_max;
184 u8 vdd_w_curr_min;
185 u8 vdd_w_curr_max;
186 u8 c_size_mult;
187 union {
188 struct { /* MMC system specification version 3.1 */
189 u8 erase_grp_size;
190 u8 erase_grp_mult;
191 } v31;
192 struct { /* MMC system specification version 2.2 */
193 u8 sector_size;
194 u8 erase_grp_size;
195 } v22;
196 } erase;
197 u8 wp_grp_size;
198 u8 wp_grp_enable;
199 u8 default_ecc;
200 u8 r2w_factor;
201 u8 write_bl_len;
202 u8 write_bl_partial;
203 u8 file_format_grp;
204 u8 copy;
205 u8 perm_write_protect;
206 u8 tmp_write_protect;
207 u8 file_format;
208 u8 ecc;
209 };
210
211 /*
212 * OCR bits are mostly in host.h
213 */
214 #define MMC_CARD_BUSY 0x80000000 /* Card Power up status bit */
215
216 /*
217 * Card Command Classes (CCC)
218 */
219 #define CCC_BASIC (1<<0) /* (0) Basic protocol functions */
220 /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
221 #define CCC_STREAM_READ (1<<1) /* (1) Stream read commands */
222 /* (CMD11) */
223 #define CCC_BLOCK_READ (1<<2) /* (2) Block read commands */
224 /* (CMD16,17,18) */
225 #define CCC_STREAM_WRITE (1<<3) /* (3) Stream write commands */
226 /* (CMD20) */
227 #define CCC_BLOCK_WRITE (1<<4) /* (4) Block write commands */
228 /* (CMD16,24,25,26,27) */
229 #define CCC_ERASE (1<<5) /* (5) Ability to erase blocks */
230 /* (CMD32,33,34,35,36,37,38,39) */
231 #define CCC_WRITE_PROT (1<<6) /* (6) Able to write protect blocks */
232 /* (CMD28,29,30) */
233 #define CCC_LOCK_CARD (1<<7) /* (7) Able to lock down card */
234 /* (CMD16,CMD42) */
235 #define CCC_APP_SPEC (1<<8) /* (8) Application specific */
236 /* (CMD55,56,57,ACMD*) */
237 #define CCC_IO_MODE (1<<9) /* (9) I/O mode */
238 /* (CMD5,39,40,52,53) */
239 #define CCC_SWITCH (1<<10) /* (10) High speed switch */
240 /* (CMD6,34,35,36,37,50) */
241 /* (11) Reserved */
242 /* (CMD?) */
243
244 /*
245 * CSD field definitions
246 */
247
248 #define CSD_STRUCT_VER_1_0 0 /* Valid for system specification 1.0 - 1.2 */
249 #define CSD_STRUCT_VER_1_1 1 /* Valid for system specification 1.4 - 2.2 */
250 #define CSD_STRUCT_VER_1_2 2 /* Valid for system specification 3.1 - 3.2 - 3.31 - 4.0 - 4.1 */
251 #define CSD_STRUCT_EXT_CSD 3 /* Version is coded in CSD_STRUCTURE in EXT_CSD */
252
253 #define CSD_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.2 */
254 #define CSD_SPEC_VER_1 1 /* Implements system specification 1.4 */
255 #define CSD_SPEC_VER_2 2 /* Implements system specification 2.0 - 2.2 */
256 #define CSD_SPEC_VER_3 3 /* Implements system specification 3.1 - 3.2 - 3.31 */
257 #define CSD_SPEC_VER_4 4 /* Implements system specification 4.0 - 4.1 */
258
259 /*
260 * EXT_CSD fields
261 */
262
263 #define EXT_CSD_BUS_WIDTH 183 /* R/W */
264 #define EXT_CSD_HS_TIMING 185 /* R/W */
265 #define EXT_CSD_CARD_TYPE 196 /* RO */
266 #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
267
268 /*
269 * EXT_CSD field definitions
270 */
271
272 #define EXT_CSD_CMD_SET_NORMAL (1<<0)
273 #define EXT_CSD_CMD_SET_SECURE (1<<1)
274 #define EXT_CSD_CMD_SET_CPSECURE (1<<2)
275
276 #define EXT_CSD_CARD_TYPE_26 (1<<0) /* Card can run at 26MHz */
277 #define EXT_CSD_CARD_TYPE_52 (1<<1) /* Card can run at 52MHz */
278
279 #define EXT_CSD_BUS_WIDTH_1 0 /* Card is in 1 bit mode */
280 #define EXT_CSD_BUS_WIDTH_4 1 /* Card is in 4 bit mode */
281 #define EXT_CSD_BUS_WIDTH_8 2 /* Card is in 8 bit mode */
282
283 /*
284 * MMC_SWITCH access modes
285 */
286
287 #define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
288 #define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits which are 1 in value */
289 #define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits which are 1 in value */
290 #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
291
292 /*
293 * SCR field definitions
294 */
295
296 #define SCR_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.01 */
297 #define SCR_SPEC_VER_1 1 /* Implements system specification 1.10 */
298 #define SCR_SPEC_VER_2 2 /* Implements system specification 2.00 */
299
300 /*
301 * SD bus widths
302 */
303 #define SD_BUS_WIDTH_1 0
304 #define SD_BUS_WIDTH_4 2
305
306 #endif /* MMC_MMC_PROTOCOL_H */
307 1 /*
308 2 * Header for MultiMediaCard (MMC)
include/linux/mmc/sd.h
File was created 1 /*
2 * include/linux/mmc/sd.h
3 *
4 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
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 #ifndef MMC_SD_H
13 #define MMC_SD_H
14
15 /* SD commands type argument response */
16 /* class 0 */
17 /* This is basically the same command as for MMC with some quirks. */
18 #define SD_SEND_RELATIVE_ADDR 3 /* bcr R6 */
19 #define SD_SEND_IF_COND 8 /* bcr [11:0] See below R7 */
20
21 /* class 10 */
22 #define SD_SWITCH 6 /* adtc [31:0] See below R1 */
23
24 /* Application commands */
25 #define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
26 #define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */
27 #define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */
28 #define SD_APP_SEND_SCR 51 /* adtc R1 */
29
30 /*
31 * SD_SWITCH argument format:
32 *
33 * [31] Check (0) or switch (1)
34 * [30:24] Reserved (0)
35 * [23:20] Function group 6
36 * [19:16] Function group 5
37 * [15:12] Function group 4
38 * [11:8] Function group 3
39 * [7:4] Function group 2
40 * [3:0] Function group 1
41 */
42
43 /*
44 * SD_SEND_IF_COND argument format:
45 *
46 * [31:12] Reserved (0)
47 * [11:8] Host Voltage Supply Flags
48 * [7:0] Check Pattern (0xAA)
49 */
50
51 /*
52 * SCR field definitions
53 */
54
55 #define SCR_SPEC_VER_0 0 /* Implements system specification 1.0 - 1.01 */
56 #define SCR_SPEC_VER_1 1 /* Implements system specification 1.10 */
57 #define SCR_SPEC_VER_2 2 /* Implements system specification 2.00 */
58
59 /*
60 * SD bus widths
61 */
62 #define SD_BUS_WIDTH_1 0
63 #define SD_BUS_WIDTH_4 2
64
65 /*
66 * SD_SWITCH mode
67 */
68 #define SD_SWITCH_CHECK 0
69 #define SD_SWITCH_SET 1
70
71 /*
72 * SD_SWITCH function groups
73 */
74 #define SD_SWITCH_GRP_ACCESS 0
75
76 /*
77 * SD_SWITCH access modes
78 */
79 #define SD_SWITCH_ACCESS_DEF 0
80 #define SD_SWITCH_ACCESS_HS 1
81
82 #endif
83
84