xref: /linux/drivers/mmc/host/dw_mmc.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f95f3850SWill Newton /*
3f95f3850SWill Newton  * Synopsys DesignWare Multimedia Card Interface driver
4f95f3850SWill Newton  *  (Based on NXP driver for lpc 31xx)
5f95f3850SWill Newton  *
6f95f3850SWill Newton  * Copyright (C) 2009 NXP Semiconductors
7f95f3850SWill Newton  * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
8f95f3850SWill Newton  */
9f95f3850SWill Newton 
10f95f3850SWill Newton #include <linux/blkdev.h>
11f95f3850SWill Newton #include <linux/clk.h>
12f95f3850SWill Newton #include <linux/debugfs.h>
13f95f3850SWill Newton #include <linux/device.h>
14f95f3850SWill Newton #include <linux/dma-mapping.h>
15f95f3850SWill Newton #include <linux/err.h>
16f95f3850SWill Newton #include <linux/init.h>
17f95f3850SWill Newton #include <linux/interrupt.h>
18b6d2d81cSShawn Lin #include <linux/iopoll.h>
19f95f3850SWill Newton #include <linux/ioport.h>
202b8ac062SVincent Whitchurch #include <linux/ktime.h>
21f95f3850SWill Newton #include <linux/module.h>
22f95f3850SWill Newton #include <linux/platform_device.h>
23a6db2c86SDouglas Anderson #include <linux/pm_runtime.h>
242b8ac062SVincent Whitchurch #include <linux/prandom.h>
25f95f3850SWill Newton #include <linux/seq_file.h>
26f95f3850SWill Newton #include <linux/slab.h>
27f95f3850SWill Newton #include <linux/stat.h>
28f95f3850SWill Newton #include <linux/delay.h>
29f95f3850SWill Newton #include <linux/irq.h>
30b24c8b26SDoug Anderson #include <linux/mmc/card.h>
31f95f3850SWill Newton #include <linux/mmc/host.h>
32f95f3850SWill Newton #include <linux/mmc/mmc.h>
3301730558SDoug Anderson #include <linux/mmc/sd.h>
3490c2143aSSeungwon Jeon #include <linux/mmc/sdio.h>
35f95f3850SWill Newton #include <linux/bitops.h>
36c07946a3SJaehoon Chung #include <linux/regulator/consumer.h>
37c91eab4bSThomas Abraham #include <linux/of.h>
38bf626e55SZhangfei Gao #include <linux/mmc/slot-gpio.h>
39f95f3850SWill Newton 
40f95f3850SWill Newton #include "dw_mmc.h"
41f95f3850SWill Newton 
42f95f3850SWill Newton /* Common flag combinations */
433f7eec62SJaehoon Chung #define DW_MCI_DATA_ERROR_FLAGS	(SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
44f95f3850SWill Newton 				 SDMMC_INT_HTO | SDMMC_INT_SBE  | \
457a3c5677SDoug Anderson 				 SDMMC_INT_EBE | SDMMC_INT_HLE)
46f95f3850SWill Newton #define DW_MCI_CMD_ERROR_FLAGS	(SDMMC_INT_RTO | SDMMC_INT_RCRC | \
477a3c5677SDoug Anderson 				 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
48f95f3850SWill Newton #define DW_MCI_ERROR_FLAGS	(DW_MCI_DATA_ERROR_FLAGS | \
497a3c5677SDoug Anderson 				 DW_MCI_CMD_ERROR_FLAGS)
50f95f3850SWill Newton #define DW_MCI_SEND_STATUS	1
51f95f3850SWill Newton #define DW_MCI_RECV_STATUS	2
52f95f3850SWill Newton #define DW_MCI_DMA_THRESHOLD	16
53f95f3850SWill Newton 
541f44a2a5SSeungwon Jeon #define DW_MCI_FREQ_MAX	200000000	/* unit: HZ */
5572e83577SJaehoon Chung #define DW_MCI_FREQ_MIN	100000		/* unit: HZ */
561f44a2a5SSeungwon Jeon 
57fc79a4d6SJoonyoung Shim #define IDMAC_INT_CLR		(SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
58fc79a4d6SJoonyoung Shim 				 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
59fc79a4d6SJoonyoung Shim 				 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
60fc79a4d6SJoonyoung Shim 				 SDMMC_IDMAC_INT_TI)
61fc79a4d6SJoonyoung Shim 
62cc190d4cSShawn Lin #define DESC_RING_BUF_SZ	PAGE_SIZE
63cc190d4cSShawn Lin 
6469d99fdcSPrabu Thangamuthu struct idmac_desc_64addr {
6569d99fdcSPrabu Thangamuthu 	u32		des0;	/* Control Descriptor */
66b6d2d81cSShawn Lin #define IDMAC_OWN_CLR64(x) \
67b6d2d81cSShawn Lin 	!((x) & cpu_to_le32(IDMAC_DES0_OWN))
6869d99fdcSPrabu Thangamuthu 
6969d99fdcSPrabu Thangamuthu 	u32		des1;	/* Reserved */
7069d99fdcSPrabu Thangamuthu 
7169d99fdcSPrabu Thangamuthu 	u32		des2;	/*Buffer sizes */
7269d99fdcSPrabu Thangamuthu #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
736687c42fSBen Dooks 	((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
746687c42fSBen Dooks 	 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
7569d99fdcSPrabu Thangamuthu 
7669d99fdcSPrabu Thangamuthu 	u32		des3;	/* Reserved */
7769d99fdcSPrabu Thangamuthu 
7869d99fdcSPrabu Thangamuthu 	u32		des4;	/* Lower 32-bits of Buffer Address Pointer 1*/
7969d99fdcSPrabu Thangamuthu 	u32		des5;	/* Upper 32-bits of Buffer Address Pointer 1*/
8069d99fdcSPrabu Thangamuthu 
8169d99fdcSPrabu Thangamuthu 	u32		des6;	/* Lower 32-bits of Next Descriptor Address */
8269d99fdcSPrabu Thangamuthu 	u32		des7;	/* Upper 32-bits of Next Descriptor Address */
8369d99fdcSPrabu Thangamuthu };
8469d99fdcSPrabu Thangamuthu 
85f95f3850SWill Newton struct idmac_desc {
866687c42fSBen Dooks 	__le32		des0;	/* Control Descriptor */
87f95f3850SWill Newton #define IDMAC_DES0_DIC	BIT(1)
88f95f3850SWill Newton #define IDMAC_DES0_LD	BIT(2)
89f95f3850SWill Newton #define IDMAC_DES0_FD	BIT(3)
90f95f3850SWill Newton #define IDMAC_DES0_CH	BIT(4)
91f95f3850SWill Newton #define IDMAC_DES0_ER	BIT(5)
92f95f3850SWill Newton #define IDMAC_DES0_CES	BIT(30)
93f95f3850SWill Newton #define IDMAC_DES0_OWN	BIT(31)
94f95f3850SWill Newton 
956687c42fSBen Dooks 	__le32		des1;	/* Buffer sizes */
96f95f3850SWill Newton #define IDMAC_SET_BUFFER1_SIZE(d, s) \
97e5306c3aSBen Dooks 	((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
98f95f3850SWill Newton 
996687c42fSBen Dooks 	__le32		des2;	/* buffer 1 physical address */
100f95f3850SWill Newton 
1016687c42fSBen Dooks 	__le32		des3;	/* buffer 2 physical address */
102f95f3850SWill Newton };
1035959b32eSAlexey Brodkin 
1045959b32eSAlexey Brodkin /* Each descriptor can transfer up to 4KB of data in chained mode */
1055959b32eSAlexey Brodkin #define DW_MCI_DESC_DATA_LENGTH	0x1000
106f95f3850SWill Newton 
107f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS)
dw_mci_req_show(struct seq_file * s,void * v)108f95f3850SWill Newton static int dw_mci_req_show(struct seq_file *s, void *v)
109f95f3850SWill Newton {
110f95f3850SWill Newton 	struct dw_mci_slot *slot = s->private;
111f95f3850SWill Newton 	struct mmc_request *mrq;
112f95f3850SWill Newton 	struct mmc_command *cmd;
113f95f3850SWill Newton 	struct mmc_command *stop;
114f95f3850SWill Newton 	struct mmc_data	*data;
115f95f3850SWill Newton 
116f95f3850SWill Newton 	/* Make sure we get a consistent snapshot */
117f95f3850SWill Newton 	spin_lock_bh(&slot->host->lock);
118f95f3850SWill Newton 	mrq = slot->mrq;
119f95f3850SWill Newton 
120f95f3850SWill Newton 	if (mrq) {
121f95f3850SWill Newton 		cmd = mrq->cmd;
122f95f3850SWill Newton 		data = mrq->data;
123f95f3850SWill Newton 		stop = mrq->stop;
124f95f3850SWill Newton 
125f95f3850SWill Newton 		if (cmd)
126f95f3850SWill Newton 			seq_printf(s,
127f95f3850SWill Newton 				   "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
128f95f3850SWill Newton 				   cmd->opcode, cmd->arg, cmd->flags,
129f95f3850SWill Newton 				   cmd->resp[0], cmd->resp[1], cmd->resp[2],
130f95f3850SWill Newton 				   cmd->resp[2], cmd->error);
131f95f3850SWill Newton 		if (data)
132f95f3850SWill Newton 			seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
133f95f3850SWill Newton 				   data->bytes_xfered, data->blocks,
134f95f3850SWill Newton 				   data->blksz, data->flags, data->error);
135f95f3850SWill Newton 		if (stop)
136f95f3850SWill Newton 			seq_printf(s,
137f95f3850SWill Newton 				   "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
138f95f3850SWill Newton 				   stop->opcode, stop->arg, stop->flags,
139f95f3850SWill Newton 				   stop->resp[0], stop->resp[1], stop->resp[2],
140f95f3850SWill Newton 				   stop->resp[2], stop->error);
141f95f3850SWill Newton 	}
142f95f3850SWill Newton 
143f95f3850SWill Newton 	spin_unlock_bh(&slot->host->lock);
144f95f3850SWill Newton 
145f95f3850SWill Newton 	return 0;
146f95f3850SWill Newton }
14764c1412bSShawn Lin DEFINE_SHOW_ATTRIBUTE(dw_mci_req);
148f95f3850SWill Newton 
dw_mci_regs_show(struct seq_file * s,void * v)149f95f3850SWill Newton static int dw_mci_regs_show(struct seq_file *s, void *v)
150f95f3850SWill Newton {
15121657ebdSJaehoon Chung 	struct dw_mci *host = s->private;
15221657ebdSJaehoon Chung 
1535b43df8bSShawn Lin 	pm_runtime_get_sync(host->dev);
1545b43df8bSShawn Lin 
15521657ebdSJaehoon Chung 	seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
15621657ebdSJaehoon Chung 	seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
15721657ebdSJaehoon Chung 	seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
15821657ebdSJaehoon Chung 	seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
15921657ebdSJaehoon Chung 	seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
16021657ebdSJaehoon Chung 	seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
161f95f3850SWill Newton 
1625b43df8bSShawn Lin 	pm_runtime_put_autosuspend(host->dev);
1635b43df8bSShawn Lin 
164f95f3850SWill Newton 	return 0;
165f95f3850SWill Newton }
16664c1412bSShawn Lin DEFINE_SHOW_ATTRIBUTE(dw_mci_regs);
167f95f3850SWill Newton 
dw_mci_init_debugfs(struct dw_mci_slot * slot)168f95f3850SWill Newton static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
169f95f3850SWill Newton {
170f95f3850SWill Newton 	struct mmc_host	*mmc = slot->mmc;
171f95f3850SWill Newton 	struct dw_mci *host = slot->host;
172f95f3850SWill Newton 	struct dentry *root;
173f95f3850SWill Newton 
174f95f3850SWill Newton 	root = mmc->debugfs_root;
175f95f3850SWill Newton 	if (!root)
176f95f3850SWill Newton 		return;
177f95f3850SWill Newton 
178fcac1527SGreg Kroah-Hartman 	debugfs_create_file("regs", S_IRUSR, root, host, &dw_mci_regs_fops);
179fcac1527SGreg Kroah-Hartman 	debugfs_create_file("req", S_IRUSR, root, slot, &dw_mci_req_fops);
180118e1118SGeert Uytterhoeven 	debugfs_create_u32("state", S_IRUSR, root, &host->state);
1810c40c1beSGeert Uytterhoeven 	debugfs_create_xul("pending_events", S_IRUSR, root,
1820c40c1beSGeert Uytterhoeven 			   &host->pending_events);
1830c40c1beSGeert Uytterhoeven 	debugfs_create_xul("completed_events", S_IRUSR, root,
1840c40c1beSGeert Uytterhoeven 			   &host->completed_events);
1852b8ac062SVincent Whitchurch #ifdef CONFIG_FAULT_INJECTION
1862b8ac062SVincent Whitchurch 	fault_create_debugfs_attr("fail_data_crc", root, &host->fail_data_crc);
1872b8ac062SVincent Whitchurch #endif
188f95f3850SWill Newton }
189f95f3850SWill Newton #endif /* defined(CONFIG_DEBUG_FS) */
190f95f3850SWill Newton 
dw_mci_ctrl_reset(struct dw_mci * host,u32 reset)1918e6db1f6SShawn Lin static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
1928e6db1f6SShawn Lin {
1938e6db1f6SShawn Lin 	u32 ctrl;
1948e6db1f6SShawn Lin 
1958e6db1f6SShawn Lin 	ctrl = mci_readl(host, CTRL);
1968e6db1f6SShawn Lin 	ctrl |= reset;
1978e6db1f6SShawn Lin 	mci_writel(host, CTRL, ctrl);
1988e6db1f6SShawn Lin 
1998e6db1f6SShawn Lin 	/* wait till resets clear */
2008e6db1f6SShawn Lin 	if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
2018e6db1f6SShawn Lin 				      !(ctrl & reset),
2028e6db1f6SShawn Lin 				      1, 500 * USEC_PER_MSEC)) {
2038e6db1f6SShawn Lin 		dev_err(host->dev,
2048e6db1f6SShawn Lin 			"Timeout resetting block (ctrl reset %#x)\n",
2058e6db1f6SShawn Lin 			ctrl & reset);
2068e6db1f6SShawn Lin 		return false;
2078e6db1f6SShawn Lin 	}
2088e6db1f6SShawn Lin 
2098e6db1f6SShawn Lin 	return true;
2108e6db1f6SShawn Lin }
21101730558SDoug Anderson 
dw_mci_wait_while_busy(struct dw_mci * host,u32 cmd_flags)2124dba18deSShawn Lin static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
2134dba18deSShawn Lin {
2144dba18deSShawn Lin 	u32 status;
2154dba18deSShawn Lin 
2164dba18deSShawn Lin 	/*
2174dba18deSShawn Lin 	 * Databook says that before issuing a new data transfer command
2184dba18deSShawn Lin 	 * we need to check to see if the card is busy.  Data transfer commands
2194dba18deSShawn Lin 	 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
2204dba18deSShawn Lin 	 *
2214dba18deSShawn Lin 	 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
2224dba18deSShawn Lin 	 * expected.
2234dba18deSShawn Lin 	 */
2244dba18deSShawn Lin 	if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
2254dba18deSShawn Lin 	    !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
2264dba18deSShawn Lin 		if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
2274dba18deSShawn Lin 					      status,
2284dba18deSShawn Lin 					      !(status & SDMMC_STATUS_BUSY),
2294dba18deSShawn Lin 					      10, 500 * USEC_PER_MSEC))
2304dba18deSShawn Lin 			dev_err(host->dev, "Busy; trying anyway\n");
2314dba18deSShawn Lin 	}
2324dba18deSShawn Lin }
2334dba18deSShawn Lin 
mci_send_cmd(struct dw_mci_slot * slot,u32 cmd,u32 arg)2344dba18deSShawn Lin static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
2354dba18deSShawn Lin {
2364dba18deSShawn Lin 	struct dw_mci *host = slot->host;
2374dba18deSShawn Lin 	unsigned int cmd_status = 0;
2384dba18deSShawn Lin 
2394dba18deSShawn Lin 	mci_writel(host, CMDARG, arg);
2404dba18deSShawn Lin 	wmb(); /* drain writebuffer */
2414dba18deSShawn Lin 	dw_mci_wait_while_busy(host, cmd);
2424dba18deSShawn Lin 	mci_writel(host, CMD, SDMMC_CMD_START | cmd);
2434dba18deSShawn Lin 
2444dba18deSShawn Lin 	if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
2454dba18deSShawn Lin 				      !(cmd_status & SDMMC_CMD_START),
2464dba18deSShawn Lin 				      1, 500 * USEC_PER_MSEC))
2474dba18deSShawn Lin 		dev_err(&slot->mmc->class_dev,
2484dba18deSShawn Lin 			"Timeout sending command (cmd %#x arg %#x status %#x)\n",
2494dba18deSShawn Lin 			cmd, arg, cmd_status);
2504dba18deSShawn Lin }
2514dba18deSShawn Lin 
dw_mci_prepare_command(struct mmc_host * mmc,struct mmc_command * cmd)252f95f3850SWill Newton static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
253f95f3850SWill Newton {
254800d78bfSThomas Abraham 	struct dw_mci_slot *slot = mmc_priv(mmc);
25501730558SDoug Anderson 	struct dw_mci *host = slot->host;
256f95f3850SWill Newton 	u32 cmdr;
257f95f3850SWill Newton 
2580e3a22c0SShawn Lin 	cmd->error = -EINPROGRESS;
259f95f3850SWill Newton 	cmdr = cmd->opcode;
260f95f3850SWill Newton 
26190c2143aSSeungwon Jeon 	if (cmd->opcode == MMC_STOP_TRANSMISSION ||
26290c2143aSSeungwon Jeon 	    cmd->opcode == MMC_GO_IDLE_STATE ||
26390c2143aSSeungwon Jeon 	    cmd->opcode == MMC_GO_INACTIVE_STATE ||
26490c2143aSSeungwon Jeon 	    (cmd->opcode == SD_IO_RW_DIRECT &&
26590c2143aSSeungwon Jeon 	     ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
266f95f3850SWill Newton 		cmdr |= SDMMC_CMD_STOP;
2674a1b27adSJaehoon Chung 	else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
268f95f3850SWill Newton 		cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
269f95f3850SWill Newton 
27001730558SDoug Anderson 	if (cmd->opcode == SD_SWITCH_VOLTAGE) {
27101730558SDoug Anderson 		u32 clk_en_a;
27201730558SDoug Anderson 
27301730558SDoug Anderson 		/* Special bit makes CMD11 not die */
27401730558SDoug Anderson 		cmdr |= SDMMC_CMD_VOLT_SWITCH;
27501730558SDoug Anderson 
27601730558SDoug Anderson 		/* Change state to continue to handle CMD11 weirdness */
27701730558SDoug Anderson 		WARN_ON(slot->host->state != STATE_SENDING_CMD);
27801730558SDoug Anderson 		slot->host->state = STATE_SENDING_CMD11;
27901730558SDoug Anderson 
28001730558SDoug Anderson 		/*
28101730558SDoug Anderson 		 * We need to disable low power mode (automatic clock stop)
28201730558SDoug Anderson 		 * while doing voltage switch so we don't confuse the card,
28301730558SDoug Anderson 		 * since stopping the clock is a specific part of the UHS
28401730558SDoug Anderson 		 * voltage change dance.
28501730558SDoug Anderson 		 *
28601730558SDoug Anderson 		 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
28701730558SDoug Anderson 		 * unconditionally turned back on in dw_mci_setup_bus() if it's
28801730558SDoug Anderson 		 * ever called with a non-zero clock.  That shouldn't happen
28901730558SDoug Anderson 		 * until the voltage change is all done.
29001730558SDoug Anderson 		 */
29101730558SDoug Anderson 		clk_en_a = mci_readl(host, CLKENA);
29201730558SDoug Anderson 		clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
29301730558SDoug Anderson 		mci_writel(host, CLKENA, clk_en_a);
29401730558SDoug Anderson 		mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
29501730558SDoug Anderson 			     SDMMC_CMD_PRV_DAT_WAIT, 0);
29601730558SDoug Anderson 	}
29701730558SDoug Anderson 
298f95f3850SWill Newton 	if (cmd->flags & MMC_RSP_PRESENT) {
299f95f3850SWill Newton 		/* We expect a response, so set this bit */
300f95f3850SWill Newton 		cmdr |= SDMMC_CMD_RESP_EXP;
301f95f3850SWill Newton 		if (cmd->flags & MMC_RSP_136)
302f95f3850SWill Newton 			cmdr |= SDMMC_CMD_RESP_LONG;
303f95f3850SWill Newton 	}
304f95f3850SWill Newton 
305f95f3850SWill Newton 	if (cmd->flags & MMC_RSP_CRC)
306f95f3850SWill Newton 		cmdr |= SDMMC_CMD_RESP_CRC;
307f95f3850SWill Newton 
3080349c085SJaehoon Chung 	if (cmd->data) {
309f95f3850SWill Newton 		cmdr |= SDMMC_CMD_DAT_EXP;
3100349c085SJaehoon Chung 		if (cmd->data->flags & MMC_DATA_WRITE)
311f95f3850SWill Newton 			cmdr |= SDMMC_CMD_DAT_WR;
312f95f3850SWill Newton 	}
313f95f3850SWill Newton 
314aaaaeb7aSJaehoon Chung 	if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
315aaaaeb7aSJaehoon Chung 		cmdr |= SDMMC_CMD_USE_HOLD_REG;
316800d78bfSThomas Abraham 
317f95f3850SWill Newton 	return cmdr;
318f95f3850SWill Newton }
319f95f3850SWill Newton 
dw_mci_prep_stop_abort(struct dw_mci * host,struct mmc_command * cmd)32090c2143aSSeungwon Jeon static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
32190c2143aSSeungwon Jeon {
32290c2143aSSeungwon Jeon 	struct mmc_command *stop;
32390c2143aSSeungwon Jeon 	u32 cmdr;
32490c2143aSSeungwon Jeon 
32590c2143aSSeungwon Jeon 	if (!cmd->data)
32690c2143aSSeungwon Jeon 		return 0;
32790c2143aSSeungwon Jeon 
32890c2143aSSeungwon Jeon 	stop = &host->stop_abort;
32990c2143aSSeungwon Jeon 	cmdr = cmd->opcode;
33090c2143aSSeungwon Jeon 	memset(stop, 0, sizeof(struct mmc_command));
33190c2143aSSeungwon Jeon 
33290c2143aSSeungwon Jeon 	if (cmdr == MMC_READ_SINGLE_BLOCK ||
33390c2143aSSeungwon Jeon 	    cmdr == MMC_READ_MULTIPLE_BLOCK ||
33490c2143aSSeungwon Jeon 	    cmdr == MMC_WRITE_BLOCK ||
3356c2c6506SUlf Hansson 	    cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
336*b98e7e8dSChanWoo Lee 	    mmc_op_tuning(cmdr) ||
3379f0d3cc2SMårten Lindahl 	    cmdr == MMC_GEN_CMD) {
33890c2143aSSeungwon Jeon 		stop->opcode = MMC_STOP_TRANSMISSION;
33990c2143aSSeungwon Jeon 		stop->arg = 0;
34090c2143aSSeungwon Jeon 		stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
34190c2143aSSeungwon Jeon 	} else if (cmdr == SD_IO_RW_EXTENDED) {
34290c2143aSSeungwon Jeon 		stop->opcode = SD_IO_RW_DIRECT;
34390c2143aSSeungwon Jeon 		stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
34490c2143aSSeungwon Jeon 			     ((cmd->arg >> 28) & 0x7);
34590c2143aSSeungwon Jeon 		stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
34690c2143aSSeungwon Jeon 	} else {
34790c2143aSSeungwon Jeon 		return 0;
34890c2143aSSeungwon Jeon 	}
34990c2143aSSeungwon Jeon 
35090c2143aSSeungwon Jeon 	cmdr = stop->opcode | SDMMC_CMD_STOP |
35190c2143aSSeungwon Jeon 		SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
35290c2143aSSeungwon Jeon 
35342f989c0SJaehoon Chung 	if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags))
3548c005b40SJaehoon Chung 		cmdr |= SDMMC_CMD_USE_HOLD_REG;
3558c005b40SJaehoon Chung 
35690c2143aSSeungwon Jeon 	return cmdr;
35790c2143aSSeungwon Jeon }
35890c2143aSSeungwon Jeon 
dw_mci_set_cto(struct dw_mci * host)35903de1921SAddy Ke static inline void dw_mci_set_cto(struct dw_mci *host)
36003de1921SAddy Ke {
36103de1921SAddy Ke 	unsigned int cto_clks;
3624c2357f5SDouglas Anderson 	unsigned int cto_div;
36303de1921SAddy Ke 	unsigned int cto_ms;
3648892b705SDouglas Anderson 	unsigned long irqflags;
36503de1921SAddy Ke 
36603de1921SAddy Ke 	cto_clks = mci_readl(host, TMOUT) & 0xff;
3674c2357f5SDouglas Anderson 	cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
3684c2357f5SDouglas Anderson 	if (cto_div == 0)
3694c2357f5SDouglas Anderson 		cto_div = 1;
370c7151602SEvgeniy Didin 
371c7151602SEvgeniy Didin 	cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div,
372c7151602SEvgeniy Didin 				  host->bus_hz);
37303de1921SAddy Ke 
37403de1921SAddy Ke 	/* add a bit spare time */
37503de1921SAddy Ke 	cto_ms += 10;
37603de1921SAddy Ke 
3778892b705SDouglas Anderson 	/*
3788892b705SDouglas Anderson 	 * The durations we're working with are fairly short so we have to be
3798892b705SDouglas Anderson 	 * extra careful about synchronization here.  Specifically in hardware a
3808892b705SDouglas Anderson 	 * command timeout is _at most_ 5.1 ms, so that means we expect an
3818892b705SDouglas Anderson 	 * interrupt (either command done or timeout) to come rather quickly
3828892b705SDouglas Anderson 	 * after the mci_writel.  ...but just in case we have a long interrupt
3838892b705SDouglas Anderson 	 * latency let's add a bit of paranoia.
3848892b705SDouglas Anderson 	 *
3858892b705SDouglas Anderson 	 * In general we'll assume that at least an interrupt will be asserted
3868892b705SDouglas Anderson 	 * in hardware by the time the cto_timer runs.  ...and if it hasn't
3878892b705SDouglas Anderson 	 * been asserted in hardware by that time then we'll assume it'll never
3888892b705SDouglas Anderson 	 * come.
3898892b705SDouglas Anderson 	 */
3908892b705SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
3918892b705SDouglas Anderson 	if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
39203de1921SAddy Ke 		mod_timer(&host->cto_timer,
39303de1921SAddy Ke 			jiffies + msecs_to_jiffies(cto_ms) + 1);
3948892b705SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
39503de1921SAddy Ke }
39603de1921SAddy Ke 
dw_mci_start_command(struct dw_mci * host,struct mmc_command * cmd,u32 cmd_flags)397f95f3850SWill Newton static void dw_mci_start_command(struct dw_mci *host,
398f95f3850SWill Newton 				 struct mmc_command *cmd, u32 cmd_flags)
399f95f3850SWill Newton {
400f95f3850SWill Newton 	host->cmd = cmd;
4014a90920cSThomas Abraham 	dev_vdbg(host->dev,
402f95f3850SWill Newton 		 "start command: ARGR=0x%08x CMDR=0x%08x\n",
403f95f3850SWill Newton 		 cmd->arg, cmd_flags);
404f95f3850SWill Newton 
405f95f3850SWill Newton 	mci_writel(host, CMDARG, cmd->arg);
4060e3a22c0SShawn Lin 	wmb(); /* drain writebuffer */
4070bdbd0e8SDoug Anderson 	dw_mci_wait_while_busy(host, cmd_flags);
408f95f3850SWill Newton 
4098892b705SDouglas Anderson 	mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
4108892b705SDouglas Anderson 
41103de1921SAddy Ke 	/* response expected command only */
41203de1921SAddy Ke 	if (cmd_flags & SDMMC_CMD_RESP_EXP)
41303de1921SAddy Ke 		dw_mci_set_cto(host);
414f95f3850SWill Newton }
415f95f3850SWill Newton 
send_stop_abort(struct dw_mci * host,struct mmc_data * data)41690c2143aSSeungwon Jeon static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
417f95f3850SWill Newton {
418e13c3c08SJaehoon Chung 	struct mmc_command *stop = &host->stop_abort;
4190e3a22c0SShawn Lin 
42090c2143aSSeungwon Jeon 	dw_mci_start_command(host, stop, host->stop_cmdr);
421f95f3850SWill Newton }
422f95f3850SWill Newton 
423f95f3850SWill Newton /* DMA interface functions */
dw_mci_stop_dma(struct dw_mci * host)424f95f3850SWill Newton static void dw_mci_stop_dma(struct dw_mci *host)
425f95f3850SWill Newton {
42603e8cb53SJames Hogan 	if (host->using_dma) {
427f95f3850SWill Newton 		host->dma_ops->stop(host);
428f95f3850SWill Newton 		host->dma_ops->cleanup(host);
429aa50f259SSeungwon Jeon 	}
430aa50f259SSeungwon Jeon 
431f95f3850SWill Newton 	/* Data transfer was stopped by the interrupt handler */
432f95f3850SWill Newton 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
433f95f3850SWill Newton }
434f95f3850SWill Newton 
dw_mci_dma_cleanup(struct dw_mci * host)435f95f3850SWill Newton static void dw_mci_dma_cleanup(struct dw_mci *host)
436f95f3850SWill Newton {
437f95f3850SWill Newton 	struct mmc_data *data = host->data;
438f95f3850SWill Newton 
439a4cc7eb4SJaehoon Chung 	if (data && data->host_cookie == COOKIE_MAPPED) {
4404a90920cSThomas Abraham 		dma_unmap_sg(host->dev,
4419aa51408SSeungwon Jeon 			     data->sg,
4429aa51408SSeungwon Jeon 			     data->sg_len,
443feeef096SHeiner Kallweit 			     mmc_get_dma_dir(data));
444a4cc7eb4SJaehoon Chung 		data->host_cookie = COOKIE_UNMAPPED;
445a4cc7eb4SJaehoon Chung 	}
446f95f3850SWill Newton }
447f95f3850SWill Newton 
dw_mci_idmac_reset(struct dw_mci * host)4485ce9d961SSeungwon Jeon static void dw_mci_idmac_reset(struct dw_mci *host)
4495ce9d961SSeungwon Jeon {
4505ce9d961SSeungwon Jeon 	u32 bmod = mci_readl(host, BMOD);
4515ce9d961SSeungwon Jeon 	/* Software reset of DMA */
4525ce9d961SSeungwon Jeon 	bmod |= SDMMC_IDMAC_SWRESET;
4535ce9d961SSeungwon Jeon 	mci_writel(host, BMOD, bmod);
4545ce9d961SSeungwon Jeon }
4555ce9d961SSeungwon Jeon 
dw_mci_idmac_stop_dma(struct dw_mci * host)456f95f3850SWill Newton static void dw_mci_idmac_stop_dma(struct dw_mci *host)
457f95f3850SWill Newton {
458f95f3850SWill Newton 	u32 temp;
459f95f3850SWill Newton 
460f95f3850SWill Newton 	/* Disable and reset the IDMAC interface */
461f95f3850SWill Newton 	temp = mci_readl(host, CTRL);
462f95f3850SWill Newton 	temp &= ~SDMMC_CTRL_USE_IDMAC;
463f95f3850SWill Newton 	temp |= SDMMC_CTRL_DMA_RESET;
464f95f3850SWill Newton 	mci_writel(host, CTRL, temp);
465f95f3850SWill Newton 
466f95f3850SWill Newton 	/* Stop the IDMAC running */
467f95f3850SWill Newton 	temp = mci_readl(host, BMOD);
468a5289a43SJaehoon Chung 	temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
4695ce9d961SSeungwon Jeon 	temp |= SDMMC_IDMAC_SWRESET;
470f95f3850SWill Newton 	mci_writel(host, BMOD, temp);
471f95f3850SWill Newton }
472f95f3850SWill Newton 
dw_mci_dmac_complete_dma(void * arg)4733fc7eaefSShawn Lin static void dw_mci_dmac_complete_dma(void *arg)
474f95f3850SWill Newton {
4753fc7eaefSShawn Lin 	struct dw_mci *host = arg;
476f95f3850SWill Newton 	struct mmc_data *data = host->data;
477f95f3850SWill Newton 
4784a90920cSThomas Abraham 	dev_vdbg(host->dev, "DMA complete\n");
479f95f3850SWill Newton 
4803fc7eaefSShawn Lin 	if ((host->use_dma == TRANS_MODE_EDMAC) &&
4813fc7eaefSShawn Lin 	    data && (data->flags & MMC_DATA_READ))
4823fc7eaefSShawn Lin 		/* Invalidate cache after read */
48342f989c0SJaehoon Chung 		dma_sync_sg_for_cpu(mmc_dev(host->slot->mmc),
4843fc7eaefSShawn Lin 				    data->sg,
4853fc7eaefSShawn Lin 				    data->sg_len,
4863fc7eaefSShawn Lin 				    DMA_FROM_DEVICE);
4873fc7eaefSShawn Lin 
488f95f3850SWill Newton 	host->dma_ops->cleanup(host);
489f95f3850SWill Newton 
490f95f3850SWill Newton 	/*
491f95f3850SWill Newton 	 * If the card was removed, data will be NULL. No point in trying to
492f95f3850SWill Newton 	 * send the stop command or waiting for NBUSY in this case.
493f95f3850SWill Newton 	 */
494f95f3850SWill Newton 	if (data) {
495f95f3850SWill Newton 		set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
496f95f3850SWill Newton 		tasklet_schedule(&host->tasklet);
497f95f3850SWill Newton 	}
498f95f3850SWill Newton }
499f95f3850SWill Newton 
dw_mci_idmac_init(struct dw_mci * host)500f95f3850SWill Newton static int dw_mci_idmac_init(struct dw_mci *host)
501f95f3850SWill Newton {
502897b69e7SSeungwon Jeon 	int i;
503f95f3850SWill Newton 
50469d99fdcSPrabu Thangamuthu 	if (host->dma_64bit_address == 1) {
50569d99fdcSPrabu Thangamuthu 		struct idmac_desc_64addr *p;
50669d99fdcSPrabu Thangamuthu 		/* Number of descriptors in the ring buffer */
507cc190d4cSShawn Lin 		host->ring_size =
508cc190d4cSShawn Lin 			DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
50969d99fdcSPrabu Thangamuthu 
51069d99fdcSPrabu Thangamuthu 		/* Forward link the descriptor list */
51169d99fdcSPrabu Thangamuthu 		for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
51269d99fdcSPrabu Thangamuthu 								i++, p++) {
51369d99fdcSPrabu Thangamuthu 			p->des6 = (host->sg_dma +
51469d99fdcSPrabu Thangamuthu 					(sizeof(struct idmac_desc_64addr) *
51569d99fdcSPrabu Thangamuthu 							(i + 1))) & 0xffffffff;
51669d99fdcSPrabu Thangamuthu 
51769d99fdcSPrabu Thangamuthu 			p->des7 = (u64)(host->sg_dma +
51869d99fdcSPrabu Thangamuthu 					(sizeof(struct idmac_desc_64addr) *
51969d99fdcSPrabu Thangamuthu 							(i + 1))) >> 32;
52069d99fdcSPrabu Thangamuthu 			/* Initialize reserved and buffer size fields to "0" */
52147b7de2fSEvgeniy Didin 			p->des0 = 0;
52269d99fdcSPrabu Thangamuthu 			p->des1 = 0;
52369d99fdcSPrabu Thangamuthu 			p->des2 = 0;
52469d99fdcSPrabu Thangamuthu 			p->des3 = 0;
52569d99fdcSPrabu Thangamuthu 		}
52669d99fdcSPrabu Thangamuthu 
52769d99fdcSPrabu Thangamuthu 		/* Set the last descriptor as the end-of-ring descriptor */
52869d99fdcSPrabu Thangamuthu 		p->des6 = host->sg_dma & 0xffffffff;
52969d99fdcSPrabu Thangamuthu 		p->des7 = (u64)host->sg_dma >> 32;
53069d99fdcSPrabu Thangamuthu 		p->des0 = IDMAC_DES0_ER;
53169d99fdcSPrabu Thangamuthu 
53269d99fdcSPrabu Thangamuthu 	} else {
53369d99fdcSPrabu Thangamuthu 		struct idmac_desc *p;
534f95f3850SWill Newton 		/* Number of descriptors in the ring buffer */
535cc190d4cSShawn Lin 		host->ring_size =
536cc190d4cSShawn Lin 			DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
537f95f3850SWill Newton 
538f95f3850SWill Newton 		/* Forward link the descriptor list */
5390e3a22c0SShawn Lin 		for (i = 0, p = host->sg_cpu;
5400e3a22c0SShawn Lin 		     i < host->ring_size - 1;
5410e3a22c0SShawn Lin 		     i++, p++) {
5426687c42fSBen Dooks 			p->des3 = cpu_to_le32(host->sg_dma +
5436687c42fSBen Dooks 					(sizeof(struct idmac_desc) * (i + 1)));
54447b7de2fSEvgeniy Didin 			p->des0 = 0;
5454b244724SZhangfei Gao 			p->des1 = 0;
5464b244724SZhangfei Gao 		}
547f95f3850SWill Newton 
548f95f3850SWill Newton 		/* Set the last descriptor as the end-of-ring descriptor */
5496687c42fSBen Dooks 		p->des3 = cpu_to_le32(host->sg_dma);
5506687c42fSBen Dooks 		p->des0 = cpu_to_le32(IDMAC_DES0_ER);
55169d99fdcSPrabu Thangamuthu 	}
552f95f3850SWill Newton 
5535ce9d961SSeungwon Jeon 	dw_mci_idmac_reset(host);
554141a712aSSeungwon Jeon 
55569d99fdcSPrabu Thangamuthu 	if (host->dma_64bit_address == 1) {
55669d99fdcSPrabu Thangamuthu 		/* Mask out interrupts - get Tx & Rx complete only */
55769d99fdcSPrabu Thangamuthu 		mci_writel(host, IDSTS64, IDMAC_INT_CLR);
55869d99fdcSPrabu Thangamuthu 		mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
55969d99fdcSPrabu Thangamuthu 				SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
56069d99fdcSPrabu Thangamuthu 
56169d99fdcSPrabu Thangamuthu 		/* Set the descriptor base address */
56269d99fdcSPrabu Thangamuthu 		mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
56369d99fdcSPrabu Thangamuthu 		mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
56469d99fdcSPrabu Thangamuthu 
56569d99fdcSPrabu Thangamuthu 	} else {
566f95f3850SWill Newton 		/* Mask out interrupts - get Tx & Rx complete only */
567fc79a4d6SJoonyoung Shim 		mci_writel(host, IDSTS, IDMAC_INT_CLR);
56869d99fdcSPrabu Thangamuthu 		mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
56969d99fdcSPrabu Thangamuthu 				SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
570f95f3850SWill Newton 
571f95f3850SWill Newton 		/* Set the descriptor base address */
572f95f3850SWill Newton 		mci_writel(host, DBADDR, host->sg_dma);
57369d99fdcSPrabu Thangamuthu 	}
57469d99fdcSPrabu Thangamuthu 
575f95f3850SWill Newton 	return 0;
576f95f3850SWill Newton }
577f95f3850SWill Newton 
dw_mci_prepare_desc64(struct dw_mci * host,struct mmc_data * data,unsigned int sg_len)5783b2a067bSShawn Lin static inline int dw_mci_prepare_desc64(struct dw_mci *host,
5793b2a067bSShawn Lin 					 struct mmc_data *data,
5803b2a067bSShawn Lin 					 unsigned int sg_len)
5813b2a067bSShawn Lin {
5823b2a067bSShawn Lin 	unsigned int desc_len;
5833b2a067bSShawn Lin 	struct idmac_desc_64addr *desc_first, *desc_last, *desc;
584b6d2d81cSShawn Lin 	u32 val;
5853b2a067bSShawn Lin 	int i;
5863b2a067bSShawn Lin 
5873b2a067bSShawn Lin 	desc_first = desc_last = desc = host->sg_cpu;
5883b2a067bSShawn Lin 
5893b2a067bSShawn Lin 	for (i = 0; i < sg_len; i++) {
5903b2a067bSShawn Lin 		unsigned int length = sg_dma_len(&data->sg[i]);
5913b2a067bSShawn Lin 
5923b2a067bSShawn Lin 		u64 mem_addr = sg_dma_address(&data->sg[i]);
5933b2a067bSShawn Lin 
5943b2a067bSShawn Lin 		for ( ; length ; desc++) {
5953b2a067bSShawn Lin 			desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
5963b2a067bSShawn Lin 				   length : DW_MCI_DESC_DATA_LENGTH;
5973b2a067bSShawn Lin 
5983b2a067bSShawn Lin 			length -= desc_len;
5993b2a067bSShawn Lin 
6003b2a067bSShawn Lin 			/*
6013b2a067bSShawn Lin 			 * Wait for the former clear OWN bit operation
6023b2a067bSShawn Lin 			 * of IDMAC to make sure that this descriptor
6033b2a067bSShawn Lin 			 * isn't still owned by IDMAC as IDMAC's write
6043b2a067bSShawn Lin 			 * ops and CPU's read ops are asynchronous.
6053b2a067bSShawn Lin 			 */
606b6d2d81cSShawn Lin 			if (readl_poll_timeout_atomic(&desc->des0, val,
607b6d2d81cSShawn Lin 						!(val & IDMAC_DES0_OWN),
608b6d2d81cSShawn Lin 						10, 100 * USEC_PER_MSEC))
6093b2a067bSShawn Lin 				goto err_own_bit;
6103b2a067bSShawn Lin 
6113b2a067bSShawn Lin 			/*
6123b2a067bSShawn Lin 			 * Set the OWN bit and disable interrupts
6133b2a067bSShawn Lin 			 * for this descriptor
6143b2a067bSShawn Lin 			 */
6153b2a067bSShawn Lin 			desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
6163b2a067bSShawn Lin 						IDMAC_DES0_CH;
6173b2a067bSShawn Lin 
6183b2a067bSShawn Lin 			/* Buffer length */
6193b2a067bSShawn Lin 			IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
6203b2a067bSShawn Lin 
6213b2a067bSShawn Lin 			/* Physical address to DMA to/from */
6223b2a067bSShawn Lin 			desc->des4 = mem_addr & 0xffffffff;
6233b2a067bSShawn Lin 			desc->des5 = mem_addr >> 32;
6243b2a067bSShawn Lin 
6253b2a067bSShawn Lin 			/* Update physical address for the next desc */
6263b2a067bSShawn Lin 			mem_addr += desc_len;
6273b2a067bSShawn Lin 
6283b2a067bSShawn Lin 			/* Save pointer to the last descriptor */
6293b2a067bSShawn Lin 			desc_last = desc;
6303b2a067bSShawn Lin 		}
6313b2a067bSShawn Lin 	}
6323b2a067bSShawn Lin 
6333b2a067bSShawn Lin 	/* Set first descriptor */
6343b2a067bSShawn Lin 	desc_first->des0 |= IDMAC_DES0_FD;
6353b2a067bSShawn Lin 
6363b2a067bSShawn Lin 	/* Set last descriptor */
6373b2a067bSShawn Lin 	desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
6383b2a067bSShawn Lin 	desc_last->des0 |= IDMAC_DES0_LD;
6393b2a067bSShawn Lin 
6403b2a067bSShawn Lin 	return 0;
6413b2a067bSShawn Lin err_own_bit:
6423b2a067bSShawn Lin 	/* restore the descriptor chain as it's polluted */
64326be9d70SColin Ian King 	dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
644cc190d4cSShawn Lin 	memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
6453b2a067bSShawn Lin 	dw_mci_idmac_init(host);
6463b2a067bSShawn Lin 	return -EINVAL;
6473b2a067bSShawn Lin }
6483b2a067bSShawn Lin 
6493b2a067bSShawn Lin 
dw_mci_prepare_desc32(struct dw_mci * host,struct mmc_data * data,unsigned int sg_len)6503b2a067bSShawn Lin static inline int dw_mci_prepare_desc32(struct dw_mci *host,
6513b2a067bSShawn Lin 					 struct mmc_data *data,
6523b2a067bSShawn Lin 					 unsigned int sg_len)
6533b2a067bSShawn Lin {
6543b2a067bSShawn Lin 	unsigned int desc_len;
6553b2a067bSShawn Lin 	struct idmac_desc *desc_first, *desc_last, *desc;
656b6d2d81cSShawn Lin 	u32 val;
6573b2a067bSShawn Lin 	int i;
6583b2a067bSShawn Lin 
6593b2a067bSShawn Lin 	desc_first = desc_last = desc = host->sg_cpu;
6603b2a067bSShawn Lin 
6613b2a067bSShawn Lin 	for (i = 0; i < sg_len; i++) {
6623b2a067bSShawn Lin 		unsigned int length = sg_dma_len(&data->sg[i]);
6633b2a067bSShawn Lin 
6643b2a067bSShawn Lin 		u32 mem_addr = sg_dma_address(&data->sg[i]);
6653b2a067bSShawn Lin 
6663b2a067bSShawn Lin 		for ( ; length ; desc++) {
6673b2a067bSShawn Lin 			desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
6683b2a067bSShawn Lin 				   length : DW_MCI_DESC_DATA_LENGTH;
6693b2a067bSShawn Lin 
6703b2a067bSShawn Lin 			length -= desc_len;
6713b2a067bSShawn Lin 
6723b2a067bSShawn Lin 			/*
6733b2a067bSShawn Lin 			 * Wait for the former clear OWN bit operation
6743b2a067bSShawn Lin 			 * of IDMAC to make sure that this descriptor
6753b2a067bSShawn Lin 			 * isn't still owned by IDMAC as IDMAC's write
6763b2a067bSShawn Lin 			 * ops and CPU's read ops are asynchronous.
6773b2a067bSShawn Lin 			 */
678b6d2d81cSShawn Lin 			if (readl_poll_timeout_atomic(&desc->des0, val,
679b6d2d81cSShawn Lin 						      IDMAC_OWN_CLR64(val),
680b6d2d81cSShawn Lin 						      10,
681b6d2d81cSShawn Lin 						      100 * USEC_PER_MSEC))
6823b2a067bSShawn Lin 				goto err_own_bit;
6833b2a067bSShawn Lin 
6843b2a067bSShawn Lin 			/*
6853b2a067bSShawn Lin 			 * Set the OWN bit and disable interrupts
6863b2a067bSShawn Lin 			 * for this descriptor
6873b2a067bSShawn Lin 			 */
6883b2a067bSShawn Lin 			desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
6893b2a067bSShawn Lin 						 IDMAC_DES0_DIC |
6903b2a067bSShawn Lin 						 IDMAC_DES0_CH);
6913b2a067bSShawn Lin 
6923b2a067bSShawn Lin 			/* Buffer length */
6933b2a067bSShawn Lin 			IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
6943b2a067bSShawn Lin 
6953b2a067bSShawn Lin 			/* Physical address to DMA to/from */
6963b2a067bSShawn Lin 			desc->des2 = cpu_to_le32(mem_addr);
6973b2a067bSShawn Lin 
6983b2a067bSShawn Lin 			/* Update physical address for the next desc */
6993b2a067bSShawn Lin 			mem_addr += desc_len;
7003b2a067bSShawn Lin 
7013b2a067bSShawn Lin 			/* Save pointer to the last descriptor */
7023b2a067bSShawn Lin 			desc_last = desc;
7033b2a067bSShawn Lin 		}
7043b2a067bSShawn Lin 	}
7053b2a067bSShawn Lin 
7063b2a067bSShawn Lin 	/* Set first descriptor */
7073b2a067bSShawn Lin 	desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
7083b2a067bSShawn Lin 
7093b2a067bSShawn Lin 	/* Set last descriptor */
7103b2a067bSShawn Lin 	desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
7113b2a067bSShawn Lin 				       IDMAC_DES0_DIC));
7123b2a067bSShawn Lin 	desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
7133b2a067bSShawn Lin 
7143b2a067bSShawn Lin 	return 0;
7153b2a067bSShawn Lin err_own_bit:
7163b2a067bSShawn Lin 	/* restore the descriptor chain as it's polluted */
71726be9d70SColin Ian King 	dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
718cc190d4cSShawn Lin 	memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
7193b2a067bSShawn Lin 	dw_mci_idmac_init(host);
7203b2a067bSShawn Lin 	return -EINVAL;
7213b2a067bSShawn Lin }
7223b2a067bSShawn Lin 
dw_mci_idmac_start_dma(struct dw_mci * host,unsigned int sg_len)7233b2a067bSShawn Lin static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
7243b2a067bSShawn Lin {
7253b2a067bSShawn Lin 	u32 temp;
7263b2a067bSShawn Lin 	int ret;
7273b2a067bSShawn Lin 
7283b2a067bSShawn Lin 	if (host->dma_64bit_address == 1)
7293b2a067bSShawn Lin 		ret = dw_mci_prepare_desc64(host, host->data, sg_len);
7303b2a067bSShawn Lin 	else
7313b2a067bSShawn Lin 		ret = dw_mci_prepare_desc32(host, host->data, sg_len);
7323b2a067bSShawn Lin 
7333b2a067bSShawn Lin 	if (ret)
7343b2a067bSShawn Lin 		goto out;
7353b2a067bSShawn Lin 
7363b2a067bSShawn Lin 	/* drain writebuffer */
7373b2a067bSShawn Lin 	wmb();
7383b2a067bSShawn Lin 
7393b2a067bSShawn Lin 	/* Make sure to reset DMA in case we did PIO before this */
7403b2a067bSShawn Lin 	dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
7413b2a067bSShawn Lin 	dw_mci_idmac_reset(host);
7423b2a067bSShawn Lin 
7433b2a067bSShawn Lin 	/* Select IDMAC interface */
7443b2a067bSShawn Lin 	temp = mci_readl(host, CTRL);
7453b2a067bSShawn Lin 	temp |= SDMMC_CTRL_USE_IDMAC;
7463b2a067bSShawn Lin 	mci_writel(host, CTRL, temp);
7473b2a067bSShawn Lin 
7483b2a067bSShawn Lin 	/* drain writebuffer */
7493b2a067bSShawn Lin 	wmb();
7503b2a067bSShawn Lin 
7513b2a067bSShawn Lin 	/* Enable the IDMAC */
7523b2a067bSShawn Lin 	temp = mci_readl(host, BMOD);
7533b2a067bSShawn Lin 	temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
7543b2a067bSShawn Lin 	mci_writel(host, BMOD, temp);
7553b2a067bSShawn Lin 
7563b2a067bSShawn Lin 	/* Start it running */
7573b2a067bSShawn Lin 	mci_writel(host, PLDMND, 1);
7583b2a067bSShawn Lin 
7593b2a067bSShawn Lin out:
7603b2a067bSShawn Lin 	return ret;
7613b2a067bSShawn Lin }
7623b2a067bSShawn Lin 
7638e2b36eaSArnd Bergmann static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
764885c3e80SSeungwon Jeon 	.init = dw_mci_idmac_init,
765885c3e80SSeungwon Jeon 	.start = dw_mci_idmac_start_dma,
766885c3e80SSeungwon Jeon 	.stop = dw_mci_idmac_stop_dma,
7673fc7eaefSShawn Lin 	.complete = dw_mci_dmac_complete_dma,
768885c3e80SSeungwon Jeon 	.cleanup = dw_mci_dma_cleanup,
769885c3e80SSeungwon Jeon };
7703fc7eaefSShawn Lin 
dw_mci_edmac_stop_dma(struct dw_mci * host)7713fc7eaefSShawn Lin static void dw_mci_edmac_stop_dma(struct dw_mci *host)
7723fc7eaefSShawn Lin {
773ab925a31SShawn Lin 	dmaengine_terminate_async(host->dms->ch);
7743fc7eaefSShawn Lin }
7753fc7eaefSShawn Lin 
dw_mci_edmac_start_dma(struct dw_mci * host,unsigned int sg_len)7763fc7eaefSShawn Lin static int dw_mci_edmac_start_dma(struct dw_mci *host,
7773fc7eaefSShawn Lin 					    unsigned int sg_len)
7783fc7eaefSShawn Lin {
7793fc7eaefSShawn Lin 	struct dma_slave_config cfg;
7803fc7eaefSShawn Lin 	struct dma_async_tx_descriptor *desc = NULL;
7813fc7eaefSShawn Lin 	struct scatterlist *sgl = host->data->sg;
78227d70d36SColin Ian King 	static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
7833fc7eaefSShawn Lin 	u32 sg_elems = host->data->sg_len;
7843fc7eaefSShawn Lin 	u32 fifoth_val;
7853fc7eaefSShawn Lin 	u32 fifo_offset = host->fifo_reg - host->regs;
7863fc7eaefSShawn Lin 	int ret = 0;
7873fc7eaefSShawn Lin 
7883fc7eaefSShawn Lin 	/* Set external dma config: burst size, burst width */
789c3ff0189STony Lindgren 	memset(&cfg, 0, sizeof(cfg));
790260b3164SArnd Bergmann 	cfg.dst_addr = host->phy_regs + fifo_offset;
7913fc7eaefSShawn Lin 	cfg.src_addr = cfg.dst_addr;
7923fc7eaefSShawn Lin 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
7933fc7eaefSShawn Lin 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
7943fc7eaefSShawn Lin 
7953fc7eaefSShawn Lin 	/* Match burst msize with external dma config */
7963fc7eaefSShawn Lin 	fifoth_val = mci_readl(host, FIFOTH);
7973fc7eaefSShawn Lin 	cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
7983fc7eaefSShawn Lin 	cfg.src_maxburst = cfg.dst_maxburst;
7993fc7eaefSShawn Lin 
8003fc7eaefSShawn Lin 	if (host->data->flags & MMC_DATA_WRITE)
8013fc7eaefSShawn Lin 		cfg.direction = DMA_MEM_TO_DEV;
8023fc7eaefSShawn Lin 	else
8033fc7eaefSShawn Lin 		cfg.direction = DMA_DEV_TO_MEM;
8043fc7eaefSShawn Lin 
8053fc7eaefSShawn Lin 	ret = dmaengine_slave_config(host->dms->ch, &cfg);
8063fc7eaefSShawn Lin 	if (ret) {
8073fc7eaefSShawn Lin 		dev_err(host->dev, "Failed to config edmac.\n");
8083fc7eaefSShawn Lin 		return -EBUSY;
8093fc7eaefSShawn Lin 	}
8103fc7eaefSShawn Lin 
8113fc7eaefSShawn Lin 	desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
8123fc7eaefSShawn Lin 				       sg_len, cfg.direction,
8133fc7eaefSShawn Lin 				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
8143fc7eaefSShawn Lin 	if (!desc) {
8153fc7eaefSShawn Lin 		dev_err(host->dev, "Can't prepare slave sg.\n");
8163fc7eaefSShawn Lin 		return -EBUSY;
8173fc7eaefSShawn Lin 	}
8183fc7eaefSShawn Lin 
8193fc7eaefSShawn Lin 	/* Set dw_mci_dmac_complete_dma as callback */
8203fc7eaefSShawn Lin 	desc->callback = dw_mci_dmac_complete_dma;
8213fc7eaefSShawn Lin 	desc->callback_param = (void *)host;
8223fc7eaefSShawn Lin 	dmaengine_submit(desc);
8233fc7eaefSShawn Lin 
8243fc7eaefSShawn Lin 	/* Flush cache before write */
8253fc7eaefSShawn Lin 	if (host->data->flags & MMC_DATA_WRITE)
82642f989c0SJaehoon Chung 		dma_sync_sg_for_device(mmc_dev(host->slot->mmc), sgl,
8273fc7eaefSShawn Lin 				       sg_elems, DMA_TO_DEVICE);
8283fc7eaefSShawn Lin 
8293fc7eaefSShawn Lin 	dma_async_issue_pending(host->dms->ch);
8303fc7eaefSShawn Lin 
8313fc7eaefSShawn Lin 	return 0;
8323fc7eaefSShawn Lin }
8333fc7eaefSShawn Lin 
dw_mci_edmac_init(struct dw_mci * host)8343fc7eaefSShawn Lin static int dw_mci_edmac_init(struct dw_mci *host)
8353fc7eaefSShawn Lin {
8363fc7eaefSShawn Lin 	/* Request external dma channel */
8373fc7eaefSShawn Lin 	host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
8383fc7eaefSShawn Lin 	if (!host->dms)
8393fc7eaefSShawn Lin 		return -ENOMEM;
8403fc7eaefSShawn Lin 
841c1fce225SPeter Ujfalusi 	host->dms->ch = dma_request_chan(host->dev, "rx-tx");
842c1fce225SPeter Ujfalusi 	if (IS_ERR(host->dms->ch)) {
843c1fce225SPeter Ujfalusi 		int ret = PTR_ERR(host->dms->ch);
844c1fce225SPeter Ujfalusi 
8454539d36eSDan Carpenter 		dev_err(host->dev, "Failed to get external DMA channel.\n");
8463fc7eaefSShawn Lin 		kfree(host->dms);
8473fc7eaefSShawn Lin 		host->dms = NULL;
848c1fce225SPeter Ujfalusi 		return ret;
8493fc7eaefSShawn Lin 	}
8503fc7eaefSShawn Lin 
8513fc7eaefSShawn Lin 	return 0;
8523fc7eaefSShawn Lin }
8533fc7eaefSShawn Lin 
dw_mci_edmac_exit(struct dw_mci * host)8543fc7eaefSShawn Lin static void dw_mci_edmac_exit(struct dw_mci *host)
8553fc7eaefSShawn Lin {
8563fc7eaefSShawn Lin 	if (host->dms) {
8573fc7eaefSShawn Lin 		if (host->dms->ch) {
8583fc7eaefSShawn Lin 			dma_release_channel(host->dms->ch);
8593fc7eaefSShawn Lin 			host->dms->ch = NULL;
8603fc7eaefSShawn Lin 		}
8613fc7eaefSShawn Lin 		kfree(host->dms);
8623fc7eaefSShawn Lin 		host->dms = NULL;
8633fc7eaefSShawn Lin 	}
8643fc7eaefSShawn Lin }
8653fc7eaefSShawn Lin 
8663fc7eaefSShawn Lin static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
8673fc7eaefSShawn Lin 	.init = dw_mci_edmac_init,
8683fc7eaefSShawn Lin 	.exit = dw_mci_edmac_exit,
8693fc7eaefSShawn Lin 	.start = dw_mci_edmac_start_dma,
8703fc7eaefSShawn Lin 	.stop = dw_mci_edmac_stop_dma,
8713fc7eaefSShawn Lin 	.complete = dw_mci_dmac_complete_dma,
8723fc7eaefSShawn Lin 	.cleanup = dw_mci_dma_cleanup,
8733fc7eaefSShawn Lin };
874885c3e80SSeungwon Jeon 
dw_mci_pre_dma_transfer(struct dw_mci * host,struct mmc_data * data,int cookie)8759aa51408SSeungwon Jeon static int dw_mci_pre_dma_transfer(struct dw_mci *host,
8769aa51408SSeungwon Jeon 				   struct mmc_data *data,
877a4cc7eb4SJaehoon Chung 				   int cookie)
878f95f3850SWill Newton {
879f95f3850SWill Newton 	struct scatterlist *sg;
8809aa51408SSeungwon Jeon 	unsigned int i, sg_len;
881f95f3850SWill Newton 
882a4cc7eb4SJaehoon Chung 	if (data->host_cookie == COOKIE_PRE_MAPPED)
883a4cc7eb4SJaehoon Chung 		return data->sg_len;
884f95f3850SWill Newton 
885f95f3850SWill Newton 	/*
886f95f3850SWill Newton 	 * We don't do DMA on "complex" transfers, i.e. with
887f95f3850SWill Newton 	 * non-word-aligned buffers or lengths. Also, we don't bother
888f95f3850SWill Newton 	 * with all the DMA setup overhead for short transfers.
889f95f3850SWill Newton 	 */
890f95f3850SWill Newton 	if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
891f95f3850SWill Newton 		return -EINVAL;
8929aa51408SSeungwon Jeon 
893f95f3850SWill Newton 	if (data->blksz & 3)
894f95f3850SWill Newton 		return -EINVAL;
895f95f3850SWill Newton 
896f95f3850SWill Newton 	for_each_sg(data->sg, sg, data->sg_len, i) {
897f95f3850SWill Newton 		if (sg->offset & 3 || sg->length & 3)
898f95f3850SWill Newton 			return -EINVAL;
899f95f3850SWill Newton 	}
900f95f3850SWill Newton 
9014a90920cSThomas Abraham 	sg_len = dma_map_sg(host->dev,
9029aa51408SSeungwon Jeon 			    data->sg,
9039aa51408SSeungwon Jeon 			    data->sg_len,
904feeef096SHeiner Kallweit 			    mmc_get_dma_dir(data));
9059aa51408SSeungwon Jeon 	if (sg_len == 0)
9069aa51408SSeungwon Jeon 		return -EINVAL;
9079aa51408SSeungwon Jeon 
908a4cc7eb4SJaehoon Chung 	data->host_cookie = cookie;
9099aa51408SSeungwon Jeon 
9109aa51408SSeungwon Jeon 	return sg_len;
9119aa51408SSeungwon Jeon }
9129aa51408SSeungwon Jeon 
dw_mci_pre_req(struct mmc_host * mmc,struct mmc_request * mrq)9139aa51408SSeungwon Jeon static void dw_mci_pre_req(struct mmc_host *mmc,
914d3c6aac3SLinus Walleij 			   struct mmc_request *mrq)
9159aa51408SSeungwon Jeon {
9169aa51408SSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
9179aa51408SSeungwon Jeon 	struct mmc_data *data = mrq->data;
9189aa51408SSeungwon Jeon 
9199aa51408SSeungwon Jeon 	if (!slot->host->use_dma || !data)
9209aa51408SSeungwon Jeon 		return;
9219aa51408SSeungwon Jeon 
922a4cc7eb4SJaehoon Chung 	/* This data might be unmapped at this time */
923a4cc7eb4SJaehoon Chung 	data->host_cookie = COOKIE_UNMAPPED;
9249aa51408SSeungwon Jeon 
925a4cc7eb4SJaehoon Chung 	if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
926a4cc7eb4SJaehoon Chung 				COOKIE_PRE_MAPPED) < 0)
927a4cc7eb4SJaehoon Chung 		data->host_cookie = COOKIE_UNMAPPED;
9289aa51408SSeungwon Jeon }
9299aa51408SSeungwon Jeon 
dw_mci_post_req(struct mmc_host * mmc,struct mmc_request * mrq,int err)9309aa51408SSeungwon Jeon static void dw_mci_post_req(struct mmc_host *mmc,
9319aa51408SSeungwon Jeon 			    struct mmc_request *mrq,
9329aa51408SSeungwon Jeon 			    int err)
9339aa51408SSeungwon Jeon {
9349aa51408SSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
9359aa51408SSeungwon Jeon 	struct mmc_data *data = mrq->data;
9369aa51408SSeungwon Jeon 
9379aa51408SSeungwon Jeon 	if (!slot->host->use_dma || !data)
9389aa51408SSeungwon Jeon 		return;
9399aa51408SSeungwon Jeon 
940a4cc7eb4SJaehoon Chung 	if (data->host_cookie != COOKIE_UNMAPPED)
9414a90920cSThomas Abraham 		dma_unmap_sg(slot->host->dev,
9429aa51408SSeungwon Jeon 			     data->sg,
9439aa51408SSeungwon Jeon 			     data->sg_len,
944feeef096SHeiner Kallweit 			     mmc_get_dma_dir(data));
945a4cc7eb4SJaehoon Chung 	data->host_cookie = COOKIE_UNMAPPED;
9469aa51408SSeungwon Jeon }
9479aa51408SSeungwon Jeon 
dw_mci_get_cd(struct mmc_host * mmc)948671fa142SShawn Lin static int dw_mci_get_cd(struct mmc_host *mmc)
949671fa142SShawn Lin {
950671fa142SShawn Lin 	int present;
951671fa142SShawn Lin 	struct dw_mci_slot *slot = mmc_priv(mmc);
952671fa142SShawn Lin 	struct dw_mci *host = slot->host;
953671fa142SShawn Lin 	int gpio_cd = mmc_gpio_get_cd(mmc);
954671fa142SShawn Lin 
955671fa142SShawn Lin 	/* Use platform get_cd function, else try onboard card detect */
956671fa142SShawn Lin 	if (((mmc->caps & MMC_CAP_NEEDS_POLL)
957671fa142SShawn Lin 				|| !mmc_card_is_removable(mmc))) {
958671fa142SShawn Lin 		present = 1;
959671fa142SShawn Lin 
960671fa142SShawn Lin 		if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
961671fa142SShawn Lin 			if (mmc->caps & MMC_CAP_NEEDS_POLL) {
962671fa142SShawn Lin 				dev_info(&mmc->class_dev,
963671fa142SShawn Lin 					"card is polling.\n");
964671fa142SShawn Lin 			} else {
965671fa142SShawn Lin 				dev_info(&mmc->class_dev,
966671fa142SShawn Lin 					"card is non-removable.\n");
967671fa142SShawn Lin 			}
968671fa142SShawn Lin 			set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
969671fa142SShawn Lin 		}
970671fa142SShawn Lin 
971671fa142SShawn Lin 		return present;
972671fa142SShawn Lin 	} else if (gpio_cd >= 0)
973671fa142SShawn Lin 		present = gpio_cd;
974671fa142SShawn Lin 	else
975671fa142SShawn Lin 		present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
976671fa142SShawn Lin 			== 0 ? 1 : 0;
977671fa142SShawn Lin 
978671fa142SShawn Lin 	spin_lock_bh(&host->lock);
979671fa142SShawn Lin 	if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
980671fa142SShawn Lin 		dev_dbg(&mmc->class_dev, "card is present\n");
981671fa142SShawn Lin 	else if (!present &&
982671fa142SShawn Lin 			!test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
983671fa142SShawn Lin 		dev_dbg(&mmc->class_dev, "card is not present\n");
984671fa142SShawn Lin 	spin_unlock_bh(&host->lock);
985671fa142SShawn Lin 
986671fa142SShawn Lin 	return present;
987671fa142SShawn Lin }
988671fa142SShawn Lin 
dw_mci_adjust_fifoth(struct dw_mci * host,struct mmc_data * data)98952426899SSeungwon Jeon static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
99052426899SSeungwon Jeon {
99152426899SSeungwon Jeon 	unsigned int blksz = data->blksz;
99227d70d36SColin Ian King 	static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
99352426899SSeungwon Jeon 	u32 fifo_width = 1 << host->data_shift;
99452426899SSeungwon Jeon 	u32 blksz_depth = blksz / fifo_width, fifoth_val;
99552426899SSeungwon Jeon 	u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
9960e3a22c0SShawn Lin 	int idx = ARRAY_SIZE(mszs) - 1;
99752426899SSeungwon Jeon 
9983fc7eaefSShawn Lin 	/* pio should ship this scenario */
9993fc7eaefSShawn Lin 	if (!host->use_dma)
10003fc7eaefSShawn Lin 		return;
10013fc7eaefSShawn Lin 
100252426899SSeungwon Jeon 	tx_wmark = (host->fifo_depth) / 2;
100352426899SSeungwon Jeon 	tx_wmark_invers = host->fifo_depth - tx_wmark;
100452426899SSeungwon Jeon 
100552426899SSeungwon Jeon 	/*
100652426899SSeungwon Jeon 	 * MSIZE is '1',
100752426899SSeungwon Jeon 	 * if blksz is not a multiple of the FIFO width
100852426899SSeungwon Jeon 	 */
100920753569SShawn Lin 	if (blksz % fifo_width)
101052426899SSeungwon Jeon 		goto done;
101152426899SSeungwon Jeon 
101252426899SSeungwon Jeon 	do {
101352426899SSeungwon Jeon 		if (!((blksz_depth % mszs[idx]) ||
101452426899SSeungwon Jeon 		     (tx_wmark_invers % mszs[idx]))) {
101552426899SSeungwon Jeon 			msize = idx;
101652426899SSeungwon Jeon 			rx_wmark = mszs[idx] - 1;
101752426899SSeungwon Jeon 			break;
101852426899SSeungwon Jeon 		}
101952426899SSeungwon Jeon 	} while (--idx > 0);
102052426899SSeungwon Jeon 	/*
102152426899SSeungwon Jeon 	 * If idx is '0', it won't be tried
102252426899SSeungwon Jeon 	 * Thus, initial values are uesed
102352426899SSeungwon Jeon 	 */
102452426899SSeungwon Jeon done:
102552426899SSeungwon Jeon 	fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
102652426899SSeungwon Jeon 	mci_writel(host, FIFOTH, fifoth_val);
102752426899SSeungwon Jeon }
102852426899SSeungwon Jeon 
dw_mci_ctrl_thld(struct dw_mci * host,struct mmc_data * data)10297e4bf1bcSJaehoon Chung static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
1030f1d2736cSSeungwon Jeon {
1031f1d2736cSSeungwon Jeon 	unsigned int blksz = data->blksz;
1032f1d2736cSSeungwon Jeon 	u32 blksz_depth, fifo_depth;
1033f1d2736cSSeungwon Jeon 	u16 thld_size;
10347e4bf1bcSJaehoon Chung 	u8 enable;
1035f1d2736cSSeungwon Jeon 
103666dfd101SJames Hogan 	/*
103766dfd101SJames Hogan 	 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
103866dfd101SJames Hogan 	 * in the FIFO region, so we really shouldn't access it).
103966dfd101SJames Hogan 	 */
10407e4bf1bcSJaehoon Chung 	if (host->verid < DW_MMC_240A ||
10417e4bf1bcSJaehoon Chung 		(host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
104266dfd101SJames Hogan 		return;
104366dfd101SJames Hogan 
10447e4bf1bcSJaehoon Chung 	/*
10457e4bf1bcSJaehoon Chung 	 * Card write Threshold is introduced since 2.80a
10467e4bf1bcSJaehoon Chung 	 * It's used when HS400 mode is enabled.
10477e4bf1bcSJaehoon Chung 	 */
10487e4bf1bcSJaehoon Chung 	if (data->flags & MMC_DATA_WRITE &&
10497a6b9f4dSx00270170 		host->timing != MMC_TIMING_MMC_HS400)
10507a6b9f4dSx00270170 		goto disable;
10517e4bf1bcSJaehoon Chung 
10527e4bf1bcSJaehoon Chung 	if (data->flags & MMC_DATA_WRITE)
10537e4bf1bcSJaehoon Chung 		enable = SDMMC_CARD_WR_THR_EN;
10547e4bf1bcSJaehoon Chung 	else
10557e4bf1bcSJaehoon Chung 		enable = SDMMC_CARD_RD_THR_EN;
10567e4bf1bcSJaehoon Chung 
1057f1d2736cSSeungwon Jeon 	if (host->timing != MMC_TIMING_MMC_HS200 &&
10587a6b9f4dSx00270170 	    host->timing != MMC_TIMING_UHS_SDR104 &&
10597a6b9f4dSx00270170 	    host->timing != MMC_TIMING_MMC_HS400)
1060f1d2736cSSeungwon Jeon 		goto disable;
1061f1d2736cSSeungwon Jeon 
1062f1d2736cSSeungwon Jeon 	blksz_depth = blksz / (1 << host->data_shift);
1063f1d2736cSSeungwon Jeon 	fifo_depth = host->fifo_depth;
1064f1d2736cSSeungwon Jeon 
1065f1d2736cSSeungwon Jeon 	if (blksz_depth > fifo_depth)
1066f1d2736cSSeungwon Jeon 		goto disable;
1067f1d2736cSSeungwon Jeon 
1068f1d2736cSSeungwon Jeon 	/*
1069f1d2736cSSeungwon Jeon 	 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1070f1d2736cSSeungwon Jeon 	 * If (blksz_depth) <  (fifo_depth >> 1), should be thld_size = blksz
1071f1d2736cSSeungwon Jeon 	 * Currently just choose blksz.
1072f1d2736cSSeungwon Jeon 	 */
1073f1d2736cSSeungwon Jeon 	thld_size = blksz;
10747e4bf1bcSJaehoon Chung 	mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
1075f1d2736cSSeungwon Jeon 	return;
1076f1d2736cSSeungwon Jeon 
1077f1d2736cSSeungwon Jeon disable:
10787e4bf1bcSJaehoon Chung 	mci_writel(host, CDTHRCTL, 0);
1079f1d2736cSSeungwon Jeon }
1080f1d2736cSSeungwon Jeon 
dw_mci_submit_data_dma(struct dw_mci * host,struct mmc_data * data)10819aa51408SSeungwon Jeon static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
10829aa51408SSeungwon Jeon {
1083f8c58c11SDoug Anderson 	unsigned long irqflags;
10849aa51408SSeungwon Jeon 	int sg_len;
10859aa51408SSeungwon Jeon 	u32 temp;
10869aa51408SSeungwon Jeon 
10879aa51408SSeungwon Jeon 	host->using_dma = 0;
10889aa51408SSeungwon Jeon 
10899aa51408SSeungwon Jeon 	/* If we don't have a channel, we can't do DMA */
10909aa51408SSeungwon Jeon 	if (!host->use_dma)
10919aa51408SSeungwon Jeon 		return -ENODEV;
10929aa51408SSeungwon Jeon 
1093a4cc7eb4SJaehoon Chung 	sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
1094a99aa9b9SSeungwon Jeon 	if (sg_len < 0) {
1095a99aa9b9SSeungwon Jeon 		host->dma_ops->stop(host);
10969aa51408SSeungwon Jeon 		return sg_len;
1097a99aa9b9SSeungwon Jeon 	}
10989aa51408SSeungwon Jeon 
109903e8cb53SJames Hogan 	host->using_dma = 1;
110003e8cb53SJames Hogan 
11013fc7eaefSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC)
11024a90920cSThomas Abraham 		dev_vdbg(host->dev,
1103f95f3850SWill Newton 			 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
11043fc7eaefSShawn Lin 			 (unsigned long)host->sg_cpu,
11053fc7eaefSShawn Lin 			 (unsigned long)host->sg_dma,
1106f95f3850SWill Newton 			 sg_len);
1107f95f3850SWill Newton 
110852426899SSeungwon Jeon 	/*
110952426899SSeungwon Jeon 	 * Decide the MSIZE and RX/TX Watermark.
111052426899SSeungwon Jeon 	 * If current block size is same with previous size,
111152426899SSeungwon Jeon 	 * no need to update fifoth.
111252426899SSeungwon Jeon 	 */
111352426899SSeungwon Jeon 	if (host->prev_blksz != data->blksz)
111452426899SSeungwon Jeon 		dw_mci_adjust_fifoth(host, data);
111552426899SSeungwon Jeon 
1116f95f3850SWill Newton 	/* Enable the DMA interface */
1117f95f3850SWill Newton 	temp = mci_readl(host, CTRL);
1118f95f3850SWill Newton 	temp |= SDMMC_CTRL_DMA_ENABLE;
1119f95f3850SWill Newton 	mci_writel(host, CTRL, temp);
1120f95f3850SWill Newton 
1121f95f3850SWill Newton 	/* Disable RX/TX IRQs, let DMA handle it */
1122f8c58c11SDoug Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
1123f95f3850SWill Newton 	temp = mci_readl(host, INTMASK);
1124f95f3850SWill Newton 	temp  &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1125f95f3850SWill Newton 	mci_writel(host, INTMASK, temp);
1126f8c58c11SDoug Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
1127f95f3850SWill Newton 
11283fc7eaefSShawn Lin 	if (host->dma_ops->start(host, sg_len)) {
1129647f80a1SJaehoon Chung 		host->dma_ops->stop(host);
1130d12d0cb1SShawn Lin 		/* We can't do DMA, try PIO for this one */
1131d12d0cb1SShawn Lin 		dev_dbg(host->dev,
1132d12d0cb1SShawn Lin 			"%s: fall back to PIO mode for current transfer\n",
1133d12d0cb1SShawn Lin 			__func__);
11343fc7eaefSShawn Lin 		return -ENODEV;
11353fc7eaefSShawn Lin 	}
1136f95f3850SWill Newton 
1137f95f3850SWill Newton 	return 0;
1138f95f3850SWill Newton }
1139f95f3850SWill Newton 
dw_mci_submit_data(struct dw_mci * host,struct mmc_data * data)1140f95f3850SWill Newton static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1141f95f3850SWill Newton {
1142f8c58c11SDoug Anderson 	unsigned long irqflags;
11430e3a22c0SShawn Lin 	int flags = SG_MITER_ATOMIC;
1144f95f3850SWill Newton 	u32 temp;
1145f95f3850SWill Newton 
1146f95f3850SWill Newton 	data->error = -EINPROGRESS;
1147f95f3850SWill Newton 
1148f95f3850SWill Newton 	WARN_ON(host->data);
1149f95f3850SWill Newton 	host->sg = NULL;
1150f95f3850SWill Newton 	host->data = data;
1151f95f3850SWill Newton 
11527e4bf1bcSJaehoon Chung 	if (data->flags & MMC_DATA_READ)
115355c5efbcSJames Hogan 		host->dir_status = DW_MCI_RECV_STATUS;
11547e4bf1bcSJaehoon Chung 	else
115555c5efbcSJames Hogan 		host->dir_status = DW_MCI_SEND_STATUS;
11567e4bf1bcSJaehoon Chung 
11577e4bf1bcSJaehoon Chung 	dw_mci_ctrl_thld(host, data);
115855c5efbcSJames Hogan 
1159f95f3850SWill Newton 	if (dw_mci_submit_data_dma(host, data)) {
1160f9c2a0dcSSeungwon Jeon 		if (host->data->flags & MMC_DATA_READ)
1161f9c2a0dcSSeungwon Jeon 			flags |= SG_MITER_TO_SG;
1162f9c2a0dcSSeungwon Jeon 		else
1163f9c2a0dcSSeungwon Jeon 			flags |= SG_MITER_FROM_SG;
1164f9c2a0dcSSeungwon Jeon 
1165f9c2a0dcSSeungwon Jeon 		sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1166f95f3850SWill Newton 		host->sg = data->sg;
116734b664a2SJames Hogan 		host->part_buf_start = 0;
116834b664a2SJames Hogan 		host->part_buf_count = 0;
1169f95f3850SWill Newton 
1170b40af3aaSJames Hogan 		mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
1171f8c58c11SDoug Anderson 
1172f8c58c11SDoug Anderson 		spin_lock_irqsave(&host->irq_lock, irqflags);
1173f95f3850SWill Newton 		temp = mci_readl(host, INTMASK);
1174f95f3850SWill Newton 		temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1175f95f3850SWill Newton 		mci_writel(host, INTMASK, temp);
1176f8c58c11SDoug Anderson 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
1177f95f3850SWill Newton 
1178f95f3850SWill Newton 		temp = mci_readl(host, CTRL);
1179f95f3850SWill Newton 		temp &= ~SDMMC_CTRL_DMA_ENABLE;
1180f95f3850SWill Newton 		mci_writel(host, CTRL, temp);
118152426899SSeungwon Jeon 
118252426899SSeungwon Jeon 		/*
1183d6fced83SJun Nie 		 * Use the initial fifoth_val for PIO mode. If wm_algined
1184d6fced83SJun Nie 		 * is set, we set watermark same as data size.
118552426899SSeungwon Jeon 		 * If next issued data may be transfered by DMA mode,
118652426899SSeungwon Jeon 		 * prev_blksz should be invalidated.
118752426899SSeungwon Jeon 		 */
1188d6fced83SJun Nie 		if (host->wm_aligned)
1189d6fced83SJun Nie 			dw_mci_adjust_fifoth(host, data);
1190d6fced83SJun Nie 		else
119152426899SSeungwon Jeon 			mci_writel(host, FIFOTH, host->fifoth_val);
119252426899SSeungwon Jeon 		host->prev_blksz = 0;
119352426899SSeungwon Jeon 	} else {
119452426899SSeungwon Jeon 		/*
119552426899SSeungwon Jeon 		 * Keep the current block size.
119652426899SSeungwon Jeon 		 * It will be used to decide whether to update
119752426899SSeungwon Jeon 		 * fifoth register next time.
119852426899SSeungwon Jeon 		 */
119952426899SSeungwon Jeon 		host->prev_blksz = data->blksz;
1200f95f3850SWill Newton 	}
1201f95f3850SWill Newton }
1202f95f3850SWill Newton 
dw_mci_setup_bus(struct dw_mci_slot * slot,bool force_clkinit)1203ab269128SAbhilash Kesavan static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
1204f95f3850SWill Newton {
1205f95f3850SWill Newton 	struct dw_mci *host = slot->host;
1206fdf492a1SDoug Anderson 	unsigned int clock = slot->clock;
1207f95f3850SWill Newton 	u32 div;
12089623b5b9SDoug Anderson 	u32 clk_en_a;
120901730558SDoug Anderson 	u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
121001730558SDoug Anderson 
121101730558SDoug Anderson 	/* We must continue to set bit 28 in CMD until the change is complete */
121201730558SDoug Anderson 	if (host->state == STATE_WAITING_CMD11_DONE)
121301730558SDoug Anderson 		sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
1214f95f3850SWill Newton 
1215ff178981SShawn Lin 	slot->mmc->actual_clock = 0;
1216ff178981SShawn Lin 
1217fdf492a1SDoug Anderson 	if (!clock) {
1218fdf492a1SDoug Anderson 		mci_writel(host, CLKENA, 0);
121901730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1220fdf492a1SDoug Anderson 	} else if (clock != host->current_speed || force_clkinit) {
1221fdf492a1SDoug Anderson 		div = host->bus_hz / clock;
1222fdf492a1SDoug Anderson 		if (host->bus_hz % clock && host->bus_hz > clock)
1223f95f3850SWill Newton 			/*
1224f95f3850SWill Newton 			 * move the + 1 after the divide to prevent
1225f95f3850SWill Newton 			 * over-clocking the card.
1226f95f3850SWill Newton 			 */
1227e419990bSSeungwon Jeon 			div += 1;
1228e419990bSSeungwon Jeon 
1229fdf492a1SDoug Anderson 		div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
1230f95f3850SWill Newton 
1231e6cd7a8eSJaehoon Chung 		if ((clock != slot->__clk_old &&
1232e6cd7a8eSJaehoon Chung 			!test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1233e6cd7a8eSJaehoon Chung 			force_clkinit) {
1234ce69e2feSShawn Lin 			/* Silent the verbose log if calling from PM context */
1235ce69e2feSShawn Lin 			if (!force_clkinit)
1236f95f3850SWill Newton 				dev_info(&slot->mmc->class_dev,
1237fdf492a1SDoug Anderson 					 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1238fdf492a1SDoug Anderson 					 slot->id, host->bus_hz, clock,
1239fdf492a1SDoug Anderson 					 div ? ((host->bus_hz / div) >> 1) :
1240fdf492a1SDoug Anderson 					 host->bus_hz, div);
1241f95f3850SWill Newton 
1242e6cd7a8eSJaehoon Chung 			/*
1243e6cd7a8eSJaehoon Chung 			 * If card is polling, display the message only
1244e6cd7a8eSJaehoon Chung 			 * one time at boot time.
1245e6cd7a8eSJaehoon Chung 			 */
1246e6cd7a8eSJaehoon Chung 			if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1247e6cd7a8eSJaehoon Chung 					slot->mmc->f_min == clock)
1248e6cd7a8eSJaehoon Chung 				set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1249e6cd7a8eSJaehoon Chung 		}
1250e6cd7a8eSJaehoon Chung 
1251f95f3850SWill Newton 		/* disable clock */
1252f95f3850SWill Newton 		mci_writel(host, CLKENA, 0);
1253f95f3850SWill Newton 		mci_writel(host, CLKSRC, 0);
1254f95f3850SWill Newton 
1255f95f3850SWill Newton 		/* inform CIU */
125601730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1257f95f3850SWill Newton 
1258f95f3850SWill Newton 		/* set clock to desired speed */
1259f95f3850SWill Newton 		mci_writel(host, CLKDIV, div);
1260f95f3850SWill Newton 
1261f95f3850SWill Newton 		/* inform CIU */
126201730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1263f95f3850SWill Newton 
12649623b5b9SDoug Anderson 		/* enable clock; only low power if no SDIO */
12659623b5b9SDoug Anderson 		clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1266b24c8b26SDoug Anderson 		if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
12679623b5b9SDoug Anderson 			clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
12689623b5b9SDoug Anderson 		mci_writel(host, CLKENA, clk_en_a);
1269f95f3850SWill Newton 
1270f95f3850SWill Newton 		/* inform CIU */
127101730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1272005d675aSJaehoon Chung 
1273005d675aSJaehoon Chung 		/* keep the last clock value that was requested from core */
1274005d675aSJaehoon Chung 		slot->__clk_old = clock;
1275ff178981SShawn Lin 		slot->mmc->actual_clock = div ? ((host->bus_hz / div) >> 1) :
1276ff178981SShawn Lin 					  host->bus_hz;
1277f95f3850SWill Newton 	}
1278f95f3850SWill Newton 
1279fdf492a1SDoug Anderson 	host->current_speed = clock;
1280fdf492a1SDoug Anderson 
1281f95f3850SWill Newton 	/* Set the current slot bus width */
12821d56c453SSeungwon Jeon 	mci_writel(host, CTYPE, (slot->ctype << slot->id));
1283f95f3850SWill Newton }
1284f95f3850SWill Newton 
dw_mci_set_data_timeout(struct dw_mci * host,unsigned int timeout_ns)12856a8c2018SMårten Lindahl static void dw_mci_set_data_timeout(struct dw_mci *host,
12866a8c2018SMårten Lindahl 				    unsigned int timeout_ns)
12876a8c2018SMårten Lindahl {
128825d5417aSMårten Lindahl 	const struct dw_mci_drv_data *drv_data = host->drv_data;
12896a8c2018SMårten Lindahl 	u32 clk_div, tmout;
12906a8c2018SMårten Lindahl 	u64 tmp;
12916a8c2018SMårten Lindahl 
129225d5417aSMårten Lindahl 	if (drv_data && drv_data->set_data_timeout)
129325d5417aSMårten Lindahl 		return drv_data->set_data_timeout(host, timeout_ns);
129425d5417aSMårten Lindahl 
12956a8c2018SMårten Lindahl 	clk_div = (mci_readl(host, CLKDIV) & 0xFF) * 2;
12966a8c2018SMårten Lindahl 	if (clk_div == 0)
12976a8c2018SMårten Lindahl 		clk_div = 1;
12986a8c2018SMårten Lindahl 
12996a8c2018SMårten Lindahl 	tmp = DIV_ROUND_UP_ULL((u64)timeout_ns * host->bus_hz, NSEC_PER_SEC);
13006a8c2018SMårten Lindahl 	tmp = DIV_ROUND_UP_ULL(tmp, clk_div);
13016a8c2018SMårten Lindahl 
13026a8c2018SMårten Lindahl 	/* TMOUT[7:0] (RESPONSE_TIMEOUT) */
13036a8c2018SMårten Lindahl 	tmout = 0xFF; /* Set maximum */
13046a8c2018SMårten Lindahl 
13056a8c2018SMårten Lindahl 	/* TMOUT[31:8] (DATA_TIMEOUT) */
13066a8c2018SMårten Lindahl 	if (!tmp || tmp > 0xFFFFFF)
13076a8c2018SMårten Lindahl 		tmout |= (0xFFFFFF << 8);
13086a8c2018SMårten Lindahl 	else
13096a8c2018SMårten Lindahl 		tmout |= (tmp & 0xFFFFFF) << 8;
13106a8c2018SMårten Lindahl 
13116a8c2018SMårten Lindahl 	mci_writel(host, TMOUT, tmout);
1312ebc4dcf1SDan Carpenter 	dev_dbg(host->dev, "timeout_ns: %u => TMOUT[31:8]: %#08x",
13136a8c2018SMårten Lindahl 		timeout_ns, tmout >> 8);
13146a8c2018SMårten Lindahl }
13156a8c2018SMårten Lindahl 
__dw_mci_start_request(struct dw_mci * host,struct dw_mci_slot * slot,struct mmc_command * cmd)1316053b3ce6SSeungwon Jeon static void __dw_mci_start_request(struct dw_mci *host,
1317053b3ce6SSeungwon Jeon 				   struct dw_mci_slot *slot,
1318053b3ce6SSeungwon Jeon 				   struct mmc_command *cmd)
1319f95f3850SWill Newton {
1320f95f3850SWill Newton 	struct mmc_request *mrq;
1321f95f3850SWill Newton 	struct mmc_data	*data;
1322f95f3850SWill Newton 	u32 cmdflags;
1323f95f3850SWill Newton 
1324f95f3850SWill Newton 	mrq = slot->mrq;
1325f95f3850SWill Newton 
1326f95f3850SWill Newton 	host->mrq = mrq;
1327f95f3850SWill Newton 
1328f95f3850SWill Newton 	host->pending_events = 0;
1329f95f3850SWill Newton 	host->completed_events = 0;
1330e352c813SSeungwon Jeon 	host->cmd_status = 0;
1331f95f3850SWill Newton 	host->data_status = 0;
1332e352c813SSeungwon Jeon 	host->dir_status = 0;
1333f95f3850SWill Newton 
1334053b3ce6SSeungwon Jeon 	data = cmd->data;
1335f95f3850SWill Newton 	if (data) {
13366a8c2018SMårten Lindahl 		dw_mci_set_data_timeout(host, data->timeout_ns);
1337f95f3850SWill Newton 		mci_writel(host, BYTCNT, data->blksz*data->blocks);
1338f95f3850SWill Newton 		mci_writel(host, BLKSIZ, data->blksz);
1339f95f3850SWill Newton 	}
1340f95f3850SWill Newton 
1341f95f3850SWill Newton 	cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1342f95f3850SWill Newton 
1343f95f3850SWill Newton 	/* this is the first command, send the initialization clock */
1344f95f3850SWill Newton 	if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1345f95f3850SWill Newton 		cmdflags |= SDMMC_CMD_INIT;
1346f95f3850SWill Newton 
1347f95f3850SWill Newton 	if (data) {
1348f95f3850SWill Newton 		dw_mci_submit_data(host, data);
13490e3a22c0SShawn Lin 		wmb(); /* drain writebuffer */
1350f95f3850SWill Newton 	}
1351f95f3850SWill Newton 
1352f95f3850SWill Newton 	dw_mci_start_command(host, cmd, cmdflags);
1353f95f3850SWill Newton 
13545c935165SDoug Anderson 	if (cmd->opcode == SD_SWITCH_VOLTAGE) {
135549ba0302SDoug Anderson 		unsigned long irqflags;
135649ba0302SDoug Anderson 
13575c935165SDoug Anderson 		/*
13588886a6fdSDoug Anderson 		 * Databook says to fail after 2ms w/ no response, but evidence
13598886a6fdSDoug Anderson 		 * shows that sometimes the cmd11 interrupt takes over 130ms.
13608886a6fdSDoug Anderson 		 * We'll set to 500ms, plus an extra jiffy just in case jiffies
13618886a6fdSDoug Anderson 		 * is just about to roll over.
136249ba0302SDoug Anderson 		 *
136349ba0302SDoug Anderson 		 * We do this whole thing under spinlock and only if the
13641ad0dcb9Swangjianli 		 * command hasn't already completed (indicating the irq
136549ba0302SDoug Anderson 		 * already ran so we don't want the timeout).
13665c935165SDoug Anderson 		 */
136749ba0302SDoug Anderson 		spin_lock_irqsave(&host->irq_lock, irqflags);
136849ba0302SDoug Anderson 		if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
13695c935165SDoug Anderson 			mod_timer(&host->cmd11_timer,
13708886a6fdSDoug Anderson 				jiffies + msecs_to_jiffies(500) + 1);
137149ba0302SDoug Anderson 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
13725c935165SDoug Anderson 	}
13735c935165SDoug Anderson 
137490c2143aSSeungwon Jeon 	host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
1375f95f3850SWill Newton }
1376f95f3850SWill Newton 
dw_mci_start_request(struct dw_mci * host,struct dw_mci_slot * slot)1377053b3ce6SSeungwon Jeon static void dw_mci_start_request(struct dw_mci *host,
1378053b3ce6SSeungwon Jeon 				 struct dw_mci_slot *slot)
1379053b3ce6SSeungwon Jeon {
1380053b3ce6SSeungwon Jeon 	struct mmc_request *mrq = slot->mrq;
1381053b3ce6SSeungwon Jeon 	struct mmc_command *cmd;
1382053b3ce6SSeungwon Jeon 
1383053b3ce6SSeungwon Jeon 	cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1384053b3ce6SSeungwon Jeon 	__dw_mci_start_request(host, slot, cmd);
1385053b3ce6SSeungwon Jeon }
1386053b3ce6SSeungwon Jeon 
13877456caaeSJames Hogan /* must be called with host->lock held */
dw_mci_queue_request(struct dw_mci * host,struct dw_mci_slot * slot,struct mmc_request * mrq)1388f95f3850SWill Newton static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1389f95f3850SWill Newton 				 struct mmc_request *mrq)
1390f95f3850SWill Newton {
1391f95f3850SWill Newton 	dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1392f95f3850SWill Newton 		 host->state);
1393f95f3850SWill Newton 
1394f95f3850SWill Newton 	slot->mrq = mrq;
1395f95f3850SWill Newton 
139601730558SDoug Anderson 	if (host->state == STATE_WAITING_CMD11_DONE) {
139701730558SDoug Anderson 		dev_warn(&slot->mmc->class_dev,
139801730558SDoug Anderson 			 "Voltage change didn't complete\n");
139901730558SDoug Anderson 		/*
140001730558SDoug Anderson 		 * this case isn't expected to happen, so we can
140101730558SDoug Anderson 		 * either crash here or just try to continue on
140201730558SDoug Anderson 		 * in the closest possible state
140301730558SDoug Anderson 		 */
140401730558SDoug Anderson 		host->state = STATE_IDLE;
140501730558SDoug Anderson 	}
140601730558SDoug Anderson 
1407f95f3850SWill Newton 	if (host->state == STATE_IDLE) {
1408f95f3850SWill Newton 		host->state = STATE_SENDING_CMD;
1409f95f3850SWill Newton 		dw_mci_start_request(host, slot);
1410f95f3850SWill Newton 	} else {
1411f95f3850SWill Newton 		list_add_tail(&slot->queue_node, &host->queue);
1412f95f3850SWill Newton 	}
1413f95f3850SWill Newton }
1414f95f3850SWill Newton 
dw_mci_request(struct mmc_host * mmc,struct mmc_request * mrq)1415f95f3850SWill Newton static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1416f95f3850SWill Newton {
1417f95f3850SWill Newton 	struct dw_mci_slot *slot = mmc_priv(mmc);
1418f95f3850SWill Newton 	struct dw_mci *host = slot->host;
1419f95f3850SWill Newton 
1420f95f3850SWill Newton 	WARN_ON(slot->mrq);
1421f95f3850SWill Newton 
14227456caaeSJames Hogan 	/*
14237456caaeSJames Hogan 	 * The check for card presence and queueing of the request must be
14247456caaeSJames Hogan 	 * atomic, otherwise the card could be removed in between and the
14257456caaeSJames Hogan 	 * request wouldn't fail until another card was inserted.
14267456caaeSJames Hogan 	 */
14277456caaeSJames Hogan 
142856f6911cSShawn Lin 	if (!dw_mci_get_cd(mmc)) {
1429f95f3850SWill Newton 		mrq->cmd->error = -ENOMEDIUM;
1430f95f3850SWill Newton 		mmc_request_done(mmc, mrq);
1431f95f3850SWill Newton 		return;
1432f95f3850SWill Newton 	}
1433f95f3850SWill Newton 
143456f6911cSShawn Lin 	spin_lock_bh(&host->lock);
143556f6911cSShawn Lin 
1436f95f3850SWill Newton 	dw_mci_queue_request(host, slot, mrq);
14377456caaeSJames Hogan 
14387456caaeSJames Hogan 	spin_unlock_bh(&host->lock);
1439f95f3850SWill Newton }
1440f95f3850SWill Newton 
dw_mci_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)1441f95f3850SWill Newton static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1442f95f3850SWill Newton {
1443f95f3850SWill Newton 	struct dw_mci_slot *slot = mmc_priv(mmc);
1444e95baf13SArnd Bergmann 	const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
144541babf75SJaehoon Chung 	u32 regs;
144651da2240SYuvaraj CD 	int ret;
1447f95f3850SWill Newton 
1448f95f3850SWill Newton 	switch (ios->bus_width) {
1449f95f3850SWill Newton 	case MMC_BUS_WIDTH_4:
1450f95f3850SWill Newton 		slot->ctype = SDMMC_CTYPE_4BIT;
1451f95f3850SWill Newton 		break;
1452c9b2a06fSJaehoon Chung 	case MMC_BUS_WIDTH_8:
1453c9b2a06fSJaehoon Chung 		slot->ctype = SDMMC_CTYPE_8BIT;
1454c9b2a06fSJaehoon Chung 		break;
1455b2f7cb45SJaehoon Chung 	default:
1456b2f7cb45SJaehoon Chung 		/* set default 1 bit mode */
1457b2f7cb45SJaehoon Chung 		slot->ctype = SDMMC_CTYPE_1BIT;
1458f95f3850SWill Newton 	}
1459f95f3850SWill Newton 
146041babf75SJaehoon Chung 	regs = mci_readl(slot->host, UHS_REG);
14613f514291SSeungwon Jeon 
14623f514291SSeungwon Jeon 	/* DDR mode set */
146380113132SSeungwon Jeon 	if (ios->timing == MMC_TIMING_MMC_DDR52 ||
14647cc8d580SJaehoon Chung 	    ios->timing == MMC_TIMING_UHS_DDR50 ||
146580113132SSeungwon Jeon 	    ios->timing == MMC_TIMING_MMC_HS400)
1466c69042a5SHyeonsu Kim 		regs |= ((0x1 << slot->id) << 16);
14673f514291SSeungwon Jeon 	else
1468c69042a5SHyeonsu Kim 		regs &= ~((0x1 << slot->id) << 16);
14693f514291SSeungwon Jeon 
147041babf75SJaehoon Chung 	mci_writel(slot->host, UHS_REG, regs);
1471f1d2736cSSeungwon Jeon 	slot->host->timing = ios->timing;
147241babf75SJaehoon Chung 
1473f95f3850SWill Newton 	/*
1474f95f3850SWill Newton 	 * Use mirror of ios->clock to prevent race with mmc
1475f95f3850SWill Newton 	 * core ios update when finding the minimum.
1476f95f3850SWill Newton 	 */
1477f95f3850SWill Newton 	slot->clock = ios->clock;
1478f95f3850SWill Newton 
1479cb27a843SJames Hogan 	if (drv_data && drv_data->set_ios)
1480cb27a843SJames Hogan 		drv_data->set_ios(slot->host, ios);
1481800d78bfSThomas Abraham 
1482f95f3850SWill Newton 	switch (ios->power_mode) {
1483f95f3850SWill Newton 	case MMC_POWER_UP:
148451da2240SYuvaraj CD 		if (!IS_ERR(mmc->supply.vmmc)) {
148551da2240SYuvaraj CD 			ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
148651da2240SYuvaraj CD 					ios->vdd);
148751da2240SYuvaraj CD 			if (ret) {
148851da2240SYuvaraj CD 				dev_err(slot->host->dev,
148951da2240SYuvaraj CD 					"failed to enable vmmc regulator\n");
149051da2240SYuvaraj CD 				/*return, if failed turn on vmmc*/
149151da2240SYuvaraj CD 				return;
149251da2240SYuvaraj CD 			}
149351da2240SYuvaraj CD 		}
149429d0d161SDoug Anderson 		set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
149529d0d161SDoug Anderson 		regs = mci_readl(slot->host, PWREN);
149629d0d161SDoug Anderson 		regs |= (1 << slot->id);
149729d0d161SDoug Anderson 		mci_writel(slot->host, PWREN, regs);
149829d0d161SDoug Anderson 		break;
149929d0d161SDoug Anderson 	case MMC_POWER_ON:
1500d1f1dd86SDoug Anderson 		if (!slot->host->vqmmc_enabled) {
1501d1f1dd86SDoug Anderson 			if (!IS_ERR(mmc->supply.vqmmc)) {
150251da2240SYuvaraj CD 				ret = regulator_enable(mmc->supply.vqmmc);
150351da2240SYuvaraj CD 				if (ret < 0)
150451da2240SYuvaraj CD 					dev_err(slot->host->dev,
1505d1f1dd86SDoug Anderson 						"failed to enable vqmmc\n");
150651da2240SYuvaraj CD 				else
150751da2240SYuvaraj CD 					slot->host->vqmmc_enabled = true;
1508d1f1dd86SDoug Anderson 
1509d1f1dd86SDoug Anderson 			} else {
1510d1f1dd86SDoug Anderson 				/* Keep track so we don't reset again */
1511d1f1dd86SDoug Anderson 				slot->host->vqmmc_enabled = true;
1512d1f1dd86SDoug Anderson 			}
1513d1f1dd86SDoug Anderson 
1514d1f1dd86SDoug Anderson 			/* Reset our state machine after powering on */
1515d1f1dd86SDoug Anderson 			dw_mci_ctrl_reset(slot->host,
1516d1f1dd86SDoug Anderson 					  SDMMC_CTRL_ALL_RESET_FLAGS);
151751da2240SYuvaraj CD 		}
1518655babbdSDoug Anderson 
1519655babbdSDoug Anderson 		/* Adjust clock / bus width after power is up */
1520655babbdSDoug Anderson 		dw_mci_setup_bus(slot, false);
1521655babbdSDoug Anderson 
1522e6f34e2fSJames Hogan 		break;
1523e6f34e2fSJames Hogan 	case MMC_POWER_OFF:
1524655babbdSDoug Anderson 		/* Turn clock off before power goes down */
1525655babbdSDoug Anderson 		dw_mci_setup_bus(slot, false);
1526655babbdSDoug Anderson 
152751da2240SYuvaraj CD 		if (!IS_ERR(mmc->supply.vmmc))
152851da2240SYuvaraj CD 			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
152951da2240SYuvaraj CD 
1530d1f1dd86SDoug Anderson 		if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
153151da2240SYuvaraj CD 			regulator_disable(mmc->supply.vqmmc);
153251da2240SYuvaraj CD 		slot->host->vqmmc_enabled = false;
153351da2240SYuvaraj CD 
15344366dcc5SJaehoon Chung 		regs = mci_readl(slot->host, PWREN);
15354366dcc5SJaehoon Chung 		regs &= ~(1 << slot->id);
15364366dcc5SJaehoon Chung 		mci_writel(slot->host, PWREN, regs);
1537f95f3850SWill Newton 		break;
1538f95f3850SWill Newton 	default:
1539f95f3850SWill Newton 		break;
1540f95f3850SWill Newton 	}
1541655babbdSDoug Anderson 
1542655babbdSDoug Anderson 	if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1543655babbdSDoug Anderson 		slot->host->state = STATE_IDLE;
1544f95f3850SWill Newton }
1545f95f3850SWill Newton 
dw_mci_card_busy(struct mmc_host * mmc)154601730558SDoug Anderson static int dw_mci_card_busy(struct mmc_host *mmc)
154701730558SDoug Anderson {
154801730558SDoug Anderson 	struct dw_mci_slot *slot = mmc_priv(mmc);
154901730558SDoug Anderson 	u32 status;
155001730558SDoug Anderson 
155101730558SDoug Anderson 	/*
155201730558SDoug Anderson 	 * Check the busy bit which is low when DAT[3:0]
155301730558SDoug Anderson 	 * (the data lines) are 0000
155401730558SDoug Anderson 	 */
155501730558SDoug Anderson 	status = mci_readl(slot->host, STATUS);
155601730558SDoug Anderson 
155701730558SDoug Anderson 	return !!(status & SDMMC_STATUS_BUSY);
155801730558SDoug Anderson }
155901730558SDoug Anderson 
dw_mci_switch_voltage(struct mmc_host * mmc,struct mmc_ios * ios)156001730558SDoug Anderson static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
156101730558SDoug Anderson {
156201730558SDoug Anderson 	struct dw_mci_slot *slot = mmc_priv(mmc);
156301730558SDoug Anderson 	struct dw_mci *host = slot->host;
15648f7849c4SZhangfei Gao 	const struct dw_mci_drv_data *drv_data = host->drv_data;
156501730558SDoug Anderson 	u32 uhs;
156601730558SDoug Anderson 	u32 v18 = SDMMC_UHS_18V << slot->id;
156701730558SDoug Anderson 	int ret;
156801730558SDoug Anderson 
15698f7849c4SZhangfei Gao 	if (drv_data && drv_data->switch_voltage)
15708f7849c4SZhangfei Gao 		return drv_data->switch_voltage(mmc, ios);
15718f7849c4SZhangfei Gao 
157201730558SDoug Anderson 	/*
157301730558SDoug Anderson 	 * Program the voltage.  Note that some instances of dw_mmc may use
157401730558SDoug Anderson 	 * the UHS_REG for this.  For other instances (like exynos) the UHS_REG
157501730558SDoug Anderson 	 * does no harm but you need to set the regulator directly.  Try both.
157601730558SDoug Anderson 	 */
157701730558SDoug Anderson 	uhs = mci_readl(host, UHS_REG);
1578e0848f5dSDouglas Anderson 	if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
157901730558SDoug Anderson 		uhs &= ~v18;
1580e0848f5dSDouglas Anderson 	else
158101730558SDoug Anderson 		uhs |= v18;
1582e0848f5dSDouglas Anderson 
158301730558SDoug Anderson 	if (!IS_ERR(mmc->supply.vqmmc)) {
1584e0848f5dSDouglas Anderson 		ret = mmc_regulator_set_vqmmc(mmc, ios);
15859cbe0fc8SMarek Vasut 		if (ret < 0) {
1586b19caf37SDoug Anderson 			dev_dbg(&mmc->class_dev,
1587e0848f5dSDouglas Anderson 					 "Regulator set error %d - %s V\n",
1588e0848f5dSDouglas Anderson 					 ret, uhs & v18 ? "1.8" : "3.3");
158901730558SDoug Anderson 			return ret;
159001730558SDoug Anderson 		}
159101730558SDoug Anderson 	}
159201730558SDoug Anderson 	mci_writel(host, UHS_REG, uhs);
159301730558SDoug Anderson 
159401730558SDoug Anderson 	return 0;
159501730558SDoug Anderson }
159601730558SDoug Anderson 
dw_mci_get_ro(struct mmc_host * mmc)1597f95f3850SWill Newton static int dw_mci_get_ro(struct mmc_host *mmc)
1598f95f3850SWill Newton {
1599f95f3850SWill Newton 	int read_only;
1600f95f3850SWill Newton 	struct dw_mci_slot *slot = mmc_priv(mmc);
16019795a846SJaehoon Chung 	int gpio_ro = mmc_gpio_get_ro(mmc);
1602f95f3850SWill Newton 
1603f95f3850SWill Newton 	/* Use platform get_ro function, else try on board write protect */
1604287980e4SArnd Bergmann 	if (gpio_ro >= 0)
16059795a846SJaehoon Chung 		read_only = gpio_ro;
1606f95f3850SWill Newton 	else
1607f95f3850SWill Newton 		read_only =
1608f95f3850SWill Newton 			mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1609f95f3850SWill Newton 
1610f95f3850SWill Newton 	dev_dbg(&mmc->class_dev, "card is %s\n",
1611f95f3850SWill Newton 		read_only ? "read-only" : "read-write");
1612f95f3850SWill Newton 
1613f95f3850SWill Newton 	return read_only;
1614f95f3850SWill Newton }
1615f95f3850SWill Newton 
dw_mci_hw_reset(struct mmc_host * mmc)1616935a665eSShawn Lin static void dw_mci_hw_reset(struct mmc_host *mmc)
1617935a665eSShawn Lin {
1618935a665eSShawn Lin 	struct dw_mci_slot *slot = mmc_priv(mmc);
1619935a665eSShawn Lin 	struct dw_mci *host = slot->host;
1620935a665eSShawn Lin 	int reset;
1621935a665eSShawn Lin 
1622935a665eSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC)
1623935a665eSShawn Lin 		dw_mci_idmac_reset(host);
1624935a665eSShawn Lin 
1625935a665eSShawn Lin 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1626935a665eSShawn Lin 				     SDMMC_CTRL_FIFO_RESET))
1627935a665eSShawn Lin 		return;
1628935a665eSShawn Lin 
1629935a665eSShawn Lin 	/*
1630935a665eSShawn Lin 	 * According to eMMC spec, card reset procedure:
1631935a665eSShawn Lin 	 * tRstW >= 1us:   RST_n pulse width
1632935a665eSShawn Lin 	 * tRSCA >= 200us: RST_n to Command time
1633935a665eSShawn Lin 	 * tRSTH >= 1us:   RST_n high period
1634935a665eSShawn Lin 	 */
1635935a665eSShawn Lin 	reset = mci_readl(host, RST_N);
1636935a665eSShawn Lin 	reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1637935a665eSShawn Lin 	mci_writel(host, RST_N, reset);
1638935a665eSShawn Lin 	usleep_range(1, 2);
1639935a665eSShawn Lin 	reset |= SDMMC_RST_HWACTIVE << slot->id;
1640935a665eSShawn Lin 	mci_writel(host, RST_N, reset);
1641935a665eSShawn Lin 	usleep_range(200, 300);
1642935a665eSShawn Lin }
1643935a665eSShawn Lin 
dw_mci_prepare_sdio_irq(struct dw_mci_slot * slot,bool prepare)164461840edcSUlf Hansson static void dw_mci_prepare_sdio_irq(struct dw_mci_slot *slot, bool prepare)
1645b24c8b26SDoug Anderson {
1646b24c8b26SDoug Anderson 	struct dw_mci *host = slot->host;
164761840edcSUlf Hansson 	const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
164861840edcSUlf Hansson 	u32 clk_en_a_old;
164961840edcSUlf Hansson 	u32 clk_en_a;
1650b24c8b26SDoug Anderson 
16519623b5b9SDoug Anderson 	/*
16529623b5b9SDoug Anderson 	 * Low power mode will stop the card clock when idle.  According to the
16539623b5b9SDoug Anderson 	 * description of the CLKENA register we should disable low power mode
16549623b5b9SDoug Anderson 	 * for SDIO cards if we need SDIO interrupts to work.
16559623b5b9SDoug Anderson 	 */
16569623b5b9SDoug Anderson 
1657b24c8b26SDoug Anderson 	clk_en_a_old = mci_readl(host, CLKENA);
165861840edcSUlf Hansson 	if (prepare) {
1659b24c8b26SDoug Anderson 		set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1660b24c8b26SDoug Anderson 		clk_en_a = clk_en_a_old & ~clken_low_pwr;
1661b24c8b26SDoug Anderson 	} else {
1662b24c8b26SDoug Anderson 		clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1663b24c8b26SDoug Anderson 		clk_en_a = clk_en_a_old | clken_low_pwr;
1664b24c8b26SDoug Anderson 	}
1665b24c8b26SDoug Anderson 
1666b24c8b26SDoug Anderson 	if (clk_en_a != clk_en_a_old) {
1667b24c8b26SDoug Anderson 		mci_writel(host, CLKENA, clk_en_a);
166861840edcSUlf Hansson 		mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT,
166961840edcSUlf Hansson 			     0);
16709623b5b9SDoug Anderson 	}
1671b24c8b26SDoug Anderson }
16729623b5b9SDoug Anderson 
__dw_mci_enable_sdio_irq(struct dw_mci_slot * slot,int enb)167332dba737SUlf Hansson static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb)
16741a5c8e1fSShashidhar Hiremath {
16751a5c8e1fSShashidhar Hiremath 	struct dw_mci *host = slot->host;
1676f8c58c11SDoug Anderson 	unsigned long irqflags;
16771a5c8e1fSShashidhar Hiremath 	u32 int_mask;
16781a5c8e1fSShashidhar Hiremath 
1679f8c58c11SDoug Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
1680f8c58c11SDoug Anderson 
16811a5c8e1fSShashidhar Hiremath 	/* Enable/disable Slot Specific SDIO interrupt */
16821a5c8e1fSShashidhar Hiremath 	int_mask = mci_readl(host, INTMASK);
1683b24c8b26SDoug Anderson 	if (enb)
1684b24c8b26SDoug Anderson 		int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1685b24c8b26SDoug Anderson 	else
1686b24c8b26SDoug Anderson 		int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1687b24c8b26SDoug Anderson 	mci_writel(host, INTMASK, int_mask);
1688f8c58c11SDoug Anderson 
1689f8c58c11SDoug Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
16901a5c8e1fSShashidhar Hiremath }
16911a5c8e1fSShashidhar Hiremath 
dw_mci_enable_sdio_irq(struct mmc_host * mmc,int enb)169232dba737SUlf Hansson static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
169332dba737SUlf Hansson {
169432dba737SUlf Hansson 	struct dw_mci_slot *slot = mmc_priv(mmc);
1695ca8971caSUlf Hansson 	struct dw_mci *host = slot->host;
169632dba737SUlf Hansson 
169761840edcSUlf Hansson 	dw_mci_prepare_sdio_irq(slot, enb);
169832dba737SUlf Hansson 	__dw_mci_enable_sdio_irq(slot, enb);
1699ca8971caSUlf Hansson 
1700ca8971caSUlf Hansson 	/* Avoid runtime suspending the device when SDIO IRQ is enabled */
1701ca8971caSUlf Hansson 	if (enb)
1702ca8971caSUlf Hansson 		pm_runtime_get_noresume(host->dev);
1703ca8971caSUlf Hansson 	else
1704ca8971caSUlf Hansson 		pm_runtime_put_noidle(host->dev);
170532dba737SUlf Hansson }
170632dba737SUlf Hansson 
dw_mci_ack_sdio_irq(struct mmc_host * mmc)170732dba737SUlf Hansson static void dw_mci_ack_sdio_irq(struct mmc_host *mmc)
170832dba737SUlf Hansson {
170932dba737SUlf Hansson 	struct dw_mci_slot *slot = mmc_priv(mmc);
171032dba737SUlf Hansson 
171132dba737SUlf Hansson 	__dw_mci_enable_sdio_irq(slot, 1);
171232dba737SUlf Hansson }
171332dba737SUlf Hansson 
dw_mci_execute_tuning(struct mmc_host * mmc,u32 opcode)17140976f16dSSeungwon Jeon static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
17150976f16dSSeungwon Jeon {
17160976f16dSSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
17170976f16dSSeungwon Jeon 	struct dw_mci *host = slot->host;
17180976f16dSSeungwon Jeon 	const struct dw_mci_drv_data *drv_data = host->drv_data;
17190e3a22c0SShawn Lin 	int err = -EINVAL;
17200976f16dSSeungwon Jeon 
17210976f16dSSeungwon Jeon 	if (drv_data && drv_data->execute_tuning)
17229979dbe5SChaotian Jing 		err = drv_data->execute_tuning(slot, opcode);
17230976f16dSSeungwon Jeon 	return err;
17240976f16dSSeungwon Jeon }
17250976f16dSSeungwon Jeon 
dw_mci_prepare_hs400_tuning(struct mmc_host * mmc,struct mmc_ios * ios)17260e3a22c0SShawn Lin static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
17270e3a22c0SShawn Lin 				       struct mmc_ios *ios)
172880113132SSeungwon Jeon {
172980113132SSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
173080113132SSeungwon Jeon 	struct dw_mci *host = slot->host;
173180113132SSeungwon Jeon 	const struct dw_mci_drv_data *drv_data = host->drv_data;
173280113132SSeungwon Jeon 
173380113132SSeungwon Jeon 	if (drv_data && drv_data->prepare_hs400_tuning)
173480113132SSeungwon Jeon 		return drv_data->prepare_hs400_tuning(host, ios);
173580113132SSeungwon Jeon 
173680113132SSeungwon Jeon 	return 0;
173780113132SSeungwon Jeon }
173880113132SSeungwon Jeon 
dw_mci_reset(struct dw_mci * host)17394e7392b2SShawn Lin static bool dw_mci_reset(struct dw_mci *host)
17404e7392b2SShawn Lin {
17414e7392b2SShawn Lin 	u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
17424e7392b2SShawn Lin 	bool ret = false;
1743bc2dcc1aSShawn Lin 	u32 status = 0;
17444e7392b2SShawn Lin 
17454e7392b2SShawn Lin 	/*
17464e7392b2SShawn Lin 	 * Resetting generates a block interrupt, hence setting
17474e7392b2SShawn Lin 	 * the scatter-gather pointer to NULL.
17484e7392b2SShawn Lin 	 */
17494e7392b2SShawn Lin 	if (host->sg) {
17504e7392b2SShawn Lin 		sg_miter_stop(&host->sg_miter);
17514e7392b2SShawn Lin 		host->sg = NULL;
17524e7392b2SShawn Lin 	}
17534e7392b2SShawn Lin 
17544e7392b2SShawn Lin 	if (host->use_dma)
17554e7392b2SShawn Lin 		flags |= SDMMC_CTRL_DMA_RESET;
17564e7392b2SShawn Lin 
17574e7392b2SShawn Lin 	if (dw_mci_ctrl_reset(host, flags)) {
17584e7392b2SShawn Lin 		/*
1759bc2dcc1aSShawn Lin 		 * In all cases we clear the RAWINTS
1760bc2dcc1aSShawn Lin 		 * register to clear any interrupts.
17614e7392b2SShawn Lin 		 */
17624e7392b2SShawn Lin 		mci_writel(host, RINTSTS, 0xFFFFFFFF);
17634e7392b2SShawn Lin 
1764bc2dcc1aSShawn Lin 		if (!host->use_dma) {
1765bc2dcc1aSShawn Lin 			ret = true;
1766bc2dcc1aSShawn Lin 			goto ciu_out;
1767bc2dcc1aSShawn Lin 		}
17684e7392b2SShawn Lin 
1769bc2dcc1aSShawn Lin 		/* Wait for dma_req to be cleared */
17704e7392b2SShawn Lin 		if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
17714e7392b2SShawn Lin 					      status,
17724e7392b2SShawn Lin 					      !(status & SDMMC_STATUS_DMA_REQ),
17734e7392b2SShawn Lin 					      1, 500 * USEC_PER_MSEC)) {
17744e7392b2SShawn Lin 			dev_err(host->dev,
1775bc2dcc1aSShawn Lin 				"%s: Timeout waiting for dma_req to be cleared\n",
17764e7392b2SShawn Lin 				__func__);
17774e7392b2SShawn Lin 			goto ciu_out;
17784e7392b2SShawn Lin 		}
17794e7392b2SShawn Lin 
17804e7392b2SShawn Lin 		/* when using DMA next we reset the fifo again */
17814e7392b2SShawn Lin 		if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
17824e7392b2SShawn Lin 			goto ciu_out;
17834e7392b2SShawn Lin 	} else {
17844e7392b2SShawn Lin 		/* if the controller reset bit did clear, then set clock regs */
17854e7392b2SShawn Lin 		if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
17864e7392b2SShawn Lin 			dev_err(host->dev,
17874e7392b2SShawn Lin 				"%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
17884e7392b2SShawn Lin 				__func__);
17894e7392b2SShawn Lin 			goto ciu_out;
17904e7392b2SShawn Lin 		}
17914e7392b2SShawn Lin 	}
17924e7392b2SShawn Lin 
17934e7392b2SShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC)
179447b7de2fSEvgeniy Didin 		/* It is also required that we reinit idmac */
179547b7de2fSEvgeniy Didin 		dw_mci_idmac_init(host);
17964e7392b2SShawn Lin 
17974e7392b2SShawn Lin 	ret = true;
17984e7392b2SShawn Lin 
17994e7392b2SShawn Lin ciu_out:
18004e7392b2SShawn Lin 	/* After a CTRL reset we need to have CIU set clock registers  */
180142f989c0SJaehoon Chung 	mci_send_cmd(host->slot, SDMMC_CMD_UPD_CLK, 0);
18024e7392b2SShawn Lin 
18034e7392b2SShawn Lin 	return ret;
18044e7392b2SShawn Lin }
18054e7392b2SShawn Lin 
1806f95f3850SWill Newton static const struct mmc_host_ops dw_mci_ops = {
1807f95f3850SWill Newton 	.request		= dw_mci_request,
18089aa51408SSeungwon Jeon 	.pre_req		= dw_mci_pre_req,
18099aa51408SSeungwon Jeon 	.post_req		= dw_mci_post_req,
1810f95f3850SWill Newton 	.set_ios		= dw_mci_set_ios,
1811f95f3850SWill Newton 	.get_ro			= dw_mci_get_ro,
1812f95f3850SWill Newton 	.get_cd			= dw_mci_get_cd,
181332f18e59SWolfram Sang 	.card_hw_reset          = dw_mci_hw_reset,
18141a5c8e1fSShashidhar Hiremath 	.enable_sdio_irq	= dw_mci_enable_sdio_irq,
181532dba737SUlf Hansson 	.ack_sdio_irq		= dw_mci_ack_sdio_irq,
18160976f16dSSeungwon Jeon 	.execute_tuning		= dw_mci_execute_tuning,
181701730558SDoug Anderson 	.card_busy		= dw_mci_card_busy,
181801730558SDoug Anderson 	.start_signal_voltage_switch = dw_mci_switch_voltage,
181980113132SSeungwon Jeon 	.prepare_hs400_tuning	= dw_mci_prepare_hs400_tuning,
1820f95f3850SWill Newton };
1821f95f3850SWill Newton 
18222b8ac062SVincent Whitchurch #ifdef CONFIG_FAULT_INJECTION
dw_mci_fault_timer(struct hrtimer * t)18232b8ac062SVincent Whitchurch static enum hrtimer_restart dw_mci_fault_timer(struct hrtimer *t)
18242b8ac062SVincent Whitchurch {
18252b8ac062SVincent Whitchurch 	struct dw_mci *host = container_of(t, struct dw_mci, fault_timer);
18262b8ac062SVincent Whitchurch 	unsigned long flags;
18272b8ac062SVincent Whitchurch 
18282b8ac062SVincent Whitchurch 	spin_lock_irqsave(&host->irq_lock, flags);
18292b8ac062SVincent Whitchurch 
183026391e49SVincent Whitchurch 	/*
183126391e49SVincent Whitchurch 	 * Only inject an error if we haven't already got an error or data over
183226391e49SVincent Whitchurch 	 * interrupt.
183326391e49SVincent Whitchurch 	 */
183426391e49SVincent Whitchurch 	if (!host->data_status) {
18352b8ac062SVincent Whitchurch 		host->data_status = SDMMC_INT_DCRC;
18362b8ac062SVincent Whitchurch 		set_bit(EVENT_DATA_ERROR, &host->pending_events);
18372b8ac062SVincent Whitchurch 		tasklet_schedule(&host->tasklet);
183826391e49SVincent Whitchurch 	}
18392b8ac062SVincent Whitchurch 
18402b8ac062SVincent Whitchurch 	spin_unlock_irqrestore(&host->irq_lock, flags);
18412b8ac062SVincent Whitchurch 
18422b8ac062SVincent Whitchurch 	return HRTIMER_NORESTART;
18432b8ac062SVincent Whitchurch }
18442b8ac062SVincent Whitchurch 
dw_mci_start_fault_timer(struct dw_mci * host)18452b8ac062SVincent Whitchurch static void dw_mci_start_fault_timer(struct dw_mci *host)
18462b8ac062SVincent Whitchurch {
18472b8ac062SVincent Whitchurch 	struct mmc_data *data = host->data;
18482b8ac062SVincent Whitchurch 
18492b8ac062SVincent Whitchurch 	if (!data || data->blocks <= 1)
18502b8ac062SVincent Whitchurch 		return;
18512b8ac062SVincent Whitchurch 
18522b8ac062SVincent Whitchurch 	if (!should_fail(&host->fail_data_crc, 1))
18532b8ac062SVincent Whitchurch 		return;
18542b8ac062SVincent Whitchurch 
18552b8ac062SVincent Whitchurch 	/*
18562b8ac062SVincent Whitchurch 	 * Try to inject the error at random points during the data transfer.
18572b8ac062SVincent Whitchurch 	 */
18582b8ac062SVincent Whitchurch 	hrtimer_start(&host->fault_timer,
18598032bf12SJason A. Donenfeld 		      ms_to_ktime(get_random_u32_below(25)),
18602b8ac062SVincent Whitchurch 		      HRTIMER_MODE_REL);
18612b8ac062SVincent Whitchurch }
18622b8ac062SVincent Whitchurch 
dw_mci_stop_fault_timer(struct dw_mci * host)18632b8ac062SVincent Whitchurch static void dw_mci_stop_fault_timer(struct dw_mci *host)
18642b8ac062SVincent Whitchurch {
18652b8ac062SVincent Whitchurch 	hrtimer_cancel(&host->fault_timer);
18662b8ac062SVincent Whitchurch }
18672b8ac062SVincent Whitchurch 
dw_mci_init_fault(struct dw_mci * host)18682b8ac062SVincent Whitchurch static void dw_mci_init_fault(struct dw_mci *host)
18692b8ac062SVincent Whitchurch {
18702b8ac062SVincent Whitchurch 	host->fail_data_crc = (struct fault_attr) FAULT_ATTR_INITIALIZER;
18712b8ac062SVincent Whitchurch 
18722b8ac062SVincent Whitchurch 	hrtimer_init(&host->fault_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
18732b8ac062SVincent Whitchurch 	host->fault_timer.function = dw_mci_fault_timer;
18742b8ac062SVincent Whitchurch }
18752b8ac062SVincent Whitchurch #else
dw_mci_init_fault(struct dw_mci * host)18762b8ac062SVincent Whitchurch static void dw_mci_init_fault(struct dw_mci *host)
18772b8ac062SVincent Whitchurch {
18782b8ac062SVincent Whitchurch }
18792b8ac062SVincent Whitchurch 
dw_mci_start_fault_timer(struct dw_mci * host)18802b8ac062SVincent Whitchurch static void dw_mci_start_fault_timer(struct dw_mci *host)
18812b8ac062SVincent Whitchurch {
18822b8ac062SVincent Whitchurch }
18832b8ac062SVincent Whitchurch 
dw_mci_stop_fault_timer(struct dw_mci * host)18842b8ac062SVincent Whitchurch static void dw_mci_stop_fault_timer(struct dw_mci *host)
18852b8ac062SVincent Whitchurch {
18862b8ac062SVincent Whitchurch }
18872b8ac062SVincent Whitchurch #endif
18882b8ac062SVincent Whitchurch 
dw_mci_request_end(struct dw_mci * host,struct mmc_request * mrq)1889f95f3850SWill Newton static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1890f95f3850SWill Newton 	__releases(&host->lock)
1891f95f3850SWill Newton 	__acquires(&host->lock)
1892f95f3850SWill Newton {
1893f95f3850SWill Newton 	struct dw_mci_slot *slot;
189442f989c0SJaehoon Chung 	struct mmc_host	*prev_mmc = host->slot->mmc;
1895f95f3850SWill Newton 
1896f95f3850SWill Newton 	WARN_ON(host->cmd || host->data);
1897f95f3850SWill Newton 
189842f989c0SJaehoon Chung 	host->slot->mrq = NULL;
1899f95f3850SWill Newton 	host->mrq = NULL;
1900f95f3850SWill Newton 	if (!list_empty(&host->queue)) {
1901f95f3850SWill Newton 		slot = list_entry(host->queue.next,
1902f95f3850SWill Newton 				  struct dw_mci_slot, queue_node);
1903f95f3850SWill Newton 		list_del(&slot->queue_node);
19044a90920cSThomas Abraham 		dev_vdbg(host->dev, "list not empty: %s is next\n",
1905f95f3850SWill Newton 			 mmc_hostname(slot->mmc));
1906f95f3850SWill Newton 		host->state = STATE_SENDING_CMD;
1907f95f3850SWill Newton 		dw_mci_start_request(host, slot);
1908f95f3850SWill Newton 	} else {
19094a90920cSThomas Abraham 		dev_vdbg(host->dev, "list empty\n");
191001730558SDoug Anderson 
191101730558SDoug Anderson 		if (host->state == STATE_SENDING_CMD11)
191201730558SDoug Anderson 			host->state = STATE_WAITING_CMD11_DONE;
191301730558SDoug Anderson 		else
1914f95f3850SWill Newton 			host->state = STATE_IDLE;
1915f95f3850SWill Newton 	}
1916f95f3850SWill Newton 
1917f95f3850SWill Newton 	spin_unlock(&host->lock);
1918f95f3850SWill Newton 	mmc_request_done(prev_mmc, mrq);
1919f95f3850SWill Newton 	spin_lock(&host->lock);
1920f95f3850SWill Newton }
1921f95f3850SWill Newton 
dw_mci_command_complete(struct dw_mci * host,struct mmc_command * cmd)1922e352c813SSeungwon Jeon static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1923f95f3850SWill Newton {
1924f95f3850SWill Newton 	u32 status = host->cmd_status;
1925f95f3850SWill Newton 
1926f95f3850SWill Newton 	host->cmd_status = 0;
1927f95f3850SWill Newton 
1928f95f3850SWill Newton 	/* Read the response from the card (up to 16 bytes) */
1929f95f3850SWill Newton 	if (cmd->flags & MMC_RSP_PRESENT) {
1930f95f3850SWill Newton 		if (cmd->flags & MMC_RSP_136) {
1931f95f3850SWill Newton 			cmd->resp[3] = mci_readl(host, RESP0);
1932f95f3850SWill Newton 			cmd->resp[2] = mci_readl(host, RESP1);
1933f95f3850SWill Newton 			cmd->resp[1] = mci_readl(host, RESP2);
1934f95f3850SWill Newton 			cmd->resp[0] = mci_readl(host, RESP3);
1935f95f3850SWill Newton 		} else {
1936f95f3850SWill Newton 			cmd->resp[0] = mci_readl(host, RESP0);
1937f95f3850SWill Newton 			cmd->resp[1] = 0;
1938f95f3850SWill Newton 			cmd->resp[2] = 0;
1939f95f3850SWill Newton 			cmd->resp[3] = 0;
1940f95f3850SWill Newton 		}
1941f95f3850SWill Newton 	}
1942f95f3850SWill Newton 
1943f95f3850SWill Newton 	if (status & SDMMC_INT_RTO)
1944f95f3850SWill Newton 		cmd->error = -ETIMEDOUT;
1945f95f3850SWill Newton 	else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1946f95f3850SWill Newton 		cmd->error = -EILSEQ;
1947f95f3850SWill Newton 	else if (status & SDMMC_INT_RESP_ERR)
1948f95f3850SWill Newton 		cmd->error = -EIO;
1949f95f3850SWill Newton 	else
1950f95f3850SWill Newton 		cmd->error = 0;
1951f95f3850SWill Newton 
1952e352c813SSeungwon Jeon 	return cmd->error;
1953e352c813SSeungwon Jeon }
1954e352c813SSeungwon Jeon 
dw_mci_data_complete(struct dw_mci * host,struct mmc_data * data)1955e352c813SSeungwon Jeon static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1956e352c813SSeungwon Jeon {
195731bff450SSeungwon Jeon 	u32 status = host->data_status;
1958e352c813SSeungwon Jeon 
1959e352c813SSeungwon Jeon 	if (status & DW_MCI_DATA_ERROR_FLAGS) {
1960e352c813SSeungwon Jeon 		if (status & SDMMC_INT_DRTO) {
1961e352c813SSeungwon Jeon 			data->error = -ETIMEDOUT;
1962e352c813SSeungwon Jeon 		} else if (status & SDMMC_INT_DCRC) {
1963e352c813SSeungwon Jeon 			data->error = -EILSEQ;
1964e352c813SSeungwon Jeon 		} else if (status & SDMMC_INT_EBE) {
1965e352c813SSeungwon Jeon 			if (host->dir_status ==
1966e352c813SSeungwon Jeon 				DW_MCI_SEND_STATUS) {
1967e352c813SSeungwon Jeon 				/*
1968e352c813SSeungwon Jeon 				 * No data CRC status was returned.
1969e352c813SSeungwon Jeon 				 * The number of bytes transferred
1970e352c813SSeungwon Jeon 				 * will be exaggerated in PIO mode.
1971e352c813SSeungwon Jeon 				 */
1972e352c813SSeungwon Jeon 				data->bytes_xfered = 0;
1973e352c813SSeungwon Jeon 				data->error = -ETIMEDOUT;
1974e352c813SSeungwon Jeon 			} else if (host->dir_status ==
1975e352c813SSeungwon Jeon 					DW_MCI_RECV_STATUS) {
1976e7a1dec1SShawn Lin 				data->error = -EILSEQ;
1977e352c813SSeungwon Jeon 			}
1978e352c813SSeungwon Jeon 		} else {
1979e352c813SSeungwon Jeon 			/* SDMMC_INT_SBE is included */
1980e7a1dec1SShawn Lin 			data->error = -EILSEQ;
1981e352c813SSeungwon Jeon 		}
1982e352c813SSeungwon Jeon 
1983e6cc0123SDoug Anderson 		dev_dbg(host->dev, "data error, status 0x%08x\n", status);
1984e352c813SSeungwon Jeon 
1985e352c813SSeungwon Jeon 		/*
1986e352c813SSeungwon Jeon 		 * After an error, there may be data lingering
198731bff450SSeungwon Jeon 		 * in the FIFO
1988e352c813SSeungwon Jeon 		 */
19893a33a94cSSonny Rao 		dw_mci_reset(host);
1990e352c813SSeungwon Jeon 	} else {
1991e352c813SSeungwon Jeon 		data->bytes_xfered = data->blocks * data->blksz;
1992e352c813SSeungwon Jeon 		data->error = 0;
1993e352c813SSeungwon Jeon 	}
1994e352c813SSeungwon Jeon 
1995e352c813SSeungwon Jeon 	return data->error;
1996f95f3850SWill Newton }
1997f95f3850SWill Newton 
dw_mci_set_drto(struct dw_mci * host)199857e10486SAddy Ke static void dw_mci_set_drto(struct dw_mci *host)
199957e10486SAddy Ke {
200025d5417aSMårten Lindahl 	const struct dw_mci_drv_data *drv_data = host->drv_data;
200157e10486SAddy Ke 	unsigned int drto_clks;
20029d9491a7SDouglas Anderson 	unsigned int drto_div;
200357e10486SAddy Ke 	unsigned int drto_ms;
200493c23ae3SDouglas Anderson 	unsigned long irqflags;
200557e10486SAddy Ke 
200625d5417aSMårten Lindahl 	if (drv_data && drv_data->get_drto_clks)
200725d5417aSMårten Lindahl 		drto_clks = drv_data->get_drto_clks(host);
200825d5417aSMårten Lindahl 	else
200957e10486SAddy Ke 		drto_clks = mci_readl(host, TMOUT) >> 8;
20109d9491a7SDouglas Anderson 	drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
20119d9491a7SDouglas Anderson 	if (drto_div == 0)
20129d9491a7SDouglas Anderson 		drto_div = 1;
2013c7151602SEvgeniy Didin 
2014c7151602SEvgeniy Didin 	drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div,
20159d9491a7SDouglas Anderson 				   host->bus_hz);
201657e10486SAddy Ke 
201725d5417aSMårten Lindahl 	dev_dbg(host->dev, "drto_ms: %u\n", drto_ms);
201825d5417aSMårten Lindahl 
201957e10486SAddy Ke 	/* add a bit spare time */
202057e10486SAddy Ke 	drto_ms += 10;
202157e10486SAddy Ke 
202293c23ae3SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
202393c23ae3SDouglas Anderson 	if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
202493c23ae3SDouglas Anderson 		mod_timer(&host->dto_timer,
202593c23ae3SDouglas Anderson 			  jiffies + msecs_to_jiffies(drto_ms));
202693c23ae3SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
202757e10486SAddy Ke }
202857e10486SAddy Ke 
dw_mci_clear_pending_cmd_complete(struct dw_mci * host)20298892b705SDouglas Anderson static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host)
20308892b705SDouglas Anderson {
20318892b705SDouglas Anderson 	if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
20328892b705SDouglas Anderson 		return false;
20338892b705SDouglas Anderson 
20348892b705SDouglas Anderson 	/*
20358892b705SDouglas Anderson 	 * Really be certain that the timer has stopped.  This is a bit of
20368892b705SDouglas Anderson 	 * paranoia and could only really happen if we had really bad
20378892b705SDouglas Anderson 	 * interrupt latency and the interrupt routine and timeout were
20388892b705SDouglas Anderson 	 * running concurrently so that the del_timer() in the interrupt
20398892b705SDouglas Anderson 	 * handler couldn't run.
20408892b705SDouglas Anderson 	 */
20418892b705SDouglas Anderson 	WARN_ON(del_timer_sync(&host->cto_timer));
20428892b705SDouglas Anderson 	clear_bit(EVENT_CMD_COMPLETE, &host->pending_events);
20438892b705SDouglas Anderson 
20448892b705SDouglas Anderson 	return true;
20458892b705SDouglas Anderson }
20468892b705SDouglas Anderson 
dw_mci_clear_pending_data_complete(struct dw_mci * host)204793c23ae3SDouglas Anderson static bool dw_mci_clear_pending_data_complete(struct dw_mci *host)
204893c23ae3SDouglas Anderson {
204993c23ae3SDouglas Anderson 	if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
205093c23ae3SDouglas Anderson 		return false;
205193c23ae3SDouglas Anderson 
205293c23ae3SDouglas Anderson 	/* Extra paranoia just like dw_mci_clear_pending_cmd_complete() */
205393c23ae3SDouglas Anderson 	WARN_ON(del_timer_sync(&host->dto_timer));
205493c23ae3SDouglas Anderson 	clear_bit(EVENT_DATA_COMPLETE, &host->pending_events);
205593c23ae3SDouglas Anderson 
205693c23ae3SDouglas Anderson 	return true;
205793c23ae3SDouglas Anderson }
205893c23ae3SDouglas Anderson 
dw_mci_tasklet_func(struct tasklet_struct * t)20596078df15SEmil Renner Berthing static void dw_mci_tasklet_func(struct tasklet_struct *t)
2060f95f3850SWill Newton {
20616078df15SEmil Renner Berthing 	struct dw_mci *host = from_tasklet(host, t, tasklet);
2062f95f3850SWill Newton 	struct mmc_data	*data;
2063f95f3850SWill Newton 	struct mmc_command *cmd;
2064e352c813SSeungwon Jeon 	struct mmc_request *mrq;
2065f95f3850SWill Newton 	enum dw_mci_state state;
2066f95f3850SWill Newton 	enum dw_mci_state prev_state;
2067e352c813SSeungwon Jeon 	unsigned int err;
2068f95f3850SWill Newton 
2069f95f3850SWill Newton 	spin_lock(&host->lock);
2070f95f3850SWill Newton 
2071f95f3850SWill Newton 	state = host->state;
2072f95f3850SWill Newton 	data = host->data;
2073e352c813SSeungwon Jeon 	mrq = host->mrq;
2074f95f3850SWill Newton 
2075f95f3850SWill Newton 	do {
2076f95f3850SWill Newton 		prev_state = state;
2077f95f3850SWill Newton 
2078f95f3850SWill Newton 		switch (state) {
2079f95f3850SWill Newton 		case STATE_IDLE:
208001730558SDoug Anderson 		case STATE_WAITING_CMD11_DONE:
2081f95f3850SWill Newton 			break;
2082f95f3850SWill Newton 
208301730558SDoug Anderson 		case STATE_SENDING_CMD11:
2084f95f3850SWill Newton 		case STATE_SENDING_CMD:
20858892b705SDouglas Anderson 			if (!dw_mci_clear_pending_cmd_complete(host))
2086f95f3850SWill Newton 				break;
2087f95f3850SWill Newton 
2088f95f3850SWill Newton 			cmd = host->cmd;
2089f95f3850SWill Newton 			host->cmd = NULL;
2090f95f3850SWill Newton 			set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
2091e352c813SSeungwon Jeon 			err = dw_mci_command_complete(host, cmd);
2092e352c813SSeungwon Jeon 			if (cmd == mrq->sbc && !err) {
209342f989c0SJaehoon Chung 				__dw_mci_start_request(host, host->slot,
2094e352c813SSeungwon Jeon 						       mrq->cmd);
2095053b3ce6SSeungwon Jeon 				goto unlock;
2096053b3ce6SSeungwon Jeon 			}
2097053b3ce6SSeungwon Jeon 
2098e352c813SSeungwon Jeon 			if (cmd->data && err) {
209946d17952SDoug Anderson 				/*
210046d17952SDoug Anderson 				 * During UHS tuning sequence, sending the stop
210146d17952SDoug Anderson 				 * command after the response CRC error would
210246d17952SDoug Anderson 				 * throw the system into a confused state
210346d17952SDoug Anderson 				 * causing all future tuning phases to report
210446d17952SDoug Anderson 				 * failure.
210546d17952SDoug Anderson 				 *
210646d17952SDoug Anderson 				 * In such case controller will move into a data
210746d17952SDoug Anderson 				 * transfer state after a response error or
210846d17952SDoug Anderson 				 * response CRC error. Let's let that finish
210946d17952SDoug Anderson 				 * before trying to send a stop, so we'll go to
211046d17952SDoug Anderson 				 * STATE_SENDING_DATA.
211146d17952SDoug Anderson 				 *
211246d17952SDoug Anderson 				 * Although letting the data transfer take place
211346d17952SDoug Anderson 				 * will waste a bit of time (we already know
211446d17952SDoug Anderson 				 * the command was bad), it can't cause any
211546d17952SDoug Anderson 				 * errors since it's possible it would have
211646d17952SDoug Anderson 				 * taken place anyway if this tasklet got
211746d17952SDoug Anderson 				 * delayed. Allowing the transfer to take place
211846d17952SDoug Anderson 				 * avoids races and keeps things simple.
211946d17952SDoug Anderson 				 */
212043592c87SChristian Löhle 				if (err != -ETIMEDOUT &&
212143592c87SChristian Löhle 				    host->dir_status == DW_MCI_RECV_STATUS) {
212246d17952SDoug Anderson 					state = STATE_SENDING_DATA;
212346d17952SDoug Anderson 					continue;
212446d17952SDoug Anderson 				}
212546d17952SDoug Anderson 
212690c2143aSSeungwon Jeon 				send_stop_abort(host, data);
212725f8203bSVincent Whitchurch 				dw_mci_stop_dma(host);
212871abb133SSeungwon Jeon 				state = STATE_SENDING_STOP;
212971abb133SSeungwon Jeon 				break;
213071abb133SSeungwon Jeon 			}
213171abb133SSeungwon Jeon 
2132e352c813SSeungwon Jeon 			if (!cmd->data || err) {
2133e352c813SSeungwon Jeon 				dw_mci_request_end(host, mrq);
2134f95f3850SWill Newton 				goto unlock;
2135f95f3850SWill Newton 			}
2136f95f3850SWill Newton 
2137f95f3850SWill Newton 			prev_state = state = STATE_SENDING_DATA;
2138df561f66SGustavo A. R. Silva 			fallthrough;
2139f95f3850SWill Newton 
2140f95f3850SWill Newton 		case STATE_SENDING_DATA:
21412aa35465SDoug Anderson 			/*
21422aa35465SDoug Anderson 			 * We could get a data error and never a transfer
21432aa35465SDoug Anderson 			 * complete so we'd better check for it here.
21442aa35465SDoug Anderson 			 *
21452aa35465SDoug Anderson 			 * Note that we don't really care if we also got a
21462aa35465SDoug Anderson 			 * transfer complete; stopping the DMA and sending an
21472aa35465SDoug Anderson 			 * abort won't hurt.
21482aa35465SDoug Anderson 			 */
2149f95f3850SWill Newton 			if (test_and_clear_bit(EVENT_DATA_ERROR,
2150f95f3850SWill Newton 					       &host->pending_events)) {
2151e13c3c08SJaehoon Chung 				if (!(host->data_status & (SDMMC_INT_DRTO |
2152bdb9a90bSaddy ke 							   SDMMC_INT_EBE)))
215390c2143aSSeungwon Jeon 					send_stop_abort(host, data);
215425f8203bSVincent Whitchurch 				dw_mci_stop_dma(host);
2155f95f3850SWill Newton 				state = STATE_DATA_ERROR;
2156f95f3850SWill Newton 				break;
2157f95f3850SWill Newton 			}
2158f95f3850SWill Newton 
2159f95f3850SWill Newton 			if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
216057e10486SAddy Ke 						&host->pending_events)) {
216157e10486SAddy Ke 				/*
216257e10486SAddy Ke 				 * If all data-related interrupts don't come
216357e10486SAddy Ke 				 * within the given time in reading data state.
216457e10486SAddy Ke 				 */
216516a34574SJaehoon Chung 				if (host->dir_status == DW_MCI_RECV_STATUS)
216657e10486SAddy Ke 					dw_mci_set_drto(host);
2167f95f3850SWill Newton 				break;
216857e10486SAddy Ke 			}
2169f95f3850SWill Newton 
2170f95f3850SWill Newton 			set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
21712aa35465SDoug Anderson 
21722aa35465SDoug Anderson 			/*
21732aa35465SDoug Anderson 			 * Handle an EVENT_DATA_ERROR that might have shown up
21742aa35465SDoug Anderson 			 * before the transfer completed.  This might not have
21752aa35465SDoug Anderson 			 * been caught by the check above because the interrupt
21762aa35465SDoug Anderson 			 * could have gone off between the previous check and
21772aa35465SDoug Anderson 			 * the check for transfer complete.
21782aa35465SDoug Anderson 			 *
21792aa35465SDoug Anderson 			 * Technically this ought not be needed assuming we
21802aa35465SDoug Anderson 			 * get a DATA_COMPLETE eventually (we'll notice the
21812aa35465SDoug Anderson 			 * error and end the request), but it shouldn't hurt.
21822aa35465SDoug Anderson 			 *
21832aa35465SDoug Anderson 			 * This has the advantage of sending the stop command.
21842aa35465SDoug Anderson 			 */
21852aa35465SDoug Anderson 			if (test_and_clear_bit(EVENT_DATA_ERROR,
21862aa35465SDoug Anderson 					       &host->pending_events)) {
2187e13c3c08SJaehoon Chung 				if (!(host->data_status & (SDMMC_INT_DRTO |
2188bdb9a90bSaddy ke 							   SDMMC_INT_EBE)))
21892aa35465SDoug Anderson 					send_stop_abort(host, data);
219025f8203bSVincent Whitchurch 				dw_mci_stop_dma(host);
21912aa35465SDoug Anderson 				state = STATE_DATA_ERROR;
21922aa35465SDoug Anderson 				break;
21932aa35465SDoug Anderson 			}
2194f95f3850SWill Newton 			prev_state = state = STATE_DATA_BUSY;
21952aa35465SDoug Anderson 
2196df561f66SGustavo A. R. Silva 			fallthrough;
2197f95f3850SWill Newton 
2198f95f3850SWill Newton 		case STATE_DATA_BUSY:
219993c23ae3SDouglas Anderson 			if (!dw_mci_clear_pending_data_complete(host)) {
220057e10486SAddy Ke 				/*
220157e10486SAddy Ke 				 * If data error interrupt comes but data over
220257e10486SAddy Ke 				 * interrupt doesn't come within the given time.
220357e10486SAddy Ke 				 * in reading data state.
220457e10486SAddy Ke 				 */
220516a34574SJaehoon Chung 				if (host->dir_status == DW_MCI_RECV_STATUS)
220657e10486SAddy Ke 					dw_mci_set_drto(host);
2207f95f3850SWill Newton 				break;
220857e10486SAddy Ke 			}
2209f95f3850SWill Newton 
22102b8ac062SVincent Whitchurch 			dw_mci_stop_fault_timer(host);
2211f95f3850SWill Newton 			host->data = NULL;
2212f95f3850SWill Newton 			set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2213e352c813SSeungwon Jeon 			err = dw_mci_data_complete(host, data);
2214f95f3850SWill Newton 
2215e352c813SSeungwon Jeon 			if (!err) {
2216e352c813SSeungwon Jeon 				if (!data->stop || mrq->sbc) {
221717c8bc85SSachin Kamat 					if (mrq->sbc && data->stop)
2218053b3ce6SSeungwon Jeon 						data->stop->error = 0;
2219e352c813SSeungwon Jeon 					dw_mci_request_end(host, mrq);
2220053b3ce6SSeungwon Jeon 					goto unlock;
2221053b3ce6SSeungwon Jeon 				}
2222053b3ce6SSeungwon Jeon 
222390c2143aSSeungwon Jeon 				/* stop command for open-ended transfer*/
2224e352c813SSeungwon Jeon 				if (data->stop)
222590c2143aSSeungwon Jeon 					send_stop_abort(host, data);
22262aa35465SDoug Anderson 			} else {
22272aa35465SDoug Anderson 				/*
22282aa35465SDoug Anderson 				 * If we don't have a command complete now we'll
22292aa35465SDoug Anderson 				 * never get one since we just reset everything;
22302aa35465SDoug Anderson 				 * better end the request.
22312aa35465SDoug Anderson 				 *
22322aa35465SDoug Anderson 				 * If we do have a command complete we'll fall
22332aa35465SDoug Anderson 				 * through to the SENDING_STOP command and
22342aa35465SDoug Anderson 				 * everything will be peachy keen.
22352aa35465SDoug Anderson 				 */
22362aa35465SDoug Anderson 				if (!test_bit(EVENT_CMD_COMPLETE,
22372aa35465SDoug Anderson 					      &host->pending_events)) {
22382aa35465SDoug Anderson 					host->cmd = NULL;
22392aa35465SDoug Anderson 					dw_mci_request_end(host, mrq);
22402aa35465SDoug Anderson 					goto unlock;
22412aa35465SDoug Anderson 				}
224290c2143aSSeungwon Jeon 			}
2243e352c813SSeungwon Jeon 
2244e352c813SSeungwon Jeon 			/*
2245e352c813SSeungwon Jeon 			 * If err has non-zero,
2246e352c813SSeungwon Jeon 			 * stop-abort command has been already issued.
2247e352c813SSeungwon Jeon 			 */
2248e352c813SSeungwon Jeon 			prev_state = state = STATE_SENDING_STOP;
2249e352c813SSeungwon Jeon 
2250df561f66SGustavo A. R. Silva 			fallthrough;
2251f95f3850SWill Newton 
2252f95f3850SWill Newton 		case STATE_SENDING_STOP:
22538892b705SDouglas Anderson 			if (!dw_mci_clear_pending_cmd_complete(host))
2254f95f3850SWill Newton 				break;
2255f95f3850SWill Newton 
225671abb133SSeungwon Jeon 			/* CMD error in data command */
225731bff450SSeungwon Jeon 			if (mrq->cmd->error && mrq->data)
22583a33a94cSSonny Rao 				dw_mci_reset(host);
225971abb133SSeungwon Jeon 
22602b8ac062SVincent Whitchurch 			dw_mci_stop_fault_timer(host);
2261f95f3850SWill Newton 			host->cmd = NULL;
226271abb133SSeungwon Jeon 			host->data = NULL;
226390c2143aSSeungwon Jeon 
2264e13c3c08SJaehoon Chung 			if (!mrq->sbc && mrq->stop)
2265e352c813SSeungwon Jeon 				dw_mci_command_complete(host, mrq->stop);
226690c2143aSSeungwon Jeon 			else
226790c2143aSSeungwon Jeon 				host->cmd_status = 0;
226890c2143aSSeungwon Jeon 
2269e352c813SSeungwon Jeon 			dw_mci_request_end(host, mrq);
2270f95f3850SWill Newton 			goto unlock;
2271f95f3850SWill Newton 
2272f95f3850SWill Newton 		case STATE_DATA_ERROR:
2273f95f3850SWill Newton 			if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2274f95f3850SWill Newton 						&host->pending_events))
2275f95f3850SWill Newton 				break;
2276f95f3850SWill Newton 
2277f95f3850SWill Newton 			state = STATE_DATA_BUSY;
2278f95f3850SWill Newton 			break;
2279f95f3850SWill Newton 		}
2280f95f3850SWill Newton 	} while (state != prev_state);
2281f95f3850SWill Newton 
2282f95f3850SWill Newton 	host->state = state;
2283f95f3850SWill Newton unlock:
2284f95f3850SWill Newton 	spin_unlock(&host->lock);
2285f95f3850SWill Newton 
2286f95f3850SWill Newton }
2287f95f3850SWill Newton 
228834b664a2SJames Hogan /* push final bytes to part_buf, only use during push */
dw_mci_set_part_bytes(struct dw_mci * host,void * buf,int cnt)228934b664a2SJames Hogan static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
229034b664a2SJames Hogan {
229134b664a2SJames Hogan 	memcpy((void *)&host->part_buf, buf, cnt);
229234b664a2SJames Hogan 	host->part_buf_count = cnt;
229334b664a2SJames Hogan }
229434b664a2SJames Hogan 
229534b664a2SJames Hogan /* append bytes to part_buf, only use during push */
dw_mci_push_part_bytes(struct dw_mci * host,void * buf,int cnt)229634b664a2SJames Hogan static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
229734b664a2SJames Hogan {
229834b664a2SJames Hogan 	cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
229934b664a2SJames Hogan 	memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
230034b664a2SJames Hogan 	host->part_buf_count += cnt;
230134b664a2SJames Hogan 	return cnt;
230234b664a2SJames Hogan }
230334b664a2SJames Hogan 
230434b664a2SJames Hogan /* pull first bytes from part_buf, only use during pull */
dw_mci_pull_part_bytes(struct dw_mci * host,void * buf,int cnt)230534b664a2SJames Hogan static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
230634b664a2SJames Hogan {
23070e3a22c0SShawn Lin 	cnt = min_t(int, cnt, host->part_buf_count);
230834b664a2SJames Hogan 	if (cnt) {
230934b664a2SJames Hogan 		memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
231034b664a2SJames Hogan 		       cnt);
231134b664a2SJames Hogan 		host->part_buf_count -= cnt;
231234b664a2SJames Hogan 		host->part_buf_start += cnt;
231334b664a2SJames Hogan 	}
231434b664a2SJames Hogan 	return cnt;
231534b664a2SJames Hogan }
231634b664a2SJames Hogan 
231734b664a2SJames Hogan /* pull final bytes from the part_buf, assuming it's just been filled */
dw_mci_pull_final_bytes(struct dw_mci * host,void * buf,int cnt)231834b664a2SJames Hogan static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
231934b664a2SJames Hogan {
232034b664a2SJames Hogan 	memcpy(buf, &host->part_buf, cnt);
232134b664a2SJames Hogan 	host->part_buf_start = cnt;
232234b664a2SJames Hogan 	host->part_buf_count = (1 << host->data_shift) - cnt;
232334b664a2SJames Hogan }
232434b664a2SJames Hogan 
dw_mci_push_data16(struct dw_mci * host,void * buf,int cnt)2325f95f3850SWill Newton static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2326f95f3850SWill Newton {
2327cfbeb59cSMarkos Chandras 	struct mmc_data *data = host->data;
2328cfbeb59cSMarkos Chandras 	int init_cnt = cnt;
2329cfbeb59cSMarkos Chandras 
233034b664a2SJames Hogan 	/* try and push anything in the part_buf */
233134b664a2SJames Hogan 	if (unlikely(host->part_buf_count)) {
233234b664a2SJames Hogan 		int len = dw_mci_push_part_bytes(host, buf, cnt);
23330e3a22c0SShawn Lin 
233434b664a2SJames Hogan 		buf += len;
233534b664a2SJames Hogan 		cnt -= len;
2336cfbeb59cSMarkos Chandras 		if (host->part_buf_count == 2) {
233776184ac1SBen Dooks 			mci_fifo_writew(host->fifo_reg, host->part_buf16);
233834b664a2SJames Hogan 			host->part_buf_count = 0;
233934b664a2SJames Hogan 		}
234034b664a2SJames Hogan 	}
234134b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
234234b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x1)) {
234334b664a2SJames Hogan 		while (cnt >= 2) {
234434b664a2SJames Hogan 			u16 aligned_buf[64];
234534b664a2SJames Hogan 			int len = min(cnt & -2, (int)sizeof(aligned_buf));
234634b664a2SJames Hogan 			int items = len >> 1;
234734b664a2SJames Hogan 			int i;
234834b664a2SJames Hogan 			/* memcpy from input buffer into aligned buffer */
234934b664a2SJames Hogan 			memcpy(aligned_buf, buf, len);
235034b664a2SJames Hogan 			buf += len;
235134b664a2SJames Hogan 			cnt -= len;
235234b664a2SJames Hogan 			/* push data from aligned buffer into fifo */
235334b664a2SJames Hogan 			for (i = 0; i < items; ++i)
235476184ac1SBen Dooks 				mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
235534b664a2SJames Hogan 		}
235634b664a2SJames Hogan 	} else
235734b664a2SJames Hogan #endif
235834b664a2SJames Hogan 	{
235934b664a2SJames Hogan 		u16 *pdata = buf;
23600e3a22c0SShawn Lin 
236134b664a2SJames Hogan 		for (; cnt >= 2; cnt -= 2)
236276184ac1SBen Dooks 			mci_fifo_writew(host->fifo_reg, *pdata++);
236334b664a2SJames Hogan 		buf = pdata;
236434b664a2SJames Hogan 	}
236534b664a2SJames Hogan 	/* put anything remaining in the part_buf */
236634b664a2SJames Hogan 	if (cnt) {
236734b664a2SJames Hogan 		dw_mci_set_part_bytes(host, buf, cnt);
2368cfbeb59cSMarkos Chandras 		 /* Push data if we have reached the expected data length */
2369cfbeb59cSMarkos Chandras 		if ((data->bytes_xfered + init_cnt) ==
2370cfbeb59cSMarkos Chandras 		    (data->blksz * data->blocks))
237176184ac1SBen Dooks 			mci_fifo_writew(host->fifo_reg, host->part_buf16);
2372f95f3850SWill Newton 	}
2373f95f3850SWill Newton }
2374f95f3850SWill Newton 
dw_mci_pull_data16(struct dw_mci * host,void * buf,int cnt)2375f95f3850SWill Newton static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2376f95f3850SWill Newton {
237734b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
237834b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x1)) {
237934b664a2SJames Hogan 		while (cnt >= 2) {
238034b664a2SJames Hogan 			/* pull data from fifo into aligned buffer */
238134b664a2SJames Hogan 			u16 aligned_buf[64];
238234b664a2SJames Hogan 			int len = min(cnt & -2, (int)sizeof(aligned_buf));
238334b664a2SJames Hogan 			int items = len >> 1;
238434b664a2SJames Hogan 			int i;
23850e3a22c0SShawn Lin 
238634b664a2SJames Hogan 			for (i = 0; i < items; ++i)
238776184ac1SBen Dooks 				aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
238834b664a2SJames Hogan 			/* memcpy from aligned buffer into output buffer */
238934b664a2SJames Hogan 			memcpy(buf, aligned_buf, len);
239034b664a2SJames Hogan 			buf += len;
239134b664a2SJames Hogan 			cnt -= len;
239234b664a2SJames Hogan 		}
239334b664a2SJames Hogan 	} else
239434b664a2SJames Hogan #endif
239534b664a2SJames Hogan 	{
239634b664a2SJames Hogan 		u16 *pdata = buf;
23970e3a22c0SShawn Lin 
239834b664a2SJames Hogan 		for (; cnt >= 2; cnt -= 2)
239976184ac1SBen Dooks 			*pdata++ = mci_fifo_readw(host->fifo_reg);
240034b664a2SJames Hogan 		buf = pdata;
240134b664a2SJames Hogan 	}
240234b664a2SJames Hogan 	if (cnt) {
240376184ac1SBen Dooks 		host->part_buf16 = mci_fifo_readw(host->fifo_reg);
240434b664a2SJames Hogan 		dw_mci_pull_final_bytes(host, buf, cnt);
2405f95f3850SWill Newton 	}
2406f95f3850SWill Newton }
2407f95f3850SWill Newton 
dw_mci_push_data32(struct dw_mci * host,void * buf,int cnt)2408f95f3850SWill Newton static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2409f95f3850SWill Newton {
2410cfbeb59cSMarkos Chandras 	struct mmc_data *data = host->data;
2411cfbeb59cSMarkos Chandras 	int init_cnt = cnt;
2412cfbeb59cSMarkos Chandras 
241334b664a2SJames Hogan 	/* try and push anything in the part_buf */
241434b664a2SJames Hogan 	if (unlikely(host->part_buf_count)) {
241534b664a2SJames Hogan 		int len = dw_mci_push_part_bytes(host, buf, cnt);
24160e3a22c0SShawn Lin 
241734b664a2SJames Hogan 		buf += len;
241834b664a2SJames Hogan 		cnt -= len;
2419cfbeb59cSMarkos Chandras 		if (host->part_buf_count == 4) {
242076184ac1SBen Dooks 			mci_fifo_writel(host->fifo_reg,	host->part_buf32);
242134b664a2SJames Hogan 			host->part_buf_count = 0;
242234b664a2SJames Hogan 		}
242334b664a2SJames Hogan 	}
242434b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
242534b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x3)) {
242634b664a2SJames Hogan 		while (cnt >= 4) {
242734b664a2SJames Hogan 			u32 aligned_buf[32];
242834b664a2SJames Hogan 			int len = min(cnt & -4, (int)sizeof(aligned_buf));
242934b664a2SJames Hogan 			int items = len >> 2;
243034b664a2SJames Hogan 			int i;
243134b664a2SJames Hogan 			/* memcpy from input buffer into aligned buffer */
243234b664a2SJames Hogan 			memcpy(aligned_buf, buf, len);
243334b664a2SJames Hogan 			buf += len;
243434b664a2SJames Hogan 			cnt -= len;
243534b664a2SJames Hogan 			/* push data from aligned buffer into fifo */
243634b664a2SJames Hogan 			for (i = 0; i < items; ++i)
243776184ac1SBen Dooks 				mci_fifo_writel(host->fifo_reg,	aligned_buf[i]);
243834b664a2SJames Hogan 		}
243934b664a2SJames Hogan 	} else
244034b664a2SJames Hogan #endif
244134b664a2SJames Hogan 	{
244234b664a2SJames Hogan 		u32 *pdata = buf;
24430e3a22c0SShawn Lin 
244434b664a2SJames Hogan 		for (; cnt >= 4; cnt -= 4)
244576184ac1SBen Dooks 			mci_fifo_writel(host->fifo_reg, *pdata++);
244634b664a2SJames Hogan 		buf = pdata;
244734b664a2SJames Hogan 	}
244834b664a2SJames Hogan 	/* put anything remaining in the part_buf */
244934b664a2SJames Hogan 	if (cnt) {
245034b664a2SJames Hogan 		dw_mci_set_part_bytes(host, buf, cnt);
2451cfbeb59cSMarkos Chandras 		 /* Push data if we have reached the expected data length */
2452cfbeb59cSMarkos Chandras 		if ((data->bytes_xfered + init_cnt) ==
2453cfbeb59cSMarkos Chandras 		    (data->blksz * data->blocks))
245476184ac1SBen Dooks 			mci_fifo_writel(host->fifo_reg, host->part_buf32);
2455f95f3850SWill Newton 	}
2456f95f3850SWill Newton }
2457f95f3850SWill Newton 
dw_mci_pull_data32(struct dw_mci * host,void * buf,int cnt)2458f95f3850SWill Newton static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2459f95f3850SWill Newton {
246034b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
246134b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x3)) {
246234b664a2SJames Hogan 		while (cnt >= 4) {
246334b664a2SJames Hogan 			/* pull data from fifo into aligned buffer */
246434b664a2SJames Hogan 			u32 aligned_buf[32];
246534b664a2SJames Hogan 			int len = min(cnt & -4, (int)sizeof(aligned_buf));
246634b664a2SJames Hogan 			int items = len >> 2;
246734b664a2SJames Hogan 			int i;
24680e3a22c0SShawn Lin 
246934b664a2SJames Hogan 			for (i = 0; i < items; ++i)
247076184ac1SBen Dooks 				aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
247134b664a2SJames Hogan 			/* memcpy from aligned buffer into output buffer */
247234b664a2SJames Hogan 			memcpy(buf, aligned_buf, len);
247334b664a2SJames Hogan 			buf += len;
247434b664a2SJames Hogan 			cnt -= len;
247534b664a2SJames Hogan 		}
247634b664a2SJames Hogan 	} else
247734b664a2SJames Hogan #endif
247834b664a2SJames Hogan 	{
247934b664a2SJames Hogan 		u32 *pdata = buf;
24800e3a22c0SShawn Lin 
248134b664a2SJames Hogan 		for (; cnt >= 4; cnt -= 4)
248276184ac1SBen Dooks 			*pdata++ = mci_fifo_readl(host->fifo_reg);
248334b664a2SJames Hogan 		buf = pdata;
248434b664a2SJames Hogan 	}
248534b664a2SJames Hogan 	if (cnt) {
248676184ac1SBen Dooks 		host->part_buf32 = mci_fifo_readl(host->fifo_reg);
248734b664a2SJames Hogan 		dw_mci_pull_final_bytes(host, buf, cnt);
2488f95f3850SWill Newton 	}
2489f95f3850SWill Newton }
2490f95f3850SWill Newton 
dw_mci_push_data64(struct dw_mci * host,void * buf,int cnt)2491f95f3850SWill Newton static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2492f95f3850SWill Newton {
2493cfbeb59cSMarkos Chandras 	struct mmc_data *data = host->data;
2494cfbeb59cSMarkos Chandras 	int init_cnt = cnt;
2495cfbeb59cSMarkos Chandras 
249634b664a2SJames Hogan 	/* try and push anything in the part_buf */
249734b664a2SJames Hogan 	if (unlikely(host->part_buf_count)) {
249834b664a2SJames Hogan 		int len = dw_mci_push_part_bytes(host, buf, cnt);
24990e3a22c0SShawn Lin 
250034b664a2SJames Hogan 		buf += len;
250134b664a2SJames Hogan 		cnt -= len;
2502c09fbd74SSeungwon Jeon 
2503cfbeb59cSMarkos Chandras 		if (host->part_buf_count == 8) {
250476184ac1SBen Dooks 			mci_fifo_writeq(host->fifo_reg,	host->part_buf);
250534b664a2SJames Hogan 			host->part_buf_count = 0;
250634b664a2SJames Hogan 		}
250734b664a2SJames Hogan 	}
250834b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
250934b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x7)) {
251034b664a2SJames Hogan 		while (cnt >= 8) {
251134b664a2SJames Hogan 			u64 aligned_buf[16];
251234b664a2SJames Hogan 			int len = min(cnt & -8, (int)sizeof(aligned_buf));
251334b664a2SJames Hogan 			int items = len >> 3;
251434b664a2SJames Hogan 			int i;
251534b664a2SJames Hogan 			/* memcpy from input buffer into aligned buffer */
251634b664a2SJames Hogan 			memcpy(aligned_buf, buf, len);
251734b664a2SJames Hogan 			buf += len;
251834b664a2SJames Hogan 			cnt -= len;
251934b664a2SJames Hogan 			/* push data from aligned buffer into fifo */
252034b664a2SJames Hogan 			for (i = 0; i < items; ++i)
252176184ac1SBen Dooks 				mci_fifo_writeq(host->fifo_reg,	aligned_buf[i]);
252234b664a2SJames Hogan 		}
252334b664a2SJames Hogan 	} else
252434b664a2SJames Hogan #endif
252534b664a2SJames Hogan 	{
252634b664a2SJames Hogan 		u64 *pdata = buf;
25270e3a22c0SShawn Lin 
252834b664a2SJames Hogan 		for (; cnt >= 8; cnt -= 8)
252976184ac1SBen Dooks 			mci_fifo_writeq(host->fifo_reg, *pdata++);
253034b664a2SJames Hogan 		buf = pdata;
253134b664a2SJames Hogan 	}
253234b664a2SJames Hogan 	/* put anything remaining in the part_buf */
253334b664a2SJames Hogan 	if (cnt) {
253434b664a2SJames Hogan 		dw_mci_set_part_bytes(host, buf, cnt);
2535cfbeb59cSMarkos Chandras 		/* Push data if we have reached the expected data length */
2536cfbeb59cSMarkos Chandras 		if ((data->bytes_xfered + init_cnt) ==
2537cfbeb59cSMarkos Chandras 		    (data->blksz * data->blocks))
253876184ac1SBen Dooks 			mci_fifo_writeq(host->fifo_reg, host->part_buf);
2539f95f3850SWill Newton 	}
2540f95f3850SWill Newton }
2541f95f3850SWill Newton 
dw_mci_pull_data64(struct dw_mci * host,void * buf,int cnt)2542f95f3850SWill Newton static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2543f95f3850SWill Newton {
254434b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
254534b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x7)) {
254634b664a2SJames Hogan 		while (cnt >= 8) {
254734b664a2SJames Hogan 			/* pull data from fifo into aligned buffer */
254834b664a2SJames Hogan 			u64 aligned_buf[16];
254934b664a2SJames Hogan 			int len = min(cnt & -8, (int)sizeof(aligned_buf));
255034b664a2SJames Hogan 			int items = len >> 3;
255134b664a2SJames Hogan 			int i;
25520e3a22c0SShawn Lin 
255334b664a2SJames Hogan 			for (i = 0; i < items; ++i)
255476184ac1SBen Dooks 				aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
255576184ac1SBen Dooks 
255634b664a2SJames Hogan 			/* memcpy from aligned buffer into output buffer */
255734b664a2SJames Hogan 			memcpy(buf, aligned_buf, len);
255834b664a2SJames Hogan 			buf += len;
255934b664a2SJames Hogan 			cnt -= len;
2560f95f3850SWill Newton 		}
256134b664a2SJames Hogan 	} else
256234b664a2SJames Hogan #endif
256334b664a2SJames Hogan 	{
256434b664a2SJames Hogan 		u64 *pdata = buf;
25650e3a22c0SShawn Lin 
256634b664a2SJames Hogan 		for (; cnt >= 8; cnt -= 8)
256776184ac1SBen Dooks 			*pdata++ = mci_fifo_readq(host->fifo_reg);
256834b664a2SJames Hogan 		buf = pdata;
256934b664a2SJames Hogan 	}
257034b664a2SJames Hogan 	if (cnt) {
257176184ac1SBen Dooks 		host->part_buf = mci_fifo_readq(host->fifo_reg);
257234b664a2SJames Hogan 		dw_mci_pull_final_bytes(host, buf, cnt);
257334b664a2SJames Hogan 	}
257434b664a2SJames Hogan }
257534b664a2SJames Hogan 
dw_mci_pull_data(struct dw_mci * host,void * buf,int cnt)257634b664a2SJames Hogan static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
257734b664a2SJames Hogan {
257834b664a2SJames Hogan 	int len;
257934b664a2SJames Hogan 
258034b664a2SJames Hogan 	/* get remaining partial bytes */
258134b664a2SJames Hogan 	len = dw_mci_pull_part_bytes(host, buf, cnt);
258234b664a2SJames Hogan 	if (unlikely(len == cnt))
258334b664a2SJames Hogan 		return;
258434b664a2SJames Hogan 	buf += len;
258534b664a2SJames Hogan 	cnt -= len;
258634b664a2SJames Hogan 
258734b664a2SJames Hogan 	/* get the rest of the data */
258834b664a2SJames Hogan 	host->pull_data(host, buf, cnt);
2589f95f3850SWill Newton }
2590f95f3850SWill Newton 
dw_mci_read_data_pio(struct dw_mci * host,bool dto)259187a74d39SKyoungil Kim static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2592f95f3850SWill Newton {
2593f9c2a0dcSSeungwon Jeon 	struct sg_mapping_iter *sg_miter = &host->sg_miter;
2594f9c2a0dcSSeungwon Jeon 	void *buf;
2595f9c2a0dcSSeungwon Jeon 	unsigned int offset;
2596f95f3850SWill Newton 	struct mmc_data	*data = host->data;
2597f95f3850SWill Newton 	int shift = host->data_shift;
2598f95f3850SWill Newton 	u32 status;
25993e4b0d8bSMarkos Chandras 	unsigned int len;
2600f9c2a0dcSSeungwon Jeon 	unsigned int remain, fcnt;
2601f95f3850SWill Newton 
2602f95f3850SWill Newton 	do {
2603f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2604f9c2a0dcSSeungwon Jeon 			goto done;
2605f95f3850SWill Newton 
26064225fc85SImre Deak 		host->sg = sg_miter->piter.sg;
2607f9c2a0dcSSeungwon Jeon 		buf = sg_miter->addr;
2608f9c2a0dcSSeungwon Jeon 		remain = sg_miter->length;
2609f9c2a0dcSSeungwon Jeon 		offset = 0;
2610f9c2a0dcSSeungwon Jeon 
2611f9c2a0dcSSeungwon Jeon 		do {
2612f9c2a0dcSSeungwon Jeon 			fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2613f9c2a0dcSSeungwon Jeon 					<< shift) + host->part_buf_count;
2614f9c2a0dcSSeungwon Jeon 			len = min(remain, fcnt);
2615f9c2a0dcSSeungwon Jeon 			if (!len)
2616f9c2a0dcSSeungwon Jeon 				break;
2617f9c2a0dcSSeungwon Jeon 			dw_mci_pull_data(host, (void *)(buf + offset), len);
26183e4b0d8bSMarkos Chandras 			data->bytes_xfered += len;
2619f95f3850SWill Newton 			offset += len;
2620f9c2a0dcSSeungwon Jeon 			remain -= len;
2621f9c2a0dcSSeungwon Jeon 		} while (remain);
2622f95f3850SWill Newton 
2623e74f3a9cSSeungwon Jeon 		sg_miter->consumed = offset;
2624f95f3850SWill Newton 		status = mci_readl(host, MINTSTS);
2625f95f3850SWill Newton 		mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
262687a74d39SKyoungil Kim 	/* if the RXDR is ready read again */
262787a74d39SKyoungil Kim 	} while ((status & SDMMC_INT_RXDR) ||
262887a74d39SKyoungil Kim 		 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2629f9c2a0dcSSeungwon Jeon 
2630f9c2a0dcSSeungwon Jeon 	if (!remain) {
2631f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2632f9c2a0dcSSeungwon Jeon 			goto done;
2633f9c2a0dcSSeungwon Jeon 		sg_miter->consumed = 0;
2634f9c2a0dcSSeungwon Jeon 	}
2635f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2636f95f3850SWill Newton 	return;
2637f95f3850SWill Newton 
2638f95f3850SWill Newton done:
2639f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2640f9c2a0dcSSeungwon Jeon 	host->sg = NULL;
26410e3a22c0SShawn Lin 	smp_wmb(); /* drain writebuffer */
2642f95f3850SWill Newton 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2643f95f3850SWill Newton }
2644f95f3850SWill Newton 
dw_mci_write_data_pio(struct dw_mci * host)2645f95f3850SWill Newton static void dw_mci_write_data_pio(struct dw_mci *host)
2646f95f3850SWill Newton {
2647f9c2a0dcSSeungwon Jeon 	struct sg_mapping_iter *sg_miter = &host->sg_miter;
2648f9c2a0dcSSeungwon Jeon 	void *buf;
2649f9c2a0dcSSeungwon Jeon 	unsigned int offset;
2650f95f3850SWill Newton 	struct mmc_data	*data = host->data;
2651f95f3850SWill Newton 	int shift = host->data_shift;
2652f95f3850SWill Newton 	u32 status;
26533e4b0d8bSMarkos Chandras 	unsigned int len;
2654f9c2a0dcSSeungwon Jeon 	unsigned int fifo_depth = host->fifo_depth;
2655f9c2a0dcSSeungwon Jeon 	unsigned int remain, fcnt;
2656f95f3850SWill Newton 
2657f95f3850SWill Newton 	do {
2658f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2659f9c2a0dcSSeungwon Jeon 			goto done;
2660f95f3850SWill Newton 
26614225fc85SImre Deak 		host->sg = sg_miter->piter.sg;
2662f9c2a0dcSSeungwon Jeon 		buf = sg_miter->addr;
2663f9c2a0dcSSeungwon Jeon 		remain = sg_miter->length;
2664f9c2a0dcSSeungwon Jeon 		offset = 0;
2665f9c2a0dcSSeungwon Jeon 
2666f9c2a0dcSSeungwon Jeon 		do {
2667f9c2a0dcSSeungwon Jeon 			fcnt = ((fifo_depth -
2668f9c2a0dcSSeungwon Jeon 				 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2669f9c2a0dcSSeungwon Jeon 					<< shift) - host->part_buf_count;
2670f9c2a0dcSSeungwon Jeon 			len = min(remain, fcnt);
2671f9c2a0dcSSeungwon Jeon 			if (!len)
2672f9c2a0dcSSeungwon Jeon 				break;
2673f9c2a0dcSSeungwon Jeon 			host->push_data(host, (void *)(buf + offset), len);
26743e4b0d8bSMarkos Chandras 			data->bytes_xfered += len;
2675f95f3850SWill Newton 			offset += len;
2676f9c2a0dcSSeungwon Jeon 			remain -= len;
2677f9c2a0dcSSeungwon Jeon 		} while (remain);
2678f95f3850SWill Newton 
2679e74f3a9cSSeungwon Jeon 		sg_miter->consumed = offset;
2680f95f3850SWill Newton 		status = mci_readl(host, MINTSTS);
2681f95f3850SWill Newton 		mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2682f95f3850SWill Newton 	} while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2683f9c2a0dcSSeungwon Jeon 
2684f9c2a0dcSSeungwon Jeon 	if (!remain) {
2685f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2686f9c2a0dcSSeungwon Jeon 			goto done;
2687f9c2a0dcSSeungwon Jeon 		sg_miter->consumed = 0;
2688f9c2a0dcSSeungwon Jeon 	}
2689f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2690f95f3850SWill Newton 	return;
2691f95f3850SWill Newton 
2692f95f3850SWill Newton done:
2693f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2694f9c2a0dcSSeungwon Jeon 	host->sg = NULL;
26950e3a22c0SShawn Lin 	smp_wmb(); /* drain writebuffer */
2696f95f3850SWill Newton 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2697f95f3850SWill Newton }
2698f95f3850SWill Newton 
dw_mci_cmd_interrupt(struct dw_mci * host,u32 status)2699f95f3850SWill Newton static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2700f95f3850SWill Newton {
27010363b12dSDouglas Anderson 	del_timer(&host->cto_timer);
27020363b12dSDouglas Anderson 
2703f95f3850SWill Newton 	if (!host->cmd_status)
2704f95f3850SWill Newton 		host->cmd_status = status;
2705f95f3850SWill Newton 
27060e3a22c0SShawn Lin 	smp_wmb(); /* drain writebuffer */
2707f95f3850SWill Newton 
2708f95f3850SWill Newton 	set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2709f95f3850SWill Newton 	tasklet_schedule(&host->tasklet);
27102b8ac062SVincent Whitchurch 
27112b8ac062SVincent Whitchurch 	dw_mci_start_fault_timer(host);
2712f95f3850SWill Newton }
2713f95f3850SWill Newton 
dw_mci_handle_cd(struct dw_mci * host)27146130e7a9SDoug Anderson static void dw_mci_handle_cd(struct dw_mci *host)
27156130e7a9SDoug Anderson {
2716b23475faSJaehoon Chung 	struct dw_mci_slot *slot = host->slot;
27176130e7a9SDoug Anderson 
27186130e7a9SDoug Anderson 	mmc_detect_change(slot->mmc,
27196130e7a9SDoug Anderson 		msecs_to_jiffies(host->pdata->detect_delay_ms));
27206130e7a9SDoug Anderson }
27216130e7a9SDoug Anderson 
dw_mci_interrupt(int irq,void * dev_id)2722f95f3850SWill Newton static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2723f95f3850SWill Newton {
2724f95f3850SWill Newton 	struct dw_mci *host = dev_id;
2725182c9081SSeungwon Jeon 	u32 pending;
2726b23475faSJaehoon Chung 	struct dw_mci_slot *slot = host->slot;
2727f95f3850SWill Newton 
2728f95f3850SWill Newton 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2729f95f3850SWill Newton 
2730476d79f1SDoug Anderson 	if (pending) {
273101730558SDoug Anderson 		/* Check volt switch first, since it can look like an error */
273201730558SDoug Anderson 		if ((host->state == STATE_SENDING_CMD11) &&
273301730558SDoug Anderson 		    (pending & SDMMC_INT_VOLT_SWITCH)) {
273401730558SDoug Anderson 			mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
273501730558SDoug Anderson 			pending &= ~SDMMC_INT_VOLT_SWITCH;
273649ba0302SDoug Anderson 
273749ba0302SDoug Anderson 			/*
273849ba0302SDoug Anderson 			 * Hold the lock; we know cmd11_timer can't be kicked
273949ba0302SDoug Anderson 			 * off after the lock is released, so safe to delete.
274049ba0302SDoug Anderson 			 */
27419f7d4c91STian Tao 			spin_lock(&host->irq_lock);
274201730558SDoug Anderson 			dw_mci_cmd_interrupt(host, pending);
27439f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
274449ba0302SDoug Anderson 
274549ba0302SDoug Anderson 			del_timer(&host->cmd11_timer);
274601730558SDoug Anderson 		}
274701730558SDoug Anderson 
2748f95f3850SWill Newton 		if (pending & DW_MCI_CMD_ERROR_FLAGS) {
27499f7d4c91STian Tao 			spin_lock(&host->irq_lock);
27508892b705SDouglas Anderson 
275103de1921SAddy Ke 			del_timer(&host->cto_timer);
2752f95f3850SWill Newton 			mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2753182c9081SSeungwon Jeon 			host->cmd_status = pending;
27540e3a22c0SShawn Lin 			smp_wmb(); /* drain writebuffer */
2755f95f3850SWill Newton 			set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
27568892b705SDouglas Anderson 
27579f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
2758f95f3850SWill Newton 		}
2759f95f3850SWill Newton 
2760f95f3850SWill Newton 		if (pending & DW_MCI_DATA_ERROR_FLAGS) {
276126391e49SVincent Whitchurch 			spin_lock(&host->irq_lock);
276226391e49SVincent Whitchurch 
27631a6fe7bbSMårten Lindahl 			if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
27641a6fe7bbSMårten Lindahl 				del_timer(&host->dto_timer);
27651a6fe7bbSMårten Lindahl 
2766f95f3850SWill Newton 			/* if there is an error report DATA_ERROR */
2767f95f3850SWill Newton 			mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2768182c9081SSeungwon Jeon 			host->data_status = pending;
27690e3a22c0SShawn Lin 			smp_wmb(); /* drain writebuffer */
2770f95f3850SWill Newton 			set_bit(EVENT_DATA_ERROR, &host->pending_events);
27711a6fe7bbSMårten Lindahl 
27721a6fe7bbSMårten Lindahl 			if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
27731a6fe7bbSMårten Lindahl 				/* In case of error, we cannot expect a DTO */
27741a6fe7bbSMårten Lindahl 				set_bit(EVENT_DATA_COMPLETE,
27751a6fe7bbSMårten Lindahl 					&host->pending_events);
27761a6fe7bbSMårten Lindahl 
2777f95f3850SWill Newton 			tasklet_schedule(&host->tasklet);
277826391e49SVincent Whitchurch 
277926391e49SVincent Whitchurch 			spin_unlock(&host->irq_lock);
2780f95f3850SWill Newton 		}
2781f95f3850SWill Newton 
2782f95f3850SWill Newton 		if (pending & SDMMC_INT_DATA_OVER) {
27839f7d4c91STian Tao 			spin_lock(&host->irq_lock);
278493c23ae3SDouglas Anderson 
278557e10486SAddy Ke 			del_timer(&host->dto_timer);
278657e10486SAddy Ke 
2787f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2788f95f3850SWill Newton 			if (!host->data_status)
2789182c9081SSeungwon Jeon 				host->data_status = pending;
27900e3a22c0SShawn Lin 			smp_wmb(); /* drain writebuffer */
2791f95f3850SWill Newton 			if (host->dir_status == DW_MCI_RECV_STATUS) {
2792f95f3850SWill Newton 				if (host->sg != NULL)
279387a74d39SKyoungil Kim 					dw_mci_read_data_pio(host, true);
2794f95f3850SWill Newton 			}
2795f95f3850SWill Newton 			set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2796f95f3850SWill Newton 			tasklet_schedule(&host->tasklet);
279793c23ae3SDouglas Anderson 
27989f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
2799f95f3850SWill Newton 		}
2800f95f3850SWill Newton 
2801f95f3850SWill Newton 		if (pending & SDMMC_INT_RXDR) {
2802f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2803b40af3aaSJames Hogan 			if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
280487a74d39SKyoungil Kim 				dw_mci_read_data_pio(host, false);
2805f95f3850SWill Newton 		}
2806f95f3850SWill Newton 
2807f95f3850SWill Newton 		if (pending & SDMMC_INT_TXDR) {
2808f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2809b40af3aaSJames Hogan 			if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2810f95f3850SWill Newton 				dw_mci_write_data_pio(host);
2811f95f3850SWill Newton 		}
2812f95f3850SWill Newton 
2813f95f3850SWill Newton 		if (pending & SDMMC_INT_CMD_DONE) {
28149f7d4c91STian Tao 			spin_lock(&host->irq_lock);
28158892b705SDouglas Anderson 
2816f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2817182c9081SSeungwon Jeon 			dw_mci_cmd_interrupt(host, pending);
28188892b705SDouglas Anderson 
28199f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
2820f95f3850SWill Newton 		}
2821f95f3850SWill Newton 
2822f95f3850SWill Newton 		if (pending & SDMMC_INT_CD) {
2823f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_CD);
28246130e7a9SDoug Anderson 			dw_mci_handle_cd(host);
2825f95f3850SWill Newton 		}
2826f95f3850SWill Newton 
282776756234SAddy Ke 		if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
282876756234SAddy Ke 			mci_writel(host, RINTSTS,
282976756234SAddy Ke 				   SDMMC_INT_SDIO(slot->sdio_id));
283032dba737SUlf Hansson 			__dw_mci_enable_sdio_irq(slot, 0);
283132dba737SUlf Hansson 			sdio_signal_irq(slot->mmc);
28321a5c8e1fSShashidhar Hiremath 		}
28331a5c8e1fSShashidhar Hiremath 
28341fb5f68aSMarkos Chandras 	}
2835f95f3850SWill Newton 
28363fc7eaefSShawn Lin 	if (host->use_dma != TRANS_MODE_IDMAC)
28373fc7eaefSShawn Lin 		return IRQ_HANDLED;
28383fc7eaefSShawn Lin 
28393fc7eaefSShawn Lin 	/* Handle IDMA interrupts */
284069d99fdcSPrabu Thangamuthu 	if (host->dma_64bit_address == 1) {
284169d99fdcSPrabu Thangamuthu 		pending = mci_readl(host, IDSTS64);
284269d99fdcSPrabu Thangamuthu 		if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
284369d99fdcSPrabu Thangamuthu 			mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
284469d99fdcSPrabu Thangamuthu 							SDMMC_IDMAC_INT_RI);
284569d99fdcSPrabu Thangamuthu 			mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2846faecf411SShawn Lin 			if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
28473fc7eaefSShawn Lin 				host->dma_ops->complete((void *)host);
284869d99fdcSPrabu Thangamuthu 		}
284969d99fdcSPrabu Thangamuthu 	} else {
2850f95f3850SWill Newton 		pending = mci_readl(host, IDSTS);
2851f95f3850SWill Newton 		if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
285269d99fdcSPrabu Thangamuthu 			mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
285369d99fdcSPrabu Thangamuthu 							SDMMC_IDMAC_INT_RI);
2854f95f3850SWill Newton 			mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2855faecf411SShawn Lin 			if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
28563fc7eaefSShawn Lin 				host->dma_ops->complete((void *)host);
2857f95f3850SWill Newton 		}
285869d99fdcSPrabu Thangamuthu 	}
2859f95f3850SWill Newton 
2860f95f3850SWill Newton 	return IRQ_HANDLED;
2861f95f3850SWill Newton }
2862f95f3850SWill Newton 
dw_mci_init_slot_caps(struct dw_mci_slot * slot)2863a4faa492SShawn Lin static int dw_mci_init_slot_caps(struct dw_mci_slot *slot)
2864a4faa492SShawn Lin {
2865a4faa492SShawn Lin 	struct dw_mci *host = slot->host;
2866a4faa492SShawn Lin 	const struct dw_mci_drv_data *drv_data = host->drv_data;
2867a4faa492SShawn Lin 	struct mmc_host *mmc = slot->mmc;
2868a4faa492SShawn Lin 	int ctrl_id;
2869a4faa492SShawn Lin 
2870a4faa492SShawn Lin 	if (host->pdata->caps)
2871a4faa492SShawn Lin 		mmc->caps = host->pdata->caps;
2872a4faa492SShawn Lin 
2873a4faa492SShawn Lin 	if (host->pdata->pm_caps)
2874a4faa492SShawn Lin 		mmc->pm_caps = host->pdata->pm_caps;
2875a4faa492SShawn Lin 
28760dc7a3ecSJohn Keeping 	if (drv_data)
28770dc7a3ecSJohn Keeping 		mmc->caps |= drv_data->common_caps;
28780dc7a3ecSJohn Keeping 
2879a4faa492SShawn Lin 	if (host->dev->of_node) {
2880a4faa492SShawn Lin 		ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2881a4faa492SShawn Lin 		if (ctrl_id < 0)
2882a4faa492SShawn Lin 			ctrl_id = 0;
2883a4faa492SShawn Lin 	} else {
2884a4faa492SShawn Lin 		ctrl_id = to_platform_device(host->dev)->id;
2885a4faa492SShawn Lin 	}
28860d84b9e5SShawn Lin 
28870d84b9e5SShawn Lin 	if (drv_data && drv_data->caps) {
28880d84b9e5SShawn Lin 		if (ctrl_id >= drv_data->num_caps) {
28890d84b9e5SShawn Lin 			dev_err(host->dev, "invalid controller id %d\n",
28900d84b9e5SShawn Lin 				ctrl_id);
28910d84b9e5SShawn Lin 			return -EINVAL;
28920d84b9e5SShawn Lin 		}
2893a4faa492SShawn Lin 		mmc->caps |= drv_data->caps[ctrl_id];
28940d84b9e5SShawn Lin 	}
2895a4faa492SShawn Lin 
2896a4faa492SShawn Lin 	if (host->pdata->caps2)
2897a4faa492SShawn Lin 		mmc->caps2 = host->pdata->caps2;
2898a4faa492SShawn Lin 
2899c4313e75SPeter Geis 	/* if host has set a minimum_freq, we should respect it */
2900c4313e75SPeter Geis 	if (host->minimum_speed)
2901c4313e75SPeter Geis 		mmc->f_min = host->minimum_speed;
2902c4313e75SPeter Geis 	else
290386b93a48SJaehoon Chung 		mmc->f_min = DW_MCI_FREQ_MIN;
2904c4313e75SPeter Geis 
290586b93a48SJaehoon Chung 	if (!mmc->f_max)
290686b93a48SJaehoon Chung 		mmc->f_max = DW_MCI_FREQ_MAX;
290786b93a48SJaehoon Chung 
2908a4faa492SShawn Lin 	/* Process SDIO IRQs through the sdio_irq_work. */
2909a4faa492SShawn Lin 	if (mmc->caps & MMC_CAP_SDIO_IRQ)
2910a4faa492SShawn Lin 		mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2911a4faa492SShawn Lin 
2912a4faa492SShawn Lin 	return 0;
2913a4faa492SShawn Lin }
2914a4faa492SShawn Lin 
dw_mci_init_slot(struct dw_mci * host)2915e4a65ef7SJaehoon Chung static int dw_mci_init_slot(struct dw_mci *host)
2916f95f3850SWill Newton {
2917f95f3850SWill Newton 	struct mmc_host *mmc;
2918f95f3850SWill Newton 	struct dw_mci_slot *slot;
2919a4faa492SShawn Lin 	int ret;
2920f95f3850SWill Newton 
29214a90920cSThomas Abraham 	mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2922f95f3850SWill Newton 	if (!mmc)
2923f95f3850SWill Newton 		return -ENOMEM;
2924f95f3850SWill Newton 
2925f95f3850SWill Newton 	slot = mmc_priv(mmc);
2926e4a65ef7SJaehoon Chung 	slot->id = 0;
2927e4a65ef7SJaehoon Chung 	slot->sdio_id = host->sdio_id0 + slot->id;
2928f95f3850SWill Newton 	slot->mmc = mmc;
2929f95f3850SWill Newton 	slot->host = host;
2930b23475faSJaehoon Chung 	host->slot = slot;
2931f95f3850SWill Newton 
2932f95f3850SWill Newton 	mmc->ops = &dw_mci_ops;
2933f95f3850SWill Newton 
293451da2240SYuvaraj CD 	/*if there are external regulators, get them*/
293551da2240SYuvaraj CD 	ret = mmc_regulator_get_supply(mmc);
29360f3a47b8SWolfram Sang 	if (ret)
29373cf890fcSDoug Anderson 		goto err_host_allocated;
293851da2240SYuvaraj CD 
293951da2240SYuvaraj CD 	if (!mmc->ocr_avail)
2940f95f3850SWill Newton 		mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2941f95f3850SWill Newton 
29423cf890fcSDoug Anderson 	ret = mmc_of_parse(mmc);
29433cf890fcSDoug Anderson 	if (ret)
29443cf890fcSDoug Anderson 		goto err_host_allocated;
2945f95f3850SWill Newton 
2946a4faa492SShawn Lin 	ret = dw_mci_init_slot_caps(slot);
2947a4faa492SShawn Lin 	if (ret)
2948a4faa492SShawn Lin 		goto err_host_allocated;
294932dba737SUlf Hansson 
2950f95f3850SWill Newton 	/* Useful defaults if platform data is unset. */
29513fc7eaefSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC) {
2952a39e5746SJaehoon Chung 		mmc->max_segs = host->ring_size;
2953225faf87SJaehoon Chung 		mmc->max_blk_size = 65535;
2954575c319dSHeiko Stuebner 		mmc->max_seg_size = 0x1000;
29551a25b1b4SSeungwon Jeon 		mmc->max_req_size = mmc->max_seg_size * host->ring_size;
29561a25b1b4SSeungwon Jeon 		mmc->max_blk_count = mmc->max_req_size / 512;
29573fc7eaefSShawn Lin 	} else if (host->use_dma == TRANS_MODE_EDMAC) {
29583fc7eaefSShawn Lin 		mmc->max_segs = 64;
2959225faf87SJaehoon Chung 		mmc->max_blk_size = 65535;
29603fc7eaefSShawn Lin 		mmc->max_blk_count = 65535;
29613fc7eaefSShawn Lin 		mmc->max_req_size =
29623fc7eaefSShawn Lin 				mmc->max_blk_size * mmc->max_blk_count;
29633fc7eaefSShawn Lin 		mmc->max_seg_size = mmc->max_req_size;
2964575c319dSHeiko Stuebner 	} else {
29653fc7eaefSShawn Lin 		/* TRANS_MODE_PIO */
2966f95f3850SWill Newton 		mmc->max_segs = 64;
2967225faf87SJaehoon Chung 		mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
2968f95f3850SWill Newton 		mmc->max_blk_count = 512;
2969575c319dSHeiko Stuebner 		mmc->max_req_size = mmc->max_blk_size *
2970575c319dSHeiko Stuebner 				    mmc->max_blk_count;
2971f95f3850SWill Newton 		mmc->max_seg_size = mmc->max_req_size;
2972575c319dSHeiko Stuebner 	}
2973f95f3850SWill Newton 
2974c0834a58SShawn Lin 	dw_mci_get_cd(mmc);
2975ae0eb348SJaehoon Chung 
29760cea529dSJaehoon Chung 	ret = mmc_add_host(mmc);
29770cea529dSJaehoon Chung 	if (ret)
29783cf890fcSDoug Anderson 		goto err_host_allocated;
2979f95f3850SWill Newton 
2980f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS)
2981f95f3850SWill Newton 	dw_mci_init_debugfs(slot);
2982f95f3850SWill Newton #endif
2983f95f3850SWill Newton 
2984f95f3850SWill Newton 	return 0;
2985800d78bfSThomas Abraham 
29863cf890fcSDoug Anderson err_host_allocated:
2987800d78bfSThomas Abraham 	mmc_free_host(mmc);
298851da2240SYuvaraj CD 	return ret;
2989f95f3850SWill Newton }
2990f95f3850SWill Newton 
dw_mci_cleanup_slot(struct dw_mci_slot * slot)2991e4a65ef7SJaehoon Chung static void dw_mci_cleanup_slot(struct dw_mci_slot *slot)
2992f95f3850SWill Newton {
2993f95f3850SWill Newton 	/* Debugfs stuff is cleaned up by mmc core */
2994f95f3850SWill Newton 	mmc_remove_host(slot->mmc);
2995b23475faSJaehoon Chung 	slot->host->slot = NULL;
2996f95f3850SWill Newton 	mmc_free_host(slot->mmc);
2997f95f3850SWill Newton }
2998f95f3850SWill Newton 
dw_mci_init_dma(struct dw_mci * host)2999f95f3850SWill Newton static void dw_mci_init_dma(struct dw_mci *host)
3000f95f3850SWill Newton {
300169d99fdcSPrabu Thangamuthu 	int addr_config;
30023fc7eaefSShawn Lin 	struct device *dev = host->dev;
30033fc7eaefSShawn Lin 
30043fc7eaefSShawn Lin 	/*
30053fc7eaefSShawn Lin 	* Check tansfer mode from HCON[17:16]
30063fc7eaefSShawn Lin 	* Clear the ambiguous description of dw_mmc databook:
30073fc7eaefSShawn Lin 	* 2b'00: No DMA Interface -> Actually means using Internal DMA block
30083fc7eaefSShawn Lin 	* 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
30093fc7eaefSShawn Lin 	* 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
30103fc7eaefSShawn Lin 	* 2b'11: Non DW DMA Interface -> pio only
30113fc7eaefSShawn Lin 	* Compared to DesignWare DMA Interface, Generic DMA Interface has a
30123fc7eaefSShawn Lin 	* simpler request/acknowledge handshake mechanism and both of them
30133fc7eaefSShawn Lin 	* are regarded as external dma master for dw_mmc.
30143fc7eaefSShawn Lin 	*/
30153fc7eaefSShawn Lin 	host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
30163fc7eaefSShawn Lin 	if (host->use_dma == DMA_INTERFACE_IDMA) {
30173fc7eaefSShawn Lin 		host->use_dma = TRANS_MODE_IDMAC;
30183fc7eaefSShawn Lin 	} else if (host->use_dma == DMA_INTERFACE_DWDMA ||
30193fc7eaefSShawn Lin 		   host->use_dma == DMA_INTERFACE_GDMA) {
30203fc7eaefSShawn Lin 		host->use_dma = TRANS_MODE_EDMAC;
30213fc7eaefSShawn Lin 	} else {
30223fc7eaefSShawn Lin 		goto no_dma;
30233fc7eaefSShawn Lin 	}
30243fc7eaefSShawn Lin 
30253fc7eaefSShawn Lin 	/* Determine which DMA interface to use */
30263fc7eaefSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC) {
30273fc7eaefSShawn Lin 		/*
30283fc7eaefSShawn Lin 		* Check ADDR_CONFIG bit in HCON to find
30293fc7eaefSShawn Lin 		* IDMAC address bus width
30303fc7eaefSShawn Lin 		*/
303170692752SShawn Lin 		addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
303269d99fdcSPrabu Thangamuthu 
303369d99fdcSPrabu Thangamuthu 		if (addr_config == 1) {
303469d99fdcSPrabu Thangamuthu 			/* host supports IDMAC in 64-bit address mode */
303569d99fdcSPrabu Thangamuthu 			host->dma_64bit_address = 1;
30363fc7eaefSShawn Lin 			dev_info(host->dev,
30373fc7eaefSShawn Lin 				 "IDMAC supports 64-bit address mode.\n");
303869d99fdcSPrabu Thangamuthu 			if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
30393fc7eaefSShawn Lin 				dma_set_coherent_mask(host->dev,
30403fc7eaefSShawn Lin 						      DMA_BIT_MASK(64));
304169d99fdcSPrabu Thangamuthu 		} else {
304269d99fdcSPrabu Thangamuthu 			/* host supports IDMAC in 32-bit address mode */
304369d99fdcSPrabu Thangamuthu 			host->dma_64bit_address = 0;
30443fc7eaefSShawn Lin 			dev_info(host->dev,
30453fc7eaefSShawn Lin 				 "IDMAC supports 32-bit address mode.\n");
304669d99fdcSPrabu Thangamuthu 		}
304769d99fdcSPrabu Thangamuthu 
3048f95f3850SWill Newton 		/* Alloc memory for sg translation */
3049cc190d4cSShawn Lin 		host->sg_cpu = dmam_alloc_coherent(host->dev,
3050cc190d4cSShawn Lin 						   DESC_RING_BUF_SZ,
3051f95f3850SWill Newton 						   &host->sg_dma, GFP_KERNEL);
3052f95f3850SWill Newton 		if (!host->sg_cpu) {
30533fc7eaefSShawn Lin 			dev_err(host->dev,
30543fc7eaefSShawn Lin 				"%s: could not alloc DMA memory\n",
3055f95f3850SWill Newton 				__func__);
3056f95f3850SWill Newton 			goto no_dma;
3057f95f3850SWill Newton 		}
3058f95f3850SWill Newton 
3059f95f3850SWill Newton 		host->dma_ops = &dw_mci_idmac_ops;
306000956ea3SSeungwon Jeon 		dev_info(host->dev, "Using internal DMA controller.\n");
30613fc7eaefSShawn Lin 	} else {
30623fc7eaefSShawn Lin 		/* TRANS_MODE_EDMAC: check dma bindings again */
306343fa33aaSAndy Shevchenko 		if ((device_property_string_array_count(dev, "dma-names") < 0) ||
3064852ff5feSDavid Woods 		    !device_property_present(dev, "dmas")) {
3065f95f3850SWill Newton 			goto no_dma;
30663fc7eaefSShawn Lin 		}
30673fc7eaefSShawn Lin 		host->dma_ops = &dw_mci_edmac_ops;
30683fc7eaefSShawn Lin 		dev_info(host->dev, "Using external DMA controller.\n");
30693fc7eaefSShawn Lin 	}
3070f95f3850SWill Newton 
3071e1631f98SJaehoon Chung 	if (host->dma_ops->init && host->dma_ops->start &&
3072e1631f98SJaehoon Chung 	    host->dma_ops->stop && host->dma_ops->cleanup) {
3073f95f3850SWill Newton 		if (host->dma_ops->init(host)) {
30740e3a22c0SShawn Lin 			dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
30750e3a22c0SShawn Lin 				__func__);
3076f95f3850SWill Newton 			goto no_dma;
3077f95f3850SWill Newton 		}
3078f95f3850SWill Newton 	} else {
30794a90920cSThomas Abraham 		dev_err(host->dev, "DMA initialization not found.\n");
3080f95f3850SWill Newton 		goto no_dma;
3081f95f3850SWill Newton 	}
3082f95f3850SWill Newton 
3083f95f3850SWill Newton 	return;
3084f95f3850SWill Newton 
3085f95f3850SWill Newton no_dma:
30864a90920cSThomas Abraham 	dev_info(host->dev, "Using PIO mode.\n");
30873fc7eaefSShawn Lin 	host->use_dma = TRANS_MODE_PIO;
3088f95f3850SWill Newton }
3089f95f3850SWill Newton 
dw_mci_cmd11_timer(struct timer_list * t)309037977729SKees Cook static void dw_mci_cmd11_timer(struct timer_list *t)
30915c935165SDoug Anderson {
309237977729SKees Cook 	struct dw_mci *host = from_timer(host, t, cmd11_timer);
30935c935165SDoug Anderson 
3094fd674198SDoug Anderson 	if (host->state != STATE_SENDING_CMD11) {
3095fd674198SDoug Anderson 		dev_warn(host->dev, "Unexpected CMD11 timeout\n");
3096fd674198SDoug Anderson 		return;
3097fd674198SDoug Anderson 	}
30985c935165SDoug Anderson 
30995c935165SDoug Anderson 	host->cmd_status = SDMMC_INT_RTO;
31005c935165SDoug Anderson 	set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
31015c935165SDoug Anderson 	tasklet_schedule(&host->tasklet);
31025c935165SDoug Anderson }
31035c935165SDoug Anderson 
dw_mci_cto_timer(struct timer_list * t)310437977729SKees Cook static void dw_mci_cto_timer(struct timer_list *t)
310503de1921SAddy Ke {
310637977729SKees Cook 	struct dw_mci *host = from_timer(host, t, cto_timer);
31078892b705SDouglas Anderson 	unsigned long irqflags;
31088892b705SDouglas Anderson 	u32 pending;
310903de1921SAddy Ke 
31108892b705SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
31118892b705SDouglas Anderson 
31128892b705SDouglas Anderson 	/*
31138892b705SDouglas Anderson 	 * If somehow we have very bad interrupt latency it's remotely possible
31148892b705SDouglas Anderson 	 * that the timer could fire while the interrupt is still pending or
31158892b705SDouglas Anderson 	 * while the interrupt is midway through running.  Let's be paranoid
31168892b705SDouglas Anderson 	 * and detect those two cases.  Note that this is paranoia is somewhat
31178892b705SDouglas Anderson 	 * justified because in this function we don't actually cancel the
31188892b705SDouglas Anderson 	 * pending command in the controller--we just assume it will never come.
31198892b705SDouglas Anderson 	 */
31208892b705SDouglas Anderson 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
31218892b705SDouglas Anderson 	if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) {
31228892b705SDouglas Anderson 		/* The interrupt should fire; no need to act but we can warn */
31238892b705SDouglas Anderson 		dev_warn(host->dev, "Unexpected interrupt latency\n");
31248892b705SDouglas Anderson 		goto exit;
31258892b705SDouglas Anderson 	}
31268892b705SDouglas Anderson 	if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) {
31278892b705SDouglas Anderson 		/* Presumably interrupt handler couldn't delete the timer */
31288892b705SDouglas Anderson 		dev_warn(host->dev, "CTO timeout when already completed\n");
31298892b705SDouglas Anderson 		goto exit;
31308892b705SDouglas Anderson 	}
31318892b705SDouglas Anderson 
31328892b705SDouglas Anderson 	/*
31338892b705SDouglas Anderson 	 * Continued paranoia to make sure we're in the state we expect.
31348892b705SDouglas Anderson 	 * This paranoia isn't really justified but it seems good to be safe.
31358892b705SDouglas Anderson 	 */
313603de1921SAddy Ke 	switch (host->state) {
313703de1921SAddy Ke 	case STATE_SENDING_CMD11:
313803de1921SAddy Ke 	case STATE_SENDING_CMD:
313903de1921SAddy Ke 	case STATE_SENDING_STOP:
314003de1921SAddy Ke 		/*
314103de1921SAddy Ke 		 * If CMD_DONE interrupt does NOT come in sending command
314203de1921SAddy Ke 		 * state, we should notify the driver to terminate current
314303de1921SAddy Ke 		 * transfer and report a command timeout to the core.
314403de1921SAddy Ke 		 */
314503de1921SAddy Ke 		host->cmd_status = SDMMC_INT_RTO;
314603de1921SAddy Ke 		set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
314703de1921SAddy Ke 		tasklet_schedule(&host->tasklet);
314803de1921SAddy Ke 		break;
314903de1921SAddy Ke 	default:
315003de1921SAddy Ke 		dev_warn(host->dev, "Unexpected command timeout, state %d\n",
315103de1921SAddy Ke 			 host->state);
315203de1921SAddy Ke 		break;
315303de1921SAddy Ke 	}
31548892b705SDouglas Anderson 
31558892b705SDouglas Anderson exit:
31568892b705SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
315703de1921SAddy Ke }
315803de1921SAddy Ke 
dw_mci_dto_timer(struct timer_list * t)315937977729SKees Cook static void dw_mci_dto_timer(struct timer_list *t)
316057e10486SAddy Ke {
316137977729SKees Cook 	struct dw_mci *host = from_timer(host, t, dto_timer);
316293c23ae3SDouglas Anderson 	unsigned long irqflags;
316393c23ae3SDouglas Anderson 	u32 pending;
316457e10486SAddy Ke 
316593c23ae3SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
316693c23ae3SDouglas Anderson 
316793c23ae3SDouglas Anderson 	/*
316893c23ae3SDouglas Anderson 	 * The DTO timer is much longer than the CTO timer, so it's even less
316993c23ae3SDouglas Anderson 	 * likely that we'll these cases, but it pays to be paranoid.
317093c23ae3SDouglas Anderson 	 */
317193c23ae3SDouglas Anderson 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
317293c23ae3SDouglas Anderson 	if (pending & SDMMC_INT_DATA_OVER) {
317393c23ae3SDouglas Anderson 		/* The interrupt should fire; no need to act but we can warn */
317493c23ae3SDouglas Anderson 		dev_warn(host->dev, "Unexpected data interrupt latency\n");
317593c23ae3SDouglas Anderson 		goto exit;
317693c23ae3SDouglas Anderson 	}
317793c23ae3SDouglas Anderson 	if (test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) {
317893c23ae3SDouglas Anderson 		/* Presumably interrupt handler couldn't delete the timer */
317993c23ae3SDouglas Anderson 		dev_warn(host->dev, "DTO timeout when already completed\n");
318093c23ae3SDouglas Anderson 		goto exit;
318193c23ae3SDouglas Anderson 	}
318293c23ae3SDouglas Anderson 
318393c23ae3SDouglas Anderson 	/*
318493c23ae3SDouglas Anderson 	 * Continued paranoia to make sure we're in the state we expect.
318593c23ae3SDouglas Anderson 	 * This paranoia isn't really justified but it seems good to be safe.
318693c23ae3SDouglas Anderson 	 */
318757e10486SAddy Ke 	switch (host->state) {
318857e10486SAddy Ke 	case STATE_SENDING_DATA:
318957e10486SAddy Ke 	case STATE_DATA_BUSY:
319057e10486SAddy Ke 		/*
319157e10486SAddy Ke 		 * If DTO interrupt does NOT come in sending data state,
319257e10486SAddy Ke 		 * we should notify the driver to terminate current transfer
319357e10486SAddy Ke 		 * and report a data timeout to the core.
319457e10486SAddy Ke 		 */
319557e10486SAddy Ke 		host->data_status = SDMMC_INT_DRTO;
319657e10486SAddy Ke 		set_bit(EVENT_DATA_ERROR, &host->pending_events);
319757e10486SAddy Ke 		set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
319857e10486SAddy Ke 		tasklet_schedule(&host->tasklet);
319957e10486SAddy Ke 		break;
320057e10486SAddy Ke 	default:
320193c23ae3SDouglas Anderson 		dev_warn(host->dev, "Unexpected data timeout, state %d\n",
320293c23ae3SDouglas Anderson 			 host->state);
320357e10486SAddy Ke 		break;
320457e10486SAddy Ke 	}
320593c23ae3SDouglas Anderson 
320693c23ae3SDouglas Anderson exit:
320793c23ae3SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
320857e10486SAddy Ke }
320957e10486SAddy Ke 
3210c91eab4bSThomas Abraham #ifdef CONFIG_OF
dw_mci_parse_dt(struct dw_mci * host)3211c91eab4bSThomas Abraham static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3212c91eab4bSThomas Abraham {
3213c91eab4bSThomas Abraham 	struct dw_mci_board *pdata;
3214c91eab4bSThomas Abraham 	struct device *dev = host->dev;
3215e95baf13SArnd Bergmann 	const struct dw_mci_drv_data *drv_data = host->drv_data;
3216e8cc37b8SShawn Lin 	int ret;
32173c6d89eaSDoug Anderson 	u32 clock_frequency;
3218c91eab4bSThomas Abraham 
3219c91eab4bSThomas Abraham 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3220bf3707eaSBeomho Seo 	if (!pdata)
3221c91eab4bSThomas Abraham 		return ERR_PTR(-ENOMEM);
3222c91eab4bSThomas Abraham 
3223d6786fefSGuodong Xu 	/* find reset controller when exist */
3224a93d6f31SPhilipp Zabel 	pdata->rstc = devm_reset_control_get_optional_exclusive(dev, "reset");
3225baf6fe40SPhilipp Zabel 	if (IS_ERR(pdata->rstc))
3226baf6fe40SPhilipp Zabel 		return ERR_CAST(pdata->rstc);
3227d6786fefSGuodong Xu 
3228852ff5feSDavid Woods 	if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth))
32290e3a22c0SShawn Lin 		dev_info(dev,
32300e3a22c0SShawn Lin 			 "fifo-depth property not found, using value of FIFOTH register as default\n");
3231c91eab4bSThomas Abraham 
3232852ff5feSDavid Woods 	device_property_read_u32(dev, "card-detect-delay",
3233852ff5feSDavid Woods 				 &pdata->detect_delay_ms);
3234c91eab4bSThomas Abraham 
3235852ff5feSDavid Woods 	device_property_read_u32(dev, "data-addr", &host->data_addr_override);
3236a0361c1aSJun Nie 
3237852ff5feSDavid Woods 	if (device_property_present(dev, "fifo-watermark-aligned"))
3238d6fced83SJun Nie 		host->wm_aligned = true;
3239d6fced83SJun Nie 
3240852ff5feSDavid Woods 	if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency))
32413c6d89eaSDoug Anderson 		pdata->bus_hz = clock_frequency;
32423c6d89eaSDoug Anderson 
3243cb27a843SJames Hogan 	if (drv_data && drv_data->parse_dt) {
3244cb27a843SJames Hogan 		ret = drv_data->parse_dt(host);
3245800d78bfSThomas Abraham 		if (ret)
3246800d78bfSThomas Abraham 			return ERR_PTR(ret);
3247800d78bfSThomas Abraham 	}
3248800d78bfSThomas Abraham 
3249c91eab4bSThomas Abraham 	return pdata;
3250c91eab4bSThomas Abraham }
3251c91eab4bSThomas Abraham 
3252c91eab4bSThomas Abraham #else /* CONFIG_OF */
dw_mci_parse_dt(struct dw_mci * host)3253c91eab4bSThomas Abraham static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3254c91eab4bSThomas Abraham {
3255c91eab4bSThomas Abraham 	return ERR_PTR(-EINVAL);
3256c91eab4bSThomas Abraham }
3257c91eab4bSThomas Abraham #endif /* CONFIG_OF */
3258c91eab4bSThomas Abraham 
dw_mci_enable_cd(struct dw_mci * host)3259fa0c3283SDoug Anderson static void dw_mci_enable_cd(struct dw_mci *host)
3260fa0c3283SDoug Anderson {
3261fa0c3283SDoug Anderson 	unsigned long irqflags;
3262fa0c3283SDoug Anderson 	u32 temp;
3263fa0c3283SDoug Anderson 
3264e8cc37b8SShawn Lin 	/*
3265e8cc37b8SShawn Lin 	 * No need for CD if all slots have a non-error GPIO
3266e8cc37b8SShawn Lin 	 * as well as broken card detection is found.
3267e8cc37b8SShawn Lin 	 */
3268e47c0b96SJaehoon Chung 	if (host->slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3269e8cc37b8SShawn Lin 		return;
3270fa0c3283SDoug Anderson 
3271e47c0b96SJaehoon Chung 	if (mmc_gpio_get_cd(host->slot->mmc) < 0) {
3272fa0c3283SDoug Anderson 		spin_lock_irqsave(&host->irq_lock, irqflags);
3273fa0c3283SDoug Anderson 		temp = mci_readl(host, INTMASK);
3274fa0c3283SDoug Anderson 		temp  |= SDMMC_INT_CD;
3275fa0c3283SDoug Anderson 		mci_writel(host, INTMASK, temp);
3276fa0c3283SDoug Anderson 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
3277fa0c3283SDoug Anderson 	}
327858870241SJaehoon Chung }
3279fa0c3283SDoug Anderson 
dw_mci_probe(struct dw_mci * host)328062ca8034SShashidhar Hiremath int dw_mci_probe(struct dw_mci *host)
3281f95f3850SWill Newton {
3282e95baf13SArnd Bergmann 	const struct dw_mci_drv_data *drv_data = host->drv_data;
328362ca8034SShashidhar Hiremath 	int width, i, ret = 0;
3284f95f3850SWill Newton 	u32 fifo_size;
3285f95f3850SWill Newton 
3286c91eab4bSThomas Abraham 	if (!host->pdata) {
3287c91eab4bSThomas Abraham 		host->pdata = dw_mci_parse_dt(host);
3288308d2722SKrzysztof Kozlowski 		if (IS_ERR(host->pdata))
3289308d2722SKrzysztof Kozlowski 			return dev_err_probe(host->dev, PTR_ERR(host->pdata),
3290308d2722SKrzysztof Kozlowski 					     "platform data not available\n");
3291f95f3850SWill Newton 	}
3292f95f3850SWill Newton 
3293780f22afSSeungwon Jeon 	host->biu_clk = devm_clk_get(host->dev, "biu");
3294f90a0612SThomas Abraham 	if (IS_ERR(host->biu_clk)) {
3295f90a0612SThomas Abraham 		dev_dbg(host->dev, "biu clock not available\n");
3296f90a0612SThomas Abraham 	} else {
3297f90a0612SThomas Abraham 		ret = clk_prepare_enable(host->biu_clk);
3298f90a0612SThomas Abraham 		if (ret) {
3299f90a0612SThomas Abraham 			dev_err(host->dev, "failed to enable biu clock\n");
3300f90a0612SThomas Abraham 			return ret;
3301f90a0612SThomas Abraham 		}
3302f95f3850SWill Newton 	}
3303f95f3850SWill Newton 
3304780f22afSSeungwon Jeon 	host->ciu_clk = devm_clk_get(host->dev, "ciu");
3305f90a0612SThomas Abraham 	if (IS_ERR(host->ciu_clk)) {
3306f90a0612SThomas Abraham 		dev_dbg(host->dev, "ciu clock not available\n");
33073c6d89eaSDoug Anderson 		host->bus_hz = host->pdata->bus_hz;
3308f90a0612SThomas Abraham 	} else {
3309f90a0612SThomas Abraham 		ret = clk_prepare_enable(host->ciu_clk);
3310f90a0612SThomas Abraham 		if (ret) {
3311f90a0612SThomas Abraham 			dev_err(host->dev, "failed to enable ciu clock\n");
3312f90a0612SThomas Abraham 			goto err_clk_biu;
3313f90a0612SThomas Abraham 		}
3314f90a0612SThomas Abraham 
33153c6d89eaSDoug Anderson 		if (host->pdata->bus_hz) {
33163c6d89eaSDoug Anderson 			ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
33173c6d89eaSDoug Anderson 			if (ret)
33183c6d89eaSDoug Anderson 				dev_warn(host->dev,
3319612de4c1SJaehoon Chung 					 "Unable to set bus rate to %uHz\n",
33203c6d89eaSDoug Anderson 					 host->pdata->bus_hz);
33213c6d89eaSDoug Anderson 		}
3322f90a0612SThomas Abraham 		host->bus_hz = clk_get_rate(host->ciu_clk);
33233c6d89eaSDoug Anderson 	}
3324f90a0612SThomas Abraham 
3325612de4c1SJaehoon Chung 	if (!host->bus_hz) {
3326612de4c1SJaehoon Chung 		dev_err(host->dev,
3327612de4c1SJaehoon Chung 			"Platform data must supply bus speed\n");
3328612de4c1SJaehoon Chung 		ret = -ENODEV;
3329612de4c1SJaehoon Chung 		goto err_clk_ciu;
3330612de4c1SJaehoon Chung 	}
3331612de4c1SJaehoon Chung 
3332baf6fe40SPhilipp Zabel 	if (host->pdata->rstc) {
3333941e372dSliwei 		reset_control_assert(host->pdata->rstc);
3334941e372dSliwei 		usleep_range(10, 50);
3335941e372dSliwei 		reset_control_deassert(host->pdata->rstc);
3336941e372dSliwei 	}
3337941e372dSliwei 
3338002f0d5cSYuvaraj Kumar C D 	if (drv_data && drv_data->init) {
3339002f0d5cSYuvaraj Kumar C D 		ret = drv_data->init(host);
3340002f0d5cSYuvaraj Kumar C D 		if (ret) {
3341002f0d5cSYuvaraj Kumar C D 			dev_err(host->dev,
3342002f0d5cSYuvaraj Kumar C D 				"implementation specific init failed\n");
3343002f0d5cSYuvaraj Kumar C D 			goto err_clk_ciu;
3344002f0d5cSYuvaraj Kumar C D 		}
3345002f0d5cSYuvaraj Kumar C D 	}
3346002f0d5cSYuvaraj Kumar C D 
334737977729SKees Cook 	timer_setup(&host->cmd11_timer, dw_mci_cmd11_timer, 0);
334837977729SKees Cook 	timer_setup(&host->cto_timer, dw_mci_cto_timer, 0);
334937977729SKees Cook 	timer_setup(&host->dto_timer, dw_mci_dto_timer, 0);
335057e10486SAddy Ke 
3351f95f3850SWill Newton 	spin_lock_init(&host->lock);
3352f8c58c11SDoug Anderson 	spin_lock_init(&host->irq_lock);
3353f95f3850SWill Newton 	INIT_LIST_HEAD(&host->queue);
3354f95f3850SWill Newton 
33552b8ac062SVincent Whitchurch 	dw_mci_init_fault(host);
33562b8ac062SVincent Whitchurch 
3357f95f3850SWill Newton 	/*
3358f95f3850SWill Newton 	 * Get the host data width - this assumes that HCON has been set with
3359f95f3850SWill Newton 	 * the correct values.
3360f95f3850SWill Newton 	 */
336170692752SShawn Lin 	i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
3362f95f3850SWill Newton 	if (!i) {
3363f95f3850SWill Newton 		host->push_data = dw_mci_push_data16;
3364f95f3850SWill Newton 		host->pull_data = dw_mci_pull_data16;
3365f95f3850SWill Newton 		width = 16;
3366f95f3850SWill Newton 		host->data_shift = 1;
3367f95f3850SWill Newton 	} else if (i == 2) {
3368f95f3850SWill Newton 		host->push_data = dw_mci_push_data64;
3369f95f3850SWill Newton 		host->pull_data = dw_mci_pull_data64;
3370f95f3850SWill Newton 		width = 64;
3371f95f3850SWill Newton 		host->data_shift = 3;
3372f95f3850SWill Newton 	} else {
3373f95f3850SWill Newton 		/* Check for a reserved value, and warn if it is */
3374f95f3850SWill Newton 		WARN((i != 1),
3375f95f3850SWill Newton 		     "HCON reports a reserved host data width!\n"
3376f95f3850SWill Newton 		     "Defaulting to 32-bit access.\n");
3377f95f3850SWill Newton 		host->push_data = dw_mci_push_data32;
3378f95f3850SWill Newton 		host->pull_data = dw_mci_pull_data32;
3379f95f3850SWill Newton 		width = 32;
3380f95f3850SWill Newton 		host->data_shift = 2;
3381f95f3850SWill Newton 	}
3382f95f3850SWill Newton 
3383f95f3850SWill Newton 	/* Reset all blocks */
33843744415cSShawn Lin 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
33853744415cSShawn Lin 		ret = -ENODEV;
33863744415cSShawn Lin 		goto err_clk_ciu;
33873744415cSShawn Lin 	}
3388141a712aSSeungwon Jeon 
3389141a712aSSeungwon Jeon 	host->dma_ops = host->pdata->dma_ops;
3390141a712aSSeungwon Jeon 	dw_mci_init_dma(host);
3391f95f3850SWill Newton 
3392f95f3850SWill Newton 	/* Clear the interrupts for the host controller */
3393f95f3850SWill Newton 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3394f95f3850SWill Newton 	mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3395f95f3850SWill Newton 
3396f95f3850SWill Newton 	/* Put in max timeout */
3397f95f3850SWill Newton 	mci_writel(host, TMOUT, 0xFFFFFFFF);
3398f95f3850SWill Newton 
3399f95f3850SWill Newton 	/*
3400f95f3850SWill Newton 	 * FIFO threshold settings  RxMark  = fifo_size / 2 - 1,
3401f95f3850SWill Newton 	 *                          Tx Mark = fifo_size / 2 DMA Size = 8
3402f95f3850SWill Newton 	 */
3403b86d8253SJames Hogan 	if (!host->pdata->fifo_depth) {
3404b86d8253SJames Hogan 		/*
3405b86d8253SJames Hogan 		 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3406b86d8253SJames Hogan 		 * have been overwritten by the bootloader, just like we're
3407b86d8253SJames Hogan 		 * about to do, so if you know the value for your hardware, you
3408b86d8253SJames Hogan 		 * should put it in the platform data.
3409b86d8253SJames Hogan 		 */
3410f95f3850SWill Newton 		fifo_size = mci_readl(host, FIFOTH);
34118234e869SJaehoon Chung 		fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
3412b86d8253SJames Hogan 	} else {
3413b86d8253SJames Hogan 		fifo_size = host->pdata->fifo_depth;
3414b86d8253SJames Hogan 	}
3415b86d8253SJames Hogan 	host->fifo_depth = fifo_size;
341652426899SSeungwon Jeon 	host->fifoth_val =
341752426899SSeungwon Jeon 		SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
3418e61cf118SJaehoon Chung 	mci_writel(host, FIFOTH, host->fifoth_val);
3419f95f3850SWill Newton 
3420f95f3850SWill Newton 	/* disable clock to CIU */
3421f95f3850SWill Newton 	mci_writel(host, CLKENA, 0);
3422f95f3850SWill Newton 	mci_writel(host, CLKSRC, 0);
3423f95f3850SWill Newton 
342463008768SJames Hogan 	/*
342563008768SJames Hogan 	 * In 2.40a spec, Data offset is changed.
342663008768SJames Hogan 	 * Need to check the version-id and set data-offset for DATA register.
342763008768SJames Hogan 	 */
342863008768SJames Hogan 	host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
342963008768SJames Hogan 	dev_info(host->dev, "Version ID is %04x\n", host->verid);
343063008768SJames Hogan 
3431a0361c1aSJun Nie 	if (host->data_addr_override)
3432a0361c1aSJun Nie 		host->fifo_reg = host->regs + host->data_addr_override;
3433a0361c1aSJun Nie 	else if (host->verid < DW_MMC_240A)
343476184ac1SBen Dooks 		host->fifo_reg = host->regs + DATA_OFFSET;
343563008768SJames Hogan 	else
343676184ac1SBen Dooks 		host->fifo_reg = host->regs + DATA_240A_OFFSET;
343763008768SJames Hogan 
34386078df15SEmil Renner Berthing 	tasklet_setup(&host->tasklet, dw_mci_tasklet_func);
3439780f22afSSeungwon Jeon 	ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3440780f22afSSeungwon Jeon 			       host->irq_flags, "dw-mci", host);
3441f95f3850SWill Newton 	if (ret)
34426130e7a9SDoug Anderson 		goto err_dmaunmap;
3443f95f3850SWill Newton 
3444d30a8f7bSJaehoon Chung 	/*
3445fa0c3283SDoug Anderson 	 * Enable interrupts for command done, data over, data empty,
34462da1d7f2SYuvaraj CD 	 * receive ready and error such as transmit, receive timeout, crc error
34472da1d7f2SYuvaraj CD 	 */
34482da1d7f2SYuvaraj CD 	mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
34492da1d7f2SYuvaraj CD 		   SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3450fa0c3283SDoug Anderson 		   DW_MCI_ERROR_FLAGS);
34510e3a22c0SShawn Lin 	/* Enable mci interrupt */
34520e3a22c0SShawn Lin 	mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
34532da1d7f2SYuvaraj CD 
34540e3a22c0SShawn Lin 	dev_info(host->dev,
34550e3a22c0SShawn Lin 		 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
34562da1d7f2SYuvaraj CD 		 host->irq, width, fifo_size);
34572da1d7f2SYuvaraj CD 
3458f95f3850SWill Newton 	/* We need at least one slot to succeed */
3459e4a65ef7SJaehoon Chung 	ret = dw_mci_init_slot(host);
346058870241SJaehoon Chung 	if (ret) {
34611c2215b7SThomas Abraham 		dev_dbg(host->dev, "slot %d init failed\n", i);
34626130e7a9SDoug Anderson 		goto err_dmaunmap;
3463f95f3850SWill Newton 	}
3464f95f3850SWill Newton 
3465b793f658SDoug Anderson 	/* Now that slots are all setup, we can enable card detect */
3466b793f658SDoug Anderson 	dw_mci_enable_cd(host);
3467b793f658SDoug Anderson 
3468f95f3850SWill Newton 	return 0;
3469f95f3850SWill Newton 
3470f95f3850SWill Newton err_dmaunmap:
3471f95f3850SWill Newton 	if (host->use_dma && host->dma_ops->exit)
3472f95f3850SWill Newton 		host->dma_ops->exit(host);
3473f90a0612SThomas Abraham 
3474d6786fefSGuodong Xu 	reset_control_assert(host->pdata->rstc);
3475d6786fefSGuodong Xu 
3476f90a0612SThomas Abraham err_clk_ciu:
3477f90a0612SThomas Abraham 	clk_disable_unprepare(host->ciu_clk);
3478780f22afSSeungwon Jeon 
3479f90a0612SThomas Abraham err_clk_biu:
3480f90a0612SThomas Abraham 	clk_disable_unprepare(host->biu_clk);
3481780f22afSSeungwon Jeon 
3482f95f3850SWill Newton 	return ret;
3483f95f3850SWill Newton }
348462ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_probe);
3485f95f3850SWill Newton 
dw_mci_remove(struct dw_mci * host)348662ca8034SShashidhar Hiremath void dw_mci_remove(struct dw_mci *host)
3487f95f3850SWill Newton {
3488e4a65ef7SJaehoon Chung 	dev_dbg(host->dev, "remove slot\n");
3489b23475faSJaehoon Chung 	if (host->slot)
3490e4a65ef7SJaehoon Chung 		dw_mci_cleanup_slot(host->slot);
3491f95f3850SWill Newton 
3492048fd7e6SPrabu Thangamuthu 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3493048fd7e6SPrabu Thangamuthu 	mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3494048fd7e6SPrabu Thangamuthu 
3495f95f3850SWill Newton 	/* disable clock to CIU */
3496f95f3850SWill Newton 	mci_writel(host, CLKENA, 0);
3497f95f3850SWill Newton 	mci_writel(host, CLKSRC, 0);
3498f95f3850SWill Newton 
3499f95f3850SWill Newton 	if (host->use_dma && host->dma_ops->exit)
3500f95f3850SWill Newton 		host->dma_ops->exit(host);
3501f95f3850SWill Newton 
3502d6786fefSGuodong Xu 	reset_control_assert(host->pdata->rstc);
3503d6786fefSGuodong Xu 
3504f90a0612SThomas Abraham 	clk_disable_unprepare(host->ciu_clk);
3505f90a0612SThomas Abraham 	clk_disable_unprepare(host->biu_clk);
3506f95f3850SWill Newton }
350762ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_remove);
350862ca8034SShashidhar Hiremath 
350962ca8034SShashidhar Hiremath 
3510f95f3850SWill Newton 
3511e9ed8835SShawn Lin #ifdef CONFIG_PM
dw_mci_runtime_suspend(struct device * dev)3512ed24e1ffSShawn Lin int dw_mci_runtime_suspend(struct device *dev)
3513f95f3850SWill Newton {
3514ed24e1ffSShawn Lin 	struct dw_mci *host = dev_get_drvdata(dev);
3515ed24e1ffSShawn Lin 
35163fc7eaefSShawn Lin 	if (host->use_dma && host->dma_ops->exit)
35173fc7eaefSShawn Lin 		host->dma_ops->exit(host);
35183fc7eaefSShawn Lin 
3519ed24e1ffSShawn Lin 	clk_disable_unprepare(host->ciu_clk);
3520ed24e1ffSShawn Lin 
352142f989c0SJaehoon Chung 	if (host->slot &&
352242f989c0SJaehoon Chung 	    (mmc_can_gpio_cd(host->slot->mmc) ||
352342f989c0SJaehoon Chung 	     !mmc_card_is_removable(host->slot->mmc)))
3524ed24e1ffSShawn Lin 		clk_disable_unprepare(host->biu_clk);
3525ed24e1ffSShawn Lin 
3526f95f3850SWill Newton 	return 0;
3527f95f3850SWill Newton }
3528ed24e1ffSShawn Lin EXPORT_SYMBOL(dw_mci_runtime_suspend);
3529f95f3850SWill Newton 
dw_mci_runtime_resume(struct device * dev)3530ed24e1ffSShawn Lin int dw_mci_runtime_resume(struct device *dev)
3531f95f3850SWill Newton {
3532b23475faSJaehoon Chung 	int ret = 0;
3533ed24e1ffSShawn Lin 	struct dw_mci *host = dev_get_drvdata(dev);
3534f95f3850SWill Newton 
353542f989c0SJaehoon Chung 	if (host->slot &&
353642f989c0SJaehoon Chung 	    (mmc_can_gpio_cd(host->slot->mmc) ||
353742f989c0SJaehoon Chung 	     !mmc_card_is_removable(host->slot->mmc))) {
3538ed24e1ffSShawn Lin 		ret = clk_prepare_enable(host->biu_clk);
3539ed24e1ffSShawn Lin 		if (ret)
3540e61cf118SJaehoon Chung 			return ret;
3541e61cf118SJaehoon Chung 	}
3542e61cf118SJaehoon Chung 
3543ed24e1ffSShawn Lin 	ret = clk_prepare_enable(host->ciu_clk);
3544ed24e1ffSShawn Lin 	if (ret)
3545df9bcc2bSJoonyoung Shim 		goto err;
3546df9bcc2bSJoonyoung Shim 
3547df9bcc2bSJoonyoung Shim 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3548df9bcc2bSJoonyoung Shim 		clk_disable_unprepare(host->ciu_clk);
3549df9bcc2bSJoonyoung Shim 		ret = -ENODEV;
3550df9bcc2bSJoonyoung Shim 		goto err;
3551df9bcc2bSJoonyoung Shim 	}
3552ed24e1ffSShawn Lin 
35533bfe619dSJonathan Kliegman 	if (host->use_dma && host->dma_ops->init)
3554141a712aSSeungwon Jeon 		host->dma_ops->init(host);
3555141a712aSSeungwon Jeon 
355652426899SSeungwon Jeon 	/*
355752426899SSeungwon Jeon 	 * Restore the initial value at FIFOTH register
355852426899SSeungwon Jeon 	 * And Invalidate the prev_blksz with zero
355952426899SSeungwon Jeon 	 */
3560e61cf118SJaehoon Chung 	mci_writel(host, FIFOTH, host->fifoth_val);
356152426899SSeungwon Jeon 	host->prev_blksz = 0;
3562e61cf118SJaehoon Chung 
35632eb2944fSDoug Anderson 	/* Put in max timeout */
35642eb2944fSDoug Anderson 	mci_writel(host, TMOUT, 0xFFFFFFFF);
35652eb2944fSDoug Anderson 
3566e61cf118SJaehoon Chung 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3567e61cf118SJaehoon Chung 	mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3568e61cf118SJaehoon Chung 		   SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3569fa0c3283SDoug Anderson 		   DW_MCI_ERROR_FLAGS);
3570e61cf118SJaehoon Chung 	mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3571e61cf118SJaehoon Chung 
35720e3a22c0SShawn Lin 
35734a835afdSWen Zhiwei 	if (host->slot && host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
3574e47c0b96SJaehoon Chung 		dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios);
3575e9748e03SZiyuan Xu 
3576e9748e03SZiyuan Xu 	/* Force setup bus to guarantee available clock output */
3577e47c0b96SJaehoon Chung 	dw_mci_setup_bus(host->slot, true);
3578fa0c3283SDoug Anderson 
35797c526608SUlf Hansson 	/* Re-enable SDIO interrupts. */
35807c526608SUlf Hansson 	if (sdio_irq_claimed(host->slot->mmc))
35817c526608SUlf Hansson 		__dw_mci_enable_sdio_irq(host->slot, 1);
35827c526608SUlf Hansson 
3583fa0c3283SDoug Anderson 	/* Now that slots are all setup, we can enable card detect */
3584fa0c3283SDoug Anderson 	dw_mci_enable_cd(host);
3585fa0c3283SDoug Anderson 
3586df9bcc2bSJoonyoung Shim 	return 0;
3587df9bcc2bSJoonyoung Shim 
3588df9bcc2bSJoonyoung Shim err:
358942f989c0SJaehoon Chung 	if (host->slot &&
359042f989c0SJaehoon Chung 	    (mmc_can_gpio_cd(host->slot->mmc) ||
359142f989c0SJaehoon Chung 	     !mmc_card_is_removable(host->slot->mmc)))
3592df9bcc2bSJoonyoung Shim 		clk_disable_unprepare(host->biu_clk);
3593df9bcc2bSJoonyoung Shim 
35941f5c51d7SShawn Lin 	return ret;
35951f5c51d7SShawn Lin }
3596e9ed8835SShawn Lin EXPORT_SYMBOL(dw_mci_runtime_resume);
3597e9ed8835SShawn Lin #endif /* CONFIG_PM */
35986fe8890dSJaehoon Chung 
dw_mci_init(void)3599f95f3850SWill Newton static int __init dw_mci_init(void)
3600f95f3850SWill Newton {
36018e1c4e4dSSachin Kamat 	pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
360262ca8034SShashidhar Hiremath 	return 0;
3603f95f3850SWill Newton }
3604f95f3850SWill Newton 
dw_mci_exit(void)3605f95f3850SWill Newton static void __exit dw_mci_exit(void)
3606f95f3850SWill Newton {
3607f95f3850SWill Newton }
3608f95f3850SWill Newton 
3609f95f3850SWill Newton module_init(dw_mci_init);
3610f95f3850SWill Newton module_exit(dw_mci_exit);
3611f95f3850SWill Newton 
3612f95f3850SWill Newton MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3613f95f3850SWill Newton MODULE_AUTHOR("NXP Semiconductor VietNam");
3614f95f3850SWill Newton MODULE_AUTHOR("Imagination Technologies Ltd");
3615f95f3850SWill Newton MODULE_LICENSE("GPL v2");
3616