xref: /openbmc/linux/drivers/mmc/host/dw_mmc.c (revision b98e7e8d)
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>
3855a6ceb2SDoug Anderson #include <linux/of_gpio.h>
39bf626e55SZhangfei Gao #include <linux/mmc/slot-gpio.h>
40f95f3850SWill Newton 
41f95f3850SWill Newton #include "dw_mmc.h"
42f95f3850SWill Newton 
43f95f3850SWill Newton /* Common flag combinations */
443f7eec62SJaehoon Chung #define DW_MCI_DATA_ERROR_FLAGS	(SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
45f95f3850SWill Newton 				 SDMMC_INT_HTO | SDMMC_INT_SBE  | \
467a3c5677SDoug Anderson 				 SDMMC_INT_EBE | SDMMC_INT_HLE)
47f95f3850SWill Newton #define DW_MCI_CMD_ERROR_FLAGS	(SDMMC_INT_RTO | SDMMC_INT_RCRC | \
487a3c5677SDoug Anderson 				 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
49f95f3850SWill Newton #define DW_MCI_ERROR_FLAGS	(DW_MCI_DATA_ERROR_FLAGS | \
507a3c5677SDoug Anderson 				 DW_MCI_CMD_ERROR_FLAGS)
51f95f3850SWill Newton #define DW_MCI_SEND_STATUS	1
52f95f3850SWill Newton #define DW_MCI_RECV_STATUS	2
53f95f3850SWill Newton #define DW_MCI_DMA_THRESHOLD	16
54f95f3850SWill Newton 
551f44a2a5SSeungwon Jeon #define DW_MCI_FREQ_MAX	200000000	/* unit: HZ */
5672e83577SJaehoon Chung #define DW_MCI_FREQ_MIN	100000		/* unit: HZ */
571f44a2a5SSeungwon Jeon 
58fc79a4d6SJoonyoung Shim #define IDMAC_INT_CLR		(SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
59fc79a4d6SJoonyoung Shim 				 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
60fc79a4d6SJoonyoung Shim 				 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
61fc79a4d6SJoonyoung Shim 				 SDMMC_IDMAC_INT_TI)
62fc79a4d6SJoonyoung Shim 
63cc190d4cSShawn Lin #define DESC_RING_BUF_SZ	PAGE_SIZE
64cc190d4cSShawn Lin 
6569d99fdcSPrabu Thangamuthu struct idmac_desc_64addr {
6669d99fdcSPrabu Thangamuthu 	u32		des0;	/* Control Descriptor */
67b6d2d81cSShawn Lin #define IDMAC_OWN_CLR64(x) \
68b6d2d81cSShawn Lin 	!((x) & cpu_to_le32(IDMAC_DES0_OWN))
6969d99fdcSPrabu Thangamuthu 
7069d99fdcSPrabu Thangamuthu 	u32		des1;	/* Reserved */
7169d99fdcSPrabu Thangamuthu 
7269d99fdcSPrabu Thangamuthu 	u32		des2;	/*Buffer sizes */
7369d99fdcSPrabu Thangamuthu #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
746687c42fSBen Dooks 	((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
756687c42fSBen Dooks 	 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
7669d99fdcSPrabu Thangamuthu 
7769d99fdcSPrabu Thangamuthu 	u32		des3;	/* Reserved */
7869d99fdcSPrabu Thangamuthu 
7969d99fdcSPrabu Thangamuthu 	u32		des4;	/* Lower 32-bits of Buffer Address Pointer 1*/
8069d99fdcSPrabu Thangamuthu 	u32		des5;	/* Upper 32-bits of Buffer Address Pointer 1*/
8169d99fdcSPrabu Thangamuthu 
8269d99fdcSPrabu Thangamuthu 	u32		des6;	/* Lower 32-bits of Next Descriptor Address */
8369d99fdcSPrabu Thangamuthu 	u32		des7;	/* Upper 32-bits of Next Descriptor Address */
8469d99fdcSPrabu Thangamuthu };
8569d99fdcSPrabu Thangamuthu 
86f95f3850SWill Newton struct idmac_desc {
876687c42fSBen Dooks 	__le32		des0;	/* Control Descriptor */
88f95f3850SWill Newton #define IDMAC_DES0_DIC	BIT(1)
89f95f3850SWill Newton #define IDMAC_DES0_LD	BIT(2)
90f95f3850SWill Newton #define IDMAC_DES0_FD	BIT(3)
91f95f3850SWill Newton #define IDMAC_DES0_CH	BIT(4)
92f95f3850SWill Newton #define IDMAC_DES0_ER	BIT(5)
93f95f3850SWill Newton #define IDMAC_DES0_CES	BIT(30)
94f95f3850SWill Newton #define IDMAC_DES0_OWN	BIT(31)
95f95f3850SWill Newton 
966687c42fSBen Dooks 	__le32		des1;	/* Buffer sizes */
97f95f3850SWill Newton #define IDMAC_SET_BUFFER1_SIZE(d, s) \
98e5306c3aSBen Dooks 	((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
99f95f3850SWill Newton 
1006687c42fSBen Dooks 	__le32		des2;	/* buffer 1 physical address */
101f95f3850SWill Newton 
1026687c42fSBen Dooks 	__le32		des3;	/* buffer 2 physical address */
103f95f3850SWill Newton };
1045959b32eSAlexey Brodkin 
1055959b32eSAlexey Brodkin /* Each descriptor can transfer up to 4KB of data in chained mode */
1065959b32eSAlexey Brodkin #define DW_MCI_DESC_DATA_LENGTH	0x1000
107f95f3850SWill Newton 
108f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS)
dw_mci_req_show(struct seq_file * s,void * v)109f95f3850SWill Newton static int dw_mci_req_show(struct seq_file *s, void *v)
110f95f3850SWill Newton {
111f95f3850SWill Newton 	struct dw_mci_slot *slot = s->private;
112f95f3850SWill Newton 	struct mmc_request *mrq;
113f95f3850SWill Newton 	struct mmc_command *cmd;
114f95f3850SWill Newton 	struct mmc_command *stop;
115f95f3850SWill Newton 	struct mmc_data	*data;
116f95f3850SWill Newton 
117f95f3850SWill Newton 	/* Make sure we get a consistent snapshot */
118f95f3850SWill Newton 	spin_lock_bh(&slot->host->lock);
119f95f3850SWill Newton 	mrq = slot->mrq;
120f95f3850SWill Newton 
121f95f3850SWill Newton 	if (mrq) {
122f95f3850SWill Newton 		cmd = mrq->cmd;
123f95f3850SWill Newton 		data = mrq->data;
124f95f3850SWill Newton 		stop = mrq->stop;
125f95f3850SWill Newton 
126f95f3850SWill Newton 		if (cmd)
127f95f3850SWill Newton 			seq_printf(s,
128f95f3850SWill Newton 				   "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
129f95f3850SWill Newton 				   cmd->opcode, cmd->arg, cmd->flags,
130f95f3850SWill Newton 				   cmd->resp[0], cmd->resp[1], cmd->resp[2],
131f95f3850SWill Newton 				   cmd->resp[2], cmd->error);
132f95f3850SWill Newton 		if (data)
133f95f3850SWill Newton 			seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
134f95f3850SWill Newton 				   data->bytes_xfered, data->blocks,
135f95f3850SWill Newton 				   data->blksz, data->flags, data->error);
136f95f3850SWill Newton 		if (stop)
137f95f3850SWill Newton 			seq_printf(s,
138f95f3850SWill Newton 				   "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
139f95f3850SWill Newton 				   stop->opcode, stop->arg, stop->flags,
140f95f3850SWill Newton 				   stop->resp[0], stop->resp[1], stop->resp[2],
141f95f3850SWill Newton 				   stop->resp[2], stop->error);
142f95f3850SWill Newton 	}
143f95f3850SWill Newton 
144f95f3850SWill Newton 	spin_unlock_bh(&slot->host->lock);
145f95f3850SWill Newton 
146f95f3850SWill Newton 	return 0;
147f95f3850SWill Newton }
14864c1412bSShawn Lin DEFINE_SHOW_ATTRIBUTE(dw_mci_req);
149f95f3850SWill Newton 
dw_mci_regs_show(struct seq_file * s,void * v)150f95f3850SWill Newton static int dw_mci_regs_show(struct seq_file *s, void *v)
151f95f3850SWill Newton {
15221657ebdSJaehoon Chung 	struct dw_mci *host = s->private;
15321657ebdSJaehoon Chung 
1545b43df8bSShawn Lin 	pm_runtime_get_sync(host->dev);
1555b43df8bSShawn Lin 
15621657ebdSJaehoon Chung 	seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
15721657ebdSJaehoon Chung 	seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
15821657ebdSJaehoon Chung 	seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
15921657ebdSJaehoon Chung 	seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
16021657ebdSJaehoon Chung 	seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
16121657ebdSJaehoon Chung 	seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
162f95f3850SWill Newton 
1635b43df8bSShawn Lin 	pm_runtime_put_autosuspend(host->dev);
1645b43df8bSShawn Lin 
165f95f3850SWill Newton 	return 0;
166f95f3850SWill Newton }
16764c1412bSShawn Lin DEFINE_SHOW_ATTRIBUTE(dw_mci_regs);
168f95f3850SWill Newton 
dw_mci_init_debugfs(struct dw_mci_slot * slot)169f95f3850SWill Newton static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
170f95f3850SWill Newton {
171f95f3850SWill Newton 	struct mmc_host	*mmc = slot->mmc;
172f95f3850SWill Newton 	struct dw_mci *host = slot->host;
173f95f3850SWill Newton 	struct dentry *root;
174f95f3850SWill Newton 
175f95f3850SWill Newton 	root = mmc->debugfs_root;
176f95f3850SWill Newton 	if (!root)
177f95f3850SWill Newton 		return;
178f95f3850SWill Newton 
179fcac1527SGreg Kroah-Hartman 	debugfs_create_file("regs", S_IRUSR, root, host, &dw_mci_regs_fops);
180fcac1527SGreg Kroah-Hartman 	debugfs_create_file("req", S_IRUSR, root, slot, &dw_mci_req_fops);
181118e1118SGeert Uytterhoeven 	debugfs_create_u32("state", S_IRUSR, root, &host->state);
1820c40c1beSGeert Uytterhoeven 	debugfs_create_xul("pending_events", S_IRUSR, root,
1830c40c1beSGeert Uytterhoeven 			   &host->pending_events);
1840c40c1beSGeert Uytterhoeven 	debugfs_create_xul("completed_events", S_IRUSR, root,
1850c40c1beSGeert Uytterhoeven 			   &host->completed_events);
1862b8ac062SVincent Whitchurch #ifdef CONFIG_FAULT_INJECTION
1872b8ac062SVincent Whitchurch 	fault_create_debugfs_attr("fail_data_crc", root, &host->fail_data_crc);
1882b8ac062SVincent Whitchurch #endif
189f95f3850SWill Newton }
190f95f3850SWill Newton #endif /* defined(CONFIG_DEBUG_FS) */
191f95f3850SWill Newton 
dw_mci_ctrl_reset(struct dw_mci * host,u32 reset)1928e6db1f6SShawn Lin static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
1938e6db1f6SShawn Lin {
1948e6db1f6SShawn Lin 	u32 ctrl;
1958e6db1f6SShawn Lin 
1968e6db1f6SShawn Lin 	ctrl = mci_readl(host, CTRL);
1978e6db1f6SShawn Lin 	ctrl |= reset;
1988e6db1f6SShawn Lin 	mci_writel(host, CTRL, ctrl);
1998e6db1f6SShawn Lin 
2008e6db1f6SShawn Lin 	/* wait till resets clear */
2018e6db1f6SShawn Lin 	if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
2028e6db1f6SShawn Lin 				      !(ctrl & reset),
2038e6db1f6SShawn Lin 				      1, 500 * USEC_PER_MSEC)) {
2048e6db1f6SShawn Lin 		dev_err(host->dev,
2058e6db1f6SShawn Lin 			"Timeout resetting block (ctrl reset %#x)\n",
2068e6db1f6SShawn Lin 			ctrl & reset);
2078e6db1f6SShawn Lin 		return false;
2088e6db1f6SShawn Lin 	}
2098e6db1f6SShawn Lin 
2108e6db1f6SShawn Lin 	return true;
2118e6db1f6SShawn Lin }
21201730558SDoug Anderson 
dw_mci_wait_while_busy(struct dw_mci * host,u32 cmd_flags)2134dba18deSShawn Lin static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
2144dba18deSShawn Lin {
2154dba18deSShawn Lin 	u32 status;
2164dba18deSShawn Lin 
2174dba18deSShawn Lin 	/*
2184dba18deSShawn Lin 	 * Databook says that before issuing a new data transfer command
2194dba18deSShawn Lin 	 * we need to check to see if the card is busy.  Data transfer commands
2204dba18deSShawn Lin 	 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
2214dba18deSShawn Lin 	 *
2224dba18deSShawn Lin 	 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
2234dba18deSShawn Lin 	 * expected.
2244dba18deSShawn Lin 	 */
2254dba18deSShawn Lin 	if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
2264dba18deSShawn Lin 	    !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
2274dba18deSShawn Lin 		if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
2284dba18deSShawn Lin 					      status,
2294dba18deSShawn Lin 					      !(status & SDMMC_STATUS_BUSY),
2304dba18deSShawn Lin 					      10, 500 * USEC_PER_MSEC))
2314dba18deSShawn Lin 			dev_err(host->dev, "Busy; trying anyway\n");
2324dba18deSShawn Lin 	}
2334dba18deSShawn Lin }
2344dba18deSShawn Lin 
mci_send_cmd(struct dw_mci_slot * slot,u32 cmd,u32 arg)2354dba18deSShawn Lin static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
2364dba18deSShawn Lin {
2374dba18deSShawn Lin 	struct dw_mci *host = slot->host;
2384dba18deSShawn Lin 	unsigned int cmd_status = 0;
2394dba18deSShawn Lin 
2404dba18deSShawn Lin 	mci_writel(host, CMDARG, arg);
2414dba18deSShawn Lin 	wmb(); /* drain writebuffer */
2424dba18deSShawn Lin 	dw_mci_wait_while_busy(host, cmd);
2434dba18deSShawn Lin 	mci_writel(host, CMD, SDMMC_CMD_START | cmd);
2444dba18deSShawn Lin 
2454dba18deSShawn Lin 	if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
2464dba18deSShawn Lin 				      !(cmd_status & SDMMC_CMD_START),
2474dba18deSShawn Lin 				      1, 500 * USEC_PER_MSEC))
2484dba18deSShawn Lin 		dev_err(&slot->mmc->class_dev,
2494dba18deSShawn Lin 			"Timeout sending command (cmd %#x arg %#x status %#x)\n",
2504dba18deSShawn Lin 			cmd, arg, cmd_status);
2514dba18deSShawn Lin }
2524dba18deSShawn Lin 
dw_mci_prepare_command(struct mmc_host * mmc,struct mmc_command * cmd)253f95f3850SWill Newton static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
254f95f3850SWill Newton {
255800d78bfSThomas Abraham 	struct dw_mci_slot *slot = mmc_priv(mmc);
25601730558SDoug Anderson 	struct dw_mci *host = slot->host;
257f95f3850SWill Newton 	u32 cmdr;
258f95f3850SWill Newton 
2590e3a22c0SShawn Lin 	cmd->error = -EINPROGRESS;
260f95f3850SWill Newton 	cmdr = cmd->opcode;
261f95f3850SWill Newton 
26290c2143aSSeungwon Jeon 	if (cmd->opcode == MMC_STOP_TRANSMISSION ||
26390c2143aSSeungwon Jeon 	    cmd->opcode == MMC_GO_IDLE_STATE ||
26490c2143aSSeungwon Jeon 	    cmd->opcode == MMC_GO_INACTIVE_STATE ||
26590c2143aSSeungwon Jeon 	    (cmd->opcode == SD_IO_RW_DIRECT &&
26690c2143aSSeungwon Jeon 	     ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
267f95f3850SWill Newton 		cmdr |= SDMMC_CMD_STOP;
2684a1b27adSJaehoon Chung 	else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
269f95f3850SWill Newton 		cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
270f95f3850SWill Newton 
27101730558SDoug Anderson 	if (cmd->opcode == SD_SWITCH_VOLTAGE) {
27201730558SDoug Anderson 		u32 clk_en_a;
27301730558SDoug Anderson 
27401730558SDoug Anderson 		/* Special bit makes CMD11 not die */
27501730558SDoug Anderson 		cmdr |= SDMMC_CMD_VOLT_SWITCH;
27601730558SDoug Anderson 
27701730558SDoug Anderson 		/* Change state to continue to handle CMD11 weirdness */
27801730558SDoug Anderson 		WARN_ON(slot->host->state != STATE_SENDING_CMD);
27901730558SDoug Anderson 		slot->host->state = STATE_SENDING_CMD11;
28001730558SDoug Anderson 
28101730558SDoug Anderson 		/*
28201730558SDoug Anderson 		 * We need to disable low power mode (automatic clock stop)
28301730558SDoug Anderson 		 * while doing voltage switch so we don't confuse the card,
28401730558SDoug Anderson 		 * since stopping the clock is a specific part of the UHS
28501730558SDoug Anderson 		 * voltage change dance.
28601730558SDoug Anderson 		 *
28701730558SDoug Anderson 		 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
28801730558SDoug Anderson 		 * unconditionally turned back on in dw_mci_setup_bus() if it's
28901730558SDoug Anderson 		 * ever called with a non-zero clock.  That shouldn't happen
29001730558SDoug Anderson 		 * until the voltage change is all done.
29101730558SDoug Anderson 		 */
29201730558SDoug Anderson 		clk_en_a = mci_readl(host, CLKENA);
29301730558SDoug Anderson 		clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
29401730558SDoug Anderson 		mci_writel(host, CLKENA, clk_en_a);
29501730558SDoug Anderson 		mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
29601730558SDoug Anderson 			     SDMMC_CMD_PRV_DAT_WAIT, 0);
29701730558SDoug Anderson 	}
29801730558SDoug Anderson 
299f95f3850SWill Newton 	if (cmd->flags & MMC_RSP_PRESENT) {
300f95f3850SWill Newton 		/* We expect a response, so set this bit */
301f95f3850SWill Newton 		cmdr |= SDMMC_CMD_RESP_EXP;
302f95f3850SWill Newton 		if (cmd->flags & MMC_RSP_136)
303f95f3850SWill Newton 			cmdr |= SDMMC_CMD_RESP_LONG;
304f95f3850SWill Newton 	}
305f95f3850SWill Newton 
306f95f3850SWill Newton 	if (cmd->flags & MMC_RSP_CRC)
307f95f3850SWill Newton 		cmdr |= SDMMC_CMD_RESP_CRC;
308f95f3850SWill Newton 
3090349c085SJaehoon Chung 	if (cmd->data) {
310f95f3850SWill Newton 		cmdr |= SDMMC_CMD_DAT_EXP;
3110349c085SJaehoon Chung 		if (cmd->data->flags & MMC_DATA_WRITE)
312f95f3850SWill Newton 			cmdr |= SDMMC_CMD_DAT_WR;
313f95f3850SWill Newton 	}
314f95f3850SWill Newton 
315aaaaeb7aSJaehoon Chung 	if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
316aaaaeb7aSJaehoon Chung 		cmdr |= SDMMC_CMD_USE_HOLD_REG;
317800d78bfSThomas Abraham 
318f95f3850SWill Newton 	return cmdr;
319f95f3850SWill Newton }
320f95f3850SWill Newton 
dw_mci_prep_stop_abort(struct dw_mci * host,struct mmc_command * cmd)32190c2143aSSeungwon Jeon static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
32290c2143aSSeungwon Jeon {
32390c2143aSSeungwon Jeon 	struct mmc_command *stop;
32490c2143aSSeungwon Jeon 	u32 cmdr;
32590c2143aSSeungwon Jeon 
32690c2143aSSeungwon Jeon 	if (!cmd->data)
32790c2143aSSeungwon Jeon 		return 0;
32890c2143aSSeungwon Jeon 
32990c2143aSSeungwon Jeon 	stop = &host->stop_abort;
33090c2143aSSeungwon Jeon 	cmdr = cmd->opcode;
33190c2143aSSeungwon Jeon 	memset(stop, 0, sizeof(struct mmc_command));
33290c2143aSSeungwon Jeon 
33390c2143aSSeungwon Jeon 	if (cmdr == MMC_READ_SINGLE_BLOCK ||
33490c2143aSSeungwon Jeon 	    cmdr == MMC_READ_MULTIPLE_BLOCK ||
33590c2143aSSeungwon Jeon 	    cmdr == MMC_WRITE_BLOCK ||
3366c2c6506SUlf Hansson 	    cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
337*b98e7e8dSChanWoo Lee 	    mmc_op_tuning(cmdr) ||
3389f0d3cc2SMårten Lindahl 	    cmdr == MMC_GEN_CMD) {
33990c2143aSSeungwon Jeon 		stop->opcode = MMC_STOP_TRANSMISSION;
34090c2143aSSeungwon Jeon 		stop->arg = 0;
34190c2143aSSeungwon Jeon 		stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
34290c2143aSSeungwon Jeon 	} else if (cmdr == SD_IO_RW_EXTENDED) {
34390c2143aSSeungwon Jeon 		stop->opcode = SD_IO_RW_DIRECT;
34490c2143aSSeungwon Jeon 		stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
34590c2143aSSeungwon Jeon 			     ((cmd->arg >> 28) & 0x7);
34690c2143aSSeungwon Jeon 		stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
34790c2143aSSeungwon Jeon 	} else {
34890c2143aSSeungwon Jeon 		return 0;
34990c2143aSSeungwon Jeon 	}
35090c2143aSSeungwon Jeon 
35190c2143aSSeungwon Jeon 	cmdr = stop->opcode | SDMMC_CMD_STOP |
35290c2143aSSeungwon Jeon 		SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
35390c2143aSSeungwon Jeon 
35442f989c0SJaehoon Chung 	if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags))
3558c005b40SJaehoon Chung 		cmdr |= SDMMC_CMD_USE_HOLD_REG;
3568c005b40SJaehoon Chung 
35790c2143aSSeungwon Jeon 	return cmdr;
35890c2143aSSeungwon Jeon }
35990c2143aSSeungwon Jeon 
dw_mci_set_cto(struct dw_mci * host)36003de1921SAddy Ke static inline void dw_mci_set_cto(struct dw_mci *host)
36103de1921SAddy Ke {
36203de1921SAddy Ke 	unsigned int cto_clks;
3634c2357f5SDouglas Anderson 	unsigned int cto_div;
36403de1921SAddy Ke 	unsigned int cto_ms;
3658892b705SDouglas Anderson 	unsigned long irqflags;
36603de1921SAddy Ke 
36703de1921SAddy Ke 	cto_clks = mci_readl(host, TMOUT) & 0xff;
3684c2357f5SDouglas Anderson 	cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
3694c2357f5SDouglas Anderson 	if (cto_div == 0)
3704c2357f5SDouglas Anderson 		cto_div = 1;
371c7151602SEvgeniy Didin 
372c7151602SEvgeniy Didin 	cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div,
373c7151602SEvgeniy Didin 				  host->bus_hz);
37403de1921SAddy Ke 
37503de1921SAddy Ke 	/* add a bit spare time */
37603de1921SAddy Ke 	cto_ms += 10;
37703de1921SAddy Ke 
3788892b705SDouglas Anderson 	/*
3798892b705SDouglas Anderson 	 * The durations we're working with are fairly short so we have to be
3808892b705SDouglas Anderson 	 * extra careful about synchronization here.  Specifically in hardware a
3818892b705SDouglas Anderson 	 * command timeout is _at most_ 5.1 ms, so that means we expect an
3828892b705SDouglas Anderson 	 * interrupt (either command done or timeout) to come rather quickly
3838892b705SDouglas Anderson 	 * after the mci_writel.  ...but just in case we have a long interrupt
3848892b705SDouglas Anderson 	 * latency let's add a bit of paranoia.
3858892b705SDouglas Anderson 	 *
3868892b705SDouglas Anderson 	 * In general we'll assume that at least an interrupt will be asserted
3878892b705SDouglas Anderson 	 * in hardware by the time the cto_timer runs.  ...and if it hasn't
3888892b705SDouglas Anderson 	 * been asserted in hardware by that time then we'll assume it'll never
3898892b705SDouglas Anderson 	 * come.
3908892b705SDouglas Anderson 	 */
3918892b705SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
3928892b705SDouglas Anderson 	if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
39303de1921SAddy Ke 		mod_timer(&host->cto_timer,
39403de1921SAddy Ke 			jiffies + msecs_to_jiffies(cto_ms) + 1);
3958892b705SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
39603de1921SAddy Ke }
39703de1921SAddy Ke 
dw_mci_start_command(struct dw_mci * host,struct mmc_command * cmd,u32 cmd_flags)398f95f3850SWill Newton static void dw_mci_start_command(struct dw_mci *host,
399f95f3850SWill Newton 				 struct mmc_command *cmd, u32 cmd_flags)
400f95f3850SWill Newton {
401f95f3850SWill Newton 	host->cmd = cmd;
4024a90920cSThomas Abraham 	dev_vdbg(host->dev,
403f95f3850SWill Newton 		 "start command: ARGR=0x%08x CMDR=0x%08x\n",
404f95f3850SWill Newton 		 cmd->arg, cmd_flags);
405f95f3850SWill Newton 
406f95f3850SWill Newton 	mci_writel(host, CMDARG, cmd->arg);
4070e3a22c0SShawn Lin 	wmb(); /* drain writebuffer */
4080bdbd0e8SDoug Anderson 	dw_mci_wait_while_busy(host, cmd_flags);
409f95f3850SWill Newton 
4108892b705SDouglas Anderson 	mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
4118892b705SDouglas Anderson 
41203de1921SAddy Ke 	/* response expected command only */
41303de1921SAddy Ke 	if (cmd_flags & SDMMC_CMD_RESP_EXP)
41403de1921SAddy Ke 		dw_mci_set_cto(host);
415f95f3850SWill Newton }
416f95f3850SWill Newton 
send_stop_abort(struct dw_mci * host,struct mmc_data * data)41790c2143aSSeungwon Jeon static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
418f95f3850SWill Newton {
419e13c3c08SJaehoon Chung 	struct mmc_command *stop = &host->stop_abort;
4200e3a22c0SShawn Lin 
42190c2143aSSeungwon Jeon 	dw_mci_start_command(host, stop, host->stop_cmdr);
422f95f3850SWill Newton }
423f95f3850SWill Newton 
424f95f3850SWill Newton /* DMA interface functions */
dw_mci_stop_dma(struct dw_mci * host)425f95f3850SWill Newton static void dw_mci_stop_dma(struct dw_mci *host)
426f95f3850SWill Newton {
42703e8cb53SJames Hogan 	if (host->using_dma) {
428f95f3850SWill Newton 		host->dma_ops->stop(host);
429f95f3850SWill Newton 		host->dma_ops->cleanup(host);
430aa50f259SSeungwon Jeon 	}
431aa50f259SSeungwon Jeon 
432f95f3850SWill Newton 	/* Data transfer was stopped by the interrupt handler */
433f95f3850SWill Newton 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
434f95f3850SWill Newton }
435f95f3850SWill Newton 
dw_mci_dma_cleanup(struct dw_mci * host)436f95f3850SWill Newton static void dw_mci_dma_cleanup(struct dw_mci *host)
437f95f3850SWill Newton {
438f95f3850SWill Newton 	struct mmc_data *data = host->data;
439f95f3850SWill Newton 
440a4cc7eb4SJaehoon Chung 	if (data && data->host_cookie == COOKIE_MAPPED) {
4414a90920cSThomas Abraham 		dma_unmap_sg(host->dev,
4429aa51408SSeungwon Jeon 			     data->sg,
4439aa51408SSeungwon Jeon 			     data->sg_len,
444feeef096SHeiner Kallweit 			     mmc_get_dma_dir(data));
445a4cc7eb4SJaehoon Chung 		data->host_cookie = COOKIE_UNMAPPED;
446a4cc7eb4SJaehoon Chung 	}
447f95f3850SWill Newton }
448f95f3850SWill Newton 
dw_mci_idmac_reset(struct dw_mci * host)4495ce9d961SSeungwon Jeon static void dw_mci_idmac_reset(struct dw_mci *host)
4505ce9d961SSeungwon Jeon {
4515ce9d961SSeungwon Jeon 	u32 bmod = mci_readl(host, BMOD);
4525ce9d961SSeungwon Jeon 	/* Software reset of DMA */
4535ce9d961SSeungwon Jeon 	bmod |= SDMMC_IDMAC_SWRESET;
4545ce9d961SSeungwon Jeon 	mci_writel(host, BMOD, bmod);
4555ce9d961SSeungwon Jeon }
4565ce9d961SSeungwon Jeon 
dw_mci_idmac_stop_dma(struct dw_mci * host)457f95f3850SWill Newton static void dw_mci_idmac_stop_dma(struct dw_mci *host)
458f95f3850SWill Newton {
459f95f3850SWill Newton 	u32 temp;
460f95f3850SWill Newton 
461f95f3850SWill Newton 	/* Disable and reset the IDMAC interface */
462f95f3850SWill Newton 	temp = mci_readl(host, CTRL);
463f95f3850SWill Newton 	temp &= ~SDMMC_CTRL_USE_IDMAC;
464f95f3850SWill Newton 	temp |= SDMMC_CTRL_DMA_RESET;
465f95f3850SWill Newton 	mci_writel(host, CTRL, temp);
466f95f3850SWill Newton 
467f95f3850SWill Newton 	/* Stop the IDMAC running */
468f95f3850SWill Newton 	temp = mci_readl(host, BMOD);
469a5289a43SJaehoon Chung 	temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
4705ce9d961SSeungwon Jeon 	temp |= SDMMC_IDMAC_SWRESET;
471f95f3850SWill Newton 	mci_writel(host, BMOD, temp);
472f95f3850SWill Newton }
473f95f3850SWill Newton 
dw_mci_dmac_complete_dma(void * arg)4743fc7eaefSShawn Lin static void dw_mci_dmac_complete_dma(void *arg)
475f95f3850SWill Newton {
4763fc7eaefSShawn Lin 	struct dw_mci *host = arg;
477f95f3850SWill Newton 	struct mmc_data *data = host->data;
478f95f3850SWill Newton 
4794a90920cSThomas Abraham 	dev_vdbg(host->dev, "DMA complete\n");
480f95f3850SWill Newton 
4813fc7eaefSShawn Lin 	if ((host->use_dma == TRANS_MODE_EDMAC) &&
4823fc7eaefSShawn Lin 	    data && (data->flags & MMC_DATA_READ))
4833fc7eaefSShawn Lin 		/* Invalidate cache after read */
48442f989c0SJaehoon Chung 		dma_sync_sg_for_cpu(mmc_dev(host->slot->mmc),
4853fc7eaefSShawn Lin 				    data->sg,
4863fc7eaefSShawn Lin 				    data->sg_len,
4873fc7eaefSShawn Lin 				    DMA_FROM_DEVICE);
4883fc7eaefSShawn Lin 
489f95f3850SWill Newton 	host->dma_ops->cleanup(host);
490f95f3850SWill Newton 
491f95f3850SWill Newton 	/*
492f95f3850SWill Newton 	 * If the card was removed, data will be NULL. No point in trying to
493f95f3850SWill Newton 	 * send the stop command or waiting for NBUSY in this case.
494f95f3850SWill Newton 	 */
495f95f3850SWill Newton 	if (data) {
496f95f3850SWill Newton 		set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
497f95f3850SWill Newton 		tasklet_schedule(&host->tasklet);
498f95f3850SWill Newton 	}
499f95f3850SWill Newton }
500f95f3850SWill Newton 
dw_mci_idmac_init(struct dw_mci * host)501f95f3850SWill Newton static int dw_mci_idmac_init(struct dw_mci *host)
502f95f3850SWill Newton {
503897b69e7SSeungwon Jeon 	int i;
504f95f3850SWill Newton 
50569d99fdcSPrabu Thangamuthu 	if (host->dma_64bit_address == 1) {
50669d99fdcSPrabu Thangamuthu 		struct idmac_desc_64addr *p;
50769d99fdcSPrabu Thangamuthu 		/* Number of descriptors in the ring buffer */
508cc190d4cSShawn Lin 		host->ring_size =
509cc190d4cSShawn Lin 			DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
51069d99fdcSPrabu Thangamuthu 
51169d99fdcSPrabu Thangamuthu 		/* Forward link the descriptor list */
51269d99fdcSPrabu Thangamuthu 		for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
51369d99fdcSPrabu Thangamuthu 								i++, p++) {
51469d99fdcSPrabu Thangamuthu 			p->des6 = (host->sg_dma +
51569d99fdcSPrabu Thangamuthu 					(sizeof(struct idmac_desc_64addr) *
51669d99fdcSPrabu Thangamuthu 							(i + 1))) & 0xffffffff;
51769d99fdcSPrabu Thangamuthu 
51869d99fdcSPrabu Thangamuthu 			p->des7 = (u64)(host->sg_dma +
51969d99fdcSPrabu Thangamuthu 					(sizeof(struct idmac_desc_64addr) *
52069d99fdcSPrabu Thangamuthu 							(i + 1))) >> 32;
52169d99fdcSPrabu Thangamuthu 			/* Initialize reserved and buffer size fields to "0" */
52247b7de2fSEvgeniy Didin 			p->des0 = 0;
52369d99fdcSPrabu Thangamuthu 			p->des1 = 0;
52469d99fdcSPrabu Thangamuthu 			p->des2 = 0;
52569d99fdcSPrabu Thangamuthu 			p->des3 = 0;
52669d99fdcSPrabu Thangamuthu 		}
52769d99fdcSPrabu Thangamuthu 
52869d99fdcSPrabu Thangamuthu 		/* Set the last descriptor as the end-of-ring descriptor */
52969d99fdcSPrabu Thangamuthu 		p->des6 = host->sg_dma & 0xffffffff;
53069d99fdcSPrabu Thangamuthu 		p->des7 = (u64)host->sg_dma >> 32;
53169d99fdcSPrabu Thangamuthu 		p->des0 = IDMAC_DES0_ER;
53269d99fdcSPrabu Thangamuthu 
53369d99fdcSPrabu Thangamuthu 	} else {
53469d99fdcSPrabu Thangamuthu 		struct idmac_desc *p;
535f95f3850SWill Newton 		/* Number of descriptors in the ring buffer */
536cc190d4cSShawn Lin 		host->ring_size =
537cc190d4cSShawn Lin 			DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
538f95f3850SWill Newton 
539f95f3850SWill Newton 		/* Forward link the descriptor list */
5400e3a22c0SShawn Lin 		for (i = 0, p = host->sg_cpu;
5410e3a22c0SShawn Lin 		     i < host->ring_size - 1;
5420e3a22c0SShawn Lin 		     i++, p++) {
5436687c42fSBen Dooks 			p->des3 = cpu_to_le32(host->sg_dma +
5446687c42fSBen Dooks 					(sizeof(struct idmac_desc) * (i + 1)));
54547b7de2fSEvgeniy Didin 			p->des0 = 0;
5464b244724SZhangfei Gao 			p->des1 = 0;
5474b244724SZhangfei Gao 		}
548f95f3850SWill Newton 
549f95f3850SWill Newton 		/* Set the last descriptor as the end-of-ring descriptor */
5506687c42fSBen Dooks 		p->des3 = cpu_to_le32(host->sg_dma);
5516687c42fSBen Dooks 		p->des0 = cpu_to_le32(IDMAC_DES0_ER);
55269d99fdcSPrabu Thangamuthu 	}
553f95f3850SWill Newton 
5545ce9d961SSeungwon Jeon 	dw_mci_idmac_reset(host);
555141a712aSSeungwon Jeon 
55669d99fdcSPrabu Thangamuthu 	if (host->dma_64bit_address == 1) {
55769d99fdcSPrabu Thangamuthu 		/* Mask out interrupts - get Tx & Rx complete only */
55869d99fdcSPrabu Thangamuthu 		mci_writel(host, IDSTS64, IDMAC_INT_CLR);
55969d99fdcSPrabu Thangamuthu 		mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
56069d99fdcSPrabu Thangamuthu 				SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
56169d99fdcSPrabu Thangamuthu 
56269d99fdcSPrabu Thangamuthu 		/* Set the descriptor base address */
56369d99fdcSPrabu Thangamuthu 		mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
56469d99fdcSPrabu Thangamuthu 		mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
56569d99fdcSPrabu Thangamuthu 
56669d99fdcSPrabu Thangamuthu 	} else {
567f95f3850SWill Newton 		/* Mask out interrupts - get Tx & Rx complete only */
568fc79a4d6SJoonyoung Shim 		mci_writel(host, IDSTS, IDMAC_INT_CLR);
56969d99fdcSPrabu Thangamuthu 		mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
57069d99fdcSPrabu Thangamuthu 				SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
571f95f3850SWill Newton 
572f95f3850SWill Newton 		/* Set the descriptor base address */
573f95f3850SWill Newton 		mci_writel(host, DBADDR, host->sg_dma);
57469d99fdcSPrabu Thangamuthu 	}
57569d99fdcSPrabu Thangamuthu 
576f95f3850SWill Newton 	return 0;
577f95f3850SWill Newton }
578f95f3850SWill Newton 
dw_mci_prepare_desc64(struct dw_mci * host,struct mmc_data * data,unsigned int sg_len)5793b2a067bSShawn Lin static inline int dw_mci_prepare_desc64(struct dw_mci *host,
5803b2a067bSShawn Lin 					 struct mmc_data *data,
5813b2a067bSShawn Lin 					 unsigned int sg_len)
5823b2a067bSShawn Lin {
5833b2a067bSShawn Lin 	unsigned int desc_len;
5843b2a067bSShawn Lin 	struct idmac_desc_64addr *desc_first, *desc_last, *desc;
585b6d2d81cSShawn Lin 	u32 val;
5863b2a067bSShawn Lin 	int i;
5873b2a067bSShawn Lin 
5883b2a067bSShawn Lin 	desc_first = desc_last = desc = host->sg_cpu;
5893b2a067bSShawn Lin 
5903b2a067bSShawn Lin 	for (i = 0; i < sg_len; i++) {
5913b2a067bSShawn Lin 		unsigned int length = sg_dma_len(&data->sg[i]);
5923b2a067bSShawn Lin 
5933b2a067bSShawn Lin 		u64 mem_addr = sg_dma_address(&data->sg[i]);
5943b2a067bSShawn Lin 
5953b2a067bSShawn Lin 		for ( ; length ; desc++) {
5963b2a067bSShawn Lin 			desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
5973b2a067bSShawn Lin 				   length : DW_MCI_DESC_DATA_LENGTH;
5983b2a067bSShawn Lin 
5993b2a067bSShawn Lin 			length -= desc_len;
6003b2a067bSShawn Lin 
6013b2a067bSShawn Lin 			/*
6023b2a067bSShawn Lin 			 * Wait for the former clear OWN bit operation
6033b2a067bSShawn Lin 			 * of IDMAC to make sure that this descriptor
6043b2a067bSShawn Lin 			 * isn't still owned by IDMAC as IDMAC's write
6053b2a067bSShawn Lin 			 * ops and CPU's read ops are asynchronous.
6063b2a067bSShawn Lin 			 */
607b6d2d81cSShawn Lin 			if (readl_poll_timeout_atomic(&desc->des0, val,
608b6d2d81cSShawn Lin 						!(val & IDMAC_DES0_OWN),
609b6d2d81cSShawn Lin 						10, 100 * USEC_PER_MSEC))
6103b2a067bSShawn Lin 				goto err_own_bit;
6113b2a067bSShawn Lin 
6123b2a067bSShawn Lin 			/*
6133b2a067bSShawn Lin 			 * Set the OWN bit and disable interrupts
6143b2a067bSShawn Lin 			 * for this descriptor
6153b2a067bSShawn Lin 			 */
6163b2a067bSShawn Lin 			desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
6173b2a067bSShawn Lin 						IDMAC_DES0_CH;
6183b2a067bSShawn Lin 
6193b2a067bSShawn Lin 			/* Buffer length */
6203b2a067bSShawn Lin 			IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
6213b2a067bSShawn Lin 
6223b2a067bSShawn Lin 			/* Physical address to DMA to/from */
6233b2a067bSShawn Lin 			desc->des4 = mem_addr & 0xffffffff;
6243b2a067bSShawn Lin 			desc->des5 = mem_addr >> 32;
6253b2a067bSShawn Lin 
6263b2a067bSShawn Lin 			/* Update physical address for the next desc */
6273b2a067bSShawn Lin 			mem_addr += desc_len;
6283b2a067bSShawn Lin 
6293b2a067bSShawn Lin 			/* Save pointer to the last descriptor */
6303b2a067bSShawn Lin 			desc_last = desc;
6313b2a067bSShawn Lin 		}
6323b2a067bSShawn Lin 	}
6333b2a067bSShawn Lin 
6343b2a067bSShawn Lin 	/* Set first descriptor */
6353b2a067bSShawn Lin 	desc_first->des0 |= IDMAC_DES0_FD;
6363b2a067bSShawn Lin 
6373b2a067bSShawn Lin 	/* Set last descriptor */
6383b2a067bSShawn Lin 	desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
6393b2a067bSShawn Lin 	desc_last->des0 |= IDMAC_DES0_LD;
6403b2a067bSShawn Lin 
6413b2a067bSShawn Lin 	return 0;
6423b2a067bSShawn Lin err_own_bit:
6433b2a067bSShawn Lin 	/* restore the descriptor chain as it's polluted */
64426be9d70SColin Ian King 	dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
645cc190d4cSShawn Lin 	memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
6463b2a067bSShawn Lin 	dw_mci_idmac_init(host);
6473b2a067bSShawn Lin 	return -EINVAL;
6483b2a067bSShawn Lin }
6493b2a067bSShawn Lin 
6503b2a067bSShawn Lin 
dw_mci_prepare_desc32(struct dw_mci * host,struct mmc_data * data,unsigned int sg_len)6513b2a067bSShawn Lin static inline int dw_mci_prepare_desc32(struct dw_mci *host,
6523b2a067bSShawn Lin 					 struct mmc_data *data,
6533b2a067bSShawn Lin 					 unsigned int sg_len)
6543b2a067bSShawn Lin {
6553b2a067bSShawn Lin 	unsigned int desc_len;
6563b2a067bSShawn Lin 	struct idmac_desc *desc_first, *desc_last, *desc;
657b6d2d81cSShawn Lin 	u32 val;
6583b2a067bSShawn Lin 	int i;
6593b2a067bSShawn Lin 
6603b2a067bSShawn Lin 	desc_first = desc_last = desc = host->sg_cpu;
6613b2a067bSShawn Lin 
6623b2a067bSShawn Lin 	for (i = 0; i < sg_len; i++) {
6633b2a067bSShawn Lin 		unsigned int length = sg_dma_len(&data->sg[i]);
6643b2a067bSShawn Lin 
6653b2a067bSShawn Lin 		u32 mem_addr = sg_dma_address(&data->sg[i]);
6663b2a067bSShawn Lin 
6673b2a067bSShawn Lin 		for ( ; length ; desc++) {
6683b2a067bSShawn Lin 			desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
6693b2a067bSShawn Lin 				   length : DW_MCI_DESC_DATA_LENGTH;
6703b2a067bSShawn Lin 
6713b2a067bSShawn Lin 			length -= desc_len;
6723b2a067bSShawn Lin 
6733b2a067bSShawn Lin 			/*
6743b2a067bSShawn Lin 			 * Wait for the former clear OWN bit operation
6753b2a067bSShawn Lin 			 * of IDMAC to make sure that this descriptor
6763b2a067bSShawn Lin 			 * isn't still owned by IDMAC as IDMAC's write
6773b2a067bSShawn Lin 			 * ops and CPU's read ops are asynchronous.
6783b2a067bSShawn Lin 			 */
679b6d2d81cSShawn Lin 			if (readl_poll_timeout_atomic(&desc->des0, val,
680b6d2d81cSShawn Lin 						      IDMAC_OWN_CLR64(val),
681b6d2d81cSShawn Lin 						      10,
682b6d2d81cSShawn Lin 						      100 * USEC_PER_MSEC))
6833b2a067bSShawn Lin 				goto err_own_bit;
6843b2a067bSShawn Lin 
6853b2a067bSShawn Lin 			/*
6863b2a067bSShawn Lin 			 * Set the OWN bit and disable interrupts
6873b2a067bSShawn Lin 			 * for this descriptor
6883b2a067bSShawn Lin 			 */
6893b2a067bSShawn Lin 			desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
6903b2a067bSShawn Lin 						 IDMAC_DES0_DIC |
6913b2a067bSShawn Lin 						 IDMAC_DES0_CH);
6923b2a067bSShawn Lin 
6933b2a067bSShawn Lin 			/* Buffer length */
6943b2a067bSShawn Lin 			IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
6953b2a067bSShawn Lin 
6963b2a067bSShawn Lin 			/* Physical address to DMA to/from */
6973b2a067bSShawn Lin 			desc->des2 = cpu_to_le32(mem_addr);
6983b2a067bSShawn Lin 
6993b2a067bSShawn Lin 			/* Update physical address for the next desc */
7003b2a067bSShawn Lin 			mem_addr += desc_len;
7013b2a067bSShawn Lin 
7023b2a067bSShawn Lin 			/* Save pointer to the last descriptor */
7033b2a067bSShawn Lin 			desc_last = desc;
7043b2a067bSShawn Lin 		}
7053b2a067bSShawn Lin 	}
7063b2a067bSShawn Lin 
7073b2a067bSShawn Lin 	/* Set first descriptor */
7083b2a067bSShawn Lin 	desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
7093b2a067bSShawn Lin 
7103b2a067bSShawn Lin 	/* Set last descriptor */
7113b2a067bSShawn Lin 	desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
7123b2a067bSShawn Lin 				       IDMAC_DES0_DIC));
7133b2a067bSShawn Lin 	desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
7143b2a067bSShawn Lin 
7153b2a067bSShawn Lin 	return 0;
7163b2a067bSShawn Lin err_own_bit:
7173b2a067bSShawn Lin 	/* restore the descriptor chain as it's polluted */
71826be9d70SColin Ian King 	dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
719cc190d4cSShawn Lin 	memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
7203b2a067bSShawn Lin 	dw_mci_idmac_init(host);
7213b2a067bSShawn Lin 	return -EINVAL;
7223b2a067bSShawn Lin }
7233b2a067bSShawn Lin 
dw_mci_idmac_start_dma(struct dw_mci * host,unsigned int sg_len)7243b2a067bSShawn Lin static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
7253b2a067bSShawn Lin {
7263b2a067bSShawn Lin 	u32 temp;
7273b2a067bSShawn Lin 	int ret;
7283b2a067bSShawn Lin 
7293b2a067bSShawn Lin 	if (host->dma_64bit_address == 1)
7303b2a067bSShawn Lin 		ret = dw_mci_prepare_desc64(host, host->data, sg_len);
7313b2a067bSShawn Lin 	else
7323b2a067bSShawn Lin 		ret = dw_mci_prepare_desc32(host, host->data, sg_len);
7333b2a067bSShawn Lin 
7343b2a067bSShawn Lin 	if (ret)
7353b2a067bSShawn Lin 		goto out;
7363b2a067bSShawn Lin 
7373b2a067bSShawn Lin 	/* drain writebuffer */
7383b2a067bSShawn Lin 	wmb();
7393b2a067bSShawn Lin 
7403b2a067bSShawn Lin 	/* Make sure to reset DMA in case we did PIO before this */
7413b2a067bSShawn Lin 	dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
7423b2a067bSShawn Lin 	dw_mci_idmac_reset(host);
7433b2a067bSShawn Lin 
7443b2a067bSShawn Lin 	/* Select IDMAC interface */
7453b2a067bSShawn Lin 	temp = mci_readl(host, CTRL);
7463b2a067bSShawn Lin 	temp |= SDMMC_CTRL_USE_IDMAC;
7473b2a067bSShawn Lin 	mci_writel(host, CTRL, temp);
7483b2a067bSShawn Lin 
7493b2a067bSShawn Lin 	/* drain writebuffer */
7503b2a067bSShawn Lin 	wmb();
7513b2a067bSShawn Lin 
7523b2a067bSShawn Lin 	/* Enable the IDMAC */
7533b2a067bSShawn Lin 	temp = mci_readl(host, BMOD);
7543b2a067bSShawn Lin 	temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
7553b2a067bSShawn Lin 	mci_writel(host, BMOD, temp);
7563b2a067bSShawn Lin 
7573b2a067bSShawn Lin 	/* Start it running */
7583b2a067bSShawn Lin 	mci_writel(host, PLDMND, 1);
7593b2a067bSShawn Lin 
7603b2a067bSShawn Lin out:
7613b2a067bSShawn Lin 	return ret;
7623b2a067bSShawn Lin }
7633b2a067bSShawn Lin 
7648e2b36eaSArnd Bergmann static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
765885c3e80SSeungwon Jeon 	.init = dw_mci_idmac_init,
766885c3e80SSeungwon Jeon 	.start = dw_mci_idmac_start_dma,
767885c3e80SSeungwon Jeon 	.stop = dw_mci_idmac_stop_dma,
7683fc7eaefSShawn Lin 	.complete = dw_mci_dmac_complete_dma,
769885c3e80SSeungwon Jeon 	.cleanup = dw_mci_dma_cleanup,
770885c3e80SSeungwon Jeon };
7713fc7eaefSShawn Lin 
dw_mci_edmac_stop_dma(struct dw_mci * host)7723fc7eaefSShawn Lin static void dw_mci_edmac_stop_dma(struct dw_mci *host)
7733fc7eaefSShawn Lin {
774ab925a31SShawn Lin 	dmaengine_terminate_async(host->dms->ch);
7753fc7eaefSShawn Lin }
7763fc7eaefSShawn Lin 
dw_mci_edmac_start_dma(struct dw_mci * host,unsigned int sg_len)7773fc7eaefSShawn Lin static int dw_mci_edmac_start_dma(struct dw_mci *host,
7783fc7eaefSShawn Lin 					    unsigned int sg_len)
7793fc7eaefSShawn Lin {
7803fc7eaefSShawn Lin 	struct dma_slave_config cfg;
7813fc7eaefSShawn Lin 	struct dma_async_tx_descriptor *desc = NULL;
7823fc7eaefSShawn Lin 	struct scatterlist *sgl = host->data->sg;
78327d70d36SColin Ian King 	static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
7843fc7eaefSShawn Lin 	u32 sg_elems = host->data->sg_len;
7853fc7eaefSShawn Lin 	u32 fifoth_val;
7863fc7eaefSShawn Lin 	u32 fifo_offset = host->fifo_reg - host->regs;
7873fc7eaefSShawn Lin 	int ret = 0;
7883fc7eaefSShawn Lin 
7893fc7eaefSShawn Lin 	/* Set external dma config: burst size, burst width */
790c3ff0189STony Lindgren 	memset(&cfg, 0, sizeof(cfg));
791260b3164SArnd Bergmann 	cfg.dst_addr = host->phy_regs + fifo_offset;
7923fc7eaefSShawn Lin 	cfg.src_addr = cfg.dst_addr;
7933fc7eaefSShawn Lin 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
7943fc7eaefSShawn Lin 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
7953fc7eaefSShawn Lin 
7963fc7eaefSShawn Lin 	/* Match burst msize with external dma config */
7973fc7eaefSShawn Lin 	fifoth_val = mci_readl(host, FIFOTH);
7983fc7eaefSShawn Lin 	cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
7993fc7eaefSShawn Lin 	cfg.src_maxburst = cfg.dst_maxburst;
8003fc7eaefSShawn Lin 
8013fc7eaefSShawn Lin 	if (host->data->flags & MMC_DATA_WRITE)
8023fc7eaefSShawn Lin 		cfg.direction = DMA_MEM_TO_DEV;
8033fc7eaefSShawn Lin 	else
8043fc7eaefSShawn Lin 		cfg.direction = DMA_DEV_TO_MEM;
8053fc7eaefSShawn Lin 
8063fc7eaefSShawn Lin 	ret = dmaengine_slave_config(host->dms->ch, &cfg);
8073fc7eaefSShawn Lin 	if (ret) {
8083fc7eaefSShawn Lin 		dev_err(host->dev, "Failed to config edmac.\n");
8093fc7eaefSShawn Lin 		return -EBUSY;
8103fc7eaefSShawn Lin 	}
8113fc7eaefSShawn Lin 
8123fc7eaefSShawn Lin 	desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
8133fc7eaefSShawn Lin 				       sg_len, cfg.direction,
8143fc7eaefSShawn Lin 				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
8153fc7eaefSShawn Lin 	if (!desc) {
8163fc7eaefSShawn Lin 		dev_err(host->dev, "Can't prepare slave sg.\n");
8173fc7eaefSShawn Lin 		return -EBUSY;
8183fc7eaefSShawn Lin 	}
8193fc7eaefSShawn Lin 
8203fc7eaefSShawn Lin 	/* Set dw_mci_dmac_complete_dma as callback */
8213fc7eaefSShawn Lin 	desc->callback = dw_mci_dmac_complete_dma;
8223fc7eaefSShawn Lin 	desc->callback_param = (void *)host;
8233fc7eaefSShawn Lin 	dmaengine_submit(desc);
8243fc7eaefSShawn Lin 
8253fc7eaefSShawn Lin 	/* Flush cache before write */
8263fc7eaefSShawn Lin 	if (host->data->flags & MMC_DATA_WRITE)
82742f989c0SJaehoon Chung 		dma_sync_sg_for_device(mmc_dev(host->slot->mmc), sgl,
8283fc7eaefSShawn Lin 				       sg_elems, DMA_TO_DEVICE);
8293fc7eaefSShawn Lin 
8303fc7eaefSShawn Lin 	dma_async_issue_pending(host->dms->ch);
8313fc7eaefSShawn Lin 
8323fc7eaefSShawn Lin 	return 0;
8333fc7eaefSShawn Lin }
8343fc7eaefSShawn Lin 
dw_mci_edmac_init(struct dw_mci * host)8353fc7eaefSShawn Lin static int dw_mci_edmac_init(struct dw_mci *host)
8363fc7eaefSShawn Lin {
8373fc7eaefSShawn Lin 	/* Request external dma channel */
8383fc7eaefSShawn Lin 	host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
8393fc7eaefSShawn Lin 	if (!host->dms)
8403fc7eaefSShawn Lin 		return -ENOMEM;
8413fc7eaefSShawn Lin 
842c1fce225SPeter Ujfalusi 	host->dms->ch = dma_request_chan(host->dev, "rx-tx");
843c1fce225SPeter Ujfalusi 	if (IS_ERR(host->dms->ch)) {
844c1fce225SPeter Ujfalusi 		int ret = PTR_ERR(host->dms->ch);
845c1fce225SPeter Ujfalusi 
8464539d36eSDan Carpenter 		dev_err(host->dev, "Failed to get external DMA channel.\n");
8473fc7eaefSShawn Lin 		kfree(host->dms);
8483fc7eaefSShawn Lin 		host->dms = NULL;
849c1fce225SPeter Ujfalusi 		return ret;
8503fc7eaefSShawn Lin 	}
8513fc7eaefSShawn Lin 
8523fc7eaefSShawn Lin 	return 0;
8533fc7eaefSShawn Lin }
8543fc7eaefSShawn Lin 
dw_mci_edmac_exit(struct dw_mci * host)8553fc7eaefSShawn Lin static void dw_mci_edmac_exit(struct dw_mci *host)
8563fc7eaefSShawn Lin {
8573fc7eaefSShawn Lin 	if (host->dms) {
8583fc7eaefSShawn Lin 		if (host->dms->ch) {
8593fc7eaefSShawn Lin 			dma_release_channel(host->dms->ch);
8603fc7eaefSShawn Lin 			host->dms->ch = NULL;
8613fc7eaefSShawn Lin 		}
8623fc7eaefSShawn Lin 		kfree(host->dms);
8633fc7eaefSShawn Lin 		host->dms = NULL;
8643fc7eaefSShawn Lin 	}
8653fc7eaefSShawn Lin }
8663fc7eaefSShawn Lin 
8673fc7eaefSShawn Lin static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
8683fc7eaefSShawn Lin 	.init = dw_mci_edmac_init,
8693fc7eaefSShawn Lin 	.exit = dw_mci_edmac_exit,
8703fc7eaefSShawn Lin 	.start = dw_mci_edmac_start_dma,
8713fc7eaefSShawn Lin 	.stop = dw_mci_edmac_stop_dma,
8723fc7eaefSShawn Lin 	.complete = dw_mci_dmac_complete_dma,
8733fc7eaefSShawn Lin 	.cleanup = dw_mci_dma_cleanup,
8743fc7eaefSShawn Lin };
875885c3e80SSeungwon Jeon 
dw_mci_pre_dma_transfer(struct dw_mci * host,struct mmc_data * data,int cookie)8769aa51408SSeungwon Jeon static int dw_mci_pre_dma_transfer(struct dw_mci *host,
8779aa51408SSeungwon Jeon 				   struct mmc_data *data,
878a4cc7eb4SJaehoon Chung 				   int cookie)
879f95f3850SWill Newton {
880f95f3850SWill Newton 	struct scatterlist *sg;
8819aa51408SSeungwon Jeon 	unsigned int i, sg_len;
882f95f3850SWill Newton 
883a4cc7eb4SJaehoon Chung 	if (data->host_cookie == COOKIE_PRE_MAPPED)
884a4cc7eb4SJaehoon Chung 		return data->sg_len;
885f95f3850SWill Newton 
886f95f3850SWill Newton 	/*
887f95f3850SWill Newton 	 * We don't do DMA on "complex" transfers, i.e. with
888f95f3850SWill Newton 	 * non-word-aligned buffers or lengths. Also, we don't bother
889f95f3850SWill Newton 	 * with all the DMA setup overhead for short transfers.
890f95f3850SWill Newton 	 */
891f95f3850SWill Newton 	if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
892f95f3850SWill Newton 		return -EINVAL;
8939aa51408SSeungwon Jeon 
894f95f3850SWill Newton 	if (data->blksz & 3)
895f95f3850SWill Newton 		return -EINVAL;
896f95f3850SWill Newton 
897f95f3850SWill Newton 	for_each_sg(data->sg, sg, data->sg_len, i) {
898f95f3850SWill Newton 		if (sg->offset & 3 || sg->length & 3)
899f95f3850SWill Newton 			return -EINVAL;
900f95f3850SWill Newton 	}
901f95f3850SWill Newton 
9024a90920cSThomas Abraham 	sg_len = dma_map_sg(host->dev,
9039aa51408SSeungwon Jeon 			    data->sg,
9049aa51408SSeungwon Jeon 			    data->sg_len,
905feeef096SHeiner Kallweit 			    mmc_get_dma_dir(data));
9069aa51408SSeungwon Jeon 	if (sg_len == 0)
9079aa51408SSeungwon Jeon 		return -EINVAL;
9089aa51408SSeungwon Jeon 
909a4cc7eb4SJaehoon Chung 	data->host_cookie = cookie;
9109aa51408SSeungwon Jeon 
9119aa51408SSeungwon Jeon 	return sg_len;
9129aa51408SSeungwon Jeon }
9139aa51408SSeungwon Jeon 
dw_mci_pre_req(struct mmc_host * mmc,struct mmc_request * mrq)9149aa51408SSeungwon Jeon static void dw_mci_pre_req(struct mmc_host *mmc,
915d3c6aac3SLinus Walleij 			   struct mmc_request *mrq)
9169aa51408SSeungwon Jeon {
9179aa51408SSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
9189aa51408SSeungwon Jeon 	struct mmc_data *data = mrq->data;
9199aa51408SSeungwon Jeon 
9209aa51408SSeungwon Jeon 	if (!slot->host->use_dma || !data)
9219aa51408SSeungwon Jeon 		return;
9229aa51408SSeungwon Jeon 
923a4cc7eb4SJaehoon Chung 	/* This data might be unmapped at this time */
924a4cc7eb4SJaehoon Chung 	data->host_cookie = COOKIE_UNMAPPED;
9259aa51408SSeungwon Jeon 
926a4cc7eb4SJaehoon Chung 	if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
927a4cc7eb4SJaehoon Chung 				COOKIE_PRE_MAPPED) < 0)
928a4cc7eb4SJaehoon Chung 		data->host_cookie = COOKIE_UNMAPPED;
9299aa51408SSeungwon Jeon }
9309aa51408SSeungwon Jeon 
dw_mci_post_req(struct mmc_host * mmc,struct mmc_request * mrq,int err)9319aa51408SSeungwon Jeon static void dw_mci_post_req(struct mmc_host *mmc,
9329aa51408SSeungwon Jeon 			    struct mmc_request *mrq,
9339aa51408SSeungwon Jeon 			    int err)
9349aa51408SSeungwon Jeon {
9359aa51408SSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
9369aa51408SSeungwon Jeon 	struct mmc_data *data = mrq->data;
9379aa51408SSeungwon Jeon 
9389aa51408SSeungwon Jeon 	if (!slot->host->use_dma || !data)
9399aa51408SSeungwon Jeon 		return;
9409aa51408SSeungwon Jeon 
941a4cc7eb4SJaehoon Chung 	if (data->host_cookie != COOKIE_UNMAPPED)
9424a90920cSThomas Abraham 		dma_unmap_sg(slot->host->dev,
9439aa51408SSeungwon Jeon 			     data->sg,
9449aa51408SSeungwon Jeon 			     data->sg_len,
945feeef096SHeiner Kallweit 			     mmc_get_dma_dir(data));
946a4cc7eb4SJaehoon Chung 	data->host_cookie = COOKIE_UNMAPPED;
9479aa51408SSeungwon Jeon }
9489aa51408SSeungwon Jeon 
dw_mci_get_cd(struct mmc_host * mmc)949671fa142SShawn Lin static int dw_mci_get_cd(struct mmc_host *mmc)
950671fa142SShawn Lin {
951671fa142SShawn Lin 	int present;
952671fa142SShawn Lin 	struct dw_mci_slot *slot = mmc_priv(mmc);
953671fa142SShawn Lin 	struct dw_mci *host = slot->host;
954671fa142SShawn Lin 	int gpio_cd = mmc_gpio_get_cd(mmc);
955671fa142SShawn Lin 
956671fa142SShawn Lin 	/* Use platform get_cd function, else try onboard card detect */
957671fa142SShawn Lin 	if (((mmc->caps & MMC_CAP_NEEDS_POLL)
958671fa142SShawn Lin 				|| !mmc_card_is_removable(mmc))) {
959671fa142SShawn Lin 		present = 1;
960671fa142SShawn Lin 
961671fa142SShawn Lin 		if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
962671fa142SShawn Lin 			if (mmc->caps & MMC_CAP_NEEDS_POLL) {
963671fa142SShawn Lin 				dev_info(&mmc->class_dev,
964671fa142SShawn Lin 					"card is polling.\n");
965671fa142SShawn Lin 			} else {
966671fa142SShawn Lin 				dev_info(&mmc->class_dev,
967671fa142SShawn Lin 					"card is non-removable.\n");
968671fa142SShawn Lin 			}
969671fa142SShawn Lin 			set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
970671fa142SShawn Lin 		}
971671fa142SShawn Lin 
972671fa142SShawn Lin 		return present;
973671fa142SShawn Lin 	} else if (gpio_cd >= 0)
974671fa142SShawn Lin 		present = gpio_cd;
975671fa142SShawn Lin 	else
976671fa142SShawn Lin 		present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
977671fa142SShawn Lin 			== 0 ? 1 : 0;
978671fa142SShawn Lin 
979671fa142SShawn Lin 	spin_lock_bh(&host->lock);
980671fa142SShawn Lin 	if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
981671fa142SShawn Lin 		dev_dbg(&mmc->class_dev, "card is present\n");
982671fa142SShawn Lin 	else if (!present &&
983671fa142SShawn Lin 			!test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
984671fa142SShawn Lin 		dev_dbg(&mmc->class_dev, "card is not present\n");
985671fa142SShawn Lin 	spin_unlock_bh(&host->lock);
986671fa142SShawn Lin 
987671fa142SShawn Lin 	return present;
988671fa142SShawn Lin }
989671fa142SShawn Lin 
dw_mci_adjust_fifoth(struct dw_mci * host,struct mmc_data * data)99052426899SSeungwon Jeon static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
99152426899SSeungwon Jeon {
99252426899SSeungwon Jeon 	unsigned int blksz = data->blksz;
99327d70d36SColin Ian King 	static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
99452426899SSeungwon Jeon 	u32 fifo_width = 1 << host->data_shift;
99552426899SSeungwon Jeon 	u32 blksz_depth = blksz / fifo_width, fifoth_val;
99652426899SSeungwon Jeon 	u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
9970e3a22c0SShawn Lin 	int idx = ARRAY_SIZE(mszs) - 1;
99852426899SSeungwon Jeon 
9993fc7eaefSShawn Lin 	/* pio should ship this scenario */
10003fc7eaefSShawn Lin 	if (!host->use_dma)
10013fc7eaefSShawn Lin 		return;
10023fc7eaefSShawn Lin 
100352426899SSeungwon Jeon 	tx_wmark = (host->fifo_depth) / 2;
100452426899SSeungwon Jeon 	tx_wmark_invers = host->fifo_depth - tx_wmark;
100552426899SSeungwon Jeon 
100652426899SSeungwon Jeon 	/*
100752426899SSeungwon Jeon 	 * MSIZE is '1',
100852426899SSeungwon Jeon 	 * if blksz is not a multiple of the FIFO width
100952426899SSeungwon Jeon 	 */
101020753569SShawn Lin 	if (blksz % fifo_width)
101152426899SSeungwon Jeon 		goto done;
101252426899SSeungwon Jeon 
101352426899SSeungwon Jeon 	do {
101452426899SSeungwon Jeon 		if (!((blksz_depth % mszs[idx]) ||
101552426899SSeungwon Jeon 		     (tx_wmark_invers % mszs[idx]))) {
101652426899SSeungwon Jeon 			msize = idx;
101752426899SSeungwon Jeon 			rx_wmark = mszs[idx] - 1;
101852426899SSeungwon Jeon 			break;
101952426899SSeungwon Jeon 		}
102052426899SSeungwon Jeon 	} while (--idx > 0);
102152426899SSeungwon Jeon 	/*
102252426899SSeungwon Jeon 	 * If idx is '0', it won't be tried
102352426899SSeungwon Jeon 	 * Thus, initial values are uesed
102452426899SSeungwon Jeon 	 */
102552426899SSeungwon Jeon done:
102652426899SSeungwon Jeon 	fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
102752426899SSeungwon Jeon 	mci_writel(host, FIFOTH, fifoth_val);
102852426899SSeungwon Jeon }
102952426899SSeungwon Jeon 
dw_mci_ctrl_thld(struct dw_mci * host,struct mmc_data * data)10307e4bf1bcSJaehoon Chung static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
1031f1d2736cSSeungwon Jeon {
1032f1d2736cSSeungwon Jeon 	unsigned int blksz = data->blksz;
1033f1d2736cSSeungwon Jeon 	u32 blksz_depth, fifo_depth;
1034f1d2736cSSeungwon Jeon 	u16 thld_size;
10357e4bf1bcSJaehoon Chung 	u8 enable;
1036f1d2736cSSeungwon Jeon 
103766dfd101SJames Hogan 	/*
103866dfd101SJames Hogan 	 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
103966dfd101SJames Hogan 	 * in the FIFO region, so we really shouldn't access it).
104066dfd101SJames Hogan 	 */
10417e4bf1bcSJaehoon Chung 	if (host->verid < DW_MMC_240A ||
10427e4bf1bcSJaehoon Chung 		(host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
104366dfd101SJames Hogan 		return;
104466dfd101SJames Hogan 
10457e4bf1bcSJaehoon Chung 	/*
10467e4bf1bcSJaehoon Chung 	 * Card write Threshold is introduced since 2.80a
10477e4bf1bcSJaehoon Chung 	 * It's used when HS400 mode is enabled.
10487e4bf1bcSJaehoon Chung 	 */
10497e4bf1bcSJaehoon Chung 	if (data->flags & MMC_DATA_WRITE &&
10507a6b9f4dSx00270170 		host->timing != MMC_TIMING_MMC_HS400)
10517a6b9f4dSx00270170 		goto disable;
10527e4bf1bcSJaehoon Chung 
10537e4bf1bcSJaehoon Chung 	if (data->flags & MMC_DATA_WRITE)
10547e4bf1bcSJaehoon Chung 		enable = SDMMC_CARD_WR_THR_EN;
10557e4bf1bcSJaehoon Chung 	else
10567e4bf1bcSJaehoon Chung 		enable = SDMMC_CARD_RD_THR_EN;
10577e4bf1bcSJaehoon Chung 
1058f1d2736cSSeungwon Jeon 	if (host->timing != MMC_TIMING_MMC_HS200 &&
10597a6b9f4dSx00270170 	    host->timing != MMC_TIMING_UHS_SDR104 &&
10607a6b9f4dSx00270170 	    host->timing != MMC_TIMING_MMC_HS400)
1061f1d2736cSSeungwon Jeon 		goto disable;
1062f1d2736cSSeungwon Jeon 
1063f1d2736cSSeungwon Jeon 	blksz_depth = blksz / (1 << host->data_shift);
1064f1d2736cSSeungwon Jeon 	fifo_depth = host->fifo_depth;
1065f1d2736cSSeungwon Jeon 
1066f1d2736cSSeungwon Jeon 	if (blksz_depth > fifo_depth)
1067f1d2736cSSeungwon Jeon 		goto disable;
1068f1d2736cSSeungwon Jeon 
1069f1d2736cSSeungwon Jeon 	/*
1070f1d2736cSSeungwon Jeon 	 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1071f1d2736cSSeungwon Jeon 	 * If (blksz_depth) <  (fifo_depth >> 1), should be thld_size = blksz
1072f1d2736cSSeungwon Jeon 	 * Currently just choose blksz.
1073f1d2736cSSeungwon Jeon 	 */
1074f1d2736cSSeungwon Jeon 	thld_size = blksz;
10757e4bf1bcSJaehoon Chung 	mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
1076f1d2736cSSeungwon Jeon 	return;
1077f1d2736cSSeungwon Jeon 
1078f1d2736cSSeungwon Jeon disable:
10797e4bf1bcSJaehoon Chung 	mci_writel(host, CDTHRCTL, 0);
1080f1d2736cSSeungwon Jeon }
1081f1d2736cSSeungwon Jeon 
dw_mci_submit_data_dma(struct dw_mci * host,struct mmc_data * data)10829aa51408SSeungwon Jeon static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
10839aa51408SSeungwon Jeon {
1084f8c58c11SDoug Anderson 	unsigned long irqflags;
10859aa51408SSeungwon Jeon 	int sg_len;
10869aa51408SSeungwon Jeon 	u32 temp;
10879aa51408SSeungwon Jeon 
10889aa51408SSeungwon Jeon 	host->using_dma = 0;
10899aa51408SSeungwon Jeon 
10909aa51408SSeungwon Jeon 	/* If we don't have a channel, we can't do DMA */
10919aa51408SSeungwon Jeon 	if (!host->use_dma)
10929aa51408SSeungwon Jeon 		return -ENODEV;
10939aa51408SSeungwon Jeon 
1094a4cc7eb4SJaehoon Chung 	sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
1095a99aa9b9SSeungwon Jeon 	if (sg_len < 0) {
1096a99aa9b9SSeungwon Jeon 		host->dma_ops->stop(host);
10979aa51408SSeungwon Jeon 		return sg_len;
1098a99aa9b9SSeungwon Jeon 	}
10999aa51408SSeungwon Jeon 
110003e8cb53SJames Hogan 	host->using_dma = 1;
110103e8cb53SJames Hogan 
11023fc7eaefSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC)
11034a90920cSThomas Abraham 		dev_vdbg(host->dev,
1104f95f3850SWill Newton 			 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
11053fc7eaefSShawn Lin 			 (unsigned long)host->sg_cpu,
11063fc7eaefSShawn Lin 			 (unsigned long)host->sg_dma,
1107f95f3850SWill Newton 			 sg_len);
1108f95f3850SWill Newton 
110952426899SSeungwon Jeon 	/*
111052426899SSeungwon Jeon 	 * Decide the MSIZE and RX/TX Watermark.
111152426899SSeungwon Jeon 	 * If current block size is same with previous size,
111252426899SSeungwon Jeon 	 * no need to update fifoth.
111352426899SSeungwon Jeon 	 */
111452426899SSeungwon Jeon 	if (host->prev_blksz != data->blksz)
111552426899SSeungwon Jeon 		dw_mci_adjust_fifoth(host, data);
111652426899SSeungwon Jeon 
1117f95f3850SWill Newton 	/* Enable the DMA interface */
1118f95f3850SWill Newton 	temp = mci_readl(host, CTRL);
1119f95f3850SWill Newton 	temp |= SDMMC_CTRL_DMA_ENABLE;
1120f95f3850SWill Newton 	mci_writel(host, CTRL, temp);
1121f95f3850SWill Newton 
1122f95f3850SWill Newton 	/* Disable RX/TX IRQs, let DMA handle it */
1123f8c58c11SDoug Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
1124f95f3850SWill Newton 	temp = mci_readl(host, INTMASK);
1125f95f3850SWill Newton 	temp  &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1126f95f3850SWill Newton 	mci_writel(host, INTMASK, temp);
1127f8c58c11SDoug Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
1128f95f3850SWill Newton 
11293fc7eaefSShawn Lin 	if (host->dma_ops->start(host, sg_len)) {
1130647f80a1SJaehoon Chung 		host->dma_ops->stop(host);
1131d12d0cb1SShawn Lin 		/* We can't do DMA, try PIO for this one */
1132d12d0cb1SShawn Lin 		dev_dbg(host->dev,
1133d12d0cb1SShawn Lin 			"%s: fall back to PIO mode for current transfer\n",
1134d12d0cb1SShawn Lin 			__func__);
11353fc7eaefSShawn Lin 		return -ENODEV;
11363fc7eaefSShawn Lin 	}
1137f95f3850SWill Newton 
1138f95f3850SWill Newton 	return 0;
1139f95f3850SWill Newton }
1140f95f3850SWill Newton 
dw_mci_submit_data(struct dw_mci * host,struct mmc_data * data)1141f95f3850SWill Newton static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1142f95f3850SWill Newton {
1143f8c58c11SDoug Anderson 	unsigned long irqflags;
11440e3a22c0SShawn Lin 	int flags = SG_MITER_ATOMIC;
1145f95f3850SWill Newton 	u32 temp;
1146f95f3850SWill Newton 
1147f95f3850SWill Newton 	data->error = -EINPROGRESS;
1148f95f3850SWill Newton 
1149f95f3850SWill Newton 	WARN_ON(host->data);
1150f95f3850SWill Newton 	host->sg = NULL;
1151f95f3850SWill Newton 	host->data = data;
1152f95f3850SWill Newton 
11537e4bf1bcSJaehoon Chung 	if (data->flags & MMC_DATA_READ)
115455c5efbcSJames Hogan 		host->dir_status = DW_MCI_RECV_STATUS;
11557e4bf1bcSJaehoon Chung 	else
115655c5efbcSJames Hogan 		host->dir_status = DW_MCI_SEND_STATUS;
11577e4bf1bcSJaehoon Chung 
11587e4bf1bcSJaehoon Chung 	dw_mci_ctrl_thld(host, data);
115955c5efbcSJames Hogan 
1160f95f3850SWill Newton 	if (dw_mci_submit_data_dma(host, data)) {
1161f9c2a0dcSSeungwon Jeon 		if (host->data->flags & MMC_DATA_READ)
1162f9c2a0dcSSeungwon Jeon 			flags |= SG_MITER_TO_SG;
1163f9c2a0dcSSeungwon Jeon 		else
1164f9c2a0dcSSeungwon Jeon 			flags |= SG_MITER_FROM_SG;
1165f9c2a0dcSSeungwon Jeon 
1166f9c2a0dcSSeungwon Jeon 		sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1167f95f3850SWill Newton 		host->sg = data->sg;
116834b664a2SJames Hogan 		host->part_buf_start = 0;
116934b664a2SJames Hogan 		host->part_buf_count = 0;
1170f95f3850SWill Newton 
1171b40af3aaSJames Hogan 		mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
1172f8c58c11SDoug Anderson 
1173f8c58c11SDoug Anderson 		spin_lock_irqsave(&host->irq_lock, irqflags);
1174f95f3850SWill Newton 		temp = mci_readl(host, INTMASK);
1175f95f3850SWill Newton 		temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1176f95f3850SWill Newton 		mci_writel(host, INTMASK, temp);
1177f8c58c11SDoug Anderson 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
1178f95f3850SWill Newton 
1179f95f3850SWill Newton 		temp = mci_readl(host, CTRL);
1180f95f3850SWill Newton 		temp &= ~SDMMC_CTRL_DMA_ENABLE;
1181f95f3850SWill Newton 		mci_writel(host, CTRL, temp);
118252426899SSeungwon Jeon 
118352426899SSeungwon Jeon 		/*
1184d6fced83SJun Nie 		 * Use the initial fifoth_val for PIO mode. If wm_algined
1185d6fced83SJun Nie 		 * is set, we set watermark same as data size.
118652426899SSeungwon Jeon 		 * If next issued data may be transfered by DMA mode,
118752426899SSeungwon Jeon 		 * prev_blksz should be invalidated.
118852426899SSeungwon Jeon 		 */
1189d6fced83SJun Nie 		if (host->wm_aligned)
1190d6fced83SJun Nie 			dw_mci_adjust_fifoth(host, data);
1191d6fced83SJun Nie 		else
119252426899SSeungwon Jeon 			mci_writel(host, FIFOTH, host->fifoth_val);
119352426899SSeungwon Jeon 		host->prev_blksz = 0;
119452426899SSeungwon Jeon 	} else {
119552426899SSeungwon Jeon 		/*
119652426899SSeungwon Jeon 		 * Keep the current block size.
119752426899SSeungwon Jeon 		 * It will be used to decide whether to update
119852426899SSeungwon Jeon 		 * fifoth register next time.
119952426899SSeungwon Jeon 		 */
120052426899SSeungwon Jeon 		host->prev_blksz = data->blksz;
1201f95f3850SWill Newton 	}
1202f95f3850SWill Newton }
1203f95f3850SWill Newton 
dw_mci_setup_bus(struct dw_mci_slot * slot,bool force_clkinit)1204ab269128SAbhilash Kesavan static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
1205f95f3850SWill Newton {
1206f95f3850SWill Newton 	struct dw_mci *host = slot->host;
1207fdf492a1SDoug Anderson 	unsigned int clock = slot->clock;
1208f95f3850SWill Newton 	u32 div;
12099623b5b9SDoug Anderson 	u32 clk_en_a;
121001730558SDoug Anderson 	u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
121101730558SDoug Anderson 
121201730558SDoug Anderson 	/* We must continue to set bit 28 in CMD until the change is complete */
121301730558SDoug Anderson 	if (host->state == STATE_WAITING_CMD11_DONE)
121401730558SDoug Anderson 		sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
1215f95f3850SWill Newton 
1216ff178981SShawn Lin 	slot->mmc->actual_clock = 0;
1217ff178981SShawn Lin 
1218fdf492a1SDoug Anderson 	if (!clock) {
1219fdf492a1SDoug Anderson 		mci_writel(host, CLKENA, 0);
122001730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1221fdf492a1SDoug Anderson 	} else if (clock != host->current_speed || force_clkinit) {
1222fdf492a1SDoug Anderson 		div = host->bus_hz / clock;
1223fdf492a1SDoug Anderson 		if (host->bus_hz % clock && host->bus_hz > clock)
1224f95f3850SWill Newton 			/*
1225f95f3850SWill Newton 			 * move the + 1 after the divide to prevent
1226f95f3850SWill Newton 			 * over-clocking the card.
1227f95f3850SWill Newton 			 */
1228e419990bSSeungwon Jeon 			div += 1;
1229e419990bSSeungwon Jeon 
1230fdf492a1SDoug Anderson 		div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
1231f95f3850SWill Newton 
1232e6cd7a8eSJaehoon Chung 		if ((clock != slot->__clk_old &&
1233e6cd7a8eSJaehoon Chung 			!test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1234e6cd7a8eSJaehoon Chung 			force_clkinit) {
1235ce69e2feSShawn Lin 			/* Silent the verbose log if calling from PM context */
1236ce69e2feSShawn Lin 			if (!force_clkinit)
1237f95f3850SWill Newton 				dev_info(&slot->mmc->class_dev,
1238fdf492a1SDoug Anderson 					 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1239fdf492a1SDoug Anderson 					 slot->id, host->bus_hz, clock,
1240fdf492a1SDoug Anderson 					 div ? ((host->bus_hz / div) >> 1) :
1241fdf492a1SDoug Anderson 					 host->bus_hz, div);
1242f95f3850SWill Newton 
1243e6cd7a8eSJaehoon Chung 			/*
1244e6cd7a8eSJaehoon Chung 			 * If card is polling, display the message only
1245e6cd7a8eSJaehoon Chung 			 * one time at boot time.
1246e6cd7a8eSJaehoon Chung 			 */
1247e6cd7a8eSJaehoon Chung 			if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1248e6cd7a8eSJaehoon Chung 					slot->mmc->f_min == clock)
1249e6cd7a8eSJaehoon Chung 				set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1250e6cd7a8eSJaehoon Chung 		}
1251e6cd7a8eSJaehoon Chung 
1252f95f3850SWill Newton 		/* disable clock */
1253f95f3850SWill Newton 		mci_writel(host, CLKENA, 0);
1254f95f3850SWill Newton 		mci_writel(host, CLKSRC, 0);
1255f95f3850SWill Newton 
1256f95f3850SWill Newton 		/* inform CIU */
125701730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1258f95f3850SWill Newton 
1259f95f3850SWill Newton 		/* set clock to desired speed */
1260f95f3850SWill Newton 		mci_writel(host, CLKDIV, div);
1261f95f3850SWill Newton 
1262f95f3850SWill Newton 		/* inform CIU */
126301730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1264f95f3850SWill Newton 
12659623b5b9SDoug Anderson 		/* enable clock; only low power if no SDIO */
12669623b5b9SDoug Anderson 		clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
1267b24c8b26SDoug Anderson 		if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
12689623b5b9SDoug Anderson 			clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
12699623b5b9SDoug Anderson 		mci_writel(host, CLKENA, clk_en_a);
1270f95f3850SWill Newton 
1271f95f3850SWill Newton 		/* inform CIU */
127201730558SDoug Anderson 		mci_send_cmd(slot, sdmmc_cmd_bits, 0);
1273005d675aSJaehoon Chung 
1274005d675aSJaehoon Chung 		/* keep the last clock value that was requested from core */
1275005d675aSJaehoon Chung 		slot->__clk_old = clock;
1276ff178981SShawn Lin 		slot->mmc->actual_clock = div ? ((host->bus_hz / div) >> 1) :
1277ff178981SShawn Lin 					  host->bus_hz;
1278f95f3850SWill Newton 	}
1279f95f3850SWill Newton 
1280fdf492a1SDoug Anderson 	host->current_speed = clock;
1281fdf492a1SDoug Anderson 
1282f95f3850SWill Newton 	/* Set the current slot bus width */
12831d56c453SSeungwon Jeon 	mci_writel(host, CTYPE, (slot->ctype << slot->id));
1284f95f3850SWill Newton }
1285f95f3850SWill Newton 
dw_mci_set_data_timeout(struct dw_mci * host,unsigned int timeout_ns)12866a8c2018SMårten Lindahl static void dw_mci_set_data_timeout(struct dw_mci *host,
12876a8c2018SMårten Lindahl 				    unsigned int timeout_ns)
12886a8c2018SMårten Lindahl {
128925d5417aSMårten Lindahl 	const struct dw_mci_drv_data *drv_data = host->drv_data;
12906a8c2018SMårten Lindahl 	u32 clk_div, tmout;
12916a8c2018SMårten Lindahl 	u64 tmp;
12926a8c2018SMårten Lindahl 
129325d5417aSMårten Lindahl 	if (drv_data && drv_data->set_data_timeout)
129425d5417aSMårten Lindahl 		return drv_data->set_data_timeout(host, timeout_ns);
129525d5417aSMårten Lindahl 
12966a8c2018SMårten Lindahl 	clk_div = (mci_readl(host, CLKDIV) & 0xFF) * 2;
12976a8c2018SMårten Lindahl 	if (clk_div == 0)
12986a8c2018SMårten Lindahl 		clk_div = 1;
12996a8c2018SMårten Lindahl 
13006a8c2018SMårten Lindahl 	tmp = DIV_ROUND_UP_ULL((u64)timeout_ns * host->bus_hz, NSEC_PER_SEC);
13016a8c2018SMårten Lindahl 	tmp = DIV_ROUND_UP_ULL(tmp, clk_div);
13026a8c2018SMårten Lindahl 
13036a8c2018SMårten Lindahl 	/* TMOUT[7:0] (RESPONSE_TIMEOUT) */
13046a8c2018SMårten Lindahl 	tmout = 0xFF; /* Set maximum */
13056a8c2018SMårten Lindahl 
13066a8c2018SMårten Lindahl 	/* TMOUT[31:8] (DATA_TIMEOUT) */
13076a8c2018SMårten Lindahl 	if (!tmp || tmp > 0xFFFFFF)
13086a8c2018SMårten Lindahl 		tmout |= (0xFFFFFF << 8);
13096a8c2018SMårten Lindahl 	else
13106a8c2018SMårten Lindahl 		tmout |= (tmp & 0xFFFFFF) << 8;
13116a8c2018SMårten Lindahl 
13126a8c2018SMårten Lindahl 	mci_writel(host, TMOUT, tmout);
1313ebc4dcf1SDan Carpenter 	dev_dbg(host->dev, "timeout_ns: %u => TMOUT[31:8]: %#08x",
13146a8c2018SMårten Lindahl 		timeout_ns, tmout >> 8);
13156a8c2018SMårten Lindahl }
13166a8c2018SMårten Lindahl 
__dw_mci_start_request(struct dw_mci * host,struct dw_mci_slot * slot,struct mmc_command * cmd)1317053b3ce6SSeungwon Jeon static void __dw_mci_start_request(struct dw_mci *host,
1318053b3ce6SSeungwon Jeon 				   struct dw_mci_slot *slot,
1319053b3ce6SSeungwon Jeon 				   struct mmc_command *cmd)
1320f95f3850SWill Newton {
1321f95f3850SWill Newton 	struct mmc_request *mrq;
1322f95f3850SWill Newton 	struct mmc_data	*data;
1323f95f3850SWill Newton 	u32 cmdflags;
1324f95f3850SWill Newton 
1325f95f3850SWill Newton 	mrq = slot->mrq;
1326f95f3850SWill Newton 
1327f95f3850SWill Newton 	host->mrq = mrq;
1328f95f3850SWill Newton 
1329f95f3850SWill Newton 	host->pending_events = 0;
1330f95f3850SWill Newton 	host->completed_events = 0;
1331e352c813SSeungwon Jeon 	host->cmd_status = 0;
1332f95f3850SWill Newton 	host->data_status = 0;
1333e352c813SSeungwon Jeon 	host->dir_status = 0;
1334f95f3850SWill Newton 
1335053b3ce6SSeungwon Jeon 	data = cmd->data;
1336f95f3850SWill Newton 	if (data) {
13376a8c2018SMårten Lindahl 		dw_mci_set_data_timeout(host, data->timeout_ns);
1338f95f3850SWill Newton 		mci_writel(host, BYTCNT, data->blksz*data->blocks);
1339f95f3850SWill Newton 		mci_writel(host, BLKSIZ, data->blksz);
1340f95f3850SWill Newton 	}
1341f95f3850SWill Newton 
1342f95f3850SWill Newton 	cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1343f95f3850SWill Newton 
1344f95f3850SWill Newton 	/* this is the first command, send the initialization clock */
1345f95f3850SWill Newton 	if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1346f95f3850SWill Newton 		cmdflags |= SDMMC_CMD_INIT;
1347f95f3850SWill Newton 
1348f95f3850SWill Newton 	if (data) {
1349f95f3850SWill Newton 		dw_mci_submit_data(host, data);
13500e3a22c0SShawn Lin 		wmb(); /* drain writebuffer */
1351f95f3850SWill Newton 	}
1352f95f3850SWill Newton 
1353f95f3850SWill Newton 	dw_mci_start_command(host, cmd, cmdflags);
1354f95f3850SWill Newton 
13555c935165SDoug Anderson 	if (cmd->opcode == SD_SWITCH_VOLTAGE) {
135649ba0302SDoug Anderson 		unsigned long irqflags;
135749ba0302SDoug Anderson 
13585c935165SDoug Anderson 		/*
13598886a6fdSDoug Anderson 		 * Databook says to fail after 2ms w/ no response, but evidence
13608886a6fdSDoug Anderson 		 * shows that sometimes the cmd11 interrupt takes over 130ms.
13618886a6fdSDoug Anderson 		 * We'll set to 500ms, plus an extra jiffy just in case jiffies
13628886a6fdSDoug Anderson 		 * is just about to roll over.
136349ba0302SDoug Anderson 		 *
136449ba0302SDoug Anderson 		 * We do this whole thing under spinlock and only if the
13651ad0dcb9Swangjianli 		 * command hasn't already completed (indicating the irq
136649ba0302SDoug Anderson 		 * already ran so we don't want the timeout).
13675c935165SDoug Anderson 		 */
136849ba0302SDoug Anderson 		spin_lock_irqsave(&host->irq_lock, irqflags);
136949ba0302SDoug Anderson 		if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
13705c935165SDoug Anderson 			mod_timer(&host->cmd11_timer,
13718886a6fdSDoug Anderson 				jiffies + msecs_to_jiffies(500) + 1);
137249ba0302SDoug Anderson 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
13735c935165SDoug Anderson 	}
13745c935165SDoug Anderson 
137590c2143aSSeungwon Jeon 	host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
1376f95f3850SWill Newton }
1377f95f3850SWill Newton 
dw_mci_start_request(struct dw_mci * host,struct dw_mci_slot * slot)1378053b3ce6SSeungwon Jeon static void dw_mci_start_request(struct dw_mci *host,
1379053b3ce6SSeungwon Jeon 				 struct dw_mci_slot *slot)
1380053b3ce6SSeungwon Jeon {
1381053b3ce6SSeungwon Jeon 	struct mmc_request *mrq = slot->mrq;
1382053b3ce6SSeungwon Jeon 	struct mmc_command *cmd;
1383053b3ce6SSeungwon Jeon 
1384053b3ce6SSeungwon Jeon 	cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1385053b3ce6SSeungwon Jeon 	__dw_mci_start_request(host, slot, cmd);
1386053b3ce6SSeungwon Jeon }
1387053b3ce6SSeungwon Jeon 
13887456caaeSJames 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)1389f95f3850SWill Newton static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1390f95f3850SWill Newton 				 struct mmc_request *mrq)
1391f95f3850SWill Newton {
1392f95f3850SWill Newton 	dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1393f95f3850SWill Newton 		 host->state);
1394f95f3850SWill Newton 
1395f95f3850SWill Newton 	slot->mrq = mrq;
1396f95f3850SWill Newton 
139701730558SDoug Anderson 	if (host->state == STATE_WAITING_CMD11_DONE) {
139801730558SDoug Anderson 		dev_warn(&slot->mmc->class_dev,
139901730558SDoug Anderson 			 "Voltage change didn't complete\n");
140001730558SDoug Anderson 		/*
140101730558SDoug Anderson 		 * this case isn't expected to happen, so we can
140201730558SDoug Anderson 		 * either crash here or just try to continue on
140301730558SDoug Anderson 		 * in the closest possible state
140401730558SDoug Anderson 		 */
140501730558SDoug Anderson 		host->state = STATE_IDLE;
140601730558SDoug Anderson 	}
140701730558SDoug Anderson 
1408f95f3850SWill Newton 	if (host->state == STATE_IDLE) {
1409f95f3850SWill Newton 		host->state = STATE_SENDING_CMD;
1410f95f3850SWill Newton 		dw_mci_start_request(host, slot);
1411f95f3850SWill Newton 	} else {
1412f95f3850SWill Newton 		list_add_tail(&slot->queue_node, &host->queue);
1413f95f3850SWill Newton 	}
1414f95f3850SWill Newton }
1415f95f3850SWill Newton 
dw_mci_request(struct mmc_host * mmc,struct mmc_request * mrq)1416f95f3850SWill Newton static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1417f95f3850SWill Newton {
1418f95f3850SWill Newton 	struct dw_mci_slot *slot = mmc_priv(mmc);
1419f95f3850SWill Newton 	struct dw_mci *host = slot->host;
1420f95f3850SWill Newton 
1421f95f3850SWill Newton 	WARN_ON(slot->mrq);
1422f95f3850SWill Newton 
14237456caaeSJames Hogan 	/*
14247456caaeSJames Hogan 	 * The check for card presence and queueing of the request must be
14257456caaeSJames Hogan 	 * atomic, otherwise the card could be removed in between and the
14267456caaeSJames Hogan 	 * request wouldn't fail until another card was inserted.
14277456caaeSJames Hogan 	 */
14287456caaeSJames Hogan 
142956f6911cSShawn Lin 	if (!dw_mci_get_cd(mmc)) {
1430f95f3850SWill Newton 		mrq->cmd->error = -ENOMEDIUM;
1431f95f3850SWill Newton 		mmc_request_done(mmc, mrq);
1432f95f3850SWill Newton 		return;
1433f95f3850SWill Newton 	}
1434f95f3850SWill Newton 
143556f6911cSShawn Lin 	spin_lock_bh(&host->lock);
143656f6911cSShawn Lin 
1437f95f3850SWill Newton 	dw_mci_queue_request(host, slot, mrq);
14387456caaeSJames Hogan 
14397456caaeSJames Hogan 	spin_unlock_bh(&host->lock);
1440f95f3850SWill Newton }
1441f95f3850SWill Newton 
dw_mci_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)1442f95f3850SWill Newton static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1443f95f3850SWill Newton {
1444f95f3850SWill Newton 	struct dw_mci_slot *slot = mmc_priv(mmc);
1445e95baf13SArnd Bergmann 	const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
144641babf75SJaehoon Chung 	u32 regs;
144751da2240SYuvaraj CD 	int ret;
1448f95f3850SWill Newton 
1449f95f3850SWill Newton 	switch (ios->bus_width) {
1450f95f3850SWill Newton 	case MMC_BUS_WIDTH_4:
1451f95f3850SWill Newton 		slot->ctype = SDMMC_CTYPE_4BIT;
1452f95f3850SWill Newton 		break;
1453c9b2a06fSJaehoon Chung 	case MMC_BUS_WIDTH_8:
1454c9b2a06fSJaehoon Chung 		slot->ctype = SDMMC_CTYPE_8BIT;
1455c9b2a06fSJaehoon Chung 		break;
1456b2f7cb45SJaehoon Chung 	default:
1457b2f7cb45SJaehoon Chung 		/* set default 1 bit mode */
1458b2f7cb45SJaehoon Chung 		slot->ctype = SDMMC_CTYPE_1BIT;
1459f95f3850SWill Newton 	}
1460f95f3850SWill Newton 
146141babf75SJaehoon Chung 	regs = mci_readl(slot->host, UHS_REG);
14623f514291SSeungwon Jeon 
14633f514291SSeungwon Jeon 	/* DDR mode set */
146480113132SSeungwon Jeon 	if (ios->timing == MMC_TIMING_MMC_DDR52 ||
14657cc8d580SJaehoon Chung 	    ios->timing == MMC_TIMING_UHS_DDR50 ||
146680113132SSeungwon Jeon 	    ios->timing == MMC_TIMING_MMC_HS400)
1467c69042a5SHyeonsu Kim 		regs |= ((0x1 << slot->id) << 16);
14683f514291SSeungwon Jeon 	else
1469c69042a5SHyeonsu Kim 		regs &= ~((0x1 << slot->id) << 16);
14703f514291SSeungwon Jeon 
147141babf75SJaehoon Chung 	mci_writel(slot->host, UHS_REG, regs);
1472f1d2736cSSeungwon Jeon 	slot->host->timing = ios->timing;
147341babf75SJaehoon Chung 
1474f95f3850SWill Newton 	/*
1475f95f3850SWill Newton 	 * Use mirror of ios->clock to prevent race with mmc
1476f95f3850SWill Newton 	 * core ios update when finding the minimum.
1477f95f3850SWill Newton 	 */
1478f95f3850SWill Newton 	slot->clock = ios->clock;
1479f95f3850SWill Newton 
1480cb27a843SJames Hogan 	if (drv_data && drv_data->set_ios)
1481cb27a843SJames Hogan 		drv_data->set_ios(slot->host, ios);
1482800d78bfSThomas Abraham 
1483f95f3850SWill Newton 	switch (ios->power_mode) {
1484f95f3850SWill Newton 	case MMC_POWER_UP:
148551da2240SYuvaraj CD 		if (!IS_ERR(mmc->supply.vmmc)) {
148651da2240SYuvaraj CD 			ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
148751da2240SYuvaraj CD 					ios->vdd);
148851da2240SYuvaraj CD 			if (ret) {
148951da2240SYuvaraj CD 				dev_err(slot->host->dev,
149051da2240SYuvaraj CD 					"failed to enable vmmc regulator\n");
149151da2240SYuvaraj CD 				/*return, if failed turn on vmmc*/
149251da2240SYuvaraj CD 				return;
149351da2240SYuvaraj CD 			}
149451da2240SYuvaraj CD 		}
149529d0d161SDoug Anderson 		set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
149629d0d161SDoug Anderson 		regs = mci_readl(slot->host, PWREN);
149729d0d161SDoug Anderson 		regs |= (1 << slot->id);
149829d0d161SDoug Anderson 		mci_writel(slot->host, PWREN, regs);
149929d0d161SDoug Anderson 		break;
150029d0d161SDoug Anderson 	case MMC_POWER_ON:
1501d1f1dd86SDoug Anderson 		if (!slot->host->vqmmc_enabled) {
1502d1f1dd86SDoug Anderson 			if (!IS_ERR(mmc->supply.vqmmc)) {
150351da2240SYuvaraj CD 				ret = regulator_enable(mmc->supply.vqmmc);
150451da2240SYuvaraj CD 				if (ret < 0)
150551da2240SYuvaraj CD 					dev_err(slot->host->dev,
1506d1f1dd86SDoug Anderson 						"failed to enable vqmmc\n");
150751da2240SYuvaraj CD 				else
150851da2240SYuvaraj CD 					slot->host->vqmmc_enabled = true;
1509d1f1dd86SDoug Anderson 
1510d1f1dd86SDoug Anderson 			} else {
1511d1f1dd86SDoug Anderson 				/* Keep track so we don't reset again */
1512d1f1dd86SDoug Anderson 				slot->host->vqmmc_enabled = true;
1513d1f1dd86SDoug Anderson 			}
1514d1f1dd86SDoug Anderson 
1515d1f1dd86SDoug Anderson 			/* Reset our state machine after powering on */
1516d1f1dd86SDoug Anderson 			dw_mci_ctrl_reset(slot->host,
1517d1f1dd86SDoug Anderson 					  SDMMC_CTRL_ALL_RESET_FLAGS);
151851da2240SYuvaraj CD 		}
1519655babbdSDoug Anderson 
1520655babbdSDoug Anderson 		/* Adjust clock / bus width after power is up */
1521655babbdSDoug Anderson 		dw_mci_setup_bus(slot, false);
1522655babbdSDoug Anderson 
1523e6f34e2fSJames Hogan 		break;
1524e6f34e2fSJames Hogan 	case MMC_POWER_OFF:
1525655babbdSDoug Anderson 		/* Turn clock off before power goes down */
1526655babbdSDoug Anderson 		dw_mci_setup_bus(slot, false);
1527655babbdSDoug Anderson 
152851da2240SYuvaraj CD 		if (!IS_ERR(mmc->supply.vmmc))
152951da2240SYuvaraj CD 			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
153051da2240SYuvaraj CD 
1531d1f1dd86SDoug Anderson 		if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
153251da2240SYuvaraj CD 			regulator_disable(mmc->supply.vqmmc);
153351da2240SYuvaraj CD 		slot->host->vqmmc_enabled = false;
153451da2240SYuvaraj CD 
15354366dcc5SJaehoon Chung 		regs = mci_readl(slot->host, PWREN);
15364366dcc5SJaehoon Chung 		regs &= ~(1 << slot->id);
15374366dcc5SJaehoon Chung 		mci_writel(slot->host, PWREN, regs);
1538f95f3850SWill Newton 		break;
1539f95f3850SWill Newton 	default:
1540f95f3850SWill Newton 		break;
1541f95f3850SWill Newton 	}
1542655babbdSDoug Anderson 
1543655babbdSDoug Anderson 	if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1544655babbdSDoug Anderson 		slot->host->state = STATE_IDLE;
1545f95f3850SWill Newton }
1546f95f3850SWill Newton 
dw_mci_card_busy(struct mmc_host * mmc)154701730558SDoug Anderson static int dw_mci_card_busy(struct mmc_host *mmc)
154801730558SDoug Anderson {
154901730558SDoug Anderson 	struct dw_mci_slot *slot = mmc_priv(mmc);
155001730558SDoug Anderson 	u32 status;
155101730558SDoug Anderson 
155201730558SDoug Anderson 	/*
155301730558SDoug Anderson 	 * Check the busy bit which is low when DAT[3:0]
155401730558SDoug Anderson 	 * (the data lines) are 0000
155501730558SDoug Anderson 	 */
155601730558SDoug Anderson 	status = mci_readl(slot->host, STATUS);
155701730558SDoug Anderson 
155801730558SDoug Anderson 	return !!(status & SDMMC_STATUS_BUSY);
155901730558SDoug Anderson }
156001730558SDoug Anderson 
dw_mci_switch_voltage(struct mmc_host * mmc,struct mmc_ios * ios)156101730558SDoug Anderson static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
156201730558SDoug Anderson {
156301730558SDoug Anderson 	struct dw_mci_slot *slot = mmc_priv(mmc);
156401730558SDoug Anderson 	struct dw_mci *host = slot->host;
15658f7849c4SZhangfei Gao 	const struct dw_mci_drv_data *drv_data = host->drv_data;
156601730558SDoug Anderson 	u32 uhs;
156701730558SDoug Anderson 	u32 v18 = SDMMC_UHS_18V << slot->id;
156801730558SDoug Anderson 	int ret;
156901730558SDoug Anderson 
15708f7849c4SZhangfei Gao 	if (drv_data && drv_data->switch_voltage)
15718f7849c4SZhangfei Gao 		return drv_data->switch_voltage(mmc, ios);
15728f7849c4SZhangfei Gao 
157301730558SDoug Anderson 	/*
157401730558SDoug Anderson 	 * Program the voltage.  Note that some instances of dw_mmc may use
157501730558SDoug Anderson 	 * the UHS_REG for this.  For other instances (like exynos) the UHS_REG
157601730558SDoug Anderson 	 * does no harm but you need to set the regulator directly.  Try both.
157701730558SDoug Anderson 	 */
157801730558SDoug Anderson 	uhs = mci_readl(host, UHS_REG);
1579e0848f5dSDouglas Anderson 	if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
158001730558SDoug Anderson 		uhs &= ~v18;
1581e0848f5dSDouglas Anderson 	else
158201730558SDoug Anderson 		uhs |= v18;
1583e0848f5dSDouglas Anderson 
158401730558SDoug Anderson 	if (!IS_ERR(mmc->supply.vqmmc)) {
1585e0848f5dSDouglas Anderson 		ret = mmc_regulator_set_vqmmc(mmc, ios);
15869cbe0fc8SMarek Vasut 		if (ret < 0) {
1587b19caf37SDoug Anderson 			dev_dbg(&mmc->class_dev,
1588e0848f5dSDouglas Anderson 					 "Regulator set error %d - %s V\n",
1589e0848f5dSDouglas Anderson 					 ret, uhs & v18 ? "1.8" : "3.3");
159001730558SDoug Anderson 			return ret;
159101730558SDoug Anderson 		}
159201730558SDoug Anderson 	}
159301730558SDoug Anderson 	mci_writel(host, UHS_REG, uhs);
159401730558SDoug Anderson 
159501730558SDoug Anderson 	return 0;
159601730558SDoug Anderson }
159701730558SDoug Anderson 
dw_mci_get_ro(struct mmc_host * mmc)1598f95f3850SWill Newton static int dw_mci_get_ro(struct mmc_host *mmc)
1599f95f3850SWill Newton {
1600f95f3850SWill Newton 	int read_only;
1601f95f3850SWill Newton 	struct dw_mci_slot *slot = mmc_priv(mmc);
16029795a846SJaehoon Chung 	int gpio_ro = mmc_gpio_get_ro(mmc);
1603f95f3850SWill Newton 
1604f95f3850SWill Newton 	/* Use platform get_ro function, else try on board write protect */
1605287980e4SArnd Bergmann 	if (gpio_ro >= 0)
16069795a846SJaehoon Chung 		read_only = gpio_ro;
1607f95f3850SWill Newton 	else
1608f95f3850SWill Newton 		read_only =
1609f95f3850SWill Newton 			mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1610f95f3850SWill Newton 
1611f95f3850SWill Newton 	dev_dbg(&mmc->class_dev, "card is %s\n",
1612f95f3850SWill Newton 		read_only ? "read-only" : "read-write");
1613f95f3850SWill Newton 
1614f95f3850SWill Newton 	return read_only;
1615f95f3850SWill Newton }
1616f95f3850SWill Newton 
dw_mci_hw_reset(struct mmc_host * mmc)1617935a665eSShawn Lin static void dw_mci_hw_reset(struct mmc_host *mmc)
1618935a665eSShawn Lin {
1619935a665eSShawn Lin 	struct dw_mci_slot *slot = mmc_priv(mmc);
1620935a665eSShawn Lin 	struct dw_mci *host = slot->host;
1621935a665eSShawn Lin 	int reset;
1622935a665eSShawn Lin 
1623935a665eSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC)
1624935a665eSShawn Lin 		dw_mci_idmac_reset(host);
1625935a665eSShawn Lin 
1626935a665eSShawn Lin 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1627935a665eSShawn Lin 				     SDMMC_CTRL_FIFO_RESET))
1628935a665eSShawn Lin 		return;
1629935a665eSShawn Lin 
1630935a665eSShawn Lin 	/*
1631935a665eSShawn Lin 	 * According to eMMC spec, card reset procedure:
1632935a665eSShawn Lin 	 * tRstW >= 1us:   RST_n pulse width
1633935a665eSShawn Lin 	 * tRSCA >= 200us: RST_n to Command time
1634935a665eSShawn Lin 	 * tRSTH >= 1us:   RST_n high period
1635935a665eSShawn Lin 	 */
1636935a665eSShawn Lin 	reset = mci_readl(host, RST_N);
1637935a665eSShawn Lin 	reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1638935a665eSShawn Lin 	mci_writel(host, RST_N, reset);
1639935a665eSShawn Lin 	usleep_range(1, 2);
1640935a665eSShawn Lin 	reset |= SDMMC_RST_HWACTIVE << slot->id;
1641935a665eSShawn Lin 	mci_writel(host, RST_N, reset);
1642935a665eSShawn Lin 	usleep_range(200, 300);
1643935a665eSShawn Lin }
1644935a665eSShawn Lin 
dw_mci_prepare_sdio_irq(struct dw_mci_slot * slot,bool prepare)164561840edcSUlf Hansson static void dw_mci_prepare_sdio_irq(struct dw_mci_slot *slot, bool prepare)
1646b24c8b26SDoug Anderson {
1647b24c8b26SDoug Anderson 	struct dw_mci *host = slot->host;
164861840edcSUlf Hansson 	const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
164961840edcSUlf Hansson 	u32 clk_en_a_old;
165061840edcSUlf Hansson 	u32 clk_en_a;
1651b24c8b26SDoug Anderson 
16529623b5b9SDoug Anderson 	/*
16539623b5b9SDoug Anderson 	 * Low power mode will stop the card clock when idle.  According to the
16549623b5b9SDoug Anderson 	 * description of the CLKENA register we should disable low power mode
16559623b5b9SDoug Anderson 	 * for SDIO cards if we need SDIO interrupts to work.
16569623b5b9SDoug Anderson 	 */
16579623b5b9SDoug Anderson 
1658b24c8b26SDoug Anderson 	clk_en_a_old = mci_readl(host, CLKENA);
165961840edcSUlf Hansson 	if (prepare) {
1660b24c8b26SDoug Anderson 		set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1661b24c8b26SDoug Anderson 		clk_en_a = clk_en_a_old & ~clken_low_pwr;
1662b24c8b26SDoug Anderson 	} else {
1663b24c8b26SDoug Anderson 		clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1664b24c8b26SDoug Anderson 		clk_en_a = clk_en_a_old | clken_low_pwr;
1665b24c8b26SDoug Anderson 	}
1666b24c8b26SDoug Anderson 
1667b24c8b26SDoug Anderson 	if (clk_en_a != clk_en_a_old) {
1668b24c8b26SDoug Anderson 		mci_writel(host, CLKENA, clk_en_a);
166961840edcSUlf Hansson 		mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT,
167061840edcSUlf Hansson 			     0);
16719623b5b9SDoug Anderson 	}
1672b24c8b26SDoug Anderson }
16739623b5b9SDoug Anderson 
__dw_mci_enable_sdio_irq(struct dw_mci_slot * slot,int enb)167432dba737SUlf Hansson static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb)
16751a5c8e1fSShashidhar Hiremath {
16761a5c8e1fSShashidhar Hiremath 	struct dw_mci *host = slot->host;
1677f8c58c11SDoug Anderson 	unsigned long irqflags;
16781a5c8e1fSShashidhar Hiremath 	u32 int_mask;
16791a5c8e1fSShashidhar Hiremath 
1680f8c58c11SDoug Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
1681f8c58c11SDoug Anderson 
16821a5c8e1fSShashidhar Hiremath 	/* Enable/disable Slot Specific SDIO interrupt */
16831a5c8e1fSShashidhar Hiremath 	int_mask = mci_readl(host, INTMASK);
1684b24c8b26SDoug Anderson 	if (enb)
1685b24c8b26SDoug Anderson 		int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1686b24c8b26SDoug Anderson 	else
1687b24c8b26SDoug Anderson 		int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1688b24c8b26SDoug Anderson 	mci_writel(host, INTMASK, int_mask);
1689f8c58c11SDoug Anderson 
1690f8c58c11SDoug Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
16911a5c8e1fSShashidhar Hiremath }
16921a5c8e1fSShashidhar Hiremath 
dw_mci_enable_sdio_irq(struct mmc_host * mmc,int enb)169332dba737SUlf Hansson static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
169432dba737SUlf Hansson {
169532dba737SUlf Hansson 	struct dw_mci_slot *slot = mmc_priv(mmc);
1696ca8971caSUlf Hansson 	struct dw_mci *host = slot->host;
169732dba737SUlf Hansson 
169861840edcSUlf Hansson 	dw_mci_prepare_sdio_irq(slot, enb);
169932dba737SUlf Hansson 	__dw_mci_enable_sdio_irq(slot, enb);
1700ca8971caSUlf Hansson 
1701ca8971caSUlf Hansson 	/* Avoid runtime suspending the device when SDIO IRQ is enabled */
1702ca8971caSUlf Hansson 	if (enb)
1703ca8971caSUlf Hansson 		pm_runtime_get_noresume(host->dev);
1704ca8971caSUlf Hansson 	else
1705ca8971caSUlf Hansson 		pm_runtime_put_noidle(host->dev);
170632dba737SUlf Hansson }
170732dba737SUlf Hansson 
dw_mci_ack_sdio_irq(struct mmc_host * mmc)170832dba737SUlf Hansson static void dw_mci_ack_sdio_irq(struct mmc_host *mmc)
170932dba737SUlf Hansson {
171032dba737SUlf Hansson 	struct dw_mci_slot *slot = mmc_priv(mmc);
171132dba737SUlf Hansson 
171232dba737SUlf Hansson 	__dw_mci_enable_sdio_irq(slot, 1);
171332dba737SUlf Hansson }
171432dba737SUlf Hansson 
dw_mci_execute_tuning(struct mmc_host * mmc,u32 opcode)17150976f16dSSeungwon Jeon static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
17160976f16dSSeungwon Jeon {
17170976f16dSSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
17180976f16dSSeungwon Jeon 	struct dw_mci *host = slot->host;
17190976f16dSSeungwon Jeon 	const struct dw_mci_drv_data *drv_data = host->drv_data;
17200e3a22c0SShawn Lin 	int err = -EINVAL;
17210976f16dSSeungwon Jeon 
17220976f16dSSeungwon Jeon 	if (drv_data && drv_data->execute_tuning)
17239979dbe5SChaotian Jing 		err = drv_data->execute_tuning(slot, opcode);
17240976f16dSSeungwon Jeon 	return err;
17250976f16dSSeungwon Jeon }
17260976f16dSSeungwon Jeon 
dw_mci_prepare_hs400_tuning(struct mmc_host * mmc,struct mmc_ios * ios)17270e3a22c0SShawn Lin static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
17280e3a22c0SShawn Lin 				       struct mmc_ios *ios)
172980113132SSeungwon Jeon {
173080113132SSeungwon Jeon 	struct dw_mci_slot *slot = mmc_priv(mmc);
173180113132SSeungwon Jeon 	struct dw_mci *host = slot->host;
173280113132SSeungwon Jeon 	const struct dw_mci_drv_data *drv_data = host->drv_data;
173380113132SSeungwon Jeon 
173480113132SSeungwon Jeon 	if (drv_data && drv_data->prepare_hs400_tuning)
173580113132SSeungwon Jeon 		return drv_data->prepare_hs400_tuning(host, ios);
173680113132SSeungwon Jeon 
173780113132SSeungwon Jeon 	return 0;
173880113132SSeungwon Jeon }
173980113132SSeungwon Jeon 
dw_mci_reset(struct dw_mci * host)17404e7392b2SShawn Lin static bool dw_mci_reset(struct dw_mci *host)
17414e7392b2SShawn Lin {
17424e7392b2SShawn Lin 	u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
17434e7392b2SShawn Lin 	bool ret = false;
1744bc2dcc1aSShawn Lin 	u32 status = 0;
17454e7392b2SShawn Lin 
17464e7392b2SShawn Lin 	/*
17474e7392b2SShawn Lin 	 * Resetting generates a block interrupt, hence setting
17484e7392b2SShawn Lin 	 * the scatter-gather pointer to NULL.
17494e7392b2SShawn Lin 	 */
17504e7392b2SShawn Lin 	if (host->sg) {
17514e7392b2SShawn Lin 		sg_miter_stop(&host->sg_miter);
17524e7392b2SShawn Lin 		host->sg = NULL;
17534e7392b2SShawn Lin 	}
17544e7392b2SShawn Lin 
17554e7392b2SShawn Lin 	if (host->use_dma)
17564e7392b2SShawn Lin 		flags |= SDMMC_CTRL_DMA_RESET;
17574e7392b2SShawn Lin 
17584e7392b2SShawn Lin 	if (dw_mci_ctrl_reset(host, flags)) {
17594e7392b2SShawn Lin 		/*
1760bc2dcc1aSShawn Lin 		 * In all cases we clear the RAWINTS
1761bc2dcc1aSShawn Lin 		 * register to clear any interrupts.
17624e7392b2SShawn Lin 		 */
17634e7392b2SShawn Lin 		mci_writel(host, RINTSTS, 0xFFFFFFFF);
17644e7392b2SShawn Lin 
1765bc2dcc1aSShawn Lin 		if (!host->use_dma) {
1766bc2dcc1aSShawn Lin 			ret = true;
1767bc2dcc1aSShawn Lin 			goto ciu_out;
1768bc2dcc1aSShawn Lin 		}
17694e7392b2SShawn Lin 
1770bc2dcc1aSShawn Lin 		/* Wait for dma_req to be cleared */
17714e7392b2SShawn Lin 		if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
17724e7392b2SShawn Lin 					      status,
17734e7392b2SShawn Lin 					      !(status & SDMMC_STATUS_DMA_REQ),
17744e7392b2SShawn Lin 					      1, 500 * USEC_PER_MSEC)) {
17754e7392b2SShawn Lin 			dev_err(host->dev,
1776bc2dcc1aSShawn Lin 				"%s: Timeout waiting for dma_req to be cleared\n",
17774e7392b2SShawn Lin 				__func__);
17784e7392b2SShawn Lin 			goto ciu_out;
17794e7392b2SShawn Lin 		}
17804e7392b2SShawn Lin 
17814e7392b2SShawn Lin 		/* when using DMA next we reset the fifo again */
17824e7392b2SShawn Lin 		if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
17834e7392b2SShawn Lin 			goto ciu_out;
17844e7392b2SShawn Lin 	} else {
17854e7392b2SShawn Lin 		/* if the controller reset bit did clear, then set clock regs */
17864e7392b2SShawn Lin 		if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
17874e7392b2SShawn Lin 			dev_err(host->dev,
17884e7392b2SShawn Lin 				"%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
17894e7392b2SShawn Lin 				__func__);
17904e7392b2SShawn Lin 			goto ciu_out;
17914e7392b2SShawn Lin 		}
17924e7392b2SShawn Lin 	}
17934e7392b2SShawn Lin 
17944e7392b2SShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC)
179547b7de2fSEvgeniy Didin 		/* It is also required that we reinit idmac */
179647b7de2fSEvgeniy Didin 		dw_mci_idmac_init(host);
17974e7392b2SShawn Lin 
17984e7392b2SShawn Lin 	ret = true;
17994e7392b2SShawn Lin 
18004e7392b2SShawn Lin ciu_out:
18014e7392b2SShawn Lin 	/* After a CTRL reset we need to have CIU set clock registers  */
180242f989c0SJaehoon Chung 	mci_send_cmd(host->slot, SDMMC_CMD_UPD_CLK, 0);
18034e7392b2SShawn Lin 
18044e7392b2SShawn Lin 	return ret;
18054e7392b2SShawn Lin }
18064e7392b2SShawn Lin 
1807f95f3850SWill Newton static const struct mmc_host_ops dw_mci_ops = {
1808f95f3850SWill Newton 	.request		= dw_mci_request,
18099aa51408SSeungwon Jeon 	.pre_req		= dw_mci_pre_req,
18109aa51408SSeungwon Jeon 	.post_req		= dw_mci_post_req,
1811f95f3850SWill Newton 	.set_ios		= dw_mci_set_ios,
1812f95f3850SWill Newton 	.get_ro			= dw_mci_get_ro,
1813f95f3850SWill Newton 	.get_cd			= dw_mci_get_cd,
181432f18e59SWolfram Sang 	.card_hw_reset          = dw_mci_hw_reset,
18151a5c8e1fSShashidhar Hiremath 	.enable_sdio_irq	= dw_mci_enable_sdio_irq,
181632dba737SUlf Hansson 	.ack_sdio_irq		= dw_mci_ack_sdio_irq,
18170976f16dSSeungwon Jeon 	.execute_tuning		= dw_mci_execute_tuning,
181801730558SDoug Anderson 	.card_busy		= dw_mci_card_busy,
181901730558SDoug Anderson 	.start_signal_voltage_switch = dw_mci_switch_voltage,
182080113132SSeungwon Jeon 	.prepare_hs400_tuning	= dw_mci_prepare_hs400_tuning,
1821f95f3850SWill Newton };
1822f95f3850SWill Newton 
18232b8ac062SVincent Whitchurch #ifdef CONFIG_FAULT_INJECTION
dw_mci_fault_timer(struct hrtimer * t)18242b8ac062SVincent Whitchurch static enum hrtimer_restart dw_mci_fault_timer(struct hrtimer *t)
18252b8ac062SVincent Whitchurch {
18262b8ac062SVincent Whitchurch 	struct dw_mci *host = container_of(t, struct dw_mci, fault_timer);
18272b8ac062SVincent Whitchurch 	unsigned long flags;
18282b8ac062SVincent Whitchurch 
18292b8ac062SVincent Whitchurch 	spin_lock_irqsave(&host->irq_lock, flags);
18302b8ac062SVincent Whitchurch 
183126391e49SVincent Whitchurch 	/*
183226391e49SVincent Whitchurch 	 * Only inject an error if we haven't already got an error or data over
183326391e49SVincent Whitchurch 	 * interrupt.
183426391e49SVincent Whitchurch 	 */
183526391e49SVincent Whitchurch 	if (!host->data_status) {
18362b8ac062SVincent Whitchurch 		host->data_status = SDMMC_INT_DCRC;
18372b8ac062SVincent Whitchurch 		set_bit(EVENT_DATA_ERROR, &host->pending_events);
18382b8ac062SVincent Whitchurch 		tasklet_schedule(&host->tasklet);
183926391e49SVincent Whitchurch 	}
18402b8ac062SVincent Whitchurch 
18412b8ac062SVincent Whitchurch 	spin_unlock_irqrestore(&host->irq_lock, flags);
18422b8ac062SVincent Whitchurch 
18432b8ac062SVincent Whitchurch 	return HRTIMER_NORESTART;
18442b8ac062SVincent Whitchurch }
18452b8ac062SVincent Whitchurch 
dw_mci_start_fault_timer(struct dw_mci * host)18462b8ac062SVincent Whitchurch static void dw_mci_start_fault_timer(struct dw_mci *host)
18472b8ac062SVincent Whitchurch {
18482b8ac062SVincent Whitchurch 	struct mmc_data *data = host->data;
18492b8ac062SVincent Whitchurch 
18502b8ac062SVincent Whitchurch 	if (!data || data->blocks <= 1)
18512b8ac062SVincent Whitchurch 		return;
18522b8ac062SVincent Whitchurch 
18532b8ac062SVincent Whitchurch 	if (!should_fail(&host->fail_data_crc, 1))
18542b8ac062SVincent Whitchurch 		return;
18552b8ac062SVincent Whitchurch 
18562b8ac062SVincent Whitchurch 	/*
18572b8ac062SVincent Whitchurch 	 * Try to inject the error at random points during the data transfer.
18582b8ac062SVincent Whitchurch 	 */
18592b8ac062SVincent Whitchurch 	hrtimer_start(&host->fault_timer,
186081895a65SJason A. Donenfeld 		      ms_to_ktime(get_random_u32_below(25)),
18612b8ac062SVincent Whitchurch 		      HRTIMER_MODE_REL);
18622b8ac062SVincent Whitchurch }
18632b8ac062SVincent Whitchurch 
dw_mci_stop_fault_timer(struct dw_mci * host)18642b8ac062SVincent Whitchurch static void dw_mci_stop_fault_timer(struct dw_mci *host)
18652b8ac062SVincent Whitchurch {
18662b8ac062SVincent Whitchurch 	hrtimer_cancel(&host->fault_timer);
18672b8ac062SVincent Whitchurch }
18682b8ac062SVincent Whitchurch 
dw_mci_init_fault(struct dw_mci * host)18692b8ac062SVincent Whitchurch static void dw_mci_init_fault(struct dw_mci *host)
18702b8ac062SVincent Whitchurch {
18712b8ac062SVincent Whitchurch 	host->fail_data_crc = (struct fault_attr) FAULT_ATTR_INITIALIZER;
18722b8ac062SVincent Whitchurch 
18732b8ac062SVincent Whitchurch 	hrtimer_init(&host->fault_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
18742b8ac062SVincent Whitchurch 	host->fault_timer.function = dw_mci_fault_timer;
18752b8ac062SVincent Whitchurch }
18762b8ac062SVincent Whitchurch #else
dw_mci_init_fault(struct dw_mci * host)18772b8ac062SVincent Whitchurch static void dw_mci_init_fault(struct dw_mci *host)
18782b8ac062SVincent Whitchurch {
18792b8ac062SVincent Whitchurch }
18802b8ac062SVincent Whitchurch 
dw_mci_start_fault_timer(struct dw_mci * host)18812b8ac062SVincent Whitchurch static void dw_mci_start_fault_timer(struct dw_mci *host)
18822b8ac062SVincent Whitchurch {
18832b8ac062SVincent Whitchurch }
18842b8ac062SVincent Whitchurch 
dw_mci_stop_fault_timer(struct dw_mci * host)18852b8ac062SVincent Whitchurch static void dw_mci_stop_fault_timer(struct dw_mci *host)
18862b8ac062SVincent Whitchurch {
18872b8ac062SVincent Whitchurch }
18882b8ac062SVincent Whitchurch #endif
18892b8ac062SVincent Whitchurch 
dw_mci_request_end(struct dw_mci * host,struct mmc_request * mrq)1890f95f3850SWill Newton static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1891f95f3850SWill Newton 	__releases(&host->lock)
1892f95f3850SWill Newton 	__acquires(&host->lock)
1893f95f3850SWill Newton {
1894f95f3850SWill Newton 	struct dw_mci_slot *slot;
189542f989c0SJaehoon Chung 	struct mmc_host	*prev_mmc = host->slot->mmc;
1896f95f3850SWill Newton 
1897f95f3850SWill Newton 	WARN_ON(host->cmd || host->data);
1898f95f3850SWill Newton 
189942f989c0SJaehoon Chung 	host->slot->mrq = NULL;
1900f95f3850SWill Newton 	host->mrq = NULL;
1901f95f3850SWill Newton 	if (!list_empty(&host->queue)) {
1902f95f3850SWill Newton 		slot = list_entry(host->queue.next,
1903f95f3850SWill Newton 				  struct dw_mci_slot, queue_node);
1904f95f3850SWill Newton 		list_del(&slot->queue_node);
19054a90920cSThomas Abraham 		dev_vdbg(host->dev, "list not empty: %s is next\n",
1906f95f3850SWill Newton 			 mmc_hostname(slot->mmc));
1907f95f3850SWill Newton 		host->state = STATE_SENDING_CMD;
1908f95f3850SWill Newton 		dw_mci_start_request(host, slot);
1909f95f3850SWill Newton 	} else {
19104a90920cSThomas Abraham 		dev_vdbg(host->dev, "list empty\n");
191101730558SDoug Anderson 
191201730558SDoug Anderson 		if (host->state == STATE_SENDING_CMD11)
191301730558SDoug Anderson 			host->state = STATE_WAITING_CMD11_DONE;
191401730558SDoug Anderson 		else
1915f95f3850SWill Newton 			host->state = STATE_IDLE;
1916f95f3850SWill Newton 	}
1917f95f3850SWill Newton 
1918f95f3850SWill Newton 	spin_unlock(&host->lock);
1919f95f3850SWill Newton 	mmc_request_done(prev_mmc, mrq);
1920f95f3850SWill Newton 	spin_lock(&host->lock);
1921f95f3850SWill Newton }
1922f95f3850SWill Newton 
dw_mci_command_complete(struct dw_mci * host,struct mmc_command * cmd)1923e352c813SSeungwon Jeon static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
1924f95f3850SWill Newton {
1925f95f3850SWill Newton 	u32 status = host->cmd_status;
1926f95f3850SWill Newton 
1927f95f3850SWill Newton 	host->cmd_status = 0;
1928f95f3850SWill Newton 
1929f95f3850SWill Newton 	/* Read the response from the card (up to 16 bytes) */
1930f95f3850SWill Newton 	if (cmd->flags & MMC_RSP_PRESENT) {
1931f95f3850SWill Newton 		if (cmd->flags & MMC_RSP_136) {
1932f95f3850SWill Newton 			cmd->resp[3] = mci_readl(host, RESP0);
1933f95f3850SWill Newton 			cmd->resp[2] = mci_readl(host, RESP1);
1934f95f3850SWill Newton 			cmd->resp[1] = mci_readl(host, RESP2);
1935f95f3850SWill Newton 			cmd->resp[0] = mci_readl(host, RESP3);
1936f95f3850SWill Newton 		} else {
1937f95f3850SWill Newton 			cmd->resp[0] = mci_readl(host, RESP0);
1938f95f3850SWill Newton 			cmd->resp[1] = 0;
1939f95f3850SWill Newton 			cmd->resp[2] = 0;
1940f95f3850SWill Newton 			cmd->resp[3] = 0;
1941f95f3850SWill Newton 		}
1942f95f3850SWill Newton 	}
1943f95f3850SWill Newton 
1944f95f3850SWill Newton 	if (status & SDMMC_INT_RTO)
1945f95f3850SWill Newton 		cmd->error = -ETIMEDOUT;
1946f95f3850SWill Newton 	else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1947f95f3850SWill Newton 		cmd->error = -EILSEQ;
1948f95f3850SWill Newton 	else if (status & SDMMC_INT_RESP_ERR)
1949f95f3850SWill Newton 		cmd->error = -EIO;
1950f95f3850SWill Newton 	else
1951f95f3850SWill Newton 		cmd->error = 0;
1952f95f3850SWill Newton 
1953e352c813SSeungwon Jeon 	return cmd->error;
1954e352c813SSeungwon Jeon }
1955e352c813SSeungwon Jeon 
dw_mci_data_complete(struct dw_mci * host,struct mmc_data * data)1956e352c813SSeungwon Jeon static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1957e352c813SSeungwon Jeon {
195831bff450SSeungwon Jeon 	u32 status = host->data_status;
1959e352c813SSeungwon Jeon 
1960e352c813SSeungwon Jeon 	if (status & DW_MCI_DATA_ERROR_FLAGS) {
1961e352c813SSeungwon Jeon 		if (status & SDMMC_INT_DRTO) {
1962e352c813SSeungwon Jeon 			data->error = -ETIMEDOUT;
1963e352c813SSeungwon Jeon 		} else if (status & SDMMC_INT_DCRC) {
1964e352c813SSeungwon Jeon 			data->error = -EILSEQ;
1965e352c813SSeungwon Jeon 		} else if (status & SDMMC_INT_EBE) {
1966e352c813SSeungwon Jeon 			if (host->dir_status ==
1967e352c813SSeungwon Jeon 				DW_MCI_SEND_STATUS) {
1968e352c813SSeungwon Jeon 				/*
1969e352c813SSeungwon Jeon 				 * No data CRC status was returned.
1970e352c813SSeungwon Jeon 				 * The number of bytes transferred
1971e352c813SSeungwon Jeon 				 * will be exaggerated in PIO mode.
1972e352c813SSeungwon Jeon 				 */
1973e352c813SSeungwon Jeon 				data->bytes_xfered = 0;
1974e352c813SSeungwon Jeon 				data->error = -ETIMEDOUT;
1975e352c813SSeungwon Jeon 			} else if (host->dir_status ==
1976e352c813SSeungwon Jeon 					DW_MCI_RECV_STATUS) {
1977e7a1dec1SShawn Lin 				data->error = -EILSEQ;
1978e352c813SSeungwon Jeon 			}
1979e352c813SSeungwon Jeon 		} else {
1980e352c813SSeungwon Jeon 			/* SDMMC_INT_SBE is included */
1981e7a1dec1SShawn Lin 			data->error = -EILSEQ;
1982e352c813SSeungwon Jeon 		}
1983e352c813SSeungwon Jeon 
1984e6cc0123SDoug Anderson 		dev_dbg(host->dev, "data error, status 0x%08x\n", status);
1985e352c813SSeungwon Jeon 
1986e352c813SSeungwon Jeon 		/*
1987e352c813SSeungwon Jeon 		 * After an error, there may be data lingering
198831bff450SSeungwon Jeon 		 * in the FIFO
1989e352c813SSeungwon Jeon 		 */
19903a33a94cSSonny Rao 		dw_mci_reset(host);
1991e352c813SSeungwon Jeon 	} else {
1992e352c813SSeungwon Jeon 		data->bytes_xfered = data->blocks * data->blksz;
1993e352c813SSeungwon Jeon 		data->error = 0;
1994e352c813SSeungwon Jeon 	}
1995e352c813SSeungwon Jeon 
1996e352c813SSeungwon Jeon 	return data->error;
1997f95f3850SWill Newton }
1998f95f3850SWill Newton 
dw_mci_set_drto(struct dw_mci * host)199957e10486SAddy Ke static void dw_mci_set_drto(struct dw_mci *host)
200057e10486SAddy Ke {
200125d5417aSMårten Lindahl 	const struct dw_mci_drv_data *drv_data = host->drv_data;
200257e10486SAddy Ke 	unsigned int drto_clks;
20039d9491a7SDouglas Anderson 	unsigned int drto_div;
200457e10486SAddy Ke 	unsigned int drto_ms;
200593c23ae3SDouglas Anderson 	unsigned long irqflags;
200657e10486SAddy Ke 
200725d5417aSMårten Lindahl 	if (drv_data && drv_data->get_drto_clks)
200825d5417aSMårten Lindahl 		drto_clks = drv_data->get_drto_clks(host);
200925d5417aSMårten Lindahl 	else
201057e10486SAddy Ke 		drto_clks = mci_readl(host, TMOUT) >> 8;
20119d9491a7SDouglas Anderson 	drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
20129d9491a7SDouglas Anderson 	if (drto_div == 0)
20139d9491a7SDouglas Anderson 		drto_div = 1;
2014c7151602SEvgeniy Didin 
2015c7151602SEvgeniy Didin 	drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div,
20169d9491a7SDouglas Anderson 				   host->bus_hz);
201757e10486SAddy Ke 
201825d5417aSMårten Lindahl 	dev_dbg(host->dev, "drto_ms: %u\n", drto_ms);
201925d5417aSMårten Lindahl 
202057e10486SAddy Ke 	/* add a bit spare time */
202157e10486SAddy Ke 	drto_ms += 10;
202257e10486SAddy Ke 
202393c23ae3SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
202493c23ae3SDouglas Anderson 	if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
202593c23ae3SDouglas Anderson 		mod_timer(&host->dto_timer,
202693c23ae3SDouglas Anderson 			  jiffies + msecs_to_jiffies(drto_ms));
202793c23ae3SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
202857e10486SAddy Ke }
202957e10486SAddy Ke 
dw_mci_clear_pending_cmd_complete(struct dw_mci * host)20308892b705SDouglas Anderson static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host)
20318892b705SDouglas Anderson {
20328892b705SDouglas Anderson 	if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
20338892b705SDouglas Anderson 		return false;
20348892b705SDouglas Anderson 
20358892b705SDouglas Anderson 	/*
20368892b705SDouglas Anderson 	 * Really be certain that the timer has stopped.  This is a bit of
20378892b705SDouglas Anderson 	 * paranoia and could only really happen if we had really bad
20388892b705SDouglas Anderson 	 * interrupt latency and the interrupt routine and timeout were
20398892b705SDouglas Anderson 	 * running concurrently so that the del_timer() in the interrupt
20408892b705SDouglas Anderson 	 * handler couldn't run.
20418892b705SDouglas Anderson 	 */
20428892b705SDouglas Anderson 	WARN_ON(del_timer_sync(&host->cto_timer));
20438892b705SDouglas Anderson 	clear_bit(EVENT_CMD_COMPLETE, &host->pending_events);
20448892b705SDouglas Anderson 
20458892b705SDouglas Anderson 	return true;
20468892b705SDouglas Anderson }
20478892b705SDouglas Anderson 
dw_mci_clear_pending_data_complete(struct dw_mci * host)204893c23ae3SDouglas Anderson static bool dw_mci_clear_pending_data_complete(struct dw_mci *host)
204993c23ae3SDouglas Anderson {
205093c23ae3SDouglas Anderson 	if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
205193c23ae3SDouglas Anderson 		return false;
205293c23ae3SDouglas Anderson 
205393c23ae3SDouglas Anderson 	/* Extra paranoia just like dw_mci_clear_pending_cmd_complete() */
205493c23ae3SDouglas Anderson 	WARN_ON(del_timer_sync(&host->dto_timer));
205593c23ae3SDouglas Anderson 	clear_bit(EVENT_DATA_COMPLETE, &host->pending_events);
205693c23ae3SDouglas Anderson 
205793c23ae3SDouglas Anderson 	return true;
205893c23ae3SDouglas Anderson }
205993c23ae3SDouglas Anderson 
dw_mci_tasklet_func(struct tasklet_struct * t)20606078df15SEmil Renner Berthing static void dw_mci_tasklet_func(struct tasklet_struct *t)
2061f95f3850SWill Newton {
20626078df15SEmil Renner Berthing 	struct dw_mci *host = from_tasklet(host, t, tasklet);
2063f95f3850SWill Newton 	struct mmc_data	*data;
2064f95f3850SWill Newton 	struct mmc_command *cmd;
2065e352c813SSeungwon Jeon 	struct mmc_request *mrq;
2066f95f3850SWill Newton 	enum dw_mci_state state;
2067f95f3850SWill Newton 	enum dw_mci_state prev_state;
2068e352c813SSeungwon Jeon 	unsigned int err;
2069f95f3850SWill Newton 
2070f95f3850SWill Newton 	spin_lock(&host->lock);
2071f95f3850SWill Newton 
2072f95f3850SWill Newton 	state = host->state;
2073f95f3850SWill Newton 	data = host->data;
2074e352c813SSeungwon Jeon 	mrq = host->mrq;
2075f95f3850SWill Newton 
2076f95f3850SWill Newton 	do {
2077f95f3850SWill Newton 		prev_state = state;
2078f95f3850SWill Newton 
2079f95f3850SWill Newton 		switch (state) {
2080f95f3850SWill Newton 		case STATE_IDLE:
208101730558SDoug Anderson 		case STATE_WAITING_CMD11_DONE:
2082f95f3850SWill Newton 			break;
2083f95f3850SWill Newton 
208401730558SDoug Anderson 		case STATE_SENDING_CMD11:
2085f95f3850SWill Newton 		case STATE_SENDING_CMD:
20868892b705SDouglas Anderson 			if (!dw_mci_clear_pending_cmd_complete(host))
2087f95f3850SWill Newton 				break;
2088f95f3850SWill Newton 
2089f95f3850SWill Newton 			cmd = host->cmd;
2090f95f3850SWill Newton 			host->cmd = NULL;
2091f95f3850SWill Newton 			set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
2092e352c813SSeungwon Jeon 			err = dw_mci_command_complete(host, cmd);
2093e352c813SSeungwon Jeon 			if (cmd == mrq->sbc && !err) {
209442f989c0SJaehoon Chung 				__dw_mci_start_request(host, host->slot,
2095e352c813SSeungwon Jeon 						       mrq->cmd);
2096053b3ce6SSeungwon Jeon 				goto unlock;
2097053b3ce6SSeungwon Jeon 			}
2098053b3ce6SSeungwon Jeon 
2099e352c813SSeungwon Jeon 			if (cmd->data && err) {
210046d17952SDoug Anderson 				/*
210146d17952SDoug Anderson 				 * During UHS tuning sequence, sending the stop
210246d17952SDoug Anderson 				 * command after the response CRC error would
210346d17952SDoug Anderson 				 * throw the system into a confused state
210446d17952SDoug Anderson 				 * causing all future tuning phases to report
210546d17952SDoug Anderson 				 * failure.
210646d17952SDoug Anderson 				 *
210746d17952SDoug Anderson 				 * In such case controller will move into a data
210846d17952SDoug Anderson 				 * transfer state after a response error or
210946d17952SDoug Anderson 				 * response CRC error. Let's let that finish
211046d17952SDoug Anderson 				 * before trying to send a stop, so we'll go to
211146d17952SDoug Anderson 				 * STATE_SENDING_DATA.
211246d17952SDoug Anderson 				 *
211346d17952SDoug Anderson 				 * Although letting the data transfer take place
211446d17952SDoug Anderson 				 * will waste a bit of time (we already know
211546d17952SDoug Anderson 				 * the command was bad), it can't cause any
211646d17952SDoug Anderson 				 * errors since it's possible it would have
211746d17952SDoug Anderson 				 * taken place anyway if this tasklet got
211846d17952SDoug Anderson 				 * delayed. Allowing the transfer to take place
211946d17952SDoug Anderson 				 * avoids races and keeps things simple.
212046d17952SDoug Anderson 				 */
212143592c87SChristian Löhle 				if (err != -ETIMEDOUT &&
212243592c87SChristian Löhle 				    host->dir_status == DW_MCI_RECV_STATUS) {
212346d17952SDoug Anderson 					state = STATE_SENDING_DATA;
212446d17952SDoug Anderson 					continue;
212546d17952SDoug Anderson 				}
212646d17952SDoug Anderson 
212790c2143aSSeungwon Jeon 				send_stop_abort(host, data);
212825f8203bSVincent Whitchurch 				dw_mci_stop_dma(host);
212971abb133SSeungwon Jeon 				state = STATE_SENDING_STOP;
213071abb133SSeungwon Jeon 				break;
213171abb133SSeungwon Jeon 			}
213271abb133SSeungwon Jeon 
2133e352c813SSeungwon Jeon 			if (!cmd->data || err) {
2134e352c813SSeungwon Jeon 				dw_mci_request_end(host, mrq);
2135f95f3850SWill Newton 				goto unlock;
2136f95f3850SWill Newton 			}
2137f95f3850SWill Newton 
2138f95f3850SWill Newton 			prev_state = state = STATE_SENDING_DATA;
2139df561f66SGustavo A. R. Silva 			fallthrough;
2140f95f3850SWill Newton 
2141f95f3850SWill Newton 		case STATE_SENDING_DATA:
21422aa35465SDoug Anderson 			/*
21432aa35465SDoug Anderson 			 * We could get a data error and never a transfer
21442aa35465SDoug Anderson 			 * complete so we'd better check for it here.
21452aa35465SDoug Anderson 			 *
21462aa35465SDoug Anderson 			 * Note that we don't really care if we also got a
21472aa35465SDoug Anderson 			 * transfer complete; stopping the DMA and sending an
21482aa35465SDoug Anderson 			 * abort won't hurt.
21492aa35465SDoug Anderson 			 */
2150f95f3850SWill Newton 			if (test_and_clear_bit(EVENT_DATA_ERROR,
2151f95f3850SWill Newton 					       &host->pending_events)) {
2152e13c3c08SJaehoon Chung 				if (!(host->data_status & (SDMMC_INT_DRTO |
2153bdb9a90bSaddy ke 							   SDMMC_INT_EBE)))
215490c2143aSSeungwon Jeon 					send_stop_abort(host, data);
215525f8203bSVincent Whitchurch 				dw_mci_stop_dma(host);
2156f95f3850SWill Newton 				state = STATE_DATA_ERROR;
2157f95f3850SWill Newton 				break;
2158f95f3850SWill Newton 			}
2159f95f3850SWill Newton 
2160f95f3850SWill Newton 			if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
216157e10486SAddy Ke 						&host->pending_events)) {
216257e10486SAddy Ke 				/*
216357e10486SAddy Ke 				 * If all data-related interrupts don't come
216457e10486SAddy Ke 				 * within the given time in reading data state.
216557e10486SAddy Ke 				 */
216616a34574SJaehoon Chung 				if (host->dir_status == DW_MCI_RECV_STATUS)
216757e10486SAddy Ke 					dw_mci_set_drto(host);
2168f95f3850SWill Newton 				break;
216957e10486SAddy Ke 			}
2170f95f3850SWill Newton 
2171f95f3850SWill Newton 			set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
21722aa35465SDoug Anderson 
21732aa35465SDoug Anderson 			/*
21742aa35465SDoug Anderson 			 * Handle an EVENT_DATA_ERROR that might have shown up
21752aa35465SDoug Anderson 			 * before the transfer completed.  This might not have
21762aa35465SDoug Anderson 			 * been caught by the check above because the interrupt
21772aa35465SDoug Anderson 			 * could have gone off between the previous check and
21782aa35465SDoug Anderson 			 * the check for transfer complete.
21792aa35465SDoug Anderson 			 *
21802aa35465SDoug Anderson 			 * Technically this ought not be needed assuming we
21812aa35465SDoug Anderson 			 * get a DATA_COMPLETE eventually (we'll notice the
21822aa35465SDoug Anderson 			 * error and end the request), but it shouldn't hurt.
21832aa35465SDoug Anderson 			 *
21842aa35465SDoug Anderson 			 * This has the advantage of sending the stop command.
21852aa35465SDoug Anderson 			 */
21862aa35465SDoug Anderson 			if (test_and_clear_bit(EVENT_DATA_ERROR,
21872aa35465SDoug Anderson 					       &host->pending_events)) {
2188e13c3c08SJaehoon Chung 				if (!(host->data_status & (SDMMC_INT_DRTO |
2189bdb9a90bSaddy ke 							   SDMMC_INT_EBE)))
21902aa35465SDoug Anderson 					send_stop_abort(host, data);
219125f8203bSVincent Whitchurch 				dw_mci_stop_dma(host);
21922aa35465SDoug Anderson 				state = STATE_DATA_ERROR;
21932aa35465SDoug Anderson 				break;
21942aa35465SDoug Anderson 			}
2195f95f3850SWill Newton 			prev_state = state = STATE_DATA_BUSY;
21962aa35465SDoug Anderson 
2197df561f66SGustavo A. R. Silva 			fallthrough;
2198f95f3850SWill Newton 
2199f95f3850SWill Newton 		case STATE_DATA_BUSY:
220093c23ae3SDouglas Anderson 			if (!dw_mci_clear_pending_data_complete(host)) {
220157e10486SAddy Ke 				/*
220257e10486SAddy Ke 				 * If data error interrupt comes but data over
220357e10486SAddy Ke 				 * interrupt doesn't come within the given time.
220457e10486SAddy Ke 				 * in reading data state.
220557e10486SAddy Ke 				 */
220616a34574SJaehoon Chung 				if (host->dir_status == DW_MCI_RECV_STATUS)
220757e10486SAddy Ke 					dw_mci_set_drto(host);
2208f95f3850SWill Newton 				break;
220957e10486SAddy Ke 			}
2210f95f3850SWill Newton 
22112b8ac062SVincent Whitchurch 			dw_mci_stop_fault_timer(host);
2212f95f3850SWill Newton 			host->data = NULL;
2213f95f3850SWill Newton 			set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
2214e352c813SSeungwon Jeon 			err = dw_mci_data_complete(host, data);
2215f95f3850SWill Newton 
2216e352c813SSeungwon Jeon 			if (!err) {
2217e352c813SSeungwon Jeon 				if (!data->stop || mrq->sbc) {
221817c8bc85SSachin Kamat 					if (mrq->sbc && data->stop)
2219053b3ce6SSeungwon Jeon 						data->stop->error = 0;
2220e352c813SSeungwon Jeon 					dw_mci_request_end(host, mrq);
2221053b3ce6SSeungwon Jeon 					goto unlock;
2222053b3ce6SSeungwon Jeon 				}
2223053b3ce6SSeungwon Jeon 
222490c2143aSSeungwon Jeon 				/* stop command for open-ended transfer*/
2225e352c813SSeungwon Jeon 				if (data->stop)
222690c2143aSSeungwon Jeon 					send_stop_abort(host, data);
22272aa35465SDoug Anderson 			} else {
22282aa35465SDoug Anderson 				/*
22292aa35465SDoug Anderson 				 * If we don't have a command complete now we'll
22302aa35465SDoug Anderson 				 * never get one since we just reset everything;
22312aa35465SDoug Anderson 				 * better end the request.
22322aa35465SDoug Anderson 				 *
22332aa35465SDoug Anderson 				 * If we do have a command complete we'll fall
22342aa35465SDoug Anderson 				 * through to the SENDING_STOP command and
22352aa35465SDoug Anderson 				 * everything will be peachy keen.
22362aa35465SDoug Anderson 				 */
22372aa35465SDoug Anderson 				if (!test_bit(EVENT_CMD_COMPLETE,
22382aa35465SDoug Anderson 					      &host->pending_events)) {
22392aa35465SDoug Anderson 					host->cmd = NULL;
22402aa35465SDoug Anderson 					dw_mci_request_end(host, mrq);
22412aa35465SDoug Anderson 					goto unlock;
22422aa35465SDoug Anderson 				}
224390c2143aSSeungwon Jeon 			}
2244e352c813SSeungwon Jeon 
2245e352c813SSeungwon Jeon 			/*
2246e352c813SSeungwon Jeon 			 * If err has non-zero,
2247e352c813SSeungwon Jeon 			 * stop-abort command has been already issued.
2248e352c813SSeungwon Jeon 			 */
2249e352c813SSeungwon Jeon 			prev_state = state = STATE_SENDING_STOP;
2250e352c813SSeungwon Jeon 
2251df561f66SGustavo A. R. Silva 			fallthrough;
2252f95f3850SWill Newton 
2253f95f3850SWill Newton 		case STATE_SENDING_STOP:
22548892b705SDouglas Anderson 			if (!dw_mci_clear_pending_cmd_complete(host))
2255f95f3850SWill Newton 				break;
2256f95f3850SWill Newton 
225771abb133SSeungwon Jeon 			/* CMD error in data command */
225831bff450SSeungwon Jeon 			if (mrq->cmd->error && mrq->data)
22593a33a94cSSonny Rao 				dw_mci_reset(host);
226071abb133SSeungwon Jeon 
22612b8ac062SVincent Whitchurch 			dw_mci_stop_fault_timer(host);
2262f95f3850SWill Newton 			host->cmd = NULL;
226371abb133SSeungwon Jeon 			host->data = NULL;
226490c2143aSSeungwon Jeon 
2265e13c3c08SJaehoon Chung 			if (!mrq->sbc && mrq->stop)
2266e352c813SSeungwon Jeon 				dw_mci_command_complete(host, mrq->stop);
226790c2143aSSeungwon Jeon 			else
226890c2143aSSeungwon Jeon 				host->cmd_status = 0;
226990c2143aSSeungwon Jeon 
2270e352c813SSeungwon Jeon 			dw_mci_request_end(host, mrq);
2271f95f3850SWill Newton 			goto unlock;
2272f95f3850SWill Newton 
2273f95f3850SWill Newton 		case STATE_DATA_ERROR:
2274f95f3850SWill Newton 			if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2275f95f3850SWill Newton 						&host->pending_events))
2276f95f3850SWill Newton 				break;
2277f95f3850SWill Newton 
2278f95f3850SWill Newton 			state = STATE_DATA_BUSY;
2279f95f3850SWill Newton 			break;
2280f95f3850SWill Newton 		}
2281f95f3850SWill Newton 	} while (state != prev_state);
2282f95f3850SWill Newton 
2283f95f3850SWill Newton 	host->state = state;
2284f95f3850SWill Newton unlock:
2285f95f3850SWill Newton 	spin_unlock(&host->lock);
2286f95f3850SWill Newton 
2287f95f3850SWill Newton }
2288f95f3850SWill Newton 
228934b664a2SJames Hogan /* push final bytes to part_buf, only use during push */
dw_mci_set_part_bytes(struct dw_mci * host,void * buf,int cnt)229034b664a2SJames Hogan static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
229134b664a2SJames Hogan {
229234b664a2SJames Hogan 	memcpy((void *)&host->part_buf, buf, cnt);
229334b664a2SJames Hogan 	host->part_buf_count = cnt;
229434b664a2SJames Hogan }
229534b664a2SJames Hogan 
229634b664a2SJames Hogan /* append bytes to part_buf, only use during push */
dw_mci_push_part_bytes(struct dw_mci * host,void * buf,int cnt)229734b664a2SJames Hogan static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
229834b664a2SJames Hogan {
229934b664a2SJames Hogan 	cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
230034b664a2SJames Hogan 	memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
230134b664a2SJames Hogan 	host->part_buf_count += cnt;
230234b664a2SJames Hogan 	return cnt;
230334b664a2SJames Hogan }
230434b664a2SJames Hogan 
230534b664a2SJames Hogan /* pull first bytes from part_buf, only use during pull */
dw_mci_pull_part_bytes(struct dw_mci * host,void * buf,int cnt)230634b664a2SJames Hogan static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
230734b664a2SJames Hogan {
23080e3a22c0SShawn Lin 	cnt = min_t(int, cnt, host->part_buf_count);
230934b664a2SJames Hogan 	if (cnt) {
231034b664a2SJames Hogan 		memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
231134b664a2SJames Hogan 		       cnt);
231234b664a2SJames Hogan 		host->part_buf_count -= cnt;
231334b664a2SJames Hogan 		host->part_buf_start += cnt;
231434b664a2SJames Hogan 	}
231534b664a2SJames Hogan 	return cnt;
231634b664a2SJames Hogan }
231734b664a2SJames Hogan 
231834b664a2SJames 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)231934b664a2SJames Hogan static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
232034b664a2SJames Hogan {
232134b664a2SJames Hogan 	memcpy(buf, &host->part_buf, cnt);
232234b664a2SJames Hogan 	host->part_buf_start = cnt;
232334b664a2SJames Hogan 	host->part_buf_count = (1 << host->data_shift) - cnt;
232434b664a2SJames Hogan }
232534b664a2SJames Hogan 
dw_mci_push_data16(struct dw_mci * host,void * buf,int cnt)2326f95f3850SWill Newton static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2327f95f3850SWill Newton {
2328cfbeb59cSMarkos Chandras 	struct mmc_data *data = host->data;
2329cfbeb59cSMarkos Chandras 	int init_cnt = cnt;
2330cfbeb59cSMarkos Chandras 
233134b664a2SJames Hogan 	/* try and push anything in the part_buf */
233234b664a2SJames Hogan 	if (unlikely(host->part_buf_count)) {
233334b664a2SJames Hogan 		int len = dw_mci_push_part_bytes(host, buf, cnt);
23340e3a22c0SShawn Lin 
233534b664a2SJames Hogan 		buf += len;
233634b664a2SJames Hogan 		cnt -= len;
2337cfbeb59cSMarkos Chandras 		if (host->part_buf_count == 2) {
233876184ac1SBen Dooks 			mci_fifo_writew(host->fifo_reg, host->part_buf16);
233934b664a2SJames Hogan 			host->part_buf_count = 0;
234034b664a2SJames Hogan 		}
234134b664a2SJames Hogan 	}
234234b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
234334b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x1)) {
234434b664a2SJames Hogan 		while (cnt >= 2) {
234534b664a2SJames Hogan 			u16 aligned_buf[64];
234634b664a2SJames Hogan 			int len = min(cnt & -2, (int)sizeof(aligned_buf));
234734b664a2SJames Hogan 			int items = len >> 1;
234834b664a2SJames Hogan 			int i;
234934b664a2SJames Hogan 			/* memcpy from input buffer into aligned buffer */
235034b664a2SJames Hogan 			memcpy(aligned_buf, buf, len);
235134b664a2SJames Hogan 			buf += len;
235234b664a2SJames Hogan 			cnt -= len;
235334b664a2SJames Hogan 			/* push data from aligned buffer into fifo */
235434b664a2SJames Hogan 			for (i = 0; i < items; ++i)
235576184ac1SBen Dooks 				mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
235634b664a2SJames Hogan 		}
235734b664a2SJames Hogan 	} else
235834b664a2SJames Hogan #endif
235934b664a2SJames Hogan 	{
236034b664a2SJames Hogan 		u16 *pdata = buf;
23610e3a22c0SShawn Lin 
236234b664a2SJames Hogan 		for (; cnt >= 2; cnt -= 2)
236376184ac1SBen Dooks 			mci_fifo_writew(host->fifo_reg, *pdata++);
236434b664a2SJames Hogan 		buf = pdata;
236534b664a2SJames Hogan 	}
236634b664a2SJames Hogan 	/* put anything remaining in the part_buf */
236734b664a2SJames Hogan 	if (cnt) {
236834b664a2SJames Hogan 		dw_mci_set_part_bytes(host, buf, cnt);
2369cfbeb59cSMarkos Chandras 		 /* Push data if we have reached the expected data length */
2370cfbeb59cSMarkos Chandras 		if ((data->bytes_xfered + init_cnt) ==
2371cfbeb59cSMarkos Chandras 		    (data->blksz * data->blocks))
237276184ac1SBen Dooks 			mci_fifo_writew(host->fifo_reg, host->part_buf16);
2373f95f3850SWill Newton 	}
2374f95f3850SWill Newton }
2375f95f3850SWill Newton 
dw_mci_pull_data16(struct dw_mci * host,void * buf,int cnt)2376f95f3850SWill Newton static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2377f95f3850SWill Newton {
237834b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
237934b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x1)) {
238034b664a2SJames Hogan 		while (cnt >= 2) {
238134b664a2SJames Hogan 			/* pull data from fifo into aligned buffer */
238234b664a2SJames Hogan 			u16 aligned_buf[64];
238334b664a2SJames Hogan 			int len = min(cnt & -2, (int)sizeof(aligned_buf));
238434b664a2SJames Hogan 			int items = len >> 1;
238534b664a2SJames Hogan 			int i;
23860e3a22c0SShawn Lin 
238734b664a2SJames Hogan 			for (i = 0; i < items; ++i)
238876184ac1SBen Dooks 				aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
238934b664a2SJames Hogan 			/* memcpy from aligned buffer into output buffer */
239034b664a2SJames Hogan 			memcpy(buf, aligned_buf, len);
239134b664a2SJames Hogan 			buf += len;
239234b664a2SJames Hogan 			cnt -= len;
239334b664a2SJames Hogan 		}
239434b664a2SJames Hogan 	} else
239534b664a2SJames Hogan #endif
239634b664a2SJames Hogan 	{
239734b664a2SJames Hogan 		u16 *pdata = buf;
23980e3a22c0SShawn Lin 
239934b664a2SJames Hogan 		for (; cnt >= 2; cnt -= 2)
240076184ac1SBen Dooks 			*pdata++ = mci_fifo_readw(host->fifo_reg);
240134b664a2SJames Hogan 		buf = pdata;
240234b664a2SJames Hogan 	}
240334b664a2SJames Hogan 	if (cnt) {
240476184ac1SBen Dooks 		host->part_buf16 = mci_fifo_readw(host->fifo_reg);
240534b664a2SJames Hogan 		dw_mci_pull_final_bytes(host, buf, cnt);
2406f95f3850SWill Newton 	}
2407f95f3850SWill Newton }
2408f95f3850SWill Newton 
dw_mci_push_data32(struct dw_mci * host,void * buf,int cnt)2409f95f3850SWill Newton static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2410f95f3850SWill Newton {
2411cfbeb59cSMarkos Chandras 	struct mmc_data *data = host->data;
2412cfbeb59cSMarkos Chandras 	int init_cnt = cnt;
2413cfbeb59cSMarkos Chandras 
241434b664a2SJames Hogan 	/* try and push anything in the part_buf */
241534b664a2SJames Hogan 	if (unlikely(host->part_buf_count)) {
241634b664a2SJames Hogan 		int len = dw_mci_push_part_bytes(host, buf, cnt);
24170e3a22c0SShawn Lin 
241834b664a2SJames Hogan 		buf += len;
241934b664a2SJames Hogan 		cnt -= len;
2420cfbeb59cSMarkos Chandras 		if (host->part_buf_count == 4) {
242176184ac1SBen Dooks 			mci_fifo_writel(host->fifo_reg,	host->part_buf32);
242234b664a2SJames Hogan 			host->part_buf_count = 0;
242334b664a2SJames Hogan 		}
242434b664a2SJames Hogan 	}
242534b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
242634b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x3)) {
242734b664a2SJames Hogan 		while (cnt >= 4) {
242834b664a2SJames Hogan 			u32 aligned_buf[32];
242934b664a2SJames Hogan 			int len = min(cnt & -4, (int)sizeof(aligned_buf));
243034b664a2SJames Hogan 			int items = len >> 2;
243134b664a2SJames Hogan 			int i;
243234b664a2SJames Hogan 			/* memcpy from input buffer into aligned buffer */
243334b664a2SJames Hogan 			memcpy(aligned_buf, buf, len);
243434b664a2SJames Hogan 			buf += len;
243534b664a2SJames Hogan 			cnt -= len;
243634b664a2SJames Hogan 			/* push data from aligned buffer into fifo */
243734b664a2SJames Hogan 			for (i = 0; i < items; ++i)
243876184ac1SBen Dooks 				mci_fifo_writel(host->fifo_reg,	aligned_buf[i]);
243934b664a2SJames Hogan 		}
244034b664a2SJames Hogan 	} else
244134b664a2SJames Hogan #endif
244234b664a2SJames Hogan 	{
244334b664a2SJames Hogan 		u32 *pdata = buf;
24440e3a22c0SShawn Lin 
244534b664a2SJames Hogan 		for (; cnt >= 4; cnt -= 4)
244676184ac1SBen Dooks 			mci_fifo_writel(host->fifo_reg, *pdata++);
244734b664a2SJames Hogan 		buf = pdata;
244834b664a2SJames Hogan 	}
244934b664a2SJames Hogan 	/* put anything remaining in the part_buf */
245034b664a2SJames Hogan 	if (cnt) {
245134b664a2SJames Hogan 		dw_mci_set_part_bytes(host, buf, cnt);
2452cfbeb59cSMarkos Chandras 		 /* Push data if we have reached the expected data length */
2453cfbeb59cSMarkos Chandras 		if ((data->bytes_xfered + init_cnt) ==
2454cfbeb59cSMarkos Chandras 		    (data->blksz * data->blocks))
245576184ac1SBen Dooks 			mci_fifo_writel(host->fifo_reg, host->part_buf32);
2456f95f3850SWill Newton 	}
2457f95f3850SWill Newton }
2458f95f3850SWill Newton 
dw_mci_pull_data32(struct dw_mci * host,void * buf,int cnt)2459f95f3850SWill Newton static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2460f95f3850SWill Newton {
246134b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
246234b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x3)) {
246334b664a2SJames Hogan 		while (cnt >= 4) {
246434b664a2SJames Hogan 			/* pull data from fifo into aligned buffer */
246534b664a2SJames Hogan 			u32 aligned_buf[32];
246634b664a2SJames Hogan 			int len = min(cnt & -4, (int)sizeof(aligned_buf));
246734b664a2SJames Hogan 			int items = len >> 2;
246834b664a2SJames Hogan 			int i;
24690e3a22c0SShawn Lin 
247034b664a2SJames Hogan 			for (i = 0; i < items; ++i)
247176184ac1SBen Dooks 				aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
247234b664a2SJames Hogan 			/* memcpy from aligned buffer into output buffer */
247334b664a2SJames Hogan 			memcpy(buf, aligned_buf, len);
247434b664a2SJames Hogan 			buf += len;
247534b664a2SJames Hogan 			cnt -= len;
247634b664a2SJames Hogan 		}
247734b664a2SJames Hogan 	} else
247834b664a2SJames Hogan #endif
247934b664a2SJames Hogan 	{
248034b664a2SJames Hogan 		u32 *pdata = buf;
24810e3a22c0SShawn Lin 
248234b664a2SJames Hogan 		for (; cnt >= 4; cnt -= 4)
248376184ac1SBen Dooks 			*pdata++ = mci_fifo_readl(host->fifo_reg);
248434b664a2SJames Hogan 		buf = pdata;
248534b664a2SJames Hogan 	}
248634b664a2SJames Hogan 	if (cnt) {
248776184ac1SBen Dooks 		host->part_buf32 = mci_fifo_readl(host->fifo_reg);
248834b664a2SJames Hogan 		dw_mci_pull_final_bytes(host, buf, cnt);
2489f95f3850SWill Newton 	}
2490f95f3850SWill Newton }
2491f95f3850SWill Newton 
dw_mci_push_data64(struct dw_mci * host,void * buf,int cnt)2492f95f3850SWill Newton static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2493f95f3850SWill Newton {
2494cfbeb59cSMarkos Chandras 	struct mmc_data *data = host->data;
2495cfbeb59cSMarkos Chandras 	int init_cnt = cnt;
2496cfbeb59cSMarkos Chandras 
249734b664a2SJames Hogan 	/* try and push anything in the part_buf */
249834b664a2SJames Hogan 	if (unlikely(host->part_buf_count)) {
249934b664a2SJames Hogan 		int len = dw_mci_push_part_bytes(host, buf, cnt);
25000e3a22c0SShawn Lin 
250134b664a2SJames Hogan 		buf += len;
250234b664a2SJames Hogan 		cnt -= len;
2503c09fbd74SSeungwon Jeon 
2504cfbeb59cSMarkos Chandras 		if (host->part_buf_count == 8) {
250576184ac1SBen Dooks 			mci_fifo_writeq(host->fifo_reg,	host->part_buf);
250634b664a2SJames Hogan 			host->part_buf_count = 0;
250734b664a2SJames Hogan 		}
250834b664a2SJames Hogan 	}
250934b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
251034b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x7)) {
251134b664a2SJames Hogan 		while (cnt >= 8) {
251234b664a2SJames Hogan 			u64 aligned_buf[16];
251334b664a2SJames Hogan 			int len = min(cnt & -8, (int)sizeof(aligned_buf));
251434b664a2SJames Hogan 			int items = len >> 3;
251534b664a2SJames Hogan 			int i;
251634b664a2SJames Hogan 			/* memcpy from input buffer into aligned buffer */
251734b664a2SJames Hogan 			memcpy(aligned_buf, buf, len);
251834b664a2SJames Hogan 			buf += len;
251934b664a2SJames Hogan 			cnt -= len;
252034b664a2SJames Hogan 			/* push data from aligned buffer into fifo */
252134b664a2SJames Hogan 			for (i = 0; i < items; ++i)
252276184ac1SBen Dooks 				mci_fifo_writeq(host->fifo_reg,	aligned_buf[i]);
252334b664a2SJames Hogan 		}
252434b664a2SJames Hogan 	} else
252534b664a2SJames Hogan #endif
252634b664a2SJames Hogan 	{
252734b664a2SJames Hogan 		u64 *pdata = buf;
25280e3a22c0SShawn Lin 
252934b664a2SJames Hogan 		for (; cnt >= 8; cnt -= 8)
253076184ac1SBen Dooks 			mci_fifo_writeq(host->fifo_reg, *pdata++);
253134b664a2SJames Hogan 		buf = pdata;
253234b664a2SJames Hogan 	}
253334b664a2SJames Hogan 	/* put anything remaining in the part_buf */
253434b664a2SJames Hogan 	if (cnt) {
253534b664a2SJames Hogan 		dw_mci_set_part_bytes(host, buf, cnt);
2536cfbeb59cSMarkos Chandras 		/* Push data if we have reached the expected data length */
2537cfbeb59cSMarkos Chandras 		if ((data->bytes_xfered + init_cnt) ==
2538cfbeb59cSMarkos Chandras 		    (data->blksz * data->blocks))
253976184ac1SBen Dooks 			mci_fifo_writeq(host->fifo_reg, host->part_buf);
2540f95f3850SWill Newton 	}
2541f95f3850SWill Newton }
2542f95f3850SWill Newton 
dw_mci_pull_data64(struct dw_mci * host,void * buf,int cnt)2543f95f3850SWill Newton static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2544f95f3850SWill Newton {
254534b664a2SJames Hogan #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
254634b664a2SJames Hogan 	if (unlikely((unsigned long)buf & 0x7)) {
254734b664a2SJames Hogan 		while (cnt >= 8) {
254834b664a2SJames Hogan 			/* pull data from fifo into aligned buffer */
254934b664a2SJames Hogan 			u64 aligned_buf[16];
255034b664a2SJames Hogan 			int len = min(cnt & -8, (int)sizeof(aligned_buf));
255134b664a2SJames Hogan 			int items = len >> 3;
255234b664a2SJames Hogan 			int i;
25530e3a22c0SShawn Lin 
255434b664a2SJames Hogan 			for (i = 0; i < items; ++i)
255576184ac1SBen Dooks 				aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
255676184ac1SBen Dooks 
255734b664a2SJames Hogan 			/* memcpy from aligned buffer into output buffer */
255834b664a2SJames Hogan 			memcpy(buf, aligned_buf, len);
255934b664a2SJames Hogan 			buf += len;
256034b664a2SJames Hogan 			cnt -= len;
2561f95f3850SWill Newton 		}
256234b664a2SJames Hogan 	} else
256334b664a2SJames Hogan #endif
256434b664a2SJames Hogan 	{
256534b664a2SJames Hogan 		u64 *pdata = buf;
25660e3a22c0SShawn Lin 
256734b664a2SJames Hogan 		for (; cnt >= 8; cnt -= 8)
256876184ac1SBen Dooks 			*pdata++ = mci_fifo_readq(host->fifo_reg);
256934b664a2SJames Hogan 		buf = pdata;
257034b664a2SJames Hogan 	}
257134b664a2SJames Hogan 	if (cnt) {
257276184ac1SBen Dooks 		host->part_buf = mci_fifo_readq(host->fifo_reg);
257334b664a2SJames Hogan 		dw_mci_pull_final_bytes(host, buf, cnt);
257434b664a2SJames Hogan 	}
257534b664a2SJames Hogan }
257634b664a2SJames Hogan 
dw_mci_pull_data(struct dw_mci * host,void * buf,int cnt)257734b664a2SJames Hogan static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
257834b664a2SJames Hogan {
257934b664a2SJames Hogan 	int len;
258034b664a2SJames Hogan 
258134b664a2SJames Hogan 	/* get remaining partial bytes */
258234b664a2SJames Hogan 	len = dw_mci_pull_part_bytes(host, buf, cnt);
258334b664a2SJames Hogan 	if (unlikely(len == cnt))
258434b664a2SJames Hogan 		return;
258534b664a2SJames Hogan 	buf += len;
258634b664a2SJames Hogan 	cnt -= len;
258734b664a2SJames Hogan 
258834b664a2SJames Hogan 	/* get the rest of the data */
258934b664a2SJames Hogan 	host->pull_data(host, buf, cnt);
2590f95f3850SWill Newton }
2591f95f3850SWill Newton 
dw_mci_read_data_pio(struct dw_mci * host,bool dto)259287a74d39SKyoungil Kim static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
2593f95f3850SWill Newton {
2594f9c2a0dcSSeungwon Jeon 	struct sg_mapping_iter *sg_miter = &host->sg_miter;
2595f9c2a0dcSSeungwon Jeon 	void *buf;
2596f9c2a0dcSSeungwon Jeon 	unsigned int offset;
2597f95f3850SWill Newton 	struct mmc_data	*data = host->data;
2598f95f3850SWill Newton 	int shift = host->data_shift;
2599f95f3850SWill Newton 	u32 status;
26003e4b0d8bSMarkos Chandras 	unsigned int len;
2601f9c2a0dcSSeungwon Jeon 	unsigned int remain, fcnt;
2602f95f3850SWill Newton 
2603f95f3850SWill Newton 	do {
2604f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2605f9c2a0dcSSeungwon Jeon 			goto done;
2606f95f3850SWill Newton 
26074225fc85SImre Deak 		host->sg = sg_miter->piter.sg;
2608f9c2a0dcSSeungwon Jeon 		buf = sg_miter->addr;
2609f9c2a0dcSSeungwon Jeon 		remain = sg_miter->length;
2610f9c2a0dcSSeungwon Jeon 		offset = 0;
2611f9c2a0dcSSeungwon Jeon 
2612f9c2a0dcSSeungwon Jeon 		do {
2613f9c2a0dcSSeungwon Jeon 			fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2614f9c2a0dcSSeungwon Jeon 					<< shift) + host->part_buf_count;
2615f9c2a0dcSSeungwon Jeon 			len = min(remain, fcnt);
2616f9c2a0dcSSeungwon Jeon 			if (!len)
2617f9c2a0dcSSeungwon Jeon 				break;
2618f9c2a0dcSSeungwon Jeon 			dw_mci_pull_data(host, (void *)(buf + offset), len);
26193e4b0d8bSMarkos Chandras 			data->bytes_xfered += len;
2620f95f3850SWill Newton 			offset += len;
2621f9c2a0dcSSeungwon Jeon 			remain -= len;
2622f9c2a0dcSSeungwon Jeon 		} while (remain);
2623f95f3850SWill Newton 
2624e74f3a9cSSeungwon Jeon 		sg_miter->consumed = offset;
2625f95f3850SWill Newton 		status = mci_readl(host, MINTSTS);
2626f95f3850SWill Newton 		mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
262787a74d39SKyoungil Kim 	/* if the RXDR is ready read again */
262887a74d39SKyoungil Kim 	} while ((status & SDMMC_INT_RXDR) ||
262987a74d39SKyoungil Kim 		 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
2630f9c2a0dcSSeungwon Jeon 
2631f9c2a0dcSSeungwon Jeon 	if (!remain) {
2632f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2633f9c2a0dcSSeungwon Jeon 			goto done;
2634f9c2a0dcSSeungwon Jeon 		sg_miter->consumed = 0;
2635f9c2a0dcSSeungwon Jeon 	}
2636f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2637f95f3850SWill Newton 	return;
2638f95f3850SWill Newton 
2639f95f3850SWill Newton done:
2640f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2641f9c2a0dcSSeungwon Jeon 	host->sg = NULL;
26420e3a22c0SShawn Lin 	smp_wmb(); /* drain writebuffer */
2643f95f3850SWill Newton 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2644f95f3850SWill Newton }
2645f95f3850SWill Newton 
dw_mci_write_data_pio(struct dw_mci * host)2646f95f3850SWill Newton static void dw_mci_write_data_pio(struct dw_mci *host)
2647f95f3850SWill Newton {
2648f9c2a0dcSSeungwon Jeon 	struct sg_mapping_iter *sg_miter = &host->sg_miter;
2649f9c2a0dcSSeungwon Jeon 	void *buf;
2650f9c2a0dcSSeungwon Jeon 	unsigned int offset;
2651f95f3850SWill Newton 	struct mmc_data	*data = host->data;
2652f95f3850SWill Newton 	int shift = host->data_shift;
2653f95f3850SWill Newton 	u32 status;
26543e4b0d8bSMarkos Chandras 	unsigned int len;
2655f9c2a0dcSSeungwon Jeon 	unsigned int fifo_depth = host->fifo_depth;
2656f9c2a0dcSSeungwon Jeon 	unsigned int remain, fcnt;
2657f95f3850SWill Newton 
2658f95f3850SWill Newton 	do {
2659f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2660f9c2a0dcSSeungwon Jeon 			goto done;
2661f95f3850SWill Newton 
26624225fc85SImre Deak 		host->sg = sg_miter->piter.sg;
2663f9c2a0dcSSeungwon Jeon 		buf = sg_miter->addr;
2664f9c2a0dcSSeungwon Jeon 		remain = sg_miter->length;
2665f9c2a0dcSSeungwon Jeon 		offset = 0;
2666f9c2a0dcSSeungwon Jeon 
2667f9c2a0dcSSeungwon Jeon 		do {
2668f9c2a0dcSSeungwon Jeon 			fcnt = ((fifo_depth -
2669f9c2a0dcSSeungwon Jeon 				 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2670f9c2a0dcSSeungwon Jeon 					<< shift) - host->part_buf_count;
2671f9c2a0dcSSeungwon Jeon 			len = min(remain, fcnt);
2672f9c2a0dcSSeungwon Jeon 			if (!len)
2673f9c2a0dcSSeungwon Jeon 				break;
2674f9c2a0dcSSeungwon Jeon 			host->push_data(host, (void *)(buf + offset), len);
26753e4b0d8bSMarkos Chandras 			data->bytes_xfered += len;
2676f95f3850SWill Newton 			offset += len;
2677f9c2a0dcSSeungwon Jeon 			remain -= len;
2678f9c2a0dcSSeungwon Jeon 		} while (remain);
2679f95f3850SWill Newton 
2680e74f3a9cSSeungwon Jeon 		sg_miter->consumed = offset;
2681f95f3850SWill Newton 		status = mci_readl(host, MINTSTS);
2682f95f3850SWill Newton 		mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2683f95f3850SWill Newton 	} while (status & SDMMC_INT_TXDR); /* if TXDR write again */
2684f9c2a0dcSSeungwon Jeon 
2685f9c2a0dcSSeungwon Jeon 	if (!remain) {
2686f9c2a0dcSSeungwon Jeon 		if (!sg_miter_next(sg_miter))
2687f9c2a0dcSSeungwon Jeon 			goto done;
2688f9c2a0dcSSeungwon Jeon 		sg_miter->consumed = 0;
2689f9c2a0dcSSeungwon Jeon 	}
2690f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2691f95f3850SWill Newton 	return;
2692f95f3850SWill Newton 
2693f95f3850SWill Newton done:
2694f9c2a0dcSSeungwon Jeon 	sg_miter_stop(sg_miter);
2695f9c2a0dcSSeungwon Jeon 	host->sg = NULL;
26960e3a22c0SShawn Lin 	smp_wmb(); /* drain writebuffer */
2697f95f3850SWill Newton 	set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2698f95f3850SWill Newton }
2699f95f3850SWill Newton 
dw_mci_cmd_interrupt(struct dw_mci * host,u32 status)2700f95f3850SWill Newton static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2701f95f3850SWill Newton {
27020363b12dSDouglas Anderson 	del_timer(&host->cto_timer);
27030363b12dSDouglas Anderson 
2704f95f3850SWill Newton 	if (!host->cmd_status)
2705f95f3850SWill Newton 		host->cmd_status = status;
2706f95f3850SWill Newton 
27070e3a22c0SShawn Lin 	smp_wmb(); /* drain writebuffer */
2708f95f3850SWill Newton 
2709f95f3850SWill Newton 	set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2710f95f3850SWill Newton 	tasklet_schedule(&host->tasklet);
27112b8ac062SVincent Whitchurch 
27122b8ac062SVincent Whitchurch 	dw_mci_start_fault_timer(host);
2713f95f3850SWill Newton }
2714f95f3850SWill Newton 
dw_mci_handle_cd(struct dw_mci * host)27156130e7a9SDoug Anderson static void dw_mci_handle_cd(struct dw_mci *host)
27166130e7a9SDoug Anderson {
2717b23475faSJaehoon Chung 	struct dw_mci_slot *slot = host->slot;
27186130e7a9SDoug Anderson 
27196130e7a9SDoug Anderson 	mmc_detect_change(slot->mmc,
27206130e7a9SDoug Anderson 		msecs_to_jiffies(host->pdata->detect_delay_ms));
27216130e7a9SDoug Anderson }
27226130e7a9SDoug Anderson 
dw_mci_interrupt(int irq,void * dev_id)2723f95f3850SWill Newton static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2724f95f3850SWill Newton {
2725f95f3850SWill Newton 	struct dw_mci *host = dev_id;
2726182c9081SSeungwon Jeon 	u32 pending;
2727b23475faSJaehoon Chung 	struct dw_mci_slot *slot = host->slot;
2728f95f3850SWill Newton 
2729f95f3850SWill Newton 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2730f95f3850SWill Newton 
2731476d79f1SDoug Anderson 	if (pending) {
273201730558SDoug Anderson 		/* Check volt switch first, since it can look like an error */
273301730558SDoug Anderson 		if ((host->state == STATE_SENDING_CMD11) &&
273401730558SDoug Anderson 		    (pending & SDMMC_INT_VOLT_SWITCH)) {
273501730558SDoug Anderson 			mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
273601730558SDoug Anderson 			pending &= ~SDMMC_INT_VOLT_SWITCH;
273749ba0302SDoug Anderson 
273849ba0302SDoug Anderson 			/*
273949ba0302SDoug Anderson 			 * Hold the lock; we know cmd11_timer can't be kicked
274049ba0302SDoug Anderson 			 * off after the lock is released, so safe to delete.
274149ba0302SDoug Anderson 			 */
27429f7d4c91STian Tao 			spin_lock(&host->irq_lock);
274301730558SDoug Anderson 			dw_mci_cmd_interrupt(host, pending);
27449f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
274549ba0302SDoug Anderson 
274649ba0302SDoug Anderson 			del_timer(&host->cmd11_timer);
274701730558SDoug Anderson 		}
274801730558SDoug Anderson 
2749f95f3850SWill Newton 		if (pending & DW_MCI_CMD_ERROR_FLAGS) {
27509f7d4c91STian Tao 			spin_lock(&host->irq_lock);
27518892b705SDouglas Anderson 
275203de1921SAddy Ke 			del_timer(&host->cto_timer);
2753f95f3850SWill Newton 			mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
2754182c9081SSeungwon Jeon 			host->cmd_status = pending;
27550e3a22c0SShawn Lin 			smp_wmb(); /* drain writebuffer */
2756f95f3850SWill Newton 			set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
27578892b705SDouglas Anderson 
27589f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
2759f95f3850SWill Newton 		}
2760f95f3850SWill Newton 
2761f95f3850SWill Newton 		if (pending & DW_MCI_DATA_ERROR_FLAGS) {
276226391e49SVincent Whitchurch 			spin_lock(&host->irq_lock);
276326391e49SVincent Whitchurch 
27641a6fe7bbSMårten Lindahl 			if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
27651a6fe7bbSMårten Lindahl 				del_timer(&host->dto_timer);
27661a6fe7bbSMårten Lindahl 
2767f95f3850SWill Newton 			/* if there is an error report DATA_ERROR */
2768f95f3850SWill Newton 			mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
2769182c9081SSeungwon Jeon 			host->data_status = pending;
27700e3a22c0SShawn Lin 			smp_wmb(); /* drain writebuffer */
2771f95f3850SWill Newton 			set_bit(EVENT_DATA_ERROR, &host->pending_events);
27721a6fe7bbSMårten Lindahl 
27731a6fe7bbSMårten Lindahl 			if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
27741a6fe7bbSMårten Lindahl 				/* In case of error, we cannot expect a DTO */
27751a6fe7bbSMårten Lindahl 				set_bit(EVENT_DATA_COMPLETE,
27761a6fe7bbSMårten Lindahl 					&host->pending_events);
27771a6fe7bbSMårten Lindahl 
2778f95f3850SWill Newton 			tasklet_schedule(&host->tasklet);
277926391e49SVincent Whitchurch 
278026391e49SVincent Whitchurch 			spin_unlock(&host->irq_lock);
2781f95f3850SWill Newton 		}
2782f95f3850SWill Newton 
2783f95f3850SWill Newton 		if (pending & SDMMC_INT_DATA_OVER) {
27849f7d4c91STian Tao 			spin_lock(&host->irq_lock);
278593c23ae3SDouglas Anderson 
278657e10486SAddy Ke 			del_timer(&host->dto_timer);
278757e10486SAddy Ke 
2788f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2789f95f3850SWill Newton 			if (!host->data_status)
2790182c9081SSeungwon Jeon 				host->data_status = pending;
27910e3a22c0SShawn Lin 			smp_wmb(); /* drain writebuffer */
2792f95f3850SWill Newton 			if (host->dir_status == DW_MCI_RECV_STATUS) {
2793f95f3850SWill Newton 				if (host->sg != NULL)
279487a74d39SKyoungil Kim 					dw_mci_read_data_pio(host, true);
2795f95f3850SWill Newton 			}
2796f95f3850SWill Newton 			set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2797f95f3850SWill Newton 			tasklet_schedule(&host->tasklet);
279893c23ae3SDouglas Anderson 
27999f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
2800f95f3850SWill Newton 		}
2801f95f3850SWill Newton 
2802f95f3850SWill Newton 		if (pending & SDMMC_INT_RXDR) {
2803f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
2804b40af3aaSJames Hogan 			if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
280587a74d39SKyoungil Kim 				dw_mci_read_data_pio(host, false);
2806f95f3850SWill Newton 		}
2807f95f3850SWill Newton 
2808f95f3850SWill Newton 		if (pending & SDMMC_INT_TXDR) {
2809f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
2810b40af3aaSJames Hogan 			if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
2811f95f3850SWill Newton 				dw_mci_write_data_pio(host);
2812f95f3850SWill Newton 		}
2813f95f3850SWill Newton 
2814f95f3850SWill Newton 		if (pending & SDMMC_INT_CMD_DONE) {
28159f7d4c91STian Tao 			spin_lock(&host->irq_lock);
28168892b705SDouglas Anderson 
2817f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
2818182c9081SSeungwon Jeon 			dw_mci_cmd_interrupt(host, pending);
28198892b705SDouglas Anderson 
28209f7d4c91STian Tao 			spin_unlock(&host->irq_lock);
2821f95f3850SWill Newton 		}
2822f95f3850SWill Newton 
2823f95f3850SWill Newton 		if (pending & SDMMC_INT_CD) {
2824f95f3850SWill Newton 			mci_writel(host, RINTSTS, SDMMC_INT_CD);
28256130e7a9SDoug Anderson 			dw_mci_handle_cd(host);
2826f95f3850SWill Newton 		}
2827f95f3850SWill Newton 
282876756234SAddy Ke 		if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
282976756234SAddy Ke 			mci_writel(host, RINTSTS,
283076756234SAddy Ke 				   SDMMC_INT_SDIO(slot->sdio_id));
283132dba737SUlf Hansson 			__dw_mci_enable_sdio_irq(slot, 0);
283232dba737SUlf Hansson 			sdio_signal_irq(slot->mmc);
28331a5c8e1fSShashidhar Hiremath 		}
28341a5c8e1fSShashidhar Hiremath 
28351fb5f68aSMarkos Chandras 	}
2836f95f3850SWill Newton 
28373fc7eaefSShawn Lin 	if (host->use_dma != TRANS_MODE_IDMAC)
28383fc7eaefSShawn Lin 		return IRQ_HANDLED;
28393fc7eaefSShawn Lin 
28403fc7eaefSShawn Lin 	/* Handle IDMA interrupts */
284169d99fdcSPrabu Thangamuthu 	if (host->dma_64bit_address == 1) {
284269d99fdcSPrabu Thangamuthu 		pending = mci_readl(host, IDSTS64);
284369d99fdcSPrabu Thangamuthu 		if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
284469d99fdcSPrabu Thangamuthu 			mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
284569d99fdcSPrabu Thangamuthu 							SDMMC_IDMAC_INT_RI);
284669d99fdcSPrabu Thangamuthu 			mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2847faecf411SShawn Lin 			if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
28483fc7eaefSShawn Lin 				host->dma_ops->complete((void *)host);
284969d99fdcSPrabu Thangamuthu 		}
285069d99fdcSPrabu Thangamuthu 	} else {
2851f95f3850SWill Newton 		pending = mci_readl(host, IDSTS);
2852f95f3850SWill Newton 		if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
285369d99fdcSPrabu Thangamuthu 			mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
285469d99fdcSPrabu Thangamuthu 							SDMMC_IDMAC_INT_RI);
2855f95f3850SWill Newton 			mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2856faecf411SShawn Lin 			if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
28573fc7eaefSShawn Lin 				host->dma_ops->complete((void *)host);
2858f95f3850SWill Newton 		}
285969d99fdcSPrabu Thangamuthu 	}
2860f95f3850SWill Newton 
2861f95f3850SWill Newton 	return IRQ_HANDLED;
2862f95f3850SWill Newton }
2863f95f3850SWill Newton 
dw_mci_init_slot_caps(struct dw_mci_slot * slot)2864a4faa492SShawn Lin static int dw_mci_init_slot_caps(struct dw_mci_slot *slot)
2865a4faa492SShawn Lin {
2866a4faa492SShawn Lin 	struct dw_mci *host = slot->host;
2867a4faa492SShawn Lin 	const struct dw_mci_drv_data *drv_data = host->drv_data;
2868a4faa492SShawn Lin 	struct mmc_host *mmc = slot->mmc;
2869a4faa492SShawn Lin 	int ctrl_id;
2870a4faa492SShawn Lin 
2871a4faa492SShawn Lin 	if (host->pdata->caps)
2872a4faa492SShawn Lin 		mmc->caps = host->pdata->caps;
2873a4faa492SShawn Lin 
2874a4faa492SShawn Lin 	if (host->pdata->pm_caps)
2875a4faa492SShawn Lin 		mmc->pm_caps = host->pdata->pm_caps;
2876a4faa492SShawn Lin 
28770dc7a3ecSJohn Keeping 	if (drv_data)
28780dc7a3ecSJohn Keeping 		mmc->caps |= drv_data->common_caps;
28790dc7a3ecSJohn Keeping 
2880a4faa492SShawn Lin 	if (host->dev->of_node) {
2881a4faa492SShawn Lin 		ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2882a4faa492SShawn Lin 		if (ctrl_id < 0)
2883a4faa492SShawn Lin 			ctrl_id = 0;
2884a4faa492SShawn Lin 	} else {
2885a4faa492SShawn Lin 		ctrl_id = to_platform_device(host->dev)->id;
2886a4faa492SShawn Lin 	}
28870d84b9e5SShawn Lin 
28880d84b9e5SShawn Lin 	if (drv_data && drv_data->caps) {
28890d84b9e5SShawn Lin 		if (ctrl_id >= drv_data->num_caps) {
28900d84b9e5SShawn Lin 			dev_err(host->dev, "invalid controller id %d\n",
28910d84b9e5SShawn Lin 				ctrl_id);
28920d84b9e5SShawn Lin 			return -EINVAL;
28930d84b9e5SShawn Lin 		}
2894a4faa492SShawn Lin 		mmc->caps |= drv_data->caps[ctrl_id];
28950d84b9e5SShawn Lin 	}
2896a4faa492SShawn Lin 
2897a4faa492SShawn Lin 	if (host->pdata->caps2)
2898a4faa492SShawn Lin 		mmc->caps2 = host->pdata->caps2;
2899a4faa492SShawn Lin 
2900c4313e75SPeter Geis 	/* if host has set a minimum_freq, we should respect it */
2901c4313e75SPeter Geis 	if (host->minimum_speed)
2902c4313e75SPeter Geis 		mmc->f_min = host->minimum_speed;
2903c4313e75SPeter Geis 	else
290486b93a48SJaehoon Chung 		mmc->f_min = DW_MCI_FREQ_MIN;
2905c4313e75SPeter Geis 
290686b93a48SJaehoon Chung 	if (!mmc->f_max)
290786b93a48SJaehoon Chung 		mmc->f_max = DW_MCI_FREQ_MAX;
290886b93a48SJaehoon Chung 
2909a4faa492SShawn Lin 	/* Process SDIO IRQs through the sdio_irq_work. */
2910a4faa492SShawn Lin 	if (mmc->caps & MMC_CAP_SDIO_IRQ)
2911a4faa492SShawn Lin 		mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2912a4faa492SShawn Lin 
2913a4faa492SShawn Lin 	return 0;
2914a4faa492SShawn Lin }
2915a4faa492SShawn Lin 
dw_mci_init_slot(struct dw_mci * host)2916e4a65ef7SJaehoon Chung static int dw_mci_init_slot(struct dw_mci *host)
2917f95f3850SWill Newton {
2918f95f3850SWill Newton 	struct mmc_host *mmc;
2919f95f3850SWill Newton 	struct dw_mci_slot *slot;
2920a4faa492SShawn Lin 	int ret;
2921f95f3850SWill Newton 
29224a90920cSThomas Abraham 	mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
2923f95f3850SWill Newton 	if (!mmc)
2924f95f3850SWill Newton 		return -ENOMEM;
2925f95f3850SWill Newton 
2926f95f3850SWill Newton 	slot = mmc_priv(mmc);
2927e4a65ef7SJaehoon Chung 	slot->id = 0;
2928e4a65ef7SJaehoon Chung 	slot->sdio_id = host->sdio_id0 + slot->id;
2929f95f3850SWill Newton 	slot->mmc = mmc;
2930f95f3850SWill Newton 	slot->host = host;
2931b23475faSJaehoon Chung 	host->slot = slot;
2932f95f3850SWill Newton 
2933f95f3850SWill Newton 	mmc->ops = &dw_mci_ops;
2934f95f3850SWill Newton 
293551da2240SYuvaraj CD 	/*if there are external regulators, get them*/
293651da2240SYuvaraj CD 	ret = mmc_regulator_get_supply(mmc);
29370f3a47b8SWolfram Sang 	if (ret)
29383cf890fcSDoug Anderson 		goto err_host_allocated;
293951da2240SYuvaraj CD 
294051da2240SYuvaraj CD 	if (!mmc->ocr_avail)
2941f95f3850SWill Newton 		mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2942f95f3850SWill Newton 
29433cf890fcSDoug Anderson 	ret = mmc_of_parse(mmc);
29443cf890fcSDoug Anderson 	if (ret)
29453cf890fcSDoug Anderson 		goto err_host_allocated;
2946f95f3850SWill Newton 
2947a4faa492SShawn Lin 	ret = dw_mci_init_slot_caps(slot);
2948a4faa492SShawn Lin 	if (ret)
2949a4faa492SShawn Lin 		goto err_host_allocated;
295032dba737SUlf Hansson 
2951f95f3850SWill Newton 	/* Useful defaults if platform data is unset. */
29523fc7eaefSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC) {
2953a39e5746SJaehoon Chung 		mmc->max_segs = host->ring_size;
2954225faf87SJaehoon Chung 		mmc->max_blk_size = 65535;
2955575c319dSHeiko Stuebner 		mmc->max_seg_size = 0x1000;
29561a25b1b4SSeungwon Jeon 		mmc->max_req_size = mmc->max_seg_size * host->ring_size;
29571a25b1b4SSeungwon Jeon 		mmc->max_blk_count = mmc->max_req_size / 512;
29583fc7eaefSShawn Lin 	} else if (host->use_dma == TRANS_MODE_EDMAC) {
29593fc7eaefSShawn Lin 		mmc->max_segs = 64;
2960225faf87SJaehoon Chung 		mmc->max_blk_size = 65535;
29613fc7eaefSShawn Lin 		mmc->max_blk_count = 65535;
29623fc7eaefSShawn Lin 		mmc->max_req_size =
29633fc7eaefSShawn Lin 				mmc->max_blk_size * mmc->max_blk_count;
29643fc7eaefSShawn Lin 		mmc->max_seg_size = mmc->max_req_size;
2965575c319dSHeiko Stuebner 	} else {
29663fc7eaefSShawn Lin 		/* TRANS_MODE_PIO */
2967f95f3850SWill Newton 		mmc->max_segs = 64;
2968225faf87SJaehoon Chung 		mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
2969f95f3850SWill Newton 		mmc->max_blk_count = 512;
2970575c319dSHeiko Stuebner 		mmc->max_req_size = mmc->max_blk_size *
2971575c319dSHeiko Stuebner 				    mmc->max_blk_count;
2972f95f3850SWill Newton 		mmc->max_seg_size = mmc->max_req_size;
2973575c319dSHeiko Stuebner 	}
2974f95f3850SWill Newton 
2975c0834a58SShawn Lin 	dw_mci_get_cd(mmc);
2976ae0eb348SJaehoon Chung 
29770cea529dSJaehoon Chung 	ret = mmc_add_host(mmc);
29780cea529dSJaehoon Chung 	if (ret)
29793cf890fcSDoug Anderson 		goto err_host_allocated;
2980f95f3850SWill Newton 
2981f95f3850SWill Newton #if defined(CONFIG_DEBUG_FS)
2982f95f3850SWill Newton 	dw_mci_init_debugfs(slot);
2983f95f3850SWill Newton #endif
2984f95f3850SWill Newton 
2985f95f3850SWill Newton 	return 0;
2986800d78bfSThomas Abraham 
29873cf890fcSDoug Anderson err_host_allocated:
2988800d78bfSThomas Abraham 	mmc_free_host(mmc);
298951da2240SYuvaraj CD 	return ret;
2990f95f3850SWill Newton }
2991f95f3850SWill Newton 
dw_mci_cleanup_slot(struct dw_mci_slot * slot)2992e4a65ef7SJaehoon Chung static void dw_mci_cleanup_slot(struct dw_mci_slot *slot)
2993f95f3850SWill Newton {
2994f95f3850SWill Newton 	/* Debugfs stuff is cleaned up by mmc core */
2995f95f3850SWill Newton 	mmc_remove_host(slot->mmc);
2996b23475faSJaehoon Chung 	slot->host->slot = NULL;
2997f95f3850SWill Newton 	mmc_free_host(slot->mmc);
2998f95f3850SWill Newton }
2999f95f3850SWill Newton 
dw_mci_init_dma(struct dw_mci * host)3000f95f3850SWill Newton static void dw_mci_init_dma(struct dw_mci *host)
3001f95f3850SWill Newton {
300269d99fdcSPrabu Thangamuthu 	int addr_config;
30033fc7eaefSShawn Lin 	struct device *dev = host->dev;
30043fc7eaefSShawn Lin 
30053fc7eaefSShawn Lin 	/*
30063fc7eaefSShawn Lin 	* Check tansfer mode from HCON[17:16]
30073fc7eaefSShawn Lin 	* Clear the ambiguous description of dw_mmc databook:
30083fc7eaefSShawn Lin 	* 2b'00: No DMA Interface -> Actually means using Internal DMA block
30093fc7eaefSShawn Lin 	* 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
30103fc7eaefSShawn Lin 	* 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
30113fc7eaefSShawn Lin 	* 2b'11: Non DW DMA Interface -> pio only
30123fc7eaefSShawn Lin 	* Compared to DesignWare DMA Interface, Generic DMA Interface has a
30133fc7eaefSShawn Lin 	* simpler request/acknowledge handshake mechanism and both of them
30143fc7eaefSShawn Lin 	* are regarded as external dma master for dw_mmc.
30153fc7eaefSShawn Lin 	*/
30163fc7eaefSShawn Lin 	host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
30173fc7eaefSShawn Lin 	if (host->use_dma == DMA_INTERFACE_IDMA) {
30183fc7eaefSShawn Lin 		host->use_dma = TRANS_MODE_IDMAC;
30193fc7eaefSShawn Lin 	} else if (host->use_dma == DMA_INTERFACE_DWDMA ||
30203fc7eaefSShawn Lin 		   host->use_dma == DMA_INTERFACE_GDMA) {
30213fc7eaefSShawn Lin 		host->use_dma = TRANS_MODE_EDMAC;
30223fc7eaefSShawn Lin 	} else {
30233fc7eaefSShawn Lin 		goto no_dma;
30243fc7eaefSShawn Lin 	}
30253fc7eaefSShawn Lin 
30263fc7eaefSShawn Lin 	/* Determine which DMA interface to use */
30273fc7eaefSShawn Lin 	if (host->use_dma == TRANS_MODE_IDMAC) {
30283fc7eaefSShawn Lin 		/*
30293fc7eaefSShawn Lin 		* Check ADDR_CONFIG bit in HCON to find
30303fc7eaefSShawn Lin 		* IDMAC address bus width
30313fc7eaefSShawn Lin 		*/
303270692752SShawn Lin 		addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
303369d99fdcSPrabu Thangamuthu 
303469d99fdcSPrabu Thangamuthu 		if (addr_config == 1) {
303569d99fdcSPrabu Thangamuthu 			/* host supports IDMAC in 64-bit address mode */
303669d99fdcSPrabu Thangamuthu 			host->dma_64bit_address = 1;
30373fc7eaefSShawn Lin 			dev_info(host->dev,
30383fc7eaefSShawn Lin 				 "IDMAC supports 64-bit address mode.\n");
303969d99fdcSPrabu Thangamuthu 			if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
30403fc7eaefSShawn Lin 				dma_set_coherent_mask(host->dev,
30413fc7eaefSShawn Lin 						      DMA_BIT_MASK(64));
304269d99fdcSPrabu Thangamuthu 		} else {
304369d99fdcSPrabu Thangamuthu 			/* host supports IDMAC in 32-bit address mode */
304469d99fdcSPrabu Thangamuthu 			host->dma_64bit_address = 0;
30453fc7eaefSShawn Lin 			dev_info(host->dev,
30463fc7eaefSShawn Lin 				 "IDMAC supports 32-bit address mode.\n");
304769d99fdcSPrabu Thangamuthu 		}
304869d99fdcSPrabu Thangamuthu 
3049f95f3850SWill Newton 		/* Alloc memory for sg translation */
3050cc190d4cSShawn Lin 		host->sg_cpu = dmam_alloc_coherent(host->dev,
3051cc190d4cSShawn Lin 						   DESC_RING_BUF_SZ,
3052f95f3850SWill Newton 						   &host->sg_dma, GFP_KERNEL);
3053f95f3850SWill Newton 		if (!host->sg_cpu) {
30543fc7eaefSShawn Lin 			dev_err(host->dev,
30553fc7eaefSShawn Lin 				"%s: could not alloc DMA memory\n",
3056f95f3850SWill Newton 				__func__);
3057f95f3850SWill Newton 			goto no_dma;
3058f95f3850SWill Newton 		}
3059f95f3850SWill Newton 
3060f95f3850SWill Newton 		host->dma_ops = &dw_mci_idmac_ops;
306100956ea3SSeungwon Jeon 		dev_info(host->dev, "Using internal DMA controller.\n");
30623fc7eaefSShawn Lin 	} else {
30633fc7eaefSShawn Lin 		/* TRANS_MODE_EDMAC: check dma bindings again */
306443fa33aaSAndy Shevchenko 		if ((device_property_string_array_count(dev, "dma-names") < 0) ||
3065852ff5feSDavid Woods 		    !device_property_present(dev, "dmas")) {
3066f95f3850SWill Newton 			goto no_dma;
30673fc7eaefSShawn Lin 		}
30683fc7eaefSShawn Lin 		host->dma_ops = &dw_mci_edmac_ops;
30693fc7eaefSShawn Lin 		dev_info(host->dev, "Using external DMA controller.\n");
30703fc7eaefSShawn Lin 	}
3071f95f3850SWill Newton 
3072e1631f98SJaehoon Chung 	if (host->dma_ops->init && host->dma_ops->start &&
3073e1631f98SJaehoon Chung 	    host->dma_ops->stop && host->dma_ops->cleanup) {
3074f95f3850SWill Newton 		if (host->dma_ops->init(host)) {
30750e3a22c0SShawn Lin 			dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
30760e3a22c0SShawn Lin 				__func__);
3077f95f3850SWill Newton 			goto no_dma;
3078f95f3850SWill Newton 		}
3079f95f3850SWill Newton 	} else {
30804a90920cSThomas Abraham 		dev_err(host->dev, "DMA initialization not found.\n");
3081f95f3850SWill Newton 		goto no_dma;
3082f95f3850SWill Newton 	}
3083f95f3850SWill Newton 
3084f95f3850SWill Newton 	return;
3085f95f3850SWill Newton 
3086f95f3850SWill Newton no_dma:
30874a90920cSThomas Abraham 	dev_info(host->dev, "Using PIO mode.\n");
30883fc7eaefSShawn Lin 	host->use_dma = TRANS_MODE_PIO;
3089f95f3850SWill Newton }
3090f95f3850SWill Newton 
dw_mci_cmd11_timer(struct timer_list * t)309137977729SKees Cook static void dw_mci_cmd11_timer(struct timer_list *t)
30925c935165SDoug Anderson {
309337977729SKees Cook 	struct dw_mci *host = from_timer(host, t, cmd11_timer);
30945c935165SDoug Anderson 
3095fd674198SDoug Anderson 	if (host->state != STATE_SENDING_CMD11) {
3096fd674198SDoug Anderson 		dev_warn(host->dev, "Unexpected CMD11 timeout\n");
3097fd674198SDoug Anderson 		return;
3098fd674198SDoug Anderson 	}
30995c935165SDoug Anderson 
31005c935165SDoug Anderson 	host->cmd_status = SDMMC_INT_RTO;
31015c935165SDoug Anderson 	set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
31025c935165SDoug Anderson 	tasklet_schedule(&host->tasklet);
31035c935165SDoug Anderson }
31045c935165SDoug Anderson 
dw_mci_cto_timer(struct timer_list * t)310537977729SKees Cook static void dw_mci_cto_timer(struct timer_list *t)
310603de1921SAddy Ke {
310737977729SKees Cook 	struct dw_mci *host = from_timer(host, t, cto_timer);
31088892b705SDouglas Anderson 	unsigned long irqflags;
31098892b705SDouglas Anderson 	u32 pending;
311003de1921SAddy Ke 
31118892b705SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
31128892b705SDouglas Anderson 
31138892b705SDouglas Anderson 	/*
31148892b705SDouglas Anderson 	 * If somehow we have very bad interrupt latency it's remotely possible
31158892b705SDouglas Anderson 	 * that the timer could fire while the interrupt is still pending or
31168892b705SDouglas Anderson 	 * while the interrupt is midway through running.  Let's be paranoid
31178892b705SDouglas Anderson 	 * and detect those two cases.  Note that this is paranoia is somewhat
31188892b705SDouglas Anderson 	 * justified because in this function we don't actually cancel the
31198892b705SDouglas Anderson 	 * pending command in the controller--we just assume it will never come.
31208892b705SDouglas Anderson 	 */
31218892b705SDouglas Anderson 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
31228892b705SDouglas Anderson 	if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) {
31238892b705SDouglas Anderson 		/* The interrupt should fire; no need to act but we can warn */
31248892b705SDouglas Anderson 		dev_warn(host->dev, "Unexpected interrupt latency\n");
31258892b705SDouglas Anderson 		goto exit;
31268892b705SDouglas Anderson 	}
31278892b705SDouglas Anderson 	if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) {
31288892b705SDouglas Anderson 		/* Presumably interrupt handler couldn't delete the timer */
31298892b705SDouglas Anderson 		dev_warn(host->dev, "CTO timeout when already completed\n");
31308892b705SDouglas Anderson 		goto exit;
31318892b705SDouglas Anderson 	}
31328892b705SDouglas Anderson 
31338892b705SDouglas Anderson 	/*
31348892b705SDouglas Anderson 	 * Continued paranoia to make sure we're in the state we expect.
31358892b705SDouglas Anderson 	 * This paranoia isn't really justified but it seems good to be safe.
31368892b705SDouglas Anderson 	 */
313703de1921SAddy Ke 	switch (host->state) {
313803de1921SAddy Ke 	case STATE_SENDING_CMD11:
313903de1921SAddy Ke 	case STATE_SENDING_CMD:
314003de1921SAddy Ke 	case STATE_SENDING_STOP:
314103de1921SAddy Ke 		/*
314203de1921SAddy Ke 		 * If CMD_DONE interrupt does NOT come in sending command
314303de1921SAddy Ke 		 * state, we should notify the driver to terminate current
314403de1921SAddy Ke 		 * transfer and report a command timeout to the core.
314503de1921SAddy Ke 		 */
314603de1921SAddy Ke 		host->cmd_status = SDMMC_INT_RTO;
314703de1921SAddy Ke 		set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
314803de1921SAddy Ke 		tasklet_schedule(&host->tasklet);
314903de1921SAddy Ke 		break;
315003de1921SAddy Ke 	default:
315103de1921SAddy Ke 		dev_warn(host->dev, "Unexpected command timeout, state %d\n",
315203de1921SAddy Ke 			 host->state);
315303de1921SAddy Ke 		break;
315403de1921SAddy Ke 	}
31558892b705SDouglas Anderson 
31568892b705SDouglas Anderson exit:
31578892b705SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
315803de1921SAddy Ke }
315903de1921SAddy Ke 
dw_mci_dto_timer(struct timer_list * t)316037977729SKees Cook static void dw_mci_dto_timer(struct timer_list *t)
316157e10486SAddy Ke {
316237977729SKees Cook 	struct dw_mci *host = from_timer(host, t, dto_timer);
316393c23ae3SDouglas Anderson 	unsigned long irqflags;
316493c23ae3SDouglas Anderson 	u32 pending;
316557e10486SAddy Ke 
316693c23ae3SDouglas Anderson 	spin_lock_irqsave(&host->irq_lock, irqflags);
316793c23ae3SDouglas Anderson 
316893c23ae3SDouglas Anderson 	/*
316993c23ae3SDouglas Anderson 	 * The DTO timer is much longer than the CTO timer, so it's even less
317093c23ae3SDouglas Anderson 	 * likely that we'll these cases, but it pays to be paranoid.
317193c23ae3SDouglas Anderson 	 */
317293c23ae3SDouglas Anderson 	pending = mci_readl(host, MINTSTS); /* read-only mask reg */
317393c23ae3SDouglas Anderson 	if (pending & SDMMC_INT_DATA_OVER) {
317493c23ae3SDouglas Anderson 		/* The interrupt should fire; no need to act but we can warn */
317593c23ae3SDouglas Anderson 		dev_warn(host->dev, "Unexpected data interrupt latency\n");
317693c23ae3SDouglas Anderson 		goto exit;
317793c23ae3SDouglas Anderson 	}
317893c23ae3SDouglas Anderson 	if (test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) {
317993c23ae3SDouglas Anderson 		/* Presumably interrupt handler couldn't delete the timer */
318093c23ae3SDouglas Anderson 		dev_warn(host->dev, "DTO timeout when already completed\n");
318193c23ae3SDouglas Anderson 		goto exit;
318293c23ae3SDouglas Anderson 	}
318393c23ae3SDouglas Anderson 
318493c23ae3SDouglas Anderson 	/*
318593c23ae3SDouglas Anderson 	 * Continued paranoia to make sure we're in the state we expect.
318693c23ae3SDouglas Anderson 	 * This paranoia isn't really justified but it seems good to be safe.
318793c23ae3SDouglas Anderson 	 */
318857e10486SAddy Ke 	switch (host->state) {
318957e10486SAddy Ke 	case STATE_SENDING_DATA:
319057e10486SAddy Ke 	case STATE_DATA_BUSY:
319157e10486SAddy Ke 		/*
319257e10486SAddy Ke 		 * If DTO interrupt does NOT come in sending data state,
319357e10486SAddy Ke 		 * we should notify the driver to terminate current transfer
319457e10486SAddy Ke 		 * and report a data timeout to the core.
319557e10486SAddy Ke 		 */
319657e10486SAddy Ke 		host->data_status = SDMMC_INT_DRTO;
319757e10486SAddy Ke 		set_bit(EVENT_DATA_ERROR, &host->pending_events);
319857e10486SAddy Ke 		set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
319957e10486SAddy Ke 		tasklet_schedule(&host->tasklet);
320057e10486SAddy Ke 		break;
320157e10486SAddy Ke 	default:
320293c23ae3SDouglas Anderson 		dev_warn(host->dev, "Unexpected data timeout, state %d\n",
320393c23ae3SDouglas Anderson 			 host->state);
320457e10486SAddy Ke 		break;
320557e10486SAddy Ke 	}
320693c23ae3SDouglas Anderson 
320793c23ae3SDouglas Anderson exit:
320893c23ae3SDouglas Anderson 	spin_unlock_irqrestore(&host->irq_lock, irqflags);
320957e10486SAddy Ke }
321057e10486SAddy Ke 
3211c91eab4bSThomas Abraham #ifdef CONFIG_OF
dw_mci_parse_dt(struct dw_mci * host)3212c91eab4bSThomas Abraham static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3213c91eab4bSThomas Abraham {
3214c91eab4bSThomas Abraham 	struct dw_mci_board *pdata;
3215c91eab4bSThomas Abraham 	struct device *dev = host->dev;
3216e95baf13SArnd Bergmann 	const struct dw_mci_drv_data *drv_data = host->drv_data;
3217e8cc37b8SShawn Lin 	int ret;
32183c6d89eaSDoug Anderson 	u32 clock_frequency;
3219c91eab4bSThomas Abraham 
3220c91eab4bSThomas Abraham 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3221bf3707eaSBeomho Seo 	if (!pdata)
3222c91eab4bSThomas Abraham 		return ERR_PTR(-ENOMEM);
3223c91eab4bSThomas Abraham 
3224d6786fefSGuodong Xu 	/* find reset controller when exist */
3225a93d6f31SPhilipp Zabel 	pdata->rstc = devm_reset_control_get_optional_exclusive(dev, "reset");
3226baf6fe40SPhilipp Zabel 	if (IS_ERR(pdata->rstc))
3227baf6fe40SPhilipp Zabel 		return ERR_CAST(pdata->rstc);
3228d6786fefSGuodong Xu 
3229852ff5feSDavid Woods 	if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth))
32300e3a22c0SShawn Lin 		dev_info(dev,
32310e3a22c0SShawn Lin 			 "fifo-depth property not found, using value of FIFOTH register as default\n");
3232c91eab4bSThomas Abraham 
3233852ff5feSDavid Woods 	device_property_read_u32(dev, "card-detect-delay",
3234852ff5feSDavid Woods 				 &pdata->detect_delay_ms);
3235c91eab4bSThomas Abraham 
3236852ff5feSDavid Woods 	device_property_read_u32(dev, "data-addr", &host->data_addr_override);
3237a0361c1aSJun Nie 
3238852ff5feSDavid Woods 	if (device_property_present(dev, "fifo-watermark-aligned"))
3239d6fced83SJun Nie 		host->wm_aligned = true;
3240d6fced83SJun Nie 
3241852ff5feSDavid Woods 	if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency))
32423c6d89eaSDoug Anderson 		pdata->bus_hz = clock_frequency;
32433c6d89eaSDoug Anderson 
3244cb27a843SJames Hogan 	if (drv_data && drv_data->parse_dt) {
3245cb27a843SJames Hogan 		ret = drv_data->parse_dt(host);
3246800d78bfSThomas Abraham 		if (ret)
3247800d78bfSThomas Abraham 			return ERR_PTR(ret);
3248800d78bfSThomas Abraham 	}
3249800d78bfSThomas Abraham 
3250c91eab4bSThomas Abraham 	return pdata;
3251c91eab4bSThomas Abraham }
3252c91eab4bSThomas Abraham 
3253c91eab4bSThomas Abraham #else /* CONFIG_OF */
dw_mci_parse_dt(struct dw_mci * host)3254c91eab4bSThomas Abraham static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3255c91eab4bSThomas Abraham {
3256c91eab4bSThomas Abraham 	return ERR_PTR(-EINVAL);
3257c91eab4bSThomas Abraham }
3258c91eab4bSThomas Abraham #endif /* CONFIG_OF */
3259c91eab4bSThomas Abraham 
dw_mci_enable_cd(struct dw_mci * host)3260fa0c3283SDoug Anderson static void dw_mci_enable_cd(struct dw_mci *host)
3261fa0c3283SDoug Anderson {
3262fa0c3283SDoug Anderson 	unsigned long irqflags;
3263fa0c3283SDoug Anderson 	u32 temp;
3264fa0c3283SDoug Anderson 
3265e8cc37b8SShawn Lin 	/*
3266e8cc37b8SShawn Lin 	 * No need for CD if all slots have a non-error GPIO
3267e8cc37b8SShawn Lin 	 * as well as broken card detection is found.
3268e8cc37b8SShawn Lin 	 */
3269e47c0b96SJaehoon Chung 	if (host->slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3270e8cc37b8SShawn Lin 		return;
3271fa0c3283SDoug Anderson 
3272e47c0b96SJaehoon Chung 	if (mmc_gpio_get_cd(host->slot->mmc) < 0) {
3273fa0c3283SDoug Anderson 		spin_lock_irqsave(&host->irq_lock, irqflags);
3274fa0c3283SDoug Anderson 		temp = mci_readl(host, INTMASK);
3275fa0c3283SDoug Anderson 		temp  |= SDMMC_INT_CD;
3276fa0c3283SDoug Anderson 		mci_writel(host, INTMASK, temp);
3277fa0c3283SDoug Anderson 		spin_unlock_irqrestore(&host->irq_lock, irqflags);
3278fa0c3283SDoug Anderson 	}
327958870241SJaehoon Chung }
3280fa0c3283SDoug Anderson 
dw_mci_probe(struct dw_mci * host)328162ca8034SShashidhar Hiremath int dw_mci_probe(struct dw_mci *host)
3282f95f3850SWill Newton {
3283e95baf13SArnd Bergmann 	const struct dw_mci_drv_data *drv_data = host->drv_data;
328462ca8034SShashidhar Hiremath 	int width, i, ret = 0;
3285f95f3850SWill Newton 	u32 fifo_size;
3286f95f3850SWill Newton 
3287c91eab4bSThomas Abraham 	if (!host->pdata) {
3288c91eab4bSThomas Abraham 		host->pdata = dw_mci_parse_dt(host);
3289308d2722SKrzysztof Kozlowski 		if (IS_ERR(host->pdata))
3290308d2722SKrzysztof Kozlowski 			return dev_err_probe(host->dev, PTR_ERR(host->pdata),
3291308d2722SKrzysztof Kozlowski 					     "platform data not available\n");
3292f95f3850SWill Newton 	}
3293f95f3850SWill Newton 
3294780f22afSSeungwon Jeon 	host->biu_clk = devm_clk_get(host->dev, "biu");
3295f90a0612SThomas Abraham 	if (IS_ERR(host->biu_clk)) {
3296f90a0612SThomas Abraham 		dev_dbg(host->dev, "biu clock not available\n");
3297f90a0612SThomas Abraham 	} else {
3298f90a0612SThomas Abraham 		ret = clk_prepare_enable(host->biu_clk);
3299f90a0612SThomas Abraham 		if (ret) {
3300f90a0612SThomas Abraham 			dev_err(host->dev, "failed to enable biu clock\n");
3301f90a0612SThomas Abraham 			return ret;
3302f90a0612SThomas Abraham 		}
3303f95f3850SWill Newton 	}
3304f95f3850SWill Newton 
3305780f22afSSeungwon Jeon 	host->ciu_clk = devm_clk_get(host->dev, "ciu");
3306f90a0612SThomas Abraham 	if (IS_ERR(host->ciu_clk)) {
3307f90a0612SThomas Abraham 		dev_dbg(host->dev, "ciu clock not available\n");
33083c6d89eaSDoug Anderson 		host->bus_hz = host->pdata->bus_hz;
3309f90a0612SThomas Abraham 	} else {
3310f90a0612SThomas Abraham 		ret = clk_prepare_enable(host->ciu_clk);
3311f90a0612SThomas Abraham 		if (ret) {
3312f90a0612SThomas Abraham 			dev_err(host->dev, "failed to enable ciu clock\n");
3313f90a0612SThomas Abraham 			goto err_clk_biu;
3314f90a0612SThomas Abraham 		}
3315f90a0612SThomas Abraham 
33163c6d89eaSDoug Anderson 		if (host->pdata->bus_hz) {
33173c6d89eaSDoug Anderson 			ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
33183c6d89eaSDoug Anderson 			if (ret)
33193c6d89eaSDoug Anderson 				dev_warn(host->dev,
3320612de4c1SJaehoon Chung 					 "Unable to set bus rate to %uHz\n",
33213c6d89eaSDoug Anderson 					 host->pdata->bus_hz);
33223c6d89eaSDoug Anderson 		}
3323f90a0612SThomas Abraham 		host->bus_hz = clk_get_rate(host->ciu_clk);
33243c6d89eaSDoug Anderson 	}
3325f90a0612SThomas Abraham 
3326612de4c1SJaehoon Chung 	if (!host->bus_hz) {
3327612de4c1SJaehoon Chung 		dev_err(host->dev,
3328612de4c1SJaehoon Chung 			"Platform data must supply bus speed\n");
3329612de4c1SJaehoon Chung 		ret = -ENODEV;
3330612de4c1SJaehoon Chung 		goto err_clk_ciu;
3331612de4c1SJaehoon Chung 	}
3332612de4c1SJaehoon Chung 
3333baf6fe40SPhilipp Zabel 	if (host->pdata->rstc) {
3334941e372dSliwei 		reset_control_assert(host->pdata->rstc);
3335941e372dSliwei 		usleep_range(10, 50);
3336941e372dSliwei 		reset_control_deassert(host->pdata->rstc);
3337941e372dSliwei 	}
3338941e372dSliwei 
3339002f0d5cSYuvaraj Kumar C D 	if (drv_data && drv_data->init) {
3340002f0d5cSYuvaraj Kumar C D 		ret = drv_data->init(host);
3341002f0d5cSYuvaraj Kumar C D 		if (ret) {
3342002f0d5cSYuvaraj Kumar C D 			dev_err(host->dev,
3343002f0d5cSYuvaraj Kumar C D 				"implementation specific init failed\n");
3344002f0d5cSYuvaraj Kumar C D 			goto err_clk_ciu;
3345002f0d5cSYuvaraj Kumar C D 		}
3346002f0d5cSYuvaraj Kumar C D 	}
3347002f0d5cSYuvaraj Kumar C D 
334837977729SKees Cook 	timer_setup(&host->cmd11_timer, dw_mci_cmd11_timer, 0);
334937977729SKees Cook 	timer_setup(&host->cto_timer, dw_mci_cto_timer, 0);
335037977729SKees Cook 	timer_setup(&host->dto_timer, dw_mci_dto_timer, 0);
335157e10486SAddy Ke 
3352f95f3850SWill Newton 	spin_lock_init(&host->lock);
3353f8c58c11SDoug Anderson 	spin_lock_init(&host->irq_lock);
3354f95f3850SWill Newton 	INIT_LIST_HEAD(&host->queue);
3355f95f3850SWill Newton 
33562b8ac062SVincent Whitchurch 	dw_mci_init_fault(host);
33572b8ac062SVincent Whitchurch 
3358f95f3850SWill Newton 	/*
3359f95f3850SWill Newton 	 * Get the host data width - this assumes that HCON has been set with
3360f95f3850SWill Newton 	 * the correct values.
3361f95f3850SWill Newton 	 */
336270692752SShawn Lin 	i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
3363f95f3850SWill Newton 	if (!i) {
3364f95f3850SWill Newton 		host->push_data = dw_mci_push_data16;
3365f95f3850SWill Newton 		host->pull_data = dw_mci_pull_data16;
3366f95f3850SWill Newton 		width = 16;
3367f95f3850SWill Newton 		host->data_shift = 1;
3368f95f3850SWill Newton 	} else if (i == 2) {
3369f95f3850SWill Newton 		host->push_data = dw_mci_push_data64;
3370f95f3850SWill Newton 		host->pull_data = dw_mci_pull_data64;
3371f95f3850SWill Newton 		width = 64;
3372f95f3850SWill Newton 		host->data_shift = 3;
3373f95f3850SWill Newton 	} else {
3374f95f3850SWill Newton 		/* Check for a reserved value, and warn if it is */
3375f95f3850SWill Newton 		WARN((i != 1),
3376f95f3850SWill Newton 		     "HCON reports a reserved host data width!\n"
3377f95f3850SWill Newton 		     "Defaulting to 32-bit access.\n");
3378f95f3850SWill Newton 		host->push_data = dw_mci_push_data32;
3379f95f3850SWill Newton 		host->pull_data = dw_mci_pull_data32;
3380f95f3850SWill Newton 		width = 32;
3381f95f3850SWill Newton 		host->data_shift = 2;
3382f95f3850SWill Newton 	}
3383f95f3850SWill Newton 
3384f95f3850SWill Newton 	/* Reset all blocks */
33853744415cSShawn Lin 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
33863744415cSShawn Lin 		ret = -ENODEV;
33873744415cSShawn Lin 		goto err_clk_ciu;
33883744415cSShawn Lin 	}
3389141a712aSSeungwon Jeon 
3390141a712aSSeungwon Jeon 	host->dma_ops = host->pdata->dma_ops;
3391141a712aSSeungwon Jeon 	dw_mci_init_dma(host);
3392f95f3850SWill Newton 
3393f95f3850SWill Newton 	/* Clear the interrupts for the host controller */
3394f95f3850SWill Newton 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3395f95f3850SWill Newton 	mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3396f95f3850SWill Newton 
3397f95f3850SWill Newton 	/* Put in max timeout */
3398f95f3850SWill Newton 	mci_writel(host, TMOUT, 0xFFFFFFFF);
3399f95f3850SWill Newton 
3400f95f3850SWill Newton 	/*
3401f95f3850SWill Newton 	 * FIFO threshold settings  RxMark  = fifo_size / 2 - 1,
3402f95f3850SWill Newton 	 *                          Tx Mark = fifo_size / 2 DMA Size = 8
3403f95f3850SWill Newton 	 */
3404b86d8253SJames Hogan 	if (!host->pdata->fifo_depth) {
3405b86d8253SJames Hogan 		/*
3406b86d8253SJames Hogan 		 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3407b86d8253SJames Hogan 		 * have been overwritten by the bootloader, just like we're
3408b86d8253SJames Hogan 		 * about to do, so if you know the value for your hardware, you
3409b86d8253SJames Hogan 		 * should put it in the platform data.
3410b86d8253SJames Hogan 		 */
3411f95f3850SWill Newton 		fifo_size = mci_readl(host, FIFOTH);
34128234e869SJaehoon Chung 		fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
3413b86d8253SJames Hogan 	} else {
3414b86d8253SJames Hogan 		fifo_size = host->pdata->fifo_depth;
3415b86d8253SJames Hogan 	}
3416b86d8253SJames Hogan 	host->fifo_depth = fifo_size;
341752426899SSeungwon Jeon 	host->fifoth_val =
341852426899SSeungwon Jeon 		SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
3419e61cf118SJaehoon Chung 	mci_writel(host, FIFOTH, host->fifoth_val);
3420f95f3850SWill Newton 
3421f95f3850SWill Newton 	/* disable clock to CIU */
3422f95f3850SWill Newton 	mci_writel(host, CLKENA, 0);
3423f95f3850SWill Newton 	mci_writel(host, CLKSRC, 0);
3424f95f3850SWill Newton 
342563008768SJames Hogan 	/*
342663008768SJames Hogan 	 * In 2.40a spec, Data offset is changed.
342763008768SJames Hogan 	 * Need to check the version-id and set data-offset for DATA register.
342863008768SJames Hogan 	 */
342963008768SJames Hogan 	host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
343063008768SJames Hogan 	dev_info(host->dev, "Version ID is %04x\n", host->verid);
343163008768SJames Hogan 
3432a0361c1aSJun Nie 	if (host->data_addr_override)
3433a0361c1aSJun Nie 		host->fifo_reg = host->regs + host->data_addr_override;
3434a0361c1aSJun Nie 	else if (host->verid < DW_MMC_240A)
343576184ac1SBen Dooks 		host->fifo_reg = host->regs + DATA_OFFSET;
343663008768SJames Hogan 	else
343776184ac1SBen Dooks 		host->fifo_reg = host->regs + DATA_240A_OFFSET;
343863008768SJames Hogan 
34396078df15SEmil Renner Berthing 	tasklet_setup(&host->tasklet, dw_mci_tasklet_func);
3440780f22afSSeungwon Jeon 	ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3441780f22afSSeungwon Jeon 			       host->irq_flags, "dw-mci", host);
3442f95f3850SWill Newton 	if (ret)
34436130e7a9SDoug Anderson 		goto err_dmaunmap;
3444f95f3850SWill Newton 
3445d30a8f7bSJaehoon Chung 	/*
3446fa0c3283SDoug Anderson 	 * Enable interrupts for command done, data over, data empty,
34472da1d7f2SYuvaraj CD 	 * receive ready and error such as transmit, receive timeout, crc error
34482da1d7f2SYuvaraj CD 	 */
34492da1d7f2SYuvaraj CD 	mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
34502da1d7f2SYuvaraj CD 		   SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3451fa0c3283SDoug Anderson 		   DW_MCI_ERROR_FLAGS);
34520e3a22c0SShawn Lin 	/* Enable mci interrupt */
34530e3a22c0SShawn Lin 	mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
34542da1d7f2SYuvaraj CD 
34550e3a22c0SShawn Lin 	dev_info(host->dev,
34560e3a22c0SShawn Lin 		 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
34572da1d7f2SYuvaraj CD 		 host->irq, width, fifo_size);
34582da1d7f2SYuvaraj CD 
3459f95f3850SWill Newton 	/* We need at least one slot to succeed */
3460e4a65ef7SJaehoon Chung 	ret = dw_mci_init_slot(host);
346158870241SJaehoon Chung 	if (ret) {
34621c2215b7SThomas Abraham 		dev_dbg(host->dev, "slot %d init failed\n", i);
34636130e7a9SDoug Anderson 		goto err_dmaunmap;
3464f95f3850SWill Newton 	}
3465f95f3850SWill Newton 
3466b793f658SDoug Anderson 	/* Now that slots are all setup, we can enable card detect */
3467b793f658SDoug Anderson 	dw_mci_enable_cd(host);
3468b793f658SDoug Anderson 
3469f95f3850SWill Newton 	return 0;
3470f95f3850SWill Newton 
3471f95f3850SWill Newton err_dmaunmap:
3472f95f3850SWill Newton 	if (host->use_dma && host->dma_ops->exit)
3473f95f3850SWill Newton 		host->dma_ops->exit(host);
3474f90a0612SThomas Abraham 
3475d6786fefSGuodong Xu 	reset_control_assert(host->pdata->rstc);
3476d6786fefSGuodong Xu 
3477f90a0612SThomas Abraham err_clk_ciu:
3478f90a0612SThomas Abraham 	clk_disable_unprepare(host->ciu_clk);
3479780f22afSSeungwon Jeon 
3480f90a0612SThomas Abraham err_clk_biu:
3481f90a0612SThomas Abraham 	clk_disable_unprepare(host->biu_clk);
3482780f22afSSeungwon Jeon 
3483f95f3850SWill Newton 	return ret;
3484f95f3850SWill Newton }
348562ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_probe);
3486f95f3850SWill Newton 
dw_mci_remove(struct dw_mci * host)348762ca8034SShashidhar Hiremath void dw_mci_remove(struct dw_mci *host)
3488f95f3850SWill Newton {
3489e4a65ef7SJaehoon Chung 	dev_dbg(host->dev, "remove slot\n");
3490b23475faSJaehoon Chung 	if (host->slot)
3491e4a65ef7SJaehoon Chung 		dw_mci_cleanup_slot(host->slot);
3492f95f3850SWill Newton 
3493048fd7e6SPrabu Thangamuthu 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3494048fd7e6SPrabu Thangamuthu 	mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3495048fd7e6SPrabu Thangamuthu 
3496f95f3850SWill Newton 	/* disable clock to CIU */
3497f95f3850SWill Newton 	mci_writel(host, CLKENA, 0);
3498f95f3850SWill Newton 	mci_writel(host, CLKSRC, 0);
3499f95f3850SWill Newton 
3500f95f3850SWill Newton 	if (host->use_dma && host->dma_ops->exit)
3501f95f3850SWill Newton 		host->dma_ops->exit(host);
3502f95f3850SWill Newton 
3503d6786fefSGuodong Xu 	reset_control_assert(host->pdata->rstc);
3504d6786fefSGuodong Xu 
3505f90a0612SThomas Abraham 	clk_disable_unprepare(host->ciu_clk);
3506f90a0612SThomas Abraham 	clk_disable_unprepare(host->biu_clk);
3507f95f3850SWill Newton }
350862ca8034SShashidhar Hiremath EXPORT_SYMBOL(dw_mci_remove);
350962ca8034SShashidhar Hiremath 
351062ca8034SShashidhar Hiremath 
3511f95f3850SWill Newton 
3512e9ed8835SShawn Lin #ifdef CONFIG_PM
dw_mci_runtime_suspend(struct device * dev)3513ed24e1ffSShawn Lin int dw_mci_runtime_suspend(struct device *dev)
3514f95f3850SWill Newton {
3515ed24e1ffSShawn Lin 	struct dw_mci *host = dev_get_drvdata(dev);
3516ed24e1ffSShawn Lin 
35173fc7eaefSShawn Lin 	if (host->use_dma && host->dma_ops->exit)
35183fc7eaefSShawn Lin 		host->dma_ops->exit(host);
35193fc7eaefSShawn Lin 
3520ed24e1ffSShawn Lin 	clk_disable_unprepare(host->ciu_clk);
3521ed24e1ffSShawn Lin 
352242f989c0SJaehoon Chung 	if (host->slot &&
352342f989c0SJaehoon Chung 	    (mmc_can_gpio_cd(host->slot->mmc) ||
352442f989c0SJaehoon Chung 	     !mmc_card_is_removable(host->slot->mmc)))
3525ed24e1ffSShawn Lin 		clk_disable_unprepare(host->biu_clk);
3526ed24e1ffSShawn Lin 
3527f95f3850SWill Newton 	return 0;
3528f95f3850SWill Newton }
3529ed24e1ffSShawn Lin EXPORT_SYMBOL(dw_mci_runtime_suspend);
3530f95f3850SWill Newton 
dw_mci_runtime_resume(struct device * dev)3531ed24e1ffSShawn Lin int dw_mci_runtime_resume(struct device *dev)
3532f95f3850SWill Newton {
3533b23475faSJaehoon Chung 	int ret = 0;
3534ed24e1ffSShawn Lin 	struct dw_mci *host = dev_get_drvdata(dev);
3535f95f3850SWill Newton 
353642f989c0SJaehoon Chung 	if (host->slot &&
353742f989c0SJaehoon Chung 	    (mmc_can_gpio_cd(host->slot->mmc) ||
353842f989c0SJaehoon Chung 	     !mmc_card_is_removable(host->slot->mmc))) {
3539ed24e1ffSShawn Lin 		ret = clk_prepare_enable(host->biu_clk);
3540ed24e1ffSShawn Lin 		if (ret)
3541e61cf118SJaehoon Chung 			return ret;
3542e61cf118SJaehoon Chung 	}
3543e61cf118SJaehoon Chung 
3544ed24e1ffSShawn Lin 	ret = clk_prepare_enable(host->ciu_clk);
3545ed24e1ffSShawn Lin 	if (ret)
3546df9bcc2bSJoonyoung Shim 		goto err;
3547df9bcc2bSJoonyoung Shim 
3548df9bcc2bSJoonyoung Shim 	if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3549df9bcc2bSJoonyoung Shim 		clk_disable_unprepare(host->ciu_clk);
3550df9bcc2bSJoonyoung Shim 		ret = -ENODEV;
3551df9bcc2bSJoonyoung Shim 		goto err;
3552df9bcc2bSJoonyoung Shim 	}
3553ed24e1ffSShawn Lin 
35543bfe619dSJonathan Kliegman 	if (host->use_dma && host->dma_ops->init)
3555141a712aSSeungwon Jeon 		host->dma_ops->init(host);
3556141a712aSSeungwon Jeon 
355752426899SSeungwon Jeon 	/*
355852426899SSeungwon Jeon 	 * Restore the initial value at FIFOTH register
355952426899SSeungwon Jeon 	 * And Invalidate the prev_blksz with zero
356052426899SSeungwon Jeon 	 */
3561e61cf118SJaehoon Chung 	mci_writel(host, FIFOTH, host->fifoth_val);
356252426899SSeungwon Jeon 	host->prev_blksz = 0;
3563e61cf118SJaehoon Chung 
35642eb2944fSDoug Anderson 	/* Put in max timeout */
35652eb2944fSDoug Anderson 	mci_writel(host, TMOUT, 0xFFFFFFFF);
35662eb2944fSDoug Anderson 
3567e61cf118SJaehoon Chung 	mci_writel(host, RINTSTS, 0xFFFFFFFF);
3568e61cf118SJaehoon Chung 	mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3569e61cf118SJaehoon Chung 		   SDMMC_INT_TXDR | SDMMC_INT_RXDR |
3570fa0c3283SDoug Anderson 		   DW_MCI_ERROR_FLAGS);
3571e61cf118SJaehoon Chung 	mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3572e61cf118SJaehoon Chung 
35730e3a22c0SShawn Lin 
35744a835afdSWen Zhiwei 	if (host->slot && host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
3575e47c0b96SJaehoon Chung 		dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios);
3576e9748e03SZiyuan Xu 
3577e9748e03SZiyuan Xu 	/* Force setup bus to guarantee available clock output */
3578e47c0b96SJaehoon Chung 	dw_mci_setup_bus(host->slot, true);
3579fa0c3283SDoug Anderson 
35807c526608SUlf Hansson 	/* Re-enable SDIO interrupts. */
35817c526608SUlf Hansson 	if (sdio_irq_claimed(host->slot->mmc))
35827c526608SUlf Hansson 		__dw_mci_enable_sdio_irq(host->slot, 1);
35837c526608SUlf Hansson 
3584fa0c3283SDoug Anderson 	/* Now that slots are all setup, we can enable card detect */
3585fa0c3283SDoug Anderson 	dw_mci_enable_cd(host);
3586fa0c3283SDoug Anderson 
3587df9bcc2bSJoonyoung Shim 	return 0;
3588df9bcc2bSJoonyoung Shim 
3589df9bcc2bSJoonyoung Shim err:
359042f989c0SJaehoon Chung 	if (host->slot &&
359142f989c0SJaehoon Chung 	    (mmc_can_gpio_cd(host->slot->mmc) ||
359242f989c0SJaehoon Chung 	     !mmc_card_is_removable(host->slot->mmc)))
3593df9bcc2bSJoonyoung Shim 		clk_disable_unprepare(host->biu_clk);
3594df9bcc2bSJoonyoung Shim 
35951f5c51d7SShawn Lin 	return ret;
35961f5c51d7SShawn Lin }
3597e9ed8835SShawn Lin EXPORT_SYMBOL(dw_mci_runtime_resume);
3598e9ed8835SShawn Lin #endif /* CONFIG_PM */
35996fe8890dSJaehoon Chung 
dw_mci_init(void)3600f95f3850SWill Newton static int __init dw_mci_init(void)
3601f95f3850SWill Newton {
36028e1c4e4dSSachin Kamat 	pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
360362ca8034SShashidhar Hiremath 	return 0;
3604f95f3850SWill Newton }
3605f95f3850SWill Newton 
dw_mci_exit(void)3606f95f3850SWill Newton static void __exit dw_mci_exit(void)
3607f95f3850SWill Newton {
3608f95f3850SWill Newton }
3609f95f3850SWill Newton 
3610f95f3850SWill Newton module_init(dw_mci_init);
3611f95f3850SWill Newton module_exit(dw_mci_exit);
3612f95f3850SWill Newton 
3613f95f3850SWill Newton MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3614f95f3850SWill Newton MODULE_AUTHOR("NXP Semiconductor VietNam");
3615f95f3850SWill Newton MODULE_AUTHOR("Imagination Technologies Ltd");
3616f95f3850SWill Newton MODULE_LICENSE("GPL v2");
3617