blob: f40f1be1528a647e1d6892280fcf4ef84779a030 [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/* SPIPC0 */
53#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
54#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
55#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
56#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000057
58#define SPIINT_MASKALL 0x0101035F
Brian Niebuhre0d205e2010-09-02 16:52:06 +053059#define SPIINT_MASKINT 0x0000015F
60#define SPI_INTLVL_1 0x000001FF
61#define SPI_INTLVL_0 0x00000000
Sandeep Paulraj358934a2009-12-16 22:02:18 +000062
Brian Niebuhrcfbc5d12010-08-12 12:27:33 +053063/* SPIDAT1 (upper 16 bit defines) */
64#define SPIDAT1_CSHOLD_MASK BIT(12)
65
66/* SPIGCR1 */
Sandeep Paulraj358934a2009-12-16 22:02:18 +000067#define SPIGCR1_CLKMOD_MASK BIT(1)
68#define SPIGCR1_MASTER_MASK BIT(0)
Brian Niebuhr3f27b572010-10-06 18:25:43 +053069#define SPIGCR1_POWERDOWN_MASK BIT(8)
Sandeep Paulraj358934a2009-12-16 22:02:18 +000070#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
Brian Niebuhr3f27b572010-10-06 18:25:43 +0530559 clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000560 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
Brian Niebuhr3f27b572010-10-06 18:25:43 +0530696 clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
697 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
698
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000699 /*
700 * Check for bit error, desync error,parity error,timeout error and
701 * receive overflow errors
702 */
Brian Niebuhr839c9962010-08-23 16:39:19 +0530703 if (errors) {
704 ret = davinci_spi_check_error(davinci_spi, errors);
705 WARN(!ret, "%s: error reported but no error found!\n",
706 dev_name(&spi->dev));
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000707 return ret;
Brian Niebuhr839c9962010-08-23 16:39:19 +0530708 }
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000709
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530710 if (davinci_spi->rcount != 0 || davinci_spi->wcount != 0) {
711 dev_err(sdev, "SPI data transfer error\n");
712 return -EIO;
713 }
714
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000715 return t->len;
716}
717
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530718/**
719 * davinci_spi_irq - Interrupt handler for SPI Master Controller
720 * @irq: IRQ number for this SPI Master
721 * @context_data: structure for SPI Master controller davinci_spi
722 *
723 * ISR will determine that interrupt arrives either for READ or WRITE command.
724 * According to command it will do the appropriate action. It will check
725 * transfer length and if it is not zero then dispatch transfer command again.
726 * If transfer length is zero then it will indicate the COMPLETION so that
727 * davinci_spi_bufs function can go ahead.
728 */
729static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
730{
731 struct davinci_spi *davinci_spi = context_data;
732 int status;
733
734 status = davinci_spi_process_events(davinci_spi);
735 if (unlikely(status != 0))
736 clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT);
737
738 if ((!davinci_spi->rcount && !davinci_spi->wcount) || status)
739 complete(&davinci_spi->done);
740
741 return IRQ_HANDLED;
742}
743
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530744static int davinci_spi_request_dma(struct davinci_spi *davinci_spi)
Sekhar Nori903ca252010-10-01 14:51:40 +0530745{
746 int r;
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530747 struct davinci_spi_dma *davinci_spi_dma = &davinci_spi->dma_channels;
Sekhar Nori903ca252010-10-01 14:51:40 +0530748
749 r = edma_alloc_channel(davinci_spi_dma->dma_rx_channel,
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530750 davinci_spi_dma_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530751 davinci_spi_dma->eventq);
752 if (r < 0) {
753 pr_err("Unable to request DMA channel for SPI RX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530754 r = -EAGAIN;
755 goto rx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530756 }
757
758 r = edma_alloc_channel(davinci_spi_dma->dma_tx_channel,
Brian Niebuhr6dbd29b2010-10-05 15:43:08 +0530759 davinci_spi_dma_callback, davinci_spi,
Sekhar Nori903ca252010-10-01 14:51:40 +0530760 davinci_spi_dma->eventq);
761 if (r < 0) {
Sekhar Nori903ca252010-10-01 14:51:40 +0530762 pr_err("Unable to request DMA channel for SPI TX\n");
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530763 r = -EAGAIN;
764 goto tx_dma_failed;
Sekhar Nori903ca252010-10-01 14:51:40 +0530765 }
766
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530767 r = edma_alloc_slot(EDMA_CTLR(davinci_spi_dma->dma_tx_channel),
768 EDMA_SLOT_ANY);
769 if (r < 0) {
770 pr_err("Unable to request SPI TX DMA param slot\n");
771 r = -EAGAIN;
772 goto param_failed;
773 }
774 davinci_spi_dma->dummy_param_slot = r;
775 edma_link(davinci_spi_dma->dummy_param_slot,
776 davinci_spi_dma->dummy_param_slot);
777
Sekhar Nori903ca252010-10-01 14:51:40 +0530778 return 0;
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530779param_failed:
780 edma_free_channel(davinci_spi_dma->dma_tx_channel);
781tx_dma_failed:
782 edma_free_channel(davinci_spi_dma->dma_rx_channel);
783rx_dma_failed:
784 return r;
Sekhar Nori903ca252010-10-01 14:51:40 +0530785}
786
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000787/**
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000788 * davinci_spi_probe - probe function for SPI Master Controller
789 * @pdev: platform_device structure which contains plateform specific data
Brian Niebuhr035540f2010-10-06 18:32:40 +0530790 *
791 * According to Linux Device Model this function will be invoked by Linux
792 * with platform_device struct which contains the device specific info.
793 * This function will map the SPI controller's memory, register IRQ,
794 * Reset SPI controller and setting its registers to default value.
795 * It will invoke spi_bitbang_start to create work queue so that client driver
796 * can register transfer method to work queue.
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000797 */
798static int davinci_spi_probe(struct platform_device *pdev)
799{
800 struct spi_master *master;
801 struct davinci_spi *davinci_spi;
802 struct davinci_spi_platform_data *pdata;
803 struct resource *r, *mem;
804 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
805 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
806 resource_size_t dma_eventq = SPI_NO_RESOURCE;
807 int i = 0, ret = 0;
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530808 u32 spipc0;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000809
810 pdata = pdev->dev.platform_data;
811 if (pdata == NULL) {
812 ret = -ENODEV;
813 goto err;
814 }
815
816 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
817 if (master == NULL) {
818 ret = -ENOMEM;
819 goto err;
820 }
821
822 dev_set_drvdata(&pdev->dev, master);
823
824 davinci_spi = spi_master_get_devdata(master);
825 if (davinci_spi == NULL) {
826 ret = -ENOENT;
827 goto free_master;
828 }
829
830 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
831 if (r == NULL) {
832 ret = -ENOENT;
833 goto free_master;
834 }
835
836 davinci_spi->pbase = r->start;
837 davinci_spi->region_size = resource_size(r);
838 davinci_spi->pdata = pdata;
839
840 mem = request_mem_region(r->start, davinci_spi->region_size,
841 pdev->name);
842 if (mem == NULL) {
843 ret = -EBUSY;
844 goto free_master;
845 }
846
Sekhar Nori50356dd2010-10-08 15:27:26 +0530847 davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000848 if (davinci_spi->base == NULL) {
849 ret = -ENOMEM;
850 goto release_region;
851 }
852
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530853 davinci_spi->irq = platform_get_irq(pdev, 0);
854 if (davinci_spi->irq <= 0) {
855 ret = -EINVAL;
856 goto unmap_io;
857 }
858
859 ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0,
860 dev_name(&pdev->dev), davinci_spi);
861 if (ret)
862 goto unmap_io;
863
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000864 davinci_spi->bitbang.master = spi_master_get(master);
865 if (davinci_spi->bitbang.master == NULL) {
866 ret = -ENODEV;
Brian Niebuhrd3f71412010-09-29 12:31:54 +0530867 goto irq_free;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000868 }
869
870 davinci_spi->clk = clk_get(&pdev->dev, NULL);
871 if (IS_ERR(davinci_spi->clk)) {
872 ret = -ENODEV;
873 goto put_master;
874 }
875 clk_enable(davinci_spi->clk);
876
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000877 master->bus_num = pdev->id;
878 master->num_chipselect = pdata->num_chipselect;
879 master->setup = davinci_spi_setup;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000880
881 davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
882 davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
883
884 davinci_spi->version = pdata->version;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000885
886 davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
887 if (davinci_spi->version == SPI_VERSION_2)
888 davinci_spi->bitbang.flags |= SPI_READY;
889
Sekhar Nori903ca252010-10-01 14:51:40 +0530890 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
891 if (r)
892 dma_rx_chan = r->start;
893 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
894 if (r)
895 dma_tx_chan = r->start;
896 r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
897 if (r)
898 dma_eventq = r->start;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000899
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530900 davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs;
Sekhar Nori903ca252010-10-01 14:51:40 +0530901 if (dma_rx_chan != SPI_NO_RESOURCE &&
902 dma_tx_chan != SPI_NO_RESOURCE &&
903 dma_eventq != SPI_NO_RESOURCE) {
904 davinci_spi->dma_channels.dma_rx_channel = dma_rx_chan;
905 davinci_spi->dma_channels.dma_tx_channel = dma_tx_chan;
Brian Niebuhr96fd8812010-09-27 22:23:23 +0530906 davinci_spi->dma_channels.eventq = dma_eventq;
907
Brian Niebuhr9b189fd72010-10-05 11:38:41 +0530908 ret = davinci_spi_request_dma(davinci_spi);
Sekhar Nori903ca252010-10-01 14:51:40 +0530909 if (ret)
910 goto free_clk;
911
Brian Niebuhr87467bd2010-10-06 17:03:10 +0530912 dev_info(&pdev->dev, "DMA: supported\n");
913 dev_info(&pdev->dev, "DMA: RX channel: %d, TX channel: %d, "
914 "event queue: %d\n", dma_rx_chan, dma_tx_chan,
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000915 dma_eventq);
916 }
917
918 davinci_spi->get_rx = davinci_spi_rx_buf_u8;
919 davinci_spi->get_tx = davinci_spi_tx_buf_u8;
920
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530921 init_completion(&davinci_spi->done);
922
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000923 /* Reset In/OUT SPI module */
924 iowrite32(0, davinci_spi->base + SPIGCR0);
925 udelay(100);
926 iowrite32(1, davinci_spi->base + SPIGCR0);
927
Brian Niebuhrbe884712010-09-03 12:15:28 +0530928 /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
Brian Niebuhrf34bd4c2010-09-03 11:56:35 +0530929 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
930 iowrite32(spipc0, davinci_spi->base + SPIPC0);
931
Brian Niebuhr23853972010-08-13 10:57:44 +0530932 /* initialize chip selects */
933 if (pdata->chip_sel) {
934 for (i = 0; i < pdata->num_chipselect; i++) {
935 if (pdata->chip_sel[i] != SPI_INTERN_CS)
936 gpio_direction_output(pdata->chip_sel[i], 1);
937 }
938 }
939
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530940 if (pdata->intr_line)
941 iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
942 else
943 iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
944
Brian Niebuhr843a7132010-08-12 12:49:05 +0530945 iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
946
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000947 /* master mode default */
Brian Niebuhr3409e402010-10-06 18:13:31 +0530948 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000949 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
Brian Niebuhr3f27b572010-10-06 18:25:43 +0530950 set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000951
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000952 ret = spi_bitbang_start(&davinci_spi->bitbang);
953 if (ret)
Sekhar Nori903ca252010-10-01 14:51:40 +0530954 goto free_dma;
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000955
Brian Niebuhr3b740b12010-09-03 14:50:07 +0530956 dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000957
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000958 return ret;
959
Sekhar Nori903ca252010-10-01 14:51:40 +0530960free_dma:
961 edma_free_channel(davinci_spi->dma_channels.dma_tx_channel);
962 edma_free_channel(davinci_spi->dma_channels.dma_rx_channel);
Brian Niebuhr523c37e2010-10-04 17:35:34 +0530963 edma_free_slot(davinci_spi->dma_channels.dummy_param_slot);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000964free_clk:
965 clk_disable(davinci_spi->clk);
966 clk_put(davinci_spi->clk);
967put_master:
968 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +0530969irq_free:
970 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +0000971unmap_io:
972 iounmap(davinci_spi->base);
973release_region:
974 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
975free_master:
976 kfree(master);
977err:
978 return ret;
979}
980
981/**
982 * davinci_spi_remove - remove function for SPI Master Controller
983 * @pdev: platform_device structure which contains plateform specific data
984 *
985 * This function will do the reverse action of davinci_spi_probe function
986 * It will free the IRQ and SPI controller's memory region.
987 * It will also call spi_bitbang_stop to destroy the work queue which was
988 * created by spi_bitbang_start.
989 */
990static int __exit davinci_spi_remove(struct platform_device *pdev)
991{
992 struct davinci_spi *davinci_spi;
993 struct spi_master *master;
994
995 master = dev_get_drvdata(&pdev->dev);
996 davinci_spi = spi_master_get_devdata(master);
997
998 spi_bitbang_stop(&davinci_spi->bitbang);
999
1000 clk_disable(davinci_spi->clk);
1001 clk_put(davinci_spi->clk);
1002 spi_master_put(master);
Brian Niebuhre0d205e2010-09-02 16:52:06 +05301003 free_irq(davinci_spi->irq, davinci_spi);
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001004 iounmap(davinci_spi->base);
1005 release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
1006
1007 return 0;
1008}
1009
1010static struct platform_driver davinci_spi_driver = {
Brian Niebuhrd8c174c2010-10-06 18:47:16 +05301011 .driver = {
1012 .name = "spi_davinci",
1013 .owner = THIS_MODULE,
1014 },
Sandeep Paulraj358934a2009-12-16 22:02:18 +00001015 .remove = __exit_p(davinci_spi_remove),
1016};
1017
1018static int __init davinci_spi_init(void)
1019{
1020 return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
1021}
1022module_init(davinci_spi_init);
1023
1024static void __exit davinci_spi_exit(void)
1025{
1026 platform_driver_unregister(&davinci_spi_driver);
1027}
1028module_exit(davinci_spi_exit);
1029
1030MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1031MODULE_LICENSE("GPL");