blob: c47f9587983368c7f31e943e2a54a5a2a5504f46 [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) {
258 dev_err(mcspi->dev, "failed to get sync: %d\n", err);
259 return;
260 }
261
Michael Wellingddcad7e2015-05-12 12:38:57 -0500262 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530263
Michael Wellingddcad7e2015-05-12 12:38:57 -0500264 if (enable)
265 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
266 else
267 l |= OMAP2_MCSPI_CHCONF_FORCE;
268
269 mcspi_write_chconf0(spi, l);
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200270
271 pm_runtime_mark_last_busy(mcspi->dev);
272 pm_runtime_put_autosuspend(mcspi->dev);
Michael Wellingddcad7e2015-05-12 12:38:57 -0500273 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700274}
275
276static void omap2_mcspi_set_master_mode(struct spi_master *master)
277{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530278 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
279 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700280 u32 l;
281
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530282 /*
283 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700284 * to single-channel master mode
285 */
286 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530287 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
288 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700289 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700290
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530291 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700292}
293
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300294static void omap2_mcspi_set_fifo(const struct spi_device *spi,
295 struct spi_transfer *t, int enable)
296{
297 struct spi_master *master = spi->master;
298 struct omap2_mcspi_cs *cs = spi->controller_state;
299 struct omap2_mcspi *mcspi;
300 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300301 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300302 u32 chconf, xferlevel;
303
304 mcspi = spi_master_get_devdata(master);
305
306 chconf = mcspi_cached_chconf0(spi);
307 if (enable) {
308 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
309 if (t->len % bytes_per_word != 0)
310 goto disable_fifo;
311
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300312 if (t->rx_buf != NULL && t->tx_buf != NULL)
313 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
314 else
315 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
316
317 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300318 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
319 goto disable_fifo;
320
321 wcnt = t->len / bytes_per_word;
322 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
323 goto disable_fifo;
324
325 xferlevel = wcnt << 16;
326 if (t->rx_buf != NULL) {
327 chconf |= OMAP2_MCSPI_CHCONF_FFER;
328 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300329 }
330 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300331 chconf |= OMAP2_MCSPI_CHCONF_FFET;
332 xferlevel |= fifo_depth - 1;
333 }
334
335 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
336 mcspi_write_chconf0(spi, chconf);
337 mcspi->fifo_depth = fifo_depth;
338
339 return;
340 }
341
342disable_fifo:
343 if (t->rx_buf != NULL)
344 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500345
346 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300347 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
348
349 mcspi_write_chconf0(spi, chconf);
350 mcspi->fifo_depth = 0;
351}
352
Hemanth Va41ae1a2009-09-22 16:46:16 -0700353static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
354{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530355 struct spi_master *spi_cntrl = mcspi->master;
356 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
357 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700358
359 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530360 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
361 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700362
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530363 list_for_each_entry(cs, &ctx->cs, node)
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200364 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700365}
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700366
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300367static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
368{
369 unsigned long timeout;
370
371 timeout = jiffies + msecs_to_jiffies(1000);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200372 while (!(readl_relaxed(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100373 if (time_after(jiffies, timeout)) {
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200374 if (!(readl_relaxed(reg) & bit))
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100375 return -ETIMEDOUT;
376 else
377 return 0;
378 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300379 cpu_relax();
380 }
381 return 0;
382}
383
Russell King53741ed2012-04-23 13:51:48 +0100384static void omap2_mcspi_rx_callback(void *data)
385{
386 struct spi_device *spi = data;
387 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
388 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
389
Russell King53741ed2012-04-23 13:51:48 +0100390 /* We must disable the DMA RX request */
391 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200392
393 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100394}
395
396static void omap2_mcspi_tx_callback(void *data)
397{
398 struct spi_device *spi = data;
399 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
400 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
401
Russell King53741ed2012-04-23 13:51:48 +0100402 /* We must disable the DMA TX request */
403 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200404
405 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100406}
407
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530408static void omap2_mcspi_tx_dma(struct spi_device *spi,
409 struct spi_transfer *xfer,
410 struct dma_slave_config cfg)
411{
412 struct omap2_mcspi *mcspi;
413 struct omap2_mcspi_dma *mcspi_dma;
414 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530415
416 mcspi = spi_master_get_devdata(spi->master);
417 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
418 count = xfer->len;
419
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530420 if (mcspi_dma->dma_tx) {
421 struct dma_async_tx_descriptor *tx;
422 struct scatterlist sg;
423
424 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
425
426 sg_init_table(&sg, 1);
427 sg_dma_address(&sg) = xfer->tx_dma;
428 sg_dma_len(&sg) = xfer->len;
429
430 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
431 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
432 if (tx) {
433 tx->callback = omap2_mcspi_tx_callback;
434 tx->callback_param = spi;
435 dmaengine_submit(tx);
436 } else {
437 /* FIXME: fall back to PIO? */
438 }
439 }
440 dma_async_issue_pending(mcspi_dma->dma_tx);
441 omap2_mcspi_set_dma_req(spi, 0, 1);
442
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530443}
444
445static unsigned
446omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
447 struct dma_slave_config cfg,
448 unsigned es)
449{
450 struct omap2_mcspi *mcspi;
451 struct omap2_mcspi_dma *mcspi_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300452 unsigned int count, dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530453 u32 l;
454 int elements = 0;
455 int word_len, element_count;
456 struct omap2_mcspi_cs *cs = spi->controller_state;
457 mcspi = spi_master_get_devdata(spi->master);
458 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
459 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300460 dma_count = xfer->len;
461
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500462 /*
463 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
464 * it mentions reducing DMA transfer length by one element in master
465 * normal mode.
466 */
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300467 if (mcspi->fifo_depth == 0)
468 dma_count -= es;
469
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530470 word_len = cs->word_len;
471 l = mcspi_cached_chconf0(spi);
472
473 if (word_len <= 8)
474 element_count = count;
475 else if (word_len <= 16)
476 element_count = count >> 1;
477 else /* word_len <= 32 */
478 element_count = count >> 2;
479
480 if (mcspi_dma->dma_rx) {
481 struct dma_async_tx_descriptor *tx;
482 struct scatterlist sg;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530483
484 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
485
Franklin S Cooper Jr4bd00412016-06-27 09:54:08 -0500486 /*
487 * Reduce DMA transfer length by one more if McSPI is
488 * configured in turbo mode.
489 */
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300490 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
491 dma_count -= es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530492
493 sg_init_table(&sg, 1);
494 sg_dma_address(&sg) = xfer->rx_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300495 sg_dma_len(&sg) = dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530496
497 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
498 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
499 DMA_CTRL_ACK);
500 if (tx) {
501 tx->callback = omap2_mcspi_rx_callback;
502 tx->callback_param = spi;
503 dmaengine_submit(tx);
504 } else {
505 /* FIXME: fall back to PIO? */
506 }
507 }
508
509 dma_async_issue_pending(mcspi_dma->dma_rx);
510 omap2_mcspi_set_dma_req(spi, 1, 1);
511
512 wait_for_completion(&mcspi_dma->dma_rx_completion);
513 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
514 DMA_FROM_DEVICE);
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
530 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
531 & OMAP2_MCSPI_CHSTAT_RXS)) {
532 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 }
549 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
550 & OMAP2_MCSPI_CHSTAT_RXS)) {
551 u32 w;
552
553 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
554 if (word_len <= 8)
555 ((u8 *)xfer->rx_buf)[elements] = w;
556 else if (word_len <= 16)
557 ((u16 *)xfer->rx_buf)[elements] = w;
558 else /* word_len <= 32 */
559 ((u32 *)xfer->rx_buf)[elements] = w;
560 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300561 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300562 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530563 }
564 omap2_mcspi_set_enable(spi, 1);
565 return count;
566}
567
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700568static unsigned
569omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
570{
571 struct omap2_mcspi *mcspi;
572 struct omap2_mcspi_cs *cs = spi->controller_state;
573 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100574 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000575 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530576 u8 *rx;
577 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100578 struct dma_slave_config cfg;
579 enum dma_slave_buswidth width;
580 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300581 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530582 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300583 void __iomem *irqstat_reg;
584 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700585
586 mcspi = spi_master_get_devdata(spi->master);
587 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000588 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700589
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300590
Russell King53741ed2012-04-23 13:51:48 +0100591 if (cs->word_len <= 8) {
592 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
593 es = 1;
594 } else if (cs->word_len <= 16) {
595 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
596 es = 2;
597 } else {
598 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
599 es = 4;
600 }
601
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300602 count = xfer->len;
603 burst = 1;
604
605 if (mcspi->fifo_depth > 0) {
606 if (count > mcspi->fifo_depth)
607 burst = mcspi->fifo_depth / es;
608 else
609 burst = count / es;
610 }
611
Russell King53741ed2012-04-23 13:51:48 +0100612 memset(&cfg, 0, sizeof(cfg));
613 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
614 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
615 cfg.src_addr_width = width;
616 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300617 cfg.src_maxburst = burst;
618 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100619
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700620 rx = xfer->rx_buf;
621 tx = xfer->tx_buf;
622
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530623 if (tx != NULL)
624 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700625
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530626 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530627 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700628
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530629 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530630 wait_for_completion(&mcspi_dma->dma_tx_completion);
631 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
632 DMA_TO_DEVICE);
633
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300634 if (mcspi->fifo_depth > 0) {
635 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
636
637 if (mcspi_wait_for_reg_bit(irqstat_reg,
638 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
639 dev_err(&spi->dev, "EOW timed out\n");
640
641 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
642 OMAP2_MCSPI_IRQSTATUS_EOW);
643 }
644
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530645 /* for TX_ONLY mode, be sure all words have shifted out */
646 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300647 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
648 if (mcspi->fifo_depth > 0) {
649 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
650 OMAP2_MCSPI_CHSTAT_TXFFE);
651 if (wait_res < 0)
652 dev_err(&spi->dev, "TXFFE timed out\n");
653 } else {
654 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
655 OMAP2_MCSPI_CHSTAT_TXS);
656 if (wait_res < 0)
657 dev_err(&spi->dev, "TXS timed out\n");
658 }
659 if (wait_res >= 0 &&
660 (mcspi_wait_for_reg_bit(chstat_reg,
661 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530662 dev_err(&spi->dev, "EOT timed out\n");
663 }
664 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700665 return count;
666}
667
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700668static unsigned
669omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
670{
671 struct omap2_mcspi *mcspi;
672 struct omap2_mcspi_cs *cs = spi->controller_state;
673 unsigned int count, c;
674 u32 l;
675 void __iomem *base = cs->base;
676 void __iomem *tx_reg;
677 void __iomem *rx_reg;
678 void __iomem *chstat_reg;
679 int word_len;
680
681 mcspi = spi_master_get_devdata(spi->master);
682 count = xfer->len;
683 c = count;
684 word_len = cs->word_len;
685
Hemanth Va41ae1a2009-09-22 16:46:16 -0700686 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700687
688 /* We store the pre-calculated register addresses on stack to speed
689 * up the transfer loop. */
690 tx_reg = base + OMAP2_MCSPI_TX0;
691 rx_reg = base + OMAP2_MCSPI_RX0;
692 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
693
Michael Jonesadef6582011-02-25 16:55:11 +0100694 if (c < (word_len>>3))
695 return 0;
696
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700697 if (word_len <= 8) {
698 u8 *rx;
699 const u8 *tx;
700
701 rx = xfer->rx_buf;
702 tx = xfer->tx_buf;
703
704 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800705 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700706 if (tx != NULL) {
707 if (mcspi_wait_for_reg_bit(chstat_reg,
708 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
709 dev_err(&spi->dev, "TXS timed out\n");
710 goto out;
711 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900712 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700713 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200714 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700715 }
716 if (rx != NULL) {
717 if (mcspi_wait_for_reg_bit(chstat_reg,
718 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
719 dev_err(&spi->dev, "RXS timed out\n");
720 goto out;
721 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000722
723 if (c == 1 && tx == NULL &&
724 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
725 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200726 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900727 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000728 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000729 if (mcspi_wait_for_reg_bit(chstat_reg,
730 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
731 dev_err(&spi->dev,
732 "RXS timed out\n");
733 goto out;
734 }
735 c = 0;
736 } else if (c == 0 && tx == NULL) {
737 omap2_mcspi_set_enable(spi, 0);
738 }
739
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200740 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900741 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700742 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700743 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200744 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700745 } else if (word_len <= 16) {
746 u16 *rx;
747 const u16 *tx;
748
749 rx = xfer->rx_buf;
750 tx = xfer->tx_buf;
751 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800752 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700753 if (tx != NULL) {
754 if (mcspi_wait_for_reg_bit(chstat_reg,
755 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
756 dev_err(&spi->dev, "TXS timed out\n");
757 goto out;
758 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900759 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700760 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200761 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700762 }
763 if (rx != NULL) {
764 if (mcspi_wait_for_reg_bit(chstat_reg,
765 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
766 dev_err(&spi->dev, "RXS timed out\n");
767 goto out;
768 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000769
770 if (c == 2 && tx == NULL &&
771 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
772 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200773 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900774 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000775 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000776 if (mcspi_wait_for_reg_bit(chstat_reg,
777 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
778 dev_err(&spi->dev,
779 "RXS timed out\n");
780 goto out;
781 }
782 c = 0;
783 } else if (c == 0 && tx == NULL) {
784 omap2_mcspi_set_enable(spi, 0);
785 }
786
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200787 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900788 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700789 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700790 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200791 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700792 } else if (word_len <= 32) {
793 u32 *rx;
794 const u32 *tx;
795
796 rx = xfer->rx_buf;
797 tx = xfer->tx_buf;
798 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800799 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700800 if (tx != NULL) {
801 if (mcspi_wait_for_reg_bit(chstat_reg,
802 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
803 dev_err(&spi->dev, "TXS timed out\n");
804 goto out;
805 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900806 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700807 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200808 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700809 }
810 if (rx != NULL) {
811 if (mcspi_wait_for_reg_bit(chstat_reg,
812 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
813 dev_err(&spi->dev, "RXS timed out\n");
814 goto out;
815 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000816
817 if (c == 4 && tx == NULL &&
818 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
819 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200820 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900821 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000822 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000823 if (mcspi_wait_for_reg_bit(chstat_reg,
824 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
825 dev_err(&spi->dev,
826 "RXS timed out\n");
827 goto out;
828 }
829 c = 0;
830 } else if (c == 0 && tx == NULL) {
831 omap2_mcspi_set_enable(spi, 0);
832 }
833
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200834 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900835 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700836 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700837 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200838 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700839 }
840
841 /* for TX_ONLY mode, be sure all words have shifted out */
842 if (xfer->rx_buf == NULL) {
843 if (mcspi_wait_for_reg_bit(chstat_reg,
844 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
845 dev_err(&spi->dev, "TXS timed out\n");
846 } else if (mcspi_wait_for_reg_bit(chstat_reg,
847 OMAP2_MCSPI_CHSTAT_EOT) < 0)
848 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800849
850 /* disable chan to purge rx datas received in TX_ONLY transfer,
851 * otherwise these rx datas will affect the direct following
852 * RX_ONLY transfer.
853 */
854 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700855 }
856out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000857 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700858 return count - c;
859}
860
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200861static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
862{
863 u32 div;
864
865 for (div = 0; div < 15; div++)
866 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
867 return div;
868
869 return 15;
870}
871
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700872/* called only when no transfer is active to this device */
873static int omap2_mcspi_setup_transfer(struct spi_device *spi,
874 struct spi_transfer *t)
875{
876 struct omap2_mcspi_cs *cs = spi->controller_state;
877 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700878 struct spi_master *spi_cntrl;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100879 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700880 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700881 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700882
883 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700884 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700885
886 if (t != NULL && t->bits_per_word)
887 word_len = t->bits_per_word;
888
889 cs->word_len = word_len;
890
Scott Ellis9bd45172010-03-10 14:23:13 -0700891 if (t && t->speed_hz)
892 speed_hz = t->speed_hz;
893
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200894 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100895 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
896 clkd = omap2_mcspi_calc_divisor(speed_hz);
897 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
898 clkg = 0;
899 } else {
900 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
901 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
902 clkd = (div - 1) & 0xf;
903 extclk = (div - 1) >> 4;
904 clkg = OMAP2_MCSPI_CHCONF_CLKG;
905 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700906
Hemanth Va41ae1a2009-09-22 16:46:16 -0700907 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700908
909 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
910 * REVISIT: this controller could support SPI_3WIRE mode.
911 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800912 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200913 l &= ~OMAP2_MCSPI_CHCONF_IS;
914 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
915 l |= OMAP2_MCSPI_CHCONF_DPE0;
916 } else {
917 l |= OMAP2_MCSPI_CHCONF_IS;
918 l |= OMAP2_MCSPI_CHCONF_DPE1;
919 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
920 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700921
922 /* wordlength */
923 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
924 l |= (word_len - 1) << 7;
925
926 /* set chipselect polarity; manage with FORCE */
927 if (!(spi->mode & SPI_CS_HIGH))
928 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
929 else
930 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
931
932 /* set clock divisor */
933 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100934 l |= clkd << 2;
935
936 /* set clock granularity */
937 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
938 l |= clkg;
939 if (clkg) {
940 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
941 cs->chctrl0 |= extclk << 8;
942 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
943 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700944
945 /* set SPI mode 0..3 */
946 if (spi->mode & SPI_CPOL)
947 l |= OMAP2_MCSPI_CHCONF_POL;
948 else
949 l &= ~OMAP2_MCSPI_CHCONF_POL;
950 if (spi->mode & SPI_CPHA)
951 l |= OMAP2_MCSPI_CHCONF_PHA;
952 else
953 l &= ~OMAP2_MCSPI_CHCONF_PHA;
954
Hemanth Va41ae1a2009-09-22 16:46:16 -0700955 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700956
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700957 cs->mode = spi->mode;
958
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700959 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100960 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700961 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
962 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
963
964 return 0;
965}
966
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700967/*
968 * Note that we currently allow DMA only if we get a channel
969 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
970 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700971static int omap2_mcspi_request_dma(struct spi_device *spi)
972{
973 struct spi_master *master = spi->master;
974 struct omap2_mcspi *mcspi;
975 struct omap2_mcspi_dma *mcspi_dma;
Peter Ujfalusib085c612016-04-29 16:11:56 +0300976 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700977
978 mcspi = spi_master_get_devdata(master);
979 mcspi_dma = mcspi->dma_channels + spi->chip_select;
980
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700981 init_completion(&mcspi_dma->dma_rx_completion);
982 init_completion(&mcspi_dma->dma_tx_completion);
983
Peter Ujfalusib085c612016-04-29 16:11:56 +0300984 mcspi_dma->dma_rx = dma_request_chan(&master->dev,
985 mcspi_dma->dma_rx_ch_name);
986 if (IS_ERR(mcspi_dma->dma_rx)) {
987 ret = PTR_ERR(mcspi_dma->dma_rx);
Russell King53741ed2012-04-23 13:51:48 +0100988 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700989 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100990 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700991
Peter Ujfalusib085c612016-04-29 16:11:56 +0300992 mcspi_dma->dma_tx = dma_request_chan(&master->dev,
993 mcspi_dma->dma_tx_ch_name);
994 if (IS_ERR(mcspi_dma->dma_tx)) {
995 ret = PTR_ERR(mcspi_dma->dma_tx);
996 mcspi_dma->dma_tx = NULL;
997 dma_release_channel(mcspi_dma->dma_rx);
998 mcspi_dma->dma_rx = NULL;
999 }
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001000
1001no_dma:
Peter Ujfalusib085c612016-04-29 16:11:56 +03001002 return ret;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001003}
1004
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001005static int omap2_mcspi_setup(struct spi_device *spi)
1006{
1007 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301008 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
1009 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001010 struct omap2_mcspi_dma *mcspi_dma;
1011 struct omap2_mcspi_cs *cs = spi->controller_state;
1012
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001013 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1014
1015 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +01001016 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001017 if (!cs)
1018 return -ENOMEM;
1019 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +01001020 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001021 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001022 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001023 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001024 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001025 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301026 list_add_tail(&cs->node, &ctx->cs);
Michael Welling2f538c02015-11-30 09:02:39 -06001027
1028 if (gpio_is_valid(spi->cs_gpio)) {
1029 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
1030 if (ret) {
1031 dev_err(&spi->dev, "failed to request gpio\n");
1032 return ret;
1033 }
1034 gpio_direction_output(spi->cs_gpio,
1035 !(spi->mode & SPI_CS_HIGH));
1036 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001037 }
1038
Russell King8c7494a2012-04-23 13:56:25 +01001039 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001040 ret = omap2_mcspi_request_dma(spi);
Peter Ujfalusib085c612016-04-29 16:11:56 +03001041 if (ret)
1042 dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n",
1043 ret);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001044 }
1045
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301046 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301047 if (ret < 0)
1048 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001049
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001050 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301051 pm_runtime_mark_last_busy(mcspi->dev);
1052 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001053
1054 return ret;
1055}
1056
1057static void omap2_mcspi_cleanup(struct spi_device *spi)
1058{
1059 struct omap2_mcspi *mcspi;
1060 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001061 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001062
1063 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001064
Scott Ellis5e774942010-03-10 14:22:45 -07001065 if (spi->controller_state) {
1066 /* Unlink controller state from context save list */
1067 cs = spi->controller_state;
1068 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001069
Russell King10aa5a32012-06-18 11:27:04 +01001070 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001071 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001072
Scott Ellis99f1a432010-05-24 14:20:27 +00001073 if (spi->chip_select < spi->master->num_chipselect) {
1074 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1075
Russell King53741ed2012-04-23 13:51:48 +01001076 if (mcspi_dma->dma_rx) {
1077 dma_release_channel(mcspi_dma->dma_rx);
1078 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001079 }
Russell King53741ed2012-04-23 13:51:48 +01001080 if (mcspi_dma->dma_tx) {
1081 dma_release_channel(mcspi_dma->dma_tx);
1082 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001083 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001084 }
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001085
1086 if (gpio_is_valid(spi->cs_gpio))
1087 gpio_free(spi->cs_gpio);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001088}
1089
Michael Wellingb28cb942015-05-07 18:36:53 -05001090static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
1091 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001092{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001093
1094 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301095 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001096 * arbitrate among multiple channels. This corresponds to "single
1097 * channel" master mode. As a side effect, we need to manage the
1098 * chipselect with the FORCE bit ... CS != channel enable.
1099 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001100
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001101 struct spi_master *master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001102 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301103 struct omap2_mcspi_cs *cs;
1104 struct omap2_mcspi_device_config *cd;
1105 int par_override = 0;
1106 int status = 0;
1107 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001108
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001109 master = spi->master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001110 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301111 cs = spi->controller_state;
1112 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001113
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001114 /*
1115 * The slave driver could have changed spi->mode in which case
1116 * it will be different from cs->mode (the current hardware setup).
1117 * If so, set par_override (even though its not a parity issue) so
1118 * omap2_mcspi_setup_transfer will be called to configure the hardware
1119 * with the correct mode on the first iteration of the loop below.
1120 */
1121 if (spi->mode != cs->mode)
1122 par_override = 1;
1123
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001124 omap2_mcspi_set_enable(spi, 0);
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001125
Michael Wellinga06b4302015-05-23 21:13:44 -05001126 if (gpio_is_valid(spi->cs_gpio))
1127 omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
1128
Michael Wellingb28cb942015-05-07 18:36:53 -05001129 if (par_override ||
1130 (t->speed_hz != spi->max_speed_hz) ||
1131 (t->bits_per_word != spi->bits_per_word)) {
1132 par_override = 1;
1133 status = omap2_mcspi_setup_transfer(spi, t);
1134 if (status < 0)
1135 goto out;
1136 if (t->speed_hz == spi->max_speed_hz &&
1137 t->bits_per_word == spi->bits_per_word)
1138 par_override = 0;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301139 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001140 if (cd && cd->cs_per_word) {
1141 chconf = mcspi->ctx.modulctrl;
1142 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1143 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1144 mcspi->ctx.modulctrl =
1145 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1146 }
1147
Michael Wellingb28cb942015-05-07 18:36:53 -05001148 chconf = mcspi_cached_chconf0(spi);
1149 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1150 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1151
1152 if (t->tx_buf == NULL)
1153 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1154 else if (t->rx_buf == NULL)
1155 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1156
1157 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1158 /* Turbo mode is for more than one word */
1159 if (t->len > ((cs->word_len + 7) >> 3))
1160 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1161 }
1162
1163 mcspi_write_chconf0(spi, chconf);
1164
1165 if (t->len) {
1166 unsigned count;
1167
1168 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1169 (t->len >= DMA_MIN_BYTES))
1170 omap2_mcspi_set_fifo(spi, t, 1);
1171
1172 omap2_mcspi_set_enable(spi, 1);
1173
1174 /* RX_ONLY mode needs dummy data in TX reg */
1175 if (t->tx_buf == NULL)
1176 writel_relaxed(0, cs->base
1177 + OMAP2_MCSPI_TX0);
1178
1179 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1180 (t->len >= DMA_MIN_BYTES))
1181 count = omap2_mcspi_txrx_dma(spi, t);
1182 else
1183 count = omap2_mcspi_txrx_pio(spi, t);
1184
1185 if (count != t->len) {
1186 status = -EIO;
1187 goto out;
1188 }
1189 }
1190
Michael Wellingb28cb942015-05-07 18:36:53 -05001191 omap2_mcspi_set_enable(spi, 0);
1192
1193 if (mcspi->fifo_depth > 0)
1194 omap2_mcspi_set_fifo(spi, t, 0);
1195
1196out:
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301197 /* Restore defaults if they were overriden */
1198 if (par_override) {
1199 par_override = 0;
1200 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001201 }
1202
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001203 if (cd && cd->cs_per_word) {
1204 chconf = mcspi->ctx.modulctrl;
1205 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1206 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1207 mcspi->ctx.modulctrl =
1208 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1209 }
1210
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301211 omap2_mcspi_set_enable(spi, 0);
1212
Michael Wellinga06b4302015-05-23 21:13:44 -05001213 if (gpio_is_valid(spi->cs_gpio))
1214 omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
1215
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001216 if (mcspi->fifo_depth > 0 && t)
1217 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301218
Michael Wellingb28cb942015-05-07 18:36:53 -05001219 return status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001220}
1221
Neil Armstrong468a3202015-10-09 15:47:41 +02001222static int omap2_mcspi_prepare_message(struct spi_master *master,
1223 struct spi_message *msg)
1224{
1225 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1226 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1227 struct omap2_mcspi_cs *cs;
1228
1229 /* Only a single channel can have the FORCE bit enabled
1230 * in its chconf0 register.
1231 * Scan all channels and disable them except the current one.
1232 * A FORCE can remain from a last transfer having cs_change enabled
1233 */
1234 list_for_each_entry(cs, &ctx->cs, node) {
1235 if (msg->spi->controller_state == cs)
1236 continue;
1237
1238 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
1239 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1240 writel_relaxed(cs->chconf0,
1241 cs->base + OMAP2_MCSPI_CHCONF0);
1242 readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
1243 }
1244 }
1245
1246 return 0;
1247}
1248
Michael Wellingb28cb942015-05-07 18:36:53 -05001249static int omap2_mcspi_transfer_one(struct spi_master *master,
1250 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001251{
1252 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001253 struct omap2_mcspi_dma *mcspi_dma;
Michael Wellingb28cb942015-05-07 18:36:53 -05001254 const void *tx_buf = t->tx_buf;
1255 void *rx_buf = t->rx_buf;
1256 unsigned len = t->len;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001257
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301258 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001259 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001260
Michael Wellingb28cb942015-05-07 18:36:53 -05001261 if ((len && !(rx_buf || tx_buf))) {
1262 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1263 t->speed_hz,
1264 len,
1265 tx_buf ? "tx" : "",
1266 rx_buf ? "rx" : "",
1267 t->bits_per_word);
1268 return -EINVAL;
1269 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001270
Michael Wellingb28cb942015-05-07 18:36:53 -05001271 if (len < DMA_MIN_BYTES)
1272 goto skip_dma_map;
1273
1274 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1275 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1276 len, DMA_TO_DEVICE);
1277 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1278 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1279 'T', len);
1280 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001281 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001282 }
1283 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1284 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1285 DMA_FROM_DEVICE);
1286 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1287 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1288 'R', len);
1289 if (tx_buf != NULL)
1290 dma_unmap_single(mcspi->dev, t->tx_dma,
1291 len, DMA_TO_DEVICE);
1292 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001293 }
1294 }
1295
Michael Wellingb28cb942015-05-07 18:36:53 -05001296skip_dma_map:
1297 return omap2_mcspi_work_one(mcspi, spi, t);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001298}
1299
Grant Likelyfd4a3192012-12-07 16:57:14 +00001300static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001301{
1302 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301303 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301304 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001305
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301306 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301307 if (ret < 0)
1308 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001309
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301310 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001311 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301312 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001313
1314 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301315 pm_runtime_mark_last_busy(mcspi->dev);
1316 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001317 return 0;
1318}
1319
Govindraj.R1f1a4382011-02-02 17:52:15 +05301320static int omap_mcspi_runtime_resume(struct device *dev)
1321{
1322 struct omap2_mcspi *mcspi;
1323 struct spi_master *master;
1324
1325 master = dev_get_drvdata(dev);
1326 mcspi = spi_master_get_devdata(master);
1327 omap2_mcspi_restore_ctx(mcspi);
1328
1329 return 0;
1330}
1331
Benoit Coussond5a80032012-02-15 18:37:34 +01001332static struct omap2_mcspi_platform_config omap2_pdata = {
1333 .regs_offset = 0,
1334};
1335
1336static struct omap2_mcspi_platform_config omap4_pdata = {
1337 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1338};
1339
1340static const struct of_device_id omap_mcspi_of_match[] = {
1341 {
1342 .compatible = "ti,omap2-mcspi",
1343 .data = &omap2_pdata,
1344 },
1345 {
1346 .compatible = "ti,omap4-mcspi",
1347 .data = &omap4_pdata,
1348 },
1349 { },
1350};
1351MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001352
Grant Likelyfd4a3192012-12-07 16:57:14 +00001353static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001354{
1355 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001356 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001357 struct omap2_mcspi *mcspi;
1358 struct resource *r;
1359 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001360 u32 regs_offset = 0;
1361 static int bus_num = 1;
1362 struct device_node *node = pdev->dev.of_node;
1363 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001364
1365 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1366 if (master == NULL) {
1367 dev_dbg(&pdev->dev, "master allocation failed\n");
1368 return -ENOMEM;
1369 }
1370
David Brownelle7db06b2009-06-17 16:26:04 -07001371 /* the spi->mode bits understood by this driver: */
1372 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001373 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001374 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001375 master->auto_runtime_pm = true;
Neil Armstrong468a3202015-10-09 15:47:41 +02001376 master->prepare_message = omap2_mcspi_prepare_message;
Michael Wellingb28cb942015-05-07 18:36:53 -05001377 master->transfer_one = omap2_mcspi_transfer_one;
Michael Wellingddcad7e2015-05-12 12:38:57 -05001378 master->set_cs = omap2_mcspi_set_cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001379 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001380 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001381 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1382 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001383
Jingoo Han24b5a822013-05-23 19:20:40 +09001384 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001385
1386 mcspi = spi_master_get_devdata(master);
1387 mcspi->master = master;
1388
Benoit Coussond5a80032012-02-15 18:37:34 +01001389 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1390 if (match) {
1391 u32 num_cs = 1; /* default number of chipselect */
1392 pdata = match->data;
1393
1394 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1395 master->num_chipselect = num_cs;
1396 master->bus_num = bus_num++;
Daniel Mack2cd45172012-11-14 11:14:26 +08001397 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1398 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001399 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001400 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001401 master->num_chipselect = pdata->num_cs;
1402 if (pdev->id != -1)
1403 master->bus_num = pdev->id;
Daniel Mack0384e902012-10-07 18:19:44 +02001404 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001405 }
1406 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001407
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001408 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1409 if (r == NULL) {
1410 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301411 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001412 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301413
Benoit Coussond5a80032012-02-15 18:37:34 +01001414 r->start += regs_offset;
1415 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301416 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001417
Thierry Redingb0ee5602013-01-21 11:09:18 +01001418 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1419 if (IS_ERR(mcspi->base)) {
1420 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301421 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001422 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001423
Govindraj.R1f1a4382011-02-02 17:52:15 +05301424 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001425
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301426 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001427
Axel Lina6f936d2014-03-29 21:37:44 +08001428 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1429 sizeof(struct omap2_mcspi_dma),
1430 GFP_KERNEL);
1431 if (mcspi->dma_channels == NULL) {
1432 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301433 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001434 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001435
Charulatha V1a5d8192011-02-02 17:52:14 +05301436 for (i = 0; i < master->num_chipselect; i++) {
Peter Ujfalusib085c612016-04-29 16:11:56 +03001437 sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
1438 sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001439 }
1440
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301441 if (status < 0)
Axel Lina6f936d2014-03-29 21:37:44 +08001442 goto free_master;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301443
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301444 pm_runtime_use_autosuspend(&pdev->dev);
1445 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301446 pm_runtime_enable(&pdev->dev);
1447
Wei Yongjun142e07b2013-04-18 11:14:59 +08001448 status = omap2_mcspi_master_setup(mcspi);
1449 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301450 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001451
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001452 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001453 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301454 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001455
1456 return status;
1457
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301458disable_pm:
Tony Lindgren0e6f3572016-02-10 15:02:46 -08001459 pm_runtime_dont_use_autosuspend(&pdev->dev);
1460 pm_runtime_put_sync(&pdev->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301461 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301462free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301463 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001464 return status;
1465}
1466
Grant Likelyfd4a3192012-12-07 16:57:14 +00001467static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001468{
Axel Lina6f936d2014-03-29 21:37:44 +08001469 struct spi_master *master = platform_get_drvdata(pdev);
1470 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001471
Tony Lindgren0e6f3572016-02-10 15:02:46 -08001472 pm_runtime_dont_use_autosuspend(mcspi->dev);
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301473 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301474 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001475
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001476 return 0;
1477}
1478
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001479/* work with hotplug and coldplug */
1480MODULE_ALIAS("platform:omap2_mcspi");
1481
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001482#ifdef CONFIG_SUSPEND
1483/*
1484 * When SPI wake up from off-mode, CS is in activate state. If it was in
1485 * unactive state when driver was suspend, then force it to unactive state at
1486 * wake up.
1487 */
1488static int omap2_mcspi_resume(struct device *dev)
1489{
1490 struct spi_master *master = dev_get_drvdata(dev);
1491 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301492 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1493 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001494
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301495 pm_runtime_get_sync(mcspi->dev);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301496 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001497 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001498 /*
1499 * We need to toggle CS state for OMAP take this
1500 * change in account.
1501 */
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301502 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001503 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301504 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001505 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001506 }
1507 }
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301508 pm_runtime_mark_last_busy(mcspi->dev);
1509 pm_runtime_put_autosuspend(mcspi->dev);
Pascal Huerstbeca3652015-11-19 16:18:28 +01001510
1511 return pinctrl_pm_select_default_state(dev);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001512}
Pascal Huerstbeca3652015-11-19 16:18:28 +01001513
1514static int omap2_mcspi_suspend(struct device *dev)
1515{
1516 return pinctrl_pm_select_sleep_state(dev);
1517}
1518
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001519#else
Pascal Huerstbeca3652015-11-19 16:18:28 +01001520#define omap2_mcspi_suspend NULL
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001521#define omap2_mcspi_resume NULL
1522#endif
1523
1524static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1525 .resume = omap2_mcspi_resume,
Pascal Huerstbeca3652015-11-19 16:18:28 +01001526 .suspend = omap2_mcspi_suspend,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301527 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001528};
1529
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001530static struct platform_driver omap2_mcspi_driver = {
1531 .driver = {
1532 .name = "omap2_mcspi",
Benoit Coussond5a80032012-02-15 18:37:34 +01001533 .pm = &omap2_mcspi_pm_ops,
1534 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001535 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001536 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001537 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001538};
1539
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001540module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001541MODULE_LICENSE("GPL");