blob: a47947da17a3d0c5be4c62e3751323c3fc94fff5 [file] [log] [blame]
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001/*
2 * Copyright (C) 2009 Texas Instruments.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
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 Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/gpio.h>
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/platform_device.h>
25#include <linux/err.h>
26#include <linux/clk.h>
27#include <linux/dma-mapping.h>
28#include <linux/spi/spi.h>
29#include <linux/spi/spi_bitbang.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Sandeep Paulraj358934a2009-12-16 22:02:18 +000031
32#include <mach/spi.h>
33#include <mach/edma.h>
34
35#define SPI_NO_RESOURCE ((resource_size_t)-1)
36
37#define SPI_MAX_CHIPSELECT 2
38
39#define CS_DEFAULT 0xFF
40
Sandeep Paulraj358934a2009-12-16 22:02:18 +000041#define SPIFMT_PHASE_MASK BIT(16)
42#define SPIFMT_POLARITY_MASK BIT(17)
43#define SPIFMT_DISTIMER_MASK BIT(18)
44#define SPIFMT_SHIFTDIR_MASK BIT(20)
45#define SPIFMT_WAITENA_MASK BIT(21)
46#define SPIFMT_PARITYENA_MASK BIT(22)
47#define SPIFMT_ODD_PARITY_MASK BIT(23)
48#define SPIFMT_WDELAY_MASK 0x3f000000u
49#define SPIFMT_WDELAY_SHIFT 24
Brian Niebuhr7fe00922010-08-13 13:27:23 +053050#define SPIFMT_PRESCALE_SHIFT 8
Sandeep Paulraj358934a2009-12-16 22:02:18 +000051
Sandeep Paulraj358934a2009-12-16 22:02:18 +000052
53/* SPIPC0 */
54#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
55#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
56#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
57#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000058
59#define SPIINT_MASKALL 0x0101035F
Brian Niebuhre0d205e2010-09-02 16:52:06 +053060#define SPIINT_MASKINT 0x0000015F
61#define SPI_INTLVL_1 0x000001FF
62#define SPI_INTLVL_0 0x00000000
Sandeep Paulraj358934a2009-12-16 22:02:18 +000063
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +053064/* SPIDAT1 (upper 16 bit defines) */
65#define SPIDAT1_CSHOLD_MASK BIT(12)
66
67/* SPIGCR1 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000068#define SPIGCR1_CLKMOD_MASK BIT(1)
69#define SPIGCR1_MASTER_MASK BIT(0)
70#define SPIGCR1_LOOPBACK_MASK BIT(16)
Sekhar Nori8e206f12010-08-20 16:20:49 +053071#define SPIGCR1_SPIENA_MASK BIT(24)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000072
73/* SPIBUF */
74#define SPIBUF_TXFULL_MASK BIT(29)
75#define SPIBUF_RXEMPTY_MASK BIT(31)
76
Brian Niebuhr7abbf232010-08-19 15:07:38 +053077/* SPIDELAY */
78#define SPIDELAY_C2TDELAY_SHIFT 24
79#define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
80#define SPIDELAY_T2CDELAY_SHIFT 16
81#define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
82#define SPIDELAY_T2EDELAY_SHIFT 8
83#define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
84#define SPIDELAY_C2EDELAY_SHIFT 0
85#define SPIDELAY_C2EDELAY_MASK 0xFF
86
Sandeep Paulraj358934a2009-12-16 22:02:18 +000087/* Error Masks */
88#define SPIFLG_DLEN_ERR_MASK BIT(0)
89#define SPIFLG_TIMEOUT_MASK BIT(1)
90#define SPIFLG_PARERR_MASK BIT(2)
91#define SPIFLG_DESYNC_MASK BIT(3)
92#define SPIFLG_BITERR_MASK BIT(4)
93#define SPIFLG_OVRRUN_MASK BIT(6)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000094#define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
Brian Niebuhr839c9962010-08-23 16:39:19 +053095#define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
96 | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
97 | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
98 | SPIFLG_OVRRUN_MASK)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000099
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000100#define SPIINT_DMA_REQ_EN BIT(16)
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000101
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000102/* SPI Controller registers */
103#define SPIGCR0 0x00
104#define SPIGCR1 0x04
105#define SPIINT 0x08
106#define SPILVL 0x0c
107#define SPIFLG 0x10
108#define SPIPC0 0x14
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000109#define SPIDAT1 0x3c
110#define SPIBUF 0x40
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000111#define SPIDELAY 0x48
112#define SPIDEF 0x4c
113#define SPIFMT0 0x50
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000114
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000115/* We have 2 DMA channels per CS, one for RX and one for TX */
116struct davinci_spi_dma {
117 int dma_tx_channel;
118 int dma_rx_channel;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530119 int dummy_param_slot;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000120 enum dma_event_q eventq;
121
122 struct completion dma_tx_completion;
123 struct completion dma_rx_completion;
124};
125
126/* SPI Controller driver's private data. */
127struct davinci_spi {
128 struct spi_bitbang bitbang;
129 struct clk *clk;
130
131 u8 version;
132 resource_size_t pbase;
133 void __iomem *base;
134 size_t region_size;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530135 u32 irq;
136 struct completion done;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000137
138 const void *tx;
139 void *rx;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530140#define SPI_TMP_BUFSZ (SMP_CACHE_BYTES + 1)
141 u8 rx_tmp_buf[SPI_TMP_BUFSZ];
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530142 int rcount;
143 int wcount;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530144 struct davinci_spi_dma dma_channels;
Brian Niebuhr778e2612010-09-03 15:15:06 +0530145 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000146
147 void (*get_rx)(u32 rx_data, struct davinci_spi *);
148 u32 (*get_tx)(struct davinci_spi *);
149
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530150 u8 bytes_per_word[SPI_MAX_CHIPSELECT];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000151};
152
Brian Niebuhr53a31b02010-08-16 15:05:51 +0530153static struct davinci_spi_config davinci_spi_default_cfg;
154
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000155static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
156{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530157 if (davinci_spi->rx) {
158 u8 *rx = davinci_spi->rx;
159 *rx++ = (u8)data;
160 davinci_spi->rx = rx;
161 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000162}
163
164static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
165{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530166 if (davinci_spi->rx) {
167 u16 *rx = davinci_spi->rx;
168 *rx++ = (u16)data;
169 davinci_spi->rx = rx;
170 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000171}
172
173static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
174{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530175 u32 data = 0;
176 if (davinci_spi->tx) {
177 const u8 *tx = davinci_spi->tx;
178 data = *tx++;
179 davinci_spi->tx = tx;
180 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000181 return data;
182}
183
184static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
185{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530186 u32 data = 0;
187 if (davinci_spi->tx) {
188 const u16 *tx = davinci_spi->tx;
189 data = *tx++;
190 davinci_spi->tx = tx;
191 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000192 return data;
193}
194
195static inline void set_io_bits(void __iomem *addr, u32 bits)
196{
197 u32 v = ioread32(addr);
198
199 v |= bits;
200 iowrite32(v, addr);
201}
202
203static inline void clear_io_bits(void __iomem *addr, u32 bits)
204{
205 u32 v = ioread32(addr);
206
207 v &= ~bits;
208 iowrite32(v, addr);
209}
210
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000211/*
212 * Interface to control the chip select signal
213 */
214static void davinci_spi_chipselect(struct spi_device *spi, int value)
215{
216 struct davinci_spi *davinci_spi;
217 struct davinci_spi_platform_data *pdata;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530218 u8 chip_sel = spi->chip_select;
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +0530219 u16 spidat1_cfg = CS_DEFAULT;
Brian Niebuhr23853972010-08-13 10:57:44 +0530220 bool gpio_chipsel = false;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000221
222 davinci_spi = spi_master_get_devdata(spi->master);
223 pdata = davinci_spi->pdata;
224
Brian Niebuhr23853972010-08-13 10:57:44 +0530225 if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
226 pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
227 gpio_chipsel = true;
228
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000229 /*
230 * Board specific chip select logic decides the polarity and cs
231 * line for the controller
232 */
Brian Niebuhr23853972010-08-13 10:57:44 +0530233 if (gpio_chipsel) {
234 if (value == BITBANG_CS_ACTIVE)
235 gpio_set_value(pdata->chip_sel[chip_sel], 0);
236 else
237 gpio_set_value(pdata->chip_sel[chip_sel], 1);
238 } else {
239 if (value == BITBANG_CS_ACTIVE) {
240 spidat1_cfg |= SPIDAT1_CSHOLD_MASK;
241 spidat1_cfg &= ~(0x1 << chip_sel);
242 }
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530243
Brian Niebuhr23853972010-08-13 10:57:44 +0530244 iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2);
245 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000246}
247
248/**
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530249 * davinci_spi_get_prescale - Calculates the correct prescale value
250 * @maxspeed_hz: the maximum rate the SPI clock can run at
251 *
252 * This function calculates the prescale value that generates a clock rate
253 * less than or equal to the specified maximum.
254 *
255 * Returns: calculated prescale - 1 for easy programming into SPI registers
256 * or negative error number if valid prescalar cannot be updated.
257 */
258static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi,
259 u32 max_speed_hz)
260{
261 int ret;
262
263 ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz);
264
265 if (ret < 3 || ret > 256)
266 return -EINVAL;
267
268 return ret - 1;
269}
270
271/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000272 * davinci_spi_setup_transfer - This functions will determine transfer method
273 * @spi: spi device on which data transfer to be done
274 * @t: spi transfer in which transfer info is filled
275 *
276 * This function determines data transfer method (8/16/32 bit transfer).
277 * It will also set the SPI Clock Control register according to
278 * SPI slave device freq.
279 */
280static int davinci_spi_setup_transfer(struct spi_device *spi,
281 struct spi_transfer *t)
282{
283
284 struct davinci_spi *davinci_spi;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530285 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000286 u8 bits_per_word = 0;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530287 u32 hz = 0, spifmt = 0, prescale = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000288
289 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530290 spicfg = (struct davinci_spi_config *)spi->controller_data;
291 if (!spicfg)
292 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000293
294 if (t) {
295 bits_per_word = t->bits_per_word;
296 hz = t->speed_hz;
297 }
298
299 /* if bits_per_word is not set then set it default */
300 if (!bits_per_word)
301 bits_per_word = spi->bits_per_word;
302
303 /*
304 * Assign function pointer to appropriate transfer method
305 * 8bit, 16bit or 32bit transfer
306 */
307 if (bits_per_word <= 8 && bits_per_word >= 2) {
308 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
309 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530310 davinci_spi->bytes_per_word[spi->chip_select] = 1;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000311 } else if (bits_per_word <= 16 && bits_per_word >= 2) {
312 davinci_spi->get_rx = davinci_spi_rx_buf_u16;
313 davinci_spi->get_tx = davinci_spi_tx_buf_u16;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530314 davinci_spi->bytes_per_word[spi->chip_select] = 2;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000315 } else
316 return -EINVAL;
317
318 if (!hz)
319 hz = spi->max_speed_hz;
320
Brian Niebuhr25f33512010-08-19 12:15:22 +0530321 /* Set up SPIFMTn register, unique to this chipselect. */
322
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530323 prescale = davinci_spi_get_prescale(davinci_spi, hz);
324 if (prescale < 0)
325 return prescale;
326
Brian Niebuhr25f33512010-08-19 12:15:22 +0530327 spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000328
Brian Niebuhr25f33512010-08-19 12:15:22 +0530329 if (spi->mode & SPI_LSB_FIRST)
330 spifmt |= SPIFMT_SHIFTDIR_MASK;
331
332 if (spi->mode & SPI_CPOL)
333 spifmt |= SPIFMT_POLARITY_MASK;
334
335 if (!(spi->mode & SPI_CPHA))
336 spifmt |= SPIFMT_PHASE_MASK;
337
338 /*
339 * Version 1 hardware supports two basic SPI modes:
340 * - Standard SPI mode uses 4 pins, with chipselect
341 * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
342 * (distinct from SPI_3WIRE, with just one data wire;
343 * or similar variants without MOSI or without MISO)
344 *
345 * Version 2 hardware supports an optional handshaking signal,
346 * so it can support two more modes:
347 * - 5 pin SPI variant is standard SPI plus SPI_READY
348 * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
349 */
350
351 if (davinci_spi->version == SPI_VERSION_2) {
352
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530353 u32 delay = 0;
354
Brian Niebuhr25f33512010-08-19 12:15:22 +0530355 spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
356 & SPIFMT_WDELAY_MASK);
357
358 if (spicfg->odd_parity)
359 spifmt |= SPIFMT_ODD_PARITY_MASK;
360
361 if (spicfg->parity_enable)
362 spifmt |= SPIFMT_PARITYENA_MASK;
363
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530364 if (spicfg->timer_disable) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530365 spifmt |= SPIFMT_DISTIMER_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530366 } else {
367 delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
368 & SPIDELAY_C2TDELAY_MASK;
369 delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
370 & SPIDELAY_T2CDELAY_MASK;
371 }
Brian Niebuhr25f33512010-08-19 12:15:22 +0530372
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530373 if (spi->mode & SPI_READY) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530374 spifmt |= SPIFMT_WAITENA_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530375 delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
376 & SPIDELAY_T2EDELAY_MASK;
377 delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
378 & SPIDELAY_C2EDELAY_MASK;
379 }
380
381 iowrite32(delay, davinci_spi->base + SPIDELAY);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530382 }
383
384 iowrite32(spifmt, davinci_spi->base + SPIFMT0);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000385
386 return 0;
387}
388
389static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data)
390{
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530391 struct davinci_spi *davinci_spi = data;
392 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
393
394 edma_stop(davinci_spi_dma->dma_rx_channel);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000395
396 if (ch_status == DMA_COMPLETE)
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530397 davinci_spi->rcount = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000398
399 complete(&davinci_spi_dma->dma_rx_completion);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000400}
401
402static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
403{
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530404 struct davinci_spi *davinci_spi = data;
405 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
406
407 edma_stop(davinci_spi_dma->dma_tx_channel);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000408
409 if (ch_status == DMA_COMPLETE)
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530410 davinci_spi->wcount = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000411
412 complete(&davinci_spi_dma->dma_tx_completion);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000413}
414
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000415/**
416 * davinci_spi_setup - This functions will set default transfer method
417 * @spi: spi device on which data transfer to be done
418 *
419 * This functions sets the default transfer method.
420 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000421static int davinci_spi_setup(struct spi_device *spi)
422{
Brian Niebuhrb23a5d42010-09-24 18:53:32 +0530423 int retval = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000424 struct davinci_spi *davinci_spi;
Brian Niebuhrbe884712010-09-03 12:15:28 +0530425 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000426
427 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhrbe884712010-09-03 12:15:28 +0530428 pdata = davinci_spi->pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000429
430 /* if bits per word length is zero then set it default 8 */
431 if (!spi->bits_per_word)
432 spi->bits_per_word = 8;
433
Brian Niebuhrbe884712010-09-03 12:15:28 +0530434 if (!(spi->mode & SPI_NO_CS)) {
435 if ((pdata->chip_sel == NULL) ||
436 (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
437 set_io_bits(davinci_spi->base + SPIPC0,
438 1 << spi->chip_select);
439
440 }
441
442 if (spi->mode & SPI_READY)
443 set_io_bits(davinci_spi->base + SPIPC0, SPIPC0_SPIENA_MASK);
444
445 if (spi->mode & SPI_LOOP)
446 set_io_bits(davinci_spi->base + SPIGCR1,
447 SPIGCR1_LOOPBACK_MASK);
448 else
449 clear_io_bits(davinci_spi->base + SPIGCR1,
450 SPIGCR1_LOOPBACK_MASK);
451
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000452 return retval;
453}
454
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000455static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
456 int int_status)
457{
458 struct device *sdev = davinci_spi->bitbang.master->dev.parent;
459
460 if (int_status & SPIFLG_TIMEOUT_MASK) {
461 dev_dbg(sdev, "SPI Time-out Error\n");
462 return -ETIMEDOUT;
463 }
464 if (int_status & SPIFLG_DESYNC_MASK) {
465 dev_dbg(sdev, "SPI Desynchronization Error\n");
466 return -EIO;
467 }
468 if (int_status & SPIFLG_BITERR_MASK) {
469 dev_dbg(sdev, "SPI Bit error\n");
470 return -EIO;
471 }
472
473 if (davinci_spi->version == SPI_VERSION_2) {
474 if (int_status & SPIFLG_DLEN_ERR_MASK) {
475 dev_dbg(sdev, "SPI Data Length Error\n");
476 return -EIO;
477 }
478 if (int_status & SPIFLG_PARERR_MASK) {
479 dev_dbg(sdev, "SPI Parity Error\n");
480 return -EIO;
481 }
482 if (int_status & SPIFLG_OVRRUN_MASK) {
483 dev_dbg(sdev, "SPI Data Overrun error\n");
484 return -EIO;
485 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000486 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
487 dev_dbg(sdev, "SPI Buffer Init Active\n");
488 return -EBUSY;
489 }
490 }
491
492 return 0;
493}
494
495/**
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530496 * davinci_spi_process_events - check for and handle any SPI controller events
497 * @davinci_spi: the controller data
498 *
499 * This function will check the SPIFLG register and handle any events that are
500 * detected there
501 */
502static int davinci_spi_process_events(struct davinci_spi *davinci_spi)
503{
504 u32 buf, status, errors = 0, data1_reg_val;
505
506 buf = ioread32(davinci_spi->base + SPIBUF);
507
508 if (davinci_spi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
509 davinci_spi->get_rx(buf & 0xFFFF, davinci_spi);
510 davinci_spi->rcount--;
511 }
512
513 status = ioread32(davinci_spi->base + SPIFLG);
514
515 if (unlikely(status & SPIFLG_ERROR_MASK)) {
516 errors = status & SPIFLG_ERROR_MASK;
517 goto out;
518 }
519
520 if (davinci_spi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
521 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
522 davinci_spi->wcount--;
523 data1_reg_val &= ~0xFFFF;
524 data1_reg_val |= 0xFFFF & davinci_spi->get_tx(davinci_spi);
525 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
526 }
527
528out:
529 return errors;
530}
531
532/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000533 * davinci_spi_bufs - functions which will handle transfer data
534 * @spi: spi device on which data transfer to be done
535 * @t: spi transfer in which transfer info is filled
536 *
537 * This function will put data to be transferred into data register
538 * of SPI controller and then wait until the completion will be marked
539 * by the IRQ Handler.
540 */
541static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
542{
543 struct davinci_spi *davinci_spi;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530544 int ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000545 u32 tx_data, data1_reg_val;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530546 u32 errors = 0;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530547 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000548 struct davinci_spi_platform_data *pdata;
549
550 davinci_spi = spi_master_get_devdata(spi->master);
551 pdata = davinci_spi->pdata;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530552 spicfg = (struct davinci_spi_config *)spi->controller_data;
553 if (!spicfg)
554 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000555
556 davinci_spi->tx = t->tx_buf;
557 davinci_spi->rx = t->rx_buf;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530558 davinci_spi->wcount = t->len /
559 davinci_spi->bytes_per_word[spi->chip_select];
560 davinci_spi->rcount = davinci_spi->wcount;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530561
Brian Niebuhr839c9962010-08-23 16:39:19 +0530562 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
563
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000564 /* Enable SPI */
565 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
566
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530567 if (spicfg->io_type == SPI_IO_TYPE_INTR) {
568 set_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
569 INIT_COMPLETION(davinci_spi->done);
570 }
Brian Niebuhrcf90fe72010-08-20 17:02:49 +0530571
Brian Niebuhr839c9962010-08-23 16:39:19 +0530572 /* start the transfer */
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530573 davinci_spi->wcount--;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530574 tx_data = davinci_spi->get_tx(davinci_spi);
575 data1_reg_val &= 0xFFFF0000;
576 data1_reg_val |= tx_data & 0xFFFF;
577 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000578
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530579 /* Wait for the transfer to complete */
580 if (spicfg->io_type == SPI_IO_TYPE_INTR) {
581 wait_for_completion_interruptible(&(davinci_spi->done));
582 } else {
583 while (davinci_spi->rcount > 0 || davinci_spi->wcount > 0) {
584 errors = davinci_spi_process_events(davinci_spi);
585 if (errors)
586 break;
587 cpu_relax();
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000588 }
589 }
590
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530591 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
592
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000593 /*
594 * Check for bit error, desync error,parity error,timeout error and
595 * receive overflow errors
596 */
Brian Niebuhr839c9962010-08-23 16:39:19 +0530597 if (errors) {
598 ret = davinci_spi_check_error(davinci_spi, errors);
599 WARN(!ret, "%s: error reported but no error found!\n",
600 dev_name(&spi->dev));
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000601 return ret;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530602 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000603
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000604 return t->len;
605}
606
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530607/**
608 * davinci_spi_irq - Interrupt handler for SPI Master Controller
609 * @irq: IRQ number for this SPI Master
610 * @context_data: structure for SPI Master controller davinci_spi
611 *
612 * ISR will determine that interrupt arrives either for READ or WRITE command.
613 * According to command it will do the appropriate action. It will check
614 * transfer length and if it is not zero then dispatch transfer command again.
615 * If transfer length is zero then it will indicate the COMPLETION so that
616 * davinci_spi_bufs function can go ahead.
617 */
618static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
619{
620 struct davinci_spi *davinci_spi = context_data;
621 int status;
622
623 status = davinci_spi_process_events(davinci_spi);
624 if (unlikely(status != 0))
625 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
626
627 if ((!davinci_spi->rcount && !davinci_spi->wcount) || status)
628 complete(&davinci_spi->done);
629
630 return IRQ_HANDLED;
631}
632
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000633static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
634{
635 struct davinci_spi *davinci_spi;
636 int int_status = 0;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530637 unsigned rx_buf_count;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000638 struct davinci_spi_dma *davinci_spi_dma;
Brian Niebuhrb7ab24a2010-08-19 16:42:42 +0530639 int data_type, ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000640 unsigned long tx_reg, rx_reg;
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530641 struct davinci_spi_platform_data *pdata;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530642 void *rx_buf;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000643 struct device *sdev;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530644 struct edmacc_param param;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000645
646 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530647 pdata = davinci_spi->pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000648 sdev = davinci_spi->bitbang.master->dev.parent;
649
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530650 davinci_spi_dma = &davinci_spi->dma_channels;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000651
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530652 /* convert len to words based on bits_per_word */
653 data_type = davinci_spi->bytes_per_word[spi->chip_select];
654
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000655 tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
656 rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
657
658 davinci_spi->tx = t->tx_buf;
659 davinci_spi->rx = t->rx_buf;
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530660 davinci_spi->wcount = t->len / data_type;
661 davinci_spi->rcount = davinci_spi->wcount;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000662
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000663 init_completion(&davinci_spi_dma->dma_rx_completion);
664 init_completion(&davinci_spi_dma->dma_tx_completion);
665
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000666 /* disable all interrupts for dma transfers */
667 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000668 /* Enable SPI */
669 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
670
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530671 /*
672 * Transmit DMA setup
673 *
674 * If there is transmit data, map the transmit buffer, set it as the
675 * source of data and set the source B index to data size.
676 * If there is no transmit data, set the transmit register as the
677 * source of data, and set the source B index to zero.
678 *
679 * The destination is always the transmit register itself. And the
680 * destination never increments.
681 */
682
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000683 if (t->tx_buf) {
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530684 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf,
685 davinci_spi->wcount, DMA_TO_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000686 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530687 dev_dbg(sdev, "Unable to DMA map %d bytes TX buffer\n",
688 davinci_spi->wcount);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000689 return -ENOMEM;
690 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000691 }
692
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530693 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_tx_channel);
694 param.src = t->tx_buf ? t->tx_dma : tx_reg;
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530695 param.a_b_cnt = davinci_spi->wcount << 16 | data_type;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530696 param.dst = tx_reg;
697 param.src_dst_bidx = t->tx_buf ? data_type : 0;
698 param.link_bcntrld = 0xffff;
699 param.src_dst_cidx = 0;
700 param.ccnt = 1;
701 edma_write_slot(davinci_spi_dma->dma_tx_channel, &param);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530702 edma_link(davinci_spi_dma->dma_tx_channel,
703 davinci_spi_dma->dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000704
Brian Niebuhre91c6592010-10-01 10:29:29 +0530705 /*
706 * Receive DMA setup
707 *
708 * If there is receive buffer, use it to receive data. If there
709 * is none provided, use a temporary receive buffer. Set the
710 * destination B index to 0 so effectively only one byte is used
711 * in the temporary buffer (address does not increment).
712 *
713 * The source of receive data is the receive data register. The
714 * source address never increments.
715 */
716
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000717 if (t->rx_buf) {
Brian Niebuhre91c6592010-10-01 10:29:29 +0530718 rx_buf = t->rx_buf;
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530719 rx_buf_count = davinci_spi->rcount;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530720 } else {
721 rx_buf = davinci_spi->rx_tmp_buf;
722 rx_buf_count = sizeof(davinci_spi->rx_tmp_buf);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000723 }
724
Brian Niebuhre91c6592010-10-01 10:29:29 +0530725 t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count,
726 DMA_FROM_DEVICE);
727 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
728 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
729 rx_buf_count);
730 if (t->tx_buf)
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530731 dma_unmap_single(NULL, t->tx_dma, davinci_spi->wcount,
732 DMA_TO_DEVICE);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530733 return -ENOMEM;
734 }
735
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530736 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_rx_channel);
737 param.src = rx_reg;
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530738 param.a_b_cnt = davinci_spi->rcount << 16 | data_type;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530739 param.dst = t->rx_dma;
740 param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16;
741 param.link_bcntrld = 0xffff;
742 param.src_dst_cidx = 0;
743 param.ccnt = 1;
744 edma_write_slot(davinci_spi_dma->dma_rx_channel, &param);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530745
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530746 if (pdata->cshold_bug) {
747 u16 spidat1 = ioread16(davinci_spi->base + SPIDAT1 + 2);
748 iowrite16(spidat1, davinci_spi->base + SPIDAT1 + 2);
749 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000750
Brian Niebuhre91c6592010-10-01 10:29:29 +0530751 edma_start(davinci_spi_dma->dma_rx_channel);
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530752 edma_start(davinci_spi_dma->dma_tx_channel);
Brian Niebuhra4f44972010-10-01 14:00:48 +0530753 set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000754
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530755 wait_for_completion_interruptible(&davinci_spi_dma->dma_tx_completion);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530756 wait_for_completion_interruptible(&davinci_spi_dma->dma_rx_completion);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000757
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530758 if (t->tx_buf)
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530759 dma_unmap_single(NULL, t->tx_dma, davinci_spi->wcount,
760 DMA_TO_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000761
Brian Niebuhre91c6592010-10-01 10:29:29 +0530762 dma_unmap_single(NULL, t->rx_dma, rx_buf_count, DMA_FROM_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000763
Brian Niebuhra4f44972010-10-01 14:00:48 +0530764 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
765
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000766 /*
767 * Check for bit error, desync error,parity error,timeout error and
768 * receive overflow errors
769 */
770 int_status = ioread32(davinci_spi->base + SPIFLG);
771
772 ret = davinci_spi_check_error(davinci_spi, int_status);
773 if (ret != 0)
774 return ret;
775
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530776 if (davinci_spi->rcount != 0 || davinci_spi->wcount != 0) {
777 dev_err(sdev, "SPI data transfer error\n");
778 return -EIO;
779 }
780
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000781 return t->len;
782}
783
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530784static int davinci_spi_request_dma(struct davinci_spi *davinci_spi)
Sekhar Nori903ca252010-10-01 14:51:40 +0530785{
786 int r;
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530787 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
Sekhar Nori903ca252010-10-01 14:51:40 +0530788
789 r = edma_alloc_channel(davinci_spi_dma->dma_rx_channel,
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530790 davinci_spi_dma_rx_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530791 davinci_spi_dma->eventq);
792 if (r < 0) {
793 pr_err("Unable to request DMA channel for SPI RX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530794 r = -EAGAIN;
795 goto rx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530796 }
797
798 r = edma_alloc_channel(davinci_spi_dma->dma_tx_channel,
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530799 davinci_spi_dma_tx_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530800 davinci_spi_dma->eventq);
801 if (r < 0) {
Sekhar Nori903ca252010-10-01 14:51:40 +0530802 pr_err("Unable to request DMA channel for SPI TX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530803 r = -EAGAIN;
804 goto tx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530805 }
806
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530807 r = edma_alloc_slot(EDMA_CTLR(davinci_spi_dma->dma_tx_channel),
808 EDMA_SLOT_ANY);
809 if (r < 0) {
810 pr_err("Unable to request SPI TX DMA param slot\n");
811 r = -EAGAIN;
812 goto param_failed;
813 }
814 davinci_spi_dma->dummy_param_slot = r;
815 edma_link(davinci_spi_dma->dummy_param_slot,
816 davinci_spi_dma->dummy_param_slot);
817
Sekhar Nori903ca252010-10-01 14:51:40 +0530818 return 0;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530819param_failed:
820 edma_free_channel(davinci_spi_dma->dma_tx_channel);
821tx_dma_failed:
822 edma_free_channel(davinci_spi_dma->dma_rx_channel);
823rx_dma_failed:
824 return r;
Sekhar Nori903ca252010-10-01 14:51:40 +0530825}
826
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000827/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000828 * davinci_spi_probe - probe function for SPI Master Controller
829 * @pdev: platform_device structure which contains plateform specific data
830 */
831static int davinci_spi_probe(struct platform_device *pdev)
832{
833 struct spi_master *master;
834 struct davinci_spi *davinci_spi;
835 struct davinci_spi_platform_data *pdata;
836 struct resource *r, *mem;
837 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
838 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
839 resource_size_t dma_eventq = SPI_NO_RESOURCE;
840 int i = 0, ret = 0;
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530841 u32 spipc0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000842
843 pdata = pdev->dev.platform_data;
844 if (pdata == NULL) {
845 ret = -ENODEV;
846 goto err;
847 }
848
849 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
850 if (master == NULL) {
851 ret = -ENOMEM;
852 goto err;
853 }
854
855 dev_set_drvdata(&pdev->dev, master);
856
857 davinci_spi = spi_master_get_devdata(master);
858 if (davinci_spi == NULL) {
859 ret = -ENOENT;
860 goto free_master;
861 }
862
863 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
864 if (r == NULL) {
865 ret = -ENOENT;
866 goto free_master;
867 }
868
869 davinci_spi->pbase = r->start;
870 davinci_spi->region_size = resource_size(r);
871 davinci_spi->pdata = pdata;
872
873 mem = request_mem_region(r->start, davinci_spi->region_size,
874 pdev->name);
875 if (mem == NULL) {
876 ret = -EBUSY;
877 goto free_master;
878 }
879
Sekhar Nori50356dd2010-10-08 15:27:26 +0530880 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000881 if (davinci_spi->base == NULL) {
882 ret = -ENOMEM;
883 goto release_region;
884 }
885
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530886 davinci_spi->irq = platform_get_irq(pdev, 0);
887 if (davinci_spi->irq <= 0) {
888 ret = -EINVAL;
889 goto unmap_io;
890 }
891
892 ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0,
893 dev_name(&pdev->dev), davinci_spi);
894 if (ret)
895 goto unmap_io;
896
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000897 davinci_spi->bitbang.master = spi_master_get(master);
898 if (davinci_spi->bitbang.master == NULL) {
899 ret = -ENODEV;
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530900 goto irq_free;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000901 }
902
903 davinci_spi->clk = clk_get(&pdev->dev, NULL);
904 if (IS_ERR(davinci_spi->clk)) {
905 ret = -ENODEV;
906 goto put_master;
907 }
908 clk_enable(davinci_spi->clk);
909
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000910 master->bus_num = pdev->id;
911 master->num_chipselect = pdata->num_chipselect;
912 master->setup = davinci_spi_setup;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000913
914 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
915 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
916
917 davinci_spi->version = pdata->version;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000918
919 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
920 if (davinci_spi->version == SPI_VERSION_2)
921 davinci_spi->bitbang.flags |= SPI_READY;
922
Sekhar Nori903ca252010-10-01 14:51:40 +0530923 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
924 if (r)
925 dma_rx_chan = r->start;
926 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
927 if (r)
928 dma_tx_chan = r->start;
929 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
930 if (r)
931 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000932
Sekhar Nori903ca252010-10-01 14:51:40 +0530933 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
934 if (dma_rx_chan != SPI_NO_RESOURCE &&
935 dma_tx_chan != SPI_NO_RESOURCE &&
936 dma_eventq != SPI_NO_RESOURCE) {
937 davinci_spi->dma_channels.dma_rx_channel = dma_rx_chan;
938 davinci_spi->dma_channels.dma_tx_channel = dma_tx_chan;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530939 davinci_spi->dma_channels.eventq = dma_eventq;
940
Brian Niebuhr9b189fd2010-10-05 11:38:41 +0530941 ret = davinci_spi_request_dma(davinci_spi);
Sekhar Nori903ca252010-10-01 14:51:40 +0530942 if (ret)
943 goto free_clk;
944
945 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000946 dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
947 "Using RX channel = %d , TX channel = %d and "
948 "event queue = %d", dma_rx_chan, dma_tx_chan,
949 dma_eventq);
950 }
951
952 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
953 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
954
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530955 init_completion(&davinci_spi->done);
956
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000957 /* Reset In/OUT SPI module */
958 iowrite32(0, davinci_spi->base + SPIGCR0);
959 udelay(100);
960 iowrite32(1, davinci_spi->base + SPIGCR0);
961
Brian Niebuhrbe884712010-09-03 12:15:28 +0530962 /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530963 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
964 iowrite32(spipc0, davinci_spi->base + SPIPC0);
965
Brian Niebuhr23853972010-08-13 10:57:44 +0530966 /* initialize chip selects */
967 if (pdata->chip_sel) {
968 for (i = 0; i < pdata->num_chipselect; i++) {
969 if (pdata->chip_sel[i] != SPI_INTERN_CS)
970 gpio_direction_output(pdata->chip_sel[i], 1);
971 }
972 }
973
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000974 /* Clock internal */
975 if (davinci_spi->pdata->clk_internal)
976 set_io_bits(davinci_spi->base + SPIGCR1,
977 SPIGCR1_CLKMOD_MASK);
978 else
979 clear_io_bits(davinci_spi->base + SPIGCR1,
980 SPIGCR1_CLKMOD_MASK);
981
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530982 if (pdata->intr_line)
983 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
984 else
985 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
986
Brian Niebuhr843a7132010-08-12 12:49:05 +0530987 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
988
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000989 /* master mode default */
990 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
991
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000992 ret = spi_bitbang_start(&davinci_spi->bitbang);
993 if (ret)
Sekhar Nori903ca252010-10-01 14:51:40 +0530994 goto free_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000995
Brian Niebuhr3b740b12010-09-03 14:50:07 +0530996 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000997
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000998 return ret;
999
Sekhar Nori903ca252010-10-01 14:51:40 +05301000free_dma:
1001 edma_free_channel(davinci_spi->dma_channels.dma_tx_channel);
1002 edma_free_channel(davinci_spi->dma_channels.dma_rx_channel);
Brian Niebuhr523c37e2010-10-04 17:35:34 +05301003 edma_free_slot(davinci_spi->dma_channels.dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001004free_clk:
1005 clk_disable(davinci_spi->clk);
1006 clk_put(davinci_spi->clk);
1007put_master:
1008 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +05301009irq_free:
1010 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001011unmap_io:
1012 iounmap(davinci_spi->base);
1013release_region:
1014 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1015free_master:
1016 kfree(master);
1017err:
1018 return ret;
1019}
1020
1021/**
1022 * davinci_spi_remove - remove function for SPI Master Controller
1023 * @pdev: platform_device structure which contains plateform specific data
1024 *
1025 * This function will do the reverse action of davinci_spi_probe function
1026 * It will free the IRQ and SPI controller's memory region.
1027 * It will also call spi_bitbang_stop to destroy the work queue which was
1028 * created by spi_bitbang_start.
1029 */
1030static int __exit davinci_spi_remove(struct platform_device *pdev)
1031{
1032 struct davinci_spi *davinci_spi;
1033 struct spi_master *master;
1034
1035 master = dev_get_drvdata(&pdev->dev);
1036 davinci_spi = spi_master_get_devdata(master);
1037
1038 spi_bitbang_stop(&davinci_spi->bitbang);
1039
1040 clk_disable(davinci_spi->clk);
1041 clk_put(davinci_spi->clk);
1042 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +05301043 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001044 iounmap(davinci_spi->base);
1045 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1046
1047 return 0;
1048}
1049
1050static struct platform_driver davinci_spi_driver = {
1051 .driver.name = "spi_davinci",
1052 .remove = __exit_p(davinci_spi_remove),
1053};
1054
1055static int __init davinci_spi_init(void)
1056{
1057 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1058}
1059module_init(davinci_spi_init);
1060
1061static void __exit davinci_spi_exit(void)
1062{
1063 platform_driver_unregister(&davinci_spi_driver);
1064}
1065module_exit(davinci_spi_exit);
1066
1067MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1068MODULE_LICENSE("GPL");