Skip to content

Commit 26470a2

Browse files
committed
spi: imx: support word delay in ecspi
Merge series from Jonas Rebmann <jre@pengutronix.de>: The i.MX SPI controller supports inserting a configurable delay between subsequent words, which is needed for some slower devices that couldn't keep up otherwise. This patch series introduces support for the word delay parameters for i.MX51 onwards. The SPI clock (CSRC=0) was chosen as the clock source over the also available 32.768 KHz Low-Frequency Reference Clock (CSRC=1). The sample period control bits (SAMPLE_PERIOD) are set to the selected word delay converted to SPI clock cycles. A deviation from the requested number of wait cycles and the actual word delay was observed via both software timings and oscilloscope measurements and accounted for. The Chip Select Delay Control bits in the Sample Period Control Register remain zero. Behaviour on i.MX35 and earlier, where the CSPI interface is used, remains unchanged.
2 parents f3c6051 + a3bb4e6 commit 26470a2

File tree

1 file changed

+92
-16
lines changed

1 file changed

+92
-16
lines changed

drivers/spi/spi-imx.c

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (C) 2008 Juergen Beisert
44

55
#include <linux/bits.h>
6+
#include <linux/bitfield.h>
67
#include <linux/clk.h>
78
#include <linux/completion.h>
89
#include <linux/delay.h>
@@ -13,7 +14,10 @@
1314
#include <linux/io.h>
1415
#include <linux/irq.h>
1516
#include <linux/kernel.h>
17+
#include <linux/math.h>
18+
#include <linux/math64.h>
1619
#include <linux/module.h>
20+
#include <linux/overflow.h>
1721
#include <linux/pinctrl/consumer.h>
1822
#include <linux/platform_device.h>
1923
#include <linux/pm_runtime.h>
@@ -71,7 +75,8 @@ struct spi_imx_data;
7175
struct spi_imx_devtype_data {
7276
void (*intctrl)(struct spi_imx_data *spi_imx, int enable);
7377
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);
7580
void (*trigger)(struct spi_imx_data *spi_imx);
7681
int (*rx_available)(struct spi_imx_data *spi_imx);
7782
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
301306
#define MX51_ECSPI_STAT 0x18
302307
#define MX51_ECSPI_STAT_RR (1 << 3)
303308

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+
304321
#define MX51_ECSPI_TESTREG 0x20
305322
#define MX51_ECSPI_TESTREG_LBC BIT(31)
306323

@@ -648,9 +665,10 @@ static void mx51_configure_cpha(struct spi_imx_data *spi_imx,
648665
}
649666

650667
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)
652669
{
653670
u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
671+
u64 word_delay_sck;
654672
u32 clk;
655673

656674
/* Clear BL field and set the right value */
@@ -682,6 +700,49 @@ static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
682700

683701
writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
684702

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+
685746
return 0;
686747
}
687748

@@ -773,7 +834,7 @@ static int mx31_prepare_message(struct spi_imx_data *spi_imx,
773834
}
774835

775836
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)
777838
{
778839
unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_HOST;
779840
unsigned int clk;
@@ -877,7 +938,7 @@ static int mx21_prepare_message(struct spi_imx_data *spi_imx,
877938
}
878939

879940
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)
881942
{
882943
unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_HOST;
883944
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,
9521013
}
9531014

9541015
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)
9561017
{
9571018
unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_HOST;
9581019
unsigned int clk;
@@ -1262,11 +1323,13 @@ static int spi_imx_setupxfer(struct spi_device *spi,
12621323

12631324
/*
12641325
* 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.
12671329
*/
12681330
if (spi_imx->devtype_data->dynamic_burst && !spi_imx->target_mode &&
12691331
!(spi->mode & SPI_CS_WORD) &&
1332+
!(t->word_delay.value) &&
12701333
(spi_imx->bits_per_word == 8 ||
12711334
spi_imx->bits_per_word == 16 ||
12721335
spi_imx->bits_per_word == 32)) {
@@ -1303,7 +1366,7 @@ static int spi_imx_setupxfer(struct spi_device *spi,
13031366
spi_imx->target_burst = t->len;
13041367
}
13051368

1306-
spi_imx->devtype_data->prepare_transfer(spi_imx, spi);
1369+
spi_imx->devtype_data->prepare_transfer(spi_imx, spi, t);
13071370

13081371
return 0;
13091372
}
@@ -1609,12 +1672,30 @@ static int spi_imx_pio_transfer_target(struct spi_device *spi,
16091672
return ret;
16101673
}
16111674

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+
16121694
static int spi_imx_transfer_one(struct spi_controller *controller,
16131695
struct spi_device *spi,
16141696
struct spi_transfer *transfer)
16151697
{
16161698
struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1617-
unsigned long hz_per_byte, byte_limit;
16181699

16191700
spi_imx_setupxfer(spi, transfer);
16201701
transfer->effective_speed_hz = spi_imx->spi_bus_clk;
@@ -1633,15 +1714,10 @@ static int spi_imx_transfer_one(struct spi_controller *controller,
16331714
*/
16341715
if (spi_imx->usedma)
16351716
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;
16421717

16431718
/* 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))
16451721
return spi_imx_poll_transfer(spi, transfer);
16461722

16471723
return spi_imx_pio_transfer(spi, transfer);

0 commit comments

Comments
 (0)