blob: 3dac2038b5a6c3cea8da2741b4450175a1e005ba [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)
42#define DAVINCI_DMA_DATA_TYPE_S8 0x01
43#define DAVINCI_DMA_DATA_TYPE_S16 0x02
44#define DAVINCI_DMA_DATA_TYPE_S32 0x04
45
46#define SPIFMT_PHASE_MASK BIT(16)
47#define SPIFMT_POLARITY_MASK BIT(17)
48#define SPIFMT_DISTIMER_MASK BIT(18)
49#define SPIFMT_SHIFTDIR_MASK BIT(20)
50#define SPIFMT_WAITENA_MASK BIT(21)
51#define SPIFMT_PARITYENA_MASK BIT(22)
52#define SPIFMT_ODD_PARITY_MASK BIT(23)
53#define SPIFMT_WDELAY_MASK 0x3f000000u
54#define SPIFMT_WDELAY_SHIFT 24
Brian Niebuhr7fe00922010-08-13 13:27:23 +053055#define SPIFMT_PRESCALE_SHIFT 8
Sandeep Paulraj358934a2009-12-16 22:02:18 +000056
Sandeep Paulraj358934a2009-12-16 22:02:18 +000057
58/* SPIPC0 */
59#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
60#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
61#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
62#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000063
64#define SPIINT_MASKALL 0x0101035F
65#define SPI_INTLVL_1 0x000001FFu
66#define SPI_INTLVL_0 0x00000000u
67
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +053068/* SPIDAT1 (upper 16 bit defines) */
69#define SPIDAT1_CSHOLD_MASK BIT(12)
70
71/* SPIGCR1 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000072#define SPIGCR1_CLKMOD_MASK BIT(1)
73#define SPIGCR1_MASTER_MASK BIT(0)
74#define SPIGCR1_LOOPBACK_MASK BIT(16)
Sekhar Nori8e206f12010-08-20 16:20:49 +053075#define SPIGCR1_SPIENA_MASK BIT(24)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000076
77/* SPIBUF */
78#define SPIBUF_TXFULL_MASK BIT(29)
79#define SPIBUF_RXEMPTY_MASK BIT(31)
80
Brian Niebuhr7abbf232010-08-19 15:07:38 +053081/* SPIDELAY */
82#define SPIDELAY_C2TDELAY_SHIFT 24
83#define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
84#define SPIDELAY_T2CDELAY_SHIFT 16
85#define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
86#define SPIDELAY_T2EDELAY_SHIFT 8
87#define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
88#define SPIDELAY_C2EDELAY_SHIFT 0
89#define SPIDELAY_C2EDELAY_MASK 0xFF
90
Sandeep Paulraj358934a2009-12-16 22:02:18 +000091/* Error Masks */
92#define SPIFLG_DLEN_ERR_MASK BIT(0)
93#define SPIFLG_TIMEOUT_MASK BIT(1)
94#define SPIFLG_PARERR_MASK BIT(2)
95#define SPIFLG_DESYNC_MASK BIT(3)
96#define SPIFLG_BITERR_MASK BIT(4)
97#define SPIFLG_OVRRUN_MASK BIT(6)
98#define SPIFLG_RX_INTR_MASK BIT(8)
99#define SPIFLG_TX_INTR_MASK BIT(9)
100#define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000101
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000102#define SPIINT_BITERR_INTR BIT(4)
103#define SPIINT_OVRRUN_INTR BIT(6)
104#define SPIINT_RX_INTR BIT(8)
105#define SPIINT_TX_INTR BIT(9)
106#define SPIINT_DMA_REQ_EN BIT(16)
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000107
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000108/* SPI Controller registers */
109#define SPIGCR0 0x00
110#define SPIGCR1 0x04
111#define SPIINT 0x08
112#define SPILVL 0x0c
113#define SPIFLG 0x10
114#define SPIPC0 0x14
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000115#define SPIDAT1 0x3c
116#define SPIBUF 0x40
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000117#define SPIDELAY 0x48
118#define SPIDEF 0x4c
119#define SPIFMT0 0x50
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000120
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000121/* We have 2 DMA channels per CS, one for RX and one for TX */
122struct davinci_spi_dma {
123 int dma_tx_channel;
124 int dma_rx_channel;
125 int dma_tx_sync_dev;
126 int dma_rx_sync_dev;
127 enum dma_event_q eventq;
128
129 struct completion dma_tx_completion;
130 struct completion dma_rx_completion;
131};
132
133/* SPI Controller driver's private data. */
134struct davinci_spi {
135 struct spi_bitbang bitbang;
136 struct clk *clk;
137
138 u8 version;
139 resource_size_t pbase;
140 void __iomem *base;
141 size_t region_size;
142 u32 irq;
143 struct completion done;
144
145 const void *tx;
146 void *rx;
147 u8 *tmp_buf;
148 int count;
149 struct davinci_spi_dma *dma_channels;
Brian Niebuhr778e2612010-09-03 15:15:06 +0530150 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000151
152 void (*get_rx)(u32 rx_data, struct davinci_spi *);
153 u32 (*get_tx)(struct davinci_spi *);
154
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530155 u8 bytes_per_word[SPI_MAX_CHIPSELECT];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000156};
157
Brian Niebuhr53a31b02010-08-16 15:05:51 +0530158static struct davinci_spi_config davinci_spi_default_cfg;
159
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000160static unsigned use_dma;
161
162static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
163{
164 u8 *rx = davinci_spi->rx;
165
166 *rx++ = (u8)data;
167 davinci_spi->rx = rx;
168}
169
170static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
171{
172 u16 *rx = davinci_spi->rx;
173
174 *rx++ = (u16)data;
175 davinci_spi->rx = rx;
176}
177
178static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
179{
180 u32 data;
181 const u8 *tx = davinci_spi->tx;
182
183 data = *tx++;
184 davinci_spi->tx = tx;
185 return data;
186}
187
188static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
189{
190 u32 data;
191 const u16 *tx = davinci_spi->tx;
192
193 data = *tx++;
194 davinci_spi->tx = tx;
195 return data;
196}
197
198static inline void set_io_bits(void __iomem *addr, u32 bits)
199{
200 u32 v = ioread32(addr);
201
202 v |= bits;
203 iowrite32(v, addr);
204}
205
206static inline void clear_io_bits(void __iomem *addr, u32 bits)
207{
208 u32 v = ioread32(addr);
209
210 v &= ~bits;
211 iowrite32(v, addr);
212}
213
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000214static void davinci_spi_set_dma_req(const struct spi_device *spi, int enable)
215{
216 struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
217
218 if (enable)
219 set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
220 else
221 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
222}
223
224/*
225 * Interface to control the chip select signal
226 */
227static void davinci_spi_chipselect(struct spi_device *spi, int value)
228{
229 struct davinci_spi *davinci_spi;
230 struct davinci_spi_platform_data *pdata;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530231 u8 chip_sel = spi->chip_select;
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +0530232 u16 spidat1_cfg = CS_DEFAULT;
Brian Niebuhr23853972010-08-13 10:57:44 +0530233 bool gpio_chipsel = false;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000234
235 davinci_spi = spi_master_get_devdata(spi->master);
236 pdata = davinci_spi->pdata;
237
Brian Niebuhr23853972010-08-13 10:57:44 +0530238 if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
239 pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
240 gpio_chipsel = true;
241
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000242 /*
243 * Board specific chip select logic decides the polarity and cs
244 * line for the controller
245 */
Brian Niebuhr23853972010-08-13 10:57:44 +0530246 if (gpio_chipsel) {
247 if (value == BITBANG_CS_ACTIVE)
248 gpio_set_value(pdata->chip_sel[chip_sel], 0);
249 else
250 gpio_set_value(pdata->chip_sel[chip_sel], 1);
251 } else {
252 if (value == BITBANG_CS_ACTIVE) {
253 spidat1_cfg |= SPIDAT1_CSHOLD_MASK;
254 spidat1_cfg &= ~(0x1 << chip_sel);
255 }
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530256
Brian Niebuhr23853972010-08-13 10:57:44 +0530257 iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2);
258 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000259}
260
261/**
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530262 * davinci_spi_get_prescale - Calculates the correct prescale value
263 * @maxspeed_hz: the maximum rate the SPI clock can run at
264 *
265 * This function calculates the prescale value that generates a clock rate
266 * less than or equal to the specified maximum.
267 *
268 * Returns: calculated prescale - 1 for easy programming into SPI registers
269 * or negative error number if valid prescalar cannot be updated.
270 */
271static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi,
272 u32 max_speed_hz)
273{
274 int ret;
275
276 ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz);
277
278 if (ret < 3 || ret > 256)
279 return -EINVAL;
280
281 return ret - 1;
282}
283
284/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000285 * davinci_spi_setup_transfer - This functions will determine transfer method
286 * @spi: spi device on which data transfer to be done
287 * @t: spi transfer in which transfer info is filled
288 *
289 * This function determines data transfer method (8/16/32 bit transfer).
290 * It will also set the SPI Clock Control register according to
291 * SPI slave device freq.
292 */
293static int davinci_spi_setup_transfer(struct spi_device *spi,
294 struct spi_transfer *t)
295{
296
297 struct davinci_spi *davinci_spi;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530298 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000299 u8 bits_per_word = 0;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530300 u32 hz = 0, spifmt = 0, prescale = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000301
302 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530303 spicfg = (struct davinci_spi_config *)spi->controller_data;
304 if (!spicfg)
305 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000306
307 if (t) {
308 bits_per_word = t->bits_per_word;
309 hz = t->speed_hz;
310 }
311
312 /* if bits_per_word is not set then set it default */
313 if (!bits_per_word)
314 bits_per_word = spi->bits_per_word;
315
316 /*
317 * Assign function pointer to appropriate transfer method
318 * 8bit, 16bit or 32bit transfer
319 */
320 if (bits_per_word <= 8 && bits_per_word >= 2) {
321 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
322 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530323 davinci_spi->bytes_per_word[spi->chip_select] = 1;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000324 } else if (bits_per_word <= 16 && bits_per_word >= 2) {
325 davinci_spi->get_rx = davinci_spi_rx_buf_u16;
326 davinci_spi->get_tx = davinci_spi_tx_buf_u16;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530327 davinci_spi->bytes_per_word[spi->chip_select] = 2;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000328 } else
329 return -EINVAL;
330
331 if (!hz)
332 hz = spi->max_speed_hz;
333
Brian Niebuhr25f33512010-08-19 12:15:22 +0530334 /* Set up SPIFMTn register, unique to this chipselect. */
335
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530336 prescale = davinci_spi_get_prescale(davinci_spi, hz);
337 if (prescale < 0)
338 return prescale;
339
Brian Niebuhr25f33512010-08-19 12:15:22 +0530340 spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000341
Brian Niebuhr25f33512010-08-19 12:15:22 +0530342 if (spi->mode & SPI_LSB_FIRST)
343 spifmt |= SPIFMT_SHIFTDIR_MASK;
344
345 if (spi->mode & SPI_CPOL)
346 spifmt |= SPIFMT_POLARITY_MASK;
347
348 if (!(spi->mode & SPI_CPHA))
349 spifmt |= SPIFMT_PHASE_MASK;
350
351 /*
352 * Version 1 hardware supports two basic SPI modes:
353 * - Standard SPI mode uses 4 pins, with chipselect
354 * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
355 * (distinct from SPI_3WIRE, with just one data wire;
356 * or similar variants without MOSI or without MISO)
357 *
358 * Version 2 hardware supports an optional handshaking signal,
359 * so it can support two more modes:
360 * - 5 pin SPI variant is standard SPI plus SPI_READY
361 * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
362 */
363
364 if (davinci_spi->version == SPI_VERSION_2) {
365
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530366 u32 delay = 0;
367
Brian Niebuhr25f33512010-08-19 12:15:22 +0530368 spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
369 & SPIFMT_WDELAY_MASK);
370
371 if (spicfg->odd_parity)
372 spifmt |= SPIFMT_ODD_PARITY_MASK;
373
374 if (spicfg->parity_enable)
375 spifmt |= SPIFMT_PARITYENA_MASK;
376
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530377 if (spicfg->timer_disable) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530378 spifmt |= SPIFMT_DISTIMER_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530379 } else {
380 delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
381 & SPIDELAY_C2TDELAY_MASK;
382 delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
383 & SPIDELAY_T2CDELAY_MASK;
384 }
Brian Niebuhr25f33512010-08-19 12:15:22 +0530385
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530386 if (spi->mode & SPI_READY) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530387 spifmt |= SPIFMT_WAITENA_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530388 delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
389 & SPIDELAY_T2EDELAY_MASK;
390 delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
391 & SPIDELAY_C2EDELAY_MASK;
392 }
393
394 iowrite32(delay, davinci_spi->base + SPIDELAY);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530395 }
396
397 iowrite32(spifmt, davinci_spi->base + SPIFMT0);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000398
399 return 0;
400}
401
402static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data)
403{
404 struct spi_device *spi = (struct spi_device *)data;
405 struct davinci_spi *davinci_spi;
406 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000407
408 davinci_spi = spi_master_get_devdata(spi->master);
409 davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000410
411 if (ch_status == DMA_COMPLETE)
412 edma_stop(davinci_spi_dma->dma_rx_channel);
413 else
414 edma_clean_channel(davinci_spi_dma->dma_rx_channel);
415
416 complete(&davinci_spi_dma->dma_rx_completion);
417 /* We must disable the DMA RX request */
418 davinci_spi_set_dma_req(spi, 0);
419}
420
421static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
422{
423 struct spi_device *spi = (struct spi_device *)data;
424 struct davinci_spi *davinci_spi;
425 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000426
427 davinci_spi = spi_master_get_devdata(spi->master);
428 davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000429
430 if (ch_status == DMA_COMPLETE)
431 edma_stop(davinci_spi_dma->dma_tx_channel);
432 else
433 edma_clean_channel(davinci_spi_dma->dma_tx_channel);
434
435 complete(&davinci_spi_dma->dma_tx_completion);
436 /* We must disable the DMA TX request */
437 davinci_spi_set_dma_req(spi, 0);
438}
439
440static int davinci_spi_request_dma(struct spi_device *spi)
441{
442 struct davinci_spi *davinci_spi;
443 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000444 struct device *sdev;
445 int r;
446
447 davinci_spi = spi_master_get_devdata(spi->master);
448 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000449 sdev = davinci_spi->bitbang.master->dev.parent;
450
451 r = edma_alloc_channel(davinci_spi_dma->dma_rx_sync_dev,
452 davinci_spi_dma_rx_callback, spi,
453 davinci_spi_dma->eventq);
454 if (r < 0) {
455 dev_dbg(sdev, "Unable to request DMA channel for SPI RX\n");
456 return -EAGAIN;
457 }
458 davinci_spi_dma->dma_rx_channel = r;
459 r = edma_alloc_channel(davinci_spi_dma->dma_tx_sync_dev,
460 davinci_spi_dma_tx_callback, spi,
461 davinci_spi_dma->eventq);
462 if (r < 0) {
463 edma_free_channel(davinci_spi_dma->dma_rx_channel);
464 davinci_spi_dma->dma_rx_channel = -1;
465 dev_dbg(sdev, "Unable to request DMA channel for SPI TX\n");
466 return -EAGAIN;
467 }
468 davinci_spi_dma->dma_tx_channel = r;
469
470 return 0;
471}
472
473/**
474 * davinci_spi_setup - This functions will set default transfer method
475 * @spi: spi device on which data transfer to be done
476 *
477 * This functions sets the default transfer method.
478 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000479static int davinci_spi_setup(struct spi_device *spi)
480{
481 int retval;
482 struct davinci_spi *davinci_spi;
483 struct davinci_spi_dma *davinci_spi_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000484
485 davinci_spi = spi_master_get_devdata(spi->master);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000486
487 /* if bits per word length is zero then set it default 8 */
488 if (!spi->bits_per_word)
489 spi->bits_per_word = 8;
490
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000491 if (use_dma && davinci_spi->dma_channels) {
492 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
493
494 if ((davinci_spi_dma->dma_rx_channel == -1)
495 || (davinci_spi_dma->dma_tx_channel == -1)) {
496 retval = davinci_spi_request_dma(spi);
497 if (retval < 0)
498 return retval;
499 }
500 }
501
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000502 retval = davinci_spi_setup_transfer(spi, NULL);
503
504 return retval;
505}
506
507static void davinci_spi_cleanup(struct spi_device *spi)
508{
509 struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
510 struct davinci_spi_dma *davinci_spi_dma;
511
512 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
513
514 if (use_dma && davinci_spi->dma_channels) {
515 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
516
517 if ((davinci_spi_dma->dma_rx_channel != -1)
518 && (davinci_spi_dma->dma_tx_channel != -1)) {
519 edma_free_channel(davinci_spi_dma->dma_tx_channel);
520 edma_free_channel(davinci_spi_dma->dma_rx_channel);
521 }
522 }
523}
524
525static int davinci_spi_bufs_prep(struct spi_device *spi,
526 struct davinci_spi *davinci_spi)
527{
Brian Niebuhr23853972010-08-13 10:57:44 +0530528 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000529 int op_mode = 0;
530
531 /*
532 * REVISIT unless devices disagree about SPI_LOOP or
533 * SPI_READY (SPI_NO_CS only allows one device!), this
534 * should not need to be done before each message...
535 * optimize for both flags staying cleared.
536 */
537
538 op_mode = SPIPC0_DIFUN_MASK
539 | SPIPC0_DOFUN_MASK
540 | SPIPC0_CLKFUN_MASK;
Brian Niebuhr23853972010-08-13 10:57:44 +0530541 if (!(spi->mode & SPI_NO_CS)) {
542 pdata = davinci_spi->pdata;
543 if (!pdata->chip_sel ||
544 pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS)
545 op_mode |= 1 << spi->chip_select;
546 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000547 if (spi->mode & SPI_READY)
548 op_mode |= SPIPC0_SPIENA_MASK;
549
550 iowrite32(op_mode, davinci_spi->base + SPIPC0);
551
552 if (spi->mode & SPI_LOOP)
553 set_io_bits(davinci_spi->base + SPIGCR1,
554 SPIGCR1_LOOPBACK_MASK);
555 else
556 clear_io_bits(davinci_spi->base + SPIGCR1,
557 SPIGCR1_LOOPBACK_MASK);
558
559 return 0;
560}
561
562static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
563 int int_status)
564{
565 struct device *sdev = davinci_spi->bitbang.master->dev.parent;
566
567 if (int_status & SPIFLG_TIMEOUT_MASK) {
568 dev_dbg(sdev, "SPI Time-out Error\n");
569 return -ETIMEDOUT;
570 }
571 if (int_status & SPIFLG_DESYNC_MASK) {
572 dev_dbg(sdev, "SPI Desynchronization Error\n");
573 return -EIO;
574 }
575 if (int_status & SPIFLG_BITERR_MASK) {
576 dev_dbg(sdev, "SPI Bit error\n");
577 return -EIO;
578 }
579
580 if (davinci_spi->version == SPI_VERSION_2) {
581 if (int_status & SPIFLG_DLEN_ERR_MASK) {
582 dev_dbg(sdev, "SPI Data Length Error\n");
583 return -EIO;
584 }
585 if (int_status & SPIFLG_PARERR_MASK) {
586 dev_dbg(sdev, "SPI Parity Error\n");
587 return -EIO;
588 }
589 if (int_status & SPIFLG_OVRRUN_MASK) {
590 dev_dbg(sdev, "SPI Data Overrun error\n");
591 return -EIO;
592 }
593 if (int_status & SPIFLG_TX_INTR_MASK) {
594 dev_dbg(sdev, "SPI TX intr bit set\n");
595 return -EIO;
596 }
597 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
598 dev_dbg(sdev, "SPI Buffer Init Active\n");
599 return -EBUSY;
600 }
601 }
602
603 return 0;
604}
605
606/**
607 * davinci_spi_bufs - functions which will handle transfer data
608 * @spi: spi device on which data transfer to be done
609 * @t: spi transfer in which transfer info is filled
610 *
611 * This function will put data to be transferred into data register
612 * of SPI controller and then wait until the completion will be marked
613 * by the IRQ Handler.
614 */
615static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
616{
617 struct davinci_spi *davinci_spi;
618 int int_status, count, ret;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530619 u8 conv;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000620 u32 tx_data, data1_reg_val;
621 u32 buf_val, flg_val;
622 struct davinci_spi_platform_data *pdata;
623
624 davinci_spi = spi_master_get_devdata(spi->master);
625 pdata = davinci_spi->pdata;
626
627 davinci_spi->tx = t->tx_buf;
628 davinci_spi->rx = t->rx_buf;
629
630 /* convert len to words based on bits_per_word */
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530631 conv = davinci_spi->bytes_per_word[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000632 davinci_spi->count = t->len / conv;
633
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530634 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
635
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000636 INIT_COMPLETION(davinci_spi->done);
637
638 ret = davinci_spi_bufs_prep(spi, davinci_spi);
639 if (ret)
640 return ret;
641
642 /* Enable SPI */
643 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
644
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000645 count = davinci_spi->count;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000646
647 /* Determine the command to execute READ or WRITE */
648 if (t->tx_buf) {
649 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
650
651 while (1) {
652 tx_data = davinci_spi->get_tx(davinci_spi);
653
654 data1_reg_val &= ~(0xFFFF);
655 data1_reg_val |= (0xFFFF & tx_data);
656
657 buf_val = ioread32(davinci_spi->base + SPIBUF);
658 if ((buf_val & SPIBUF_TXFULL_MASK) == 0) {
659 iowrite32(data1_reg_val,
660 davinci_spi->base + SPIDAT1);
661
662 count--;
663 }
664 while (ioread32(davinci_spi->base + SPIBUF)
665 & SPIBUF_RXEMPTY_MASK)
666 cpu_relax();
667
668 /* getting the returned byte */
669 if (t->rx_buf) {
670 buf_val = ioread32(davinci_spi->base + SPIBUF);
671 davinci_spi->get_rx(buf_val, davinci_spi);
672 }
673 if (count <= 0)
674 break;
675 }
676 } else {
677 if (pdata->poll_mode) {
678 while (1) {
679 /* keeps the serial clock going */
680 if ((ioread32(davinci_spi->base + SPIBUF)
681 & SPIBUF_TXFULL_MASK) == 0)
682 iowrite32(data1_reg_val,
683 davinci_spi->base + SPIDAT1);
684
685 while (ioread32(davinci_spi->base + SPIBUF) &
686 SPIBUF_RXEMPTY_MASK)
687 cpu_relax();
688
689 flg_val = ioread32(davinci_spi->base + SPIFLG);
690 buf_val = ioread32(davinci_spi->base + SPIBUF);
691
692 davinci_spi->get_rx(buf_val, davinci_spi);
693
694 count--;
695 if (count <= 0)
696 break;
697 }
698 } else { /* Receive in Interrupt mode */
699 int i;
700
701 for (i = 0; i < davinci_spi->count; i++) {
702 set_io_bits(davinci_spi->base + SPIINT,
703 SPIINT_BITERR_INTR
704 | SPIINT_OVRRUN_INTR
705 | SPIINT_RX_INTR);
706
707 iowrite32(data1_reg_val,
708 davinci_spi->base + SPIDAT1);
709
710 while (ioread32(davinci_spi->base + SPIINT) &
711 SPIINT_RX_INTR)
712 cpu_relax();
713 }
714 iowrite32((data1_reg_val & 0x0ffcffff),
715 davinci_spi->base + SPIDAT1);
716 }
717 }
718
719 /*
720 * Check for bit error, desync error,parity error,timeout error and
721 * receive overflow errors
722 */
723 int_status = ioread32(davinci_spi->base + SPIFLG);
724
725 ret = davinci_spi_check_error(davinci_spi, int_status);
726 if (ret != 0)
727 return ret;
728
729 /* SPI Framework maintains the count only in bytes so convert back */
730 davinci_spi->count *= conv;
731
732 return t->len;
733}
734
735#define DAVINCI_DMA_DATA_TYPE_S8 0x01
736#define DAVINCI_DMA_DATA_TYPE_S16 0x02
737#define DAVINCI_DMA_DATA_TYPE_S32 0x04
738
739static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
740{
741 struct davinci_spi *davinci_spi;
742 int int_status = 0;
743 int count, temp_count;
744 u8 conv = 1;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000745 u32 data1_reg_val;
746 struct davinci_spi_dma *davinci_spi_dma;
747 int word_len, data_type, ret;
748 unsigned long tx_reg, rx_reg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000749 struct device *sdev;
750
751 davinci_spi = spi_master_get_devdata(spi->master);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000752 sdev = davinci_spi->bitbang.master->dev.parent;
753
754 davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
755
756 tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
757 rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
758
759 davinci_spi->tx = t->tx_buf;
760 davinci_spi->rx = t->rx_buf;
761
762 /* convert len to words based on bits_per_word */
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530763 conv = davinci_spi->bytes_per_word[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000764 davinci_spi->count = t->len / conv;
765
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530766 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
767
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000768 INIT_COMPLETION(davinci_spi->done);
769
770 init_completion(&davinci_spi_dma->dma_rx_completion);
771 init_completion(&davinci_spi_dma->dma_tx_completion);
772
773 word_len = conv * 8;
774
775 if (word_len <= 8)
776 data_type = DAVINCI_DMA_DATA_TYPE_S8;
777 else if (word_len <= 16)
778 data_type = DAVINCI_DMA_DATA_TYPE_S16;
779 else if (word_len <= 32)
780 data_type = DAVINCI_DMA_DATA_TYPE_S32;
781 else
782 return -EINVAL;
783
784 ret = davinci_spi_bufs_prep(spi, davinci_spi);
785 if (ret)
786 return ret;
787
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000788 count = davinci_spi->count; /* the number of elements */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000789
790 /* disable all interrupts for dma transfers */
791 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
792 /* Disable SPI to write configuration bits in SPIDAT */
793 clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000794 /* Enable SPI */
795 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
796
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000797 if (t->tx_buf) {
798 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
799 DMA_TO_DEVICE);
800 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
801 dev_dbg(sdev, "Unable to DMA map a %d bytes"
802 " TX buffer\n", count);
803 return -ENOMEM;
804 }
805 temp_count = count;
806 } else {
807 /* We need TX clocking for RX transaction */
808 t->tx_dma = dma_map_single(&spi->dev,
809 (void *)davinci_spi->tmp_buf, count + 1,
810 DMA_TO_DEVICE);
811 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
812 dev_dbg(sdev, "Unable to DMA map a %d bytes"
813 " TX tmp buffer\n", count);
814 return -ENOMEM;
815 }
816 temp_count = count + 1;
817 }
818
819 edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
820 data_type, temp_count, 1, 0, ASYNC);
821 edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
822 edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
823 edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
824 edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
825
826 if (t->rx_buf) {
827 /* initiate transaction */
828 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
829
830 t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
831 DMA_FROM_DEVICE);
832 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
833 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
834 count);
835 if (t->tx_buf != NULL)
836 dma_unmap_single(NULL, t->tx_dma,
837 count, DMA_TO_DEVICE);
838 return -ENOMEM;
839 }
840 edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
841 data_type, count, 1, 0, ASYNC);
842 edma_set_src(davinci_spi_dma->dma_rx_channel,
843 rx_reg, INCR, W8BIT);
844 edma_set_dest(davinci_spi_dma->dma_rx_channel,
845 t->rx_dma, INCR, W8BIT);
846 edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
847 edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
848 data_type, 0);
849 }
850
851 if ((t->tx_buf) || (t->rx_buf))
852 edma_start(davinci_spi_dma->dma_tx_channel);
853
854 if (t->rx_buf)
855 edma_start(davinci_spi_dma->dma_rx_channel);
856
857 if ((t->rx_buf) || (t->tx_buf))
858 davinci_spi_set_dma_req(spi, 1);
859
860 if (t->tx_buf)
861 wait_for_completion_interruptible(
862 &davinci_spi_dma->dma_tx_completion);
863
864 if (t->rx_buf)
865 wait_for_completion_interruptible(
866 &davinci_spi_dma->dma_rx_completion);
867
868 dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
869
870 if (t->rx_buf)
871 dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
872
873 /*
874 * Check for bit error, desync error,parity error,timeout error and
875 * receive overflow errors
876 */
877 int_status = ioread32(davinci_spi->base + SPIFLG);
878
879 ret = davinci_spi_check_error(davinci_spi, int_status);
880 if (ret != 0)
881 return ret;
882
883 /* SPI Framework maintains the count only in bytes so convert back */
884 davinci_spi->count *= conv;
885
886 return t->len;
887}
888
889/**
890 * davinci_spi_irq - IRQ handler for DaVinci SPI
891 * @irq: IRQ number for this SPI Master
892 * @context_data: structure for SPI Master controller davinci_spi
893 */
894static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
895{
896 struct davinci_spi *davinci_spi = context_data;
897 u32 int_status, rx_data = 0;
898 irqreturn_t ret = IRQ_NONE;
899
900 int_status = ioread32(davinci_spi->base + SPIFLG);
901
902 while ((int_status & SPIFLG_RX_INTR_MASK)) {
903 if (likely(int_status & SPIFLG_RX_INTR_MASK)) {
904 ret = IRQ_HANDLED;
905
906 rx_data = ioread32(davinci_spi->base + SPIBUF);
907 davinci_spi->get_rx(rx_data, davinci_spi);
908
909 /* Disable Receive Interrupt */
910 iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR),
911 davinci_spi->base + SPIINT);
912 } else
913 (void)davinci_spi_check_error(davinci_spi, int_status);
914
915 int_status = ioread32(davinci_spi->base + SPIFLG);
916 }
917
918 return ret;
919}
920
921/**
922 * davinci_spi_probe - probe function for SPI Master Controller
923 * @pdev: platform_device structure which contains plateform specific data
924 */
925static int davinci_spi_probe(struct platform_device *pdev)
926{
927 struct spi_master *master;
928 struct davinci_spi *davinci_spi;
929 struct davinci_spi_platform_data *pdata;
930 struct resource *r, *mem;
931 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
932 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
933 resource_size_t dma_eventq = SPI_NO_RESOURCE;
934 int i = 0, ret = 0;
935
936 pdata = pdev->dev.platform_data;
937 if (pdata == NULL) {
938 ret = -ENODEV;
939 goto err;
940 }
941
942 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
943 if (master == NULL) {
944 ret = -ENOMEM;
945 goto err;
946 }
947
948 dev_set_drvdata(&pdev->dev, master);
949
950 davinci_spi = spi_master_get_devdata(master);
951 if (davinci_spi == NULL) {
952 ret = -ENOENT;
953 goto free_master;
954 }
955
956 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
957 if (r == NULL) {
958 ret = -ENOENT;
959 goto free_master;
960 }
961
962 davinci_spi->pbase = r->start;
963 davinci_spi->region_size = resource_size(r);
964 davinci_spi->pdata = pdata;
965
966 mem = request_mem_region(r->start, davinci_spi->region_size,
967 pdev->name);
968 if (mem == NULL) {
969 ret = -EBUSY;
970 goto free_master;
971 }
972
Sekhar Nori50356dd2010-10-08 15:27:26 +0530973 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000974 if (davinci_spi->base == NULL) {
975 ret = -ENOMEM;
976 goto release_region;
977 }
978
979 davinci_spi->irq = platform_get_irq(pdev, 0);
980 if (davinci_spi->irq <= 0) {
981 ret = -EINVAL;
982 goto unmap_io;
983 }
984
985 ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED,
986 dev_name(&pdev->dev), davinci_spi);
987 if (ret)
988 goto unmap_io;
989
990 /* Allocate tmp_buf for tx_buf */
991 davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
992 if (davinci_spi->tmp_buf == NULL) {
993 ret = -ENOMEM;
994 goto irq_free;
995 }
996
997 davinci_spi->bitbang.master = spi_master_get(master);
998 if (davinci_spi->bitbang.master == NULL) {
999 ret = -ENODEV;
1000 goto free_tmp_buf;
1001 }
1002
1003 davinci_spi->clk = clk_get(&pdev->dev, NULL);
1004 if (IS_ERR(davinci_spi->clk)) {
1005 ret = -ENODEV;
1006 goto put_master;
1007 }
1008 clk_enable(davinci_spi->clk);
1009
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001010 master->bus_num = pdev->id;
1011 master->num_chipselect = pdata->num_chipselect;
1012 master->setup = davinci_spi_setup;
1013 master->cleanup = davinci_spi_cleanup;
1014
1015 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
1016 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
1017
1018 davinci_spi->version = pdata->version;
1019 use_dma = pdata->use_dma;
1020
1021 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
1022 if (davinci_spi->version == SPI_VERSION_2)
1023 davinci_spi->bitbang.flags |= SPI_READY;
1024
1025 if (use_dma) {
Brian Niebuhr778e2612010-09-03 15:15:06 +05301026 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1027 if (r)
1028 dma_rx_chan = r->start;
1029 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1030 if (r)
1031 dma_tx_chan = r->start;
1032 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1033 if (r)
1034 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001035 }
1036
1037 if (!use_dma ||
1038 dma_rx_chan == SPI_NO_RESOURCE ||
1039 dma_tx_chan == SPI_NO_RESOURCE ||
1040 dma_eventq == SPI_NO_RESOURCE) {
1041 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
1042 use_dma = 0;
1043 } else {
1044 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
1045 davinci_spi->dma_channels = kzalloc(master->num_chipselect
1046 * sizeof(struct davinci_spi_dma), GFP_KERNEL);
1047 if (davinci_spi->dma_channels == NULL) {
1048 ret = -ENOMEM;
1049 goto free_clk;
1050 }
1051
1052 for (i = 0; i < master->num_chipselect; i++) {
1053 davinci_spi->dma_channels[i].dma_rx_channel = -1;
1054 davinci_spi->dma_channels[i].dma_rx_sync_dev =
1055 dma_rx_chan;
1056 davinci_spi->dma_channels[i].dma_tx_channel = -1;
1057 davinci_spi->dma_channels[i].dma_tx_sync_dev =
1058 dma_tx_chan;
1059 davinci_spi->dma_channels[i].eventq = dma_eventq;
1060 }
1061 dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
1062 "Using RX channel = %d , TX channel = %d and "
1063 "event queue = %d", dma_rx_chan, dma_tx_chan,
1064 dma_eventq);
1065 }
1066
1067 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
1068 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
1069
1070 init_completion(&davinci_spi->done);
1071
1072 /* Reset In/OUT SPI module */
1073 iowrite32(0, davinci_spi->base + SPIGCR0);
1074 udelay(100);
1075 iowrite32(1, davinci_spi->base + SPIGCR0);
1076
Brian Niebuhr23853972010-08-13 10:57:44 +05301077 /* initialize chip selects */
1078 if (pdata->chip_sel) {
1079 for (i = 0; i < pdata->num_chipselect; i++) {
1080 if (pdata->chip_sel[i] != SPI_INTERN_CS)
1081 gpio_direction_output(pdata->chip_sel[i], 1);
1082 }
1083 }
1084
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001085 /* Clock internal */
1086 if (davinci_spi->pdata->clk_internal)
1087 set_io_bits(davinci_spi->base + SPIGCR1,
1088 SPIGCR1_CLKMOD_MASK);
1089 else
1090 clear_io_bits(davinci_spi->base + SPIGCR1,
1091 SPIGCR1_CLKMOD_MASK);
1092
Brian Niebuhr843a7132010-08-12 12:49:05 +05301093 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
1094
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001095 /* master mode default */
1096 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
1097
1098 if (davinci_spi->pdata->intr_level)
1099 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
1100 else
1101 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
1102
1103 ret = spi_bitbang_start(&davinci_spi->bitbang);
1104 if (ret)
1105 goto free_clk;
1106
Brian Niebuhr3b740b12010-09-03 14:50:07 +05301107 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001108
1109 if (!pdata->poll_mode)
1110 dev_info(&pdev->dev, "Operating in interrupt mode"
1111 " using IRQ %d\n", davinci_spi->irq);
1112
1113 return ret;
1114
1115free_clk:
1116 clk_disable(davinci_spi->clk);
1117 clk_put(davinci_spi->clk);
1118put_master:
1119 spi_master_put(master);
1120free_tmp_buf:
1121 kfree(davinci_spi->tmp_buf);
1122irq_free:
1123 free_irq(davinci_spi->irq, davinci_spi);
1124unmap_io:
1125 iounmap(davinci_spi->base);
1126release_region:
1127 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1128free_master:
1129 kfree(master);
1130err:
1131 return ret;
1132}
1133
1134/**
1135 * davinci_spi_remove - remove function for SPI Master Controller
1136 * @pdev: platform_device structure which contains plateform specific data
1137 *
1138 * This function will do the reverse action of davinci_spi_probe function
1139 * It will free the IRQ and SPI controller's memory region.
1140 * It will also call spi_bitbang_stop to destroy the work queue which was
1141 * created by spi_bitbang_start.
1142 */
1143static int __exit davinci_spi_remove(struct platform_device *pdev)
1144{
1145 struct davinci_spi *davinci_spi;
1146 struct spi_master *master;
1147
1148 master = dev_get_drvdata(&pdev->dev);
1149 davinci_spi = spi_master_get_devdata(master);
1150
1151 spi_bitbang_stop(&davinci_spi->bitbang);
1152
1153 clk_disable(davinci_spi->clk);
1154 clk_put(davinci_spi->clk);
1155 spi_master_put(master);
1156 kfree(davinci_spi->tmp_buf);
1157 free_irq(davinci_spi->irq, davinci_spi);
1158 iounmap(davinci_spi->base);
1159 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1160
1161 return 0;
1162}
1163
1164static struct platform_driver davinci_spi_driver = {
1165 .driver.name = "spi_davinci",
1166 .remove = __exit_p(davinci_spi_remove),
1167};
1168
1169static int __init davinci_spi_init(void)
1170{
1171 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1172}
1173module_init(davinci_spi_init);
1174
1175static void __exit davinci_spi_exit(void)
1176{
1177 platform_driver_unregister(&davinci_spi_driver);
1178}
1179module_exit(davinci_spi_exit);
1180
1181MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1182MODULE_LICENSE("GPL");