spi-topcliff-pch.c 48.0 KB
Newer Older
1 2
/*
 * SPI bus driver for the Topcliff PCH used by Intel SoCs
3
 *
4
 * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 */

20
#include <linux/delay.h>
21 22 23 24 25 26 27 28
#include <linux/pci.h>
#include <linux/wait.h>
#include <linux/spi/spi.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/spi/spidev.h>
#include <linux/module.h>
#include <linux/device.h>
29
#include <linux/platform_device.h>
30

T
Tomoya MORINAGA 已提交
31 32 33
#include <linux/dmaengine.h>
#include <linux/pch_dma.h>

34 35 36 37 38 39 40 41
/* Register offsets */
#define PCH_SPCR		0x00	/* SPI control register */
#define PCH_SPBRR		0x04	/* SPI baud rate register */
#define PCH_SPSR		0x08	/* SPI status register */
#define PCH_SPDWR		0x0C	/* SPI write data register */
#define PCH_SPDRR		0x10	/* SPI read data register */
#define PCH_SSNXCR		0x18	/* SSN Expand Control Register */
#define PCH_SRST		0x1C	/* SPI reset register */
T
Tomoya MORINAGA 已提交
42
#define PCH_ADDRESS_SIZE	0x20
43 44 45 46 47 48 49 50 51 52

#define PCH_SPSR_TFD		0x000007C0
#define PCH_SPSR_RFD		0x0000F800

#define PCH_READABLE(x)		(((x) & PCH_SPSR_RFD)>>11)
#define PCH_WRITABLE(x)		(((x) & PCH_SPSR_TFD)>>6)

#define PCH_RX_THOLD		7
#define PCH_RX_THOLD_MAX	15

53 54
#define PCH_TX_THOLD		2

55 56 57 58 59 60 61 62
#define PCH_MAX_BAUDRATE	5000000
#define PCH_MAX_FIFO_DEPTH	16

#define STATUS_RUNNING		1
#define STATUS_EXITING		2
#define PCH_SLEEP_TIME		10

#define SSN_LOW			0x02U
63
#define SSN_HIGH		0x03U
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#define SSN_NO_CONTROL		0x00U
#define PCH_MAX_CS		0xFF
#define PCI_DEVICE_ID_GE_SPI	0x8816

#define SPCR_SPE_BIT		(1 << 0)
#define SPCR_MSTR_BIT		(1 << 1)
#define SPCR_LSBF_BIT		(1 << 4)
#define SPCR_CPHA_BIT		(1 << 5)
#define SPCR_CPOL_BIT		(1 << 6)
#define SPCR_TFIE_BIT		(1 << 8)
#define SPCR_RFIE_BIT		(1 << 9)
#define SPCR_FIE_BIT		(1 << 10)
#define SPCR_ORIE_BIT		(1 << 11)
#define SPCR_MDFIE_BIT		(1 << 12)
#define SPCR_FICLR_BIT		(1 << 24)
#define SPSR_TFI_BIT		(1 << 0)
#define SPSR_RFI_BIT		(1 << 1)
#define SPSR_FI_BIT		(1 << 2)
T
Tomoya MORINAGA 已提交
82
#define SPSR_ORF_BIT		(1 << 3)
83 84
#define SPBRR_SIZE_BIT		(1 << 10)

85 86
#define PCH_ALL			(SPCR_TFIE_BIT|SPCR_RFIE_BIT|SPCR_FIE_BIT|\
				SPCR_ORIE_BIT|SPCR_MDFIE_BIT)
87

88 89 90
#define SPCR_RFIC_FIELD		20
#define SPCR_TFIC_FIELD		16

T
Tomoya MORINAGA 已提交
91 92 93
#define MASK_SPBRR_SPBR_BITS	((1 << 10) - 1)
#define MASK_RFIC_SPCR_BITS	(0xf << SPCR_RFIC_FIELD)
#define MASK_TFIC_SPCR_BITS	(0xf << SPCR_TFIC_FIELD)
94 95 96 97

#define PCH_CLOCK_HZ		50000000
#define PCH_MAX_SPBR		1023

98
/* Definition for ML7213/ML7223/ML7831 by LAPIS Semiconductor */
99 100
#define PCI_VENDOR_ID_ROHM		0x10DB
#define PCI_DEVICE_ID_ML7213_SPI	0x802c
101
#define PCI_DEVICE_ID_ML7223_SPI	0x800F
102
#define PCI_DEVICE_ID_ML7831_SPI	0x8816
103 104 105 106

/*
 * Set the number of SPI instance max
 * Intel EG20T PCH :		1ch
107 108 109
 * LAPIS Semiconductor ML7213 IOH :	2ch
 * LAPIS Semiconductor ML7223 IOH :	1ch
 * LAPIS Semiconductor ML7831 IOH :	1ch
110 111
*/
#define PCH_SPI_MAX_DEV			2
112

T
Tomoya MORINAGA 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#define PCH_BUF_SIZE		4096
#define PCH_DMA_TRANS_SIZE	12

static int use_dma = 1;

struct pch_spi_dma_ctrl {
	struct dma_async_tx_descriptor	*desc_tx;
	struct dma_async_tx_descriptor	*desc_rx;
	struct pch_dma_slave		param_tx;
	struct pch_dma_slave		param_rx;
	struct dma_chan		*chan_tx;
	struct dma_chan		*chan_rx;
	struct scatterlist		*sg_tx_p;
	struct scatterlist		*sg_rx_p;
	struct scatterlist		sg_tx;
	struct scatterlist		sg_rx;
	int				nent;
	void				*tx_buf_virt;
	void				*rx_buf_virt;
	dma_addr_t			tx_buf_dma;
	dma_addr_t			rx_buf_dma;
};
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/**
 * struct pch_spi_data - Holds the SPI channel specific details
 * @io_remap_addr:		The remapped PCI base address
 * @master:			Pointer to the SPI master structure
 * @work:			Reference to work queue handler
 * @wk:				Workqueue for carrying out execution of the
 *				requests
 * @wait:			Wait queue for waking up upon receiving an
 *				interrupt.
 * @transfer_complete:		Status of SPI Transfer
 * @bcurrent_msg_processing:	Status flag for message processing
 * @lock:			Lock for protecting this structure
 * @queue:			SPI Message queue
 * @status:			Status of the SPI driver
 * @bpw_len:			Length of data to be transferred in bits per
 *				word
 * @transfer_active:		Flag showing active transfer
 * @tx_index:			Transmit data count; for bookkeeping during
 *				transfer
 * @rx_index:			Receive data count; for bookkeeping during
 *				transfer
 * @tx_buff:			Buffer for data to be transmitted
 * @rx_index:			Buffer for Received data
 * @n_curnt_chip:		The chip number that this SPI driver currently
 *				operates on
 * @current_chip:		Reference to the current chip that this SPI
 *				driver currently operates on
 * @current_msg:		The current message that this SPI driver is
 *				handling
 * @cur_trans:			The current transfer that this SPI driver is
 *				handling
 * @board_dat:			Reference to the SPI device data structure
167 168 169
 * @plat_dev:			platform_device structure
 * @ch:				SPI channel number
 * @irq_reg_sts:		Status of IRQ registration
170 171 172
 */
struct pch_spi_data {
	void __iomem *io_remap_addr;
T
Tomoya MORINAGA 已提交
173
	unsigned long io_base_addr;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	struct spi_master *master;
	struct work_struct work;
	struct workqueue_struct *wk;
	wait_queue_head_t wait;
	u8 transfer_complete;
	u8 bcurrent_msg_processing;
	spinlock_t lock;
	struct list_head queue;
	u8 status;
	u32 bpw_len;
	u8 transfer_active;
	u32 tx_index;
	u32 rx_index;
	u16 *pkt_tx_buff;
	u16 *pkt_rx_buff;
	u8 n_curnt_chip;
	struct spi_device *current_chip;
	struct spi_message *current_msg;
	struct spi_transfer *cur_trans;
	struct pch_spi_board_data *board_dat;
194 195
	struct platform_device	*plat_dev;
	int ch;
T
Tomoya MORINAGA 已提交
196 197
	struct pch_spi_dma_ctrl dma;
	int use_dma;
198
	u8 irq_reg_sts;
199
	int save_total_len;
200 201 202 203 204 205
};

/**
 * struct pch_spi_board_data - Holds the SPI device specific details
 * @pdev:		Pointer to the PCI device
 * @suspend_sts:	Status of suspend
206
 * @num:		The number of SPI device instance
207 208 209 210
 */
struct pch_spi_board_data {
	struct pci_dev *pdev;
	u8 suspend_sts;
211 212 213 214 215 216 217
	int num;
};

struct pch_pd_dev_save {
	int num;
	struct platform_device *pd_save[PCH_SPI_MAX_DEV];
	struct pch_spi_board_data *board_dat;
218 219
};

220
static DEFINE_PCI_DEVICE_TABLE(pch_spi_pcidev_id) = {
221 222
	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_GE_SPI),    1, },
	{ PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_SPI), 2, },
223
	{ PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_SPI), 1, },
224
	{ PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_SPI), 1, },
225
	{ }
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
};

/**
 * pch_spi_writereg() - Performs  register writes
 * @master:	Pointer to struct spi_master.
 * @idx:	Register offset.
 * @val:	Value to be written to register.
 */
static inline void pch_spi_writereg(struct spi_master *master, int idx, u32 val)
{
	struct pch_spi_data *data = spi_master_get_devdata(master);
	iowrite32(val, (data->io_remap_addr + idx));
}

/**
 * pch_spi_readreg() - Performs register reads
 * @master:	Pointer to struct spi_master.
 * @idx:	Register offset.
 */
static inline u32 pch_spi_readreg(struct spi_master *master, int idx)
{
	struct pch_spi_data *data = spi_master_get_devdata(master);
	return ioread32(data->io_remap_addr + idx);
}

static inline void pch_spi_setclr_reg(struct spi_master *master, int idx,
				      u32 set, u32 clr)
{
	u32 tmp = pch_spi_readreg(master, idx);
	tmp = (tmp & ~clr) | set;
	pch_spi_writereg(master, idx, tmp);
}

static void pch_spi_set_master_mode(struct spi_master *master)
{
	pch_spi_setclr_reg(master, PCH_SPCR, SPCR_MSTR_BIT, 0);
}

/**
 * pch_spi_clear_fifo() - Clears the Transmit and Receive FIFOs
 * @master:	Pointer to struct spi_master.
 */
static void pch_spi_clear_fifo(struct spi_master *master)
{
	pch_spi_setclr_reg(master, PCH_SPCR, SPCR_FICLR_BIT, 0);
	pch_spi_setclr_reg(master, PCH_SPCR, 0, SPCR_FICLR_BIT);
}

static void pch_spi_handler_sub(struct pch_spi_data *data, u32 reg_spsr_val,
				void __iomem *io_remap_addr)
{
	u32 n_read, tx_index, rx_index, bpw_len;
	u16 *pkt_rx_buffer, *pkt_tx_buff;
	int read_cnt;
	u32 reg_spcr_val;
	void __iomem *spsr;
	void __iomem *spdrr;
	void __iomem *spdwr;

	spsr = io_remap_addr + PCH_SPSR;
	iowrite32(reg_spsr_val, spsr);

	if (data->transfer_active) {
		rx_index = data->rx_index;
		tx_index = data->tx_index;
		bpw_len = data->bpw_len;
		pkt_rx_buffer = data->pkt_rx_buff;
		pkt_tx_buff = data->pkt_tx_buff;

		spdrr = io_remap_addr + PCH_SPDRR;
		spdwr = io_remap_addr + PCH_SPDWR;

		n_read = PCH_READABLE(reg_spsr_val);

		for (read_cnt = 0; (read_cnt < n_read); read_cnt++) {
			pkt_rx_buffer[rx_index++] = ioread32(spdrr);
			if (tx_index < bpw_len)
				iowrite32(pkt_tx_buff[tx_index++], spdwr);
		}

		/* disable RFI if not needed */
		if ((bpw_len - rx_index) <= PCH_MAX_FIFO_DEPTH) {
			reg_spcr_val = ioread32(io_remap_addr + PCH_SPCR);
309
			reg_spcr_val &= ~SPCR_RFIE_BIT; /* disable RFI */
310 311

			/* reset rx threshold */
T
Tomoya MORINAGA 已提交
312
			reg_spcr_val &= ~MASK_RFIC_SPCR_BITS;
313
			reg_spcr_val |= (PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD);
T
Tomoya MORINAGA 已提交
314 315

			iowrite32(reg_spcr_val, (io_remap_addr + PCH_SPCR));
316 317 318 319 320 321
		}

		/* update counts */
		data->tx_index = tx_index;
		data->rx_index = rx_index;

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		/* if transfer complete interrupt */
		if (reg_spsr_val & SPSR_FI_BIT) {
			if ((tx_index == bpw_len) && (rx_index == tx_index)) {
				/* disable interrupts */
				pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
						   PCH_ALL);

				/* transfer is completed;
				   inform pch_spi_process_messages */
				data->transfer_complete = true;
				data->transfer_active = false;
				wake_up(&data->wait);
			} else {
				dev_err(&data->master->dev,
					"%s : Transfer is not completed",
					__func__);
			}
339
		}
340 341 342 343 344 345 346 347 348 349 350 351 352 353
	}
}

/**
 * pch_spi_handler() - Interrupt handler
 * @irq:	The interrupt number.
 * @dev_id:	Pointer to struct pch_spi_board_data.
 */
static irqreturn_t pch_spi_handler(int irq, void *dev_id)
{
	u32 reg_spsr_val;
	void __iomem *spsr;
	void __iomem *io_remap_addr;
	irqreturn_t ret = IRQ_NONE;
354 355
	struct pch_spi_data *data = dev_id;
	struct pch_spi_board_data *board_dat = data->board_dat;
356 357 358 359 360 361 362 363 364 365 366 367

	if (board_dat->suspend_sts) {
		dev_dbg(&board_dat->pdev->dev,
			"%s returning due to suspend\n", __func__);
		return IRQ_NONE;
	}

	io_remap_addr = data->io_remap_addr;
	spsr = io_remap_addr + PCH_SPSR;

	reg_spsr_val = ioread32(spsr);

368 369
	if (reg_spsr_val & SPSR_ORF_BIT) {
		dev_err(&board_dat->pdev->dev, "%s Over run error\n", __func__);
370
		if (data->current_msg->complete) {
371 372 373 374 375 376 377 378 379 380 381
			data->transfer_complete = true;
			data->current_msg->status = -EIO;
			data->current_msg->complete(data->current_msg->context);
			data->bcurrent_msg_processing = false;
			data->current_msg = NULL;
			data->cur_trans = NULL;
		}
	}

	if (data->use_dma)
		return IRQ_NONE;
T
Tomoya MORINAGA 已提交
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	/* Check if the interrupt is for SPI device */
	if (reg_spsr_val & (SPSR_FI_BIT | SPSR_RFI_BIT)) {
		pch_spi_handler_sub(data, reg_spsr_val, io_remap_addr);
		ret = IRQ_HANDLED;
	}

	dev_dbg(&board_dat->pdev->dev, "%s EXIT return value=%d\n",
		__func__, ret);

	return ret;
}

/**
 * pch_spi_set_baud_rate() - Sets SPBR field in SPBRR
 * @master:	Pointer to struct spi_master.
 * @speed_hz:	Baud rate.
 */
static void pch_spi_set_baud_rate(struct spi_master *master, u32 speed_hz)
{
402
	u32 n_spbr = PCH_CLOCK_HZ / (speed_hz * 2);
403 404 405 406 407

	/* if baud rate is less than we can support limit it */
	if (n_spbr > PCH_MAX_SPBR)
		n_spbr = PCH_MAX_SPBR;

T
Tomoya MORINAGA 已提交
408
	pch_spi_setclr_reg(master, PCH_SPBRR, n_spbr, MASK_SPBRR_SPBR_BITS);
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
}

/**
 * pch_spi_set_bits_per_word() - Sets SIZE field in SPBRR
 * @master:		Pointer to struct spi_master.
 * @bits_per_word:	Bits per word for SPI transfer.
 */
static void pch_spi_set_bits_per_word(struct spi_master *master,
				      u8 bits_per_word)
{
	if (bits_per_word == 8)
		pch_spi_setclr_reg(master, PCH_SPBRR, 0, SPBRR_SIZE_BIT);
	else
		pch_spi_setclr_reg(master, PCH_SPBRR, SPBRR_SIZE_BIT, 0);
}

/**
 * pch_spi_setup_transfer() - Configures the PCH SPI hardware for transfer
 * @spi:	Pointer to struct spi_device.
 */
static void pch_spi_setup_transfer(struct spi_device *spi)
{
431
	u32 flags = 0;
432 433 434 435 436 437 438 439 440

	dev_dbg(&spi->dev, "%s SPBRR content =%x setting baud rate=%d\n",
		__func__, pch_spi_readreg(spi->master, PCH_SPBRR),
		spi->max_speed_hz);
	pch_spi_set_baud_rate(spi->master, spi->max_speed_hz);

	/* set bits per word */
	pch_spi_set_bits_per_word(spi->master, spi->bits_per_word);

441 442
	if (!(spi->mode & SPI_LSB_FIRST))
		flags |= SPCR_LSBF_BIT;
443
	if (spi->mode & SPI_CPOL)
444
		flags |= SPCR_CPOL_BIT;
445
	if (spi->mode & SPI_CPHA)
446 447 448
		flags |= SPCR_CPHA_BIT;
	pch_spi_setclr_reg(spi->master, PCH_SPCR, flags,
			   (SPCR_LSBF_BIT | SPCR_CPOL_BIT | SPCR_CPHA_BIT));
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	/* Clear the FIFO by toggling  FICLR to 1 and back to 0 */
	pch_spi_clear_fifo(spi->master);
}

/**
 * pch_spi_reset() - Clears SPI registers
 * @master:	Pointer to struct spi_master.
 */
static void pch_spi_reset(struct spi_master *master)
{
	/* write 1 to reset SPI */
	pch_spi_writereg(master, PCH_SRST, 0x1);

	/* clear reset */
	pch_spi_writereg(master, PCH_SRST, 0x0);
}

static int pch_spi_setup(struct spi_device *pspi)
{
	/* check bits per word */
470
	if (pspi->bits_per_word == 0) {
471 472 473 474 475 476 477 478 479 480 481
		pspi->bits_per_word = 8;
		dev_dbg(&pspi->dev, "%s 8 bits per word\n", __func__);
	}

	/* Check baud rate setting */
	/* if baud rate of chip is greater than
	   max we can support,return error */
	if ((pspi->max_speed_hz) > PCH_MAX_BAUDRATE)
		pspi->max_speed_hz = PCH_MAX_BAUDRATE;

	dev_dbg(&pspi->dev, "%s MODE = %x\n", __func__,
482
		(pspi->mode) & (SPI_CPOL | SPI_CPHA));
483 484 485 486 487 488 489 490 491 492 493 494 495

	return 0;
}

static int pch_spi_transfer(struct spi_device *pspi, struct spi_message *pmsg)
{

	struct spi_transfer *transfer;
	struct pch_spi_data *data = spi_master_get_devdata(pspi->master);
	int retval;
	unsigned long flags;

	/* validate spi message and baud rate */
496 497 498 499 500
	if (unlikely(list_empty(&pmsg->transfers) == 1)) {
		dev_err(&pspi->dev, "%s list empty\n", __func__);
		retval = -EINVAL;
		goto err_out;
	}
501

502
	if (unlikely(pspi->max_speed_hz == 0)) {
503
		dev_err(&pspi->dev, "%s pch_spi_transfer maxspeed=%d\n",
504
			__func__, pspi->max_speed_hz);
505 506 507 508
		retval = -EINVAL;
		goto err_out;
	}

509 510
	dev_dbg(&pspi->dev,
		"%s Transfer List not empty. Transfer Speed is set.\n", __func__);
511

T
Tomoya MORINAGA 已提交
512
	spin_lock_irqsave(&data->lock, flags);
513 514
	/* validate Tx/Rx buffers and Transfer length */
	list_for_each_entry(transfer, &pmsg->transfers, transfer_list) {
515
		if (!transfer->tx_buf && !transfer->rx_buf) {
516 517 518
			dev_err(&pspi->dev,
				"%s Tx and Rx buffer NULL\n", __func__);
			retval = -EINVAL;
T
Tomoya MORINAGA 已提交
519
			goto err_return_spinlock;
520 521
		}

522
		if (!transfer->len) {
523 524 525
			dev_err(&pspi->dev, "%s Transfer length invalid\n",
				__func__);
			retval = -EINVAL;
T
Tomoya MORINAGA 已提交
526
			goto err_return_spinlock;
527 528
		}

529 530 531
		dev_dbg(&pspi->dev,
			"%s Tx/Rx buffer valid. Transfer length valid\n",
			__func__);
532

T
Tomoya MORINAGA 已提交
533
		/* if baud rate has been specified validate the same */
534 535
		if (transfer->speed_hz > PCH_MAX_BAUDRATE)
			transfer->speed_hz = PCH_MAX_BAUDRATE;
536
	}
T
Tomoya MORINAGA 已提交
537
	spin_unlock_irqrestore(&data->lock, flags);
538

539 540
	/* We won't process any messages if we have been asked to terminate */
	if (data->status == STATUS_EXITING) {
541 542
		dev_err(&pspi->dev, "%s status = STATUS_EXITING.\n", __func__);
		retval = -ESHUTDOWN;
T
Tomoya MORINAGA 已提交
543
		goto err_out;
544 545 546 547
	}

	/* If suspended ,return -EINVAL */
	if (data->board_dat->suspend_sts) {
548
		dev_err(&pspi->dev, "%s suspend; returning EINVAL\n", __func__);
549
		retval = -EINVAL;
T
Tomoya MORINAGA 已提交
550
		goto err_out;
551 552 553 554 555 556 557
	}

	/* set status of message */
	pmsg->actual_length = 0;
	dev_dbg(&pspi->dev, "%s - pmsg->status =%d\n", __func__, pmsg->status);

	pmsg->status = -EINPROGRESS;
T
Tomoya MORINAGA 已提交
558
	spin_lock_irqsave(&data->lock, flags);
559 560
	/* add message to queue */
	list_add_tail(&pmsg->queue, &data->queue);
T
Tomoya MORINAGA 已提交
561 562
	spin_unlock_irqrestore(&data->lock, flags);

563 564 565 566 567 568 569 570 571 572 573
	dev_dbg(&pspi->dev, "%s - Invoked list_add_tail\n", __func__);

	/* schedule work queue to run */
	queue_work(data->wk, &data->work);
	dev_dbg(&pspi->dev, "%s - Invoked queue work\n", __func__);

	retval = 0;

err_out:
	dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
	return retval;
T
Tomoya MORINAGA 已提交
574 575 576 577
err_return_spinlock:
	dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
	spin_unlock_irqrestore(&data->lock, flags);
	return retval;
578 579 580 581 582
}

static inline void pch_spi_select_chip(struct pch_spi_data *data,
				       struct spi_device *pspi)
{
583 584 585
	if (data->current_chip != NULL) {
		if (pspi->chip_select != data->n_curnt_chip) {
			dev_dbg(&pspi->dev, "%s : different slave\n", __func__);
586 587 588 589 590 591 592 593 594 595 596 597
			data->current_chip = NULL;
		}
	}

	data->current_chip = pspi;

	data->n_curnt_chip = data->current_chip->chip_select;

	dev_dbg(&pspi->dev, "%s :Invoking pch_spi_setup_transfer\n", __func__);
	pch_spi_setup_transfer(pspi);
}

T
Tomoya MORINAGA 已提交
598
static void pch_spi_set_tx(struct pch_spi_data *data, int *bpw)
599 600 601 602
{
	int size;
	u32 n_writes;
	int j;
603
	struct spi_message *pmsg, *tmp;
604 605 606 607 608
	const u8 *tx_buf;
	const u16 *tx_sbuf;

	/* set baud rate if needed */
	if (data->cur_trans->speed_hz) {
609 610
		dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
		pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
611 612 613
	}

	/* set bits per word if needed */
614 615 616
	if (data->cur_trans->bits_per_word &&
	    (data->current_msg->spi->bits_per_word != data->cur_trans->bits_per_word)) {
		dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
617
		pch_spi_set_bits_per_word(data->master,
618
					  data->cur_trans->bits_per_word);
619 620 621 622 623 624 625 626 627 628 629 630
		*bpw = data->cur_trans->bits_per_word;
	} else {
		*bpw = data->current_msg->spi->bits_per_word;
	}

	/* reset Tx/Rx index */
	data->tx_index = 0;
	data->rx_index = 0;

	data->bpw_len = data->cur_trans->len / (*bpw / 8);

	/* find alloc size */
631 632
	size = data->cur_trans->len * sizeof(*data->pkt_tx_buff);

633 634 635 636
	/* allocate memory for pkt_tx_buff & pkt_rx_buffer */
	data->pkt_tx_buff = kzalloc(size, GFP_KERNEL);
	if (data->pkt_tx_buff != NULL) {
		data->pkt_rx_buff = kzalloc(size, GFP_KERNEL);
637
		if (!data->pkt_rx_buff)
638 639 640
			kfree(data->pkt_tx_buff);
	}

641
	if (!data->pkt_rx_buff) {
642
		/* flush queue and set status of all transfers to -ENOMEM */
643
		dev_err(&data->master->dev, "%s :kzalloc failed\n", __func__);
644
		list_for_each_entry_safe(pmsg, tmp, data->queue.next, queue) {
645 646
			pmsg->status = -ENOMEM;

647
			if (pmsg->complete)
648 649 650 651 652 653 654 655 656
				pmsg->complete(pmsg->context);

			/* delete from queue */
			list_del_init(&pmsg->queue);
		}
		return;
	}

	/* copy Tx Data */
657
	if (data->cur_trans->tx_buf != NULL) {
658
		if (*bpw == 8) {
659 660 661
			tx_buf = data->cur_trans->tx_buf;
			for (j = 0; j < data->bpw_len; j++)
				data->pkt_tx_buff[j] = *tx_buf++;
662
		} else {
663 664 665
			tx_sbuf = data->cur_trans->tx_buf;
			for (j = 0; j < data->bpw_len; j++)
				data->pkt_tx_buff[j] = *tx_sbuf++;
666 667 668 669
		}
	}

	/* if len greater than PCH_MAX_FIFO_DEPTH, write 16,else len bytes */
670 671
	n_writes = data->bpw_len;
	if (n_writes > PCH_MAX_FIFO_DEPTH)
672 673
		n_writes = PCH_MAX_FIFO_DEPTH;

674
	dev_dbg(&data->master->dev, "\n%s:Pulling down SSN low - writing "
675 676 677
		"0x2 to SSNXCR\n", __func__);
	pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);

678 679
	for (j = 0; j < n_writes; j++)
		pch_spi_writereg(data->master, PCH_SPDWR, data->pkt_tx_buff[j]);
680 681 682 683 684 685 686 687 688

	/* update tx_index */
	data->tx_index = j;

	/* reset transfer complete flag */
	data->transfer_complete = false;
	data->transfer_active = true;
}

T
Tomoya MORINAGA 已提交
689
static void pch_spi_nomore_transfer(struct pch_spi_data *data)
690
{
691
	struct spi_message *pmsg, *tmp;
692
	dev_dbg(&data->master->dev, "%s called\n", __func__);
693
	/* Invoke complete callback
694
	 * [To the spi core..indicating end of transfer] */
695 696
	data->current_msg->status = 0;

697
	if (data->current_msg->complete) {
698 699 700 701 702 703 704 705 706 707 708 709 710 711
		dev_dbg(&data->master->dev,
			"%s:Invoking callback of SPI core\n", __func__);
		data->current_msg->complete(data->current_msg->context);
	}

	/* update status in global variable */
	data->bcurrent_msg_processing = false;

	dev_dbg(&data->master->dev,
		"%s:data->bcurrent_msg_processing = false\n", __func__);

	data->current_msg = NULL;
	data->cur_trans = NULL;

712 713
	/* check if we have items in list and not suspending
	 * return 1 if list empty */
714
	if ((list_empty(&data->queue) == 0) &&
715 716
	    (!data->board_dat->suspend_sts) &&
	    (data->status != STATUS_EXITING)) {
717
		/* We have some more work to do (either there is more tranint
718 719 720 721
		 * bpw;sfer requests in the current message or there are
		 *more messages)
		 */
		dev_dbg(&data->master->dev, "%s:Invoke queue_work\n", __func__);
722
		queue_work(data->wk, &data->work);
723 724
	} else if (data->board_dat->suspend_sts ||
		   data->status == STATUS_EXITING) {
725 726 727
		dev_dbg(&data->master->dev,
			"%s suspend/remove initiated, flushing queue\n",
			__func__);
728
		list_for_each_entry_safe(pmsg, tmp, data->queue.next, queue) {
729 730
			pmsg->status = -EIO;

731
			if (pmsg->complete)
732 733 734 735 736 737 738 739 740 741
				pmsg->complete(pmsg->context);

			/* delete from queue */
			list_del_init(&pmsg->queue);
		}
	}
}

static void pch_spi_set_ir(struct pch_spi_data *data)
{
T
Tomoya MORINAGA 已提交
742 743
	/* enable interrupts, set threshold, enable SPI */
	if ((data->bpw_len) > PCH_MAX_FIFO_DEPTH)
744
		/* set receive threshold to PCH_RX_THOLD */
745
		pch_spi_setclr_reg(data->master, PCH_SPCR,
T
Tomoya MORINAGA 已提交
746 747 748 749 750
				   PCH_RX_THOLD << SPCR_RFIC_FIELD |
				   SPCR_FIE_BIT | SPCR_RFIE_BIT |
				   SPCR_ORIE_BIT | SPCR_SPE_BIT,
				   MASK_RFIC_SPCR_BITS | PCH_ALL);
	else
751
		/* set receive threshold to maximum */
752
		pch_spi_setclr_reg(data->master, PCH_SPCR,
T
Tomoya MORINAGA 已提交
753 754 755 756
				   PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD |
				   SPCR_FIE_BIT | SPCR_ORIE_BIT |
				   SPCR_SPE_BIT,
				   MASK_RFIC_SPCR_BITS | PCH_ALL);
757 758 759 760 761 762 763 764 765 766

	/* Wait until the transfer completes; go to sleep after
				 initiating the transfer. */
	dev_dbg(&data->master->dev,
		"%s:waiting for transfer to get over\n", __func__);

	wait_event_interruptible(data->wait, data->transfer_complete);

	/* clear all interrupts */
	pch_spi_writereg(data->master, PCH_SPSR,
767
			 pch_spi_readreg(data->master, PCH_SPSR));
T
Tomoya MORINAGA 已提交
768 769 770 771
	/* Disable interrupts and SPI transfer */
	pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL | SPCR_SPE_BIT);
	/* clear FIFO */
	pch_spi_clear_fifo(data->master);
772 773 774 775 776 777 778 779 780
}

static void pch_spi_copy_rx_data(struct pch_spi_data *data, int bpw)
{
	int j;
	u8 *rx_buf;
	u16 *rx_sbuf;

	/* copy Rx Data */
781
	if (!data->cur_trans->rx_buf)
782 783 784
		return;

	if (bpw == 8) {
785 786 787
		rx_buf = data->cur_trans->rx_buf;
		for (j = 0; j < data->bpw_len; j++)
			*rx_buf++ = data->pkt_rx_buff[j] & 0xFF;
788
	} else {
789 790 791
		rx_sbuf = data->cur_trans->rx_buf;
		for (j = 0; j < data->bpw_len; j++)
			*rx_sbuf++ = data->pkt_rx_buff[j];
792 793 794
	}
}

T
Tomoya MORINAGA 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
static void pch_spi_copy_rx_data_for_dma(struct pch_spi_data *data, int bpw)
{
	int j;
	u8 *rx_buf;
	u16 *rx_sbuf;
	const u8 *rx_dma_buf;
	const u16 *rx_dma_sbuf;

	/* copy Rx Data */
	if (!data->cur_trans->rx_buf)
		return;

	if (bpw == 8) {
		rx_buf = data->cur_trans->rx_buf;
		rx_dma_buf = data->dma.rx_buf_virt;
		for (j = 0; j < data->bpw_len; j++)
			*rx_buf++ = *rx_dma_buf++ & 0xFF;
812
		data->cur_trans->rx_buf = rx_buf;
T
Tomoya MORINAGA 已提交
813 814 815 816 817
	} else {
		rx_sbuf = data->cur_trans->rx_buf;
		rx_dma_sbuf = data->dma.rx_buf_virt;
		for (j = 0; j < data->bpw_len; j++)
			*rx_sbuf++ = *rx_dma_sbuf++;
818
		data->cur_trans->rx_buf = rx_sbuf;
T
Tomoya MORINAGA 已提交
819 820 821
	}
}

822
static int pch_spi_start_transfer(struct pch_spi_data *data)
T
Tomoya MORINAGA 已提交
823 824 825
{
	struct pch_spi_dma_ctrl *dma;
	unsigned long flags;
826
	int rtn;
T
Tomoya MORINAGA 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840

	dma = &data->dma;

	spin_lock_irqsave(&data->lock, flags);

	/* disable interrupts, SPI set enable */
	pch_spi_setclr_reg(data->master, PCH_SPCR, SPCR_SPE_BIT, PCH_ALL);

	spin_unlock_irqrestore(&data->lock, flags);

	/* Wait until the transfer completes; go to sleep after
				 initiating the transfer. */
	dev_dbg(&data->master->dev,
		"%s:waiting for transfer to get over\n", __func__);
841 842 843
	rtn = wait_event_interruptible_timeout(data->wait,
					       data->transfer_complete,
					       msecs_to_jiffies(2 * HZ));
844 845 846
	if (!rtn)
		dev_err(&data->master->dev,
			"%s wait-event timeout\n", __func__);
T
Tomoya MORINAGA 已提交
847 848 849

	dma_sync_sg_for_cpu(&data->master->dev, dma->sg_rx_p, dma->nent,
			    DMA_FROM_DEVICE);
850 851 852 853 854

	dma_sync_sg_for_cpu(&data->master->dev, dma->sg_tx_p, dma->nent,
			    DMA_FROM_DEVICE);
	memset(data->dma.tx_buf_virt, 0, PAGE_SIZE);

T
Tomoya MORINAGA 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	async_tx_ack(dma->desc_rx);
	async_tx_ack(dma->desc_tx);
	kfree(dma->sg_tx_p);
	kfree(dma->sg_rx_p);

	spin_lock_irqsave(&data->lock, flags);

	/* clear fifo threshold, disable interrupts, disable SPI transfer */
	pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
			   MASK_RFIC_SPCR_BITS | MASK_TFIC_SPCR_BITS | PCH_ALL |
			   SPCR_SPE_BIT);
	/* clear all interrupts */
	pch_spi_writereg(data->master, PCH_SPSR,
			 pch_spi_readreg(data->master, PCH_SPSR));
	/* clear FIFO */
	pch_spi_clear_fifo(data->master);

	spin_unlock_irqrestore(&data->lock, flags);
873 874

	return rtn;
T
Tomoya MORINAGA 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
}

static void pch_dma_rx_complete(void *arg)
{
	struct pch_spi_data *data = arg;

	/* transfer is completed;inform pch_spi_process_messages_dma */
	data->transfer_complete = true;
	wake_up_interruptible(&data->wait);
}

static bool pch_spi_filter(struct dma_chan *chan, void *slave)
{
	struct pch_dma_slave *param = slave;

	if ((chan->chan_id == param->chan_id) &&
	    (param->dma_dev == chan->device->dev)) {
		chan->private = param;
		return true;
	} else {
		return false;
	}
}

static void pch_spi_request_dma(struct pch_spi_data *data, int bpw)
{
	dma_cap_mask_t mask;
	struct dma_chan *chan;
	struct pci_dev *dma_dev;
	struct pch_dma_slave *param;
	struct pch_spi_dma_ctrl *dma;
	unsigned int width;

	if (bpw == 8)
		width = PCH_DMA_WIDTH_1_BYTE;
	else
		width = PCH_DMA_WIDTH_2_BYTES;

	dma = &data->dma;
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

	/* Get DMA's dev information */
918 919
	dma_dev = pci_get_bus_and_slot(data->board_dat->pdev->bus->number,
				       PCI_DEVFN(12, 0));
T
Tomoya MORINAGA 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

	/* Set Tx DMA */
	param = &dma->param_tx;
	param->dma_dev = &dma_dev->dev;
	param->chan_id = data->master->bus_num * 2; /* Tx = 0, 2 */
	param->tx_reg = data->io_base_addr + PCH_SPDWR;
	param->width = width;
	chan = dma_request_channel(mask, pch_spi_filter, param);
	if (!chan) {
		dev_err(&data->master->dev,
			"ERROR: dma_request_channel FAILS(Tx)\n");
		data->use_dma = 0;
		return;
	}
	dma->chan_tx = chan;

	/* Set Rx DMA */
	param = &dma->param_rx;
	param->dma_dev = &dma_dev->dev;
	param->chan_id = data->master->bus_num * 2 + 1; /* Rx = Tx + 1 */
	param->rx_reg = data->io_base_addr + PCH_SPDRR;
	param->width = width;
	chan = dma_request_channel(mask, pch_spi_filter, param);
	if (!chan) {
		dev_err(&data->master->dev,
			"ERROR: dma_request_channel FAILS(Rx)\n");
		dma_release_channel(dma->chan_tx);
		dma->chan_tx = NULL;
		data->use_dma = 0;
		return;
	}
	dma->chan_rx = chan;
}

static void pch_spi_release_dma(struct pch_spi_data *data)
{
	struct pch_spi_dma_ctrl *dma;

	dma = &data->dma;
	if (dma->chan_tx) {
		dma_release_channel(dma->chan_tx);
		dma->chan_tx = NULL;
	}
	if (dma->chan_rx) {
		dma_release_channel(dma->chan_rx);
		dma->chan_rx = NULL;
	}
	return;
}

static void pch_spi_handle_dma(struct pch_spi_data *data, int *bpw)
{
	const u8 *tx_buf;
	const u16 *tx_sbuf;
	u8 *tx_dma_buf;
	u16 *tx_dma_sbuf;
	struct scatterlist *sg;
	struct dma_async_tx_descriptor *desc_tx;
	struct dma_async_tx_descriptor *desc_rx;
	int num;
	int i;
	int size;
	int rem;
983
	int head;
T
Tomoya MORINAGA 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	unsigned long flags;
	struct pch_spi_dma_ctrl *dma;

	dma = &data->dma;

	/* set baud rate if needed */
	if (data->cur_trans->speed_hz) {
		dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
		spin_lock_irqsave(&data->lock, flags);
		pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
		spin_unlock_irqrestore(&data->lock, flags);
	}

	/* set bits per word if needed */
	if (data->cur_trans->bits_per_word &&
	    (data->current_msg->spi->bits_per_word !=
	     data->cur_trans->bits_per_word)) {
		dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
		spin_lock_irqsave(&data->lock, flags);
		pch_spi_set_bits_per_word(data->master,
					  data->cur_trans->bits_per_word);
		spin_unlock_irqrestore(&data->lock, flags);
		*bpw = data->cur_trans->bits_per_word;
	} else {
		*bpw = data->current_msg->spi->bits_per_word;
	}
	data->bpw_len = data->cur_trans->len / (*bpw / 8);

1012 1013 1014 1015 1016
	if (data->bpw_len > PCH_BUF_SIZE) {
		data->bpw_len = PCH_BUF_SIZE;
		data->cur_trans->len -= PCH_BUF_SIZE;
	}

T
Tomoya MORINAGA 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	/* copy Tx Data */
	if (data->cur_trans->tx_buf != NULL) {
		if (*bpw == 8) {
			tx_buf = data->cur_trans->tx_buf;
			tx_dma_buf = dma->tx_buf_virt;
			for (i = 0; i < data->bpw_len; i++)
				*tx_dma_buf++ = *tx_buf++;
		} else {
			tx_sbuf = data->cur_trans->tx_buf;
			tx_dma_sbuf = dma->tx_buf_virt;
			for (i = 0; i < data->bpw_len; i++)
				*tx_dma_sbuf++ = *tx_sbuf++;
		}
	}
1031 1032

	/* Calculate Rx parameter for DMA transmitting */
T
Tomoya MORINAGA 已提交
1033
	if (data->bpw_len > PCH_DMA_TRANS_SIZE) {
1034 1035 1036 1037 1038 1039 1040
		if (data->bpw_len % PCH_DMA_TRANS_SIZE) {
			num = data->bpw_len / PCH_DMA_TRANS_SIZE + 1;
			rem = data->bpw_len % PCH_DMA_TRANS_SIZE;
		} else {
			num = data->bpw_len / PCH_DMA_TRANS_SIZE;
			rem = PCH_DMA_TRANS_SIZE;
		}
T
Tomoya MORINAGA 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
		size = PCH_DMA_TRANS_SIZE;
	} else {
		num = 1;
		size = data->bpw_len;
		rem = data->bpw_len;
	}
	dev_dbg(&data->master->dev, "%s num=%d size=%d rem=%d\n",
		__func__, num, size, rem);
	spin_lock_irqsave(&data->lock, flags);

	/* set receive fifo threshold and transmit fifo threshold */
	pch_spi_setclr_reg(data->master, PCH_SPCR,
			   ((size - 1) << SPCR_RFIC_FIELD) |
1054
			   (PCH_TX_THOLD << SPCR_TFIC_FIELD),
T
Tomoya MORINAGA 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
			   MASK_RFIC_SPCR_BITS | MASK_TFIC_SPCR_BITS);

	spin_unlock_irqrestore(&data->lock, flags);

	/* RX */
	dma->sg_rx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
	sg_init_table(dma->sg_rx_p, num); /* Initialize SG table */
	/* offset, length setting */
	sg = dma->sg_rx_p;
	for (i = 0; i < num; i++, sg++) {
1065 1066 1067
		if (i == (num - 2)) {
			sg->offset = size * i;
			sg->offset = sg->offset * (*bpw / 8);
T
Tomoya MORINAGA 已提交
1068 1069 1070
			sg_set_page(sg, virt_to_page(dma->rx_buf_virt), rem,
				    sg->offset);
			sg_dma_len(sg) = rem;
1071 1072 1073 1074 1075 1076
		} else if (i == (num - 1)) {
			sg->offset = size * (i - 1) + rem;
			sg->offset = sg->offset * (*bpw / 8);
			sg_set_page(sg, virt_to_page(dma->rx_buf_virt), size,
				    sg->offset);
			sg_dma_len(sg) = size;
T
Tomoya MORINAGA 已提交
1077
		} else {
1078
			sg->offset = size * i;
T
Tomoya MORINAGA 已提交
1079 1080 1081 1082 1083 1084 1085 1086
			sg->offset = sg->offset * (*bpw / 8);
			sg_set_page(sg, virt_to_page(dma->rx_buf_virt), size,
				    sg->offset);
			sg_dma_len(sg) = size;
		}
		sg_dma_address(sg) = dma->rx_buf_dma + sg->offset;
	}
	sg = dma->sg_rx_p;
1087
	desc_rx = dmaengine_prep_slave_sg(dma->chan_rx, sg,
1088
					num, DMA_DEV_TO_MEM,
T
Tomoya MORINAGA 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
	if (!desc_rx) {
		dev_err(&data->master->dev, "%s:device_prep_slave_sg Failed\n",
			__func__);
		return;
	}
	dma_sync_sg_for_device(&data->master->dev, sg, num, DMA_FROM_DEVICE);
	desc_rx->callback = pch_dma_rx_complete;
	desc_rx->callback_param = data;
	dma->nent = num;
	dma->desc_rx = desc_rx;

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	/* Calculate Tx parameter for DMA transmitting */
	if (data->bpw_len > PCH_MAX_FIFO_DEPTH) {
		head = PCH_MAX_FIFO_DEPTH - PCH_DMA_TRANS_SIZE;
		if (data->bpw_len % PCH_DMA_TRANS_SIZE > 4) {
			num = data->bpw_len / PCH_DMA_TRANS_SIZE + 1;
			rem = data->bpw_len % PCH_DMA_TRANS_SIZE - head;
		} else {
			num = data->bpw_len / PCH_DMA_TRANS_SIZE;
			rem = data->bpw_len % PCH_DMA_TRANS_SIZE +
			      PCH_DMA_TRANS_SIZE - head;
		}
1112 1113 1114 1115 1116
		size = PCH_DMA_TRANS_SIZE;
	} else {
		num = 1;
		size = data->bpw_len;
		rem = data->bpw_len;
1117
		head = 0;
1118 1119
	}

T
Tomoya MORINAGA 已提交
1120 1121 1122 1123 1124 1125 1126
	dma->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
	sg_init_table(dma->sg_tx_p, num); /* Initialize SG table */
	/* offset, length setting */
	sg = dma->sg_tx_p;
	for (i = 0; i < num; i++, sg++) {
		if (i == 0) {
			sg->offset = 0;
1127 1128 1129 1130 1131 1132
			sg_set_page(sg, virt_to_page(dma->tx_buf_virt), size + head,
				    sg->offset);
			sg_dma_len(sg) = size + head;
		} else if (i == (num - 1)) {
			sg->offset = head + size * i;
			sg->offset = sg->offset * (*bpw / 8);
T
Tomoya MORINAGA 已提交
1133 1134 1135 1136
			sg_set_page(sg, virt_to_page(dma->tx_buf_virt), rem,
				    sg->offset);
			sg_dma_len(sg) = rem;
		} else {
1137
			sg->offset = head + size * i;
T
Tomoya MORINAGA 已提交
1138 1139 1140 1141 1142 1143 1144 1145
			sg->offset = sg->offset * (*bpw / 8);
			sg_set_page(sg, virt_to_page(dma->tx_buf_virt), size,
				    sg->offset);
			sg_dma_len(sg) = size;
		}
		sg_dma_address(sg) = dma->tx_buf_dma + sg->offset;
	}
	sg = dma->sg_tx_p;
1146
	desc_tx = dmaengine_prep_slave_sg(dma->chan_tx,
1147
					sg, num, DMA_MEM_TO_DEV,
T
Tomoya MORINAGA 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
	if (!desc_tx) {
		dev_err(&data->master->dev, "%s:device_prep_slave_sg Failed\n",
			__func__);
		return;
	}
	dma_sync_sg_for_device(&data->master->dev, sg, num, DMA_TO_DEVICE);
	desc_tx->callback = NULL;
	desc_tx->callback_param = data;
	dma->nent = num;
	dma->desc_tx = desc_tx;

	dev_dbg(&data->master->dev, "\n%s:Pulling down SSN low - writing "
		"0x2 to SSNXCR\n", __func__);

	spin_lock_irqsave(&data->lock, flags);
	pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);
	desc_rx->tx_submit(desc_rx);
	desc_tx->tx_submit(desc_tx);
	spin_unlock_irqrestore(&data->lock, flags);

	/* reset transfer complete flag */
	data->transfer_complete = false;
}
1172 1173 1174

static void pch_spi_process_messages(struct work_struct *pwork)
{
1175
	struct spi_message *pmsg, *tmp;
1176
	struct pch_spi_data *data;
1177 1178
	int bpw;

1179
	data = container_of(pwork, struct pch_spi_data, work);
1180
	dev_dbg(&data->master->dev, "%s data initialized\n", __func__);
1181 1182 1183

	spin_lock(&data->lock);
	/* check if suspend has been initiated;if yes flush queue */
1184
	if (data->board_dat->suspend_sts || (data->status == STATUS_EXITING)) {
1185 1186
		dev_dbg(&data->master->dev,
			"%s suspend/remove initiated, flushing queue\n", __func__);
1187
		list_for_each_entry_safe(pmsg, tmp, data->queue.next, queue) {
1188 1189
			pmsg->status = -EIO;

1190
			if (pmsg->complete) {
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
				spin_unlock(&data->lock);
				pmsg->complete(pmsg->context);
				spin_lock(&data->lock);
			}

			/* delete from queue */
			list_del_init(&pmsg->queue);
		}

		spin_unlock(&data->lock);
		return;
	}

	data->bcurrent_msg_processing = true;
	dev_dbg(&data->master->dev,
		"%s Set data->bcurrent_msg_processing= true\n", __func__);

	/* Get the message from the queue and delete it from there. */
1209 1210
	data->current_msg = list_entry(data->queue.next, struct spi_message,
					queue);
1211 1212 1213 1214 1215 1216 1217 1218 1219

	list_del_init(&data->current_msg->queue);

	data->current_msg->status = 0;

	pch_spi_select_chip(data, data->current_msg->spi);

	spin_unlock(&data->lock);

T
Tomoya MORINAGA 已提交
1220 1221 1222
	if (data->use_dma)
		pch_spi_request_dma(data,
				    data->current_msg->spi->bits_per_word);
1223
	pch_spi_writereg(data->master, PCH_SSNXCR, SSN_NO_CONTROL);
1224
	do {
1225
		int cnt;
1226 1227 1228 1229 1230 1231
		/* If we are already processing a message get the next
		transfer structure from the message otherwise retrieve
		the 1st transfer request from the message. */
		spin_lock(&data->lock);
		if (data->cur_trans == NULL) {
			data->cur_trans =
T
Tomoya MORINAGA 已提交
1232 1233 1234 1235
				list_entry(data->current_msg->transfers.next,
					   struct spi_transfer, transfer_list);
			dev_dbg(&data->master->dev, "%s "
				":Getting 1st transfer message\n", __func__);
1236 1237
		} else {
			data->cur_trans =
T
Tomoya MORINAGA 已提交
1238 1239 1240 1241
				list_entry(data->cur_trans->transfer_list.next,
					   struct spi_transfer, transfer_list);
			dev_dbg(&data->master->dev, "%s "
				":Getting next transfer message\n", __func__);
1242 1243 1244
		}
		spin_unlock(&data->lock);

1245 1246 1247 1248
		if (!data->cur_trans->len)
			goto out;
		cnt = (data->cur_trans->len - 1) / PCH_BUF_SIZE + 1;
		data->save_total_len = data->cur_trans->len;
T
Tomoya MORINAGA 已提交
1249
		if (data->use_dma) {
1250 1251 1252 1253
			int i;
			char *save_rx_buf = data->cur_trans->rx_buf;
			for (i = 0; i < cnt; i ++) {
				pch_spi_handle_dma(data, &bpw);
1254 1255 1256 1257 1258 1259 1260 1261
				if (!pch_spi_start_transfer(data)) {
					data->transfer_complete = true;
					data->current_msg->status = -EIO;
					data->current_msg->complete
						   (data->current_msg->context);
					data->bcurrent_msg_processing = false;
					data->current_msg = NULL;
					data->cur_trans = NULL;
1262
					goto out;
1263
				}
1264 1265 1266
				pch_spi_copy_rx_data_for_dma(data, bpw);
			}
			data->cur_trans->rx_buf = save_rx_buf;
T
Tomoya MORINAGA 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275
		} else {
			pch_spi_set_tx(data, &bpw);
			pch_spi_set_ir(data);
			pch_spi_copy_rx_data(data, bpw);
			kfree(data->pkt_rx_buff);
			data->pkt_rx_buff = NULL;
			kfree(data->pkt_tx_buff);
			data->pkt_tx_buff = NULL;
		}
1276
		/* increment message count */
1277
		data->cur_trans->len = data->save_total_len;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
		data->current_msg->actual_length += data->cur_trans->len;

		dev_dbg(&data->master->dev,
			"%s:data->current_msg->actual_length=%d\n",
			__func__, data->current_msg->actual_length);

		/* check for delay */
		if (data->cur_trans->delay_usecs) {
			dev_dbg(&data->master->dev, "%s:"
				"delay in usec=%d\n", __func__,
				data->cur_trans->delay_usecs);
			udelay(data->cur_trans->delay_usecs);
		}

		spin_lock(&data->lock);

		/* No more transfer in this message. */
		if ((data->cur_trans->transfer_list.next) ==
		    &(data->current_msg->transfers)) {
T
Tomoya MORINAGA 已提交
1297
			pch_spi_nomore_transfer(data);
1298 1299 1300 1301
		}

		spin_unlock(&data->lock);

1302
	} while (data->cur_trans != NULL);
T
Tomoya MORINAGA 已提交
1303

1304
out:
1305
	pch_spi_writereg(data->master, PCH_SSNXCR, SSN_HIGH);
T
Tomoya MORINAGA 已提交
1306 1307
	if (data->use_dma)
		pch_spi_release_dma(data);
1308 1309
}

1310 1311
static void pch_spi_free_resources(struct pch_spi_board_data *board_dat,
				   struct pch_spi_data *data)
1312 1313 1314 1315
{
	dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);

	/* free workqueue */
1316 1317 1318
	if (data->wk != NULL) {
		destroy_workqueue(data->wk);
		data->wk = NULL;
1319 1320 1321 1322 1323 1324
		dev_dbg(&board_dat->pdev->dev,
			"%s destroy_workqueue invoked successfully\n",
			__func__);
	}
}

1325 1326
static int pch_spi_get_resources(struct pch_spi_board_data *board_dat,
				 struct pch_spi_data *data)
1327
{
1328 1329
	int retval = 0;

1330 1331 1332
	dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);

	/* create workqueue */
1333 1334
	data->wk = create_singlethread_workqueue(KBUILD_MODNAME);
	if (!data->wk) {
1335 1336 1337 1338 1339 1340 1341
		dev_err(&board_dat->pdev->dev,
			"%s create_singlet hread_workqueue failed\n", __func__);
		retval = -EBUSY;
		goto err_return;
	}

	/* reset PCH SPI h/w */
1342
	pch_spi_reset(data->master);
1343 1344 1345
	dev_dbg(&board_dat->pdev->dev,
		"%s pch_spi_reset invoked successfully\n", __func__);

1346
	dev_dbg(&board_dat->pdev->dev, "%s data->irq_reg_sts=true\n", __func__);
1347 1348 1349 1350 1351

err_return:
	if (retval != 0) {
		dev_err(&board_dat->pdev->dev,
			"%s FAIL:invoking pch_spi_free_resources\n", __func__);
1352
		pch_spi_free_resources(board_dat, data);
1353 1354 1355 1356 1357 1358 1359
	}

	dev_dbg(&board_dat->pdev->dev, "%s Return=%d\n", __func__, retval);

	return retval;
}

T
Tomoya MORINAGA 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
static void pch_free_dma_buf(struct pch_spi_board_data *board_dat,
			     struct pch_spi_data *data)
{
	struct pch_spi_dma_ctrl *dma;

	dma = &data->dma;
	if (dma->tx_buf_dma)
		dma_free_coherent(&board_dat->pdev->dev, PCH_BUF_SIZE,
				  dma->tx_buf_virt, dma->tx_buf_dma);
	if (dma->rx_buf_dma)
		dma_free_coherent(&board_dat->pdev->dev, PCH_BUF_SIZE,
				  dma->rx_buf_virt, dma->rx_buf_dma);
	return;
}

static void pch_alloc_dma_buf(struct pch_spi_board_data *board_dat,
			      struct pch_spi_data *data)
{
	struct pch_spi_dma_ctrl *dma;

	dma = &data->dma;
	/* Get Consistent memory for Tx DMA */
	dma->tx_buf_virt = dma_alloc_coherent(&board_dat->pdev->dev,
				PCH_BUF_SIZE, &dma->tx_buf_dma, GFP_KERNEL);
	/* Get Consistent memory for Rx DMA */
	dma->rx_buf_virt = dma_alloc_coherent(&board_dat->pdev->dev,
				PCH_BUF_SIZE, &dma->rx_buf_dma, GFP_KERNEL);
}

1389
static int pch_spi_pd_probe(struct platform_device *plat_dev)
1390
{
1391
	int ret;
1392
	struct spi_master *master;
1393 1394
	struct pch_spi_board_data *board_dat = dev_get_platdata(&plat_dev->dev);
	struct pch_spi_data *data;
1395

T
Tomoya MORINAGA 已提交
1396 1397
	dev_dbg(&plat_dev->dev, "%s:debug\n", __func__);

1398 1399 1400 1401 1402 1403
	master = spi_alloc_master(&board_dat->pdev->dev,
				  sizeof(struct pch_spi_data));
	if (!master) {
		dev_err(&plat_dev->dev, "spi_alloc_master[%d] failed.\n",
			plat_dev->id);
		return -ENOMEM;
1404 1405
	}

1406 1407
	data = spi_master_get_devdata(master);
	data->master = master;
1408

1409
	platform_set_drvdata(plat_dev, data);
1410

T
Tomoya MORINAGA 已提交
1411 1412 1413
	/* baseaddress + address offset) */
	data->io_base_addr = pci_resource_start(board_dat->pdev, 1) +
					 PCH_ADDRESS_SIZE * plat_dev->id;
1414
	data->io_remap_addr = pci_iomap(board_dat->pdev, 1, 0) +
T
Tomoya MORINAGA 已提交
1415
					 PCH_ADDRESS_SIZE * plat_dev->id;
1416 1417 1418 1419
	if (!data->io_remap_addr) {
		dev_err(&plat_dev->dev, "%s pci_iomap failed\n", __func__);
		ret = -ENOMEM;
		goto err_pci_iomap;
1420 1421
	}

1422 1423
	dev_dbg(&plat_dev->dev, "[ch%d] remap_addr=%p\n",
		plat_dev->id, data->io_remap_addr);
1424 1425 1426 1427 1428

	/* initialize members of SPI master */
	master->num_chipselect = PCH_MAX_CS;
	master->setup = pch_spi_setup;
	master->transfer = pch_spi_transfer;
1429
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1430
	master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
1431

1432 1433 1434 1435 1436
	data->board_dat = board_dat;
	data->plat_dev = plat_dev;
	data->n_curnt_chip = 255;
	data->status = STATUS_RUNNING;
	data->ch = plat_dev->id;
T
Tomoya MORINAGA 已提交
1437
	data->use_dma = use_dma;
1438

1439 1440 1441 1442
	INIT_LIST_HEAD(&data->queue);
	spin_lock_init(&data->lock);
	INIT_WORK(&data->work, pch_spi_process_messages);
	init_waitqueue_head(&data->wait);
1443

1444 1445 1446
	ret = pch_spi_get_resources(board_dat, data);
	if (ret) {
		dev_err(&plat_dev->dev, "%s fail(retval=%d)\n", __func__, ret);
1447 1448 1449
		goto err_spi_get_resources;
	}

1450 1451 1452 1453 1454 1455 1456 1457
	ret = request_irq(board_dat->pdev->irq, pch_spi_handler,
			  IRQF_SHARED, KBUILD_MODNAME, data);
	if (ret) {
		dev_err(&plat_dev->dev,
			"%s request_irq failed\n", __func__);
		goto err_request_irq;
	}
	data->irq_reg_sts = true;
1458 1459 1460

	pch_spi_set_master_mode(master);

1461 1462 1463
	ret = spi_register_master(master);
	if (ret != 0) {
		dev_err(&plat_dev->dev,
1464
			"%s spi_register_master FAILED\n", __func__);
1465
		goto err_spi_register_master;
1466 1467
	}

T
Tomoya MORINAGA 已提交
1468 1469 1470 1471 1472
	if (use_dma) {
		dev_info(&plat_dev->dev, "Use DMA for data transfers\n");
		pch_alloc_dma_buf(board_dat, data);
	}

1473 1474
	return 0;

1475
err_spi_register_master:
1476
	free_irq(board_dat->pdev->irq, data);
1477 1478
err_request_irq:
	pch_spi_free_resources(board_dat, data);
1479
err_spi_get_resources:
1480 1481
	pci_iounmap(board_dat->pdev, data->io_remap_addr);
err_pci_iomap:
1482
	spi_master_put(master);
1483 1484

	return ret;
1485 1486
}

1487
static int pch_spi_pd_remove(struct platform_device *plat_dev)
1488
{
1489 1490
	struct pch_spi_board_data *board_dat = dev_get_platdata(&plat_dev->dev);
	struct pch_spi_data *data = platform_get_drvdata(plat_dev);
1491
	int count;
T
Tomoya MORINAGA 已提交
1492
	unsigned long flags;
1493

1494 1495
	dev_dbg(&plat_dev->dev, "%s:[ch%d] irq=%d\n",
		__func__, plat_dev->id, board_dat->pdev->irq);
T
Tomoya MORINAGA 已提交
1496 1497 1498 1499

	if (use_dma)
		pch_free_dma_buf(board_dat, data);

1500 1501 1502
	/* check for any pending messages; no action is taken if the queue
	 * is still full; but at least we tried.  Unload anyway */
	count = 500;
T
Tomoya MORINAGA 已提交
1503
	spin_lock_irqsave(&data->lock, flags);
1504 1505
	data->status = STATUS_EXITING;
	while ((list_empty(&data->queue) == 0) && --count) {
1506 1507
		dev_dbg(&board_dat->pdev->dev, "%s :queue not empty\n",
			__func__);
T
Tomoya MORINAGA 已提交
1508
		spin_unlock_irqrestore(&data->lock, flags);
1509
		msleep(PCH_SLEEP_TIME);
T
Tomoya MORINAGA 已提交
1510
		spin_lock_irqsave(&data->lock, flags);
1511
	}
T
Tomoya MORINAGA 已提交
1512
	spin_unlock_irqrestore(&data->lock, flags);
1513

1514 1515 1516 1517 1518 1519 1520 1521
	pch_spi_free_resources(board_dat, data);
	/* disable interrupts & free IRQ */
	if (data->irq_reg_sts) {
		/* disable interrupts */
		pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
		data->irq_reg_sts = false;
		free_irq(board_dat->pdev->irq, data);
	}
1522

1523 1524
	pci_iounmap(board_dat->pdev, data->io_remap_addr);
	spi_unregister_master(data->master);
1525

1526
	return 0;
1527 1528
}
#ifdef CONFIG_PM
1529 1530
static int pch_spi_pd_suspend(struct platform_device *pd_dev,
			      pm_message_t state)
1531 1532
{
	u8 count;
1533 1534
	struct pch_spi_board_data *board_dat = dev_get_platdata(&pd_dev->dev);
	struct pch_spi_data *data = platform_get_drvdata(pd_dev);
1535

1536
	dev_dbg(&pd_dev->dev, "%s ENTRY\n", __func__);
1537 1538

	if (!board_dat) {
1539
		dev_err(&pd_dev->dev,
1540 1541 1542 1543 1544 1545 1546
			"%s pci_get_drvdata returned NULL\n", __func__);
		return -EFAULT;
	}

	/* check if the current message is processed:
	   Only after thats done the transfer will be suspended */
	count = 255;
T
Tomoya MORINAGA 已提交
1547 1548
	while ((--count) > 0) {
		if (!(data->bcurrent_msg_processing))
1549 1550 1551 1552 1553
			break;
		msleep(PCH_SLEEP_TIME);
	}

	/* Free IRQ */
1554
	if (data->irq_reg_sts) {
1555
		/* disable all interrupts */
1556 1557 1558
		pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
		pch_spi_reset(data->master);
		free_irq(board_dat->pdev->irq, data);
1559

1560 1561
		data->irq_reg_sts = false;
		dev_dbg(&pd_dev->dev,
1562 1563 1564
			"%s free_irq invoked successfully.\n", __func__);
	}

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	return 0;
}

static int pch_spi_pd_resume(struct platform_device *pd_dev)
{
	struct pch_spi_board_data *board_dat = dev_get_platdata(&pd_dev->dev);
	struct pch_spi_data *data = platform_get_drvdata(pd_dev);
	int retval;

	if (!board_dat) {
		dev_err(&pd_dev->dev,
			"%s pci_get_drvdata returned NULL\n", __func__);
		return -EFAULT;
	}

	if (!data->irq_reg_sts) {
		/* register IRQ */
		retval = request_irq(board_dat->pdev->irq, pch_spi_handler,
				     IRQF_SHARED, KBUILD_MODNAME, data);
		if (retval < 0) {
			dev_err(&pd_dev->dev,
				"%s request_irq failed\n", __func__);
			return retval;
		}

		/* reset PCH SPI h/w */
		pch_spi_reset(data->master);
		pch_spi_set_master_mode(data->master);
		data->irq_reg_sts = true;
	}
	return 0;
}
#else
#define pch_spi_pd_suspend NULL
#define pch_spi_pd_resume NULL
#endif

static struct platform_driver pch_spi_pd_driver = {
	.driver = {
		.name = "pch-spi",
		.owner = THIS_MODULE,
	},
	.probe = pch_spi_pd_probe,
1608
	.remove = pch_spi_pd_remove,
1609 1610 1611 1612
	.suspend = pch_spi_pd_suspend,
	.resume = pch_spi_pd_resume
};

1613
static int pch_spi_probe(struct pci_dev *pdev,
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
				   const struct pci_device_id *id)
{
	struct pch_spi_board_data *board_dat;
	struct platform_device *pd_dev = NULL;
	int retval;
	int i;
	struct pch_pd_dev_save *pd_dev_save;

	pd_dev_save = kzalloc(sizeof(struct pch_pd_dev_save), GFP_KERNEL);
	if (!pd_dev_save) {
		dev_err(&pdev->dev, "%s Can't allocate pd_dev_sav\n", __func__);
		return -ENOMEM;
	}

	board_dat = kzalloc(sizeof(struct pch_spi_board_data), GFP_KERNEL);
	if (!board_dat) {
		dev_err(&pdev->dev, "%s Can't allocate board_dat\n", __func__);
		retval = -ENOMEM;
		goto err_no_mem;
	}

	retval = pci_request_regions(pdev, KBUILD_MODNAME);
	if (retval) {
		dev_err(&pdev->dev, "%s request_region failed\n", __func__);
		goto pci_request_regions;
	}

	board_dat->pdev = pdev;
	board_dat->num = id->driver_data;
	pd_dev_save->num = id->driver_data;
	pd_dev_save->board_dat = board_dat;

	retval = pci_enable_device(pdev);
	if (retval) {
		dev_err(&pdev->dev, "%s pci_enable_device failed\n", __func__);
		goto pci_enable_device;
	}

	for (i = 0; i < board_dat->num; i++) {
		pd_dev = platform_device_alloc("pch-spi", i);
		if (!pd_dev) {
			dev_err(&pdev->dev, "platform_device_alloc failed\n");
1656
			retval = -ENOMEM;
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
			goto err_platform_device;
		}
		pd_dev_save->pd_save[i] = pd_dev;
		pd_dev->dev.parent = &pdev->dev;

		retval = platform_device_add_data(pd_dev, board_dat,
						  sizeof(*board_dat));
		if (retval) {
			dev_err(&pdev->dev,
				"platform_device_add_data failed\n");
			platform_device_put(pd_dev);
			goto err_platform_device;
		}

		retval = platform_device_add(pd_dev);
		if (retval) {
			dev_err(&pdev->dev, "platform_device_add failed\n");
			platform_device_put(pd_dev);
			goto err_platform_device;
		}
	}

	pci_set_drvdata(pdev, pd_dev_save);

	return 0;

err_platform_device:
	pci_disable_device(pdev);
pci_enable_device:
	pci_release_regions(pdev);
pci_request_regions:
	kfree(board_dat);
err_no_mem:
	kfree(pd_dev_save);

	return retval;
}

1695
static void pch_spi_remove(struct pci_dev *pdev)
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
{
	int i;
	struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);

	dev_dbg(&pdev->dev, "%s ENTRY:pdev=%p\n", __func__, pdev);

	for (i = 0; i < pd_dev_save->num; i++)
		platform_device_unregister(pd_dev_save->pd_save[i]);

	pci_disable_device(pdev);
	pci_release_regions(pdev);
	kfree(pd_dev_save->board_dat);
	kfree(pd_dev_save);
}

#ifdef CONFIG_PM
static int pch_spi_suspend(struct pci_dev *pdev, pm_message_t state)
{
	int retval;
	struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);

	dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);

	pd_dev_save->board_dat->suspend_sts = true;

1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	/* save config space */
	retval = pci_save_state(pdev);
	if (retval == 0) {
		pci_enable_wake(pdev, PCI_D3hot, 0);
		pci_disable_device(pdev);
		pci_set_power_state(pdev, PCI_D3hot);
	} else {
		dev_err(&pdev->dev, "%s pci_save_state failed\n", __func__);
	}

	return retval;
}

static int pch_spi_resume(struct pci_dev *pdev)
{
	int retval;
1737
	struct pch_pd_dev_save *pd_dev_save = pci_get_drvdata(pdev);
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
	dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);

	pci_set_power_state(pdev, PCI_D0);
	pci_restore_state(pdev);

	retval = pci_enable_device(pdev);
	if (retval < 0) {
		dev_err(&pdev->dev,
			"%s pci_enable_device failed\n", __func__);
	} else {
		pci_enable_wake(pdev, PCI_D3hot, 0);

1750 1751
		/* set suspend status to false */
		pd_dev_save->board_dat->suspend_sts = false;
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	}

	return retval;
}
#else
#define pch_spi_suspend NULL
#define pch_spi_resume NULL

#endif

1762
static struct pci_driver pch_spi_pcidev_driver = {
1763 1764 1765
	.name = "pch_spi",
	.id_table = pch_spi_pcidev_id,
	.probe = pch_spi_probe,
1766
	.remove = pch_spi_remove,
1767 1768 1769 1770 1771 1772
	.suspend = pch_spi_suspend,
	.resume = pch_spi_resume,
};

static int __init pch_spi_init(void)
{
1773 1774 1775 1776 1777
	int ret;
	ret = platform_driver_register(&pch_spi_pd_driver);
	if (ret)
		return ret;

1778
	ret = pci_register_driver(&pch_spi_pcidev_driver);
1779 1780
	if (ret) {
		platform_driver_unregister(&pch_spi_pd_driver);
1781
		return ret;
1782
	}
1783 1784

	return 0;
1785 1786 1787 1788 1789
}
module_init(pch_spi_init);

static void __exit pch_spi_exit(void)
{
1790
	pci_unregister_driver(&pch_spi_pcidev_driver);
1791
	platform_driver_unregister(&pch_spi_pd_driver);
1792 1793 1794
}
module_exit(pch_spi_exit);

T
Tomoya MORINAGA 已提交
1795 1796 1797 1798
module_param(use_dma, int, 0644);
MODULE_PARM_DESC(use_dma,
		 "to use DMA for data transfers pass 1 else 0; default 1");

1799
MODULE_LICENSE("GPL");
1800
MODULE_DESCRIPTION("Intel EG20T PCH/LAPIS Semiconductor ML7xxx IOH SPI Driver");
1801 1802
MODULE_DEVICE_TABLE(pci, pch_spi_pcidev_id);