2009-12-17 06:02:18 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Texas Instruments.
|
2010-10-06 21:04:47 +08:00
|
|
|
* Copyright (C) 2010 EF Johnson Technologies
|
2009-12-17 06:02:18 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk.h>
|
2012-08-23 09:09:36 +08:00
|
|
|
#include <linux/dmaengine.h>
|
2009-12-17 06:02:18 +08:00
|
|
|
#include <linux/dma-mapping.h>
|
2012-12-12 05:20:39 +08:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2014-08-02 00:40:32 +08:00
|
|
|
#include <linux/of_gpio.h>
|
2009-12-17 06:02:18 +08:00
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
#include <linux/spi/spi_bitbang.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-17 06:02:18 +08:00
|
|
|
|
2012-08-24 21:11:34 +08:00
|
|
|
#include <linux/platform_data/spi-davinci.h>
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
#define CS_DEFAULT 0xFF
|
|
|
|
|
|
|
|
#define SPIFMT_PHASE_MASK BIT(16)
|
|
|
|
#define SPIFMT_POLARITY_MASK BIT(17)
|
|
|
|
#define SPIFMT_DISTIMER_MASK BIT(18)
|
|
|
|
#define SPIFMT_SHIFTDIR_MASK BIT(20)
|
|
|
|
#define SPIFMT_WAITENA_MASK BIT(21)
|
|
|
|
#define SPIFMT_PARITYENA_MASK BIT(22)
|
|
|
|
#define SPIFMT_ODD_PARITY_MASK BIT(23)
|
|
|
|
#define SPIFMT_WDELAY_MASK 0x3f000000u
|
|
|
|
#define SPIFMT_WDELAY_SHIFT 24
|
2010-08-13 15:57:23 +08:00
|
|
|
#define SPIFMT_PRESCALE_SHIFT 8
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
/* SPIPC0 */
|
|
|
|
#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
|
|
|
|
#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
|
|
|
|
#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
|
|
|
|
#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
|
|
|
|
|
|
|
|
#define SPIINT_MASKALL 0x0101035F
|
2010-09-02 19:22:06 +08:00
|
|
|
#define SPIINT_MASKINT 0x0000015F
|
|
|
|
#define SPI_INTLVL_1 0x000001FF
|
|
|
|
#define SPI_INTLVL_0 0x00000000
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-08-12 14:57:33 +08:00
|
|
|
/* SPIDAT1 (upper 16 bit defines) */
|
|
|
|
#define SPIDAT1_CSHOLD_MASK BIT(12)
|
2014-09-16 19:25:05 +08:00
|
|
|
#define SPIDAT1_WDEL BIT(10)
|
2010-08-12 14:57:33 +08:00
|
|
|
|
|
|
|
/* SPIGCR1 */
|
2009-12-17 06:02:18 +08:00
|
|
|
#define SPIGCR1_CLKMOD_MASK BIT(1)
|
|
|
|
#define SPIGCR1_MASTER_MASK BIT(0)
|
2010-10-06 20:55:43 +08:00
|
|
|
#define SPIGCR1_POWERDOWN_MASK BIT(8)
|
2009-12-17 06:02:18 +08:00
|
|
|
#define SPIGCR1_LOOPBACK_MASK BIT(16)
|
2010-08-20 18:50:49 +08:00
|
|
|
#define SPIGCR1_SPIENA_MASK BIT(24)
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
/* SPIBUF */
|
|
|
|
#define SPIBUF_TXFULL_MASK BIT(29)
|
|
|
|
#define SPIBUF_RXEMPTY_MASK BIT(31)
|
|
|
|
|
2010-08-19 17:37:38 +08:00
|
|
|
/* SPIDELAY */
|
|
|
|
#define SPIDELAY_C2TDELAY_SHIFT 24
|
|
|
|
#define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
|
|
|
|
#define SPIDELAY_T2CDELAY_SHIFT 16
|
|
|
|
#define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
|
|
|
|
#define SPIDELAY_T2EDELAY_SHIFT 8
|
|
|
|
#define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
|
|
|
|
#define SPIDELAY_C2EDELAY_SHIFT 0
|
|
|
|
#define SPIDELAY_C2EDELAY_MASK 0xFF
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/* Error Masks */
|
|
|
|
#define SPIFLG_DLEN_ERR_MASK BIT(0)
|
|
|
|
#define SPIFLG_TIMEOUT_MASK BIT(1)
|
|
|
|
#define SPIFLG_PARERR_MASK BIT(2)
|
|
|
|
#define SPIFLG_DESYNC_MASK BIT(3)
|
|
|
|
#define SPIFLG_BITERR_MASK BIT(4)
|
|
|
|
#define SPIFLG_OVRRUN_MASK BIT(6)
|
|
|
|
#define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
|
2010-08-23 19:09:19 +08:00
|
|
|
#define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
|
|
|
|
| SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
|
|
|
|
| SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
|
|
|
|
| SPIFLG_OVRRUN_MASK)
|
2010-08-20 18:50:49 +08:00
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
#define SPIINT_DMA_REQ_EN BIT(16)
|
|
|
|
|
|
|
|
/* SPI Controller registers */
|
|
|
|
#define SPIGCR0 0x00
|
|
|
|
#define SPIGCR1 0x04
|
|
|
|
#define SPIINT 0x08
|
|
|
|
#define SPILVL 0x0c
|
|
|
|
#define SPIFLG 0x10
|
|
|
|
#define SPIPC0 0x14
|
|
|
|
#define SPIDAT1 0x3c
|
|
|
|
#define SPIBUF 0x40
|
|
|
|
#define SPIDELAY 0x48
|
|
|
|
#define SPIDEF 0x4c
|
|
|
|
#define SPIFMT0 0x50
|
|
|
|
|
2017-02-24 02:01:59 +08:00
|
|
|
#define DMA_MIN_BYTES 16
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/* SPI Controller driver's private data. */
|
|
|
|
struct davinci_spi {
|
|
|
|
struct spi_bitbang bitbang;
|
|
|
|
struct clk *clk;
|
|
|
|
|
|
|
|
u8 version;
|
|
|
|
resource_size_t pbase;
|
|
|
|
void __iomem *base;
|
2010-09-02 19:22:06 +08:00
|
|
|
u32 irq;
|
|
|
|
struct completion done;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
const void *tx;
|
|
|
|
void *rx;
|
2010-09-02 19:22:06 +08:00
|
|
|
int rcount;
|
|
|
|
int wcount;
|
2012-08-23 09:09:36 +08:00
|
|
|
|
|
|
|
struct dma_chan *dma_rx;
|
|
|
|
struct dma_chan *dma_tx;
|
|
|
|
|
2012-12-12 05:20:39 +08:00
|
|
|
struct davinci_spi_platform_data pdata;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
void (*get_rx)(u32 rx_data, struct davinci_spi *);
|
|
|
|
u32 (*get_tx)(struct davinci_spi *);
|
|
|
|
|
2014-08-01 01:33:14 +08:00
|
|
|
u8 *bytes_per_word;
|
2015-07-22 20:32:22 +08:00
|
|
|
|
|
|
|
u8 prescaler_limit;
|
2009-12-17 06:02:18 +08:00
|
|
|
};
|
|
|
|
|
2010-08-16 17:35:51 +08:00
|
|
|
static struct davinci_spi_config davinci_spi_default_cfg;
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->rx) {
|
|
|
|
u8 *rx = dspi->rx;
|
2010-08-19 19:34:25 +08:00
|
|
|
*rx++ = (u8)data;
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->rx = rx;
|
2010-08-19 19:34:25 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->rx) {
|
|
|
|
u16 *rx = dspi->rx;
|
2010-08-19 19:34:25 +08:00
|
|
|
*rx++ = (u16)data;
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->rx = rx;
|
2010-08-19 19:34:25 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-08-19 19:34:25 +08:00
|
|
|
u32 data = 0;
|
2014-09-02 10:48:00 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->tx) {
|
|
|
|
const u8 *tx = dspi->tx;
|
2014-09-02 10:48:00 +08:00
|
|
|
|
2010-08-19 19:34:25 +08:00
|
|
|
data = *tx++;
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->tx = tx;
|
2010-08-19 19:34:25 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-08-19 19:34:25 +08:00
|
|
|
u32 data = 0;
|
2014-09-02 10:48:00 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->tx) {
|
|
|
|
const u16 *tx = dspi->tx;
|
2014-09-02 10:48:00 +08:00
|
|
|
|
2010-08-19 19:34:25 +08:00
|
|
|
data = *tx++;
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->tx = tx;
|
2010-08-19 19:34:25 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_io_bits(void __iomem *addr, u32 bits)
|
|
|
|
{
|
|
|
|
u32 v = ioread32(addr);
|
|
|
|
|
|
|
|
v |= bits;
|
|
|
|
iowrite32(v, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void clear_io_bits(void __iomem *addr, u32 bits)
|
|
|
|
{
|
|
|
|
u32 v = ioread32(addr);
|
|
|
|
|
|
|
|
v &= ~bits;
|
|
|
|
iowrite32(v, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface to control the chip select signal
|
|
|
|
*/
|
|
|
|
static void davinci_spi_chipselect(struct spi_device *spi, int value)
|
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi;
|
2009-12-17 06:02:18 +08:00
|
|
|
struct davinci_spi_platform_data *pdata;
|
2014-09-16 19:25:05 +08:00
|
|
|
struct davinci_spi_config *spicfg = spi->controller_data;
|
2010-08-13 12:41:03 +08:00
|
|
|
u8 chip_sel = spi->chip_select;
|
2010-10-11 13:11:39 +08:00
|
|
|
u16 spidat1 = CS_DEFAULT;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi = spi_master_get_devdata(spi->master);
|
2012-12-12 05:20:39 +08:00
|
|
|
pdata = &dspi->pdata;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2014-09-16 19:25:05 +08:00
|
|
|
/* program delay transfers if tx_delay is non zero */
|
2018-08-10 17:13:52 +08:00
|
|
|
if (spicfg && spicfg->wdelay)
|
2014-09-16 19:25:05 +08:00
|
|
|
spidat1 |= SPIDAT1_WDEL;
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/*
|
|
|
|
* Board specific chip select logic decides the polarity and cs
|
|
|
|
* line for the controller
|
|
|
|
*/
|
2015-10-16 23:22:07 +08:00
|
|
|
if (spi->cs_gpio >= 0) {
|
2010-08-13 13:27:44 +08:00
|
|
|
if (value == BITBANG_CS_ACTIVE)
|
2015-10-16 23:22:07 +08:00
|
|
|
gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH);
|
2010-08-13 13:27:44 +08:00
|
|
|
else
|
2015-10-16 23:22:07 +08:00
|
|
|
gpio_set_value(spi->cs_gpio,
|
|
|
|
!(spi->mode & SPI_CS_HIGH));
|
2010-08-13 13:27:44 +08:00
|
|
|
} else {
|
|
|
|
if (value == BITBANG_CS_ACTIVE) {
|
2010-10-11 13:11:39 +08:00
|
|
|
spidat1 |= SPIDAT1_CSHOLD_MASK;
|
|
|
|
spidat1 &= ~(0x1 << chip_sel);
|
2010-08-13 13:27:44 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 19:25:05 +08:00
|
|
|
|
|
|
|
iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
|
2010-08-13 15:57:23 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_get_prescale - Calculates the correct prescale value
|
|
|
|
* @maxspeed_hz: the maximum rate the SPI clock can run at
|
|
|
|
*
|
|
|
|
* This function calculates the prescale value that generates a clock rate
|
|
|
|
* less than or equal to the specified maximum.
|
|
|
|
*
|
2015-07-22 20:32:21 +08:00
|
|
|
* Returns: calculated prescale value for easy programming into SPI registers
|
2010-08-13 15:57:23 +08:00
|
|
|
* or negative error number if valid prescalar cannot be updated.
|
|
|
|
*/
|
2010-10-11 13:11:39 +08:00
|
|
|
static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
|
2010-08-13 15:57:23 +08:00
|
|
|
u32 max_speed_hz)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-07-22 20:32:21 +08:00
|
|
|
/* Subtract 1 to match what will be programmed into SPI register. */
|
|
|
|
ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz) - 1;
|
2010-08-13 15:57:23 +08:00
|
|
|
|
2015-07-22 20:32:22 +08:00
|
|
|
if (ret < dspi->prescaler_limit || ret > 255)
|
2010-08-13 15:57:23 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-22 20:32:21 +08:00
|
|
|
return ret;
|
2010-08-13 15:57:23 +08:00
|
|
|
}
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_setup_transfer - This functions will determine transfer method
|
|
|
|
* @spi: spi device on which data transfer to be done
|
|
|
|
* @t: spi transfer in which transfer info is filled
|
|
|
|
*
|
|
|
|
* This function determines data transfer method (8/16/32 bit transfer).
|
|
|
|
* It will also set the SPI Clock Control register according to
|
|
|
|
* SPI slave device freq.
|
|
|
|
*/
|
|
|
|
static int davinci_spi_setup_transfer(struct spi_device *spi,
|
|
|
|
struct spi_transfer *t)
|
|
|
|
{
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi;
|
2010-08-19 14:45:22 +08:00
|
|
|
struct davinci_spi_config *spicfg;
|
2009-12-17 06:02:18 +08:00
|
|
|
u8 bits_per_word = 0;
|
2013-09-11 18:35:04 +08:00
|
|
|
u32 hz = 0, spifmt = 0;
|
|
|
|
int prescale;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi = spi_master_get_devdata(spi->master);
|
2014-09-16 19:25:05 +08:00
|
|
|
spicfg = spi->controller_data;
|
2010-08-19 14:45:22 +08:00
|
|
|
if (!spicfg)
|
|
|
|
spicfg = &davinci_spi_default_cfg;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
if (t) {
|
|
|
|
bits_per_word = t->bits_per_word;
|
|
|
|
hz = t->speed_hz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if bits_per_word is not set then set it default */
|
|
|
|
if (!bits_per_word)
|
|
|
|
bits_per_word = spi->bits_per_word;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assign function pointer to appropriate transfer method
|
|
|
|
* 8bit, 16bit or 32bit transfer
|
|
|
|
*/
|
2013-05-22 10:36:35 +08:00
|
|
|
if (bits_per_word <= 8) {
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->get_rx = davinci_spi_rx_buf_u8;
|
|
|
|
dspi->get_tx = davinci_spi_tx_buf_u8;
|
|
|
|
dspi->bytes_per_word[spi->chip_select] = 1;
|
2013-05-22 10:36:35 +08:00
|
|
|
} else {
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->get_rx = davinci_spi_rx_buf_u16;
|
|
|
|
dspi->get_tx = davinci_spi_tx_buf_u16;
|
|
|
|
dspi->bytes_per_word[spi->chip_select] = 2;
|
2013-05-22 10:36:35 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
if (!hz)
|
|
|
|
hz = spi->max_speed_hz;
|
|
|
|
|
2010-08-19 14:45:22 +08:00
|
|
|
/* Set up SPIFMTn register, unique to this chipselect. */
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
prescale = davinci_spi_get_prescale(dspi, hz);
|
2010-08-13 15:57:23 +08:00
|
|
|
if (prescale < 0)
|
|
|
|
return prescale;
|
|
|
|
|
2010-08-19 14:45:22 +08:00
|
|
|
spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
|
|
|
|
|
|
|
|
if (spi->mode & SPI_LSB_FIRST)
|
|
|
|
spifmt |= SPIFMT_SHIFTDIR_MASK;
|
|
|
|
|
|
|
|
if (spi->mode & SPI_CPOL)
|
|
|
|
spifmt |= SPIFMT_POLARITY_MASK;
|
|
|
|
|
|
|
|
if (!(spi->mode & SPI_CPHA))
|
|
|
|
spifmt |= SPIFMT_PHASE_MASK;
|
|
|
|
|
2014-09-16 19:25:05 +08:00
|
|
|
/*
|
|
|
|
* Assume wdelay is used only on SPI peripherals that has this field
|
|
|
|
* in SPIFMTn register and when it's configured from board file or DT.
|
|
|
|
*/
|
|
|
|
if (spicfg->wdelay)
|
|
|
|
spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
|
|
|
|
& SPIFMT_WDELAY_MASK);
|
|
|
|
|
2010-08-19 14:45:22 +08:00
|
|
|
/*
|
|
|
|
* Version 1 hardware supports two basic SPI modes:
|
|
|
|
* - Standard SPI mode uses 4 pins, with chipselect
|
|
|
|
* - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
|
|
|
|
* (distinct from SPI_3WIRE, with just one data wire;
|
|
|
|
* or similar variants without MOSI or without MISO)
|
|
|
|
*
|
|
|
|
* Version 2 hardware supports an optional handshaking signal,
|
|
|
|
* so it can support two more modes:
|
|
|
|
* - 5 pin SPI variant is standard SPI plus SPI_READY
|
|
|
|
* - 4 pin with enable is (SPI_READY | SPI_NO_CS)
|
|
|
|
*/
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->version == SPI_VERSION_2) {
|
2010-08-19 14:45:22 +08:00
|
|
|
|
2010-08-19 17:37:38 +08:00
|
|
|
u32 delay = 0;
|
|
|
|
|
2010-08-19 14:45:22 +08:00
|
|
|
if (spicfg->odd_parity)
|
|
|
|
spifmt |= SPIFMT_ODD_PARITY_MASK;
|
|
|
|
|
|
|
|
if (spicfg->parity_enable)
|
|
|
|
spifmt |= SPIFMT_PARITYENA_MASK;
|
|
|
|
|
2010-08-19 17:37:38 +08:00
|
|
|
if (spicfg->timer_disable) {
|
2010-08-19 14:45:22 +08:00
|
|
|
spifmt |= SPIFMT_DISTIMER_MASK;
|
2010-08-19 17:37:38 +08:00
|
|
|
} else {
|
|
|
|
delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
|
|
|
|
& SPIDELAY_C2TDELAY_MASK;
|
|
|
|
delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
|
|
|
|
& SPIDELAY_T2CDELAY_MASK;
|
|
|
|
}
|
2010-08-19 14:45:22 +08:00
|
|
|
|
2010-08-19 17:37:38 +08:00
|
|
|
if (spi->mode & SPI_READY) {
|
2010-08-19 14:45:22 +08:00
|
|
|
spifmt |= SPIFMT_WAITENA_MASK;
|
2010-08-19 17:37:38 +08:00
|
|
|
delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
|
|
|
|
& SPIDELAY_T2EDELAY_MASK;
|
|
|
|
delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
|
|
|
|
& SPIDELAY_C2EDELAY_MASK;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(delay, dspi->base + SPIDELAY);
|
2010-08-19 14:45:22 +08:00
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(spifmt, dspi->base + SPIFMT0);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-16 19:25:05 +08:00
|
|
|
static int davinci_spi_of_setup(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
struct davinci_spi_config *spicfg = spi->controller_data;
|
|
|
|
struct device_node *np = spi->dev.of_node;
|
2017-02-24 02:01:57 +08:00
|
|
|
struct davinci_spi *dspi = spi_master_get_devdata(spi->master);
|
2014-09-16 19:25:05 +08:00
|
|
|
u32 prop;
|
|
|
|
|
|
|
|
if (spicfg == NULL && np) {
|
|
|
|
spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL);
|
|
|
|
if (!spicfg)
|
|
|
|
return -ENOMEM;
|
|
|
|
*spicfg = davinci_spi_default_cfg;
|
|
|
|
/* override with dt configured values */
|
|
|
|
if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
|
|
|
|
spicfg->wdelay = (u8)prop;
|
|
|
|
spi->controller_data = spicfg;
|
2017-02-24 02:01:57 +08:00
|
|
|
|
|
|
|
if (dspi->dma_rx && dspi->dma_tx)
|
|
|
|
spicfg->io_type = SPI_IO_TYPE_DMA;
|
2014-09-16 19:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_setup - This functions will set default transfer method
|
|
|
|
* @spi: spi device on which data transfer to be done
|
|
|
|
*
|
|
|
|
* This functions sets the default transfer method.
|
|
|
|
*/
|
|
|
|
static int davinci_spi_setup(struct spi_device *spi)
|
|
|
|
{
|
2010-09-24 21:23:32 +08:00
|
|
|
int retval = 0;
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi;
|
2010-09-03 14:45:28 +08:00
|
|
|
struct davinci_spi_platform_data *pdata;
|
2014-08-02 00:40:32 +08:00
|
|
|
struct spi_master *master = spi->master;
|
|
|
|
struct device_node *np = spi->dev.of_node;
|
|
|
|
bool internal_cs = true;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi = spi_master_get_devdata(spi->master);
|
2012-12-12 05:20:39 +08:00
|
|
|
pdata = &dspi->pdata;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-09-03 14:45:28 +08:00
|
|
|
if (!(spi->mode & SPI_NO_CS)) {
|
2014-08-02 00:40:32 +08:00
|
|
|
if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) {
|
2014-09-12 22:54:00 +08:00
|
|
|
retval = gpio_direction_output(
|
|
|
|
spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
|
2014-08-02 00:40:32 +08:00
|
|
|
internal_cs = false;
|
|
|
|
} else if (pdata->chip_sel &&
|
|
|
|
spi->chip_select < pdata->num_chipselect &&
|
|
|
|
pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) {
|
2014-08-02 00:40:33 +08:00
|
|
|
spi->cs_gpio = pdata->chip_sel[spi->chip_select];
|
2014-09-12 22:54:00 +08:00
|
|
|
retval = gpio_direction_output(
|
|
|
|
spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
|
2014-08-02 00:40:32 +08:00
|
|
|
internal_cs = false;
|
|
|
|
}
|
2010-09-03 14:45:28 +08:00
|
|
|
|
2014-08-21 23:25:05 +08:00
|
|
|
if (retval) {
|
|
|
|
dev_err(&spi->dev, "GPIO %d setup failed (%d)\n",
|
|
|
|
spi->cs_gpio, retval);
|
|
|
|
return retval;
|
|
|
|
}
|
2014-08-02 00:40:33 +08:00
|
|
|
|
2014-08-21 23:25:05 +08:00
|
|
|
if (internal_cs)
|
|
|
|
set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
|
|
|
|
}
|
2014-08-02 00:40:32 +08:00
|
|
|
|
2010-09-03 14:45:28 +08:00
|
|
|
if (spi->mode & SPI_READY)
|
2010-10-11 13:11:39 +08:00
|
|
|
set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
|
2010-09-03 14:45:28 +08:00
|
|
|
|
|
|
|
if (spi->mode & SPI_LOOP)
|
2010-10-11 13:11:39 +08:00
|
|
|
set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
|
2010-09-03 14:45:28 +08:00
|
|
|
else
|
2010-10-11 13:11:39 +08:00
|
|
|
clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
|
2010-09-03 14:45:28 +08:00
|
|
|
|
2014-09-16 19:25:05 +08:00
|
|
|
return davinci_spi_of_setup(spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void davinci_spi_cleanup(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
struct davinci_spi_config *spicfg = spi->controller_data;
|
|
|
|
|
|
|
|
spi->controller_data = NULL;
|
|
|
|
if (spi->dev.of_node)
|
|
|
|
kfree(spicfg);
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 02:01:56 +08:00
|
|
|
static bool davinci_spi_can_dma(struct spi_master *master,
|
|
|
|
struct spi_device *spi,
|
|
|
|
struct spi_transfer *xfer)
|
|
|
|
{
|
|
|
|
struct davinci_spi_config *spicfg = spi->controller_data;
|
|
|
|
bool can_dma = false;
|
|
|
|
|
|
|
|
if (spicfg)
|
2017-02-24 02:01:59 +08:00
|
|
|
can_dma = (spicfg->io_type == SPI_IO_TYPE_DMA) &&
|
2017-02-24 02:02:00 +08:00
|
|
|
(xfer->len >= DMA_MIN_BYTES) &&
|
|
|
|
!is_vmalloc_addr(xfer->rx_buf) &&
|
|
|
|
!is_vmalloc_addr(xfer->tx_buf);
|
2017-02-24 02:01:56 +08:00
|
|
|
|
|
|
|
return can_dma;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
struct device *sdev = dspi->bitbang.master->dev.parent;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
if (int_status & SPIFLG_TIMEOUT_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Time-out Error\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
if (int_status & SPIFLG_DESYNC_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Desynchronization Error\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (int_status & SPIFLG_BITERR_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Bit error\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->version == SPI_VERSION_2) {
|
2009-12-17 06:02:18 +08:00
|
|
|
if (int_status & SPIFLG_DLEN_ERR_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Data Length Error\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (int_status & SPIFLG_PARERR_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Parity Error\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (int_status & SPIFLG_OVRRUN_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Data Overrun error\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
|
2015-12-11 00:29:05 +08:00
|
|
|
dev_err(sdev, "SPI Buffer Init Active\n");
|
2009-12-17 06:02:18 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-02 19:22:06 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_process_events - check for and handle any SPI controller events
|
2010-10-11 13:11:39 +08:00
|
|
|
* @dspi: the controller data
|
2010-09-02 19:22:06 +08:00
|
|
|
*
|
|
|
|
* This function will check the SPIFLG register and handle any events that are
|
|
|
|
* detected there
|
|
|
|
*/
|
2010-10-11 13:11:39 +08:00
|
|
|
static int davinci_spi_process_events(struct davinci_spi *dspi)
|
2010-09-02 19:22:06 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
u32 buf, status, errors = 0, spidat1;
|
2010-09-02 19:22:06 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
buf = ioread32(dspi->base + SPIBUF);
|
2010-09-02 19:22:06 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
|
|
|
|
dspi->get_rx(buf & 0xFFFF, dspi);
|
|
|
|
dspi->rcount--;
|
2010-09-02 19:22:06 +08:00
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
status = ioread32(dspi->base + SPIFLG);
|
2010-09-02 19:22:06 +08:00
|
|
|
|
|
|
|
if (unlikely(status & SPIFLG_ERROR_MASK)) {
|
|
|
|
errors = status & SPIFLG_ERROR_MASK;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
|
|
|
|
spidat1 = ioread32(dspi->base + SPIDAT1);
|
|
|
|
dspi->wcount--;
|
|
|
|
spidat1 &= ~0xFFFF;
|
|
|
|
spidat1 |= 0xFFFF & dspi->get_tx(dspi);
|
|
|
|
iowrite32(spidat1, dspi->base + SPIDAT1);
|
2010-09-02 19:22:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2012-08-23 09:09:36 +08:00
|
|
|
static void davinci_spi_dma_rx_callback(void *data)
|
2010-10-06 19:33:10 +08:00
|
|
|
{
|
2012-08-23 09:09:36 +08:00
|
|
|
struct davinci_spi *dspi = (struct davinci_spi *)data;
|
2010-10-06 19:33:10 +08:00
|
|
|
|
2012-08-23 09:09:36 +08:00
|
|
|
dspi->rcount = 0;
|
2010-10-06 19:33:10 +08:00
|
|
|
|
2012-08-23 09:09:36 +08:00
|
|
|
if (!dspi->wcount && !dspi->rcount)
|
|
|
|
complete(&dspi->done);
|
|
|
|
}
|
2010-10-06 19:33:10 +08:00
|
|
|
|
2012-08-23 09:09:36 +08:00
|
|
|
static void davinci_spi_dma_tx_callback(void *data)
|
|
|
|
{
|
|
|
|
struct davinci_spi *dspi = (struct davinci_spi *)data;
|
|
|
|
|
|
|
|
dspi->wcount = 0;
|
|
|
|
|
|
|
|
if (!dspi->wcount && !dspi->rcount)
|
2010-10-11 13:11:39 +08:00
|
|
|
complete(&dspi->done);
|
2010-10-06 19:33:10 +08:00
|
|
|
}
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_bufs - functions which will handle transfer data
|
|
|
|
* @spi: spi device on which data transfer to be done
|
|
|
|
* @t: spi transfer in which transfer info is filled
|
|
|
|
*
|
|
|
|
* This function will put data to be transferred into data register
|
|
|
|
* of SPI controller and then wait until the completion will be marked
|
|
|
|
* by the IRQ Handler.
|
|
|
|
*/
|
2010-10-06 19:33:10 +08:00
|
|
|
static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi;
|
2012-08-23 09:09:36 +08:00
|
|
|
int data_type, ret = -ENOMEM;
|
2010-10-11 13:11:39 +08:00
|
|
|
u32 tx_data, spidat1;
|
2010-08-23 19:09:19 +08:00
|
|
|
u32 errors = 0;
|
2010-09-02 19:22:06 +08:00
|
|
|
struct davinci_spi_config *spicfg;
|
2009-12-17 06:02:18 +08:00
|
|
|
struct davinci_spi_platform_data *pdata;
|
2010-10-06 19:33:10 +08:00
|
|
|
unsigned uninitialized_var(rx_buf_count);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi = spi_master_get_devdata(spi->master);
|
2012-12-12 05:20:39 +08:00
|
|
|
pdata = &dspi->pdata;
|
2010-09-02 19:22:06 +08:00
|
|
|
spicfg = (struct davinci_spi_config *)spi->controller_data;
|
|
|
|
if (!spicfg)
|
|
|
|
spicfg = &davinci_spi_default_cfg;
|
2010-10-06 19:33:10 +08:00
|
|
|
|
|
|
|
/* convert len to words based on bits_per_word */
|
2010-10-11 13:11:39 +08:00
|
|
|
data_type = dspi->bytes_per_word[spi->chip_select];
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->tx = t->tx_buf;
|
|
|
|
dspi->rx = t->rx_buf;
|
|
|
|
dspi->wcount = t->len / data_type;
|
|
|
|
dspi->rcount = dspi->wcount;
|
2010-08-13 12:41:03 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
spidat1 = ioread32(dspi->base + SPIDAT1);
|
2010-08-23 19:09:19 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
|
|
|
|
set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2013-11-15 06:32:02 +08:00
|
|
|
reinit_completion(&dspi->done);
|
2010-10-06 19:33:10 +08:00
|
|
|
|
2017-02-24 02:01:59 +08:00
|
|
|
if (!davinci_spi_can_dma(spi->master, spi, t)) {
|
|
|
|
if (spicfg->io_type != SPI_IO_TYPE_POLL)
|
|
|
|
set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
|
2010-10-06 19:33:10 +08:00
|
|
|
/* start the transfer */
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->wcount--;
|
|
|
|
tx_data = dspi->get_tx(dspi);
|
|
|
|
spidat1 &= 0xFFFF0000;
|
|
|
|
spidat1 |= tx_data & 0xFFFF;
|
|
|
|
iowrite32(spidat1, dspi->base + SPIDAT1);
|
2010-10-06 19:33:10 +08:00
|
|
|
} else {
|
2012-08-23 09:09:36 +08:00
|
|
|
struct dma_slave_config dma_rx_conf = {
|
|
|
|
.direction = DMA_DEV_TO_MEM,
|
|
|
|
.src_addr = (unsigned long)dspi->pbase + SPIBUF,
|
|
|
|
.src_addr_width = data_type,
|
|
|
|
.src_maxburst = 1,
|
|
|
|
};
|
|
|
|
struct dma_slave_config dma_tx_conf = {
|
|
|
|
.direction = DMA_MEM_TO_DEV,
|
|
|
|
.dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
|
|
|
|
.dst_addr_width = data_type,
|
|
|
|
.dst_maxburst = 1,
|
|
|
|
};
|
|
|
|
struct dma_async_tx_descriptor *rxdesc;
|
|
|
|
struct dma_async_tx_descriptor *txdesc;
|
|
|
|
|
|
|
|
dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
|
|
|
|
dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
|
|
|
|
|
|
|
|
rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
|
2017-02-24 02:01:56 +08:00
|
|
|
t->rx_sg.sgl, t->rx_sg.nents, DMA_DEV_TO_MEM,
|
2012-08-23 09:09:36 +08:00
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!rxdesc)
|
|
|
|
goto err_desc;
|
|
|
|
|
2017-02-24 02:01:58 +08:00
|
|
|
if (!t->tx_buf) {
|
2017-03-17 23:41:10 +08:00
|
|
|
/* To avoid errors when doing rx-only transfers with
|
|
|
|
* many SG entries (> 20), use the rx buffer as the
|
|
|
|
* dummy tx buffer so that dma reloads are done at the
|
|
|
|
* same time for rx and tx.
|
|
|
|
*/
|
2017-02-24 02:01:58 +08:00
|
|
|
t->tx_sg.sgl = t->rx_sg.sgl;
|
|
|
|
t->tx_sg.nents = t->rx_sg.nents;
|
|
|
|
}
|
|
|
|
|
2012-08-23 09:09:36 +08:00
|
|
|
txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
|
2017-02-24 02:01:56 +08:00
|
|
|
t->tx_sg.sgl, t->tx_sg.nents, DMA_MEM_TO_DEV,
|
2012-08-23 09:09:36 +08:00
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!txdesc)
|
|
|
|
goto err_desc;
|
|
|
|
|
|
|
|
rxdesc->callback = davinci_spi_dma_rx_callback;
|
|
|
|
rxdesc->callback_param = (void *)dspi;
|
|
|
|
txdesc->callback = davinci_spi_dma_tx_callback;
|
|
|
|
txdesc->callback_param = (void *)dspi;
|
2010-10-06 19:33:10 +08:00
|
|
|
|
|
|
|
if (pdata->cshold_bug)
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
|
2010-10-06 19:33:10 +08:00
|
|
|
|
2012-08-23 09:09:36 +08:00
|
|
|
dmaengine_submit(rxdesc);
|
|
|
|
dmaengine_submit(txdesc);
|
|
|
|
|
|
|
|
dma_async_issue_pending(dspi->dma_rx);
|
|
|
|
dma_async_issue_pending(dspi->dma_tx);
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
|
2010-10-06 19:33:10 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-09-02 19:22:06 +08:00
|
|
|
/* Wait for the transfer to complete */
|
2010-10-06 19:33:10 +08:00
|
|
|
if (spicfg->io_type != SPI_IO_TYPE_POLL) {
|
2015-12-11 00:29:04 +08:00
|
|
|
if (wait_for_completion_timeout(&dspi->done, HZ) == 0)
|
|
|
|
errors = SPIFLG_TIMEOUT_MASK;
|
2010-09-02 19:22:06 +08:00
|
|
|
} else {
|
2010-10-11 13:11:39 +08:00
|
|
|
while (dspi->rcount > 0 || dspi->wcount > 0) {
|
|
|
|
errors = davinci_spi_process_events(dspi);
|
2010-09-02 19:22:06 +08:00
|
|
|
if (errors)
|
|
|
|
break;
|
|
|
|
cpu_relax();
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
|
2017-02-24 02:01:59 +08:00
|
|
|
if (davinci_spi_can_dma(spi->master, spi, t))
|
2010-10-11 13:11:39 +08:00
|
|
|
clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
|
2012-08-23 09:09:36 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
|
|
|
|
set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
|
2010-10-06 20:55:43 +08:00
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/*
|
|
|
|
* Check for bit error, desync error,parity error,timeout error and
|
|
|
|
* receive overflow errors
|
|
|
|
*/
|
2010-08-23 19:09:19 +08:00
|
|
|
if (errors) {
|
2010-10-11 13:11:39 +08:00
|
|
|
ret = davinci_spi_check_error(dspi, errors);
|
2010-08-23 19:09:19 +08:00
|
|
|
WARN(!ret, "%s: error reported but no error found!\n",
|
|
|
|
dev_name(&spi->dev));
|
2009-12-17 06:02:18 +08:00
|
|
|
return ret;
|
2010-08-23 19:09:19 +08:00
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if (dspi->rcount != 0 || dspi->wcount != 0) {
|
2012-08-23 09:09:36 +08:00
|
|
|
dev_err(&spi->dev, "SPI data transfer error\n");
|
2010-10-06 19:33:10 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
return t->len;
|
2012-08-23 09:09:36 +08:00
|
|
|
|
|
|
|
err_desc:
|
|
|
|
return ret;
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
|
2012-12-22 04:13:26 +08:00
|
|
|
/**
|
|
|
|
* dummy_thread_fn - dummy thread function
|
|
|
|
* @irq: IRQ number for this SPI Master
|
|
|
|
* @context_data: structure for SPI Master controller davinci_spi
|
|
|
|
*
|
|
|
|
* This is to satisfy the request_threaded_irq() API so that the irq
|
|
|
|
* handler is called in interrupt context.
|
|
|
|
*/
|
|
|
|
static irqreturn_t dummy_thread_fn(s32 irq, void *data)
|
|
|
|
{
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2010-09-02 19:22:06 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_irq - Interrupt handler for SPI Master Controller
|
|
|
|
* @irq: IRQ number for this SPI Master
|
|
|
|
* @context_data: structure for SPI Master controller davinci_spi
|
|
|
|
*
|
|
|
|
* ISR will determine that interrupt arrives either for READ or WRITE command.
|
|
|
|
* According to command it will do the appropriate action. It will check
|
|
|
|
* transfer length and if it is not zero then dispatch transfer command again.
|
|
|
|
* If transfer length is zero then it will indicate the COMPLETION so that
|
|
|
|
* davinci_spi_bufs function can go ahead.
|
|
|
|
*/
|
2010-10-11 13:11:39 +08:00
|
|
|
static irqreturn_t davinci_spi_irq(s32 irq, void *data)
|
2010-09-02 19:22:06 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi = data;
|
2010-09-02 19:22:06 +08:00
|
|
|
int status;
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
status = davinci_spi_process_events(dspi);
|
2010-09-02 19:22:06 +08:00
|
|
|
if (unlikely(status != 0))
|
2010-10-11 13:11:39 +08:00
|
|
|
clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
|
2010-09-02 19:22:06 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
if ((!dspi->rcount && !dspi->wcount) || status)
|
|
|
|
complete(&dspi->done);
|
2010-09-02 19:22:06 +08:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
static int davinci_spi_request_dma(struct davinci_spi *dspi)
|
2010-10-01 17:21:40 +08:00
|
|
|
{
|
2012-08-23 09:09:36 +08:00
|
|
|
struct device *sdev = dspi->bitbang.master->dev.parent;
|
|
|
|
|
2016-04-29 21:10:22 +08:00
|
|
|
dspi->dma_rx = dma_request_chan(sdev, "rx");
|
|
|
|
if (IS_ERR(dspi->dma_rx))
|
|
|
|
return PTR_ERR(dspi->dma_rx);
|
2010-10-01 17:21:40 +08:00
|
|
|
|
2016-04-29 21:10:22 +08:00
|
|
|
dspi->dma_tx = dma_request_chan(sdev, "tx");
|
|
|
|
if (IS_ERR(dspi->dma_tx)) {
|
|
|
|
dma_release_channel(dspi->dma_rx);
|
|
|
|
return PTR_ERR(dspi->dma_tx);
|
2010-10-01 17:21:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:20:39 +08:00
|
|
|
#if defined(CONFIG_OF)
|
2015-07-22 20:32:22 +08:00
|
|
|
|
|
|
|
/* OF SPI data structure */
|
|
|
|
struct davinci_spi_of_data {
|
|
|
|
u8 version;
|
|
|
|
u8 prescaler_limit;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct davinci_spi_of_data dm6441_spi_data = {
|
|
|
|
.version = SPI_VERSION_1,
|
|
|
|
.prescaler_limit = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct davinci_spi_of_data da830_spi_data = {
|
|
|
|
.version = SPI_VERSION_2,
|
|
|
|
.prescaler_limit = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct davinci_spi_of_data keystone_spi_data = {
|
|
|
|
.version = SPI_VERSION_1,
|
|
|
|
.prescaler_limit = 0,
|
|
|
|
};
|
|
|
|
|
2012-12-12 05:20:39 +08:00
|
|
|
static const struct of_device_id davinci_spi_of_match[] = {
|
|
|
|
{
|
2013-04-03 22:09:06 +08:00
|
|
|
.compatible = "ti,dm6441-spi",
|
2015-07-22 20:32:22 +08:00
|
|
|
.data = &dm6441_spi_data,
|
2012-12-12 05:20:39 +08:00
|
|
|
},
|
|
|
|
{
|
2013-04-03 22:09:06 +08:00
|
|
|
.compatible = "ti,da830-spi",
|
2015-07-22 20:32:22 +08:00
|
|
|
.data = &da830_spi_data,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "ti,keystone-spi",
|
|
|
|
.data = &keystone_spi_data,
|
2012-12-12 05:20:39 +08:00
|
|
|
},
|
|
|
|
{ },
|
|
|
|
};
|
2013-02-25 18:44:07 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
|
2012-12-12 05:20:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* spi_davinci_get_pdata - Get platform data from DTS binding
|
|
|
|
* @pdev: ptr to platform data
|
|
|
|
* @dspi: ptr to driver data
|
|
|
|
*
|
|
|
|
* Parses and populates pdata in dspi from device tree bindings.
|
|
|
|
*
|
|
|
|
* NOTE: Not all platform data params are supported currently.
|
|
|
|
*/
|
|
|
|
static int spi_davinci_get_pdata(struct platform_device *pdev,
|
|
|
|
struct davinci_spi *dspi)
|
|
|
|
{
|
|
|
|
struct device_node *node = pdev->dev.of_node;
|
2015-07-22 20:32:22 +08:00
|
|
|
struct davinci_spi_of_data *spi_data;
|
2012-12-12 05:20:39 +08:00
|
|
|
struct davinci_spi_platform_data *pdata;
|
|
|
|
unsigned int num_cs, intr_line = 0;
|
|
|
|
const struct of_device_id *match;
|
|
|
|
|
|
|
|
pdata = &dspi->pdata;
|
|
|
|
|
2014-02-06 11:45:08 +08:00
|
|
|
match = of_match_device(davinci_spi_of_match, &pdev->dev);
|
2012-12-12 05:20:39 +08:00
|
|
|
if (!match)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2015-07-22 20:32:22 +08:00
|
|
|
spi_data = (struct davinci_spi_of_data *)match->data;
|
2012-12-12 05:20:39 +08:00
|
|
|
|
2015-07-22 20:32:22 +08:00
|
|
|
pdata->version = spi_data->version;
|
|
|
|
pdata->prescaler_limit = spi_data->prescaler_limit;
|
2012-12-12 05:20:39 +08:00
|
|
|
/*
|
|
|
|
* default num_cs is 1 and all chipsel are internal to the chip
|
2014-08-02 00:40:32 +08:00
|
|
|
* indicated by chip_sel being NULL or cs_gpios being NULL or
|
|
|
|
* set to -ENOENT. num-cs includes internal as well as gpios.
|
2012-12-12 05:20:39 +08:00
|
|
|
* indicated by chip_sel being NULL. GPIO based CS is not
|
|
|
|
* supported yet in DT bindings.
|
|
|
|
*/
|
|
|
|
num_cs = 1;
|
|
|
|
of_property_read_u32(node, "num-cs", &num_cs);
|
|
|
|
pdata->num_chipselect = num_cs;
|
|
|
|
of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
|
|
|
|
pdata->intr_line = intr_line;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2017-06-05 21:50:40 +08:00
|
|
|
static int spi_davinci_get_pdata(struct platform_device *pdev,
|
|
|
|
struct davinci_spi *dspi)
|
2012-12-12 05:20:39 +08:00
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/**
|
|
|
|
* davinci_spi_probe - probe function for SPI Master Controller
|
|
|
|
* @pdev: platform_device structure which contains plateform specific data
|
2010-10-06 21:02:40 +08:00
|
|
|
*
|
|
|
|
* According to Linux Device Model this function will be invoked by Linux
|
|
|
|
* with platform_device struct which contains the device specific info.
|
|
|
|
* This function will map the SPI controller's memory, register IRQ,
|
|
|
|
* Reset SPI controller and setting its registers to default value.
|
|
|
|
* It will invoke spi_bitbang_start to create work queue so that client driver
|
|
|
|
* can register transfer method to work queue.
|
2009-12-17 06:02:18 +08:00
|
|
|
*/
|
2012-12-08 00:57:14 +08:00
|
|
|
static int davinci_spi_probe(struct platform_device *pdev)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
|
|
|
struct spi_master *master;
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi;
|
2009-12-17 06:02:18 +08:00
|
|
|
struct davinci_spi_platform_data *pdata;
|
2013-12-09 18:12:03 +08:00
|
|
|
struct resource *r;
|
2014-08-02 00:40:33 +08:00
|
|
|
int ret = 0;
|
2010-09-03 14:26:35 +08:00
|
|
|
u32 spipc0;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
|
|
|
|
if (master == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-05-23 18:20:40 +08:00
|
|
|
platform_set_drvdata(pdev, master);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi = spi_master_get_devdata(master);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2013-07-30 15:58:59 +08:00
|
|
|
if (dev_get_platdata(&pdev->dev)) {
|
|
|
|
pdata = dev_get_platdata(&pdev->dev);
|
2012-12-12 05:20:39 +08:00
|
|
|
dspi->pdata = *pdata;
|
|
|
|
} else {
|
|
|
|
/* update dspi pdata with that from the DT */
|
|
|
|
ret = spi_davinci_get_pdata(pdev, dspi);
|
|
|
|
if (ret < 0)
|
|
|
|
goto free_master;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pdata in dspi is now updated and point pdata to that */
|
|
|
|
pdata = &dspi->pdata;
|
|
|
|
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:07:58 +08:00
|
|
|
dspi->bytes_per_word = devm_kcalloc(&pdev->dev,
|
|
|
|
pdata->num_chipselect,
|
|
|
|
sizeof(*dspi->bytes_per_word),
|
|
|
|
GFP_KERNEL);
|
2014-08-01 01:33:14 +08:00
|
|
|
if (dspi->bytes_per_word == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto free_master;
|
|
|
|
}
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
if (r == NULL) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto free_master;
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->pbase = r->start;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2013-12-09 18:12:03 +08:00
|
|
|
dspi->base = devm_ioremap_resource(&pdev->dev, r);
|
|
|
|
if (IS_ERR(dspi->base)) {
|
|
|
|
ret = PTR_ERR(dspi->base);
|
2009-12-17 06:02:18 +08:00
|
|
|
goto free_master;
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:36:59 +08:00
|
|
|
init_completion(&dspi->done);
|
|
|
|
|
2015-09-24 22:00:10 +08:00
|
|
|
ret = platform_get_irq(pdev, 0);
|
|
|
|
if (ret == 0)
|
2010-09-02 19:22:06 +08:00
|
|
|
ret = -EINVAL;
|
2015-09-24 22:00:10 +08:00
|
|
|
if (ret < 0)
|
2013-12-09 18:12:03 +08:00
|
|
|
goto free_master;
|
2015-09-24 22:00:10 +08:00
|
|
|
dspi->irq = ret;
|
2010-09-02 19:22:06 +08:00
|
|
|
|
2013-12-09 18:12:03 +08:00
|
|
|
ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
|
|
|
|
dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
|
2010-09-02 19:22:06 +08:00
|
|
|
if (ret)
|
2013-12-09 18:12:03 +08:00
|
|
|
goto free_master;
|
2010-09-02 19:22:06 +08:00
|
|
|
|
2013-09-10 15:43:41 +08:00
|
|
|
dspi->bitbang.master = master;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2013-12-09 18:12:03 +08:00
|
|
|
dspi->clk = devm_clk_get(&pdev->dev, NULL);
|
2010-10-11 13:11:39 +08:00
|
|
|
if (IS_ERR(dspi->clk)) {
|
2009-12-17 06:02:18 +08:00
|
|
|
ret = -ENODEV;
|
2013-12-09 18:12:03 +08:00
|
|
|
goto free_master;
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
2017-06-05 20:06:28 +08:00
|
|
|
ret = clk_prepare_enable(dspi->clk);
|
|
|
|
if (ret)
|
|
|
|
goto free_master;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2012-12-12 05:20:39 +08:00
|
|
|
master->dev.of_node = pdev->dev.of_node;
|
2009-12-17 06:02:18 +08:00
|
|
|
master->bus_num = pdev->id;
|
|
|
|
master->num_chipselect = pdata->num_chipselect;
|
2013-05-22 10:36:35 +08:00
|
|
|
master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
|
2017-02-24 02:01:58 +08:00
|
|
|
master->flags = SPI_MASTER_MUST_RX;
|
2009-12-17 06:02:18 +08:00
|
|
|
master->setup = davinci_spi_setup;
|
2014-09-16 19:25:05 +08:00
|
|
|
master->cleanup = davinci_spi_cleanup;
|
2017-02-24 02:01:56 +08:00
|
|
|
master->can_dma = davinci_spi_can_dma;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->bitbang.chipselect = davinci_spi_chipselect;
|
|
|
|
dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
|
2015-07-22 20:32:22 +08:00
|
|
|
dspi->prescaler_limit = pdata->prescaler_limit;
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->version = pdata->version;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
|
|
|
|
if (dspi->version == SPI_VERSION_2)
|
|
|
|
dspi->bitbang.flags |= SPI_READY;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2014-09-12 22:54:00 +08:00
|
|
|
if (pdev->dev.of_node) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < pdata->num_chipselect; i++) {
|
|
|
|
int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
|
|
|
|
"cs-gpios", i);
|
|
|
|
|
|
|
|
if (cs_gpio == -EPROBE_DEFER) {
|
|
|
|
ret = cs_gpio;
|
|
|
|
goto free_clk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gpio_is_valid(cs_gpio)) {
|
|
|
|
ret = devm_gpio_request(&pdev->dev, cs_gpio,
|
|
|
|
dev_name(&pdev->dev));
|
|
|
|
if (ret)
|
|
|
|
goto free_clk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->bitbang.txrx_bufs = davinci_spi_bufs;
|
2016-04-29 21:10:22 +08:00
|
|
|
|
|
|
|
ret = davinci_spi_request_dma(dspi);
|
|
|
|
if (ret == -EPROBE_DEFER) {
|
|
|
|
goto free_clk;
|
|
|
|
} else if (ret) {
|
|
|
|
dev_info(&pdev->dev, "DMA is not supported (%d)\n", ret);
|
|
|
|
dspi->dma_rx = NULL;
|
|
|
|
dspi->dma_tx = NULL;
|
2009-12-17 06:02:18 +08:00
|
|
|
}
|
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi->get_rx = davinci_spi_rx_buf_u8;
|
|
|
|
dspi->get_tx = davinci_spi_tx_buf_u8;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
/* Reset In/OUT SPI module */
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(0, dspi->base + SPIGCR0);
|
2009-12-17 06:02:18 +08:00
|
|
|
udelay(100);
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(1, dspi->base + SPIGCR0);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-09-03 14:45:28 +08:00
|
|
|
/* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
|
2010-09-03 14:26:35 +08:00
|
|
|
spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(spipc0, dspi->base + SPIPC0);
|
2010-09-03 14:26:35 +08:00
|
|
|
|
2010-09-02 19:22:06 +08:00
|
|
|
if (pdata->intr_line)
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
|
2010-09-02 19:22:06 +08:00
|
|
|
else
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
|
2010-09-02 19:22:06 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
|
2010-08-12 15:19:05 +08:00
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
/* master mode default */
|
2010-10-11 13:11:39 +08:00
|
|
|
set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
|
|
|
|
set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
|
|
|
|
set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
ret = spi_bitbang_start(&dspi->bitbang);
|
2009-12-17 06:02:18 +08:00
|
|
|
if (ret)
|
2010-10-01 17:21:40 +08:00
|
|
|
goto free_dma;
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2010-10-01 17:21:40 +08:00
|
|
|
free_dma:
|
2016-04-29 21:10:22 +08:00
|
|
|
if (dspi->dma_rx) {
|
|
|
|
dma_release_channel(dspi->dma_rx);
|
|
|
|
dma_release_channel(dspi->dma_tx);
|
|
|
|
}
|
2009-12-17 06:02:18 +08:00
|
|
|
free_clk:
|
2012-12-12 05:20:39 +08:00
|
|
|
clk_disable_unprepare(dspi->clk);
|
2009-12-17 06:02:18 +08:00
|
|
|
free_master:
|
2013-09-10 15:43:41 +08:00
|
|
|
spi_master_put(master);
|
2009-12-17 06:02:18 +08:00
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* davinci_spi_remove - remove function for SPI Master Controller
|
|
|
|
* @pdev: platform_device structure which contains plateform specific data
|
|
|
|
*
|
|
|
|
* This function will do the reverse action of davinci_spi_probe function
|
|
|
|
* It will free the IRQ and SPI controller's memory region.
|
|
|
|
* It will also call spi_bitbang_stop to destroy the work queue which was
|
|
|
|
* created by spi_bitbang_start.
|
|
|
|
*/
|
2012-12-08 00:57:14 +08:00
|
|
|
static int davinci_spi_remove(struct platform_device *pdev)
|
2009-12-17 06:02:18 +08:00
|
|
|
{
|
2010-10-11 13:11:39 +08:00
|
|
|
struct davinci_spi *dspi;
|
2009-12-17 06:02:18 +08:00
|
|
|
struct spi_master *master;
|
|
|
|
|
2013-05-23 18:20:40 +08:00
|
|
|
master = platform_get_drvdata(pdev);
|
2010-10-11 13:11:39 +08:00
|
|
|
dspi = spi_master_get_devdata(master);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2010-10-11 13:11:39 +08:00
|
|
|
spi_bitbang_stop(&dspi->bitbang);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2012-12-12 05:20:39 +08:00
|
|
|
clk_disable_unprepare(dspi->clk);
|
2013-09-10 15:43:41 +08:00
|
|
|
spi_master_put(master);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
2016-04-29 21:10:22 +08:00
|
|
|
if (dspi->dma_rx) {
|
|
|
|
dma_release_channel(dspi->dma_rx);
|
|
|
|
dma_release_channel(dspi->dma_tx);
|
|
|
|
}
|
|
|
|
|
2009-12-17 06:02:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver davinci_spi_driver = {
|
2010-10-06 21:17:16 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "spi_davinci",
|
2014-02-06 11:45:08 +08:00
|
|
|
.of_match_table = of_match_ptr(davinci_spi_of_match),
|
2010-10-06 21:17:16 +08:00
|
|
|
},
|
2011-10-06 01:29:49 +08:00
|
|
|
.probe = davinci_spi_probe,
|
2012-12-08 00:57:14 +08:00
|
|
|
.remove = davinci_spi_remove,
|
2009-12-17 06:02:18 +08:00
|
|
|
};
|
2011-10-06 01:29:49 +08:00
|
|
|
module_platform_driver(davinci_spi_driver);
|
2009-12-17 06:02:18 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|