2019-05-29 22:18:02 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-12-15 06:20:22 +08:00
|
|
|
/*
|
2011-06-06 15:16:30 +08:00
|
|
|
* Designware SPI core controller driver (refer pxa2xx_spi.c)
|
2009-12-15 06:20:22 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2009, Intel Corporation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/interrupt.h>
|
2011-07-04 03:44:29 +08:00
|
|
|
#include <linux/module.h>
|
2009-12-15 06:20:22 +08:00
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/delay.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-12-15 06:20:22 +08:00
|
|
|
#include <linux/spi/spi.h>
|
2020-08-25 04:30:05 +08:00
|
|
|
#include <linux/of.h>
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2011-06-06 15:16:30 +08:00
|
|
|
#include "spi-dw.h"
|
2011-03-01 03:47:12 +08:00
|
|
|
|
2009-12-15 06:20:22 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Slave spi_dev related */
|
|
|
|
struct chip_data {
|
|
|
|
u8 tmode; /* TR/TO/RO/EEPROM */
|
|
|
|
|
2020-10-08 07:54:52 +08:00
|
|
|
u32 cr0;
|
2020-08-25 04:30:05 +08:00
|
|
|
u32 rx_sample_dly; /* RX sample delay */
|
2009-12-15 06:20:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
2020-05-29 21:12:04 +08:00
|
|
|
|
|
|
|
#define DW_SPI_DBGFS_REG(_name, _off) \
|
|
|
|
{ \
|
|
|
|
.name = _name, \
|
|
|
|
.offset = _off, \
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:12:04 +08:00
|
|
|
static const struct debugfs_reg32 dw_spi_dbgfs_regs[] = {
|
|
|
|
DW_SPI_DBGFS_REG("CTRLR0", DW_SPI_CTRLR0),
|
|
|
|
DW_SPI_DBGFS_REG("CTRLR1", DW_SPI_CTRLR1),
|
|
|
|
DW_SPI_DBGFS_REG("SSIENR", DW_SPI_SSIENR),
|
|
|
|
DW_SPI_DBGFS_REG("SER", DW_SPI_SER),
|
|
|
|
DW_SPI_DBGFS_REG("BAUDR", DW_SPI_BAUDR),
|
|
|
|
DW_SPI_DBGFS_REG("TXFTLR", DW_SPI_TXFTLR),
|
|
|
|
DW_SPI_DBGFS_REG("RXFTLR", DW_SPI_RXFTLR),
|
|
|
|
DW_SPI_DBGFS_REG("TXFLR", DW_SPI_TXFLR),
|
|
|
|
DW_SPI_DBGFS_REG("RXFLR", DW_SPI_RXFLR),
|
|
|
|
DW_SPI_DBGFS_REG("SR", DW_SPI_SR),
|
|
|
|
DW_SPI_DBGFS_REG("IMR", DW_SPI_IMR),
|
|
|
|
DW_SPI_DBGFS_REG("ISR", DW_SPI_ISR),
|
|
|
|
DW_SPI_DBGFS_REG("DMACR", DW_SPI_DMACR),
|
|
|
|
DW_SPI_DBGFS_REG("DMATDLR", DW_SPI_DMATDLR),
|
|
|
|
DW_SPI_DBGFS_REG("DMARDLR", DW_SPI_DMARDLR),
|
2020-08-25 04:30:05 +08:00
|
|
|
DW_SPI_DBGFS_REG("RX_SAMPLE_DLY", DW_SPI_RX_SAMPLE_DLY),
|
2009-12-15 06:20:22 +08:00
|
|
|
};
|
|
|
|
|
2014-09-12 20:11:56 +08:00
|
|
|
static int dw_spi_debugfs_init(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2017-01-06 17:35:13 +08:00
|
|
|
char name[32];
|
2016-12-22 17:18:12 +08:00
|
|
|
|
2017-01-06 17:35:13 +08:00
|
|
|
snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
|
2016-12-22 17:18:12 +08:00
|
|
|
dws->debugfs = debugfs_create_dir(name, NULL);
|
2009-12-15 06:20:22 +08:00
|
|
|
if (!dws->debugfs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-05-29 21:12:04 +08:00
|
|
|
dws->regset.regs = dw_spi_dbgfs_regs;
|
|
|
|
dws->regset.nregs = ARRAY_SIZE(dw_spi_dbgfs_regs);
|
|
|
|
dws->regset.base = dws->regs;
|
|
|
|
debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
|
|
|
|
|
2009-12-15 06:20:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-12 20:11:56 +08:00
|
|
|
static void dw_spi_debugfs_remove(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2014-09-02 10:49:24 +08:00
|
|
|
debugfs_remove_recursive(dws->debugfs);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2014-09-12 20:11:56 +08:00
|
|
|
static inline int dw_spi_debugfs_init(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2010-01-21 19:40:49 +08:00
|
|
|
return 0;
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 20:11:56 +08:00
|
|
|
static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DEBUG_FS */
|
|
|
|
|
2018-07-28 03:53:54 +08:00
|
|
|
void dw_spi_set_cs(struct spi_device *spi, bool enable)
|
2015-03-02 20:58:57 +08:00
|
|
|
{
|
2018-02-01 23:17:29 +08:00
|
|
|
struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
|
2020-05-15 18:47:43 +08:00
|
|
|
bool cs_high = !!(spi->mode & SPI_CS_HIGH);
|
2015-03-02 20:58:57 +08:00
|
|
|
|
2020-05-15 18:47:43 +08:00
|
|
|
/*
|
|
|
|
* DW SPI controller demands any native CS being set in order to
|
|
|
|
* proceed with data transfer. So in order to activate the SPI
|
|
|
|
* communications we must set a corresponding bit in the Slave
|
|
|
|
* Enable register no matter whether the SPI core is configured to
|
|
|
|
* support active-high or active-low CS level.
|
|
|
|
*/
|
|
|
|
if (cs_high == enable)
|
2015-03-02 20:58:57 +08:00
|
|
|
dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
|
2020-09-20 19:28:53 +08:00
|
|
|
else if (dws->caps & DW_SPI_CAP_CS_OVERRIDE)
|
2018-10-11 19:20:07 +08:00
|
|
|
dw_writel(dws, DW_SPI_SER, 0);
|
2015-03-02 20:58:57 +08:00
|
|
|
}
|
2018-07-28 03:53:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(dw_spi_set_cs);
|
2015-03-02 20:58:57 +08:00
|
|
|
|
2011-03-30 23:09:54 +08:00
|
|
|
/* Return the max entries we can fill into tx fifo */
|
|
|
|
static inline u32 tx_max(struct dw_spi *dws)
|
|
|
|
{
|
|
|
|
u32 tx_left, tx_room, rxtx_gap;
|
|
|
|
|
|
|
|
tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
|
2015-03-13 03:19:31 +08:00
|
|
|
tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
|
2011-03-30 23:09:54 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Another concern is about the tx/rx mismatch, we
|
|
|
|
* though to use (dws->fifo_len - rxflr - txflr) as
|
|
|
|
* one maximum value for tx, but it doesn't cover the
|
|
|
|
* data which is out of tx/rx fifo and inside the
|
|
|
|
* shift registers. So a control from sw point of
|
|
|
|
* view is taken.
|
|
|
|
*/
|
|
|
|
rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
|
|
|
|
/ dws->n_bytes;
|
|
|
|
|
|
|
|
return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the max entries we should read out of rx fifo */
|
|
|
|
static inline u32 rx_max(struct dw_spi *dws)
|
|
|
|
{
|
|
|
|
u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
|
|
|
|
|
2015-03-13 03:19:31 +08:00
|
|
|
return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
|
2011-03-30 23:09:54 +08:00
|
|
|
}
|
|
|
|
|
2011-03-30 23:09:55 +08:00
|
|
|
static void dw_writer(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2020-09-20 19:28:48 +08:00
|
|
|
u32 max = tx_max(dws);
|
2011-03-30 23:09:52 +08:00
|
|
|
u16 txw = 0;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2011-03-30 23:09:54 +08:00
|
|
|
while (max--) {
|
|
|
|
/* Set the tx word if the transfer's original "tx" is not null */
|
|
|
|
if (dws->tx_end - dws->len) {
|
|
|
|
if (dws->n_bytes == 1)
|
|
|
|
txw = *(u8 *)(dws->tx);
|
|
|
|
else
|
|
|
|
txw = *(u16 *)(dws->tx);
|
|
|
|
}
|
2015-08-19 04:21:53 +08:00
|
|
|
dw_write_io_reg(dws, DW_SPI_DR, txw);
|
2011-03-30 23:09:54 +08:00
|
|
|
dws->tx += dws->n_bytes;
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-30 23:09:55 +08:00
|
|
|
static void dw_reader(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2020-09-20 19:28:48 +08:00
|
|
|
u32 max = rx_max(dws);
|
2011-03-30 23:09:52 +08:00
|
|
|
u16 rxw;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2011-03-30 23:09:54 +08:00
|
|
|
while (max--) {
|
2015-08-19 04:21:53 +08:00
|
|
|
rxw = dw_read_io_reg(dws, DW_SPI_DR);
|
2011-03-30 23:09:52 +08:00
|
|
|
/* Care rx only if the transfer's original "rx" is not null */
|
|
|
|
if (dws->rx_end - dws->len) {
|
|
|
|
if (dws->n_bytes == 1)
|
|
|
|
*(u8 *)(dws->rx) = rxw;
|
|
|
|
else
|
|
|
|
*(u16 *)(dws->rx) = rxw;
|
|
|
|
}
|
|
|
|
dws->rx += dws->n_bytes;
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void int_error_stop(struct dw_spi *dws, const char *msg)
|
|
|
|
{
|
2015-03-02 20:58:55 +08:00
|
|
|
spi_reset_chip(dws);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
|
|
|
dev_err(&dws->master->dev, "%s\n", msg);
|
2015-03-02 20:58:57 +08:00
|
|
|
dws->master->cur_msg->status = -EIO;
|
|
|
|
spi_finalize_current_transfer(dws->master);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t interrupt_transfer(struct dw_spi *dws)
|
|
|
|
{
|
2015-03-13 03:19:31 +08:00
|
|
|
u16 irq_status = dw_readl(dws, DW_SPI_ISR);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
|
|
|
/* Error handling */
|
|
|
|
if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
|
2015-03-13 03:19:31 +08:00
|
|
|
dw_readl(dws, DW_SPI_ICR);
|
2011-03-30 23:09:55 +08:00
|
|
|
int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
|
2009-12-15 06:20:22 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2011-03-30 23:09:55 +08:00
|
|
|
dw_reader(dws);
|
|
|
|
if (dws->rx_end == dws->rx) {
|
2020-09-20 19:28:50 +08:00
|
|
|
spi_mask_intr(dws, 0xff);
|
2015-03-02 20:58:57 +08:00
|
|
|
spi_finalize_current_transfer(dws->master);
|
2011-03-30 23:09:55 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
2010-01-21 04:49:45 +08:00
|
|
|
if (irq_status & SPI_INT_TXEI) {
|
|
|
|
spi_mask_intr(dws, SPI_INT_TXEI);
|
2011-03-30 23:09:55 +08:00
|
|
|
dw_writer(dws);
|
|
|
|
/* Enable TX irq always, it will be disabled when RX finished */
|
|
|
|
spi_umask_intr(dws, SPI_INT_TXEI);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t dw_spi_irq(int irq, void *dev_id)
|
|
|
|
{
|
2018-02-01 23:17:29 +08:00
|
|
|
struct spi_controller *master = dev_id;
|
|
|
|
struct dw_spi *dws = spi_controller_get_devdata(master);
|
2015-03-13 03:19:31 +08:00
|
|
|
u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
|
2010-09-07 15:27:27 +08:00
|
|
|
|
|
|
|
if (!irq_status)
|
|
|
|
return IRQ_NONE;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2015-03-02 20:58:57 +08:00
|
|
|
if (!master->cur_msg) {
|
2020-09-20 19:28:50 +08:00
|
|
|
spi_mask_intr(dws, 0xff);
|
2009-12-15 06:20:22 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dws->transfer_handler(dws);
|
|
|
|
}
|
|
|
|
|
2020-10-08 07:54:52 +08:00
|
|
|
static u32 dw_spi_prepare_cr0(struct dw_spi *dws, struct spi_device *spi)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2020-10-08 07:54:52 +08:00
|
|
|
u32 cr0 = 0;
|
2020-05-05 21:06:13 +08:00
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
if (!(dws->caps & DW_SPI_CAP_DWC_SSI)) {
|
|
|
|
/* CTRLR0[ 5: 4] Frame Format */
|
|
|
|
cr0 |= SSI_MOTO_SPI << SPI_FRF_OFFSET;
|
2020-05-05 21:06:13 +08:00
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
/*
|
|
|
|
* SPI mode (SCPOL|SCPH)
|
|
|
|
* CTRLR0[ 6] Serial Clock Phase
|
|
|
|
* CTRLR0[ 7] Serial Clock Polarity
|
|
|
|
*/
|
|
|
|
cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET;
|
|
|
|
cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET;
|
2020-05-05 21:06:14 +08:00
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
/* CTRLR0[11] Shift Register Loop */
|
|
|
|
cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET;
|
|
|
|
} else {
|
|
|
|
/* CTRLR0[ 7: 6] Frame Format */
|
|
|
|
cr0 |= SSI_MOTO_SPI << DWC_SSI_CTRLR0_FRF_OFFSET;
|
2020-05-05 21:06:14 +08:00
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
/*
|
|
|
|
* SPI mode (SCPOL|SCPH)
|
|
|
|
* CTRLR0[ 8] Serial Clock Phase
|
|
|
|
* CTRLR0[ 9] Serial Clock Polarity
|
|
|
|
*/
|
|
|
|
cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << DWC_SSI_CTRLR0_SCPOL_OFFSET;
|
|
|
|
cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << DWC_SSI_CTRLR0_SCPH_OFFSET;
|
2020-05-05 21:06:14 +08:00
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
/* CTRLR0[13] Shift Register Loop */
|
|
|
|
cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << DWC_SSI_CTRLR0_SRL_OFFSET;
|
2020-05-05 21:06:14 +08:00
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
if (dws->caps & DW_SPI_CAP_KEEMBAY_MST)
|
|
|
|
cr0 |= DWC_SSI_CTRLR0_KEEMBAY_MST;
|
|
|
|
}
|
2020-09-20 19:28:54 +08:00
|
|
|
|
2020-10-08 07:54:52 +08:00
|
|
|
return cr0;
|
|
|
|
}
|
|
|
|
|
2020-10-08 07:54:53 +08:00
|
|
|
static void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi,
|
|
|
|
struct spi_transfer *transfer)
|
2020-10-08 07:54:52 +08:00
|
|
|
{
|
|
|
|
struct chip_data *chip = spi_get_ctldata(spi);
|
|
|
|
u32 cr0 = chip->cr0;
|
2020-10-08 07:54:54 +08:00
|
|
|
u32 speed_hz;
|
|
|
|
u16 clk_div;
|
2020-10-08 07:54:52 +08:00
|
|
|
|
|
|
|
/* CTRLR0[ 4/3: 0] Data Frame Size */
|
|
|
|
cr0 |= (transfer->bits_per_word - 1);
|
|
|
|
|
|
|
|
if (!(dws->caps & DW_SPI_CAP_DWC_SSI))
|
|
|
|
/* CTRLR0[ 9:8] Transfer Mode */
|
|
|
|
cr0 |= chip->tmode << SPI_TMOD_OFFSET;
|
|
|
|
else
|
|
|
|
/* CTRLR0[11:10] Transfer Mode */
|
|
|
|
cr0 |= chip->tmode << DWC_SSI_CTRLR0_TMOD_OFFSET;
|
|
|
|
|
2020-10-08 07:54:51 +08:00
|
|
|
dw_writel(dws, DW_SPI_CTRLR0, cr0);
|
2020-10-08 07:54:53 +08:00
|
|
|
|
2020-10-08 07:54:54 +08:00
|
|
|
/* Note DW APB SSI clock divider doesn't support odd numbers */
|
|
|
|
clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
|
|
|
|
speed_hz = dws->max_freq / clk_div;
|
|
|
|
|
|
|
|
if (dws->current_freq != speed_hz) {
|
|
|
|
spi_set_clk(dws, clk_div);
|
|
|
|
dws->current_freq = speed_hz;
|
2020-10-08 07:54:53 +08:00
|
|
|
}
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 23:17:29 +08:00
|
|
|
static int dw_spi_transfer_one(struct spi_controller *master,
|
2015-03-02 20:58:57 +08:00
|
|
|
struct spi_device *spi, struct spi_transfer *transfer)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2018-02-01 23:17:29 +08:00
|
|
|
struct dw_spi *dws = spi_controller_get_devdata(master);
|
2015-03-02 20:58:57 +08:00
|
|
|
struct chip_data *chip = spi_get_ctldata(spi);
|
2009-12-15 06:20:22 +08:00
|
|
|
u8 imask = 0;
|
2015-02-24 19:32:11 +08:00
|
|
|
u16 txlevel = 0;
|
2015-03-09 22:48:46 +08:00
|
|
|
int ret;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2015-03-09 22:48:49 +08:00
|
|
|
dws->dma_mapped = 0;
|
2020-09-20 19:28:47 +08:00
|
|
|
dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
|
2009-12-15 06:20:22 +08:00
|
|
|
dws->tx = (void *)transfer->tx_buf;
|
|
|
|
dws->tx_end = dws->tx + transfer->len;
|
|
|
|
dws->rx = transfer->rx_buf;
|
|
|
|
dws->rx_end = dws->rx + transfer->len;
|
2015-03-02 20:58:57 +08:00
|
|
|
dws->len = transfer->len;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2020-01-03 10:52:10 +08:00
|
|
|
/* Ensure dw->rx and dw->rx_end are visible */
|
|
|
|
smp_mb();
|
|
|
|
|
2015-03-02 20:58:56 +08:00
|
|
|
spi_enable_chip(dws, 0);
|
|
|
|
|
2020-10-08 07:54:53 +08:00
|
|
|
dw_spi_update_config(dws, spi, transfer);
|
2018-09-05 03:49:44 +08:00
|
|
|
|
2020-10-08 07:54:54 +08:00
|
|
|
transfer->effective_speed_hz = dws->current_freq;
|
2010-01-21 19:40:52 +08:00
|
|
|
|
2009-12-15 06:20:22 +08:00
|
|
|
/* Check if current transfer is a DMA transaction */
|
2015-03-09 22:48:49 +08:00
|
|
|
if (master->can_dma && master->can_dma(master, spi, transfer))
|
|
|
|
dws->dma_mapped = master->cur_msg_mapped;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2020-08-25 04:30:05 +08:00
|
|
|
/* Update RX sample delay if required */
|
|
|
|
if (dws->cur_rx_sample_dly != chip->rx_sample_dly) {
|
|
|
|
dw_writel(dws, DW_SPI_RX_SAMPLE_DLY, chip->rx_sample_dly);
|
|
|
|
dws->cur_rx_sample_dly = chip->rx_sample_dly;
|
|
|
|
}
|
|
|
|
|
2015-03-02 20:58:56 +08:00
|
|
|
/* For poll mode just disable all interrupts */
|
|
|
|
spi_mask_intr(dws, 0xff);
|
|
|
|
|
2010-01-21 04:49:45 +08:00
|
|
|
/*
|
|
|
|
* Interrupt mode
|
|
|
|
* we only need set the TXEI IRQ, as TX/RX always happen syncronizely
|
|
|
|
*/
|
2015-03-09 22:48:46 +08:00
|
|
|
if (dws->dma_mapped) {
|
2015-03-09 22:48:49 +08:00
|
|
|
ret = dws->dma_ops->dma_setup(dws, transfer);
|
2015-03-09 22:48:46 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
spi_enable_chip(dws, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-04-16 19:09:16 +08:00
|
|
|
} else {
|
2015-02-24 19:32:11 +08:00
|
|
|
txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
|
2020-05-05 21:06:12 +08:00
|
|
|
dw_writel(dws, DW_SPI_TXFTLR, txlevel);
|
2010-01-21 04:49:45 +08:00
|
|
|
|
2015-03-02 20:58:56 +08:00
|
|
|
/* Set the interrupt mask */
|
2014-09-02 10:49:24 +08:00
|
|
|
imask |= SPI_INT_TXEI | SPI_INT_TXOI |
|
|
|
|
SPI_INT_RXUI | SPI_INT_RXOI;
|
2015-03-02 20:58:56 +08:00
|
|
|
spi_umask_intr(dws, imask);
|
|
|
|
|
2009-12-15 06:20:22 +08:00
|
|
|
dws->transfer_handler = interrupt_transfer;
|
|
|
|
}
|
|
|
|
|
2015-03-02 20:58:56 +08:00
|
|
|
spi_enable_chip(dws, 1);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2020-05-29 21:11:51 +08:00
|
|
|
if (dws->dma_mapped)
|
|
|
|
return dws->dma_ops->dma_transfer(dws, transfer);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2015-03-02 20:58:57 +08:00
|
|
|
return 1;
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 23:17:29 +08:00
|
|
|
static void dw_spi_handle_err(struct spi_controller *master,
|
2014-01-31 18:07:44 +08:00
|
|
|
struct spi_message *msg)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2018-02-01 23:17:29 +08:00
|
|
|
struct dw_spi *dws = spi_controller_get_devdata(master);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2015-03-09 22:48:48 +08:00
|
|
|
if (dws->dma_mapped)
|
|
|
|
dws->dma_ops->dma_stop(dws);
|
|
|
|
|
2015-03-02 20:58:57 +08:00
|
|
|
spi_reset_chip(dws);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This may be called twice for each spi dev */
|
|
|
|
static int dw_spi_setup(struct spi_device *spi)
|
|
|
|
{
|
2020-10-08 07:54:52 +08:00
|
|
|
struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
|
2009-12-15 06:20:22 +08:00
|
|
|
struct chip_data *chip;
|
|
|
|
|
|
|
|
/* Only alloc on first setup */
|
|
|
|
chip = spi_get_ctldata(spi);
|
|
|
|
if (!chip) {
|
2020-08-25 04:30:05 +08:00
|
|
|
struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
|
|
|
|
u32 rx_sample_dly_ns;
|
|
|
|
|
2014-08-31 12:47:06 +08:00
|
|
|
chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
|
2009-12-15 06:20:22 +08:00
|
|
|
if (!chip)
|
|
|
|
return -ENOMEM;
|
2013-12-31 02:30:46 +08:00
|
|
|
spi_set_ctldata(spi, chip);
|
2020-08-25 04:30:05 +08:00
|
|
|
/* Get specific / default rx-sample-delay */
|
|
|
|
if (device_property_read_u32(&spi->dev,
|
|
|
|
"rx-sample-delay-ns",
|
|
|
|
&rx_sample_dly_ns) != 0)
|
|
|
|
/* Use default controller value */
|
|
|
|
rx_sample_dly_ns = dws->def_rx_sample_dly_ns;
|
|
|
|
chip->rx_sample_dly = DIV_ROUND_CLOSEST(rx_sample_dly_ns,
|
|
|
|
NSEC_PER_SEC /
|
|
|
|
dws->max_freq);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2020-10-08 07:54:52 +08:00
|
|
|
/*
|
|
|
|
* Update CR0 data each time the setup callback is invoked since
|
|
|
|
* the device parameters could have been changed, for instance, by
|
|
|
|
* the MMC SPI driver or something else.
|
|
|
|
*/
|
|
|
|
chip->cr0 = dw_spi_prepare_cr0(dws, spi);
|
|
|
|
|
2015-12-23 19:05:39 +08:00
|
|
|
chip->tmode = SPI_TMOD_TR;
|
2014-09-19 01:08:56 +08:00
|
|
|
|
2009-12-15 06:20:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-31 12:47:06 +08:00
|
|
|
static void dw_spi_cleanup(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
struct chip_data *chip = spi_get_ctldata(spi);
|
|
|
|
|
|
|
|
kfree(chip);
|
|
|
|
spi_set_ctldata(spi, NULL);
|
|
|
|
}
|
|
|
|
|
2009-12-15 06:20:22 +08:00
|
|
|
/* Restart the controller, disable all interrupts, clean rx fifo */
|
2015-01-07 22:56:55 +08:00
|
|
|
static void spi_hw_init(struct device *dev, struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2015-03-02 20:58:55 +08:00
|
|
|
spi_reset_chip(dws);
|
2010-01-21 10:41:10 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to detect the FIFO depth if not set by interface driver,
|
|
|
|
* the depth could be from 2 to 256 from HW spec
|
|
|
|
*/
|
|
|
|
if (!dws->fifo_len) {
|
|
|
|
u32 fifo;
|
2014-09-02 10:49:24 +08:00
|
|
|
|
2015-02-25 17:39:36 +08:00
|
|
|
for (fifo = 1; fifo < 256; fifo++) {
|
2020-05-05 21:06:12 +08:00
|
|
|
dw_writel(dws, DW_SPI_TXFTLR, fifo);
|
|
|
|
if (fifo != dw_readl(dws, DW_SPI_TXFTLR))
|
2010-01-21 10:41:10 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-05-05 21:06:12 +08:00
|
|
|
dw_writel(dws, DW_SPI_TXFTLR, 0);
|
2010-01-21 10:41:10 +08:00
|
|
|
|
2015-02-25 17:39:36 +08:00
|
|
|
dws->fifo_len = (fifo == 1) ? 0 : fifo;
|
2015-01-07 22:56:55 +08:00
|
|
|
dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
|
2010-01-21 10:41:10 +08:00
|
|
|
}
|
2018-10-11 19:20:07 +08:00
|
|
|
|
|
|
|
/* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
|
2020-09-20 19:28:53 +08:00
|
|
|
if (dws->caps & DW_SPI_CAP_CS_OVERRIDE)
|
2018-10-11 19:20:07 +08:00
|
|
|
dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2013-12-31 02:30:44 +08:00
|
|
|
int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2018-02-01 23:17:29 +08:00
|
|
|
struct spi_controller *master;
|
2009-12-15 06:20:22 +08:00
|
|
|
int ret;
|
|
|
|
|
2019-12-06 07:14:21 +08:00
|
|
|
if (!dws)
|
|
|
|
return -EINVAL;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2013-12-31 02:30:44 +08:00
|
|
|
master = spi_alloc_master(dev, 0);
|
|
|
|
if (!master)
|
|
|
|
return -ENOMEM;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
|
|
|
dws->master = master;
|
2015-10-27 23:48:16 +08:00
|
|
|
dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2018-07-17 22:23:10 +08:00
|
|
|
spi_controller_set_devdata(master, dws);
|
|
|
|
|
2020-09-20 19:28:50 +08:00
|
|
|
/* Basic HW init */
|
|
|
|
spi_hw_init(dev, dws);
|
|
|
|
|
2017-01-06 17:35:13 +08:00
|
|
|
ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
|
|
|
|
master);
|
2009-12-15 06:20:22 +08:00
|
|
|
if (ret < 0) {
|
2015-10-15 04:12:17 +08:00
|
|
|
dev_err(dev, "can not get IRQ\n");
|
2009-12-15 06:20:22 +08:00
|
|
|
goto err_free_master;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:51:56 +08:00
|
|
|
master->use_gpio_descriptors = true;
|
2014-09-19 01:08:56 +08:00
|
|
|
master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
|
2018-09-05 03:49:44 +08:00
|
|
|
master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
|
2009-12-15 06:20:22 +08:00
|
|
|
master->bus_num = dws->bus_num;
|
|
|
|
master->num_chipselect = dws->num_cs;
|
|
|
|
master->setup = dw_spi_setup;
|
2014-08-31 12:47:06 +08:00
|
|
|
master->cleanup = dw_spi_cleanup;
|
2020-10-08 07:54:50 +08:00
|
|
|
if (dws->set_cs)
|
|
|
|
master->set_cs = dws->set_cs;
|
|
|
|
else
|
|
|
|
master->set_cs = dw_spi_set_cs;
|
2015-03-02 20:58:57 +08:00
|
|
|
master->transfer_one = dw_spi_transfer_one;
|
|
|
|
master->handle_err = dw_spi_handle_err;
|
2014-02-20 21:37:56 +08:00
|
|
|
master->max_speed_hz = dws->max_freq;
|
2014-10-09 02:51:34 +08:00
|
|
|
master->dev.of_node = dev->of_node;
|
2018-12-03 11:15:50 +08:00
|
|
|
master->dev.fwnode = dev->fwnode;
|
2016-10-10 22:25:25 +08:00
|
|
|
master->flags = SPI_MASTER_GPIO_SS;
|
2019-09-18 16:04:35 +08:00
|
|
|
master->auto_runtime_pm = true;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2020-08-25 04:30:05 +08:00
|
|
|
/* Get default rx sample delay */
|
|
|
|
device_property_read_u32(dev, "rx-sample-delay-ns",
|
|
|
|
&dws->def_rx_sample_dly_ns);
|
|
|
|
|
2010-12-24 13:59:11 +08:00
|
|
|
if (dws->dma_ops && dws->dma_ops->dma_init) {
|
2020-05-06 23:30:24 +08:00
|
|
|
ret = dws->dma_ops->dma_init(dev, dws);
|
2010-12-24 13:59:11 +08:00
|
|
|
if (ret) {
|
2015-01-07 22:56:54 +08:00
|
|
|
dev_warn(dev, "DMA init failed\n");
|
2015-03-09 22:48:49 +08:00
|
|
|
} else {
|
|
|
|
master->can_dma = dws->dma_ops->can_dma;
|
2020-05-29 21:11:57 +08:00
|
|
|
master->flags |= SPI_CONTROLLER_MUST_TX;
|
2010-12-24 13:59:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 20:25:01 +08:00
|
|
|
ret = spi_register_controller(master);
|
2009-12-15 06:20:22 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(&master->dev, "problem registering spi master\n");
|
2014-01-31 18:07:44 +08:00
|
|
|
goto err_dma_exit;
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 20:11:56 +08:00
|
|
|
dw_spi_debugfs_init(dws);
|
2009-12-15 06:20:22 +08:00
|
|
|
return 0;
|
|
|
|
|
2014-01-31 18:07:44 +08:00
|
|
|
err_dma_exit:
|
2010-12-24 13:59:11 +08:00
|
|
|
if (dws->dma_ops && dws->dma_ops->dma_exit)
|
|
|
|
dws->dma_ops->dma_exit(dws);
|
2009-12-15 06:20:22 +08:00
|
|
|
spi_enable_chip(dws, 0);
|
2015-10-20 17:11:40 +08:00
|
|
|
free_irq(dws->irq, master);
|
2009-12-15 06:20:22 +08:00
|
|
|
err_free_master:
|
2018-02-01 23:17:29 +08:00
|
|
|
spi_controller_put(master);
|
2009-12-15 06:20:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2010-12-24 13:59:10 +08:00
|
|
|
EXPORT_SYMBOL_GPL(dw_spi_add_host);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2012-12-08 00:57:14 +08:00
|
|
|
void dw_spi_remove_host(struct dw_spi *dws)
|
2009-12-15 06:20:22 +08:00
|
|
|
{
|
2014-09-12 20:11:56 +08:00
|
|
|
dw_spi_debugfs_remove(dws);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2020-05-25 20:25:01 +08:00
|
|
|
spi_unregister_controller(dws->master);
|
|
|
|
|
2010-12-24 13:59:11 +08:00
|
|
|
if (dws->dma_ops && dws->dma_ops->dma_exit)
|
|
|
|
dws->dma_ops->dma_exit(dws);
|
2015-10-15 04:12:23 +08:00
|
|
|
|
|
|
|
spi_shutdown_chip(dws);
|
2015-10-20 17:11:40 +08:00
|
|
|
|
|
|
|
free_irq(dws->irq, dws->master);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
2010-12-24 13:59:10 +08:00
|
|
|
EXPORT_SYMBOL_GPL(dw_spi_remove_host);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
|
|
|
int dw_spi_suspend_host(struct dw_spi *dws)
|
|
|
|
{
|
2015-10-15 04:12:23 +08:00
|
|
|
int ret;
|
2009-12-15 06:20:22 +08:00
|
|
|
|
2018-02-01 23:17:29 +08:00
|
|
|
ret = spi_controller_suspend(dws->master);
|
2009-12-15 06:20:22 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-10-15 04:12:23 +08:00
|
|
|
|
|
|
|
spi_shutdown_chip(dws);
|
|
|
|
return 0;
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
2010-12-24 13:59:10 +08:00
|
|
|
EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
|
|
|
int dw_spi_resume_host(struct dw_spi *dws)
|
|
|
|
{
|
2015-01-07 22:56:55 +08:00
|
|
|
spi_hw_init(&dws->master->dev, dws);
|
2018-09-05 16:51:57 +08:00
|
|
|
return spi_controller_resume(dws->master);
|
2009-12-15 06:20:22 +08:00
|
|
|
}
|
2010-12-24 13:59:10 +08:00
|
|
|
EXPORT_SYMBOL_GPL(dw_spi_resume_host);
|
2009-12-15 06:20:22 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
|
|
|
|
MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
|
|
|
|
MODULE_LICENSE("GPL v2");
|