OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [drivers/] [mmc/] [card/] [block.c] - Blame information for rev 62

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 62 marcus.erl
/*
2
 * Block driver for media (i.e., flash cards)
3
 *
4
 * Copyright 2002 Hewlett-Packard Company
5
 * Copyright 2005-2007 Pierre Ossman
6
 *
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.
10
 *
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.
14
 *
15
 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16
 *
17
 * Author:  Andrew Christian
18
 *          28 May 2002
19
 */
20
#include <linux/moduleparam.h>
21
#include <linux/module.h>
22
#include <linux/init.h>
23
 
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>
32
 
33
#include <linux/mmc/card.h>
34
#include <linux/mmc/host.h>
35
#include <linux/mmc/mmc.h>
36
#include <linux/mmc/sd.h>
37
 
38
#include <asm/system.h>
39
#include <asm/uaccess.h>
40
 
41
#include "queue.h"
42
 
43
/*
44
 * max 8 partitions per card
45
 */
46
#define MMC_SHIFT       3
47
#define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
48
 
49
static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
50
 
51
/*
52
 * There is one mmc_blk_data per slot.
53
 */
54
struct mmc_blk_data {
55
        spinlock_t      lock;
56
        struct gendisk  *disk;
57
        struct mmc_queue queue;
58
 
59
        unsigned int    usage;
60
        unsigned int    block_bits;
61
        unsigned int    read_only;
62
};
63
 
64
static DEFINE_MUTEX(open_lock);
65
 
66
static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
67
{
68
        struct mmc_blk_data *md;
69
 
70
        mutex_lock(&open_lock);
71
        md = disk->private_data;
72
        if (md && md->usage == 0)
73
                md = NULL;
74
        if (md)
75
                md->usage++;
76
        mutex_unlock(&open_lock);
77
 
78
        return md;
79
}
80
 
81
static void mmc_blk_put(struct mmc_blk_data *md)
82
{
83
        mutex_lock(&open_lock);
84
        md->usage--;
85
        if (md->usage == 0) {
86
                int devidx = md->disk->first_minor >> MMC_SHIFT;
87
                __clear_bit(devidx, dev_use);
88
 
89
                put_disk(md->disk);
90
                kfree(md);
91
        }
92
        mutex_unlock(&open_lock);
93
}
94
 
95
static int mmc_blk_open(struct inode *inode, struct file *filp)
96
{
97
        struct mmc_blk_data *md;
98
        int ret = -ENXIO;
99
 
100
        md = mmc_blk_get(inode->i_bdev->bd_disk);
101
        if (md) {
102
                if (md->usage == 2)
103
                        check_disk_change(inode->i_bdev);
104
                ret = 0;
105
 
106
                if ((filp->f_mode & FMODE_WRITE) && md->read_only)
107
                        ret = -EROFS;
108
        }
109
 
110
        return ret;
111
}
112
 
113
static int mmc_blk_release(struct inode *inode, struct file *filp)
114
{
115
        struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
116
 
117
        mmc_blk_put(md);
118
        return 0;
119
}
120
 
121
static int
122
mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
123
{
124
        geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
125
        geo->heads = 4;
126
        geo->sectors = 16;
127
        return 0;
128
}
129
 
130
static struct block_device_operations mmc_bdops = {
131
        .open                   = mmc_blk_open,
132
        .release                = mmc_blk_release,
133
        .getgeo                 = mmc_blk_getgeo,
134
        .owner                  = THIS_MODULE,
135
};
136
 
137
struct mmc_blk_request {
138
        struct mmc_request      mrq;
139
        struct mmc_command      cmd;
140
        struct mmc_command      stop;
141
        struct mmc_data         data;
142
};
143
 
144
static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
145
{
146
        int err;
147
        u32 blocks;
148
 
149
        struct mmc_request mrq;
150
        struct mmc_command cmd;
151
        struct mmc_data data;
152
        unsigned int timeout_us;
153
 
154
        struct scatterlist sg;
155
 
156
        memset(&cmd, 0, sizeof(struct mmc_command));
157
 
158
        cmd.opcode = MMC_APP_CMD;
159
        cmd.arg = card->rca << 16;
160
        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
161
 
162
        err = mmc_wait_for_cmd(card->host, &cmd, 0);
163
        if (err)
164
                return (u32)-1;
165
        if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
166
                return (u32)-1;
167
 
168
        memset(&cmd, 0, sizeof(struct mmc_command));
169
 
170
        cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
171
        cmd.arg = 0;
172
        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
173
 
174
        memset(&data, 0, sizeof(struct mmc_data));
175
 
176
        data.timeout_ns = card->csd.tacc_ns * 100;
177
        data.timeout_clks = card->csd.tacc_clks * 100;
178
 
179
        timeout_us = data.timeout_ns / 1000;
180
        timeout_us += data.timeout_clks * 1000 /
181
                (card->host->ios.clock / 1000);
182
 
183
        if (timeout_us > 100000) {
184
                data.timeout_ns = 100000000;
185
                data.timeout_clks = 0;
186
        }
187
 
188
        data.blksz = 4;
189
        data.blocks = 1;
190
        data.flags = MMC_DATA_READ;
191
        data.sg = &sg;
192
        data.sg_len = 1;
193
 
194
        memset(&mrq, 0, sizeof(struct mmc_request));
195
 
196
        mrq.cmd = &cmd;
197
        mrq.data = &data;
198
 
199
        sg_init_one(&sg, &blocks, 4);
200
 
201
        mmc_wait_for_req(card->host, &mrq);
202
 
203
        if (cmd.error || data.error)
204
                return (u32)-1;
205
 
206
        blocks = ntohl(blocks);
207
 
208
        return blocks;
209
}
210
 
211
static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
212
{
213
        struct mmc_blk_data *md = mq->data;
214
        struct mmc_card *card = md->queue.card;
215
        struct mmc_blk_request brq;
216
        int ret = 1, sg_pos, data_size;
217
 
218
        mmc_claim_host(card->host);
219
 
220
        do {
221
                struct mmc_command cmd;
222
                u32 readcmd, writecmd;
223
 
224
                memset(&brq, 0, sizeof(struct mmc_blk_request));
225
                brq.mrq.cmd = &brq.cmd;
226
                brq.mrq.data = &brq.data;
227
 
228
                brq.cmd.arg = req->sector;
229
                if (!mmc_card_blockaddr(card))
230
                        brq.cmd.arg <<= 9;
231
                brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
232
                brq.data.blksz = 1 << md->block_bits;
233
                brq.stop.opcode = MMC_STOP_TRANSMISSION;
234
                brq.stop.arg = 0;
235
                brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
236
                brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
237
                if (brq.data.blocks > card->host->max_blk_count)
238
                        brq.data.blocks = card->host->max_blk_count;
239
 
240
                /*
241
                 * If the host doesn't support multiple block writes, force
242
                 * block writes to single block. SD cards are excepted from
243
                 * this rule as they support querying the number of
244
                 * successfully written sectors.
245
                 */
246
                if (rq_data_dir(req) != READ &&
247
                    !(card->host->caps & MMC_CAP_MULTIWRITE) &&
248
                    !mmc_card_sd(card))
249
                        brq.data.blocks = 1;
250
 
251
                if (brq.data.blocks > 1) {
252
                        /* SPI multiblock writes terminate using a special
253
                         * token, not a STOP_TRANSMISSION request.
254
                         */
255
                        if (!mmc_host_is_spi(card->host)
256
                                        || rq_data_dir(req) == READ)
257
                                brq.mrq.stop = &brq.stop;
258
                        readcmd = MMC_READ_MULTIPLE_BLOCK;
259
                        writecmd = MMC_WRITE_MULTIPLE_BLOCK;
260
                } else {
261
                        brq.mrq.stop = NULL;
262
                        readcmd = MMC_READ_SINGLE_BLOCK;
263
                        writecmd = MMC_WRITE_BLOCK;
264
                }
265
 
266
                if (rq_data_dir(req) == READ) {
267
                        brq.cmd.opcode = readcmd;
268
                        brq.data.flags |= MMC_DATA_READ;
269
                } else {
270
                        brq.cmd.opcode = writecmd;
271
                        brq.data.flags |= MMC_DATA_WRITE;
272
                }
273
 
274
                mmc_set_data_timeout(&brq.data, card);
275
 
276
                brq.data.sg = mq->sg;
277
                brq.data.sg_len = mmc_queue_map_sg(mq);
278
 
279
                mmc_queue_bounce_pre(mq);
280
 
281
                if (brq.data.blocks !=
282
                    (req->nr_sectors >> (md->block_bits - 9))) {
283
                        data_size = brq.data.blocks * brq.data.blksz;
284
                        for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
285
                                data_size -= mq->sg[sg_pos].length;
286
                                if (data_size <= 0) {
287
                                        mq->sg[sg_pos].length += data_size;
288
                                        sg_pos++;
289
                                        break;
290
                                }
291
                        }
292
                        brq.data.sg_len = sg_pos;
293
                }
294
 
295
                mmc_wait_for_req(card->host, &brq.mrq);
296
 
297
                mmc_queue_bounce_post(mq);
298
 
299
                if (brq.cmd.error) {
300
                        printk(KERN_ERR "%s: error %d sending read/write command\n",
301
                               req->rq_disk->disk_name, brq.cmd.error);
302
                        goto cmd_err;
303
                }
304
 
305
                if (brq.data.error) {
306
                        printk(KERN_ERR "%s: error %d transferring data\n",
307
                               req->rq_disk->disk_name, brq.data.error);
308
                        goto cmd_err;
309
                }
310
 
311
                if (brq.stop.error) {
312
                        printk(KERN_ERR "%s: error %d sending stop command\n",
313
                               req->rq_disk->disk_name, brq.stop.error);
314
                        goto cmd_err;
315
                }
316
 
317
                if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
318
                        do {
319
                                int err;
320
 
321
                                cmd.opcode = MMC_SEND_STATUS;
322
                                cmd.arg = card->rca << 16;
323
                                cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
324
                                err = mmc_wait_for_cmd(card->host, &cmd, 5);
325
                                if (err) {
326
                                        printk(KERN_ERR "%s: error %d requesting status\n",
327
                                               req->rq_disk->disk_name, err);
328
                                        goto cmd_err;
329
                                }
330
                                /*
331
                                 * Some cards mishandle the status bits,
332
                                 * so make sure to check both the busy
333
                                 * indication and the card state.
334
                                 */
335
                        } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
336
                                (R1_CURRENT_STATE(cmd.resp[0]) == 7));
337
 
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
345
                }
346
 
347
                /*
348
                 * A block was successfully transferred.
349
                 */
350
                spin_lock_irq(&md->lock);
351
                ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
352
                if (!ret) {
353
                        /*
354
                         * The whole request completed successfully.
355
                         */
356
                        add_disk_randomness(req->rq_disk);
357
                        blkdev_dequeue_request(req);
358
                        end_that_request_last(req, 1);
359
                }
360
                spin_unlock_irq(&md->lock);
361
        } while (ret);
362
 
363
        mmc_release_host(card->host);
364
 
365
        return 1;
366
 
367
 cmd_err:
368
        /*
369
         * If this is an SD card and we're writing, we can first
370
         * mark the known good sectors as ok.
371
         *
372
         * If the card is not SD, we can still ok written sectors
373
         * if the controller can do proper error reporting.
374
         *
375
         * For reads we just fail the entire chunk as that should
376
         * be safe in all cases.
377
         */
378
        if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
379
                u32 blocks;
380
                unsigned int bytes;
381
 
382
                blocks = mmc_sd_num_wr_blocks(card);
383
                if (blocks != (u32)-1) {
384
                        if (card->csd.write_partial)
385
                                bytes = blocks << md->block_bits;
386
                        else
387
                                bytes = blocks << 9;
388
                        spin_lock_irq(&md->lock);
389
                        ret = end_that_request_chunk(req, 1, bytes);
390
                        spin_unlock_irq(&md->lock);
391
                }
392
        } else if (rq_data_dir(req) != READ &&
393
                   (card->host->caps & MMC_CAP_MULTIWRITE)) {
394
                spin_lock_irq(&md->lock);
395
                ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
396
                spin_unlock_irq(&md->lock);
397
        }
398
 
399
        mmc_release_host(card->host);
400
 
401
        spin_lock_irq(&md->lock);
402
        while (ret) {
403
                ret = end_that_request_chunk(req, 0,
404
                                req->current_nr_sectors << 9);
405
        }
406
 
407
        add_disk_randomness(req->rq_disk);
408
        blkdev_dequeue_request(req);
409
        end_that_request_last(req, 0);
410
        spin_unlock_irq(&md->lock);
411
 
412
        return 0;
413
}
414
 
415
 
416
static inline int mmc_blk_readonly(struct mmc_card *card)
417
{
418
        return mmc_card_readonly(card) ||
419
               !(card->csd.cmdclass & CCC_BLOCK_WRITE);
420
}
421
 
422
static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
423
{
424
        struct mmc_blk_data *md;
425
        int devidx, ret;
426
 
427
        devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
428
        if (devidx >= MMC_NUM_MINORS)
429
                return ERR_PTR(-ENOSPC);
430
        __set_bit(devidx, dev_use);
431
 
432
        md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
433
        if (!md) {
434
                ret = -ENOMEM;
435
                goto out;
436
        }
437
 
438
 
439
        /*
440
         * Set the read-only status based on the supported commands
441
         * and the write protect switch.
442
         */
443
        md->read_only = mmc_blk_readonly(card);
444
 
445
        /*
446
         * Both SD and MMC specifications state (although a bit
447
         * unclearly in the MMC case) that a block size of 512
448
         * bytes must always be supported by the card.
449
         */
450
        md->block_bits = 9;
451
 
452
        md->disk = alloc_disk(1 << MMC_SHIFT);
453
        if (md->disk == NULL) {
454
                ret = -ENOMEM;
455
                goto err_kfree;
456
        }
457
 
458
        spin_lock_init(&md->lock);
459
        md->usage = 1;
460
 
461
        ret = mmc_init_queue(&md->queue, card, &md->lock);
462
        if (ret)
463
                goto err_putdisk;
464
 
465
        md->queue.issue_fn = mmc_blk_issue_rq;
466
        md->queue.data = md;
467
 
468
        md->disk->major = MMC_BLOCK_MAJOR;
469
        md->disk->first_minor = devidx << MMC_SHIFT;
470
        md->disk->fops = &mmc_bdops;
471
        md->disk->private_data = md;
472
        md->disk->queue = md->queue.queue;
473
        md->disk->driverfs_dev = &card->dev;
474
 
475
        /*
476
         * As discussed on lkml, GENHD_FL_REMOVABLE should:
477
         *
478
         * - be set for removable media with permanent block devices
479
         * - be unset for removable block devices with permanent media
480
         *
481
         * Since MMC block devices clearly fall under the second
482
         * case, we do not set GENHD_FL_REMOVABLE.  Userspace
483
         * should use the block device creation/destruction hotplug
484
         * messages to tell when the card is present.
485
         */
486
 
487
        sprintf(md->disk->disk_name, "mmcblk%d", devidx);
488
 
489
        blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
490
 
491
        if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
492
                /*
493
                 * The EXT_CSD sector count is in number or 512 byte
494
                 * sectors.
495
                 */
496
                set_capacity(md->disk, card->ext_csd.sectors);
497
        } else {
498
                /*
499
                 * The CSD capacity field is in units of read_blkbits.
500
                 * set_capacity takes units of 512 bytes.
501
                 */
502
                set_capacity(md->disk,
503
                        card->csd.capacity << (card->csd.read_blkbits - 9));
504
        }
505
        return md;
506
 
507
 err_putdisk:
508
        put_disk(md->disk);
509
 err_kfree:
510
        kfree(md);
511
 out:
512
        return ERR_PTR(ret);
513
}
514
 
515
static int
516
mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
517
{
518
        struct mmc_command cmd;
519
        int err;
520
 
521
        /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
522
        if (mmc_card_blockaddr(card))
523
                return 0;
524
 
525
        mmc_claim_host(card->host);
526
        cmd.opcode = MMC_SET_BLOCKLEN;
527
        cmd.arg = 1 << md->block_bits;
528
        cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
529
        err = mmc_wait_for_cmd(card->host, &cmd, 5);
530
        mmc_release_host(card->host);
531
 
532
        if (err) {
533
                printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
534
                        md->disk->disk_name, cmd.arg, err);
535
                return -EINVAL;
536
        }
537
 
538
        return 0;
539
}
540
 
541
static int mmc_blk_probe(struct mmc_card *card)
542
{
543
        struct mmc_blk_data *md;
544
        int err;
545
 
546
        /*
547
         * Check that the card supports the command class(es) we need.
548
         */
549
        if (!(card->csd.cmdclass & CCC_BLOCK_READ))
550
                return -ENODEV;
551
 
552
        md = mmc_blk_alloc(card);
553
        if (IS_ERR(md))
554
                return PTR_ERR(md);
555
 
556
        err = mmc_blk_set_blksize(md, card);
557
        if (err)
558
                goto out;
559
 
560
        printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
561
                md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
562
                (unsigned long long)(get_capacity(md->disk) >> 1),
563
                md->read_only ? "(ro)" : "");
564
 
565
        mmc_set_drvdata(card, md);
566
        add_disk(md->disk);
567
        return 0;
568
 
569
 out:
570
        mmc_blk_put(md);
571
 
572
        return err;
573
}
574
 
575
static void mmc_blk_remove(struct mmc_card *card)
576
{
577
        struct mmc_blk_data *md = mmc_get_drvdata(card);
578
 
579
        if (md) {
580
                /* Stop new requests from getting into the queue */
581
                del_gendisk(md->disk);
582
 
583
                /* Then flush out any already in there */
584
                mmc_cleanup_queue(&md->queue);
585
 
586
                mmc_blk_put(md);
587
        }
588
        mmc_set_drvdata(card, NULL);
589
}
590
 
591
#ifdef CONFIG_PM
592
static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
593
{
594
        struct mmc_blk_data *md = mmc_get_drvdata(card);
595
 
596
        if (md) {
597
                mmc_queue_suspend(&md->queue);
598
        }
599
        return 0;
600
}
601
 
602
static int mmc_blk_resume(struct mmc_card *card)
603
{
604
        struct mmc_blk_data *md = mmc_get_drvdata(card);
605
 
606
        if (md) {
607
                mmc_blk_set_blksize(md, card);
608
                mmc_queue_resume(&md->queue);
609
        }
610
        return 0;
611
}
612
#else
613
#define mmc_blk_suspend NULL
614
#define mmc_blk_resume  NULL
615
#endif
616
 
617
static struct mmc_driver mmc_driver = {
618
        .drv            = {
619
                .name   = "mmcblk",
620
        },
621
        .probe          = mmc_blk_probe,
622
        .remove         = mmc_blk_remove,
623
        .suspend        = mmc_blk_suspend,
624
        .resume         = mmc_blk_resume,
625
};
626
 
627
static int __init mmc_blk_init(void)
628
{
629
        int res = -ENOMEM;
630
 
631
        res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
632
        if (res)
633
                goto out;
634
 
635
        return mmc_register_driver(&mmc_driver);
636
 
637
 out:
638
        return res;
639
}
640
 
641
static void __exit mmc_blk_exit(void)
642
{
643
        mmc_unregister_driver(&mmc_driver);
644
        unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
645
}
646
 
647
module_init(mmc_blk_init);
648
module_exit(mmc_blk_exit);
649
 
650
MODULE_LICENSE("GPL");
651
MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
652
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.