@@ -208,6 +208,65 @@ static void mtk_spi_reset(struct mtk_spi *mdata)
208
208
writel (reg_val , mdata -> base + SPI_CMD_REG );
209
209
}
210
210
211
+ static int mtk_spi_set_hw_cs_timing (struct spi_device * spi )
212
+ {
213
+ struct mtk_spi * mdata = spi_master_get_devdata (spi -> master );
214
+ struct spi_delay * cs_setup = & spi -> cs_setup ;
215
+ struct spi_delay * cs_hold = & spi -> cs_hold ;
216
+ struct spi_delay * cs_inactive = & spi -> cs_inactive ;
217
+ u16 setup , hold , inactive ;
218
+ u32 reg_val ;
219
+ int delay ;
220
+
221
+ delay = spi_delay_to_ns (cs_setup , NULL );
222
+ if (delay < 0 )
223
+ return delay ;
224
+ setup = (delay * DIV_ROUND_UP (mdata -> spi_clk_hz , 1000000 )) / 1000 ;
225
+
226
+ delay = spi_delay_to_ns (cs_hold , NULL );
227
+ if (delay < 0 )
228
+ return delay ;
229
+ hold = (delay * DIV_ROUND_UP (mdata -> spi_clk_hz , 1000000 )) / 1000 ;
230
+
231
+ delay = spi_delay_to_ns (cs_inactive , NULL );
232
+ if (delay < 0 )
233
+ return delay ;
234
+ inactive = (delay * DIV_ROUND_UP (mdata -> spi_clk_hz , 1000000 )) / 1000 ;
235
+
236
+ setup = setup ? setup : 1 ;
237
+ hold = hold ? hold : 1 ;
238
+ inactive = inactive ? inactive : 1 ;
239
+
240
+ reg_val = readl (mdata -> base + SPI_CFG0_REG );
241
+ if (mdata -> dev_comp -> enhance_timing ) {
242
+ hold = min (hold , 0xffff );
243
+ setup = min (setup , 0xffff );
244
+ reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET );
245
+ reg_val |= (((hold - 1 ) & 0xffff )
246
+ << SPI_ADJUST_CFG0_CS_HOLD_OFFSET );
247
+ reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET );
248
+ reg_val |= (((setup - 1 ) & 0xffff )
249
+ << SPI_ADJUST_CFG0_CS_SETUP_OFFSET );
250
+ } else {
251
+ hold = min (hold , 0xff );
252
+ setup = min (setup , 0xff );
253
+ reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET );
254
+ reg_val |= (((hold - 1 ) & 0xff ) << SPI_CFG0_CS_HOLD_OFFSET );
255
+ reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET );
256
+ reg_val |= (((setup - 1 ) & 0xff )
257
+ << SPI_CFG0_CS_SETUP_OFFSET );
258
+ }
259
+ writel (reg_val , mdata -> base + SPI_CFG0_REG );
260
+
261
+ inactive = min (inactive , 0xff );
262
+ reg_val = readl (mdata -> base + SPI_CFG1_REG );
263
+ reg_val &= ~SPI_CFG1_CS_IDLE_MASK ;
264
+ reg_val |= (((inactive - 1 ) & 0xff ) << SPI_CFG1_CS_IDLE_OFFSET );
265
+ writel (reg_val , mdata -> base + SPI_CFG1_REG );
266
+
267
+ return 0 ;
268
+ }
269
+
211
270
static int mtk_spi_prepare_message (struct spi_master * master ,
212
271
struct spi_message * msg )
213
272
{
@@ -284,6 +343,8 @@ static int mtk_spi_prepare_message(struct spi_master *master,
284
343
<< SPI_CFG1_GET_TICK_DLY_OFFSET );
285
344
writel (reg_val , mdata -> base + SPI_CFG1_REG );
286
345
346
+ /* set hw cs timing */
347
+ mtk_spi_set_hw_cs_timing (spi );
287
348
return 0 ;
288
349
}
289
350
@@ -528,52 +589,6 @@ static bool mtk_spi_can_dma(struct spi_master *master,
528
589
(unsigned long )xfer -> rx_buf % 4 == 0 );
529
590
}
530
591
531
- static int mtk_spi_set_hw_cs_timing (struct spi_device * spi ,
532
- struct spi_delay * setup ,
533
- struct spi_delay * hold ,
534
- struct spi_delay * inactive )
535
- {
536
- struct mtk_spi * mdata = spi_master_get_devdata (spi -> master );
537
- u16 setup_dly , hold_dly , inactive_dly ;
538
- u32 reg_val ;
539
-
540
- if ((setup && setup -> unit != SPI_DELAY_UNIT_SCK ) ||
541
- (hold && hold -> unit != SPI_DELAY_UNIT_SCK ) ||
542
- (inactive && inactive -> unit != SPI_DELAY_UNIT_SCK )) {
543
- dev_err (& spi -> dev ,
544
- "Invalid delay unit, should be SPI_DELAY_UNIT_SCK\n" );
545
- return - EINVAL ;
546
- }
547
-
548
- setup_dly = setup ? setup -> value : 1 ;
549
- hold_dly = hold ? hold -> value : 1 ;
550
- inactive_dly = inactive ? inactive -> value : 1 ;
551
-
552
- reg_val = readl (mdata -> base + SPI_CFG0_REG );
553
- if (mdata -> dev_comp -> enhance_timing ) {
554
- reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET );
555
- reg_val |= (((hold_dly - 1 ) & 0xffff )
556
- << SPI_ADJUST_CFG0_CS_HOLD_OFFSET );
557
- reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET );
558
- reg_val |= (((setup_dly - 1 ) & 0xffff )
559
- << SPI_ADJUST_CFG0_CS_SETUP_OFFSET );
560
- } else {
561
- reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET );
562
- reg_val |= (((hold_dly - 1 ) & 0xff ) << SPI_CFG0_CS_HOLD_OFFSET );
563
- reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET );
564
- reg_val |= (((setup_dly - 1 ) & 0xff )
565
- << SPI_CFG0_CS_SETUP_OFFSET );
566
- }
567
- writel (reg_val , mdata -> base + SPI_CFG0_REG );
568
-
569
- reg_val = readl (mdata -> base + SPI_CFG1_REG );
570
- reg_val &= ~SPI_CFG1_CS_IDLE_MASK ;
571
- reg_val |= (((inactive_dly - 1 ) & 0xff ) << SPI_CFG1_CS_IDLE_OFFSET );
572
- writel (reg_val , mdata -> base + SPI_CFG1_REG );
573
-
574
- return 0 ;
575
- }
576
-
577
592
static int mtk_spi_setup (struct spi_device * spi )
578
593
{
579
594
struct mtk_spi * mdata = spi_master_get_devdata (spi -> master );
0 commit comments