4646#define SUN4I_CTL_TP BIT(18)
4747
4848#define SUN4I_INT_CTL_REG 0x0c
49+ #define SUN4I_INT_CTL_RF_F34 BIT(4)
50+ #define SUN4I_INT_CTL_TF_E34 BIT(12)
4951#define SUN4I_INT_CTL_TC BIT(16)
5052
5153#define SUN4I_INT_STA_REG 0x10
6163#define SUN4I_CLK_CTL_CDR1 (div ) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8)
6264#define SUN4I_CLK_CTL_DRS BIT(12)
6365
66+ #define SUN4I_MAX_XFER_SIZE 0xffffff
67+
6468#define SUN4I_BURST_CNT_REG 0x20
65- #define SUN4I_BURST_CNT (cnt ) ((cnt) & 0xffffff )
69+ #define SUN4I_BURST_CNT (cnt ) ((cnt) & SUN4I_MAX_XFER_SIZE )
6670
6771#define SUN4I_XMIT_CNT_REG 0x24
68- #define SUN4I_XMIT_CNT (cnt ) ((cnt) & 0xffffff)
72+ #define SUN4I_XMIT_CNT (cnt ) ((cnt) & SUN4I_MAX_XFER_SIZE)
73+
6974
7075#define SUN4I_FIFO_STA_REG 0x28
7176#define SUN4I_FIFO_STA_RF_CNT_MASK 0x7f
@@ -96,6 +101,31 @@ static inline void sun4i_spi_write(struct sun4i_spi *sspi, u32 reg, u32 value)
96101 writel (value , sspi -> base_addr + reg );
97102}
98103
104+ static inline u32 sun4i_spi_get_tx_fifo_count (struct sun4i_spi * sspi )
105+ {
106+ u32 reg = sun4i_spi_read (sspi , SUN4I_FIFO_STA_REG );
107+
108+ reg >>= SUN4I_FIFO_STA_TF_CNT_BITS ;
109+
110+ return reg & SUN4I_FIFO_STA_TF_CNT_MASK ;
111+ }
112+
113+ static inline void sun4i_spi_enable_interrupt (struct sun4i_spi * sspi , u32 mask )
114+ {
115+ u32 reg = sun4i_spi_read (sspi , SUN4I_INT_CTL_REG );
116+
117+ reg |= mask ;
118+ sun4i_spi_write (sspi , SUN4I_INT_CTL_REG , reg );
119+ }
120+
121+ static inline void sun4i_spi_disable_interrupt (struct sun4i_spi * sspi , u32 mask )
122+ {
123+ u32 reg = sun4i_spi_read (sspi , SUN4I_INT_CTL_REG );
124+
125+ reg &= ~mask ;
126+ sun4i_spi_write (sspi , SUN4I_INT_CTL_REG , reg );
127+ }
128+
99129static inline void sun4i_spi_drain_fifo (struct sun4i_spi * sspi , int len )
100130{
101131 u32 reg , cnt ;
@@ -118,10 +148,13 @@ static inline void sun4i_spi_drain_fifo(struct sun4i_spi *sspi, int len)
118148
119149static inline void sun4i_spi_fill_fifo (struct sun4i_spi * sspi , int len )
120150{
151+ u32 cnt ;
121152 u8 byte ;
122153
123- if (len > sspi -> len )
124- len = sspi -> len ;
154+ /* See how much data we can fit */
155+ cnt = SUN4I_FIFO_DEPTH - sun4i_spi_get_tx_fifo_count (sspi );
156+
157+ len = min3 (len , (int )cnt , sspi -> len );
125158
126159 while (len -- ) {
127160 byte = sspi -> tx_buf ? * sspi -> tx_buf ++ : 0 ;
@@ -184,10 +217,10 @@ static int sun4i_spi_transfer_one(struct spi_master *master,
184217 u32 reg ;
185218
186219 /* We don't support transfer larger than the FIFO */
187- if (tfr -> len > SUN4I_FIFO_DEPTH )
220+ if (tfr -> len > SUN4I_MAX_XFER_SIZE )
188221 return - EMSGSIZE ;
189222
190- if (tfr -> tx_buf && tfr -> len >= SUN4I_FIFO_DEPTH )
223+ if (tfr -> tx_buf && tfr -> len >= SUN4I_MAX_XFER_SIZE )
191224 return - EMSGSIZE ;
192225
193226 reinit_completion (& sspi -> done );
@@ -286,7 +319,11 @@ static int sun4i_spi_transfer_one(struct spi_master *master,
286319 sun4i_spi_fill_fifo (sspi , SUN4I_FIFO_DEPTH - 1 );
287320
288321 /* Enable the interrupts */
289- sun4i_spi_write (sspi , SUN4I_INT_CTL_REG , SUN4I_INT_CTL_TC );
322+ sun4i_spi_enable_interrupt (sspi , SUN4I_INT_CTL_TC |
323+ SUN4I_INT_CTL_RF_F34 );
324+ /* Only enable Tx FIFO interrupt if we really need it */
325+ if (tx_len > SUN4I_FIFO_DEPTH )
326+ sun4i_spi_enable_interrupt (sspi , SUN4I_INT_CTL_TF_E34 );
290327
291328 /* Start the transfer */
292329 reg = sun4i_spi_read (sspi , SUN4I_CTL_REG );
@@ -306,7 +343,6 @@ static int sun4i_spi_transfer_one(struct spi_master *master,
306343 goto out ;
307344 }
308345
309- sun4i_spi_drain_fifo (sspi , SUN4I_FIFO_DEPTH );
310346
311347out :
312348 sun4i_spi_write (sspi , SUN4I_INT_CTL_REG , 0 );
@@ -322,10 +358,33 @@ static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
322358 /* Transfer complete */
323359 if (status & SUN4I_INT_CTL_TC ) {
324360 sun4i_spi_write (sspi , SUN4I_INT_STA_REG , SUN4I_INT_CTL_TC );
361+ sun4i_spi_drain_fifo (sspi , SUN4I_FIFO_DEPTH );
325362 complete (& sspi -> done );
326363 return IRQ_HANDLED ;
327364 }
328365
366+ /* Receive FIFO 3/4 full */
367+ if (status & SUN4I_INT_CTL_RF_F34 ) {
368+ sun4i_spi_drain_fifo (sspi , SUN4I_FIFO_DEPTH );
369+ /* Only clear the interrupt _after_ draining the FIFO */
370+ sun4i_spi_write (sspi , SUN4I_INT_STA_REG , SUN4I_INT_CTL_RF_F34 );
371+ return IRQ_HANDLED ;
372+ }
373+
374+ /* Transmit FIFO 3/4 empty */
375+ if (status & SUN4I_INT_CTL_TF_E34 ) {
376+ sun4i_spi_fill_fifo (sspi , SUN4I_FIFO_DEPTH );
377+
378+ if (!sspi -> len )
379+ /* nothing left to transmit */
380+ sun4i_spi_disable_interrupt (sspi , SUN4I_INT_CTL_TF_E34 );
381+
382+ /* Only clear the interrupt _after_ re-seeding the FIFO */
383+ sun4i_spi_write (sspi , SUN4I_INT_STA_REG , SUN4I_INT_CTL_TF_E34 );
384+
385+ return IRQ_HANDLED ;
386+ }
387+
329388 return IRQ_NONE ;
330389}
331390
0 commit comments