Skip to content

Commit 87232ea

Browse files
swilkins-raymarinebroonie
authored andcommitted
spi: microchip-core: add support for word sizes of 1 to 32 bits
The current implementation only supports a word size of 8 bits, which limits the devices it can be used with. Add support for any word size between 1 and 32 bits, as supported by the hardware. Signed-off-by: Steve Wilkins <steve.wilkins@raymarine.com> Signed-off-by: Conor Dooley <conor.dooley@microchip.com> Link: https://patch.msgid.link/20240715-cogwheel-uniquely-0d4ef518b809@wendy Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 9cf71eb commit 87232ea

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

drivers/spi/spi-microchip-core.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct mchp_corespi {
111111
int irq;
112112
int tx_len;
113113
int rx_len;
114-
int pending;
114+
int n_bytes;
115115
};
116116

117117
static inline u32 mchp_corespi_read(struct mchp_corespi *spi, unsigned int reg)
@@ -135,20 +135,23 @@ static inline void mchp_corespi_disable(struct mchp_corespi *spi)
135135

136136
static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi)
137137
{
138-
u8 data;
139-
int fifo_max, i = 0;
138+
while (spi->rx_len >= spi->n_bytes && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)) {
139+
u32 data = mchp_corespi_read(spi, REG_RX_DATA);
140140

141-
fifo_max = min(spi->rx_len, FIFO_DEPTH);
141+
spi->rx_len -= spi->n_bytes;
142142

143-
while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)) {
144-
data = mchp_corespi_read(spi, REG_RX_DATA);
143+
if (!spi->rx_buf)
144+
continue;
145145

146-
if (spi->rx_buf)
147-
*spi->rx_buf++ = data;
148-
i++;
146+
if (spi->n_bytes == 4)
147+
*((u32 *)spi->rx_buf) = data;
148+
else if (spi->n_bytes == 2)
149+
*((u16 *)spi->rx_buf) = data;
150+
else
151+
*spi->rx_buf = data;
152+
153+
spi->rx_buf += spi->n_bytes;
149154
}
150-
spi->rx_len -= i;
151-
spi->pending -= i;
152155
}
153156

154157
static void mchp_corespi_enable_ints(struct mchp_corespi *spi)
@@ -210,20 +213,28 @@ static inline void mchp_corespi_set_xfer_size(struct mchp_corespi *spi, int len)
210213

211214
static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi)
212215
{
213-
u8 byte;
214216
int fifo_max, i = 0;
215217

216-
fifo_max = min(spi->tx_len, FIFO_DEPTH);
218+
fifo_max = DIV_ROUND_UP(min(spi->tx_len, FIFO_DEPTH), spi->n_bytes);
217219
mchp_corespi_set_xfer_size(spi, fifo_max);
218220

219221
while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_TXFIFO_FULL)) {
220-
byte = spi->tx_buf ? *spi->tx_buf++ : 0xaa;
221-
mchp_corespi_write(spi, REG_TX_DATA, byte);
222+
u32 word;
223+
224+
if (spi->n_bytes == 4)
225+
word = spi->tx_buf ? *((u32 *)spi->tx_buf) : 0xaa;
226+
else if (spi->n_bytes == 2)
227+
word = spi->tx_buf ? *((u16 *)spi->tx_buf) : 0xaa;
228+
else
229+
word = spi->tx_buf ? *spi->tx_buf : 0xaa;
230+
231+
mchp_corespi_write(spi, REG_TX_DATA, word);
232+
if (spi->tx_buf)
233+
spi->tx_buf += spi->n_bytes;
222234
i++;
223235
}
224236

225-
spi->tx_len -= i;
226-
spi->pending += i;
237+
spi->tx_len -= i * spi->n_bytes;
227238
}
228239

229240
static inline void mchp_corespi_set_framesize(struct mchp_corespi *spi, int bt)
@@ -493,10 +504,9 @@ static int mchp_corespi_transfer_one(struct spi_controller *host,
493504
spi->rx_buf = xfer->rx_buf;
494505
spi->tx_len = xfer->len;
495506
spi->rx_len = xfer->len;
496-
spi->pending = 0;
507+
spi->n_bytes = roundup_pow_of_two(DIV_ROUND_UP(xfer->bits_per_word, BITS_PER_BYTE));
497508

498-
mchp_corespi_set_xfer_size(spi, (spi->tx_len > FIFO_DEPTH)
499-
? FIFO_DEPTH : spi->tx_len);
509+
mchp_corespi_set_framesize(spi, xfer->bits_per_word);
500510

501511
mchp_corespi_write(spi, REG_COMMAND, COMMAND_RXFIFORST | COMMAND_TXFIFORST);
502512

@@ -514,7 +524,6 @@ static int mchp_corespi_prepare_message(struct spi_controller *host,
514524
struct spi_device *spi_dev = msg->spi;
515525
struct mchp_corespi *spi = spi_controller_get_devdata(host);
516526

517-
mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE);
518527
mchp_corespi_set_mode(spi, spi_dev->mode);
519528

520529
return 0;
@@ -542,7 +551,7 @@ static int mchp_corespi_probe(struct platform_device *pdev)
542551
host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
543552
host->use_gpio_descriptors = true;
544553
host->setup = mchp_corespi_setup;
545-
host->bits_per_word_mask = SPI_BPW_MASK(8);
554+
host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
546555
host->transfer_one = mchp_corespi_transfer_one;
547556
host->prepare_message = mchp_corespi_prepare_message;
548557
host->set_cs = mchp_corespi_set_cs;

0 commit comments

Comments
 (0)