blob: 31e3f866d11a78be9786803548b5f936bfb1e057 [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
27 u8 poll_mode; /* 1 means use poll mode */
28
Feng Tange24c7452009-12-14 14:20:22 -080029 u16 clk_div; /* baud rate divider */
30 u32 speed_hz; /* baud rate */
Feng Tange24c7452009-12-14 14:20:22 -080031 void (*cs_control)(u32 command);
32};
33
34#ifdef CONFIG_DEBUG_FS
Feng Tange24c7452009-12-14 14:20:22 -080035#define SPI_REGS_BUFSIZE 1024
Andy Shevchenko53288fe2014-09-12 15:11:56 +030036static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
37 size_t count, loff_t *ppos)
Feng Tange24c7452009-12-14 14:20:22 -080038{
Andy Shevchenko53288fe2014-09-12 15:11:56 +030039 struct dw_spi *dws = file->private_data;
Feng Tange24c7452009-12-14 14:20:22 -080040 char *buf;
41 u32 len = 0;
42 ssize_t ret;
43
Feng Tange24c7452009-12-14 14:20:22 -080044 buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
45 if (!buf)
46 return 0;
47
Silvio Cesared1d6bd72019-01-12 16:28:44 +010048 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Andy Shevchenko53288fe2014-09-12 15:11:56 +030049 "%s registers:\n", dev_name(&dws->master->dev));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010050 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Feng Tange24c7452009-12-14 14:20:22 -080051 "=================================\n");
Silvio Cesared1d6bd72019-01-12 16:28:44 +010052 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070053 "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010054 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070055 "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010056 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070057 "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010058 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070059 "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010060 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070061 "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010062 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070063 "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010064 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070065 "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010066 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070067 "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010068 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070069 "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010070 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070071 "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010072 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070073 "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010074 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070075 "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010076 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070077 "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010078 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070079 "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010080 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070081 "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
Silvio Cesared1d6bd72019-01-12 16:28:44 +010082 len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
Feng Tange24c7452009-12-14 14:20:22 -080083 "=================================\n");
84
Andy Shevchenko53288fe2014-09-12 15:11:56 +030085 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
Feng Tange24c7452009-12-14 14:20:22 -080086 kfree(buf);
87 return ret;
88}
89
Andy Shevchenko53288fe2014-09-12 15:11:56 +030090static const struct file_operations dw_spi_regs_ops = {
Feng Tange24c7452009-12-14 14:20:22 -080091 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -070092 .open = simple_open,
Andy Shevchenko53288fe2014-09-12 15:11:56 +030093 .read = dw_spi_show_regs,
Arnd Bergmann6038f372010-08-15 18:52:59 +020094 .llseek = default_llseek,
Feng Tange24c7452009-12-14 14:20:22 -080095};
96
Andy Shevchenko53288fe2014-09-12 15:11:56 +030097static int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -080098{
Phil Reide70002c802017-01-06 17:35:13 +080099 char name[32];
Phil Reid13288bd2016-12-22 17:18:12 +0800100
Phil Reide70002c802017-01-06 17:35:13 +0800101 snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
Phil Reid13288bd2016-12-22 17:18:12 +0800102 dws->debugfs = debugfs_create_dir(name, NULL);
Feng Tange24c7452009-12-14 14:20:22 -0800103 if (!dws->debugfs)
104 return -ENOMEM;
105
106 debugfs_create_file("registers", S_IFREG | S_IRUGO,
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300107 dws->debugfs, (void *)dws, &dw_spi_regs_ops);
Feng Tange24c7452009-12-14 14:20:22 -0800108 return 0;
109}
110
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300111static void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800112{
Jingoo Hanfadcace2014-09-02 11:49:24 +0900113 debugfs_remove_recursive(dws->debugfs);
Feng Tange24c7452009-12-14 14:20:22 -0800114}
115
116#else
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300117static inline int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800118{
George Shore20a588f2010-01-21 11:40:49 +0000119 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800120}
121
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300122static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800123{
124}
125#endif /* CONFIG_DEBUG_FS */
126
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200127void dw_spi_set_cs(struct spi_device *spi, bool enable)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200128{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200129 struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200130 struct chip_data *chip = spi_get_ctldata(spi);
131
Charles Keepaxada9e3f2019-11-27 15:39:36 +0000132 /* Chip select logic is inverted from spi_set_cs() */
Andy Shevchenko207cda92015-03-25 20:26:26 +0200133 if (chip && chip->cs_control)
Charles Keepaxada9e3f2019-11-27 15:39:36 +0000134 chip->cs_control(!enable);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200135
Charles Keepaxada9e3f2019-11-27 15:39:36 +0000136 if (!enable)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200137 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
Talel Shenharf2d70472018-10-11 14:20:07 +0300138 else if (dws->cs_override)
139 dw_writel(dws, DW_SPI_SER, 0);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200140}
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200141EXPORT_SYMBOL_GPL(dw_spi_set_cs);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200142
Alek Du2ff271b2011-03-30 23:09:54 +0800143/* Return the max entries we can fill into tx fifo */
144static inline u32 tx_max(struct dw_spi *dws)
145{
146 u32 tx_left, tx_room, rxtx_gap;
147
148 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
Thor Thayerdd114442015-03-12 14:19:31 -0500149 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
Alek Du2ff271b2011-03-30 23:09:54 +0800150
151 /*
152 * Another concern is about the tx/rx mismatch, we
153 * though to use (dws->fifo_len - rxflr - txflr) as
154 * one maximum value for tx, but it doesn't cover the
155 * data which is out of tx/rx fifo and inside the
156 * shift registers. So a control from sw point of
157 * view is taken.
158 */
159 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
160 / dws->n_bytes;
161
162 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
163}
164
165/* Return the max entries we should read out of rx fifo */
166static inline u32 rx_max(struct dw_spi *dws)
167{
168 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
169
Thor Thayerdd114442015-03-12 14:19:31 -0500170 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
Alek Du2ff271b2011-03-30 23:09:54 +0800171}
172
Alek Du3b8a4dd2011-03-30 23:09:55 +0800173static void dw_writer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800174{
wuxu.wu19b61392020-01-01 11:39:41 +0800175 u32 max;
Feng Tangde6efe02011-03-30 23:09:52 +0800176 u16 txw = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800177
wuxu.wu19b61392020-01-01 11:39:41 +0800178 spin_lock(&dws->buf_lock);
179 max = tx_max(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800180 while (max--) {
181 /* Set the tx word if the transfer's original "tx" is not null */
182 if (dws->tx_end - dws->len) {
183 if (dws->n_bytes == 1)
184 txw = *(u8 *)(dws->tx);
185 else
186 txw = *(u16 *)(dws->tx);
187 }
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200188 dw_write_io_reg(dws, DW_SPI_DR, txw);
Alek Du2ff271b2011-03-30 23:09:54 +0800189 dws->tx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800190 }
wuxu.wu19b61392020-01-01 11:39:41 +0800191 spin_unlock(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800192}
193
Alek Du3b8a4dd2011-03-30 23:09:55 +0800194static void dw_reader(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800195{
wuxu.wu19b61392020-01-01 11:39:41 +0800196 u32 max;
Feng Tangde6efe02011-03-30 23:09:52 +0800197 u16 rxw;
Feng Tange24c7452009-12-14 14:20:22 -0800198
wuxu.wu19b61392020-01-01 11:39:41 +0800199 spin_lock(&dws->buf_lock);
200 max = rx_max(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800201 while (max--) {
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200202 rxw = dw_read_io_reg(dws, DW_SPI_DR);
Feng Tangde6efe02011-03-30 23:09:52 +0800203 /* Care rx only if the transfer's original "rx" is not null */
204 if (dws->rx_end - dws->len) {
205 if (dws->n_bytes == 1)
206 *(u8 *)(dws->rx) = rxw;
207 else
208 *(u16 *)(dws->rx) = rxw;
209 }
210 dws->rx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800211 }
wuxu.wu19b61392020-01-01 11:39:41 +0800212 spin_unlock(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800213}
214
Feng Tange24c7452009-12-14 14:20:22 -0800215static void int_error_stop(struct dw_spi *dws, const char *msg)
216{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200217 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800218
219 dev_err(&dws->master->dev, "%s\n", msg);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200220 dws->master->cur_msg->status = -EIO;
221 spi_finalize_current_transfer(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800222}
223
Feng Tange24c7452009-12-14 14:20:22 -0800224static irqreturn_t interrupt_transfer(struct dw_spi *dws)
225{
Thor Thayerdd114442015-03-12 14:19:31 -0500226 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Feng Tange24c7452009-12-14 14:20:22 -0800227
Feng Tange24c7452009-12-14 14:20:22 -0800228 /* Error handling */
229 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
Thor Thayerdd114442015-03-12 14:19:31 -0500230 dw_readl(dws, DW_SPI_ICR);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800231 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
Feng Tange24c7452009-12-14 14:20:22 -0800232 return IRQ_HANDLED;
233 }
234
Alek Du3b8a4dd2011-03-30 23:09:55 +0800235 dw_reader(dws);
236 if (dws->rx_end == dws->rx) {
237 spi_mask_intr(dws, SPI_INT_TXEI);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200238 spi_finalize_current_transfer(dws->master);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800239 return IRQ_HANDLED;
240 }
Feng Tang552e4502010-01-20 13:49:45 -0700241 if (irq_status & SPI_INT_TXEI) {
242 spi_mask_intr(dws, SPI_INT_TXEI);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800243 dw_writer(dws);
244 /* Enable TX irq always, it will be disabled when RX finished */
245 spi_umask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800246 }
Feng Tang552e4502010-01-20 13:49:45 -0700247
Feng Tange24c7452009-12-14 14:20:22 -0800248 return IRQ_HANDLED;
249}
250
251static irqreturn_t dw_spi_irq(int irq, void *dev_id)
252{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200253 struct spi_controller *master = dev_id;
254 struct dw_spi *dws = spi_controller_get_devdata(master);
Thor Thayerdd114442015-03-12 14:19:31 -0500255 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
Yong Wangcbcc0622010-09-07 15:27:27 +0800256
Yong Wangcbcc0622010-09-07 15:27:27 +0800257 if (!irq_status)
258 return IRQ_NONE;
Feng Tange24c7452009-12-14 14:20:22 -0800259
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200260 if (!master->cur_msg) {
Feng Tange24c7452009-12-14 14:20:22 -0800261 spi_mask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800262 return IRQ_HANDLED;
263 }
264
265 return dws->transfer_handler(dws);
266}
267
268/* Must be called inside pump_transfers() */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200269static int poll_transfer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800270{
Alek Du2ff271b2011-03-30 23:09:54 +0800271 do {
272 dw_writer(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800273 dw_reader(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800274 cpu_relax();
275 } while (dws->rx_end > dws->rx);
Feng Tange24c7452009-12-14 14:20:22 -0800276
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200277 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800278}
279
Jarkko Nikula721483e2018-02-01 17:17:29 +0200280static int dw_spi_transfer_one(struct spi_controller *master,
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200281 struct spi_device *spi, struct spi_transfer *transfer)
Feng Tange24c7452009-12-14 14:20:22 -0800282{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200283 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200284 struct chip_data *chip = spi_get_ctldata(spi);
wuxu.wu19b61392020-01-01 11:39:41 +0800285 unsigned long flags;
Feng Tange24c7452009-12-14 14:20:22 -0800286 u8 imask = 0;
Andy Shevchenkoea113702015-02-24 13:32:11 +0200287 u16 txlevel = 0;
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300288 u32 cr0;
Andy Shevchenko9f145382015-03-09 16:48:46 +0200289 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800290
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200291 dws->dma_mapped = 0;
wuxu.wu19b61392020-01-01 11:39:41 +0800292 spin_lock_irqsave(&dws->buf_lock, flags);
Feng Tange24c7452009-12-14 14:20:22 -0800293 dws->tx = (void *)transfer->tx_buf;
294 dws->tx_end = dws->tx + transfer->len;
295 dws->rx = transfer->rx_buf;
296 dws->rx_end = dws->rx + transfer->len;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200297 dws->len = transfer->len;
wuxu.wu19b61392020-01-01 11:39:41 +0800298 spin_unlock_irqrestore(&dws->buf_lock, flags);
Feng Tange24c7452009-12-14 14:20:22 -0800299
Xinwei Kongbfda0442020-01-03 10:52:10 +0800300 /* Ensure dw->rx and dw->rx_end are visible */
301 smp_mb();
302
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200303 spi_enable_chip(dws, 0);
304
Feng Tange24c7452009-12-14 14:20:22 -0800305 /* Handle per transfer options for bpw and speed */
Matthias Seidel13b10302016-09-04 02:04:49 +0200306 if (transfer->speed_hz != dws->current_freq) {
307 if (transfer->speed_hz != chip->speed_hz) {
308 /* clk_div doesn't support odd number */
Matthias Seidel3aef4632016-09-07 17:45:30 +0200309 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
Matthias Seidel13b10302016-09-04 02:04:49 +0200310 chip->speed_hz = transfer->speed_hz;
311 }
312 dws->current_freq = transfer->speed_hz;
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300313 spi_set_clk(dws, chip->clk_div);
Feng Tange24c7452009-12-14 14:20:22 -0800314 }
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200315
316 dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
317 dws->dma_width = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
318
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300319 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300320 cr0 = (transfer->bits_per_word - 1)
321 | (chip->type << SPI_FRF_OFFSET)
shaftargere1bc2042018-12-28 16:33:12 +0800322 | ((((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET) |
Thor Thayer1403cfa2019-11-05 14:22:10 -0600323 (((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET) |
324 (((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET))
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300325 | (chip->tmode << SPI_TMOD_OFFSET);
Feng Tange24c7452009-12-14 14:20:22 -0800326
George Shore052dc7c2010-01-21 11:40:52 +0000327 /*
328 * Adjust transfer mode if necessary. Requires platform dependent
329 * chipselect mechanism.
330 */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200331 if (chip->cs_control) {
George Shore052dc7c2010-01-21 11:40:52 +0000332 if (dws->rx && dws->tx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800333 chip->tmode = SPI_TMOD_TR;
George Shore052dc7c2010-01-21 11:40:52 +0000334 else if (dws->rx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800335 chip->tmode = SPI_TMOD_RO;
George Shore052dc7c2010-01-21 11:40:52 +0000336 else
Feng Tange3e55ff2010-09-07 15:52:06 +0800337 chip->tmode = SPI_TMOD_TO;
George Shore052dc7c2010-01-21 11:40:52 +0000338
Feng Tange3e55ff2010-09-07 15:52:06 +0800339 cr0 &= ~SPI_TMOD_MASK;
George Shore052dc7c2010-01-21 11:40:52 +0000340 cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
341 }
342
Thor Thayerdd114442015-03-12 14:19:31 -0500343 dw_writel(dws, DW_SPI_CTRL0, cr0);
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200344
Feng Tange24c7452009-12-14 14:20:22 -0800345 /* Check if current transfer is a DMA transaction */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200346 if (master->can_dma && master->can_dma(master, spi, transfer))
347 dws->dma_mapped = master->cur_msg_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800348
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200349 /* For poll mode just disable all interrupts */
350 spi_mask_intr(dws, 0xff);
351
Feng Tang552e4502010-01-20 13:49:45 -0700352 /*
353 * Interrupt mode
354 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
355 */
Andy Shevchenko9f145382015-03-09 16:48:46 +0200356 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200357 ret = dws->dma_ops->dma_setup(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200358 if (ret < 0) {
359 spi_enable_chip(dws, 1);
360 return ret;
361 }
362 } else if (!chip->poll_mode) {
Andy Shevchenkoea113702015-02-24 13:32:11 +0200363 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
Thor Thayerdd114442015-03-12 14:19:31 -0500364 dw_writel(dws, DW_SPI_TXFLTR, txlevel);
Feng Tang552e4502010-01-20 13:49:45 -0700365
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200366 /* Set the interrupt mask */
Jingoo Hanfadcace2014-09-02 11:49:24 +0900367 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
368 SPI_INT_RXUI | SPI_INT_RXOI;
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200369 spi_umask_intr(dws, imask);
370
Feng Tange24c7452009-12-14 14:20:22 -0800371 dws->transfer_handler = interrupt_transfer;
372 }
373
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200374 spi_enable_chip(dws, 1);
Feng Tange24c7452009-12-14 14:20:22 -0800375
Andy Shevchenko9f145382015-03-09 16:48:46 +0200376 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200377 ret = dws->dma_ops->dma_transfer(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200378 if (ret < 0)
379 return ret;
380 }
Feng Tange24c7452009-12-14 14:20:22 -0800381
382 if (chip->poll_mode)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200383 return poll_transfer(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800384
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200385 return 1;
Feng Tange24c7452009-12-14 14:20:22 -0800386}
387
Jarkko Nikula721483e2018-02-01 17:17:29 +0200388static void dw_spi_handle_err(struct spi_controller *master,
Baruch Siachec37e8e2014-01-31 12:07:44 +0200389 struct spi_message *msg)
Feng Tange24c7452009-12-14 14:20:22 -0800390{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200391 struct dw_spi *dws = spi_controller_get_devdata(master);
Feng Tange24c7452009-12-14 14:20:22 -0800392
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200393 if (dws->dma_mapped)
394 dws->dma_ops->dma_stop(dws);
395
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200396 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800397}
398
399/* This may be called twice for each spi dev */
400static int dw_spi_setup(struct spi_device *spi)
401{
402 struct dw_spi_chip *chip_info = NULL;
403 struct chip_data *chip;
404
Feng Tange24c7452009-12-14 14:20:22 -0800405 /* Only alloc on first setup */
406 chip = spi_get_ctldata(spi);
407 if (!chip) {
Axel Lina97c8832014-08-31 12:47:06 +0800408 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Feng Tange24c7452009-12-14 14:20:22 -0800409 if (!chip)
410 return -ENOMEM;
Baruch Siach43f627a2013-12-30 20:30:46 +0200411 spi_set_ctldata(spi, chip);
Feng Tange24c7452009-12-14 14:20:22 -0800412 }
413
414 /*
415 * Protocol drivers may change the chip settings, so...
416 * if chip_info exists, use it
417 */
418 chip_info = spi->controller_data;
419
420 /* chip_info doesn't always exist */
421 if (chip_info) {
422 if (chip_info->cs_control)
423 chip->cs_control = chip_info->cs_control;
424
425 chip->poll_mode = chip_info->poll_mode;
426 chip->type = chip_info->type;
Feng Tange24c7452009-12-14 14:20:22 -0800427 }
428
Jisheng Zhang60968282015-12-23 19:05:39 +0800429 chip->tmode = SPI_TMOD_TR;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300430
Feng Tange24c7452009-12-14 14:20:22 -0800431 return 0;
432}
433
Axel Lina97c8832014-08-31 12:47:06 +0800434static void dw_spi_cleanup(struct spi_device *spi)
435{
436 struct chip_data *chip = spi_get_ctldata(spi);
437
438 kfree(chip);
439 spi_set_ctldata(spi, NULL);
440}
441
Feng Tange24c7452009-12-14 14:20:22 -0800442/* Restart the controller, disable all interrupts, clean rx fifo */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200443static void spi_hw_init(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800444{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200445 spi_reset_chip(dws);
Feng Tangc587b6f2010-01-21 10:41:10 +0800446
447 /*
448 * Try to detect the FIFO depth if not set by interface driver,
449 * the depth could be from 2 to 256 from HW spec
450 */
451 if (!dws->fifo_len) {
452 u32 fifo;
Jingoo Hanfadcace2014-09-02 11:49:24 +0900453
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200454 for (fifo = 1; fifo < 256; fifo++) {
Thor Thayerdd114442015-03-12 14:19:31 -0500455 dw_writel(dws, DW_SPI_TXFLTR, fifo);
456 if (fifo != dw_readl(dws, DW_SPI_TXFLTR))
Feng Tangc587b6f2010-01-21 10:41:10 +0800457 break;
458 }
Thor Thayerdd114442015-03-12 14:19:31 -0500459 dw_writel(dws, DW_SPI_TXFLTR, 0);
Feng Tangc587b6f2010-01-21 10:41:10 +0800460
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200461 dws->fifo_len = (fifo == 1) ? 0 : fifo;
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200462 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
Feng Tangc587b6f2010-01-21 10:41:10 +0800463 }
Talel Shenharf2d70472018-10-11 14:20:07 +0300464
465 /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
466 if (dws->cs_override)
467 dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
Feng Tange24c7452009-12-14 14:20:22 -0800468}
469
Baruch Siach04f421e2013-12-30 20:30:44 +0200470int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800471{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200472 struct spi_controller *master;
Feng Tange24c7452009-12-14 14:20:22 -0800473 int ret;
474
Aditya Pakki169f9ac2019-12-05 17:14:21 -0600475 if (!dws)
476 return -EINVAL;
Feng Tange24c7452009-12-14 14:20:22 -0800477
Baruch Siach04f421e2013-12-30 20:30:44 +0200478 master = spi_alloc_master(dev, 0);
479 if (!master)
480 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -0800481
482 dws->master = master;
483 dws->type = SSI_MOTO_SPI;
Feng Tange24c7452009-12-14 14:20:22 -0800484 dws->dma_inited = 0;
Andy Shevchenkod7ef54c2015-10-27 17:48:16 +0200485 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
wuxu.wu19b61392020-01-01 11:39:41 +0800486 spin_lock_init(&dws->buf_lock);
Feng Tange24c7452009-12-14 14:20:22 -0800487
Alexandre Belloni66b19d72018-07-17 16:23:10 +0200488 spi_controller_set_devdata(master, dws);
489
Phil Reide70002c802017-01-06 17:35:13 +0800490 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
491 master);
Feng Tange24c7452009-12-14 14:20:22 -0800492 if (ret < 0) {
Andy Shevchenko5f0966e2015-10-14 23:12:17 +0300493 dev_err(dev, "can not get IRQ\n");
Feng Tange24c7452009-12-14 14:20:22 -0800494 goto err_free_master;
495 }
496
Linus Walleij9400c412019-01-07 16:51:56 +0100497 master->use_gpio_descriptors = true;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300498 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200499 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Feng Tange24c7452009-12-14 14:20:22 -0800500 master->bus_num = dws->bus_num;
501 master->num_chipselect = dws->num_cs;
Feng Tange24c7452009-12-14 14:20:22 -0800502 master->setup = dw_spi_setup;
Axel Lina97c8832014-08-31 12:47:06 +0800503 master->cleanup = dw_spi_cleanup;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200504 master->set_cs = dw_spi_set_cs;
505 master->transfer_one = dw_spi_transfer_one;
506 master->handle_err = dw_spi_handle_err;
Axel Lin765ee702014-02-20 21:37:56 +0800507 master->max_speed_hz = dws->max_freq;
Thor Thayer9c6de472014-10-08 13:51:34 -0500508 master->dev.of_node = dev->of_node;
Jay Fang32215a62018-12-03 11:15:50 +0800509 master->dev.fwnode = dev->fwnode;
Thor Thayer80b444e2016-10-10 09:25:25 -0500510 master->flags = SPI_MASTER_GPIO_SS;
Phil Edworthy1e695982019-09-18 09:04:35 +0100511 master->auto_runtime_pm = true;
Feng Tange24c7452009-12-14 14:20:22 -0800512
Alexandre Belloni62dbbae2018-07-17 16:23:11 +0200513 if (dws->set_cs)
514 master->set_cs = dws->set_cs;
515
Feng Tange24c7452009-12-14 14:20:22 -0800516 /* Basic HW init */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200517 spi_hw_init(dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -0800518
Feng Tang7063c0d2010-12-24 13:59:11 +0800519 if (dws->dma_ops && dws->dma_ops->dma_init) {
520 ret = dws->dma_ops->dma_init(dws);
521 if (ret) {
Andy Shevchenko3dbb3b92015-01-07 16:56:54 +0200522 dev_warn(dev, "DMA init failed\n");
Feng Tang7063c0d2010-12-24 13:59:11 +0800523 dws->dma_inited = 0;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200524 } else {
525 master->can_dma = dws->dma_ops->can_dma;
Feng Tang7063c0d2010-12-24 13:59:11 +0800526 }
527 }
528
Jarkko Nikula721483e2018-02-01 17:17:29 +0200529 ret = devm_spi_register_controller(dev, master);
Feng Tange24c7452009-12-14 14:20:22 -0800530 if (ret) {
531 dev_err(&master->dev, "problem registering spi master\n");
Baruch Siachec37e8e2014-01-31 12:07:44 +0200532 goto err_dma_exit;
Feng Tange24c7452009-12-14 14:20:22 -0800533 }
534
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300535 dw_spi_debugfs_init(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800536 return 0;
537
Baruch Siachec37e8e2014-01-31 12:07:44 +0200538err_dma_exit:
Feng Tang7063c0d2010-12-24 13:59:11 +0800539 if (dws->dma_ops && dws->dma_ops->dma_exit)
540 dws->dma_ops->dma_exit(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800541 spi_enable_chip(dws, 0);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300542 free_irq(dws->irq, master);
Feng Tange24c7452009-12-14 14:20:22 -0800543err_free_master:
Jarkko Nikula721483e2018-02-01 17:17:29 +0200544 spi_controller_put(master);
Feng Tange24c7452009-12-14 14:20:22 -0800545 return ret;
546}
Feng Tang79290a22010-12-24 13:59:10 +0800547EXPORT_SYMBOL_GPL(dw_spi_add_host);
Feng Tange24c7452009-12-14 14:20:22 -0800548
Grant Likelyfd4a3192012-12-07 16:57:14 +0000549void dw_spi_remove_host(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800550{
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300551 dw_spi_debugfs_remove(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800552
Feng Tang7063c0d2010-12-24 13:59:11 +0800553 if (dws->dma_ops && dws->dma_ops->dma_exit)
554 dws->dma_ops->dma_exit(dws);
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300555
556 spi_shutdown_chip(dws);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300557
558 free_irq(dws->irq, dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800559}
Feng Tang79290a22010-12-24 13:59:10 +0800560EXPORT_SYMBOL_GPL(dw_spi_remove_host);
Feng Tange24c7452009-12-14 14:20:22 -0800561
562int dw_spi_suspend_host(struct dw_spi *dws)
563{
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300564 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800565
Jarkko Nikula721483e2018-02-01 17:17:29 +0200566 ret = spi_controller_suspend(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800567 if (ret)
568 return ret;
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300569
570 spi_shutdown_chip(dws);
571 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800572}
Feng Tang79290a22010-12-24 13:59:10 +0800573EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
Feng Tange24c7452009-12-14 14:20:22 -0800574
575int dw_spi_resume_host(struct dw_spi *dws)
576{
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200577 spi_hw_init(&dws->master->dev, dws);
Geert Uytterhoeven7c5d8a22018-09-05 10:51:57 +0200578 return spi_controller_resume(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800579}
Feng Tang79290a22010-12-24 13:59:10 +0800580EXPORT_SYMBOL_GPL(dw_spi_resume_host);
Feng Tange24c7452009-12-14 14:20:22 -0800581
582MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
583MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
584MODULE_LICENSE("GPL v2");