blob: f63cb30f9010ed891d99958d44527ddba4028d53 [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;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070062};
63
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020064enum spi_imx_devtype {
Shawn Guo04ee5852011-07-10 01:16:39 +080065 IMX1_CSPI,
66 IMX21_CSPI,
67 IMX27_CSPI,
68 IMX31_CSPI,
69 IMX35_CSPI, /* CSPI on all i.mx except above */
70 IMX51_ECSPI, /* ECSPI on i.mx51 and later */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020071};
72
73struct spi_imx_data;
74
75struct spi_imx_devtype_data {
76 void (*intctrl)(struct spi_imx_data *, int);
Alexander Shiyanb36581d2016-06-08 20:02:06 +030077 int (*config)(struct spi_device *, struct spi_imx_config *);
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020078 void (*trigger)(struct spi_imx_data *);
79 int (*rx_available)(struct spi_imx_data *);
Uwe Kleine-König1723e662010-09-10 09:19:18 +020080 void (*reset)(struct spi_imx_data *);
Shawn Guo04ee5852011-07-10 01:16:39 +080081 enum spi_imx_devtype devtype;
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020082};
83
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070084struct spi_imx_data {
85 struct spi_bitbang bitbang;
Sascha Hauer6aa800c2016-02-17 14:28:48 +010086 struct device *dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070087
88 struct completion xfer_done;
Uwe Kleine-Königcc4d22a2012-03-29 21:54:18 +020089 void __iomem *base;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010090 unsigned long base_phys;
91
Sascha Haueraa29d842012-03-07 09:30:22 +010092 struct clk *clk_per;
93 struct clk *clk_ipg;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070094 unsigned long spi_clk;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +010095 unsigned int spi_bus_clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070096
Anton Bondarenkof12ae172016-02-24 09:20:29 +010097 unsigned int bytes_per_word;
98
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070099 unsigned int count;
100 void (*tx)(struct spi_imx_data *);
101 void (*rx)(struct spi_imx_data *);
102 void *rx_buf;
103 const void *tx_buf;
104 unsigned int txfifo; /* number of words pushed in tx FIFO */
105
Robin Gongf62cacc2014-09-11 09:18:44 +0800106 /* DMA */
Robin Gongf62cacc2014-09-11 09:18:44 +0800107 bool usedma;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100108 u32 wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800109 struct completion dma_rx_completion;
110 struct completion dma_tx_completion;
111
Uwe Kleine-König80023cb2012-05-21 21:49:35 +0200112 const struct spi_imx_devtype_data *devtype_data;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700113};
114
Shawn Guo04ee5852011-07-10 01:16:39 +0800115static inline int is_imx27_cspi(struct spi_imx_data *d)
116{
117 return d->devtype_data->devtype == IMX27_CSPI;
118}
119
120static inline int is_imx35_cspi(struct spi_imx_data *d)
121{
122 return d->devtype_data->devtype == IMX35_CSPI;
123}
124
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100125static inline int is_imx51_ecspi(struct spi_imx_data *d)
126{
127 return d->devtype_data->devtype == IMX51_ECSPI;
128}
129
Shawn Guo04ee5852011-07-10 01:16:39 +0800130static inline unsigned spi_imx_get_fifosize(struct spi_imx_data *d)
131{
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100132 return is_imx51_ecspi(d) ? 64 : 8;
Shawn Guo04ee5852011-07-10 01:16:39 +0800133}
134
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700135#define MXC_SPI_BUF_RX(type) \
136static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
137{ \
138 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
139 \
140 if (spi_imx->rx_buf) { \
141 *(type *)spi_imx->rx_buf = val; \
142 spi_imx->rx_buf += sizeof(type); \
143 } \
144}
145
146#define MXC_SPI_BUF_TX(type) \
147static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
148{ \
149 type val = 0; \
150 \
151 if (spi_imx->tx_buf) { \
152 val = *(type *)spi_imx->tx_buf; \
153 spi_imx->tx_buf += sizeof(type); \
154 } \
155 \
156 spi_imx->count -= sizeof(type); \
157 \
158 writel(val, spi_imx->base + MXC_CSPITXDATA); \
159}
160
161MXC_SPI_BUF_RX(u8)
162MXC_SPI_BUF_TX(u8)
163MXC_SPI_BUF_RX(u16)
164MXC_SPI_BUF_TX(u16)
165MXC_SPI_BUF_RX(u32)
166MXC_SPI_BUF_TX(u32)
167
168/* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
169 * (which is currently not the case in this driver)
170 */
171static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
172 256, 384, 512, 768, 1024};
173
174/* MX21, MX27 */
175static unsigned int spi_imx_clkdiv_1(unsigned int fin,
Shawn Guo04ee5852011-07-10 01:16:39 +0800176 unsigned int fspi, unsigned int max)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700177{
Shawn Guo04ee5852011-07-10 01:16:39 +0800178 int i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700179
180 for (i = 2; i < max; i++)
181 if (fspi * mxc_clkdivs[i] >= fin)
182 return i;
183
184 return max;
185}
186
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200187/* MX1, MX31, MX35, MX51 CSPI */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700188static unsigned int spi_imx_clkdiv_2(unsigned int fin,
189 unsigned int fspi)
190{
191 int i, div = 4;
192
193 for (i = 0; i < 7; i++) {
194 if (fspi * div >= fin)
195 return i;
196 div <<= 1;
197 }
198
199 return 7;
200}
201
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100202static int spi_imx_bytes_per_word(const int bpw)
203{
204 return DIV_ROUND_UP(bpw, BITS_PER_BYTE);
205}
206
Robin Gongf62cacc2014-09-11 09:18:44 +0800207static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
208 struct spi_transfer *transfer)
209{
210 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Sascha Hauercd8dd412016-03-17 09:21:50 +0100211 unsigned int bpw;
Robin Gongf62cacc2014-09-11 09:18:44 +0800212
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100213 if (!master->dma_rx)
214 return false;
215
Sascha Hauercd8dd412016-03-17 09:21:50 +0100216 if (!transfer)
217 return false;
218
219 bpw = transfer->bits_per_word;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100220 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
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300312static void 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
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300325static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200326{
Sascha Hauerb03c3882016-02-24 09:20:32 +0100327 u32 reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200328
Sascha Hauerb03c3882016-02-24 09:20:32 +0100329 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
330 reg |= MX51_ECSPI_CTRL_XCH;
Shawn Guo66de7572011-07-10 01:16:37 +0800331 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200332}
333
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300334static int mx51_ecspi_config(struct spi_device *spi,
335 struct spi_imx_config *config)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200336{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300337 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100338 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200339 u32 clk = config->speed_hz, delay, reg;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100340 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200341
Sascha Hauerf020c392011-02-08 21:08:59 +0100342 /*
343 * The hardware seems to have a race condition when changing modes. The
344 * current assumption is that the selection of the channel arrives
345 * earlier in the hardware than the mode bits when they are written at
346 * the same time.
347 * So set master mode for all channels as we do not support slave mode.
348 */
Shawn Guo66de7572011-07-10 01:16:37 +0800349 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200350
351 /* set clock speed */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100352 ctrl |= mx51_ecspi_clkdiv(spi_imx, config->speed_hz, &clk);
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100353 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200354
355 /* set chip select to use */
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300356 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200357
Shawn Guo66de7572011-07-10 01:16:37 +0800358 ctrl |= (config->bpw - 1) << MX51_ECSPI_CTRL_BL_OFFSET;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200359
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300360 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200361
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300362 if (spi->mode & SPI_CPHA)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300363 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100364 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300365 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200366
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300367 if (spi->mode & SPI_CPOL) {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300368 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
369 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100370 } else {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300371 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
372 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200373 }
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300374 if (spi->mode & SPI_CS_HIGH)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300375 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100376 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300377 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200378
Sascha Hauerb03c3882016-02-24 09:20:32 +0100379 if (spi_imx->usedma)
380 ctrl |= MX51_ECSPI_CTRL_SMC;
381
Anton Bondarenkof677f172015-12-08 07:43:43 +0100382 /* CTRL register always go first to bring out controller from reset */
383 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
384
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200385 reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300386 if (spi->mode & SPI_LOOP)
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200387 reg |= MX51_ECSPI_TESTREG_LBC;
388 else
389 reg &= ~MX51_ECSPI_TESTREG_LBC;
390 writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
391
Shawn Guo66de7572011-07-10 01:16:37 +0800392 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200393
Marek Vasut6fd8b852013-12-18 18:31:47 +0100394 /*
395 * Wait until the changes in the configuration register CONFIGREG
396 * propagate into the hardware. It takes exactly one tick of the
397 * SCLK clock, but we will wait two SCLK clock just to be sure. The
398 * effect of the delay it takes for the hardware to apply changes
399 * is noticable if the SCLK clock run very slow. In such a case, if
400 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
401 * be asserted before the SCLK polarity changes, which would disrupt
402 * the SPI communication as the device on the other end would consider
403 * the change of SCLK polarity as a clock tick already.
404 */
405 delay = (2 * 1000000) / clk;
406 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
407 udelay(delay);
408 else /* SCLK is _very_ slow */
409 usleep_range(delay, delay + 10);
410
Robin Gongf62cacc2014-09-11 09:18:44 +0800411 /*
412 * Configure the DMA register: setup the watermark
413 * and enable DMA request.
414 */
Robin Gongf62cacc2014-09-11 09:18:44 +0800415
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100416 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
417 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
418 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100419 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
420 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Robin Gongf62cacc2014-09-11 09:18:44 +0800421
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200422 return 0;
423}
424
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300425static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200426{
Shawn Guo66de7572011-07-10 01:16:37 +0800427 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200428}
429
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300430static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200431{
432 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800433 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200434 readl(spi_imx->base + MXC_CSPIRXDATA);
435}
436
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700437#define MX31_INTREG_TEEN (1 << 0)
438#define MX31_INTREG_RREN (1 << 3)
439
440#define MX31_CSPICTRL_ENABLE (1 << 0)
441#define MX31_CSPICTRL_MASTER (1 << 1)
442#define MX31_CSPICTRL_XCH (1 << 2)
443#define MX31_CSPICTRL_POL (1 << 4)
444#define MX31_CSPICTRL_PHA (1 << 5)
445#define MX31_CSPICTRL_SSCTL (1 << 6)
446#define MX31_CSPICTRL_SSPOL (1 << 7)
447#define MX31_CSPICTRL_BC_SHIFT 8
448#define MX35_CSPICTRL_BL_SHIFT 20
449#define MX31_CSPICTRL_CS_SHIFT 24
450#define MX35_CSPICTRL_CS_SHIFT 12
451#define MX31_CSPICTRL_DR_SHIFT 16
452
453#define MX31_CSPISTATUS 0x14
454#define MX31_STATUS_RR (1 << 3)
455
456/* These functions also work for the i.MX35, but be aware that
457 * the i.MX35 has a slightly different register layout for bits
458 * we do not use here.
459 */
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300460static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700461{
462 unsigned int val = 0;
463
464 if (enable & MXC_INT_TE)
465 val |= MX31_INTREG_TEEN;
466 if (enable & MXC_INT_RR)
467 val |= MX31_INTREG_RREN;
468
469 writel(val, spi_imx->base + MXC_CSPIINT);
470}
471
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300472static void mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700473{
474 unsigned int reg;
475
476 reg = readl(spi_imx->base + MXC_CSPICTRL);
477 reg |= MX31_CSPICTRL_XCH;
478 writel(reg, spi_imx->base + MXC_CSPICTRL);
479}
480
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300481static int mx31_config(struct spi_device *spi, struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700482{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300483 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700484 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
485
486 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
487 MX31_CSPICTRL_DR_SHIFT;
488
Shawn Guo04ee5852011-07-10 01:16:39 +0800489 if (is_imx35_cspi(spi_imx)) {
Shawn Guo2a64a902011-07-10 01:16:38 +0800490 reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
491 reg |= MX31_CSPICTRL_SSCTL;
492 } else {
493 reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
494 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700495
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300496 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700497 reg |= MX31_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300498 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700499 reg |= MX31_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300500 if (spi->mode & SPI_CS_HIGH)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700501 reg |= MX31_CSPICTRL_SSPOL;
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300502 if (spi->cs_gpio < 0)
503 reg |= (spi->cs_gpio + 32) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800504 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
505 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200506
507 writel(reg, spi_imx->base + MXC_CSPICTRL);
508
509 return 0;
510}
511
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300512static int mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700513{
514 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
515}
516
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300517static void mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200518{
519 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800520 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200521 readl(spi_imx->base + MXC_CSPIRXDATA);
522}
523
Shawn Guo3451fb12011-07-10 01:16:36 +0800524#define MX21_INTREG_RR (1 << 4)
525#define MX21_INTREG_TEEN (1 << 9)
526#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700527
Shawn Guo3451fb12011-07-10 01:16:36 +0800528#define MX21_CSPICTRL_POL (1 << 5)
529#define MX21_CSPICTRL_PHA (1 << 6)
530#define MX21_CSPICTRL_SSPOL (1 << 8)
531#define MX21_CSPICTRL_XCH (1 << 9)
532#define MX21_CSPICTRL_ENABLE (1 << 10)
533#define MX21_CSPICTRL_MASTER (1 << 11)
534#define MX21_CSPICTRL_DR_SHIFT 14
535#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700536
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300537static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700538{
539 unsigned int val = 0;
540
541 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800542 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700543 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800544 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700545
546 writel(val, spi_imx->base + MXC_CSPIINT);
547}
548
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300549static void mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700550{
551 unsigned int reg;
552
553 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800554 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700555 writel(reg, spi_imx->base + MXC_CSPICTRL);
556}
557
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300558static int mx21_config(struct spi_device *spi, struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700559{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300560 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Shawn Guo3451fb12011-07-10 01:16:36 +0800561 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Shawn Guo04ee5852011-07-10 01:16:39 +0800562 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700563
Shawn Guo04ee5852011-07-10 01:16:39 +0800564 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) <<
Shawn Guo3451fb12011-07-10 01:16:36 +0800565 MX21_CSPICTRL_DR_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700566 reg |= config->bpw - 1;
567
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300568 if (spi->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800569 reg |= MX21_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300570 if (spi->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800571 reg |= MX21_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300572 if (spi->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800573 reg |= MX21_CSPICTRL_SSPOL;
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300574 if (spi->cs_gpio < 0)
575 reg |= (spi->cs_gpio + 32) << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700576
577 writel(reg, spi_imx->base + MXC_CSPICTRL);
578
579 return 0;
580}
581
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300582static int mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700583{
Shawn Guo3451fb12011-07-10 01:16:36 +0800584 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700585}
586
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300587static void mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200588{
589 writel(1, spi_imx->base + MXC_RESET);
590}
591
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700592#define MX1_INTREG_RR (1 << 3)
593#define MX1_INTREG_TEEN (1 << 8)
594#define MX1_INTREG_RREN (1 << 11)
595
596#define MX1_CSPICTRL_POL (1 << 4)
597#define MX1_CSPICTRL_PHA (1 << 5)
598#define MX1_CSPICTRL_XCH (1 << 8)
599#define MX1_CSPICTRL_ENABLE (1 << 9)
600#define MX1_CSPICTRL_MASTER (1 << 10)
601#define MX1_CSPICTRL_DR_SHIFT 13
602
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300603static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700604{
605 unsigned int val = 0;
606
607 if (enable & MXC_INT_TE)
608 val |= MX1_INTREG_TEEN;
609 if (enable & MXC_INT_RR)
610 val |= MX1_INTREG_RREN;
611
612 writel(val, spi_imx->base + MXC_CSPIINT);
613}
614
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300615static void mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700616{
617 unsigned int reg;
618
619 reg = readl(spi_imx->base + MXC_CSPICTRL);
620 reg |= MX1_CSPICTRL_XCH;
621 writel(reg, spi_imx->base + MXC_CSPICTRL);
622}
623
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300624static int mx1_config(struct spi_device *spi, struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700625{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300626 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700627 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
628
629 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
630 MX1_CSPICTRL_DR_SHIFT;
631 reg |= config->bpw - 1;
632
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300633 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700634 reg |= MX1_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300635 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700636 reg |= MX1_CSPICTRL_POL;
637
638 writel(reg, spi_imx->base + MXC_CSPICTRL);
639
640 return 0;
641}
642
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300643static int mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700644{
645 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
646}
647
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300648static void mx1_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200649{
650 writel(1, spi_imx->base + MXC_RESET);
651}
652
Shawn Guo04ee5852011-07-10 01:16:39 +0800653static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
654 .intctrl = mx1_intctrl,
655 .config = mx1_config,
656 .trigger = mx1_trigger,
657 .rx_available = mx1_rx_available,
658 .reset = mx1_reset,
659 .devtype = IMX1_CSPI,
660};
661
662static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
663 .intctrl = mx21_intctrl,
664 .config = mx21_config,
665 .trigger = mx21_trigger,
666 .rx_available = mx21_rx_available,
667 .reset = mx21_reset,
668 .devtype = IMX21_CSPI,
669};
670
671static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
672 /* i.mx27 cspi shares the functions with i.mx21 one */
673 .intctrl = mx21_intctrl,
674 .config = mx21_config,
675 .trigger = mx21_trigger,
676 .rx_available = mx21_rx_available,
677 .reset = mx21_reset,
678 .devtype = IMX27_CSPI,
679};
680
681static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
682 .intctrl = mx31_intctrl,
683 .config = mx31_config,
684 .trigger = mx31_trigger,
685 .rx_available = mx31_rx_available,
686 .reset = mx31_reset,
687 .devtype = IMX31_CSPI,
688};
689
690static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
691 /* i.mx35 and later cspi shares the functions with i.mx31 one */
692 .intctrl = mx31_intctrl,
693 .config = mx31_config,
694 .trigger = mx31_trigger,
695 .rx_available = mx31_rx_available,
696 .reset = mx31_reset,
697 .devtype = IMX35_CSPI,
698};
699
700static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
701 .intctrl = mx51_ecspi_intctrl,
702 .config = mx51_ecspi_config,
703 .trigger = mx51_ecspi_trigger,
704 .rx_available = mx51_ecspi_rx_available,
705 .reset = mx51_ecspi_reset,
706 .devtype = IMX51_ECSPI,
707};
708
Krzysztof Kozlowskidb1b8202015-05-02 00:44:04 +0900709static const struct platform_device_id spi_imx_devtype[] = {
Shawn Guo04ee5852011-07-10 01:16:39 +0800710 {
711 .name = "imx1-cspi",
712 .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
713 }, {
714 .name = "imx21-cspi",
715 .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
716 }, {
717 .name = "imx27-cspi",
718 .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
719 }, {
720 .name = "imx31-cspi",
721 .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
722 }, {
723 .name = "imx35-cspi",
724 .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
725 }, {
726 .name = "imx51-ecspi",
727 .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
728 }, {
729 /* sentinel */
730 }
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200731};
732
Shawn Guo22a85e42011-07-10 01:16:41 +0800733static const struct of_device_id spi_imx_dt_ids[] = {
734 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
735 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
736 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
737 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
738 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
739 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
740 { /* sentinel */ }
741};
Niels de Vos27743e02013-07-29 09:38:05 +0200742MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +0800743
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700744static void spi_imx_chipselect(struct spi_device *spi, int is_active)
745{
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700746 int active = is_active != BITBANG_CS_INACTIVE;
747 int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700748
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300749 if (!gpio_is_valid(spi->cs_gpio))
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700750 return;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700751
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300752 gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700753}
754
755static void spi_imx_push(struct spi_imx_data *spi_imx)
756{
Shawn Guo04ee5852011-07-10 01:16:39 +0800757 while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700758 if (!spi_imx->count)
759 break;
760 spi_imx->tx(spi_imx);
761 spi_imx->txfifo++;
762 }
763
Shawn Guoedd501bb2011-07-10 01:16:35 +0800764 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700765}
766
767static irqreturn_t spi_imx_isr(int irq, void *dev_id)
768{
769 struct spi_imx_data *spi_imx = dev_id;
770
Shawn Guoedd501bb2011-07-10 01:16:35 +0800771 while (spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700772 spi_imx->rx(spi_imx);
773 spi_imx->txfifo--;
774 }
775
776 if (spi_imx->count) {
777 spi_imx_push(spi_imx);
778 return IRQ_HANDLED;
779 }
780
781 if (spi_imx->txfifo) {
782 /* No data left to push, but still waiting for rx data,
783 * enable receive data available interrupt.
784 */
Shawn Guoedd501bb2011-07-10 01:16:35 +0800785 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200786 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700787 return IRQ_HANDLED;
788 }
789
Shawn Guoedd501bb2011-07-10 01:16:35 +0800790 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700791 complete(&spi_imx->xfer_done);
792
793 return IRQ_HANDLED;
794}
795
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100796static int spi_imx_dma_configure(struct spi_master *master,
797 int bytes_per_word)
798{
799 int ret;
800 enum dma_slave_buswidth buswidth;
801 struct dma_slave_config rx = {}, tx = {};
802 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
803
804 if (bytes_per_word == spi_imx->bytes_per_word)
805 /* Same as last time */
806 return 0;
807
808 switch (bytes_per_word) {
809 case 4:
810 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
811 break;
812 case 2:
813 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
814 break;
815 case 1:
816 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
817 break;
818 default:
819 return -EINVAL;
820 }
821
822 tx.direction = DMA_MEM_TO_DEV;
823 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
824 tx.dst_addr_width = buswidth;
825 tx.dst_maxburst = spi_imx->wml;
826 ret = dmaengine_slave_config(master->dma_tx, &tx);
827 if (ret) {
828 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
829 return ret;
830 }
831
832 rx.direction = DMA_DEV_TO_MEM;
833 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
834 rx.src_addr_width = buswidth;
835 rx.src_maxburst = spi_imx->wml;
836 ret = dmaengine_slave_config(master->dma_rx, &rx);
837 if (ret) {
838 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
839 return ret;
840 }
841
842 spi_imx->bytes_per_word = bytes_per_word;
843
844 return 0;
845}
846
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700847static int spi_imx_setupxfer(struct spi_device *spi,
848 struct spi_transfer *t)
849{
850 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
851 struct spi_imx_config config;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100852 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700853
854 config.bpw = t ? t->bits_per_word : spi->bits_per_word;
855 config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700856
Sascha Hauer462d26b2009-10-01 15:44:29 -0700857 if (!config.speed_hz)
858 config.speed_hz = spi->max_speed_hz;
859 if (!config.bpw)
860 config.bpw = spi->bits_per_word;
Sascha Hauer462d26b2009-10-01 15:44:29 -0700861
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700862 /* Initialize the functions for transfer */
863 if (config.bpw <= 8) {
864 spi_imx->rx = spi_imx_buf_rx_u8;
865 spi_imx->tx = spi_imx_buf_tx_u8;
866 } else if (config.bpw <= 16) {
867 spi_imx->rx = spi_imx_buf_rx_u16;
868 spi_imx->tx = spi_imx_buf_tx_u16;
Sachin Kamat60514262013-05-30 13:38:09 +0530869 } else {
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700870 spi_imx->rx = spi_imx_buf_rx_u32;
871 spi_imx->tx = spi_imx_buf_tx_u32;
Stephen Warren24778be2013-05-21 20:36:35 -0600872 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700873
Sascha Hauerc008a802016-02-24 09:20:26 +0100874 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
875 spi_imx->usedma = 1;
876 else
877 spi_imx->usedma = 0;
878
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100879 if (spi_imx->usedma) {
880 ret = spi_imx_dma_configure(spi->master,
881 spi_imx_bytes_per_word(config.bpw));
882 if (ret)
883 return ret;
884 }
885
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300886 spi_imx->devtype_data->config(spi, &config);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700887
888 return 0;
889}
890
Robin Gongf62cacc2014-09-11 09:18:44 +0800891static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
892{
893 struct spi_master *master = spi_imx->bitbang.master;
894
895 if (master->dma_rx) {
896 dma_release_channel(master->dma_rx);
897 master->dma_rx = NULL;
898 }
899
900 if (master->dma_tx) {
901 dma_release_channel(master->dma_tx);
902 master->dma_tx = NULL;
903 }
Robin Gongf62cacc2014-09-11 09:18:44 +0800904}
905
906static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100907 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +0800908{
Robin Gongf62cacc2014-09-11 09:18:44 +0800909 int ret;
910
Robin Gonga02bb402015-02-03 10:25:53 +0800911 /* use pio mode for i.mx6dl chip TKT238285 */
912 if (of_machine_is_compatible("fsl,imx6dl"))
913 return 0;
914
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100915 spi_imx->wml = spi_imx_get_fifosize(spi_imx) / 2;
916
Robin Gongf62cacc2014-09-11 09:18:44 +0800917 /* Prepare for TX DMA: */
Anton Bondarenko37600472015-12-08 07:43:45 +0100918 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
919 if (IS_ERR(master->dma_tx)) {
920 ret = PTR_ERR(master->dma_tx);
921 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
922 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800923 goto err;
924 }
925
Robin Gongf62cacc2014-09-11 09:18:44 +0800926 /* Prepare for RX : */
Anton Bondarenko37600472015-12-08 07:43:45 +0100927 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
928 if (IS_ERR(master->dma_rx)) {
929 ret = PTR_ERR(master->dma_rx);
930 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
931 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800932 goto err;
933 }
934
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100935 spi_imx_dma_configure(master, 1);
Robin Gongf62cacc2014-09-11 09:18:44 +0800936
937 init_completion(&spi_imx->dma_rx_completion);
938 init_completion(&spi_imx->dma_tx_completion);
939 master->can_dma = spi_imx_can_dma;
940 master->max_dma_len = MAX_SDMA_BD_BYTES;
941 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
942 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +0800943
944 return 0;
945err:
946 spi_imx_sdma_exit(spi_imx);
947 return ret;
948}
949
950static void spi_imx_dma_rx_callback(void *cookie)
951{
952 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
953
954 complete(&spi_imx->dma_rx_completion);
955}
956
957static void spi_imx_dma_tx_callback(void *cookie)
958{
959 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
960
961 complete(&spi_imx->dma_tx_completion);
962}
963
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100964static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
965{
966 unsigned long timeout = 0;
967
968 /* Time with actual data transfer and CS change delay related to HW */
969 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
970
971 /* Add extra second for scheduler related activities */
972 timeout += 1;
973
974 /* Double calculated timeout */
975 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
976}
977
Robin Gongf62cacc2014-09-11 09:18:44 +0800978static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
979 struct spi_transfer *transfer)
980{
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100981 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100982 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -0500983 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +0800984 struct spi_master *master = spi_imx->bitbang.master;
985 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
986
Anton Bondarenkofab44ef2015-12-05 17:57:00 +0100987 /*
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100988 * The TX DMA setup starts the transfer, so make sure RX is configured
989 * before TX.
Anton Bondarenkofab44ef2015-12-05 17:57:00 +0100990 */
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100991 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
992 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
993 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
994 if (!desc_rx)
995 return -EINVAL;
996
997 desc_rx->callback = spi_imx_dma_rx_callback;
998 desc_rx->callback_param = (void *)spi_imx;
999 dmaengine_submit(desc_rx);
1000 reinit_completion(&spi_imx->dma_rx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001001 dma_async_issue_pending(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001002
1003 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1004 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1005 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1006 if (!desc_tx) {
1007 dmaengine_terminate_all(master->dma_tx);
1008 return -EINVAL;
1009 }
1010
1011 desc_tx->callback = spi_imx_dma_tx_callback;
1012 desc_tx->callback_param = (void *)spi_imx;
1013 dmaengine_submit(desc_tx);
1014 reinit_completion(&spi_imx->dma_tx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001015 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001016
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001017 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1018
Robin Gongf62cacc2014-09-11 09:18:44 +08001019 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001020 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001021 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001022 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001023 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001024 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001025 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001026 return -ETIMEDOUT;
Robin Gongf62cacc2014-09-11 09:18:44 +08001027 }
1028
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001029 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1030 transfer_timeout);
1031 if (!timeout) {
1032 dev_err(&master->dev, "I/O Error in DMA RX\n");
1033 spi_imx->devtype_data->reset(spi_imx);
1034 dmaengine_terminate_all(master->dma_rx);
1035 return -ETIMEDOUT;
1036 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001037
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001038 return transfer->len;
Robin Gongf62cacc2014-09-11 09:18:44 +08001039}
1040
1041static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001042 struct spi_transfer *transfer)
1043{
1044 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001045 unsigned long transfer_timeout;
1046 unsigned long timeout;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001047
1048 spi_imx->tx_buf = transfer->tx_buf;
1049 spi_imx->rx_buf = transfer->rx_buf;
1050 spi_imx->count = transfer->len;
1051 spi_imx->txfifo = 0;
1052
Axel Linaa0fe822014-02-09 11:06:04 +08001053 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001054
1055 spi_imx_push(spi_imx);
1056
Shawn Guoedd501bb2011-07-10 01:16:35 +08001057 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001058
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001059 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1060
1061 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1062 transfer_timeout);
1063 if (!timeout) {
1064 dev_err(&spi->dev, "I/O Error in PIO\n");
1065 spi_imx->devtype_data->reset(spi_imx);
1066 return -ETIMEDOUT;
1067 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001068
1069 return transfer->len;
1070}
1071
Robin Gongf62cacc2014-09-11 09:18:44 +08001072static int spi_imx_transfer(struct spi_device *spi,
1073 struct spi_transfer *transfer)
1074{
Robin Gongf62cacc2014-09-11 09:18:44 +08001075 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1076
Sascha Hauerc008a802016-02-24 09:20:26 +01001077 if (spi_imx->usedma)
Sascha Hauer99f1cf12016-02-23 10:23:50 +01001078 return spi_imx_dma_transfer(spi_imx, transfer);
Sascha Hauerc008a802016-02-24 09:20:26 +01001079 else
1080 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001081}
1082
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001083static int spi_imx_setup(struct spi_device *spi)
1084{
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001085 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001086 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1087
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001088 if (gpio_is_valid(spi->cs_gpio))
1089 gpio_direction_output(spi->cs_gpio,
1090 spi->mode & SPI_CS_HIGH ? 0 : 1);
Sascha Hauer6c23e5d2009-10-01 15:44:29 -07001091
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001092 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
1093
1094 return 0;
1095}
1096
1097static void spi_imx_cleanup(struct spi_device *spi)
1098{
1099}
1100
Huang Shijie9e556dc2013-10-23 16:31:50 +08001101static int
1102spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1103{
1104 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1105 int ret;
1106
1107 ret = clk_enable(spi_imx->clk_per);
1108 if (ret)
1109 return ret;
1110
1111 ret = clk_enable(spi_imx->clk_ipg);
1112 if (ret) {
1113 clk_disable(spi_imx->clk_per);
1114 return ret;
1115 }
1116
1117 return 0;
1118}
1119
1120static int
1121spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1122{
1123 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1124
1125 clk_disable(spi_imx->clk_ipg);
1126 clk_disable(spi_imx->clk_per);
1127 return 0;
1128}
1129
Grant Likelyfd4a3192012-12-07 16:57:14 +00001130static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001131{
Shawn Guo22a85e42011-07-10 01:16:41 +08001132 struct device_node *np = pdev->dev.of_node;
1133 const struct of_device_id *of_id =
1134 of_match_device(spi_imx_dt_ids, &pdev->dev);
1135 struct spi_imx_master *mxc_platform_info =
1136 dev_get_platdata(&pdev->dev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001137 struct spi_master *master;
1138 struct spi_imx_data *spi_imx;
1139 struct resource *res;
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001140 int i, ret, irq;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001141
Shawn Guo22a85e42011-07-10 01:16:41 +08001142 if (!np && !mxc_platform_info) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001143 dev_err(&pdev->dev, "can't get the platform data\n");
1144 return -EINVAL;
1145 }
1146
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001147 master = spi_alloc_master(&pdev->dev, sizeof(struct spi_imx_data));
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001148 if (!master)
1149 return -ENOMEM;
1150
1151 platform_set_drvdata(pdev, master);
1152
Stephen Warren24778be2013-05-21 20:36:35 -06001153 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001154 master->bus_num = np ? -1 : pdev->id;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001155
1156 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001157 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001158 spi_imx->dev = &pdev->dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001159
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001160 spi_imx->devtype_data = of_id ? of_id->data :
1161 (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
1162
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001163 if (mxc_platform_info) {
1164 master->num_chipselect = mxc_platform_info->num_chipselect;
1165 master->cs_gpios = devm_kzalloc(&master->dev,
1166 sizeof(int) * master->num_chipselect, GFP_KERNEL);
1167 if (!master->cs_gpios)
1168 return -ENOMEM;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001169
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001170 for (i = 0; i < master->num_chipselect; i++)
1171 master->cs_gpios[i] = mxc_platform_info->chipselect[i];
1172 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001173
1174 spi_imx->bitbang.chipselect = spi_imx_chipselect;
1175 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1176 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1177 spi_imx->bitbang.master->setup = spi_imx_setup;
1178 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001179 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1180 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001181 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1182 if (is_imx51_ecspi(spi_imx))
1183 spi_imx->bitbang.master->mode_bits |= SPI_LOOP;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001184
1185 init_completion(&spi_imx->xfer_done);
1186
1187 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001188 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1189 if (IS_ERR(spi_imx->base)) {
1190 ret = PTR_ERR(spi_imx->base);
1191 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001192 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001193 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001194
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001195 irq = platform_get_irq(pdev, 0);
1196 if (irq < 0) {
1197 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001198 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001199 }
1200
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001201 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001202 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001203 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001204 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001205 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001206 }
1207
Sascha Haueraa29d842012-03-07 09:30:22 +01001208 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1209 if (IS_ERR(spi_imx->clk_ipg)) {
1210 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001211 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001212 }
1213
Sascha Haueraa29d842012-03-07 09:30:22 +01001214 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1215 if (IS_ERR(spi_imx->clk_per)) {
1216 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001217 goto out_master_put;
Sascha Haueraa29d842012-03-07 09:30:22 +01001218 }
1219
Fabio Estevam83174622013-07-11 01:26:49 -03001220 ret = clk_prepare_enable(spi_imx->clk_per);
1221 if (ret)
1222 goto out_master_put;
1223
1224 ret = clk_prepare_enable(spi_imx->clk_ipg);
1225 if (ret)
1226 goto out_put_per;
Sascha Haueraa29d842012-03-07 09:30:22 +01001227
1228 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001229 /*
1230 * Only validated on i.mx6 now, can remove the constrain if validated on
1231 * other chips.
1232 */
Anton Bondarenko37600472015-12-08 07:43:45 +01001233 if (is_imx51_ecspi(spi_imx)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001234 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001235 if (ret == -EPROBE_DEFER)
1236 goto out_clk_put;
1237
Anton Bondarenko37600472015-12-08 07:43:45 +01001238 if (ret < 0)
1239 dev_err(&pdev->dev, "dma setup error %d, use pio\n",
1240 ret);
1241 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001242
Shawn Guoedd501bb2011-07-10 01:16:35 +08001243 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001244
Shawn Guoedd501bb2011-07-10 01:16:35 +08001245 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001246
Shawn Guo22a85e42011-07-10 01:16:41 +08001247 master->dev.of_node = pdev->dev.of_node;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001248 ret = spi_bitbang_start(&spi_imx->bitbang);
1249 if (ret) {
1250 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
1251 goto out_clk_put;
1252 }
1253
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001254 for (i = 0; i < master->num_chipselect; i++) {
1255 if (!gpio_is_valid(master->cs_gpios[i]))
1256 continue;
1257
1258 ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
1259 DRIVER_NAME);
1260 if (ret) {
1261 dev_err(&pdev->dev, "Can't get CS GPIO %i\n",
1262 master->cs_gpios[i]);
1263 goto out_clk_put;
1264 }
1265 }
1266
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001267 dev_info(&pdev->dev, "probed\n");
1268
Huang Shijie9e556dc2013-10-23 16:31:50 +08001269 clk_disable(spi_imx->clk_ipg);
1270 clk_disable(spi_imx->clk_per);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001271 return ret;
1272
1273out_clk_put:
Sascha Haueraa29d842012-03-07 09:30:22 +01001274 clk_disable_unprepare(spi_imx->clk_ipg);
Fabio Estevam83174622013-07-11 01:26:49 -03001275out_put_per:
1276 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001277out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001278 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001279
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001280 return ret;
1281}
1282
Grant Likelyfd4a3192012-12-07 16:57:14 +00001283static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001284{
1285 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001286 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001287
1288 spi_bitbang_stop(&spi_imx->bitbang);
1289
1290 writel(0, spi_imx->base + MXC_CSPICTRL);
Philippe De Muyterfd40dcc2014-02-27 10:16:15 +01001291 clk_unprepare(spi_imx->clk_ipg);
1292 clk_unprepare(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001293 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001294 spi_master_put(master);
1295
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001296 return 0;
1297}
1298
1299static struct platform_driver spi_imx_driver = {
1300 .driver = {
1301 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001302 .of_match_table = spi_imx_dt_ids,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001303 },
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001304 .id_table = spi_imx_devtype,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001305 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001306 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001307};
Grant Likely940ab882011-10-05 11:29:49 -06001308module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001309
1310MODULE_DESCRIPTION("SPI Master Controller driver");
1311MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1312MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001313MODULE_ALIAS("platform:" DRIVER_NAME);