spi: atcspi200: Modernize driver and add spi-mem + data merge support
cover spi: atcspi200: Modernize driver and add spi-mem + data merge support
This series modernizes the Andes ATCSPI200 SPI controller driver and adds two new features: spi-mem framework support (including dual/quad modes) and an optional 4-byte data merge mode for higher throughput on aligned transfers.
Patches 1-4 are cleanups and fixes with no intended functional change to existing behavior:
1. Replace volatile struct register access with readl()/writel() + offset defines, convert bit macros to BIT()/GENMASK()/FIELD_PREP(), modernize DT parsing (dev_read_u32_default, dev_remap_addr), rename struct nds_spi_slave to atcspi200_priv, and fix coding style throughout.
2. Improve clock configuration: add sensible fallbacks for clock source and max frequency, honor spi-max-frequency from DT, use a round-up divider that picks the closest frequency not exceeding the target, and stop clobbering clock settings on every stop().
3. Replace manual busy-wait counters with wait_for_bit_le32() and get_timer() based timeouts so the driver benefits from watchdog handling and time-based (rather than iteration-based) limits.
4. Remove a duplicate atcspi200_hw_stop() call after the transfer loop and simplify the CHUNK_SIZE=1 transfer logic that had accumulated dead variables. Also includes a Fixes: tag for the duplicate-stop issue.
Patches 5-6 add new functionality:
5. Wire up spi_controller_mem_ops so the driver can service the spi-mem framework with proper cmd/addr/dummy/data phase handling and single/dual/quad bus widths. A shared atcspi200_pio_transfer() helper is used by both the spi-mem and legacy xfer paths.
6. Add CONFIG_ATCSPI200_SPI_DATA_MERGE. When enabled and the transfer is 4-byte aligned, the driver uses 32-bit FIFO accesses (one u32 per register access instead of one byte) for a meaningful throughput improvement. Data merge is enabled/disabled dynamically per transfer based on alignment.
Patches 7-8 are consumers of the new support:
7. Add quad-mode (1-4-4) read and page-program fixups for Macronix MX25U1635E and MX25U25635F, which do not advertise their quad capabilities correctly via SFDP.
8. Enable CONFIG_ATCSPI200_SPI_DATA_MERGE in all AE350 defconfig variants.
Tested on AE350 with MX25U1635E and MX25U25635F flash in 1-1-1, 1-1-4, and 1-4-4 modes, with and without data merge.
Leo Yu-Chi Liang (8):
spi: atcspi200: Clean up register access, macros, naming, DT parsing,
and style
spi: atcspi200: Improve clock configuration and divider logic
spi: atcspi200: Use proper timeout handling for busy-wait loops
spi: atcspi200: Fix double stop call and simplify transfer loop
spi: atcspi200: Add spi-mem framework support
spi: atcspi200: Add data merge mode support
mtd: spi-nor: Add Macronix MX25U quad-mode fixups
configs: ae350: Enable ATCSPI200 data merge mode
configs/ae350_rv32_defconfig | 1 + configs/ae350_rv32_falcon_defconfig | 1 + configs/ae350_rv32_falcon_xip_defconfig | 1 + configs/ae350_rv32_spl_defconfig | 1 + configs/ae350_rv32_spl_xip_defconfig | 1 + configs/ae350_rv32_xip_defconfig | 1 + configs/ae350_rv64_defconfig | 1 + configs/ae350_rv64_falcon_defconfig | 1 + configs/ae350_rv64_falcon_xip_defconfig | 1 + configs/ae350_rv64_spl_defconfig | 1 + configs/ae350_rv64_spl_xip_defconfig | 1 + configs/ae350_rv64_xip_defconfig | 1 + drivers/mtd/spi/spi-nor-core.c | 25 +- drivers/spi/Kconfig | 9 + drivers/spi/atcspi200_spi.c | 658 ++++++++++++++---------- 15 files changed, 436 insertions(+), 268 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/8 spi: atcspi200: Clean up register access, macros, naming, DT parsing, and style
Modernize the ATCSPI200 SPI controller driver:
- Replace volatile struct pointer register access with readl()/writel() using register offset defines and inline wrappers
- Convert bit field macros from raw shifts to BIT()/GENMASK()/FIELD_PREP()
- Remove unused macros (SPI0_BUS, SPI1_BUS, SPI0_BASE, SPI1_BASE, NSPI_MAX_CS_NUM) and unused includes (malloc.h, asm/global_data.h)
- Remove empty __atcspi200_spi_release_bus() function
- Modernize DT parsing: replace fdtdec_get_int() with dev_read_u32_default(), map_physmem() with dev_remap_addr()
- Rename struct nds_spi_slave to atcspi200_priv, ns to priv
- Remove __ prefix from static helpers, rename for consistency
- Fix typo: atcspi200_ofdata_to_platadata -> atcspi200_spi_of_to_plat
- Fix coding style: spacing, braces, indentation throughout
No functional change.
--- drivers/spi/atcspi200_spi.c | 471 ++++++++++++++++++------------------ 1 file changed, 234 insertions(+), 237 deletions(-)
drivers/spi/atcspi200_spi.c +234 -237
@@ -7,75 +7,63 @@*/ #include <clk.h> +#include <dm.h> +#include <dm/device_compat.h> #include <log.h> -#include <malloc.h> #include <spi.h> -#include <asm/global_data.h> #include <asm/io.h> -#include <dm.h> - -DECLARE_GLOBAL_DATA_PTR ; +#include <linux/bitfield.h> +#include <linux/bitops.h> #define MAX_TRANSFER_LEN 512#define CHUNK_SIZE 1#define SPI_TIMEOUT 0x100000 -#define SPI0_BUS 0 -#define SPI1_BUS 1 -#define SPI0_BASE 0xf0b00000 -#define SPI1_BASE 0xf0f00000 -#define NSPI_MAX_CS_NUM 1 - -struct atcspi200_spi_regs { -u32 rev; -u32 reserve1[3 ]; -u32 format;/* 0x10 */ -#define DATA_LENGTH (x) ((x-1)<<8) -u32 pio; -u32 reserve2[2 ]; -u32 tctrl;/* 0x20 */ -#define TRAMODE_OFFSET 24 -#define TRAMODE_MASK (0x0F<<TRAMODE_OFFSET) -#define TRAMODE_WR_SYNC (0<<TRAMODE_OFFSET) -#define TRAMODE_WO (1<<TRAMODE_OFFSET) -#define TRAMODE_RO (2<<TRAMODE_OFFSET) -#define TRAMODE_WR (3<<TRAMODE_OFFSET) -#define TRAMODE_RW (4<<TRAMODE_OFFSET) -#define TRAMODE_WDR (5<<TRAMODE_OFFSET) -#define TRAMODE_RDW (6<<TRAMODE_OFFSET) -#define TRAMODE_NONE (7<<TRAMODE_OFFSET) -#define TRAMODE_DW (8<<TRAMODE_OFFSET) -#define TRAMODE_DR (9<<TRAMODE_OFFSET) -#define WCNT_OFFSET 12 -#define WCNT_MASK (0x1FF<<WCNT_OFFSET) -#define RCNT_OFFSET 0 -#define RCNT_MASK (0x1FF<<RCNT_OFFSET) -u32 cmd; -u32 addr; -u32 data; -u32 ctrl;/* 0x30 */ -#define TXFTH_OFFSET 16 -#define RXFTH_OFFSET 8 -#define TXDMAEN (1<<4) -#define RXDMAEN (1<<3) -#define TXFRST (1<<2) -#define RXFRST (1<<1) -#define SPIRST (1<<0) -u32 status; -#define TXFFL (1<<23) -#define TXEPTY (1<<22) -#define TXFVE_MASK (0x1F<<16) -#define RXFEM (1<<14) -#define RXFVE_OFFSET (8) -#define RXFVE_MASK (0x1F<<RXFVE_OFFSET) -#define SPIBSY (1<<0) -u32 inten; -u32 intsta; -u32 timing;/* 0x40 */ -#define SCLK_DIV_MASK 0xFF -}; -struct nds_spi_slave { -volatile struct atcspi200_spi_regs *regs; +/* Register offsets */ +#define ATCSPI200_REG_FORMAT 0x10 +#define ATCSPI200_REG_TCTRL 0x20 +#define ATCSPI200_REG_CMD 0x24 +#define ATCSPI200_REG_ADDR 0x28 +#define ATCSPI200_REG_DATA 0x2c +#define ATCSPI200_REG_CTRL 0x30 +#define ATCSPI200_REG_STATUS 0x34 +#define ATCSPI200_REG_TIMING 0x40 + +/* FORMAT register fields */ +#define DATA_LEN_MASK GENMASK(12, 8) + +/* TCTRL register fields */ +#define TRAMODE_MASK GENMASK(27, 24) +#define TRAMODE_WR_SYNC FIELD_PREP(TRAMODE_MASK, 0) +#define TRAMODE_WO FIELD_PREP(TRAMODE_MASK, 1) +#define TRAMODE_RO FIELD_PREP(TRAMODE_MASK, 2) +#define TRAMODE_WR FIELD_PREP(TRAMODE_MASK, 3) +#define TRAMODE_RW FIELD_PREP(TRAMODE_MASK, 4) +#define TRAMODE_WDR FIELD_PREP(TRAMODE_MASK, 5) +#define TRAMODE_RDW FIELD_PREP(TRAMODE_MASK, 6) +#define TRAMODE_NONE FIELD_PREP(TRAMODE_MASK, 7) +#define TRAMODE_DW FIELD_PREP(TRAMODE_MASK, 8) +#define TRAMODE_DR FIELD_PREP(TRAMODE_MASK, 9) +#define WCNT_MASK GENMASK(20, 12) +#define RCNT_MASK GENMASK(8, 0) + +/* CTRL register fields */ +#define TXFRST BIT(2) +#define RXFRST BIT(1) +#define SPIRST BIT(0) + +/* STATUS register fields */ +#define TXFFL BIT(23) +#define TXEPTY BIT(22) +#define TXFVE_MASK GENMASK(20, 16) +#define RXFVE_MASK GENMASK(12, 8) +#define SPIBSY BIT(0) + +/* TIMING register fields */ +#define SCLK_DIV_MASK GENMASK(7, 0) + +struct atcspi200_priv { +void *regs;int to;unsigned int freq;ulong clock;
@@ -88,265 +76,278 @@ struct nds_spi_slave {size_t tran_len;u8 *din;u8 *dout; -unsigned int max_transfer_length; +unsigned int max_transfer_length; }; -static int __atcspi200_spi_set_speed (struct nds_spi_slave *ns) +static inline u32 atcspi200_read (struct atcspi200_priv *priv,u32 offset) +{ +return readl (priv->regs + offset); +} + +static inline void atcspi200_write (struct atcspi200_priv *priv,u32 offset, +u32 val) +{ +writel (val, priv->regs + offset); +} + +static int atcspi200_hw_set_speed (struct atcspi200_priv *priv) {u32 tm;u8 div; - tm = ns->regs->timing; + + tm =atcspi200_read (priv,ATCSPI200_REG_TIMING ); tm &= ~SCLK_DIV_MASK ; -if (ns->freq >= ns->clock) - div =0xff ; -else { +if (priv->freq >= priv->clock) { + div =0xff ; + }else {for (div =0 ; div <0xff ; div++) { -if (ns->freq >= ns->clock / (2 * (div +1 ))) +if (priv->freq >= priv->clock / (2 * (div +1 )))break ; } } tm |= div; - ns->regs->timing = tm; +atcspi200_write (priv,ATCSPI200_REG_TIMING , tm);return 0 ; - } -static int __atcspi200_spi_claim_bus (struct nds_spi_slave *ns) +static int atcspi200_hw_claim_bus (struct atcspi200_priv *priv) { -unsigned int format=0 ; - ns->regs->ctrl |= (TXFRST |RXFRST |SPIRST ); -while ((ns->regs->ctrl &(TXFRST |RXFRST |SPIRST ))&&(ns->to--)) -if (!ns->to) -return -EINVAL ; - - ns->cmd_len =0 ; - format = ns->mode|DATA_LENGTH (8 ); - ns->regs->format = format; -__atcspi200_spi_set_speed (ns); +unsigned int format; + +atcspi200_write (priv,ATCSPI200_REG_CTRL , +atcspi200_read (priv,ATCSPI200_REG_CTRL ) | +TXFRST |RXFRST |SPIRST ); +while ((atcspi200_read (priv,ATCSPI200_REG_CTRL ) & + (TXFRST |RXFRST |SPIRST )) && priv->to--) +if (!priv->to) +return -EINVAL ; -return 0 ; -} + priv->cmd_len =0 ; + format = priv->mode |FIELD_PREP (DATA_LEN_MASK ,8 -1 ); +atcspi200_write (priv,ATCSPI200_REG_FORMAT , format); +atcspi200_hw_set_speed (priv); -static int __atcspi200_spi_release_bus (struct nds_spi_slave *ns) -{ -/* do nothing */ return 0 ; } -static int __atcspi200_spi_start (struct nds_spi_slave *ns) +static int atcspi200_hw_start (struct atcspi200_priv *priv) { -int i,olen=0 ; -int tc = ns->regs->tctrl; +int i, olen =0 ; +u32 tc; - tc &= ~(WCNT_MASK |RCNT_MASK |TRAMODE_MASK ); -if ((ns->din)&&(ns->cmd_len)) + tc =atcspi200_read (priv,ATCSPI200_REG_TCTRL ); + tc &= ~(WCNT_MASK |RCNT_MASK |TRAMODE_MASK ); + +if (priv->din && priv->cmd_len) tc |=TRAMODE_WR ; -else if (ns->din) +else if (priv->din) tc |=TRAMODE_RO ;else tc |=TRAMODE_WO ; -if (ns->dout) - olen = ns->tran_len; - tc |= (ns->cmd_len+olen-1 ) <<WCNT_OFFSET ; +if (priv->dout) + olen = priv->tran_len; + tc |=FIELD_PREP (WCNT_MASK , priv->cmd_len + olen -1 ); -if (ns->din) - tc |= (ns->tran_len-1 ) <<RCNT_OFFSET ; +if (priv->din) + tc |=FIELD_PREP (RCNT_MASK , priv->tran_len -1 ); - ns->regs->tctrl = tc; - ns->regs->cmd =1 ; +atcspi200_write (priv,ATCSPI200_REG_TCTRL , tc); +atcspi200_write (priv,ATCSPI200_REG_CMD ,1 ); -for (i=0 ;i<ns->cmd_len;i++) - ns->regs->data = ns->cmd_buf[i]; +for (i =0 ; i < priv->cmd_len; i++) +atcspi200_write (priv,ATCSPI200_REG_DATA , priv->cmd_buf[i]);return 0 ; } -static int __atcspi200_spi_stop (struct nds_spi_slave *ns) +static int atcspi200_hw_stop (struct atcspi200_priv *priv) { - ns->regs->timing = ns->mtiming; -while ((ns->regs->status &SPIBSY )&&(ns->to--)) -if (!ns->to) +atcspi200_write (priv,ATCSPI200_REG_TIMING , priv->mtiming); +while ((atcspi200_read (priv,ATCSPI200_REG_STATUS ) &SPIBSY ) && + priv->to--) +if (!priv->to)return -EINVAL ;return 0 ; } -static void __nspi_espi_tx (struct nds_spi_slave *ns,const void *dout) +static void atcspi200_tx_byte (struct atcspi200_priv *priv,const void *dout) { - ns->regs->data = *(u8 *)dout; +atcspi200_write (priv,ATCSPI200_REG_DATA , *(u8 *)dout); } -static int __nspi_espi_rx (struct nds_spi_slave *ns,void *din,unsigned int bytes) +static int atcspi200_rx_byte (struct atcspi200_priv *priv,void *din, +unsigned int bytes) { - *(u8 *)din = ns->regs->data; + *(u8 *)din = (u8 )atcspi200_read (priv,ATCSPI200_REG_DATA );return bytes; } -static int __atcspi200_spi_xfer (struct nds_spi_slave *ns, -unsigned int bitlen,const void *data_out,void *data_in, -unsigned long flags) +static int atcspi200_hw_xfer (struct atcspi200_priv *priv, +unsigned int bitlen,const void *data_out, +void *data_in,unsigned long flags) { -unsigned int event, rx_bytes; -const void *dout =NULL ; -void *din =NULL ; -int num_blks, num_chunks, max_tran_len, tran_len; -int num_bytes; -u8 *cmd_buf = ns->cmd_buf; -size_t cmd_len = ns->cmd_len; -unsigned long data_len = bitlen /8 ; -int rf_cnt; -int ret =0 , timeout =0 ; - - max_tran_len = ns->max_transfer_length; -switch (flags) { -case SPI_XFER_BEGIN : - cmd_len = ns->cmd_len = data_len; -memcpy (cmd_buf, data_out, cmd_len); +unsigned int event, rx_bytes; +const void *dout =NULL ; +void *din =NULL ; +int num_blks, num_chunks, max_tran_len, tran_len; +int num_bytes; +u8 *cmd_buf = priv->cmd_buf; +size_t cmd_len = priv->cmd_len; +unsigned long data_len = bitlen /8 ; +int rf_cnt; +int ret =0 , timeout =0 ; + + max_tran_len = priv->max_transfer_length; +switch (flags) { +case SPI_XFER_BEGIN : + cmd_len = priv->cmd_len = data_len; +memcpy (cmd_buf, data_out, cmd_len); +return 0 ; + +case 0 : +case SPI_XFER_END : +if (bitlen ==0 )return 0 ; + priv->data_len = data_len; + priv->din = (u8 *)data_in; + priv->dout = (u8 *)data_out; +break ; + +case SPI_XFER_BEGIN |SPI_XFER_END : + priv->data_len =0 ; + priv->din =0 ; + priv->dout =0 ; + cmd_len = priv->cmd_len = data_len; +memcpy (cmd_buf, data_out, cmd_len); + data_out =0 ; + data_len =0 ; +atcspi200_hw_start (priv); +break ; + } -case 0 : -case SPI_XFER_END : -if (bitlen ==0 ) { -return 0 ; +if (data_out) +debug ("spi_xfer: data_out %08X(%p) data_in %08X(%p) data_len %lu\n" , + *(uint *)data_out, data_out, *(uint *)data_in, + data_in, data_len); + + num_chunks =DIV_ROUND_UP (data_len, max_tran_len); + din = data_in; + dout = data_out; +while (num_chunks--) { + tran_len =min ((size_t )data_len, (size_t )max_tran_len); + priv->tran_len = tran_len; + num_blks =DIV_ROUND_UP (tran_len,CHUNK_SIZE ); + num_bytes = tran_len %CHUNK_SIZE ; + timeout =SPI_TIMEOUT ; +if (num_bytes ==0 ) + num_bytes =CHUNK_SIZE ; +atcspi200_hw_start (priv); + +while (num_blks && timeout--) { + event =atcspi200_read (priv,ATCSPI200_REG_STATUS ); + +if ((event &TXEPTY ) && data_out) { +atcspi200_tx_byte (priv, dout); + num_blks -=CHUNK_SIZE ; + dout +=CHUNK_SIZE ; } - ns->data_len = data_len; - ns->din = (u8 *)data_in; - ns->dout = (u8 *)data_out; -break ; - -case SPI_XFER_BEGIN |SPI_XFER_END : - ns->data_len =0 ; - ns->din =0 ; - ns->dout =0 ; - cmd_len = ns->cmd_len = data_len; -memcpy (cmd_buf, data_out, cmd_len); - data_out =0 ; - data_len =0 ; -__atcspi200_spi_start (ns); -break ; - } -if (data_out) -debug ("spi_xfer: data_out %08X(%p) data_in %08X(%p) data_len %lu\n" , - *(uint *)data_out, data_out, *(uint *)data_in, - data_in, data_len); - num_chunks =DIV_ROUND_UP (data_len, max_tran_len); - din = data_in; - dout = data_out; -while (num_chunks--) { - tran_len =min ((size_t )data_len, (size_t )max_tran_len); - ns->tran_len = tran_len; - num_blks =DIV_ROUND_UP (tran_len ,CHUNK_SIZE ); - num_bytes = (tran_len) %CHUNK_SIZE ; - timeout =SPI_TIMEOUT ; -if (num_bytes ==0 ) - num_bytes =CHUNK_SIZE ; -__atcspi200_spi_start (ns); - -while (num_blks && (timeout--)) { - event =in_le32 (&ns->regs->status); -if ((event &TXEPTY ) && (data_out)) { -__nspi_espi_tx (ns, dout); - num_blks -=CHUNK_SIZE ; - dout +=CHUNK_SIZE ; - } - -if ((event &RXFVE_MASK ) && (data_in)) { - rf_cnt = ((event &RXFVE_MASK )>>RXFVE_OFFSET ); -if (rf_cnt >=CHUNK_SIZE ) - rx_bytes =CHUNK_SIZE ; -else if (num_blks ==1 && rf_cnt == num_bytes) - rx_bytes = num_bytes; -else -continue ; - -if (__nspi_espi_rx (ns, din, rx_bytes) == rx_bytes) { - num_blks -=CHUNK_SIZE ; - din = (unsigned char *)din + rx_bytes; - } - } -if (!timeout) { -debug ("spi_xfer: %s() timeout\n" , __func__); -break ; +if ((event &RXFVE_MASK ) && data_in) { + rf_cnt =FIELD_GET (RXFVE_MASK , event); +if (rf_cnt >=CHUNK_SIZE ) + rx_bytes =CHUNK_SIZE ; +else if (num_blks ==1 && + rf_cnt == num_bytes) + rx_bytes = num_bytes; +else +continue ; + +if (atcspi200_rx_byte (priv, din, rx_bytes) == + rx_bytes) { + num_blks -=CHUNK_SIZE ; + din = (unsigned char *)din + rx_bytes; } } - data_len -= tran_len; -if (data_len) - { - ns->cmd_buf[1 ] += ((tran_len>>16 )&0xff ); - ns->cmd_buf[2 ] += ((tran_len>>8 )&0xff ); - ns->cmd_buf[3 ] += ((tran_len)&0xff ); - ns->data_len = data_len; +if (!timeout) { +debug ("spi_xfer: %s() timeout\n" , __func__); +break ; } - ret =__atcspi200_spi_stop (ns); } - ret =__atcspi200_spi_stop (ns); -return ret; + data_len -= tran_len; +if (data_len) { + priv->cmd_buf[1 ] += ((tran_len >>16 ) &0xff ); + priv->cmd_buf[2 ] += ((tran_len >>8 ) &0xff ); + priv->cmd_buf[3 ] += (tran_len &0xff ); + priv->data_len = data_len; + } + ret =atcspi200_hw_stop (priv); + } + ret =atcspi200_hw_stop (priv); + +return ret; }static int atcspi200_spi_set_speed (struct udevice *bus,uint max_hz) { -struct nds_spi_slave *ns =dev_get_priv (bus); +struct atcspi200_priv *priv =dev_get_priv (bus);debug ("%s speed %u\n" , __func__, max_hz); - ns->freq = max_hz; -__atcspi200_spi_set_speed (ns); + priv->freq = max_hz; +atcspi200_hw_set_speed (priv);return 0 ; }static int atcspi200_spi_set_mode (struct udevice *bus,uint mode) { -struct nds_spi_slave *ns =dev_get_priv (bus); +struct atcspi200_priv *priv =dev_get_priv (bus);debug ("%s mode %u\n" , __func__, mode); - ns->mode = mode; + priv->mode = mode;return 0 ; }static int atcspi200_spi_claim_bus (struct udevice *dev) { -struct dm_spi_slave_plat *slave_plat = -dev_get_parent_plat (dev); +struct dm_spi_slave_plat *slave_plat =dev_get_parent_plat (dev);struct udevice *bus = dev->parent; -struct nds_spi_slave *ns =dev_get_priv (bus); +struct atcspi200_priv *priv =dev_get_priv (bus); -if (slave_plat->cs[0 ] >= ns->num_cs) { -printf ("Invalid SPI chipselect\n" ); +if (slave_plat->cs[0 ] >= priv->num_cs) { +dev_err (dev,"Invalid SPI chipselect\n" );return -EINVAL ; } -return __atcspi200_spi_claim_bus (ns); +return atcspi200_hw_claim_bus (priv); }static int atcspi200_spi_release_bus (struct udevice *dev) { -struct nds_spi_slave *ns =dev_get_priv (dev->parent); - -return __atcspi200_spi_release_bus (ns); +return 0 ; }static int atcspi200_spi_xfer (struct udevice *dev,unsigned int bitlen, -const void *dout,void *din, -unsigned long flags) +const void *dout,void *din, +unsigned long flags) {struct udevice *bus = dev->parent; -struct nds_spi_slave *ns =dev_get_priv (bus); +struct atcspi200_priv *priv =dev_get_priv (bus); -return __atcspi200_spi_xfer (ns, bitlen, dout, din, flags); +return atcspi200_hw_xfer (priv, bitlen, dout, din, flags); }static int atcspi200_spi_get_clk (struct udevice *bus) { -struct nds_spi_slave *ns =dev_get_priv (bus); +struct atcspi200_priv *priv =dev_get_priv (bus);struct clk clk;ulong clk_rate;int ret;
@@ -359,37 +360,33 @@ static int atcspi200_spi_get_clk(struct udevice *bus)if (!clk_rate)return -EINVAL ; - ns->clock = clk_rate; + priv->clock = clk_rate;return 0 ; }static int atcspi200_spi_probe (struct udevice *bus) { -struct nds_spi_slave *ns =dev_get_priv (bus); +struct atcspi200_priv *priv =dev_get_priv (bus); - ns->to =SPI_TIMEOUT ; - ns->max_transfer_length =MAX_TRANSFER_LEN ; - ns->mtiming = ns->regs->timing; + priv->to =SPI_TIMEOUT ; + priv->max_transfer_length =MAX_TRANSFER_LEN ; + priv->mtiming =atcspi200_read (priv,ATCSPI200_REG_TIMING );atcspi200_spi_get_clk (bus);return 0 ; } -static int atcspi200_ofdata_to_platadata (struct udevice *bus) +static int atcspi200_spi_of_to_plat (struct udevice *bus) { -struct nds_spi_slave *ns =dev_get_priv (bus); -const void *blob = gd->fdt_blob; -int node =dev_of_offset (bus); - - ns->regs =map_physmem (dev_read_addr (bus), -sizeof (struct atcspi200_spi_regs ), -MAP_NOCACHE ); -if (!ns->regs) { -printf ("%s: could not map device address\n" , __func__); +struct atcspi200_priv *priv =dev_get_priv (bus); + + priv->regs =dev_remap_addr (bus); +if (!priv->regs) { +dev_err (bus,"could not map device address\n" );return -EINVAL ; } - ns->num_cs =fdtdec_get_int (blob, node,"num-cs" ,4 ); + priv->num_cs =dev_read_u32_default (bus,"num-cs" ,4 );return 0 ; }
@@ -412,7 +409,7 @@ U_BOOT_DRIVER(atcspi200_spi) = { .id = UCLASS_SPI, .of_match = atcspi200_spi_ids, .ops = &atcspi200_spi_ops, - .of_to_plat = atcspi200_ofdata_to_platadata, - .priv_auto = sizeof(struct nds_spi_slave), + .of_to_plat = atcspi200_spi_of_to_plat, + .priv_auto = sizeof(struct atcspi200_priv), .probe = atcspi200_spi_probe, };
Signature · 1 line
2.34.1
PATCH 2/8 spi: atcspi200: Improve clock configuration and divider logic
In reply to Leo Yu-Chi Liang- Add default clock source (100 MHz) and max frequency (40 MHz) fallbacks when DT properties are missing
- Read spi-max-frequency from DT to cap the operating frequency
- Use improved round-up divider formula that selects the closest available frequency not exceeding the target
- Add SCLK_DIV_BYPASS (0xFF) and SCLK_DIV_MAX (0xFE) constants
- Remove mtiming field and timing restore in stop(), which was incorrectly resetting clock settings after every transfer
- Change clock fields from ulong to unsigned int
--- drivers/spi/atcspi200_spi.c | 57 +++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 21 deletions(-)
drivers/spi/atcspi200_spi.c +36 -21
@@ -18,6 +18,8 @@#define MAX_TRANSFER_LEN 512#define CHUNK_SIZE 1#define SPI_TIMEOUT 0x100000 +#define SPI_DEF_SRC_CLK 100000000 +#define SPI_DEF_MAX_CLK 40000000/* Register offsets */ #define ATCSPI200_REG_FORMAT 0x10
@@ -61,15 +63,17 @@/* TIMING register fields */ #define SCLK_DIV_MASK GENMASK(7, 0) +#define SCLK_DIV_BYPASS 0xFF +#define SCLK_DIV_MAX 0xFEstruct atcspi200_priv {void *regs;int to;unsigned int freq; -ulong clock; +unsigned int src_clk; +unsigned int max_clk;unsigned int mode;u8 num_cs; -unsigned int mtiming;size_t cmd_len;u8 cmd_buf[16 ];size_t data_len;
@@ -93,22 +97,28 @@ static inline void atcspi200_write(struct atcspi200_priv *priv, u32 offset,static int atcspi200_hw_set_speed (struct atcspi200_priv *priv) {u32 tm; -u8 div; +u32 div; +u32 spi_sclk; - tm =atcspi200_read (priv,ATCSPI200_REG_TIMING ); - tm &= ~SCLK_DIV_MASK ; + tm =atcspi200_read (priv,ATCSPI200_REG_TIMING ) & ~SCLK_DIV_MASK ; -if (priv->freq >= priv->clock) { - div =0xff ; + spi_sclk =min (priv->freq, priv->max_clk); + +/* +* SCLK_DIV = 0xFF bypasses the divider, giving SCLK = src_clk. +* Otherwise SCLK = src_clk / (2 * (SCLK_DIV + 1)). +*/ +if (spi_sclk >= priv->src_clk) { + div =SCLK_DIV_BYPASS ; }else { -for (div =0 ; div <0xff ; div++) { -if (priv->freq >= priv->clock / (2 * (div +1 ))) -break ; + div =DIV_ROUND_UP (priv->src_clk, spi_sclk *2 ) -1 ; +if (div >SCLK_DIV_MAX ) { +pr_err ("Unsupported SPI clock %u\n" , spi_sclk); +return -EINVAL ; } } - tm |= div; -atcspi200_write (priv,ATCSPI200_REG_TIMING , tm); +atcspi200_write (priv,ATCSPI200_REG_TIMING , tm | div);return 0 ; }
@@ -166,7 +176,6 @@ static int atcspi200_hw_start(struct atcspi200_priv *priv)static int atcspi200_hw_stop (struct atcspi200_priv *priv) { -atcspi200_write (priv,ATCSPI200_REG_TIMING , priv->mtiming);while ((atcspi200_read (priv,ATCSPI200_REG_STATUS ) &SPIBSY ) && priv->to--)if (!priv->to)
@@ -353,14 +362,21 @@ static int atcspi200_spi_get_clk(struct udevice *bus)int ret; ret =clk_get_by_index (bus,0 , &clk); -if (ret) -return -EINVAL ; - - clk_rate =clk_get_rate (&clk); -if (!clk_rate) -return -EINVAL ; +if (ret) { +dev_warn (bus,"no clock node, using default %d Hz\n" , +SPI_DEF_SRC_CLK ); + priv->src_clk =SPI_DEF_SRC_CLK ; + }else { + clk_rate =clk_get_rate (&clk); +if (!clk_rate) { +dev_err (bus,"invalid clock rate\n" ); +return -EINVAL ; + } + priv->src_clk = clk_rate; + } - priv->clock = clk_rate; + priv->max_clk =dev_read_u32_default (bus,"spi-max-frequency" , +SPI_DEF_MAX_CLK );return 0 ; }
@@ -371,7 +387,6 @@ static int atcspi200_spi_probe(struct udevice *bus) priv->to =SPI_TIMEOUT ; priv->max_transfer_length =MAX_TRANSFER_LEN ; - priv->mtiming =atcspi200_read (priv,ATCSPI200_REG_TIMING );atcspi200_spi_get_clk (bus);return 0 ;
Signature · 1 line
2.34.1
PATCH 3/8 spi: atcspi200: Use proper timeout handling for busy-wait loops
In reply to Leo Yu-Chi LiangReplace manual busy-wait counters with standard U-Boot timeout APIs:
- Use wait_for_bit_le32() for FIFO reset wait in claim_bus and SPI busy wait in stop, providing time-based timeouts with watchdog support
- Use get_timer() for the transfer polling loop where TX/RX work is done inside the loop
- Remove the 'to' field from private data (no longer needed)
- Replace iteration-count SPI_TIMEOUT with time-based SPI_TIMEOUT_MS
--- drivers/spi/atcspi200_spi.c | 41 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 21 deletions(-)
drivers/spi/atcspi200_spi.c +20 -21
@@ -11,13 +11,15 @@#include <dm/device_compat.h> #include <log.h> #include <spi.h> +#include <time.h> +#include <wait_bit.h> #include <asm/io.h> #include <linux/bitfield.h> #include <linux/bitops.h> #define MAX_TRANSFER_LEN 512#define CHUNK_SIZE 1 -#define SPI_TIMEOUT 0x100000 +#define SPI_TIMEOUT_MS 1000#define SPI_DEF_SRC_CLK 100000000#define SPI_DEF_MAX_CLK 40000000
@@ -68,7 +70,6 @@struct atcspi200_priv {void *regs; -int to;unsigned int freq;unsigned int src_clk;unsigned int max_clk;
@@ -126,14 +127,16 @@ static int atcspi200_hw_set_speed(struct atcspi200_priv *priv)static int atcspi200_hw_claim_bus (struct atcspi200_priv *priv) {unsigned int format; +int ret;atcspi200_write (priv,ATCSPI200_REG_CTRL ,atcspi200_read (priv,ATCSPI200_REG_CTRL ) |TXFRST |RXFRST |SPIRST ); -while ((atcspi200_read (priv,ATCSPI200_REG_CTRL ) & - (TXFRST |RXFRST |SPIRST )) && priv->to--) -if (!priv->to) -return -EINVAL ; + ret =wait_for_bit_le32 (priv->regs +ATCSPI200_REG_CTRL , +TXFRST |RXFRST |SPIRST , + false,SPI_TIMEOUT_MS , false); +if (ret) +return ret; priv->cmd_len =0 ; format = priv->mode |FIELD_PREP (DATA_LEN_MASK ,8 -1 );
@@ -176,12 +179,8 @@ static int atcspi200_hw_start(struct atcspi200_priv *priv)static int atcspi200_hw_stop (struct atcspi200_priv *priv) { -while ((atcspi200_read (priv,ATCSPI200_REG_STATUS ) &SPIBSY ) && - priv->to--) -if (!priv->to) -return -EINVAL ; - -return 0 ; +return wait_for_bit_le32 (priv->regs +ATCSPI200_REG_STATUS , +SPIBSY , false,SPI_TIMEOUT_MS , false); }static void atcspi200_tx_byte (struct atcspi200_priv *priv,const void *dout)
@@ -209,7 +208,8 @@ static int atcspi200_hw_xfer(struct atcspi200_priv *priv,size_t cmd_len = priv->cmd_len;unsigned long data_len = bitlen /8 ;int rf_cnt; -int ret =0 , timeout =0 ; +int ret =0 ; +unsigned long start; max_tran_len = priv->max_transfer_length;switch (flags) {
@@ -252,12 +252,17 @@ static int atcspi200_hw_xfer(struct atcspi200_priv *priv, priv->tran_len = tran_len; num_blks =DIV_ROUND_UP (tran_len,CHUNK_SIZE ); num_bytes = tran_len %CHUNK_SIZE ; - timeout =SPI_TIMEOUT ;if (num_bytes ==0 ) num_bytes =CHUNK_SIZE ; + start =get_timer (0 );atcspi200_hw_start (priv); -while (num_blks && timeout--) { +while (num_blks) { +if (get_timer (start) >SPI_TIMEOUT_MS ) { +debug ("spi_xfer: %s() timeout\n" , __func__); +break ; + } + event =atcspi200_read (priv,ATCSPI200_REG_STATUS );if ((event &TXEPTY ) && data_out) {
@@ -282,11 +287,6 @@ static int atcspi200_hw_xfer(struct atcspi200_priv *priv, din = (unsigned char *)din + rx_bytes; } } - -if (!timeout) { -debug ("spi_xfer: %s() timeout\n" , __func__); -break ; - } } data_len -= tran_len;
@@ -385,7 +385,6 @@ static int atcspi200_spi_probe(struct udevice *bus) {struct atcspi200_priv *priv =dev_get_priv (bus); - priv->to =SPI_TIMEOUT ; priv->max_transfer_length =MAX_TRANSFER_LEN ;atcspi200_spi_get_clk (bus);
Signature · 1 line
2.34.1
PATCH 4/8 spi: atcspi200: Fix double stop call and simplify transfer loop
In reply to Leo Yu-Chi Liang- Remove duplicate atcspi200_hw_stop() call after the while loop; the stop inside the loop already handles the final chunk
- Simplify the CHUNK_SIZE=1 transfer logic: since CHUNK_SIZE was always 1, remove the constant and inline its value, eliminating dead num_bytes/rf_cnt/rx_bytes variables
- Simplify atcspi200_rx_byte() to void (return value was unused)
- Add clarifying comment on the multi-chunk address increment
--- drivers/spi/atcspi200_spi.c | 41 ++++++++++++------------------------- 1 file changed, 13 insertions(+), 28 deletions(-)
drivers/spi/atcspi200_spi.c +13 -28
@@ -18,7 +18,6 @@#include <linux/bitops.h> #define MAX_TRANSFER_LEN 512 -#define CHUNK_SIZE 1#define SPI_TIMEOUT_MS 1000#define SPI_DEF_SRC_CLK 100000000#define SPI_DEF_MAX_CLK 40000000
@@ -188,26 +187,22 @@ static void atcspi200_tx_byte(struct atcspi200_priv *priv, const void *dout)atcspi200_write (priv,ATCSPI200_REG_DATA , *(u8 *)dout); } -static int atcspi200_rx_byte (struct atcspi200_priv *priv,void *din, -unsigned int bytes) +static void atcspi200_rx_byte (struct atcspi200_priv *priv,void *din) { *(u8 *)din = (u8 )atcspi200_read (priv,ATCSPI200_REG_DATA ); -return bytes; }static int atcspi200_hw_xfer (struct atcspi200_priv *priv,unsigned int bitlen,const void *data_out,void *data_in,unsigned long flags) { -unsigned int event, rx_bytes; +unsigned int event;const void *dout =NULL ;void *din =NULL ;int num_blks, num_chunks, max_tran_len, tran_len; -int num_bytes;u8 *cmd_buf = priv->cmd_buf;size_t cmd_len = priv->cmd_len;unsigned long data_len = bitlen /8 ; -int rf_cnt;int ret =0 ;unsigned long start;
@@ -250,10 +245,7 @@ static int atcspi200_hw_xfer(struct atcspi200_priv *priv,while (num_chunks--) { tran_len =min ((size_t )data_len, (size_t )max_tran_len); priv->tran_len = tran_len; - num_blks =DIV_ROUND_UP (tran_len,CHUNK_SIZE ); - num_bytes = tran_len %CHUNK_SIZE ; -if (num_bytes ==0 ) - num_bytes =CHUNK_SIZE ; + num_blks = tran_len; start =get_timer (0 );atcspi200_hw_start (priv);
@@ -267,30 +259,24 @@ static int atcspi200_hw_xfer(struct atcspi200_priv *priv,if ((event &TXEPTY ) && data_out) {atcspi200_tx_byte (priv, dout); - num_blks -=CHUNK_SIZE ; - dout +=CHUNK_SIZE ; + num_blks--; + dout++; }if ((event &RXFVE_MASK ) && data_in) { - rf_cnt =FIELD_GET (RXFVE_MASK , event); -if (rf_cnt >=CHUNK_SIZE ) - rx_bytes =CHUNK_SIZE ; -else if (num_blks ==1 && - rf_cnt == num_bytes) - rx_bytes = num_bytes; -else -continue ; - -if (atcspi200_rx_byte (priv, din, rx_bytes) == - rx_bytes) { - num_blks -=CHUNK_SIZE ; - din = (unsigned char *)din + rx_bytes; - } +atcspi200_rx_byte (priv, din); + num_blks--; + din = (unsigned char *)din +1 ; } } data_len -= tran_len;if (data_len) { +/* +* Increment SPI NOR read address in cmd buffer +* for multi-chunk transfers (3-byte big-endian +* address at cmd_buf[1..3]). +*/ priv->cmd_buf[1 ] += ((tran_len >>16 ) &0xff ); priv->cmd_buf[2 ] += ((tran_len >>8 ) &0xff ); priv->cmd_buf[3 ] += (tran_len &0xff );
@@ -298,7 +284,6 @@ static int atcspi200_hw_xfer(struct atcspi200_priv *priv, } ret =atcspi200_hw_stop (priv); } - ret =atcspi200_hw_stop (priv);return ret; }
Signature · 1 line
2.34.1