blob: 985f00d8a9649d5bb9a692b8b1b17075e5149159 [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>
Vignesh R13d515c2018-10-15 12:08:27 +053036#include <linux/iopoll.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070037
38#include <linux/spi/spi.h>
Michael Wellingbc7f9bb2015-05-08 13:31:01 -050039#include <linux/gpio.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070040
Arnd Bergmann22037472012-08-24 15:21:06 +020041#include <linux/platform_data/spi-omap2-mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070042
43#define OMAP2_MCSPI_MAX_FREQ 48000000
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010044#define OMAP2_MCSPI_MAX_DIVIDER 4096
Illia Smyrnovd33f4732013-06-17 16:31:06 +030045#define OMAP2_MCSPI_MAX_FIFODEPTH 64
46#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
Shubhrajyoti D27b52842012-03-26 17:04:22 +053047#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070048
49#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070050#define OMAP2_MCSPI_SYSSTATUS 0x14
51#define OMAP2_MCSPI_IRQSTATUS 0x18
52#define OMAP2_MCSPI_IRQENABLE 0x1c
53#define OMAP2_MCSPI_WAKEUPENABLE 0x20
54#define OMAP2_MCSPI_SYST 0x24
55#define OMAP2_MCSPI_MODULCTRL 0x28
Illia Smyrnovd33f4732013-06-17 16:31:06 +030056#define OMAP2_MCSPI_XFERLEVEL 0x7c
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070057
58/* per-channel banks, 0x14 bytes each, first is: */
59#define OMAP2_MCSPI_CHCONF0 0x2c
60#define OMAP2_MCSPI_CHSTAT0 0x30
61#define OMAP2_MCSPI_CHCTRL0 0x34
62#define OMAP2_MCSPI_TX0 0x38
63#define OMAP2_MCSPI_RX0 0x3c
64
65/* per-register bitmasks: */
Illia Smyrnovd33f4732013-06-17 16:31:06 +030066#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070067
Jouni Hogander7a8fa722009-09-22 16:45:58 -070068#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
69#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
70#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070071
Jouni Hogander7a8fa722009-09-22 16:45:58 -070072#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
73#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070074#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070075#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070076#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070077#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
78#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070079#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070080#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
81#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
82#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
83#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
84#define OMAP2_MCSPI_CHCONF_IS BIT(18)
85#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
86#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030087#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
88#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010089#define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070090
Jouni Hogander7a8fa722009-09-22 16:45:58 -070091#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
92#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
93#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030094#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070095
Jouni Hogander7a8fa722009-09-22 16:45:58 -070096#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010097#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070098
Jouni Hogander7a8fa722009-09-22 16:45:58 -070099#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700100
101/* We have 2 DMA channels per CS, one for RX and one for TX */
102struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +0100103 struct dma_chan *dma_tx;
104 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700105
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700106 struct completion dma_tx_completion;
107 struct completion dma_rx_completion;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530108
109 char dma_rx_ch_name[14];
110 char dma_tx_ch_name[14];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700111};
112
113/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
114 * cache operations; better heuristics consider wordsize and bitrate.
115 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000116#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700117
118
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530119/*
120 * Used for context save and restore, structure members to be updated whenever
121 * corresponding registers are modified.
122 */
123struct omap2_mcspi_regs {
124 u32 modulctrl;
125 u32 wakeupenable;
126 struct list_head cs;
127};
128
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700129struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700130 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700131 /* Virtual base address of the controller */
132 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100133 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700134 /* SPI1 has 4 channels, while SPI2 has 2 */
135 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530136 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530137 struct omap2_mcspi_regs ctx;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300138 int fifo_depth;
Daniel Mack0384e902012-10-07 18:19:44 +0200139 unsigned int pin_dir:1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700140};
141
142struct omap2_mcspi_cs {
143 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100144 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700145 int word_len;
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700146 u16 mode;
Tero Kristo89c05372009-09-22 16:46:17 -0700147 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700148 /* Context save and restore shadow register */
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100149 u32 chconf0, chctrl0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700150};
151
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700152static inline void mcspi_write_reg(struct spi_master *master,
153 int idx, u32 val)
154{
155 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
156
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200157 writel_relaxed(val, mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700158}
159
160static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
161{
162 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
163
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200164 return readl_relaxed(mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700165}
166
167static inline void mcspi_write_cs_reg(const struct spi_device *spi,
168 int idx, u32 val)
169{
170 struct omap2_mcspi_cs *cs = spi->controller_state;
171
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200172 writel_relaxed(val, cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700173}
174
175static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
176{
177 struct omap2_mcspi_cs *cs = spi->controller_state;
178
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200179 return readl_relaxed(cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700180}
181
Hemanth Va41ae1a2009-09-22 16:46:16 -0700182static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
183{
184 struct omap2_mcspi_cs *cs = spi->controller_state;
185
186 return cs->chconf0;
187}
188
189static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
190{
191 struct omap2_mcspi_cs *cs = spi->controller_state;
192
193 cs->chconf0 = val;
194 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000195 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700196}
197
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300198static inline int mcspi_bytes_per_word(int word_len)
199{
200 if (word_len <= 8)
201 return 1;
202 else if (word_len <= 16)
203 return 2;
204 else /* word_len <= 32 */
205 return 4;
206}
207
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700208static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
209 int is_read, int enable)
210{
211 u32 l, rw;
212
Hemanth Va41ae1a2009-09-22 16:46:16 -0700213 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700214
215 if (is_read) /* 1 is read, 0 write */
216 rw = OMAP2_MCSPI_CHCONF_DMAR;
217 else
218 rw = OMAP2_MCSPI_CHCONF_DMAW;
219
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530220 if (enable)
221 l |= rw;
222 else
223 l &= ~rw;
224
Hemanth Va41ae1a2009-09-22 16:46:16 -0700225 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700226}
227
228static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
229{
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100230 struct omap2_mcspi_cs *cs = spi->controller_state;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700231 u32 l;
232
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100233 l = cs->chctrl0;
234 if (enable)
235 l |= OMAP2_MCSPI_CHCTRL_EN;
236 else
237 l &= ~OMAP2_MCSPI_CHCTRL_EN;
238 cs->chctrl0 = l;
239 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000240 /* Flash post-writes */
241 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700242}
243
Michael Wellingddcad7e2015-05-12 12:38:57 -0500244static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700245{
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200246 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700247 u32 l;
248
Michael Welling4373f8b2015-05-23 21:13:43 -0500249 /* The controller handles the inverted chip selects
250 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
251 * the inversion from the core spi_set_cs function.
252 */
253 if (spi->mode & SPI_CS_HIGH)
254 enable = !enable;
255
Michael Wellingddcad7e2015-05-12 12:38:57 -0500256 if (spi->controller_state) {
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200257 int err = pm_runtime_get_sync(mcspi->dev);
258 if (err < 0) {
Tony Lindgren5a686b22018-04-27 08:50:07 -0700259 pm_runtime_put_noidle(mcspi->dev);
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200260 dev_err(mcspi->dev, "failed to get sync: %d\n", err);
261 return;
262 }
263
Michael Wellingddcad7e2015-05-12 12:38:57 -0500264 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530265
Michael Wellingddcad7e2015-05-12 12:38:57 -0500266 if (enable)
267 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
268 else
269 l |= OMAP2_MCSPI_CHCONF_FORCE;
270
271 mcspi_write_chconf0(spi, l);
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200272
273 pm_runtime_mark_last_busy(mcspi->dev);
274 pm_runtime_put_autosuspend(mcspi->dev);
Michael Wellingddcad7e2015-05-12 12:38:57 -0500275 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700276}
277
278static void omap2_mcspi_set_master_mode(struct spi_master *master)
279{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530280 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
281 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700282 u32 l;
283
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530284 /*
285 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700286 * to single-channel master mode
287 */
288 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530289 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
290 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700291 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700292
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530293 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700294}
295
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300296static void omap2_mcspi_set_fifo(const struct spi_device *spi,
297 struct spi_transfer *t, int enable)
298{
299 struct spi_master *master = spi->master;
300 struct omap2_mcspi_cs *cs = spi->controller_state;
301 struct omap2_mcspi *mcspi;
302 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300303 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300304 u32 chconf, xferlevel;
305
306 mcspi = spi_master_get_devdata(master);
307
308 chconf = mcspi_cached_chconf0(spi);
309 if (enable) {
310 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
311 if (t->len % bytes_per_word != 0)
312 goto disable_fifo;
313
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300314 if (t->rx_buf != NULL && t->tx_buf != NULL)
315 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
316 else
317 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
318
319 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300320 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
321 goto disable_fifo;
322
323 wcnt = t->len / bytes_per_word;
324 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
325 goto disable_fifo;
326
327 xferlevel = wcnt << 16;
328 if (t->rx_buf != NULL) {
329 chconf |= OMAP2_MCSPI_CHCONF_FFER;
330 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300331 }
332 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300333 chconf |= OMAP2_MCSPI_CHCONF_FFET;
334 xferlevel |= fifo_depth - 1;
335 }
336
337 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
338 mcspi_write_chconf0(spi, chconf);
339 mcspi->fifo_depth = fifo_depth;
340
341 return;
342 }
343
344disable_fifo:
345 if (t->rx_buf != NULL)
346 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500347
348 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300349 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
350
351 mcspi_write_chconf0(spi, chconf);
352 mcspi->fifo_depth = 0;
353}
354
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300355static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
356{
Vignesh R13d515c2018-10-15 12:08:27 +0530357 u32 val;
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300358
Vignesh R13d515c2018-10-15 12:08:27 +0530359 return readl_poll_timeout(reg, val, val & bit, 1, MSEC_PER_SEC);
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300360}
361
Russell King53741ed2012-04-23 13:51:48 +0100362static void omap2_mcspi_rx_callback(void *data)
363{
364 struct spi_device *spi = data;
365 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
366 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
367
Russell King53741ed2012-04-23 13:51:48 +0100368 /* We must disable the DMA RX request */
369 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200370
371 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100372}
373
374static void omap2_mcspi_tx_callback(void *data)
375{
376 struct spi_device *spi = data;
377 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
378 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
379
Russell King53741ed2012-04-23 13:51:48 +0100380 /* We must disable the DMA TX request */
381 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200382
383 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100384}
385
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530386static void omap2_mcspi_tx_dma(struct spi_device *spi,
387 struct spi_transfer *xfer,
388 struct dma_slave_config cfg)
389{
390 struct omap2_mcspi *mcspi;
391 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530392
393 mcspi = spi_master_get_devdata(spi->master);
394 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530395
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530396 if (mcspi_dma->dma_tx) {
397 struct dma_async_tx_descriptor *tx;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530398
399 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
400
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500401 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
402 xfer->tx_sg.nents,
403 DMA_MEM_TO_DEV,
404 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530405 if (tx) {
406 tx->callback = omap2_mcspi_tx_callback;
407 tx->callback_param = spi;
408 dmaengine_submit(tx);
409 } else {
410 /* FIXME: fall back to PIO? */
411 }
412 }
413 dma_async_issue_pending(mcspi_dma->dma_tx);
414 omap2_mcspi_set_dma_req(spi, 0, 1);
415
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530416}
417
418static unsigned
419omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
420 struct dma_slave_config cfg,
421 unsigned es)
422{
423 struct omap2_mcspi *mcspi;
424 struct omap2_mcspi_dma *mcspi_dma;
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500425 unsigned int count, transfer_reduction = 0;
426 struct scatterlist *sg_out[2];
427 int nb_sizes = 0, out_mapped_nents[2], ret, x;
428 size_t sizes[2];
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530429 u32 l;
430 int elements = 0;
431 int word_len, element_count;
432 struct omap2_mcspi_cs *cs = spi->controller_state;
Akinobu Mita81261352017-03-22 09:18:26 +0900433 void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
434
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530435 mcspi = spi_master_get_devdata(spi->master);
436 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
437 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300438
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500439 /*
440 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
441 * it mentions reducing DMA transfer length by one element in master
442 * normal mode.
443 */
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300444 if (mcspi->fifo_depth == 0)
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500445 transfer_reduction = es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300446
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530447 word_len = cs->word_len;
448 l = mcspi_cached_chconf0(spi);
449
450 if (word_len <= 8)
451 element_count = count;
452 else if (word_len <= 16)
453 element_count = count >> 1;
454 else /* word_len <= 32 */
455 element_count = count >> 2;
456
457 if (mcspi_dma->dma_rx) {
458 struct dma_async_tx_descriptor *tx;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530459
460 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
461
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500462 /*
463 * Reduce DMA transfer length by one more if McSPI is
464 * configured in turbo mode.
465 */
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300466 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500467 transfer_reduction += es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530468
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500469 if (transfer_reduction) {
470 /* Split sgl into two. The second sgl won't be used. */
471 sizes[0] = count - transfer_reduction;
472 sizes[1] = transfer_reduction;
473 nb_sizes = 2;
474 } else {
475 /*
476 * Don't bother splitting the sgl. This essentially
477 * clones the original sgl.
478 */
479 sizes[0] = count;
480 nb_sizes = 1;
481 }
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530482
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500483 ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents,
484 0, nb_sizes,
485 sizes,
486 sg_out, out_mapped_nents,
487 GFP_KERNEL);
488
489 if (ret < 0) {
490 dev_err(&spi->dev, "sg_split failed\n");
491 return 0;
492 }
493
494 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx,
495 sg_out[0],
496 out_mapped_nents[0],
497 DMA_DEV_TO_MEM,
498 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530499 if (tx) {
500 tx->callback = omap2_mcspi_rx_callback;
501 tx->callback_param = spi;
502 dmaengine_submit(tx);
503 } else {
504 /* FIXME: fall back to PIO? */
505 }
506 }
507
508 dma_async_issue_pending(mcspi_dma->dma_rx);
509 omap2_mcspi_set_dma_req(spi, 1, 1);
510
511 wait_for_completion(&mcspi_dma->dma_rx_completion);
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -0500512
513 for (x = 0; x < nb_sizes; x++)
514 kfree(sg_out[x]);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300515
516 if (mcspi->fifo_depth > 0)
517 return count;
518
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500519 /*
520 * Due to the DMA transfer length reduction the missing bytes must
521 * be read manually to receive all of the expected data.
522 */
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530523 omap2_mcspi_set_enable(spi, 0);
524
525 elements = element_count - 1;
526
527 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
528 elements--;
529
Akinobu Mita81261352017-03-22 09:18:26 +0900530 if (!mcspi_wait_for_reg_bit(chstat_reg,
531 OMAP2_MCSPI_CHSTAT_RXS)) {
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530532 u32 w;
533
534 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
535 if (word_len <= 8)
536 ((u8 *)xfer->rx_buf)[elements++] = w;
537 else if (word_len <= 16)
538 ((u16 *)xfer->rx_buf)[elements++] = w;
539 else /* word_len <= 32 */
540 ((u32 *)xfer->rx_buf)[elements++] = w;
541 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300542 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300543 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300544 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530545 omap2_mcspi_set_enable(spi, 1);
546 return count;
547 }
548 }
Akinobu Mita81261352017-03-22 09:18:26 +0900549 if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530550 u32 w;
551
552 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
553 if (word_len <= 8)
554 ((u8 *)xfer->rx_buf)[elements] = w;
555 else if (word_len <= 16)
556 ((u16 *)xfer->rx_buf)[elements] = w;
557 else /* word_len <= 32 */
558 ((u32 *)xfer->rx_buf)[elements] = w;
559 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300560 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300561 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530562 }
563 omap2_mcspi_set_enable(spi, 1);
564 return count;
565}
566
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700567static unsigned
568omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
569{
570 struct omap2_mcspi *mcspi;
571 struct omap2_mcspi_cs *cs = spi->controller_state;
572 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100573 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530574 u8 *rx;
575 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100576 struct dma_slave_config cfg;
577 enum dma_slave_buswidth width;
578 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300579 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530580 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300581 void __iomem *irqstat_reg;
582 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700583
584 mcspi = spi_master_get_devdata(spi->master);
585 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300586
Russell King53741ed2012-04-23 13:51:48 +0100587 if (cs->word_len <= 8) {
588 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
589 es = 1;
590 } else if (cs->word_len <= 16) {
591 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
592 es = 2;
593 } else {
594 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
595 es = 4;
596 }
597
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300598 count = xfer->len;
599 burst = 1;
600
601 if (mcspi->fifo_depth > 0) {
602 if (count > mcspi->fifo_depth)
603 burst = mcspi->fifo_depth / es;
604 else
605 burst = count / es;
606 }
607
Russell King53741ed2012-04-23 13:51:48 +0100608 memset(&cfg, 0, sizeof(cfg));
609 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
610 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
611 cfg.src_addr_width = width;
612 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300613 cfg.src_maxburst = burst;
614 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100615
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700616 rx = xfer->rx_buf;
617 tx = xfer->tx_buf;
618
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530619 if (tx != NULL)
620 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700621
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530622 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530623 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700624
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530625 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530626 wait_for_completion(&mcspi_dma->dma_tx_completion);
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530627
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300628 if (mcspi->fifo_depth > 0) {
629 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
630
631 if (mcspi_wait_for_reg_bit(irqstat_reg,
632 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
633 dev_err(&spi->dev, "EOW timed out\n");
634
635 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
636 OMAP2_MCSPI_IRQSTATUS_EOW);
637 }
638
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530639 /* for TX_ONLY mode, be sure all words have shifted out */
640 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300641 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
642 if (mcspi->fifo_depth > 0) {
643 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
644 OMAP2_MCSPI_CHSTAT_TXFFE);
645 if (wait_res < 0)
646 dev_err(&spi->dev, "TXFFE timed out\n");
647 } else {
648 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
649 OMAP2_MCSPI_CHSTAT_TXS);
650 if (wait_res < 0)
651 dev_err(&spi->dev, "TXS timed out\n");
652 }
653 if (wait_res >= 0 &&
654 (mcspi_wait_for_reg_bit(chstat_reg,
655 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530656 dev_err(&spi->dev, "EOT timed out\n");
657 }
658 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700659 return count;
660}
661
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700662static unsigned
663omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
664{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700665 struct omap2_mcspi_cs *cs = spi->controller_state;
666 unsigned int count, c;
667 u32 l;
668 void __iomem *base = cs->base;
669 void __iomem *tx_reg;
670 void __iomem *rx_reg;
671 void __iomem *chstat_reg;
672 int word_len;
673
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700674 count = xfer->len;
675 c = count;
676 word_len = cs->word_len;
677
Hemanth Va41ae1a2009-09-22 16:46:16 -0700678 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700679
680 /* We store the pre-calculated register addresses on stack to speed
681 * up the transfer loop. */
682 tx_reg = base + OMAP2_MCSPI_TX0;
683 rx_reg = base + OMAP2_MCSPI_RX0;
684 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
685
Michael Jonesadef6582011-02-25 16:55:11 +0100686 if (c < (word_len>>3))
687 return 0;
688
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700689 if (word_len <= 8) {
690 u8 *rx;
691 const u8 *tx;
692
693 rx = xfer->rx_buf;
694 tx = xfer->tx_buf;
695
696 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800697 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700698 if (tx != NULL) {
699 if (mcspi_wait_for_reg_bit(chstat_reg,
700 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
701 dev_err(&spi->dev, "TXS timed out\n");
702 goto out;
703 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900704 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700705 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200706 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700707 }
708 if (rx != NULL) {
709 if (mcspi_wait_for_reg_bit(chstat_reg,
710 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
711 dev_err(&spi->dev, "RXS timed out\n");
712 goto out;
713 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000714
715 if (c == 1 && tx == NULL &&
716 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
717 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200718 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900719 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000720 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000721 if (mcspi_wait_for_reg_bit(chstat_reg,
722 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
723 dev_err(&spi->dev,
724 "RXS timed out\n");
725 goto out;
726 }
727 c = 0;
728 } else if (c == 0 && tx == NULL) {
729 omap2_mcspi_set_enable(spi, 0);
730 }
731
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200732 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900733 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700734 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700735 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200736 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700737 } else if (word_len <= 16) {
738 u16 *rx;
739 const u16 *tx;
740
741 rx = xfer->rx_buf;
742 tx = xfer->tx_buf;
743 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800744 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700745 if (tx != NULL) {
746 if (mcspi_wait_for_reg_bit(chstat_reg,
747 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
748 dev_err(&spi->dev, "TXS timed out\n");
749 goto out;
750 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900751 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700752 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200753 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700754 }
755 if (rx != NULL) {
756 if (mcspi_wait_for_reg_bit(chstat_reg,
757 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
758 dev_err(&spi->dev, "RXS timed out\n");
759 goto out;
760 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000761
762 if (c == 2 && tx == NULL &&
763 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
764 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200765 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900766 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000767 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000768 if (mcspi_wait_for_reg_bit(chstat_reg,
769 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
770 dev_err(&spi->dev,
771 "RXS timed out\n");
772 goto out;
773 }
774 c = 0;
775 } else if (c == 0 && tx == NULL) {
776 omap2_mcspi_set_enable(spi, 0);
777 }
778
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200779 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900780 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700781 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700782 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200783 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700784 } else if (word_len <= 32) {
785 u32 *rx;
786 const u32 *tx;
787
788 rx = xfer->rx_buf;
789 tx = xfer->tx_buf;
790 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800791 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700792 if (tx != NULL) {
793 if (mcspi_wait_for_reg_bit(chstat_reg,
794 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
795 dev_err(&spi->dev, "TXS timed out\n");
796 goto out;
797 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900798 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700799 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200800 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700801 }
802 if (rx != NULL) {
803 if (mcspi_wait_for_reg_bit(chstat_reg,
804 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
805 dev_err(&spi->dev, "RXS timed out\n");
806 goto out;
807 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000808
809 if (c == 4 && tx == NULL &&
810 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
811 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200812 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900813 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000814 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000815 if (mcspi_wait_for_reg_bit(chstat_reg,
816 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
817 dev_err(&spi->dev,
818 "RXS timed out\n");
819 goto out;
820 }
821 c = 0;
822 } else if (c == 0 && tx == NULL) {
823 omap2_mcspi_set_enable(spi, 0);
824 }
825
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200826 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900827 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700828 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700829 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200830 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700831 }
832
833 /* for TX_ONLY mode, be sure all words have shifted out */
834 if (xfer->rx_buf == NULL) {
835 if (mcspi_wait_for_reg_bit(chstat_reg,
836 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
837 dev_err(&spi->dev, "TXS timed out\n");
838 } else if (mcspi_wait_for_reg_bit(chstat_reg,
839 OMAP2_MCSPI_CHSTAT_EOT) < 0)
840 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed62010-10-19 18:03:27 +0800841
842 /* disable chan to purge rx datas received in TX_ONLY transfer,
843 * otherwise these rx datas will affect the direct following
844 * RX_ONLY transfer.
845 */
846 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700847 }
848out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000849 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700850 return count - c;
851}
852
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200853static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
854{
855 u32 div;
856
857 for (div = 0; div < 15; div++)
858 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
859 return div;
860
861 return 15;
862}
863
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700864/* called only when no transfer is active to this device */
865static int omap2_mcspi_setup_transfer(struct spi_device *spi,
866 struct spi_transfer *t)
867{
868 struct omap2_mcspi_cs *cs = spi->controller_state;
869 struct omap2_mcspi *mcspi;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100870 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700871 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700872 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700873
874 mcspi = spi_master_get_devdata(spi->master);
875
876 if (t != NULL && t->bits_per_word)
877 word_len = t->bits_per_word;
878
879 cs->word_len = word_len;
880
Scott Ellis9bd45172010-03-10 14:23:13 -0700881 if (t && t->speed_hz)
882 speed_hz = t->speed_hz;
883
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200884 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100885 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
886 clkd = omap2_mcspi_calc_divisor(speed_hz);
887 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
888 clkg = 0;
889 } else {
890 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
891 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
892 clkd = (div - 1) & 0xf;
893 extclk = (div - 1) >> 4;
894 clkg = OMAP2_MCSPI_CHCONF_CLKG;
895 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700896
Hemanth Va41ae1a2009-09-22 16:46:16 -0700897 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700898
899 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
900 * REVISIT: this controller could support SPI_3WIRE mode.
901 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800902 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200903 l &= ~OMAP2_MCSPI_CHCONF_IS;
904 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
905 l |= OMAP2_MCSPI_CHCONF_DPE0;
906 } else {
907 l |= OMAP2_MCSPI_CHCONF_IS;
908 l |= OMAP2_MCSPI_CHCONF_DPE1;
909 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
910 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700911
912 /* wordlength */
913 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
914 l |= (word_len - 1) << 7;
915
916 /* set chipselect polarity; manage with FORCE */
917 if (!(spi->mode & SPI_CS_HIGH))
918 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
919 else
920 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
921
922 /* set clock divisor */
923 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100924 l |= clkd << 2;
925
926 /* set clock granularity */
927 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
928 l |= clkg;
929 if (clkg) {
930 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
931 cs->chctrl0 |= extclk << 8;
932 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
933 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700934
935 /* set SPI mode 0..3 */
936 if (spi->mode & SPI_CPOL)
937 l |= OMAP2_MCSPI_CHCONF_POL;
938 else
939 l &= ~OMAP2_MCSPI_CHCONF_POL;
940 if (spi->mode & SPI_CPHA)
941 l |= OMAP2_MCSPI_CHCONF_PHA;
942 else
943 l &= ~OMAP2_MCSPI_CHCONF_PHA;
944
Hemanth Va41ae1a2009-09-22 16:46:16 -0700945 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700946
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700947 cs->mode = spi->mode;
948
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700949 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100950 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700951 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
952 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
953
954 return 0;
955}
956
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700957/*
958 * Note that we currently allow DMA only if we get a channel
959 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
960 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700961static int omap2_mcspi_request_dma(struct spi_device *spi)
962{
963 struct spi_master *master = spi->master;
964 struct omap2_mcspi *mcspi;
965 struct omap2_mcspi_dma *mcspi_dma;
Peter Ujfalusib085c612016-04-29 16:11:56 +0300966 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700967
968 mcspi = spi_master_get_devdata(master);
969 mcspi_dma = mcspi->dma_channels + spi->chip_select;
970
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700971 init_completion(&mcspi_dma->dma_rx_completion);
972 init_completion(&mcspi_dma->dma_tx_completion);
973
Peter Ujfalusib085c612016-04-29 16:11:56 +0300974 mcspi_dma->dma_rx = dma_request_chan(&master->dev,
975 mcspi_dma->dma_rx_ch_name);
976 if (IS_ERR(mcspi_dma->dma_rx)) {
977 ret = PTR_ERR(mcspi_dma->dma_rx);
Russell King53741ed2012-04-23 13:51:48 +0100978 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700979 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100980 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700981
Peter Ujfalusib085c612016-04-29 16:11:56 +0300982 mcspi_dma->dma_tx = dma_request_chan(&master->dev,
983 mcspi_dma->dma_tx_ch_name);
984 if (IS_ERR(mcspi_dma->dma_tx)) {
985 ret = PTR_ERR(mcspi_dma->dma_tx);
986 mcspi_dma->dma_tx = NULL;
987 dma_release_channel(mcspi_dma->dma_rx);
988 mcspi_dma->dma_rx = NULL;
989 }
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700990
991no_dma:
Peter Ujfalusib085c612016-04-29 16:11:56 +0300992 return ret;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700993}
994
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700995static int omap2_mcspi_setup(struct spi_device *spi)
996{
997 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530998 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
999 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001000 struct omap2_mcspi_dma *mcspi_dma;
1001 struct omap2_mcspi_cs *cs = spi->controller_state;
1002
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001003 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1004
1005 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +01001006 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001007 if (!cs)
1008 return -ENOMEM;
1009 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +01001010 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001011 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001012 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001013 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001014 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001015 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301016 list_add_tail(&cs->node, &ctx->cs);
Michael Welling2f538c02015-11-30 09:02:39 -06001017
1018 if (gpio_is_valid(spi->cs_gpio)) {
1019 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
1020 if (ret) {
1021 dev_err(&spi->dev, "failed to request gpio\n");
1022 return ret;
1023 }
1024 gpio_direction_output(spi->cs_gpio,
1025 !(spi->mode & SPI_CS_HIGH));
1026 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001027 }
1028
Russell King8c7494a2012-04-23 13:56:25 +01001029 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001030 ret = omap2_mcspi_request_dma(spi);
Peter Ujfalusib085c612016-04-29 16:11:56 +03001031 if (ret)
1032 dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n",
1033 ret);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001034 }
1035
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301036 ret = pm_runtime_get_sync(mcspi->dev);
Tony Lindgren5a686b22018-04-27 08:50:07 -07001037 if (ret < 0) {
1038 pm_runtime_put_noidle(mcspi->dev);
1039
Govindraj.R1f1a4382011-02-02 17:52:15 +05301040 return ret;
Tony Lindgren5a686b22018-04-27 08:50:07 -07001041 }
Hemanth Va41ae1a2009-09-22 16:46:16 -07001042
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001043 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301044 pm_runtime_mark_last_busy(mcspi->dev);
1045 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001046
1047 return ret;
1048}
1049
1050static void omap2_mcspi_cleanup(struct spi_device *spi)
1051{
1052 struct omap2_mcspi *mcspi;
1053 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001054 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001055
1056 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001057
Scott Ellis5e774942010-03-10 14:22:45 -07001058 if (spi->controller_state) {
1059 /* Unlink controller state from context save list */
1060 cs = spi->controller_state;
1061 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001062
Russell King10aa5a32012-06-18 11:27:04 +01001063 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001064 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001065
Scott Ellis99f1a432010-05-24 14:20:27 +00001066 if (spi->chip_select < spi->master->num_chipselect) {
1067 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1068
Russell King53741ed2012-04-23 13:51:48 +01001069 if (mcspi_dma->dma_rx) {
1070 dma_release_channel(mcspi_dma->dma_rx);
1071 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001072 }
Russell King53741ed2012-04-23 13:51:48 +01001073 if (mcspi_dma->dma_tx) {
1074 dma_release_channel(mcspi_dma->dma_tx);
1075 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001076 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001077 }
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001078
1079 if (gpio_is_valid(spi->cs_gpio))
1080 gpio_free(spi->cs_gpio);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001081}
1082
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001083static int omap2_mcspi_transfer_one(struct spi_master *master,
1084 struct spi_device *spi,
1085 struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001086{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001087
1088 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301089 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001090 * arbitrate among multiple channels. This corresponds to "single
1091 * channel" master mode. As a side effect, we need to manage the
1092 * chipselect with the FORCE bit ... CS != channel enable.
1093 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001094
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001095 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001096 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301097 struct omap2_mcspi_cs *cs;
1098 struct omap2_mcspi_device_config *cd;
1099 int par_override = 0;
1100 int status = 0;
1101 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001102
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001103 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001104 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301105 cs = spi->controller_state;
1106 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001107
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001108 /*
1109 * The slave driver could have changed spi->mode in which case
1110 * it will be different from cs->mode (the current hardware setup).
1111 * If so, set par_override (even though its not a parity issue) so
1112 * omap2_mcspi_setup_transfer will be called to configure the hardware
1113 * with the correct mode on the first iteration of the loop below.
1114 */
1115 if (spi->mode != cs->mode)
1116 par_override = 1;
1117
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001118 omap2_mcspi_set_enable(spi, 0);
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001119
Michael Wellinga06b4302015-05-23 21:13:44 -05001120 if (gpio_is_valid(spi->cs_gpio))
1121 omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
1122
Michael Wellingb28cb942015-05-07 18:36:53 -05001123 if (par_override ||
1124 (t->speed_hz != spi->max_speed_hz) ||
1125 (t->bits_per_word != spi->bits_per_word)) {
1126 par_override = 1;
1127 status = omap2_mcspi_setup_transfer(spi, t);
1128 if (status < 0)
1129 goto out;
1130 if (t->speed_hz == spi->max_speed_hz &&
1131 t->bits_per_word == spi->bits_per_word)
1132 par_override = 0;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301133 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001134 if (cd && cd->cs_per_word) {
1135 chconf = mcspi->ctx.modulctrl;
1136 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1137 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1138 mcspi->ctx.modulctrl =
1139 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1140 }
1141
Michael Wellingb28cb942015-05-07 18:36:53 -05001142 chconf = mcspi_cached_chconf0(spi);
1143 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1144 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1145
1146 if (t->tx_buf == NULL)
1147 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1148 else if (t->rx_buf == NULL)
1149 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1150
1151 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1152 /* Turbo mode is for more than one word */
1153 if (t->len > ((cs->word_len + 7) >> 3))
1154 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1155 }
1156
1157 mcspi_write_chconf0(spi, chconf);
1158
1159 if (t->len) {
1160 unsigned count;
1161
1162 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001163 master->cur_msg_mapped &&
1164 master->can_dma(master, spi, t))
Michael Wellingb28cb942015-05-07 18:36:53 -05001165 omap2_mcspi_set_fifo(spi, t, 1);
1166
1167 omap2_mcspi_set_enable(spi, 1);
1168
1169 /* RX_ONLY mode needs dummy data in TX reg */
1170 if (t->tx_buf == NULL)
1171 writel_relaxed(0, cs->base
1172 + OMAP2_MCSPI_TX0);
1173
1174 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001175 master->cur_msg_mapped &&
1176 master->can_dma(master, spi, t))
Michael Wellingb28cb942015-05-07 18:36:53 -05001177 count = omap2_mcspi_txrx_dma(spi, t);
1178 else
1179 count = omap2_mcspi_txrx_pio(spi, t);
1180
1181 if (count != t->len) {
1182 status = -EIO;
1183 goto out;
1184 }
1185 }
1186
Michael Wellingb28cb942015-05-07 18:36:53 -05001187 omap2_mcspi_set_enable(spi, 0);
1188
1189 if (mcspi->fifo_depth > 0)
1190 omap2_mcspi_set_fifo(spi, t, 0);
1191
1192out:
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301193 /* Restore defaults if they were overriden */
1194 if (par_override) {
1195 par_override = 0;
1196 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001197 }
1198
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001199 if (cd && cd->cs_per_word) {
1200 chconf = mcspi->ctx.modulctrl;
1201 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1202 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1203 mcspi->ctx.modulctrl =
1204 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1205 }
1206
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301207 omap2_mcspi_set_enable(spi, 0);
1208
Michael Wellinga06b4302015-05-23 21:13:44 -05001209 if (gpio_is_valid(spi->cs_gpio))
1210 omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
1211
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001212 if (mcspi->fifo_depth > 0 && t)
1213 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301214
Michael Wellingb28cb942015-05-07 18:36:53 -05001215 return status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001216}
1217
Neil Armstrong468a3202015-10-09 15:47:41 +02001218static int omap2_mcspi_prepare_message(struct spi_master *master,
1219 struct spi_message *msg)
1220{
1221 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1222 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1223 struct omap2_mcspi_cs *cs;
1224
1225 /* Only a single channel can have the FORCE bit enabled
1226 * in its chconf0 register.
1227 * Scan all channels and disable them except the current one.
1228 * A FORCE can remain from a last transfer having cs_change enabled
1229 */
1230 list_for_each_entry(cs, &ctx->cs, node) {
1231 if (msg->spi->controller_state == cs)
1232 continue;
1233
1234 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
1235 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1236 writel_relaxed(cs->chconf0,
1237 cs->base + OMAP2_MCSPI_CHCONF0);
1238 readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
1239 }
1240 }
1241
1242 return 0;
1243}
1244
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001245static bool omap2_mcspi_can_dma(struct spi_master *master,
1246 struct spi_device *spi,
1247 struct spi_transfer *xfer)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001248{
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001249 return (xfer->len >= DMA_MIN_BYTES);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001250}
1251
Grant Likelyfd4a3192012-12-07 16:57:14 +00001252static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001253{
1254 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301255 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301256 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001257
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301258 ret = pm_runtime_get_sync(mcspi->dev);
Tony Lindgren5a686b22018-04-27 08:50:07 -07001259 if (ret < 0) {
1260 pm_runtime_put_noidle(mcspi->dev);
1261
Govindraj.R1f1a4382011-02-02 17:52:15 +05301262 return ret;
Tony Lindgren5a686b22018-04-27 08:50:07 -07001263 }
Jouni Hoganderddb22192009-07-29 15:02:11 -07001264
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301265 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001266 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301267 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001268
1269 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301270 pm_runtime_mark_last_busy(mcspi->dev);
1271 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001272 return 0;
1273}
1274
Tony Lindgren52e9a5b2018-04-25 07:08:43 -07001275/*
1276 * When SPI wake up from off-mode, CS is in activate state. If it was in
1277 * inactive state when driver was suspend, then force it to inactive state at
1278 * wake up.
1279 */
Govindraj.R1f1a4382011-02-02 17:52:15 +05301280static int omap_mcspi_runtime_resume(struct device *dev)
1281{
Tony Lindgren52e9a5b2018-04-25 07:08:43 -07001282 struct spi_master *master = dev_get_drvdata(dev);
1283 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1284 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1285 struct omap2_mcspi_cs *cs;
Govindraj.R1f1a4382011-02-02 17:52:15 +05301286
Tony Lindgren52e9a5b2018-04-25 07:08:43 -07001287 /* McSPI: context restore */
1288 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
1289 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
1290
1291 list_for_each_entry(cs, &ctx->cs, node) {
1292 /*
1293 * We need to toggle CS state for OMAP take this
1294 * change in account.
1295 */
1296 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1297 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1298 writel_relaxed(cs->chconf0,
1299 cs->base + OMAP2_MCSPI_CHCONF0);
1300 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1301 writel_relaxed(cs->chconf0,
1302 cs->base + OMAP2_MCSPI_CHCONF0);
1303 } else {
1304 writel_relaxed(cs->chconf0,
1305 cs->base + OMAP2_MCSPI_CHCONF0);
1306 }
1307 }
Govindraj.R1f1a4382011-02-02 17:52:15 +05301308
1309 return 0;
1310}
1311
Benoit Coussond5a80032012-02-15 18:37:34 +01001312static struct omap2_mcspi_platform_config omap2_pdata = {
1313 .regs_offset = 0,
1314};
1315
1316static struct omap2_mcspi_platform_config omap4_pdata = {
1317 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1318};
1319
1320static const struct of_device_id omap_mcspi_of_match[] = {
1321 {
1322 .compatible = "ti,omap2-mcspi",
1323 .data = &omap2_pdata,
1324 },
1325 {
1326 .compatible = "ti,omap4-mcspi",
1327 .data = &omap4_pdata,
1328 },
1329 { },
1330};
1331MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001332
Grant Likelyfd4a3192012-12-07 16:57:14 +00001333static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001334{
1335 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001336 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001337 struct omap2_mcspi *mcspi;
1338 struct resource *r;
1339 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001340 u32 regs_offset = 0;
Benoit Coussond5a80032012-02-15 18:37:34 +01001341 struct device_node *node = pdev->dev.of_node;
1342 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001343
1344 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1345 if (master == NULL) {
1346 dev_dbg(&pdev->dev, "master allocation failed\n");
1347 return -ENOMEM;
1348 }
1349
David Brownelle7db06b2009-06-17 16:26:04 -07001350 /* the spi->mode bits understood by this driver: */
1351 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001352 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001353 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001354 master->auto_runtime_pm = true;
Neil Armstrong468a3202015-10-09 15:47:41 +02001355 master->prepare_message = omap2_mcspi_prepare_message;
Franklin S Cooper Jr0ba18702016-07-07 12:17:50 -05001356 master->can_dma = omap2_mcspi_can_dma;
Michael Wellingb28cb942015-05-07 18:36:53 -05001357 master->transfer_one = omap2_mcspi_transfer_one;
Michael Wellingddcad7e2015-05-12 12:38:57 -05001358 master->set_cs = omap2_mcspi_set_cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001359 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001360 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001361 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1362 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001363
Jingoo Han24b5a822013-05-23 19:20:40 +09001364 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001365
1366 mcspi = spi_master_get_devdata(master);
1367 mcspi->master = master;
1368
Benoit Coussond5a80032012-02-15 18:37:34 +01001369 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1370 if (match) {
1371 u32 num_cs = 1; /* default number of chipselect */
1372 pdata = match->data;
1373
1374 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1375 master->num_chipselect = num_cs;
Daniel Mack2cd45172012-11-14 11:14:26 +08001376 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1377 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001378 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001379 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001380 master->num_chipselect = pdata->num_cs;
Daniel Mack0384e902012-10-07 18:19:44 +02001381 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001382 }
1383 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001384
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001385 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb0ee5602013-01-21 11:09:18 +01001386 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1387 if (IS_ERR(mcspi->base)) {
1388 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301389 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001390 }
Vikram Naf9e53f2016-09-30 19:53:11 +05301391 mcspi->phys = r->start + regs_offset;
1392 mcspi->base += regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001393
Govindraj.R1f1a4382011-02-02 17:52:15 +05301394 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001395
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301396 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001397
Axel Lina6f936d2014-03-29 21:37:44 +08001398 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1399 sizeof(struct omap2_mcspi_dma),
1400 GFP_KERNEL);
1401 if (mcspi->dma_channels == NULL) {
1402 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301403 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001404 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001405
Charulatha V1a5d8192011-02-02 17:52:14 +05301406 for (i = 0; i < master->num_chipselect; i++) {
Peter Ujfalusib085c612016-04-29 16:11:56 +03001407 sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
1408 sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001409 }
1410
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301411 pm_runtime_use_autosuspend(&pdev->dev);
1412 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301413 pm_runtime_enable(&pdev->dev);
1414
Wei Yongjun142e07b2013-04-18 11:14:59 +08001415 status = omap2_mcspi_master_setup(mcspi);
1416 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301417 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001418
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001419 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001420 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301421 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001422
1423 return status;
1424
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301425disable_pm:
Tony Lindgren0e6f3572016-02-10 15:02:46 -08001426 pm_runtime_dont_use_autosuspend(&pdev->dev);
1427 pm_runtime_put_sync(&pdev->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301428 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301429free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301430 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001431 return status;
1432}
1433
Grant Likelyfd4a3192012-12-07 16:57:14 +00001434static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001435{
Axel Lina6f936d2014-03-29 21:37:44 +08001436 struct spi_master *master = platform_get_drvdata(pdev);
1437 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001438
Tony Lindgren0e6f3572016-02-10 15:02:46 -08001439 pm_runtime_dont_use_autosuspend(mcspi->dev);
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301440 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301441 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001442
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001443 return 0;
1444}
1445
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001446/* work with hotplug and coldplug */
1447MODULE_ALIAS("platform:omap2_mcspi");
1448
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001449#ifdef CONFIG_SUSPEND
Tony Lindgren5a686b22018-04-27 08:50:07 -07001450static int omap2_mcspi_suspend_noirq(struct device *dev)
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001451{
Pascal Huerstbeca3652015-11-19 16:18:28 +01001452 return pinctrl_pm_select_sleep_state(dev);
1453}
1454
Tony Lindgren5a686b22018-04-27 08:50:07 -07001455static int omap2_mcspi_resume_noirq(struct device *dev)
1456{
1457 struct spi_master *master = dev_get_drvdata(dev);
1458 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1459 int error;
1460
1461 error = pinctrl_pm_select_default_state(dev);
1462 if (error)
1463 dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
1464 __func__, error);
1465
Tony Lindgren5a686b22018-04-27 08:50:07 -07001466 return 0;
1467}
1468
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001469#else
Tony Lindgren5a686b22018-04-27 08:50:07 -07001470#define omap2_mcspi_suspend_noirq NULL
1471#define omap2_mcspi_resume_noirq NULL
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001472#endif
1473
1474static const struct dev_pm_ops omap2_mcspi_pm_ops = {
Tony Lindgren5a686b22018-04-27 08:50:07 -07001475 .suspend_noirq = omap2_mcspi_suspend_noirq,
1476 .resume_noirq = omap2_mcspi_resume_noirq,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301477 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001478};
1479
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001480static struct platform_driver omap2_mcspi_driver = {
1481 .driver = {
1482 .name = "omap2_mcspi",
Benoit Coussond5a80032012-02-15 18:37:34 +01001483 .pm = &omap2_mcspi_pm_ops,
1484 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001485 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001486 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001487 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001488};
1489
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001490module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001491MODULE_LICENSE("GPL");