atmel-mci.c 66.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Atmel MultiMedia Card Interface driver
 *
 * Copyright (C) 2004-2008 Atmel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/blkdev.h>
#include <linux/clk.h>
H
Haavard Skinnemoen 已提交
12
#include <linux/debugfs.h>
13
#include <linux/device.h>
14 15
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
B
Ben Nizette 已提交
16
#include <linux/err.h>
D
David Brownell 已提交
17
#include <linux/gpio.h>
18 19
#include <linux/init.h>
#include <linux/interrupt.h>
20
#include <linux/io.h>
21 22
#include <linux/ioport.h>
#include <linux/module.h>
23 24 25
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
26 27
#include <linux/platform_device.h>
#include <linux/scatterlist.h>
H
Haavard Skinnemoen 已提交
28
#include <linux/seq_file.h>
29
#include <linux/slab.h>
H
Haavard Skinnemoen 已提交
30
#include <linux/stat.h>
31
#include <linux/types.h>
32
#include <linux/platform_data/atmel.h>
33 34

#include <linux/mmc/host.h>
35
#include <linux/mmc/sdio.h>
36 37

#include <mach/atmel-mci.h>
38
#include <linux/atmel-mci.h>
39
#include <linux/atmel_pdc.h>
40

41
#include <asm/cacheflush.h>
42 43 44 45 46
#include <asm/io.h>
#include <asm/unaligned.h>

#include "atmel-mci-regs.h"

47
#define ATMCI_DATA_ERROR_FLAGS	(ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
48
#define ATMCI_DMA_THRESHOLD	16
49 50

enum {
51
	EVENT_CMD_RDY = 0,
52
	EVENT_XFER_COMPLETE,
53
	EVENT_NOTBUSY,
54 55 56 57
	EVENT_DATA_ERROR,
};

enum atmel_mci_state {
58 59
	STATE_IDLE = 0,
	STATE_SENDING_CMD,
60 61
	STATE_DATA_XFER,
	STATE_WAITING_NOTBUSY,
62
	STATE_SENDING_STOP,
63
	STATE_END_REQUEST,
64 65
};

66 67 68 69 70 71 72 73 74 75 76
enum atmci_xfer_dir {
	XFER_RECEIVE = 0,
	XFER_TRANSMIT,
};

enum atmci_pdc_buf {
	PDC_FIRST_BUF = 0,
	PDC_SECOND_BUF,
};

struct atmel_mci_caps {
77
	bool    has_dma_conf_reg;
78 79 80 81 82
	bool    has_pdc;
	bool    has_cfg_reg;
	bool    has_cstor_reg;
	bool    has_highspeed;
	bool    has_rwproof;
83
	bool	has_odd_clk_div;
84 85 86
	bool	has_bad_data_ordering;
	bool	need_reset_after_xfer;
	bool	need_blksz_mul_4;
87
	bool	need_notbusy_for_read_ops;
88 89
};

90 91 92 93 94
struct atmel_mci_dma {
	struct dma_chan			*chan;
	struct dma_async_tx_descriptor	*data_desc;
};

95 96 97 98
/**
 * struct atmel_mci - MMC controller state shared between all slots
 * @lock: Spinlock protecting the queue and associated data.
 * @regs: Pointer to MMIO registers.
99
 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
100
 * @pio_offset: Offset into the current scatterlist entry.
101 102 103 104 105
 * @buffer: Buffer used if we don't have the r/w proof capability. We
 *      don't have the time to switch pdc buffers so we have to use only
 *      one buffer for the full transaction.
 * @buf_size: size of the buffer.
 * @phys_buf_addr: buffer address needed for pdc.
106 107 108 109 110 111
 * @cur_slot: The slot which is currently using the controller.
 * @mrq: The request currently being processed on @cur_slot,
 *	or NULL if the controller is idle.
 * @cmd: The command currently being sent to the card, or NULL.
 * @data: The data currently being transferred, or NULL if no data
 *	transfer is in progress.
112
 * @data_size: just data->blocks * data->blksz.
113 114
 * @dma: DMA client state.
 * @data_chan: DMA channel being used for the current data transfer.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
 * @cmd_status: Snapshot of SR taken upon completion of the current
 *	command. Only valid when EVENT_CMD_COMPLETE is pending.
 * @data_status: Snapshot of SR taken upon completion of the current
 *	data transfer. Only valid when EVENT_DATA_COMPLETE or
 *	EVENT_DATA_ERROR is pending.
 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
 *	to be sent.
 * @tasklet: Tasklet running the request state machine.
 * @pending_events: Bitmask of events flagged by the interrupt handler
 *	to be processed by the tasklet.
 * @completed_events: Bitmask of events which the state machine has
 *	processed.
 * @state: Tasklet state.
 * @queue: List of slots waiting for access to the controller.
 * @need_clock_update: Update the clock rate before the next request.
 * @need_reset: Reset controller before next request.
131
 * @timer: Timer to balance the data timeout error flag which cannot rise.
132
 * @mode_reg: Value of the MR register.
133
 * @cfg_reg: Value of the CFG register.
134 135 136 137 138 139
 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
 *	rate and timeout calculations.
 * @mapbase: Physical address of the MMIO registers.
 * @mck: The peripheral bus clock hooked up to the MMC controller.
 * @pdev: Platform device associated with the MMC controller.
 * @slot: Slots sharing this MMC controller.
140 141 142 143 144 145 146
 * @caps: MCI capabilities depending on MCI version.
 * @prepare_data: function to setup MCI before data transfer which
 * depends on MCI capabilities.
 * @submit_data: function to start data transfer which depends on MCI
 * capabilities.
 * @stop_transfer: function to stop data transfer which depends on MCI
 * capabilities.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
 *
 * Locking
 * =======
 *
 * @lock is a softirq-safe spinlock protecting @queue as well as
 * @cur_slot, @mrq and @state. These must always be updated
 * at the same time while holding @lock.
 *
 * @lock also protects mode_reg and need_clock_update since these are
 * used to synchronize mode register updates with the queue
 * processing.
 *
 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
 * and must always be written at the same time as the slot is added to
 * @queue.
 *
 * @pending_events and @completed_events are accessed using atomic bit
 * operations, so they don't need any locking.
 *
 * None of the fields touched by the interrupt handler need any
 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
 * interrupts must be disabled and @data_status updated with a
 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
L
Lucas De Marchi 已提交
171
 * CMDRDY interrupt must be disabled and @cmd_status updated with a
172 173 174 175
 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
 * bytes_xfered field of @data must be written. This is ensured by
 * using barriers.
 */
176
struct atmel_mci {
177
	spinlock_t		lock;
178 179 180
	void __iomem		*regs;

	struct scatterlist	*sg;
181
	unsigned int		sg_len;
182
	unsigned int		pio_offset;
183 184 185
	unsigned int		*buffer;
	unsigned int		buf_size;
	dma_addr_t		buf_phys_addr;
186

187
	struct atmel_mci_slot	*cur_slot;
188 189 190
	struct mmc_request	*mrq;
	struct mmc_command	*cmd;
	struct mmc_data		*data;
191
	unsigned int		data_size;
192

193 194
	struct atmel_mci_dma	dma;
	struct dma_chan		*data_chan;
195
	struct dma_slave_config	dma_conf;
196

197 198 199 200 201 202 203
	u32			cmd_status;
	u32			data_status;
	u32			stop_cmdr;

	struct tasklet_struct	tasklet;
	unsigned long		pending_events;
	unsigned long		completed_events;
204
	enum atmel_mci_state	state;
205
	struct list_head	queue;
206

207 208
	bool			need_clock_update;
	bool			need_reset;
209
	struct timer_list	timer;
210
	u32			mode_reg;
211
	u32			cfg_reg;
212 213 214 215
	unsigned long		bus_hz;
	unsigned long		mapbase;
	struct clk		*mck;
	struct platform_device	*pdev;
216

217
	struct atmel_mci_slot	*slot[ATMCI_MAX_NR_SLOTS];
218 219 220 221 222 223

	struct atmel_mci_caps   caps;

	u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
	void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
	void (*stop_transfer)(struct atmel_mci *host);
224 225 226 227 228 229 230
};

/**
 * struct atmel_mci_slot - MMC slot state
 * @mmc: The mmc_host representing this slot.
 * @host: The MMC controller this slot is using.
 * @sdc_reg: Value of SDCR to be written before using this slot.
231
 * @sdio_irq: SDIO irq mask for this slot.
232 233 234 235 236 237 238 239 240 241
 * @mrq: mmc_request currently being processed or waiting to be
 *	processed, or NULL when the slot is idle.
 * @queue_node: List node for placing this node in the @queue list of
 *	&struct atmel_mci.
 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
 * @flags: Random state bits associated with the slot.
 * @detect_pin: GPIO pin used for card detection, or negative if not
 *	available.
 * @wp_pin: GPIO pin used for card write protect sending, or negative
 *	if not available.
242
 * @detect_is_active_high: The state of the detect pin when it is active.
243 244 245 246 247 248 249
 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
 */
struct atmel_mci_slot {
	struct mmc_host		*mmc;
	struct atmel_mci	*host;

	u32			sdc_reg;
250
	u32			sdio_irq;
251 252 253 254 255 256 257 258 259 260 261 262

	struct mmc_request	*mrq;
	struct list_head	queue_node;

	unsigned int		clock;
	unsigned long		flags;
#define ATMCI_CARD_PRESENT	0
#define ATMCI_CARD_NEED_INIT	1
#define ATMCI_SHUTDOWN		2

	int			detect_pin;
	int			wp_pin;
263
	bool			detect_is_active_high;
264 265

	struct timer_list	detect_timer;
266 267 268 269 270 271 272 273 274
};

#define atmci_test_and_clear_pending(host, event)		\
	test_and_clear_bit(event, &host->pending_events)
#define atmci_set_completed(host, event)			\
	set_bit(event, &host->completed_events)
#define atmci_set_pending(host, event)				\
	set_bit(event, &host->pending_events)

H
Haavard Skinnemoen 已提交
275 276 277 278 279 280
/*
 * The debugfs stuff below is mostly optimized away when
 * CONFIG_DEBUG_FS is not set.
 */
static int atmci_req_show(struct seq_file *s, void *v)
{
281 282
	struct atmel_mci_slot	*slot = s->private;
	struct mmc_request	*mrq;
H
Haavard Skinnemoen 已提交
283 284 285 286 287
	struct mmc_command	*cmd;
	struct mmc_command	*stop;
	struct mmc_data		*data;

	/* Make sure we get a consistent snapshot */
288 289
	spin_lock_bh(&slot->host->lock);
	mrq = slot->mrq;
H
Haavard Skinnemoen 已提交
290 291 292 293 294 295 296 297 298 299 300

	if (mrq) {
		cmd = mrq->cmd;
		data = mrq->data;
		stop = mrq->stop;

		if (cmd)
			seq_printf(s,
				"CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
				cmd->opcode, cmd->arg, cmd->flags,
				cmd->resp[0], cmd->resp[1], cmd->resp[2],
301
				cmd->resp[3], cmd->error);
H
Haavard Skinnemoen 已提交
302 303 304 305 306 307 308 309 310
		if (data)
			seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
				data->bytes_xfered, data->blocks,
				data->blksz, data->flags, data->error);
		if (stop)
			seq_printf(s,
				"CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
				stop->opcode, stop->arg, stop->flags,
				stop->resp[0], stop->resp[1], stop->resp[2],
311
				stop->resp[3], stop->error);
H
Haavard Skinnemoen 已提交
312 313
	}

314
	spin_unlock_bh(&slot->host->lock);
H
Haavard Skinnemoen 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

	return 0;
}

static int atmci_req_open(struct inode *inode, struct file *file)
{
	return single_open(file, atmci_req_show, inode->i_private);
}

static const struct file_operations atmci_req_fops = {
	.owner		= THIS_MODULE,
	.open		= atmci_req_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static void atmci_show_status_reg(struct seq_file *s,
		const char *regname, u32 value)
{
	static const char	*sr_bit[] = {
		[0]	= "CMDRDY",
		[1]	= "RXRDY",
		[2]	= "TXRDY",
		[3]	= "BLKE",
		[4]	= "DTIP",
		[5]	= "NOTBUSY",
342 343
		[6]	= "ENDRX",
		[7]	= "ENDTX",
H
Haavard Skinnemoen 已提交
344 345
		[8]	= "SDIOIRQA",
		[9]	= "SDIOIRQB",
346 347 348
		[12]	= "SDIOWAIT",
		[14]	= "RXBUFF",
		[15]	= "TXBUFE",
H
Haavard Skinnemoen 已提交
349 350 351 352 353 354 355
		[16]	= "RINDE",
		[17]	= "RDIRE",
		[18]	= "RCRCE",
		[19]	= "RENDE",
		[20]	= "RTOE",
		[21]	= "DCRCE",
		[22]	= "DTOE",
356 357 358 359 360
		[23]	= "CSTOE",
		[24]	= "BLKOVRE",
		[25]	= "DMADONE",
		[26]	= "FIFOEMPTY",
		[27]	= "XFRDONE",
H
Haavard Skinnemoen 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		[30]	= "OVRE",
		[31]	= "UNRE",
	};
	unsigned int		i;

	seq_printf(s, "%s:\t0x%08x", regname, value);
	for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
		if (value & (1 << i)) {
			if (sr_bit[i])
				seq_printf(s, " %s", sr_bit[i]);
			else
				seq_puts(s, " UNKNOWN");
		}
	}
	seq_putc(s, '\n');
}

static int atmci_regs_show(struct seq_file *s, void *v)
{
	struct atmel_mci	*host = s->private;
	u32			*buf;
382 383
	int			ret = 0;

H
Haavard Skinnemoen 已提交
384

385
	buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
H
Haavard Skinnemoen 已提交
386 387 388
	if (!buf)
		return -ENOMEM;

389 390 391 392 393
	/*
	 * Grab a more or less consistent snapshot. Note that we're
	 * not disabling interrupts, so IMR and SR may not be
	 * consistent.
	 */
394 395 396 397
	ret = clk_prepare_enable(host->mck);
	if (ret)
		goto out;

398
	spin_lock_bh(&host->lock);
399
	memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
400
	spin_unlock_bh(&host->lock);
H
Haavard Skinnemoen 已提交
401

402 403
	clk_disable_unprepare(host->mck);

404
	seq_printf(s, "MR:\t0x%08x%s%s ",
405 406
			buf[ATMCI_MR / 4],
			buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
407 408 409 410 411 412 413 414
			buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
	if (host->caps.has_odd_clk_div)
		seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
				((buf[ATMCI_MR / 4] & 0xff) << 1)
				| ((buf[ATMCI_MR / 4] >> 16) & 1));
	else
		seq_printf(s, "CLKDIV=%u\n",
				(buf[ATMCI_MR / 4] & 0xff));
415 416 417
	seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
	seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
	seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
H
Haavard Skinnemoen 已提交
418
	seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
419 420 421
			buf[ATMCI_BLKR / 4],
			buf[ATMCI_BLKR / 4] & 0xffff,
			(buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
422
	if (host->caps.has_cstor_reg)
423
		seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
H
Haavard Skinnemoen 已提交
424 425 426

	/* Don't read RSPR and RDR; it will consume the data there */

427 428
	atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
	atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
H
Haavard Skinnemoen 已提交
429

430
	if (host->caps.has_dma_conf_reg) {
431 432
		u32 val;

433
		val = buf[ATMCI_DMA / 4];
434 435 436 437
		seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
				val, val & 3,
				((val >> 4) & 3) ?
					1 << (((val >> 4) & 3) + 1) : 1,
438
				val & ATMCI_DMAEN ? " DMAEN" : "");
439 440 441
	}
	if (host->caps.has_cfg_reg) {
		u32 val;
442

443
		val = buf[ATMCI_CFG / 4];
444 445
		seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
				val,
446 447 448 449
				val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
				val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
				val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
				val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
450 451
	}

452
out:
453 454
	kfree(buf);

455
	return ret;
H
Haavard Skinnemoen 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

static int atmci_regs_open(struct inode *inode, struct file *file)
{
	return single_open(file, atmci_regs_show, inode->i_private);
}

static const struct file_operations atmci_regs_fops = {
	.owner		= THIS_MODULE,
	.open		= atmci_regs_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

471
static void atmci_init_debugfs(struct atmel_mci_slot *slot)
H
Haavard Skinnemoen 已提交
472
{
473 474 475 476
	struct mmc_host		*mmc = slot->mmc;
	struct atmel_mci	*host = slot->host;
	struct dentry		*root;
	struct dentry		*node;
H
Haavard Skinnemoen 已提交
477 478 479 480 481 482 483 484 485 486 487 488

	root = mmc->debugfs_root;
	if (!root)
		return;

	node = debugfs_create_file("regs", S_IRUSR, root, host,
			&atmci_regs_fops);
	if (IS_ERR(node))
		return;
	if (!node)
		goto err;

489
	node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
H
Haavard Skinnemoen 已提交
490 491 492
	if (!node)
		goto err;

493 494 495 496
	node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
	if (!node)
		goto err;

H
Haavard Skinnemoen 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509
	node = debugfs_create_x32("pending_events", S_IRUSR, root,
				     (u32 *)&host->pending_events);
	if (!node)
		goto err;

	node = debugfs_create_x32("completed_events", S_IRUSR, root,
				     (u32 *)&host->completed_events);
	if (!node)
		goto err;

	return;

err:
510
	dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
H
Haavard Skinnemoen 已提交
511
}
512

513 514 515 516 517 518 519 520
#if defined(CONFIG_OF)
static const struct of_device_id atmci_dt_ids[] = {
	{ .compatible = "atmel,hsmci" },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, atmci_dt_ids);

B
Bill Pemberton 已提交
521
static struct mci_platform_data*
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
atmci_of_init(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct device_node *cnp;
	struct mci_platform_data *pdata;
	u32 slot_id;

	if (!np) {
		dev_err(&pdev->dev, "device node not found\n");
		return ERR_PTR(-EINVAL);
	}

	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata) {
		dev_err(&pdev->dev, "could not allocate memory for pdata\n");
		return ERR_PTR(-ENOMEM);
	}

	for_each_child_of_node(np, cnp) {
		if (of_property_read_u32(cnp, "reg", &slot_id)) {
			dev_warn(&pdev->dev, "reg property is missing for %s\n",
				 cnp->full_name);
			continue;
		}

		if (slot_id >= ATMCI_MAX_NR_SLOTS) {
			dev_warn(&pdev->dev, "can't have more than %d slots\n",
			         ATMCI_MAX_NR_SLOTS);
			break;
		}

		if (of_property_read_u32(cnp, "bus-width",
		                         &pdata->slot[slot_id].bus_width))
			pdata->slot[slot_id].bus_width = 1;

		pdata->slot[slot_id].detect_pin =
			of_get_named_gpio(cnp, "cd-gpios", 0);

		pdata->slot[slot_id].detect_is_active_high =
			of_property_read_bool(cnp, "cd-inverted");

		pdata->slot[slot_id].wp_pin =
			of_get_named_gpio(cnp, "wp-gpios", 0);
	}

	return pdata;
}
#else /* CONFIG_OF */
static inline struct mci_platform_data*
atmci_of_init(struct platform_device *dev)
{
	return ERR_PTR(-EINVAL);
}
#endif

577 578 579 580 581
static inline unsigned int atmci_get_version(struct atmel_mci *host)
{
	return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
}

582 583 584 585 586 587 588 589 590 591 592
static void atmci_timeout_timer(unsigned long data)
{
	struct atmel_mci *host;

	host = (struct atmel_mci *)data;

	dev_dbg(&host->pdev->dev, "software timeout\n");

	if (host->mrq->cmd->data) {
		host->mrq->cmd->data->error = -ETIMEDOUT;
		host->data = NULL;
593 594 595 596 597 598 599
		/*
		 * With some SDIO modules, sometimes DMA transfer hangs. If
		 * stop_transfer() is not called then the DMA request is not
		 * removed, following ones are queued and never computed.
		 */
		if (host->state == STATE_DATA_XFER)
			host->stop_transfer(host);
600 601 602 603 604 605 606 607 608 609
	} else {
		host->mrq->cmd->error = -ETIMEDOUT;
		host->cmd = NULL;
	}
	host->need_reset = 1;
	host->state = STATE_END_REQUEST;
	smp_wmb();
	tasklet_schedule(&host->tasklet);
}

610
static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
611 612
					unsigned int ns)
{
613 614 615 616 617 618 619 620
	/*
	 * It is easier here to use us instead of ns for the timeout,
	 * it prevents from overflows during calculation.
	 */
	unsigned int us = DIV_ROUND_UP(ns, 1000);

	/* Maximum clock frequency is host->bus_hz/2 */
	return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
621 622 623
}

static void atmci_set_timeout(struct atmel_mci *host,
624
		struct atmel_mci_slot *slot, struct mmc_data *data)
625 626 627 628 629 630 631 632
{
	static unsigned	dtomul_to_shift[] = {
		0, 4, 7, 8, 10, 12, 16, 20
	};
	unsigned	timeout;
	unsigned	dtocyc;
	unsigned	dtomul;

633 634
	timeout = atmci_ns_to_clocks(host, data->timeout_ns)
		+ data->timeout_clks;
635 636 637 638 639 640 641 642 643 644 645 646 647

	for (dtomul = 0; dtomul < 8; dtomul++) {
		unsigned shift = dtomul_to_shift[dtomul];
		dtocyc = (timeout + (1 << shift) - 1) >> shift;
		if (dtocyc < 15)
			break;
	}

	if (dtomul >= 8) {
		dtomul = 7;
		dtocyc = 15;
	}

648
	dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
649
			dtocyc << dtomul_to_shift[dtomul]);
650
	atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
651 652 653 654 655 656 657 658 659 660 661 662 663
}

/*
 * Return mask with command flags to be enabled for this command.
 */
static u32 atmci_prepare_command(struct mmc_host *mmc,
				 struct mmc_command *cmd)
{
	struct mmc_data	*data;
	u32		cmdr;

	cmd->error = -EINPROGRESS;

664
	cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
665 666 667

	if (cmd->flags & MMC_RSP_PRESENT) {
		if (cmd->flags & MMC_RSP_136)
668
			cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
669
		else
670
			cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
671 672 673 674 675 676 677
	}

	/*
	 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
	 * it's too difficult to determine whether this is an ACMD or
	 * not. Better make it 64.
	 */
678
	cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
679 680

	if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
681
		cmdr |= ATMCI_CMDR_OPDCMD;
682 683 684

	data = cmd->data;
	if (data) {
685
		cmdr |= ATMCI_CMDR_START_XFER;
686 687

		if (cmd->opcode == SD_IO_RW_EXTENDED) {
688
			cmdr |= ATMCI_CMDR_SDIO_BLOCK;
689 690
		} else {
			if (data->flags & MMC_DATA_STREAM)
691
				cmdr |= ATMCI_CMDR_STREAM;
692
			else if (data->blocks > 1)
693
				cmdr |= ATMCI_CMDR_MULTI_BLOCK;
694
			else
695
				cmdr |= ATMCI_CMDR_BLOCK;
696
		}
697 698

		if (data->flags & MMC_DATA_READ)
699
			cmdr |= ATMCI_CMDR_TRDIR_READ;
700 701 702 703 704
	}

	return cmdr;
}

705
static void atmci_send_command(struct atmel_mci *host,
706
		struct mmc_command *cmd, u32 cmd_flags)
707 708 709 710
{
	WARN_ON(host->cmd);
	host->cmd = cmd;

711
	dev_vdbg(&host->pdev->dev,
712 713 714
			"start command: ARGR=0x%08x CMDR=0x%08x\n",
			cmd->arg, cmd_flags);

715 716
	atmci_writel(host, ATMCI_ARGR, cmd->arg);
	atmci_writel(host, ATMCI_CMDR, cmd_flags);
717 718
}

719
static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
720
{
721
	dev_dbg(&host->pdev->dev, "send stop command\n");
722
	atmci_send_command(host, data->stop, host->stop_cmdr);
723
	atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
724 725
}

726 727 728 729 730 731 732 733
/*
 * Configure given PDC buffer taking care of alignement issues.
 * Update host->data_size and host->sg.
 */
static void atmci_pdc_set_single_buf(struct atmel_mci *host,
	enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
{
	u32 pointer_reg, counter_reg;
734
	unsigned int buf_size;
735 736 737 738 739 740 741 742 743 744

	if (dir == XFER_RECEIVE) {
		pointer_reg = ATMEL_PDC_RPR;
		counter_reg = ATMEL_PDC_RCR;
	} else {
		pointer_reg = ATMEL_PDC_TPR;
		counter_reg = ATMEL_PDC_TCR;
	}

	if (buf_nb == PDC_SECOND_BUF) {
745 746
		pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
		counter_reg += ATMEL_PDC_SCND_BUF_OFF;
747 748
	}

749 750 751 752 753 754 755 756 757
	if (!host->caps.has_rwproof) {
		buf_size = host->buf_size;
		atmci_writel(host, pointer_reg, host->buf_phys_addr);
	} else {
		buf_size = sg_dma_len(host->sg);
		atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
	}

	if (host->data_size <= buf_size) {
758 759 760 761 762 763 764 765 766 767 768
		if (host->data_size & 0x3) {
			/* If size is different from modulo 4, transfer bytes */
			atmci_writel(host, counter_reg, host->data_size);
			atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
		} else {
			/* Else transfer 32-bits words */
			atmci_writel(host, counter_reg, host->data_size / 4);
		}
		host->data_size = 0;
	} else {
		/* We assume the size of a page is 32-bits aligned */
769 770
		atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
		host->data_size -= sg_dma_len(host->sg);
771 772 773 774 775 776 777 778 779 780 781
		if (host->data_size)
			host->sg = sg_next(host->sg);
	}
}

/*
 * Configure PDC buffer according to the data size ie configuring one or two
 * buffers. Don't use this function if you want to configure only the second
 * buffer. In this case, use atmci_pdc_set_single_buf.
 */
static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
782
{
783 784 785 786 787 788 789 790 791 792 793
	atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
	if (host->data_size)
		atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
}

/*
 * Unmap sg lists, called when transfer is finished.
 */
static void atmci_pdc_cleanup(struct atmel_mci *host)
{
	struct mmc_data         *data = host->data;
794

795
	if (data)
796 797 798 799
		dma_unmap_sg(&host->pdev->dev,
				data->sg, data->sg_len,
				((data->flags & MMC_DATA_WRITE)
				 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
800 801
}

802 803 804 805 806 807
/*
 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
 * interrupt needed for both transfer directions.
 */
static void atmci_pdc_complete(struct atmel_mci *host)
808
{
809
	int transfer_size = host->data->blocks * host->data->blksz;
810
	int i;
811

812
	atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
813 814

	if ((!host->caps.has_rwproof)
815 816 817 818
	    && (host->data->flags & MMC_DATA_READ)) {
		if (host->caps.has_bad_data_ordering)
			for (i = 0; i < transfer_size; i++)
				host->buffer[i] = swab32(host->buffer[i]);
819 820
		sg_copy_from_buffer(host->data->sg, host->data->sg_len,
		                    host->buffer, transfer_size);
821
	}
822

823
	atmci_pdc_cleanup(host);
824

825 826 827
	dev_dbg(&host->pdev->dev, "(%s) set pending xfer complete\n", __func__);
	atmci_set_pending(host, EVENT_XFER_COMPLETE);
	tasklet_schedule(&host->tasklet);
828 829
}

830 831 832 833 834 835 836 837 838 839 840 841 842 843
static void atmci_dma_cleanup(struct atmel_mci *host)
{
	struct mmc_data                 *data = host->data;

	if (data)
		dma_unmap_sg(host->dma.chan->device->dev,
				data->sg, data->sg_len,
				((data->flags & MMC_DATA_WRITE)
				 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
}

/*
 * This function is called by the DMA driver from tasklet context.
 */
844 845 846 847 848 849 850
static void atmci_dma_complete(void *arg)
{
	struct atmel_mci	*host = arg;
	struct mmc_data		*data = host->data;

	dev_vdbg(&host->pdev->dev, "DMA complete\n");

851
	if (host->caps.has_dma_conf_reg)
852
		/* Disable DMA hardware handshaking on MCI */
853
		atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
854

855 856 857 858 859 860 861
	atmci_dma_cleanup(host);

	/*
	 * If the card was removed, data will be NULL. No point trying
	 * to send the stop command or waiting for NBUSY in this case.
	 */
	if (data) {
862 863
		dev_dbg(&host->pdev->dev,
		        "(%s) set pending xfer complete\n", __func__);
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
		atmci_set_pending(host, EVENT_XFER_COMPLETE);
		tasklet_schedule(&host->tasklet);

		/*
		 * Regardless of what the documentation says, we have
		 * to wait for NOTBUSY even after block read
		 * operations.
		 *
		 * When the DMA transfer is complete, the controller
		 * may still be reading the CRC from the card, i.e.
		 * the data transfer is still in progress and we
		 * haven't seen all the potential error bits yet.
		 *
		 * The interrupt handler will schedule a different
		 * tasklet to finish things up when the data transfer
		 * is completely done.
		 *
		 * We may not complete the mmc request here anyway
		 * because the mmc layer may call back and cause us to
		 * violate the "don't submit new operations from the
		 * completion callback" rule of the dma engine
		 * framework.
		 */
887
		atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
888 889 890
	}
}

891 892 893 894 895 896 897 898 899 900 901
/*
 * Returns a mask of interrupt flags to be enabled after the whole
 * request has been prepared.
 */
static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
{
	u32 iflags;

	data->error = -EINPROGRESS;

	host->sg = data->sg;
902
	host->sg_len = data->sg_len;
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	host->data = data;
	host->data_chan = NULL;

	iflags = ATMCI_DATA_ERROR_FLAGS;

	/*
	 * Errata: MMC data write operation with less than 12
	 * bytes is impossible.
	 *
	 * Errata: MCI Transmit Data Register (TDR) FIFO
	 * corruption when length is not multiple of 4.
	 */
	if (data->blocks * data->blksz < 12
			|| (data->blocks * data->blksz) & 3)
		host->need_reset = true;

	host->pio_offset = 0;
	if (data->flags & MMC_DATA_READ)
		iflags |= ATMCI_RXRDY;
	else
		iflags |= ATMCI_TXRDY;

	return iflags;
}

/*
 * Set interrupt flags and set block length into the MCI mode register even
 * if this value is also accessible in the MCI block register. It seems to be
 * necessary before the High Speed MCI version. It also map sg and configure
 * PDC registers.
 */
static u32
atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
{
	u32 iflags, tmp;
	unsigned int sg_len;
	enum dma_data_direction dir;
940
	int i;
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

	data->error = -EINPROGRESS;

	host->data = data;
	host->sg = data->sg;
	iflags = ATMCI_DATA_ERROR_FLAGS;

	/* Enable pdc mode */
	atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);

	if (data->flags & MMC_DATA_READ) {
		dir = DMA_FROM_DEVICE;
		iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
	} else {
		dir = DMA_TO_DEVICE;
956
		iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
957 958 959 960 961 962 963 964 965 966 967
	}

	/* Set BLKLEN */
	tmp = atmci_readl(host, ATMCI_MR);
	tmp &= 0x0000ffff;
	tmp |= ATMCI_BLKLEN(data->blksz);
	atmci_writel(host, ATMCI_MR, tmp);

	/* Configure PDC */
	host->data_size = data->blocks * data->blksz;
	sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
968 969

	if ((!host->caps.has_rwproof)
970
	    && (host->data->flags & MMC_DATA_WRITE)) {
971 972
		sg_copy_to_buffer(host->data->sg, host->data->sg_len,
		                  host->buffer, host->data_size);
973 974 975 976
		if (host->caps.has_bad_data_ordering)
			for (i = 0; i < host->data_size; i++)
				host->buffer[i] = swab32(host->buffer[i]);
	}
977

978 979 980 981 982 983 984 985
	if (host->data_size)
		atmci_pdc_set_both_buf(host,
			((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));

	return iflags;
}

static u32
986
atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
987 988 989 990 991 992
{
	struct dma_chan			*chan;
	struct dma_async_tx_descriptor	*desc;
	struct scatterlist		*sg;
	unsigned int			i;
	enum dma_data_direction		direction;
993
	enum dma_transfer_direction	slave_dirn;
994
	unsigned int			sglen;
995
	u32				maxburst;
996 997 998 999 1000 1001 1002 1003 1004
	u32 iflags;

	data->error = -EINPROGRESS;

	WARN_ON(host->data);
	host->sg = NULL;
	host->data = data;

	iflags = ATMCI_DATA_ERROR_FLAGS;
1005 1006 1007 1008 1009 1010

	/*
	 * We don't do DMA on "complex" transfers, i.e. with
	 * non-word-aligned buffers or lengths. Also, we don't bother
	 * with all the DMA setup overhead for short transfers.
	 */
1011 1012
	if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
		return atmci_prepare_data(host, data);
1013
	if (data->blksz & 3)
1014
		return atmci_prepare_data(host, data);
1015 1016 1017

	for_each_sg(data->sg, sg, data->sg_len, i) {
		if (sg->offset & 3 || sg->length & 3)
1018
			return atmci_prepare_data(host, data);
1019 1020 1021 1022
	}

	/* If we don't have a channel, we can't do DMA */
	chan = host->dma.chan;
1023
	if (chan)
1024 1025 1026 1027 1028
		host->data_chan = chan;

	if (!chan)
		return -ENODEV;

1029
	if (data->flags & MMC_DATA_READ) {
1030
		direction = DMA_FROM_DEVICE;
1031
		host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
1032
		maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst);
1033
	} else {
1034
		direction = DMA_TO_DEVICE;
1035
		host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
1036
		maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst);
1037
	}
1038

1039 1040 1041
	if (host->caps.has_dma_conf_reg)
		atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) |
			ATMCI_DMAEN);
1042

1043
	sglen = dma_map_sg(chan->device->dev, data->sg,
1044
			data->sg_len, direction);
1045

1046
	dmaengine_slave_config(chan, &host->dma_conf);
1047
	desc = dmaengine_prep_slave_sg(chan,
1048
			data->sg, sglen, slave_dirn,
1049 1050
			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
	if (!desc)
1051
		goto unmap_exit;
1052 1053 1054 1055 1056

	host->dma.data_desc = desc;
	desc->callback = atmci_dma_complete;
	desc->callback_param = host;

1057
	return iflags;
1058
unmap_exit:
1059
	dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
1060
	return -ENOMEM;
1061 1062
}

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
static void
atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
{
	return;
}

/*
 * Start PDC according to transfer direction.
 */
static void
atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
{
	if (data->flags & MMC_DATA_READ)
		atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
	else
		atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
}

static void
atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
1083 1084 1085 1086 1087
{
	struct dma_chan			*chan = host->data_chan;
	struct dma_async_tx_descriptor	*desc = host->dma.data_desc;

	if (chan) {
1088 1089
		dmaengine_submit(desc);
		dma_async_issue_pending(chan);
1090 1091 1092
	}
}

1093
static void atmci_stop_transfer(struct atmel_mci *host)
1094
{
1095 1096
	dev_dbg(&host->pdev->dev,
	        "(%s) set pending xfer complete\n", __func__);
1097
	atmci_set_pending(host, EVENT_XFER_COMPLETE);
1098
	atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1099 1100
}

1101
/*
M
Masanari Iida 已提交
1102
 * Stop data transfer because error(s) occurred.
1103
 */
1104
static void atmci_stop_transfer_pdc(struct atmel_mci *host)
1105
{
1106
	atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
1107
}
1108

1109 1110 1111
static void atmci_stop_transfer_dma(struct atmel_mci *host)
{
	struct dma_chan *chan = host->data_chan;
1112

1113 1114 1115 1116 1117
	if (chan) {
		dmaengine_terminate_all(chan);
		atmci_dma_cleanup(host);
	} else {
		/* Data transfer was stopped by the interrupt handler */
1118 1119
		dev_dbg(&host->pdev->dev,
		        "(%s) set pending xfer complete\n", __func__);
1120 1121
		atmci_set_pending(host, EVENT_XFER_COMPLETE);
		atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1122
	}
1123 1124
}

1125 1126 1127 1128
/*
 * Start a request: prepare data if needed, prepare the command and activate
 * interrupts.
 */
1129 1130
static void atmci_start_request(struct atmel_mci *host,
		struct atmel_mci_slot *slot)
1131
{
1132
	struct mmc_request	*mrq;
1133
	struct mmc_command	*cmd;
1134
	struct mmc_data		*data;
1135
	u32			iflags;
1136
	u32			cmdflags;
1137

1138 1139
	mrq = slot->mrq;
	host->cur_slot = slot;
1140
	host->mrq = mrq;
1141

1142 1143
	host->pending_events = 0;
	host->completed_events = 0;
1144
	host->cmd_status = 0;
1145
	host->data_status = 0;
1146

1147 1148
	dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);

1149
	if (host->need_reset || host->caps.need_reset_after_xfer) {
1150 1151
		iflags = atmci_readl(host, ATMCI_IMR);
		iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
1152 1153 1154
		atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
		atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
		atmci_writel(host, ATMCI_MR, host->mode_reg);
1155
		if (host->caps.has_cfg_reg)
1156
			atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1157
		atmci_writel(host, ATMCI_IER, iflags);
1158 1159
		host->need_reset = false;
	}
1160
	atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
1161

1162
	iflags = atmci_readl(host, ATMCI_IMR);
1163
	if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
1164
		dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
1165 1166 1167 1168
				iflags);

	if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
		/* Send init sequence (74 clock cycles) */
1169 1170
		atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
		while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
1171 1172
			cpu_relax();
	}
1173
	iflags = 0;
1174 1175
	data = mrq->data;
	if (data) {
1176
		atmci_set_timeout(host, slot, data);
1177 1178

		/* Must set block count/size before sending command */
1179
		atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
1180
				| ATMCI_BLKLEN(data->blksz));
1181
		dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
1182
			ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
1183

1184
		iflags |= host->prepare_data(host, data);
1185 1186
	}

1187
	iflags |= ATMCI_CMDRDY;
1188
	cmd = mrq->cmd;
1189
	cmdflags = atmci_prepare_command(slot->mmc, cmd);
1190 1191 1192 1193 1194 1195 1196 1197 1198

	/*
	 * DMA transfer should be started before sending the command to avoid
	 * unexpected errors especially for read operations in SDIO mode.
	 * Unfortunately, in PDC mode, command has to be sent before starting
	 * the transfer.
	 */
	if (host->submit_data != &atmci_submit_data_dma)
		atmci_send_command(host, cmd, cmdflags);
1199 1200

	if (data)
1201
		host->submit_data(host, data);
1202

1203 1204 1205
	if (host->submit_data == &atmci_submit_data_dma)
		atmci_send_command(host, cmd, cmdflags);

1206
	if (mrq->stop) {
1207
		host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
1208
		host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
1209
		if (!(data->flags & MMC_DATA_WRITE))
1210
			host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
1211
		if (data->flags & MMC_DATA_STREAM)
1212
			host->stop_cmdr |= ATMCI_CMDR_STREAM;
1213
		else
1214
			host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
1215 1216 1217 1218 1219 1220 1221 1222
	}

	/*
	 * We could have enabled interrupts earlier, but I suspect
	 * that would open up a nice can of interesting race
	 * conditions (e.g. command and data complete, but stop not
	 * prepared yet.)
	 */
1223
	atmci_writel(host, ATMCI_IER, iflags);
1224 1225

	mod_timer(&host->timer, jiffies +  msecs_to_jiffies(2000));
1226
}
1227

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
static void atmci_queue_request(struct atmel_mci *host,
		struct atmel_mci_slot *slot, struct mmc_request *mrq)
{
	dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
			host->state);

	spin_lock_bh(&host->lock);
	slot->mrq = mrq;
	if (host->state == STATE_IDLE) {
		host->state = STATE_SENDING_CMD;
		atmci_start_request(host, slot);
	} else {
1240
		dev_dbg(&host->pdev->dev, "queue request\n");
1241 1242 1243 1244
		list_add_tail(&slot->queue_node, &host->queue);
	}
	spin_unlock_bh(&host->lock);
}
1245

1246 1247 1248 1249 1250 1251 1252
static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
	struct atmel_mci_slot	*slot = mmc_priv(mmc);
	struct atmel_mci	*host = slot->host;
	struct mmc_data		*data;

	WARN_ON(slot->mrq);
1253
	dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

	/*
	 * We may "know" the card is gone even though there's still an
	 * electrical connection. If so, we really need to communicate
	 * this to the MMC core since there won't be any more
	 * interrupts as the card is completely removed. Otherwise,
	 * the MMC core might believe the card is still there even
	 * though the card was just removed very slowly.
	 */
	if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
		mrq->cmd->error = -ENOMEDIUM;
		mmc_request_done(mmc, mrq);
		return;
	}

	/* We don't support multiple blocks of weird lengths. */
	data = mrq->data;
	if (data && data->blocks > 1 && data->blksz & 3) {
		mrq->cmd->error = -EINVAL;
		mmc_request_done(mmc, mrq);
	}

	atmci_queue_request(host, slot, mrq);
1277 1278 1279 1280
}

static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
1281 1282 1283
	struct atmel_mci_slot	*slot = mmc_priv(mmc);
	struct atmel_mci	*host = slot->host;
	unsigned int		i;
1284
	bool			unprepare_clk;
1285

1286
	slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
1287 1288
	switch (ios->bus_width) {
	case MMC_BUS_WIDTH_1:
1289
		slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
1290 1291
		break;
	case MMC_BUS_WIDTH_4:
1292
		slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
1293 1294 1295
		break;
	}

1296
	if (ios->clock) {
1297
		unsigned int clock_min = ~0U;
1298 1299
		u32 clkdiv;

1300 1301 1302
		clk_prepare(host->mck);
		unprepare_clk = true;

1303 1304
		spin_lock_bh(&host->lock);
		if (!host->mode_reg) {
1305
			clk_enable(host->mck);
1306
			unprepare_clk = false;
1307 1308
			atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
			atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1309
			if (host->caps.has_cfg_reg)
1310
				atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1311
		}
1312

1313 1314 1315 1316 1317
		/*
		 * Use mirror of ios->clock to prevent race with mmc
		 * core ios update when finding the minimum.
		 */
		slot->clock = ios->clock;
1318
		for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1319 1320 1321 1322 1323 1324
			if (host->slot[i] && host->slot[i]->clock
					&& host->slot[i]->clock < clock_min)
				clock_min = host->slot[i]->clock;
		}

		/* Calculate clock divider */
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
		if (host->caps.has_odd_clk_div) {
			clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
			if (clkdiv > 511) {
				dev_warn(&mmc->class_dev,
				         "clock %u too slow; using %lu\n",
				         clock_min, host->bus_hz / (511 + 2));
				clkdiv = 511;
			}
			host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
			                 | ATMCI_MR_CLKODD(clkdiv & 1);
		} else {
			clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
			if (clkdiv > 255) {
				dev_warn(&mmc->class_dev,
				         "clock %u too slow; using %lu\n",
				         clock_min, host->bus_hz / (2 * 256));
				clkdiv = 255;
			}
			host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
1344 1345
		}

1346 1347 1348 1349 1350
		/*
		 * WRPROOF and RDPROOF prevent overruns/underruns by
		 * stopping the clock when the FIFO is full/empty.
		 * This state is not expected to last for long.
		 */
1351
		if (host->caps.has_rwproof)
1352
			host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
1353

1354
		if (host->caps.has_cfg_reg) {
1355 1356
			/* setup High Speed mode in relation with card capacity */
			if (ios->timing == MMC_TIMING_SD_HS)
1357
				host->cfg_reg |= ATMCI_CFG_HSMODE;
1358
			else
1359
				host->cfg_reg &= ~ATMCI_CFG_HSMODE;
1360 1361 1362
		}

		if (list_empty(&host->queue)) {
1363
			atmci_writel(host, ATMCI_MR, host->mode_reg);
1364
			if (host->caps.has_cfg_reg)
1365
				atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1366
		} else {
1367
			host->need_clock_update = true;
1368
		}
1369 1370

		spin_unlock_bh(&host->lock);
1371
	} else {
1372 1373
		bool any_slot_active = false;

1374 1375
		unprepare_clk = false;

1376 1377
		spin_lock_bh(&host->lock);
		slot->clock = 0;
1378
		for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1379 1380 1381 1382
			if (host->slot[i] && host->slot[i]->clock) {
				any_slot_active = true;
				break;
			}
1383
		}
1384
		if (!any_slot_active) {
1385
			atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
1386
			if (host->mode_reg) {
1387
				atmci_readl(host, ATMCI_MR);
1388
				clk_disable(host->mck);
1389
				unprepare_clk = true;
1390 1391 1392 1393
			}
			host->mode_reg = 0;
		}
		spin_unlock_bh(&host->lock);
1394 1395
	}

1396 1397 1398
	if (unprepare_clk)
		clk_unprepare(host->mck);

1399
	switch (ios->power_mode) {
1400 1401 1402 1403
	case MMC_POWER_OFF:
		if (!IS_ERR(mmc->supply.vmmc))
			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
		break;
1404 1405
	case MMC_POWER_UP:
		set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1406 1407
		if (!IS_ERR(mmc->supply.vmmc))
			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1408
		break;
1409 1410 1411 1412 1413
	default:
		/*
		 * TODO: None of the currently available AVR32-based
		 * boards allow MMC power to be turned off. Implement
		 * power control when this can be tested properly.
1414 1415 1416 1417 1418 1419 1420
		 *
		 * We also need to hook this into the clock management
		 * somehow so that newly inserted cards aren't
		 * subjected to a fast clock before we have a chance
		 * to figure out what the maximum rate is. Currently,
		 * there's no way to avoid this, and there never will
		 * be for boards that don't support power control.
1421 1422 1423 1424 1425 1426 1427
		 */
		break;
	}
}

static int atmci_get_ro(struct mmc_host *mmc)
{
1428 1429
	int			read_only = -ENOSYS;
	struct atmel_mci_slot	*slot = mmc_priv(mmc);
1430

1431 1432
	if (gpio_is_valid(slot->wp_pin)) {
		read_only = gpio_get_value(slot->wp_pin);
1433 1434 1435 1436 1437 1438 1439
		dev_dbg(&mmc->class_dev, "card is %s\n",
				read_only ? "read-only" : "read-write");
	}

	return read_only;
}

1440 1441 1442 1443 1444 1445
static int atmci_get_cd(struct mmc_host *mmc)
{
	int			present = -ENOSYS;
	struct atmel_mci_slot	*slot = mmc_priv(mmc);

	if (gpio_is_valid(slot->detect_pin)) {
1446 1447
		present = !(gpio_get_value(slot->detect_pin) ^
			    slot->detect_is_active_high);
1448 1449 1450 1451 1452 1453 1454
		dev_dbg(&mmc->class_dev, "card is %spresent\n",
				present ? "" : "not ");
	}

	return present;
}

1455 1456 1457 1458 1459 1460
static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
{
	struct atmel_mci_slot	*slot = mmc_priv(mmc);
	struct atmel_mci	*host = slot->host;

	if (enable)
1461
		atmci_writel(host, ATMCI_IER, slot->sdio_irq);
1462
	else
1463
		atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
1464 1465
}

1466
static const struct mmc_host_ops atmci_ops = {
1467 1468 1469
	.request	= atmci_request,
	.set_ios	= atmci_set_ios,
	.get_ro		= atmci_get_ro,
1470
	.get_cd		= atmci_get_cd,
1471
	.enable_sdio_irq = atmci_enable_sdio_irq,
1472 1473
};

1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
/* Called with host->lock held */
static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
	__releases(&host->lock)
	__acquires(&host->lock)
{
	struct atmel_mci_slot	*slot = NULL;
	struct mmc_host		*prev_mmc = host->cur_slot->mmc;

	WARN_ON(host->cmd || host->data);

	/*
	 * Update the MMC clock rate if necessary. This may be
	 * necessary if set_ios() is called when a different slot is
L
Lucas De Marchi 已提交
1487
	 * busy transferring data.
1488
	 */
1489
	if (host->need_clock_update) {
1490
		atmci_writel(host, ATMCI_MR, host->mode_reg);
1491
		if (host->caps.has_cfg_reg)
1492
			atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1493
	}
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509

	host->cur_slot->mrq = NULL;
	host->mrq = NULL;
	if (!list_empty(&host->queue)) {
		slot = list_entry(host->queue.next,
				struct atmel_mci_slot, queue_node);
		list_del(&slot->queue_node);
		dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
				mmc_hostname(slot->mmc));
		host->state = STATE_SENDING_CMD;
		atmci_start_request(host, slot);
	} else {
		dev_vdbg(&host->pdev->dev, "list empty\n");
		host->state = STATE_IDLE;
	}

1510 1511
	del_timer(&host->timer);

1512 1513 1514 1515 1516
	spin_unlock(&host->lock);
	mmc_request_done(prev_mmc, mrq);
	spin_lock(&host->lock);
}

1517
static void atmci_command_complete(struct atmel_mci *host,
1518
			struct mmc_command *cmd)
1519
{
1520 1521
	u32		status = host->cmd_status;

1522
	/* Read the response from the card (up to 16 bytes) */
1523 1524 1525 1526
	cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
	cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
	cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
	cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
1527

1528
	if (status & ATMCI_RTOE)
1529
		cmd->error = -ETIMEDOUT;
1530
	else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
1531
		cmd->error = -EILSEQ;
1532
	else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
1533
		cmd->error = -EIO;
1534 1535 1536 1537 1538 1539
	else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
		if (host->caps.need_blksz_mul_4) {
			cmd->error = -EINVAL;
			host->need_reset = 1;
		}
	} else
1540 1541 1542 1543 1544
		cmd->error = 0;
}

static void atmci_detect_change(unsigned long data)
{
1545 1546 1547
	struct atmel_mci_slot	*slot = (struct atmel_mci_slot *)data;
	bool			present;
	bool			present_old;
1548 1549

	/*
1550 1551 1552 1553
	 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
	 * freeing the interrupt. We must not re-enable the interrupt
	 * if it has been freed, and if we're shutting down, it
	 * doesn't really matter whether the card is present or not.
1554 1555
	 */
	smp_rmb();
1556
	if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
1557 1558
		return;

1559
	enable_irq(gpio_to_irq(slot->detect_pin));
1560 1561
	present = !(gpio_get_value(slot->detect_pin) ^
		    slot->detect_is_active_high);
1562
	present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
1563

1564 1565
	dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
			present, present_old);
1566

1567 1568 1569 1570 1571
	if (present != present_old) {
		struct atmel_mci	*host = slot->host;
		struct mmc_request	*mrq;

		dev_dbg(&slot->mmc->class_dev, "card %s\n",
1572 1573
			present ? "inserted" : "removed");

1574 1575 1576 1577 1578 1579
		spin_lock(&host->lock);

		if (!present)
			clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
		else
			set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1580 1581

		/* Clean up queue if present */
1582
		mrq = slot->mrq;
1583
		if (mrq) {
1584 1585 1586 1587 1588
			if (mrq == host->mrq) {
				/*
				 * Reset controller to terminate any ongoing
				 * commands or data transfers.
				 */
1589 1590 1591
				atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
				atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
				atmci_writel(host, ATMCI_MR, host->mode_reg);
1592
				if (host->caps.has_cfg_reg)
1593
					atmci_writel(host, ATMCI_CFG, host->cfg_reg);
1594 1595 1596 1597 1598 1599

				host->data = NULL;
				host->cmd = NULL;

				switch (host->state) {
				case STATE_IDLE:
1600
					break;
1601 1602
				case STATE_SENDING_CMD:
					mrq->cmd->error = -ENOMEDIUM;
1603 1604 1605 1606
					if (mrq->data)
						host->stop_transfer(host);
					break;
				case STATE_DATA_XFER:
1607
					mrq->data->error = -ENOMEDIUM;
1608
					host->stop_transfer(host);
1609
					break;
1610 1611 1612
				case STATE_WAITING_NOTBUSY:
					mrq->data->error = -ENOMEDIUM;
					break;
1613 1614 1615
				case STATE_SENDING_STOP:
					mrq->stop->error = -ENOMEDIUM;
					break;
1616 1617
				case STATE_END_REQUEST:
					break;
1618
				}
1619

1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
				atmci_request_end(host, mrq);
			} else {
				list_del(&slot->queue_node);
				mrq->cmd->error = -ENOMEDIUM;
				if (mrq->data)
					mrq->data->error = -ENOMEDIUM;
				if (mrq->stop)
					mrq->stop->error = -ENOMEDIUM;

				spin_unlock(&host->lock);
				mmc_request_done(slot->mmc, mrq);
				spin_lock(&host->lock);
			}
1633
		}
1634
		spin_unlock(&host->lock);
1635

1636
		mmc_detect_change(slot->mmc, 0);
1637 1638 1639 1640 1641
	}
}

static void atmci_tasklet_func(unsigned long priv)
{
1642
	struct atmel_mci	*host = (struct atmel_mci *)priv;
1643 1644
	struct mmc_request	*mrq = host->mrq;
	struct mmc_data		*data = host->data;
1645 1646 1647 1648
	enum atmel_mci_state	state = host->state;
	enum atmel_mci_state	prev_state;
	u32			status;

1649 1650
	spin_lock(&host->lock);

1651
	state = host->state;
1652

1653
	dev_vdbg(&host->pdev->dev,
1654 1655
		"tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
		state, host->pending_events, host->completed_events,
1656
		atmci_readl(host, ATMCI_IMR));
1657

1658 1659
	do {
		prev_state = state;
1660
		dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state);
1661

1662
		switch (state) {
1663 1664 1665
		case STATE_IDLE:
			break;

1666
		case STATE_SENDING_CMD:
1667 1668 1669 1670 1671 1672
			/*
			 * Command has been sent, we are waiting for command
			 * ready. Then we have three next states possible:
			 * END_REQUEST by default, WAITING_NOTBUSY if it's a
			 * command needing it or DATA_XFER if there is data.
			 */
1673
			dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
1674
			if (!atmci_test_and_clear_pending(host,
1675
						EVENT_CMD_RDY))
1676
				break;
1677

1678
			dev_dbg(&host->pdev->dev, "set completed cmd ready\n");
1679
			host->cmd = NULL;
1680
			atmci_set_completed(host, EVENT_CMD_RDY);
1681
			atmci_command_complete(host, mrq->cmd);
1682
			if (mrq->data) {
1683 1684
				dev_dbg(&host->pdev->dev,
				        "command with data transfer");
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
				/*
				 * If there is a command error don't start
				 * data transfer.
				 */
				if (mrq->cmd->error) {
					host->stop_transfer(host);
					host->data = NULL;
					atmci_writel(host, ATMCI_IDR,
					             ATMCI_TXRDY | ATMCI_RXRDY
					             | ATMCI_DATA_ERROR_FLAGS);
					state = STATE_END_REQUEST;
				} else
					state = STATE_DATA_XFER;
			} else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
1699 1700
				dev_dbg(&host->pdev->dev,
				        "command response need waiting notbusy");
1701 1702 1703 1704
				atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
				state = STATE_WAITING_NOTBUSY;
			} else
				state = STATE_END_REQUEST;
1705

1706
			break;
1707

1708
		case STATE_DATA_XFER:
1709 1710
			if (atmci_test_and_clear_pending(host,
						EVENT_DATA_ERROR)) {
1711
				dev_dbg(&host->pdev->dev, "set completed data error\n");
1712 1713
				atmci_set_completed(host, EVENT_DATA_ERROR);
				state = STATE_END_REQUEST;
1714 1715
				break;
			}
1716

1717 1718 1719 1720 1721 1722 1723
			/*
			 * A data transfer is in progress. The event expected
			 * to move to the next state depends of data transfer
			 * type (PDC or DMA). Once transfer done we can move
			 * to the next step which is WAITING_NOTBUSY in write
			 * case and directly SENDING_STOP in read case.
			 */
1724
			dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n");
1725 1726 1727
			if (!atmci_test_and_clear_pending(host,
						EVENT_XFER_COMPLETE))
				break;
1728

1729 1730 1731
			dev_dbg(&host->pdev->dev,
			        "(%s) set completed xfer complete\n",
				__func__);
1732
			atmci_set_completed(host, EVENT_XFER_COMPLETE);
1733

1734 1735
			if (host->caps.need_notbusy_for_read_ops ||
			   (host->data->flags & MMC_DATA_WRITE)) {
1736 1737 1738 1739 1740 1741
				atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
				state = STATE_WAITING_NOTBUSY;
			} else if (host->mrq->stop) {
				atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
				atmci_send_stop_cmd(host, data);
				state = STATE_SENDING_STOP;
1742
			} else {
1743
				host->data = NULL;
1744 1745
				data->bytes_xfered = data->blocks * data->blksz;
				data->error = 0;
1746
				state = STATE_END_REQUEST;
1747
			}
1748
			break;
1749

1750 1751 1752 1753 1754 1755 1756
		case STATE_WAITING_NOTBUSY:
			/*
			 * We can be in the state for two reasons: a command
			 * requiring waiting not busy signal (stop command
			 * included) or a write operation. In the latest case,
			 * we need to send a stop command.
			 */
1757
			dev_dbg(&host->pdev->dev, "FSM: not busy?\n");
1758 1759 1760
			if (!atmci_test_and_clear_pending(host,
						EVENT_NOTBUSY))
				break;
1761

1762
			dev_dbg(&host->pdev->dev, "set completed not busy\n");
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
			atmci_set_completed(host, EVENT_NOTBUSY);

			if (host->data) {
				/*
				 * For some commands such as CMD53, even if
				 * there is data transfer, there is no stop
				 * command to send.
				 */
				if (host->mrq->stop) {
					atmci_writel(host, ATMCI_IER,
					             ATMCI_CMDRDY);
					atmci_send_stop_cmd(host, data);
					state = STATE_SENDING_STOP;
				} else {
					host->data = NULL;
					data->bytes_xfered = data->blocks
					                     * data->blksz;
					data->error = 0;
					state = STATE_END_REQUEST;
				}
			} else
				state = STATE_END_REQUEST;
			break;
1786 1787

		case STATE_SENDING_STOP:
1788 1789 1790 1791 1792 1793
			/*
			 * In this state, it is important to set host->data to
			 * NULL (which is tested in the waiting notbusy state)
			 * in order to go to the end request state instead of
			 * sending stop again.
			 */
1794
			dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
1795
			if (!atmci_test_and_clear_pending(host,
1796
						EVENT_CMD_RDY))
1797 1798
				break;

1799
			dev_dbg(&host->pdev->dev, "FSM: cmd ready\n");
1800
			host->cmd = NULL;
1801 1802
			data->bytes_xfered = data->blocks * data->blksz;
			data->error = 0;
1803
			atmci_command_complete(host, mrq->stop);
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
			if (mrq->stop->error) {
				host->stop_transfer(host);
				atmci_writel(host, ATMCI_IDR,
				             ATMCI_TXRDY | ATMCI_RXRDY
				             | ATMCI_DATA_ERROR_FLAGS);
				state = STATE_END_REQUEST;
			} else {
				atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
				state = STATE_WAITING_NOTBUSY;
			}
1814
			host->data = NULL;
1815
			break;
1816

1817 1818 1819 1820 1821 1822 1823
		case STATE_END_REQUEST:
			atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
			                   | ATMCI_DATA_ERROR_FLAGS);
			status = host->data_status;
			if (unlikely(status)) {
				host->stop_transfer(host);
				host->data = NULL;
1824 1825 1826 1827 1828 1829 1830 1831
				if (data) {
					if (status & ATMCI_DTOE) {
						data->error = -ETIMEDOUT;
					} else if (status & ATMCI_DCRCE) {
						data->error = -EILSEQ;
					} else {
						data->error = -EIO;
					}
1832 1833
				}
			}
1834

1835 1836
			atmci_request_end(host, host->mrq);
			state = STATE_IDLE;
1837 1838 1839 1840 1841
			break;
		}
	} while (state != prev_state);

	host->state = state;
1842 1843

	spin_unlock(&host->lock);
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
}

static void atmci_read_data_pio(struct atmel_mci *host)
{
	struct scatterlist	*sg = host->sg;
	void			*buf = sg_virt(sg);
	unsigned int		offset = host->pio_offset;
	struct mmc_data		*data = host->data;
	u32			value;
	u32			status;
	unsigned int		nbytes = 0;

	do {
1857
		value = atmci_readl(host, ATMCI_RDR);
1858 1859 1860 1861 1862 1863 1864
		if (likely(offset + 4 <= sg->length)) {
			put_unaligned(value, (u32 *)(buf + offset));

			offset += 4;
			nbytes += 4;

			if (offset == sg->length) {
1865
				flush_dcache_page(sg_page(sg));
1866
				host->sg = sg = sg_next(sg);
1867 1868
				host->sg_len--;
				if (!sg || !host->sg_len)
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
					goto done;

				offset = 0;
				buf = sg_virt(sg);
			}
		} else {
			unsigned int remaining = sg->length - offset;
			memcpy(buf + offset, &value, remaining);
			nbytes += remaining;

			flush_dcache_page(sg_page(sg));
			host->sg = sg = sg_next(sg);
1881 1882
			host->sg_len--;
			if (!sg || !host->sg_len)
1883 1884 1885 1886 1887 1888 1889 1890
				goto done;

			offset = 4 - remaining;
			buf = sg_virt(sg);
			memcpy(buf, (u8 *)&value + remaining, offset);
			nbytes += offset;
		}

1891
		status = atmci_readl(host, ATMCI_SR);
1892
		if (status & ATMCI_DATA_ERROR_FLAGS) {
1893
			atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
1894 1895
						| ATMCI_DATA_ERROR_FLAGS));
			host->data_status = status;
1896 1897
			data->bytes_xfered += nbytes;
			return;
1898
		}
1899
	} while (status & ATMCI_RXRDY);
1900 1901 1902 1903 1904 1905 1906

	host->pio_offset = offset;
	data->bytes_xfered += nbytes;

	return;

done:
1907 1908
	atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
	atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1909
	data->bytes_xfered += nbytes;
1910
	smp_wmb();
1911
	atmci_set_pending(host, EVENT_XFER_COMPLETE);
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
}

static void atmci_write_data_pio(struct atmel_mci *host)
{
	struct scatterlist	*sg = host->sg;
	void			*buf = sg_virt(sg);
	unsigned int		offset = host->pio_offset;
	struct mmc_data		*data = host->data;
	u32			value;
	u32			status;
	unsigned int		nbytes = 0;

	do {
		if (likely(offset + 4 <= sg->length)) {
			value = get_unaligned((u32 *)(buf + offset));
1927
			atmci_writel(host, ATMCI_TDR, value);
1928 1929 1930 1931 1932

			offset += 4;
			nbytes += 4;
			if (offset == sg->length) {
				host->sg = sg = sg_next(sg);
1933 1934
				host->sg_len--;
				if (!sg || !host->sg_len)
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
					goto done;

				offset = 0;
				buf = sg_virt(sg);
			}
		} else {
			unsigned int remaining = sg->length - offset;

			value = 0;
			memcpy(&value, buf + offset, remaining);
			nbytes += remaining;

			host->sg = sg = sg_next(sg);
1948 1949
			host->sg_len--;
			if (!sg || !host->sg_len) {
1950
				atmci_writel(host, ATMCI_TDR, value);
1951 1952 1953 1954 1955 1956
				goto done;
			}

			offset = 4 - remaining;
			buf = sg_virt(sg);
			memcpy((u8 *)&value + remaining, buf, offset);
1957
			atmci_writel(host, ATMCI_TDR, value);
1958 1959 1960
			nbytes += offset;
		}

1961
		status = atmci_readl(host, ATMCI_SR);
1962
		if (status & ATMCI_DATA_ERROR_FLAGS) {
1963
			atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
1964 1965
						| ATMCI_DATA_ERROR_FLAGS));
			host->data_status = status;
1966 1967
			data->bytes_xfered += nbytes;
			return;
1968
		}
1969
	} while (status & ATMCI_TXRDY);
1970 1971 1972 1973 1974 1975 1976

	host->pio_offset = offset;
	data->bytes_xfered += nbytes;

	return;

done:
1977 1978
	atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
	atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1979
	data->bytes_xfered += nbytes;
1980
	smp_wmb();
1981
	atmci_set_pending(host, EVENT_XFER_COMPLETE);
1982 1983
}

1984 1985 1986 1987
static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
{
	int	i;

1988
	for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
1989 1990 1991 1992 1993 1994 1995 1996
		struct atmel_mci_slot *slot = host->slot[i];
		if (slot && (status & slot->sdio_irq)) {
			mmc_signal_sdio_irq(slot->mmc);
		}
	}
}


1997 1998
static irqreturn_t atmci_interrupt(int irq, void *dev_id)
{
1999
	struct atmel_mci	*host = dev_id;
2000 2001 2002 2003
	u32			status, mask, pending;
	unsigned int		pass_count = 0;

	do {
2004 2005
		status = atmci_readl(host, ATMCI_SR);
		mask = atmci_readl(host, ATMCI_IMR);
2006 2007 2008 2009 2010
		pending = status & mask;
		if (!pending)
			break;

		if (pending & ATMCI_DATA_ERROR_FLAGS) {
2011
			dev_dbg(&host->pdev->dev, "IRQ: data error\n");
2012
			atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
2013 2014 2015
					| ATMCI_RXRDY | ATMCI_TXRDY
					| ATMCI_ENDRX | ATMCI_ENDTX
					| ATMCI_RXBUFF | ATMCI_TXBUFE);
2016

2017
			host->data_status = status;
2018
			dev_dbg(&host->pdev->dev, "set pending data error\n");
2019
			smp_wmb();
2020 2021 2022
			atmci_set_pending(host, EVENT_DATA_ERROR);
			tasklet_schedule(&host->tasklet);
		}
2023 2024

		if (pending & ATMCI_TXBUFE) {
2025
			dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n");
2026
			atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
2027
			atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
2028 2029 2030 2031 2032 2033 2034
			/*
			 * We can receive this interruption before having configured
			 * the second pdc buffer, so we need to reconfigure first and
			 * second buffers again
			 */
			if (host->data_size) {
				atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
2035
				atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
2036 2037 2038 2039
				atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
			} else {
				atmci_pdc_complete(host);
			}
2040
		} else if (pending & ATMCI_ENDTX) {
2041
			dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n");
2042
			atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
2043 2044 2045

			if (host->data_size) {
				atmci_pdc_set_single_buf(host,
2046 2047
						XFER_TRANSMIT, PDC_SECOND_BUF);
				atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
2048 2049 2050 2051
			}
		}

		if (pending & ATMCI_RXBUFF) {
2052
			dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n");
2053
			atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
2054
			atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2055 2056 2057 2058 2059 2060 2061
			/*
			 * We can receive this interruption before having configured
			 * the second pdc buffer, so we need to reconfigure first and
			 * second buffers again
			 */
			if (host->data_size) {
				atmci_pdc_set_both_buf(host, XFER_RECEIVE);
2062
				atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2063 2064 2065 2066
				atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
			} else {
				atmci_pdc_complete(host);
			}
2067
		} else if (pending & ATMCI_ENDRX) {
2068
			dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n");
2069 2070 2071 2072 2073 2074 2075
			atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);

			if (host->data_size) {
				atmci_pdc_set_single_buf(host,
						XFER_RECEIVE, PDC_SECOND_BUF);
				atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
			}
2076 2077
		}

2078 2079 2080 2081 2082 2083 2084
		/*
		 * First mci IPs, so mainly the ones having pdc, have some
		 * issues with the notbusy signal. You can't get it after
		 * data transmission if you have not sent a stop command.
		 * The appropriate workaround is to use the BLKE signal.
		 */
		if (pending & ATMCI_BLKE) {
2085
			dev_dbg(&host->pdev->dev, "IRQ: blke\n");
2086 2087
			atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
			smp_wmb();
2088
			dev_dbg(&host->pdev->dev, "set pending notbusy\n");
2089 2090 2091
			atmci_set_pending(host, EVENT_NOTBUSY);
			tasklet_schedule(&host->tasklet);
		}
2092

2093
		if (pending & ATMCI_NOTBUSY) {
2094
			dev_dbg(&host->pdev->dev, "IRQ: not_busy\n");
2095
			atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
2096
			smp_wmb();
2097
			dev_dbg(&host->pdev->dev, "set pending notbusy\n");
2098
			atmci_set_pending(host, EVENT_NOTBUSY);
2099 2100
			tasklet_schedule(&host->tasklet);
		}
2101

2102
		if (pending & ATMCI_RXRDY)
2103
			atmci_read_data_pio(host);
2104
		if (pending & ATMCI_TXRDY)
2105 2106
			atmci_write_data_pio(host);

2107
		if (pending & ATMCI_CMDRDY) {
2108
			dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n");
2109 2110 2111
			atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
			host->cmd_status = status;
			smp_wmb();
2112
			dev_dbg(&host->pdev->dev, "set pending cmd rdy\n");
2113 2114 2115
			atmci_set_pending(host, EVENT_CMD_RDY);
			tasklet_schedule(&host->tasklet);
		}
2116

2117
		if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
2118 2119
			atmci_sdio_interrupt(host, status);

2120 2121 2122 2123 2124 2125 2126
	} while (pass_count++ < 5);

	return pass_count ? IRQ_HANDLED : IRQ_NONE;
}

static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
{
2127
	struct atmel_mci_slot	*slot = dev_id;
2128 2129 2130 2131 2132 2133 2134

	/*
	 * Disable interrupts until the pin has stabilized and check
	 * the state then. Use mod_timer() since we may be in the
	 * middle of the timer routine when this interrupt triggers.
	 */
	disable_irq_nosync(irq);
2135
	mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
2136 2137 2138 2139

	return IRQ_HANDLED;
}

2140 2141
static int __init atmci_init_slot(struct atmel_mci *host,
		struct mci_slot_pdata *slot_data, unsigned int id,
2142
		u32 sdc_reg, u32 sdio_irq)
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
{
	struct mmc_host			*mmc;
	struct atmel_mci_slot		*slot;

	mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
	if (!mmc)
		return -ENOMEM;

	slot = mmc_priv(mmc);
	slot->mmc = mmc;
	slot->host = host;
	slot->detect_pin = slot_data->detect_pin;
	slot->wp_pin = slot_data->wp_pin;
2156
	slot->detect_is_active_high = slot_data->detect_is_active_high;
2157
	slot->sdc_reg = sdc_reg;
2158
	slot->sdio_irq = sdio_irq;
2159

2160 2161 2162 2163 2164 2165 2166
	dev_dbg(&mmc->class_dev,
	        "slot[%u]: bus_width=%u, detect_pin=%d, "
		"detect_is_active_high=%s, wp_pin=%d\n",
		id, slot_data->bus_width, slot_data->detect_pin,
		slot_data->detect_is_active_high ? "true" : "false",
		slot_data->wp_pin);

2167 2168 2169 2170
	mmc->ops = &atmci_ops;
	mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
	mmc->f_max = host->bus_hz / 2;
	mmc->ocr_avail	= MMC_VDD_32_33 | MMC_VDD_33_34;
2171 2172
	if (sdio_irq)
		mmc->caps |= MMC_CAP_SDIO_IRQ;
2173
	if (host->caps.has_highspeed)
2174
		mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2175 2176 2177 2178 2179 2180
	/*
	 * Without the read/write proof capability, it is strongly suggested to
	 * use only one bit for data to prevent fifo underruns and overruns
	 * which will corrupt data.
	 */
	if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
2181 2182
		mmc->caps |= MMC_CAP_4_BIT_DATA;

2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
	if (atmci_get_version(host) < 0x200) {
		mmc->max_segs = 256;
		mmc->max_blk_size = 4095;
		mmc->max_blk_count = 256;
		mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
		mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
	} else {
		mmc->max_segs = 64;
		mmc->max_req_size = 32768 * 512;
		mmc->max_blk_size = 32768;
		mmc->max_blk_count = 512;
	}
2195 2196 2197 2198

	/* Assume card is present initially */
	set_bit(ATMCI_CARD_PRESENT, &slot->flags);
	if (gpio_is_valid(slot->detect_pin)) {
2199 2200
		if (devm_gpio_request(&host->pdev->dev, slot->detect_pin,
				      "mmc_detect")) {
2201 2202
			dev_dbg(&mmc->class_dev, "no detect pin available\n");
			slot->detect_pin = -EBUSY;
2203 2204
		} else if (gpio_get_value(slot->detect_pin) ^
				slot->detect_is_active_high) {
2205 2206 2207 2208 2209 2210 2211 2212
			clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
		}
	}

	if (!gpio_is_valid(slot->detect_pin))
		mmc->caps |= MMC_CAP_NEEDS_POLL;

	if (gpio_is_valid(slot->wp_pin)) {
2213 2214
		if (devm_gpio_request(&host->pdev->dev, slot->wp_pin,
				      "mmc_wp")) {
2215 2216 2217 2218 2219 2220
			dev_dbg(&mmc->class_dev, "no WP pin available\n");
			slot->wp_pin = -EBUSY;
		}
	}

	host->slot[id] = slot;
2221
	mmc_regulator_get_supply(mmc);
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
	mmc_add_host(mmc);

	if (gpio_is_valid(slot->detect_pin)) {
		int ret;

		setup_timer(&slot->detect_timer, atmci_detect_change,
				(unsigned long)slot);

		ret = request_irq(gpio_to_irq(slot->detect_pin),
				atmci_detect_interrupt,
				IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
				"mmc-detect", slot);
		if (ret) {
			dev_dbg(&mmc->class_dev,
				"could not request IRQ %d for detect pin\n",
				gpio_to_irq(slot->detect_pin));
			slot->detect_pin = -EBUSY;
		}
	}

	atmci_init_debugfs(slot);

	return 0;
}

static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
		unsigned int id)
{
	/* Debugfs stuff is cleaned up by mmc core */

	set_bit(ATMCI_SHUTDOWN, &slot->flags);
	smp_wmb();

	mmc_remove_host(slot->mmc);

	if (gpio_is_valid(slot->detect_pin)) {
		int pin = slot->detect_pin;

		free_irq(gpio_to_irq(pin), slot);
		del_timer_sync(&slot->detect_timer);
	}

	slot->host->slot[id] = NULL;
	mmc_free_host(slot->mmc);
}

2268
static bool atmci_filter(struct dma_chan *chan, void *pdata)
2269
{
2270 2271
	struct mci_platform_data *sl_pdata = pdata;
	struct mci_dma_data *sl;
2272

2273 2274 2275 2276
	if (!sl_pdata)
		return false;

	sl = sl_pdata->dma_slave;
2277 2278
	if (sl && find_slave_dev(sl) == chan->device->dev) {
		chan->private = slave_data_ptr(sl);
2279
		return true;
2280
	} else {
2281
		return false;
2282
	}
2283
}
2284

2285
static bool atmci_configure_dma(struct atmel_mci *host)
2286 2287
{
	struct mci_platform_data	*pdata;
2288
	dma_cap_mask_t mask;
2289 2290

	if (host == NULL)
2291
		return false;
2292 2293 2294

	pdata = host->pdev->dev.platform_data;

2295 2296
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);
2297

2298 2299
	host->dma.chan = dma_request_slave_channel_compat(mask, atmci_filter, pdata,
							  &host->pdev->dev, "rxtx");
2300 2301 2302 2303
	if (!host->dma.chan) {
		dev_warn(&host->pdev->dev, "no DMA channel available\n");
		return false;
	} else {
2304
		dev_info(&host->pdev->dev,
L
Ludovic Desroches 已提交
2305
					"using %s for DMA transfers\n",
2306
					dma_chan_name(host->dma.chan));
2307 2308 2309 2310 2311 2312 2313 2314

		host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
		host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
		host->dma_conf.src_maxburst = 1;
		host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
		host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
		host->dma_conf.dst_maxburst = 1;
		host->dma_conf.device_fc = false;
2315 2316
		return true;
	}
2317
}
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331

/*
 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
 * HSMCI provides DMA support and a new config register but no more supports
 * PDC.
 */
static void __init atmci_get_cap(struct atmel_mci *host)
{
	unsigned int version;

	version = atmci_get_version(host);
	dev_info(&host->pdev->dev,
			"version: 0x%x\n", version);

2332
	host->caps.has_dma_conf_reg = 0;
2333
	host->caps.has_pdc = ATMCI_PDC_CONNECTED;
2334 2335 2336 2337
	host->caps.has_cfg_reg = 0;
	host->caps.has_cstor_reg = 0;
	host->caps.has_highspeed = 0;
	host->caps.has_rwproof = 0;
2338
	host->caps.has_odd_clk_div = 0;
2339 2340 2341
	host->caps.has_bad_data_ordering = 1;
	host->caps.need_reset_after_xfer = 1;
	host->caps.need_blksz_mul_4 = 1;
2342
	host->caps.need_notbusy_for_read_ops = 0;
2343 2344 2345

	/* keep only major version number */
	switch (version & 0xf00) {
2346
	case 0x600:
2347
	case 0x500:
2348 2349 2350
		host->caps.has_odd_clk_div = 1;
	case 0x400:
	case 0x300:
2351
		host->caps.has_dma_conf_reg = 1;
2352
		host->caps.has_pdc = 0;
2353 2354 2355
		host->caps.has_cfg_reg = 1;
		host->caps.has_cstor_reg = 1;
		host->caps.has_highspeed = 1;
2356
	case 0x200:
2357
		host->caps.has_rwproof = 1;
2358
		host->caps.need_blksz_mul_4 = 0;
2359
		host->caps.need_notbusy_for_read_ops = 1;
2360
	case 0x100:
2361 2362 2363
		host->caps.has_bad_data_ordering = 0;
		host->caps.need_reset_after_xfer = 0;
	case 0x0:
2364 2365
		break;
	default:
2366
		host->caps.has_pdc = 0;
2367 2368 2369 2370 2371
		dev_warn(&host->pdev->dev,
				"Unmanaged mci version, set minimum capabilities\n");
		break;
	}
}
2372

2373 2374 2375
static int __init atmci_probe(struct platform_device *pdev)
{
	struct mci_platform_data	*pdata;
2376 2377 2378 2379
	struct atmel_mci		*host;
	struct resource			*regs;
	unsigned int			nr_slots;
	int				irq;
2380
	int				ret, i;
2381 2382 2383 2384 2385

	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!regs)
		return -ENXIO;
	pdata = pdev->dev.platform_data;
2386 2387 2388 2389 2390 2391 2392 2393
	if (!pdata) {
		pdata = atmci_of_init(pdev);
		if (IS_ERR(pdata)) {
			dev_err(&pdev->dev, "platform data not available\n");
			return PTR_ERR(pdata);
		}
	}

2394 2395 2396 2397
	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return irq;

2398
	host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
2399
	if (!host)
2400 2401 2402
		return -ENOMEM;

	host->pdev = pdev;
2403 2404
	spin_lock_init(&host->lock);
	INIT_LIST_HEAD(&host->queue);
2405

2406 2407 2408
	host->mck = devm_clk_get(&pdev->dev, "mci_clk");
	if (IS_ERR(host->mck))
		return PTR_ERR(host->mck);
2409

2410
	host->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
2411
	if (!host->regs)
2412
		return -ENOMEM;
2413

2414 2415
	ret = clk_prepare_enable(host->mck);
	if (ret)
2416 2417
		return ret;

2418
	atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
2419
	host->bus_hz = clk_get_rate(host->mck);
2420
	clk_disable_unprepare(host->mck);
2421 2422 2423

	host->mapbase = regs->start;

2424
	tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
2425

2426
	ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
2427
	if (ret)
2428
		return ret;
2429

2430 2431
	/* Get MCI capabilities and set operations according to it */
	atmci_get_cap(host);
2432
	if (atmci_configure_dma(host)) {
2433 2434 2435 2436 2437 2438 2439 2440 2441
		host->prepare_data = &atmci_prepare_data_dma;
		host->submit_data = &atmci_submit_data_dma;
		host->stop_transfer = &atmci_stop_transfer_dma;
	} else if (host->caps.has_pdc) {
		dev_info(&pdev->dev, "using PDC\n");
		host->prepare_data = &atmci_prepare_data_pdc;
		host->submit_data = &atmci_submit_data_pdc;
		host->stop_transfer = &atmci_stop_transfer_pdc;
	} else {
2442
		dev_info(&pdev->dev, "using PIO\n");
2443 2444 2445 2446 2447
		host->prepare_data = &atmci_prepare_data;
		host->submit_data = &atmci_submit_data;
		host->stop_transfer = &atmci_stop_transfer;
	}

2448 2449
	platform_set_drvdata(pdev, host);

2450 2451
	setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);

2452 2453 2454 2455 2456
	/* We need at least one slot to succeed */
	nr_slots = 0;
	ret = -ENODEV;
	if (pdata->slot[0].bus_width) {
		ret = atmci_init_slot(host, &pdata->slot[0],
2457
				0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
2458
		if (!ret) {
2459
			nr_slots++;
2460 2461
			host->buf_size = host->slot[0]->mmc->max_req_size;
		}
2462 2463 2464
	}
	if (pdata->slot[1].bus_width) {
		ret = atmci_init_slot(host, &pdata->slot[1],
2465
				1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
2466
		if (!ret) {
2467
			nr_slots++;
2468 2469 2470 2471
			if (host->slot[1]->mmc->max_req_size > host->buf_size)
				host->buf_size =
					host->slot[1]->mmc->max_req_size;
		}
2472 2473
	}

2474 2475
	if (!nr_slots) {
		dev_err(&pdev->dev, "init failed: no slot defined\n");
2476
		goto err_init_slot;
2477
	}
2478

2479 2480 2481 2482 2483 2484 2485
	if (!host->caps.has_rwproof) {
		host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
		                                  &host->buf_phys_addr,
						  GFP_KERNEL);
		if (!host->buffer) {
			ret = -ENOMEM;
			dev_err(&pdev->dev, "buffer allocation failed\n");
2486
			goto err_dma_alloc;
2487 2488 2489
		}
	}

2490 2491 2492
	dev_info(&pdev->dev,
			"Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
			host->mapbase, irq, nr_slots);
H
Haavard Skinnemoen 已提交
2493

2494 2495
	return 0;

2496 2497 2498 2499 2500
err_dma_alloc:
	for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
		if (host->slot[i])
			atmci_cleanup_slot(host->slot[i], i);
	}
2501
err_init_slot:
2502
	del_timer_sync(&host->timer);
2503 2504
	if (host->dma.chan)
		dma_release_channel(host->dma.chan);
2505
	free_irq(irq, host);
2506 2507 2508 2509 2510
	return ret;
}

static int __exit atmci_remove(struct platform_device *pdev)
{
2511 2512
	struct atmel_mci	*host = platform_get_drvdata(pdev);
	unsigned int		i;
2513

2514 2515 2516 2517
	if (host->buffer)
		dma_free_coherent(&pdev->dev, host->buf_size,
		                  host->buffer, host->buf_phys_addr);

2518
	for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2519 2520 2521
		if (host->slot[i])
			atmci_cleanup_slot(host->slot[i], i);
	}
2522

2523
	clk_prepare_enable(host->mck);
2524 2525 2526
	atmci_writel(host, ATMCI_IDR, ~0UL);
	atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
	atmci_readl(host, ATMCI_SR);
2527
	clk_disable_unprepare(host->mck);
2528

2529
	del_timer_sync(&host->timer);
2530 2531
	if (host->dma.chan)
		dma_release_channel(host->dma.chan);
2532

2533
	free_irq(platform_get_irq(pdev, 0), host);
2534 2535 2536 2537 2538 2539 2540 2541

	return 0;
}

static struct platform_driver atmci_driver = {
	.remove		= __exit_p(atmci_remove),
	.driver		= {
		.name		= "atmel_mci",
2542
		.of_match_table	= of_match_ptr(atmci_dt_ids),
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
	},
};

static int __init atmci_init(void)
{
	return platform_driver_probe(&atmci_driver, atmci_probe);
}

static void __exit atmci_exit(void)
{
	platform_driver_unregister(&atmci_driver);
}

2556
late_initcall(atmci_init); /* try to load after dma driver when built-in */
2557 2558 2559
module_exit(atmci_exit);

MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
J
Jean Delvare 已提交
2560
MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2561
MODULE_LICENSE("GPL v2");