blob: 6c628a54e946942c1a32acea6292abf56717be4c [file] [log] [blame]
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001/*
2 * OMAP2 McSPI controller driver
3 *
4 * Copyright (C) 2005, 2006 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
Charulatha V1a5d8192011-02-02 17:52:14 +05306 * Juha Yrj�l� <juha.yrjola@nokia.com>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070017 */
18
19#include <linux/kernel.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070020#include <linux/interrupt.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
Russell King53741ed2012-04-23 13:51:48 +010025#include <linux/dmaengine.h>
Pascal Huerstbeca3652015-11-19 16:18:28 +010026#include <linux/pinctrl/consumer.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070027#include <linux/platform_device.h>
28#include <linux/err.h>
29#include <linux/clk.h>
30#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Govindraj.R1f1a4382011-02-02 17:52:15 +053032#include <linux/pm_runtime.h>
Benoit Coussond5a80032012-02-15 18:37:34 +010033#include <linux/of.h>
34#include <linux/of_device.h>
Illia Smyrnovd33f4732013-06-17 16:31:06 +030035#include <linux/gcd.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070036
37#include <linux/spi/spi.h>
Michael Wellingbc7f9bb2015-05-08 13:31:01 -050038#include <linux/gpio.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070039
Arnd Bergmann22037472012-08-24 15:21:06 +020040#include <linux/platform_data/spi-omap2-mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070041
42#define OMAP2_MCSPI_MAX_FREQ 48000000
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010043#define OMAP2_MCSPI_MAX_DIVIDER 4096
Illia Smyrnovd33f4732013-06-17 16:31:06 +030044#define OMAP2_MCSPI_MAX_FIFODEPTH 64
45#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
Shubhrajyoti D27b52842012-03-26 17:04:22 +053046#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070047
48#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070049#define OMAP2_MCSPI_SYSSTATUS 0x14
50#define OMAP2_MCSPI_IRQSTATUS 0x18
51#define OMAP2_MCSPI_IRQENABLE 0x1c
52#define OMAP2_MCSPI_WAKEUPENABLE 0x20
53#define OMAP2_MCSPI_SYST 0x24
54#define OMAP2_MCSPI_MODULCTRL 0x28
Illia Smyrnovd33f4732013-06-17 16:31:06 +030055#define OMAP2_MCSPI_XFERLEVEL 0x7c
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070056
57/* per-channel banks, 0x14 bytes each, first is: */
58#define OMAP2_MCSPI_CHCONF0 0x2c
59#define OMAP2_MCSPI_CHSTAT0 0x30
60#define OMAP2_MCSPI_CHCTRL0 0x34
61#define OMAP2_MCSPI_TX0 0x38
62#define OMAP2_MCSPI_RX0 0x3c
63
64/* per-register bitmasks: */
Illia Smyrnovd33f4732013-06-17 16:31:06 +030065#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070066
Jouni Hogander7a8fa722009-09-22 16:45:58 -070067#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
68#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
69#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070070
Jouni Hogander7a8fa722009-09-22 16:45:58 -070071#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
72#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070073#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070074#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070075#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070076#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
77#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070078#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070079#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
80#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
81#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
82#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
83#define OMAP2_MCSPI_CHCONF_IS BIT(18)
84#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
85#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030086#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
87#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010088#define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070089
Jouni Hogander7a8fa722009-09-22 16:45:58 -070090#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
91#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
92#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030093#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070094
Jouni Hogander7a8fa722009-09-22 16:45:58 -070095#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010096#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070097
Jouni Hogander7a8fa722009-09-22 16:45:58 -070098#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070099
100/* We have 2 DMA channels per CS, one for RX and one for TX */
101struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +0100102 struct dma_chan *dma_tx;
103 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700104
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700105 struct completion dma_tx_completion;
106 struct completion dma_rx_completion;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530107
108 char dma_rx_ch_name[14];
109 char dma_tx_ch_name[14];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700110};
111
112/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
113 * cache operations; better heuristics consider wordsize and bitrate.
114 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000115#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700116
117
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530118/*
119 * Used for context save and restore, structure members to be updated whenever
120 * corresponding registers are modified.
121 */
122struct omap2_mcspi_regs {
123 u32 modulctrl;
124 u32 wakeupenable;
125 struct list_head cs;
126};
127
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700128struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700129 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700130 /* Virtual base address of the controller */
131 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100132 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700133 /* SPI1 has 4 channels, while SPI2 has 2 */
134 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530135 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530136 struct omap2_mcspi_regs ctx;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300137 int fifo_depth;
Daniel Mack0384e902012-10-07 18:19:44 +0200138 unsigned int pin_dir:1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700139};
140
141struct omap2_mcspi_cs {
142 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100143 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700144 int word_len;
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700145 u16 mode;
Tero Kristo89c05372009-09-22 16:46:17 -0700146 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700147 /* Context save and restore shadow register */
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100148 u32 chconf0, chctrl0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700149};
150
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700151static inline void mcspi_write_reg(struct spi_master *master,
152 int idx, u32 val)
153{
154 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
155
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200156 writel_relaxed(val, mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700157}
158
159static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
160{
161 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
162
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200163 return readl_relaxed(mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700164}
165
166static inline void mcspi_write_cs_reg(const struct spi_device *spi,
167 int idx, u32 val)
168{
169 struct omap2_mcspi_cs *cs = spi->controller_state;
170
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200171 writel_relaxed(val, cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700172}
173
174static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
175{
176 struct omap2_mcspi_cs *cs = spi->controller_state;
177
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200178 return readl_relaxed(cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700179}
180
Hemanth Va41ae1a2009-09-22 16:46:16 -0700181static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
182{
183 struct omap2_mcspi_cs *cs = spi->controller_state;
184
185 return cs->chconf0;
186}
187
188static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
189{
190 struct omap2_mcspi_cs *cs = spi->controller_state;
191
192 cs->chconf0 = val;
193 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000194 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700195}
196
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300197static inline int mcspi_bytes_per_word(int word_len)
198{
199 if (word_len <= 8)
200 return 1;
201 else if (word_len <= 16)
202 return 2;
203 else /* word_len <= 32 */
204 return 4;
205}
206
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700207static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
208 int is_read, int enable)
209{
210 u32 l, rw;
211
Hemanth Va41ae1a2009-09-22 16:46:16 -0700212 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700213
214 if (is_read) /* 1 is read, 0 write */
215 rw = OMAP2_MCSPI_CHCONF_DMAR;
216 else
217 rw = OMAP2_MCSPI_CHCONF_DMAW;
218
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530219 if (enable)
220 l |= rw;
221 else
222 l &= ~rw;
223
Hemanth Va41ae1a2009-09-22 16:46:16 -0700224 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700225}
226
227static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
228{
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100229 struct omap2_mcspi_cs *cs = spi->controller_state;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700230 u32 l;
231
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100232 l = cs->chctrl0;
233 if (enable)
234 l |= OMAP2_MCSPI_CHCTRL_EN;
235 else
236 l &= ~OMAP2_MCSPI_CHCTRL_EN;
237 cs->chctrl0 = l;
238 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000239 /* Flash post-writes */
240 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700241}
242
Michael Wellingddcad7e2015-05-12 12:38:57 -0500243static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700244{
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200245 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700246 u32 l;
247
Michael Welling4373f8b2015-05-23 21:13:43 -0500248 /* The controller handles the inverted chip selects
249 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
250 * the inversion from the core spi_set_cs function.
251 */
252 if (spi->mode & SPI_CS_HIGH)
253 enable = !enable;
254
Michael Wellingddcad7e2015-05-12 12:38:57 -0500255 if (spi->controller_state) {
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200256 int err = pm_runtime_get_sync(mcspi->dev);
257 if (err < 0) {
Tony Lindgren5a686b22018-04-27 08:50:07 -0700258 pm_runtime_put_noidle(mcspi->dev);
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200259 dev_err(mcspi->dev, "failed to get sync: %d\n", err);
260 return;
261 }
262
Michael Wellingddcad7e2015-05-12 12:38:57 -0500263 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530264
Michael Wellingddcad7e2015-05-12 12:38:57 -0500265 if (enable)
266 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
267 else
268 l |= OMAP2_MCSPI_CHCONF_FORCE;
269
270 mcspi_write_chconf0(spi, l);
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200271
272 pm_runtime_mark_last_busy(mcspi->dev);
273 pm_runtime_put_autosuspend(mcspi->dev);
Michael Wellingddcad7e2015-05-12 12:38:57 -0500274 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700275}
276
277static void omap2_mcspi_set_master_mode(struct spi_master *master)
278{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530279 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
280 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700281 u32 l;
282
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530283 /*
284 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700285 * to single-channel master mode
286 */
287 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530288 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
289 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700290 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700291
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530292 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700293}
294
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300295static void omap2_mcspi_set_fifo(const struct spi_device *spi,
296 struct spi_transfer *t, int enable)
297{
298 struct spi_master *master = spi->master;
299 struct omap2_mcspi_cs *cs = spi->controller_state;
300 struct omap2_mcspi *mcspi;
301 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300302 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300303 u32 chconf, xferlevel;
304
305 mcspi = spi_master_get_devdata(master);
306
307 chconf = mcspi_cached_chconf0(spi);
308 if (enable) {
309 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
310 if (t->len % bytes_per_word != 0)
311 goto disable_fifo;
312
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300313 if (t->rx_buf != NULL && t->tx_buf != NULL)
314 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
315 else
316 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
317
318 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300319 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
320 goto disable_fifo;
321
322 wcnt = t->len / bytes_per_word;
323 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
324 goto disable_fifo;
325
326 xferlevel = wcnt << 16;
327 if (t->rx_buf != NULL) {
328 chconf |= OMAP2_MCSPI_CHCONF_FFER;
329 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300330 }
331 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300332 chconf |= OMAP2_MCSPI_CHCONF_FFET;
333 xferlevel |= fifo_depth - 1;
334 }
335
336 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
337 mcspi_write_chconf0(spi, chconf);
338 mcspi->fifo_depth = fifo_depth;
339
340 return;
341 }
342
343disable_fifo:
344 if (t->rx_buf != NULL)
345 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500346
347 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300348 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
349
350 mcspi_write_chconf0(spi, chconf);
351 mcspi->fifo_depth = 0;
352}
353
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300354static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
355{
356 unsigned long timeout;
357
358 timeout = jiffies + msecs_to_jiffies(1000);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200359 while (!(readl_relaxed(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100360 if (time_after(jiffies, timeout)) {
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200361 if (!(readl_relaxed(reg) & bit))
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100362 return -ETIMEDOUT;
363 else
364 return 0;
365 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300366 cpu_relax();
367 }
368 return 0;
369}
370
Russell King53741ed2012-04-23 13:51:48 +0100371static void omap2_mcspi_rx_callback(void *data)
372{
373 struct spi_device *spi = data;
374 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
375 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
376
Russell King53741ed2012-04-23 13:51:48 +0100377 /* We must disable the DMA RX request */
378 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200379
380 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100381}
382
383static void omap2_mcspi_tx_callback(void *data)
384{
385 struct spi_device *spi = data;
386 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
387 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
388
Russell King53741ed2012-04-23 13:51:48 +0100389 /* We must disable the DMA TX request */
390 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200391
392 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100393}
394
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530395static void omap2_mcspi_tx_dma(struct spi_device *spi,
396 struct spi_transfer *xfer,
397 struct dma_slave_config cfg)
398{
399 struct omap2_mcspi *mcspi;
400 struct omap2_mcspi_dma *mcspi_dma;
401 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530402
403 mcspi = spi_master_get_devdata(spi->master);
404 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
405 count = xfer->len;
406
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530407 if (mcspi_dma->dma_tx) {
408 struct dma_async_tx_descriptor *tx;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530409
410 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
411
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500412 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
413 xfer->tx_sg.nents,
414 DMA_MEM_TO_DEV,
415 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530416 if (tx) {
417 tx->callback = omap2_mcspi_tx_callback;
418 tx->callback_param = spi;
419 dmaengine_submit(tx);
420 } else {
421 /* FIXME: fall back to PIO? */
422 }
423 }
424 dma_async_issue_pending(mcspi_dma->dma_tx);
425 omap2_mcspi_set_dma_req(spi, 0, 1);
426
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530427}
428
429static unsigned
430omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
431 struct dma_slave_config cfg,
432 unsigned es)
433{
434 struct omap2_mcspi *mcspi;
435 struct omap2_mcspi_dma *mcspi_dma;
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500436 unsigned int count, transfer_reduction = 0;
437 struct scatterlist *sg_out[2];
438 int nb_sizes = 0, out_mapped_nents[2], ret, x;
439 size_t sizes[2];
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530440 u32 l;
441 int elements = 0;
442 int word_len, element_count;
443 struct omap2_mcspi_cs *cs = spi->controller_state;
Akinobu Mita81261352017-03-22 09:18:26 +0900444 void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
445
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530446 mcspi = spi_master_get_devdata(spi->master);
447 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
448 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300449
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500450 /*
451 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
452 * it mentions reducing DMA transfer length by one element in master
453 * normal mode.
454 */
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300455 if (mcspi->fifo_depth == 0)
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500456 transfer_reduction = es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300457
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530458 word_len = cs->word_len;
459 l = mcspi_cached_chconf0(spi);
460
461 if (word_len <= 8)
462 element_count = count;
463 else if (word_len <= 16)
464 element_count = count >> 1;
465 else /* word_len <= 32 */
466 element_count = count >> 2;
467
468 if (mcspi_dma->dma_rx) {
469 struct dma_async_tx_descriptor *tx;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530470
471 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
472
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500473 /*
474 * Reduce DMA transfer length by one more if McSPI is
475 * configured in turbo mode.
476 */
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300477 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500478 transfer_reduction += es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530479
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500480 if (transfer_reduction) {
481 /* Split sgl into two. The second sgl won't be used. */
482 sizes[0] = count - transfer_reduction;
483 sizes[1] = transfer_reduction;
484 nb_sizes = 2;
485 } else {
486 /*
487 * Don't bother splitting the sgl. This essentially
488 * clones the original sgl.
489 */
490 sizes[0] = count;
491 nb_sizes = 1;
492 }
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530493
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500494 ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents,
495 0, nb_sizes,
496 sizes,
497 sg_out, out_mapped_nents,
498 GFP_KERNEL);
499
500 if (ret < 0) {
501 dev_err(&spi->dev, "sg_split failed\n");
502 return 0;
503 }
504
505 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx,
506 sg_out[0],
507 out_mapped_nents[0],
508 DMA_DEV_TO_MEM,
509 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530510 if (tx) {
511 tx->callback = omap2_mcspi_rx_callback;
512 tx->callback_param = spi;
513 dmaengine_submit(tx);
514 } else {
515 /* FIXME: fall back to PIO? */
516 }
517 }
518
519 dma_async_issue_pending(mcspi_dma->dma_rx);
520 omap2_mcspi_set_dma_req(spi, 1, 1);
521
522 wait_for_completion(&mcspi_dma->dma_rx_completion);
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500523
524 for (x = 0; x < nb_sizes; x++)
525 kfree(sg_out[x]);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300526
527 if (mcspi->fifo_depth > 0)
528 return count;
529
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500530 /*
531 * Due to the DMA transfer length reduction the missing bytes must
532 * be read manually to receive all of the expected data.
533 */
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530534 omap2_mcspi_set_enable(spi, 0);
535
536 elements = element_count - 1;
537
538 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
539 elements--;
540
Akinobu Mita81261352017-03-22 09:18:26 +0900541 if (!mcspi_wait_for_reg_bit(chstat_reg,
542 OMAP2_MCSPI_CHSTAT_RXS)) {
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530543 u32 w;
544
545 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
546 if (word_len <= 8)
547 ((u8 *)xfer->rx_buf)[elements++] = w;
548 else if (word_len <= 16)
549 ((u16 *)xfer->rx_buf)[elements++] = w;
550 else /* word_len <= 32 */
551 ((u32 *)xfer->rx_buf)[elements++] = w;
552 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300553 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300554 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300555 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530556 omap2_mcspi_set_enable(spi, 1);
557 return count;
558 }
559 }
Akinobu Mita81261352017-03-22 09:18:26 +0900560 if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530561 u32 w;
562
563 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
564 if (word_len <= 8)
565 ((u8 *)xfer->rx_buf)[elements] = w;
566 else if (word_len <= 16)
567 ((u16 *)xfer->rx_buf)[elements] = w;
568 else /* word_len <= 32 */
569 ((u32 *)xfer->rx_buf)[elements] = w;
570 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300571 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300572 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530573 }
574 omap2_mcspi_set_enable(spi, 1);
575 return count;
576}
577
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700578static unsigned
579omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
580{
581 struct omap2_mcspi *mcspi;
582 struct omap2_mcspi_cs *cs = spi->controller_state;
583 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100584 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000585 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530586 u8 *rx;
587 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100588 struct dma_slave_config cfg;
589 enum dma_slave_buswidth width;
590 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300591 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530592 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300593 void __iomem *irqstat_reg;
594 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700595
596 mcspi = spi_master_get_devdata(spi->master);
597 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000598 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700599
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300600
Russell King53741ed2012-04-23 13:51:48 +0100601 if (cs->word_len <= 8) {
602 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
603 es = 1;
604 } else if (cs->word_len <= 16) {
605 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
606 es = 2;
607 } else {
608 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
609 es = 4;
610 }
611
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300612 count = xfer->len;
613 burst = 1;
614
615 if (mcspi->fifo_depth > 0) {
616 if (count > mcspi->fifo_depth)
617 burst = mcspi->fifo_depth / es;
618 else
619 burst = count / es;
620 }
621
Russell King53741ed2012-04-23 13:51:48 +0100622 memset(&cfg, 0, sizeof(cfg));
623 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
624 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
625 cfg.src_addr_width = width;
626 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300627 cfg.src_maxburst = burst;
628 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100629
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700630 rx = xfer->rx_buf;
631 tx = xfer->tx_buf;
632
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530633 if (tx != NULL)
634 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700635
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530636 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530637 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700638
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530639 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530640 wait_for_completion(&mcspi_dma->dma_tx_completion);
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530641
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300642 if (mcspi->fifo_depth > 0) {
643 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
644
645 if (mcspi_wait_for_reg_bit(irqstat_reg,
646 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
647 dev_err(&spi->dev, "EOW timed out\n");
648
649 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
650 OMAP2_MCSPI_IRQSTATUS_EOW);
651 }
652
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530653 /* for TX_ONLY mode, be sure all words have shifted out */
654 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300655 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
656 if (mcspi->fifo_depth > 0) {
657 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
658 OMAP2_MCSPI_CHSTAT_TXFFE);
659 if (wait_res < 0)
660 dev_err(&spi->dev, "TXFFE timed out\n");
661 } else {
662 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
663 OMAP2_MCSPI_CHSTAT_TXS);
664 if (wait_res < 0)
665 dev_err(&spi->dev, "TXS timed out\n");
666 }
667 if (wait_res >= 0 &&
668 (mcspi_wait_for_reg_bit(chstat_reg,
669 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530670 dev_err(&spi->dev, "EOT timed out\n");
671 }
672 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700673 return count;
674}
675
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700676static unsigned
677omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
678{
679 struct omap2_mcspi *mcspi;
680 struct omap2_mcspi_cs *cs = spi->controller_state;
681 unsigned int count, c;
682 u32 l;
683 void __iomem *base = cs->base;
684 void __iomem *tx_reg;
685 void __iomem *rx_reg;
686 void __iomem *chstat_reg;
687 int word_len;
688
689 mcspi = spi_master_get_devdata(spi->master);
690 count = xfer->len;
691 c = count;
692 word_len = cs->word_len;
693
Hemanth Va41ae1a2009-09-22 16:46:16 -0700694 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700695
696 /* We store the pre-calculated register addresses on stack to speed
697 * up the transfer loop. */
698 tx_reg = base + OMAP2_MCSPI_TX0;
699 rx_reg = base + OMAP2_MCSPI_RX0;
700 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
701
Michael Jonesadef6582011-02-25 16:55:11 +0100702 if (c < (word_len>>3))
703 return 0;
704
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700705 if (word_len <= 8) {
706 u8 *rx;
707 const u8 *tx;
708
709 rx = xfer->rx_buf;
710 tx = xfer->tx_buf;
711
712 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800713 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700714 if (tx != NULL) {
715 if (mcspi_wait_for_reg_bit(chstat_reg,
716 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
717 dev_err(&spi->dev, "TXS timed out\n");
718 goto out;
719 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900720 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700721 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200722 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700723 }
724 if (rx != NULL) {
725 if (mcspi_wait_for_reg_bit(chstat_reg,
726 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
727 dev_err(&spi->dev, "RXS timed out\n");
728 goto out;
729 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000730
731 if (c == 1 && tx == NULL &&
732 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
733 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200734 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900735 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000736 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000737 if (mcspi_wait_for_reg_bit(chstat_reg,
738 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
739 dev_err(&spi->dev,
740 "RXS timed out\n");
741 goto out;
742 }
743 c = 0;
744 } else if (c == 0 && tx == NULL) {
745 omap2_mcspi_set_enable(spi, 0);
746 }
747
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200748 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900749 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700750 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700751 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200752 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700753 } else if (word_len <= 16) {
754 u16 *rx;
755 const u16 *tx;
756
757 rx = xfer->rx_buf;
758 tx = xfer->tx_buf;
759 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800760 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700761 if (tx != NULL) {
762 if (mcspi_wait_for_reg_bit(chstat_reg,
763 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
764 dev_err(&spi->dev, "TXS timed out\n");
765 goto out;
766 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900767 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700768 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200769 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700770 }
771 if (rx != NULL) {
772 if (mcspi_wait_for_reg_bit(chstat_reg,
773 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
774 dev_err(&spi->dev, "RXS timed out\n");
775 goto out;
776 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000777
778 if (c == 2 && tx == NULL &&
779 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
780 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200781 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900782 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000783 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000784 if (mcspi_wait_for_reg_bit(chstat_reg,
785 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
786 dev_err(&spi->dev,
787 "RXS timed out\n");
788 goto out;
789 }
790 c = 0;
791 } else if (c == 0 && tx == NULL) {
792 omap2_mcspi_set_enable(spi, 0);
793 }
794
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200795 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900796 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700797 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700798 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200799 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700800 } else if (word_len <= 32) {
801 u32 *rx;
802 const u32 *tx;
803
804 rx = xfer->rx_buf;
805 tx = xfer->tx_buf;
806 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800807 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700808 if (tx != NULL) {
809 if (mcspi_wait_for_reg_bit(chstat_reg,
810 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
811 dev_err(&spi->dev, "TXS timed out\n");
812 goto out;
813 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900814 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700815 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200816 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700817 }
818 if (rx != NULL) {
819 if (mcspi_wait_for_reg_bit(chstat_reg,
820 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
821 dev_err(&spi->dev, "RXS timed out\n");
822 goto out;
823 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000824
825 if (c == 4 && tx == NULL &&
826 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
827 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200828 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900829 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000830 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000831 if (mcspi_wait_for_reg_bit(chstat_reg,
832 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
833 dev_err(&spi->dev,
834 "RXS timed out\n");
835 goto out;
836 }
837 c = 0;
838 } else if (c == 0 && tx == NULL) {
839 omap2_mcspi_set_enable(spi, 0);
840 }
841
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200842 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900843 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700844 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700845 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200846 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700847 }
848
849 /* for TX_ONLY mode, be sure all words have shifted out */
850 if (xfer->rx_buf == NULL) {
851 if (mcspi_wait_for_reg_bit(chstat_reg,
852 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
853 dev_err(&spi->dev, "TXS timed out\n");
854 } else if (mcspi_wait_for_reg_bit(chstat_reg,
855 OMAP2_MCSPI_CHSTAT_EOT) < 0)
856 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800857
858 /* disable chan to purge rx datas received in TX_ONLY transfer,
859 * otherwise these rx datas will affect the direct following
860 * RX_ONLY transfer.
861 */
862 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700863 }
864out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000865 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700866 return count - c;
867}
868
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200869static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
870{
871 u32 div;
872
873 for (div = 0; div < 15; div++)
874 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
875 return div;
876
877 return 15;
878}
879
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700880/* called only when no transfer is active to this device */
881static int omap2_mcspi_setup_transfer(struct spi_device *spi,
882 struct spi_transfer *t)
883{
884 struct omap2_mcspi_cs *cs = spi->controller_state;
885 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700886 struct spi_master *spi_cntrl;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100887 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700888 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700889 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700890
891 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700892 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700893
894 if (t != NULL && t->bits_per_word)
895 word_len = t->bits_per_word;
896
897 cs->word_len = word_len;
898
Scott Ellis9bd45172010-03-10 14:23:13 -0700899 if (t && t->speed_hz)
900 speed_hz = t->speed_hz;
901
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200902 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100903 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
904 clkd = omap2_mcspi_calc_divisor(speed_hz);
905 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
906 clkg = 0;
907 } else {
908 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
909 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
910 clkd = (div - 1) & 0xf;
911 extclk = (div - 1) >> 4;
912 clkg = OMAP2_MCSPI_CHCONF_CLKG;
913 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700914
Hemanth Va41ae1a2009-09-22 16:46:16 -0700915 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700916
917 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
918 * REVISIT: this controller could support SPI_3WIRE mode.
919 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800920 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200921 l &= ~OMAP2_MCSPI_CHCONF_IS;
922 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
923 l |= OMAP2_MCSPI_CHCONF_DPE0;
924 } else {
925 l |= OMAP2_MCSPI_CHCONF_IS;
926 l |= OMAP2_MCSPI_CHCONF_DPE1;
927 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
928 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700929
930 /* wordlength */
931 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
932 l |= (word_len - 1) << 7;
933
934 /* set chipselect polarity; manage with FORCE */
935 if (!(spi->mode & SPI_CS_HIGH))
936 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
937 else
938 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
939
940 /* set clock divisor */
941 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100942 l |= clkd << 2;
943
944 /* set clock granularity */
945 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
946 l |= clkg;
947 if (clkg) {
948 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
949 cs->chctrl0 |= extclk << 8;
950 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
951 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700952
953 /* set SPI mode 0..3 */
954 if (spi->mode & SPI_CPOL)
955 l |= OMAP2_MCSPI_CHCONF_POL;
956 else
957 l &= ~OMAP2_MCSPI_CHCONF_POL;
958 if (spi->mode & SPI_CPHA)
959 l |= OMAP2_MCSPI_CHCONF_PHA;
960 else
961 l &= ~OMAP2_MCSPI_CHCONF_PHA;
962
Hemanth Va41ae1a2009-09-22 16:46:16 -0700963 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700964
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700965 cs->mode = spi->mode;
966
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700967 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100968 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700969 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
970 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
971
972 return 0;
973}
974
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700975/*
976 * Note that we currently allow DMA only if we get a channel
977 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
978 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700979static int omap2_mcspi_request_dma(struct spi_device *spi)
980{
981 struct spi_master *master = spi->master;
982 struct omap2_mcspi *mcspi;
983 struct omap2_mcspi_dma *mcspi_dma;
Peter Ujfalusib085c612016-04-29 16:11:56 +0300984 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700985
986 mcspi = spi_master_get_devdata(master);
987 mcspi_dma = mcspi->dma_channels + spi->chip_select;
988
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700989 init_completion(&mcspi_dma->dma_rx_completion);
990 init_completion(&mcspi_dma->dma_tx_completion);
991
Peter Ujfalusib085c612016-04-29 16:11:56 +0300992 mcspi_dma->dma_rx = dma_request_chan(&master->dev,
993 mcspi_dma->dma_rx_ch_name);
994 if (IS_ERR(mcspi_dma->dma_rx)) {
995 ret = PTR_ERR(mcspi_dma->dma_rx);
Russell King53741ed2012-04-23 13:51:48 +0100996 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700997 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100998 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700999
Peter Ujfalusib085c612016-04-29 16:11:56 +03001000 mcspi_dma->dma_tx = dma_request_chan(&master->dev,
1001 mcspi_dma->dma_tx_ch_name);
1002 if (IS_ERR(mcspi_dma->dma_tx)) {
1003 ret = PTR_ERR(mcspi_dma->dma_tx);
1004 mcspi_dma->dma_tx = NULL;
1005 dma_release_channel(mcspi_dma->dma_rx);
1006 mcspi_dma->dma_rx = NULL;
1007 }
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001008
1009no_dma:
Peter Ujfalusib085c612016-04-29 16:11:56 +03001010 return ret;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001011}
1012
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001013static int omap2_mcspi_setup(struct spi_device *spi)
1014{
1015 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301016 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
1017 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001018 struct omap2_mcspi_dma *mcspi_dma;
1019 struct omap2_mcspi_cs *cs = spi->controller_state;
1020
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001021 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1022
1023 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +01001024 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001025 if (!cs)
1026 return -ENOMEM;
1027 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +01001028 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001029 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001030 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001031 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001032 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001033 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301034 list_add_tail(&cs->node, &ctx->cs);
Michael Welling2f538c02015-11-30 09:02:39 -06001035
1036 if (gpio_is_valid(spi->cs_gpio)) {
1037 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
1038 if (ret) {
1039 dev_err(&spi->dev, "failed to request gpio\n");
1040 return ret;
1041 }
1042 gpio_direction_output(spi->cs_gpio,
1043 !(spi->mode & SPI_CS_HIGH));
1044 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001045 }
1046
Russell King8c7494a2012-04-23 13:56:25 +01001047 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001048 ret = omap2_mcspi_request_dma(spi);
Peter Ujfalusib085c612016-04-29 16:11:56 +03001049 if (ret)
1050 dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n",
1051 ret);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001052 }
1053
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301054 ret = pm_runtime_get_sync(mcspi->dev);
Tony Lindgren5a686b22018-04-27 08:50:07 -07001055 if (ret < 0) {
1056 pm_runtime_put_noidle(mcspi->dev);
1057
Govindraj.R1f1a4382011-02-02 17:52:15 +05301058 return ret;
Tony Lindgren5a686b22018-04-27 08:50:07 -07001059 }
Hemanth Va41ae1a2009-09-22 16:46:16 -07001060
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001061 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301062 pm_runtime_mark_last_busy(mcspi->dev);
1063 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001064
1065 return ret;
1066}
1067
1068static void omap2_mcspi_cleanup(struct spi_device *spi)
1069{
1070 struct omap2_mcspi *mcspi;
1071 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001072 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001073
1074 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001075
Scott Ellis5e774942010-03-10 14:22:45 -07001076 if (spi->controller_state) {
1077 /* Unlink controller state from context save list */
1078 cs = spi->controller_state;
1079 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001080
Russell King10aa5a32012-06-18 11:27:04 +01001081 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001082 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001083
Scott Ellis99f1a432010-05-24 14:20:27 +00001084 if (spi->chip_select < spi->master->num_chipselect) {
1085 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1086
Russell King53741ed2012-04-23 13:51:48 +01001087 if (mcspi_dma->dma_rx) {
1088 dma_release_channel(mcspi_dma->dma_rx);
1089 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001090 }
Russell King53741ed2012-04-23 13:51:48 +01001091 if (mcspi_dma->dma_tx) {
1092 dma_release_channel(mcspi_dma->dma_tx);
1093 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001094 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001095 }
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001096
1097 if (gpio_is_valid(spi->cs_gpio))
1098 gpio_free(spi->cs_gpio);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001099}
1100
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001101static int omap2_mcspi_transfer_one(struct spi_master *master,
1102 struct spi_device *spi,
1103 struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001104{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001105
1106 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301107 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001108 * arbitrate among multiple channels. This corresponds to "single
1109 * channel" master mode. As a side effect, we need to manage the
1110 * chipselect with the FORCE bit ... CS != channel enable.
1111 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001112
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001113 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001114 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301115 struct omap2_mcspi_cs *cs;
1116 struct omap2_mcspi_device_config *cd;
1117 int par_override = 0;
1118 int status = 0;
1119 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001120
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001121 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001122 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301123 cs = spi->controller_state;
1124 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001125
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001126 /*
1127 * The slave driver could have changed spi->mode in which case
1128 * it will be different from cs->mode (the current hardware setup).
1129 * If so, set par_override (even though its not a parity issue) so
1130 * omap2_mcspi_setup_transfer will be called to configure the hardware
1131 * with the correct mode on the first iteration of the loop below.
1132 */
1133 if (spi->mode != cs->mode)
1134 par_override = 1;
1135
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001136 omap2_mcspi_set_enable(spi, 0);
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001137
Michael Wellinga06b4302015-05-23 21:13:44 -05001138 if (gpio_is_valid(spi->cs_gpio))
1139 omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
1140
Michael Wellingb28cb942015-05-07 18:36:53 -05001141 if (par_override ||
1142 (t->speed_hz != spi->max_speed_hz) ||
1143 (t->bits_per_word != spi->bits_per_word)) {
1144 par_override = 1;
1145 status = omap2_mcspi_setup_transfer(spi, t);
1146 if (status < 0)
1147 goto out;
1148 if (t->speed_hz == spi->max_speed_hz &&
1149 t->bits_per_word == spi->bits_per_word)
1150 par_override = 0;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301151 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001152 if (cd && cd->cs_per_word) {
1153 chconf = mcspi->ctx.modulctrl;
1154 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1155 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1156 mcspi->ctx.modulctrl =
1157 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1158 }
1159
Michael Wellingb28cb942015-05-07 18:36:53 -05001160 chconf = mcspi_cached_chconf0(spi);
1161 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1162 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1163
1164 if (t->tx_buf == NULL)
1165 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1166 else if (t->rx_buf == NULL)
1167 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1168
1169 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1170 /* Turbo mode is for more than one word */
1171 if (t->len > ((cs->word_len + 7) >> 3))
1172 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1173 }
1174
1175 mcspi_write_chconf0(spi, chconf);
1176
1177 if (t->len) {
1178 unsigned count;
1179
1180 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001181 master->cur_msg_mapped &&
1182 master->can_dma(master, spi, t))
Michael Wellingb28cb942015-05-07 18:36:53 -05001183 omap2_mcspi_set_fifo(spi, t, 1);
1184
1185 omap2_mcspi_set_enable(spi, 1);
1186
1187 /* RX_ONLY mode needs dummy data in TX reg */
1188 if (t->tx_buf == NULL)
1189 writel_relaxed(0, cs->base
1190 + OMAP2_MCSPI_TX0);
1191
1192 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001193 master->cur_msg_mapped &&
1194 master->can_dma(master, spi, t))
Michael Wellingb28cb942015-05-07 18:36:53 -05001195 count = omap2_mcspi_txrx_dma(spi, t);
1196 else
1197 count = omap2_mcspi_txrx_pio(spi, t);
1198
1199 if (count != t->len) {
1200 status = -EIO;
1201 goto out;
1202 }
1203 }
1204
Michael Wellingb28cb942015-05-07 18:36:53 -05001205 omap2_mcspi_set_enable(spi, 0);
1206
1207 if (mcspi->fifo_depth > 0)
1208 omap2_mcspi_set_fifo(spi, t, 0);
1209
1210out:
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301211 /* Restore defaults if they were overriden */
1212 if (par_override) {
1213 par_override = 0;
1214 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001215 }
1216
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001217 if (cd && cd->cs_per_word) {
1218 chconf = mcspi->ctx.modulctrl;
1219 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1220 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1221 mcspi->ctx.modulctrl =
1222 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1223 }
1224
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301225 omap2_mcspi_set_enable(spi, 0);
1226
Michael Wellinga06b4302015-05-23 21:13:44 -05001227 if (gpio_is_valid(spi->cs_gpio))
1228 omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
1229
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001230 if (mcspi->fifo_depth > 0 && t)
1231 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301232
Michael Wellingb28cb942015-05-07 18:36:53 -05001233 return status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001234}
1235
Neil Armstrong468a3202015-10-09 15:47:41 +02001236static int omap2_mcspi_prepare_message(struct spi_master *master,
1237 struct spi_message *msg)
1238{
1239 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1240 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1241 struct omap2_mcspi_cs *cs;
1242
1243 /* Only a single channel can have the FORCE bit enabled
1244 * in its chconf0 register.
1245 * Scan all channels and disable them except the current one.
1246 * A FORCE can remain from a last transfer having cs_change enabled
1247 */
1248 list_for_each_entry(cs, &ctx->cs, node) {
1249 if (msg->spi->controller_state == cs)
1250 continue;
1251
1252 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
1253 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1254 writel_relaxed(cs->chconf0,
1255 cs->base + OMAP2_MCSPI_CHCONF0);
1256 readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
1257 }
1258 }
1259
1260 return 0;
1261}
1262
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001263static bool omap2_mcspi_can_dma(struct spi_master *master,
1264 struct spi_device *spi,
1265 struct spi_transfer *xfer)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001266{
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001267 return (xfer->len >= DMA_MIN_BYTES);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001268}
1269
Grant Likelyfd4a3192012-12-07 16:57:14 +00001270static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001271{
1272 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301273 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301274 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001275
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301276 ret = pm_runtime_get_sync(mcspi->dev);
Tony Lindgren5a686b22018-04-27 08:50:07 -07001277 if (ret < 0) {
1278 pm_runtime_put_noidle(mcspi->dev);
1279
Govindraj.R1f1a4382011-02-02 17:52:15 +05301280 return ret;
Tony Lindgren5a686b22018-04-27 08:50:07 -07001281 }
Jouni Hoganderddb22192009-07-29 15:02:11 -07001282
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301283 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001284 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301285 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001286
1287 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301288 pm_runtime_mark_last_busy(mcspi->dev);
1289 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001290 return 0;
1291}
1292
Tony Lindgren52e9a5b2018-04-25 07:08:43 -07001293/*
1294 * When SPI wake up from off-mode, CS is in activate state. If it was in
1295 * inactive state when driver was suspend, then force it to inactive state at
1296 * wake up.
1297 */
Govindraj.R1f1a4382011-02-02 17:52:15 +05301298static int omap_mcspi_runtime_resume(struct device *dev)
1299{
Tony Lindgren52e9a5b2018-04-25 07:08:43 -07001300 struct spi_master *master = dev_get_drvdata(dev);
1301 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1302 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1303 struct omap2_mcspi_cs *cs;
Govindraj.R1f1a4382011-02-02 17:52:15 +05301304
Tony Lindgren52e9a5b2018-04-25 07:08:43 -07001305 /* McSPI: context restore */
1306 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
1307 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
1308
1309 list_for_each_entry(cs, &ctx->cs, node) {
1310 /*
1311 * We need to toggle CS state for OMAP take this
1312 * change in account.
1313 */
1314 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1315 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1316 writel_relaxed(cs->chconf0,
1317 cs->base + OMAP2_MCSPI_CHCONF0);
1318 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1319 writel_relaxed(cs->chconf0,
1320 cs->base + OMAP2_MCSPI_CHCONF0);
1321 } else {
1322 writel_relaxed(cs->chconf0,
1323 cs->base + OMAP2_MCSPI_CHCONF0);
1324 }
1325 }
Govindraj.R1f1a4382011-02-02 17:52:15 +05301326
1327 return 0;
1328}
1329
Benoit Coussond5a80032012-02-15 18:37:34 +01001330static struct omap2_mcspi_platform_config omap2_pdata = {
1331 .regs_offset = 0,
1332};
1333
1334static struct omap2_mcspi_platform_config omap4_pdata = {
1335 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1336};
1337
1338static const struct of_device_id omap_mcspi_of_match[] = {
1339 {
1340 .compatible = "ti,omap2-mcspi",
1341 .data = &omap2_pdata,
1342 },
1343 {
1344 .compatible = "ti,omap4-mcspi",
1345 .data = &omap4_pdata,
1346 },
1347 { },
1348};
1349MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001350
Grant Likelyfd4a3192012-12-07 16:57:14 +00001351static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001352{
1353 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001354 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001355 struct omap2_mcspi *mcspi;
1356 struct resource *r;
1357 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001358 u32 regs_offset = 0;
Benoit Coussond5a80032012-02-15 18:37:34 +01001359 struct device_node *node = pdev->dev.of_node;
1360 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001361
1362 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1363 if (master == NULL) {
1364 dev_dbg(&pdev->dev, "master allocation failed\n");
1365 return -ENOMEM;
1366 }
1367
David Brownelle7db06b2009-06-17 16:26:04 -07001368 /* the spi->mode bits understood by this driver: */
1369 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001370 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001371 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001372 master->auto_runtime_pm = true;
Neil Armstrong468a3202015-10-09 15:47:41 +02001373 master->prepare_message = omap2_mcspi_prepare_message;
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001374 master->can_dma = omap2_mcspi_can_dma;
Michael Wellingb28cb942015-05-07 18:36:53 -05001375 master->transfer_one = omap2_mcspi_transfer_one;
Michael Wellingddcad7e2015-05-12 12:38:57 -05001376 master->set_cs = omap2_mcspi_set_cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001377 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001378 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001379 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1380 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001381
Jingoo Han24b5a822013-05-23 19:20:40 +09001382 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001383
1384 mcspi = spi_master_get_devdata(master);
1385 mcspi->master = master;
1386
Benoit Coussond5a80032012-02-15 18:37:34 +01001387 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1388 if (match) {
1389 u32 num_cs = 1; /* default number of chipselect */
1390 pdata = match->data;
1391
1392 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1393 master->num_chipselect = num_cs;
Daniel Mack2cd45172012-11-14 11:14:26 +08001394 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1395 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001396 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001397 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001398 master->num_chipselect = pdata->num_cs;
Daniel Mack0384e902012-10-07 18:19:44 +02001399 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001400 }
1401 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001402
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001403 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0ee5602013-01-21 11:09:18 +01001404 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1405 if (IS_ERR(mcspi->base)) {
1406 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301407 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001408 }
Vikram Naf9e53f2016-09-30 19:53:11 +05301409 mcspi->phys = r->start + regs_offset;
1410 mcspi->base += regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001411
Govindraj.R1f1a4382011-02-02 17:52:15 +05301412 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001413
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301414 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001415
Axel Lina6f936d2014-03-29 21:37:44 +08001416 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1417 sizeof(struct omap2_mcspi_dma),
1418 GFP_KERNEL);
1419 if (mcspi->dma_channels == NULL) {
1420 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301421 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001422 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001423
Charulatha V1a5d8192011-02-02 17:52:14 +05301424 for (i = 0; i < master->num_chipselect; i++) {
Peter Ujfalusib085c612016-04-29 16:11:56 +03001425 sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
1426 sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001427 }
1428
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301429 pm_runtime_use_autosuspend(&pdev->dev);
1430 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301431 pm_runtime_enable(&pdev->dev);
1432
Wei Yongjun142e07b2013-04-18 11:14:59 +08001433 status = omap2_mcspi_master_setup(mcspi);
1434 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301435 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001436
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001437 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001438 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301439 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001440
1441 return status;
1442
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301443disable_pm:
Tony Lindgren0e6f3572016-02-10 15:02:46 -08001444 pm_runtime_dont_use_autosuspend(&pdev->dev);
1445 pm_runtime_put_sync(&pdev->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301446 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301447free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301448 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001449 return status;
1450}
1451
Grant Likelyfd4a3192012-12-07 16:57:14 +00001452static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001453{
Axel Lina6f936d2014-03-29 21:37:44 +08001454 struct spi_master *master = platform_get_drvdata(pdev);
1455 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001456
Tony Lindgren0e6f3572016-02-10 15:02:46 -08001457 pm_runtime_dont_use_autosuspend(mcspi->dev);
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301458 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301459 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001460
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001461 return 0;
1462}
1463
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001464/* work with hotplug and coldplug */
1465MODULE_ALIAS("platform:omap2_mcspi");
1466
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001467#ifdef CONFIG_SUSPEND
Tony Lindgren5a686b22018-04-27 08:50:07 -07001468static int omap2_mcspi_suspend_noirq(struct device *dev)
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001469{
Pascal Huerstbeca3652015-11-19 16:18:28 +01001470 return pinctrl_pm_select_sleep_state(dev);
1471}
1472
Tony Lindgren5a686b22018-04-27 08:50:07 -07001473static int omap2_mcspi_resume_noirq(struct device *dev)
1474{
1475 struct spi_master *master = dev_get_drvdata(dev);
1476 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1477 int error;
1478
1479 error = pinctrl_pm_select_default_state(dev);
1480 if (error)
1481 dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
1482 __func__, error);
1483
Tony Lindgren5a686b22018-04-27 08:50:07 -07001484 return 0;
1485}
1486
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001487#else
Tony Lindgren5a686b22018-04-27 08:50:07 -07001488#define omap2_mcspi_suspend_noirq NULL
1489#define omap2_mcspi_resume_noirq NULL
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001490#endif
1491
1492static const struct dev_pm_ops omap2_mcspi_pm_ops = {
Tony Lindgren5a686b22018-04-27 08:50:07 -07001493 .suspend_noirq = omap2_mcspi_suspend_noirq,
1494 .resume_noirq = omap2_mcspi_resume_noirq,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301495 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001496};
1497
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001498static struct platform_driver omap2_mcspi_driver = {
1499 .driver = {
1500 .name = "omap2_mcspi",
Benoit Coussond5a80032012-02-15 18:37:34 +01001501 .pm = &omap2_mcspi_pm_ops,
1502 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001503 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001504 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001505 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001506};
1507
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001508module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001509MODULE_LICENSE("GPL");