blob: 5fe298099a1a2c34574bc7595d9363e516966f1e [file] [log] [blame]
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001/*
2 * Copyright (C) 2009 Texas Instruments.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/gpio.h>
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/platform_device.h>
25#include <linux/err.h>
26#include <linux/clk.h>
27#include <linux/dma-mapping.h>
28#include <linux/spi/spi.h>
29#include <linux/spi/spi_bitbang.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Sandeep Paulraj358934a2009-12-16 22:02:18 +000031
32#include <mach/spi.h>
33#include <mach/edma.h>
34
35#define SPI_NO_RESOURCE ((resource_size_t)-1)
36
37#define SPI_MAX_CHIPSELECT 2
38
39#define CS_DEFAULT 0xFF
40
Sandeep Paulraj358934a2009-12-16 22:02:18 +000041#define SPIFMT_PHASE_MASK BIT(16)
42#define SPIFMT_POLARITY_MASK BIT(17)
43#define SPIFMT_DISTIMER_MASK BIT(18)
44#define SPIFMT_SHIFTDIR_MASK BIT(20)
45#define SPIFMT_WAITENA_MASK BIT(21)
46#define SPIFMT_PARITYENA_MASK BIT(22)
47#define SPIFMT_ODD_PARITY_MASK BIT(23)
48#define SPIFMT_WDELAY_MASK 0x3f000000u
49#define SPIFMT_WDELAY_SHIFT 24
Brian Niebuhr7fe00922010-08-13 13:27:23 +053050#define SPIFMT_PRESCALE_SHIFT 8
Sandeep Paulraj358934a2009-12-16 22:02:18 +000051
Sandeep Paulraj358934a2009-12-16 22:02:18 +000052
53/* SPIPC0 */
54#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
55#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
56#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
57#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000058
59#define SPIINT_MASKALL 0x0101035F
Brian Niebuhre0d205e2010-09-02 16:52:06 +053060#define SPIINT_MASKINT 0x0000015F
61#define SPI_INTLVL_1 0x000001FF
62#define SPI_INTLVL_0 0x00000000
Sandeep Paulraj358934a2009-12-16 22:02:18 +000063
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +053064/* SPIDAT1 (upper 16 bit defines) */
65#define SPIDAT1_CSHOLD_MASK BIT(12)
66
67/* SPIGCR1 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000068#define SPIGCR1_CLKMOD_MASK BIT(1)
69#define SPIGCR1_MASTER_MASK BIT(0)
70#define SPIGCR1_LOOPBACK_MASK BIT(16)
Sekhar Nori8e206f12010-08-20 16:20:49 +053071#define SPIGCR1_SPIENA_MASK BIT(24)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000072
73/* SPIBUF */
74#define SPIBUF_TXFULL_MASK BIT(29)
75#define SPIBUF_RXEMPTY_MASK BIT(31)
76
Brian Niebuhr7abbf232010-08-19 15:07:38 +053077/* SPIDELAY */
78#define SPIDELAY_C2TDELAY_SHIFT 24
79#define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
80#define SPIDELAY_T2CDELAY_SHIFT 16
81#define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
82#define SPIDELAY_T2EDELAY_SHIFT 8
83#define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
84#define SPIDELAY_C2EDELAY_SHIFT 0
85#define SPIDELAY_C2EDELAY_MASK 0xFF
86
Sandeep Paulraj358934a2009-12-16 22:02:18 +000087/* Error Masks */
88#define SPIFLG_DLEN_ERR_MASK BIT(0)
89#define SPIFLG_TIMEOUT_MASK BIT(1)
90#define SPIFLG_PARERR_MASK BIT(2)
91#define SPIFLG_DESYNC_MASK BIT(3)
92#define SPIFLG_BITERR_MASK BIT(4)
93#define SPIFLG_OVRRUN_MASK BIT(6)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000094#define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
Brian Niebuhr839c9962010-08-23 16:39:19 +053095#define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
96 | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
97 | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
98 | SPIFLG_OVRRUN_MASK)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000099
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000100#define SPIINT_DMA_REQ_EN BIT(16)
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000101
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000102/* SPI Controller registers */
103#define SPIGCR0 0x00
104#define SPIGCR1 0x04
105#define SPIINT 0x08
106#define SPILVL 0x0c
107#define SPIFLG 0x10
108#define SPIPC0 0x14
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000109#define SPIDAT1 0x3c
110#define SPIBUF 0x40
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000111#define SPIDELAY 0x48
112#define SPIDEF 0x4c
113#define SPIFMT0 0x50
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000114
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000115/* We have 2 DMA channels per CS, one for RX and one for TX */
116struct davinci_spi_dma {
117 int dma_tx_channel;
118 int dma_rx_channel;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530119 int dummy_param_slot;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000120 enum dma_event_q eventq;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000121};
122
123/* SPI Controller driver's private data. */
124struct davinci_spi {
125 struct spi_bitbang bitbang;
126 struct clk *clk;
127
128 u8 version;
129 resource_size_t pbase;
130 void __iomem *base;
131 size_t region_size;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530132 u32 irq;
133 struct completion done;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000134
135 const void *tx;
136 void *rx;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530137#define SPI_TMP_BUFSZ (SMP_CACHE_BYTES + 1)
138 u8 rx_tmp_buf[SPI_TMP_BUFSZ];
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530139 int rcount;
140 int wcount;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530141 struct davinci_spi_dma dma_channels;
Brian Niebuhr778e2612010-09-03 15:15:06 +0530142 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000143
144 void (*get_rx)(u32 rx_data, struct davinci_spi *);
145 u32 (*get_tx)(struct davinci_spi *);
146
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530147 u8 bytes_per_word[SPI_MAX_CHIPSELECT];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000148};
149
Brian Niebuhr53a31b02010-08-16 15:05:51 +0530150static struct davinci_spi_config davinci_spi_default_cfg;
151
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000152static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
153{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530154 if (davinci_spi->rx) {
155 u8 *rx = davinci_spi->rx;
156 *rx++ = (u8)data;
157 davinci_spi->rx = rx;
158 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000159}
160
161static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
162{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530163 if (davinci_spi->rx) {
164 u16 *rx = davinci_spi->rx;
165 *rx++ = (u16)data;
166 davinci_spi->rx = rx;
167 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000168}
169
170static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
171{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530172 u32 data = 0;
173 if (davinci_spi->tx) {
174 const u8 *tx = davinci_spi->tx;
175 data = *tx++;
176 davinci_spi->tx = tx;
177 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000178 return data;
179}
180
181static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
182{
Brian Niebuhr53d454a2010-08-19 17:04:25 +0530183 u32 data = 0;
184 if (davinci_spi->tx) {
185 const u16 *tx = davinci_spi->tx;
186 data = *tx++;
187 davinci_spi->tx = tx;
188 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000189 return data;
190}
191
192static inline void set_io_bits(void __iomem *addr, u32 bits)
193{
194 u32 v = ioread32(addr);
195
196 v |= bits;
197 iowrite32(v, addr);
198}
199
200static inline void clear_io_bits(void __iomem *addr, u32 bits)
201{
202 u32 v = ioread32(addr);
203
204 v &= ~bits;
205 iowrite32(v, addr);
206}
207
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000208/*
209 * Interface to control the chip select signal
210 */
211static void davinci_spi_chipselect(struct spi_device *spi, int value)
212{
213 struct davinci_spi *davinci_spi;
214 struct davinci_spi_platform_data *pdata;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530215 u8 chip_sel = spi->chip_select;
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +0530216 u16 spidat1_cfg = CS_DEFAULT;
Brian Niebuhr23853972010-08-13 10:57:44 +0530217 bool gpio_chipsel = false;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000218
219 davinci_spi = spi_master_get_devdata(spi->master);
220 pdata = davinci_spi->pdata;
221
Brian Niebuhr23853972010-08-13 10:57:44 +0530222 if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
223 pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
224 gpio_chipsel = true;
225
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000226 /*
227 * Board specific chip select logic decides the polarity and cs
228 * line for the controller
229 */
Brian Niebuhr23853972010-08-13 10:57:44 +0530230 if (gpio_chipsel) {
231 if (value == BITBANG_CS_ACTIVE)
232 gpio_set_value(pdata->chip_sel[chip_sel], 0);
233 else
234 gpio_set_value(pdata->chip_sel[chip_sel], 1);
235 } else {
236 if (value == BITBANG_CS_ACTIVE) {
237 spidat1_cfg |= SPIDAT1_CSHOLD_MASK;
238 spidat1_cfg &= ~(0x1 << chip_sel);
239 }
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530240
Brian Niebuhr23853972010-08-13 10:57:44 +0530241 iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2);
242 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000243}
244
245/**
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530246 * davinci_spi_get_prescale - Calculates the correct prescale value
247 * @maxspeed_hz: the maximum rate the SPI clock can run at
248 *
249 * This function calculates the prescale value that generates a clock rate
250 * less than or equal to the specified maximum.
251 *
252 * Returns: calculated prescale - 1 for easy programming into SPI registers
253 * or negative error number if valid prescalar cannot be updated.
254 */
255static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi,
256 u32 max_speed_hz)
257{
258 int ret;
259
260 ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz);
261
262 if (ret < 3 || ret > 256)
263 return -EINVAL;
264
265 return ret - 1;
266}
267
268/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000269 * davinci_spi_setup_transfer - This functions will determine transfer method
270 * @spi: spi device on which data transfer to be done
271 * @t: spi transfer in which transfer info is filled
272 *
273 * This function determines data transfer method (8/16/32 bit transfer).
274 * It will also set the SPI Clock Control register according to
275 * SPI slave device freq.
276 */
277static int davinci_spi_setup_transfer(struct spi_device *spi,
278 struct spi_transfer *t)
279{
280
281 struct davinci_spi *davinci_spi;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530282 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000283 u8 bits_per_word = 0;
Brian Niebuhr25f33512010-08-19 12:15:22 +0530284 u32 hz = 0, spifmt = 0, prescale = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000285
286 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530287 spicfg = (struct davinci_spi_config *)spi->controller_data;
288 if (!spicfg)
289 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000290
291 if (t) {
292 bits_per_word = t->bits_per_word;
293 hz = t->speed_hz;
294 }
295
296 /* if bits_per_word is not set then set it default */
297 if (!bits_per_word)
298 bits_per_word = spi->bits_per_word;
299
300 /*
301 * Assign function pointer to appropriate transfer method
302 * 8bit, 16bit or 32bit transfer
303 */
304 if (bits_per_word <= 8 && bits_per_word >= 2) {
305 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
306 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530307 davinci_spi->bytes_per_word[spi->chip_select] = 1;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000308 } else if (bits_per_word <= 16 && bits_per_word >= 2) {
309 davinci_spi->get_rx = davinci_spi_rx_buf_u16;
310 davinci_spi->get_tx = davinci_spi_tx_buf_u16;
Brian Niebuhrcda987e2010-08-19 16:16:28 +0530311 davinci_spi->bytes_per_word[spi->chip_select] = 2;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000312 } else
313 return -EINVAL;
314
315 if (!hz)
316 hz = spi->max_speed_hz;
317
Brian Niebuhr25f33512010-08-19 12:15:22 +0530318 /* Set up SPIFMTn register, unique to this chipselect. */
319
Brian Niebuhr7fe00922010-08-13 13:27:23 +0530320 prescale = davinci_spi_get_prescale(davinci_spi, hz);
321 if (prescale < 0)
322 return prescale;
323
Brian Niebuhr25f33512010-08-19 12:15:22 +0530324 spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000325
Brian Niebuhr25f33512010-08-19 12:15:22 +0530326 if (spi->mode & SPI_LSB_FIRST)
327 spifmt |= SPIFMT_SHIFTDIR_MASK;
328
329 if (spi->mode & SPI_CPOL)
330 spifmt |= SPIFMT_POLARITY_MASK;
331
332 if (!(spi->mode & SPI_CPHA))
333 spifmt |= SPIFMT_PHASE_MASK;
334
335 /*
336 * Version 1 hardware supports two basic SPI modes:
337 * - Standard SPI mode uses 4 pins, with chipselect
338 * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
339 * (distinct from SPI_3WIRE, with just one data wire;
340 * or similar variants without MOSI or without MISO)
341 *
342 * Version 2 hardware supports an optional handshaking signal,
343 * so it can support two more modes:
344 * - 5 pin SPI variant is standard SPI plus SPI_READY
345 * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
346 */
347
348 if (davinci_spi->version == SPI_VERSION_2) {
349
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530350 u32 delay = 0;
351
Brian Niebuhr25f33512010-08-19 12:15:22 +0530352 spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
353 & SPIFMT_WDELAY_MASK);
354
355 if (spicfg->odd_parity)
356 spifmt |= SPIFMT_ODD_PARITY_MASK;
357
358 if (spicfg->parity_enable)
359 spifmt |= SPIFMT_PARITYENA_MASK;
360
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530361 if (spicfg->timer_disable) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530362 spifmt |= SPIFMT_DISTIMER_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530363 } else {
364 delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
365 & SPIDELAY_C2TDELAY_MASK;
366 delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
367 & SPIDELAY_T2CDELAY_MASK;
368 }
Brian Niebuhr25f33512010-08-19 12:15:22 +0530369
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530370 if (spi->mode & SPI_READY) {
Brian Niebuhr25f33512010-08-19 12:15:22 +0530371 spifmt |= SPIFMT_WAITENA_MASK;
Brian Niebuhr7abbf232010-08-19 15:07:38 +0530372 delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
373 & SPIDELAY_T2EDELAY_MASK;
374 delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
375 & SPIDELAY_C2EDELAY_MASK;
376 }
377
378 iowrite32(delay, davinci_spi->base + SPIDELAY);
Brian Niebuhr25f33512010-08-19 12:15:22 +0530379 }
380
381 iowrite32(spifmt, davinci_spi->base + SPIFMT0);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000382
383 return 0;
384}
385
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000386/**
387 * davinci_spi_setup - This functions will set default transfer method
388 * @spi: spi device on which data transfer to be done
389 *
390 * This functions sets the default transfer method.
391 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000392static int davinci_spi_setup(struct spi_device *spi)
393{
Brian Niebuhrb23a5d42010-09-24 18:53:32 +0530394 int retval = 0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000395 struct davinci_spi *davinci_spi;
Brian Niebuhrbe884712010-09-03 12:15:28 +0530396 struct davinci_spi_platform_data *pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000397
398 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhrbe884712010-09-03 12:15:28 +0530399 pdata = davinci_spi->pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000400
401 /* if bits per word length is zero then set it default 8 */
402 if (!spi->bits_per_word)
403 spi->bits_per_word = 8;
404
Brian Niebuhrbe884712010-09-03 12:15:28 +0530405 if (!(spi->mode & SPI_NO_CS)) {
406 if ((pdata->chip_sel == NULL) ||
407 (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
408 set_io_bits(davinci_spi->base + SPIPC0,
409 1 << spi->chip_select);
410
411 }
412
413 if (spi->mode & SPI_READY)
414 set_io_bits(davinci_spi->base + SPIPC0, SPIPC0_SPIENA_MASK);
415
416 if (spi->mode & SPI_LOOP)
417 set_io_bits(davinci_spi->base + SPIGCR1,
418 SPIGCR1_LOOPBACK_MASK);
419 else
420 clear_io_bits(davinci_spi->base + SPIGCR1,
421 SPIGCR1_LOOPBACK_MASK);
422
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000423 return retval;
424}
425
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000426static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
427 int int_status)
428{
429 struct device *sdev = davinci_spi->bitbang.master->dev.parent;
430
431 if (int_status & SPIFLG_TIMEOUT_MASK) {
432 dev_dbg(sdev, "SPI Time-out Error\n");
433 return -ETIMEDOUT;
434 }
435 if (int_status & SPIFLG_DESYNC_MASK) {
436 dev_dbg(sdev, "SPI Desynchronization Error\n");
437 return -EIO;
438 }
439 if (int_status & SPIFLG_BITERR_MASK) {
440 dev_dbg(sdev, "SPI Bit error\n");
441 return -EIO;
442 }
443
444 if (davinci_spi->version == SPI_VERSION_2) {
445 if (int_status & SPIFLG_DLEN_ERR_MASK) {
446 dev_dbg(sdev, "SPI Data Length Error\n");
447 return -EIO;
448 }
449 if (int_status & SPIFLG_PARERR_MASK) {
450 dev_dbg(sdev, "SPI Parity Error\n");
451 return -EIO;
452 }
453 if (int_status & SPIFLG_OVRRUN_MASK) {
454 dev_dbg(sdev, "SPI Data Overrun error\n");
455 return -EIO;
456 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000457 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
458 dev_dbg(sdev, "SPI Buffer Init Active\n");
459 return -EBUSY;
460 }
461 }
462
463 return 0;
464}
465
466/**
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530467 * davinci_spi_process_events - check for and handle any SPI controller events
468 * @davinci_spi: the controller data
469 *
470 * This function will check the SPIFLG register and handle any events that are
471 * detected there
472 */
473static int davinci_spi_process_events(struct davinci_spi *davinci_spi)
474{
475 u32 buf, status, errors = 0, data1_reg_val;
476
477 buf = ioread32(davinci_spi->base + SPIBUF);
478
479 if (davinci_spi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
480 davinci_spi->get_rx(buf & 0xFFFF, davinci_spi);
481 davinci_spi->rcount--;
482 }
483
484 status = ioread32(davinci_spi->base + SPIFLG);
485
486 if (unlikely(status & SPIFLG_ERROR_MASK)) {
487 errors = status & SPIFLG_ERROR_MASK;
488 goto out;
489 }
490
491 if (davinci_spi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
492 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
493 davinci_spi->wcount--;
494 data1_reg_val &= ~0xFFFF;
495 data1_reg_val |= 0xFFFF & davinci_spi->get_tx(davinci_spi);
496 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
497 }
498
499out:
500 return errors;
501}
502
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530503static void davinci_spi_dma_callback(unsigned lch, u16 status, void *data)
504{
505 struct davinci_spi *davinci_spi = data;
506 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
507
508 edma_stop(lch);
509
510 if (status == DMA_COMPLETE) {
511 if (lch == davinci_spi_dma->dma_rx_channel)
512 davinci_spi->rcount = 0;
513 if (lch == davinci_spi_dma->dma_tx_channel)
514 davinci_spi->wcount = 0;
515 }
516
517 if ((!davinci_spi->wcount && !davinci_spi->rcount) ||
518 (status != DMA_COMPLETE))
519 complete(&davinci_spi->done);
520}
521
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530522/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000523 * davinci_spi_bufs - functions which will handle transfer data
524 * @spi: spi device on which data transfer to be done
525 * @t: spi transfer in which transfer info is filled
526 *
527 * This function will put data to be transferred into data register
528 * of SPI controller and then wait until the completion will be marked
529 * by the IRQ Handler.
530 */
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530531static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000532{
533 struct davinci_spi *davinci_spi;
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530534 int data_type, ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000535 u32 tx_data, data1_reg_val;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530536 u32 errors = 0;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530537 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000538 struct davinci_spi_platform_data *pdata;
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530539 unsigned uninitialized_var(rx_buf_count);
540 struct device *sdev;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000541
542 davinci_spi = spi_master_get_devdata(spi->master);
543 pdata = davinci_spi->pdata;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530544 spicfg = (struct davinci_spi_config *)spi->controller_data;
545 if (!spicfg)
546 spicfg = &davinci_spi_default_cfg;
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530547 sdev = davinci_spi->bitbang.master->dev.parent;
548
549 /* convert len to words based on bits_per_word */
550 data_type = davinci_spi->bytes_per_word[spi->chip_select];
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000551
552 davinci_spi->tx = t->tx_buf;
553 davinci_spi->rx = t->rx_buf;
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530554 davinci_spi->wcount = t->len / data_type;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530555 davinci_spi->rcount = davinci_spi->wcount;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530556
Brian Niebuhr839c9962010-08-23 16:39:19 +0530557 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
558
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000559 /* Enable SPI */
560 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
561
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530562 INIT_COMPLETION(davinci_spi->done);
563
564 if (spicfg->io_type == SPI_IO_TYPE_INTR)
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530565 set_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530566
567 if (spicfg->io_type != SPI_IO_TYPE_DMA) {
568 /* start the transfer */
569 davinci_spi->wcount--;
570 tx_data = davinci_spi->get_tx(davinci_spi);
571 data1_reg_val &= 0xFFFF0000;
572 data1_reg_val |= tx_data & 0xFFFF;
573 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
574 } else {
575 struct davinci_spi_dma *davinci_spi_dma;
576 unsigned long tx_reg, rx_reg;
577 struct edmacc_param param;
578 void *rx_buf;
579
580 davinci_spi_dma = &davinci_spi->dma_channels;
581
582 tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
583 rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
584
585 /*
586 * Transmit DMA setup
587 *
588 * If there is transmit data, map the transmit buffer, set it
589 * as the source of data and set the source B index to data
590 * size. If there is no transmit data, set the transmit register
591 * as the source of data, and set the source B index to zero.
592 *
593 * The destination is always the transmit register itself. And
594 * the destination never increments.
595 */
596
597 if (t->tx_buf) {
598 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf,
599 davinci_spi->wcount, DMA_TO_DEVICE);
600 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
601 dev_dbg(sdev, "Unable to DMA map %d bytes"
602 "TX buffer\n",
603 davinci_spi->wcount);
604 return -ENOMEM;
605 }
606 }
607
608 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_tx_channel);
609 param.src = t->tx_buf ? t->tx_dma : tx_reg;
610 param.a_b_cnt = davinci_spi->wcount << 16 | data_type;
611 param.dst = tx_reg;
612 param.src_dst_bidx = t->tx_buf ? data_type : 0;
613 param.link_bcntrld = 0xffff;
614 param.src_dst_cidx = 0;
615 param.ccnt = 1;
616 edma_write_slot(davinci_spi_dma->dma_tx_channel, &param);
617 edma_link(davinci_spi_dma->dma_tx_channel,
618 davinci_spi_dma->dummy_param_slot);
619
620 /*
621 * Receive DMA setup
622 *
623 * If there is receive buffer, use it to receive data. If there
624 * is none provided, use a temporary receive buffer. Set the
625 * destination B index to 0 so effectively only one byte is used
626 * in the temporary buffer (address does not increment).
627 *
628 * The source of receive data is the receive data register. The
629 * source address never increments.
630 */
631
632 if (t->rx_buf) {
633 rx_buf = t->rx_buf;
634 rx_buf_count = davinci_spi->rcount;
635 } else {
636 rx_buf = davinci_spi->rx_tmp_buf;
637 rx_buf_count = sizeof(davinci_spi->rx_tmp_buf);
638 }
639
640 t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count,
641 DMA_FROM_DEVICE);
642 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
643 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
644 rx_buf_count);
645 if (t->tx_buf)
646 dma_unmap_single(NULL, t->tx_dma,
647 davinci_spi->wcount,
648 DMA_TO_DEVICE);
649 return -ENOMEM;
650 }
651
652 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_rx_channel);
653 param.src = rx_reg;
654 param.a_b_cnt = davinci_spi->rcount << 16 | data_type;
655 param.dst = t->rx_dma;
656 param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16;
657 param.link_bcntrld = 0xffff;
658 param.src_dst_cidx = 0;
659 param.ccnt = 1;
660 edma_write_slot(davinci_spi_dma->dma_rx_channel, &param);
661
662 if (pdata->cshold_bug)
663 iowrite16(data1_reg_val >> 16,
664 davinci_spi->base + SPIDAT1 + 2);
665
666 edma_start(davinci_spi_dma->dma_rx_channel);
667 edma_start(davinci_spi_dma->dma_tx_channel);
668 set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530669 }
Brian Niebuhrcf90fe72010-08-20 17:02:49 +0530670
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530671 /* Wait for the transfer to complete */
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530672 if (spicfg->io_type != SPI_IO_TYPE_POLL) {
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530673 wait_for_completion_interruptible(&(davinci_spi->done));
674 } else {
675 while (davinci_spi->rcount > 0 || davinci_spi->wcount > 0) {
676 errors = davinci_spi_process_events(davinci_spi);
677 if (errors)
678 break;
679 cpu_relax();
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000680 }
681 }
682
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530683 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530684 if (spicfg->io_type == SPI_IO_TYPE_DMA) {
685
686 if (t->tx_buf)
687 dma_unmap_single(NULL, t->tx_dma, davinci_spi->wcount,
688 DMA_TO_DEVICE);
689
690 dma_unmap_single(NULL, t->rx_dma, rx_buf_count,
691 DMA_FROM_DEVICE);
692
693 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
694 }
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530695
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000696 /*
697 * Check for bit error, desync error,parity error,timeout error and
698 * receive overflow errors
699 */
Brian Niebuhr839c9962010-08-23 16:39:19 +0530700 if (errors) {
701 ret = davinci_spi_check_error(davinci_spi, errors);
702 WARN(!ret, "%s: error reported but no error found!\n",
703 dev_name(&spi->dev));
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000704 return ret;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530705 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000706
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530707 if (davinci_spi->rcount != 0 || davinci_spi->wcount != 0) {
708 dev_err(sdev, "SPI data transfer error\n");
709 return -EIO;
710 }
711
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000712 return t->len;
713}
714
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530715/**
716 * davinci_spi_irq - Interrupt handler for SPI Master Controller
717 * @irq: IRQ number for this SPI Master
718 * @context_data: structure for SPI Master controller davinci_spi
719 *
720 * ISR will determine that interrupt arrives either for READ or WRITE command.
721 * According to command it will do the appropriate action. It will check
722 * transfer length and if it is not zero then dispatch transfer command again.
723 * If transfer length is zero then it will indicate the COMPLETION so that
724 * davinci_spi_bufs function can go ahead.
725 */
726static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
727{
728 struct davinci_spi *davinci_spi = context_data;
729 int status;
730
731 status = davinci_spi_process_events(davinci_spi);
732 if (unlikely(status != 0))
733 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
734
735 if ((!davinci_spi->rcount && !davinci_spi->wcount) || status)
736 complete(&davinci_spi->done);
737
738 return IRQ_HANDLED;
739}
740
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530741static int davinci_spi_request_dma(struct davinci_spi *davinci_spi)
Sekhar Nori903ca252010-10-01 14:51:40 +0530742{
743 int r;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530744 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
Sekhar Nori903ca252010-10-01 14:51:40 +0530745
746 r = edma_alloc_channel(davinci_spi_dma->dma_rx_channel,
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530747 davinci_spi_dma_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530748 davinci_spi_dma->eventq);
749 if (r < 0) {
750 pr_err("Unable to request DMA channel for SPI RX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530751 r = -EAGAIN;
752 goto rx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530753 }
754
755 r = edma_alloc_channel(davinci_spi_dma->dma_tx_channel,
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530756 davinci_spi_dma_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530757 davinci_spi_dma->eventq);
758 if (r < 0) {
Sekhar Nori903ca252010-10-01 14:51:40 +0530759 pr_err("Unable to request DMA channel for SPI TX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530760 r = -EAGAIN;
761 goto tx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530762 }
763
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530764 r = edma_alloc_slot(EDMA_CTLR(davinci_spi_dma->dma_tx_channel),
765 EDMA_SLOT_ANY);
766 if (r < 0) {
767 pr_err("Unable to request SPI TX DMA param slot\n");
768 r = -EAGAIN;
769 goto param_failed;
770 }
771 davinci_spi_dma->dummy_param_slot = r;
772 edma_link(davinci_spi_dma->dummy_param_slot,
773 davinci_spi_dma->dummy_param_slot);
774
Sekhar Nori903ca252010-10-01 14:51:40 +0530775 return 0;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530776param_failed:
777 edma_free_channel(davinci_spi_dma->dma_tx_channel);
778tx_dma_failed:
779 edma_free_channel(davinci_spi_dma->dma_rx_channel);
780rx_dma_failed:
781 return r;
Sekhar Nori903ca252010-10-01 14:51:40 +0530782}
783
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000784/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000785 * davinci_spi_probe - probe function for SPI Master Controller
786 * @pdev: platform_device structure which contains plateform specific data
787 */
788static int davinci_spi_probe(struct platform_device *pdev)
789{
790 struct spi_master *master;
791 struct davinci_spi *davinci_spi;
792 struct davinci_spi_platform_data *pdata;
793 struct resource *r, *mem;
794 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
795 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
796 resource_size_t dma_eventq = SPI_NO_RESOURCE;
797 int i = 0, ret = 0;
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530798 u32 spipc0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000799
800 pdata = pdev->dev.platform_data;
801 if (pdata == NULL) {
802 ret = -ENODEV;
803 goto err;
804 }
805
806 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
807 if (master == NULL) {
808 ret = -ENOMEM;
809 goto err;
810 }
811
812 dev_set_drvdata(&pdev->dev, master);
813
814 davinci_spi = spi_master_get_devdata(master);
815 if (davinci_spi == NULL) {
816 ret = -ENOENT;
817 goto free_master;
818 }
819
820 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
821 if (r == NULL) {
822 ret = -ENOENT;
823 goto free_master;
824 }
825
826 davinci_spi->pbase = r->start;
827 davinci_spi->region_size = resource_size(r);
828 davinci_spi->pdata = pdata;
829
830 mem = request_mem_region(r->start, davinci_spi->region_size,
831 pdev->name);
832 if (mem == NULL) {
833 ret = -EBUSY;
834 goto free_master;
835 }
836
Sekhar Nori50356dd2010-10-08 15:27:26 +0530837 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000838 if (davinci_spi->base == NULL) {
839 ret = -ENOMEM;
840 goto release_region;
841 }
842
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530843 davinci_spi->irq = platform_get_irq(pdev, 0);
844 if (davinci_spi->irq <= 0) {
845 ret = -EINVAL;
846 goto unmap_io;
847 }
848
849 ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0,
850 dev_name(&pdev->dev), davinci_spi);
851 if (ret)
852 goto unmap_io;
853
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000854 davinci_spi->bitbang.master = spi_master_get(master);
855 if (davinci_spi->bitbang.master == NULL) {
856 ret = -ENODEV;
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530857 goto irq_free;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000858 }
859
860 davinci_spi->clk = clk_get(&pdev->dev, NULL);
861 if (IS_ERR(davinci_spi->clk)) {
862 ret = -ENODEV;
863 goto put_master;
864 }
865 clk_enable(davinci_spi->clk);
866
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000867 master->bus_num = pdev->id;
868 master->num_chipselect = pdata->num_chipselect;
869 master->setup = davinci_spi_setup;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000870
871 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
872 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
873
874 davinci_spi->version = pdata->version;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000875
876 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
877 if (davinci_spi->version == SPI_VERSION_2)
878 davinci_spi->bitbang.flags |= SPI_READY;
879
Sekhar Nori903ca252010-10-01 14:51:40 +0530880 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
881 if (r)
882 dma_rx_chan = r->start;
883 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
884 if (r)
885 dma_tx_chan = r->start;
886 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
887 if (r)
888 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000889
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530890 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs;
Sekhar Nori903ca252010-10-01 14:51:40 +0530891 if (dma_rx_chan != SPI_NO_RESOURCE &&
892 dma_tx_chan != SPI_NO_RESOURCE &&
893 dma_eventq != SPI_NO_RESOURCE) {
894 davinci_spi->dma_channels.dma_rx_channel = dma_rx_chan;
895 davinci_spi->dma_channels.dma_tx_channel = dma_tx_chan;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530896 davinci_spi->dma_channels.eventq = dma_eventq;
897
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530898 ret = davinci_spi_request_dma(davinci_spi);
Sekhar Nori903ca252010-10-01 14:51:40 +0530899 if (ret)
900 goto free_clk;
901
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530902 dev_info(&pdev->dev, "DMA: supported\n");
903 dev_info(&pdev->dev, "DMA: RX channel: %d, TX channel: %d, "
904 "event queue: %d\n", dma_rx_chan, dma_tx_chan,
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000905 dma_eventq);
906 }
907
908 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
909 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
910
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530911 init_completion(&davinci_spi->done);
912
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000913 /* Reset In/OUT SPI module */
914 iowrite32(0, davinci_spi->base + SPIGCR0);
915 udelay(100);
916 iowrite32(1, davinci_spi->base + SPIGCR0);
917
Brian Niebuhrbe884712010-09-03 12:15:28 +0530918 /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530919 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
920 iowrite32(spipc0, davinci_spi->base + SPIPC0);
921
Brian Niebuhr23853972010-08-13 10:57:44 +0530922 /* initialize chip selects */
923 if (pdata->chip_sel) {
924 for (i = 0; i < pdata->num_chipselect; i++) {
925 if (pdata->chip_sel[i] != SPI_INTERN_CS)
926 gpio_direction_output(pdata->chip_sel[i], 1);
927 }
928 }
929
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000930 /* Clock internal */
931 if (davinci_spi->pdata->clk_internal)
932 set_io_bits(davinci_spi->base + SPIGCR1,
933 SPIGCR1_CLKMOD_MASK);
934 else
935 clear_io_bits(davinci_spi->base + SPIGCR1,
936 SPIGCR1_CLKMOD_MASK);
937
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530938 if (pdata->intr_line)
939 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
940 else
941 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
942
Brian Niebuhr843a7132010-08-12 12:49:05 +0530943 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
944
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000945 /* master mode default */
946 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
947
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000948 ret = spi_bitbang_start(&davinci_spi->bitbang);
949 if (ret)
Sekhar Nori903ca252010-10-01 14:51:40 +0530950 goto free_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000951
Brian Niebuhr3b740b12010-09-03 14:50:07 +0530952 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000953
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000954 return ret;
955
Sekhar Nori903ca252010-10-01 14:51:40 +0530956free_dma:
957 edma_free_channel(davinci_spi->dma_channels.dma_tx_channel);
958 edma_free_channel(davinci_spi->dma_channels.dma_rx_channel);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530959 edma_free_slot(davinci_spi->dma_channels.dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000960free_clk:
961 clk_disable(davinci_spi->clk);
962 clk_put(davinci_spi->clk);
963put_master:
964 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530965irq_free:
966 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000967unmap_io:
968 iounmap(davinci_spi->base);
969release_region:
970 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
971free_master:
972 kfree(master);
973err:
974 return ret;
975}
976
977/**
978 * davinci_spi_remove - remove function for SPI Master Controller
979 * @pdev: platform_device structure which contains plateform specific data
980 *
981 * This function will do the reverse action of davinci_spi_probe function
982 * It will free the IRQ and SPI controller's memory region.
983 * It will also call spi_bitbang_stop to destroy the work queue which was
984 * created by spi_bitbang_start.
985 */
986static int __exit davinci_spi_remove(struct platform_device *pdev)
987{
988 struct davinci_spi *davinci_spi;
989 struct spi_master *master;
990
991 master = dev_get_drvdata(&pdev->dev);
992 davinci_spi = spi_master_get_devdata(master);
993
994 spi_bitbang_stop(&davinci_spi->bitbang);
995
996 clk_disable(davinci_spi->clk);
997 clk_put(davinci_spi->clk);
998 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530999 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001000 iounmap(davinci_spi->base);
1001 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1002
1003 return 0;
1004}
1005
1006static struct platform_driver davinci_spi_driver = {
1007 .driver.name = "spi_davinci",
1008 .remove = __exit_p(davinci_spi_remove),
1009};
1010
1011static int __init davinci_spi_init(void)
1012{
1013 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1014}
1015module_init(davinci_spi_init);
1016
1017static void __exit davinci_spi_exit(void)
1018{
1019 platform_driver_unregister(&davinci_spi_driver);
1020}
1021module_exit(davinci_spi_exit);
1022
1023MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1024MODULE_LICENSE("GPL");