blob: cbcfb8b70b21c67c548f985b122bd19bab55c005 [file] [log] [blame]
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001/*
2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright (C) 2008 Juergen Beisert
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/clk.h>
22#include <linux/completion.h>
23#include <linux/delay.h>
Robin Gongf62cacc2014-09-11 09:18:44 +080024#include <linux/dmaengine.h>
25#include <linux/dma-mapping.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070026#include <linux/err.h>
27#include <linux/gpio.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070028#include <linux/interrupt.h>
29#include <linux/io.h>
30#include <linux/irq.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070035#include <linux/spi/spi.h>
36#include <linux/spi/spi_bitbang.h>
37#include <linux/types.h>
Shawn Guo22a85e42011-07-10 01:16:41 +080038#include <linux/of.h>
39#include <linux/of_device.h>
40#include <linux/of_gpio.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070041
Robin Gongf62cacc2014-09-11 09:18:44 +080042#include <linux/platform_data/dma-imx.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020043#include <linux/platform_data/spi-imx.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070044
45#define DRIVER_NAME "spi_imx"
46
47#define MXC_CSPIRXDATA 0x00
48#define MXC_CSPITXDATA 0x04
49#define MXC_CSPICTRL 0x08
50#define MXC_CSPIINT 0x0c
51#define MXC_RESET 0x1c
52
53/* generic defines to abstract from the different register layouts */
54#define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
55#define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
56
Robin Gongf62cacc2014-09-11 09:18:44 +080057/* The maximum bytes that a sdma BD can transfer.*/
58#define MAX_SDMA_BD_BYTES (1 << 15)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070059struct spi_imx_config {
60 unsigned int speed_hz;
61 unsigned int bpw;
62 unsigned int mode;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070063};
64
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020065enum spi_imx_devtype {
Shawn Guo04ee5852011-07-10 01:16:39 +080066 IMX1_CSPI,
67 IMX21_CSPI,
68 IMX27_CSPI,
69 IMX31_CSPI,
70 IMX35_CSPI, /* CSPI on all i.mx except above */
71 IMX51_ECSPI, /* ECSPI on i.mx51 and later */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020072};
73
74struct spi_imx_data;
75
76struct spi_imx_devtype_data {
77 void (*intctrl)(struct spi_imx_data *, int);
Alexander Shiyanb36581d2016-06-08 20:02:06 +030078 int (*config)(struct spi_device *, struct spi_imx_config *);
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020079 void (*trigger)(struct spi_imx_data *);
80 int (*rx_available)(struct spi_imx_data *);
Uwe Kleine-König1723e662010-09-10 09:19:18 +020081 void (*reset)(struct spi_imx_data *);
Shawn Guo04ee5852011-07-10 01:16:39 +080082 enum spi_imx_devtype devtype;
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020083};
84
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070085struct spi_imx_data {
86 struct spi_bitbang bitbang;
Sascha Hauer6aa800c2016-02-17 14:28:48 +010087 struct device *dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070088
89 struct completion xfer_done;
Uwe Kleine-Königcc4d22a2012-03-29 21:54:18 +020090 void __iomem *base;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010091 unsigned long base_phys;
92
Sascha Haueraa29d842012-03-07 09:30:22 +010093 struct clk *clk_per;
94 struct clk *clk_ipg;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070095 unsigned long spi_clk;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +010096 unsigned int spi_bus_clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070097
Anton Bondarenkof12ae172016-02-24 09:20:29 +010098 unsigned int bytes_per_word;
99
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700100 unsigned int count;
101 void (*tx)(struct spi_imx_data *);
102 void (*rx)(struct spi_imx_data *);
103 void *rx_buf;
104 const void *tx_buf;
105 unsigned int txfifo; /* number of words pushed in tx FIFO */
106
Robin Gongf62cacc2014-09-11 09:18:44 +0800107 /* DMA */
Robin Gongf62cacc2014-09-11 09:18:44 +0800108 bool usedma;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100109 u32 wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800110 struct completion dma_rx_completion;
111 struct completion dma_tx_completion;
112
Uwe Kleine-König80023cb2012-05-21 21:49:35 +0200113 const struct spi_imx_devtype_data *devtype_data;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700114};
115
Shawn Guo04ee5852011-07-10 01:16:39 +0800116static inline int is_imx27_cspi(struct spi_imx_data *d)
117{
118 return d->devtype_data->devtype == IMX27_CSPI;
119}
120
121static inline int is_imx35_cspi(struct spi_imx_data *d)
122{
123 return d->devtype_data->devtype == IMX35_CSPI;
124}
125
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100126static inline int is_imx51_ecspi(struct spi_imx_data *d)
127{
128 return d->devtype_data->devtype == IMX51_ECSPI;
129}
130
Shawn Guo04ee5852011-07-10 01:16:39 +0800131static inline unsigned spi_imx_get_fifosize(struct spi_imx_data *d)
132{
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100133 return is_imx51_ecspi(d) ? 64 : 8;
Shawn Guo04ee5852011-07-10 01:16:39 +0800134}
135
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700136#define MXC_SPI_BUF_RX(type) \
137static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
138{ \
139 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
140 \
141 if (spi_imx->rx_buf) { \
142 *(type *)spi_imx->rx_buf = val; \
143 spi_imx->rx_buf += sizeof(type); \
144 } \
145}
146
147#define MXC_SPI_BUF_TX(type) \
148static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
149{ \
150 type val = 0; \
151 \
152 if (spi_imx->tx_buf) { \
153 val = *(type *)spi_imx->tx_buf; \
154 spi_imx->tx_buf += sizeof(type); \
155 } \
156 \
157 spi_imx->count -= sizeof(type); \
158 \
159 writel(val, spi_imx->base + MXC_CSPITXDATA); \
160}
161
162MXC_SPI_BUF_RX(u8)
163MXC_SPI_BUF_TX(u8)
164MXC_SPI_BUF_RX(u16)
165MXC_SPI_BUF_TX(u16)
166MXC_SPI_BUF_RX(u32)
167MXC_SPI_BUF_TX(u32)
168
169/* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
170 * (which is currently not the case in this driver)
171 */
172static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
173 256, 384, 512, 768, 1024};
174
175/* MX21, MX27 */
176static unsigned int spi_imx_clkdiv_1(unsigned int fin,
Shawn Guo04ee5852011-07-10 01:16:39 +0800177 unsigned int fspi, unsigned int max)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700178{
Shawn Guo04ee5852011-07-10 01:16:39 +0800179 int i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700180
181 for (i = 2; i < max; i++)
182 if (fspi * mxc_clkdivs[i] >= fin)
183 return i;
184
185 return max;
186}
187
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200188/* MX1, MX31, MX35, MX51 CSPI */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700189static unsigned int spi_imx_clkdiv_2(unsigned int fin,
190 unsigned int fspi)
191{
192 int i, div = 4;
193
194 for (i = 0; i < 7; i++) {
195 if (fspi * div >= fin)
196 return i;
197 div <<= 1;
198 }
199
200 return 7;
201}
202
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100203static int spi_imx_bytes_per_word(const int bpw)
204{
205 return DIV_ROUND_UP(bpw, BITS_PER_BYTE);
206}
207
Robin Gongf62cacc2014-09-11 09:18:44 +0800208static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
209 struct spi_transfer *transfer)
210{
211 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Sascha Hauercd8dd412016-03-17 09:21:50 +0100212 unsigned int bpw;
Robin Gongf62cacc2014-09-11 09:18:44 +0800213
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100214 if (!master->dma_rx)
215 return false;
216
Sascha Hauercd8dd412016-03-17 09:21:50 +0100217 if (!transfer)
218 return false;
219
220 bpw = transfer->bits_per_word;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100221 if (!bpw)
222 bpw = spi->bits_per_word;
223
224 bpw = spi_imx_bytes_per_word(bpw);
225
226 if (bpw != 1 && bpw != 2 && bpw != 4)
227 return false;
228
229 if (transfer->len < spi_imx->wml * bpw)
230 return false;
231
232 if (transfer->len % (spi_imx->wml * bpw))
233 return false;
234
235 return true;
Robin Gongf62cacc2014-09-11 09:18:44 +0800236}
237
Shawn Guo66de7572011-07-10 01:16:37 +0800238#define MX51_ECSPI_CTRL 0x08
239#define MX51_ECSPI_CTRL_ENABLE (1 << 0)
240#define MX51_ECSPI_CTRL_XCH (1 << 2)
Robin Gongf62cacc2014-09-11 09:18:44 +0800241#define MX51_ECSPI_CTRL_SMC (1 << 3)
Shawn Guo66de7572011-07-10 01:16:37 +0800242#define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
243#define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
244#define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
245#define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
246#define MX51_ECSPI_CTRL_BL_OFFSET 20
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200247
Shawn Guo66de7572011-07-10 01:16:37 +0800248#define MX51_ECSPI_CONFIG 0x0c
249#define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
250#define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
251#define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
252#define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200253#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200254
Shawn Guo66de7572011-07-10 01:16:37 +0800255#define MX51_ECSPI_INT 0x10
256#define MX51_ECSPI_INT_TEEN (1 << 0)
257#define MX51_ECSPI_INT_RREN (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200258
Robin Gongf62cacc2014-09-11 09:18:44 +0800259#define MX51_ECSPI_DMA 0x14
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100260#define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
261#define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
262#define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
Robin Gongf62cacc2014-09-11 09:18:44 +0800263
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100264#define MX51_ECSPI_DMA_TEDEN (1 << 7)
265#define MX51_ECSPI_DMA_RXDEN (1 << 23)
266#define MX51_ECSPI_DMA_RXTDEN (1 << 31)
Robin Gongf62cacc2014-09-11 09:18:44 +0800267
Shawn Guo66de7572011-07-10 01:16:37 +0800268#define MX51_ECSPI_STAT 0x18
269#define MX51_ECSPI_STAT_RR (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200270
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200271#define MX51_ECSPI_TESTREG 0x20
272#define MX51_ECSPI_TESTREG_LBC BIT(31)
273
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200274/* MX51 eCSPI */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100275static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
276 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200277{
278 /*
279 * there are two 4-bit dividers, the pre-divider divides by
280 * $pre, the post-divider by 2^$post
281 */
282 unsigned int pre, post;
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100283 unsigned int fin = spi_imx->spi_clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200284
285 if (unlikely(fspi > fin))
286 return 0;
287
288 post = fls(fin) - fls(fspi);
289 if (fin > fspi << post)
290 post++;
291
292 /* now we have: (fin <= fspi << post) with post being minimal */
293
294 post = max(4U, post) - 4;
295 if (unlikely(post > 0xf)) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100296 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
297 fspi, fin);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200298 return 0xff;
299 }
300
301 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
302
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100303 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200304 __func__, fin, fspi, post, pre);
Marek Vasut6fd8b852013-12-18 18:31:47 +0100305
306 /* Resulting frequency for the SCLK line. */
307 *fres = (fin / (pre + 1)) >> post;
308
Shawn Guo66de7572011-07-10 01:16:37 +0800309 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
310 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200311}
312
Shawn Guo66de7572011-07-10 01:16:37 +0800313static void __maybe_unused mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200314{
315 unsigned val = 0;
316
317 if (enable & MXC_INT_TE)
Shawn Guo66de7572011-07-10 01:16:37 +0800318 val |= MX51_ECSPI_INT_TEEN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200319
320 if (enable & MXC_INT_RR)
Shawn Guo66de7572011-07-10 01:16:37 +0800321 val |= MX51_ECSPI_INT_RREN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200322
Shawn Guo66de7572011-07-10 01:16:37 +0800323 writel(val, spi_imx->base + MX51_ECSPI_INT);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200324}
325
Shawn Guo66de7572011-07-10 01:16:37 +0800326static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200327{
Sascha Hauerb03c3882016-02-24 09:20:32 +0100328 u32 reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200329
Sascha Hauerb03c3882016-02-24 09:20:32 +0100330 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
331 reg |= MX51_ECSPI_CTRL_XCH;
Shawn Guo66de7572011-07-10 01:16:37 +0800332 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200333}
334
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300335static int __maybe_unused mx51_ecspi_config(struct spi_device *spi,
336 struct spi_imx_config *config)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200337{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300338 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100339 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200340 u32 clk = config->speed_hz, delay, reg;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100341 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200342
Sascha Hauerf020c392011-02-08 21:08:59 +0100343 /*
344 * The hardware seems to have a race condition when changing modes. The
345 * current assumption is that the selection of the channel arrives
346 * earlier in the hardware than the mode bits when they are written at
347 * the same time.
348 * So set master mode for all channels as we do not support slave mode.
349 */
Shawn Guo66de7572011-07-10 01:16:37 +0800350 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200351
352 /* set clock speed */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100353 ctrl |= mx51_ecspi_clkdiv(spi_imx, config->speed_hz, &clk);
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100354 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200355
356 /* set chip select to use */
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300357 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200358
Shawn Guo66de7572011-07-10 01:16:37 +0800359 ctrl |= (config->bpw - 1) << MX51_ECSPI_CTRL_BL_OFFSET;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200360
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300361 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200362
363 if (config->mode & SPI_CPHA)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300364 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100365 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300366 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200367
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200368 if (config->mode & SPI_CPOL) {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300369 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
370 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100371 } else {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300372 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
373 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200374 }
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200375 if (config->mode & SPI_CS_HIGH)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300376 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100377 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300378 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200379
Sascha Hauerb03c3882016-02-24 09:20:32 +0100380 if (spi_imx->usedma)
381 ctrl |= MX51_ECSPI_CTRL_SMC;
382
Anton Bondarenkof677f172015-12-08 07:43:43 +0100383 /* CTRL register always go first to bring out controller from reset */
384 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
385
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200386 reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
387 if (config->mode & SPI_LOOP)
388 reg |= MX51_ECSPI_TESTREG_LBC;
389 else
390 reg &= ~MX51_ECSPI_TESTREG_LBC;
391 writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
392
Shawn Guo66de7572011-07-10 01:16:37 +0800393 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200394
Marek Vasut6fd8b852013-12-18 18:31:47 +0100395 /*
396 * Wait until the changes in the configuration register CONFIGREG
397 * propagate into the hardware. It takes exactly one tick of the
398 * SCLK clock, but we will wait two SCLK clock just to be sure. The
399 * effect of the delay it takes for the hardware to apply changes
400 * is noticable if the SCLK clock run very slow. In such a case, if
401 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
402 * be asserted before the SCLK polarity changes, which would disrupt
403 * the SPI communication as the device on the other end would consider
404 * the change of SCLK polarity as a clock tick already.
405 */
406 delay = (2 * 1000000) / clk;
407 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
408 udelay(delay);
409 else /* SCLK is _very_ slow */
410 usleep_range(delay, delay + 10);
411
Robin Gongf62cacc2014-09-11 09:18:44 +0800412 /*
413 * Configure the DMA register: setup the watermark
414 * and enable DMA request.
415 */
Robin Gongf62cacc2014-09-11 09:18:44 +0800416
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100417 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
418 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
419 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100420 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
421 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Robin Gongf62cacc2014-09-11 09:18:44 +0800422
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200423 return 0;
424}
425
Shawn Guo66de7572011-07-10 01:16:37 +0800426static int __maybe_unused mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200427{
Shawn Guo66de7572011-07-10 01:16:37 +0800428 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200429}
430
Shawn Guo66de7572011-07-10 01:16:37 +0800431static void __maybe_unused mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200432{
433 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800434 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200435 readl(spi_imx->base + MXC_CSPIRXDATA);
436}
437
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700438#define MX31_INTREG_TEEN (1 << 0)
439#define MX31_INTREG_RREN (1 << 3)
440
441#define MX31_CSPICTRL_ENABLE (1 << 0)
442#define MX31_CSPICTRL_MASTER (1 << 1)
443#define MX31_CSPICTRL_XCH (1 << 2)
444#define MX31_CSPICTRL_POL (1 << 4)
445#define MX31_CSPICTRL_PHA (1 << 5)
446#define MX31_CSPICTRL_SSCTL (1 << 6)
447#define MX31_CSPICTRL_SSPOL (1 << 7)
448#define MX31_CSPICTRL_BC_SHIFT 8
449#define MX35_CSPICTRL_BL_SHIFT 20
450#define MX31_CSPICTRL_CS_SHIFT 24
451#define MX35_CSPICTRL_CS_SHIFT 12
452#define MX31_CSPICTRL_DR_SHIFT 16
453
454#define MX31_CSPISTATUS 0x14
455#define MX31_STATUS_RR (1 << 3)
456
457/* These functions also work for the i.MX35, but be aware that
458 * the i.MX35 has a slightly different register layout for bits
459 * we do not use here.
460 */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200461static void __maybe_unused mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700462{
463 unsigned int val = 0;
464
465 if (enable & MXC_INT_TE)
466 val |= MX31_INTREG_TEEN;
467 if (enable & MXC_INT_RR)
468 val |= MX31_INTREG_RREN;
469
470 writel(val, spi_imx->base + MXC_CSPIINT);
471}
472
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200473static void __maybe_unused mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700474{
475 unsigned int reg;
476
477 reg = readl(spi_imx->base + MXC_CSPICTRL);
478 reg |= MX31_CSPICTRL_XCH;
479 writel(reg, spi_imx->base + MXC_CSPICTRL);
480}
481
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300482static int __maybe_unused mx31_config(struct spi_device *spi,
483 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;
487
488 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
489 MX31_CSPICTRL_DR_SHIFT;
490
Shawn Guo04ee5852011-07-10 01:16:39 +0800491 if (is_imx35_cspi(spi_imx)) {
Shawn Guo2a64a902011-07-10 01:16:38 +0800492 reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
493 reg |= MX31_CSPICTRL_SSCTL;
494 } else {
495 reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
496 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700497
498 if (config->mode & SPI_CPHA)
499 reg |= MX31_CSPICTRL_PHA;
500 if (config->mode & SPI_CPOL)
501 reg |= MX31_CSPICTRL_POL;
502 if (config->mode & SPI_CS_HIGH)
503 reg |= MX31_CSPICTRL_SSPOL;
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300504 if (spi->cs_gpio < 0)
505 reg |= (spi->cs_gpio + 32) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800506 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
507 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200508
509 writel(reg, spi_imx->base + MXC_CSPICTRL);
510
511 return 0;
512}
513
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200514static int __maybe_unused mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700515{
516 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
517}
518
Shawn Guo2a64a902011-07-10 01:16:38 +0800519static void __maybe_unused mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200520{
521 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800522 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200523 readl(spi_imx->base + MXC_CSPIRXDATA);
524}
525
Shawn Guo3451fb12011-07-10 01:16:36 +0800526#define MX21_INTREG_RR (1 << 4)
527#define MX21_INTREG_TEEN (1 << 9)
528#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700529
Shawn Guo3451fb12011-07-10 01:16:36 +0800530#define MX21_CSPICTRL_POL (1 << 5)
531#define MX21_CSPICTRL_PHA (1 << 6)
532#define MX21_CSPICTRL_SSPOL (1 << 8)
533#define MX21_CSPICTRL_XCH (1 << 9)
534#define MX21_CSPICTRL_ENABLE (1 << 10)
535#define MX21_CSPICTRL_MASTER (1 << 11)
536#define MX21_CSPICTRL_DR_SHIFT 14
537#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700538
Shawn Guo3451fb12011-07-10 01:16:36 +0800539static void __maybe_unused mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700540{
541 unsigned int val = 0;
542
543 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800544 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700545 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800546 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700547
548 writel(val, spi_imx->base + MXC_CSPIINT);
549}
550
Shawn Guo3451fb12011-07-10 01:16:36 +0800551static void __maybe_unused mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700552{
553 unsigned int reg;
554
555 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800556 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700557 writel(reg, spi_imx->base + MXC_CSPICTRL);
558}
559
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300560static int __maybe_unused mx21_config(struct spi_device *spi,
561 struct spi_imx_config *config)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700562{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300563 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Shawn Guo3451fb12011-07-10 01:16:36 +0800564 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Shawn Guo04ee5852011-07-10 01:16:39 +0800565 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700566
Shawn Guo04ee5852011-07-10 01:16:39 +0800567 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) <<
Shawn Guo3451fb12011-07-10 01:16:36 +0800568 MX21_CSPICTRL_DR_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700569 reg |= config->bpw - 1;
570
571 if (config->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800572 reg |= MX21_CSPICTRL_PHA;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700573 if (config->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800574 reg |= MX21_CSPICTRL_POL;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700575 if (config->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800576 reg |= MX21_CSPICTRL_SSPOL;
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300577 if (spi->cs_gpio < 0)
578 reg |= (spi->cs_gpio + 32) << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700579
580 writel(reg, spi_imx->base + MXC_CSPICTRL);
581
582 return 0;
583}
584
Shawn Guo3451fb12011-07-10 01:16:36 +0800585static int __maybe_unused mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700586{
Shawn Guo3451fb12011-07-10 01:16:36 +0800587 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700588}
589
Shawn Guo3451fb12011-07-10 01:16:36 +0800590static void __maybe_unused mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200591{
592 writel(1, spi_imx->base + MXC_RESET);
593}
594
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700595#define MX1_INTREG_RR (1 << 3)
596#define MX1_INTREG_TEEN (1 << 8)
597#define MX1_INTREG_RREN (1 << 11)
598
599#define MX1_CSPICTRL_POL (1 << 4)
600#define MX1_CSPICTRL_PHA (1 << 5)
601#define MX1_CSPICTRL_XCH (1 << 8)
602#define MX1_CSPICTRL_ENABLE (1 << 9)
603#define MX1_CSPICTRL_MASTER (1 << 10)
604#define MX1_CSPICTRL_DR_SHIFT 13
605
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200606static void __maybe_unused mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700607{
608 unsigned int val = 0;
609
610 if (enable & MXC_INT_TE)
611 val |= MX1_INTREG_TEEN;
612 if (enable & MXC_INT_RR)
613 val |= MX1_INTREG_RREN;
614
615 writel(val, spi_imx->base + MXC_CSPIINT);
616}
617
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200618static void __maybe_unused mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700619{
620 unsigned int reg;
621
622 reg = readl(spi_imx->base + MXC_CSPICTRL);
623 reg |= MX1_CSPICTRL_XCH;
624 writel(reg, spi_imx->base + MXC_CSPICTRL);
625}
626
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300627static int __maybe_unused mx1_config(struct spi_device *spi,
628 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;
632
633 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
634 MX1_CSPICTRL_DR_SHIFT;
635 reg |= config->bpw - 1;
636
637 if (config->mode & SPI_CPHA)
638 reg |= MX1_CSPICTRL_PHA;
639 if (config->mode & SPI_CPOL)
640 reg |= MX1_CSPICTRL_POL;
641
642 writel(reg, spi_imx->base + MXC_CSPICTRL);
643
644 return 0;
645}
646
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200647static int __maybe_unused mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700648{
649 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
650}
651
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200652static void __maybe_unused mx1_reset(struct spi_imx_data *spi_imx)
653{
654 writel(1, spi_imx->base + MXC_RESET);
655}
656
Shawn Guo04ee5852011-07-10 01:16:39 +0800657static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
658 .intctrl = mx1_intctrl,
659 .config = mx1_config,
660 .trigger = mx1_trigger,
661 .rx_available = mx1_rx_available,
662 .reset = mx1_reset,
663 .devtype = IMX1_CSPI,
664};
665
666static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
667 .intctrl = mx21_intctrl,
668 .config = mx21_config,
669 .trigger = mx21_trigger,
670 .rx_available = mx21_rx_available,
671 .reset = mx21_reset,
672 .devtype = IMX21_CSPI,
673};
674
675static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
676 /* i.mx27 cspi shares the functions with i.mx21 one */
677 .intctrl = mx21_intctrl,
678 .config = mx21_config,
679 .trigger = mx21_trigger,
680 .rx_available = mx21_rx_available,
681 .reset = mx21_reset,
682 .devtype = IMX27_CSPI,
683};
684
685static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
686 .intctrl = mx31_intctrl,
687 .config = mx31_config,
688 .trigger = mx31_trigger,
689 .rx_available = mx31_rx_available,
690 .reset = mx31_reset,
691 .devtype = IMX31_CSPI,
692};
693
694static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
695 /* i.mx35 and later cspi shares the functions with i.mx31 one */
696 .intctrl = mx31_intctrl,
697 .config = mx31_config,
698 .trigger = mx31_trigger,
699 .rx_available = mx31_rx_available,
700 .reset = mx31_reset,
701 .devtype = IMX35_CSPI,
702};
703
704static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
705 .intctrl = mx51_ecspi_intctrl,
706 .config = mx51_ecspi_config,
707 .trigger = mx51_ecspi_trigger,
708 .rx_available = mx51_ecspi_rx_available,
709 .reset = mx51_ecspi_reset,
710 .devtype = IMX51_ECSPI,
711};
712
Krzysztof Kozlowskidb1b8202015-05-02 00:44:04 +0900713static const struct platform_device_id spi_imx_devtype[] = {
Shawn Guo04ee5852011-07-10 01:16:39 +0800714 {
715 .name = "imx1-cspi",
716 .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
717 }, {
718 .name = "imx21-cspi",
719 .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
720 }, {
721 .name = "imx27-cspi",
722 .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
723 }, {
724 .name = "imx31-cspi",
725 .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
726 }, {
727 .name = "imx35-cspi",
728 .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
729 }, {
730 .name = "imx51-ecspi",
731 .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
732 }, {
733 /* sentinel */
734 }
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200735};
736
Shawn Guo22a85e42011-07-10 01:16:41 +0800737static const struct of_device_id spi_imx_dt_ids[] = {
738 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
739 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
740 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
741 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
742 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
743 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
744 { /* sentinel */ }
745};
Niels de Vos27743e02013-07-29 09:38:05 +0200746MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +0800747
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700748static void spi_imx_chipselect(struct spi_device *spi, int is_active)
749{
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700750 int active = is_active != BITBANG_CS_INACTIVE;
751 int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700752
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300753 if (!gpio_is_valid(spi->cs_gpio))
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700754 return;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700755
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300756 gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700757}
758
759static void spi_imx_push(struct spi_imx_data *spi_imx)
760{
Shawn Guo04ee5852011-07-10 01:16:39 +0800761 while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700762 if (!spi_imx->count)
763 break;
764 spi_imx->tx(spi_imx);
765 spi_imx->txfifo++;
766 }
767
Shawn Guoedd501bb2011-07-10 01:16:35 +0800768 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700769}
770
771static irqreturn_t spi_imx_isr(int irq, void *dev_id)
772{
773 struct spi_imx_data *spi_imx = dev_id;
774
Shawn Guoedd501bb2011-07-10 01:16:35 +0800775 while (spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700776 spi_imx->rx(spi_imx);
777 spi_imx->txfifo--;
778 }
779
780 if (spi_imx->count) {
781 spi_imx_push(spi_imx);
782 return IRQ_HANDLED;
783 }
784
785 if (spi_imx->txfifo) {
786 /* No data left to push, but still waiting for rx data,
787 * enable receive data available interrupt.
788 */
Shawn Guoedd501bb2011-07-10 01:16:35 +0800789 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200790 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700791 return IRQ_HANDLED;
792 }
793
Shawn Guoedd501bb2011-07-10 01:16:35 +0800794 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700795 complete(&spi_imx->xfer_done);
796
797 return IRQ_HANDLED;
798}
799
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100800static int spi_imx_dma_configure(struct spi_master *master,
801 int bytes_per_word)
802{
803 int ret;
804 enum dma_slave_buswidth buswidth;
805 struct dma_slave_config rx = {}, tx = {};
806 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
807
808 if (bytes_per_word == spi_imx->bytes_per_word)
809 /* Same as last time */
810 return 0;
811
812 switch (bytes_per_word) {
813 case 4:
814 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
815 break;
816 case 2:
817 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
818 break;
819 case 1:
820 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
821 break;
822 default:
823 return -EINVAL;
824 }
825
826 tx.direction = DMA_MEM_TO_DEV;
827 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
828 tx.dst_addr_width = buswidth;
829 tx.dst_maxburst = spi_imx->wml;
830 ret = dmaengine_slave_config(master->dma_tx, &tx);
831 if (ret) {
832 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
833 return ret;
834 }
835
836 rx.direction = DMA_DEV_TO_MEM;
837 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
838 rx.src_addr_width = buswidth;
839 rx.src_maxburst = spi_imx->wml;
840 ret = dmaengine_slave_config(master->dma_rx, &rx);
841 if (ret) {
842 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
843 return ret;
844 }
845
846 spi_imx->bytes_per_word = bytes_per_word;
847
848 return 0;
849}
850
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700851static int spi_imx_setupxfer(struct spi_device *spi,
852 struct spi_transfer *t)
853{
854 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
855 struct spi_imx_config config;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100856 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700857
858 config.bpw = t ? t->bits_per_word : spi->bits_per_word;
859 config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
860 config.mode = spi->mode;
861
Sascha Hauer462d26b2009-10-01 15:44:29 -0700862 if (!config.speed_hz)
863 config.speed_hz = spi->max_speed_hz;
864 if (!config.bpw)
865 config.bpw = spi->bits_per_word;
Sascha Hauer462d26b2009-10-01 15:44:29 -0700866
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700867 /* Initialize the functions for transfer */
868 if (config.bpw <= 8) {
869 spi_imx->rx = spi_imx_buf_rx_u8;
870 spi_imx->tx = spi_imx_buf_tx_u8;
871 } else if (config.bpw <= 16) {
872 spi_imx->rx = spi_imx_buf_rx_u16;
873 spi_imx->tx = spi_imx_buf_tx_u16;
Sachin Kamat60514262013-05-30 13:38:09 +0530874 } else {
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700875 spi_imx->rx = spi_imx_buf_rx_u32;
876 spi_imx->tx = spi_imx_buf_tx_u32;
Stephen Warren24778be2013-05-21 20:36:35 -0600877 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700878
Sascha Hauerc008a802016-02-24 09:20:26 +0100879 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
880 spi_imx->usedma = 1;
881 else
882 spi_imx->usedma = 0;
883
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100884 if (spi_imx->usedma) {
885 ret = spi_imx_dma_configure(spi->master,
886 spi_imx_bytes_per_word(config.bpw));
887 if (ret)
888 return ret;
889 }
890
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300891 spi_imx->devtype_data->config(spi, &config);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700892
893 return 0;
894}
895
Robin Gongf62cacc2014-09-11 09:18:44 +0800896static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
897{
898 struct spi_master *master = spi_imx->bitbang.master;
899
900 if (master->dma_rx) {
901 dma_release_channel(master->dma_rx);
902 master->dma_rx = NULL;
903 }
904
905 if (master->dma_tx) {
906 dma_release_channel(master->dma_tx);
907 master->dma_tx = NULL;
908 }
Robin Gongf62cacc2014-09-11 09:18:44 +0800909}
910
911static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100912 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +0800913{
Robin Gongf62cacc2014-09-11 09:18:44 +0800914 int ret;
915
Robin Gonga02bb402015-02-03 10:25:53 +0800916 /* use pio mode for i.mx6dl chip TKT238285 */
917 if (of_machine_is_compatible("fsl,imx6dl"))
918 return 0;
919
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100920 spi_imx->wml = spi_imx_get_fifosize(spi_imx) / 2;
921
Robin Gongf62cacc2014-09-11 09:18:44 +0800922 /* Prepare for TX DMA: */
Anton Bondarenko37600472015-12-08 07:43:45 +0100923 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
924 if (IS_ERR(master->dma_tx)) {
925 ret = PTR_ERR(master->dma_tx);
926 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
927 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800928 goto err;
929 }
930
Robin Gongf62cacc2014-09-11 09:18:44 +0800931 /* Prepare for RX : */
Anton Bondarenko37600472015-12-08 07:43:45 +0100932 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
933 if (IS_ERR(master->dma_rx)) {
934 ret = PTR_ERR(master->dma_rx);
935 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
936 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +0800937 goto err;
938 }
939
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100940 spi_imx_dma_configure(master, 1);
Robin Gongf62cacc2014-09-11 09:18:44 +0800941
942 init_completion(&spi_imx->dma_rx_completion);
943 init_completion(&spi_imx->dma_tx_completion);
944 master->can_dma = spi_imx_can_dma;
945 master->max_dma_len = MAX_SDMA_BD_BYTES;
946 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
947 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +0800948
949 return 0;
950err:
951 spi_imx_sdma_exit(spi_imx);
952 return ret;
953}
954
955static void spi_imx_dma_rx_callback(void *cookie)
956{
957 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
958
959 complete(&spi_imx->dma_rx_completion);
960}
961
962static void spi_imx_dma_tx_callback(void *cookie)
963{
964 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
965
966 complete(&spi_imx->dma_tx_completion);
967}
968
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100969static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
970{
971 unsigned long timeout = 0;
972
973 /* Time with actual data transfer and CS change delay related to HW */
974 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
975
976 /* Add extra second for scheduler related activities */
977 timeout += 1;
978
979 /* Double calculated timeout */
980 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
981}
982
Robin Gongf62cacc2014-09-11 09:18:44 +0800983static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
984 struct spi_transfer *transfer)
985{
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100986 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100987 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -0500988 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +0800989 struct spi_master *master = spi_imx->bitbang.master;
990 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
991
Anton Bondarenkofab44ef2015-12-05 17:57:00 +0100992 /*
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100993 * The TX DMA setup starts the transfer, so make sure RX is configured
994 * before TX.
Anton Bondarenkofab44ef2015-12-05 17:57:00 +0100995 */
Sascha Hauer6b6192c2016-02-24 09:20:33 +0100996 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
997 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
998 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
999 if (!desc_rx)
1000 return -EINVAL;
1001
1002 desc_rx->callback = spi_imx_dma_rx_callback;
1003 desc_rx->callback_param = (void *)spi_imx;
1004 dmaengine_submit(desc_rx);
1005 reinit_completion(&spi_imx->dma_rx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001006 dma_async_issue_pending(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001007
1008 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1009 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1010 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1011 if (!desc_tx) {
1012 dmaengine_terminate_all(master->dma_tx);
1013 return -EINVAL;
1014 }
1015
1016 desc_tx->callback = spi_imx_dma_tx_callback;
1017 desc_tx->callback_param = (void *)spi_imx;
1018 dmaengine_submit(desc_tx);
1019 reinit_completion(&spi_imx->dma_tx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001020 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001021
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001022 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1023
Robin Gongf62cacc2014-09-11 09:18:44 +08001024 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001025 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001026 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001027 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001028 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001029 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001030 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001031 return -ETIMEDOUT;
Robin Gongf62cacc2014-09-11 09:18:44 +08001032 }
1033
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001034 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1035 transfer_timeout);
1036 if (!timeout) {
1037 dev_err(&master->dev, "I/O Error in DMA RX\n");
1038 spi_imx->devtype_data->reset(spi_imx);
1039 dmaengine_terminate_all(master->dma_rx);
1040 return -ETIMEDOUT;
1041 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001042
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001043 return transfer->len;
Robin Gongf62cacc2014-09-11 09:18:44 +08001044}
1045
1046static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001047 struct spi_transfer *transfer)
1048{
1049 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1050
1051 spi_imx->tx_buf = transfer->tx_buf;
1052 spi_imx->rx_buf = transfer->rx_buf;
1053 spi_imx->count = transfer->len;
1054 spi_imx->txfifo = 0;
1055
Axel Linaa0fe822014-02-09 11:06:04 +08001056 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001057
1058 spi_imx_push(spi_imx);
1059
Shawn Guoedd501bb2011-07-10 01:16:35 +08001060 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001061
1062 wait_for_completion(&spi_imx->xfer_done);
1063
1064 return transfer->len;
1065}
1066
Robin Gongf62cacc2014-09-11 09:18:44 +08001067static int spi_imx_transfer(struct spi_device *spi,
1068 struct spi_transfer *transfer)
1069{
Robin Gongf62cacc2014-09-11 09:18:44 +08001070 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1071
Sascha Hauerc008a802016-02-24 09:20:26 +01001072 if (spi_imx->usedma)
Sascha Hauer99f1cf12016-02-23 10:23:50 +01001073 return spi_imx_dma_transfer(spi_imx, transfer);
Sascha Hauerc008a802016-02-24 09:20:26 +01001074 else
1075 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001076}
1077
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001078static int spi_imx_setup(struct spi_device *spi)
1079{
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001080 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001081 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1082
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001083 if (gpio_is_valid(spi->cs_gpio))
1084 gpio_direction_output(spi->cs_gpio,
1085 spi->mode & SPI_CS_HIGH ? 0 : 1);
Sascha Hauer6c23e5d2009-10-01 15:44:29 -07001086
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001087 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
1088
1089 return 0;
1090}
1091
1092static void spi_imx_cleanup(struct spi_device *spi)
1093{
1094}
1095
Huang Shijie9e556dc2013-10-23 16:31:50 +08001096static int
1097spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1098{
1099 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1100 int ret;
1101
1102 ret = clk_enable(spi_imx->clk_per);
1103 if (ret)
1104 return ret;
1105
1106 ret = clk_enable(spi_imx->clk_ipg);
1107 if (ret) {
1108 clk_disable(spi_imx->clk_per);
1109 return ret;
1110 }
1111
1112 return 0;
1113}
1114
1115static int
1116spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1117{
1118 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1119
1120 clk_disable(spi_imx->clk_ipg);
1121 clk_disable(spi_imx->clk_per);
1122 return 0;
1123}
1124
Grant Likelyfd4a3192012-12-07 16:57:14 +00001125static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001126{
Shawn Guo22a85e42011-07-10 01:16:41 +08001127 struct device_node *np = pdev->dev.of_node;
1128 const struct of_device_id *of_id =
1129 of_match_device(spi_imx_dt_ids, &pdev->dev);
1130 struct spi_imx_master *mxc_platform_info =
1131 dev_get_platdata(&pdev->dev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001132 struct spi_master *master;
1133 struct spi_imx_data *spi_imx;
1134 struct resource *res;
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001135 int i, ret, irq;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001136
Shawn Guo22a85e42011-07-10 01:16:41 +08001137 if (!np && !mxc_platform_info) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001138 dev_err(&pdev->dev, "can't get the platform data\n");
1139 return -EINVAL;
1140 }
1141
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001142 master = spi_alloc_master(&pdev->dev, sizeof(struct spi_imx_data));
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001143 if (!master)
1144 return -ENOMEM;
1145
1146 platform_set_drvdata(pdev, master);
1147
Stephen Warren24778be2013-05-21 20:36:35 -06001148 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001149 master->bus_num = np ? -1 : pdev->id;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001150
1151 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001152 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001153 spi_imx->dev = &pdev->dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001154
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001155 spi_imx->devtype_data = of_id ? of_id->data :
1156 (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
1157
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001158 if (mxc_platform_info) {
1159 master->num_chipselect = mxc_platform_info->num_chipselect;
1160 master->cs_gpios = devm_kzalloc(&master->dev,
1161 sizeof(int) * master->num_chipselect, GFP_KERNEL);
1162 if (!master->cs_gpios)
1163 return -ENOMEM;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001164
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001165 for (i = 0; i < master->num_chipselect; i++)
1166 master->cs_gpios[i] = mxc_platform_info->chipselect[i];
1167 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001168
1169 spi_imx->bitbang.chipselect = spi_imx_chipselect;
1170 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1171 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1172 spi_imx->bitbang.master->setup = spi_imx_setup;
1173 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001174 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1175 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001176 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1177 if (is_imx51_ecspi(spi_imx))
1178 spi_imx->bitbang.master->mode_bits |= SPI_LOOP;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001179
1180 init_completion(&spi_imx->xfer_done);
1181
1182 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001183 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1184 if (IS_ERR(spi_imx->base)) {
1185 ret = PTR_ERR(spi_imx->base);
1186 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001187 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001188 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001189
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001190 irq = platform_get_irq(pdev, 0);
1191 if (irq < 0) {
1192 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001193 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001194 }
1195
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001196 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001197 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001198 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001199 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001200 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001201 }
1202
Sascha Haueraa29d842012-03-07 09:30:22 +01001203 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1204 if (IS_ERR(spi_imx->clk_ipg)) {
1205 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001206 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001207 }
1208
Sascha Haueraa29d842012-03-07 09:30:22 +01001209 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1210 if (IS_ERR(spi_imx->clk_per)) {
1211 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001212 goto out_master_put;
Sascha Haueraa29d842012-03-07 09:30:22 +01001213 }
1214
Fabio Estevam83174622013-07-11 01:26:49 -03001215 ret = clk_prepare_enable(spi_imx->clk_per);
1216 if (ret)
1217 goto out_master_put;
1218
1219 ret = clk_prepare_enable(spi_imx->clk_ipg);
1220 if (ret)
1221 goto out_put_per;
Sascha Haueraa29d842012-03-07 09:30:22 +01001222
1223 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001224 /*
1225 * Only validated on i.mx6 now, can remove the constrain if validated on
1226 * other chips.
1227 */
Anton Bondarenko37600472015-12-08 07:43:45 +01001228 if (is_imx51_ecspi(spi_imx)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001229 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001230 if (ret == -EPROBE_DEFER)
1231 goto out_clk_put;
1232
Anton Bondarenko37600472015-12-08 07:43:45 +01001233 if (ret < 0)
1234 dev_err(&pdev->dev, "dma setup error %d, use pio\n",
1235 ret);
1236 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001237
Shawn Guoedd501bb2011-07-10 01:16:35 +08001238 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001239
Shawn Guoedd501bb2011-07-10 01:16:35 +08001240 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001241
Shawn Guo22a85e42011-07-10 01:16:41 +08001242 master->dev.of_node = pdev->dev.of_node;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001243 ret = spi_bitbang_start(&spi_imx->bitbang);
1244 if (ret) {
1245 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
1246 goto out_clk_put;
1247 }
1248
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001249 for (i = 0; i < master->num_chipselect; i++) {
1250 if (!gpio_is_valid(master->cs_gpios[i]))
1251 continue;
1252
1253 ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
1254 DRIVER_NAME);
1255 if (ret) {
1256 dev_err(&pdev->dev, "Can't get CS GPIO %i\n",
1257 master->cs_gpios[i]);
1258 goto out_clk_put;
1259 }
1260 }
1261
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001262 dev_info(&pdev->dev, "probed\n");
1263
Huang Shijie9e556dc2013-10-23 16:31:50 +08001264 clk_disable(spi_imx->clk_ipg);
1265 clk_disable(spi_imx->clk_per);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001266 return ret;
1267
1268out_clk_put:
Sascha Haueraa29d842012-03-07 09:30:22 +01001269 clk_disable_unprepare(spi_imx->clk_ipg);
Fabio Estevam83174622013-07-11 01:26:49 -03001270out_put_per:
1271 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001272out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001273 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001274
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001275 return ret;
1276}
1277
Grant Likelyfd4a3192012-12-07 16:57:14 +00001278static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001279{
1280 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001281 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001282
1283 spi_bitbang_stop(&spi_imx->bitbang);
1284
1285 writel(0, spi_imx->base + MXC_CSPICTRL);
Philippe De Muyterfd40dcc2014-02-27 10:16:15 +01001286 clk_unprepare(spi_imx->clk_ipg);
1287 clk_unprepare(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001288 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001289 spi_master_put(master);
1290
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001291 return 0;
1292}
1293
1294static struct platform_driver spi_imx_driver = {
1295 .driver = {
1296 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001297 .of_match_table = spi_imx_dt_ids,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001298 },
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001299 .id_table = spi_imx_devtype,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001300 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001301 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001302};
Grant Likely940ab882011-10-05 11:29:49 -06001303module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001304
1305MODULE_DESCRIPTION("SPI Master Controller driver");
1306MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1307MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001308MODULE_ALIAS("platform:" DRIVER_NAME);