blob: 10b0a08d262509066625cbc53f11894a2076b6e6 [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
41#define SPI_BUFSIZ (SMP_CACHE_BYTES + 1)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000042
43#define SPIFMT_PHASE_MASK BIT(16)
44#define SPIFMT_POLARITY_MASK BIT(17)
45#define SPIFMT_DISTIMER_MASK BIT(18)
46#define SPIFMT_SHIFTDIR_MASK BIT(20)
47#define SPIFMT_WAITENA_MASK BIT(21)
48#define SPIFMT_PARITYENA_MASK BIT(22)
49#define SPIFMT_ODD_PARITY_MASK BIT(23)
50#define SPIFMT_WDELAY_MASK 0x3f000000u
51#define SPIFMT_WDELAY_SHIFT 24
Brian Niebuhr7fe00922010-08-13 13:27:23 +053052#define SPIFMT_PRESCALE_SHIFT 8
Sandeep Paulraj358934a2009-12-16 22:02:18 +000053
Sandeep Paulraj358934a2009-12-16 22:02:18 +000054
55/* SPIPC0 */
56#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
57#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
58#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
59#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000060
61#define SPIINT_MASKALL 0x0101035F
62#define SPI_INTLVL_1 0x000001FFu
63#define SPI_INTLVL_0 0x00000000u
64
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +053065/* SPIDAT1 (upper 16 bit defines) */
66#define SPIDAT1_CSHOLD_MASK BIT(12)
67
68/* SPIGCR1 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000069#define SPIGCR1_CLKMOD_MASK BIT(1)
70#define SPIGCR1_MASTER_MASK BIT(0)
71#define SPIGCR1_LOOPBACK_MASK BIT(16)
Sekhar Nori8e206f12010-08-20 16:20:49 +053072#define SPIGCR1_SPIENA_MASK BIT(24)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000073
74/* SPIBUF */
75#define SPIBUF_TXFULL_MASK BIT(29)
76#define SPIBUF_RXEMPTY_MASK BIT(31)
77
Brian Niebuhr7abbf232010-08-19 15:07:38 +053078/* SPIDELAY */
79#define SPIDELAY_C2TDELAY_SHIFT 24
80#define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
81#define SPIDELAY_T2CDELAY_SHIFT 16
82#define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
83#define SPIDELAY_T2EDELAY_SHIFT 8
84#define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
85#define SPIDELAY_C2EDELAY_SHIFT 0
86#define SPIDELAY_C2EDELAY_MASK 0xFF
87
Sandeep Paulraj358934a2009-12-16 22:02:18 +000088/* Error Masks */
89#define SPIFLG_DLEN_ERR_MASK BIT(0)
90#define SPIFLG_TIMEOUT_MASK BIT(1)
91#define SPIFLG_PARERR_MASK BIT(2)
92#define SPIFLG_DESYNC_MASK BIT(3)
93#define SPIFLG_BITERR_MASK BIT(4)
94#define SPIFLG_OVRRUN_MASK BIT(6)
95#define SPIFLG_RX_INTR_MASK BIT(8)
96#define SPIFLG_TX_INTR_MASK BIT(9)
97#define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000098
Sandeep Paulraj358934a2009-12-16 22:02:18 +000099#define SPIINT_BITERR_INTR BIT(4)
100#define SPIINT_OVRRUN_INTR BIT(6)
101#define SPIINT_RX_INTR BIT(8)
102#define SPIINT_TX_INTR BIT(9)
103#define SPIINT_DMA_REQ_EN BIT(16)
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000104
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000105/* SPI Controller registers */
106#define SPIGCR0 0x00
107#define SPIGCR1 0x04
108#define SPIINT 0x08
109#define SPILVL 0x0c
110#define SPIFLG 0x10
111#define SPIPC0 0x14
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000112#define SPIDAT1 0x3c
113#define SPIBUF 0x40
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000114#define SPIDELAY 0x48
115#define SPIDEF 0x4c
116#define SPIFMT0 0x50
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000117
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000118/* We have 2 DMA channels per CS, one for RX and one for TX */
119struct davinci_spi_dma {
120 int dma_tx_channel;
121 int dma_rx_channel;
122 int dma_tx_sync_dev;
123 int dma_rx_sync_dev;
124 enum dma_event_q eventq;
125
126 struct completion dma_tx_completion;
127 struct completion dma_rx_completion;
128};
129
130/* SPI Controller driver's private data. */
131struct davinci_spi {
132 struct spi_bitbang bitbang;
133 struct clk *clk;
134
135 u8 version;
136 resource_size_t pbase;
137 void __iomem *base;
138 size_t region_size;
139 u32 irq;
140 struct completion done;
141
142 const void *tx;
143 void *rx;
144 u8 *tmp_buf;
145 int count;
146 struct davinci_spi_dma *dma_channels;
Brian Niebuhr778e2612010-09-03 15:15:06 +0530147 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000148
149 void (*get_rx)(u32 rx_data, struct davinci_spi *);
150 u32 (*get_tx)(struct davinci_spi *);
151
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530152 u8 bytes_per_word[SPI_MAX_CHIPSELECT];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000153};
154
Brian Niebuhr53a31b02010-08-16 15:05:51 +0530155static struct davinci_spi_config davinci_spi_default_cfg;
156
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000157static unsigned use_dma;
158
159static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
160{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530161 if (davinci_spi->rx) {
162 u8 *rx = davinci_spi->rx;
163 *rx++ = (u8)data;
164 davinci_spi->rx = rx;
165 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000166}
167
168static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
169{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530170 if (davinci_spi->rx) {
171 u16 *rx = davinci_spi->rx;
172 *rx++ = (u16)data;
173 davinci_spi->rx = rx;
174 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000175}
176
177static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
178{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530179 u32 data = 0;
180 if (davinci_spi->tx) {
181 const u8 *tx = davinci_spi->tx;
182 data = *tx++;
183 davinci_spi->tx = tx;
184 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000185 return data;
186}
187
188static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
189{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530190 u32 data = 0;
191 if (davinci_spi->tx) {
192 const u16 *tx = davinci_spi->tx;
193 data = *tx++;
194 davinci_spi->tx = tx;
195 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000196 return data;
197}
198
199static inline void set_io_bits(void __iomem *addr, u32 bits)
200{
201 u32 v = ioread32(addr);
202
203 v |= bits;
204 iowrite32(v, addr);
205}
206
207static inline void clear_io_bits(void __iomem *addr, u32 bits)
208{
209 u32 v = ioread32(addr);
210
211 v &= ~bits;
212 iowrite32(v, addr);
213}
214
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000215static void davinci_spi_set_dma_req(const struct spi_device *spi, int enable)
216{
217 struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
218
219 if (enable)
220 set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
221 else
222 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
223}
224
225/*
226 * Interface to control the chip select signal
227 */
228static void davinci_spi_chipselect(struct spi_device *spi, int value)
229{
230 struct davinci_spi *davinci_spi;
231 struct davinci_spi_platform_data *pdata;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530232 u8 chip_sel = spi->chip_select;
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +0530233 u16 spidat1_cfg = CS_DEFAULT;
Brian Niebuhr23853972010-08-13 10:57:44 +0530234 bool gpio_chipsel = false;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000235
236 davinci_spi = spi_master_get_devdata(spi->master);
237 pdata = davinci_spi->pdata;
238
Brian Niebuhr23853972010-08-13 10:57:44 +0530239 if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
240 pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
241 gpio_chipsel = true;
242
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000243 /*
244 * Board specific chip select logic decides the polarity and cs
245 * line for the controller
246 */
Brian Niebuhr23853972010-08-13 10:57:44 +0530247 if (gpio_chipsel) {
248 if (value == BITBANG_CS_ACTIVE)
249 gpio_set_value(pdata->chip_sel[chip_sel], 0);
250 else
251 gpio_set_value(pdata->chip_sel[chip_sel], 1);
252 } else {
253 if (value == BITBANG_CS_ACTIVE) {
254 spidat1_cfg |= SPIDAT1_CSHOLD_MASK;
255 spidat1_cfg &= ~(0x1 << chip_sel);
256 }
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530257
Brian Niebuhr23853972010-08-13 10:57:44 +0530258 iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2);
259 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000260}
261
262/**
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530263 * davinci_spi_get_prescale - Calculates the correct prescale value
264 * @maxspeed_hz: the maximum rate the SPI clock can run at
265 *
266 * This function calculates the prescale value that generates a clock rate
267 * less than or equal to the specified maximum.
268 *
269 * Returns: calculated prescale - 1 for easy programming into SPI registers
270 * or negative error number if valid prescalar cannot be updated.
271 */
272static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi,
273 u32 max_speed_hz)
274{
275 int ret;
276
277 ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz);
278
279 if (ret < 3 || ret > 256)
280 return -EINVAL;
281
282 return ret - 1;
283}
284
285/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000286 * davinci_spi_setup_transfer - This functions will determine transfer method
287 * @spi: spi device on which data transfer to be done
288 * @t: spi transfer in which transfer info is filled
289 *
290 * This function determines data transfer method (8/16/32 bit transfer).
291 * It will also set the SPI Clock Control register according to
292 * SPI slave device freq.
293 */
294static int davinci_spi_setup_transfer(struct spi_device *spi,
295 struct spi_transfer *t)
296{
297
298 struct davinci_spi *davinci_spi;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530299 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000300 u8 bits_per_word = 0;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530301 u32 hz = 0, spifmt = 0, prescale = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000302
303 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530304 spicfg = (struct davinci_spi_config *)spi->controller_data;
305 if (!spicfg)
306 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000307
308 if (t) {
309 bits_per_word = t->bits_per_word;
310 hz = t->speed_hz;
311 }
312
313 /* if bits_per_word is not set then set it default */
314 if (!bits_per_word)
315 bits_per_word = spi->bits_per_word;
316
317 /*
318 * Assign function pointer to appropriate transfer method
319 * 8bit, 16bit or 32bit transfer
320 */
321 if (bits_per_word <= 8 && bits_per_word >= 2) {
322 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
323 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530324 davinci_spi->bytes_per_word[spi->chip_select] = 1;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000325 } else if (bits_per_word <= 16 && bits_per_word >= 2) {
326 davinci_spi->get_rx = davinci_spi_rx_buf_u16;
327 davinci_spi->get_tx = davinci_spi_tx_buf_u16;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530328 davinci_spi->bytes_per_word[spi->chip_select] = 2;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000329 } else
330 return -EINVAL;
331
332 if (!hz)
333 hz = spi->max_speed_hz;
334
Brian Niebuhr25f33512010-08-19 12:15:22 +0530335 /* Set up SPIFMTn register, unique to this chipselect. */
336
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530337 prescale = davinci_spi_get_prescale(davinci_spi, hz);
338 if (prescale < 0)
339 return prescale;
340
Brian Niebuhr25f33512010-08-19 12:15:22 +0530341 spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000342
Brian Niebuhr25f33512010-08-19 12:15:22 +0530343 if (spi->mode & SPI_LSB_FIRST)
344 spifmt |= SPIFMT_SHIFTDIR_MASK;
345
346 if (spi->mode & SPI_CPOL)
347 spifmt |= SPIFMT_POLARITY_MASK;
348
349 if (!(spi->mode & SPI_CPHA))
350 spifmt |= SPIFMT_PHASE_MASK;
351
352 /*
353 * Version 1 hardware supports two basic SPI modes:
354 * - Standard SPI mode uses 4 pins, with chipselect
355 * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
356 * (distinct from SPI_3WIRE, with just one data wire;
357 * or similar variants without MOSI or without MISO)
358 *
359 * Version 2 hardware supports an optional handshaking signal,
360 * so it can support two more modes:
361 * - 5 pin SPI variant is standard SPI plus SPI_READY
362 * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
363 */
364
365 if (davinci_spi->version == SPI_VERSION_2) {
366
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530367 u32 delay = 0;
368
Brian Niebuhr25f33512010-08-19 12:15:22 +0530369 spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
370 & SPIFMT_WDELAY_MASK);
371
372 if (spicfg->odd_parity)
373 spifmt |= SPIFMT_ODD_PARITY_MASK;
374
375 if (spicfg->parity_enable)
376 spifmt |= SPIFMT_PARITYENA_MASK;
377
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530378 if (spicfg->timer_disable) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530379 spifmt |= SPIFMT_DISTIMER_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530380 } else {
381 delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
382 & SPIDELAY_C2TDELAY_MASK;
383 delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
384 & SPIDELAY_T2CDELAY_MASK;
385 }
Brian Niebuhr25f33512010-08-19 12:15:22 +0530386
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530387 if (spi->mode & SPI_READY) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530388 spifmt |= SPIFMT_WAITENA_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530389 delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
390 & SPIDELAY_T2EDELAY_MASK;
391 delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
392 & SPIDELAY_C2EDELAY_MASK;
393 }
394
395 iowrite32(delay, davinci_spi->base + SPIDELAY);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530396 }
397
398 iowrite32(spifmt, davinci_spi->base + SPIFMT0);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000399
400 return 0;
401}
402
403static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data)
404{
405 struct spi_device *spi = (struct spi_device *)data;
406 struct davinci_spi *davinci_spi;
407 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000408
409 davinci_spi = spi_master_get_devdata(spi->master);
410 davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000411
412 if (ch_status == DMA_COMPLETE)
413 edma_stop(davinci_spi_dma->dma_rx_channel);
414 else
415 edma_clean_channel(davinci_spi_dma->dma_rx_channel);
416
417 complete(&davinci_spi_dma->dma_rx_completion);
418 /* We must disable the DMA RX request */
419 davinci_spi_set_dma_req(spi, 0);
420}
421
422static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
423{
424 struct spi_device *spi = (struct spi_device *)data;
425 struct davinci_spi *davinci_spi;
426 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000427
428 davinci_spi = spi_master_get_devdata(spi->master);
429 davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000430
431 if (ch_status == DMA_COMPLETE)
432 edma_stop(davinci_spi_dma->dma_tx_channel);
433 else
434 edma_clean_channel(davinci_spi_dma->dma_tx_channel);
435
436 complete(&davinci_spi_dma->dma_tx_completion);
437 /* We must disable the DMA TX request */
438 davinci_spi_set_dma_req(spi, 0);
439}
440
441static int davinci_spi_request_dma(struct spi_device *spi)
442{
443 struct davinci_spi *davinci_spi;
444 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000445 struct device *sdev;
446 int r;
447
448 davinci_spi = spi_master_get_devdata(spi->master);
449 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000450 sdev = davinci_spi->bitbang.master->dev.parent;
451
452 r = edma_alloc_channel(davinci_spi_dma->dma_rx_sync_dev,
453 davinci_spi_dma_rx_callback, spi,
454 davinci_spi_dma->eventq);
455 if (r < 0) {
456 dev_dbg(sdev, "Unable to request DMA channel for SPI RX\n");
457 return -EAGAIN;
458 }
459 davinci_spi_dma->dma_rx_channel = r;
460 r = edma_alloc_channel(davinci_spi_dma->dma_tx_sync_dev,
461 davinci_spi_dma_tx_callback, spi,
462 davinci_spi_dma->eventq);
463 if (r < 0) {
464 edma_free_channel(davinci_spi_dma->dma_rx_channel);
465 davinci_spi_dma->dma_rx_channel = -1;
466 dev_dbg(sdev, "Unable to request DMA channel for SPI TX\n");
467 return -EAGAIN;
468 }
469 davinci_spi_dma->dma_tx_channel = r;
470
471 return 0;
472}
473
474/**
475 * davinci_spi_setup - This functions will set default transfer method
476 * @spi: spi device on which data transfer to be done
477 *
478 * This functions sets the default transfer method.
479 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000480static int davinci_spi_setup(struct spi_device *spi)
481{
482 int retval;
483 struct davinci_spi *davinci_spi;
484 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000485
486 davinci_spi = spi_master_get_devdata(spi->master);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000487
488 /* if bits per word length is zero then set it default 8 */
489 if (!spi->bits_per_word)
490 spi->bits_per_word = 8;
491
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000492 if (use_dma && davinci_spi->dma_channels) {
493 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
494
495 if ((davinci_spi_dma->dma_rx_channel == -1)
496 || (davinci_spi_dma->dma_tx_channel == -1)) {
497 retval = davinci_spi_request_dma(spi);
498 if (retval < 0)
499 return retval;
500 }
501 }
502
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000503 retval = davinci_spi_setup_transfer(spi, NULL);
504
505 return retval;
506}
507
508static void davinci_spi_cleanup(struct spi_device *spi)
509{
510 struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
511 struct davinci_spi_dma *davinci_spi_dma;
512
513 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
514
515 if (use_dma && davinci_spi->dma_channels) {
516 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
517
518 if ((davinci_spi_dma->dma_rx_channel != -1)
519 && (davinci_spi_dma->dma_tx_channel != -1)) {
520 edma_free_channel(davinci_spi_dma->dma_tx_channel);
521 edma_free_channel(davinci_spi_dma->dma_rx_channel);
522 }
523 }
524}
525
526static int davinci_spi_bufs_prep(struct spi_device *spi,
527 struct davinci_spi *davinci_spi)
528{
Brian Niebuhr23853972010-08-13 10:57:44 +0530529 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000530 int op_mode = 0;
531
532 /*
533 * REVISIT unless devices disagree about SPI_LOOP or
534 * SPI_READY (SPI_NO_CS only allows one device!), this
535 * should not need to be done before each message...
536 * optimize for both flags staying cleared.
537 */
538
539 op_mode = SPIPC0_DIFUN_MASK
540 | SPIPC0_DOFUN_MASK
541 | SPIPC0_CLKFUN_MASK;
Brian Niebuhr23853972010-08-13 10:57:44 +0530542 if (!(spi->mode & SPI_NO_CS)) {
543 pdata = davinci_spi->pdata;
544 if (!pdata->chip_sel ||
545 pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS)
546 op_mode |= 1 << spi->chip_select;
547 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000548 if (spi->mode & SPI_READY)
549 op_mode |= SPIPC0_SPIENA_MASK;
550
551 iowrite32(op_mode, davinci_spi->base + SPIPC0);
552
553 if (spi->mode & SPI_LOOP)
554 set_io_bits(davinci_spi->base + SPIGCR1,
555 SPIGCR1_LOOPBACK_MASK);
556 else
557 clear_io_bits(davinci_spi->base + SPIGCR1,
558 SPIGCR1_LOOPBACK_MASK);
559
560 return 0;
561}
562
563static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
564 int int_status)
565{
566 struct device *sdev = davinci_spi->bitbang.master->dev.parent;
567
568 if (int_status & SPIFLG_TIMEOUT_MASK) {
569 dev_dbg(sdev, "SPI Time-out Error\n");
570 return -ETIMEDOUT;
571 }
572 if (int_status & SPIFLG_DESYNC_MASK) {
573 dev_dbg(sdev, "SPI Desynchronization Error\n");
574 return -EIO;
575 }
576 if (int_status & SPIFLG_BITERR_MASK) {
577 dev_dbg(sdev, "SPI Bit error\n");
578 return -EIO;
579 }
580
581 if (davinci_spi->version == SPI_VERSION_2) {
582 if (int_status & SPIFLG_DLEN_ERR_MASK) {
583 dev_dbg(sdev, "SPI Data Length Error\n");
584 return -EIO;
585 }
586 if (int_status & SPIFLG_PARERR_MASK) {
587 dev_dbg(sdev, "SPI Parity Error\n");
588 return -EIO;
589 }
590 if (int_status & SPIFLG_OVRRUN_MASK) {
591 dev_dbg(sdev, "SPI Data Overrun error\n");
592 return -EIO;
593 }
594 if (int_status & SPIFLG_TX_INTR_MASK) {
595 dev_dbg(sdev, "SPI TX intr bit set\n");
596 return -EIO;
597 }
598 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
599 dev_dbg(sdev, "SPI Buffer Init Active\n");
600 return -EBUSY;
601 }
602 }
603
604 return 0;
605}
606
607/**
608 * davinci_spi_bufs - functions which will handle transfer data
609 * @spi: spi device on which data transfer to be done
610 * @t: spi transfer in which transfer info is filled
611 *
612 * This function will put data to be transferred into data register
613 * of SPI controller and then wait until the completion will be marked
614 * by the IRQ Handler.
615 */
616static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
617{
618 struct davinci_spi *davinci_spi;
619 int int_status, count, ret;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530620 u8 conv;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000621 u32 tx_data, data1_reg_val;
622 u32 buf_val, flg_val;
623 struct davinci_spi_platform_data *pdata;
624
625 davinci_spi = spi_master_get_devdata(spi->master);
626 pdata = davinci_spi->pdata;
627
628 davinci_spi->tx = t->tx_buf;
629 davinci_spi->rx = t->rx_buf;
630
631 /* convert len to words based on bits_per_word */
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530632 conv = davinci_spi->bytes_per_word[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000633 davinci_spi->count = t->len / conv;
634
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530635 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
636
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000637 INIT_COMPLETION(davinci_spi->done);
638
639 ret = davinci_spi_bufs_prep(spi, davinci_spi);
640 if (ret)
641 return ret;
642
643 /* Enable SPI */
644 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
645
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000646 count = davinci_spi->count;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000647
648 /* Determine the command to execute READ or WRITE */
649 if (t->tx_buf) {
650 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
651
652 while (1) {
653 tx_data = davinci_spi->get_tx(davinci_spi);
654
655 data1_reg_val &= ~(0xFFFF);
656 data1_reg_val |= (0xFFFF & tx_data);
657
658 buf_val = ioread32(davinci_spi->base + SPIBUF);
659 if ((buf_val & SPIBUF_TXFULL_MASK) == 0) {
660 iowrite32(data1_reg_val,
661 davinci_spi->base + SPIDAT1);
662
663 count--;
664 }
665 while (ioread32(davinci_spi->base + SPIBUF)
666 & SPIBUF_RXEMPTY_MASK)
667 cpu_relax();
668
669 /* getting the returned byte */
670 if (t->rx_buf) {
671 buf_val = ioread32(davinci_spi->base + SPIBUF);
672 davinci_spi->get_rx(buf_val, davinci_spi);
673 }
674 if (count <= 0)
675 break;
676 }
677 } else {
678 if (pdata->poll_mode) {
679 while (1) {
680 /* keeps the serial clock going */
681 if ((ioread32(davinci_spi->base + SPIBUF)
682 & SPIBUF_TXFULL_MASK) == 0)
683 iowrite32(data1_reg_val,
684 davinci_spi->base + SPIDAT1);
685
686 while (ioread32(davinci_spi->base + SPIBUF) &
687 SPIBUF_RXEMPTY_MASK)
688 cpu_relax();
689
690 flg_val = ioread32(davinci_spi->base + SPIFLG);
691 buf_val = ioread32(davinci_spi->base + SPIBUF);
692
693 davinci_spi->get_rx(buf_val, davinci_spi);
694
695 count--;
696 if (count <= 0)
697 break;
698 }
699 } else { /* Receive in Interrupt mode */
700 int i;
701
702 for (i = 0; i < davinci_spi->count; i++) {
703 set_io_bits(davinci_spi->base + SPIINT,
704 SPIINT_BITERR_INTR
705 | SPIINT_OVRRUN_INTR
706 | SPIINT_RX_INTR);
707
708 iowrite32(data1_reg_val,
709 davinci_spi->base + SPIDAT1);
710
711 while (ioread32(davinci_spi->base + SPIINT) &
712 SPIINT_RX_INTR)
713 cpu_relax();
714 }
715 iowrite32((data1_reg_val & 0x0ffcffff),
716 davinci_spi->base + SPIDAT1);
717 }
718 }
719
720 /*
721 * Check for bit error, desync error,parity error,timeout error and
722 * receive overflow errors
723 */
724 int_status = ioread32(davinci_spi->base + SPIFLG);
725
726 ret = davinci_spi_check_error(davinci_spi, int_status);
727 if (ret != 0)
728 return ret;
729
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000730 return t->len;
731}
732
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000733static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
734{
735 struct davinci_spi *davinci_spi;
736 int int_status = 0;
737 int count, temp_count;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000738 u32 data1_reg_val;
739 struct davinci_spi_dma *davinci_spi_dma;
Brian Niebuhrb7ab24a2010-08-19 16:42:42 +0530740 int data_type, ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000741 unsigned long tx_reg, rx_reg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000742 struct device *sdev;
743
744 davinci_spi = spi_master_get_devdata(spi->master);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000745 sdev = davinci_spi->bitbang.master->dev.parent;
746
747 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
748
749 tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
750 rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
751
752 davinci_spi->tx = t->tx_buf;
753 davinci_spi->rx = t->rx_buf;
754
755 /* convert len to words based on bits_per_word */
Brian Niebuhrb7ab24a2010-08-19 16:42:42 +0530756 data_type = davinci_spi->bytes_per_word[spi->chip_select];
757 davinci_spi->count = t->len / data_type;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000758
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530759 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
760
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000761 INIT_COMPLETION(davinci_spi->done);
762
763 init_completion(&davinci_spi_dma->dma_rx_completion);
764 init_completion(&davinci_spi_dma->dma_tx_completion);
765
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000766 ret = davinci_spi_bufs_prep(spi, davinci_spi);
767 if (ret)
768 return ret;
769
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000770 count = davinci_spi->count; /* the number of elements */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000771
772 /* disable all interrupts for dma transfers */
773 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
774 /* Disable SPI to write configuration bits in SPIDAT */
775 clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000776 /* Enable SPI */
777 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
778
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000779 if (t->tx_buf) {
780 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
781 DMA_TO_DEVICE);
782 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
783 dev_dbg(sdev, "Unable to DMA map a %d bytes"
784 " TX buffer\n", count);
785 return -ENOMEM;
786 }
787 temp_count = count;
788 } else {
789 /* We need TX clocking for RX transaction */
790 t->tx_dma = dma_map_single(&spi->dev,
791 (void *)davinci_spi->tmp_buf, count + 1,
792 DMA_TO_DEVICE);
793 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
794 dev_dbg(sdev, "Unable to DMA map a %d bytes"
795 " TX tmp buffer\n", count);
796 return -ENOMEM;
797 }
798 temp_count = count + 1;
799 }
800
801 edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
802 data_type, temp_count, 1, 0, ASYNC);
803 edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
804 edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
805 edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
806 edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
807
808 if (t->rx_buf) {
809 /* initiate transaction */
810 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
811
812 t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
813 DMA_FROM_DEVICE);
814 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
815 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
816 count);
817 if (t->tx_buf != NULL)
818 dma_unmap_single(NULL, t->tx_dma,
819 count, DMA_TO_DEVICE);
820 return -ENOMEM;
821 }
822 edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
823 data_type, count, 1, 0, ASYNC);
824 edma_set_src(davinci_spi_dma->dma_rx_channel,
825 rx_reg, INCR, W8BIT);
826 edma_set_dest(davinci_spi_dma->dma_rx_channel,
827 t->rx_dma, INCR, W8BIT);
828 edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
829 edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
830 data_type, 0);
831 }
832
833 if ((t->tx_buf) || (t->rx_buf))
834 edma_start(davinci_spi_dma->dma_tx_channel);
835
836 if (t->rx_buf)
837 edma_start(davinci_spi_dma->dma_rx_channel);
838
839 if ((t->rx_buf) || (t->tx_buf))
840 davinci_spi_set_dma_req(spi, 1);
841
842 if (t->tx_buf)
843 wait_for_completion_interruptible(
844 &davinci_spi_dma->dma_tx_completion);
845
846 if (t->rx_buf)
847 wait_for_completion_interruptible(
848 &davinci_spi_dma->dma_rx_completion);
849
850 dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
851
852 if (t->rx_buf)
853 dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
854
855 /*
856 * Check for bit error, desync error,parity error,timeout error and
857 * receive overflow errors
858 */
859 int_status = ioread32(davinci_spi->base + SPIFLG);
860
861 ret = davinci_spi_check_error(davinci_spi, int_status);
862 if (ret != 0)
863 return ret;
864
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000865 return t->len;
866}
867
868/**
869 * davinci_spi_irq - IRQ handler for DaVinci SPI
870 * @irq: IRQ number for this SPI Master
871 * @context_data: structure for SPI Master controller davinci_spi
872 */
873static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
874{
875 struct davinci_spi *davinci_spi = context_data;
876 u32 int_status, rx_data = 0;
877 irqreturn_t ret = IRQ_NONE;
878
879 int_status = ioread32(davinci_spi->base + SPIFLG);
880
881 while ((int_status & SPIFLG_RX_INTR_MASK)) {
882 if (likely(int_status & SPIFLG_RX_INTR_MASK)) {
883 ret = IRQ_HANDLED;
884
885 rx_data = ioread32(davinci_spi->base + SPIBUF);
886 davinci_spi->get_rx(rx_data, davinci_spi);
887
888 /* Disable Receive Interrupt */
889 iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR),
890 davinci_spi->base + SPIINT);
891 } else
892 (void)davinci_spi_check_error(davinci_spi, int_status);
893
894 int_status = ioread32(davinci_spi->base + SPIFLG);
895 }
896
897 return ret;
898}
899
900/**
901 * davinci_spi_probe - probe function for SPI Master Controller
902 * @pdev: platform_device structure which contains plateform specific data
903 */
904static int davinci_spi_probe(struct platform_device *pdev)
905{
906 struct spi_master *master;
907 struct davinci_spi *davinci_spi;
908 struct davinci_spi_platform_data *pdata;
909 struct resource *r, *mem;
910 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
911 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
912 resource_size_t dma_eventq = SPI_NO_RESOURCE;
913 int i = 0, ret = 0;
914
915 pdata = pdev->dev.platform_data;
916 if (pdata == NULL) {
917 ret = -ENODEV;
918 goto err;
919 }
920
921 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
922 if (master == NULL) {
923 ret = -ENOMEM;
924 goto err;
925 }
926
927 dev_set_drvdata(&pdev->dev, master);
928
929 davinci_spi = spi_master_get_devdata(master);
930 if (davinci_spi == NULL) {
931 ret = -ENOENT;
932 goto free_master;
933 }
934
935 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
936 if (r == NULL) {
937 ret = -ENOENT;
938 goto free_master;
939 }
940
941 davinci_spi->pbase = r->start;
942 davinci_spi->region_size = resource_size(r);
943 davinci_spi->pdata = pdata;
944
945 mem = request_mem_region(r->start, davinci_spi->region_size,
946 pdev->name);
947 if (mem == NULL) {
948 ret = -EBUSY;
949 goto free_master;
950 }
951
Sekhar Nori50356dd2010-10-08 15:27:26 +0530952 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000953 if (davinci_spi->base == NULL) {
954 ret = -ENOMEM;
955 goto release_region;
956 }
957
958 davinci_spi->irq = platform_get_irq(pdev, 0);
959 if (davinci_spi->irq <= 0) {
960 ret = -EINVAL;
961 goto unmap_io;
962 }
963
964 ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED,
965 dev_name(&pdev->dev), davinci_spi);
966 if (ret)
967 goto unmap_io;
968
969 /* Allocate tmp_buf for tx_buf */
970 davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
971 if (davinci_spi->tmp_buf == NULL) {
972 ret = -ENOMEM;
973 goto irq_free;
974 }
975
976 davinci_spi->bitbang.master = spi_master_get(master);
977 if (davinci_spi->bitbang.master == NULL) {
978 ret = -ENODEV;
979 goto free_tmp_buf;
980 }
981
982 davinci_spi->clk = clk_get(&pdev->dev, NULL);
983 if (IS_ERR(davinci_spi->clk)) {
984 ret = -ENODEV;
985 goto put_master;
986 }
987 clk_enable(davinci_spi->clk);
988
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000989 master->bus_num = pdev->id;
990 master->num_chipselect = pdata->num_chipselect;
991 master->setup = davinci_spi_setup;
992 master->cleanup = davinci_spi_cleanup;
993
994 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
995 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
996
997 davinci_spi->version = pdata->version;
998 use_dma = pdata->use_dma;
999
1000 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
1001 if (davinci_spi->version == SPI_VERSION_2)
1002 davinci_spi->bitbang.flags |= SPI_READY;
1003
1004 if (use_dma) {
Brian Niebuhr778e2612010-09-03 15:15:06 +05301005 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1006 if (r)
1007 dma_rx_chan = r->start;
1008 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1009 if (r)
1010 dma_tx_chan = r->start;
1011 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1012 if (r)
1013 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001014 }
1015
1016 if (!use_dma ||
1017 dma_rx_chan == SPI_NO_RESOURCE ||
1018 dma_tx_chan == SPI_NO_RESOURCE ||
1019 dma_eventq == SPI_NO_RESOURCE) {
1020 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
1021 use_dma = 0;
1022 } else {
1023 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
1024 davinci_spi->dma_channels = kzalloc(master->num_chipselect
1025 * sizeof(struct davinci_spi_dma), GFP_KERNEL);
1026 if (davinci_spi->dma_channels == NULL) {
1027 ret = -ENOMEM;
1028 goto free_clk;
1029 }
1030
1031 for (i = 0; i < master->num_chipselect; i++) {
1032 davinci_spi->dma_channels[i].dma_rx_channel = -1;
1033 davinci_spi->dma_channels[i].dma_rx_sync_dev =
1034 dma_rx_chan;
1035 davinci_spi->dma_channels[i].dma_tx_channel = -1;
1036 davinci_spi->dma_channels[i].dma_tx_sync_dev =
1037 dma_tx_chan;
1038 davinci_spi->dma_channels[i].eventq = dma_eventq;
1039 }
1040 dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
1041 "Using RX channel = %d , TX channel = %d and "
1042 "event queue = %d", dma_rx_chan, dma_tx_chan,
1043 dma_eventq);
1044 }
1045
1046 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
1047 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
1048
1049 init_completion(&davinci_spi->done);
1050
1051 /* Reset In/OUT SPI module */
1052 iowrite32(0, davinci_spi->base + SPIGCR0);
1053 udelay(100);
1054 iowrite32(1, davinci_spi->base + SPIGCR0);
1055
Brian Niebuhr23853972010-08-13 10:57:44 +05301056 /* initialize chip selects */
1057 if (pdata->chip_sel) {
1058 for (i = 0; i < pdata->num_chipselect; i++) {
1059 if (pdata->chip_sel[i] != SPI_INTERN_CS)
1060 gpio_direction_output(pdata->chip_sel[i], 1);
1061 }
1062 }
1063
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001064 /* Clock internal */
1065 if (davinci_spi->pdata->clk_internal)
1066 set_io_bits(davinci_spi->base + SPIGCR1,
1067 SPIGCR1_CLKMOD_MASK);
1068 else
1069 clear_io_bits(davinci_spi->base + SPIGCR1,
1070 SPIGCR1_CLKMOD_MASK);
1071
Brian Niebuhr843a7132010-08-12 12:49:05 +05301072 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
1073
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001074 /* master mode default */
1075 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
1076
1077 if (davinci_spi->pdata->intr_level)
1078 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
1079 else
1080 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
1081
1082 ret = spi_bitbang_start(&davinci_spi->bitbang);
1083 if (ret)
1084 goto free_clk;
1085
Brian Niebuhr3b740b12010-09-03 14:50:07 +05301086 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001087
1088 if (!pdata->poll_mode)
1089 dev_info(&pdev->dev, "Operating in interrupt mode"
1090 " using IRQ %d\n", davinci_spi->irq);
1091
1092 return ret;
1093
1094free_clk:
1095 clk_disable(davinci_spi->clk);
1096 clk_put(davinci_spi->clk);
1097put_master:
1098 spi_master_put(master);
1099free_tmp_buf:
1100 kfree(davinci_spi->tmp_buf);
1101irq_free:
1102 free_irq(davinci_spi->irq, davinci_spi);
1103unmap_io:
1104 iounmap(davinci_spi->base);
1105release_region:
1106 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1107free_master:
1108 kfree(master);
1109err:
1110 return ret;
1111}
1112
1113/**
1114 * davinci_spi_remove - remove function for SPI Master Controller
1115 * @pdev: platform_device structure which contains plateform specific data
1116 *
1117 * This function will do the reverse action of davinci_spi_probe function
1118 * It will free the IRQ and SPI controller's memory region.
1119 * It will also call spi_bitbang_stop to destroy the work queue which was
1120 * created by spi_bitbang_start.
1121 */
1122static int __exit davinci_spi_remove(struct platform_device *pdev)
1123{
1124 struct davinci_spi *davinci_spi;
1125 struct spi_master *master;
1126
1127 master = dev_get_drvdata(&pdev->dev);
1128 davinci_spi = spi_master_get_devdata(master);
1129
1130 spi_bitbang_stop(&davinci_spi->bitbang);
1131
1132 clk_disable(davinci_spi->clk);
1133 clk_put(davinci_spi->clk);
1134 spi_master_put(master);
1135 kfree(davinci_spi->tmp_buf);
1136 free_irq(davinci_spi->irq, davinci_spi);
1137 iounmap(davinci_spi->base);
1138 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1139
1140 return 0;
1141}
1142
1143static struct platform_driver davinci_spi_driver = {
1144 .driver.name = "spi_davinci",
1145 .remove = __exit_p(davinci_spi_remove),
1146};
1147
1148static int __init davinci_spi_init(void)
1149{
1150 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1151}
1152module_init(davinci_spi_init);
1153
1154static void __exit davinci_spi_exit(void)
1155{
1156 platform_driver_unregister(&davinci_spi_driver);
1157}
1158module_exit(davinci_spi_exit);
1159
1160MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1161MODULE_LICENSE("GPL");