sandbox: spi: Add more logging

Add logging to aid debugging features in these drivers. Also drop some
code in sandbox_spi_xfer() which is not used.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2018-10-01 11:55:13 -06:00
parent 1c5a81d803
commit c3aed5db59
3 changed files with 39 additions and 54 deletions

View File

@ -8,6 +8,8 @@
* Licensed under the GPL-2 or later. * Licensed under the GPL-2 or later.
*/ */
#define LOG_CATEGORY UCLASS_SPI_FLASH
#include <common.h> #include <common.h>
#include <dm.h> #include <dm.h>
#include <malloc.h> #include <malloc.h>
@ -41,6 +43,7 @@ enum sandbox_sf_state {
SF_WRITE_STATUS, /* write the flash's status register */ SF_WRITE_STATUS, /* write the flash's status register */
}; };
#if CONFIG_IS_ENABLED(LOG)
static const char *sandbox_sf_state_name(enum sandbox_sf_state state) static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
{ {
static const char * const states[] = { static const char * const states[] = {
@ -49,6 +52,7 @@ static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
}; };
return states[state]; return states[state];
} }
#endif /* LOG */
/* Bits for the status register */ /* Bits for the status register */
#define STAT_WIP (1 << 0) #define STAT_WIP (1 << 0)
@ -191,7 +195,7 @@ static void sandbox_sf_cs_activate(struct udevice *dev)
{ {
struct sandbox_spi_flash *sbsf = dev_get_priv(dev); struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
debug("sandbox_sf: CS activated; state is fresh!\n"); log_content("sandbox_sf: CS activated; state is fresh!\n");
/* CS is asserted, so reset state */ /* CS is asserted, so reset state */
sbsf->off = 0; sbsf->off = 0;
@ -203,7 +207,7 @@ static void sandbox_sf_cs_activate(struct udevice *dev)
static void sandbox_sf_cs_deactivate(struct udevice *dev) static void sandbox_sf_cs_deactivate(struct udevice *dev)
{ {
debug("sandbox_sf: CS deactivated; cmd done processing!\n"); log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
} }
/* /*
@ -279,7 +283,7 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
} }
if (oldstate != sbsf->state) if (oldstate != sbsf->state)
debug(" cmd: transition to %s state\n", log_content(" cmd: transition to %s state\n",
sandbox_sf_state_name(sbsf->state)); sandbox_sf_state_name(sbsf->state));
return 0; return 0;
@ -311,7 +315,7 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
int bytes = bitlen / 8; int bytes = bitlen / 8;
int ret; int ret;
debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state, log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
sandbox_sf_state_name(sbsf->state), bytes); sandbox_sf_state_name(sbsf->state), bytes);
if ((flags & SPI_XFER_BEGIN)) if ((flags & SPI_XFER_BEGIN))
@ -331,7 +335,7 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
case SF_ID: { case SF_ID: {
u8 id; u8 id;
debug(" id: off:%u tx:", sbsf->off); log_content(" id: off:%u tx:", sbsf->off);
if (sbsf->off < IDCODE_LEN) { if (sbsf->off < IDCODE_LEN) {
/* Extract correct byte from ID 0x00aabbcc */ /* Extract correct byte from ID 0x00aabbcc */
id = ((JEDEC_MFR(sbsf->data) << 16) | id = ((JEDEC_MFR(sbsf->data) << 16) |
@ -340,18 +344,18 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
} else { } else {
id = 0; id = 0;
} }
debug("%d %02x\n", sbsf->off, id); log_content("%d %02x\n", sbsf->off, id);
tx[pos++] = id; tx[pos++] = id;
++sbsf->off; ++sbsf->off;
break; break;
} }
case SF_ADDR: case SF_ADDR:
debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes, log_content(" addr: bytes:%u rx:%02x ",
rx[pos]); sbsf->addr_bytes, rx[pos]);
if (sbsf->addr_bytes++ < SF_ADDR_LEN) if (sbsf->addr_bytes++ < SF_ADDR_LEN)
sbsf->off = (sbsf->off << 8) | rx[pos]; sbsf->off = (sbsf->off << 8) | rx[pos];
debug("addr:%06x\n", sbsf->off); log_content("addr:%06x\n", sbsf->off);
if (tx) if (tx)
sandbox_spi_tristate(&tx[pos], 1); sandbox_spi_tristate(&tx[pos], 1);
@ -380,7 +384,7 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
sbsf->state = SF_ERASE; sbsf->state = SF_ERASE;
goto case_sf_erase; goto case_sf_erase;
} }
debug(" cmd: transition to %s state\n", log_content(" cmd: transition to %s state\n",
sandbox_sf_state_name(sbsf->state)); sandbox_sf_state_name(sbsf->state));
break; break;
case SF_READ: case SF_READ:
@ -390,7 +394,7 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
*/ */
cnt = bytes - pos; cnt = bytes - pos;
debug(" tx: read(%u)\n", cnt); log_content(" tx: read(%u)\n", cnt);
assert(tx); assert(tx);
ret = os_read(sbsf->fd, tx + pos, cnt); ret = os_read(sbsf->fd, tx + pos, cnt);
if (ret < 0) { if (ret < 0) {
@ -400,19 +404,19 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
pos += ret; pos += ret;
break; break;
case SF_READ_STATUS: case SF_READ_STATUS:
debug(" read status: %#x\n", sbsf->status); log_content(" read status: %#x\n", sbsf->status);
cnt = bytes - pos; cnt = bytes - pos;
memset(tx + pos, sbsf->status, cnt); memset(tx + pos, sbsf->status, cnt);
pos += cnt; pos += cnt;
break; break;
case SF_READ_STATUS1: case SF_READ_STATUS1:
debug(" read status: %#x\n", sbsf->status); log_content(" read status: %#x\n", sbsf->status);
cnt = bytes - pos; cnt = bytes - pos;
memset(tx + pos, sbsf->status >> 8, cnt); memset(tx + pos, sbsf->status >> 8, cnt);
pos += cnt; pos += cnt;
break; break;
case SF_WRITE_STATUS: case SF_WRITE_STATUS:
debug(" write status: %#x (ignored)\n", rx[pos]); log_content(" write status: %#x (ignored)\n", rx[pos]);
pos = bytes; pos = bytes;
break; break;
case SF_WRITE: case SF_WRITE:
@ -428,7 +432,7 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
} }
cnt = bytes - pos; cnt = bytes - pos;
debug(" rx: write(%u)\n", cnt); log_content(" rx: write(%u)\n", cnt);
if (tx) if (tx)
sandbox_spi_tristate(&tx[pos], cnt); sandbox_spi_tristate(&tx[pos], cnt);
ret = os_write(sbsf->fd, rx + pos, cnt); ret = os_write(sbsf->fd, rx + pos, cnt);
@ -448,15 +452,15 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
/* verify address is aligned */ /* verify address is aligned */
if (sbsf->off & (sbsf->erase_size - 1)) { if (sbsf->off & (sbsf->erase_size - 1)) {
debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n", log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
sbsf->cmd, sbsf->erase_size, sbsf->cmd, sbsf->erase_size,
sbsf->off); sbsf->off);
sbsf->status &= ~STAT_WEL; sbsf->status &= ~STAT_WEL;
goto done; goto done;
} }
debug(" sector erase addr: %u, size: %u\n", sbsf->off, log_content(" sector erase addr: %u, size: %u\n",
sbsf->erase_size); sbsf->off, sbsf->erase_size);
cnt = bytes - pos; cnt = bytes - pos;
if (tx) if (tx)
@ -470,13 +474,13 @@ static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
ret = sandbox_erase_part(sbsf, sbsf->erase_size); ret = sandbox_erase_part(sbsf, sbsf->erase_size);
sbsf->status &= ~STAT_WEL; sbsf->status &= ~STAT_WEL;
if (ret) { if (ret) {
debug("sandbox_sf: Erase failed\n"); log_content("sandbox_sf: Erase failed\n");
goto done; goto done;
} }
goto done; goto done;
} }
default: default:
debug(" ??? no idea what to do ???\n"); log_content(" ??? no idea what to do ???\n");
goto done; goto done;
} }
} }

View File

@ -8,6 +8,8 @@
* Licensed under the GPL-2 or later. * Licensed under the GPL-2 or later.
*/ */
#define LOG_CATEGORY UCLASS_SPI
#include <common.h> #include <common.h>
#include <dm.h> #include <dm.h>
#include <malloc.h> #include <malloc.h>
@ -56,7 +58,6 @@ static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
struct udevice *emul; struct udevice *emul;
uint bytes = bitlen / 8, i; uint bytes = bitlen / 8, i;
int ret; int ret;
u8 *tx = (void *)dout, *rx = din;
uint busnum, cs; uint busnum, cs;
if (bitlen == 0) if (bitlen == 0)
@ -87,37 +88,16 @@ static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
if (ret) if (ret)
return ret; return ret;
/* make sure rx/tx buffers are full so clients can assume */
if (!tx) {
debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
tx = malloc(bytes);
if (!tx) {
debug("sandbox_spi: Out of memory\n");
return -ENOMEM;
}
}
if (!rx) {
debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
rx = malloc(bytes);
if (!rx) {
debug("sandbox_spi: Out of memory\n");
return -ENOMEM;
}
}
ops = spi_emul_get_ops(emul); ops = spi_emul_get_ops(emul);
ret = ops->xfer(emul, bitlen, dout, din, flags); ret = ops->xfer(emul, bitlen, dout, din, flags);
debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:", log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
ret, ret ? "bad" : "good"); ret, ret ? "bad" : "good");
if (din) {
for (i = 0; i < bytes; ++i) for (i = 0; i < bytes; ++i)
debug(" %u:%02x", i, rx[i]); log_content(" %u:%02x", i, ((u8 *)din)[i]);
debug("\n"); }
log_content("\n");
if (tx != dout)
free(tx);
if (rx != din)
free(rx);
return ret; return ret;
} }

View File

@ -46,6 +46,7 @@ enum log_category_t {
LOGC_DM, /* Core driver-model */ LOGC_DM, /* Core driver-model */
LOGC_DT, /* Device-tree */ LOGC_DT, /* Device-tree */
LOGC_EFI, /* EFI implementation */ LOGC_EFI, /* EFI implementation */
LOGC_ALLOC, /* Memory allocation */
LOGC_COUNT, LOGC_COUNT,
LOGC_END, LOGC_END,