blob: 3082e72e4f6c62ed00651e389381ee2896336c52 [file] [log] [blame]
Fabio Estevam6576bf02018-07-24 10:04:02 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright 2013 Freescale Semiconductor, Inc.
4//
5// Freescale DSPI driver
6// This file contains a driver for the Freescale DSPI
Chao Fu349ad662013-08-16 11:08:55 +08007
Xiubo Lia3108362014-09-29 10:57:06 +08008#include <linux/clk.h>
9#include <linux/delay.h>
Sanchayan Maity90ba3702016-11-10 17:49:15 +053010#include <linux/dmaengine.h>
11#include <linux/dma-mapping.h>
Xiubo Lia3108362014-09-29 10:57:06 +080012#include <linux/err.h>
13#include <linux/errno.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
Chao Fu349ad662013-08-16 11:08:55 +080016#include <linux/kernel.h>
Aaron Brice95bf15f2015-04-03 13:39:31 -070017#include <linux/math64.h>
Chao Fu349ad662013-08-16 11:08:55 +080018#include <linux/module.h>
Chao Fu349ad662013-08-16 11:08:55 +080019#include <linux/of.h>
20#include <linux/of_device.h>
Mirza Krak432a17d2015-06-12 18:55:22 +020021#include <linux/pinctrl/consumer.h>
Xiubo Lia3108362014-09-29 10:57:06 +080022#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/regmap.h>
25#include <linux/sched.h>
26#include <linux/spi/spi.h>
Angelo Dureghelloec7ed772017-10-28 00:23:01 +020027#include <linux/spi/spi-fsl-dspi.h>
Xiubo Lia3108362014-09-29 10:57:06 +080028#include <linux/spi/spi_bitbang.h>
Aaron Brice95bf15f2015-04-03 13:39:31 -070029#include <linux/time.h>
Chao Fu349ad662013-08-16 11:08:55 +080030
31#define DRIVER_NAME "fsl-dspi"
32
Angelo Dureghello5223c9c2018-08-18 01:51:58 +020033#ifdef CONFIG_M5441x
34#define DSPI_FIFO_SIZE 16
35#else
Chao Fu349ad662013-08-16 11:08:55 +080036#define DSPI_FIFO_SIZE 4
Angelo Dureghello5223c9c2018-08-18 01:51:58 +020037#endif
Sanchayan Maity90ba3702016-11-10 17:49:15 +053038#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
Chao Fu349ad662013-08-16 11:08:55 +080039
40#define SPI_MCR 0x00
41#define SPI_MCR_MASTER (1 << 31)
42#define SPI_MCR_PCSIS (0x3F << 16)
43#define SPI_MCR_CLR_TXF (1 << 11)
44#define SPI_MCR_CLR_RXF (1 << 10)
Esben Haabendal3e7cc622018-06-20 09:34:42 +020045#define SPI_MCR_XSPI (1 << 3)
Chao Fu349ad662013-08-16 11:08:55 +080046
47#define SPI_TCR 0x08
Haikun Wangc042af92015-06-09 19:45:37 +080048#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
Chao Fu349ad662013-08-16 11:08:55 +080049
Alexander Stein5cc7b042014-11-04 09:20:18 +010050#define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
Chao Fu349ad662013-08-16 11:08:55 +080051#define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
52#define SPI_CTAR_CPOL(x) ((x) << 26)
53#define SPI_CTAR_CPHA(x) ((x) << 25)
54#define SPI_CTAR_LSBFE(x) ((x) << 24)
Aaron Brice95bf15f2015-04-03 13:39:31 -070055#define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
Chao Fu349ad662013-08-16 11:08:55 +080056#define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
57#define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
58#define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
59#define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
60#define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
61#define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
62#define SPI_CTAR_BR(x) ((x) & 0x0000000f)
Aaron Brice95bf15f2015-04-03 13:39:31 -070063#define SPI_CTAR_SCALE_BITS 0xf
Chao Fu349ad662013-08-16 11:08:55 +080064
65#define SPI_CTAR0_SLAVE 0x0c
66
67#define SPI_SR 0x2c
68#define SPI_SR_EOQF 0x10000000
Haikun Wangd1f4a382015-06-09 19:45:27 +080069#define SPI_SR_TCFQF 0x80000000
Yuan Yao5ee67b52016-10-17 18:02:34 +080070#define SPI_SR_CLEAR 0xdaad0000
Chao Fu349ad662013-08-16 11:08:55 +080071
Sanchayan Maity90ba3702016-11-10 17:49:15 +053072#define SPI_RSER_TFFFE BIT(25)
73#define SPI_RSER_TFFFD BIT(24)
74#define SPI_RSER_RFDFE BIT(17)
75#define SPI_RSER_RFDFD BIT(16)
Chao Fu349ad662013-08-16 11:08:55 +080076
77#define SPI_RSER 0x30
78#define SPI_RSER_EOQFE 0x10000000
Haikun Wangd1f4a382015-06-09 19:45:27 +080079#define SPI_RSER_TCFQE 0x80000000
Chao Fu349ad662013-08-16 11:08:55 +080080
81#define SPI_PUSHR 0x34
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +020082#define SPI_PUSHR_CMD_CONT (1 << 15)
83#define SPI_PUSHR_CONT (SPI_PUSHR_CMD_CONT << 16)
84#define SPI_PUSHR_CMD_CTAS(x) (((x) & 0x0003) << 12)
85#define SPI_PUSHR_CTAS(x) (SPI_PUSHR_CMD_CTAS(x) << 16)
86#define SPI_PUSHR_CMD_EOQ (1 << 11)
87#define SPI_PUSHR_EOQ (SPI_PUSHR_CMD_EOQ << 16)
88#define SPI_PUSHR_CMD_CTCNT (1 << 10)
89#define SPI_PUSHR_CTCNT (SPI_PUSHR_CMD_CTCNT << 16)
90#define SPI_PUSHR_CMD_PCS(x) ((1 << x) & 0x003f)
91#define SPI_PUSHR_PCS(x) (SPI_PUSHR_CMD_PCS(x) << 16)
Chao Fu349ad662013-08-16 11:08:55 +080092#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
93
94#define SPI_PUSHR_SLAVE 0x34
95
96#define SPI_POPR 0x38
97#define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
98
99#define SPI_TXFR0 0x3c
100#define SPI_TXFR1 0x40
101#define SPI_TXFR2 0x44
102#define SPI_TXFR3 0x48
103#define SPI_RXFR0 0x7c
104#define SPI_RXFR1 0x80
105#define SPI_RXFR2 0x84
106#define SPI_RXFR3 0x88
107
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200108#define SPI_CTARE(x) (0x11c + (((x) & 0x3) * 4))
109#define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
110#define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
111
112#define SPI_SREX 0x13c
113
Chao Fu349ad662013-08-16 11:08:55 +0800114#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
115#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
116#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
117#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
118
Esben Haabendal51d583a2018-06-20 09:34:39 +0200119#define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
120#define SPI_FRAME_EBITS_MASK SPI_CTARE_FMSZE(1)
121
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200122/* Register offsets for regmap_pushr */
123#define PUSHR_CMD 0x0
124#define PUSHR_TX 0x2
125
Chao Fu349ad662013-08-16 11:08:55 +0800126#define SPI_CS_INIT 0x01
127#define SPI_CS_ASSERT 0x02
128#define SPI_CS_DROP 0x04
129
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530130#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
131
Chao Fu349ad662013-08-16 11:08:55 +0800132struct chip_data {
Chao Fu349ad662013-08-16 11:08:55 +0800133 u32 ctar_val;
134 u16 void_write_data;
135};
136
Haikun Wangd1f4a382015-06-09 19:45:27 +0800137enum dspi_trans_mode {
138 DSPI_EOQ_MODE = 0,
139 DSPI_TCFQ_MODE,
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530140 DSPI_DMA_MODE,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800141};
142
143struct fsl_dspi_devtype_data {
144 enum dspi_trans_mode trans_mode;
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530145 u8 max_clock_factor;
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200146 bool xspi_mode;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800147};
148
149static const struct fsl_dspi_devtype_data vf610_data = {
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530150 .trans_mode = DSPI_DMA_MODE,
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530151 .max_clock_factor = 2,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800152};
153
154static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
155 .trans_mode = DSPI_TCFQ_MODE,
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530156 .max_clock_factor = 8,
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200157 .xspi_mode = true,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800158};
159
160static const struct fsl_dspi_devtype_data ls2085a_data = {
161 .trans_mode = DSPI_TCFQ_MODE,
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530162 .max_clock_factor = 8,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800163};
164
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200165static const struct fsl_dspi_devtype_data coldfire_data = {
166 .trans_mode = DSPI_EOQ_MODE,
167 .max_clock_factor = 8,
168};
169
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530170struct fsl_dspi_dma {
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530171 /* Length of transfer in words of DSPI_FIFO_SIZE */
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530172 u32 curr_xfer_len;
173
174 u32 *tx_dma_buf;
175 struct dma_chan *chan_tx;
176 dma_addr_t tx_dma_phys;
177 struct completion cmd_tx_complete;
178 struct dma_async_tx_descriptor *tx_desc;
179
180 u32 *rx_dma_buf;
181 struct dma_chan *chan_rx;
182 dma_addr_t rx_dma_phys;
183 struct completion cmd_rx_complete;
184 struct dma_async_tx_descriptor *rx_desc;
185};
186
Chao Fu349ad662013-08-16 11:08:55 +0800187struct fsl_dspi {
Chao Fu9298bc72015-01-27 16:27:22 +0530188 struct spi_master *master;
Chao Fu349ad662013-08-16 11:08:55 +0800189 struct platform_device *pdev;
190
Chao Fu1acbdeb2014-02-12 15:29:05 +0800191 struct regmap *regmap;
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200192 struct regmap *regmap_pushr;
Chao Fu349ad662013-08-16 11:08:55 +0800193 int irq;
Chao Fu88386e82014-02-12 15:29:06 +0800194 struct clk *clk;
Chao Fu349ad662013-08-16 11:08:55 +0800195
Chao Fu88386e82014-02-12 15:29:06 +0800196 struct spi_transfer *cur_transfer;
Chao Fu9298bc72015-01-27 16:27:22 +0530197 struct spi_message *cur_msg;
Chao Fu349ad662013-08-16 11:08:55 +0800198 struct chip_data *cur_chip;
199 size_t len;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200200 const void *tx;
Chao Fu349ad662013-08-16 11:08:55 +0800201 void *rx;
202 void *rx_end;
Chao Fu349ad662013-08-16 11:08:55 +0800203 u16 void_write_data;
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +0200204 u16 tx_cmd;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200205 u8 bits_per_word;
206 u8 bytes_per_word;
LABBE Corentin94b968b2016-08-16 11:50:20 +0200207 const struct fsl_dspi_devtype_data *devtype_data;
Chao Fu349ad662013-08-16 11:08:55 +0800208
Chao Fu88386e82014-02-12 15:29:06 +0800209 wait_queue_head_t waitq;
210 u32 waitflags;
Haikun Wangc042af92015-06-09 19:45:37 +0800211
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530212 struct fsl_dspi_dma *dma;
Chao Fu349ad662013-08-16 11:08:55 +0800213};
214
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200215static u32 dspi_pop_tx(struct fsl_dspi *dspi)
Chao Fu349ad662013-08-16 11:08:55 +0800216{
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200217 u32 txdata = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800218
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200219 if (dspi->tx) {
220 if (dspi->bytes_per_word == 1)
221 txdata = *(u8 *)dspi->tx;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200222 else if (dspi->bytes_per_word == 2)
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200223 txdata = *(u16 *)dspi->tx;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200224 else /* dspi->bytes_per_word == 4 */
225 txdata = *(u32 *)dspi->tx;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200226 dspi->tx += dspi->bytes_per_word;
227 }
228 dspi->len -= dspi->bytes_per_word;
229 return txdata;
230}
Chao Fu349ad662013-08-16 11:08:55 +0800231
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200232static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
233{
234 u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
235
236 if (dspi->len > 0)
237 cmd |= SPI_PUSHR_CMD_CONT;
238 return cmd << 16 | data;
239}
240
241static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
242{
243 if (!dspi->rx)
244 return;
245
246 /* Mask of undefined bits */
247 rxdata &= (1 << dspi->bits_per_word) - 1;
248
249 if (dspi->bytes_per_word == 1)
250 *(u8 *)dspi->rx = rxdata;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200251 else if (dspi->bytes_per_word == 2)
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200252 *(u16 *)dspi->rx = rxdata;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200253 else /* dspi->bytes_per_word == 4 */
254 *(u32 *)dspi->rx = rxdata;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200255 dspi->rx += dspi->bytes_per_word;
Chao Fu349ad662013-08-16 11:08:55 +0800256}
257
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530258static void dspi_tx_dma_callback(void *arg)
259{
260 struct fsl_dspi *dspi = arg;
261 struct fsl_dspi_dma *dma = dspi->dma;
262
263 complete(&dma->cmd_tx_complete);
264}
265
266static void dspi_rx_dma_callback(void *arg)
267{
268 struct fsl_dspi *dspi = arg;
269 struct fsl_dspi_dma *dma = dspi->dma;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530270 int i;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530271
Esben Haabendal4779f232018-06-20 09:34:32 +0200272 if (dspi->rx) {
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200273 for (i = 0; i < dma->curr_xfer_len; i++)
274 dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530275 }
276
277 complete(&dma->cmd_rx_complete);
278}
279
280static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
281{
282 struct fsl_dspi_dma *dma = dspi->dma;
283 struct device *dev = &dspi->pdev->dev;
284 int time_left;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530285 int i;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530286
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200287 for (i = 0; i < dma->curr_xfer_len; i++)
288 dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530289
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530290 dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
291 dma->tx_dma_phys,
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530292 dma->curr_xfer_len *
293 DMA_SLAVE_BUSWIDTH_4_BYTES,
294 DMA_MEM_TO_DEV,
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530295 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
296 if (!dma->tx_desc) {
297 dev_err(dev, "Not able to get desc for DMA xfer\n");
298 return -EIO;
299 }
300
301 dma->tx_desc->callback = dspi_tx_dma_callback;
302 dma->tx_desc->callback_param = dspi;
303 if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
304 dev_err(dev, "DMA submit failed\n");
305 return -EINVAL;
306 }
307
308 dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
309 dma->rx_dma_phys,
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530310 dma->curr_xfer_len *
311 DMA_SLAVE_BUSWIDTH_4_BYTES,
312 DMA_DEV_TO_MEM,
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530313 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
314 if (!dma->rx_desc) {
315 dev_err(dev, "Not able to get desc for DMA xfer\n");
316 return -EIO;
317 }
318
319 dma->rx_desc->callback = dspi_rx_dma_callback;
320 dma->rx_desc->callback_param = dspi;
321 if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
322 dev_err(dev, "DMA submit failed\n");
323 return -EINVAL;
324 }
325
326 reinit_completion(&dspi->dma->cmd_rx_complete);
327 reinit_completion(&dspi->dma->cmd_tx_complete);
328
329 dma_async_issue_pending(dma->chan_rx);
330 dma_async_issue_pending(dma->chan_tx);
331
332 time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
333 DMA_COMPLETION_TIMEOUT);
334 if (time_left == 0) {
335 dev_err(dev, "DMA tx timeout\n");
336 dmaengine_terminate_all(dma->chan_tx);
337 dmaengine_terminate_all(dma->chan_rx);
338 return -ETIMEDOUT;
339 }
340
341 time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
342 DMA_COMPLETION_TIMEOUT);
343 if (time_left == 0) {
344 dev_err(dev, "DMA rx timeout\n");
345 dmaengine_terminate_all(dma->chan_tx);
346 dmaengine_terminate_all(dma->chan_rx);
347 return -ETIMEDOUT;
348 }
349
350 return 0;
351}
352
353static int dspi_dma_xfer(struct fsl_dspi *dspi)
354{
355 struct fsl_dspi_dma *dma = dspi->dma;
356 struct device *dev = &dspi->pdev->dev;
Andrey Smirnov5f8f8032018-07-16 21:33:29 -0700357 struct spi_message *message = dspi->cur_msg;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530358 int curr_remaining_bytes;
359 int bytes_per_buffer;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530360 int ret = 0;
361
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530362 curr_remaining_bytes = dspi->len;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530363 bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530364 while (curr_remaining_bytes) {
365 /* Check if current transfer fits the DMA buffer */
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200366 dma->curr_xfer_len = curr_remaining_bytes
367 / dspi->bytes_per_word;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530368 if (dma->curr_xfer_len > bytes_per_buffer)
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530369 dma->curr_xfer_len = bytes_per_buffer;
370
371 ret = dspi_next_xfer_dma_submit(dspi);
372 if (ret) {
373 dev_err(dev, "DMA transfer failed\n");
374 goto exit;
375
376 } else {
Andrey Smirnov5f8f8032018-07-16 21:33:29 -0700377 const int len =
378 dma->curr_xfer_len * dspi->bytes_per_word;
379 curr_remaining_bytes -= len;
380 message->actual_length += len;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530381 if (curr_remaining_bytes < 0)
382 curr_remaining_bytes = 0;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530383 }
384 }
385
386exit:
387 return ret;
388}
389
390static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
391{
392 struct fsl_dspi_dma *dma;
393 struct dma_slave_config cfg;
394 struct device *dev = &dspi->pdev->dev;
395 int ret;
396
397 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
398 if (!dma)
399 return -ENOMEM;
400
401 dma->chan_rx = dma_request_slave_channel(dev, "rx");
402 if (!dma->chan_rx) {
403 dev_err(dev, "rx dma channel not available\n");
404 ret = -ENODEV;
405 return ret;
406 }
407
408 dma->chan_tx = dma_request_slave_channel(dev, "tx");
409 if (!dma->chan_tx) {
410 dev_err(dev, "tx dma channel not available\n");
411 ret = -ENODEV;
412 goto err_tx_channel;
413 }
414
415 dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
416 &dma->tx_dma_phys, GFP_KERNEL);
417 if (!dma->tx_dma_buf) {
418 ret = -ENOMEM;
419 goto err_tx_dma_buf;
420 }
421
422 dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
423 &dma->rx_dma_phys, GFP_KERNEL);
424 if (!dma->rx_dma_buf) {
425 ret = -ENOMEM;
426 goto err_rx_dma_buf;
427 }
428
429 cfg.src_addr = phy_addr + SPI_POPR;
430 cfg.dst_addr = phy_addr + SPI_PUSHR;
431 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
432 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
433 cfg.src_maxburst = 1;
434 cfg.dst_maxburst = 1;
435
436 cfg.direction = DMA_DEV_TO_MEM;
437 ret = dmaengine_slave_config(dma->chan_rx, &cfg);
438 if (ret) {
439 dev_err(dev, "can't configure rx dma channel\n");
440 ret = -EINVAL;
441 goto err_slave_config;
442 }
443
444 cfg.direction = DMA_MEM_TO_DEV;
445 ret = dmaengine_slave_config(dma->chan_tx, &cfg);
446 if (ret) {
447 dev_err(dev, "can't configure tx dma channel\n");
448 ret = -EINVAL;
449 goto err_slave_config;
450 }
451
452 dspi->dma = dma;
453 init_completion(&dma->cmd_tx_complete);
454 init_completion(&dma->cmd_rx_complete);
455
456 return 0;
457
458err_slave_config:
Sanchayan Maity27d21e92016-11-22 12:31:32 +0530459 dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
460 dma->rx_dma_buf, dma->rx_dma_phys);
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530461err_rx_dma_buf:
Sanchayan Maity27d21e92016-11-22 12:31:32 +0530462 dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
463 dma->tx_dma_buf, dma->tx_dma_phys);
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530464err_tx_dma_buf:
465 dma_release_channel(dma->chan_tx);
466err_tx_channel:
467 dma_release_channel(dma->chan_rx);
468
469 devm_kfree(dev, dma);
470 dspi->dma = NULL;
471
472 return ret;
473}
474
475static void dspi_release_dma(struct fsl_dspi *dspi)
476{
477 struct fsl_dspi_dma *dma = dspi->dma;
478 struct device *dev = &dspi->pdev->dev;
479
480 if (dma) {
481 if (dma->chan_tx) {
482 dma_unmap_single(dev, dma->tx_dma_phys,
483 DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
484 dma_release_channel(dma->chan_tx);
485 }
486
487 if (dma->chan_rx) {
488 dma_unmap_single(dev, dma->rx_dma_phys,
489 DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
490 dma_release_channel(dma->chan_rx);
491 }
492 }
493}
494
Chao Fu349ad662013-08-16 11:08:55 +0800495static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
496 unsigned long clkrate)
497{
498 /* Valid baud rate pre-scaler values */
499 int pbr_tbl[4] = {2, 3, 5, 7};
500 int brs[16] = { 2, 4, 6, 8,
501 16, 32, 64, 128,
502 256, 512, 1024, 2048,
503 4096, 8192, 16384, 32768 };
Aaron Brice6fd63082015-03-30 10:49:15 -0700504 int scale_needed, scale, minscale = INT_MAX;
505 int i, j;
Chao Fu349ad662013-08-16 11:08:55 +0800506
Aaron Brice6fd63082015-03-30 10:49:15 -0700507 scale_needed = clkrate / speed_hz;
Aaron Bricee689d6d2015-04-03 13:39:29 -0700508 if (clkrate % speed_hz)
509 scale_needed++;
Chao Fu349ad662013-08-16 11:08:55 +0800510
Aaron Brice6fd63082015-03-30 10:49:15 -0700511 for (i = 0; i < ARRAY_SIZE(brs); i++)
512 for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
513 scale = brs[i] * pbr_tbl[j];
514 if (scale >= scale_needed) {
515 if (scale < minscale) {
516 minscale = scale;
517 *br = i;
518 *pbr = j;
519 }
520 break;
Chao Fu349ad662013-08-16 11:08:55 +0800521 }
522 }
523
Aaron Brice6fd63082015-03-30 10:49:15 -0700524 if (minscale == INT_MAX) {
525 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
526 speed_hz, clkrate);
527 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
528 *br = ARRAY_SIZE(brs) - 1;
529 }
Chao Fu349ad662013-08-16 11:08:55 +0800530}
531
Aaron Brice95bf15f2015-04-03 13:39:31 -0700532static void ns_delay_scale(char *psc, char *sc, int delay_ns,
533 unsigned long clkrate)
534{
535 int pscale_tbl[4] = {1, 3, 5, 7};
536 int scale_needed, scale, minscale = INT_MAX;
537 int i, j;
538 u32 remainder;
539
540 scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
541 &remainder);
542 if (remainder)
543 scale_needed++;
544
545 for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
546 for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
547 scale = pscale_tbl[i] * (2 << j);
548 if (scale >= scale_needed) {
549 if (scale < minscale) {
550 minscale = scale;
551 *psc = i;
552 *sc = j;
553 }
554 break;
555 }
556 }
557
558 if (minscale == INT_MAX) {
559 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
560 delay_ns, clkrate);
561 *psc = ARRAY_SIZE(pscale_tbl) - 1;
562 *sc = SPI_CTAR_SCALE_BITS;
563 }
Chao Fu349ad662013-08-16 11:08:55 +0800564}
565
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200566static void fifo_write(struct fsl_dspi *dspi)
Haikun Wangd1f4a382015-06-09 19:45:27 +0800567{
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200568 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pop_tx_pushr(dspi));
Haikun Wangd1f4a382015-06-09 19:45:27 +0800569}
570
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200571static void cmd_fifo_write(struct fsl_dspi *dspi)
572{
573 u16 cmd = dspi->tx_cmd;
574
575 if (dspi->len > 0)
576 cmd |= SPI_PUSHR_CMD_CONT;
577 regmap_write(dspi->regmap_pushr, PUSHR_CMD, cmd);
578}
579
580static void tx_fifo_write(struct fsl_dspi *dspi, u16 txdata)
581{
582 regmap_write(dspi->regmap_pushr, PUSHR_TX, txdata);
583}
584
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200585static void dspi_tcfq_write(struct fsl_dspi *dspi)
Haikun Wangd1f4a382015-06-09 19:45:27 +0800586{
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200587 /* Clear transfer count */
588 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200589
590 if (dspi->devtype_data->xspi_mode && dspi->bits_per_word > 16) {
591 /* Write two TX FIFO entries first, and then the corresponding
592 * CMD FIFO entry.
593 */
594 u32 data = dspi_pop_tx(dspi);
595
596 if (dspi->cur_chip->ctar_val & SPI_CTAR_LSBFE(1)) {
597 /* LSB */
598 tx_fifo_write(dspi, data & 0xFFFF);
599 tx_fifo_write(dspi, data >> 16);
600 } else {
601 /* MSB */
602 tx_fifo_write(dspi, data >> 16);
603 tx_fifo_write(dspi, data & 0xFFFF);
604 }
605 cmd_fifo_write(dspi);
606 } else {
607 /* Write one entry to both TX FIFO and CMD FIFO
608 * simultaneously.
609 */
610 fifo_write(dspi);
611 }
Haikun Wangd1f4a382015-06-09 19:45:27 +0800612}
613
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200614static u32 fifo_read(struct fsl_dspi *dspi)
Chao Fu349ad662013-08-16 11:08:55 +0800615{
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200616 u32 rxdata = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800617
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200618 regmap_read(dspi->regmap, SPI_POPR, &rxdata);
619 return rxdata;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800620}
621
622static void dspi_tcfq_read(struct fsl_dspi *dspi)
623{
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200624 dspi_push_rx(dspi, fifo_read(dspi));
625}
Haikun Wangd1f4a382015-06-09 19:45:27 +0800626
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200627static void dspi_eoq_write(struct fsl_dspi *dspi)
628{
629 int fifo_size = DSPI_FIFO_SIZE;
Angelo Dureghello5223c9c2018-08-18 01:51:58 +0200630 u16 xfer_cmd = dspi->tx_cmd;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800631
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200632 /* Fill TX FIFO with as many transfers as possible */
633 while (dspi->len && fifo_size--) {
Angelo Dureghello5223c9c2018-08-18 01:51:58 +0200634 dspi->tx_cmd = xfer_cmd;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200635 /* Request EOQF for last transfer in FIFO */
636 if (dspi->len == dspi->bytes_per_word || fifo_size == 0)
637 dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ;
638 /* Clear transfer count for first transfer in FIFO */
639 if (fifo_size == (DSPI_FIFO_SIZE - 1))
640 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
641 /* Write combined TX FIFO and CMD FIFO entry */
642 fifo_write(dspi);
643 }
644}
645
646static void dspi_eoq_read(struct fsl_dspi *dspi)
647{
648 int fifo_size = DSPI_FIFO_SIZE;
649
650 /* Read one FIFO entry at and push to rx buffer */
651 while ((dspi->rx < dspi->rx_end) && fifo_size--)
652 dspi_push_rx(dspi, fifo_read(dspi));
Haikun Wangd1f4a382015-06-09 19:45:27 +0800653}
654
Chao Fu9298bc72015-01-27 16:27:22 +0530655static int dspi_transfer_one_message(struct spi_master *master,
656 struct spi_message *message)
Chao Fu349ad662013-08-16 11:08:55 +0800657{
Chao Fu9298bc72015-01-27 16:27:22 +0530658 struct fsl_dspi *dspi = spi_master_get_devdata(master);
659 struct spi_device *spi = message->spi;
660 struct spi_transfer *transfer;
661 int status = 0;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800662 enum dspi_trans_mode trans_mode;
663
Chao Fu9298bc72015-01-27 16:27:22 +0530664 message->actual_length = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800665
Chao Fu9298bc72015-01-27 16:27:22 +0530666 list_for_each_entry(transfer, &message->transfers, transfer_list) {
667 dspi->cur_transfer = transfer;
668 dspi->cur_msg = message;
669 dspi->cur_chip = spi_get_ctldata(spi);
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +0200670 /* Prepare command word for CMD FIFO */
671 dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
672 SPI_PUSHR_CMD_PCS(spi->chip_select);
Andrey Vostrikov92dc20d2016-04-05 15:33:14 +0300673 if (list_is_last(&dspi->cur_transfer->transfer_list,
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +0200674 &dspi->cur_msg->transfers)) {
675 /* Leave PCS activated after last transfer when
676 * cs_change is set.
677 */
678 if (transfer->cs_change)
679 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
680 } else {
681 /* Keep PCS active between transfers in same message
682 * when cs_change is not set, and de-activate PCS
683 * between transfers in the same message when
684 * cs_change is set.
685 */
686 if (!transfer->cs_change)
687 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
688 }
689
Chao Fu9298bc72015-01-27 16:27:22 +0530690 dspi->void_write_data = dspi->cur_chip->void_write_data;
Chao Fu349ad662013-08-16 11:08:55 +0800691
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200692 dspi->tx = transfer->tx_buf;
Chao Fu9298bc72015-01-27 16:27:22 +0530693 dspi->rx = transfer->rx_buf;
694 dspi->rx_end = dspi->rx + transfer->len;
695 dspi->len = transfer->len;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200696 /* Validated transfer specific frame size (defaults applied) */
697 dspi->bits_per_word = transfer->bits_per_word;
698 if (transfer->bits_per_word <= 8)
699 dspi->bytes_per_word = 1;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200700 else if (transfer->bits_per_word <= 16)
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200701 dspi->bytes_per_word = 2;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200702 else
703 dspi->bytes_per_word = 4;
Chao Fu349ad662013-08-16 11:08:55 +0800704
Chao Fu9298bc72015-01-27 16:27:22 +0530705 regmap_update_bits(dspi->regmap, SPI_MCR,
Esben Haabendald87e08f2018-06-20 09:34:37 +0200706 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
707 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
Bhuvanchandra DVef22d162015-12-10 11:25:30 +0530708 regmap_write(dspi->regmap, SPI_CTAR(0),
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200709 dspi->cur_chip->ctar_val |
710 SPI_FRAME_BITS(transfer->bits_per_word));
Esben Haabendal51d583a2018-06-20 09:34:39 +0200711 if (dspi->devtype_data->xspi_mode)
712 regmap_write(dspi->regmap, SPI_CTARE(0),
713 SPI_FRAME_EBITS(transfer->bits_per_word)
714 | SPI_CTARE_DTCP(1));
Chao Fu349ad662013-08-16 11:08:55 +0800715
Haikun Wangd1f4a382015-06-09 19:45:27 +0800716 trans_mode = dspi->devtype_data->trans_mode;
717 switch (trans_mode) {
718 case DSPI_EOQ_MODE:
719 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
Haikun Wangc042af92015-06-09 19:45:37 +0800720 dspi_eoq_write(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800721 break;
722 case DSPI_TCFQ_MODE:
723 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
Haikun Wangc042af92015-06-09 19:45:37 +0800724 dspi_tcfq_write(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800725 break;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530726 case DSPI_DMA_MODE:
727 regmap_write(dspi->regmap, SPI_RSER,
728 SPI_RSER_TFFFE | SPI_RSER_TFFFD |
729 SPI_RSER_RFDFE | SPI_RSER_RFDFD);
730 status = dspi_dma_xfer(dspi);
Sanchayan Maity98114302016-11-17 17:46:48 +0530731 break;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800732 default:
733 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
734 trans_mode);
735 status = -EINVAL;
736 goto out;
737 }
Chao Fu349ad662013-08-16 11:08:55 +0800738
Sanchayan Maity98114302016-11-17 17:46:48 +0530739 if (trans_mode != DSPI_DMA_MODE) {
740 if (wait_event_interruptible(dspi->waitq,
741 dspi->waitflags))
742 dev_err(&dspi->pdev->dev,
743 "wait transfer complete fail!\n");
744 dspi->waitflags = 0;
745 }
Chao Fu349ad662013-08-16 11:08:55 +0800746
Chao Fu9298bc72015-01-27 16:27:22 +0530747 if (transfer->delay_usecs)
748 udelay(transfer->delay_usecs);
Chao Fu349ad662013-08-16 11:08:55 +0800749 }
750
Haikun Wangd1f4a382015-06-09 19:45:27 +0800751out:
Chao Fu9298bc72015-01-27 16:27:22 +0530752 message->status = status;
753 spi_finalize_current_message(master);
754
755 return status;
Chao Fu349ad662013-08-16 11:08:55 +0800756}
757
Chao Fu9298bc72015-01-27 16:27:22 +0530758static int dspi_setup(struct spi_device *spi)
Chao Fu349ad662013-08-16 11:08:55 +0800759{
760 struct chip_data *chip;
761 struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200762 struct fsl_dspi_platform_data *pdata;
Aaron Brice95bf15f2015-04-03 13:39:31 -0700763 u32 cs_sck_delay = 0, sck_cs_delay = 0;
764 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200765 unsigned char pasc = 0, asc = 0;
Aaron Brice95bf15f2015-04-03 13:39:31 -0700766 unsigned long clkrate;
Chao Fu349ad662013-08-16 11:08:55 +0800767
768 /* Only alloc on first setup */
769 chip = spi_get_ctldata(spi);
770 if (chip == NULL) {
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530771 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Chao Fu349ad662013-08-16 11:08:55 +0800772 if (!chip)
773 return -ENOMEM;
774 }
775
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200776 pdata = dev_get_platdata(&dspi->pdev->dev);
Aaron Brice95bf15f2015-04-03 13:39:31 -0700777
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200778 if (!pdata) {
779 of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
780 &cs_sck_delay);
781
782 of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
783 &sck_cs_delay);
784 } else {
785 cs_sck_delay = pdata->cs_sck_delay;
786 sck_cs_delay = pdata->sck_cs_delay;
787 }
Aaron Brice95bf15f2015-04-03 13:39:31 -0700788
Chao Fu349ad662013-08-16 11:08:55 +0800789 chip->void_write_data = 0;
790
Aaron Brice95bf15f2015-04-03 13:39:31 -0700791 clkrate = clk_get_rate(dspi->clk);
792 hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
793
794 /* Set PCS to SCK delay scale values */
795 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
796
797 /* Set After SCK delay scale values */
798 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
Chao Fu349ad662013-08-16 11:08:55 +0800799
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200800 chip->ctar_val = SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
Chao Fu349ad662013-08-16 11:08:55 +0800801 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
802 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
Aaron Brice95bf15f2015-04-03 13:39:31 -0700803 | SPI_CTAR_PCSSCK(pcssck)
804 | SPI_CTAR_CSSCK(cssck)
805 | SPI_CTAR_PASC(pasc)
806 | SPI_CTAR_ASC(asc)
Chao Fu349ad662013-08-16 11:08:55 +0800807 | SPI_CTAR_PBR(pbr)
808 | SPI_CTAR_BR(br);
809
810 spi_set_ctldata(spi, chip);
811
812 return 0;
813}
814
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530815static void dspi_cleanup(struct spi_device *spi)
816{
817 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
818
819 dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
820 spi->master->bus_num, spi->chip_select);
821
822 kfree(chip);
823}
824
Chao Fu349ad662013-08-16 11:08:55 +0800825static irqreturn_t dspi_interrupt(int irq, void *dev_id)
826{
827 struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
Chao Fu9298bc72015-01-27 16:27:22 +0530828 struct spi_message *msg = dspi->cur_msg;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800829 enum dspi_trans_mode trans_mode;
Haikun Wangc042af92015-06-09 19:45:37 +0800830 u32 spi_sr, spi_tcr;
Esben Haabendal0a4ec2c2018-06-20 09:34:34 +0200831 u16 spi_tcnt;
Chao Fu349ad662013-08-16 11:08:55 +0800832
Haikun Wangd1f4a382015-06-09 19:45:27 +0800833 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
834 regmap_write(dspi->regmap, SPI_SR, spi_sr);
835
Chao Fu349ad662013-08-16 11:08:55 +0800836
Haikun Wangc042af92015-06-09 19:45:37 +0800837 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
Esben Haabendal0a4ec2c2018-06-20 09:34:34 +0200838 /* Get transfer counter (in number of SPI transfers). It was
839 * reset to 0 when transfer(s) were started.
840 */
Haikun Wangc042af92015-06-09 19:45:37 +0800841 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
842 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
Esben Haabendal0a4ec2c2018-06-20 09:34:34 +0200843 /* Update total number of bytes that were transferred */
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200844 msg->actual_length += spi_tcnt * dspi->bytes_per_word;
Haikun Wangc042af92015-06-09 19:45:37 +0800845
846 trans_mode = dspi->devtype_data->trans_mode;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800847 switch (trans_mode) {
848 case DSPI_EOQ_MODE:
Haikun Wangc042af92015-06-09 19:45:37 +0800849 dspi_eoq_read(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800850 break;
851 case DSPI_TCFQ_MODE:
Haikun Wangc042af92015-06-09 19:45:37 +0800852 dspi_tcfq_read(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800853 break;
854 default:
855 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
856 trans_mode);
Haikun Wangc042af92015-06-09 19:45:37 +0800857 return IRQ_HANDLED;
858 }
859
860 if (!dspi->len) {
Haikun Wangc042af92015-06-09 19:45:37 +0800861 dspi->waitflags = 1;
862 wake_up_interruptible(&dspi->waitq);
863 } else {
864 switch (trans_mode) {
865 case DSPI_EOQ_MODE:
866 dspi_eoq_write(dspi);
867 break;
868 case DSPI_TCFQ_MODE:
869 dspi_tcfq_write(dspi);
870 break;
871 default:
872 dev_err(&dspi->pdev->dev,
873 "unsupported trans_mode %u\n",
874 trans_mode);
875 }
Haikun Wangd1f4a382015-06-09 19:45:27 +0800876 }
877 }
Haikun Wangc042af92015-06-09 19:45:37 +0800878
Chao Fu349ad662013-08-16 11:08:55 +0800879 return IRQ_HANDLED;
880}
881
Jingoo Han790d1902014-05-07 16:45:41 +0900882static const struct of_device_id fsl_dspi_dt_ids[] = {
Julia Lawall230c08b2018-01-02 14:28:06 +0100883 { .compatible = "fsl,vf610-dspi", .data = &vf610_data, },
884 { .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, },
885 { .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, },
Chao Fu349ad662013-08-16 11:08:55 +0800886 { /* sentinel */ }
887};
888MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
889
890#ifdef CONFIG_PM_SLEEP
891static int dspi_suspend(struct device *dev)
892{
893 struct spi_master *master = dev_get_drvdata(dev);
894 struct fsl_dspi *dspi = spi_master_get_devdata(master);
895
896 spi_master_suspend(master);
897 clk_disable_unprepare(dspi->clk);
898
Mirza Krak432a17d2015-06-12 18:55:22 +0200899 pinctrl_pm_select_sleep_state(dev);
900
Chao Fu349ad662013-08-16 11:08:55 +0800901 return 0;
902}
903
904static int dspi_resume(struct device *dev)
905{
Chao Fu349ad662013-08-16 11:08:55 +0800906 struct spi_master *master = dev_get_drvdata(dev);
907 struct fsl_dspi *dspi = spi_master_get_devdata(master);
Fabio Estevam1c5ea2b2016-08-21 23:05:30 -0300908 int ret;
Chao Fu349ad662013-08-16 11:08:55 +0800909
Mirza Krak432a17d2015-06-12 18:55:22 +0200910 pinctrl_pm_select_default_state(dev);
911
Fabio Estevam1c5ea2b2016-08-21 23:05:30 -0300912 ret = clk_prepare_enable(dspi->clk);
913 if (ret)
914 return ret;
Chao Fu349ad662013-08-16 11:08:55 +0800915 spi_master_resume(master);
916
917 return 0;
918}
919#endif /* CONFIG_PM_SLEEP */
920
Jingoo Hanba811ad2014-02-26 10:30:14 +0900921static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
Chao Fu349ad662013-08-16 11:08:55 +0800922
Esben Haabendal85700432018-06-20 09:34:36 +0200923static const struct regmap_range dspi_volatile_ranges[] = {
924 regmap_reg_range(SPI_MCR, SPI_TCR),
925 regmap_reg_range(SPI_SR, SPI_SR),
926 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
927};
928
929static const struct regmap_access_table dspi_volatile_table = {
930 .yes_ranges = dspi_volatile_ranges,
931 .n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges),
932};
933
Xiubo Li409851c2014-10-09 11:27:45 +0800934static const struct regmap_config dspi_regmap_config = {
Chao Fu1acbdeb2014-02-12 15:29:05 +0800935 .reg_bits = 32,
936 .val_bits = 32,
937 .reg_stride = 4,
938 .max_register = 0x88,
Esben Haabendal85700432018-06-20 09:34:36 +0200939 .volatile_table = &dspi_volatile_table,
Chao Fu349ad662013-08-16 11:08:55 +0800940};
941
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200942static const struct regmap_range dspi_xspi_volatile_ranges[] = {
943 regmap_reg_range(SPI_MCR, SPI_TCR),
944 regmap_reg_range(SPI_SR, SPI_SR),
945 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
946 regmap_reg_range(SPI_SREX, SPI_SREX),
947};
948
949static const struct regmap_access_table dspi_xspi_volatile_table = {
950 .yes_ranges = dspi_xspi_volatile_ranges,
951 .n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges),
952};
953
954static const struct regmap_config dspi_xspi_regmap_config[] = {
955 {
956 .reg_bits = 32,
957 .val_bits = 32,
958 .reg_stride = 4,
959 .max_register = 0x13c,
960 .volatile_table = &dspi_xspi_volatile_table,
961 },
962 {
963 .name = "pushr",
964 .reg_bits = 16,
965 .val_bits = 16,
966 .reg_stride = 2,
967 .max_register = 0x2,
968 },
969};
970
Yuan Yao5ee67b52016-10-17 18:02:34 +0800971static void dspi_init(struct fsl_dspi *dspi)
972{
Esben Haabendal3e7cc622018-06-20 09:34:42 +0200973 regmap_write(dspi->regmap, SPI_MCR, SPI_MCR_MASTER | SPI_MCR_PCSIS |
974 (dspi->devtype_data->xspi_mode ? SPI_MCR_XSPI : 0));
Yuan Yao5ee67b52016-10-17 18:02:34 +0800975 regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
Esben Haabendal51d583a2018-06-20 09:34:39 +0200976 if (dspi->devtype_data->xspi_mode)
977 regmap_write(dspi->regmap, SPI_CTARE(0),
978 SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
Yuan Yao5ee67b52016-10-17 18:02:34 +0800979}
980
Chao Fu349ad662013-08-16 11:08:55 +0800981static int dspi_probe(struct platform_device *pdev)
982{
983 struct device_node *np = pdev->dev.of_node;
984 struct spi_master *master;
985 struct fsl_dspi *dspi;
986 struct resource *res;
Esben Haabendal58ba07e2018-06-20 09:34:38 +0200987 const struct regmap_config *regmap_config;
Chao Fu1acbdeb2014-02-12 15:29:05 +0800988 void __iomem *base;
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200989 struct fsl_dspi_platform_data *pdata;
Chao Fu349ad662013-08-16 11:08:55 +0800990 int ret = 0, cs_num, bus_num;
991
992 master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
993 if (!master)
994 return -ENOMEM;
995
996 dspi = spi_master_get_devdata(master);
997 dspi->pdev = pdev;
Chao Fu9298bc72015-01-27 16:27:22 +0530998 dspi->master = master;
999
1000 master->transfer = NULL;
1001 master->setup = dspi_setup;
1002 master->transfer_one_message = dspi_transfer_one_message;
1003 master->dev.of_node = pdev->dev.of_node;
Chao Fu349ad662013-08-16 11:08:55 +08001004
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +05301005 master->cleanup = dspi_cleanup;
Kurt Kanzenbach00ac9562017-11-13 08:47:21 +01001006 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Chao Fu349ad662013-08-16 11:08:55 +08001007
Angelo Dureghelloec7ed772017-10-28 00:23:01 +02001008 pdata = dev_get_platdata(&pdev->dev);
1009 if (pdata) {
1010 master->num_chipselect = pdata->cs_num;
1011 master->bus_num = pdata->bus_num;
Chao Fu349ad662013-08-16 11:08:55 +08001012
Angelo Dureghelloec7ed772017-10-28 00:23:01 +02001013 dspi->devtype_data = &coldfire_data;
1014 } else {
Chao Fu349ad662013-08-16 11:08:55 +08001015
Angelo Dureghelloec7ed772017-10-28 00:23:01 +02001016 ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
1017 if (ret < 0) {
1018 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
1019 goto out_master_put;
1020 }
1021 master->num_chipselect = cs_num;
1022
1023 ret = of_property_read_u32(np, "bus-num", &bus_num);
1024 if (ret < 0) {
1025 dev_err(&pdev->dev, "can't get bus-num\n");
1026 goto out_master_put;
1027 }
1028 master->bus_num = bus_num;
1029
1030 dspi->devtype_data = of_device_get_match_data(&pdev->dev);
1031 if (!dspi->devtype_data) {
1032 dev_err(&pdev->dev, "can't get devtype_data\n");
1033 ret = -EFAULT;
1034 goto out_master_put;
1035 }
Haikun Wangd1f4a382015-06-09 19:45:27 +08001036 }
1037
Esben Haabendal35c9d462018-06-20 09:34:41 +02001038 if (dspi->devtype_data->xspi_mode)
1039 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1040 else
1041 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1042
Chao Fu349ad662013-08-16 11:08:55 +08001043 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chao Fu1acbdeb2014-02-12 15:29:05 +08001044 base = devm_ioremap_resource(&pdev->dev, res);
1045 if (IS_ERR(base)) {
1046 ret = PTR_ERR(base);
Chao Fu349ad662013-08-16 11:08:55 +08001047 goto out_master_put;
1048 }
1049
Esben Haabendal58ba07e2018-06-20 09:34:38 +02001050 if (dspi->devtype_data->xspi_mode)
1051 regmap_config = &dspi_xspi_regmap_config[0];
1052 else
1053 regmap_config = &dspi_regmap_config;
1054 dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
Chao Fu1acbdeb2014-02-12 15:29:05 +08001055 if (IS_ERR(dspi->regmap)) {
1056 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
1057 PTR_ERR(dspi->regmap));
Christophe JAILLETfbad6c22017-02-19 14:19:02 +01001058 ret = PTR_ERR(dspi->regmap);
1059 goto out_master_put;
Chao Fu1acbdeb2014-02-12 15:29:05 +08001060 }
1061
Esben Haabendal58ba07e2018-06-20 09:34:38 +02001062 if (dspi->devtype_data->xspi_mode) {
1063 dspi->regmap_pushr = devm_regmap_init_mmio(
1064 &pdev->dev, base + SPI_PUSHR,
1065 &dspi_xspi_regmap_config[1]);
1066 if (IS_ERR(dspi->regmap_pushr)) {
1067 dev_err(&pdev->dev,
1068 "failed to init pushr regmap: %ld\n",
1069 PTR_ERR(dspi->regmap_pushr));
Gustavo A. R. Silva80dc12c2018-06-21 08:22:09 -05001070 ret = PTR_ERR(dspi->regmap_pushr);
Esben Haabendal58ba07e2018-06-20 09:34:38 +02001071 goto out_master_put;
1072 }
1073 }
1074
Chao Fu349ad662013-08-16 11:08:55 +08001075 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1076 if (IS_ERR(dspi->clk)) {
1077 ret = PTR_ERR(dspi->clk);
1078 dev_err(&pdev->dev, "unable to get clock\n");
1079 goto out_master_put;
1080 }
Fabio Estevam1c5ea2b2016-08-21 23:05:30 -03001081 ret = clk_prepare_enable(dspi->clk);
1082 if (ret)
1083 goto out_master_put;
Chao Fu349ad662013-08-16 11:08:55 +08001084
Krzysztof Kozlowskid8ffee22018-06-29 13:33:09 +02001085 dspi_init(dspi);
1086 dspi->irq = platform_get_irq(pdev, 0);
1087 if (dspi->irq < 0) {
1088 dev_err(&pdev->dev, "can't get platform irq\n");
1089 ret = dspi->irq;
1090 goto out_clk_put;
1091 }
1092
1093 ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
1094 pdev->name, dspi);
1095 if (ret < 0) {
1096 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1097 goto out_clk_put;
1098 }
1099
Sanchayan Maity90ba3702016-11-10 17:49:15 +05301100 if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
Nikita Yushchenkocddebdd2017-05-22 16:19:20 +03001101 ret = dspi_request_dma(dspi, res->start);
1102 if (ret < 0) {
Sanchayan Maity90ba3702016-11-10 17:49:15 +05301103 dev_err(&pdev->dev, "can't get dma channels\n");
1104 goto out_clk_put;
1105 }
1106 }
1107
Bhuvanchandra DV9419b202016-03-22 01:41:52 +05301108 master->max_speed_hz =
1109 clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
1110
Chao Fu349ad662013-08-16 11:08:55 +08001111 init_waitqueue_head(&dspi->waitq);
Axel Lin017145f2014-02-14 12:49:12 +08001112 platform_set_drvdata(pdev, master);
Chao Fu349ad662013-08-16 11:08:55 +08001113
Chao Fu9298bc72015-01-27 16:27:22 +05301114 ret = spi_register_master(master);
Chao Fu349ad662013-08-16 11:08:55 +08001115 if (ret != 0) {
1116 dev_err(&pdev->dev, "Problem registering DSPI master\n");
1117 goto out_clk_put;
1118 }
1119
Chao Fu349ad662013-08-16 11:08:55 +08001120 return ret;
1121
1122out_clk_put:
1123 clk_disable_unprepare(dspi->clk);
1124out_master_put:
1125 spi_master_put(master);
Chao Fu349ad662013-08-16 11:08:55 +08001126
1127 return ret;
1128}
1129
1130static int dspi_remove(struct platform_device *pdev)
1131{
Axel Lin017145f2014-02-14 12:49:12 +08001132 struct spi_master *master = platform_get_drvdata(pdev);
1133 struct fsl_dspi *dspi = spi_master_get_devdata(master);
Chao Fu349ad662013-08-16 11:08:55 +08001134
1135 /* Disconnect from the SPI framework */
Sanchayan Maity90ba3702016-11-10 17:49:15 +05301136 dspi_release_dma(dspi);
Wei Yongjun05209f42013-10-12 15:15:31 +08001137 clk_disable_unprepare(dspi->clk);
Chao Fu9298bc72015-01-27 16:27:22 +05301138 spi_unregister_master(dspi->master);
Chao Fu349ad662013-08-16 11:08:55 +08001139
1140 return 0;
1141}
1142
1143static struct platform_driver fsl_dspi_driver = {
1144 .driver.name = DRIVER_NAME,
1145 .driver.of_match_table = fsl_dspi_dt_ids,
1146 .driver.owner = THIS_MODULE,
1147 .driver.pm = &dspi_pm,
1148 .probe = dspi_probe,
1149 .remove = dspi_remove,
1150};
1151module_platform_driver(fsl_dspi_driver);
1152
1153MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
Uwe Kleine-Königb444d1d2013-09-10 10:46:33 +02001154MODULE_LICENSE("GPL");
Chao Fu349ad662013-08-16 11:08:55 +08001155MODULE_ALIAS("platform:" DRIVER_NAME);