blob: 0887b19ef3ad72eba33e67172d4c32b07e4e9435 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
addy ke64e36822014-07-01 09:03:59 +08002/*
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
Addy Ke5dcc44e2014-07-11 10:07:56 +08004 * Author: Addy Ke <addy.ke@rock-chips.com>
addy ke64e36822014-07-01 09:03:59 +08005 */
6
addy ke64e36822014-07-01 09:03:59 +08007#include <linux/clk.h>
addy ke64e36822014-07-01 09:03:59 +08008#include <linux/dmaengine.h>
Suren Baghdasaryan8af0c182019-05-14 15:41:12 -07009#include <linux/interrupt.h>
Shawn Linec5c5d82016-03-10 14:51:48 +080010#include <linux/module.h>
11#include <linux/of.h>
Brian Norris23e291c2016-12-16 16:59:16 -080012#include <linux/pinctrl/consumer.h>
Shawn Linec5c5d82016-03-10 14:51:48 +080013#include <linux/platform_device.h>
14#include <linux/spi/spi.h>
15#include <linux/pm_runtime.h>
16#include <linux/scatterlist.h>
addy ke64e36822014-07-01 09:03:59 +080017
18#define DRIVER_NAME "rockchip-spi"
19
Jeffy Chenaa099382017-06-28 12:38:43 +080020#define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
21 writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
22#define ROCKCHIP_SPI_SET_BITS(reg, bits) \
23 writel_relaxed(readl_relaxed(reg) | (bits), reg)
24
addy ke64e36822014-07-01 09:03:59 +080025/* SPI register offsets */
26#define ROCKCHIP_SPI_CTRLR0 0x0000
27#define ROCKCHIP_SPI_CTRLR1 0x0004
28#define ROCKCHIP_SPI_SSIENR 0x0008
29#define ROCKCHIP_SPI_SER 0x000c
30#define ROCKCHIP_SPI_BAUDR 0x0010
31#define ROCKCHIP_SPI_TXFTLR 0x0014
32#define ROCKCHIP_SPI_RXFTLR 0x0018
33#define ROCKCHIP_SPI_TXFLR 0x001c
34#define ROCKCHIP_SPI_RXFLR 0x0020
35#define ROCKCHIP_SPI_SR 0x0024
36#define ROCKCHIP_SPI_IPR 0x0028
37#define ROCKCHIP_SPI_IMR 0x002c
38#define ROCKCHIP_SPI_ISR 0x0030
39#define ROCKCHIP_SPI_RISR 0x0034
40#define ROCKCHIP_SPI_ICR 0x0038
41#define ROCKCHIP_SPI_DMACR 0x003c
Jon Lin13a96932020-07-23 08:43:55 +080042#define ROCKCHIP_SPI_DMATDLR 0x0040
43#define ROCKCHIP_SPI_DMARDLR 0x0044
44#define ROCKCHIP_SPI_VERSION 0x0048
addy ke64e36822014-07-01 09:03:59 +080045#define ROCKCHIP_SPI_TXDR 0x0400
46#define ROCKCHIP_SPI_RXDR 0x0800
47
48/* Bit fields in CTRLR0 */
49#define CR0_DFS_OFFSET 0
Emil Renner Berthing65498c62018-10-31 11:57:10 +010050#define CR0_DFS_4BIT 0x0
51#define CR0_DFS_8BIT 0x1
52#define CR0_DFS_16BIT 0x2
addy ke64e36822014-07-01 09:03:59 +080053
54#define CR0_CFS_OFFSET 2
55
56#define CR0_SCPH_OFFSET 6
57
58#define CR0_SCPOL_OFFSET 7
59
60#define CR0_CSM_OFFSET 8
61#define CR0_CSM_KEEP 0x0
62/* ss_n be high for half sclk_out cycles */
63#define CR0_CSM_HALF 0X1
64/* ss_n be high for one sclk_out cycle */
65#define CR0_CSM_ONE 0x2
66
67/* ss_n to sclk_out delay */
68#define CR0_SSD_OFFSET 10
69/*
70 * The period between ss_n active and
71 * sclk_out active is half sclk_out cycles
72 */
73#define CR0_SSD_HALF 0x0
74/*
75 * The period between ss_n active and
76 * sclk_out active is one sclk_out cycle
77 */
78#define CR0_SSD_ONE 0x1
79
80#define CR0_EM_OFFSET 11
81#define CR0_EM_LITTLE 0x0
82#define CR0_EM_BIG 0x1
83
84#define CR0_FBM_OFFSET 12
85#define CR0_FBM_MSB 0x0
86#define CR0_FBM_LSB 0x1
87
88#define CR0_BHT_OFFSET 13
89#define CR0_BHT_16BIT 0x0
90#define CR0_BHT_8BIT 0x1
91
92#define CR0_RSD_OFFSET 14
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +010093#define CR0_RSD_MAX 0x3
addy ke64e36822014-07-01 09:03:59 +080094
95#define CR0_FRF_OFFSET 16
96#define CR0_FRF_SPI 0x0
97#define CR0_FRF_SSP 0x1
98#define CR0_FRF_MICROWIRE 0x2
99
100#define CR0_XFM_OFFSET 18
101#define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
102#define CR0_XFM_TR 0x0
103#define CR0_XFM_TO 0x1
104#define CR0_XFM_RO 0x2
105
106#define CR0_OPM_OFFSET 20
107#define CR0_OPM_MASTER 0x0
108#define CR0_OPM_SLAVE 0x1
109
110#define CR0_MTM_OFFSET 0x21
111
112/* Bit fields in SER, 2bit */
113#define SER_MASK 0x3
114
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100115/* Bit fields in BAUDR */
116#define BAUDR_SCKDV_MIN 2
117#define BAUDR_SCKDV_MAX 65534
118
addy ke64e36822014-07-01 09:03:59 +0800119/* Bit fields in SR, 5bit */
120#define SR_MASK 0x1f
121#define SR_BUSY (1 << 0)
122#define SR_TF_FULL (1 << 1)
123#define SR_TF_EMPTY (1 << 2)
124#define SR_RF_EMPTY (1 << 3)
125#define SR_RF_FULL (1 << 4)
126
127/* Bit fields in ISR, IMR, ISR, RISR, 5bit */
128#define INT_MASK 0x1f
129#define INT_TF_EMPTY (1 << 0)
130#define INT_TF_OVERFLOW (1 << 1)
131#define INT_RF_UNDERFLOW (1 << 2)
132#define INT_RF_OVERFLOW (1 << 3)
133#define INT_RF_FULL (1 << 4)
134
135/* Bit fields in ICR, 4bit */
136#define ICR_MASK 0x0f
137#define ICR_ALL (1 << 0)
138#define ICR_RF_UNDERFLOW (1 << 1)
139#define ICR_RF_OVERFLOW (1 << 2)
140#define ICR_TF_OVERFLOW (1 << 3)
141
142/* Bit fields in DMACR */
143#define RF_DMA_EN (1 << 0)
144#define TF_DMA_EN (1 << 1)
145
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100146/* Driver state flags */
147#define RXDMA (1 << 0)
148#define TXDMA (1 << 1)
addy ke64e36822014-07-01 09:03:59 +0800149
Addy Kef9cfd522014-10-15 19:25:49 +0800150/* sclk_out: spi master internal logic in rk3x can support 50Mhz */
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100151#define MAX_SCLK_OUT 50000000U
Addy Kef9cfd522014-10-15 19:25:49 +0800152
Brian Norris5185a812016-07-14 18:30:59 -0700153/*
154 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
155 * the controller seems to hang when given 0x10000, so stick with this for now.
156 */
157#define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
158
Jeffy Chenaa099382017-06-28 12:38:43 +0800159#define ROCKCHIP_SPI_MAX_CS_NUM 2
Jon Lin13a96932020-07-23 08:43:55 +0800160#define ROCKCHIP_SPI_VER2_TYPE1 0x05EC0002
161#define ROCKCHIP_SPI_VER2_TYPE2 0x00110002
Jeffy Chenaa099382017-06-28 12:38:43 +0800162
Alexander Kochetkov940f3bb2020-10-16 11:50:14 +0300163#define ROCKCHIP_AUTOSUSPEND_TIMEOUT 2000
164
addy ke64e36822014-07-01 09:03:59 +0800165struct rockchip_spi {
166 struct device *dev;
addy ke64e36822014-07-01 09:03:59 +0800167
168 struct clk *spiclk;
169 struct clk *apb_pclk;
170
171 void __iomem *regs;
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100172 dma_addr_t dma_addr_rx;
173 dma_addr_t dma_addr_tx;
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100174
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100175 const void *tx;
176 void *rx;
177 unsigned int tx_left;
178 unsigned int rx_left;
179
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100180 atomic_t state;
181
addy ke64e36822014-07-01 09:03:59 +0800182 /*depth of the FIFO buffer */
183 u32 fifo_len;
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100184 /* frequency of spiclk */
185 u32 freq;
addy ke64e36822014-07-01 09:03:59 +0800186
addy ke64e36822014-07-01 09:03:59 +0800187 u8 n_bytes;
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100188 u8 rsd;
addy ke64e36822014-07-01 09:03:59 +0800189
Jeffy Chenaa099382017-06-28 12:38:43 +0800190 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
Chris Ruehld065f412020-05-11 16:30:21 +0800191
192 bool slave_abort;
addy ke64e36822014-07-01 09:03:59 +0800193};
194
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100195static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
addy ke64e36822014-07-01 09:03:59 +0800196{
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100197 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
addy ke64e36822014-07-01 09:03:59 +0800198}
199
Addy Ke2df08e72014-07-11 10:08:24 +0800200static inline void wait_for_idle(struct rockchip_spi *rs)
201{
202 unsigned long timeout = jiffies + msecs_to_jiffies(5);
203
204 do {
205 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
206 return;
Doug Anderson64bc0112014-09-03 13:44:25 -0700207 } while (!time_after(jiffies, timeout));
Addy Ke2df08e72014-07-11 10:08:24 +0800208
209 dev_warn(rs->dev, "spi controller is in busy state!\n");
210}
211
addy ke64e36822014-07-01 09:03:59 +0800212static u32 get_fifo_len(struct rockchip_spi *rs)
213{
Jon Lin13a96932020-07-23 08:43:55 +0800214 u32 ver;
addy ke64e36822014-07-01 09:03:59 +0800215
Jon Lin13a96932020-07-23 08:43:55 +0800216 ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION);
217
218 switch (ver) {
219 case ROCKCHIP_SPI_VER2_TYPE1:
220 case ROCKCHIP_SPI_VER2_TYPE2:
221 return 64;
222 default:
223 return 32;
addy ke64e36822014-07-01 09:03:59 +0800224 }
addy ke64e36822014-07-01 09:03:59 +0800225}
226
addy ke64e36822014-07-01 09:03:59 +0800227static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
228{
Chris Ruehld66571a2020-05-11 16:30:20 +0800229 struct spi_controller *ctlr = spi->controller;
230 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Jeffy Chenaa099382017-06-28 12:38:43 +0800231 bool cs_asserted = !enable;
Huibin Hongb920cc32016-02-24 18:00:04 +0800232
Jeffy Chenaa099382017-06-28 12:38:43 +0800233 /* Return immediately for no-op */
234 if (cs_asserted == rs->cs_asserted[spi->chip_select])
235 return;
addy ke64e36822014-07-01 09:03:59 +0800236
Jeffy Chenaa099382017-06-28 12:38:43 +0800237 if (cs_asserted) {
238 /* Keep things powered as long as CS is asserted */
239 pm_runtime_get_sync(rs->dev);
addy ke64e36822014-07-01 09:03:59 +0800240
Jeffy Chenaa099382017-06-28 12:38:43 +0800241 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
242 BIT(spi->chip_select));
243 } else {
244 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
245 BIT(spi->chip_select));
addy ke64e36822014-07-01 09:03:59 +0800246
Jeffy Chenaa099382017-06-28 12:38:43 +0800247 /* Drop reference from when we first asserted CS */
248 pm_runtime_put(rs->dev);
249 }
Huibin Hongb920cc32016-02-24 18:00:04 +0800250
Jeffy Chenaa099382017-06-28 12:38:43 +0800251 rs->cs_asserted[spi->chip_select] = cs_asserted;
addy ke64e36822014-07-01 09:03:59 +0800252}
253
Chris Ruehld66571a2020-05-11 16:30:20 +0800254static void rockchip_spi_handle_err(struct spi_controller *ctlr,
Andy Shevchenko22917932015-02-27 17:34:16 +0200255 struct spi_message *msg)
addy ke64e36822014-07-01 09:03:59 +0800256{
Chris Ruehld66571a2020-05-11 16:30:20 +0800257 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800258
Emil Renner Berthingce386102018-10-31 11:57:02 +0100259 /* stop running spi transfer
260 * this also flushes both rx and tx fifos
Addy Ke5dcc44e2014-07-11 10:07:56 +0800261 */
Emil Renner Berthingce386102018-10-31 11:57:02 +0100262 spi_enable_chip(rs, false);
263
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100264 /* make sure all interrupts are masked */
265 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
266
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100267 if (atomic_read(&rs->state) & TXDMA)
Chris Ruehld66571a2020-05-11 16:30:20 +0800268 dmaengine_terminate_async(ctlr->dma_tx);
addy ke64e36822014-07-01 09:03:59 +0800269
Emil Renner Berthingce386102018-10-31 11:57:02 +0100270 if (atomic_read(&rs->state) & RXDMA)
Chris Ruehld66571a2020-05-11 16:30:20 +0800271 dmaengine_terminate_async(ctlr->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800272}
273
274static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
275{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100276 u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
277 u32 words = min(rs->tx_left, tx_free);
addy ke64e36822014-07-01 09:03:59 +0800278
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100279 rs->tx_left -= words;
280 for (; words; words--) {
281 u32 txw;
282
addy ke64e36822014-07-01 09:03:59 +0800283 if (rs->n_bytes == 1)
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100284 txw = *(u8 *)rs->tx;
addy ke64e36822014-07-01 09:03:59 +0800285 else
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100286 txw = *(u16 *)rs->tx;
addy ke64e36822014-07-01 09:03:59 +0800287
288 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
289 rs->tx += rs->n_bytes;
290 }
291}
292
293static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
294{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100295 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
Jon Lin4294e4a2020-07-23 08:43:56 +0800296 u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
addy ke64e36822014-07-01 09:03:59 +0800297
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100298 /* the hardware doesn't allow us to change fifo threshold
299 * level while spi is enabled, so instead make sure to leave
300 * enough words in the rx fifo to get the last interrupt
301 * exactly when all words have been received
302 */
303 if (rx_left) {
304 u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
305
306 if (rx_left < ftl) {
307 rx_left = ftl;
308 words = rs->rx_left - rx_left;
309 }
310 }
311
312 rs->rx_left = rx_left;
313 for (; words; words--) {
314 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
315
316 if (!rs->rx)
317 continue;
318
addy ke64e36822014-07-01 09:03:59 +0800319 if (rs->n_bytes == 1)
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100320 *(u8 *)rs->rx = (u8)rxw;
addy ke64e36822014-07-01 09:03:59 +0800321 else
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100322 *(u16 *)rs->rx = (u16)rxw;
addy ke64e36822014-07-01 09:03:59 +0800323 rs->rx += rs->n_bytes;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800324 }
addy ke64e36822014-07-01 09:03:59 +0800325}
326
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100327static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
addy ke64e36822014-07-01 09:03:59 +0800328{
Chris Ruehld66571a2020-05-11 16:30:20 +0800329 struct spi_controller *ctlr = dev_id;
330 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800331
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100332 if (rs->tx_left)
333 rockchip_spi_pio_writer(rs);
334
335 rockchip_spi_pio_reader(rs);
336 if (!rs->rx_left) {
337 spi_enable_chip(rs, false);
338 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
Chris Ruehld66571a2020-05-11 16:30:20 +0800339 spi_finalize_current_transfer(ctlr);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100340 }
341
342 return IRQ_HANDLED;
343}
344
345static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
346 struct spi_transfer *xfer)
347{
348 rs->tx = xfer->tx_buf;
349 rs->rx = xfer->rx_buf;
350 rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
351 rs->rx_left = xfer->len / rs->n_bytes;
352
353 writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100354 spi_enable_chip(rs, true);
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200355
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100356 if (rs->tx_left)
357 rockchip_spi_pio_writer(rs);
addy ke64e36822014-07-01 09:03:59 +0800358
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100359 /* 1 means the transfer is in progress */
360 return 1;
addy ke64e36822014-07-01 09:03:59 +0800361}
362
363static void rockchip_spi_dma_rxcb(void *data)
364{
Chris Ruehld66571a2020-05-11 16:30:20 +0800365 struct spi_controller *ctlr = data;
366 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100367 int state = atomic_fetch_andnot(RXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800368
Chris Ruehld065f412020-05-11 16:30:21 +0800369 if (state & TXDMA && !rs->slave_abort)
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100370 return;
addy ke64e36822014-07-01 09:03:59 +0800371
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100372 spi_enable_chip(rs, false);
Chris Ruehld66571a2020-05-11 16:30:20 +0800373 spi_finalize_current_transfer(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800374}
375
376static void rockchip_spi_dma_txcb(void *data)
377{
Chris Ruehld66571a2020-05-11 16:30:20 +0800378 struct spi_controller *ctlr = data;
379 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100380 int state = atomic_fetch_andnot(TXDMA, &rs->state);
381
Chris Ruehld065f412020-05-11 16:30:21 +0800382 if (state & RXDMA && !rs->slave_abort)
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100383 return;
addy ke64e36822014-07-01 09:03:59 +0800384
Addy Ke2df08e72014-07-11 10:08:24 +0800385 /* Wait until the FIFO data completely. */
386 wait_for_idle(rs);
387
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100388 spi_enable_chip(rs, false);
Chris Ruehld66571a2020-05-11 16:30:20 +0800389 spi_finalize_current_transfer(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800390}
391
Jon Lin4d9ca632020-07-23 08:43:54 +0800392static u32 rockchip_spi_calc_burst_size(u32 data_len)
393{
394 u32 i;
395
396 /* burst size: 1, 2, 4, 8 */
397 for (i = 1; i < 8; i <<= 1) {
398 if (data_len & i)
399 break;
400 }
401
402 return i;
403}
404
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100405static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
Chris Ruehld66571a2020-05-11 16:30:20 +0800406 struct spi_controller *ctlr, struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800407{
addy ke64e36822014-07-01 09:03:59 +0800408 struct dma_async_tx_descriptor *rxdesc, *txdesc;
409
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100410 atomic_set(&rs->state, 0);
addy ke64e36822014-07-01 09:03:59 +0800411
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100412 rxdesc = NULL;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100413 if (xfer->rx_buf) {
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100414 struct dma_slave_config rxconf = {
415 .direction = DMA_DEV_TO_MEM,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100416 .src_addr = rs->dma_addr_rx,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100417 .src_addr_width = rs->n_bytes,
Jon Lin4d9ca632020-07-23 08:43:54 +0800418 .src_maxburst = rockchip_spi_calc_burst_size(xfer->len /
419 rs->n_bytes),
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100420 };
421
Chris Ruehld66571a2020-05-11 16:30:20 +0800422 dmaengine_slave_config(ctlr->dma_rx, &rxconf);
addy ke64e36822014-07-01 09:03:59 +0800423
Addy Ke5dcc44e2014-07-11 10:07:56 +0800424 rxdesc = dmaengine_prep_slave_sg(
Chris Ruehld66571a2020-05-11 16:30:20 +0800425 ctlr->dma_rx,
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100426 xfer->rx_sg.sgl, xfer->rx_sg.nents,
Emil Renner Berthingd9071b72018-10-10 11:00:37 +0200427 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800428 if (!rxdesc)
429 return -EINVAL;
addy ke64e36822014-07-01 09:03:59 +0800430
431 rxdesc->callback = rockchip_spi_dma_rxcb;
Chris Ruehld66571a2020-05-11 16:30:20 +0800432 rxdesc->callback_param = ctlr;
addy ke64e36822014-07-01 09:03:59 +0800433 }
434
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100435 txdesc = NULL;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100436 if (xfer->tx_buf) {
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100437 struct dma_slave_config txconf = {
438 .direction = DMA_MEM_TO_DEV,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100439 .dst_addr = rs->dma_addr_tx,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100440 .dst_addr_width = rs->n_bytes,
Emil Renner Berthing47300722019-04-12 12:53:20 +0200441 .dst_maxburst = rs->fifo_len / 4,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100442 };
443
Chris Ruehld66571a2020-05-11 16:30:20 +0800444 dmaengine_slave_config(ctlr->dma_tx, &txconf);
addy ke64e36822014-07-01 09:03:59 +0800445
Addy Ke5dcc44e2014-07-11 10:07:56 +0800446 txdesc = dmaengine_prep_slave_sg(
Chris Ruehld66571a2020-05-11 16:30:20 +0800447 ctlr->dma_tx,
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100448 xfer->tx_sg.sgl, xfer->tx_sg.nents,
Emil Renner Berthingd9071b72018-10-10 11:00:37 +0200449 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800450 if (!txdesc) {
451 if (rxdesc)
Chris Ruehld66571a2020-05-11 16:30:20 +0800452 dmaengine_terminate_sync(ctlr->dma_rx);
Shawn Linea984912016-03-09 16:11:15 +0800453 return -EINVAL;
454 }
addy ke64e36822014-07-01 09:03:59 +0800455
456 txdesc->callback = rockchip_spi_dma_txcb;
Chris Ruehld66571a2020-05-11 16:30:20 +0800457 txdesc->callback_param = ctlr;
addy ke64e36822014-07-01 09:03:59 +0800458 }
459
460 /* rx must be started before tx due to spi instinct */
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100461 if (rxdesc) {
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100462 atomic_or(RXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800463 dmaengine_submit(rxdesc);
Chris Ruehld66571a2020-05-11 16:30:20 +0800464 dma_async_issue_pending(ctlr->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800465 }
466
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100467 spi_enable_chip(rs, true);
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200468
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100469 if (txdesc) {
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100470 atomic_or(TXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800471 dmaengine_submit(txdesc);
Chris Ruehld66571a2020-05-11 16:30:20 +0800472 dma_async_issue_pending(ctlr->dma_tx);
addy ke64e36822014-07-01 09:03:59 +0800473 }
Shawn Linea984912016-03-09 16:11:15 +0800474
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200475 /* 1 means the transfer is in progress */
476 return 1;
addy ke64e36822014-07-01 09:03:59 +0800477}
478
Arnd Bergmanne5098952021-02-26 15:00:48 +0100479static int rockchip_spi_config(struct rockchip_spi *rs,
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100480 struct spi_device *spi, struct spi_transfer *xfer,
Chris Ruehld065f412020-05-11 16:30:21 +0800481 bool use_dma, bool slave_mode)
addy ke64e36822014-07-01 09:03:59 +0800482{
Emil Renner Berthing2410d6a2018-10-31 11:57:00 +0100483 u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
Jay Fang02621792021-03-24 14:16:41 +0800484 | CR0_BHT_8BIT << CR0_BHT_OFFSET
485 | CR0_SSD_ONE << CR0_SSD_OFFSET
486 | CR0_EM_BIG << CR0_EM_OFFSET;
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100487 u32 cr1;
488 u32 dmacr = 0;
addy ke64e36822014-07-01 09:03:59 +0800489
Chris Ruehld065f412020-05-11 16:30:21 +0800490 if (slave_mode)
491 cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET;
492 rs->slave_abort = false;
493
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100494 cr0 |= rs->rsd << CR0_RSD_OFFSET;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100495 cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
Emil Renner Berthing04290192018-10-31 11:57:11 +0100496 if (spi->mode & SPI_LSB_FIRST)
497 cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100498
499 if (xfer->rx_buf && xfer->tx_buf)
500 cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
501 else if (xfer->rx_buf)
502 cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100503 else if (use_dma)
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100504 cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
addy ke64e36822014-07-01 09:03:59 +0800505
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100506 switch (xfer->bits_per_word) {
507 case 4:
508 cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
509 cr1 = xfer->len - 1;
510 break;
511 case 8:
512 cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
513 cr1 = xfer->len - 1;
514 break;
515 case 16:
516 cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
517 cr1 = xfer->len / 2 - 1;
518 break;
519 default:
520 /* we only whitelist 4, 8 and 16 bit words in
Chris Ruehld66571a2020-05-11 16:30:20 +0800521 * ctlr->bits_per_word_mask, so this shouldn't
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100522 * happen
523 */
Arnd Bergmanne5098952021-02-26 15:00:48 +0100524 dev_err(rs->dev, "unknown bits per word: %d\n",
525 xfer->bits_per_word);
526 return -EINVAL;
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100527 }
528
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100529 if (use_dma) {
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100530 if (xfer->tx_buf)
addy ke64e36822014-07-01 09:03:59 +0800531 dmacr |= TF_DMA_EN;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100532 if (xfer->rx_buf)
addy ke64e36822014-07-01 09:03:59 +0800533 dmacr |= RF_DMA_EN;
534 }
535
addy ke64e36822014-07-01 09:03:59 +0800536 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100537 writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
Huibin Hong04b37d22017-08-16 10:12:02 +0800538
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100539 /* unfortunately setting the fifo threshold level to generate an
540 * interrupt exactly when the fifo is full doesn't seem to work,
541 * so we need the strict inequality here
542 */
Jon Lin4a47fcd2021-06-21 18:47:57 +0800543 if ((xfer->len / rs->n_bytes) < rs->fifo_len)
544 writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100545 else
546 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
addy ke64e36822014-07-01 09:03:59 +0800547
Emil Renner Berthing47300722019-04-12 12:53:20 +0200548 writel_relaxed(rs->fifo_len / 2, rs->regs + ROCKCHIP_SPI_DMATDLR);
Jon Lin4d9ca632020-07-23 08:43:54 +0800549 writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1,
550 rs->regs + ROCKCHIP_SPI_DMARDLR);
addy ke64e36822014-07-01 09:03:59 +0800551 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
552
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100553 /* the hardware only supports an even clock divisor, so
554 * round divisor = spiclk / speed up to nearest even number
555 * so that the resulting speed is <= the requested speed
556 */
557 writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
558 rs->regs + ROCKCHIP_SPI_BAUDR);
Arnd Bergmanne5098952021-02-26 15:00:48 +0100559
560 return 0;
addy ke64e36822014-07-01 09:03:59 +0800561}
562
Brian Norris5185a812016-07-14 18:30:59 -0700563static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
564{
565 return ROCKCHIP_SPI_MAX_TRANLEN;
566}
567
Chris Ruehld065f412020-05-11 16:30:21 +0800568static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
569{
570 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
571
572 rs->slave_abort = true;
Vincent Pelletier6bd2c8672021-01-07 23:58:32 +0000573 spi_finalize_current_transfer(ctlr);
Chris Ruehld065f412020-05-11 16:30:21 +0800574
575 return 0;
576}
577
Addy Ke5dcc44e2014-07-11 10:07:56 +0800578static int rockchip_spi_transfer_one(
Chris Ruehld66571a2020-05-11 16:30:20 +0800579 struct spi_controller *ctlr,
addy ke64e36822014-07-01 09:03:59 +0800580 struct spi_device *spi,
581 struct spi_transfer *xfer)
582{
Chris Ruehld66571a2020-05-11 16:30:20 +0800583 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Arnd Bergmanne5098952021-02-26 15:00:48 +0100584 int ret;
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100585 bool use_dma;
addy ke64e36822014-07-01 09:03:59 +0800586
Doug Anderson62946172014-09-03 13:44:26 -0700587 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
588 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
addy ke64e36822014-07-01 09:03:59 +0800589
590 if (!xfer->tx_buf && !xfer->rx_buf) {
591 dev_err(rs->dev, "No buffer for transfer\n");
592 return -EINVAL;
593 }
594
Brian Norris5185a812016-07-14 18:30:59 -0700595 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
596 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
597 return -EINVAL;
598 }
599
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100600 rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
addy ke64e36822014-07-01 09:03:59 +0800601
Chris Ruehld66571a2020-05-11 16:30:20 +0800602 use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
addy ke64e36822014-07-01 09:03:59 +0800603
Arnd Bergmanne5098952021-02-26 15:00:48 +0100604 ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
605 if (ret)
606 return ret;
addy ke64e36822014-07-01 09:03:59 +0800607
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100608 if (use_dma)
Chris Ruehld66571a2020-05-11 16:30:20 +0800609 return rockchip_spi_prepare_dma(rs, ctlr, xfer);
addy ke64e36822014-07-01 09:03:59 +0800610
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100611 return rockchip_spi_prepare_irq(rs, xfer);
addy ke64e36822014-07-01 09:03:59 +0800612}
613
Chris Ruehld66571a2020-05-11 16:30:20 +0800614static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
Addy Ke5dcc44e2014-07-11 10:07:56 +0800615 struct spi_device *spi,
616 struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800617{
Chris Ruehld66571a2020-05-11 16:30:20 +0800618 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100619 unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
addy ke64e36822014-07-01 09:03:59 +0800620
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100621 /* if the numbor of spi words to transfer is less than the fifo
622 * length we can just fill the fifo and wait for a single irq,
623 * so don't bother setting up dma
624 */
625 return xfer->len / bytes_per_word >= rs->fifo_len;
addy ke64e36822014-07-01 09:03:59 +0800626}
627
628static int rockchip_spi_probe(struct platform_device *pdev)
629{
Jeffy Chen43de9792017-08-07 20:40:18 +0800630 int ret;
addy ke64e36822014-07-01 09:03:59 +0800631 struct rockchip_spi *rs;
Chris Ruehld66571a2020-05-11 16:30:20 +0800632 struct spi_controller *ctlr;
addy ke64e36822014-07-01 09:03:59 +0800633 struct resource *mem;
Chris Ruehld065f412020-05-11 16:30:21 +0800634 struct device_node *np = pdev->dev.of_node;
Julius Werner76b17e62015-03-26 16:30:25 -0700635 u32 rsd_nsecs;
Chris Ruehld065f412020-05-11 16:30:21 +0800636 bool slave_mode;
addy ke64e36822014-07-01 09:03:59 +0800637
Chris Ruehld065f412020-05-11 16:30:21 +0800638 slave_mode = of_property_read_bool(np, "spi-slave");
639
640 if (slave_mode)
641 ctlr = spi_alloc_slave(&pdev->dev,
642 sizeof(struct rockchip_spi));
643 else
644 ctlr = spi_alloc_master(&pdev->dev,
645 sizeof(struct rockchip_spi));
646
Chris Ruehld66571a2020-05-11 16:30:20 +0800647 if (!ctlr)
addy ke64e36822014-07-01 09:03:59 +0800648 return -ENOMEM;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800649
Chris Ruehld66571a2020-05-11 16:30:20 +0800650 platform_set_drvdata(pdev, ctlr);
addy ke64e36822014-07-01 09:03:59 +0800651
Chris Ruehld66571a2020-05-11 16:30:20 +0800652 rs = spi_controller_get_devdata(ctlr);
Chris Ruehld065f412020-05-11 16:30:21 +0800653 ctlr->slave = slave_mode;
addy ke64e36822014-07-01 09:03:59 +0800654
655 /* Get basic io resource and map it */
656 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
657 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
658 if (IS_ERR(rs->regs)) {
addy ke64e36822014-07-01 09:03:59 +0800659 ret = PTR_ERR(rs->regs);
Chris Ruehld66571a2020-05-11 16:30:20 +0800660 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800661 }
662
663 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
664 if (IS_ERR(rs->apb_pclk)) {
665 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
666 ret = PTR_ERR(rs->apb_pclk);
Chris Ruehld66571a2020-05-11 16:30:20 +0800667 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800668 }
669
670 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
671 if (IS_ERR(rs->spiclk)) {
672 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
673 ret = PTR_ERR(rs->spiclk);
Chris Ruehld66571a2020-05-11 16:30:20 +0800674 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800675 }
676
677 ret = clk_prepare_enable(rs->apb_pclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800678 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800679 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
Chris Ruehld66571a2020-05-11 16:30:20 +0800680 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800681 }
682
683 ret = clk_prepare_enable(rs->spiclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800684 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800685 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800686 goto err_disable_apbclk;
addy ke64e36822014-07-01 09:03:59 +0800687 }
688
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100689 spi_enable_chip(rs, false);
addy ke64e36822014-07-01 09:03:59 +0800690
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100691 ret = platform_get_irq(pdev, 0);
692 if (ret < 0)
693 goto err_disable_spiclk;
694
695 ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
Chris Ruehld66571a2020-05-11 16:30:20 +0800696 IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100697 if (ret)
698 goto err_disable_spiclk;
699
addy ke64e36822014-07-01 09:03:59 +0800700 rs->dev = &pdev->dev;
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100701 rs->freq = clk_get_rate(rs->spiclk);
addy ke64e36822014-07-01 09:03:59 +0800702
Julius Werner76b17e62015-03-26 16:30:25 -0700703 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100704 &rsd_nsecs)) {
705 /* rx sample delay is expressed in parent clock cycles (max 3) */
706 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
707 1000000000 >> 8);
708 if (!rsd) {
709 dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
710 rs->freq, rsd_nsecs);
711 } else if (rsd > CR0_RSD_MAX) {
712 rsd = CR0_RSD_MAX;
713 dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
714 rs->freq, rsd_nsecs,
715 CR0_RSD_MAX * 1000000000U / rs->freq);
716 }
717 rs->rsd = rsd;
718 }
Julius Werner76b17e62015-03-26 16:30:25 -0700719
addy ke64e36822014-07-01 09:03:59 +0800720 rs->fifo_len = get_fifo_len(rs);
721 if (!rs->fifo_len) {
722 dev_err(&pdev->dev, "Failed to get fifo length\n");
Wei Yongjundb7e8d92014-07-20 22:02:04 +0800723 ret = -EINVAL;
Jeffy Chenc3515872017-06-13 13:25:40 +0800724 goto err_disable_spiclk;
addy ke64e36822014-07-01 09:03:59 +0800725 }
726
Alexander Kochetkov940f3bb2020-10-16 11:50:14 +0300727 pm_runtime_set_autosuspend_delay(&pdev->dev, ROCKCHIP_AUTOSUSPEND_TIMEOUT);
728 pm_runtime_use_autosuspend(&pdev->dev);
addy ke64e36822014-07-01 09:03:59 +0800729 pm_runtime_set_active(&pdev->dev);
730 pm_runtime_enable(&pdev->dev);
731
Chris Ruehld66571a2020-05-11 16:30:20 +0800732 ctlr->auto_runtime_pm = true;
733 ctlr->bus_num = pdev->id;
734 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
Chris Ruehld065f412020-05-11 16:30:21 +0800735 if (slave_mode) {
736 ctlr->mode_bits |= SPI_NO_CS;
737 ctlr->slave_abort = rockchip_spi_slave_abort;
738 } else {
739 ctlr->flags = SPI_MASTER_GPIO_SS;
Chris Ruehleb1262e2020-05-11 16:30:22 +0800740 ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM;
741 /*
742 * rk spi0 has two native cs, spi1..5 one cs only
743 * if num-cs is missing in the dts, default to 1
744 */
745 if (of_property_read_u16(np, "num-cs", &ctlr->num_chipselect))
746 ctlr->num_chipselect = 1;
747 ctlr->use_gpio_descriptors = true;
Chris Ruehld065f412020-05-11 16:30:21 +0800748 }
Chris Ruehld66571a2020-05-11 16:30:20 +0800749 ctlr->dev.of_node = pdev->dev.of_node;
750 ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
751 ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
752 ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
addy ke64e36822014-07-01 09:03:59 +0800753
Chris Ruehld66571a2020-05-11 16:30:20 +0800754 ctlr->set_cs = rockchip_spi_set_cs;
755 ctlr->transfer_one = rockchip_spi_transfer_one;
756 ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
757 ctlr->handle_err = rockchip_spi_handle_err;
addy ke64e36822014-07-01 09:03:59 +0800758
Chris Ruehld66571a2020-05-11 16:30:20 +0800759 ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
760 if (IS_ERR(ctlr->dma_tx)) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800761 /* Check tx to see if we need defer probing driver */
Chris Ruehld66571a2020-05-11 16:30:20 +0800762 if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800763 ret = -EPROBE_DEFER;
Jeffy Chenc3515872017-06-13 13:25:40 +0800764 goto err_disable_pm_runtime;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800765 }
addy ke64e36822014-07-01 09:03:59 +0800766 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
Chris Ruehld66571a2020-05-11 16:30:20 +0800767 ctlr->dma_tx = NULL;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800768 }
addy ke64e36822014-07-01 09:03:59 +0800769
Chris Ruehld66571a2020-05-11 16:30:20 +0800770 ctlr->dma_rx = dma_request_chan(rs->dev, "rx");
771 if (IS_ERR(ctlr->dma_rx)) {
772 if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) {
Shawn Line4c0e062016-03-31 11:11:41 +0800773 ret = -EPROBE_DEFER;
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300774 goto err_free_dma_tx;
addy ke64e36822014-07-01 09:03:59 +0800775 }
776 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
Chris Ruehld66571a2020-05-11 16:30:20 +0800777 ctlr->dma_rx = NULL;
addy ke64e36822014-07-01 09:03:59 +0800778 }
779
Chris Ruehld66571a2020-05-11 16:30:20 +0800780 if (ctlr->dma_tx && ctlr->dma_rx) {
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100781 rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
782 rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
Chris Ruehld66571a2020-05-11 16:30:20 +0800783 ctlr->can_dma = rockchip_spi_can_dma;
addy ke64e36822014-07-01 09:03:59 +0800784 }
785
Chris Ruehld66571a2020-05-11 16:30:20 +0800786 ret = devm_spi_register_controller(&pdev->dev, ctlr);
Jeffy Chen43de9792017-08-07 20:40:18 +0800787 if (ret < 0) {
Chris Ruehld66571a2020-05-11 16:30:20 +0800788 dev_err(&pdev->dev, "Failed to register controller\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800789 goto err_free_dma_rx;
addy ke64e36822014-07-01 09:03:59 +0800790 }
791
addy ke64e36822014-07-01 09:03:59 +0800792 return 0;
793
Jeffy Chenc3515872017-06-13 13:25:40 +0800794err_free_dma_rx:
Chris Ruehld66571a2020-05-11 16:30:20 +0800795 if (ctlr->dma_rx)
796 dma_release_channel(ctlr->dma_rx);
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300797err_free_dma_tx:
Chris Ruehld66571a2020-05-11 16:30:20 +0800798 if (ctlr->dma_tx)
799 dma_release_channel(ctlr->dma_tx);
Jeffy Chenc3515872017-06-13 13:25:40 +0800800err_disable_pm_runtime:
801 pm_runtime_disable(&pdev->dev);
802err_disable_spiclk:
addy ke64e36822014-07-01 09:03:59 +0800803 clk_disable_unprepare(rs->spiclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800804err_disable_apbclk:
addy ke64e36822014-07-01 09:03:59 +0800805 clk_disable_unprepare(rs->apb_pclk);
Chris Ruehld66571a2020-05-11 16:30:20 +0800806err_put_ctlr:
807 spi_controller_put(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800808
809 return ret;
810}
811
812static int rockchip_spi_remove(struct platform_device *pdev)
813{
Chris Ruehld66571a2020-05-11 16:30:20 +0800814 struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev));
815 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800816
Jeffy Chen6a06e892017-08-07 20:40:19 +0800817 pm_runtime_get_sync(&pdev->dev);
addy ke64e36822014-07-01 09:03:59 +0800818
819 clk_disable_unprepare(rs->spiclk);
820 clk_disable_unprepare(rs->apb_pclk);
821
Jeffy Chen6a06e892017-08-07 20:40:19 +0800822 pm_runtime_put_noidle(&pdev->dev);
823 pm_runtime_disable(&pdev->dev);
824 pm_runtime_set_suspended(&pdev->dev);
825
Chris Ruehld66571a2020-05-11 16:30:20 +0800826 if (ctlr->dma_tx)
827 dma_release_channel(ctlr->dma_tx);
828 if (ctlr->dma_rx)
829 dma_release_channel(ctlr->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800830
Chris Ruehld66571a2020-05-11 16:30:20 +0800831 spi_controller_put(ctlr);
Shawn Lin844c9f42016-02-15 16:28:12 +0800832
addy ke64e36822014-07-01 09:03:59 +0800833 return 0;
834}
835
836#ifdef CONFIG_PM_SLEEP
837static int rockchip_spi_suspend(struct device *dev)
838{
Jeffy Chen43de9792017-08-07 20:40:18 +0800839 int ret;
Chris Ruehld66571a2020-05-11 16:30:20 +0800840 struct spi_controller *ctlr = dev_get_drvdata(dev);
addy ke64e36822014-07-01 09:03:59 +0800841
Chris Ruehld66571a2020-05-11 16:30:20 +0800842 ret = spi_controller_suspend(ctlr);
Jeffy Chen43de9792017-08-07 20:40:18 +0800843 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800844 return ret;
845
Jeffy Chend38c4ae12017-08-07 20:40:20 +0800846 ret = pm_runtime_force_suspend(dev);
847 if (ret < 0)
848 return ret;
addy ke64e36822014-07-01 09:03:59 +0800849
Brian Norris23e291c2016-12-16 16:59:16 -0800850 pinctrl_pm_select_sleep_state(dev);
851
Jeffy Chen43de9792017-08-07 20:40:18 +0800852 return 0;
addy ke64e36822014-07-01 09:03:59 +0800853}
854
855static int rockchip_spi_resume(struct device *dev)
856{
Jeffy Chen43de9792017-08-07 20:40:18 +0800857 int ret;
Chris Ruehld66571a2020-05-11 16:30:20 +0800858 struct spi_controller *ctlr = dev_get_drvdata(dev);
859 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800860
Brian Norris23e291c2016-12-16 16:59:16 -0800861 pinctrl_pm_select_default_state(dev);
862
Jeffy Chend38c4ae12017-08-07 20:40:20 +0800863 ret = pm_runtime_force_resume(dev);
864 if (ret < 0)
865 return ret;
addy ke64e36822014-07-01 09:03:59 +0800866
Chris Ruehld66571a2020-05-11 16:30:20 +0800867 ret = spi_controller_resume(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800868 if (ret < 0) {
869 clk_disable_unprepare(rs->spiclk);
870 clk_disable_unprepare(rs->apb_pclk);
871 }
872
Jeffy Chen43de9792017-08-07 20:40:18 +0800873 return 0;
addy ke64e36822014-07-01 09:03:59 +0800874}
875#endif /* CONFIG_PM_SLEEP */
876
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100877#ifdef CONFIG_PM
addy ke64e36822014-07-01 09:03:59 +0800878static int rockchip_spi_runtime_suspend(struct device *dev)
879{
Chris Ruehld66571a2020-05-11 16:30:20 +0800880 struct spi_controller *ctlr = dev_get_drvdata(dev);
881 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800882
883 clk_disable_unprepare(rs->spiclk);
884 clk_disable_unprepare(rs->apb_pclk);
885
886 return 0;
887}
888
889static int rockchip_spi_runtime_resume(struct device *dev)
890{
891 int ret;
Chris Ruehld66571a2020-05-11 16:30:20 +0800892 struct spi_controller *ctlr = dev_get_drvdata(dev);
893 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800894
895 ret = clk_prepare_enable(rs->apb_pclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800896 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800897 return ret;
898
899 ret = clk_prepare_enable(rs->spiclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800900 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800901 clk_disable_unprepare(rs->apb_pclk);
902
Jeffy Chen43de9792017-08-07 20:40:18 +0800903 return 0;
addy ke64e36822014-07-01 09:03:59 +0800904}
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100905#endif /* CONFIG_PM */
addy ke64e36822014-07-01 09:03:59 +0800906
907static const struct dev_pm_ops rockchip_spi_pm = {
908 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
909 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
910 rockchip_spi_runtime_resume, NULL)
911};
912
913static const struct of_device_id rockchip_spi_dt_match[] = {
Johan Jonkerc6486ea2020-03-09 16:10:03 +0100914 { .compatible = "rockchip,px30-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800915 { .compatible = "rockchip,rk3036-spi", },
addy ke64e36822014-07-01 09:03:59 +0800916 { .compatible = "rockchip,rk3066-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800917 { .compatible = "rockchip,rk3188-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800918 { .compatible = "rockchip,rk3228-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800919 { .compatible = "rockchip,rk3288-spi", },
Johan Jonkerc6486ea2020-03-09 16:10:03 +0100920 { .compatible = "rockchip,rk3308-spi", },
921 { .compatible = "rockchip,rk3328-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800922 { .compatible = "rockchip,rk3368-spi", },
Xu Jianqun9b7a5622016-02-18 19:16:31 +0800923 { .compatible = "rockchip,rk3399-spi", },
Johan Jonkerc6486ea2020-03-09 16:10:03 +0100924 { .compatible = "rockchip,rv1108-spi", },
Jon Lin0f4f58b2021-06-21 18:47:56 +0800925 { .compatible = "rockchip,rv1126-spi", },
addy ke64e36822014-07-01 09:03:59 +0800926 { },
927};
928MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
929
930static struct platform_driver rockchip_spi_driver = {
931 .driver = {
932 .name = DRIVER_NAME,
addy ke64e36822014-07-01 09:03:59 +0800933 .pm = &rockchip_spi_pm,
934 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
935 },
936 .probe = rockchip_spi_probe,
937 .remove = rockchip_spi_remove,
938};
939
940module_platform_driver(rockchip_spi_driver);
941
Addy Ke5dcc44e2014-07-11 10:07:56 +0800942MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
addy ke64e36822014-07-01 09:03:59 +0800943MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
944MODULE_LICENSE("GPL v2");