spi-pxa2xx.c 45.9 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
 *
 * 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.
 */

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

36
#include "spi-pxa2xx.h"
37 38

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

43 44
#define TIMOUT_DFLT		1000

45 46 47 48 49 50 51 52
/*
 * 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 \
53
				| SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
54 55 56 57
				| SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
				| SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
				| SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
				| SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
58

59 60 61 62 63 64
#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)

65 66 67
#define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE	BIT(24)
#define LPSS_CS_CONTROL_SW_MODE			BIT(0)
#define LPSS_CS_CONTROL_CS_HIGH			BIT(1)
68 69
#define LPSS_CAPS_CS_EN_SHIFT			9
#define LPSS_CAPS_CS_EN_MASK			(0xf << LPSS_CAPS_CS_EN_SHIFT)
70

71 72 73 74 75 76 77
struct lpss_config {
	/* LPSS offset from drv_data->ioaddr */
	unsigned offset;
	/* Register offsets from drv_data->lpss_base or -1 */
	int reg_general;
	int reg_ssp;
	int reg_cs_ctrl;
78
	int reg_capabilities;
79 80 81 82
	/* FIFO thresholds */
	u32 rx_threshold;
	u32 tx_threshold_lo;
	u32 tx_threshold_hi;
83 84 85
	/* Chip select control */
	unsigned cs_sel_shift;
	unsigned cs_sel_mask;
86
	unsigned cs_num;
87 88 89 90 91 92 93 94 95
};

/* Keep these sorted with enum pxa_ssp_type */
static const struct lpss_config lpss_platforms[] = {
	{	/* LPSS_LPT_SSP */
		.offset = 0x800,
		.reg_general = 0x08,
		.reg_ssp = 0x0c,
		.reg_cs_ctrl = 0x18,
96
		.reg_capabilities = -1,
97 98 99 100 101 102 103 104 105
		.rx_threshold = 64,
		.tx_threshold_lo = 160,
		.tx_threshold_hi = 224,
	},
	{	/* LPSS_BYT_SSP */
		.offset = 0x400,
		.reg_general = 0x08,
		.reg_ssp = 0x0c,
		.reg_cs_ctrl = 0x18,
106
		.reg_capabilities = -1,
107 108 109 110
		.rx_threshold = 64,
		.tx_threshold_lo = 160,
		.tx_threshold_hi = 224,
	},
111 112 113 114 115 116 117 118 119 120 121 122 123
	{	/* LPSS_BSW_SSP */
		.offset = 0x400,
		.reg_general = 0x08,
		.reg_ssp = 0x0c,
		.reg_cs_ctrl = 0x18,
		.reg_capabilities = -1,
		.rx_threshold = 64,
		.tx_threshold_lo = 160,
		.tx_threshold_hi = 224,
		.cs_sel_shift = 2,
		.cs_sel_mask = 1 << 2,
		.cs_num = 2,
	},
124 125 126 127 128
	{	/* LPSS_SPT_SSP */
		.offset = 0x200,
		.reg_general = -1,
		.reg_ssp = 0x20,
		.reg_cs_ctrl = 0x24,
129
		.reg_capabilities = 0xfc,
130 131 132 133
		.rx_threshold = 1,
		.tx_threshold_lo = 32,
		.tx_threshold_hi = 56,
	},
134 135 136 137 138 139 140 141 142
	{	/* LPSS_BXT_SSP */
		.offset = 0x200,
		.reg_general = -1,
		.reg_ssp = 0x20,
		.reg_cs_ctrl = 0x24,
		.reg_capabilities = 0xfc,
		.rx_threshold = 1,
		.tx_threshold_lo = 16,
		.tx_threshold_hi = 48,
143 144
		.cs_sel_shift = 8,
		.cs_sel_mask = 3 << 8,
145
	},
146 147 148 149 150 151 152 153
};

static inline const struct lpss_config
*lpss_get_config(const struct driver_data *drv_data)
{
	return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
}

154 155
static bool is_lpss_ssp(const struct driver_data *drv_data)
{
156 157 158
	switch (drv_data->ssp_type) {
	case LPSS_LPT_SSP:
	case LPSS_BYT_SSP:
159
	case LPSS_BSW_SSP:
160
	case LPSS_SPT_SSP:
161
	case LPSS_BXT_SSP:
162 163 164 165
		return true;
	default:
		return false;
	}
166 167
}

168 169 170 171 172
static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
{
	return drv_data->ssp_type == QUARK_X1000_SSP;
}

173 174 175
static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
{
	switch (drv_data->ssp_type) {
176 177
	case QUARK_X1000_SSP:
		return QUARK_X1000_SSCR1_CHANGE_MASK;
178 179 180 181 182 183 184 185 186
	default:
		return SSCR1_CHANGE_MASK;
	}
}

static u32
pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
{
	switch (drv_data->ssp_type) {
187 188
	case QUARK_X1000_SSP:
		return RX_THRESH_QUARK_X1000_DFLT;
189 190 191 192 193 194 195 196 197 198
	default:
		return RX_THRESH_DFLT;
	}
}

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

	switch (drv_data->ssp_type) {
199 200 201
	case QUARK_X1000_SSP:
		mask = QUARK_X1000_SSSR_TFL_MASK;
		break;
202 203 204 205 206
	default:
		mask = SSSR_TFL_MASK;
		break;
	}

207
	return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
208 209 210 211 212 213 214 215
}

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

	switch (drv_data->ssp_type) {
216 217 218
	case QUARK_X1000_SSP:
		mask = QUARK_X1000_SSCR1_RFT;
		break;
219 220 221 222 223 224 225 226 227 228 229
	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) {
230 231 232
	case QUARK_X1000_SSP:
		*sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
		break;
233 234 235 236 237 238 239 240 241 242
	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) {
243 244 245 246 247
	case QUARK_X1000_SSP:
		return clk_div
			| QUARK_X1000_SSCR0_Motorola
			| QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
			| SSCR0_SSE;
248 249 250 251 252 253 254 255 256
	default:
		return clk_div
			| SSCR0_Motorola
			| SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
			| SSCR0_SSE
			| (bits > 16 ? SSCR0_EDSS : 0);
	}
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/*
 * 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)
{
283 284
	const struct lpss_config *config;
	u32 value;
285

286 287
	config = lpss_get_config(drv_data);
	drv_data->lpss_base = drv_data->ioaddr + config->offset;
288 289

	/* Enable software chip select control */
290
	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
291 292
	value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
	value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
293
	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
294 295

	/* Enable multiblock DMA transfers */
296
	if (drv_data->master_info->enable_dma) {
297
		__lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
298

299 300 301
		if (config->reg_general >= 0) {
			value = __lpss_ssp_read_priv(drv_data,
						     config->reg_general);
302
			value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
303 304 305
			__lpss_ssp_write_priv(drv_data,
					      config->reg_general, value);
		}
306
	}
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
static void lpss_ssp_select_cs(struct driver_data *drv_data,
			       const struct lpss_config *config)
{
	u32 value, cs;

	if (!config->cs_sel_mask)
		return;

	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);

	cs = drv_data->cur_msg->spi->chip_select;
	cs <<= config->cs_sel_shift;
	if (cs != (value & config->cs_sel_mask)) {
		/*
		 * When switching another chip select output active the
		 * output must be selected first and wait 2 ssp_clk cycles
		 * before changing state to active. Otherwise a short
		 * glitch will occur on the previous chip select since
		 * output select is latched but state control is not.
		 */
		value &= ~config->cs_sel_mask;
		value |= cs;
		__lpss_ssp_write_priv(drv_data,
				      config->reg_cs_ctrl, value);
		ndelay(1000000000 /
		       (drv_data->master->max_speed_hz / 2));
	}
}

338 339
static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
{
340
	const struct lpss_config *config;
341
	u32 value;
342

343 344
	config = lpss_get_config(drv_data);

345 346 347
	if (enable)
		lpss_ssp_select_cs(drv_data, config);

348
	value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
349
	if (enable)
350
		value &= ~LPSS_CS_CONTROL_CS_HIGH;
351
	else
352
		value |= LPSS_CS_CONTROL_CS_HIGH;
353
	__lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
354 355
}

356 357 358 359
static void cs_assert(struct driver_data *drv_data)
{
	struct chip_data *chip = drv_data->cur_chip;

360
	if (drv_data->ssp_type == CE4100_SSP) {
361
		pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
362 363 364
		return;
	}

365 366 367 368 369
	if (chip->cs_control) {
		chip->cs_control(PXA2XX_CS_ASSERT);
		return;
	}

370
	if (gpio_is_valid(chip->gpio_cs)) {
371
		gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
372 373 374
		return;
	}

375 376
	if (is_lpss_ssp(drv_data))
		lpss_ssp_cs_control(drv_data, true);
377 378 379 380 381 382
}

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

383 384 385
	if (drv_data->ssp_type == CE4100_SSP)
		return;

386
	if (chip->cs_control) {
387
		chip->cs_control(PXA2XX_CS_DEASSERT);
388 389 390
		return;
	}

391
	if (gpio_is_valid(chip->gpio_cs)) {
392
		gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
393 394 395
		return;
	}

396 397
	if (is_lpss_ssp(drv_data))
		lpss_ssp_cs_control(drv_data, false);
398 399
}

400
int pxa2xx_spi_flush(struct driver_data *drv_data)
401 402 403 404
{
	unsigned long limit = loops_per_jiffy << 1;

	do {
405 406 407
		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);
408
	write_SSSR_CS(drv_data, SSSR_ROR);
409 410 411 412

	return limit;
}

413
static int null_writer(struct driver_data *drv_data)
414
{
415
	u8 n_bytes = drv_data->n_bytes;
416

417
	if (pxa2xx_spi_txfifo_full(drv_data)
418 419 420
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

421
	pxa2xx_spi_write(drv_data, SSDR, 0);
422 423 424
	drv_data->tx += n_bytes;

	return 1;
425 426
}

427
static int null_reader(struct driver_data *drv_data)
428
{
429
	u8 n_bytes = drv_data->n_bytes;
430

431 432 433
	while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
	       && (drv_data->rx < drv_data->rx_end)) {
		pxa2xx_spi_read(drv_data, SSDR);
434 435
		drv_data->rx += n_bytes;
	}
436 437

	return drv_data->rx == drv_data->rx_end;
438 439
}

440
static int u8_writer(struct driver_data *drv_data)
441
{
442
	if (pxa2xx_spi_txfifo_full(drv_data)
443 444 445
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

446
	pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
447 448 449
	++drv_data->tx;

	return 1;
450 451
}

452
static int u8_reader(struct driver_data *drv_data)
453
{
454 455 456
	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);
457 458
		++drv_data->rx;
	}
459 460

	return drv_data->rx == drv_data->rx_end;
461 462
}

463
static int u16_writer(struct driver_data *drv_data)
464
{
465
	if (pxa2xx_spi_txfifo_full(drv_data)
466 467 468
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

469
	pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
470 471 472
	drv_data->tx += 2;

	return 1;
473 474
}

475
static int u16_reader(struct driver_data *drv_data)
476
{
477 478 479
	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);
480 481
		drv_data->rx += 2;
	}
482 483

	return drv_data->rx == drv_data->rx_end;
484
}
485 486

static int u32_writer(struct driver_data *drv_data)
487
{
488
	if (pxa2xx_spi_txfifo_full(drv_data)
489 490 491
		|| (drv_data->tx == drv_data->tx_end))
		return 0;

492
	pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
493 494 495
	drv_data->tx += 4;

	return 1;
496 497
}

498
static int u32_reader(struct driver_data *drv_data)
499
{
500 501 502
	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);
503 504
		drv_data->rx += 4;
	}
505 506

	return drv_data->rx == drv_data->rx_end;
507 508
}

509
void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
	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 已提交
526
static void giveback(struct driver_data *drv_data)
527 528
{
	struct spi_transfer* last_transfer;
S
Stephen Street 已提交
529
	struct spi_message *msg;
530
	unsigned long timeout;
531

S
Stephen Street 已提交
532 533 534 535
	msg = drv_data->cur_msg;
	drv_data->cur_msg = NULL;
	drv_data->cur_transfer = NULL;

536
	last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
537 538
					transfer_list);

N
Ned Forrester 已提交
539 540 541 542
	/* Delay if requested before any change in chip select */
	if (last_transfer->delay_usecs)
		udelay(last_transfer->delay_usecs);

543 544 545 546 547 548
	/* Wait until SSP becomes idle before deasserting the CS */
	timeout = jiffies + msecs_to_jiffies(10);
	while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
	       !time_after(jiffies, timeout))
		cpu_relax();

N
Ned Forrester 已提交
549 550 551
	/* Drop chip select UNLESS cs_change is true or we are returning
	 * a message with an error, or next message is for another chip
	 */
552
	if (!last_transfer->cs_change)
553
		cs_deassert(drv_data);
N
Ned Forrester 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567
	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 */
568
		next_msg = spi_get_next_queued_message(drv_data->master);
N
Ned Forrester 已提交
569 570 571 572 573 574 575

		/* 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)
576
			cs_deassert(drv_data);
N
Ned Forrester 已提交
577
	}
578

579
	drv_data->cur_chip = NULL;
580
	spi_finalize_current_message(drv_data->master);
581 582
}

583 584 585 586 587
static void reset_sccr1(struct driver_data *drv_data)
{
	struct chip_data *chip = drv_data->cur_chip;
	u32 sccr1_reg;

588
	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
589 590
	sccr1_reg &= ~SSCR1_RFT;
	sccr1_reg |= chip->threshold;
591
	pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
592 593
}

594
static void int_error_stop(struct driver_data *drv_data, const char* msg)
595
{
596
	/* Stop and reset SSP */
597
	write_SSSR_CS(drv_data, drv_data->clear_sr);
598
	reset_sccr1(drv_data);
599
	if (!pxa25x_ssp_comp(drv_data))
600
		pxa2xx_spi_write(drv_data, SSTO, 0);
601
	pxa2xx_spi_flush(drv_data);
602 603
	pxa2xx_spi_write(drv_data, SSCR0,
			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
604

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

607 608 609
	drv_data->cur_msg->state = ERROR_STATE;
	tasklet_schedule(&drv_data->pump_transfers);
}
S
Stephen Street 已提交
610

611 612
static void int_transfer_complete(struct driver_data *drv_data)
{
613
	/* Clear and disable interrupts */
614
	write_SSSR_CS(drv_data, drv_data->clear_sr);
615
	reset_sccr1(drv_data);
616
	if (!pxa25x_ssp_comp(drv_data))
617
		pxa2xx_spi_write(drv_data, SSTO, 0);
618

L
Lucas De Marchi 已提交
619
	/* Update total byte transferred return count actual bytes read */
620 621
	drv_data->cur_msg->actual_length += drv_data->len -
				(drv_data->rx_end - drv_data->rx);
622

N
Ned Forrester 已提交
623 624 625
	/* Transfer delays and chip select release are
	 * handled in pump_transfers or giveback
	 */
626

627
	/* Move to next transfer */
628
	drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
629

630 631 632
	/* Schedule transfer tasklet */
	tasklet_schedule(&drv_data->pump_transfers);
}
633

634 635
static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
{
636 637
	u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
		       drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
638

639
	u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
640

641 642 643 644
	if (irq_status & SSSR_ROR) {
		int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
		return IRQ_HANDLED;
	}
645

646
	if (irq_status & SSSR_TINT) {
647
		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
648 649 650 651 652
		if (drv_data->read(drv_data)) {
			int_transfer_complete(drv_data);
			return IRQ_HANDLED;
		}
	}
653

654 655 656 657 658 659 660
	/* 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));
661

662 663 664 665
	if (drv_data->read(drv_data)) {
		int_transfer_complete(drv_data);
		return IRQ_HANDLED;
	}
666

667
	if (drv_data->tx == drv_data->tx_end) {
668 669 670
		u32 bytes_left;
		u32 sccr1_reg;

671
		sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
672 673 674 675
		sccr1_reg &= ~SSCR1_TIE;

		/*
		 * PXA25x_SSP has no timeout, set up rx threshould for the
L
Lucas De Marchi 已提交
676
		 * remaining RX bytes.
677
		 */
678
		if (pxa25x_ssp_comp(drv_data)) {
679
			u32 rx_thre;
680

681
			pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
682 683 684 685 686 687 688

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

691 692 693
			rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
			if (rx_thre > bytes_left)
				rx_thre = bytes_left;
694

695
			pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
696
		}
697
		pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
698 699
	}

S
Stephen Street 已提交
700 701
	/* We did something */
	return IRQ_HANDLED;
702 703
}

704
static irqreturn_t ssp_int(int irq, void *dev_id)
705
{
706
	struct driver_data *drv_data = dev_id;
707
	u32 sccr1_reg;
708 709 710
	u32 mask = drv_data->mask_sr;
	u32 status;

711 712 713 714 715 716 717 718 719
	/*
	 * 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;

720 721 722 723 724 725
	/*
	 * 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.
	 */
726
	status = pxa2xx_spi_read(drv_data, SSSR);
727 728 729
	if (status == ~0)
		return IRQ_NONE;

730
	sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
731 732 733 734 735

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

736 737 738 739
	/* Ignore RX timeout interrupt if it is disabled */
	if (!(sccr1_reg & SSCR1_TINTE))
		mask &= ~SSSR_TINT;

740 741
	if (!(status & mask))
		return IRQ_NONE;
742 743

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

745 746 747 748 749 750
		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);
751
		if (!pxa25x_ssp_comp(drv_data))
752
			pxa2xx_spi_write(drv_data, SSTO, 0);
753
		write_SSSR_CS(drv_data, drv_data->clear_sr);
S
Stephen Street 已提交
754

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

758 759 760 761 762 763 764
		/* Never fail */
		return IRQ_HANDLED;
	}

	return drv_data->transfer_handler(drv_data);
}

765
/*
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
 * 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.
796
 */
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
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 */
816
	q1 = DIV_ROUND_UP(fref1, rate);
817 818 819 820 821 822 823 824

	/* 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;
825
		}
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

		/* 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 */

841
	q2 = DIV_ROUND_UP(fref2, rate);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	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;
858 859
	}

860
	/* Check case 3 only if the divisor is big enough */
861 862 863 864 865
	if (fref / rate >= 80) {
		u64 fssp;
		u32 m;

		/* Calculate initial quot */
866
		q1 = DIV_ROUND_UP(fref, rate);
867 868 869 870 871 872 873 874 875 876 877 878 879 880
		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;
		}
	}
881

882 883
	*dds = mul;
	return q - 1;
884 885
}

886
static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
887
{
888
	unsigned long ssp_clk = drv_data->master->max_speed_hz;
889 890 891
	const struct ssp_device *ssp = drv_data->ssp;

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

893
	if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
894
		return (ssp_clk / (2 * rate) - 1) & 0xff;
895
	else
896
		return (ssp_clk / rate - 1) & 0xfff;
897 898
}

899
static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
900
					   int rate)
901
{
902
	struct chip_data *chip = drv_data->cur_chip;
903
	unsigned int clk_div;
904 905 906

	switch (drv_data->ssp_type) {
	case QUARK_X1000_SSP:
907
		clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
908
		break;
909
	default:
910
		clk_div = ssp_get_clk_div(drv_data, rate);
911
		break;
912
	}
913
	return clk_div << 8;
914 915
}

916 917 918 919 920 921 922
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;
923 924 925 926
	u32 clk_div = 0;
	u8 bits = 0;
	u32 speed = 0;
	u32 cr0;
927 928 929
	u32 cr1;
	u32 dma_thresh = drv_data->cur_chip->dma_threshold;
	u32 dma_burst = drv_data->cur_chip->dma_burst_size;
930
	u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
931 932 933 934 935 936 937 938 939

	/* 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 已提交
940
		giveback(drv_data);
941 942 943 944 945 946
		return;
	}

	/* Handle end of message */
	if (message->state == DONE_STATE) {
		message->status = 0;
S
Stephen Street 已提交
947
		giveback(drv_data);
948 949 950
		return;
	}

N
Ned Forrester 已提交
951
	/* Delay if requested at end of transfer before CS change */
952 953 954 955 956 957
	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 已提交
958 959 960

		/* Drop chip select only if cs_change is requested */
		if (previous->cs_change)
961
			cs_deassert(drv_data);
962 963
	}

964 965
	/* Check if we can DMA this transfer */
	if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
N
Ned Forrester 已提交
966 967 968 969 970

		/* 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,
971 972
				"pump_transfers: mapped transfer length of "
				"%u is greater than %d\n",
N
Ned Forrester 已提交
973 974 975 976 977 978 979
				transfer->len, MAX_DMA_LEN);
			message->status = -EINVAL;
			giveback(drv_data);
			return;
		}

		/* warn ... we force this to PIO mode */
980 981 982 983
		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);
984 985
	}

986
	/* Setup the transfer state based on the type of transfer */
987
	if (pxa2xx_spi_flush(drv_data) == 0) {
988 989
		dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
		message->status = -EIO;
S
Stephen Street 已提交
990
		giveback(drv_data);
991 992
		return;
	}
993
	drv_data->n_bytes = chip->n_bytes;
994 995 996 997
	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;
998
	drv_data->len = transfer->len;
999 1000
	drv_data->write = drv_data->tx ? chip->write : null_writer;
	drv_data->read = drv_data->rx ? chip->read : null_reader;
1001 1002

	/* Change speed and bit per word on a per transfer */
1003 1004 1005
	bits = transfer->bits_per_word;
	speed = transfer->speed_hz;

1006
	clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

	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;
1026
	}
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	/*
	 * if bits/word is changed in dma mode, then must check the
	 * thresholds and burst also
	 */
	if (chip->enable_dma) {
		if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
						message->spi,
						bits, &dma_burst,
						&dma_thresh))
			dev_warn_ratelimited(&message->spi->dev,
					     "pump_transfers: DMA burst size reduced to match bits_per_word\n");
1038 1039
	}

1040 1041
	message->state = RUNNING_STATE;

N
Ned Forrester 已提交
1042
	drv_data->dma_mapped = 0;
1043 1044
	if (pxa2xx_spi_dma_is_possible(drv_data->len))
		drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
N
Ned Forrester 已提交
1045
	if (drv_data->dma_mapped) {
1046 1047

		/* Ensure we have the correct interrupt handler */
1048 1049 1050
		drv_data->transfer_handler = pxa2xx_spi_dma_transfer;

		pxa2xx_spi_dma_prepare(drv_data, dma_burst);
1051

1052 1053
		/* Clear status and start DMA engine */
		cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1054
		pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1055 1056

		pxa2xx_spi_dma_start(drv_data);
1057 1058 1059 1060
	} else {
		/* Ensure we have the correct interrupt handler	*/
		drv_data->transfer_handler = interrupt_transfer;

1061 1062
		/* Clear status  */
		cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1063
		write_SSSR_CS(drv_data, drv_data->clear_sr);
1064 1065
	}

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	/* NOTE:  PXA25x_SSP _could_ use external clocking ... */
	cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
	if (!pxa25x_ssp_comp(drv_data))
		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
			drv_data->master->max_speed_hz
				/ (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
			drv_data->dma_mapped ? "DMA" : "PIO");
	else
		dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
			drv_data->master->max_speed_hz / 2
				/ (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
			drv_data->dma_mapped ? "DMA" : "PIO");

1079
	if (is_lpss_ssp(drv_data)) {
1080 1081 1082 1083 1084 1085 1086 1087
		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);
1088 1089
	}

1090
	if (is_quark_x1000_ssp(drv_data) &&
1091 1092
	    (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
		pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1093

1094
	/* see if we need to reload the config registers */
1095 1096 1097
	if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
	    || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
	    != (cr1 & change_mask)) {
1098
		/* stop the SSP, and update the other bits */
1099
		pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1100
		if (!pxa25x_ssp_comp(drv_data))
1101
			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1102
		/* first set CR1 without interrupt and service enables */
1103
		pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1104
		/* restart the SSP */
1105
		pxa2xx_spi_write(drv_data, SSCR0, cr0);
1106

1107
	} else {
1108
		if (!pxa25x_ssp_comp(drv_data))
1109
			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1110
	}
1111

1112
	cs_assert(drv_data);
1113 1114 1115

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

1119 1120
static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
					   struct spi_message *msg)
1121
{
1122
	struct driver_data *drv_data = spi_master_get_devdata(master);
1123

1124
	drv_data->cur_msg = msg;
1125 1126 1127 1128 1129 1130
	/* 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);

1131 1132
	/* prepare to setup the SSP, in pump_transfers, using the per
	 * chip configuration */
1133 1134 1135 1136 1137 1138 1139
	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;
}

1140 1141 1142 1143 1144
static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
{
	struct driver_data *drv_data = spi_master_get_devdata(master);

	/* Disable the SSP now */
1145 1146
	pxa2xx_spi_write(drv_data, SSCR0,
			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1147 1148 1149 1150

	return 0;
}

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
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) {
1174 1175
			dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
				chip_info->gpio_cs);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
			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;
}

1189 1190 1191 1192
static int setup(struct spi_device *spi)
{
	struct pxa2xx_spi_chip *chip_info = NULL;
	struct chip_data *chip;
1193
	const struct lpss_config *config;
1194
	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1195 1196
	uint tx_thres, tx_hi_thres, rx_thres;

1197 1198 1199 1200 1201 1202
	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;
1203 1204
	case LPSS_LPT_SSP:
	case LPSS_BYT_SSP:
1205
	case LPSS_BSW_SSP:
1206
	case LPSS_SPT_SSP:
1207
	case LPSS_BXT_SSP:
1208 1209 1210 1211
		config = lpss_get_config(drv_data);
		tx_thres = config->tx_threshold_lo;
		tx_hi_thres = config->tx_threshold_hi;
		rx_thres = config->rx_threshold;
1212 1213
		break;
	default:
1214 1215 1216
		tx_thres = TX_THRESH_DFLT;
		tx_hi_thres = 0;
		rx_thres = RX_THRESH_DFLT;
1217
		break;
1218
	}
1219

1220
	/* Only alloc on first setup */
1221
	chip = spi_get_ctldata(spi);
1222
	if (!chip) {
1223
		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1224
		if (!chip)
1225 1226
			return -ENOMEM;

1227 1228
		if (drv_data->ssp_type == CE4100_SSP) {
			if (spi->chip_select > 4) {
1229 1230
				dev_err(&spi->dev,
					"failed setup: cs number must not be > 4.\n");
1231 1232 1233 1234 1235 1236 1237
				kfree(chip);
				return -EINVAL;
			}

			chip->frm = spi->chip_select;
		} else
			chip->gpio_cs = -1;
1238
		chip->enable_dma = 0;
1239
		chip->timeout = TIMOUT_DFLT;
1240 1241
	}

1242 1243 1244 1245
	/* protocol drivers may change the chip settings, so...
	 * if chip_info exists, use it */
	chip_info = spi->controller_data;

1246
	/* chip_info isn't always needed */
1247
	chip->cr1 = 0;
1248
	if (chip_info) {
1249 1250 1251 1252
		if (chip_info->timeout)
			chip->timeout = chip_info->timeout;
		if (chip_info->tx_threshold)
			tx_thres = chip_info->tx_threshold;
1253 1254
		if (chip_info->tx_hi_threshold)
			tx_hi_thres = chip_info->tx_hi_threshold;
1255 1256 1257
		if (chip_info->rx_threshold)
			rx_thres = chip_info->rx_threshold;
		chip->enable_dma = drv_data->master_info->enable_dma;
1258 1259 1260
		chip->dma_threshold = 0;
		if (chip_info->enable_loopback)
			chip->cr1 = SSCR1_LBM;
1261 1262 1263 1264 1265 1266 1267
	} 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;
1268 1269
	}

1270 1271 1272 1273
	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
				| SSITF_TxHiThresh(tx_hi_thres);

1274 1275 1276 1277 1278
	/* 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 */
1279 1280
		if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
						spi->bits_per_word,
1281 1282
						&chip->dma_burst_size,
						&chip->dma_threshold)) {
1283 1284
			dev_warn(&spi->dev,
				 "in setup: DMA burst size reduced to match bits_per_word\n");
1285 1286 1287
		}
	}

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	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;
	}

1301 1302 1303
	chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
	chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
			| (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1304

1305 1306 1307
	if (spi->mode & SPI_LOOP)
		chip->cr1 |= SSCR1_LBM;

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	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) {
		chip->n_bytes = 4;
		chip->read = u32_reader;
		chip->write = u32_writer;
	}

	spi_set_ctldata(spi, chip);

1324 1325 1326
	if (drv_data->ssp_type == CE4100_SSP)
		return 0;

1327
	return setup_cs(spi, chip, chip_info);
1328 1329
}

1330
static void cleanup(struct spi_device *spi)
1331
{
1332
	struct chip_data *chip = spi_get_ctldata(spi);
1333
	struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1334

1335 1336 1337
	if (!chip)
		return;

1338
	if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
1339 1340
		gpio_free(chip->gpio_cs);

1341 1342 1343
	kfree(chip);
}

1344
#ifdef CONFIG_PCI
1345
#ifdef CONFIG_ACPI
1346

1347
static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1348 1349 1350 1351 1352
	{ "INT33C0", LPSS_LPT_SSP },
	{ "INT33C1", LPSS_LPT_SSP },
	{ "INT3430", LPSS_LPT_SSP },
	{ "INT3431", LPSS_LPT_SSP },
	{ "80860F0E", LPSS_BYT_SSP },
1353
	{ "8086228E", LPSS_BSW_SSP },
1354 1355 1356 1357
	{ },
};
MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
{
	unsigned int devid;
	int port_id = -1;

	if (adev && adev->pnp.unique_id &&
	    !kstrtouint(adev->pnp.unique_id, 0, &devid))
		port_id = devid;
	return port_id;
}
#else /* !CONFIG_ACPI */
static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
{
	return -1;
}
#endif

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
/*
 * PCI IDs of compound devices that integrate both host controller and private
 * integrated DMA engine. Please note these are not used in module
 * autoloading and probing in this module but matching the LPSS SSP type.
 */
static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
	/* SPT-LP */
	{ PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
	{ PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
	/* SPT-H */
	{ PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
	{ PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1387
	/* BXT A-Step */
1388 1389 1390
	{ PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
	{ PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
	{ PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1391 1392 1393 1394
	/* BXT B-Step */
	{ PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
	{ PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
	{ PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1395 1396 1397 1398
	/* APL */
	{ PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
	{ PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
	{ PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1399
	{ },
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
};

static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
{
	struct device *dev = param;

	if (dev != chan->device->dev->parent)
		return false;

	return true;
}

1412
static struct pxa2xx_spi_master *
1413
pxa2xx_spi_init_pdata(struct platform_device *pdev)
1414 1415 1416 1417 1418
{
	struct pxa2xx_spi_master *pdata;
	struct acpi_device *adev;
	struct ssp_device *ssp;
	struct resource *res;
1419 1420
	const struct acpi_device_id *adev_id = NULL;
	const struct pci_device_id *pcidev_id = NULL;
1421
	int type;
1422

1423
	adev = ACPI_COMPANION(&pdev->dev);
1424

1425 1426 1427
	if (dev_is_pci(pdev->dev.parent))
		pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
					 to_pci_dev(pdev->dev.parent));
1428
	else if (adev)
1429 1430
		adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
					    &pdev->dev);
1431 1432
	else
		return NULL;
1433 1434 1435 1436 1437

	if (adev_id)
		type = (int)adev_id->driver_data;
	else if (pcidev_id)
		type = (int)pcidev_id->driver_data;
1438 1439 1440
	else
		return NULL;

1441
	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1442
	if (!pdata)
1443 1444 1445 1446 1447 1448 1449 1450 1451
		return NULL;

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

	ssp = &pdata->ssp;

	ssp->phys_base = res->start;
1452 1453
	ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(ssp->mmio_base))
1454
		return NULL;
1455

1456 1457 1458 1459 1460 1461
	if (pcidev_id) {
		pdata->tx_param = pdev->dev.parent;
		pdata->rx_param = pdev->dev.parent;
		pdata->dma_filter = pxa2xx_spi_idma_filter;
	}

1462 1463
	ssp->clk = devm_clk_get(&pdev->dev, NULL);
	ssp->irq = platform_get_irq(pdev, 0);
1464
	ssp->type = type;
1465
	ssp->pdev = pdev;
1466
	ssp->port_id = pxa2xx_spi_get_port_id(adev);
1467 1468

	pdata->num_chipselect = 1;
1469
	pdata->enable_dma = true;
1470 1471 1472 1473

	return pdata;
}

1474
#else /* !CONFIG_PCI */
1475
static inline struct pxa2xx_spi_master *
1476
pxa2xx_spi_init_pdata(struct platform_device *pdev)
1477 1478 1479 1480 1481
{
	return NULL;
}
#endif

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
{
	struct driver_data *drv_data = spi_master_get_devdata(master);

	if (has_acpi_companion(&drv_data->pdev->dev)) {
		switch (drv_data->ssp_type) {
		/*
		 * For Atoms the ACPI DeviceSelection used by the Windows
		 * driver starts from 1 instead of 0 so translate it here
		 * to match what Linux expects.
		 */
		case LPSS_BYT_SSP:
1494
		case LPSS_BSW_SSP:
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
			return cs - 1;

		default:
			break;
		}
	}

	return cs;
}

1505
static int pxa2xx_spi_probe(struct platform_device *pdev)
1506 1507 1508 1509
{
	struct device *dev = &pdev->dev;
	struct pxa2xx_spi_master *platform_info;
	struct spi_master *master;
G
Guennadi Liakhovetski 已提交
1510
	struct driver_data *drv_data;
1511
	struct ssp_device *ssp;
1512
	const struct lpss_config *config;
G
Guennadi Liakhovetski 已提交
1513
	int status;
1514
	u32 tmp;
1515

1516 1517
	platform_info = dev_get_platdata(dev);
	if (!platform_info) {
1518
		platform_info = pxa2xx_spi_init_pdata(pdev);
1519 1520 1521 1522
		if (!platform_info) {
			dev_err(&pdev->dev, "missing platform data\n");
			return -ENODEV;
		}
1523
	}
1524

H
Haojian Zhuang 已提交
1525
	ssp = pxa_ssp_request(pdev->id, pdev->name);
1526 1527 1528 1529 1530
	if (!ssp)
		ssp = &platform_info->ssp;

	if (!ssp->mmio_base) {
		dev_err(&pdev->dev, "failed to get ssp\n");
1531 1532 1533
		return -ENODEV;
	}

1534
	master = spi_alloc_master(dev, sizeof(struct driver_data));
1535
	if (!master) {
G
Guennadi Liakhovetski 已提交
1536
		dev_err(&pdev->dev, "cannot alloc spi_master\n");
H
Haojian Zhuang 已提交
1537
		pxa_ssp_free(ssp);
1538 1539 1540 1541 1542 1543
		return -ENOMEM;
	}
	drv_data = spi_master_get_devdata(master);
	drv_data->master = master;
	drv_data->master_info = platform_info;
	drv_data->pdev = pdev;
1544
	drv_data->ssp = ssp;
1545

1546 1547
	master->dev.parent = &pdev->dev;
	master->dev.of_node = pdev->dev.of_node;
1548
	/* the spi->mode bits understood by this driver: */
1549
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1550

1551
	master->bus_num = ssp->port_id;
1552
	master->dma_alignment = DMA_ALIGNMENT;
1553 1554
	master->cleanup = cleanup;
	master->setup = setup;
1555
	master->transfer_one_message = pxa2xx_spi_transfer_one_message;
1556
	master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1557
	master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1558
	master->auto_runtime_pm = true;
1559

1560
	drv_data->ssp_type = ssp->type;
1561

1562 1563
	drv_data->ioaddr = ssp->mmio_base;
	drv_data->ssdr_physical = ssp->phys_base + SSDR;
1564
	if (pxa25x_ssp_comp(drv_data)) {
1565 1566 1567 1568 1569 1570 1571 1572 1573
		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;
		}

1574 1575 1576 1577 1578
		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 {
1579
		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1580
		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1581
		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1582 1583 1584 1585
		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
	}

1586 1587
	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
			drv_data);
1588
	if (status < 0) {
G
Guennadi Liakhovetski 已提交
1589
		dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1590 1591 1592 1593 1594
		goto out_error_master_alloc;
	}

	/* Setup DMA if requested */
	if (platform_info->enable_dma) {
1595 1596
		status = pxa2xx_spi_dma_setup(drv_data);
		if (status) {
1597
			dev_dbg(dev, "no DMA channels available, using PIO\n");
1598
			platform_info->enable_dma = false;
1599 1600 1601 1602
		}
	}

	/* Enable SOC clock */
1603 1604
	clk_prepare_enable(ssp->clk);

1605
	master->max_speed_hz = clk_get_rate(ssp->clk);
1606 1607

	/* Load default SSP configuration */
1608
	pxa2xx_spi_write(drv_data, SSCR0, 0);
1609 1610
	switch (drv_data->ssp_type) {
	case QUARK_X1000_SSP:
1611 1612 1613
		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);
1614 1615

		/* using the Motorola SPI protocol and use 8 bit frame */
1616 1617 1618
		pxa2xx_spi_write(drv_data, SSCR0,
				 QUARK_X1000_SSCR0_Motorola
				 | QUARK_X1000_SSCR0_DataSize(8));
1619 1620
		break;
	default:
1621 1622 1623 1624 1625
		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);
1626 1627 1628
		break;
	}

1629
	if (!pxa25x_ssp_comp(drv_data))
1630
		pxa2xx_spi_write(drv_data, SSTO, 0);
1631 1632

	if (!is_quark_x1000_ssp(drv_data))
1633
		pxa2xx_spi_write(drv_data, SSPSP, 0);
1634

1635 1636 1637 1638 1639 1640 1641 1642 1643
	if (is_lpss_ssp(drv_data)) {
		lpss_ssp_setup(drv_data);
		config = lpss_get_config(drv_data);
		if (config->reg_capabilities >= 0) {
			tmp = __lpss_ssp_read_priv(drv_data,
						   config->reg_capabilities);
			tmp &= LPSS_CAPS_CS_EN_MASK;
			tmp >>= LPSS_CAPS_CS_EN_SHIFT;
			platform_info->num_chipselect = ffz(tmp);
1644 1645
		} else if (config->cs_num) {
			platform_info->num_chipselect = config->cs_num;
1646 1647 1648 1649
		}
	}
	master->num_chipselect = platform_info->num_chipselect;

1650 1651
	tasklet_init(&drv_data->pump_transfers, pump_transfers,
		     (unsigned long)drv_data);
1652

1653 1654 1655 1656 1657
	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);

1658 1659
	/* Register with the SPI framework */
	platform_set_drvdata(pdev, drv_data);
1660
	status = devm_spi_register_master(&pdev->dev, master);
1661 1662
	if (status != 0) {
		dev_err(&pdev->dev, "problem registering spi master\n");
1663
		goto out_error_clock_enabled;
1664 1665 1666 1667 1668
	}

	return status;

out_error_clock_enabled:
1669
	clk_disable_unprepare(ssp->clk);
1670
	pxa2xx_spi_dma_release(drv_data);
1671
	free_irq(ssp->irq, drv_data);
1672 1673 1674

out_error_master_alloc:
	spi_master_put(master);
H
Haojian Zhuang 已提交
1675
	pxa_ssp_free(ssp);
1676 1677 1678 1679 1680 1681
	return status;
}

static int pxa2xx_spi_remove(struct platform_device *pdev)
{
	struct driver_data *drv_data = platform_get_drvdata(pdev);
1682
	struct ssp_device *ssp;
1683 1684 1685

	if (!drv_data)
		return 0;
1686
	ssp = drv_data->ssp;
1687

1688 1689
	pm_runtime_get_sync(&pdev->dev);

1690
	/* Disable the SSP at the peripheral and SOC level */
1691
	pxa2xx_spi_write(drv_data, SSCR0, 0);
1692
	clk_disable_unprepare(ssp->clk);
1693 1694

	/* Release DMA */
1695 1696
	if (drv_data->master_info->enable_dma)
		pxa2xx_spi_dma_release(drv_data);
1697

1698 1699 1700
	pm_runtime_put_noidle(&pdev->dev);
	pm_runtime_disable(&pdev->dev);

1701
	/* Release IRQ */
1702 1703 1704
	free_irq(ssp->irq, drv_data);

	/* Release SSP */
H
Haojian Zhuang 已提交
1705
	pxa_ssp_free(ssp);
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717

	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);
}

1718
#ifdef CONFIG_PM_SLEEP
1719
static int pxa2xx_spi_suspend(struct device *dev)
1720
{
1721
	struct driver_data *drv_data = dev_get_drvdata(dev);
1722
	struct ssp_device *ssp = drv_data->ssp;
1723 1724
	int status = 0;

1725
	status = spi_master_suspend(drv_data->master);
1726 1727
	if (status != 0)
		return status;
1728
	pxa2xx_spi_write(drv_data, SSCR0, 0);
1729 1730 1731

	if (!pm_runtime_suspended(dev))
		clk_disable_unprepare(ssp->clk);
1732 1733 1734 1735

	return 0;
}

1736
static int pxa2xx_spi_resume(struct device *dev)
1737
{
1738
	struct driver_data *drv_data = dev_get_drvdata(dev);
1739
	struct ssp_device *ssp = drv_data->ssp;
1740 1741 1742
	int status = 0;

	/* Enable the SSP clock */
1743 1744
	if (!pm_runtime_suspended(dev))
		clk_prepare_enable(ssp->clk);
1745

1746
	/* Restore LPSS private register bits */
1747 1748
	if (is_lpss_ssp(drv_data))
		lpss_ssp_setup(drv_data);
1749

1750
	/* Start the queue running */
1751
	status = spi_master_resume(drv_data->master);
1752
	if (status != 0) {
1753
		dev_err(dev, "problem starting queue (%d)\n", status);
1754 1755 1756 1757 1758
		return status;
	}

	return 0;
}
1759 1760
#endif

1761
#ifdef CONFIG_PM
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
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
1778

1779
static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1780 1781 1782
	SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
	SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
			   pxa2xx_spi_runtime_resume, NULL)
1783
};
1784 1785 1786

static struct platform_driver driver = {
	.driver = {
1787 1788
		.name	= "pxa2xx-spi",
		.pm	= &pxa2xx_spi_pm_ops,
1789
		.acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1790
	},
1791
	.probe = pxa2xx_spi_probe,
1792
	.remove = pxa2xx_spi_remove,
1793 1794 1795 1796 1797
	.shutdown = pxa2xx_spi_shutdown,
};

static int __init pxa2xx_spi_init(void)
{
1798
	return platform_driver_register(&driver);
1799
}
A
Antonio Ospite 已提交
1800
subsys_initcall(pxa2xx_spi_init);
1801 1802 1803 1804 1805 1806

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