mmc_block: hard code 512 byte block size
[linux-2.6/linux-2.6-openrd.git] / drivers / mmc / card / block.c
blob1d1e469e08ea8f79e5063fef5406c0c09ef59ef3
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
18 * 28 May 2002
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/errno.h>
27 #include <linux/hdreg.h>
28 #include <linux/kdev_t.h>
29 #include <linux/blkdev.h>
30 #include <linux/mutex.h>
31 #include <linux/scatterlist.h>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
41 #include "queue.h"
44 * max 8 partitions per card
46 #define MMC_SHIFT 3
47 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
49 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
52 * There is one mmc_blk_data per slot.
54 struct mmc_blk_data {
55 spinlock_t lock;
56 struct gendisk *disk;
57 struct mmc_queue queue;
59 unsigned int usage;
60 unsigned int read_only;
63 static DEFINE_MUTEX(open_lock);
65 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
67 struct mmc_blk_data *md;
69 mutex_lock(&open_lock);
70 md = disk->private_data;
71 if (md && md->usage == 0)
72 md = NULL;
73 if (md)
74 md->usage++;
75 mutex_unlock(&open_lock);
77 return md;
80 static void mmc_blk_put(struct mmc_blk_data *md)
82 mutex_lock(&open_lock);
83 md->usage--;
84 if (md->usage == 0) {
85 int devidx = md->disk->first_minor >> MMC_SHIFT;
86 __clear_bit(devidx, dev_use);
88 put_disk(md->disk);
89 kfree(md);
91 mutex_unlock(&open_lock);
94 static int mmc_blk_open(struct inode *inode, struct file *filp)
96 struct mmc_blk_data *md;
97 int ret = -ENXIO;
99 md = mmc_blk_get(inode->i_bdev->bd_disk);
100 if (md) {
101 if (md->usage == 2)
102 check_disk_change(inode->i_bdev);
103 ret = 0;
105 if ((filp->f_mode & FMODE_WRITE) && md->read_only) {
106 mmc_blk_put(md);
107 ret = -EROFS;
111 return ret;
114 static int mmc_blk_release(struct inode *inode, struct file *filp)
116 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
118 mmc_blk_put(md);
119 return 0;
122 static int
123 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
125 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
126 geo->heads = 4;
127 geo->sectors = 16;
128 return 0;
131 static struct block_device_operations mmc_bdops = {
132 .open = mmc_blk_open,
133 .release = mmc_blk_release,
134 .getgeo = mmc_blk_getgeo,
135 .owner = THIS_MODULE,
138 struct mmc_blk_request {
139 struct mmc_request mrq;
140 struct mmc_command cmd;
141 struct mmc_command stop;
142 struct mmc_data data;
145 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
147 int err;
148 u32 blocks;
150 struct mmc_request mrq;
151 struct mmc_command cmd;
152 struct mmc_data data;
153 unsigned int timeout_us;
155 struct scatterlist sg;
157 memset(&cmd, 0, sizeof(struct mmc_command));
159 cmd.opcode = MMC_APP_CMD;
160 cmd.arg = card->rca << 16;
161 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
163 err = mmc_wait_for_cmd(card->host, &cmd, 0);
164 if (err)
165 return (u32)-1;
166 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
167 return (u32)-1;
169 memset(&cmd, 0, sizeof(struct mmc_command));
171 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
172 cmd.arg = 0;
173 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
175 memset(&data, 0, sizeof(struct mmc_data));
177 data.timeout_ns = card->csd.tacc_ns * 100;
178 data.timeout_clks = card->csd.tacc_clks * 100;
180 timeout_us = data.timeout_ns / 1000;
181 timeout_us += data.timeout_clks * 1000 /
182 (card->host->ios.clock / 1000);
184 if (timeout_us > 100000) {
185 data.timeout_ns = 100000000;
186 data.timeout_clks = 0;
189 data.blksz = 4;
190 data.blocks = 1;
191 data.flags = MMC_DATA_READ;
192 data.sg = &sg;
193 data.sg_len = 1;
195 memset(&mrq, 0, sizeof(struct mmc_request));
197 mrq.cmd = &cmd;
198 mrq.data = &data;
200 sg_init_one(&sg, &blocks, 4);
202 mmc_wait_for_req(card->host, &mrq);
204 if (cmd.error || data.error)
205 return (u32)-1;
207 blocks = ntohl(blocks);
209 return blocks;
212 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
214 struct mmc_blk_data *md = mq->data;
215 struct mmc_card *card = md->queue.card;
216 struct mmc_blk_request brq;
217 int ret = 1;
219 mmc_claim_host(card->host);
221 do {
222 struct mmc_command cmd;
223 u32 readcmd, writecmd;
225 memset(&brq, 0, sizeof(struct mmc_blk_request));
226 brq.mrq.cmd = &brq.cmd;
227 brq.mrq.data = &brq.data;
229 brq.cmd.arg = req->sector;
230 if (!mmc_card_blockaddr(card))
231 brq.cmd.arg <<= 9;
232 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
233 brq.data.blksz = 512;
234 brq.stop.opcode = MMC_STOP_TRANSMISSION;
235 brq.stop.arg = 0;
236 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
237 brq.data.blocks = req->nr_sectors;
239 if (brq.data.blocks > 1) {
240 /* SPI multiblock writes terminate using a special
241 * token, not a STOP_TRANSMISSION request.
243 if (!mmc_host_is_spi(card->host)
244 || rq_data_dir(req) == READ)
245 brq.mrq.stop = &brq.stop;
246 readcmd = MMC_READ_MULTIPLE_BLOCK;
247 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
248 } else {
249 brq.mrq.stop = NULL;
250 readcmd = MMC_READ_SINGLE_BLOCK;
251 writecmd = MMC_WRITE_BLOCK;
254 if (rq_data_dir(req) == READ) {
255 brq.cmd.opcode = readcmd;
256 brq.data.flags |= MMC_DATA_READ;
257 } else {
258 brq.cmd.opcode = writecmd;
259 brq.data.flags |= MMC_DATA_WRITE;
262 mmc_set_data_timeout(&brq.data, card);
264 brq.data.sg = mq->sg;
265 brq.data.sg_len = mmc_queue_map_sg(mq);
267 mmc_queue_bounce_pre(mq);
269 mmc_wait_for_req(card->host, &brq.mrq);
271 mmc_queue_bounce_post(mq);
274 * Check for errors here, but don't jump to cmd_err
275 * until later as we need to wait for the card to leave
276 * programming mode even when things go wrong.
278 if (brq.cmd.error) {
279 printk(KERN_ERR "%s: error %d sending read/write command\n",
280 req->rq_disk->disk_name, brq.cmd.error);
283 if (brq.data.error) {
284 printk(KERN_ERR "%s: error %d transferring data\n",
285 req->rq_disk->disk_name, brq.data.error);
288 if (brq.stop.error) {
289 printk(KERN_ERR "%s: error %d sending stop command\n",
290 req->rq_disk->disk_name, brq.stop.error);
293 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
294 do {
295 int err;
297 cmd.opcode = MMC_SEND_STATUS;
298 cmd.arg = card->rca << 16;
299 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
300 err = mmc_wait_for_cmd(card->host, &cmd, 5);
301 if (err) {
302 printk(KERN_ERR "%s: error %d requesting status\n",
303 req->rq_disk->disk_name, err);
304 goto cmd_err;
307 * Some cards mishandle the status bits,
308 * so make sure to check both the busy
309 * indication and the card state.
311 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
312 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
314 #if 0
315 if (cmd.resp[0] & ~0x00000900)
316 printk(KERN_ERR "%s: status = %08x\n",
317 req->rq_disk->disk_name, cmd.resp[0]);
318 if (mmc_decode_status(cmd.resp))
319 goto cmd_err;
320 #endif
323 if (brq.cmd.error || brq.data.error || brq.stop.error)
324 goto cmd_err;
327 * A block was successfully transferred.
329 spin_lock_irq(&md->lock);
330 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
331 spin_unlock_irq(&md->lock);
332 } while (ret);
334 mmc_release_host(card->host);
336 return 1;
338 cmd_err:
340 * If this is an SD card and we're writing, we can first
341 * mark the known good sectors as ok.
343 * If the card is not SD, we can still ok written sectors
344 * as reported by the controller (which might be less than
345 * the real number of written sectors, but never more).
347 * For reads we just fail the entire chunk as that should
348 * be safe in all cases.
350 if (rq_data_dir(req) != READ) {
351 if (mmc_card_sd(card)) {
352 u32 blocks;
354 blocks = mmc_sd_num_wr_blocks(card);
355 if (blocks != (u32)-1) {
356 spin_lock_irq(&md->lock);
357 ret = __blk_end_request(req, 0, blocks << 9);
358 spin_unlock_irq(&md->lock);
360 } else {
361 spin_lock_irq(&md->lock);
362 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
363 spin_unlock_irq(&md->lock);
367 mmc_release_host(card->host);
369 spin_lock_irq(&md->lock);
370 while (ret)
371 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
372 spin_unlock_irq(&md->lock);
374 return 0;
378 static inline int mmc_blk_readonly(struct mmc_card *card)
380 return mmc_card_readonly(card) ||
381 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
384 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
386 struct mmc_blk_data *md;
387 int devidx, ret;
389 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
390 if (devidx >= MMC_NUM_MINORS)
391 return ERR_PTR(-ENOSPC);
392 __set_bit(devidx, dev_use);
394 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
395 if (!md) {
396 ret = -ENOMEM;
397 goto out;
402 * Set the read-only status based on the supported commands
403 * and the write protect switch.
405 md->read_only = mmc_blk_readonly(card);
407 md->disk = alloc_disk(1 << MMC_SHIFT);
408 if (md->disk == NULL) {
409 ret = -ENOMEM;
410 goto err_kfree;
413 spin_lock_init(&md->lock);
414 md->usage = 1;
416 ret = mmc_init_queue(&md->queue, card, &md->lock);
417 if (ret)
418 goto err_putdisk;
420 md->queue.issue_fn = mmc_blk_issue_rq;
421 md->queue.data = md;
423 md->disk->major = MMC_BLOCK_MAJOR;
424 md->disk->first_minor = devidx << MMC_SHIFT;
425 md->disk->fops = &mmc_bdops;
426 md->disk->private_data = md;
427 md->disk->queue = md->queue.queue;
428 md->disk->driverfs_dev = &card->dev;
431 * As discussed on lkml, GENHD_FL_REMOVABLE should:
433 * - be set for removable media with permanent block devices
434 * - be unset for removable block devices with permanent media
436 * Since MMC block devices clearly fall under the second
437 * case, we do not set GENHD_FL_REMOVABLE. Userspace
438 * should use the block device creation/destruction hotplug
439 * messages to tell when the card is present.
442 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
444 blk_queue_hardsect_size(md->queue.queue, 512);
446 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
448 * The EXT_CSD sector count is in number or 512 byte
449 * sectors.
451 set_capacity(md->disk, card->ext_csd.sectors);
452 } else {
454 * The CSD capacity field is in units of read_blkbits.
455 * set_capacity takes units of 512 bytes.
457 set_capacity(md->disk,
458 card->csd.capacity << (card->csd.read_blkbits - 9));
460 return md;
462 err_putdisk:
463 put_disk(md->disk);
464 err_kfree:
465 kfree(md);
466 out:
467 return ERR_PTR(ret);
470 static int
471 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
473 struct mmc_command cmd;
474 int err;
476 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
477 if (mmc_card_blockaddr(card))
478 return 0;
480 mmc_claim_host(card->host);
481 cmd.opcode = MMC_SET_BLOCKLEN;
482 cmd.arg = 512;
483 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
484 err = mmc_wait_for_cmd(card->host, &cmd, 5);
485 mmc_release_host(card->host);
487 if (err) {
488 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
489 md->disk->disk_name, cmd.arg, err);
490 return -EINVAL;
493 return 0;
496 static int mmc_blk_probe(struct mmc_card *card)
498 struct mmc_blk_data *md;
499 int err;
502 * Check that the card supports the command class(es) we need.
504 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
505 return -ENODEV;
507 md = mmc_blk_alloc(card);
508 if (IS_ERR(md))
509 return PTR_ERR(md);
511 err = mmc_blk_set_blksize(md, card);
512 if (err)
513 goto out;
515 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
516 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
517 (unsigned long long)(get_capacity(md->disk) >> 1),
518 md->read_only ? "(ro)" : "");
520 mmc_set_drvdata(card, md);
521 add_disk(md->disk);
522 return 0;
524 out:
525 mmc_blk_put(md);
527 return err;
530 static void mmc_blk_remove(struct mmc_card *card)
532 struct mmc_blk_data *md = mmc_get_drvdata(card);
534 if (md) {
535 /* Stop new requests from getting into the queue */
536 del_gendisk(md->disk);
538 /* Then flush out any already in there */
539 mmc_cleanup_queue(&md->queue);
541 mmc_blk_put(md);
543 mmc_set_drvdata(card, NULL);
546 #ifdef CONFIG_PM
547 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
549 struct mmc_blk_data *md = mmc_get_drvdata(card);
551 if (md) {
552 mmc_queue_suspend(&md->queue);
554 return 0;
557 static int mmc_blk_resume(struct mmc_card *card)
559 struct mmc_blk_data *md = mmc_get_drvdata(card);
561 if (md) {
562 mmc_blk_set_blksize(md, card);
563 mmc_queue_resume(&md->queue);
565 return 0;
567 #else
568 #define mmc_blk_suspend NULL
569 #define mmc_blk_resume NULL
570 #endif
572 static struct mmc_driver mmc_driver = {
573 .drv = {
574 .name = "mmcblk",
576 .probe = mmc_blk_probe,
577 .remove = mmc_blk_remove,
578 .suspend = mmc_blk_suspend,
579 .resume = mmc_blk_resume,
582 static int __init mmc_blk_init(void)
584 int res;
586 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
587 if (res)
588 goto out;
590 res = mmc_register_driver(&mmc_driver);
591 if (res)
592 goto out2;
594 return 0;
595 out2:
596 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
597 out:
598 return res;
601 static void __exit mmc_blk_exit(void)
603 mmc_unregister_driver(&mmc_driver);
604 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
607 module_init(mmc_blk_init);
608 module_exit(mmc_blk_exit);
610 MODULE_LICENSE("GPL");
611 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");