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
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.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>
32 #include <linux/string_helpers.h>
34 #include <linux/mmc/card.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/mmc.h>
37 #include <linux/mmc/sd.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
45 * max 8 partitions per card
48 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
50 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
53 * There is one mmc_blk_data per slot.
58 struct mmc_queue queue;
61 unsigned int read_only;
64 static DEFINE_MUTEX(open_lock);
66 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
68 struct mmc_blk_data *md;
70 mutex_lock(&open_lock);
71 md = disk->private_data;
72 if (md && md->usage == 0)
76 mutex_unlock(&open_lock);
81 static void mmc_blk_put(struct mmc_blk_data *md)
83 mutex_lock(&open_lock);
86 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
87 __clear_bit(devidx, dev_use);
92 mutex_unlock(&open_lock);
95 static int mmc_blk_open(struct inode *inode, struct file *filp)
97 struct mmc_blk_data *md;
100 md = mmc_blk_get(inode->i_bdev->bd_disk);
103 check_disk_change(inode->i_bdev);
106 if ((filp->f_mode & FMODE_WRITE) && md->read_only) {
115 static int mmc_blk_release(struct inode *inode, struct file *filp)
117 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
124 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
126 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
132 static struct block_device_operations mmc_bdops = {
133 .open = mmc_blk_open,
134 .release = mmc_blk_release,
135 .getgeo = mmc_blk_getgeo,
136 .owner = THIS_MODULE,
139 struct mmc_blk_request {
140 struct mmc_request mrq;
141 struct mmc_command cmd;
142 struct mmc_command stop;
143 struct mmc_data data;
146 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
151 struct mmc_request mrq;
152 struct mmc_command cmd;
153 struct mmc_data data;
154 unsigned int timeout_us;
156 struct scatterlist sg;
158 memset(&cmd, 0, sizeof(struct mmc_command));
160 cmd.opcode = MMC_APP_CMD;
161 cmd.arg = card->rca << 16;
162 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
164 err = mmc_wait_for_cmd(card->host, &cmd, 0);
167 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
170 memset(&cmd, 0, sizeof(struct mmc_command));
172 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
174 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
176 memset(&data, 0, sizeof(struct mmc_data));
178 data.timeout_ns = card->csd.tacc_ns * 100;
179 data.timeout_clks = card->csd.tacc_clks * 100;
181 timeout_us = data.timeout_ns / 1000;
182 timeout_us += data.timeout_clks * 1000 /
183 (card->host->ios.clock / 1000);
185 if (timeout_us > 100000) {
186 data.timeout_ns = 100000000;
187 data.timeout_clks = 0;
192 data.flags = MMC_DATA_READ;
196 memset(&mrq, 0, sizeof(struct mmc_request));
201 sg_init_one(&sg, &blocks, 4);
203 mmc_wait_for_req(card->host, &mrq);
205 if (cmd.error || data.error)
208 blocks = ntohl(blocks);
213 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
215 struct mmc_blk_data *md = mq->data;
216 struct mmc_card *card = md->queue.card;
217 struct mmc_blk_request brq;
220 mmc_claim_host(card->host);
223 struct mmc_command cmd;
224 u32 readcmd, writecmd;
226 memset(&brq, 0, sizeof(struct mmc_blk_request));
227 brq.mrq.cmd = &brq.cmd;
228 brq.mrq.data = &brq.data;
230 brq.cmd.arg = req->sector;
231 if (!mmc_card_blockaddr(card))
233 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
234 brq.data.blksz = 512;
235 brq.stop.opcode = MMC_STOP_TRANSMISSION;
237 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
238 brq.data.blocks = req->nr_sectors;
240 if (brq.data.blocks > 1) {
241 /* SPI multiblock writes terminate using a special
242 * token, not a STOP_TRANSMISSION request.
244 if (!mmc_host_is_spi(card->host)
245 || rq_data_dir(req) == READ)
246 brq.mrq.stop = &brq.stop;
247 readcmd = MMC_READ_MULTIPLE_BLOCK;
248 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
251 readcmd = MMC_READ_SINGLE_BLOCK;
252 writecmd = MMC_WRITE_BLOCK;
255 if (rq_data_dir(req) == READ) {
256 brq.cmd.opcode = readcmd;
257 brq.data.flags |= MMC_DATA_READ;
259 brq.cmd.opcode = writecmd;
260 brq.data.flags |= MMC_DATA_WRITE;
263 mmc_set_data_timeout(&brq.data, card);
265 brq.data.sg = mq->sg;
266 brq.data.sg_len = mmc_queue_map_sg(mq);
268 mmc_queue_bounce_pre(mq);
270 mmc_wait_for_req(card->host, &brq.mrq);
272 mmc_queue_bounce_post(mq);
275 * Check for errors here, but don't jump to cmd_err
276 * until later as we need to wait for the card to leave
277 * programming mode even when things go wrong.
280 printk(KERN_ERR "%s: error %d sending read/write command\n",
281 req->rq_disk->disk_name, brq.cmd.error);
284 if (brq.data.error) {
285 printk(KERN_ERR "%s: error %d transferring data\n",
286 req->rq_disk->disk_name, brq.data.error);
289 if (brq.stop.error) {
290 printk(KERN_ERR "%s: error %d sending stop command\n",
291 req->rq_disk->disk_name, brq.stop.error);
294 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
298 cmd.opcode = MMC_SEND_STATUS;
299 cmd.arg = card->rca << 16;
300 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
301 err = mmc_wait_for_cmd(card->host, &cmd, 5);
303 printk(KERN_ERR "%s: error %d requesting status\n",
304 req->rq_disk->disk_name, err);
308 * Some cards mishandle the status bits,
309 * so make sure to check both the busy
310 * indication and the card state.
312 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
313 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
316 if (cmd.resp[0] & ~0x00000900)
317 printk(KERN_ERR "%s: status = %08x\n",
318 req->rq_disk->disk_name, cmd.resp[0]);
319 if (mmc_decode_status(cmd.resp))
324 if (brq.cmd.error || brq.data.error || brq.stop.error)
328 * A block was successfully transferred.
330 spin_lock_irq(&md->lock);
331 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
332 spin_unlock_irq(&md->lock);
335 mmc_release_host(card->host);
341 * If this is an SD card and we're writing, we can first
342 * mark the known good sectors as ok.
344 * If the card is not SD, we can still ok written sectors
345 * as reported by the controller (which might be less than
346 * the real number of written sectors, but never more).
348 * For reads we just fail the entire chunk as that should
349 * be safe in all cases.
351 if (rq_data_dir(req) != READ) {
352 if (mmc_card_sd(card)) {
355 blocks = mmc_sd_num_wr_blocks(card);
356 if (blocks != (u32)-1) {
357 spin_lock_irq(&md->lock);
358 ret = __blk_end_request(req, 0, blocks << 9);
359 spin_unlock_irq(&md->lock);
362 spin_lock_irq(&md->lock);
363 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
364 spin_unlock_irq(&md->lock);
368 mmc_release_host(card->host);
370 spin_lock_irq(&md->lock);
372 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
373 spin_unlock_irq(&md->lock);
379 static inline int mmc_blk_readonly(struct mmc_card *card)
381 return mmc_card_readonly(card) ||
382 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
385 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
387 struct mmc_blk_data *md;
390 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
391 if (devidx >= MMC_NUM_MINORS)
392 return ERR_PTR(-ENOSPC);
393 __set_bit(devidx, dev_use);
395 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
403 * Set the read-only status based on the supported commands
404 * and the write protect switch.
406 md->read_only = mmc_blk_readonly(card);
408 md->disk = alloc_disk(1 << MMC_SHIFT);
409 if (md->disk == NULL) {
414 spin_lock_init(&md->lock);
417 ret = mmc_init_queue(&md->queue, card, &md->lock);
421 md->queue.issue_fn = mmc_blk_issue_rq;
424 md->disk->major = MMC_BLOCK_MAJOR;
425 md->disk->first_minor = devidx << MMC_SHIFT;
426 md->disk->fops = &mmc_bdops;
427 md->disk->private_data = md;
428 md->disk->queue = md->queue.queue;
429 md->disk->driverfs_dev = &card->dev;
432 * As discussed on lkml, GENHD_FL_REMOVABLE should:
434 * - be set for removable media with permanent block devices
435 * - be unset for removable block devices with permanent media
437 * Since MMC block devices clearly fall under the second
438 * case, we do not set GENHD_FL_REMOVABLE. Userspace
439 * should use the block device creation/destruction hotplug
440 * messages to tell when the card is present.
443 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
445 blk_queue_hardsect_size(md->queue.queue, 512);
447 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
449 * The EXT_CSD sector count is in number or 512 byte
452 set_capacity(md->disk, card->ext_csd.sectors);
455 * The CSD capacity field is in units of read_blkbits.
456 * set_capacity takes units of 512 bytes.
458 set_capacity(md->disk,
459 card->csd.capacity << (card->csd.read_blkbits - 9));
472 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
474 struct mmc_command cmd;
477 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
478 if (mmc_card_blockaddr(card))
481 mmc_claim_host(card->host);
482 cmd.opcode = MMC_SET_BLOCKLEN;
484 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
485 err = mmc_wait_for_cmd(card->host, &cmd, 5);
486 mmc_release_host(card->host);
489 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
490 md->disk->disk_name, cmd.arg, err);
497 static int mmc_blk_probe(struct mmc_card *card)
499 struct mmc_blk_data *md;
505 * Check that the card supports the command class(es) we need.
507 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
510 md = mmc_blk_alloc(card);
514 err = mmc_blk_set_blksize(md, card);
518 string_get_size(get_capacity(md->disk) << 9, STRING_UNITS_2,
519 cap_str, sizeof(cap_str));
520 printk(KERN_INFO "%s: %s %s %s %s\n",
521 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
522 cap_str, md->read_only ? "(ro)" : "");
524 mmc_set_drvdata(card, md);
534 static void mmc_blk_remove(struct mmc_card *card)
536 struct mmc_blk_data *md = mmc_get_drvdata(card);
539 /* Stop new requests from getting into the queue */
540 del_gendisk(md->disk);
542 /* Then flush out any already in there */
543 mmc_cleanup_queue(&md->queue);
547 mmc_set_drvdata(card, NULL);
551 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
553 struct mmc_blk_data *md = mmc_get_drvdata(card);
556 mmc_queue_suspend(&md->queue);
561 static int mmc_blk_resume(struct mmc_card *card)
563 struct mmc_blk_data *md = mmc_get_drvdata(card);
566 mmc_blk_set_blksize(md, card);
567 mmc_queue_resume(&md->queue);
572 #define mmc_blk_suspend NULL
573 #define mmc_blk_resume NULL
576 static struct mmc_driver mmc_driver = {
580 .probe = mmc_blk_probe,
581 .remove = mmc_blk_remove,
582 .suspend = mmc_blk_suspend,
583 .resume = mmc_blk_resume,
586 static int __init mmc_blk_init(void)
590 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
594 res = mmc_register_driver(&mmc_driver);
600 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
605 static void __exit mmc_blk_exit(void)
607 mmc_unregister_driver(&mmc_driver);
608 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
611 module_init(mmc_blk_init);
612 module_exit(mmc_blk_exit);
614 MODULE_LICENSE("GPL");
615 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");