block_dev.c 13.7 KB
Newer Older
1 2 3 4 5
/*
 * File      : block_dev.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20
 *
 * Change Logs:
Y
yiyue.fang 已提交
21 22
 * Date           Author        Notes
 * 2011-07-25     weety     first version
23 24 25 26 27
 */

#include <rtthread.h>
#include <dfs_fs.h>

28
#include <drivers/mmcsd_core.h>
29

30 31 32 33 34 35 36 37 38 39
#define DBG_ENABLE
#define DBG_SECTION_NAME               "[SDIO]"
#ifdef RT_SDIO_DEBUG
#define DBG_LEVEL                      DBG_LOG
#else
#define DBG_LEVEL                      DBG_INFO
#endif /* RT_SDIO_DEBUG */
#define DBG_COLOR
#include <rtdbg.h>

40
static rt_list_t blk_devices = RT_LIST_OBJECT_INIT(blk_devices);
41

42 43
struct mmcsd_blk_device
{
Y
yiyue.fang 已提交
44 45 46 47 48
    struct rt_mmcsd_card *card;
    rt_list_t list;
    struct rt_device dev;
    struct dfs_partition part;
    struct rt_device_blk_geometry geometry;
49 50 51 52 53 54
};

#ifndef RT_MMCSD_MAX_PARTITION
#define RT_MMCSD_MAX_PARTITION 16
#endif

55
rt_int32_t mmcsd_num_wr_blocks(struct rt_mmcsd_card *card)
56
{
Y
yiyue.fang 已提交
57 58
    rt_int32_t err;
    rt_uint32_t blocks;
59

Y
yiyue.fang 已提交
60 61 62 63
    struct rt_mmcsd_req req;
    struct rt_mmcsd_cmd cmd;
    struct rt_mmcsd_data data;
    rt_uint32_t timeout_us;
64

Y
yiyue.fang 已提交
65
    rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
66

Y
yiyue.fang 已提交
67 68 69
    cmd.cmd_code = APP_CMD;
    cmd.arg = card->rca << 16;
    cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
70

Y
yiyue.fang 已提交
71 72 73 74 75
    err = mmcsd_send_cmd(card->host, &cmd, 0);
    if (err)
        return -RT_ERROR;
    if (!controller_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
        return -RT_ERROR;
76

Y
yiyue.fang 已提交
77
    rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
78

Y
yiyue.fang 已提交
79 80 81
    cmd.cmd_code = SD_APP_SEND_NUM_WR_BLKS;
    cmd.arg = 0;
    cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
82

Y
yiyue.fang 已提交
83
    rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
84

Y
yiyue.fang 已提交
85 86
    data.timeout_ns = card->tacc_ns * 100;
    data.timeout_clks = card->tacc_clks * 100;
87

Y
yiyue.fang 已提交
88 89 90
    timeout_us = data.timeout_ns / 1000;
    timeout_us += data.timeout_clks * 1000 /
        (card->host->io_cfg.clock / 1000);
91

Y
yiyue.fang 已提交
92 93 94 95 96
    if (timeout_us > 100000) 
    {
        data.timeout_ns = 100000000;
        data.timeout_clks = 0;
    }
97

Y
yiyue.fang 已提交
98 99 100 101
    data.blksize = 4;
    data.blks = 1;
    data.flags = DATA_DIR_READ;
    data.buf = &blocks;
102

Y
yiyue.fang 已提交
103
    rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
104

Y
yiyue.fang 已提交
105 106
    req.cmd = &cmd;
    req.data = &data;
107

Y
yiyue.fang 已提交
108
    mmcsd_send_request(card->host, &req);
109

Y
yiyue.fang 已提交
110 111
    if (cmd.err || data.err)
        return -RT_ERROR;
112

Y
yiyue.fang 已提交
113
    return blocks;
114 115
}

Y
yiyue.fang 已提交
116 117 118 119 120
static rt_err_t rt_mmcsd_req_blk(struct rt_mmcsd_card *card,
                                 rt_uint32_t           sector,
                                 void                 *buf,
                                 rt_size_t             blks,
                                 rt_uint8_t            dir)
121
{
Y
yiyue.fang 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    struct rt_mmcsd_cmd  cmd, stop;
    struct rt_mmcsd_data  data;
    struct rt_mmcsd_req  req;
    struct rt_mmcsd_host *host = card->host;
    rt_uint32_t r_cmd, w_cmd;

    mmcsd_host_lock(host);
    rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
    rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
    rt_memset(&stop, 0, sizeof(struct rt_mmcsd_cmd));
    rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
    req.cmd = &cmd;
    req.data = &data;
    
    cmd.arg = sector;
    if (!(card->flags & CARD_FLAG_SDHC)) 
    {
        cmd.arg <<= 9;
    }
    cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;

    data.blksize = SECTOR_SIZE;
    data.blks  = blks;

    if (blks > 1) 
    {
        if (!controller_is_spi(card->host) || !dir)
        {
            req.stop = &stop;
            stop.cmd_code = STOP_TRANSMISSION;
            stop.arg = 0;
            stop.flags = RESP_SPI_R1B | RESP_R1B | CMD_AC;
        }
        r_cmd = READ_MULTIPLE_BLOCK;
        w_cmd = WRITE_MULTIPLE_BLOCK;
    }
    else
    {
W
weety 已提交
160
        req.stop = RT_NULL;
Y
yiyue.fang 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        r_cmd = READ_SINGLE_BLOCK;
        w_cmd = WRITE_BLOCK;
    }

    if (!dir) 
    {
        cmd.cmd_code = r_cmd;
        data.flags |= DATA_DIR_READ;
    }
    else
    {
        cmd.cmd_code = w_cmd;
        data.flags |= DATA_DIR_WRITE;
    }

    mmcsd_set_data_timeout(&data, card);
    data.buf = buf;
    mmcsd_send_request(host, &req);

    if (!controller_is_spi(card->host) && dir != 0) 
    {
        do 
        {
            rt_int32_t err;

            cmd.cmd_code = SEND_STATUS;
            cmd.arg = card->rca << 16;
            cmd.flags = RESP_R1 | CMD_AC;
            err = mmcsd_send_cmd(card->host, &cmd, 5);
            if (err) 
            {
192
                LOG_E("error %d requesting status", err);
Y
yiyue.fang 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
                break;
            }
            /*
             * Some cards mishandle the status bits,
             * so make sure to check both the busy
             * indication and the card state.
             */
         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
            (R1_CURRENT_STATE(cmd.resp[0]) == 7));
    }

    mmcsd_host_unlock(host);

    if (cmd.err || data.err || stop.err) 
    {
208 209
        LOG_E("mmcsd request blocks error");
        LOG_E("%d,%d,%d, 0x%08x,0x%08x",
Y
yiyue.fang 已提交
210 211 212 213 214 215
                   cmd.err, data.err, stop.err, data.flags, sector);

        return -RT_ERROR;
    }

    return RT_EOK;
216 217 218 219
}

static rt_err_t rt_mmcsd_init(rt_device_t dev)
{
Y
yiyue.fang 已提交
220
    return RT_EOK;
221 222 223 224
}

static rt_err_t rt_mmcsd_open(rt_device_t dev, rt_uint16_t oflag)
{
Y
yiyue.fang 已提交
225
    return RT_EOK;
226 227 228 229
}

static rt_err_t rt_mmcsd_close(rt_device_t dev)
{
Y
yiyue.fang 已提交
230
    return RT_EOK;
231 232
}

B
bernard 已提交
233
static rt_err_t rt_mmcsd_control(rt_device_t dev, int cmd, void *args)
234
{
Y
yiyue.fang 已提交
235 236 237 238 239 240 241 242 243 244
    struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
    switch (cmd)
    {
    case RT_DEVICE_CTRL_BLK_GETGEOME:
        rt_memcpy(args, &blk_dev->geometry, sizeof(struct rt_device_blk_geometry));
        break;
    default:
        break;
    }
    return RT_EOK;
245 246
}

Y
yiyue.fang 已提交
247 248 249 250
static rt_size_t rt_mmcsd_read(rt_device_t dev,
                               rt_off_t    pos,
                               void       *buffer,
                               rt_size_t   size)
251
{
Y
yiyue.fang 已提交
252 253 254 255 256 257
    rt_err_t err;
    struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
    struct dfs_partition *part = &blk_dev->part;

    if (dev == RT_NULL)
    {
B
bernard 已提交
258
        rt_set_errno(-EINVAL);
Y
yiyue.fang 已提交
259 260 261 262 263 264 265 266 267 268 269

        return 0;
    }

    rt_sem_take(part->lock, RT_WAITING_FOREVER);
    err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos, buffer, size, 0);
    rt_sem_release(part->lock);

    /* the length of reading must align to SECTOR SIZE */
    if (err) 
    {
B
bernard 已提交
270
        rt_set_errno(-EIO);
Y
yiyue.fang 已提交
271 272 273
        return 0;
    }
    return size;
274 275
}

Y
yiyue.fang 已提交
276 277 278 279
static rt_size_t rt_mmcsd_write(rt_device_t dev,
                                rt_off_t    pos,
                                const void *buffer,
                                rt_size_t   size)
280
{
Y
yiyue.fang 已提交
281 282 283 284 285 286
    rt_err_t err;
    struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
    struct dfs_partition *part = &blk_dev->part;

    if (dev == RT_NULL)
    {
B
bernard 已提交
287
        rt_set_errno(-EINVAL);
Y
yiyue.fang 已提交
288 289 290 291 292 293 294 295 296 297 298

        return 0;
    }

    rt_sem_take(part->lock, RT_WAITING_FOREVER);
    err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos, (void *)buffer, size, 1);
    rt_sem_release(part->lock);

    /* the length of reading must align to SECTOR SIZE */
    if (err) 
    {
B
bernard 已提交
299
        rt_set_errno(-EIO);
Y
yiyue.fang 已提交
300 301 302 303

        return 0;
    }
    return size;
304 305 306 307
}

static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card)
{
Y
yiyue.fang 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    struct rt_mmcsd_cmd cmd;
    int err;

    /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
    if (card->flags & CARD_FLAG_SDHC)
        return 0;

    mmcsd_host_lock(card->host);
    cmd.cmd_code = SET_BLOCKLEN;
    cmd.arg = 512;
    cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
    err = mmcsd_send_cmd(card->host, &cmd, 5);
    mmcsd_host_unlock(card->host);

    if (err) 
    {
324
        LOG_E("MMCSD: unable to set block size to %d: %d", cmd.arg, err);
Y
yiyue.fang 已提交
325 326 327 328 329

        return -RT_ERROR;
    }

    return 0;
330 331
}

B
Bernard Xiong 已提交
332 333 334 335 336 337 338 339 340 341 342 343
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops mmcsd_blk_ops = 
{
    rt_mmcsd_init,
    rt_mmcsd_open,
    rt_mmcsd_close,
    rt_mmcsd_read,
    rt_mmcsd_write,
    rt_mmcsd_control
};
#endif

344 345
rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
{
Y
yiyue.fang 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358
    rt_int32_t err = 0;
    rt_uint8_t i, status;
    rt_uint8_t *sector;
    char dname[4];
    char sname[8];
    struct mmcsd_blk_device *blk_dev = RT_NULL;

    err = mmcsd_set_blksize(card);
    if(err) 
    {
        return err;
    }

359
    LOG_I("probe mmcsd block device!");
360

Y
yiyue.fang 已提交
361 362 363 364
    /* get the first sector to read partition table */
    sector = (rt_uint8_t *)rt_malloc(SECTOR_SIZE);
    if (sector == RT_NULL)
    {
365
        LOG_E("allocate partition sector buffer failed!");
Y
yiyue.fang 已提交
366 367 368 369 370 371 372 373 374

        return -RT_ENOMEM;
    }

    status = rt_mmcsd_req_blk(card, 0, sector, 1, 0);
    if (status == RT_EOK)
    {
        for (i = 0; i < RT_MMCSD_MAX_PARTITION; i++)
        {
375
            blk_dev = rt_calloc(1, sizeof(struct mmcsd_blk_device));
Y
yiyue.fang 已提交
376 377
            if (!blk_dev) 
            {
378
                LOG_E("mmcsd:malloc memory failed!");
Y
yiyue.fang 已提交
379 380
                break;
            }
381

Y
yiyue.fang 已提交
382 383 384 385 386 387 388 389 390
            /* get the first partition */
            status = dfs_filesystem_get_partition(&blk_dev->part, sector, i);
            if (status == RT_EOK)
            {
                rt_snprintf(dname, 4, "sd%d",  i);
                rt_snprintf(sname, 8, "sem_sd%d",  i);
                blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
    
                /* register mmcsd device */
B
Bernard Xiong 已提交
391 392 393 394
                blk_dev->dev.type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
                blk_dev->dev.ops  = &mmcsd_blk_ops;
#else
Y
yiyue.fang 已提交
395 396 397 398 399 400
                blk_dev->dev.init = rt_mmcsd_init;
                blk_dev->dev.open = rt_mmcsd_open;
                blk_dev->dev.close = rt_mmcsd_close;
                blk_dev->dev.read = rt_mmcsd_read;
                blk_dev->dev.write = rt_mmcsd_write;
                blk_dev->dev.control = rt_mmcsd_control;
B
Bernard Xiong 已提交
401
#endif
Y
yiyue.fang 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
                blk_dev->dev.user_data = blk_dev;

                blk_dev->card = card;
                
                blk_dev->geometry.bytes_per_sector = 1<<9;
                blk_dev->geometry.block_size = card->card_blksize;
                blk_dev->geometry.sector_count = blk_dev->part.size;
    
                rt_device_register(&blk_dev->dev, dname,
                    RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
                rt_list_insert_after(&blk_devices, &blk_dev->list);
            }
            else
            {
                if (i == 0)
                {
                    /* there is no partition table */
                    blk_dev->part.offset = 0;
                    blk_dev->part.size   = 0;
                    blk_dev->part.lock = rt_sem_create("sem_sd0", 1, RT_IPC_FLAG_FIFO);
    
                    /* register mmcsd device */
                    blk_dev->dev.type  = RT_Device_Class_Block;
B
Bernard Xiong 已提交
425 426 427
#ifdef RT_USING_DEVICE_OPS
                    blk_dev->dev.ops  = &mmcsd_blk_ops;
#else
Y
yiyue.fang 已提交
428 429 430 431 432 433
                    blk_dev->dev.init = rt_mmcsd_init;
                    blk_dev->dev.open = rt_mmcsd_open;
                    blk_dev->dev.close = rt_mmcsd_close;
                    blk_dev->dev.read = rt_mmcsd_read;
                    blk_dev->dev.write = rt_mmcsd_write;
                    blk_dev->dev.control = rt_mmcsd_control;
B
Bernard Xiong 已提交
434
#endif
Y
yiyue.fang 已提交
435 436 437 438 439 440
                    blk_dev->dev.user_data = blk_dev;

                    blk_dev->card = card;

                    blk_dev->geometry.bytes_per_sector = 1<<9;
                    blk_dev->geometry.block_size = card->card_blksize;
W
weety 已提交
441 442
                    blk_dev->geometry.sector_count = 
                        card->card_capacity * (1024 / 512);
Y
yiyue.fang 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456
    
                    rt_device_register(&blk_dev->dev, "sd0",
                        RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
                    rt_list_insert_after(&blk_devices, &blk_dev->list);
    
                    break;
                }
                else
                {
                    rt_free(blk_dev);
                    blk_dev = RT_NULL;
                    break;
                }
            }
457 458 459 460

#ifdef RT_USING_DFS_MNTTABLE
            if (0) // if (blk_dev)
            {
461
            	LOG_I("try to mount file system!");
462 463 464 465
            	/* try to mount file system on this block device */
            	dfs_mount_device(&(blk_dev->dev));
            }
#endif
Y
yiyue.fang 已提交
466 467 468 469
        }
    }
    else
    {
470
        LOG_E("read mmcsd first sector failed");
Y
yiyue.fang 已提交
471 472 473 474 475 476 477
        err = -RT_ERROR;
    }
    
    /* release sector buffer */
    rt_free(sector);
    
    return err;
478 479 480 481
}

void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card)
{
482
    rt_list_t *l, *n;
Y
yiyue.fang 已提交
483
    struct mmcsd_blk_device *blk_dev;
484 485

    for (l = (&blk_devices)->next, n = l->next; l != &blk_devices; l = n)
Y
yiyue.fang 已提交
486 487 488 489
    {
        blk_dev = (struct mmcsd_blk_device *)rt_list_entry(l, struct mmcsd_blk_device, list);
        if (blk_dev->card == card) 
        {
490 491 492 493 494 495 496
        	/* unmount file system */
        	const char * mounted_path = dfs_filesystem_get_mounted_path(&(blk_dev->dev));
        	if (mounted_path)
        	{
        		dfs_unmount(mounted_path);
        	}

Y
yiyue.fang 已提交
497 498 499 500 501
            rt_device_unregister(&blk_dev->dev);
            rt_list_remove(&blk_dev->list);
            rt_free(blk_dev);
        }
    }
502 503
}

504 505 506 507 508 509
/*
 * This function will initialize block device on the mmc/sd.
 *
 * @deprecated since 2.1.0, this function does not need to be invoked
 * in the system initialization.
 */
W
weety 已提交
510
int rt_mmcsd_blk_init(void)
511
{
W
weety 已提交
512 513
    /* nothing */
    return 0;
514
}