spi: davinci: Fix checkpatch issue
[deliverable/linux.git] / drivers / spi / spi-davinci.c
CommitLineData
358934a6
SP
1/*
2 * Copyright (C) 2009 Texas Instruments.
43abb11b 3 * Copyright (C) 2010 EF Johnson Technologies
358934a6
SP
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/gpio.h>
23#include <linux/module.h>
24#include <linux/delay.h>
25#include <linux/platform_device.h>
26#include <linux/err.h>
27#include <linux/clk.h>
048177ce 28#include <linux/dmaengine.h>
358934a6 29#include <linux/dma-mapping.h>
048177ce 30#include <linux/edma.h>
aae7147d
MK
31#include <linux/of.h>
32#include <linux/of_device.h>
a88e34ea 33#include <linux/of_gpio.h>
358934a6
SP
34#include <linux/spi/spi.h>
35#include <linux/spi/spi_bitbang.h>
5a0e3ad6 36#include <linux/slab.h>
358934a6 37
ec2a0833 38#include <linux/platform_data/spi-davinci.h>
358934a6
SP
39
40#define SPI_NO_RESOURCE ((resource_size_t)-1)
41
358934a6
SP
42#define CS_DEFAULT 0xFF
43
358934a6
SP
44#define SPIFMT_PHASE_MASK BIT(16)
45#define SPIFMT_POLARITY_MASK BIT(17)
46#define SPIFMT_DISTIMER_MASK BIT(18)
47#define SPIFMT_SHIFTDIR_MASK BIT(20)
48#define SPIFMT_WAITENA_MASK BIT(21)
49#define SPIFMT_PARITYENA_MASK BIT(22)
50#define SPIFMT_ODD_PARITY_MASK BIT(23)
51#define SPIFMT_WDELAY_MASK 0x3f000000u
52#define SPIFMT_WDELAY_SHIFT 24
7fe0092b 53#define SPIFMT_PRESCALE_SHIFT 8
358934a6 54
358934a6
SP
55/* SPIPC0 */
56#define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
57#define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
58#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
59#define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
358934a6
SP
60
61#define SPIINT_MASKALL 0x0101035F
e0d205e9
BN
62#define SPIINT_MASKINT 0x0000015F
63#define SPI_INTLVL_1 0x000001FF
64#define SPI_INTLVL_0 0x00000000
358934a6 65
cfbc5d1d
BN
66/* SPIDAT1 (upper 16 bit defines) */
67#define SPIDAT1_CSHOLD_MASK BIT(12)
68
69/* SPIGCR1 */
358934a6
SP
70#define SPIGCR1_CLKMOD_MASK BIT(1)
71#define SPIGCR1_MASTER_MASK BIT(0)
3f27b57c 72#define SPIGCR1_POWERDOWN_MASK BIT(8)
358934a6 73#define SPIGCR1_LOOPBACK_MASK BIT(16)
8e206f1c 74#define SPIGCR1_SPIENA_MASK BIT(24)
358934a6
SP
75
76/* SPIBUF */
77#define SPIBUF_TXFULL_MASK BIT(29)
78#define SPIBUF_RXEMPTY_MASK BIT(31)
79
7abbf23c
BN
80/* SPIDELAY */
81#define SPIDELAY_C2TDELAY_SHIFT 24
82#define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
83#define SPIDELAY_T2CDELAY_SHIFT 16
84#define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
85#define SPIDELAY_T2EDELAY_SHIFT 8
86#define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
87#define SPIDELAY_C2EDELAY_SHIFT 0
88#define SPIDELAY_C2EDELAY_MASK 0xFF
89
358934a6
SP
90/* Error Masks */
91#define SPIFLG_DLEN_ERR_MASK BIT(0)
92#define SPIFLG_TIMEOUT_MASK BIT(1)
93#define SPIFLG_PARERR_MASK BIT(2)
94#define SPIFLG_DESYNC_MASK BIT(3)
95#define SPIFLG_BITERR_MASK BIT(4)
96#define SPIFLG_OVRRUN_MASK BIT(6)
358934a6 97#define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
839c996c
BN
98#define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
99 | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
100 | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
101 | SPIFLG_OVRRUN_MASK)
8e206f1c 102
358934a6 103#define SPIINT_DMA_REQ_EN BIT(16)
358934a6 104
358934a6
SP
105/* SPI Controller registers */
106#define SPIGCR0 0x00
107#define SPIGCR1 0x04
108#define SPIINT 0x08
109#define SPILVL 0x0c
110#define SPIFLG 0x10
111#define SPIPC0 0x14
358934a6
SP
112#define SPIDAT1 0x3c
113#define SPIBUF 0x40
358934a6
SP
114#define SPIDELAY 0x48
115#define SPIDEF 0x4c
116#define SPIFMT0 0x50
358934a6 117
358934a6
SP
118/* SPI Controller driver's private data. */
119struct davinci_spi {
120 struct spi_bitbang bitbang;
121 struct clk *clk;
122
123 u8 version;
124 resource_size_t pbase;
125 void __iomem *base;
e0d205e9
BN
126 u32 irq;
127 struct completion done;
358934a6
SP
128
129 const void *tx;
130 void *rx;
e0d205e9
BN
131 int rcount;
132 int wcount;
048177ce
MP
133
134 struct dma_chan *dma_rx;
135 struct dma_chan *dma_tx;
136 int dma_rx_chnum;
137 int dma_tx_chnum;
138
aae7147d 139 struct davinci_spi_platform_data pdata;
358934a6
SP
140
141 void (*get_rx)(u32 rx_data, struct davinci_spi *);
142 u32 (*get_tx)(struct davinci_spi *);
143
7480e755 144 u8 *bytes_per_word;
358934a6
SP
145};
146
53a31b07
BN
147static struct davinci_spi_config davinci_spi_default_cfg;
148
212d4b69 149static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
358934a6 150{
212d4b69
SN
151 if (dspi->rx) {
152 u8 *rx = dspi->rx;
53d454a1 153 *rx++ = (u8)data;
212d4b69 154 dspi->rx = rx;
53d454a1 155 }
358934a6
SP
156}
157
212d4b69 158static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
358934a6 159{
212d4b69
SN
160 if (dspi->rx) {
161 u16 *rx = dspi->rx;
53d454a1 162 *rx++ = (u16)data;
212d4b69 163 dspi->rx = rx;
53d454a1 164 }
358934a6
SP
165}
166
212d4b69 167static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
358934a6 168{
53d454a1 169 u32 data = 0;
859c3377 170
212d4b69
SN
171 if (dspi->tx) {
172 const u8 *tx = dspi->tx;
859c3377 173
53d454a1 174 data = *tx++;
212d4b69 175 dspi->tx = tx;
53d454a1 176 }
358934a6
SP
177 return data;
178}
179
212d4b69 180static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
358934a6 181{
53d454a1 182 u32 data = 0;
859c3377 183
212d4b69
SN
184 if (dspi->tx) {
185 const u16 *tx = dspi->tx;
859c3377 186
53d454a1 187 data = *tx++;
212d4b69 188 dspi->tx = tx;
53d454a1 189 }
358934a6
SP
190 return data;
191}
192
193static inline void set_io_bits(void __iomem *addr, u32 bits)
194{
195 u32 v = ioread32(addr);
196
197 v |= bits;
198 iowrite32(v, addr);
199}
200
201static inline void clear_io_bits(void __iomem *addr, u32 bits)
202{
203 u32 v = ioread32(addr);
204
205 v &= ~bits;
206 iowrite32(v, addr);
207}
208
358934a6
SP
209/*
210 * Interface to control the chip select signal
211 */
212static void davinci_spi_chipselect(struct spi_device *spi, int value)
213{
212d4b69 214 struct davinci_spi *dspi;
358934a6 215 struct davinci_spi_platform_data *pdata;
7978b8c3 216 u8 chip_sel = spi->chip_select;
212d4b69 217 u16 spidat1 = CS_DEFAULT;
23853973 218 bool gpio_chipsel = false;
a88e34ea 219 int gpio;
358934a6 220
212d4b69 221 dspi = spi_master_get_devdata(spi->master);
aae7147d 222 pdata = &dspi->pdata;
358934a6 223
c0600140 224 if (spi->cs_gpio >= 0) {
a88e34ea 225 /* SPI core parse and update master->cs_gpio */
23853973 226 gpio_chipsel = true;
a88e34ea 227 gpio = spi->cs_gpio;
a88e34ea 228 }
23853973 229
358934a6
SP
230 /*
231 * Board specific chip select logic decides the polarity and cs
232 * line for the controller
233 */
23853973
BN
234 if (gpio_chipsel) {
235 if (value == BITBANG_CS_ACTIVE)
c0600140 236 gpio_set_value(gpio, spi->mode & SPI_CS_HIGH);
23853973 237 else
c0600140 238 gpio_set_value(gpio, !(spi->mode & SPI_CS_HIGH));
23853973
BN
239 } else {
240 if (value == BITBANG_CS_ACTIVE) {
212d4b69
SN
241 spidat1 |= SPIDAT1_CSHOLD_MASK;
242 spidat1 &= ~(0x1 << chip_sel);
23853973 243 }
7978b8c3 244
212d4b69 245 iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
23853973 246 }
358934a6
SP
247}
248
7fe0092b
BN
249/**
250 * davinci_spi_get_prescale - Calculates the correct prescale value
251 * @maxspeed_hz: the maximum rate the SPI clock can run at
252 *
253 * This function calculates the prescale value that generates a clock rate
254 * less than or equal to the specified maximum.
255 *
256 * Returns: calculated prescale - 1 for easy programming into SPI registers
257 * or negative error number if valid prescalar cannot be updated.
258 */
212d4b69 259static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
7fe0092b
BN
260 u32 max_speed_hz)
261{
262 int ret;
263
212d4b69 264 ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz);
7fe0092b
BN
265
266 if (ret < 3 || ret > 256)
267 return -EINVAL;
268
269 return ret - 1;
270}
271
358934a6
SP
272/**
273 * davinci_spi_setup_transfer - This functions will determine transfer method
274 * @spi: spi device on which data transfer to be done
275 * @t: spi transfer in which transfer info is filled
276 *
277 * This function determines data transfer method (8/16/32 bit transfer).
278 * It will also set the SPI Clock Control register according to
279 * SPI slave device freq.
280 */
281static int davinci_spi_setup_transfer(struct spi_device *spi,
282 struct spi_transfer *t)
283{
284
212d4b69 285 struct davinci_spi *dspi;
25f33512 286 struct davinci_spi_config *spicfg;
358934a6 287 u8 bits_per_word = 0;
32ea3944
SK
288 u32 hz = 0, spifmt = 0;
289 int prescale;
358934a6 290
212d4b69 291 dspi = spi_master_get_devdata(spi->master);
25f33512
BN
292 spicfg = (struct davinci_spi_config *)spi->controller_data;
293 if (!spicfg)
294 spicfg = &davinci_spi_default_cfg;
358934a6
SP
295
296 if (t) {
297 bits_per_word = t->bits_per_word;
298 hz = t->speed_hz;
299 }
300
301 /* if bits_per_word is not set then set it default */
302 if (!bits_per_word)
303 bits_per_word = spi->bits_per_word;
304
305 /*
306 * Assign function pointer to appropriate transfer method
307 * 8bit, 16bit or 32bit transfer
308 */
24778be2 309 if (bits_per_word <= 8) {
212d4b69
SN
310 dspi->get_rx = davinci_spi_rx_buf_u8;
311 dspi->get_tx = davinci_spi_tx_buf_u8;
312 dspi->bytes_per_word[spi->chip_select] = 1;
24778be2 313 } else {
212d4b69
SN
314 dspi->get_rx = davinci_spi_rx_buf_u16;
315 dspi->get_tx = davinci_spi_tx_buf_u16;
316 dspi->bytes_per_word[spi->chip_select] = 2;
24778be2 317 }
358934a6
SP
318
319 if (!hz)
320 hz = spi->max_speed_hz;
321
25f33512
BN
322 /* Set up SPIFMTn register, unique to this chipselect. */
323
212d4b69 324 prescale = davinci_spi_get_prescale(dspi, hz);
7fe0092b
BN
325 if (prescale < 0)
326 return prescale;
327
25f33512
BN
328 spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
329
330 if (spi->mode & SPI_LSB_FIRST)
331 spifmt |= SPIFMT_SHIFTDIR_MASK;
332
333 if (spi->mode & SPI_CPOL)
334 spifmt |= SPIFMT_POLARITY_MASK;
335
336 if (!(spi->mode & SPI_CPHA))
337 spifmt |= SPIFMT_PHASE_MASK;
338
339 /*
340 * Version 1 hardware supports two basic SPI modes:
341 * - Standard SPI mode uses 4 pins, with chipselect
342 * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
343 * (distinct from SPI_3WIRE, with just one data wire;
344 * or similar variants without MOSI or without MISO)
345 *
346 * Version 2 hardware supports an optional handshaking signal,
347 * so it can support two more modes:
348 * - 5 pin SPI variant is standard SPI plus SPI_READY
349 * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
350 */
351
212d4b69 352 if (dspi->version == SPI_VERSION_2) {
25f33512 353
7abbf23c
BN
354 u32 delay = 0;
355
25f33512
BN
356 spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
357 & SPIFMT_WDELAY_MASK);
358934a6 358
25f33512
BN
359 if (spicfg->odd_parity)
360 spifmt |= SPIFMT_ODD_PARITY_MASK;
361
362 if (spicfg->parity_enable)
363 spifmt |= SPIFMT_PARITYENA_MASK;
364
7abbf23c 365 if (spicfg->timer_disable) {
25f33512 366 spifmt |= SPIFMT_DISTIMER_MASK;
7abbf23c
BN
367 } else {
368 delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
369 & SPIDELAY_C2TDELAY_MASK;
370 delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
371 & SPIDELAY_T2CDELAY_MASK;
372 }
25f33512 373
7abbf23c 374 if (spi->mode & SPI_READY) {
25f33512 375 spifmt |= SPIFMT_WAITENA_MASK;
7abbf23c
BN
376 delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
377 & SPIDELAY_T2EDELAY_MASK;
378 delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
379 & SPIDELAY_C2EDELAY_MASK;
380 }
381
212d4b69 382 iowrite32(delay, dspi->base + SPIDELAY);
25f33512
BN
383 }
384
212d4b69 385 iowrite32(spifmt, dspi->base + SPIFMT0);
358934a6
SP
386
387 return 0;
388}
389
358934a6
SP
390/**
391 * davinci_spi_setup - This functions will set default transfer method
392 * @spi: spi device on which data transfer to be done
393 *
394 * This functions sets the default transfer method.
395 */
358934a6
SP
396static int davinci_spi_setup(struct spi_device *spi)
397{
b23a5d46 398 int retval = 0;
212d4b69 399 struct davinci_spi *dspi;
be88471b 400 struct davinci_spi_platform_data *pdata;
a88e34ea
MK
401 struct spi_master *master = spi->master;
402 struct device_node *np = spi->dev.of_node;
403 bool internal_cs = true;
c0600140 404 unsigned long flags = GPIOF_DIR_OUT;
358934a6 405
212d4b69 406 dspi = spi_master_get_devdata(spi->master);
aae7147d 407 pdata = &dspi->pdata;
358934a6 408
c0600140
GS
409 flags |= (spi->mode & SPI_CS_HIGH) ? GPIOF_INIT_LOW : GPIOF_INIT_HIGH;
410
be88471b 411 if (!(spi->mode & SPI_NO_CS)) {
a88e34ea 412 if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) {
a88e34ea
MK
413 retval = gpio_request_one(spi->cs_gpio,
414 flags, dev_name(&spi->dev));
a88e34ea
MK
415 internal_cs = false;
416 } else if (pdata->chip_sel &&
417 spi->chip_select < pdata->num_chipselect &&
418 pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) {
c0600140
GS
419 spi->cs_gpio = pdata->chip_sel[spi->chip_select];
420 retval = gpio_request_one(spi->cs_gpio,
421 flags, dev_name(&spi->dev));
a88e34ea
MK
422 internal_cs = false;
423 }
be88471b
BN
424 }
425
c0600140
GS
426 if (retval) {
427 dev_err(&spi->dev, "GPIO %d setup failed (%d)\n",
428 spi->cs_gpio, retval);
429 return retval;
430 }
431
a88e34ea
MK
432 if (internal_cs)
433 set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
434
be88471b 435 if (spi->mode & SPI_READY)
212d4b69 436 set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
be88471b
BN
437
438 if (spi->mode & SPI_LOOP)
212d4b69 439 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
be88471b 440 else
212d4b69 441 clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
be88471b 442
358934a6
SP
443 return retval;
444}
445
a88e34ea
MK
446static void davinci_spi_cleanup(struct spi_device *spi)
447{
c0600140 448 if (spi->cs_gpio >= 0)
a88e34ea
MK
449 gpio_free(spi->cs_gpio);
450}
451
212d4b69 452static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
358934a6 453{
212d4b69 454 struct device *sdev = dspi->bitbang.master->dev.parent;
358934a6
SP
455
456 if (int_status & SPIFLG_TIMEOUT_MASK) {
457 dev_dbg(sdev, "SPI Time-out Error\n");
458 return -ETIMEDOUT;
459 }
460 if (int_status & SPIFLG_DESYNC_MASK) {
461 dev_dbg(sdev, "SPI Desynchronization Error\n");
462 return -EIO;
463 }
464 if (int_status & SPIFLG_BITERR_MASK) {
465 dev_dbg(sdev, "SPI Bit error\n");
466 return -EIO;
467 }
468
212d4b69 469 if (dspi->version == SPI_VERSION_2) {
358934a6
SP
470 if (int_status & SPIFLG_DLEN_ERR_MASK) {
471 dev_dbg(sdev, "SPI Data Length Error\n");
472 return -EIO;
473 }
474 if (int_status & SPIFLG_PARERR_MASK) {
475 dev_dbg(sdev, "SPI Parity Error\n");
476 return -EIO;
477 }
478 if (int_status & SPIFLG_OVRRUN_MASK) {
479 dev_dbg(sdev, "SPI Data Overrun error\n");
480 return -EIO;
481 }
358934a6
SP
482 if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
483 dev_dbg(sdev, "SPI Buffer Init Active\n");
484 return -EBUSY;
485 }
486 }
487
488 return 0;
489}
490
e0d205e9
BN
491/**
492 * davinci_spi_process_events - check for and handle any SPI controller events
212d4b69 493 * @dspi: the controller data
e0d205e9
BN
494 *
495 * This function will check the SPIFLG register and handle any events that are
496 * detected there
497 */
212d4b69 498static int davinci_spi_process_events(struct davinci_spi *dspi)
e0d205e9 499{
212d4b69 500 u32 buf, status, errors = 0, spidat1;
e0d205e9 501
212d4b69 502 buf = ioread32(dspi->base + SPIBUF);
e0d205e9 503
212d4b69
SN
504 if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
505 dspi->get_rx(buf & 0xFFFF, dspi);
506 dspi->rcount--;
e0d205e9
BN
507 }
508
212d4b69 509 status = ioread32(dspi->base + SPIFLG);
e0d205e9
BN
510
511 if (unlikely(status & SPIFLG_ERROR_MASK)) {
512 errors = status & SPIFLG_ERROR_MASK;
513 goto out;
514 }
515
212d4b69
SN
516 if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
517 spidat1 = ioread32(dspi->base + SPIDAT1);
518 dspi->wcount--;
519 spidat1 &= ~0xFFFF;
520 spidat1 |= 0xFFFF & dspi->get_tx(dspi);
521 iowrite32(spidat1, dspi->base + SPIDAT1);
e0d205e9
BN
522 }
523
524out:
525 return errors;
526}
527
048177ce 528static void davinci_spi_dma_rx_callback(void *data)
87467bd9 529{
048177ce 530 struct davinci_spi *dspi = (struct davinci_spi *)data;
87467bd9 531
048177ce 532 dspi->rcount = 0;
87467bd9 533
048177ce
MP
534 if (!dspi->wcount && !dspi->rcount)
535 complete(&dspi->done);
536}
87467bd9 537
048177ce
MP
538static void davinci_spi_dma_tx_callback(void *data)
539{
540 struct davinci_spi *dspi = (struct davinci_spi *)data;
541
542 dspi->wcount = 0;
543
544 if (!dspi->wcount && !dspi->rcount)
212d4b69 545 complete(&dspi->done);
87467bd9
BN
546}
547
358934a6
SP
548/**
549 * davinci_spi_bufs - functions which will handle transfer data
550 * @spi: spi device on which data transfer to be done
551 * @t: spi transfer in which transfer info is filled
552 *
553 * This function will put data to be transferred into data register
554 * of SPI controller and then wait until the completion will be marked
555 * by the IRQ Handler.
556 */
87467bd9 557static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
358934a6 558{
212d4b69 559 struct davinci_spi *dspi;
048177ce 560 int data_type, ret = -ENOMEM;
212d4b69 561 u32 tx_data, spidat1;
839c996c 562 u32 errors = 0;
e0d205e9 563 struct davinci_spi_config *spicfg;
358934a6 564 struct davinci_spi_platform_data *pdata;
87467bd9 565 unsigned uninitialized_var(rx_buf_count);
048177ce
MP
566 void *dummy_buf = NULL;
567 struct scatterlist sg_rx, sg_tx;
358934a6 568
212d4b69 569 dspi = spi_master_get_devdata(spi->master);
aae7147d 570 pdata = &dspi->pdata;
e0d205e9
BN
571 spicfg = (struct davinci_spi_config *)spi->controller_data;
572 if (!spicfg)
573 spicfg = &davinci_spi_default_cfg;
87467bd9
BN
574
575 /* convert len to words based on bits_per_word */
212d4b69 576 data_type = dspi->bytes_per_word[spi->chip_select];
358934a6 577
212d4b69
SN
578 dspi->tx = t->tx_buf;
579 dspi->rx = t->rx_buf;
580 dspi->wcount = t->len / data_type;
581 dspi->rcount = dspi->wcount;
7978b8c3 582
212d4b69 583 spidat1 = ioread32(dspi->base + SPIDAT1);
839c996c 584
212d4b69
SN
585 clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
586 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
358934a6 587
16735d02 588 reinit_completion(&dspi->done);
87467bd9
BN
589
590 if (spicfg->io_type == SPI_IO_TYPE_INTR)
212d4b69 591 set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
cf90fe73 592
87467bd9
BN
593 if (spicfg->io_type != SPI_IO_TYPE_DMA) {
594 /* start the transfer */
212d4b69
SN
595 dspi->wcount--;
596 tx_data = dspi->get_tx(dspi);
597 spidat1 &= 0xFFFF0000;
598 spidat1 |= tx_data & 0xFFFF;
599 iowrite32(spidat1, dspi->base + SPIDAT1);
87467bd9 600 } else {
048177ce
MP
601 struct dma_slave_config dma_rx_conf = {
602 .direction = DMA_DEV_TO_MEM,
603 .src_addr = (unsigned long)dspi->pbase + SPIBUF,
604 .src_addr_width = data_type,
605 .src_maxburst = 1,
606 };
607 struct dma_slave_config dma_tx_conf = {
608 .direction = DMA_MEM_TO_DEV,
609 .dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
610 .dst_addr_width = data_type,
611 .dst_maxburst = 1,
612 };
613 struct dma_async_tx_descriptor *rxdesc;
614 struct dma_async_tx_descriptor *txdesc;
615 void *buf;
616
617 dummy_buf = kzalloc(t->len, GFP_KERNEL);
618 if (!dummy_buf)
619 goto err_alloc_dummy_buf;
620
621 dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
622 dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
623
624 sg_init_table(&sg_rx, 1);
625 if (!t->rx_buf)
626 buf = dummy_buf;
b1178b21 627 else
048177ce
MP
628 buf = t->rx_buf;
629 t->rx_dma = dma_map_single(&spi->dev, buf,
630 t->len, DMA_FROM_DEVICE);
631 if (!t->rx_dma) {
632 ret = -EFAULT;
633 goto err_rx_map;
87467bd9 634 }
048177ce
MP
635 sg_dma_address(&sg_rx) = t->rx_dma;
636 sg_dma_len(&sg_rx) = t->len;
87467bd9 637
048177ce
MP
638 sg_init_table(&sg_tx, 1);
639 if (!t->tx_buf)
640 buf = dummy_buf;
641 else
642 buf = (void *)t->tx_buf;
643 t->tx_dma = dma_map_single(&spi->dev, buf,
89c66ee8 644 t->len, DMA_TO_DEVICE);
048177ce
MP
645 if (!t->tx_dma) {
646 ret = -EFAULT;
647 goto err_tx_map;
87467bd9 648 }
048177ce
MP
649 sg_dma_address(&sg_tx) = t->tx_dma;
650 sg_dma_len(&sg_tx) = t->len;
651
652 rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
653 &sg_rx, 1, DMA_DEV_TO_MEM,
654 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
655 if (!rxdesc)
656 goto err_desc;
657
658 txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
659 &sg_tx, 1, DMA_MEM_TO_DEV,
660 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
661 if (!txdesc)
662 goto err_desc;
663
664 rxdesc->callback = davinci_spi_dma_rx_callback;
665 rxdesc->callback_param = (void *)dspi;
666 txdesc->callback = davinci_spi_dma_tx_callback;
667 txdesc->callback_param = (void *)dspi;
87467bd9
BN
668
669 if (pdata->cshold_bug)
212d4b69 670 iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
87467bd9 671
048177ce
MP
672 dmaengine_submit(rxdesc);
673 dmaengine_submit(txdesc);
674
675 dma_async_issue_pending(dspi->dma_rx);
676 dma_async_issue_pending(dspi->dma_tx);
677
212d4b69 678 set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
87467bd9 679 }
358934a6 680
e0d205e9 681 /* Wait for the transfer to complete */
87467bd9 682 if (spicfg->io_type != SPI_IO_TYPE_POLL) {
212d4b69 683 wait_for_completion_interruptible(&(dspi->done));
e0d205e9 684 } else {
212d4b69
SN
685 while (dspi->rcount > 0 || dspi->wcount > 0) {
686 errors = davinci_spi_process_events(dspi);
e0d205e9
BN
687 if (errors)
688 break;
689 cpu_relax();
358934a6
SP
690 }
691 }
692
212d4b69 693 clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
87467bd9 694 if (spicfg->io_type == SPI_IO_TYPE_DMA) {
212d4b69 695 clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
048177ce
MP
696
697 dma_unmap_single(&spi->dev, t->rx_dma,
698 t->len, DMA_FROM_DEVICE);
699 dma_unmap_single(&spi->dev, t->tx_dma,
700 t->len, DMA_TO_DEVICE);
701 kfree(dummy_buf);
87467bd9 702 }
e0d205e9 703
212d4b69
SN
704 clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
705 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
3f27b57c 706
358934a6
SP
707 /*
708 * Check for bit error, desync error,parity error,timeout error and
709 * receive overflow errors
710 */
839c996c 711 if (errors) {
212d4b69 712 ret = davinci_spi_check_error(dspi, errors);
839c996c
BN
713 WARN(!ret, "%s: error reported but no error found!\n",
714 dev_name(&spi->dev));
358934a6 715 return ret;
839c996c 716 }
358934a6 717
212d4b69 718 if (dspi->rcount != 0 || dspi->wcount != 0) {
048177ce 719 dev_err(&spi->dev, "SPI data transfer error\n");
87467bd9
BN
720 return -EIO;
721 }
722
358934a6 723 return t->len;
048177ce
MP
724
725err_desc:
726 dma_unmap_single(&spi->dev, t->tx_dma, t->len, DMA_TO_DEVICE);
727err_tx_map:
728 dma_unmap_single(&spi->dev, t->rx_dma, t->len, DMA_FROM_DEVICE);
729err_rx_map:
730 kfree(dummy_buf);
731err_alloc_dummy_buf:
732 return ret;
358934a6
SP
733}
734
32310aaf
MK
735/**
736 * dummy_thread_fn - dummy thread function
737 * @irq: IRQ number for this SPI Master
738 * @context_data: structure for SPI Master controller davinci_spi
739 *
740 * This is to satisfy the request_threaded_irq() API so that the irq
741 * handler is called in interrupt context.
742 */
743static irqreturn_t dummy_thread_fn(s32 irq, void *data)
744{
745 return IRQ_HANDLED;
746}
747
e0d205e9
BN
748/**
749 * davinci_spi_irq - Interrupt handler for SPI Master Controller
750 * @irq: IRQ number for this SPI Master
751 * @context_data: structure for SPI Master controller davinci_spi
752 *
753 * ISR will determine that interrupt arrives either for READ or WRITE command.
754 * According to command it will do the appropriate action. It will check
755 * transfer length and if it is not zero then dispatch transfer command again.
756 * If transfer length is zero then it will indicate the COMPLETION so that
757 * davinci_spi_bufs function can go ahead.
758 */
212d4b69 759static irqreturn_t davinci_spi_irq(s32 irq, void *data)
e0d205e9 760{
212d4b69 761 struct davinci_spi *dspi = data;
e0d205e9
BN
762 int status;
763
212d4b69 764 status = davinci_spi_process_events(dspi);
e0d205e9 765 if (unlikely(status != 0))
212d4b69 766 clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
e0d205e9 767
212d4b69
SN
768 if ((!dspi->rcount && !dspi->wcount) || status)
769 complete(&dspi->done);
e0d205e9
BN
770
771 return IRQ_HANDLED;
772}
773
212d4b69 774static int davinci_spi_request_dma(struct davinci_spi *dspi)
903ca25b 775{
048177ce
MP
776 dma_cap_mask_t mask;
777 struct device *sdev = dspi->bitbang.master->dev.parent;
903ca25b
SN
778 int r;
779
048177ce
MP
780 dma_cap_zero(mask);
781 dma_cap_set(DMA_SLAVE, mask);
782
783 dspi->dma_rx = dma_request_channel(mask, edma_filter_fn,
784 &dspi->dma_rx_chnum);
785 if (!dspi->dma_rx) {
786 dev_err(sdev, "request RX DMA channel failed\n");
787 r = -ENODEV;
523c37e7 788 goto rx_dma_failed;
903ca25b
SN
789 }
790
048177ce
MP
791 dspi->dma_tx = dma_request_channel(mask, edma_filter_fn,
792 &dspi->dma_tx_chnum);
793 if (!dspi->dma_tx) {
794 dev_err(sdev, "request TX DMA channel failed\n");
795 r = -ENODEV;
523c37e7 796 goto tx_dma_failed;
903ca25b
SN
797 }
798
799 return 0;
048177ce 800
523c37e7 801tx_dma_failed:
048177ce 802 dma_release_channel(dspi->dma_rx);
523c37e7
BN
803rx_dma_failed:
804 return r;
903ca25b
SN
805}
806
aae7147d
MK
807#if defined(CONFIG_OF)
808static const struct of_device_id davinci_spi_of_match[] = {
809 {
804413f2 810 .compatible = "ti,dm6441-spi",
aae7147d
MK
811 },
812 {
804413f2 813 .compatible = "ti,da830-spi",
aae7147d
MK
814 .data = (void *)SPI_VERSION_2,
815 },
816 { },
817};
0d2d0cc5 818MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
aae7147d
MK
819
820/**
821 * spi_davinci_get_pdata - Get platform data from DTS binding
822 * @pdev: ptr to platform data
823 * @dspi: ptr to driver data
824 *
825 * Parses and populates pdata in dspi from device tree bindings.
826 *
827 * NOTE: Not all platform data params are supported currently.
828 */
829static int spi_davinci_get_pdata(struct platform_device *pdev,
830 struct davinci_spi *dspi)
831{
832 struct device_node *node = pdev->dev.of_node;
833 struct davinci_spi_platform_data *pdata;
834 unsigned int num_cs, intr_line = 0;
835 const struct of_device_id *match;
836
837 pdata = &dspi->pdata;
838
839 pdata->version = SPI_VERSION_1;
b53b34f0 840 match = of_match_device(davinci_spi_of_match, &pdev->dev);
aae7147d
MK
841 if (!match)
842 return -ENODEV;
843
844 /* match data has the SPI version number for SPI_VERSION_2 */
845 if (match->data == (void *)SPI_VERSION_2)
846 pdata->version = SPI_VERSION_2;
847
848 /*
849 * default num_cs is 1 and all chipsel are internal to the chip
a88e34ea
MK
850 * indicated by chip_sel being NULL or cs_gpios being NULL or
851 * set to -ENOENT. num-cs includes internal as well as gpios.
aae7147d
MK
852 * indicated by chip_sel being NULL. GPIO based CS is not
853 * supported yet in DT bindings.
854 */
855 num_cs = 1;
856 of_property_read_u32(node, "num-cs", &num_cs);
857 pdata->num_chipselect = num_cs;
858 of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
859 pdata->intr_line = intr_line;
860 return 0;
861}
862#else
aae7147d
MK
863static struct davinci_spi_platform_data
864 *spi_davinci_get_pdata(struct platform_device *pdev,
865 struct davinci_spi *dspi)
866{
867 return -ENODEV;
868}
869#endif
870
358934a6
SP
871/**
872 * davinci_spi_probe - probe function for SPI Master Controller
873 * @pdev: platform_device structure which contains plateform specific data
035540f6
BN
874 *
875 * According to Linux Device Model this function will be invoked by Linux
876 * with platform_device struct which contains the device specific info.
877 * This function will map the SPI controller's memory, register IRQ,
878 * Reset SPI controller and setting its registers to default value.
879 * It will invoke spi_bitbang_start to create work queue so that client driver
880 * can register transfer method to work queue.
358934a6 881 */
fd4a319b 882static int davinci_spi_probe(struct platform_device *pdev)
358934a6
SP
883{
884 struct spi_master *master;
212d4b69 885 struct davinci_spi *dspi;
358934a6 886 struct davinci_spi_platform_data *pdata;
5b3bb596 887 struct resource *r;
358934a6
SP
888 resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
889 resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
c0600140 890 int ret = 0;
f34bd4cc 891 u32 spipc0;
358934a6 892
358934a6
SP
893 master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
894 if (master == NULL) {
895 ret = -ENOMEM;
896 goto err;
897 }
898
24b5a82c 899 platform_set_drvdata(pdev, master);
358934a6 900
212d4b69 901 dspi = spi_master_get_devdata(master);
358934a6 902
8074cf06
JH
903 if (dev_get_platdata(&pdev->dev)) {
904 pdata = dev_get_platdata(&pdev->dev);
aae7147d
MK
905 dspi->pdata = *pdata;
906 } else {
907 /* update dspi pdata with that from the DT */
908 ret = spi_davinci_get_pdata(pdev, dspi);
909 if (ret < 0)
910 goto free_master;
911 }
912
913 /* pdata in dspi is now updated and point pdata to that */
914 pdata = &dspi->pdata;
915
7480e755
MK
916 dspi->bytes_per_word = devm_kzalloc(&pdev->dev,
917 sizeof(*dspi->bytes_per_word) *
918 pdata->num_chipselect, GFP_KERNEL);
919 if (dspi->bytes_per_word == NULL) {
920 ret = -ENOMEM;
921 goto free_master;
922 }
923
358934a6
SP
924 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
925 if (r == NULL) {
926 ret = -ENOENT;
927 goto free_master;
928 }
929
212d4b69 930 dspi->pbase = r->start;
358934a6 931
5b3bb596
JH
932 dspi->base = devm_ioremap_resource(&pdev->dev, r);
933 if (IS_ERR(dspi->base)) {
934 ret = PTR_ERR(dspi->base);
358934a6
SP
935 goto free_master;
936 }
937
212d4b69
SN
938 dspi->irq = platform_get_irq(pdev, 0);
939 if (dspi->irq <= 0) {
e0d205e9 940 ret = -EINVAL;
5b3bb596 941 goto free_master;
e0d205e9
BN
942 }
943
5b3bb596
JH
944 ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
945 dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
e0d205e9 946 if (ret)
5b3bb596 947 goto free_master;
e0d205e9 948
94c69f76 949 dspi->bitbang.master = master;
358934a6 950
5b3bb596 951 dspi->clk = devm_clk_get(&pdev->dev, NULL);
212d4b69 952 if (IS_ERR(dspi->clk)) {
358934a6 953 ret = -ENODEV;
5b3bb596 954 goto free_master;
358934a6 955 }
aae7147d 956 clk_prepare_enable(dspi->clk);
358934a6 957
aae7147d 958 master->dev.of_node = pdev->dev.of_node;
358934a6
SP
959 master->bus_num = pdev->id;
960 master->num_chipselect = pdata->num_chipselect;
24778be2 961 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
358934a6 962 master->setup = davinci_spi_setup;
a88e34ea 963 master->cleanup = davinci_spi_cleanup;
358934a6 964
212d4b69
SN
965 dspi->bitbang.chipselect = davinci_spi_chipselect;
966 dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
358934a6 967
212d4b69 968 dspi->version = pdata->version;
358934a6 969
212d4b69
SN
970 dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
971 if (dspi->version == SPI_VERSION_2)
972 dspi->bitbang.flags |= SPI_READY;
358934a6 973
903ca25b
SN
974 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
975 if (r)
976 dma_rx_chan = r->start;
977 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
978 if (r)
979 dma_tx_chan = r->start;
903ca25b 980
212d4b69 981 dspi->bitbang.txrx_bufs = davinci_spi_bufs;
903ca25b 982 if (dma_rx_chan != SPI_NO_RESOURCE &&
2e3e2a5e 983 dma_tx_chan != SPI_NO_RESOURCE) {
048177ce
MP
984 dspi->dma_rx_chnum = dma_rx_chan;
985 dspi->dma_tx_chnum = dma_tx_chan;
96fd881f 986
212d4b69 987 ret = davinci_spi_request_dma(dspi);
903ca25b
SN
988 if (ret)
989 goto free_clk;
990
87467bd9 991 dev_info(&pdev->dev, "DMA: supported\n");
859c3377
JH
992 dev_info(&pdev->dev, "DMA: RX channel: %pa, TX channel: %pa, event queue: %d\n",
993 &dma_rx_chan, &dma_tx_chan,
2e3e2a5e 994 pdata->dma_event_q);
358934a6
SP
995 }
996
212d4b69
SN
997 dspi->get_rx = davinci_spi_rx_buf_u8;
998 dspi->get_tx = davinci_spi_tx_buf_u8;
358934a6 999
212d4b69 1000 init_completion(&dspi->done);
e0d205e9 1001
358934a6 1002 /* Reset In/OUT SPI module */
212d4b69 1003 iowrite32(0, dspi->base + SPIGCR0);
358934a6 1004 udelay(100);
212d4b69 1005 iowrite32(1, dspi->base + SPIGCR0);
358934a6 1006
be88471b 1007 /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
f34bd4cc 1008 spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
212d4b69 1009 iowrite32(spipc0, dspi->base + SPIPC0);
f34bd4cc 1010
e0d205e9 1011 if (pdata->intr_line)
212d4b69 1012 iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
e0d205e9 1013 else
212d4b69 1014 iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
e0d205e9 1015
212d4b69 1016 iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
843a713b 1017
358934a6 1018 /* master mode default */
212d4b69
SN
1019 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
1020 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
1021 set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
358934a6 1022
212d4b69 1023 ret = spi_bitbang_start(&dspi->bitbang);
358934a6 1024 if (ret)
903ca25b 1025 goto free_dma;
358934a6 1026
212d4b69 1027 dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
358934a6 1028
358934a6
SP
1029 return ret;
1030
903ca25b 1031free_dma:
048177ce
MP
1032 dma_release_channel(dspi->dma_rx);
1033 dma_release_channel(dspi->dma_tx);
358934a6 1034free_clk:
aae7147d 1035 clk_disable_unprepare(dspi->clk);
358934a6 1036free_master:
94c69f76 1037 spi_master_put(master);
358934a6
SP
1038err:
1039 return ret;
1040}
1041
1042/**
1043 * davinci_spi_remove - remove function for SPI Master Controller
1044 * @pdev: platform_device structure which contains plateform specific data
1045 *
1046 * This function will do the reverse action of davinci_spi_probe function
1047 * It will free the IRQ and SPI controller's memory region.
1048 * It will also call spi_bitbang_stop to destroy the work queue which was
1049 * created by spi_bitbang_start.
1050 */
fd4a319b 1051static int davinci_spi_remove(struct platform_device *pdev)
358934a6 1052{
212d4b69 1053 struct davinci_spi *dspi;
358934a6
SP
1054 struct spi_master *master;
1055
24b5a82c 1056 master = platform_get_drvdata(pdev);
212d4b69 1057 dspi = spi_master_get_devdata(master);
358934a6 1058
212d4b69 1059 spi_bitbang_stop(&dspi->bitbang);
358934a6 1060
aae7147d 1061 clk_disable_unprepare(dspi->clk);
94c69f76 1062 spi_master_put(master);
358934a6
SP
1063
1064 return 0;
1065}
1066
1067static struct platform_driver davinci_spi_driver = {
d8c174cd
BN
1068 .driver = {
1069 .name = "spi_davinci",
1070 .owner = THIS_MODULE,
b53b34f0 1071 .of_match_table = of_match_ptr(davinci_spi_of_match),
d8c174cd 1072 },
940ab889 1073 .probe = davinci_spi_probe,
fd4a319b 1074 .remove = davinci_spi_remove,
358934a6 1075};
940ab889 1076module_platform_driver(davinci_spi_driver);
358934a6
SP
1077
1078MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1079MODULE_LICENSE("GPL");
This page took 0.412701 seconds and 5 git commands to generate.