blob: 240a61b66a06605732d14541f9fda8addcbbc255 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Feng Tange24c7452009-12-14 14:20:22 -08002/*
Grant Likelyca632f52011-06-06 01:16:30 -06003 * Designware SPI core controller driver (refer pxa2xx_spi.c)
Feng Tange24c7452009-12-14 14:20:22 -08004 *
5 * Copyright (c) 2009, Intel Corporation.
Feng Tange24c7452009-12-14 14:20:22 -08006 */
7
8#include <linux/dma-mapping.h>
9#include <linux/interrupt.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040010#include <linux/module.h>
Feng Tange24c7452009-12-14 14:20:22 -080011#include <linux/highmem.h>
12#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Feng Tange24c7452009-12-14 14:20:22 -080014#include <linux/spi/spi.h>
15
Grant Likelyca632f52011-06-06 01:16:30 -060016#include "spi-dw.h"
Grant Likely568a60e2011-02-28 12:47:12 -070017
Feng Tange24c7452009-12-14 14:20:22 -080018#ifdef CONFIG_DEBUG_FS
19#include <linux/debugfs.h>
20#endif
21
Feng Tange24c7452009-12-14 14:20:22 -080022/* Slave spi_dev related */
23struct chip_data {
Feng Tange24c7452009-12-14 14:20:22 -080024 u8 tmode; /* TR/TO/RO/EEPROM */
25 u8 type; /* SPI/SSP/MicroWire */
26
Feng Tange24c7452009-12-14 14:20:22 -080027 u16 clk_div; /* baud rate divider */
28 u32 speed_hz; /* baud rate */
Feng Tange24c7452009-12-14 14:20:22 -080029};
30
31#ifdef CONFIG_DEBUG_FS
Feng Tange24c7452009-12-14 14:20:22 -080032#define SPI_REGS_BUFSIZE 1024
Andy Shevchenko53288fe2014-09-12 15:11:56 +030033static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
34 size_t count, loff_t *ppos)
Feng Tange24c7452009-12-14 14:20:22 -080035{
Andy Shevchenko53288fe2014-09-12 15:11:56 +030036 struct dw_spi *dws = file->private_data;
Feng Tange24c7452009-12-14 14:20:22 -080037 char *buf;
38 u32 len = 0;
39 ssize_t ret;
40
Feng Tange24c7452009-12-14 14:20:22 -080041 buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
42 if (!buf)
43 return 0;
44
Silvio Cesared1d6bd72019-01-12 16:28:44 +010045 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Andy Shevchenko53288fe2014-09-12 15:11:56 +030046 "%s registers:\n", dev_name(&dws->master->dev));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010047 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Feng Tange24c7452009-12-14 14:20:22 -080048 "=================================\n");
Silvio Cesared1d6bd72019-01-12 16:28:44 +010049 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080050 "CTRLR0: \t0x%08x\n", dw_readl(dws, DW_SPI_CTRLR0));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010051 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080052 "CTRLR1: \t0x%08x\n", dw_readl(dws, DW_SPI_CTRLR1));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010053 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070054 "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010055 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070056 "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010057 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070058 "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010059 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080060 "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFTLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010061 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080062 "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFTLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010063 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070064 "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010065 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070066 "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010067 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070068 "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010069 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070070 "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010071 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070072 "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010073 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070074 "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010075 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070076 "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010077 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070078 "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010079 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Feng Tange24c7452009-12-14 14:20:22 -080080 "=================================\n");
81
Andy Shevchenko53288fe2014-09-12 15:11:56 +030082 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
Feng Tange24c7452009-12-14 14:20:22 -080083 kfree(buf);
84 return ret;
85}
86
Andy Shevchenko53288fe2014-09-12 15:11:56 +030087static const struct file_operations dw_spi_regs_ops = {
Feng Tange24c7452009-12-14 14:20:22 -080088 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -070089 .open = simple_open,
Andy Shevchenko53288fe2014-09-12 15:11:56 +030090 .read = dw_spi_show_regs,
Arnd Bergmann6038f372010-08-15 18:52:59 +020091 .llseek = default_llseek,
Feng Tange24c7452009-12-14 14:20:22 -080092};
93
Andy Shevchenko53288fe2014-09-12 15:11:56 +030094static int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -080095{
Phil Reide70002c802017-01-06 17:35:13 +080096 char name[32];
Phil Reid13288bd2016-12-22 17:18:12 +080097
Phil Reide70002c802017-01-06 17:35:13 +080098 snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
Phil Reid13288bd2016-12-22 17:18:12 +080099 dws->debugfs = debugfs_create_dir(name, NULL);
Feng Tange24c7452009-12-14 14:20:22 -0800100 if (!dws->debugfs)
101 return -ENOMEM;
102
103 debugfs_create_file("registers", S_IFREG | S_IRUGO,
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300104 dws->debugfs, (void *)dws, &dw_spi_regs_ops);
Feng Tange24c7452009-12-14 14:20:22 -0800105 return 0;
106}
107
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300108static void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800109{
Jingoo Hanfadcace2014-09-02 11:49:24 +0900110 debugfs_remove_recursive(dws->debugfs);
Feng Tange24c7452009-12-14 14:20:22 -0800111}
112
113#else
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300114static inline int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800115{
George Shore20a588f2010-01-21 11:40:49 +0000116 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800117}
118
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300119static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800120{
121}
122#endif /* CONFIG_DEBUG_FS */
123
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200124void dw_spi_set_cs(struct spi_device *spi, bool enable)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200125{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200126 struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200127
Charles Keepaxada9e3f2019-11-27 15:39:36 +0000128 if (!enable)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200129 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
Talel Shenharf2d70472018-10-11 14:20:07 +0300130 else if (dws->cs_override)
131 dw_writel(dws, DW_SPI_SER, 0);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200132}
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200133EXPORT_SYMBOL_GPL(dw_spi_set_cs);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200134
Alek Du2ff271b2011-03-30 23:09:54 +0800135/* Return the max entries we can fill into tx fifo */
136static inline u32 tx_max(struct dw_spi *dws)
137{
138 u32 tx_left, tx_room, rxtx_gap;
139
140 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
Thor Thayerdd114442015-03-12 14:19:31 -0500141 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
Alek Du2ff271b2011-03-30 23:09:54 +0800142
143 /*
144 * Another concern is about the tx/rx mismatch, we
145 * though to use (dws->fifo_len - rxflr - txflr) as
146 * one maximum value for tx, but it doesn't cover the
147 * data which is out of tx/rx fifo and inside the
148 * shift registers. So a control from sw point of
149 * view is taken.
150 */
151 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
152 / dws->n_bytes;
153
154 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
155}
156
157/* Return the max entries we should read out of rx fifo */
158static inline u32 rx_max(struct dw_spi *dws)
159{
160 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
161
Thor Thayerdd114442015-03-12 14:19:31 -0500162 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
Alek Du2ff271b2011-03-30 23:09:54 +0800163}
164
Alek Du3b8a4dd2011-03-30 23:09:55 +0800165static void dw_writer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800166{
wuxu.wu19b61392020-01-01 11:39:41 +0800167 u32 max;
Feng Tangde6efe02011-03-30 23:09:52 +0800168 u16 txw = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800169
wuxu.wu19b61392020-01-01 11:39:41 +0800170 spin_lock(&dws->buf_lock);
171 max = tx_max(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800172 while (max--) {
173 /* Set the tx word if the transfer's original "tx" is not null */
174 if (dws->tx_end - dws->len) {
175 if (dws->n_bytes == 1)
176 txw = *(u8 *)(dws->tx);
177 else
178 txw = *(u16 *)(dws->tx);
179 }
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200180 dw_write_io_reg(dws, DW_SPI_DR, txw);
Alek Du2ff271b2011-03-30 23:09:54 +0800181 dws->tx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800182 }
wuxu.wu19b61392020-01-01 11:39:41 +0800183 spin_unlock(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800184}
185
Alek Du3b8a4dd2011-03-30 23:09:55 +0800186static void dw_reader(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800187{
wuxu.wu19b61392020-01-01 11:39:41 +0800188 u32 max;
Feng Tangde6efe02011-03-30 23:09:52 +0800189 u16 rxw;
Feng Tange24c7452009-12-14 14:20:22 -0800190
wuxu.wu19b61392020-01-01 11:39:41 +0800191 spin_lock(&dws->buf_lock);
192 max = rx_max(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800193 while (max--) {
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200194 rxw = dw_read_io_reg(dws, DW_SPI_DR);
Feng Tangde6efe02011-03-30 23:09:52 +0800195 /* Care rx only if the transfer's original "rx" is not null */
196 if (dws->rx_end - dws->len) {
197 if (dws->n_bytes == 1)
198 *(u8 *)(dws->rx) = rxw;
199 else
200 *(u16 *)(dws->rx) = rxw;
201 }
202 dws->rx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800203 }
wuxu.wu19b61392020-01-01 11:39:41 +0800204 spin_unlock(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800205}
206
Feng Tange24c7452009-12-14 14:20:22 -0800207static void int_error_stop(struct dw_spi *dws, const char *msg)
208{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200209 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800210
211 dev_err(&dws->master->dev, "%s\n", msg);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200212 dws->master->cur_msg->status = -EIO;
213 spi_finalize_current_transfer(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800214}
215
Feng Tange24c7452009-12-14 14:20:22 -0800216static irqreturn_t interrupt_transfer(struct dw_spi *dws)
217{
Thor Thayerdd114442015-03-12 14:19:31 -0500218 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Feng Tange24c7452009-12-14 14:20:22 -0800219
Feng Tange24c7452009-12-14 14:20:22 -0800220 /* Error handling */
221 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
Thor Thayerdd114442015-03-12 14:19:31 -0500222 dw_readl(dws, DW_SPI_ICR);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800223 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
Feng Tange24c7452009-12-14 14:20:22 -0800224 return IRQ_HANDLED;
225 }
226
Alek Du3b8a4dd2011-03-30 23:09:55 +0800227 dw_reader(dws);
228 if (dws->rx_end == dws->rx) {
229 spi_mask_intr(dws, SPI_INT_TXEI);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200230 spi_finalize_current_transfer(dws->master);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800231 return IRQ_HANDLED;
232 }
Feng Tang552e4502010-01-20 13:49:45 -0700233 if (irq_status & SPI_INT_TXEI) {
234 spi_mask_intr(dws, SPI_INT_TXEI);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800235 dw_writer(dws);
236 /* Enable TX irq always, it will be disabled when RX finished */
237 spi_umask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800238 }
Feng Tang552e4502010-01-20 13:49:45 -0700239
Feng Tange24c7452009-12-14 14:20:22 -0800240 return IRQ_HANDLED;
241}
242
243static irqreturn_t dw_spi_irq(int irq, void *dev_id)
244{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200245 struct spi_controller *master = dev_id;
246 struct dw_spi *dws = spi_controller_get_devdata(master);
Thor Thayerdd114442015-03-12 14:19:31 -0500247 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
Yong Wangcbcc0622010-09-07 15:27:27 +0800248
Yong Wangcbcc0622010-09-07 15:27:27 +0800249 if (!irq_status)
250 return IRQ_NONE;
Feng Tange24c7452009-12-14 14:20:22 -0800251
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200252 if (!master->cur_msg) {
Feng Tange24c7452009-12-14 14:20:22 -0800253 spi_mask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800254 return IRQ_HANDLED;
255 }
256
257 return dws->transfer_handler(dws);
258}
259
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800260/* Configure CTRLR0 for DW_apb_ssi */
261u32 dw_spi_update_cr0(struct spi_controller *master, struct spi_device *spi,
262 struct spi_transfer *transfer)
263{
264 struct dw_spi *dws = spi_controller_get_devdata(master);
265 struct chip_data *chip = spi_get_ctldata(spi);
266 u32 cr0;
267
268 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
269 cr0 = (transfer->bits_per_word - 1)
270 | (chip->type << SPI_FRF_OFFSET)
271 | ((((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET) |
272 (((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET) |
273 (((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET))
274 | (chip->tmode << SPI_TMOD_OFFSET);
275
276 return cr0;
277}
278EXPORT_SYMBOL_GPL(dw_spi_update_cr0);
279
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800280/* Configure CTRLR0 for DWC_ssi */
281u32 dw_spi_update_cr0_v1_01a(struct spi_controller *master,
282 struct spi_device *spi,
283 struct spi_transfer *transfer)
284{
285 struct dw_spi *dws = spi_controller_get_devdata(master);
286 struct chip_data *chip = spi_get_ctldata(spi);
287 u32 cr0;
288
289 /* CTRLR0[ 4: 0] Data Frame Size */
290 cr0 = (transfer->bits_per_word - 1);
291
292 /* CTRLR0[ 7: 6] Frame Format */
293 cr0 |= chip->type << DWC_SSI_CTRLR0_FRF_OFFSET;
294
295 /*
296 * SPI mode (SCPOL|SCPH)
297 * CTRLR0[ 8] Serial Clock Phase
298 * CTRLR0[ 9] Serial Clock Polarity
299 */
300 cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << DWC_SSI_CTRLR0_SCPOL_OFFSET;
301 cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << DWC_SSI_CTRLR0_SCPH_OFFSET;
302
303 /* CTRLR0[11:10] Transfer Mode */
304 cr0 |= chip->tmode << DWC_SSI_CTRLR0_TMOD_OFFSET;
305
306 /* CTRLR0[13] Shift Register Loop */
307 cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << DWC_SSI_CTRLR0_SRL_OFFSET;
308
309 return cr0;
310}
311EXPORT_SYMBOL_GPL(dw_spi_update_cr0_v1_01a);
312
Jarkko Nikula721483e2018-02-01 17:17:29 +0200313static int dw_spi_transfer_one(struct spi_controller *master,
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200314 struct spi_device *spi, struct spi_transfer *transfer)
Feng Tange24c7452009-12-14 14:20:22 -0800315{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200316 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200317 struct chip_data *chip = spi_get_ctldata(spi);
wuxu.wu19b61392020-01-01 11:39:41 +0800318 unsigned long flags;
Feng Tange24c7452009-12-14 14:20:22 -0800319 u8 imask = 0;
Andy Shevchenkoea113702015-02-24 13:32:11 +0200320 u16 txlevel = 0;
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300321 u32 cr0;
Andy Shevchenko9f145382015-03-09 16:48:46 +0200322 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800323
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200324 dws->dma_mapped = 0;
wuxu.wu19b61392020-01-01 11:39:41 +0800325 spin_lock_irqsave(&dws->buf_lock, flags);
Feng Tange24c7452009-12-14 14:20:22 -0800326 dws->tx = (void *)transfer->tx_buf;
327 dws->tx_end = dws->tx + transfer->len;
328 dws->rx = transfer->rx_buf;
329 dws->rx_end = dws->rx + transfer->len;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200330 dws->len = transfer->len;
wuxu.wu19b61392020-01-01 11:39:41 +0800331 spin_unlock_irqrestore(&dws->buf_lock, flags);
Feng Tange24c7452009-12-14 14:20:22 -0800332
Xinwei Kongbfda0442020-01-03 10:52:10 +0800333 /* Ensure dw->rx and dw->rx_end are visible */
334 smp_mb();
335
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200336 spi_enable_chip(dws, 0);
337
Feng Tange24c7452009-12-14 14:20:22 -0800338 /* Handle per transfer options for bpw and speed */
Matthias Seidel13b10302016-09-04 02:04:49 +0200339 if (transfer->speed_hz != dws->current_freq) {
340 if (transfer->speed_hz != chip->speed_hz) {
341 /* clk_div doesn't support odd number */
Matthias Seidel3aef4632016-09-07 17:45:30 +0200342 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
Matthias Seidel13b10302016-09-04 02:04:49 +0200343 chip->speed_hz = transfer->speed_hz;
344 }
345 dws->current_freq = transfer->speed_hz;
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300346 spi_set_clk(dws, chip->clk_div);
Feng Tange24c7452009-12-14 14:20:22 -0800347 }
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200348
349 dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
350 dws->dma_width = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
351
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800352 cr0 = dws->update_cr0(master, spi, transfer);
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +0800353 dw_writel(dws, DW_SPI_CTRLR0, cr0);
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200354
Feng Tange24c7452009-12-14 14:20:22 -0800355 /* Check if current transfer is a DMA transaction */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200356 if (master->can_dma && master->can_dma(master, spi, transfer))
357 dws->dma_mapped = master->cur_msg_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800358
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200359 /* For poll mode just disable all interrupts */
360 spi_mask_intr(dws, 0xff);
361
Feng Tang552e4502010-01-20 13:49:45 -0700362 /*
363 * Interrupt mode
364 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
365 */
Andy Shevchenko9f145382015-03-09 16:48:46 +0200366 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200367 ret = dws->dma_ops->dma_setup(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200368 if (ret < 0) {
369 spi_enable_chip(dws, 1);
370 return ret;
371 }
Clement Leger33e8fd42020-04-16 13:09:16 +0200372 } else {
Andy Shevchenkoea113702015-02-24 13:32:11 +0200373 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +0800374 dw_writel(dws, DW_SPI_TXFTLR, txlevel);
Feng Tang552e4502010-01-20 13:49:45 -0700375
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200376 /* Set the interrupt mask */
Jingoo Hanfadcace2014-09-02 11:49:24 +0900377 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
378 SPI_INT_RXUI | SPI_INT_RXOI;
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200379 spi_umask_intr(dws, imask);
380
Feng Tange24c7452009-12-14 14:20:22 -0800381 dws->transfer_handler = interrupt_transfer;
382 }
383
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200384 spi_enable_chip(dws, 1);
Feng Tange24c7452009-12-14 14:20:22 -0800385
Andy Shevchenko9f145382015-03-09 16:48:46 +0200386 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200387 ret = dws->dma_ops->dma_transfer(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200388 if (ret < 0)
389 return ret;
390 }
Feng Tange24c7452009-12-14 14:20:22 -0800391
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200392 return 1;
Feng Tange24c7452009-12-14 14:20:22 -0800393}
394
Jarkko Nikula721483e2018-02-01 17:17:29 +0200395static void dw_spi_handle_err(struct spi_controller *master,
Baruch Siachec37e8e2014-01-31 12:07:44 +0200396 struct spi_message *msg)
Feng Tange24c7452009-12-14 14:20:22 -0800397{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200398 struct dw_spi *dws = spi_controller_get_devdata(master);
Feng Tange24c7452009-12-14 14:20:22 -0800399
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200400 if (dws->dma_mapped)
401 dws->dma_ops->dma_stop(dws);
402
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200403 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800404}
405
406/* This may be called twice for each spi dev */
407static int dw_spi_setup(struct spi_device *spi)
408{
Feng Tange24c7452009-12-14 14:20:22 -0800409 struct chip_data *chip;
410
Feng Tange24c7452009-12-14 14:20:22 -0800411 /* Only alloc on first setup */
412 chip = spi_get_ctldata(spi);
413 if (!chip) {
Axel Lina97c8832014-08-31 12:47:06 +0800414 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Feng Tange24c7452009-12-14 14:20:22 -0800415 if (!chip)
416 return -ENOMEM;
Baruch Siach43f627a2013-12-30 20:30:46 +0200417 spi_set_ctldata(spi, chip);
Feng Tange24c7452009-12-14 14:20:22 -0800418 }
419
Jisheng Zhang60968282015-12-23 19:05:39 +0800420 chip->tmode = SPI_TMOD_TR;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300421
Feng Tange24c7452009-12-14 14:20:22 -0800422 return 0;
423}
424
Axel Lina97c8832014-08-31 12:47:06 +0800425static void dw_spi_cleanup(struct spi_device *spi)
426{
427 struct chip_data *chip = spi_get_ctldata(spi);
428
429 kfree(chip);
430 spi_set_ctldata(spi, NULL);
431}
432
Feng Tange24c7452009-12-14 14:20:22 -0800433/* Restart the controller, disable all interrupts, clean rx fifo */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200434static void spi_hw_init(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800435{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200436 spi_reset_chip(dws);
Feng Tangc587b6f2010-01-21 10:41:10 +0800437
438 /*
439 * Try to detect the FIFO depth if not set by interface driver,
440 * the depth could be from 2 to 256 from HW spec
441 */
442 if (!dws->fifo_len) {
443 u32 fifo;
Jingoo Hanfadcace2014-09-02 11:49:24 +0900444
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200445 for (fifo = 1; fifo < 256; fifo++) {
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +0800446 dw_writel(dws, DW_SPI_TXFTLR, fifo);
447 if (fifo != dw_readl(dws, DW_SPI_TXFTLR))
Feng Tangc587b6f2010-01-21 10:41:10 +0800448 break;
449 }
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +0800450 dw_writel(dws, DW_SPI_TXFTLR, 0);
Feng Tangc587b6f2010-01-21 10:41:10 +0800451
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200452 dws->fifo_len = (fifo == 1) ? 0 : fifo;
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200453 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
Feng Tangc587b6f2010-01-21 10:41:10 +0800454 }
Talel Shenharf2d70472018-10-11 14:20:07 +0300455
456 /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
457 if (dws->cs_override)
458 dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
Feng Tange24c7452009-12-14 14:20:22 -0800459}
460
Baruch Siach04f421e2013-12-30 20:30:44 +0200461int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800462{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200463 struct spi_controller *master;
Feng Tange24c7452009-12-14 14:20:22 -0800464 int ret;
465
Aditya Pakki169f9ac2019-12-05 17:14:21 -0600466 if (!dws)
467 return -EINVAL;
Feng Tange24c7452009-12-14 14:20:22 -0800468
Baruch Siach04f421e2013-12-30 20:30:44 +0200469 master = spi_alloc_master(dev, 0);
470 if (!master)
471 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -0800472
473 dws->master = master;
474 dws->type = SSI_MOTO_SPI;
Feng Tange24c7452009-12-14 14:20:22 -0800475 dws->dma_inited = 0;
Andy Shevchenkod7ef54c2015-10-27 17:48:16 +0200476 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
wuxu.wu19b61392020-01-01 11:39:41 +0800477 spin_lock_init(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800478
Alexandre Belloni66b19d72018-07-17 16:23:10 +0200479 spi_controller_set_devdata(master, dws);
480
Phil Reide70002c802017-01-06 17:35:13 +0800481 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
482 master);
Feng Tange24c7452009-12-14 14:20:22 -0800483 if (ret < 0) {
Andy Shevchenko5f0966e2015-10-14 23:12:17 +0300484 dev_err(dev, "can not get IRQ\n");
Feng Tange24c7452009-12-14 14:20:22 -0800485 goto err_free_master;
486 }
487
Linus Walleij9400c412019-01-07 16:51:56 +0100488 master->use_gpio_descriptors = true;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300489 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200490 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Feng Tange24c7452009-12-14 14:20:22 -0800491 master->bus_num = dws->bus_num;
492 master->num_chipselect = dws->num_cs;
Feng Tange24c7452009-12-14 14:20:22 -0800493 master->setup = dw_spi_setup;
Axel Lina97c8832014-08-31 12:47:06 +0800494 master->cleanup = dw_spi_cleanup;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200495 master->set_cs = dw_spi_set_cs;
496 master->transfer_one = dw_spi_transfer_one;
497 master->handle_err = dw_spi_handle_err;
Axel Lin765ee702014-02-20 21:37:56 +0800498 master->max_speed_hz = dws->max_freq;
Thor Thayer9c6de472014-10-08 13:51:34 -0500499 master->dev.of_node = dev->of_node;
Jay Fang32215a62018-12-03 11:15:50 +0800500 master->dev.fwnode = dev->fwnode;
Thor Thayer80b444e2016-10-10 09:25:25 -0500501 master->flags = SPI_MASTER_GPIO_SS;
Phil Edworthy1e695982019-09-18 09:04:35 +0100502 master->auto_runtime_pm = true;
Feng Tange24c7452009-12-14 14:20:22 -0800503
Alexandre Belloni62dbbae2018-07-17 16:23:11 +0200504 if (dws->set_cs)
505 master->set_cs = dws->set_cs;
506
Feng Tange24c7452009-12-14 14:20:22 -0800507 /* Basic HW init */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200508 spi_hw_init(dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -0800509
Feng Tang7063c0d2010-12-24 13:59:11 +0800510 if (dws->dma_ops && dws->dma_ops->dma_init) {
511 ret = dws->dma_ops->dma_init(dws);
512 if (ret) {
Andy Shevchenko3dbb3b92015-01-07 16:56:54 +0200513 dev_warn(dev, "DMA init failed\n");
Feng Tang7063c0d2010-12-24 13:59:11 +0800514 dws->dma_inited = 0;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200515 } else {
516 master->can_dma = dws->dma_ops->can_dma;
Feng Tang7063c0d2010-12-24 13:59:11 +0800517 }
518 }
519
Jarkko Nikula721483e2018-02-01 17:17:29 +0200520 ret = devm_spi_register_controller(dev, master);
Feng Tange24c7452009-12-14 14:20:22 -0800521 if (ret) {
522 dev_err(&master->dev, "problem registering spi master\n");
Baruch Siachec37e8e2014-01-31 12:07:44 +0200523 goto err_dma_exit;
Feng Tange24c7452009-12-14 14:20:22 -0800524 }
525
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300526 dw_spi_debugfs_init(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800527 return 0;
528
Baruch Siachec37e8e2014-01-31 12:07:44 +0200529err_dma_exit:
Feng Tang7063c0d2010-12-24 13:59:11 +0800530 if (dws->dma_ops && dws->dma_ops->dma_exit)
531 dws->dma_ops->dma_exit(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800532 spi_enable_chip(dws, 0);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300533 free_irq(dws->irq, master);
Feng Tange24c7452009-12-14 14:20:22 -0800534err_free_master:
Jarkko Nikula721483e2018-02-01 17:17:29 +0200535 spi_controller_put(master);
Feng Tange24c7452009-12-14 14:20:22 -0800536 return ret;
537}
Feng Tang79290a22010-12-24 13:59:10 +0800538EXPORT_SYMBOL_GPL(dw_spi_add_host);
Feng Tange24c7452009-12-14 14:20:22 -0800539
Grant Likelyfd4a3192012-12-07 16:57:14 +0000540void dw_spi_remove_host(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800541{
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300542 dw_spi_debugfs_remove(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800543
Feng Tang7063c0d2010-12-24 13:59:11 +0800544 if (dws->dma_ops && dws->dma_ops->dma_exit)
545 dws->dma_ops->dma_exit(dws);
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300546
547 spi_shutdown_chip(dws);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300548
549 free_irq(dws->irq, dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800550}
Feng Tang79290a22010-12-24 13:59:10 +0800551EXPORT_SYMBOL_GPL(dw_spi_remove_host);
Feng Tange24c7452009-12-14 14:20:22 -0800552
553int dw_spi_suspend_host(struct dw_spi *dws)
554{
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300555 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800556
Jarkko Nikula721483e2018-02-01 17:17:29 +0200557 ret = spi_controller_suspend(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800558 if (ret)
559 return ret;
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300560
561 spi_shutdown_chip(dws);
562 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800563}
Feng Tang79290a22010-12-24 13:59:10 +0800564EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
Feng Tange24c7452009-12-14 14:20:22 -0800565
566int dw_spi_resume_host(struct dw_spi *dws)
567{
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200568 spi_hw_init(&dws->master->dev, dws);
Geert Uytterhoeven7c5d8a22018-09-05 10:51:57 +0200569 return spi_controller_resume(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800570}
Feng Tang79290a22010-12-24 13:59:10 +0800571EXPORT_SYMBOL_GPL(dw_spi_resume_host);
Feng Tange24c7452009-12-14 14:20:22 -0800572
573MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
574MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
575MODULE_LICENSE("GPL v2");