12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 264e36824Saddy ke /* 364e36824Saddy ke * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd 45dcc44edSAddy Ke * Author: Addy Ke <addy.ke@rock-chips.com> 564e36824Saddy ke */ 664e36824Saddy ke 764e36824Saddy ke #include <linux/clk.h> 864e36824Saddy ke #include <linux/dmaengine.h> 98af0c18aSSuren Baghdasaryan #include <linux/interrupt.h> 10ec5c5d8aSShawn Lin #include <linux/module.h> 11ec5c5d8aSShawn Lin #include <linux/of.h> 1223e291c2SBrian Norris #include <linux/pinctrl/consumer.h> 13ec5c5d8aSShawn Lin #include <linux/platform_device.h> 14ec5c5d8aSShawn Lin #include <linux/spi/spi.h> 15ec5c5d8aSShawn Lin #include <linux/pm_runtime.h> 16ec5c5d8aSShawn Lin #include <linux/scatterlist.h> 1764e36824Saddy ke 1864e36824Saddy ke #define DRIVER_NAME "rockchip-spi" 1964e36824Saddy ke 20aa099382SJeffy Chen #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \ 21aa099382SJeffy Chen writel_relaxed(readl_relaxed(reg) & ~(bits), reg) 22aa099382SJeffy Chen #define ROCKCHIP_SPI_SET_BITS(reg, bits) \ 23aa099382SJeffy Chen writel_relaxed(readl_relaxed(reg) | (bits), reg) 24aa099382SJeffy Chen 2564e36824Saddy ke /* SPI register offsets */ 2664e36824Saddy ke #define ROCKCHIP_SPI_CTRLR0 0x0000 2764e36824Saddy ke #define ROCKCHIP_SPI_CTRLR1 0x0004 2864e36824Saddy ke #define ROCKCHIP_SPI_SSIENR 0x0008 2964e36824Saddy ke #define ROCKCHIP_SPI_SER 0x000c 3064e36824Saddy ke #define ROCKCHIP_SPI_BAUDR 0x0010 3164e36824Saddy ke #define ROCKCHIP_SPI_TXFTLR 0x0014 3264e36824Saddy ke #define ROCKCHIP_SPI_RXFTLR 0x0018 3364e36824Saddy ke #define ROCKCHIP_SPI_TXFLR 0x001c 3464e36824Saddy ke #define ROCKCHIP_SPI_RXFLR 0x0020 3564e36824Saddy ke #define ROCKCHIP_SPI_SR 0x0024 3664e36824Saddy ke #define ROCKCHIP_SPI_IPR 0x0028 3764e36824Saddy ke #define ROCKCHIP_SPI_IMR 0x002c 3864e36824Saddy ke #define ROCKCHIP_SPI_ISR 0x0030 3964e36824Saddy ke #define ROCKCHIP_SPI_RISR 0x0034 4064e36824Saddy ke #define ROCKCHIP_SPI_ICR 0x0038 4164e36824Saddy ke #define ROCKCHIP_SPI_DMACR 0x003c 4264e36824Saddy ke #define ROCKCHIP_SPI_DMATDLR 0x0040 4364e36824Saddy ke #define ROCKCHIP_SPI_DMARDLR 0x0044 4413a96935SJon Lin #define ROCKCHIP_SPI_VERSION 0x0048 4564e36824Saddy ke #define ROCKCHIP_SPI_TXDR 0x0400 4664e36824Saddy ke #define ROCKCHIP_SPI_RXDR 0x0800 4764e36824Saddy ke 4864e36824Saddy ke /* Bit fields in CTRLR0 */ 4964e36824Saddy ke #define CR0_DFS_OFFSET 0 5065498c6aSEmil Renner Berthing #define CR0_DFS_4BIT 0x0 5165498c6aSEmil Renner Berthing #define CR0_DFS_8BIT 0x1 5265498c6aSEmil Renner Berthing #define CR0_DFS_16BIT 0x2 5364e36824Saddy ke 5464e36824Saddy ke #define CR0_CFS_OFFSET 2 5564e36824Saddy ke 5664e36824Saddy ke #define CR0_SCPH_OFFSET 6 5764e36824Saddy ke 5864e36824Saddy ke #define CR0_SCPOL_OFFSET 7 5964e36824Saddy ke 6064e36824Saddy ke #define CR0_CSM_OFFSET 8 6164e36824Saddy ke #define CR0_CSM_KEEP 0x0 6264e36824Saddy ke /* ss_n be high for half sclk_out cycles */ 6364e36824Saddy ke #define CR0_CSM_HALF 0X1 6464e36824Saddy ke /* ss_n be high for one sclk_out cycle */ 6564e36824Saddy ke #define CR0_CSM_ONE 0x2 6664e36824Saddy ke 6764e36824Saddy ke /* ss_n to sclk_out delay */ 6864e36824Saddy ke #define CR0_SSD_OFFSET 10 6964e36824Saddy ke /* 7064e36824Saddy ke * The period between ss_n active and 7164e36824Saddy ke * sclk_out active is half sclk_out cycles 7264e36824Saddy ke */ 7364e36824Saddy ke #define CR0_SSD_HALF 0x0 7464e36824Saddy ke /* 7564e36824Saddy ke * The period between ss_n active and 7664e36824Saddy ke * sclk_out active is one sclk_out cycle 7764e36824Saddy ke */ 7864e36824Saddy ke #define CR0_SSD_ONE 0x1 7964e36824Saddy ke 8064e36824Saddy ke #define CR0_EM_OFFSET 11 8164e36824Saddy ke #define CR0_EM_LITTLE 0x0 8264e36824Saddy ke #define CR0_EM_BIG 0x1 8364e36824Saddy ke 8464e36824Saddy ke #define CR0_FBM_OFFSET 12 8564e36824Saddy ke #define CR0_FBM_MSB 0x0 8664e36824Saddy ke #define CR0_FBM_LSB 0x1 8764e36824Saddy ke 8864e36824Saddy ke #define CR0_BHT_OFFSET 13 8964e36824Saddy ke #define CR0_BHT_16BIT 0x0 9064e36824Saddy ke #define CR0_BHT_8BIT 0x1 9164e36824Saddy ke 9264e36824Saddy ke #define CR0_RSD_OFFSET 14 9374b7efa8SEmil Renner Berthing #define CR0_RSD_MAX 0x3 9464e36824Saddy ke 9564e36824Saddy ke #define CR0_FRF_OFFSET 16 9664e36824Saddy ke #define CR0_FRF_SPI 0x0 9764e36824Saddy ke #define CR0_FRF_SSP 0x1 9864e36824Saddy ke #define CR0_FRF_MICROWIRE 0x2 9964e36824Saddy ke 10064e36824Saddy ke #define CR0_XFM_OFFSET 18 10164e36824Saddy ke #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET) 10264e36824Saddy ke #define CR0_XFM_TR 0x0 10364e36824Saddy ke #define CR0_XFM_TO 0x1 10464e36824Saddy ke #define CR0_XFM_RO 0x2 10564e36824Saddy ke 10664e36824Saddy ke #define CR0_OPM_OFFSET 20 10764e36824Saddy ke #define CR0_OPM_MASTER 0x0 10864e36824Saddy ke #define CR0_OPM_SLAVE 0x1 10964e36824Saddy ke 110736b81e0SJon Lin #define CR0_SOI_OFFSET 23 111736b81e0SJon Lin 11264e36824Saddy ke #define CR0_MTM_OFFSET 0x21 11364e36824Saddy ke 11464e36824Saddy ke /* Bit fields in SER, 2bit */ 11564e36824Saddy ke #define SER_MASK 0x3 11664e36824Saddy ke 117420b82f8SEmil Renner Berthing /* Bit fields in BAUDR */ 118420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MIN 2 119420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MAX 65534 120420b82f8SEmil Renner Berthing 1212758bd09SJon Lin /* Bit fields in SR, 6bit */ 1222758bd09SJon Lin #define SR_MASK 0x3f 12364e36824Saddy ke #define SR_BUSY (1 << 0) 12464e36824Saddy ke #define SR_TF_FULL (1 << 1) 12564e36824Saddy ke #define SR_TF_EMPTY (1 << 2) 12664e36824Saddy ke #define SR_RF_EMPTY (1 << 3) 12764e36824Saddy ke #define SR_RF_FULL (1 << 4) 1282758bd09SJon Lin #define SR_SLAVE_TX_BUSY (1 << 5) 12964e36824Saddy ke 13064e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */ 13164e36824Saddy ke #define INT_MASK 0x1f 13264e36824Saddy ke #define INT_TF_EMPTY (1 << 0) 13364e36824Saddy ke #define INT_TF_OVERFLOW (1 << 1) 13464e36824Saddy ke #define INT_RF_UNDERFLOW (1 << 2) 13564e36824Saddy ke #define INT_RF_OVERFLOW (1 << 3) 13664e36824Saddy ke #define INT_RF_FULL (1 << 4) 13764e36824Saddy ke 13864e36824Saddy ke /* Bit fields in ICR, 4bit */ 13964e36824Saddy ke #define ICR_MASK 0x0f 14064e36824Saddy ke #define ICR_ALL (1 << 0) 14164e36824Saddy ke #define ICR_RF_UNDERFLOW (1 << 1) 14264e36824Saddy ke #define ICR_RF_OVERFLOW (1 << 2) 14364e36824Saddy ke #define ICR_TF_OVERFLOW (1 << 3) 14464e36824Saddy ke 14564e36824Saddy ke /* Bit fields in DMACR */ 14664e36824Saddy ke #define RF_DMA_EN (1 << 0) 14764e36824Saddy ke #define TF_DMA_EN (1 << 1) 14864e36824Saddy ke 149fab3e487SEmil Renner Berthing /* Driver state flags */ 150fab3e487SEmil Renner Berthing #define RXDMA (1 << 0) 151fab3e487SEmil Renner Berthing #define TXDMA (1 << 1) 15264e36824Saddy ke 153f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */ 154420b82f8SEmil Renner Berthing #define MAX_SCLK_OUT 50000000U 155f9cfd522SAddy Ke 1565185a81cSBrian Norris /* 1575185a81cSBrian Norris * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However, 1585185a81cSBrian Norris * the controller seems to hang when given 0x10000, so stick with this for now. 1595185a81cSBrian Norris */ 1605185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff 1615185a81cSBrian Norris 162b8d42371SJon Lin /* 2 for native cs, 2 for cs-gpio */ 163b8d42371SJon Lin #define ROCKCHIP_SPI_MAX_CS_NUM 4 16413a96935SJon Lin #define ROCKCHIP_SPI_VER2_TYPE1 0x05EC0002 16513a96935SJon Lin #define ROCKCHIP_SPI_VER2_TYPE2 0x00110002 166aa099382SJeffy Chen 167940f3bbfSAlexander Kochetkov #define ROCKCHIP_AUTOSUSPEND_TIMEOUT 2000 168940f3bbfSAlexander Kochetkov 16964e36824Saddy ke struct rockchip_spi { 17064e36824Saddy ke struct device *dev; 17164e36824Saddy ke 17264e36824Saddy ke struct clk *spiclk; 17364e36824Saddy ke struct clk *apb_pclk; 17464e36824Saddy ke 17564e36824Saddy ke void __iomem *regs; 176eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_rx; 177eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_tx; 178fab3e487SEmil Renner Berthing 17901b59ce5SEmil Renner Berthing const void *tx; 18001b59ce5SEmil Renner Berthing void *rx; 18101b59ce5SEmil Renner Berthing unsigned int tx_left; 18201b59ce5SEmil Renner Berthing unsigned int rx_left; 18301b59ce5SEmil Renner Berthing 184fab3e487SEmil Renner Berthing atomic_t state; 185fab3e487SEmil Renner Berthing 18664e36824Saddy ke /*depth of the FIFO buffer */ 18764e36824Saddy ke u32 fifo_len; 188420b82f8SEmil Renner Berthing /* frequency of spiclk */ 189420b82f8SEmil Renner Berthing u32 freq; 19064e36824Saddy ke 19164e36824Saddy ke u8 n_bytes; 19274b7efa8SEmil Renner Berthing u8 rsd; 19364e36824Saddy ke 194aa099382SJeffy Chen bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM]; 195d065f41aSChris Ruehl 196d065f41aSChris Ruehl bool slave_abort; 19764e36824Saddy ke }; 19864e36824Saddy ke 19930688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable) 20064e36824Saddy ke { 20130688e4eSEmil Renner Berthing writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR); 20264e36824Saddy ke } 20364e36824Saddy ke 2042758bd09SJon Lin static inline void wait_for_tx_idle(struct rockchip_spi *rs, bool slave_mode) 2052df08e78SAddy Ke { 2062df08e78SAddy Ke unsigned long timeout = jiffies + msecs_to_jiffies(5); 2072df08e78SAddy Ke 2082df08e78SAddy Ke do { 2092758bd09SJon Lin if (slave_mode) { 2102758bd09SJon Lin if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_SLAVE_TX_BUSY) && 2112758bd09SJon Lin !((readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))) 2122758bd09SJon Lin return; 2132758bd09SJon Lin } else { 2142df08e78SAddy Ke if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) 2152df08e78SAddy Ke return; 2162758bd09SJon Lin } 21764bc0110SDoug Anderson } while (!time_after(jiffies, timeout)); 2182df08e78SAddy Ke 2192df08e78SAddy Ke dev_warn(rs->dev, "spi controller is in busy state!\n"); 2202df08e78SAddy Ke } 2212df08e78SAddy Ke 22264e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs) 22364e36824Saddy ke { 22413a96935SJon Lin u32 ver; 22564e36824Saddy ke 22613a96935SJon Lin ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION); 22713a96935SJon Lin 22813a96935SJon Lin switch (ver) { 22913a96935SJon Lin case ROCKCHIP_SPI_VER2_TYPE1: 23013a96935SJon Lin case ROCKCHIP_SPI_VER2_TYPE2: 23113a96935SJon Lin return 64; 23213a96935SJon Lin default: 23313a96935SJon Lin return 32; 23464e36824Saddy ke } 23564e36824Saddy ke } 23664e36824Saddy ke 23764e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable) 23864e36824Saddy ke { 239d66571a2SChris Ruehl struct spi_controller *ctlr = spi->controller; 240d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 241736b81e0SJon Lin bool cs_asserted = spi->mode & SPI_CS_HIGH ? enable : !enable; 242b920cc31SHuibin Hong 243aa099382SJeffy Chen /* Return immediately for no-op */ 244aa099382SJeffy Chen if (cs_asserted == rs->cs_asserted[spi->chip_select]) 245aa099382SJeffy Chen return; 246aa099382SJeffy Chen 247aa099382SJeffy Chen if (cs_asserted) { 248aa099382SJeffy Chen /* Keep things powered as long as CS is asserted */ 249b920cc31SHuibin Hong pm_runtime_get_sync(rs->dev); 25064e36824Saddy ke 251b8d42371SJon Lin if (spi->cs_gpiod) 252b8d42371SJon Lin ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1); 253b8d42371SJon Lin else 254b8d42371SJon Lin ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select)); 255aa099382SJeffy Chen } else { 256b8d42371SJon Lin if (spi->cs_gpiod) 257b8d42371SJon Lin ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1); 258b8d42371SJon Lin else 259b8d42371SJon Lin ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select)); 26064e36824Saddy ke 261aa099382SJeffy Chen /* Drop reference from when we first asserted CS */ 262aa099382SJeffy Chen pm_runtime_put(rs->dev); 263aa099382SJeffy Chen } 26464e36824Saddy ke 265aa099382SJeffy Chen rs->cs_asserted[spi->chip_select] = cs_asserted; 26664e36824Saddy ke } 26764e36824Saddy ke 268d66571a2SChris Ruehl static void rockchip_spi_handle_err(struct spi_controller *ctlr, 26964e36824Saddy ke struct spi_message *msg) 27064e36824Saddy ke { 271d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 27264e36824Saddy ke 273ce386100SEmil Renner Berthing /* stop running spi transfer 274ce386100SEmil Renner Berthing * this also flushes both rx and tx fifos 2755dcc44edSAddy Ke */ 276ce386100SEmil Renner Berthing spi_enable_chip(rs, false); 277ce386100SEmil Renner Berthing 27801b59ce5SEmil Renner Berthing /* make sure all interrupts are masked */ 27901b59ce5SEmil Renner Berthing writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); 28001b59ce5SEmil Renner Berthing 281fab3e487SEmil Renner Berthing if (atomic_read(&rs->state) & TXDMA) 282d66571a2SChris Ruehl dmaengine_terminate_async(ctlr->dma_tx); 283fab3e487SEmil Renner Berthing 284ce386100SEmil Renner Berthing if (atomic_read(&rs->state) & RXDMA) 285d66571a2SChris Ruehl dmaengine_terminate_async(ctlr->dma_rx); 28664e36824Saddy ke } 28764e36824Saddy ke 28864e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs) 28964e36824Saddy ke { 29001b59ce5SEmil Renner Berthing u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); 29101b59ce5SEmil Renner Berthing u32 words = min(rs->tx_left, tx_free); 29264e36824Saddy ke 29301b59ce5SEmil Renner Berthing rs->tx_left -= words; 29401b59ce5SEmil Renner Berthing for (; words; words--) { 29501b59ce5SEmil Renner Berthing u32 txw; 29601b59ce5SEmil Renner Berthing 29764e36824Saddy ke if (rs->n_bytes == 1) 29801b59ce5SEmil Renner Berthing txw = *(u8 *)rs->tx; 29964e36824Saddy ke else 30001b59ce5SEmil Renner Berthing txw = *(u16 *)rs->tx; 30164e36824Saddy ke 30264e36824Saddy ke writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); 30364e36824Saddy ke rs->tx += rs->n_bytes; 30464e36824Saddy ke } 30564e36824Saddy ke } 30664e36824Saddy ke 30764e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs) 30864e36824Saddy ke { 30901b59ce5SEmil Renner Berthing u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); 3104294e4acSJon Lin u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0; 31164e36824Saddy ke 31201b59ce5SEmil Renner Berthing /* the hardware doesn't allow us to change fifo threshold 31301b59ce5SEmil Renner Berthing * level while spi is enabled, so instead make sure to leave 31401b59ce5SEmil Renner Berthing * enough words in the rx fifo to get the last interrupt 31501b59ce5SEmil Renner Berthing * exactly when all words have been received 31601b59ce5SEmil Renner Berthing */ 31701b59ce5SEmil Renner Berthing if (rx_left) { 31801b59ce5SEmil Renner Berthing u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1; 31901b59ce5SEmil Renner Berthing 32001b59ce5SEmil Renner Berthing if (rx_left < ftl) { 32101b59ce5SEmil Renner Berthing rx_left = ftl; 32201b59ce5SEmil Renner Berthing words = rs->rx_left - rx_left; 32301b59ce5SEmil Renner Berthing } 32401b59ce5SEmil Renner Berthing } 32501b59ce5SEmil Renner Berthing 32601b59ce5SEmil Renner Berthing rs->rx_left = rx_left; 32701b59ce5SEmil Renner Berthing for (; words; words--) { 32801b59ce5SEmil Renner Berthing u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); 32901b59ce5SEmil Renner Berthing 33001b59ce5SEmil Renner Berthing if (!rs->rx) 33101b59ce5SEmil Renner Berthing continue; 33201b59ce5SEmil Renner Berthing 33364e36824Saddy ke if (rs->n_bytes == 1) 33401b59ce5SEmil Renner Berthing *(u8 *)rs->rx = (u8)rxw; 33564e36824Saddy ke else 33601b59ce5SEmil Renner Berthing *(u16 *)rs->rx = (u16)rxw; 33764e36824Saddy ke rs->rx += rs->n_bytes; 3385dcc44edSAddy Ke } 33964e36824Saddy ke } 34064e36824Saddy ke 34101b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id) 34264e36824Saddy ke { 343d66571a2SChris Ruehl struct spi_controller *ctlr = dev_id; 344d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 34564e36824Saddy ke 34601b59ce5SEmil Renner Berthing if (rs->tx_left) 34701b59ce5SEmil Renner Berthing rockchip_spi_pio_writer(rs); 34801b59ce5SEmil Renner Berthing 34901b59ce5SEmil Renner Berthing rockchip_spi_pio_reader(rs); 35001b59ce5SEmil Renner Berthing if (!rs->rx_left) { 35101b59ce5SEmil Renner Berthing spi_enable_chip(rs, false); 35201b59ce5SEmil Renner Berthing writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); 353d66571a2SChris Ruehl spi_finalize_current_transfer(ctlr); 35401b59ce5SEmil Renner Berthing } 35501b59ce5SEmil Renner Berthing 35601b59ce5SEmil Renner Berthing return IRQ_HANDLED; 35701b59ce5SEmil Renner Berthing } 35801b59ce5SEmil Renner Berthing 35901b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs, 36001b59ce5SEmil Renner Berthing struct spi_transfer *xfer) 36101b59ce5SEmil Renner Berthing { 36201b59ce5SEmil Renner Berthing rs->tx = xfer->tx_buf; 36301b59ce5SEmil Renner Berthing rs->rx = xfer->rx_buf; 36401b59ce5SEmil Renner Berthing rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0; 36501b59ce5SEmil Renner Berthing rs->rx_left = xfer->len / rs->n_bytes; 36601b59ce5SEmil Renner Berthing 36701b59ce5SEmil Renner Berthing writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR); 36830688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 369a3c17402SEmil Renner Berthing 37001b59ce5SEmil Renner Berthing if (rs->tx_left) 37164e36824Saddy ke rockchip_spi_pio_writer(rs); 37264e36824Saddy ke 37301b59ce5SEmil Renner Berthing /* 1 means the transfer is in progress */ 37401b59ce5SEmil Renner Berthing return 1; 37564e36824Saddy ke } 37664e36824Saddy ke 37764e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data) 37864e36824Saddy ke { 379d66571a2SChris Ruehl struct spi_controller *ctlr = data; 380d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 381fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(RXDMA, &rs->state); 38264e36824Saddy ke 383d065f41aSChris Ruehl if (state & TXDMA && !rs->slave_abort) 384fab3e487SEmil Renner Berthing return; 38564e36824Saddy ke 38630688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 387d66571a2SChris Ruehl spi_finalize_current_transfer(ctlr); 388c28be31bSAddy Ke } 38964e36824Saddy ke 39064e36824Saddy ke static void rockchip_spi_dma_txcb(void *data) 39164e36824Saddy ke { 392d66571a2SChris Ruehl struct spi_controller *ctlr = data; 393d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 394fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(TXDMA, &rs->state); 395fab3e487SEmil Renner Berthing 396d065f41aSChris Ruehl if (state & RXDMA && !rs->slave_abort) 397fab3e487SEmil Renner Berthing return; 39864e36824Saddy ke 3992df08e78SAddy Ke /* Wait until the FIFO data completely. */ 4002758bd09SJon Lin wait_for_tx_idle(rs, ctlr->slave); 4012df08e78SAddy Ke 40230688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 403d66571a2SChris Ruehl spi_finalize_current_transfer(ctlr); 4042c2bc748SAddy Ke } 40564e36824Saddy ke 4064d9ca632SJon Lin static u32 rockchip_spi_calc_burst_size(u32 data_len) 4074d9ca632SJon Lin { 4084d9ca632SJon Lin u32 i; 4094d9ca632SJon Lin 4104d9ca632SJon Lin /* burst size: 1, 2, 4, 8 */ 4114d9ca632SJon Lin for (i = 1; i < 8; i <<= 1) { 4124d9ca632SJon Lin if (data_len & i) 4134d9ca632SJon Lin break; 4144d9ca632SJon Lin } 4154d9ca632SJon Lin 4164d9ca632SJon Lin return i; 4174d9ca632SJon Lin } 4184d9ca632SJon Lin 419fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs, 420d66571a2SChris Ruehl struct spi_controller *ctlr, struct spi_transfer *xfer) 42164e36824Saddy ke { 42264e36824Saddy ke struct dma_async_tx_descriptor *rxdesc, *txdesc; 42364e36824Saddy ke 424fab3e487SEmil Renner Berthing atomic_set(&rs->state, 0); 42564e36824Saddy ke 42697cf5669SArnd Bergmann rxdesc = NULL; 427fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) { 42831bcb57bSEmil Renner Berthing struct dma_slave_config rxconf = { 42931bcb57bSEmil Renner Berthing .direction = DMA_DEV_TO_MEM, 430eee06a9eSEmil Renner Berthing .src_addr = rs->dma_addr_rx, 43131bcb57bSEmil Renner Berthing .src_addr_width = rs->n_bytes, 4324d9ca632SJon Lin .src_maxburst = rockchip_spi_calc_burst_size(xfer->len / 4334d9ca632SJon Lin rs->n_bytes), 43431bcb57bSEmil Renner Berthing }; 43531bcb57bSEmil Renner Berthing 436d66571a2SChris Ruehl dmaengine_slave_config(ctlr->dma_rx, &rxconf); 43764e36824Saddy ke 4385dcc44edSAddy Ke rxdesc = dmaengine_prep_slave_sg( 439d66571a2SChris Ruehl ctlr->dma_rx, 440fc1ad8eeSEmil Renner Berthing xfer->rx_sg.sgl, xfer->rx_sg.nents, 441d9071b7eSEmil Renner Berthing DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); 442ea984911SShawn Lin if (!rxdesc) 443ea984911SShawn Lin return -EINVAL; 44464e36824Saddy ke 44564e36824Saddy ke rxdesc->callback = rockchip_spi_dma_rxcb; 446d66571a2SChris Ruehl rxdesc->callback_param = ctlr; 44764e36824Saddy ke } 44864e36824Saddy ke 44997cf5669SArnd Bergmann txdesc = NULL; 450fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) { 45131bcb57bSEmil Renner Berthing struct dma_slave_config txconf = { 45231bcb57bSEmil Renner Berthing .direction = DMA_MEM_TO_DEV, 453eee06a9eSEmil Renner Berthing .dst_addr = rs->dma_addr_tx, 45431bcb57bSEmil Renner Berthing .dst_addr_width = rs->n_bytes, 45547300728SEmil Renner Berthing .dst_maxburst = rs->fifo_len / 4, 45631bcb57bSEmil Renner Berthing }; 45731bcb57bSEmil Renner Berthing 458d66571a2SChris Ruehl dmaengine_slave_config(ctlr->dma_tx, &txconf); 45964e36824Saddy ke 4605dcc44edSAddy Ke txdesc = dmaengine_prep_slave_sg( 461d66571a2SChris Ruehl ctlr->dma_tx, 462fc1ad8eeSEmil Renner Berthing xfer->tx_sg.sgl, xfer->tx_sg.nents, 463d9071b7eSEmil Renner Berthing DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); 464ea984911SShawn Lin if (!txdesc) { 465ea984911SShawn Lin if (rxdesc) 466d66571a2SChris Ruehl dmaengine_terminate_sync(ctlr->dma_rx); 467ea984911SShawn Lin return -EINVAL; 468ea984911SShawn Lin } 46964e36824Saddy ke 47064e36824Saddy ke txdesc->callback = rockchip_spi_dma_txcb; 471d66571a2SChris Ruehl txdesc->callback_param = ctlr; 47264e36824Saddy ke } 47364e36824Saddy ke 47464e36824Saddy ke /* rx must be started before tx due to spi instinct */ 47597cf5669SArnd Bergmann if (rxdesc) { 476fab3e487SEmil Renner Berthing atomic_or(RXDMA, &rs->state); 47764e36824Saddy ke dmaengine_submit(rxdesc); 478d66571a2SChris Ruehl dma_async_issue_pending(ctlr->dma_rx); 47964e36824Saddy ke } 48064e36824Saddy ke 48130688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 482a3c17402SEmil Renner Berthing 48397cf5669SArnd Bergmann if (txdesc) { 484fab3e487SEmil Renner Berthing atomic_or(TXDMA, &rs->state); 48564e36824Saddy ke dmaengine_submit(txdesc); 486d66571a2SChris Ruehl dma_async_issue_pending(ctlr->dma_tx); 48764e36824Saddy ke } 488ea984911SShawn Lin 489a3c17402SEmil Renner Berthing /* 1 means the transfer is in progress */ 490a3c17402SEmil Renner Berthing return 1; 49164e36824Saddy ke } 49264e36824Saddy ke 493e5098952SArnd Bergmann static int rockchip_spi_config(struct rockchip_spi *rs, 494eff0275eSEmil Renner Berthing struct spi_device *spi, struct spi_transfer *xfer, 495d065f41aSChris Ruehl bool use_dma, bool slave_mode) 49664e36824Saddy ke { 4972410d6a3SEmil Renner Berthing u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET 4982410d6a3SEmil Renner Berthing | CR0_BHT_8BIT << CR0_BHT_OFFSET 4992410d6a3SEmil Renner Berthing | CR0_SSD_ONE << CR0_SSD_OFFSET 5002410d6a3SEmil Renner Berthing | CR0_EM_BIG << CR0_EM_OFFSET; 50165498c6aSEmil Renner Berthing u32 cr1; 50265498c6aSEmil Renner Berthing u32 dmacr = 0; 50364e36824Saddy ke 504d065f41aSChris Ruehl if (slave_mode) 505d065f41aSChris Ruehl cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET; 506d065f41aSChris Ruehl rs->slave_abort = false; 507d065f41aSChris Ruehl 50874b7efa8SEmil Renner Berthing cr0 |= rs->rsd << CR0_RSD_OFFSET; 509fc1ad8eeSEmil Renner Berthing cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET; 51004290192SEmil Renner Berthing if (spi->mode & SPI_LSB_FIRST) 51104290192SEmil Renner Berthing cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET; 512736b81e0SJon Lin if (spi->mode & SPI_CS_HIGH) 513736b81e0SJon Lin cr0 |= BIT(spi->chip_select) << CR0_SOI_OFFSET; 514fc1ad8eeSEmil Renner Berthing 515fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf && xfer->tx_buf) 516fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET; 517fc1ad8eeSEmil Renner Berthing else if (xfer->rx_buf) 518fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET; 51901b59ce5SEmil Renner Berthing else if (use_dma) 520fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET; 52164e36824Saddy ke 52265498c6aSEmil Renner Berthing switch (xfer->bits_per_word) { 52365498c6aSEmil Renner Berthing case 4: 52465498c6aSEmil Renner Berthing cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET; 52565498c6aSEmil Renner Berthing cr1 = xfer->len - 1; 52665498c6aSEmil Renner Berthing break; 52765498c6aSEmil Renner Berthing case 8: 52865498c6aSEmil Renner Berthing cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET; 52965498c6aSEmil Renner Berthing cr1 = xfer->len - 1; 53065498c6aSEmil Renner Berthing break; 53165498c6aSEmil Renner Berthing case 16: 53265498c6aSEmil Renner Berthing cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET; 53365498c6aSEmil Renner Berthing cr1 = xfer->len / 2 - 1; 53465498c6aSEmil Renner Berthing break; 53565498c6aSEmil Renner Berthing default: 53665498c6aSEmil Renner Berthing /* we only whitelist 4, 8 and 16 bit words in 537d66571a2SChris Ruehl * ctlr->bits_per_word_mask, so this shouldn't 53865498c6aSEmil Renner Berthing * happen 53965498c6aSEmil Renner Berthing */ 540e5098952SArnd Bergmann dev_err(rs->dev, "unknown bits per word: %d\n", 541e5098952SArnd Bergmann xfer->bits_per_word); 542e5098952SArnd Bergmann return -EINVAL; 54365498c6aSEmil Renner Berthing } 54465498c6aSEmil Renner Berthing 545eff0275eSEmil Renner Berthing if (use_dma) { 546fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) 54764e36824Saddy ke dmacr |= TF_DMA_EN; 548fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) 54964e36824Saddy ke dmacr |= RF_DMA_EN; 55064e36824Saddy ke } 55164e36824Saddy ke 55264e36824Saddy ke writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); 55365498c6aSEmil Renner Berthing writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1); 55404b37d2dSHuibin Hong 55501b59ce5SEmil Renner Berthing /* unfortunately setting the fifo threshold level to generate an 55601b59ce5SEmil Renner Berthing * interrupt exactly when the fifo is full doesn't seem to work, 55701b59ce5SEmil Renner Berthing * so we need the strict inequality here 55801b59ce5SEmil Renner Berthing */ 5594a47fcdbSJon Lin if ((xfer->len / rs->n_bytes) < rs->fifo_len) 5604a47fcdbSJon Lin writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 56101b59ce5SEmil Renner Berthing else 56264e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 56364e36824Saddy ke 5642758bd09SJon Lin writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR); 5654d9ca632SJon Lin writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1, 5664d9ca632SJon Lin rs->regs + ROCKCHIP_SPI_DMARDLR); 56764e36824Saddy ke writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); 56864e36824Saddy ke 569420b82f8SEmil Renner Berthing /* the hardware only supports an even clock divisor, so 570420b82f8SEmil Renner Berthing * round divisor = spiclk / speed up to nearest even number 571420b82f8SEmil Renner Berthing * so that the resulting speed is <= the requested speed 572420b82f8SEmil Renner Berthing */ 573420b82f8SEmil Renner Berthing writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz), 574420b82f8SEmil Renner Berthing rs->regs + ROCKCHIP_SPI_BAUDR); 575e5098952SArnd Bergmann 576e5098952SArnd Bergmann return 0; 57764e36824Saddy ke } 57864e36824Saddy ke 5795185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi) 5805185a81cSBrian Norris { 5815185a81cSBrian Norris return ROCKCHIP_SPI_MAX_TRANLEN; 5825185a81cSBrian Norris } 5835185a81cSBrian Norris 584d065f41aSChris Ruehl static int rockchip_spi_slave_abort(struct spi_controller *ctlr) 585d065f41aSChris Ruehl { 586d065f41aSChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 587d065f41aSChris Ruehl 588d065f41aSChris Ruehl rs->slave_abort = true; 5896bd2c867SVincent Pelletier spi_finalize_current_transfer(ctlr); 590d065f41aSChris Ruehl 591d065f41aSChris Ruehl return 0; 592d065f41aSChris Ruehl } 593d065f41aSChris Ruehl 5945dcc44edSAddy Ke static int rockchip_spi_transfer_one( 595d66571a2SChris Ruehl struct spi_controller *ctlr, 59664e36824Saddy ke struct spi_device *spi, 59764e36824Saddy ke struct spi_transfer *xfer) 59864e36824Saddy ke { 599d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 600e5098952SArnd Bergmann int ret; 601eff0275eSEmil Renner Berthing bool use_dma; 60264e36824Saddy ke 6035457773eSTobias Schramm /* Zero length transfers won't trigger an interrupt on completion */ 6045457773eSTobias Schramm if (!xfer->len) { 6055457773eSTobias Schramm spi_finalize_current_transfer(ctlr); 6065457773eSTobias Schramm return 1; 6075457773eSTobias Schramm } 6085457773eSTobias Schramm 60962946172SDoug Anderson WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && 61062946172SDoug Anderson (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); 61164e36824Saddy ke 61264e36824Saddy ke if (!xfer->tx_buf && !xfer->rx_buf) { 61364e36824Saddy ke dev_err(rs->dev, "No buffer for transfer\n"); 61464e36824Saddy ke return -EINVAL; 61564e36824Saddy ke } 61664e36824Saddy ke 6175185a81cSBrian Norris if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) { 6185185a81cSBrian Norris dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len); 6195185a81cSBrian Norris return -EINVAL; 6205185a81cSBrian Norris } 6215185a81cSBrian Norris 62265498c6aSEmil Renner Berthing rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2; 62364e36824Saddy ke 624d66571a2SChris Ruehl use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false; 62564e36824Saddy ke 626e5098952SArnd Bergmann ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave); 627e5098952SArnd Bergmann if (ret) 628e5098952SArnd Bergmann return ret; 62964e36824Saddy ke 630eff0275eSEmil Renner Berthing if (use_dma) 631d66571a2SChris Ruehl return rockchip_spi_prepare_dma(rs, ctlr, xfer); 63264e36824Saddy ke 63301b59ce5SEmil Renner Berthing return rockchip_spi_prepare_irq(rs, xfer); 63464e36824Saddy ke } 63564e36824Saddy ke 636d66571a2SChris Ruehl static bool rockchip_spi_can_dma(struct spi_controller *ctlr, 63764e36824Saddy ke struct spi_device *spi, 63864e36824Saddy ke struct spi_transfer *xfer) 63964e36824Saddy ke { 640d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 64101b59ce5SEmil Renner Berthing unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2; 64264e36824Saddy ke 64301b59ce5SEmil Renner Berthing /* if the numbor of spi words to transfer is less than the fifo 64401b59ce5SEmil Renner Berthing * length we can just fill the fifo and wait for a single irq, 64501b59ce5SEmil Renner Berthing * so don't bother setting up dma 64601b59ce5SEmil Renner Berthing */ 64701b59ce5SEmil Renner Berthing return xfer->len / bytes_per_word >= rs->fifo_len; 64864e36824Saddy ke } 64964e36824Saddy ke 65064e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev) 65164e36824Saddy ke { 65243de979dSJeffy Chen int ret; 65364e36824Saddy ke struct rockchip_spi *rs; 654d66571a2SChris Ruehl struct spi_controller *ctlr; 65564e36824Saddy ke struct resource *mem; 656d065f41aSChris Ruehl struct device_node *np = pdev->dev.of_node; 657*9382df0aSJon Lin u32 rsd_nsecs, num_cs; 658d065f41aSChris Ruehl bool slave_mode; 65964e36824Saddy ke 660d065f41aSChris Ruehl slave_mode = of_property_read_bool(np, "spi-slave"); 661d065f41aSChris Ruehl 662d065f41aSChris Ruehl if (slave_mode) 663d065f41aSChris Ruehl ctlr = spi_alloc_slave(&pdev->dev, 664d065f41aSChris Ruehl sizeof(struct rockchip_spi)); 665d065f41aSChris Ruehl else 666d065f41aSChris Ruehl ctlr = spi_alloc_master(&pdev->dev, 667d065f41aSChris Ruehl sizeof(struct rockchip_spi)); 668d065f41aSChris Ruehl 669d66571a2SChris Ruehl if (!ctlr) 67064e36824Saddy ke return -ENOMEM; 6715dcc44edSAddy Ke 672d66571a2SChris Ruehl platform_set_drvdata(pdev, ctlr); 67364e36824Saddy ke 674d66571a2SChris Ruehl rs = spi_controller_get_devdata(ctlr); 675d065f41aSChris Ruehl ctlr->slave = slave_mode; 67664e36824Saddy ke 67764e36824Saddy ke /* Get basic io resource and map it */ 67864e36824Saddy ke mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 67964e36824Saddy ke rs->regs = devm_ioremap_resource(&pdev->dev, mem); 68064e36824Saddy ke if (IS_ERR(rs->regs)) { 68164e36824Saddy ke ret = PTR_ERR(rs->regs); 682d66571a2SChris Ruehl goto err_put_ctlr; 68364e36824Saddy ke } 68464e36824Saddy ke 68564e36824Saddy ke rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 68664e36824Saddy ke if (IS_ERR(rs->apb_pclk)) { 68764e36824Saddy ke dev_err(&pdev->dev, "Failed to get apb_pclk\n"); 68864e36824Saddy ke ret = PTR_ERR(rs->apb_pclk); 689d66571a2SChris Ruehl goto err_put_ctlr; 69064e36824Saddy ke } 69164e36824Saddy ke 69264e36824Saddy ke rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); 69364e36824Saddy ke if (IS_ERR(rs->spiclk)) { 69464e36824Saddy ke dev_err(&pdev->dev, "Failed to get spi_pclk\n"); 69564e36824Saddy ke ret = PTR_ERR(rs->spiclk); 696d66571a2SChris Ruehl goto err_put_ctlr; 69764e36824Saddy ke } 69864e36824Saddy ke 69964e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 70043de979dSJeffy Chen if (ret < 0) { 70164e36824Saddy ke dev_err(&pdev->dev, "Failed to enable apb_pclk\n"); 702d66571a2SChris Ruehl goto err_put_ctlr; 70364e36824Saddy ke } 70464e36824Saddy ke 70564e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 70643de979dSJeffy Chen if (ret < 0) { 70764e36824Saddy ke dev_err(&pdev->dev, "Failed to enable spi_clk\n"); 708c351587eSJeffy Chen goto err_disable_apbclk; 70964e36824Saddy ke } 71064e36824Saddy ke 71130688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 71264e36824Saddy ke 71301b59ce5SEmil Renner Berthing ret = platform_get_irq(pdev, 0); 71401b59ce5SEmil Renner Berthing if (ret < 0) 71501b59ce5SEmil Renner Berthing goto err_disable_spiclk; 71601b59ce5SEmil Renner Berthing 71701b59ce5SEmil Renner Berthing ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL, 718d66571a2SChris Ruehl IRQF_ONESHOT, dev_name(&pdev->dev), ctlr); 71901b59ce5SEmil Renner Berthing if (ret) 72001b59ce5SEmil Renner Berthing goto err_disable_spiclk; 72101b59ce5SEmil Renner Berthing 72264e36824Saddy ke rs->dev = &pdev->dev; 723420b82f8SEmil Renner Berthing rs->freq = clk_get_rate(rs->spiclk); 72464e36824Saddy ke 72576b17e6eSJulius Werner if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", 72674b7efa8SEmil Renner Berthing &rsd_nsecs)) { 72774b7efa8SEmil Renner Berthing /* rx sample delay is expressed in parent clock cycles (max 3) */ 72874b7efa8SEmil Renner Berthing u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8), 72974b7efa8SEmil Renner Berthing 1000000000 >> 8); 73074b7efa8SEmil Renner Berthing if (!rsd) { 73174b7efa8SEmil Renner Berthing dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n", 73274b7efa8SEmil Renner Berthing rs->freq, rsd_nsecs); 73374b7efa8SEmil Renner Berthing } else if (rsd > CR0_RSD_MAX) { 73474b7efa8SEmil Renner Berthing rsd = CR0_RSD_MAX; 73574b7efa8SEmil Renner Berthing dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n", 73674b7efa8SEmil Renner Berthing rs->freq, rsd_nsecs, 73774b7efa8SEmil Renner Berthing CR0_RSD_MAX * 1000000000U / rs->freq); 73874b7efa8SEmil Renner Berthing } 73974b7efa8SEmil Renner Berthing rs->rsd = rsd; 74074b7efa8SEmil Renner Berthing } 74176b17e6eSJulius Werner 74264e36824Saddy ke rs->fifo_len = get_fifo_len(rs); 74364e36824Saddy ke if (!rs->fifo_len) { 74464e36824Saddy ke dev_err(&pdev->dev, "Failed to get fifo length\n"); 745db7e8d90SWei Yongjun ret = -EINVAL; 746c351587eSJeffy Chen goto err_disable_spiclk; 74764e36824Saddy ke } 74864e36824Saddy ke 749940f3bbfSAlexander Kochetkov pm_runtime_set_autosuspend_delay(&pdev->dev, ROCKCHIP_AUTOSUSPEND_TIMEOUT); 750940f3bbfSAlexander Kochetkov pm_runtime_use_autosuspend(&pdev->dev); 75164e36824Saddy ke pm_runtime_set_active(&pdev->dev); 75264e36824Saddy ke pm_runtime_enable(&pdev->dev); 75364e36824Saddy ke 754d66571a2SChris Ruehl ctlr->auto_runtime_pm = true; 755d66571a2SChris Ruehl ctlr->bus_num = pdev->id; 756d66571a2SChris Ruehl ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST; 757d065f41aSChris Ruehl if (slave_mode) { 758d065f41aSChris Ruehl ctlr->mode_bits |= SPI_NO_CS; 759d065f41aSChris Ruehl ctlr->slave_abort = rockchip_spi_slave_abort; 760d065f41aSChris Ruehl } else { 761d065f41aSChris Ruehl ctlr->flags = SPI_MASTER_GPIO_SS; 762eb1262e3SChris Ruehl ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM; 763eb1262e3SChris Ruehl /* 764eb1262e3SChris Ruehl * rk spi0 has two native cs, spi1..5 one cs only 765eb1262e3SChris Ruehl * if num-cs is missing in the dts, default to 1 766eb1262e3SChris Ruehl */ 767*9382df0aSJon Lin if (of_property_read_u32(np, "num-cs", &num_cs)) 768*9382df0aSJon Lin num_cs = 1; 769*9382df0aSJon Lin ctlr->num_chipselect = num_cs; 770eb1262e3SChris Ruehl ctlr->use_gpio_descriptors = true; 771d065f41aSChris Ruehl } 772d66571a2SChris Ruehl ctlr->dev.of_node = pdev->dev.of_node; 773d66571a2SChris Ruehl ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4); 774d66571a2SChris Ruehl ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX; 775d66571a2SChris Ruehl ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT); 77664e36824Saddy ke 777d66571a2SChris Ruehl ctlr->set_cs = rockchip_spi_set_cs; 778d66571a2SChris Ruehl ctlr->transfer_one = rockchip_spi_transfer_one; 779d66571a2SChris Ruehl ctlr->max_transfer_size = rockchip_spi_max_transfer_size; 780d66571a2SChris Ruehl ctlr->handle_err = rockchip_spi_handle_err; 78164e36824Saddy ke 782d66571a2SChris Ruehl ctlr->dma_tx = dma_request_chan(rs->dev, "tx"); 783d66571a2SChris Ruehl if (IS_ERR(ctlr->dma_tx)) { 78461cadcf4SShawn Lin /* Check tx to see if we need defer probing driver */ 785d66571a2SChris Ruehl if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) { 78661cadcf4SShawn Lin ret = -EPROBE_DEFER; 787c351587eSJeffy Chen goto err_disable_pm_runtime; 78861cadcf4SShawn Lin } 78964e36824Saddy ke dev_warn(rs->dev, "Failed to request TX DMA channel\n"); 790d66571a2SChris Ruehl ctlr->dma_tx = NULL; 79164e36824Saddy ke } 792e4c0e06fSShawn Lin 793d66571a2SChris Ruehl ctlr->dma_rx = dma_request_chan(rs->dev, "rx"); 794d66571a2SChris Ruehl if (IS_ERR(ctlr->dma_rx)) { 795d66571a2SChris Ruehl if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) { 796e4c0e06fSShawn Lin ret = -EPROBE_DEFER; 7975de7ed0cSDan Carpenter goto err_free_dma_tx; 798e4c0e06fSShawn Lin } 79964e36824Saddy ke dev_warn(rs->dev, "Failed to request RX DMA channel\n"); 800d66571a2SChris Ruehl ctlr->dma_rx = NULL; 80164e36824Saddy ke } 80264e36824Saddy ke 803d66571a2SChris Ruehl if (ctlr->dma_tx && ctlr->dma_rx) { 804eee06a9eSEmil Renner Berthing rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR; 805eee06a9eSEmil Renner Berthing rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR; 806d66571a2SChris Ruehl ctlr->can_dma = rockchip_spi_can_dma; 80764e36824Saddy ke } 80864e36824Saddy ke 809736b81e0SJon Lin switch (readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION)) { 810736b81e0SJon Lin case ROCKCHIP_SPI_VER2_TYPE2: 811736b81e0SJon Lin ctlr->mode_bits |= SPI_CS_HIGH; 812736b81e0SJon Lin break; 813736b81e0SJon Lin default: 814736b81e0SJon Lin break; 815736b81e0SJon Lin } 816736b81e0SJon Lin 817d66571a2SChris Ruehl ret = devm_spi_register_controller(&pdev->dev, ctlr); 81843de979dSJeffy Chen if (ret < 0) { 819d66571a2SChris Ruehl dev_err(&pdev->dev, "Failed to register controller\n"); 820c351587eSJeffy Chen goto err_free_dma_rx; 82164e36824Saddy ke } 82264e36824Saddy ke 82364e36824Saddy ke return 0; 82464e36824Saddy ke 825c351587eSJeffy Chen err_free_dma_rx: 826d66571a2SChris Ruehl if (ctlr->dma_rx) 827d66571a2SChris Ruehl dma_release_channel(ctlr->dma_rx); 8285de7ed0cSDan Carpenter err_free_dma_tx: 829d66571a2SChris Ruehl if (ctlr->dma_tx) 830d66571a2SChris Ruehl dma_release_channel(ctlr->dma_tx); 831c351587eSJeffy Chen err_disable_pm_runtime: 832c351587eSJeffy Chen pm_runtime_disable(&pdev->dev); 833c351587eSJeffy Chen err_disable_spiclk: 83464e36824Saddy ke clk_disable_unprepare(rs->spiclk); 835c351587eSJeffy Chen err_disable_apbclk: 83664e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 837d66571a2SChris Ruehl err_put_ctlr: 838d66571a2SChris Ruehl spi_controller_put(ctlr); 83964e36824Saddy ke 84064e36824Saddy ke return ret; 84164e36824Saddy ke } 84264e36824Saddy ke 84364e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev) 84464e36824Saddy ke { 845d66571a2SChris Ruehl struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev)); 846d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 84764e36824Saddy ke 8486a06e895SJeffy Chen pm_runtime_get_sync(&pdev->dev); 84964e36824Saddy ke 85064e36824Saddy ke clk_disable_unprepare(rs->spiclk); 85164e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 85264e36824Saddy ke 8536a06e895SJeffy Chen pm_runtime_put_noidle(&pdev->dev); 8546a06e895SJeffy Chen pm_runtime_disable(&pdev->dev); 8556a06e895SJeffy Chen pm_runtime_set_suspended(&pdev->dev); 8566a06e895SJeffy Chen 857d66571a2SChris Ruehl if (ctlr->dma_tx) 858d66571a2SChris Ruehl dma_release_channel(ctlr->dma_tx); 859d66571a2SChris Ruehl if (ctlr->dma_rx) 860d66571a2SChris Ruehl dma_release_channel(ctlr->dma_rx); 86164e36824Saddy ke 862d66571a2SChris Ruehl spi_controller_put(ctlr); 863844c9f47SShawn Lin 86464e36824Saddy ke return 0; 86564e36824Saddy ke } 86664e36824Saddy ke 86764e36824Saddy ke #ifdef CONFIG_PM_SLEEP 86864e36824Saddy ke static int rockchip_spi_suspend(struct device *dev) 86964e36824Saddy ke { 87043de979dSJeffy Chen int ret; 871d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 87264e36824Saddy ke 873d66571a2SChris Ruehl ret = spi_controller_suspend(ctlr); 87443de979dSJeffy Chen if (ret < 0) 87564e36824Saddy ke return ret; 87664e36824Saddy ke 877d38c4ae1SJeffy Chen ret = pm_runtime_force_suspend(dev); 878d38c4ae1SJeffy Chen if (ret < 0) 879d38c4ae1SJeffy Chen return ret; 88064e36824Saddy ke 88123e291c2SBrian Norris pinctrl_pm_select_sleep_state(dev); 88223e291c2SBrian Norris 88343de979dSJeffy Chen return 0; 88464e36824Saddy ke } 88564e36824Saddy ke 88664e36824Saddy ke static int rockchip_spi_resume(struct device *dev) 88764e36824Saddy ke { 88843de979dSJeffy Chen int ret; 889d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 890d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 89164e36824Saddy ke 89223e291c2SBrian Norris pinctrl_pm_select_default_state(dev); 89323e291c2SBrian Norris 894d38c4ae1SJeffy Chen ret = pm_runtime_force_resume(dev); 89564e36824Saddy ke if (ret < 0) 89664e36824Saddy ke return ret; 89764e36824Saddy ke 898d66571a2SChris Ruehl ret = spi_controller_resume(ctlr); 89964e36824Saddy ke if (ret < 0) { 90064e36824Saddy ke clk_disable_unprepare(rs->spiclk); 90164e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 90264e36824Saddy ke } 90364e36824Saddy ke 90443de979dSJeffy Chen return 0; 90564e36824Saddy ke } 90664e36824Saddy ke #endif /* CONFIG_PM_SLEEP */ 90764e36824Saddy ke 908ec833050SRafael J. Wysocki #ifdef CONFIG_PM 90964e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev) 91064e36824Saddy ke { 911d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 912d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 91364e36824Saddy ke 91464e36824Saddy ke clk_disable_unprepare(rs->spiclk); 91564e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 91664e36824Saddy ke 91764e36824Saddy ke return 0; 91864e36824Saddy ke } 91964e36824Saddy ke 92064e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev) 92164e36824Saddy ke { 92264e36824Saddy ke int ret; 923d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 924d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 92564e36824Saddy ke 92664e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 92743de979dSJeffy Chen if (ret < 0) 92864e36824Saddy ke return ret; 92964e36824Saddy ke 93064e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 93143de979dSJeffy Chen if (ret < 0) 93264e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 93364e36824Saddy ke 93443de979dSJeffy Chen return 0; 93564e36824Saddy ke } 936ec833050SRafael J. Wysocki #endif /* CONFIG_PM */ 93764e36824Saddy ke 93864e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = { 93964e36824Saddy ke SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume) 94064e36824Saddy ke SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend, 94164e36824Saddy ke rockchip_spi_runtime_resume, NULL) 94264e36824Saddy ke }; 94364e36824Saddy ke 94464e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = { 945c6486eadSJohan Jonker { .compatible = "rockchip,px30-spi", }, 946aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3036-spi", }, 94764e36824Saddy ke { .compatible = "rockchip,rk3066-spi", }, 948b839b785SAddy Ke { .compatible = "rockchip,rk3188-spi", }, 949aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3228-spi", }, 950b839b785SAddy Ke { .compatible = "rockchip,rk3288-spi", }, 951c6486eadSJohan Jonker { .compatible = "rockchip,rk3308-spi", }, 952c6486eadSJohan Jonker { .compatible = "rockchip,rk3328-spi", }, 953aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3368-spi", }, 9549b7a5622SXu Jianqun { .compatible = "rockchip,rk3399-spi", }, 955c6486eadSJohan Jonker { .compatible = "rockchip,rv1108-spi", }, 9560f4f58b8SJon Lin { .compatible = "rockchip,rv1126-spi", }, 95764e36824Saddy ke { }, 95864e36824Saddy ke }; 95964e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); 96064e36824Saddy ke 96164e36824Saddy ke static struct platform_driver rockchip_spi_driver = { 96264e36824Saddy ke .driver = { 96364e36824Saddy ke .name = DRIVER_NAME, 96464e36824Saddy ke .pm = &rockchip_spi_pm, 96564e36824Saddy ke .of_match_table = of_match_ptr(rockchip_spi_dt_match), 96664e36824Saddy ke }, 96764e36824Saddy ke .probe = rockchip_spi_probe, 96864e36824Saddy ke .remove = rockchip_spi_remove, 96964e36824Saddy ke }; 97064e36824Saddy ke 97164e36824Saddy ke module_platform_driver(rockchip_spi_driver); 97264e36824Saddy ke 9735dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); 97464e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver"); 97564e36824Saddy ke MODULE_LICENSE("GPL v2"); 976