summaryrefslogtreecommitdiff
path: root/bsp/zynqmp-r5-axu4ev/drivers/drv_sdcard.c
blob: b1f700971b0d88966d81b16410d7adcbccd9b2ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
160
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-24     WangHuachen  the first version
 */
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <dfs_fs.h>
#include <drivers/dev_mmcsd_core.h>

#include <stdint.h>
#include <stdio.h>

#include "drv_sdcard.h"
#include "xsdps_core.h"

#define DBG_TAG "drv.sdcard"
#define DBG_LVL DBG_INFO
#include "rtdbg.h"

#define FILE_SYSTEM_INTERFACE_SD
#define SD_CD_DELAY 10000U

typedef rt_uint8_t BYTE;
typedef rt_uint32_t DWORD;
typedef unsigned int UINT;

/* Status of Disk Functions */
typedef rt_uint8_t DSTATUS;

/* Results of Disk Functions */
typedef enum
{
    RES_OK = 0, /* 0: Successful */
    RES_ERROR,  /* 1: R/W Error */
    RES_WRPRT,  /* 2: Write Protected */
    RES_NOTRDY, /* 3: Not Ready */
    RES_PARERR  /* 4: Invalid Parameter */
} DRESULT;

/* Disk Status Bits (DSTATUS) */

#define STA_NOINIT 0x01  /* Drive not initialized */
#define STA_NODISK 0x02  /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */

static DSTATUS Stat[2] = {STA_NOINIT, STA_NOINIT}; /* Disk status */

#ifdef FILE_SYSTEM_INTERFACE_SD
static XSdPs SdInstance[2];
static u32 BaseAddress;
static u32 CardDetect;
static u32 WriteProtect;
static u32 SlotType[2];
static u8 HostCntrlrVer[2];
#endif

static BYTE sdcard_drvnum = 0;
static struct rt_device sdcard_device;
static struct dfs_partition part;
static struct rt_mutex sd_lock[2];

static DSTATUS disk_status(
    BYTE pdrv /* Drive number (0) */
)
{
    DSTATUS s = Stat[pdrv];
#ifdef FILE_SYSTEM_INTERFACE_SD
    u32 StatusReg;
    u32 DelayCount = 0;

    if (SdInstance[pdrv].Config.BaseAddress == (u32)0)
    {
#ifdef XPAR_XSDPS_1_DEVICE_ID
        if (pdrv == 1)
        {
            BaseAddress = XPAR_XSDPS_1_BASEADDR;
            CardDetect = XPAR_XSDPS_1_HAS_CD;
            WriteProtect = XPAR_XSDPS_1_HAS_WP;
        }
        else
        {
#endif
            BaseAddress = XPAR_XSDPS_0_BASEADDR;
            CardDetect = XPAR_XSDPS_0_HAS_CD;
            WriteProtect = XPAR_XSDPS_0_HAS_WP;
#ifdef XPAR_XSDPS_1_DEVICE_ID
        }
#endif
        HostCntrlrVer[pdrv] = (u8)(XSdPs_ReadReg16(BaseAddress,
                                                   XSDPS_HOST_CTRL_VER_OFFSET) &
                                   XSDPS_HC_SPEC_VER_MASK);
        if (HostCntrlrVer[pdrv] == XSDPS_HC_SPEC_V3)
        {
            SlotType[pdrv] = XSdPs_ReadReg(BaseAddress,
                                           XSDPS_CAPS_OFFSET) &
                             XSDPS_CAPS_SLOT_TYPE_MASK;
        }
        else
        {
            SlotType[pdrv] = 0;
        }
    }

    /* If SD is not powered up then mark it as not initialized */
    if ((XSdPs_ReadReg8((u32)BaseAddress, XSDPS_POWER_CTRL_OFFSET) &
         XSDPS_PC_BUS_PWR_MASK) == 0U)
    {
        s |= STA_NOINIT;
    }

    StatusReg = XSdPs_GetPresentStatusReg((u32)BaseAddress);
    if (SlotType[pdrv] != XSDPS_CAPS_EMB_SLOT)
    {
        if (CardDetect)
        {
            while ((StatusReg & XSDPS_PSR_CARD_INSRT_MASK) == 0U)
            {
                if (DelayCount == 500U)
                {
                    s = STA_NODISK | STA_NOINIT;
                    goto Label;
                }
                else
                {
                    /* Wait for 10 msec */
                    usleep(SD_CD_DELAY);
                    DelayCount++;
                    StatusReg = XSdPs_GetPresentStatusReg((u32)BaseAddress);
                }
            }
        }
        s &= ~STA_NODISK;
        if (WriteProtect)
        {
            if ((StatusReg & XSDPS_PSR_WPS_PL_MASK) == 0U)
            {
                s |= STA_PROTECT;
                goto Label;
            }
        }
        s &= ~STA_PROTECT;
    }
    else
    {
        s &= ~STA_NODISK & ~STA_PROTECT;
    }

Label:
    Stat[pdrv] = s;
#endif

    return s;
}

static DSTATUS disk_initialize(
    BYTE pdrv /* Physical drive number (0) */
)
{
    DSTATUS s;
#ifdef FILE_SYSTEM_INTERFACE_SD
    s32 Status = XST_FAILURE;
    XSdPs_Config *SdConfig;
#endif

    s = disk_status(pdrv);
    if ((s & STA_NODISK) != 0U)
    {
        return s;
    }

    /* If disk is already initialized */
    if ((s & STA_NOINIT) == 0U)
    {
        return s;
    }

#ifdef FILE_SYSTEM_INTERFACE_SD
    if (CardDetect)
    {
        /*
             * Card detection check
             * If the HC detects the No Card State, power will be cleared
             */
        while (!((XSDPS_PSR_CARD_DPL_MASK |
                  XSDPS_PSR_CARD_STABLE_MASK |
                  XSDPS_PSR_CARD_INSRT_MASK) ==
                 (XSdPs_GetPresentStatusReg((u32)BaseAddress) &
                  (XSDPS_PSR_CARD_DPL_MASK |
                   XSDPS_PSR_CARD_STABLE_MASK |
                   XSDPS_PSR_CARD_INSRT_MASK))))
            ;
    }

    /*
     * Initialize the host controller
     */
    SdConfig = XSdPs_LookupConfig((u16)pdrv);
    if (NULL == SdConfig)
    {
        s |= STA_NOINIT;
        return s;
    }

    Status = XSdPs_CfgInitialize(&SdInstance[pdrv], SdConfig,
                                 SdConfig->BaseAddress);
    if (Status != XST_SUCCESS)
    {
        s |= STA_NOINIT;
        return s;
    }

    Status = XSdPs_CardInitialize(&SdInstance[pdrv]);
    if (Status != XST_SUCCESS)
    {
        s |= STA_NOINIT;
        return s;
    }

    /*
     * Disk is initialized.
     * Store the same in Stat.
     */
    s &= (~STA_NOINIT);

    Stat[pdrv] = s;
#endif

#ifdef FILE_SYSTEM_INTERFACE_RAM
    /* Assign RAMFS address value from xparameters.h */
    dataramfs = (char *)RAMFS_START_ADDR;

    /* Clearing No init Status for RAM */
    s &= (~STA_NOINIT);
    Stat[pdrv] = s;
#endif

    return s;
}

static DRESULT disk_read(
    BYTE pdrv,    /* Physical drive number (0) */
    BYTE *buff,   /* Pointer to the data buffer to store read data */
    DWORD sector, /* Start sector number (LBA) */
    UINT count    /* Sector count (1..128) */
)
{
    DSTATUS s;
#ifdef FILE_SYSTEM_INTERFACE_SD
    s32 Status = XST_FAILURE;
    DWORD LocSector = sector;
#endif

    s = disk_status(pdrv);

    if ((s & STA_NOINIT) != 0U)
    {
        return RES_NOTRDY;
    }
    if (count == 0U)
    {
        return RES_PARERR;
    }

#ifdef FILE_SYSTEM_INTERFACE_SD
    /* Convert LBA to byte address if needed */
    if ((SdInstance[pdrv].HCS) == 0U)
    {
        LocSector *= (DWORD)XSDPS_BLK_SIZE_512_MASK;
    }

    Status = XSdPs_ReadPolled(&SdInstance[pdrv], (u32)LocSector, count, buff);
    if (Status != XST_SUCCESS)
    {
        return RES_ERROR;
    }
#endif

#ifdef FILE_SYSTEM_INTERFACE_RAM
    memcpy(buff, dataramfs + (sector * SECTORSIZE), count * SECTORSIZE);
#endif

    return RES_OK;
}

static DRESULT disk_write(
    BYTE pdrv,        /* Physical drive nmuber (0..) */
    const BYTE *buff, /* Data to be written */
    DWORD sector,     /* Sector address (LBA) */
    UINT count        /* Number of sectors to write (1..128) */
)
{
    DSTATUS s;
#ifdef FILE_SYSTEM_INTERFACE_SD
    s32 Status = XST_FAILURE;
    DWORD LocSector = sector;
#endif

    s = disk_status(pdrv);
    if ((s & STA_NOINIT) != 0U)
    {
        return RES_NOTRDY;
    }
    if (count == 0U)
    {
        return RES_PARERR;
    }

#ifdef FILE_SYSTEM_INTERFACE_SD
    /* Convert LBA to byte address if needed */
    if ((SdInstance[pdrv].HCS) == 0U)
    {
        LocSector *= (DWORD)XSDPS_BLK_SIZE_512_MASK;
    }

    Status = XSdPs_WritePolled(&SdInstance[pdrv], (u32)LocSector, count, buff);
    if (Status != XST_SUCCESS)
    {
        return RES_ERROR;
    }

#endif

#ifdef FILE_SYSTEM_INTERFACE_RAM
    memcpy(dataramfs + (sector * SECTORSIZE), buff, count * SECTORSIZE);
#endif

    return RES_OK;
}

static rt_err_t rt_sdcard_init(rt_device_t dev)
{
    char sdlock_name[20];
    BYTE drvnum = *((BYTE *)dev->user_data);

    rt_snprintf(sdlock_name, sizeof(sdlock_name), "sdlock%d", drvnum);
    if (rt_mutex_init(&sd_lock[drvnum], sdlock_name, RT_IPC_FLAG_PRIO) != RT_EOK)
    {
        LOG_E("init sdlock semaphore failed\n");
    }
    else
        LOG_D("sdcard init OK\n");

    return RT_EOK;
}

static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag)
{
    return RT_EOK;
}

static rt_err_t rt_sdcard_close(rt_device_t dev)
{
    return RT_EOK;
}

static rt_ssize_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
    RT_ASSERT(((rt_uint32_t)buffer % 4) == 0);
    DRESULT status;
    BYTE drvnum = *((BYTE *)dev->user_data);

    rt_mutex_take(&sd_lock[drvnum], RT_WAITING_FOREVER);
    status = disk_read(drvnum, buffer, part.offset + pos, size);
    rt_mutex_release(&sd_lock[drvnum]);

    if (status == RES_OK)
        return size;

    LOG_E("sdcard read failed\n");
    return 0;
}

static rt_ssize_t rt_sdcard_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    RT_ASSERT(((rt_uint32_t)buffer % 4) == 0);
    DRESULT status;
    BYTE drvnum = *((BYTE *)dev->user_data);

    rt_mutex_take(&sd_lock[drvnum], RT_WAITING_FOREVER);
    status = disk_write(drvnum, buffer, part.offset + pos, size);
    rt_mutex_release(&sd_lock[drvnum]);

    if (status == RES_OK)
        return size;

    LOG_E("sdcard write failed\n");
    return 0;
}

static rt_err_t rt_sdcard_control(rt_device_t dev, int cmd, void *args)
{
    RT_ASSERT(dev != RT_NULL);

    BYTE drvnum = *((BYTE *)dev->user_data);

    if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
    {
        struct rt_device_blk_geometry *geometry;

        geometry = (struct rt_device_blk_geometry *)args;
        if (geometry == RT_NULL)
            return -RT_ERROR;

        geometry->bytes_per_sector = 512;
        geometry->block_size = 128;
        geometry->sector_count = SdInstance[drvnum].SectorCount;
    }

    return RT_EOK;
}

#ifdef BSP_USING_SD0
static int rh_hw_emmc_init(void)
{
    if (disk_initialize(sdcard_drvnum) == RES_OK)
    {
        DRESULT status;
        rt_uint8_t *sector;

        /* get the first sector to read partition table */
        sector = (rt_uint8_t *)rt_malloc(512);
        if (sector == RT_NULL)
        {
            LOG_E("allocate partition sector buffer failed\n");
            return -RT_ERROR;
        }
        status = disk_read(0, sector, 0, 1);
        if (status == RES_OK)
        {
            /* get the first partition */
            if (dfs_filesystem_get_partition(&part, sector, 0) != 0)
            {
                /* there is no partition */
                part.offset = 0;
                part.size = 0;
            }
        }
        else
        {
            /* there is no partition table */
            part.offset = 0;
            part.size = 0;
        }

        /* release sector buffer */
        rt_free(sector);

        /* register sdcard device */
        sdcard_device.type = RT_Device_Class_Block;
        sdcard_device.init = rt_sdcard_init;
        sdcard_device.open = rt_sdcard_open;
        sdcard_device.close = rt_sdcard_close;
        sdcard_device.read = rt_sdcard_read;
        sdcard_device.write = rt_sdcard_write;
        sdcard_device.control = rt_sdcard_control;

        /* no private */
        sdcard_device.user_data = &sdcard_drvnum;
        rt_device_register(&sdcard_device, "sd0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
        return RT_EOK;
    }

    return -RT_ERROR;
}
INIT_DEVICE_EXPORT(rh_hw_emmc_init);
#endif