blob: 6094e3a07853cbf1f836e3ce441a8a1879d586b0 [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 Niebuhr53d454a12010-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 Niebuhr53d454a12010-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 Niebuhr53d454a12010-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 Niebuhr53d454a12010-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
503/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000504 * davinci_spi_bufs - functions which will handle transfer data
505 * @spi: spi device on which data transfer to be done
506 * @t: spi transfer in which transfer info is filled
507 *
508 * This function will put data to be transferred into data register
509 * of SPI controller and then wait until the completion will be marked
510 * by the IRQ Handler.
511 */
512static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
513{
514 struct davinci_spi *davinci_spi;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530515 int ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000516 u32 tx_data, data1_reg_val;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530517 u32 errors = 0;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530518 struct davinci_spi_config *spicfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000519 struct davinci_spi_platform_data *pdata;
520
521 davinci_spi = spi_master_get_devdata(spi->master);
522 pdata = davinci_spi->pdata;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530523 spicfg = (struct davinci_spi_config *)spi->controller_data;
524 if (!spicfg)
525 spicfg = &davinci_spi_default_cfg;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000526
527 davinci_spi->tx = t->tx_buf;
528 davinci_spi->rx = t->rx_buf;
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530529 davinci_spi->wcount = t->len /
530 davinci_spi->bytes_per_word[spi->chip_select];
531 davinci_spi->rcount = davinci_spi->wcount;
Brian Niebuhr7978b8c2010-08-13 10:11:03 +0530532
Brian Niebuhr839c9962010-08-23 16:39:19 +0530533 data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
534
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000535 /* Enable SPI */
536 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
537
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530538 if (spicfg->io_type == SPI_IO_TYPE_INTR) {
539 set_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
540 INIT_COMPLETION(davinci_spi->done);
541 }
Brian Niebuhrcf90fe72010-08-20 17:02:49 +0530542
Brian Niebuhr839c9962010-08-23 16:39:19 +0530543 /* start the transfer */
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530544 davinci_spi->wcount--;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530545 tx_data = davinci_spi->get_tx(davinci_spi);
546 data1_reg_val &= 0xFFFF0000;
547 data1_reg_val |= tx_data & 0xFFFF;
548 iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000549
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530550 /* Wait for the transfer to complete */
551 if (spicfg->io_type == SPI_IO_TYPE_INTR) {
552 wait_for_completion_interruptible(&(davinci_spi->done));
553 } else {
554 while (davinci_spi->rcount > 0 || davinci_spi->wcount > 0) {
555 errors = davinci_spi_process_events(davinci_spi);
556 if (errors)
557 break;
558 cpu_relax();
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000559 }
560 }
561
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530562 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
563
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000564 /*
565 * Check for bit error, desync error,parity error,timeout error and
566 * receive overflow errors
567 */
Brian Niebuhr839c9962010-08-23 16:39:19 +0530568 if (errors) {
569 ret = davinci_spi_check_error(davinci_spi, errors);
570 WARN(!ret, "%s: error reported but no error found!\n",
571 dev_name(&spi->dev));
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000572 return ret;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530573 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000574
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000575 return t->len;
576}
577
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530578/**
579 * davinci_spi_irq - Interrupt handler for SPI Master Controller
580 * @irq: IRQ number for this SPI Master
581 * @context_data: structure for SPI Master controller davinci_spi
582 *
583 * ISR will determine that interrupt arrives either for READ or WRITE command.
584 * According to command it will do the appropriate action. It will check
585 * transfer length and if it is not zero then dispatch transfer command again.
586 * If transfer length is zero then it will indicate the COMPLETION so that
587 * davinci_spi_bufs function can go ahead.
588 */
589static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
590{
591 struct davinci_spi *davinci_spi = context_data;
592 int status;
593
594 status = davinci_spi_process_events(davinci_spi);
595 if (unlikely(status != 0))
596 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
597
598 if ((!davinci_spi->rcount && !davinci_spi->wcount) || status)
599 complete(&davinci_spi->done);
600
601 return IRQ_HANDLED;
602}
603
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530604static void davinci_spi_dma_callback(unsigned lch, u16 status, void *data)
605{
606 struct davinci_spi *davinci_spi = data;
607 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
608
609 edma_stop(lch);
610
611 if (status == DMA_COMPLETE) {
612 if (lch == davinci_spi_dma->dma_rx_channel)
613 davinci_spi->rcount = 0;
614 if (lch == davinci_spi_dma->dma_tx_channel)
615 davinci_spi->wcount = 0;
616 }
617
618 if ((!davinci_spi->wcount && !davinci_spi->rcount) ||
619 (status != DMA_COMPLETE))
620 complete(&davinci_spi->done);
621}
622
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000623static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
624{
625 struct davinci_spi *davinci_spi;
626 int int_status = 0;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530627 unsigned rx_buf_count;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000628 struct davinci_spi_dma *davinci_spi_dma;
Brian Niebuhrb7ab24a2010-08-19 16:42:42 +0530629 int data_type, ret;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000630 unsigned long tx_reg, rx_reg;
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530631 struct davinci_spi_platform_data *pdata;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530632 void *rx_buf;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000633 struct device *sdev;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530634 struct edmacc_param param;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000635
636 davinci_spi = spi_master_get_devdata(spi->master);
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530637 pdata = davinci_spi->pdata;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000638 sdev = davinci_spi->bitbang.master->dev.parent;
639
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530640 davinci_spi_dma = &davinci_spi->dma_channels;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000641
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530642 /* convert len to words based on bits_per_word */
643 data_type = davinci_spi->bytes_per_word[spi->chip_select];
644
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000645 tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
646 rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
647
648 davinci_spi->tx = t->tx_buf;
649 davinci_spi->rx = t->rx_buf;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530650 davinci_spi->wcount = t->len / data_type;
651 davinci_spi->rcount = davinci_spi->wcount;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000652
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530653 INIT_COMPLETION(davinci_spi->done);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000654
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000655 /* disable all interrupts for dma transfers */
656 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000657 /* Enable SPI */
658 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
659
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530660 /*
661 * Transmit DMA setup
662 *
663 * If there is transmit data, map the transmit buffer, set it as the
664 * source of data and set the source B index to data size.
665 * If there is no transmit data, set the transmit register as the
666 * source of data, and set the source B index to zero.
667 *
668 * The destination is always the transmit register itself. And the
669 * destination never increments.
670 */
671
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000672 if (t->tx_buf) {
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530673 t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf,
674 davinci_spi->wcount, DMA_TO_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000675 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530676 dev_dbg(sdev, "Unable to DMA map %d bytes TX buffer\n",
677 davinci_spi->wcount);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000678 return -ENOMEM;
679 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000680 }
681
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530682 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_tx_channel);
683 param.src = t->tx_buf ? t->tx_dma : tx_reg;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530684 param.a_b_cnt = davinci_spi->wcount << 16 | data_type;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530685 param.dst = tx_reg;
686 param.src_dst_bidx = t->tx_buf ? data_type : 0;
687 param.link_bcntrld = 0xffff;
688 param.src_dst_cidx = 0;
689 param.ccnt = 1;
690 edma_write_slot(davinci_spi_dma->dma_tx_channel, &param);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530691 edma_link(davinci_spi_dma->dma_tx_channel,
692 davinci_spi_dma->dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000693
Brian Niebuhre91c6592010-10-01 10:29:29 +0530694 /*
695 * Receive DMA setup
696 *
697 * If there is receive buffer, use it to receive data. If there
698 * is none provided, use a temporary receive buffer. Set the
699 * destination B index to 0 so effectively only one byte is used
700 * in the temporary buffer (address does not increment).
701 *
702 * The source of receive data is the receive data register. The
703 * source address never increments.
704 */
705
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000706 if (t->rx_buf) {
Brian Niebuhre91c6592010-10-01 10:29:29 +0530707 rx_buf = t->rx_buf;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530708 rx_buf_count = davinci_spi->rcount;
Brian Niebuhre91c6592010-10-01 10:29:29 +0530709 } else {
710 rx_buf = davinci_spi->rx_tmp_buf;
711 rx_buf_count = sizeof(davinci_spi->rx_tmp_buf);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000712 }
713
Brian Niebuhre91c6592010-10-01 10:29:29 +0530714 t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count,
715 DMA_FROM_DEVICE);
716 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
717 dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
718 rx_buf_count);
719 if (t->tx_buf)
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530720 dma_unmap_single(NULL, t->tx_dma, davinci_spi->wcount,
721 DMA_TO_DEVICE);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530722 return -ENOMEM;
723 }
724
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530725 param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_rx_channel);
726 param.src = rx_reg;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530727 param.a_b_cnt = davinci_spi->rcount << 16 | data_type;
Brian Niebuhr49fc3f42010-10-01 11:22:23 +0530728 param.dst = t->rx_dma;
729 param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16;
730 param.link_bcntrld = 0xffff;
731 param.src_dst_cidx = 0;
732 param.ccnt = 1;
733 edma_write_slot(davinci_spi_dma->dma_rx_channel, &param);
Brian Niebuhre91c6592010-10-01 10:29:29 +0530734
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530735 if (pdata->cshold_bug) {
736 u16 spidat1 = ioread16(davinci_spi->base + SPIDAT1 + 2);
737 iowrite16(spidat1, davinci_spi->base + SPIDAT1 + 2);
738 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000739
Brian Niebuhre91c6592010-10-01 10:29:29 +0530740 edma_start(davinci_spi_dma->dma_rx_channel);
Brian Niebuhrc29e3c62010-09-28 13:59:26 +0530741 edma_start(davinci_spi_dma->dma_tx_channel);
Brian Niebuhra4f44972010-10-01 14:00:48 +0530742 set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000743
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530744 wait_for_completion_interruptible(&davinci_spi->done);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000745
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530746 if (t->tx_buf)
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530747 dma_unmap_single(NULL, t->tx_dma, davinci_spi->wcount,
748 DMA_TO_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000749
Brian Niebuhre91c6592010-10-01 10:29:29 +0530750 dma_unmap_single(NULL, t->rx_dma, rx_buf_count, DMA_FROM_DEVICE);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000751
Brian Niebuhra4f44972010-10-01 14:00:48 +0530752 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
753
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000754 /*
755 * Check for bit error, desync error,parity error,timeout error and
756 * receive overflow errors
757 */
758 int_status = ioread32(davinci_spi->base + SPIFLG);
759
760 ret = davinci_spi_check_error(davinci_spi, int_status);
761 if (ret != 0)
762 return ret;
763
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530764 if (davinci_spi->rcount != 0 || davinci_spi->wcount != 0) {
765 dev_err(sdev, "SPI data transfer error\n");
766 return -EIO;
767 }
768
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000769 return t->len;
770}
771
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530772static int davinci_spi_request_dma(struct davinci_spi *davinci_spi)
Sekhar Nori903ca252010-10-01 14:51:40 +0530773{
774 int r;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530775 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
Sekhar Nori903ca252010-10-01 14:51:40 +0530776
777 r = edma_alloc_channel(davinci_spi_dma->dma_rx_channel,
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530778 davinci_spi_dma_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530779 davinci_spi_dma->eventq);
780 if (r < 0) {
781 pr_err("Unable to request DMA channel for SPI RX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530782 r = -EAGAIN;
783 goto rx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530784 }
785
786 r = edma_alloc_channel(davinci_spi_dma->dma_tx_channel,
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530787 davinci_spi_dma_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530788 davinci_spi_dma->eventq);
789 if (r < 0) {
Sekhar Nori903ca252010-10-01 14:51:40 +0530790 pr_err("Unable to request DMA channel for SPI TX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530791 r = -EAGAIN;
792 goto tx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530793 }
794
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530795 r = edma_alloc_slot(EDMA_CTLR(davinci_spi_dma->dma_tx_channel),
796 EDMA_SLOT_ANY);
797 if (r < 0) {
798 pr_err("Unable to request SPI TX DMA param slot\n");
799 r = -EAGAIN;
800 goto param_failed;
801 }
802 davinci_spi_dma->dummy_param_slot = r;
803 edma_link(davinci_spi_dma->dummy_param_slot,
804 davinci_spi_dma->dummy_param_slot);
805
Sekhar Nori903ca252010-10-01 14:51:40 +0530806 return 0;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530807param_failed:
808 edma_free_channel(davinci_spi_dma->dma_tx_channel);
809tx_dma_failed:
810 edma_free_channel(davinci_spi_dma->dma_rx_channel);
811rx_dma_failed:
812 return r;
Sekhar Nori903ca252010-10-01 14:51:40 +0530813}
814
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000815/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000816 * davinci_spi_probe - probe function for SPI Master Controller
817 * @pdev: platform_device structure which contains plateform specific data
818 */
819static int davinci_spi_probe(struct platform_device *pdev)
820{
821 struct spi_master *master;
822 struct davinci_spi *davinci_spi;
823 struct davinci_spi_platform_data *pdata;
824 struct resource *r, *mem;
825 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
826 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
827 resource_size_t dma_eventq = SPI_NO_RESOURCE;
828 int i = 0, ret = 0;
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530829 u32 spipc0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000830
831 pdata = pdev->dev.platform_data;
832 if (pdata == NULL) {
833 ret = -ENODEV;
834 goto err;
835 }
836
837 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
838 if (master == NULL) {
839 ret = -ENOMEM;
840 goto err;
841 }
842
843 dev_set_drvdata(&pdev->dev, master);
844
845 davinci_spi = spi_master_get_devdata(master);
846 if (davinci_spi == NULL) {
847 ret = -ENOENT;
848 goto free_master;
849 }
850
851 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
852 if (r == NULL) {
853 ret = -ENOENT;
854 goto free_master;
855 }
856
857 davinci_spi->pbase = r->start;
858 davinci_spi->region_size = resource_size(r);
859 davinci_spi->pdata = pdata;
860
861 mem = request_mem_region(r->start, davinci_spi->region_size,
862 pdev->name);
863 if (mem == NULL) {
864 ret = -EBUSY;
865 goto free_master;
866 }
867
Sekhar Nori50356dd2010-10-08 15:27:26 +0530868 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000869 if (davinci_spi->base == NULL) {
870 ret = -ENOMEM;
871 goto release_region;
872 }
873
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530874 davinci_spi->irq = platform_get_irq(pdev, 0);
875 if (davinci_spi->irq <= 0) {
876 ret = -EINVAL;
877 goto unmap_io;
878 }
879
880 ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0,
881 dev_name(&pdev->dev), davinci_spi);
882 if (ret)
883 goto unmap_io;
884
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000885 davinci_spi->bitbang.master = spi_master_get(master);
886 if (davinci_spi->bitbang.master == NULL) {
887 ret = -ENODEV;
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530888 goto irq_free;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000889 }
890
891 davinci_spi->clk = clk_get(&pdev->dev, NULL);
892 if (IS_ERR(davinci_spi->clk)) {
893 ret = -ENODEV;
894 goto put_master;
895 }
896 clk_enable(davinci_spi->clk);
897
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000898 master->bus_num = pdev->id;
899 master->num_chipselect = pdata->num_chipselect;
900 master->setup = davinci_spi_setup;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000901
902 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
903 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
904
905 davinci_spi->version = pdata->version;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000906
907 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
908 if (davinci_spi->version == SPI_VERSION_2)
909 davinci_spi->bitbang.flags |= SPI_READY;
910
Sekhar Nori903ca252010-10-01 14:51:40 +0530911 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
912 if (r)
913 dma_rx_chan = r->start;
914 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
915 if (r)
916 dma_tx_chan = r->start;
917 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
918 if (r)
919 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000920
Sekhar Nori903ca252010-10-01 14:51:40 +0530921 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
922 if (dma_rx_chan != SPI_NO_RESOURCE &&
923 dma_tx_chan != SPI_NO_RESOURCE &&
924 dma_eventq != SPI_NO_RESOURCE) {
925 davinci_spi->dma_channels.dma_rx_channel = dma_rx_chan;
926 davinci_spi->dma_channels.dma_tx_channel = dma_tx_chan;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530927 davinci_spi->dma_channels.eventq = dma_eventq;
928
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530929 ret = davinci_spi_request_dma(davinci_spi);
Sekhar Nori903ca252010-10-01 14:51:40 +0530930 if (ret)
931 goto free_clk;
932
933 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000934 dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
935 "Using RX channel = %d , TX channel = %d and "
936 "event queue = %d", dma_rx_chan, dma_tx_chan,
937 dma_eventq);
938 }
939
940 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
941 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
942
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530943 init_completion(&davinci_spi->done);
944
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000945 /* Reset In/OUT SPI module */
946 iowrite32(0, davinci_spi->base + SPIGCR0);
947 udelay(100);
948 iowrite32(1, davinci_spi->base + SPIGCR0);
949
Brian Niebuhrbe884712010-09-03 12:15:28 +0530950 /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530951 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
952 iowrite32(spipc0, davinci_spi->base + SPIPC0);
953
Brian Niebuhr23853972010-08-13 10:57:44 +0530954 /* initialize chip selects */
955 if (pdata->chip_sel) {
956 for (i = 0; i < pdata->num_chipselect; i++) {
957 if (pdata->chip_sel[i] != SPI_INTERN_CS)
958 gpio_direction_output(pdata->chip_sel[i], 1);
959 }
960 }
961
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000962 /* Clock internal */
963 if (davinci_spi->pdata->clk_internal)
964 set_io_bits(davinci_spi->base + SPIGCR1,
965 SPIGCR1_CLKMOD_MASK);
966 else
967 clear_io_bits(davinci_spi->base + SPIGCR1,
968 SPIGCR1_CLKMOD_MASK);
969
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530970 if (pdata->intr_line)
971 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
972 else
973 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
974
Brian Niebuhr843a7132010-08-12 12:49:05 +0530975 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
976
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000977 /* master mode default */
978 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
979
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000980 ret = spi_bitbang_start(&davinci_spi->bitbang);
981 if (ret)
Sekhar Nori903ca252010-10-01 14:51:40 +0530982 goto free_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000983
Brian Niebuhr3b740b12010-09-03 14:50:07 +0530984 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000985
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000986 return ret;
987
Sekhar Nori903ca252010-10-01 14:51:40 +0530988free_dma:
989 edma_free_channel(davinci_spi->dma_channels.dma_tx_channel);
990 edma_free_channel(davinci_spi->dma_channels.dma_rx_channel);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530991 edma_free_slot(davinci_spi->dma_channels.dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000992free_clk:
993 clk_disable(davinci_spi->clk);
994 clk_put(davinci_spi->clk);
995put_master:
996 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530997irq_free:
998 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000999unmap_io:
1000 iounmap(davinci_spi->base);
1001release_region:
1002 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1003free_master:
1004 kfree(master);
1005err:
1006 return ret;
1007}
1008
1009/**
1010 * davinci_spi_remove - remove function for SPI Master Controller
1011 * @pdev: platform_device structure which contains plateform specific data
1012 *
1013 * This function will do the reverse action of davinci_spi_probe function
1014 * It will free the IRQ and SPI controller's memory region.
1015 * It will also call spi_bitbang_stop to destroy the work queue which was
1016 * created by spi_bitbang_start.
1017 */
1018static int __exit davinci_spi_remove(struct platform_device *pdev)
1019{
1020 struct davinci_spi *davinci_spi;
1021 struct spi_master *master;
1022
1023 master = dev_get_drvdata(&pdev->dev);
1024 davinci_spi = spi_master_get_devdata(master);
1025
1026 spi_bitbang_stop(&davinci_spi->bitbang);
1027
1028 clk_disable(davinci_spi->clk);
1029 clk_put(davinci_spi->clk);
1030 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +05301031 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001032 iounmap(davinci_spi->base);
1033 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1034
1035 return 0;
1036}
1037
1038static struct platform_driver davinci_spi_driver = {
1039 .driver.name = "spi_davinci",
1040 .remove = __exit_p(davinci_spi_remove),
1041};
1042
1043static int __init davinci_spi_init(void)
1044{
1045 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1046}
1047module_init(davinci_spi_init);
1048
1049static void __exit davinci_spi_exit(void)
1050{
1051 platform_driver_unregister(&davinci_spi_driver);
1052}
1053module_exit(davinci_spi_exit);
1054
1055MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1056MODULE_LICENSE("GPL");