MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / mmc / mmc_block.c
blob35075561fe54c2fa5fa26f6f23be46886de9e850
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
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.
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.
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 * Author: Andrew Christian
17 * 28 May 2002
19 #include <linux/moduleparam.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/config.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/fs.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
34 #include <linux/mmc/card.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/protocol.h>
37 #include <linux/mmc/host.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #ifdef CONFIG_ARCH_UC7101
42 #include <asm/arch/gpio.h>
43 #endif
45 #include "mmc_queue.h"
48 * max 8 partitions per card
50 #define MMC_SHIFT 3
52 #if 0 // mask by Victor Yu. 03-07-2007
53 static int major;
54 #else
55 static int major=121;
56 #endif
59 * There is one mmc_blk_data per slot.
61 struct mmc_blk_data {
62 spinlock_t lock;
63 struct gendisk *disk;
64 struct mmc_queue queue;
66 unsigned int usage;
67 unsigned int block_bits;
68 unsigned int read_only;
71 static DEFINE_MUTEX(open_lock);
73 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
75 struct mmc_blk_data *md;
77 mutex_lock(&open_lock);
78 md = disk->private_data;
79 if (md && md->usage == 0)
80 md = NULL;
81 if (md)
82 md->usage++;
83 mutex_unlock(&open_lock);
85 return md;
88 static void mmc_blk_put(struct mmc_blk_data *md)
90 mutex_lock(&open_lock);
91 md->usage--;
92 if (md->usage == 0) {
93 put_disk(md->disk);
94 mmc_cleanup_queue(&md->queue);
95 kfree(md);
97 mutex_unlock(&open_lock);
100 static int mmc_blk_open(struct inode *inode, struct file *filp)
102 struct mmc_blk_data *md;
103 int ret = -ENXIO;
105 #if 0 // mask by Victor Yu. 03-07-2007
106 md = mmc_blk_get(inode->i_bdev->bd_disk);
107 #else
108 md = mmc_blk_get(inode->u.i_bdev->bd_disk);
109 #endif
110 if (md) {
111 if (md->usage == 2)
112 #if 0 // mask by Victor Yu. 03-07-2007
113 check_disk_change(inode->i_bdev);
114 #else
115 check_disk_change(inode->u.i_bdev);
116 #endif
117 ret = 0;
119 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
120 ret = -EROFS;
123 return ret;
126 static int mmc_blk_release(struct inode *inode, struct file *filp)
128 #if 0 // mask by Victor Yu. 03-07-2007
129 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
130 #else
131 struct mmc_blk_data *md = inode->u.i_bdev->bd_disk->private_data;
132 #endif
134 mmc_blk_put(md);
135 return 0;
138 static int
139 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
141 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
142 geo->heads = 4;
143 geo->sectors = 16;
144 return 0;
147 static struct block_device_operations mmc_bdops = {
148 .open = mmc_blk_open,
149 .release = mmc_blk_release,
150 .getgeo = mmc_blk_getgeo,
151 .owner = THIS_MODULE,
154 struct mmc_blk_request {
155 struct mmc_request mrq;
156 struct mmc_command cmd;
157 struct mmc_command stop;
158 struct mmc_data data;
161 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
163 struct mmc_blk_data *md = mq->data;
164 int stat = BLKPREP_OK;
167 * If we have no device, we haven't finished initialising.
169 if (!md || !mq->card) {
170 printk(KERN_ERR "%s: killing request - no device/host\n",
171 req->rq_disk->disk_name);
172 stat = BLKPREP_KILL;
175 return stat;
178 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
180 int err;
181 u32 blocks;
183 struct mmc_request mrq;
184 struct mmc_command cmd;
185 struct mmc_data data;
186 unsigned int timeout_us;
188 struct scatterlist sg;
190 memset(&cmd, 0, sizeof(struct mmc_command));
192 cmd.opcode = MMC_APP_CMD;
193 cmd.arg = card->rca << 16;
194 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
196 err = mmc_wait_for_cmd(card->host, &cmd, 0);
197 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
198 return (u32)-1;
200 memset(&cmd, 0, sizeof(struct mmc_command));
202 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
203 cmd.arg = 0;
204 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
206 memset(&data, 0, sizeof(struct mmc_data));
208 data.timeout_ns = card->csd.tacc_ns * 100;
209 data.timeout_clks = card->csd.tacc_clks * 100;
211 timeout_us = data.timeout_ns / 1000;
212 timeout_us += data.timeout_clks * 1000 /
213 (card->host->ios.clock / 1000);
215 if (timeout_us > 100000) {
216 data.timeout_ns = 100000000;
217 data.timeout_clks = 0;
220 data.blksz = 4;
221 data.blocks = 1;
222 data.flags = MMC_DATA_READ;
223 data.sg = &sg;
224 data.sg_len = 1;
226 memset(&mrq, 0, sizeof(struct mmc_request));
228 mrq.cmd = &cmd;
229 mrq.data = &data;
231 sg_init_one(&sg, &blocks, 4);
233 mmc_wait_for_req(card->host, &mrq);
235 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
236 return (u32)-1;
238 blocks = ntohl(blocks);
240 return blocks;
243 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
245 struct mmc_blk_data *md = mq->data;
246 struct mmc_card *card = md->queue.card;
247 struct mmc_blk_request brq;
248 int ret;
250 if (mmc_card_claim_host(card))
251 goto cmd_err;
253 do {
254 struct mmc_command cmd;
255 u32 readcmd, writecmd;
257 memset(&brq, 0, sizeof(struct mmc_blk_request));
258 brq.mrq.cmd = &brq.cmd;
259 brq.mrq.data = &brq.data;
261 brq.cmd.arg = req->sector << 9;
262 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
263 brq.data.blksz = 1 << md->block_bits;
264 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
265 brq.stop.opcode = MMC_STOP_TRANSMISSION;
266 brq.stop.arg = 0;
267 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
269 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
272 * If the host doesn't support multiple block writes, force
273 * block writes to single block. SD cards are excepted from
274 * this rule as they support querying the number of
275 * successfully written sectors.
277 if (rq_data_dir(req) != READ &&
278 !(card->host->caps & MMC_CAP_MULTIWRITE) &&
279 !mmc_card_sd(card))
280 brq.data.blocks = 1;
282 if (brq.data.blocks > 1) {
283 brq.data.flags |= MMC_DATA_MULTI;
284 brq.mrq.stop = &brq.stop;
285 readcmd = MMC_READ_MULTIPLE_BLOCK;
286 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
287 } else {
288 brq.mrq.stop = NULL;
289 readcmd = MMC_READ_SINGLE_BLOCK;
290 writecmd = MMC_WRITE_BLOCK;
293 if (rq_data_dir(req) == READ) {
294 brq.cmd.opcode = readcmd;
295 brq.data.flags |= MMC_DATA_READ;
296 } else {
297 brq.cmd.opcode = writecmd;
298 brq.data.flags |= MMC_DATA_WRITE;
301 brq.data.sg = mq->sg;
302 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
304 mmc_wait_for_req(card->host, &brq.mrq);
305 if (brq.cmd.error) {
306 printk(KERN_ERR "%s: victor error %d sending read/write command\n",
307 req->rq_disk->disk_name, brq.cmd.error);
308 goto cmd_err;
311 if (brq.data.error) {
312 printk(KERN_ERR "%s: error %d transferring data\n",
313 req->rq_disk->disk_name, brq.data.error);
314 goto cmd_err;
317 if (brq.stop.error) {
318 printk(KERN_ERR "%s: error %d sending stop command\n",
319 req->rq_disk->disk_name, brq.stop.error);
320 goto cmd_err;
323 if (rq_data_dir(req) != READ) {
324 do {
325 int err;
327 cmd.opcode = MMC_SEND_STATUS;
328 cmd.arg = card->rca << 16;
329 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
330 err = mmc_wait_for_cmd(card->host, &cmd, 5);
331 if (err) {
332 printk(KERN_ERR "%s: error %d requesting status\n",
333 req->rq_disk->disk_name, err);
334 goto cmd_err;
336 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
338 #if 0
339 if (cmd.resp[0] & ~0x00000900)
340 printk(KERN_ERR "%s: status = %08x\n",
341 req->rq_disk->disk_name, cmd.resp[0]);
342 if (mmc_decode_status(cmd.resp))
343 goto cmd_err;
344 #endif
348 * A block was successfully transferred.
350 spin_lock_irq(&md->lock);
351 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
352 if (!ret) {
354 * The whole request completed successfully.
356 add_disk_randomness(req->rq_disk);
357 blkdev_dequeue_request(req);
358 end_that_request_last(req, 1);
360 spin_unlock_irq(&md->lock);
361 } while (ret);
363 mmc_card_release_host(card);
365 return 1;
367 cmd_err:
368 ret = 1;
371 * If this is an SD card and we're writing, we can first
372 * mark the known good sectors as ok.
374 * If the card is not SD, we can still ok written sectors
375 * if the controller can do proper error reporting.
377 * For reads we just fail the entire chunk as that should
378 * be safe in all cases.
380 if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
381 u32 blocks;
382 unsigned int bytes;
384 blocks = mmc_sd_num_wr_blocks(card);
385 if (blocks != (u32)-1) {
386 if (card->csd.write_partial)
387 bytes = blocks << md->block_bits;
388 else
389 bytes = blocks << 9;
390 spin_lock_irq(&md->lock);
391 ret = end_that_request_chunk(req, 1, bytes);
392 spin_unlock_irq(&md->lock);
394 } else if (rq_data_dir(req) != READ &&
395 (card->host->caps & MMC_CAP_MULTIWRITE)) {
396 spin_lock_irq(&md->lock);
397 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
398 spin_unlock_irq(&md->lock);
401 mmc_card_release_host(card);
403 spin_lock_irq(&md->lock);
404 while (ret) {
405 ret = end_that_request_chunk(req, 0,
406 req->current_nr_sectors << 9);
409 add_disk_randomness(req->rq_disk);
410 blkdev_dequeue_request(req);
411 end_that_request_last(req, 0);
412 spin_unlock_irq(&md->lock);
414 return 0;
417 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
419 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
421 static inline int mmc_blk_readonly(struct mmc_card *card)
423 return mmc_card_readonly(card) ||
424 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
427 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
429 struct mmc_blk_data *md;
430 int devidx, ret;
432 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
433 if (devidx >= MMC_NUM_MINORS)
434 return ERR_PTR(-ENOSPC);
435 __set_bit(devidx, dev_use);
437 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
438 if (!md) {
439 ret = -ENOMEM;
440 goto out;
443 memset(md, 0, sizeof(struct mmc_blk_data));
446 * Set the read-only status based on the supported commands
447 * and the write protect switch.
449 md->read_only = mmc_blk_readonly(card);
452 * Both SD and MMC specifications state (although a bit
453 * unclearly in the MMC case) that a block size of 512
454 * bytes must always be supported by the card.
456 md->block_bits = 9;
458 md->disk = alloc_disk(1 << MMC_SHIFT);
459 if (md->disk == NULL) {
460 ret = -ENOMEM;
461 goto err_kfree;
464 spin_lock_init(&md->lock);
465 md->usage = 1;
467 ret = mmc_init_queue(&md->queue, card, &md->lock);
468 if (ret)
469 goto err_putdisk;
471 md->queue.prep_fn = mmc_blk_prep_rq;
472 md->queue.issue_fn = mmc_blk_issue_rq;
473 md->queue.data = md;
475 md->disk->major = major;
476 md->disk->first_minor = devidx << MMC_SHIFT;
477 md->disk->fops = &mmc_bdops;
478 md->disk->private_data = md;
479 md->disk->queue = md->queue.queue;
480 md->disk->driverfs_dev = &card->dev;
483 * As discussed on lkml, GENHD_FL_REMOVABLE should:
485 * - be set for removable media with permanent block devices
486 * - be unset for removable block devices with permanent media
488 * Since MMC block devices clearly fall under the second
489 * case, we do not set GENHD_FL_REMOVABLE. Userspace
490 * should use the block device creation/destruction hotplug
491 * messages to tell when the card is present.
494 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
496 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
499 * The CSD capacity field is in units of read_blkbits.
500 * set_capacity takes units of 512 bytes.
502 set_capacity(md->disk, card->csd.capacity << (card->csd.read_blkbits - 9));
503 return md;
505 err_putdisk:
506 put_disk(md->disk);
507 err_kfree:
508 kfree(md);
509 out:
510 return ERR_PTR(ret);
513 static int
514 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
516 struct mmc_command cmd;
517 int err;
519 mmc_card_claim_host(card);
520 cmd.opcode = MMC_SET_BLOCKLEN;
521 cmd.arg = 1 << md->block_bits;
522 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
523 err = mmc_wait_for_cmd(card->host, &cmd, 5);
524 mmc_card_release_host(card);
526 if (err) {
527 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
528 md->disk->disk_name, cmd.arg, err);
529 return -EINVAL;
532 return 0;
535 #if 1 // add by Victor Yu. 03-07-2007
536 #ifdef CONFIG_ARCH_UC7101
537 #define SD_LED (1<<30)
538 #endif
539 static void moxa_pnp(int status)
541 char *argv[3] , *envp[4] ;
543 envp[0] = "HOME=/" ;
544 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin" ;
545 envp[2] = "TERM=console" ;
546 envp[3] = NULL ;
548 argv[0] = "pnp" ;
549 argv[2] = NULL ;
551 if(status) {
552 argv[1] = "mount" ;
553 if(call_usermodehelper("/bin/pnp",argv,envp,0)==-1)
554 printk("execute mount failure\n") ;
555 #ifdef CONFIG_ARCH_UC7101
556 else
557 mcpu_gpio_set(SD_LED, MCPU_GPIO_LOW);
558 #endif
559 } else {
560 argv[1] = "umount" ;
561 if(call_usermodehelper("/bin/pnp",argv,envp,0)==-1)
562 printk("execute umount failure\n") ;
563 #ifdef CONFIG_ARCH_UC7101
564 else
565 mcpu_gpio_set(SD_LED, MCPU_GPIO_HIGH);
566 #endif
569 #endif
571 static int mmc_blk_probe(struct mmc_card *card)
573 struct mmc_blk_data *md;
574 int err;
577 * Check that the card supports the command class(es) we need.
579 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
580 return -ENODEV;
582 md = mmc_blk_alloc(card);
583 if (IS_ERR(md))
584 return PTR_ERR(md);
586 err = mmc_blk_set_blksize(md, card);
587 if (err)
588 goto out;
590 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
591 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
592 (unsigned long long)(get_capacity(md->disk) >> 1),
593 md->read_only ? "(ro)" : "");
595 mmc_set_drvdata(card, md);
596 add_disk(md->disk);
597 #if 1 // add by Victor Yu. 03-07-2007
598 moxa_pnp(1);
599 #endif
600 return 0;
602 out:
603 mmc_blk_put(md);
605 return err;
608 static void mmc_blk_remove(struct mmc_card *card)
610 struct mmc_blk_data *md = mmc_get_drvdata(card);
612 if (md) {
613 int devidx;
615 del_gendisk(md->disk);
618 * I think this is needed.
620 md->disk->queue = NULL;
622 devidx = md->disk->first_minor >> MMC_SHIFT;
623 __clear_bit(devidx, dev_use);
625 mmc_blk_put(md);
627 mmc_set_drvdata(card, NULL);
628 #if 1 // add by Victor Yu. 03-07-2007
629 moxa_pnp(0);
630 #endif
633 #ifdef CONFIG_PM
634 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
636 struct mmc_blk_data *md = mmc_get_drvdata(card);
638 if (md) {
639 mmc_queue_suspend(&md->queue);
641 return 0;
644 static int mmc_blk_resume(struct mmc_card *card)
646 struct mmc_blk_data *md = mmc_get_drvdata(card);
648 if (md) {
649 mmc_blk_set_blksize(md, card);
650 mmc_queue_resume(&md->queue);
652 return 0;
654 #else
655 #define mmc_blk_suspend NULL
656 #define mmc_blk_resume NULL
657 #endif
659 static struct mmc_driver mmc_driver = {
660 .drv = {
661 .name = "mmcblk",
663 .probe = mmc_blk_probe,
664 .remove = mmc_blk_remove,
665 .suspend = mmc_blk_suspend,
666 .resume = mmc_blk_resume,
669 static int __init mmc_blk_init(void)
671 int res = -ENOMEM;
673 res = register_blkdev(major, "mmc");
674 if (res < 0) {
675 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
676 major, res);
677 goto out;
679 if (major == 0)
680 major = res;
682 return mmc_register_driver(&mmc_driver);
684 out:
685 return res;
688 static void __exit mmc_blk_exit(void)
690 mmc_unregister_driver(&mmc_driver);
691 unregister_blkdev(major, "mmc");
694 module_init(mmc_blk_init);
695 module_exit(mmc_blk_exit);
697 MODULE_LICENSE("GPL");
698 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
700 module_param(major, int, 0444);
701 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");