drv_sdio.c 17.3 KB
Newer Older
B
bigmagic 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * File      : drv_sdio.c
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2019-07-29     zdzn           first version
 */

#include "drv_sdio.h"
B
bigmagic 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include "raspi.h"

static rt_uint32_t sdCommandTable[] = {
    SD_CMD_INDEX(0),
    SD_CMD_RESERVED(1),
    SD_CMD_INDEX(2) | SD_RESP_R2,
    SD_CMD_INDEX(3) | SD_RESP_R1,
    SD_CMD_INDEX(4),
    SD_CMD_RESERVED(5), //SD_CMD_INDEX(5) | SD_RESP_R4,
    SD_CMD_INDEX(6) | SD_RESP_R1,
    SD_CMD_INDEX(7) | SD_RESP_R1b,
    SD_CMD_INDEX(8) | SD_RESP_R1,
    SD_CMD_INDEX(9) | SD_RESP_R2,
    SD_CMD_INDEX(10) | SD_RESP_R2,
    SD_CMD_INDEX(11) | SD_RESP_R1,
    SD_CMD_INDEX(12) | SD_RESP_R1b | SD_CMD_TYPE_ABORT,
    SD_CMD_INDEX(13) | SD_RESP_R1,
    SD_CMD_RESERVED(14),
    SD_CMD_INDEX(15),
    SD_CMD_INDEX(16) | SD_RESP_R1,
    SD_CMD_INDEX(17) | SD_RESP_R1 | SD_DATA_READ,
    SD_CMD_INDEX(18) | SD_RESP_R1 | SD_DATA_READ | SD_CMD_MULTI_BLOCK | SD_CMD_BLKCNT_EN,
    SD_CMD_INDEX(19) | SD_RESP_R1 | SD_DATA_READ,
    SD_CMD_INDEX(20) | SD_RESP_R1b,
    SD_CMD_RESERVED(21),
    SD_CMD_RESERVED(22),
    SD_CMD_INDEX(23) | SD_RESP_R1,
    SD_CMD_INDEX(24) | SD_RESP_R1 | SD_DATA_WRITE,
    SD_CMD_INDEX(25) | SD_RESP_R1 | SD_DATA_WRITE | SD_CMD_MULTI_BLOCK | SD_CMD_BLKCNT_EN,
    SD_CMD_INDEX(26) | SD_RESP_R1 | SD_DATA_WRITE, //add
    SD_CMD_INDEX(27) | SD_RESP_R1 | SD_DATA_WRITE,
    SD_CMD_INDEX(28) | SD_RESP_R1b,
    SD_CMD_INDEX(29) | SD_RESP_R1b,
    SD_CMD_INDEX(30) | SD_RESP_R1 | SD_DATA_READ,
    SD_CMD_RESERVED(31),
    SD_CMD_INDEX(32) | SD_RESP_R1,
    SD_CMD_INDEX(33) | SD_RESP_R1,
    SD_CMD_RESERVED(34),
    SD_CMD_INDEX(35) | SD_RESP_R1, //add
    SD_CMD_INDEX(36) | SD_RESP_R1, //add
    SD_CMD_RESERVED(37),
    SD_CMD_INDEX(38) | SD_RESP_R1b,
    SD_CMD_INDEX(39) | SD_RESP_R4, //add
    SD_CMD_INDEX(40) | SD_RESP_R5, //add
    SD_CMD_INDEX(41) | SD_RESP_R3, //add, mov from harbote
    SD_CMD_RESERVED(42) | SD_RESP_R1,
    SD_CMD_RESERVED(43),
    SD_CMD_RESERVED(44),
    SD_CMD_RESERVED(45),
    SD_CMD_RESERVED(46),
    SD_CMD_RESERVED(47),
    SD_CMD_RESERVED(48),
    SD_CMD_RESERVED(49),
    SD_CMD_RESERVED(50),
    SD_CMD_INDEX(51) | SD_RESP_R1 | SD_DATA_READ,
    SD_CMD_RESERVED(52),
    SD_CMD_RESERVED(53),
    SD_CMD_RESERVED(54),
    SD_CMD_INDEX(55) | SD_RESP_R3,
    SD_CMD_INDEX(56) | SD_RESP_R1 | SD_CMD_ISDATA,
    SD_CMD_RESERVED(57),
    SD_CMD_RESERVED(58),
    SD_CMD_RESERVED(59),
    SD_CMD_RESERVED(60),
    SD_CMD_RESERVED(61),
    SD_CMD_RESERVED(62),
    SD_CMD_RESERVED(63)
B
bigmagic 已提交
80 81 82 83
};

static inline rt_uint32_t read32(rt_uint32_t addr)
{
B
bigmagic 已提交
84 85
    return (*((volatile unsigned int*)((rt_uint64_t)addr)));
    //return (*((volatile rt_uint64_t *)(((long))addr)));
B
bigmagic 已提交
86 87 88 89
}

static inline void write32(rt_uint32_t addr, rt_uint32_t value)
{
B
bigmagic 已提交
90 91
    (*((volatile unsigned int*)((rt_uint64_t)addr))) = value;
    //*((volatile rt_uint64_t *)(((long))addr)) = value;
B
bigmagic 已提交
92 93
}

B
bigmagic 已提交
94
rt_err_t sd_int(struct sdhci_pdata_t * pdat, unsigned int mask)
B
bigmagic 已提交
95
{
B
bigmagic 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    unsigned int r;
    unsigned int m = mask | INT_ERROR_MASK;
    int cnt = 1000000;
    while (!(read32(pdat->virt + EMMC_INTERRUPT) & (m | INT_ERROR_MASK)) && cnt--)
        DELAY_MICROS(1);
    r = read32(pdat->virt + EMMC_INTERRUPT);
    if (cnt <= 0 || (r & INT_CMD_TIMEOUT) || (r & INT_DATA_TIMEOUT))
    {
        write32(pdat->virt + EMMC_INTERRUPT, r);
        //qemu maybe can not use sdcard
        //rt_kprintf("send cmd/data timeout wait for %x int: %x, status: %x\n",mask, r, read32(pdat->virt + EMMC_STATUS));
        //return -RT_ETIMEOUT;
    }
    else if (r & INT_ERROR_MASK)
    {
        write32(pdat->virt + EMMC_INTERRUPT, r);
        rt_kprintf("send cmd/data error %x -> %x\n",r, read32(pdat->virt + EMMC_INTERRUPT));
        return -RT_ERROR;
    }
    write32(pdat->virt + EMMC_INTERRUPT, mask);
    return RT_EOK;
}
B
bigmagic 已提交
118

B
bigmagic 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131
rt_err_t sd_status(struct sdhci_pdata_t * pdat, unsigned int mask)
{
    int cnt = 500000;
    while ((read32(pdat->virt + EMMC_STATUS) & mask) && !(read32(pdat->virt + EMMC_INTERRUPT) & INT_ERROR_MASK) && cnt--)
        DELAY_MICROS(1);
    if (cnt <= 0)
    {
        return -RT_ETIMEOUT;
    }
    else if (read32(pdat->virt + EMMC_INTERRUPT) & INT_ERROR_MASK)
    {
        return  -RT_ERROR;
    } 
B
bigmagic 已提交
132

B
bigmagic 已提交
133 134
    return RT_EOK;
}
B
bigmagic 已提交
135

B
bigmagic 已提交
136 137 138 139 140 141 142 143 144 145
static rt_err_t raspi_transfer_command(struct sdhci_pdata_t * pdat, struct sdhci_cmd_t * cmd)
{
    rt_uint32_t cmdidx;
    rt_err_t ret = RT_EOK;
    ret = sd_status(pdat, SR_CMD_INHIBIT);
    if (ret)
    {
        rt_kprintf("ERROR: EMMC busy %d\n", ret);
        return ret;
    }
B
bigmagic 已提交
146

B
bigmagic 已提交
147 148 149 150 151 152 153 154 155
    cmdidx = sdCommandTable[cmd->cmdidx];
    if (cmdidx == 0xFFFFFFFF)
        return -RT_EINVAL;
    if (cmd->datarw == DATA_READ)
        cmdidx |= SD_DATA_READ;
    if (cmd->datarw == DATA_WRITE)
        cmdidx |= SD_DATA_WRITE;
    mmcsd_dbg("transfer cmd %x(%d) %x %x\n", cmdidx, cmd->cmdidx, cmd->cmdarg, read32(pdat->virt + EMMC_INTERRUPT));
    write32(pdat->virt + EMMC_INTERRUPT,read32(pdat->virt + EMMC_INTERRUPT));
B
bigmagic 已提交
156 157
    write32(pdat->virt + EMMC_ARG1, cmd->cmdarg);
    write32(pdat->virt + EMMC_CMDTM, cmdidx);
B
bigmagic 已提交
158 159 160 161 162 163
    if (cmd->cmdidx == SD_APP_OP_COND)
        DELAY_MICROS(1000);
    else if ((cmd->cmdidx == SD_SEND_IF_COND) || (cmd->cmdidx == APP_CMD))
        DELAY_MICROS(100);
    ret = sd_int(pdat, INT_CMD_DONE);
    if (ret)
B
bigmagic 已提交
164
    {
B
bigmagic 已提交
165 166 167 168 169 170
        return ret;
    }
    if (cmd->resptype & RESP_MASK)
    {

        if (cmd->resptype & RESP_R2)
B
bigmagic 已提交
171
        {
B
bigmagic 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            rt_uint32_t resp[4];
            resp[0] = read32(pdat->virt + EMMC_RESP0);
            resp[1] = read32(pdat->virt + EMMC_RESP1);
            resp[2] = read32(pdat->virt + EMMC_RESP2);
            resp[3] = read32(pdat->virt + EMMC_RESP3);
            if (cmd->resptype == RESP_R2)
            {
                cmd->response[0] = resp[3]<<8 |((resp[2]>>24)&0xff);
                cmd->response[1] = resp[2]<<8 |((resp[1]>>24)&0xff);
                cmd->response[2] = resp[1]<<8 |((resp[0]>>24)&0xff);
                cmd->response[3] = resp[0]<<8 ;
            }
            else
            {
                cmd->response[0] = resp[0];
                cmd->response[1] = resp[1];
                cmd->response[2] = resp[2];
                cmd->response[3] = resp[3];
            }
B
bigmagic 已提交
191
        }
B
bigmagic 已提交
192 193
        else
            cmd->response[0] = read32(pdat->virt + EMMC_RESP0);
B
bigmagic 已提交
194
    }
B
bigmagic 已提交
195
    mmcsd_dbg("response: %x: %x %x %x %x (%x, %x)\n", cmd->resptype, cmd->response[0], cmd->response[1], cmd->response[2], cmd->response[3], read32(pdat->virt + EMMC_STATUS),read32(pdat->virt + EMMC_INTERRUPT));
B
bigmagic 已提交
196 197 198 199 200
    return ret;
}

static rt_err_t read_bytes(struct sdhci_pdata_t * pdat, rt_uint32_t * buf, rt_uint32_t blkcount, rt_uint32_t blksize)
{
B
bigmagic 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    int c = 0;
    rt_err_t ret;
    int d;
    while (c < blkcount)
    {
        if ((ret = sd_int(pdat, INT_READ_RDY)))
        {
            rt_kprintf("timeout happens when reading block %d\n",c);
            return ret;
        }
        for (d=0; d < blksize / 4; d++)
            if (read32(pdat->virt + EMMC_STATUS) & SR_READ_AVAILABLE)
                buf[d] = read32(pdat->virt + EMMC_DATA);
        c++;
        buf += blksize / 4;
    }
B
bigmagic 已提交
217 218 219 220 221
    return RT_EOK;
}

static rt_err_t write_bytes(struct sdhci_pdata_t * pdat, rt_uint32_t * buf, rt_uint32_t blkcount, rt_uint32_t blksize)
{
B
bigmagic 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    int c = 0;
    rt_err_t ret;
    int d;
    while (c < blkcount)
    {
        if ((ret = sd_int(pdat, INT_WRITE_RDY)))
        {
            return ret;
        }
        for (d=0; d < blksize / 4; d++)
            write32(pdat->virt + EMMC_DATA, buf[d]);
        c++;
        buf += blksize / 4;
    }
    if ((ret = sd_int(pdat, INT_DATA_DONE)))
    {
        return ret;
    }
B
bigmagic 已提交
240 241 242 243 244 245
    return RT_EOK;
}

static rt_err_t raspi_transfer_data(struct sdhci_pdata_t * pdat, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat)
{
    rt_uint32_t dlen = (rt_uint32_t)(dat->blkcnt * dat->blksz);
B
bigmagic 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    rt_err_t ret = sd_status(pdat, SR_DAT_INHIBIT);
    if (ret)
    {
        rt_kprintf("ERROR: EMMC busy\n");
        return ret;
    }
    if (dat->blkcnt > 1)
    {
        struct sdhci_cmd_t newcmd;
        newcmd.cmdidx = SET_BLOCK_COUNT;
        newcmd.cmdarg = dat->blkcnt;
        newcmd.resptype = RESP_R1;
        ret = raspi_transfer_command(pdat, &newcmd);
        if (ret) return ret;
    }
B
bigmagic 已提交
261

B
bigmagic 已提交
262
    if(dlen < 512)
B
bigmagic 已提交
263
    {
B
bigmagic 已提交
264 265 266 267 268 269 270 271 272
        write32(pdat->virt + EMMC_BLKSIZECNT, dlen | 1 << 16);
    }
    else
    {
        write32(pdat->virt + EMMC_BLKSIZECNT, 512 | (dat->blkcnt) << 16);
    }
    if (dat->flag & DATA_DIR_READ)
    {
        cmd->datarw = DATA_READ;
B
bigmagic 已提交
273
        ret = raspi_transfer_command(pdat, cmd);
B
bigmagic 已提交
274 275
        if (ret) return ret;
        mmcsd_dbg("read_block %d, %d\n", dat->blkcnt, dat->blksz );
B
bigmagic 已提交
276 277
        ret = read_bytes(pdat, (rt_uint32_t *)dat->buf, dat->blkcnt, dat->blksz);
    }
B
bigmagic 已提交
278
    else if (dat->flag & DATA_DIR_WRITE)
B
bigmagic 已提交
279
    {
B
bigmagic 已提交
280
        cmd->datarw = DATA_WRITE;
B
bigmagic 已提交
281
        ret = raspi_transfer_command(pdat, cmd);
B
bigmagic 已提交
282 283
        if (ret) return ret;
        mmcsd_dbg("write_block %d, %d", dat->blkcnt, dat->blksz );
B
bigmagic 已提交
284 285 286 287 288
        ret = write_bytes(pdat, (rt_uint32_t *)dat->buf, dat->blkcnt, dat->blksz);
    }
    return ret;
}

B
bigmagic 已提交
289 290 291 292 293 294 295 296 297
static rt_err_t sdhci_transfer(struct sdhci_t * sdhci, struct sdhci_cmd_t * cmd, struct sdhci_data_t * dat)
{
    struct sdhci_pdata_t * pdat = (struct sdhci_pdata_t *)sdhci->priv;
    if (!dat)
        return raspi_transfer_command(pdat, cmd);

    return raspi_transfer_data(pdat, cmd, dat);
}

B
bigmagic 已提交
298 299 300 301 302 303 304 305 306 307 308 309
static void mmc_request_send(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
{
    struct sdhci_t *sdhci = (struct sdhci_t *)host->private_data;
    struct sdhci_cmd_t cmd;
    struct sdhci_cmd_t stop;
    struct sdhci_data_t dat;
    rt_memset(&cmd, 0, sizeof(struct sdhci_cmd_t));
    rt_memset(&stop, 0, sizeof(struct sdhci_cmd_t));
    rt_memset(&dat, 0, sizeof(struct sdhci_data_t));

    cmd.cmdidx = req->cmd->cmd_code;
    cmd.cmdarg = req->cmd->arg;
B
bigmagic 已提交
310 311
    cmd.resptype =resp_type(req->cmd);
    if (req->data)
B
bigmagic 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    {
        dat.buf = (rt_uint8_t *)req->data->buf;
        dat.flag = req->data->flags;
        dat.blksz = req->data->blksize;
        dat.blkcnt = req->data->blks;

        req->cmd->err = sdhci_transfer(sdhci, &cmd, &dat);
    }
    else
    {
        req->cmd->err = sdhci_transfer(sdhci, &cmd, RT_NULL);
    }

    req->cmd->resp[3] = cmd.response[3];
    req->cmd->resp[2] = cmd.response[2];
    req->cmd->resp[1] = cmd.response[1];
    req->cmd->resp[0] = cmd.response[0];

    if (req->stop)
    {
        stop.cmdidx = req->stop->cmd_code;
        stop.cmdarg = req->stop->arg;
B
bigmagic 已提交
334
        cmd.resptype =resp_type(req->stop);
B
bigmagic 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        req->stop->err = sdhci_transfer(sdhci, &stop, RT_NULL);
    }

    mmcsd_req_complete(host);
}

rt_int32_t mmc_card_status(struct rt_mmcsd_host *host)
{
    return 0;
}

void mmc_enable_irq(struct rt_mmcsd_host *host, rt_int32_t en)
{

}

static rt_err_t sdhci_detect(struct sdhci_t * sdhci)
{
    return RT_EOK;
}

static rt_err_t sdhci_setwidth(struct sdhci_t * sdhci, rt_uint32_t width)
{
    rt_uint32_t temp = 0;
    struct sdhci_pdata_t * pdat = (struct sdhci_pdata_t *)sdhci->priv;
B
bigmagic 已提交
360 361 362 363 364 365 366
    if (width == MMCSD_BUS_WIDTH_4)
    {
        temp = read32((pdat->virt + EMMC_CONTROL0));
        temp |= C0_HCTL_HS_EN;
        temp |= C0_HCTL_DWITDH;   // always use 4 data lines:
        write32((pdat->virt + EMMC_CONTROL0), temp);
    }
B
bigmagic 已提交
367 368 369
    return RT_EOK;
}

B
bigmagic 已提交
370
static rt_uint32_t sdhci_getdivider(rt_uint32_t sdHostVer, rt_uint32_t freq)
B
bigmagic 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383
{
    rt_uint32_t divisor;
    rt_uint32_t closest = 41666666 / freq;
    rt_uint32_t shiftcount = __rt_fls(closest - 1);


    if (shiftcount > 0) shiftcount--;
    if (shiftcount > 7) shiftcount = 7;
    if (sdHostVer > HOST_SPEC_V2)
        divisor = closest;
    else
        divisor = (1 << shiftcount);

B
bigmagic 已提交
384 385
    if (divisor <= 2)
    {
B
bigmagic 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        divisor = 2;
        shiftcount = 0;
    }

    rt_uint32_t hi = 0;
    if (sdHostVer > HOST_SPEC_V2)
        hi = (divisor & 0x300) >> 2;
    rt_uint32_t lo = (divisor & 0x0ff);
    rt_uint32_t cdiv = (lo << 8) + hi;
    return cdiv;
}

static rt_err_t sdhci_setclock(struct sdhci_t * sdhci, rt_uint32_t clock)
{
    rt_uint32_t temp = 0;
    rt_uint32_t sdHostVer = 0;
    int count = 100000;
B
bigmagic 已提交
403
    struct sdhci_pdata_t * pdat = (struct sdhci_pdata_t *)(sdhci->priv);
B
bigmagic 已提交
404

B
bigmagic 已提交
405 406 407
    while ((read32(pdat->virt + EMMC_STATUS) & (SR_CMD_INHIBIT | SR_DAT_INHIBIT)) && (--count))
        DELAY_MICROS(1);
    if (count <= 0)
B
bigmagic 已提交
408
    {
B
bigmagic 已提交
409
        rt_kprintf("EMMC: Set clock: timeout waiting for inhibit flags. Status %08x.\n",read32(pdat->virt + EMMC_STATUS));
B
bigmagic 已提交
410 411 412 413 414
        return RT_ERROR;
    }

    // Switch clock off.
    temp = read32((pdat->virt + EMMC_CONTROL1));
B
bigmagic 已提交
415
    temp &= ~C1_CLK_EN;
B
bigmagic 已提交
416
    write32((pdat->virt + EMMC_CONTROL1),temp);
B
bigmagic 已提交
417
    DELAY_MICROS(10);
B
bigmagic 已提交
418 419 420 421 422 423 424
    // Request the new clock setting and enable the clock
    temp = read32(pdat->virt + EMMC_SLOTISR_VER);
    sdHostVer = (temp & HOST_SPEC_NUM) >> HOST_SPEC_NUM_SHIFT;
    int cdiv = sdhci_getdivider(sdHostVer, clock);
    temp = read32((pdat->virt + EMMC_CONTROL1));
    temp = (temp & 0xffff003f) | cdiv;
    write32((pdat->virt + EMMC_CONTROL1),temp);
B
bigmagic 已提交
425
    DELAY_MICROS(10);
B
bigmagic 已提交
426 427 428 429 430

    // Enable the clock.
    temp = read32(pdat->virt + EMMC_CONTROL1);
    temp |= C1_CLK_EN;
    write32((pdat->virt + EMMC_CONTROL1),temp);
B
bigmagic 已提交
431
    DELAY_MICROS(10);
B
bigmagic 已提交
432 433
    // Wait for clock to be stable.
    count = 10000;
B
bigmagic 已提交
434 435 436
    while (!(read32(pdat->virt + EMMC_CONTROL1) & C1_CLK_STABLE) && count--)
        DELAY_MICROS(10);
    if (count <= 0)
B
bigmagic 已提交
437
    {
B
bigmagic 已提交
438
        rt_kprintf("EMMC: ERROR: failed to get stable clock %d.\n", clock);
B
bigmagic 已提交
439 440
        return RT_ERROR;
    }
B
bigmagic 已提交
441
    mmcsd_dbg("set stable clock %d.\n", clock);
B
bigmagic 已提交
442 443 444
    return RT_EOK;
}

B
bigmagic 已提交
445 446 447 448 449 450 451
static void mmc_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
{
    struct sdhci_t * sdhci = (struct sdhci_t *)host->private_data;
    sdhci_setclock(sdhci, io_cfg->clock);
    sdhci_setwidth(sdhci, io_cfg->bus_width);
}

B
bigmagic 已提交
452 453 454 455 456 457 458 459 460 461
static const struct rt_mmcsd_host_ops ops =
{
    mmc_request_send,
    mmc_set_iocfg,
    RT_NULL,
    RT_NULL,
};

static void sdmmc_gpio_init()
{
B
bigmagic 已提交
462 463 464 465 466 467 468 469 470 471
//    int pin;
//    bcm283x_gpio_fsel(47,BCM283X_GPIO_FSEL_INPT);
//    bcm283x_gpio_set_pud(47, BCM283X_GPIO_PUD_UP);
//    bcm283x_peri_set_bits(BCM283X_GPIO_BASE + BCM283X_GPIO_GPHEN1, 1<<15, 1<<15);
//    for (pin = 53; pin >= 48; pin--)
//    {
//        bcm283x_gpio_fsel(pin, BCM283X_GPIO_FSEL_ALT3);
//        bcm283x_gpio_set_pud(pin, BCM283X_GPIO_PUD_UP);
//    }
}
B
bigmagic 已提交
472

B
bigmagic 已提交
473 474 475 476 477 478 479 480 481
static rt_err_t reset_emmc(struct sdhci_pdata_t * pdat){
    rt_uint32_t temp;
    int cnt;
    write32((pdat->virt + EMMC_CONTROL0),0);
    temp = read32((pdat->virt + EMMC_CONTROL1));
    temp |= C1_SRST_HC;
    write32((pdat->virt + EMMC_CONTROL1),temp);
    cnt = 10000;
    do
B
bigmagic 已提交
482
    {
B
bigmagic 已提交
483
        DELAY_MICROS(10);
B
bigmagic 已提交
484
    }
B
bigmagic 已提交
485 486 487 488 489 490 491 492 493 494 495
    while ((read32((pdat->virt + EMMC_CONTROL1)) & C1_SRST_HC) && cnt--);
    if (cnt <= 0)
    {
        rt_kprintf("ERROR: failed to reset EMMC\n");
        return RT_ERROR;
    }
    temp = read32((pdat->virt + EMMC_CONTROL1));
    temp |= C1_CLK_INTLEN | C1_TOUNIT_MAX;
    write32((pdat->virt + EMMC_CONTROL1),temp);
    DELAY_MICROS(10);
    return RT_EOK;
B
bigmagic 已提交
496 497
}

B
bigmagic 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
#ifdef RT_MMCSD_DBG
void dump_registers(struct sdhci_pdata_t * pdat){
    rt_kprintf("EMMC registers:");
    int i = EMMC_ARG2;
    for (; i <= EMMC_CONTROL2; i += 4)
        rt_kprintf("\t%x:%x\n", i, read32(pdat->virt + i));
    rt_kprintf("\t%x:%x\n", 0x50, read32(pdat->virt + 0x50));
    rt_kprintf("\t%x:%x\n", 0x70, read32(pdat->virt + 0x70));
    rt_kprintf("\t%x:%x\n", 0x74, read32(pdat->virt + 0x74));
    rt_kprintf("\t%x:%x\n", 0x80, read32(pdat->virt + 0x80));
    rt_kprintf("\t%x:%x\n", 0x84, read32(pdat->virt + 0x84));
    rt_kprintf("\t%x:%x\n", 0x88, read32(pdat->virt + 0x88));
    rt_kprintf("\t%x:%x\n", 0x8c, read32(pdat->virt + 0x8c));
    rt_kprintf("\t%x:%x\n", 0x90, read32(pdat->virt + 0x90));
    rt_kprintf("\t%x:%x\n", 0xf0, read32(pdat->virt + 0xf0));
    rt_kprintf("\t%x:%x\n", 0xfc, read32(pdat->virt + 0xfc));
}
#endif

B
bigmagic 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
int raspi_sdmmc_init(void)
{
    rt_uint32_t virt;
    struct rt_mmcsd_host * host = RT_NULL;
    struct sdhci_pdata_t * pdat = RT_NULL;
    struct sdhci_t * sdhci = RT_NULL;
#ifdef BSP_USING_SDIO0
    host = mmcsd_alloc_host();
    if (!host)
    {
        rt_kprintf("alloc host failed");
        goto err;
    }

    sdhci = rt_malloc(sizeof(struct sdhci_t));
    if (!sdhci)
    {
        rt_kprintf("alloc sdhci failed");
        goto err;
    }
    rt_memset(sdhci, 0, sizeof(struct sdhci_t));

    sdmmc_gpio_init();
    virt = MMC0_BASE_ADDR;

    pdat = (struct sdhci_pdata_t *)rt_malloc(sizeof(struct sdhci_pdata_t));
    RT_ASSERT(pdat != RT_NULL);

    pdat->virt = (rt_uint32_t)virt;
B
bigmagic 已提交
546
    reset_emmc(pdat);
B
bigmagic 已提交
547 548 549 550

    sdhci->name = "sd0";
    sdhci->voltages = VDD_33_34;
    sdhci->width = MMCSD_BUSWIDTH_4;
B
bigmagic 已提交
551
    sdhci->clock = 200 * 1000 * 1000;
B
bigmagic 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
    sdhci->removeable = RT_TRUE;

    sdhci->detect = sdhci_detect;
    sdhci->setwidth = sdhci_setwidth;
    sdhci->setclock = sdhci_setclock;
    sdhci->transfer = sdhci_transfer;
    sdhci->priv = pdat;

    host->ops = &ops;
    host->freq_min = 400000;
    host->freq_max = 50000000;
    host->valid_ocr = VDD_32_33 | VDD_33_34;
    host->flags = MMCSD_MUTBLKWRITE | MMCSD_SUP_HIGHSPEED | MMCSD_SUP_SDIO_IRQ | MMCSD_BUSWIDTH_4;
    host->max_seg_size = 2048;
    host->max_dma_segs = 10;
    host->max_blk_size = 512;
    host->max_blk_count = 4096;

    host->private_data = sdhci;

B
bigmagic 已提交
572 573 574 575 576
    write32((pdat->virt + EMMC_IRPT_EN),0xffffffff);
    write32((pdat->virt + EMMC_IRPT_MASK),0xffffffff);
#ifdef RT_MMCSD_DBG
    dump_registers(pdat);
#endif
B
bigmagic 已提交
577
    mmcsd_change(host);
B
bigmagic 已提交
578
#endif
B
bigmagic 已提交
579 580 581
    return RT_EOK;

err:
B
bigmagic 已提交
582 583
    if (host)  rt_free(host);
    if (sdhci) rt_free(sdhci);
B
bigmagic 已提交
584 585 586

    return -RT_EIO;
}
B
bigmagic 已提交
587 588

INIT_DEVICE_EXPORT(raspi_sdmmc_init);