blob: 1169e8e7462e3d10a0e5da8df9c13c55f37c0185 [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);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000774 /* Enable SPI */
775 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
776
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000777 if (t->tx_buf) {
778 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
779 DMA_TO_DEVICE);
780 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
781 dev_dbg(sdev, "Unable to DMA map a %d bytes"
782 " TX buffer\n", count);
783 return -ENOMEM;
784 }
785 temp_count = count;
786 } else {
787 /* We need TX clocking for RX transaction */
788 t->tx_dma = dma_map_single(&spi->dev,
789 (void *)davinci_spi->tmp_buf, count + 1,
790 DMA_TO_DEVICE);
791 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
792 dev_dbg(sdev, "Unable to DMA map a %d bytes"
793 " TX tmp buffer\n", count);
794 return -ENOMEM;
795 }
796 temp_count = count + 1;
797 }
798
799 edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
800 data_type, temp_count, 1, 0, ASYNC);
801 edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
802 edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
803 edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
804 edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
805
806 if (t->rx_buf) {
807 /* initiate transaction */
808 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
809
810 t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
811 DMA_FROM_DEVICE);
812 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
813 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
814 count);
815 if (t->tx_buf != NULL)
816 dma_unmap_single(NULL, t->tx_dma,
817 count, DMA_TO_DEVICE);
818 return -ENOMEM;
819 }
820 edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
821 data_type, count, 1, 0, ASYNC);
822 edma_set_src(davinci_spi_dma->dma_rx_channel,
823 rx_reg, INCR, W8BIT);
824 edma_set_dest(davinci_spi_dma->dma_rx_channel,
825 t->rx_dma, INCR, W8BIT);
826 edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
827 edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
828 data_type, 0);
829 }
830
831 if ((t->tx_buf) || (t->rx_buf))
832 edma_start(davinci_spi_dma->dma_tx_channel);
833
834 if (t->rx_buf)
835 edma_start(davinci_spi_dma->dma_rx_channel);
836
837 if ((t->rx_buf) || (t->tx_buf))
838 davinci_spi_set_dma_req(spi, 1);
839
840 if (t->tx_buf)
841 wait_for_completion_interruptible(
842 &davinci_spi_dma->dma_tx_completion);
843
844 if (t->rx_buf)
845 wait_for_completion_interruptible(
846 &davinci_spi_dma->dma_rx_completion);
847
848 dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
849
850 if (t->rx_buf)
851 dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
852
853 /*
854 * Check for bit error, desync error,parity error,timeout error and
855 * receive overflow errors
856 */
857 int_status = ioread32(davinci_spi->base + SPIFLG);
858
859 ret = davinci_spi_check_error(davinci_spi, int_status);
860 if (ret != 0)
861 return ret;
862
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000863 return t->len;
864}
865
866/**
867 * davinci_spi_irq - IRQ handler for DaVinci SPI
868 * @irq: IRQ number for this SPI Master
869 * @context_data: structure for SPI Master controller davinci_spi
870 */
871static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
872{
873 struct davinci_spi *davinci_spi = context_data;
874 u32 int_status, rx_data = 0;
875 irqreturn_t ret = IRQ_NONE;
876
877 int_status = ioread32(davinci_spi->base + SPIFLG);
878
879 while ((int_status & SPIFLG_RX_INTR_MASK)) {
880 if (likely(int_status & SPIFLG_RX_INTR_MASK)) {
881 ret = IRQ_HANDLED;
882
883 rx_data = ioread32(davinci_spi->base + SPIBUF);
884 davinci_spi->get_rx(rx_data, davinci_spi);
885
886 /* Disable Receive Interrupt */
887 iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR),
888 davinci_spi->base + SPIINT);
889 } else
890 (void)davinci_spi_check_error(davinci_spi, int_status);
891
892 int_status = ioread32(davinci_spi->base + SPIFLG);
893 }
894
895 return ret;
896}
897
898/**
899 * davinci_spi_probe - probe function for SPI Master Controller
900 * @pdev: platform_device structure which contains plateform specific data
901 */
902static int davinci_spi_probe(struct platform_device *pdev)
903{
904 struct spi_master *master;
905 struct davinci_spi *davinci_spi;
906 struct davinci_spi_platform_data *pdata;
907 struct resource *r, *mem;
908 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
909 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
910 resource_size_t dma_eventq = SPI_NO_RESOURCE;
911 int i = 0, ret = 0;
912
913 pdata = pdev->dev.platform_data;
914 if (pdata == NULL) {
915 ret = -ENODEV;
916 goto err;
917 }
918
919 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
920 if (master == NULL) {
921 ret = -ENOMEM;
922 goto err;
923 }
924
925 dev_set_drvdata(&pdev->dev, master);
926
927 davinci_spi = spi_master_get_devdata(master);
928 if (davinci_spi == NULL) {
929 ret = -ENOENT;
930 goto free_master;
931 }
932
933 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
934 if (r == NULL) {
935 ret = -ENOENT;
936 goto free_master;
937 }
938
939 davinci_spi->pbase = r->start;
940 davinci_spi->region_size = resource_size(r);
941 davinci_spi->pdata = pdata;
942
943 mem = request_mem_region(r->start, davinci_spi->region_size,
944 pdev->name);
945 if (mem == NULL) {
946 ret = -EBUSY;
947 goto free_master;
948 }
949
Sekhar Nori50356dd2010-10-08 15:27:26 +0530950 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000951 if (davinci_spi->base == NULL) {
952 ret = -ENOMEM;
953 goto release_region;
954 }
955
956 davinci_spi->irq = platform_get_irq(pdev, 0);
957 if (davinci_spi->irq <= 0) {
958 ret = -EINVAL;
959 goto unmap_io;
960 }
961
962 ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED,
963 dev_name(&pdev->dev), davinci_spi);
964 if (ret)
965 goto unmap_io;
966
967 /* Allocate tmp_buf for tx_buf */
968 davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
969 if (davinci_spi->tmp_buf == NULL) {
970 ret = -ENOMEM;
971 goto irq_free;
972 }
973
974 davinci_spi->bitbang.master = spi_master_get(master);
975 if (davinci_spi->bitbang.master == NULL) {
976 ret = -ENODEV;
977 goto free_tmp_buf;
978 }
979
980 davinci_spi->clk = clk_get(&pdev->dev, NULL);
981 if (IS_ERR(davinci_spi->clk)) {
982 ret = -ENODEV;
983 goto put_master;
984 }
985 clk_enable(davinci_spi->clk);
986
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000987 master->bus_num = pdev->id;
988 master->num_chipselect = pdata->num_chipselect;
989 master->setup = davinci_spi_setup;
990 master->cleanup = davinci_spi_cleanup;
991
992 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
993 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
994
995 davinci_spi->version = pdata->version;
996 use_dma = pdata->use_dma;
997
998 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
999 if (davinci_spi->version == SPI_VERSION_2)
1000 davinci_spi->bitbang.flags |= SPI_READY;
1001
1002 if (use_dma) {
Brian Niebuhr778e2612010-09-03 15:15:06 +05301003 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1004 if (r)
1005 dma_rx_chan = r->start;
1006 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1007 if (r)
1008 dma_tx_chan = r->start;
1009 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1010 if (r)
1011 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001012 }
1013
1014 if (!use_dma ||
1015 dma_rx_chan == SPI_NO_RESOURCE ||
1016 dma_tx_chan == SPI_NO_RESOURCE ||
1017 dma_eventq == SPI_NO_RESOURCE) {
1018 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
1019 use_dma = 0;
1020 } else {
1021 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
1022 davinci_spi->dma_channels = kzalloc(master->num_chipselect
1023 * sizeof(struct davinci_spi_dma), GFP_KERNEL);
1024 if (davinci_spi->dma_channels == NULL) {
1025 ret = -ENOMEM;
1026 goto free_clk;
1027 }
1028
1029 for (i = 0; i < master->num_chipselect; i++) {
1030 davinci_spi->dma_channels[i].dma_rx_channel = -1;
1031 davinci_spi->dma_channels[i].dma_rx_sync_dev =
1032 dma_rx_chan;
1033 davinci_spi->dma_channels[i].dma_tx_channel = -1;
1034 davinci_spi->dma_channels[i].dma_tx_sync_dev =
1035 dma_tx_chan;
1036 davinci_spi->dma_channels[i].eventq = dma_eventq;
1037 }
1038 dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
1039 "Using RX channel = %d , TX channel = %d and "
1040 "event queue = %d", dma_rx_chan, dma_tx_chan,
1041 dma_eventq);
1042 }
1043
1044 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
1045 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
1046
1047 init_completion(&davinci_spi->done);
1048
1049 /* Reset In/OUT SPI module */
1050 iowrite32(0, davinci_spi->base + SPIGCR0);
1051 udelay(100);
1052 iowrite32(1, davinci_spi->base + SPIGCR0);
1053
Brian Niebuhr23853972010-08-13 10:57:44 +05301054 /* initialize chip selects */
1055 if (pdata->chip_sel) {
1056 for (i = 0; i < pdata->num_chipselect; i++) {
1057 if (pdata->chip_sel[i] != SPI_INTERN_CS)
1058 gpio_direction_output(pdata->chip_sel[i], 1);
1059 }
1060 }
1061
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001062 /* Clock internal */
1063 if (davinci_spi->pdata->clk_internal)
1064 set_io_bits(davinci_spi->base + SPIGCR1,
1065 SPIGCR1_CLKMOD_MASK);
1066 else
1067 clear_io_bits(davinci_spi->base + SPIGCR1,
1068 SPIGCR1_CLKMOD_MASK);
1069
Brian Niebuhr843a7132010-08-12 12:49:05 +05301070 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
1071
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001072 /* master mode default */
1073 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
1074
1075 if (davinci_spi->pdata->intr_level)
1076 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
1077 else
1078 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
1079
1080 ret = spi_bitbang_start(&davinci_spi->bitbang);
1081 if (ret)
1082 goto free_clk;
1083
Brian Niebuhr3b740b12010-09-03 14:50:07 +05301084 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001085
1086 if (!pdata->poll_mode)
1087 dev_info(&pdev->dev, "Operating in interrupt mode"
1088 " using IRQ %d\n", davinci_spi->irq);
1089
1090 return ret;
1091
1092free_clk:
1093 clk_disable(davinci_spi->clk);
1094 clk_put(davinci_spi->clk);
1095put_master:
1096 spi_master_put(master);
1097free_tmp_buf:
1098 kfree(davinci_spi->tmp_buf);
1099irq_free:
1100 free_irq(davinci_spi->irq, davinci_spi);
1101unmap_io:
1102 iounmap(davinci_spi->base);
1103release_region:
1104 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1105free_master:
1106 kfree(master);
1107err:
1108 return ret;
1109}
1110
1111/**
1112 * davinci_spi_remove - remove function for SPI Master Controller
1113 * @pdev: platform_device structure which contains plateform specific data
1114 *
1115 * This function will do the reverse action of davinci_spi_probe function
1116 * It will free the IRQ and SPI controller's memory region.
1117 * It will also call spi_bitbang_stop to destroy the work queue which was
1118 * created by spi_bitbang_start.
1119 */
1120static int __exit davinci_spi_remove(struct platform_device *pdev)
1121{
1122 struct davinci_spi *davinci_spi;
1123 struct spi_master *master;
1124
1125 master = dev_get_drvdata(&pdev->dev);
1126 davinci_spi = spi_master_get_devdata(master);
1127
1128 spi_bitbang_stop(&davinci_spi->bitbang);
1129
1130 clk_disable(davinci_spi->clk);
1131 clk_put(davinci_spi->clk);
1132 spi_master_put(master);
1133 kfree(davinci_spi->tmp_buf);
1134 free_irq(davinci_spi->irq, davinci_spi);
1135 iounmap(davinci_spi->base);
1136 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1137
1138 return 0;
1139}
1140
1141static struct platform_driver davinci_spi_driver = {
1142 .driver.name = "spi_davinci",
1143 .remove = __exit_p(davinci_spi_remove),
1144};
1145
1146static int __init davinci_spi_init(void)
1147{
1148 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1149}
1150module_init(davinci_spi_init);
1151
1152static void __exit davinci_spi_exit(void)
1153{
1154 platform_driver_unregister(&davinci_spi_driver);
1155}
1156module_exit(davinci_spi_exit);
1157
1158MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1159MODULE_LICENSE("GPL");