blob: 9a49e073e8b7347f101a6d2e97f96ebe5108158b [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
Andy Shevchenko207cda92015-03-25 20:26:26 +0200132 if (chip && chip->cs_control)
Linus Walleij6e0a32d2019-01-16 09:21:08 +0100133 chip->cs_control(enable);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200134
Linus Walleij6e0a32d2019-01-16 09:21:08 +0100135 if (enable)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200136 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
Talel Shenharf2d70472018-10-11 14:20:07 +0300137 else if (dws->cs_override)
138 dw_writel(dws, DW_SPI_SER, 0);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200139}
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200140EXPORT_SYMBOL_GPL(dw_spi_set_cs);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200141
Alek Du2ff271b2011-03-30 23:09:54 +0800142/* Return the max entries we can fill into tx fifo */
143static inline u32 tx_max(struct dw_spi *dws)
144{
145 u32 tx_left, tx_room, rxtx_gap;
146
147 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
Thor Thayerdd114442015-03-12 14:19:31 -0500148 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
Alek Du2ff271b2011-03-30 23:09:54 +0800149
150 /*
151 * Another concern is about the tx/rx mismatch, we
152 * though to use (dws->fifo_len - rxflr - txflr) as
153 * one maximum value for tx, but it doesn't cover the
154 * data which is out of tx/rx fifo and inside the
155 * shift registers. So a control from sw point of
156 * view is taken.
157 */
158 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
159 / dws->n_bytes;
160
161 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
162}
163
164/* Return the max entries we should read out of rx fifo */
165static inline u32 rx_max(struct dw_spi *dws)
166{
167 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
168
Thor Thayerdd114442015-03-12 14:19:31 -0500169 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
Alek Du2ff271b2011-03-30 23:09:54 +0800170}
171
Alek Du3b8a4dd2011-03-30 23:09:55 +0800172static void dw_writer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800173{
Alek Du2ff271b2011-03-30 23:09:54 +0800174 u32 max = tx_max(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800175 u16 txw = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800176
Alek Du2ff271b2011-03-30 23:09:54 +0800177 while (max--) {
178 /* Set the tx word if the transfer's original "tx" is not null */
179 if (dws->tx_end - dws->len) {
180 if (dws->n_bytes == 1)
181 txw = *(u8 *)(dws->tx);
182 else
183 txw = *(u16 *)(dws->tx);
184 }
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200185 dw_write_io_reg(dws, DW_SPI_DR, txw);
Alek Du2ff271b2011-03-30 23:09:54 +0800186 dws->tx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800187 }
Feng Tange24c7452009-12-14 14:20:22 -0800188}
189
Alek Du3b8a4dd2011-03-30 23:09:55 +0800190static void dw_reader(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800191{
Alek Du2ff271b2011-03-30 23:09:54 +0800192 u32 max = rx_max(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800193 u16 rxw;
Feng Tange24c7452009-12-14 14:20:22 -0800194
Alek Du2ff271b2011-03-30 23:09:54 +0800195 while (max--) {
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200196 rxw = dw_read_io_reg(dws, DW_SPI_DR);
Feng Tangde6efe02011-03-30 23:09:52 +0800197 /* Care rx only if the transfer's original "rx" is not null */
198 if (dws->rx_end - dws->len) {
199 if (dws->n_bytes == 1)
200 *(u8 *)(dws->rx) = rxw;
201 else
202 *(u16 *)(dws->rx) = rxw;
203 }
204 dws->rx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800205 }
Feng Tange24c7452009-12-14 14:20:22 -0800206}
207
Feng Tange24c7452009-12-14 14:20:22 -0800208static void int_error_stop(struct dw_spi *dws, const char *msg)
209{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200210 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800211
212 dev_err(&dws->master->dev, "%s\n", msg);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200213 dws->master->cur_msg->status = -EIO;
214 spi_finalize_current_transfer(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800215}
216
Feng Tange24c7452009-12-14 14:20:22 -0800217static irqreturn_t interrupt_transfer(struct dw_spi *dws)
218{
Thor Thayerdd114442015-03-12 14:19:31 -0500219 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Feng Tange24c7452009-12-14 14:20:22 -0800220
Feng Tange24c7452009-12-14 14:20:22 -0800221 /* Error handling */
222 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
Thor Thayerdd114442015-03-12 14:19:31 -0500223 dw_readl(dws, DW_SPI_ICR);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800224 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
Feng Tange24c7452009-12-14 14:20:22 -0800225 return IRQ_HANDLED;
226 }
227
Alek Du3b8a4dd2011-03-30 23:09:55 +0800228 dw_reader(dws);
229 if (dws->rx_end == dws->rx) {
230 spi_mask_intr(dws, SPI_INT_TXEI);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200231 spi_finalize_current_transfer(dws->master);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800232 return IRQ_HANDLED;
233 }
Feng Tang552e4502010-01-20 13:49:45 -0700234 if (irq_status & SPI_INT_TXEI) {
235 spi_mask_intr(dws, SPI_INT_TXEI);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800236 dw_writer(dws);
237 /* Enable TX irq always, it will be disabled when RX finished */
238 spi_umask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800239 }
Feng Tang552e4502010-01-20 13:49:45 -0700240
Feng Tange24c7452009-12-14 14:20:22 -0800241 return IRQ_HANDLED;
242}
243
244static irqreturn_t dw_spi_irq(int irq, void *dev_id)
245{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200246 struct spi_controller *master = dev_id;
247 struct dw_spi *dws = spi_controller_get_devdata(master);
Thor Thayerdd114442015-03-12 14:19:31 -0500248 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
Yong Wangcbcc0622010-09-07 15:27:27 +0800249
Yong Wangcbcc0622010-09-07 15:27:27 +0800250 if (!irq_status)
251 return IRQ_NONE;
Feng Tange24c7452009-12-14 14:20:22 -0800252
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200253 if (!master->cur_msg) {
Feng Tange24c7452009-12-14 14:20:22 -0800254 spi_mask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800255 return IRQ_HANDLED;
256 }
257
258 return dws->transfer_handler(dws);
259}
260
261/* Must be called inside pump_transfers() */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200262static int poll_transfer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800263{
Alek Du2ff271b2011-03-30 23:09:54 +0800264 do {
265 dw_writer(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800266 dw_reader(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800267 cpu_relax();
268 } while (dws->rx_end > dws->rx);
Feng Tange24c7452009-12-14 14:20:22 -0800269
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200270 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800271}
272
Jarkko Nikula721483e2018-02-01 17:17:29 +0200273static int dw_spi_transfer_one(struct spi_controller *master,
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200274 struct spi_device *spi, struct spi_transfer *transfer)
Feng Tange24c7452009-12-14 14:20:22 -0800275{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200276 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200277 struct chip_data *chip = spi_get_ctldata(spi);
Feng Tange24c7452009-12-14 14:20:22 -0800278 u8 imask = 0;
Andy Shevchenkoea113702015-02-24 13:32:11 +0200279 u16 txlevel = 0;
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300280 u32 cr0;
Andy Shevchenko9f145382015-03-09 16:48:46 +0200281 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800282
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200283 dws->dma_mapped = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800284
Feng Tange24c7452009-12-14 14:20:22 -0800285 dws->tx = (void *)transfer->tx_buf;
286 dws->tx_end = dws->tx + transfer->len;
287 dws->rx = transfer->rx_buf;
288 dws->rx_end = dws->rx + transfer->len;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200289 dws->len = transfer->len;
Feng Tange24c7452009-12-14 14:20:22 -0800290
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200291 spi_enable_chip(dws, 0);
292
Feng Tange24c7452009-12-14 14:20:22 -0800293 /* Handle per transfer options for bpw and speed */
Matthias Seidel13b10302016-09-04 02:04:49 +0200294 if (transfer->speed_hz != dws->current_freq) {
295 if (transfer->speed_hz != chip->speed_hz) {
296 /* clk_div doesn't support odd number */
Matthias Seidel3aef4632016-09-07 17:45:30 +0200297 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
Matthias Seidel13b10302016-09-04 02:04:49 +0200298 chip->speed_hz = transfer->speed_hz;
299 }
300 dws->current_freq = transfer->speed_hz;
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300301 spi_set_clk(dws, chip->clk_div);
Feng Tange24c7452009-12-14 14:20:22 -0800302 }
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200303
304 dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
305 dws->dma_width = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
306
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300307 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300308 cr0 = (transfer->bits_per_word - 1)
309 | (chip->type << SPI_FRF_OFFSET)
shaftargere1bc2042018-12-28 16:33:12 +0800310 | ((((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET) |
311 (((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET))
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300312 | (chip->tmode << SPI_TMOD_OFFSET);
Feng Tange24c7452009-12-14 14:20:22 -0800313
George Shore052dc7c2010-01-21 11:40:52 +0000314 /*
315 * Adjust transfer mode if necessary. Requires platform dependent
316 * chipselect mechanism.
317 */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200318 if (chip->cs_control) {
George Shore052dc7c2010-01-21 11:40:52 +0000319 if (dws->rx && dws->tx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800320 chip->tmode = SPI_TMOD_TR;
George Shore052dc7c2010-01-21 11:40:52 +0000321 else if (dws->rx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800322 chip->tmode = SPI_TMOD_RO;
George Shore052dc7c2010-01-21 11:40:52 +0000323 else
Feng Tange3e55ff2010-09-07 15:52:06 +0800324 chip->tmode = SPI_TMOD_TO;
George Shore052dc7c2010-01-21 11:40:52 +0000325
Feng Tange3e55ff2010-09-07 15:52:06 +0800326 cr0 &= ~SPI_TMOD_MASK;
George Shore052dc7c2010-01-21 11:40:52 +0000327 cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
328 }
329
Thor Thayerdd114442015-03-12 14:19:31 -0500330 dw_writel(dws, DW_SPI_CTRL0, cr0);
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200331
Feng Tange24c7452009-12-14 14:20:22 -0800332 /* Check if current transfer is a DMA transaction */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200333 if (master->can_dma && master->can_dma(master, spi, transfer))
334 dws->dma_mapped = master->cur_msg_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800335
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200336 /* For poll mode just disable all interrupts */
337 spi_mask_intr(dws, 0xff);
338
Feng Tang552e4502010-01-20 13:49:45 -0700339 /*
340 * Interrupt mode
341 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
342 */
Andy Shevchenko9f145382015-03-09 16:48:46 +0200343 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200344 ret = dws->dma_ops->dma_setup(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200345 if (ret < 0) {
346 spi_enable_chip(dws, 1);
347 return ret;
348 }
349 } else if (!chip->poll_mode) {
Andy Shevchenkoea113702015-02-24 13:32:11 +0200350 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
Thor Thayerdd114442015-03-12 14:19:31 -0500351 dw_writel(dws, DW_SPI_TXFLTR, txlevel);
Feng Tang552e4502010-01-20 13:49:45 -0700352
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200353 /* Set the interrupt mask */
Jingoo Hanfadcace2014-09-02 11:49:24 +0900354 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
355 SPI_INT_RXUI | SPI_INT_RXOI;
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200356 spi_umask_intr(dws, imask);
357
Feng Tange24c7452009-12-14 14:20:22 -0800358 dws->transfer_handler = interrupt_transfer;
359 }
360
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200361 spi_enable_chip(dws, 1);
Feng Tange24c7452009-12-14 14:20:22 -0800362
Andy Shevchenko9f145382015-03-09 16:48:46 +0200363 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200364 ret = dws->dma_ops->dma_transfer(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200365 if (ret < 0)
366 return ret;
367 }
Feng Tange24c7452009-12-14 14:20:22 -0800368
369 if (chip->poll_mode)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200370 return poll_transfer(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800371
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200372 return 1;
Feng Tange24c7452009-12-14 14:20:22 -0800373}
374
Jarkko Nikula721483e2018-02-01 17:17:29 +0200375static void dw_spi_handle_err(struct spi_controller *master,
Baruch Siachec37e8e2014-01-31 12:07:44 +0200376 struct spi_message *msg)
Feng Tange24c7452009-12-14 14:20:22 -0800377{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200378 struct dw_spi *dws = spi_controller_get_devdata(master);
Feng Tange24c7452009-12-14 14:20:22 -0800379
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200380 if (dws->dma_mapped)
381 dws->dma_ops->dma_stop(dws);
382
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200383 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800384}
385
386/* This may be called twice for each spi dev */
387static int dw_spi_setup(struct spi_device *spi)
388{
389 struct dw_spi_chip *chip_info = NULL;
390 struct chip_data *chip;
391
Feng Tange24c7452009-12-14 14:20:22 -0800392 /* Only alloc on first setup */
393 chip = spi_get_ctldata(spi);
394 if (!chip) {
Axel Lina97c8832014-08-31 12:47:06 +0800395 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Feng Tange24c7452009-12-14 14:20:22 -0800396 if (!chip)
397 return -ENOMEM;
Baruch Siach43f627a2013-12-30 20:30:46 +0200398 spi_set_ctldata(spi, chip);
Feng Tange24c7452009-12-14 14:20:22 -0800399 }
400
401 /*
402 * Protocol drivers may change the chip settings, so...
403 * if chip_info exists, use it
404 */
405 chip_info = spi->controller_data;
406
407 /* chip_info doesn't always exist */
408 if (chip_info) {
409 if (chip_info->cs_control)
410 chip->cs_control = chip_info->cs_control;
411
412 chip->poll_mode = chip_info->poll_mode;
413 chip->type = chip_info->type;
Feng Tange24c7452009-12-14 14:20:22 -0800414 }
415
Jisheng Zhang60968282015-12-23 19:05:39 +0800416 chip->tmode = SPI_TMOD_TR;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300417
Feng Tange24c7452009-12-14 14:20:22 -0800418 return 0;
419}
420
Axel Lina97c8832014-08-31 12:47:06 +0800421static void dw_spi_cleanup(struct spi_device *spi)
422{
423 struct chip_data *chip = spi_get_ctldata(spi);
424
425 kfree(chip);
426 spi_set_ctldata(spi, NULL);
427}
428
Feng Tange24c7452009-12-14 14:20:22 -0800429/* Restart the controller, disable all interrupts, clean rx fifo */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200430static void spi_hw_init(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800431{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200432 spi_reset_chip(dws);
Feng Tangc587b6f2010-01-21 10:41:10 +0800433
434 /*
435 * Try to detect the FIFO depth if not set by interface driver,
436 * the depth could be from 2 to 256 from HW spec
437 */
438 if (!dws->fifo_len) {
439 u32 fifo;
Jingoo Hanfadcace2014-09-02 11:49:24 +0900440
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200441 for (fifo = 1; fifo < 256; fifo++) {
Thor Thayerdd114442015-03-12 14:19:31 -0500442 dw_writel(dws, DW_SPI_TXFLTR, fifo);
443 if (fifo != dw_readl(dws, DW_SPI_TXFLTR))
Feng Tangc587b6f2010-01-21 10:41:10 +0800444 break;
445 }
Thor Thayerdd114442015-03-12 14:19:31 -0500446 dw_writel(dws, DW_SPI_TXFLTR, 0);
Feng Tangc587b6f2010-01-21 10:41:10 +0800447
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200448 dws->fifo_len = (fifo == 1) ? 0 : fifo;
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200449 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
Feng Tangc587b6f2010-01-21 10:41:10 +0800450 }
Talel Shenharf2d70472018-10-11 14:20:07 +0300451
452 /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
453 if (dws->cs_override)
454 dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
Feng Tange24c7452009-12-14 14:20:22 -0800455}
456
Baruch Siach04f421e2013-12-30 20:30:44 +0200457int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800458{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200459 struct spi_controller *master;
Feng Tange24c7452009-12-14 14:20:22 -0800460 int ret;
461
462 BUG_ON(dws == NULL);
463
Baruch Siach04f421e2013-12-30 20:30:44 +0200464 master = spi_alloc_master(dev, 0);
465 if (!master)
466 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -0800467
468 dws->master = master;
469 dws->type = SSI_MOTO_SPI;
Feng Tange24c7452009-12-14 14:20:22 -0800470 dws->dma_inited = 0;
Andy Shevchenkod7ef54c2015-10-27 17:48:16 +0200471 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
Feng Tange24c7452009-12-14 14:20:22 -0800472
Alexandre Belloni66b19d72018-07-17 16:23:10 +0200473 spi_controller_set_devdata(master, dws);
474
Phil Reide70002c802017-01-06 17:35:13 +0800475 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
476 master);
Feng Tange24c7452009-12-14 14:20:22 -0800477 if (ret < 0) {
Andy Shevchenko5f0966e2015-10-14 23:12:17 +0300478 dev_err(dev, "can not get IRQ\n");
Feng Tange24c7452009-12-14 14:20:22 -0800479 goto err_free_master;
480 }
481
Linus Walleij9400c412019-01-07 16:51:56 +0100482 master->use_gpio_descriptors = true;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300483 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200484 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Feng Tange24c7452009-12-14 14:20:22 -0800485 master->bus_num = dws->bus_num;
486 master->num_chipselect = dws->num_cs;
Feng Tange24c7452009-12-14 14:20:22 -0800487 master->setup = dw_spi_setup;
Axel Lina97c8832014-08-31 12:47:06 +0800488 master->cleanup = dw_spi_cleanup;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200489 master->set_cs = dw_spi_set_cs;
490 master->transfer_one = dw_spi_transfer_one;
491 master->handle_err = dw_spi_handle_err;
Axel Lin765ee702014-02-20 21:37:56 +0800492 master->max_speed_hz = dws->max_freq;
Thor Thayer9c6de472014-10-08 13:51:34 -0500493 master->dev.of_node = dev->of_node;
Jay Fang32215a62018-12-03 11:15:50 +0800494 master->dev.fwnode = dev->fwnode;
Thor Thayer80b444e2016-10-10 09:25:25 -0500495 master->flags = SPI_MASTER_GPIO_SS;
Feng Tange24c7452009-12-14 14:20:22 -0800496
Alexandre Belloni62dbbae2018-07-17 16:23:11 +0200497 if (dws->set_cs)
498 master->set_cs = dws->set_cs;
499
Feng Tange24c7452009-12-14 14:20:22 -0800500 /* Basic HW init */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200501 spi_hw_init(dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -0800502
Feng Tang7063c0d2010-12-24 13:59:11 +0800503 if (dws->dma_ops && dws->dma_ops->dma_init) {
504 ret = dws->dma_ops->dma_init(dws);
505 if (ret) {
Andy Shevchenko3dbb3b92015-01-07 16:56:54 +0200506 dev_warn(dev, "DMA init failed\n");
Feng Tang7063c0d2010-12-24 13:59:11 +0800507 dws->dma_inited = 0;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200508 } else {
509 master->can_dma = dws->dma_ops->can_dma;
Feng Tang7063c0d2010-12-24 13:59:11 +0800510 }
511 }
512
Jarkko Nikula721483e2018-02-01 17:17:29 +0200513 ret = devm_spi_register_controller(dev, master);
Feng Tange24c7452009-12-14 14:20:22 -0800514 if (ret) {
515 dev_err(&master->dev, "problem registering spi master\n");
Baruch Siachec37e8e2014-01-31 12:07:44 +0200516 goto err_dma_exit;
Feng Tange24c7452009-12-14 14:20:22 -0800517 }
518
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300519 dw_spi_debugfs_init(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800520 return 0;
521
Baruch Siachec37e8e2014-01-31 12:07:44 +0200522err_dma_exit:
Feng Tang7063c0d2010-12-24 13:59:11 +0800523 if (dws->dma_ops && dws->dma_ops->dma_exit)
524 dws->dma_ops->dma_exit(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800525 spi_enable_chip(dws, 0);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300526 free_irq(dws->irq, master);
Feng Tange24c7452009-12-14 14:20:22 -0800527err_free_master:
Jarkko Nikula721483e2018-02-01 17:17:29 +0200528 spi_controller_put(master);
Feng Tange24c7452009-12-14 14:20:22 -0800529 return ret;
530}
Feng Tang79290a22010-12-24 13:59:10 +0800531EXPORT_SYMBOL_GPL(dw_spi_add_host);
Feng Tange24c7452009-12-14 14:20:22 -0800532
Grant Likelyfd4a3192012-12-07 16:57:14 +0000533void dw_spi_remove_host(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800534{
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300535 dw_spi_debugfs_remove(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800536
Feng Tang7063c0d2010-12-24 13:59:11 +0800537 if (dws->dma_ops && dws->dma_ops->dma_exit)
538 dws->dma_ops->dma_exit(dws);
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300539
540 spi_shutdown_chip(dws);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300541
542 free_irq(dws->irq, dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800543}
Feng Tang79290a22010-12-24 13:59:10 +0800544EXPORT_SYMBOL_GPL(dw_spi_remove_host);
Feng Tange24c7452009-12-14 14:20:22 -0800545
546int dw_spi_suspend_host(struct dw_spi *dws)
547{
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300548 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800549
Jarkko Nikula721483e2018-02-01 17:17:29 +0200550 ret = spi_controller_suspend(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800551 if (ret)
552 return ret;
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300553
554 spi_shutdown_chip(dws);
555 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800556}
Feng Tang79290a22010-12-24 13:59:10 +0800557EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
Feng Tange24c7452009-12-14 14:20:22 -0800558
559int dw_spi_resume_host(struct dw_spi *dws)
560{
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200561 spi_hw_init(&dws->master->dev, dws);
Geert Uytterhoeven7c5d8a22018-09-05 10:51:57 +0200562 return spi_controller_resume(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800563}
Feng Tang79290a22010-12-24 13:59:10 +0800564EXPORT_SYMBOL_GPL(dw_spi_resume_host);
Feng Tange24c7452009-12-14 14:20:22 -0800565
566MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
567MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
568MODULE_LICENSE("GPL v2");