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 block_device *bdev, fmode_t mode)
97 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
102 check_disk_change(bdev);
105 if ((mode & FMODE_WRITE) && md->read_only) {
114 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
116 struct mmc_blk_data *md = disk->private_data;
123 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
125 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
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)
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);
166 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
169 memset(&cmd, 0, sizeof(struct mmc_command));
171 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
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;
191 data.flags = MMC_DATA_READ;
195 memset(&mrq, 0, sizeof(struct mmc_request));
200 sg_init_one(&sg, &blocks, 4);
202 mmc_wait_for_req(card->host, &mrq);
204 if (cmd.error || data.error)
207 return ntohl(blocks);
210 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
212 struct mmc_blk_data *md = mq->data;
213 struct mmc_card *card = md->queue.card;
214 struct mmc_blk_request brq;
217 mmc_claim_host(card->host);
220 struct mmc_command cmd;
221 u32 readcmd, writecmd;
223 memset(&brq, 0, sizeof(struct mmc_blk_request));
224 brq.mrq.cmd = &brq.cmd;
225 brq.mrq.data = &brq.data;
227 brq.cmd.arg = req->sector;
228 if (!mmc_card_blockaddr(card))
230 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
231 brq.data.blksz = 512;
232 brq.stop.opcode = MMC_STOP_TRANSMISSION;
234 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
235 brq.data.blocks = req->nr_sectors;
237 if (brq.data.blocks > 1) {
238 /* SPI multiblock writes terminate using a special
239 * token, not a STOP_TRANSMISSION request.
241 if (!mmc_host_is_spi(card->host)
242 || rq_data_dir(req) == READ)
243 brq.mrq.stop = &brq.stop;
244 readcmd = MMC_READ_MULTIPLE_BLOCK;
245 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
248 readcmd = MMC_READ_SINGLE_BLOCK;
249 writecmd = MMC_WRITE_BLOCK;
252 if (rq_data_dir(req) == READ) {
253 brq.cmd.opcode = readcmd;
254 brq.data.flags |= MMC_DATA_READ;
256 brq.cmd.opcode = writecmd;
257 brq.data.flags |= MMC_DATA_WRITE;
260 mmc_set_data_timeout(&brq.data, card);
262 brq.data.sg = mq->sg;
263 brq.data.sg_len = mmc_queue_map_sg(mq);
265 mmc_queue_bounce_pre(mq);
267 mmc_wait_for_req(card->host, &brq.mrq);
269 mmc_queue_bounce_post(mq);
272 * Check for errors here, but don't jump to cmd_err
273 * until later as we need to wait for the card to leave
274 * programming mode even when things go wrong.
277 printk(KERN_ERR "%s: error %d sending read/write command\n",
278 req->rq_disk->disk_name, brq.cmd.error);
281 if (brq.data.error) {
282 printk(KERN_ERR "%s: error %d transferring data\n",
283 req->rq_disk->disk_name, brq.data.error);
286 if (brq.stop.error) {
287 printk(KERN_ERR "%s: error %d sending stop command\n",
288 req->rq_disk->disk_name, brq.stop.error);
291 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
295 cmd.opcode = MMC_SEND_STATUS;
296 cmd.arg = card->rca << 16;
297 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
298 err = mmc_wait_for_cmd(card->host, &cmd, 5);
300 printk(KERN_ERR "%s: error %d requesting status\n",
301 req->rq_disk->disk_name, err);
305 * Some cards mishandle the status bits,
306 * so make sure to check both the busy
307 * indication and the card state.
309 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
310 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
313 if (cmd.resp[0] & ~0x00000900)
314 printk(KERN_ERR "%s: status = %08x\n",
315 req->rq_disk->disk_name, cmd.resp[0]);
316 if (mmc_decode_status(cmd.resp))
321 if (brq.cmd.error || brq.data.error || brq.stop.error)
325 * A block was successfully transferred.
327 spin_lock_irq(&md->lock);
328 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
329 spin_unlock_irq(&md->lock);
332 mmc_release_host(card->host);
338 * If this is an SD card and we're writing, we can first
339 * mark the known good sectors as ok.
341 * If the card is not SD, we can still ok written sectors
342 * as reported by the controller (which might be less than
343 * the real number of written sectors, but never more).
345 * For reads we just fail the entire chunk as that should
346 * be safe in all cases.
348 if (rq_data_dir(req) != READ) {
349 if (mmc_card_sd(card)) {
352 blocks = mmc_sd_num_wr_blocks(card);
353 if (blocks != (u32)-1) {
354 spin_lock_irq(&md->lock);
355 ret = __blk_end_request(req, 0, blocks << 9);
356 spin_unlock_irq(&md->lock);
359 spin_lock_irq(&md->lock);
360 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
361 spin_unlock_irq(&md->lock);
365 mmc_release_host(card->host);
367 spin_lock_irq(&md->lock);
369 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
370 spin_unlock_irq(&md->lock);
376 static inline int mmc_blk_readonly(struct mmc_card *card)
378 return mmc_card_readonly(card) ||
379 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
382 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
384 struct mmc_blk_data *md;
387 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
388 if (devidx >= MMC_NUM_MINORS)
389 return ERR_PTR(-ENOSPC);
390 __set_bit(devidx, dev_use);
392 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
400 * Set the read-only status based on the supported commands
401 * and the write protect switch.
403 md->read_only = mmc_blk_readonly(card);
405 md->disk = alloc_disk(1 << MMC_SHIFT);
406 if (md->disk == NULL) {
411 spin_lock_init(&md->lock);
414 ret = mmc_init_queue(&md->queue, card, &md->lock);
418 md->queue.issue_fn = mmc_blk_issue_rq;
421 md->disk->major = MMC_BLOCK_MAJOR;
422 md->disk->first_minor = devidx << MMC_SHIFT;
423 md->disk->fops = &mmc_bdops;
424 md->disk->private_data = md;
425 md->disk->queue = md->queue.queue;
426 md->disk->driverfs_dev = &card->dev;
429 * As discussed on lkml, GENHD_FL_REMOVABLE should:
431 * - be set for removable media with permanent block devices
432 * - be unset for removable block devices with permanent media
434 * Since MMC block devices clearly fall under the second
435 * case, we do not set GENHD_FL_REMOVABLE. Userspace
436 * should use the block device creation/destruction hotplug
437 * messages to tell when the card is present.
440 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
442 blk_queue_hardsect_size(md->queue.queue, 512);
444 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
446 * The EXT_CSD sector count is in number or 512 byte
449 set_capacity(md->disk, card->ext_csd.sectors);
452 * The CSD capacity field is in units of read_blkbits.
453 * set_capacity takes units of 512 bytes.
455 set_capacity(md->disk,
456 card->csd.capacity << (card->csd.read_blkbits - 9));
469 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
471 struct mmc_command cmd;
474 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
475 if (mmc_card_blockaddr(card))
478 mmc_claim_host(card->host);
479 cmd.opcode = MMC_SET_BLOCKLEN;
481 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
482 err = mmc_wait_for_cmd(card->host, &cmd, 5);
483 mmc_release_host(card->host);
486 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
487 md->disk->disk_name, cmd.arg, err);
494 static int mmc_blk_probe(struct mmc_card *card)
496 struct mmc_blk_data *md;
502 * Check that the card supports the command class(es) we need.
504 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
507 md = mmc_blk_alloc(card);
511 err = mmc_blk_set_blksize(md, card);
515 string_get_size(get_capacity(md->disk) << 9, STRING_UNITS_2,
516 cap_str, sizeof(cap_str));
517 printk(KERN_INFO "%s: %s %s %s %s\n",
518 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
519 cap_str, md->read_only ? "(ro)" : "");
521 mmc_set_drvdata(card, md);
531 static void mmc_blk_remove(struct mmc_card *card)
533 struct mmc_blk_data *md = mmc_get_drvdata(card);
536 /* Stop new requests from getting into the queue */
537 del_gendisk(md->disk);
539 /* Then flush out any already in there */
540 mmc_cleanup_queue(&md->queue);
544 mmc_set_drvdata(card, NULL);
548 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
550 struct mmc_blk_data *md = mmc_get_drvdata(card);
553 mmc_queue_suspend(&md->queue);
558 static int mmc_blk_resume(struct mmc_card *card)
560 struct mmc_blk_data *md = mmc_get_drvdata(card);
563 mmc_blk_set_blksize(md, card);
564 mmc_queue_resume(&md->queue);
569 #define mmc_blk_suspend NULL
570 #define mmc_blk_resume NULL
573 static struct mmc_driver mmc_driver = {
577 .probe = mmc_blk_probe,
578 .remove = mmc_blk_remove,
579 .suspend = mmc_blk_suspend,
580 .resume = mmc_blk_resume,
583 static int __init mmc_blk_init(void)
587 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
591 res = mmc_register_driver(&mmc_driver);
597 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
602 static void __exit mmc_blk_exit(void)
604 mmc_unregister_driver(&mmc_driver);
605 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
608 module_init(mmc_blk_init);
609 module_exit(mmc_blk_exit);
611 MODULE_LICENSE("GPL");
612 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");