blob: 975c2a228d0a7398240be974323486ba13c45252 [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{
Sekhar Nori903ca252010-10-01 14:51:40 +0530391 struct davinci_spi_dma *davinci_spi_dma = data;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000392
393 if (ch_status == DMA_COMPLETE)
394 edma_stop(davinci_spi_dma->dma_rx_channel);
395 else
396 edma_clean_channel(davinci_spi_dma->dma_rx_channel);
397
398 complete(&davinci_spi_dma->dma_rx_completion);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000399}
400
401static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
402{
Sekhar Nori903ca252010-10-01 14:51:40 +0530403 struct davinci_spi_dma *davinci_spi_dma = data;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000404
405 if (ch_status == DMA_COMPLETE)
406 edma_stop(davinci_spi_dma->dma_tx_channel);
407 else
408 edma_clean_channel(davinci_spi_dma->dma_tx_channel);
409
410 complete(&davinci_spi_dma->dma_tx_completion);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000411}
412
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000413/**
414 * davinci_spi_setup - This functions will set default transfer method
415 * @spi: spi device on which data transfer to be done
416 *
417 * This functions sets the default transfer method.
418 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000419static int davinci_spi_setup(struct spi_device *spi)
420{
Brian Niebuhrb23a5d42010-09-24 18:53:32 +0530421 int retval = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000422 struct davinci_spi *davinci_spi;
Brian Niebuhrbe884712010-09-03 12:15:28 +0530423 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000424
425 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhrbe884712010-09-03 12:15:28 +0530426 pdata = davinci_spi->pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000427
428 /* if bits per word length is zero then set it default 8 */
429 if (!spi->bits_per_word)
430 spi->bits_per_word = 8;
431
Brian Niebuhrbe884712010-09-03 12:15:28 +0530432 if (!(spi->mode & SPI_NO_CS)) {
433 if ((pdata->chip_sel == NULL) ||
434 (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
435 set_io_bits(davinci_spi->base + SPIPC0,
436 1 << spi->chip_select);
437
438 }
439
440 if (spi->mode & SPI_READY)
441 set_io_bits(davinci_spi->base + SPIPC0, SPIPC0_SPIENA_MASK);
442
443 if (spi->mode & SPI_LOOP)
444 set_io_bits(davinci_spi->base + SPIGCR1,
445 SPIGCR1_LOOPBACK_MASK);
446 else
447 clear_io_bits(davinci_spi->base + SPIGCR1,
448 SPIGCR1_LOOPBACK_MASK);
449
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000450 return retval;
451}
452
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000453static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
454 int int_status)
455{
456 struct device *sdev = davinci_spi->bitbang.master->dev.parent;
457
458 if (int_status & SPIFLG_TIMEOUT_MASK) {
459 dev_dbg(sdev, "SPI Time-out Error\n");
460 return -ETIMEDOUT;
461 }
462 if (int_status & SPIFLG_DESYNC_MASK) {
463 dev_dbg(sdev, "SPI Desynchronization Error\n");
464 return -EIO;
465 }
466 if (int_status & SPIFLG_BITERR_MASK) {
467 dev_dbg(sdev, "SPI Bit error\n");
468 return -EIO;
469 }
470
471 if (davinci_spi->version == SPI_VERSION_2) {
472 if (int_status & SPIFLG_DLEN_ERR_MASK) {
473 dev_dbg(sdev, "SPI Data Length Error\n");
474 return -EIO;
475 }
476 if (int_status & SPIFLG_PARERR_MASK) {
477 dev_dbg(sdev, "SPI Parity Error\n");
478 return -EIO;
479 }
480 if (int_status & SPIFLG_OVRRUN_MASK) {
481 dev_dbg(sdev, "SPI Data Overrun error\n");
482 return -EIO;
483 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000484 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
485 dev_dbg(sdev, "SPI Buffer Init Active\n");
486 return -EBUSY;
487 }
488 }
489
490 return 0;
491}
492
493/**
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530494 * davinci_spi_process_events - check for and handle any SPI controller events
495 * @davinci_spi: the controller data
496 *
497 * This function will check the SPIFLG register and handle any events that are
498 * detected there
499 */
500static int davinci_spi_process_events(struct davinci_spi *davinci_spi)
501{
502 u32 buf, status, errors = 0, data1_reg_val;
503
504 buf = ioread32(davinci_spi->base + SPIBUF);
505
506 if (davinci_spi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
507 davinci_spi->get_rx(buf & 0xFFFF, davinci_spi);
508 davinci_spi->rcount--;
509 }
510
511 status = ioread32(davinci_spi->base + SPIFLG);
512
513 if (unlikely(status & SPIFLG_ERROR_MASK)) {
514 errors = status & SPIFLG_ERROR_MASK;
515 goto out;
516 }
517
518 if (davinci_spi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
519 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
520 davinci_spi->wcount--;
521 data1_reg_val &= ~0xFFFF;
522 data1_reg_val |= 0xFFFF & davinci_spi->get_tx(davinci_spi);
523 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
524 }
525
526out:
527 return errors;
528}
529
530/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000531 * davinci_spi_bufs - functions which will handle transfer data
532 * @spi: spi device on which data transfer to be done
533 * @t: spi transfer in which transfer info is filled
534 *
535 * This function will put data to be transferred into data register
536 * of SPI controller and then wait until the completion will be marked
537 * by the IRQ Handler.
538 */
539static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
540{
541 struct davinci_spi *davinci_spi;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530542 int ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000543 u32 tx_data, data1_reg_val;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530544 u32 errors = 0;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530545 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000546 struct davinci_spi_platform_data *pdata;
547
548 davinci_spi = spi_master_get_devdata(spi->master);
549 pdata = davinci_spi->pdata;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530550 spicfg = (struct davinci_spi_config *)spi->controller_data;
551 if (!spicfg)
552 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000553
554 davinci_spi->tx = t->tx_buf;
555 davinci_spi->rx = t->rx_buf;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530556 davinci_spi->wcount = t->len /
557 davinci_spi->bytes_per_word[spi->chip_select];
558 davinci_spi->rcount = davinci_spi->wcount;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530559
Brian Niebuhr839c9962010-08-23 16:39:19 +0530560 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
561
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000562 /* Enable SPI */
563 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
564
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530565 if (spicfg->io_type == SPI_IO_TYPE_INTR) {
566 set_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
567 INIT_COMPLETION(davinci_spi->done);
568 }
Brian Niebuhrcf90fe72010-08-20 17:02:49 +0530569
Brian Niebuhr839c9962010-08-23 16:39:19 +0530570 /* start the transfer */
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530571 davinci_spi->wcount--;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530572 tx_data = davinci_spi->get_tx(davinci_spi);
573 data1_reg_val &= 0xFFFF0000;
574 data1_reg_val |= tx_data & 0xFFFF;
575 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000576
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530577 /* Wait for the transfer to complete */
578 if (spicfg->io_type == SPI_IO_TYPE_INTR) {
579 wait_for_completion_interruptible(&(davinci_spi->done));
580 } else {
581 while (davinci_spi->rcount > 0 || davinci_spi->wcount > 0) {
582 errors = davinci_spi_process_events(davinci_spi);
583 if (errors)
584 break;
585 cpu_relax();
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000586 }
587 }
588
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530589 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
590
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000591 /*
592 * Check for bit error, desync error,parity error,timeout error and
593 * receive overflow errors
594 */
Brian Niebuhr839c9962010-08-23 16:39:19 +0530595 if (errors) {
596 ret = davinci_spi_check_error(davinci_spi, errors);
597 WARN(!ret, "%s: error reported but no error found!\n",
598 dev_name(&spi->dev));
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000599 return ret;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530600 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000601
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000602 return t->len;
603}
604
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530605/**
606 * davinci_spi_irq - Interrupt handler for SPI Master Controller
607 * @irq: IRQ number for this SPI Master
608 * @context_data: structure for SPI Master controller davinci_spi
609 *
610 * ISR will determine that interrupt arrives either for READ or WRITE command.
611 * According to command it will do the appropriate action. It will check
612 * transfer length and if it is not zero then dispatch transfer command again.
613 * If transfer length is zero then it will indicate the COMPLETION so that
614 * davinci_spi_bufs function can go ahead.
615 */
616static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
617{
618 struct davinci_spi *davinci_spi = context_data;
619 int status;
620
621 status = davinci_spi_process_events(davinci_spi);
622 if (unlikely(status != 0))
623 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
624
625 if ((!davinci_spi->rcount && !davinci_spi->wcount) || status)
626 complete(&davinci_spi->done);
627
628 return IRQ_HANDLED;
629}
630
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000631static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
632{
633 struct davinci_spi *davinci_spi;
634 int int_status = 0;
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530635 int count;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530636 unsigned rx_buf_count;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000637 struct davinci_spi_dma *davinci_spi_dma;
Brian Niebuhrb7ab24a2010-08-19 16:42:42 +0530638 int data_type, ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000639 unsigned long tx_reg, rx_reg;
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530640 struct davinci_spi_platform_data *pdata;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530641 void *rx_buf;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000642 struct device *sdev;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530643 struct edmacc_param param;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000644
645 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530646 pdata = davinci_spi->pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000647 sdev = davinci_spi->bitbang.master->dev.parent;
648
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530649 davinci_spi_dma = &davinci_spi->dma_channels;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000650
651 tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
652 rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
653
654 davinci_spi->tx = t->tx_buf;
655 davinci_spi->rx = t->rx_buf;
656
657 /* convert len to words based on bits_per_word */
Brian Niebuhrb7ab24a2010-08-19 16:42:42 +0530658 data_type = davinci_spi->bytes_per_word[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000659
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000660 init_completion(&davinci_spi_dma->dma_rx_completion);
661 init_completion(&davinci_spi_dma->dma_tx_completion);
662
Brian Niebuhrf2bf4e82010-08-20 15:28:23 +0530663 count = t->len / data_type; /* the number of elements */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000664
665 /* disable all interrupts for dma transfers */
666 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000667 /* Enable SPI */
668 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
669
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530670 /*
671 * Transmit DMA setup
672 *
673 * If there is transmit data, map the transmit buffer, set it as the
674 * source of data and set the source B index to data size.
675 * If there is no transmit data, set the transmit register as the
676 * source of data, and set the source B index to zero.
677 *
678 * The destination is always the transmit register itself. And the
679 * destination never increments.
680 */
681
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000682 if (t->tx_buf) {
683 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
684 DMA_TO_DEVICE);
685 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
686 dev_dbg(sdev, "Unable to DMA map a %d bytes"
687 " TX buffer\n", count);
688 return -ENOMEM;
689 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000690 }
691
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530692 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_tx_channel);
693 param.src = t->tx_buf ? t->tx_dma : tx_reg;
694 param.a_b_cnt = count << 16 | data_type;
695 param.dst = tx_reg;
696 param.src_dst_bidx = t->tx_buf ? data_type : 0;
697 param.link_bcntrld = 0xffff;
698 param.src_dst_cidx = 0;
699 param.ccnt = 1;
700 edma_write_slot(davinci_spi_dma->dma_tx_channel, &param);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530701 edma_link(davinci_spi_dma->dma_tx_channel,
702 davinci_spi_dma->dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000703
Brian Niebuhre91c6592010-10-01 10:29:29 +0530704 /*
705 * Receive DMA setup
706 *
707 * If there is receive buffer, use it to receive data. If there
708 * is none provided, use a temporary receive buffer. Set the
709 * destination B index to 0 so effectively only one byte is used
710 * in the temporary buffer (address does not increment).
711 *
712 * The source of receive data is the receive data register. The
713 * source address never increments.
714 */
715
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000716 if (t->rx_buf) {
Brian Niebuhre91c6592010-10-01 10:29:29 +0530717 rx_buf = t->rx_buf;
718 rx_buf_count = count;
719 } else {
720 rx_buf = davinci_spi->rx_tmp_buf;
721 rx_buf_count = sizeof(davinci_spi->rx_tmp_buf);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000722 }
723
Brian Niebuhre91c6592010-10-01 10:29:29 +0530724 t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count,
725 DMA_FROM_DEVICE);
726 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
727 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
728 rx_buf_count);
729 if (t->tx_buf)
730 dma_unmap_single(NULL, t->tx_dma, count, DMA_TO_DEVICE);
731 return -ENOMEM;
732 }
733
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530734 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_rx_channel);
735 param.src = rx_reg;
736 param.a_b_cnt = count << 16 | data_type;
737 param.dst = t->rx_dma;
738 param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16;
739 param.link_bcntrld = 0xffff;
740 param.src_dst_cidx = 0;
741 param.ccnt = 1;
742 edma_write_slot(davinci_spi_dma->dma_rx_channel, &param);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530743
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530744 if (pdata->cshold_bug) {
745 u16 spidat1 = ioread16(davinci_spi->base + SPIDAT1 + 2);
746 iowrite16(spidat1, davinci_spi->base + SPIDAT1 + 2);
747 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000748
Brian Niebuhre91c6592010-10-01 10:29:29 +0530749 edma_start(davinci_spi_dma->dma_rx_channel);
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530750 edma_start(davinci_spi_dma->dma_tx_channel);
Brian Niebuhra4f44972010-10-01 14:00:48 +0530751 set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000752
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530753 wait_for_completion_interruptible(&davinci_spi_dma->dma_tx_completion);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530754 wait_for_completion_interruptible(&davinci_spi_dma->dma_rx_completion);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000755
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530756 if (t->tx_buf)
757 dma_unmap_single(NULL, t->tx_dma, count, DMA_TO_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000758
Brian Niebuhre91c6592010-10-01 10:29:29 +0530759 dma_unmap_single(NULL, t->rx_dma, rx_buf_count, DMA_FROM_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000760
Brian Niebuhra4f44972010-10-01 14:00:48 +0530761 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
762
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000763 /*
764 * Check for bit error, desync error,parity error,timeout error and
765 * receive overflow errors
766 */
767 int_status = ioread32(davinci_spi->base + SPIFLG);
768
769 ret = davinci_spi_check_error(davinci_spi, int_status);
770 if (ret != 0)
771 return ret;
772
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000773 return t->len;
774}
775
Sekhar Nori903ca252010-10-01 14:51:40 +0530776static int davinci_spi_request_dma(struct davinci_spi_dma *davinci_spi_dma)
777{
778 int r;
779
780 r = edma_alloc_channel(davinci_spi_dma->dma_rx_channel,
781 davinci_spi_dma_rx_callback, davinci_spi_dma,
782 davinci_spi_dma->eventq);
783 if (r < 0) {
784 pr_err("Unable to request DMA channel for SPI RX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530785 r = -EAGAIN;
786 goto rx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530787 }
788
789 r = edma_alloc_channel(davinci_spi_dma->dma_tx_channel,
790 davinci_spi_dma_tx_callback, davinci_spi_dma,
791 davinci_spi_dma->eventq);
792 if (r < 0) {
Sekhar Nori903ca252010-10-01 14:51:40 +0530793 pr_err("Unable to request DMA channel for SPI TX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530794 r = -EAGAIN;
795 goto tx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530796 }
797
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530798 r = edma_alloc_slot(EDMA_CTLR(davinci_spi_dma->dma_tx_channel),
799 EDMA_SLOT_ANY);
800 if (r < 0) {
801 pr_err("Unable to request SPI TX DMA param slot\n");
802 r = -EAGAIN;
803 goto param_failed;
804 }
805 davinci_spi_dma->dummy_param_slot = r;
806 edma_link(davinci_spi_dma->dummy_param_slot,
807 davinci_spi_dma->dummy_param_slot);
808
Sekhar Nori903ca252010-10-01 14:51:40 +0530809 return 0;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530810param_failed:
811 edma_free_channel(davinci_spi_dma->dma_tx_channel);
812tx_dma_failed:
813 edma_free_channel(davinci_spi_dma->dma_rx_channel);
814rx_dma_failed:
815 return r;
Sekhar Nori903ca252010-10-01 14:51:40 +0530816}
817
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000818/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000819 * davinci_spi_probe - probe function for SPI Master Controller
820 * @pdev: platform_device structure which contains plateform specific data
821 */
822static int davinci_spi_probe(struct platform_device *pdev)
823{
824 struct spi_master *master;
825 struct davinci_spi *davinci_spi;
826 struct davinci_spi_platform_data *pdata;
827 struct resource *r, *mem;
828 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
829 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
830 resource_size_t dma_eventq = SPI_NO_RESOURCE;
831 int i = 0, ret = 0;
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530832 u32 spipc0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000833
834 pdata = pdev->dev.platform_data;
835 if (pdata == NULL) {
836 ret = -ENODEV;
837 goto err;
838 }
839
840 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
841 if (master == NULL) {
842 ret = -ENOMEM;
843 goto err;
844 }
845
846 dev_set_drvdata(&pdev->dev, master);
847
848 davinci_spi = spi_master_get_devdata(master);
849 if (davinci_spi == NULL) {
850 ret = -ENOENT;
851 goto free_master;
852 }
853
854 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
855 if (r == NULL) {
856 ret = -ENOENT;
857 goto free_master;
858 }
859
860 davinci_spi->pbase = r->start;
861 davinci_spi->region_size = resource_size(r);
862 davinci_spi->pdata = pdata;
863
864 mem = request_mem_region(r->start, davinci_spi->region_size,
865 pdev->name);
866 if (mem == NULL) {
867 ret = -EBUSY;
868 goto free_master;
869 }
870
Sekhar Nori50356dd2010-10-08 15:27:26 +0530871 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000872 if (davinci_spi->base == NULL) {
873 ret = -ENOMEM;
874 goto release_region;
875 }
876
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530877 davinci_spi->irq = platform_get_irq(pdev, 0);
878 if (davinci_spi->irq <= 0) {
879 ret = -EINVAL;
880 goto unmap_io;
881 }
882
883 ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0,
884 dev_name(&pdev->dev), davinci_spi);
885 if (ret)
886 goto unmap_io;
887
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000888 davinci_spi->bitbang.master = spi_master_get(master);
889 if (davinci_spi->bitbang.master == NULL) {
890 ret = -ENODEV;
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530891 goto irq_free;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000892 }
893
894 davinci_spi->clk = clk_get(&pdev->dev, NULL);
895 if (IS_ERR(davinci_spi->clk)) {
896 ret = -ENODEV;
897 goto put_master;
898 }
899 clk_enable(davinci_spi->clk);
900
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000901 master->bus_num = pdev->id;
902 master->num_chipselect = pdata->num_chipselect;
903 master->setup = davinci_spi_setup;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000904
905 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
906 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
907
908 davinci_spi->version = pdata->version;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000909
910 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
911 if (davinci_spi->version == SPI_VERSION_2)
912 davinci_spi->bitbang.flags |= SPI_READY;
913
Sekhar Nori903ca252010-10-01 14:51:40 +0530914 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
915 if (r)
916 dma_rx_chan = r->start;
917 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
918 if (r)
919 dma_tx_chan = r->start;
920 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
921 if (r)
922 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000923
Sekhar Nori903ca252010-10-01 14:51:40 +0530924 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
925 if (dma_rx_chan != SPI_NO_RESOURCE &&
926 dma_tx_chan != SPI_NO_RESOURCE &&
927 dma_eventq != SPI_NO_RESOURCE) {
928 davinci_spi->dma_channels.dma_rx_channel = dma_rx_chan;
929 davinci_spi->dma_channels.dma_tx_channel = dma_tx_chan;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530930 davinci_spi->dma_channels.eventq = dma_eventq;
931
Sekhar Nori903ca252010-10-01 14:51:40 +0530932 ret = davinci_spi_request_dma(&davinci_spi->dma_channels);
933 if (ret)
934 goto free_clk;
935
936 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000937 dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
938 "Using RX channel = %d , TX channel = %d and "
939 "event queue = %d", dma_rx_chan, dma_tx_chan,
940 dma_eventq);
941 }
942
943 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
944 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
945
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530946 init_completion(&davinci_spi->done);
947
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000948 /* Reset In/OUT SPI module */
949 iowrite32(0, davinci_spi->base + SPIGCR0);
950 udelay(100);
951 iowrite32(1, davinci_spi->base + SPIGCR0);
952
Brian Niebuhrbe884712010-09-03 12:15:28 +0530953 /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530954 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
955 iowrite32(spipc0, davinci_spi->base + SPIPC0);
956
Brian Niebuhr23853972010-08-13 10:57:44 +0530957 /* initialize chip selects */
958 if (pdata->chip_sel) {
959 for (i = 0; i < pdata->num_chipselect; i++) {
960 if (pdata->chip_sel[i] != SPI_INTERN_CS)
961 gpio_direction_output(pdata->chip_sel[i], 1);
962 }
963 }
964
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000965 /* Clock internal */
966 if (davinci_spi->pdata->clk_internal)
967 set_io_bits(davinci_spi->base + SPIGCR1,
968 SPIGCR1_CLKMOD_MASK);
969 else
970 clear_io_bits(davinci_spi->base + SPIGCR1,
971 SPIGCR1_CLKMOD_MASK);
972
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530973 if (pdata->intr_line)
974 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
975 else
976 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
977
Brian Niebuhr843a7132010-08-12 12:49:05 +0530978 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
979
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000980 /* master mode default */
981 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
982
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000983 ret = spi_bitbang_start(&davinci_spi->bitbang);
984 if (ret)
Sekhar Nori903ca252010-10-01 14:51:40 +0530985 goto free_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000986
Brian Niebuhr3b740b12010-09-03 14:50:07 +0530987 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000988
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000989 return ret;
990
Sekhar Nori903ca252010-10-01 14:51:40 +0530991free_dma:
992 edma_free_channel(davinci_spi->dma_channels.dma_tx_channel);
993 edma_free_channel(davinci_spi->dma_channels.dma_rx_channel);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530994 edma_free_slot(davinci_spi->dma_channels.dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000995free_clk:
996 clk_disable(davinci_spi->clk);
997 clk_put(davinci_spi->clk);
998put_master:
999 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +05301000irq_free:
1001 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001002unmap_io:
1003 iounmap(davinci_spi->base);
1004release_region:
1005 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1006free_master:
1007 kfree(master);
1008err:
1009 return ret;
1010}
1011
1012/**
1013 * davinci_spi_remove - remove function for SPI Master Controller
1014 * @pdev: platform_device structure which contains plateform specific data
1015 *
1016 * This function will do the reverse action of davinci_spi_probe function
1017 * It will free the IRQ and SPI controller's memory region.
1018 * It will also call spi_bitbang_stop to destroy the work queue which was
1019 * created by spi_bitbang_start.
1020 */
1021static int __exit davinci_spi_remove(struct platform_device *pdev)
1022{
1023 struct davinci_spi *davinci_spi;
1024 struct spi_master *master;
1025
1026 master = dev_get_drvdata(&pdev->dev);
1027 davinci_spi = spi_master_get_devdata(master);
1028
1029 spi_bitbang_stop(&davinci_spi->bitbang);
1030
1031 clk_disable(davinci_spi->clk);
1032 clk_put(davinci_spi->clk);
1033 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +05301034 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001035 iounmap(davinci_spi->base);
1036 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1037
1038 return 0;
1039}
1040
1041static struct platform_driver davinci_spi_driver = {
1042 .driver.name = "spi_davinci",
1043 .remove = __exit_p(davinci_spi_remove),
1044};
1045
1046static int __init davinci_spi_init(void)
1047{
1048 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1049}
1050module_init(davinci_spi_init);
1051
1052static void __exit davinci_spi_exit(void)
1053{
1054 platform_driver_unregister(&davinci_spi_driver);
1055}
1056module_exit(davinci_spi_exit);
1057
1058MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1059MODULE_LICENSE("GPL");