spi-davinci.c 26.6 KB
Newer Older
1 2
/*
 * Copyright (C) 2009 Texas Instruments.
3
 * Copyright (C) 2010 EF Johnson Technologies
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/clk.h>
28
#include <linux/dmaengine.h>
29
#include <linux/dma-mapping.h>
30
#include <linux/edma.h>
31 32
#include <linux/of.h>
#include <linux/of_device.h>
33 34
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
35
#include <linux/slab.h>
36

37
#include <linux/platform_data/spi-davinci.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51

#define SPI_NO_RESOURCE		((resource_size_t)-1)

#define CS_DEFAULT	0xFF

#define SPIFMT_PHASE_MASK	BIT(16)
#define SPIFMT_POLARITY_MASK	BIT(17)
#define SPIFMT_DISTIMER_MASK	BIT(18)
#define SPIFMT_SHIFTDIR_MASK	BIT(20)
#define SPIFMT_WAITENA_MASK	BIT(21)
#define SPIFMT_PARITYENA_MASK	BIT(22)
#define SPIFMT_ODD_PARITY_MASK	BIT(23)
#define SPIFMT_WDELAY_MASK	0x3f000000u
#define SPIFMT_WDELAY_SHIFT	24
52
#define SPIFMT_PRESCALE_SHIFT	8
53 54 55 56 57 58 59 60

/* SPIPC0 */
#define SPIPC0_DIFUN_MASK	BIT(11)		/* MISO */
#define SPIPC0_DOFUN_MASK	BIT(10)		/* MOSI */
#define SPIPC0_CLKFUN_MASK	BIT(9)		/* CLK */
#define SPIPC0_SPIENA_MASK	BIT(8)		/* nREADY */

#define SPIINT_MASKALL		0x0101035F
61 62 63
#define SPIINT_MASKINT		0x0000015F
#define SPI_INTLVL_1		0x000001FF
#define SPI_INTLVL_0		0x00000000
64

65 66 67 68
/* SPIDAT1 (upper 16 bit defines) */
#define SPIDAT1_CSHOLD_MASK	BIT(12)

/* SPIGCR1 */
69 70
#define SPIGCR1_CLKMOD_MASK	BIT(1)
#define SPIGCR1_MASTER_MASK     BIT(0)
71
#define SPIGCR1_POWERDOWN_MASK	BIT(8)
72
#define SPIGCR1_LOOPBACK_MASK	BIT(16)
73
#define SPIGCR1_SPIENA_MASK	BIT(24)
74 75 76 77 78

/* SPIBUF */
#define SPIBUF_TXFULL_MASK	BIT(29)
#define SPIBUF_RXEMPTY_MASK	BIT(31)

79 80 81 82 83 84 85 86 87 88
/* SPIDELAY */
#define SPIDELAY_C2TDELAY_SHIFT 24
#define SPIDELAY_C2TDELAY_MASK  (0xFF << SPIDELAY_C2TDELAY_SHIFT)
#define SPIDELAY_T2CDELAY_SHIFT 16
#define SPIDELAY_T2CDELAY_MASK  (0xFF << SPIDELAY_T2CDELAY_SHIFT)
#define SPIDELAY_T2EDELAY_SHIFT 8
#define SPIDELAY_T2EDELAY_MASK  (0xFF << SPIDELAY_T2EDELAY_SHIFT)
#define SPIDELAY_C2EDELAY_SHIFT 0
#define SPIDELAY_C2EDELAY_MASK  0xFF

89 90 91 92 93 94 95 96
/* Error Masks */
#define SPIFLG_DLEN_ERR_MASK		BIT(0)
#define SPIFLG_TIMEOUT_MASK		BIT(1)
#define SPIFLG_PARERR_MASK		BIT(2)
#define SPIFLG_DESYNC_MASK		BIT(3)
#define SPIFLG_BITERR_MASK		BIT(4)
#define SPIFLG_OVRRUN_MASK		BIT(6)
#define SPIFLG_BUF_INIT_ACTIVE_MASK	BIT(24)
97 98 99 100
#define SPIFLG_ERROR_MASK		(SPIFLG_DLEN_ERR_MASK \
				| SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
				| SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
				| SPIFLG_OVRRUN_MASK)
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#define SPIINT_DMA_REQ_EN	BIT(16)

/* SPI Controller registers */
#define SPIGCR0		0x00
#define SPIGCR1		0x04
#define SPIINT		0x08
#define SPILVL		0x0c
#define SPIFLG		0x10
#define SPIPC0		0x14
#define SPIDAT1		0x3c
#define SPIBUF		0x40
#define SPIDELAY	0x48
#define SPIDEF		0x4c
#define SPIFMT0		0x50

/* SPI Controller driver's private data. */
struct davinci_spi {
	struct spi_bitbang	bitbang;
	struct clk		*clk;

	u8			version;
	resource_size_t		pbase;
	void __iomem		*base;
125 126
	u32			irq;
	struct completion	done;
127 128 129

	const void		*tx;
	void			*rx;
130 131
	int			rcount;
	int			wcount;
132 133 134 135 136 137

	struct dma_chan		*dma_rx;
	struct dma_chan		*dma_tx;
	int			dma_rx_chnum;
	int			dma_tx_chnum;

138
	struct davinci_spi_platform_data pdata;
139 140 141 142

	void			(*get_rx)(u32 rx_data, struct davinci_spi *);
	u32			(*get_tx)(struct davinci_spi *);

143
	u8			*bytes_per_word;
144 145
};

146 147
static struct davinci_spi_config davinci_spi_default_cfg;

S
Sekhar Nori 已提交
148
static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
149
{
S
Sekhar Nori 已提交
150 151
	if (dspi->rx) {
		u8 *rx = dspi->rx;
152
		*rx++ = (u8)data;
S
Sekhar Nori 已提交
153
		dspi->rx = rx;
154
	}
155 156
}

S
Sekhar Nori 已提交
157
static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
158
{
S
Sekhar Nori 已提交
159 160
	if (dspi->rx) {
		u16 *rx = dspi->rx;
161
		*rx++ = (u16)data;
S
Sekhar Nori 已提交
162
		dspi->rx = rx;
163
	}
164 165
}

S
Sekhar Nori 已提交
166
static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
167
{
168
	u32 data = 0;
S
Sekhar Nori 已提交
169 170
	if (dspi->tx) {
		const u8 *tx = dspi->tx;
171
		data = *tx++;
S
Sekhar Nori 已提交
172
		dspi->tx = tx;
173
	}
174 175 176
	return data;
}

S
Sekhar Nori 已提交
177
static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
178
{
179
	u32 data = 0;
S
Sekhar Nori 已提交
180 181
	if (dspi->tx) {
		const u16 *tx = dspi->tx;
182
		data = *tx++;
S
Sekhar Nori 已提交
183
		dspi->tx = tx;
184
	}
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	return data;
}

static inline void set_io_bits(void __iomem *addr, u32 bits)
{
	u32 v = ioread32(addr);

	v |= bits;
	iowrite32(v, addr);
}

static inline void clear_io_bits(void __iomem *addr, u32 bits)
{
	u32 v = ioread32(addr);

	v &= ~bits;
	iowrite32(v, addr);
}

/*
 * Interface to control the chip select signal
 */
static void davinci_spi_chipselect(struct spi_device *spi, int value)
{
S
Sekhar Nori 已提交
209
	struct davinci_spi *dspi;
210
	struct davinci_spi_platform_data *pdata;
211
	u8 chip_sel = spi->chip_select;
S
Sekhar Nori 已提交
212
	u16 spidat1 = CS_DEFAULT;
213
	bool gpio_chipsel = false;
214

S
Sekhar Nori 已提交
215
	dspi = spi_master_get_devdata(spi->master);
216
	pdata = &dspi->pdata;
217

218 219 220 221
	if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
				pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
		gpio_chipsel = true;

222 223 224 225
	/*
	 * Board specific chip select logic decides the polarity and cs
	 * line for the controller
	 */
226 227 228 229 230 231 232
	if (gpio_chipsel) {
		if (value == BITBANG_CS_ACTIVE)
			gpio_set_value(pdata->chip_sel[chip_sel], 0);
		else
			gpio_set_value(pdata->chip_sel[chip_sel], 1);
	} else {
		if (value == BITBANG_CS_ACTIVE) {
S
Sekhar Nori 已提交
233 234
			spidat1 |= SPIDAT1_CSHOLD_MASK;
			spidat1 &= ~(0x1 << chip_sel);
235
		}
236

S
Sekhar Nori 已提交
237
		iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
238
	}
239 240
}

241 242 243 244 245 246 247 248 249 250
/**
 * davinci_spi_get_prescale - Calculates the correct prescale value
 * @maxspeed_hz: the maximum rate the SPI clock can run at
 *
 * This function calculates the prescale value that generates a clock rate
 * less than or equal to the specified maximum.
 *
 * Returns: calculated prescale - 1 for easy programming into SPI registers
 * or negative error number if valid prescalar cannot be updated.
 */
S
Sekhar Nori 已提交
251
static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
252 253 254 255
							u32 max_speed_hz)
{
	int ret;

S
Sekhar Nori 已提交
256
	ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz);
257 258 259 260 261 262 263

	if (ret < 3 || ret > 256)
		return -EINVAL;

	return ret - 1;
}

264 265 266 267 268 269 270 271 272 273 274 275 276
/**
 * davinci_spi_setup_transfer - This functions will determine transfer method
 * @spi: spi device on which data transfer to be done
 * @t: spi transfer in which transfer info is filled
 *
 * This function determines data transfer method (8/16/32 bit transfer).
 * It will also set the SPI Clock Control register according to
 * SPI slave device freq.
 */
static int davinci_spi_setup_transfer(struct spi_device *spi,
		struct spi_transfer *t)
{

S
Sekhar Nori 已提交
277
	struct davinci_spi *dspi;
278
	struct davinci_spi_config *spicfg;
279
	u8 bits_per_word = 0;
280 281
	u32 hz = 0, spifmt = 0;
	int prescale;
282

S
Sekhar Nori 已提交
283
	dspi = spi_master_get_devdata(spi->master);
284 285 286
	spicfg = (struct davinci_spi_config *)spi->controller_data;
	if (!spicfg)
		spicfg = &davinci_spi_default_cfg;
287 288 289 290 291 292 293 294 295 296 297 298 299 300

	if (t) {
		bits_per_word = t->bits_per_word;
		hz = t->speed_hz;
	}

	/* if bits_per_word is not set then set it default */
	if (!bits_per_word)
		bits_per_word = spi->bits_per_word;

	/*
	 * Assign function pointer to appropriate transfer method
	 * 8bit, 16bit or 32bit transfer
	 */
301
	if (bits_per_word <= 8) {
S
Sekhar Nori 已提交
302 303 304
		dspi->get_rx = davinci_spi_rx_buf_u8;
		dspi->get_tx = davinci_spi_tx_buf_u8;
		dspi->bytes_per_word[spi->chip_select] = 1;
305
	} else {
S
Sekhar Nori 已提交
306 307 308
		dspi->get_rx = davinci_spi_rx_buf_u16;
		dspi->get_tx = davinci_spi_tx_buf_u16;
		dspi->bytes_per_word[spi->chip_select] = 2;
309
	}
310 311 312 313

	if (!hz)
		hz = spi->max_speed_hz;

314 315
	/* Set up SPIFMTn register, unique to this chipselect. */

S
Sekhar Nori 已提交
316
	prescale = davinci_spi_get_prescale(dspi, hz);
317 318 319
	if (prescale < 0)
		return prescale;

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);

	if (spi->mode & SPI_LSB_FIRST)
		spifmt |= SPIFMT_SHIFTDIR_MASK;

	if (spi->mode & SPI_CPOL)
		spifmt |= SPIFMT_POLARITY_MASK;

	if (!(spi->mode & SPI_CPHA))
		spifmt |= SPIFMT_PHASE_MASK;

	/*
	 * Version 1 hardware supports two basic SPI modes:
	 *  - Standard SPI mode uses 4 pins, with chipselect
	 *  - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
	 *	(distinct from SPI_3WIRE, with just one data wire;
	 *	or similar variants without MOSI or without MISO)
	 *
	 * Version 2 hardware supports an optional handshaking signal,
	 * so it can support two more modes:
	 *  - 5 pin SPI variant is standard SPI plus SPI_READY
	 *  - 4 pin with enable is (SPI_READY | SPI_NO_CS)
	 */

S
Sekhar Nori 已提交
344
	if (dspi->version == SPI_VERSION_2) {
345

346 347
		u32 delay = 0;

348 349
		spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
							& SPIFMT_WDELAY_MASK);
350

351 352 353 354 355 356
		if (spicfg->odd_parity)
			spifmt |= SPIFMT_ODD_PARITY_MASK;

		if (spicfg->parity_enable)
			spifmt |= SPIFMT_PARITYENA_MASK;

357
		if (spicfg->timer_disable) {
358
			spifmt |= SPIFMT_DISTIMER_MASK;
359 360 361 362 363 364
		} else {
			delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
						& SPIDELAY_C2TDELAY_MASK;
			delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
						& SPIDELAY_T2CDELAY_MASK;
		}
365

366
		if (spi->mode & SPI_READY) {
367
			spifmt |= SPIFMT_WAITENA_MASK;
368 369 370 371 372 373
			delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
						& SPIDELAY_T2EDELAY_MASK;
			delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
						& SPIDELAY_C2EDELAY_MASK;
		}

S
Sekhar Nori 已提交
374
		iowrite32(delay, dspi->base + SPIDELAY);
375 376
	}

S
Sekhar Nori 已提交
377
	iowrite32(spifmt, dspi->base + SPIFMT0);
378 379 380 381 382 383 384 385 386 387 388 389

	return 0;
}

/**
 * davinci_spi_setup - This functions will set default transfer method
 * @spi: spi device on which data transfer to be done
 *
 * This functions sets the default transfer method.
 */
static int davinci_spi_setup(struct spi_device *spi)
{
390
	int retval = 0;
S
Sekhar Nori 已提交
391
	struct davinci_spi *dspi;
392
	struct davinci_spi_platform_data *pdata;
393

S
Sekhar Nori 已提交
394
	dspi = spi_master_get_devdata(spi->master);
395
	pdata = &dspi->pdata;
396

397 398 399
	if (!(spi->mode & SPI_NO_CS)) {
		if ((pdata->chip_sel == NULL) ||
		    (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
S
Sekhar Nori 已提交
400
			set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
401 402 403 404

	}

	if (spi->mode & SPI_READY)
S
Sekhar Nori 已提交
405
		set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
406 407

	if (spi->mode & SPI_LOOP)
S
Sekhar Nori 已提交
408
		set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
409
	else
S
Sekhar Nori 已提交
410
		clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
411

412 413 414
	return retval;
}

S
Sekhar Nori 已提交
415
static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
416
{
S
Sekhar Nori 已提交
417
	struct device *sdev = dspi->bitbang.master->dev.parent;
418 419 420 421 422 423 424 425 426 427 428 429 430 431

	if (int_status & SPIFLG_TIMEOUT_MASK) {
		dev_dbg(sdev, "SPI Time-out Error\n");
		return -ETIMEDOUT;
	}
	if (int_status & SPIFLG_DESYNC_MASK) {
		dev_dbg(sdev, "SPI Desynchronization Error\n");
		return -EIO;
	}
	if (int_status & SPIFLG_BITERR_MASK) {
		dev_dbg(sdev, "SPI Bit error\n");
		return -EIO;
	}

S
Sekhar Nori 已提交
432
	if (dspi->version == SPI_VERSION_2) {
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		if (int_status & SPIFLG_DLEN_ERR_MASK) {
			dev_dbg(sdev, "SPI Data Length Error\n");
			return -EIO;
		}
		if (int_status & SPIFLG_PARERR_MASK) {
			dev_dbg(sdev, "SPI Parity Error\n");
			return -EIO;
		}
		if (int_status & SPIFLG_OVRRUN_MASK) {
			dev_dbg(sdev, "SPI Data Overrun error\n");
			return -EIO;
		}
		if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
			dev_dbg(sdev, "SPI Buffer Init Active\n");
			return -EBUSY;
		}
	}

	return 0;
}

454 455
/**
 * davinci_spi_process_events - check for and handle any SPI controller events
S
Sekhar Nori 已提交
456
 * @dspi: the controller data
457 458 459 460
 *
 * This function will check the SPIFLG register and handle any events that are
 * detected there
 */
S
Sekhar Nori 已提交
461
static int davinci_spi_process_events(struct davinci_spi *dspi)
462
{
S
Sekhar Nori 已提交
463
	u32 buf, status, errors = 0, spidat1;
464

S
Sekhar Nori 已提交
465
	buf = ioread32(dspi->base + SPIBUF);
466

S
Sekhar Nori 已提交
467 468 469
	if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
		dspi->get_rx(buf & 0xFFFF, dspi);
		dspi->rcount--;
470 471
	}

S
Sekhar Nori 已提交
472
	status = ioread32(dspi->base + SPIFLG);
473 474 475 476 477 478

	if (unlikely(status & SPIFLG_ERROR_MASK)) {
		errors = status & SPIFLG_ERROR_MASK;
		goto out;
	}

S
Sekhar Nori 已提交
479 480 481 482 483 484
	if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
		spidat1 = ioread32(dspi->base + SPIDAT1);
		dspi->wcount--;
		spidat1 &= ~0xFFFF;
		spidat1 |= 0xFFFF & dspi->get_tx(dspi);
		iowrite32(spidat1, dspi->base + SPIDAT1);
485 486 487 488 489 490
	}

out:
	return errors;
}

491
static void davinci_spi_dma_rx_callback(void *data)
492
{
493
	struct davinci_spi *dspi = (struct davinci_spi *)data;
494

495
	dspi->rcount = 0;
496

497 498 499
	if (!dspi->wcount && !dspi->rcount)
		complete(&dspi->done);
}
500

501 502 503 504 505 506 507
static void davinci_spi_dma_tx_callback(void *data)
{
	struct davinci_spi *dspi = (struct davinci_spi *)data;

	dspi->wcount = 0;

	if (!dspi->wcount && !dspi->rcount)
S
Sekhar Nori 已提交
508
		complete(&dspi->done);
509 510
}

511 512 513 514 515 516 517 518 519
/**
 * davinci_spi_bufs - functions which will handle transfer data
 * @spi: spi device on which data transfer to be done
 * @t: spi transfer in which transfer info is filled
 *
 * This function will put data to be transferred into data register
 * of SPI controller and then wait until the completion will be marked
 * by the IRQ Handler.
 */
520
static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
521
{
S
Sekhar Nori 已提交
522
	struct davinci_spi *dspi;
523
	int data_type, ret = -ENOMEM;
S
Sekhar Nori 已提交
524
	u32 tx_data, spidat1;
525
	u32 errors = 0;
526
	struct davinci_spi_config *spicfg;
527
	struct davinci_spi_platform_data *pdata;
528
	unsigned uninitialized_var(rx_buf_count);
529 530
	void *dummy_buf = NULL;
	struct scatterlist sg_rx, sg_tx;
531

S
Sekhar Nori 已提交
532
	dspi = spi_master_get_devdata(spi->master);
533
	pdata = &dspi->pdata;
534 535 536
	spicfg = (struct davinci_spi_config *)spi->controller_data;
	if (!spicfg)
		spicfg = &davinci_spi_default_cfg;
537 538

	/* convert len to words based on bits_per_word */
S
Sekhar Nori 已提交
539
	data_type = dspi->bytes_per_word[spi->chip_select];
540

S
Sekhar Nori 已提交
541 542 543 544
	dspi->tx = t->tx_buf;
	dspi->rx = t->rx_buf;
	dspi->wcount = t->len / data_type;
	dspi->rcount = dspi->wcount;
545

S
Sekhar Nori 已提交
546
	spidat1 = ioread32(dspi->base + SPIDAT1);
547

S
Sekhar Nori 已提交
548 549
	clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
550

551
	reinit_completion(&dspi->done);
552 553

	if (spicfg->io_type == SPI_IO_TYPE_INTR)
S
Sekhar Nori 已提交
554
		set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
555

556 557
	if (spicfg->io_type != SPI_IO_TYPE_DMA) {
		/* start the transfer */
S
Sekhar Nori 已提交
558 559 560 561 562
		dspi->wcount--;
		tx_data = dspi->get_tx(dspi);
		spidat1 &= 0xFFFF0000;
		spidat1 |= tx_data & 0xFFFF;
		iowrite32(spidat1, dspi->base + SPIDAT1);
563
	} else {
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
		struct dma_slave_config dma_rx_conf = {
			.direction = DMA_DEV_TO_MEM,
			.src_addr = (unsigned long)dspi->pbase + SPIBUF,
			.src_addr_width = data_type,
			.src_maxburst = 1,
		};
		struct dma_slave_config dma_tx_conf = {
			.direction = DMA_MEM_TO_DEV,
			.dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
			.dst_addr_width = data_type,
			.dst_maxburst = 1,
		};
		struct dma_async_tx_descriptor *rxdesc;
		struct dma_async_tx_descriptor *txdesc;
		void *buf;

		dummy_buf = kzalloc(t->len, GFP_KERNEL);
		if (!dummy_buf)
			goto err_alloc_dummy_buf;

		dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
		dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);

		sg_init_table(&sg_rx, 1);
		if (!t->rx_buf)
			buf = dummy_buf;
590
		else
591 592 593 594 595 596
			buf = t->rx_buf;
		t->rx_dma = dma_map_single(&spi->dev, buf,
				t->len, DMA_FROM_DEVICE);
		if (!t->rx_dma) {
			ret = -EFAULT;
			goto err_rx_map;
597
		}
598 599
		sg_dma_address(&sg_rx) = t->rx_dma;
		sg_dma_len(&sg_rx) = t->len;
600

601 602 603 604 605 606
		sg_init_table(&sg_tx, 1);
		if (!t->tx_buf)
			buf = dummy_buf;
		else
			buf = (void *)t->tx_buf;
		t->tx_dma = dma_map_single(&spi->dev, buf,
607
				t->len, DMA_TO_DEVICE);
608 609 610
		if (!t->tx_dma) {
			ret = -EFAULT;
			goto err_tx_map;
611
		}
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
		sg_dma_address(&sg_tx) = t->tx_dma;
		sg_dma_len(&sg_tx) = t->len;

		rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
				&sg_rx, 1, DMA_DEV_TO_MEM,
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
		if (!rxdesc)
			goto err_desc;

		txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
				&sg_tx, 1, DMA_MEM_TO_DEV,
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
		if (!txdesc)
			goto err_desc;

		rxdesc->callback = davinci_spi_dma_rx_callback;
		rxdesc->callback_param = (void *)dspi;
		txdesc->callback = davinci_spi_dma_tx_callback;
		txdesc->callback_param = (void *)dspi;
631 632

		if (pdata->cshold_bug)
S
Sekhar Nori 已提交
633
			iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
634

635 636 637 638 639 640
		dmaengine_submit(rxdesc);
		dmaengine_submit(txdesc);

		dma_async_issue_pending(dspi->dma_rx);
		dma_async_issue_pending(dspi->dma_tx);

S
Sekhar Nori 已提交
641
		set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
642
	}
643

644
	/* Wait for the transfer to complete */
645
	if (spicfg->io_type != SPI_IO_TYPE_POLL) {
S
Sekhar Nori 已提交
646
		wait_for_completion_interruptible(&(dspi->done));
647
	} else {
S
Sekhar Nori 已提交
648 649
		while (dspi->rcount > 0 || dspi->wcount > 0) {
			errors = davinci_spi_process_events(dspi);
650 651 652
			if (errors)
				break;
			cpu_relax();
653 654 655
		}
	}

S
Sekhar Nori 已提交
656
	clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
657
	if (spicfg->io_type == SPI_IO_TYPE_DMA) {
S
Sekhar Nori 已提交
658
		clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
659 660 661 662 663 664

		dma_unmap_single(&spi->dev, t->rx_dma,
				t->len, DMA_FROM_DEVICE);
		dma_unmap_single(&spi->dev, t->tx_dma,
				t->len, DMA_TO_DEVICE);
		kfree(dummy_buf);
665
	}
666

S
Sekhar Nori 已提交
667 668
	clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
669

670 671 672 673
	/*
	 * Check for bit error, desync error,parity error,timeout error and
	 * receive overflow errors
	 */
674
	if (errors) {
S
Sekhar Nori 已提交
675
		ret = davinci_spi_check_error(dspi, errors);
676 677
		WARN(!ret, "%s: error reported but no error found!\n",
							dev_name(&spi->dev));
678
		return ret;
679
	}
680

S
Sekhar Nori 已提交
681
	if (dspi->rcount != 0 || dspi->wcount != 0) {
682
		dev_err(&spi->dev, "SPI data transfer error\n");
683 684 685
		return -EIO;
	}

686
	return t->len;
687 688 689 690 691 692 693 694 695

err_desc:
	dma_unmap_single(&spi->dev, t->tx_dma, t->len, DMA_TO_DEVICE);
err_tx_map:
	dma_unmap_single(&spi->dev, t->rx_dma, t->len, DMA_FROM_DEVICE);
err_rx_map:
	kfree(dummy_buf);
err_alloc_dummy_buf:
	return ret;
696 697
}

698 699 700 701 702 703 704 705 706 707 708 709 710
/**
 * dummy_thread_fn - dummy thread function
 * @irq: IRQ number for this SPI Master
 * @context_data: structure for SPI Master controller davinci_spi
 *
 * This is to satisfy the request_threaded_irq() API so that the irq
 * handler is called in interrupt context.
 */
static irqreturn_t dummy_thread_fn(s32 irq, void *data)
{
	return IRQ_HANDLED;
}

711 712 713 714 715 716 717 718 719 720 721
/**
 * davinci_spi_irq - Interrupt handler for SPI Master Controller
 * @irq: IRQ number for this SPI Master
 * @context_data: structure for SPI Master controller davinci_spi
 *
 * ISR will determine that interrupt arrives either for READ or WRITE command.
 * According to command it will do the appropriate action. It will check
 * transfer length and if it is not zero then dispatch transfer command again.
 * If transfer length is zero then it will indicate the COMPLETION so that
 * davinci_spi_bufs function can go ahead.
 */
S
Sekhar Nori 已提交
722
static irqreturn_t davinci_spi_irq(s32 irq, void *data)
723
{
S
Sekhar Nori 已提交
724
	struct davinci_spi *dspi = data;
725 726
	int status;

S
Sekhar Nori 已提交
727
	status = davinci_spi_process_events(dspi);
728
	if (unlikely(status != 0))
S
Sekhar Nori 已提交
729
		clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
730

S
Sekhar Nori 已提交
731 732
	if ((!dspi->rcount && !dspi->wcount) || status)
		complete(&dspi->done);
733 734 735 736

	return IRQ_HANDLED;
}

S
Sekhar Nori 已提交
737
static int davinci_spi_request_dma(struct davinci_spi *dspi)
738
{
739 740
	dma_cap_mask_t mask;
	struct device *sdev = dspi->bitbang.master->dev.parent;
741 742
	int r;

743 744 745 746 747 748 749 750
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

	dspi->dma_rx = dma_request_channel(mask, edma_filter_fn,
					   &dspi->dma_rx_chnum);
	if (!dspi->dma_rx) {
		dev_err(sdev, "request RX DMA channel failed\n");
		r = -ENODEV;
751
		goto rx_dma_failed;
752 753
	}

754 755 756 757 758
	dspi->dma_tx = dma_request_channel(mask, edma_filter_fn,
					   &dspi->dma_tx_chnum);
	if (!dspi->dma_tx) {
		dev_err(sdev, "request TX DMA channel failed\n");
		r = -ENODEV;
759
		goto tx_dma_failed;
760 761 762
	}

	return 0;
763

764
tx_dma_failed:
765
	dma_release_channel(dspi->dma_rx);
766 767
rx_dma_failed:
	return r;
768 769
}

770 771 772
#if defined(CONFIG_OF)
static const struct of_device_id davinci_spi_of_match[] = {
	{
773
		.compatible = "ti,dm6441-spi",
774 775
	},
	{
776
		.compatible = "ti,da830-spi",
777 778 779 780
		.data = (void *)SPI_VERSION_2,
	},
	{ },
};
781
MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

/**
 * spi_davinci_get_pdata - Get platform data from DTS binding
 * @pdev: ptr to platform data
 * @dspi: ptr to driver data
 *
 * Parses and populates pdata in dspi from device tree bindings.
 *
 * NOTE: Not all platform data params are supported currently.
 */
static int spi_davinci_get_pdata(struct platform_device *pdev,
			struct davinci_spi *dspi)
{
	struct device_node *node = pdev->dev.of_node;
	struct davinci_spi_platform_data *pdata;
	unsigned int num_cs, intr_line = 0;
	const struct of_device_id *match;

	pdata = &dspi->pdata;

	pdata->version = SPI_VERSION_1;
803
	match = of_match_device(davinci_spi_of_match, &pdev->dev);
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
	if (!match)
		return -ENODEV;

	/* match data has the SPI version number for SPI_VERSION_2 */
	if (match->data == (void *)SPI_VERSION_2)
		pdata->version = SPI_VERSION_2;

	/*
	 * default num_cs is 1 and all chipsel are internal to the chip
	 * indicated by chip_sel being NULL. GPIO based CS is not
	 * supported yet in DT bindings.
	 */
	num_cs = 1;
	of_property_read_u32(node, "num-cs", &num_cs);
	pdata->num_chipselect = num_cs;
	of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
	pdata->intr_line = intr_line;
	return 0;
}
#else
static struct davinci_spi_platform_data
	*spi_davinci_get_pdata(struct platform_device *pdev,
		struct davinci_spi *dspi)
{
	return -ENODEV;
}
#endif

832 833 834
/**
 * davinci_spi_probe - probe function for SPI Master Controller
 * @pdev: platform_device structure which contains plateform specific data
835 836 837 838 839 840 841
 *
 * According to Linux Device Model this function will be invoked by Linux
 * with platform_device struct which contains the device specific info.
 * This function will map the SPI controller's memory, register IRQ,
 * Reset SPI controller and setting its registers to default value.
 * It will invoke spi_bitbang_start to create work queue so that client driver
 * can register transfer method to work queue.
842
 */
843
static int davinci_spi_probe(struct platform_device *pdev)
844 845
{
	struct spi_master *master;
S
Sekhar Nori 已提交
846
	struct davinci_spi *dspi;
847
	struct davinci_spi_platform_data *pdata;
J
Jingoo Han 已提交
848
	struct resource *r;
849 850 851
	resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
	resource_size_t	dma_tx_chan = SPI_NO_RESOURCE;
	int i = 0, ret = 0;
852
	u32 spipc0;
853 854 855 856 857 858 859

	master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
	if (master == NULL) {
		ret = -ENOMEM;
		goto err;
	}

860
	platform_set_drvdata(pdev, master);
861

S
Sekhar Nori 已提交
862
	dspi = spi_master_get_devdata(master);
863

J
Jingoo Han 已提交
864 865
	if (dev_get_platdata(&pdev->dev)) {
		pdata = dev_get_platdata(&pdev->dev);
866 867 868 869 870 871 872 873 874 875 876
		dspi->pdata = *pdata;
	} else {
		/* update dspi pdata with that from the DT */
		ret = spi_davinci_get_pdata(pdev, dspi);
		if (ret < 0)
			goto free_master;
	}

	/* pdata in dspi is now updated and point pdata to that */
	pdata = &dspi->pdata;

877 878 879 880 881 882 883 884
	dspi->bytes_per_word = devm_kzalloc(&pdev->dev,
					    sizeof(*dspi->bytes_per_word) *
					    pdata->num_chipselect, GFP_KERNEL);
	if (dspi->bytes_per_word == NULL) {
		ret = -ENOMEM;
		goto free_master;
	}

885 886 887 888 889 890
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (r == NULL) {
		ret = -ENOENT;
		goto free_master;
	}

S
Sekhar Nori 已提交
891
	dspi->pbase = r->start;
892

J
Jingoo Han 已提交
893 894 895
	dspi->base = devm_ioremap_resource(&pdev->dev, r);
	if (IS_ERR(dspi->base)) {
		ret = PTR_ERR(dspi->base);
896 897 898
		goto free_master;
	}

S
Sekhar Nori 已提交
899 900
	dspi->irq = platform_get_irq(pdev, 0);
	if (dspi->irq <= 0) {
901
		ret = -EINVAL;
J
Jingoo Han 已提交
902
		goto free_master;
903 904
	}

J
Jingoo Han 已提交
905 906
	ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
				dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
907
	if (ret)
J
Jingoo Han 已提交
908
		goto free_master;
909

910
	dspi->bitbang.master = master;
911

J
Jingoo Han 已提交
912
	dspi->clk = devm_clk_get(&pdev->dev, NULL);
S
Sekhar Nori 已提交
913
	if (IS_ERR(dspi->clk)) {
914
		ret = -ENODEV;
J
Jingoo Han 已提交
915
		goto free_master;
916
	}
917
	clk_prepare_enable(dspi->clk);
918

919
	master->dev.of_node = pdev->dev.of_node;
920 921
	master->bus_num = pdev->id;
	master->num_chipselect = pdata->num_chipselect;
922
	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
923 924
	master->setup = davinci_spi_setup;

S
Sekhar Nori 已提交
925 926
	dspi->bitbang.chipselect = davinci_spi_chipselect;
	dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
927

S
Sekhar Nori 已提交
928
	dspi->version = pdata->version;
929

S
Sekhar Nori 已提交
930 931 932
	dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
	if (dspi->version == SPI_VERSION_2)
		dspi->bitbang.flags |= SPI_READY;
933

934 935 936 937 938 939 940
	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
	if (r)
		dma_rx_chan = r->start;
	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
	if (r)
		dma_tx_chan = r->start;

S
Sekhar Nori 已提交
941
	dspi->bitbang.txrx_bufs = davinci_spi_bufs;
942
	if (dma_rx_chan != SPI_NO_RESOURCE &&
943
	    dma_tx_chan != SPI_NO_RESOURCE) {
944 945
		dspi->dma_rx_chnum = dma_rx_chan;
		dspi->dma_tx_chnum = dma_tx_chan;
946

S
Sekhar Nori 已提交
947
		ret = davinci_spi_request_dma(dspi);
948 949 950
		if (ret)
			goto free_clk;

951
		dev_info(&pdev->dev, "DMA: supported\n");
952 953
		dev_info(&pdev->dev, "DMA: RX channel: %pa, TX channel: %pa, "
				"event queue: %d\n", &dma_rx_chan, &dma_tx_chan,
954
				pdata->dma_event_q);
955 956
	}

S
Sekhar Nori 已提交
957 958
	dspi->get_rx = davinci_spi_rx_buf_u8;
	dspi->get_tx = davinci_spi_tx_buf_u8;
959

S
Sekhar Nori 已提交
960
	init_completion(&dspi->done);
961

962
	/* Reset In/OUT SPI module */
S
Sekhar Nori 已提交
963
	iowrite32(0, dspi->base + SPIGCR0);
964
	udelay(100);
S
Sekhar Nori 已提交
965
	iowrite32(1, dspi->base + SPIGCR0);
966

967
	/* Set up SPIPC0.  CS and ENA init is done in davinci_spi_setup */
968
	spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
S
Sekhar Nori 已提交
969
	iowrite32(spipc0, dspi->base + SPIPC0);
970

971 972 973 974 975 976 977 978
	/* initialize chip selects */
	if (pdata->chip_sel) {
		for (i = 0; i < pdata->num_chipselect; i++) {
			if (pdata->chip_sel[i] != SPI_INTERN_CS)
				gpio_direction_output(pdata->chip_sel[i], 1);
		}
	}

979
	if (pdata->intr_line)
S
Sekhar Nori 已提交
980
		iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
981
	else
S
Sekhar Nori 已提交
982
		iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
983

S
Sekhar Nori 已提交
984
	iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
985

986
	/* master mode default */
S
Sekhar Nori 已提交
987 988 989
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
990

S
Sekhar Nori 已提交
991
	ret = spi_bitbang_start(&dspi->bitbang);
992
	if (ret)
993
		goto free_dma;
994

S
Sekhar Nori 已提交
995
	dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
996 997 998

	return ret;

999
free_dma:
1000 1001
	dma_release_channel(dspi->dma_rx);
	dma_release_channel(dspi->dma_tx);
1002
free_clk:
1003
	clk_disable_unprepare(dspi->clk);
1004
free_master:
1005
	spi_master_put(master);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
err:
	return ret;
}

/**
 * davinci_spi_remove - remove function for SPI Master Controller
 * @pdev: platform_device structure which contains plateform specific data
 *
 * This function will do the reverse action of davinci_spi_probe function
 * It will free the IRQ and SPI controller's memory region.
 * It will also call spi_bitbang_stop to destroy the work queue which was
 * created by spi_bitbang_start.
 */
1019
static int davinci_spi_remove(struct platform_device *pdev)
1020
{
S
Sekhar Nori 已提交
1021
	struct davinci_spi *dspi;
1022 1023
	struct spi_master *master;

1024
	master = platform_get_drvdata(pdev);
S
Sekhar Nori 已提交
1025
	dspi = spi_master_get_devdata(master);
1026

S
Sekhar Nori 已提交
1027
	spi_bitbang_stop(&dspi->bitbang);
1028

1029
	clk_disable_unprepare(dspi->clk);
1030
	spi_master_put(master);
1031 1032 1033 1034 1035

	return 0;
}

static struct platform_driver davinci_spi_driver = {
1036 1037 1038
	.driver = {
		.name = "spi_davinci",
		.owner = THIS_MODULE,
1039
		.of_match_table = of_match_ptr(davinci_spi_of_match),
1040
	},
1041
	.probe = davinci_spi_probe,
1042
	.remove = davinci_spi_remove,
1043
};
1044
module_platform_driver(davinci_spi_driver);
1045 1046 1047

MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
MODULE_LICENSE("GPL");