blob: 5cc72be30744797af4330287aba71161a9109e8f [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 Haueraa29d8402012-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 Bondarenkof8a876172015-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 Bondarenkof8a876172015-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,
Martin Kaiser2636ba82016-09-01 22:38:40 +0200189 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700190{
191 int i, div = 4;
192
193 for (i = 0; i < 7; i++) {
194 if (fspi * div >= fin)
Martin Kaiser2636ba82016-09-01 22:38:40 +0200195 goto out;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700196 div <<= 1;
197 }
198
Martin Kaiser2636ba82016-09-01 22:38:40 +0200199out:
200 *fres = fin / div;
201 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700202}
203
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100204static int spi_imx_bytes_per_word(const int bpw)
205{
206 return DIV_ROUND_UP(bpw, BITS_PER_BYTE);
207}
208
Robin Gongf62cacc2014-09-11 09:18:44 +0800209static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
210 struct spi_transfer *transfer)
211{
212 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Sascha Hauercd8dd412016-03-17 09:21:50 +0100213 unsigned int bpw;
Robin Gongf62cacc2014-09-11 09:18:44 +0800214
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100215 if (!master->dma_rx)
216 return false;
217
Sascha Hauercd8dd412016-03-17 09:21:50 +0100218 if (!transfer)
219 return false;
220
221 bpw = transfer->bits_per_word;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100222 if (!bpw)
223 bpw = spi->bits_per_word;
224
225 bpw = spi_imx_bytes_per_word(bpw);
226
227 if (bpw != 1 && bpw != 2 && bpw != 4)
228 return false;
229
230 if (transfer->len < spi_imx->wml * bpw)
231 return false;
232
233 if (transfer->len % (spi_imx->wml * bpw))
234 return false;
235
236 return true;
Robin Gongf62cacc2014-09-11 09:18:44 +0800237}
238
Shawn Guo66de7572011-07-10 01:16:37 +0800239#define MX51_ECSPI_CTRL 0x08
240#define MX51_ECSPI_CTRL_ENABLE (1 << 0)
241#define MX51_ECSPI_CTRL_XCH (1 << 2)
Robin Gongf62cacc2014-09-11 09:18:44 +0800242#define MX51_ECSPI_CTRL_SMC (1 << 3)
Shawn Guo66de7572011-07-10 01:16:37 +0800243#define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
244#define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
245#define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
246#define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
247#define MX51_ECSPI_CTRL_BL_OFFSET 20
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200248
Shawn Guo66de7572011-07-10 01:16:37 +0800249#define MX51_ECSPI_CONFIG 0x0c
250#define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
251#define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
252#define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
253#define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200254#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200255
Shawn Guo66de7572011-07-10 01:16:37 +0800256#define MX51_ECSPI_INT 0x10
257#define MX51_ECSPI_INT_TEEN (1 << 0)
258#define MX51_ECSPI_INT_RREN (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200259
Robin Gongf62cacc2014-09-11 09:18:44 +0800260#define MX51_ECSPI_DMA 0x14
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100261#define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
262#define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
263#define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
Robin Gongf62cacc2014-09-11 09:18:44 +0800264
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100265#define MX51_ECSPI_DMA_TEDEN (1 << 7)
266#define MX51_ECSPI_DMA_RXDEN (1 << 23)
267#define MX51_ECSPI_DMA_RXTDEN (1 << 31)
Robin Gongf62cacc2014-09-11 09:18:44 +0800268
Shawn Guo66de7572011-07-10 01:16:37 +0800269#define MX51_ECSPI_STAT 0x18
270#define MX51_ECSPI_STAT_RR (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200271
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200272#define MX51_ECSPI_TESTREG 0x20
273#define MX51_ECSPI_TESTREG_LBC BIT(31)
274
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200275/* MX51 eCSPI */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100276static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
277 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200278{
279 /*
280 * there are two 4-bit dividers, the pre-divider divides by
281 * $pre, the post-divider by 2^$post
282 */
283 unsigned int pre, post;
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100284 unsigned int fin = spi_imx->spi_clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200285
286 if (unlikely(fspi > fin))
287 return 0;
288
289 post = fls(fin) - fls(fspi);
290 if (fin > fspi << post)
291 post++;
292
293 /* now we have: (fin <= fspi << post) with post being minimal */
294
295 post = max(4U, post) - 4;
296 if (unlikely(post > 0xf)) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100297 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
298 fspi, fin);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200299 return 0xff;
300 }
301
302 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
303
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100304 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200305 __func__, fin, fspi, post, pre);
Marek Vasut6fd8b852013-12-18 18:31:47 +0100306
307 /* Resulting frequency for the SCLK line. */
308 *fres = (fin / (pre + 1)) >> post;
309
Shawn Guo66de7572011-07-10 01:16:37 +0800310 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
311 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200312}
313
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300314static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200315{
316 unsigned val = 0;
317
318 if (enable & MXC_INT_TE)
Shawn Guo66de7572011-07-10 01:16:37 +0800319 val |= MX51_ECSPI_INT_TEEN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200320
321 if (enable & MXC_INT_RR)
Shawn Guo66de7572011-07-10 01:16:37 +0800322 val |= MX51_ECSPI_INT_RREN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200323
Shawn Guo66de7572011-07-10 01:16:37 +0800324 writel(val, spi_imx->base + MX51_ECSPI_INT);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200325}
326
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300327static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200328{
Sascha Hauerb03c3882016-02-24 09:20:32 +0100329 u32 reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200330
Sascha Hauerb03c3882016-02-24 09:20:32 +0100331 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
332 reg |= MX51_ECSPI_CTRL_XCH;
Shawn Guo66de7572011-07-10 01:16:37 +0800333 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200334}
335
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300336static int mx51_ecspi_config(struct spi_device *spi,
337 struct spi_imx_config *config)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200338{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300339 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100340 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200341 u32 clk = config->speed_hz, delay, reg;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100342 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
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 */
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300358 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
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
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300362 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200363
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300364 if (spi->mode & SPI_CPHA)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300365 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100366 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300367 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200368
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300369 if (spi->mode & SPI_CPOL) {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300370 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
371 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100372 } else {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300373 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
374 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200375 }
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300376 if (spi->mode & SPI_CS_HIGH)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300377 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100378 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300379 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200380
Sascha Hauerb03c3882016-02-24 09:20:32 +0100381 if (spi_imx->usedma)
382 ctrl |= MX51_ECSPI_CTRL_SMC;
383
Anton Bondarenkof677f172015-12-08 07:43:43 +0100384 /* CTRL register always go first to bring out controller from reset */
385 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
386
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200387 reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300388 if (spi->mode & SPI_LOOP)
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200389 reg |= MX51_ECSPI_TESTREG_LBC;
390 else
391 reg &= ~MX51_ECSPI_TESTREG_LBC;
392 writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
393
Shawn Guo66de7572011-07-10 01:16:37 +0800394 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200395
Marek Vasut6fd8b852013-12-18 18:31:47 +0100396 /*
397 * Wait until the changes in the configuration register CONFIGREG
398 * propagate into the hardware. It takes exactly one tick of the
399 * SCLK clock, but we will wait two SCLK clock just to be sure. The
400 * effect of the delay it takes for the hardware to apply changes
401 * is noticable if the SCLK clock run very slow. In such a case, if
402 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
403 * be asserted before the SCLK polarity changes, which would disrupt
404 * the SPI communication as the device on the other end would consider
405 * the change of SCLK polarity as a clock tick already.
406 */
407 delay = (2 * 1000000) / clk;
408 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
409 udelay(delay);
410 else /* SCLK is _very_ slow */
411 usleep_range(delay, delay + 10);
412
Robin Gongf62cacc2014-09-11 09:18:44 +0800413 /*
414 * Configure the DMA register: setup the watermark
415 * and enable DMA request.
416 */
Robin Gongf62cacc2014-09-11 09:18:44 +0800417
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100418 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
419 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
420 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100421 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
422 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Robin Gongf62cacc2014-09-11 09:18:44 +0800423
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200424 return 0;
425}
426
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300427static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200428{
Shawn Guo66de7572011-07-10 01:16:37 +0800429 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200430}
431
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300432static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200433{
434 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800435 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200436 readl(spi_imx->base + MXC_CSPIRXDATA);
437}
438
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700439#define MX31_INTREG_TEEN (1 << 0)
440#define MX31_INTREG_RREN (1 << 3)
441
442#define MX31_CSPICTRL_ENABLE (1 << 0)
443#define MX31_CSPICTRL_MASTER (1 << 1)
444#define MX31_CSPICTRL_XCH (1 << 2)
445#define MX31_CSPICTRL_POL (1 << 4)
446#define MX31_CSPICTRL_PHA (1 << 5)
447#define MX31_CSPICTRL_SSCTL (1 << 6)
448#define MX31_CSPICTRL_SSPOL (1 << 7)
449#define MX31_CSPICTRL_BC_SHIFT 8
450#define MX35_CSPICTRL_BL_SHIFT 20
451#define MX31_CSPICTRL_CS_SHIFT 24
452#define MX35_CSPICTRL_CS_SHIFT 12
453#define MX31_CSPICTRL_DR_SHIFT 16
454
455#define MX31_CSPISTATUS 0x14
456#define MX31_STATUS_RR (1 << 3)
457
458/* These functions also work for the i.MX35, but be aware that
459 * the i.MX35 has a slightly different register layout for bits
460 * we do not use here.
461 */
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300462static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700463{
464 unsigned int val = 0;
465
466 if (enable & MXC_INT_TE)
467 val |= MX31_INTREG_TEEN;
468 if (enable & MXC_INT_RR)
469 val |= MX31_INTREG_RREN;
470
471 writel(val, spi_imx->base + MXC_CSPIINT);
472}
473
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300474static void mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700475{
476 unsigned int reg;
477
478 reg = readl(spi_imx->base + MXC_CSPICTRL);
479 reg |= MX31_CSPICTRL_XCH;
480 writel(reg, spi_imx->base + MXC_CSPICTRL);
481}
482
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300483static int mx31_config(struct spi_device *spi, struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700484{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300485 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700486 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200487 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700488
Martin Kaiser2636ba82016-09-01 22:38:40 +0200489 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700490 MX31_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200491 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700492
Shawn Guo04ee5852011-07-10 01:16:39 +0800493 if (is_imx35_cspi(spi_imx)) {
Shawn Guo2a64a902011-07-10 01:16:38 +0800494 reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
495 reg |= MX31_CSPICTRL_SSCTL;
496 } else {
497 reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
498 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700499
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300500 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700501 reg |= MX31_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300502 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700503 reg |= MX31_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300504 if (spi->mode & SPI_CS_HIGH)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700505 reg |= MX31_CSPICTRL_SSPOL;
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300506 if (spi->cs_gpio < 0)
507 reg |= (spi->cs_gpio + 32) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800508 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
509 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200510
511 writel(reg, spi_imx->base + MXC_CSPICTRL);
512
513 return 0;
514}
515
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300516static int mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700517{
518 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
519}
520
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300521static void mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200522{
523 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800524 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200525 readl(spi_imx->base + MXC_CSPIRXDATA);
526}
527
Shawn Guo3451fb12011-07-10 01:16:36 +0800528#define MX21_INTREG_RR (1 << 4)
529#define MX21_INTREG_TEEN (1 << 9)
530#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700531
Shawn Guo3451fb12011-07-10 01:16:36 +0800532#define MX21_CSPICTRL_POL (1 << 5)
533#define MX21_CSPICTRL_PHA (1 << 6)
534#define MX21_CSPICTRL_SSPOL (1 << 8)
535#define MX21_CSPICTRL_XCH (1 << 9)
536#define MX21_CSPICTRL_ENABLE (1 << 10)
537#define MX21_CSPICTRL_MASTER (1 << 11)
538#define MX21_CSPICTRL_DR_SHIFT 14
539#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700540
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300541static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700542{
543 unsigned int val = 0;
544
545 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800546 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700547 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800548 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700549
550 writel(val, spi_imx->base + MXC_CSPIINT);
551}
552
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300553static void mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700554{
555 unsigned int reg;
556
557 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800558 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700559 writel(reg, spi_imx->base + MXC_CSPICTRL);
560}
561
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300562static int mx21_config(struct spi_device *spi, struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700563{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300564 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Shawn Guo3451fb12011-07-10 01:16:36 +0800565 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Shawn Guo04ee5852011-07-10 01:16:39 +0800566 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700567
Shawn Guo04ee5852011-07-10 01:16:39 +0800568 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) <<
Shawn Guo3451fb12011-07-10 01:16:36 +0800569 MX21_CSPICTRL_DR_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700570 reg |= config->bpw - 1;
571
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300572 if (spi->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800573 reg |= MX21_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300574 if (spi->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800575 reg |= MX21_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300576 if (spi->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800577 reg |= MX21_CSPICTRL_SSPOL;
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300578 if (spi->cs_gpio < 0)
579 reg |= (spi->cs_gpio + 32) << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700580
581 writel(reg, spi_imx->base + MXC_CSPICTRL);
582
583 return 0;
584}
585
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300586static int mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700587{
Shawn Guo3451fb12011-07-10 01:16:36 +0800588 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700589}
590
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300591static void mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200592{
593 writel(1, spi_imx->base + MXC_RESET);
594}
595
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700596#define MX1_INTREG_RR (1 << 3)
597#define MX1_INTREG_TEEN (1 << 8)
598#define MX1_INTREG_RREN (1 << 11)
599
600#define MX1_CSPICTRL_POL (1 << 4)
601#define MX1_CSPICTRL_PHA (1 << 5)
602#define MX1_CSPICTRL_XCH (1 << 8)
603#define MX1_CSPICTRL_ENABLE (1 << 9)
604#define MX1_CSPICTRL_MASTER (1 << 10)
605#define MX1_CSPICTRL_DR_SHIFT 13
606
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300607static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700608{
609 unsigned int val = 0;
610
611 if (enable & MXC_INT_TE)
612 val |= MX1_INTREG_TEEN;
613 if (enable & MXC_INT_RR)
614 val |= MX1_INTREG_RREN;
615
616 writel(val, spi_imx->base + MXC_CSPIINT);
617}
618
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300619static void mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700620{
621 unsigned int reg;
622
623 reg = readl(spi_imx->base + MXC_CSPICTRL);
624 reg |= MX1_CSPICTRL_XCH;
625 writel(reg, spi_imx->base + MXC_CSPICTRL);
626}
627
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300628static int mx1_config(struct spi_device *spi, struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700629{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300630 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700631 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200632 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700633
Martin Kaiser2636ba82016-09-01 22:38:40 +0200634 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700635 MX1_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200636 spi_imx->spi_bus_clk = clk;
637
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700638 reg |= config->bpw - 1;
639
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300640 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700641 reg |= MX1_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300642 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700643 reg |= MX1_CSPICTRL_POL;
644
645 writel(reg, spi_imx->base + MXC_CSPICTRL);
646
647 return 0;
648}
649
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300650static int mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700651{
652 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
653}
654
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300655static void mx1_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200656{
657 writel(1, spi_imx->base + MXC_RESET);
658}
659
Shawn Guo04ee5852011-07-10 01:16:39 +0800660static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
661 .intctrl = mx1_intctrl,
662 .config = mx1_config,
663 .trigger = mx1_trigger,
664 .rx_available = mx1_rx_available,
665 .reset = mx1_reset,
666 .devtype = IMX1_CSPI,
667};
668
669static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
670 .intctrl = mx21_intctrl,
671 .config = mx21_config,
672 .trigger = mx21_trigger,
673 .rx_available = mx21_rx_available,
674 .reset = mx21_reset,
675 .devtype = IMX21_CSPI,
676};
677
678static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
679 /* i.mx27 cspi shares the functions with i.mx21 one */
680 .intctrl = mx21_intctrl,
681 .config = mx21_config,
682 .trigger = mx21_trigger,
683 .rx_available = mx21_rx_available,
684 .reset = mx21_reset,
685 .devtype = IMX27_CSPI,
686};
687
688static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
689 .intctrl = mx31_intctrl,
690 .config = mx31_config,
691 .trigger = mx31_trigger,
692 .rx_available = mx31_rx_available,
693 .reset = mx31_reset,
694 .devtype = IMX31_CSPI,
695};
696
697static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
698 /* i.mx35 and later cspi shares the functions with i.mx31 one */
699 .intctrl = mx31_intctrl,
700 .config = mx31_config,
701 .trigger = mx31_trigger,
702 .rx_available = mx31_rx_available,
703 .reset = mx31_reset,
704 .devtype = IMX35_CSPI,
705};
706
707static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
708 .intctrl = mx51_ecspi_intctrl,
709 .config = mx51_ecspi_config,
710 .trigger = mx51_ecspi_trigger,
711 .rx_available = mx51_ecspi_rx_available,
712 .reset = mx51_ecspi_reset,
713 .devtype = IMX51_ECSPI,
714};
715
Krzysztof Kozlowskidb1b8202015-05-02 00:44:04 +0900716static const struct platform_device_id spi_imx_devtype[] = {
Shawn Guo04ee5852011-07-10 01:16:39 +0800717 {
718 .name = "imx1-cspi",
719 .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
720 }, {
721 .name = "imx21-cspi",
722 .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
723 }, {
724 .name = "imx27-cspi",
725 .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
726 }, {
727 .name = "imx31-cspi",
728 .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
729 }, {
730 .name = "imx35-cspi",
731 .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
732 }, {
733 .name = "imx51-ecspi",
734 .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
735 }, {
736 /* sentinel */
737 }
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200738};
739
Shawn Guo22a85e42011-07-10 01:16:41 +0800740static const struct of_device_id spi_imx_dt_ids[] = {
741 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
742 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
743 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
744 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
745 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
746 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
747 { /* sentinel */ }
748};
Niels de Vos27743e02013-07-29 09:38:05 +0200749MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +0800750
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700751static void spi_imx_chipselect(struct spi_device *spi, int is_active)
752{
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700753 int active = is_active != BITBANG_CS_INACTIVE;
754 int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700755
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300756 if (!gpio_is_valid(spi->cs_gpio))
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700757 return;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700758
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300759 gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700760}
761
762static void spi_imx_push(struct spi_imx_data *spi_imx)
763{
Shawn Guo04ee5852011-07-10 01:16:39 +0800764 while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700765 if (!spi_imx->count)
766 break;
767 spi_imx->tx(spi_imx);
768 spi_imx->txfifo++;
769 }
770
Shawn Guoedd501bb2011-07-10 01:16:35 +0800771 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700772}
773
774static irqreturn_t spi_imx_isr(int irq, void *dev_id)
775{
776 struct spi_imx_data *spi_imx = dev_id;
777
Shawn Guoedd501bb2011-07-10 01:16:35 +0800778 while (spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700779 spi_imx->rx(spi_imx);
780 spi_imx->txfifo--;
781 }
782
783 if (spi_imx->count) {
784 spi_imx_push(spi_imx);
785 return IRQ_HANDLED;
786 }
787
788 if (spi_imx->txfifo) {
789 /* No data left to push, but still waiting for rx data,
790 * enable receive data available interrupt.
791 */
Shawn Guoedd501bb2011-07-10 01:16:35 +0800792 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200793 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700794 return IRQ_HANDLED;
795 }
796
Shawn Guoedd501bb2011-07-10 01:16:35 +0800797 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700798 complete(&spi_imx->xfer_done);
799
800 return IRQ_HANDLED;
801}
802
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100803static int spi_imx_dma_configure(struct spi_master *master,
804 int bytes_per_word)
805{
806 int ret;
807 enum dma_slave_buswidth buswidth;
808 struct dma_slave_config rx = {}, tx = {};
809 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
810
811 if (bytes_per_word == spi_imx->bytes_per_word)
812 /* Same as last time */
813 return 0;
814
815 switch (bytes_per_word) {
816 case 4:
817 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
818 break;
819 case 2:
820 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
821 break;
822 case 1:
823 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
824 break;
825 default:
826 return -EINVAL;
827 }
828
829 tx.direction = DMA_MEM_TO_DEV;
830 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
831 tx.dst_addr_width = buswidth;
832 tx.dst_maxburst = spi_imx->wml;
833 ret = dmaengine_slave_config(master->dma_tx, &tx);
834 if (ret) {
835 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
836 return ret;
837 }
838
839 rx.direction = DMA_DEV_TO_MEM;
840 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
841 rx.src_addr_width = buswidth;
842 rx.src_maxburst = spi_imx->wml;
843 ret = dmaengine_slave_config(master->dma_rx, &rx);
844 if (ret) {
845 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
846 return ret;
847 }
848
849 spi_imx->bytes_per_word = bytes_per_word;
850
851 return 0;
852}
853
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700854static int spi_imx_setupxfer(struct spi_device *spi,
855 struct spi_transfer *t)
856{
857 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
858 struct spi_imx_config config;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100859 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700860
861 config.bpw = t ? t->bits_per_word : spi->bits_per_word;
862 config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700863
Sascha Hauer462d26b2009-10-01 15:44:29 -0700864 if (!config.speed_hz)
865 config.speed_hz = spi->max_speed_hz;
866 if (!config.bpw)
867 config.bpw = spi->bits_per_word;
Sascha Hauer462d26b2009-10-01 15:44:29 -0700868
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700869 /* Initialize the functions for transfer */
870 if (config.bpw <= 8) {
871 spi_imx->rx = spi_imx_buf_rx_u8;
872 spi_imx->tx = spi_imx_buf_tx_u8;
873 } else if (config.bpw <= 16) {
874 spi_imx->rx = spi_imx_buf_rx_u16;
875 spi_imx->tx = spi_imx_buf_tx_u16;
Sachin Kamat60514262013-05-30 13:38:09 +0530876 } else {
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700877 spi_imx->rx = spi_imx_buf_rx_u32;
878 spi_imx->tx = spi_imx_buf_tx_u32;
Stephen Warren24778be2013-05-21 20:36:35 -0600879 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700880
Sascha Hauerc008a802016-02-24 09:20:26 +0100881 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
882 spi_imx->usedma = 1;
883 else
884 spi_imx->usedma = 0;
885
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100886 if (spi_imx->usedma) {
887 ret = spi_imx_dma_configure(spi->master,
888 spi_imx_bytes_per_word(config.bpw));
889 if (ret)
890 return ret;
891 }
892
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300893 spi_imx->devtype_data->config(spi, &config);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700894
895 return 0;
896}
897
Robin Gongf62cacc2014-09-11 09:18:44 +0800898static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
899{
900 struct spi_master *master = spi_imx->bitbang.master;
901
902 if (master->dma_rx) {
903 dma_release_channel(master->dma_rx);
904 master->dma_rx = NULL;
905 }
906
907 if (master->dma_tx) {
908 dma_release_channel(master->dma_tx);
909 master->dma_tx = NULL;
910 }
Robin Gongf62cacc2014-09-11 09:18:44 +0800911}
912
913static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100914 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +0800915{
Robin Gongf62cacc2014-09-11 09:18:44 +0800916 int ret;
917
Robin Gonga02bb402015-02-03 10:25:53 +0800918 /* use pio mode for i.mx6dl chip TKT238285 */
919 if (of_machine_is_compatible("fsl,imx6dl"))
920 return 0;
921
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100922 spi_imx->wml = spi_imx_get_fifosize(spi_imx) / 2;
923
Robin Gongf62cacc2014-09-11 09:18:44 +0800924 /* Prepare for TX DMA: */
Anton Bondarenko37600472015-12-08 07:43:45 +0100925 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
926 if (IS_ERR(master->dma_tx)) {
927 ret = PTR_ERR(master->dma_tx);
928 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
929 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800930 goto err;
931 }
932
Robin Gongf62cacc2014-09-11 09:18:44 +0800933 /* Prepare for RX : */
Anton Bondarenko37600472015-12-08 07:43:45 +0100934 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
935 if (IS_ERR(master->dma_rx)) {
936 ret = PTR_ERR(master->dma_rx);
937 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
938 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800939 goto err;
940 }
941
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100942 spi_imx_dma_configure(master, 1);
Robin Gongf62cacc2014-09-11 09:18:44 +0800943
944 init_completion(&spi_imx->dma_rx_completion);
945 init_completion(&spi_imx->dma_tx_completion);
946 master->can_dma = spi_imx_can_dma;
947 master->max_dma_len = MAX_SDMA_BD_BYTES;
948 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
949 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +0800950
951 return 0;
952err:
953 spi_imx_sdma_exit(spi_imx);
954 return ret;
955}
956
957static void spi_imx_dma_rx_callback(void *cookie)
958{
959 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
960
961 complete(&spi_imx->dma_rx_completion);
962}
963
964static void spi_imx_dma_tx_callback(void *cookie)
965{
966 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
967
968 complete(&spi_imx->dma_tx_completion);
969}
970
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100971static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
972{
973 unsigned long timeout = 0;
974
975 /* Time with actual data transfer and CS change delay related to HW */
976 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
977
978 /* Add extra second for scheduler related activities */
979 timeout += 1;
980
981 /* Double calculated timeout */
982 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
983}
984
Robin Gongf62cacc2014-09-11 09:18:44 +0800985static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
986 struct spi_transfer *transfer)
987{
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100988 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100989 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -0500990 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +0800991 struct spi_master *master = spi_imx->bitbang.master;
992 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
993
Anton Bondarenkofab44ef2015-12-05 17:57:00 +0100994 /*
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100995 * The TX DMA setup starts the transfer, so make sure RX is configured
996 * before TX.
Anton Bondarenkofab44ef2015-12-05 17:57:00 +0100997 */
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100998 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
999 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1000 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1001 if (!desc_rx)
1002 return -EINVAL;
1003
1004 desc_rx->callback = spi_imx_dma_rx_callback;
1005 desc_rx->callback_param = (void *)spi_imx;
1006 dmaengine_submit(desc_rx);
1007 reinit_completion(&spi_imx->dma_rx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001008 dma_async_issue_pending(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001009
1010 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1011 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1012 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1013 if (!desc_tx) {
1014 dmaengine_terminate_all(master->dma_tx);
1015 return -EINVAL;
1016 }
1017
1018 desc_tx->callback = spi_imx_dma_tx_callback;
1019 desc_tx->callback_param = (void *)spi_imx;
1020 dmaengine_submit(desc_tx);
1021 reinit_completion(&spi_imx->dma_tx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001022 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001023
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001024 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1025
Robin Gongf62cacc2014-09-11 09:18:44 +08001026 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001027 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001028 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001029 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001030 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001031 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001032 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001033 return -ETIMEDOUT;
Robin Gongf62cacc2014-09-11 09:18:44 +08001034 }
1035
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001036 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1037 transfer_timeout);
1038 if (!timeout) {
1039 dev_err(&master->dev, "I/O Error in DMA RX\n");
1040 spi_imx->devtype_data->reset(spi_imx);
1041 dmaengine_terminate_all(master->dma_rx);
1042 return -ETIMEDOUT;
1043 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001044
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001045 return transfer->len;
Robin Gongf62cacc2014-09-11 09:18:44 +08001046}
1047
1048static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001049 struct spi_transfer *transfer)
1050{
1051 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001052 unsigned long transfer_timeout;
1053 unsigned long timeout;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001054
1055 spi_imx->tx_buf = transfer->tx_buf;
1056 spi_imx->rx_buf = transfer->rx_buf;
1057 spi_imx->count = transfer->len;
1058 spi_imx->txfifo = 0;
1059
Axel Linaa0fe822014-02-09 11:06:04 +08001060 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001061
1062 spi_imx_push(spi_imx);
1063
Shawn Guoedd501bb2011-07-10 01:16:35 +08001064 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001065
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001066 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1067
1068 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1069 transfer_timeout);
1070 if (!timeout) {
1071 dev_err(&spi->dev, "I/O Error in PIO\n");
1072 spi_imx->devtype_data->reset(spi_imx);
1073 return -ETIMEDOUT;
1074 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001075
1076 return transfer->len;
1077}
1078
Robin Gongf62cacc2014-09-11 09:18:44 +08001079static int spi_imx_transfer(struct spi_device *spi,
1080 struct spi_transfer *transfer)
1081{
Robin Gongf62cacc2014-09-11 09:18:44 +08001082 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1083
Sascha Hauerc008a802016-02-24 09:20:26 +01001084 if (spi_imx->usedma)
Sascha Hauer99f1cf12016-02-23 10:23:50 +01001085 return spi_imx_dma_transfer(spi_imx, transfer);
Sascha Hauerc008a802016-02-24 09:20:26 +01001086 else
1087 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001088}
1089
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001090static int spi_imx_setup(struct spi_device *spi)
1091{
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001092 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001093 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1094
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001095 if (gpio_is_valid(spi->cs_gpio))
1096 gpio_direction_output(spi->cs_gpio,
1097 spi->mode & SPI_CS_HIGH ? 0 : 1);
Sascha Hauer6c23e5d2009-10-01 15:44:29 -07001098
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001099 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
1100
1101 return 0;
1102}
1103
1104static void spi_imx_cleanup(struct spi_device *spi)
1105{
1106}
1107
Huang Shijie9e556dc2013-10-23 16:31:50 +08001108static int
1109spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1110{
1111 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1112 int ret;
1113
1114 ret = clk_enable(spi_imx->clk_per);
1115 if (ret)
1116 return ret;
1117
1118 ret = clk_enable(spi_imx->clk_ipg);
1119 if (ret) {
1120 clk_disable(spi_imx->clk_per);
1121 return ret;
1122 }
1123
1124 return 0;
1125}
1126
1127static int
1128spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1129{
1130 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1131
1132 clk_disable(spi_imx->clk_ipg);
1133 clk_disable(spi_imx->clk_per);
1134 return 0;
1135}
1136
Grant Likelyfd4a3192012-12-07 16:57:14 +00001137static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001138{
Shawn Guo22a85e42011-07-10 01:16:41 +08001139 struct device_node *np = pdev->dev.of_node;
1140 const struct of_device_id *of_id =
1141 of_match_device(spi_imx_dt_ids, &pdev->dev);
1142 struct spi_imx_master *mxc_platform_info =
1143 dev_get_platdata(&pdev->dev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001144 struct spi_master *master;
1145 struct spi_imx_data *spi_imx;
1146 struct resource *res;
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001147 int i, ret, irq;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001148
Shawn Guo22a85e42011-07-10 01:16:41 +08001149 if (!np && !mxc_platform_info) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001150 dev_err(&pdev->dev, "can't get the platform data\n");
1151 return -EINVAL;
1152 }
1153
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001154 master = spi_alloc_master(&pdev->dev, sizeof(struct spi_imx_data));
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001155 if (!master)
1156 return -ENOMEM;
1157
1158 platform_set_drvdata(pdev, master);
1159
Stephen Warren24778be2013-05-21 20:36:35 -06001160 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001161 master->bus_num = np ? -1 : pdev->id;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001162
1163 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001164 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001165 spi_imx->dev = &pdev->dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001166
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001167 spi_imx->devtype_data = of_id ? of_id->data :
1168 (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
1169
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001170 if (mxc_platform_info) {
1171 master->num_chipselect = mxc_platform_info->num_chipselect;
1172 master->cs_gpios = devm_kzalloc(&master->dev,
1173 sizeof(int) * master->num_chipselect, GFP_KERNEL);
1174 if (!master->cs_gpios)
1175 return -ENOMEM;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001176
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001177 for (i = 0; i < master->num_chipselect; i++)
1178 master->cs_gpios[i] = mxc_platform_info->chipselect[i];
1179 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001180
1181 spi_imx->bitbang.chipselect = spi_imx_chipselect;
1182 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1183 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1184 spi_imx->bitbang.master->setup = spi_imx_setup;
1185 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001186 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1187 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001188 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1189 if (is_imx51_ecspi(spi_imx))
1190 spi_imx->bitbang.master->mode_bits |= SPI_LOOP;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001191
1192 init_completion(&spi_imx->xfer_done);
1193
1194 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001195 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1196 if (IS_ERR(spi_imx->base)) {
1197 ret = PTR_ERR(spi_imx->base);
1198 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001199 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001200 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001201
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001202 irq = platform_get_irq(pdev, 0);
1203 if (irq < 0) {
1204 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001205 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001206 }
1207
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001208 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001209 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001210 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001211 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001212 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001213 }
1214
Sascha Haueraa29d8402012-03-07 09:30:22 +01001215 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1216 if (IS_ERR(spi_imx->clk_ipg)) {
1217 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001218 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001219 }
1220
Sascha Haueraa29d8402012-03-07 09:30:22 +01001221 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1222 if (IS_ERR(spi_imx->clk_per)) {
1223 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001224 goto out_master_put;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001225 }
1226
Fabio Estevam83174622013-07-11 01:26:49 -03001227 ret = clk_prepare_enable(spi_imx->clk_per);
1228 if (ret)
1229 goto out_master_put;
1230
1231 ret = clk_prepare_enable(spi_imx->clk_ipg);
1232 if (ret)
1233 goto out_put_per;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001234
1235 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001236 /*
1237 * Only validated on i.mx6 now, can remove the constrain if validated on
1238 * other chips.
1239 */
Anton Bondarenko37600472015-12-08 07:43:45 +01001240 if (is_imx51_ecspi(spi_imx)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001241 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001242 if (ret == -EPROBE_DEFER)
1243 goto out_clk_put;
1244
Anton Bondarenko37600472015-12-08 07:43:45 +01001245 if (ret < 0)
1246 dev_err(&pdev->dev, "dma setup error %d, use pio\n",
1247 ret);
1248 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001249
Shawn Guoedd501bb2011-07-10 01:16:35 +08001250 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001251
Shawn Guoedd501bb2011-07-10 01:16:35 +08001252 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001253
Shawn Guo22a85e42011-07-10 01:16:41 +08001254 master->dev.of_node = pdev->dev.of_node;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001255 ret = spi_bitbang_start(&spi_imx->bitbang);
1256 if (ret) {
1257 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
1258 goto out_clk_put;
1259 }
1260
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001261 for (i = 0; i < master->num_chipselect; i++) {
1262 if (!gpio_is_valid(master->cs_gpios[i]))
1263 continue;
1264
1265 ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
1266 DRIVER_NAME);
1267 if (ret) {
1268 dev_err(&pdev->dev, "Can't get CS GPIO %i\n",
1269 master->cs_gpios[i]);
1270 goto out_clk_put;
1271 }
1272 }
1273
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001274 dev_info(&pdev->dev, "probed\n");
1275
Huang Shijie9e556dc2013-10-23 16:31:50 +08001276 clk_disable(spi_imx->clk_ipg);
1277 clk_disable(spi_imx->clk_per);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001278 return ret;
1279
1280out_clk_put:
Sascha Haueraa29d8402012-03-07 09:30:22 +01001281 clk_disable_unprepare(spi_imx->clk_ipg);
Fabio Estevam83174622013-07-11 01:26:49 -03001282out_put_per:
1283 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001284out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001285 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001286
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001287 return ret;
1288}
1289
Grant Likelyfd4a3192012-12-07 16:57:14 +00001290static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001291{
1292 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001293 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001294
1295 spi_bitbang_stop(&spi_imx->bitbang);
1296
1297 writel(0, spi_imx->base + MXC_CSPICTRL);
Philippe De Muyterfd40dcc2014-02-27 10:16:15 +01001298 clk_unprepare(spi_imx->clk_ipg);
1299 clk_unprepare(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001300 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001301 spi_master_put(master);
1302
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001303 return 0;
1304}
1305
1306static struct platform_driver spi_imx_driver = {
1307 .driver = {
1308 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001309 .of_match_table = spi_imx_dt_ids,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001310 },
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001311 .id_table = spi_imx_devtype,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001312 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001313 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001314};
Grant Likely940ab882011-10-05 11:29:49 -06001315module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001316
1317MODULE_DESCRIPTION("SPI Master Controller driver");
1318MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1319MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001320MODULE_ALIAS("platform:" DRIVER_NAME);