spi-s3c64xx.c 43.4 KB
Newer Older
G
Grant Likely 已提交
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (C) 2009 Samsung Electronics Ltd.
 *	Jaswinder Singh <jassi.brar@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/workqueue.h>
M
Mark Brown 已提交
23
#include <linux/interrupt.h>
24 25 26
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/dma-mapping.h>
27
#include <linux/dmaengine.h>
28
#include <linux/platform_device.h>
29
#include <linux/pm_runtime.h>
30
#include <linux/spi/spi.h>
31
#include <linux/gpio.h>
32 33
#include <linux/of.h>
#include <linux/of_gpio.h>
34

35
#include <linux/platform_data/spi-s3c64xx.h>
36

37
#ifdef CONFIG_S3C_DMA
38 39 40
#include <mach/dma.h>
#endif

41
#define MAX_SPI_PORTS		3
42
#define S3C64XX_SPI_QUIRK_POLL		(1 << 0)
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/* Registers and bit-fields */

#define S3C64XX_SPI_CH_CFG		0x00
#define S3C64XX_SPI_CLK_CFG		0x04
#define S3C64XX_SPI_MODE_CFG	0x08
#define S3C64XX_SPI_SLAVE_SEL	0x0C
#define S3C64XX_SPI_INT_EN		0x10
#define S3C64XX_SPI_STATUS		0x14
#define S3C64XX_SPI_TX_DATA		0x18
#define S3C64XX_SPI_RX_DATA		0x1C
#define S3C64XX_SPI_PACKET_CNT	0x20
#define S3C64XX_SPI_PENDING_CLR	0x24
#define S3C64XX_SPI_SWAP_CFG	0x28
#define S3C64XX_SPI_FB_CLK		0x2C

#define S3C64XX_SPI_CH_HS_EN		(1<<6)	/* High Speed Enable */
#define S3C64XX_SPI_CH_SW_RST		(1<<5)
#define S3C64XX_SPI_CH_SLAVE		(1<<4)
#define S3C64XX_SPI_CPOL_L		(1<<3)
#define S3C64XX_SPI_CPHA_B		(1<<2)
#define S3C64XX_SPI_CH_RXCH_ON		(1<<1)
#define S3C64XX_SPI_CH_TXCH_ON		(1<<0)

#define S3C64XX_SPI_CLKSEL_SRCMSK	(3<<9)
#define S3C64XX_SPI_CLKSEL_SRCSHFT	9
#define S3C64XX_SPI_ENCLK_ENABLE	(1<<8)
70
#define S3C64XX_SPI_PSR_MASK		0xff
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

#define S3C64XX_SPI_MODE_CH_TSZ_BYTE		(0<<29)
#define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD	(1<<29)
#define S3C64XX_SPI_MODE_CH_TSZ_WORD		(2<<29)
#define S3C64XX_SPI_MODE_CH_TSZ_MASK		(3<<29)
#define S3C64XX_SPI_MODE_BUS_TSZ_BYTE		(0<<17)
#define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD	(1<<17)
#define S3C64XX_SPI_MODE_BUS_TSZ_WORD		(2<<17)
#define S3C64XX_SPI_MODE_BUS_TSZ_MASK		(3<<17)
#define S3C64XX_SPI_MODE_RXDMA_ON		(1<<2)
#define S3C64XX_SPI_MODE_TXDMA_ON		(1<<1)
#define S3C64XX_SPI_MODE_4BURST			(1<<0)

#define S3C64XX_SPI_SLAVE_AUTO			(1<<1)
#define S3C64XX_SPI_SLAVE_SIG_INACT		(1<<0)

#define S3C64XX_SPI_INT_TRAILING_EN		(1<<6)
#define S3C64XX_SPI_INT_RX_OVERRUN_EN		(1<<5)
#define S3C64XX_SPI_INT_RX_UNDERRUN_EN		(1<<4)
#define S3C64XX_SPI_INT_TX_OVERRUN_EN		(1<<3)
#define S3C64XX_SPI_INT_TX_UNDERRUN_EN		(1<<2)
#define S3C64XX_SPI_INT_RX_FIFORDY_EN		(1<<1)
#define S3C64XX_SPI_INT_TX_FIFORDY_EN		(1<<0)

#define S3C64XX_SPI_ST_RX_OVERRUN_ERR		(1<<5)
#define S3C64XX_SPI_ST_RX_UNDERRUN_ERR	(1<<4)
#define S3C64XX_SPI_ST_TX_OVERRUN_ERR		(1<<3)
#define S3C64XX_SPI_ST_TX_UNDERRUN_ERR	(1<<2)
#define S3C64XX_SPI_ST_RX_FIFORDY		(1<<1)
#define S3C64XX_SPI_ST_TX_FIFORDY		(1<<0)

#define S3C64XX_SPI_PACKET_CNT_EN		(1<<16)

#define S3C64XX_SPI_PND_TX_UNDERRUN_CLR		(1<<4)
#define S3C64XX_SPI_PND_TX_OVERRUN_CLR		(1<<3)
#define S3C64XX_SPI_PND_RX_UNDERRUN_CLR		(1<<2)
#define S3C64XX_SPI_PND_RX_OVERRUN_CLR		(1<<1)
#define S3C64XX_SPI_PND_TRAILING_CLR		(1<<0)

#define S3C64XX_SPI_SWAP_RX_HALF_WORD		(1<<7)
#define S3C64XX_SPI_SWAP_RX_BYTE		(1<<6)
#define S3C64XX_SPI_SWAP_RX_BIT			(1<<5)
#define S3C64XX_SPI_SWAP_RX_EN			(1<<4)
#define S3C64XX_SPI_SWAP_TX_HALF_WORD		(1<<3)
#define S3C64XX_SPI_SWAP_TX_BYTE		(1<<2)
#define S3C64XX_SPI_SWAP_TX_BIT			(1<<1)
#define S3C64XX_SPI_SWAP_TX_EN			(1<<0)

#define S3C64XX_SPI_FBCLK_MSK		(3<<0)

121 122 123 124 125 126
#define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
#define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
				(1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
#define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
#define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
					FIFO_LVL_MASK(i))
127 128 129 130 131 132 133

#define S3C64XX_SPI_MAX_TRAILCNT	0x3ff
#define S3C64XX_SPI_TRAILCNT_OFF	19

#define S3C64XX_SPI_TRAILCNT		S3C64XX_SPI_MAX_TRAILCNT

#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
134
#define is_polling(x)	(x->port_conf->quirks & S3C64XX_SPI_QUIRK_POLL)
135 136 137 138

#define RXBUSY    (1<<2)
#define TXBUSY    (1<<3)

B
Boojin Kim 已提交
139
struct s3c64xx_spi_dma_data {
140
	struct dma_chan *ch;
141
	enum dma_transfer_direction direction;
142
	unsigned int dmach;
B
Boojin Kim 已提交
143 144
};

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/**
 * struct s3c64xx_spi_info - SPI Controller hardware info
 * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
 * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
 * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
 * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
 * @clk_from_cmu: True, if the controller does not include a clock mux and
 *	prescaler unit.
 *
 * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
 * differ in some aspects such as the size of the fifo and spi bus clock
 * setup. Such differences are specified to the driver using this structure
 * which is provided as driver data to the driver.
 */
struct s3c64xx_spi_port_config {
	int	fifo_lvl_mask[MAX_SPI_PORTS];
	int	rx_lvl_offset;
	int	tx_st_done;
163
	int	quirks;
164 165 166 167
	bool	high_speed;
	bool	clk_from_cmu;
};

168 169 170
/**
 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
 * @clk: Pointer to the spi clock.
171
 * @src_clk: Pointer to the clock used to generate SPI signals.
172 173 174 175 176 177 178 179 180
 * @master: Pointer to the SPI Protocol master.
 * @cntrlr_info: Platform specific data for the controller this driver manages.
 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
 * @lock: Controller specific lock.
 * @state: Set of FLAGS to indicate status.
 * @rx_dmach: Controller's DMA channel for Rx.
 * @tx_dmach: Controller's DMA channel for Tx.
 * @sfr_start: BUS address of SPI controller regs.
 * @regs: Pointer to ioremap'ed controller registers.
M
Mark Brown 已提交
181
 * @irq: interrupt
182 183 184 185 186 187 188 189
 * @xfer_completion: To indicate completion of xfer task.
 * @cur_mode: Stores the active configuration of the controller.
 * @cur_bpw: Stores the active bits per word settings.
 * @cur_speed: Stores the active xfer clock speed.
 */
struct s3c64xx_spi_driver_data {
	void __iomem                    *regs;
	struct clk                      *clk;
190
	struct clk                      *src_clk;
191 192
	struct platform_device          *pdev;
	struct spi_master               *master;
193
	struct s3c64xx_spi_info  *cntrlr_info;
194 195 196 197 198 199 200
	struct spi_device               *tgl_spi;
	spinlock_t                      lock;
	unsigned long                   sfr_start;
	struct completion               xfer_completion;
	unsigned                        state;
	unsigned                        cur_mode, cur_bpw;
	unsigned                        cur_speed;
B
Boojin Kim 已提交
201 202
	struct s3c64xx_spi_dma_data	rx_dma;
	struct s3c64xx_spi_dma_data	tx_dma;
203
#ifdef CONFIG_S3C_DMA
204
	struct samsung_dma_ops		*ops;
205
#endif
206 207
	struct s3c64xx_spi_port_config	*port_conf;
	unsigned int			port_id;
208
	bool				cs_gpio;
209 210 211 212 213 214 215 216 217 218
};

static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
{
	void __iomem *regs = sdd->regs;
	unsigned long loops;
	u32 val;

	writel(0, regs + S3C64XX_SPI_PACKET_CNT);

219 220 221 222
	val = readl(regs + S3C64XX_SPI_CH_CFG);
	val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
	writel(val, regs + S3C64XX_SPI_CH_CFG);

223 224 225 226 227 228 229 230 231
	val = readl(regs + S3C64XX_SPI_CH_CFG);
	val |= S3C64XX_SPI_CH_SW_RST;
	val &= ~S3C64XX_SPI_CH_HS_EN;
	writel(val, regs + S3C64XX_SPI_CH_CFG);

	/* Flush TxFIFO*/
	loops = msecs_to_loops(1);
	do {
		val = readl(regs + S3C64XX_SPI_STATUS);
232
	} while (TX_FIFO_LVL(val, sdd) && loops--);
233

234 235 236
	if (loops == 0)
		dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");

237 238 239 240
	/* Flush RxFIFO*/
	loops = msecs_to_loops(1);
	do {
		val = readl(regs + S3C64XX_SPI_STATUS);
241
		if (RX_FIFO_LVL(val, sdd))
242 243 244 245 246
			readl(regs + S3C64XX_SPI_RX_DATA);
		else
			break;
	} while (loops--);

247 248 249
	if (loops == 0)
		dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");

250 251 252 253 254 255 256 257 258
	val = readl(regs + S3C64XX_SPI_CH_CFG);
	val &= ~S3C64XX_SPI_CH_SW_RST;
	writel(val, regs + S3C64XX_SPI_CH_CFG);

	val = readl(regs + S3C64XX_SPI_MODE_CFG);
	val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
	writel(val, regs + S3C64XX_SPI_MODE_CFG);
}

B
Boojin Kim 已提交
259
static void s3c64xx_spi_dmacb(void *data)
260
{
B
Boojin Kim 已提交
261 262
	struct s3c64xx_spi_driver_data *sdd;
	struct s3c64xx_spi_dma_data *dma = data;
263 264
	unsigned long flags;

265
	if (dma->direction == DMA_DEV_TO_MEM)
B
Boojin Kim 已提交
266 267 268 269 270 271
		sdd = container_of(data,
			struct s3c64xx_spi_driver_data, rx_dma);
	else
		sdd = container_of(data,
			struct s3c64xx_spi_driver_data, tx_dma);

272 273
	spin_lock_irqsave(&sdd->lock, flags);

274
	if (dma->direction == DMA_DEV_TO_MEM) {
B
Boojin Kim 已提交
275 276 277 278 279 280 281 282
		sdd->state &= ~RXBUSY;
		if (!(sdd->state & TXBUSY))
			complete(&sdd->xfer_completion);
	} else {
		sdd->state &= ~TXBUSY;
		if (!(sdd->state & RXBUSY))
			complete(&sdd->xfer_completion);
	}
283 284 285 286

	spin_unlock_irqrestore(&sdd->lock, flags);
}

287
#ifdef CONFIG_S3C_DMA
288 289 290 291 292 293
/* FIXME: remove this section once arch/arm/mach-s3c64xx uses dmaengine */

static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
	.name = "samsung-spi-dma",
};

B
Boojin Kim 已提交
294 295
static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
					unsigned len, dma_addr_t buf)
296
{
B
Boojin Kim 已提交
297
	struct s3c64xx_spi_driver_data *sdd;
298 299
	struct samsung_dma_prep info;
	struct samsung_dma_config config;
300

301
	if (dma->direction == DMA_DEV_TO_MEM) {
B
Boojin Kim 已提交
302 303
		sdd = container_of((void *)dma,
			struct s3c64xx_spi_driver_data, rx_dma);
304 305 306
		config.direction = sdd->rx_dma.direction;
		config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
		config.width = sdd->cur_bpw / 8;
307
		sdd->ops->config((enum dma_ch)sdd->rx_dma.ch, &config);
308
	} else {
B
Boojin Kim 已提交
309 310
		sdd = container_of((void *)dma,
			struct s3c64xx_spi_driver_data, tx_dma);
311 312 313
		config.direction =  sdd->tx_dma.direction;
		config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
		config.width = sdd->cur_bpw / 8;
314
		sdd->ops->config((enum dma_ch)sdd->tx_dma.ch, &config);
315
	}
316

B
Boojin Kim 已提交
317 318 319 320 321 322 323
	info.cap = DMA_SLAVE;
	info.len = len;
	info.fp = s3c64xx_spi_dmacb;
	info.fp_param = dma;
	info.direction = dma->direction;
	info.buf = buf;

324 325
	sdd->ops->prepare((enum dma_ch)dma->ch, &info);
	sdd->ops->trigger((enum dma_ch)dma->ch);
B
Boojin Kim 已提交
326
}
327

B
Boojin Kim 已提交
328 329
static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
{
330
	struct samsung_dma_req req;
331
	struct device *dev = &sdd->pdev->dev;
B
Boojin Kim 已提交
332 333 334

	sdd->ops = samsung_dma_get_ops();

335 336 337
	req.cap = DMA_SLAVE;
	req.client = &s3c64xx_spi_dma_client;

J
Jingoo Han 已提交
338 339 340 341
	sdd->rx_dma.ch = (struct dma_chan *)(unsigned long)sdd->ops->request(
					sdd->rx_dma.dmach, &req, dev, "rx");
	sdd->tx_dma.ch = (struct dma_chan *)(unsigned long)sdd->ops->request(
					sdd->tx_dma.dmach, &req, dev, "tx");
B
Boojin Kim 已提交
342 343

	return 1;
344 345
}

346 347 348 349
static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
{
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);

350 351 352 353 354 355
	/*
	 * If DMA resource was not available during
	 * probe, no need to continue with dma requests
	 * else Acquire DMA channels
	 */
	while (!is_polling(sdd) && !acquire_dma(sdd))
356 357 358 359 360 361 362 363 364 365
		usleep_range(10000, 11000);

	return 0;
}

static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
{
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);

	/* Free DMA channels */
366 367 368 369 370 371
	if (!is_polling(sdd)) {
		sdd->ops->release((enum dma_ch)sdd->rx_dma.ch,
					&s3c64xx_spi_dma_client);
		sdd->ops->release((enum dma_ch)sdd->tx_dma.ch,
					&s3c64xx_spi_dma_client);
	}
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

	return 0;
}

static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
				 struct s3c64xx_spi_dma_data *dma)
{
	sdd->ops->stop((enum dma_ch)dma->ch);
}
#else

static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
					unsigned len, dma_addr_t buf)
{
	struct s3c64xx_spi_driver_data *sdd;
	struct dma_slave_config config;
	struct dma_async_tx_descriptor *desc;

390 391
	memset(&config, 0, sizeof(config));

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	if (dma->direction == DMA_DEV_TO_MEM) {
		sdd = container_of((void *)dma,
			struct s3c64xx_spi_driver_data, rx_dma);
		config.direction = dma->direction;
		config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
		config.src_addr_width = sdd->cur_bpw / 8;
		config.src_maxburst = 1;
		dmaengine_slave_config(dma->ch, &config);
	} else {
		sdd = container_of((void *)dma,
			struct s3c64xx_spi_driver_data, tx_dma);
		config.direction = dma->direction;
		config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
		config.dst_addr_width = sdd->cur_bpw / 8;
		config.dst_maxburst = 1;
		dmaengine_slave_config(dma->ch, &config);
	}

410 411
	desc = dmaengine_prep_slave_single(dma->ch, buf, len,
					dma->direction, DMA_PREP_INTERRUPT);
412 413 414 415 416 417 418 419 420 421 422 423 424 425

	desc->callback = s3c64xx_spi_dmacb;
	desc->callback_param = dma;

	dmaengine_submit(desc);
	dma_async_issue_pending(dma->ch);
}

static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
{
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
	dma_filter_fn filter = sdd->cntrlr_info->filter;
	struct device *dev = &sdd->pdev->dev;
	dma_cap_mask_t mask;
426
	int ret;
427

428 429 430 431 432 433 434 435 436 437 438 439
	if (!is_polling(sdd)) {
		dma_cap_zero(mask);
		dma_cap_set(DMA_SLAVE, mask);

		/* Acquire DMA channels */
		sdd->rx_dma.ch = dma_request_slave_channel_compat(mask, filter,
				   (void *)sdd->rx_dma.dmach, dev, "rx");
		if (!sdd->rx_dma.ch) {
			dev_err(dev, "Failed to get RX DMA channel\n");
			ret = -EBUSY;
			goto out;
		}
440

441 442 443 444 445 446 447
		sdd->tx_dma.ch = dma_request_slave_channel_compat(mask, filter,
				   (void *)sdd->tx_dma.dmach, dev, "tx");
		if (!sdd->tx_dma.ch) {
			dev_err(dev, "Failed to get TX DMA channel\n");
			ret = -EBUSY;
			goto out_rx;
		}
448 449 450
	}

	ret = pm_runtime_get_sync(&sdd->pdev->dev);
451
	if (ret < 0) {
452 453 454
		dev_err(dev, "Failed to enable device: %d\n", ret);
		goto out_tx;
	}
455 456

	return 0;
457 458 459 460 461 462 463

out_tx:
	dma_release_channel(sdd->tx_dma.ch);
out_rx:
	dma_release_channel(sdd->rx_dma.ch);
out:
	return ret;
464 465 466 467 468 469 470
}

static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
{
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);

	/* Free DMA channels */
471 472 473 474
	if (!is_polling(sdd)) {
		dma_release_channel(sdd->rx_dma.ch);
		dma_release_channel(sdd->tx_dma.ch);
	}
475 476 477 478 479 480 481 482 483 484 485 486

	pm_runtime_put(&sdd->pdev->dev);
	return 0;
}

static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
				 struct s3c64xx_spi_dma_data *dma)
{
	dmaengine_terminate_all(dma->ch);
}
#endif

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
				struct spi_device *spi,
				struct spi_transfer *xfer, int dma_mode)
{
	void __iomem *regs = sdd->regs;
	u32 modecfg, chcfg;

	modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
	modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);

	chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
	chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;

	if (dma_mode) {
		chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
	} else {
		/* Always shift in data in FIFO, even if xfer is Tx only,
		 * this helps setting PCKT_CNT value for generating clocks
		 * as exactly needed.
		 */
		chcfg |= S3C64XX_SPI_CH_RXCH_ON;
		writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
					| S3C64XX_SPI_PACKET_CNT_EN,
					regs + S3C64XX_SPI_PACKET_CNT);
	}

	if (xfer->tx_buf != NULL) {
		sdd->state |= TXBUSY;
		chcfg |= S3C64XX_SPI_CH_TXCH_ON;
		if (dma_mode) {
			modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
B
Boojin Kim 已提交
518
			prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
519
		} else {
520 521 522 523 524 525 526 527 528 529 530 531 532 533
			switch (sdd->cur_bpw) {
			case 32:
				iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
					xfer->tx_buf, xfer->len / 4);
				break;
			case 16:
				iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
					xfer->tx_buf, xfer->len / 2);
				break;
			default:
				iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
					xfer->tx_buf, xfer->len);
				break;
			}
534 535 536 537 538 539
		}
	}

	if (xfer->rx_buf != NULL) {
		sdd->state |= RXBUSY;

540
		if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
541 542 543 544 545 546 547 548 549
					&& !(sdd->cur_mode & SPI_CPHA))
			chcfg |= S3C64XX_SPI_CH_HS_EN;

		if (dma_mode) {
			modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
			chcfg |= S3C64XX_SPI_CH_RXCH_ON;
			writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
					| S3C64XX_SPI_PACKET_CNT_EN,
					regs + S3C64XX_SPI_PACKET_CNT);
B
Boojin Kim 已提交
550
			prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		}
	}

	writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
	writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
}

static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
						struct spi_device *spi)
{
	struct s3c64xx_spi_csinfo *cs;

	if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
		if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
			/* Deselect the last toggled device */
			cs = sdd->tgl_spi->controller_data;
567 568 569
			if (sdd->cs_gpio)
				gpio_set_value(cs->line,
					spi->mode & SPI_CS_HIGH ? 0 : 1);
570 571 572 573 574
		}
		sdd->tgl_spi = NULL;
	}

	cs = spi->controller_data;
575 576
	if (sdd->cs_gpio)
		gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
577 578 579 580 581

	/* Start the signals */
	writel(0, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
}

582
static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
					int timeout_ms)
{
	void __iomem *regs = sdd->regs;
	unsigned long val = 1;
	u32 status;

	/* max fifo depth available */
	u32 max_fifo = (FIFO_LVL_MASK(sdd) >> 1) + 1;

	if (timeout_ms)
		val = msecs_to_loops(timeout_ms);

	do {
		status = readl(regs + S3C64XX_SPI_STATUS);
	} while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);

	/* return the actual received data length */
	return RX_FIFO_LVL(status, sdd);
601 602 603 604 605 606 607 608 609 610 611
}

static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
				struct spi_transfer *xfer, int dma_mode)
{
	void __iomem *regs = sdd->regs;
	unsigned long val;
	int ms;

	/* millisecs to xfer 'len' bytes @ 'cur_speed' */
	ms = xfer->len * 8 * 1000 / sdd->cur_speed;
612
	ms += 10; /* some tolerance */
613 614 615 616 617

	if (dma_mode) {
		val = msecs_to_jiffies(ms) + 10;
		val = wait_for_completion_timeout(&sdd->xfer_completion, val);
	} else {
618
		u32 status;
619 620
		val = msecs_to_loops(ms);
		do {
621
			status = readl(regs + S3C64XX_SPI_STATUS);
622
		} while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
623 624 625 626 627 628
	}

	if (dma_mode) {
		u32 status;

		/*
629 630
		 * If the previous xfer was completed within timeout, then
		 * proceed further else return -EIO.
631 632 633 634 635 636
		 * DmaTx returns after simply writing data in the FIFO,
		 * w/o waiting for real transmission on the bus to finish.
		 * DmaRx returns only after Dma read data from FIFO which
		 * needs bus transmission to finish, so we don't worry if
		 * Xfer involved Rx(with or without Tx).
		 */
637
		if (val && !xfer->rx_buf) {
638 639
			val = msecs_to_loops(10);
			status = readl(regs + S3C64XX_SPI_STATUS);
640 641
			while ((TX_FIFO_LVL(status, sdd)
				|| !S3C64XX_SPI_ST_TX_DONE(status, sdd))
642 643 644 645 646 647
					&& --val) {
				cpu_relax();
				status = readl(regs + S3C64XX_SPI_STATUS);
			}

		}
648 649 650 651

		/* If timed out while checking rx/tx status return error */
		if (!val)
			return -EIO;
652
	} else {
653 654 655 656
		int loops;
		u32 cpy_len;
		u8 *buf;

657
		/* If it was only Tx */
658
		if (!xfer->rx_buf) {
659 660 661 662
			sdd->state &= ~TXBUSY;
			return 0;
		}

663 664 665 666 667 668 669 670 671 672 673 674
		/*
		 * If the receive length is bigger than the controller fifo
		 * size, calculate the loops and read the fifo as many times.
		 * loops = length / max fifo size (calculated by using the
		 * fifo mask).
		 * For any size less than the fifo size the below code is
		 * executed atleast once.
		 */
		loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);
		buf = xfer->rx_buf;
		do {
			/* wait for data to be received in the fifo */
675 676
			cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
						(loops ? ms : 0));
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

			switch (sdd->cur_bpw) {
			case 32:
				ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
					buf, cpy_len / 4);
				break;
			case 16:
				ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
					buf, cpy_len / 2);
				break;
			default:
				ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
					buf, cpy_len);
				break;
			}

			buf = buf + cpy_len;
		} while (loops--);
695 696 697 698 699 700 701 702 703 704 705 706 707 708
		sdd->state &= ~RXBUSY;
	}

	return 0;
}

static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
						struct spi_device *spi)
{
	struct s3c64xx_spi_csinfo *cs = spi->controller_data;

	if (sdd->tgl_spi == spi)
		sdd->tgl_spi = NULL;

709 710
	if (sdd->cs_gpio)
		gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
711 712 713

	/* Quiese the signals */
	writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
714 715 716 717 718 719 720 721
}

static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
{
	void __iomem *regs = sdd->regs;
	u32 val;

	/* Disable Clock */
722
	if (sdd->port_conf->clk_from_cmu) {
723
		clk_disable_unprepare(sdd->src_clk);
724 725 726 727 728
	} else {
		val = readl(regs + S3C64XX_SPI_CLK_CFG);
		val &= ~S3C64XX_SPI_ENCLK_ENABLE;
		writel(val, regs + S3C64XX_SPI_CLK_CFG);
	}
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

	/* Set Polarity and Phase */
	val = readl(regs + S3C64XX_SPI_CH_CFG);
	val &= ~(S3C64XX_SPI_CH_SLAVE |
			S3C64XX_SPI_CPOL_L |
			S3C64XX_SPI_CPHA_B);

	if (sdd->cur_mode & SPI_CPOL)
		val |= S3C64XX_SPI_CPOL_L;

	if (sdd->cur_mode & SPI_CPHA)
		val |= S3C64XX_SPI_CPHA_B;

	writel(val, regs + S3C64XX_SPI_CH_CFG);

	/* Set Channel & DMA Mode */
	val = readl(regs + S3C64XX_SPI_MODE_CFG);
	val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
			| S3C64XX_SPI_MODE_CH_TSZ_MASK);

	switch (sdd->cur_bpw) {
	case 32:
		val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
752
		val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
753 754 755
		break;
	case 16:
		val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
756
		val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
757 758 759
		break;
	default:
		val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
760
		val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
761 762 763 764 765
		break;
	}

	writel(val, regs + S3C64XX_SPI_MODE_CFG);

766
	if (sdd->port_conf->clk_from_cmu) {
767 768 769 770
		/* Configure Clock */
		/* There is half-multiplier before the SPI */
		clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
		/* Enable Clock */
771
		clk_prepare_enable(sdd->src_clk);
772 773 774 775 776 777 778 779 780 781 782 783 784
	} else {
		/* Configure Clock */
		val = readl(regs + S3C64XX_SPI_CLK_CFG);
		val &= ~S3C64XX_SPI_PSR_MASK;
		val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
				& S3C64XX_SPI_PSR_MASK);
		writel(val, regs + S3C64XX_SPI_CLK_CFG);

		/* Enable Clock */
		val = readl(regs + S3C64XX_SPI_CLK_CFG);
		val |= S3C64XX_SPI_ENCLK_ENABLE;
		writel(val, regs + S3C64XX_SPI_CLK_CFG);
	}
785 786 787 788 789 790 791 792 793 794
}

#define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)

static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
						struct spi_message *msg)
{
	struct device *dev = &sdd->pdev->dev;
	struct spi_transfer *xfer;

795
	if (is_polling(sdd) || msg->is_dma_mapped)
796 797 798 799 800 801 802 803 804 805 806
		return 0;

	/* First mark all xfer unmapped */
	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
		xfer->rx_dma = XFER_DMAADDR_INVALID;
		xfer->tx_dma = XFER_DMAADDR_INVALID;
	}

	/* Map until end or first fail */
	list_for_each_entry(xfer, &msg->transfers, transfer_list) {

807
		if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
808 809
			continue;

810
		if (xfer->tx_buf != NULL) {
811 812 813
			xfer->tx_dma = dma_map_single(dev,
					(void *)xfer->tx_buf, xfer->len,
					DMA_TO_DEVICE);
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
			if (dma_mapping_error(dev, xfer->tx_dma)) {
				dev_err(dev, "dma_map_single Tx failed\n");
				xfer->tx_dma = XFER_DMAADDR_INVALID;
				return -ENOMEM;
			}
		}

		if (xfer->rx_buf != NULL) {
			xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
						xfer->len, DMA_FROM_DEVICE);
			if (dma_mapping_error(dev, xfer->rx_dma)) {
				dev_err(dev, "dma_map_single Rx failed\n");
				dma_unmap_single(dev, xfer->tx_dma,
						xfer->len, DMA_TO_DEVICE);
				xfer->tx_dma = XFER_DMAADDR_INVALID;
				xfer->rx_dma = XFER_DMAADDR_INVALID;
				return -ENOMEM;
			}
		}
	}

	return 0;
}

static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
						struct spi_message *msg)
{
	struct device *dev = &sdd->pdev->dev;
	struct spi_transfer *xfer;

844
	if (is_polling(sdd) || msg->is_dma_mapped)
845 846 847 848
		return;

	list_for_each_entry(xfer, &msg->transfers, transfer_list) {

849
		if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
850 851
			continue;

852 853 854 855 856 857 858 859 860 861 862 863
		if (xfer->rx_buf != NULL
				&& xfer->rx_dma != XFER_DMAADDR_INVALID)
			dma_unmap_single(dev, xfer->rx_dma,
						xfer->len, DMA_FROM_DEVICE);

		if (xfer->tx_buf != NULL
				&& xfer->tx_dma != XFER_DMAADDR_INVALID)
			dma_unmap_single(dev, xfer->tx_dma,
						xfer->len, DMA_TO_DEVICE);
	}
}

864 865
static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
					    struct spi_message *msg)
866
{
867
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
868 869 870 871 872 873 874 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
	struct spi_device *spi = msg->spi;
	struct s3c64xx_spi_csinfo *cs = spi->controller_data;
	struct spi_transfer *xfer;
	int status = 0, cs_toggle = 0;
	u32 speed;
	u8 bpw;

	/* If Master's(controller) state differs from that needed by Slave */
	if (sdd->cur_speed != spi->max_speed_hz
			|| sdd->cur_mode != spi->mode
			|| sdd->cur_bpw != spi->bits_per_word) {
		sdd->cur_bpw = spi->bits_per_word;
		sdd->cur_speed = spi->max_speed_hz;
		sdd->cur_mode = spi->mode;
		s3c64xx_spi_config(sdd);
	}

	/* Map all the transfers if needed */
	if (s3c64xx_spi_map_mssg(sdd, msg)) {
		dev_err(&spi->dev,
			"Xfer: Unable to map message buffers!\n");
		status = -ENOMEM;
		goto out;
	}

	/* Configure feedback delay */
	writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);

	list_for_each_entry(xfer, &msg->transfers, transfer_list) {

		unsigned long flags;
		int use_dma;

		INIT_COMPLETION(sdd->xfer_completion);

		/* Only BPW and Speed may change across transfers */
904
		bpw = xfer->bits_per_word;
905 906
		speed = xfer->speed_hz ? : spi->max_speed_hz;

907 908 909 910 911 912 913 914
		if (xfer->len % (bpw / 8)) {
			dev_err(&spi->dev,
				"Xfer length(%u) not a multiple of word size(%u)\n",
				xfer->len, bpw / 8);
			status = -EIO;
			goto out;
		}

915 916 917 918 919 920 921
		if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
			sdd->cur_bpw = bpw;
			sdd->cur_speed = speed;
			s3c64xx_spi_config(sdd);
		}

		/* Polling method for xfers not bigger than FIFO capacity */
922
		use_dma = 0;
923 924 925
		if (!is_polling(sdd) &&
			(sdd->rx_dma.ch && sdd->tx_dma.ch &&
			(xfer->len > ((FIFO_LVL_MASK(sdd) >> 1) + 1))))
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
			use_dma = 1;

		spin_lock_irqsave(&sdd->lock, flags);

		/* Pending only which is to be done */
		sdd->state &= ~RXBUSY;
		sdd->state &= ~TXBUSY;

		enable_datapath(sdd, spi, xfer, use_dma);

		/* Slave Select */
		enable_cs(sdd, spi);

		spin_unlock_irqrestore(&sdd->lock, flags);

		status = wait_for_xfer(sdd, xfer, use_dma);

		if (status) {
944
			dev_err(&spi->dev, "I/O Error: rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
945 946 947 948 949 950 951 952
				xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
				(sdd->state & RXBUSY) ? 'f' : 'p',
				(sdd->state & TXBUSY) ? 'f' : 'p',
				xfer->len);

			if (use_dma) {
				if (xfer->tx_buf != NULL
						&& (sdd->state & TXBUSY))
953
					s3c64xx_spi_dma_stop(sdd, &sdd->tx_dma);
954 955
				if (xfer->rx_buf != NULL
						&& (sdd->state & RXBUSY))
956
					s3c64xx_spi_dma_stop(sdd, &sdd->rx_dma);
957 958 959 960 961
			}

			goto out;
		}

962 963
		flush_fifo(sdd);

964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
		if (xfer->delay_usecs)
			udelay(xfer->delay_usecs);

		if (xfer->cs_change) {
			/* Hint that the next mssg is gonna be
			   for the same device */
			if (list_is_last(&xfer->transfer_list,
						&msg->transfers))
				cs_toggle = 1;
		}

		msg->actual_length += xfer->len;
	}

out:
	if (!cs_toggle || status)
		disable_cs(sdd, spi);
	else
		sdd->tgl_spi = spi;

	s3c64xx_spi_unmap_mssg(sdd, msg);

	msg->status = status;

988 989 990
	spi_finalize_current_message(master);

	return 0;
991 992
}

993 994 995 996
static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
				struct spi_device *spi)
{
	struct s3c64xx_spi_csinfo *cs;
997
	struct device_node *slave_np, *data_np = NULL;
998
	struct s3c64xx_spi_driver_data *sdd;
999 1000
	u32 fb_delay = 0;

1001
	sdd = spi_master_get_devdata(spi->master);
1002 1003 1004 1005 1006 1007
	slave_np = spi->dev.of_node;
	if (!slave_np) {
		dev_err(&spi->dev, "device node not found\n");
		return ERR_PTR(-EINVAL);
	}

1008
	data_np = of_get_child_by_name(slave_np, "controller-data");
1009 1010 1011 1012 1013 1014 1015
	if (!data_np) {
		dev_err(&spi->dev, "child node 'controller-data' not found\n");
		return ERR_PTR(-EINVAL);
	}

	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
	if (!cs) {
1016
		dev_err(&spi->dev, "could not allocate memory for controller data\n");
1017
		of_node_put(data_np);
1018 1019 1020
		return ERR_PTR(-ENOMEM);
	}

1021 1022 1023 1024
	/* The CS line is asserted/deasserted by the gpio pin */
	if (sdd->cs_gpio)
		cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);

1025
	if (!gpio_is_valid(cs->line)) {
1026
		dev_err(&spi->dev, "chip select gpio is not specified or invalid\n");
1027
		kfree(cs);
1028
		of_node_put(data_np);
1029 1030 1031 1032 1033
		return ERR_PTR(-EINVAL);
	}

	of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
	cs->fb_delay = fb_delay;
1034
	of_node_put(data_np);
1035 1036 1037
	return cs;
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
/*
 * Here we only check the validity of requested configuration
 * and save the configuration in a local data-structure.
 * The controller is actually configured only just before we
 * get a message to transfer.
 */
static int s3c64xx_spi_setup(struct spi_device *spi)
{
	struct s3c64xx_spi_csinfo *cs = spi->controller_data;
	struct s3c64xx_spi_driver_data *sdd;
1048
	struct s3c64xx_spi_info *sci;
1049
	int err;
1050

1051 1052
	sdd = spi_master_get_devdata(spi->master);
	if (!cs && spi->dev.of_node) {
1053
		cs = s3c64xx_get_slave_ctrldata(spi);
1054 1055 1056 1057
		spi->controller_data = cs;
	}

	if (IS_ERR_OR_NULL(cs)) {
1058 1059 1060 1061
		dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
		return -ENODEV;
	}

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	if (!spi_get_ctldata(spi)) {
		/* Request gpio only if cs line is asserted by gpio pins */
		if (sdd->cs_gpio) {
			err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
					dev_name(&spi->dev));
			if (err) {
				dev_err(&spi->dev,
					"Failed to get /CS gpio [%d]: %d\n",
					cs->line, err);
				goto err_gpio_req;
			}
1073 1074
		}

1075
		spi_set_ctldata(spi, cs);
1076 1077 1078 1079
	}

	sci = sdd->cntrlr_info;

1080 1081
	pm_runtime_get_sync(&sdd->pdev->dev);

1082
	/* Check if we can provide the requested rate */
1083
	if (!sdd->port_conf->clk_from_cmu) {
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
		u32 psr, speed;

		/* Max possible */
		speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);

		if (spi->max_speed_hz > speed)
			spi->max_speed_hz = speed;

		psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
		psr &= S3C64XX_SPI_PSR_MASK;
		if (psr == S3C64XX_SPI_PSR_MASK)
			psr--;

		speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
		if (spi->max_speed_hz < speed) {
			if (psr+1 < S3C64XX_SPI_PSR_MASK) {
				psr++;
			} else {
				err = -EINVAL;
				goto setup_exit;
			}
		}
1106

1107
		speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
1108
		if (spi->max_speed_hz >= speed) {
1109
			spi->max_speed_hz = speed;
1110
		} else {
1111 1112
			dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
				spi->max_speed_hz);
1113
			err = -EINVAL;
1114 1115
			goto setup_exit;
		}
1116 1117
	}

1118
	pm_runtime_put(&sdd->pdev->dev);
1119 1120
	disable_cs(sdd, spi);
	return 0;
1121

1122 1123 1124 1125
setup_exit:
	/* setup() returns with device de-selected */
	disable_cs(sdd, spi);

1126 1127 1128 1129
	gpio_free(cs->line);
	spi_set_ctldata(spi, NULL);

err_gpio_req:
1130 1131
	if (spi->dev.of_node)
		kfree(cs);
1132

1133 1134 1135
	return err;
}

1136 1137 1138
static void s3c64xx_spi_cleanup(struct spi_device *spi)
{
	struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
1139
	struct s3c64xx_spi_driver_data *sdd;
1140

1141 1142
	sdd = spi_master_get_devdata(spi->master);
	if (cs && sdd->cs_gpio) {
1143
		gpio_free(cs->line);
1144 1145 1146
		if (spi->dev.of_node)
			kfree(cs);
	}
1147 1148 1149
	spi_set_ctldata(spi, NULL);
}

M
Mark Brown 已提交
1150 1151 1152 1153
static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
{
	struct s3c64xx_spi_driver_data *sdd = data;
	struct spi_master *spi = sdd->master;
1154
	unsigned int val, clr = 0;
M
Mark Brown 已提交
1155

1156
	val = readl(sdd->regs + S3C64XX_SPI_STATUS);
M
Mark Brown 已提交
1157

1158 1159
	if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
		clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
M
Mark Brown 已提交
1160
		dev_err(&spi->dev, "RX overrun\n");
1161 1162 1163
	}
	if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
		clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
M
Mark Brown 已提交
1164
		dev_err(&spi->dev, "RX underrun\n");
1165 1166 1167
	}
	if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
		clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
M
Mark Brown 已提交
1168
		dev_err(&spi->dev, "TX overrun\n");
1169 1170 1171
	}
	if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
		clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
M
Mark Brown 已提交
1172
		dev_err(&spi->dev, "TX underrun\n");
1173 1174 1175 1176 1177
	}

	/* Clear the pending irq by setting and then clearing it */
	writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
	writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
M
Mark Brown 已提交
1178 1179 1180 1181

	return IRQ_HANDLED;
}

1182 1183
static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
{
1184
	struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1185 1186 1187 1188 1189
	void __iomem *regs = sdd->regs;
	unsigned int val;

	sdd->cur_speed = 0;

1190
	writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
1191 1192 1193 1194

	/* Disable Interrupts - we use Polling if not DMA mode */
	writel(0, regs + S3C64XX_SPI_INT_EN);

1195
	if (!sdd->port_conf->clk_from_cmu)
1196
		writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
1197 1198 1199 1200
				regs + S3C64XX_SPI_CLK_CFG);
	writel(0, regs + S3C64XX_SPI_MODE_CFG);
	writel(0, regs + S3C64XX_SPI_PACKET_CNT);

1201 1202 1203 1204 1205 1206 1207
	/* Clear any irq pending bits, should set and clear the bits */
	val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
		S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
		S3C64XX_SPI_PND_TX_OVERRUN_CLR |
		S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
	writel(val, regs + S3C64XX_SPI_PENDING_CLR);
	writel(0, regs + S3C64XX_SPI_PENDING_CLR);
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

	writel(0, regs + S3C64XX_SPI_SWAP_CFG);

	val = readl(regs + S3C64XX_SPI_MODE_CFG);
	val &= ~S3C64XX_SPI_MODE_4BURST;
	val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
	val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
	writel(val, regs + S3C64XX_SPI_MODE_CFG);

	flush_fifo(sdd);
}

1220
#ifdef CONFIG_OF
1221
static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
{
	struct s3c64xx_spi_info *sci;
	u32 temp;

	sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
	if (!sci) {
		dev_err(dev, "memory allocation for spi_info failed\n");
		return ERR_PTR(-ENOMEM);
	}

	if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1233
		dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
1234 1235 1236 1237 1238 1239
		sci->src_clk_nr = 0;
	} else {
		sci->src_clk_nr = temp;
	}

	if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1240
		dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
		sci->num_cs = 1;
	} else {
		sci->num_cs = temp;
	}

	return sci;
}
#else
static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
{
J
Jingoo Han 已提交
1251
	return dev_get_platdata(dev);
1252 1253 1254 1255 1256
}
#endif

static const struct of_device_id s3c64xx_spi_dt_match[];

1257 1258 1259
static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
						struct platform_device *pdev)
{
1260 1261 1262 1263 1264 1265 1266
#ifdef CONFIG_OF
	if (pdev->dev.of_node) {
		const struct of_device_id *match;
		match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
		return (struct s3c64xx_spi_port_config *)match->data;
	}
#endif
1267 1268 1269 1270
	return (struct s3c64xx_spi_port_config *)
			 platform_get_device_id(pdev)->driver_data;
}

1271
static int s3c64xx_spi_probe(struct platform_device *pdev)
1272
{
1273
	struct resource	*mem_res;
1274
	struct resource	*res;
1275
	struct s3c64xx_spi_driver_data *sdd;
J
Jingoo Han 已提交
1276
	struct s3c64xx_spi_info *sci = dev_get_platdata(&pdev->dev);
1277
	struct spi_master *master;
M
Mark Brown 已提交
1278
	int ret, irq;
1279
	char clk_name[16];
1280

1281 1282 1283 1284
	if (!sci && pdev->dev.of_node) {
		sci = s3c64xx_spi_parse_dt(&pdev->dev);
		if (IS_ERR(sci))
			return PTR_ERR(sci);
1285 1286
	}

1287
	if (!sci) {
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
		dev_err(&pdev->dev, "platform_data missing!\n");
		return -ENODEV;
	}

	mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (mem_res == NULL) {
		dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
		return -ENXIO;
	}

M
Mark Brown 已提交
1298 1299 1300 1301 1302 1303
	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
		return irq;
	}

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	master = spi_alloc_master(&pdev->dev,
				sizeof(struct s3c64xx_spi_driver_data));
	if (master == NULL) {
		dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
		return -ENOMEM;
	}

	platform_set_drvdata(pdev, master);

	sdd = spi_master_get_devdata(master);
1314
	sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
1315 1316 1317 1318
	sdd->master = master;
	sdd->cntrlr_info = sci;
	sdd->pdev = pdev;
	sdd->sfr_start = mem_res->start;
1319
	sdd->cs_gpio = true;
1320
	if (pdev->dev.of_node) {
1321 1322 1323
		if (!of_find_property(pdev->dev.of_node, "cs-gpio", NULL))
			sdd->cs_gpio = false;

1324 1325
		ret = of_alias_get_id(pdev->dev.of_node, "spi");
		if (ret < 0) {
1326 1327
			dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
				ret);
1328 1329 1330 1331 1332 1333
			goto err0;
		}
		sdd->port_id = ret;
	} else {
		sdd->port_id = pdev->id;
	}
1334 1335 1336

	sdd->cur_bpw = 8;

1337 1338 1339
	if (!sdd->pdev->dev.of_node) {
		res = platform_get_resource(pdev, IORESOURCE_DMA,  0);
		if (!res) {
1340
			dev_warn(&pdev->dev, "Unable to get SPI tx dma resource. Switching to poll mode\n");
1341 1342 1343
			sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
		} else
			sdd->tx_dma.dmach = res->start;
1344 1345 1346

		res = platform_get_resource(pdev, IORESOURCE_DMA,  1);
		if (!res) {
1347
			dev_warn(&pdev->dev, "Unable to get SPI rx dma resource. Switching to poll mode\n");
1348 1349 1350
			sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
		} else
			sdd->rx_dma.dmach = res->start;
1351
	}
1352

1353 1354
	sdd->tx_dma.direction = DMA_MEM_TO_DEV;
	sdd->rx_dma.direction = DMA_DEV_TO_MEM;
1355 1356

	master->dev.of_node = pdev->dev.of_node;
1357
	master->bus_num = sdd->port_id;
1358
	master->setup = s3c64xx_spi_setup;
1359
	master->cleanup = s3c64xx_spi_cleanup;
1360 1361 1362
	master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
	master->transfer_one_message = s3c64xx_spi_transfer_one_message;
	master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
1363 1364
	master->num_chipselect = sci->num_cs;
	master->dma_alignment = 8;
1365 1366
	master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
					SPI_BPW_MASK(8);
1367 1368
	/* the spi->mode bits understood by this driver: */
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1369
	master->auto_runtime_pm = true;
1370

1371 1372 1373
	sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
	if (IS_ERR(sdd->regs)) {
		ret = PTR_ERR(sdd->regs);
1374
		goto err0;
1375 1376
	}

1377
	if (sci->cfg_gpio && sci->cfg_gpio()) {
1378 1379
		dev_err(&pdev->dev, "Unable to config gpio\n");
		ret = -EBUSY;
1380
		goto err0;
1381 1382 1383
	}

	/* Setup clocks */
1384
	sdd->clk = devm_clk_get(&pdev->dev, "spi");
1385 1386 1387
	if (IS_ERR(sdd->clk)) {
		dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
		ret = PTR_ERR(sdd->clk);
1388
		goto err0;
1389 1390
	}

1391
	if (clk_prepare_enable(sdd->clk)) {
1392 1393
		dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
		ret = -EBUSY;
1394
		goto err0;
1395 1396
	}

1397
	sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1398
	sdd->src_clk = devm_clk_get(&pdev->dev, clk_name);
1399
	if (IS_ERR(sdd->src_clk)) {
1400
		dev_err(&pdev->dev,
1401
			"Unable to acquire clock '%s'\n", clk_name);
1402
		ret = PTR_ERR(sdd->src_clk);
1403
		goto err2;
1404 1405
	}

1406
	if (clk_prepare_enable(sdd->src_clk)) {
1407
		dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
1408
		ret = -EBUSY;
1409
		goto err2;
1410 1411 1412
	}

	/* Setup Deufult Mode */
1413
	s3c64xx_spi_hwinit(sdd, sdd->port_id);
1414 1415 1416 1417

	spin_lock_init(&sdd->lock);
	init_completion(&sdd->xfer_completion);

1418 1419
	ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
				"spi-s3c64xx", sdd);
M
Mark Brown 已提交
1420 1421 1422
	if (ret != 0) {
		dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
			irq, ret);
1423
		goto err3;
M
Mark Brown 已提交
1424 1425 1426 1427 1428 1429
	}

	writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
	       S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
	       sdd->regs + S3C64XX_SPI_INT_EN);

1430 1431
	pm_runtime_enable(&pdev->dev);

1432 1433 1434
	ret = devm_spi_register_master(&pdev->dev, master);
	if (ret != 0) {
		dev_err(&pdev->dev, "cannot register SPI master: %d\n", ret);
1435
		goto err3;
1436 1437
	}

1438
	dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Slaves attached\n",
1439
					sdd->port_id, master->num_chipselect);
J
Jingoo Han 已提交
1440 1441
	dev_dbg(&pdev->dev, "\tIOmem=[%pR]\tDMA=[Rx-%d, Tx-%d]\n",
					mem_res,
B
Boojin Kim 已提交
1442
					sdd->rx_dma.dmach, sdd->tx_dma.dmach);
1443 1444 1445

	return 0;

1446
err3:
1447
	clk_disable_unprepare(sdd->src_clk);
1448
err2:
1449
	clk_disable_unprepare(sdd->clk);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
err0:
	spi_master_put(master);

	return ret;
}

static int s3c64xx_spi_remove(struct platform_device *pdev)
{
	struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);

1461 1462
	pm_runtime_disable(&pdev->dev);

M
Mark Brown 已提交
1463 1464
	writel(0, sdd->regs + S3C64XX_SPI_INT_EN);

1465
	clk_disable_unprepare(sdd->src_clk);
1466

1467
	clk_disable_unprepare(sdd->clk);
1468 1469 1470 1471

	return 0;
}

1472
#ifdef CONFIG_PM_SLEEP
M
Mark Brown 已提交
1473
static int s3c64xx_spi_suspend(struct device *dev)
1474
{
1475
	struct spi_master *master = dev_get_drvdata(dev);
1476 1477
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);

1478
	spi_master_suspend(master);
1479 1480

	/* Disable the clock */
1481 1482
	clk_disable_unprepare(sdd->src_clk);
	clk_disable_unprepare(sdd->clk);
1483 1484 1485 1486 1487 1488

	sdd->cur_speed = 0; /* Output Clock is stopped */

	return 0;
}

M
Mark Brown 已提交
1489
static int s3c64xx_spi_resume(struct device *dev)
1490
{
1491
	struct spi_master *master = dev_get_drvdata(dev);
1492
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1493
	struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1494

1495
	if (sci->cfg_gpio)
1496
		sci->cfg_gpio();
1497 1498

	/* Enable the clock */
1499 1500
	clk_prepare_enable(sdd->src_clk);
	clk_prepare_enable(sdd->clk);
1501

1502
	s3c64xx_spi_hwinit(sdd, sdd->port_id);
1503

1504
	spi_master_resume(master);
1505 1506 1507

	return 0;
}
1508
#endif /* CONFIG_PM_SLEEP */
1509

1510 1511 1512
#ifdef CONFIG_PM_RUNTIME
static int s3c64xx_spi_runtime_suspend(struct device *dev)
{
1513
	struct spi_master *master = dev_get_drvdata(dev);
1514 1515
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);

1516 1517
	clk_disable_unprepare(sdd->clk);
	clk_disable_unprepare(sdd->src_clk);
1518 1519 1520 1521 1522 1523

	return 0;
}

static int s3c64xx_spi_runtime_resume(struct device *dev)
{
1524
	struct spi_master *master = dev_get_drvdata(dev);
1525
	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1526
	int ret;
1527

1528 1529 1530 1531 1532 1533 1534 1535 1536
	ret = clk_prepare_enable(sdd->src_clk);
	if (ret != 0)
		return ret;

	ret = clk_prepare_enable(sdd->clk);
	if (ret != 0) {
		clk_disable_unprepare(sdd->src_clk);
		return ret;
	}
1537 1538 1539 1540 1541

	return 0;
}
#endif /* CONFIG_PM_RUNTIME */

M
Mark Brown 已提交
1542 1543
static const struct dev_pm_ops s3c64xx_spi_pm = {
	SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
1544 1545
	SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
			   s3c64xx_spi_runtime_resume, NULL)
M
Mark Brown 已提交
1546 1547
};

1548
static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1549 1550 1551 1552 1553 1554
	.fifo_lvl_mask	= { 0x7f },
	.rx_lvl_offset	= 13,
	.tx_st_done	= 21,
	.high_speed	= true,
};

1555
static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1556 1557 1558 1559 1560
	.fifo_lvl_mask	= { 0x7f, 0x7F },
	.rx_lvl_offset	= 13,
	.tx_st_done	= 21,
};

1561
static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
1562 1563 1564 1565 1566
	.fifo_lvl_mask	= { 0x1ff, 0x7F },
	.rx_lvl_offset	= 15,
	.tx_st_done	= 25,
};

1567
static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
1568 1569 1570 1571 1572 1573
	.fifo_lvl_mask	= { 0x7f, 0x7F },
	.rx_lvl_offset	= 13,
	.tx_st_done	= 21,
	.high_speed	= true,
};

1574
static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1575 1576 1577 1578 1579 1580
	.fifo_lvl_mask	= { 0x1ff, 0x7F },
	.rx_lvl_offset	= 15,
	.tx_st_done	= 25,
	.high_speed	= true,
};

1581
static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1582 1583 1584 1585 1586 1587 1588
	.fifo_lvl_mask	= { 0x1ff, 0x7F, 0x7F },
	.rx_lvl_offset	= 15,
	.tx_st_done	= 25,
	.high_speed	= true,
	.clk_from_cmu	= true,
};

1589 1590 1591 1592 1593 1594 1595 1596 1597
static struct s3c64xx_spi_port_config exynos5440_spi_port_config = {
	.fifo_lvl_mask	= { 0x1ff },
	.rx_lvl_offset	= 15,
	.tx_st_done	= 25,
	.high_speed	= true,
	.clk_from_cmu	= true,
	.quirks		= S3C64XX_SPI_QUIRK_POLL,
};

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
static struct platform_device_id s3c64xx_spi_driver_ids[] = {
	{
		.name		= "s3c2443-spi",
		.driver_data	= (kernel_ulong_t)&s3c2443_spi_port_config,
	}, {
		.name		= "s3c6410-spi",
		.driver_data	= (kernel_ulong_t)&s3c6410_spi_port_config,
	}, {
		.name		= "s5p64x0-spi",
		.driver_data	= (kernel_ulong_t)&s5p64x0_spi_port_config,
	}, {
		.name		= "s5pc100-spi",
		.driver_data	= (kernel_ulong_t)&s5pc100_spi_port_config,
	}, {
		.name		= "s5pv210-spi",
		.driver_data	= (kernel_ulong_t)&s5pv210_spi_port_config,
	}, {
		.name		= "exynos4210-spi",
		.driver_data	= (kernel_ulong_t)&exynos4_spi_port_config,
	},
	{ },
};

1621
static const struct of_device_id s3c64xx_spi_dt_match[] = {
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
	{ .compatible = "samsung,s3c2443-spi",
			.data = (void *)&s3c2443_spi_port_config,
	},
	{ .compatible = "samsung,s3c6410-spi",
			.data = (void *)&s3c6410_spi_port_config,
	},
	{ .compatible = "samsung,s5pc100-spi",
			.data = (void *)&s5pc100_spi_port_config,
	},
	{ .compatible = "samsung,s5pv210-spi",
			.data = (void *)&s5pv210_spi_port_config,
	},
1634 1635 1636
	{ .compatible = "samsung,exynos4210-spi",
			.data = (void *)&exynos4_spi_port_config,
	},
1637 1638 1639
	{ .compatible = "samsung,exynos5440-spi",
			.data = (void *)&exynos5440_spi_port_config,
	},
1640 1641 1642 1643
	{ },
};
MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);

1644 1645 1646 1647
static struct platform_driver s3c64xx_spi_driver = {
	.driver = {
		.name	= "s3c64xx-spi",
		.owner = THIS_MODULE,
M
Mark Brown 已提交
1648
		.pm = &s3c64xx_spi_pm,
1649
		.of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
1650
	},
1651
	.probe = s3c64xx_spi_probe,
1652
	.remove = s3c64xx_spi_remove,
1653
	.id_table = s3c64xx_spi_driver_ids,
1654 1655 1656
};
MODULE_ALIAS("platform:s3c64xx-spi");

1657
module_platform_driver(s3c64xx_spi_driver);
1658 1659 1660 1661

MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
MODULE_LICENSE("GPL");