blob: 0b4a52b3e1dc601234366d5998b7b5dac66506d1 [file] [log] [blame]
addy ke64e36822014-07-01 09:03:59 +08001/*
2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
Addy Ke5dcc44e2014-07-11 10:07:56 +08003 * Author: Addy Ke <addy.ke@rock-chips.com>
addy ke64e36822014-07-01 09:03:59 +08004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
addy ke64e36822014-07-01 09:03:59 +080016#include <linux/clk.h>
addy ke64e36822014-07-01 09:03:59 +080017#include <linux/dmaengine.h>
Shawn Linec5c5d82016-03-10 14:51:48 +080018#include <linux/module.h>
19#include <linux/of.h>
Brian Norris23e291c2016-12-16 16:59:16 -080020#include <linux/pinctrl/consumer.h>
Shawn Linec5c5d82016-03-10 14:51:48 +080021#include <linux/platform_device.h>
22#include <linux/spi/spi.h>
23#include <linux/pm_runtime.h>
24#include <linux/scatterlist.h>
addy ke64e36822014-07-01 09:03:59 +080025
26#define DRIVER_NAME "rockchip-spi"
27
Jeffy Chenaa099382017-06-28 12:38:43 +080028#define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
29 writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
30#define ROCKCHIP_SPI_SET_BITS(reg, bits) \
31 writel_relaxed(readl_relaxed(reg) | (bits), reg)
32
addy ke64e36822014-07-01 09:03:59 +080033/* SPI register offsets */
34#define ROCKCHIP_SPI_CTRLR0 0x0000
35#define ROCKCHIP_SPI_CTRLR1 0x0004
36#define ROCKCHIP_SPI_SSIENR 0x0008
37#define ROCKCHIP_SPI_SER 0x000c
38#define ROCKCHIP_SPI_BAUDR 0x0010
39#define ROCKCHIP_SPI_TXFTLR 0x0014
40#define ROCKCHIP_SPI_RXFTLR 0x0018
41#define ROCKCHIP_SPI_TXFLR 0x001c
42#define ROCKCHIP_SPI_RXFLR 0x0020
43#define ROCKCHIP_SPI_SR 0x0024
44#define ROCKCHIP_SPI_IPR 0x0028
45#define ROCKCHIP_SPI_IMR 0x002c
46#define ROCKCHIP_SPI_ISR 0x0030
47#define ROCKCHIP_SPI_RISR 0x0034
48#define ROCKCHIP_SPI_ICR 0x0038
49#define ROCKCHIP_SPI_DMACR 0x003c
50#define ROCKCHIP_SPI_DMATDLR 0x0040
51#define ROCKCHIP_SPI_DMARDLR 0x0044
52#define ROCKCHIP_SPI_TXDR 0x0400
53#define ROCKCHIP_SPI_RXDR 0x0800
54
55/* Bit fields in CTRLR0 */
56#define CR0_DFS_OFFSET 0
57
58#define CR0_CFS_OFFSET 2
59
60#define CR0_SCPH_OFFSET 6
61
62#define CR0_SCPOL_OFFSET 7
63
64#define CR0_CSM_OFFSET 8
65#define CR0_CSM_KEEP 0x0
66/* ss_n be high for half sclk_out cycles */
67#define CR0_CSM_HALF 0X1
68/* ss_n be high for one sclk_out cycle */
69#define CR0_CSM_ONE 0x2
70
71/* ss_n to sclk_out delay */
72#define CR0_SSD_OFFSET 10
73/*
74 * The period between ss_n active and
75 * sclk_out active is half sclk_out cycles
76 */
77#define CR0_SSD_HALF 0x0
78/*
79 * The period between ss_n active and
80 * sclk_out active is one sclk_out cycle
81 */
82#define CR0_SSD_ONE 0x1
83
84#define CR0_EM_OFFSET 11
85#define CR0_EM_LITTLE 0x0
86#define CR0_EM_BIG 0x1
87
88#define CR0_FBM_OFFSET 12
89#define CR0_FBM_MSB 0x0
90#define CR0_FBM_LSB 0x1
91
92#define CR0_BHT_OFFSET 13
93#define CR0_BHT_16BIT 0x0
94#define CR0_BHT_8BIT 0x1
95
96#define CR0_RSD_OFFSET 14
97
98#define CR0_FRF_OFFSET 16
99#define CR0_FRF_SPI 0x0
100#define CR0_FRF_SSP 0x1
101#define CR0_FRF_MICROWIRE 0x2
102
103#define CR0_XFM_OFFSET 18
104#define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
105#define CR0_XFM_TR 0x0
106#define CR0_XFM_TO 0x1
107#define CR0_XFM_RO 0x2
108
109#define CR0_OPM_OFFSET 20
110#define CR0_OPM_MASTER 0x0
111#define CR0_OPM_SLAVE 0x1
112
113#define CR0_MTM_OFFSET 0x21
114
115/* Bit fields in SER, 2bit */
116#define SER_MASK 0x3
117
118/* Bit fields in SR, 5bit */
119#define SR_MASK 0x1f
120#define SR_BUSY (1 << 0)
121#define SR_TF_FULL (1 << 1)
122#define SR_TF_EMPTY (1 << 2)
123#define SR_RF_EMPTY (1 << 3)
124#define SR_RF_FULL (1 << 4)
125
126/* Bit fields in ISR, IMR, ISR, RISR, 5bit */
127#define INT_MASK 0x1f
128#define INT_TF_EMPTY (1 << 0)
129#define INT_TF_OVERFLOW (1 << 1)
130#define INT_RF_UNDERFLOW (1 << 2)
131#define INT_RF_OVERFLOW (1 << 3)
132#define INT_RF_FULL (1 << 4)
133
134/* Bit fields in ICR, 4bit */
135#define ICR_MASK 0x0f
136#define ICR_ALL (1 << 0)
137#define ICR_RF_UNDERFLOW (1 << 1)
138#define ICR_RF_OVERFLOW (1 << 2)
139#define ICR_TF_OVERFLOW (1 << 3)
140
141/* Bit fields in DMACR */
142#define RF_DMA_EN (1 << 0)
143#define TF_DMA_EN (1 << 1)
144
145#define RXBUSY (1 << 0)
146#define TXBUSY (1 << 1)
147
Addy Kef9cfd522014-10-15 19:25:49 +0800148/* sclk_out: spi master internal logic in rk3x can support 50Mhz */
149#define MAX_SCLK_OUT 50000000
150
Brian Norris5185a812016-07-14 18:30:59 -0700151/*
152 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
153 * the controller seems to hang when given 0x10000, so stick with this for now.
154 */
155#define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
156
Jeffy Chenaa099382017-06-28 12:38:43 +0800157#define ROCKCHIP_SPI_MAX_CS_NUM 2
158
addy ke64e36822014-07-01 09:03:59 +0800159enum rockchip_ssi_type {
160 SSI_MOTO_SPI = 0,
161 SSI_TI_SSP,
162 SSI_NS_MICROWIRE,
163};
164
165struct rockchip_spi_dma_data {
166 struct dma_chan *ch;
167 enum dma_transfer_direction direction;
168 dma_addr_t addr;
169};
170
171struct rockchip_spi {
172 struct device *dev;
173 struct spi_master *master;
174
175 struct clk *spiclk;
176 struct clk *apb_pclk;
177
178 void __iomem *regs;
179 /*depth of the FIFO buffer */
180 u32 fifo_len;
181 /* max bus freq supported */
182 u32 max_freq;
183 /* supported slave numbers */
184 enum rockchip_ssi_type type;
185
186 u16 mode;
187 u8 tmode;
188 u8 bpw;
189 u8 n_bytes;
Shawn Lin108b5c82016-03-10 14:52:27 +0800190 u32 rsd_nsecs;
addy ke64e36822014-07-01 09:03:59 +0800191 unsigned len;
192 u32 speed;
193
194 const void *tx;
195 const void *tx_end;
196 void *rx;
197 void *rx_end;
198
199 u32 state;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800200 /* protect state */
addy ke64e36822014-07-01 09:03:59 +0800201 spinlock_t lock;
202
Jeffy Chenaa099382017-06-28 12:38:43 +0800203 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
204
addy ke64e36822014-07-01 09:03:59 +0800205 u32 use_dma;
206 struct sg_table tx_sg;
207 struct sg_table rx_sg;
208 struct rockchip_spi_dma_data dma_rx;
209 struct rockchip_spi_dma_data dma_tx;
Addy Ke80abf882016-01-22 19:06:52 +0800210 struct dma_slave_caps dma_caps;
addy ke64e36822014-07-01 09:03:59 +0800211};
212
213static inline void spi_enable_chip(struct rockchip_spi *rs, int enable)
214{
215 writel_relaxed((enable ? 1 : 0), rs->regs + ROCKCHIP_SPI_SSIENR);
216}
217
218static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
219{
220 writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
221}
222
223static inline void flush_fifo(struct rockchip_spi *rs)
224{
225 while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR))
226 readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
227}
228
Addy Ke2df08e72014-07-11 10:08:24 +0800229static inline void wait_for_idle(struct rockchip_spi *rs)
230{
231 unsigned long timeout = jiffies + msecs_to_jiffies(5);
232
233 do {
234 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
235 return;
Doug Anderson64bc0112014-09-03 13:44:25 -0700236 } while (!time_after(jiffies, timeout));
Addy Ke2df08e72014-07-11 10:08:24 +0800237
238 dev_warn(rs->dev, "spi controller is in busy state!\n");
239}
240
addy ke64e36822014-07-01 09:03:59 +0800241static u32 get_fifo_len(struct rockchip_spi *rs)
242{
243 u32 fifo;
244
245 for (fifo = 2; fifo < 32; fifo++) {
246 writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
247 if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
248 break;
249 }
250
251 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
252
253 return (fifo == 31) ? 0 : fifo;
254}
255
256static inline u32 tx_max(struct rockchip_spi *rs)
257{
258 u32 tx_left, tx_room;
259
260 tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
261 tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
262
263 return min(tx_left, tx_room);
264}
265
266static inline u32 rx_max(struct rockchip_spi *rs)
267{
268 u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
269 u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
270
271 return min(rx_left, rx_room);
272}
273
274static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
275{
Huibin Hongb920cc32016-02-24 18:00:04 +0800276 struct spi_master *master = spi->master;
277 struct rockchip_spi *rs = spi_master_get_devdata(master);
Jeffy Chenaa099382017-06-28 12:38:43 +0800278 bool cs_asserted = !enable;
Huibin Hongb920cc32016-02-24 18:00:04 +0800279
Jeffy Chenaa099382017-06-28 12:38:43 +0800280 /* Return immediately for no-op */
281 if (cs_asserted == rs->cs_asserted[spi->chip_select])
282 return;
addy ke64e36822014-07-01 09:03:59 +0800283
Jeffy Chenaa099382017-06-28 12:38:43 +0800284 if (cs_asserted) {
285 /* Keep things powered as long as CS is asserted */
286 pm_runtime_get_sync(rs->dev);
addy ke64e36822014-07-01 09:03:59 +0800287
Jeffy Chenaa099382017-06-28 12:38:43 +0800288 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
289 BIT(spi->chip_select));
290 } else {
291 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
292 BIT(spi->chip_select));
addy ke64e36822014-07-01 09:03:59 +0800293
Jeffy Chenaa099382017-06-28 12:38:43 +0800294 /* Drop reference from when we first asserted CS */
295 pm_runtime_put(rs->dev);
296 }
Huibin Hongb920cc32016-02-24 18:00:04 +0800297
Jeffy Chenaa099382017-06-28 12:38:43 +0800298 rs->cs_asserted[spi->chip_select] = cs_asserted;
addy ke64e36822014-07-01 09:03:59 +0800299}
300
301static int rockchip_spi_prepare_message(struct spi_master *master,
Addy Ke5dcc44e2014-07-11 10:07:56 +0800302 struct spi_message *msg)
addy ke64e36822014-07-01 09:03:59 +0800303{
304 struct rockchip_spi *rs = spi_master_get_devdata(master);
305 struct spi_device *spi = msg->spi;
306
addy ke64e36822014-07-01 09:03:59 +0800307 rs->mode = spi->mode;
308
309 return 0;
310}
311
Andy Shevchenko22917932015-02-27 17:34:16 +0200312static void rockchip_spi_handle_err(struct spi_master *master,
313 struct spi_message *msg)
addy ke64e36822014-07-01 09:03:59 +0800314{
315 unsigned long flags;
316 struct rockchip_spi *rs = spi_master_get_devdata(master);
317
318 spin_lock_irqsave(&rs->lock, flags);
319
Addy Ke5dcc44e2014-07-11 10:07:56 +0800320 /*
321 * For DMA mode, we need terminate DMA channel and flush
322 * fifo for the next transfer if DMA thansfer timeout.
Andy Shevchenko22917932015-02-27 17:34:16 +0200323 * handle_err() was called by core if transfer failed.
324 * Maybe it is reasonable for error handling here.
Addy Ke5dcc44e2014-07-11 10:07:56 +0800325 */
addy ke64e36822014-07-01 09:03:59 +0800326 if (rs->use_dma) {
327 if (rs->state & RXBUSY) {
Shawn Lin557b7ea2016-03-09 16:11:23 +0800328 dmaengine_terminate_async(rs->dma_rx.ch);
addy ke64e36822014-07-01 09:03:59 +0800329 flush_fifo(rs);
330 }
331
332 if (rs->state & TXBUSY)
Shawn Lin557b7ea2016-03-09 16:11:23 +0800333 dmaengine_terminate_async(rs->dma_tx.ch);
addy ke64e36822014-07-01 09:03:59 +0800334 }
335
336 spin_unlock_irqrestore(&rs->lock, flags);
Andy Shevchenko22917932015-02-27 17:34:16 +0200337}
338
339static int rockchip_spi_unprepare_message(struct spi_master *master,
340 struct spi_message *msg)
341{
342 struct rockchip_spi *rs = spi_master_get_devdata(master);
addy ke64e36822014-07-01 09:03:59 +0800343
Addy Kec28be312014-10-15 19:26:18 +0800344 spi_enable_chip(rs, 0);
345
addy ke64e36822014-07-01 09:03:59 +0800346 return 0;
347}
348
349static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
350{
351 u32 max = tx_max(rs);
352 u32 txw = 0;
353
354 while (max--) {
355 if (rs->n_bytes == 1)
356 txw = *(u8 *)(rs->tx);
357 else
358 txw = *(u16 *)(rs->tx);
359
360 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
361 rs->tx += rs->n_bytes;
362 }
363}
364
365static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
366{
367 u32 max = rx_max(rs);
368 u32 rxw;
369
370 while (max--) {
371 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
372 if (rs->n_bytes == 1)
373 *(u8 *)(rs->rx) = (u8)rxw;
374 else
375 *(u16 *)(rs->rx) = (u16)rxw;
376 rs->rx += rs->n_bytes;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800377 }
addy ke64e36822014-07-01 09:03:59 +0800378}
379
380static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
381{
382 int remain = 0;
383
384 do {
385 if (rs->tx) {
386 remain = rs->tx_end - rs->tx;
387 rockchip_spi_pio_writer(rs);
388 }
389
390 if (rs->rx) {
391 remain = rs->rx_end - rs->rx;
392 rockchip_spi_pio_reader(rs);
393 }
394
395 cpu_relax();
396 } while (remain);
397
Addy Ke2df08e72014-07-11 10:08:24 +0800398 /* If tx, wait until the FIFO data completely. */
399 if (rs->tx)
400 wait_for_idle(rs);
401
Addy Kec28be312014-10-15 19:26:18 +0800402 spi_enable_chip(rs, 0);
403
addy ke64e36822014-07-01 09:03:59 +0800404 return 0;
405}
406
407static void rockchip_spi_dma_rxcb(void *data)
408{
409 unsigned long flags;
410 struct rockchip_spi *rs = data;
411
412 spin_lock_irqsave(&rs->lock, flags);
413
414 rs->state &= ~RXBUSY;
Addy Kec28be312014-10-15 19:26:18 +0800415 if (!(rs->state & TXBUSY)) {
416 spi_enable_chip(rs, 0);
addy ke64e36822014-07-01 09:03:59 +0800417 spi_finalize_current_transfer(rs->master);
Addy Kec28be312014-10-15 19:26:18 +0800418 }
addy ke64e36822014-07-01 09:03:59 +0800419
420 spin_unlock_irqrestore(&rs->lock, flags);
421}
422
423static void rockchip_spi_dma_txcb(void *data)
424{
425 unsigned long flags;
426 struct rockchip_spi *rs = data;
427
Addy Ke2df08e72014-07-11 10:08:24 +0800428 /* Wait until the FIFO data completely. */
429 wait_for_idle(rs);
430
addy ke64e36822014-07-01 09:03:59 +0800431 spin_lock_irqsave(&rs->lock, flags);
432
433 rs->state &= ~TXBUSY;
Addy Ke2c2bc742014-10-17 09:44:13 +0800434 if (!(rs->state & RXBUSY)) {
435 spi_enable_chip(rs, 0);
addy ke64e36822014-07-01 09:03:59 +0800436 spi_finalize_current_transfer(rs->master);
Addy Ke2c2bc742014-10-17 09:44:13 +0800437 }
addy ke64e36822014-07-01 09:03:59 +0800438
439 spin_unlock_irqrestore(&rs->lock, flags);
440}
441
Shawn Linea984912016-03-09 16:11:15 +0800442static int rockchip_spi_prepare_dma(struct rockchip_spi *rs)
addy ke64e36822014-07-01 09:03:59 +0800443{
444 unsigned long flags;
445 struct dma_slave_config rxconf, txconf;
446 struct dma_async_tx_descriptor *rxdesc, *txdesc;
447
448 spin_lock_irqsave(&rs->lock, flags);
449 rs->state &= ~RXBUSY;
450 rs->state &= ~TXBUSY;
451 spin_unlock_irqrestore(&rs->lock, flags);
452
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100453 rxdesc = NULL;
addy ke64e36822014-07-01 09:03:59 +0800454 if (rs->rx) {
455 rxconf.direction = rs->dma_rx.direction;
456 rxconf.src_addr = rs->dma_rx.addr;
457 rxconf.src_addr_width = rs->n_bytes;
Addy Ke80abf882016-01-22 19:06:52 +0800458 if (rs->dma_caps.max_burst > 4)
459 rxconf.src_maxburst = 4;
460 else
461 rxconf.src_maxburst = 1;
addy ke64e36822014-07-01 09:03:59 +0800462 dmaengine_slave_config(rs->dma_rx.ch, &rxconf);
463
Addy Ke5dcc44e2014-07-11 10:07:56 +0800464 rxdesc = dmaengine_prep_slave_sg(
465 rs->dma_rx.ch,
addy ke64e36822014-07-01 09:03:59 +0800466 rs->rx_sg.sgl, rs->rx_sg.nents,
467 rs->dma_rx.direction, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800468 if (!rxdesc)
469 return -EINVAL;
addy ke64e36822014-07-01 09:03:59 +0800470
471 rxdesc->callback = rockchip_spi_dma_rxcb;
472 rxdesc->callback_param = rs;
473 }
474
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100475 txdesc = NULL;
addy ke64e36822014-07-01 09:03:59 +0800476 if (rs->tx) {
477 txconf.direction = rs->dma_tx.direction;
478 txconf.dst_addr = rs->dma_tx.addr;
479 txconf.dst_addr_width = rs->n_bytes;
Addy Ke80abf882016-01-22 19:06:52 +0800480 if (rs->dma_caps.max_burst > 4)
481 txconf.dst_maxburst = 4;
482 else
483 txconf.dst_maxburst = 1;
addy ke64e36822014-07-01 09:03:59 +0800484 dmaengine_slave_config(rs->dma_tx.ch, &txconf);
485
Addy Ke5dcc44e2014-07-11 10:07:56 +0800486 txdesc = dmaengine_prep_slave_sg(
487 rs->dma_tx.ch,
addy ke64e36822014-07-01 09:03:59 +0800488 rs->tx_sg.sgl, rs->tx_sg.nents,
489 rs->dma_tx.direction, DMA_PREP_INTERRUPT);
Shawn Linea984912016-03-09 16:11:15 +0800490 if (!txdesc) {
491 if (rxdesc)
492 dmaengine_terminate_sync(rs->dma_rx.ch);
493 return -EINVAL;
494 }
addy ke64e36822014-07-01 09:03:59 +0800495
496 txdesc->callback = rockchip_spi_dma_txcb;
497 txdesc->callback_param = rs;
498 }
499
500 /* rx must be started before tx due to spi instinct */
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100501 if (rxdesc) {
addy ke64e36822014-07-01 09:03:59 +0800502 spin_lock_irqsave(&rs->lock, flags);
503 rs->state |= RXBUSY;
504 spin_unlock_irqrestore(&rs->lock, flags);
505 dmaengine_submit(rxdesc);
506 dma_async_issue_pending(rs->dma_rx.ch);
507 }
508
Arnd Bergmann97cf5662015-01-28 14:25:10 +0100509 if (txdesc) {
addy ke64e36822014-07-01 09:03:59 +0800510 spin_lock_irqsave(&rs->lock, flags);
511 rs->state |= TXBUSY;
512 spin_unlock_irqrestore(&rs->lock, flags);
513 dmaengine_submit(txdesc);
514 dma_async_issue_pending(rs->dma_tx.ch);
515 }
Shawn Linea984912016-03-09 16:11:15 +0800516
517 return 0;
addy ke64e36822014-07-01 09:03:59 +0800518}
519
520static void rockchip_spi_config(struct rockchip_spi *rs)
521{
522 u32 div = 0;
523 u32 dmacr = 0;
Julius Werner76b17e62015-03-26 16:30:25 -0700524 int rsd = 0;
addy ke64e36822014-07-01 09:03:59 +0800525
526 u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET)
Alexander Kochetkov0277e012016-03-06 13:04:17 +0300527 | (CR0_SSD_ONE << CR0_SSD_OFFSET)
528 | (CR0_EM_BIG << CR0_EM_OFFSET);
addy ke64e36822014-07-01 09:03:59 +0800529
530 cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
531 cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
532 cr0 |= (rs->tmode << CR0_XFM_OFFSET);
533 cr0 |= (rs->type << CR0_FRF_OFFSET);
534
535 if (rs->use_dma) {
536 if (rs->tx)
537 dmacr |= TF_DMA_EN;
538 if (rs->rx)
539 dmacr |= RF_DMA_EN;
540 }
541
Addy Kef9cfd522014-10-15 19:25:49 +0800542 if (WARN_ON(rs->speed > MAX_SCLK_OUT))
543 rs->speed = MAX_SCLK_OUT;
544
Geert Uytterhoevenbb515372016-03-14 16:30:16 +0100545 /* the minimum divisor is 2 */
Addy Kef9cfd522014-10-15 19:25:49 +0800546 if (rs->max_freq < 2 * rs->speed) {
547 clk_set_rate(rs->spiclk, 2 * rs->speed);
548 rs->max_freq = clk_get_rate(rs->spiclk);
549 }
550
addy ke64e36822014-07-01 09:03:59 +0800551 /* div doesn't support odd number */
Julius Werner754ec432015-03-26 16:30:24 -0700552 div = DIV_ROUND_UP(rs->max_freq, rs->speed);
addy ke64e36822014-07-01 09:03:59 +0800553 div = (div + 1) & 0xfffe;
554
Julius Werner76b17e62015-03-26 16:30:25 -0700555 /* Rx sample delay is expressed in parent clock cycles (max 3) */
556 rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8),
557 1000000000 >> 8);
558 if (!rsd && rs->rsd_nsecs) {
559 pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n",
560 rs->max_freq, rs->rsd_nsecs);
561 } else if (rsd > 3) {
562 rsd = 3;
563 pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n",
564 rs->max_freq, rs->rsd_nsecs,
565 rsd * 1000000000U / rs->max_freq);
566 }
567 cr0 |= rsd << CR0_RSD_OFFSET;
568
addy ke64e36822014-07-01 09:03:59 +0800569 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
570
571 writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
572 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
573 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
574
575 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMATDLR);
576 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
577 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
578
579 spi_set_clk(rs, div);
580
Addy Ke5dcc44e2014-07-11 10:07:56 +0800581 dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
addy ke64e36822014-07-01 09:03:59 +0800582}
583
Brian Norris5185a812016-07-14 18:30:59 -0700584static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
585{
586 return ROCKCHIP_SPI_MAX_TRANLEN;
587}
588
Addy Ke5dcc44e2014-07-11 10:07:56 +0800589static int rockchip_spi_transfer_one(
590 struct spi_master *master,
addy ke64e36822014-07-01 09:03:59 +0800591 struct spi_device *spi,
592 struct spi_transfer *xfer)
593{
Tomeu Vizoso4dc0dd82016-06-08 09:32:51 +0200594 int ret = 0;
addy ke64e36822014-07-01 09:03:59 +0800595 struct rockchip_spi *rs = spi_master_get_devdata(master);
596
Doug Anderson62946172014-09-03 13:44:26 -0700597 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
598 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
addy ke64e36822014-07-01 09:03:59 +0800599
600 if (!xfer->tx_buf && !xfer->rx_buf) {
601 dev_err(rs->dev, "No buffer for transfer\n");
602 return -EINVAL;
603 }
604
Brian Norris5185a812016-07-14 18:30:59 -0700605 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
606 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
607 return -EINVAL;
608 }
609
addy ke64e36822014-07-01 09:03:59 +0800610 rs->speed = xfer->speed_hz;
611 rs->bpw = xfer->bits_per_word;
612 rs->n_bytes = rs->bpw >> 3;
613
614 rs->tx = xfer->tx_buf;
615 rs->tx_end = rs->tx + xfer->len;
616 rs->rx = xfer->rx_buf;
617 rs->rx_end = rs->rx + xfer->len;
618 rs->len = xfer->len;
619
620 rs->tx_sg = xfer->tx_sg;
621 rs->rx_sg = xfer->rx_sg;
622
addy ke64e36822014-07-01 09:03:59 +0800623 if (rs->tx && rs->rx)
624 rs->tmode = CR0_XFM_TR;
625 else if (rs->tx)
626 rs->tmode = CR0_XFM_TO;
627 else if (rs->rx)
628 rs->tmode = CR0_XFM_RO;
629
Addy Kea24e70c2014-09-25 14:59:41 +0800630 /* we need prepare dma before spi was enabled */
Addy Kec28be312014-10-15 19:26:18 +0800631 if (master->can_dma && master->can_dma(master, spi, xfer))
addy ke64e36822014-07-01 09:03:59 +0800632 rs->use_dma = 1;
Addy Kec28be312014-10-15 19:26:18 +0800633 else
addy ke64e36822014-07-01 09:03:59 +0800634 rs->use_dma = 0;
635
636 rockchip_spi_config(rs);
637
Addy Kec28be312014-10-15 19:26:18 +0800638 if (rs->use_dma) {
639 if (rs->tmode == CR0_XFM_RO) {
640 /* rx: dma must be prepared first */
Shawn Linea984912016-03-09 16:11:15 +0800641 ret = rockchip_spi_prepare_dma(rs);
Addy Kec28be312014-10-15 19:26:18 +0800642 spi_enable_chip(rs, 1);
643 } else {
644 /* tx or tr: spi must be enabled first */
645 spi_enable_chip(rs, 1);
Shawn Linea984912016-03-09 16:11:15 +0800646 ret = rockchip_spi_prepare_dma(rs);
Addy Kec28be312014-10-15 19:26:18 +0800647 }
Tomeu Vizoso4dc0dd82016-06-08 09:32:51 +0200648 /* successful DMA prepare means the transfer is in progress */
649 ret = ret ? ret : 1;
Addy Kec28be312014-10-15 19:26:18 +0800650 } else {
651 spi_enable_chip(rs, 1);
addy ke64e36822014-07-01 09:03:59 +0800652 ret = rockchip_spi_pio_transfer(rs);
Addy Kec28be312014-10-15 19:26:18 +0800653 }
addy ke64e36822014-07-01 09:03:59 +0800654
655 return ret;
656}
657
658static bool rockchip_spi_can_dma(struct spi_master *master,
Addy Ke5dcc44e2014-07-11 10:07:56 +0800659 struct spi_device *spi,
660 struct spi_transfer *xfer)
addy ke64e36822014-07-01 09:03:59 +0800661{
662 struct rockchip_spi *rs = spi_master_get_devdata(master);
663
664 return (xfer->len > rs->fifo_len);
665}
666
667static int rockchip_spi_probe(struct platform_device *pdev)
668{
669 int ret = 0;
670 struct rockchip_spi *rs;
671 struct spi_master *master;
672 struct resource *mem;
Julius Werner76b17e62015-03-26 16:30:25 -0700673 u32 rsd_nsecs;
addy ke64e36822014-07-01 09:03:59 +0800674
675 master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
Addy Ke5dcc44e2014-07-11 10:07:56 +0800676 if (!master)
addy ke64e36822014-07-01 09:03:59 +0800677 return -ENOMEM;
Addy Ke5dcc44e2014-07-11 10:07:56 +0800678
addy ke64e36822014-07-01 09:03:59 +0800679 platform_set_drvdata(pdev, master);
680
681 rs = spi_master_get_devdata(master);
addy ke64e36822014-07-01 09:03:59 +0800682
683 /* Get basic io resource and map it */
684 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
685 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
686 if (IS_ERR(rs->regs)) {
addy ke64e36822014-07-01 09:03:59 +0800687 ret = PTR_ERR(rs->regs);
Jeffy Chenc3515872017-06-13 13:25:40 +0800688 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800689 }
690
691 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
692 if (IS_ERR(rs->apb_pclk)) {
693 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
694 ret = PTR_ERR(rs->apb_pclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800695 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800696 }
697
698 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
699 if (IS_ERR(rs->spiclk)) {
700 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
701 ret = PTR_ERR(rs->spiclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800702 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800703 }
704
705 ret = clk_prepare_enable(rs->apb_pclk);
706 if (ret) {
707 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800708 goto err_put_master;
addy ke64e36822014-07-01 09:03:59 +0800709 }
710
711 ret = clk_prepare_enable(rs->spiclk);
712 if (ret) {
713 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800714 goto err_disable_apbclk;
addy ke64e36822014-07-01 09:03:59 +0800715 }
716
717 spi_enable_chip(rs, 0);
718
719 rs->type = SSI_MOTO_SPI;
720 rs->master = master;
721 rs->dev = &pdev->dev;
722 rs->max_freq = clk_get_rate(rs->spiclk);
723
Julius Werner76b17e62015-03-26 16:30:25 -0700724 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
725 &rsd_nsecs))
726 rs->rsd_nsecs = rsd_nsecs;
727
addy ke64e36822014-07-01 09:03:59 +0800728 rs->fifo_len = get_fifo_len(rs);
729 if (!rs->fifo_len) {
730 dev_err(&pdev->dev, "Failed to get fifo length\n");
Wei Yongjundb7e8d92014-07-20 22:02:04 +0800731 ret = -EINVAL;
Jeffy Chenc3515872017-06-13 13:25:40 +0800732 goto err_disable_spiclk;
addy ke64e36822014-07-01 09:03:59 +0800733 }
734
735 spin_lock_init(&rs->lock);
736
737 pm_runtime_set_active(&pdev->dev);
738 pm_runtime_enable(&pdev->dev);
739
740 master->auto_runtime_pm = true;
741 master->bus_num = pdev->id;
Addy Keee780992014-07-11 10:08:51 +0800742 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
Jeffy Chenaa099382017-06-28 12:38:43 +0800743 master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
addy ke64e36822014-07-01 09:03:59 +0800744 master->dev.of_node = pdev->dev.of_node;
745 master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
746
747 master->set_cs = rockchip_spi_set_cs;
748 master->prepare_message = rockchip_spi_prepare_message;
749 master->unprepare_message = rockchip_spi_unprepare_message;
750 master->transfer_one = rockchip_spi_transfer_one;
Brian Norris5185a812016-07-14 18:30:59 -0700751 master->max_transfer_size = rockchip_spi_max_transfer_size;
Andy Shevchenko22917932015-02-27 17:34:16 +0200752 master->handle_err = rockchip_spi_handle_err;
Jeffy Chenc8637952017-06-28 12:38:42 +0800753 master->flags = SPI_MASTER_GPIO_SS;
addy ke64e36822014-07-01 09:03:59 +0800754
Shawn Line4c0e062016-03-31 11:11:41 +0800755 rs->dma_tx.ch = dma_request_chan(rs->dev, "tx");
756 if (IS_ERR(rs->dma_tx.ch)) {
Shawn Lin61cadcf2016-03-09 16:11:32 +0800757 /* Check tx to see if we need defer probing driver */
758 if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) {
759 ret = -EPROBE_DEFER;
Jeffy Chenc3515872017-06-13 13:25:40 +0800760 goto err_disable_pm_runtime;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800761 }
addy ke64e36822014-07-01 09:03:59 +0800762 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
Shawn Line4c0e062016-03-31 11:11:41 +0800763 rs->dma_tx.ch = NULL;
Shawn Lin61cadcf2016-03-09 16:11:32 +0800764 }
addy ke64e36822014-07-01 09:03:59 +0800765
Shawn Line4c0e062016-03-31 11:11:41 +0800766 rs->dma_rx.ch = dma_request_chan(rs->dev, "rx");
767 if (IS_ERR(rs->dma_rx.ch)) {
768 if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) {
Shawn Line4c0e062016-03-31 11:11:41 +0800769 ret = -EPROBE_DEFER;
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300770 goto err_free_dma_tx;
addy ke64e36822014-07-01 09:03:59 +0800771 }
772 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
Shawn Line4c0e062016-03-31 11:11:41 +0800773 rs->dma_rx.ch = NULL;
addy ke64e36822014-07-01 09:03:59 +0800774 }
775
776 if (rs->dma_tx.ch && rs->dma_rx.ch) {
Addy Ke80abf882016-01-22 19:06:52 +0800777 dma_get_slave_caps(rs->dma_rx.ch, &(rs->dma_caps));
addy ke64e36822014-07-01 09:03:59 +0800778 rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
779 rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
780 rs->dma_tx.direction = DMA_MEM_TO_DEV;
Addy Ke0ac7a492014-08-20 11:47:42 +0800781 rs->dma_rx.direction = DMA_DEV_TO_MEM;
addy ke64e36822014-07-01 09:03:59 +0800782
783 master->can_dma = rockchip_spi_can_dma;
784 master->dma_tx = rs->dma_tx.ch;
785 master->dma_rx = rs->dma_rx.ch;
786 }
787
788 ret = devm_spi_register_master(&pdev->dev, master);
789 if (ret) {
790 dev_err(&pdev->dev, "Failed to register master\n");
Jeffy Chenc3515872017-06-13 13:25:40 +0800791 goto err_free_dma_rx;
addy ke64e36822014-07-01 09:03:59 +0800792 }
793
addy ke64e36822014-07-01 09:03:59 +0800794 return 0;
795
Jeffy Chenc3515872017-06-13 13:25:40 +0800796err_free_dma_rx:
addy ke64e36822014-07-01 09:03:59 +0800797 if (rs->dma_rx.ch)
798 dma_release_channel(rs->dma_rx.ch);
Dan Carpenter5de7ed02016-05-04 09:25:46 +0300799err_free_dma_tx:
800 if (rs->dma_tx.ch)
801 dma_release_channel(rs->dma_tx.ch);
Jeffy Chenc3515872017-06-13 13:25:40 +0800802err_disable_pm_runtime:
803 pm_runtime_disable(&pdev->dev);
804err_disable_spiclk:
addy ke64e36822014-07-01 09:03:59 +0800805 clk_disable_unprepare(rs->spiclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800806err_disable_apbclk:
addy ke64e36822014-07-01 09:03:59 +0800807 clk_disable_unprepare(rs->apb_pclk);
Jeffy Chenc3515872017-06-13 13:25:40 +0800808err_put_master:
addy ke64e36822014-07-01 09:03:59 +0800809 spi_master_put(master);
810
811 return ret;
812}
813
814static int rockchip_spi_remove(struct platform_device *pdev)
815{
816 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
817 struct rockchip_spi *rs = spi_master_get_devdata(master);
818
819 pm_runtime_disable(&pdev->dev);
820
821 clk_disable_unprepare(rs->spiclk);
822 clk_disable_unprepare(rs->apb_pclk);
823
824 if (rs->dma_tx.ch)
825 dma_release_channel(rs->dma_tx.ch);
826 if (rs->dma_rx.ch)
827 dma_release_channel(rs->dma_rx.ch);
828
Shawn Lin844c9f42016-02-15 16:28:12 +0800829 spi_master_put(master);
830
addy ke64e36822014-07-01 09:03:59 +0800831 return 0;
832}
833
834#ifdef CONFIG_PM_SLEEP
835static int rockchip_spi_suspend(struct device *dev)
836{
837 int ret = 0;
838 struct spi_master *master = dev_get_drvdata(dev);
839 struct rockchip_spi *rs = spi_master_get_devdata(master);
840
841 ret = spi_master_suspend(rs->master);
842 if (ret)
843 return ret;
844
845 if (!pm_runtime_suspended(dev)) {
846 clk_disable_unprepare(rs->spiclk);
847 clk_disable_unprepare(rs->apb_pclk);
848 }
849
Brian Norris23e291c2016-12-16 16:59:16 -0800850 pinctrl_pm_select_sleep_state(dev);
851
addy ke64e36822014-07-01 09:03:59 +0800852 return ret;
853}
854
855static int rockchip_spi_resume(struct device *dev)
856{
857 int ret = 0;
858 struct spi_master *master = dev_get_drvdata(dev);
859 struct rockchip_spi *rs = spi_master_get_devdata(master);
860
Brian Norris23e291c2016-12-16 16:59:16 -0800861 pinctrl_pm_select_default_state(dev);
862
addy ke64e36822014-07-01 09:03:59 +0800863 if (!pm_runtime_suspended(dev)) {
864 ret = clk_prepare_enable(rs->apb_pclk);
865 if (ret < 0)
866 return ret;
867
868 ret = clk_prepare_enable(rs->spiclk);
869 if (ret < 0) {
870 clk_disable_unprepare(rs->apb_pclk);
871 return ret;
872 }
873 }
874
875 ret = spi_master_resume(rs->master);
876 if (ret < 0) {
877 clk_disable_unprepare(rs->spiclk);
878 clk_disable_unprepare(rs->apb_pclk);
879 }
880
881 return ret;
882}
883#endif /* CONFIG_PM_SLEEP */
884
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100885#ifdef CONFIG_PM
addy ke64e36822014-07-01 09:03:59 +0800886static int rockchip_spi_runtime_suspend(struct device *dev)
887{
888 struct spi_master *master = dev_get_drvdata(dev);
889 struct rockchip_spi *rs = spi_master_get_devdata(master);
890
891 clk_disable_unprepare(rs->spiclk);
892 clk_disable_unprepare(rs->apb_pclk);
893
894 return 0;
895}
896
897static int rockchip_spi_runtime_resume(struct device *dev)
898{
899 int ret;
900 struct spi_master *master = dev_get_drvdata(dev);
901 struct rockchip_spi *rs = spi_master_get_devdata(master);
902
903 ret = clk_prepare_enable(rs->apb_pclk);
904 if (ret)
905 return ret;
906
907 ret = clk_prepare_enable(rs->spiclk);
908 if (ret)
909 clk_disable_unprepare(rs->apb_pclk);
910
911 return ret;
912}
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100913#endif /* CONFIG_PM */
addy ke64e36822014-07-01 09:03:59 +0800914
915static const struct dev_pm_ops rockchip_spi_pm = {
916 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
917 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
918 rockchip_spi_runtime_resume, NULL)
919};
920
921static const struct of_device_id rockchip_spi_dt_match[] = {
Caesar Wangaa29ea32016-05-20 07:56:21 +0800922 { .compatible = "rockchip,rk3036-spi", },
addy ke64e36822014-07-01 09:03:59 +0800923 { .compatible = "rockchip,rk3066-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800924 { .compatible = "rockchip,rk3188-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800925 { .compatible = "rockchip,rk3228-spi", },
Addy Keb839b782014-07-11 10:09:19 +0800926 { .compatible = "rockchip,rk3288-spi", },
Caesar Wangaa29ea32016-05-20 07:56:21 +0800927 { .compatible = "rockchip,rk3368-spi", },
Xu Jianqun9b7a5622016-02-18 19:16:31 +0800928 { .compatible = "rockchip,rk3399-spi", },
addy ke64e36822014-07-01 09:03:59 +0800929 { },
930};
931MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
932
933static struct platform_driver rockchip_spi_driver = {
934 .driver = {
935 .name = DRIVER_NAME,
addy ke64e36822014-07-01 09:03:59 +0800936 .pm = &rockchip_spi_pm,
937 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
938 },
939 .probe = rockchip_spi_probe,
940 .remove = rockchip_spi_remove,
941};
942
943module_platform_driver(rockchip_spi_driver);
944
Addy Ke5dcc44e2014-07-11 10:07:56 +0800945MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
addy ke64e36822014-07-01 09:03:59 +0800946MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
947MODULE_LICENSE("GPL v2");