spi-davinci.c 26.3 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 28 29 30
 *
 * 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>
#include <linux/dma-mapping.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
31
#include <linux/slab.h>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#include <mach/spi.h>
#include <mach/edma.h>

#define SPI_NO_RESOURCE		((resource_size_t)-1)

#define SPI_MAX_CHIPSELECT	2

#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
51
#define SPIFMT_PRESCALE_SHIFT	8
52 53 54 55 56 57 58 59

/* 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
60 61 62
#define SPIINT_MASKINT		0x0000015F
#define SPI_INTLVL_1		0x000001FF
#define SPI_INTLVL_0		0x00000000
63

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

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

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

78 79 80 81 82 83 84 85 86 87
/* 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

88 89 90 91 92 93 94 95
/* 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)
96 97 98 99
#define SPIFLG_ERROR_MASK		(SPIFLG_DLEN_ERR_MASK \
				| SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
				| SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
				| SPIFLG_OVRRUN_MASK)
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#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

/* We have 2 DMA channels per CS, one for RX and one for TX */
struct davinci_spi_dma {
S
Sekhar Nori 已提交
118 119
	int			tx_channel;
	int			rx_channel;
120
	int			dummy_param_slot;
121 122 123 124 125 126 127 128 129 130 131
	enum dma_event_q	eventq;
};

/* 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;
132 133
	u32			irq;
	struct completion	done;
134 135 136

	const void		*tx;
	void			*rx;
137 138
#define SPI_TMP_BUFSZ	(SMP_CACHE_BYTES + 1)
	u8			rx_tmp_buf[SPI_TMP_BUFSZ];
139 140
	int			rcount;
	int			wcount;
S
Sekhar Nori 已提交
141
	struct davinci_spi_dma	dma;
B
Brian Niebuhr 已提交
142
	struct davinci_spi_platform_data *pdata;
143 144 145 146

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

147
	u8			bytes_per_word[SPI_MAX_CHIPSELECT];
148 149
};

150 151
static struct davinci_spi_config davinci_spi_default_cfg;

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

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

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

S
Sekhar Nori 已提交
181
static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
182
{
183
	u32 data = 0;
S
Sekhar Nori 已提交
184 185
	if (dspi->tx) {
		const u16 *tx = dspi->tx;
186
		data = *tx++;
S
Sekhar Nori 已提交
187
		dspi->tx = tx;
188
	}
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	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 已提交
213
	struct davinci_spi *dspi;
214
	struct davinci_spi_platform_data *pdata;
215
	u8 chip_sel = spi->chip_select;
S
Sekhar Nori 已提交
216
	u16 spidat1 = CS_DEFAULT;
217
	bool gpio_chipsel = false;
218

S
Sekhar Nori 已提交
219 220
	dspi = spi_master_get_devdata(spi->master);
	pdata = dspi->pdata;
221

222 223 224 225
	if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
				pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
		gpio_chipsel = true;

226 227 228 229
	/*
	 * Board specific chip select logic decides the polarity and cs
	 * line for the controller
	 */
230 231 232 233 234 235 236
	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 已提交
237 238
			spidat1 |= SPIDAT1_CSHOLD_MASK;
			spidat1 &= ~(0x1 << chip_sel);
239
		}
240

S
Sekhar Nori 已提交
241
		iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
242
	}
243 244
}

245 246 247 248 249 250 251 252 253 254
/**
 * 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 已提交
255
static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
256 257 258 259
							u32 max_speed_hz)
{
	int ret;

S
Sekhar Nori 已提交
260
	ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz);
261 262 263 264 265 266 267

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

	return ret - 1;
}

268 269 270 271 272 273 274 275 276 277 278 279 280
/**
 * 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 已提交
281
	struct davinci_spi *dspi;
282
	struct davinci_spi_config *spicfg;
283
	u8 bits_per_word = 0;
284
	u32 hz = 0, spifmt = 0, prescale = 0;
285

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

	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
	 */
	if (bits_per_word <= 8 && bits_per_word >= 2) {
S
Sekhar Nori 已提交
305 306 307
		dspi->get_rx = davinci_spi_rx_buf_u8;
		dspi->get_tx = davinci_spi_tx_buf_u8;
		dspi->bytes_per_word[spi->chip_select] = 1;
308
	} else if (bits_per_word <= 16 && bits_per_word >= 2) {
S
Sekhar Nori 已提交
309 310 311
		dspi->get_rx = davinci_spi_rx_buf_u16;
		dspi->get_tx = davinci_spi_tx_buf_u16;
		dspi->bytes_per_word[spi->chip_select] = 2;
312 313 314 315 316 317
	} else
		return -EINVAL;

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

318 319
	/* Set up SPIFMTn register, unique to this chipselect. */

S
Sekhar Nori 已提交
320
	prescale = davinci_spi_get_prescale(dspi, hz);
321 322 323
	if (prescale < 0)
		return prescale;

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	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 已提交
348
	if (dspi->version == SPI_VERSION_2) {
349

350 351
		u32 delay = 0;

352 353
		spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
							& SPIFMT_WDELAY_MASK);
354

355 356 357 358 359 360
		if (spicfg->odd_parity)
			spifmt |= SPIFMT_ODD_PARITY_MASK;

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

361
		if (spicfg->timer_disable) {
362
			spifmt |= SPIFMT_DISTIMER_MASK;
363 364 365 366 367 368
		} else {
			delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
						& SPIDELAY_C2TDELAY_MASK;
			delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
						& SPIDELAY_T2CDELAY_MASK;
		}
369

370
		if (spi->mode & SPI_READY) {
371
			spifmt |= SPIFMT_WAITENA_MASK;
372 373 374 375 376 377
			delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
						& SPIDELAY_T2EDELAY_MASK;
			delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
						& SPIDELAY_C2EDELAY_MASK;
		}

S
Sekhar Nori 已提交
378
		iowrite32(delay, dspi->base + SPIDELAY);
379 380
	}

S
Sekhar Nori 已提交
381
	iowrite32(spifmt, dspi->base + SPIFMT0);
382 383 384 385 386 387 388 389 390 391 392 393

	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)
{
394
	int retval = 0;
S
Sekhar Nori 已提交
395
	struct davinci_spi *dspi;
396
	struct davinci_spi_platform_data *pdata;
397

S
Sekhar Nori 已提交
398 399
	dspi = spi_master_get_devdata(spi->master);
	pdata = dspi->pdata;
400 401 402 403 404

	/* if bits per word length is zero then set it default 8 */
	if (!spi->bits_per_word)
		spi->bits_per_word = 8;

405 406 407
	if (!(spi->mode & SPI_NO_CS)) {
		if ((pdata->chip_sel == NULL) ||
		    (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS))
S
Sekhar Nori 已提交
408
			set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
409 410 411 412

	}

	if (spi->mode & SPI_READY)
S
Sekhar Nori 已提交
413
		set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
414 415

	if (spi->mode & SPI_LOOP)
S
Sekhar Nori 已提交
416
		set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
417
	else
S
Sekhar Nori 已提交
418
		clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
419

420 421 422
	return retval;
}

S
Sekhar Nori 已提交
423
static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
424
{
S
Sekhar Nori 已提交
425
	struct device *sdev = dspi->bitbang.master->dev.parent;
426 427 428 429 430 431 432 433 434 435 436 437 438 439

	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 已提交
440
	if (dspi->version == SPI_VERSION_2) {
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
		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;
}

462 463
/**
 * davinci_spi_process_events - check for and handle any SPI controller events
S
Sekhar Nori 已提交
464
 * @dspi: the controller data
465 466 467 468
 *
 * This function will check the SPIFLG register and handle any events that are
 * detected there
 */
S
Sekhar Nori 已提交
469
static int davinci_spi_process_events(struct davinci_spi *dspi)
470
{
S
Sekhar Nori 已提交
471
	u32 buf, status, errors = 0, spidat1;
472

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

S
Sekhar Nori 已提交
475 476 477
	if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
		dspi->get_rx(buf & 0xFFFF, dspi);
		dspi->rcount--;
478 479
	}

S
Sekhar Nori 已提交
480
	status = ioread32(dspi->base + SPIFLG);
481 482 483 484 485 486

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

S
Sekhar Nori 已提交
487 488 489 490 491 492
	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);
493 494 495 496 497 498
	}

out:
	return errors;
}

499 500
static void davinci_spi_dma_callback(unsigned lch, u16 status, void *data)
{
S
Sekhar Nori 已提交
501 502
	struct davinci_spi *dspi = data;
	struct davinci_spi_dma *dma = &dspi->dma;
503 504 505 506

	edma_stop(lch);

	if (status == DMA_COMPLETE) {
S
Sekhar Nori 已提交
507 508 509 510
		if (lch == dma->rx_channel)
			dspi->rcount = 0;
		if (lch == dma->tx_channel)
			dspi->wcount = 0;
511 512
	}

S
Sekhar Nori 已提交
513 514
	if ((!dspi->wcount && !dspi->rcount) || (status != DMA_COMPLETE))
		complete(&dspi->done);
515 516
}

517 518 519 520 521 522 523 524 525
/**
 * 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.
 */
526
static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
527
{
S
Sekhar Nori 已提交
528
	struct davinci_spi *dspi;
529
	int data_type, ret;
S
Sekhar Nori 已提交
530
	u32 tx_data, spidat1;
531
	u32 errors = 0;
532
	struct davinci_spi_config *spicfg;
533
	struct davinci_spi_platform_data *pdata;
534 535
	unsigned uninitialized_var(rx_buf_count);
	struct device *sdev;
536

S
Sekhar Nori 已提交
537 538
	dspi = spi_master_get_devdata(spi->master);
	pdata = dspi->pdata;
539 540 541
	spicfg = (struct davinci_spi_config *)spi->controller_data;
	if (!spicfg)
		spicfg = &davinci_spi_default_cfg;
S
Sekhar Nori 已提交
542
	sdev = dspi->bitbang.master->dev.parent;
543 544

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

S
Sekhar Nori 已提交
547 548 549 550
	dspi->tx = t->tx_buf;
	dspi->rx = t->rx_buf;
	dspi->wcount = t->len / data_type;
	dspi->rcount = dspi->wcount;
551

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

S
Sekhar Nori 已提交
554 555
	clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
556

S
Sekhar Nori 已提交
557
	INIT_COMPLETION(dspi->done);
558 559

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

562 563
	if (spicfg->io_type != SPI_IO_TYPE_DMA) {
		/* start the transfer */
S
Sekhar Nori 已提交
564 565 566 567 568
		dspi->wcount--;
		tx_data = dspi->get_tx(dspi);
		spidat1 &= 0xFFFF0000;
		spidat1 |= tx_data & 0xFFFF;
		iowrite32(spidat1, dspi->base + SPIDAT1);
569
	} else {
S
Sekhar Nori 已提交
570
		struct davinci_spi_dma *dma;
571 572 573
		unsigned long tx_reg, rx_reg;
		struct edmacc_param param;
		void *rx_buf;
574
		int b, c;
575

S
Sekhar Nori 已提交
576
		dma = &dspi->dma;
577

S
Sekhar Nori 已提交
578 579
		tx_reg = (unsigned long)dspi->pbase + SPIDAT1;
		rx_reg = (unsigned long)dspi->pbase + SPIBUF;
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

		/*
		 * Transmit DMA setup
		 *
		 * If there is transmit data, map the transmit buffer, set it
		 * as the source of data and set the source B index to data
		 * size. If there is no transmit data, set the transmit register
		 * as the source of data, and set the source B index to zero.
		 *
		 * The destination is always the transmit register itself. And
		 * the destination never increments.
		 */

		if (t->tx_buf) {
			t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf,
595
						t->len, DMA_TO_DEVICE);
596 597
			if (dma_mapping_error(&spi->dev, t->tx_dma)) {
				dev_dbg(sdev, "Unable to DMA map %d bytes"
598
						"TX buffer\n", t->len);
599 600 601 602
				return -ENOMEM;
			}
		}

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		/*
		 * If number of words is greater than 65535, then we need
		 * to configure a 3 dimension transfer.  Use the BCNTRLD
		 * feature to allow for transfers that aren't even multiples
		 * of 65535 (or any other possible b size) by first transferring
		 * the remainder amount then grabbing the next N blocks of
		 * 65535 words.
		 */

		c = dspi->wcount / (SZ_64K - 1);	/* N 65535 Blocks */
		b = dspi->wcount - c * (SZ_64K - 1);	/* Remainder */
		if (b)
			c++;
		else
			b = SZ_64K - 1;

S
Sekhar Nori 已提交
619
		param.opt = TCINTEN | EDMA_TCC(dma->tx_channel);
620
		param.src = t->tx_buf ? t->tx_dma : tx_reg;
621
		param.a_b_cnt = b << 16 | data_type;
622 623
		param.dst = tx_reg;
		param.src_dst_bidx = t->tx_buf ? data_type : 0;
624 625 626
		param.link_bcntrld = 0xffffffff;
		param.src_dst_cidx = t->tx_buf ? data_type : 0;
		param.ccnt = c;
S
Sekhar Nori 已提交
627 628
		edma_write_slot(dma->tx_channel, &param);
		edma_link(dma->tx_channel, dma->dummy_param_slot);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

		/*
		 * Receive DMA setup
		 *
		 * If there is receive buffer, use it to receive data. If there
		 * is none provided, use a temporary receive buffer. Set the
		 * destination B index to 0 so effectively only one byte is used
		 * in the temporary buffer (address does not increment).
		 *
		 * The source of receive data is the receive data register. The
		 * source address never increments.
		 */

		if (t->rx_buf) {
			rx_buf = t->rx_buf;
644
			rx_buf_count = t->len;
645
		} else {
S
Sekhar Nori 已提交
646 647
			rx_buf = dspi->rx_tmp_buf;
			rx_buf_count = sizeof(dspi->rx_tmp_buf);
648 649 650 651 652 653 654 655
		}

		t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count,
							DMA_FROM_DEVICE);
		if (dma_mapping_error(&spi->dev, t->rx_dma)) {
			dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
								rx_buf_count);
			if (t->tx_buf)
656
				dma_unmap_single(&spi->dev, t->tx_dma, t->len,
S
Sekhar Nori 已提交
657
								DMA_TO_DEVICE);
658 659 660
			return -ENOMEM;
		}

S
Sekhar Nori 已提交
661
		param.opt = TCINTEN | EDMA_TCC(dma->rx_channel);
662
		param.src = rx_reg;
663
		param.a_b_cnt = b << 16 | data_type;
664 665
		param.dst = t->rx_dma;
		param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16;
666 667 668
		param.link_bcntrld = 0xffffffff;
		param.src_dst_cidx = (t->rx_buf ? data_type : 0) << 16;
		param.ccnt = c;
S
Sekhar Nori 已提交
669
		edma_write_slot(dma->rx_channel, &param);
670 671

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

S
Sekhar Nori 已提交
674 675 676
		edma_start(dma->rx_channel);
		edma_start(dma->tx_channel);
		set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
677
	}
678

679
	/* Wait for the transfer to complete */
680
	if (spicfg->io_type != SPI_IO_TYPE_POLL) {
S
Sekhar Nori 已提交
681
		wait_for_completion_interruptible(&(dspi->done));
682
	} else {
S
Sekhar Nori 已提交
683 684
		while (dspi->rcount > 0 || dspi->wcount > 0) {
			errors = davinci_spi_process_events(dspi);
685 686 687
			if (errors)
				break;
			cpu_relax();
688 689 690
		}
	}

S
Sekhar Nori 已提交
691
	clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
692 693 694
	if (spicfg->io_type == SPI_IO_TYPE_DMA) {

		if (t->tx_buf)
695
			dma_unmap_single(&spi->dev, t->tx_dma, t->len,
696 697
								DMA_TO_DEVICE);

698
		dma_unmap_single(&spi->dev, t->rx_dma, rx_buf_count,
699 700
							DMA_FROM_DEVICE);

S
Sekhar Nori 已提交
701
		clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
702
	}
703

S
Sekhar Nori 已提交
704 705
	clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
	set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
706

707 708 709 710
	/*
	 * Check for bit error, desync error,parity error,timeout error and
	 * receive overflow errors
	 */
711
	if (errors) {
S
Sekhar Nori 已提交
712
		ret = davinci_spi_check_error(dspi, errors);
713 714
		WARN(!ret, "%s: error reported but no error found!\n",
							dev_name(&spi->dev));
715
		return ret;
716
	}
717

S
Sekhar Nori 已提交
718
	if (dspi->rcount != 0 || dspi->wcount != 0) {
719 720 721 722
		dev_err(sdev, "SPI data transfer error\n");
		return -EIO;
	}

723 724 725
	return t->len;
}

726 727 728 729 730 731 732 733 734 735 736
/**
 * 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 已提交
737
static irqreturn_t davinci_spi_irq(s32 irq, void *data)
738
{
S
Sekhar Nori 已提交
739
	struct davinci_spi *dspi = data;
740 741
	int status;

S
Sekhar Nori 已提交
742
	status = davinci_spi_process_events(dspi);
743
	if (unlikely(status != 0))
S
Sekhar Nori 已提交
744
		clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
745

S
Sekhar Nori 已提交
746 747
	if ((!dspi->rcount && !dspi->wcount) || status)
		complete(&dspi->done);
748 749 750 751

	return IRQ_HANDLED;
}

S
Sekhar Nori 已提交
752
static int davinci_spi_request_dma(struct davinci_spi *dspi)
753 754
{
	int r;
S
Sekhar Nori 已提交
755
	struct davinci_spi_dma *dma = &dspi->dma;
756

S
Sekhar Nori 已提交
757 758
	r = edma_alloc_channel(dma->rx_channel, davinci_spi_dma_callback, dspi,
								dma->eventq);
759 760
	if (r < 0) {
		pr_err("Unable to request DMA channel for SPI RX\n");
761 762
		r = -EAGAIN;
		goto rx_dma_failed;
763 764
	}

S
Sekhar Nori 已提交
765 766
	r = edma_alloc_channel(dma->tx_channel, davinci_spi_dma_callback, dspi,
								dma->eventq);
767 768
	if (r < 0) {
		pr_err("Unable to request DMA channel for SPI TX\n");
769 770
		r = -EAGAIN;
		goto tx_dma_failed;
771 772
	}

S
Sekhar Nori 已提交
773
	r = edma_alloc_slot(EDMA_CTLR(dma->tx_channel), EDMA_SLOT_ANY);
774 775 776 777 778
	if (r < 0) {
		pr_err("Unable to request SPI TX DMA param slot\n");
		r = -EAGAIN;
		goto param_failed;
	}
S
Sekhar Nori 已提交
779 780
	dma->dummy_param_slot = r;
	edma_link(dma->dummy_param_slot, dma->dummy_param_slot);
781

782
	return 0;
783
param_failed:
S
Sekhar Nori 已提交
784
	edma_free_channel(dma->tx_channel);
785
tx_dma_failed:
S
Sekhar Nori 已提交
786
	edma_free_channel(dma->rx_channel);
787 788
rx_dma_failed:
	return r;
789 790
}

791 792 793
/**
 * davinci_spi_probe - probe function for SPI Master Controller
 * @pdev: platform_device structure which contains plateform specific data
794 795 796 797 798 799 800
 *
 * 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.
801
 */
802
static int __devinit davinci_spi_probe(struct platform_device *pdev)
803 804
{
	struct spi_master *master;
S
Sekhar Nori 已提交
805
	struct davinci_spi *dspi;
806 807 808 809 810
	struct davinci_spi_platform_data *pdata;
	struct resource *r, *mem;
	resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
	resource_size_t	dma_tx_chan = SPI_NO_RESOURCE;
	int i = 0, ret = 0;
811
	u32 spipc0;
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

	pdata = pdev->dev.platform_data;
	if (pdata == NULL) {
		ret = -ENODEV;
		goto err;
	}

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

	dev_set_drvdata(&pdev->dev, master);

S
Sekhar Nori 已提交
827 828
	dspi = spi_master_get_devdata(master);
	if (dspi == NULL) {
829 830 831 832 833 834 835 836 837 838
		ret = -ENOENT;
		goto free_master;
	}

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (r == NULL) {
		ret = -ENOENT;
		goto free_master;
	}

S
Sekhar Nori 已提交
839 840
	dspi->pbase = r->start;
	dspi->pdata = pdata;
841

842
	mem = request_mem_region(r->start, resource_size(r), pdev->name);
843 844 845 846 847
	if (mem == NULL) {
		ret = -EBUSY;
		goto free_master;
	}

S
Sekhar Nori 已提交
848 849
	dspi->base = ioremap(r->start, resource_size(r));
	if (dspi->base == NULL) {
850 851 852 853
		ret = -ENOMEM;
		goto release_region;
	}

S
Sekhar Nori 已提交
854 855
	dspi->irq = platform_get_irq(pdev, 0);
	if (dspi->irq <= 0) {
856 857 858 859
		ret = -EINVAL;
		goto unmap_io;
	}

S
Sekhar Nori 已提交
860 861
	ret = request_irq(dspi->irq, davinci_spi_irq, 0, dev_name(&pdev->dev),
									dspi);
862 863 864
	if (ret)
		goto unmap_io;

S
Sekhar Nori 已提交
865 866
	dspi->bitbang.master = spi_master_get(master);
	if (dspi->bitbang.master == NULL) {
867
		ret = -ENODEV;
868
		goto irq_free;
869 870
	}

S
Sekhar Nori 已提交
871 872
	dspi->clk = clk_get(&pdev->dev, NULL);
	if (IS_ERR(dspi->clk)) {
873 874 875
		ret = -ENODEV;
		goto put_master;
	}
S
Sekhar Nori 已提交
876
	clk_enable(dspi->clk);
877 878 879 880 881

	master->bus_num = pdev->id;
	master->num_chipselect = pdata->num_chipselect;
	master->setup = davinci_spi_setup;

S
Sekhar Nori 已提交
882 883
	dspi->bitbang.chipselect = davinci_spi_chipselect;
	dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
884

S
Sekhar Nori 已提交
885
	dspi->version = pdata->version;
886

S
Sekhar Nori 已提交
887 888 889
	dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
	if (dspi->version == SPI_VERSION_2)
		dspi->bitbang.flags |= SPI_READY;
890

891 892 893 894 895 896 897
	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 已提交
898
	dspi->bitbang.txrx_bufs = davinci_spi_bufs;
899
	if (dma_rx_chan != SPI_NO_RESOURCE &&
900
	    dma_tx_chan != SPI_NO_RESOURCE) {
S
Sekhar Nori 已提交
901 902
		dspi->dma.rx_channel = dma_rx_chan;
		dspi->dma.tx_channel = dma_tx_chan;
903
		dspi->dma.eventq = pdata->dma_event_q;
904

S
Sekhar Nori 已提交
905
		ret = davinci_spi_request_dma(dspi);
906 907 908
		if (ret)
			goto free_clk;

909 910 911
		dev_info(&pdev->dev, "DMA: supported\n");
		dev_info(&pdev->dev, "DMA: RX channel: %d, TX channel: %d, "
				"event queue: %d\n", dma_rx_chan, dma_tx_chan,
912
				pdata->dma_event_q);
913 914
	}

S
Sekhar Nori 已提交
915 916
	dspi->get_rx = davinci_spi_rx_buf_u8;
	dspi->get_tx = davinci_spi_tx_buf_u8;
917

S
Sekhar Nori 已提交
918
	init_completion(&dspi->done);
919

920
	/* Reset In/OUT SPI module */
S
Sekhar Nori 已提交
921
	iowrite32(0, dspi->base + SPIGCR0);
922
	udelay(100);
S
Sekhar Nori 已提交
923
	iowrite32(1, dspi->base + SPIGCR0);
924

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

929 930 931 932 933 934 935 936
	/* 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);
		}
	}

937
	if (pdata->intr_line)
S
Sekhar Nori 已提交
938
		iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
939
	else
S
Sekhar Nori 已提交
940
		iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
941

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

944
	/* master mode default */
S
Sekhar Nori 已提交
945 946 947
	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);
948

S
Sekhar Nori 已提交
949
	ret = spi_bitbang_start(&dspi->bitbang);
950
	if (ret)
951
		goto free_dma;
952

S
Sekhar Nori 已提交
953
	dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
954 955 956

	return ret;

957
free_dma:
S
Sekhar Nori 已提交
958 959 960
	edma_free_channel(dspi->dma.tx_channel);
	edma_free_channel(dspi->dma.rx_channel);
	edma_free_slot(dspi->dma.dummy_param_slot);
961
free_clk:
S
Sekhar Nori 已提交
962 963
	clk_disable(dspi->clk);
	clk_put(dspi->clk);
964 965
put_master:
	spi_master_put(master);
966
irq_free:
S
Sekhar Nori 已提交
967
	free_irq(dspi->irq, dspi);
968
unmap_io:
S
Sekhar Nori 已提交
969
	iounmap(dspi->base);
970
release_region:
S
Sekhar Nori 已提交
971
	release_mem_region(dspi->pbase, resource_size(r));
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
free_master:
	kfree(master);
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.
 */
987
static int __devexit davinci_spi_remove(struct platform_device *pdev)
988
{
S
Sekhar Nori 已提交
989
	struct davinci_spi *dspi;
990
	struct spi_master *master;
991
	struct resource *r;
992 993

	master = dev_get_drvdata(&pdev->dev);
S
Sekhar Nori 已提交
994
	dspi = spi_master_get_devdata(master);
995

S
Sekhar Nori 已提交
996
	spi_bitbang_stop(&dspi->bitbang);
997

S
Sekhar Nori 已提交
998 999
	clk_disable(dspi->clk);
	clk_put(dspi->clk);
1000
	spi_master_put(master);
S
Sekhar Nori 已提交
1001 1002
	free_irq(dspi->irq, dspi);
	iounmap(dspi->base);
1003
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
S
Sekhar Nori 已提交
1004
	release_mem_region(dspi->pbase, resource_size(r));
1005 1006 1007 1008 1009

	return 0;
}

static struct platform_driver davinci_spi_driver = {
1010 1011 1012 1013
	.driver = {
		.name = "spi_davinci",
		.owner = THIS_MODULE,
	},
1014 1015
	.probe = davinci_spi_probe,
	.remove = __devexit_p(davinci_spi_remove),
1016
};
1017
module_platform_driver(davinci_spi_driver);
1018 1019 1020

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