blob: 96e32d4098201ba712e1505b03d860ed9709fd5c [file] [log] [blame]
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001/*
2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright (C) 2008 Juergen Beisert
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/clk.h>
22#include <linux/completion.h>
23#include <linux/delay.h>
Robin Gongf62cacc2014-09-11 09:18:44 +080024#include <linux/dmaengine.h>
25#include <linux/dma-mapping.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070026#include <linux/err.h>
27#include <linux/gpio.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070028#include <linux/interrupt.h>
29#include <linux/io.h>
30#include <linux/irq.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070035#include <linux/spi/spi.h>
36#include <linux/spi/spi_bitbang.h>
37#include <linux/types.h>
Shawn Guo22a85e42011-07-10 01:16:41 +080038#include <linux/of.h>
39#include <linux/of_device.h>
40#include <linux/of_gpio.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070041
Robin Gongf62cacc2014-09-11 09:18:44 +080042#include <linux/platform_data/dma-imx.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020043#include <linux/platform_data/spi-imx.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070044
45#define DRIVER_NAME "spi_imx"
46
47#define MXC_CSPIRXDATA 0x00
48#define MXC_CSPITXDATA 0x04
49#define MXC_CSPICTRL 0x08
50#define MXC_CSPIINT 0x0c
51#define MXC_RESET 0x1c
52
53/* generic defines to abstract from the different register layouts */
54#define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
55#define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
56
Robin Gongf62cacc2014-09-11 09:18:44 +080057/* The maximum bytes that a sdma BD can transfer.*/
58#define MAX_SDMA_BD_BYTES (1 << 15)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070059struct spi_imx_config {
60 unsigned int speed_hz;
61 unsigned int bpw;
62 unsigned int mode;
Uwe Kleine-König3b2aa892010-09-10 09:42:29 +020063 u8 cs;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070064};
65
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020066enum spi_imx_devtype {
Shawn Guo04ee5852011-07-10 01:16:39 +080067 IMX1_CSPI,
68 IMX21_CSPI,
69 IMX27_CSPI,
70 IMX31_CSPI,
71 IMX35_CSPI, /* CSPI on all i.mx except above */
72 IMX51_ECSPI, /* ECSPI on i.mx51 and later */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020073};
74
75struct spi_imx_data;
76
77struct spi_imx_devtype_data {
78 void (*intctrl)(struct spi_imx_data *, int);
79 int (*config)(struct spi_imx_data *, struct spi_imx_config *);
80 void (*trigger)(struct spi_imx_data *);
81 int (*rx_available)(struct spi_imx_data *);
Uwe Kleine-König1723e662010-09-10 09:19:18 +020082 void (*reset)(struct spi_imx_data *);
Shawn Guo04ee5852011-07-10 01:16:39 +080083 enum spi_imx_devtype devtype;
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020084};
85
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070086struct spi_imx_data {
87 struct spi_bitbang bitbang;
Sascha Hauer6aa800c2016-02-17 14:28:48 +010088 struct device *dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070089
90 struct completion xfer_done;
Uwe Kleine-Königcc4d22a2012-03-29 21:54:18 +020091 void __iomem *base;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010092 unsigned long base_phys;
93
Sascha Haueraa29d8402012-03-07 09:30:22 +010094 struct clk *clk_per;
95 struct clk *clk_ipg;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070096 unsigned long spi_clk;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +010097 unsigned int spi_bus_clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070098
Anton Bondarenkof12ae172016-02-24 09:20:29 +010099 unsigned int bytes_per_word;
100
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700101 unsigned int count;
102 void (*tx)(struct spi_imx_data *);
103 void (*rx)(struct spi_imx_data *);
104 void *rx_buf;
105 const void *tx_buf;
106 unsigned int txfifo; /* number of words pushed in tx FIFO */
107
Robin Gongf62cacc2014-09-11 09:18:44 +0800108 /* DMA */
Robin Gongf62cacc2014-09-11 09:18:44 +0800109 unsigned int dma_finished;
110 bool usedma;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100111 u32 wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800112 struct completion dma_rx_completion;
113 struct completion dma_tx_completion;
114
Uwe Kleine-König80023cb2012-05-21 21:49:35 +0200115 const struct spi_imx_devtype_data *devtype_data;
Shawn Guoc2387cb2011-07-10 01:16:40 +0800116 int chipselect[0];
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700117};
118
Shawn Guo04ee5852011-07-10 01:16:39 +0800119static inline int is_imx27_cspi(struct spi_imx_data *d)
120{
121 return d->devtype_data->devtype == IMX27_CSPI;
122}
123
124static inline int is_imx35_cspi(struct spi_imx_data *d)
125{
126 return d->devtype_data->devtype == IMX35_CSPI;
127}
128
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100129static inline int is_imx51_ecspi(struct spi_imx_data *d)
130{
131 return d->devtype_data->devtype == IMX51_ECSPI;
132}
133
Shawn Guo04ee5852011-07-10 01:16:39 +0800134static inline unsigned spi_imx_get_fifosize(struct spi_imx_data *d)
135{
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100136 return is_imx51_ecspi(d) ? 64 : 8;
Shawn Guo04ee5852011-07-10 01:16:39 +0800137}
138
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700139#define MXC_SPI_BUF_RX(type) \
140static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
141{ \
142 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
143 \
144 if (spi_imx->rx_buf) { \
145 *(type *)spi_imx->rx_buf = val; \
146 spi_imx->rx_buf += sizeof(type); \
147 } \
148}
149
150#define MXC_SPI_BUF_TX(type) \
151static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
152{ \
153 type val = 0; \
154 \
155 if (spi_imx->tx_buf) { \
156 val = *(type *)spi_imx->tx_buf; \
157 spi_imx->tx_buf += sizeof(type); \
158 } \
159 \
160 spi_imx->count -= sizeof(type); \
161 \
162 writel(val, spi_imx->base + MXC_CSPITXDATA); \
163}
164
165MXC_SPI_BUF_RX(u8)
166MXC_SPI_BUF_TX(u8)
167MXC_SPI_BUF_RX(u16)
168MXC_SPI_BUF_TX(u16)
169MXC_SPI_BUF_RX(u32)
170MXC_SPI_BUF_TX(u32)
171
172/* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
173 * (which is currently not the case in this driver)
174 */
175static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
176 256, 384, 512, 768, 1024};
177
178/* MX21, MX27 */
179static unsigned int spi_imx_clkdiv_1(unsigned int fin,
Shawn Guo04ee5852011-07-10 01:16:39 +0800180 unsigned int fspi, unsigned int max)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700181{
Shawn Guo04ee5852011-07-10 01:16:39 +0800182 int i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700183
184 for (i = 2; i < max; i++)
185 if (fspi * mxc_clkdivs[i] >= fin)
186 return i;
187
188 return max;
189}
190
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200191/* MX1, MX31, MX35, MX51 CSPI */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700192static unsigned int spi_imx_clkdiv_2(unsigned int fin,
193 unsigned int fspi)
194{
195 int i, div = 4;
196
197 for (i = 0; i < 7; i++) {
198 if (fspi * div >= fin)
199 return i;
200 div <<= 1;
201 }
202
203 return 7;
204}
205
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100206static int spi_imx_bytes_per_word(const int bpw)
207{
208 return DIV_ROUND_UP(bpw, BITS_PER_BYTE);
209}
210
Robin Gongf62cacc2014-09-11 09:18:44 +0800211static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
212 struct spi_transfer *transfer)
213{
214 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100215 unsigned int bpw = transfer->bits_per_word;
Robin Gongf62cacc2014-09-11 09:18:44 +0800216
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100217 if (!master->dma_rx)
218 return false;
219
220 if (!bpw)
221 bpw = spi->bits_per_word;
222
223 bpw = spi_imx_bytes_per_word(bpw);
224
225 if (bpw != 1 && bpw != 2 && bpw != 4)
226 return false;
227
228 if (transfer->len < spi_imx->wml * bpw)
229 return false;
230
231 if (transfer->len % (spi_imx->wml * bpw))
232 return false;
233
234 return true;
Robin Gongf62cacc2014-09-11 09:18:44 +0800235}
236
Shawn Guo66de7572011-07-10 01:16:37 +0800237#define MX51_ECSPI_CTRL 0x08
238#define MX51_ECSPI_CTRL_ENABLE (1 << 0)
239#define MX51_ECSPI_CTRL_XCH (1 << 2)
Robin Gongf62cacc2014-09-11 09:18:44 +0800240#define MX51_ECSPI_CTRL_SMC (1 << 3)
Shawn Guo66de7572011-07-10 01:16:37 +0800241#define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
242#define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
243#define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
244#define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
245#define MX51_ECSPI_CTRL_BL_OFFSET 20
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200246
Shawn Guo66de7572011-07-10 01:16:37 +0800247#define MX51_ECSPI_CONFIG 0x0c
248#define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
249#define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
250#define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
251#define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200252#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200253
Shawn Guo66de7572011-07-10 01:16:37 +0800254#define MX51_ECSPI_INT 0x10
255#define MX51_ECSPI_INT_TEEN (1 << 0)
256#define MX51_ECSPI_INT_RREN (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200257
Robin Gongf62cacc2014-09-11 09:18:44 +0800258#define MX51_ECSPI_DMA 0x14
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100259#define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
260#define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
261#define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
Robin Gongf62cacc2014-09-11 09:18:44 +0800262
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100263#define MX51_ECSPI_DMA_TEDEN (1 << 7)
264#define MX51_ECSPI_DMA_RXDEN (1 << 23)
265#define MX51_ECSPI_DMA_RXTDEN (1 << 31)
Robin Gongf62cacc2014-09-11 09:18:44 +0800266
Shawn Guo66de7572011-07-10 01:16:37 +0800267#define MX51_ECSPI_STAT 0x18
268#define MX51_ECSPI_STAT_RR (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200269
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200270#define MX51_ECSPI_TESTREG 0x20
271#define MX51_ECSPI_TESTREG_LBC BIT(31)
272
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200273/* MX51 eCSPI */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100274static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
275 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200276{
277 /*
278 * there are two 4-bit dividers, the pre-divider divides by
279 * $pre, the post-divider by 2^$post
280 */
281 unsigned int pre, post;
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100282 unsigned int fin = spi_imx->spi_clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200283
284 if (unlikely(fspi > fin))
285 return 0;
286
287 post = fls(fin) - fls(fspi);
288 if (fin > fspi << post)
289 post++;
290
291 /* now we have: (fin <= fspi << post) with post being minimal */
292
293 post = max(4U, post) - 4;
294 if (unlikely(post > 0xf)) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100295 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
296 fspi, fin);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200297 return 0xff;
298 }
299
300 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
301
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100302 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200303 __func__, fin, fspi, post, pre);
Marek Vasut6fd8b852013-12-18 18:31:47 +0100304
305 /* Resulting frequency for the SCLK line. */
306 *fres = (fin / (pre + 1)) >> post;
307
Shawn Guo66de7572011-07-10 01:16:37 +0800308 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
309 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200310}
311
Shawn Guo66de7572011-07-10 01:16:37 +0800312static void __maybe_unused mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200313{
314 unsigned val = 0;
315
316 if (enable & MXC_INT_TE)
Shawn Guo66de7572011-07-10 01:16:37 +0800317 val |= MX51_ECSPI_INT_TEEN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200318
319 if (enable & MXC_INT_RR)
Shawn Guo66de7572011-07-10 01:16:37 +0800320 val |= MX51_ECSPI_INT_RREN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200321
Shawn Guo66de7572011-07-10 01:16:37 +0800322 writel(val, spi_imx->base + MX51_ECSPI_INT);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200323}
324
Shawn Guo66de7572011-07-10 01:16:37 +0800325static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200326{
Robin Gongf62cacc2014-09-11 09:18:44 +0800327 u32 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200328
Robin Gongf62cacc2014-09-11 09:18:44 +0800329 if (!spi_imx->usedma)
330 reg |= MX51_ECSPI_CTRL_XCH;
331 else if (!spi_imx->dma_finished)
332 reg |= MX51_ECSPI_CTRL_SMC;
333 else
334 reg &= ~MX51_ECSPI_CTRL_SMC;
Shawn Guo66de7572011-07-10 01:16:37 +0800335 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200336}
337
Shawn Guo66de7572011-07-10 01:16:37 +0800338static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200339 struct spi_imx_config *config)
340{
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100341 u32 ctrl = MX51_ECSPI_CTRL_ENABLE, cfg = 0;
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200342 u32 clk = config->speed_hz, delay, reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200343
Sascha Hauerf020c392011-02-08 21:08:59 +0100344 /*
345 * The hardware seems to have a race condition when changing modes. The
346 * current assumption is that the selection of the channel arrives
347 * earlier in the hardware than the mode bits when they are written at
348 * the same time.
349 * So set master mode for all channels as we do not support slave mode.
350 */
Shawn Guo66de7572011-07-10 01:16:37 +0800351 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200352
353 /* set clock speed */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100354 ctrl |= mx51_ecspi_clkdiv(spi_imx, config->speed_hz, &clk);
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100355 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200356
357 /* set chip select to use */
Shawn Guo66de7572011-07-10 01:16:37 +0800358 ctrl |= MX51_ECSPI_CTRL_CS(config->cs);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200359
Shawn Guo66de7572011-07-10 01:16:37 +0800360 ctrl |= (config->bpw - 1) << MX51_ECSPI_CTRL_BL_OFFSET;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200361
Shawn Guo66de7572011-07-10 01:16:37 +0800362 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(config->cs);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200363
364 if (config->mode & SPI_CPHA)
Shawn Guo66de7572011-07-10 01:16:37 +0800365 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(config->cs);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200366
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200367 if (config->mode & SPI_CPOL) {
Shawn Guo66de7572011-07-10 01:16:37 +0800368 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(config->cs);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200369 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(config->cs);
370 }
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200371 if (config->mode & SPI_CS_HIGH)
Shawn Guo66de7572011-07-10 01:16:37 +0800372 cfg |= MX51_ECSPI_CONFIG_SSBPOL(config->cs);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200373
Anton Bondarenkof677f172015-12-08 07:43:43 +0100374 /* CTRL register always go first to bring out controller from reset */
375 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
376
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200377 reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
378 if (config->mode & SPI_LOOP)
379 reg |= MX51_ECSPI_TESTREG_LBC;
380 else
381 reg &= ~MX51_ECSPI_TESTREG_LBC;
382 writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
383
Shawn Guo66de7572011-07-10 01:16:37 +0800384 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200385
Marek Vasut6fd8b852013-12-18 18:31:47 +0100386 /*
387 * Wait until the changes in the configuration register CONFIGREG
388 * propagate into the hardware. It takes exactly one tick of the
389 * SCLK clock, but we will wait two SCLK clock just to be sure. The
390 * effect of the delay it takes for the hardware to apply changes
391 * is noticable if the SCLK clock run very slow. In such a case, if
392 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
393 * be asserted before the SCLK polarity changes, which would disrupt
394 * the SPI communication as the device on the other end would consider
395 * the change of SCLK polarity as a clock tick already.
396 */
397 delay = (2 * 1000000) / clk;
398 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
399 udelay(delay);
400 else /* SCLK is _very_ slow */
401 usleep_range(delay, delay + 10);
402
Robin Gongf62cacc2014-09-11 09:18:44 +0800403 /*
404 * Configure the DMA register: setup the watermark
405 * and enable DMA request.
406 */
Robin Gongf62cacc2014-09-11 09:18:44 +0800407
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100408 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
409 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
410 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100411 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
412 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Robin Gongf62cacc2014-09-11 09:18:44 +0800413
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200414 return 0;
415}
416
Shawn Guo66de7572011-07-10 01:16:37 +0800417static int __maybe_unused mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200418{
Shawn Guo66de7572011-07-10 01:16:37 +0800419 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200420}
421
Shawn Guo66de7572011-07-10 01:16:37 +0800422static void __maybe_unused mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200423{
424 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800425 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200426 readl(spi_imx->base + MXC_CSPIRXDATA);
427}
428
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700429#define MX31_INTREG_TEEN (1 << 0)
430#define MX31_INTREG_RREN (1 << 3)
431
432#define MX31_CSPICTRL_ENABLE (1 << 0)
433#define MX31_CSPICTRL_MASTER (1 << 1)
434#define MX31_CSPICTRL_XCH (1 << 2)
435#define MX31_CSPICTRL_POL (1 << 4)
436#define MX31_CSPICTRL_PHA (1 << 5)
437#define MX31_CSPICTRL_SSCTL (1 << 6)
438#define MX31_CSPICTRL_SSPOL (1 << 7)
439#define MX31_CSPICTRL_BC_SHIFT 8
440#define MX35_CSPICTRL_BL_SHIFT 20
441#define MX31_CSPICTRL_CS_SHIFT 24
442#define MX35_CSPICTRL_CS_SHIFT 12
443#define MX31_CSPICTRL_DR_SHIFT 16
444
445#define MX31_CSPISTATUS 0x14
446#define MX31_STATUS_RR (1 << 3)
447
448/* These functions also work for the i.MX35, but be aware that
449 * the i.MX35 has a slightly different register layout for bits
450 * we do not use here.
451 */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200452static void __maybe_unused mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700453{
454 unsigned int val = 0;
455
456 if (enable & MXC_INT_TE)
457 val |= MX31_INTREG_TEEN;
458 if (enable & MXC_INT_RR)
459 val |= MX31_INTREG_RREN;
460
461 writel(val, spi_imx->base + MXC_CSPIINT);
462}
463
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200464static void __maybe_unused mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700465{
466 unsigned int reg;
467
468 reg = readl(spi_imx->base + MXC_CSPICTRL);
469 reg |= MX31_CSPICTRL_XCH;
470 writel(reg, spi_imx->base + MXC_CSPICTRL);
471}
472
Shawn Guo2a64a902011-07-10 01:16:38 +0800473static int __maybe_unused mx31_config(struct spi_imx_data *spi_imx,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700474 struct spi_imx_config *config)
475{
476 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
Uwe Kleine-König3b2aa892010-09-10 09:42:29 +0200477 int cs = spi_imx->chipselect[config->cs];
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700478
479 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
480 MX31_CSPICTRL_DR_SHIFT;
481
Shawn Guo04ee5852011-07-10 01:16:39 +0800482 if (is_imx35_cspi(spi_imx)) {
Shawn Guo2a64a902011-07-10 01:16:38 +0800483 reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
484 reg |= MX31_CSPICTRL_SSCTL;
485 } else {
486 reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
487 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700488
489 if (config->mode & SPI_CPHA)
490 reg |= MX31_CSPICTRL_PHA;
491 if (config->mode & SPI_CPOL)
492 reg |= MX31_CSPICTRL_POL;
493 if (config->mode & SPI_CS_HIGH)
494 reg |= MX31_CSPICTRL_SSPOL;
Uwe Kleine-König3b2aa892010-09-10 09:42:29 +0200495 if (cs < 0)
Shawn Guo2a64a902011-07-10 01:16:38 +0800496 reg |= (cs + 32) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800497 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
498 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200499
500 writel(reg, spi_imx->base + MXC_CSPICTRL);
501
502 return 0;
503}
504
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200505static int __maybe_unused mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700506{
507 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
508}
509
Shawn Guo2a64a902011-07-10 01:16:38 +0800510static void __maybe_unused mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200511{
512 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800513 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200514 readl(spi_imx->base + MXC_CSPIRXDATA);
515}
516
Shawn Guo3451fb12011-07-10 01:16:36 +0800517#define MX21_INTREG_RR (1 << 4)
518#define MX21_INTREG_TEEN (1 << 9)
519#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700520
Shawn Guo3451fb12011-07-10 01:16:36 +0800521#define MX21_CSPICTRL_POL (1 << 5)
522#define MX21_CSPICTRL_PHA (1 << 6)
523#define MX21_CSPICTRL_SSPOL (1 << 8)
524#define MX21_CSPICTRL_XCH (1 << 9)
525#define MX21_CSPICTRL_ENABLE (1 << 10)
526#define MX21_CSPICTRL_MASTER (1 << 11)
527#define MX21_CSPICTRL_DR_SHIFT 14
528#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700529
Shawn Guo3451fb12011-07-10 01:16:36 +0800530static void __maybe_unused mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700531{
532 unsigned int val = 0;
533
534 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800535 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700536 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800537 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700538
539 writel(val, spi_imx->base + MXC_CSPIINT);
540}
541
Shawn Guo3451fb12011-07-10 01:16:36 +0800542static void __maybe_unused mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700543{
544 unsigned int reg;
545
546 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800547 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700548 writel(reg, spi_imx->base + MXC_CSPICTRL);
549}
550
Shawn Guo3451fb12011-07-10 01:16:36 +0800551static int __maybe_unused mx21_config(struct spi_imx_data *spi_imx,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700552 struct spi_imx_config *config)
553{
Shawn Guo3451fb12011-07-10 01:16:36 +0800554 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Uwe Kleine-König3b2aa892010-09-10 09:42:29 +0200555 int cs = spi_imx->chipselect[config->cs];
Shawn Guo04ee5852011-07-10 01:16:39 +0800556 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700557
Shawn Guo04ee5852011-07-10 01:16:39 +0800558 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) <<
Shawn Guo3451fb12011-07-10 01:16:36 +0800559 MX21_CSPICTRL_DR_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700560 reg |= config->bpw - 1;
561
562 if (config->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800563 reg |= MX21_CSPICTRL_PHA;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700564 if (config->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800565 reg |= MX21_CSPICTRL_POL;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700566 if (config->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800567 reg |= MX21_CSPICTRL_SSPOL;
Uwe Kleine-König3b2aa892010-09-10 09:42:29 +0200568 if (cs < 0)
Shawn Guo3451fb12011-07-10 01:16:36 +0800569 reg |= (cs + 32) << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700570
571 writel(reg, spi_imx->base + MXC_CSPICTRL);
572
573 return 0;
574}
575
Shawn Guo3451fb12011-07-10 01:16:36 +0800576static int __maybe_unused mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700577{
Shawn Guo3451fb12011-07-10 01:16:36 +0800578 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700579}
580
Shawn Guo3451fb12011-07-10 01:16:36 +0800581static void __maybe_unused mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200582{
583 writel(1, spi_imx->base + MXC_RESET);
584}
585
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700586#define MX1_INTREG_RR (1 << 3)
587#define MX1_INTREG_TEEN (1 << 8)
588#define MX1_INTREG_RREN (1 << 11)
589
590#define MX1_CSPICTRL_POL (1 << 4)
591#define MX1_CSPICTRL_PHA (1 << 5)
592#define MX1_CSPICTRL_XCH (1 << 8)
593#define MX1_CSPICTRL_ENABLE (1 << 9)
594#define MX1_CSPICTRL_MASTER (1 << 10)
595#define MX1_CSPICTRL_DR_SHIFT 13
596
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200597static void __maybe_unused mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700598{
599 unsigned int val = 0;
600
601 if (enable & MXC_INT_TE)
602 val |= MX1_INTREG_TEEN;
603 if (enable & MXC_INT_RR)
604 val |= MX1_INTREG_RREN;
605
606 writel(val, spi_imx->base + MXC_CSPIINT);
607}
608
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200609static void __maybe_unused mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700610{
611 unsigned int reg;
612
613 reg = readl(spi_imx->base + MXC_CSPICTRL);
614 reg |= MX1_CSPICTRL_XCH;
615 writel(reg, spi_imx->base + MXC_CSPICTRL);
616}
617
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200618static int __maybe_unused mx1_config(struct spi_imx_data *spi_imx,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700619 struct spi_imx_config *config)
620{
621 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
622
623 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
624 MX1_CSPICTRL_DR_SHIFT;
625 reg |= config->bpw - 1;
626
627 if (config->mode & SPI_CPHA)
628 reg |= MX1_CSPICTRL_PHA;
629 if (config->mode & SPI_CPOL)
630 reg |= MX1_CSPICTRL_POL;
631
632 writel(reg, spi_imx->base + MXC_CSPICTRL);
633
634 return 0;
635}
636
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200637static int __maybe_unused mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700638{
639 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
640}
641
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200642static void __maybe_unused mx1_reset(struct spi_imx_data *spi_imx)
643{
644 writel(1, spi_imx->base + MXC_RESET);
645}
646
Shawn Guo04ee5852011-07-10 01:16:39 +0800647static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
648 .intctrl = mx1_intctrl,
649 .config = mx1_config,
650 .trigger = mx1_trigger,
651 .rx_available = mx1_rx_available,
652 .reset = mx1_reset,
653 .devtype = IMX1_CSPI,
654};
655
656static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
657 .intctrl = mx21_intctrl,
658 .config = mx21_config,
659 .trigger = mx21_trigger,
660 .rx_available = mx21_rx_available,
661 .reset = mx21_reset,
662 .devtype = IMX21_CSPI,
663};
664
665static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
666 /* i.mx27 cspi shares the functions with i.mx21 one */
667 .intctrl = mx21_intctrl,
668 .config = mx21_config,
669 .trigger = mx21_trigger,
670 .rx_available = mx21_rx_available,
671 .reset = mx21_reset,
672 .devtype = IMX27_CSPI,
673};
674
675static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
676 .intctrl = mx31_intctrl,
677 .config = mx31_config,
678 .trigger = mx31_trigger,
679 .rx_available = mx31_rx_available,
680 .reset = mx31_reset,
681 .devtype = IMX31_CSPI,
682};
683
684static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
685 /* i.mx35 and later cspi shares the functions with i.mx31 one */
686 .intctrl = mx31_intctrl,
687 .config = mx31_config,
688 .trigger = mx31_trigger,
689 .rx_available = mx31_rx_available,
690 .reset = mx31_reset,
691 .devtype = IMX35_CSPI,
692};
693
694static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
695 .intctrl = mx51_ecspi_intctrl,
696 .config = mx51_ecspi_config,
697 .trigger = mx51_ecspi_trigger,
698 .rx_available = mx51_ecspi_rx_available,
699 .reset = mx51_ecspi_reset,
700 .devtype = IMX51_ECSPI,
701};
702
Krzysztof Kozlowskidb1b8202015-05-02 00:44:04 +0900703static const struct platform_device_id spi_imx_devtype[] = {
Shawn Guo04ee5852011-07-10 01:16:39 +0800704 {
705 .name = "imx1-cspi",
706 .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
707 }, {
708 .name = "imx21-cspi",
709 .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
710 }, {
711 .name = "imx27-cspi",
712 .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
713 }, {
714 .name = "imx31-cspi",
715 .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
716 }, {
717 .name = "imx35-cspi",
718 .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
719 }, {
720 .name = "imx51-ecspi",
721 .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
722 }, {
723 /* sentinel */
724 }
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200725};
726
Shawn Guo22a85e42011-07-10 01:16:41 +0800727static const struct of_device_id spi_imx_dt_ids[] = {
728 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
729 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
730 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
731 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
732 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
733 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
734 { /* sentinel */ }
735};
Niels de Vos27743e02013-07-29 09:38:05 +0200736MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +0800737
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700738static void spi_imx_chipselect(struct spi_device *spi, int is_active)
739{
740 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700741 int gpio = spi_imx->chipselect[spi->chip_select];
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700742 int active = is_active != BITBANG_CS_INACTIVE;
743 int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700744
Hui Wang8b17e052012-07-13 10:51:29 +0800745 if (!gpio_is_valid(gpio))
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700746 return;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700747
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700748 gpio_set_value(gpio, dev_is_lowactive ^ active);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700749}
750
751static void spi_imx_push(struct spi_imx_data *spi_imx)
752{
Shawn Guo04ee5852011-07-10 01:16:39 +0800753 while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700754 if (!spi_imx->count)
755 break;
756 spi_imx->tx(spi_imx);
757 spi_imx->txfifo++;
758 }
759
Shawn Guoedd501bb2011-07-10 01:16:35 +0800760 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700761}
762
763static irqreturn_t spi_imx_isr(int irq, void *dev_id)
764{
765 struct spi_imx_data *spi_imx = dev_id;
766
Shawn Guoedd501bb2011-07-10 01:16:35 +0800767 while (spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700768 spi_imx->rx(spi_imx);
769 spi_imx->txfifo--;
770 }
771
772 if (spi_imx->count) {
773 spi_imx_push(spi_imx);
774 return IRQ_HANDLED;
775 }
776
777 if (spi_imx->txfifo) {
778 /* No data left to push, but still waiting for rx data,
779 * enable receive data available interrupt.
780 */
Shawn Guoedd501bb2011-07-10 01:16:35 +0800781 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200782 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700783 return IRQ_HANDLED;
784 }
785
Shawn Guoedd501bb2011-07-10 01:16:35 +0800786 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700787 complete(&spi_imx->xfer_done);
788
789 return IRQ_HANDLED;
790}
791
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100792static int spi_imx_dma_configure(struct spi_master *master,
793 int bytes_per_word)
794{
795 int ret;
796 enum dma_slave_buswidth buswidth;
797 struct dma_slave_config rx = {}, tx = {};
798 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
799
800 if (bytes_per_word == spi_imx->bytes_per_word)
801 /* Same as last time */
802 return 0;
803
804 switch (bytes_per_word) {
805 case 4:
806 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
807 break;
808 case 2:
809 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
810 break;
811 case 1:
812 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
813 break;
814 default:
815 return -EINVAL;
816 }
817
818 tx.direction = DMA_MEM_TO_DEV;
819 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
820 tx.dst_addr_width = buswidth;
821 tx.dst_maxburst = spi_imx->wml;
822 ret = dmaengine_slave_config(master->dma_tx, &tx);
823 if (ret) {
824 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
825 return ret;
826 }
827
828 rx.direction = DMA_DEV_TO_MEM;
829 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
830 rx.src_addr_width = buswidth;
831 rx.src_maxburst = spi_imx->wml;
832 ret = dmaengine_slave_config(master->dma_rx, &rx);
833 if (ret) {
834 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
835 return ret;
836 }
837
838 spi_imx->bytes_per_word = bytes_per_word;
839
840 return 0;
841}
842
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700843static int spi_imx_setupxfer(struct spi_device *spi,
844 struct spi_transfer *t)
845{
846 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
847 struct spi_imx_config config;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100848 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700849
850 config.bpw = t ? t->bits_per_word : spi->bits_per_word;
851 config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
852 config.mode = spi->mode;
Uwe Kleine-König3b2aa892010-09-10 09:42:29 +0200853 config.cs = spi->chip_select;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700854
Sascha Hauer462d26b2009-10-01 15:44:29 -0700855 if (!config.speed_hz)
856 config.speed_hz = spi->max_speed_hz;
857 if (!config.bpw)
858 config.bpw = spi->bits_per_word;
Sascha Hauer462d26b2009-10-01 15:44:29 -0700859
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700860 /* Initialize the functions for transfer */
861 if (config.bpw <= 8) {
862 spi_imx->rx = spi_imx_buf_rx_u8;
863 spi_imx->tx = spi_imx_buf_tx_u8;
864 } else if (config.bpw <= 16) {
865 spi_imx->rx = spi_imx_buf_rx_u16;
866 spi_imx->tx = spi_imx_buf_tx_u16;
Sachin Kamat60514262013-05-30 13:38:09 +0530867 } else {
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700868 spi_imx->rx = spi_imx_buf_rx_u32;
869 spi_imx->tx = spi_imx_buf_tx_u32;
Stephen Warren24778be2013-05-21 20:36:35 -0600870 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700871
Sascha Hauerc008a802016-02-24 09:20:26 +0100872 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
873 spi_imx->usedma = 1;
874 else
875 spi_imx->usedma = 0;
876
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100877 if (spi_imx->usedma) {
878 ret = spi_imx_dma_configure(spi->master,
879 spi_imx_bytes_per_word(config.bpw));
880 if (ret)
881 return ret;
882 }
883
Shawn Guoedd501bb2011-07-10 01:16:35 +0800884 spi_imx->devtype_data->config(spi_imx, &config);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700885
886 return 0;
887}
888
Robin Gongf62cacc2014-09-11 09:18:44 +0800889static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
890{
891 struct spi_master *master = spi_imx->bitbang.master;
892
893 if (master->dma_rx) {
894 dma_release_channel(master->dma_rx);
895 master->dma_rx = NULL;
896 }
897
898 if (master->dma_tx) {
899 dma_release_channel(master->dma_tx);
900 master->dma_tx = NULL;
901 }
Robin Gongf62cacc2014-09-11 09:18:44 +0800902}
903
904static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100905 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +0800906{
Robin Gongf62cacc2014-09-11 09:18:44 +0800907 int ret;
908
Robin Gonga02bb402015-02-03 10:25:53 +0800909 /* use pio mode for i.mx6dl chip TKT238285 */
910 if (of_machine_is_compatible("fsl,imx6dl"))
911 return 0;
912
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100913 spi_imx->wml = spi_imx_get_fifosize(spi_imx) / 2;
914
Robin Gongf62cacc2014-09-11 09:18:44 +0800915 /* Prepare for TX DMA: */
Anton Bondarenko37600472015-12-08 07:43:45 +0100916 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
917 if (IS_ERR(master->dma_tx)) {
918 ret = PTR_ERR(master->dma_tx);
919 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
920 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800921 goto err;
922 }
923
Robin Gongf62cacc2014-09-11 09:18:44 +0800924 /* Prepare for RX : */
Anton Bondarenko37600472015-12-08 07:43:45 +0100925 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
926 if (IS_ERR(master->dma_rx)) {
927 ret = PTR_ERR(master->dma_rx);
928 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
929 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800930 goto err;
931 }
932
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100933 spi_imx_dma_configure(master, 1);
Robin Gongf62cacc2014-09-11 09:18:44 +0800934
935 init_completion(&spi_imx->dma_rx_completion);
936 init_completion(&spi_imx->dma_tx_completion);
937 master->can_dma = spi_imx_can_dma;
938 master->max_dma_len = MAX_SDMA_BD_BYTES;
939 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
940 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +0800941
942 return 0;
943err:
944 spi_imx_sdma_exit(spi_imx);
945 return ret;
946}
947
948static void spi_imx_dma_rx_callback(void *cookie)
949{
950 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
951
952 complete(&spi_imx->dma_rx_completion);
953}
954
955static void spi_imx_dma_tx_callback(void *cookie)
956{
957 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
958
959 complete(&spi_imx->dma_tx_completion);
960}
961
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100962static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
963{
964 unsigned long timeout = 0;
965
966 /* Time with actual data transfer and CS change delay related to HW */
967 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
968
969 /* Add extra second for scheduler related activities */
970 timeout += 1;
971
972 /* Double calculated timeout */
973 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
974}
975
Robin Gongf62cacc2014-09-11 09:18:44 +0800976static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
977 struct spi_transfer *transfer)
978{
979 struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
980 int ret;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100981 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -0500982 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +0800983 struct spi_master *master = spi_imx->bitbang.master;
984 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
985
986 if (tx) {
987 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
Stefan Agnere8361f72015-03-03 00:28:31 +0100988 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
Robin Gongf62cacc2014-09-11 09:18:44 +0800989 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
990 if (!desc_tx)
Sascha Hauer99f1cf12016-02-23 10:23:50 +0100991 return -EINVAL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800992
993 desc_tx->callback = spi_imx_dma_tx_callback;
994 desc_tx->callback_param = (void *)spi_imx;
995 dmaengine_submit(desc_tx);
996 }
997
998 if (rx) {
999 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
Stefan Agnere8361f72015-03-03 00:28:31 +01001000 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
Robin Gongf62cacc2014-09-11 09:18:44 +08001001 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Sascha Hauer99f1cf12016-02-23 10:23:50 +01001002 if (!desc_rx) {
1003 dmaengine_terminate_all(master->dma_tx);
1004 return -EINVAL;
1005 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001006
1007 desc_rx->callback = spi_imx_dma_rx_callback;
1008 desc_rx->callback_param = (void *)spi_imx;
1009 dmaengine_submit(desc_rx);
1010 }
1011
1012 reinit_completion(&spi_imx->dma_rx_completion);
1013 reinit_completion(&spi_imx->dma_tx_completion);
1014
1015 /* Trigger the cspi module. */
1016 spi_imx->dma_finished = 0;
1017
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001018 /*
1019 * Set these order to avoid potential RX overflow. The overflow may
1020 * happen if we enable SPI HW before starting RX DMA due to rescheduling
1021 * for another task and/or interrupt.
1022 * So RX DMA enabled first to make sure data would be read out from FIFO
1023 * ASAP. TX DMA enabled next to start filling TX FIFO with new data.
1024 * And finaly SPI HW enabled to start actual data transfer.
1025 */
1026 dma_async_issue_pending(master->dma_rx);
1027 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001028 spi_imx->devtype_data->trigger(spi_imx);
1029
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001030 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1031
Robin Gongf62cacc2014-09-11 09:18:44 +08001032 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001033 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001034 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001035 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001036 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001037 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001038 dmaengine_terminate_all(master->dma_rx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001039 } else {
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001040 timeout = wait_for_completion_timeout(
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001041 &spi_imx->dma_rx_completion, transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001042 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001043 dev_err(spi_imx->dev, "I/O Error in DMA RX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001044 spi_imx->devtype_data->reset(spi_imx);
1045 dmaengine_terminate_all(master->dma_rx);
1046 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001047 }
1048
1049 spi_imx->dma_finished = 1;
1050 spi_imx->devtype_data->trigger(spi_imx);
1051
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001052 if (!timeout)
Robin Gongf62cacc2014-09-11 09:18:44 +08001053 ret = -ETIMEDOUT;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001054 else
Robin Gongf62cacc2014-09-11 09:18:44 +08001055 ret = transfer->len;
1056
1057 return ret;
Robin Gongf62cacc2014-09-11 09:18:44 +08001058}
1059
1060static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001061 struct spi_transfer *transfer)
1062{
1063 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1064
1065 spi_imx->tx_buf = transfer->tx_buf;
1066 spi_imx->rx_buf = transfer->rx_buf;
1067 spi_imx->count = transfer->len;
1068 spi_imx->txfifo = 0;
1069
Axel Linaa0fe822014-02-09 11:06:04 +08001070 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001071
1072 spi_imx_push(spi_imx);
1073
Shawn Guoedd501bb2011-07-10 01:16:35 +08001074 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001075
1076 wait_for_completion(&spi_imx->xfer_done);
1077
1078 return transfer->len;
1079}
1080
Robin Gongf62cacc2014-09-11 09:18:44 +08001081static int spi_imx_transfer(struct spi_device *spi,
1082 struct spi_transfer *transfer)
1083{
Robin Gongf62cacc2014-09-11 09:18:44 +08001084 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1085
Sascha Hauerc008a802016-02-24 09:20:26 +01001086 if (spi_imx->usedma)
Sascha Hauer99f1cf12016-02-23 10:23:50 +01001087 return spi_imx_dma_transfer(spi_imx, transfer);
Sascha Hauerc008a802016-02-24 09:20:26 +01001088 else
1089 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001090}
1091
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001092static int spi_imx_setup(struct spi_device *spi)
1093{
Sascha Hauer6c23e5d2009-10-01 15:44:29 -07001094 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1095 int gpio = spi_imx->chipselect[spi->chip_select];
1096
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001097 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001098 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1099
Hui Wang8b17e052012-07-13 10:51:29 +08001100 if (gpio_is_valid(gpio))
Sascha Hauer6c23e5d2009-10-01 15:44:29 -07001101 gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
1102
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001103 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
1104
1105 return 0;
1106}
1107
1108static void spi_imx_cleanup(struct spi_device *spi)
1109{
1110}
1111
Huang Shijie9e556dc2013-10-23 16:31:50 +08001112static int
1113spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1114{
1115 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1116 int ret;
1117
1118 ret = clk_enable(spi_imx->clk_per);
1119 if (ret)
1120 return ret;
1121
1122 ret = clk_enable(spi_imx->clk_ipg);
1123 if (ret) {
1124 clk_disable(spi_imx->clk_per);
1125 return ret;
1126 }
1127
1128 return 0;
1129}
1130
1131static int
1132spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1133{
1134 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1135
1136 clk_disable(spi_imx->clk_ipg);
1137 clk_disable(spi_imx->clk_per);
1138 return 0;
1139}
1140
Grant Likelyfd4a3192012-12-07 16:57:14 +00001141static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001142{
Shawn Guo22a85e42011-07-10 01:16:41 +08001143 struct device_node *np = pdev->dev.of_node;
1144 const struct of_device_id *of_id =
1145 of_match_device(spi_imx_dt_ids, &pdev->dev);
1146 struct spi_imx_master *mxc_platform_info =
1147 dev_get_platdata(&pdev->dev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001148 struct spi_master *master;
1149 struct spi_imx_data *spi_imx;
1150 struct resource *res;
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001151 int i, ret, num_cs, irq;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001152
Shawn Guo22a85e42011-07-10 01:16:41 +08001153 if (!np && !mxc_platform_info) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001154 dev_err(&pdev->dev, "can't get the platform data\n");
1155 return -EINVAL;
1156 }
1157
Shawn Guo22a85e42011-07-10 01:16:41 +08001158 ret = of_property_read_u32(np, "fsl,spi-num-chipselects", &num_cs);
Lothar Waßmann39ec0d32012-04-03 15:03:44 +02001159 if (ret < 0) {
1160 if (mxc_platform_info)
1161 num_cs = mxc_platform_info->num_chipselect;
1162 else
1163 return ret;
1164 }
Shawn Guo22a85e42011-07-10 01:16:41 +08001165
Shawn Guoc2387cb2011-07-10 01:16:40 +08001166 master = spi_alloc_master(&pdev->dev,
1167 sizeof(struct spi_imx_data) + sizeof(int) * num_cs);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001168 if (!master)
1169 return -ENOMEM;
1170
1171 platform_set_drvdata(pdev, master);
1172
Stephen Warren24778be2013-05-21 20:36:35 -06001173 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001174 master->bus_num = pdev->id;
Shawn Guoc2387cb2011-07-10 01:16:40 +08001175 master->num_chipselect = num_cs;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001176
1177 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001178 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001179 spi_imx->dev = &pdev->dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001180
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001181 spi_imx->devtype_data = of_id ? of_id->data :
1182 (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
1183
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001184 for (i = 0; i < master->num_chipselect; i++) {
Shawn Guo22a85e42011-07-10 01:16:41 +08001185 int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
Hui Wang8b17e052012-07-13 10:51:29 +08001186 if (!gpio_is_valid(cs_gpio) && mxc_platform_info)
Shawn Guo22a85e42011-07-10 01:16:41 +08001187 cs_gpio = mxc_platform_info->chipselect[i];
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001188
1189 spi_imx->chipselect[i] = cs_gpio;
Hui Wang8b17e052012-07-13 10:51:29 +08001190 if (!gpio_is_valid(cs_gpio))
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001191 continue;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001192
Fabio Estevam130b82c2013-07-11 01:26:48 -03001193 ret = devm_gpio_request(&pdev->dev, spi_imx->chipselect[i],
1194 DRIVER_NAME);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001195 if (ret) {
John Ognessbbd050a2009-11-24 16:53:07 +00001196 dev_err(&pdev->dev, "can't get cs gpios\n");
Fabio Estevam130b82c2013-07-11 01:26:48 -03001197 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001198 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001199 }
1200
1201 spi_imx->bitbang.chipselect = spi_imx_chipselect;
1202 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1203 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1204 spi_imx->bitbang.master->setup = spi_imx_setup;
1205 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001206 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1207 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001208 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1209 if (is_imx51_ecspi(spi_imx))
1210 spi_imx->bitbang.master->mode_bits |= SPI_LOOP;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001211
1212 init_completion(&spi_imx->xfer_done);
1213
1214 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001215 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1216 if (IS_ERR(spi_imx->base)) {
1217 ret = PTR_ERR(spi_imx->base);
1218 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001219 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001220 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001221
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001222 irq = platform_get_irq(pdev, 0);
1223 if (irq < 0) {
1224 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001225 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001226 }
1227
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001228 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001229 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001230 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001231 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001232 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001233 }
1234
Sascha Haueraa29d8402012-03-07 09:30:22 +01001235 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1236 if (IS_ERR(spi_imx->clk_ipg)) {
1237 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001238 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001239 }
1240
Sascha Haueraa29d8402012-03-07 09:30:22 +01001241 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1242 if (IS_ERR(spi_imx->clk_per)) {
1243 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001244 goto out_master_put;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001245 }
1246
Fabio Estevam83174622013-07-11 01:26:49 -03001247 ret = clk_prepare_enable(spi_imx->clk_per);
1248 if (ret)
1249 goto out_master_put;
1250
1251 ret = clk_prepare_enable(spi_imx->clk_ipg);
1252 if (ret)
1253 goto out_put_per;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001254
1255 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001256 /*
1257 * Only validated on i.mx6 now, can remove the constrain if validated on
1258 * other chips.
1259 */
Anton Bondarenko37600472015-12-08 07:43:45 +01001260 if (is_imx51_ecspi(spi_imx)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001261 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001262 if (ret == -EPROBE_DEFER)
1263 goto out_clk_put;
1264
Anton Bondarenko37600472015-12-08 07:43:45 +01001265 if (ret < 0)
1266 dev_err(&pdev->dev, "dma setup error %d, use pio\n",
1267 ret);
1268 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001269
Shawn Guoedd501bb2011-07-10 01:16:35 +08001270 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001271
Shawn Guoedd501bb2011-07-10 01:16:35 +08001272 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001273
Shawn Guo22a85e42011-07-10 01:16:41 +08001274 master->dev.of_node = pdev->dev.of_node;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001275 ret = spi_bitbang_start(&spi_imx->bitbang);
1276 if (ret) {
1277 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
1278 goto out_clk_put;
1279 }
1280
1281 dev_info(&pdev->dev, "probed\n");
1282
Huang Shijie9e556dc2013-10-23 16:31:50 +08001283 clk_disable(spi_imx->clk_ipg);
1284 clk_disable(spi_imx->clk_per);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001285 return ret;
1286
1287out_clk_put:
Sascha Haueraa29d8402012-03-07 09:30:22 +01001288 clk_disable_unprepare(spi_imx->clk_ipg);
Fabio Estevam83174622013-07-11 01:26:49 -03001289out_put_per:
1290 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001291out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001292 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001293
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001294 return ret;
1295}
1296
Grant Likelyfd4a3192012-12-07 16:57:14 +00001297static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001298{
1299 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001300 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001301
1302 spi_bitbang_stop(&spi_imx->bitbang);
1303
1304 writel(0, spi_imx->base + MXC_CSPICTRL);
Philippe De Muyterfd40dcc2014-02-27 10:16:15 +01001305 clk_unprepare(spi_imx->clk_ipg);
1306 clk_unprepare(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001307 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001308 spi_master_put(master);
1309
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001310 return 0;
1311}
1312
1313static struct platform_driver spi_imx_driver = {
1314 .driver = {
1315 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001316 .of_match_table = spi_imx_dt_ids,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001317 },
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001318 .id_table = spi_imx_devtype,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001319 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001320 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001321};
Grant Likely940ab882011-10-05 11:29:49 -06001322module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001323
1324MODULE_DESCRIPTION("SPI Master Controller driver");
1325MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1326MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001327MODULE_ALIAS("platform:" DRIVER_NAME);