Commit e2df04ed authored by Mark Brown's avatar Mark Brown

Merge remote-tracking branches 'spi/topic/fsl-espi', 'spi/topic/imx',...

Merge remote-tracking branches 'spi/topic/fsl-espi', 'spi/topic/imx', 'spi/topic/jcore', 'spi/topic/loopback' and 'spi/topic/meson' into spi-next
J-Core SPI master
Required properties:
- compatible: Must be "jcore,spi2".
- reg: Memory region for registers.
- #address-cells: Must be 1.
- #size-cells: Must be 0.
Optional properties:
- clocks: If a phandle named "ref_clk" is present, SPI clock speed
programming is relative to the frequency of the indicated clock.
Necessary only if the input clock rate is something other than a
fixed 50 MHz.
- clock-names: Clock names, one for each phandle in clocks.
See spi-bus.txt for additional properties not specific to this device.
Example:
spi@40 {
compatible = "jcore,spi2";
#address-cells = <1>;
#size-cells = <0>;
reg = <0x40 0x8>;
spi-max-frequency = <25000000>;
clocks = <&bus_clk>;
clock-names = "ref_clk";
}
...@@ -7,7 +7,7 @@ NOR memories, without DMA support and a 64-byte unified transmit / ...@@ -7,7 +7,7 @@ NOR memories, without DMA support and a 64-byte unified transmit /
receive buffer. receive buffer.
Required properties: Required properties:
- compatible: should be "amlogic,meson6-spifc" - compatible: should be "amlogic,meson6-spifc" or "amlogic,meson-gxbb-spifc"
- reg: physical base address and length of the controller registers - reg: physical base address and length of the controller registers
- clocks: phandle of the input clock for the baud rate generator - clocks: phandle of the input clock for the baud rate generator
- #address-cells: should be 1 - #address-cells: should be 1
......
...@@ -295,6 +295,13 @@ config SPI_IMX ...@@ -295,6 +295,13 @@ config SPI_IMX
This enables using the Freescale i.MX SPI controllers in master This enables using the Freescale i.MX SPI controllers in master
mode. mode.
config SPI_JCORE
tristate "J-Core SPI Master"
depends on OF && (SUPERH || COMPILE_TEST)
help
This enables support for the SPI master controller in the J-Core
synthesizable, open source SoC.
config SPI_LM70_LLP config SPI_LM70_LLP
tristate "Parallel port adapter for LM70 eval board (DEVELOPMENT)" tristate "Parallel port adapter for LM70 eval board (DEVELOPMENT)"
depends on PARPORT depends on PARPORT
......
...@@ -47,6 +47,7 @@ obj-$(CONFIG_SPI_FSL_SPI) += spi-fsl-spi.o ...@@ -47,6 +47,7 @@ obj-$(CONFIG_SPI_FSL_SPI) += spi-fsl-spi.o
obj-$(CONFIG_SPI_GPIO) += spi-gpio.o obj-$(CONFIG_SPI_GPIO) += spi-gpio.o
obj-$(CONFIG_SPI_IMG_SPFI) += spi-img-spfi.o obj-$(CONFIG_SPI_IMG_SPFI) += spi-img-spfi.o
obj-$(CONFIG_SPI_IMX) += spi-imx.o obj-$(CONFIG_SPI_IMX) += spi-imx.o
obj-$(CONFIG_SPI_JCORE) += spi-jcore.o
obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o
obj-$(CONFIG_SPI_LP8841_RTC) += spi-lp8841-rtc.o obj-$(CONFIG_SPI_LP8841_RTC) += spi-lp8841-rtc.o
obj-$(CONFIG_SPI_MESON_SPIFC) += spi-meson-spifc.o obj-$(CONFIG_SPI_MESON_SPIFC) += spi-meson-spifc.o
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <linux/err.h> #include <linux/err.h>
#include <linux/fsl_devices.h> #include <linux/fsl_devices.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/of.h> #include <linux/of.h>
...@@ -27,40 +26,29 @@ ...@@ -27,40 +26,29 @@
#include "spi-fsl-lib.h" #include "spi-fsl-lib.h"
/* eSPI Controller registers */ /* eSPI Controller registers */
struct fsl_espi_reg { #define ESPI_SPMODE 0x00 /* eSPI mode register */
__be32 mode; /* 0x000 - eSPI mode register */ #define ESPI_SPIE 0x04 /* eSPI event register */
__be32 event; /* 0x004 - eSPI event register */ #define ESPI_SPIM 0x08 /* eSPI mask register */
__be32 mask; /* 0x008 - eSPI mask register */ #define ESPI_SPCOM 0x0c /* eSPI command register */
__be32 command; /* 0x00c - eSPI command register */ #define ESPI_SPITF 0x10 /* eSPI transmit FIFO access register*/
__be32 transmit; /* 0x010 - eSPI transmit FIFO access register*/ #define ESPI_SPIRF 0x14 /* eSPI receive FIFO access register*/
__be32 receive; /* 0x014 - eSPI receive FIFO access register*/ #define ESPI_SPMODE0 0x20 /* eSPI cs0 mode register */
u8 res[8]; /* 0x018 - 0x01c reserved */
__be32 csmode[4]; /* 0x020 - 0x02c eSPI cs mode register */
};
struct fsl_espi_transfer { #define ESPI_SPMODEx(x) (ESPI_SPMODE0 + (x) * 4)
const void *tx_buf;
void *rx_buf;
unsigned len;
unsigned n_tx;
unsigned n_rx;
unsigned actual_length;
int status;
};
/* eSPI Controller mode register definitions */ /* eSPI Controller mode register definitions */
#define SPMODE_ENABLE (1 << 31) #define SPMODE_ENABLE BIT(31)
#define SPMODE_LOOP (1 << 30) #define SPMODE_LOOP BIT(30)
#define SPMODE_TXTHR(x) ((x) << 8) #define SPMODE_TXTHR(x) ((x) << 8)
#define SPMODE_RXTHR(x) ((x) << 0) #define SPMODE_RXTHR(x) ((x) << 0)
/* eSPI Controller CS mode register definitions */ /* eSPI Controller CS mode register definitions */
#define CSMODE_CI_INACTIVEHIGH (1 << 31) #define CSMODE_CI_INACTIVEHIGH BIT(31)
#define CSMODE_CP_BEGIN_EDGECLK (1 << 30) #define CSMODE_CP_BEGIN_EDGECLK BIT(30)
#define CSMODE_REV (1 << 29) #define CSMODE_REV BIT(29)
#define CSMODE_DIV16 (1 << 28) #define CSMODE_DIV16 BIT(28)
#define CSMODE_PM(x) ((x) << 24) #define CSMODE_PM(x) ((x) << 24)
#define CSMODE_POL_1 (1 << 20) #define CSMODE_POL_1 BIT(20)
#define CSMODE_LEN(x) ((x) << 16) #define CSMODE_LEN(x) ((x) << 16)
#define CSMODE_BEF(x) ((x) << 12) #define CSMODE_BEF(x) ((x) << 12)
#define CSMODE_AFT(x) ((x) << 8) #define CSMODE_AFT(x) ((x) << 8)
...@@ -72,29 +60,114 @@ struct fsl_espi_transfer { ...@@ -72,29 +60,114 @@ struct fsl_espi_transfer {
| CSMODE_AFT(0) | CSMODE_CG(1)) | CSMODE_AFT(0) | CSMODE_CG(1))
/* SPIE register values */ /* SPIE register values */
#define SPIE_NE 0x00000200 /* Not empty */
#define SPIE_NF 0x00000100 /* Not full */
/* SPIM register values */
#define SPIM_NE 0x00000200 /* Not empty */
#define SPIM_NF 0x00000100 /* Not full */
#define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F) #define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
#define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F) #define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
#define SPIE_TXE BIT(15) /* TX FIFO empty */
#define SPIE_DON BIT(14) /* TX done */
#define SPIE_RXT BIT(13) /* RX FIFO threshold */
#define SPIE_RXF BIT(12) /* RX FIFO full */
#define SPIE_TXT BIT(11) /* TX FIFO threshold*/
#define SPIE_RNE BIT(9) /* RX FIFO not empty */
#define SPIE_TNF BIT(8) /* TX FIFO not full */
/* SPIM register values */
#define SPIM_TXE BIT(15) /* TX FIFO empty */
#define SPIM_DON BIT(14) /* TX done */
#define SPIM_RXT BIT(13) /* RX FIFO threshold */
#define SPIM_RXF BIT(12) /* RX FIFO full */
#define SPIM_TXT BIT(11) /* TX FIFO threshold*/
#define SPIM_RNE BIT(9) /* RX FIFO not empty */
#define SPIM_TNF BIT(8) /* TX FIFO not full */
/* SPCOM register values */ /* SPCOM register values */
#define SPCOM_CS(x) ((x) << 30) #define SPCOM_CS(x) ((x) << 30)
#define SPCOM_DO BIT(28) /* Dual output */
#define SPCOM_TO BIT(27) /* TX only */
#define SPCOM_RXSKIP(x) ((x) << 16)
#define SPCOM_TRANLEN(x) ((x) << 0) #define SPCOM_TRANLEN(x) ((x) << 0)
#define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */ #define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */
#define AUTOSUSPEND_TIMEOUT 2000 #define AUTOSUSPEND_TIMEOUT 2000
static inline u32 fsl_espi_read_reg(struct mpc8xxx_spi *mspi, int offset)
{
return ioread32be(mspi->reg_base + offset);
}
static inline u8 fsl_espi_read_reg8(struct mpc8xxx_spi *mspi, int offset)
{
return ioread8(mspi->reg_base + offset);
}
static inline void fsl_espi_write_reg(struct mpc8xxx_spi *mspi, int offset,
u32 val)
{
iowrite32be(val, mspi->reg_base + offset);
}
static inline void fsl_espi_write_reg8(struct mpc8xxx_spi *mspi, int offset,
u8 val)
{
iowrite8(val, mspi->reg_base + offset);
}
static void fsl_espi_copy_to_buf(struct spi_message *m,
struct mpc8xxx_spi *mspi)
{
struct spi_transfer *t;
u8 *buf = mspi->local_buf;
list_for_each_entry(t, &m->transfers, transfer_list) {
if (t->tx_buf)
memcpy(buf, t->tx_buf, t->len);
else
memset(buf, 0, t->len);
buf += t->len;
}
}
static void fsl_espi_copy_from_buf(struct spi_message *m,
struct mpc8xxx_spi *mspi)
{
struct spi_transfer *t;
u8 *buf = mspi->local_buf;
list_for_each_entry(t, &m->transfers, transfer_list) {
if (t->rx_buf)
memcpy(t->rx_buf, buf, t->len);
buf += t->len;
}
}
static int fsl_espi_check_message(struct spi_message *m)
{
struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
struct spi_transfer *t, *first;
if (m->frame_length > SPCOM_TRANLEN_MAX) {
dev_err(mspi->dev, "message too long, size is %u bytes\n",
m->frame_length);
return -EMSGSIZE;
}
first = list_first_entry(&m->transfers, struct spi_transfer,
transfer_list);
list_for_each_entry(t, &m->transfers, transfer_list) {
if (first->bits_per_word != t->bits_per_word ||
first->speed_hz != t->speed_hz) {
dev_err(mspi->dev, "bits_per_word/speed_hz should be the same for all transfers\n");
return -EINVAL;
}
}
return 0;
}
static void fsl_espi_change_mode(struct spi_device *spi) static void fsl_espi_change_mode(struct spi_device *spi)
{ {
struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master); struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
struct spi_mpc8xxx_cs *cs = spi->controller_state; struct spi_mpc8xxx_cs *cs = spi->controller_state;
struct fsl_espi_reg *reg_base = mspi->reg_base;
__be32 __iomem *mode = &reg_base->csmode[spi->chip_select];
__be32 __iomem *espi_mode = &reg_base->mode;
u32 tmp; u32 tmp;
unsigned long flags; unsigned long flags;
...@@ -102,10 +175,11 @@ static void fsl_espi_change_mode(struct spi_device *spi) ...@@ -102,10 +175,11 @@ static void fsl_espi_change_mode(struct spi_device *spi)
local_irq_save(flags); local_irq_save(flags);
/* Turn off SPI unit prior changing mode */ /* Turn off SPI unit prior changing mode */
tmp = mpc8xxx_spi_read_reg(espi_mode); tmp = fsl_espi_read_reg(mspi, ESPI_SPMODE);
mpc8xxx_spi_write_reg(espi_mode, tmp & ~SPMODE_ENABLE); fsl_espi_write_reg(mspi, ESPI_SPMODE, tmp & ~SPMODE_ENABLE);
mpc8xxx_spi_write_reg(mode, cs->hw_mode); fsl_espi_write_reg(mspi, ESPI_SPMODEx(spi->chip_select),
mpc8xxx_spi_write_reg(espi_mode, tmp); cs->hw_mode);
fsl_espi_write_reg(mspi, ESPI_SPMODE, tmp);
local_irq_restore(flags); local_irq_restore(flags);
} }
...@@ -131,27 +205,15 @@ static u32 fsl_espi_tx_buf_lsb(struct mpc8xxx_spi *mpc8xxx_spi) ...@@ -131,27 +205,15 @@ static u32 fsl_espi_tx_buf_lsb(struct mpc8xxx_spi *mpc8xxx_spi)
return data; return data;
} }
static int fsl_espi_setup_transfer(struct spi_device *spi, static void fsl_espi_setup_transfer(struct spi_device *spi,
struct spi_transfer *t) struct spi_transfer *t)
{ {
struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
int bits_per_word = 0; int bits_per_word = t ? t->bits_per_word : spi->bits_per_word;
u32 hz = t ? t->speed_hz : spi->max_speed_hz;
u8 pm; u8 pm;
u32 hz = 0;
struct spi_mpc8xxx_cs *cs = spi->controller_state; struct spi_mpc8xxx_cs *cs = spi->controller_state;
if (t) {
bits_per_word = t->bits_per_word;
hz = t->speed_hz;
}
/* spi_transfer level calls that work per-word */
if (!bits_per_word)
bits_per_word = spi->bits_per_word;
if (!hz)
hz = spi->max_speed_hz;
cs->rx_shift = 0; cs->rx_shift = 0;
cs->tx_shift = 0; cs->tx_shift = 0;
cs->get_rx = mpc8xxx_spi_rx_buf_u32; cs->get_rx = mpc8xxx_spi_rx_buf_u32;
...@@ -169,12 +231,10 @@ static int fsl_espi_setup_transfer(struct spi_device *spi, ...@@ -169,12 +231,10 @@ static int fsl_espi_setup_transfer(struct spi_device *spi,
mpc8xxx_spi->get_rx = cs->get_rx; mpc8xxx_spi->get_rx = cs->get_rx;
mpc8xxx_spi->get_tx = cs->get_tx; mpc8xxx_spi->get_tx = cs->get_tx;
bits_per_word = bits_per_word - 1;
/* mask out bits we are going to set */ /* mask out bits we are going to set */
cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF)); cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
cs->hw_mode |= CSMODE_LEN(bits_per_word); cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
if ((mpc8xxx_spi->spibrg / hz) > 64) { if ((mpc8xxx_spi->spibrg / hz) > 64) {
cs->hw_mode |= CSMODE_DIV16; cs->hw_mode |= CSMODE_DIV16;
...@@ -196,36 +256,16 @@ static int fsl_espi_setup_transfer(struct spi_device *spi, ...@@ -196,36 +256,16 @@ static int fsl_espi_setup_transfer(struct spi_device *spi,
cs->hw_mode |= CSMODE_PM(pm); cs->hw_mode |= CSMODE_PM(pm);
fsl_espi_change_mode(spi); fsl_espi_change_mode(spi);
return 0;
}
static int fsl_espi_cpu_bufs(struct mpc8xxx_spi *mspi, struct spi_transfer *t,
unsigned int len)
{
u32 word;
struct fsl_espi_reg *reg_base = mspi->reg_base;
mspi->count = len;
/* enable rx ints */
mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
/* transmit word */
word = mspi->get_tx(mspi);
mpc8xxx_spi_write_reg(&reg_base->transmit, word);
return 0;
} }
static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t) static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
{ {
struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base; u32 word;
unsigned int len = t->len;
int ret; int ret;
mpc8xxx_spi->len = t->len; mpc8xxx_spi->len = t->len;
len = roundup(len, 4) / 4; mpc8xxx_spi->count = roundup(t->len, 4) / 4;
mpc8xxx_spi->tx = t->tx_buf; mpc8xxx_spi->tx = t->tx_buf;
mpc8xxx_spi->rx = t->rx_buf; mpc8xxx_spi->rx = t->rx_buf;
...@@ -233,17 +273,15 @@ static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t) ...@@ -233,17 +273,15 @@ static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
reinit_completion(&mpc8xxx_spi->done); reinit_completion(&mpc8xxx_spi->done);
/* Set SPCOM[CS] and SPCOM[TRANLEN] field */ /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
if (t->len > SPCOM_TRANLEN_MAX) { fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM,
dev_err(mpc8xxx_spi->dev, "Transaction length (%d)"
" beyond the SPCOM[TRANLEN] field\n", t->len);
return -EINVAL;
}
mpc8xxx_spi_write_reg(&reg_base->command,
(SPCOM_CS(spi->chip_select) | SPCOM_TRANLEN(t->len - 1))); (SPCOM_CS(spi->chip_select) | SPCOM_TRANLEN(t->len - 1)));
ret = fsl_espi_cpu_bufs(mpc8xxx_spi, t, len); /* enable rx ints */
if (ret) fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, SPIM_RNE);
return ret;
/* transmit word */
word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPITF, word);
/* Won't hang up forever, SPI bus sometimes got lost interrupts... */ /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ); ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ);
...@@ -253,230 +291,76 @@ static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t) ...@@ -253,230 +291,76 @@ static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
mpc8xxx_spi->count); mpc8xxx_spi->count);
/* disable rx ints */ /* disable rx ints */
mpc8xxx_spi_write_reg(&reg_base->mask, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
return mpc8xxx_spi->count;
}
static inline void fsl_espi_addr2cmd(unsigned int addr, u8 *cmd)
{
if (cmd) {
cmd[1] = (u8)(addr >> 16);
cmd[2] = (u8)(addr >> 8);
cmd[3] = (u8)(addr >> 0);
}
}
static inline unsigned int fsl_espi_cmd2addr(u8 *cmd) return mpc8xxx_spi->count > 0 ? -EMSGSIZE : 0;
{
if (cmd)
return cmd[1] << 16 | cmd[2] << 8 | cmd[3] << 0;
return 0;
} }
static void fsl_espi_do_trans(struct spi_message *m, static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans)
struct fsl_espi_transfer *tr)
{ {
struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
struct spi_device *spi = m->spi; struct spi_device *spi = m->spi;
struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master); int ret;
struct fsl_espi_transfer *espi_trans = tr;
struct spi_message message;
struct spi_transfer *t, *first, trans;
int status = 0;
spi_message_init(&message);
memset(&trans, 0, sizeof(trans));
first = list_first_entry(&m->transfers, struct spi_transfer,
transfer_list);
list_for_each_entry(t, &m->transfers, transfer_list) {
if ((first->bits_per_word != t->bits_per_word) ||
(first->speed_hz != t->speed_hz)) {
espi_trans->status = -EINVAL;
dev_err(mspi->dev,
"bits_per_word/speed_hz should be same for the same SPI transfer\n");
return;
}
trans.speed_hz = t->speed_hz;
trans.bits_per_word = t->bits_per_word;
trans.delay_usecs = max(first->delay_usecs, t->delay_usecs);
}
trans.len = espi_trans->len;
trans.tx_buf = espi_trans->tx_buf;
trans.rx_buf = espi_trans->rx_buf;
spi_message_add_tail(&trans, &message);
list_for_each_entry(t, &message.transfers, transfer_list) {
if (t->bits_per_word || t->speed_hz) {
status = -EINVAL;
status = fsl_espi_setup_transfer(spi, t);
if (status < 0)
break;
}
if (t->len) fsl_espi_copy_to_buf(m, mspi);
status = fsl_espi_bufs(spi, t); fsl_espi_setup_transfer(spi, trans);
if (status) { ret = fsl_espi_bufs(spi, trans);
status = -EMSGSIZE;
break;
}
if (t->delay_usecs) if (trans->delay_usecs)
udelay(t->delay_usecs); udelay(trans->delay_usecs);
}
espi_trans->status = status;
fsl_espi_setup_transfer(spi, NULL); fsl_espi_setup_transfer(spi, NULL);
}
static void fsl_espi_cmd_trans(struct spi_message *m,
struct fsl_espi_transfer *trans, u8 *rx_buff)
{
struct spi_transfer *t;
u8 *local_buf;
int i = 0;
struct fsl_espi_transfer *espi_trans = trans;
local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL);
if (!local_buf) {
espi_trans->status = -ENOMEM;
return;
}
list_for_each_entry(t, &m->transfers, transfer_list) {
if (t->tx_buf) {
memcpy(local_buf + i, t->tx_buf, t->len);
i += t->len;
}
}
espi_trans->tx_buf = local_buf; if (!ret)
espi_trans->rx_buf = local_buf; fsl_espi_copy_from_buf(m, mspi);
fsl_espi_do_trans(m, espi_trans);
espi_trans->actual_length = espi_trans->len; return ret;
kfree(local_buf);
} }
static void fsl_espi_rw_trans(struct spi_message *m, static int fsl_espi_do_one_msg(struct spi_master *master,
struct fsl_espi_transfer *trans, u8 *rx_buff) struct spi_message *m)
{ {
struct fsl_espi_transfer *espi_trans = trans; struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
unsigned int total_len = espi_trans->len; unsigned int delay_usecs = 0;
struct spi_transfer *t; struct spi_transfer *t, trans = {};
u8 *local_buf; int ret;
u8 *rx_buf = rx_buff;
unsigned int trans_len;
unsigned int addr;
unsigned int tx_only;
unsigned int rx_pos = 0;
unsigned int pos;
int i, loop;
local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL);
if (!local_buf) {
espi_trans->status = -ENOMEM;
return;
}
for (pos = 0, loop = 0; pos < total_len; pos += trans_len, loop++) { ret = fsl_espi_check_message(m);
trans_len = total_len - pos; if (ret)
goto out;
i = 0;
tx_only = 0;
list_for_each_entry(t, &m->transfers, transfer_list) { list_for_each_entry(t, &m->transfers, transfer_list) {
if (t->tx_buf) { if (t->delay_usecs > delay_usecs)
memcpy(local_buf + i, t->tx_buf, t->len); delay_usecs = t->delay_usecs;
i += t->len;
if (!t->rx_buf)
tx_only += t->len;
}
}
/* Add additional TX bytes to compensate SPCOM_TRANLEN_MAX */
if (loop > 0)
trans_len += tx_only;
if (trans_len > SPCOM_TRANLEN_MAX)
trans_len = SPCOM_TRANLEN_MAX;
/* Update device offset */
if (pos > 0) {
addr = fsl_espi_cmd2addr(local_buf);
addr += rx_pos;
fsl_espi_addr2cmd(addr, local_buf);
} }
espi_trans->len = trans_len; t = list_first_entry(&m->transfers, struct spi_transfer,
espi_trans->tx_buf = local_buf; transfer_list);
espi_trans->rx_buf = local_buf;
fsl_espi_do_trans(m, espi_trans);
/* If there is at least one RX byte then copy it to rx_buf */
if (tx_only < SPCOM_TRANLEN_MAX)
memcpy(rx_buf + rx_pos, espi_trans->rx_buf + tx_only,
trans_len - tx_only);
rx_pos += trans_len - tx_only;
if (loop > 0)
espi_trans->actual_length += espi_trans->len - tx_only;
else
espi_trans->actual_length += espi_trans->len;
}
kfree(local_buf);
}
static int fsl_espi_do_one_msg(struct spi_master *master,
struct spi_message *m)
{
struct spi_transfer *t;
u8 *rx_buf = NULL;
unsigned int n_tx = 0;
unsigned int n_rx = 0;
unsigned int xfer_len = 0;
struct fsl_espi_transfer espi_trans;
list_for_each_entry(t, &m->transfers, transfer_list) { trans.len = m->frame_length;
if (t->tx_buf) trans.speed_hz = t->speed_hz;
n_tx += t->len; trans.bits_per_word = t->bits_per_word;
if (t->rx_buf) { trans.delay_usecs = delay_usecs;
n_rx += t->len; trans.tx_buf = mspi->local_buf;
rx_buf = t->rx_buf; trans.rx_buf = mspi->local_buf;
}
if ((t->tx_buf) || (t->rx_buf))
xfer_len += t->len;
}
espi_trans.n_tx = n_tx; if (trans.len)
espi_trans.n_rx = n_rx; ret = fsl_espi_trans(m, &trans);
espi_trans.len = xfer_len;
espi_trans.actual_length = 0;
espi_trans.status = 0;
if (!rx_buf) m->actual_length = ret ? 0 : trans.len;
fsl_espi_cmd_trans(m, &espi_trans, NULL); out:
else if (m->status == -EINPROGRESS)
fsl_espi_rw_trans(m, &espi_trans, rx_buf); m->status = ret;
m->actual_length = espi_trans.actual_length;
m->status = espi_trans.status;
spi_finalize_current_message(master); spi_finalize_current_message(master);
return 0;
return ret;
} }
static int fsl_espi_setup(struct spi_device *spi) static int fsl_espi_setup(struct spi_device *spi)
{ {
struct mpc8xxx_spi *mpc8xxx_spi; struct mpc8xxx_spi *mpc8xxx_spi;
struct fsl_espi_reg *reg_base;
int retval;
u32 hw_mode;
u32 loop_mode; u32 loop_mode;
struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi); struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
...@@ -491,13 +375,11 @@ static int fsl_espi_setup(struct spi_device *spi) ...@@ -491,13 +375,11 @@ static int fsl_espi_setup(struct spi_device *spi)
} }
mpc8xxx_spi = spi_master_get_devdata(spi->master); mpc8xxx_spi = spi_master_get_devdata(spi->master);
reg_base = mpc8xxx_spi->reg_base;
pm_runtime_get_sync(mpc8xxx_spi->dev); pm_runtime_get_sync(mpc8xxx_spi->dev);
hw_mode = cs->hw_mode; /* Save original settings */ cs->hw_mode = fsl_espi_read_reg(mpc8xxx_spi,
cs->hw_mode = mpc8xxx_spi_read_reg( ESPI_SPMODEx(spi->chip_select));
&reg_base->csmode[spi->chip_select]);
/* mask out bits we are going to set */ /* mask out bits we are going to set */
cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
| CSMODE_REV); | CSMODE_REV);
...@@ -510,21 +392,17 @@ static int fsl_espi_setup(struct spi_device *spi) ...@@ -510,21 +392,17 @@ static int fsl_espi_setup(struct spi_device *spi)
cs->hw_mode |= CSMODE_REV; cs->hw_mode |= CSMODE_REV;
/* Handle the loop mode */ /* Handle the loop mode */
loop_mode = mpc8xxx_spi_read_reg(&reg_base->mode); loop_mode = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
loop_mode &= ~SPMODE_LOOP; loop_mode &= ~SPMODE_LOOP;
if (spi->mode & SPI_LOOP) if (spi->mode & SPI_LOOP)
loop_mode |= SPMODE_LOOP; loop_mode |= SPMODE_LOOP;
mpc8xxx_spi_write_reg(&reg_base->mode, loop_mode); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, loop_mode);
retval = fsl_espi_setup_transfer(spi, NULL); fsl_espi_setup_transfer(spi, NULL);
pm_runtime_mark_last_busy(mpc8xxx_spi->dev); pm_runtime_mark_last_busy(mpc8xxx_spi->dev);
pm_runtime_put_autosuspend(mpc8xxx_spi->dev); pm_runtime_put_autosuspend(mpc8xxx_spi->dev);
if (retval < 0) {
cs->hw_mode = hw_mode; /* Restore settings */
return retval;
}
return 0; return 0;
} }
...@@ -536,12 +414,10 @@ static void fsl_espi_cleanup(struct spi_device *spi) ...@@ -536,12 +414,10 @@ static void fsl_espi_cleanup(struct spi_device *spi)
spi_set_ctldata(spi, NULL); spi_set_ctldata(spi, NULL);
} }
void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
{ {
struct fsl_espi_reg *reg_base = mspi->reg_base;
/* We need handle RX first */ /* We need handle RX first */
if (events & SPIE_NE) { if (events & SPIE_RNE) {
u32 rx_data, tmp; u32 rx_data, tmp;
u8 rx_data_8; u8 rx_data_8;
int rx_nr_bytes = 4; int rx_nr_bytes = 4;
...@@ -551,7 +427,7 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) ...@@ -551,7 +427,7 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
if (SPIE_RXCNT(events) < min(4, mspi->len)) { if (SPIE_RXCNT(events) < min(4, mspi->len)) {
ret = spin_event_timeout( ret = spin_event_timeout(
!(SPIE_RXCNT(events = !(SPIE_RXCNT(events =
mpc8xxx_spi_read_reg(&reg_base->event)) < fsl_espi_read_reg(mspi, ESPI_SPIE)) <
min(4, mspi->len)), min(4, mspi->len)),
10000, 0); /* 10 msec */ 10000, 0); /* 10 msec */
if (!ret) if (!ret)
...@@ -560,10 +436,10 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) ...@@ -560,10 +436,10 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
} }
if (mspi->len >= 4) { if (mspi->len >= 4) {
rx_data = mpc8xxx_spi_read_reg(&reg_base->receive); rx_data = fsl_espi_read_reg(mspi, ESPI_SPIRF);
} else if (mspi->len <= 0) { } else if (mspi->len <= 0) {
dev_err(mspi->dev, dev_err(mspi->dev,
"unexpected RX(SPIE_NE) interrupt occurred,\n" "unexpected RX(SPIE_RNE) interrupt occurred,\n"
"(local rxlen %d bytes, reg rxlen %d bytes)\n", "(local rxlen %d bytes, reg rxlen %d bytes)\n",
min(4, mspi->len), SPIE_RXCNT(events)); min(4, mspi->len), SPIE_RXCNT(events));
rx_nr_bytes = 0; rx_nr_bytes = 0;
...@@ -572,7 +448,8 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) ...@@ -572,7 +448,8 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
tmp = mspi->len; tmp = mspi->len;
rx_data = 0; rx_data = 0;
while (tmp--) { while (tmp--) {
rx_data_8 = in_8((u8 *)&reg_base->receive); rx_data_8 = fsl_espi_read_reg8(mspi,
ESPI_SPIRF);
rx_data |= (rx_data_8 << (tmp * 8)); rx_data |= (rx_data_8 << (tmp * 8));
} }
...@@ -585,30 +462,24 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) ...@@ -585,30 +462,24 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
mspi->get_rx(rx_data, mspi); mspi->get_rx(rx_data, mspi);
} }
if (!(events & SPIE_NF)) { if (!(events & SPIE_TNF)) {
int ret; int ret;
/* spin until TX is done */ /* spin until TX is done */
ret = spin_event_timeout(((events = mpc8xxx_spi_read_reg( ret = spin_event_timeout(((events = fsl_espi_read_reg(
&reg_base->event)) & SPIE_NF), 1000, 0); mspi, ESPI_SPIE)) & SPIE_TNF), 1000, 0);
if (!ret) { if (!ret) {
dev_err(mspi->dev, "tired waiting for SPIE_NF\n"); dev_err(mspi->dev, "tired waiting for SPIE_TNF\n");
/* Clear the SPIE bits */
mpc8xxx_spi_write_reg(&reg_base->event, events);
complete(&mspi->done); complete(&mspi->done);
return; return;
} }
} }
/* Clear the events */
mpc8xxx_spi_write_reg(&reg_base->event, events);
mspi->count -= 1; mspi->count -= 1;
if (mspi->count) { if (mspi->count) {
u32 word = mspi->get_tx(mspi); u32 word = mspi->get_tx(mspi);
mpc8xxx_spi_write_reg(&reg_base->transmit, word); fsl_espi_write_reg(mspi, ESPI_SPITF, word);
} else { } else {
complete(&mspi->done); complete(&mspi->done);
} }
...@@ -617,20 +488,21 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events) ...@@ -617,20 +488,21 @@ void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
static irqreturn_t fsl_espi_irq(s32 irq, void *context_data) static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
{ {
struct mpc8xxx_spi *mspi = context_data; struct mpc8xxx_spi *mspi = context_data;
struct fsl_espi_reg *reg_base = mspi->reg_base;
irqreturn_t ret = IRQ_NONE;
u32 events; u32 events;
/* Get interrupt events(tx/rx) */ /* Get interrupt events(tx/rx) */
events = mpc8xxx_spi_read_reg(&reg_base->event); events = fsl_espi_read_reg(mspi, ESPI_SPIE);
if (events) if (!events)
ret = IRQ_HANDLED; return IRQ_NONE;
dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events); dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events);
fsl_espi_cpu_irq(mspi, events); fsl_espi_cpu_irq(mspi, events);
return ret; /* Clear the events */
fsl_espi_write_reg(mspi, ESPI_SPIE, events);
return IRQ_HANDLED;
} }
#ifdef CONFIG_PM #ifdef CONFIG_PM
...@@ -638,12 +510,11 @@ static int fsl_espi_runtime_suspend(struct device *dev) ...@@ -638,12 +510,11 @@ static int fsl_espi_runtime_suspend(struct device *dev)
{ {
struct spi_master *master = dev_get_drvdata(dev); struct spi_master *master = dev_get_drvdata(dev);
struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
u32 regval; u32 regval;
regval = mpc8xxx_spi_read_reg(&reg_base->mode); regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
regval &= ~SPMODE_ENABLE; regval &= ~SPMODE_ENABLE;
mpc8xxx_spi_write_reg(&reg_base->mode, regval); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
return 0; return 0;
} }
...@@ -652,39 +523,35 @@ static int fsl_espi_runtime_resume(struct device *dev) ...@@ -652,39 +523,35 @@ static int fsl_espi_runtime_resume(struct device *dev)
{ {
struct spi_master *master = dev_get_drvdata(dev); struct spi_master *master = dev_get_drvdata(dev);
struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master); struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
u32 regval; u32 regval;
regval = mpc8xxx_spi_read_reg(&reg_base->mode); regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
regval |= SPMODE_ENABLE; regval |= SPMODE_ENABLE;
mpc8xxx_spi_write_reg(&reg_base->mode, regval); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
return 0; return 0;
} }
#endif #endif
static size_t fsl_espi_max_transfer_size(struct spi_device *spi) static size_t fsl_espi_max_message_size(struct spi_device *spi)
{ {
return SPCOM_TRANLEN_MAX; return SPCOM_TRANLEN_MAX;
} }
static struct spi_master * fsl_espi_probe(struct device *dev, static int fsl_espi_probe(struct device *dev, struct resource *mem,
struct resource *mem, unsigned int irq) unsigned int irq)
{ {
struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
struct spi_master *master; struct spi_master *master;
struct mpc8xxx_spi *mpc8xxx_spi; struct mpc8xxx_spi *mpc8xxx_spi;
struct fsl_espi_reg *reg_base;
struct device_node *nc; struct device_node *nc;
const __be32 *prop; const __be32 *prop;
u32 regval, csmode; u32 regval, csmode;
int i, len, ret = 0; int i, len, ret;
master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi)); master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
if (!master) { if (!master)
ret = -ENOMEM; return -ENOMEM;
goto err;
}
dev_set_drvdata(dev, master); dev_set_drvdata(dev, master);
...@@ -695,18 +562,23 @@ static struct spi_master * fsl_espi_probe(struct device *dev, ...@@ -695,18 +562,23 @@ static struct spi_master * fsl_espi_probe(struct device *dev,
master->cleanup = fsl_espi_cleanup; master->cleanup = fsl_espi_cleanup;
master->transfer_one_message = fsl_espi_do_one_msg; master->transfer_one_message = fsl_espi_do_one_msg;
master->auto_runtime_pm = true; master->auto_runtime_pm = true;
master->max_transfer_size = fsl_espi_max_transfer_size; master->max_message_size = fsl_espi_max_message_size;
mpc8xxx_spi = spi_master_get_devdata(master); mpc8xxx_spi = spi_master_get_devdata(master);
mpc8xxx_spi->local_buf =
devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL);
if (!mpc8xxx_spi->local_buf) {
ret = -ENOMEM;
goto err_probe;
}
mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem); mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
if (IS_ERR(mpc8xxx_spi->reg_base)) { if (IS_ERR(mpc8xxx_spi->reg_base)) {
ret = PTR_ERR(mpc8xxx_spi->reg_base); ret = PTR_ERR(mpc8xxx_spi->reg_base);
goto err_probe; goto err_probe;
} }
reg_base = mpc8xxx_spi->reg_base;
/* Register for SPI Interrupt */ /* Register for SPI Interrupt */
ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq, ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq,
0, "fsl_espi", mpc8xxx_spi); 0, "fsl_espi", mpc8xxx_spi);
...@@ -719,10 +591,10 @@ static struct spi_master * fsl_espi_probe(struct device *dev, ...@@ -719,10 +591,10 @@ static struct spi_master * fsl_espi_probe(struct device *dev,
} }
/* SPI controller initializations */ /* SPI controller initializations */
mpc8xxx_spi_write_reg(&reg_base->mode, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
mpc8xxx_spi_write_reg(&reg_base->mask, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
mpc8xxx_spi_write_reg(&reg_base->command, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
/* Init eSPI CS mode register */ /* Init eSPI CS mode register */
for_each_available_child_of_node(master->dev.of_node, nc) { for_each_available_child_of_node(master->dev.of_node, nc) {
...@@ -747,7 +619,7 @@ static struct spi_master * fsl_espi_probe(struct device *dev, ...@@ -747,7 +619,7 @@ static struct spi_master * fsl_espi_probe(struct device *dev,
csmode &= ~(CSMODE_AFT(0xf)); csmode &= ~(CSMODE_AFT(0xf));
csmode |= CSMODE_AFT(be32_to_cpup(prop)); csmode |= CSMODE_AFT(be32_to_cpup(prop));
} }
mpc8xxx_spi_write_reg(&reg_base->csmode[i], csmode); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(i), csmode);
dev_info(dev, "cs=%d, init_csmode=0x%x\n", i, csmode); dev_info(dev, "cs=%d, init_csmode=0x%x\n", i, csmode);
} }
...@@ -755,7 +627,7 @@ static struct spi_master * fsl_espi_probe(struct device *dev, ...@@ -755,7 +627,7 @@ static struct spi_master * fsl_espi_probe(struct device *dev,
/* Enable SPI interface */ /* Enable SPI interface */
regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
mpc8xxx_spi_write_reg(&reg_base->mode, regval); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT); pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
pm_runtime_use_autosuspend(dev); pm_runtime_use_autosuspend(dev);
...@@ -767,12 +639,13 @@ static struct spi_master * fsl_espi_probe(struct device *dev, ...@@ -767,12 +639,13 @@ static struct spi_master * fsl_espi_probe(struct device *dev,
if (ret < 0) if (ret < 0)
goto err_pm; goto err_pm;
dev_info(dev, "at 0x%p (irq = %d)\n", reg_base, mpc8xxx_spi->irq); dev_info(dev, "at 0x%p (irq = %d)\n", mpc8xxx_spi->reg_base,
mpc8xxx_spi->irq);
pm_runtime_mark_last_busy(dev); pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev); pm_runtime_put_autosuspend(dev);
return master; return 0;
err_pm: err_pm:
pm_runtime_put_noidle(dev); pm_runtime_put_noidle(dev);
...@@ -780,8 +653,7 @@ static struct spi_master * fsl_espi_probe(struct device *dev, ...@@ -780,8 +653,7 @@ static struct spi_master * fsl_espi_probe(struct device *dev,
pm_runtime_set_suspended(dev); pm_runtime_set_suspended(dev);
err_probe: err_probe:
spi_master_put(master); spi_master_put(master);
err: return ret;
return ERR_PTR(ret);
} }
static int of_fsl_espi_get_chipselects(struct device *dev) static int of_fsl_espi_get_chipselects(struct device *dev)
...@@ -807,10 +679,9 @@ static int of_fsl_espi_probe(struct platform_device *ofdev) ...@@ -807,10 +679,9 @@ static int of_fsl_espi_probe(struct platform_device *ofdev)
{ {
struct device *dev = &ofdev->dev; struct device *dev = &ofdev->dev;
struct device_node *np = ofdev->dev.of_node; struct device_node *np = ofdev->dev.of_node;
struct spi_master *master;
struct resource mem; struct resource mem;
unsigned int irq; unsigned int irq;
int ret = -ENOMEM; int ret;
ret = of_mpc8xxx_spi_probe(ofdev); ret = of_mpc8xxx_spi_probe(ofdev);
if (ret) if (ret)
...@@ -818,28 +689,17 @@ static int of_fsl_espi_probe(struct platform_device *ofdev) ...@@ -818,28 +689,17 @@ static int of_fsl_espi_probe(struct platform_device *ofdev)
ret = of_fsl_espi_get_chipselects(dev); ret = of_fsl_espi_get_chipselects(dev);
if (ret) if (ret)
goto err; return ret;
ret = of_address_to_resource(np, 0, &mem); ret = of_address_to_resource(np, 0, &mem);
if (ret) if (ret)
goto err; return ret;
irq = irq_of_parse_and_map(np, 0); irq = irq_of_parse_and_map(np, 0);
if (!irq) { if (!irq)
ret = -EINVAL; return -EINVAL;
goto err;
}
master = fsl_espi_probe(dev, &mem, irq);
if (IS_ERR(master)) {
ret = PTR_ERR(master);
goto err;
}
return 0;
err: return fsl_espi_probe(dev, &mem, irq);
return ret;
} }
static int of_fsl_espi_remove(struct platform_device *dev) static int of_fsl_espi_remove(struct platform_device *dev)
...@@ -873,27 +733,26 @@ static int of_fsl_espi_resume(struct device *dev) ...@@ -873,27 +733,26 @@ static int of_fsl_espi_resume(struct device *dev)
struct fsl_spi_platform_data *pdata = dev_get_platdata(dev); struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
struct spi_master *master = dev_get_drvdata(dev); struct spi_master *master = dev_get_drvdata(dev);
struct mpc8xxx_spi *mpc8xxx_spi; struct mpc8xxx_spi *mpc8xxx_spi;
struct fsl_espi_reg *reg_base;
u32 regval; u32 regval;
int i, ret; int i, ret;
mpc8xxx_spi = spi_master_get_devdata(master); mpc8xxx_spi = spi_master_get_devdata(master);
reg_base = mpc8xxx_spi->reg_base;
/* SPI controller initializations */ /* SPI controller initializations */
mpc8xxx_spi_write_reg(&reg_base->mode, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
mpc8xxx_spi_write_reg(&reg_base->mask, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
mpc8xxx_spi_write_reg(&reg_base->command, 0); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
/* Init eSPI CS mode register */ /* Init eSPI CS mode register */
for (i = 0; i < pdata->max_chipselect; i++) for (i = 0; i < pdata->max_chipselect; i++)
mpc8xxx_spi_write_reg(&reg_base->csmode[i], CSMODE_INIT_VAL); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(i),
CSMODE_INIT_VAL);
/* Enable SPI interface */ /* Enable SPI interface */
regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
mpc8xxx_spi_write_reg(&reg_base->mode, regval); fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
ret = pm_runtime_force_resume(dev); ret = pm_runtime_force_resume(dev);
if (ret < 0) if (ret < 0)
......
...@@ -23,13 +23,14 @@ ...@@ -23,13 +23,14 @@
/* SPI/eSPI Controller driver's private data. */ /* SPI/eSPI Controller driver's private data. */
struct mpc8xxx_spi { struct mpc8xxx_spi {
struct device *dev; struct device *dev;
void *reg_base; void __iomem *reg_base;
/* rx & tx bufs from the spi_transfer */ /* rx & tx bufs from the spi_transfer */
const void *tx; const void *tx;
void *rx; void *rx;
#if IS_ENABLED(CONFIG_SPI_FSL_ESPI) #if IS_ENABLED(CONFIG_SPI_FSL_ESPI)
int len; int len;
u8 *local_buf;
#endif #endif
int subblock; int subblock;
......
...@@ -186,17 +186,19 @@ static unsigned int spi_imx_clkdiv_1(unsigned int fin, ...@@ -186,17 +186,19 @@ static unsigned int spi_imx_clkdiv_1(unsigned int fin,
/* MX1, MX31, MX35, MX51 CSPI */ /* MX1, MX31, MX35, MX51 CSPI */
static unsigned int spi_imx_clkdiv_2(unsigned int fin, static unsigned int spi_imx_clkdiv_2(unsigned int fin,
unsigned int fspi) unsigned int fspi, unsigned int *fres)
{ {
int i, div = 4; int i, div = 4;
for (i = 0; i < 7; i++) { for (i = 0; i < 7; i++) {
if (fspi * div >= fin) if (fspi * div >= fin)
return i; goto out;
div <<= 1; div <<= 1;
} }
return 7; out:
*fres = fin / div;
return i;
} }
static int spi_imx_bytes_per_word(const int bpw) static int spi_imx_bytes_per_word(const int bpw)
...@@ -453,6 +455,9 @@ static void mx51_ecspi_reset(struct spi_imx_data *spi_imx) ...@@ -453,6 +455,9 @@ static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
#define MX31_CSPISTATUS 0x14 #define MX31_CSPISTATUS 0x14
#define MX31_STATUS_RR (1 << 3) #define MX31_STATUS_RR (1 << 3)
#define MX31_CSPI_TESTREG 0x1C
#define MX31_TEST_LBC (1 << 14)
/* These functions also work for the i.MX35, but be aware that /* These functions also work for the i.MX35, but be aware that
* the i.MX35 has a slightly different register layout for bits * the i.MX35 has a slightly different register layout for bits
* we do not use here. * we do not use here.
...@@ -482,9 +487,11 @@ static int mx31_config(struct spi_device *spi, struct spi_imx_config *config) ...@@ -482,9 +487,11 @@ static int mx31_config(struct spi_device *spi, struct spi_imx_config *config)
{ {
struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER; unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
unsigned int clk;
reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) << reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz, &clk) <<
MX31_CSPICTRL_DR_SHIFT; MX31_CSPICTRL_DR_SHIFT;
spi_imx->spi_bus_clk = clk;
if (is_imx35_cspi(spi_imx)) { if (is_imx35_cspi(spi_imx)) {
reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT; reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
...@@ -506,6 +513,13 @@ static int mx31_config(struct spi_device *spi, struct spi_imx_config *config) ...@@ -506,6 +513,13 @@ static int mx31_config(struct spi_device *spi, struct spi_imx_config *config)
writel(reg, spi_imx->base + MXC_CSPICTRL); writel(reg, spi_imx->base + MXC_CSPICTRL);
reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
if (spi->mode & SPI_LOOP)
reg |= MX31_TEST_LBC;
else
reg &= ~MX31_TEST_LBC;
writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
return 0; return 0;
} }
...@@ -625,9 +639,12 @@ static int mx1_config(struct spi_device *spi, struct spi_imx_config *config) ...@@ -625,9 +639,12 @@ static int mx1_config(struct spi_device *spi, struct spi_imx_config *config)
{ {
struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master); struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER; unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
unsigned int clk;
reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) << reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz, &clk) <<
MX1_CSPICTRL_DR_SHIFT; MX1_CSPICTRL_DR_SHIFT;
spi_imx->spi_bus_clk = clk;
reg |= config->bpw - 1; reg |= config->bpw - 1;
if (spi->mode & SPI_CPHA) if (spi->mode & SPI_CPHA)
...@@ -1179,7 +1196,7 @@ static int spi_imx_probe(struct platform_device *pdev) ...@@ -1179,7 +1196,7 @@ static int spi_imx_probe(struct platform_device *pdev)
spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message; spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message; spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
if (is_imx51_ecspi(spi_imx)) if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx))
spi_imx->bitbang.master->mode_bits |= SPI_LOOP; spi_imx->bitbang.master->mode_bits |= SPI_LOOP;
init_completion(&spi_imx->xfer_done); init_completion(&spi_imx->xfer_done);
...@@ -1251,6 +1268,12 @@ static int spi_imx_probe(struct platform_device *pdev) ...@@ -1251,6 +1268,12 @@ static int spi_imx_probe(struct platform_device *pdev)
goto out_clk_put; goto out_clk_put;
} }
if (!master->cs_gpios) {
dev_err(&pdev->dev, "No CS GPIOs available\n");
ret = -EINVAL;
goto out_clk_put;
}
for (i = 0; i < master->num_chipselect; i++) { for (i = 0; i < master->num_chipselect; i++) {
if (!gpio_is_valid(master->cs_gpios[i])) if (!gpio_is_valid(master->cs_gpios[i]))
continue; continue;
......
/*
* J-Core SPI controller driver
*
* Copyright (C) 2012-2016 Smart Energy Instruments, Inc.
*
* Current version by Rich Felker
* Based loosely on initial version by Oleksandr G Zhadan
*
*/
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/delay.h>
#define DRV_NAME "jcore_spi"
#define CTRL_REG 0x0
#define DATA_REG 0x4
#define JCORE_SPI_CTRL_XMIT 0x02
#define JCORE_SPI_STAT_BUSY 0x02
#define JCORE_SPI_CTRL_LOOP 0x08
#define JCORE_SPI_CTRL_CS_BITS 0x15
#define JCORE_SPI_WAIT_RDY_MAX_LOOP 2000000
struct jcore_spi {
struct spi_master *master;
void __iomem *base;
unsigned int cs_reg;
unsigned int speed_reg;
unsigned int speed_hz;
unsigned int clock_freq;
};
static int jcore_spi_wait(void __iomem *ctrl_reg)
{
unsigned timeout = JCORE_SPI_WAIT_RDY_MAX_LOOP;
do {
if (!(readl(ctrl_reg) & JCORE_SPI_STAT_BUSY))
return 0;
cpu_relax();
} while (--timeout);
return -EBUSY;
}
static void jcore_spi_program(struct jcore_spi *hw)
{
void __iomem *ctrl_reg = hw->base + CTRL_REG;
if (jcore_spi_wait(ctrl_reg))
dev_err(hw->master->dev.parent,
"timeout waiting to program ctrl reg.\n");
writel(hw->cs_reg | hw->speed_reg, ctrl_reg);
}
static void jcore_spi_chipsel(struct spi_device *spi, bool value)
{
struct jcore_spi *hw = spi_master_get_devdata(spi->master);
u32 csbit = 1U << (2 * spi->chip_select);
dev_dbg(hw->master->dev.parent, "chipselect %d\n", spi->chip_select);
if (value)
hw->cs_reg |= csbit;
else
hw->cs_reg &= ~csbit;
jcore_spi_program(hw);
}
static void jcore_spi_baudrate(struct jcore_spi *hw, int speed)
{
if (speed == hw->speed_hz) return;
hw->speed_hz = speed;
if (speed >= hw->clock_freq / 2)
hw->speed_reg = 0;
else
hw->speed_reg = ((hw->clock_freq / 2 / speed) - 1) << 27;
jcore_spi_program(hw);
dev_dbg(hw->master->dev.parent, "speed=%d reg=0x%x\n",
speed, hw->speed_reg);
}
static int jcore_spi_txrx(struct spi_master *master, struct spi_device *spi,
struct spi_transfer *t)
{
struct jcore_spi *hw = spi_master_get_devdata(master);
void __iomem *ctrl_reg = hw->base + CTRL_REG;
void __iomem *data_reg = hw->base + DATA_REG;
u32 xmit;
/* data buffers */
const unsigned char *tx;
unsigned char *rx;
unsigned int len;
unsigned int count;
jcore_spi_baudrate(hw, t->speed_hz);
xmit = hw->cs_reg | hw->speed_reg | JCORE_SPI_CTRL_XMIT;
tx = t->tx_buf;
rx = t->rx_buf;
len = t->len;
for (count = 0; count < len; count++) {
if (jcore_spi_wait(ctrl_reg))
break;
writel(tx ? *tx++ : 0, data_reg);
writel(xmit, ctrl_reg);
if (jcore_spi_wait(ctrl_reg))
break;
if (rx)
*rx++ = readl(data_reg);
}
spi_finalize_current_transfer(master);
if (count < len)
return -EREMOTEIO;
return 0;
}
static int jcore_spi_probe(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
struct jcore_spi *hw;
struct spi_master *master;
struct resource *res;
u32 clock_freq;
struct clk *clk;
int err = -ENODEV;
master = spi_alloc_master(&pdev->dev, sizeof(struct jcore_spi));
if (!master)
return err;
/* Setup the master state. */
master->num_chipselect = 3;
master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
master->transfer_one = jcore_spi_txrx;
master->set_cs = jcore_spi_chipsel;
master->dev.of_node = node;
master->bus_num = pdev->id;
hw = spi_master_get_devdata(master);
hw->master = master;
platform_set_drvdata(pdev, hw);
/* Find and map our resources */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
goto exit_busy;
if (!devm_request_mem_region(&pdev->dev, res->start,
resource_size(res), pdev->name))
goto exit_busy;
hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
resource_size(res));
if (!hw->base)
goto exit_busy;
/*
* The SPI clock rate controlled via a configurable clock divider
* which is applied to the reference clock. A 50 MHz reference is
* most suitable for obtaining standard SPI clock rates, but some
* designs may have a different reference clock, and the DT must
* make the driver aware so that it can properly program the
* requested rate. If the clock is omitted, 50 MHz is assumed.
*/
clock_freq = 50000000;
clk = devm_clk_get(&pdev->dev, "ref_clk");
if (!IS_ERR_OR_NULL(clk)) {
if (clk_enable(clk) == 0)
clock_freq = clk_get_rate(clk);
else
dev_warn(&pdev->dev, "could not enable ref_clk\n");
}
hw->clock_freq = clock_freq;
/* Initialize all CS bits to high. */
hw->cs_reg = JCORE_SPI_CTRL_CS_BITS;
jcore_spi_baudrate(hw, 400000);
/* Register our spi controller */
err = devm_spi_register_master(&pdev->dev, master);
if (err)
goto exit;
return 0;
exit_busy:
err = -EBUSY;
exit:
spi_master_put(master);
return err;
}
static const struct of_device_id jcore_spi_of_match[] = {
{ .compatible = "jcore,spi2" },
{},
};
static struct platform_driver jcore_spi_driver = {
.probe = jcore_spi_probe,
.driver = {
.name = DRV_NAME,
.of_match_table = jcore_spi_of_match,
},
};
module_platform_driver(jcore_spi_driver);
MODULE_DESCRIPTION("J-Core SPI driver");
MODULE_AUTHOR("Rich Felker <dalias@libc.org>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);
...@@ -405,7 +405,7 @@ struct rx_ranges { ...@@ -405,7 +405,7 @@ struct rx_ranges {
u8 *end; u8 *end;
}; };
int rx_ranges_cmp(void *priv, struct list_head *a, struct list_head *b) static int rx_ranges_cmp(void *priv, struct list_head *a, struct list_head *b)
{ {
struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list); struct rx_ranges *rx_a = list_entry(a, struct rx_ranges, list);
struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list); struct rx_ranges *rx_b = list_entry(b, struct rx_ranges, list);
......
...@@ -442,6 +442,7 @@ static const struct dev_pm_ops meson_spifc_pm_ops = { ...@@ -442,6 +442,7 @@ static const struct dev_pm_ops meson_spifc_pm_ops = {
static const struct of_device_id meson_spifc_dt_match[] = { static const struct of_device_id meson_spifc_dt_match[] = {
{ .compatible = "amlogic,meson6-spifc", }, { .compatible = "amlogic,meson6-spifc", },
{ .compatible = "amlogic,meson-gxbb-spifc", },
{ }, { },
}; };
MODULE_DEVICE_TABLE(of, meson_spifc_dt_match); MODULE_DEVICE_TABLE(of, meson_spifc_dt_match);
......
...@@ -312,6 +312,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv) ...@@ -312,6 +312,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
* @flags: other constraints relevant to this driver * @flags: other constraints relevant to this driver
* @max_transfer_size: function that returns the max transfer size for * @max_transfer_size: function that returns the max transfer size for
* a &spi_device; may be %NULL, so the default %SIZE_MAX will be used. * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
* @max_message_size: function that returns the max message size for
* a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
* @io_mutex: mutex for physical bus access * @io_mutex: mutex for physical bus access
* @bus_lock_spinlock: spinlock for SPI bus locking * @bus_lock_spinlock: spinlock for SPI bus locking
* @bus_lock_mutex: mutex for exclusion of multiple callers * @bus_lock_mutex: mutex for exclusion of multiple callers
...@@ -442,10 +444,11 @@ struct spi_master { ...@@ -442,10 +444,11 @@ struct spi_master {
#define SPI_MASTER_MUST_TX BIT(4) /* requires tx */ #define SPI_MASTER_MUST_TX BIT(4) /* requires tx */
/* /*
* on some hardware transfer size may be constrained * on some hardware transfer / message size may be constrained
* the limit may depend on device transfer settings * the limit may depend on device transfer settings
*/ */
size_t (*max_transfer_size)(struct spi_device *spi); size_t (*max_transfer_size)(struct spi_device *spi);
size_t (*max_message_size)(struct spi_device *spi);
/* I/O mutex */ /* I/O mutex */
struct mutex io_mutex; struct mutex io_mutex;
...@@ -905,12 +908,26 @@ extern int spi_async_locked(struct spi_device *spi, ...@@ -905,12 +908,26 @@ extern int spi_async_locked(struct spi_device *spi,
struct spi_message *message); struct spi_message *message);
static inline size_t static inline size_t
spi_max_transfer_size(struct spi_device *spi) spi_max_message_size(struct spi_device *spi)
{ {
struct spi_master *master = spi->master; struct spi_master *master = spi->master;
if (!master->max_transfer_size) if (!master->max_message_size)
return SIZE_MAX; return SIZE_MAX;
return master->max_transfer_size(spi); return master->max_message_size(spi);
}
static inline size_t
spi_max_transfer_size(struct spi_device *spi)
{
struct spi_master *master = spi->master;
size_t tr_max = SIZE_MAX;
size_t msg_max = spi_max_message_size(spi);
if (master->max_transfer_size)
tr_max = master->max_transfer_size(spi);
/* transfer size limit must not be greater than messsage size limit */
return min(tr_max, msg_max);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment