spi-pxa2xx.c 41.4 KB
Newer Older
1 2
/*
 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3
 * Copyright (C) 2013, Intel Corporation
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/ioport.h>
#include <linux/errno.h>
21
#include <linux/err.h>
22
#include <linux/interrupt.h>
23
#include <linux/kernel.h>
24
#include <linux/platform_device.h>
25
#include <linux/spi/pxa2xx_spi.h>
26 27
#include <linux/spi/spi.h>
#include <linux/delay.h>
28
#include <linux/gpio.h>
29
#include <linux/slab.h>
30
#include <linux/clk.h>
31
#include <linux/pm_runtime.h>
32
#include <linux/acpi.h>
33

34
#include "spi-pxa2xx.h"
35 36

MODULE_AUTHOR("Stephen Street");
W
Will Newton 已提交
37
MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
38
MODULE_LICENSE("GPL");
39
MODULE_ALIAS("platform:pxa2xx-spi");
40

41 42
#define TIMOUT_DFLT		1000

43 44 45 46 47 48 49 50
/*
 * for testing SSCR1 changes that require SSP restart, basically
 * everything except the service and interrupt enables, the pxa270 developer
 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
 * list, but the PXA255 dev man says all bits without really meaning the
 * service and interrupt enables
 */
#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
51
				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
52 53 54 55
				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
56

57 58 59 60 61 62
#define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF	\
				| QUARK_X1000_SSCR1_EFWR	\
				| QUARK_X1000_SSCR1_RFT		\
				| QUARK_X1000_SSCR1_TFT		\
				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)

63 64 65 66 67
#define LPSS_RX_THRESH_DFLT	64
#define LPSS_TX_LOTHRESH_DFLT	160
#define LPSS_TX_HITHRESH_DFLT	224

/* Offset from drv_data->lpss_base */
68 69
#define GENERAL_REG		0x08
#define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
70
#define SSP_REG			0x0c
71 72 73 74 75 76
#define SPI_CS_CONTROL		0x18
#define SPI_CS_CONTROL_SW_MODE	BIT(0)
#define SPI_CS_CONTROL_CS_HIGH	BIT(1)

static bool is_lpss_ssp(const struct driver_data *drv_data)
{
77 78 79 80 81 82 83
	switch (drv_data->ssp_type) {
	case LPSS_LPT_SSP:
	case LPSS_BYT_SSP:
		return true;
	default:
		return false;
	}
84 85
}

86 87 88 89 90
static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
{
	return drv_data->ssp_type == QUARK_X1000_SSP;
}

91 92 93
static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
{
	switch (drv_data->ssp_type) {
94 95
	case QUARK_X1000_SSP:
		return QUARK_X1000_SSCR1_CHANGE_MASK;
96 97 98 99 100 101 102 103 104
	default:
		return SSCR1_CHANGE_MASK;
	}
}

static u32
pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
{
	switch (drv_data->ssp_type) {
105 106
	case QUARK_X1000_SSP:
		return RX_THRESH_QUARK_X1000_DFLT;
107 108 109 110 111 112 113 114 115 116
	default:
		return RX_THRESH_DFLT;
	}
}

static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
{
	u32 mask;

	switch (drv_data->ssp_type) {
117 118 119
	case QUARK_X1000_SSP:
		mask = QUARK_X1000_SSSR_TFL_MASK;
		break;
120 121 122 123 124
	default:
		mask = SSSR_TFL_MASK;
		break;
	}

125
	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
126 127 128 129 130 131 132 133
}

static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
				     u32 *sccr1_reg)
{
	u32 mask;

	switch (drv_data->ssp_type) {
134 135 136
	case QUARK_X1000_SSP:
		mask = QUARK_X1000_SSCR1_RFT;
		break;
137 138 139 140 141 142 143 144 145 146 147
	default:
		mask = SSCR1_RFT;
		break;
	}
	*sccr1_reg &= ~mask;
}

static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
				   u32 *sccr1_reg, u32 threshold)
{
	switch (drv_data->ssp_type) {
148 149 150
	case QUARK_X1000_SSP:
		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
		break;
151 152 153 154 155 156 157 158 159 160
	default:
		*sccr1_reg |= SSCR1_RxTresh(threshold);
		break;
	}
}

static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
				  u32 clk_div, u8 bits)
{
	switch (drv_data->ssp_type) {
161 162 163 164 165
	case QUARK_X1000_SSP:
		return clk_div
			| QUARK_X1000_SSCR0_Motorola
			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
			| SSCR0_SSE;
166 167 168 169 170 171 172 173 174
	default:
		return clk_div
			| SSCR0_Motorola
			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
			| SSCR0_SSE
			| (bits > 16 ? SSCR0_EDSS : 0);
	}
}

175 176 177 178 179 180 181 182 183 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 209
/*
 * Read and write LPSS SSP private registers. Caller must first check that
 * is_lpss_ssp() returns true before these can be called.
 */
static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
{
	WARN_ON(!drv_data->lpss_base);
	return readl(drv_data->lpss_base + offset);
}

static void __lpss_ssp_write_priv(struct driver_data *drv_data,
				  unsigned offset, u32 value)
{
	WARN_ON(!drv_data->lpss_base);
	writel(value, drv_data->lpss_base + offset);
}

/*
 * lpss_ssp_setup - perform LPSS SSP specific setup
 * @drv_data: pointer to the driver private data
 *
 * Perform LPSS SSP specific setup. This function must be called first if
 * one is going to use LPSS SSP private registers.
 */
static void lpss_ssp_setup(struct driver_data *drv_data)
{
	unsigned offset = 0x400;
	u32 value, orig;

	/*
	 * Perform auto-detection of the LPSS SSP private registers. They
	 * can be either at 1k or 2k offset from the base address.
	 */
	orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);

210
	/* Test SPI_CS_CONTROL_SW_MODE bit enabling */
211 212 213 214 215 216 217 218
	value = orig | SPI_CS_CONTROL_SW_MODE;
	writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
	value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
	if (value != (orig | SPI_CS_CONTROL_SW_MODE)) {
		offset = 0x800;
		goto detection_done;
	}

219 220 221 222
	orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);

	/* Test SPI_CS_CONTROL_SW_MODE bit disabling */
	value = orig & ~SPI_CS_CONTROL_SW_MODE;
223 224
	writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
	value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
225
	if (value != (orig & ~SPI_CS_CONTROL_SW_MODE)) {
226 227 228 229 230 231 232 233 234 235 236
		offset = 0x800;
		goto detection_done;
	}

detection_done:
	/* Now set the LPSS base */
	drv_data->lpss_base = drv_data->ioaddr + offset;

	/* Enable software chip select control */
	value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
	__lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
237 238

	/* Enable multiblock DMA transfers */
239
	if (drv_data->master_info->enable_dma) {
240
		__lpss_ssp_write_priv(drv_data, SSP_REG, 1);
241 242 243 244 245

		value = __lpss_ssp_read_priv(drv_data, GENERAL_REG);
		value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
		__lpss_ssp_write_priv(drv_data, GENERAL_REG, value);
	}
246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
{
	u32 value;

	value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL);
	if (enable)
		value &= ~SPI_CS_CONTROL_CS_HIGH;
	else
		value |= SPI_CS_CONTROL_CS_HIGH;
	__lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
}

260 261 262 263
static void cs_assert(struct driver_data *drv_data)
{
	struct chip_data *chip = drv_data->cur_chip;

264
	if (drv_data->ssp_type == CE4100_SSP) {
265
		pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
266 267 268
		return;
	}

269 270 271 272 273
	if (chip->cs_control) {
		chip->cs_control(PXA2XX_CS_ASSERT);
		return;
	}

274
	if (gpio_is_valid(chip->gpio_cs)) {
275
		gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
276 277 278
		return;
	}

279 280
	if (is_lpss_ssp(drv_data))
		lpss_ssp_cs_control(drv_data, true);
281 282 283 284 285 286
}

static void cs_deassert(struct driver_data *drv_data)
{
	struct chip_data *chip = drv_data->cur_chip;

287 288 289
	if (drv_data->ssp_type == CE4100_SSP)
		return;

290
	if (chip->cs_control) {
291
		chip->cs_control(PXA2XX_CS_DEASSERT);
292 293 294
		return;
	}

295
	if (gpio_is_valid(chip->gpio_cs)) {
296
		gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
297 298 299
		return;
	}

300 301
	if (is_lpss_ssp(drv_data))
		lpss_ssp_cs_control(drv_data, false);
302 303
}

304
int pxa2xx_spi_flush(struct driver_data *drv_data)
305 306 307 308
{
	unsigned long limit = loops_per_jiffy << 1;

	do {
309 310 311
		while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
			pxa2xx_spi_read(drv_data, SSDR);
	} while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
312
	write_SSSR_CS(drv_data, SSSR_ROR);
313 314 315 316

	return limit;
}

317
static int null_writer(struct driver_data *drv_data)
318
{
319
	u8 n_bytes = drv_data->n_bytes;
320

321
	if (pxa2xx_spi_txfifo_full(drv_data)
322 323 324
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

325
	pxa2xx_spi_write(drv_data, SSDR, 0);
326 327 328
	drv_data->tx += n_bytes;

	return 1;
329 330
}

331
static int null_reader(struct driver_data *drv_data)
332
{
333
	u8 n_bytes = drv_data->n_bytes;
334

335 336 337
	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
	       && (drv_data->rx < drv_data->rx_end)) {
		pxa2xx_spi_read(drv_data, SSDR);
338 339
		drv_data->rx += n_bytes;
	}
340 341

	return drv_data->rx == drv_data->rx_end;
342 343
}

344
static int u8_writer(struct driver_data *drv_data)
345
{
346
	if (pxa2xx_spi_txfifo_full(drv_data)
347 348 349
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

350
	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
351 352 353
	++drv_data->tx;

	return 1;
354 355
}

356
static int u8_reader(struct driver_data *drv_data)
357
{
358 359 360
	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
	       && (drv_data->rx < drv_data->rx_end)) {
		*(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
361 362
		++drv_data->rx;
	}
363 364

	return drv_data->rx == drv_data->rx_end;
365 366
}

367
static int u16_writer(struct driver_data *drv_data)
368
{
369
	if (pxa2xx_spi_txfifo_full(drv_data)
370 371 372
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

373
	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
374 375 376
	drv_data->tx += 2;

	return 1;
377 378
}

379
static int u16_reader(struct driver_data *drv_data)
380
{
381 382 383
	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
	       && (drv_data->rx < drv_data->rx_end)) {
		*(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
384 385
		drv_data->rx += 2;
	}
386 387

	return drv_data->rx == drv_data->rx_end;
388
}
389 390

static int u32_writer(struct driver_data *drv_data)
391
{
392
	if (pxa2xx_spi_txfifo_full(drv_data)
393 394 395
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

396
	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
397 398 399
	drv_data->tx += 4;

	return 1;
400 401
}

402
static int u32_reader(struct driver_data *drv_data)
403
{
404 405 406
	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
	       && (drv_data->rx < drv_data->rx_end)) {
		*(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
407 408
		drv_data->rx += 4;
	}
409 410

	return drv_data->rx == drv_data->rx_end;
411 412
}

413
void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
{
	struct spi_message *msg = drv_data->cur_msg;
	struct spi_transfer *trans = drv_data->cur_transfer;

	/* Move to next transfer */
	if (trans->transfer_list.next != &msg->transfers) {
		drv_data->cur_transfer =
			list_entry(trans->transfer_list.next,
					struct spi_transfer,
					transfer_list);
		return RUNNING_STATE;
	} else
		return DONE_STATE;
}

/* caller already set message->status; dma and pio irqs are blocked */
S
Stephen Street 已提交
430
static void giveback(struct driver_data *drv_data)
431 432
{
	struct spi_transfer* last_transfer;
S
Stephen Street 已提交
433
	struct spi_message *msg;
434

S
Stephen Street 已提交
435 436 437 438
	msg = drv_data->cur_msg;
	drv_data->cur_msg = NULL;
	drv_data->cur_transfer = NULL;

439
	last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
440 441
					transfer_list);

N
Ned Forrester 已提交
442 443 444 445 446 447 448
	/* Delay if requested before any change in chip select */
	if (last_transfer->delay_usecs)
		udelay(last_transfer->delay_usecs);

	/* Drop chip select UNLESS cs_change is true or we are returning
	 * a message with an error, or next message is for another chip
	 */
449
	if (!last_transfer->cs_change)
450
		cs_deassert(drv_data);
N
Ned Forrester 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464
	else {
		struct spi_message *next_msg;

		/* Holding of cs was hinted, but we need to make sure
		 * the next message is for the same chip.  Don't waste
		 * time with the following tests unless this was hinted.
		 *
		 * We cannot postpone this until pump_messages, because
		 * after calling msg->complete (below) the driver that
		 * sent the current message could be unloaded, which
		 * could invalidate the cs_control() callback...
		 */

		/* get a pointer to the next message, if any */
465
		next_msg = spi_get_next_queued_message(drv_data->master);
N
Ned Forrester 已提交
466 467 468 469 470 471 472

		/* see if the next and current messages point
		 * to the same chip
		 */
		if (next_msg && next_msg->spi != msg->spi)
			next_msg = NULL;
		if (!next_msg || msg->state == ERROR_STATE)
473
			cs_deassert(drv_data);
N
Ned Forrester 已提交
474
	}
475

476
	drv_data->cur_chip = NULL;
477
	spi_finalize_current_message(drv_data->master);
478 479
}

480 481 482 483 484
static void reset_sccr1(struct driver_data *drv_data)
{
	struct chip_data *chip = drv_data->cur_chip;
	u32 sccr1_reg;

485
	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
486 487
	sccr1_reg &= ~SSCR1_RFT;
	sccr1_reg |= chip->threshold;
488
	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
489 490
}

491
static void int_error_stop(struct driver_data *drv_data, const char* msg)
492
{
493
	/* Stop and reset SSP */
494
	write_SSSR_CS(drv_data, drv_data->clear_sr);
495
	reset_sccr1(drv_data);
496
	if (!pxa25x_ssp_comp(drv_data))
497
		pxa2xx_spi_write(drv_data, SSTO, 0);
498
	pxa2xx_spi_flush(drv_data);
499 500
	pxa2xx_spi_write(drv_data, SSCR0,
			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
501

502
	dev_err(&drv_data->pdev->dev, "%s\n", msg);
503

504 505 506
	drv_data->cur_msg->state = ERROR_STATE;
	tasklet_schedule(&drv_data->pump_transfers);
}
S
Stephen Street 已提交
507

508 509 510
static void int_transfer_complete(struct driver_data *drv_data)
{
	/* Stop SSP */
511
	write_SSSR_CS(drv_data, drv_data->clear_sr);
512
	reset_sccr1(drv_data);
513
	if (!pxa25x_ssp_comp(drv_data))
514
		pxa2xx_spi_write(drv_data, SSTO, 0);
515

L
Lucas De Marchi 已提交
516
	/* Update total byte transferred return count actual bytes read */
517 518
	drv_data->cur_msg->actual_length += drv_data->len -
				(drv_data->rx_end - drv_data->rx);
519

N
Ned Forrester 已提交
520 521 522
	/* Transfer delays and chip select release are
	 * handled in pump_transfers or giveback
	 */
523

524
	/* Move to next transfer */
525
	drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
526

527 528 529
	/* Schedule transfer tasklet */
	tasklet_schedule(&drv_data->pump_transfers);
}
530

531 532
static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
{
533 534
	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
535

536
	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
537

538 539 540 541
	if (irq_status & SSSR_ROR) {
		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
		return IRQ_HANDLED;
	}
542

543
	if (irq_status & SSSR_TINT) {
544
		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
545 546 547 548 549
		if (drv_data->read(drv_data)) {
			int_transfer_complete(drv_data);
			return IRQ_HANDLED;
		}
	}
550

551 552 553 554 555 556 557
	/* Drain rx fifo, Fill tx fifo and prevent overruns */
	do {
		if (drv_data->read(drv_data)) {
			int_transfer_complete(drv_data);
			return IRQ_HANDLED;
		}
	} while (drv_data->write(drv_data));
558

559 560 561 562
	if (drv_data->read(drv_data)) {
		int_transfer_complete(drv_data);
		return IRQ_HANDLED;
	}
563

564
	if (drv_data->tx == drv_data->tx_end) {
565 566 567
		u32 bytes_left;
		u32 sccr1_reg;

568
		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
569 570 571 572
		sccr1_reg &= ~SSCR1_TIE;

		/*
		 * PXA25x_SSP has no timeout, set up rx threshould for the
L
Lucas De Marchi 已提交
573
		 * remaining RX bytes.
574
		 */
575
		if (pxa25x_ssp_comp(drv_data)) {
576
			u32 rx_thre;
577

578
			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
579 580 581 582 583 584 585

			bytes_left = drv_data->rx_end - drv_data->rx;
			switch (drv_data->n_bytes) {
			case 4:
				bytes_left >>= 1;
			case 2:
				bytes_left >>= 1;
586
			}
587

588 589 590
			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
			if (rx_thre > bytes_left)
				rx_thre = bytes_left;
591

592
			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
593
		}
594
		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
595 596
	}

S
Stephen Street 已提交
597 598
	/* We did something */
	return IRQ_HANDLED;
599 600
}

601
static irqreturn_t ssp_int(int irq, void *dev_id)
602
{
603
	struct driver_data *drv_data = dev_id;
604
	u32 sccr1_reg;
605 606 607
	u32 mask = drv_data->mask_sr;
	u32 status;

608 609 610 611 612 613 614 615 616
	/*
	 * The IRQ might be shared with other peripherals so we must first
	 * check that are we RPM suspended or not. If we are we assume that
	 * the IRQ was not for us (we shouldn't be RPM suspended when the
	 * interrupt is enabled).
	 */
	if (pm_runtime_suspended(&drv_data->pdev->dev))
		return IRQ_NONE;

617 618 619 620 621 622
	/*
	 * If the device is not yet in RPM suspended state and we get an
	 * interrupt that is meant for another device, check if status bits
	 * are all set to one. That means that the device is already
	 * powered off.
	 */
623
	status = pxa2xx_spi_read(drv_data, SSSR);
624 625 626
	if (status == ~0)
		return IRQ_NONE;

627
	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
628 629 630 631 632 633 634

	/* Ignore possible writes if we don't need to write */
	if (!(sccr1_reg & SSCR1_TIE))
		mask &= ~SSSR_TFS;

	if (!(status & mask))
		return IRQ_NONE;
635 636

	if (!drv_data->cur_msg) {
S
Stephen Street 已提交
637

638 639 640 641 642 643
		pxa2xx_spi_write(drv_data, SSCR0,
				 pxa2xx_spi_read(drv_data, SSCR0)
				 & ~SSCR0_SSE);
		pxa2xx_spi_write(drv_data, SSCR1,
				 pxa2xx_spi_read(drv_data, SSCR1)
				 & ~drv_data->int_cr1);
644
		if (!pxa25x_ssp_comp(drv_data))
645
			pxa2xx_spi_write(drv_data, SSTO, 0);
646
		write_SSSR_CS(drv_data, drv_data->clear_sr);
S
Stephen Street 已提交
647

648 649
		dev_err(&drv_data->pdev->dev,
			"bad message state in interrupt handler\n");
S
Stephen Street 已提交
650

651 652 653 654 655 656 657
		/* Never fail */
		return IRQ_HANDLED;
	}

	return drv_data->transfer_handler(drv_data);
}

658
/*
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
 * input frequency by fractions of 2^24. It also has a divider by 5.
 *
 * There are formulas to get baud rate value for given input frequency and
 * divider parameters, such as DDS_CLK_RATE and SCR:
 *
 * Fsys = 200MHz
 *
 * Fssp = Fsys * DDS_CLK_RATE / 2^24			(1)
 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1))		(2)
 *
 * DDS_CLK_RATE either 2^n or 2^n / 5.
 * SCR is in range 0 .. 255
 *
 * Divisor = 5^i * 2^j * 2 * k
 *       i = [0, 1]      i = 1 iff j = 0 or j > 3
 *       j = [0, 23]     j = 0 iff i = 1
 *       k = [1, 256]
 * Special case: j = 0, i = 1: Divisor = 2 / 5
 *
 * Accordingly to the specification the recommended values for DDS_CLK_RATE
 * are:
 *	Case 1:		2^n, n = [0, 23]
 *	Case 2:		2^24 * 2 / 5 (0x666666)
 *	Case 3:		less than or equal to 2^24 / 5 / 16 (0x33333)
 *
 * In all cases the lowest possible value is better.
 *
 * The function calculates parameters for all cases and chooses the one closest
 * to the asked baud rate.
689
 */
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
{
	unsigned long xtal = 200000000;
	unsigned long fref = xtal / 2;		/* mandatory division by 2,
						   see (2) */
						/* case 3 */
	unsigned long fref1 = fref / 2;		/* case 1 */
	unsigned long fref2 = fref * 2 / 5;	/* case 2 */
	unsigned long scale;
	unsigned long q, q1, q2;
	long r, r1, r2;
	u32 mul;

	/* Case 1 */

	/* Set initial value for DDS_CLK_RATE */
	mul = (1 << 24) >> 1;

	/* Calculate initial quot */
	q1 = DIV_ROUND_CLOSEST(fref1, rate);

	/* Scale q1 if it's too big */
	if (q1 > 256) {
		/* Scale q1 to range [1, 512] */
		scale = fls_long(q1 - 1);
		if (scale > 9) {
			q1 >>= scale - 9;
			mul >>= scale - 9;
718
		}
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

		/* Round the result if we have a remainder */
		q1 += q1 & 1;
	}

	/* Decrease DDS_CLK_RATE as much as we can without loss in precision */
	scale = __ffs(q1);
	q1 >>= scale;
	mul >>= scale;

	/* Get the remainder */
	r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);

	/* Case 2 */

	q2 = DIV_ROUND_CLOSEST(fref2, rate);
	r2 = abs(fref2 / q2 - rate);

	/*
	 * Choose the best between two: less remainder we have the better. We
	 * can't go case 2 if q2 is greater than 256 since SCR register can
	 * hold only values 0 .. 255.
	 */
	if (r2 >= r1 || q2 > 256) {
		/* case 1 is better */
		r = r1;
		q = q1;
	} else {
		/* case 2 is better */
		r = r2;
		q = q2;
		mul = (1 << 24) * 2 / 5;
751 752
	}

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	/* Check case 3 only If the divisor is big enough */
	if (fref / rate >= 80) {
		u64 fssp;
		u32 m;

		/* Calculate initial quot */
		q1 = DIV_ROUND_CLOSEST(fref, rate);
		m = (1 << 24) / q1;

		/* Get the remainder */
		fssp = (u64)fref * m;
		do_div(fssp, 1 << 24);
		r1 = abs(fssp - rate);

		/* Choose this one if it suits better */
		if (r1 < r) {
			/* case 3 is better */
			q = 1;
			mul = m;
		}
	}
774

775 776
	*dds = mul;
	return q - 1;
777 778
}

779
static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
780
{
781 782 783 784
	unsigned long ssp_clk = drv_data->max_clk_rate;
	const struct ssp_device *ssp = drv_data->ssp;

	rate = min_t(int, ssp_clk, rate);
785

786
	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
787
		return (ssp_clk / (2 * rate) - 1) & 0xff;
788
	else
789
		return (ssp_clk / rate - 1) & 0xfff;
790 791
}

792 793 794
static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
					   struct chip_data *chip, int rate)
{
795
	unsigned int clk_div;
796 797 798

	switch (drv_data->ssp_type) {
	case QUARK_X1000_SSP:
799
		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
800
		break;
801
	default:
802
		clk_div = ssp_get_clk_div(drv_data, rate);
803
		break;
804
	}
805
	return clk_div << 8;
806 807
}

808 809 810 811 812 813 814
static void pump_transfers(unsigned long data)
{
	struct driver_data *drv_data = (struct driver_data *)data;
	struct spi_message *message = NULL;
	struct spi_transfer *transfer = NULL;
	struct spi_transfer *previous = NULL;
	struct chip_data *chip = NULL;
815 816 817 818
	u32 clk_div = 0;
	u8 bits = 0;
	u32 speed = 0;
	u32 cr0;
819 820 821
	u32 cr1;
	u32 dma_thresh = drv_data->cur_chip->dma_threshold;
	u32 dma_burst = drv_data->cur_chip->dma_burst_size;
822
	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
823 824 825 826 827 828 829 830 831

	/* Get current state information */
	message = drv_data->cur_msg;
	transfer = drv_data->cur_transfer;
	chip = drv_data->cur_chip;

	/* Handle for abort */
	if (message->state == ERROR_STATE) {
		message->status = -EIO;
S
Stephen Street 已提交
832
		giveback(drv_data);
833 834 835 836 837 838
		return;
	}

	/* Handle end of message */
	if (message->state == DONE_STATE) {
		message->status = 0;
S
Stephen Street 已提交
839
		giveback(drv_data);
840 841 842
		return;
	}

N
Ned Forrester 已提交
843
	/* Delay if requested at end of transfer before CS change */
844 845 846 847 848 849
	if (message->state == RUNNING_STATE) {
		previous = list_entry(transfer->transfer_list.prev,
					struct spi_transfer,
					transfer_list);
		if (previous->delay_usecs)
			udelay(previous->delay_usecs);
N
Ned Forrester 已提交
850 851 852

		/* Drop chip select only if cs_change is requested */
		if (previous->cs_change)
853
			cs_deassert(drv_data);
854 855
	}

856 857
	/* Check if we can DMA this transfer */
	if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
N
Ned Forrester 已提交
858 859 860 861 862

		/* reject already-mapped transfers; PIO won't always work */
		if (message->is_dma_mapped
				|| transfer->rx_dma || transfer->tx_dma) {
			dev_err(&drv_data->pdev->dev,
863 864
				"pump_transfers: mapped transfer length of "
				"%u is greater than %d\n",
N
Ned Forrester 已提交
865 866 867 868 869 870 871
				transfer->len, MAX_DMA_LEN);
			message->status = -EINVAL;
			giveback(drv_data);
			return;
		}

		/* warn ... we force this to PIO mode */
872 873 874 875
		dev_warn_ratelimited(&message->spi->dev,
				     "pump_transfers: DMA disabled for transfer length %ld "
				     "greater than %d\n",
				     (long)drv_data->len, MAX_DMA_LEN);
876 877
	}

878
	/* Setup the transfer state based on the type of transfer */
879
	if (pxa2xx_spi_flush(drv_data) == 0) {
880 881
		dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
		message->status = -EIO;
S
Stephen Street 已提交
882
		giveback(drv_data);
883 884
		return;
	}
885
	drv_data->n_bytes = chip->n_bytes;
886 887 888 889 890 891
	drv_data->tx = (void *)transfer->tx_buf;
	drv_data->tx_end = drv_data->tx + transfer->len;
	drv_data->rx = transfer->rx_buf;
	drv_data->rx_end = drv_data->rx + transfer->len;
	drv_data->rx_dma = transfer->rx_dma;
	drv_data->tx_dma = transfer->tx_dma;
892
	drv_data->len = transfer->len;
893 894
	drv_data->write = drv_data->tx ? chip->write : null_writer;
	drv_data->read = drv_data->rx ? chip->read : null_reader;
895 896

	/* Change speed and bit per word on a per transfer */
897
	cr0 = chip->cr0;
898 899 900 901 902 903 904 905 906 907 908
	if (transfer->speed_hz || transfer->bits_per_word) {

		bits = chip->bits_per_word;
		speed = chip->speed_hz;

		if (transfer->speed_hz)
			speed = transfer->speed_hz;

		if (transfer->bits_per_word)
			bits = transfer->bits_per_word;

909
		clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

		if (bits <= 8) {
			drv_data->n_bytes = 1;
			drv_data->read = drv_data->read != null_reader ?
						u8_reader : null_reader;
			drv_data->write = drv_data->write != null_writer ?
						u8_writer : null_writer;
		} else if (bits <= 16) {
			drv_data->n_bytes = 2;
			drv_data->read = drv_data->read != null_reader ?
						u16_reader : null_reader;
			drv_data->write = drv_data->write != null_writer ?
						u16_writer : null_writer;
		} else if (bits <= 32) {
			drv_data->n_bytes = 4;
			drv_data->read = drv_data->read != null_reader ?
						u32_reader : null_reader;
			drv_data->write = drv_data->write != null_writer ?
						u32_writer : null_writer;
		}
930 931 932
		/* if bits/word is changed in dma mode, then must check the
		 * thresholds and burst also */
		if (chip->enable_dma) {
933 934
			if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
							message->spi,
935 936
							bits, &dma_burst,
							&dma_thresh))
937 938
				dev_warn_ratelimited(&message->spi->dev,
						     "pump_transfers: DMA burst size reduced to match bits_per_word\n");
939
		}
940

941
		cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
942 943
	}

944 945
	message->state = RUNNING_STATE;

N
Ned Forrester 已提交
946
	drv_data->dma_mapped = 0;
947 948
	if (pxa2xx_spi_dma_is_possible(drv_data->len))
		drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
N
Ned Forrester 已提交
949
	if (drv_data->dma_mapped) {
950 951

		/* Ensure we have the correct interrupt handler */
952 953 954
		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;

		pxa2xx_spi_dma_prepare(drv_data, dma_burst);
955

956 957
		/* Clear status and start DMA engine */
		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
958
		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
959 960

		pxa2xx_spi_dma_start(drv_data);
961 962 963 964
	} else {
		/* Ensure we have the correct interrupt handler	*/
		drv_data->transfer_handler = interrupt_transfer;

965 966
		/* Clear status  */
		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
967
		write_SSSR_CS(drv_data, drv_data->clear_sr);
968 969
	}

970
	if (is_lpss_ssp(drv_data)) {
971 972 973 974 975 976 977 978
		if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
		    != chip->lpss_rx_threshold)
			pxa2xx_spi_write(drv_data, SSIRF,
					 chip->lpss_rx_threshold);
		if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
		    != chip->lpss_tx_threshold)
			pxa2xx_spi_write(drv_data, SSITF,
					 chip->lpss_tx_threshold);
979 980
	}

981
	if (is_quark_x1000_ssp(drv_data) &&
982 983
	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
984

985
	/* see if we need to reload the config registers */
986 987 988
	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
	    != (cr1 & change_mask)) {
989
		/* stop the SSP, and update the other bits */
990
		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
991
		if (!pxa25x_ssp_comp(drv_data))
992
			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
993
		/* first set CR1 without interrupt and service enables */
994
		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
995
		/* restart the SSP */
996
		pxa2xx_spi_write(drv_data, SSCR0, cr0);
997

998
	} else {
999
		if (!pxa25x_ssp_comp(drv_data))
1000
			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1001
	}
1002

1003
	cs_assert(drv_data);
1004 1005 1006

	/* after chip select, release the data by enabling service
	 * requests and interrupts, without changing any mode bits */
1007
	pxa2xx_spi_write(drv_data, SSCR1, cr1);
1008 1009
}

1010 1011
static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
					   struct spi_message *msg)
1012
{
1013
	struct driver_data *drv_data = spi_master_get_devdata(master);
1014

1015
	drv_data->cur_msg = msg;
1016 1017 1018 1019 1020 1021
	/* Initial message state*/
	drv_data->cur_msg->state = START_STATE;
	drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
						struct spi_transfer,
						transfer_list);

1022 1023
	/* prepare to setup the SSP, in pump_transfers, using the per
	 * chip configuration */
1024 1025 1026 1027 1028 1029 1030
	drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);

	/* Mark as busy and launch transfers */
	tasklet_schedule(&drv_data->pump_transfers);
	return 0;
}

1031 1032 1033 1034 1035
static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
{
	struct driver_data *drv_data = spi_master_get_devdata(master);

	/* Disable the SSP now */
1036 1037
	pxa2xx_spi_write(drv_data, SSCR0,
			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1038 1039 1040 1041

	return 0;
}

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
static int setup_cs(struct spi_device *spi, struct chip_data *chip,
		    struct pxa2xx_spi_chip *chip_info)
{
	int err = 0;

	if (chip == NULL || chip_info == NULL)
		return 0;

	/* NOTE: setup() can be called multiple times, possibly with
	 * different chip_info, release previously requested GPIO
	 */
	if (gpio_is_valid(chip->gpio_cs))
		gpio_free(chip->gpio_cs);

	/* If (*cs_control) is provided, ignore GPIO chip select */
	if (chip_info->cs_control) {
		chip->cs_control = chip_info->cs_control;
		return 0;
	}

	if (gpio_is_valid(chip_info->gpio_cs)) {
		err = gpio_request(chip_info->gpio_cs, "SPI_CS");
		if (err) {
1065 1066
			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
				chip_info->gpio_cs);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
			return err;
		}

		chip->gpio_cs = chip_info->gpio_cs;
		chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;

		err = gpio_direction_output(chip->gpio_cs,
					!chip->gpio_cs_inverted);
	}

	return err;
}

1080 1081 1082 1083 1084 1085
static int setup(struct spi_device *spi)
{
	struct pxa2xx_spi_chip *chip_info = NULL;
	struct chip_data *chip;
	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
	unsigned int clk_div;
1086 1087
	uint tx_thres, tx_hi_thres, rx_thres;

1088 1089 1090 1091 1092 1093
	switch (drv_data->ssp_type) {
	case QUARK_X1000_SSP:
		tx_thres = TX_THRESH_QUARK_X1000_DFLT;
		tx_hi_thres = 0;
		rx_thres = RX_THRESH_QUARK_X1000_DFLT;
		break;
1094 1095
	case LPSS_LPT_SSP:
	case LPSS_BYT_SSP:
1096 1097 1098
		tx_thres = LPSS_TX_LOTHRESH_DFLT;
		tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
		rx_thres = LPSS_RX_THRESH_DFLT;
1099 1100
		break;
	default:
1101 1102 1103
		tx_thres = TX_THRESH_DFLT;
		tx_hi_thres = 0;
		rx_thres = RX_THRESH_DFLT;
1104
		break;
1105
	}
1106

1107
	/* Only alloc on first setup */
1108
	chip = spi_get_ctldata(spi);
1109
	if (!chip) {
1110
		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1111
		if (!chip)
1112 1113
			return -ENOMEM;

1114 1115
		if (drv_data->ssp_type == CE4100_SSP) {
			if (spi->chip_select > 4) {
1116 1117
				dev_err(&spi->dev,
					"failed setup: cs number must not be > 4.\n");
1118 1119 1120 1121 1122 1123 1124
				kfree(chip);
				return -EINVAL;
			}

			chip->frm = spi->chip_select;
		} else
			chip->gpio_cs = -1;
1125
		chip->enable_dma = 0;
1126
		chip->timeout = TIMOUT_DFLT;
1127 1128
	}

1129 1130 1131 1132
	/* protocol drivers may change the chip settings, so...
	 * if chip_info exists, use it */
	chip_info = spi->controller_data;

1133
	/* chip_info isn't always needed */
1134
	chip->cr1 = 0;
1135
	if (chip_info) {
1136 1137 1138 1139
		if (chip_info->timeout)
			chip->timeout = chip_info->timeout;
		if (chip_info->tx_threshold)
			tx_thres = chip_info->tx_threshold;
1140 1141
		if (chip_info->tx_hi_threshold)
			tx_hi_thres = chip_info->tx_hi_threshold;
1142 1143 1144
		if (chip_info->rx_threshold)
			rx_thres = chip_info->rx_threshold;
		chip->enable_dma = drv_data->master_info->enable_dma;
1145 1146 1147
		chip->dma_threshold = 0;
		if (chip_info->enable_loopback)
			chip->cr1 = SSCR1_LBM;
1148 1149 1150 1151 1152 1153 1154
	} else if (ACPI_HANDLE(&spi->dev)) {
		/*
		 * Slave devices enumerated from ACPI namespace don't
		 * usually have chip_info but we still might want to use
		 * DMA with them.
		 */
		chip->enable_dma = drv_data->master_info->enable_dma;
1155 1156
	}

1157 1158 1159 1160
	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
				| SSITF_TxHiThresh(tx_hi_thres);

1161 1162 1163 1164 1165
	/* set dma burst and threshold outside of chip_info path so that if
	 * chip_info goes away after setting chip->enable_dma, the
	 * burst and threshold can still respond to changes in bits_per_word */
	if (chip->enable_dma) {
		/* set up legal burst and threshold for dma */
1166 1167
		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
						spi->bits_per_word,
1168 1169
						&chip->dma_burst_size,
						&chip->dma_threshold)) {
1170 1171
			dev_warn(&spi->dev,
				 "in setup: DMA burst size reduced to match bits_per_word\n");
1172 1173 1174
		}
	}

1175
	clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
1176
	chip->speed_hz = spi->max_speed_hz;
1177

1178 1179
	chip->cr0 = pxa2xx_configure_sscr0(drv_data, clk_div,
					   spi->bits_per_word);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	switch (drv_data->ssp_type) {
	case QUARK_X1000_SSP:
		chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
				   & QUARK_X1000_SSCR1_RFT)
				   | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
				   & QUARK_X1000_SSCR1_TFT);
		break;
	default:
		chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
			(SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
		break;
	}

1193 1194 1195
	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1196

1197 1198 1199
	if (spi->mode & SPI_LOOP)
		chip->cr1 |= SSCR1_LBM;

1200
	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
1201
	if (!pxa25x_ssp_comp(drv_data))
1202
		dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
1203
			drv_data->max_clk_rate
1204 1205
				/ (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
			chip->enable_dma ? "DMA" : "PIO");
1206
	else
1207
		dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
1208
			drv_data->max_clk_rate / 2
1209 1210
				/ (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
			chip->enable_dma ? "DMA" : "PIO");
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

	if (spi->bits_per_word <= 8) {
		chip->n_bytes = 1;
		chip->read = u8_reader;
		chip->write = u8_writer;
	} else if (spi->bits_per_word <= 16) {
		chip->n_bytes = 2;
		chip->read = u16_reader;
		chip->write = u16_writer;
	} else if (spi->bits_per_word <= 32) {
1221 1222
		if (!is_quark_x1000_ssp(drv_data))
			chip->cr0 |= SSCR0_EDSS;
1223 1224 1225 1226
		chip->n_bytes = 4;
		chip->read = u32_reader;
		chip->write = u32_writer;
	}
1227
	chip->bits_per_word = spi->bits_per_word;
1228 1229 1230

	spi_set_ctldata(spi, chip);

1231 1232 1233
	if (drv_data->ssp_type == CE4100_SSP)
		return 0;

1234
	return setup_cs(spi, chip, chip_info);
1235 1236
}

1237
static void cleanup(struct spi_device *spi)
1238
{
1239
	struct chip_data *chip = spi_get_ctldata(spi);
1240
	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1241

1242 1243 1244
	if (!chip)
		return;

1245
	if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
1246 1247
		gpio_free(chip->gpio_cs);

1248 1249 1250
	kfree(chip);
}

1251
#ifdef CONFIG_ACPI
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263

static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
	{ "INT33C0", LPSS_LPT_SSP },
	{ "INT33C1", LPSS_LPT_SSP },
	{ "INT3430", LPSS_LPT_SSP },
	{ "INT3431", LPSS_LPT_SSP },
	{ "80860F0E", LPSS_BYT_SSP },
	{ "8086228E", LPSS_BYT_SSP },
	{ },
};
MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);

1264 1265 1266 1267 1268 1269 1270
static struct pxa2xx_spi_master *
pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
{
	struct pxa2xx_spi_master *pdata;
	struct acpi_device *adev;
	struct ssp_device *ssp;
	struct resource *res;
1271 1272
	const struct acpi_device_id *id;
	int devid, type;
1273 1274 1275 1276 1277

	if (!ACPI_HANDLE(&pdev->dev) ||
	    acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
		return NULL;

1278 1279 1280 1281 1282 1283
	id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
	if (id)
		type = (int)id->driver_data;
	else
		return NULL;

1284
	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1285
	if (!pdata)
1286 1287 1288 1289 1290 1291 1292 1293 1294
		return NULL;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return NULL;

	ssp = &pdata->ssp;

	ssp->phys_base = res->start;
1295 1296
	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(ssp->mmio_base))
1297
		return NULL;
1298 1299 1300

	ssp->clk = devm_clk_get(&pdev->dev, NULL);
	ssp->irq = platform_get_irq(pdev, 0);
1301
	ssp->type = type;
1302 1303 1304 1305 1306 1307 1308
	ssp->pdev = pdev;

	ssp->port_id = -1;
	if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
		ssp->port_id = devid;

	pdata->num_chipselect = 1;
1309
	pdata->enable_dma = true;
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321

	return pdata;
}

#else
static inline struct pxa2xx_spi_master *
pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
{
	return NULL;
}
#endif

1322
static int pxa2xx_spi_probe(struct platform_device *pdev)
1323 1324 1325 1326
{
	struct device *dev = &pdev->dev;
	struct pxa2xx_spi_master *platform_info;
	struct spi_master *master;
G
Guennadi Liakhovetski 已提交
1327
	struct driver_data *drv_data;
1328
	struct ssp_device *ssp;
G
Guennadi Liakhovetski 已提交
1329
	int status;
1330
	u32 tmp;
1331

1332 1333
	platform_info = dev_get_platdata(dev);
	if (!platform_info) {
1334 1335 1336 1337 1338
		platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
		if (!platform_info) {
			dev_err(&pdev->dev, "missing platform data\n");
			return -ENODEV;
		}
1339
	}
1340

H
Haojian Zhuang 已提交
1341
	ssp = pxa_ssp_request(pdev->id, pdev->name);
1342 1343 1344 1345 1346
	if (!ssp)
		ssp = &platform_info->ssp;

	if (!ssp->mmio_base) {
		dev_err(&pdev->dev, "failed to get ssp\n");
1347 1348 1349 1350 1351 1352
		return -ENODEV;
	}

	/* Allocate master with space for drv_data and null dma buffer */
	master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
	if (!master) {
G
Guennadi Liakhovetski 已提交
1353
		dev_err(&pdev->dev, "cannot alloc spi_master\n");
H
Haojian Zhuang 已提交
1354
		pxa_ssp_free(ssp);
1355 1356 1357 1358 1359 1360
		return -ENOMEM;
	}
	drv_data = spi_master_get_devdata(master);
	drv_data->master = master;
	drv_data->master_info = platform_info;
	drv_data->pdev = pdev;
1361
	drv_data->ssp = ssp;
1362

1363 1364
	master->dev.parent = &pdev->dev;
	master->dev.of_node = pdev->dev.of_node;
1365
	/* the spi->mode bits understood by this driver: */
1366
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1367

1368
	master->bus_num = ssp->port_id;
1369
	master->num_chipselect = platform_info->num_chipselect;
1370
	master->dma_alignment = DMA_ALIGNMENT;
1371 1372
	master->cleanup = cleanup;
	master->setup = setup;
1373
	master->transfer_one_message = pxa2xx_spi_transfer_one_message;
1374
	master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1375
	master->auto_runtime_pm = true;
1376

1377
	drv_data->ssp_type = ssp->type;
1378
	drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
1379

1380 1381
	drv_data->ioaddr = ssp->mmio_base;
	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1382
	if (pxa25x_ssp_comp(drv_data)) {
1383 1384 1385 1386 1387 1388 1389 1390 1391
		switch (drv_data->ssp_type) {
		case QUARK_X1000_SSP:
			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
			break;
		default:
			master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
			break;
		}

1392 1393 1394 1395 1396
		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
		drv_data->dma_cr1 = 0;
		drv_data->clear_sr = SSSR_ROR;
		drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
	} else {
1397
		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1398
		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1399
		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1400 1401 1402 1403
		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
	}

1404 1405
	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
			drv_data);
1406
	if (status < 0) {
G
Guennadi Liakhovetski 已提交
1407
		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1408 1409 1410 1411 1412 1413 1414
		goto out_error_master_alloc;
	}

	/* Setup DMA if requested */
	drv_data->tx_channel = -1;
	drv_data->rx_channel = -1;
	if (platform_info->enable_dma) {
1415 1416
		status = pxa2xx_spi_dma_setup(drv_data);
		if (status) {
1417
			dev_dbg(dev, "no DMA channels available, using PIO\n");
1418
			platform_info->enable_dma = false;
1419 1420 1421 1422
		}
	}

	/* Enable SOC clock */
1423 1424 1425
	clk_prepare_enable(ssp->clk);

	drv_data->max_clk_rate = clk_get_rate(ssp->clk);
1426 1427

	/* Load default SSP configuration */
1428
	pxa2xx_spi_write(drv_data, SSCR0, 0);
1429 1430
	switch (drv_data->ssp_type) {
	case QUARK_X1000_SSP:
1431 1432 1433
		tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
		      | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
		pxa2xx_spi_write(drv_data, SSCR1, tmp);
1434 1435

		/* using the Motorola SPI protocol and use 8 bit frame */
1436 1437 1438
		pxa2xx_spi_write(drv_data, SSCR0,
				 QUARK_X1000_SSCR0_Motorola
				 | QUARK_X1000_SSCR0_DataSize(8));
1439 1440
		break;
	default:
1441 1442 1443 1444 1445
		tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
		      SSCR1_TxTresh(TX_THRESH_DFLT);
		pxa2xx_spi_write(drv_data, SSCR1, tmp);
		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
		pxa2xx_spi_write(drv_data, SSCR0, tmp);
1446 1447 1448
		break;
	}

1449
	if (!pxa25x_ssp_comp(drv_data))
1450
		pxa2xx_spi_write(drv_data, SSTO, 0);
1451 1452

	if (!is_quark_x1000_ssp(drv_data))
1453
		pxa2xx_spi_write(drv_data, SSPSP, 0);
1454

1455 1456
	if (is_lpss_ssp(drv_data))
		lpss_ssp_setup(drv_data);
1457

1458 1459
	tasklet_init(&drv_data->pump_transfers, pump_transfers,
		     (unsigned long)drv_data);
1460

1461 1462 1463 1464 1465
	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);

1466 1467
	/* Register with the SPI framework */
	platform_set_drvdata(pdev, drv_data);
1468
	status = devm_spi_register_master(&pdev->dev, master);
1469 1470
	if (status != 0) {
		dev_err(&pdev->dev, "problem registering spi master\n");
1471
		goto out_error_clock_enabled;
1472 1473 1474 1475 1476
	}

	return status;

out_error_clock_enabled:
1477
	clk_disable_unprepare(ssp->clk);
1478
	pxa2xx_spi_dma_release(drv_data);
1479
	free_irq(ssp->irq, drv_data);
1480 1481 1482

out_error_master_alloc:
	spi_master_put(master);
H
Haojian Zhuang 已提交
1483
	pxa_ssp_free(ssp);
1484 1485 1486 1487 1488 1489
	return status;
}

static int pxa2xx_spi_remove(struct platform_device *pdev)
{
	struct driver_data *drv_data = platform_get_drvdata(pdev);
1490
	struct ssp_device *ssp;
1491 1492 1493

	if (!drv_data)
		return 0;
1494
	ssp = drv_data->ssp;
1495

1496 1497
	pm_runtime_get_sync(&pdev->dev);

1498
	/* Disable the SSP at the peripheral and SOC level */
1499
	pxa2xx_spi_write(drv_data, SSCR0, 0);
1500
	clk_disable_unprepare(ssp->clk);
1501 1502

	/* Release DMA */
1503 1504
	if (drv_data->master_info->enable_dma)
		pxa2xx_spi_dma_release(drv_data);
1505

1506 1507 1508
	pm_runtime_put_noidle(&pdev->dev);
	pm_runtime_disable(&pdev->dev);

1509
	/* Release IRQ */
1510 1511 1512
	free_irq(ssp->irq, drv_data);

	/* Release SSP */
H
Haojian Zhuang 已提交
1513
	pxa_ssp_free(ssp);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

	return 0;
}

static void pxa2xx_spi_shutdown(struct platform_device *pdev)
{
	int status = 0;

	if ((status = pxa2xx_spi_remove(pdev)) != 0)
		dev_err(&pdev->dev, "shutdown failed with %d\n", status);
}

1526
#ifdef CONFIG_PM_SLEEP
1527
static int pxa2xx_spi_suspend(struct device *dev)
1528
{
1529
	struct driver_data *drv_data = dev_get_drvdata(dev);
1530
	struct ssp_device *ssp = drv_data->ssp;
1531 1532
	int status = 0;

1533
	status = spi_master_suspend(drv_data->master);
1534 1535
	if (status != 0)
		return status;
1536
	pxa2xx_spi_write(drv_data, SSCR0, 0);
1537 1538 1539

	if (!pm_runtime_suspended(dev))
		clk_disable_unprepare(ssp->clk);
1540 1541 1542 1543

	return 0;
}

1544
static int pxa2xx_spi_resume(struct device *dev)
1545
{
1546
	struct driver_data *drv_data = dev_get_drvdata(dev);
1547
	struct ssp_device *ssp = drv_data->ssp;
1548 1549
	int status = 0;

1550
	pxa2xx_spi_dma_resume(drv_data);
1551

1552
	/* Enable the SSP clock */
1553 1554
	if (!pm_runtime_suspended(dev))
		clk_prepare_enable(ssp->clk);
1555

1556
	/* Restore LPSS private register bits */
1557 1558
	if (is_lpss_ssp(drv_data))
		lpss_ssp_setup(drv_data);
1559

1560
	/* Start the queue running */
1561
	status = spi_master_resume(drv_data->master);
1562
	if (status != 0) {
1563
		dev_err(dev, "problem starting queue (%d)\n", status);
1564 1565 1566 1567 1568
		return status;
	}

	return 0;
}
1569 1570
#endif

1571
#ifdef CONFIG_PM
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
static int pxa2xx_spi_runtime_suspend(struct device *dev)
{
	struct driver_data *drv_data = dev_get_drvdata(dev);

	clk_disable_unprepare(drv_data->ssp->clk);
	return 0;
}

static int pxa2xx_spi_runtime_resume(struct device *dev)
{
	struct driver_data *drv_data = dev_get_drvdata(dev);

	clk_prepare_enable(drv_data->ssp->clk);
	return 0;
}
#endif
1588

1589
static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1590 1591 1592
	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
			   pxa2xx_spi_runtime_resume, NULL)
1593
};
1594 1595 1596

static struct platform_driver driver = {
	.driver = {
1597 1598
		.name	= "pxa2xx-spi",
		.pm	= &pxa2xx_spi_pm_ops,
1599
		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1600
	},
1601
	.probe = pxa2xx_spi_probe,
1602
	.remove = pxa2xx_spi_remove,
1603 1604 1605 1606 1607
	.shutdown = pxa2xx_spi_shutdown,
};

static int __init pxa2xx_spi_init(void)
{
1608
	return platform_driver_register(&driver);
1609
}
A
Antonio Ospite 已提交
1610
subsys_initcall(pxa2xx_spi_init);
1611 1612 1613 1614 1615 1616

static void __exit pxa2xx_spi_exit(void)
{
	platform_driver_unregister(&driver);
}
module_exit(pxa2xx_spi_exit);