blob: 91f44e3e1930865ed7704836624c11af0ca0aaf0 [file] [log] [blame]
Feng Tange24c7452009-12-14 14:20:22 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Designware SPI core controller driver (refer pxa2xx_spi.c)
Feng Tange24c7452009-12-14 14:20:22 -08003 *
4 * Copyright (c) 2009, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
Feng Tange24c7452009-12-14 14:20:22 -080014 */
15
16#include <linux/dma-mapping.h>
17#include <linux/interrupt.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040018#include <linux/module.h>
Feng Tange24c7452009-12-14 14:20:22 -080019#include <linux/highmem.h>
20#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Feng Tange24c7452009-12-14 14:20:22 -080022#include <linux/spi/spi.h>
Baruch Siachd9c73bb2014-01-31 12:07:47 +020023#include <linux/gpio.h>
Feng Tange24c7452009-12-14 14:20:22 -080024
Grant Likelyca632f52011-06-06 01:16:30 -060025#include "spi-dw.h"
Grant Likely568a60e2011-02-28 12:47:12 -070026
Feng Tange24c7452009-12-14 14:20:22 -080027#ifdef CONFIG_DEBUG_FS
28#include <linux/debugfs.h>
29#endif
30
Feng Tange24c7452009-12-14 14:20:22 -080031/* Slave spi_dev related */
32struct chip_data {
Feng Tange24c7452009-12-14 14:20:22 -080033 u8 cs; /* chip select pin */
Feng Tange24c7452009-12-14 14:20:22 -080034 u8 tmode; /* TR/TO/RO/EEPROM */
35 u8 type; /* SPI/SSP/MicroWire */
36
37 u8 poll_mode; /* 1 means use poll mode */
38
Feng Tange24c7452009-12-14 14:20:22 -080039 u8 enable_dma;
Feng Tange24c7452009-12-14 14:20:22 -080040 u16 clk_div; /* baud rate divider */
41 u32 speed_hz; /* baud rate */
Feng Tange24c7452009-12-14 14:20:22 -080042 void (*cs_control)(u32 command);
43};
44
45#ifdef CONFIG_DEBUG_FS
Feng Tange24c7452009-12-14 14:20:22 -080046#define SPI_REGS_BUFSIZE 1024
Andy Shevchenko53288fe2014-09-12 15:11:56 +030047static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
48 size_t count, loff_t *ppos)
Feng Tange24c7452009-12-14 14:20:22 -080049{
Andy Shevchenko53288fe2014-09-12 15:11:56 +030050 struct dw_spi *dws = file->private_data;
Feng Tange24c7452009-12-14 14:20:22 -080051 char *buf;
52 u32 len = 0;
53 ssize_t ret;
54
Feng Tange24c7452009-12-14 14:20:22 -080055 buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
56 if (!buf)
57 return 0;
58
59 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
Andy Shevchenko53288fe2014-09-12 15:11:56 +030060 "%s registers:\n", dev_name(&dws->master->dev));
Feng Tange24c7452009-12-14 14:20:22 -080061 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
62 "=================================\n");
63 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070064 "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
Feng Tange24c7452009-12-14 14:20:22 -080065 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070066 "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
Feng Tange24c7452009-12-14 14:20:22 -080067 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070068 "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
Feng Tange24c7452009-12-14 14:20:22 -080069 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070070 "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
Feng Tange24c7452009-12-14 14:20:22 -080071 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070072 "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
Feng Tange24c7452009-12-14 14:20:22 -080073 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070074 "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
Feng Tange24c7452009-12-14 14:20:22 -080075 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070076 "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
Feng Tange24c7452009-12-14 14:20:22 -080077 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070078 "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
Feng Tange24c7452009-12-14 14:20:22 -080079 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070080 "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
Feng Tange24c7452009-12-14 14:20:22 -080081 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070082 "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
Feng Tange24c7452009-12-14 14:20:22 -080083 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070084 "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
Feng Tange24c7452009-12-14 14:20:22 -080085 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070086 "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
Feng Tange24c7452009-12-14 14:20:22 -080087 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070088 "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
Feng Tange24c7452009-12-14 14:20:22 -080089 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070090 "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
Feng Tange24c7452009-12-14 14:20:22 -080091 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070092 "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
Feng Tange24c7452009-12-14 14:20:22 -080093 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
94 "=================================\n");
95
Andy Shevchenko53288fe2014-09-12 15:11:56 +030096 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
Feng Tange24c7452009-12-14 14:20:22 -080097 kfree(buf);
98 return ret;
99}
100
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300101static const struct file_operations dw_spi_regs_ops = {
Feng Tange24c7452009-12-14 14:20:22 -0800102 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700103 .open = simple_open,
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300104 .read = dw_spi_show_regs,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200105 .llseek = default_llseek,
Feng Tange24c7452009-12-14 14:20:22 -0800106};
107
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300108static int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800109{
Phil Reid14e5c8c2016-12-22 17:18:12 +0800110 char name[128];
111
112 snprintf(name, 128, "dw_spi-%s", dev_name(&dws->master->dev));
113 dws->debugfs = debugfs_create_dir(name, NULL);
Feng Tange24c7452009-12-14 14:20:22 -0800114 if (!dws->debugfs)
115 return -ENOMEM;
116
117 debugfs_create_file("registers", S_IFREG | S_IRUGO,
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300118 dws->debugfs, (void *)dws, &dw_spi_regs_ops);
Feng Tange24c7452009-12-14 14:20:22 -0800119 return 0;
120}
121
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300122static void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800123{
Jingoo Hanfadcace2014-09-02 11:49:24 +0900124 debugfs_remove_recursive(dws->debugfs);
Feng Tange24c7452009-12-14 14:20:22 -0800125}
126
127#else
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300128static inline int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800129{
George Shore20a588f2010-01-21 11:40:49 +0000130 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800131}
132
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300133static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800134{
135}
136#endif /* CONFIG_DEBUG_FS */
137
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200138static void dw_spi_set_cs(struct spi_device *spi, bool enable)
139{
140 struct dw_spi *dws = spi_master_get_devdata(spi->master);
141 struct chip_data *chip = spi_get_ctldata(spi);
142
143 /* Chip select logic is inverted from spi_set_cs() */
Andy Shevchenko207cda92015-03-25 20:26:26 +0200144 if (chip && chip->cs_control)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200145 chip->cs_control(!enable);
146
147 if (!enable)
148 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
149}
150
Alek Du2ff271b2011-03-30 23:09:54 +0800151/* Return the max entries we can fill into tx fifo */
152static inline u32 tx_max(struct dw_spi *dws)
153{
154 u32 tx_left, tx_room, rxtx_gap;
155
156 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
Thor Thayerdd114442015-03-12 14:19:31 -0500157 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
Alek Du2ff271b2011-03-30 23:09:54 +0800158
159 /*
160 * Another concern is about the tx/rx mismatch, we
161 * though to use (dws->fifo_len - rxflr - txflr) as
162 * one maximum value for tx, but it doesn't cover the
163 * data which is out of tx/rx fifo and inside the
164 * shift registers. So a control from sw point of
165 * view is taken.
166 */
167 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
168 / dws->n_bytes;
169
170 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
171}
172
173/* Return the max entries we should read out of rx fifo */
174static inline u32 rx_max(struct dw_spi *dws)
175{
176 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
177
Thor Thayerdd114442015-03-12 14:19:31 -0500178 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
Alek Du2ff271b2011-03-30 23:09:54 +0800179}
180
Alek Du3b8a4dd2011-03-30 23:09:55 +0800181static void dw_writer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800182{
wuxu.wu15029152020-01-01 11:39:41 +0800183 u32 max;
Feng Tangde6efe02011-03-30 23:09:52 +0800184 u16 txw = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800185
wuxu.wu15029152020-01-01 11:39:41 +0800186 spin_lock(&dws->buf_lock);
187 max = tx_max(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800188 while (max--) {
189 /* Set the tx word if the transfer's original "tx" is not null */
190 if (dws->tx_end - dws->len) {
191 if (dws->n_bytes == 1)
192 txw = *(u8 *)(dws->tx);
193 else
194 txw = *(u16 *)(dws->tx);
195 }
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200196 dw_write_io_reg(dws, DW_SPI_DR, txw);
Alek Du2ff271b2011-03-30 23:09:54 +0800197 dws->tx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800198 }
wuxu.wu15029152020-01-01 11:39:41 +0800199 spin_unlock(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800200}
201
Alek Du3b8a4dd2011-03-30 23:09:55 +0800202static void dw_reader(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800203{
wuxu.wu15029152020-01-01 11:39:41 +0800204 u32 max;
Feng Tangde6efe02011-03-30 23:09:52 +0800205 u16 rxw;
Feng Tange24c7452009-12-14 14:20:22 -0800206
wuxu.wu15029152020-01-01 11:39:41 +0800207 spin_lock(&dws->buf_lock);
208 max = rx_max(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800209 while (max--) {
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200210 rxw = dw_read_io_reg(dws, DW_SPI_DR);
Feng Tangde6efe02011-03-30 23:09:52 +0800211 /* Care rx only if the transfer's original "rx" is not null */
212 if (dws->rx_end - dws->len) {
213 if (dws->n_bytes == 1)
214 *(u8 *)(dws->rx) = rxw;
215 else
216 *(u16 *)(dws->rx) = rxw;
217 }
218 dws->rx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800219 }
wuxu.wu15029152020-01-01 11:39:41 +0800220 spin_unlock(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800221}
222
Feng Tange24c7452009-12-14 14:20:22 -0800223static void int_error_stop(struct dw_spi *dws, const char *msg)
224{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200225 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800226
227 dev_err(&dws->master->dev, "%s\n", msg);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200228 dws->master->cur_msg->status = -EIO;
229 spi_finalize_current_transfer(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800230}
231
Feng Tange24c7452009-12-14 14:20:22 -0800232static irqreturn_t interrupt_transfer(struct dw_spi *dws)
233{
Thor Thayerdd114442015-03-12 14:19:31 -0500234 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Feng Tange24c7452009-12-14 14:20:22 -0800235
Feng Tange24c7452009-12-14 14:20:22 -0800236 /* Error handling */
237 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
Thor Thayerdd114442015-03-12 14:19:31 -0500238 dw_readl(dws, DW_SPI_ICR);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800239 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
Feng Tange24c7452009-12-14 14:20:22 -0800240 return IRQ_HANDLED;
241 }
242
Alek Du3b8a4dd2011-03-30 23:09:55 +0800243 dw_reader(dws);
244 if (dws->rx_end == dws->rx) {
245 spi_mask_intr(dws, SPI_INT_TXEI);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200246 spi_finalize_current_transfer(dws->master);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800247 return IRQ_HANDLED;
248 }
Feng Tang552e4502010-01-20 13:49:45 -0700249 if (irq_status & SPI_INT_TXEI) {
250 spi_mask_intr(dws, SPI_INT_TXEI);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800251 dw_writer(dws);
252 /* Enable TX irq always, it will be disabled when RX finished */
253 spi_umask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800254 }
Feng Tang552e4502010-01-20 13:49:45 -0700255
Feng Tange24c7452009-12-14 14:20:22 -0800256 return IRQ_HANDLED;
257}
258
259static irqreturn_t dw_spi_irq(int irq, void *dev_id)
260{
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200261 struct spi_master *master = dev_id;
262 struct dw_spi *dws = spi_master_get_devdata(master);
Thor Thayerdd114442015-03-12 14:19:31 -0500263 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
Yong Wangcbcc0622010-09-07 15:27:27 +0800264
Yong Wangcbcc0622010-09-07 15:27:27 +0800265 if (!irq_status)
266 return IRQ_NONE;
Feng Tange24c7452009-12-14 14:20:22 -0800267
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200268 if (!master->cur_msg) {
Feng Tange24c7452009-12-14 14:20:22 -0800269 spi_mask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800270 return IRQ_HANDLED;
271 }
272
273 return dws->transfer_handler(dws);
274}
275
276/* Must be called inside pump_transfers() */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200277static int poll_transfer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800278{
Alek Du2ff271b2011-03-30 23:09:54 +0800279 do {
280 dw_writer(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800281 dw_reader(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800282 cpu_relax();
283 } while (dws->rx_end > dws->rx);
Feng Tange24c7452009-12-14 14:20:22 -0800284
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200285 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800286}
287
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200288static int dw_spi_transfer_one(struct spi_master *master,
289 struct spi_device *spi, struct spi_transfer *transfer)
Feng Tange24c7452009-12-14 14:20:22 -0800290{
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200291 struct dw_spi *dws = spi_master_get_devdata(master);
292 struct chip_data *chip = spi_get_ctldata(spi);
wuxu.wu15029152020-01-01 11:39:41 +0800293 unsigned long flags;
Feng Tange24c7452009-12-14 14:20:22 -0800294 u8 imask = 0;
Andy Shevchenkoea113702015-02-24 13:32:11 +0200295 u16 txlevel = 0;
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300296 u32 cr0;
Andy Shevchenko9f145382015-03-09 16:48:46 +0200297 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800298
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200299 dws->dma_mapped = 0;
wuxu.wu15029152020-01-01 11:39:41 +0800300 spin_lock_irqsave(&dws->buf_lock, flags);
Feng Tange24c7452009-12-14 14:20:22 -0800301 dws->tx = (void *)transfer->tx_buf;
302 dws->tx_end = dws->tx + transfer->len;
303 dws->rx = transfer->rx_buf;
304 dws->rx_end = dws->rx + transfer->len;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200305 dws->len = transfer->len;
wuxu.wu15029152020-01-01 11:39:41 +0800306 spin_unlock_irqrestore(&dws->buf_lock, flags);
Feng Tange24c7452009-12-14 14:20:22 -0800307
Xinwei Kong57209012020-01-03 10:52:10 +0800308 /* Ensure dw->rx and dw->rx_end are visible */
309 smp_mb();
310
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200311 spi_enable_chip(dws, 0);
312
Feng Tange24c7452009-12-14 14:20:22 -0800313 /* Handle per transfer options for bpw and speed */
Matthias Seidel13b10302016-09-04 02:04:49 +0200314 if (transfer->speed_hz != dws->current_freq) {
315 if (transfer->speed_hz != chip->speed_hz) {
316 /* clk_div doesn't support odd number */
Matthias Seidel3aef4632016-09-07 17:45:30 +0200317 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
Matthias Seidel13b10302016-09-04 02:04:49 +0200318 chip->speed_hz = transfer->speed_hz;
319 }
320 dws->current_freq = transfer->speed_hz;
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300321 spi_set_clk(dws, chip->clk_div);
Feng Tange24c7452009-12-14 14:20:22 -0800322 }
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300323 if (transfer->bits_per_word == 8) {
324 dws->n_bytes = 1;
325 dws->dma_width = 1;
326 } else if (transfer->bits_per_word == 16) {
327 dws->n_bytes = 2;
328 dws->dma_width = 2;
Andy Shevchenko863cb2f2015-10-14 23:12:20 +0300329 } else {
330 return -EINVAL;
Feng Tange24c7452009-12-14 14:20:22 -0800331 }
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300332 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300333 cr0 = (transfer->bits_per_word - 1)
334 | (chip->type << SPI_FRF_OFFSET)
335 | (spi->mode << SPI_MODE_OFFSET)
336 | (chip->tmode << SPI_TMOD_OFFSET);
Feng Tange24c7452009-12-14 14:20:22 -0800337
George Shore052dc7c2010-01-21 11:40:52 +0000338 /*
339 * Adjust transfer mode if necessary. Requires platform dependent
340 * chipselect mechanism.
341 */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200342 if (chip->cs_control) {
George Shore052dc7c2010-01-21 11:40:52 +0000343 if (dws->rx && dws->tx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800344 chip->tmode = SPI_TMOD_TR;
George Shore052dc7c2010-01-21 11:40:52 +0000345 else if (dws->rx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800346 chip->tmode = SPI_TMOD_RO;
George Shore052dc7c2010-01-21 11:40:52 +0000347 else
Feng Tange3e55ff2010-09-07 15:52:06 +0800348 chip->tmode = SPI_TMOD_TO;
George Shore052dc7c2010-01-21 11:40:52 +0000349
Feng Tange3e55ff2010-09-07 15:52:06 +0800350 cr0 &= ~SPI_TMOD_MASK;
George Shore052dc7c2010-01-21 11:40:52 +0000351 cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
352 }
353
Thor Thayerdd114442015-03-12 14:19:31 -0500354 dw_writel(dws, DW_SPI_CTRL0, cr0);
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200355
Feng Tange24c7452009-12-14 14:20:22 -0800356 /* Check if current transfer is a DMA transaction */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200357 if (master->can_dma && master->can_dma(master, spi, transfer))
358 dws->dma_mapped = master->cur_msg_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800359
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200360 /* For poll mode just disable all interrupts */
361 spi_mask_intr(dws, 0xff);
362
Feng Tang552e4502010-01-20 13:49:45 -0700363 /*
364 * Interrupt mode
365 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
366 */
Andy Shevchenko9f145382015-03-09 16:48:46 +0200367 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200368 ret = dws->dma_ops->dma_setup(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200369 if (ret < 0) {
370 spi_enable_chip(dws, 1);
371 return ret;
372 }
373 } else if (!chip->poll_mode) {
Andy Shevchenkoea113702015-02-24 13:32:11 +0200374 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
Thor Thayerdd114442015-03-12 14:19:31 -0500375 dw_writel(dws, DW_SPI_TXFLTR, txlevel);
Feng Tang552e4502010-01-20 13:49:45 -0700376
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200377 /* Set the interrupt mask */
Jingoo Hanfadcace2014-09-02 11:49:24 +0900378 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
379 SPI_INT_RXUI | SPI_INT_RXOI;
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200380 spi_umask_intr(dws, imask);
381
Feng Tange24c7452009-12-14 14:20:22 -0800382 dws->transfer_handler = interrupt_transfer;
383 }
384
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200385 spi_enable_chip(dws, 1);
Feng Tange24c7452009-12-14 14:20:22 -0800386
Serge Semind6043872020-05-29 16:11:51 +0300387 if (dws->dma_mapped)
388 return dws->dma_ops->dma_transfer(dws, transfer);
Feng Tange24c7452009-12-14 14:20:22 -0800389
390 if (chip->poll_mode)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200391 return poll_transfer(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800392
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200393 return 1;
Feng Tange24c7452009-12-14 14:20:22 -0800394}
395
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200396static void dw_spi_handle_err(struct spi_master *master,
Baruch Siachec37e8e2014-01-31 12:07:44 +0200397 struct spi_message *msg)
Feng Tange24c7452009-12-14 14:20:22 -0800398{
Baruch Siachec37e8e2014-01-31 12:07:44 +0200399 struct dw_spi *dws = spi_master_get_devdata(master);
Feng Tange24c7452009-12-14 14:20:22 -0800400
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200401 if (dws->dma_mapped)
402 dws->dma_ops->dma_stop(dws);
403
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200404 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800405}
406
407/* This may be called twice for each spi dev */
408static int dw_spi_setup(struct spi_device *spi)
409{
410 struct dw_spi_chip *chip_info = NULL;
411 struct chip_data *chip;
Baruch Siachd9c73bb2014-01-31 12:07:47 +0200412 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800413
Feng Tange24c7452009-12-14 14:20:22 -0800414 /* Only alloc on first setup */
415 chip = spi_get_ctldata(spi);
416 if (!chip) {
Axel Lina97c8832014-08-31 12:47:06 +0800417 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Feng Tange24c7452009-12-14 14:20:22 -0800418 if (!chip)
419 return -ENOMEM;
Baruch Siach43f627a2013-12-30 20:30:46 +0200420 spi_set_ctldata(spi, chip);
Feng Tange24c7452009-12-14 14:20:22 -0800421 }
422
423 /*
424 * Protocol drivers may change the chip settings, so...
425 * if chip_info exists, use it
426 */
427 chip_info = spi->controller_data;
428
429 /* chip_info doesn't always exist */
430 if (chip_info) {
431 if (chip_info->cs_control)
432 chip->cs_control = chip_info->cs_control;
433
434 chip->poll_mode = chip_info->poll_mode;
435 chip->type = chip_info->type;
Feng Tange24c7452009-12-14 14:20:22 -0800436 }
437
Jisheng Zhang60968282015-12-23 19:05:39 +0800438 chip->tmode = SPI_TMOD_TR;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300439
Baruch Siachd9c73bb2014-01-31 12:07:47 +0200440 if (gpio_is_valid(spi->cs_gpio)) {
441 ret = gpio_direction_output(spi->cs_gpio,
442 !(spi->mode & SPI_CS_HIGH));
443 if (ret)
444 return ret;
445 }
446
Feng Tange24c7452009-12-14 14:20:22 -0800447 return 0;
448}
449
Axel Lina97c8832014-08-31 12:47:06 +0800450static void dw_spi_cleanup(struct spi_device *spi)
451{
452 struct chip_data *chip = spi_get_ctldata(spi);
453
454 kfree(chip);
455 spi_set_ctldata(spi, NULL);
456}
457
Feng Tange24c7452009-12-14 14:20:22 -0800458/* Restart the controller, disable all interrupts, clean rx fifo */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200459static void spi_hw_init(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800460{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200461 spi_reset_chip(dws);
Feng Tangc587b6f2010-01-21 10:41:10 +0800462
463 /*
464 * Try to detect the FIFO depth if not set by interface driver,
465 * the depth could be from 2 to 256 from HW spec
466 */
467 if (!dws->fifo_len) {
468 u32 fifo;
Jingoo Hanfadcace2014-09-02 11:49:24 +0900469
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200470 for (fifo = 1; fifo < 256; fifo++) {
Thor Thayerdd114442015-03-12 14:19:31 -0500471 dw_writel(dws, DW_SPI_TXFLTR, fifo);
472 if (fifo != dw_readl(dws, DW_SPI_TXFLTR))
Feng Tangc587b6f2010-01-21 10:41:10 +0800473 break;
474 }
Thor Thayerdd114442015-03-12 14:19:31 -0500475 dw_writel(dws, DW_SPI_TXFLTR, 0);
Feng Tangc587b6f2010-01-21 10:41:10 +0800476
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200477 dws->fifo_len = (fifo == 1) ? 0 : fifo;
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200478 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
Feng Tangc587b6f2010-01-21 10:41:10 +0800479 }
Feng Tange24c7452009-12-14 14:20:22 -0800480}
481
Baruch Siach04f421e2013-12-30 20:30:44 +0200482int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800483{
484 struct spi_master *master;
485 int ret;
486
487 BUG_ON(dws == NULL);
488
Baruch Siach04f421e2013-12-30 20:30:44 +0200489 master = spi_alloc_master(dev, 0);
490 if (!master)
491 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -0800492
493 dws->master = master;
494 dws->type = SSI_MOTO_SPI;
Feng Tange24c7452009-12-14 14:20:22 -0800495 dws->dma_inited = 0;
Andy Shevchenkod7ef54c2015-10-27 17:48:16 +0200496 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
Andy Shevchenkoc3c6e232014-09-18 20:08:57 +0300497 snprintf(dws->name, sizeof(dws->name), "dw_spi%d", dws->bus_num);
wuxu.wu15029152020-01-01 11:39:41 +0800498 spin_lock_init(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800499
Sasha Levin79645f02020-06-15 17:28:08 -0400500 spi_master_set_devdata(master, dws);
501
Andy Shevchenko02f20382015-10-20 12:11:40 +0300502 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dws->name, master);
Feng Tange24c7452009-12-14 14:20:22 -0800503 if (ret < 0) {
Andy Shevchenko5f0966e2015-10-14 23:12:17 +0300504 dev_err(dev, "can not get IRQ\n");
Feng Tange24c7452009-12-14 14:20:22 -0800505 goto err_free_master;
506 }
507
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300508 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
Stephen Warren24778be2013-05-21 20:36:35 -0600509 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Feng Tange24c7452009-12-14 14:20:22 -0800510 master->bus_num = dws->bus_num;
511 master->num_chipselect = dws->num_cs;
Feng Tange24c7452009-12-14 14:20:22 -0800512 master->setup = dw_spi_setup;
Axel Lina97c8832014-08-31 12:47:06 +0800513 master->cleanup = dw_spi_cleanup;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200514 master->set_cs = dw_spi_set_cs;
515 master->transfer_one = dw_spi_transfer_one;
516 master->handle_err = dw_spi_handle_err;
Axel Lin765ee702014-02-20 21:37:56 +0800517 master->max_speed_hz = dws->max_freq;
Thor Thayer9c6de472014-10-08 13:51:34 -0500518 master->dev.of_node = dev->of_node;
Feng Tange24c7452009-12-14 14:20:22 -0800519
Feng Tange24c7452009-12-14 14:20:22 -0800520 /* Basic HW init */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200521 spi_hw_init(dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -0800522
Feng Tang7063c0d2010-12-24 13:59:11 +0800523 if (dws->dma_ops && dws->dma_ops->dma_init) {
524 ret = dws->dma_ops->dma_init(dws);
525 if (ret) {
Andy Shevchenko3dbb3b92015-01-07 16:56:54 +0200526 dev_warn(dev, "DMA init failed\n");
Feng Tang7063c0d2010-12-24 13:59:11 +0800527 dws->dma_inited = 0;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200528 } else {
529 master->can_dma = dws->dma_ops->can_dma;
Feng Tang7063c0d2010-12-24 13:59:11 +0800530 }
531 }
532
Lukas Wunner35654d42020-05-25 14:25:01 +0200533 ret = spi_register_master(master);
Feng Tange24c7452009-12-14 14:20:22 -0800534 if (ret) {
535 dev_err(&master->dev, "problem registering spi master\n");
Baruch Siachec37e8e2014-01-31 12:07:44 +0200536 goto err_dma_exit;
Feng Tange24c7452009-12-14 14:20:22 -0800537 }
538
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300539 dw_spi_debugfs_init(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800540 return 0;
541
Baruch Siachec37e8e2014-01-31 12:07:44 +0200542err_dma_exit:
Feng Tang7063c0d2010-12-24 13:59:11 +0800543 if (dws->dma_ops && dws->dma_ops->dma_exit)
544 dws->dma_ops->dma_exit(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800545 spi_enable_chip(dws, 0);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300546 free_irq(dws->irq, master);
Feng Tange24c7452009-12-14 14:20:22 -0800547err_free_master:
548 spi_master_put(master);
Feng Tange24c7452009-12-14 14:20:22 -0800549 return ret;
550}
Feng Tang79290a22010-12-24 13:59:10 +0800551EXPORT_SYMBOL_GPL(dw_spi_add_host);
Feng Tange24c7452009-12-14 14:20:22 -0800552
Grant Likelyfd4a3192012-12-07 16:57:14 +0000553void dw_spi_remove_host(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800554{
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300555 dw_spi_debugfs_remove(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800556
Lukas Wunner35654d42020-05-25 14:25:01 +0200557 spi_unregister_master(dws->master);
558
Feng Tang7063c0d2010-12-24 13:59:11 +0800559 if (dws->dma_ops && dws->dma_ops->dma_exit)
560 dws->dma_ops->dma_exit(dws);
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300561
562 spi_shutdown_chip(dws);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300563
564 free_irq(dws->irq, dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800565}
Feng Tang79290a22010-12-24 13:59:10 +0800566EXPORT_SYMBOL_GPL(dw_spi_remove_host);
Feng Tange24c7452009-12-14 14:20:22 -0800567
568int dw_spi_suspend_host(struct dw_spi *dws)
569{
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300570 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800571
Baruch Siachec37e8e2014-01-31 12:07:44 +0200572 ret = spi_master_suspend(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800573 if (ret)
574 return ret;
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300575
576 spi_shutdown_chip(dws);
577 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800578}
Feng Tang79290a22010-12-24 13:59:10 +0800579EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
Feng Tange24c7452009-12-14 14:20:22 -0800580
581int dw_spi_resume_host(struct dw_spi *dws)
582{
583 int ret;
584
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200585 spi_hw_init(&dws->master->dev, dws);
Baruch Siachec37e8e2014-01-31 12:07:44 +0200586 ret = spi_master_resume(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800587 if (ret)
588 dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret);
589 return ret;
590}
Feng Tang79290a22010-12-24 13:59:10 +0800591EXPORT_SYMBOL_GPL(dw_spi_resume_host);
Feng Tange24c7452009-12-14 14:20:22 -0800592
593MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
594MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
595MODULE_LICENSE("GPL v2");