3
3
// Copyright (C) 2008 Juergen Beisert
4
4
5
5
#include <linux/bits.h>
6
+ #include <linux/bitfield.h>
6
7
#include <linux/clk.h>
7
8
#include <linux/completion.h>
8
9
#include <linux/delay.h>
13
14
#include <linux/io.h>
14
15
#include <linux/irq.h>
15
16
#include <linux/kernel.h>
17
+ #include <linux/math.h>
18
+ #include <linux/math64.h>
16
19
#include <linux/module.h>
20
+ #include <linux/overflow.h>
17
21
#include <linux/pinctrl/consumer.h>
18
22
#include <linux/platform_device.h>
19
23
#include <linux/pm_runtime.h>
@@ -71,7 +75,8 @@ struct spi_imx_data;
71
75
struct spi_imx_devtype_data {
72
76
void (* intctrl )(struct spi_imx_data * spi_imx , int enable );
73
77
int (* prepare_message )(struct spi_imx_data * spi_imx , struct spi_message * msg );
74
- int (* prepare_transfer )(struct spi_imx_data * spi_imx , struct spi_device * spi );
78
+ int (* prepare_transfer )(struct spi_imx_data * spi_imx , struct spi_device * spi ,
79
+ struct spi_transfer * t );
75
80
void (* trigger )(struct spi_imx_data * spi_imx );
76
81
int (* rx_available )(struct spi_imx_data * spi_imx );
77
82
void (* reset )(struct spi_imx_data * spi_imx );
@@ -301,6 +306,18 @@ static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device
301
306
#define MX51_ECSPI_STAT 0x18
302
307
#define MX51_ECSPI_STAT_RR (1 << 3)
303
308
309
+ #define MX51_ECSPI_PERIOD 0x1c
310
+ #define MX51_ECSPI_PERIOD_MASK 0x7fff
311
+ /*
312
+ * As measured on the i.MX6, the SPI host controller inserts a 4 SPI-Clock
313
+ * (SCLK) delay after each burst if the PERIOD reg is 0x0. This value will be
314
+ * called MX51_ECSPI_PERIOD_MIN_DELAY_SCK.
315
+ *
316
+ * If the PERIOD register is != 0, the controller inserts a delay of
317
+ * MX51_ECSPI_PERIOD_MIN_DELAY_SCK + register value + 1 SCLK after each burst.
318
+ */
319
+ #define MX51_ECSPI_PERIOD_MIN_DELAY_SCK 4
320
+
304
321
#define MX51_ECSPI_TESTREG 0x20
305
322
#define MX51_ECSPI_TESTREG_LBC BIT(31)
306
323
@@ -648,9 +665,10 @@ static void mx51_configure_cpha(struct spi_imx_data *spi_imx,
648
665
}
649
666
650
667
static int mx51_ecspi_prepare_transfer (struct spi_imx_data * spi_imx ,
651
- struct spi_device * spi )
668
+ struct spi_device * spi , struct spi_transfer * t )
652
669
{
653
670
u32 ctrl = readl (spi_imx -> base + MX51_ECSPI_CTRL );
671
+ u64 word_delay_sck ;
654
672
u32 clk ;
655
673
656
674
/* Clear BL field and set the right value */
@@ -682,6 +700,49 @@ static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
682
700
683
701
writel (ctrl , spi_imx -> base + MX51_ECSPI_CTRL );
684
702
703
+ /* calculate word delay in SPI Clock (SCLK) cycles */
704
+ if (t -> word_delay .value == 0 ) {
705
+ word_delay_sck = 0 ;
706
+ } else if (t -> word_delay .unit == SPI_DELAY_UNIT_SCK ) {
707
+ word_delay_sck = t -> word_delay .value ;
708
+
709
+ if (word_delay_sck <= MX51_ECSPI_PERIOD_MIN_DELAY_SCK )
710
+ word_delay_sck = 0 ;
711
+ else if (word_delay_sck <= MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1 )
712
+ word_delay_sck = 1 ;
713
+ else
714
+ word_delay_sck -= MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1 ;
715
+ } else {
716
+ int word_delay_ns ;
717
+
718
+ word_delay_ns = spi_delay_to_ns (& t -> word_delay , t );
719
+ if (word_delay_ns < 0 )
720
+ return word_delay_ns ;
721
+
722
+ if (word_delay_ns <= mul_u64_u32_div (NSEC_PER_SEC ,
723
+ MX51_ECSPI_PERIOD_MIN_DELAY_SCK ,
724
+ spi_imx -> spi_bus_clk )) {
725
+ word_delay_sck = 0 ;
726
+ } else if (word_delay_ns <= mul_u64_u32_div (NSEC_PER_SEC ,
727
+ MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1 ,
728
+ spi_imx -> spi_bus_clk )) {
729
+ word_delay_sck = 1 ;
730
+ } else {
731
+ word_delay_ns -= mul_u64_u32_div (NSEC_PER_SEC ,
732
+ MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1 ,
733
+ spi_imx -> spi_bus_clk );
734
+
735
+ word_delay_sck = DIV_U64_ROUND_UP ((u64 )word_delay_ns * spi_imx -> spi_bus_clk ,
736
+ NSEC_PER_SEC );
737
+ }
738
+ }
739
+
740
+ if (!FIELD_FIT (MX51_ECSPI_PERIOD_MASK , word_delay_sck ))
741
+ return - EINVAL ;
742
+
743
+ writel (FIELD_PREP (MX51_ECSPI_PERIOD_MASK , word_delay_sck ),
744
+ spi_imx -> base + MX51_ECSPI_PERIOD );
745
+
685
746
return 0 ;
686
747
}
687
748
@@ -773,7 +834,7 @@ static int mx31_prepare_message(struct spi_imx_data *spi_imx,
773
834
}
774
835
775
836
static int mx31_prepare_transfer (struct spi_imx_data * spi_imx ,
776
- struct spi_device * spi )
837
+ struct spi_device * spi , struct spi_transfer * t )
777
838
{
778
839
unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_HOST ;
779
840
unsigned int clk ;
@@ -877,7 +938,7 @@ static int mx21_prepare_message(struct spi_imx_data *spi_imx,
877
938
}
878
939
879
940
static int mx21_prepare_transfer (struct spi_imx_data * spi_imx ,
880
- struct spi_device * spi )
941
+ struct spi_device * spi , struct spi_transfer * t )
881
942
{
882
943
unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_HOST ;
883
944
unsigned int max = is_imx27_cspi (spi_imx ) ? 16 : 18 ;
@@ -952,7 +1013,7 @@ static int mx1_prepare_message(struct spi_imx_data *spi_imx,
952
1013
}
953
1014
954
1015
static int mx1_prepare_transfer (struct spi_imx_data * spi_imx ,
955
- struct spi_device * spi )
1016
+ struct spi_device * spi , struct spi_transfer * t )
956
1017
{
957
1018
unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_HOST ;
958
1019
unsigned int clk ;
@@ -1262,11 +1323,13 @@ static int spi_imx_setupxfer(struct spi_device *spi,
1262
1323
1263
1324
/*
1264
1325
* Initialize the functions for transfer. To transfer non byte-aligned
1265
- * words, we have to use multiple word-size bursts, we can't use
1266
- * dynamic_burst in that case.
1326
+ * words, we have to use multiple word-size bursts. To insert word
1327
+ * delay, the burst size has to equal the word size. We can't use
1328
+ * dynamic_burst in these cases.
1267
1329
*/
1268
1330
if (spi_imx -> devtype_data -> dynamic_burst && !spi_imx -> target_mode &&
1269
1331
!(spi -> mode & SPI_CS_WORD ) &&
1332
+ !(t -> word_delay .value ) &&
1270
1333
(spi_imx -> bits_per_word == 8 ||
1271
1334
spi_imx -> bits_per_word == 16 ||
1272
1335
spi_imx -> bits_per_word == 32 )) {
@@ -1303,7 +1366,7 @@ static int spi_imx_setupxfer(struct spi_device *spi,
1303
1366
spi_imx -> target_burst = t -> len ;
1304
1367
}
1305
1368
1306
- spi_imx -> devtype_data -> prepare_transfer (spi_imx , spi );
1369
+ spi_imx -> devtype_data -> prepare_transfer (spi_imx , spi , t );
1307
1370
1308
1371
return 0 ;
1309
1372
}
@@ -1609,12 +1672,30 @@ static int spi_imx_pio_transfer_target(struct spi_device *spi,
1609
1672
return ret ;
1610
1673
}
1611
1674
1675
+ static unsigned int spi_imx_transfer_estimate_time_us (struct spi_transfer * transfer )
1676
+ {
1677
+ u64 result ;
1678
+
1679
+ result = DIV_U64_ROUND_CLOSEST ((u64 )USEC_PER_SEC * transfer -> len * BITS_PER_BYTE ,
1680
+ transfer -> effective_speed_hz );
1681
+ if (transfer -> word_delay .value ) {
1682
+ unsigned int word_delay_us ;
1683
+ unsigned int words ;
1684
+
1685
+ words = DIV_ROUND_UP (transfer -> len * BITS_PER_BYTE , transfer -> bits_per_word );
1686
+ word_delay_us = DIV_ROUND_CLOSEST (spi_delay_to_ns (& transfer -> word_delay , transfer ),
1687
+ NSEC_PER_USEC );
1688
+ result += words * word_delay_us ;
1689
+ }
1690
+
1691
+ return min (result , U32_MAX );
1692
+ }
1693
+
1612
1694
static int spi_imx_transfer_one (struct spi_controller * controller ,
1613
1695
struct spi_device * spi ,
1614
1696
struct spi_transfer * transfer )
1615
1697
{
1616
1698
struct spi_imx_data * spi_imx = spi_controller_get_devdata (spi -> controller );
1617
- unsigned long hz_per_byte , byte_limit ;
1618
1699
1619
1700
spi_imx_setupxfer (spi , transfer );
1620
1701
transfer -> effective_speed_hz = spi_imx -> spi_bus_clk ;
@@ -1633,15 +1714,10 @@ static int spi_imx_transfer_one(struct spi_controller *controller,
1633
1714
*/
1634
1715
if (spi_imx -> usedma )
1635
1716
return spi_imx_dma_transfer (spi_imx , transfer );
1636
- /*
1637
- * Calculate the estimated time in us the transfer runs. Find
1638
- * the number of Hz per byte per polling limit.
1639
- */
1640
- hz_per_byte = polling_limit_us ? ((8 + 4 ) * USEC_PER_SEC ) / polling_limit_us : 0 ;
1641
- byte_limit = hz_per_byte ? transfer -> effective_speed_hz / hz_per_byte : 1 ;
1642
1717
1643
1718
/* run in polling mode for short transfers */
1644
- if (transfer -> len < byte_limit )
1719
+ if (transfer -> len == 1 || (polling_limit_us &&
1720
+ spi_imx_transfer_estimate_time_us (transfer ) < polling_limit_us ))
1645
1721
return spi_imx_poll_transfer (spi , transfer );
1646
1722
1647
1723
return spi_imx_pio_transfer (spi , transfer );
0 commit comments