2022-06-07 07:38:33 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0)
|
|
|
|
/*
|
|
|
|
* Microchip CoreSPI SPI controller driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018-2022 Microchip Technology Inc. and its subsidiaries
|
|
|
|
*
|
|
|
|
* Author: Daire McNamara <daire.mcnamara@microchip.com>
|
|
|
|
* Author: Conor Dooley <conor.dooley@microchip.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
|
|
|
|
#define MAX_LEN (0xffff)
|
2024-05-14 10:45:07 +00:00
|
|
|
#define MAX_CS (1)
|
2022-06-07 07:38:33 +00:00
|
|
|
#define DEFAULT_FRAMESIZE (8)
|
|
|
|
#define FIFO_DEPTH (32)
|
|
|
|
#define CLK_GEN_MODE1_MAX (255)
|
|
|
|
#define CLK_GEN_MODE0_MAX (15)
|
|
|
|
#define CLK_GEN_MIN (0)
|
|
|
|
#define MODE_X_MASK_SHIFT (24)
|
|
|
|
|
|
|
|
#define CONTROL_ENABLE BIT(0)
|
|
|
|
#define CONTROL_MASTER BIT(1)
|
|
|
|
#define CONTROL_RX_DATA_INT BIT(4)
|
|
|
|
#define CONTROL_TX_DATA_INT BIT(5)
|
|
|
|
#define CONTROL_RX_OVER_INT BIT(6)
|
|
|
|
#define CONTROL_TX_UNDER_INT BIT(7)
|
|
|
|
#define CONTROL_SPO BIT(24)
|
|
|
|
#define CONTROL_SPH BIT(25)
|
|
|
|
#define CONTROL_SPS BIT(26)
|
|
|
|
#define CONTROL_FRAMEURUN BIT(27)
|
|
|
|
#define CONTROL_CLKMODE BIT(28)
|
|
|
|
#define CONTROL_BIGFIFO BIT(29)
|
|
|
|
#define CONTROL_OENOFF BIT(30)
|
|
|
|
#define CONTROL_RESET BIT(31)
|
|
|
|
|
|
|
|
#define CONTROL_MODE_MASK GENMASK(3, 2)
|
|
|
|
#define MOTOROLA_MODE (0)
|
|
|
|
#define CONTROL_FRAMECNT_MASK GENMASK(23, 8)
|
|
|
|
#define CONTROL_FRAMECNT_SHIFT (8)
|
|
|
|
|
|
|
|
#define STATUS_ACTIVE BIT(14)
|
|
|
|
#define STATUS_SSEL BIT(13)
|
|
|
|
#define STATUS_FRAMESTART BIT(12)
|
|
|
|
#define STATUS_TXFIFO_EMPTY_NEXT_READ BIT(11)
|
|
|
|
#define STATUS_TXFIFO_EMPTY BIT(10)
|
|
|
|
#define STATUS_TXFIFO_FULL_NEXT_WRITE BIT(9)
|
|
|
|
#define STATUS_TXFIFO_FULL BIT(8)
|
|
|
|
#define STATUS_RXFIFO_EMPTY_NEXT_READ BIT(7)
|
|
|
|
#define STATUS_RXFIFO_EMPTY BIT(6)
|
|
|
|
#define STATUS_RXFIFO_FULL_NEXT_WRITE BIT(5)
|
|
|
|
#define STATUS_RXFIFO_FULL BIT(4)
|
|
|
|
#define STATUS_TX_UNDERRUN BIT(3)
|
|
|
|
#define STATUS_RX_OVERFLOW BIT(2)
|
|
|
|
#define STATUS_RXDAT_RXED BIT(1)
|
|
|
|
#define STATUS_TXDAT_SENT BIT(0)
|
|
|
|
|
|
|
|
#define INT_TXDONE BIT(0)
|
|
|
|
#define INT_RXRDY BIT(1)
|
|
|
|
#define INT_RX_CHANNEL_OVERFLOW BIT(2)
|
|
|
|
#define INT_TX_CHANNEL_UNDERRUN BIT(3)
|
|
|
|
|
|
|
|
#define INT_ENABLE_MASK (CONTROL_RX_DATA_INT | CONTROL_TX_DATA_INT | \
|
|
|
|
CONTROL_RX_OVER_INT | CONTROL_TX_UNDER_INT)
|
|
|
|
|
|
|
|
#define REG_CONTROL (0x00)
|
|
|
|
#define REG_FRAME_SIZE (0x04)
|
2024-07-15 11:13:54 +00:00
|
|
|
#define FRAME_SIZE_MASK GENMASK(5, 0)
|
2022-06-07 07:38:33 +00:00
|
|
|
#define REG_STATUS (0x08)
|
|
|
|
#define REG_INT_CLEAR (0x0c)
|
|
|
|
#define REG_RX_DATA (0x10)
|
|
|
|
#define REG_TX_DATA (0x14)
|
|
|
|
#define REG_CLK_GEN (0x18)
|
|
|
|
#define REG_SLAVE_SELECT (0x1c)
|
|
|
|
#define SSEL_MASK GENMASK(7, 0)
|
|
|
|
#define SSEL_DIRECT BIT(8)
|
|
|
|
#define SSELOUT_SHIFT 9
|
|
|
|
#define SSELOUT BIT(SSELOUT_SHIFT)
|
|
|
|
#define REG_MIS (0x20)
|
|
|
|
#define REG_RIS (0x24)
|
|
|
|
#define REG_CONTROL2 (0x28)
|
|
|
|
#define REG_COMMAND (0x2c)
|
2024-07-15 11:13:54 +00:00
|
|
|
#define COMMAND_CLRFRAMECNT BIT(4)
|
2024-07-15 11:13:56 +00:00
|
|
|
#define COMMAND_TXFIFORST BIT(3)
|
|
|
|
#define COMMAND_RXFIFORST BIT(2)
|
2022-06-07 07:38:33 +00:00
|
|
|
#define REG_PKTSIZE (0x30)
|
|
|
|
#define REG_CMD_SIZE (0x34)
|
|
|
|
#define REG_HWSTATUS (0x38)
|
|
|
|
#define REG_STAT8 (0x3c)
|
|
|
|
#define REG_CTRL2 (0x48)
|
|
|
|
#define REG_FRAMESUP (0x50)
|
|
|
|
|
|
|
|
struct mchp_corespi {
|
|
|
|
void __iomem *regs;
|
|
|
|
struct clk *clk;
|
|
|
|
const u8 *tx_buf;
|
|
|
|
u8 *rx_buf;
|
|
|
|
u32 clk_gen; /* divider for spi output clock generated by the controller */
|
|
|
|
u32 clk_mode;
|
2024-07-15 11:13:53 +00:00
|
|
|
u32 pending_slave_select;
|
2022-06-07 07:38:33 +00:00
|
|
|
int irq;
|
|
|
|
int tx_len;
|
|
|
|
int rx_len;
|
2024-07-15 11:13:57 +00:00
|
|
|
int n_bytes;
|
2022-06-07 07:38:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline u32 mchp_corespi_read(struct mchp_corespi *spi, unsigned int reg)
|
|
|
|
{
|
|
|
|
return readl(spi->regs + reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_write(struct mchp_corespi *spi, unsigned int reg, u32 val)
|
|
|
|
{
|
|
|
|
writel(val, spi->regs + reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_disable(struct mchp_corespi *spi)
|
|
|
|
{
|
|
|
|
u32 control = mchp_corespi_read(spi, REG_CONTROL);
|
|
|
|
|
|
|
|
control &= ~CONTROL_ENABLE;
|
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi)
|
|
|
|
{
|
2024-07-15 11:13:57 +00:00
|
|
|
while (spi->rx_len >= spi->n_bytes && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)) {
|
|
|
|
u32 data = mchp_corespi_read(spi, REG_RX_DATA);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:57 +00:00
|
|
|
spi->rx_len -= spi->n_bytes;
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:57 +00:00
|
|
|
if (!spi->rx_buf)
|
|
|
|
continue;
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:57 +00:00
|
|
|
if (spi->n_bytes == 4)
|
|
|
|
*((u32 *)spi->rx_buf) = data;
|
|
|
|
else if (spi->n_bytes == 2)
|
|
|
|
*((u16 *)spi->rx_buf) = data;
|
|
|
|
else
|
|
|
|
*spi->rx_buf = data;
|
|
|
|
|
|
|
|
spi->rx_buf += spi->n_bytes;
|
2022-06-07 07:38:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mchp_corespi_enable_ints(struct mchp_corespi *spi)
|
|
|
|
{
|
2024-07-15 11:13:54 +00:00
|
|
|
u32 control = mchp_corespi_read(spi, REG_CONTROL);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:54 +00:00
|
|
|
control |= INT_ENABLE_MASK;
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mchp_corespi_disable_ints(struct mchp_corespi *spi)
|
|
|
|
{
|
2024-07-15 11:13:54 +00:00
|
|
|
u32 control = mchp_corespi_read(spi, REG_CONTROL);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:54 +00:00
|
|
|
control &= ~INT_ENABLE_MASK;
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_set_xfer_size(struct mchp_corespi *spi, int len)
|
|
|
|
{
|
|
|
|
u32 control;
|
2024-07-15 11:13:54 +00:00
|
|
|
u32 lenpart;
|
|
|
|
u32 frames = mchp_corespi_read(spi, REG_FRAMESUP);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
/*
|
2024-07-15 11:13:54 +00:00
|
|
|
* Writing to FRAMECNT in REG_CONTROL will reset the frame count, taking
|
|
|
|
* a shortcut requires an explicit clear.
|
2022-06-07 07:38:33 +00:00
|
|
|
*/
|
2024-07-15 11:13:54 +00:00
|
|
|
if (frames == len) {
|
|
|
|
mchp_corespi_write(spi, REG_COMMAND, COMMAND_CLRFRAMECNT);
|
|
|
|
return;
|
|
|
|
}
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The lower 16 bits of the frame count are stored in the control reg
|
|
|
|
* for legacy reasons, but the upper 16 written to a different register:
|
|
|
|
* FRAMESUP. While both the upper and lower bits can be *READ* from the
|
2024-07-15 11:13:54 +00:00
|
|
|
* FRAMESUP register, writing to the lower 16 bits is (supposedly) a NOP.
|
|
|
|
*
|
|
|
|
* The driver used to disable the controller while modifying the frame
|
|
|
|
* count, and mask off the lower 16 bits of len while writing to
|
|
|
|
* FRAMES_UP. When the driver was changed to disable the controller as
|
|
|
|
* infrequently as possible, it was discovered that the logic of
|
|
|
|
* lenpart = len & 0xffff_0000
|
|
|
|
* write(REG_FRAMESUP, lenpart)
|
|
|
|
* would actually write zeros into the lower 16 bits on an mpfs250t-es,
|
|
|
|
* despite documentation stating these bits were read-only.
|
|
|
|
* Writing len unmasked into FRAMES_UP ensures those bits aren't zeroed
|
|
|
|
* on an mpfs250t-es and will be a NOP for the lower 16 bits on hardware
|
|
|
|
* that matches the documentation.
|
2022-06-07 07:38:33 +00:00
|
|
|
*/
|
|
|
|
lenpart = len & 0xffff;
|
|
|
|
control = mchp_corespi_read(spi, REG_CONTROL);
|
|
|
|
control &= ~CONTROL_FRAMECNT_MASK;
|
|
|
|
control |= lenpart << CONTROL_FRAMECNT_SHIFT;
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
2024-07-15 11:13:54 +00:00
|
|
|
mchp_corespi_write(spi, REG_FRAMESUP, len);
|
2022-06-07 07:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi)
|
|
|
|
{
|
|
|
|
int fifo_max, i = 0;
|
|
|
|
|
2024-07-15 11:13:57 +00:00
|
|
|
fifo_max = DIV_ROUND_UP(min(spi->tx_len, FIFO_DEPTH), spi->n_bytes);
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_set_xfer_size(spi, fifo_max);
|
|
|
|
|
|
|
|
while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_TXFIFO_FULL)) {
|
2024-07-15 11:13:57 +00:00
|
|
|
u32 word;
|
|
|
|
|
|
|
|
if (spi->n_bytes == 4)
|
|
|
|
word = spi->tx_buf ? *((u32 *)spi->tx_buf) : 0xaa;
|
|
|
|
else if (spi->n_bytes == 2)
|
|
|
|
word = spi->tx_buf ? *((u16 *)spi->tx_buf) : 0xaa;
|
|
|
|
else
|
|
|
|
word = spi->tx_buf ? *spi->tx_buf : 0xaa;
|
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_TX_DATA, word);
|
|
|
|
if (spi->tx_buf)
|
|
|
|
spi->tx_buf += spi->n_bytes;
|
2022-06-07 07:38:33 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:13:57 +00:00
|
|
|
spi->tx_len -= i * spi->n_bytes;
|
2022-06-07 07:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_set_framesize(struct mchp_corespi *spi, int bt)
|
|
|
|
{
|
2024-07-15 11:13:54 +00:00
|
|
|
u32 frame_size = mchp_corespi_read(spi, REG_FRAME_SIZE);
|
2022-06-07 07:38:33 +00:00
|
|
|
u32 control;
|
|
|
|
|
2024-07-15 11:13:54 +00:00
|
|
|
if ((frame_size & FRAME_SIZE_MASK) == bt)
|
|
|
|
return;
|
|
|
|
|
2022-06-07 07:38:33 +00:00
|
|
|
/*
|
|
|
|
* Disable the SPI controller. Writes to the frame size have
|
|
|
|
* no effect when the controller is enabled.
|
|
|
|
*/
|
2024-07-15 11:13:54 +00:00
|
|
|
control = mchp_corespi_read(spi, REG_CONTROL);
|
|
|
|
control &= ~CONTROL_ENABLE;
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_FRAME_SIZE, bt);
|
|
|
|
|
|
|
|
control |= CONTROL_ENABLE;
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
|
|
|
|
{
|
|
|
|
u32 reg;
|
2023-08-23 03:29:48 +00:00
|
|
|
struct mchp_corespi *corespi = spi_controller_get_devdata(spi->controller);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
|
2023-03-10 17:32:03 +00:00
|
|
|
reg &= ~BIT(spi_get_chipselect(spi, 0));
|
|
|
|
reg |= !disable << spi_get_chipselect(spi, 0);
|
2024-07-15 11:13:53 +00:00
|
|
|
corespi->pending_slave_select = reg;
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:53 +00:00
|
|
|
/*
|
|
|
|
* Only deassert chip select immediately. Writing to some registers
|
|
|
|
* requires the controller to be disabled, which results in the
|
|
|
|
* output pins being tristated and can cause the SCLK and MOSI lines
|
|
|
|
* to transition. Therefore asserting the chip select is deferred
|
|
|
|
* until just before writing to the TX FIFO, to ensure the device
|
|
|
|
* doesn't see any spurious clock transitions whilst CS is enabled.
|
|
|
|
*/
|
|
|
|
if (((spi->mode & SPI_CS_HIGH) == 0) == disable)
|
|
|
|
mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
|
2022-06-07 07:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mchp_corespi_setup(struct spi_device *spi)
|
|
|
|
{
|
2023-08-23 03:29:48 +00:00
|
|
|
struct mchp_corespi *corespi = spi_controller_get_devdata(spi->controller);
|
2022-06-07 07:38:33 +00:00
|
|
|
u32 reg;
|
|
|
|
|
2024-05-14 10:45:08 +00:00
|
|
|
if (spi_is_csgpiod(spi))
|
|
|
|
return 0;
|
|
|
|
|
2022-06-07 07:38:33 +00:00
|
|
|
/*
|
2023-08-23 03:29:48 +00:00
|
|
|
* Active high targets need to be specifically set to their inactive
|
2022-06-07 07:38:33 +00:00
|
|
|
* states during probe by adding them to the "control group" & thus
|
|
|
|
* driving their select line low.
|
|
|
|
*/
|
|
|
|
if (spi->mode & SPI_CS_HIGH) {
|
|
|
|
reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
|
2023-03-10 17:32:03 +00:00
|
|
|
reg |= BIT(spi_get_chipselect(spi, 0));
|
2024-07-15 11:13:53 +00:00
|
|
|
corespi->pending_slave_select = reg;
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
static void mchp_corespi_init(struct spi_controller *host, struct mchp_corespi *spi)
|
2022-06-07 07:38:33 +00:00
|
|
|
{
|
|
|
|
unsigned long clk_hz;
|
|
|
|
u32 control = mchp_corespi_read(spi, REG_CONTROL);
|
|
|
|
|
2024-07-15 11:13:55 +00:00
|
|
|
control &= ~CONTROL_ENABLE;
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:55 +00:00
|
|
|
control |= CONTROL_MASTER;
|
2022-06-07 07:38:33 +00:00
|
|
|
control &= ~CONTROL_MODE_MASK;
|
|
|
|
control |= MOTOROLA_MODE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The controller must be configured so that it doesn't remove Chip
|
|
|
|
* Select until the entire message has been transferred, even if at
|
|
|
|
* some points TX FIFO becomes empty.
|
|
|
|
*
|
|
|
|
* BIGFIFO mode is also enabled, which sets the fifo depth to 32 frames
|
|
|
|
* for the 8 bit transfers that this driver uses.
|
|
|
|
*/
|
|
|
|
control |= CONTROL_SPS | CONTROL_BIGFIFO;
|
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
|
2024-07-15 11:13:55 +00:00
|
|
|
mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE);
|
|
|
|
|
|
|
|
/* max. possible spi clock rate is the apb clock rate */
|
|
|
|
clk_hz = clk_get_rate(spi->clk);
|
|
|
|
host->max_speed_hz = clk_hz;
|
|
|
|
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_enable_ints(spi);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is required to enable direct mode, otherwise control over the chip
|
|
|
|
* select is relinquished to the hardware. SSELOUT is enabled too so we
|
2023-08-23 03:29:48 +00:00
|
|
|
* can deal with active high targets.
|
2022-06-07 07:38:33 +00:00
|
|
|
*/
|
2024-07-15 11:13:53 +00:00
|
|
|
spi->pending_slave_select = SSELOUT | SSEL_DIRECT;
|
|
|
|
mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
control = mchp_corespi_read(spi, REG_CONTROL);
|
|
|
|
|
|
|
|
control &= ~CONTROL_RESET;
|
|
|
|
control |= CONTROL_ENABLE;
|
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_set_clk_gen(struct mchp_corespi *spi)
|
|
|
|
{
|
|
|
|
u32 control;
|
|
|
|
|
|
|
|
control = mchp_corespi_read(spi, REG_CONTROL);
|
|
|
|
if (spi->clk_mode)
|
|
|
|
control |= CONTROL_CLKMODE;
|
|
|
|
else
|
|
|
|
control &= ~CONTROL_CLKMODE;
|
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_CLK_GEN, spi->clk_gen);
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void mchp_corespi_set_mode(struct mchp_corespi *spi, unsigned int mode)
|
|
|
|
{
|
2024-07-15 11:13:54 +00:00
|
|
|
u32 mode_val;
|
|
|
|
u32 control = mchp_corespi_read(spi, REG_CONTROL);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
switch (mode & SPI_MODE_X_MASK) {
|
|
|
|
case SPI_MODE_0:
|
|
|
|
mode_val = 0;
|
|
|
|
break;
|
|
|
|
case SPI_MODE_1:
|
|
|
|
mode_val = CONTROL_SPH;
|
|
|
|
break;
|
|
|
|
case SPI_MODE_2:
|
|
|
|
mode_val = CONTROL_SPO;
|
|
|
|
break;
|
|
|
|
case SPI_MODE_3:
|
|
|
|
mode_val = CONTROL_SPH | CONTROL_SPO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-07-15 11:13:54 +00:00
|
|
|
* Disable the SPI controller. Writes to the frame protocol have
|
2022-06-07 07:38:33 +00:00
|
|
|
* no effect when the controller is enabled.
|
|
|
|
*/
|
|
|
|
|
2024-07-15 11:13:54 +00:00
|
|
|
control &= ~CONTROL_ENABLE;
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
|
2022-06-07 07:38:33 +00:00
|
|
|
control &= ~(SPI_MODE_X_MASK << MODE_X_MASK_SHIFT);
|
|
|
|
control |= mode_val;
|
|
|
|
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
|
|
|
|
control |= CONTROL_ENABLE;
|
|
|
|
mchp_corespi_write(spi, REG_CONTROL, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t mchp_corespi_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
2023-08-23 03:29:48 +00:00
|
|
|
struct spi_controller *host = dev_id;
|
|
|
|
struct mchp_corespi *spi = spi_controller_get_devdata(host);
|
2022-06-07 07:38:33 +00:00
|
|
|
u32 intfield = mchp_corespi_read(spi, REG_MIS) & 0xf;
|
|
|
|
bool finalise = false;
|
|
|
|
|
|
|
|
/* Interrupt line may be shared and not for us at all */
|
|
|
|
if (intfield == 0)
|
|
|
|
return IRQ_NONE;
|
|
|
|
|
2024-07-15 11:13:52 +00:00
|
|
|
if (intfield & INT_TXDONE)
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_write(spi, REG_INT_CLEAR, INT_TXDONE);
|
|
|
|
|
2024-07-15 11:13:52 +00:00
|
|
|
if (intfield & INT_RXRDY) {
|
|
|
|
mchp_corespi_write(spi, REG_INT_CLEAR, INT_RXRDY);
|
|
|
|
|
2022-06-07 07:38:33 +00:00
|
|
|
if (spi->rx_len)
|
|
|
|
mchp_corespi_read_fifo(spi);
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:13:52 +00:00
|
|
|
if (!spi->rx_len && !spi->tx_len)
|
|
|
|
finalise = true;
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
if (intfield & INT_RX_CHANNEL_OVERFLOW) {
|
|
|
|
mchp_corespi_write(spi, REG_INT_CLEAR, INT_RX_CHANNEL_OVERFLOW);
|
|
|
|
finalise = true;
|
2023-08-23 03:29:48 +00:00
|
|
|
dev_err(&host->dev,
|
2022-06-07 07:38:33 +00:00
|
|
|
"%s: RX OVERFLOW: rxlen: %d, txlen: %d\n", __func__,
|
|
|
|
spi->rx_len, spi->tx_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intfield & INT_TX_CHANNEL_UNDERRUN) {
|
|
|
|
mchp_corespi_write(spi, REG_INT_CLEAR, INT_TX_CHANNEL_UNDERRUN);
|
|
|
|
finalise = true;
|
2023-08-23 03:29:48 +00:00
|
|
|
dev_err(&host->dev,
|
2022-06-07 07:38:33 +00:00
|
|
|
"%s: TX UNDERFLOW: rxlen: %d, txlen: %d\n", __func__,
|
|
|
|
spi->rx_len, spi->tx_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (finalise)
|
2023-08-23 03:29:48 +00:00
|
|
|
spi_finalize_current_transfer(host);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mchp_corespi_calculate_clkgen(struct mchp_corespi *spi,
|
|
|
|
unsigned long target_hz)
|
|
|
|
{
|
|
|
|
unsigned long clk_hz, spi_hz, clk_gen;
|
|
|
|
|
|
|
|
clk_hz = clk_get_rate(spi->clk);
|
2022-06-15 14:20:29 +00:00
|
|
|
if (!clk_hz)
|
|
|
|
return -EINVAL;
|
2022-06-07 07:38:33 +00:00
|
|
|
spi_hz = min(target_hz, clk_hz);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There are two possible clock modes for the controller generated
|
|
|
|
* clock's division ratio:
|
|
|
|
* CLK_MODE = 0: 1 / (2^(CLK_GEN + 1)) where CLK_GEN = 0 to 15.
|
|
|
|
* CLK_MODE = 1: 1 / (2 * CLK_GEN + 1) where CLK_GEN = 0 to 255.
|
|
|
|
* First try mode 1, fall back to 0 and if we have tried both modes and
|
|
|
|
* we /still/ can't get a good setting, we then throw the toys out of
|
|
|
|
* the pram and give up
|
|
|
|
* clk_gen is the register name for the clock divider on MPFS.
|
|
|
|
*/
|
|
|
|
clk_gen = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
|
|
|
|
if (clk_gen > CLK_GEN_MODE1_MAX || clk_gen <= CLK_GEN_MIN) {
|
|
|
|
clk_gen = DIV_ROUND_UP(clk_hz, spi_hz);
|
|
|
|
clk_gen = fls(clk_gen) - 1;
|
|
|
|
|
|
|
|
if (clk_gen > CLK_GEN_MODE0_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
spi->clk_mode = 0;
|
|
|
|
} else {
|
|
|
|
spi->clk_mode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
spi->clk_gen = clk_gen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
static int mchp_corespi_transfer_one(struct spi_controller *host,
|
2022-06-07 07:38:33 +00:00
|
|
|
struct spi_device *spi_dev,
|
|
|
|
struct spi_transfer *xfer)
|
|
|
|
{
|
2023-08-23 03:29:48 +00:00
|
|
|
struct mchp_corespi *spi = spi_controller_get_devdata(host);
|
2022-06-07 07:38:33 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mchp_corespi_calculate_clkgen(spi, (unsigned long)xfer->speed_hz);
|
|
|
|
if (ret) {
|
2023-08-23 03:29:48 +00:00
|
|
|
dev_err(&host->dev, "failed to set clk_gen for target %u Hz\n", xfer->speed_hz);
|
2022-06-07 07:38:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
mchp_corespi_set_clk_gen(spi);
|
|
|
|
|
|
|
|
spi->tx_buf = xfer->tx_buf;
|
|
|
|
spi->rx_buf = xfer->rx_buf;
|
|
|
|
spi->tx_len = xfer->len;
|
|
|
|
spi->rx_len = xfer->len;
|
2024-07-15 11:13:57 +00:00
|
|
|
spi->n_bytes = roundup_pow_of_two(DIV_ROUND_UP(xfer->bits_per_word, BITS_PER_BYTE));
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:57 +00:00
|
|
|
mchp_corespi_set_framesize(spi, xfer->bits_per_word);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2024-07-15 11:13:56 +00:00
|
|
|
mchp_corespi_write(spi, REG_COMMAND, COMMAND_RXFIFORST | COMMAND_TXFIFORST);
|
|
|
|
|
2024-07-15 11:13:53 +00:00
|
|
|
mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select);
|
|
|
|
|
2024-07-15 11:13:52 +00:00
|
|
|
while (spi->tx_len)
|
2022-06-07 07:38:33 +00:00
|
|
|
mchp_corespi_write_fifo(spi);
|
2024-07-15 11:13:52 +00:00
|
|
|
|
2022-06-07 07:38:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
static int mchp_corespi_prepare_message(struct spi_controller *host,
|
2022-06-07 07:38:33 +00:00
|
|
|
struct spi_message *msg)
|
|
|
|
{
|
|
|
|
struct spi_device *spi_dev = msg->spi;
|
2023-08-23 03:29:48 +00:00
|
|
|
struct mchp_corespi *spi = spi_controller_get_devdata(host);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
mchp_corespi_set_mode(spi, spi_dev->mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mchp_corespi_probe(struct platform_device *pdev)
|
|
|
|
{
|
2023-08-23 03:29:48 +00:00
|
|
|
struct spi_controller *host;
|
2022-06-07 07:38:33 +00:00
|
|
|
struct mchp_corespi *spi;
|
|
|
|
struct resource *res;
|
|
|
|
u32 num_cs;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
host = devm_spi_alloc_host(&pdev->dev, sizeof(*spi));
|
|
|
|
if (!host)
|
2022-06-07 07:38:33 +00:00
|
|
|
return dev_err_probe(&pdev->dev, -ENOMEM,
|
2023-08-23 03:29:48 +00:00
|
|
|
"unable to allocate host for SPI controller\n");
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
platform_set_drvdata(pdev, host);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
if (of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs))
|
|
|
|
num_cs = MAX_CS;
|
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
host->num_chipselect = num_cs;
|
|
|
|
host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
|
2024-05-14 10:45:08 +00:00
|
|
|
host->use_gpio_descriptors = true;
|
2023-08-23 03:29:48 +00:00
|
|
|
host->setup = mchp_corespi_setup;
|
2024-07-15 11:13:57 +00:00
|
|
|
host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
|
2023-08-23 03:29:48 +00:00
|
|
|
host->transfer_one = mchp_corespi_transfer_one;
|
|
|
|
host->prepare_message = mchp_corespi_prepare_message;
|
|
|
|
host->set_cs = mchp_corespi_set_cs;
|
|
|
|
host->dev.of_node = pdev->dev.of_node;
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
spi = spi_controller_get_devdata(host);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
spi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
|
2022-07-13 02:56:56 +00:00
|
|
|
if (IS_ERR(spi->regs))
|
|
|
|
return PTR_ERR(spi->regs);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
spi->irq = platform_get_irq(pdev, 0);
|
2023-07-28 07:57:29 +00:00
|
|
|
if (spi->irq < 0)
|
|
|
|
return spi->irq;
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
|
2023-08-23 03:29:48 +00:00
|
|
|
IRQF_SHARED, dev_name(&pdev->dev), host);
|
2022-07-13 02:56:57 +00:00
|
|
|
if (ret)
|
|
|
|
return dev_err_probe(&pdev->dev, ret,
|
2022-08-05 21:33:17 +00:00
|
|
|
"could not request irq\n");
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2023-08-23 13:39:31 +00:00
|
|
|
spi->clk = devm_clk_get_enabled(&pdev->dev, NULL);
|
2022-07-13 02:56:57 +00:00
|
|
|
if (IS_ERR(spi->clk))
|
|
|
|
return dev_err_probe(&pdev->dev, PTR_ERR(spi->clk),
|
2022-08-05 21:33:17 +00:00
|
|
|
"could not get clk\n");
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
mchp_corespi_init(host, spi);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
ret = devm_spi_register_controller(&pdev->dev, host);
|
2022-06-07 07:38:33 +00:00
|
|
|
if (ret) {
|
2022-07-13 02:56:57 +00:00
|
|
|
mchp_corespi_disable(spi);
|
|
|
|
return dev_err_probe(&pdev->dev, ret,
|
2023-08-23 03:29:48 +00:00
|
|
|
"unable to register host for SPI controller\n");
|
2022-06-07 07:38:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 03:29:48 +00:00
|
|
|
dev_info(&pdev->dev, "Registered SPI controller %d\n", host->bus_num);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-03 17:19:51 +00:00
|
|
|
static void mchp_corespi_remove(struct platform_device *pdev)
|
2022-06-07 07:38:33 +00:00
|
|
|
{
|
2023-08-23 03:29:48 +00:00
|
|
|
struct spi_controller *host = platform_get_drvdata(pdev);
|
|
|
|
struct mchp_corespi *spi = spi_controller_get_devdata(host);
|
2022-06-07 07:38:33 +00:00
|
|
|
|
|
|
|
mchp_corespi_disable_ints(spi);
|
|
|
|
mchp_corespi_disable(spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MICROCHIP_SPI_PM_OPS (NULL)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Platform driver data structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_OF)
|
|
|
|
static const struct of_device_id mchp_corespi_dt_ids[] = {
|
|
|
|
{ .compatible = "microchip,mpfs-spi" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, mchp_corespi_dt_ids);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct platform_driver mchp_corespi_driver = {
|
|
|
|
.probe = mchp_corespi_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "microchip-corespi",
|
|
|
|
.pm = MICROCHIP_SPI_PM_OPS,
|
|
|
|
.of_match_table = of_match_ptr(mchp_corespi_dt_ids),
|
|
|
|
},
|
2023-03-03 17:19:51 +00:00
|
|
|
.remove_new = mchp_corespi_remove,
|
2022-06-07 07:38:33 +00:00
|
|
|
};
|
|
|
|
module_platform_driver(mchp_corespi_driver);
|
|
|
|
MODULE_DESCRIPTION("Microchip coreSPI SPI controller driver");
|
|
|
|
MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");
|
|
|
|
MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|