Commit de6efe0a authored by Feng Tang's avatar Feng Tang Committed by Grant Likely

spi/dw_spi: unify the low level read/write routines

The original version has many duplicated codes for null/u8/u16 case,
so unify them to make it cleaner
Signed-off-by: default avatarFeng Tang <feng.tang@intel.com>
Signed-off-by: default avatarGrant Likely <grant.likely@secretlab.ca>
parent 0ce790e7
...@@ -58,8 +58,6 @@ struct chip_data { ...@@ -58,8 +58,6 @@ struct chip_data {
u8 bits_per_word; u8 bits_per_word;
u16 clk_div; /* baud rate divider */ u16 clk_div; /* baud rate divider */
u32 speed_hz; /* baud rate */ u32 speed_hz; /* baud rate */
int (*write)(struct dw_spi *dws);
int (*read)(struct dw_spi *dws);
void (*cs_control)(u32 command); void (*cs_control)(u32 command);
}; };
...@@ -185,80 +183,45 @@ static void flush(struct dw_spi *dws) ...@@ -185,80 +183,45 @@ static void flush(struct dw_spi *dws)
wait_till_not_busy(dws); wait_till_not_busy(dws);
} }
static int null_writer(struct dw_spi *dws)
{
u8 n_bytes = dws->n_bytes;
if (!(dw_readw(dws, sr) & SR_TF_NOT_FULL)
|| (dws->tx == dws->tx_end))
return 0;
dw_writew(dws, dr, 0);
dws->tx += n_bytes;
wait_till_not_busy(dws); static int dw_writer(struct dw_spi *dws)
return 1;
}
static int null_reader(struct dw_spi *dws)
{ {
u8 n_bytes = dws->n_bytes; u16 txw = 0;
while ((dw_readw(dws, sr) & SR_RF_NOT_EMPT)
&& (dws->rx < dws->rx_end)) {
dw_readw(dws, dr);
dws->rx += n_bytes;
}
wait_till_not_busy(dws);
return dws->rx == dws->rx_end;
}
static int u8_writer(struct dw_spi *dws)
{
if (!(dw_readw(dws, sr) & SR_TF_NOT_FULL) if (!(dw_readw(dws, sr) & SR_TF_NOT_FULL)
|| (dws->tx == dws->tx_end)) || (dws->tx == dws->tx_end))
return 0; return 0;
dw_writew(dws, dr, *(u8 *)(dws->tx)); /* Set the tx word if the transfer's original "tx" is not null */
++dws->tx; if (dws->tx_end - dws->len) {
if (dws->n_bytes == 1)
wait_till_not_busy(dws); txw = *(u8 *)(dws->tx);
return 1; else
} txw = *(u16 *)(dws->tx);
static int u8_reader(struct dw_spi *dws)
{
while ((dw_readw(dws, sr) & SR_RF_NOT_EMPT)
&& (dws->rx < dws->rx_end)) {
*(u8 *)(dws->rx) = dw_readw(dws, dr);
++dws->rx;
} }
wait_till_not_busy(dws); dw_writew(dws, dr, txw);
return dws->rx == dws->rx_end; dws->tx += dws->n_bytes;
}
static int u16_writer(struct dw_spi *dws)
{
if (!(dw_readw(dws, sr) & SR_TF_NOT_FULL)
|| (dws->tx == dws->tx_end))
return 0;
dw_writew(dws, dr, *(u16 *)(dws->tx));
dws->tx += 2;
wait_till_not_busy(dws); wait_till_not_busy(dws);
return 1; return 1;
} }
static int u16_reader(struct dw_spi *dws) static int dw_reader(struct dw_spi *dws)
{ {
u16 temp; u16 rxw;
while ((dw_readw(dws, sr) & SR_RF_NOT_EMPT) while ((dw_readw(dws, sr) & SR_RF_NOT_EMPT)
&& (dws->rx < dws->rx_end)) { && (dws->rx < dws->rx_end)) {
temp = dw_readw(dws, dr); rxw = dw_readw(dws, dr);
*(u16 *)(dws->rx) = temp; /* Care rx only if the transfer's original "rx" is not null */
dws->rx += 2; if (dws->rx_end - dws->len) {
if (dws->n_bytes == 1)
*(u8 *)(dws->rx) = rxw;
else
*(u16 *)(dws->rx) = rxw;
}
dws->rx += dws->n_bytes;
} }
wait_till_not_busy(dws); wait_till_not_busy(dws);
...@@ -383,8 +346,8 @@ static irqreturn_t interrupt_transfer(struct dw_spi *dws) ...@@ -383,8 +346,8 @@ static irqreturn_t interrupt_transfer(struct dw_spi *dws)
left = (left > int_level) ? int_level : left; left = (left > int_level) ? int_level : left;
while (left--) while (left--)
dws->write(dws); dw_writer(dws);
dws->read(dws); dw_reader(dws);
/* Re-enable the IRQ if there is still data left to tx */ /* Re-enable the IRQ if there is still data left to tx */
if (dws->tx_end > dws->tx) if (dws->tx_end > dws->tx)
...@@ -417,13 +380,13 @@ static irqreturn_t dw_spi_irq(int irq, void *dev_id) ...@@ -417,13 +380,13 @@ static irqreturn_t dw_spi_irq(int irq, void *dev_id)
/* Must be called inside pump_transfers() */ /* Must be called inside pump_transfers() */
static void poll_transfer(struct dw_spi *dws) static void poll_transfer(struct dw_spi *dws)
{ {
while (dws->write(dws)) while (dw_writer(dws))
dws->read(dws); dw_reader(dws);
/* /*
* There is a possibility that the last word of a transaction * There is a possibility that the last word of a transaction
* will be lost if data is not ready. Re-read to solve this issue. * will be lost if data is not ready. Re-read to solve this issue.
*/ */
dws->read(dws); dw_reader(dws);
dw_spi_xfer_done(dws); dw_spi_xfer_done(dws);
} }
...@@ -483,8 +446,6 @@ static void pump_transfers(unsigned long data) ...@@ -483,8 +446,6 @@ static void pump_transfers(unsigned long data)
dws->tx_end = dws->tx + transfer->len; dws->tx_end = dws->tx + transfer->len;
dws->rx = transfer->rx_buf; dws->rx = transfer->rx_buf;
dws->rx_end = dws->rx + transfer->len; dws->rx_end = dws->rx + transfer->len;
dws->write = dws->tx ? chip->write : null_writer;
dws->read = dws->rx ? chip->read : null_reader;
dws->cs_change = transfer->cs_change; dws->cs_change = transfer->cs_change;
dws->len = dws->cur_transfer->len; dws->len = dws->cur_transfer->len;
if (chip != dws->prev_chip) if (chip != dws->prev_chip)
...@@ -520,18 +481,10 @@ static void pump_transfers(unsigned long data) ...@@ -520,18 +481,10 @@ static void pump_transfers(unsigned long data)
case 8: case 8:
dws->n_bytes = 1; dws->n_bytes = 1;
dws->dma_width = 1; dws->dma_width = 1;
dws->read = (dws->read != null_reader) ?
u8_reader : null_reader;
dws->write = (dws->write != null_writer) ?
u8_writer : null_writer;
break; break;
case 16: case 16:
dws->n_bytes = 2; dws->n_bytes = 2;
dws->dma_width = 2; dws->dma_width = 2;
dws->read = (dws->read != null_reader) ?
u16_reader : null_reader;
dws->write = (dws->write != null_writer) ?
u16_writer : null_writer;
break; break;
default: default:
printk(KERN_ERR "MRST SPI0: unsupported bits:" printk(KERN_ERR "MRST SPI0: unsupported bits:"
...@@ -733,13 +686,9 @@ static int dw_spi_setup(struct spi_device *spi) ...@@ -733,13 +686,9 @@ static int dw_spi_setup(struct spi_device *spi)
if (spi->bits_per_word <= 8) { if (spi->bits_per_word <= 8) {
chip->n_bytes = 1; chip->n_bytes = 1;
chip->dma_width = 1; chip->dma_width = 1;
chip->read = u8_reader;
chip->write = u8_writer;
} else if (spi->bits_per_word <= 16) { } else if (spi->bits_per_word <= 16) {
chip->n_bytes = 2; chip->n_bytes = 2;
chip->dma_width = 2; chip->dma_width = 2;
chip->read = u16_reader;
chip->write = u16_writer;
} else { } else {
/* Never take >16b case for MRST SPIC */ /* Never take >16b case for MRST SPIC */
dev_err(&spi->dev, "invalid wordsize\n"); dev_err(&spi->dev, "invalid wordsize\n");
......
...@@ -137,8 +137,6 @@ struct dw_spi { ...@@ -137,8 +137,6 @@ struct dw_spi {
u8 max_bits_per_word; /* maxim is 16b */ u8 max_bits_per_word; /* maxim is 16b */
u32 dma_width; u32 dma_width;
int cs_change; int cs_change;
int (*write)(struct dw_spi *dws);
int (*read)(struct dw_spi *dws);
irqreturn_t (*transfer_handler)(struct dw_spi *dws); irqreturn_t (*transfer_handler)(struct dw_spi *dws);
void (*cs_control)(u32 command); void (*cs_control)(u32 command);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment