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 11064e36824Saddy ke #define CR0_MTM_OFFSET 0x21 11164e36824Saddy ke 11264e36824Saddy ke /* Bit fields in SER, 2bit */ 11364e36824Saddy ke #define SER_MASK 0x3 11464e36824Saddy ke 115420b82f8SEmil Renner Berthing /* Bit fields in BAUDR */ 116420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MIN 2 117420b82f8SEmil Renner Berthing #define BAUDR_SCKDV_MAX 65534 118420b82f8SEmil Renner Berthing 11964e36824Saddy ke /* Bit fields in SR, 5bit */ 12064e36824Saddy ke #define SR_MASK 0x1f 12164e36824Saddy ke #define SR_BUSY (1 << 0) 12264e36824Saddy ke #define SR_TF_FULL (1 << 1) 12364e36824Saddy ke #define SR_TF_EMPTY (1 << 2) 12464e36824Saddy ke #define SR_RF_EMPTY (1 << 3) 12564e36824Saddy ke #define SR_RF_FULL (1 << 4) 12664e36824Saddy ke 12764e36824Saddy ke /* Bit fields in ISR, IMR, ISR, RISR, 5bit */ 12864e36824Saddy ke #define INT_MASK 0x1f 12964e36824Saddy ke #define INT_TF_EMPTY (1 << 0) 13064e36824Saddy ke #define INT_TF_OVERFLOW (1 << 1) 13164e36824Saddy ke #define INT_RF_UNDERFLOW (1 << 2) 13264e36824Saddy ke #define INT_RF_OVERFLOW (1 << 3) 13364e36824Saddy ke #define INT_RF_FULL (1 << 4) 13464e36824Saddy ke 13564e36824Saddy ke /* Bit fields in ICR, 4bit */ 13664e36824Saddy ke #define ICR_MASK 0x0f 13764e36824Saddy ke #define ICR_ALL (1 << 0) 13864e36824Saddy ke #define ICR_RF_UNDERFLOW (1 << 1) 13964e36824Saddy ke #define ICR_RF_OVERFLOW (1 << 2) 14064e36824Saddy ke #define ICR_TF_OVERFLOW (1 << 3) 14164e36824Saddy ke 14264e36824Saddy ke /* Bit fields in DMACR */ 14364e36824Saddy ke #define RF_DMA_EN (1 << 0) 14464e36824Saddy ke #define TF_DMA_EN (1 << 1) 14564e36824Saddy ke 146fab3e487SEmil Renner Berthing /* Driver state flags */ 147fab3e487SEmil Renner Berthing #define RXDMA (1 << 0) 148fab3e487SEmil Renner Berthing #define TXDMA (1 << 1) 14964e36824Saddy ke 150f9cfd522SAddy Ke /* sclk_out: spi master internal logic in rk3x can support 50Mhz */ 151420b82f8SEmil Renner Berthing #define MAX_SCLK_OUT 50000000U 152f9cfd522SAddy Ke 1535185a81cSBrian Norris /* 1545185a81cSBrian Norris * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However, 1555185a81cSBrian Norris * the controller seems to hang when given 0x10000, so stick with this for now. 1565185a81cSBrian Norris */ 1575185a81cSBrian Norris #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff 1585185a81cSBrian Norris 159aa099382SJeffy Chen #define ROCKCHIP_SPI_MAX_CS_NUM 2 16013a96935SJon Lin #define ROCKCHIP_SPI_VER2_TYPE1 0x05EC0002 16113a96935SJon Lin #define ROCKCHIP_SPI_VER2_TYPE2 0x00110002 162aa099382SJeffy Chen 16364e36824Saddy ke struct rockchip_spi { 16464e36824Saddy ke struct device *dev; 16564e36824Saddy ke 16664e36824Saddy ke struct clk *spiclk; 16764e36824Saddy ke struct clk *apb_pclk; 16864e36824Saddy ke 16964e36824Saddy ke void __iomem *regs; 170eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_rx; 171eee06a9eSEmil Renner Berthing dma_addr_t dma_addr_tx; 172fab3e487SEmil Renner Berthing 17301b59ce5SEmil Renner Berthing const void *tx; 17401b59ce5SEmil Renner Berthing void *rx; 17501b59ce5SEmil Renner Berthing unsigned int tx_left; 17601b59ce5SEmil Renner Berthing unsigned int rx_left; 17701b59ce5SEmil Renner Berthing 178fab3e487SEmil Renner Berthing atomic_t state; 179fab3e487SEmil Renner Berthing 18064e36824Saddy ke /*depth of the FIFO buffer */ 18164e36824Saddy ke u32 fifo_len; 182420b82f8SEmil Renner Berthing /* frequency of spiclk */ 183420b82f8SEmil Renner Berthing u32 freq; 18464e36824Saddy ke 18564e36824Saddy ke u8 n_bytes; 18674b7efa8SEmil Renner Berthing u8 rsd; 18764e36824Saddy ke 188aa099382SJeffy Chen bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM]; 189d065f41aSChris Ruehl 190d065f41aSChris Ruehl bool slave_abort; 19164e36824Saddy ke }; 19264e36824Saddy ke 19330688e4eSEmil Renner Berthing static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable) 19464e36824Saddy ke { 19530688e4eSEmil Renner Berthing writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR); 19664e36824Saddy ke } 19764e36824Saddy ke 1982df08e78SAddy Ke static inline void wait_for_idle(struct rockchip_spi *rs) 1992df08e78SAddy Ke { 2002df08e78SAddy Ke unsigned long timeout = jiffies + msecs_to_jiffies(5); 2012df08e78SAddy Ke 2022df08e78SAddy Ke do { 2032df08e78SAddy Ke if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)) 2042df08e78SAddy Ke return; 20564bc0110SDoug Anderson } while (!time_after(jiffies, timeout)); 2062df08e78SAddy Ke 2072df08e78SAddy Ke dev_warn(rs->dev, "spi controller is in busy state!\n"); 2082df08e78SAddy Ke } 2092df08e78SAddy Ke 21064e36824Saddy ke static u32 get_fifo_len(struct rockchip_spi *rs) 21164e36824Saddy ke { 21213a96935SJon Lin u32 ver; 21364e36824Saddy ke 21413a96935SJon Lin ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION); 21513a96935SJon Lin 21613a96935SJon Lin switch (ver) { 21713a96935SJon Lin case ROCKCHIP_SPI_VER2_TYPE1: 21813a96935SJon Lin case ROCKCHIP_SPI_VER2_TYPE2: 21913a96935SJon Lin return 64; 22013a96935SJon Lin default: 22113a96935SJon Lin return 32; 22264e36824Saddy ke } 22364e36824Saddy ke } 22464e36824Saddy ke 22564e36824Saddy ke static void rockchip_spi_set_cs(struct spi_device *spi, bool enable) 22664e36824Saddy ke { 227d66571a2SChris Ruehl struct spi_controller *ctlr = spi->controller; 228d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 229aa099382SJeffy Chen bool cs_asserted = !enable; 230b920cc31SHuibin Hong 231aa099382SJeffy Chen /* Return immediately for no-op */ 232aa099382SJeffy Chen if (cs_asserted == rs->cs_asserted[spi->chip_select]) 233aa099382SJeffy Chen return; 234aa099382SJeffy Chen 235aa099382SJeffy Chen if (cs_asserted) { 236aa099382SJeffy Chen /* Keep things powered as long as CS is asserted */ 237b920cc31SHuibin Hong pm_runtime_get_sync(rs->dev); 23864e36824Saddy ke 239aa099382SJeffy Chen ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 240aa099382SJeffy Chen BIT(spi->chip_select)); 241aa099382SJeffy Chen } else { 242aa099382SJeffy Chen ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 243aa099382SJeffy Chen BIT(spi->chip_select)); 24464e36824Saddy ke 245aa099382SJeffy Chen /* Drop reference from when we first asserted CS */ 246aa099382SJeffy Chen pm_runtime_put(rs->dev); 247aa099382SJeffy Chen } 24864e36824Saddy ke 249aa099382SJeffy Chen rs->cs_asserted[spi->chip_select] = cs_asserted; 25064e36824Saddy ke } 25164e36824Saddy ke 252d66571a2SChris Ruehl static void rockchip_spi_handle_err(struct spi_controller *ctlr, 25364e36824Saddy ke struct spi_message *msg) 25464e36824Saddy ke { 255d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 25664e36824Saddy ke 257ce386100SEmil Renner Berthing /* stop running spi transfer 258ce386100SEmil Renner Berthing * this also flushes both rx and tx fifos 2595dcc44edSAddy Ke */ 260ce386100SEmil Renner Berthing spi_enable_chip(rs, false); 261ce386100SEmil Renner Berthing 26201b59ce5SEmil Renner Berthing /* make sure all interrupts are masked */ 26301b59ce5SEmil Renner Berthing writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); 26401b59ce5SEmil Renner Berthing 265fab3e487SEmil Renner Berthing if (atomic_read(&rs->state) & TXDMA) 266d66571a2SChris Ruehl dmaengine_terminate_async(ctlr->dma_tx); 267fab3e487SEmil Renner Berthing 268ce386100SEmil Renner Berthing if (atomic_read(&rs->state) & RXDMA) 269d66571a2SChris Ruehl dmaengine_terminate_async(ctlr->dma_rx); 27064e36824Saddy ke } 27164e36824Saddy ke 27264e36824Saddy ke static void rockchip_spi_pio_writer(struct rockchip_spi *rs) 27364e36824Saddy ke { 27401b59ce5SEmil Renner Berthing u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR); 27501b59ce5SEmil Renner Berthing u32 words = min(rs->tx_left, tx_free); 27664e36824Saddy ke 27701b59ce5SEmil Renner Berthing rs->tx_left -= words; 27801b59ce5SEmil Renner Berthing for (; words; words--) { 27901b59ce5SEmil Renner Berthing u32 txw; 28001b59ce5SEmil Renner Berthing 28164e36824Saddy ke if (rs->n_bytes == 1) 28201b59ce5SEmil Renner Berthing txw = *(u8 *)rs->tx; 28364e36824Saddy ke else 28401b59ce5SEmil Renner Berthing txw = *(u16 *)rs->tx; 28564e36824Saddy ke 28664e36824Saddy ke writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR); 28764e36824Saddy ke rs->tx += rs->n_bytes; 28864e36824Saddy ke } 28964e36824Saddy ke } 29064e36824Saddy ke 29164e36824Saddy ke static void rockchip_spi_pio_reader(struct rockchip_spi *rs) 29264e36824Saddy ke { 29301b59ce5SEmil Renner Berthing u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); 294*4294e4acSJon Lin u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0; 29564e36824Saddy ke 29601b59ce5SEmil Renner Berthing /* the hardware doesn't allow us to change fifo threshold 29701b59ce5SEmil Renner Berthing * level while spi is enabled, so instead make sure to leave 29801b59ce5SEmil Renner Berthing * enough words in the rx fifo to get the last interrupt 29901b59ce5SEmil Renner Berthing * exactly when all words have been received 30001b59ce5SEmil Renner Berthing */ 30101b59ce5SEmil Renner Berthing if (rx_left) { 30201b59ce5SEmil Renner Berthing u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1; 30301b59ce5SEmil Renner Berthing 30401b59ce5SEmil Renner Berthing if (rx_left < ftl) { 30501b59ce5SEmil Renner Berthing rx_left = ftl; 30601b59ce5SEmil Renner Berthing words = rs->rx_left - rx_left; 30701b59ce5SEmil Renner Berthing } 30801b59ce5SEmil Renner Berthing } 30901b59ce5SEmil Renner Berthing 31001b59ce5SEmil Renner Berthing rs->rx_left = rx_left; 31101b59ce5SEmil Renner Berthing for (; words; words--) { 31201b59ce5SEmil Renner Berthing u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR); 31301b59ce5SEmil Renner Berthing 31401b59ce5SEmil Renner Berthing if (!rs->rx) 31501b59ce5SEmil Renner Berthing continue; 31601b59ce5SEmil Renner Berthing 31764e36824Saddy ke if (rs->n_bytes == 1) 31801b59ce5SEmil Renner Berthing *(u8 *)rs->rx = (u8)rxw; 31964e36824Saddy ke else 32001b59ce5SEmil Renner Berthing *(u16 *)rs->rx = (u16)rxw; 32164e36824Saddy ke rs->rx += rs->n_bytes; 3225dcc44edSAddy Ke } 32364e36824Saddy ke } 32464e36824Saddy ke 32501b59ce5SEmil Renner Berthing static irqreturn_t rockchip_spi_isr(int irq, void *dev_id) 32664e36824Saddy ke { 327d66571a2SChris Ruehl struct spi_controller *ctlr = dev_id; 328d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 32964e36824Saddy ke 33001b59ce5SEmil Renner Berthing if (rs->tx_left) 33101b59ce5SEmil Renner Berthing rockchip_spi_pio_writer(rs); 33201b59ce5SEmil Renner Berthing 33301b59ce5SEmil Renner Berthing rockchip_spi_pio_reader(rs); 33401b59ce5SEmil Renner Berthing if (!rs->rx_left) { 33501b59ce5SEmil Renner Berthing spi_enable_chip(rs, false); 33601b59ce5SEmil Renner Berthing writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR); 337d66571a2SChris Ruehl spi_finalize_current_transfer(ctlr); 33801b59ce5SEmil Renner Berthing } 33901b59ce5SEmil Renner Berthing 34001b59ce5SEmil Renner Berthing return IRQ_HANDLED; 34101b59ce5SEmil Renner Berthing } 34201b59ce5SEmil Renner Berthing 34301b59ce5SEmil Renner Berthing static int rockchip_spi_prepare_irq(struct rockchip_spi *rs, 34401b59ce5SEmil Renner Berthing struct spi_transfer *xfer) 34501b59ce5SEmil Renner Berthing { 34601b59ce5SEmil Renner Berthing rs->tx = xfer->tx_buf; 34701b59ce5SEmil Renner Berthing rs->rx = xfer->rx_buf; 34801b59ce5SEmil Renner Berthing rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0; 34901b59ce5SEmil Renner Berthing rs->rx_left = xfer->len / rs->n_bytes; 35001b59ce5SEmil Renner Berthing 35101b59ce5SEmil Renner Berthing writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR); 35230688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 353a3c17402SEmil Renner Berthing 35401b59ce5SEmil Renner Berthing if (rs->tx_left) 35564e36824Saddy ke rockchip_spi_pio_writer(rs); 35664e36824Saddy ke 35701b59ce5SEmil Renner Berthing /* 1 means the transfer is in progress */ 35801b59ce5SEmil Renner Berthing return 1; 35964e36824Saddy ke } 36064e36824Saddy ke 36164e36824Saddy ke static void rockchip_spi_dma_rxcb(void *data) 36264e36824Saddy ke { 363d66571a2SChris Ruehl struct spi_controller *ctlr = data; 364d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 365fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(RXDMA, &rs->state); 36664e36824Saddy ke 367d065f41aSChris Ruehl if (state & TXDMA && !rs->slave_abort) 368fab3e487SEmil Renner Berthing return; 36964e36824Saddy ke 37030688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 371d66571a2SChris Ruehl spi_finalize_current_transfer(ctlr); 372c28be31bSAddy Ke } 37364e36824Saddy ke 37464e36824Saddy ke static void rockchip_spi_dma_txcb(void *data) 37564e36824Saddy ke { 376d66571a2SChris Ruehl struct spi_controller *ctlr = data; 377d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 378fab3e487SEmil Renner Berthing int state = atomic_fetch_andnot(TXDMA, &rs->state); 379fab3e487SEmil Renner Berthing 380d065f41aSChris Ruehl if (state & RXDMA && !rs->slave_abort) 381fab3e487SEmil Renner Berthing return; 38264e36824Saddy ke 3832df08e78SAddy Ke /* Wait until the FIFO data completely. */ 3842df08e78SAddy Ke wait_for_idle(rs); 3852df08e78SAddy Ke 38630688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 387d66571a2SChris Ruehl spi_finalize_current_transfer(ctlr); 3882c2bc748SAddy Ke } 38964e36824Saddy ke 3904d9ca632SJon Lin static u32 rockchip_spi_calc_burst_size(u32 data_len) 3914d9ca632SJon Lin { 3924d9ca632SJon Lin u32 i; 3934d9ca632SJon Lin 3944d9ca632SJon Lin /* burst size: 1, 2, 4, 8 */ 3954d9ca632SJon Lin for (i = 1; i < 8; i <<= 1) { 3964d9ca632SJon Lin if (data_len & i) 3974d9ca632SJon Lin break; 3984d9ca632SJon Lin } 3994d9ca632SJon Lin 4004d9ca632SJon Lin return i; 4014d9ca632SJon Lin } 4024d9ca632SJon Lin 403fc1ad8eeSEmil Renner Berthing static int rockchip_spi_prepare_dma(struct rockchip_spi *rs, 404d66571a2SChris Ruehl struct spi_controller *ctlr, struct spi_transfer *xfer) 40564e36824Saddy ke { 40664e36824Saddy ke struct dma_async_tx_descriptor *rxdesc, *txdesc; 40764e36824Saddy ke 408fab3e487SEmil Renner Berthing atomic_set(&rs->state, 0); 40964e36824Saddy ke 41097cf5669SArnd Bergmann rxdesc = NULL; 411fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) { 41231bcb57bSEmil Renner Berthing struct dma_slave_config rxconf = { 41331bcb57bSEmil Renner Berthing .direction = DMA_DEV_TO_MEM, 414eee06a9eSEmil Renner Berthing .src_addr = rs->dma_addr_rx, 41531bcb57bSEmil Renner Berthing .src_addr_width = rs->n_bytes, 4164d9ca632SJon Lin .src_maxburst = rockchip_spi_calc_burst_size(xfer->len / 4174d9ca632SJon Lin rs->n_bytes), 41831bcb57bSEmil Renner Berthing }; 41931bcb57bSEmil Renner Berthing 420d66571a2SChris Ruehl dmaengine_slave_config(ctlr->dma_rx, &rxconf); 42164e36824Saddy ke 4225dcc44edSAddy Ke rxdesc = dmaengine_prep_slave_sg( 423d66571a2SChris Ruehl ctlr->dma_rx, 424fc1ad8eeSEmil Renner Berthing xfer->rx_sg.sgl, xfer->rx_sg.nents, 425d9071b7eSEmil Renner Berthing DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); 426ea984911SShawn Lin if (!rxdesc) 427ea984911SShawn Lin return -EINVAL; 42864e36824Saddy ke 42964e36824Saddy ke rxdesc->callback = rockchip_spi_dma_rxcb; 430d66571a2SChris Ruehl rxdesc->callback_param = ctlr; 43164e36824Saddy ke } 43264e36824Saddy ke 43397cf5669SArnd Bergmann txdesc = NULL; 434fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) { 43531bcb57bSEmil Renner Berthing struct dma_slave_config txconf = { 43631bcb57bSEmil Renner Berthing .direction = DMA_MEM_TO_DEV, 437eee06a9eSEmil Renner Berthing .dst_addr = rs->dma_addr_tx, 43831bcb57bSEmil Renner Berthing .dst_addr_width = rs->n_bytes, 43947300728SEmil Renner Berthing .dst_maxburst = rs->fifo_len / 4, 44031bcb57bSEmil Renner Berthing }; 44131bcb57bSEmil Renner Berthing 442d66571a2SChris Ruehl dmaengine_slave_config(ctlr->dma_tx, &txconf); 44364e36824Saddy ke 4445dcc44edSAddy Ke txdesc = dmaengine_prep_slave_sg( 445d66571a2SChris Ruehl ctlr->dma_tx, 446fc1ad8eeSEmil Renner Berthing xfer->tx_sg.sgl, xfer->tx_sg.nents, 447d9071b7eSEmil Renner Berthing DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT); 448ea984911SShawn Lin if (!txdesc) { 449ea984911SShawn Lin if (rxdesc) 450d66571a2SChris Ruehl dmaengine_terminate_sync(ctlr->dma_rx); 451ea984911SShawn Lin return -EINVAL; 452ea984911SShawn Lin } 45364e36824Saddy ke 45464e36824Saddy ke txdesc->callback = rockchip_spi_dma_txcb; 455d66571a2SChris Ruehl txdesc->callback_param = ctlr; 45664e36824Saddy ke } 45764e36824Saddy ke 45864e36824Saddy ke /* rx must be started before tx due to spi instinct */ 45997cf5669SArnd Bergmann if (rxdesc) { 460fab3e487SEmil Renner Berthing atomic_or(RXDMA, &rs->state); 46164e36824Saddy ke dmaengine_submit(rxdesc); 462d66571a2SChris Ruehl dma_async_issue_pending(ctlr->dma_rx); 46364e36824Saddy ke } 46464e36824Saddy ke 46530688e4eSEmil Renner Berthing spi_enable_chip(rs, true); 466a3c17402SEmil Renner Berthing 46797cf5669SArnd Bergmann if (txdesc) { 468fab3e487SEmil Renner Berthing atomic_or(TXDMA, &rs->state); 46964e36824Saddy ke dmaengine_submit(txdesc); 470d66571a2SChris Ruehl dma_async_issue_pending(ctlr->dma_tx); 47164e36824Saddy ke } 472ea984911SShawn Lin 473a3c17402SEmil Renner Berthing /* 1 means the transfer is in progress */ 474a3c17402SEmil Renner Berthing return 1; 47564e36824Saddy ke } 47664e36824Saddy ke 477fc1ad8eeSEmil Renner Berthing static void rockchip_spi_config(struct rockchip_spi *rs, 478eff0275eSEmil Renner Berthing struct spi_device *spi, struct spi_transfer *xfer, 479d065f41aSChris Ruehl bool use_dma, bool slave_mode) 48064e36824Saddy ke { 4812410d6a3SEmil Renner Berthing u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET 4822410d6a3SEmil Renner Berthing | CR0_BHT_8BIT << CR0_BHT_OFFSET 4832410d6a3SEmil Renner Berthing | CR0_SSD_ONE << CR0_SSD_OFFSET 4842410d6a3SEmil Renner Berthing | CR0_EM_BIG << CR0_EM_OFFSET; 48565498c6aSEmil Renner Berthing u32 cr1; 48665498c6aSEmil Renner Berthing u32 dmacr = 0; 48764e36824Saddy ke 488d065f41aSChris Ruehl if (slave_mode) 489d065f41aSChris Ruehl cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET; 490d065f41aSChris Ruehl rs->slave_abort = false; 491d065f41aSChris Ruehl 49274b7efa8SEmil Renner Berthing cr0 |= rs->rsd << CR0_RSD_OFFSET; 493fc1ad8eeSEmil Renner Berthing cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET; 49404290192SEmil Renner Berthing if (spi->mode & SPI_LSB_FIRST) 49504290192SEmil Renner Berthing cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET; 496fc1ad8eeSEmil Renner Berthing 497fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf && xfer->tx_buf) 498fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET; 499fc1ad8eeSEmil Renner Berthing else if (xfer->rx_buf) 500fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET; 50101b59ce5SEmil Renner Berthing else if (use_dma) 502fc1ad8eeSEmil Renner Berthing cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET; 50364e36824Saddy ke 50465498c6aSEmil Renner Berthing switch (xfer->bits_per_word) { 50565498c6aSEmil Renner Berthing case 4: 50665498c6aSEmil Renner Berthing cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET; 50765498c6aSEmil Renner Berthing cr1 = xfer->len - 1; 50865498c6aSEmil Renner Berthing break; 50965498c6aSEmil Renner Berthing case 8: 51065498c6aSEmil Renner Berthing cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET; 51165498c6aSEmil Renner Berthing cr1 = xfer->len - 1; 51265498c6aSEmil Renner Berthing break; 51365498c6aSEmil Renner Berthing case 16: 51465498c6aSEmil Renner Berthing cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET; 51565498c6aSEmil Renner Berthing cr1 = xfer->len / 2 - 1; 51665498c6aSEmil Renner Berthing break; 51765498c6aSEmil Renner Berthing default: 51865498c6aSEmil Renner Berthing /* we only whitelist 4, 8 and 16 bit words in 519d66571a2SChris Ruehl * ctlr->bits_per_word_mask, so this shouldn't 52065498c6aSEmil Renner Berthing * happen 52165498c6aSEmil Renner Berthing */ 52265498c6aSEmil Renner Berthing unreachable(); 52365498c6aSEmil Renner Berthing } 52465498c6aSEmil Renner Berthing 525eff0275eSEmil Renner Berthing if (use_dma) { 526fc1ad8eeSEmil Renner Berthing if (xfer->tx_buf) 52764e36824Saddy ke dmacr |= TF_DMA_EN; 528fc1ad8eeSEmil Renner Berthing if (xfer->rx_buf) 52964e36824Saddy ke dmacr |= RF_DMA_EN; 53064e36824Saddy ke } 53164e36824Saddy ke 53264e36824Saddy ke writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0); 53365498c6aSEmil Renner Berthing writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1); 53404b37d2dSHuibin Hong 53501b59ce5SEmil Renner Berthing /* unfortunately setting the fifo threshold level to generate an 53601b59ce5SEmil Renner Berthing * interrupt exactly when the fifo is full doesn't seem to work, 53701b59ce5SEmil Renner Berthing * so we need the strict inequality here 53801b59ce5SEmil Renner Berthing */ 53901b59ce5SEmil Renner Berthing if (xfer->len < rs->fifo_len) 54001b59ce5SEmil Renner Berthing writel_relaxed(xfer->len - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 54101b59ce5SEmil Renner Berthing else 54264e36824Saddy ke writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR); 54364e36824Saddy ke 54447300728SEmil Renner Berthing writel_relaxed(rs->fifo_len / 2, rs->regs + ROCKCHIP_SPI_DMATDLR); 5454d9ca632SJon Lin writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1, 5464d9ca632SJon Lin rs->regs + ROCKCHIP_SPI_DMARDLR); 54764e36824Saddy ke writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR); 54864e36824Saddy ke 549420b82f8SEmil Renner Berthing /* the hardware only supports an even clock divisor, so 550420b82f8SEmil Renner Berthing * round divisor = spiclk / speed up to nearest even number 551420b82f8SEmil Renner Berthing * so that the resulting speed is <= the requested speed 552420b82f8SEmil Renner Berthing */ 553420b82f8SEmil Renner Berthing writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz), 554420b82f8SEmil Renner Berthing rs->regs + ROCKCHIP_SPI_BAUDR); 55564e36824Saddy ke } 55664e36824Saddy ke 5575185a81cSBrian Norris static size_t rockchip_spi_max_transfer_size(struct spi_device *spi) 5585185a81cSBrian Norris { 5595185a81cSBrian Norris return ROCKCHIP_SPI_MAX_TRANLEN; 5605185a81cSBrian Norris } 5615185a81cSBrian Norris 562d065f41aSChris Ruehl static int rockchip_spi_slave_abort(struct spi_controller *ctlr) 563d065f41aSChris Ruehl { 564d065f41aSChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 565d065f41aSChris Ruehl 566d065f41aSChris Ruehl rs->slave_abort = true; 567d065f41aSChris Ruehl complete(&ctlr->xfer_completion); 568d065f41aSChris Ruehl 569d065f41aSChris Ruehl return 0; 570d065f41aSChris Ruehl } 571d065f41aSChris Ruehl 5725dcc44edSAddy Ke static int rockchip_spi_transfer_one( 573d66571a2SChris Ruehl struct spi_controller *ctlr, 57464e36824Saddy ke struct spi_device *spi, 57564e36824Saddy ke struct spi_transfer *xfer) 57664e36824Saddy ke { 577d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 578eff0275eSEmil Renner Berthing bool use_dma; 57964e36824Saddy ke 58062946172SDoug Anderson WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) && 58162946172SDoug Anderson (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)); 58264e36824Saddy ke 58364e36824Saddy ke if (!xfer->tx_buf && !xfer->rx_buf) { 58464e36824Saddy ke dev_err(rs->dev, "No buffer for transfer\n"); 58564e36824Saddy ke return -EINVAL; 58664e36824Saddy ke } 58764e36824Saddy ke 5885185a81cSBrian Norris if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) { 5895185a81cSBrian Norris dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len); 5905185a81cSBrian Norris return -EINVAL; 5915185a81cSBrian Norris } 5925185a81cSBrian Norris 59365498c6aSEmil Renner Berthing rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2; 59464e36824Saddy ke 595d66571a2SChris Ruehl use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false; 59664e36824Saddy ke 597d065f41aSChris Ruehl rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave); 59864e36824Saddy ke 599eff0275eSEmil Renner Berthing if (use_dma) 600d66571a2SChris Ruehl return rockchip_spi_prepare_dma(rs, ctlr, xfer); 60164e36824Saddy ke 60201b59ce5SEmil Renner Berthing return rockchip_spi_prepare_irq(rs, xfer); 60364e36824Saddy ke } 60464e36824Saddy ke 605d66571a2SChris Ruehl static bool rockchip_spi_can_dma(struct spi_controller *ctlr, 60664e36824Saddy ke struct spi_device *spi, 60764e36824Saddy ke struct spi_transfer *xfer) 60864e36824Saddy ke { 609d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 61001b59ce5SEmil Renner Berthing unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2; 61164e36824Saddy ke 61201b59ce5SEmil Renner Berthing /* if the numbor of spi words to transfer is less than the fifo 61301b59ce5SEmil Renner Berthing * length we can just fill the fifo and wait for a single irq, 61401b59ce5SEmil Renner Berthing * so don't bother setting up dma 61501b59ce5SEmil Renner Berthing */ 61601b59ce5SEmil Renner Berthing return xfer->len / bytes_per_word >= rs->fifo_len; 61764e36824Saddy ke } 61864e36824Saddy ke 61964e36824Saddy ke static int rockchip_spi_probe(struct platform_device *pdev) 62064e36824Saddy ke { 62143de979dSJeffy Chen int ret; 62264e36824Saddy ke struct rockchip_spi *rs; 623d66571a2SChris Ruehl struct spi_controller *ctlr; 62464e36824Saddy ke struct resource *mem; 625d065f41aSChris Ruehl struct device_node *np = pdev->dev.of_node; 62676b17e6eSJulius Werner u32 rsd_nsecs; 627d065f41aSChris Ruehl bool slave_mode; 62864e36824Saddy ke 629d065f41aSChris Ruehl slave_mode = of_property_read_bool(np, "spi-slave"); 630d065f41aSChris Ruehl 631d065f41aSChris Ruehl if (slave_mode) 632d065f41aSChris Ruehl ctlr = spi_alloc_slave(&pdev->dev, 633d065f41aSChris Ruehl sizeof(struct rockchip_spi)); 634d065f41aSChris Ruehl else 635d065f41aSChris Ruehl ctlr = spi_alloc_master(&pdev->dev, 636d065f41aSChris Ruehl sizeof(struct rockchip_spi)); 637d065f41aSChris Ruehl 638d66571a2SChris Ruehl if (!ctlr) 63964e36824Saddy ke return -ENOMEM; 6405dcc44edSAddy Ke 641d66571a2SChris Ruehl platform_set_drvdata(pdev, ctlr); 64264e36824Saddy ke 643d66571a2SChris Ruehl rs = spi_controller_get_devdata(ctlr); 644d065f41aSChris Ruehl ctlr->slave = slave_mode; 64564e36824Saddy ke 64664e36824Saddy ke /* Get basic io resource and map it */ 64764e36824Saddy ke mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 64864e36824Saddy ke rs->regs = devm_ioremap_resource(&pdev->dev, mem); 64964e36824Saddy ke if (IS_ERR(rs->regs)) { 65064e36824Saddy ke ret = PTR_ERR(rs->regs); 651d66571a2SChris Ruehl goto err_put_ctlr; 65264e36824Saddy ke } 65364e36824Saddy ke 65464e36824Saddy ke rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 65564e36824Saddy ke if (IS_ERR(rs->apb_pclk)) { 65664e36824Saddy ke dev_err(&pdev->dev, "Failed to get apb_pclk\n"); 65764e36824Saddy ke ret = PTR_ERR(rs->apb_pclk); 658d66571a2SChris Ruehl goto err_put_ctlr; 65964e36824Saddy ke } 66064e36824Saddy ke 66164e36824Saddy ke rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); 66264e36824Saddy ke if (IS_ERR(rs->spiclk)) { 66364e36824Saddy ke dev_err(&pdev->dev, "Failed to get spi_pclk\n"); 66464e36824Saddy ke ret = PTR_ERR(rs->spiclk); 665d66571a2SChris Ruehl goto err_put_ctlr; 66664e36824Saddy ke } 66764e36824Saddy ke 66864e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 66943de979dSJeffy Chen if (ret < 0) { 67064e36824Saddy ke dev_err(&pdev->dev, "Failed to enable apb_pclk\n"); 671d66571a2SChris Ruehl goto err_put_ctlr; 67264e36824Saddy ke } 67364e36824Saddy ke 67464e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 67543de979dSJeffy Chen if (ret < 0) { 67664e36824Saddy ke dev_err(&pdev->dev, "Failed to enable spi_clk\n"); 677c351587eSJeffy Chen goto err_disable_apbclk; 67864e36824Saddy ke } 67964e36824Saddy ke 68030688e4eSEmil Renner Berthing spi_enable_chip(rs, false); 68164e36824Saddy ke 68201b59ce5SEmil Renner Berthing ret = platform_get_irq(pdev, 0); 68301b59ce5SEmil Renner Berthing if (ret < 0) 68401b59ce5SEmil Renner Berthing goto err_disable_spiclk; 68501b59ce5SEmil Renner Berthing 68601b59ce5SEmil Renner Berthing ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL, 687d66571a2SChris Ruehl IRQF_ONESHOT, dev_name(&pdev->dev), ctlr); 68801b59ce5SEmil Renner Berthing if (ret) 68901b59ce5SEmil Renner Berthing goto err_disable_spiclk; 69001b59ce5SEmil Renner Berthing 69164e36824Saddy ke rs->dev = &pdev->dev; 692420b82f8SEmil Renner Berthing rs->freq = clk_get_rate(rs->spiclk); 69364e36824Saddy ke 69476b17e6eSJulius Werner if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", 69574b7efa8SEmil Renner Berthing &rsd_nsecs)) { 69674b7efa8SEmil Renner Berthing /* rx sample delay is expressed in parent clock cycles (max 3) */ 69774b7efa8SEmil Renner Berthing u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8), 69874b7efa8SEmil Renner Berthing 1000000000 >> 8); 69974b7efa8SEmil Renner Berthing if (!rsd) { 70074b7efa8SEmil Renner Berthing dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n", 70174b7efa8SEmil Renner Berthing rs->freq, rsd_nsecs); 70274b7efa8SEmil Renner Berthing } else if (rsd > CR0_RSD_MAX) { 70374b7efa8SEmil Renner Berthing rsd = CR0_RSD_MAX; 70474b7efa8SEmil Renner Berthing dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n", 70574b7efa8SEmil Renner Berthing rs->freq, rsd_nsecs, 70674b7efa8SEmil Renner Berthing CR0_RSD_MAX * 1000000000U / rs->freq); 70774b7efa8SEmil Renner Berthing } 70874b7efa8SEmil Renner Berthing rs->rsd = rsd; 70974b7efa8SEmil Renner Berthing } 71076b17e6eSJulius Werner 71164e36824Saddy ke rs->fifo_len = get_fifo_len(rs); 71264e36824Saddy ke if (!rs->fifo_len) { 71364e36824Saddy ke dev_err(&pdev->dev, "Failed to get fifo length\n"); 714db7e8d90SWei Yongjun ret = -EINVAL; 715c351587eSJeffy Chen goto err_disable_spiclk; 71664e36824Saddy ke } 71764e36824Saddy ke 71864e36824Saddy ke pm_runtime_set_active(&pdev->dev); 71964e36824Saddy ke pm_runtime_enable(&pdev->dev); 72064e36824Saddy ke 721d66571a2SChris Ruehl ctlr->auto_runtime_pm = true; 722d66571a2SChris Ruehl ctlr->bus_num = pdev->id; 723d66571a2SChris Ruehl ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST; 724d065f41aSChris Ruehl if (slave_mode) { 725d065f41aSChris Ruehl ctlr->mode_bits |= SPI_NO_CS; 726d065f41aSChris Ruehl ctlr->slave_abort = rockchip_spi_slave_abort; 727d065f41aSChris Ruehl } else { 728d065f41aSChris Ruehl ctlr->flags = SPI_MASTER_GPIO_SS; 729eb1262e3SChris Ruehl ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM; 730eb1262e3SChris Ruehl /* 731eb1262e3SChris Ruehl * rk spi0 has two native cs, spi1..5 one cs only 732eb1262e3SChris Ruehl * if num-cs is missing in the dts, default to 1 733eb1262e3SChris Ruehl */ 734eb1262e3SChris Ruehl if (of_property_read_u16(np, "num-cs", &ctlr->num_chipselect)) 735eb1262e3SChris Ruehl ctlr->num_chipselect = 1; 736eb1262e3SChris Ruehl ctlr->use_gpio_descriptors = true; 737d065f41aSChris Ruehl } 738d66571a2SChris Ruehl ctlr->dev.of_node = pdev->dev.of_node; 739d66571a2SChris Ruehl ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4); 740d66571a2SChris Ruehl ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX; 741d66571a2SChris Ruehl ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT); 74264e36824Saddy ke 743d66571a2SChris Ruehl ctlr->set_cs = rockchip_spi_set_cs; 744d66571a2SChris Ruehl ctlr->transfer_one = rockchip_spi_transfer_one; 745d66571a2SChris Ruehl ctlr->max_transfer_size = rockchip_spi_max_transfer_size; 746d66571a2SChris Ruehl ctlr->handle_err = rockchip_spi_handle_err; 74764e36824Saddy ke 748d66571a2SChris Ruehl ctlr->dma_tx = dma_request_chan(rs->dev, "tx"); 749d66571a2SChris Ruehl if (IS_ERR(ctlr->dma_tx)) { 75061cadcf4SShawn Lin /* Check tx to see if we need defer probing driver */ 751d66571a2SChris Ruehl if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) { 75261cadcf4SShawn Lin ret = -EPROBE_DEFER; 753c351587eSJeffy Chen goto err_disable_pm_runtime; 75461cadcf4SShawn Lin } 75564e36824Saddy ke dev_warn(rs->dev, "Failed to request TX DMA channel\n"); 756d66571a2SChris Ruehl ctlr->dma_tx = NULL; 75764e36824Saddy ke } 758e4c0e06fSShawn Lin 759d66571a2SChris Ruehl ctlr->dma_rx = dma_request_chan(rs->dev, "rx"); 760d66571a2SChris Ruehl if (IS_ERR(ctlr->dma_rx)) { 761d66571a2SChris Ruehl if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) { 762e4c0e06fSShawn Lin ret = -EPROBE_DEFER; 7635de7ed0cSDan Carpenter goto err_free_dma_tx; 764e4c0e06fSShawn Lin } 76564e36824Saddy ke dev_warn(rs->dev, "Failed to request RX DMA channel\n"); 766d66571a2SChris Ruehl ctlr->dma_rx = NULL; 76764e36824Saddy ke } 76864e36824Saddy ke 769d66571a2SChris Ruehl if (ctlr->dma_tx && ctlr->dma_rx) { 770eee06a9eSEmil Renner Berthing rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR; 771eee06a9eSEmil Renner Berthing rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR; 772d66571a2SChris Ruehl ctlr->can_dma = rockchip_spi_can_dma; 77364e36824Saddy ke } 77464e36824Saddy ke 775d66571a2SChris Ruehl ret = devm_spi_register_controller(&pdev->dev, ctlr); 77643de979dSJeffy Chen if (ret < 0) { 777d66571a2SChris Ruehl dev_err(&pdev->dev, "Failed to register controller\n"); 778c351587eSJeffy Chen goto err_free_dma_rx; 77964e36824Saddy ke } 78064e36824Saddy ke 78164e36824Saddy ke return 0; 78264e36824Saddy ke 783c351587eSJeffy Chen err_free_dma_rx: 784d66571a2SChris Ruehl if (ctlr->dma_rx) 785d66571a2SChris Ruehl dma_release_channel(ctlr->dma_rx); 7865de7ed0cSDan Carpenter err_free_dma_tx: 787d66571a2SChris Ruehl if (ctlr->dma_tx) 788d66571a2SChris Ruehl dma_release_channel(ctlr->dma_tx); 789c351587eSJeffy Chen err_disable_pm_runtime: 790c351587eSJeffy Chen pm_runtime_disable(&pdev->dev); 791c351587eSJeffy Chen err_disable_spiclk: 79264e36824Saddy ke clk_disable_unprepare(rs->spiclk); 793c351587eSJeffy Chen err_disable_apbclk: 79464e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 795d66571a2SChris Ruehl err_put_ctlr: 796d66571a2SChris Ruehl spi_controller_put(ctlr); 79764e36824Saddy ke 79864e36824Saddy ke return ret; 79964e36824Saddy ke } 80064e36824Saddy ke 80164e36824Saddy ke static int rockchip_spi_remove(struct platform_device *pdev) 80264e36824Saddy ke { 803d66571a2SChris Ruehl struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev)); 804d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 80564e36824Saddy ke 8066a06e895SJeffy Chen pm_runtime_get_sync(&pdev->dev); 80764e36824Saddy ke 80864e36824Saddy ke clk_disable_unprepare(rs->spiclk); 80964e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 81064e36824Saddy ke 8116a06e895SJeffy Chen pm_runtime_put_noidle(&pdev->dev); 8126a06e895SJeffy Chen pm_runtime_disable(&pdev->dev); 8136a06e895SJeffy Chen pm_runtime_set_suspended(&pdev->dev); 8146a06e895SJeffy Chen 815d66571a2SChris Ruehl if (ctlr->dma_tx) 816d66571a2SChris Ruehl dma_release_channel(ctlr->dma_tx); 817d66571a2SChris Ruehl if (ctlr->dma_rx) 818d66571a2SChris Ruehl dma_release_channel(ctlr->dma_rx); 81964e36824Saddy ke 820d66571a2SChris Ruehl spi_controller_put(ctlr); 821844c9f47SShawn Lin 82264e36824Saddy ke return 0; 82364e36824Saddy ke } 82464e36824Saddy ke 82564e36824Saddy ke #ifdef CONFIG_PM_SLEEP 82664e36824Saddy ke static int rockchip_spi_suspend(struct device *dev) 82764e36824Saddy ke { 82843de979dSJeffy Chen int ret; 829d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 83064e36824Saddy ke 831d66571a2SChris Ruehl ret = spi_controller_suspend(ctlr); 83243de979dSJeffy Chen if (ret < 0) 83364e36824Saddy ke return ret; 83464e36824Saddy ke 835d38c4ae1SJeffy Chen ret = pm_runtime_force_suspend(dev); 836d38c4ae1SJeffy Chen if (ret < 0) 837d38c4ae1SJeffy Chen return ret; 83864e36824Saddy ke 83923e291c2SBrian Norris pinctrl_pm_select_sleep_state(dev); 84023e291c2SBrian Norris 84143de979dSJeffy Chen return 0; 84264e36824Saddy ke } 84364e36824Saddy ke 84464e36824Saddy ke static int rockchip_spi_resume(struct device *dev) 84564e36824Saddy ke { 84643de979dSJeffy Chen int ret; 847d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 848d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 84964e36824Saddy ke 85023e291c2SBrian Norris pinctrl_pm_select_default_state(dev); 85123e291c2SBrian Norris 852d38c4ae1SJeffy Chen ret = pm_runtime_force_resume(dev); 85364e36824Saddy ke if (ret < 0) 85464e36824Saddy ke return ret; 85564e36824Saddy ke 856d66571a2SChris Ruehl ret = spi_controller_resume(ctlr); 85764e36824Saddy ke if (ret < 0) { 85864e36824Saddy ke clk_disable_unprepare(rs->spiclk); 85964e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 86064e36824Saddy ke } 86164e36824Saddy ke 86243de979dSJeffy Chen return 0; 86364e36824Saddy ke } 86464e36824Saddy ke #endif /* CONFIG_PM_SLEEP */ 86564e36824Saddy ke 866ec833050SRafael J. Wysocki #ifdef CONFIG_PM 86764e36824Saddy ke static int rockchip_spi_runtime_suspend(struct device *dev) 86864e36824Saddy ke { 869d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 870d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 87164e36824Saddy ke 87264e36824Saddy ke clk_disable_unprepare(rs->spiclk); 87364e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 87464e36824Saddy ke 87564e36824Saddy ke return 0; 87664e36824Saddy ke } 87764e36824Saddy ke 87864e36824Saddy ke static int rockchip_spi_runtime_resume(struct device *dev) 87964e36824Saddy ke { 88064e36824Saddy ke int ret; 881d66571a2SChris Ruehl struct spi_controller *ctlr = dev_get_drvdata(dev); 882d66571a2SChris Ruehl struct rockchip_spi *rs = spi_controller_get_devdata(ctlr); 88364e36824Saddy ke 88464e36824Saddy ke ret = clk_prepare_enable(rs->apb_pclk); 88543de979dSJeffy Chen if (ret < 0) 88664e36824Saddy ke return ret; 88764e36824Saddy ke 88864e36824Saddy ke ret = clk_prepare_enable(rs->spiclk); 88943de979dSJeffy Chen if (ret < 0) 89064e36824Saddy ke clk_disable_unprepare(rs->apb_pclk); 89164e36824Saddy ke 89243de979dSJeffy Chen return 0; 89364e36824Saddy ke } 894ec833050SRafael J. Wysocki #endif /* CONFIG_PM */ 89564e36824Saddy ke 89664e36824Saddy ke static const struct dev_pm_ops rockchip_spi_pm = { 89764e36824Saddy ke SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume) 89864e36824Saddy ke SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend, 89964e36824Saddy ke rockchip_spi_runtime_resume, NULL) 90064e36824Saddy ke }; 90164e36824Saddy ke 90264e36824Saddy ke static const struct of_device_id rockchip_spi_dt_match[] = { 903c6486eadSJohan Jonker { .compatible = "rockchip,px30-spi", }, 904aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3036-spi", }, 90564e36824Saddy ke { .compatible = "rockchip,rk3066-spi", }, 906b839b785SAddy Ke { .compatible = "rockchip,rk3188-spi", }, 907aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3228-spi", }, 908b839b785SAddy Ke { .compatible = "rockchip,rk3288-spi", }, 909c6486eadSJohan Jonker { .compatible = "rockchip,rk3308-spi", }, 910c6486eadSJohan Jonker { .compatible = "rockchip,rk3328-spi", }, 911aa29ea3dSCaesar Wang { .compatible = "rockchip,rk3368-spi", }, 9129b7a5622SXu Jianqun { .compatible = "rockchip,rk3399-spi", }, 913c6486eadSJohan Jonker { .compatible = "rockchip,rv1108-spi", }, 91464e36824Saddy ke { }, 91564e36824Saddy ke }; 91664e36824Saddy ke MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); 91764e36824Saddy ke 91864e36824Saddy ke static struct platform_driver rockchip_spi_driver = { 91964e36824Saddy ke .driver = { 92064e36824Saddy ke .name = DRIVER_NAME, 92164e36824Saddy ke .pm = &rockchip_spi_pm, 92264e36824Saddy ke .of_match_table = of_match_ptr(rockchip_spi_dt_match), 92364e36824Saddy ke }, 92464e36824Saddy ke .probe = rockchip_spi_probe, 92564e36824Saddy ke .remove = rockchip_spi_remove, 92664e36824Saddy ke }; 92764e36824Saddy ke 92864e36824Saddy ke module_platform_driver(rockchip_spi_driver); 92964e36824Saddy ke 9305dcc44edSAddy Ke MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); 93164e36824Saddy ke MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver"); 93264e36824Saddy ke MODULE_LICENSE("GPL v2"); 933