qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH pic32 v3 14/16] pic32: add file pic32_sdcard.c


From: Peter Crosthwaite
Subject: Re: [Qemu-devel] [PATCH pic32 v3 14/16] pic32: add file pic32_sdcard.c
Date: Mon, 6 Jul 2015 01:05:04 -0700

On Sun, Jul 5, 2015 at 11:15 PM, Serge Vakulenko
<address@hidden> wrote:
> Implement access to SD card, attached to pic32 SPI port.
>

Can you use sd.c SD card definition?

> Signed-off-by: Serge Vakulenko <address@hidden>
> ---
>  hw/mips/pic32_sdcard.c | 428 
> +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 428 insertions(+)
>  create mode 100644 hw/mips/pic32_sdcard.c
>
> diff --git a/hw/mips/pic32_sdcard.c b/hw/mips/pic32_sdcard.c
> new file mode 100644
> index 0000000..7611233
> --- /dev/null
> +++ b/hw/mips/pic32_sdcard.c
> @@ -0,0 +1,428 @@
> +/*
> + * SD/MMC card emulation.
> + *
> + * Copyright (C) 2011-2015 Serge Vakulenko
> + *
> + * Permission to use, copy, modify, and distribute this software
> + * and its documentation for any purpose and without fee is hereby
> + * granted, provided that the above copyright notice appear in all
> + * copies and that both that the copyright notice and this
> + * permission notice and warranty disclaimer appear in supporting
> + * documentation, and that the name of the author not be used in
> + * advertising or publicity pertaining to distribution of the
> + * software without specific, written prior permission.
> + *
> + * The author disclaim all warranties with regard to this
> + * software, including all implied warranties of merchantability
> + * and fitness.  In no event shall the author be liable for any
> + * special, indirect or consequential damages or any damages
> + * whatsoever resulting from loss of use, data or profits, whether
> + * in an action of contract, negligence or other tortious action,
> + * arising out of or in connection with the use or performance of
> + * this software.
> + */
> +#include "hw/hw.h"
> +#include "pic32_peripherals.h"
> +
> +/*
> + * Definitions for MMC/SDC commands.
> + */
> +#define CMD_GO_IDLE         (0x40+0)    /* CMD0 */
> +#define CMD_SEND_OP_SDC     (0x40+41)   /* ACMD41 (SDC) */
> +#define CMD_SET_BLEN        (0x40+16)
> +#define CMD_SEND_IF_COND    (0x40+8)
> +#define CMD_SEND_CSD        (0x40+9)
> +#define CMD_STOP            (0x40+12)
> +#define CMD_READ_SINGLE     (0x40+17)
> +#define CMD_READ_MULTIPLE   (0x40+18)
> +#define CMD_SET_WBECNT      (0x40+23)   /* ACMD23 */
> +#define CMD_WRITE_SINGLE    (0x40+24)
> +#define CMD_WRITE_MULTIPLE  (0x40+25)
> +#define CMD_APP             (0x40+55)   /* CMD55 */
> +
> +#define DATA_START_BLOCK        0xFE    /* start data for single block */
> +#define STOP_TRAN_TOKEN         0xFD    /* stop token for write multiple */
> +#define WRITE_MULTIPLE_TOKEN    0xFC    /* start data for write multiple */
> +
> +/*
> + * TODO: add option to enable tracing.
> + */
> +#define TRACE   0
> +
> +static void read_data(int fd, unsigned offset,
> +    unsigned char *buf, unsigned blen)
> +{
> +    int result;
> +
> +    /* Fill uninitialized blocks by FF: simulate real flash media. */
> +    memset(buf, 0xFF, blen);
> +
> +    result = pread(fd, buf, blen, offset);
> +    if (result < 0) {
> +        printf("sdcard: pread failed, %u bytes, offset %#x: %s\n",
> +            blen, offset, strerror(errno));
> +        return;
> +    }
> +    if (result != blen) {
> +        /* Partial file read, return zeroes. */
> +        memset(buf, 0, blen);
> +    }
> +#if 0
> +    printf("(%#x)\n", offset);
> +    int i, k;
> +    for (i = 0; i < blen; i += 32) {
> +        for (k = 0; k < 32; k++) {
> +            printf(" %02x", buf[i+k]);
> +        }
> +        printf("\n");
> +    }
> +#endif
> +}
> +
> +static void write_data(int fd, unsigned offset,
> +    unsigned char *buf, unsigned blen)
> +{
> +    if (pwrite(fd, buf, blen, offset) != blen) {
> +        printf("sdcard: pwrite failed, offset %#x\n", offset);
> +        return;
> +    }
> +}
> +
> +static void card_reset(sdcard_t *d)
> +{
> +    d->select = 0;
> +    d->blen = 512;
> +    d->count = 0;
> +}
> +
> +/*
> + * Reset sdcard.
> + */
> +void pic32_sdcard_reset(pic32_t *s)
> +{
> +    card_reset(&s->sdcard[0]);
> +    card_reset(&s->sdcard[1]);
> +}
> +
> +/*
> + * Initialize SD card.
> + */
> +void pic32_sdcard_init(pic32_t *s, int unit, const char *name,
> +    const char *filename, int cs_port, int cs_pin)
> +{
> +    sdcard_t *d = &s->sdcard[unit];
> +    struct stat st;
> +
> +    memset(d, 0, sizeof(*d));
> +    d->name = name;
> +    if (!filename) {
> +        /* No SD card installed. */
> +        return;
> +    }
> +    d->gpio_port = cs_port;
> +    d->gpio_cs = (cs_pin >= 0) ? (1 << cs_pin) : 0;
> +
> +    d->fd = open(filename, O_RDWR);

Storage media is managed by QEMUs block layer rather than devs doing
their own file ops. Using sd.c will handle this for you. Note there is
SPI sd support (although it is a little dated). check ssi-sd.c.

Regards,
Peter

> +    if (d->fd < 0) {
> +        /* Fatal: no image available. */
> +        perror(filename);
> +        exit(1);
> +    }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]