Navigation Menu

Skip to content

Commit

Permalink
Hack hack hackss...
Browse files Browse the repository at this point in the history
SD card via SPI mode kind of working though...
  • Loading branch information
mithro committed Apr 16, 2017
1 parent 4111496 commit 83113eb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
24 changes: 24 additions & 0 deletions hw/litex/hw.c
Expand Up @@ -273,4 +273,28 @@ void litex_create_memory(MemoryRegion *address_space_mem, qemu_irq irqs[])
}
#endif

#define CSR_SPISDCARD_BASE 0xe0015000
#ifdef CSR_SPISDCARD_BASE
{
DeviceState *spi_master;
DeviceState *spi_sdcard;
SSIBus *spi_bus;
qemu_irq cs_line;

spi_master = qdev_create(NULL, "litex_ssi");
qdev_init_nofail(spi_master);
sysbus_mmio_map(SYS_BUS_DEVICE(spi_master), 0, CSR_SPISDCARD_BASE & MEM_MASK);

spi_bus = (SSIBus *)qdev_get_child_bus(spi_master, "ssi");

spi_sdcard = ssi_create_slave_no_init(spi_bus, "ssi-sd");
qdev_init_nofail(spi_sdcard);

cs_line = qdev_get_gpio_in_named(spi_sdcard, SSI_GPIO_CS, 0);
qdev_connect_gpio_out_named(spi_master, SSI_GPIO_CS, 0, cs_line);
}
#else
#error "Blah!"
#endif

}
4 changes: 2 additions & 2 deletions hw/sd/Makefile.objs
@@ -1,6 +1,6 @@
common-obj-$(CONFIG_PL181) += pl181.o
common-obj-$(CONFIG_SSI_SD) += ssi-sd.o
common-obj-$(CONFIG_SD) += sd.o core.o
common-obj-y += ssi-sd.o
common-obj-y += sd.o core.o
common-obj-$(CONFIG_SDHCI) += sdhci.o

obj-$(CONFIG_MILKYMIST) += milkymist-memcard.o
Expand Down
25 changes: 23 additions & 2 deletions hw/sd/sd.c
Expand Up @@ -41,7 +41,7 @@
#include "qemu/timer.h"
#include "qemu/log.h"

//#define DEBUG_SD 1
#define DEBUG_SD 1

#ifdef DEBUG_SD
#define DPRINTF(fmt, ...) \
Expand Down Expand Up @@ -276,7 +276,7 @@ static void sd_set_csd(SDState *sd, uint64_t size)
uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1;

if (size <= 0x40000000) { /* Standard Capacity SD */
sd->csd[0] = 0x00; /* CSD structure */
sd->csd[0] = 0x40; /* CSD structure */
sd->csd[1] = 0x26; /* Data read access-time-1 */
sd->csd[2] = 0x00; /* Data read access-time-2 */
sd->csd[3] = 0x5a; /* Max. data transfer rate */
Expand Down Expand Up @@ -1298,6 +1298,26 @@ static sd_rsp_type_t sd_normal_command(SDState *sd,
}
break;

case 58: /* CMD58: SPI_READ_OCR */
if (!sd->spi)
goto bad_cmd;

return sd_r3;

// case 59: /* CMD59: SPI_CRC_ON_OFF */
// if (!sd->spi)
// goto bad_cmd;

// return sd_r1;

// case 59: /* CMD59: SPI_CRC_ON_OFF */
// if (!sd->spi)
// goto bad_cmd;
//
// sd->state = sd_transfer_state;
// return sd_r1;


default:
bad_cmd:
qemu_log_mask(LOG_GUEST_ERROR, "SD: Unknown CMD%i\n", req.cmd);
Expand Down Expand Up @@ -1503,6 +1523,7 @@ int sd_do_command(SDState *sd, SDRequest *req,
}

if (rtype == sd_illegal) {
DPRINTF("sd_illegal %d\n", rtype);
sd->card_status |= ILLEGAL_COMMAND;
} else {
/* Valid command, we can update the 'state before command' bits.
Expand Down
7 changes: 4 additions & 3 deletions hw/sd/ssi-sd.c
Expand Up @@ -17,7 +17,7 @@
#include "hw/sd/sd.h"
#include "qapi/error.h"

//#define DEBUG_SSI_SD 1
#define DEBUG_SSI_SD 1

#ifdef DEBUG_SSI_SD
#define DPRINTF(fmt, ...) \
Expand Down Expand Up @@ -96,12 +96,13 @@ static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
request.cmd = s->cmd;
request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
| (s->cmdarg[2] << 8) | s->cmdarg[3];
DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
DPRINTF("CMD%d arg 0x%08x ", s->cmd, request.arg);
s->arglen = sd_do_command(s->sd, &request, longresp);
DPRINTF(" -> %d\n", ((int)s->arglen));
if (s->arglen <= 0) {
s->arglen = 1;
s->response[0] = 4;
DPRINTF("SD command failed\n");
DPRINTF("SD command failed %d\n", ((int)s->arglen));
} else if (s->cmd == 58) {
/* CMD58 returns R3 response (OCR) */
DPRINTF("Returned OCR\n");
Expand Down
6 changes: 3 additions & 3 deletions hw/ssi/bitbang_ssi.c
Expand Up @@ -14,7 +14,7 @@
#include "bitbang_ssi.h"
#include "hw/sysbus.h"

#define DEBUG_BITBANG_SSI
//#define DEBUG_BITBANG_SSI

#ifdef DEBUG_BITBANG_SSI
#define DPRINTF(fmt, ...) \
Expand Down Expand Up @@ -64,14 +64,14 @@ static unsigned char reverse_bits(unsigned char b) {

static void bitbang_ssi_get_miso(bitbang_ssi_interface *ssi)
{
int i = 0;
if (ssi->data_miso_count > 0) {
ssi->last_miso = ssi->data_miso & 0x1;
ssi->data_miso = ssi->data_miso >> 1;
ssi->data_miso_count -= 1;
}
#ifdef DEBUG_BITBANG_SSI
{
int i = 0;
DPRINTF("get_miso %x (%d)[", ssi->last_miso, ssi->data_miso_count);
for (i=0; i < ssi->data_miso_count; i++) {
printf("%s", ((ssi->data_miso >> i) & 0x1) ? "1" : "0");
Expand Down Expand Up @@ -175,7 +175,7 @@ int bitbang_ssi_set(bitbang_ssi_interface *ssi, bitbang_ssi_line line, int level
if (ssi->data_mosi_count == ssi->transfer_size) {
ssi->data_miso = reverse_bits(ssi_transfer(ssi->bus, ssi->data_mosi));
ssi->data_miso_count = ssi->transfer_size;
DPRINTF("transfer(%x) -> %x\n", ssi->data_mosi, ssi->data_miso);
printf("bitbang_ssi: transfer(%x) -> %x\n", ssi->data_mosi, ssi->data_miso);
ssi->state = IDLE;
}
return ssi->last_miso;
Expand Down
5 changes: 0 additions & 5 deletions hw/ssi/litex_ssi.c
Expand Up @@ -63,11 +63,6 @@ static uint64_t litex_ssi_read(void *opaque, hwaddr addr, unsigned size)
printf("Tried to access invalid address %08x\n", (unsigned int)addr);
return 0;
}
switch(addr) {
case R_SPIFLASH_MISO:
printf("Reading MISO %08x\n", (unsigned int)s->regs[addr]);
break;
}

uint64_t r = s->regs[addr];
return r;
Expand Down

0 comments on commit 83113eb

Please sign in to comment.