2018-08-01 15:29:12 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// spi-uniphier.c - Socionext UniPhier SPI controller driver
|
|
|
|
// Copyright 2012 Panasonic Corporation
|
|
|
|
// Copyright 2016-2018 Socionext Inc.
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/bitfield.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/clk.h>
|
2019-09-03 13:31:01 +08:00
|
|
|
#include <linux/delay.h>
|
2019-12-24 08:58:27 +08:00
|
|
|
#include <linux/dmaengine.h>
|
2018-08-01 15:29:12 +08:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
|
|
|
#define SSI_TIMEOUT_MS 2000
|
2019-09-03 13:31:01 +08:00
|
|
|
#define SSI_POLL_TIMEOUT_US 200
|
2018-08-01 15:29:12 +08:00
|
|
|
#define SSI_MAX_CLK_DIVIDER 254
|
|
|
|
#define SSI_MIN_CLK_DIVIDER 4
|
|
|
|
|
|
|
|
struct uniphier_spi_priv {
|
|
|
|
void __iomem *base;
|
2019-12-24 08:58:27 +08:00
|
|
|
dma_addr_t base_dma_addr;
|
2018-08-01 15:29:12 +08:00
|
|
|
struct clk *clk;
|
2023-11-28 17:30:23 +08:00
|
|
|
struct spi_controller *host;
|
2018-08-01 15:29:12 +08:00
|
|
|
struct completion xfer_done;
|
|
|
|
|
|
|
|
int error;
|
|
|
|
unsigned int tx_bytes;
|
|
|
|
unsigned int rx_bytes;
|
|
|
|
const u8 *tx_buf;
|
|
|
|
u8 *rx_buf;
|
2019-12-24 08:58:27 +08:00
|
|
|
atomic_t dma_busy;
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
bool is_save_param;
|
|
|
|
u8 bits_per_word;
|
|
|
|
u16 mode;
|
|
|
|
u32 speed_hz;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SSI_CTL 0x00
|
|
|
|
#define SSI_CTL_EN BIT(0)
|
|
|
|
|
|
|
|
#define SSI_CKS 0x04
|
|
|
|
#define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
|
|
|
|
#define SSI_CKS_CKPHS BIT(14)
|
|
|
|
#define SSI_CKS_CKINIT BIT(13)
|
|
|
|
#define SSI_CKS_CKDLY BIT(12)
|
|
|
|
|
|
|
|
#define SSI_TXWDS 0x08
|
|
|
|
#define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
|
|
|
|
#define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
|
|
|
|
#define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
|
|
|
|
|
|
|
|
#define SSI_RXWDS 0x0c
|
|
|
|
#define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
|
|
|
|
|
|
|
|
#define SSI_FPS 0x10
|
|
|
|
#define SSI_FPS_FSPOL BIT(15)
|
|
|
|
#define SSI_FPS_FSTRT BIT(14)
|
|
|
|
|
|
|
|
#define SSI_SR 0x14
|
2019-12-24 08:58:27 +08:00
|
|
|
#define SSI_SR_BUSY BIT(7)
|
2018-08-01 15:29:12 +08:00
|
|
|
#define SSI_SR_RNE BIT(0)
|
|
|
|
|
|
|
|
#define SSI_IE 0x18
|
2019-12-24 08:58:27 +08:00
|
|
|
#define SSI_IE_TCIE BIT(4)
|
2018-08-01 15:29:12 +08:00
|
|
|
#define SSI_IE_RCIE BIT(3)
|
2019-12-24 08:58:27 +08:00
|
|
|
#define SSI_IE_TXRE BIT(2)
|
|
|
|
#define SSI_IE_RXRE BIT(1)
|
2018-08-01 15:29:12 +08:00
|
|
|
#define SSI_IE_RORIE BIT(0)
|
2019-12-24 08:58:27 +08:00
|
|
|
#define SSI_IE_ALL_MASK GENMASK(4, 0)
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
#define SSI_IS 0x1c
|
|
|
|
#define SSI_IS_RXRS BIT(9)
|
|
|
|
#define SSI_IS_RCID BIT(3)
|
|
|
|
#define SSI_IS_RORID BIT(0)
|
|
|
|
|
|
|
|
#define SSI_IC 0x1c
|
|
|
|
#define SSI_IC_TCIC BIT(4)
|
|
|
|
#define SSI_IC_RCIC BIT(3)
|
|
|
|
#define SSI_IC_RORIC BIT(0)
|
|
|
|
|
|
|
|
#define SSI_FC 0x20
|
|
|
|
#define SSI_FC_TXFFL BIT(12)
|
|
|
|
#define SSI_FC_TXFTH_MASK GENMASK(11, 8)
|
|
|
|
#define SSI_FC_RXFFL BIT(4)
|
|
|
|
#define SSI_FC_RXFTH_MASK GENMASK(3, 0)
|
|
|
|
|
|
|
|
#define SSI_TXDR 0x24
|
|
|
|
#define SSI_RXDR 0x24
|
|
|
|
|
|
|
|
#define SSI_FIFO_DEPTH 8U
|
2019-12-24 08:58:27 +08:00
|
|
|
#define SSI_FIFO_BURST_NUM 1
|
|
|
|
|
|
|
|
#define SSI_DMA_RX_BUSY BIT(1)
|
|
|
|
#define SSI_DMA_TX_BUSY BIT(0)
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
static inline unsigned int bytes_per_word(unsigned int bits)
|
|
|
|
{
|
|
|
|
return bits <= 8 ? 1 : (bits <= 16 ? 2 : 4);
|
|
|
|
}
|
|
|
|
|
2019-12-24 08:58:24 +08:00
|
|
|
static inline void uniphier_spi_irq_enable(struct uniphier_spi_priv *priv,
|
|
|
|
u32 mask)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_IE);
|
|
|
|
val |= mask;
|
|
|
|
writel(val, priv->base + SSI_IE);
|
|
|
|
}
|
|
|
|
|
2019-12-24 08:58:24 +08:00
|
|
|
static inline void uniphier_spi_irq_disable(struct uniphier_spi_priv *priv,
|
|
|
|
u32 mask)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_IE);
|
|
|
|
val &= ~mask;
|
|
|
|
writel(val, priv->base + SSI_IE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_set_mode(struct spi_device *spi)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(spi->controller);
|
2018-08-01 15:29:12 +08:00
|
|
|
u32 val1, val2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clock setting
|
|
|
|
* CKPHS capture timing. 0:rising edge, 1:falling edge
|
|
|
|
* CKINIT clock initial level. 0:low, 1:high
|
|
|
|
* CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
|
|
|
|
* (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
|
|
|
|
*
|
|
|
|
* frame setting
|
|
|
|
* FSPOL frame signal porarity. 0: low, 1: high
|
|
|
|
* FSTRT start frame timing
|
|
|
|
* 0: rising edge of clock, 1: falling edge of clock
|
|
|
|
*/
|
2021-05-10 21:12:17 +08:00
|
|
|
switch (spi->mode & SPI_MODE_X_MASK) {
|
2018-08-01 15:29:12 +08:00
|
|
|
case SPI_MODE_0:
|
|
|
|
/* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
|
|
|
|
val1 = SSI_CKS_CKPHS | SSI_CKS_CKDLY;
|
|
|
|
val2 = 0;
|
|
|
|
break;
|
|
|
|
case SPI_MODE_1:
|
|
|
|
/* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
|
|
|
|
val1 = 0;
|
|
|
|
val2 = SSI_FPS_FSTRT;
|
|
|
|
break;
|
|
|
|
case SPI_MODE_2:
|
|
|
|
/* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
|
|
|
|
val1 = SSI_CKS_CKINIT | SSI_CKS_CKDLY;
|
|
|
|
val2 = SSI_FPS_FSTRT;
|
|
|
|
break;
|
|
|
|
case SPI_MODE_3:
|
|
|
|
/* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
|
|
|
|
val1 = SSI_CKS_CKPHS | SSI_CKS_CKINIT;
|
|
|
|
val2 = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(spi->mode & SPI_CS_HIGH))
|
|
|
|
val2 |= SSI_FPS_FSPOL;
|
|
|
|
|
|
|
|
writel(val1, priv->base + SSI_CKS);
|
|
|
|
writel(val2, priv->base + SSI_FPS);
|
|
|
|
|
|
|
|
val1 = 0;
|
|
|
|
if (spi->mode & SPI_LSB_FIRST)
|
|
|
|
val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
|
|
|
|
writel(val1, priv->base + SSI_TXWDS);
|
|
|
|
writel(val1, priv->base + SSI_RXWDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_set_transfer_size(struct spi_device *spi, int size)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(spi->controller);
|
2018-08-01 15:29:12 +08:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_TXWDS);
|
|
|
|
val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
|
|
|
|
val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
|
|
|
|
val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
|
|
|
|
writel(val, priv->base + SSI_TXWDS);
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_RXWDS);
|
|
|
|
val &= ~SSI_RXWDS_DTLEN_MASK;
|
|
|
|
val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
|
|
|
|
writel(val, priv->base + SSI_RXWDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_set_baudrate(struct spi_device *spi,
|
|
|
|
unsigned int speed)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(spi->controller);
|
2018-08-01 15:29:12 +08:00
|
|
|
u32 val, ckdiv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the supported rates are even numbers from 4 to 254. (4,6,8...254)
|
|
|
|
* round up as we look for equal or less speed
|
|
|
|
*/
|
|
|
|
ckdiv = DIV_ROUND_UP(clk_get_rate(priv->clk), speed);
|
|
|
|
ckdiv = round_up(ckdiv, 2);
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_CKS);
|
|
|
|
val &= ~SSI_CKS_CKRAT_MASK;
|
|
|
|
val |= ckdiv & SSI_CKS_CKRAT_MASK;
|
|
|
|
writel(val, priv->base + SSI_CKS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_setup_transfer(struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(spi->controller);
|
2018-08-01 15:29:12 +08:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
priv->error = 0;
|
|
|
|
priv->tx_buf = t->tx_buf;
|
|
|
|
priv->rx_buf = t->rx_buf;
|
|
|
|
priv->tx_bytes = priv->rx_bytes = t->len;
|
|
|
|
|
|
|
|
if (!priv->is_save_param || priv->mode != spi->mode) {
|
|
|
|
uniphier_spi_set_mode(spi);
|
|
|
|
priv->mode = spi->mode;
|
2019-09-03 13:30:59 +08:00
|
|
|
priv->is_save_param = false;
|
2018-08-01 15:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!priv->is_save_param || priv->bits_per_word != t->bits_per_word) {
|
|
|
|
uniphier_spi_set_transfer_size(spi, t->bits_per_word);
|
|
|
|
priv->bits_per_word = t->bits_per_word;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!priv->is_save_param || priv->speed_hz != t->speed_hz) {
|
|
|
|
uniphier_spi_set_baudrate(spi, t->speed_hz);
|
|
|
|
priv->speed_hz = t->speed_hz;
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:31:00 +08:00
|
|
|
priv->is_save_param = true;
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
/* reset FIFOs */
|
|
|
|
val = SSI_FC_TXFFL | SSI_FC_RXFFL;
|
|
|
|
writel(val, priv->base + SSI_FC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_send(struct uniphier_spi_priv *priv)
|
|
|
|
{
|
|
|
|
int wsize;
|
|
|
|
u32 val = 0;
|
|
|
|
|
|
|
|
wsize = min(bytes_per_word(priv->bits_per_word), priv->tx_bytes);
|
|
|
|
priv->tx_bytes -= wsize;
|
|
|
|
|
|
|
|
if (priv->tx_buf) {
|
|
|
|
switch (wsize) {
|
|
|
|
case 1:
|
|
|
|
val = *priv->tx_buf;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
val = get_unaligned_le16(priv->tx_buf);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
val = get_unaligned_le32(priv->tx_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->tx_buf += wsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
writel(val, priv->base + SSI_TXDR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_recv(struct uniphier_spi_priv *priv)
|
|
|
|
{
|
|
|
|
int rsize;
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
rsize = min(bytes_per_word(priv->bits_per_word), priv->rx_bytes);
|
|
|
|
priv->rx_bytes -= rsize;
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_RXDR);
|
|
|
|
|
|
|
|
if (priv->rx_buf) {
|
|
|
|
switch (rsize) {
|
|
|
|
case 1:
|
|
|
|
*priv->rx_buf = val;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
put_unaligned_le16(val, priv->rx_buf);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
put_unaligned_le32(val, priv->rx_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->rx_buf += rsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
spi: uniphier: Fix FIFO threshold
Rx threshold means the value to inform the receiver when the number of words
in Rx FIFO is equal to or more than the value. Similarly, Tx threshold means
the value to inform the sender when the number of words in Tx FIFO is equal
to or less than the value. The controller triggers the driver to start
the transfer.
In case of Rx, the driver wants to detect that the specified number of words
N are in Rx FIFO, so the value of Rx threshold should be N. In case of Tx,
the driver wants to detect that the same number of spaces as Rx are in
Tx FIFO, so the value of Tx threshold should be (FIFO size - N).
For example, in order for the driver to receive at least 3 words from
Rx FIFO, set 3 to Rx threshold.
+-+-+-+-+-+-+-+-+
| | | | | |*|*|*|
+-+-+-+-+-+-+-+-+
In order for the driver to send at least 3 words to Tx FIFO, because
it needs at least 3 spaces, set 8(FIFO size) - 3 = 5 to Tx threshold.
+-+-+-+-+-+-+-+-+
|*|*|*|*|*| | | |
+-+-+-+-+-+-+-+-+
This adds new function uniphier_spi_set_fifo_threshold() to set
threshold value to the register.
And more, FIFO counts by 'words', so this renames 'fill_bytes' with
'fill_words', and fixes the calculation using bytes_per_words.
Fixes: 37ffab817098 ("spi: uniphier: introduce polling mode")
Cc: Keiji Hayashibara <hayashibara.keiji@socionext.com>
Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
Link: https://lore.kernel.org/r/1577149107-30670-2-git-send-email-hayashi.kunihiko@socionext.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-12-24 08:58:23 +08:00
|
|
|
static void uniphier_spi_set_fifo_threshold(struct uniphier_spi_priv *priv,
|
|
|
|
unsigned int threshold)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_FC);
|
|
|
|
val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
|
spi: uniphier: Fix FIFO threshold
Rx threshold means the value to inform the receiver when the number of words
in Rx FIFO is equal to or more than the value. Similarly, Tx threshold means
the value to inform the sender when the number of words in Tx FIFO is equal
to or less than the value. The controller triggers the driver to start
the transfer.
In case of Rx, the driver wants to detect that the specified number of words
N are in Rx FIFO, so the value of Rx threshold should be N. In case of Tx,
the driver wants to detect that the same number of spaces as Rx are in
Tx FIFO, so the value of Tx threshold should be (FIFO size - N).
For example, in order for the driver to receive at least 3 words from
Rx FIFO, set 3 to Rx threshold.
+-+-+-+-+-+-+-+-+
| | | | | |*|*|*|
+-+-+-+-+-+-+-+-+
In order for the driver to send at least 3 words to Tx FIFO, because
it needs at least 3 spaces, set 8(FIFO size) - 3 = 5 to Tx threshold.
+-+-+-+-+-+-+-+-+
|*|*|*|*|*| | | |
+-+-+-+-+-+-+-+-+
This adds new function uniphier_spi_set_fifo_threshold() to set
threshold value to the register.
And more, FIFO counts by 'words', so this renames 'fill_bytes' with
'fill_words', and fixes the calculation using bytes_per_words.
Fixes: 37ffab817098 ("spi: uniphier: introduce polling mode")
Cc: Keiji Hayashibara <hayashibara.keiji@socionext.com>
Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
Link: https://lore.kernel.org/r/1577149107-30670-2-git-send-email-hayashi.kunihiko@socionext.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-12-24 08:58:23 +08:00
|
|
|
val |= FIELD_PREP(SSI_FC_TXFTH_MASK, SSI_FIFO_DEPTH - threshold);
|
|
|
|
val |= FIELD_PREP(SSI_FC_RXFTH_MASK, threshold);
|
2018-08-01 15:29:12 +08:00
|
|
|
writel(val, priv->base + SSI_FC);
|
spi: uniphier: Fix FIFO threshold
Rx threshold means the value to inform the receiver when the number of words
in Rx FIFO is equal to or more than the value. Similarly, Tx threshold means
the value to inform the sender when the number of words in Tx FIFO is equal
to or less than the value. The controller triggers the driver to start
the transfer.
In case of Rx, the driver wants to detect that the specified number of words
N are in Rx FIFO, so the value of Rx threshold should be N. In case of Tx,
the driver wants to detect that the same number of spaces as Rx are in
Tx FIFO, so the value of Tx threshold should be (FIFO size - N).
For example, in order for the driver to receive at least 3 words from
Rx FIFO, set 3 to Rx threshold.
+-+-+-+-+-+-+-+-+
| | | | | |*|*|*|
+-+-+-+-+-+-+-+-+
In order for the driver to send at least 3 words to Tx FIFO, because
it needs at least 3 spaces, set 8(FIFO size) - 3 = 5 to Tx threshold.
+-+-+-+-+-+-+-+-+
|*|*|*|*|*| | | |
+-+-+-+-+-+-+-+-+
This adds new function uniphier_spi_set_fifo_threshold() to set
threshold value to the register.
And more, FIFO counts by 'words', so this renames 'fill_bytes' with
'fill_words', and fixes the calculation using bytes_per_words.
Fixes: 37ffab817098 ("spi: uniphier: introduce polling mode")
Cc: Keiji Hayashibara <hayashibara.keiji@socionext.com>
Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
Link: https://lore.kernel.org/r/1577149107-30670-2-git-send-email-hayashi.kunihiko@socionext.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-12-24 08:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_fill_tx_fifo(struct uniphier_spi_priv *priv)
|
|
|
|
{
|
|
|
|
unsigned int fifo_threshold, fill_words;
|
|
|
|
unsigned int bpw = bytes_per_word(priv->bits_per_word);
|
|
|
|
|
|
|
|
fifo_threshold = DIV_ROUND_UP(priv->rx_bytes, bpw);
|
|
|
|
fifo_threshold = min(fifo_threshold, SSI_FIFO_DEPTH);
|
|
|
|
|
|
|
|
uniphier_spi_set_fifo_threshold(priv, fifo_threshold);
|
|
|
|
|
|
|
|
fill_words = fifo_threshold -
|
|
|
|
DIV_ROUND_UP(priv->rx_bytes - priv->tx_bytes, bpw);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
spi: uniphier: Fix FIFO threshold
Rx threshold means the value to inform the receiver when the number of words
in Rx FIFO is equal to or more than the value. Similarly, Tx threshold means
the value to inform the sender when the number of words in Tx FIFO is equal
to or less than the value. The controller triggers the driver to start
the transfer.
In case of Rx, the driver wants to detect that the specified number of words
N are in Rx FIFO, so the value of Rx threshold should be N. In case of Tx,
the driver wants to detect that the same number of spaces as Rx are in
Tx FIFO, so the value of Tx threshold should be (FIFO size - N).
For example, in order for the driver to receive at least 3 words from
Rx FIFO, set 3 to Rx threshold.
+-+-+-+-+-+-+-+-+
| | | | | |*|*|*|
+-+-+-+-+-+-+-+-+
In order for the driver to send at least 3 words to Tx FIFO, because
it needs at least 3 spaces, set 8(FIFO size) - 3 = 5 to Tx threshold.
+-+-+-+-+-+-+-+-+
|*|*|*|*|*| | | |
+-+-+-+-+-+-+-+-+
This adds new function uniphier_spi_set_fifo_threshold() to set
threshold value to the register.
And more, FIFO counts by 'words', so this renames 'fill_bytes' with
'fill_words', and fixes the calculation using bytes_per_words.
Fixes: 37ffab817098 ("spi: uniphier: introduce polling mode")
Cc: Keiji Hayashibara <hayashibara.keiji@socionext.com>
Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
Link: https://lore.kernel.org/r/1577149107-30670-2-git-send-email-hayashi.kunihiko@socionext.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-12-24 08:58:23 +08:00
|
|
|
while (fill_words--)
|
2018-08-01 15:29:12 +08:00
|
|
|
uniphier_spi_send(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_set_cs(struct spi_device *spi, bool enable)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(spi->controller);
|
2018-08-01 15:29:12 +08:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(priv->base + SSI_FPS);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
val |= SSI_FPS_FSPOL;
|
|
|
|
else
|
|
|
|
val &= ~SSI_FPS_FSPOL;
|
|
|
|
|
|
|
|
writel(val, priv->base + SSI_FPS);
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static bool uniphier_spi_can_dma(struct spi_controller *host,
|
2019-12-24 08:58:27 +08:00
|
|
|
struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-12-24 08:58:27 +08:00
|
|
|
unsigned int bpw = bytes_per_word(priv->bits_per_word);
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
if ((!host->dma_tx && !host->dma_rx)
|
|
|
|
|| (!host->dma_tx && t->tx_buf)
|
|
|
|
|| (!host->dma_rx && t->rx_buf))
|
2019-12-24 08:58:27 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return DIV_ROUND_UP(t->len, bpw) > SSI_FIFO_DEPTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_dma_rxcb(void *data)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct spi_controller *host = data;
|
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-12-24 08:58:27 +08:00
|
|
|
int state = atomic_fetch_andnot(SSI_DMA_RX_BUSY, &priv->dma_busy);
|
|
|
|
|
|
|
|
uniphier_spi_irq_disable(priv, SSI_IE_RXRE);
|
|
|
|
|
|
|
|
if (!(state & SSI_DMA_TX_BUSY))
|
2023-11-28 17:30:23 +08:00
|
|
|
spi_finalize_current_transfer(host);
|
2019-12-24 08:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uniphier_spi_dma_txcb(void *data)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct spi_controller *host = data;
|
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-12-24 08:58:27 +08:00
|
|
|
int state = atomic_fetch_andnot(SSI_DMA_TX_BUSY, &priv->dma_busy);
|
|
|
|
|
|
|
|
uniphier_spi_irq_disable(priv, SSI_IE_TXRE);
|
|
|
|
|
|
|
|
if (!(state & SSI_DMA_RX_BUSY))
|
2023-11-28 17:30:23 +08:00
|
|
|
spi_finalize_current_transfer(host);
|
2019-12-24 08:58:27 +08:00
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static int uniphier_spi_transfer_one_dma(struct spi_controller *host,
|
2019-12-24 08:58:27 +08:00
|
|
|
struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-12-24 08:58:27 +08:00
|
|
|
struct dma_async_tx_descriptor *rxdesc = NULL, *txdesc = NULL;
|
|
|
|
int buswidth;
|
|
|
|
|
|
|
|
atomic_set(&priv->dma_busy, 0);
|
|
|
|
|
|
|
|
uniphier_spi_set_fifo_threshold(priv, SSI_FIFO_BURST_NUM);
|
|
|
|
|
|
|
|
if (priv->bits_per_word <= 8)
|
|
|
|
buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
|
|
|
|
else if (priv->bits_per_word <= 16)
|
|
|
|
buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
|
|
|
|
else
|
|
|
|
buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
|
|
|
|
if (priv->rx_buf) {
|
|
|
|
struct dma_slave_config rxconf = {
|
|
|
|
.direction = DMA_DEV_TO_MEM,
|
|
|
|
.src_addr = priv->base_dma_addr + SSI_RXDR,
|
|
|
|
.src_addr_width = buswidth,
|
|
|
|
.src_maxburst = SSI_FIFO_BURST_NUM,
|
|
|
|
};
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
dmaengine_slave_config(host->dma_rx, &rxconf);
|
2019-12-24 08:58:27 +08:00
|
|
|
|
|
|
|
rxdesc = dmaengine_prep_slave_sg(
|
2023-11-28 17:30:23 +08:00
|
|
|
host->dma_rx,
|
2019-12-24 08:58:27 +08:00
|
|
|
t->rx_sg.sgl, t->rx_sg.nents,
|
|
|
|
DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!rxdesc)
|
|
|
|
goto out_err_prep;
|
|
|
|
|
|
|
|
rxdesc->callback = uniphier_spi_dma_rxcb;
|
2023-11-28 17:30:23 +08:00
|
|
|
rxdesc->callback_param = host;
|
2019-12-24 08:58:27 +08:00
|
|
|
|
|
|
|
uniphier_spi_irq_enable(priv, SSI_IE_RXRE);
|
|
|
|
atomic_or(SSI_DMA_RX_BUSY, &priv->dma_busy);
|
|
|
|
|
|
|
|
dmaengine_submit(rxdesc);
|
2023-11-28 17:30:23 +08:00
|
|
|
dma_async_issue_pending(host->dma_rx);
|
2019-12-24 08:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->tx_buf) {
|
|
|
|
struct dma_slave_config txconf = {
|
|
|
|
.direction = DMA_MEM_TO_DEV,
|
|
|
|
.dst_addr = priv->base_dma_addr + SSI_TXDR,
|
|
|
|
.dst_addr_width = buswidth,
|
|
|
|
.dst_maxburst = SSI_FIFO_BURST_NUM,
|
|
|
|
};
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
dmaengine_slave_config(host->dma_tx, &txconf);
|
2019-12-24 08:58:27 +08:00
|
|
|
|
|
|
|
txdesc = dmaengine_prep_slave_sg(
|
2023-11-28 17:30:23 +08:00
|
|
|
host->dma_tx,
|
2019-12-24 08:58:27 +08:00
|
|
|
t->tx_sg.sgl, t->tx_sg.nents,
|
|
|
|
DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!txdesc)
|
|
|
|
goto out_err_prep;
|
|
|
|
|
|
|
|
txdesc->callback = uniphier_spi_dma_txcb;
|
2023-11-28 17:30:23 +08:00
|
|
|
txdesc->callback_param = host;
|
2019-12-24 08:58:27 +08:00
|
|
|
|
|
|
|
uniphier_spi_irq_enable(priv, SSI_IE_TXRE);
|
|
|
|
atomic_or(SSI_DMA_TX_BUSY, &priv->dma_busy);
|
|
|
|
|
|
|
|
dmaengine_submit(txdesc);
|
2023-11-28 17:30:23 +08:00
|
|
|
dma_async_issue_pending(host->dma_tx);
|
2019-12-24 08:58:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* signal that we need to wait for completion */
|
|
|
|
return (priv->tx_buf || priv->rx_buf);
|
|
|
|
|
|
|
|
out_err_prep:
|
|
|
|
if (rxdesc)
|
2023-11-28 17:30:23 +08:00
|
|
|
dmaengine_terminate_sync(host->dma_rx);
|
2019-12-24 08:58:27 +08:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static int uniphier_spi_transfer_one_irq(struct spi_controller *host,
|
2019-09-03 13:31:01 +08:00
|
|
|
struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
|
|
|
struct device *dev = host->dev.parent;
|
2019-06-26 08:41:47 +08:00
|
|
|
unsigned long time_left;
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
reinit_completion(&priv->xfer_done);
|
|
|
|
|
|
|
|
uniphier_spi_fill_tx_fifo(priv);
|
|
|
|
|
2019-12-24 08:58:24 +08:00
|
|
|
uniphier_spi_irq_enable(priv, SSI_IE_RCIE | SSI_IE_RORIE);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2019-06-26 08:41:47 +08:00
|
|
|
time_left = wait_for_completion_timeout(&priv->xfer_done,
|
|
|
|
msecs_to_jiffies(SSI_TIMEOUT_MS));
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2019-12-24 08:58:24 +08:00
|
|
|
uniphier_spi_irq_disable(priv, SSI_IE_RCIE | SSI_IE_RORIE);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2019-06-26 08:41:47 +08:00
|
|
|
if (!time_left) {
|
|
|
|
dev_err(dev, "transfer timeout.\n");
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
return priv->error;
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static int uniphier_spi_transfer_one_poll(struct spi_controller *host,
|
2019-09-03 13:31:01 +08:00
|
|
|
struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-09-03 13:31:01 +08:00
|
|
|
int loop = SSI_POLL_TIMEOUT_US * 10;
|
|
|
|
|
|
|
|
while (priv->tx_bytes) {
|
|
|
|
uniphier_spi_fill_tx_fifo(priv);
|
|
|
|
|
|
|
|
while ((priv->rx_bytes - priv->tx_bytes) > 0) {
|
|
|
|
while (!(readl(priv->base + SSI_SR) & SSI_SR_RNE)
|
|
|
|
&& loop--)
|
|
|
|
ndelay(100);
|
|
|
|
|
|
|
|
if (loop == -1)
|
|
|
|
goto irq_transfer;
|
|
|
|
|
|
|
|
uniphier_spi_recv(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
irq_transfer:
|
2023-11-28 17:30:23 +08:00
|
|
|
return uniphier_spi_transfer_one_irq(host, spi, t);
|
2019-09-03 13:31:01 +08:00
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static int uniphier_spi_transfer_one(struct spi_controller *host,
|
2019-09-03 13:31:01 +08:00
|
|
|
struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-09-03 13:31:01 +08:00
|
|
|
unsigned long threshold;
|
2019-12-24 08:58:27 +08:00
|
|
|
bool use_dma;
|
2019-09-03 13:31:01 +08:00
|
|
|
|
|
|
|
/* Terminate and return success for 0 byte length transfer */
|
|
|
|
if (!t->len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uniphier_spi_setup_transfer(spi, t);
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
use_dma = host->can_dma ? host->can_dma(host, spi, t) : false;
|
2019-12-24 08:58:27 +08:00
|
|
|
if (use_dma)
|
2023-11-28 17:30:23 +08:00
|
|
|
return uniphier_spi_transfer_one_dma(host, spi, t);
|
2019-12-24 08:58:27 +08:00
|
|
|
|
2019-09-03 13:31:01 +08:00
|
|
|
/*
|
|
|
|
* If the transfer operation will take longer than
|
|
|
|
* SSI_POLL_TIMEOUT_US, it should use irq.
|
|
|
|
*/
|
|
|
|
threshold = DIV_ROUND_UP(SSI_POLL_TIMEOUT_US * priv->speed_hz,
|
|
|
|
USEC_PER_SEC * BITS_PER_BYTE);
|
|
|
|
if (t->len > threshold)
|
2023-11-28 17:30:23 +08:00
|
|
|
return uniphier_spi_transfer_one_irq(host, spi, t);
|
2019-09-03 13:31:01 +08:00
|
|
|
else
|
2023-11-28 17:30:23 +08:00
|
|
|
return uniphier_spi_transfer_one_poll(host, spi, t);
|
2019-09-03 13:31:01 +08:00
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static int uniphier_spi_prepare_transfer_hardware(struct spi_controller *host)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
writel(SSI_CTL_EN, priv->base + SSI_CTL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static int uniphier_spi_unprepare_transfer_hardware(struct spi_controller *host)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
writel(0, priv->base + SSI_CTL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
static void uniphier_spi_handle_err(struct spi_controller *host,
|
2019-12-24 08:58:25 +08:00
|
|
|
struct spi_message *msg)
|
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2019-12-24 08:58:25 +08:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
/* stop running spi transfer */
|
|
|
|
writel(0, priv->base + SSI_CTL);
|
|
|
|
|
|
|
|
/* reset FIFOs */
|
|
|
|
val = SSI_FC_TXFFL | SSI_FC_RXFFL;
|
|
|
|
writel(val, priv->base + SSI_FC);
|
|
|
|
|
2019-12-24 08:58:27 +08:00
|
|
|
uniphier_spi_irq_disable(priv, SSI_IE_ALL_MASK);
|
|
|
|
|
|
|
|
if (atomic_read(&priv->dma_busy) & SSI_DMA_TX_BUSY) {
|
2023-11-28 17:30:23 +08:00
|
|
|
dmaengine_terminate_async(host->dma_tx);
|
2019-12-24 08:58:27 +08:00
|
|
|
atomic_andnot(SSI_DMA_TX_BUSY, &priv->dma_busy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_read(&priv->dma_busy) & SSI_DMA_RX_BUSY) {
|
2023-11-28 17:30:23 +08:00
|
|
|
dmaengine_terminate_async(host->dma_rx);
|
2019-12-24 08:58:27 +08:00
|
|
|
atomic_andnot(SSI_DMA_RX_BUSY, &priv->dma_busy);
|
|
|
|
}
|
2019-12-24 08:58:25 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:29:12 +08:00
|
|
|
static irqreturn_t uniphier_spi_handler(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct uniphier_spi_priv *priv = dev_id;
|
|
|
|
u32 val, stat;
|
|
|
|
|
|
|
|
stat = readl(priv->base + SSI_IS);
|
|
|
|
val = SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC;
|
|
|
|
writel(val, priv->base + SSI_IC);
|
|
|
|
|
|
|
|
/* rx fifo overrun */
|
|
|
|
if (stat & SSI_IS_RORID) {
|
|
|
|
priv->error = -EIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rx complete */
|
|
|
|
if ((stat & SSI_IS_RCID) && (stat & SSI_IS_RXRS)) {
|
|
|
|
while ((readl(priv->base + SSI_SR) & SSI_SR_RNE) &&
|
|
|
|
(priv->rx_bytes - priv->tx_bytes) > 0)
|
|
|
|
uniphier_spi_recv(priv);
|
|
|
|
|
|
|
|
if ((readl(priv->base + SSI_SR) & SSI_SR_RNE) ||
|
|
|
|
(priv->rx_bytes != priv->tx_bytes)) {
|
|
|
|
priv->error = -EIO;
|
|
|
|
goto done;
|
|
|
|
} else if (priv->rx_bytes == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* next tx transfer */
|
|
|
|
uniphier_spi_fill_tx_fifo(priv);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_NONE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
complete(&priv->xfer_done);
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uniphier_spi_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct uniphier_spi_priv *priv;
|
2023-11-28 17:30:23 +08:00
|
|
|
struct spi_controller *host;
|
2019-12-24 08:58:27 +08:00
|
|
|
struct resource *res;
|
|
|
|
struct dma_slave_caps caps;
|
|
|
|
u32 dma_tx_burst = 0, dma_rx_burst = 0;
|
2018-08-01 15:29:12 +08:00
|
|
|
unsigned long clk_rate;
|
|
|
|
int irq;
|
|
|
|
int ret;
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host = spi_alloc_host(&pdev->dev, sizeof(*priv));
|
|
|
|
if (!host)
|
2018-08-01 15:29:12 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
platform_set_drvdata(pdev, host);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
priv = spi_controller_get_devdata(host);
|
|
|
|
priv->host = host;
|
2018-08-01 15:29:12 +08:00
|
|
|
priv->is_save_param = false;
|
|
|
|
|
2020-05-11 16:25:30 +08:00
|
|
|
priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
|
2018-08-01 15:29:12 +08:00
|
|
|
if (IS_ERR(priv->base)) {
|
|
|
|
ret = PTR_ERR(priv->base);
|
2023-11-28 17:30:23 +08:00
|
|
|
goto out_host_put;
|
2018-08-01 15:29:12 +08:00
|
|
|
}
|
2019-12-24 08:58:27 +08:00
|
|
|
priv->base_dma_addr = res->start;
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
priv->clk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(priv->clk)) {
|
|
|
|
dev_err(&pdev->dev, "failed to get clock\n");
|
|
|
|
ret = PTR_ERR(priv->clk);
|
2023-11-28 17:30:23 +08:00
|
|
|
goto out_host_put;
|
2018-08-01 15:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(priv->clk);
|
|
|
|
if (ret)
|
2023-11-28 17:30:23 +08:00
|
|
|
goto out_host_put;
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
if (irq < 0) {
|
|
|
|
ret = irq;
|
|
|
|
goto out_disable_clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = devm_request_irq(&pdev->dev, irq, uniphier_spi_handler,
|
|
|
|
0, "uniphier-spi", priv);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "failed to request IRQ\n");
|
|
|
|
goto out_disable_clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_completion(&priv->xfer_done);
|
|
|
|
|
|
|
|
clk_rate = clk_get_rate(priv->clk);
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host->max_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MIN_CLK_DIVIDER);
|
|
|
|
host->min_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MAX_CLK_DIVIDER);
|
|
|
|
host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
|
|
|
|
host->dev.of_node = pdev->dev.of_node;
|
|
|
|
host->bus_num = pdev->id;
|
|
|
|
host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host->set_cs = uniphier_spi_set_cs;
|
|
|
|
host->transfer_one = uniphier_spi_transfer_one;
|
|
|
|
host->prepare_transfer_hardware
|
2018-08-01 15:29:12 +08:00
|
|
|
= uniphier_spi_prepare_transfer_hardware;
|
2023-11-28 17:30:23 +08:00
|
|
|
host->unprepare_transfer_hardware
|
2018-08-01 15:29:12 +08:00
|
|
|
= uniphier_spi_unprepare_transfer_hardware;
|
2023-11-28 17:30:23 +08:00
|
|
|
host->handle_err = uniphier_spi_handle_err;
|
|
|
|
host->can_dma = uniphier_spi_can_dma;
|
2019-12-24 08:58:27 +08:00
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host->num_chipselect = 1;
|
|
|
|
host->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
|
2019-12-24 08:58:27 +08:00
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host->dma_tx = dma_request_chan(&pdev->dev, "tx");
|
|
|
|
if (IS_ERR_OR_NULL(host->dma_tx)) {
|
|
|
|
if (PTR_ERR(host->dma_tx) == -EPROBE_DEFER) {
|
2020-04-29 15:58:55 +08:00
|
|
|
ret = -EPROBE_DEFER;
|
2019-12-24 08:58:27 +08:00
|
|
|
goto out_disable_clk;
|
2020-04-29 15:58:55 +08:00
|
|
|
}
|
2023-11-28 17:30:23 +08:00
|
|
|
host->dma_tx = NULL;
|
2019-12-24 08:58:27 +08:00
|
|
|
dma_tx_burst = INT_MAX;
|
|
|
|
} else {
|
2023-11-28 17:30:23 +08:00
|
|
|
ret = dma_get_slave_caps(host->dma_tx, &caps);
|
2019-12-24 08:58:27 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "failed to get TX DMA capacities: %d\n",
|
|
|
|
ret);
|
2022-01-25 18:12:15 +08:00
|
|
|
goto out_release_dma;
|
2019-12-24 08:58:27 +08:00
|
|
|
}
|
|
|
|
dma_tx_burst = caps.max_burst;
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host->dma_rx = dma_request_chan(&pdev->dev, "rx");
|
|
|
|
if (IS_ERR_OR_NULL(host->dma_rx)) {
|
|
|
|
if (PTR_ERR(host->dma_rx) == -EPROBE_DEFER) {
|
2020-04-29 15:58:55 +08:00
|
|
|
ret = -EPROBE_DEFER;
|
2022-01-25 18:12:15 +08:00
|
|
|
goto out_release_dma;
|
2020-04-29 15:58:55 +08:00
|
|
|
}
|
2023-11-28 17:30:23 +08:00
|
|
|
host->dma_rx = NULL;
|
2019-12-24 08:58:27 +08:00
|
|
|
dma_rx_burst = INT_MAX;
|
|
|
|
} else {
|
2023-11-28 17:30:23 +08:00
|
|
|
ret = dma_get_slave_caps(host->dma_rx, &caps);
|
2019-12-24 08:58:27 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "failed to get RX DMA capacities: %d\n",
|
|
|
|
ret);
|
2022-01-25 18:12:15 +08:00
|
|
|
goto out_release_dma;
|
2019-12-24 08:58:27 +08:00
|
|
|
}
|
|
|
|
dma_rx_burst = caps.max_burst;
|
|
|
|
}
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
host->max_dma_len = min(dma_tx_burst, dma_rx_burst);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
ret = devm_spi_register_controller(&pdev->dev, host);
|
2018-08-01 15:29:12 +08:00
|
|
|
if (ret)
|
2022-01-25 18:12:15 +08:00
|
|
|
goto out_release_dma;
|
2018-08-01 15:29:12 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2022-01-25 18:12:15 +08:00
|
|
|
out_release_dma:
|
2023-11-28 17:30:23 +08:00
|
|
|
if (!IS_ERR_OR_NULL(host->dma_rx)) {
|
|
|
|
dma_release_channel(host->dma_rx);
|
|
|
|
host->dma_rx = NULL;
|
2022-01-25 18:12:15 +08:00
|
|
|
}
|
2023-11-28 17:30:23 +08:00
|
|
|
if (!IS_ERR_OR_NULL(host->dma_tx)) {
|
|
|
|
dma_release_channel(host->dma_tx);
|
|
|
|
host->dma_tx = NULL;
|
2022-01-25 18:12:15 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:29:12 +08:00
|
|
|
out_disable_clk:
|
|
|
|
clk_disable_unprepare(priv->clk);
|
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
out_host_put:
|
|
|
|
spi_controller_put(host);
|
2018-08-01 15:29:12 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-04 01:20:37 +08:00
|
|
|
static void uniphier_spi_remove(struct platform_device *pdev)
|
2018-08-01 15:29:12 +08:00
|
|
|
{
|
2023-11-28 17:30:23 +08:00
|
|
|
struct spi_controller *host = platform_get_drvdata(pdev);
|
|
|
|
struct uniphier_spi_priv *priv = spi_controller_get_devdata(host);
|
2018-08-01 15:29:12 +08:00
|
|
|
|
2023-11-28 17:30:23 +08:00
|
|
|
if (host->dma_tx)
|
|
|
|
dma_release_channel(host->dma_tx);
|
|
|
|
if (host->dma_rx)
|
|
|
|
dma_release_channel(host->dma_rx);
|
2019-12-24 08:58:27 +08:00
|
|
|
|
2018-08-01 15:29:12 +08:00
|
|
|
clk_disable_unprepare(priv->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id uniphier_spi_match[] = {
|
|
|
|
{ .compatible = "socionext,uniphier-scssi" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, uniphier_spi_match);
|
|
|
|
|
|
|
|
static struct platform_driver uniphier_spi_driver = {
|
|
|
|
.probe = uniphier_spi_probe,
|
2023-03-04 01:20:37 +08:00
|
|
|
.remove_new = uniphier_spi_remove,
|
2018-08-01 15:29:12 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "uniphier-spi",
|
|
|
|
.of_match_table = uniphier_spi_match,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
module_platform_driver(uniphier_spi_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
|
|
|
|
MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
|
|
|
|
MODULE_DESCRIPTION("Socionext UniPhier SPI controller driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|