blob: 67cd2e90125541bdeddef2d0f88005b52849786b [file] [log] [blame]
Chao Fu349ad662013-08-16 11:08:55 +08001/*
2 * drivers/spi/spi-fsl-dspi.c
3 *
4 * Copyright 2013 Freescale Semiconductor, Inc.
5 *
6 * Freescale DSPI driver
7 * This file contains a driver for the Freescale DSPI
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
Xiubo Lia3108362014-09-29 10:57:06 +080016#include <linux/clk.h>
17#include <linux/delay.h>
Sanchayan Maity90ba3702016-11-10 17:49:15 +053018#include <linux/dmaengine.h>
19#include <linux/dma-mapping.h>
Xiubo Lia3108362014-09-29 10:57:06 +080020#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Chao Fu349ad662013-08-16 11:08:55 +080024#include <linux/kernel.h>
Aaron Brice95bf15f2015-04-03 13:39:31 -070025#include <linux/math64.h>
Chao Fu349ad662013-08-16 11:08:55 +080026#include <linux/module.h>
Chao Fu349ad662013-08-16 11:08:55 +080027#include <linux/of.h>
28#include <linux/of_device.h>
Mirza Krak432a17d2015-06-12 18:55:22 +020029#include <linux/pinctrl/consumer.h>
Xiubo Lia3108362014-09-29 10:57:06 +080030#include <linux/platform_device.h>
31#include <linux/pm_runtime.h>
32#include <linux/regmap.h>
33#include <linux/sched.h>
34#include <linux/spi/spi.h>
Angelo Dureghelloec7ed772017-10-28 00:23:01 +020035#include <linux/spi/spi-fsl-dspi.h>
Xiubo Lia3108362014-09-29 10:57:06 +080036#include <linux/spi/spi_bitbang.h>
Aaron Brice95bf15f2015-04-03 13:39:31 -070037#include <linux/time.h>
Chao Fu349ad662013-08-16 11:08:55 +080038
39#define DRIVER_NAME "fsl-dspi"
40
Chao Fu349ad662013-08-16 11:08:55 +080041#define DSPI_FIFO_SIZE 4
Sanchayan Maity90ba3702016-11-10 17:49:15 +053042#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
Chao Fu349ad662013-08-16 11:08:55 +080043
44#define SPI_MCR 0x00
45#define SPI_MCR_MASTER (1 << 31)
46#define SPI_MCR_PCSIS (0x3F << 16)
47#define SPI_MCR_CLR_TXF (1 << 11)
48#define SPI_MCR_CLR_RXF (1 << 10)
49
50#define SPI_TCR 0x08
Haikun Wangc042af92015-06-09 19:45:37 +080051#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
Chao Fu349ad662013-08-16 11:08:55 +080052
Alexander Stein5cc7b042014-11-04 09:20:18 +010053#define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
Chao Fu349ad662013-08-16 11:08:55 +080054#define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
55#define SPI_CTAR_CPOL(x) ((x) << 26)
56#define SPI_CTAR_CPHA(x) ((x) << 25)
57#define SPI_CTAR_LSBFE(x) ((x) << 24)
Aaron Brice95bf15f2015-04-03 13:39:31 -070058#define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
Chao Fu349ad662013-08-16 11:08:55 +080059#define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
60#define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
61#define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
62#define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
63#define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
64#define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
65#define SPI_CTAR_BR(x) ((x) & 0x0000000f)
Aaron Brice95bf15f2015-04-03 13:39:31 -070066#define SPI_CTAR_SCALE_BITS 0xf
Chao Fu349ad662013-08-16 11:08:55 +080067
68#define SPI_CTAR0_SLAVE 0x0c
69
70#define SPI_SR 0x2c
71#define SPI_SR_EOQF 0x10000000
Haikun Wangd1f4a382015-06-09 19:45:27 +080072#define SPI_SR_TCFQF 0x80000000
Yuan Yao5ee67b52016-10-17 18:02:34 +080073#define SPI_SR_CLEAR 0xdaad0000
Chao Fu349ad662013-08-16 11:08:55 +080074
Sanchayan Maity90ba3702016-11-10 17:49:15 +053075#define SPI_RSER_TFFFE BIT(25)
76#define SPI_RSER_TFFFD BIT(24)
77#define SPI_RSER_RFDFE BIT(17)
78#define SPI_RSER_RFDFD BIT(16)
Chao Fu349ad662013-08-16 11:08:55 +080079
80#define SPI_RSER 0x30
81#define SPI_RSER_EOQFE 0x10000000
Haikun Wangd1f4a382015-06-09 19:45:27 +080082#define SPI_RSER_TCFQE 0x80000000
Chao Fu349ad662013-08-16 11:08:55 +080083
84#define SPI_PUSHR 0x34
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +020085#define SPI_PUSHR_CMD_CONT (1 << 15)
86#define SPI_PUSHR_CONT (SPI_PUSHR_CMD_CONT << 16)
87#define SPI_PUSHR_CMD_CTAS(x) (((x) & 0x0003) << 12)
88#define SPI_PUSHR_CTAS(x) (SPI_PUSHR_CMD_CTAS(x) << 16)
89#define SPI_PUSHR_CMD_EOQ (1 << 11)
90#define SPI_PUSHR_EOQ (SPI_PUSHR_CMD_EOQ << 16)
91#define SPI_PUSHR_CMD_CTCNT (1 << 10)
92#define SPI_PUSHR_CTCNT (SPI_PUSHR_CMD_CTCNT << 16)
93#define SPI_PUSHR_CMD_PCS(x) ((1 << x) & 0x003f)
94#define SPI_PUSHR_PCS(x) (SPI_PUSHR_CMD_PCS(x) << 16)
Chao Fu349ad662013-08-16 11:08:55 +080095#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
96
97#define SPI_PUSHR_SLAVE 0x34
98
99#define SPI_POPR 0x38
100#define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
101
102#define SPI_TXFR0 0x3c
103#define SPI_TXFR1 0x40
104#define SPI_TXFR2 0x44
105#define SPI_TXFR3 0x48
106#define SPI_RXFR0 0x7c
107#define SPI_RXFR1 0x80
108#define SPI_RXFR2 0x84
109#define SPI_RXFR3 0x88
110
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200111#define SPI_CTARE(x) (0x11c + (((x) & 0x3) * 4))
112#define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
113#define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
114
115#define SPI_SREX 0x13c
116
Chao Fu349ad662013-08-16 11:08:55 +0800117#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
118#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
119#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
120#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
121
Esben Haabendal51d583a2018-06-20 09:34:39 +0200122#define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
123#define SPI_FRAME_EBITS_MASK SPI_CTARE_FMSZE(1)
124
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200125/* Register offsets for regmap_pushr */
126#define PUSHR_CMD 0x0
127#define PUSHR_TX 0x2
128
Chao Fu349ad662013-08-16 11:08:55 +0800129#define SPI_CS_INIT 0x01
130#define SPI_CS_ASSERT 0x02
131#define SPI_CS_DROP 0x04
132
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530133#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
134
Chao Fu349ad662013-08-16 11:08:55 +0800135struct chip_data {
Chao Fu349ad662013-08-16 11:08:55 +0800136 u32 ctar_val;
137 u16 void_write_data;
138};
139
Haikun Wangd1f4a382015-06-09 19:45:27 +0800140enum dspi_trans_mode {
141 DSPI_EOQ_MODE = 0,
142 DSPI_TCFQ_MODE,
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530143 DSPI_DMA_MODE,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800144};
145
146struct fsl_dspi_devtype_data {
147 enum dspi_trans_mode trans_mode;
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530148 u8 max_clock_factor;
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200149 bool xspi_mode;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800150};
151
152static const struct fsl_dspi_devtype_data vf610_data = {
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530153 .trans_mode = DSPI_DMA_MODE,
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530154 .max_clock_factor = 2,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800155};
156
157static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
158 .trans_mode = DSPI_TCFQ_MODE,
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530159 .max_clock_factor = 8,
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200160 .xspi_mode = true,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800161};
162
163static const struct fsl_dspi_devtype_data ls2085a_data = {
164 .trans_mode = DSPI_TCFQ_MODE,
Bhuvanchandra DV9419b202016-03-22 01:41:52 +0530165 .max_clock_factor = 8,
Haikun Wangd1f4a382015-06-09 19:45:27 +0800166};
167
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200168static const struct fsl_dspi_devtype_data coldfire_data = {
169 .trans_mode = DSPI_EOQ_MODE,
170 .max_clock_factor = 8,
171};
172
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530173struct fsl_dspi_dma {
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530174 /* Length of transfer in words of DSPI_FIFO_SIZE */
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530175 u32 curr_xfer_len;
176
177 u32 *tx_dma_buf;
178 struct dma_chan *chan_tx;
179 dma_addr_t tx_dma_phys;
180 struct completion cmd_tx_complete;
181 struct dma_async_tx_descriptor *tx_desc;
182
183 u32 *rx_dma_buf;
184 struct dma_chan *chan_rx;
185 dma_addr_t rx_dma_phys;
186 struct completion cmd_rx_complete;
187 struct dma_async_tx_descriptor *rx_desc;
188};
189
Chao Fu349ad662013-08-16 11:08:55 +0800190struct fsl_dspi {
Chao Fu9298bc72015-01-27 16:27:22 +0530191 struct spi_master *master;
Chao Fu349ad662013-08-16 11:08:55 +0800192 struct platform_device *pdev;
193
Chao Fu1acbdeb2014-02-12 15:29:05 +0800194 struct regmap *regmap;
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200195 struct regmap *regmap_pushr;
Chao Fu349ad662013-08-16 11:08:55 +0800196 int irq;
Chao Fu88386e82014-02-12 15:29:06 +0800197 struct clk *clk;
Chao Fu349ad662013-08-16 11:08:55 +0800198
Chao Fu88386e82014-02-12 15:29:06 +0800199 struct spi_transfer *cur_transfer;
Chao Fu9298bc72015-01-27 16:27:22 +0530200 struct spi_message *cur_msg;
Chao Fu349ad662013-08-16 11:08:55 +0800201 struct chip_data *cur_chip;
202 size_t len;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200203 const void *tx;
Chao Fu349ad662013-08-16 11:08:55 +0800204 void *rx;
205 void *rx_end;
Chao Fu349ad662013-08-16 11:08:55 +0800206 u16 void_write_data;
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +0200207 u16 tx_cmd;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200208 u8 bits_per_word;
209 u8 bytes_per_word;
LABBE Corentin94b968b2016-08-16 11:50:20 +0200210 const struct fsl_dspi_devtype_data *devtype_data;
Chao Fu349ad662013-08-16 11:08:55 +0800211
Chao Fu88386e82014-02-12 15:29:06 +0800212 wait_queue_head_t waitq;
213 u32 waitflags;
Haikun Wangc042af92015-06-09 19:45:37 +0800214
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530215 struct fsl_dspi_dma *dma;
Chao Fu349ad662013-08-16 11:08:55 +0800216};
217
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200218static u32 dspi_pop_tx(struct fsl_dspi *dspi)
Chao Fu349ad662013-08-16 11:08:55 +0800219{
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200220 u32 txdata = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800221
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200222 if (dspi->tx) {
223 if (dspi->bytes_per_word == 1)
224 txdata = *(u8 *)dspi->tx;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200225 else if (dspi->bytes_per_word == 2)
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200226 txdata = *(u16 *)dspi->tx;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200227 else /* dspi->bytes_per_word == 4 */
228 txdata = *(u32 *)dspi->tx;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200229 dspi->tx += dspi->bytes_per_word;
230 }
231 dspi->len -= dspi->bytes_per_word;
232 return txdata;
233}
Chao Fu349ad662013-08-16 11:08:55 +0800234
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200235static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
236{
237 u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
238
239 if (dspi->len > 0)
240 cmd |= SPI_PUSHR_CMD_CONT;
241 return cmd << 16 | data;
242}
243
244static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
245{
246 if (!dspi->rx)
247 return;
248
249 /* Mask of undefined bits */
250 rxdata &= (1 << dspi->bits_per_word) - 1;
251
252 if (dspi->bytes_per_word == 1)
253 *(u8 *)dspi->rx = rxdata;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200254 else if (dspi->bytes_per_word == 2)
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200255 *(u16 *)dspi->rx = rxdata;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200256 else /* dspi->bytes_per_word == 4 */
257 *(u32 *)dspi->rx = rxdata;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200258 dspi->rx += dspi->bytes_per_word;
Chao Fu349ad662013-08-16 11:08:55 +0800259}
260
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530261static void dspi_tx_dma_callback(void *arg)
262{
263 struct fsl_dspi *dspi = arg;
264 struct fsl_dspi_dma *dma = dspi->dma;
265
266 complete(&dma->cmd_tx_complete);
267}
268
269static void dspi_rx_dma_callback(void *arg)
270{
271 struct fsl_dspi *dspi = arg;
272 struct fsl_dspi_dma *dma = dspi->dma;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530273 int i;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530274
Esben Haabendal4779f232018-06-20 09:34:32 +0200275 if (dspi->rx) {
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200276 for (i = 0; i < dma->curr_xfer_len; i++)
277 dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530278 }
279
280 complete(&dma->cmd_rx_complete);
281}
282
283static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
284{
285 struct fsl_dspi_dma *dma = dspi->dma;
286 struct device *dev = &dspi->pdev->dev;
287 int time_left;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530288 int i;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530289
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200290 for (i = 0; i < dma->curr_xfer_len; i++)
291 dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530292
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530293 dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
294 dma->tx_dma_phys,
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530295 dma->curr_xfer_len *
296 DMA_SLAVE_BUSWIDTH_4_BYTES,
297 DMA_MEM_TO_DEV,
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530298 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
299 if (!dma->tx_desc) {
300 dev_err(dev, "Not able to get desc for DMA xfer\n");
301 return -EIO;
302 }
303
304 dma->tx_desc->callback = dspi_tx_dma_callback;
305 dma->tx_desc->callback_param = dspi;
306 if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
307 dev_err(dev, "DMA submit failed\n");
308 return -EINVAL;
309 }
310
311 dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
312 dma->rx_dma_phys,
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530313 dma->curr_xfer_len *
314 DMA_SLAVE_BUSWIDTH_4_BYTES,
315 DMA_DEV_TO_MEM,
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530316 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
317 if (!dma->rx_desc) {
318 dev_err(dev, "Not able to get desc for DMA xfer\n");
319 return -EIO;
320 }
321
322 dma->rx_desc->callback = dspi_rx_dma_callback;
323 dma->rx_desc->callback_param = dspi;
324 if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
325 dev_err(dev, "DMA submit failed\n");
326 return -EINVAL;
327 }
328
329 reinit_completion(&dspi->dma->cmd_rx_complete);
330 reinit_completion(&dspi->dma->cmd_tx_complete);
331
332 dma_async_issue_pending(dma->chan_rx);
333 dma_async_issue_pending(dma->chan_tx);
334
335 time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
336 DMA_COMPLETION_TIMEOUT);
337 if (time_left == 0) {
338 dev_err(dev, "DMA tx timeout\n");
339 dmaengine_terminate_all(dma->chan_tx);
340 dmaengine_terminate_all(dma->chan_rx);
341 return -ETIMEDOUT;
342 }
343
344 time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
345 DMA_COMPLETION_TIMEOUT);
346 if (time_left == 0) {
347 dev_err(dev, "DMA rx timeout\n");
348 dmaengine_terminate_all(dma->chan_tx);
349 dmaengine_terminate_all(dma->chan_rx);
350 return -ETIMEDOUT;
351 }
352
353 return 0;
354}
355
356static int dspi_dma_xfer(struct fsl_dspi *dspi)
357{
358 struct fsl_dspi_dma *dma = dspi->dma;
359 struct device *dev = &dspi->pdev->dev;
360 int curr_remaining_bytes;
361 int bytes_per_buffer;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530362 int ret = 0;
363
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530364 curr_remaining_bytes = dspi->len;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530365 bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530366 while (curr_remaining_bytes) {
367 /* Check if current transfer fits the DMA buffer */
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200368 dma->curr_xfer_len = curr_remaining_bytes
369 / dspi->bytes_per_word;
Sanchayan Maity1eaccf22016-11-22 12:31:30 +0530370 if (dma->curr_xfer_len > bytes_per_buffer)
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530371 dma->curr_xfer_len = bytes_per_buffer;
372
373 ret = dspi_next_xfer_dma_submit(dspi);
374 if (ret) {
375 dev_err(dev, "DMA transfer failed\n");
376 goto exit;
377
378 } else {
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200379 curr_remaining_bytes -= dma->curr_xfer_len
380 * dspi->bytes_per_word;
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;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800630
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200631 /* Fill TX FIFO with as many transfers as possible */
632 while (dspi->len && fifo_size--) {
633 /* Request EOQF for last transfer in FIFO */
634 if (dspi->len == dspi->bytes_per_word || fifo_size == 0)
635 dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ;
636 /* Clear transfer count for first transfer in FIFO */
637 if (fifo_size == (DSPI_FIFO_SIZE - 1))
638 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
639 /* Write combined TX FIFO and CMD FIFO entry */
640 fifo_write(dspi);
641 }
642}
643
644static void dspi_eoq_read(struct fsl_dspi *dspi)
645{
646 int fifo_size = DSPI_FIFO_SIZE;
647
648 /* Read one FIFO entry at and push to rx buffer */
649 while ((dspi->rx < dspi->rx_end) && fifo_size--)
650 dspi_push_rx(dspi, fifo_read(dspi));
Haikun Wangd1f4a382015-06-09 19:45:27 +0800651}
652
Chao Fu9298bc72015-01-27 16:27:22 +0530653static int dspi_transfer_one_message(struct spi_master *master,
654 struct spi_message *message)
Chao Fu349ad662013-08-16 11:08:55 +0800655{
Chao Fu9298bc72015-01-27 16:27:22 +0530656 struct fsl_dspi *dspi = spi_master_get_devdata(master);
657 struct spi_device *spi = message->spi;
658 struct spi_transfer *transfer;
659 int status = 0;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800660 enum dspi_trans_mode trans_mode;
661
Chao Fu9298bc72015-01-27 16:27:22 +0530662 message->actual_length = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800663
Chao Fu9298bc72015-01-27 16:27:22 +0530664 list_for_each_entry(transfer, &message->transfers, transfer_list) {
665 dspi->cur_transfer = transfer;
666 dspi->cur_msg = message;
667 dspi->cur_chip = spi_get_ctldata(spi);
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +0200668 /* Prepare command word for CMD FIFO */
669 dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
670 SPI_PUSHR_CMD_PCS(spi->chip_select);
Andrey Vostrikov92dc20d2016-04-05 15:33:14 +0300671 if (list_is_last(&dspi->cur_transfer->transfer_list,
Esben Haabendal9e1dc9b2018-06-20 09:34:33 +0200672 &dspi->cur_msg->transfers)) {
673 /* Leave PCS activated after last transfer when
674 * cs_change is set.
675 */
676 if (transfer->cs_change)
677 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
678 } else {
679 /* Keep PCS active between transfers in same message
680 * when cs_change is not set, and de-activate PCS
681 * between transfers in the same message when
682 * cs_change is set.
683 */
684 if (!transfer->cs_change)
685 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
686 }
687
Chao Fu9298bc72015-01-27 16:27:22 +0530688 dspi->void_write_data = dspi->cur_chip->void_write_data;
Chao Fu349ad662013-08-16 11:08:55 +0800689
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200690 dspi->tx = transfer->tx_buf;
Chao Fu9298bc72015-01-27 16:27:22 +0530691 dspi->rx = transfer->rx_buf;
692 dspi->rx_end = dspi->rx + transfer->len;
693 dspi->len = transfer->len;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200694 /* Validated transfer specific frame size (defaults applied) */
695 dspi->bits_per_word = transfer->bits_per_word;
696 if (transfer->bits_per_word <= 8)
697 dspi->bytes_per_word = 1;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200698 else if (transfer->bits_per_word <= 16)
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200699 dspi->bytes_per_word = 2;
Esben Haabendal8fcd1512018-06-20 09:34:40 +0200700 else
701 dspi->bytes_per_word = 4;
Chao Fu349ad662013-08-16 11:08:55 +0800702
Chao Fu9298bc72015-01-27 16:27:22 +0530703 regmap_update_bits(dspi->regmap, SPI_MCR,
Esben Haabendald87e08f2018-06-20 09:34:37 +0200704 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
705 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
Bhuvanchandra DVef22d162015-12-10 11:25:30 +0530706 regmap_write(dspi->regmap, SPI_CTAR(0),
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200707 dspi->cur_chip->ctar_val |
708 SPI_FRAME_BITS(transfer->bits_per_word));
Esben Haabendal51d583a2018-06-20 09:34:39 +0200709 if (dspi->devtype_data->xspi_mode)
710 regmap_write(dspi->regmap, SPI_CTARE(0),
711 SPI_FRAME_EBITS(transfer->bits_per_word)
712 | SPI_CTARE_DTCP(1));
Chao Fu349ad662013-08-16 11:08:55 +0800713
Haikun Wangd1f4a382015-06-09 19:45:27 +0800714 trans_mode = dspi->devtype_data->trans_mode;
715 switch (trans_mode) {
716 case DSPI_EOQ_MODE:
717 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
Haikun Wangc042af92015-06-09 19:45:37 +0800718 dspi_eoq_write(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800719 break;
720 case DSPI_TCFQ_MODE:
721 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
Haikun Wangc042af92015-06-09 19:45:37 +0800722 dspi_tcfq_write(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800723 break;
Sanchayan Maity90ba3702016-11-10 17:49:15 +0530724 case DSPI_DMA_MODE:
725 regmap_write(dspi->regmap, SPI_RSER,
726 SPI_RSER_TFFFE | SPI_RSER_TFFFD |
727 SPI_RSER_RFDFE | SPI_RSER_RFDFD);
728 status = dspi_dma_xfer(dspi);
Sanchayan Maity98114302016-11-17 17:46:48 +0530729 break;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800730 default:
731 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
732 trans_mode);
733 status = -EINVAL;
734 goto out;
735 }
Chao Fu349ad662013-08-16 11:08:55 +0800736
Sanchayan Maity98114302016-11-17 17:46:48 +0530737 if (trans_mode != DSPI_DMA_MODE) {
738 if (wait_event_interruptible(dspi->waitq,
739 dspi->waitflags))
740 dev_err(&dspi->pdev->dev,
741 "wait transfer complete fail!\n");
742 dspi->waitflags = 0;
743 }
Chao Fu349ad662013-08-16 11:08:55 +0800744
Chao Fu9298bc72015-01-27 16:27:22 +0530745 if (transfer->delay_usecs)
746 udelay(transfer->delay_usecs);
Chao Fu349ad662013-08-16 11:08:55 +0800747 }
748
Haikun Wangd1f4a382015-06-09 19:45:27 +0800749out:
Chao Fu9298bc72015-01-27 16:27:22 +0530750 message->status = status;
751 spi_finalize_current_message(master);
752
753 return status;
Chao Fu349ad662013-08-16 11:08:55 +0800754}
755
Chao Fu9298bc72015-01-27 16:27:22 +0530756static int dspi_setup(struct spi_device *spi)
Chao Fu349ad662013-08-16 11:08:55 +0800757{
758 struct chip_data *chip;
759 struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200760 struct fsl_dspi_platform_data *pdata;
Aaron Brice95bf15f2015-04-03 13:39:31 -0700761 u32 cs_sck_delay = 0, sck_cs_delay = 0;
762 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200763 unsigned char pasc = 0, asc = 0;
Aaron Brice95bf15f2015-04-03 13:39:31 -0700764 unsigned long clkrate;
Chao Fu349ad662013-08-16 11:08:55 +0800765
766 /* Only alloc on first setup */
767 chip = spi_get_ctldata(spi);
768 if (chip == NULL) {
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530769 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Chao Fu349ad662013-08-16 11:08:55 +0800770 if (!chip)
771 return -ENOMEM;
772 }
773
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200774 pdata = dev_get_platdata(&dspi->pdev->dev);
Aaron Brice95bf15f2015-04-03 13:39:31 -0700775
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200776 if (!pdata) {
777 of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
778 &cs_sck_delay);
779
780 of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
781 &sck_cs_delay);
782 } else {
783 cs_sck_delay = pdata->cs_sck_delay;
784 sck_cs_delay = pdata->sck_cs_delay;
785 }
Aaron Brice95bf15f2015-04-03 13:39:31 -0700786
Chao Fu349ad662013-08-16 11:08:55 +0800787 chip->void_write_data = 0;
788
Aaron Brice95bf15f2015-04-03 13:39:31 -0700789 clkrate = clk_get_rate(dspi->clk);
790 hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
791
792 /* Set PCS to SCK delay scale values */
793 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
794
795 /* Set After SCK delay scale values */
796 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
Chao Fu349ad662013-08-16 11:08:55 +0800797
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200798 chip->ctar_val = SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
Chao Fu349ad662013-08-16 11:08:55 +0800799 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
800 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
Aaron Brice95bf15f2015-04-03 13:39:31 -0700801 | SPI_CTAR_PCSSCK(pcssck)
802 | SPI_CTAR_CSSCK(cssck)
803 | SPI_CTAR_PASC(pasc)
804 | SPI_CTAR_ASC(asc)
Chao Fu349ad662013-08-16 11:08:55 +0800805 | SPI_CTAR_PBR(pbr)
806 | SPI_CTAR_BR(br);
807
808 spi_set_ctldata(spi, chip);
809
810 return 0;
811}
812
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530813static void dspi_cleanup(struct spi_device *spi)
814{
815 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
816
817 dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
818 spi->master->bus_num, spi->chip_select);
819
820 kfree(chip);
821}
822
Chao Fu349ad662013-08-16 11:08:55 +0800823static irqreturn_t dspi_interrupt(int irq, void *dev_id)
824{
825 struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
Chao Fu9298bc72015-01-27 16:27:22 +0530826 struct spi_message *msg = dspi->cur_msg;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800827 enum dspi_trans_mode trans_mode;
Haikun Wangc042af92015-06-09 19:45:37 +0800828 u32 spi_sr, spi_tcr;
Esben Haabendal0a4ec2c2018-06-20 09:34:34 +0200829 u16 spi_tcnt;
Chao Fu349ad662013-08-16 11:08:55 +0800830
Haikun Wangd1f4a382015-06-09 19:45:27 +0800831 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
832 regmap_write(dspi->regmap, SPI_SR, spi_sr);
833
Chao Fu349ad662013-08-16 11:08:55 +0800834
Haikun Wangc042af92015-06-09 19:45:37 +0800835 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
Esben Haabendal0a4ec2c2018-06-20 09:34:34 +0200836 /* Get transfer counter (in number of SPI transfers). It was
837 * reset to 0 when transfer(s) were started.
838 */
Haikun Wangc042af92015-06-09 19:45:37 +0800839 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
840 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
Esben Haabendal0a4ec2c2018-06-20 09:34:34 +0200841 /* Update total number of bytes that were transferred */
Esben Haabendaldadcf4a2018-06-20 09:34:35 +0200842 msg->actual_length += spi_tcnt * dspi->bytes_per_word;
Haikun Wangc042af92015-06-09 19:45:37 +0800843
844 trans_mode = dspi->devtype_data->trans_mode;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800845 switch (trans_mode) {
846 case DSPI_EOQ_MODE:
Haikun Wangc042af92015-06-09 19:45:37 +0800847 dspi_eoq_read(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800848 break;
849 case DSPI_TCFQ_MODE:
Haikun Wangc042af92015-06-09 19:45:37 +0800850 dspi_tcfq_read(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800851 break;
852 default:
853 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
854 trans_mode);
Haikun Wangc042af92015-06-09 19:45:37 +0800855 return IRQ_HANDLED;
856 }
857
858 if (!dspi->len) {
Haikun Wangc042af92015-06-09 19:45:37 +0800859 dspi->waitflags = 1;
860 wake_up_interruptible(&dspi->waitq);
861 } else {
862 switch (trans_mode) {
863 case DSPI_EOQ_MODE:
864 dspi_eoq_write(dspi);
865 break;
866 case DSPI_TCFQ_MODE:
867 dspi_tcfq_write(dspi);
868 break;
869 default:
870 dev_err(&dspi->pdev->dev,
871 "unsupported trans_mode %u\n",
872 trans_mode);
873 }
Haikun Wangd1f4a382015-06-09 19:45:27 +0800874 }
875 }
Haikun Wangc042af92015-06-09 19:45:37 +0800876
Chao Fu349ad662013-08-16 11:08:55 +0800877 return IRQ_HANDLED;
878}
879
Jingoo Han790d1902014-05-07 16:45:41 +0900880static const struct of_device_id fsl_dspi_dt_ids[] = {
Julia Lawall230c08b2018-01-02 14:28:06 +0100881 { .compatible = "fsl,vf610-dspi", .data = &vf610_data, },
882 { .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, },
883 { .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, },
Chao Fu349ad662013-08-16 11:08:55 +0800884 { /* sentinel */ }
885};
886MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
887
888#ifdef CONFIG_PM_SLEEP
889static int dspi_suspend(struct device *dev)
890{
891 struct spi_master *master = dev_get_drvdata(dev);
892 struct fsl_dspi *dspi = spi_master_get_devdata(master);
893
894 spi_master_suspend(master);
895 clk_disable_unprepare(dspi->clk);
896
Mirza Krak432a17d2015-06-12 18:55:22 +0200897 pinctrl_pm_select_sleep_state(dev);
898
Chao Fu349ad662013-08-16 11:08:55 +0800899 return 0;
900}
901
902static int dspi_resume(struct device *dev)
903{
Chao Fu349ad662013-08-16 11:08:55 +0800904 struct spi_master *master = dev_get_drvdata(dev);
905 struct fsl_dspi *dspi = spi_master_get_devdata(master);
Fabio Estevam1c5ea2b2016-08-21 23:05:30 -0300906 int ret;
Chao Fu349ad662013-08-16 11:08:55 +0800907
Mirza Krak432a17d2015-06-12 18:55:22 +0200908 pinctrl_pm_select_default_state(dev);
909
Fabio Estevam1c5ea2b2016-08-21 23:05:30 -0300910 ret = clk_prepare_enable(dspi->clk);
911 if (ret)
912 return ret;
Chao Fu349ad662013-08-16 11:08:55 +0800913 spi_master_resume(master);
914
915 return 0;
916}
917#endif /* CONFIG_PM_SLEEP */
918
Jingoo Hanba811ad2014-02-26 10:30:14 +0900919static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
Chao Fu349ad662013-08-16 11:08:55 +0800920
Esben Haabendal85700432018-06-20 09:34:36 +0200921static const struct regmap_range dspi_volatile_ranges[] = {
922 regmap_reg_range(SPI_MCR, SPI_TCR),
923 regmap_reg_range(SPI_SR, SPI_SR),
924 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
925};
926
927static const struct regmap_access_table dspi_volatile_table = {
928 .yes_ranges = dspi_volatile_ranges,
929 .n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges),
930};
931
Xiubo Li409851c2014-10-09 11:27:45 +0800932static const struct regmap_config dspi_regmap_config = {
Chao Fu1acbdeb2014-02-12 15:29:05 +0800933 .reg_bits = 32,
934 .val_bits = 32,
935 .reg_stride = 4,
936 .max_register = 0x88,
Esben Haabendal85700432018-06-20 09:34:36 +0200937 .volatile_table = &dspi_volatile_table,
Chao Fu349ad662013-08-16 11:08:55 +0800938};
939
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200940static const struct regmap_range dspi_xspi_volatile_ranges[] = {
941 regmap_reg_range(SPI_MCR, SPI_TCR),
942 regmap_reg_range(SPI_SR, SPI_SR),
943 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
944 regmap_reg_range(SPI_SREX, SPI_SREX),
945};
946
947static const struct regmap_access_table dspi_xspi_volatile_table = {
948 .yes_ranges = dspi_xspi_volatile_ranges,
949 .n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges),
950};
951
952static const struct regmap_config dspi_xspi_regmap_config[] = {
953 {
954 .reg_bits = 32,
955 .val_bits = 32,
956 .reg_stride = 4,
957 .max_register = 0x13c,
958 .volatile_table = &dspi_xspi_volatile_table,
959 },
960 {
961 .name = "pushr",
962 .reg_bits = 16,
963 .val_bits = 16,
964 .reg_stride = 2,
965 .max_register = 0x2,
966 },
967};
968
Yuan Yao5ee67b52016-10-17 18:02:34 +0800969static void dspi_init(struct fsl_dspi *dspi)
970{
Esben Haabendald87e08f2018-06-20 09:34:37 +0200971 regmap_write(dspi->regmap, SPI_MCR, SPI_MCR_MASTER | SPI_MCR_PCSIS);
Yuan Yao5ee67b52016-10-17 18:02:34 +0800972 regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
Esben Haabendal51d583a2018-06-20 09:34:39 +0200973 if (dspi->devtype_data->xspi_mode)
974 regmap_write(dspi->regmap, SPI_CTARE(0),
975 SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
Yuan Yao5ee67b52016-10-17 18:02:34 +0800976}
977
Chao Fu349ad662013-08-16 11:08:55 +0800978static int dspi_probe(struct platform_device *pdev)
979{
980 struct device_node *np = pdev->dev.of_node;
981 struct spi_master *master;
982 struct fsl_dspi *dspi;
983 struct resource *res;
Esben Haabendal58ba07ec2018-06-20 09:34:38 +0200984 const struct regmap_config *regmap_config;
Chao Fu1acbdeb2014-02-12 15:29:05 +0800985 void __iomem *base;
Angelo Dureghelloec7ed772017-10-28 00:23:01 +0200986 struct fsl_dspi_platform_data *pdata;
Chao Fu349ad662013-08-16 11:08:55 +0800987 int ret = 0, cs_num, bus_num;
988
989 master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
990 if (!master)
991 return -ENOMEM;
992
993 dspi = spi_master_get_devdata(master);
994 dspi->pdev = pdev;
Chao Fu9298bc72015-01-27 16:27:22 +0530995 dspi->master = master;
996
997 master->transfer = NULL;
998 master->setup = dspi_setup;
999 master->transfer_one_message = dspi_transfer_one_message;
1000 master->dev.of_node = pdev->dev.of_node;
Chao Fu349ad662013-08-16 11:08:55 +08001001
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +05301002 master->cleanup = dspi_cleanup;
Kurt Kanzenbach00ac9562017-11-13 08:47:21 +01001003 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Esben Haabendaldadcf4a2018-06-20 09:34:35 +02001004 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Chao Fu349ad662013-08-16 11:08:55 +08001005
Angelo Dureghelloec7ed772017-10-28 00:23:01 +02001006 pdata = dev_get_platdata(&pdev->dev);
1007 if (pdata) {
1008 master->num_chipselect = pdata->cs_num;
1009 master->bus_num = pdata->bus_num;
Chao Fu349ad662013-08-16 11:08:55 +08001010
Angelo Dureghelloec7ed772017-10-28 00:23:01 +02001011 dspi->devtype_data = &coldfire_data;
1012 } else {
Chao Fu349ad662013-08-16 11:08:55 +08001013
Angelo Dureghelloec7ed772017-10-28 00:23:01 +02001014 ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
1015 if (ret < 0) {
1016 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
1017 goto out_master_put;
1018 }
1019 master->num_chipselect = cs_num;
1020
1021 ret = of_property_read_u32(np, "bus-num", &bus_num);
1022 if (ret < 0) {
1023 dev_err(&pdev->dev, "can't get bus-num\n");
1024 goto out_master_put;
1025 }
1026 master->bus_num = bus_num;
1027
1028 dspi->devtype_data = of_device_get_match_data(&pdev->dev);
1029 if (!dspi->devtype_data) {
1030 dev_err(&pdev->dev, "can't get devtype_data\n");
1031 ret = -EFAULT;
1032 goto out_master_put;
1033 }
Haikun Wangd1f4a382015-06-09 19:45:27 +08001034 }
1035
Chao Fu349ad662013-08-16 11:08:55 +08001036 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chao Fu1acbdeb2014-02-12 15:29:05 +08001037 base = devm_ioremap_resource(&pdev->dev, res);
1038 if (IS_ERR(base)) {
1039 ret = PTR_ERR(base);
Chao Fu349ad662013-08-16 11:08:55 +08001040 goto out_master_put;
1041 }
1042
Esben Haabendal58ba07ec2018-06-20 09:34:38 +02001043 if (dspi->devtype_data->xspi_mode)
1044 regmap_config = &dspi_xspi_regmap_config[0];
1045 else
1046 regmap_config = &dspi_regmap_config;
1047 dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
Chao Fu1acbdeb2014-02-12 15:29:05 +08001048 if (IS_ERR(dspi->regmap)) {
1049 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
1050 PTR_ERR(dspi->regmap));
Christophe JAILLETfbad6c22017-02-19 14:19:02 +01001051 ret = PTR_ERR(dspi->regmap);
1052 goto out_master_put;
Chao Fu1acbdeb2014-02-12 15:29:05 +08001053 }
1054
Esben Haabendal58ba07ec2018-06-20 09:34:38 +02001055 if (dspi->devtype_data->xspi_mode) {
1056 dspi->regmap_pushr = devm_regmap_init_mmio(
1057 &pdev->dev, base + SPI_PUSHR,
1058 &dspi_xspi_regmap_config[1]);
1059 if (IS_ERR(dspi->regmap_pushr)) {
1060 dev_err(&pdev->dev,
1061 "failed to init pushr regmap: %ld\n",
1062 PTR_ERR(dspi->regmap_pushr));
1063 ret = PTR_ERR(dspi->regmap);
1064 goto out_master_put;
1065 }
1066 }
1067
Yuan Yao5ee67b52016-10-17 18:02:34 +08001068 dspi_init(dspi);
Chao Fu349ad662013-08-16 11:08:55 +08001069 dspi->irq = platform_get_irq(pdev, 0);
1070 if (dspi->irq < 0) {
1071 dev_err(&pdev->dev, "can't get platform irq\n");
1072 ret = dspi->irq;
1073 goto out_master_put;
1074 }
1075
1076 ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
1077 pdev->name, dspi);
1078 if (ret < 0) {
1079 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1080 goto out_master_put;
1081 }
1082
1083 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1084 if (IS_ERR(dspi->clk)) {
1085 ret = PTR_ERR(dspi->clk);
1086 dev_err(&pdev->dev, "unable to get clock\n");
1087 goto out_master_put;
1088 }
Fabio Estevam1c5ea2b2016-08-21 23:05:30 -03001089 ret = clk_prepare_enable(dspi->clk);
1090 if (ret)
1091 goto out_master_put;
Chao Fu349ad662013-08-16 11:08:55 +08001092
Sanchayan Maity90ba3702016-11-10 17:49:15 +05301093 if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
Nikita Yushchenkocddebdd2017-05-22 16:19:20 +03001094 ret = dspi_request_dma(dspi, res->start);
1095 if (ret < 0) {
Sanchayan Maity90ba3702016-11-10 17:49:15 +05301096 dev_err(&pdev->dev, "can't get dma channels\n");
1097 goto out_clk_put;
1098 }
1099 }
1100
Bhuvanchandra DV9419b202016-03-22 01:41:52 +05301101 master->max_speed_hz =
1102 clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
1103
Chao Fu349ad662013-08-16 11:08:55 +08001104 init_waitqueue_head(&dspi->waitq);
Axel Lin017145f2014-02-14 12:49:12 +08001105 platform_set_drvdata(pdev, master);
Chao Fu349ad662013-08-16 11:08:55 +08001106
Chao Fu9298bc72015-01-27 16:27:22 +05301107 ret = spi_register_master(master);
Chao Fu349ad662013-08-16 11:08:55 +08001108 if (ret != 0) {
1109 dev_err(&pdev->dev, "Problem registering DSPI master\n");
1110 goto out_clk_put;
1111 }
1112
Chao Fu349ad662013-08-16 11:08:55 +08001113 return ret;
1114
1115out_clk_put:
1116 clk_disable_unprepare(dspi->clk);
1117out_master_put:
1118 spi_master_put(master);
Chao Fu349ad662013-08-16 11:08:55 +08001119
1120 return ret;
1121}
1122
1123static int dspi_remove(struct platform_device *pdev)
1124{
Axel Lin017145f2014-02-14 12:49:12 +08001125 struct spi_master *master = platform_get_drvdata(pdev);
1126 struct fsl_dspi *dspi = spi_master_get_devdata(master);
Chao Fu349ad662013-08-16 11:08:55 +08001127
1128 /* Disconnect from the SPI framework */
Sanchayan Maity90ba3702016-11-10 17:49:15 +05301129 dspi_release_dma(dspi);
Wei Yongjun05209f42013-10-12 15:15:31 +08001130 clk_disable_unprepare(dspi->clk);
Chao Fu9298bc72015-01-27 16:27:22 +05301131 spi_unregister_master(dspi->master);
Chao Fu349ad662013-08-16 11:08:55 +08001132
1133 return 0;
1134}
1135
1136static struct platform_driver fsl_dspi_driver = {
1137 .driver.name = DRIVER_NAME,
1138 .driver.of_match_table = fsl_dspi_dt_ids,
1139 .driver.owner = THIS_MODULE,
1140 .driver.pm = &dspi_pm,
1141 .probe = dspi_probe,
1142 .remove = dspi_remove,
1143};
1144module_platform_driver(fsl_dspi_driver);
1145
1146MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
Uwe Kleine-Königb444d1d2013-09-10 10:46:33 +02001147MODULE_LICENSE("GPL");
Chao Fu349ad662013-08-16 11:08:55 +08001148MODULE_ALIAS("platform:" DRIVER_NAME);