blob: b705f2bdb8b9493cd39d416d512084f4b8d33849 [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 tmode; /* TR/TO/RO/EEPROM */
34 u8 type; /* SPI/SSP/MicroWire */
35
36 u8 poll_mode; /* 1 means use poll mode */
37
Feng Tange24c7452009-12-14 14:20:22 -080038 u16 clk_div; /* baud rate divider */
39 u32 speed_hz; /* baud rate */
Feng Tange24c7452009-12-14 14:20:22 -080040 void (*cs_control)(u32 command);
41};
42
43#ifdef CONFIG_DEBUG_FS
Feng Tange24c7452009-12-14 14:20:22 -080044#define SPI_REGS_BUFSIZE 1024
Andy Shevchenko53288fe2014-09-12 15:11:56 +030045static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
46 size_t count, loff_t *ppos)
Feng Tange24c7452009-12-14 14:20:22 -080047{
Andy Shevchenko53288fe2014-09-12 15:11:56 +030048 struct dw_spi *dws = file->private_data;
Feng Tange24c7452009-12-14 14:20:22 -080049 char *buf;
50 u32 len = 0;
51 ssize_t ret;
52
Feng Tange24c7452009-12-14 14:20:22 -080053 buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
54 if (!buf)
55 return 0;
56
57 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
Andy Shevchenko53288fe2014-09-12 15:11:56 +030058 "%s registers:\n", dev_name(&dws->master->dev));
Feng Tange24c7452009-12-14 14:20:22 -080059 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
60 "=================================\n");
61 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070062 "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
Feng Tange24c7452009-12-14 14:20:22 -080063 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070064 "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
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 "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
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 "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
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 "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
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 "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
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 "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
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 "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
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 "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
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 "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
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 "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
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 "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
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 "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
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 "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
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 "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
Feng Tange24c7452009-12-14 14:20:22 -080091 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
92 "=================================\n");
93
Andy Shevchenko53288fe2014-09-12 15:11:56 +030094 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
Feng Tange24c7452009-12-14 14:20:22 -080095 kfree(buf);
96 return ret;
97}
98
Andy Shevchenko53288fe2014-09-12 15:11:56 +030099static const struct file_operations dw_spi_regs_ops = {
Feng Tange24c7452009-12-14 14:20:22 -0800100 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700101 .open = simple_open,
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300102 .read = dw_spi_show_regs,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200103 .llseek = default_llseek,
Feng Tange24c7452009-12-14 14:20:22 -0800104};
105
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300106static int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800107{
Phil Reide70002c802017-01-06 17:35:13 +0800108 char name[32];
Phil Reid13288bd2016-12-22 17:18:12 +0800109
Phil Reide70002c802017-01-06 17:35:13 +0800110 snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
Phil Reid13288bd2016-12-22 17:18:12 +0800111 dws->debugfs = debugfs_create_dir(name, NULL);
Feng Tange24c7452009-12-14 14:20:22 -0800112 if (!dws->debugfs)
113 return -ENOMEM;
114
115 debugfs_create_file("registers", S_IFREG | S_IRUGO,
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300116 dws->debugfs, (void *)dws, &dw_spi_regs_ops);
Feng Tange24c7452009-12-14 14:20:22 -0800117 return 0;
118}
119
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300120static void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800121{
Jingoo Hanfadcace2014-09-02 11:49:24 +0900122 debugfs_remove_recursive(dws->debugfs);
Feng Tange24c7452009-12-14 14:20:22 -0800123}
124
125#else
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300126static inline int dw_spi_debugfs_init(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800127{
George Shore20a588f2010-01-21 11:40:49 +0000128 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800129}
130
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300131static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800132{
133}
134#endif /* CONFIG_DEBUG_FS */
135
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200136void dw_spi_set_cs(struct spi_device *spi, bool enable)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200137{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200138 struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200139 struct chip_data *chip = spi_get_ctldata(spi);
140
141 /* Chip select logic is inverted from spi_set_cs() */
Andy Shevchenko207cda92015-03-25 20:26:26 +0200142 if (chip && chip->cs_control)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200143 chip->cs_control(!enable);
144
145 if (!enable)
146 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
Talel Shenharf2d70472018-10-11 14:20:07 +0300147 else if (dws->cs_override)
148 dw_writel(dws, DW_SPI_SER, 0);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200149}
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200150EXPORT_SYMBOL_GPL(dw_spi_set_cs);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200151
Alek Du2ff271b2011-03-30 23:09:54 +0800152/* Return the max entries we can fill into tx fifo */
153static inline u32 tx_max(struct dw_spi *dws)
154{
155 u32 tx_left, tx_room, rxtx_gap;
156
157 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
Thor Thayerdd114442015-03-12 14:19:31 -0500158 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
Alek Du2ff271b2011-03-30 23:09:54 +0800159
160 /*
161 * Another concern is about the tx/rx mismatch, we
162 * though to use (dws->fifo_len - rxflr - txflr) as
163 * one maximum value for tx, but it doesn't cover the
164 * data which is out of tx/rx fifo and inside the
165 * shift registers. So a control from sw point of
166 * view is taken.
167 */
168 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
169 / dws->n_bytes;
170
171 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
172}
173
174/* Return the max entries we should read out of rx fifo */
175static inline u32 rx_max(struct dw_spi *dws)
176{
177 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
178
Thor Thayerdd114442015-03-12 14:19:31 -0500179 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
Alek Du2ff271b2011-03-30 23:09:54 +0800180}
181
Alek Du3b8a4dd2011-03-30 23:09:55 +0800182static void dw_writer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800183{
Alek Du2ff271b2011-03-30 23:09:54 +0800184 u32 max = tx_max(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800185 u16 txw = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800186
Alek Du2ff271b2011-03-30 23:09:54 +0800187 while (max--) {
188 /* Set the tx word if the transfer's original "tx" is not null */
189 if (dws->tx_end - dws->len) {
190 if (dws->n_bytes == 1)
191 txw = *(u8 *)(dws->tx);
192 else
193 txw = *(u16 *)(dws->tx);
194 }
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200195 dw_write_io_reg(dws, DW_SPI_DR, txw);
Alek Du2ff271b2011-03-30 23:09:54 +0800196 dws->tx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800197 }
Feng Tange24c7452009-12-14 14:20:22 -0800198}
199
Alek Du3b8a4dd2011-03-30 23:09:55 +0800200static void dw_reader(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800201{
Alek Du2ff271b2011-03-30 23:09:54 +0800202 u32 max = rx_max(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800203 u16 rxw;
Feng Tange24c7452009-12-14 14:20:22 -0800204
Alek Du2ff271b2011-03-30 23:09:54 +0800205 while (max--) {
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200206 rxw = dw_read_io_reg(dws, DW_SPI_DR);
Feng Tangde6efe02011-03-30 23:09:52 +0800207 /* Care rx only if the transfer's original "rx" is not null */
208 if (dws->rx_end - dws->len) {
209 if (dws->n_bytes == 1)
210 *(u8 *)(dws->rx) = rxw;
211 else
212 *(u16 *)(dws->rx) = rxw;
213 }
214 dws->rx += dws->n_bytes;
Feng Tange24c7452009-12-14 14:20:22 -0800215 }
Feng Tange24c7452009-12-14 14:20:22 -0800216}
217
Feng Tange24c7452009-12-14 14:20:22 -0800218static void int_error_stop(struct dw_spi *dws, const char *msg)
219{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200220 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800221
222 dev_err(&dws->master->dev, "%s\n", msg);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200223 dws->master->cur_msg->status = -EIO;
224 spi_finalize_current_transfer(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800225}
226
Feng Tange24c7452009-12-14 14:20:22 -0800227static irqreturn_t interrupt_transfer(struct dw_spi *dws)
228{
Thor Thayerdd114442015-03-12 14:19:31 -0500229 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
Feng Tange24c7452009-12-14 14:20:22 -0800230
Feng Tange24c7452009-12-14 14:20:22 -0800231 /* Error handling */
232 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
Thor Thayerdd114442015-03-12 14:19:31 -0500233 dw_readl(dws, DW_SPI_ICR);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800234 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
Feng Tange24c7452009-12-14 14:20:22 -0800235 return IRQ_HANDLED;
236 }
237
Alek Du3b8a4dd2011-03-30 23:09:55 +0800238 dw_reader(dws);
239 if (dws->rx_end == dws->rx) {
240 spi_mask_intr(dws, SPI_INT_TXEI);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200241 spi_finalize_current_transfer(dws->master);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800242 return IRQ_HANDLED;
243 }
Feng Tang552e4502010-01-20 13:49:45 -0700244 if (irq_status & SPI_INT_TXEI) {
245 spi_mask_intr(dws, SPI_INT_TXEI);
Alek Du3b8a4dd2011-03-30 23:09:55 +0800246 dw_writer(dws);
247 /* Enable TX irq always, it will be disabled when RX finished */
248 spi_umask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800249 }
Feng Tang552e4502010-01-20 13:49:45 -0700250
Feng Tange24c7452009-12-14 14:20:22 -0800251 return IRQ_HANDLED;
252}
253
254static irqreturn_t dw_spi_irq(int irq, void *dev_id)
255{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200256 struct spi_controller *master = dev_id;
257 struct dw_spi *dws = spi_controller_get_devdata(master);
Thor Thayerdd114442015-03-12 14:19:31 -0500258 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
Yong Wangcbcc0622010-09-07 15:27:27 +0800259
Yong Wangcbcc0622010-09-07 15:27:27 +0800260 if (!irq_status)
261 return IRQ_NONE;
Feng Tange24c7452009-12-14 14:20:22 -0800262
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200263 if (!master->cur_msg) {
Feng Tange24c7452009-12-14 14:20:22 -0800264 spi_mask_intr(dws, SPI_INT_TXEI);
Feng Tange24c7452009-12-14 14:20:22 -0800265 return IRQ_HANDLED;
266 }
267
268 return dws->transfer_handler(dws);
269}
270
271/* Must be called inside pump_transfers() */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200272static int poll_transfer(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800273{
Alek Du2ff271b2011-03-30 23:09:54 +0800274 do {
275 dw_writer(dws);
Feng Tangde6efe02011-03-30 23:09:52 +0800276 dw_reader(dws);
Alek Du2ff271b2011-03-30 23:09:54 +0800277 cpu_relax();
278 } while (dws->rx_end > dws->rx);
Feng Tange24c7452009-12-14 14:20:22 -0800279
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200280 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800281}
282
Jarkko Nikula721483e2018-02-01 17:17:29 +0200283static int dw_spi_transfer_one(struct spi_controller *master,
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200284 struct spi_device *spi, struct spi_transfer *transfer)
Feng Tange24c7452009-12-14 14:20:22 -0800285{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200286 struct dw_spi *dws = spi_controller_get_devdata(master);
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200287 struct chip_data *chip = spi_get_ctldata(spi);
Feng Tange24c7452009-12-14 14:20:22 -0800288 u8 imask = 0;
Andy Shevchenkoea113702015-02-24 13:32:11 +0200289 u16 txlevel = 0;
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300290 u32 cr0;
Andy Shevchenko9f145382015-03-09 16:48:46 +0200291 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800292
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200293 dws->dma_mapped = 0;
Feng Tange24c7452009-12-14 14:20:22 -0800294
Feng Tange24c7452009-12-14 14:20:22 -0800295 dws->tx = (void *)transfer->tx_buf;
296 dws->tx_end = dws->tx + transfer->len;
297 dws->rx = transfer->rx_buf;
298 dws->rx_end = dws->rx + transfer->len;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200299 dws->len = transfer->len;
Feng Tange24c7452009-12-14 14:20:22 -0800300
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200301 spi_enable_chip(dws, 0);
302
Feng Tange24c7452009-12-14 14:20:22 -0800303 /* Handle per transfer options for bpw and speed */
Matthias Seidel13b10302016-09-04 02:04:49 +0200304 if (transfer->speed_hz != dws->current_freq) {
305 if (transfer->speed_hz != chip->speed_hz) {
306 /* clk_div doesn't support odd number */
Matthias Seidel3aef4632016-09-07 17:45:30 +0200307 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
Matthias Seidel13b10302016-09-04 02:04:49 +0200308 chip->speed_hz = transfer->speed_hz;
309 }
310 dws->current_freq = transfer->speed_hz;
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300311 spi_set_clk(dws, chip->clk_div);
Feng Tange24c7452009-12-14 14:20:22 -0800312 }
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200313
314 dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
315 dws->dma_width = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
316
Andy Shevchenko4adb1f82015-10-14 23:12:18 +0300317 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
Jarkko Nikula0ed36992015-09-15 16:26:23 +0300318 cr0 = (transfer->bits_per_word - 1)
319 | (chip->type << SPI_FRF_OFFSET)
320 | (spi->mode << SPI_MODE_OFFSET)
321 | (chip->tmode << SPI_TMOD_OFFSET);
Feng Tange24c7452009-12-14 14:20:22 -0800322
George Shore052dc7c2010-01-21 11:40:52 +0000323 /*
324 * Adjust transfer mode if necessary. Requires platform dependent
325 * chipselect mechanism.
326 */
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200327 if (chip->cs_control) {
George Shore052dc7c2010-01-21 11:40:52 +0000328 if (dws->rx && dws->tx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800329 chip->tmode = SPI_TMOD_TR;
George Shore052dc7c2010-01-21 11:40:52 +0000330 else if (dws->rx)
Feng Tange3e55ff2010-09-07 15:52:06 +0800331 chip->tmode = SPI_TMOD_RO;
George Shore052dc7c2010-01-21 11:40:52 +0000332 else
Feng Tange3e55ff2010-09-07 15:52:06 +0800333 chip->tmode = SPI_TMOD_TO;
George Shore052dc7c2010-01-21 11:40:52 +0000334
Feng Tange3e55ff2010-09-07 15:52:06 +0800335 cr0 &= ~SPI_TMOD_MASK;
George Shore052dc7c2010-01-21 11:40:52 +0000336 cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
337 }
338
Thor Thayerdd114442015-03-12 14:19:31 -0500339 dw_writel(dws, DW_SPI_CTRL0, cr0);
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200340
Feng Tange24c7452009-12-14 14:20:22 -0800341 /* Check if current transfer is a DMA transaction */
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200342 if (master->can_dma && master->can_dma(master, spi, transfer))
343 dws->dma_mapped = master->cur_msg_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800344
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200345 /* For poll mode just disable all interrupts */
346 spi_mask_intr(dws, 0xff);
347
Feng Tang552e4502010-01-20 13:49:45 -0700348 /*
349 * Interrupt mode
350 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
351 */
Andy Shevchenko9f145382015-03-09 16:48:46 +0200352 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200353 ret = dws->dma_ops->dma_setup(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200354 if (ret < 0) {
355 spi_enable_chip(dws, 1);
356 return ret;
357 }
358 } else if (!chip->poll_mode) {
Andy Shevchenkoea113702015-02-24 13:32:11 +0200359 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
Thor Thayerdd114442015-03-12 14:19:31 -0500360 dw_writel(dws, DW_SPI_TXFLTR, txlevel);
Feng Tang552e4502010-01-20 13:49:45 -0700361
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200362 /* Set the interrupt mask */
Jingoo Hanfadcace2014-09-02 11:49:24 +0900363 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
364 SPI_INT_RXUI | SPI_INT_RXOI;
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200365 spi_umask_intr(dws, imask);
366
Feng Tange24c7452009-12-14 14:20:22 -0800367 dws->transfer_handler = interrupt_transfer;
368 }
369
Andy Shevchenko0b2e8912015-03-02 14:58:56 +0200370 spi_enable_chip(dws, 1);
Feng Tange24c7452009-12-14 14:20:22 -0800371
Andy Shevchenko9f145382015-03-09 16:48:46 +0200372 if (dws->dma_mapped) {
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200373 ret = dws->dma_ops->dma_transfer(dws, transfer);
Andy Shevchenko9f145382015-03-09 16:48:46 +0200374 if (ret < 0)
375 return ret;
376 }
Feng Tange24c7452009-12-14 14:20:22 -0800377
378 if (chip->poll_mode)
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200379 return poll_transfer(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800380
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200381 return 1;
Feng Tange24c7452009-12-14 14:20:22 -0800382}
383
Jarkko Nikula721483e2018-02-01 17:17:29 +0200384static void dw_spi_handle_err(struct spi_controller *master,
Baruch Siachec37e8e2014-01-31 12:07:44 +0200385 struct spi_message *msg)
Feng Tange24c7452009-12-14 14:20:22 -0800386{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200387 struct dw_spi *dws = spi_controller_get_devdata(master);
Feng Tange24c7452009-12-14 14:20:22 -0800388
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200389 if (dws->dma_mapped)
390 dws->dma_ops->dma_stop(dws);
391
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200392 spi_reset_chip(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800393}
394
395/* This may be called twice for each spi dev */
396static int dw_spi_setup(struct spi_device *spi)
397{
398 struct dw_spi_chip *chip_info = NULL;
399 struct chip_data *chip;
Baruch Siachd9c73bb2014-01-31 12:07:47 +0200400 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800401
Feng Tange24c7452009-12-14 14:20:22 -0800402 /* Only alloc on first setup */
403 chip = spi_get_ctldata(spi);
404 if (!chip) {
Axel Lina97c8832014-08-31 12:47:06 +0800405 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Feng Tange24c7452009-12-14 14:20:22 -0800406 if (!chip)
407 return -ENOMEM;
Baruch Siach43f627a2013-12-30 20:30:46 +0200408 spi_set_ctldata(spi, chip);
Feng Tange24c7452009-12-14 14:20:22 -0800409 }
410
411 /*
412 * Protocol drivers may change the chip settings, so...
413 * if chip_info exists, use it
414 */
415 chip_info = spi->controller_data;
416
417 /* chip_info doesn't always exist */
418 if (chip_info) {
419 if (chip_info->cs_control)
420 chip->cs_control = chip_info->cs_control;
421
422 chip->poll_mode = chip_info->poll_mode;
423 chip->type = chip_info->type;
Feng Tange24c7452009-12-14 14:20:22 -0800424 }
425
Jisheng Zhang60968282015-12-23 19:05:39 +0800426 chip->tmode = SPI_TMOD_TR;
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300427
Baruch Siachd9c73bb2014-01-31 12:07:47 +0200428 if (gpio_is_valid(spi->cs_gpio)) {
429 ret = gpio_direction_output(spi->cs_gpio,
430 !(spi->mode & SPI_CS_HIGH));
431 if (ret)
432 return ret;
433 }
434
Feng Tange24c7452009-12-14 14:20:22 -0800435 return 0;
436}
437
Axel Lina97c8832014-08-31 12:47:06 +0800438static void dw_spi_cleanup(struct spi_device *spi)
439{
440 struct chip_data *chip = spi_get_ctldata(spi);
441
442 kfree(chip);
443 spi_set_ctldata(spi, NULL);
444}
445
Feng Tange24c7452009-12-14 14:20:22 -0800446/* Restart the controller, disable all interrupts, clean rx fifo */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200447static void spi_hw_init(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800448{
Andy Shevchenko45746e82015-03-02 14:58:55 +0200449 spi_reset_chip(dws);
Feng Tangc587b6f2010-01-21 10:41:10 +0800450
451 /*
452 * Try to detect the FIFO depth if not set by interface driver,
453 * the depth could be from 2 to 256 from HW spec
454 */
455 if (!dws->fifo_len) {
456 u32 fifo;
Jingoo Hanfadcace2014-09-02 11:49:24 +0900457
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200458 for (fifo = 1; fifo < 256; fifo++) {
Thor Thayerdd114442015-03-12 14:19:31 -0500459 dw_writel(dws, DW_SPI_TXFLTR, fifo);
460 if (fifo != dw_readl(dws, DW_SPI_TXFLTR))
Feng Tangc587b6f2010-01-21 10:41:10 +0800461 break;
462 }
Thor Thayerdd114442015-03-12 14:19:31 -0500463 dw_writel(dws, DW_SPI_TXFLTR, 0);
Feng Tangc587b6f2010-01-21 10:41:10 +0800464
Andy Shevchenko9d239d32015-02-25 11:39:36 +0200465 dws->fifo_len = (fifo == 1) ? 0 : fifo;
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200466 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
Feng Tangc587b6f2010-01-21 10:41:10 +0800467 }
Talel Shenharf2d70472018-10-11 14:20:07 +0300468
469 /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
470 if (dws->cs_override)
471 dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
Feng Tange24c7452009-12-14 14:20:22 -0800472}
473
Baruch Siach04f421e2013-12-30 20:30:44 +0200474int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800475{
Jarkko Nikula721483e2018-02-01 17:17:29 +0200476 struct spi_controller *master;
Feng Tange24c7452009-12-14 14:20:22 -0800477 int ret;
478
479 BUG_ON(dws == NULL);
480
Baruch Siach04f421e2013-12-30 20:30:44 +0200481 master = spi_alloc_master(dev, 0);
482 if (!master)
483 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -0800484
485 dws->master = master;
486 dws->type = SSI_MOTO_SPI;
Feng Tange24c7452009-12-14 14:20:22 -0800487 dws->dma_inited = 0;
Andy Shevchenkod7ef54c2015-10-27 17:48:16 +0200488 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
Feng Tange24c7452009-12-14 14:20:22 -0800489
Alexandre Belloni66b19d72018-07-17 16:23:10 +0200490 spi_controller_set_devdata(master, dws);
491
Phil Reide70002c802017-01-06 17:35:13 +0800492 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
493 master);
Feng Tange24c7452009-12-14 14:20:22 -0800494 if (ret < 0) {
Andy Shevchenko5f0966e2015-10-14 23:12:17 +0300495 dev_err(dev, "can not get IRQ\n");
Feng Tange24c7452009-12-14 14:20:22 -0800496 goto err_free_master;
497 }
498
Andy Shevchenkoc3ce15b2014-09-18 20:08:56 +0300499 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
Simon Goldschmidtaf060b32018-09-04 21:49:44 +0200500 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Feng Tange24c7452009-12-14 14:20:22 -0800501 master->bus_num = dws->bus_num;
502 master->num_chipselect = dws->num_cs;
Feng Tange24c7452009-12-14 14:20:22 -0800503 master->setup = dw_spi_setup;
Axel Lina97c8832014-08-31 12:47:06 +0800504 master->cleanup = dw_spi_cleanup;
Andy Shevchenkoc22c62d2015-03-02 14:58:57 +0200505 master->set_cs = dw_spi_set_cs;
506 master->transfer_one = dw_spi_transfer_one;
507 master->handle_err = dw_spi_handle_err;
Axel Lin765ee702014-02-20 21:37:56 +0800508 master->max_speed_hz = dws->max_freq;
Thor Thayer9c6de472014-10-08 13:51:34 -0500509 master->dev.of_node = dev->of_node;
Thor Thayer80b444e2016-10-10 09:25:25 -0500510 master->flags = SPI_MASTER_GPIO_SS;
Feng Tange24c7452009-12-14 14:20:22 -0800511
Alexandre Belloni62dbbae2018-07-17 16:23:11 +0200512 if (dws->set_cs)
513 master->set_cs = dws->set_cs;
514
Feng Tange24c7452009-12-14 14:20:22 -0800515 /* Basic HW init */
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200516 spi_hw_init(dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -0800517
Feng Tang7063c0d2010-12-24 13:59:11 +0800518 if (dws->dma_ops && dws->dma_ops->dma_init) {
519 ret = dws->dma_ops->dma_init(dws);
520 if (ret) {
Andy Shevchenko3dbb3b92015-01-07 16:56:54 +0200521 dev_warn(dev, "DMA init failed\n");
Feng Tang7063c0d2010-12-24 13:59:11 +0800522 dws->dma_inited = 0;
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200523 } else {
524 master->can_dma = dws->dma_ops->can_dma;
Feng Tang7063c0d2010-12-24 13:59:11 +0800525 }
526 }
527
Jarkko Nikula721483e2018-02-01 17:17:29 +0200528 ret = devm_spi_register_controller(dev, master);
Feng Tange24c7452009-12-14 14:20:22 -0800529 if (ret) {
530 dev_err(&master->dev, "problem registering spi master\n");
Baruch Siachec37e8e2014-01-31 12:07:44 +0200531 goto err_dma_exit;
Feng Tange24c7452009-12-14 14:20:22 -0800532 }
533
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300534 dw_spi_debugfs_init(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800535 return 0;
536
Baruch Siachec37e8e2014-01-31 12:07:44 +0200537err_dma_exit:
Feng Tang7063c0d2010-12-24 13:59:11 +0800538 if (dws->dma_ops && dws->dma_ops->dma_exit)
539 dws->dma_ops->dma_exit(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800540 spi_enable_chip(dws, 0);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300541 free_irq(dws->irq, master);
Feng Tange24c7452009-12-14 14:20:22 -0800542err_free_master:
Jarkko Nikula721483e2018-02-01 17:17:29 +0200543 spi_controller_put(master);
Feng Tange24c7452009-12-14 14:20:22 -0800544 return ret;
545}
Feng Tang79290a22010-12-24 13:59:10 +0800546EXPORT_SYMBOL_GPL(dw_spi_add_host);
Feng Tange24c7452009-12-14 14:20:22 -0800547
Grant Likelyfd4a3192012-12-07 16:57:14 +0000548void dw_spi_remove_host(struct dw_spi *dws)
Feng Tange24c7452009-12-14 14:20:22 -0800549{
Andy Shevchenko53288fe2014-09-12 15:11:56 +0300550 dw_spi_debugfs_remove(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800551
Feng Tang7063c0d2010-12-24 13:59:11 +0800552 if (dws->dma_ops && dws->dma_ops->dma_exit)
553 dws->dma_ops->dma_exit(dws);
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300554
555 spi_shutdown_chip(dws);
Andy Shevchenko02f20382015-10-20 12:11:40 +0300556
557 free_irq(dws->irq, dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800558}
Feng Tang79290a22010-12-24 13:59:10 +0800559EXPORT_SYMBOL_GPL(dw_spi_remove_host);
Feng Tange24c7452009-12-14 14:20:22 -0800560
561int dw_spi_suspend_host(struct dw_spi *dws)
562{
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300563 int ret;
Feng Tange24c7452009-12-14 14:20:22 -0800564
Jarkko Nikula721483e2018-02-01 17:17:29 +0200565 ret = spi_controller_suspend(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800566 if (ret)
567 return ret;
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300568
569 spi_shutdown_chip(dws);
570 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800571}
Feng Tang79290a22010-12-24 13:59:10 +0800572EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
Feng Tange24c7452009-12-14 14:20:22 -0800573
574int dw_spi_resume_host(struct dw_spi *dws)
575{
Andy Shevchenko30b4b702015-01-07 16:56:55 +0200576 spi_hw_init(&dws->master->dev, dws);
Geert Uytterhoeven7c5d8a22018-09-05 10:51:57 +0200577 return spi_controller_resume(dws->master);
Feng Tange24c7452009-12-14 14:20:22 -0800578}
Feng Tang79290a22010-12-24 13:59:10 +0800579EXPORT_SYMBOL_GPL(dw_spi_resume_host);
Feng Tange24c7452009-12-14 14:20:22 -0800580
581MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
582MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
583MODULE_LICENSE("GPL v2");