blob: 553b6b9d02222ba835f468ec32acdd3ccd60155c [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
Jon Lin736b81e2021-06-21 18:48:48 +0800110#define CR0_SOI_OFFSET 23
111
addy ke64e36822014-07-01 09:03:59 +0800112#define CR0_MTM_OFFSET 0x21
113
114/* Bit fields in SER, 2bit */
115#define SER_MASK 0x3
116
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100117/* Bit fields in BAUDR */
118#define BAUDR_SCKDV_MIN 2
119#define BAUDR_SCKDV_MAX 65534
120
Jon Lin2758bd02021-06-21 18:47:58 +0800121/* Bit fields in SR, 6bit */
122#define SR_MASK 0x3f
addy ke64e36822014-07-01 09:03:59 +0800123#define SR_BUSY (1 << 0)
124#define SR_TF_FULL (1 << 1)
125#define SR_TF_EMPTY (1 << 2)
126#define SR_RF_EMPTY (1 << 3)
127#define SR_RF_FULL (1 << 4)
Jon Lin2758bd02021-06-21 18:47:58 +0800128#define SR_SLAVE_TX_BUSY (1 << 5)
addy ke64e36822014-07-01 09:03:59 +0800129
130/* Bit fields in ISR, IMR, ISR, RISR, 5bit */
131#define INT_MASK 0x1f
132#define INT_TF_EMPTY (1 << 0)
133#define INT_TF_OVERFLOW (1 << 1)
134#define INT_RF_UNDERFLOW (1 << 2)
135#define INT_RF_OVERFLOW (1 << 3)
136#define INT_RF_FULL (1 << 4)
137
138/* Bit fields in ICR, 4bit */
139#define ICR_MASK 0x0f
140#define ICR_ALL (1 << 0)
141#define ICR_RF_UNDERFLOW (1 << 1)
142#define ICR_RF_OVERFLOW (1 << 2)
143#define ICR_TF_OVERFLOW (1 << 3)
144
145/* Bit fields in DMACR */
146#define RF_DMA_EN (1 << 0)
147#define TF_DMA_EN (1 << 1)
148
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100149/* Driver state flags */
150#define RXDMA (1 << 0)
151#define TXDMA (1 << 1)
addy ke64e36822014-07-01 09:03:59 +0800152
Addy Kef9cfd522014-10-15 19:25:49 +0800153/* sclk_out: spi master internal logic in rk3x can support 50Mhz */
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100154#define MAX_SCLK_OUT 50000000U
Addy Kef9cfd522014-10-15 19:25:49 +0800155
Brian Norris5185a812016-07-14 18:30:59 -0700156/*
157 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
158 * the controller seems to hang when given 0x10000, so stick with this for now.
159 */
160#define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
161
Jon Linb8d42372021-06-21 18:48:47 +0800162/* 2 for native cs, 2 for cs-gpio */
163#define ROCKCHIP_SPI_MAX_CS_NUM 4
Jon Lin13a96932020-07-23 08:43:55 +0800164#define ROCKCHIP_SPI_VER2_TYPE1 0x05EC0002
165#define ROCKCHIP_SPI_VER2_TYPE2 0x00110002
Jeffy Chenaa099382017-06-28 12:38:43 +0800166
Alexander Kochetkov940f3bb2020-10-16 11:50:14 +0300167#define ROCKCHIP_AUTOSUSPEND_TIMEOUT 2000
168
addy ke64e36822014-07-01 09:03:59 +0800169struct rockchip_spi {
170 struct device *dev;
addy ke64e36822014-07-01 09:03:59 +0800171
172 struct clk *spiclk;
173 struct clk *apb_pclk;
174
175 void __iomem *regs;
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100176 dma_addr_t dma_addr_rx;
177 dma_addr_t dma_addr_tx;
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100178
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100179 const void *tx;
180 void *rx;
181 unsigned int tx_left;
182 unsigned int rx_left;
183
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100184 atomic_t state;
185
addy ke64e36822014-07-01 09:03:59 +0800186 /*depth of the FIFO buffer */
187 u32 fifo_len;
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100188 /* frequency of spiclk */
189 u32 freq;
addy ke64e36822014-07-01 09:03:59 +0800190
addy ke64e36822014-07-01 09:03:59 +0800191 u8 n_bytes;
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100192 u8 rsd;
addy ke64e36822014-07-01 09:03:59 +0800193
Jeffy Chenaa099382017-06-28 12:38:43 +0800194 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
Chris Ruehld065f412020-05-11 16:30:21 +0800195
196 bool slave_abort;
addy ke64e36822014-07-01 09:03:59 +0800197};
198
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100199static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
addy ke64e36822014-07-01 09:03:59 +0800200{
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100201 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
addy ke64e36822014-07-01 09:03:59 +0800202}
203
Jon Lin2758bd02021-06-21 18:47:58 +0800204static inline void wait_for_tx_idle(struct rockchip_spi *rs, bool slave_mode)
Addy Ke2df08e72014-07-11 10:08:24 +0800205{
206 unsigned long timeout = jiffies + msecs_to_jiffies(5);
207
208 do {
Jon Lin2758bd02021-06-21 18:47:58 +0800209 if (slave_mode) {
210 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_SLAVE_TX_BUSY) &&
211 !((readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)))
212 return;
213 } else {
214 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
215 return;
216 }
Doug Anderson64bc0112014-09-03 13:44:25 -0700217 } while (!time_after(jiffies, timeout));
Addy Ke2df08e72014-07-11 10:08:24 +0800218
219 dev_warn(rs->dev, "spi controller is in busy state!\n");
220}
221
addy ke64e36822014-07-01 09:03:59 +0800222static u32 get_fifo_len(struct rockchip_spi *rs)
223{
Jon Lin13a96932020-07-23 08:43:55 +0800224 u32 ver;
addy ke64e36822014-07-01 09:03:59 +0800225
Jon Lin13a96932020-07-23 08:43:55 +0800226 ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION);
227
228 switch (ver) {
229 case ROCKCHIP_SPI_VER2_TYPE1:
230 case ROCKCHIP_SPI_VER2_TYPE2:
231 return 64;
232 default:
233 return 32;
addy ke64e36822014-07-01 09:03:59 +0800234 }
addy ke64e36822014-07-01 09:03:59 +0800235}
236
addy ke64e36822014-07-01 09:03:59 +0800237static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
238{
Chris Ruehld66571a2020-05-11 16:30:20 +0800239 struct spi_controller *ctlr = spi->controller;
240 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Jon Lin736b81e2021-06-21 18:48:48 +0800241 bool cs_asserted = spi->mode & SPI_CS_HIGH ? enable : !enable;
Huibin Hongb920cc32016-02-24 18:00:04 +0800242
Jeffy Chenaa099382017-06-28 12:38:43 +0800243 /* Return immediately for no-op */
244 if (cs_asserted == rs->cs_asserted[spi->chip_select])
245 return;
addy ke64e36822014-07-01 09:03:59 +0800246
Jeffy Chenaa099382017-06-28 12:38:43 +0800247 if (cs_asserted) {
248 /* Keep things powered as long as CS is asserted */
249 pm_runtime_get_sync(rs->dev);
addy ke64e36822014-07-01 09:03:59 +0800250
Jon Linb8d42372021-06-21 18:48:47 +0800251 if (spi->cs_gpiod)
252 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
253 else
254 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
Jeffy Chenaa099382017-06-28 12:38:43 +0800255 } else {
Jon Linb8d42372021-06-21 18:48:47 +0800256 if (spi->cs_gpiod)
257 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
258 else
259 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
addy ke64e36822014-07-01 09:03:59 +0800260
Jeffy Chenaa099382017-06-28 12:38:43 +0800261 /* Drop reference from when we first asserted CS */
262 pm_runtime_put(rs->dev);
263 }
Huibin Hongb920cc32016-02-24 18:00:04 +0800264
Jeffy Chenaa099382017-06-28 12:38:43 +0800265 rs->cs_asserted[spi->chip_select] = cs_asserted;
addy ke64e36822014-07-01 09:03:59 +0800266}
267
Chris Ruehld66571a2020-05-11 16:30:20 +0800268static void rockchip_spi_handle_err(struct spi_controller *ctlr,
Andy Shevchenko22917932015-02-27 17:34:16 +0200269 struct spi_message *msg)
addy ke64e36822014-07-01 09:03:59 +0800270{
Chris Ruehld66571a2020-05-11 16:30:20 +0800271 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800272
Emil Renner Berthingce386102018-10-31 11:57:02 +0100273 /* stop running spi transfer
274 * this also flushes both rx and tx fifos
Addy Ke5dcc44e2014-07-11 10:07:56 +0800275 */
Emil Renner Berthingce386102018-10-31 11:57:02 +0100276 spi_enable_chip(rs, false);
277
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100278 /* make sure all interrupts are masked */
279 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
280
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100281 if (atomic_read(&rs->state) & TXDMA)
Chris Ruehld66571a2020-05-11 16:30:20 +0800282 dmaengine_terminate_async(ctlr->dma_tx);
addy ke64e36822014-07-01 09:03:59 +0800283
Emil Renner Berthingce386102018-10-31 11:57:02 +0100284 if (atomic_read(&rs->state) & RXDMA)
Chris Ruehld66571a2020-05-11 16:30:20 +0800285 dmaengine_terminate_async(ctlr->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800286}
287
288static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
289{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100290 u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
291 u32 words = min(rs->tx_left, tx_free);
addy ke64e36822014-07-01 09:03:59 +0800292
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100293 rs->tx_left -= words;
294 for (; words; words--) {
295 u32 txw;
296
addy ke64e36822014-07-01 09:03:59 +0800297 if (rs->n_bytes == 1)
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100298 txw = *(u8 *)rs->tx;
addy ke64e36822014-07-01 09:03:59 +0800299 else
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100300 txw = *(u16 *)rs->tx;
addy ke64e36822014-07-01 09:03:59 +0800301
302 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
303 rs->tx += rs->n_bytes;
304 }
305}
306
307static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
308{
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100309 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
Jon Lin4294e4a2020-07-23 08:43:56 +0800310 u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
addy ke64e36822014-07-01 09:03:59 +0800311
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100312 /* the hardware doesn't allow us to change fifo threshold
313 * level while spi is enabled, so instead make sure to leave
314 * enough words in the rx fifo to get the last interrupt
315 * exactly when all words have been received
316 */
317 if (rx_left) {
318 u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
319
320 if (rx_left < ftl) {
321 rx_left = ftl;
322 words = rs->rx_left - rx_left;
323 }
324 }
325
326 rs->rx_left = rx_left;
327 for (; words; words--) {
328 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
329
330 if (!rs->rx)
331 continue;
332
addy ke64e36822014-07-01 09:03:59 +0800333 if (rs->n_bytes == 1)
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100334 *(u8 *)rs->rx = (u8)rxw;
addy ke64e36822014-07-01 09:03:59 +0800335 else
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100336 *(u16 *)rs->rx = (u16)rxw;
addy ke64e36822014-07-01 09:03:59 +0800337 rs->rx += rs->n_bytes;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800338 }
addy ke64e36822014-07-01 09:03:59 +0800339}
340
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100341static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
addy ke64e36822014-07-01 09:03:59 +0800342{
Chris Ruehld66571a2020-05-11 16:30:20 +0800343 struct spi_controller *ctlr = dev_id;
344 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800345
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100346 if (rs->tx_left)
347 rockchip_spi_pio_writer(rs);
348
349 rockchip_spi_pio_reader(rs);
350 if (!rs->rx_left) {
351 spi_enable_chip(rs, false);
352 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
Chris Ruehld66571a2020-05-11 16:30:20 +0800353 spi_finalize_current_transfer(ctlr);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100354 }
355
356 return IRQ_HANDLED;
357}
358
359static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
360 struct spi_transfer *xfer)
361{
362 rs->tx = xfer->tx_buf;
363 rs->rx = xfer->rx_buf;
364 rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
365 rs->rx_left = xfer->len / rs->n_bytes;
366
367 writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100368 spi_enable_chip(rs, true);
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200369
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100370 if (rs->tx_left)
371 rockchip_spi_pio_writer(rs);
addy ke64e36822014-07-01 09:03:59 +0800372
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100373 /* 1 means the transfer is in progress */
374 return 1;
addy ke64e36822014-07-01 09:03:59 +0800375}
376
377static void rockchip_spi_dma_rxcb(void *data)
378{
Chris Ruehld66571a2020-05-11 16:30:20 +0800379 struct spi_controller *ctlr = data;
380 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100381 int state = atomic_fetch_andnot(RXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800382
Chris Ruehld065f412020-05-11 16:30:21 +0800383 if (state & TXDMA && !rs->slave_abort)
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100384 return;
addy ke64e36822014-07-01 09:03:59 +0800385
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100386 spi_enable_chip(rs, false);
Chris Ruehld66571a2020-05-11 16:30:20 +0800387 spi_finalize_current_transfer(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800388}
389
390static void rockchip_spi_dma_txcb(void *data)
391{
Chris Ruehld66571a2020-05-11 16:30:20 +0800392 struct spi_controller *ctlr = data;
393 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100394 int state = atomic_fetch_andnot(TXDMA, &rs->state);
395
Chris Ruehld065f412020-05-11 16:30:21 +0800396 if (state & RXDMA && !rs->slave_abort)
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100397 return;
addy ke64e36822014-07-01 09:03:59 +0800398
Addy Ke2df08e72014-07-11 10:08:24 +0800399 /* Wait until the FIFO data completely. */
Jon Lin2758bd02021-06-21 18:47:58 +0800400 wait_for_tx_idle(rs, ctlr->slave);
Addy Ke2df08e72014-07-11 10:08:24 +0800401
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100402 spi_enable_chip(rs, false);
Chris Ruehld66571a2020-05-11 16:30:20 +0800403 spi_finalize_current_transfer(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800404}
405
Jon Lin4d9ca632020-07-23 08:43:54 +0800406static u32 rockchip_spi_calc_burst_size(u32 data_len)
407{
408 u32 i;
409
410 /* burst size: 1, 2, 4, 8 */
411 for (i = 1; i < 8; i <<= 1) {
412 if (data_len & i)
413 break;
414 }
415
416 return i;
417}
418
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100419static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
Chris Ruehld66571a2020-05-11 16:30:20 +0800420 struct spi_controller *ctlr, struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800421{
addy ke64e36822014-07-01 09:03:59 +0800422 struct dma_async_tx_descriptor *rxdesc, *txdesc;
423
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100424 atomic_set(&rs->state, 0);
addy ke64e36822014-07-01 09:03:59 +0800425
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100426 rxdesc = NULL;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100427 if (xfer->rx_buf) {
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100428 struct dma_slave_config rxconf = {
429 .direction = DMA_DEV_TO_MEM,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100430 .src_addr = rs->dma_addr_rx,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100431 .src_addr_width = rs->n_bytes,
Jon Lin4d9ca632020-07-23 08:43:54 +0800432 .src_maxburst = rockchip_spi_calc_burst_size(xfer->len /
433 rs->n_bytes),
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100434 };
435
Chris Ruehld66571a2020-05-11 16:30:20 +0800436 dmaengine_slave_config(ctlr->dma_rx, &rxconf);
addy ke64e36822014-07-01 09:03:59 +0800437
Addy Ke5dcc44e2014-07-11 10:07:56 +0800438 rxdesc = dmaengine_prep_slave_sg(
Chris Ruehld66571a2020-05-11 16:30:20 +0800439 ctlr->dma_rx,
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100440 xfer->rx_sg.sgl, xfer->rx_sg.nents,
Emil Renner Berthingd9071b72018-10-10 11:00:37 +0200441 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800442 if (!rxdesc)
443 return -EINVAL;
addy ke64e36822014-07-01 09:03:59 +0800444
445 rxdesc->callback = rockchip_spi_dma_rxcb;
Chris Ruehld66571a2020-05-11 16:30:20 +0800446 rxdesc->callback_param = ctlr;
addy ke64e36822014-07-01 09:03:59 +0800447 }
448
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100449 txdesc = NULL;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100450 if (xfer->tx_buf) {
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100451 struct dma_slave_config txconf = {
452 .direction = DMA_MEM_TO_DEV,
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100453 .dst_addr = rs->dma_addr_tx,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100454 .dst_addr_width = rs->n_bytes,
Emil Renner Berthing47300722019-04-12 12:53:20 +0200455 .dst_maxburst = rs->fifo_len / 4,
Emil Renner Berthing31bcb572018-10-31 11:56:59 +0100456 };
457
Chris Ruehld66571a2020-05-11 16:30:20 +0800458 dmaengine_slave_config(ctlr->dma_tx, &txconf);
addy ke64e36822014-07-01 09:03:59 +0800459
Addy Ke5dcc44e2014-07-11 10:07:56 +0800460 txdesc = dmaengine_prep_slave_sg(
Chris Ruehld66571a2020-05-11 16:30:20 +0800461 ctlr->dma_tx,
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100462 xfer->tx_sg.sgl, xfer->tx_sg.nents,
Emil Renner Berthingd9071b72018-10-10 11:00:37 +0200463 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800464 if (!txdesc) {
465 if (rxdesc)
Chris Ruehld66571a2020-05-11 16:30:20 +0800466 dmaengine_terminate_sync(ctlr->dma_rx);
Shawn Linea984912016-03-09 16:11:15 +0800467 return -EINVAL;
468 }
addy ke64e36822014-07-01 09:03:59 +0800469
470 txdesc->callback = rockchip_spi_dma_txcb;
Chris Ruehld66571a2020-05-11 16:30:20 +0800471 txdesc->callback_param = ctlr;
addy ke64e36822014-07-01 09:03:59 +0800472 }
473
474 /* rx must be started before tx due to spi instinct */
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100475 if (rxdesc) {
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100476 atomic_or(RXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800477 dmaengine_submit(rxdesc);
Chris Ruehld66571a2020-05-11 16:30:20 +0800478 dma_async_issue_pending(ctlr->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800479 }
480
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100481 spi_enable_chip(rs, true);
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200482
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100483 if (txdesc) {
Emil Renner Berthingfab3e482018-10-31 11:57:01 +0100484 atomic_or(TXDMA, &rs->state);
addy ke64e36822014-07-01 09:03:59 +0800485 dmaengine_submit(txdesc);
Chris Ruehld66571a2020-05-11 16:30:20 +0800486 dma_async_issue_pending(ctlr->dma_tx);
addy ke64e36822014-07-01 09:03:59 +0800487 }
Shawn Linea984912016-03-09 16:11:15 +0800488
Emil Renner Berthinga3c17402018-10-10 11:00:38 +0200489 /* 1 means the transfer is in progress */
490 return 1;
addy ke64e36822014-07-01 09:03:59 +0800491}
492
Arnd Bergmanne5098952021-02-26 15:00:48 +0100493static int rockchip_spi_config(struct rockchip_spi *rs,
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100494 struct spi_device *spi, struct spi_transfer *xfer,
Chris Ruehld065f412020-05-11 16:30:21 +0800495 bool use_dma, bool slave_mode)
addy ke64e36822014-07-01 09:03:59 +0800496{
Emil Renner Berthing2410d6a2018-10-31 11:57:00 +0100497 u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
Jay Fang02621792021-03-24 14:16:41 +0800498 | CR0_BHT_8BIT << CR0_BHT_OFFSET
499 | CR0_SSD_ONE << CR0_SSD_OFFSET
500 | CR0_EM_BIG << CR0_EM_OFFSET;
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100501 u32 cr1;
502 u32 dmacr = 0;
addy ke64e36822014-07-01 09:03:59 +0800503
Chris Ruehld065f412020-05-11 16:30:21 +0800504 if (slave_mode)
505 cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET;
506 rs->slave_abort = false;
507
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100508 cr0 |= rs->rsd << CR0_RSD_OFFSET;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100509 cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
Emil Renner Berthing04290192018-10-31 11:57:11 +0100510 if (spi->mode & SPI_LSB_FIRST)
511 cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
Jon Lin736b81e2021-06-21 18:48:48 +0800512 if (spi->mode & SPI_CS_HIGH)
513 cr0 |= BIT(spi->chip_select) << CR0_SOI_OFFSET;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100514
515 if (xfer->rx_buf && xfer->tx_buf)
516 cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
517 else if (xfer->rx_buf)
518 cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100519 else if (use_dma)
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100520 cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
addy ke64e36822014-07-01 09:03:59 +0800521
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100522 switch (xfer->bits_per_word) {
523 case 4:
524 cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
525 cr1 = xfer->len - 1;
526 break;
527 case 8:
528 cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
529 cr1 = xfer->len - 1;
530 break;
531 case 16:
532 cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
533 cr1 = xfer->len / 2 - 1;
534 break;
535 default:
536 /* we only whitelist 4, 8 and 16 bit words in
Chris Ruehld66571a2020-05-11 16:30:20 +0800537 * ctlr->bits_per_word_mask, so this shouldn't
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100538 * happen
539 */
Arnd Bergmanne5098952021-02-26 15:00:48 +0100540 dev_err(rs->dev, "unknown bits per word: %d\n",
541 xfer->bits_per_word);
542 return -EINVAL;
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100543 }
544
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100545 if (use_dma) {
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100546 if (xfer->tx_buf)
addy ke64e36822014-07-01 09:03:59 +0800547 dmacr |= TF_DMA_EN;
Emil Renner Berthingfc1ad8e2018-10-31 11:57:03 +0100548 if (xfer->rx_buf)
addy ke64e36822014-07-01 09:03:59 +0800549 dmacr |= RF_DMA_EN;
550 }
551
addy ke64e36822014-07-01 09:03:59 +0800552 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100553 writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
Huibin Hong04b37d22017-08-16 10:12:02 +0800554
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100555 /* unfortunately setting the fifo threshold level to generate an
556 * interrupt exactly when the fifo is full doesn't seem to work,
557 * so we need the strict inequality here
558 */
Jon Lin4a47fcd2021-06-21 18:47:57 +0800559 if ((xfer->len / rs->n_bytes) < rs->fifo_len)
560 writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100561 else
562 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
addy ke64e36822014-07-01 09:03:59 +0800563
Jon Lin2758bd02021-06-21 18:47:58 +0800564 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR);
Jon Lin4d9ca632020-07-23 08:43:54 +0800565 writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1,
566 rs->regs + ROCKCHIP_SPI_DMARDLR);
addy ke64e36822014-07-01 09:03:59 +0800567 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
568
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100569 /* the hardware only supports an even clock divisor, so
570 * round divisor = spiclk / speed up to nearest even number
571 * so that the resulting speed is <= the requested speed
572 */
573 writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
574 rs->regs + ROCKCHIP_SPI_BAUDR);
Arnd Bergmanne5098952021-02-26 15:00:48 +0100575
576 return 0;
addy ke64e36822014-07-01 09:03:59 +0800577}
578
Brian Norris5185a812016-07-14 18:30:59 -0700579static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
580{
581 return ROCKCHIP_SPI_MAX_TRANLEN;
582}
583
Chris Ruehld065f412020-05-11 16:30:21 +0800584static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
585{
586 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
587
588 rs->slave_abort = true;
Vincent Pelletier6bd2c8672021-01-07 23:58:32 +0000589 spi_finalize_current_transfer(ctlr);
Chris Ruehld065f412020-05-11 16:30:21 +0800590
591 return 0;
592}
593
Addy Ke5dcc44e2014-07-11 10:07:56 +0800594static int rockchip_spi_transfer_one(
Chris Ruehld66571a2020-05-11 16:30:20 +0800595 struct spi_controller *ctlr,
addy ke64e36822014-07-01 09:03:59 +0800596 struct spi_device *spi,
597 struct spi_transfer *xfer)
598{
Chris Ruehld66571a2020-05-11 16:30:20 +0800599 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Arnd Bergmanne5098952021-02-26 15:00:48 +0100600 int ret;
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100601 bool use_dma;
addy ke64e36822014-07-01 09:03:59 +0800602
Tobias Schramm54577732021-08-27 07:03:57 +0200603 /* Zero length transfers won't trigger an interrupt on completion */
604 if (!xfer->len) {
605 spi_finalize_current_transfer(ctlr);
606 return 1;
607 }
608
Doug Anderson62946172014-09-03 13:44:26 -0700609 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
610 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
addy ke64e36822014-07-01 09:03:59 +0800611
612 if (!xfer->tx_buf && !xfer->rx_buf) {
613 dev_err(rs->dev, "No buffer for transfer\n");
614 return -EINVAL;
615 }
616
Brian Norris5185a812016-07-14 18:30:59 -0700617 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
618 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
619 return -EINVAL;
620 }
621
Emil Renner Berthing65498c62018-10-31 11:57:10 +0100622 rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
addy ke64e36822014-07-01 09:03:59 +0800623
Chris Ruehld66571a2020-05-11 16:30:20 +0800624 use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
addy ke64e36822014-07-01 09:03:59 +0800625
Arnd Bergmanne5098952021-02-26 15:00:48 +0100626 ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
627 if (ret)
628 return ret;
addy ke64e36822014-07-01 09:03:59 +0800629
Emil Renner Berthingeff02752018-10-31 11:57:06 +0100630 if (use_dma)
Chris Ruehld66571a2020-05-11 16:30:20 +0800631 return rockchip_spi_prepare_dma(rs, ctlr, xfer);
addy ke64e36822014-07-01 09:03:59 +0800632
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100633 return rockchip_spi_prepare_irq(rs, xfer);
addy ke64e36822014-07-01 09:03:59 +0800634}
635
Chris Ruehld66571a2020-05-11 16:30:20 +0800636static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
Addy Ke5dcc44e2014-07-11 10:07:56 +0800637 struct spi_device *spi,
638 struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800639{
Chris Ruehld66571a2020-05-11 16:30:20 +0800640 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100641 unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
addy ke64e36822014-07-01 09:03:59 +0800642
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100643 /* if the numbor of spi words to transfer is less than the fifo
644 * length we can just fill the fifo and wait for a single irq,
645 * so don't bother setting up dma
646 */
647 return xfer->len / bytes_per_word >= rs->fifo_len;
addy ke64e36822014-07-01 09:03:59 +0800648}
649
650static int rockchip_spi_probe(struct platform_device *pdev)
651{
Jeffy Chen43de9792017-08-07 20:40:18 +0800652 int ret;
addy ke64e36822014-07-01 09:03:59 +0800653 struct rockchip_spi *rs;
Chris Ruehld66571a2020-05-11 16:30:20 +0800654 struct spi_controller *ctlr;
addy ke64e36822014-07-01 09:03:59 +0800655 struct resource *mem;
Chris Ruehld065f412020-05-11 16:30:21 +0800656 struct device_node *np = pdev->dev.of_node;
Julius Werner76b17e62015-03-26 16:30:25 -0700657 u32 rsd_nsecs;
Chris Ruehld065f412020-05-11 16:30:21 +0800658 bool slave_mode;
addy ke64e36822014-07-01 09:03:59 +0800659
Chris Ruehld065f412020-05-11 16:30:21 +0800660 slave_mode = of_property_read_bool(np, "spi-slave");
661
662 if (slave_mode)
663 ctlr = spi_alloc_slave(&pdev->dev,
664 sizeof(struct rockchip_spi));
665 else
666 ctlr = spi_alloc_master(&pdev->dev,
667 sizeof(struct rockchip_spi));
668
Chris Ruehld66571a2020-05-11 16:30:20 +0800669 if (!ctlr)
addy ke64e36822014-07-01 09:03:59 +0800670 return -ENOMEM;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800671
Chris Ruehld66571a2020-05-11 16:30:20 +0800672 platform_set_drvdata(pdev, ctlr);
addy ke64e36822014-07-01 09:03:59 +0800673
Chris Ruehld66571a2020-05-11 16:30:20 +0800674 rs = spi_controller_get_devdata(ctlr);
Chris Ruehld065f412020-05-11 16:30:21 +0800675 ctlr->slave = slave_mode;
addy ke64e36822014-07-01 09:03:59 +0800676
677 /* Get basic io resource and map it */
678 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
679 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
680 if (IS_ERR(rs->regs)) {
addy ke64e36822014-07-01 09:03:59 +0800681 ret = PTR_ERR(rs->regs);
Chris Ruehld66571a2020-05-11 16:30:20 +0800682 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800683 }
684
685 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
686 if (IS_ERR(rs->apb_pclk)) {
687 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
688 ret = PTR_ERR(rs->apb_pclk);
Chris Ruehld66571a2020-05-11 16:30:20 +0800689 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800690 }
691
692 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
693 if (IS_ERR(rs->spiclk)) {
694 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
695 ret = PTR_ERR(rs->spiclk);
Chris Ruehld66571a2020-05-11 16:30:20 +0800696 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800697 }
698
699 ret = clk_prepare_enable(rs->apb_pclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800700 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800701 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
Chris Ruehld66571a2020-05-11 16:30:20 +0800702 goto err_put_ctlr;
addy ke64e36822014-07-01 09:03:59 +0800703 }
704
705 ret = clk_prepare_enable(rs->spiclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800706 if (ret < 0) {
addy ke64e36822014-07-01 09:03:59 +0800707 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800708 goto err_disable_apbclk;
addy ke64e36822014-07-01 09:03:59 +0800709 }
710
Emil Renner Berthing30688e42018-10-31 11:56:58 +0100711 spi_enable_chip(rs, false);
addy ke64e36822014-07-01 09:03:59 +0800712
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100713 ret = platform_get_irq(pdev, 0);
714 if (ret < 0)
715 goto err_disable_spiclk;
716
717 ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
Chris Ruehld66571a2020-05-11 16:30:20 +0800718 IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
Emil Renner Berthing01b59ce2018-10-31 11:57:09 +0100719 if (ret)
720 goto err_disable_spiclk;
721
addy ke64e36822014-07-01 09:03:59 +0800722 rs->dev = &pdev->dev;
Emil Renner Berthing420b82f2018-10-31 11:57:07 +0100723 rs->freq = clk_get_rate(rs->spiclk);
addy ke64e36822014-07-01 09:03:59 +0800724
Julius Werner76b17e62015-03-26 16:30:25 -0700725 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
Emil Renner Berthing74b7efa2018-10-31 11:57:08 +0100726 &rsd_nsecs)) {
727 /* rx sample delay is expressed in parent clock cycles (max 3) */
728 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
729 1000000000 >> 8);
730 if (!rsd) {
731 dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
732 rs->freq, rsd_nsecs);
733 } else if (rsd > CR0_RSD_MAX) {
734 rsd = CR0_RSD_MAX;
735 dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
736 rs->freq, rsd_nsecs,
737 CR0_RSD_MAX * 1000000000U / rs->freq);
738 }
739 rs->rsd = rsd;
740 }
Julius Werner76b17e62015-03-26 16:30:25 -0700741
addy ke64e36822014-07-01 09:03:59 +0800742 rs->fifo_len = get_fifo_len(rs);
743 if (!rs->fifo_len) {
744 dev_err(&pdev->dev, "Failed to get fifo length\n");
Wei Yongjundb7e8d92014-07-20 22:02:04 +0800745 ret = -EINVAL;
Jeffy Chenc3515872017-06-13 13:25:40 +0800746 goto err_disable_spiclk;
addy ke64e36822014-07-01 09:03:59 +0800747 }
748
Alexander Kochetkov940f3bb2020-10-16 11:50:14 +0300749 pm_runtime_set_autosuspend_delay(&pdev->dev, ROCKCHIP_AUTOSUSPEND_TIMEOUT);
750 pm_runtime_use_autosuspend(&pdev->dev);
addy ke64e36822014-07-01 09:03:59 +0800751 pm_runtime_set_active(&pdev->dev);
752 pm_runtime_enable(&pdev->dev);
753
Chris Ruehld66571a2020-05-11 16:30:20 +0800754 ctlr->auto_runtime_pm = true;
755 ctlr->bus_num = pdev->id;
756 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
Chris Ruehld065f412020-05-11 16:30:21 +0800757 if (slave_mode) {
758 ctlr->mode_bits |= SPI_NO_CS;
759 ctlr->slave_abort = rockchip_spi_slave_abort;
760 } else {
761 ctlr->flags = SPI_MASTER_GPIO_SS;
Chris Ruehleb1262e2020-05-11 16:30:22 +0800762 ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM;
763 /*
764 * rk spi0 has two native cs, spi1..5 one cs only
765 * if num-cs is missing in the dts, default to 1
766 */
767 if (of_property_read_u16(np, "num-cs", &ctlr->num_chipselect))
768 ctlr->num_chipselect = 1;
769 ctlr->use_gpio_descriptors = true;
Chris Ruehld065f412020-05-11 16:30:21 +0800770 }
Chris Ruehld66571a2020-05-11 16:30:20 +0800771 ctlr->dev.of_node = pdev->dev.of_node;
772 ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
773 ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
774 ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
addy ke64e36822014-07-01 09:03:59 +0800775
Chris Ruehld66571a2020-05-11 16:30:20 +0800776 ctlr->set_cs = rockchip_spi_set_cs;
777 ctlr->transfer_one = rockchip_spi_transfer_one;
778 ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
779 ctlr->handle_err = rockchip_spi_handle_err;
addy ke64e36822014-07-01 09:03:59 +0800780
Chris Ruehld66571a2020-05-11 16:30:20 +0800781 ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
782 if (IS_ERR(ctlr->dma_tx)) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800783 /* Check tx to see if we need defer probing driver */
Chris Ruehld66571a2020-05-11 16:30:20 +0800784 if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800785 ret = -EPROBE_DEFER;
Jeffy Chenc3515872017-06-13 13:25:40 +0800786 goto err_disable_pm_runtime;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800787 }
addy ke64e36822014-07-01 09:03:59 +0800788 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
Chris Ruehld66571a2020-05-11 16:30:20 +0800789 ctlr->dma_tx = NULL;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800790 }
addy ke64e36822014-07-01 09:03:59 +0800791
Chris Ruehld66571a2020-05-11 16:30:20 +0800792 ctlr->dma_rx = dma_request_chan(rs->dev, "rx");
793 if (IS_ERR(ctlr->dma_rx)) {
794 if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) {
Shawn Line4c0e062016-03-31 11:11:41 +0800795 ret = -EPROBE_DEFER;
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300796 goto err_free_dma_tx;
addy ke64e36822014-07-01 09:03:59 +0800797 }
798 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
Chris Ruehld66571a2020-05-11 16:30:20 +0800799 ctlr->dma_rx = NULL;
addy ke64e36822014-07-01 09:03:59 +0800800 }
801
Chris Ruehld66571a2020-05-11 16:30:20 +0800802 if (ctlr->dma_tx && ctlr->dma_rx) {
Emil Renner Berthingeee06a92018-10-31 11:57:04 +0100803 rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
804 rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
Chris Ruehld66571a2020-05-11 16:30:20 +0800805 ctlr->can_dma = rockchip_spi_can_dma;
addy ke64e36822014-07-01 09:03:59 +0800806 }
807
Jon Lin736b81e2021-06-21 18:48:48 +0800808 switch (readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION)) {
809 case ROCKCHIP_SPI_VER2_TYPE2:
810 ctlr->mode_bits |= SPI_CS_HIGH;
811 break;
812 default:
813 break;
814 }
815
Chris Ruehld66571a2020-05-11 16:30:20 +0800816 ret = devm_spi_register_controller(&pdev->dev, ctlr);
Jeffy Chen43de9792017-08-07 20:40:18 +0800817 if (ret < 0) {
Chris Ruehld66571a2020-05-11 16:30:20 +0800818 dev_err(&pdev->dev, "Failed to register controller\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800819 goto err_free_dma_rx;
addy ke64e36822014-07-01 09:03:59 +0800820 }
821
addy ke64e36822014-07-01 09:03:59 +0800822 return 0;
823
Jeffy Chenc3515872017-06-13 13:25:40 +0800824err_free_dma_rx:
Chris Ruehld66571a2020-05-11 16:30:20 +0800825 if (ctlr->dma_rx)
826 dma_release_channel(ctlr->dma_rx);
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300827err_free_dma_tx:
Chris Ruehld66571a2020-05-11 16:30:20 +0800828 if (ctlr->dma_tx)
829 dma_release_channel(ctlr->dma_tx);
Jeffy Chenc3515872017-06-13 13:25:40 +0800830err_disable_pm_runtime:
831 pm_runtime_disable(&pdev->dev);
832err_disable_spiclk:
addy ke64e36822014-07-01 09:03:59 +0800833 clk_disable_unprepare(rs->spiclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800834err_disable_apbclk:
addy ke64e36822014-07-01 09:03:59 +0800835 clk_disable_unprepare(rs->apb_pclk);
Chris Ruehld66571a2020-05-11 16:30:20 +0800836err_put_ctlr:
837 spi_controller_put(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800838
839 return ret;
840}
841
842static int rockchip_spi_remove(struct platform_device *pdev)
843{
Chris Ruehld66571a2020-05-11 16:30:20 +0800844 struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev));
845 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800846
Jeffy Chen6a06e892017-08-07 20:40:19 +0800847 pm_runtime_get_sync(&pdev->dev);
addy ke64e36822014-07-01 09:03:59 +0800848
849 clk_disable_unprepare(rs->spiclk);
850 clk_disable_unprepare(rs->apb_pclk);
851
Jeffy Chen6a06e892017-08-07 20:40:19 +0800852 pm_runtime_put_noidle(&pdev->dev);
853 pm_runtime_disable(&pdev->dev);
854 pm_runtime_set_suspended(&pdev->dev);
855
Chris Ruehld66571a2020-05-11 16:30:20 +0800856 if (ctlr->dma_tx)
857 dma_release_channel(ctlr->dma_tx);
858 if (ctlr->dma_rx)
859 dma_release_channel(ctlr->dma_rx);
addy ke64e36822014-07-01 09:03:59 +0800860
Chris Ruehld66571a2020-05-11 16:30:20 +0800861 spi_controller_put(ctlr);
Shawn Lin844c9f42016-02-15 16:28:12 +0800862
addy ke64e36822014-07-01 09:03:59 +0800863 return 0;
864}
865
866#ifdef CONFIG_PM_SLEEP
867static int rockchip_spi_suspend(struct device *dev)
868{
Jeffy Chen43de9792017-08-07 20:40:18 +0800869 int ret;
Chris Ruehld66571a2020-05-11 16:30:20 +0800870 struct spi_controller *ctlr = dev_get_drvdata(dev);
addy ke64e36822014-07-01 09:03:59 +0800871
Chris Ruehld66571a2020-05-11 16:30:20 +0800872 ret = spi_controller_suspend(ctlr);
Jeffy Chen43de9792017-08-07 20:40:18 +0800873 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800874 return ret;
875
Jeffy Chend38c4ae12017-08-07 20:40:20 +0800876 ret = pm_runtime_force_suspend(dev);
877 if (ret < 0)
878 return ret;
addy ke64e36822014-07-01 09:03:59 +0800879
Brian Norris23e291c2016-12-16 16:59:16 -0800880 pinctrl_pm_select_sleep_state(dev);
881
Jeffy Chen43de9792017-08-07 20:40:18 +0800882 return 0;
addy ke64e36822014-07-01 09:03:59 +0800883}
884
885static int rockchip_spi_resume(struct device *dev)
886{
Jeffy Chen43de9792017-08-07 20:40:18 +0800887 int ret;
Chris Ruehld66571a2020-05-11 16:30:20 +0800888 struct spi_controller *ctlr = dev_get_drvdata(dev);
889 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800890
Brian Norris23e291c2016-12-16 16:59:16 -0800891 pinctrl_pm_select_default_state(dev);
892
Jeffy Chend38c4ae12017-08-07 20:40:20 +0800893 ret = pm_runtime_force_resume(dev);
894 if (ret < 0)
895 return ret;
addy ke64e36822014-07-01 09:03:59 +0800896
Chris Ruehld66571a2020-05-11 16:30:20 +0800897 ret = spi_controller_resume(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800898 if (ret < 0) {
899 clk_disable_unprepare(rs->spiclk);
900 clk_disable_unprepare(rs->apb_pclk);
901 }
902
Jeffy Chen43de9792017-08-07 20:40:18 +0800903 return 0;
addy ke64e36822014-07-01 09:03:59 +0800904}
905#endif /* CONFIG_PM_SLEEP */
906
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100907#ifdef CONFIG_PM
addy ke64e36822014-07-01 09:03:59 +0800908static int rockchip_spi_runtime_suspend(struct device *dev)
909{
Chris Ruehld66571a2020-05-11 16:30:20 +0800910 struct spi_controller *ctlr = dev_get_drvdata(dev);
911 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800912
913 clk_disable_unprepare(rs->spiclk);
914 clk_disable_unprepare(rs->apb_pclk);
915
916 return 0;
917}
918
919static int rockchip_spi_runtime_resume(struct device *dev)
920{
921 int ret;
Chris Ruehld66571a2020-05-11 16:30:20 +0800922 struct spi_controller *ctlr = dev_get_drvdata(dev);
923 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
addy ke64e36822014-07-01 09:03:59 +0800924
925 ret = clk_prepare_enable(rs->apb_pclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800926 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800927 return ret;
928
929 ret = clk_prepare_enable(rs->spiclk);
Jeffy Chen43de9792017-08-07 20:40:18 +0800930 if (ret < 0)
addy ke64e36822014-07-01 09:03:59 +0800931 clk_disable_unprepare(rs->apb_pclk);
932
Jeffy Chen43de9792017-08-07 20:40:18 +0800933 return 0;
addy ke64e36822014-07-01 09:03:59 +0800934}
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100935#endif /* CONFIG_PM */
addy ke64e36822014-07-01 09:03:59 +0800936
937static const struct dev_pm_ops rockchip_spi_pm = {
938 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
939 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
940 rockchip_spi_runtime_resume, NULL)
941};
942
943static const struct of_device_id rockchip_spi_dt_match[] = {
Johan Jonkerc6486ea2020-03-09 16:10:03 +0100944 { .compatible = "rockchip,px30-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800945 { .compatible = "rockchip,rk3036-spi", },
addy ke64e36822014-07-01 09:03:59 +0800946 { .compatible = "rockchip,rk3066-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800947 { .compatible = "rockchip,rk3188-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800948 { .compatible = "rockchip,rk3228-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800949 { .compatible = "rockchip,rk3288-spi", },
Johan Jonkerc6486ea2020-03-09 16:10:03 +0100950 { .compatible = "rockchip,rk3308-spi", },
951 { .compatible = "rockchip,rk3328-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800952 { .compatible = "rockchip,rk3368-spi", },
Xu Jianqun9b7a5622016-02-18 19:16:31 +0800953 { .compatible = "rockchip,rk3399-spi", },
Johan Jonkerc6486ea2020-03-09 16:10:03 +0100954 { .compatible = "rockchip,rv1108-spi", },
Jon Lin0f4f58b2021-06-21 18:47:56 +0800955 { .compatible = "rockchip,rv1126-spi", },
addy ke64e36822014-07-01 09:03:59 +0800956 { },
957};
958MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
959
960static struct platform_driver rockchip_spi_driver = {
961 .driver = {
962 .name = DRIVER_NAME,
addy ke64e36822014-07-01 09:03:59 +0800963 .pm = &rockchip_spi_pm,
964 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
965 },
966 .probe = rockchip_spi_probe,
967 .remove = rockchip_spi_remove,
968};
969
970module_platform_driver(rockchip_spi_driver);
971
Addy Ke5dcc44e2014-07-11 10:07:56 +0800972MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
addy ke64e36822014-07-01 09:03:59 +0800973MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
974MODULE_LICENSE("GPL v2");